@copilotkit/svelte 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (180) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +325 -2
  3. package/dist/components/chat/CopilotChat.d.svelte.ts +1 -0
  4. package/dist/components/chat/CopilotChat.svelte.d.ts +1 -0
  5. package/dist/components/chat/CopilotChatAssistantMessage.d.svelte.ts +1 -0
  6. package/dist/components/chat/CopilotChatAssistantMessage.svelte.d.ts +1 -0
  7. package/dist/components/chat/CopilotChatAttachmentQueue.d.svelte.ts +1 -0
  8. package/dist/components/chat/CopilotChatAttachmentQueue.svelte.d.ts +1 -0
  9. package/dist/components/chat/CopilotChatInput.d.svelte.ts +1 -0
  10. package/dist/components/chat/CopilotChatInput.svelte.d.ts +1 -0
  11. package/dist/components/chat/CopilotChatMessage.d.svelte.ts +1 -0
  12. package/dist/components/chat/CopilotChatMessage.svelte.d.ts +1 -0
  13. package/dist/components/chat/CopilotChatMessageView.d.svelte.ts +1 -0
  14. package/dist/components/chat/CopilotChatMessageView.svelte.d.ts +1 -0
  15. package/dist/components/chat/CopilotChatReasoningMessage.d.svelte.ts +1 -0
  16. package/dist/components/chat/CopilotChatReasoningMessage.svelte.d.ts +1 -0
  17. package/dist/components/chat/CopilotChatSuggestionPill.d.svelte.ts +1 -0
  18. package/dist/components/chat/CopilotChatSuggestionPill.svelte.d.ts +1 -0
  19. package/dist/components/chat/CopilotChatSuggestionView.d.svelte.ts +1 -0
  20. package/dist/components/chat/CopilotChatSuggestionView.svelte.d.ts +1 -0
  21. package/dist/components/chat/CopilotChatToggleButton.d.svelte.ts +1 -0
  22. package/dist/components/chat/CopilotChatToggleButton.svelte.d.ts +1 -0
  23. package/dist/components/chat/CopilotChatToolCallsView.d.svelte.ts +1 -0
  24. package/dist/components/chat/CopilotChatToolCallsView.svelte.d.ts +1 -0
  25. package/dist/components/chat/CopilotChatUserMessage.d.svelte.ts +1 -0
  26. package/dist/components/chat/CopilotChatUserMessage.svelte.d.ts +1 -0
  27. package/dist/components/chat/CopilotChatView.d.svelte.ts +1 -0
  28. package/dist/components/chat/CopilotChatView.svelte.d.ts +1 -0
  29. package/dist/components/chat/CopilotPopup.d.svelte.ts +1 -0
  30. package/dist/components/chat/CopilotPopup.svelte.d.ts +1 -0
  31. package/dist/components/chat/CopilotSidebar.d.svelte.ts +1 -0
  32. package/dist/components/chat/CopilotSidebar.svelte.d.ts +1 -0
  33. package/dist/components/chat/StreamMarkdown.d.svelte.ts +1 -0
  34. package/dist/components/chat/StreamMarkdown.svelte.d.ts +1 -0
  35. package/dist/components/chat/chat-config-context.d.svelte.ts +10 -0
  36. package/dist/components/chat/chat-config-context.svelte.d.ts +10 -0
  37. package/dist/components/chat/chat-config-context.svelte.d.ts.map +1 -0
  38. package/dist/components/chat/dispatch-after-active-run.d.ts +3 -0
  39. package/dist/components/chat/dispatch-after-active-run.d.ts.map +1 -0
  40. package/dist/components/chat/markdown/CodeBlock.svelte.d.ts +1 -0
  41. package/dist/components/chat/markdown/CodeSpan.svelte.d.ts +1 -0
  42. package/dist/components/chat/markdown/Table.svelte.d.ts +1 -0
  43. package/dist/components/chat/markdown/TableBody.svelte.d.ts +1 -0
  44. package/dist/components/chat/markdown/TableCell.svelte.d.ts +1 -0
  45. package/dist/components/chat/markdown/TableHead.svelte.d.ts +1 -0
  46. package/dist/components/chat/markdown/TableRow.svelte.d.ts +1 -0
  47. package/dist/components/chat/markdown/highlight.d.ts +8 -0
  48. package/dist/components/chat/markdown/highlight.d.ts.map +1 -0
  49. package/dist/components/chat/media-parts.d.ts +11 -0
  50. package/dist/components/chat/media-parts.d.ts.map +1 -0
  51. package/dist/components/chat/types.d.ts +137 -0
  52. package/dist/components/chat/types.d.ts.map +1 -0
  53. package/dist/components/icons/index.d.ts +2 -0
  54. package/dist/components/icons/index.d.ts.map +1 -0
  55. package/dist/components/index.d.ts +18 -0
  56. package/dist/components/index.d.ts.map +1 -0
  57. package/dist/hooks/connect-agent-context.svelte.d.ts +9 -0
  58. package/dist/hooks/connect-agent-context.svelte.d.ts.map +1 -0
  59. package/dist/hooks/create-agent.svelte.d.ts +23 -0
  60. package/dist/hooks/create-agent.svelte.d.ts.map +1 -0
  61. package/dist/hooks/create-attachments.svelte.d.ts +33 -0
  62. package/dist/hooks/create-attachments.svelte.d.ts.map +1 -0
  63. package/dist/hooks/create-capabilities.svelte.d.ts +6 -0
  64. package/dist/hooks/create-capabilities.svelte.d.ts.map +1 -0
  65. package/dist/hooks/create-interrupt.svelte.d.ts +24 -0
  66. package/dist/hooks/create-interrupt.svelte.d.ts.map +1 -0
  67. package/dist/hooks/create-suggestions.svelte.d.ts +12 -0
  68. package/dist/hooks/create-suggestions.svelte.d.ts.map +1 -0
  69. package/dist/hooks/create-threads.svelte.d.ts +33 -0
  70. package/dist/hooks/create-threads.svelte.d.ts.map +1 -0
  71. package/dist/hooks/index.d.ts +20 -0
  72. package/dist/hooks/index.d.ts.map +1 -0
  73. package/dist/hooks/load-katex-styles.svelte.d.ts +2 -0
  74. package/dist/hooks/load-katex-styles.svelte.d.ts.map +1 -0
  75. package/dist/hooks/register-frontend-tool.svelte.d.ts +3 -0
  76. package/dist/hooks/register-frontend-tool.svelte.d.ts.map +1 -0
  77. package/dist/hooks/register-human-in-the-loop.svelte.d.ts +3 -0
  78. package/dist/hooks/register-human-in-the-loop.svelte.d.ts.map +1 -0
  79. package/dist/hooks/register-render-tool-call.svelte.d.ts +30 -0
  80. package/dist/hooks/register-render-tool-call.svelte.d.ts.map +1 -0
  81. package/dist/index.cjs +5 -0
  82. package/dist/index.cjs.map +1 -0
  83. package/dist/index.d.cts +6 -0
  84. package/dist/index.d.mts +6 -0
  85. package/dist/index.d.ts +6 -0
  86. package/dist/index.d.ts.map +1 -0
  87. package/dist/index.mjs +3037 -0
  88. package/dist/index.mjs.map +1 -0
  89. package/dist/lib/svelte-core.d.ts +53 -0
  90. package/dist/lib/svelte-core.d.ts.map +1 -0
  91. package/dist/providers/CopilotKitProvider.svelte.d.ts +1 -0
  92. package/dist/providers/CopilotKitProvider.types.d.ts +47 -0
  93. package/dist/providers/CopilotKitProvider.types.d.ts.map +1 -0
  94. package/dist/providers/context.d.ts +19 -0
  95. package/dist/providers/context.d.ts.map +1 -0
  96. package/dist/providers/index.d.ts +6 -0
  97. package/dist/providers/index.d.ts.map +1 -0
  98. package/dist/providers/useCopilotKit.d.ts +3 -0
  99. package/dist/providers/useCopilotKit.d.ts.map +1 -0
  100. package/dist/styles.css +3 -0
  101. package/dist/types/a2ui.d.ts +5 -0
  102. package/dist/types/a2ui.d.ts.map +1 -0
  103. package/dist/types/activity-message-renderer.d.ts +17 -0
  104. package/dist/types/activity-message-renderer.d.ts.map +1 -0
  105. package/dist/types/custom-message-renderer.d.ts +18 -0
  106. package/dist/types/custom-message-renderer.d.ts.map +1 -0
  107. package/dist/types/frontend-tool.d.ts +6 -0
  108. package/dist/types/frontend-tool.d.ts.map +1 -0
  109. package/dist/types/human-in-the-loop.d.ts +31 -0
  110. package/dist/types/human-in-the-loop.d.ts.map +1 -0
  111. package/dist/types/index.d.ts +9 -0
  112. package/dist/types/index.d.ts.map +1 -0
  113. package/dist/types/interrupt.d.ts +23 -0
  114. package/dist/types/interrupt.d.ts.map +1 -0
  115. package/dist/types/sandbox-function.d.ts +8 -0
  116. package/dist/types/sandbox-function.d.ts.map +1 -0
  117. package/dist/types/tool-call-renderer.d.ts +29 -0
  118. package/dist/types/tool-call-renderer.d.ts.map +1 -0
  119. package/package.json +114 -3
  120. package/src/app.d.ts +7 -0
  121. package/src/components/chat/CopilotChat.svelte +222 -0
  122. package/src/components/chat/CopilotChatAssistantMessage.svelte +295 -0
  123. package/src/components/chat/CopilotChatAttachmentQueue.svelte +241 -0
  124. package/src/components/chat/CopilotChatInput.svelte +192 -0
  125. package/src/components/chat/CopilotChatMessage.svelte +37 -0
  126. package/src/components/chat/CopilotChatMessageView.svelte +76 -0
  127. package/src/components/chat/CopilotChatReasoningMessage.svelte +210 -0
  128. package/src/components/chat/CopilotChatSuggestionPill.svelte +66 -0
  129. package/src/components/chat/CopilotChatSuggestionView.svelte +36 -0
  130. package/src/components/chat/CopilotChatToggleButton.svelte +47 -0
  131. package/src/components/chat/CopilotChatToolCallsView.svelte +187 -0
  132. package/src/components/chat/CopilotChatUserMessage.svelte +312 -0
  133. package/src/components/chat/CopilotChatView.svelte +177 -0
  134. package/src/components/chat/CopilotPopup.svelte +187 -0
  135. package/src/components/chat/CopilotSidebar.svelte +138 -0
  136. package/src/components/chat/StreamMarkdown.svelte +30 -0
  137. package/src/components/chat/chat-config-context.svelte.ts +28 -0
  138. package/src/components/chat/dispatch-after-active-run.ts +40 -0
  139. package/src/components/chat/markdown/CodeBlock.svelte +78 -0
  140. package/src/components/chat/markdown/CodeSpan.svelte +29 -0
  141. package/src/components/chat/markdown/Table.svelte +15 -0
  142. package/src/components/chat/markdown/TableBody.svelte +11 -0
  143. package/src/components/chat/markdown/TableCell.svelte +29 -0
  144. package/src/components/chat/markdown/TableHead.svelte +11 -0
  145. package/src/components/chat/markdown/TableRow.svelte +11 -0
  146. package/src/components/chat/markdown/highlight.ts +96 -0
  147. package/src/components/chat/media-parts.ts +34 -0
  148. package/src/components/chat/types.ts +176 -0
  149. package/src/components/icons/index.ts +23 -0
  150. package/src/components/index.ts +17 -0
  151. package/src/hooks/connect-agent-context.svelte.ts +46 -0
  152. package/src/hooks/create-agent.svelte.ts +420 -0
  153. package/src/hooks/create-attachments.svelte.ts +311 -0
  154. package/src/hooks/create-capabilities.svelte.ts +26 -0
  155. package/src/hooks/create-interrupt.svelte.ts +467 -0
  156. package/src/hooks/create-suggestions.svelte.ts +84 -0
  157. package/src/hooks/create-threads.svelte.ts +337 -0
  158. package/src/hooks/index.ts +45 -0
  159. package/src/hooks/load-katex-styles.svelte.ts +13 -0
  160. package/src/hooks/register-frontend-tool.svelte.ts +45 -0
  161. package/src/hooks/register-human-in-the-loop.svelte.ts +118 -0
  162. package/src/hooks/register-render-tool-call.svelte.ts +75 -0
  163. package/src/index.ts +6 -0
  164. package/src/lib/svelte-core.ts +230 -0
  165. package/src/providers/CopilotKitProvider.svelte +291 -0
  166. package/src/providers/CopilotKitProvider.types.ts +55 -0
  167. package/src/providers/context.ts +26 -0
  168. package/src/providers/index.ts +5 -0
  169. package/src/providers/useCopilotKit.ts +11 -0
  170. package/src/styles/globals.css +691 -0
  171. package/src/styles.d.ts +1 -0
  172. package/src/types/a2ui.ts +5 -0
  173. package/src/types/activity-message-renderer.ts +21 -0
  174. package/src/types/custom-message-renderer.ts +23 -0
  175. package/src/types/frontend-tool.ts +8 -0
  176. package/src/types/human-in-the-loop.ts +40 -0
  177. package/src/types/index.ts +8 -0
  178. package/src/types/interrupt.ts +32 -0
  179. package/src/types/sandbox-function.ts +8 -0
  180. package/src/types/tool-call-renderer.ts +36 -0
