@copilotkit/vue 2.0.0-rc.7 → 2.0.0-rc.8

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 (124) hide show
  1. package/dist/index.cjs +1 -73
  2. package/dist/index.cjs.map +1 -1
  3. package/dist/index.mjs +292 -16947
  4. package/dist/index.mjs.map +1 -1
  5. package/dist/use-render-activity-message-C5LCG9a9.js +16451 -0
  6. package/dist/use-render-activity-message-C5LCG9a9.js.map +1 -0
  7. package/dist/use-render-activity-message-CPzguixR.cjs +74 -0
  8. package/dist/use-render-activity-message-CPzguixR.cjs.map +1 -0
  9. package/dist/v2/components/LicenseWarningBanner.vue.d.ts.map +1 -1
  10. package/dist/v2/components/chat/CopilotChat.vue.d.ts.map +1 -1
  11. package/dist/v2/components/chat/CopilotChatAttachmentQueue.vue.d.ts.map +1 -1
  12. package/dist/v2/components/chat/CopilotChatAttachmentRenderer.vue.d.ts.map +1 -1
  13. package/dist/v2/components/chat/CopilotChatInput.vue.d.ts +6 -270
  14. package/dist/v2/components/chat/CopilotChatInput.vue.d.ts.map +1 -1
  15. package/dist/v2/components/chat/CopilotChatMessageView.vue.d.ts +1 -1
  16. package/dist/v2/components/chat/CopilotChatMessageView.vue.d.ts.map +1 -1
  17. package/dist/v2/components/chat/CopilotChatReasoningMessage.vue.d.ts.map +1 -1
  18. package/dist/v2/components/chat/CopilotChatToolCallGroup.vue.d.ts +1 -1
  19. package/dist/v2/components/chat/CopilotChatToolCallGroup.vue.d.ts.map +1 -1
  20. package/dist/v2/components/chat/CopilotChatToolCallItem.vue.d.ts.map +1 -1
  21. package/dist/v2/components/chat/CopilotChatToolCallsView.vue.d.ts +1 -1
  22. package/dist/v2/components/chat/CopilotChatToolCallsView.vue.d.ts.map +1 -1
  23. package/dist/v2/components/chat/CopilotChatView.vue.d.ts.map +1 -1
  24. package/dist/v2/components/chat/CopilotChatWorkSummary.vue.d.ts.map +1 -1
  25. package/dist/v2/components/chat/copilotChatToolCallItemRender.d.ts +0 -1
  26. package/dist/v2/components/chat/copilotChatToolCallItemRender.d.ts.map +1 -1
  27. package/dist/v2/components/chat/index.d.ts +0 -1
  28. package/dist/v2/components/chat/index.d.ts.map +1 -1
  29. package/dist/v2/components/chat/tool-call-display-context.d.ts +2 -19
  30. package/dist/v2/components/chat/tool-call-display-context.d.ts.map +1 -1
  31. package/dist/v2/components/chat/tool-call-group-parts.d.ts +7 -20
  32. package/dist/v2/components/chat/tool-call-group-parts.d.ts.map +1 -1
  33. package/dist/v2/components/chat/turn-work.d.ts +0 -2
  34. package/dist/v2/components/chat/turn-work.d.ts.map +1 -1
  35. package/dist/v2/components/chat/types.d.ts +1 -2
  36. package/dist/v2/components/chat/types.d.ts.map +1 -1
  37. package/dist/v2/components/chat/use-chat-labels.d.ts +8 -0
  38. package/dist/v2/components/chat/use-chat-labels.d.ts.map +1 -0
  39. package/dist/v2/components/icons/index.d.ts +968 -1
  40. package/dist/v2/components/icons/index.d.ts.map +1 -1
  41. package/dist/v2/hooks/use-frontend-tool.d.ts +5 -0
  42. package/dist/v2/hooks/use-frontend-tool.d.ts.map +1 -1
  43. package/dist/v2/hooks/use-render-activity-message.d.ts +13 -9
  44. package/dist/v2/hooks/use-render-activity-message.d.ts.map +1 -1
  45. package/dist/v2/hooks/use-render-custom-messages.d.ts +5 -7
  46. package/dist/v2/hooks/use-render-custom-messages.d.ts.map +1 -1
  47. package/dist/v2/index.cjs +1 -1
  48. package/dist/v2/index.mjs +196 -199
  49. package/dist/v2/index.mjs.map +1 -1
  50. package/dist/v2/lib/vue-core.d.ts +0 -1
  51. package/dist/v2/lib/vue-core.d.ts.map +1 -1
  52. package/dist/v2/providers/CopilotKitProvider.vue.d.ts.map +1 -1
  53. package/dist/v2/providers/types.d.ts +3 -46
  54. package/dist/v2/providers/types.d.ts.map +1 -1
  55. package/dist/v2/types/human-in-the-loop.d.ts +8 -29
  56. package/dist/v2/types/human-in-the-loop.d.ts.map +1 -1
  57. package/dist/v2/types/interrupt.d.ts +1 -22
  58. package/dist/v2/types/interrupt.d.ts.map +1 -1
  59. package/dist/v2/types/vue-activity-message-renderer.d.ts +16 -15
  60. package/dist/v2/types/vue-activity-message-renderer.d.ts.map +1 -1
  61. package/dist/v2/types/vue-custom-message-renderer.d.ts +7 -16
  62. package/dist/v2/types/vue-custom-message-renderer.d.ts.map +1 -1
  63. package/dist/v2/types/vue-tool-call-renderer.d.ts +16 -35
  64. package/dist/v2/types/vue-tool-call-renderer.d.ts.map +1 -1
  65. package/package.json +7 -8
  66. package/src/__tests__/build-entries.test.ts +27 -0
  67. package/src/v2/components/LicenseWarningBanner.vue +31 -15
  68. package/src/v2/components/__tests__/LicenseWarningBanner.test.ts +20 -0
  69. package/src/v2/components/chat/CopilotChat.vue +19 -40
  70. package/src/v2/components/chat/CopilotChatAttachmentQueue.vue +14 -10
  71. package/src/v2/components/chat/CopilotChatAttachmentRenderer.vue +14 -6
  72. package/src/v2/components/chat/CopilotChatInput.vue +40 -30
  73. package/src/v2/components/chat/CopilotChatMessageView.vue +72 -140
  74. package/src/v2/components/chat/CopilotChatReasoningMessage.vue +31 -13
  75. package/src/v2/components/chat/CopilotChatToolCallGroup.vue +60 -37
  76. package/src/v2/components/chat/CopilotChatToolCallItem.vue +3 -3
  77. package/src/v2/components/chat/CopilotChatToolCallsView.vue +15 -13
  78. package/src/v2/components/chat/CopilotChatView.vue +5 -5
  79. package/src/v2/components/chat/CopilotChatWorkSummary.vue +44 -18
  80. package/src/v2/components/chat/__tests__/CopilotChat.labels.test.ts +19 -4
  81. package/src/v2/components/chat/__tests__/CopilotChatInput.test.ts +33 -1
  82. package/src/v2/components/chat/__tests__/CopilotChatMessageView.test.ts +247 -403
  83. package/src/v2/components/chat/__tests__/CopilotChatMessageView.toolCallDisplay.test.ts +1 -99
  84. package/src/v2/components/chat/copilotChatToolCallItemRender.ts +3 -19
  85. package/src/v2/components/chat/index.ts +0 -1
  86. package/src/v2/components/chat/tool-call-display-context.ts +2 -23
  87. package/src/v2/components/chat/tool-call-group-parts.ts +44 -85
  88. package/src/v2/components/chat/turn-work.ts +0 -9
  89. package/src/v2/components/chat/types.ts +4 -4
  90. package/src/v2/components/chat/use-chat-labels.ts +16 -0
  91. package/src/v2/components/icons/__tests__/icons.test.ts +72 -68
  92. package/src/v2/components/icons/index.ts +109 -27
  93. package/src/v2/hooks/__tests__/use-frontend-tool.e2e.test.ts +62 -1
  94. package/src/v2/hooks/__tests__/use-render-activity-message.test.ts +105 -0
  95. package/src/v2/hooks/__tests__/use-render-tool.test.ts +3 -6
  96. package/src/v2/hooks/__tests__/zod-regression.test.ts +4 -3
  97. package/src/v2/hooks/use-frontend-tool.ts +10 -10
  98. package/src/v2/hooks/use-render-activity-message.ts +27 -48
  99. package/src/v2/hooks/use-render-custom-messages.ts +14 -35
  100. package/src/v2/lib/vue-core.ts +3 -5
  101. package/src/v2/providers/CopilotKitProvider.vue +29 -80
  102. package/src/v2/providers/__tests__/CopilotKitProvider.headersAtSendTime.test.ts +3 -3
  103. package/src/v2/providers/__tests__/CopilotKitProvider.learning.test.ts +3 -1
  104. package/src/v2/providers/__tests__/CopilotKitProvider.test.ts +17 -0
  105. package/src/v2/providers/types.ts +3 -49
  106. package/src/v2/types/__tests__/defineToolCallRenderer.test.ts +28 -33
  107. package/src/v2/types/defineToolCallRenderer.ts +5 -4
  108. package/src/v2/types/human-in-the-loop.ts +8 -32
  109. package/src/v2/types/interrupt.ts +7 -31
  110. package/src/v2/types/vue-activity-message-renderer.ts +24 -15
  111. package/src/v2/types/vue-custom-message-renderer.ts +12 -16
  112. package/src/v2/types/vue-tool-call-renderer.ts +23 -38
  113. package/vite.config.ts +1 -2
  114. package/dist/v2/components/chat/BasicMarkdown.vue.d.ts +0 -9
  115. package/dist/v2/components/chat/BasicMarkdown.vue.d.ts.map +0 -1
  116. package/dist/v2/components/chat/tool-call-layout.d.ts +0 -115
  117. package/dist/v2/components/chat/tool-call-layout.d.ts.map +0 -1
  118. package/dist/v2/lib/transcription-client.d.ts +0 -19
  119. package/dist/v2/lib/transcription-client.d.ts.map +0 -1
  120. package/src/v2/components/chat/BasicMarkdown.vue +0 -160
  121. package/src/v2/components/chat/__tests__/BasicMarkdown.test.ts +0 -69
  122. package/src/v2/components/chat/tool-call-layout.ts +0 -304
  123. package/src/v2/lib/__tests__/transcription-client.test.ts +0 -67
  124. package/src/v2/lib/transcription-client.ts +0 -151
