@copilotkit/svelte 0.0.0-stage → 0.1.0

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 (180) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +325 -2
  3. package/dist/components/chat/CopilotChat.d.svelte.ts +1 -0
  4. package/dist/components/chat/CopilotChat.svelte.d.ts +1 -0
  5. package/dist/components/chat/CopilotChatAssistantMessage.d.svelte.ts +1 -0
  6. package/dist/components/chat/CopilotChatAssistantMessage.svelte.d.ts +1 -0
  7. package/dist/components/chat/CopilotChatAttachmentQueue.d.svelte.ts +1 -0
  8. package/dist/components/chat/CopilotChatAttachmentQueue.svelte.d.ts +1 -0
  9. package/dist/components/chat/CopilotChatInput.d.svelte.ts +1 -0
  10. package/dist/components/chat/CopilotChatInput.svelte.d.ts +1 -0
  11. package/dist/components/chat/CopilotChatMessage.d.svelte.ts +1 -0
  12. package/dist/components/chat/CopilotChatMessage.svelte.d.ts +1 -0
  13. package/dist/components/chat/CopilotChatMessageView.d.svelte.ts +1 -0
  14. package/dist/components/chat/CopilotChatMessageView.svelte.d.ts +1 -0
  15. package/dist/components/chat/CopilotChatReasoningMessage.d.svelte.ts +1 -0
  16. package/dist/components/chat/CopilotChatReasoningMessage.svelte.d.ts +1 -0
  17. package/dist/components/chat/CopilotChatSuggestionPill.d.svelte.ts +1 -0
  18. package/dist/components/chat/CopilotChatSuggestionPill.svelte.d.ts +1 -0
  19. package/dist/components/chat/CopilotChatSuggestionView.d.svelte.ts +1 -0
  20. package/dist/components/chat/CopilotChatSuggestionView.svelte.d.ts +1 -0
  21. package/dist/components/chat/CopilotChatToggleButton.d.svelte.ts +1 -0
  22. package/dist/components/chat/CopilotChatToggleButton.svelte.d.ts +1 -0
  23. package/dist/components/chat/CopilotChatToolCallsView.d.svelte.ts +1 -0
  24. package/dist/components/chat/CopilotChatToolCallsView.svelte.d.ts +1 -0
  25. package/dist/components/chat/CopilotChatUserMessage.d.svelte.ts +1 -0
  26. package/dist/components/chat/CopilotChatUserMessage.svelte.d.ts +1 -0
  27. package/dist/components/chat/CopilotChatView.d.svelte.ts +1 -0
  28. package/dist/components/chat/CopilotChatView.svelte.d.ts +1 -0
  29. package/dist/components/chat/CopilotPopup.d.svelte.ts +1 -0
  30. package/dist/components/chat/CopilotPopup.svelte.d.ts +1 -0
  31. package/dist/components/chat/CopilotSidebar.d.svelte.ts +1 -0
  32. package/dist/components/chat/CopilotSidebar.svelte.d.ts +1 -0
  33. package/dist/components/chat/StreamMarkdown.d.svelte.ts +1 -0
  34. package/dist/components/chat/StreamMarkdown.svelte.d.ts +1 -0
  35. package/dist/components/chat/chat-config-context.d.svelte.ts +10 -0
  36. package/dist/components/chat/chat-config-context.svelte.d.ts +10 -0
  37. package/dist/components/chat/chat-config-context.svelte.d.ts.map +1 -0
  38. package/dist/components/chat/dispatch-after-active-run.d.ts +3 -0
  39. package/dist/components/chat/dispatch-after-active-run.d.ts.map +1 -0
  40. package/dist/components/chat/markdown/CodeBlock.svelte.d.ts +1 -0
  41. package/dist/components/chat/markdown/CodeSpan.svelte.d.ts +1 -0
  42. package/dist/components/chat/markdown/Table.svelte.d.ts +1 -0
  43. package/dist/components/chat/markdown/TableBody.svelte.d.ts +1 -0
  44. package/dist/components/chat/markdown/TableCell.svelte.d.ts +1 -0
  45. package/dist/components/chat/markdown/TableHead.svelte.d.ts +1 -0
  46. package/dist/components/chat/markdown/TableRow.svelte.d.ts +1 -0
  47. package/dist/components/chat/markdown/highlight.d.ts +8 -0
  48. package/dist/components/chat/markdown/highlight.d.ts.map +1 -0
  49. package/dist/components/chat/media-parts.d.ts +11 -0
  50. package/dist/components/chat/media-parts.d.ts.map +1 -0
  51. package/dist/components/chat/types.d.ts +137 -0
  52. package/dist/components/chat/types.d.ts.map +1 -0
  53. package/dist/components/icons/index.d.ts +2 -0
  54. package/dist/components/icons/index.d.ts.map +1 -0
  55. package/dist/components/index.d.ts +18 -0
  56. package/dist/components/index.d.ts.map +1 -0
  57. package/dist/hooks/connect-agent-context.svelte.d.ts +9 -0
  58. package/dist/hooks/connect-agent-context.svelte.d.ts.map +1 -0
  59. package/dist/hooks/create-agent.svelte.d.ts +23 -0
  60. package/dist/hooks/create-agent.svelte.d.ts.map +1 -0
  61. package/dist/hooks/create-attachments.svelte.d.ts +33 -0
  62. package/dist/hooks/create-attachments.svelte.d.ts.map +1 -0
  63. package/dist/hooks/create-capabilities.svelte.d.ts +6 -0
  64. package/dist/hooks/create-capabilities.svelte.d.ts.map +1 -0
  65. package/dist/hooks/create-interrupt.svelte.d.ts +24 -0
  66. package/dist/hooks/create-interrupt.svelte.d.ts.map +1 -0
  67. package/dist/hooks/create-suggestions.svelte.d.ts +12 -0
  68. package/dist/hooks/create-suggestions.svelte.d.ts.map +1 -0
  69. package/dist/hooks/create-threads.svelte.d.ts +33 -0
  70. package/dist/hooks/create-threads.svelte.d.ts.map +1 -0
  71. package/dist/hooks/index.d.ts +20 -0
  72. package/dist/hooks/index.d.ts.map +1 -0
  73. package/dist/hooks/load-katex-styles.svelte.d.ts +2 -0
  74. package/dist/hooks/load-katex-styles.svelte.d.ts.map +1 -0
  75. package/dist/hooks/register-frontend-tool.svelte.d.ts +3 -0
  76. package/dist/hooks/register-frontend-tool.svelte.d.ts.map +1 -0
  77. package/dist/hooks/register-human-in-the-loop.svelte.d.ts +3 -0
  78. package/dist/hooks/register-human-in-the-loop.svelte.d.ts.map +1 -0
  79. package/dist/hooks/register-render-tool-call.svelte.d.ts +30 -0
  80. package/dist/hooks/register-render-tool-call.svelte.d.ts.map +1 -0
  81. package/dist/index.cjs +5 -0
  82. package/dist/index.cjs.map +1 -0
  83. package/dist/index.d.cts +6 -0
  84. package/dist/index.d.mts +6 -0
  85. package/dist/index.d.ts +6 -0
  86. package/dist/index.d.ts.map +1 -0
  87. package/dist/index.mjs +3037 -0
  88. package/dist/index.mjs.map +1 -0
  89. package/dist/lib/svelte-core.d.ts +53 -0
  90. package/dist/lib/svelte-core.d.ts.map +1 -0
  91. package/dist/providers/CopilotKitProvider.svelte.d.ts +1 -0
  92. package/dist/providers/CopilotKitProvider.types.d.ts +47 -0
  93. package/dist/providers/CopilotKitProvider.types.d.ts.map +1 -0
  94. package/dist/providers/context.d.ts +19 -0
  95. package/dist/providers/context.d.ts.map +1 -0
  96. package/dist/providers/index.d.ts +6 -0
  97. package/dist/providers/index.d.ts.map +1 -0
  98. package/dist/providers/useCopilotKit.d.ts +3 -0
  99. package/dist/providers/useCopilotKit.d.ts.map +1 -0
  100. package/dist/styles.css +3 -0
  101. package/dist/types/a2ui.d.ts +5 -0
  102. package/dist/types/a2ui.d.ts.map +1 -0
  103. package/dist/types/activity-message-renderer.d.ts +17 -0
  104. package/dist/types/activity-message-renderer.d.ts.map +1 -0
  105. package/dist/types/custom-message-renderer.d.ts +18 -0
  106. package/dist/types/custom-message-renderer.d.ts.map +1 -0
  107. package/dist/types/frontend-tool.d.ts +6 -0
  108. package/dist/types/frontend-tool.d.ts.map +1 -0
  109. package/dist/types/human-in-the-loop.d.ts +31 -0
  110. package/dist/types/human-in-the-loop.d.ts.map +1 -0
  111. package/dist/types/index.d.ts +9 -0
  112. package/dist/types/index.d.ts.map +1 -0
  113. package/dist/types/interrupt.d.ts +23 -0
  114. package/dist/types/interrupt.d.ts.map +1 -0
  115. package/dist/types/sandbox-function.d.ts +8 -0
  116. package/dist/types/sandbox-function.d.ts.map +1 -0
  117. package/dist/types/tool-call-renderer.d.ts +29 -0
  118. package/dist/types/tool-call-renderer.d.ts.map +1 -0
  119. package/package.json +114 -3
  120. package/src/app.d.ts +7 -0
  121. package/src/components/chat/CopilotChat.svelte +222 -0
  122. package/src/components/chat/CopilotChatAssistantMessage.svelte +295 -0
  123. package/src/components/chat/CopilotChatAttachmentQueue.svelte +241 -0
  124. package/src/components/chat/CopilotChatInput.svelte +192 -0
  125. package/src/components/chat/CopilotChatMessage.svelte +37 -0
  126. package/src/components/chat/CopilotChatMessageView.svelte +76 -0
  127. package/src/components/chat/CopilotChatReasoningMessage.svelte +210 -0
  128. package/src/components/chat/CopilotChatSuggestionPill.svelte +66 -0
  129. package/src/components/chat/CopilotChatSuggestionView.svelte +36 -0
  130. package/src/components/chat/CopilotChatToggleButton.svelte +47 -0
  131. package/src/components/chat/CopilotChatToolCallsView.svelte +187 -0
  132. package/src/components/chat/CopilotChatUserMessage.svelte +312 -0
  133. package/src/components/chat/CopilotChatView.svelte +177 -0
  134. package/src/components/chat/CopilotPopup.svelte +187 -0
  135. package/src/components/chat/CopilotSidebar.svelte +138 -0
  136. package/src/components/chat/StreamMarkdown.svelte +30 -0
  137. package/src/components/chat/chat-config-context.svelte.ts +28 -0
  138. package/src/components/chat/dispatch-after-active-run.ts +40 -0
  139. package/src/components/chat/markdown/CodeBlock.svelte +78 -0
  140. package/src/components/chat/markdown/CodeSpan.svelte +29 -0
  141. package/src/components/chat/markdown/Table.svelte +15 -0
  142. package/src/components/chat/markdown/TableBody.svelte +11 -0
  143. package/src/components/chat/markdown/TableCell.svelte +29 -0
  144. package/src/components/chat/markdown/TableHead.svelte +11 -0
  145. package/src/components/chat/markdown/TableRow.svelte +11 -0
  146. package/src/components/chat/markdown/highlight.ts +96 -0
  147. package/src/components/chat/media-parts.ts +34 -0
  148. package/src/components/chat/types.ts +176 -0
  149. package/src/components/icons/index.ts +23 -0
  150. package/src/components/index.ts +17 -0
  151. package/src/hooks/connect-agent-context.svelte.ts +46 -0
  152. package/src/hooks/create-agent.svelte.ts +420 -0
  153. package/src/hooks/create-attachments.svelte.ts +311 -0
  154. package/src/hooks/create-capabilities.svelte.ts +26 -0
  155. package/src/hooks/create-interrupt.svelte.ts +467 -0
  156. package/src/hooks/create-suggestions.svelte.ts +84 -0
  157. package/src/hooks/create-threads.svelte.ts +337 -0
  158. package/src/hooks/index.ts +45 -0
  159. package/src/hooks/load-katex-styles.svelte.ts +13 -0
  160. package/src/hooks/register-frontend-tool.svelte.ts +45 -0
  161. package/src/hooks/register-human-in-the-loop.svelte.ts +118 -0
  162. package/src/hooks/register-render-tool-call.svelte.ts +75 -0
  163. package/src/index.ts +6 -0
  164. package/src/lib/svelte-core.ts +230 -0
  165. package/src/providers/CopilotKitProvider.svelte +291 -0
  166. package/src/providers/CopilotKitProvider.types.ts +55 -0
  167. package/src/providers/context.ts +26 -0
  168. package/src/providers/index.ts +5 -0
  169. package/src/providers/useCopilotKit.ts +11 -0
  170. package/src/styles/globals.css +691 -0
  171. package/src/styles.d.ts +1 -0
  172. package/src/types/a2ui.ts +5 -0
  173. package/src/types/activity-message-renderer.ts +21 -0
  174. package/src/types/custom-message-renderer.ts +23 -0
  175. package/src/types/frontend-tool.ts +8 -0
  176. package/src/types/human-in-the-loop.ts +40 -0
  177. package/src/types/index.ts +8 -0
  178. package/src/types/interrupt.ts +32 -0
  179. package/src/types/sandbox-function.ts +8 -0
  180. package/src/types/tool-call-renderer.ts +36 -0
