@assistant-ui/core 0.2.15 → 0.2.16
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.
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ToolCallMessagePartComponent } from "../types/MessagePartComponentTypes.js";
|
|
1
|
+
import { ToolCallMessagePartComponent, ToolCallMessagePartProps } from "../types/MessagePartComponentTypes.js";
|
|
2
2
|
import { Tool, ToolCallReader, ToolDeclaration, ToolModelOutputFunction } from "assistant-stream";
|
|
3
3
|
import { ReactNode } from "react";
|
|
4
4
|
|
|
@@ -46,7 +46,7 @@ type ToolCallText<TArgs extends Record<string, unknown>, TResult> = {
|
|
|
46
46
|
running?: ToolCallRunningText<TArgs> | undefined;
|
|
47
47
|
complete: ToolCallCompleteText<TArgs, TResult>;
|
|
48
48
|
};
|
|
49
|
-
declare const makeToolCallTextComponent: <TArgs extends Record<string, unknown>, TResult>(text: ToolCallText<TArgs, TResult>) =>
|
|
49
|
+
declare const makeToolCallTextComponent: <TArgs extends Record<string, unknown>, TResult>(text: ToolCallText<TArgs, TResult>) => ((part: ToolCallMessagePartProps<TArgs, TResult>) => ReactNode);
|
|
50
50
|
/**
|
|
51
51
|
* Tool definition accepted by the React tool registry.
|
|
52
52
|
*
|
|
@@ -89,7 +89,7 @@ type OverrideToolDeclarationCallbacks<T extends {
|
|
|
89
89
|
streamCall?: unknown;
|
|
90
90
|
}, TArgs extends Record<string, unknown>, TResult> = Omit<T, "type" | "execute" | "toModelOutput" | "experimental_onSchemaValidationError" | "streamCall"> & {
|
|
91
91
|
type?: never;
|
|
92
|
-
} & ("execute" extends keyof T ? OverrideOptionalField<T, "execute", ToolExecute<NoInfer<TArgs>, TResult>> : {}) & ("toModelOutput" extends keyof T ? OverrideOptionalField<T, "toModelOutput", ToolModelOutputFunction<NoInfer<TArgs>, NoInfer<TResult>>> : {}) & ("experimental_onSchemaValidationError" extends keyof T ? OverrideOptionalField<T, "experimental_onSchemaValidationError", (args: unknown, context: ToolExecuteContext) => NoInfer<TResult> | Promise<NoInfer<TResult>>> : {}) & OverrideOptionalField<T, "streamCall", ToolStreamCall<TArgs,
|
|
92
|
+
} & ("execute" extends keyof T ? OverrideOptionalField<T, "execute", ToolExecute<NoInfer<TArgs>, TResult>> : {}) & ("toModelOutput" extends keyof T ? OverrideOptionalField<T, "toModelOutput", ToolModelOutputFunction<NoInfer<TArgs>, NoInfer<TResult>>> : {}) & ("experimental_onSchemaValidationError" extends keyof T ? OverrideOptionalField<T, "experimental_onSchemaValidationError", (args: unknown, context: ToolExecuteContext) => NoInfer<TResult> | Promise<NoInfer<TResult>>> : {}) & OverrideOptionalField<T, "streamCall", ToolStreamCall<TArgs, unknown>>;
|
|
93
93
|
type ToolkitDefinitionInput<TArgs extends Record<string, unknown>, TResult> = WithRender<ToolDeclaration<TArgs, TResult> extends infer T ? T extends {
|
|
94
94
|
streamCall?: unknown;
|
|
95
95
|
} ? OverrideToolDeclarationCallbacks<T, TArgs, TResult> : never : never, TArgs, TResult>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toolbox.d.ts","names":[],"sources":["../../../src/react/model-context/toolbox.ts"],"mappings":";;;;;;;AAqBA;;;;;;;cAAa,uBAAA,GACX,IAAA,EAAM,IAAI,CAAC,IAAA;AAAA,KAMR,UAAA,kBAA4B,MAAA,8BAAoC,CAAA;EACnE,IAAA;AAAA,IAEE,CAAA,IACG,CAAA;EAAY,IAAA;AAAA;EAEH,MAAA,EAAQ,4BAAA,CAA6B,KAAA,EAAO,OAAA;AAAA;EAE5C,MAAA,GAAS,4BAAA,CAA6B,KAAA,EAAO,OAAA;EAC7C,UAAA,EAAY,YAAA,CAAa,KAAA,EAAO,OAAA;AAAA;EAEpC,MAAA,EAAQ,4BAAA,CAA6B,KAAA,EAAO,OAAA;AAAA,KACpD,CAAA;EACE,MAAA,GAAS,4BAAA,CAA6B,KAAA,EAAO,OAAA;EAC7C,UAAA,GAAa,YAAA,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CAAY,cAAA,CAAe,KAAA;AAAA;;;;;;KAQ7B,iBAAA,mCAEI,WAAA,GAAc,MAAA,sBACxB,MAAA,mDACgC,WAAA,6BACtB,WAAA,0BAGF,WAAA,GAAc,sBAAA,CACxB,WAAA,CAAY,CAAA,GACZ,aAAA,CAAc,CAAA;;KAKN,WAAA;EArMP,kFAuMH,OAAA,EAAS,OAAO;AAAA"}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"toolbox.js","names":["Tool","ToolCallReader","ToolDeclaration","ToolModelOutputFunction","ReactNode","ToolCallMessagePartComponent","ToolCallMessagePartProps","isStandaloneToolDisplay","tool","Pick","display","undefined","type","WithRender","Record","T","render","TArgs","TResult","renderText","ToolCallText","ToolParameters","ToolExecuteContext","Parameters","NonNullable","ToolExecute","args","context","Promise","ToolStreamCall","reader","ToolCallRunningText","options","ToolCallCompleteText","result","running","complete","resolveToolCallText","text","part","isRunning","status","value","makeToolCallTextComponent","ToolCallTextComponent","ToolDefinition","Toolkit","OverrideOptionalField","TKey","Exclude","TValue","OverrideToolDeclarationCallbacks","streamCall","Omit","NoInfer","ToolkitDefinitionInput","ToolkitDefinitionEntry","ToolkitDefinitionEntryWithParameters","parameters","ToolkitDefinition","TArgsByName","K","TResultByName","ToolsConfig","toolkit"],"sources":["../../../src/react/model-context/toolbox.ts"],"sourcesContent":["import type {\n Tool,\n ToolCallReader,\n ToolDeclaration,\n ToolModelOutputFunction,\n} from \"assistant-stream\";\nimport type { ReactNode } from \"react\";\nimport type {\n ToolCallMessagePartComponent,\n ToolCallMessagePartProps,\n} from \"../types/MessagePartComponentTypes\";\n\n/**\n * Resolves whether a tool's UI should be presented standalone (outside the\n * chain-of-thought grouping), applying the type-based defaults.\n *\n * An explicit `display` wins. Otherwise `human` tools default to standalone\n * (they prompt the user), and every other tool defaults to inline (a trace of\n * what the model is doing). MCP-app tool calls are detected separately from\n * the part itself and are not resolved here.\n */\nexport const isStandaloneToolDisplay = (\n tool: Pick<Tool<any, any>, \"type\" | \"display\">,\n): boolean => {\n if (tool.display !== undefined) return tool.display === \"standalone\";\n return tool.type === \"human\";\n};\n\ntype WithRender<T, TArgs extends Record<string, unknown>, TResult> = T extends {\n type: \"frontend\" | \"human\";\n}\n ? T &\n (T extends { type: \"frontend\" }\n ?\n | { render: ToolCallMessagePartComponent<TArgs, TResult> }\n | {\n render?: ToolCallMessagePartComponent<TArgs, TResult>;\n renderText: ToolCallText<TArgs, TResult>;\n }\n : { render: ToolCallMessagePartComponent<TArgs, TResult> })\n : T & {\n render?: ToolCallMessagePartComponent<TArgs, TResult> | undefined;\n renderText?: ToolCallText<TArgs, TResult> | undefined;\n };\n\ntype ToolParameters<TArgs extends Record<string, unknown>> =\n ToolDeclaration<TArgs>[\"parameters\"];\n\n// ToolExecutionContext is not re-exported from assistant-stream's public entry.\ntype ToolExecuteContext = Parameters<\n NonNullable<ToolDeclaration[\"execute\"]>\n>[1];\n\ntype ToolExecute<TArgs extends Record<string, unknown>, TResult> = (\n args: TArgs,\n context: ToolExecuteContext,\n) => TResult | Promise<TResult>;\n\ntype ToolStreamCall<TArgs extends Record<string, unknown>, TResult> = (\n reader: ToolCallReader<TArgs, TResult>,\n context: ToolExecuteContext,\n) => void;\n\ntype ToolCallRunningText<TArgs extends Record<string, unknown>> =\n | ReactNode\n | ((options: { args: TArgs }) => ReactNode);\n\ntype ToolCallCompleteText<TArgs extends Record<string, unknown>, TResult> =\n | ReactNode\n | ((options: { args: TArgs; result: TResult | undefined }) => ReactNode);\n\nexport type ToolCallText<TArgs extends Record<string, unknown>, TResult> =\n | {\n running: ToolCallRunningText<TArgs>;\n complete?: ToolCallCompleteText<TArgs, TResult> | undefined;\n }\n | {\n running?: ToolCallRunningText<TArgs> | undefined;\n complete: ToolCallCompleteText<TArgs, TResult>;\n };\n\nconst resolveToolCallText = <TArgs extends Record<string, unknown>, TResult>(\n text: ToolCallText<TArgs, TResult>,\n part: ToolCallMessagePartProps<TArgs, TResult>,\n): ReactNode => {\n const isRunning =\n part.status?.type === \"running\" || part.status?.type === \"requires-action\";\n\n if (!isRunning) {\n const value = text.complete;\n if (typeof value !== \"function\") return value ?? null;\n return value({ args: part.args, result: part.result });\n }\n\n const value = text.running;\n if (typeof value !== \"function\") return value ?? null;\n return value({ args: part.args });\n};\n\nexport const makeToolCallTextComponent = <\n TArgs extends Record<string, unknown>,\n TResult,\n>(\n text: ToolCallText<TArgs, TResult>,\n): ToolCallMessagePartComponent<TArgs, TResult> => {\n return function ToolCallTextComponent(part) {\n return resolveToolCallText(text, part);\n };\n};\n\n/**\n * Tool definition accepted by the React tool registry.\n *\n * Extends the core tool contract with tool-call display options. Human tools\n * rely on `render` to collect input from the user. Frontend tools execute in\n * the browser and require either `render` or `renderText` for their progress\n * and result. Backend tools execute server-side and may omit a renderer.\n */\nexport type ToolDefinition<\n TArgs extends Record<string, unknown> = Record<string, unknown>,\n TResult = unknown,\n> = WithRender<Tool<TArgs, TResult>, TArgs, TResult>;\n\n/**\n * Named collection of tools exposed to the assistant model.\n *\n * Keys are the tool names the model receives and uses in tool calls.\n *\n * @example\n * ```tsx\n * const toolkit = defineToolkit({\n * get_weather: {\n * type: \"frontend\",\n * description: \"Get the weather for a city.\",\n * parameters: weatherSchema,\n * execute: async ({ city }: { city: string }) => fetchWeather(city),\n * render: WeatherToolUI,\n * },\n * });\n * ```\n */\nexport type Toolkit = Record<string, ToolDefinition<any, any>>;\n\n/**\n * A tool as authored, before the build splits it: like {@link ToolDefinition}\n * but it may declare `description`, `parameters`, and a server-side `execute`\n * alongside its `render`. The `type` field is **not** authored — the\n * `\"use generative\"` compiler infers it (`execute: humanTool()` → human;\n * `execute: providerTool(...)` → provider; `execute` with a `\"use client\"`\n * directive → frontend; otherwise backend) and writes it back — so declaring it\n * here is a type error.\n */\ntype OverrideOptionalField<\n T,\n TKey extends keyof T,\n TValue,\n> = undefined extends T[TKey]\n ? // Preserve `?: undefined` fields (for variants that explicitly disallow a\n // callback) instead of widening them to accept the override value.\n Exclude<T[TKey], undefined> extends never\n ? { [K in TKey]?: undefined }\n : { [K in TKey]?: TValue | undefined }\n : { [K in TKey]: TValue };\n\ntype OverrideToolDeclarationCallbacks<\n T extends { streamCall?: unknown },\n TArgs extends Record<string, unknown>,\n TResult,\n> = Omit<\n T,\n | \"type\"\n | \"execute\"\n | \"toModelOutput\"\n | \"experimental_onSchemaValidationError\"\n | \"streamCall\"\n> & {\n type?: never;\n} & (\"execute\" extends keyof T\n ? OverrideOptionalField<T, \"execute\", ToolExecute<NoInfer<TArgs>, TResult>>\n : {}) &\n (\"toModelOutput\" extends keyof T\n ? OverrideOptionalField<\n T,\n \"toModelOutput\",\n ToolModelOutputFunction<NoInfer<TArgs>, NoInfer<TResult>>\n >\n : {}) &\n (\"experimental_onSchemaValidationError\" extends keyof T\n ? OverrideOptionalField<\n T,\n \"experimental_onSchemaValidationError\",\n (\n args: unknown,\n context: ToolExecuteContext,\n ) => NoInfer<TResult> | Promise<NoInfer<TResult>>\n >\n : {}) &\n OverrideOptionalField<\n T,\n \"streamCall\",\n ToolStreamCall<TArgs, NoInfer<TResult>>\n >;\n\n// Keep the authored shape tied to ToolDeclaration's union variants while\n// overriding callback fields to avoid inference pollution.\ntype ToolkitDefinitionInput<\n TArgs extends Record<string, unknown>,\n TResult,\n> = WithRender<\n ToolDeclaration<TArgs, TResult> extends infer T\n ? T extends { streamCall?: unknown }\n ? OverrideToolDeclarationCallbacks<T, TArgs, TResult>\n : never\n : never,\n TArgs,\n TResult\n>;\n\n/**\n * A single entry in a {@link ToolkitDefinition}.\n *\n * Either authored inline (whose `type` the compiler infers) or an already-formed\n * {@link ToolDefinition} produced by a factory whose own build splits it across\n * targets — e.g. `new JSONGenerativeUI({ library }).present()`. The factory case\n * carries a `type`, so it can only match the {@link ToolDefinition} arm of this\n * union.\n */\nexport type ToolkitDefinitionEntry<\n TArgs extends Record<string, unknown> = Record<string, unknown>,\n TResult = unknown,\n> = ToolkitDefinitionInput<TArgs, TResult> | ToolDefinition<any, any>;\n\nexport type ToolkitDefinitionEntryWithParameters<\n TArgs extends Record<string, unknown> = Record<string, unknown>,\n TResult = unknown,\n> = ToolkitDefinitionInput<TArgs, TResult> & {\n parameters: NonNullable<ToolParameters<TArgs>>;\n};\n\n/**\n * The permissive, authoring-time counterpart to {@link Toolkit} — the input to\n * {@link defineToolkit}. Backend entries may carry their server `execute` here;\n * the canonical {@link Toolkit} keeps those fields `undefined`.\n */\nexport type ToolkitDefinition<\n TArgsByName extends {\n [K in keyof TArgsByName]: Record<string, unknown>;\n } = Record<string, any>,\n TResultByName extends { [K in keyof TArgsByName]: unknown } = {\n [K in keyof TArgsByName]: any;\n },\n> = {\n [K in keyof TArgsByName]: ToolkitDefinitionEntry<\n TArgsByName[K],\n TResultByName[K]\n >;\n};\n\n/** Configuration for the {@link Tools} resource. */\nexport type ToolsConfig = {\n /** Tools to register with model context and, when provided, message renderers. */\n toolkit: Toolkit;\n};\n"],"mappings":";;;;;;;;;;AAqBA,MAAaO,2BACXC,SACY;CACZ,IAAIA,KAAKE,YAAYC,KAAAA,GAAW,OAAOH,KAAKE,YAAY;CACxD,OAAOF,KAAKI,SAAS;AACvB;AAuDA,MAAMyB,uBACJC,MACAC,SACc;CAId,IAAI,EAFFA,KAAKE,QAAQ7B,SAAS,aAAa2B,KAAKE,QAAQ7B,SAAS,oBAE3C;EACd,MAAM8B,QAAQJ,KAAKF;EACnB,IAAI,OAAOM,UAAU,YAAY,OAAOA,SAAS;EACjD,OAAOA,MAAM;GAAEhB,MAAMa,KAAKb;GAAMQ,QAAQK,KAAKL;EAAO,CAAC;CACvD;CAEA,MAAMQ,QAAQJ,KAAKH;CACnB,IAAI,OAAOO,UAAU,YAAY,OAAOA,SAAS;CACjD,OAAOA,MAAM,EAAEhB,MAAMa,KAAKb,KAAK,CAAC;AAClC;AAEA,MAAaiB,6BAIXL,SACiD;CACjD,OAAO,SAASM,sBAAsBL,MAAM;EAC1C,OAAOF,oBAAoBC,MAAMC,IAAI;CACvC;AACF"}
|
|
1
|
+
{"version":3,"file":"toolbox.js","names":["Tool","ToolCallReader","ToolDeclaration","ToolModelOutputFunction","ReactNode","ToolCallMessagePartComponent","ToolCallMessagePartProps","isStandaloneToolDisplay","tool","Pick","display","undefined","type","WithRender","Record","T","render","TArgs","TResult","renderText","ToolCallText","ToolParameters","ToolExecuteContext","Parameters","NonNullable","ToolExecute","args","context","Promise","ToolStreamCall","reader","ToolCallRunningText","options","ToolCallCompleteText","result","running","complete","resolveToolCallText","text","part","isRunning","status","value","makeToolCallTextComponent","ToolCallTextComponent","ToolDefinition","Toolkit","OverrideOptionalField","TKey","Exclude","TValue","OverrideToolDeclarationCallbacks","streamCall","Omit","NoInfer","ToolkitDefinitionInput","ToolkitDefinitionEntry","ToolkitDefinitionEntryWithParameters","parameters","ToolkitDefinition","TArgsByName","K","TResultByName","ToolsConfig","toolkit"],"sources":["../../../src/react/model-context/toolbox.ts"],"sourcesContent":["import type {\n Tool,\n ToolCallReader,\n ToolDeclaration,\n ToolModelOutputFunction,\n} from \"assistant-stream\";\nimport type { ReactNode } from \"react\";\nimport type {\n ToolCallMessagePartComponent,\n ToolCallMessagePartProps,\n} from \"../types/MessagePartComponentTypes\";\n\n/**\n * Resolves whether a tool's UI should be presented standalone (outside the\n * chain-of-thought grouping), applying the type-based defaults.\n *\n * An explicit `display` wins. Otherwise `human` tools default to standalone\n * (they prompt the user), and every other tool defaults to inline (a trace of\n * what the model is doing). MCP-app tool calls are detected separately from\n * the part itself and are not resolved here.\n */\nexport const isStandaloneToolDisplay = (\n tool: Pick<Tool<any, any>, \"type\" | \"display\">,\n): boolean => {\n if (tool.display !== undefined) return tool.display === \"standalone\";\n return tool.type === \"human\";\n};\n\ntype WithRender<T, TArgs extends Record<string, unknown>, TResult> = T extends {\n type: \"frontend\" | \"human\";\n}\n ? T &\n (T extends { type: \"frontend\" }\n ?\n | { render: ToolCallMessagePartComponent<TArgs, TResult> }\n | {\n render?: ToolCallMessagePartComponent<TArgs, TResult>;\n renderText: ToolCallText<TArgs, TResult>;\n }\n : { render: ToolCallMessagePartComponent<TArgs, TResult> })\n : T & {\n render?: ToolCallMessagePartComponent<TArgs, TResult> | undefined;\n renderText?: ToolCallText<TArgs, TResult> | undefined;\n };\n\ntype ToolParameters<TArgs extends Record<string, unknown>> =\n ToolDeclaration<TArgs>[\"parameters\"];\n\n// ToolExecutionContext is not re-exported from assistant-stream's public entry.\ntype ToolExecuteContext = Parameters<\n NonNullable<ToolDeclaration[\"execute\"]>\n>[1];\n\ntype ToolExecute<TArgs extends Record<string, unknown>, TResult> = (\n args: TArgs,\n context: ToolExecuteContext,\n) => TResult | Promise<TResult>;\n\ntype ToolStreamCall<TArgs extends Record<string, unknown>, TResult> = (\n reader: ToolCallReader<TArgs, TResult>,\n context: ToolExecuteContext,\n) => void;\n\ntype ToolCallRunningText<TArgs extends Record<string, unknown>> =\n | ReactNode\n | ((options: { args: TArgs }) => ReactNode);\n\ntype ToolCallCompleteText<TArgs extends Record<string, unknown>, TResult> =\n | ReactNode\n | ((options: { args: TArgs; result: TResult | undefined }) => ReactNode);\n\nexport type ToolCallText<TArgs extends Record<string, unknown>, TResult> =\n | {\n running: ToolCallRunningText<TArgs>;\n complete?: ToolCallCompleteText<TArgs, TResult> | undefined;\n }\n | {\n running?: ToolCallRunningText<TArgs> | undefined;\n complete: ToolCallCompleteText<TArgs, TResult>;\n };\n\nconst resolveToolCallText = <TArgs extends Record<string, unknown>, TResult>(\n text: ToolCallText<TArgs, TResult>,\n part: ToolCallMessagePartProps<TArgs, TResult>,\n): ReactNode => {\n const isRunning =\n part.status?.type === \"running\" || part.status?.type === \"requires-action\";\n\n if (!isRunning) {\n const value = text.complete;\n if (typeof value !== \"function\") return value ?? null;\n return value({ args: part.args, result: part.result });\n }\n\n const value = text.running;\n if (typeof value !== \"function\") return value ?? null;\n return value({ args: part.args });\n};\n\nexport const makeToolCallTextComponent = <\n TArgs extends Record<string, unknown>,\n TResult,\n>(\n text: ToolCallText<TArgs, TResult>,\n): ((part: ToolCallMessagePartProps<TArgs, TResult>) => ReactNode) => {\n return function ToolCallTextComponent(part) {\n return resolveToolCallText(text, part);\n };\n};\n\n/**\n * Tool definition accepted by the React tool registry.\n *\n * Extends the core tool contract with tool-call display options. Human tools\n * rely on `render` to collect input from the user. Frontend tools execute in\n * the browser and require either `render` or `renderText` for their progress\n * and result. Backend tools execute server-side and may omit a renderer.\n */\nexport type ToolDefinition<\n TArgs extends Record<string, unknown> = Record<string, unknown>,\n TResult = unknown,\n> = WithRender<Tool<TArgs, TResult>, TArgs, TResult>;\n\n/**\n * Named collection of tools exposed to the assistant model.\n *\n * Keys are the tool names the model receives and uses in tool calls.\n *\n * @example\n * ```tsx\n * const toolkit = defineToolkit({\n * get_weather: {\n * type: \"frontend\",\n * description: \"Get the weather for a city.\",\n * parameters: weatherSchema,\n * execute: async ({ city }: { city: string }) => fetchWeather(city),\n * render: WeatherToolUI,\n * },\n * });\n * ```\n */\nexport type Toolkit = Record<string, ToolDefinition<any, any>>;\n\n/**\n * A tool as authored, before the build splits it: like {@link ToolDefinition}\n * but it may declare `description`, `parameters`, and a server-side `execute`\n * alongside its `render`. The `type` field is **not** authored — the\n * `\"use generative\"` compiler infers it (`execute: humanTool()` → human;\n * `execute: providerTool(...)` → provider; `execute` with a `\"use client\"`\n * directive → frontend; otherwise backend) and writes it back — so declaring it\n * here is a type error.\n */\ntype OverrideOptionalField<\n T,\n TKey extends keyof T,\n TValue,\n> = undefined extends T[TKey]\n ? // Preserve `?: undefined` fields (for variants that explicitly disallow a\n // callback) instead of widening them to accept the override value.\n Exclude<T[TKey], undefined> extends never\n ? { [K in TKey]?: undefined }\n : { [K in TKey]?: TValue | undefined }\n : { [K in TKey]: TValue };\n\ntype OverrideToolDeclarationCallbacks<\n T extends { streamCall?: unknown },\n TArgs extends Record<string, unknown>,\n TResult,\n> = Omit<\n T,\n | \"type\"\n | \"execute\"\n | \"toModelOutput\"\n | \"experimental_onSchemaValidationError\"\n | \"streamCall\"\n> & {\n type?: never;\n} & (\"execute\" extends keyof T\n ? OverrideOptionalField<T, \"execute\", ToolExecute<NoInfer<TArgs>, TResult>>\n : {}) &\n (\"toModelOutput\" extends keyof T\n ? OverrideOptionalField<\n T,\n \"toModelOutput\",\n ToolModelOutputFunction<NoInfer<TArgs>, NoInfer<TResult>>\n >\n : {}) &\n (\"experimental_onSchemaValidationError\" extends keyof T\n ? OverrideOptionalField<\n T,\n \"experimental_onSchemaValidationError\",\n (\n args: unknown,\n context: ToolExecuteContext,\n ) => NoInfer<TResult> | Promise<NoInfer<TResult>>\n >\n : {}) &\n OverrideOptionalField<T, \"streamCall\", ToolStreamCall<TArgs, unknown>>;\n\n// Keep the authored shape tied to ToolDeclaration's union variants while\n// overriding callback fields to avoid inference pollution.\ntype ToolkitDefinitionInput<\n TArgs extends Record<string, unknown>,\n TResult,\n> = WithRender<\n ToolDeclaration<TArgs, TResult> extends infer T\n ? T extends { streamCall?: unknown }\n ? OverrideToolDeclarationCallbacks<T, TArgs, TResult>\n : never\n : never,\n TArgs,\n TResult\n>;\n\n/**\n * A single entry in a {@link ToolkitDefinition}.\n *\n * Either authored inline (whose `type` the compiler infers) or an already-formed\n * {@link ToolDefinition} produced by a factory whose own build splits it across\n * targets — e.g. `new JSONGenerativeUI({ library }).present()`. The factory case\n * carries a `type`, so it can only match the {@link ToolDefinition} arm of this\n * union.\n */\nexport type ToolkitDefinitionEntry<\n TArgs extends Record<string, unknown> = Record<string, unknown>,\n TResult = unknown,\n> = ToolkitDefinitionInput<TArgs, TResult> | ToolDefinition<any, any>;\n\nexport type ToolkitDefinitionEntryWithParameters<\n TArgs extends Record<string, unknown> = Record<string, unknown>,\n TResult = unknown,\n> = ToolkitDefinitionInput<TArgs, TResult> & {\n parameters: NonNullable<ToolParameters<TArgs>>;\n};\n\n/**\n * The permissive, authoring-time counterpart to {@link Toolkit} — the input to\n * {@link defineToolkit}. Backend entries may carry their server `execute` here;\n * the canonical {@link Toolkit} keeps those fields `undefined`.\n */\nexport type ToolkitDefinition<\n TArgsByName extends {\n [K in keyof TArgsByName]: Record<string, unknown>;\n } = Record<string, any>,\n TResultByName extends { [K in keyof TArgsByName]: unknown } = {\n [K in keyof TArgsByName]: any;\n },\n> = {\n [K in keyof TArgsByName]: ToolkitDefinitionEntry<\n TArgsByName[K],\n TResultByName[K]\n >;\n};\n\n/** Configuration for the {@link Tools} resource. */\nexport type ToolsConfig = {\n /** Tools to register with model context and, when provided, message renderers. */\n toolkit: Toolkit;\n};\n"],"mappings":";;;;;;;;;;AAqBA,MAAaO,2BACXC,SACY;CACZ,IAAIA,KAAKE,YAAYC,KAAAA,GAAW,OAAOH,KAAKE,YAAY;CACxD,OAAOF,KAAKI,SAAS;AACvB;AAuDA,MAAMyB,uBACJC,MACAC,SACc;CAId,IAAI,EAFFA,KAAKE,QAAQ7B,SAAS,aAAa2B,KAAKE,QAAQ7B,SAAS,oBAE3C;EACd,MAAM8B,QAAQJ,KAAKF;EACnB,IAAI,OAAOM,UAAU,YAAY,OAAOA,SAAS;EACjD,OAAOA,MAAM;GAAEhB,MAAMa,KAAKb;GAAMQ,QAAQK,KAAKL;EAAO,CAAC;CACvD;CAEA,MAAMQ,QAAQJ,KAAKH;CACnB,IAAI,OAAOO,UAAU,YAAY,OAAOA,SAAS;CACjD,OAAOA,MAAM,EAAEhB,MAAMa,KAAKb,KAAK,CAAC;AAClC;AAEA,MAAaiB,6BAIXL,SACoE;CACpE,OAAO,SAASM,sBAAsBL,MAAM;EAC1C,OAAOF,oBAAoBC,MAAMC,IAAI;CACvC;AACF"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@assistant-ui/core",
|
|
3
|
-
"version": "0.2.
|
|
3
|
+
"version": "0.2.16",
|
|
4
4
|
"description": "Framework-agnostic core runtime for assistant-ui",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"assistant",
|
|
@@ -59,7 +59,7 @@
|
|
|
59
59
|
],
|
|
60
60
|
"sideEffects": false,
|
|
61
61
|
"dependencies": {
|
|
62
|
-
"assistant-stream": "^0.3.
|
|
62
|
+
"assistant-stream": "^0.3.23",
|
|
63
63
|
"nanoid": "^5.1.11"
|
|
64
64
|
},
|
|
65
65
|
"optionalDevDependencies": {
|
|
@@ -67,7 +67,7 @@
|
|
|
67
67
|
},
|
|
68
68
|
"peerDependencies": {
|
|
69
69
|
"@assistant-ui/store": "^0.2.13",
|
|
70
|
-
"@assistant-ui/tap": "^0.
|
|
70
|
+
"@assistant-ui/tap": "^0.9.0",
|
|
71
71
|
"@types/react": "*",
|
|
72
72
|
"react": "^18 || ^19",
|
|
73
73
|
"assistant-cloud": "^0.1.31",
|
|
@@ -92,11 +92,11 @@
|
|
|
92
92
|
"react": "^19.2.7",
|
|
93
93
|
"vitest": "^4.1.8",
|
|
94
94
|
"zustand": "^5.0.14",
|
|
95
|
-
"@assistant-ui/store": "0.2.
|
|
96
|
-
"@assistant-ui/tap": "0.
|
|
97
|
-
"@assistant-ui/vite": "0.0.
|
|
98
|
-
"@assistant-ui/x-buildutils": "0.0.
|
|
99
|
-
"assistant-cloud": "0.1.
|
|
95
|
+
"@assistant-ui/store": "0.2.18",
|
|
96
|
+
"@assistant-ui/tap": "0.9.1",
|
|
97
|
+
"@assistant-ui/vite": "0.0.5",
|
|
98
|
+
"@assistant-ui/x-buildutils": "0.0.14",
|
|
99
|
+
"assistant-cloud": "0.1.33"
|
|
100
100
|
},
|
|
101
101
|
"publishConfig": {
|
|
102
102
|
"access": "public",
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
|
+
import type { ToolCallMessagePartProps } from "../types/MessagePartComponentTypes";
|
|
2
3
|
import { makeToolCallTextComponent } from "./toolbox";
|
|
3
4
|
|
|
4
5
|
describe("makeToolCallTextComponent", () => {
|
|
@@ -94,7 +95,7 @@ describe("makeToolCallTextComponent", () => {
|
|
|
94
95
|
addResult: () => {},
|
|
95
96
|
resume: () => {},
|
|
96
97
|
respondToApproval: () => {},
|
|
97
|
-
}),
|
|
98
|
+
} as unknown as ToolCallMessagePartProps),
|
|
98
99
|
).toBe("Complete");
|
|
99
100
|
});
|
|
100
101
|
|
|
@@ -102,7 +102,7 @@ export const makeToolCallTextComponent = <
|
|
|
102
102
|
TResult,
|
|
103
103
|
>(
|
|
104
104
|
text: ToolCallText<TArgs, TResult>,
|
|
105
|
-
):
|
|
105
|
+
): ((part: ToolCallMessagePartProps<TArgs, TResult>) => ReactNode) => {
|
|
106
106
|
return function ToolCallTextComponent(part) {
|
|
107
107
|
return resolveToolCallText(text, part);
|
|
108
108
|
};
|
|
@@ -195,11 +195,7 @@ type OverrideToolDeclarationCallbacks<
|
|
|
195
195
|
) => NoInfer<TResult> | Promise<NoInfer<TResult>>
|
|
196
196
|
>
|
|
197
197
|
: {}) &
|
|
198
|
-
OverrideOptionalField<
|
|
199
|
-
T,
|
|
200
|
-
"streamCall",
|
|
201
|
-
ToolStreamCall<TArgs, NoInfer<TResult>>
|
|
202
|
-
>;
|
|
198
|
+
OverrideOptionalField<T, "streamCall", ToolStreamCall<TArgs, unknown>>;
|
|
203
199
|
|
|
204
200
|
// Keep the authored shape tied to ToolDeclaration's union variants while
|
|
205
201
|
// overriding callback fields to avoid inference pollution.
|