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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (181) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +324 -3
  3. package/dist/app.d.ts +7 -0
  4. package/dist/components/chat/CopilotChat.svelte +247 -0
  5. package/dist/components/chat/CopilotChat.svelte.d.ts +5 -0
  6. package/dist/components/chat/CopilotChat.svelte.d.ts.map +1 -0
  7. package/dist/components/chat/CopilotChatAssistantMessage.svelte +295 -0
  8. package/dist/components/chat/CopilotChatAssistantMessage.svelte.d.ts +80 -0
  9. package/dist/components/chat/CopilotChatAssistantMessage.svelte.d.ts.map +1 -0
  10. package/dist/components/chat/CopilotChatAttachmentQueue.svelte +241 -0
  11. package/dist/components/chat/CopilotChatAttachmentQueue.svelte.d.ts +9 -0
  12. package/dist/components/chat/CopilotChatAttachmentQueue.svelte.d.ts.map +1 -0
  13. package/dist/components/chat/CopilotChatInput.svelte +192 -0
  14. package/dist/components/chat/CopilotChatInput.svelte.d.ts +20 -0
  15. package/dist/components/chat/CopilotChatInput.svelte.d.ts.map +1 -0
  16. package/dist/components/chat/CopilotChatMessage.svelte +37 -0
  17. package/dist/components/chat/CopilotChatMessage.svelte.d.ts +10 -0
  18. package/dist/components/chat/CopilotChatMessage.svelte.d.ts.map +1 -0
  19. package/dist/components/chat/CopilotChatMessageView.svelte +76 -0
  20. package/dist/components/chat/CopilotChatMessageView.svelte.d.ts +14 -0
  21. package/dist/components/chat/CopilotChatMessageView.svelte.d.ts.map +1 -0
  22. package/dist/components/chat/CopilotChatReasoningMessage.svelte +210 -0
  23. package/dist/components/chat/CopilotChatReasoningMessage.svelte.d.ts +40 -0
  24. package/dist/components/chat/CopilotChatReasoningMessage.svelte.d.ts.map +1 -0
  25. package/dist/components/chat/CopilotChatSuggestionPill.svelte +66 -0
  26. package/dist/components/chat/CopilotChatSuggestionPill.svelte.d.ts +10 -0
  27. package/dist/components/chat/CopilotChatSuggestionPill.svelte.d.ts.map +1 -0
  28. package/dist/components/chat/CopilotChatSuggestionView.svelte +36 -0
  29. package/dist/components/chat/CopilotChatSuggestionView.svelte.d.ts +10 -0
  30. package/dist/components/chat/CopilotChatSuggestionView.svelte.d.ts.map +1 -0
  31. package/dist/components/chat/CopilotChatToggleButton.svelte +47 -0
  32. package/dist/components/chat/CopilotChatToggleButton.svelte.d.ts +8 -0
  33. package/dist/components/chat/CopilotChatToggleButton.svelte.d.ts.map +1 -0
  34. package/dist/components/chat/CopilotChatToolCallsView.svelte +187 -0
  35. package/dist/components/chat/CopilotChatToolCallsView.svelte.d.ts +12 -0
  36. package/dist/components/chat/CopilotChatToolCallsView.svelte.d.ts.map +1 -0
  37. package/dist/components/chat/CopilotChatUserMessage.svelte +312 -0
  38. package/dist/components/chat/CopilotChatUserMessage.svelte.d.ts +62 -0
  39. package/dist/components/chat/CopilotChatUserMessage.svelte.d.ts.map +1 -0
  40. package/dist/components/chat/CopilotChatView.svelte +177 -0
  41. package/dist/components/chat/CopilotChatView.svelte.d.ts +32 -0
  42. package/dist/components/chat/CopilotChatView.svelte.d.ts.map +1 -0
  43. package/dist/components/chat/CopilotPopup.svelte +187 -0
  44. package/dist/components/chat/CopilotPopup.svelte.d.ts +5 -0
  45. package/dist/components/chat/CopilotPopup.svelte.d.ts.map +1 -0
  46. package/dist/components/chat/CopilotSidebar.svelte +138 -0
  47. package/dist/components/chat/CopilotSidebar.svelte.d.ts +5 -0
  48. package/dist/components/chat/CopilotSidebar.svelte.d.ts.map +1 -0
  49. package/dist/components/chat/StreamMarkdown.svelte +30 -0
  50. package/dist/components/chat/StreamMarkdown.svelte.d.ts +7 -0
  51. package/dist/components/chat/StreamMarkdown.svelte.d.ts.map +1 -0
  52. package/dist/components/chat/chat-config-context.svelte.d.ts +10 -0
  53. package/dist/components/chat/chat-config-context.svelte.d.ts.map +1 -0
  54. package/dist/components/chat/chat-config-context.svelte.js +20 -0
  55. package/dist/components/chat/dispatch-after-active-run.d.ts +3 -0
  56. package/dist/components/chat/dispatch-after-active-run.d.ts.map +1 -0
  57. package/dist/components/chat/dispatch-after-active-run.js +31 -0
  58. package/dist/components/chat/markdown/CodeBlock.svelte +78 -0
  59. package/dist/components/chat/markdown/CodeBlock.svelte.d.ts +8 -0
  60. package/dist/components/chat/markdown/CodeBlock.svelte.d.ts.map +1 -0
  61. package/dist/components/chat/markdown/CodeSpan.svelte +29 -0
  62. package/dist/components/chat/markdown/CodeSpan.svelte.d.ts +9 -0
  63. package/dist/components/chat/markdown/CodeSpan.svelte.d.ts.map +1 -0
  64. package/dist/components/chat/markdown/Table.svelte +15 -0
  65. package/dist/components/chat/markdown/Table.svelte.d.ts +8 -0
  66. package/dist/components/chat/markdown/Table.svelte.d.ts.map +1 -0
  67. package/dist/components/chat/markdown/TableBody.svelte +11 -0
  68. package/dist/components/chat/markdown/TableBody.svelte.d.ts +8 -0
  69. package/dist/components/chat/markdown/TableBody.svelte.d.ts.map +1 -0
  70. package/dist/components/chat/markdown/TableCell.svelte +29 -0
  71. package/dist/components/chat/markdown/TableCell.svelte.d.ts +10 -0
  72. package/dist/components/chat/markdown/TableCell.svelte.d.ts.map +1 -0
  73. package/dist/components/chat/markdown/TableHead.svelte +11 -0
  74. package/dist/components/chat/markdown/TableHead.svelte.d.ts +8 -0
  75. package/dist/components/chat/markdown/TableHead.svelte.d.ts.map +1 -0
  76. package/dist/components/chat/markdown/TableRow.svelte +11 -0
  77. package/dist/components/chat/markdown/TableRow.svelte.d.ts +8 -0
  78. package/dist/components/chat/markdown/TableRow.svelte.d.ts.map +1 -0
  79. package/dist/components/chat/markdown/highlight.d.ts +8 -0
  80. package/dist/components/chat/markdown/highlight.d.ts.map +1 -0
  81. package/dist/components/chat/markdown/highlight.js +91 -0
  82. package/dist/components/chat/media-parts.d.ts +11 -0
  83. package/dist/components/chat/media-parts.d.ts.map +1 -0
  84. package/dist/components/chat/media-parts.js +17 -0
  85. package/dist/components/chat/types.d.ts +139 -0
  86. package/dist/components/chat/types.d.ts.map +1 -0
  87. package/dist/components/chat/types.js +7 -0
  88. package/dist/components/icons/index.d.ts +2 -0
  89. package/dist/components/icons/index.d.ts.map +1 -0
  90. package/dist/components/icons/index.js +1 -0
  91. package/dist/components/index.d.ts +18 -0
  92. package/dist/components/index.d.ts.map +1 -0
  93. package/dist/components/index.js +17 -0
  94. package/dist/hooks/connect-agent-context.svelte.d.ts +9 -0
  95. package/dist/hooks/connect-agent-context.svelte.d.ts.map +1 -0
  96. package/dist/hooks/connect-agent-context.svelte.js +25 -0
  97. package/dist/hooks/create-agent.svelte.d.ts +23 -0
  98. package/dist/hooks/create-agent.svelte.d.ts.map +1 -0
  99. package/dist/hooks/create-agent.svelte.js +341 -0
  100. package/dist/hooks/create-attachments.svelte.d.ts +33 -0
  101. package/dist/hooks/create-attachments.svelte.d.ts.map +1 -0
  102. package/dist/hooks/create-attachments.svelte.js +232 -0
  103. package/dist/hooks/create-capabilities.svelte.d.ts +6 -0
  104. package/dist/hooks/create-capabilities.svelte.d.ts.map +1 -0
  105. package/dist/hooks/create-capabilities.svelte.js +17 -0
  106. package/dist/hooks/create-interrupt.svelte.d.ts +24 -0
  107. package/dist/hooks/create-interrupt.svelte.d.ts.map +1 -0
  108. package/dist/hooks/create-interrupt.svelte.js +333 -0
  109. package/dist/hooks/create-suggestions.svelte.d.ts +12 -0
  110. package/dist/hooks/create-suggestions.svelte.d.ts.map +1 -0
  111. package/dist/hooks/create-suggestions.svelte.js +61 -0
  112. package/dist/hooks/create-threads.svelte.d.ts +33 -0
  113. package/dist/hooks/create-threads.svelte.d.ts.map +1 -0
  114. package/dist/hooks/create-threads.svelte.js +230 -0
  115. package/dist/hooks/index.d.ts +20 -0
  116. package/dist/hooks/index.d.ts.map +1 -0
  117. package/dist/hooks/index.js +11 -0
  118. package/dist/hooks/load-katex-styles.svelte.d.ts +2 -0
  119. package/dist/hooks/load-katex-styles.svelte.d.ts.map +1 -0
  120. package/dist/hooks/load-katex-styles.svelte.js +11 -0
  121. package/dist/hooks/register-frontend-tool.svelte.d.ts +3 -0
  122. package/dist/hooks/register-frontend-tool.svelte.d.ts.map +1 -0
  123. package/dist/hooks/register-frontend-tool.svelte.js +32 -0
  124. package/dist/hooks/register-human-in-the-loop.svelte.d.ts +3 -0
  125. package/dist/hooks/register-human-in-the-loop.svelte.d.ts.map +1 -0
  126. package/dist/hooks/register-human-in-the-loop.svelte.js +84 -0
  127. package/dist/hooks/register-render-tool-call.svelte.d.ts +30 -0
  128. package/dist/hooks/register-render-tool-call.svelte.d.ts.map +1 -0
  129. package/dist/hooks/register-render-tool-call.svelte.js +26 -0
  130. package/dist/index.d.ts +6 -0
  131. package/dist/index.d.ts.map +1 -0
  132. package/dist/index.js +6 -0
  133. package/dist/lib/svelte-core.d.ts +54 -0
  134. package/dist/lib/svelte-core.d.ts.map +1 -0
  135. package/dist/lib/svelte-core.js +145 -0
  136. package/dist/providers/CopilotKitProvider.svelte +291 -0
  137. package/dist/providers/CopilotKitProvider.svelte.d.ts +9 -0
  138. package/dist/providers/CopilotKitProvider.svelte.d.ts.map +1 -0
  139. package/dist/providers/CopilotKitProvider.types.d.ts +47 -0
  140. package/dist/providers/CopilotKitProvider.types.d.ts.map +1 -0
  141. package/dist/providers/CopilotKitProvider.types.js +1 -0
  142. package/dist/providers/context.d.ts +19 -0
  143. package/dist/providers/context.d.ts.map +1 -0
  144. package/dist/providers/context.js +2 -0
  145. package/dist/providers/index.d.ts +6 -0
  146. package/dist/providers/index.d.ts.map +1 -0
  147. package/dist/providers/index.js +2 -0
  148. package/dist/providers/useCopilotKit.d.ts +3 -0
  149. package/dist/providers/useCopilotKit.d.ts.map +1 -0
  150. package/dist/providers/useCopilotKit.js +9 -0
  151. package/dist/styles/globals.css +691 -0
  152. package/dist/styles.css +2 -0
  153. package/dist/styles.d.ts +1 -0
  154. package/dist/types/a2ui.d.ts +5 -0
  155. package/dist/types/a2ui.d.ts.map +1 -0
  156. package/dist/types/a2ui.js +1 -0
  157. package/dist/types/activity-message-renderer.d.ts +17 -0
  158. package/dist/types/activity-message-renderer.d.ts.map +1 -0
  159. package/dist/types/activity-message-renderer.js +1 -0
  160. package/dist/types/custom-message-renderer.d.ts +18 -0
  161. package/dist/types/custom-message-renderer.d.ts.map +1 -0
  162. package/dist/types/custom-message-renderer.js +1 -0
  163. package/dist/types/frontend-tool.d.ts +6 -0
  164. package/dist/types/frontend-tool.d.ts.map +1 -0
  165. package/dist/types/frontend-tool.js +1 -0
  166. package/dist/types/human-in-the-loop.d.ts +31 -0
  167. package/dist/types/human-in-the-loop.d.ts.map +1 -0
  168. package/dist/types/human-in-the-loop.js +1 -0
  169. package/dist/types/index.d.ts +9 -0
  170. package/dist/types/index.d.ts.map +1 -0
  171. package/dist/types/index.js +8 -0
  172. package/dist/types/interrupt.d.ts +23 -0
  173. package/dist/types/interrupt.d.ts.map +1 -0
  174. package/dist/types/interrupt.js +1 -0
  175. package/dist/types/sandbox-function.d.ts +8 -0
  176. package/dist/types/sandbox-function.d.ts.map +1 -0
  177. package/dist/types/sandbox-function.js +1 -0
  178. package/dist/types/tool-call-renderer.d.ts +29 -0
  179. package/dist/types/tool-call-renderer.d.ts.map +1 -0
  180. package/dist/types/tool-call-renderer.js +1 -0
  181. package/package.json +103 -8
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License
2
+
3
+ Copyright (c) Atai Barkai
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
package/README.md CHANGED
@@ -1,5 +1,326 @@
1
- # @copilotkit/svelte
1
+ Community-maintained Svelte 5 components and runes for CopilotKit. This package
2
+ is tested with `@copilotkit/core` **1.87.1**, pinned exactly because the Svelte
3
+ bindings use internal thread APIs. Its releases are independent of the core
4
+ packages and it is not part of the CopilotKit monorepo build.
2
5
 
