@copilotkit/svelte 0.1.0 → 0.1.1-canary.svelte-preview

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (193) hide show
  1. package/{src → dist}/components/chat/CopilotChat.svelte +26 -1
  2. package/dist/components/chat/CopilotChat.svelte.d.ts +5 -1
  3. package/dist/components/chat/CopilotChat.svelte.d.ts.map +1 -0
  4. package/dist/components/chat/CopilotChatAssistantMessage.svelte.d.ts +80 -1
  5. package/dist/components/chat/CopilotChatAssistantMessage.svelte.d.ts.map +1 -0
  6. package/dist/components/chat/CopilotChatAttachmentQueue.svelte.d.ts +9 -1
  7. package/dist/components/chat/CopilotChatAttachmentQueue.svelte.d.ts.map +1 -0
  8. package/dist/components/chat/CopilotChatInput.svelte.d.ts +20 -1
  9. package/dist/components/chat/CopilotChatInput.svelte.d.ts.map +1 -0
  10. package/dist/components/chat/CopilotChatMessage.svelte.d.ts +10 -1
  11. package/dist/components/chat/CopilotChatMessage.svelte.d.ts.map +1 -0
  12. package/dist/components/chat/CopilotChatMessageView.svelte.d.ts +14 -1
  13. package/dist/components/chat/CopilotChatMessageView.svelte.d.ts.map +1 -0
  14. package/dist/components/chat/CopilotChatReasoningMessage.svelte.d.ts +40 -1
  15. package/dist/components/chat/CopilotChatReasoningMessage.svelte.d.ts.map +1 -0
  16. package/dist/components/chat/CopilotChatSuggestionPill.svelte.d.ts +10 -1
  17. package/dist/components/chat/CopilotChatSuggestionPill.svelte.d.ts.map +1 -0
  18. package/dist/components/chat/CopilotChatSuggestionView.svelte.d.ts +10 -1
  19. package/dist/components/chat/CopilotChatSuggestionView.svelte.d.ts.map +1 -0
  20. package/dist/components/chat/CopilotChatToggleButton.svelte.d.ts +8 -1
  21. package/dist/components/chat/CopilotChatToggleButton.svelte.d.ts.map +1 -0
  22. package/dist/components/chat/CopilotChatToolCallsView.svelte.d.ts +12 -1
  23. package/dist/components/chat/CopilotChatToolCallsView.svelte.d.ts.map +1 -0
  24. package/dist/components/chat/CopilotChatUserMessage.svelte.d.ts +62 -1
  25. package/dist/components/chat/CopilotChatUserMessage.svelte.d.ts.map +1 -0
  26. package/dist/components/chat/CopilotChatView.svelte.d.ts +32 -1
  27. package/dist/components/chat/CopilotChatView.svelte.d.ts.map +1 -0
  28. package/dist/components/chat/CopilotPopup.svelte.d.ts +5 -1
  29. package/dist/components/chat/CopilotPopup.svelte.d.ts.map +1 -0
  30. package/dist/components/chat/CopilotSidebar.svelte.d.ts +5 -1
  31. package/dist/components/chat/CopilotSidebar.svelte.d.ts.map +1 -0
  32. package/dist/components/chat/StreamMarkdown.svelte.d.ts +7 -1
  33. package/dist/components/chat/StreamMarkdown.svelte.d.ts.map +1 -0
  34. package/dist/components/chat/chat-config-context.svelte.js +20 -0
  35. package/dist/components/chat/dispatch-after-active-run.d.ts +1 -1
  36. package/dist/components/chat/dispatch-after-active-run.js +31 -0
  37. package/dist/components/chat/markdown/CodeBlock.svelte.d.ts +8 -1
  38. package/dist/components/chat/markdown/CodeBlock.svelte.d.ts.map +1 -0
  39. package/dist/components/chat/markdown/CodeSpan.svelte.d.ts +9 -1
  40. package/dist/components/chat/markdown/CodeSpan.svelte.d.ts.map +1 -0
  41. package/dist/components/chat/markdown/Table.svelte.d.ts +8 -1
  42. package/dist/components/chat/markdown/Table.svelte.d.ts.map +1 -0
  43. package/dist/components/chat/markdown/TableBody.svelte.d.ts +8 -1
  44. package/dist/components/chat/markdown/TableBody.svelte.d.ts.map +1 -0
  45. package/dist/components/chat/markdown/TableCell.svelte.d.ts +10 -1
  46. package/dist/components/chat/markdown/TableCell.svelte.d.ts.map +1 -0
  47. package/dist/components/chat/markdown/TableHead.svelte.d.ts +8 -1
  48. package/dist/components/chat/markdown/TableHead.svelte.d.ts.map +1 -0
  49. package/dist/components/chat/markdown/TableRow.svelte.d.ts +8 -1
  50. package/dist/components/chat/markdown/TableRow.svelte.d.ts.map +1 -0
  51. package/dist/components/chat/markdown/highlight.js +91 -0
  52. package/dist/components/chat/media-parts.d.ts +2 -2
  53. package/dist/components/chat/media-parts.js +17 -0
  54. package/dist/components/chat/types.d.ts +5 -3
  55. package/dist/components/chat/types.js +7 -0
  56. package/dist/components/icons/index.d.ts +1 -1
  57. package/dist/components/icons/index.js +1 -0
  58. package/dist/components/index.d.ts +17 -17
  59. package/dist/hooks/connect-agent-context.svelte.js +25 -0
  60. package/dist/hooks/create-agent.svelte.d.ts +3 -3
  61. package/dist/hooks/create-agent.svelte.js +341 -0
  62. package/dist/hooks/create-attachments.svelte.d.ts +1 -1
  63. package/dist/hooks/create-attachments.svelte.js +232 -0
  64. package/dist/hooks/create-capabilities.svelte.d.ts +1 -1
  65. package/dist/hooks/create-capabilities.svelte.js +17 -0
  66. package/dist/hooks/create-interrupt.svelte.d.ts +2 -2
  67. package/dist/hooks/create-interrupt.svelte.js +333 -0
  68. package/dist/hooks/create-suggestions.svelte.d.ts +1 -1
  69. package/dist/hooks/create-suggestions.svelte.js +61 -0
  70. package/dist/hooks/create-threads.svelte.js +230 -0
  71. package/dist/hooks/index.d.ts +19 -19
  72. package/dist/hooks/index.js +11 -0
  73. package/dist/hooks/load-katex-styles.svelte.js +11 -0
  74. package/dist/hooks/register-frontend-tool.svelte.d.ts +1 -1
  75. package/dist/hooks/register-frontend-tool.svelte.js +32 -0
  76. package/dist/hooks/register-human-in-the-loop.svelte.d.ts +1 -1
  77. package/dist/hooks/register-human-in-the-loop.svelte.js +84 -0
  78. package/dist/hooks/register-render-tool-call.svelte.d.ts +1 -1
  79. package/dist/hooks/register-render-tool-call.svelte.js +26 -0
  80. package/dist/index.d.ts +5 -5
  81. package/dist/lib/svelte-core.d.ts +4 -3
  82. package/dist/lib/svelte-core.js +145 -0
  83. package/dist/providers/CopilotKitProvider.svelte.d.ts +9 -1
  84. package/dist/providers/CopilotKitProvider.svelte.d.ts.map +1 -0
  85. package/dist/providers/CopilotKitProvider.types.d.ts +4 -4
  86. package/dist/providers/CopilotKitProvider.types.js +1 -0
  87. package/dist/providers/context.d.ts +3 -3
  88. package/dist/providers/context.js +2 -0
  89. package/dist/providers/index.d.ts +5 -5
  90. package/dist/providers/index.js +2 -0
  91. package/dist/providers/useCopilotKit.d.ts +1 -1
  92. package/dist/providers/useCopilotKit.js +9 -0
  93. package/dist/styles.css +1 -2
  94. package/dist/types/a2ui.js +1 -0
  95. package/dist/types/activity-message-renderer.d.ts +3 -3
  96. package/dist/types/activity-message-renderer.js +1 -0
  97. package/dist/types/custom-message-renderer.d.ts +1 -1
  98. package/dist/types/custom-message-renderer.js +1 -0
  99. package/dist/types/frontend-tool.d.ts +2 -2
  100. package/dist/types/frontend-tool.js +1 -0
  101. package/dist/types/human-in-the-loop.d.ts +1 -1
  102. package/dist/types/human-in-the-loop.js +1 -0
  103. package/dist/types/index.d.ts +8 -8
  104. package/dist/types/interrupt.d.ts +1 -1
  105. package/dist/types/interrupt.js +1 -0
  106. package/dist/types/sandbox-function.d.ts +1 -1
  107. package/dist/types/sandbox-function.js +1 -0
  108. package/dist/types/tool-call-renderer.d.ts +2 -2
  109. package/dist/types/tool-call-renderer.js +1 -0
  110. package/package.json +14 -22
  111. package/dist/components/chat/CopilotChat.d.svelte.ts +0 -1
  112. package/dist/components/chat/CopilotChatAssistantMessage.d.svelte.ts +0 -1
  113. package/dist/components/chat/CopilotChatAttachmentQueue.d.svelte.ts +0 -1
  114. package/dist/components/chat/CopilotChatInput.d.svelte.ts +0 -1
  115. package/dist/components/chat/CopilotChatMessage.d.svelte.ts +0 -1
  116. package/dist/components/chat/CopilotChatMessageView.d.svelte.ts +0 -1
  117. package/dist/components/chat/CopilotChatReasoningMessage.d.svelte.ts +0 -1
  118. package/dist/components/chat/CopilotChatSuggestionPill.d.svelte.ts +0 -1
  119. package/dist/components/chat/CopilotChatSuggestionView.d.svelte.ts +0 -1
  120. package/dist/components/chat/CopilotChatToggleButton.d.svelte.ts +0 -1
  121. package/dist/components/chat/CopilotChatToolCallsView.d.svelte.ts +0 -1
  122. package/dist/components/chat/CopilotChatUserMessage.d.svelte.ts +0 -1
  123. package/dist/components/chat/CopilotChatView.d.svelte.ts +0 -1
  124. package/dist/components/chat/CopilotPopup.d.svelte.ts +0 -1
  125. package/dist/components/chat/CopilotSidebar.d.svelte.ts +0 -1
  126. package/dist/components/chat/StreamMarkdown.d.svelte.ts +0 -1
  127. package/dist/components/chat/chat-config-context.d.svelte.ts +0 -10
  128. package/dist/index.cjs +0 -5
  129. package/dist/index.cjs.map +0 -1
  130. package/dist/index.d.cts +0 -6
  131. package/dist/index.d.mts +0 -6
  132. package/dist/index.mjs +0 -3037
  133. package/dist/index.mjs.map +0 -1
  134. package/src/components/chat/chat-config-context.svelte.ts +0 -28
  135. package/src/components/chat/dispatch-after-active-run.ts +0 -40
  136. package/src/components/chat/markdown/highlight.ts +0 -96
  137. package/src/components/chat/media-parts.ts +0 -34
  138. package/src/components/chat/types.ts +0 -176
  139. package/src/components/icons/index.ts +0 -23
  140. package/src/hooks/connect-agent-context.svelte.ts +0 -46
  141. package/src/hooks/create-agent.svelte.ts +0 -420
  142. package/src/hooks/create-attachments.svelte.ts +0 -311
  143. package/src/hooks/create-capabilities.svelte.ts +0 -26
  144. package/src/hooks/create-interrupt.svelte.ts +0 -467
  145. package/src/hooks/create-suggestions.svelte.ts +0 -84
  146. package/src/hooks/create-threads.svelte.ts +0 -337
  147. package/src/hooks/index.ts +0 -45
  148. package/src/hooks/load-katex-styles.svelte.ts +0 -13
  149. package/src/hooks/register-frontend-tool.svelte.ts +0 -45
  150. package/src/hooks/register-human-in-the-loop.svelte.ts +0 -118
  151. package/src/hooks/register-render-tool-call.svelte.ts +0 -75
  152. package/src/lib/svelte-core.ts +0 -230
  153. package/src/providers/CopilotKitProvider.types.ts +0 -55
  154. package/src/providers/context.ts +0 -26
  155. package/src/providers/index.ts +0 -5
  156. package/src/providers/useCopilotKit.ts +0 -11
  157. package/src/types/a2ui.ts +0 -5
  158. package/src/types/activity-message-renderer.ts +0 -21
  159. package/src/types/custom-message-renderer.ts +0 -23
  160. package/src/types/frontend-tool.ts +0 -8
  161. package/src/types/human-in-the-loop.ts +0 -40
  162. package/src/types/interrupt.ts +0 -32
  163. package/src/types/sandbox-function.ts +0 -8
  164. package/src/types/tool-call-renderer.ts +0 -36
  165. /package/{src → dist}/app.d.ts +0 -0
  166. /package/{src → dist}/components/chat/CopilotChatAssistantMessage.svelte +0 -0
  167. /package/{src → dist}/components/chat/CopilotChatAttachmentQueue.svelte +0 -0
  168. /package/{src → dist}/components/chat/CopilotChatInput.svelte +0 -0
  169. /package/{src → dist}/components/chat/CopilotChatMessage.svelte +0 -0
  170. /package/{src → dist}/components/chat/CopilotChatMessageView.svelte +0 -0
  171. /package/{src → dist}/components/chat/CopilotChatReasoningMessage.svelte +0 -0
  172. /package/{src → dist}/components/chat/CopilotChatSuggestionPill.svelte +0 -0
  173. /package/{src → dist}/components/chat/CopilotChatSuggestionView.svelte +0 -0
  174. /package/{src → dist}/components/chat/CopilotChatToggleButton.svelte +0 -0
  175. /package/{src → dist}/components/chat/CopilotChatToolCallsView.svelte +0 -0
  176. /package/{src → dist}/components/chat/CopilotChatUserMessage.svelte +0 -0
  177. /package/{src → dist}/components/chat/CopilotChatView.svelte +0 -0
  178. /package/{src → dist}/components/chat/CopilotPopup.svelte +0 -0
  179. /package/{src → dist}/components/chat/CopilotSidebar.svelte +0 -0
  180. /package/{src → dist}/components/chat/StreamMarkdown.svelte +0 -0
  181. /package/{src → dist}/components/chat/markdown/CodeBlock.svelte +0 -0
  182. /package/{src → dist}/components/chat/markdown/CodeSpan.svelte +0 -0
  183. /package/{src → dist}/components/chat/markdown/Table.svelte +0 -0
  184. /package/{src → dist}/components/chat/markdown/TableBody.svelte +0 -0
  185. /package/{src → dist}/components/chat/markdown/TableCell.svelte +0 -0
  186. /package/{src → dist}/components/chat/markdown/TableHead.svelte +0 -0
  187. /package/{src → dist}/components/chat/markdown/TableRow.svelte +0 -0
  188. /package/{src/components/index.ts → dist/components/index.js} +0 -0
  189. /package/{src/index.ts → dist/index.js} +0 -0
  190. /package/{src → dist}/providers/CopilotKitProvider.svelte +0 -0
  191. /package/{src → dist}/styles/globals.css +0 -0
  192. /package/{src → dist}/styles.d.ts +0 -0
  193. /package/{src/types/index.ts → dist/types/index.js} +0 -0
