@copilotkit/shared 2.0.0-rc.3 → 2.0.0-rc.31
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 +3 -14
- package/dist/a2ui-envelope.cjs +2 -2
- package/dist/a2ui-envelope.cjs.map +1 -1
- package/dist/a2ui-envelope.d.cts +1 -1
- package/dist/a2ui-envelope.d.cts.map +1 -1
- package/dist/a2ui-envelope.d.mts +1 -1
- package/dist/a2ui-envelope.d.mts.map +1 -1
- package/dist/a2ui-envelope.mjs +2 -2
- package/dist/a2ui-envelope.mjs.map +1 -1
- package/dist/a2ui-prompts.cjs +21 -62
- package/dist/a2ui-prompts.cjs.map +1 -1
- package/dist/a2ui-prompts.d.cts +8 -1
- package/dist/a2ui-prompts.d.cts.map +1 -1
- package/dist/a2ui-prompts.d.mts +8 -1
- package/dist/a2ui-prompts.d.mts.map +1 -1
- package/dist/a2ui-prompts.mjs +22 -62
- package/dist/a2ui-prompts.mjs.map +1 -1
- package/dist/attachments/types.d.cts +1 -3
- package/dist/attachments/types.d.cts.map +1 -1
- package/dist/attachments/types.d.mts +1 -3
- package/dist/attachments/types.d.mts.map +1 -1
- package/dist/attachments/utils.cjs +16 -5
- package/dist/attachments/utils.cjs.map +1 -1
- package/dist/attachments/utils.d.cts +3 -1
- package/dist/attachments/utils.d.cts.map +1 -1
- package/dist/attachments/utils.d.mts +3 -1
- package/dist/attachments/utils.d.mts.map +1 -1
- package/dist/attachments/utils.mjs +16 -5
- package/dist/attachments/utils.mjs.map +1 -1
- package/dist/finalize-events.cjs +16 -0
- package/dist/finalize-events.cjs.map +1 -1
- package/dist/finalize-events.d.cts.map +1 -1
- package/dist/finalize-events.d.mts.map +1 -1
- package/dist/finalize-events.mjs +16 -0
- package/dist/finalize-events.mjs.map +1 -1
- package/dist/generative-ui/basic-functions.cjs +1 -1
- package/dist/generative-ui/basic-functions.cjs.map +1 -1
- package/dist/generative-ui/basic-functions.mjs +1 -1
- package/dist/generative-ui/basic-functions.mjs.map +1 -1
- package/dist/generative-ui/child-refs.cjs +3 -3
- package/dist/generative-ui/child-refs.cjs.map +1 -1
- package/dist/generative-ui/child-refs.d.cts.map +1 -1
- package/dist/generative-ui/child-refs.d.mts.map +1 -1
- package/dist/generative-ui/child-refs.mjs +3 -3
- package/dist/generative-ui/child-refs.mjs.map +1 -1
- package/dist/generative-ui/compact-schema.cjs +187 -0
- package/dist/generative-ui/compact-schema.cjs.map +1 -0
- package/dist/generative-ui/compact-schema.d.cts +55 -0
- package/dist/generative-ui/compact-schema.d.cts.map +1 -0
- package/dist/generative-ui/compact-schema.d.mts +55 -0
- package/dist/generative-ui/compact-schema.d.mts.map +1 -0
- package/dist/generative-ui/compact-schema.mjs +185 -0
- package/dist/generative-ui/compact-schema.mjs.map +1 -0
- package/dist/generative-ui/prompt-spec.cjs +225 -0
- package/dist/generative-ui/prompt-spec.cjs.map +1 -0
- package/dist/generative-ui/prompt-spec.d.cts +94 -0
- package/dist/generative-ui/prompt-spec.d.cts.map +1 -0
- package/dist/generative-ui/prompt-spec.d.mts +94 -0
- package/dist/generative-ui/prompt-spec.d.mts.map +1 -0
- package/dist/generative-ui/prompt-spec.mjs +220 -0
- package/dist/generative-ui/prompt-spec.mjs.map +1 -0
- package/dist/generative-ui/prompt.cjs +10 -36
- package/dist/generative-ui/prompt.cjs.map +1 -1
- package/dist/generative-ui/prompt.d.cts +2 -1
- package/dist/generative-ui/prompt.d.cts.map +1 -1
- package/dist/generative-ui/prompt.d.mts +2 -1
- package/dist/generative-ui/prompt.d.mts.map +1 -1
- package/dist/generative-ui/prompt.mjs +12 -38
- package/dist/generative-ui/prompt.mjs.map +1 -1
- package/dist/generative-ui/render-ui-args.cjs +1 -0
- package/dist/generative-ui/render-ui-args.cjs.map +1 -1
- package/dist/generative-ui/render-ui-args.d.cts.map +1 -1
- package/dist/generative-ui/render-ui-args.d.mts.map +1 -1
- package/dist/generative-ui/render-ui-args.mjs +1 -0
- package/dist/generative-ui/render-ui-args.mjs.map +1 -1
- package/dist/generative-ui/schema-render.cjs +242 -0
- package/dist/generative-ui/schema-render.cjs.map +1 -0
- package/dist/generative-ui/schema-render.d.cts +44 -0
- package/dist/generative-ui/schema-render.d.cts.map +1 -0
- package/dist/generative-ui/schema-render.d.mts +44 -0
- package/dist/generative-ui/schema-render.d.mts.map +1 -0
- package/dist/generative-ui/schema-render.mjs +239 -0
- package/dist/generative-ui/schema-render.mjs.map +1 -0
- package/dist/generative-ui/types.cjs +2 -2
- package/dist/generative-ui/types.cjs.map +1 -1
- package/dist/generative-ui/types.d.cts +7 -7
- package/dist/generative-ui/types.d.cts.map +1 -1
- package/dist/generative-ui/types.d.mts +7 -7
- package/dist/generative-ui/types.d.mts.map +1 -1
- package/dist/generative-ui/types.mjs +2 -2
- package/dist/generative-ui/types.mjs.map +1 -1
- package/dist/generative-ui/validate-operation.cjs +2 -2
- package/dist/generative-ui/validate-operation.cjs.map +1 -1
- package/dist/generative-ui/validate-operation.mjs +2 -2
- package/dist/generative-ui/validate-operation.mjs.map +1 -1
- package/dist/index.cjs +16 -15
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +10 -10
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +10 -10
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +9 -8
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +866 -455
- package/dist/index.umd.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/dist/standard-schema.cjs +20 -7
- package/dist/standard-schema.cjs.map +1 -1
- package/dist/standard-schema.d.cts +15 -16
- package/dist/standard-schema.d.cts.map +1 -1
- package/dist/standard-schema.d.mts +15 -16
- package/dist/standard-schema.d.mts.map +1 -1
- package/dist/standard-schema.mjs +20 -8
- package/dist/standard-schema.mjs.map +1 -1
- package/dist/telemetry/lambda-client.cjs +1 -1
- package/dist/telemetry/lambda-client.cjs.map +1 -1
- package/dist/telemetry/lambda-client.d.cts.map +1 -1
- package/dist/telemetry/lambda-client.d.mts.map +1 -1
- package/dist/telemetry/lambda-client.mjs +1 -1
- package/dist/telemetry/lambda-client.mjs.map +1 -1
- package/dist/telemetry/telemetry-disabled.cjs +0 -5
- package/dist/telemetry/telemetry-disabled.cjs.map +1 -1
- package/dist/telemetry/telemetry-disabled.d.cts +0 -5
- package/dist/telemetry/telemetry-disabled.d.cts.map +1 -1
- package/dist/telemetry/telemetry-disabled.d.mts +0 -5
- package/dist/telemetry/telemetry-disabled.d.mts.map +1 -1
- package/dist/telemetry/telemetry-disabled.mjs +0 -5
- package/dist/telemetry/telemetry-disabled.mjs.map +1 -1
- package/dist/types/message.d.cts +1 -18
- package/dist/types/message.d.cts.map +1 -1
- package/dist/types/message.d.mts +1 -18
- package/dist/types/message.d.mts.map +1 -1
- package/dist/utils/errors.cjs +5 -42
- package/dist/utils/errors.cjs.map +1 -1
- package/dist/utils/errors.d.cts +16 -34
- package/dist/utils/errors.d.cts.map +1 -1
- package/dist/utils/errors.d.mts +16 -34
- package/dist/utils/errors.d.mts.map +1 -1
- package/dist/utils/errors.mjs +6 -41
- package/dist/utils/errors.mjs.map +1 -1
- package/dist/utils/index.cjs +0 -2
- package/dist/utils/index.cjs.map +1 -1
- package/dist/utils/index.d.cts +4 -6
- package/dist/utils/index.d.cts.map +1 -1
- package/dist/utils/index.d.mts +4 -6
- package/dist/utils/index.d.mts.map +1 -1
- package/dist/utils/index.mjs +4 -6
- package/dist/utils/index.mjs.map +1 -1
- package/dist/utils/inspector-visibility.cjs +25 -3
- package/dist/utils/inspector-visibility.cjs.map +1 -1
- package/dist/utils/inspector-visibility.d.cts +26 -3
- package/dist/utils/inspector-visibility.d.cts.map +1 -1
- package/dist/utils/inspector-visibility.d.mts +26 -3
- package/dist/utils/inspector-visibility.d.mts.map +1 -1
- package/dist/utils/inspector-visibility.mjs +25 -4
- package/dist/utils/inspector-visibility.mjs.map +1 -1
- package/dist/utils/json-schema.cjs +4 -79
- package/dist/utils/json-schema.cjs.map +1 -1
- package/dist/utils/json-schema.d.cts +1 -4
- package/dist/utils/json-schema.d.cts.map +1 -1
- package/dist/utils/json-schema.d.mts +1 -4
- package/dist/utils/json-schema.d.mts.map +1 -1
- package/dist/utils/json-schema.mjs +5 -78
- package/dist/utils/json-schema.mjs.map +1 -1
- package/dist/utils/random-id.cjs +13 -26
- package/dist/utils/random-id.cjs.map +1 -1
- package/dist/utils/random-id.d.cts +6 -3
- package/dist/utils/random-id.d.cts.map +1 -1
- package/dist/utils/random-id.d.mts +6 -3
- package/dist/utils/random-id.d.mts.map +1 -1
- package/dist/utils/random-id.mjs +14 -25
- package/dist/utils/random-id.mjs.map +1 -1
- package/package.json +10 -34
- package/scripts/verify-cjs-exports.cjs +1 -1
- package/src/__tests__/a2ui-envelope.test.ts +18 -13
- package/src/__tests__/a2ui-prompts.test.ts +47 -14
- package/src/__tests__/fixtures/prompt-goldens.json +354 -0
- package/src/__tests__/prompt-spec.test.ts +191 -0
- package/src/__tests__/root-entry-browser-safety.test.ts +5 -7
- package/src/__tests__/schema-render.test.ts +245 -0
- package/src/__tests__/standard-schema.test.ts +61 -101
- package/src/a2ui-envelope.ts +7 -6
- package/src/a2ui-prompts.ts +47 -96
- package/src/attachments/__tests__/utils.test.ts +22 -0
- package/src/attachments/types.ts +1 -3
- package/src/attachments/utils.ts +23 -5
- package/src/finalize-events.ts +16 -0
- package/src/generative-ui/__tests__/render-ui-args.test.ts +7 -7
- package/src/generative-ui/__tests__/validate-operation.test.ts +19 -13
- package/src/generative-ui/basic-functions.ts +1 -1
- package/src/generative-ui/child-refs.ts +5 -4
- package/src/generative-ui/compact-schema.ts +277 -0
- package/src/generative-ui/index.ts +3 -0
- package/src/generative-ui/prompt-spec.ts +335 -0
- package/src/generative-ui/prompt.ts +25 -75
- package/src/generative-ui/render-ui-args.ts +3 -0
- package/src/generative-ui/schema-render.ts +377 -0
- package/src/generative-ui/types.ts +10 -7
- package/src/generative-ui/validate-operation.ts +2 -2
- package/src/index.ts +3 -16
- package/src/standard-schema.ts +24 -27
- package/src/telemetry/lambda-client.ts +8 -3
- package/src/telemetry/model-host.test.ts +2 -5
- package/src/telemetry/telemetry-disabled.ts +0 -5
- package/src/types/message.ts +1 -19
- package/src/utils/__tests__/errors.test.ts +44 -0
- package/src/utils/__tests__/json-schema.test.ts +3 -479
- package/src/utils/__tests__/random-id.test.ts +37 -0
- package/src/utils/errors.ts +28 -85
- package/src/utils/index.ts +0 -2
- package/src/utils/inspector-visibility.test.ts +26 -0
- package/src/utils/inspector-visibility.ts +35 -3
- package/src/utils/json-schema.ts +5 -152
- package/src/utils/random-id.ts +17 -37
- package/tsconfig.json +3 -1
- package/tsdown.config.ts +2 -19
- package/dist/event-transforms/index.cjs +0 -15
- package/dist/event-transforms/index.d.cts +0 -4
- package/dist/event-transforms/index.d.mts +0 -4
- package/dist/event-transforms/index.mjs +0 -5
- package/dist/event-transforms/open-generative-ui-middleware.cjs +0 -286
- package/dist/event-transforms/open-generative-ui-middleware.cjs.map +0 -1
- package/dist/event-transforms/open-generative-ui-middleware.d.cts +0 -60
- package/dist/event-transforms/open-generative-ui-middleware.d.cts.map +0 -1
- package/dist/event-transforms/open-generative-ui-middleware.d.mts +0 -60
- package/dist/event-transforms/open-generative-ui-middleware.d.mts.map +0 -1
- package/dist/event-transforms/open-generative-ui-middleware.mjs +0 -283
- package/dist/event-transforms/open-generative-ui-middleware.mjs.map +0 -1
- package/dist/event-transforms/recorded-events.cjs +0 -59
- package/dist/event-transforms/recorded-events.cjs.map +0 -1
- package/dist/event-transforms/recorded-events.d.cts +0 -23
- package/dist/event-transforms/recorded-events.d.cts.map +0 -1
- package/dist/event-transforms/recorded-events.d.mts +0 -23
- package/dist/event-transforms/recorded-events.d.mts.map +0 -1
- package/dist/event-transforms/recorded-events.mjs +0 -58
- package/dist/event-transforms/recorded-events.mjs.map +0 -1
- package/dist/telemetry/events.d.cts +0 -72
- package/dist/telemetry/events.d.cts.map +0 -1
- package/dist/telemetry/events.d.mts +0 -72
- package/dist/telemetry/events.d.mts.map +0 -1
- package/dist/telemetry/index.cjs +0 -18
- package/dist/telemetry/index.d.cts +0 -5
- package/dist/telemetry/index.d.mts +0 -5
- package/dist/telemetry/index.mjs +0 -6
- package/dist/telemetry/telemetry-client.cjs +0 -165
- package/dist/telemetry/telemetry-client.cjs.map +0 -1
- package/dist/telemetry/telemetry-client.d.cts +0 -87
- package/dist/telemetry/telemetry-client.d.cts.map +0 -1
- package/dist/telemetry/telemetry-client.d.mts +0 -87
- package/dist/telemetry/telemetry-client.d.mts.map +0 -1
- package/dist/telemetry/telemetry-client.mjs +0 -164
- package/dist/telemetry/telemetry-client.mjs.map +0 -1
- package/dist/telemetry/utils.cjs +0 -14
- package/dist/telemetry/utils.cjs.map +0 -1
- package/dist/telemetry/utils.mjs +0 -13
- package/dist/telemetry/utils.mjs.map +0 -1
- package/dist/utils/conditions.cjs +0 -32
- package/dist/utils/conditions.cjs.map +0 -1
- package/dist/utils/conditions.d.cts +0 -31
- package/dist/utils/conditions.d.cts.map +0 -1
- package/dist/utils/conditions.d.mts +0 -31
- package/dist/utils/conditions.d.mts.map +0 -1
- package/dist/utils/conditions.mjs +0 -31
- package/dist/utils/conditions.mjs.map +0 -1
- package/dist/utils/console-styling.cjs +0 -91
- package/dist/utils/console-styling.cjs.map +0 -1
- package/dist/utils/console-styling.d.cts +0 -65
- package/dist/utils/console-styling.d.cts.map +0 -1
- package/dist/utils/console-styling.d.mts +0 -65
- package/dist/utils/console-styling.d.mts.map +0 -1
- package/dist/utils/console-styling.mjs +0 -85
- package/dist/utils/console-styling.mjs.map +0 -1
- package/src/__tests__/zod-regression.test.ts +0 -319
- package/src/event-transforms/index.ts +0 -5
- package/src/event-transforms/open-generative-ui-middleware.ts +0 -392
- package/src/event-transforms/recorded-events.test.ts +0 -166
- package/src/event-transforms/recorded-events.ts +0 -74
- package/src/telemetry/events.ts +0 -68
- package/src/telemetry/index.ts +0 -9
- package/src/telemetry/telemetry-blank-license-identity.test.ts +0 -128
- package/src/telemetry/telemetry-client.test.ts +0 -798
- package/src/telemetry/telemetry-client.ts +0 -328
- package/src/telemetry/utils.ts +0 -15
- package/src/utils/__tests__/conditions.test.ts +0 -161
- package/src/utils/conditions.ts +0 -116
- package/src/utils/console-styling.ts +0 -126
|
@@ -0,0 +1,242 @@
|
|
|
1
|
+
const require_basic_functions = require('./basic-functions.cjs');
|
|
2
|
+
const require_types = require('./types.cjs');
|
|
3
|
+
const require_compact_schema = require('./compact-schema.cjs');
|
|
4
|
+
|
|
5
|
+
//#region src/generative-ui/schema-render.ts
|
|
6
|
+
/** An own key (shared compiles to ES2017, which has no Object.hasOwn). */
|
|
7
|
+
const hasOwn = (target, key) => Object.prototype.hasOwnProperty.call(target, key);
|
|
8
|
+
function isRecord(value) {
|
|
9
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10
|
+
}
|
|
11
|
+
/** The props schema without the description of each prop. */
|
|
12
|
+
function withoutPropDescriptions(schema) {
|
|
13
|
+
if (!isRecord(schema.properties)) return schema;
|
|
14
|
+
return {
|
|
15
|
+
...schema,
|
|
16
|
+
properties: Object.fromEntries(Object.entries(schema.properties).map(([key, value]) => {
|
|
17
|
+
if (!isRecord(value)) return [key, value];
|
|
18
|
+
const { description: _description, ...rest } = value;
|
|
19
|
+
return [key, rest];
|
|
20
|
+
}))
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** The short signature of a function: its built-in signature, or its schema made short. */
|
|
24
|
+
function functionSignature(fn) {
|
|
25
|
+
const signature = fn.parameters.description;
|
|
26
|
+
return typeof signature === "string" && signature.startsWith("{") && Object.keys(fn.parameters).length === 2 ? signature : require_compact_schema.compactType(fn.parameters, 0);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A function name with its parameters: `pick{ id: string }`, `clearCart{}`
|
|
30
|
+
* for no arguments, `setVolume(number)` for a parameter that is not an object.
|
|
31
|
+
*/
|
|
32
|
+
function nameWithParameters(name, parameters) {
|
|
33
|
+
if (parameters.startsWith("{")) return `${name}${parameters}`;
|
|
34
|
+
if (parameters === "object") return `${name}{}`;
|
|
35
|
+
return `${name}(${parameters})`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* One component as a TypeScript-like signature line, with its events and
|
|
39
|
+
* slots on the lines below:
|
|
40
|
+
* `- Text(text?: string // The text): Shows text`.
|
|
41
|
+
*/
|
|
42
|
+
function signatureLines(name, component, settings) {
|
|
43
|
+
const lines = [`- ${name}(${Object.entries(require_compact_schema.compactProps(component.props, settings.propDescriptions)).map(([key, type]) => `${key}: ${type}`).join(", ")})${component.description ? `: ${oneLine(component.description)}` : ""}`];
|
|
44
|
+
const events = Object.entries(component.events ?? {}).map(([event, definition]) => `${event}(${definition.description ? oneLine(definition.description) : require_compact_schema.compactType(definition.parameters, 0)})`);
|
|
45
|
+
if (events.length > 0) lines.push(` events: ${events.join(", ")}`);
|
|
46
|
+
const slots = Object.entries(component.slots ?? {}).map(([slot, kind]) => `${slot} (${kind})`);
|
|
47
|
+
if (slots.length > 0) lines.push(` slots: ${slots.join(", ")}`);
|
|
48
|
+
return lines;
|
|
49
|
+
}
|
|
50
|
+
/** The whole schema as signature text: targets, functions, components, surfaces. */
|
|
51
|
+
function signatureSchema(manifest, settings) {
|
|
52
|
+
const targets = manifest.targets.map((t) => {
|
|
53
|
+
const limit = t.components ? `; only ${t.components.join(", ")}` : "";
|
|
54
|
+
return `- ${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : ""}`;
|
|
55
|
+
});
|
|
56
|
+
const functions = Object.entries(manifest.functions).map(([name, fn]) => {
|
|
57
|
+
const callers = require_basic_functions.functionCallers(fn);
|
|
58
|
+
const who = callers.agent && require_types.GENERATIVE_UI_AGENT_FUNCTION_CALLS ? callers.renderer ? " (UI or agent)" : " (agent only)" : "";
|
|
59
|
+
return `- ${nameWithParameters(name, functionSignature(fn))}${who}${fn.description ? `: ${oneLine(fn.description)}` : ""}`;
|
|
60
|
+
});
|
|
61
|
+
const components = Object.entries(manifest.components).flatMap(([name, component]) => signatureLines(name, component, settings));
|
|
62
|
+
const surfaces = Object.entries(manifest.surfaces).map(([id, s]) => `- ${id} (target ${s.target})${s.status ? ` status: ${s.status}` : ""}${s.dataModel ? ` user values: ${JSON.stringify(s.dataModel)}` : ""}${(s.rejected ?? []).map((r) => ` rejected: ${r}`).join("")}`);
|
|
63
|
+
return [
|
|
64
|
+
`Catalog: ${manifest.catalogId}`,
|
|
65
|
+
`Targets:\n${targets.join("\n") || "- none"}`,
|
|
66
|
+
`Functions:\n${functions.join("\n") || "- none"}`,
|
|
67
|
+
`Components:\n${components.join("\n") || "- none"}`,
|
|
68
|
+
`Surfaces:\n${surfaces.join("\n") || "- none"}`
|
|
69
|
+
].join("\n\n");
|
|
70
|
+
}
|
|
71
|
+
/** A description on one line, so its lines cannot read as new entries. */
|
|
72
|
+
function oneLine(text) {
|
|
73
|
+
return text.replace(/\s*\n\s*/g, " ");
|
|
74
|
+
}
|
|
75
|
+
/** Values that would read as a type or a number once their quotes are gone. */
|
|
76
|
+
const AMBIGUOUS_VALUE = /^(?:string|number|integer|boolean|true|false|null|object|array|unknown|any|-?\d[\d.]*)$/;
|
|
77
|
+
/**
|
|
78
|
+
* A compact type with bare enum values and tight unions:
|
|
79
|
+
* `"sm" | "md" | "lg"` becomes `sm|md|lg`. A union keeps its quotes when it
|
|
80
|
+
* also holds a type (`"sm" | number`) or a value that reads as a type or a
|
|
81
|
+
* number (`"number" | "currency"`, `"1" | "2"`), and so do values that are
|
|
82
|
+
* not plain words.
|
|
83
|
+
*/
|
|
84
|
+
function bareType(type) {
|
|
85
|
+
return type.replace(/"[A-Za-z0-9_#%.-]+"(?: \| "[A-Za-z0-9_#%.-]+")*(?! \|)/g, (union, offset, whole) => {
|
|
86
|
+
if (whole.slice(offset - 2, offset) === "| ") return union;
|
|
87
|
+
const values = union.split(" | ").map((v) => v.slice(1, -1));
|
|
88
|
+
if (values.some((v) => AMBIGUOUS_VALUE.test(v))) return union;
|
|
89
|
+
return values.join("|");
|
|
90
|
+
}).replace(/ \| /g, "|");
|
|
91
|
+
}
|
|
92
|
+
/** A compact prop (`type // description`) as `name: type - description`. */
|
|
93
|
+
function propLine(key, value) {
|
|
94
|
+
const name = key.endsWith("?") ? key.slice(0, -1) : `${key}*`;
|
|
95
|
+
const cut = value.indexOf(" // ");
|
|
96
|
+
const type = bareType(cut < 0 ? value : value.slice(0, cut));
|
|
97
|
+
const text = cut < 0 ? "" : value.slice(cut + 4).trim();
|
|
98
|
+
return ` ${name}: ${type}${text ? ` - ${oneLine(text)}` : ""}`;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The whole schema as plain lines: a header line per component and one
|
|
102
|
+
* indented line per prop: events are `action` props and slots `componentId`
|
|
103
|
+
* or `childList` props. Props are optional unless marked `*`, enum values
|
|
104
|
+
* are bare, and the common props are listed once. It carries the same names, types and descriptions as
|
|
105
|
+
* `compact`, in fewer tokens.
|
|
106
|
+
*/
|
|
107
|
+
function linesSchema(manifest, settings) {
|
|
108
|
+
const compact = require_compact_schema.buildCompactGenerativeUISchema(manifest, {
|
|
109
|
+
propDescriptions: settings.propDescriptions,
|
|
110
|
+
commonProps: settings.commonProps
|
|
111
|
+
});
|
|
112
|
+
const targets = compact.targets.map((t) => {
|
|
113
|
+
const limit = t.components ? `, only ${t.components.join(", ")}` : "";
|
|
114
|
+
return `${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : ""}`;
|
|
115
|
+
}).join("; ");
|
|
116
|
+
const functions = Object.entries(compact.functions).map(([name, fn]) => `${nameWithParameters(name, fn.parameters)}${"callers" in fn ? ` (${fn.callers})` : ""}${fn.description ? `: ${oneLine(fn.description)}` : ""}`);
|
|
117
|
+
const common = compact.commonProps ? Object.entries(compact.commonProps).map(([k, v]) => propLine(k, v)) : [];
|
|
118
|
+
const components = Object.entries(compact.components).map(([name, c]) => ({
|
|
119
|
+
name,
|
|
120
|
+
c,
|
|
121
|
+
own: [...Object.entries(c.props).map(([k, v]) => propLine(k, v)), ...Object.entries(c.events ?? {}).map(([e, d]) => ` ${e}: action - ${oneLine(d)}`)]
|
|
122
|
+
})).flatMap(({ name, c, own }) => {
|
|
123
|
+
const lines = [`${name}${c.includesCommonProps ? " +common" : ""}${c.description ? `: ${oneLine(c.description)}` : ""}`, ...own];
|
|
124
|
+
for (const [slot, kind] of Object.entries(c.slots ?? {})) lines.push(` ${slot}: ${kind === "single" ? "componentId" : "childList"}`);
|
|
125
|
+
return lines;
|
|
126
|
+
});
|
|
127
|
+
const surfaces = Object.keys(compact.surfaces).length ? JSON.stringify(compact.surfaces) : "none";
|
|
128
|
+
return [
|
|
129
|
+
`Catalog ${compact.catalogId}. Targets: ${targets || "none"}`,
|
|
130
|
+
"Props are optional unless marked *. a|b|c is one of those strings, quoted when one reads as a type or a number. A component marked +common also takes every common prop. A prop of type action is an event prop: give it {\"event\":{...}} or {\"functionCall\":{...}}. A componentId prop takes one component id; a childList prop takes a list of ids or a template.",
|
|
131
|
+
`Functions:\n${functions.join("\n")}`,
|
|
132
|
+
...common.length ? [`Common props:\n${common.join("\n")}`] : [],
|
|
133
|
+
`Components:\n${components.join("\n")}`,
|
|
134
|
+
`Surfaces: ${surfaces}`
|
|
135
|
+
].join("\n\n");
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The component schema of the markdown transport, as the text of its
|
|
139
|
+
* context entry, in the notation of `settings`. `compact` is the schema that
|
|
140
|
+
* ships (`buildCompactGenerativeUISchema`), `json-schema` the manifest parts
|
|
141
|
+
* as JSON, `ts-signature` one line per component, `lines` one line per
|
|
142
|
+
* prop with bare enums (the fewest tokens).
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const value = renderGenerativeUISchema(manifest, spec.schema.markdown);
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
function renderGenerativeUISchema(manifest, settings) {
|
|
150
|
+
switch (settings.notation) {
|
|
151
|
+
case "compact": return JSON.stringify(require_compact_schema.buildCompactGenerativeUISchema(manifest, {
|
|
152
|
+
propDescriptions: settings.propDescriptions,
|
|
153
|
+
commonProps: settings.commonProps
|
|
154
|
+
}));
|
|
155
|
+
case "json-schema": {
|
|
156
|
+
const components = Object.fromEntries(Object.entries(manifest.components).map(([name, c]) => [name, settings.propDescriptions ? c : {
|
|
157
|
+
...c,
|
|
158
|
+
props: withoutPropDescriptions(c.props)
|
|
159
|
+
}]));
|
|
160
|
+
return JSON.stringify({
|
|
161
|
+
catalogId: manifest.catalogId,
|
|
162
|
+
targets: manifest.targets,
|
|
163
|
+
functions: manifest.functions,
|
|
164
|
+
components,
|
|
165
|
+
surfaces: manifest.surfaces
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
case "ts-signature": return signatureSchema(manifest, settings);
|
|
169
|
+
case "lines": return linesSchema(manifest, settings);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* The component list of the tool guide, one entry per component, in the
|
|
174
|
+
* notation of `settings`. `json-schema` is the list that ships.
|
|
175
|
+
*/
|
|
176
|
+
function renderGenerativeUIComponentList(components, settings) {
|
|
177
|
+
return Object.entries(components).map(([name, c]) => {
|
|
178
|
+
if (settings.notation === "ts-signature" || settings.notation === "lines") return signatureLines(name, c, settings).join("\n");
|
|
179
|
+
const props = settings.notation === "compact" ? require_compact_schema.compactProps(c.props, settings.propDescriptions) : settings.propDescriptions ? c.props : withoutPropDescriptions(c.props);
|
|
180
|
+
const lines = [`- **${name}**${c.description ? `: ${oneLine(c.description)}` : ""}`, ` props: ${JSON.stringify(props)}`];
|
|
181
|
+
if (c.events) lines.push(` event props: ${JSON.stringify(c.events)}`);
|
|
182
|
+
if (c.slots) lines.push(` slot props (component ids): ${JSON.stringify(c.slots)}`);
|
|
183
|
+
return lines.join("\n");
|
|
184
|
+
}).join("\n");
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* The manifest with replaced descriptions: `"Name"` replaces the
|
|
188
|
+
* description of a component, `"Name.prop"` the description of one of its
|
|
189
|
+
* props. Keys that name no component or prop are left out of the result
|
|
190
|
+
* and listed in `unknown`. The manifest is not changed.
|
|
191
|
+
*/
|
|
192
|
+
function applyDescriptionOverrides(manifest, overrides) {
|
|
193
|
+
const components = structuredClone(manifest.components);
|
|
194
|
+
const unknown = [];
|
|
195
|
+
for (const [key, description] of Object.entries(overrides)) {
|
|
196
|
+
const [name, prop, ...rest] = key.split(".");
|
|
197
|
+
const component = name !== void 0 && hasOwn(components, name) ? components[name] : void 0;
|
|
198
|
+
if (!component || rest.length > 0) {
|
|
199
|
+
unknown.push(key);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (prop === void 0) {
|
|
203
|
+
component.description = description;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
const properties = component.props.properties;
|
|
207
|
+
const property = isRecord(properties) && hasOwn(properties, prop) ? properties[prop] : void 0;
|
|
208
|
+
if (!isRecord(property)) {
|
|
209
|
+
unknown.push(key);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
property.description = description;
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
manifest: {
|
|
216
|
+
...manifest,
|
|
217
|
+
components
|
|
218
|
+
},
|
|
219
|
+
unknown
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* The components and props of `manifest` whose names do not appear in
|
|
224
|
+
* `rendered`. A schema notation that leaves one out would hide part of the
|
|
225
|
+
* catalog from the model.
|
|
226
|
+
*/
|
|
227
|
+
function schemaCoverage(manifest, rendered) {
|
|
228
|
+
const missing = [];
|
|
229
|
+
for (const [name, component] of Object.entries(manifest.components)) {
|
|
230
|
+
if (!rendered.includes(name)) missing.push(name);
|
|
231
|
+
const properties = isRecord(component.props.properties) ? component.props.properties : {};
|
|
232
|
+
for (const prop of Object.keys(properties)) if (!rendered.includes(prop)) missing.push(`${name}.${prop}`);
|
|
233
|
+
}
|
|
234
|
+
return { missing };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
//#endregion
|
|
238
|
+
exports.applyDescriptionOverrides = applyDescriptionOverrides;
|
|
239
|
+
exports.renderGenerativeUIComponentList = renderGenerativeUIComponentList;
|
|
240
|
+
exports.renderGenerativeUISchema = renderGenerativeUISchema;
|
|
241
|
+
exports.schemaCoverage = schemaCoverage;
|
|
242
|
+
//# sourceMappingURL=schema-render.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-render.cjs","names":["compactType","compactProps","functionCallers","GENERATIVE_UI_AGENT_FUNCTION_CALLS","buildCompactGenerativeUISchema"],"sources":["../../src/generative-ui/schema-render.ts"],"sourcesContent":["import { functionCallers } from \"./basic-functions\";\nimport {\n buildCompactGenerativeUISchema,\n compactProps,\n compactType,\n} from \"./compact-schema\";\nimport type { GenerativeUISchemaSettings } from \"./prompt-spec\";\nimport { GENERATIVE_UI_AGENT_FUNCTION_CALLS } from \"./types\";\nimport type {\n GenerativeUIComponentManifest,\n GenerativeUIJSONSchema,\n GenerativeUIManifest,\n} from \"./types\";\n\ntype SchemaManifest = Pick<\n GenerativeUIManifest,\n \"catalogId\" | \"components\" | \"functions\" | \"targets\" | \"surfaces\"\n>;\n\n/** An own key (shared compiles to ES2017, which has no Object.hasOwn). */\nconst hasOwn = (target: object, key: string) =>\n Object.prototype.hasOwnProperty.call(target, key);\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/** The props schema without the description of each prop. */\nfunction withoutPropDescriptions(\n schema: GenerativeUIJSONSchema,\n): GenerativeUIJSONSchema {\n if (!isRecord(schema.properties)) return schema;\n return {\n ...schema,\n properties: Object.fromEntries(\n Object.entries(schema.properties).map(([key, value]) => {\n if (!isRecord(value)) return [key, value];\n const { description: _description, ...rest } = value;\n return [key, rest];\n }),\n ),\n };\n}\n\n/** The short signature of a function: its built-in signature, or its schema made short. */\nfunction functionSignature(fn: GenerativeUIManifest[\"functions\"][string]) {\n const signature = fn.parameters.description;\n const hasSignature =\n typeof signature === \"string\" &&\n signature.startsWith(\"{\") &&\n Object.keys(fn.parameters).length === 2;\n return hasSignature ? signature : compactType(fn.parameters, 0);\n}\n\n/**\n * A function name with its parameters: `pick{ id: string }`, `clearCart{}`\n * for no arguments, `setVolume(number)` for a parameter that is not an object.\n */\nfunction nameWithParameters(name: string, parameters: string) {\n if (parameters.startsWith(\"{\")) return `${name}${parameters}`;\n if (parameters === \"object\") return `${name}{}`;\n return `${name}(${parameters})`;\n}\n\n/**\n * One component as a TypeScript-like signature line, with its events and\n * slots on the lines below:\n * `- Text(text?: string // The text): Shows text`.\n */\nfunction signatureLines(\n name: string,\n component: GenerativeUIComponentManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const props = Object.entries(\n compactProps(component.props, settings.propDescriptions),\n ).map(([key, type]) => `${key}: ${type}`);\n const lines = [\n `- ${name}(${props.join(\", \")})${component.description ? `: ${oneLine(component.description)}` : \"\"}`,\n ];\n const events = Object.entries(component.events ?? {}).map(\n ([event, definition]) =>\n `${event}(${definition.description ? oneLine(definition.description) : compactType(definition.parameters, 0)})`,\n );\n if (events.length > 0) lines.push(` events: ${events.join(\", \")}`);\n const slots = Object.entries(component.slots ?? {}).map(\n ([slot, kind]) => `${slot} (${kind})`,\n );\n if (slots.length > 0) lines.push(` slots: ${slots.join(\", \")}`);\n return lines;\n}\n\n/** The whole schema as signature text: targets, functions, components, surfaces. */\nfunction signatureSchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const targets = manifest.targets.map((t) => {\n const limit = t.components ? `; only ${t.components.join(\", \")}` : \"\";\n return `- ${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : \"\"}`;\n });\n const functions = Object.entries(manifest.functions).map(([name, fn]) => {\n const callers = functionCallers(fn);\n const who =\n callers.agent && GENERATIVE_UI_AGENT_FUNCTION_CALLS\n ? callers.renderer\n ? \" (UI or agent)\"\n : \" (agent only)\"\n : \"\";\n return `- ${nameWithParameters(name, functionSignature(fn))}${who}${fn.description ? `: ${oneLine(fn.description)}` : \"\"}`;\n });\n const components = Object.entries(manifest.components).flatMap(\n ([name, component]) => signatureLines(name, component, settings),\n );\n const surfaces = Object.entries(manifest.surfaces).map(\n ([id, s]) =>\n `- ${id} (target ${s.target})${s.status ? ` status: ${s.status}` : \"\"}${s.dataModel ? ` user values: ${JSON.stringify(s.dataModel)}` : \"\"}${(s.rejected ?? []).map((r) => ` rejected: ${r}`).join(\"\")}`,\n );\n return [\n `Catalog: ${manifest.catalogId}`,\n `Targets:\\n${targets.join(\"\\n\") || \"- none\"}`,\n `Functions:\\n${functions.join(\"\\n\") || \"- none\"}`,\n `Components:\\n${components.join(\"\\n\") || \"- none\"}`,\n `Surfaces:\\n${surfaces.join(\"\\n\") || \"- none\"}`,\n ].join(\"\\n\\n\");\n}\n\n/** A description on one line, so its lines cannot read as new entries. */\nfunction oneLine(text: string) {\n return text.replace(/\\s*\\n\\s*/g, \" \");\n}\n\n/** Values that would read as a type or a number once their quotes are gone. */\nconst AMBIGUOUS_VALUE =\n /^(?:string|number|integer|boolean|true|false|null|object|array|unknown|any|-?\\d[\\d.]*)$/;\n\n/**\n * A compact type with bare enum values and tight unions:\n * `\"sm\" | \"md\" | \"lg\"` becomes `sm|md|lg`. A union keeps its quotes when it\n * also holds a type (`\"sm\" | number`) or a value that reads as a type or a\n * number (`\"number\" | \"currency\"`, `\"1\" | \"2\"`), and so do values that are\n * not plain words.\n */\nfunction bareType(type: string) {\n return type\n .replace(\n /\"[A-Za-z0-9_#%.-]+\"(?: \\| \"[A-Za-z0-9_#%.-]+\")*(?! \\|)/g,\n (union: string, offset: number, whole: string) => {\n if (whole.slice(offset - 2, offset) === \"| \") return union;\n const values = union.split(\" | \").map((v) => v.slice(1, -1));\n if (values.some((v) => AMBIGUOUS_VALUE.test(v))) return union;\n return values.join(\"|\");\n },\n )\n .replace(/ \\| /g, \"|\");\n}\n\n/** A compact prop (`type // description`) as `name: type - description`. */\nfunction propLine(key: string, value: string) {\n const name = key.endsWith(\"?\") ? key.slice(0, -1) : `${key}*`;\n const cut = value.indexOf(\" // \");\n const type = bareType(cut < 0 ? value : value.slice(0, cut));\n const text = cut < 0 ? \"\" : value.slice(cut + 4).trim();\n const description = text ? ` - ${oneLine(text)}` : \"\";\n return ` ${name}: ${type}${description}`;\n}\n\n/**\n * The whole schema as plain lines: a header line per component and one\n * indented line per prop: events are `action` props and slots `componentId`\n * or `childList` props. Props are optional unless marked `*`, enum values\n * are bare, and the common props are listed once. It carries the same names, types and descriptions as\n * `compact`, in fewer tokens.\n */\nfunction linesSchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n) {\n const compact = buildCompactGenerativeUISchema(manifest, {\n propDescriptions: settings.propDescriptions,\n commonProps: settings.commonProps,\n });\n const targets = compact.targets\n .map((t) => {\n const limit = t.components ? `, only ${t.components.join(\", \")}` : \"\";\n return `${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : \"\"}`;\n })\n .join(\"; \");\n const functions = Object.entries(compact.functions).map(\n ([name, fn]) =>\n `${nameWithParameters(name, fn.parameters)}${\"callers\" in fn ? ` (${fn.callers})` : \"\"}${fn.description ? `: ${oneLine(fn.description)}` : \"\"}`,\n );\n const common = compact.commonProps\n ? Object.entries(compact.commonProps).map(([k, v]) => propLine(k, v))\n : [];\n const lineSets = Object.entries(compact.components).map(([name, c]) => ({\n name,\n c,\n own: [\n ...Object.entries(c.props).map(([k, v]) => propLine(k, v)),\n ...Object.entries(c.events ?? {}).map(\n ([e, d]) => ` ${e}: action - ${oneLine(d)}`,\n ),\n ],\n }));\n const components = lineSets.flatMap(({ name, c, own }) => {\n const lines = [\n `${name}${c.includesCommonProps ? \" +common\" : \"\"}${c.description ? `: ${oneLine(c.description)}` : \"\"}`,\n ...own,\n ];\n // Slots are props that take component ids.\n for (const [slot, kind] of Object.entries(c.slots ?? {})) {\n lines.push(\n ` ${slot}: ${kind === \"single\" ? \"componentId\" : \"childList\"}`,\n );\n }\n return lines;\n });\n const surfaces = Object.keys(compact.surfaces).length\n ? JSON.stringify(compact.surfaces)\n : \"none\";\n return [\n `Catalog ${compact.catalogId}. Targets: ${targets || \"none\"}`,\n 'Props are optional unless marked *. a|b|c is one of those strings, quoted when one reads as a type or a number. A component marked +common also takes every common prop. A prop of type action is an event prop: give it {\"event\":{...}} or {\"functionCall\":{...}}. A componentId prop takes one component id; a childList prop takes a list of ids or a template.',\n `Functions:\\n${functions.join(\"\\n\")}`,\n ...(common.length ? [`Common props:\\n${common.join(\"\\n\")}`] : []),\n `Components:\\n${components.join(\"\\n\")}`,\n `Surfaces: ${surfaces}`,\n ].join(\"\\n\\n\");\n}\n\n/**\n * The component schema of the markdown transport, as the text of its\n * context entry, in the notation of `settings`. `compact` is the schema that\n * ships (`buildCompactGenerativeUISchema`), `json-schema` the manifest parts\n * as JSON, `ts-signature` one line per component, `lines` one line per\n * prop with bare enums (the fewest tokens).\n *\n * @example\n * ```ts\n * const value = renderGenerativeUISchema(manifest, spec.schema.markdown);\n * ```\n */\nexport function renderGenerativeUISchema(\n manifest: SchemaManifest,\n settings: GenerativeUISchemaSettings,\n): string {\n switch (settings.notation) {\n case \"compact\":\n return JSON.stringify(\n buildCompactGenerativeUISchema(manifest, {\n propDescriptions: settings.propDescriptions,\n commonProps: settings.commonProps,\n }),\n );\n case \"json-schema\": {\n const components = Object.fromEntries(\n Object.entries(manifest.components).map(([name, c]) => [\n name,\n settings.propDescriptions\n ? c\n : { ...c, props: withoutPropDescriptions(c.props) },\n ]),\n );\n return JSON.stringify({\n catalogId: manifest.catalogId,\n targets: manifest.targets,\n functions: manifest.functions,\n components,\n surfaces: manifest.surfaces,\n });\n }\n case \"ts-signature\":\n return signatureSchema(manifest, settings);\n case \"lines\":\n return linesSchema(manifest, settings);\n }\n}\n\n/**\n * The component list of the tool guide, one entry per component, in the\n * notation of `settings`. `json-schema` is the list that ships.\n */\nexport function renderGenerativeUIComponentList(\n components: GenerativeUIManifest[\"components\"],\n settings: GenerativeUISchemaSettings,\n): string {\n return Object.entries(components)\n .map(([name, c]) => {\n if (\n settings.notation === \"ts-signature\" ||\n settings.notation === \"lines\"\n ) {\n return signatureLines(name, c, settings).join(\"\\n\");\n }\n const props =\n settings.notation === \"compact\"\n ? compactProps(c.props, settings.propDescriptions)\n : settings.propDescriptions\n ? c.props\n : withoutPropDescriptions(c.props);\n const lines = [\n `- **${name}**${c.description ? `: ${oneLine(c.description)}` : \"\"}`,\n ` props: ${JSON.stringify(props)}`,\n ];\n if (c.events) lines.push(` event props: ${JSON.stringify(c.events)}`);\n if (c.slots)\n lines.push(` slot props (component ids): ${JSON.stringify(c.slots)}`);\n return lines.join(\"\\n\");\n })\n .join(\"\\n\");\n}\n\n/**\n * The manifest with replaced descriptions: `\"Name\"` replaces the\n * description of a component, `\"Name.prop\"` the description of one of its\n * props. Keys that name no component or prop are left out of the result\n * and listed in `unknown`. The manifest is not changed.\n */\nexport function applyDescriptionOverrides<\n M extends Pick<GenerativeUIManifest, \"components\">,\n>(\n manifest: M,\n overrides: Record<string, string>,\n): { manifest: M; unknown: string[] } {\n const components = structuredClone(manifest.components);\n const unknown: string[] = [];\n for (const [key, description] of Object.entries(overrides)) {\n const [name, prop, ...rest] = key.split(\".\");\n // Own keys only: \"__proto__\" must never reach Object.prototype.\n const component =\n name !== undefined && hasOwn(components, name)\n ? components[name]\n : undefined;\n if (!component || rest.length > 0) {\n unknown.push(key);\n continue;\n }\n if (prop === undefined) {\n component.description = description;\n continue;\n }\n const properties = component.props.properties;\n const property =\n isRecord(properties) && hasOwn(properties, prop)\n ? properties[prop]\n : undefined;\n if (!isRecord(property)) {\n unknown.push(key);\n continue;\n }\n property.description = description;\n }\n return { manifest: { ...manifest, components }, unknown };\n}\n\n/**\n * The components and props of `manifest` whose names do not appear in\n * `rendered`. A schema notation that leaves one out would hide part of the\n * catalog from the model.\n */\nexport function schemaCoverage(\n manifest: Pick<GenerativeUIManifest, \"components\">,\n rendered: string,\n): { missing: string[] } {\n const missing: string[] = [];\n for (const [name, component] of Object.entries(manifest.components)) {\n if (!rendered.includes(name)) missing.push(name);\n const properties = isRecord(component.props.properties)\n ? component.props.properties\n : {};\n for (const prop of Object.keys(properties)) {\n if (!rendered.includes(prop)) missing.push(`${name}.${prop}`);\n }\n }\n return { missing };\n}\n"],"mappings":";;;;;;AAoBA,MAAM,UAAU,QAAgB,QAC9B,OAAO,UAAU,eAAe,KAAK,QAAQ,IAAI;AAEnD,SAAS,SAAS,OAAkD;AAClE,QAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,MAAM;;;AAI7E,SAAS,wBACP,QACwB;AACxB,KAAI,CAAC,SAAS,OAAO,WAAW,CAAE,QAAO;AACzC,QAAO;EACL,GAAG;EACH,YAAY,OAAO,YACjB,OAAO,QAAQ,OAAO,WAAW,CAAC,KAAK,CAAC,KAAK,WAAW;AACtD,OAAI,CAAC,SAAS,MAAM,CAAE,QAAO,CAAC,KAAK,MAAM;GACzC,MAAM,EAAE,aAAa,cAAc,GAAG,SAAS;AAC/C,UAAO,CAAC,KAAK,KAAK;IAClB,CACH;EACF;;;AAIH,SAAS,kBAAkB,IAA+C;CACxE,MAAM,YAAY,GAAG,WAAW;AAKhC,QAHE,OAAO,cAAc,YACrB,UAAU,WAAW,IAAI,IACzB,OAAO,KAAK,GAAG,WAAW,CAAC,WAAW,IAClB,YAAYA,mCAAY,GAAG,YAAY,EAAE;;;;;;AAOjE,SAAS,mBAAmB,MAAc,YAAoB;AAC5D,KAAI,WAAW,WAAW,IAAI,CAAE,QAAO,GAAG,OAAO;AACjD,KAAI,eAAe,SAAU,QAAO,GAAG,KAAK;AAC5C,QAAO,GAAG,KAAK,GAAG,WAAW;;;;;;;AAQ/B,SAAS,eACP,MACA,WACA,UACA;CAIA,MAAM,Q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,UAAoB,EAAE;AAC5B,MAAK,MAAM,CAAC,MAAM,cAAc,OAAO,QAAQ,SAAS,WAAW,EAAE;AACnE,MAAI,CAAC,SAAS,SAAS,KAAK,CAAE,SAAQ,KAAK,KAAK;EAChD,MAAM,aAAa,SAAS,UAAU,MAAM,WAAW,GACnD,UAAU,MAAM,aAChB,EAAE;AACN,OAAK,MAAM,QAAQ,OAAO,KAAK,WAAW,CACxC,KAAI,CAAC,SAAS,SAAS,KAAK,CAAE,SAAQ,KAAK,GAAG,KAAK,GAAG,OAAO;;AAGjE,QAAO,EAAE,SAAS"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { GenerativeUIManifest } from "./types.cjs";
|
|
2
|
+
import { GenerativeUISchemaSettings } from "./prompt-spec.cjs";
|
|
3
|
+
|
|
4
|
+
//#region src/generative-ui/schema-render.d.ts
|
|
5
|
+
type SchemaManifest = Pick<GenerativeUIManifest, "catalogId" | "components" | "functions" | "targets" | "surfaces">;
|
|
6
|
+
/**
|
|
7
|
+
* The component schema of the markdown transport, as the text of its
|
|
8
|
+
* context entry, in the notation of `settings`. `compact` is the schema that
|
|
9
|
+
* ships (`buildCompactGenerativeUISchema`), `json-schema` the manifest parts
|
|
10
|
+
* as JSON, `ts-signature` one line per component, `lines` one line per
|
|
11
|
+
* prop with bare enums (the fewest tokens).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const value = renderGenerativeUISchema(manifest, spec.schema.markdown);
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
declare function renderGenerativeUISchema(manifest: SchemaManifest, settings: GenerativeUISchemaSettings): string;
|
|
19
|
+
/**
|
|
20
|
+
* The component list of the tool guide, one entry per component, in the
|
|
21
|
+
* notation of `settings`. `json-schema` is the list that ships.
|
|
22
|
+
*/
|
|
23
|
+
declare function renderGenerativeUIComponentList(components: GenerativeUIManifest["components"], settings: GenerativeUISchemaSettings): string;
|
|
24
|
+
/**
|
|
25
|
+
* The manifest with replaced descriptions: `"Name"` replaces the
|
|
26
|
+
* description of a component, `"Name.prop"` the description of one of its
|
|
27
|
+
* props. Keys that name no component or prop are left out of the result
|
|
28
|
+
* and listed in `unknown`. The manifest is not changed.
|
|
29
|
+
*/
|
|
30
|
+
declare function applyDescriptionOverrides<M extends Pick<GenerativeUIManifest, "components">>(manifest: M, overrides: Record<string, string>): {
|
|
31
|
+
manifest: M;
|
|
32
|
+
unknown: string[];
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* The components and props of `manifest` whose names do not appear in
|
|
36
|
+
* `rendered`. A schema notation that leaves one out would hide part of the
|
|
37
|
+
* catalog from the model.
|
|
38
|
+
*/
|
|
39
|
+
declare function schemaCoverage(manifest: Pick<GenerativeUIManifest, "components">, rendered: string): {
|
|
40
|
+
missing: string[];
|
|
41
|
+
};
|
|
42
|
+
//#endregion
|
|
43
|
+
export { applyDescriptionOverrides, renderGenerativeUIComponentList, renderGenerativeUISchema, schemaCoverage };
|
|
44
|
+
//# sourceMappingURL=schema-render.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-render.d.cts","names":[],"sources":["../../src/generative-ui/schema-render.ts"],"mappings":";;;;KAcK,cAAA,GAAiB,IAAA,CACpB,oBAAA;;AAHe;;;;;AAuOjB;;;;;;iBAAgB,wBAAA,CACd,QAAA,EAAU,cAAA,EACV,QAAA,EAAU,0BAAA;;;;AAsCZ;iBAAgB,+BAAA,CACd,UAAA,EAAY,oBAAA,gBACZ,QAAA,EAAU,0BAAA;;;;;;;iBAkCI,yBAAA,WACJ,IAAA,CAAK,oBAAA,gBAAA,CAEf,QAAA,EAAU,CAAA,EACV,SAAA,EAAW,MAAA;EACR,QAAA,EAAU,CAAA;EAAG,OAAA;AAAA;;;;;;iBAqCF,cAAA,CACd,QAAA,EAAU,IAAA,CAAK,oBAAA,iBACf,QAAA;EACG,OAAA;AAAA"}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { GenerativeUIManifest } from "./types.mjs";
|
|
2
|
+
import { GenerativeUISchemaSettings } from "./prompt-spec.mjs";
|
|
3
|
+
|
|
4
|
+
//#region src/generative-ui/schema-render.d.ts
|
|
5
|
+
type SchemaManifest = Pick<GenerativeUIManifest, "catalogId" | "components" | "functions" | "targets" | "surfaces">;
|
|
6
|
+
/**
|
|
7
|
+
* The component schema of the markdown transport, as the text of its
|
|
8
|
+
* context entry, in the notation of `settings`. `compact` is the schema that
|
|
9
|
+
* ships (`buildCompactGenerativeUISchema`), `json-schema` the manifest parts
|
|
10
|
+
* as JSON, `ts-signature` one line per component, `lines` one line per
|
|
11
|
+
* prop with bare enums (the fewest tokens).
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* ```ts
|
|
15
|
+
* const value = renderGenerativeUISchema(manifest, spec.schema.markdown);
|
|
16
|
+
* ```
|
|
17
|
+
*/
|
|
18
|
+
declare function renderGenerativeUISchema(manifest: SchemaManifest, settings: GenerativeUISchemaSettings): string;
|
|
19
|
+
/**
|
|
20
|
+
* The component list of the tool guide, one entry per component, in the
|
|
21
|
+
* notation of `settings`. `json-schema` is the list that ships.
|
|
22
|
+
*/
|
|
23
|
+
declare function renderGenerativeUIComponentList(components: GenerativeUIManifest["components"], settings: GenerativeUISchemaSettings): string;
|
|
24
|
+
/**
|
|
25
|
+
* The manifest with replaced descriptions: `"Name"` replaces the
|
|
26
|
+
* description of a component, `"Name.prop"` the description of one of its
|
|
27
|
+
* props. Keys that name no component or prop are left out of the result
|
|
28
|
+
* and listed in `unknown`. The manifest is not changed.
|
|
29
|
+
*/
|
|
30
|
+
declare function applyDescriptionOverrides<M extends Pick<GenerativeUIManifest, "components">>(manifest: M, overrides: Record<string, string>): {
|
|
31
|
+
manifest: M;
|
|
32
|
+
unknown: string[];
|
|
33
|
+
};
|
|
34
|
+
/**
|
|
35
|
+
* The components and props of `manifest` whose names do not appear in
|
|
36
|
+
* `rendered`. A schema notation that leaves one out would hide part of the
|
|
37
|
+
* catalog from the model.
|
|
38
|
+
*/
|
|
39
|
+
declare function schemaCoverage(manifest: Pick<GenerativeUIManifest, "components">, rendered: string): {
|
|
40
|
+
missing: string[];
|
|
41
|
+
};
|
|
42
|
+
//#endregion
|
|
43
|
+
export { applyDescriptionOverrides, renderGenerativeUIComponentList, renderGenerativeUISchema, schemaCoverage };
|
|
44
|
+
//# sourceMappingURL=schema-render.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-render.d.mts","names":[],"sources":["../../src/generative-ui/schema-render.ts"],"mappings":";;;;KAcK,cAAA,GAAiB,IAAA,CACpB,oBAAA;;AAHe;;;;;AAuOjB;;;;;;iBAAgB,wBAAA,CACd,QAAA,EAAU,cAAA,EACV,QAAA,EAAU,0BAAA;;;;AAsCZ;iBAAgB,+BAAA,CACd,UAAA,EAAY,oBAAA,gBACZ,QAAA,EAAU,0BAAA;;;;;;;iBAkCI,yBAAA,WACJ,IAAA,CAAK,oBAAA,gBAAA,CAEf,QAAA,EAAU,CAAA,EACV,SAAA,EAAW,MAAA;EACR,QAAA,EAAU,CAAA;EAAG,OAAA;AAAA;;;;;;iBAqCF,cAAA,CACd,QAAA,EAAU,IAAA,CAAK,oBAAA,iBACf,QAAA;EACG,OAAA;AAAA"}
|
|
@@ -0,0 +1,239 @@
|
|
|
1
|
+
import { functionCallers } from "./basic-functions.mjs";
|
|
2
|
+
import { GENERATIVE_UI_AGENT_FUNCTION_CALLS } from "./types.mjs";
|
|
3
|
+
import { buildCompactGenerativeUISchema, compactProps, compactType } from "./compact-schema.mjs";
|
|
4
|
+
|
|
5
|
+
//#region src/generative-ui/schema-render.ts
|
|
6
|
+
/** An own key (shared compiles to ES2017, which has no Object.hasOwn). */
|
|
7
|
+
const hasOwn = (target, key) => Object.prototype.hasOwnProperty.call(target, key);
|
|
8
|
+
function isRecord(value) {
|
|
9
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
10
|
+
}
|
|
11
|
+
/** The props schema without the description of each prop. */
|
|
12
|
+
function withoutPropDescriptions(schema) {
|
|
13
|
+
if (!isRecord(schema.properties)) return schema;
|
|
14
|
+
return {
|
|
15
|
+
...schema,
|
|
16
|
+
properties: Object.fromEntries(Object.entries(schema.properties).map(([key, value]) => {
|
|
17
|
+
if (!isRecord(value)) return [key, value];
|
|
18
|
+
const { description: _description, ...rest } = value;
|
|
19
|
+
return [key, rest];
|
|
20
|
+
}))
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
/** The short signature of a function: its built-in signature, or its schema made short. */
|
|
24
|
+
function functionSignature(fn) {
|
|
25
|
+
const signature = fn.parameters.description;
|
|
26
|
+
return typeof signature === "string" && signature.startsWith("{") && Object.keys(fn.parameters).length === 2 ? signature : compactType(fn.parameters, 0);
|
|
27
|
+
}
|
|
28
|
+
/**
|
|
29
|
+
* A function name with its parameters: `pick{ id: string }`, `clearCart{}`
|
|
30
|
+
* for no arguments, `setVolume(number)` for a parameter that is not an object.
|
|
31
|
+
*/
|
|
32
|
+
function nameWithParameters(name, parameters) {
|
|
33
|
+
if (parameters.startsWith("{")) return `${name}${parameters}`;
|
|
34
|
+
if (parameters === "object") return `${name}{}`;
|
|
35
|
+
return `${name}(${parameters})`;
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* One component as a TypeScript-like signature line, with its events and
|
|
39
|
+
* slots on the lines below:
|
|
40
|
+
* `- Text(text?: string // The text): Shows text`.
|
|
41
|
+
*/
|
|
42
|
+
function signatureLines(name, component, settings) {
|
|
43
|
+
const lines = [`- ${name}(${Object.entries(compactProps(component.props, settings.propDescriptions)).map(([key, type]) => `${key}: ${type}`).join(", ")})${component.description ? `: ${oneLine(component.description)}` : ""}`];
|
|
44
|
+
const events = Object.entries(component.events ?? {}).map(([event, definition]) => `${event}(${definition.description ? oneLine(definition.description) : compactType(definition.parameters, 0)})`);
|
|
45
|
+
if (events.length > 0) lines.push(` events: ${events.join(", ")}`);
|
|
46
|
+
const slots = Object.entries(component.slots ?? {}).map(([slot, kind]) => `${slot} (${kind})`);
|
|
47
|
+
if (slots.length > 0) lines.push(` slots: ${slots.join(", ")}`);
|
|
48
|
+
return lines;
|
|
49
|
+
}
|
|
50
|
+
/** The whole schema as signature text: targets, functions, components, surfaces. */
|
|
51
|
+
function signatureSchema(manifest, settings) {
|
|
52
|
+
const targets = manifest.targets.map((t) => {
|
|
53
|
+
const limit = t.components ? `; only ${t.components.join(", ")}` : "";
|
|
54
|
+
return `- ${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : ""}`;
|
|
55
|
+
});
|
|
56
|
+
const functions = Object.entries(manifest.functions).map(([name, fn]) => {
|
|
57
|
+
const callers = functionCallers(fn);
|
|
58
|
+
const who = callers.agent && GENERATIVE_UI_AGENT_FUNCTION_CALLS ? callers.renderer ? " (UI or agent)" : " (agent only)" : "";
|
|
59
|
+
return `- ${nameWithParameters(name, functionSignature(fn))}${who}${fn.description ? `: ${oneLine(fn.description)}` : ""}`;
|
|
60
|
+
});
|
|
61
|
+
const components = Object.entries(manifest.components).flatMap(([name, component]) => signatureLines(name, component, settings));
|
|
62
|
+
const surfaces = Object.entries(manifest.surfaces).map(([id, s]) => `- ${id} (target ${s.target})${s.status ? ` status: ${s.status}` : ""}${s.dataModel ? ` user values: ${JSON.stringify(s.dataModel)}` : ""}${(s.rejected ?? []).map((r) => ` rejected: ${r}`).join("")}`);
|
|
63
|
+
return [
|
|
64
|
+
`Catalog: ${manifest.catalogId}`,
|
|
65
|
+
`Targets:\n${targets.join("\n") || "- none"}`,
|
|
66
|
+
`Functions:\n${functions.join("\n") || "- none"}`,
|
|
67
|
+
`Components:\n${components.join("\n") || "- none"}`,
|
|
68
|
+
`Surfaces:\n${surfaces.join("\n") || "- none"}`
|
|
69
|
+
].join("\n\n");
|
|
70
|
+
}
|
|
71
|
+
/** A description on one line, so its lines cannot read as new entries. */
|
|
72
|
+
function oneLine(text) {
|
|
73
|
+
return text.replace(/\s*\n\s*/g, " ");
|
|
74
|
+
}
|
|
75
|
+
/** Values that would read as a type or a number once their quotes are gone. */
|
|
76
|
+
const AMBIGUOUS_VALUE = /^(?:string|number|integer|boolean|true|false|null|object|array|unknown|any|-?\d[\d.]*)$/;
|
|
77
|
+
/**
|
|
78
|
+
* A compact type with bare enum values and tight unions:
|
|
79
|
+
* `"sm" | "md" | "lg"` becomes `sm|md|lg`. A union keeps its quotes when it
|
|
80
|
+
* also holds a type (`"sm" | number`) or a value that reads as a type or a
|
|
81
|
+
* number (`"number" | "currency"`, `"1" | "2"`), and so do values that are
|
|
82
|
+
* not plain words.
|
|
83
|
+
*/
|
|
84
|
+
function bareType(type) {
|
|
85
|
+
return type.replace(/"[A-Za-z0-9_#%.-]+"(?: \| "[A-Za-z0-9_#%.-]+")*(?! \|)/g, (union, offset, whole) => {
|
|
86
|
+
if (whole.slice(offset - 2, offset) === "| ") return union;
|
|
87
|
+
const values = union.split(" | ").map((v) => v.slice(1, -1));
|
|
88
|
+
if (values.some((v) => AMBIGUOUS_VALUE.test(v))) return union;
|
|
89
|
+
return values.join("|");
|
|
90
|
+
}).replace(/ \| /g, "|");
|
|
91
|
+
}
|
|
92
|
+
/** A compact prop (`type // description`) as `name: type - description`. */
|
|
93
|
+
function propLine(key, value) {
|
|
94
|
+
const name = key.endsWith("?") ? key.slice(0, -1) : `${key}*`;
|
|
95
|
+
const cut = value.indexOf(" // ");
|
|
96
|
+
const type = bareType(cut < 0 ? value : value.slice(0, cut));
|
|
97
|
+
const text = cut < 0 ? "" : value.slice(cut + 4).trim();
|
|
98
|
+
return ` ${name}: ${type}${text ? ` - ${oneLine(text)}` : ""}`;
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* The whole schema as plain lines: a header line per component and one
|
|
102
|
+
* indented line per prop: events are `action` props and slots `componentId`
|
|
103
|
+
* or `childList` props. Props are optional unless marked `*`, enum values
|
|
104
|
+
* are bare, and the common props are listed once. It carries the same names, types and descriptions as
|
|
105
|
+
* `compact`, in fewer tokens.
|
|
106
|
+
*/
|
|
107
|
+
function linesSchema(manifest, settings) {
|
|
108
|
+
const compact = buildCompactGenerativeUISchema(manifest, {
|
|
109
|
+
propDescriptions: settings.propDescriptions,
|
|
110
|
+
commonProps: settings.commonProps
|
|
111
|
+
});
|
|
112
|
+
const targets = compact.targets.map((t) => {
|
|
113
|
+
const limit = t.components ? `, only ${t.components.join(", ")}` : "";
|
|
114
|
+
return `${t.id} (${t.kind}${limit})${t.description ? `: ${oneLine(t.description)}` : ""}`;
|
|
115
|
+
}).join("; ");
|
|
116
|
+
const functions = Object.entries(compact.functions).map(([name, fn]) => `${nameWithParameters(name, fn.parameters)}${"callers" in fn ? ` (${fn.callers})` : ""}${fn.description ? `: ${oneLine(fn.description)}` : ""}`);
|
|
117
|
+
const common = compact.commonProps ? Object.entries(compact.commonProps).map(([k, v]) => propLine(k, v)) : [];
|
|
118
|
+
const components = Object.entries(compact.components).map(([name, c]) => ({
|
|
119
|
+
name,
|
|
120
|
+
c,
|
|
121
|
+
own: [...Object.entries(c.props).map(([k, v]) => propLine(k, v)), ...Object.entries(c.events ?? {}).map(([e, d]) => ` ${e}: action - ${oneLine(d)}`)]
|
|
122
|
+
})).flatMap(({ name, c, own }) => {
|
|
123
|
+
const lines = [`${name}${c.includesCommonProps ? " +common" : ""}${c.description ? `: ${oneLine(c.description)}` : ""}`, ...own];
|
|
124
|
+
for (const [slot, kind] of Object.entries(c.slots ?? {})) lines.push(` ${slot}: ${kind === "single" ? "componentId" : "childList"}`);
|
|
125
|
+
return lines;
|
|
126
|
+
});
|
|
127
|
+
const surfaces = Object.keys(compact.surfaces).length ? JSON.stringify(compact.surfaces) : "none";
|
|
128
|
+
return [
|
|
129
|
+
`Catalog ${compact.catalogId}. Targets: ${targets || "none"}`,
|
|
130
|
+
"Props are optional unless marked *. a|b|c is one of those strings, quoted when one reads as a type or a number. A component marked +common also takes every common prop. A prop of type action is an event prop: give it {\"event\":{...}} or {\"functionCall\":{...}}. A componentId prop takes one component id; a childList prop takes a list of ids or a template.",
|
|
131
|
+
`Functions:\n${functions.join("\n")}`,
|
|
132
|
+
...common.length ? [`Common props:\n${common.join("\n")}`] : [],
|
|
133
|
+
`Components:\n${components.join("\n")}`,
|
|
134
|
+
`Surfaces: ${surfaces}`
|
|
135
|
+
].join("\n\n");
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* The component schema of the markdown transport, as the text of its
|
|
139
|
+
* context entry, in the notation of `settings`. `compact` is the schema that
|
|
140
|
+
* ships (`buildCompactGenerativeUISchema`), `json-schema` the manifest parts
|
|
141
|
+
* as JSON, `ts-signature` one line per component, `lines` one line per
|
|
142
|
+
* prop with bare enums (the fewest tokens).
|
|
143
|
+
*
|
|
144
|
+
* @example
|
|
145
|
+
* ```ts
|
|
146
|
+
* const value = renderGenerativeUISchema(manifest, spec.schema.markdown);
|
|
147
|
+
* ```
|
|
148
|
+
*/
|
|
149
|
+
function renderGenerativeUISchema(manifest, settings) {
|
|
150
|
+
switch (settings.notation) {
|
|
151
|
+
case "compact": return JSON.stringify(buildCompactGenerativeUISchema(manifest, {
|
|
152
|
+
propDescriptions: settings.propDescriptions,
|
|
153
|
+
commonProps: settings.commonProps
|
|
154
|
+
}));
|
|
155
|
+
case "json-schema": {
|
|
156
|
+
const components = Object.fromEntries(Object.entries(manifest.components).map(([name, c]) => [name, settings.propDescriptions ? c : {
|
|
157
|
+
...c,
|
|
158
|
+
props: withoutPropDescriptions(c.props)
|
|
159
|
+
}]));
|
|
160
|
+
return JSON.stringify({
|
|
161
|
+
catalogId: manifest.catalogId,
|
|
162
|
+
targets: manifest.targets,
|
|
163
|
+
functions: manifest.functions,
|
|
164
|
+
components,
|
|
165
|
+
surfaces: manifest.surfaces
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
case "ts-signature": return signatureSchema(manifest, settings);
|
|
169
|
+
case "lines": return linesSchema(manifest, settings);
|
|
170
|
+
}
|
|
171
|
+
}
|
|
172
|
+
/**
|
|
173
|
+
* The component list of the tool guide, one entry per component, in the
|
|
174
|
+
* notation of `settings`. `json-schema` is the list that ships.
|
|
175
|
+
*/
|
|
176
|
+
function renderGenerativeUIComponentList(components, settings) {
|
|
177
|
+
return Object.entries(components).map(([name, c]) => {
|
|
178
|
+
if (settings.notation === "ts-signature" || settings.notation === "lines") return signatureLines(name, c, settings).join("\n");
|
|
179
|
+
const props = settings.notation === "compact" ? compactProps(c.props, settings.propDescriptions) : settings.propDescriptions ? c.props : withoutPropDescriptions(c.props);
|
|
180
|
+
const lines = [`- **${name}**${c.description ? `: ${oneLine(c.description)}` : ""}`, ` props: ${JSON.stringify(props)}`];
|
|
181
|
+
if (c.events) lines.push(` event props: ${JSON.stringify(c.events)}`);
|
|
182
|
+
if (c.slots) lines.push(` slot props (component ids): ${JSON.stringify(c.slots)}`);
|
|
183
|
+
return lines.join("\n");
|
|
184
|
+
}).join("\n");
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* The manifest with replaced descriptions: `"Name"` replaces the
|
|
188
|
+
* description of a component, `"Name.prop"` the description of one of its
|
|
189
|
+
* props. Keys that name no component or prop are left out of the result
|
|
190
|
+
* and listed in `unknown`. The manifest is not changed.
|
|
191
|
+
*/
|
|
192
|
+
function applyDescriptionOverrides(manifest, overrides) {
|
|
193
|
+
const components = structuredClone(manifest.components);
|
|
194
|
+
const unknown = [];
|
|
195
|
+
for (const [key, description] of Object.entries(overrides)) {
|
|
196
|
+
const [name, prop, ...rest] = key.split(".");
|
|
197
|
+
const component = name !== void 0 && hasOwn(components, name) ? components[name] : void 0;
|
|
198
|
+
if (!component || rest.length > 0) {
|
|
199
|
+
unknown.push(key);
|
|
200
|
+
continue;
|
|
201
|
+
}
|
|
202
|
+
if (prop === void 0) {
|
|
203
|
+
component.description = description;
|
|
204
|
+
continue;
|
|
205
|
+
}
|
|
206
|
+
const properties = component.props.properties;
|
|
207
|
+
const property = isRecord(properties) && hasOwn(properties, prop) ? properties[prop] : void 0;
|
|
208
|
+
if (!isRecord(property)) {
|
|
209
|
+
unknown.push(key);
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
property.description = description;
|
|
213
|
+
}
|
|
214
|
+
return {
|
|
215
|
+
manifest: {
|
|
216
|
+
...manifest,
|
|
217
|
+
components
|
|
218
|
+
},
|
|
219
|
+
unknown
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
/**
|
|
223
|
+
* The components and props of `manifest` whose names do not appear in
|
|
224
|
+
* `rendered`. A schema notation that leaves one out would hide part of the
|
|
225
|
+
* catalog from the model.
|
|
226
|
+
*/
|
|
227
|
+
function schemaCoverage(manifest, rendered) {
|
|
228
|
+
const missing = [];
|
|
229
|
+
for (const [name, component] of Object.entries(manifest.components)) {
|
|
230
|
+
if (!rendered.includes(name)) missing.push(name);
|
|
231
|
+
const properties = isRecord(component.props.properties) ? component.props.properties : {};
|
|
232
|
+
for (const prop of Object.keys(properties)) if (!rendered.includes(prop)) missing.push(`${name}.${prop}`);
|
|
233
|
+
}
|
|
234
|
+
return { missing };
|
|
235
|
+
}
|
|
236
|
+
|
|
237
|
+
//#endregion
|
|
238
|
+
export { applyDescriptionOverrides, renderGenerativeUIComponentList, renderGenerativeUISchema, schemaCoverage };
|
|
239
|
+
//# sourceMappingURL=schema-render.mjs.map
|