@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.
- package/LICENSE +21 -0
- package/README.md +325 -2
- package/dist/components/chat/CopilotChat.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChat.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatAssistantMessage.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatAssistantMessage.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatAttachmentQueue.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatAttachmentQueue.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatInput.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatInput.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatMessage.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatMessage.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatMessageView.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatMessageView.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatReasoningMessage.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatReasoningMessage.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatSuggestionPill.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatSuggestionPill.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatSuggestionView.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatSuggestionView.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatToggleButton.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatToggleButton.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatToolCallsView.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatToolCallsView.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatUserMessage.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatUserMessage.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatView.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatView.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotPopup.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotPopup.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotSidebar.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotSidebar.svelte.d.ts +1 -0
- package/dist/components/chat/StreamMarkdown.d.svelte.ts +1 -0
- package/dist/components/chat/StreamMarkdown.svelte.d.ts +1 -0
- package/dist/components/chat/chat-config-context.d.svelte.ts +10 -0
- package/dist/components/chat/chat-config-context.svelte.d.ts +10 -0
- package/dist/components/chat/chat-config-context.svelte.d.ts.map +1 -0
- package/dist/components/chat/dispatch-after-active-run.d.ts +3 -0
- package/dist/components/chat/dispatch-after-active-run.d.ts.map +1 -0
- package/dist/components/chat/markdown/CodeBlock.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/CodeSpan.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/Table.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/TableBody.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/TableCell.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/TableHead.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/TableRow.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/highlight.d.ts +8 -0
- package/dist/components/chat/markdown/highlight.d.ts.map +1 -0
- package/dist/components/chat/media-parts.d.ts +11 -0
- package/dist/components/chat/media-parts.d.ts.map +1 -0
- package/dist/components/chat/types.d.ts +137 -0
- package/dist/components/chat/types.d.ts.map +1 -0
- package/dist/components/icons/index.d.ts +2 -0
- package/dist/components/icons/index.d.ts.map +1 -0
- package/dist/components/index.d.ts +18 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/hooks/connect-agent-context.svelte.d.ts +9 -0
- package/dist/hooks/connect-agent-context.svelte.d.ts.map +1 -0
- package/dist/hooks/create-agent.svelte.d.ts +23 -0
- package/dist/hooks/create-agent.svelte.d.ts.map +1 -0
- package/dist/hooks/create-attachments.svelte.d.ts +33 -0
- package/dist/hooks/create-attachments.svelte.d.ts.map +1 -0
- package/dist/hooks/create-capabilities.svelte.d.ts +6 -0
- package/dist/hooks/create-capabilities.svelte.d.ts.map +1 -0
- package/dist/hooks/create-interrupt.svelte.d.ts +24 -0
- package/dist/hooks/create-interrupt.svelte.d.ts.map +1 -0
- package/dist/hooks/create-suggestions.svelte.d.ts +12 -0
- package/dist/hooks/create-suggestions.svelte.d.ts.map +1 -0
- package/dist/hooks/create-threads.svelte.d.ts +33 -0
- package/dist/hooks/create-threads.svelte.d.ts.map +1 -0
- package/dist/hooks/index.d.ts +20 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/load-katex-styles.svelte.d.ts +2 -0
- package/dist/hooks/load-katex-styles.svelte.d.ts.map +1 -0
- package/dist/hooks/register-frontend-tool.svelte.d.ts +3 -0
- package/dist/hooks/register-frontend-tool.svelte.d.ts.map +1 -0
- package/dist/hooks/register-human-in-the-loop.svelte.d.ts +3 -0
- package/dist/hooks/register-human-in-the-loop.svelte.d.ts.map +1 -0
- package/dist/hooks/register-render-tool-call.svelte.d.ts +30 -0
- package/dist/hooks/register-render-tool-call.svelte.d.ts.map +1 -0
- package/dist/index.cjs +5 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +3037 -0
- package/dist/index.mjs.map +1 -0
- package/dist/lib/svelte-core.d.ts +53 -0
- package/dist/lib/svelte-core.d.ts.map +1 -0
- package/dist/providers/CopilotKitProvider.svelte.d.ts +1 -0
- package/dist/providers/CopilotKitProvider.types.d.ts +47 -0
- package/dist/providers/CopilotKitProvider.types.d.ts.map +1 -0
- package/dist/providers/context.d.ts +19 -0
- package/dist/providers/context.d.ts.map +1 -0
- package/dist/providers/index.d.ts +6 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/useCopilotKit.d.ts +3 -0
- package/dist/providers/useCopilotKit.d.ts.map +1 -0
- package/dist/styles.css +3 -0
- package/dist/types/a2ui.d.ts +5 -0
- package/dist/types/a2ui.d.ts.map +1 -0
- package/dist/types/activity-message-renderer.d.ts +17 -0
- package/dist/types/activity-message-renderer.d.ts.map +1 -0
- package/dist/types/custom-message-renderer.d.ts +18 -0
- package/dist/types/custom-message-renderer.d.ts.map +1 -0
- package/dist/types/frontend-tool.d.ts +6 -0
- package/dist/types/frontend-tool.d.ts.map +1 -0
- package/dist/types/human-in-the-loop.d.ts +31 -0
- package/dist/types/human-in-the-loop.d.ts.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/interrupt.d.ts +23 -0
- package/dist/types/interrupt.d.ts.map +1 -0
- package/dist/types/sandbox-function.d.ts +8 -0
- package/dist/types/sandbox-function.d.ts.map +1 -0
- package/dist/types/tool-call-renderer.d.ts +29 -0
- package/dist/types/tool-call-renderer.d.ts.map +1 -0
- package/package.json +114 -3
- package/src/app.d.ts +7 -0
- package/src/components/chat/CopilotChat.svelte +222 -0
- package/src/components/chat/CopilotChatAssistantMessage.svelte +295 -0
- package/src/components/chat/CopilotChatAttachmentQueue.svelte +241 -0
- package/src/components/chat/CopilotChatInput.svelte +192 -0
- package/src/components/chat/CopilotChatMessage.svelte +37 -0
- package/src/components/chat/CopilotChatMessageView.svelte +76 -0
- package/src/components/chat/CopilotChatReasoningMessage.svelte +210 -0
- package/src/components/chat/CopilotChatSuggestionPill.svelte +66 -0
- package/src/components/chat/CopilotChatSuggestionView.svelte +36 -0
- package/src/components/chat/CopilotChatToggleButton.svelte +47 -0
- package/src/components/chat/CopilotChatToolCallsView.svelte +187 -0
- package/src/components/chat/CopilotChatUserMessage.svelte +312 -0
- package/src/components/chat/CopilotChatView.svelte +177 -0
- package/src/components/chat/CopilotPopup.svelte +187 -0
- package/src/components/chat/CopilotSidebar.svelte +138 -0
- package/src/components/chat/StreamMarkdown.svelte +30 -0
- package/src/components/chat/chat-config-context.svelte.ts +28 -0
- package/src/components/chat/dispatch-after-active-run.ts +40 -0
- package/src/components/chat/markdown/CodeBlock.svelte +78 -0
- package/src/components/chat/markdown/CodeSpan.svelte +29 -0
- package/src/components/chat/markdown/Table.svelte +15 -0
- package/src/components/chat/markdown/TableBody.svelte +11 -0
- package/src/components/chat/markdown/TableCell.svelte +29 -0
- package/src/components/chat/markdown/TableHead.svelte +11 -0
- package/src/components/chat/markdown/TableRow.svelte +11 -0
- package/src/components/chat/markdown/highlight.ts +96 -0
- package/src/components/chat/media-parts.ts +34 -0
- package/src/components/chat/types.ts +176 -0
- package/src/components/icons/index.ts +23 -0
- package/src/components/index.ts +17 -0
- package/src/hooks/connect-agent-context.svelte.ts +46 -0
- package/src/hooks/create-agent.svelte.ts +420 -0
- package/src/hooks/create-attachments.svelte.ts +311 -0
- package/src/hooks/create-capabilities.svelte.ts +26 -0
- package/src/hooks/create-interrupt.svelte.ts +467 -0
- package/src/hooks/create-suggestions.svelte.ts +84 -0
- package/src/hooks/create-threads.svelte.ts +337 -0
- package/src/hooks/index.ts +45 -0
- package/src/hooks/load-katex-styles.svelte.ts +13 -0
- package/src/hooks/register-frontend-tool.svelte.ts +45 -0
- package/src/hooks/register-human-in-the-loop.svelte.ts +118 -0
- package/src/hooks/register-render-tool-call.svelte.ts +75 -0
- package/src/index.ts +6 -0
- package/src/lib/svelte-core.ts +230 -0
- package/src/providers/CopilotKitProvider.svelte +291 -0
- package/src/providers/CopilotKitProvider.types.ts +55 -0
- package/src/providers/context.ts +26 -0
- package/src/providers/index.ts +5 -0
- package/src/providers/useCopilotKit.ts +11 -0
- package/src/styles/globals.css +691 -0
- package/src/styles.d.ts +1 -0
- package/src/types/a2ui.ts +5 -0
- package/src/types/activity-message-renderer.ts +21 -0
- package/src/types/custom-message-renderer.ts +23 -0
- package/src/types/frontend-tool.ts +8 -0
- package/src/types/human-in-the-loop.ts +40 -0
- package/src/types/index.ts +8 -0
- package/src/types/interrupt.ts +32 -0
- package/src/types/sandbox-function.ts +8 -0
- package/src/types/tool-call-renderer.ts +36 -0
|
@@ -0,0 +1,311 @@
|
|
|
1
|
+
import {
|
|
2
|
+
randomUUID,
|
|
3
|
+
getModalityFromMimeType,
|
|
4
|
+
exceedsMaxSize,
|
|
5
|
+
readFileAsBase64,
|
|
6
|
+
generateVideoThumbnail,
|
|
7
|
+
matchesAcceptFilter,
|
|
8
|
+
formatFileSize,
|
|
9
|
+
} from "@copilotkit/shared";
|
|
10
|
+
import type {
|
|
11
|
+
Attachment,
|
|
12
|
+
AttachmentUploadResult,
|
|
13
|
+
AttachmentsConfig,
|
|
14
|
+
} from "@copilotkit/shared";
|
|
15
|
+
|
|
16
|
+
export type AttachmentsConfigInput =
|
|
17
|
+
| AttachmentsConfig
|
|
18
|
+
| (() => AttachmentsConfig | undefined);
|
|
19
|
+
|
|
20
|
+
export interface CreateAttachmentsProps {
|
|
21
|
+
config?: AttachmentsConfigInput;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function resolveInput(
|
|
25
|
+
input: AttachmentsConfigInput | undefined,
|
|
26
|
+
): AttachmentsConfig | undefined {
|
|
27
|
+
if (typeof input === "function") {
|
|
28
|
+
return (input as () => AttachmentsConfig | undefined)();
|
|
29
|
+
}
|
|
30
|
+
return input;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
export interface CreateAttachmentsReturn {
|
|
34
|
+
attachments: Attachment[];
|
|
35
|
+
enabled: boolean;
|
|
36
|
+
dragOver: boolean;
|
|
37
|
+
fileInputRef: HTMLInputElement | null;
|
|
38
|
+
containerRef: HTMLElement | null;
|
|
39
|
+
/** Assign the hidden file input (for `bind:this`-style callbacks). */
|
|
40
|
+
setFileInputRef: (el: HTMLInputElement | null) => void;
|
|
41
|
+
/** Assign the paste-scope container (for `bind:this`-style callbacks). */
|
|
42
|
+
setContainerRef: (el: HTMLElement | null) => void;
|
|
43
|
+
/** Svelte action for the hidden file input (`use:attachments.fileInputAction`). */
|
|
44
|
+
fileInputAction: (node: HTMLInputElement) => { destroy: () => void };
|
|
45
|
+
/** Svelte action for the paste-scope container (`use:attachments.containerAction`). */
|
|
46
|
+
containerAction: (node: HTMLElement) => { destroy: () => void };
|
|
47
|
+
processFiles: (files: File[]) => Promise<void>;
|
|
48
|
+
handleFileUpload: (event: Event) => Promise<void>;
|
|
49
|
+
handleDragOver: (event: DragEvent) => void;
|
|
50
|
+
handleDragLeave: (event: DragEvent) => void;
|
|
51
|
+
handleDrop: (event: DragEvent) => Promise<void>;
|
|
52
|
+
removeAttachment: (id: string) => void;
|
|
53
|
+
consumeAttachments: () => Attachment[];
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
export function createAttachments(
|
|
57
|
+
props: CreateAttachmentsProps,
|
|
58
|
+
): CreateAttachmentsReturn {
|
|
59
|
+
let attachments = $state<Attachment[]>([]);
|
|
60
|
+
let dragOver = $state(false);
|
|
61
|
+
let fileInputEl = $state<HTMLInputElement | null>(null);
|
|
62
|
+
let containerEl = $state<HTMLElement | null>(null);
|
|
63
|
+
let attachmentsRef: Attachment[] = [];
|
|
64
|
+
|
|
65
|
+
const resolvedConfig = $derived(resolveInput(props.config));
|
|
66
|
+
const enabled = $derived(resolvedConfig?.enabled ?? false);
|
|
67
|
+
|
|
68
|
+
function setFileInputRef(el: HTMLInputElement | null) {
|
|
69
|
+
fileInputEl = el;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
function setContainerRef(el: HTMLElement | null) {
|
|
73
|
+
containerEl = el;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
function fileInputAction(node: HTMLInputElement) {
|
|
77
|
+
fileInputEl = node;
|
|
78
|
+
return {
|
|
79
|
+
destroy() {
|
|
80
|
+
if (fileInputEl === node) fileInputEl = null;
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function containerAction(node: HTMLElement) {
|
|
86
|
+
containerEl = node;
|
|
87
|
+
return {
|
|
88
|
+
destroy() {
|
|
89
|
+
if (containerEl === node) containerEl = null;
|
|
90
|
+
},
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const setAttachments = (next: Attachment[]) => {
|
|
95
|
+
attachments = next;
|
|
96
|
+
attachmentsRef = next;
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const updateAttachments = (
|
|
100
|
+
updater: (previous: Attachment[]) => Attachment[],
|
|
101
|
+
) => {
|
|
102
|
+
setAttachments(updater(attachmentsRef));
|
|
103
|
+
};
|
|
104
|
+
|
|
105
|
+
async function processFiles(files: File[]) {
|
|
106
|
+
// Read the live config on every call so reactive getter updates apply
|
|
107
|
+
// to in-flight UI without re-creating the hook.
|
|
108
|
+
const config = resolveInput(props.config);
|
|
109
|
+
const accept = config?.accept ?? "*/*";
|
|
110
|
+
const maxSize = config?.maxSize ?? 20 * 1024 * 1024;
|
|
111
|
+
|
|
112
|
+
const rejectedFiles = files.filter(
|
|
113
|
+
(file) => !matchesAcceptFilter(file, accept),
|
|
114
|
+
);
|
|
115
|
+
for (const file of rejectedFiles) {
|
|
116
|
+
config?.onUploadFailed?.({
|
|
117
|
+
reason: "invalid-type",
|
|
118
|
+
file,
|
|
119
|
+
message: `File "${file.name}" is not accepted. Supported types: ${accept}`,
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
const validFiles = files.filter((file) =>
|
|
124
|
+
matchesAcceptFilter(file, accept),
|
|
125
|
+
);
|
|
126
|
+
for (const file of validFiles) {
|
|
127
|
+
if (exceedsMaxSize(file, maxSize)) {
|
|
128
|
+
config?.onUploadFailed?.({
|
|
129
|
+
reason: "file-too-large",
|
|
130
|
+
file,
|
|
131
|
+
message: `File "${file.name}" exceeds the maximum size of ${formatFileSize(maxSize)}`,
|
|
132
|
+
});
|
|
133
|
+
continue;
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
const modality = getModalityFromMimeType(file.type);
|
|
137
|
+
const placeholderId = randomUUID();
|
|
138
|
+
updateAttachments((previous) => [
|
|
139
|
+
...previous,
|
|
140
|
+
{
|
|
141
|
+
id: placeholderId,
|
|
142
|
+
type: modality,
|
|
143
|
+
source: { type: "data", value: "", mimeType: file.type },
|
|
144
|
+
filename: file.name,
|
|
145
|
+
size: file.size,
|
|
146
|
+
status: "uploading",
|
|
147
|
+
},
|
|
148
|
+
]);
|
|
149
|
+
|
|
150
|
+
try {
|
|
151
|
+
let source: Attachment["source"];
|
|
152
|
+
let uploadMetadata: Record<string, unknown> | undefined;
|
|
153
|
+
if (config?.onUpload) {
|
|
154
|
+
const uploadResult: AttachmentUploadResult =
|
|
155
|
+
await config.onUpload(file);
|
|
156
|
+
const { metadata, ...uploadSource } = uploadResult;
|
|
157
|
+
source = uploadSource;
|
|
158
|
+
uploadMetadata = metadata;
|
|
159
|
+
} else {
|
|
160
|
+
const base64 = await readFileAsBase64(file);
|
|
161
|
+
source = { type: "data", value: base64, mimeType: file.type };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
let thumbnail: string | undefined;
|
|
165
|
+
if (modality === "video") {
|
|
166
|
+
thumbnail = await generateVideoThumbnail(file);
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
updateAttachments((previous) =>
|
|
170
|
+
previous.map((attachment) =>
|
|
171
|
+
attachment.id === placeholderId
|
|
172
|
+
? {
|
|
173
|
+
...attachment,
|
|
174
|
+
source,
|
|
175
|
+
status: "ready",
|
|
176
|
+
thumbnail,
|
|
177
|
+
metadata: uploadMetadata,
|
|
178
|
+
}
|
|
179
|
+
: attachment,
|
|
180
|
+
),
|
|
181
|
+
);
|
|
182
|
+
} catch (error) {
|
|
183
|
+
updateAttachments((previous) =>
|
|
184
|
+
previous.filter((a) => a.id !== placeholderId),
|
|
185
|
+
);
|
|
186
|
+
console.error(`[CopilotKit] Failed to upload "${file.name}":`, error);
|
|
187
|
+
config?.onUploadFailed?.({
|
|
188
|
+
reason: "upload-failed",
|
|
189
|
+
file,
|
|
190
|
+
message:
|
|
191
|
+
error instanceof Error
|
|
192
|
+
? error.message
|
|
193
|
+
: `Failed to upload "${file.name}"`,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
async function handleFileUpload(event: Event) {
|
|
200
|
+
const target = event.target as HTMLInputElement | null;
|
|
201
|
+
if (!target?.files?.length) return;
|
|
202
|
+
await processFiles(Array.from(target.files));
|
|
203
|
+
if (target) target.value = "";
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
function handleDragOver(event: DragEvent) {
|
|
207
|
+
if (!enabled) return;
|
|
208
|
+
event.preventDefault();
|
|
209
|
+
event.stopPropagation();
|
|
210
|
+
dragOver = true;
|
|
211
|
+
}
|
|
212
|
+
|
|
213
|
+
function handleDragLeave(event: DragEvent) {
|
|
214
|
+
event.preventDefault();
|
|
215
|
+
event.stopPropagation();
|
|
216
|
+
dragOver = false;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
async function handleDrop(event: DragEvent) {
|
|
220
|
+
event.preventDefault();
|
|
221
|
+
event.stopPropagation();
|
|
222
|
+
dragOver = false;
|
|
223
|
+
if (!enabled) return;
|
|
224
|
+
const files = Array.from(event.dataTransfer?.files ?? []);
|
|
225
|
+
if (files.length > 0) {
|
|
226
|
+
await processFiles(files);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
function removeAttachment(id: string) {
|
|
231
|
+
updateAttachments((previous) =>
|
|
232
|
+
previous.filter((attachment) => attachment.id !== id),
|
|
233
|
+
);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
function consumeAttachments() {
|
|
237
|
+
const ready = attachmentsRef.filter(
|
|
238
|
+
(attachment) => attachment.status === "ready",
|
|
239
|
+
);
|
|
240
|
+
if (ready.length === 0) return ready;
|
|
241
|
+
updateAttachments((previous) =>
|
|
242
|
+
previous.filter((attachment) => attachment.status !== "ready"),
|
|
243
|
+
);
|
|
244
|
+
if (fileInputEl) {
|
|
245
|
+
fileInputEl.value = "";
|
|
246
|
+
}
|
|
247
|
+
return ready;
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
async function handlePaste(event: ClipboardEvent) {
|
|
251
|
+
const liveConfig = resolveInput(props.config);
|
|
252
|
+
if (!(liveConfig?.enabled ?? false)) return;
|
|
253
|
+
const target = event.target as HTMLElement | null;
|
|
254
|
+
const scope = containerEl;
|
|
255
|
+
// Paste is scoped to the composer/container: ignore pastes outside it so
|
|
256
|
+
// unrelated page pastes never enqueue uploads.
|
|
257
|
+
if (!target || !scope?.contains(target)) return;
|
|
258
|
+
const accept = liveConfig?.accept ?? "*/*";
|
|
259
|
+
const items = Array.from(event.clipboardData?.items ?? []);
|
|
260
|
+
const fileItems = items.filter((item) => {
|
|
261
|
+
if (item.kind !== "file") return false;
|
|
262
|
+
const file = item.getAsFile();
|
|
263
|
+
return file !== null && matchesAcceptFilter(file, accept);
|
|
264
|
+
});
|
|
265
|
+
if (fileItems.length === 0) return;
|
|
266
|
+
event.preventDefault();
|
|
267
|
+
const files = fileItems
|
|
268
|
+
.map((item) => item.getAsFile())
|
|
269
|
+
.filter((file): file is File => file !== null);
|
|
270
|
+
await processFiles(files);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
$effect(() => {
|
|
274
|
+
if (typeof document === "undefined") return;
|
|
275
|
+
// Always attached; enabled + container scoping is checked at event time
|
|
276
|
+
// so reactive config updates apply without re-subscribing.
|
|
277
|
+
document.addEventListener("paste", handlePaste);
|
|
278
|
+
return () => {
|
|
279
|
+
document.removeEventListener("paste", handlePaste);
|
|
280
|
+
};
|
|
281
|
+
});
|
|
282
|
+
|
|
283
|
+
return {
|
|
284
|
+
get attachments() {
|
|
285
|
+
return attachments;
|
|
286
|
+
},
|
|
287
|
+
get enabled() {
|
|
288
|
+
return enabled;
|
|
289
|
+
},
|
|
290
|
+
get dragOver() {
|
|
291
|
+
return dragOver;
|
|
292
|
+
},
|
|
293
|
+
get fileInputRef() {
|
|
294
|
+
return fileInputEl;
|
|
295
|
+
},
|
|
296
|
+
get containerRef() {
|
|
297
|
+
return containerEl;
|
|
298
|
+
},
|
|
299
|
+
setFileInputRef,
|
|
300
|
+
setContainerRef,
|
|
301
|
+
fileInputAction,
|
|
302
|
+
containerAction,
|
|
303
|
+
processFiles,
|
|
304
|
+
handleFileUpload,
|
|
305
|
+
handleDragOver,
|
|
306
|
+
handleDragLeave,
|
|
307
|
+
handleDrop,
|
|
308
|
+
removeAttachment,
|
|
309
|
+
consumeAttachments,
|
|
310
|
+
};
|
|
311
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AgentCapabilities } from "@ag-ui/core";
|
|
2
|
+
import { createAgent } from "./create-agent.svelte";
|
|
3
|
+
|
|
4
|
+
export interface CreateCapabilitiesResult {
|
|
5
|
+
capabilities: AgentCapabilities | undefined;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export function createCapabilities(
|
|
9
|
+
agentId?: string | (() => string | undefined),
|
|
10
|
+
): CreateCapabilitiesResult {
|
|
11
|
+
const agentHandle = createAgent({
|
|
12
|
+
get agentId() {
|
|
13
|
+
return typeof agentId === "function" ? agentId() : agentId;
|
|
14
|
+
},
|
|
15
|
+
});
|
|
16
|
+
|
|
17
|
+
return {
|
|
18
|
+
get capabilities() {
|
|
19
|
+
const a = agentHandle.agent;
|
|
20
|
+
if (a && "capabilities" in a) {
|
|
21
|
+
return (a as { capabilities?: AgentCapabilities }).capabilities;
|
|
22
|
+
}
|
|
23
|
+
return undefined;
|
|
24
|
+
},
|
|
25
|
+
};
|
|
26
|
+
}
|