forked from CopilotKit/CopilotKit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent.ts
More file actions
185 lines (164 loc) · 5.73 KB
/
Copy pathagent.ts
File metadata and controls
185 lines (164 loc) · 5.73 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
177
178
179
180
181
182
183
184
185
import type { Signal } from "@angular/core";
import {
DestroyRef,
Injectable,
inject,
signal,
computed,
} from "@angular/core";
import { CopilotKit } from "./copilotkit";
import type { AbstractAgent } from "@ag-ui/client";
import type { Message } from "@ag-ui/client";
import { DEFAULT_AGENT_ID } from "@copilotkit/shared";
import type { CopilotKitCore } from "@copilotkit/core";
import {
ProxiedCopilotRuntimeAgent,
CopilotKitCoreRuntimeConnectionStatus,
} from "@copilotkit/core";
/** Function signature for subscribing to an agent — derived from
* CopilotKitCore so the types stay in sync automatically. Injected
* by the factory so that AgentStore stays decoupled from the concrete class. */
type SubscribeToAgentFn = CopilotKitCore["subscribeToAgentWithOptions"];
type AgentWithHeaders = AbstractAgent & { headers?: Record<string, string> };
function hasAgentHeaders(agent: AbstractAgent): agent is AgentWithHeaders {
return "headers" in agent;
}
export class AgentStore {
readonly #subscription?: {
unsubscribe: () => void;
};
readonly #isRunning = signal<boolean>(false);
readonly #messages = signal<Message[]>([]);
readonly #state = signal<unknown>(undefined);
readonly agent: AbstractAgent;
readonly isRunning = this.#isRunning.asReadonly();
readonly messages = this.#messages.asReadonly();
readonly state = this.#state.asReadonly();
constructor(
abstractAgent: AbstractAgent,
destroyRef: DestroyRef,
subscribeToAgent: SubscribeToAgentFn,
) {
this.agent = abstractAgent;
this.#subscription = subscribeToAgent(abstractAgent, {
onMessagesChanged: () => {
this.#messages.set([...abstractAgent.messages]);
},
onStateChanged: () => {
this.#state.set(abstractAgent.state);
},
onRunInitialized: () => {
this.#isRunning.set(true);
},
onRunFinalized: () => {
this.#isRunning.set(false);
},
onRunFailed: () => {
this.#isRunning.set(false);
},
// Protocol-level RUN_ERROR event (distinct from onRunFailed which
// handles local exceptions like network errors).
onRunErrorEvent: () => {
this.#isRunning.set(false);
},
});
destroyRef.onDestroy(() => {
this.teardown();
});
}
teardown(): void {
if (this.#subscription) {
this.#subscription.unsubscribe();
}
}
}
@Injectable({ providedIn: "root" })
export class CopilotkitAgentFactory {
readonly #copilotkit = inject(CopilotKit);
createAgentStoreSignal(
agentId: Signal<string | undefined>,
destroyRef: DestroyRef,
): Signal<AgentStore> {
let lastAgentStore: AgentStore | undefined;
let lastAgent: AbstractAgent | undefined;
const provisionalCache = new Map<string, ProxiedCopilotRuntimeAgent>();
const subscribeToAgent: SubscribeToAgentFn =
this.#copilotkit.core.subscribeToAgentWithOptions.bind(
this.#copilotkit.core,
);
const resolveAgent = (): AbstractAgent => {
const resolvedAgentId = agentId() || DEFAULT_AGENT_ID;
const existing = this.#copilotkit.getAgent(resolvedAgentId);
if (existing) {
provisionalCache.delete(resolvedAgentId);
return existing;
}
const runtimeUrl = this.#copilotkit.runtimeUrl();
const isRuntimeConfigured = runtimeUrl !== undefined;
const { runtimeConnectionStatus } = this.#copilotkit.core;
if (
isRuntimeConfigured &&
(runtimeConnectionStatus ===
CopilotKitCoreRuntimeConnectionStatus.Disconnected ||
runtimeConnectionStatus ===
CopilotKitCoreRuntimeConnectionStatus.Connecting ||
runtimeConnectionStatus ===
CopilotKitCoreRuntimeConnectionStatus.Error)
) {
const headers = this.#copilotkit.headers();
const cached = provisionalCache.get(resolvedAgentId);
if (cached) {
if (hasAgentHeaders(cached)) {
cached.headers = { ...headers };
}
return cached;
}
const provisional = new ProxiedCopilotRuntimeAgent({
runtimeUrl,
agentId: resolvedAgentId,
transport: this.#copilotkit.runtimeTransport(),
});
if (hasAgentHeaders(provisional)) {
provisional.headers = { ...headers };
}
provisionalCache.set(resolvedAgentId, provisional);
return provisional;
}
const knownAgents = Object.keys(this.#copilotkit.agents() ?? {});
const runtimePart = isRuntimeConfigured
? `runtimeUrl=${runtimeUrl}`
: "no runtimeUrl";
throw new Error(
`injectAgentStore: Agent '${resolvedAgentId}' not found after runtime sync (${runtimePart}). ` +
(knownAgents.length
? `Known agents: [${knownAgents.join(", ")}]`
: "No agents registered.") +
" Verify your runtime /info and/or agents__unsafe_dev_only.",
);
};
return computed(() => {
this.#copilotkit.agents();
this.#copilotkit.runtimeConnectionStatus();
this.#copilotkit.runtimeUrl();
this.#copilotkit.runtimeTransport();
this.#copilotkit.headers();
const agent = resolveAgent();
if (lastAgentStore && lastAgent === agent) {
return lastAgentStore;
}
lastAgentStore?.teardown();
lastAgent = agent;
lastAgentStore = new AgentStore(agent, destroyRef, subscribeToAgent);
return lastAgentStore;
});
}
}
export function injectAgentStore(
agentId: string | Signal<string | undefined>,
): Signal<AgentStore> {
const agentFactory = inject(CopilotkitAgentFactory);
const destroyRef = inject(DestroyRef);
const agentIdSignal =
typeof agentId === "function" ? agentId : computed(() => agentId);
return agentFactory.createAgentStoreSignal(agentIdSignal, destroyRef);
}