@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
package/package.json
CHANGED
|
@@ -1,6 +1,117 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@copilotkit/svelte",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"description": "
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"private": false,
|
|
5
|
+
"description": "Svelte 5 components and runes for CopilotKit",
|
|
6
|
+
"keywords": [
|
|
7
|
+
"ai",
|
|
8
|
+
"assistant",
|
|
9
|
+
"automation",
|
|
10
|
+
"copilot",
|
|
11
|
+
"copilotkit",
|
|
12
|
+
"svelte",
|
|
13
|
+
"svelte5"
|
|
14
|
+
],
|
|
15
|
+
"homepage": "https://github.com/CopilotKit/CopilotKit",
|
|
16
|
+
"license": "MIT",
|
|
17
|
+
"repository": {
|
|
18
|
+
"type": "git",
|
|
19
|
+
"url": "https://github.com/CopilotKit/CopilotKit.git"
|
|
20
|
+
},
|
|
21
|
+
"type": "module",
|
|
22
|
+
"sideEffects": [
|
|
23
|
+
"**/*.css"
|
|
24
|
+
],
|
|
25
|
+
"files": [
|
|
26
|
+
"dist",
|
|
27
|
+
"src",
|
|
28
|
+
"!src/__tests__",
|
|
29
|
+
"LICENSE",
|
|
30
|
+
"README.md"
|
|
31
|
+
],
|
|
32
|
+
"svelte": "./src/index.ts",
|
|
33
|
+
"main": "./dist/index.cjs",
|
|
34
|
+
"module": "./dist/index.mjs",
|
|
35
|
+
"types": "./dist/index.d.ts",
|
|
36
|
+
"exports": {
|
|
37
|
+
".": {
|
|
38
|
+
"svelte": "./src/index.ts",
|
|
39
|
+
"import": {
|
|
40
|
+
"types": "./dist/index.d.ts",
|
|
41
|
+
"default": "./dist/index.mjs"
|
|
42
|
+
},
|
|
43
|
+
"require": {
|
|
44
|
+
"types": "./dist/index.d.cts",
|
|
45
|
+
"default": "./dist/index.cjs"
|
|
46
|
+
},
|
|
47
|
+
"default": "./dist/index.mjs"
|
|
48
|
+
},
|
|
49
|
+
"./styles.css": "./dist/styles.css",
|
|
50
|
+
"./package.json": "./package.json"
|
|
51
|
+
},
|
|
52
|
+
"publishConfig": {
|
|
53
|
+
"access": "public"
|
|
54
|
+
},
|
|
55
|
+
"dependencies": {
|
|
56
|
+
"@ag-ui/client": "1.0.2",
|
|
57
|
+
"@ag-ui/core": "1.0.2",
|
|
58
|
+
"@copilotkit/core": "1.87.1",
|
|
59
|
+
"@copilotkit/shared": "1.87.1",
|
|
60
|
+
"@copilotkit/web-components": "1.87.1",
|
|
61
|
+
"@copilotkit/web-inspector": "1.87.1",
|
|
62
|
+
"@humanspeak/svelte-markdown": "^1.7.3",
|
|
63
|
+
"@jetbrains/websandbox": "^1.1.3",
|
|
64
|
+
"highlight.js": "^11.12.0",
|
|
65
|
+
"katex": "^0.16.22",
|
|
66
|
+
"lucide-svelte": "^1.0.1",
|
|
67
|
+
"marked": "^16.0.0",
|
|
68
|
+
"zod": "^3.23.3",
|
|
69
|
+
"zod-to-json-schema": "^3.23.5"
|
|
70
|
+
},
|
|
71
|
+
"devDependencies": {
|
|
72
|
+
"@arethetypeswrong/cli": "^0.18.2",
|
|
73
|
+
"@eslint/js": "^9.39.2",
|
|
74
|
+
"@sveltejs/vite-plugin-svelte": "^5.0.0",
|
|
75
|
+
"@tailwindcss/cli": "^4.1.11",
|
|
76
|
+
"@tailwindcss/typography": "^0.5.16",
|
|
77
|
+
"@testing-library/svelte": "^5.2.0",
|
|
78
|
+
"@types/node": "^22.15.3",
|
|
79
|
+
"es-check": "9.6.4",
|
|
80
|
+
"eslint": "^9.30.0",
|
|
81
|
+
"eslint-plugin-svelte": "^3.0.0",
|
|
82
|
+
"globals": "^16.5.0",
|
|
83
|
+
"jsdom": "^26.1.0",
|
|
84
|
+
"postcss": "^8.5.6",
|
|
85
|
+
"publint": "^0.3.17",
|
|
86
|
+
"svelte": "^5.28.0",
|
|
87
|
+
"svelte-check": "^4.0.0",
|
|
88
|
+
"tailwindcss": "^4.0.8",
|
|
89
|
+
"tw-animate-css": "^1.4.0",
|
|
90
|
+
"typescript": "5.8.2",
|
|
91
|
+
"typescript-eslint": "^8.52.0",
|
|
92
|
+
"vite": "^6.0.0",
|
|
93
|
+
"vite-plugin-dts": "^5.0.3",
|
|
94
|
+
"vitest": "^4.1.3"
|
|
95
|
+
},
|
|
96
|
+
"peerDependencies": {
|
|
97
|
+
"svelte": "^5.0.0"
|
|
98
|
+
},
|
|
99
|
+
"engines": {
|
|
100
|
+
"node": ">=20"
|
|
101
|
+
},
|
|
102
|
+
"scripts": {
|
|
103
|
+
"build": "vite build && node scripts/fix-dts-paths.mjs && pnpm build:css && node -e \"\nconst fs=require('fs');\nconst path=require('path');\n// Copy root types for CJS/ESM\nfs.cpSync('./dist/index.d.ts','./dist/index.d.cts');\nfs.cpSync('./dist/index.d.ts','./dist/index.d.mts');\n// Rename .svelte.d.ts -> .d.svelte.ts for Svelte component types\nconst dir='./dist/components/chat';\nif(fs.existsSync(dir)){\n fs.readdirSync(dir).filter(f=>f.endsWith('.svelte.d.ts')).forEach(f=>{\n const old=path.join(dir,f);\n const New=path.join(dir,f.replace('.svelte.d.ts','.d.svelte.ts'));\n fs.cpSync(old,New);\n });\n}\n\"",
|
|
104
|
+
"build:css": "tailwindcss -i ./src/styles/globals.css -o ./dist/styles.css -m && node scripts/scope-preflight.mjs ./dist/styles.css && node scripts/merge-styles.mjs ./dist/styles.css ./dist/svelte.css",
|
|
105
|
+
"dev": "vite build --watch",
|
|
106
|
+
"test": "vitest run",
|
|
107
|
+
"test:watch": "vitest",
|
|
108
|
+
"test:coverage": "vitest run --coverage",
|
|
109
|
+
"test:ui": "vitest --ui",
|
|
110
|
+
"lint": "eslint .",
|
|
111
|
+
"typecheck": "svelte-check --workspace ./src --tsconfig ../tsconfig.json --config ../svelte.config.js",
|
|
112
|
+
"compat-check": "es-check es2022 --module 'dist/**/*.{mjs,cjs,js}'",
|
|
113
|
+
"publint": "publint .",
|
|
114
|
+
"attw": "attw --pack . --profile node16 --exclude-entrypoints ./styles.css --ignore-rules internal-resolution-error",
|
|
115
|
+
"clean": "rm -rf dist"
|
|
116
|
+
}
|
|
6
117
|
}
|
package/src/app.d.ts
ADDED
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { createAgent, createAttachments, createSuggestions } from "../../hooks";
|
|
3
|
+
import {
|
|
4
|
+
DEFAULT_AGENT_ID,
|
|
5
|
+
createAttachmentContent,
|
|
6
|
+
randomUUID,
|
|
7
|
+
} from "@copilotkit/shared";
|
|
8
|
+
import type { AttachmentsConfig, InputContent } from "@copilotkit/shared";
|
|
9
|
+
import type { Suggestion } from "@copilotkit/core";
|
|
10
|
+
import type { UserMessage } from "@ag-ui/core";
|
|
11
|
+
import { useCopilotKit } from "../../providers/useCopilotKit";
|
|
12
|
+
import { setChatConfig, getChatConfig, ChatConfig } from "./chat-config-context.svelte";
|
|
13
|
+
import CopilotChatView from "./CopilotChatView.svelte";
|
|
14
|
+
import { dispatchAfterActiveRunSettles } from "./dispatch-after-active-run";
|
|
15
|
+
import type { CopilotChatProps, CopilotChatInputMode, ToolsMenuItem } from "./types";
|
|
16
|
+
|
|
17
|
+
function normalizeAttachmentsConfig(
|
|
18
|
+
value: CopilotChatProps["attachments"],
|
|
19
|
+
): AttachmentsConfig | undefined {
|
|
20
|
+
if (value === undefined || value === false) return undefined;
|
|
21
|
+
if (value === true) return { enabled: true };
|
|
22
|
+
return {
|
|
23
|
+
...value,
|
|
24
|
+
enabled: value.enabled ?? true,
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
let {
|
|
29
|
+
agentId,
|
|
30
|
+
threadId: explicitThreadId,
|
|
31
|
+
throttleMs,
|
|
32
|
+
autoScroll = true,
|
|
33
|
+
welcomeScreen = true,
|
|
34
|
+
inputValue: controlledInputValue,
|
|
35
|
+
onInputChange,
|
|
36
|
+
inputMode = "input" as CopilotChatInputMode,
|
|
37
|
+
inputToolsMenu = [] as (ToolsMenuItem | "-")[],
|
|
38
|
+
attachments: attachmentsProp,
|
|
39
|
+
className = "",
|
|
40
|
+
}: CopilotChatProps = $props();
|
|
41
|
+
|
|
42
|
+
let generatedThreadId = $state(randomUUID());
|
|
43
|
+
let resolvedThreadId = $derived(explicitThreadId ?? generatedThreadId);
|
|
44
|
+
let hasExplicitThreadId = $derived(!!explicitThreadId);
|
|
45
|
+
let resolvedAgentId = $derived(agentId ?? DEFAULT_AGENT_ID);
|
|
46
|
+
let attachmentsConfig = $derived(normalizeAttachmentsConfig(attachmentsProp));
|
|
47
|
+
|
|
48
|
+
// svelte-ignore state_referenced_locally
|
|
49
|
+
setChatConfig(new ChatConfig(
|
|
50
|
+
agentId ?? DEFAULT_AGENT_ID,
|
|
51
|
+
resolvedThreadId,
|
|
52
|
+
hasExplicitThreadId,
|
|
53
|
+
));
|
|
54
|
+
|
|
55
|
+
let agentHandle = $state<ReturnType<typeof createAgent> | null>(null);
|
|
56
|
+
let suggestionsHandle = $state<ReturnType<typeof createSuggestions> | null>(null);
|
|
57
|
+
|
|
58
|
+
// Runs during init (before first render) and when agentId/threadId change.
|
|
59
|
+
// createAgent/createSuggestions inherently have subscribing side-effects
|
|
60
|
+
// (AG-UI event listeners), so $effect.pre is the correct rune — $derived
|
|
61
|
+
// requires pure expressions.
|
|
62
|
+
$effect.pre(() => {
|
|
63
|
+
const id = resolvedAgentId;
|
|
64
|
+
const tid = resolvedThreadId;
|
|
65
|
+
const config = getChatConfig();
|
|
66
|
+
if (config) {
|
|
67
|
+
config.agentId = id;
|
|
68
|
+
config.threadId = tid;
|
|
69
|
+
}
|
|
70
|
+
agentHandle = createAgent({ agentId: id, threadId: tid, throttleMs });
|
|
71
|
+
suggestionsHandle = createSuggestions({ agentId: id });
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
let { copilotkit } = useCopilotKit();
|
|
75
|
+
let agent = $derived(agentHandle?.agent ?? null);
|
|
76
|
+
let messages = $derived(agentHandle?.messages ?? []);
|
|
77
|
+
let isRunning = $derived(agentHandle?.isRunning ?? false);
|
|
78
|
+
let suggestions = $derived(suggestionsHandle?.suggestions ?? []);
|
|
79
|
+
let sendError = $state<string | null>(null);
|
|
80
|
+
let sentMessageId = $state<string | null>(null);
|
|
81
|
+
|
|
82
|
+
// Reactive config via getter so prop updates flow into the hook without
|
|
83
|
+
// re-creating it. Refs bind through Svelte actions (see markup below).
|
|
84
|
+
const attachmentsHandle = createAttachments({
|
|
85
|
+
config: () => attachmentsConfig,
|
|
86
|
+
});
|
|
87
|
+
let selectedAttachments = $derived(attachmentsHandle.attachments);
|
|
88
|
+
let attachmentsEnabled = $derived(attachmentsHandle.enabled);
|
|
89
|
+
let dragOver = $derived(attachmentsHandle.dragOver);
|
|
90
|
+
|
|
91
|
+
function handleAddFile() {
|
|
92
|
+
if (!attachmentsEnabled) return;
|
|
93
|
+
// Defer one tick so any open menu closes before the picker opens.
|
|
94
|
+
setTimeout(() => {
|
|
95
|
+
attachmentsHandle.fileInputRef?.click();
|
|
96
|
+
}, 100);
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
async function sendMessage(value: string, includeAttachments = false) {
|
|
100
|
+
const activeAgent = agent;
|
|
101
|
+
if (!activeAgent) return;
|
|
102
|
+
sendError = null;
|
|
103
|
+
|
|
104
|
+
try {
|
|
105
|
+
await dispatchAfterActiveRunSettles(activeAgent, async () => {
|
|
106
|
+
if (includeAttachments && selectedAttachments.some((a) => a.status === "uploading")) {
|
|
107
|
+
sendError = "Wait for attachments to finish uploading before sending.";
|
|
108
|
+
return;
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
const ready = includeAttachments
|
|
112
|
+
? attachmentsHandle.consumeAttachments()
|
|
113
|
+
: [];
|
|
114
|
+
const messageId = randomUUID();
|
|
115
|
+
if (ready.length > 0) {
|
|
116
|
+
const contentParts: InputContent[] = [];
|
|
117
|
+
if (value.trim()) {
|
|
118
|
+
contentParts.push({ type: "text", text: value });
|
|
119
|
+
}
|
|
120
|
+
for (const attachment of ready) {
|
|
121
|
+
contentParts.push(createAttachmentContent(attachment));
|
|
122
|
+
}
|
|
123
|
+
activeAgent.addMessage({
|
|
124
|
+
id: messageId,
|
|
125
|
+
role: "user",
|
|
126
|
+
content: contentParts,
|
|
127
|
+
} as UserMessage);
|
|
128
|
+
} else {
|
|
129
|
+
activeAgent.addMessage({
|
|
130
|
+
id: messageId,
|
|
131
|
+
role: "user",
|
|
132
|
+
content: value,
|
|
133
|
+
} as UserMessage);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
sentMessageId = messageId;
|
|
137
|
+
await copilotkit.runAgent({ agent: activeAgent });
|
|
138
|
+
});
|
|
139
|
+
} catch (error) {
|
|
140
|
+
console.error("CopilotChat: runAgent failed after sending a message", error);
|
|
141
|
+
sendError = "The message could not be sent. Please try again.";
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function handleSubmitMessage(value: string) {
|
|
146
|
+
return sendMessage(value, true);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function handleStop() {
|
|
150
|
+
if (agent) {
|
|
151
|
+
copilotkit.stopAgent({ agent });
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
function handleInputChange(value: string) {
|
|
156
|
+
onInputChange?.(value);
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
function handleSelectSuggestion(suggestion: Suggestion) {
|
|
160
|
+
return sendMessage(suggestion.message);
|
|
161
|
+
}
|
|
162
|
+
</script>
|
|
163
|
+
|
|
164
|
+
<div
|
|
165
|
+
data-copilotkit
|
|
166
|
+
class="copilotkit-chat {className}"
|
|
167
|
+
use:attachmentsHandle.containerAction
|
|
168
|
+
>
|
|
169
|
+
{#if sendError}
|
|
170
|
+
<p class="copilotkit-chat-error" role="alert">{sendError}</p>
|
|
171
|
+
{/if}
|
|
172
|
+
{#if attachmentsEnabled}
|
|
173
|
+
<input
|
|
174
|
+
type="file"
|
|
175
|
+
multiple
|
|
176
|
+
use:attachmentsHandle.fileInputAction
|
|
177
|
+
onchange={(e) => void attachmentsHandle.handleFileUpload(e)}
|
|
178
|
+
accept={attachmentsConfig?.accept ?? "*/*"}
|
|
179
|
+
style="display: none"
|
|
180
|
+
data-testid="copilot-file-input"
|
|
181
|
+
/>
|
|
182
|
+
{/if}
|
|
183
|
+
<CopilotChatView
|
|
184
|
+
messages={messages}
|
|
185
|
+
{isRunning}
|
|
186
|
+
{autoScroll}
|
|
187
|
+
{sentMessageId}
|
|
188
|
+
{welcomeScreen}
|
|
189
|
+
suggestions={suggestions}
|
|
190
|
+
attachments={selectedAttachments}
|
|
191
|
+
{dragOver}
|
|
192
|
+
inputValue={controlledInputValue}
|
|
193
|
+
{inputMode}
|
|
194
|
+
inputToolsMenu={inputToolsMenu}
|
|
195
|
+
onSubmitMessage={handleSubmitMessage}
|
|
196
|
+
onStop={handleStop}
|
|
197
|
+
onInputChange={handleInputChange}
|
|
198
|
+
onSelectSuggestion={handleSelectSuggestion}
|
|
199
|
+
onRemoveAttachment={attachmentsEnabled ? attachmentsHandle.removeAttachment : undefined}
|
|
200
|
+
onAddFile={attachmentsEnabled ? handleAddFile : undefined}
|
|
201
|
+
onDragOver={attachmentsEnabled ? attachmentsHandle.handleDragOver : undefined}
|
|
202
|
+
onDragLeave={attachmentsEnabled ? attachmentsHandle.handleDragLeave : undefined}
|
|
203
|
+
onDrop={attachmentsEnabled ? attachmentsHandle.handleDrop : undefined}
|
|
204
|
+
/>
|
|
205
|
+
</div>
|
|
206
|
+
|
|
207
|
+
<style>
|
|
208
|
+
.copilotkit-chat {
|
|
209
|
+
display: flex;
|
|
210
|
+
flex-direction: column;
|
|
211
|
+
height: 100%;
|
|
212
|
+
overflow: hidden;
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
.copilotkit-chat-error {
|
|
216
|
+
margin: 0;
|
|
217
|
+
padding: 8px 12px;
|
|
218
|
+
color: #b91c1c;
|
|
219
|
+
background: #fef2f2;
|
|
220
|
+
font-size: 0.875rem;
|
|
221
|
+
}
|
|
222
|
+
</style>
|
|
@@ -0,0 +1,295 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { AssistantMessage, Message } from "@ag-ui/core";
|
|
3
|
+
import type { Snippet } from "svelte";
|
|
4
|
+
import { IconCheck, IconCopy, IconThumbsUp, IconThumbsDown, IconVolume2, IconRefreshCw } from "../icons";
|
|
5
|
+
import { getChatConfig } from "./chat-config-context.svelte";
|
|
6
|
+
import StreamMarkdown from "./StreamMarkdown.svelte";
|
|
7
|
+
import CopilotChatToolCallsView from "./CopilotChatToolCallsView.svelte";
|
|
8
|
+
|
|
9
|
+
interface MessageRendererProps {
|
|
10
|
+
message: AssistantMessage;
|
|
11
|
+
content: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
interface ToolbarProps {
|
|
15
|
+
message: AssistantMessage;
|
|
16
|
+
shouldShowToolbar: boolean;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
interface CopyButtonProps {
|
|
20
|
+
onCopy: () => Promise<void>;
|
|
21
|
+
copied: boolean;
|
|
22
|
+
label: string;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
interface ThumbsUpButtonProps {
|
|
26
|
+
onThumbsUp: () => void;
|
|
27
|
+
label: string;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
interface ThumbsDownButtonProps {
|
|
31
|
+
onThumbsDown: () => void;
|
|
32
|
+
label: string;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
interface ReadAloudButtonProps {
|
|
36
|
+
onReadAloud: () => void;
|
|
37
|
+
label: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
interface RegenerateButtonProps {
|
|
41
|
+
onRegenerate: () => void;
|
|
42
|
+
label: string;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
interface ToolCallsViewProps {
|
|
46
|
+
message: AssistantMessage;
|
|
47
|
+
messages: Message[];
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
interface LayoutProps {
|
|
51
|
+
message: AssistantMessage;
|
|
52
|
+
content: string;
|
|
53
|
+
isRunning: boolean;
|
|
54
|
+
toolbarVisible: boolean;
|
|
55
|
+
shouldShowToolbar: boolean;
|
|
56
|
+
messageRenderer: unknown;
|
|
57
|
+
toolbar: unknown;
|
|
58
|
+
copyButton: unknown;
|
|
59
|
+
thumbsUpButton: unknown;
|
|
60
|
+
thumbsDownButton: unknown;
|
|
61
|
+
readAloudButton: unknown;
|
|
62
|
+
regenerateButton: unknown;
|
|
63
|
+
toolCallsView: unknown;
|
|
64
|
+
onCopy: () => Promise<void>;
|
|
65
|
+
onThumbsUp: () => void;
|
|
66
|
+
onThumbsDown: () => void;
|
|
67
|
+
onReadAloud: () => void;
|
|
68
|
+
onRegenerate: () => void;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
const defaultLabels = {
|
|
72
|
+
assistantMessageToolbarCopyMessageLabel: "Copy",
|
|
73
|
+
assistantMessageToolbarThumbsUpLabel: "Good response",
|
|
74
|
+
assistantMessageToolbarThumbsDownLabel: "Bad response",
|
|
75
|
+
assistantMessageToolbarReadAloudLabel: "Read aloud",
|
|
76
|
+
assistantMessageToolbarRegenerateLabel: "Regenerate",
|
|
77
|
+
};
|
|
78
|
+
|
|
79
|
+
let {
|
|
80
|
+
message,
|
|
81
|
+
messages = [] as Message[],
|
|
82
|
+
isRunning = false,
|
|
83
|
+
toolbarVisible = true,
|
|
84
|
+
messageRenderer,
|
|
85
|
+
toolbar: toolbarSnippet,
|
|
86
|
+
copyButton,
|
|
87
|
+
thumbsUpButton,
|
|
88
|
+
thumbsDownButton,
|
|
89
|
+
readAloudButton,
|
|
90
|
+
regenerateButton,
|
|
91
|
+
toolCallsView,
|
|
92
|
+
toolbarItems,
|
|
93
|
+
onThumbsUp,
|
|
94
|
+
onThumbsDown,
|
|
95
|
+
onReadAloud,
|
|
96
|
+
onRegenerate,
|
|
97
|
+
}: {
|
|
98
|
+
message: AssistantMessage;
|
|
99
|
+
messages?: Message[];
|
|
100
|
+
isRunning?: boolean;
|
|
101
|
+
toolbarVisible?: boolean;
|
|
102
|
+
layout?: Snippet<[LayoutProps]>;
|
|
103
|
+
messageRenderer?: Snippet<[MessageRendererProps]>;
|
|
104
|
+
toolbar?: Snippet<[ToolbarProps]>;
|
|
105
|
+
copyButton?: Snippet<[CopyButtonProps]>;
|
|
106
|
+
thumbsUpButton?: Snippet<[ThumbsUpButtonProps]>;
|
|
107
|
+
thumbsDownButton?: Snippet<[ThumbsDownButtonProps]>;
|
|
108
|
+
readAloudButton?: Snippet<[ReadAloudButtonProps]>;
|
|
109
|
+
regenerateButton?: Snippet<[RegenerateButtonProps]>;
|
|
110
|
+
toolCallsView?: Snippet<[ToolCallsViewProps]>;
|
|
111
|
+
toolbarItems?: Snippet<[]>;
|
|
112
|
+
children?: Snippet<[]>;
|
|
113
|
+
onThumbsUp?: (message: AssistantMessage) => void;
|
|
114
|
+
onThumbsDown?: (message: AssistantMessage) => void;
|
|
115
|
+
onReadAloud?: (message: AssistantMessage) => void;
|
|
116
|
+
onRegenerate?: (message: AssistantMessage) => void;
|
|
117
|
+
} = $props();
|
|
118
|
+
|
|
119
|
+
let copied = $state(false);
|
|
120
|
+
let copiedResetTimeout: ReturnType<typeof setTimeout> | null = null;
|
|
121
|
+
|
|
122
|
+
const config = getChatConfig();
|
|
123
|
+
const labels = { ...defaultLabels, ...config?.labels };
|
|
124
|
+
|
|
125
|
+
function normalizeContent(content: unknown): string {
|
|
126
|
+
if (!content) return "";
|
|
127
|
+
if (typeof content === "string") return content;
|
|
128
|
+
if (Array.isArray(content)) {
|
|
129
|
+
return content
|
|
130
|
+
.map((part) => {
|
|
131
|
+
if (part && typeof part === "object" && "type" in part && part.type === "text" && typeof part.text === "string") {
|
|
132
|
+
return part.text;
|
|
133
|
+
}
|
|
134
|
+
return "";
|
|
135
|
+
})
|
|
136
|
+
.filter((text) => text.length > 0)
|
|
137
|
+
.join("\n");
|
|
138
|
+
}
|
|
139
|
+
return "";
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
const normalizedContent = $derived(normalizeContent(message.content));
|
|
143
|
+
const hasContent = $derived(normalizedContent.trim().length > 0);
|
|
144
|
+
const isLatestAssistantMessage = $derived(
|
|
145
|
+
messages.filter((m) => m.role === "assistant").at(-1)?.id === message.id
|
|
146
|
+
);
|
|
147
|
+
const shouldShowToolbar = $derived(toolbarVisible && hasContent && !(isRunning && isLatestAssistantMessage));
|
|
148
|
+
|
|
149
|
+
function resetCopiedStateWithDelay() {
|
|
150
|
+
if (copiedResetTimeout) clearTimeout(copiedResetTimeout);
|
|
151
|
+
copied = true;
|
|
152
|
+
copiedResetTimeout = setTimeout(() => {
|
|
153
|
+
copied = false;
|
|
154
|
+
copiedResetTimeout = null;
|
|
155
|
+
}, 2000);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
async function handleCopyMessage() {
|
|
159
|
+
if (!normalizedContent) return;
|
|
160
|
+
if (typeof navigator === "undefined" || typeof navigator.clipboard?.writeText !== "function") return;
|
|
161
|
+
try {
|
|
162
|
+
await navigator.clipboard.writeText(normalizedContent);
|
|
163
|
+
resetCopiedStateWithDelay();
|
|
164
|
+
} catch (error) {
|
|
165
|
+
console.error("Failed to copy to clipboard:", error);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
function handleThumbsUp() { onThumbsUp?.(message); }
|
|
170
|
+
function handleThumbsDown() { onThumbsDown?.(message); }
|
|
171
|
+
function handleReadAloud() { onReadAloud?.(message); }
|
|
172
|
+
function handleRegenerate() { onRegenerate?.(message); }
|
|
173
|
+
|
|
174
|
+
const toolbarBtnClass = "cpk:inline-flex cpk:h-8 cpk:w-8 cpk:items-center cpk:justify-center cpk:rounded-md cpk:p-0 cpk:cursor-pointer cpk:text-[rgb(93,93,93)] cpk:transition-colors cpk:hover:bg-[#E8E8E8] cpk:hover:text-[rgb(93,93,93)] cpk:dark:text-[rgb(243,243,243)] cpk:dark:hover:bg-[#303030] cpk:dark:hover:text-[rgb(243,243,243)] cpk:disabled:pointer-events-none cpk:disabled:opacity-50";
|
|
175
|
+
</script>
|
|
176
|
+
|
|
177
|
+
<div
|
|
178
|
+
data-copilotkit
|
|
179
|
+
data-testid="copilot-assistant-message"
|
|
180
|
+
class="cpk:w-full cpk:group cpk:pt-10"
|
|
181
|
+
data-message-id={message.id}
|
|
182
|
+
>
|
|
183
|
+
<div class="cpk:prose cpk:max-w-full cpk:break-words cpk:dark:prose-invert">
|
|
184
|
+
{#if messageRenderer}
|
|
185
|
+
{@render messageRenderer({ message, content: normalizedContent })}
|
|
186
|
+
{:else}
|
|
187
|
+
{#if hasContent}
|
|
188
|
+
<StreamMarkdown content={normalizedContent} />
|
|
189
|
+
{/if}
|
|
190
|
+
{/if}
|
|
191
|
+
</div>
|
|
192
|
+
|
|
193
|
+
<div class="cpk:flex cpk:w-full cpk:flex-col cpk:items-stretch">
|
|
194
|
+
{#if toolCallsView}
|
|
195
|
+
{@render toolCallsView({ message, messages })}
|
|
196
|
+
{:else}
|
|
197
|
+
<CopilotChatToolCallsView {message} {messages} />
|
|
198
|
+
{/if}
|
|
199
|
+
</div>
|
|
200
|
+
|
|
201
|
+
{#if shouldShowToolbar}
|
|
202
|
+
{#if toolbarSnippet}
|
|
203
|
+
{@render toolbarSnippet({ message, shouldShowToolbar })}
|
|
204
|
+
{:else}
|
|
205
|
+
<div class="cpk:w-full cpk:bg-transparent cpk:flex cpk:items-center cpk:-ml-[5px] cpk:-mt-[0px]">
|
|
206
|
+
<div class="cpk:flex cpk:items-center cpk:gap-1">
|
|
207
|
+
{#if copyButton}
|
|
208
|
+
{@render copyButton({ onCopy: handleCopyMessage, copied, label: labels.assistantMessageToolbarCopyMessageLabel as string })}
|
|
209
|
+
{:else}
|
|
210
|
+
<button
|
|
211
|
+
data-testid="copilot-copy-button"
|
|
212
|
+
type="button"
|
|
213
|
+
class={toolbarBtnClass}
|
|
214
|
+
aria-label={labels.assistantMessageToolbarCopyMessageLabel as string}
|
|
215
|
+
title={labels.assistantMessageToolbarCopyMessageLabel as string}
|
|
216
|
+
onclick={handleCopyMessage}
|
|
217
|
+
>
|
|
218
|
+
{#if copied}
|
|
219
|
+
<IconCheck class="cpk:size-[18px]" />
|
|
220
|
+
{:else}
|
|
221
|
+
<IconCopy class="cpk:size-[18px]" />
|
|
222
|
+
{/if}
|
|
223
|
+
</button>
|
|
224
|
+
{/if}
|
|
225
|
+
|
|
226
|
+
{#if onThumbsUp}
|
|
227
|
+
{#if thumbsUpButton}
|
|
228
|
+
{@render thumbsUpButton({ onThumbsUp: handleThumbsUp, label: labels.assistantMessageToolbarThumbsUpLabel as string })}
|
|
229
|
+
{:else}
|
|
230
|
+
<button
|
|
231
|
+
type="button"
|
|
232
|
+
class={toolbarBtnClass}
|
|
233
|
+
aria-label={labels.assistantMessageToolbarThumbsUpLabel as string}
|
|
234
|
+
title={labels.assistantMessageToolbarThumbsUpLabel as string}
|
|
235
|
+
onclick={handleThumbsUp}
|
|
236
|
+
>
|
|
237
|
+
<IconThumbsUp class="cpk:size-[18px]" />
|
|
238
|
+
</button>
|
|
239
|
+
{/if}
|
|
240
|
+
{/if}
|
|
241
|
+
|
|
242
|
+
{#if onThumbsDown}
|
|
243
|
+
{#if thumbsDownButton}
|
|
244
|
+
{@render thumbsDownButton({ onThumbsDown: handleThumbsDown, label: labels.assistantMessageToolbarThumbsDownLabel as string })}
|
|
245
|
+
{:else}
|
|
246
|
+
<button
|
|
247
|
+
type="button"
|
|
248
|
+
class={toolbarBtnClass}
|
|
249
|
+
aria-label={labels.assistantMessageToolbarThumbsDownLabel as string}
|
|
250
|
+
title={labels.assistantMessageToolbarThumbsDownLabel as string}
|
|
251
|
+
onclick={handleThumbsDown}
|
|
252
|
+
>
|
|
253
|
+
<IconThumbsDown class="cpk:size-[18px]" />
|
|
254
|
+
</button>
|
|
255
|
+
{/if}
|
|
256
|
+
{/if}
|
|
257
|
+
|
|
258
|
+
{#if onReadAloud}
|
|
259
|
+
{#if readAloudButton}
|
|
260
|
+
{@render readAloudButton({ onReadAloud: handleReadAloud, label: labels.assistantMessageToolbarReadAloudLabel as string })}
|
|
261
|
+
{:else}
|
|
262
|
+
<button
|
|
263
|
+
type="button"
|
|
264
|
+
class={toolbarBtnClass}
|
|
265
|
+
aria-label={labels.assistantMessageToolbarReadAloudLabel as string}
|
|
266
|
+
title={labels.assistantMessageToolbarReadAloudLabel as string}
|
|
267
|
+
onclick={handleReadAloud}
|
|
268
|
+
>
|
|
269
|
+
<IconVolume2 class="cpk:size-[20px]" />
|
|
270
|
+
</button>
|
|
271
|
+
{/if}
|
|
272
|
+
{/if}
|
|
273
|
+
|
|
274
|
+
{#if onRegenerate}
|
|
275
|
+
{#if regenerateButton}
|
|
276
|
+
{@render regenerateButton({ onRegenerate: handleRegenerate, label: labels.assistantMessageToolbarRegenerateLabel as string })}
|
|
277
|
+
{:else}
|
|
278
|
+
<button
|
|
279
|
+
type="button"
|
|
280
|
+
class={toolbarBtnClass}
|
|
281
|
+
aria-label={labels.assistantMessageToolbarRegenerateLabel as string}
|
|
282
|
+
title={labels.assistantMessageToolbarRegenerateLabel as string}
|
|
283
|
+
onclick={handleRegenerate}
|
|
284
|
+
>
|
|
285
|
+
<IconRefreshCw class="cpk:size-[18px]" />
|
|
286
|
+
</button>
|
|
287
|
+
{/if}
|
|
288
|
+
{/if}
|
|
289
|
+
|
|
290
|
+
{#if toolbarItems}{@render toolbarItems()}{/if}
|
|
291
|
+
</div>
|
|
292
|
+
</div>
|
|
293
|
+
{/if}
|
|
294
|
+
{/if}
|
|
295
|
+
</div>
|