@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.
- package/{src → dist}/components/chat/CopilotChat.svelte +26 -1
- package/dist/components/chat/CopilotChat.svelte.d.ts +5 -1
- package/dist/components/chat/CopilotChat.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotChatAssistantMessage.svelte.d.ts +80 -1
- package/dist/components/chat/CopilotChatAssistantMessage.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotChatAttachmentQueue.svelte.d.ts +9 -1
- package/dist/components/chat/CopilotChatAttachmentQueue.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotChatInput.svelte.d.ts +20 -1
- package/dist/components/chat/CopilotChatInput.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotChatMessage.svelte.d.ts +10 -1
- package/dist/components/chat/CopilotChatMessage.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotChatMessageView.svelte.d.ts +14 -1
- package/dist/components/chat/CopilotChatMessageView.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotChatReasoningMessage.svelte.d.ts +40 -1
- package/dist/components/chat/CopilotChatReasoningMessage.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotChatSuggestionPill.svelte.d.ts +10 -1
- package/dist/components/chat/CopilotChatSuggestionPill.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotChatSuggestionView.svelte.d.ts +10 -1
- package/dist/components/chat/CopilotChatSuggestionView.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotChatToggleButton.svelte.d.ts +8 -1
- package/dist/components/chat/CopilotChatToggleButton.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotChatToolCallsView.svelte.d.ts +12 -1
- package/dist/components/chat/CopilotChatToolCallsView.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotChatUserMessage.svelte.d.ts +62 -1
- package/dist/components/chat/CopilotChatUserMessage.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotChatView.svelte.d.ts +32 -1
- package/dist/components/chat/CopilotChatView.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotPopup.svelte.d.ts +5 -1
- package/dist/components/chat/CopilotPopup.svelte.d.ts.map +1 -0
- package/dist/components/chat/CopilotSidebar.svelte.d.ts +5 -1
- package/dist/components/chat/CopilotSidebar.svelte.d.ts.map +1 -0
- package/dist/components/chat/StreamMarkdown.svelte.d.ts +7 -1
- package/dist/components/chat/StreamMarkdown.svelte.d.ts.map +1 -0
- package/dist/components/chat/chat-config-context.svelte.js +20 -0
- package/dist/components/chat/dispatch-after-active-run.d.ts +1 -1
- package/dist/components/chat/dispatch-after-active-run.js +31 -0
- package/dist/components/chat/markdown/CodeBlock.svelte.d.ts +8 -1
- package/dist/components/chat/markdown/CodeBlock.svelte.d.ts.map +1 -0
- package/dist/components/chat/markdown/CodeSpan.svelte.d.ts +9 -1
- package/dist/components/chat/markdown/CodeSpan.svelte.d.ts.map +1 -0
- package/dist/components/chat/markdown/Table.svelte.d.ts +8 -1
- package/dist/components/chat/markdown/Table.svelte.d.ts.map +1 -0
- package/dist/components/chat/markdown/TableBody.svelte.d.ts +8 -1
- package/dist/components/chat/markdown/TableBody.svelte.d.ts.map +1 -0
- package/dist/components/chat/markdown/TableCell.svelte.d.ts +10 -1
- package/dist/components/chat/markdown/TableCell.svelte.d.ts.map +1 -0
- package/dist/components/chat/markdown/TableHead.svelte.d.ts +8 -1
- package/dist/components/chat/markdown/TableHead.svelte.d.ts.map +1 -0
- package/dist/components/chat/markdown/TableRow.svelte.d.ts +8 -1
- package/dist/components/chat/markdown/TableRow.svelte.d.ts.map +1 -0
- package/dist/components/chat/markdown/highlight.js +91 -0
- package/dist/components/chat/media-parts.d.ts +2 -2
- package/dist/components/chat/media-parts.js +17 -0
- package/dist/components/chat/types.d.ts +5 -3
- package/dist/components/chat/types.js +7 -0
- package/dist/components/icons/index.d.ts +1 -1
- package/dist/components/icons/index.js +1 -0
- package/dist/components/index.d.ts +17 -17
- package/dist/hooks/connect-agent-context.svelte.js +25 -0
- package/dist/hooks/create-agent.svelte.d.ts +3 -3
- package/dist/hooks/create-agent.svelte.js +341 -0
- package/dist/hooks/create-attachments.svelte.d.ts +1 -1
- package/dist/hooks/create-attachments.svelte.js +232 -0
- package/dist/hooks/create-capabilities.svelte.d.ts +1 -1
- package/dist/hooks/create-capabilities.svelte.js +17 -0
- package/dist/hooks/create-interrupt.svelte.d.ts +2 -2
- package/dist/hooks/create-interrupt.svelte.js +333 -0
- package/dist/hooks/create-suggestions.svelte.d.ts +1 -1
- package/dist/hooks/create-suggestions.svelte.js +61 -0
- package/dist/hooks/create-threads.svelte.js +230 -0
- package/dist/hooks/index.d.ts +19 -19
- package/dist/hooks/index.js +11 -0
- package/dist/hooks/load-katex-styles.svelte.js +11 -0
- package/dist/hooks/register-frontend-tool.svelte.d.ts +1 -1
- package/dist/hooks/register-frontend-tool.svelte.js +32 -0
- package/dist/hooks/register-human-in-the-loop.svelte.d.ts +1 -1
- package/dist/hooks/register-human-in-the-loop.svelte.js +84 -0
- package/dist/hooks/register-render-tool-call.svelte.d.ts +1 -1
- package/dist/hooks/register-render-tool-call.svelte.js +26 -0
- package/dist/index.d.ts +5 -5
- package/dist/lib/svelte-core.d.ts +4 -3
- package/dist/lib/svelte-core.js +145 -0
- package/dist/providers/CopilotKitProvider.svelte.d.ts +9 -1
- package/dist/providers/CopilotKitProvider.svelte.d.ts.map +1 -0
- package/dist/providers/CopilotKitProvider.types.d.ts +4 -4
- package/dist/providers/CopilotKitProvider.types.js +1 -0
- package/dist/providers/context.d.ts +3 -3
- package/dist/providers/context.js +2 -0
- package/dist/providers/index.d.ts +5 -5
- package/dist/providers/index.js +2 -0
- package/dist/providers/useCopilotKit.d.ts +1 -1
- package/dist/providers/useCopilotKit.js +9 -0
- package/dist/styles.css +1 -2
- package/dist/types/a2ui.js +1 -0
- package/dist/types/activity-message-renderer.d.ts +3 -3
- package/dist/types/activity-message-renderer.js +1 -0
- package/dist/types/custom-message-renderer.d.ts +1 -1
- package/dist/types/custom-message-renderer.js +1 -0
- package/dist/types/frontend-tool.d.ts +2 -2
- package/dist/types/frontend-tool.js +1 -0
- package/dist/types/human-in-the-loop.d.ts +1 -1
- package/dist/types/human-in-the-loop.js +1 -0
- package/dist/types/index.d.ts +8 -8
- package/dist/types/interrupt.d.ts +1 -1
- package/dist/types/interrupt.js +1 -0
- package/dist/types/sandbox-function.d.ts +1 -1
- package/dist/types/sandbox-function.js +1 -0
- package/dist/types/tool-call-renderer.d.ts +2 -2
- package/dist/types/tool-call-renderer.js +1 -0
- package/package.json +14 -22
- package/dist/components/chat/CopilotChat.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotChatAssistantMessage.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotChatAttachmentQueue.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotChatInput.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotChatMessage.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotChatMessageView.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotChatReasoningMessage.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotChatSuggestionPill.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotChatSuggestionView.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotChatToggleButton.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotChatToolCallsView.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotChatUserMessage.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotChatView.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotPopup.d.svelte.ts +0 -1
- package/dist/components/chat/CopilotSidebar.d.svelte.ts +0 -1
- package/dist/components/chat/StreamMarkdown.d.svelte.ts +0 -1
- package/dist/components/chat/chat-config-context.d.svelte.ts +0 -10
- package/dist/index.cjs +0 -5
- package/dist/index.cjs.map +0 -1
- package/dist/index.d.cts +0 -6
- package/dist/index.d.mts +0 -6
- package/dist/index.mjs +0 -3037
- package/dist/index.mjs.map +0 -1
- package/src/components/chat/chat-config-context.svelte.ts +0 -28
- package/src/components/chat/dispatch-after-active-run.ts +0 -40
- package/src/components/chat/markdown/highlight.ts +0 -96
- package/src/components/chat/media-parts.ts +0 -34
- package/src/components/chat/types.ts +0 -176
- package/src/components/icons/index.ts +0 -23
- package/src/hooks/connect-agent-context.svelte.ts +0 -46
- package/src/hooks/create-agent.svelte.ts +0 -420
- package/src/hooks/create-attachments.svelte.ts +0 -311
- package/src/hooks/create-capabilities.svelte.ts +0 -26
- package/src/hooks/create-interrupt.svelte.ts +0 -467
- package/src/hooks/create-suggestions.svelte.ts +0 -84
- package/src/hooks/create-threads.svelte.ts +0 -337
- package/src/hooks/index.ts +0 -45
- package/src/hooks/load-katex-styles.svelte.ts +0 -13
- package/src/hooks/register-frontend-tool.svelte.ts +0 -45
- package/src/hooks/register-human-in-the-loop.svelte.ts +0 -118
- package/src/hooks/register-render-tool-call.svelte.ts +0 -75
- package/src/lib/svelte-core.ts +0 -230
- package/src/providers/CopilotKitProvider.types.ts +0 -55
- package/src/providers/context.ts +0 -26
- package/src/providers/index.ts +0 -5
- package/src/providers/useCopilotKit.ts +0 -11
- package/src/types/a2ui.ts +0 -5
- package/src/types/activity-message-renderer.ts +0 -21
- package/src/types/custom-message-renderer.ts +0 -23
- package/src/types/frontend-tool.ts +0 -8
- package/src/types/human-in-the-loop.ts +0 -40
- package/src/types/interrupt.ts +0 -32
- package/src/types/sandbox-function.ts +0 -8
- package/src/types/tool-call-renderer.ts +0 -36
- /package/{src → dist}/app.d.ts +0 -0
- /package/{src → dist}/components/chat/CopilotChatAssistantMessage.svelte +0 -0
- /package/{src → dist}/components/chat/CopilotChatAttachmentQueue.svelte +0 -0
- /package/{src → dist}/components/chat/CopilotChatInput.svelte +0 -0
- /package/{src → dist}/components/chat/CopilotChatMessage.svelte +0 -0
- /package/{src → dist}/components/chat/CopilotChatMessageView.svelte +0 -0
- /package/{src → dist}/components/chat/CopilotChatReasoningMessage.svelte +0 -0
- /package/{src → dist}/components/chat/CopilotChatSuggestionPill.svelte +0 -0
- /package/{src → dist}/components/chat/CopilotChatSuggestionView.svelte +0 -0
- /package/{src → dist}/components/chat/CopilotChatToggleButton.svelte +0 -0
- /package/{src → dist}/components/chat/CopilotChatToolCallsView.svelte +0 -0
- /package/{src → dist}/components/chat/CopilotChatUserMessage.svelte +0 -0
- /package/{src → dist}/components/chat/CopilotChatView.svelte +0 -0
- /package/{src → dist}/components/chat/CopilotPopup.svelte +0 -0
- /package/{src → dist}/components/chat/CopilotSidebar.svelte +0 -0
- /package/{src → dist}/components/chat/StreamMarkdown.svelte +0 -0
- /package/{src → dist}/components/chat/markdown/CodeBlock.svelte +0 -0
- /package/{src → dist}/components/chat/markdown/CodeSpan.svelte +0 -0
- /package/{src → dist}/components/chat/markdown/Table.svelte +0 -0
- /package/{src → dist}/components/chat/markdown/TableBody.svelte +0 -0
- /package/{src → dist}/components/chat/markdown/TableCell.svelte +0 -0
- /package/{src → dist}/components/chat/markdown/TableHead.svelte +0 -0
- /package/{src → dist}/components/chat/markdown/TableRow.svelte +0 -0
- /package/{src/components/index.ts → dist/components/index.js} +0 -0
- /package/{src/index.ts → dist/index.js} +0 -0
- /package/{src → dist}/providers/CopilotKitProvider.svelte +0 -0
- /package/{src → dist}/styles/globals.css +0 -0
- /package/{src → dist}/styles.d.ts +0 -0
- /package/{src/types/index.ts → dist/types/index.js} +0 -0
|
@@ -1,467 +0,0 @@
|
|
|
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
|
-
}
|
|
@@ -1,84 +0,0 @@
|
|
|
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
|
-
}
|