@copilotkit/svelte 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +325 -2
- package/dist/components/chat/CopilotChat.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChat.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatAssistantMessage.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatAssistantMessage.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatAttachmentQueue.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatAttachmentQueue.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatInput.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatInput.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatMessage.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatMessage.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatMessageView.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatMessageView.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatReasoningMessage.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatReasoningMessage.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatSuggestionPill.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatSuggestionPill.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatSuggestionView.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatSuggestionView.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatToggleButton.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatToggleButton.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatToolCallsView.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatToolCallsView.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatUserMessage.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatUserMessage.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotChatView.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotChatView.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotPopup.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotPopup.svelte.d.ts +1 -0
- package/dist/components/chat/CopilotSidebar.d.svelte.ts +1 -0
- package/dist/components/chat/CopilotSidebar.svelte.d.ts +1 -0
- package/dist/components/chat/StreamMarkdown.d.svelte.ts +1 -0
- package/dist/components/chat/StreamMarkdown.svelte.d.ts +1 -0
- package/dist/components/chat/chat-config-context.d.svelte.ts +10 -0
- package/dist/components/chat/chat-config-context.svelte.d.ts +10 -0
- package/dist/components/chat/chat-config-context.svelte.d.ts.map +1 -0
- package/dist/components/chat/dispatch-after-active-run.d.ts +3 -0
- package/dist/components/chat/dispatch-after-active-run.d.ts.map +1 -0
- package/dist/components/chat/markdown/CodeBlock.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/CodeSpan.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/Table.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/TableBody.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/TableCell.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/TableHead.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/TableRow.svelte.d.ts +1 -0
- package/dist/components/chat/markdown/highlight.d.ts +8 -0
- package/dist/components/chat/markdown/highlight.d.ts.map +1 -0
- package/dist/components/chat/media-parts.d.ts +11 -0
- package/dist/components/chat/media-parts.d.ts.map +1 -0
- package/dist/components/chat/types.d.ts +137 -0
- package/dist/components/chat/types.d.ts.map +1 -0
- package/dist/components/icons/index.d.ts +2 -0
- package/dist/components/icons/index.d.ts.map +1 -0
- package/dist/components/index.d.ts +18 -0
- package/dist/components/index.d.ts.map +1 -0
- package/dist/hooks/connect-agent-context.svelte.d.ts +9 -0
- package/dist/hooks/connect-agent-context.svelte.d.ts.map +1 -0
- package/dist/hooks/create-agent.svelte.d.ts +23 -0
- package/dist/hooks/create-agent.svelte.d.ts.map +1 -0
- package/dist/hooks/create-attachments.svelte.d.ts +33 -0
- package/dist/hooks/create-attachments.svelte.d.ts.map +1 -0
- package/dist/hooks/create-capabilities.svelte.d.ts +6 -0
- package/dist/hooks/create-capabilities.svelte.d.ts.map +1 -0
- package/dist/hooks/create-interrupt.svelte.d.ts +24 -0
- package/dist/hooks/create-interrupt.svelte.d.ts.map +1 -0
- package/dist/hooks/create-suggestions.svelte.d.ts +12 -0
- package/dist/hooks/create-suggestions.svelte.d.ts.map +1 -0
- package/dist/hooks/create-threads.svelte.d.ts +33 -0
- package/dist/hooks/create-threads.svelte.d.ts.map +1 -0
- package/dist/hooks/index.d.ts +20 -0
- package/dist/hooks/index.d.ts.map +1 -0
- package/dist/hooks/load-katex-styles.svelte.d.ts +2 -0
- package/dist/hooks/load-katex-styles.svelte.d.ts.map +1 -0
- package/dist/hooks/register-frontend-tool.svelte.d.ts +3 -0
- package/dist/hooks/register-frontend-tool.svelte.d.ts.map +1 -0
- package/dist/hooks/register-human-in-the-loop.svelte.d.ts +3 -0
- package/dist/hooks/register-human-in-the-loop.svelte.d.ts.map +1 -0
- package/dist/hooks/register-render-tool-call.svelte.d.ts +30 -0
- package/dist/hooks/register-render-tool-call.svelte.d.ts.map +1 -0
- package/dist/index.cjs +5 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +6 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.mjs +3037 -0
- package/dist/index.mjs.map +1 -0
- package/dist/lib/svelte-core.d.ts +53 -0
- package/dist/lib/svelte-core.d.ts.map +1 -0
- package/dist/providers/CopilotKitProvider.svelte.d.ts +1 -0
- package/dist/providers/CopilotKitProvider.types.d.ts +47 -0
- package/dist/providers/CopilotKitProvider.types.d.ts.map +1 -0
- package/dist/providers/context.d.ts +19 -0
- package/dist/providers/context.d.ts.map +1 -0
- package/dist/providers/index.d.ts +6 -0
- package/dist/providers/index.d.ts.map +1 -0
- package/dist/providers/useCopilotKit.d.ts +3 -0
- package/dist/providers/useCopilotKit.d.ts.map +1 -0
- package/dist/styles.css +3 -0
- package/dist/types/a2ui.d.ts +5 -0
- package/dist/types/a2ui.d.ts.map +1 -0
- package/dist/types/activity-message-renderer.d.ts +17 -0
- package/dist/types/activity-message-renderer.d.ts.map +1 -0
- package/dist/types/custom-message-renderer.d.ts +18 -0
- package/dist/types/custom-message-renderer.d.ts.map +1 -0
- package/dist/types/frontend-tool.d.ts +6 -0
- package/dist/types/frontend-tool.d.ts.map +1 -0
- package/dist/types/human-in-the-loop.d.ts +31 -0
- package/dist/types/human-in-the-loop.d.ts.map +1 -0
- package/dist/types/index.d.ts +9 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/interrupt.d.ts +23 -0
- package/dist/types/interrupt.d.ts.map +1 -0
- package/dist/types/sandbox-function.d.ts +8 -0
- package/dist/types/sandbox-function.d.ts.map +1 -0
- package/dist/types/tool-call-renderer.d.ts +29 -0
- package/dist/types/tool-call-renderer.d.ts.map +1 -0
- package/package.json +114 -3
- package/src/app.d.ts +7 -0
- package/src/components/chat/CopilotChat.svelte +222 -0
- package/src/components/chat/CopilotChatAssistantMessage.svelte +295 -0
- package/src/components/chat/CopilotChatAttachmentQueue.svelte +241 -0
- package/src/components/chat/CopilotChatInput.svelte +192 -0
- package/src/components/chat/CopilotChatMessage.svelte +37 -0
- package/src/components/chat/CopilotChatMessageView.svelte +76 -0
- package/src/components/chat/CopilotChatReasoningMessage.svelte +210 -0
- package/src/components/chat/CopilotChatSuggestionPill.svelte +66 -0
- package/src/components/chat/CopilotChatSuggestionView.svelte +36 -0
- package/src/components/chat/CopilotChatToggleButton.svelte +47 -0
- package/src/components/chat/CopilotChatToolCallsView.svelte +187 -0
- package/src/components/chat/CopilotChatUserMessage.svelte +312 -0
- package/src/components/chat/CopilotChatView.svelte +177 -0
- package/src/components/chat/CopilotPopup.svelte +187 -0
- package/src/components/chat/CopilotSidebar.svelte +138 -0
- package/src/components/chat/StreamMarkdown.svelte +30 -0
- package/src/components/chat/chat-config-context.svelte.ts +28 -0
- package/src/components/chat/dispatch-after-active-run.ts +40 -0
- package/src/components/chat/markdown/CodeBlock.svelte +78 -0
- package/src/components/chat/markdown/CodeSpan.svelte +29 -0
- package/src/components/chat/markdown/Table.svelte +15 -0
- package/src/components/chat/markdown/TableBody.svelte +11 -0
- package/src/components/chat/markdown/TableCell.svelte +29 -0
- package/src/components/chat/markdown/TableHead.svelte +11 -0
- package/src/components/chat/markdown/TableRow.svelte +11 -0
- package/src/components/chat/markdown/highlight.ts +96 -0
- package/src/components/chat/media-parts.ts +34 -0
- package/src/components/chat/types.ts +176 -0
- package/src/components/icons/index.ts +23 -0
- package/src/components/index.ts +17 -0
- package/src/hooks/connect-agent-context.svelte.ts +46 -0
- package/src/hooks/create-agent.svelte.ts +420 -0
- package/src/hooks/create-attachments.svelte.ts +311 -0
- package/src/hooks/create-capabilities.svelte.ts +26 -0
- package/src/hooks/create-interrupt.svelte.ts +467 -0
- package/src/hooks/create-suggestions.svelte.ts +84 -0
- package/src/hooks/create-threads.svelte.ts +337 -0
- package/src/hooks/index.ts +45 -0
- package/src/hooks/load-katex-styles.svelte.ts +13 -0
- package/src/hooks/register-frontend-tool.svelte.ts +45 -0
- package/src/hooks/register-human-in-the-loop.svelte.ts +118 -0
- package/src/hooks/register-render-tool-call.svelte.ts +75 -0
- package/src/index.ts +6 -0
- package/src/lib/svelte-core.ts +230 -0
- package/src/providers/CopilotKitProvider.svelte +291 -0
- package/src/providers/CopilotKitProvider.types.ts +55 -0
- package/src/providers/context.ts +26 -0
- package/src/providers/index.ts +5 -0
- package/src/providers/useCopilotKit.ts +11 -0
- package/src/styles/globals.css +691 -0
- package/src/styles.d.ts +1 -0
- package/src/types/a2ui.ts +5 -0
- package/src/types/activity-message-renderer.ts +21 -0
- package/src/types/custom-message-renderer.ts +23 -0
- package/src/types/frontend-tool.ts +8 -0
- package/src/types/human-in-the-loop.ts +40 -0
- package/src/types/index.ts +8 -0
- package/src/types/interrupt.ts +32 -0
- package/src/types/sandbox-function.ts +8 -0
- package/src/types/tool-call-renderer.ts +36 -0
|
@@ -0,0 +1,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}
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Message } from "@ag-ui/core";
|
|
3
|
+
import type { Suggestion } from "@copilotkit/core";
|
|
4
|
+
import type { Attachment } from "@copilotkit/shared";
|
|
5
|
+
import type { CopilotChatInputMode, ToolsMenuItem, AutoScrollMode } from "./types";
|
|
6
|
+
import CopilotChatMessageView from "./CopilotChatMessageView.svelte";
|
|
7
|
+
import CopilotChatInput from "./CopilotChatInput.svelte";
|
|
8
|
+
import CopilotChatSuggestionView from "./CopilotChatSuggestionView.svelte";
|
|
9
|
+
|
|
10
|
+
let {
|
|
11
|
+
messages = [] as Message[],
|
|
12
|
+
isRunning = false,
|
|
13
|
+
welcomeScreen = false,
|
|
14
|
+
suggestions = [] as Suggestion[],
|
|
15
|
+
attachments = [] as Attachment[],
|
|
16
|
+
dragOver = false,
|
|
17
|
+
inputValue = "",
|
|
18
|
+
inputMode = "input" as CopilotChatInputMode,
|
|
19
|
+
inputToolsMenu = [] as (ToolsMenuItem | "-")[],
|
|
20
|
+
autoScroll = true as AutoScrollMode | boolean,
|
|
21
|
+
sentMessageId = null,
|
|
22
|
+
onSubmitMessage,
|
|
23
|
+
onStop,
|
|
24
|
+
onInputChange,
|
|
25
|
+
onSelectSuggestion,
|
|
26
|
+
onRemoveAttachment,
|
|
27
|
+
onAddFile,
|
|
28
|
+
onDragOver,
|
|
29
|
+
onDragLeave,
|
|
30
|
+
onDrop,
|
|
31
|
+
}: {
|
|
32
|
+
messages?: Message[];
|
|
33
|
+
isRunning?: boolean;
|
|
34
|
+
welcomeScreen?: boolean;
|
|
35
|
+
suggestions?: Suggestion[];
|
|
36
|
+
attachments?: Attachment[];
|
|
37
|
+
dragOver?: boolean;
|
|
38
|
+
inputValue?: string;
|
|
39
|
+
inputMode?: CopilotChatInputMode;
|
|
40
|
+
inputToolsMenu?: (ToolsMenuItem | "-")[];
|
|
41
|
+
isConnecting?: boolean;
|
|
42
|
+
hasExplicitThreadId?: boolean;
|
|
43
|
+
autoScroll?: AutoScrollMode | boolean;
|
|
44
|
+
sentMessageId?: string | null;
|
|
45
|
+
onSubmitMessage: (value: string) => void;
|
|
46
|
+
onStop?: () => void;
|
|
47
|
+
onInputChange: (value: string) => void;
|
|
48
|
+
onSelectSuggestion: (suggestion: Suggestion, index: number) => void;
|
|
49
|
+
onRemoveAttachment?: (id: string) => void;
|
|
50
|
+
onAddFile?: () => void;
|
|
51
|
+
onDragOver?: (event: DragEvent) => void;
|
|
52
|
+
onDragLeave?: (event: DragEvent) => void;
|
|
53
|
+
onDrop?: (event: DragEvent) => void;
|
|
54
|
+
} = $props();
|
|
55
|
+
|
|
56
|
+
let canAddFile = $derived(!!onAddFile);
|
|
57
|
+
</script>
|
|
58
|
+
|
|
59
|
+
<div
|
|
60
|
+
class="copilotkit-chat-view"
|
|
61
|
+
class:drag-over={dragOver}
|
|
62
|
+
role="presentation"
|
|
63
|
+
ondragover={onDragOver}
|
|
64
|
+
ondragleave={onDragLeave}
|
|
65
|
+
ondrop={onDrop}
|
|
66
|
+
>
|
|
67
|
+
{#if welcomeScreen && messages.length === 0}
|
|
68
|
+
<div class="copilotkit-welcome">
|
|
69
|
+
<div class="copilotkit-welcome-content">
|
|
70
|
+
<div class="copilotkit-welcome-icon">🤖</div>
|
|
71
|
+
<h2 class="copilotkit-welcome-title">How can I help you?</h2>
|
|
72
|
+
<p class="copilotkit-welcome-subtitle">Ask me anything to get started.</p>
|
|
73
|
+
</div>
|
|
74
|
+
{#if suggestions.length > 0}
|
|
75
|
+
<CopilotChatSuggestionView
|
|
76
|
+
{suggestions}
|
|
77
|
+
{onSelectSuggestion}
|
|
78
|
+
/>
|
|
79
|
+
{/if}
|
|
80
|
+
</div>
|
|
81
|
+
{:else}
|
|
82
|
+
<CopilotChatMessageView {messages} {isRunning} {autoScroll} {sentMessageId}>
|
|
83
|
+
{#snippet footer()}
|
|
84
|
+
{#if !isRunning && suggestions.length > 0}
|
|
85
|
+
<CopilotChatSuggestionView
|
|
86
|
+
{suggestions}
|
|
87
|
+
{onSelectSuggestion}
|
|
88
|
+
/>
|
|
89
|
+
{/if}
|
|
90
|
+
{/snippet}
|
|
91
|
+
</CopilotChatMessageView>
|
|
92
|
+
{/if}
|
|
93
|
+
<CopilotChatInput
|
|
94
|
+
value={inputValue}
|
|
95
|
+
{isRunning}
|
|
96
|
+
{inputMode}
|
|
97
|
+
toolsMenu={inputToolsMenu}
|
|
98
|
+
onSubmit={onSubmitMessage}
|
|
99
|
+
{onStop}
|
|
100
|
+
onInputChange={onInputChange}
|
|
101
|
+
{attachments}
|
|
102
|
+
{canAddFile}
|
|
103
|
+
{onAddFile}
|
|
104
|
+
{onRemoveAttachment}
|
|
105
|
+
placeholder={welcomeScreen && messages.length === 0
|
|
106
|
+
? "Type your message..."
|
|
107
|
+
: "Type a message..."}
|
|
108
|
+
/>
|
|
109
|
+
{#if dragOver}
|
|
110
|
+
<div class="copilotkit-drag-overlay" data-testid="copilot-drag-overlay">
|
|
111
|
+
Drop files to attach
|
|
112
|
+
</div>
|
|
113
|
+
{/if}
|
|
114
|
+
</div>
|
|
115
|
+
|
|
116
|
+
<style>
|
|
117
|
+
.copilotkit-chat-view {
|
|
118
|
+
display: flex;
|
|
119
|
+
flex-direction: column;
|
|
120
|
+
flex: 1;
|
|
121
|
+
min-height: 0;
|
|
122
|
+
overflow: hidden;
|
|
123
|
+
position: relative;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
.copilotkit-chat-view.drag-over {
|
|
127
|
+
outline: 2px dashed #3b82f6;
|
|
128
|
+
outline-offset: -2px;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
.copilotkit-drag-overlay {
|
|
132
|
+
position: absolute;
|
|
133
|
+
inset: 0;
|
|
134
|
+
display: flex;
|
|
135
|
+
align-items: center;
|
|
136
|
+
justify-content: center;
|
|
137
|
+
background: rgba(59, 130, 246, 0.08);
|
|
138
|
+
color: #1d4ed8;
|
|
139
|
+
font-size: 14px;
|
|
140
|
+
font-weight: 600;
|
|
141
|
+
pointer-events: none;
|
|
142
|
+
z-index: 5;
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
.copilotkit-welcome {
|
|
146
|
+
display: flex;
|
|
147
|
+
flex-direction: column;
|
|
148
|
+
align-items: center;
|
|
149
|
+
justify-content: center;
|
|
150
|
+
flex: 1;
|
|
151
|
+
min-height: 0;
|
|
152
|
+
padding: 24px;
|
|
153
|
+
gap: 16px;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
.copilotkit-welcome-content {
|
|
157
|
+
text-align: center;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
.copilotkit-welcome-icon {
|
|
161
|
+
font-size: 48px;
|
|
162
|
+
margin-bottom: 12px;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
.copilotkit-welcome-title {
|
|
166
|
+
font-size: 20px;
|
|
167
|
+
font-weight: 600;
|
|
168
|
+
color: #1f2937;
|
|
169
|
+
margin: 0;
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
.copilotkit-welcome-subtitle {
|
|
173
|
+
font-size: 14px;
|
|
174
|
+
color: #6b7280;
|
|
175
|
+
margin: 4px 0 0;
|
|
176
|
+
}
|
|
177
|
+
</style>
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { tick } from "svelte";
|
|
3
|
+
import CopilotChat from "./CopilotChat.svelte";
|
|
4
|
+
import CopilotChatToggleButton from "./CopilotChatToggleButton.svelte";
|
|
5
|
+
import type { CopilotPopupProps } from "./types";
|
|
6
|
+
|
|
7
|
+
let {
|
|
8
|
+
width = "400px",
|
|
9
|
+
height = "600px",
|
|
10
|
+
clickOutsideToClose = true,
|
|
11
|
+
defaultOpen = false,
|
|
12
|
+
...chatProps
|
|
13
|
+
}: CopilotPopupProps = $props();
|
|
14
|
+
|
|
15
|
+
// svelte-ignore state_referenced_locally
|
|
16
|
+
let isOpen = $state(defaultOpen);
|
|
17
|
+
let popupWidth = $derived(typeof width === "number" ? `${width}px` : width);
|
|
18
|
+
let popupHeight = $derived(typeof height === "number" ? `${height}px` : height);
|
|
19
|
+
|
|
20
|
+
let popupRef: HTMLElement | undefined;
|
|
21
|
+
let wrapperRef: HTMLElement | undefined;
|
|
22
|
+
|
|
23
|
+
function toggle() {
|
|
24
|
+
if (isOpen) {
|
|
25
|
+
close();
|
|
26
|
+
} else {
|
|
27
|
+
isOpen = true;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
function close() {
|
|
32
|
+
wrapperRef?.querySelector<HTMLButtonElement>(".copilotkit-toggle-btn")?.focus();
|
|
33
|
+
isOpen = false;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
function trapFocus(event: KeyboardEvent) {
|
|
37
|
+
if (event.key === "Escape") {
|
|
38
|
+
close();
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
if (event.key !== "Tab" || !popupRef) return;
|
|
42
|
+
const focusable = [...popupRef.querySelectorAll<HTMLElement>(
|
|
43
|
+
'button:not([disabled]), input:not([disabled]), textarea:not([disabled]), select:not([disabled]), a[href], [tabindex]:not([tabindex="-1"])',
|
|
44
|
+
)].filter((element) => !element.hasAttribute("hidden"));
|
|
45
|
+
if (focusable.length === 0) {
|
|
46
|
+
event.preventDefault();
|
|
47
|
+
popupRef.focus();
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
const first = focusable[0]!;
|
|
51
|
+
const last = focusable[focusable.length - 1]!;
|
|
52
|
+
if (!popupRef.contains(document.activeElement)) {
|
|
53
|
+
event.preventDefault();
|
|
54
|
+
(event.shiftKey ? last : first).focus();
|
|
55
|
+
} else if (event.shiftKey && document.activeElement === first) {
|
|
56
|
+
event.preventDefault();
|
|
57
|
+
last.focus();
|
|
58
|
+
} else if (!event.shiftKey && document.activeElement === last) {
|
|
59
|
+
event.preventDefault();
|
|
60
|
+
first.focus();
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
$effect(() => {
|
|
65
|
+
if (!isOpen) return;
|
|
66
|
+
void tick().then(() => {
|
|
67
|
+
const firstFocusable = popupRef?.querySelector<HTMLElement>(
|
|
68
|
+
'button:not([disabled]), input:not([disabled]), textarea:not([disabled]), select:not([disabled]), a[href], [tabindex]:not([tabindex="-1"])',
|
|
69
|
+
);
|
|
70
|
+
(firstFocusable ?? popupRef)?.focus();
|
|
71
|
+
});
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
function handleOverlayClick(e: MouseEvent) {
|
|
75
|
+
if (clickOutsideToClose && e.target === e.currentTarget) {
|
|
76
|
+
close();
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
function handleKeydown(e: KeyboardEvent) {
|
|
81
|
+
if (isOpen && e.key === "Escape") close();
|
|
82
|
+
}
|
|
83
|
+
</script>
|
|
84
|
+
|
|
85
|
+
<svelte:window onkeydown={handleKeydown} />
|
|
86
|
+
|
|
87
|
+
<div class="copilotkit-popup-wrapper" bind:this={wrapperRef}>
|
|
88
|
+
<CopilotChatToggleButton isOpen={isOpen} onclick={toggle} />
|
|
89
|
+
|
|
90
|
+
<div
|
|
91
|
+
class="copilotkit-popup-overlay"
|
|
92
|
+
class:is-closed={!isOpen}
|
|
93
|
+
role="presentation"
|
|
94
|
+
aria-hidden={isOpen ? undefined : true}
|
|
95
|
+
inert={!isOpen}
|
|
96
|
+
hidden={!isOpen}
|
|
97
|
+
onclick={handleOverlayClick}
|
|
98
|
+
onkeydown={(e) => e.key === "Escape" && close()}
|
|
99
|
+
>
|
|
100
|
+
<div
|
|
101
|
+
class="copilotkit-popup"
|
|
102
|
+
style="width: {popupWidth}; height: {popupHeight}"
|
|
103
|
+
role="dialog"
|
|
104
|
+
aria-modal="true"
|
|
105
|
+
aria-labelledby="copilot-popup-title"
|
|
106
|
+
tabindex="-1"
|
|
107
|
+
bind:this={popupRef}
|
|
108
|
+
onkeydown={trapFocus}
|
|
109
|
+
>
|
|
110
|
+
<div class="copilotkit-popup-header">
|
|
111
|
+
<h3 id="copilot-popup-title" class="copilotkit-popup-title">Copilot</h3>
|
|
112
|
+
<button type="button" class="copilotkit-popup-close" aria-label="Close chat" onclick={close}>✕</button>
|
|
113
|
+
</div>
|
|
114
|
+
<div class="copilotkit-popup-body">
|
|
115
|
+
<CopilotChat {...chatProps} />
|
|
116
|
+
</div>
|
|
117
|
+
</div>
|
|
118
|
+
</div>
|
|
119
|
+
</div>
|
|
120
|
+
|
|
121
|
+
<style>
|
|
122
|
+
.copilotkit-popup-wrapper {
|
|
123
|
+
position: relative;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
.copilotkit-popup-overlay {
|
|
127
|
+
position: fixed;
|
|
128
|
+
inset: 0;
|
|
129
|
+
background: rgba(0, 0, 0, 0.3);
|
|
130
|
+
display: flex;
|
|
131
|
+
align-items: flex-end;
|
|
132
|
+
justify-content: center;
|
|
133
|
+
z-index: 99;
|
|
134
|
+
padding: 16px;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
.copilotkit-popup-overlay.is-closed,
|
|
138
|
+
.copilotkit-popup-overlay[hidden] {
|
|
139
|
+
display: none !important;
|
|
140
|
+
visibility: hidden;
|
|
141
|
+
pointer-events: none;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
.copilotkit-popup {
|
|
145
|
+
background: #fff;
|
|
146
|
+
border-radius: 12px;
|
|
147
|
+
box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
|
|
148
|
+
display: flex;
|
|
149
|
+
flex-direction: column;
|
|
150
|
+
overflow: hidden;
|
|
151
|
+
max-height: 90vh;
|
|
152
|
+
max-width: 100vw;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
.copilotkit-popup-header {
|
|
156
|
+
display: flex;
|
|
157
|
+
align-items: center;
|
|
158
|
+
justify-content: space-between;
|
|
159
|
+
padding: 16px;
|
|
160
|
+
border-bottom: 1px solid #e5e7eb;
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
.copilotkit-popup-title {
|
|
164
|
+
margin: 0;
|
|
165
|
+
font-size: 16px;
|
|
166
|
+
font-weight: 600;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
.copilotkit-popup-close {
|
|
170
|
+
border: none;
|
|
171
|
+
background: none;
|
|
172
|
+
font-size: 18px;
|
|
173
|
+
cursor: pointer;
|
|
174
|
+
color: #6b7280;
|
|
175
|
+
padding: 4px;
|
|
176
|
+
border-radius: 4px;
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
.copilotkit-popup-close:hover {
|
|
180
|
+
background: #f3f4f6;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
.copilotkit-popup-body {
|
|
184
|
+
flex: 1;
|
|
185
|
+
overflow: hidden;
|
|
186
|
+
}
|
|
187
|
+
</style>
|