@@ -0,0 +1,467 @@
1
+ import { getContext, untrack } from "svelte";
2
+ import type { AbstractAgent } from "@ag-ui/client";
3
+ import { buildResumeArray, isInterruptExpired } from "@ag-ui/client";
4
+ import type { Interrupt, RunAgentResult } from "@ag-ui/client";
5
+ import { COPILOT_KIT_KEY } from "../providers/context";
6
+ import type { CopilotKitContextValue } from "../providers/context";
7
+ import { createAgent } from "./create-agent.svelte";
8
+ import type {
9
+ InterruptEvent,
10
+ InterruptHandlerProps,
11
+ InterruptRenderProps,
12
+ InterruptResolveFn,
13
+ InterruptCancelFn,
14
+ } from "../types/interrupt";
15
+
16
+ export type {
17
+ InterruptEvent,
18
+ InterruptHandlerProps,
19
+ InterruptRenderProps,
20
+ Interrupt,
21
+ };
22
+
23
+ const INTERRUPT_EVENT_NAME = "on_interrupt";
24
+
25
+ type ResumeResponse =
26
+ | { status: "resolved"; payload?: unknown }
27
+ | { status: "cancelled" };
28
+
29
+ type PendingInterrupt =
30
+ | {
31
+ kind: "legacy";
32
+ event: InterruptEvent;
33
+ owner: AbstractAgent;
34
+ ownerVersion: number;
35
+ }
36
+ | {
37
+ kind: "standard";
38
+ interrupts: Interrupt[];
39
+ owner: AbstractAgent;
40
+ ownerVersion: number;
41
+ };
42
+
43
+ type InterruptHandlerFn<TValue, TResult> = (
44
+ props: InterruptHandlerProps<TValue>,
45
+ ) => TResult | PromiseLike<TResult>;
46
+
47
+ type InterruptResultFromHandler<THandler> = THandler extends (
48
+ ...args: never[]
49
+ ) => infer TResult
50
+ ? TResult extends PromiseLike<infer TResolved>
51
+ ? TResolved | null
52
+ : TResult | null
53
+ : null;
54
+
55
+ type InterruptResult<TValue, TResult> = InterruptResultFromHandler<
56
+ InterruptHandlerFn<TValue, TResult>
57
+ >;
58
+
59
+ export interface CreateInterruptConfig<TValue = unknown, TResult = never> {
60
+ handler?: InterruptHandlerFn<TValue, TResult>;
61
+ enabled?: (event: InterruptEvent<TValue>) => boolean;
62
+ agentId?: string | (() => string | undefined);
63
+ renderInChat?: boolean;
64
+ }
65
+
66
+ export interface CreateInterruptResult<TValue = unknown, TResult = never> {
67
+ interrupt: InterruptEvent<TValue> | null;
68
+ result: InterruptResult<TValue, TResult>;
69
+ hasInterrupt: boolean;
70
+ resolve: InterruptResolveFn;
71
+ resolveInterrupt: InterruptResolveFn;
72
+ cancel: InterruptCancelFn;
73
+ cancelInterrupt: InterruptCancelFn;
74
+ slotProps: InterruptRenderProps<
75
+ TValue,
76
+ InterruptResult<TValue, TResult>
77
+ > | null;
78
+ }
79
+
80
+ function isPromiseLike<TValue>(
81
+ value: TValue | PromiseLike<TValue>,
82
+ ): value is PromiseLike<TValue> {
83
+ return (
84
+ (typeof value === "object" || typeof value === "function") &&
85
+ value !== null &&
86
+ typeof Reflect.get(value, "then") === "function"
87
+ );
88
+ }
89
+
90
+ function normalizeAsyncResult<TValue>(
91
+ value: PromiseLike<TValue>,
92
+ ): Promise<TValue> {
93
+ if (value instanceof Promise) return value;
94
+ return new Promise<TValue>((resolve, reject) => {
95
+ try {
96
+ value.then(resolve, reject);
97
+ } catch (error) {
98
+ reject(error);
99
+ }
100
+ });
101
+ }
102
+
103
+ function toLegacyEvent(pending: PendingInterrupt): InterruptEvent {
104
+ if (pending.kind === "legacy") return pending.event;
105
+ return { name: INTERRUPT_EVENT_NAME, value: pending.interrupts[0] };
106
+ }
107
+
108
+ export function createInterrupt<TValue = unknown, TResult = never>(
109
+ config: CreateInterruptConfig<TValue, TResult> = {},
110
+ ): CreateInterruptResult<TValue, TResult> {
111
+ const context = getContext<CopilotKitContextValue | null>(COPILOT_KIT_KEY);
112
+ if (!context) {
113
+ throw new Error("createInterrupt must be used within CopilotKitProvider");
114
+ }
115
+
116
+ const agentHandle = createAgent({
117
+ get agentId() {
118
+ return typeof config.agentId === "function"
119
+ ? config.agentId()
120
+ : config.agentId;
121
+ },
122
+ });
123
+
124
+ let pending = $state.raw<PendingInterrupt | null>(null);
125
+ let result = $state<InterruptResult<TValue, TResult>>(null);
126
+ const responses: Record<string, ResumeResponse> = {};
127
+ let pendingAgentVersion: number | null = null;
128
+
129
+ function clearResponses(): void {
130
+ for (const key of Object.keys(responses)) delete responses[key];
131
+ }
132
+
133
+ $effect(() => {
134
+ const ownerVersion = agentHandle.agentInstanceVersion;
135
+ const resolvedAgent = untrack(() => agentHandle.agent);
136
+ if (pendingAgentVersion !== ownerVersion) {
137
+ pending = null;
138
+ result = null;
139
+ clearResponses();
140
+ pendingAgentVersion = ownerVersion;
141
+ }
142
+ if (!resolvedAgent) {
143
+ pending = null;
144
+ result = null;
145
+ return;
146
+ }
147
+
148
+ let localLegacy: InterruptEvent<TValue> | null = null;
149
+ let localStandard: Interrupt[] | null = null;
150
+
151
+ const subscription = resolvedAgent.subscribe({
152
+ onCustomEvent: ({ event }) => {
153
+ if (agentHandle.agentInstanceVersion !== ownerVersion) return;
154
+ if (event.name === INTERRUPT_EVENT_NAME) {
155
+ localLegacy = {
156
+ name: event.name,
157
+ value: event.value as TValue,
158
+ };
159
+ }
160
+ },
161
+ onRunFinishedEvent: (params) => {
162
+ if (agentHandle.agentInstanceVersion !== ownerVersion) return;
163
+ if (params.outcome === "interrupt") {
164
+ localStandard = params.interrupts;
165
+ }
166
+ },
167
+ onRunStartedEvent: () => {
168
+ if (agentHandle.agentInstanceVersion !== ownerVersion) return;
169
+ localLegacy = null;
170
+ localStandard = null;
171
+ clearResponses();
172
+ if (pending?.ownerVersion === ownerVersion) pending = null;
173
+ },
174
+ onRunFinalized: () => {
175
+ if (agentHandle.agentInstanceVersion !== ownerVersion) return;
176
+ if (localStandard && localStandard.length > 0) {
177
+ pending = {
178
+ kind: "standard",
179
+ interrupts: localStandard,
180
+ owner: resolvedAgent,
181
+ ownerVersion,
182
+ };
183
+ } else if (localLegacy) {
184
+ pending = {
185
+ kind: "legacy",
186
+ event: localLegacy,
187
+ owner: resolvedAgent,
188
+ ownerVersion,
189
+ };
190
+ }
191
+ localLegacy = null;
192
+ localStandard = null;
193
+ },
194
+ onRunFailed: () => {
195
+ if (agentHandle.agentInstanceVersion !== ownerVersion) return;
196
+ localLegacy = null;
197
+ localStandard = null;
198
+ clearResponses();
199
+ if (pending?.ownerVersion === ownerVersion) pending = null;
200
+ },
201
+ });
202
+
203
+ return () => subscription.unsubscribe();
204
+ });
205
+
206
+ const submitStandardIfComplete = async (
207
+ interrupts: Interrupt[],
208
+ owner: AbstractAgent,
209
+ ownerVersion: number,
210
+ ): Promise<RunAgentResult | void> => {
211
+ if (
212
+ pending?.ownerVersion !== ownerVersion ||
213
+ agentHandle.agentInstanceVersion !== ownerVersion
214
+ ) {
215
+ return;
216
+ }
217
+ const allAddressed = interrupts.every((i) => responses[i.id]);
218
+ if (!allAddressed) return;
219
+
220
+ const expired = interrupts.find((i) => isInterruptExpired(i));
221
+ if (expired) {
222
+ console.error(
223
+ `[CopilotKit] createInterrupt: interrupt ${expired.id} expired at ${expired.expiresAt}; not resuming.`,
224
+ );
225
+ clearResponses();
226
+ if (pending?.ownerVersion === ownerVersion) pending = null;
227
+ return;
228
+ }
229
+
230
+ const resume = buildResumeArray(interrupts, responses);
231
+ clearResponses();
232
+ if (agentHandle.agentInstanceVersion !== ownerVersion) return;
233
+ try {
234
+ return await context.copilotkit.runAgent({
235
+ agent: owner,
236
+ resume,
237
+ });
238
+ } catch (err) {
239
+ console.error(
240
+ "[CopilotKit] createInterrupt resolve: runAgent rejected; clearing pending + rethrowing",
241
+ err,
242
+ );
243
+ if (pending?.ownerVersion === ownerVersion) pending = null;
244
+ throw err;
245
+ }
246
+ };
247
+
248
+ const resolve: InterruptResolveFn = async (payload?, interruptId?) => {
249
+ const current = pending;
250
+ if (!current) return;
251
+ if (agentHandle.agentInstanceVersion !== current.ownerVersion) return;
252
+
253
+ if (current.kind === "legacy") {
254
+ try {
255
+ return await context.copilotkit.runAgent({
256
+ agent: current.owner,
257
+ forwardedProps: {
258
+ command: {
259
+ resume: payload,
260
+ interruptEvent: current.event.value,
261
+ },
262
+ },
263
+ });
264
+ } catch (err) {
265
+ console.error(
266
+ "[CopilotKit] createInterrupt resolve: runAgent rejected; clearing pending + rethrowing",
267
+ err,
268
+ );
269
+ if (pending?.ownerVersion === current.ownerVersion) pending = null;
270
+ throw err;
271
+ }
272
+ }
273
+
274
+ if (current.interrupts.length > 1 && interruptId === undefined) {
275
+ console.warn(
276
+ `[CopilotKit] createInterrupt: resolve() called without an interruptId while ${current.interrupts.length} interrupts are open; defaulting to the first.`,
277
+ );
278
+ }
279
+ const id = interruptId ?? current.interrupts[0]?.id;
280
+ if (!id) return;
281
+ responses[id] = { status: "resolved", payload };
282
+ return submitStandardIfComplete(
283
+ current.interrupts,
284
+ current.owner,
285
+ current.ownerVersion,
286
+ );
287
+ };
288
+
289
+ const cancel: InterruptCancelFn = async (interruptId?) => {
290
+ const current = pending;
291
+ if (!current) return;
292
+ if (agentHandle.agentInstanceVersion !== current.ownerVersion) return;
293
+
294
+ if (current.kind === "legacy") {
295
+ console.warn(
296
+ "[CopilotKit] createInterrupt: cancel() is not supported for legacy on_interrupt interrupts; dismissing.",
297
+ );
298
+ pending = null;
299
+ return;
300
+ }
301
+
302
+ if (current.interrupts.length > 1 && interruptId === undefined) {
303
+ console.warn(
304
+ `[CopilotKit] createInterrupt: cancel() called without an interruptId while ${current.interrupts.length} interrupts are open; defaulting to the first.`,
305
+ );
306
+ }
307
+ const id = interruptId ?? current.interrupts[0]?.id;
308
+ if (!id) return;
309
+ responses[id] = { status: "cancelled" };
310
+ return submitStandardIfComplete(
311
+ current.interrupts,
312
+ current.owner,
313
+ current.ownerVersion,
314
+ );
315
+ };
316
+
317
+ const resolveInterrupt: InterruptResolveFn = resolve;
318
+ const cancelInterrupt: InterruptCancelFn = cancel;
319
+
320
+ $effect(() => {
321
+ const currentPending = pending;
322
+ if (!currentPending) {
323
+ result = null;
324
+ return;
325
+ }
326
+ const legacyEvent = toLegacyEvent(currentPending) as InterruptEvent<TValue>;
327
+ if (config.enabled && !config.enabled(legacyEvent)) {
328
+ result = null;
329
+ return;
330
+ }
331
+ const handler = config.handler;
332
+ if (!handler) {
333
+ result = null;
334
+ return;
335
+ }
336
+
337
+ let cancelled = false;
338
+ const ownerVersion = currentPending.ownerVersion;
339
+ const maybePromise = handler({
340
+ event: legacyEvent,
341
+ interrupt:
342
+ currentPending.kind === "standard"
343
+ ? (currentPending.interrupts[0] ?? null)
344
+ : null,
345
+ interrupts:
346
+ currentPending.kind === "standard" ? currentPending.interrupts : [],
347
+ resolve,
348
+ cancel,
349
+ });
350
+
351
+ if (isPromiseLike(maybePromise)) {
352
+ normalizeAsyncResult(maybePromise)
353
+ .then((resolved) => {
354
+ if (
355
+ !cancelled &&
356
+ agentHandle.agentInstanceVersion === ownerVersion &&
357
+ pending?.ownerVersion === ownerVersion
358
+ ) {
359
+ result = resolved;
360
+ }
361
+ })
362
+ .catch((err) => {
363
+ if (
364
+ !cancelled &&
365
+ agentHandle.agentInstanceVersion === ownerVersion &&
366
+ pending?.ownerVersion === ownerVersion
367
+ ) {
368
+ console.error("[CopilotKit] createInterrupt handler failed:", err);
369
+ result = null;
370
+ }
371
+ });
372
+ } else {
373
+ result = maybePromise;
374
+ }
375
+
376
+ return () => {
377
+ cancelled = true;
378
+ };
379
+ });
380
+
381
+ $effect(() => {
382
+ const core = context.copilotkit;
383
+ const currentPending = pending;
384
+ if (!currentPending) {
385
+ core.setInterruptState(null);
386
+ return;
387
+ }
388
+ const legacyEvent = toLegacyEvent(currentPending) as InterruptEvent<TValue>;
389
+ if (config.enabled && !config.enabled(legacyEvent)) {
390
+ core.setInterruptState(null);
391
+ return;
392
+ }
393
+
394
+ const slotProps: InterruptRenderProps<
395
+ TValue,
396
+ InterruptResult<TValue, TResult>
397
+ > = {
398
+ event: legacyEvent,
399
+ interrupt:
400
+ currentPending.kind === "standard"
401
+ ? (currentPending.interrupts[0] ?? null)
402
+ : null,
403
+ interrupts:
404
+ currentPending.kind === "standard" ? currentPending.interrupts : [],
405
+ result: result,
406
+ resolve,
407
+ cancel,
408
+ };
409
+
410
+ core.setInterruptState(
411
+ slotProps as unknown as InterruptRenderProps<unknown, unknown>,
412
+ );
413
+
414
+ return () => {
415
+ if (core.interruptState === slotProps) {
416
+ core.setInterruptState(null);
417
+ }
418
+ };
419
+ });
420
+
421
+ const interrupt = $derived(
422
+ pending ? (toLegacyEvent(pending) as InterruptEvent<TValue>) : null,
423
+ );
424
+
425
+ const hasInterrupt = $derived(pending !== null);
426
+
427
+ const slotProps = $derived.by<InterruptRenderProps<
428
+ TValue,
429
+ InterruptResult<TValue, TResult>
430
+ > | null>(() => {
431
+ const currentPending = pending;
432
+ if (!currentPending) return null;
433
+ const legacyEvent = toLegacyEvent(currentPending) as InterruptEvent<TValue>;
434
+ if (config.enabled && !config.enabled(legacyEvent)) return null;
435
+ return {
436
+ event: legacyEvent,
437
+ interrupt:
438
+ currentPending.kind === "standard"
439
+ ? (currentPending.interrupts[0] ?? null)
440
+ : null,
441
+ interrupts:
442
+ currentPending.kind === "standard" ? currentPending.interrupts : [],
443
+ result: result,
444
+ resolve,
445
+ cancel,
446
+ };
447
+ });
448
+
449
+ return {
450
+ get interrupt() {
451
+ return interrupt;
452
+ },
453
+ get result() {
454
+ return result;
455
+ },
456
+ get hasInterrupt() {
457
+ return hasInterrupt;
458
+ },
459
+ resolve,
460
+ resolveInterrupt,
461
+ cancel,
462
+ cancelInterrupt,
463
+ get slotProps() {
464
+ return slotProps;
465
+ },
466
+ };
467
+ }
@@ -0,0 +1,84 @@
1
+ import { getContext } from "svelte";
2
+ import type { Suggestion } from "@copilotkit/core";
3
+ import { DEFAULT_AGENT_ID } from "@copilotkit/shared";
4
+ import { COPILOT_KIT_KEY } from "../providers/context";
5
+ import type { CopilotKitContextValue } from "../providers/context";
6
+
7
+ export interface CreateSuggestionsOptions {
8
+ agentId?: string | (() => string | undefined);
9
+ }
10
+
11
+ export interface CreateSuggestionsResult {
12
+ suggestions: Suggestion[];
13
+ isLoading: boolean;
14
+ reloadSuggestions: () => void;
15
+ clearSuggestions: () => void;
16
+ }
17
+
18
+ export function createSuggestions(
19
+ options: CreateSuggestionsOptions = {},
20
+ ): CreateSuggestionsResult {
21
+ const context = getContext<CopilotKitContextValue | null>(COPILOT_KIT_KEY);
22
+ if (!context) {
23
+ throw new Error("createSuggestions must be used within CopilotKitProvider");
24
+ }
25
+
26
+ const resolvedAgentId = $derived(
27
+ (typeof options.agentId === "function"
28
+ ? options.agentId()
29
+ : options.agentId) ?? DEFAULT_AGENT_ID,
30
+ );
31
+
32
+ let suggestions = $state<Suggestion[]>([]);
33
+ let isLoading = $state(false);
34
+
35
+ $effect(() => {
36
+ const core = context.copilotkit;
37
+ const result = core.getSuggestions(resolvedAgentId);
38
+ suggestions = result.suggestions;
39
+ isLoading = result.isLoading;
40
+
41
+ const sub = core.subscribe({
42
+ onSuggestionsChanged: ({ agentId: changedAgentId, suggestions: s }) => {
43
+ if (changedAgentId !== resolvedAgentId) return;
44
+ suggestions = s;
45
+ const r = core.getSuggestions(resolvedAgentId);
46
+ isLoading = r.isLoading;
47
+ },
48
+ onSuggestionsStartedLoading: ({ agentId: changedAgentId }) => {
49
+ if (changedAgentId !== resolvedAgentId) return;
50
+ isLoading = true;
51
+ },
52
+ onSuggestionsFinishedLoading: ({ agentId: changedAgentId }) => {
53
+ if (changedAgentId !== resolvedAgentId) return;
54
+ isLoading = false;
55
+ },
56
+ onSuggestionsConfigChanged: () => {
57
+ const r = core.getSuggestions(resolvedAgentId);
58
+ suggestions = r.suggestions;
59
+ isLoading = r.isLoading;
60
+ },
61
+ });
62
+
63
+ return () => sub.unsubscribe();
64
+ });
65
+
66
+ const reloadSuggestions = () => {
67
+ context.copilotkit.reloadSuggestions(resolvedAgentId);
68
+ };
69
+
70
+ const clearSuggestions = () => {
71
+ context.copilotkit.clearSuggestions(resolvedAgentId);
72
+ };
73
+
74
+ return {
75
+ get suggestions() {
76
+ return suggestions;
77
+ },
78
+ get isLoading() {
79
+ return isLoading;
80
+ },
81
+ reloadSuggestions,
82
+ clearSuggestions,
83
+ };
84
+ }