package/vite.config.ts CHANGED
@@ -13,7 +13,7 @@ export default defineConfig({
13
13
  lib: {
14
14
  entry: {
15
15
  index: resolve(__dirname, "src/index.ts"),
16
- "v2/index": resolve(__dirname, "src/index.ts"),
16
+ "v2/index": resolve(__dirname, "src/v2/index.ts"),
17
17
  },
18
18
  name: "CopilotKitVue",
19
19
  formats: ["es", "cjs"],
@@ -47,7 +47,6 @@ export default defineConfig({
47
47
  // declared") and risks double custom-element registration.
48
48
  /^@copilotkit\/web-components(\/.*)?$/,
49
49
  "@jetbrains/websandbox",
50
- "lucide-vue-next",
51
50
  "zod",
52
51
  "zod-to-json-schema",
53
52
  ],
@@ -1,9 +0,0 @@
1
- type __VLS_Props = {
2
- content: string;
3
- isStreaming?: boolean;
4
- };
5
- declare const _default: import("vue").DefineComponent<__VLS_Props, {}, {}, {}, {}, import("vue").ComponentOptionsMixin, import("vue").ComponentOptionsMixin, {}, string, import("vue").PublicProps, Readonly<__VLS_Props> & Readonly<{}>, {
6
- isStreaming: boolean;
7
- }, {}, {}, {}, string, import("vue").ComponentProvideOptions, false, {}, any>;
8
- export default _default;
9
- //# sourceMappingURL=BasicMarkdown.vue.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"BasicMarkdown.vue.d.ts","sourceRoot":"","sources":["../../../../src/v2/components/chat/BasicMarkdown.vue"],"names":[],"mappings":"AAuKA,KAAK,WAAW,GAAG;IAAE,OAAO,EAAE,MAAM,CAAC;IAAC,WAAW,CAAC,EAAE,OAAO,CAAA;CAAE,CAAC;;iBAAV,OAAO;;AAiM3D,wBAOG"}
@@ -1,115 +0,0 @@
1
- import type { Message, ToolCall, ToolMessage } from "@ag-ui/core";
2
- /**
3
- * How the chat shows tool calls.
4
- *
5
- * - `"grouped"` (default): calls made in a row share one card, collapsed to a
6
- * summary ("Used 3 tools") that opens to a row per call.
7
- * - `"line"`: the same grouping as a quiet line of text, like the reasoning
8
- * message's "Thought for 4 seconds".
9
- * - `"stacked"`: the latest call on top of a stack of the earlier ones; click
10
- * to fan the stack out into its cards.
11
- * - `"turn"`: the agent works in the open (narration, then a row per call),
12
- * and when the reply is done everything before it folds into "Worked for 9s".
13
- * - `"separate"`: every call gets its own card, as before grouping existed.
14
- *
15
- * Only calls drawn by a renderer that opts into grouping (`group: true`) are
16
- * grouped; CopilotKit's built-in card from `useDefaultRenderTool()` does. Any
17
- * other call always shows on its own and splits the group around it.
18
- */
19
- export type CopilotChatToolCallDisplay = "grouped" | "line" | "stacked" | "turn" | "separate";
20
- /** One tool call and, once it has finished, the message carrying its result. */
21
- export interface ToolCallEntry {
22
- toolCall: ToolCall;
23
- toolMessage?: ToolMessage;
24
- }
25
- export type ToolCallSegment =
26
- /** Consecutive calls drawn by renderers that opted into grouping. */
27
- {
28
- kind: "group";
29
- id: string;
30
- calls: ToolCallEntry[];
31
- /** Nothing renders after this group yet, so a running agent may add to it. */
32
- trailing: boolean;
33
- }
34
- /**
35
- * A call drawn by a renderer that didn't opt into grouping, or with no
36
- * renderer at all. The latter renders nothing (and warns in development)
37
- * without breaking a group.
38
- */
39
- | {
40
- kind: "call";
41
- id: string;
42
- call: ToolCallEntry;
43
- rendered: boolean;
44
- };
45
- /** What one assistant message's tool-call area renders. */
46
- export interface ToolCallLayout {
47
- segments: ToolCallSegment[];
48
- /** Changes whenever anything this layout renders changes; for memoization. */
49
- signature: string;
50
- }
51
- /** Which tool-call renderers are registered. */
52
- export interface ToolCallRendererNames {
53
- /** Tool names with a renderer of their own. */
54
- named: ReadonlySet<string>;
55
- /** Of those, the ones whose renderer opted into grouping. */
56
- groupedNames: ReadonlySet<string>;
57
- /** A wildcard (`"*"`) renderer is registered. */
58
- wildcard: boolean;
59
- /** The wildcard renderer opted into grouping (the built-in card does). */
60
- wildcardGroups: boolean;
61
- }
62
- /** Index of tool results by the call they answer. */
63
- export declare function getToolResults(messages: Message[]): Map<string, ToolMessage>;
64
- /**
65
- * Lays out the tool calls of every assistant message in `rendered`.
66
- *
67
- * Calls drawn by a renderer that opted into grouping join the group that's
68
- * open; anything else that shows between them closes it: assistant text, a
69
- * user, reasoning or activity message, or a call drawn by any other renderer. A group belongs to
70
- * the message where it starts, so later messages whose calls joined it render
71
- * no tool calls themselves.
72
- *
73
- * @param rendered - The messages as the chat renders them, in order.
74
- * @param all - Every message, for finding tool results (a transform may hide them).
75
- * @param renderers - The registered renderers.
76
- */
77
- export declare function getToolCallLayouts(rendered: Message[], all: Message[], renderers: ToolCallRendererNames): Map<string, ToolCallLayout>;
78
- /**
79
- * Which renderers draw tool calls, from the registered renderers and the
80
- * view's tool-call slots. A slot wins over a registered renderer and never
81
- * groups, since the chat can't tell what it draws: `#tool-call-<name>` takes
82
- * that tool, and `#tool-call` takes every tool.
83
- *
84
- * @param renderers - The registered renderers (`copilotkit.renderToolCalls`).
85
- * @param slotNames - The names of the slots the view was given.
86
- */
87
- export declare function getToolCallRendererNames(renderers: readonly {
88
- name: string;
89
- group?: boolean;
90
- }[], slotNames?: readonly string[]): ToolCallRendererNames;
91
- /** A reply's working phase, for the `"turn"` display. */
92
- export interface TurnWork {
93
- /** Stable across the turn's life: the id of the user message it answers. */
94
- key: string;
95
- /** The first assistant-side message, where the "Worked for" line goes. */
96
- firstMessageId: string;
97
- /** Everything up to and including the last message with a tool call. */
98
- workMessageIds: ReadonlySet<string>;
99
- /**
100
- * The reply's last assistant message, when it has text and is part of the
101
- * work (it also calls a tool). Its text is the answer, so it stays out.
102
- */
103
- replyTextMessageId?: string;
104
- calls: ToolCallEntry[];
105
- /** No user message follows, so the agent may still be working on it. */
106
- isLatest: boolean;
107
- }
108
- /**
109
- * Finds the working phase of every reply in `rendered`: its messages up to the
110
- * last one that calls a tool. Text in there was narration; what follows is the
111
- * reply itself. Replies that called no tools are omitted, except the latest,
112
- * which may be about to.
113
- */
114
- export declare function getTurnWork(rendered: Message[], layouts: ReadonlyMap<string, ToolCallLayout>): Map<string, TurnWork>;
115
- //# sourceMappingURL=tool-call-layout.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"tool-call-layout.d.ts","sourceRoot":"","sources":["../../../../src/v2/components/chat/tool-call-layout.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAElE;;;;;;;;;;;;;;;;GAgBG;AACH,MAAM,MAAM,0BAA0B,GAClC,SAAS,GACT,MAAM,GACN,SAAS,GACT,MAAM,GACN,UAAU,CAAC;AAEf,gFAAgF;AAChF,MAAM,WAAW,aAAa;IAC5B,QAAQ,EAAE,QAAQ,CAAC;IACnB,WAAW,CAAC,EAAE,WAAW,CAAC;CAC3B;AAED,MAAM,MAAM,eAAe;AACzB,qEAAqE;AACnE;IACE,IAAI,EAAE,OAAO,CAAC;IACd,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,aAAa,EAAE,CAAC;IACvB,8EAA8E;IAC9E,QAAQ,EAAE,OAAO,CAAC;CACnB;AACH;;;;GAIG;GACD;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,EAAE,EAAE,MAAM,CAAC;IAAC,IAAI,EAAE,aAAa,CAAC;IAAC,QAAQ,EAAE,OAAO,CAAA;CAAE,CAAC;AAEzE,2DAA2D;AAC3D,MAAM,WAAW,cAAc;IAC7B,QAAQ,EAAE,eAAe,EAAE,CAAC;IAC5B,8EAA8E;IAC9E,SAAS,EAAE,MAAM,CAAC;CACnB;AAED,gDAAgD;AAChD,MAAM,WAAW,qBAAqB;IACpC,+CAA+C;IAC/C,KAAK,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAC3B,6DAA6D;IAC7D,YAAY,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IAClC,iDAAiD;IACjD,QAAQ,EAAE,OAAO,CAAC;IAClB,0EAA0E;IAC1E,cAAc,EAAE,OAAO,CAAC;CACzB;AA4BD,qDAAqD;AACrD,wBAAgB,cAAc,CAAC,QAAQ,EAAE,OAAO,EAAE,GAAG,GAAG,CAAC,MAAM,EAAE,WAAW,CAAC,CAM5E;AAED;;;;;;;;;;;;GAYG;AACH,wBAAgB,kBAAkB,CAChC,QAAQ,EAAE,OAAO,EAAE,EACnB,GAAG,EAAE,OAAO,EAAE,EACd,SAAS,EAAE,qBAAqB,GAC/B,GAAG,CAAC,MAAM,EAAE,cAAc,CAAC,CAuD7B;AAID;;;;;;;;GAQG;AACH,wBAAgB,wBAAwB,CACtC,SAAS,EAAE,SAAS;IAAE,IAAI,EAAE,MAAM,CAAC;IAAC,KAAK,CAAC,EAAE,OAAO,CAAA;CAAE,EAAE,EACvD,SAAS,GAAE,SAAS,MAAM,EAAO,GAChC,qBAAqB,CAuBvB;AAED,yDAAyD;AACzD,MAAM,WAAW,QAAQ;IACvB,4EAA4E;IAC5E,GAAG,EAAE,MAAM,CAAC;IACZ,0EAA0E;IAC1E,cAAc,EAAE,MAAM,CAAC;IACvB,wEAAwE;IACxE,cAAc,EAAE,WAAW,CAAC,MAAM,CAAC,CAAC;IACpC;;;OAGG;IACH,kBAAkB,CAAC,EAAE,MAAM,CAAC;IAC5B,KAAK,EAAE,aAAa,EAAE,CAAC;IACvB,wEAAwE;IACxE,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED;;;;;GAKG;AACH,wBAAgB,WAAW,CACzB,QAAQ,EAAE,OAAO,EAAE,EACnB,OAAO,EAAE,WAAW,CAAC,MAAM,EAAE,cAAc,CAAC,GAC3C,GAAG,CAAC,MAAM,EAAE,QAAQ,CAAC,CA6DvB"}
@@ -1,19 +0,0 @@
1
- import type { CopilotKitCoreVue } from "./vue-core";
2
- import { TranscriptionErrorCode } from "@copilotkit/shared";
3
- export interface TranscriptionResult {
4
- text: string;
5
- size: number;
6
- type: string;
7
- }
8
- export interface TranscriptionErrorInfo {
9
- code: TranscriptionErrorCode;
10
- message: string;
11
- retryable: boolean;
12
- }
13
- export { TranscriptionErrorCode };
14
- export declare class TranscriptionError extends Error {
15
- readonly info: TranscriptionErrorInfo;
16
- constructor(info: TranscriptionErrorInfo);
17
- }
18
- export declare function transcribeAudio(core: CopilotKitCoreVue, audioBlob: Blob, filename?: string): Promise<TranscriptionResult>;
19
- //# sourceMappingURL=transcription-client.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"transcription-client.d.ts","sourceRoot":"","sources":["../../../src/v2/lib/transcription-client.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,YAAY,CAAC;AACpD,OAAO,EAAE,sBAAsB,EAAE,MAAM,oBAAoB,CAAC;AAG5D,MAAM,WAAW,mBAAmB;IAClC,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;IACb,IAAI,EAAE,MAAM,CAAC;CACd;AAED,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,sBAAsB,CAAC;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,SAAS,EAAE,OAAO,CAAC;CACpB;AAED,OAAO,EAAE,sBAAsB,EAAE,CAAC;AAuClC,qBAAa,kBAAmB,SAAQ,KAAK;IAC3C,SAAgB,IAAI,EAAE,sBAAsB,CAAC;gBAEjC,IAAI,EAAE,sBAAsB;CAKzC;AAED,wBAAsB,eAAe,CACnC,IAAI,EAAE,iBAAiB,EACvB,SAAS,EAAE,IAAI,EACf,QAAQ,GAAE,MAAyB,GAClC,OAAO,CAAC,mBAAmB,CAAC,CAiF9B"}
@@ -1,160 +0,0 @@
1
- <script setup lang="ts">
2
- import { computed, h } from "vue";
3
- import type { VNode } from "vue";
4
- import { parseMarkdown } from "@copilotkit/core";
5
- import type { MarkdownToken } from "@copilotkit/core";
6
-
7
- const props = withDefaults(
8
- defineProps<{ content: string; isStreaming?: boolean }>(),
9
- { isStreaming: false },
10
- );
11
-
12
- const tokens = computed(() => parseMarkdown(props.content ?? ""));
13
-
14
- // Allowlist URL schemes to prevent XSS via javascript:/data:/vbscript: URIs.
15
- const SAFE_HREF = /^(https?:|mailto:|tel:|#|\/|\.\/|\.\.\/)/i;
16
- const SAFE_IMG_SRC = /^(https?:|data:image\/(?!svg)|\/|\.\/|\.\.\/)/i;
17
-
18
- function sanitizeHref(href: string | undefined): string | undefined {
19
- if (!href) return undefined;
20
- return SAFE_HREF.test(href.trim()) ? href : undefined;
21
- }
22
- function sanitizeImgSrc(src: string | undefined): string | undefined {
23
- if (!src) return undefined;
24
- return SAFE_IMG_SRC.test(src.trim()) ? src : undefined;
25
- }
26
-
27
- function inline(toks: MarkdownToken[] | undefined): (VNode | string)[] {
28
- if (!toks) return [];
29
- return toks.map((t) => {
30
- switch (t.type) {
31
- case "text":
32
- return "tokens" in t && (t as any).tokens
33
- ? h("span", inline((t as any).tokens))
34
- : (t as any).text;
35
- case "strong":
36
- return h("strong", inline((t as any).tokens));
37
- case "em":
38
- return h("em", inline((t as any).tokens));
39
- case "del":
40
- return h("del", inline((t as any).tokens));
41
- case "codespan":
42
- return h(
43
- "code",
44
- { class: "cpk-markdown-inline-code" },
45
- (t as any).text,
46
- );
47
- case "br":
48
- return h("br");
49
- case "link":
50
- return h(
51
- "a",
52
- {
53
- href: sanitizeHref((t as any).href),
54
- target: "_blank",
55
- rel: "noopener noreferrer",
56
- },
57
- (t as any).tokens ? inline((t as any).tokens) : (t as any).text,
58
- );
59
- case "image":
60
- return h("img", {
61
- src: sanitizeImgSrc((t as any).href),
62
- alt: (t as any).text,
63
- class: "cpk-markdown-image",
64
- });
65
- case "escape":
66
- return (t as any).text;
67
- default:
68
- return "text" in t ? (t as any).text : "";
69
- }
70
- });
71
- }
72
-
73
- function block(t: MarkdownToken): VNode | null {
74
- switch (t.type) {
75
- case "space":
76
- return null;
77
- case "heading": {
78
- const depth = Math.min(Math.max((t as any).depth, 1), 6);
79
- return h(`h${depth}`, inline((t as any).tokens));
80
- }
81
- case "paragraph":
82
- return h("p", inline((t as any).tokens));
83
- case "blockquote":
84
- return h(
85
- "blockquote",
86
- (((t as any).tokens as MarkdownToken[] | undefined) ?? [])
87
- .map(block)
88
- .filter(Boolean),
89
- );
90
- case "code":
91
- return h("pre", { class: "cpk-markdown-code-block" }, [
92
- h(
93
- "code",
94
- { "data-language": (t as any).lang || undefined },
95
- (t as any).text,
96
- ),
97
- ]);
98
- case "hr":
99
- return h("hr");
100
- case "list": {
101
- const list = t as any;
102
- const items = list.items.map((it: any) =>
103
- h(
104
- "li",
105
- ((it.tokens as MarkdownToken[] | undefined) ?? [])
106
- .map(block)
107
- .filter(Boolean),
108
- ),
109
- );
110
- return list.ordered
111
- ? h(
112
- "ol",
113
- { start: typeof list.start === "number" ? list.start : undefined },
114
- items,
115
- )
116
- : h("ul", items);
117
- }
118
- case "table": {
119
- const tbl = t as any;
120
- return h("div", { class: "cpk-markdown-table-container" }, [
121
- h("table", [
122
- h("thead", [
123
- h(
124
- "tr",
125
- tbl.header.map((c: any) => h("th", inline(c.tokens))),
126
- ),
127
- ]),
128
- h(
129
- "tbody",
130
- tbl.rows.map((row: any) =>
131
- h(
132
- "tr",
133
- row.map((c: any) => h("td", inline(c.tokens))),
134
- ),
135
- ),
136
- ),
137
- ]),
138
- ]);
139
- }
140
- case "text":
141
- return h(
142
- "p",
143
- (t as any).tokens ? inline((t as any).tokens) : (t as any).text,
144
- );
145
- default:
146
- return "text" in t ? h("span", (t as any).text) : null;
147
- }
148
- }
149
-
150
- const Rendered = () =>
151
- h(
152
- "div",
153
- { class: "copilot-chat-assistant-markdown" },
154
- tokens.value.map(block).filter(Boolean),
155
- );
156
- </script>
157
-
158
- <template>
159
- <Rendered v-if="tokens.length" />
160
- </template>
@@ -1,69 +0,0 @@
1
- import { describe, it, expect } from "vitest";
2
- import { mount } from "@vue/test-utils";
3
- import BasicMarkdown from "../BasicMarkdown.vue";
4
-
5
- describe("BasicMarkdown.vue", () => {
6
- it("renders headings", () => {
7
- const w = mount(BasicMarkdown, { props: { content: "# Title" } });
8
- expect(w.find("h1").text()).toBe("Title");
9
- });
10
-
11
- it("renders bold/italic", () => {
12
- const w = mount(BasicMarkdown, { props: { content: "**b** *i*" } });
13
- expect(w.find("strong").text()).toBe("b");
14
- expect(w.find("em").text()).toBe("i");
15
- });
16
-
17
- it("renders code blocks", () => {
18
- const w = mount(BasicMarkdown, { props: { content: "```\nx\n```" } });
19
- expect(w.find("pre code").text()).toContain("x");
20
- });
21
-
22
- it("renders tables", () => {
23
- const w = mount(BasicMarkdown, {
24
- props: { content: "| a | b |\n| --- | --- |\n| 1 | 2 |" },
25
- });
26
- expect(w.findAll("th")).toHaveLength(2);
27
- expect(w.findAll("tbody td")).toHaveLength(2);
28
- });
29
-
30
- it("renders links with safe href", () => {
31
- const w = mount(BasicMarkdown, {
32
- props: { content: "[CK](https://copilotkit.ai)" },
33
- });
34
- expect(w.find("a").attributes("href")).toBe("https://copilotkit.ai");
35
- });
36
-
37
- it("neutralizes javascript: URIs in links (no XSS)", () => {
38
- const w = mount(BasicMarkdown, {
39
- props: { content: "[x](javascript:alert(1))" },
40
- });
41
- expect(w.find("a").attributes("href")).toBeUndefined();
42
- });
43
-
44
- it("neutralizes javascript: image src", () => {
45
- const w = mount(BasicMarkdown, {
46
- props: { content: "![x](javascript:alert(1))" },
47
- });
48
- expect(w.find("img").attributes("src")).toBeUndefined();
49
- });
50
-
51
- it("renders nothing for empty content", () => {
52
- const w = mount(BasicMarkdown, { props: { content: "" } });
53
- expect(w.text()).toBe("");
54
- });
55
-
56
- it("rejects data:image/svg+xml image src (SVG script vector)", () => {
57
- const w = mount(BasicMarkdown, {
58
- props: { content: "![x](data:image/svg+xml;base64,PHN2Zz48L3N2Zz4=)" },
59
- });
60
- expect(w.find("img").attributes("src")).toBeUndefined();
61
- });
62
-
63
- it("still allows data:image/png image src", () => {
64
- const w = mount(BasicMarkdown, {
65
- props: { content: "![x](data:image/png;base64,iVBORw0KGgo=)" },
66
- });
67
- expect(w.find("img").attributes("src")).toContain("data:image/png");
68
- });
69
- });