@@ -0,0 +1,210 @@
1
+ <script lang="ts">
2
+ import type { Message, ReasoningMessage } from "@ag-ui/core";
3
+ import type { Snippet } from "svelte";
4
+ import { IconChevronRight } from "../icons";
5
+ import StreamMarkdown from "./StreamMarkdown.svelte";
6
+
7
+ interface HeaderSlotProps {
8
+ isOpen: boolean;
9
+ label: string;
10
+ hasContent: boolean;
11
+ isStreaming: boolean;
12
+ onClick?: () => void;
13
+ }
14
+
15
+ interface ContentViewSlotProps {
16
+ isStreaming: boolean;
17
+ hasContent: boolean;
18
+ content: string;
19
+ }
20
+
21
+ interface ToggleSlotProps {
22
+ isOpen: boolean;
23
+ contentView: ContentViewSlotProps;
24
+ }
25
+
26
+ interface LayoutSlotProps {
27
+ message: ReasoningMessage;
28
+ messages: Message[];
29
+ isRunning: boolean;
30
+ header: HeaderSlotProps;
31
+ contentView: ContentViewSlotProps;
32
+ toggle: ToggleSlotProps;
33
+ }
34
+
35
+ let {
36
+ message,
37
+ messages = [] as Message[],
38
+ isRunning = false,
39
+ header,
40
+ contentView,
41
+ toggle,
42
+ layout,
43
+ }: {
44
+ message: ReasoningMessage;
45
+ messages?: Message[];
46
+ isRunning?: boolean;
47
+ header?: Snippet<[HeaderSlotProps]>;
48
+ contentView?: Snippet<[ContentViewSlotProps]>;
49
+ toggle?: Snippet<[ToggleSlotProps]>;
50
+ layout?: Snippet<[LayoutSlotProps]>;
51
+ children?: Snippet<[]>;
52
+ } = $props();
53
+
54
+ function formatDuration(seconds: number): string {
55
+ if (seconds < 1) return "a few seconds";
56
+ if (seconds < 60) return `${Math.round(seconds)} seconds`;
57
+ const mins = Math.floor(seconds / 60);
58
+ const secs = Math.round(seconds % 60);
59
+ if (secs === 0) return `${mins} minute${mins > 1 ? "s" : ""}`;
60
+ return `${mins}m ${secs}s`;
61
+ }
62
+
63
+ const normalizedContent = $derived(
64
+ typeof message.content === "string" ? message.content : "",
65
+ );
66
+ const hasContent = $derived(normalizedContent.length > 0);
67
+ const isLatest = $derived(
68
+ messages[messages.length - 1]?.id === message.id,
69
+ );
70
+ const isStreaming = $derived(!!(isRunning && isLatest));
71
+
72
+ let elapsed = $state(0);
73
+ let isOpen = $state(false);
74
+ let userToggledDuringStreaming = $state(false);
75
+ let startTimeMs: number | null = null;
76
+ let elapsedInterval: ReturnType<typeof setInterval> | null = null;
77
+
78
+ function clearElapsedInterval() {
79
+ if (elapsedInterval) {
80
+ clearInterval(elapsedInterval);
81
+ elapsedInterval = null;
82
+ }
83
+ }
84
+
85
+ function updateElapsedNow() {
86
+ if (startTimeMs !== null) {
87
+ elapsed = (Date.now() - startTimeMs) / 1000;
88
+ }
89
+ }
90
+
91
+ $effect(() => {
92
+ if (isStreaming) {
93
+ if (startTimeMs === null) {
94
+ startTimeMs = Date.now();
95
+ }
96
+ clearElapsedInterval();
97
+ elapsedInterval = setInterval(updateElapsedNow, 1000);
98
+ userToggledDuringStreaming = false;
99
+ isOpen = true;
100
+ } else {
101
+ clearElapsedInterval();
102
+ updateElapsedNow();
103
+ if (!userToggledDuringStreaming) {
104
+ isOpen = false;
105
+ }
106
+ }
107
+
108
+ return () => {
109
+ clearElapsedInterval();
110
+ };
111
+ });
112
+
113
+ const label = $derived(
114
+ isStreaming
115
+ ? "Thinking\u2026"
116
+ : `Thought for ${formatDuration(elapsed)}`,
117
+ );
118
+
119
+ function toggleOpen() {
120
+ if (!hasContent) return;
121
+ userToggledDuringStreaming = true;
122
+ isOpen = !isOpen;
123
+ }
124
+
125
+ const headerProps = $derived<HeaderSlotProps>({
126
+ get isOpen() { return isOpen; },
127
+ get label() { return label; },
128
+ get hasContent() { return hasContent; },
129
+ get isStreaming() { return isStreaming; },
130
+ get onClick() { return hasContent ? toggleOpen : undefined; },
131
+ });
132
+
133
+ const contentViewProps = $derived<ContentViewSlotProps>({
134
+ get isStreaming() { return isStreaming; },
135
+ get hasContent() { return hasContent; },
136
+ get content() { return normalizedContent; },
137
+ });
138
+
139
+ const toggleProps = $derived<ToggleSlotProps>({
140
+ get isOpen() { return isOpen; },
141
+ get contentView() { return contentViewProps; },
142
+ });
143
+ </script>
144
+
145
+ <div data-copilotkit class="cpk:my-1" data-message-id={message.id}>
146
+ {#if layout}
147
+ {@render layout({
148
+ message,
149
+ messages,
150
+ isRunning,
151
+ header: headerProps,
152
+ contentView: contentViewProps,
153
+ toggle: toggleProps,
154
+ })}
155
+ {:else}
156
+ {#if header}
157
+ {@render header(headerProps)}
158
+ {:else}
159
+ <button
160
+ type="button"
161
+ class="cpk:inline-flex cpk:items-center cpk:gap-1 cpk:py-1 cpk:text-sm cpk:text-muted-foreground cpk:transition-colors cpk:select-none"
162
+ class:cpk:hover:text-foreground={hasContent}
163
+ class:cpk:cursor-pointer={hasContent}
164
+ class:cpk:cursor-default={!hasContent}
165
+ aria-expanded={hasContent ? isOpen : undefined}
166
+ onclick={hasContent ? toggleOpen : undefined}
167
+ >
168
+ <span class="cpk:font-medium">{label}</span>
169
+ {#if isStreaming && !hasContent}
170
+ <span class="cpk:inline-flex cpk:items-center cpk:ml-1">
171
+ <span class="cpk:w-1.5 cpk:h-1.5 cpk:rounded-full cpk:bg-muted-foreground cpk:animate-pulse"></span>
172
+ </span>
173
+ {/if}
174
+ {#if hasContent}
175
+ <IconChevronRight
176
+ class={"cpk:size-3.5 cpk:shrink-0 cpk:transition-transform cpk:duration-200" + (isOpen ? " cpk:rotate-90" : "")}
177
+ />
178
+ {/if}
179
+ </button>
180
+ {/if}
181
+
182
+ {#if toggle}
183
+ {@render toggle(toggleProps)}
184
+ {:else}
185
+ <div
186
+ class="cpk:grid cpk:transition-[grid-template-rows] cpk:duration-200 cpk:ease-in-out"
187
+ style="grid-template-rows: {isOpen ? '1fr' : '0fr'}"
188
+ >
189
+ <div class="cpk:overflow-hidden">
190
+ {#if contentView}
191
+ {@render contentView(contentViewProps)}
192
+ {:else}
193
+ {#if hasContent || isStreaming}
194
+ <div class="cpk:pb-2 cpk:pt-1">
195
+ <div class="cpk:text-sm cpk:text-muted-foreground">
196
+ <StreamMarkdown content={normalizedContent} />
197
+ {#if isStreaming && hasContent}
198
+ <span class="cpk:inline-flex cpk:items-center cpk:ml-1 cpk:align-middle">
199
+ <span class="cpk:w-2 cpk:h-2 cpk:rounded-full cpk:bg-muted-foreground cpk:animate-pulse"></span>
200
+ </span>
201
+ {/if}
202
+ </div>
203
+ </div>
204
+ {/if}
205
+ {/if}
206
+ </div>
207
+ </div>
208
+ {/if}
209
+ {/if}
210
+ </div>
@@ -0,0 +1,66 @@
1
+ <script lang="ts">
2
+ import type { Suggestion } from "@copilotkit/core";
3
+
4
+ let {
5
+ suggestion,
6
+ isLoading = false,
7
+ onSelect,
8
+ }: {
9
+ suggestion: Suggestion;
10
+ isLoading?: boolean;
11
+ onSelect: () => void;
12
+ } = $props();
13
+ </script>
14
+
15
+ <button
16
+ type="button"
17
+ class="copilotkit-suggestion-pill"
18
+ onclick={onSelect}
19
+ disabled={isLoading}
20
+ >
21
+ {#if isLoading}
22
+ <span class="copilotkit-spinner"></span>
23
+ {/if}
24
+ {suggestion.title}
25
+ </button>
26
+
27
+ <style>
28
+ .copilotkit-suggestion-pill {
29
+ display: inline-flex;
30
+ align-items: center;
31
+ gap: 6px;
32
+ padding: 8px 14px;
33
+ border: 1px solid #d1d5db;
34
+ border-radius: 16px;
35
+ background: #fff;
36
+ font-size: 13px;
37
+ color: #374151;
38
+ cursor: pointer;
39
+ white-space: nowrap;
40
+ transition: all 0.15s;
41
+ }
42
+
43
+ .copilotkit-suggestion-pill:hover:not(:disabled) {
44
+ border-color: #3b82f6;
45
+ color: #3b82f6;
46
+ background: #eff6ff;
47
+ }
48
+
49
+ .copilotkit-suggestion-pill:disabled {
50
+ opacity: 0.5;
51
+ cursor: not-allowed;
52
+ }
53
+
54
+ .copilotkit-spinner {
55
+ width: 12px;
56
+ height: 12px;
57
+ border: 2px solid #e5e7eb;
58
+ border-top-color: #3b82f6;
59
+ border-radius: 50%;
60
+ animation: spin 0.6s linear infinite;
61
+ }
62
+
63
+ @keyframes spin {
64
+ to { transform: rotate(360deg); }
65
+ }
66
+ </style>
@@ -0,0 +1,36 @@
1
+ <script lang="ts">
2
+ import type { Suggestion } from "@copilotkit/core";
3
+ import CopilotChatSuggestionPill from "./CopilotChatSuggestionPill.svelte";
4
+
5
+ let {
6
+ suggestions = [] as Suggestion[],
7
+ loadingIndexes = [] as readonly number[],
8
+ onSelectSuggestion,
9
+ }: {
10
+ suggestions?: Suggestion[];
11
+ loadingIndexes?: readonly number[];
12
+ onSelectSuggestion: (suggestion: Suggestion, index: number) => void;
13
+ } = $props();
14
+ </script>
15
+
16
+ {#if suggestions.length > 0}
17
+ <div class="copilotkit-suggestions">
18
+ {#each suggestions as suggestion, i (i)}
19
+ <CopilotChatSuggestionPill
20
+ {suggestion}
21
+ isLoading={loadingIndexes.includes(i)}
22
+ onSelect={() => onSelectSuggestion(suggestion, i)}
23
+ />
24
+ {/each}
25
+ </div>
26
+ {/if}
27
+
28
+ <style>
29
+ .copilotkit-suggestions {
30
+ display: flex;
31
+ gap: 8px;
32
+ padding: 0;
33
+ overflow-x: auto;
34
+ flex-wrap: wrap;
35
+ }
36
+ </style>
@@ -0,0 +1,47 @@
1
+ <script lang="ts">
2
+ let {
3
+ isOpen = false,
4
+ onclick,
5
+ }: {
6
+ isOpen?: boolean;
7
+ onclick?: () => void;
8
+ } = $props();
9
+ </script>
10
+
11
+ <button type="button" class="copilotkit-toggle-btn" aria-label={isOpen ? "Close chat" : "Open chat"} {onclick}>
12
+ {#if isOpen}
13
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
14
+ <path d="M18 6L6 18M6 6l12 12"/>
15
+ </svg>
16
+ {:else}
17
+ <svg width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2">
18
+ <path d="M21 15a2 2 0 01-2 2H7l-4 4V5a2 2 0 012-2h14a2 2 0 012 2z"/>
19
+ </svg>
20
+ {/if}
21
+ </button>
22
+
23
+ <style>
24
+ .copilotkit-toggle-btn {
25
+ position: fixed;
26
+ bottom: 24px;
27
+ right: 24px;
28
+ width: 56px;
29
+ height: 56px;
30
+ border-radius: 50%;
31
+ border: none;
32
+ background: #3b82f6;
33
+ color: #fff;
34
+ cursor: pointer;
35
+ display: flex;
36
+ align-items: center;
37
+ justify-content: center;
38
+ box-shadow: 0 4px 12px rgba(0, 0, 0, 0.2);
39
+ z-index: 100;
40
+ transition: transform 0.2s;
41
+ }
42
+
43
+ .copilotkit-toggle-btn:hover {
44
+ transform: scale(1.05);
45
+ background: #2563eb;
46
+ }
47
+ </style>
@@ -0,0 +1,187 @@
1
+ <script lang="ts">
2
+ import type { AssistantMessage, Message, ToolCall, ToolMessage } from "@ag-ui/core";
3
+ import { ToolCallStatus } from "@copilotkit/core";
4
+ import { DEFAULT_AGENT_ID, partialJSONParse } from "@copilotkit/shared";
5
+ import type { Snippet } from "svelte";
6
+ import { useCopilotKit } from "../../providers/useCopilotKit";
7
+ import type {
8
+ SvelteToolCallRenderer,
9
+ SvelteToolCallRendererRenderProps,
10
+ } from "../../types";
11
+ import type { CopilotChatToolCallRenderSlotProps } from "./types";
12
+ import { getChatConfig } from "./chat-config-context.svelte";
13
+ import { IconCheckCircle, IconCircle, IconLoader2 } from "../icons";
14
+
15
+ let {
16
+ message,
17
+ messages = [] as Message[],
18
+ toolCall,
19
+ }: {
20
+ message: AssistantMessage;
21
+ messages?: Message[];
22
+ toolCall?: Snippet<[CopilotChatToolCallRenderSlotProps]>;
23
+ } = $props();
24
+
25
+ const { copilotkit, executingToolCallIds } = useCopilotKit();
26
+ const chatConfig = getChatConfig();
27
+
28
+ let renderToolCalls = $state<SvelteToolCallRenderer<unknown>[]>([]);
29
+
30
+ $effect(() => {
31
+ const core = copilotkit;
32
+ renderToolCalls = [...(core.renderToolCalls ?? [])];
33
+ const subscription = core.subscribe({
34
+ onRenderToolCallsChanged: (event) => {
35
+ renderToolCalls = [...(event.renderToolCalls ?? [])];
36
+ },
37
+ });
38
+ return () => subscription.unsubscribe();
39
+ });
40
+
41
+ function findToolMessage(toolCallId: string): ToolMessage | undefined {
42
+ return messages.find(
43
+ (msg) =>
44
+ msg.role === "tool" &&
45
+ (msg as ToolMessage).toolCallId === toolCallId,
46
+ ) as ToolMessage | undefined;
47
+ }
48
+
49
+ function isExecuting(toolCallId: string): boolean {
50
+ return executingToolCallIds.has(toolCallId);
51
+ }
52
+
53
+ function getToolResult(toolCallId: string): string | undefined {
54
+ const content = findToolMessage(toolCallId)?.content;
55
+ return content === undefined ? undefined : contentToText(content);
56
+ }
57
+
58
+ function contentToText(content: ToolMessage["content"]): string {
59
+ if (typeof content === "string") return content;
60
+ return content
61
+ .map((part) =>
62
+ "text" in part && typeof part.text === "string"
63
+ ? part.text
64
+ : JSON.stringify(part) ?? "",
65
+ )
66
+ .join("");
67
+ }
68
+
69
+ function toolArgs(raw: string): Record<string, unknown> {
70
+ const parsed = partialJSONParse(raw);
71
+ if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
72
+ return parsed as Record<string, unknown>;
73
+ }
74
+ return {};
75
+ }
76
+
77
+ function matchRenderer(
78
+ name: string,
79
+ agentId: string,
80
+ ): SvelteToolCallRenderer<unknown> | undefined {
81
+ const scoped = renderToolCalls.find(
82
+ (entry) => entry.name === name && entry.agentId === agentId,
83
+ );
84
+ if (scoped) return scoped;
85
+ return renderToolCalls.find((entry) => entry.name === name && !entry.agentId);
86
+ }
87
+
88
+ function resolveRenderer(
89
+ toolName: string,
90
+ ): SvelteToolCallRenderer<unknown> | undefined {
91
+ const agentId = chatConfig?.agentId || DEFAULT_AGENT_ID;
92
+ return matchRenderer(toolName, agentId) ?? matchRenderer("*", agentId);
93
+ }
94
+
95
+ function getRenderProps(tc: ToolCall): CopilotChatToolCallRenderSlotProps {
96
+ const toolMessage = findToolMessage(tc.id);
97
+ const parsedArgs = partialJSONParse(tc.function.arguments);
98
+
99
+ if (toolMessage) {
100
+ return {
101
+ name: tc.function.name,
102
+ args: parsedArgs,
103
+ status: ToolCallStatus.Complete,
104
+ result: contentToText(toolMessage.content),
105
+ toolCall: tc,
106
+ toolMessage,
107
+ };
108
+ }
109
+
110
+ return {
111
+ name: tc.function.name,
112
+ args: parsedArgs,
113
+ status: isExecuting(tc.id) ? ToolCallStatus.Executing : ToolCallStatus.InProgress,
114
+ result: undefined,
115
+ toolCall: tc,
116
+ toolMessage: undefined,
117
+ };
118
+ }
119
+
120
+ function getCoreRenderProps(
121
+ tc: ToolCall,
122
+ ): SvelteToolCallRendererRenderProps<unknown> {
123
+ const toolMessage = findToolMessage(tc.id);
124
+ const args = toolArgs(tc.function.arguments);
125
+
126
+ if (toolMessage) {
127
+ return {
128
+ name: tc.function.name,
129
+ toolCallId: tc.id,
130
+ args,
131
+ status: ToolCallStatus.Complete,
132
+ result: contentToText(toolMessage.content),
133
+ };
134
+ }
135
+
136
+ if (isExecuting(tc.id)) {
137
+ return {
138
+ name: tc.function.name,
139
+ toolCallId: tc.id,
140
+ args,
141
+ status: ToolCallStatus.Executing,
142
+ result: undefined,
143
+ };
144
+ }
145
+
146
+ return {
147
+ name: tc.function.name,
148
+ toolCallId: tc.id,
149
+ args,
150
+ status: ToolCallStatus.InProgress,
151
+ result: undefined,
152
+ };
153
+ }
154
+
155
+ function asSnippet(value: unknown): Snippet {
156
+ return value as Snippet;
157
+ }
158
+ </script>
159
+
160
+ {#each message.toolCalls ?? [] as tc (tc.id)}
161
+ {#if toolCall}
162
+ {@render toolCall(getRenderProps(tc))}
163
+ {:else}
164
+ {@const renderer = resolveRenderer(tc.function.name)}
165
+ {#if renderer}
166
+ {@const rendered = renderer.render(getCoreRenderProps(tc))}
167
+ {#if typeof rendered === "string" || typeof rendered === "number"}
168
+ {rendered}
169
+ {:else if typeof rendered === "function"}
170
+ {@render asSnippet(rendered)()}
171
+ {/if}
172
+ {:else}
173
+ <div class="cpk:flex cpk:items-center cpk:gap-2 cpk:py-1 cpk:text-sm cpk:text-muted-foreground">
174
+ {#if isExecuting(tc.id)}
175
+ <IconLoader2 class="cpk:size-4 cpk:animate-spin" />
176
+ <span>{tc.function.name}</span>
177
+ {:else if getToolResult(tc.id)}
178
+ <IconCheckCircle class="cpk:size-4 cpk:text-green-500" />
179
+ <span>{tc.function.name}</span>
180
+ {:else}
181
+ <IconCircle class="cpk:size-4 cpk:text-muted-foreground" />
182
+ <span>{tc.function.name}</span>
183
+ {/if}
184
+ </div>
185
+ {/if}
186
+ {/if}
187
+ {/each}