@copilotkit/react-core 2.0.0-rc.6 → 2.0.0-rc.60
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/README.md +1 -1
- package/dist/MarkdownRendererContext-QFCldfW_.d.cts +34 -0
- package/dist/MarkdownRendererContext-QFCldfW_.d.cts.map +1 -0
- package/dist/MarkdownRendererContext-aK63ZFd-.d.mts +34 -0
- package/dist/MarkdownRendererContext-aK63ZFd-.d.mts.map +1 -0
- package/dist/MarkdownSurfaces-Do948N1P.cjs +141 -0
- package/dist/MarkdownSurfaces-Do948N1P.cjs.map +1 -0
- package/dist/MarkdownSurfaces-rtE9wRUP.mjs +135 -0
- package/dist/MarkdownSurfaces-rtE9wRUP.mjs.map +1 -0
- package/dist/ProviderMarkdown-D0eI0aPo.cjs +268 -0
- package/dist/ProviderMarkdown-D0eI0aPo.cjs.map +1 -0
- package/dist/ProviderMarkdown-epL7F3Vt.mjs +214 -0
- package/dist/ProviderMarkdown-epL7F3Vt.mjs.map +1 -0
- package/dist/accessibility-BLK-gE0R.mjs +129 -0
- package/dist/accessibility-BLK-gE0R.mjs.map +1 -0
- package/dist/accessibility-GnodqxkH.cjs +228 -0
- package/dist/accessibility-GnodqxkH.cjs.map +1 -0
- package/dist/chat-B1zczXUD.mjs +1476 -0
- package/dist/chat-B1zczXUD.mjs.map +1 -0
- package/dist/chat-BZ_bvAkg.d.mts +38 -0
- package/dist/chat-BZ_bvAkg.d.mts.map +1 -0
- package/dist/chat-CkFZMo5E.cjs +1579 -0
- package/dist/chat-CkFZMo5E.cjs.map +1 -0
- package/dist/chat-CpZVyF_t.d.cts +38 -0
- package/dist/chat-CpZVyF_t.d.cts.map +1 -0
- package/dist/context.cjs +75 -63
- package/dist/context.cjs.map +1 -1
- package/dist/context.d.cts +62 -107
- package/dist/context.d.cts.map +1 -1
- package/dist/context.d.mts +62 -107
- package/dist/context.d.mts.map +1 -1
- package/dist/context.mjs +76 -64
- package/dist/context.mjs.map +1 -1
- package/dist/generative-ui/map.cjs +26 -0
- package/dist/generative-ui/map.cjs.map +1 -0
- package/dist/generative-ui/map.d.cts +23 -0
- package/dist/generative-ui/map.d.cts.map +1 -0
- package/dist/generative-ui/map.d.mts +23 -0
- package/dist/generative-ui/map.d.mts.map +1 -0
- package/dist/generative-ui/map.mjs +24 -0
- package/dist/generative-ui/map.mjs.map +1 -0
- package/dist/generative-ui/native.cjs +67 -0
- package/dist/generative-ui/native.cjs.map +1 -0
- package/dist/generative-ui/native.d.cts +156 -0
- package/dist/generative-ui/native.d.cts.map +1 -0
- package/dist/generative-ui/native.d.mts +156 -0
- package/dist/generative-ui/native.d.mts.map +1 -0
- package/dist/generative-ui/native.mjs +25 -0
- package/dist/generative-ui/native.mjs.map +1 -0
- package/dist/generative-ui.cjs +33 -8088
- package/dist/generative-ui.cjs.map +1 -1
- package/dist/generative-ui.d.cts +2472 -4789
- package/dist/generative-ui.d.cts.map +1 -1
- package/dist/generative-ui.d.mts +2472 -4789
- package/dist/generative-ui.d.mts.map +1 -1
- package/dist/generative-ui.mjs +9 -8074
- package/dist/generative-ui.mjs.map +1 -1
- package/dist/headless.cjs +289 -1054
- package/dist/headless.cjs.map +1 -1
- package/dist/headless.d.cts +149 -372
- package/dist/headless.d.cts.map +1 -1
- package/dist/headless.d.mts +123 -237
- package/dist/headless.d.mts.map +1 -1
- package/dist/headless.mjs +283 -1054
- package/dist/headless.mjs.map +1 -1
- package/dist/hooks-D2H3iL0N.d.mts +84 -0
- package/dist/hooks-D2H3iL0N.d.mts.map +1 -0
- package/dist/hooks-Dy8HeEdr.d.cts +84 -0
- package/dist/hooks-Dy8HeEdr.d.cts.map +1 -0
- package/dist/index.cjs +8702 -13238
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +312 -872
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +311 -871
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +1790 -7803
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +14075 -13828
- package/dist/index.umd.js.map +1 -1
- package/dist/refs-B5Dn59Pn.cjs +43 -0
- package/dist/refs-B5Dn59Pn.cjs.map +1 -0
- package/dist/refs-BW15wX_V.mjs +31 -0
- package/dist/refs-BW15wX_V.mjs.map +1 -0
- package/dist/runtime-context-B7f4M85q.d.mts +215 -0
- package/dist/runtime-context-B7f4M85q.d.mts.map +1 -0
- package/dist/runtime-context-CIkZS0ap.d.cts +215 -0
- package/dist/runtime-context-CIkZS0ap.d.cts.map +1 -0
- package/dist/standard-CSBR4FhM.mjs +5800 -0
- package/dist/standard-CSBR4FhM.mjs.map +1 -0
- package/dist/standard-DBjcBGyd.cjs +5796 -0
- package/dist/standard-DBjcBGyd.cjs.map +1 -0
- package/dist/use-generative-ui-root-CIp0ytE6.cjs +390 -0
- package/dist/use-generative-ui-root-CIp0ytE6.cjs.map +1 -0
- package/dist/use-generative-ui-root-oBqPa6uy.mjs +386 -0
- package/dist/use-generative-ui-root-oBqPa6uy.mjs.map +1 -0
- package/dist/v2/context.d.cts +28 -0
- package/dist/v2/context.d.mts +28 -0
- package/dist/v2/generative-ui/native.d.cts +112 -0
- package/dist/v2/generative-ui/native.d.mts +112 -0
- package/dist/v2/generative-ui.d.cts +96 -0
- package/dist/v2/generative-ui.d.mts +96 -0
- package/dist/v2/headless.d.cts +96 -0
- package/dist/v2/headless.d.mts +96 -0
- package/dist/v2/index.d.cts +2220 -0
- package/dist/v2/index.d.mts +2220 -0
- package/dist/v2/styles.css +5 -0
- package/package.json +116 -24
- package/dist/MarkdownRendererContext-B4Us6mwL.d.cts +0 -954
- package/dist/MarkdownRendererContext-B4Us6mwL.d.cts.map +0 -1
- package/dist/MarkdownRendererContext-CRKFzsEB.d.mts +0 -954
- package/dist/MarkdownRendererContext-CRKFzsEB.d.mts.map +0 -1
- package/dist/MarkdownSurfaces-Bd--93O1.mjs +0 -3676
- package/dist/MarkdownSurfaces-Bd--93O1.mjs.map +0 -1
- package/dist/MarkdownSurfaces-S4wfUl3J.cjs +0 -3991
- package/dist/MarkdownSurfaces-S4wfUl3J.cjs.map +0 -1
package/README.md
CHANGED
|
@@ -122,7 +122,7 @@ const modifiedConfig = copilotKitCustomizeConfig(config, {
|
|
|
122
122
|
},
|
|
123
123
|
],
|
|
124
124
|
});
|
|
125
|
-
const response = await ChatOpenAI({ model: "gpt-
|
|
125
|
+
const response = await ChatOpenAI({ model: "gpt-6-luna" }).invoke(
|
|
126
126
|
messages,
|
|
127
127
|
modifiedConfig,
|
|
128
128
|
);
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { StreamingMarkdownRendererProps } from "@copilotkit/markdown/react";
|
|
3
|
+
|
|
4
|
+
//#region src/providers/MarkdownRendererContext.d.ts
|
|
5
|
+
/** Props every markdown renderer (built-in or plugged-in) receives. */
|
|
6
|
+
interface MarkdownRendererProps {
|
|
7
|
+
/** Raw, possibly-partial (streaming) markdown string. */
|
|
8
|
+
content: string;
|
|
9
|
+
/** Hint that the message is still streaming, for cursor/affordance use. */
|
|
10
|
+
isStreaming?: boolean;
|
|
11
|
+
className?: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Config for the built-in default renderer. Pass this (instead of a component)
|
|
15
|
+
* as `markdownRenderer` to configure the built-in renderer without writing a
|
|
16
|
+
* wrapper — e.g. `markdownRenderer={{ nodeRenderers: { codeBlock: Shiki } }}`.
|
|
17
|
+
*/
|
|
18
|
+
type DefaultMarkdownRendererProps = Pick<StreamingMarkdownRendererProps, "nodeRenderers" | "caret" | "options" | "className" | "onLinkClick" | "onCitationClick">;
|
|
19
|
+
/** A markdown renderer: a component, OR config for the built-in default. */
|
|
20
|
+
type MarkdownRenderer = React.ComponentType<MarkdownRendererProps> | DefaultMarkdownRendererProps;
|
|
21
|
+
declare const MarkdownRendererProvider: React.FC<{
|
|
22
|
+
renderer?: MarkdownRenderer;
|
|
23
|
+
children: React.ReactNode;
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* The markdown renderer set at the provider level, or `undefined` when none was
|
|
27
|
+
* supplied. Message components fall back to the built-in
|
|
28
|
+
* `StreamingMarkdownDefaultRenderer` (via `CopilotChatAssistantMessage.MarkdownRenderer`)
|
|
29
|
+
* when this is `undefined`.
|
|
30
|
+
*/
|
|
31
|
+
declare function useMarkdownRenderer(): MarkdownRenderer | undefined;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { useMarkdownRenderer as a, MarkdownRendererProvider as i, MarkdownRenderer as n, MarkdownRendererProps as r, DefaultMarkdownRendererProps as t };
|
|
34
|
+
//# sourceMappingURL=MarkdownRendererContext-QFCldfW_.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MarkdownRendererContext-QFCldfW_.d.cts","names":[],"sources":["../src/providers/MarkdownRendererContext.tsx"],"mappings":";;;;;UAMiB,qBAAA;EAAA;EAEf,OAAA;;EAEA,WAAA;EACA,SAAA;AAAA;;;;AAQF;;KAAY,4BAAA,GAA+B,IAAA,CACzC,8BAAA;;KAUU,gBAAA,GACR,KAAA,CAAM,aAAA,CAAc,qBAAA,IACpB,4BAAA;AAAA,cAMS,wBAAA,EAA0B,KAAA,CAAM,EAAA;EAC3C,QAAA,GAAW,gBAAA;EACX,QAAA,EAAU,KAAA,CAAM,SAAA;AAAA;;;;;;;iBAaF,mBAAA,CAAA,GAAuB,gBAAA"}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { StreamingMarkdownRendererProps } from "@copilotkit/markdown/react";
|
|
3
|
+
|
|
4
|
+
//#region src/providers/MarkdownRendererContext.d.ts
|
|
5
|
+
/** Props every markdown renderer (built-in or plugged-in) receives. */
|
|
6
|
+
interface MarkdownRendererProps {
|
|
7
|
+
/** Raw, possibly-partial (streaming) markdown string. */
|
|
8
|
+
content: string;
|
|
9
|
+
/** Hint that the message is still streaming, for cursor/affordance use. */
|
|
10
|
+
isStreaming?: boolean;
|
|
11
|
+
className?: string;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Config for the built-in default renderer. Pass this (instead of a component)
|
|
15
|
+
* as `markdownRenderer` to configure the built-in renderer without writing a
|
|
16
|
+
* wrapper — e.g. `markdownRenderer={{ nodeRenderers: { codeBlock: Shiki } }}`.
|
|
17
|
+
*/
|
|
18
|
+
type DefaultMarkdownRendererProps = Pick<StreamingMarkdownRendererProps, "nodeRenderers" | "caret" | "options" | "className" | "onLinkClick" | "onCitationClick">;
|
|
19
|
+
/** A markdown renderer: a component, OR config for the built-in default. */
|
|
20
|
+
type MarkdownRenderer = React.ComponentType<MarkdownRendererProps> | DefaultMarkdownRendererProps;
|
|
21
|
+
declare const MarkdownRendererProvider: React.FC<{
|
|
22
|
+
renderer?: MarkdownRenderer;
|
|
23
|
+
children: React.ReactNode;
|
|
24
|
+
}>;
|
|
25
|
+
/**
|
|
26
|
+
* The markdown renderer set at the provider level, or `undefined` when none was
|
|
27
|
+
* supplied. Message components fall back to the built-in
|
|
28
|
+
* `StreamingMarkdownDefaultRenderer` (via `CopilotChatAssistantMessage.MarkdownRenderer`)
|
|
29
|
+
* when this is `undefined`.
|
|
30
|
+
*/
|
|
31
|
+
declare function useMarkdownRenderer(): MarkdownRenderer | undefined;
|
|
32
|
+
//#endregion
|
|
33
|
+
export { useMarkdownRenderer as a, MarkdownRendererProvider as i, MarkdownRenderer as n, MarkdownRendererProps as r, DefaultMarkdownRendererProps as t };
|
|
34
|
+
//# sourceMappingURL=MarkdownRendererContext-aK63ZFd-.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MarkdownRendererContext-aK63ZFd-.d.mts","names":[],"sources":["../src/providers/MarkdownRendererContext.tsx"],"mappings":";;;;;UAMiB,qBAAA;EAAA;EAEf,OAAA;;EAEA,WAAA;EACA,SAAA;AAAA;;;;AAQF;;KAAY,4BAAA,GAA+B,IAAA,CACzC,8BAAA;;KAUU,gBAAA,GACR,KAAA,CAAM,aAAA,CAAc,qBAAA,IACpB,4BAAA;AAAA,cAMS,wBAAA,EAA0B,KAAA,CAAM,EAAA;EAC3C,QAAA,GAAW,gBAAA;EACX,QAAA,EAAU,KAAA,CAAM,SAAA;AAAA;;;;;;;iBAaF,mBAAA,CAAA,GAAuB,gBAAA"}
|
|
@@ -0,0 +1,141 @@
|
|
|
1
|
+
const require_accessibility = require('./accessibility-GnodqxkH.cjs');
|
|
2
|
+
const require_ProviderMarkdown = require('./ProviderMarkdown-D0eI0aPo.cjs');
|
|
3
|
+
const require_chat = require('./chat-CkFZMo5E.cjs');
|
|
4
|
+
let _copilotkit_core = require("@copilotkit/core");
|
|
5
|
+
let react = require("react");
|
|
6
|
+
react = require_accessibility.__toESM(react);
|
|
7
|
+
let react_jsx_runtime = require("react/jsx-runtime");
|
|
8
|
+
let _copilotkit_shared = require("@copilotkit/shared");
|
|
9
|
+
let _copilotkit_core_generative_ui = require("@copilotkit/core/generative-ui");
|
|
10
|
+
|
|
11
|
+
//#region src/generative-ui/markdown/MarkdownSurfaces.tsx
|
|
12
|
+
const FADE_STYLE_ID = "a2ui-markdown-fade";
|
|
13
|
+
/**
|
|
14
|
+
* Components fade in as they stream: each element that appears under a
|
|
15
|
+
* markdown surface runs the keyframes once on mount. React keeps the keys
|
|
16
|
+
* stable per component id, so an element that is already there never
|
|
17
|
+
* animates again. SVG internals are left out: a chart library re-creates
|
|
18
|
+
* its nodes on each render, and a fade on each of them would read as the
|
|
19
|
+
* chart flashing whenever a sibling arrives. The fade runs under reduced
|
|
20
|
+
* motion too, because it moves nothing.
|
|
21
|
+
*/
|
|
22
|
+
const FADE_CSS = ["@keyframes a2ui-fade-in{from{opacity:0}}", "[data-a2ui-markdown-surface] .a2ui-surface *:not(svg):not(svg *){animation:a2ui-fade-in .35s ease-out backwards}"].join("\n");
|
|
23
|
+
const noEffect = () => {};
|
|
24
|
+
const useInjectEffect = typeof document === "undefined" ? noEffect : react.useInsertionEffect;
|
|
25
|
+
function useFadeStyles() {
|
|
26
|
+
useInjectEffect(() => {
|
|
27
|
+
if (document.getElementById(FADE_STYLE_ID)) return;
|
|
28
|
+
const style = document.createElement("style");
|
|
29
|
+
style.id = FADE_STYLE_ID;
|
|
30
|
+
style.textContent = FADE_CSS;
|
|
31
|
+
document.head.appendChild(style);
|
|
32
|
+
}, []);
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* The default wrapper of a chat surface in the markdown transport. It draws
|
|
36
|
+
* no box: a surface sits in the flow of the reply like a paragraph, and a
|
|
37
|
+
* Card draws its own box when it is interactive. Nothing shows while the
|
|
38
|
+
* surface streams. A rejected operation gets one line.
|
|
39
|
+
*/
|
|
40
|
+
function DefaultMarkdownChatSurface({ surfaceId, error, children }) {
|
|
41
|
+
useFadeStyles();
|
|
42
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsxs)("div", {
|
|
43
|
+
"data-generative-ui-chat-surface": surfaceId,
|
|
44
|
+
"data-a2ui-markdown-surface": surfaceId,
|
|
45
|
+
className: "cpk-markdown-chat-surface",
|
|
46
|
+
children: [children, error && /* @__PURE__ */ (0, react_jsx_runtime.jsx)("div", {
|
|
47
|
+
role: "alert",
|
|
48
|
+
className: "cpk-markdown-chat-surface-error",
|
|
49
|
+
children: "Part of this UI couldn't be shown."
|
|
50
|
+
})]
|
|
51
|
+
});
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* One chat surface in the chat wrapper. Nothing renders until the surface
|
|
55
|
+
* has its root component, so a half-streamed fence shows no empty card.
|
|
56
|
+
*/
|
|
57
|
+
function ChatSurface({ runtime, threadId, surfaceId, Wrapper }) {
|
|
58
|
+
const surface = runtime.manager.getSurface(threadId, surfaceId) ?? runtime.manager.getFailedSurface(threadId, surfaceId);
|
|
59
|
+
const root = surface?.model.componentsModel.get(_copilotkit_shared.GENERATIVE_UI_ROOT_ID);
|
|
60
|
+
if (!surface || !root && !surface.failed) return null;
|
|
61
|
+
const rejected = [...surface.serverRejected, ...surface.clientRejected];
|
|
62
|
+
const error = rejected.length ? new Error(`${rejected.length} UI update(s) could not be shown`, { cause: rejected }) : void 0;
|
|
63
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(Wrapper, {
|
|
64
|
+
surfaceId,
|
|
65
|
+
isStreaming: surface.status === "streaming",
|
|
66
|
+
error,
|
|
67
|
+
rejected,
|
|
68
|
+
children: surface.failed ? null : /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_chat.SurfaceView, { surface })
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/** The blocks of one fence: lifted prose and chat surfaces, in order. */
|
|
72
|
+
function FenceBlocks({ blocks, runtime, threadId, isStreaming, markdownProps }) {
|
|
73
|
+
const Wrapper = (0, react.useContext)(require_chat.GenerativeUIChatSlotContext) ?? DefaultMarkdownChatSurface;
|
|
74
|
+
let surfaceCount = 0;
|
|
75
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: blocks.map((block, index) => {
|
|
76
|
+
if (block.kind === "markdown") return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_ProviderMarkdown.ProviderMarkdown, {
|
|
77
|
+
...markdownProps,
|
|
78
|
+
content: block.markdown,
|
|
79
|
+
isStreaming: isStreaming && index === blocks.length - 1
|
|
80
|
+
}, `md-${surfaceCount}`);
|
|
81
|
+
surfaceCount++;
|
|
82
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(ChatSurface, {
|
|
83
|
+
runtime,
|
|
84
|
+
threadId,
|
|
85
|
+
surfaceId: block.surfaceId,
|
|
86
|
+
Wrapper
|
|
87
|
+
}, block.surfaceId);
|
|
88
|
+
}) });
|
|
89
|
+
}
|
|
90
|
+
/**
|
|
91
|
+
* Experimental. Renders an assistant message of the markdown transport.
|
|
92
|
+
* Prose renders as markdown. In place of each ```a2ui fence:
|
|
93
|
+
*
|
|
94
|
+
* - the fence that created a chat surface renders that surface (and the
|
|
95
|
+
* prose lifted out of it) in the chat wrapper;
|
|
96
|
+
* - every other fence renders nothing: a fence that updates the surface of
|
|
97
|
+
* an earlier fence, or a fence for a named target, which
|
|
98
|
+
* `<GenerativeUISurface id>` renders.
|
|
99
|
+
*
|
|
100
|
+
* Use it inside `<CopilotKitProvider generativeUI={{ ui, transport: "markdown" }}>`.
|
|
101
|
+
* `CopilotChatAssistantMessage` uses it there on its own.
|
|
102
|
+
*
|
|
103
|
+
* @example
|
|
104
|
+
* ```tsx
|
|
105
|
+
* <MarkdownSurfaces content={message.content} messageId={message.id} />
|
|
106
|
+
* ```
|
|
107
|
+
*/
|
|
108
|
+
function MarkdownSurfaces({ content, messageId, isStreaming = false, ...markdownProps }) {
|
|
109
|
+
const runtime = require_accessibility.useRequiredGenerativeUIRuntime();
|
|
110
|
+
const chatThreadId = require_chat.useCopilotChatConfiguration()?.threadId;
|
|
111
|
+
require_chat.useManagerVersion(runtime);
|
|
112
|
+
const segments = (0, react.useMemo)(() => (0, _copilotkit_core.splitA2UIMarkdown)(content), [content]);
|
|
113
|
+
const threadId = chatThreadId ?? runtime.manager.getActiveThread();
|
|
114
|
+
const source = (0, _copilotkit_core_generative_ui.getMarkdownSurfaceSource)(runtime);
|
|
115
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(react_jsx_runtime.Fragment, { children: segments.map((segment, index) => {
|
|
116
|
+
const isLast = index === segments.length - 1;
|
|
117
|
+
if (segment.type === "markdown") return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(require_ProviderMarkdown.ProviderMarkdown, {
|
|
118
|
+
...markdownProps,
|
|
119
|
+
content: segment.text,
|
|
120
|
+
isStreaming: isStreaming && isLast
|
|
121
|
+
}, `md-${index}`);
|
|
122
|
+
const blocks = threadId ? source.getFenceBlocks(threadId, messageId, segment.index) : void 0;
|
|
123
|
+
if (!threadId || !blocks) return null;
|
|
124
|
+
return /* @__PURE__ */ (0, react_jsx_runtime.jsx)(FenceBlocks, {
|
|
125
|
+
blocks,
|
|
126
|
+
runtime,
|
|
127
|
+
threadId,
|
|
128
|
+
isStreaming: isStreaming && isLast,
|
|
129
|
+
markdownProps
|
|
130
|
+
}, `a2ui-${segment.index}`);
|
|
131
|
+
}) });
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
//#endregion
|
|
135
|
+
Object.defineProperty(exports, 'MarkdownSurfaces', {
|
|
136
|
+
enumerable: true,
|
|
137
|
+
get: function () {
|
|
138
|
+
return MarkdownSurfaces;
|
|
139
|
+
}
|
|
140
|
+
});
|
|
141
|
+
//# sourceMappingURL=MarkdownSurfaces-Do948N1P.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MarkdownSurfaces-Do948N1P.cjs","names":["useInsertionEffect","GENERATIVE_UI_ROOT_ID","SurfaceView","GenerativeUIChatSlotContext","ProviderMarkdown","useRequiredGenerativeUIRuntime","useCopilotChatConfiguration"],"sources":["../src/generative-ui/markdown/MarkdownSurfaces.tsx"],"sourcesContent":["import React, { useContext, useInsertionEffect, useMemo } from \"react\";\nimport type { ComponentType } from \"react\";\nimport { splitA2UIMarkdown } from \"@copilotkit/core\";\nimport { GENERATIVE_UI_ROOT_ID } from \"@copilotkit/shared\";\nimport { useCopilotChatConfiguration } from \"../../providers/CopilotChatConfigurationProvider\";\nimport { GenerativeUIChatSlotContext } from \"../chat\";\nimport type { GenerativeUIChatSurfaceProps } from \"../chat\";\nimport { useManagerVersion } from \"../hooks\";\nimport { useRequiredGenerativeUIRuntime } from \"../runtime-context\";\nimport type { GenerativeUIRuntime } from \"../runtime-context\";\nimport { SurfaceView } from \"../SurfaceView\";\nimport type { DefaultMarkdownRendererProps } from \"../../providers/MarkdownRendererContext\";\nimport { ProviderMarkdown } from \"./ProviderMarkdown\";\nimport { getMarkdownSurfaceSource } from \"@copilotkit/core/generative-ui\";\nimport type { MarkdownFenceBlock } from \"@copilotkit/core/generative-ui\";\n\n/** Config for the built-in markdown renderer, plus `className`. */\ntype MarkdownProps = DefaultMarkdownRendererProps;\n\nconst FADE_STYLE_ID = \"a2ui-markdown-fade\";\n/**\n * Components fade in as they stream: each element that appears under a\n * markdown surface runs the keyframes once on mount. React keeps the keys\n * stable per component id, so an element that is already there never\n * animates again. SVG internals are left out: a chart library re-creates\n * its nodes on each render, and a fade on each of them would read as the\n * chart flashing whenever a sibling arrives. The fade runs under reduced\n * motion too, because it moves nothing.\n */\nconst FADE_CSS = [\n // Opacity only, so it also runs when the user asks for reduced motion:\n // that setting is about movement, and a short fade is not movement.\n // The keyframes give only the start, so each element fades to its own\n // opacity (a disabled control to its dimmed one), and no fill holds the\n // end: a finished fade must not pin opacity, or an element that hides\n // itself with opacity (the map's zoom hint) stays visible forever.\n \"@keyframes a2ui-fade-in{from{opacity:0}}\",\n \"[data-a2ui-markdown-surface] .a2ui-surface *:not(svg):not(svg *){animation:a2ui-fade-in .35s ease-out backwards}\",\n].join(\"\\n\");\n\n// There is no document on the server, so nothing is added there.\nconst noEffect: typeof useInsertionEffect = () => {};\nconst useInjectEffect =\n typeof document === \"undefined\" ? noEffect : useInsertionEffect;\n\nfunction useFadeStyles() {\n useInjectEffect(() => {\n if (document.getElementById(FADE_STYLE_ID)) return;\n const style = document.createElement(\"style\");\n style.id = FADE_STYLE_ID;\n style.textContent = FADE_CSS;\n document.head.appendChild(style);\n }, []);\n}\n\n/**\n * The default wrapper of a chat surface in the markdown transport. It draws\n * no box: a surface sits in the flow of the reply like a paragraph, and a\n * Card draws its own box when it is interactive. Nothing shows while the\n * surface streams. A rejected operation gets one line.\n */\nexport function DefaultMarkdownChatSurface({\n surfaceId,\n error,\n children,\n}: GenerativeUIChatSurfaceProps) {\n useFadeStyles();\n return (\n <div\n data-generative-ui-chat-surface={surfaceId}\n data-a2ui-markdown-surface={surfaceId}\n className=\"cpk-markdown-chat-surface\"\n >\n {children}\n {error && (\n <div role=\"alert\" className=\"cpk-markdown-chat-surface-error\">\n Part of this UI couldn't be shown.\n </div>\n )}\n </div>\n );\n}\n\nexport type MarkdownSurfacesProps = MarkdownProps & {\n /** The text of the assistant message, complete or still streaming. */\n content: string;\n /** The id of the message. The fences of the message are found by it. */\n messageId: string;\n /** Whether the message is still streaming. Only its last prose shows the caret. */\n isStreaming?: boolean;\n};\n\n/**\n * One chat surface in the chat wrapper. Nothing renders until the surface\n * has its root component, so a half-streamed fence shows no empty card.\n */\nfunction ChatSurface({\n runtime,\n threadId,\n surfaceId,\n Wrapper,\n}: {\n runtime: GenerativeUIRuntime;\n threadId: string;\n surfaceId: string;\n Wrapper: ComponentType<GenerativeUIChatSurfaceProps>;\n}) {\n // A surface whose createSurface was rejected shows only the error.\n const surface =\n runtime.manager.getSurface(threadId, surfaceId) ??\n runtime.manager.getFailedSurface(threadId, surfaceId);\n const root = surface?.model.componentsModel.get(GENERATIVE_UI_ROOT_ID);\n if (!surface || (!root && !surface.failed)) return null;\n const rejected = [...surface.serverRejected, ...surface.clientRejected];\n const error = rejected.length\n ? new Error(`${rejected.length} UI update(s) could not be shown`, {\n cause: rejected,\n })\n : undefined;\n return (\n <Wrapper\n surfaceId={surfaceId}\n isStreaming={surface.status === \"streaming\"}\n error={error}\n rejected={rejected}\n >\n {surface.failed ? null : <SurfaceView surface={surface} />}\n </Wrapper>\n );\n}\n\n/** The blocks of one fence: lifted prose and chat surfaces, in order. */\nfunction FenceBlocks({\n blocks,\n runtime,\n threadId,\n isStreaming,\n markdownProps,\n}: {\n blocks: MarkdownFenceBlock[];\n runtime: GenerativeUIRuntime;\n threadId: string;\n isStreaming: boolean;\n markdownProps: MarkdownProps;\n}) {\n const Wrapper =\n useContext(GenerativeUIChatSlotContext) ?? DefaultMarkdownChatSurface;\n let surfaceCount = 0;\n return (\n <>\n {blocks.map((block, index) => {\n // Keys follow the surfaces, so a surface keeps its element when\n // prose splits off around it during streaming.\n if (block.kind === \"markdown\") {\n return (\n <ProviderMarkdown\n key={`md-${surfaceCount}`}\n {...markdownProps}\n content={block.markdown}\n isStreaming={isStreaming && index === blocks.length - 1}\n />\n );\n }\n surfaceCount++;\n return (\n <ChatSurface\n key={block.surfaceId}\n runtime={runtime}\n threadId={threadId}\n surfaceId={block.surfaceId}\n Wrapper={Wrapper}\n />\n );\n })}\n </>\n );\n}\n\n/**\n * Experimental. Renders an assistant message of the markdown transport.\n * Prose renders as markdown. In place of each ```a2ui fence:\n *\n * - the fence that created a chat surface renders that surface (and the\n * prose lifted out of it) in the chat wrapper;\n * - every other fence renders nothing: a fence that updates the surface of\n * an earlier fence, or a fence for a named target, which\n * `<GenerativeUISurface id>` renders.\n *\n * Use it inside `<CopilotKitProvider generativeUI={{ ui, transport: \"markdown\" }}>`.\n * `CopilotChatAssistantMessage` uses it there on its own.\n *\n * @example\n * ```tsx\n * <MarkdownSurfaces content={message.content} messageId={message.id} />\n * ```\n */\nexport function MarkdownSurfaces({\n content,\n messageId,\n isStreaming = false,\n ...markdownProps\n}: MarkdownSurfacesProps) {\n const runtime = useRequiredGenerativeUIRuntime();\n const chatThreadId = useCopilotChatConfiguration()?.threadId;\n useManagerVersion(runtime);\n const segments = useMemo(() => splitA2UIMarkdown(content), [content]);\n const threadId = chatThreadId ?? runtime.manager.getActiveThread();\n const source = getMarkdownSurfaceSource(runtime);\n\n return (\n <>\n {segments.map((segment, index) => {\n const isLast = index === segments.length - 1;\n if (segment.type === \"markdown\") {\n return (\n <ProviderMarkdown\n key={`md-${index}`}\n {...markdownProps}\n content={segment.text}\n isStreaming={isStreaming && isLast}\n />\n );\n }\n const blocks = threadId\n ? source.getFenceBlocks(threadId, messageId, segment.index)\n : undefined;\n if (!threadId || !blocks) return null;\n return (\n <FenceBlocks\n key={`a2ui-${segment.index}`}\n blocks={blocks}\n runtime={runtime}\n threadId={threadId}\n isStreaming={isStreaming && isLast}\n markdownProps={markdownProps}\n />\n );\n })}\n </>\n );\n}\n"],"mappings":";;;;;;;;;;;AAmBA,MAAM,gBAAgB;;;;;;;;;;AAUtB,MAAM,WAAW,CAOf,4CACA,mHACD,CAAC,KAAK,KAAK;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AO,SAAS;KAHjD,MAAM,eAIX;AAGN;AACA,SACE,2CAAC;GAEU;GACC;GACV,WAAW,MAAM;GACR;KAJJ,MAAM,UAKX;GAEJ,GACD;;;;;;;;;;;;;;;;;;;;AAsBP,SAAgB,iBAAiB,EAC/B,SACA,WACA,cAAc,OACd,GAAG,iBACqB;CACxB,MAAM,UAAUC,sDAAgC;CAChD,MAAM,eAAeC,0CAA6B,EAAE;AACpD,gCAAkB,QAAQ;CAC1B,MAAM,4EAA2C,QAAQ,EAAE,CAAC,QAAQ,CAAC;CACrE,MAAM,WAAW,gBAAgB,QAAQ,QAAQ,iBAAiB;CAClE,MAAM,sEAAkC,QAAQ;AAEhD,QACE,mFACG,SAAS,KAAK,SAAS,UAAU;EAChC,MAAM,SAAS,UAAU,SAAS,SAAS;AAC3C,MAAI,QAAQ,SAAS,WACnB,QACE,2CAACF;GAEC,GAAI;GACJ,SAAS,QAAQ;GACjB,aAAa,eAAe;KAHvB,MAAM,QAIX;EAGN,MAAM,SAAS,WACX,OAAO,eAAe,UAAU,WAAW,QAAQ,MAAM,GACzD;AACJ,MAAI,CAAC,YAAY,CAAC,OAAQ,QAAO;AACjC,SACE,2CAAC;GAES;GACC;GACC;GACV,aAAa,eAAe;GACb;KALV,QAAQ,QAAQ,QAMrB;GAEJ,GACD"}
|
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
import { t as ProviderMarkdown } from "./ProviderMarkdown-epL7F3Vt.mjs";
|
|
2
|
+
import { m as useRequiredGenerativeUIRuntime } from "./accessibility-BLK-gE0R.mjs";
|
|
3
|
+
import { g as useManagerVersion, n as GenerativeUIChatSlotContext, o as SurfaceView, y as useCopilotChatConfiguration } from "./chat-B1zczXUD.mjs";
|
|
4
|
+
import { splitA2UIMarkdown } from "@copilotkit/core";
|
|
5
|
+
import React, { useContext, useInsertionEffect, useMemo } from "react";
|
|
6
|
+
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
7
|
+
import { GENERATIVE_UI_ROOT_ID } from "@copilotkit/shared";
|
|
8
|
+
import { getMarkdownSurfaceSource } from "@copilotkit/core/generative-ui";
|
|
9
|
+
|
|
10
|
+
//#region src/generative-ui/markdown/MarkdownSurfaces.tsx
|
|
11
|
+
const FADE_STYLE_ID = "a2ui-markdown-fade";
|
|
12
|
+
/**
|
|
13
|
+
* Components fade in as they stream: each element that appears under a
|
|
14
|
+
* markdown surface runs the keyframes once on mount. React keeps the keys
|
|
15
|
+
* stable per component id, so an element that is already there never
|
|
16
|
+
* animates again. SVG internals are left out: a chart library re-creates
|
|
17
|
+
* its nodes on each render, and a fade on each of them would read as the
|
|
18
|
+
* chart flashing whenever a sibling arrives. The fade runs under reduced
|
|
19
|
+
* motion too, because it moves nothing.
|
|
20
|
+
*/
|
|
21
|
+
const FADE_CSS = ["@keyframes a2ui-fade-in{from{opacity:0}}", "[data-a2ui-markdown-surface] .a2ui-surface *:not(svg):not(svg *){animation:a2ui-fade-in .35s ease-out backwards}"].join("\n");
|
|
22
|
+
const noEffect = () => {};
|
|
23
|
+
const useInjectEffect = typeof document === "undefined" ? noEffect : useInsertionEffect;
|
|
24
|
+
function useFadeStyles() {
|
|
25
|
+
useInjectEffect(() => {
|
|
26
|
+
if (document.getElementById(FADE_STYLE_ID)) return;
|
|
27
|
+
const style = document.createElement("style");
|
|
28
|
+
style.id = FADE_STYLE_ID;
|
|
29
|
+
style.textContent = FADE_CSS;
|
|
30
|
+
document.head.appendChild(style);
|
|
31
|
+
}, []);
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The default wrapper of a chat surface in the markdown transport. It draws
|
|
35
|
+
* no box: a surface sits in the flow of the reply like a paragraph, and a
|
|
36
|
+
* Card draws its own box when it is interactive. Nothing shows while the
|
|
37
|
+
* surface streams. A rejected operation gets one line.
|
|
38
|
+
*/
|
|
39
|
+
function DefaultMarkdownChatSurface({ surfaceId, error, children }) {
|
|
40
|
+
useFadeStyles();
|
|
41
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
42
|
+
"data-generative-ui-chat-surface": surfaceId,
|
|
43
|
+
"data-a2ui-markdown-surface": surfaceId,
|
|
44
|
+
className: "cpk-markdown-chat-surface",
|
|
45
|
+
children: [children, error && /* @__PURE__ */ jsx("div", {
|
|
46
|
+
role: "alert",
|
|
47
|
+
className: "cpk-markdown-chat-surface-error",
|
|
48
|
+
children: "Part of this UI couldn't be shown."
|
|
49
|
+
})]
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* One chat surface in the chat wrapper. Nothing renders until the surface
|
|
54
|
+
* has its root component, so a half-streamed fence shows no empty card.
|
|
55
|
+
*/
|
|
56
|
+
function ChatSurface({ runtime, threadId, surfaceId, Wrapper }) {
|
|
57
|
+
const surface = runtime.manager.getSurface(threadId, surfaceId) ?? runtime.manager.getFailedSurface(threadId, surfaceId);
|
|
58
|
+
const root = surface?.model.componentsModel.get(GENERATIVE_UI_ROOT_ID);
|
|
59
|
+
if (!surface || !root && !surface.failed) return null;
|
|
60
|
+
const rejected = [...surface.serverRejected, ...surface.clientRejected];
|
|
61
|
+
const error = rejected.length ? new Error(`${rejected.length} UI update(s) could not be shown`, { cause: rejected }) : void 0;
|
|
62
|
+
return /* @__PURE__ */ jsx(Wrapper, {
|
|
63
|
+
surfaceId,
|
|
64
|
+
isStreaming: surface.status === "streaming",
|
|
65
|
+
error,
|
|
66
|
+
rejected,
|
|
67
|
+
children: surface.failed ? null : /* @__PURE__ */ jsx(SurfaceView, { surface })
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
/** The blocks of one fence: lifted prose and chat surfaces, in order. */
|
|
71
|
+
function FenceBlocks({ blocks, runtime, threadId, isStreaming, markdownProps }) {
|
|
72
|
+
const Wrapper = useContext(GenerativeUIChatSlotContext) ?? DefaultMarkdownChatSurface;
|
|
73
|
+
let surfaceCount = 0;
|
|
74
|
+
return /* @__PURE__ */ jsx(Fragment$1, { children: blocks.map((block, index) => {
|
|
75
|
+
if (block.kind === "markdown") return /* @__PURE__ */ jsx(ProviderMarkdown, {
|
|
76
|
+
...markdownProps,
|
|
77
|
+
content: block.markdown,
|
|
78
|
+
isStreaming: isStreaming && index === blocks.length - 1
|
|
79
|
+
}, `md-${surfaceCount}`);
|
|
80
|
+
surfaceCount++;
|
|
81
|
+
return /* @__PURE__ */ jsx(ChatSurface, {
|
|
82
|
+
runtime,
|
|
83
|
+
threadId,
|
|
84
|
+
surfaceId: block.surfaceId,
|
|
85
|
+
Wrapper
|
|
86
|
+
}, block.surfaceId);
|
|
87
|
+
}) });
|
|
88
|
+
}
|
|
89
|
+
/**
|
|
90
|
+
* Experimental. Renders an assistant message of the markdown transport.
|
|
91
|
+
* Prose renders as markdown. In place of each ```a2ui fence:
|
|
92
|
+
*
|
|
93
|
+
* - the fence that created a chat surface renders that surface (and the
|
|
94
|
+
* prose lifted out of it) in the chat wrapper;
|
|
95
|
+
* - every other fence renders nothing: a fence that updates the surface of
|
|
96
|
+
* an earlier fence, or a fence for a named target, which
|
|
97
|
+
* `<GenerativeUISurface id>` renders.
|
|
98
|
+
*
|
|
99
|
+
* Use it inside `<CopilotKitProvider generativeUI={{ ui, transport: "markdown" }}>`.
|
|
100
|
+
* `CopilotChatAssistantMessage` uses it there on its own.
|
|
101
|
+
*
|
|
102
|
+
* @example
|
|
103
|
+
* ```tsx
|
|
104
|
+
* <MarkdownSurfaces content={message.content} messageId={message.id} />
|
|
105
|
+
* ```
|
|
106
|
+
*/
|
|
107
|
+
function MarkdownSurfaces({ content, messageId, isStreaming = false, ...markdownProps }) {
|
|
108
|
+
const runtime = useRequiredGenerativeUIRuntime();
|
|
109
|
+
const chatThreadId = useCopilotChatConfiguration()?.threadId;
|
|
110
|
+
useManagerVersion(runtime);
|
|
111
|
+
const segments = useMemo(() => splitA2UIMarkdown(content), [content]);
|
|
112
|
+
const threadId = chatThreadId ?? runtime.manager.getActiveThread();
|
|
113
|
+
const source = getMarkdownSurfaceSource(runtime);
|
|
114
|
+
return /* @__PURE__ */ jsx(Fragment$1, { children: segments.map((segment, index) => {
|
|
115
|
+
const isLast = index === segments.length - 1;
|
|
116
|
+
if (segment.type === "markdown") return /* @__PURE__ */ jsx(ProviderMarkdown, {
|
|
117
|
+
...markdownProps,
|
|
118
|
+
content: segment.text,
|
|
119
|
+
isStreaming: isStreaming && isLast
|
|
120
|
+
}, `md-${index}`);
|
|
121
|
+
const blocks = threadId ? source.getFenceBlocks(threadId, messageId, segment.index) : void 0;
|
|
122
|
+
if (!threadId || !blocks) return null;
|
|
123
|
+
return /* @__PURE__ */ jsx(FenceBlocks, {
|
|
124
|
+
blocks,
|
|
125
|
+
runtime,
|
|
126
|
+
threadId,
|
|
127
|
+
isStreaming: isStreaming && isLast,
|
|
128
|
+
markdownProps
|
|
129
|
+
}, `a2ui-${segment.index}`);
|
|
130
|
+
}) });
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
//#endregion
|
|
134
|
+
export { MarkdownSurfaces as t };
|
|
135
|
+
//# sourceMappingURL=MarkdownSurfaces-rtE9wRUP.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"MarkdownSurfaces-rtE9wRUP.mjs","names":[],"sources":["../src/generative-ui/markdown/MarkdownSurfaces.tsx"],"sourcesContent":["import React, { useContext, useInsertionEffect, useMemo } from \"react\";\nimport type { ComponentType } from \"react\";\nimport { splitA2UIMarkdown } from \"@copilotkit/core\";\nimport { GENERATIVE_UI_ROOT_ID } from \"@copilotkit/shared\";\nimport { useCopilotChatConfiguration } from \"../../providers/CopilotChatConfigurationProvider\";\nimport { GenerativeUIChatSlotContext } from \"../chat\";\nimport type { GenerativeUIChatSurfaceProps } from \"../chat\";\nimport { useManagerVersion } from \"../hooks\";\nimport { useRequiredGenerativeUIRuntime } from \"../runtime-context\";\nimport type { GenerativeUIRuntime } from \"../runtime-context\";\nimport { SurfaceView } from \"../SurfaceView\";\nimport type { DefaultMarkdownRendererProps } from \"../../providers/MarkdownRendererContext\";\nimport { ProviderMarkdown } from \"./ProviderMarkdown\";\nimport { getMarkdownSurfaceSource } from \"@copilotkit/core/generative-ui\";\nimport type { MarkdownFenceBlock } from \"@copilotkit/core/generative-ui\";\n\n/** Config for the built-in markdown renderer, plus `className`. */\ntype MarkdownProps = DefaultMarkdownRendererProps;\n\nconst FADE_STYLE_ID = \"a2ui-markdown-fade\";\n/**\n * Components fade in as they stream: each element that appears under a\n * markdown surface runs the keyframes once on mount. React keeps the keys\n * stable per component id, so an element that is already there never\n * animates again. SVG internals are left out: a chart library re-creates\n * its nodes on each render, and a fade on each of them would read as the\n * chart flashing whenever a sibling arrives. The fade runs under reduced\n * motion too, because it moves nothing.\n */\nconst FADE_CSS = [\n // Opacity only, so it also runs when the user asks for reduced motion:\n // that setting is about movement, and a short fade is not movement.\n // The keyframes give only the start, so each element fades to its own\n // opacity (a disabled control to its dimmed one), and no fill holds the\n // end: a finished fade must not pin opacity, or an element that hides\n // itself with opacity (the map's zoom hint) stays visible forever.\n \"@keyframes a2ui-fade-in{from{opacity:0}}\",\n \"[data-a2ui-markdown-surface] .a2ui-surface *:not(svg):not(svg *){animation:a2ui-fade-in .35s ease-out backwards}\",\n].join(\"\\n\");\n\n// There is no document on the server, so nothing is added there.\nconst noEffect: typeof useInsertionEffect = () => {};\nconst useInjectEffect =\n typeof document === \"undefined\" ? noEffect : useInsertionEffect;\n\nfunction useFadeStyles() {\n useInjectEffect(() => {\n if (document.getElementById(FADE_STYLE_ID)) return;\n const style = document.createElement(\"style\");\n style.id = FADE_STYLE_ID;\n style.textContent = FADE_CSS;\n document.head.appendChild(style);\n }, []);\n}\n\n/**\n * The default wrapper of a chat surface in the markdown transport. It draws\n * no box: a surface sits in the flow of the reply like a paragraph, and a\n * Card draws its own box when it is interactive. Nothing shows while the\n * surface streams. A rejected operation gets one line.\n */\nexport function DefaultMarkdownChatSurface({\n surfaceId,\n error,\n children,\n}: GenerativeUIChatSurfaceProps) {\n useFadeStyles();\n return (\n <div\n data-generative-ui-chat-surface={surfaceId}\n data-a2ui-markdown-surface={surfaceId}\n className=\"cpk-markdown-chat-surface\"\n >\n {children}\n {error && (\n <div role=\"alert\" className=\"cpk-markdown-chat-surface-error\">\n Part of this UI couldn't be shown.\n </div>\n )}\n </div>\n );\n}\n\nexport type MarkdownSurfacesProps = MarkdownProps & {\n /** The text of the assistant message, complete or still streaming. */\n content: string;\n /** The id of the message. The fences of the message are found by it. */\n messageId: string;\n /** Whether the message is still streaming. Only its last prose shows the caret. */\n isStreaming?: boolean;\n};\n\n/**\n * One chat surface in the chat wrapper. Nothing renders until the surface\n * has its root component, so a half-streamed fence shows no empty card.\n */\nfunction ChatSurface({\n runtime,\n threadId,\n surfaceId,\n Wrapper,\n}: {\n runtime: GenerativeUIRuntime;\n threadId: string;\n surfaceId: string;\n Wrapper: ComponentType<GenerativeUIChatSurfaceProps>;\n}) {\n // A surface whose createSurface was rejected shows only the error.\n const surface =\n runtime.manager.getSurface(threadId, surfaceId) ??\n runtime.manager.getFailedSurface(threadId, surfaceId);\n const root = surface?.model.componentsModel.get(GENERATIVE_UI_ROOT_ID);\n if (!surface || (!root && !surface.failed)) return null;\n const rejected = [...surface.serverRejected, ...surface.clientRejected];\n const error = rejected.length\n ? new Error(`${rejected.length} UI update(s) could not be shown`, {\n cause: rejected,\n })\n : undefined;\n return (\n <Wrapper\n surfaceId={surfaceId}\n isStreaming={surface.status === \"streaming\"}\n error={error}\n rejected={rejected}\n >\n {surface.failed ? null : <SurfaceView surface={surface} />}\n </Wrapper>\n );\n}\n\n/** The blocks of one fence: lifted prose and chat surfaces, in order. */\nfunction FenceBlocks({\n blocks,\n runtime,\n threadId,\n isStreaming,\n markdownProps,\n}: {\n blocks: MarkdownFenceBlock[];\n runtime: GenerativeUIRuntime;\n threadId: string;\n isStreaming: boolean;\n markdownProps: MarkdownProps;\n}) {\n const Wrapper =\n useContext(GenerativeUIChatSlotContext) ?? DefaultMarkdownChatSurface;\n let surfaceCount = 0;\n return (\n <>\n {blocks.map((block, index) => {\n // Keys follow the surfaces, so a surface keeps its element when\n // prose splits off around it during streaming.\n if (block.kind === \"markdown\") {\n return (\n <ProviderMarkdown\n key={`md-${surfaceCount}`}\n {...markdownProps}\n content={block.markdown}\n isStreaming={isStreaming && index === blocks.length - 1}\n />\n );\n }\n surfaceCount++;\n return (\n <ChatSurface\n key={block.surfaceId}\n runtime={runtime}\n threadId={threadId}\n surfaceId={block.surfaceId}\n Wrapper={Wrapper}\n />\n );\n })}\n </>\n );\n}\n\n/**\n * Experimental. Renders an assistant message of the markdown transport.\n * Prose renders as markdown. In place of each ```a2ui fence:\n *\n * - the fence that created a chat surface renders that surface (and the\n * prose lifted out of it) in the chat wrapper;\n * - every other fence renders nothing: a fence that updates the surface of\n * an earlier fence, or a fence for a named target, which\n * `<GenerativeUISurface id>` renders.\n *\n * Use it inside `<CopilotKitProvider generativeUI={{ ui, transport: \"markdown\" }}>`.\n * `CopilotChatAssistantMessage` uses it there on its own.\n *\n * @example\n * ```tsx\n * <MarkdownSurfaces content={message.content} messageId={message.id} />\n * ```\n */\nexport function MarkdownSurfaces({\n content,\n messageId,\n isStreaming = false,\n ...markdownProps\n}: MarkdownSurfacesProps) {\n const runtime = useRequiredGenerativeUIRuntime();\n const chatThreadId = useCopilotChatConfiguration()?.threadId;\n useManagerVersion(runtime);\n const segments = useMemo(() => splitA2UIMarkdown(content), [content]);\n const threadId = chatThreadId ?? runtime.manager.getActiveThread();\n const source = getMarkdownSurfaceSource(runtime);\n\n return (\n <>\n {segments.map((segment, index) => {\n const isLast = index === segments.length - 1;\n if (segment.type === \"markdown\") {\n return (\n <ProviderMarkdown\n key={`md-${index}`}\n {...markdownProps}\n content={segment.text}\n isStreaming={isStreaming && isLast}\n />\n );\n }\n const blocks = threadId\n ? source.getFenceBlocks(threadId, messageId, segment.index)\n : undefined;\n if (!threadId || !blocks) return null;\n return (\n <FenceBlocks\n key={`a2ui-${segment.index}`}\n blocks={blocks}\n runtime={runtime}\n threadId={threadId}\n isStrea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