@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
@@ -0,0 +1,10 @@
1
+ import type { Suggestion } from "@copilotkit/core";
2
+ type $$ComponentProps = {
3
+ suggestion: Suggestion;
4
+ isLoading?: boolean;
5
+ onSelect: () => void;
6
+ };
7
+ declare const CopilotChatSuggestionPill: import("svelte").Component<$$ComponentProps, {}, "">;
8
+ type CopilotChatSuggestionPill = ReturnType<typeof CopilotChatSuggestionPill>;
9
+ export default CopilotChatSuggestionPill;
10
+ //# sourceMappingURL=CopilotChatSuggestionPill.svelte.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CopilotChatSuggestionPill.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/chat/CopilotChatSuggestionPill.svelte"],"names":[],"mappings":"AAGE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAEpD,KAAK,gBAAgB,GAAI;IACtB,UAAU,EAAE,UAAU,CAAC;IACvB,SAAS,CAAC,EAAE,OAAO,CAAC;IACpB,QAAQ,EAAE,MAAM,IAAI,CAAC;CACtB,CAAC;AAqBJ,QAAA,MAAM,yBAAyB,sDAAwC,CAAC;AACxE,KAAK,yBAAyB,GAAG,UAAU,CAAC,OAAO,yBAAyB,CAAC,CAAC;AAC9E,eAAe,yBAAyB,CAAC"}
@@ -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,10 @@
1
+ import type { Suggestion } from "@copilotkit/core";
2
+ type $$ComponentProps = {
3
+ suggestions?: Suggestion[];
4
+ loadingIndexes?: readonly number[];
5
+ onSelectSuggestion: (suggestion: Suggestion, index: number) => void;
6
+ };
7
+ declare const CopilotChatSuggestionView: import("svelte").Component<$$ComponentProps, {}, "">;
8
+ type CopilotChatSuggestionView = ReturnType<typeof CopilotChatSuggestionView>;
9
+ export default CopilotChatSuggestionView;
10
+ //# sourceMappingURL=CopilotChatSuggestionView.svelte.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CopilotChatSuggestionView.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/chat/CopilotChatSuggestionView.svelte"],"names":[],"mappings":"AAGE,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAGpD,KAAK,gBAAgB,GAAI;IACtB,WAAW,CAAC,EAAE,UAAU,EAAE,CAAC;IAC3B,cAAc,CAAC,EAAE,SAAS,MAAM,EAAE,CAAC;IACnC,kBAAkB,EAAE,CAAC,UAAU,EAAE,UAAU,EAAE,KAAK,EAAE,MAAM,KAAK,IAAI,CAAC;CACrE,CAAC;AAsBJ,QAAA,MAAM,yBAAyB,sDAAwC,CAAC;AACxE,KAAK,yBAAyB,GAAG,UAAU,CAAC,OAAO,yBAAyB,CAAC,CAAC;AAC9E,eAAe,yBAAyB,CAAC"}
@@ -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,8 @@
1
+ type $$ComponentProps = {
2
+ isOpen?: boolean;
3
+ onclick?: () => void;
4
+ };
5
+ declare const CopilotChatToggleButton: import("svelte").Component<$$ComponentProps, {}, "">;
6
+ type CopilotChatToggleButton = ReturnType<typeof CopilotChatToggleButton>;
7
+ export default CopilotChatToggleButton;
8
+ //# sourceMappingURL=CopilotChatToggleButton.svelte.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CopilotChatToggleButton.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/chat/CopilotChatToggleButton.svelte"],"names":[],"mappings":"AAGC,KAAK,gBAAgB,GAAI;IACtB,MAAM,CAAC,EAAE,OAAO,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,IAAI,CAAC;CACtB,CAAC;AAwBJ,QAAA,MAAM,uBAAuB,sDAAwC,CAAC;AACtE,KAAK,uBAAuB,GAAG,UAAU,CAAC,OAAO,uBAAuB,CAAC,CAAC;AAC1E,eAAe,uBAAuB,CAAC"}
@@ -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}
@@ -0,0 +1,12 @@
1
+ import type { AssistantMessage, Message } from "@ag-ui/core";
2
+ import type { Snippet } from "svelte";
3
+ import type { CopilotChatToolCallRenderSlotProps } from "./types";
4
+ type $$ComponentProps = {
5
+ message: AssistantMessage;
6
+ messages?: Message[];
7
+ toolCall?: Snippet<[CopilotChatToolCallRenderSlotProps]>;
8
+ };
9
+ declare const CopilotChatToolCallsView: import("svelte").Component<$$ComponentProps, {}, "">;
10
+ type CopilotChatToolCallsView = ReturnType<typeof CopilotChatToolCallsView>;
11
+ export default CopilotChatToolCallsView;
12
+ //# sourceMappingURL=CopilotChatToolCallsView.svelte.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"CopilotChatToolCallsView.svelte.d.ts","sourceRoot":"","sources":["../../../src/components/chat/CopilotChatToolCallsView.svelte"],"names":[],"mappings":"AAGE,OAAO,KAAK,EAAE,gBAAgB,EAAE,OAAO,EAAyB,MAAM,aAAa,CAAC;AAGpF,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,QAAQ,CAAC;AAMtC,OAAO,KAAK,EAAE,kCAAkC,EAAE,MAAM,SAAS,CAAC;AAInE,KAAK,gBAAgB,GAAI;IACtB,OAAO,EAAE,gBAAgB,CAAC;IAC1B,QAAQ,CAAC,EAAE,OAAO,EAAE,CAAC;IACrB,QAAQ,CAAC,EAAE,OAAO,CAAC,CAAC,kCAAkC,CAAC,CAAC,CAAC;CAC1D,CAAC;AA+KJ,QAAA,MAAM,wBAAwB,sDAAwC,CAAC;AACvE,KAAK,wBAAwB,GAAG,UAAU,CAAC,OAAO,wBAAwB,CAAC,CAAC;AAC5E,eAAe,wBAAwB,CAAC"}
@@ -0,0 +1,312 @@
1
+ <script lang="ts">
2
+ import type { UserMessage } from "@ag-ui/core";
3
+ import type { Snippet } from "svelte";
4
+ import { getDocumentIcon, getSourceUrl } from "@copilotkit/shared";
5
+ import { getMediaPartFilename, getMediaParts } from "./media-parts";
6
+ import { IconCheck, IconCopy, IconEdit, IconChevronLeft, IconChevronRight } from "../icons";
7
+
8
+ interface MessageRendererProps {
9
+ message: UserMessage;
10
+ content: string;
11
+ isMultiline: boolean;
12
+ }
13
+
14
+ interface ToolbarProps {
15
+ message: UserMessage;
16
+ showBranchNavigation: boolean;
17
+ hasEditAction: boolean;
18
+ }
19
+
20
+ interface CopyButtonProps {
21
+ onCopy: () => Promise<void>;
22
+ copied: boolean;
23
+ label: string;
24
+ }
25
+
26
+ interface EditButtonProps {
27
+ onEdit: () => void;
28
+ label: string;
29
+ }
30
+
31
+ interface BranchNavigationProps {
32
+ branchIndex: number;
33
+ numberOfBranches: number;
34
+ canGoPrev: boolean;
35
+ canGoNext: boolean;
36
+ goPrev: () => void;
37
+ goNext: () => void;
38
+ }
39
+
40
+ interface LayoutProps {
41
+ message: UserMessage;
42
+ content: string;
43
+ isMultiline: boolean;
44
+ showBranchNavigation: boolean;
45
+ hasEditAction: boolean;
46
+ branchIndex: number;
47
+ numberOfBranches: number;
48
+ canGoPrev: boolean;
49
+ canGoNext: boolean;
50
+ onCopy: () => Promise<void>;
51
+ onEdit: () => void;
52
+ goPrev: () => void;
53
+ goNext: () => void;
54
+ copied: boolean;
55
+ }
56
+
57
+ const labels = {
58
+ userMessageToolbarCopyMessageLabel: "Copy",
59
+ userMessageToolbarEditMessageLabel: "Edit",
60
+ };
61
+
62
+ let {
63
+ message,
64
+ branchIndex = 0,
65
+ numberOfBranches = 1,
66
+ layout,
67
+ messageRenderer,
68
+ toolbar: toolbarSnippet,
69
+ copyButton,
70
+ editButton,
71
+ branchNavigation,
72
+ toolbarItems,
73
+ }: {
74
+ message: UserMessage;
75
+ branchIndex?: number;
76
+ numberOfBranches?: number;
77
+ layout?: Snippet<[LayoutProps]>;
78
+ messageRenderer?: Snippet<[MessageRendererProps]>;
79
+ toolbar?: Snippet<[ToolbarProps]>;
80
+ copyButton?: Snippet<[CopyButtonProps]>;
81
+ editButton?: Snippet<[EditButtonProps]>;
82
+ branchNavigation?: Snippet<[BranchNavigationProps]>;
83
+ toolbarItems?: Snippet<[]>;
84
+ children?: Snippet<[]>;
85
+ } = $props();
86
+
87
+ let copied = $state(false);
88
+ let copiedResetTimeout: ReturnType<typeof setTimeout> | null = null;
89
+
90
+ function flattenUserMessageContent(content?: UserMessage["content"]): string {
91
+ if (!content) return "";
92
+ if (typeof content === "string") return content;
93
+ return content
94
+ .map((part) => {
95
+ if (part && typeof part === "object" && "type" in part && (part as { type?: unknown }).type === "text" && typeof (part as { text?: unknown }).text === "string") {
96
+ return (part as { text: string }).text;
97
+ }
98
+ return "";
99
+ })
100
+ .filter((text) => text.length > 0)
101
+ .join("\n");
102
+ }
103
+
104
+ const flattenedContent = $derived(flattenUserMessageContent(message.content));
105
+ const isMultiline = $derived(flattenedContent.includes("\n"));
106
+ const mediaParts = $derived(
107
+ getMediaParts(message.content).filter(
108
+ (part) => getSourceUrl(part.source).length > 0,
109
+ ),
110
+ );
111
+
112
+ const hasEditAction = false;
113
+ const showBranchNavigation = $derived(numberOfBranches > 1);
114
+ const canGoPrev = $derived(branchIndex > 0);
115
+ const canGoNext = $derived(branchIndex < numberOfBranches - 1);
116
+
117
+ function resetCopiedStateWithDelay() {
118
+ if (copiedResetTimeout) clearTimeout(copiedResetTimeout);
119
+ copied = true;
120
+ copiedResetTimeout = setTimeout(() => {
121
+ copied = false;
122
+ copiedResetTimeout = null;
123
+ }, 2000);
124
+ }
125
+
126
+ async function handleCopyMessage() {
127
+ if (!flattenedContent) return;
128
+ if (typeof navigator === "undefined" || typeof navigator.clipboard?.writeText !== "function") return;
129
+ try {
130
+ await navigator.clipboard.writeText(flattenedContent);
131
+ resetCopiedStateWithDelay();
132
+ } catch (error) {
133
+ console.error("Failed to copy to clipboard:", error);
134
+ }
135
+ }
136
+
137
+ function handleEditMessage() {
138
+ if (typeof document !== "undefined") {
139
+ document.dispatchEvent(new CustomEvent("copilotkit:edit-message", { detail: { message } }));
140
+ }
141
+ }
142
+
143
+ function switchToBranch(branchIdx: number) {
144
+ if (typeof document !== "undefined") {
145
+ document.dispatchEvent(new CustomEvent("copilotkit:switch-branch", { detail: { branchIndex: branchIdx, numberOfBranches, message } }));
146
+ }
147
+ }
148
+
149
+ function goPrev() { if (canGoPrev) switchToBranch(branchIndex - 1); }
150
+ function goNext() { if (canGoNext) switchToBranch(branchIndex + 1); }
151
+
152
+ 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";
153
+
154
+ const layoutProps = $derived<LayoutProps>({
155
+ message,
156
+ get content() { return flattenedContent; },
157
+ get isMultiline() { return isMultiline; },
158
+ get showBranchNavigation() { return showBranchNavigation; },
159
+ get hasEditAction() { return hasEditAction; },
160
+ branchIndex,
161
+ numberOfBranches,
162
+ get canGoPrev() { return canGoPrev; },
163
+ get canGoNext() { return canGoNext; },
164
+ onCopy: handleCopyMessage,
165
+ onEdit: handleEditMessage,
166
+ goPrev,
167
+ goNext,
168
+ get copied() { return copied; },
169
+ });
170
+ </script>
171
+
172
+ {#if layout}
173
+ {@render layout(layoutProps)}
174
+ {:else}
175
+ <div
176
+ data-copilotkit
177
+ data-testid="copilot-user-message"
178
+ class="cpk:flex cpk:flex-col cpk:items-end cpk:w-full cpk:group cpk:pt-10"
179
+ data-message-id={message.id}
180
+ >
181
+ {#if mediaParts.length > 0}
182
+ <div
183
+ class="cpk:flex cpk:flex-row cpk:flex-wrap cpk:max-w-full cpk:justify-end cpk:gap-2 cpk:mb-2"
184
+ data-testid="copilot-user-attachments"
185
+ >
186
+ {#each mediaParts as part, index (index)}
187
+ {#if part.type === "image"}
188
+ <img
189
+ src={getSourceUrl(part.source)}
190
+ alt={getMediaPartFilename(part) ?? "Image attachment"}
191
+ class="cpk:max-h-[80px] cpk:max-w-[80px] cpk:h-auto cpk:w-auto cpk:rounded-xl cpk:object-cover cpk:bg-muted"
192
+ data-testid="copilot-user-message-image"
193
+ />
194
+ {:else if part.type === "video"}
195
+ <video
196
+ src={getSourceUrl(part.source)}
197
+ preload="metadata"
198
+ muted
199
+ class="cpk:max-h-[120px] cpk:max-w-[240px] cpk:rounded-xl cpk:bg-muted"
200
+ data-testid="copilot-user-message-video"
201
+ ></video>
202
+ {:else if part.type === "audio"}
203
+ <audio
204
+ src={getSourceUrl(part.source)}
205
+ controls
206
+ preload="metadata"
207
+ class="cpk:max-w-[300px] cpk:w-full cpk:h-10"
208
+ data-testid="copilot-user-message-audio"
209
+ ></audio>
210
+ {:else}
211
+ <div
212
+ class="cpk:flex cpk:items-center cpk:gap-2 cpk:rounded-lg cpk:border cpk:border-border cpk:bg-muted cpk:px-3 cpk:py-2 cpk:text-sm"
213
+ data-testid="copilot-user-message-document"
214
+ >
215
+ <span class="cpk:font-semibold cpk:text-muted-foreground">
216
+ {getDocumentIcon(part.source.mimeType ?? "")}
217
+ </span>
218
+ <span>{getMediaPartFilename(part) ?? "Document"}</span>
219
+ </div>
220
+ {/if}
221
+ {/each}
222
+ </div>
223
+ {/if}
224
+
225
+ {#if messageRenderer}
226
+ {@render messageRenderer({ message, content: flattenedContent, isMultiline })}
227
+ {:else}
228
+ <div
229
+ class="cpk:prose cpk:dark:prose-invert cpk:bg-muted cpk:relative cpk:max-w-[80%] cpk:rounded-[18px] cpk:px-4 cpk:py-1.5 cpk:inline-block cpk:whitespace-pre-wrap"
230
+ data-multiline={isMultiline ? "true" : undefined}
231
+ class:cpk:py-3={isMultiline}
232
+ >
233
+ {flattenedContent}
234
+ </div>
235
+ {/if}
236
+
237
+ {#if toolbarSnippet}
238
+ {@render toolbarSnippet({ message, showBranchNavigation, hasEditAction })}
239
+ {:else}
240
+ <div class="cpk:w-full cpk:bg-transparent cpk:flex cpk:items-center cpk:justify-end cpk:-mr-[5px] cpk:mt-[4px] cpk:opacity-0 cpk:group-hover:opacity-100 cpk:group-focus-within:opacity-100">
241
+ <div class="cpk:flex cpk:items-center cpk:gap-1 cpk:justify-end">
242
+ {#if toolbarItems}{@render toolbarItems()}{/if}
243
+
244
+ {#if copyButton}
245
+ {@render copyButton({ onCopy: handleCopyMessage, copied, label: labels.userMessageToolbarCopyMessageLabel })}
246
+ {:else}
247
+ <button
248
+ data-testid="copilot-user-copy-button"
249
+ type="button"
250
+ class={toolbarBtnClass}
251
+ aria-label={labels.userMessageToolbarCopyMessageLabel}
252
+ title={labels.userMessageToolbarCopyMessageLabel}
253
+ onclick={handleCopyMessage}
254
+ >
255
+ {#if copied}
256
+ <IconCheck class="cpk:size-[18px]" />
257
+ {:else}
258
+ <IconCopy class="cpk:size-[18px]" />
259
+ {/if}
260
+ </button>
261
+ {/if}
262
+
263
+ {#if editButton}
264
+ {@render editButton({ onEdit: handleEditMessage, label: labels.userMessageToolbarEditMessageLabel })}
265
+ {:else if hasEditAction}
266
+ <button
267
+ type="button"
268
+ class={toolbarBtnClass}
269
+ aria-label={labels.userMessageToolbarEditMessageLabel}
270
+ title={labels.userMessageToolbarEditMessageLabel}
271
+ onclick={handleEditMessage}
272
+ >
273
+ <IconEdit class="cpk:size-[18px]" />
274
+ </button>
275
+ {/if}
276
+
277
+ {#if showBranchNavigation}
278
+ {#if branchNavigation}
279
+ {@render branchNavigation({ branchIndex, numberOfBranches, canGoPrev, canGoNext, goPrev, goNext })}
280
+ {:else}
281
+ <div class="cpk:flex cpk:items-center cpk:gap-1">
282
+ <button
283
+ type="button"
284
+ class="{toolbarBtnClass} cpk:h-6 cpk:w-6 cpk:p-0"
285
+ disabled={!canGoPrev}
286
+ aria-label="Previous branch"
287
+ title="Previous branch"
288
+ onclick={goPrev}
289
+ >
290
+ <IconChevronLeft class="cpk:size-[20px]" />
291
+ </button>
292
+ <span class="cpk:text-sm cpk:text-muted-foreground cpk:px-0 cpk:font-medium">
293
+ {branchIndex + 1}/{numberOfBranches}
294
+ </span>
295
+ <button
296
+ type="button"
297
+ class="{toolbarBtnClass} cpk:h-6 cpk:w-6 cpk:p-0"
298
+ disabled={!canGoNext}
299
+ aria-label="Next branch"
300
+ title="Next branch"
301
+ onclick={goNext}
302
+ >
303
+ <IconChevronRight class="cpk:size-[20px]" />
304
+ </button>
305
+ </div>
306
+ {/if}
307
+ {/if}
308
+ </div>
309
+ </div>
310
+ {/if}
311
+ </div>
312
+ {/if}