@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,138 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { tick } from "svelte";
|
|
3
|
+
import CopilotChat from "./CopilotChat.svelte";
|
|
4
|
+
import CopilotChatToggleButton from "./CopilotChatToggleButton.svelte";
|
|
5
|
+
import type { CopilotSidebarProps } from "./types";
|
|
6
|
+
|
|
7
|
+
let {
|
|
8
|
+
width = "400px",
|
|
9
|
+
defaultOpen = false,
|
|
10
|
+
...chatProps
|
|
11
|
+
}: CopilotSidebarProps = $props();
|
|
12
|
+
|
|
13
|
+
// svelte-ignore state_referenced_locally
|
|
14
|
+
let isOpen = $state(defaultOpen);
|
|
15
|
+
let sidebarWidth = $derived(typeof width === "number" ? `${width}px` : width);
|
|
16
|
+
let wrapperRef: HTMLElement | undefined;
|
|
17
|
+
let sidebarRef: HTMLElement | undefined;
|
|
18
|
+
|
|
19
|
+
function toggle() {
|
|
20
|
+
isOpen = !isOpen;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
function close() {
|
|
24
|
+
const restoreFocus = !!sidebarRef?.contains(document.activeElement);
|
|
25
|
+
isOpen = false;
|
|
26
|
+
if (restoreFocus) {
|
|
27
|
+
void tick().then(() => {
|
|
28
|
+
wrapperRef?.querySelector<HTMLButtonElement>(".copilotkit-toggle-btn")?.focus();
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
function handleKeydown(e: KeyboardEvent) {
|
|
34
|
+
if (isOpen && e.key === "Escape") close();
|
|
35
|
+
}
|
|
36
|
+
</script>
|
|
37
|
+
|
|
38
|
+
<svelte:window onkeydown={handleKeydown} />
|
|
39
|
+
|
|
40
|
+
<div class="copilotkit-sidebar-wrapper" bind:this={wrapperRef}>
|
|
41
|
+
<CopilotChatToggleButton isOpen={isOpen} onclick={toggle} />
|
|
42
|
+
|
|
43
|
+
<div
|
|
44
|
+
class="copilotkit-sidebar-overlay"
|
|
45
|
+
class:is-closed={!isOpen}
|
|
46
|
+
role="presentation"
|
|
47
|
+
aria-hidden={isOpen ? undefined : true}
|
|
48
|
+
inert={!isOpen}
|
|
49
|
+
hidden={!isOpen}
|
|
50
|
+
onclick={close}
|
|
51
|
+
onkeydown={(e) => e.key === "Escape" && close()}
|
|
52
|
+
></div>
|
|
53
|
+
<div
|
|
54
|
+
class="copilotkit-sidebar"
|
|
55
|
+
class:is-closed={!isOpen}
|
|
56
|
+
style="width: {sidebarWidth}"
|
|
57
|
+
aria-hidden={isOpen ? undefined : true}
|
|
58
|
+
inert={!isOpen}
|
|
59
|
+
hidden={!isOpen}
|
|
60
|
+
bind:this={sidebarRef}
|
|
61
|
+
>
|
|
62
|
+
<div class="copilotkit-sidebar-header">
|
|
63
|
+
<h3 class="copilotkit-sidebar-title">Copilot</h3>
|
|
64
|
+
<button type="button" class="copilotkit-sidebar-close" aria-label="Close chat" onclick={close}>✕</button>
|
|
65
|
+
</div>
|
|
66
|
+
<div class="copilotkit-sidebar-body">
|
|
67
|
+
<CopilotChat {...chatProps} />
|
|
68
|
+
</div>
|
|
69
|
+
</div>
|
|
70
|
+
</div>
|
|
71
|
+
|
|
72
|
+
<style>
|
|
73
|
+
.copilotkit-sidebar-wrapper {
|
|
74
|
+
position: relative;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
.copilotkit-sidebar-overlay {
|
|
78
|
+
position: fixed;
|
|
79
|
+
inset: 0;
|
|
80
|
+
background: rgba(0, 0, 0, 0.3);
|
|
81
|
+
z-index: 99;
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
.copilotkit-sidebar-overlay.is-closed,
|
|
85
|
+
.copilotkit-sidebar-overlay[hidden],
|
|
86
|
+
.copilotkit-sidebar.is-closed,
|
|
87
|
+
.copilotkit-sidebar[hidden] {
|
|
88
|
+
display: none !important;
|
|
89
|
+
visibility: hidden;
|
|
90
|
+
pointer-events: none;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
.copilotkit-sidebar {
|
|
94
|
+
position: fixed;
|
|
95
|
+
top: 0;
|
|
96
|
+
right: 0;
|
|
97
|
+
height: 100%;
|
|
98
|
+
background: #fff;
|
|
99
|
+
box-shadow: -4px 0 12px rgba(0, 0, 0, 0.1);
|
|
100
|
+
z-index: 100;
|
|
101
|
+
display: flex;
|
|
102
|
+
flex-direction: column;
|
|
103
|
+
max-width: 100vw;
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
.copilotkit-sidebar-header {
|
|
107
|
+
display: flex;
|
|
108
|
+
align-items: center;
|
|
109
|
+
justify-content: space-between;
|
|
110
|
+
padding: 16px;
|
|
111
|
+
border-bottom: 1px solid #e5e7eb;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
.copilotkit-sidebar-title {
|
|
115
|
+
margin: 0;
|
|
116
|
+
font-size: 16px;
|
|
117
|
+
font-weight: 600;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
.copilotkit-sidebar-close {
|
|
121
|
+
border: none;
|
|
122
|
+
background: none;
|
|
123
|
+
font-size: 20px;
|
|
124
|
+
cursor: pointer;
|
|
125
|
+
color: #6b7280;
|
|
126
|
+
padding: 4px;
|
|
127
|
+
border-radius: 4px;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
.copilotkit-sidebar-close:hover {
|
|
131
|
+
background: #f3f4f6;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
.copilotkit-sidebar-body {
|
|
135
|
+
flex: 1;
|
|
136
|
+
overflow: hidden;
|
|
137
|
+
}
|
|
138
|
+
</style>
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import SvelteMarkdown from "@humanspeak/svelte-markdown";
|
|
3
|
+
import CodeBlock from "./markdown/CodeBlock.svelte";
|
|
4
|
+
import CodeSpan from "./markdown/CodeSpan.svelte";
|
|
5
|
+
import Table from "./markdown/Table.svelte";
|
|
6
|
+
import TableHead from "./markdown/TableHead.svelte";
|
|
7
|
+
import TableBody from "./markdown/TableBody.svelte";
|
|
8
|
+
import TableRow from "./markdown/TableRow.svelte";
|
|
9
|
+
import TableCell from "./markdown/TableCell.svelte";
|
|
10
|
+
|
|
11
|
+
let {
|
|
12
|
+
content = "",
|
|
13
|
+
}: {
|
|
14
|
+
content?: string;
|
|
15
|
+
} = $props();
|
|
16
|
+
|
|
17
|
+
// Renderers emit the same `data-streamdown` attributes as the React package so
|
|
18
|
+
// the shared stylesheet in `globals.css` styles both implementations alike.
|
|
19
|
+
const renderers = {
|
|
20
|
+
code: CodeBlock,
|
|
21
|
+
codespan: CodeSpan,
|
|
22
|
+
table: Table,
|
|
23
|
+
tablehead: TableHead,
|
|
24
|
+
tablebody: TableBody,
|
|
25
|
+
tablerow: TableRow,
|
|
26
|
+
tablecell: TableCell,
|
|
27
|
+
};
|
|
28
|
+
</script>
|
|
29
|
+
|
|
30
|
+
<SvelteMarkdown source={content} streaming={true} {renderers} />
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { setContext, getContext } from "svelte";
|
|
2
|
+
import { COPILOT_CHAT_CONFIG_KEY } from "../../providers/context";
|
|
3
|
+
|
|
4
|
+
export class ChatConfig {
|
|
5
|
+
agentId: string;
|
|
6
|
+
threadId: string | undefined;
|
|
7
|
+
hasExplicitThreadId: boolean;
|
|
8
|
+
labels: Record<string, string>;
|
|
9
|
+
|
|
10
|
+
constructor(
|
|
11
|
+
agentId: string,
|
|
12
|
+
threadId?: string,
|
|
13
|
+
hasExplicitThreadId?: boolean,
|
|
14
|
+
) {
|
|
15
|
+
this.agentId = agentId;
|
|
16
|
+
this.threadId = threadId;
|
|
17
|
+
this.hasExplicitThreadId = hasExplicitThreadId ?? false;
|
|
18
|
+
this.labels = {};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function setChatConfig(config: ChatConfig): void {
|
|
23
|
+
setContext(COPILOT_CHAT_CONFIG_KEY, config);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function getChatConfig(): ChatConfig | undefined {
|
|
27
|
+
return getContext<ChatConfig | undefined>(COPILOT_CHAT_CONFIG_KEY);
|
|
28
|
+
}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { AbstractAgent } from "@ag-ui/client";
|
|
2
|
+
import { isRunCompletionAware } from "@copilotkit/core";
|
|
3
|
+
|
|
4
|
+
const dispatchQueues = new WeakMap<AbstractAgent, Promise<void>>();
|
|
5
|
+
|
|
6
|
+
export async function dispatchAfterActiveRunSettles(
|
|
7
|
+
agent: AbstractAgent,
|
|
8
|
+
dispatch: () => Promise<void>,
|
|
9
|
+
): Promise<void> {
|
|
10
|
+
const previous = dispatchQueues.get(agent) ?? Promise.resolve();
|
|
11
|
+
const current = previous
|
|
12
|
+
.catch(() => {})
|
|
13
|
+
.then(async () => {
|
|
14
|
+
const maybeAware: unknown = agent;
|
|
15
|
+
const activeRunCompletionPromise = isRunCompletionAware(maybeAware)
|
|
16
|
+
? maybeAware.activeRunCompletionPromise
|
|
17
|
+
: undefined;
|
|
18
|
+
|
|
19
|
+
if (agent.isRunning && activeRunCompletionPromise) {
|
|
20
|
+
try {
|
|
21
|
+
await activeRunCompletionPromise;
|
|
22
|
+
} catch (error) {
|
|
23
|
+
// Let the queued message proceed, but make a failed run observable.
|
|
24
|
+
console.error(
|
|
25
|
+
"CopilotChat: in-flight run rejected while queuing send",
|
|
26
|
+
error,
|
|
27
|
+
);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
await dispatch();
|
|
32
|
+
});
|
|
33
|
+
dispatchQueues.set(agent, current);
|
|
34
|
+
|
|
35
|
+
try {
|
|
36
|
+
await current;
|
|
37
|
+
} finally {
|
|
38
|
+
if (dispatchQueues.get(agent) === current) dispatchQueues.delete(agent);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import { getDownloadFilename, getLanguageLabel, highlightCode } from "./highlight";
|
|
3
|
+
import { IconCheck, IconCopy, IconDownload } from "../../icons";
|
|
4
|
+
|
|
5
|
+
let {
|
|
6
|
+
lang = "",
|
|
7
|
+
text = "",
|
|
8
|
+
}: {
|
|
9
|
+
lang?: string;
|
|
10
|
+
text?: string;
|
|
11
|
+
} = $props();
|
|
12
|
+
|
|
13
|
+
let copied = $state(false);
|
|
14
|
+
let copiedTimeout: ReturnType<typeof setTimeout> | null = null;
|
|
15
|
+
|
|
16
|
+
const language = $derived(getLanguageLabel(lang));
|
|
17
|
+
const highlighted = $derived(highlightCode(text, lang));
|
|
18
|
+
|
|
19
|
+
async function copyCode() {
|
|
20
|
+
try {
|
|
21
|
+
await navigator.clipboard.writeText(text);
|
|
22
|
+
copied = true;
|
|
23
|
+
if (copiedTimeout) clearTimeout(copiedTimeout);
|
|
24
|
+
copiedTimeout = setTimeout(() => {
|
|
25
|
+
copied = false;
|
|
26
|
+
}, 2000);
|
|
27
|
+
} catch {
|
|
28
|
+
copied = false;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
function downloadCode() {
|
|
33
|
+
if (typeof document === "undefined") return;
|
|
34
|
+
const blob = new Blob([text], { type: "text/plain;charset=utf-8" });
|
|
35
|
+
const url = URL.createObjectURL(blob);
|
|
36
|
+
const anchor = document.createElement("a");
|
|
37
|
+
anchor.href = url;
|
|
38
|
+
anchor.download = getDownloadFilename(lang);
|
|
39
|
+
document.body.appendChild(anchor);
|
|
40
|
+
anchor.click();
|
|
41
|
+
anchor.remove();
|
|
42
|
+
URL.revokeObjectURL(url);
|
|
43
|
+
}
|
|
44
|
+
</script>
|
|
45
|
+
|
|
46
|
+
<div data-streamdown="code-block">
|
|
47
|
+
<div data-streamdown="code-block-header">
|
|
48
|
+
<span data-testid="copilot-code-block-language">{language}</span>
|
|
49
|
+
<div class="cpk:flex cpk:items-center cpk:gap-1">
|
|
50
|
+
<button
|
|
51
|
+
type="button"
|
|
52
|
+
data-streamdown="code-block-copy-button"
|
|
53
|
+
data-testid="copilot-code-block-copy"
|
|
54
|
+
aria-label={copied ? "Copied" : "Copy code"}
|
|
55
|
+
title={copied ? "Copied" : "Copy code"}
|
|
56
|
+
onclick={copyCode}
|
|
57
|
+
>
|
|
58
|
+
{#if copied}
|
|
59
|
+
<IconCheck class="cpk:size-4" />
|
|
60
|
+
{:else}
|
|
61
|
+
<IconCopy class="cpk:size-4" />
|
|
62
|
+
{/if}
|
|
63
|
+
</button>
|
|
64
|
+
<button
|
|
65
|
+
type="button"
|
|
66
|
+
data-streamdown="code-block-download-button"
|
|
67
|
+
data-testid="copilot-code-block-download"
|
|
68
|
+
aria-label="Download code"
|
|
69
|
+
title="Download code"
|
|
70
|
+
onclick={downloadCode}
|
|
71
|
+
>
|
|
72
|
+
<IconDownload class="cpk:size-4" />
|
|
73
|
+
</button>
|
|
74
|
+
</div>
|
|
75
|
+
</div>
|
|
76
|
+
<!-- eslint-disable-next-line svelte/no-at-html-tags -- highlight.js escapes the source before wrapping tokens -->
|
|
77
|
+
<pre data-streamdown="code-block-body"><code class="hljs">{@html highlighted}</code></pre>
|
|
78
|
+
</div>
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
let {
|
|
3
|
+
text,
|
|
4
|
+
raw,
|
|
5
|
+
children,
|
|
6
|
+
}: {
|
|
7
|
+
text?: string;
|
|
8
|
+
raw?: string;
|
|
9
|
+
children?: import("svelte").Snippet;
|
|
10
|
+
} = $props();
|
|
11
|
+
|
|
12
|
+
// Some markdown parsers hand over the raw token including the fence
|
|
13
|
+
// backticks. Trim them so inline code renders as plain text.
|
|
14
|
+
function stripFence(value: string): string {
|
|
15
|
+
return value.replace(/^`+/, "").replace(/`+$/, "");
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
const content = $derived(
|
|
19
|
+
typeof text === "string" ? stripFence(text) : stripFence(raw ?? ""),
|
|
20
|
+
);
|
|
21
|
+
</script>
|
|
22
|
+
|
|
23
|
+
<code data-streamdown="inline-code">
|
|
24
|
+
{#if typeof text === "string" || raw !== undefined}
|
|
25
|
+
{content}
|
|
26
|
+
{:else if children}
|
|
27
|
+
{@render children()}
|
|
28
|
+
{/if}
|
|
29
|
+
</code>
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from "svelte";
|
|
3
|
+
|
|
4
|
+
let { children }: { children?: Snippet } = $props();
|
|
5
|
+
</script>
|
|
6
|
+
|
|
7
|
+
<div data-streamdown="table-wrapper">
|
|
8
|
+
<div class="cpk:overflow-x-auto">
|
|
9
|
+
<table data-streamdown="table">
|
|
10
|
+
{#if children}
|
|
11
|
+
{@render children()}
|
|
12
|
+
{/if}
|
|
13
|
+
</table>
|
|
14
|
+
</div>
|
|
15
|
+
</div>
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
<script lang="ts">
|
|
2
|
+
import type { Snippet } from "svelte";
|
|
3
|
+
|
|
4
|
+
let {
|
|
5
|
+
header = false,
|
|
6
|
+
align = null,
|
|
7
|
+
children,
|
|
8
|
+
}: {
|
|
9
|
+
header?: boolean;
|
|
10
|
+
align?: "left" | "center" | "right" | null;
|
|
11
|
+
children?: Snippet;
|
|
12
|
+
} = $props();
|
|
13
|
+
|
|
14
|
+
const style = $derived(align ? `text-align: ${align};` : undefined);
|
|
15
|
+
</script>
|
|
16
|
+
|
|
17
|
+
{#if header}
|
|
18
|
+
<th data-streamdown="table-header-cell" {style}>
|
|
19
|
+
{#if children}
|
|
20
|
+
{@render children()}
|
|
21
|
+
{/if}
|
|
22
|
+
</th>
|
|
23
|
+
{:else}
|
|
24
|
+
<td data-streamdown="table-cell" {style}>
|
|
25
|
+
{#if children}
|
|
26
|
+
{@render children()}
|
|
27
|
+
{/if}
|
|
28
|
+
</td>
|
|
29
|
+
{/if}
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import hljs from "highlight.js";
|
|
2
|
+
|
|
3
|
+
const LANGUAGE_ALIASES: Record<string, string> = {
|
|
4
|
+
js: "javascript",
|
|
5
|
+
jsx: "javascript",
|
|
6
|
+
mjs: "javascript",
|
|
7
|
+
cjs: "javascript",
|
|
8
|
+
ts: "typescript",
|
|
9
|
+
tsx: "typescript",
|
|
10
|
+
sh: "bash",
|
|
11
|
+
shell: "bash",
|
|
12
|
+
zsh: "bash",
|
|
13
|
+
yml: "yaml",
|
|
14
|
+
md: "markdown",
|
|
15
|
+
py: "python",
|
|
16
|
+
rb: "ruby",
|
|
17
|
+
rs: "rust",
|
|
18
|
+
"c++": "cpp",
|
|
19
|
+
cs: "csharp",
|
|
20
|
+
"c#": "csharp",
|
|
21
|
+
html: "xml",
|
|
22
|
+
svg: "xml",
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
const EXTENSION_BY_LANGUAGE: Record<string, string> = {
|
|
26
|
+
bash: "sh",
|
|
27
|
+
c: "c",
|
|
28
|
+
cpp: "cpp",
|
|
29
|
+
csharp: "cs",
|
|
30
|
+
css: "css",
|
|
31
|
+
diff: "diff",
|
|
32
|
+
go: "go",
|
|
33
|
+
graphql: "graphql",
|
|
34
|
+
java: "java",
|
|
35
|
+
javascript: "js",
|
|
36
|
+
json: "json",
|
|
37
|
+
kotlin: "kt",
|
|
38
|
+
less: "less",
|
|
39
|
+
lua: "lua",
|
|
40
|
+
markdown: "md",
|
|
41
|
+
php: "php",
|
|
42
|
+
plaintext: "txt",
|
|
43
|
+
python: "py",
|
|
44
|
+
ruby: "rb",
|
|
45
|
+
rust: "rs",
|
|
46
|
+
scss: "scss",
|
|
47
|
+
sql: "sql",
|
|
48
|
+
swift: "swift",
|
|
49
|
+
typescript: "ts",
|
|
50
|
+
xml: "xml",
|
|
51
|
+
yaml: "yml",
|
|
52
|
+
};
|
|
53
|
+
|
|
54
|
+
function normalizeLanguage(lang: string | undefined): string | undefined {
|
|
55
|
+
const cleaned = lang?.trim().toLowerCase().split(/\s+/)[0];
|
|
56
|
+
if (!cleaned) return undefined;
|
|
57
|
+
return LANGUAGE_ALIASES[cleaned] ?? cleaned;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function escapeHtml(value: string): string {
|
|
61
|
+
return value
|
|
62
|
+
.replace(/&/g, "&")
|
|
63
|
+
.replace(/</g, "<")
|
|
64
|
+
.replace(/>/g, ">")
|
|
65
|
+
.replace(/"/g, """)
|
|
66
|
+
.replace(/'/g, "'");
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/**
|
|
70
|
+
* Highlights a fenced code block. Unknown languages fall back to automatic
|
|
71
|
+
* detection so highlighting still works when the fence is missing a language.
|
|
72
|
+
*/
|
|
73
|
+
export function highlightCode(code: string, lang?: string): string {
|
|
74
|
+
const language = normalizeLanguage(lang);
|
|
75
|
+
try {
|
|
76
|
+
if (language && hljs.getLanguage(language)) {
|
|
77
|
+
return hljs.highlight(code, { language, ignoreIllegals: true }).value;
|
|
78
|
+
}
|
|
79
|
+
if (code.trim().length > 0) {
|
|
80
|
+
return hljs.highlightAuto(code).value;
|
|
81
|
+
}
|
|
82
|
+
} catch {
|
|
83
|
+
// Fall through to escaped plain text on any highlighter failure.
|
|
84
|
+
}
|
|
85
|
+
return escapeHtml(code);
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export function getLanguageLabel(lang?: string): string {
|
|
89
|
+
return normalizeLanguage(lang) ?? "text";
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
export function getDownloadFilename(lang?: string): string {
|
|
93
|
+
const language = normalizeLanguage(lang);
|
|
94
|
+
const extension = (language && EXTENSION_BY_LANGUAGE[language]) || "txt";
|
|
95
|
+
return `code.${extension}`;
|
|
96
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import type { UserMessage } from "@ag-ui/core";
|
|
2
|
+
import type {
|
|
3
|
+
AudioInputPart,
|
|
4
|
+
DocumentInputPart,
|
|
5
|
+
ImageInputPart,
|
|
6
|
+
VideoInputPart,
|
|
7
|
+
} from "@copilotkit/shared";
|
|
8
|
+
|
|
9
|
+
export type MediaPart =
|
|
10
|
+
| ImageInputPart
|
|
11
|
+
| AudioInputPart
|
|
12
|
+
| VideoInputPart
|
|
13
|
+
| DocumentInputPart;
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Keeps only the non-text parts of a user message. Mirrors the React package so
|
|
17
|
+
* sent images, audio, video and documents render above the message bubble
|
|
18
|
+
* instead of being dropped.
|
|
19
|
+
*/
|
|
20
|
+
export function getMediaParts(content: UserMessage["content"]): MediaPart[] {
|
|
21
|
+
if (!content || typeof content === "string") return [];
|
|
22
|
+
return content.filter(
|
|
23
|
+
(part): part is MediaPart =>
|
|
24
|
+
part.type === "image" ||
|
|
25
|
+
part.type === "audio" ||
|
|
26
|
+
part.type === "video" ||
|
|
27
|
+
part.type === "document",
|
|
28
|
+
);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function getMediaPartFilename(part: MediaPart): string | undefined {
|
|
32
|
+
const filename = part.metadata?.filename;
|
|
33
|
+
return typeof filename === "string" ? filename : undefined;
|
|
34
|
+
}
|