@copilotkit/react-core 2.0.0-rc.38 → 2.0.0-rc.39
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/dist/MarkdownRendererContext-BL8ct_3U.d.mts +34 -0
- package/dist/MarkdownRendererContext-BL8ct_3U.d.mts.map +1 -0
- package/dist/MarkdownRendererContext-C2J_lgqF.d.cts +34 -0
- package/dist/MarkdownRendererContext-C2J_lgqF.d.cts.map +1 -0
- package/dist/MarkdownSurfaces-Byz821bg.mjs +324 -0
- package/dist/MarkdownSurfaces-Byz821bg.mjs.map +1 -0
- package/dist/MarkdownSurfaces-D2Ck-cbb.cjs +378 -0
- package/dist/MarkdownSurfaces-D2Ck-cbb.cjs.map +1 -0
- package/dist/{MarkdownSurfaces-BQ1HGCzB.mjs → chat-Bf0vVdo7.mjs} +642 -484
- package/dist/chat-Bf0vVdo7.mjs.map +1 -0
- package/dist/{MarkdownSurfaces-BFoO6wSB.cjs → chat-D3fxo6Ah.cjs} +663 -529
- package/dist/chat-D3fxo6Ah.cjs.map +1 -0
- package/dist/{MarkdownRendererContext-CPfNVyde.d.mts → chat-DgP2N7bD.d.mts} +31 -39
- package/dist/chat-DgP2N7bD.d.mts.map +1 -0
- package/dist/{MarkdownRendererContext-iOl2pj9l.d.cts → chat-DtHs4alO.d.cts} +31 -39
- package/dist/chat-DtHs4alO.d.cts.map +1 -0
- package/dist/generative-ui/native.cjs +66 -0
- package/dist/generative-ui/native.cjs.map +1 -0
- package/dist/generative-ui/native.d.cts +155 -0
- package/dist/generative-ui/native.d.cts.map +1 -0
- package/dist/generative-ui/native.d.mts +155 -0
- package/dist/generative-ui/native.d.mts.map +1 -0
- package/dist/generative-ui/native.mjs +24 -0
- package/dist/generative-ui/native.mjs.map +1 -0
- package/dist/generative-ui.cjs +45 -70
- package/dist/generative-ui.cjs.map +1 -1
- package/dist/generative-ui.d.cts +72 -144
- package/dist/generative-ui.d.cts.map +1 -1
- package/dist/generative-ui.d.mts +72 -144
- package/dist/generative-ui.d.mts.map +1 -1
- package/dist/generative-ui.mjs +4 -29
- package/dist/generative-ui.mjs.map +1 -1
- package/dist/headless.d.cts +58 -153
- package/dist/headless.d.cts.map +1 -1
- package/dist/hooks-2Ur8NQhj.d.cts +84 -0
- package/dist/hooks-2Ur8NQhj.d.cts.map +1 -0
- package/dist/hooks-BRYonCVu.d.mts +84 -0
- package/dist/hooks-BRYonCVu.d.mts.map +1 -0
- package/dist/index.cjs +82 -430
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +4 -3
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +5 -353
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +645 -140
- package/dist/index.umd.js.map +1 -1
- package/dist/refs-BIUfn1wb.cjs +43 -0
- package/dist/refs-BIUfn1wb.cjs.map +1 -0
- package/dist/refs-CMrRm3ta.mjs +31 -0
- package/dist/refs-CMrRm3ta.mjs.map +1 -0
- package/dist/use-generative-ui-root-C12htqbK.cjs +390 -0
- package/dist/use-generative-ui-root-C12htqbK.cjs.map +1 -0
- package/dist/use-generative-ui-root-CVgsDy1f.mjs +385 -0
- package/dist/use-generative-ui-root-CVgsDy1f.mjs.map +1 -0
- package/package.json +14 -10
- package/dist/MarkdownRendererContext-CPfNVyde.d.mts.map +0 -1
- package/dist/MarkdownRendererContext-iOl2pj9l.d.cts.map +0 -1
- package/dist/MarkdownSurfaces-BFoO6wSB.cjs.map +0 -1
- package/dist/MarkdownSurfaces-BQ1HGCzB.mjs.map +0 -1
|
@@ -1,102 +1,12 @@
|
|
|
1
|
-
import { CopilotChatDefaultLabels,
|
|
2
|
-
import React, { Component, createContext,
|
|
3
|
-
import { StreamingMarkdownRenderer } from "@copilotkit/markdown/react";
|
|
4
|
-
import { CHAT_MARKDOWN_NODE_CLASS_NAMES, warnUnsupportedRichSyntaxOnce } from "@copilotkit/markdown";
|
|
1
|
+
import { CopilotChatDefaultLabels, ɵChatConfigurationStore, ɵshallowEqualRecord } from "@copilotkit/core";
|
|
2
|
+
import React, { Component, createContext, memo, useCallback, useContext, useEffect, useLayoutEffect, useMemo, useReducer, useRef, useState, useSyncExternalStore } from "react";
|
|
5
3
|
import { Fragment as Fragment$1, jsx, jsxs } from "react/jsx-runtime";
|
|
6
4
|
import { CopilotKitContext, useCopilotKit, useDefaultAgentId } from "@copilotkit/react-core/context";
|
|
7
5
|
import { A2UI_COMPONENT_ENVELOPE_KEYS, GENERATIVE_UI_ACTIVITY_TYPE, GENERATIVE_UI_CHAT_TARGET, GENERATIVE_UI_FUNCTION_CALL_ACTIVITY_TYPE, GENERATIVE_UI_RENDER_TOOL, GENERATIVE_UI_ROOT_ID, isActionBinding, isPathBinding, mapAtChildRef, ɵanySchema } from "@copilotkit/shared";
|
|
8
6
|
import * as z from "zod";
|
|
9
|
-
import { accessibilityAttributes, accessibilityAttributes as accessibilityAttributes$1, controlAria,
|
|
10
|
-
import { catalogIdsOf, createGenerativeUI, formatIssues, isAsyncIssue, issueKey, setterNameOf, themeStyleSheet, validateSchema, validateSchemaSync, withoutRejectedNulls } from "@copilotkit/shared/generative-ui";
|
|
7
|
+
import { GenerativeUISurfaceContentSchema, accessibilityAttributes, accessibilityAttributes as accessibilityAttributes$1, controlAria, invokeBoundEvent, invokeMatchedEvent, joinPath, namedRole, parseGenerativeUISurfaceContent, readAccessibility, reportErrorSafely, resolvePath, tryResolveDynamicValue, wrapperAria } from "@copilotkit/core/generative-ui";
|
|
8
|
+
import { catalogIdsOf, createGenerativeUI, formatIssues, hasOwnKey, isAsyncIssue, issueKey, setterNameOf, themeStyleSheet, toError, validateSchema, validateSchemaSync, withCause, withCause as withCause$1, withoutRejectedNulls } from "@copilotkit/shared/generative-ui";
|
|
11
9
|
|
|
12
|
-
//#region src/components/chat/StreamingMarkdownDefaultRenderer.tsx
|
|
13
|
-
/**
|
|
14
|
-
* react-core's built-in default markdown renderer — a thin adapter over
|
|
15
|
-
* `@copilotkit/markdown`'s streaming renderer. Maps the pluggable
|
|
16
|
-
* renderer contract (`{ content, isStreaming, className }`) onto the streaming
|
|
17
|
-
* renderer for streaming-safe incremental rendering + per-token animation.
|
|
18
|
-
* Still overridable via the slot/provider pluggable interface.
|
|
19
|
-
*/
|
|
20
|
-
function StreamingMarkdownDefaultRenderer({ content, isStreaming, className, nodeRenderers, caret, options, onLinkClick, onCitationClick }) {
|
|
21
|
-
if (!content) return null;
|
|
22
|
-
warnUnsupportedRichSyntaxOnce(content);
|
|
23
|
-
return /* @__PURE__ */ jsx(StreamingMarkdownRenderer, {
|
|
24
|
-
className,
|
|
25
|
-
isComplete: !isStreaming,
|
|
26
|
-
caret: caret ?? !!isStreaming,
|
|
27
|
-
nodeRenderers,
|
|
28
|
-
nodeClassNames: CHAT_MARKDOWN_NODE_CLASS_NAMES,
|
|
29
|
-
options,
|
|
30
|
-
onLinkClick,
|
|
31
|
-
onCitationClick,
|
|
32
|
-
children: content
|
|
33
|
-
});
|
|
34
|
-
}
|
|
35
|
-
StreamingMarkdownDefaultRenderer.displayName = "StreamingMarkdownDefaultRenderer";
|
|
36
|
-
|
|
37
|
-
//#endregion
|
|
38
|
-
//#region src/components/icons/CopilotIcon.tsx
|
|
39
|
-
/** Turns SVG attribute names such as `stroke-width` into React's `strokeWidth`. */
|
|
40
|
-
function toReactAttributes(attributes) {
|
|
41
|
-
return Object.fromEntries(Object.entries(attributes).map(([key, value]) => [key.replace(/-([a-z])/g, (_match, letter) => letter.toUpperCase()), value]));
|
|
42
|
-
}
|
|
43
|
-
const svgDefaults = toReactAttributes(ɵcopilotIconSvgAttributes);
|
|
44
|
-
/** True when the props give the icon an accessible name or role. */
|
|
45
|
-
function hasAccessibleProp(props) {
|
|
46
|
-
return Object.keys(props).some((key) => key.startsWith("aria-") || key === "role" || key === "title");
|
|
47
|
-
}
|
|
48
|
-
/**
|
|
49
|
-
* Internal. Draws a Lucide icon from its node data, the way Lucide's React package
|
|
50
|
-
* does: Lucide's default SVG attributes, the `lucide lucide-<name>` classes,
|
|
51
|
-
* and `aria-hidden="true"` when no `aria-*`, `role` or `title` prop names it.
|
|
52
|
-
*/
|
|
53
|
-
const IconSvg = forwardRef(function IconSvg({ lucideName, nodes, size = 24, color = "currentColor", strokeWidth = 2, className, children, ...props }, ref) {
|
|
54
|
-
const isDecorative = !children && !hasAccessibleProp(props);
|
|
55
|
-
return /* @__PURE__ */ jsxs("svg", {
|
|
56
|
-
ref,
|
|
57
|
-
...svgDefaults,
|
|
58
|
-
width: size,
|
|
59
|
-
height: size,
|
|
60
|
-
stroke: color,
|
|
61
|
-
strokeWidth,
|
|
62
|
-
className: cn("lucide", `lucide-${lucideName}`, className),
|
|
63
|
-
"aria-hidden": isDecorative || void 0,
|
|
64
|
-
...props,
|
|
65
|
-
children: [nodes.map((node, index) => createElement(node.tag, {
|
|
66
|
-
key: index,
|
|
67
|
-
...node.attributes
|
|
68
|
-
})), children]
|
|
69
|
-
});
|
|
70
|
-
});
|
|
71
|
-
/**
|
|
72
|
-
* Draws one of the chat icons as an inline `<svg>`.
|
|
73
|
-
*
|
|
74
|
-
* The `<svg>` has the classes `lucide lucide-<icon name>`, so CSS that selects
|
|
75
|
-
* them (for example `svg.lucide-arrow-up`) keeps working. The icon is hidden
|
|
76
|
-
* from screen readers unless you give it an `aria-label`, `role` or `title`.
|
|
77
|
-
*
|
|
78
|
-
* @param props.name - The chat icon, for example `"ArrowUp"` or `"X"`.
|
|
79
|
-
* @param props.size - Width and height. Defaults to `24`.
|
|
80
|
-
* @param props.strokeWidth - Line width. Defaults to `2`.
|
|
81
|
-
* @param props.color - Line color. Defaults to `currentColor`.
|
|
82
|
-
*
|
|
83
|
-
* @example
|
|
84
|
-
* ```tsx
|
|
85
|
-
* <CopilotIcon name="ArrowUp" className="cpk-chat-input-send-icon" />
|
|
86
|
-
* <CopilotIcon name="X" aria-label="Close" />
|
|
87
|
-
* ```
|
|
88
|
-
*/
|
|
89
|
-
const CopilotIcon = forwardRef(function CopilotIcon({ name, ...props }, ref) {
|
|
90
|
-
const icon = ɵcopilotIcons[name];
|
|
91
|
-
return /* @__PURE__ */ jsx(IconSvg, {
|
|
92
|
-
ref,
|
|
93
|
-
lucideName: icon.name,
|
|
94
|
-
nodes: icon.nodes,
|
|
95
|
-
...props
|
|
96
|
-
});
|
|
97
|
-
});
|
|
98
|
-
|
|
99
|
-
//#endregion
|
|
100
10
|
//#region src/lib/shallow-stable-ref.ts
|
|
101
11
|
/**
|
|
102
12
|
* Shallow equality comparison for objects: same own keys, each holding the
|
|
@@ -274,84 +184,6 @@ const ControlledModalOpenScope = ({ children, open, onOpenChange }) => {
|
|
|
274
184
|
};
|
|
275
185
|
ControlledModalOpenScope.displayName = "ControlledModalOpenScope";
|
|
276
186
|
|
|
277
|
-
//#endregion
|
|
278
|
-
//#region src/lib/slots.tsx
|
|
279
|
-
/**
|
|
280
|
-
* Check if a value is a React component type (function, class, forwardRef, memo, etc.)
|
|
281
|
-
*/
|
|
282
|
-
function isReactComponentType(value) {
|
|
283
|
-
if (typeof value === "function") return true;
|
|
284
|
-
if (value && typeof value === "object" && "$$typeof" in value && !React.isValidElement(value)) return true;
|
|
285
|
-
return false;
|
|
286
|
-
}
|
|
287
|
-
/**
|
|
288
|
-
* Internal function to render a slot value as a React element (non-memoized).
|
|
289
|
-
*/
|
|
290
|
-
function renderSlotElement(slot, DefaultComponent, props) {
|
|
291
|
-
if (typeof slot === "string") {
|
|
292
|
-
const existingClassName = props.className;
|
|
293
|
-
return React.createElement(DefaultComponent, {
|
|
294
|
-
...props,
|
|
295
|
-
className: cn(existingClassName, slot)
|
|
296
|
-
});
|
|
297
|
-
}
|
|
298
|
-
if (isReactComponentType(slot)) return React.createElement(slot, props);
|
|
299
|
-
if (slot && typeof slot === "object" && !React.isValidElement(slot)) return React.createElement(DefaultComponent, {
|
|
300
|
-
...props,
|
|
301
|
-
...slot
|
|
302
|
-
});
|
|
303
|
-
return React.createElement(DefaultComponent, props);
|
|
304
|
-
}
|
|
305
|
-
/**
|
|
306
|
-
* Internal memoized wrapper component for renderSlot.
|
|
307
|
-
* Uses forwardRef to support ref forwarding.
|
|
308
|
-
*/
|
|
309
|
-
const MemoizedSlotWrapper = React.memo(React.forwardRef(function MemoizedSlotWrapper(props, ref) {
|
|
310
|
-
const { $slot, $component, ...rest } = props;
|
|
311
|
-
return renderSlotElement($slot, $component, ref !== null ? {
|
|
312
|
-
...rest,
|
|
313
|
-
ref
|
|
314
|
-
} : rest);
|
|
315
|
-
}), (prev, next) => {
|
|
316
|
-
if (prev.$slot !== next.$slot) return false;
|
|
317
|
-
if (prev.$component !== next.$component) return false;
|
|
318
|
-
const { $slot: _ps, $component: _pc, ...prevRest } = prev;
|
|
319
|
-
const { $slot: _ns, $component: _nc, ...nextRest } = next;
|
|
320
|
-
return shallowEqual(prevRest, nextRest);
|
|
321
|
-
});
|
|
322
|
-
/**
|
|
323
|
-
* Renders a slot value as a memoized React element.
|
|
324
|
-
* Automatically prevents unnecessary re-renders using shallow prop comparison.
|
|
325
|
-
* Supports ref forwarding.
|
|
326
|
-
*
|
|
327
|
-
* @example
|
|
328
|
-
* renderSlot(customInput, CopilotChatInput, { onSubmit: handleSubmit })
|
|
329
|
-
*/
|
|
330
|
-
function renderSlot(slot, DefaultComponent, props) {
|
|
331
|
-
return React.createElement(MemoizedSlotWrapper, {
|
|
332
|
-
...props,
|
|
333
|
-
$slot: slot,
|
|
334
|
-
$component: DefaultComponent
|
|
335
|
-
});
|
|
336
|
-
}
|
|
337
|
-
|
|
338
|
-
//#endregion
|
|
339
|
-
//#region src/providers/MarkdownRendererContext.tsx
|
|
340
|
-
const MarkdownRendererContext = createContext(void 0);
|
|
341
|
-
const MarkdownRendererProvider = ({ renderer, children }) => /* @__PURE__ */ jsx(MarkdownRendererContext.Provider, {
|
|
342
|
-
value: renderer,
|
|
343
|
-
children
|
|
344
|
-
});
|
|
345
|
-
/**
|
|
346
|
-
* The markdown renderer set at the provider level, or `undefined` when none was
|
|
347
|
-
* supplied. Message components fall back to the built-in
|
|
348
|
-
* `StreamingMarkdownDefaultRenderer` (via `CopilotChatAssistantMessage.MarkdownRenderer`)
|
|
349
|
-
* when this is `undefined`.
|
|
350
|
-
*/
|
|
351
|
-
function useMarkdownRenderer() {
|
|
352
|
-
return useContext(MarkdownRendererContext);
|
|
353
|
-
}
|
|
354
|
-
|
|
355
187
|
//#endregion
|
|
356
188
|
//#region src/types/defineToolCallRenderer.ts
|
|
357
189
|
function defineToolCallRenderer(def) {
|
|
@@ -365,54 +197,6 @@ function defineToolCallRenderer(def) {
|
|
|
365
197
|
};
|
|
366
198
|
}
|
|
367
199
|
|
|
368
|
-
//#endregion
|
|
369
|
-
//#region src/generative-ui/catalog.ts
|
|
370
|
-
/**
|
|
371
|
-
* Experimental. Declares the components the model can render, the app state
|
|
372
|
-
* they read, and the functions they call.
|
|
373
|
-
*
|
|
374
|
-
* - A prop with the name of a top-level state key is bound to that state.
|
|
375
|
-
* - `bind: { prop: (state, props) => value }` binds a prop to a selector.
|
|
376
|
-
* - An event binds to the function with its name, else to the name without
|
|
377
|
-
* `on` (`onAddToCart` calls `addToCart`).
|
|
378
|
-
* - `writable: ["value"]` gives the component a `setValue` prop. See
|
|
379
|
-
* `GenerativeUIComponentDefinition.writable`.
|
|
380
|
-
*
|
|
381
|
-
* The model does not see bound props or matched events.
|
|
382
|
-
*
|
|
383
|
-
* @param config - `id`, then optional `extends`, `state`, `functions` and
|
|
384
|
-
* `overrides`, then `components`. Put `state` before `components`, so the selectors
|
|
385
|
-
* get typed state.
|
|
386
|
-
* @throws Error when a name comes from two places, an override names no
|
|
387
|
-
* component of the bases, `props` is not an object
|
|
388
|
-
* schema with a JSON Schema, a key is reserved or used
|
|
389
|
-
* twice on a component, an event matches two functions, an unmatched event
|
|
390
|
-
* has no `parameters`, a `bind` key is not a prop, a `writable` key is not
|
|
391
|
-
* a prop or is a bound prop, or a setter name is already a key of the
|
|
392
|
-
* component. Also throws when `parameterOrder` contains duplicate or unknown
|
|
393
|
-
* keys, or an ordered component declares integer-index keys whose JSON object
|
|
394
|
-
* enumeration order cannot be controlled.
|
|
395
|
-
*
|
|
396
|
-
* @example
|
|
397
|
-
* ```ts
|
|
398
|
-
* const ui = createGenerativeUI({
|
|
399
|
-
* id: "acme/store@1",
|
|
400
|
-
* state: { cart: cartSchema },
|
|
401
|
-
* functions: { addToCart: { parameters: z.object({ productId: z.string() }) } },
|
|
402
|
-
* components: {
|
|
403
|
-
* CartSummary: {
|
|
404
|
-
* component: CartSummary,
|
|
405
|
-
* props: z.object({ count: z.number() }),
|
|
406
|
-
* bind: { count: (state) => state.cart.count },
|
|
407
|
-
* },
|
|
408
|
-
* },
|
|
409
|
-
* });
|
|
410
|
-
* ```
|
|
411
|
-
*/
|
|
412
|
-
function createGenerativeUI$1(config) {
|
|
413
|
-
return createGenerativeUI(config);
|
|
414
|
-
}
|
|
415
|
-
|
|
416
200
|
//#endregion
|
|
417
201
|
//#region src/generative-ui/runtime-context.ts
|
|
418
202
|
const GenerativeUIRuntimeContext = createContext(null);
|
|
@@ -421,7 +205,7 @@ function useGenerativeUIRuntime() {
|
|
|
421
205
|
}
|
|
422
206
|
function useRequiredGenerativeUIRuntime() {
|
|
423
207
|
const runtime = useContext(GenerativeUIRuntimeContext);
|
|
424
|
-
if (!runtime) throw new Error("Generative UI components must be rendered inside <CopilotKitProvider generativeUI={{ ui }}
|
|
208
|
+
if (!runtime) throw new Error("Generative UI components must be rendered inside a generative UI provider: <CopilotKitProvider generativeUI={{ ui }}> on the web, or <GenerativeUIProvider> from @copilotkit/react-native/generative-ui on React Native.");
|
|
425
209
|
return runtime;
|
|
426
210
|
}
|
|
427
211
|
const GenerativeUINodeContext = createContext(null);
|
|
@@ -514,7 +298,136 @@ function useGenerativeUIFunction(ui, name, handler) {
|
|
|
514
298
|
}
|
|
515
299
|
|
|
516
300
|
//#endregion
|
|
517
|
-
//#region src/generative-ui/
|
|
301
|
+
//#region src/generative-ui/activity.ts
|
|
302
|
+
/**
|
|
303
|
+
* The rejected operations of a surface (the runtime's first, then the
|
|
304
|
+
* client's) and the error that goes with them, for a wrapper: a generic
|
|
305
|
+
* message, with the same list as its `cause`. The `cause` is not enumerable,
|
|
306
|
+
* so `JSON.stringify`, a spread and error reporters leave the list out.
|
|
307
|
+
* `clientRejected` is appended to in place by the manager, so its length is
|
|
308
|
+
* part of the key.
|
|
309
|
+
*/
|
|
310
|
+
function useGenerativeUIRejected(serverRejected, clientRejected) {
|
|
311
|
+
const rejected = useMemo(() => [...serverRejected ?? [], ...clientRejected ?? []], [
|
|
312
|
+
serverRejected,
|
|
313
|
+
clientRejected,
|
|
314
|
+
clientRejected?.length ?? 0
|
|
315
|
+
]);
|
|
316
|
+
return {
|
|
317
|
+
rejected,
|
|
318
|
+
error: useMemo(() => rejected.length ? withCause(/* @__PURE__ */ new Error(`${rejected.length} UI update(s) could not be shown`), rejected) : void 0, [rejected])
|
|
319
|
+
};
|
|
320
|
+
}
|
|
321
|
+
/**
|
|
322
|
+
* What a renderer of one surface activity message shows: the surface (or the
|
|
323
|
+
* failed surface whose `createSurface` was rejected), and its rejected
|
|
324
|
+
* operations with their error (see {@link useGenerativeUIRejected}). It
|
|
325
|
+
* ingests `content` into the manager (idempotent: the root ingests the
|
|
326
|
+
* agent's messages too, and this covers an agent the root has not seen yet),
|
|
327
|
+
* and renders again on every change of the manager.
|
|
328
|
+
*
|
|
329
|
+
* `surface` is undefined when there is no runtime, thread or content, when
|
|
330
|
+
* the surface's target is not `target`, or when the surface does not exist
|
|
331
|
+
* (the agent deleted it). Both rejected lists cover only the latest
|
|
332
|
+
* `render_ui` call (see the manager's ingest).
|
|
333
|
+
*/
|
|
334
|
+
function useGenerativeUIActivitySurface({ runtime, threadId, agentId, content, target }) {
|
|
335
|
+
useManagerVersion(runtime);
|
|
336
|
+
useLayoutEffect(() => {
|
|
337
|
+
if (runtime && threadId && content) runtime.manager.ingest(threadId, agentId, content);
|
|
338
|
+
}, [
|
|
339
|
+
runtime,
|
|
340
|
+
threadId,
|
|
341
|
+
agentId,
|
|
342
|
+
content
|
|
343
|
+
]);
|
|
344
|
+
const surface = runtime && threadId && content && content.target === target ? runtime.manager.getSurface(threadId, content.surfaceId) ?? runtime.manager.getFailedSurface(threadId, content.surfaceId) : void 0;
|
|
345
|
+
const { rejected, error } = useGenerativeUIRejected(content?.rejected, surface?.clientRejected);
|
|
346
|
+
return {
|
|
347
|
+
surface,
|
|
348
|
+
rejected,
|
|
349
|
+
error
|
|
350
|
+
};
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
//#endregion
|
|
354
|
+
//#region src/generative-ui/catalog.ts
|
|
355
|
+
/**
|
|
356
|
+
* Experimental. Declares the components the model can render, the app state
|
|
357
|
+
* they read, and the functions they call.
|
|
358
|
+
*
|
|
359
|
+
* - A prop with the name of a top-level state key is bound to that state.
|
|
360
|
+
* - `bind: { prop: (state, props) => value }` binds a prop to a selector.
|
|
361
|
+
* - An event binds to the function with its name, else to the name without
|
|
362
|
+
* `on` (`onAddToCart` calls `addToCart`).
|
|
363
|
+
* - `writable: ["value"]` gives the component a `setValue` prop. See
|
|
364
|
+
* `GenerativeUIComponentDefinition.writable`.
|
|
365
|
+
*
|
|
366
|
+
* The model does not see bound props or matched events.
|
|
367
|
+
*
|
|
368
|
+
* @param config - `id`, then optional `extends`, `state`, `functions` and
|
|
369
|
+
* `overrides`, then `components`. Put `state` before `components`, so the selectors
|
|
370
|
+
* get typed state.
|
|
371
|
+
* @throws Error when a name comes from two places, an override names no
|
|
372
|
+
* component of the bases, `props` is not an object
|
|
373
|
+
* schema with a JSON Schema, a key is reserved or used
|
|
374
|
+
* twice on a component, an event matches two functions, an unmatched event
|
|
375
|
+
* has no `parameters`, a `bind` key is not a prop, a `writable` key is not
|
|
376
|
+
* a prop or is a bound prop, or a setter name is already a key of the
|
|
377
|
+
* component. Also throws when `parameterOrder` contains duplicate or unknown
|
|
378
|
+
* keys, or an ordered component declares integer-index keys whose JSON object
|
|
379
|
+
* enumeration order cannot be controlled.
|
|
380
|
+
*
|
|
381
|
+
* @example
|
|
382
|
+
* ```ts
|
|
383
|
+
* const ui = createGenerativeUI({
|
|
384
|
+
* id: "acme/store@1",
|
|
385
|
+
* state: { cart: cartSchema },
|
|
386
|
+
* functions: { addToCart: { parameters: z.object({ productId: z.string() }) } },
|
|
387
|
+
* components: {
|
|
388
|
+
* CartSummary: {
|
|
389
|
+
* component: CartSummary,
|
|
390
|
+
* props: z.object({ count: z.number() }),
|
|
391
|
+
* bind: { count: (state) => state.cart.count },
|
|
392
|
+
* },
|
|
393
|
+
* },
|
|
394
|
+
* });
|
|
395
|
+
* ```
|
|
396
|
+
*/
|
|
397
|
+
function createGenerativeUI$1(config) {
|
|
398
|
+
return createGenerativeUI(config);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
//#endregion
|
|
402
|
+
//#region src/generative-ui/host.tsx
|
|
403
|
+
const DISPLAY_CONTENTS = { display: "contents" };
|
|
404
|
+
/**
|
|
405
|
+
* The root element is always there, so a theme change does not remount the
|
|
406
|
+
* surface. `display: contents` keeps it out of the layout, and the CSS
|
|
407
|
+
* variables still reach every component of the surface. The `a2ui-surface`
|
|
408
|
+
* class scopes the standard catalog's surface CSS rules. The theme is a
|
|
409
|
+
* stylesheet, not an inline style, so that its `dark` values can apply in
|
|
410
|
+
* dark mode only. The values are checked in `themeStyleSheet`, and the HTML
|
|
411
|
+
* is set as is, so the server render keeps the quotes of the rule.
|
|
412
|
+
*/
|
|
413
|
+
function DomSurfaceRoot({ surfaceId, theme, children }) {
|
|
414
|
+
const sheet = React.useMemo(() => themeStyleSheet(theme), [theme]);
|
|
415
|
+
return /* @__PURE__ */ jsxs("div", {
|
|
416
|
+
className: "a2ui-surface",
|
|
417
|
+
"data-generative-ui-root": surfaceId,
|
|
418
|
+
"data-a2ui-theme": sheet?.id,
|
|
419
|
+
style: DISPLAY_CONTENTS,
|
|
420
|
+
children: [sheet ? /* @__PURE__ */ jsx("style", { dangerouslySetInnerHTML: { __html: sheet.css } }) : null, children]
|
|
421
|
+
});
|
|
422
|
+
}
|
|
423
|
+
function DomAccessibilityWrapper({ attributes, children }) {
|
|
424
|
+
return /* @__PURE__ */ jsx("span", {
|
|
425
|
+
"data-a2ui-accessibility": "",
|
|
426
|
+
style: DISPLAY_CONTENTS,
|
|
427
|
+
...attributes,
|
|
428
|
+
children
|
|
429
|
+
});
|
|
430
|
+
}
|
|
518
431
|
function DefaultNodeFallback({ message, componentId, retry }) {
|
|
519
432
|
return /* @__PURE__ */ jsxs("div", {
|
|
520
433
|
role: "alert",
|
|
@@ -542,6 +455,23 @@ function DefaultNodeFallback({ message, componentId, retry }) {
|
|
|
542
455
|
]
|
|
543
456
|
});
|
|
544
457
|
}
|
|
458
|
+
/** The DOM host: what a surface draws on the web. */
|
|
459
|
+
const domGenerativeUIHost = {
|
|
460
|
+
SurfaceRoot: DomSurfaceRoot,
|
|
461
|
+
AccessibilityWrapper: DomAccessibilityWrapper,
|
|
462
|
+
NodeFallback: DefaultNodeFallback
|
|
463
|
+
};
|
|
464
|
+
/**
|
|
465
|
+
* The host elements of the surfaces below. Defaults to the DOM host, so a
|
|
466
|
+
* web app never sets it.
|
|
467
|
+
*/
|
|
468
|
+
const GenerativeUIHostContext = createContext(domGenerativeUIHost);
|
|
469
|
+
function useGenerativeUIHost() {
|
|
470
|
+
return useContext(GenerativeUIHostContext);
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
//#endregion
|
|
474
|
+
//#region src/generative-ui/boundaries.tsx
|
|
545
475
|
/**
|
|
546
476
|
* A child that has not streamed yet renders nothing. The prose keeps
|
|
547
477
|
* streaming above the surface, and each component appears as soon as it is
|
|
@@ -550,14 +480,181 @@ function DefaultNodeFallback({ message, componentId, retry }) {
|
|
|
550
480
|
function NodePlaceholder(_props) {
|
|
551
481
|
return null;
|
|
552
482
|
}
|
|
553
|
-
/**
|
|
483
|
+
/**
|
|
484
|
+
* The error that a fallback threw, told apart from the error of the
|
|
485
|
+
* component it stood in for. The fallback's own error is the `cause`.
|
|
486
|
+
*/
|
|
487
|
+
function fallbackError(error, componentId) {
|
|
488
|
+
const subject = componentId === void 0 ? "the surface" : componentId;
|
|
489
|
+
return withCause$1(/* @__PURE__ */ new Error(`The error fallback of ${subject} threw: ${error.message}`), error);
|
|
490
|
+
}
|
|
491
|
+
function createFailureLog() {
|
|
492
|
+
const last = /* @__PURE__ */ new Map();
|
|
493
|
+
const released = /* @__PURE__ */ new Set();
|
|
494
|
+
const releasedSinceSettle = /* @__PURE__ */ new Set();
|
|
495
|
+
const heldBack = /* @__PURE__ */ new Map();
|
|
496
|
+
return {
|
|
497
|
+
report(runtime, slot, info, context = "") {
|
|
498
|
+
released.delete(slot);
|
|
499
|
+
const signature = [
|
|
500
|
+
info.kind,
|
|
501
|
+
info.error.name,
|
|
502
|
+
info.error.message,
|
|
503
|
+
context
|
|
504
|
+
].join("\n");
|
|
505
|
+
if (last.get(slot) === signature) {
|
|
506
|
+
if (releasedSinceSettle.has(slot)) heldBack.set(slot, {
|
|
507
|
+
runtime,
|
|
508
|
+
info,
|
|
509
|
+
signature
|
|
510
|
+
});
|
|
511
|
+
return;
|
|
512
|
+
}
|
|
513
|
+
heldBack.delete(slot);
|
|
514
|
+
last.set(slot, signature);
|
|
515
|
+
reportErrorSafely(runtime, info);
|
|
516
|
+
},
|
|
517
|
+
clear(slot) {
|
|
518
|
+
released.delete(slot);
|
|
519
|
+
last.delete(slot);
|
|
520
|
+
heldBack.delete(slot);
|
|
521
|
+
},
|
|
522
|
+
retain(slot) {
|
|
523
|
+
released.delete(slot);
|
|
524
|
+
},
|
|
525
|
+
release(slot) {
|
|
526
|
+
heldBack.delete(slot);
|
|
527
|
+
if (!last.has(slot)) return;
|
|
528
|
+
released.add(slot);
|
|
529
|
+
releasedSinceSettle.add(slot);
|
|
530
|
+
},
|
|
531
|
+
settle(missed = false) {
|
|
532
|
+
for (const slot of missed ? releasedSinceSettle : released) last.delete(slot);
|
|
533
|
+
if (missed) for (const [slot, { runtime, info, signature }] of heldBack) {
|
|
534
|
+
if (!releasedSinceSettle.has(slot) || last.has(slot)) continue;
|
|
535
|
+
last.set(slot, signature);
|
|
536
|
+
reportErrorSafely(runtime, info);
|
|
537
|
+
}
|
|
538
|
+
heldBack.clear();
|
|
539
|
+
released.clear();
|
|
540
|
+
releasedSinceSettle.clear();
|
|
541
|
+
}
|
|
542
|
+
};
|
|
543
|
+
}
|
|
544
|
+
/** Outside a surface, every failure is reported. */
|
|
545
|
+
const UNLOGGED = {
|
|
546
|
+
report: (runtime, _slot, info) => reportErrorSafely(runtime, info),
|
|
547
|
+
clear: () => {},
|
|
548
|
+
retain: () => {},
|
|
549
|
+
release: () => {},
|
|
550
|
+
settle: () => {}
|
|
551
|
+
};
|
|
552
|
+
/** The failure log of the surface being rendered; set by `SurfaceView`. */
|
|
553
|
+
const GenerativeUIFailureLogContext = createContext(UNLOGGED);
|
|
554
|
+
function useFailureLog() {
|
|
555
|
+
return useContext(GenerativeUIFailureLogContext);
|
|
556
|
+
}
|
|
557
|
+
/**
|
|
558
|
+
* The slots of a failure log, per node place (one rendered copy of a node,
|
|
559
|
+
* see `placeOf` in SurfaceView) and surface boundary.
|
|
560
|
+
*/
|
|
561
|
+
const failureSlots = {
|
|
562
|
+
node: (place) => `node ${place}`,
|
|
563
|
+
appFallback: (place) => `app-fallback ${place}`,
|
|
564
|
+
renderFallback: (place) => `render-fallback ${place}`,
|
|
565
|
+
fallback: (place) => `fallback ${place}`,
|
|
566
|
+
cycle: (place) => `cycle ${place}`,
|
|
567
|
+
props: (place) => `props ${place}`,
|
|
568
|
+
surface: (level) => `surface ${level}`,
|
|
569
|
+
surfaceFallback: (level) => `surface-fallback ${level}`
|
|
570
|
+
};
|
|
571
|
+
function sameKeys(a, b) {
|
|
572
|
+
return a.length === b.length && a.every((value, i) => Object.is(value, b[i]));
|
|
573
|
+
}
|
|
574
|
+
var FallbackGuard = class extends Component {
|
|
575
|
+
state = {
|
|
576
|
+
failed: false,
|
|
577
|
+
resetKeys: this.props.resetKeys
|
|
578
|
+
};
|
|
579
|
+
static getDerivedStateFromError(thrown) {
|
|
580
|
+
return {
|
|
581
|
+
failed: true,
|
|
582
|
+
error: toError(thrown)
|
|
583
|
+
};
|
|
584
|
+
}
|
|
585
|
+
static getDerivedStateFromProps(props, state) {
|
|
586
|
+
return sameKeys(props.resetKeys, state.resetKeys) ? null : {
|
|
587
|
+
failed: false,
|
|
588
|
+
error: void 0,
|
|
589
|
+
resetKeys: props.resetKeys
|
|
590
|
+
};
|
|
591
|
+
}
|
|
592
|
+
report(error) {
|
|
593
|
+
const { runtime, surfaceId, componentId, log, slot, context } = this.props;
|
|
594
|
+
log.report(runtime, slot, {
|
|
595
|
+
kind: "render",
|
|
596
|
+
surfaceId,
|
|
597
|
+
...componentId === void 0 ? {} : { componentId },
|
|
598
|
+
error: fallbackError(error, componentId)
|
|
599
|
+
}, context);
|
|
600
|
+
}
|
|
601
|
+
componentDidCatch(thrown) {
|
|
602
|
+
this.report(this.state.error ?? toError(thrown));
|
|
603
|
+
}
|
|
604
|
+
componentDidMount() {
|
|
605
|
+
if (!this.state.failed) {
|
|
606
|
+
this.props.log.clear(this.props.slot);
|
|
607
|
+
return;
|
|
608
|
+
}
|
|
609
|
+
if (this.state.error) this.report(this.state.error);
|
|
610
|
+
}
|
|
611
|
+
componentDidUpdate() {
|
|
612
|
+
if (!this.state.failed) this.props.log.clear(this.props.slot);
|
|
613
|
+
}
|
|
614
|
+
render() {
|
|
615
|
+
return this.state.failed ? this.props.replacement ?? null : this.props.children;
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
/**
|
|
619
|
+
* The host's `NodeFallback`, guarded: when it throws, the node renders
|
|
620
|
+
* nothing and the error is reported, and the parent node keeps rendering.
|
|
621
|
+
* Every place that shows the host fallback goes through this.
|
|
622
|
+
*/
|
|
623
|
+
function GuardedNodeFallback({ surfaceId, componentId, runtime, message, retry, slot, revision }) {
|
|
624
|
+
const { NodeFallback } = useGenerativeUIHost();
|
|
625
|
+
return /* @__PURE__ */ jsx(FallbackGuard, {
|
|
626
|
+
surfaceId,
|
|
627
|
+
componentId,
|
|
628
|
+
runtime,
|
|
629
|
+
log: useFailureLog(),
|
|
630
|
+
slot,
|
|
631
|
+
context: message,
|
|
632
|
+
resetKeys: [message, revision],
|
|
633
|
+
children: /* @__PURE__ */ jsx(NodeFallback, {
|
|
634
|
+
message,
|
|
635
|
+
componentId: componentId ?? "surface",
|
|
636
|
+
retry
|
|
637
|
+
})
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
/**
|
|
641
|
+
* Calls the app's `errorBoundary.fallback` in a render of its own, so that
|
|
642
|
+
* the guard above it catches what it throws.
|
|
643
|
+
*/
|
|
644
|
+
function AppErrorFallback({ render, ...props }) {
|
|
645
|
+
return /* @__PURE__ */ jsx(Fragment$1, { children: render(props) });
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Per-node boundary. Resets on retry or a new agent revision, not on data
|
|
649
|
+
* ticks. A failure that stays the same across revisions is reported once.
|
|
650
|
+
*/
|
|
554
651
|
var NodeBoundary = class extends Component {
|
|
555
652
|
state = {
|
|
556
653
|
error: null,
|
|
557
654
|
revision: this.props.revision
|
|
558
655
|
};
|
|
559
|
-
static getDerivedStateFromError(
|
|
560
|
-
return { error };
|
|
656
|
+
static getDerivedStateFromError(thrown) {
|
|
657
|
+
return { error: toError(thrown) };
|
|
561
658
|
}
|
|
562
659
|
static getDerivedStateFromProps(props, state) {
|
|
563
660
|
return props.revision !== state.revision ? {
|
|
@@ -565,51 +662,136 @@ var NodeBoundary = class extends Component {
|
|
|
565
662
|
revision: props.revision
|
|
566
663
|
} : null;
|
|
567
664
|
}
|
|
568
|
-
componentDidCatch(
|
|
569
|
-
const { runtime, surfaceId, componentId } = this.props;
|
|
570
|
-
|
|
665
|
+
componentDidCatch(thrown) {
|
|
666
|
+
const { runtime, surfaceId, componentId, log, place } = this.props;
|
|
667
|
+
log.clear(failureSlots.fallback(place));
|
|
668
|
+
log.report(runtime, failureSlots.node(place), {
|
|
669
|
+
kind: "render",
|
|
670
|
+
surfaceId,
|
|
671
|
+
componentId,
|
|
672
|
+
error: this.state.error ?? toError(thrown)
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
slots() {
|
|
676
|
+
const { place } = this.props;
|
|
677
|
+
return [
|
|
678
|
+
failureSlots.node(place),
|
|
679
|
+
failureSlots.appFallback(place),
|
|
680
|
+
failureSlots.renderFallback(place)
|
|
681
|
+
];
|
|
682
|
+
}
|
|
683
|
+
clearSlots() {
|
|
684
|
+
for (const slot of this.slots()) this.props.log.clear(slot);
|
|
685
|
+
}
|
|
686
|
+
componentDidMount() {
|
|
687
|
+
for (const slot of this.slots()) this.props.log.retain(slot);
|
|
688
|
+
if (!this.state.error) {
|
|
689
|
+
this.clearSlots();
|
|
690
|
+
return;
|
|
691
|
+
}
|
|
692
|
+
const { runtime, surfaceId, componentId, log, place } = this.props;
|
|
693
|
+
log.report(runtime, failureSlots.node(place), {
|
|
571
694
|
kind: "render",
|
|
572
695
|
surfaceId,
|
|
573
696
|
componentId,
|
|
574
|
-
error
|
|
697
|
+
error: this.state.error
|
|
575
698
|
});
|
|
576
699
|
}
|
|
700
|
+
componentDidUpdate(prevProps) {
|
|
701
|
+
if (prevProps.place !== this.props.place) {
|
|
702
|
+
const { log } = this.props;
|
|
703
|
+
log.release(failureSlots.node(prevProps.place));
|
|
704
|
+
log.release(failureSlots.appFallback(prevProps.place));
|
|
705
|
+
log.release(failureSlots.renderFallback(prevProps.place));
|
|
706
|
+
for (const slot of this.slots()) log.retain(slot);
|
|
707
|
+
}
|
|
708
|
+
if (!this.state.error) this.clearSlots();
|
|
709
|
+
}
|
|
710
|
+
componentWillUnmount() {
|
|
711
|
+
for (const slot of this.slots()) this.props.log.release(slot);
|
|
712
|
+
}
|
|
577
713
|
retry = () => this.setState({ error: null });
|
|
578
714
|
render() {
|
|
579
715
|
const { error } = this.state;
|
|
580
716
|
if (!error) return this.props.children;
|
|
581
|
-
const { runtime, surfaceId, componentId } = this.props;
|
|
582
|
-
const
|
|
583
|
-
return fallback ? fallback({
|
|
584
|
-
error,
|
|
585
|
-
retry: this.retry,
|
|
717
|
+
const { runtime, surfaceId, componentId, place, log } = this.props;
|
|
718
|
+
const hostFallback = /* @__PURE__ */ jsx(GuardedNodeFallback, {
|
|
586
719
|
surfaceId,
|
|
587
|
-
componentId
|
|
588
|
-
|
|
720
|
+
componentId,
|
|
721
|
+
runtime,
|
|
589
722
|
message: error.message,
|
|
723
|
+
retry: this.retry,
|
|
724
|
+
slot: failureSlots.renderFallback(place)
|
|
725
|
+
});
|
|
726
|
+
const fallback = runtime.errorFallback;
|
|
727
|
+
if (!fallback) return hostFallback;
|
|
728
|
+
return /* @__PURE__ */ jsx(FallbackGuard, {
|
|
729
|
+
surfaceId,
|
|
590
730
|
componentId,
|
|
591
|
-
|
|
731
|
+
runtime,
|
|
732
|
+
log,
|
|
733
|
+
slot: failureSlots.appFallback(place),
|
|
734
|
+
context: error.message,
|
|
735
|
+
resetKeys: [error],
|
|
736
|
+
replacement: hostFallback,
|
|
737
|
+
children: /* @__PURE__ */ jsx(AppErrorFallback, {
|
|
738
|
+
render: fallback,
|
|
739
|
+
error,
|
|
740
|
+
retry: this.retry,
|
|
741
|
+
surfaceId,
|
|
742
|
+
componentId
|
|
743
|
+
})
|
|
592
744
|
});
|
|
593
745
|
}
|
|
594
746
|
};
|
|
595
|
-
/**
|
|
747
|
+
/**
|
|
748
|
+
* Surface boundary: a failure here must not reach the chat or the host page.
|
|
749
|
+
* It resets on a new revision, and on Retry when `retryable`. A failure that
|
|
750
|
+
* stays the same across revisions is reported once.
|
|
751
|
+
*/
|
|
596
752
|
var SurfaceBoundary = class extends Component {
|
|
597
|
-
state = {
|
|
598
|
-
|
|
599
|
-
|
|
753
|
+
state = {
|
|
754
|
+
error: null,
|
|
755
|
+
revision: this.props.revision
|
|
756
|
+
};
|
|
757
|
+
static getDerivedStateFromError(thrown) {
|
|
758
|
+
return { error: toError(thrown) };
|
|
759
|
+
}
|
|
760
|
+
static getDerivedStateFromProps(props, state) {
|
|
761
|
+
return props.revision !== state.revision ? {
|
|
762
|
+
error: null,
|
|
763
|
+
revision: props.revision
|
|
764
|
+
} : null;
|
|
600
765
|
}
|
|
601
|
-
componentDidCatch(
|
|
602
|
-
|
|
766
|
+
componentDidCatch(thrown) {
|
|
767
|
+
const { runtime, surfaceId, log, level } = this.props;
|
|
768
|
+
log.report(runtime, failureSlots.surface(level), {
|
|
603
769
|
kind: "render",
|
|
604
|
-
surfaceId
|
|
605
|
-
error
|
|
770
|
+
surfaceId,
|
|
771
|
+
error: this.state.error ?? toError(thrown)
|
|
606
772
|
});
|
|
607
773
|
}
|
|
774
|
+
clearSlots() {
|
|
775
|
+
const { log, level } = this.props;
|
|
776
|
+
log.clear(failureSlots.surface(level));
|
|
777
|
+
log.clear(failureSlots.surfaceFallback(level));
|
|
778
|
+
}
|
|
779
|
+
componentDidMount() {
|
|
780
|
+
if (!this.state.error) this.clearSlots();
|
|
781
|
+
}
|
|
782
|
+
componentDidUpdate() {
|
|
783
|
+
if (!this.state.error) this.clearSlots();
|
|
784
|
+
}
|
|
785
|
+
retry = () => this.setState({ error: null });
|
|
608
786
|
render() {
|
|
609
787
|
if (!this.state.error) return this.props.children;
|
|
610
|
-
return /* @__PURE__ */ jsx(
|
|
788
|
+
return /* @__PURE__ */ jsx(GuardedNodeFallback, {
|
|
789
|
+
surfaceId: this.props.surfaceId,
|
|
790
|
+
componentId: void 0,
|
|
791
|
+
runtime: this.props.runtime,
|
|
611
792
|
message: "this generated UI failed to render",
|
|
612
|
-
|
|
793
|
+
retry: this.props.retryable ? this.retry : void 0,
|
|
794
|
+
slot: failureSlots.surfaceFallback(this.props.level)
|
|
613
795
|
});
|
|
614
796
|
}
|
|
615
797
|
};
|
|
@@ -639,7 +821,6 @@ function useGenerativeUIAccessibility() {
|
|
|
639
821
|
//#endregion
|
|
640
822
|
//#region src/generative-ui/SurfaceView.tsx
|
|
641
823
|
const NO_ANCESTORS = [];
|
|
642
|
-
const ROOT_STYLE = { display: "contents" };
|
|
643
824
|
const undefinedSnapshot = () => void 0;
|
|
644
825
|
const noopSubscribe = () => () => {};
|
|
645
826
|
const zeroSnapshot = () => 0;
|
|
@@ -649,23 +830,80 @@ function isTemplateChildren(value) {
|
|
|
649
830
|
}
|
|
650
831
|
function SurfaceView({ surface }) {
|
|
651
832
|
const runtime = useRequiredGenerativeUIRuntime();
|
|
652
|
-
const {
|
|
653
|
-
const
|
|
654
|
-
|
|
655
|
-
|
|
656
|
-
|
|
657
|
-
|
|
658
|
-
|
|
659
|
-
|
|
833
|
+
const { SurfaceRoot } = useGenerativeUIHost();
|
|
834
|
+
const appliedCount = useSyncExternalStore(runtime.manager.subscribe, () => surface.operations.length, undefinedSnapshot);
|
|
835
|
+
const revision = useMemo(() => ({
|
|
836
|
+
surface,
|
|
837
|
+
appliedCount
|
|
838
|
+
}), [surface, appliedCount]);
|
|
839
|
+
const [scopedLog, setScopedLog] = useState(() => ({
|
|
840
|
+
key: surface.key,
|
|
841
|
+
log: createFailureLog()
|
|
842
|
+
}));
|
|
843
|
+
let failureLog = scopedLog.log;
|
|
844
|
+
if (scopedLog.key !== surface.key) {
|
|
845
|
+
const next = {
|
|
846
|
+
key: surface.key,
|
|
847
|
+
log: createFailureLog()
|
|
848
|
+
};
|
|
849
|
+
setScopedLog(next);
|
|
850
|
+
failureLog = next.log;
|
|
851
|
+
}
|
|
852
|
+
const { threadId, surfaceId } = surface;
|
|
853
|
+
const { manager } = runtime;
|
|
854
|
+
const seen = useRef(void 0);
|
|
855
|
+
useLayoutEffect(() => {
|
|
856
|
+
const contentOf = () => {
|
|
857
|
+
const current = manager.getSurface(threadId, surfaceId);
|
|
858
|
+
return {
|
|
859
|
+
log: failureLog,
|
|
860
|
+
current,
|
|
861
|
+
applied: current?.operations.length
|
|
862
|
+
};
|
|
863
|
+
};
|
|
864
|
+
const observe = (missed) => {
|
|
865
|
+
const last = seen.current;
|
|
866
|
+
const next = contentOf();
|
|
867
|
+
seen.current = next;
|
|
868
|
+
if (last?.log === failureLog && (next.current !== last.current || next.applied !== last.applied)) failureLog.settle(missed);
|
|
869
|
+
};
|
|
870
|
+
observe(true);
|
|
871
|
+
return manager.subscribe(() => observe(false));
|
|
872
|
+
}, [
|
|
873
|
+
manager,
|
|
874
|
+
threadId,
|
|
875
|
+
surfaceId,
|
|
876
|
+
failureLog
|
|
877
|
+
]);
|
|
878
|
+
return /* @__PURE__ */ jsx(GenerativeUIFailureLogContext.Provider, {
|
|
879
|
+
value: failureLog,
|
|
880
|
+
children: /* @__PURE__ */ jsx(SurfaceBoundary, {
|
|
660
881
|
surfaceId: surface.surfaceId,
|
|
661
882
|
runtime,
|
|
662
|
-
|
|
663
|
-
|
|
664
|
-
|
|
665
|
-
|
|
666
|
-
|
|
883
|
+
log: failureLog,
|
|
884
|
+
level: "outer",
|
|
885
|
+
revision,
|
|
886
|
+
retryable: true,
|
|
887
|
+
children: /* @__PURE__ */ jsx(SurfaceRoot, {
|
|
888
|
+
surfaceId: surface.surfaceId,
|
|
889
|
+
theme: runtime.theme,
|
|
890
|
+
children: /* @__PURE__ */ jsx(SurfaceBoundary, {
|
|
891
|
+
surfaceId: surface.surfaceId,
|
|
892
|
+
runtime,
|
|
893
|
+
log: failureLog,
|
|
894
|
+
level: "inner",
|
|
895
|
+
revision,
|
|
896
|
+
children: /* @__PURE__ */ jsx(NodeView, {
|
|
897
|
+
surface,
|
|
898
|
+
componentId: GENERATIVE_UI_ROOT_ID,
|
|
899
|
+
basePath: void 0,
|
|
900
|
+
ancestors: NO_ANCESTORS,
|
|
901
|
+
parentPlace: "",
|
|
902
|
+
via: ""
|
|
903
|
+
})
|
|
904
|
+
})
|
|
667
905
|
})
|
|
668
|
-
})
|
|
906
|
+
})
|
|
669
907
|
});
|
|
670
908
|
}
|
|
671
909
|
/**
|
|
@@ -690,27 +928,63 @@ function useComponentProperties(manager, component) {
|
|
|
690
928
|
* repeats both.
|
|
691
929
|
*/
|
|
692
930
|
function scopedId(componentId, basePath) {
|
|
693
|
-
return `${basePath
|
|
931
|
+
return `${basePath === void 0 ? "" : JSON.stringify(basePath)}#${JSON.stringify(componentId)}`;
|
|
932
|
+
}
|
|
933
|
+
/**
|
|
934
|
+
* Where one rendered copy of a node is: the place of its parent, the slot
|
|
935
|
+
* or child reference of the parent it renders in, and its scoped id. Names
|
|
936
|
+
* the node's failure-log slots. A scoped id alone is not enough: the same
|
|
937
|
+
* id can render under two parents, and a cyclic reference repeats the
|
|
938
|
+
* scoped id of an ancestor. Each copy keeps its own memory of what it
|
|
939
|
+
* reported, so one copy that renders, goes away or remounts does not clear
|
|
940
|
+
* the memory of another that still fails.
|
|
941
|
+
*/
|
|
942
|
+
function placeOf(parentPlace, via, position) {
|
|
943
|
+
return `${parentPlace}/${via === "" ? "" : JSON.stringify(via)}${position}`;
|
|
694
944
|
}
|
|
695
|
-
const NodeView = memo(function NodeView({ surface, componentId, basePath, ancestors }) {
|
|
945
|
+
const NodeView = memo(function NodeView({ surface, componentId, basePath, ancestors, parentPlace, via }) {
|
|
696
946
|
const runtime = useRequiredGenerativeUIRuntime();
|
|
947
|
+
const log = useFailureLog();
|
|
697
948
|
const component = useComponentModel(runtime.manager, surface.model, componentId);
|
|
698
949
|
const properties = useComponentProperties(runtime.manager, component);
|
|
699
|
-
|
|
950
|
+
const position = scopedId(componentId, basePath);
|
|
951
|
+
const place = placeOf(parentPlace, via, position);
|
|
952
|
+
useEffect(() => {
|
|
953
|
+
const slots = [
|
|
954
|
+
failureSlots.fallback(place),
|
|
955
|
+
failureSlots.cycle(place),
|
|
956
|
+
failureSlots.props(place)
|
|
957
|
+
];
|
|
958
|
+
for (const slot of slots) log.retain(slot);
|
|
959
|
+
return () => {
|
|
960
|
+
for (const slot of slots) log.release(slot);
|
|
961
|
+
};
|
|
962
|
+
}, [log, place]);
|
|
963
|
+
if (ancestors.includes(position)) return /* @__PURE__ */ jsx(GuardedNodeFallback, {
|
|
964
|
+
surfaceId: surface.surfaceId,
|
|
965
|
+
componentId,
|
|
966
|
+
runtime,
|
|
700
967
|
message: `cyclic reference to ${componentId}`,
|
|
701
|
-
|
|
968
|
+
slot: failureSlots.cycle(place),
|
|
969
|
+
revision: properties
|
|
702
970
|
});
|
|
703
971
|
if (!component || !properties) return /* @__PURE__ */ jsx(NodePlaceholder, { componentId });
|
|
704
972
|
const definition = runtime.ui.components.get(component.type);
|
|
705
|
-
if (!definition) return /* @__PURE__ */ jsx(
|
|
973
|
+
if (!definition) return /* @__PURE__ */ jsx(GuardedNodeFallback, {
|
|
974
|
+
surfaceId: surface.surfaceId,
|
|
975
|
+
componentId,
|
|
976
|
+
runtime,
|
|
706
977
|
message: `unknown component "${component.type}"`,
|
|
707
|
-
|
|
978
|
+
slot: failureSlots.fallback(place),
|
|
979
|
+
revision: properties
|
|
708
980
|
});
|
|
709
981
|
return /* @__PURE__ */ jsx(NodeBoundary, {
|
|
710
982
|
surfaceId: surface.surfaceId,
|
|
711
983
|
componentId,
|
|
984
|
+
place,
|
|
712
985
|
revision: properties,
|
|
713
986
|
runtime,
|
|
987
|
+
log,
|
|
714
988
|
children: /* @__PURE__ */ jsx(ResolvedNode, {
|
|
715
989
|
surface,
|
|
716
990
|
componentId,
|
|
@@ -718,7 +992,8 @@ const NodeView = memo(function NodeView({ surface, componentId, basePath, ancest
|
|
|
718
992
|
properties,
|
|
719
993
|
runtime,
|
|
720
994
|
basePath,
|
|
721
|
-
ancestors
|
|
995
|
+
ancestors,
|
|
996
|
+
place
|
|
722
997
|
})
|
|
723
998
|
}, component.type);
|
|
724
999
|
});
|
|
@@ -726,7 +1001,7 @@ const NodeView = memo(function NodeView({ surface, componentId, basePath, ancest
|
|
|
726
1001
|
* Renders the component of a template once for each item of the list at the
|
|
727
1002
|
* template path, keyed by index. Renders nothing when there is no list.
|
|
728
1003
|
*/
|
|
729
|
-
function TemplateChildren({ surface, template, basePath, ancestors }) {
|
|
1004
|
+
function TemplateChildren({ surface, template, basePath, ancestors, parentPlace, via }) {
|
|
730
1005
|
const dataModel = surface.model.dataModel;
|
|
731
1006
|
const listPath = resolvePath(template.path, basePath);
|
|
732
1007
|
const subscribe = useCallback((onChange) => {
|
|
@@ -742,17 +1017,25 @@ function TemplateChildren({ surface, template, basePath, ancestors }) {
|
|
|
742
1017
|
surface,
|
|
743
1018
|
componentId: template.componentId,
|
|
744
1019
|
basePath: joinPath(listPath, String(index)),
|
|
745
|
-
ancestors
|
|
1020
|
+
ancestors,
|
|
1021
|
+
parentPlace,
|
|
1022
|
+
via
|
|
746
1023
|
}, index)) });
|
|
747
1024
|
}
|
|
748
1025
|
/**
|
|
749
1026
|
* The props the model sets, with their dynamic values resolved from the
|
|
750
1027
|
* surface data model: `{"@path"}` bindings, `{"@call"}` function values and
|
|
751
|
-
* `{"@call":"@index"}` in a template, at any depth.
|
|
752
|
-
*
|
|
753
|
-
*
|
|
754
|
-
*
|
|
755
|
-
*
|
|
1028
|
+
* `{"@call":"@index"}` in a template, at any depth. Bound props, events and
|
|
1029
|
+
* slots are left out, and so are the keys of the A2UI component envelope;
|
|
1030
|
+
* `accessibility` is resolved the same way and given on its own.
|
|
1031
|
+
*
|
|
1032
|
+
* A writable prop that is not bound to a path and that the component set
|
|
1033
|
+
* shows the local value from `overrides`, also when the model never set the
|
|
1034
|
+
* prop. That is intended: the user's edit wins over the model's literal
|
|
1035
|
+
* value, also over a newer literal the agent sends later, until the node
|
|
1036
|
+
* remounts. A literal is not data the agent and the user share; a prop
|
|
1037
|
+
* that both must write is bound to a path, and the setter writes the data
|
|
1038
|
+
* model.
|
|
756
1039
|
*
|
|
757
1040
|
* A prop is resolved again only when its raw value or a data model value it
|
|
758
1041
|
* read has changed, so the result keeps its identity otherwise. A function
|
|
@@ -763,7 +1046,7 @@ function useModelDataProps(surface, definition, properties, basePath, overrides)
|
|
|
763
1046
|
const units = useMemo(() => {
|
|
764
1047
|
const list = [];
|
|
765
1048
|
for (const key of Object.keys(properties)) {
|
|
766
|
-
if (definition.events?.[key] || definition.slots?.[key] ||
|
|
1049
|
+
if (definition.events?.[key] || definition.slots?.[key] || hasOwnKey(definition.selectors, key) || A2UI_COMPONENT_ENVELOPE_KEYS.has(key)) continue;
|
|
767
1050
|
list.push({
|
|
768
1051
|
key,
|
|
769
1052
|
raw: properties[key]
|
|
@@ -864,9 +1147,10 @@ function select(definition, state, modelProps) {
|
|
|
864
1147
|
for (const [key, selector] of selectors) try {
|
|
865
1148
|
values[key] = selector(state, modelProps);
|
|
866
1149
|
} catch (error) {
|
|
1150
|
+
const { message } = toError(error);
|
|
867
1151
|
return {
|
|
868
1152
|
ok: false,
|
|
869
|
-
problem: `${key}: selector threw: ${
|
|
1153
|
+
problem: `${key}: selector threw: ${message}`
|
|
870
1154
|
};
|
|
871
1155
|
}
|
|
872
1156
|
return {
|
|
@@ -877,8 +1161,10 @@ function select(definition, state, modelProps) {
|
|
|
877
1161
|
/**
|
|
878
1162
|
* The values of the bound props of a node, read from the app state.
|
|
879
1163
|
*
|
|
880
|
-
* The selectors
|
|
881
|
-
*
|
|
1164
|
+
* The selectors get the checked model props: the resolved values that
|
|
1165
|
+
* passed the props schema (without the nulls it rejected), not the
|
|
1166
|
+
* schema's parsed output, so its defaults and transforms are not applied
|
|
1167
|
+
* yet. They run again only when the state object or those props change. When the new values are shallow-equal to the last ones, the
|
|
882
1168
|
* last snapshot is kept. So the node re-renders only when a selected value
|
|
883
1169
|
* changes. A selector that returns a new array on each call cannot cause a
|
|
884
1170
|
* render loop: with the same inputs, the cached snapshot comes back.
|
|
@@ -908,7 +1194,10 @@ function useSelection(manager, definition, modelProps) {
|
|
|
908
1194
|
* Checks the model-set props with the props schema before the selectors run.
|
|
909
1195
|
* The bound props are not set yet, so their issues do not count. Neither do
|
|
910
1196
|
* issues with no path (a check over several props), which the full check
|
|
911
|
-
* after the selectors reports.
|
|
1197
|
+
* after the selectors reports. The exception is the issue that an async
|
|
1198
|
+
* props schema gives (`isAsyncIssue`): the render checks props
|
|
1199
|
+
* synchronously only, so the full check would fail the same way, and it is
|
|
1200
|
+
* reported here.
|
|
912
1201
|
*/
|
|
913
1202
|
function checkModelProps(definition, modelData) {
|
|
914
1203
|
if (!definition.props) return {
|
|
@@ -927,7 +1216,7 @@ function checkModelProps(definition, modelData) {
|
|
|
927
1216
|
if (!result.success) {
|
|
928
1217
|
const issues = result.issues.filter((issue) => {
|
|
929
1218
|
const key = issueKey(issue);
|
|
930
|
-
return key === void 0 ? isAsyncIssue(issue) : !
|
|
1219
|
+
return key === void 0 ? isAsyncIssue(issue) : !hasOwnKey(definition.selectors, key);
|
|
931
1220
|
});
|
|
932
1221
|
if (issues.length > 0) return {
|
|
933
1222
|
ok: false,
|
|
@@ -990,7 +1279,7 @@ function useDataProps(runtime, surface, definition, properties, basePath, overri
|
|
|
990
1279
|
function useEventProps(runtime, surface, componentId, definition, properties, basePath) {
|
|
991
1280
|
const latest = useRef(properties);
|
|
992
1281
|
latest.current = properties;
|
|
993
|
-
const boundNames = Object.keys(definition.events ?? {}).filter((name) =>
|
|
1282
|
+
const boundNames = Object.keys(definition.events ?? {}).filter((name) => hasOwnKey(definition.eventFunctions, name) || isActionBinding(properties[name])).join("|");
|
|
994
1283
|
return useMemo(() => {
|
|
995
1284
|
const handlers = {};
|
|
996
1285
|
const names = boundNames ? boundNames.split("|") : [];
|
|
@@ -1002,7 +1291,7 @@ function useEventProps(runtime, surface, componentId, definition, properties, ba
|
|
|
1002
1291
|
name,
|
|
1003
1292
|
parameters: definition.events?.[name]?.parameters
|
|
1004
1293
|
};
|
|
1005
|
-
const functionName =
|
|
1294
|
+
const functionName = hasOwnKey(definition.eventFunctions, name) ? definition.eventFunctions[name] : void 0;
|
|
1006
1295
|
handlers[name] = functionName === void 0 ? (args) => invokeBoundEvent({
|
|
1007
1296
|
...call,
|
|
1008
1297
|
binding: latest.current[name],
|
|
@@ -1028,7 +1317,9 @@ function useEventProps(runtime, surface, componentId, definition, properties, ba
|
|
|
1028
1317
|
* The setter props of a node: `setValue` for the writable prop `value`. For
|
|
1029
1318
|
* a prop the model bound to `{"@path"}`, the setter writes the data model
|
|
1030
1319
|
* and makes the path user-owned. For a literal prop, it sets `overrides`, a
|
|
1031
|
-
* local value of this mounted node
|
|
1320
|
+
* local value of this mounted node that wins over the model's value until
|
|
1321
|
+
* the node remounts (see `useModelDataProps`). The setters keep their
|
|
1322
|
+
* identity.
|
|
1032
1323
|
*/
|
|
1033
1324
|
function useSetterProps(runtime, surface, componentId, definition, properties, basePath) {
|
|
1034
1325
|
const [overrides, setOverrides] = useState({});
|
|
@@ -1051,7 +1342,7 @@ function useSetterProps(runtime, surface, componentId, definition, properties, b
|
|
|
1051
1342
|
try {
|
|
1052
1343
|
runtime.manager.setOwnedValue(surface.key, resolvePath(binding["@path"], basePath), value);
|
|
1053
1344
|
} catch (cause) {
|
|
1054
|
-
const message =
|
|
1345
|
+
const { message } = toError(cause);
|
|
1055
1346
|
reportErrorSafely(runtime, {
|
|
1056
1347
|
kind: "props",
|
|
1057
1348
|
surfaceId: surface.surfaceId,
|
|
@@ -1070,29 +1361,36 @@ function useSetterProps(runtime, surface, componentId, definition, properties, b
|
|
|
1070
1361
|
])
|
|
1071
1362
|
};
|
|
1072
1363
|
}
|
|
1073
|
-
function useSlotProps(surface, definition, properties, componentId, basePath, ancestors) {
|
|
1364
|
+
function useSlotProps(surface, definition, properties, componentId, basePath, ancestors, place) {
|
|
1074
1365
|
return useMemo(() => {
|
|
1075
1366
|
const childAncestors = [...ancestors, scopedId(componentId, basePath)];
|
|
1076
1367
|
const slots = {};
|
|
1077
1368
|
for (const [name, kind] of Object.entries(definition.slots ?? {})) {
|
|
1078
1369
|
const value = properties[name];
|
|
1370
|
+
const via = `${name}:`;
|
|
1079
1371
|
if (kind === "single" && typeof value === "string") slots[name] = /* @__PURE__ */ jsx(NodeView, {
|
|
1080
1372
|
surface,
|
|
1081
1373
|
componentId: value,
|
|
1082
1374
|
basePath,
|
|
1083
|
-
ancestors: childAncestors
|
|
1375
|
+
ancestors: childAncestors,
|
|
1376
|
+
parentPlace: place,
|
|
1377
|
+
via
|
|
1084
1378
|
});
|
|
1085
1379
|
else if (kind === "multiple" && isTemplateChildren(value)) slots[name] = /* @__PURE__ */ jsx(TemplateChildren, {
|
|
1086
1380
|
surface,
|
|
1087
1381
|
template: value,
|
|
1088
1382
|
basePath,
|
|
1089
|
-
ancestors: childAncestors
|
|
1383
|
+
ancestors: childAncestors,
|
|
1384
|
+
parentPlace: place,
|
|
1385
|
+
via
|
|
1090
1386
|
});
|
|
1091
1387
|
else if (kind === "multiple" && Array.isArray(value)) slots[name] = [...new Set(value.filter((id) => typeof id === "string"))].map((id) => /* @__PURE__ */ jsx(NodeView, {
|
|
1092
1388
|
surface,
|
|
1093
1389
|
componentId: id,
|
|
1094
1390
|
basePath,
|
|
1095
|
-
ancestors: childAncestors
|
|
1391
|
+
ancestors: childAncestors,
|
|
1392
|
+
parentPlace: place,
|
|
1393
|
+
via
|
|
1096
1394
|
}, id));
|
|
1097
1395
|
}
|
|
1098
1396
|
return slots;
|
|
@@ -1102,7 +1400,8 @@ function useSlotProps(surface, definition, properties, componentId, basePath, an
|
|
|
1102
1400
|
properties,
|
|
1103
1401
|
componentId,
|
|
1104
1402
|
basePath,
|
|
1105
|
-
ancestors
|
|
1403
|
+
ancestors,
|
|
1404
|
+
place
|
|
1106
1405
|
]);
|
|
1107
1406
|
}
|
|
1108
1407
|
/**
|
|
@@ -1110,33 +1409,43 @@ function useSlotProps(surface, definition, properties, componentId, basePath, an
|
|
|
1110
1409
|
* `childList()`, at any depth) replaced by the rendered component. Without
|
|
1111
1410
|
* references, the parsed props come back as they are.
|
|
1112
1411
|
*/
|
|
1113
|
-
function useChildRefProps(surface, refs, props, componentId, basePath, ancestors) {
|
|
1412
|
+
function useChildRefProps(surface, refs, props, componentId, basePath, ancestors, place) {
|
|
1114
1413
|
return useMemo(() => {
|
|
1115
1414
|
if (props === void 0 || refs.length === 0) return props;
|
|
1116
1415
|
const childAncestors = [...ancestors, scopedId(componentId, basePath)];
|
|
1117
|
-
const render = (kind, value) => {
|
|
1416
|
+
const render = (kind, value, via) => {
|
|
1118
1417
|
if (kind === "single") return typeof value === "string" ? /* @__PURE__ */ jsx(NodeView, {
|
|
1119
1418
|
surface,
|
|
1120
1419
|
componentId: value,
|
|
1121
1420
|
basePath,
|
|
1122
|
-
ancestors: childAncestors
|
|
1421
|
+
ancestors: childAncestors,
|
|
1422
|
+
parentPlace: place,
|
|
1423
|
+
via
|
|
1123
1424
|
}) : void 0;
|
|
1124
1425
|
if (isTemplateChildren(value)) return /* @__PURE__ */ jsx(TemplateChildren, {
|
|
1125
1426
|
surface,
|
|
1126
1427
|
template: value,
|
|
1127
1428
|
basePath,
|
|
1128
|
-
ancestors: childAncestors
|
|
1429
|
+
ancestors: childAncestors,
|
|
1430
|
+
parentPlace: place,
|
|
1431
|
+
via
|
|
1129
1432
|
});
|
|
1130
1433
|
if (!Array.isArray(value)) return void 0;
|
|
1131
1434
|
return [...new Set(value.filter((id) => typeof id === "string"))].map((id) => /* @__PURE__ */ jsx(NodeView, {
|
|
1132
1435
|
surface,
|
|
1133
1436
|
componentId: id,
|
|
1134
1437
|
basePath,
|
|
1135
|
-
ancestors: childAncestors
|
|
1438
|
+
ancestors: childAncestors,
|
|
1439
|
+
parentPlace: place,
|
|
1440
|
+
via
|
|
1136
1441
|
}, id));
|
|
1137
1442
|
};
|
|
1138
1443
|
let result = props;
|
|
1139
|
-
for (const ref of refs)
|
|
1444
|
+
for (const ref of refs) {
|
|
1445
|
+
let found = 0;
|
|
1446
|
+
const path = ref.path.join(".");
|
|
1447
|
+
result = mapAtChildRef(result, ref.path, (value) => render(ref.kind, value, `${path}[${found++}]:`));
|
|
1448
|
+
}
|
|
1140
1449
|
return result;
|
|
1141
1450
|
}, [
|
|
1142
1451
|
surface,
|
|
@@ -1144,15 +1453,17 @@ function useChildRefProps(surface, refs, props, componentId, basePath, ancestors
|
|
|
1144
1453
|
props,
|
|
1145
1454
|
componentId,
|
|
1146
1455
|
basePath,
|
|
1147
|
-
ancestors
|
|
1456
|
+
ancestors,
|
|
1457
|
+
place
|
|
1148
1458
|
]);
|
|
1149
1459
|
}
|
|
1150
|
-
function ResolvedNode({ surface, componentId, definition, properties, runtime, basePath, ancestors }) {
|
|
1460
|
+
function ResolvedNode({ surface, componentId, definition, properties, runtime, basePath, ancestors, place }) {
|
|
1461
|
+
const { AccessibilityWrapper } = useGenerativeUIHost();
|
|
1151
1462
|
const { overrides, setters } = useSetterProps(runtime, surface, componentId, definition, properties, basePath);
|
|
1152
1463
|
const dataProps = useDataProps(runtime, surface, definition, properties, basePath, overrides);
|
|
1153
1464
|
const eventProps = useEventProps(runtime, surface, componentId, definition, properties, basePath);
|
|
1154
|
-
const slotProps = useSlotProps(surface, definition, properties, componentId, basePath, ancestors);
|
|
1155
|
-
const childRefProps = useChildRefProps(surface, definition.childRefs, dataProps.ok ? dataProps.props : void 0, componentId, basePath, ancestors);
|
|
1465
|
+
const slotProps = useSlotProps(surface, definition, properties, componentId, basePath, ancestors, place);
|
|
1466
|
+
const childRefProps = useChildRefProps(surface, definition.childRefs, dataProps.ok ? dataProps.props : void 0, componentId, basePath, ancestors, place);
|
|
1156
1467
|
const accessibility = dataProps.ok ? dataProps.accessibility : void 0;
|
|
1157
1468
|
const nodeContext = useMemo(() => ({
|
|
1158
1469
|
surfaceKey: surface.key,
|
|
@@ -1166,8 +1477,19 @@ function ResolvedNode({ surface, componentId, definition, properties, runtime, b
|
|
|
1166
1477
|
accessibility
|
|
1167
1478
|
]);
|
|
1168
1479
|
const problem = dataProps.ok ? void 0 : dataProps.problem;
|
|
1480
|
+
const log = useFailureLog();
|
|
1169
1481
|
useEffect(() => {
|
|
1170
|
-
if (problem)
|
|
1482
|
+
if (!problem) {
|
|
1483
|
+
log.clear(failureSlots.fallback(place));
|
|
1484
|
+
log.clear(failureSlots.props(place));
|
|
1485
|
+
}
|
|
1486
|
+
}, [
|
|
1487
|
+
problem,
|
|
1488
|
+
log,
|
|
1489
|
+
place
|
|
1490
|
+
]);
|
|
1491
|
+
useEffect(() => {
|
|
1492
|
+
if (problem) log.report(runtime, failureSlots.props(place), {
|
|
1171
1493
|
kind: "props",
|
|
1172
1494
|
surfaceId: surface.surfaceId,
|
|
1173
1495
|
componentId,
|
|
@@ -1175,13 +1497,19 @@ function ResolvedNode({ surface, componentId, definition, properties, runtime, b
|
|
|
1175
1497
|
});
|
|
1176
1498
|
}, [
|
|
1177
1499
|
problem,
|
|
1500
|
+
log,
|
|
1501
|
+
place,
|
|
1178
1502
|
runtime,
|
|
1179
1503
|
surface.surfaceId,
|
|
1180
1504
|
componentId
|
|
1181
1505
|
]);
|
|
1182
|
-
if (!dataProps.ok) return /* @__PURE__ */ jsx(
|
|
1506
|
+
if (!dataProps.ok) return /* @__PURE__ */ jsx(GuardedNodeFallback, {
|
|
1507
|
+
surfaceId: surface.surfaceId,
|
|
1508
|
+
componentId,
|
|
1509
|
+
runtime,
|
|
1183
1510
|
message: `invalid props (${problem})`,
|
|
1184
|
-
|
|
1511
|
+
slot: failureSlots.fallback(place),
|
|
1512
|
+
revision: properties
|
|
1185
1513
|
});
|
|
1186
1514
|
const Component = definition.component;
|
|
1187
1515
|
const rendered = /* @__PURE__ */ jsx(Component, {
|
|
@@ -1193,10 +1521,8 @@ function ResolvedNode({ surface, componentId, definition, properties, runtime, b
|
|
|
1193
1521
|
const hasEnvelope = properties.accessibility !== void 0 && definition.accessibility !== "self";
|
|
1194
1522
|
return /* @__PURE__ */ jsx(GenerativeUINodeContext.Provider, {
|
|
1195
1523
|
value: nodeContext,
|
|
1196
|
-
children: hasEnvelope ? /* @__PURE__ */ jsx(
|
|
1197
|
-
|
|
1198
|
-
style: ROOT_STYLE,
|
|
1199
|
-
...wrapperAria(accessibilityAttributes$1(accessibility)),
|
|
1524
|
+
children: hasEnvelope ? /* @__PURE__ */ jsx(AccessibilityWrapper, {
|
|
1525
|
+
attributes: wrapperAria(accessibilityAttributes$1(accessibility)),
|
|
1200
1526
|
children: rendered
|
|
1201
1527
|
}) : rendered
|
|
1202
1528
|
});
|
|
@@ -1216,39 +1542,18 @@ function DefaultGenerativeUIChatCard({ surfaceId, error, children }) {
|
|
|
1216
1542
|
})]
|
|
1217
1543
|
});
|
|
1218
1544
|
}
|
|
1219
|
-
const ContentSchema = z.object({
|
|
1220
|
-
surfaceId: z.string(),
|
|
1221
|
-
target: z.string(),
|
|
1222
|
-
catalogId: z.string(),
|
|
1223
|
-
operations: z.array(z.any()),
|
|
1224
|
-
status: z.enum([
|
|
1225
|
-
"streaming",
|
|
1226
|
-
"ready",
|
|
1227
|
-
"error"
|
|
1228
|
-
]),
|
|
1229
|
-
rejected: z.array(z.object({
|
|
1230
|
-
index: z.number(),
|
|
1231
|
-
reason: z.string()
|
|
1232
|
-
})).default([])
|
|
1233
|
-
}).loose();
|
|
1234
1545
|
function GenerativeUIChatActivity({ content, agent }) {
|
|
1235
1546
|
const runtime = useGenerativeUIRuntime();
|
|
1236
1547
|
const threadId = useCopilotChatConfiguration()?.threadId ?? agent?.threadId;
|
|
1237
1548
|
const Wrapper = useContext(GenerativeUIChatSlotContext) ?? DefaultGenerativeUIChatCard;
|
|
1238
|
-
|
|
1239
|
-
useLayoutEffect(() => {
|
|
1240
|
-
if (runtime && threadId) runtime.manager.ingest(threadId, agent?.agentId, content);
|
|
1241
|
-
}, [
|
|
1549
|
+
const { surface, rejected, error } = useGenerativeUIActivitySurface({
|
|
1242
1550
|
runtime,
|
|
1243
1551
|
threadId,
|
|
1244
|
-
agent?.agentId,
|
|
1245
|
-
content
|
|
1246
|
-
|
|
1247
|
-
|
|
1248
|
-
const surface = runtime.manager.getSurface(threadId, content.surfaceId) ?? runtime.manager.getFailedSurface(threadId, content.surfaceId);
|
|
1552
|
+
agentId: agent?.agentId,
|
|
1553
|
+
content,
|
|
1554
|
+
target: GENERATIVE_UI_CHAT_TARGET
|
|
1555
|
+
});
|
|
1249
1556
|
if (!surface) return null;
|
|
1250
|
-
const rejected = [...content.rejected ?? [], ...surface.clientRejected];
|
|
1251
|
-
const error = rejected.length ? new Error(`${rejected.length} UI update(s) could not be shown`, { cause: rejected }) : void 0;
|
|
1252
1557
|
return /* @__PURE__ */ jsx(Wrapper, {
|
|
1253
1558
|
surfaceId: content.surfaceId,
|
|
1254
1559
|
isStreaming: content.status === "streaming",
|
|
@@ -1260,7 +1565,7 @@ function GenerativeUIChatActivity({ content, agent }) {
|
|
|
1260
1565
|
function createGenerativeUIActivityRenderer() {
|
|
1261
1566
|
return {
|
|
1262
1567
|
activityType: GENERATIVE_UI_ACTIVITY_TYPE,
|
|
1263
|
-
content:
|
|
1568
|
+
content: GenerativeUISurfaceContentSchema,
|
|
1264
1569
|
render: GenerativeUIChatActivity
|
|
1265
1570
|
};
|
|
1266
1571
|
}
|
|
@@ -1291,152 +1596,5 @@ function GenerativeUIToolCallRenderer() {
|
|
|
1291
1596
|
}
|
|
1292
1597
|
|
|
1293
1598
|
//#endregion
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
* Markdown prose inside generative UI, rendered like the chat text around
|
|
1297
|
-
* it: the renderer set on the provider when there is one, else the built-in
|
|
1298
|
-
* streaming renderer. Config passed here wins over provider config.
|
|
1299
|
-
*/
|
|
1300
|
-
function ProviderMarkdown({ content, isStreaming, className, ...config }) {
|
|
1301
|
-
const renderer = useMarkdownRenderer();
|
|
1302
|
-
if (renderer && isReactComponentType(renderer)) return /* @__PURE__ */ jsx(renderer, {
|
|
1303
|
-
content,
|
|
1304
|
-
isStreaming,
|
|
1305
|
-
className
|
|
1306
|
-
});
|
|
1307
|
-
return /* @__PURE__ */ jsx(StreamingMarkdownDefaultRenderer, {
|
|
1308
|
-
...renderer,
|
|
1309
|
-
...config,
|
|
1310
|
-
content,
|
|
1311
|
-
isStreaming,
|
|
1312
|
-
className
|
|
1313
|
-
});
|
|
1314
|
-
}
|
|
1315
|
-
|
|
1316
|
-
//#endregion
|
|
1317
|
-
//#region src/generative-ui/markdown/MarkdownSurfaces.tsx
|
|
1318
|
-
const FADE_STYLE_ID = "a2ui-markdown-fade";
|
|
1319
|
-
/**
|
|
1320
|
-
* Components fade in as they stream: each element that appears under a
|
|
1321
|
-
* markdown surface runs the keyframes once on mount. React keeps the keys
|
|
1322
|
-
* stable per component id, so an element that is already there never
|
|
1323
|
-
* animates again. SVG internals are left out: a chart library re-creates
|
|
1324
|
-
* its nodes on each render, and a fade on each of them would read as the
|
|
1325
|
-
* chart flashing whenever a sibling arrives. The fade runs under reduced
|
|
1326
|
-
* motion too, because it moves nothing.
|
|
1327
|
-
*/
|
|
1328
|
-
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");
|
|
1329
|
-
const noEffect = () => {};
|
|
1330
|
-
const useInjectEffect = typeof document === "undefined" ? noEffect : useInsertionEffect;
|
|
1331
|
-
function useFadeStyles() {
|
|
1332
|
-
useInjectEffect(() => {
|
|
1333
|
-
if (document.getElementById(FADE_STYLE_ID)) return;
|
|
1334
|
-
const style = document.createElement("style");
|
|
1335
|
-
style.id = FADE_STYLE_ID;
|
|
1336
|
-
style.textContent = FADE_CSS;
|
|
1337
|
-
document.head.appendChild(style);
|
|
1338
|
-
}, []);
|
|
1339
|
-
}
|
|
1340
|
-
/**
|
|
1341
|
-
* The default wrapper of a chat surface in the markdown transport. It draws
|
|
1342
|
-
* no box: a surface sits in the flow of the reply like a paragraph, and a
|
|
1343
|
-
* Card draws its own box when it is interactive. Nothing shows while the
|
|
1344
|
-
* surface streams. A rejected operation gets one line.
|
|
1345
|
-
*/
|
|
1346
|
-
function DefaultMarkdownChatSurface({ surfaceId, error, children }) {
|
|
1347
|
-
useFadeStyles();
|
|
1348
|
-
return /* @__PURE__ */ jsxs("div", {
|
|
1349
|
-
"data-generative-ui-chat-surface": surfaceId,
|
|
1350
|
-
"data-a2ui-markdown-surface": surfaceId,
|
|
1351
|
-
className: "cpk-markdown-chat-surface",
|
|
1352
|
-
children: [children, error && /* @__PURE__ */ jsx("div", {
|
|
1353
|
-
role: "alert",
|
|
1354
|
-
className: "cpk-markdown-chat-surface-error",
|
|
1355
|
-
children: "Part of this UI couldn't be shown."
|
|
1356
|
-
})]
|
|
1357
|
-
});
|
|
1358
|
-
}
|
|
1359
|
-
/**
|
|
1360
|
-
* One chat surface in the chat wrapper. Nothing renders until the surface
|
|
1361
|
-
* has its root component, so a half-streamed fence shows no empty card.
|
|
1362
|
-
*/
|
|
1363
|
-
function ChatSurface({ runtime, threadId, surfaceId, Wrapper }) {
|
|
1364
|
-
const surface = runtime.manager.getSurface(threadId, surfaceId) ?? runtime.manager.getFailedSurface(threadId, surfaceId);
|
|
1365
|
-
const root = surface?.model.componentsModel.get(GENERATIVE_UI_ROOT_ID);
|
|
1366
|
-
if (!surface || !root && !surface.failed) return null;
|
|
1367
|
-
const rejected = [...surface.serverRejected, ...surface.clientRejected];
|
|
1368
|
-
const error = rejected.length ? new Error(`${rejected.length} UI update(s) could not be shown`, { cause: rejected }) : void 0;
|
|
1369
|
-
return /* @__PURE__ */ jsx(Wrapper, {
|
|
1370
|
-
surfaceId,
|
|
1371
|
-
isStreaming: surface.status === "streaming",
|
|
1372
|
-
error,
|
|
1373
|
-
rejected,
|
|
1374
|
-
children: surface.failed ? null : /* @__PURE__ */ jsx(SurfaceView, { surface })
|
|
1375
|
-
});
|
|
1376
|
-
}
|
|
1377
|
-
/** The blocks of one fence: lifted prose and chat surfaces, in order. */
|
|
1378
|
-
function FenceBlocks({ blocks, runtime, threadId, isStreaming, markdownProps }) {
|
|
1379
|
-
const Wrapper = useContext(GenerativeUIChatSlotContext) ?? DefaultMarkdownChatSurface;
|
|
1380
|
-
let surfaceCount = 0;
|
|
1381
|
-
return /* @__PURE__ */ jsx(Fragment$1, { children: blocks.map((block, index) => {
|
|
1382
|
-
if (block.kind === "markdown") return /* @__PURE__ */ jsx(ProviderMarkdown, {
|
|
1383
|
-
...markdownProps,
|
|
1384
|
-
content: block.markdown,
|
|
1385
|
-
isStreaming: isStreaming && index === blocks.length - 1
|
|
1386
|
-
}, `md-${surfaceCount}`);
|
|
1387
|
-
surfaceCount++;
|
|
1388
|
-
return /* @__PURE__ */ jsx(ChatSurface, {
|
|
1389
|
-
runtime,
|
|
1390
|
-
threadId,
|
|
1391
|
-
surfaceId: block.surfaceId,
|
|
1392
|
-
Wrapper
|
|
1393
|
-
}, block.surfaceId);
|
|
1394
|
-
}) });
|
|
1395
|
-
}
|
|
1396
|
-
/**
|
|
1397
|
-
* Experimental. Renders an assistant message of the markdown transport.
|
|
1398
|
-
* Prose renders as markdown. In place of each ```a2ui fence:
|
|
1399
|
-
*
|
|
1400
|
-
* - the fence that created a chat surface renders that surface (and the
|
|
1401
|
-
* prose lifted out of it) in the chat wrapper;
|
|
1402
|
-
* - every other fence renders nothing: a fence that updates the surface of
|
|
1403
|
-
* an earlier fence, or a fence for a named target, which
|
|
1404
|
-
* `<GenerativeUISurface id>` renders.
|
|
1405
|
-
*
|
|
1406
|
-
* Use it inside `<CopilotKitProvider generativeUI={{ ui, transport: "markdown" }}>`.
|
|
1407
|
-
* `CopilotChatAssistantMessage` uses it there on its own.
|
|
1408
|
-
*
|
|
1409
|
-
* @example
|
|
1410
|
-
* ```tsx
|
|
1411
|
-
* <MarkdownSurfaces content={message.content} messageId={message.id} />
|
|
1412
|
-
* ```
|
|
1413
|
-
*/
|
|
1414
|
-
function MarkdownSurfaces({ content, messageId, isStreaming = false, ...markdownProps }) {
|
|
1415
|
-
const runtime = useRequiredGenerativeUIRuntime();
|
|
1416
|
-
const chatThreadId = useCopilotChatConfiguration()?.threadId;
|
|
1417
|
-
useManagerVersion(runtime);
|
|
1418
|
-
const segments = useMemo(() => splitA2UIMarkdown(content), [content]);
|
|
1419
|
-
const threadId = chatThreadId ?? runtime.manager.getActiveThread();
|
|
1420
|
-
const source = getMarkdownSurfaceSource(runtime);
|
|
1421
|
-
return /* @__PURE__ */ jsx(Fragment$1, { children: segments.map((segment, index) => {
|
|
1422
|
-
const isLast = index === segments.length - 1;
|
|
1423
|
-
if (segment.type === "markdown") return /* @__PURE__ */ jsx(ProviderMarkdown, {
|
|
1424
|
-
...markdownProps,
|
|
1425
|
-
content: segment.text,
|
|
1426
|
-
isStreaming: isStreaming && isLast
|
|
1427
|
-
}, `md-${index}`);
|
|
1428
|
-
const blocks = threadId ? source.getFenceBlocks(threadId, messageId, segment.index) : void 0;
|
|
1429
|
-
if (!threadId || !blocks) return null;
|
|
1430
|
-
return /* @__PURE__ */ jsx(FenceBlocks, {
|
|
1431
|
-
blocks,
|
|
1432
|
-
runtime,
|
|
1433
|
-
threadId,
|
|
1434
|
-
isStreaming: isStreaming && isLast,
|
|
1435
|
-
markdownProps
|
|
1436
|
-
}, `a2ui-${segment.index}`);
|
|
1437
|
-
}) });
|
|
1438
|
-
}
|
|
1439
|
-
|
|
1440
|
-
//#endregion
|
|
1441
|
-
export { useShallowStableRef as A, MarkdownRendererProvider as C, ControlledModalOpenScope as D, renderSlot as E, IconSvg as M, cn as N, CopilotChatConfigurationProvider as O, StreamingMarkdownDefaultRenderer as P, defineToolCallRenderer as S, isReactComponentType as T, GenerativeUIRuntimeContext as _, GenerativeUIToolCallRenderer as a, catalogIdsOf as b, SurfaceView as c, useGenerativeUIAccessibility as d, wrapperAria as f, GenerativeUINodeContext as g, useManagerVersion as h, GenerativeUIChatSlotContext as i, CopilotIcon as j, useCopilotChatConfiguration as k, controlAria as l, useGenerativeUIState as m, ProviderMarkdown as n, createGenerativeUIActivityRenderer as o, useGenerativeUIFunction as p, DefaultGenerativeUIChatCard as r, createGenerativeUIFunctionCallActivityRenderer as s, MarkdownSurfaces as t, namedRole as u, useGenerativeUIRuntime as v, useMarkdownRenderer as w, createGenerativeUI$1 as x, useRequiredGenerativeUIRuntime as y };
|
|
1442
|
-
//# sourceMappingURL=MarkdownSurfaces-BQ1HGCzB.mjs.map
|
|
1599
|
+
export { shallowEqual as A, GenerativeUIRuntimeContext as C, ControlledModalOpenScope as D, defineToolCallRenderer as E, CopilotChatConfigurationProvider as O, GenerativeUINodeContext as S, useRequiredGenerativeUIRuntime as T, useGenerativeUIActivitySurface as _, createGenerativeUIFunctionCallActivityRenderer as a, useGenerativeUIState as b, namedRole as c, GenerativeUIHostContext as d, useGenerativeUIHost as f, parseGenerativeUISurfaceContent as g, GenerativeUISurfaceContentSchema as h, createGenerativeUIActivityRenderer as i, useShallowStableRef as j, useCopilotChatConfiguration as k, useGenerativeUIAccessibility as l, createGenerativeUI$1 as m, GenerativeUIChatSlotContext as n, SurfaceView as o, catalogIdsOf as p, GenerativeUIToolCallRenderer as r, controlAria as s, DefaultGenerativeUIChatCard as t, wrapperAria as u, useGenerativeUIRejected as v, useGenerativeUIRuntime as w, useManagerVersion as x, useGenerativeUIFunction as y };
|
|
1600
|
+
//# sourceMappingURL=chat-Bf0vVdo7.mjs.map
|