@copilotkit/shared 1.83.0 → 1.84.1
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/a2ui-catalog-schema.cjs +264 -0
- package/dist/a2ui-catalog-schema.cjs.map +1 -0
- package/dist/a2ui-catalog-schema.d.cts +112 -0
- package/dist/a2ui-catalog-schema.d.cts.map +1 -0
- package/dist/a2ui-catalog-schema.d.mts +112 -0
- package/dist/a2ui-catalog-schema.d.mts.map +1 -0
- package/dist/a2ui-catalog-schema.mjs +259 -0
- package/dist/a2ui-catalog-schema.mjs.map +1 -0
- 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/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/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/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/index.cjs +20 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +5 -1
- package/dist/index.d.cts.map +1 -1
- package/dist/index.d.mts +5 -1
- package/dist/index.d.mts.map +1 -1
- package/dist/index.mjs +5 -1
- package/dist/index.mjs.map +1 -1
- package/dist/index.umd.js +1023 -169
- package/dist/index.umd.js.map +1 -1
- package/dist/package.cjs +1 -1
- package/dist/package.mjs +1 -1
- package/package.json +1 -1
- package/src/__tests__/a2ui-catalog-schema.test.ts +403 -0
- 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__/schema-render.test.ts +245 -0
- package/src/a2ui-catalog-schema.ts +404 -0
- package/src/a2ui-envelope.ts +5 -5
- package/src/a2ui-prompts.ts +47 -96
- 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/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/schema-render.ts +374 -0
- package/src/generative-ui/types.ts +10 -7
- package/src/index.ts +13 -0
- package/src/utils/__tests__/conditions.test.ts +153 -43
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"basic-functions.cjs","names":[],"sources":["../../src/generative-ui/basic-functions.ts"],"sourcesContent":["/**\n * The functions of the A2UI 1.0 basic catalog, as the model and the\n * validator see them. The renderer implements them (react-core\n * `generative-ui/functions.ts`); this file holds only what both sides share:\n * the names, the short signatures for the schema the model reads, and who\n * may call each one.\n */\n\n/** What a function gives back, as A2UI 1.0 names it. */\nexport type A2UIFunctionReturnType =\n | \"string\"\n | \"number\"\n | \"boolean\"\n | \"array\"\n | \"object\"\n | \"validationResult\"\n | \"any\"\n | \"void\";\n\n/**\n * Who may call a function. The renderer calls it from an event binding or\n * as a prop value; the agent calls it with `callRendererFunction`.\n */\nexport type A2UIAllowedCallers =\n | \"rendererOnly\"\n | \"agentOnly\"\n | \"rendererOrAgent\";\n\n/**\n * The callers of an app function that names none: the renderer only, as\n * in A2UI 1.0. The agent calls a function with `callRendererFunction` only\n * when its declaration allows it (`\"rendererOrAgent\"` or `\"agentOnly\"`).\n * The manifests and the exported catalog write the value out.\n */\nexport const GENERATIVE_UI_DEFAULT_ALLOWED_CALLERS: A2UIAllowedCallers =\n \"rendererOnly\";\n\n/** The A2UI 1.0 interface fields of a function: who calls it, and how. */\nexport interface A2UIFunctionPermissions {\n allowedCallers?: A2UIAllowedCallers;\n /**\n * True for a function that only runs from a user gesture (a click, a key\n * press). It is `rendererOnly`: the agent cannot call it, and a prop\n * cannot be bound to it.\n */\n requiresUserActivation?: boolean;\n}\n\n/**\n * Who may call a function, from its permissions. A function that needs a\n * user gesture is for the renderer only, whatever `allowedCallers` says.\n *\n * @example\n * ```ts\n * functionCallers({}); // { renderer: true, agent: true }\n * functionCallers({ allowedCallers: \"rendererOnly\" }); // { renderer: true, agent: false }\n * functionCallers({ requiresUserActivation: true }); // { renderer: true, agent: false }\n * ```\n */\nexport function functionCallers(fn: A2UIFunctionPermissions) {\n if (fn.requiresUserActivation) return { renderer: true, agent: false };\n const callers = fn.allowedCallers ?? GENERATIVE_UI_DEFAULT_ALLOWED_CALLERS;\n return {\n renderer: callers !== \"agentOnly\",\n agent: callers !== \"rendererOnly\",\n };\n}\n\nexport interface A2UIBasicFunctionInfo extends A2UIFunctionPermissions {\n description: string;\n /** The args, as a short TypeScript-like type for the compact schema. */\n parameters: string;\n returnType: A2UIFunctionReturnType;\n /** Built-in functions are value functions of the renderer. */\n allowedCallers: \"rendererOnly\";\n requiresUserActivation?: true;\n /** True for a CopilotKit function that the A2UI basic catalog lacks. */\n extension?: true;\n}\n\n/** Marks a table of built-in functions as callable by the renderer only. */\nfunction rendererOnly<Name extends string>(\n functions: Record<Name, Omit<A2UIBasicFunctionInfo, \"allowedCallers\">>,\n): Readonly<Record<Name, A2UIBasicFunctionInfo>> {\n return Object.fromEntries(\n Object.entries<Omit<A2UIBasicFunctionInfo, \"allowedCallers\">>(\n functions,\n ).map(([name, info]) => [\n name,\n { ...info, allowedCallers: \"rendererOnly\" },\n ]),\n ) as Record<Name, A2UIBasicFunctionInfo>;\n}\n\n/**\n * The basic functions by name. A `{\"@call\"}` in a prop, in an event\n * binding, or in a `callRendererFunction` may name any of them without a\n * declaration in `createGenerativeUI({ functions })`.\n */\nexport const A2UI_BASIC_FUNCTIONS: Readonly<\n Record<string, A2UIBasicFunctionInfo>\n> = rendererOnly({\n required: {\n description: \"Valid when the value is not empty\",\n parameters: \"{ value: unknown }\",\n returnType: \"validationResult\",\n },\n regex: {\n description: \"Valid when the value matches the pattern\",\n parameters: \"{ value: string; pattern: string }\",\n returnType: \"validationResult\",\n },\n length: {\n description: \"Valid when the length of the value is within min and max\",\n parameters: \"{ value: string; min?: number; max?: number }\",\n returnType: \"validationResult\",\n },\n numeric: {\n description: \"Valid when the value is a number within min and max\",\n parameters: \"{ value: number; min?: number; max?: number }\",\n returnType: \"validationResult\",\n },\n email: {\n description: \"Valid when the value is an email address\",\n parameters: \"{ value: string }\",\n returnType: \"validationResult\",\n },\n formatString: {\n description:\n \"Interpolates ${/path}, ${relative/path} and ${fn(arg: value)} in the value; \\\\${ is a literal\",\n parameters: \"{ value: string }\",\n returnType: \"string\",\n },\n formatNumber: {\n description: \"The number in the locale of the user\",\n parameters: \"{ value: number; decimals?: number; grouping?: boolean }\",\n returnType: \"string\",\n },\n formatCurrency: {\n description: \"The amount as money, with an ISO 4217 currency code\",\n parameters:\n \"{ value: number; currency: string; decimals?: number; grouping?: boolean }\",\n returnType: \"string\",\n },\n formatDate: {\n description:\n 'A date or ISO string in a TR35 pattern, for example \"MMM d, yyyy\" or \"HH:mm\"',\n parameters: \"{ value: string | number; format: string }\",\n returnType: \"string\",\n },\n pluralize: {\n description: \"The string of the plural category of the value\",\n parameters:\n \"{ value: number; zero?: string; one?: string; two?: string; few?: string; many?: string; other: string }\",\n returnType: \"string\",\n },\n openUrl: {\n description: \"Opens an http(s) URL in a new tab. Only from an event\",\n parameters: \"{ url: string }\",\n returnType: \"void\",\n requiresUserActivation: true,\n },\n and: {\n description: \"True when every value is true\",\n parameters: \"{ values: boolean[] }\",\n returnType: \"boolean\",\n },\n or: {\n description: \"True when any value is true\",\n parameters: \"{ values: boolean[] }\",\n returnType: \"boolean\",\n },\n not: {\n description: \"The negated value\",\n parameters: \"{ value: boolean }\",\n returnType: \"boolean\",\n },\n});\n\n/**\n * The value functions that CopilotKit adds to the basic catalog, because\n * A2UI 1.0 has no arithmetic: `calc` evaluates an expression over named\n * args, and `series` builds a computed list. A catalog may declare its own\n * functions in A2UI, so these are regular catalog functions to the model;\n * a renderer without them rejects the call instead of misrendering.\n */\nexport const COPILOTKIT_FUNCTIONS: Readonly<\n Record<string, A2UIBasicFunctionInfo>\n> = rendererOnly({\n calc: {\n description:\n \"The value of an arithmetic expression over the other args, by name: + - * / % ( ), comparisons, && || !, cond ? a : b, min max round floor ceil abs, 'text' + name\",\n parameters:\n \"{ expression: string; [name: string]: number | string | boolean }\",\n returnType: \"any\",\n extension: true,\n },\n // WORKAROUND(a2ui-agent-sdk 0.9.0): examples omit `variable` and use default i\n // because the Express compiler mistakes a literal `variable` key for its AST.\n // Runtime support for custom variable names is unchanged.\n // https://github.com/a2ui-project/a2ui/issues/3410\n series: {\n description:\n 'A list of count copies of item, for chart data and template lists. Put arithmetic in ${expression} strings in item, using the loop variable (default i, counting from start, default 1) and other named args. A whole-expression string retains the result type, so \"${i * monthlyCost}\" yields a number. Arguments, including nested calc or formatString calls in item, resolve before the series loop; those calls cannot use the loop variable. Example: {\"@call\":\"series\",\"args\":{\"count\":6,\"item\":{\"month\":\"${i}\",\"cost\":\"${i * monthlyCost}\"},\"monthlyCost\":{\"@path\":\"/monthlyCost\"}}}. Omit variable to use i.',\n parameters:\n \"{ count: number; item: unknown; variable?: string; start?: number; [name: string]: unknown }\",\n returnType: \"array\",\n extension: true,\n },\n});\n\n/** Every function a `{\"@call\"}` may name without a declaration: basic and CopilotKit. */\nexport const GENERATIVE_UI_FUNCTIONS: Readonly<\n Record<string, A2UIBasicFunctionInfo>\n> = { ...A2UI_BASIC_FUNCTIONS, ...COPILOTKIT_FUNCTIONS };\n\n/** The name of the system function that gives the index of a template item. */\nexport const A2UI_INDEX_FUNCTION = \"@index\";\n\n/** True for a built-in function name: basic, CopilotKit, or `@index`. */\nexport function isA2UIBasicFunction(name: string) {\n return (\n name === A2UI_INDEX_FUNCTION ||\n Object.prototype.hasOwnProperty.call(GENERATIVE_UI_FUNCTIONS, name)\n );\n}\n\n/**\n * True for a built-in function that a prop or a check may call: every one\n * except the functions that need a user gesture.\n */\nexport function isA2UIValueFunction(name: string) {\n if (name === A2UI_INDEX_FUNCTION) return true;\n const info = Object.prototype.hasOwnProperty.call(\n GENERATIVE_UI_FUNCTIONS,\n name,\n )\n ? GENERATIVE_UI_FUNCTIONS[name]\n : undefined;\n return info !== undefined && !info.requiresUserActivation;\n}\n\n/** `{ \"@call\": \"<name>\", \"args\"?: {...} }`: a function call value. */\nexport function isCallBinding(\n value: unknown,\n): value is { \"@call\": string; args?: Record<string, unknown> } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n !Array.isArray(value) &&\n typeof (value as Record<string, unknown>)[\"@call\"] === \"string\"\n );\n}\n\n/** How bad a failed check is. `error` is the default; only an error blocks. */\nexport type A2UIValidationSeverity = \"error\" | \"warning\" | \"info\";\n\n/**\n * A2UI's `ValidationResult`: what a check gives. A validator function gives\n * one; a check may also bind a boolean, which the renderer coerces to one.\n * `code` names the failure for programs; `severity` says how bad it is,\n * `\"error\"` when left out. Other keys are allowed, for app validators that\n * carry more (a suggested value, a field path).\n */\nexport interface A2UIValidationResult {\n valid: boolean;\n message?: string;\n code?: string;\n severity?: A2UIValidationSeverity;\n [extra: string]: unknown;\n}\n\nexport function isValidationResult(\n value: unknown,\n): value is A2UIValidationResult {\n return (\n typeof value === \"object\" &&\n value !== null &&\n !Array.isArray(value) &&\n typeof (value as Record<string, unknown>).valid === \"boolean\"\n );\n}\n"],"mappings":";;;;;;;;AAkCA,MAAa,wCACX;;;;;;;;;;;;AAwBF,SAAgB,gBAAgB,IAA6B;AAC3D,KAAI,GAAG,uBAAwB,QAAO;EAAE,UAAU;EAAM,OAAO;EAAO;CACtE,MAAM,UAAU,GAAG,kBAAkB;AACrC,QAAO;EACL,UAAU,YAAY;EACtB,OAAO,YAAY;EACpB;;;AAgBH,SAAS,aACP,WAC+C;AAC/C,QAAO,OAAO,YACZ,OAAO,QACL,UACD,CAAC,KAAK,CAAC,MAAM,UAAU,CACtB,MACA;EAAE,GAAG;EAAM,gBAAgB;EAAgB,CAC5C,CAAC,CACH;;;;;;;AAQH,MAAa,uBAET,aAAa;CACf,UAAU;EACR,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,OAAO;EACL,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,QAAQ;EACN,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,SAAS;EACP,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,OAAO;EACL,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,cAAc;EACZ,aACE;EACF,YAAY;EACZ,YAAY;EACb;CACD,cAAc;EACZ,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,gBAAgB;EACd,aAAa;EACb,YACE;EACF,YAAY;EACb;CACD,YAAY;EACV,aACE;EACF,YAAY;EACZ,YAAY;EACb;CACD,WAAW;EACT,aAAa;EACb,YACE;EACF,YAAY;EACb;CACD,SAAS;EACP,aAAa;EACb,YAAY;EACZ,YAAY;EACZ,wBAAwB;EACzB;CACD,KAAK;EACH,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,IAAI;EACF,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,KAAK;EACH,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACF,CAAC;;;;;;;;AASF,MAAa,uBAET,aAAa;CACf,MAAM;EACJ,aACE;EACF,YACE;EACF,YAAY;EACZ,WAAW;EACZ;CAKD,QAAQ;EACN,aACE;EACF,YACE;EACF,YAAY;EACZ,WAAW;EACZ;CACF,CAAC;;AAGF,MAAa,0BAET;CAAE,GAAG;CAAsB,GAAG;CAAsB;;AAGxD,MAAa,sBAAsB;;AAGnC,SAAgB,oBAAoB,MAAc;AAChD,QACE,SAAS,uBACT,OAAO,UAAU,eAAe,KAAK,yBAAyB,KAAK;;;;;;AAQvE,SAAgB,oBAAoB,MAAc;AAChD,KAAI,SAAS,oBAAqB,QAAO;CACzC,MAAM,OAAO,OAAO,UAAU,eAAe,KAC3C,yBACA,KACD,GACG,wBAAwB,QACxB;AACJ,QAAO,SAAS,UAAa,CAAC,KAAK;;;AAIrC,SAAgB,cACd,OAC8D;AAC9D,QACE,OAAO,UAAU,YACjB,UAAU,QACV,CAAC,MAAM,QAAQ,MAAM,IACrB,OAAQ,MAAkC,aAAa;;AAsB3D,SAAgB,mBACd,OAC+B;AAC/B,QACE,OAAO,UAAU,YACjB,UAAU,QACV,CAAC,MAAM,QAAQ,MAAM,IACrB,OAAQ,MAAkC,UAAU"}
|
|
1
|
+
{"version":3,"file":"basic-functions.cjs","names":[],"sources":["../../src/generative-ui/basic-functions.ts"],"sourcesContent":["/**\n * The functions of the A2UI 1.0 basic catalog, as the model and the\n * validator see them. The renderer implements them (react-core\n * `generative-ui/functions.ts`); this file holds only what both sides share:\n * the names, the short signatures for the schema the model reads, and who\n * may call each one.\n */\n\n/** What a function gives back, as A2UI 1.0 names it. */\nexport type A2UIFunctionReturnType =\n | \"string\"\n | \"number\"\n | \"boolean\"\n | \"array\"\n | \"object\"\n | \"validationResult\"\n | \"any\"\n | \"void\";\n\n/**\n * Who may call a function. The renderer calls it from an event binding or\n * as a prop value; the agent calls it with `callRendererFunction`.\n */\nexport type A2UIAllowedCallers =\n | \"rendererOnly\"\n | \"agentOnly\"\n | \"rendererOrAgent\";\n\n/**\n * The callers of an app function that names none: the renderer only, as\n * in A2UI 1.0. The agent calls a function with `callRendererFunction` only\n * when its declaration allows it (`\"rendererOrAgent\"` or `\"agentOnly\"`).\n * The manifests and the exported catalog write the value out.\n */\nexport const GENERATIVE_UI_DEFAULT_ALLOWED_CALLERS: A2UIAllowedCallers =\n \"rendererOnly\";\n\n/** The A2UI 1.0 interface fields of a function: who calls it, and how. */\nexport interface A2UIFunctionPermissions {\n allowedCallers?: A2UIAllowedCallers;\n /**\n * True for a function that only runs from a user gesture (a click, a key\n * press). It is `rendererOnly`: the agent cannot call it, and a prop\n * cannot be bound to it.\n */\n requiresUserActivation?: boolean;\n}\n\n/**\n * Who may call a function, from its permissions. A function that needs a\n * user gesture is for the renderer only, whatever `allowedCallers` says.\n *\n * @example\n * ```ts\n * functionCallers({}); // { renderer: true, agent: true }\n * functionCallers({ allowedCallers: \"rendererOnly\" }); // { renderer: true, agent: false }\n * functionCallers({ requiresUserActivation: true }); // { renderer: true, agent: false }\n * ```\n */\nexport function functionCallers(fn: A2UIFunctionPermissions) {\n if (fn.requiresUserActivation) return { renderer: true, agent: false };\n const callers = fn.allowedCallers ?? GENERATIVE_UI_DEFAULT_ALLOWED_CALLERS;\n return {\n renderer: callers !== \"agentOnly\",\n agent: callers !== \"rendererOnly\",\n };\n}\n\nexport interface A2UIBasicFunctionInfo extends A2UIFunctionPermissions {\n description: string;\n /** The args, as a short TypeScript-like type for the compact schema. */\n parameters: string;\n returnType: A2UIFunctionReturnType;\n /** Built-in functions are value functions of the renderer. */\n allowedCallers: \"rendererOnly\";\n requiresUserActivation?: true;\n /** True for a CopilotKit function that the A2UI basic catalog lacks. */\n extension?: true;\n}\n\n/** Marks a table of built-in functions as callable by the renderer only. */\nfunction rendererOnly<Name extends string>(\n functions: Record<Name, Omit<A2UIBasicFunctionInfo, \"allowedCallers\">>,\n): Readonly<Record<Name, A2UIBasicFunctionInfo>> {\n return Object.fromEntries(\n Object.entries<Omit<A2UIBasicFunctionInfo, \"allowedCallers\">>(\n functions,\n ).map(([name, info]) => [\n name,\n { ...info, allowedCallers: \"rendererOnly\" },\n ]),\n ) as Record<Name, A2UIBasicFunctionInfo>;\n}\n\n/**\n * The basic functions by name. A `{\"@call\"}` in a prop, in an event\n * binding, or in a `callRendererFunction` may name any of them without a\n * declaration in `createGenerativeUI({ functions })`.\n */\nexport const A2UI_BASIC_FUNCTIONS: Readonly<\n Record<string, A2UIBasicFunctionInfo>\n> = rendererOnly({\n required: {\n description: \"Valid when the value is not empty\",\n parameters: \"{ value: unknown }\",\n returnType: \"validationResult\",\n },\n regex: {\n description: \"Valid when the value matches the pattern\",\n parameters: \"{ value: string; pattern: string }\",\n returnType: \"validationResult\",\n },\n length: {\n description: \"Valid when the length of the value is within min and max\",\n parameters: \"{ value: string; min?: number; max?: number }\",\n returnType: \"validationResult\",\n },\n numeric: {\n description: \"Valid when the value is a number within min and max\",\n parameters: \"{ value: number; min?: number; max?: number }\",\n returnType: \"validationResult\",\n },\n email: {\n description: \"Valid when the value is an email address\",\n parameters: \"{ value: string }\",\n returnType: \"validationResult\",\n },\n formatString: {\n description:\n \"Interpolates ${/path}, ${relative/path} and ${fn(arg: value)} in the value; \\\\${ is a literal\",\n parameters: \"{ value: string }\",\n returnType: \"string\",\n },\n formatNumber: {\n description: \"The number in the locale of the user\",\n parameters: \"{ value: number; decimals?: number; grouping?: boolean }\",\n returnType: \"string\",\n },\n formatCurrency: {\n description: \"The amount as money, with an ISO 4217 currency code\",\n parameters:\n \"{ value: number; currency: string; decimals?: number; grouping?: boolean }\",\n returnType: \"string\",\n },\n formatDate: {\n description:\n 'A date or ISO string in a TR35 pattern, for example \"MMM d, yyyy\" or \"HH:mm\"',\n parameters: \"{ value: string | number; format: string }\",\n returnType: \"string\",\n },\n pluralize: {\n description: \"The string of the plural category of the value\",\n parameters:\n \"{ value: number; zero?: string; one?: string; two?: string; few?: string; many?: string; other: string }\",\n returnType: \"string\",\n },\n openUrl: {\n description: \"Opens an http(s) URL in a new tab. Only from an event\",\n parameters: \"{ url: string }\",\n returnType: \"void\",\n requiresUserActivation: true,\n },\n and: {\n description: \"True when every value is true\",\n parameters: \"{ values: boolean[] }\",\n returnType: \"boolean\",\n },\n or: {\n description: \"True when any value is true\",\n parameters: \"{ values: boolean[] }\",\n returnType: \"boolean\",\n },\n not: {\n description: \"The negated value\",\n parameters: \"{ value: boolean }\",\n returnType: \"boolean\",\n },\n});\n\n/**\n * The value functions that CopilotKit adds to the basic catalog, because\n * A2UI 1.0 has no arithmetic: `calc` evaluates an expression over named\n * args, and `series` builds a computed list. A catalog may declare its own\n * functions in A2UI, so these are regular catalog functions to the model;\n * a renderer without them rejects the call instead of misrendering.\n */\nexport const COPILOTKIT_FUNCTIONS: Readonly<\n Record<string, A2UIBasicFunctionInfo>\n> = rendererOnly({\n calc: {\n description:\n \"The value of an arithmetic expression over the other args, by name: + - * / % ( ), x ^ y (power), comparisons, && || !, cond ? a : b, min max round floor ceil abs pow sqrt exp ln log10, the constants pi and e, 'text' + name\",\n parameters:\n \"{ expression: string; [name: string]: number | string | boolean }\",\n returnType: \"any\",\n extension: true,\n },\n // WORKAROUND(a2ui-agent-sdk 0.9.0): examples omit `variable` and use default i\n // because the Express compiler mistakes a literal `variable` key for its AST.\n // Runtime support for custom variable names is unchanged.\n // https://github.com/a2ui-project/a2ui/issues/3410\n series: {\n description:\n 'A list of count copies of item, for chart data and template lists. Put arithmetic in ${expression} strings in item, using the loop variable (default i, counting from start, default 1) and other named args. A whole-expression string retains the result type, so \"${i * monthlyCost}\" yields a number. Arguments, including nested calc or formatString calls in item, resolve before the series loop; those calls cannot use the loop variable. Example: {\"@call\":\"series\",\"args\":{\"count\":6,\"item\":{\"month\":\"${i}\",\"cost\":\"${i * monthlyCost}\"},\"monthlyCost\":{\"@path\":\"/monthlyCost\"}}}. Omit variable to use i.',\n parameters:\n \"{ count: number; item: unknown; variable?: string; start?: number; [name: string]: unknown }\",\n returnType: \"array\",\n extension: true,\n },\n});\n\n/** Every function a `{\"@call\"}` may name without a declaration: basic and CopilotKit. */\nexport const GENERATIVE_UI_FUNCTIONS: Readonly<\n Record<string, A2UIBasicFunctionInfo>\n> = { ...A2UI_BASIC_FUNCTIONS, ...COPILOTKIT_FUNCTIONS };\n\n/** The name of the system function that gives the index of a template item. */\nexport const A2UI_INDEX_FUNCTION = \"@index\";\n\n/** True for a built-in function name: basic, CopilotKit, or `@index`. */\nexport function isA2UIBasicFunction(name: string) {\n return (\n name === A2UI_INDEX_FUNCTION ||\n Object.prototype.hasOwnProperty.call(GENERATIVE_UI_FUNCTIONS, name)\n );\n}\n\n/**\n * True for a built-in function that a prop or a check may call: every one\n * except the functions that need a user gesture.\n */\nexport function isA2UIValueFunction(name: string) {\n if (name === A2UI_INDEX_FUNCTION) return true;\n const info = Object.prototype.hasOwnProperty.call(\n GENERATIVE_UI_FUNCTIONS,\n name,\n )\n ? GENERATIVE_UI_FUNCTIONS[name]\n : undefined;\n return info !== undefined && !info.requiresUserActivation;\n}\n\n/** `{ \"@call\": \"<name>\", \"args\"?: {...} }`: a function call value. */\nexport function isCallBinding(\n value: unknown,\n): value is { \"@call\": string; args?: Record<string, unknown> } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n !Array.isArray(value) &&\n typeof (value as Record<string, unknown>)[\"@call\"] === \"string\"\n );\n}\n\n/** How bad a failed check is. `error` is the default; only an error blocks. */\nexport type A2UIValidationSeverity = \"error\" | \"warning\" | \"info\";\n\n/**\n * A2UI's `ValidationResult`: what a check gives. A validator function gives\n * one; a check may also bind a boolean, which the renderer coerces to one.\n * `code` names the failure for programs; `severity` says how bad it is,\n * `\"error\"` when left out. Other keys are allowed, for app validators that\n * carry more (a suggested value, a field path).\n */\nexport interface A2UIValidationResult {\n valid: boolean;\n message?: string;\n code?: string;\n severity?: A2UIValidationSeverity;\n [extra: string]: unknown;\n}\n\nexport function isValidationResult(\n value: unknown,\n): value is A2UIValidationResult {\n return (\n typeof value === \"object\" &&\n value !== null &&\n !Array.isArray(value) &&\n typeof (value as Record<string, unknown>).valid === \"boolean\"\n );\n}\n"],"mappings":";;;;;;;;AAkCA,MAAa,wCACX;;;;;;;;;;;;AAwBF,SAAgB,gBAAgB,IAA6B;AAC3D,KAAI,GAAG,uBAAwB,QAAO;EAAE,UAAU;EAAM,OAAO;EAAO;CACtE,MAAM,UAAU,GAAG,kBAAkB;AACrC,QAAO;EACL,UAAU,YAAY;EACtB,OAAO,YAAY;EACpB;;;AAgBH,SAAS,aACP,WAC+C;AAC/C,QAAO,OAAO,YACZ,OAAO,QACL,UACD,CAAC,KAAK,CAAC,MAAM,UAAU,CACtB,MACA;EAAE,GAAG;EAAM,gBAAgB;EAAgB,CAC5C,CAAC,CACH;;;;;;;AAQH,MAAa,uBAET,aAAa;CACf,UAAU;EACR,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,OAAO;EACL,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,QAAQ;EACN,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,SAAS;EACP,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,OAAO;EACL,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,cAAc;EACZ,aACE;EACF,YAAY;EACZ,YAAY;EACb;CACD,cAAc;EACZ,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,gBAAgB;EACd,aAAa;EACb,YACE;EACF,YAAY;EACb;CACD,YAAY;EACV,aACE;EACF,YAAY;EACZ,YAAY;EACb;CACD,WAAW;EACT,aAAa;EACb,YACE;EACF,YAAY;EACb;CACD,SAAS;EACP,aAAa;EACb,YAAY;EACZ,YAAY;EACZ,wBAAwB;EACzB;CACD,KAAK;EACH,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,IAAI;EACF,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,KAAK;EACH,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACF,CAAC;;;;;;;;AASF,MAAa,uBAET,aAAa;CACf,MAAM;EACJ,aACE;EACF,YACE;EACF,YAAY;EACZ,WAAW;EACZ;CAKD,QAAQ;EACN,aACE;EACF,YACE;EACF,YAAY;EACZ,WAAW;EACZ;CACF,CAAC;;AAGF,MAAa,0BAET;CAAE,GAAG;CAAsB,GAAG;CAAsB;;AAGxD,MAAa,sBAAsB;;AAGnC,SAAgB,oBAAoB,MAAc;AAChD,QACE,SAAS,uBACT,OAAO,UAAU,eAAe,KAAK,yBAAyB,KAAK;;;;;;AAQvE,SAAgB,oBAAoB,MAAc;AAChD,KAAI,SAAS,oBAAqB,QAAO;CACzC,MAAM,OAAO,OAAO,UAAU,eAAe,KAC3C,yBACA,KACD,GACG,wBAAwB,QACxB;AACJ,QAAO,SAAS,UAAa,CAAC,KAAK;;;AAIrC,SAAgB,cACd,OAC8D;AAC9D,QACE,OAAO,UAAU,YACjB,UAAU,QACV,CAAC,MAAM,QAAQ,MAAM,IACrB,OAAQ,MAAkC,aAAa;;AAsB3D,SAAgB,mBACd,OAC+B;AAC/B,QACE,OAAO,UAAU,YACjB,UAAU,QACV,CAAC,MAAM,QAAQ,MAAM,IACrB,OAAQ,MAAkC,UAAU"}
|
|
@@ -122,7 +122,7 @@ const A2UI_BASIC_FUNCTIONS = rendererOnly({
|
|
|
122
122
|
*/
|
|
123
123
|
const COPILOTKIT_FUNCTIONS = rendererOnly({
|
|
124
124
|
calc: {
|
|
125
|
-
description: "The value of an arithmetic expression over the other args, by name: + - * / % ( ), comparisons, && || !, cond ? a : b, min max round floor ceil abs, 'text' + name",
|
|
125
|
+
description: "The value of an arithmetic expression over the other args, by name: + - * / % ( ), x ^ y (power), comparisons, && || !, cond ? a : b, min max round floor ceil abs pow sqrt exp ln log10, the constants pi and e, 'text' + name",
|
|
126
126
|
parameters: "{ expression: string; [name: string]: number | string | boolean }",
|
|
127
127
|
returnType: "any",
|
|
128
128
|
extension: true
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"basic-functions.mjs","names":[],"sources":["../../src/generative-ui/basic-functions.ts"],"sourcesContent":["/**\n * The functions of the A2UI 1.0 basic catalog, as the model and the\n * validator see them. The renderer implements them (react-core\n * `generative-ui/functions.ts`); this file holds only what both sides share:\n * the names, the short signatures for the schema the model reads, and who\n * may call each one.\n */\n\n/** What a function gives back, as A2UI 1.0 names it. */\nexport type A2UIFunctionReturnType =\n | \"string\"\n | \"number\"\n | \"boolean\"\n | \"array\"\n | \"object\"\n | \"validationResult\"\n | \"any\"\n | \"void\";\n\n/**\n * Who may call a function. The renderer calls it from an event binding or\n * as a prop value; the agent calls it with `callRendererFunction`.\n */\nexport type A2UIAllowedCallers =\n | \"rendererOnly\"\n | \"agentOnly\"\n | \"rendererOrAgent\";\n\n/**\n * The callers of an app function that names none: the renderer only, as\n * in A2UI 1.0. The agent calls a function with `callRendererFunction` only\n * when its declaration allows it (`\"rendererOrAgent\"` or `\"agentOnly\"`).\n * The manifests and the exported catalog write the value out.\n */\nexport const GENERATIVE_UI_DEFAULT_ALLOWED_CALLERS: A2UIAllowedCallers =\n \"rendererOnly\";\n\n/** The A2UI 1.0 interface fields of a function: who calls it, and how. */\nexport interface A2UIFunctionPermissions {\n allowedCallers?: A2UIAllowedCallers;\n /**\n * True for a function that only runs from a user gesture (a click, a key\n * press). It is `rendererOnly`: the agent cannot call it, and a prop\n * cannot be bound to it.\n */\n requiresUserActivation?: boolean;\n}\n\n/**\n * Who may call a function, from its permissions. A function that needs a\n * user gesture is for the renderer only, whatever `allowedCallers` says.\n *\n * @example\n * ```ts\n * functionCallers({}); // { renderer: true, agent: true }\n * functionCallers({ allowedCallers: \"rendererOnly\" }); // { renderer: true, agent: false }\n * functionCallers({ requiresUserActivation: true }); // { renderer: true, agent: false }\n * ```\n */\nexport function functionCallers(fn: A2UIFunctionPermissions) {\n if (fn.requiresUserActivation) return { renderer: true, agent: false };\n const callers = fn.allowedCallers ?? GENERATIVE_UI_DEFAULT_ALLOWED_CALLERS;\n return {\n renderer: callers !== \"agentOnly\",\n agent: callers !== \"rendererOnly\",\n };\n}\n\nexport interface A2UIBasicFunctionInfo extends A2UIFunctionPermissions {\n description: string;\n /** The args, as a short TypeScript-like type for the compact schema. */\n parameters: string;\n returnType: A2UIFunctionReturnType;\n /** Built-in functions are value functions of the renderer. */\n allowedCallers: \"rendererOnly\";\n requiresUserActivation?: true;\n /** True for a CopilotKit function that the A2UI basic catalog lacks. */\n extension?: true;\n}\n\n/** Marks a table of built-in functions as callable by the renderer only. */\nfunction rendererOnly<Name extends string>(\n functions: Record<Name, Omit<A2UIBasicFunctionInfo, \"allowedCallers\">>,\n): Readonly<Record<Name, A2UIBasicFunctionInfo>> {\n return Object.fromEntries(\n Object.entries<Omit<A2UIBasicFunctionInfo, \"allowedCallers\">>(\n functions,\n ).map(([name, info]) => [\n name,\n { ...info, allowedCallers: \"rendererOnly\" },\n ]),\n ) as Record<Name, A2UIBasicFunctionInfo>;\n}\n\n/**\n * The basic functions by name. A `{\"@call\"}` in a prop, in an event\n * binding, or in a `callRendererFunction` may name any of them without a\n * declaration in `createGenerativeUI({ functions })`.\n */\nexport const A2UI_BASIC_FUNCTIONS: Readonly<\n Record<string, A2UIBasicFunctionInfo>\n> = rendererOnly({\n required: {\n description: \"Valid when the value is not empty\",\n parameters: \"{ value: unknown }\",\n returnType: \"validationResult\",\n },\n regex: {\n description: \"Valid when the value matches the pattern\",\n parameters: \"{ value: string; pattern: string }\",\n returnType: \"validationResult\",\n },\n length: {\n description: \"Valid when the length of the value is within min and max\",\n parameters: \"{ value: string; min?: number; max?: number }\",\n returnType: \"validationResult\",\n },\n numeric: {\n description: \"Valid when the value is a number within min and max\",\n parameters: \"{ value: number; min?: number; max?: number }\",\n returnType: \"validationResult\",\n },\n email: {\n description: \"Valid when the value is an email address\",\n parameters: \"{ value: string }\",\n returnType: \"validationResult\",\n },\n formatString: {\n description:\n \"Interpolates ${/path}, ${relative/path} and ${fn(arg: value)} in the value; \\\\${ is a literal\",\n parameters: \"{ value: string }\",\n returnType: \"string\",\n },\n formatNumber: {\n description: \"The number in the locale of the user\",\n parameters: \"{ value: number; decimals?: number; grouping?: boolean }\",\n returnType: \"string\",\n },\n formatCurrency: {\n description: \"The amount as money, with an ISO 4217 currency code\",\n parameters:\n \"{ value: number; currency: string; decimals?: number; grouping?: boolean }\",\n returnType: \"string\",\n },\n formatDate: {\n description:\n 'A date or ISO string in a TR35 pattern, for example \"MMM d, yyyy\" or \"HH:mm\"',\n parameters: \"{ value: string | number; format: string }\",\n returnType: \"string\",\n },\n pluralize: {\n description: \"The string of the plural category of the value\",\n parameters:\n \"{ value: number; zero?: string; one?: string; two?: string; few?: string; many?: string; other: string }\",\n returnType: \"string\",\n },\n openUrl: {\n description: \"Opens an http(s) URL in a new tab. Only from an event\",\n parameters: \"{ url: string }\",\n returnType: \"void\",\n requiresUserActivation: true,\n },\n and: {\n description: \"True when every value is true\",\n parameters: \"{ values: boolean[] }\",\n returnType: \"boolean\",\n },\n or: {\n description: \"True when any value is true\",\n parameters: \"{ values: boolean[] }\",\n returnType: \"boolean\",\n },\n not: {\n description: \"The negated value\",\n parameters: \"{ value: boolean }\",\n returnType: \"boolean\",\n },\n});\n\n/**\n * The value functions that CopilotKit adds to the basic catalog, because\n * A2UI 1.0 has no arithmetic: `calc` evaluates an expression over named\n * args, and `series` builds a computed list. A catalog may declare its own\n * functions in A2UI, so these are regular catalog functions to the model;\n * a renderer without them rejects the call instead of misrendering.\n */\nexport const COPILOTKIT_FUNCTIONS: Readonly<\n Record<string, A2UIBasicFunctionInfo>\n> = rendererOnly({\n calc: {\n description:\n \"The value of an arithmetic expression over the other args, by name: + - * / % ( ), comparisons, && || !, cond ? a : b, min max round floor ceil abs, 'text' + name\",\n parameters:\n \"{ expression: string; [name: string]: number | string | boolean }\",\n returnType: \"any\",\n extension: true,\n },\n // WORKAROUND(a2ui-agent-sdk 0.9.0): examples omit `variable` and use default i\n // because the Express compiler mistakes a literal `variable` key for its AST.\n // Runtime support for custom variable names is unchanged.\n // https://github.com/a2ui-project/a2ui/issues/3410\n series: {\n description:\n 'A list of count copies of item, for chart data and template lists. Put arithmetic in ${expression} strings in item, using the loop variable (default i, counting from start, default 1) and other named args. A whole-expression string retains the result type, so \"${i * monthlyCost}\" yields a number. Arguments, including nested calc or formatString calls in item, resolve before the series loop; those calls cannot use the loop variable. Example: {\"@call\":\"series\",\"args\":{\"count\":6,\"item\":{\"month\":\"${i}\",\"cost\":\"${i * monthlyCost}\"},\"monthlyCost\":{\"@path\":\"/monthlyCost\"}}}. Omit variable to use i.',\n parameters:\n \"{ count: number; item: unknown; variable?: string; start?: number; [name: string]: unknown }\",\n returnType: \"array\",\n extension: true,\n },\n});\n\n/** Every function a `{\"@call\"}` may name without a declaration: basic and CopilotKit. */\nexport const GENERATIVE_UI_FUNCTIONS: Readonly<\n Record<string, A2UIBasicFunctionInfo>\n> = { ...A2UI_BASIC_FUNCTIONS, ...COPILOTKIT_FUNCTIONS };\n\n/** The name of the system function that gives the index of a template item. */\nexport const A2UI_INDEX_FUNCTION = \"@index\";\n\n/** True for a built-in function name: basic, CopilotKit, or `@index`. */\nexport function isA2UIBasicFunction(name: string) {\n return (\n name === A2UI_INDEX_FUNCTION ||\n Object.prototype.hasOwnProperty.call(GENERATIVE_UI_FUNCTIONS, name)\n );\n}\n\n/**\n * True for a built-in function that a prop or a check may call: every one\n * except the functions that need a user gesture.\n */\nexport function isA2UIValueFunction(name: string) {\n if (name === A2UI_INDEX_FUNCTION) return true;\n const info = Object.prototype.hasOwnProperty.call(\n GENERATIVE_UI_FUNCTIONS,\n name,\n )\n ? GENERATIVE_UI_FUNCTIONS[name]\n : undefined;\n return info !== undefined && !info.requiresUserActivation;\n}\n\n/** `{ \"@call\": \"<name>\", \"args\"?: {...} }`: a function call value. */\nexport function isCallBinding(\n value: unknown,\n): value is { \"@call\": string; args?: Record<string, unknown> } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n !Array.isArray(value) &&\n typeof (value as Record<string, unknown>)[\"@call\"] === \"string\"\n );\n}\n\n/** How bad a failed check is. `error` is the default; only an error blocks. */\nexport type A2UIValidationSeverity = \"error\" | \"warning\" | \"info\";\n\n/**\n * A2UI's `ValidationResult`: what a check gives. A validator function gives\n * one; a check may also bind a boolean, which the renderer coerces to one.\n * `code` names the failure for programs; `severity` says how bad it is,\n * `\"error\"` when left out. Other keys are allowed, for app validators that\n * carry more (a suggested value, a field path).\n */\nexport interface A2UIValidationResult {\n valid: boolean;\n message?: string;\n code?: string;\n severity?: A2UIValidationSeverity;\n [extra: string]: unknown;\n}\n\nexport function isValidationResult(\n value: unknown,\n): value is A2UIValidationResult {\n return (\n typeof value === \"object\" &&\n value !== null &&\n !Array.isArray(value) &&\n typeof (value as Record<string, unknown>).valid === \"boolean\"\n );\n}\n"],"mappings":";;;;;;;AAkCA,MAAa,wCACX;;;;;;;;;;;;AAwBF,SAAgB,gBAAgB,IAA6B;AAC3D,KAAI,GAAG,uBAAwB,QAAO;EAAE,UAAU;EAAM,OAAO;EAAO;CACtE,MAAM,UAAU,GAAG,kBAAkB;AACrC,QAAO;EACL,UAAU,YAAY;EACtB,OAAO,YAAY;EACpB;;;AAgBH,SAAS,aACP,WAC+C;AAC/C,QAAO,OAAO,YACZ,OAAO,QACL,UACD,CAAC,KAAK,CAAC,MAAM,UAAU,CACtB,MACA;EAAE,GAAG;EAAM,gBAAgB;EAAgB,CAC5C,CAAC,CACH;;;;;;;AAQH,MAAa,uBAET,aAAa;CACf,UAAU;EACR,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,OAAO;EACL,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,QAAQ;EACN,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,SAAS;EACP,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,OAAO;EACL,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,cAAc;EACZ,aACE;EACF,YAAY;EACZ,YAAY;EACb;CACD,cAAc;EACZ,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,gBAAgB;EACd,aAAa;EACb,YACE;EACF,YAAY;EACb;CACD,YAAY;EACV,aACE;EACF,YAAY;EACZ,YAAY;EACb;CACD,WAAW;EACT,aAAa;EACb,YACE;EACF,YAAY;EACb;CACD,SAAS;EACP,aAAa;EACb,YAAY;EACZ,YAAY;EACZ,wBAAwB;EACzB;CACD,KAAK;EACH,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,IAAI;EACF,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,KAAK;EACH,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACF,CAAC;;;;;;;;AASF,MAAa,uBAET,aAAa;CACf,MAAM;EACJ,aACE;EACF,YACE;EACF,YAAY;EACZ,WAAW;EACZ;CAKD,QAAQ;EACN,aACE;EACF,YACE;EACF,YAAY;EACZ,WAAW;EACZ;CACF,CAAC;;AAGF,MAAa,0BAET;CAAE,GAAG;CAAsB,GAAG;CAAsB;;AAGxD,MAAa,sBAAsB;;AAGnC,SAAgB,oBAAoB,MAAc;AAChD,QACE,SAAS,uBACT,OAAO,UAAU,eAAe,KAAK,yBAAyB,KAAK;;;;;;AAQvE,SAAgB,oBAAoB,MAAc;AAChD,KAAI,SAAS,oBAAqB,QAAO;CACzC,MAAM,OAAO,OAAO,UAAU,eAAe,KAC3C,yBACA,KACD,GACG,wBAAwB,QACxB;AACJ,QAAO,SAAS,UAAa,CAAC,KAAK;;;AAIrC,SAAgB,cACd,OAC8D;AAC9D,QACE,OAAO,UAAU,YACjB,UAAU,QACV,CAAC,MAAM,QAAQ,MAAM,IACrB,OAAQ,MAAkC,aAAa;;AAsB3D,SAAgB,mBACd,OAC+B;AAC/B,QACE,OAAO,UAAU,YACjB,UAAU,QACV,CAAC,MAAM,QAAQ,MAAM,IACrB,OAAQ,MAAkC,UAAU"}
|
|
1
|
+
{"version":3,"file":"basic-functions.mjs","names":[],"sources":["../../src/generative-ui/basic-functions.ts"],"sourcesContent":["/**\n * The functions of the A2UI 1.0 basic catalog, as the model and the\n * validator see them. The renderer implements them (react-core\n * `generative-ui/functions.ts`); this file holds only what both sides share:\n * the names, the short signatures for the schema the model reads, and who\n * may call each one.\n */\n\n/** What a function gives back, as A2UI 1.0 names it. */\nexport type A2UIFunctionReturnType =\n | \"string\"\n | \"number\"\n | \"boolean\"\n | \"array\"\n | \"object\"\n | \"validationResult\"\n | \"any\"\n | \"void\";\n\n/**\n * Who may call a function. The renderer calls it from an event binding or\n * as a prop value; the agent calls it with `callRendererFunction`.\n */\nexport type A2UIAllowedCallers =\n | \"rendererOnly\"\n | \"agentOnly\"\n | \"rendererOrAgent\";\n\n/**\n * The callers of an app function that names none: the renderer only, as\n * in A2UI 1.0. The agent calls a function with `callRendererFunction` only\n * when its declaration allows it (`\"rendererOrAgent\"` or `\"agentOnly\"`).\n * The manifests and the exported catalog write the value out.\n */\nexport const GENERATIVE_UI_DEFAULT_ALLOWED_CALLERS: A2UIAllowedCallers =\n \"rendererOnly\";\n\n/** The A2UI 1.0 interface fields of a function: who calls it, and how. */\nexport interface A2UIFunctionPermissions {\n allowedCallers?: A2UIAllowedCallers;\n /**\n * True for a function that only runs from a user gesture (a click, a key\n * press). It is `rendererOnly`: the agent cannot call it, and a prop\n * cannot be bound to it.\n */\n requiresUserActivation?: boolean;\n}\n\n/**\n * Who may call a function, from its permissions. A function that needs a\n * user gesture is for the renderer only, whatever `allowedCallers` says.\n *\n * @example\n * ```ts\n * functionCallers({}); // { renderer: true, agent: true }\n * functionCallers({ allowedCallers: \"rendererOnly\" }); // { renderer: true, agent: false }\n * functionCallers({ requiresUserActivation: true }); // { renderer: true, agent: false }\n * ```\n */\nexport function functionCallers(fn: A2UIFunctionPermissions) {\n if (fn.requiresUserActivation) return { renderer: true, agent: false };\n const callers = fn.allowedCallers ?? GENERATIVE_UI_DEFAULT_ALLOWED_CALLERS;\n return {\n renderer: callers !== \"agentOnly\",\n agent: callers !== \"rendererOnly\",\n };\n}\n\nexport interface A2UIBasicFunctionInfo extends A2UIFunctionPermissions {\n description: string;\n /** The args, as a short TypeScript-like type for the compact schema. */\n parameters: string;\n returnType: A2UIFunctionReturnType;\n /** Built-in functions are value functions of the renderer. */\n allowedCallers: \"rendererOnly\";\n requiresUserActivation?: true;\n /** True for a CopilotKit function that the A2UI basic catalog lacks. */\n extension?: true;\n}\n\n/** Marks a table of built-in functions as callable by the renderer only. */\nfunction rendererOnly<Name extends string>(\n functions: Record<Name, Omit<A2UIBasicFunctionInfo, \"allowedCallers\">>,\n): Readonly<Record<Name, A2UIBasicFunctionInfo>> {\n return Object.fromEntries(\n Object.entries<Omit<A2UIBasicFunctionInfo, \"allowedCallers\">>(\n functions,\n ).map(([name, info]) => [\n name,\n { ...info, allowedCallers: \"rendererOnly\" },\n ]),\n ) as Record<Name, A2UIBasicFunctionInfo>;\n}\n\n/**\n * The basic functions by name. A `{\"@call\"}` in a prop, in an event\n * binding, or in a `callRendererFunction` may name any of them without a\n * declaration in `createGenerativeUI({ functions })`.\n */\nexport const A2UI_BASIC_FUNCTIONS: Readonly<\n Record<string, A2UIBasicFunctionInfo>\n> = rendererOnly({\n required: {\n description: \"Valid when the value is not empty\",\n parameters: \"{ value: unknown }\",\n returnType: \"validationResult\",\n },\n regex: {\n description: \"Valid when the value matches the pattern\",\n parameters: \"{ value: string; pattern: string }\",\n returnType: \"validationResult\",\n },\n length: {\n description: \"Valid when the length of the value is within min and max\",\n parameters: \"{ value: string; min?: number; max?: number }\",\n returnType: \"validationResult\",\n },\n numeric: {\n description: \"Valid when the value is a number within min and max\",\n parameters: \"{ value: number; min?: number; max?: number }\",\n returnType: \"validationResult\",\n },\n email: {\n description: \"Valid when the value is an email address\",\n parameters: \"{ value: string }\",\n returnType: \"validationResult\",\n },\n formatString: {\n description:\n \"Interpolates ${/path}, ${relative/path} and ${fn(arg: value)} in the value; \\\\${ is a literal\",\n parameters: \"{ value: string }\",\n returnType: \"string\",\n },\n formatNumber: {\n description: \"The number in the locale of the user\",\n parameters: \"{ value: number; decimals?: number; grouping?: boolean }\",\n returnType: \"string\",\n },\n formatCurrency: {\n description: \"The amount as money, with an ISO 4217 currency code\",\n parameters:\n \"{ value: number; currency: string; decimals?: number; grouping?: boolean }\",\n returnType: \"string\",\n },\n formatDate: {\n description:\n 'A date or ISO string in a TR35 pattern, for example \"MMM d, yyyy\" or \"HH:mm\"',\n parameters: \"{ value: string | number; format: string }\",\n returnType: \"string\",\n },\n pluralize: {\n description: \"The string of the plural category of the value\",\n parameters:\n \"{ value: number; zero?: string; one?: string; two?: string; few?: string; many?: string; other: string }\",\n returnType: \"string\",\n },\n openUrl: {\n description: \"Opens an http(s) URL in a new tab. Only from an event\",\n parameters: \"{ url: string }\",\n returnType: \"void\",\n requiresUserActivation: true,\n },\n and: {\n description: \"True when every value is true\",\n parameters: \"{ values: boolean[] }\",\n returnType: \"boolean\",\n },\n or: {\n description: \"True when any value is true\",\n parameters: \"{ values: boolean[] }\",\n returnType: \"boolean\",\n },\n not: {\n description: \"The negated value\",\n parameters: \"{ value: boolean }\",\n returnType: \"boolean\",\n },\n});\n\n/**\n * The value functions that CopilotKit adds to the basic catalog, because\n * A2UI 1.0 has no arithmetic: `calc` evaluates an expression over named\n * args, and `series` builds a computed list. A catalog may declare its own\n * functions in A2UI, so these are regular catalog functions to the model;\n * a renderer without them rejects the call instead of misrendering.\n */\nexport const COPILOTKIT_FUNCTIONS: Readonly<\n Record<string, A2UIBasicFunctionInfo>\n> = rendererOnly({\n calc: {\n description:\n \"The value of an arithmetic expression over the other args, by name: + - * / % ( ), x ^ y (power), comparisons, && || !, cond ? a : b, min max round floor ceil abs pow sqrt exp ln log10, the constants pi and e, 'text' + name\",\n parameters:\n \"{ expression: string; [name: string]: number | string | boolean }\",\n returnType: \"any\",\n extension: true,\n },\n // WORKAROUND(a2ui-agent-sdk 0.9.0): examples omit `variable` and use default i\n // because the Express compiler mistakes a literal `variable` key for its AST.\n // Runtime support for custom variable names is unchanged.\n // https://github.com/a2ui-project/a2ui/issues/3410\n series: {\n description:\n 'A list of count copies of item, for chart data and template lists. Put arithmetic in ${expression} strings in item, using the loop variable (default i, counting from start, default 1) and other named args. A whole-expression string retains the result type, so \"${i * monthlyCost}\" yields a number. Arguments, including nested calc or formatString calls in item, resolve before the series loop; those calls cannot use the loop variable. Example: {\"@call\":\"series\",\"args\":{\"count\":6,\"item\":{\"month\":\"${i}\",\"cost\":\"${i * monthlyCost}\"},\"monthlyCost\":{\"@path\":\"/monthlyCost\"}}}. Omit variable to use i.',\n parameters:\n \"{ count: number; item: unknown; variable?: string; start?: number; [name: string]: unknown }\",\n returnType: \"array\",\n extension: true,\n },\n});\n\n/** Every function a `{\"@call\"}` may name without a declaration: basic and CopilotKit. */\nexport const GENERATIVE_UI_FUNCTIONS: Readonly<\n Record<string, A2UIBasicFunctionInfo>\n> = { ...A2UI_BASIC_FUNCTIONS, ...COPILOTKIT_FUNCTIONS };\n\n/** The name of the system function that gives the index of a template item. */\nexport const A2UI_INDEX_FUNCTION = \"@index\";\n\n/** True for a built-in function name: basic, CopilotKit, or `@index`. */\nexport function isA2UIBasicFunction(name: string) {\n return (\n name === A2UI_INDEX_FUNCTION ||\n Object.prototype.hasOwnProperty.call(GENERATIVE_UI_FUNCTIONS, name)\n );\n}\n\n/**\n * True for a built-in function that a prop or a check may call: every one\n * except the functions that need a user gesture.\n */\nexport function isA2UIValueFunction(name: string) {\n if (name === A2UI_INDEX_FUNCTION) return true;\n const info = Object.prototype.hasOwnProperty.call(\n GENERATIVE_UI_FUNCTIONS,\n name,\n )\n ? GENERATIVE_UI_FUNCTIONS[name]\n : undefined;\n return info !== undefined && !info.requiresUserActivation;\n}\n\n/** `{ \"@call\": \"<name>\", \"args\"?: {...} }`: a function call value. */\nexport function isCallBinding(\n value: unknown,\n): value is { \"@call\": string; args?: Record<string, unknown> } {\n return (\n typeof value === \"object\" &&\n value !== null &&\n !Array.isArray(value) &&\n typeof (value as Record<string, unknown>)[\"@call\"] === \"string\"\n );\n}\n\n/** How bad a failed check is. `error` is the default; only an error blocks. */\nexport type A2UIValidationSeverity = \"error\" | \"warning\" | \"info\";\n\n/**\n * A2UI's `ValidationResult`: what a check gives. A validator function gives\n * one; a check may also bind a boolean, which the renderer coerces to one.\n * `code` names the failure for programs; `severity` says how bad it is,\n * `\"error\"` when left out. Other keys are allowed, for app validators that\n * carry more (a suggested value, a field path).\n */\nexport interface A2UIValidationResult {\n valid: boolean;\n message?: string;\n code?: string;\n severity?: A2UIValidationSeverity;\n [extra: string]: unknown;\n}\n\nexport function isValidationResult(\n value: unknown,\n): value is A2UIValidationResult {\n return (\n typeof value === \"object\" &&\n value !== null &&\n !Array.isArray(value) &&\n typeof (value as Record<string, unknown>).valid === \"boolean\"\n );\n}\n"],"mappings":";;;;;;;AAkCA,MAAa,wCACX;;;;;;;;;;;;AAwBF,SAAgB,gBAAgB,IAA6B;AAC3D,KAAI,GAAG,uBAAwB,QAAO;EAAE,UAAU;EAAM,OAAO;EAAO;CACtE,MAAM,UAAU,GAAG,kBAAkB;AACrC,QAAO;EACL,UAAU,YAAY;EACtB,OAAO,YAAY;EACpB;;;AAgBH,SAAS,aACP,WAC+C;AAC/C,QAAO,OAAO,YACZ,OAAO,QACL,UACD,CAAC,KAAK,CAAC,MAAM,UAAU,CACtB,MACA;EAAE,GAAG;EAAM,gBAAgB;EAAgB,CAC5C,CAAC,CACH;;;;;;;AAQH,MAAa,uBAET,aAAa;CACf,UAAU;EACR,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,OAAO;EACL,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,QAAQ;EACN,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,SAAS;EACP,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,OAAO;EACL,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,cAAc;EACZ,aACE;EACF,YAAY;EACZ,YAAY;EACb;CACD,cAAc;EACZ,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,gBAAgB;EACd,aAAa;EACb,YACE;EACF,YAAY;EACb;CACD,YAAY;EACV,aACE;EACF,YAAY;EACZ,YAAY;EACb;CACD,WAAW;EACT,aAAa;EACb,YACE;EACF,YAAY;EACb;CACD,SAAS;EACP,aAAa;EACb,YAAY;EACZ,YAAY;EACZ,wBAAwB;EACzB;CACD,KAAK;EACH,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,IAAI;EACF,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACD,KAAK;EACH,aAAa;EACb,YAAY;EACZ,YAAY;EACb;CACF,CAAC;;;;;;;;AASF,MAAa,uBAET,aAAa;CACf,MAAM;EACJ,aACE;EACF,YACE;EACF,YAAY;EACZ,WAAW;EACZ;CAKD,QAAQ;EACN,aACE;EACF,YACE;EACF,YAAY;EACZ,WAAW;EACZ;CACF,CAAC;;AAGF,MAAa,0BAET;CAAE,GAAG;CAAsB,GAAG;CAAsB;;AAGxD,MAAa,sBAAsB;;AAGnC,SAAgB,oBAAoB,MAAc;AAChD,QACE,SAAS,uBACT,OAAO,UAAU,eAAe,KAAK,yBAAyB,KAAK;;;;;;AAQvE,SAAgB,oBAAoB,MAAc;AAChD,KAAI,SAAS,oBAAqB,QAAO;CACzC,MAAM,OAAO,OAAO,UAAU,eAAe,KAC3C,yBACA,KACD,GACG,wBAAwB,QACxB;AACJ,QAAO,SAAS,UAAa,CAAC,KAAK;;;AAIrC,SAAgB,cACd,OAC8D;AAC9D,QACE,OAAO,UAAU,YACjB,UAAU,QACV,CAAC,MAAM,QAAQ,MAAM,IACrB,OAAQ,MAAkC,aAAa;;AAsB3D,SAAgB,mBACd,OAC+B;AAC/B,QACE,OAAO,UAAU,YACjB,UAAU,QACV,CAAC,MAAM,QAAQ,MAAM,IACrB,OAAQ,MAAkC,UAAU"}
|
|
@@ -0,0 +1,187 @@
|
|
|
1
|
+
const require_basic_functions = require('./basic-functions.cjs');
|
|
2
|
+
const require_child_refs = require('./child-refs.cjs');
|
|
3
|
+
const require_types = require('./types.cjs');
|
|
4
|
+
|
|
5
|
+
//#region src/generative-ui/compact-schema.ts
|
|
6
|
+
/** Objects nested deeper than this show as `object`. */
|
|
7
|
+
const MAX_OBJECT_DEPTH = 2;
|
|
8
|
+
/** A prop must appear verbatim in at least this many components to be hoisted. */
|
|
9
|
+
const COMMON_PROP_MIN_COMPONENTS = 8;
|
|
10
|
+
function isRecord(value) {
|
|
11
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
12
|
+
}
|
|
13
|
+
/** A JSON Schema of a list of schemas (`anyOf`, `oneOf`), else an empty list. */
|
|
14
|
+
function schemaList(value) {
|
|
15
|
+
return Array.isArray(value) ? value.filter(isRecord) : [];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Puts a top-level union in parentheses, so that `[]` applies to all of
|
|
19
|
+
* it. A union inside braces needs none.
|
|
20
|
+
*/
|
|
21
|
+
function asArrayItem(type) {
|
|
22
|
+
let outside = type;
|
|
23
|
+
for (let inner = outside.replace(/\{[^{}]*\}/g, ""); inner !== outside;) {
|
|
24
|
+
outside = inner;
|
|
25
|
+
inner = outside.replace(/\{[^{}]*\}/g, "");
|
|
26
|
+
}
|
|
27
|
+
return outside.includes(" | ") ? `(${type})[]` : `${type}[]`;
|
|
28
|
+
}
|
|
29
|
+
/**
|
|
30
|
+
* A JSON Schema as a short TypeScript-like type, for example
|
|
31
|
+
* `{ label: string; size?: "sm" | "md" }`.
|
|
32
|
+
*/
|
|
33
|
+
function compactType(schema, depth) {
|
|
34
|
+
const ref = require_child_refs.childRefKind(schema);
|
|
35
|
+
if (ref === "single") return "componentId";
|
|
36
|
+
if (ref === "multiple") return "componentId[] | { componentId: string; path: string }";
|
|
37
|
+
if ("const" in schema) return JSON.stringify(schema.const);
|
|
38
|
+
if (Array.isArray(schema.enum)) return schema.enum.map((value) => JSON.stringify(value)).join(" | ");
|
|
39
|
+
const variants = [...schemaList(schema.anyOf), ...schemaList(schema.oneOf)];
|
|
40
|
+
if (variants.length > 0) return variants.map((variant) => compactType(variant, depth)).join(" | ");
|
|
41
|
+
return (Array.isArray(schema.type) ? schema.type : [schema.type]).map((type) => {
|
|
42
|
+
switch (type) {
|
|
43
|
+
case "string":
|
|
44
|
+
case "boolean":
|
|
45
|
+
case "null": return type;
|
|
46
|
+
case "number":
|
|
47
|
+
case "integer": return "number";
|
|
48
|
+
case "array": return isRecord(schema.items) ? asArrayItem(compactType(schema.items, depth)) : "unknown[]";
|
|
49
|
+
case "object": return compactObject(schema, depth);
|
|
50
|
+
default: return "unknown";
|
|
51
|
+
}
|
|
52
|
+
}).join(" | ");
|
|
53
|
+
}
|
|
54
|
+
/** An object schema as `{ key: type; other?: type }`. */
|
|
55
|
+
function compactObject(schema, depth) {
|
|
56
|
+
const properties = isRecord(schema.properties) ? schema.properties : {};
|
|
57
|
+
const entries = Object.entries(properties);
|
|
58
|
+
if (entries.length === 0) return isRecord(schema.additionalProperties) ? `Record<string, ${compactType(schema.additionalProperties, depth + 1)}>` : "object";
|
|
59
|
+
if (depth >= MAX_OBJECT_DEPTH) return "object";
|
|
60
|
+
const required = Array.isArray(schema.required) ? schema.required : [];
|
|
61
|
+
return `{ ${entries.map(([key, value]) => {
|
|
62
|
+
const type = isRecord(value) ? compactType(value, depth + 1) : "unknown";
|
|
63
|
+
return `${key}${required.includes(key) ? "" : "?"}: ${type}`;
|
|
64
|
+
}).join("; ")} }`;
|
|
65
|
+
}
|
|
66
|
+
/** A schema as its type, with its description after `//`. */
|
|
67
|
+
function describedType(schema, depth) {
|
|
68
|
+
const type = compactType(schema, depth);
|
|
69
|
+
return typeof schema.description === "string" ? `${type} // ${schema.description}` : type;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* The props of a component: `{ "label": "string", "size?": "..." }`. With
|
|
73
|
+
* `descriptions`, each type carries its description after `//`.
|
|
74
|
+
*/
|
|
75
|
+
function compactProps(schema, descriptions = true) {
|
|
76
|
+
const properties = isRecord(schema.properties) ? schema.properties : {};
|
|
77
|
+
const required = Array.isArray(schema.required) ? schema.required : [];
|
|
78
|
+
const props = {};
|
|
79
|
+
for (const [key, value] of Object.entries(properties)) {
|
|
80
|
+
if (!isRecord(value)) continue;
|
|
81
|
+
const name = required.includes(key) ? key : `${key}?`;
|
|
82
|
+
props[name] = descriptions ? describedType(value, 1) : compactType(value, 1);
|
|
83
|
+
}
|
|
84
|
+
return props;
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* The prop set worth listing once. Candidates are (key, type) pairs that at
|
|
88
|
+
* least {@link COMMON_PROP_MIN_COMPONENTS} components carry verbatim. The
|
|
89
|
+
* candidate subset of each component is a possible common set, scored by
|
|
90
|
+
* the characters it saves (set size times the components that contain all
|
|
91
|
+
* of it). `undefined` when no set is shared by enough components.
|
|
92
|
+
*/
|
|
93
|
+
function findCommonProps(propsList) {
|
|
94
|
+
const pairKey = (key, type) => `${key}\u0000${type}`;
|
|
95
|
+
const counts = /* @__PURE__ */ new Map();
|
|
96
|
+
for (const props of propsList) for (const [key, type] of Object.entries(props)) {
|
|
97
|
+
const id = pairKey(key, type);
|
|
98
|
+
counts.set(id, (counts.get(id) ?? 0) + 1);
|
|
99
|
+
}
|
|
100
|
+
let best;
|
|
101
|
+
let bestScore = 0;
|
|
102
|
+
const seen = /* @__PURE__ */ new Set();
|
|
103
|
+
for (const props of propsList) {
|
|
104
|
+
const subset = Object.entries(props).filter(([key, type]) => (counts.get(pairKey(key, type)) ?? 0) >= COMMON_PROP_MIN_COMPONENTS);
|
|
105
|
+
if (subset.length < 2) continue;
|
|
106
|
+
const signature = subset.map(([key, type]) => pairKey(key, type)).join("\n");
|
|
107
|
+
if (seen.has(signature)) continue;
|
|
108
|
+
seen.add(signature);
|
|
109
|
+
const members = propsList.filter((other) => subset.every(([key, type]) => other[key] === type)).length;
|
|
110
|
+
if (members < COMMON_PROP_MIN_COMPONENTS) continue;
|
|
111
|
+
const score = subset.reduce((total, [key, type]) => total + key.length + type.length, 0) * (members - 1);
|
|
112
|
+
if (score > bestScore) {
|
|
113
|
+
bestScore = score;
|
|
114
|
+
best = Object.fromEntries(subset);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
return best;
|
|
118
|
+
}
|
|
119
|
+
/**
|
|
120
|
+
* The component schema of the markdown transport, from the per-run
|
|
121
|
+
* manifest. It is smaller than the manifest: each prop is one short type,
|
|
122
|
+
* and the props that many components share verbatim (the layout props of
|
|
123
|
+
* the containers) are listed once in `commonProps`; a component that takes
|
|
124
|
+
* them is flagged `includesCommonProps`.
|
|
125
|
+
* It has the catalog id, the targets with their descriptions and their
|
|
126
|
+
* component lists, the functions with their parameters, the components
|
|
127
|
+
* with their props, events and slots, and the surfaces of the thread. A surface lists the
|
|
128
|
+
* values that the user set and the operations that the client dropped.
|
|
129
|
+
*
|
|
130
|
+
* @param manifest - The manifest from `buildGenerativeUIManifest`.
|
|
131
|
+
* @param options - `propDescriptions` (default true) writes each prop's
|
|
132
|
+
* description; `commonProps` (default true) lists shared props once.
|
|
133
|
+
* @returns A JSON-ready object.
|
|
134
|
+
*
|
|
135
|
+
* @example
|
|
136
|
+
* ```ts
|
|
137
|
+
* const value = JSON.stringify(
|
|
138
|
+
* buildCompactGenerativeUISchema(buildGenerativeUIManifest(runtime, threadId)),
|
|
139
|
+
* );
|
|
140
|
+
* ```
|
|
141
|
+
*/
|
|
142
|
+
function buildCompactGenerativeUISchema(manifest, options = {}) {
|
|
143
|
+
const { propDescriptions = true, commonProps = true } = options;
|
|
144
|
+
const components = {};
|
|
145
|
+
for (const [name, component] of Object.entries(manifest.components)) {
|
|
146
|
+
const events = Object.entries(component.events ?? {}).map(([event, definition]) => [event, definition.description ?? compactType(definition.parameters, 0)]);
|
|
147
|
+
components[name] = {
|
|
148
|
+
...component.description ? { description: component.description } : {},
|
|
149
|
+
props: compactProps(component.props, propDescriptions),
|
|
150
|
+
...events.length > 0 ? { events: Object.fromEntries(events) } : {},
|
|
151
|
+
...component.slots ? { slots: component.slots } : {}
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
const common = commonProps ? findCommonProps(Object.values(components).map((component) => component.props)) : void 0;
|
|
155
|
+
if (common) {
|
|
156
|
+
const commonKeys = Object.keys(common);
|
|
157
|
+
for (const component of Object.values(components)) {
|
|
158
|
+
if (!commonKeys.every((key) => component.props[key] === common[key])) continue;
|
|
159
|
+
component.includesCommonProps = true;
|
|
160
|
+
component.props = Object.fromEntries(Object.entries(component.props).filter(([key]) => !(key in common)));
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
const functions = Object.fromEntries(Object.entries(manifest.functions).map(([name, fn]) => {
|
|
164
|
+
const signature = fn.parameters.description;
|
|
165
|
+
const hasSignature = typeof signature === "string" && signature.startsWith("{") && Object.keys(fn.parameters).length === 2;
|
|
166
|
+
const callers = require_basic_functions.functionCallers(fn);
|
|
167
|
+
return [name, {
|
|
168
|
+
...fn.description ? { description: fn.description } : {},
|
|
169
|
+
parameters: hasSignature ? signature : compactType(fn.parameters, 0),
|
|
170
|
+
...callers.agent && require_types.GENERATIVE_UI_AGENT_FUNCTION_CALLS ? { callers: callers.renderer ? "UI or agent" : "agent only" } : {}
|
|
171
|
+
}];
|
|
172
|
+
}));
|
|
173
|
+
return {
|
|
174
|
+
catalogId: manifest.catalogId,
|
|
175
|
+
targets: manifest.targets,
|
|
176
|
+
functions,
|
|
177
|
+
...common ? { commonProps: common } : {},
|
|
178
|
+
components,
|
|
179
|
+
surfaces: manifest.surfaces
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
//#endregion
|
|
184
|
+
exports.buildCompactGenerativeUISchema = buildCompactGenerativeUISchema;
|
|
185
|
+
exports.compactProps = compactProps;
|
|
186
|
+
exports.compactType = compactType;
|
|
187
|
+
//# sourceMappingURL=compact-schema.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compact-schema.cjs","names":["childRefKind","functionCallers","GENERATIVE_UI_AGENT_FUNCTION_CALLS"],"sources":["../../src/generative-ui/compact-schema.ts"],"sourcesContent":["import { childRefKind } from \"./child-refs\";\nimport { functionCallers } from \"./basic-functions\";\nimport { GENERATIVE_UI_AGENT_FUNCTION_CALLS } from \"./types\";\nimport type { GenerativeUIJSONSchema, GenerativeUIManifest } from \"./types\";\n\n/** Objects nested deeper than this show as `object`. */\nconst MAX_OBJECT_DEPTH = 2;\n\n/** A prop must appear verbatim in at least this many components to be hoisted. */\nconst COMMON_PROP_MIN_COMPONENTS = 8;\n\nfunction isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === \"object\" && value !== null && !Array.isArray(value);\n}\n\n/** A JSON Schema of a list of schemas (`anyOf`, `oneOf`), else an empty list. */\nfunction schemaList(value: unknown) {\n return Array.isArray(value) ? value.filter(isRecord) : [];\n}\n\n/**\n * Puts a top-level union in parentheses, so that `[]` applies to all of\n * it. A union inside braces needs none.\n */\nfunction asArrayItem(type: string) {\n let outside = type;\n for (let inner = outside.replace(/\\{[^{}]*\\}/g, \"\"); inner !== outside; ) {\n outside = inner;\n inner = outside.replace(/\\{[^{}]*\\}/g, \"\");\n }\n return outside.includes(\" | \") ? `(${type})[]` : `${type}[]`;\n}\n\n/**\n * A JSON Schema as a short TypeScript-like type, for example\n * `{ label: string; size?: \"sm\" | \"md\" }`.\n */\nexport function compactType(\n schema: GenerativeUIJSONSchema,\n depth: number,\n): string {\n const ref = childRefKind(schema);\n if (ref === \"single\") return \"componentId\";\n if (ref === \"multiple\")\n return \"componentId[] | { componentId: string; path: string }\";\n if (\"const\" in schema) return JSON.stringify(schema.const);\n if (Array.isArray(schema.enum)) {\n return schema.enum.map((value) => JSON.stringify(value)).join(\" | \");\n }\n const variants = [...schemaList(schema.anyOf), ...schemaList(schema.oneOf)];\n if (variants.length > 0) {\n return variants.map((variant) => compactType(variant, depth)).join(\" | \");\n }\n const types = Array.isArray(schema.type) ? schema.type : [schema.type];\n const summaries = types.map((type) => {\n switch (type) {\n case \"string\":\n case \"boolean\":\n case \"null\":\n return type;\n case \"number\":\n case \"integer\":\n return \"number\";\n case \"array\":\n return isRecord(schema.items)\n ? asArrayItem(compactType(schema.items, depth))\n : \"unknown[]\";\n case \"object\":\n return compactObject(schema, depth);\n default:\n return \"unknown\";\n }\n });\n return summaries.join(\" | \");\n}\n\n/** An object schema as `{ key: type; other?: type }`. */\nfunction compactObject(schema: GenerativeUIJSONSchema, depth: number) {\n const properties = isRecord(schema.properties) ? schema.properties : {};\n const entries = Object.entries(properties);\n if (entries.length === 0) {\n return isRecord(schema.additionalProperties)\n ? `Record<string, ${compactType(schema.additionalProperties, depth + 1)}>`\n : \"object\";\n }\n if (depth >= MAX_OBJECT_DEPTH) return \"object\";\n const required = Array.isArray(schema.required) ? schema.required : [];\n const fields = entries.map(([key, value]) => {\n const type = isRecord(value) ? compactType(value, depth + 1) : \"unknown\";\n return `${key}${required.includes(key) ? \"\" : \"?\"}: ${type}`;\n });\n return `{ ${fields.join(\"; \")} }`;\n}\n\n/** A schema as its type, with its description after `//`. */\nfunction describedType(schema: GenerativeUIJSONSchema, depth: number) {\n const type = compactType(schema, depth);\n return typeof schema.description === \"string\"\n ? `${type} // ${schema.description}`\n : type;\n}\n\n/**\n * The props of a component: `{ \"label\": \"string\", \"size?\": \"...\" }`. With\n * `descriptions`, each type carries its description after `//`.\n */\nexport function compactProps(\n schema: GenerativeUIJSONSchema,\n descriptions = true,\n) {\n const properties = isRecord(schema.properties) ? schema.properties : {};\n const required = Array.isArray(schema.required) ? schema.required : [];\n const props: Record<string, string> = {};\n for (const [key, value] of Object.entries(properties)) {\n if (!isRecord(value)) continue;\n const name = required.includes(key) ? key : `${key}?`;\n props[name] = descriptions\n ? describedType(value, 1)\n : compactType(value, 1);\n }\n return props;\n}\n\n/**\n * The prop set worth listing once. Candidates are (key, type) pairs that at\n * least {@link COMMON_PROP_MIN_COMPONENTS} components carry verbatim. The\n * candidate subset of each component is a possible common set, scored by\n * the characters it saves (set size times the components that contain all\n * of it). `undefined` when no set is shared by enough components.\n */\nfunction findCommonProps(propsList: Array<Record<string, string>>) {\n const pairKey = (key: string, type: string) => `${key}\\u0000${type}`;\n const counts = new Map<string, number>();\n for (const props of propsList) {\n for (const [key, type] of Object.entries(props)) {\n const id = pairKey(key, type);\n counts.set(id, (counts.get(id) ?? 0) + 1);\n }\n }\n let best: Record<string, string> | undefined;\n let bestScore = 0;\n const seen = new Set<string>();\n for (const props of propsList) {\n const subset = Object.entries(props).filter(\n ([key, type]) =>\n (counts.get(pairKey(key, type)) ?? 0) >= COMMON_PROP_MIN_COMPONENTS,\n );\n if (subset.length < 2) continue;\n const signature = subset\n .map(([key, type]) => pairKey(key, type))\n .join(\"\\n\");\n if (seen.has(signature)) continue;\n seen.add(signature);\n const members = propsList.filter((other) =>\n subset.every(([key, type]) => other[key] === type),\n ).length;\n if (members < COMMON_PROP_MIN_COMPONENTS) continue;\n const size = subset.reduce(\n (total, [key, type]) => total + key.length + type.length,\n 0,\n );\n const score = size * (members - 1);\n if (score > bestScore) {\n bestScore = score;\n best = Object.fromEntries(subset);\n }\n }\n return best;\n}\n\n/** One component of the compact schema. */\ninterface CompactComponent {\n description?: string;\n /** True when the component also takes every prop in `commonProps`. */\n includesCommonProps?: true;\n props: Record<string, string>;\n events?: Record<string, string>;\n slots?: Record<string, \"single\" | \"multiple\">;\n}\n\n/**\n * The component schema of the markdown transport, from the per-run\n * manifest. It is smaller than the manifest: each prop is one short type,\n * and the props that many components share verbatim (the layout props of\n * the containers) are listed once in `commonProps`; a component that takes\n * them is flagged `includesCommonProps`.\n * It has the catalog id, the targets with their descriptions and their\n * component lists, the functions with their parameters, the components\n * with their props, events and slots, and the surfaces of the thread. A surface lists the\n * values that the user set and the operations that the client dropped.\n *\n * @param manifest - The manifest from `buildGenerativeUIManifest`.\n * @param options - `propDescriptions` (default true) writes each prop's\n * description; `commonProps` (default true) lists shared props once.\n * @returns A JSON-ready object.\n *\n * @example\n * ```ts\n * const value = JSON.stringify(\n * buildCompactGenerativeUISchema(buildGenerativeUIManifest(runtime, threadId)),\n * );\n * ```\n */\nexport function buildCompactGenerativeUISchema(\n manifest: Pick<\n GenerativeUIManifest,\n \"catalogId\" | \"components\" | \"functions\" | \"targets\" | \"surfaces\"\n >,\n options: { propDescriptions?: boolean; commonProps?: boolean } = {},\n) {\n const { propDescriptions = true, commonProps = true } = options;\n const components: Record<string, CompactComponent> = {};\n for (const [name, component] of Object.entries(manifest.components)) {\n const events = Object.entries(component.events ?? {}).map(\n ([event, definition]) => [\n event,\n definition.description ?? compactType(definition.parameters, 0),\n ],\n );\n components[name] = {\n ...(component.description ? { description: component.description } : {}),\n props: compactProps(component.props, propDescriptions),\n ...(events.length > 0 ? { events: Object.fromEntries(events) } : {}),\n ...(component.slots ? { slots: component.slots } : {}),\n };\n }\n const common = commonProps\n ? findCommonProps(\n Object.values(components).map((component) => component.props),\n )\n : undefined;\n if (common) {\n const commonKeys = Object.keys(common);\n for (const component of Object.values(components)) {\n const includesCommon = commonKeys.every(\n (key) => component.props[key] === common[key],\n );\n if (!includesCommon) continue;\n component.includesCommonProps = true;\n component.props = Object.fromEntries(\n Object.entries(component.props).filter(([key]) => !(key in common)),\n );\n }\n }\n // A built-in function carries its short signature in the description of\n // its parameters (see `builtInFunctionManifests`); an app function gets\n // its JSON Schema made short. A function is for the UI unless it says\n // that the agent may call it too, or only the agent.\n const functions = Object.fromEntries(\n Object.entries(manifest.functions).map(([name, fn]) => {\n const signature = fn.parameters.description;\n const hasSignature =\n typeof signature === \"string\" &&\n signature.startsWith(\"{\") &&\n Object.keys(fn.parameters).length === 2;\n const callers = functionCallers(fn);\n return [\n name,\n {\n ...(fn.description ? { description: fn.description } : {}),\n parameters: hasSignature ? signature : compactType(fn.parameters, 0),\n ...(callers.agent && GENERATIVE_UI_AGENT_FUNCTION_CALLS\n ? { callers: callers.renderer ? \"UI or agent\" : \"agent only\" }\n : {}),\n },\n ];\n }),\n );\n return {\n catalogId: manifest.catalogId,\n targets: manifest.targets,\n functions,\n ...(common ? { commonProps: common } : {}),\n components,\n surfaces: manifest.surfaces,\n };\n}\n"],"mappings":"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"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { GenerativeUIManifest, GenerativeUISurfaceSummary, GenerativeUITarget } from "./types.cjs";
|
|
2
|
+
|
|
3
|
+
//#region src/generative-ui/compact-schema.d.ts
|
|
4
|
+
/** One component of the compact schema. */
|
|
5
|
+
interface CompactComponent {
|
|
6
|
+
description?: string;
|
|
7
|
+
/** True when the component also takes every prop in `commonProps`. */
|
|
8
|
+
includesCommonProps?: true;
|
|
9
|
+
props: Record<string, string>;
|
|
10
|
+
events?: Record<string, string>;
|
|
11
|
+
slots?: Record<string, "single" | "multiple">;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The component schema of the markdown transport, from the per-run
|
|
15
|
+
* manifest. It is smaller than the manifest: each prop is one short type,
|
|
16
|
+
* and the props that many components share verbatim (the layout props of
|
|
17
|
+
* the containers) are listed once in `commonProps`; a component that takes
|
|
18
|
+
* them is flagged `includesCommonProps`.
|
|
19
|
+
* It has the catalog id, the targets with their descriptions and their
|
|
20
|
+
* component lists, the functions with their parameters, the components
|
|
21
|
+
* with their props, events and slots, and the surfaces of the thread. A surface lists the
|
|
22
|
+
* values that the user set and the operations that the client dropped.
|
|
23
|
+
*
|
|
24
|
+
* @param manifest - The manifest from `buildGenerativeUIManifest`.
|
|
25
|
+
* @param options - `propDescriptions` (default true) writes each prop's
|
|
26
|
+
* description; `commonProps` (default true) lists shared props once.
|
|
27
|
+
* @returns A JSON-ready object.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const value = JSON.stringify(
|
|
32
|
+
* buildCompactGenerativeUISchema(buildGenerativeUIManifest(runtime, threadId)),
|
|
33
|
+
* );
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
declare function buildCompactGenerativeUISchema(manifest: Pick<GenerativeUIManifest, "catalogId" | "components" | "functions" | "targets" | "surfaces">, options?: {
|
|
37
|
+
propDescriptions?: boolean;
|
|
38
|
+
commonProps?: boolean;
|
|
39
|
+
}): {
|
|
40
|
+
components: Record<string, CompactComponent>;
|
|
41
|
+
surfaces: Record<string, GenerativeUISurfaceSummary>;
|
|
42
|
+
commonProps?: Record<string, string> | undefined;
|
|
43
|
+
catalogId: string;
|
|
44
|
+
targets: GenerativeUITarget[];
|
|
45
|
+
functions: {
|
|
46
|
+
[k: string]: {
|
|
47
|
+
callers?: string | undefined;
|
|
48
|
+
parameters: string;
|
|
49
|
+
description?: string | undefined;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
//#endregion
|
|
54
|
+
export { buildCompactGenerativeUISchema };
|
|
55
|
+
//# sourceMappingURL=compact-schema.d.cts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compact-schema.d.cts","names":[],"sources":["../../src/generative-ui/compact-schema.ts"],"mappings":";;;;UA2KU,gBAAA;EACR,WAAA;EAGO;EADP,mBAAA;EACA,KAAA,EAAO,MAAA;EACP,MAAA,GAAS,MAAA;EACT,KAAA,GAAQ,MAAA;AAAA;;AA0BV;;;;;;;;;;;;;;;;;;;;;;iBAAgB,8BAAA,CACd,QAAA,EAAU,IAAA,CACR,oBAAA,sEAGF,OAAA;EAAW,gBAAA;EAA4B,WAAA;AAAA"}
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { GenerativeUIManifest, GenerativeUISurfaceSummary, GenerativeUITarget } from "./types.mjs";
|
|
2
|
+
|
|
3
|
+
//#region src/generative-ui/compact-schema.d.ts
|
|
4
|
+
/** One component of the compact schema. */
|
|
5
|
+
interface CompactComponent {
|
|
6
|
+
description?: string;
|
|
7
|
+
/** True when the component also takes every prop in `commonProps`. */
|
|
8
|
+
includesCommonProps?: true;
|
|
9
|
+
props: Record<string, string>;
|
|
10
|
+
events?: Record<string, string>;
|
|
11
|
+
slots?: Record<string, "single" | "multiple">;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* The component schema of the markdown transport, from the per-run
|
|
15
|
+
* manifest. It is smaller than the manifest: each prop is one short type,
|
|
16
|
+
* and the props that many components share verbatim (the layout props of
|
|
17
|
+
* the containers) are listed once in `commonProps`; a component that takes
|
|
18
|
+
* them is flagged `includesCommonProps`.
|
|
19
|
+
* It has the catalog id, the targets with their descriptions and their
|
|
20
|
+
* component lists, the functions with their parameters, the components
|
|
21
|
+
* with their props, events and slots, and the surfaces of the thread. A surface lists the
|
|
22
|
+
* values that the user set and the operations that the client dropped.
|
|
23
|
+
*
|
|
24
|
+
* @param manifest - The manifest from `buildGenerativeUIManifest`.
|
|
25
|
+
* @param options - `propDescriptions` (default true) writes each prop's
|
|
26
|
+
* description; `commonProps` (default true) lists shared props once.
|
|
27
|
+
* @returns A JSON-ready object.
|
|
28
|
+
*
|
|
29
|
+
* @example
|
|
30
|
+
* ```ts
|
|
31
|
+
* const value = JSON.stringify(
|
|
32
|
+
* buildCompactGenerativeUISchema(buildGenerativeUIManifest(runtime, threadId)),
|
|
33
|
+
* );
|
|
34
|
+
* ```
|
|
35
|
+
*/
|
|
36
|
+
declare function buildCompactGenerativeUISchema(manifest: Pick<GenerativeUIManifest, "catalogId" | "components" | "functions" | "targets" | "surfaces">, options?: {
|
|
37
|
+
propDescriptions?: boolean;
|
|
38
|
+
commonProps?: boolean;
|
|
39
|
+
}): {
|
|
40
|
+
components: Record<string, CompactComponent>;
|
|
41
|
+
surfaces: Record<string, GenerativeUISurfaceSummary>;
|
|
42
|
+
commonProps?: Record<string, string> | undefined;
|
|
43
|
+
catalogId: string;
|
|
44
|
+
targets: GenerativeUITarget[];
|
|
45
|
+
functions: {
|
|
46
|
+
[k: string]: {
|
|
47
|
+
callers?: string | undefined;
|
|
48
|
+
parameters: string;
|
|
49
|
+
description?: string | undefined;
|
|
50
|
+
};
|
|
51
|
+
};
|
|
52
|
+
};
|
|
53
|
+
//#endregion
|
|
54
|
+
export { buildCompactGenerativeUISchema };
|
|
55
|
+
//# sourceMappingURL=compact-schema.d.mts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"compact-schema.d.mts","names":[],"sources":["../../src/generative-ui/compact-schema.ts"],"mappings":";;;;UA2KU,gBAAA;EACR,WAAA;EAGO;EADP,mBAAA;EACA,KAAA,EAAO,MAAA;EACP,MAAA,GAAS,MAAA;EACT,KAAA,GAAQ,MAAA;AAAA;;AA0BV;;;;;;;;;;;;;;;;;;;;;;iBAAgB,8BAAA,CACd,QAAA,EAAU,IAAA,CACR,oBAAA,sEAGF,OAAA;EAAW,gBAAA;EAA4B,WAAA;AAAA"}
|