3
- This version (0.0.1) is a placeholder. It contains no code.
6
+ The package provides a context provider, rune-based state factories,
7
+ lifecycle-managed tool registrations, and chat components backed by
8
+ [Copilot Runtime](https://docs.copilotkit.ai/backend/copilot-runtime).
4
9
 
5
- The first real release of the CopilotKit Svelte 5 package will be 0.1.0. The source lives in [`community/svelte`](https://github.com/CopilotKit/CopilotKit/tree/main/community/svelte).
10
+ The package covers chat and agent interaction. Check the compatibility table
11
+ below before replacing a frontend that depends on advanced renderers or
12
+ developer tooling.
13
+
14
+ ## Requirements
15
+
16
+ - Svelte 5
17
+ - Node.js 20 or later
18
+ - A Copilot Runtime endpoint
19
+
20
+ ## Install
21
+
22
+ ```bash
23
+ pnpm add @copilotkit/svelte
24
+ ```
25
+
26
+ Add `zod` when you register typed frontend tools.
27
+
28
+ ## Add the provider and styles
29
+
30
+ Place the provider above every component that uses a CopilotKit factory or
31
+ registration. Import the public stylesheet once at the application root.
32
+
33
+ ```svelte
34
+ <script lang="ts">
35
+ import "@copilotkit/svelte/styles.css";
36
+ import { CopilotKitProvider } from "@copilotkit/svelte";
37
+
38
+ let { children } = $props();
39
+ </script>
40
+
41
+ <CopilotKitProvider runtimeUrl="/api/copilotkit">
42
+ {@render children()}
43
+ </CopilotKitProvider>
44
+ ```
45
+
46
+ ### Global CSS resets
47
+
48
+ The stylesheet ships in a low-priority layer and only targets elements inside
49
+ `[data-copilotkit]`, so it does not reset your application's own markup. Avoid
50
+ adding `* { margin: 0; padding: 0 }` or similar resets at the application root:
51
+ they also strip spacing from the chat's internal elements, which makes message
52
+ spacing and markdown blocks collapse. Reset only what you need, for example:
53
+
54
+ ```css
55
+ *,
56
+ *::before,
57
+ *::after {
58
+ box-sizing: border-box;
59
+ }
60
+ ```
61
+
62
+ ## Render chat
63
+
64
+ ```svelte
65
+ <script lang="ts">
66
+ import { CopilotChat } from "@copilotkit/svelte";
67
+ </script>
68
+
69
+ <div class="chat-shell">
70
+ <CopilotChat agentId="default" />
71
+ </div>
72
+
73
+ <style>
74
+ .chat-shell {
75
+ height: 700px;
76
+ }
77
+ </style>
78
+ ```
79
+
80
+ The runtime must expose the selected agent ID. If `agentId` is omitted, the
81
+ chat uses the runtime's `default` agent.
82
+
83
+ ## Compatibility
84
+
85
+ | Area | Initial Svelte support | Notes |
86
+ | -------------------------------------- | -------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
87
+ | Runtime connection and AG-UI streaming | Supported | Includes message updates, run state, and agent state invalidation. |
88
+ | Inline chat | Supported | Use `CopilotChat`; lower-level chat and message components are also exported. |
89
+ | Popup and sidebar chat | Supported | `CopilotPopup` and `CopilotSidebar` keep the chat mounted while closed, so generated thread state persists across open/close. Pass an explicit `threadId` to share state across surfaces. |
90
+ | Frontend tools | Supported | Register with `registerFrontendTool`; registrations clean up with their component. |
91
+ | Agent context | Supported | Connect reactive application data with `connectAgentContext`. |
92
+ | Suggestions | Observation only | `createSuggestions` observes suggestions already present in core; Svelte does not yet register suggestion-generation configuration. |
93
+ | Threads | Supported headless API | `createThreads` lists pages, fetches more, and renames/archives/unarchives/deletes threads over the core runtime transport, with reactive `agentId`/`enabled`/`limit`/`includeArchived` inputs and store cleanup on unmount. A prebuilt threads drawer is not included. |
94
+ | Interrupts | Headless | Observe and resolve AG-UI interrupts with `createInterrupt`. |
95
+ | Attachments | Supported | `createAttachments` validates/uploads files (drag-drop, picker, scoped paste) and `CopilotChat` wires it into the composer with `attachments={{ enabled: true }}`. See the attachments example below. |
96
+ | Tool-call rendering | Supported in prebuilt chat | Renderers from `registerRenderToolCall` (including the `"*"` wildcard fallback) mount inside `CopilotChat`; a `toolCall` slot override still wins when provided. Unmatched calls fall back to generic status. |
97
+ | Human approval | Supported with custom UI | `registerHumanInTheLoop` mounts its renderer in the prebuilt chat and matches concurrent calls by tool-call ID. The provider-level `humanInTheLoop` prop is unsupported and throws; use the registration instead. |
98
+ | A2UI and Open Generative UI | Not yet supported | Built-in catalogs, renderers, and sandbox helpers are planned. |
99
+ | CoAgent/LangGraph state renderers | Not yet supported | Use the lower-level agent binding for state today. |
100
+ | Memories and learning APIs | Not yet supported | No Svelte-specific bindings are included in this release. |
101
+ | Modal, threads drawer, audio input | Not yet supported | Build these surfaces from the exported state factories where possible. |
102
+ | Inspector and developer console | Not yet supported | Use Runtime and browser diagnostics while developing. |
103
+
104
+ ## Reactive agent state
105
+
106
+ Stateful APIs use `create*` names because they create rune-backed state and
107
+ subscriptions. Keep the returned object intact and read its live getters in
108
+ markup; plain destructuring snapshots a getter and loses reactivity.
109
+
110
+ ```svelte
111
+ <script lang="ts">
112
+ import { createAgent, useCopilotKit } from "@copilotkit/svelte";
113
+ import { isRunCompletionAware } from "@copilotkit/core";
114
+
115
+ const agentHandle = createAgent({ agentId: "default" });
116
+ const { copilotkit } = useCopilotKit();
117
+
118
+ async function send(content: string) {
119
+ const agent = agentHandle.agent;
120
+ if (!agent) return;
121
+
122
+ const maybeAware: unknown = agent;
123
+ const activeRunCompletionPromise = isRunCompletionAware(maybeAware)
124
+ ? maybeAware.activeRunCompletionPromise
125
+ : undefined;
126
+ if (agent.isRunning && activeRunCompletionPromise) {
127
+ try {
128
+ await activeRunCompletionPromise;
129
+ } catch (error) {
130
+ console.error("The active CopilotKit run failed before this send", error);
131
+ }
132
+ }
133
+
134
+ agent.addMessage({
135
+ id: crypto.randomUUID(),
136
+ role: "user",
137
+ content,
138
+ });
139
+ await copilotkit.runAgent({ agent });
140
+ }
141
+ </script>
142
+
143
+ {#if agentHandle.isRunning}
144
+ <p>Agent is thinking…</p>
145
+ {/if}
146
+ ```
147
+
148
+ Use `$derived` only when you want a local alias or computed value:
149
+
150
+ ```svelte
151
+ <script lang="ts">
152
+ const { messages, isRunning } = $derived(agentHandle);
153
+ </script>
154
+ ```
155
+
156
+ ## Register a frontend tool
157
+
158
+ Registration APIs use `register*` names. They register during component setup
159
+ and unregister when the component is destroyed.
160
+
161
+ ```svelte
162
+ <script lang="ts">
163
+ import { registerFrontendTool } from "@copilotkit/svelte";
164
+ import { z } from "zod";
165
+
166
+ registerFrontendTool({
167
+ name: "showToast",
168
+ description: "Show a notification in the browser",
169
+ parameters: z.object({ message: z.string() }),
170
+ handler: async ({ message }) => {
171
+ alert(message);
172
+ return "Notification shown";
173
+ },
174
+ });
175
+ </script>
176
+ ```
177
+
178
+ Set `agentId` to scope a registration to one agent. Omit it to make the tool
179
+ available to every agent connected through the provider.
180
+
181
+ ## Render tool calls in chat
182
+
183
+ Registered renderers mount inside `CopilotChat`. Return a string for simple
184
+ status output, or a Svelte snippet for rich UI. A `"*"` registration acts as
185
+ the fallback when no named renderer matches.
186
+
187
+ ```svelte
188
+ <script lang="ts">
189
+ import { registerRenderToolCall } from "@copilotkit/svelte";
190
+ import { z } from "zod";
191
+
192
+ registerRenderToolCall({
193
+ name: "lookupWeather",
194
+ parameters: z.object({ city: z.string().optional() }),
195
+ render: ({ status, toolCallId, parameters, result }) =>
196
+ `weather:${toolCallId}:${status}:${String(parameters["city"] ?? "")}:${result ?? ""}`,
197
+ });
198
+
199
+ registerRenderToolCall({
200
+ name: "*",
201
+ render: ({ name, status }) => `${name}:${status}`,
202
+ });
203
+ </script>
204
+ ```
205
+
206
+ Scope with `agentId` to render only calls for one agent. For interactive
207
+ approval UI, use `registerHumanInTheLoop` instead: its `render` receives
208
+ `respond` while `status === "executing"` — render a snippet with an approve
209
+ button that calls `respond(result)` to resolve that tool call ID.
210
+
211
+ ## Attach files
212
+
213
+ Pass `attachments` to `CopilotChat` to wire `createAttachments` into the
214
+ composer (file picker, drag-drop overlay, scoped paste, upload queue).
215
+ `attachments={true}` enables defaults; pass a config object to customize.
216
+
217
+ ```svelte
218
+ <script lang="ts">
219
+ import { CopilotChat } from "@copilotkit/svelte";
220
+ </script>
221
+
222
+ <CopilotChat
223
+ agentId="default"
224
+ attachments={{ enabled: true, accept: "image/*,.pdf", maxSize: 10 * 1024 * 1024 }}
225
+ />
226
+ ```
227
+
228
+ Use `createAttachments` directly only when building a custom composer. It
229
+ exposes `fileInputAction`/`containerAction` Svelte actions for DOM refs,
230
+ plus `processFiles`, `handleFileUpload`, drag handlers, and
231
+ `consumeAttachments()`.
232
+
233
+ ## Work with threads headlessly
234
+
235
+ `createThreads` exposes the thread store without a prebuilt drawer. Inputs
236
+ are reactive getters, so updates to `agentId`, `enabled`, `limit`, or
237
+ `includeArchived` refetch automatically.
238
+
239
+ ```svelte
240
+ <script lang="ts">
241
+ import { createThreads } from "@copilotkit/svelte";
242
+
243
+ const threads = createThreads({ agentId: "default", limit: 20 });
244
+
245
+ function rename(id: string) {
246
+ void threads.renameThread(id, "Trip plan");
247
+ }
248
+ </script>
249
+
250
+ {#if threads.isLoading}
251
+ <p>Loading threads…</p>
252
+ {:else if threads.error}
253
+ <p>{threads.error.message}</p>
254
+ {:else}
255
+ <ul>
256
+ {#each threads.threads as thread (thread.id)}
257
+ <li>
258
+ {thread.name ?? thread.id}
259
+ <button onclick={() => rename(thread.id)}>Rename</button>
260
+ <button onclick={() => void threads.archiveThread(thread.id)}>Archive</button>
261
+ <button onclick={() => void threads.deleteThread(thread.id)}>Delete</button>
262
+ </li>
263
+ {/each}
264
+ </ul>
265
+ {#if threads.hasMoreThreads}
266
+ <button onclick={() => threads.fetchMoreThreads()}>Load more</button>
267
+ {/if}
268
+ {/if}
269
+ ```
270
+
271
+ ## API naming
272
+
273
+ | React concept | Svelte 5 API |
274
+ | ------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------- |
275
+ | `useAgent` | `createAgent` |
276
+ | `useFrontendTool` | `registerFrontendTool` |
277
+ | `useAgentContext` | `connectAgentContext` |
278
+ | `useRenderToolCall` (`useRenderTool` in V1) | `registerRenderToolCall` |
279
+ | `useHumanInTheLoop` | `registerHumanInTheLoop` |
280
+ | `useInterrupt` | `createInterrupt` |
281
+ | `useThreads` | `createThreads` |
282
+ | `useAttachments` | `createAttachments` |
283
+ | `useSuggestions` | `createSuggestions` (observation only; no suggestion-configuration registration — React's `useConfigureSuggestions` has no Svelte equivalent) |
284
+ | `useCapabilities` | `createCapabilities` |
285
+ | `useKatexStyles` | `loadKatexStyles` |
286
+ | Provider accessor (`useCopilotKit`) | `useCopilotKit` |
287
+
288
+ React V1 names such as `useCopilotAction`, `useCopilotReadable`, and
289
+ `useCopilotChat` are not part of the Svelte API. Prefer the Svelte `create*`,
290
+ `register*`, and `connect*` functions.
291
+
292
+ ## Remaining limitations
293
+
294
+ This release does not claim full React parity. Still missing:
295
+
296
+ - Suggestion-generation configuration (Svelte observes suggestions only).
297
+ - Prebuilt thread drawer, modal chat surface, and audio input.
298
+ - Provider-level `humanInTheLoop` prop (throws; use `registerHumanInTheLoop`).
299
+ - A2UI and Open Generative UI renderers/sandbox helpers.
300
+ - CoAgent/LangGraph state renderers.
301
+ - Memories and learning APIs.
302
+ - Inspector and developer console.
303
+
304
+ ## Try it
305
+
306
+ The [SvelteKit demo](./examples/demo) renders chat, sidebar, and popup tabs with
307
+ attachments enabled. Copy `examples/demo/.env.example` to `examples/demo/.env.local`, add one of
308
+ `OPENAI_API_KEY`, `ANTHROPIC_API_KEY`, or `GOOGLE_API_KEY`, then run:
309
+
310
+ ```bash
311
+ cd community/svelte
312
+ pnpm install
313
+ pnpm build
314
+ cd examples/demo
315
+ pnpm install
316
+ pnpm dev
317
+ ```
318
+
319
+ ## Next compatibility milestones
320
+
321
+ 1. Suggestion-generation configuration registration.
322
+ 2. Prebuilt thread drawer, modal surface, and audio input.
323
+ 3. A2UI/Open Generative UI renderers and sandbox helpers.
324
+ 4. CoAgent state renderers.
325
+ 5. Memories/learning bindings and Inspector tooling.
326
+ 6. Public API reference pages and lifecycle examples for every exported factory.
package/dist/app.d.ts ADDED
@@ -0,0 +1,7 @@
1
+ import "svelte";
2
+
3
+ declare module "*.svelte" {
4
+ import type { Component } from "svelte";
5
+ const component: Component;
6
+ export default component;
7
+ }
@@ -0,0 +1,247 @@
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 { AbstractAgent } from "@ag-ui/client";
11
+ import type { UserMessage } from "@ag-ui/core";
12
+ import { useCopilotKit } from "../../providers/useCopilotKit";
13
+ import { setChatConfig, getChatConfig, ChatConfig } from "./chat-config-context.svelte";
14
+ import CopilotChatView from "./CopilotChatView.svelte";
15
+ import { dispatchAfterActiveRunSettles } from "./dispatch-after-active-run";
16
+ import type { CopilotChatProps, CopilotChatInputMode, ToolsMenuItem } from "./types";
17
+
18
+ function normalizeAttachmentsConfig(
19
+ value: CopilotChatProps["attachments"],
20
+ ): AttachmentsConfig | undefined {
21
+ if (value === undefined || value === false) return undefined;
22
+ if (value === true) return { enabled: true };
23
+ return {
24
+ ...value,
25
+ enabled: value.enabled ?? true,
26
+ };
27
+ }
28
+
29
+ let {
30
+ agentId,
31
+ threadId: explicitThreadId,
32
+ throttleMs,
33
+ autoScroll = true,
34
+ welcomeScreen = true,
35
+ inputValue: controlledInputValue,
36
+ onInputChange,
37
+ inputMode = "input" as CopilotChatInputMode,
38
+ inputToolsMenu = [] as (ToolsMenuItem | "-")[],
39
+ attachments: attachmentsProp,
40
+ className = "",
41
+ }: CopilotChatProps = $props();
42
+
43
+ let generatedThreadId = $state(randomUUID());
44
+ let resolvedThreadId = $derived(explicitThreadId ?? generatedThreadId);
45
+ let hasExplicitThreadId = $derived(!!explicitThreadId);
46
+ let resolvedAgentId = $derived(agentId ?? DEFAULT_AGENT_ID);
47
+ let attachmentsConfig = $derived(normalizeAttachmentsConfig(attachmentsProp));
48
+
49
+ // svelte-ignore state_referenced_locally
50
+ setChatConfig(new ChatConfig(
51
+ agentId ?? DEFAULT_AGENT_ID,
52
+ resolvedThreadId,
53
+ hasExplicitThreadId,
54
+ ));
55
+
56
+ let agentHandle = $state<ReturnType<typeof createAgent> | null>(null);
57
+ let suggestionsHandle = $state<ReturnType<typeof createSuggestions> | null>(null);
58
+
59
+ // Runs during init (before first render) and when agentId/threadId change.
60
+ // createAgent/createSuggestions inherently have subscribing side-effects
61
+ // (AG-UI event listeners), so $effect.pre is the correct rune — $derived
62
+ // requires pure expressions.
63
+ $effect.pre(() => {
64
+ const id = resolvedAgentId;
65
+ const tid = resolvedThreadId;
66
+ const config = getChatConfig();
67
+ if (config) {
68
+ config.agentId = id;
69
+ config.threadId = tid;
70
+ }
71
+ // Bind to the shared agent, as React's CopilotChat does, so every
72
+ // createAgent({ agentId }) in the app sees this chat's conversation.
73
+ // The chat's thread is written onto that agent below.
74
+ agentHandle = createAgent({ agentId: id, throttleMs });
75
+ suggestionsHandle = createSuggestions({ agentId: id });
76
+ });
77
+
78
+ let { copilotkit } = useCopilotKit();
79
+ let agent = $derived(agentHandle?.agent ?? null);
80
+
81
+ // Keep the shared agent on this chat's thread. When the thread changes on
82
+ // the same agent, detach the old thread's run and clear what it left behind,
83
+ // so the new thread does not show the previous conversation.
84
+ let boundThread: { agent: AbstractAgent; threadId: string } | null = null;
85
+ $effect.pre(() => {
86
+ const activeAgent = agent;
87
+ const threadId = resolvedThreadId;
88
+ if (!activeAgent) return;
89
+ const previous = boundThread;
90
+ boundThread = { agent: activeAgent, threadId };
91
+ if (activeAgent.threadId === threadId) return;
92
+ activeAgent.threadId = threadId;
93
+ if (previous?.agent === activeAgent && previous.threadId !== threadId) {
94
+ void activeAgent.detachActiveRun().catch((error: unknown) => {
95
+ console.error("CopilotChat: detachActiveRun failed", error);
96
+ });
97
+ activeAgent.setMessages([]);
98
+ activeAgent.setState({});
99
+ }
100
+ });
101
+ let messages = $derived(agentHandle?.messages ?? []);
102
+ let isRunning = $derived(agentHandle?.isRunning ?? false);
103
+ let suggestions = $derived(suggestionsHandle?.suggestions ?? []);
104
+ let sendError = $state<string | null>(null);
105
+ let sentMessageId = $state<string | null>(null);
106
+
107
+ // Reactive config via getter so prop updates flow into the hook without
108
+ // re-creating it. Refs bind through Svelte actions (see markup below).
109
+ const attachmentsHandle = createAttachments({
110
+ config: () => attachmentsConfig,
111
+ });
112
+ let selectedAttachments = $derived(attachmentsHandle.attachments);
113
+ let attachmentsEnabled = $derived(attachmentsHandle.enabled);
114
+ let dragOver = $derived(attachmentsHandle.dragOver);
115
+
116
+ function handleAddFile() {
117
+ if (!attachmentsEnabled) return;
118
+ // Defer one tick so any open menu closes before the picker opens.
119
+ setTimeout(() => {
120
+ attachmentsHandle.fileInputRef?.click();
121
+ }, 100);
122
+ }
123
+
124
+ async function sendMessage(value: string, includeAttachments = false) {
125
+ const activeAgent = agent;
126
+ if (!activeAgent) return;
127
+ sendError = null;
128
+
129
+ try {
130
+ await dispatchAfterActiveRunSettles(activeAgent, async () => {
131
+ if (includeAttachments && selectedAttachments.some((a) => a.status === "uploading")) {
132
+ sendError = "Wait for attachments to finish uploading before sending.";
133
+ return;
134
+ }
135
+
136
+ const ready = includeAttachments
137
+ ? attachmentsHandle.consumeAttachments()
138
+ : [];
139
+ const messageId = randomUUID();
140
+ if (ready.length > 0) {
141
+ const contentParts: InputContent[] = [];
142
+ if (value.trim()) {
143
+ contentParts.push({ type: "text", text: value });
144
+ }
145
+ for (const attachment of ready) {
146
+ contentParts.push(createAttachmentContent(attachment));
147
+ }
148
+ activeAgent.addMessage({
149
+ id: messageId,
150
+ role: "user",
151
+ content: contentParts,
152
+ } as UserMessage);
153
+ } else {
154
+ activeAgent.addMessage({
155
+ id: messageId,
156
+ role: "user",
157
+ content: value,
158
+ } as UserMessage);
159
+ }
160
+
161
+ sentMessageId = messageId;
162
+ await copilotkit.runAgent({ agent: activeAgent });
163
+ });
164
+ } catch (error) {
165
+ console.error("CopilotChat: runAgent failed after sending a message", error);
166
+ sendError = "The message could not be sent. Please try again.";
167
+ }
168
+ }
169
+
170
+ function handleSubmitMessage(value: string) {
171
+ return sendMessage(value, true);
172
+ }
173
+
174
+ function handleStop() {
175
+ if (agent) {
176
+ copilotkit.stopAgent({ agent });
177
+ }
178
+ }
179
+
180
+ function handleInputChange(value: string) {
181
+ onInputChange?.(value);
182
+ }
183
+
184
+ function handleSelectSuggestion(suggestion: Suggestion) {
185
+ return sendMessage(suggestion.message);
186
+ }
187
+ </script>
188
+
189
+ <div
190
+ data-copilotkit
191
+ class="copilotkit-chat {className}"
192
+ use:attachmentsHandle.containerAction
193
+ >
194
+ {#if sendError}
195
+ <p class="copilotkit-chat-error" role="alert">{sendError}</p>
196
+ {/if}
197
+ {#if attachmentsEnabled}
198
+ <input
199
+ type="file"
200
+ multiple
201
+ use:attachmentsHandle.fileInputAction
202
+ onchange={(e) => void attachmentsHandle.handleFileUpload(e)}
203
+ accept={attachmentsConfig?.accept ?? "*/*"}
204
+ style="display: none"
205
+ data-testid="copilot-file-input"
206
+ />
207
+ {/if}
208
+ <CopilotChatView
209
+ messages={messages}
210
+ {isRunning}
211
+ {autoScroll}
212
+ {sentMessageId}
213
+ {welcomeScreen}
214
+ suggestions={suggestions}
215
+ attachments={selectedAttachments}
216
+ {dragOver}
217
+ inputValue={controlledInputValue}
218
+ {inputMode}
219
+ inputToolsMenu={inputToolsMenu}
220
+ onSubmitMessage={handleSubmitMessage}
221
+ onStop={handleStop}
222
+ onInputChange={handleInputChange}
223
+ onSelectSuggestion={handleSelectSuggestion}
224
+ onRemoveAttachment={attachmentsEnabled ? attachmentsHandle.removeAttachment : undefined}
225
+ onAddFile={attachmentsEnabled ? handleAddFile : undefined}
226
+ onDragOver={attachmentsEnabled ? attachmentsHandle.handleDragOver : undefined}
227
+ onDragLeave={attachmentsEnabled ? attachmentsHandle.handleDragLeave : undefined}
228
+ onDrop={attachmentsEnabled ? attachmentsHandle.handleDrop : undefined}
229
+ />
230
+ </div>
231
+
232
+ <style>
233
+ .copilotkit-chat {
234
+ display: flex;
235
+ flex-direction: column;
236
+ height: 100%;
237
+ overflow: hidden;
238
+ }
239
+
240
+ .copilotkit-chat-error {
241
+ margin: 0;
242
+ padding: 8px 12px;
243
+ color: #b91c1c;
244
+ background: #fef2f2;
245
+ font-size: 0.875rem;
246
+ }
247
+ </style>
@@ -0,0 +1,5 @@
1
+ import type { CopilotChatProps } from "./types";
2
+ declare const CopilotChat: import("svelte").Component<CopilotChatProps, {}, "">;
3
+ type CopilotChat = ReturnType<typeof CopilotChat>;
4
+ export default CopilotChat;
5
+ //# sourceMappingURL=CopilotChat.svelte.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CopilotChat.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/chat/CopilotChat.svelte"],"names":[],"mappings":"AAiBE,OAAO,KAAK,EAAE,gBAAgB,EAAuC,MAAM,SAAS,CAAC;AA6LvF,QAAA,MAAM,WAAW,sDAAwC,CAAC;AAC1D,KAAK,WAAW,GAAG,UAAU,CAAC,OAAO,WAAW,CAAC,CAAC;AAClD,eAAe,WAAW,CAAC"}