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
168 lines (150 loc) · 5.15 KB
/
Copy pathagent.ts
File metadata and controls
168 lines (150 loc) · 5.15 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
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"];
export class AgentStore {
readonly #subscription?: {
unsubscribe: () => void;
};
readonly #isRunning = signal<boolean>(false);
readonly #messages = signal<Message[]>([]);
readonly #state = signal<any>(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;
const subscribeToAgent: SubscribeToAgentFn =
this.#copilotkit.core.subscribeToAgentWithOptions.bind(
this.#copilotkit.core,
);
return computed(() => {
this.#copilotkit.agents();
this.#copilotkit.runtimeConnectionStatus();
const runtimeUrl = this.#copilotkit.runtimeUrl();
const runtimeTransport = this.#copilotkit.runtimeTransport();
const headers = this.#copilotkit.headers();
if (lastAgentStore) {
lastAgentStore.teardown();
lastAgentStore = undefined;
}
const resolvedAgentId = agentId() || DEFAULT_AGENT_ID;
const abstractAgent = this.#copilotkit.getAgent(resolvedAgentId);
if (!abstractAgent) {
const { runtimeConnectionStatus } = this.#copilotkit.core;
const isRuntimeConfigured = runtimeUrl !== undefined;
if (
isRuntimeConfigured &&
(runtimeConnectionStatus ===
CopilotKitCoreRuntimeConnectionStatus.Disconnected ||
runtimeConnectionStatus ===
CopilotKitCoreRuntimeConnectionStatus.Connecting ||
runtimeConnectionStatus ===
CopilotKitCoreRuntimeConnectionStatus.Error)
) {
const provisional = new ProxiedCopilotRuntimeAgent({
runtimeUrl,
agentId: resolvedAgentId,
transport: runtimeTransport,
});
// Apply current headers so runs/connects inherit them
(provisional as any).headers = { ...headers };
lastAgentStore = new AgentStore(
provisional,
destroyRef,
subscribeToAgent,
);
return lastAgentStore;
}
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.",
);
}
lastAgentStore = new AgentStore(
abstractAgent,
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);
}