forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcopilot-chat.ts
More file actions
176 lines (160 loc) · 4.79 KB
/
Copy pathcopilot-chat.ts
File metadata and controls
176 lines (160 loc) · 4.79 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import {
Component,
input,
ChangeDetectionStrategy,
ViewEncapsulation,
signal,
effect,
ChangeDetectorRef,
Injector,
Type,
computed,
inject,
} from "@angular/core";
import { CommonModule } from "@angular/common";
import { CopilotChatView } from "./copilot-chat-view";
import { DEFAULT_AGENT_ID, randomUUID } from "@copilotkit/shared";
import {
Message,
AbstractAgent,
AGUIConnectNotImplementedError,
} from "@ag-ui/client";
import { injectAgentStore } from "../../agent";
import { CopilotKit } from "../../copilotkit";
import { ChatState } from "../../chat-state";
/**
* CopilotChat component - Angular equivalent of React's <CopilotChat>
* Provides a complete chat interface that wires an agent to the chat view
*
* @example
* ```html
* <copilot-chat [agentId]="'default'" [threadId]="'abc123'"></copilot-chat>
* ```
*/
@Component({
selector: "copilot-chat",
standalone: true,
imports: [CommonModule, CopilotChatView],
changeDetection: ChangeDetectionStrategy.OnPush,
encapsulation: ViewEncapsulation.None,
host: { "data-copilotkit": "" },
template: `
<copilot-chat-view
[messages]="messages() ?? []"
[autoScroll]="true"
[messageViewClass]="'w-full'"
[showCursor]="showCursor()"
[inputComponent]="inputComponent()"
>
</copilot-chat-view>
`,
providers: [
{
provide: ChatState,
useExisting: CopilotChat,
},
],
})
export class CopilotChat implements ChatState {
readonly inputValue = signal<string>("");
readonly agentId = input<string | undefined>();
readonly threadId = input<string | undefined>();
readonly inputComponent = input<Type<any> | undefined>();
private readonly resolvedAgentId = computed(
() => this.agentId() ?? DEFAULT_AGENT_ID,
);
readonly agentStore = injectAgentStore(this.resolvedAgentId);
private readonly copilotKit = inject(CopilotKit);
// readonly chatConfig = injectChatConfig();
readonly cdr = inject(ChangeDetectorRef);
readonly injector = inject(Injector);
protected messages = computed(() => this.agentStore().messages());
protected isRunning = computed(() => this.agentStore().isRunning());
protected showCursor = signal<boolean>(false);
private generatedThreadId: string = randomUUID();
private hasConnectedOnce = false;
constructor() {
// Connect once when agent becomes available
// Connect once when agent becomes available
effect(
() => {
const a = this.agentStore().agent;
if (!a) return;
// Apply thread id when agent is available
a.threadId = this.threadId() || this.generatedThreadId;
if (!this.hasConnectedOnce) {
this.hasConnectedOnce = true;
if ("isCopilotKitAgent" in (a as any)) {
this.connectToAgent(a);
} else {
// Non-CopilotKit agent: nothing to connect; keep default cursor state
}
}
},
{ allowSignalWrites: true },
);
// Keep agent threadId in sync with input
effect(() => {
const a = this.agentStore().agent;
if (a) {
a.threadId = this.threadId() || this.generatedThreadId;
}
});
// Hide cursor when agent starts (runAgent via core does not pass subscriber callbacks)
effect(
() => {
if (this.isRunning()) {
this.showCursor.set(false);
this.cdr.markForCheck();
}
},
{ allowSignalWrites: true },
);
}
private async connectToAgent(agent: AbstractAgent): Promise<void> {
if (!agent) return;
this.showCursor.set(true);
this.cdr.markForCheck();
try {
await this.copilotKit.core.connectAgent({ agent });
this.showCursor.set(false);
this.cdr.markForCheck();
} catch (error) {
if (error instanceof AGUIConnectNotImplementedError) {
// Connect not implemented (e.g. agent only supports run), ignore
} else {
console.error("Failed to connect to agent:", error);
}
this.showCursor.set(false);
this.cdr.markForCheck();
}
}
async submitInput(value: string): Promise<void> {
const agent = this.agentStore().agent;
if (!agent || !value.trim()) return;
// Add user message
const userMessage: Message = {
id: randomUUID(),
role: "user",
content: value,
};
agent.addMessage(userMessage);
// Clear the input
this.inputValue.set("");
// Show cursor while processing
this.showCursor.set(true);
this.cdr.markForCheck();
// Run the agent via core so tools (and context, forwardedProps) are included
try {
await this.copilotKit.core.runAgent({ agent });
} catch (error) {
console.error("Agent run error:", error);
} finally {
this.showCursor.set(false);
this.cdr.markForCheck();
}
}
changeInput(value: string): void {
this.inputValue.set(value);
}
}