@@ -1,420 +0,0 @@
1
- import type { AbstractAgent } from "@ag-ui/client";
2
- import { HttpAgent } from "@ag-ui/client";
3
- import type { Message } from "@ag-ui/core";
4
- import { DEFAULT_AGENT_ID } from "@copilotkit/shared";
5
- import { ProxiedCopilotRuntimeAgent } from "@copilotkit/core";
6
- import type { SubscribeToAgentSubscriber } from "@copilotkit/core";
7
- import { CopilotKitCoreRuntimeConnectionStatus } from "@copilotkit/core";
8
- import { COPILOT_KIT_KEY } from "../providers/context";
9
- import type { CopilotKitContextValue } from "../providers/context";
10
- import { getContext } from "svelte";
11
- import { SvelteMap } from "svelte/reactivity";
12
-
13
- export enum CreateAgentUpdate {
14
- OnMessagesChanged = "OnMessagesChanged",
15
- OnStateChanged = "OnStateChanged",
16
- OnRunStatusChanged = "OnRunStatusChanged",
17
- }
18
-
19
- const ALL_UPDATES: CreateAgentUpdate[] = [
20
- CreateAgentUpdate.OnMessagesChanged,
21
- CreateAgentUpdate.OnStateChanged,
22
- CreateAgentUpdate.OnRunStatusChanged,
23
- ];
24
-
25
- export interface CreateAgentProps {
26
- agentId?: string;
27
- threadId?: string;
28
- updates?: CreateAgentUpdate[];
29
- throttleMs?: number;
30
- }
31
-
32
- export const globalThreadCloneMap = new WeakMap<
33
- AbstractAgent,
34
- SvelteMap<string, AbstractAgent>
35
- >();
36
- const globalThreadCloneConsumers = new WeakMap<
37
- AbstractAgent,
38
- SvelteMap<string, number>
39
- >();
40
-
41
- const MAX_CLONES_PER_AGENT = 50;
42
-
43
- export function getThreadClone(
44
- registryAgent: AbstractAgent | undefined | null,
45
- threadId: string | undefined | null,
46
- ): AbstractAgent | undefined {
47
- if (!registryAgent || !threadId) return undefined;
48
- return globalThreadCloneMap.get(registryAgent)?.get(threadId);
49
- }
50
-
51
- function cloneForThread(
52
- source: AbstractAgent,
53
- threadId: string,
54
- ): AbstractAgent {
55
- const clone = source.clone();
56
- if (clone === source) {
57
- throw new Error(
58
- `createAgent: ${source.constructor.name}.clone() returned the same instance. ` +
59
- "clone() must return a new, independent object.",
60
- );
61
- }
62
- clone.threadId = threadId;
63
- clone.setMessages([]);
64
- clone.setState({});
65
- return clone;
66
- }
67
-
68
- function applyProviderHeaders(
69
- core: CopilotKitContextValue["copilotkit"],
70
- target: AbstractAgent | null | undefined,
71
- headers: Readonly<Record<string, string>>,
72
- ): void {
73
- if (target instanceof HttpAgent) {
74
- if (typeof core.applyHeadersToAgent === "function") {
75
- core.applyHeadersToAgent(target);
76
- } else {
77
- // Structural test doubles and older compatible cores may not expose the
78
- // merge helper. Preserve existing agent headers in that fallback path.
79
- target.headers = { ...target.headers, ...headers };
80
- }
81
- }
82
- }
83
-
84
- function getOrCreateThreadClone(
85
- core: CopilotKitContextValue["copilotkit"],
86
- source: AbstractAgent,
87
- threadId: string,
88
- headers: Readonly<Record<string, string>>,
89
- ): AbstractAgent {
90
- let byThread = globalThreadCloneMap.get(source);
91
- if (!byThread) {
92
- byThread = new SvelteMap();
93
- globalThreadCloneMap.set(source, byThread);
94
- }
95
- const existing = byThread.get(threadId);
96
- if (existing) {
97
- existing.threadId = threadId;
98
- applyProviderHeaders(core, existing, headers);
99
- return existing;
100
- }
101
- const clone = cloneForThread(source, threadId);
102
- applyProviderHeaders(core, clone, headers);
103
- if (byThread.size >= MAX_CLONES_PER_AGENT) {
104
- const consumers = globalThreadCloneConsumers.get(source);
105
- const evictable = [...byThread.keys()].find(
106
- (candidate) => !consumers?.get(candidate),
107
- );
108
- if (evictable !== undefined) byThread.delete(evictable);
109
- }
110
- byThread.set(threadId, clone);
111
- return clone;
112
- }
113
-
114
- function retainThreadClone(source: AbstractAgent, threadId: string): void {
115
- let consumers = globalThreadCloneConsumers.get(source);
116
- if (!consumers) {
117
- consumers = new SvelteMap();
118
- globalThreadCloneConsumers.set(source, consumers);
119
- }
120
- consumers.set(threadId, (consumers.get(threadId) ?? 0) + 1);
121
- }
122
-
123
- function releaseThreadClone(source: AbstractAgent, threadId: string): void {
124
- const consumers = globalThreadCloneConsumers.get(source);
125
- const count = consumers?.get(threadId);
126
- if (count === undefined) return;
127
- if (count <= 1) consumers?.delete(threadId);
128
- else consumers?.set(threadId, count - 1);
129
-
130
- const byThread = globalThreadCloneMap.get(source);
131
- while (byThread && byThread.size > MAX_CLONES_PER_AGENT) {
132
- const evictable = [...byThread.keys()].find(
133
- (candidate) => !consumers?.get(candidate),
134
- );
135
- if (evictable === undefined) break;
136
- byThread.delete(evictable);
137
- }
138
- }
139
-
140
- export function createAgent(props: CreateAgentProps = {}) {
141
- const context = getContext<CopilotKitContextValue | null>(COPILOT_KIT_KEY);
142
- if (!context) {
143
- throw new Error("createAgent must be used within CopilotKitProvider");
144
- }
145
-
146
- const agentId = $derived(props.agentId ?? DEFAULT_AGENT_ID);
147
- const threadId = $derived(props.threadId);
148
- const updateFlags = $derived(props.updates ?? ALL_UPDATES);
149
- const hookThrottleMs = $derived(props.throttleMs);
150
-
151
- let agent = $state<AbstractAgent | null>(null);
152
- let agentIdentity: AbstractAgent | null = null;
153
- let agentRevision = $state(0);
154
- let agentInstanceRevision = $state(0);
155
- let messages = $state<Message[]>([]);
156
- let isRunning = $state(false);
157
- let subscriptionAgent = $state<AbstractAgent | null>(null);
158
- const provisionalAgentCache = new SvelteMap<
159
- string,
160
- {
161
- agent: ProxiedCopilotRuntimeAgent;
162
- runtimeUrl: string;
163
- transport: CopilotKitContextValue["runtimeTransport"];
164
- }
165
- >();
166
- let ownedClone: { source: AbstractAgent; threadId: string } | null = null;
167
-
168
- function releaseOwnedClone(): void {
169
- if (!ownedClone) return;
170
- releaseThreadClone(ownedClone.source, ownedClone.threadId);
171
- ownedClone = null;
172
- }
173
-
174
- function setResolvedAgent(next: AbstractAgent): void {
175
- if (agentIdentity !== next) agentInstanceRevision += 1;
176
- agentIdentity = next;
177
- agent = next;
178
- }
179
-
180
- const resolveAgent = () => {
181
- const id = agentId;
182
- const resolvedThreadId = threadId;
183
- const cacheKey = resolvedThreadId ? `${id}:${resolvedThreadId}` : id;
184
- const core = context.copilotkit;
185
- const runtimeUrl = context.runtimeUrl;
186
- const status = context.runtimeConnectionStatus;
187
- const transport = context.runtimeTransport;
188
- const headers = context.headers;
189
- const credentials =
190
- "credentials" in context ? context.credentials : core.credentials;
191
- const registered = context.agents ?? {};
192
- // Reactive registry wins over mutable core fields. getAgent remains the
193
- // core lookup when the provider has not published the agent yet.
194
- const coreAgent = core.getAgent(id);
195
- const existing = registered[id] ?? coreAgent;
196
- if (existing) {
197
- provisionalAgentCache.delete(cacheKey);
198
- provisionalAgentCache.delete(id);
199
- applyProviderHeaders(core, existing, headers);
200
- let resolvedAgent: AbstractAgent;
201
- if (resolvedThreadId) {
202
- if (
203
- ownedClone &&
204
- (ownedClone.source !== existing ||
205
- ownedClone.threadId !== resolvedThreadId)
206
- ) {
207
- releaseOwnedClone();
208
- }
209
- resolvedAgent = getOrCreateThreadClone(
210
- core,
211
- existing,
212
- resolvedThreadId,
213
- headers,
214
- );
215
- if (
216
- !ownedClone ||
217
- ownedClone.source !== existing ||
218
- ownedClone.threadId !== resolvedThreadId
219
- ) {
220
- retainThreadClone(existing, resolvedThreadId);
221
- ownedClone = { source: existing, threadId: resolvedThreadId };
222
- }
223
- } else {
224
- releaseOwnedClone();
225
- resolvedAgent = existing;
226
- }
227
- setResolvedAgent(resolvedAgent);
228
- messages = [...(resolvedAgent.messages ?? [])];
229
- isRunning = resolvedAgent.isRunning ?? false;
230
- subscriptionAgent = resolvedAgent;
231
- return;
232
- }
233
-
234
- const isRuntimeConfigured = runtimeUrl !== undefined;
235
-
236
- if (
237
- isRuntimeConfigured &&
238
- (status === CopilotKitCoreRuntimeConnectionStatus.Disconnected ||
239
- status === CopilotKitCoreRuntimeConnectionStatus.Connecting ||
240
- status === CopilotKitCoreRuntimeConnectionStatus.Error)
241
- ) {
242
- releaseOwnedClone();
243
- let cached = provisionalAgentCache.get(cacheKey);
244
- if (
245
- cached &&
246
- (cached.runtimeUrl !== runtimeUrl || cached.transport !== transport)
247
- ) {
248
- provisionalAgentCache.delete(cacheKey);
249
- cached = undefined;
250
- }
251
- if (cached) {
252
- cached.agent.credentials = credentials;
253
- applyProviderHeaders(core, cached.agent, headers);
254
- if (resolvedThreadId) {
255
- cached.agent.threadId = resolvedThreadId;
256
- }
257
- setResolvedAgent(cached.agent);
258
- messages = [...(cached.agent.messages ?? [])];
259
- isRunning = cached.agent.isRunning ?? false;
260
- subscriptionAgent = cached.agent;
261
- return;
262
- }
263
- const provisional = new ProxiedCopilotRuntimeAgent({
264
- runtimeUrl: runtimeUrl!,
265
- agentId: id,
266
- transport,
267
- runtimeMode: "pending",
268
- credentials,
269
- });
270
- applyProviderHeaders(core, provisional, headers);
271
- if (resolvedThreadId) {
272
- provisional.threadId = resolvedThreadId;
273
- }
274
- provisionalAgentCache.set(cacheKey, {
275
- agent: provisional,
276
- runtimeUrl: runtimeUrl!,
277
- transport,
278
- });
279
- setResolvedAgent(provisional);
280
- messages = [...(provisional.messages ?? [])];
281
- isRunning = provisional.isRunning ?? false;
282
- subscriptionAgent = provisional;
283
- return;
284
- }
285
-
286
- releaseOwnedClone();
287
- const knownAgents = Object.keys(registered);
288
- const runtimePart = isRuntimeConfigured
289
- ? `runtimeUrl=${runtimeUrl}`
290
- : "no runtimeUrl";
291
- throw new Error(
292
- `createAgent: Agent '${id}' not found after runtime sync (${runtimePart}). ` +
293
- (knownAgents.length
294
- ? `Known agents: [${knownAgents.join(", ")}]`
295
- : "No agents registered."),
296
- );
297
- };
298
-
299
- $effect(() => {
300
- resolveAgent();
301
- });
302
-
303
- $effect(() => () => releaseOwnedClone());
304
-
305
- $effect(() => {
306
- // Track the provider getter so updates rerun this effect. The core owns
307
- // the merge baseline and preserves each HttpAgent's construction headers.
308
- const headers = context.headers;
309
- const core = context.copilotkit;
310
- applyProviderHeaders(core, subscriptionAgent, headers);
311
- for (const registered of Object.values(context.agents ?? {})) {
312
- applyProviderHeaders(core, registered, headers);
313
- const clones = globalThreadCloneMap.get(registered);
314
- if (!clones) continue;
315
- for (const clone of clones.values()) {
316
- applyProviderHeaders(core, clone, headers);
317
- }
318
- }
319
- for (const provisional of provisionalAgentCache.values()) {
320
- applyProviderHeaders(core, provisional.agent, headers);
321
- }
322
- });
323
-
324
- $effect(() => {
325
- const a = subscriptionAgent;
326
- const flags = updateFlags;
327
- const core = context.copilotkit;
328
- if (!a || flags.length === 0) return;
329
-
330
- let active = true;
331
- let batchScheduled = false;
332
- const batchedRefresh = () => {
333
- if (!active) return;
334
- if (!batchScheduled) {
335
- batchScheduled = true;
336
- queueMicrotask(() => {
337
- batchScheduled = false;
338
- if (active) {
339
- agentRevision += 1;
340
- }
341
- });
342
- }
343
- };
344
-
345
- const handlers: SubscribeToAgentSubscriber = {};
346
-
347
- if (flags.includes(CreateAgentUpdate.OnMessagesChanged)) {
348
- handlers.onMessagesChanged = () => {
349
- if (active) {
350
- messages = (a.messages ?? []).map((m) => ({ ...m }));
351
- isRunning = a.isRunning ?? false;
352
- agentRevision += 1;
353
- }
354
- };
355
- }
356
-
357
- if (flags.includes(CreateAgentUpdate.OnStateChanged)) {
358
- handlers.onStateChanged = batchedRefresh;
359
- }
360
-
361
- if (flags.includes(CreateAgentUpdate.OnRunStatusChanged)) {
362
- handlers.onRunInitialized = () => {
363
- if (active) {
364
- isRunning = true;
365
- agent = a;
366
- agentRevision += 1;
367
- }
368
- };
369
- handlers.onRunFinalized = () => {
370
- if (active) {
371
- isRunning = false;
372
- agent = a;
373
- messages = (a.messages ?? []).map((m) => ({ ...m }));
374
- agentRevision += 1;
375
- }
376
- };
377
- handlers.onRunFailed = () => {
378
- if (active) {
379
- isRunning = false;
380
- agent = a;
381
- messages = (a.messages ?? []).map((m) => ({ ...m }));
382
- agentRevision += 1;
383
- }
384
- };
385
- handlers.onRunErrorEvent = () => {
386
- if (active) {
387
- isRunning = false;
388
- agent = a;
389
- messages = (a.messages ?? []).map((m) => ({ ...m }));
390
- agentRevision += 1;
391
- }
392
- };
393
- }
394
-
395
- const subscription = core.subscribeToAgentWithOptions(a, handlers, {
396
- throttleMs: hookThrottleMs,
397
- });
398
-
399
- return () => {
400
- active = false;
401
- subscription.unsubscribe();
402
- };
403
- });
404
-
405
- return {
406
- get agent() {
407
- void agentRevision;
408
- return agent;
409
- },
410
- get agentInstanceVersion() {
411
- return agentInstanceRevision;
412
- },
413
- get messages() {
414
- return messages;
415
- },
416
- get isRunning() {
417
- return isRunning;
418
- },
419
- };
420
- }