@tanstack/ai 0.1.0 → 0.2.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/README.md +0 -25
- package/dist/esm/activities/chat/index.d.ts +11 -10
- package/dist/esm/activities/chat/index.js +9 -9
- package/dist/esm/activities/chat/index.js.map +1 -1
- package/dist/esm/activities/chat/tools/schema-converter.d.ts +116 -0
- package/dist/esm/activities/chat/tools/schema-converter.js +115 -0
- package/dist/esm/activities/chat/tools/schema-converter.js.map +1 -0
- package/dist/esm/activities/chat/tools/tool-calls.js +23 -34
- package/dist/esm/activities/chat/tools/tool-calls.js.map +1 -1
- package/dist/esm/activities/chat/tools/tool-definition.d.ts +18 -14
- package/dist/esm/activities/chat/tools/tool-definition.js.map +1 -1
- package/dist/esm/index.d.ts +2 -2
- package/dist/esm/index.js +5 -4
- package/dist/esm/stream-to-response.d.ts +28 -21
- package/dist/esm/stream-to-response.js +24 -17
- package/dist/esm/stream-to-response.js.map +1 -1
- package/dist/esm/types.d.ts +44 -25
- package/package.json +3 -6
- package/src/activities/chat/index.ts +36 -28
- package/src/activities/chat/tools/schema-converter.ts +332 -0
- package/src/activities/chat/tools/tool-calls.ts +47 -53
- package/src/activities/chat/tools/tool-definition.ts +33 -29
- package/src/index.ts +5 -2
- package/src/stream-to-response.ts +59 -44
- package/src/types.ts +47 -26
- package/dist/esm/activities/chat/tools/zod-converter.d.ts +0 -69
- package/dist/esm/activities/chat/tools/zod-converter.js +0 -99
- package/dist/esm/activities/chat/tools/zod-converter.js.map +0 -1
- package/src/activities/chat/tools/zod-converter.ts +0 -235
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"schema-converter.js","sources":["../../../../../src/activities/chat/tools/schema-converter.ts"],"sourcesContent":["/* eslint-disable @typescript-eslint/no-unnecessary-condition */\n\nimport type {\n StandardJSONSchemaV1,\n StandardSchemaV1,\n} from '@standard-schema/spec'\nimport type { JSONSchema, SchemaInput } from '../../../types'\n\n/**\n * Check if a value is a Standard JSON Schema compliant schema.\n * Standard JSON Schema compliant libraries (Zod v4+, ArkType, Valibot with toStandardJsonSchema, etc.)\n * implement the '~standard' property with jsonSchema converter methods.\n */\nexport function isStandardJSONSchema(\n schema: unknown,\n): schema is StandardJSONSchemaV1 {\n return (\n typeof schema === 'object' &&\n schema !== null &&\n '~standard' in schema &&\n typeof (schema as StandardJSONSchemaV1)['~standard'] === 'object' &&\n (schema as StandardJSONSchemaV1)['~standard'].version === 1 &&\n typeof (schema as StandardJSONSchemaV1)['~standard'].jsonSchema ===\n 'object' &&\n typeof (schema as StandardJSONSchemaV1)['~standard'].jsonSchema.input ===\n 'function'\n )\n}\n\n/**\n * Check if a value is a Standard Schema compliant schema (for validation).\n * Standard Schema compliant libraries implement the '~standard' property with a validate function.\n */\nexport function isStandardSchema(schema: unknown): schema is StandardSchemaV1 {\n return (\n typeof schema === 'object' &&\n schema !== null &&\n '~standard' in schema &&\n typeof schema['~standard'] === 'object' &&\n schema !== null &&\n schema['~standard'] !== null &&\n 'version' in schema['~standard'] &&\n schema['~standard'].version === 1 &&\n 'validate' in schema['~standard'] &&\n typeof schema['~standard'].validate === 'function'\n )\n}\n\n/**\n * Transform a JSON schema to be compatible with OpenAI's structured output requirements.\n * OpenAI requires:\n * - All properties must be in the `required` array\n * - Optional fields should have null added to their type union\n * - additionalProperties must be false for objects\n *\n * @param schema - JSON schema to transform\n * @param originalRequired - Original required array (to know which fields were optional)\n * @returns Transformed schema compatible with OpenAI structured output\n */\nfunction makeStructuredOutputCompatible(\n schema: Record<string, any>,\n originalRequired: Array<string> = [],\n): Record<string, any> {\n const result = { ...schema }\n\n // Handle object types\n if (result.type === 'object' && result.properties) {\n const properties = { ...result.properties }\n const allPropertyNames = Object.keys(properties)\n\n // Transform each property\n for (const propName of allPropertyNames) {\n const prop = properties[propName]\n const wasOptional = !originalRequired.includes(propName)\n\n // Recursively transform nested objects/arrays\n if (prop.type === 'object' && prop.properties) {\n properties[propName] = makeStructuredOutputCompatible(\n prop,\n prop.required || [],\n )\n } else if (prop.type === 'array' && prop.items) {\n properties[propName] = {\n ...prop,\n items: makeStructuredOutputCompatible(\n prop.items,\n prop.items.required || [],\n ),\n }\n } else if (wasOptional) {\n // Make optional fields nullable by adding null to the type\n if (prop.type && !Array.isArray(prop.type)) {\n properties[propName] = {\n ...prop,\n type: [prop.type, 'null'],\n }\n } else if (Array.isArray(prop.type) && !prop.type.includes('null')) {\n properties[propName] = {\n ...prop,\n type: [...prop.type, 'null'],\n }\n }\n }\n }\n\n result.properties = properties\n // ALL properties must be required for OpenAI structured output\n result.required = allPropertyNames\n // additionalProperties must be false\n result.additionalProperties = false\n }\n\n // Handle array types with object items\n if (result.type === 'array' && result.items) {\n result.items = makeStructuredOutputCompatible(\n result.items,\n result.items.required || [],\n )\n }\n\n return result\n}\n\n/**\n * Options for schema conversion\n */\nexport interface ConvertSchemaOptions {\n /**\n * When true, transforms the schema to be compatible with OpenAI's structured output requirements:\n * - All properties are added to the `required` array\n * - Optional fields get null added to their type union\n * - additionalProperties is set to false for all objects\n *\n * @default false\n */\n forStructuredOutput?: boolean\n}\n\n/**\n * Converts a Standard JSON Schema compliant schema or plain JSONSchema to JSON Schema format\n * compatible with LLM providers.\n *\n * Supports any schema library that implements the Standard JSON Schema spec (v1):\n * - Zod v4+ (natively supports StandardJSONSchemaV1)\n * - ArkType (natively supports StandardJSONSchemaV1)\n * - Valibot (via `toStandardJsonSchema()` from `@valibot/to-json-schema`)\n *\n * If the input is already a plain JSONSchema object, it is returned as-is.\n *\n * @param schema - Standard JSON Schema compliant schema or plain JSONSchema object to convert\n * @param options - Conversion options\n * @returns JSON Schema object that can be sent to LLM providers\n *\n * @example\n * ```typescript\n * // Using Zod v4+ (natively supports Standard JSON Schema)\n * import * as z from 'zod';\n *\n * const zodSchema = z.object({\n * location: z.string().describe('City name'),\n * unit: z.enum(['celsius', 'fahrenheit']).optional()\n * });\n *\n * const jsonSchema = convertSchemaToJsonSchema(zodSchema);\n *\n * @example\n * // Using ArkType (natively supports Standard JSON Schema)\n * import { type } from 'arktype';\n *\n * const arkSchema = type({\n * location: 'string',\n * unit: \"'celsius' | 'fahrenheit'\"\n * });\n *\n * const jsonSchema = convertSchemaToJsonSchema(arkSchema);\n *\n * @example\n * // Using Valibot (via toStandardJsonSchema)\n * import * as v from 'valibot';\n * import { toStandardJsonSchema } from '@valibot/to-json-schema';\n *\n * const valibotSchema = toStandardJsonSchema(v.object({\n * location: v.string(),\n * unit: v.optional(v.picklist(['celsius', 'fahrenheit']))\n * }));\n *\n * const jsonSchema = convertSchemaToJsonSchema(valibotSchema);\n *\n * @example\n * // Using JSONSchema directly (passes through unchanged)\n * const rawSchema = {\n * type: 'object',\n * properties: { location: { type: 'string' } },\n * required: ['location']\n * };\n * const result = convertSchemaToJsonSchema(rawSchema);\n * ```\n */\nexport function convertSchemaToJsonSchema(\n schema: SchemaInput | undefined,\n options: ConvertSchemaOptions = {},\n): JSONSchema | undefined {\n if (!schema) return undefined\n\n const { forStructuredOutput = false } = options\n\n // If it's a Standard JSON Schema compliant schema, use the standard interface\n if (isStandardJSONSchema(schema)) {\n const jsonSchema = schema['~standard'].jsonSchema.input({\n target: 'draft-07',\n })\n\n let result = jsonSchema\n\n if (typeof result === 'object' && '$schema' in result) {\n // Remove $schema property as it's not needed for LLM providers\n const { $schema, ...rest } = result\n result = rest\n }\n\n // Ensure object schemas always have type: \"object\"\n\n if (typeof result === 'object') {\n // If it has properties (even empty), it should be an object type\n if ('properties' in result && !result.type) {\n result.type = 'object'\n }\n\n // Ensure properties exists for object types (even if empty)\n if (result.type === 'object' && !('properties' in result)) {\n result.properties = {}\n }\n\n // Ensure required exists for object types (even if empty array)\n if (result.type === 'object' && !('required' in result)) {\n result.required = []\n }\n\n // Apply structured output transformation if requested\n if (forStructuredOutput) {\n result = makeStructuredOutputCompatible(\n result,\n (result.required as Array<string>) || [],\n )\n }\n }\n\n return result as JSONSchema\n }\n\n // If it's not a Standard JSON Schema, assume it's already a JSONSchema and pass through\n // Still apply structured output transformation if requested\n\n if (forStructuredOutput && typeof schema === 'object') {\n return makeStructuredOutputCompatible(\n schema as Record<string, any>,\n ((schema as JSONSchema).required as Array<string>) || [],\n ) as JSONSchema\n }\n\n return schema as JSONSchema\n}\n\n/**\n * Validates data against a Standard Schema compliant schema.\n *\n * @param schema - Standard Schema compliant schema\n * @param data - Data to validate\n * @returns Validation result with success status, data or issues\n */\nexport async function validateWithStandardSchema<T>(\n schema: unknown,\n data: unknown,\n): Promise<\n | { success: true; data: T }\n | { success: false; issues: Array<{ message: string; path?: Array<string> }> }\n> {\n if (!isStandardSchema(schema)) {\n // If it's not a Standard Schema, just return the data as-is\n return { success: true, data: data as T }\n }\n\n const result = await schema['~standard'].validate(data)\n\n if (!result.issues) {\n return { success: true, data: result.value as T }\n }\n\n return {\n success: false,\n issues: result.issues.map((issue) => ({\n message: issue.message || 'Validation failed',\n path: issue.path?.map(String),\n })),\n }\n}\n\n/**\n * Synchronously validates data against a Standard Schema compliant schema.\n * Note: Some Standard Schema implementations may only support async validation.\n * In those cases, this function will throw.\n *\n * @param schema - Standard Schema compliant schema\n * @param data - Data to validate\n * @returns Parsed/validated data\n * @throws Error if validation fails or if the schema only supports async validation\n */\nexport function parseWithStandardSchema<T>(schema: unknown, data: unknown): T {\n if (!isStandardSchema(schema)) {\n // If it's not a Standard Schema, just return the data as-is\n return data as T\n }\n\n const result = schema['~standard'].validate(data)\n\n // Handle async result (Promise)\n if (result instanceof Promise) {\n throw new Error(\n 'Schema validation returned a Promise. Use validateWithStandardSchema for async validation.',\n )\n }\n // Standard Schema validation returns { value } for success or { issues } for failure\n if (!result.issues) {\n return result.value as T\n }\n\n // invalid validation, throw error with all issues\n const errorMessages = result.issues\n .map((issue) => issue.message || 'Validation failed')\n .join(', ')\n throw new Error(`Validation failed: ${errorMessages}`)\n}\n"],"names":[],"mappings":"AAaO,SAAS,qBACd,QACgC;AAChC,SACE,OAAO,WAAW,YAClB,W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uB,OAAO,WAAW,UAAU;AACrD,WAAO;AAAA,MACL;AAAA,MACE,OAAsB,YAA8B,CAAA;AAAA,IAAC;AAAA,EAE3D;AAEA,SAAO;AACT;AA8CO,SAAS,wBAA2B,QAAiB,MAAkB;AAC5E,MAAI,CAAC,iBAAiB,MAAM,GAAG;AAE7B,WAAO;AAAA,EACT;AAEA,QAAM,SAAS,OAAO,WAAW,EAAE,SAAS,IAAI;AAGhD,MAAI,kBAAkB,SAAS;AAC7B,UAAM,IAAI;AAAA,MACR;AAAA,IAAA;AAAA,EAEJ;AAEA,MAAI,CAAC,OAAO,QAAQ;AAClB,WAAO,OAAO;AAAA,EAChB;AAGA,QAAM,gBAAgB,OAAO,OAC1B,IAAI,CAAC,UAAU,MAAM,WAAW,mBAAmB,EACnD,KAAK,IAAI;AACZ,QAAM,IAAI,MAAM,sBAAsB,aAAa,EAAE;AACvD;"}
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
if (!schema) return false;
|
|
3
|
-
return "_zod" in schema && typeof schema._zod === "object";
|
|
4
|
-
}
|
|
1
|
+
import { isStandardSchema, parseWithStandardSchema } from "./schema-converter.js";
|
|
5
2
|
class ToolCallManager {
|
|
6
3
|
constructor(tools) {
|
|
7
4
|
this.toolCallsMap = /* @__PURE__ */ new Map();
|
|
@@ -68,28 +65,31 @@ class ToolCallManager {
|
|
|
68
65
|
`Failed to parse tool arguments as JSON: ${toolCall.function.arguments}`
|
|
69
66
|
);
|
|
70
67
|
}
|
|
71
|
-
if (tool.inputSchema &&
|
|
68
|
+
if (tool.inputSchema && isStandardSchema(tool.inputSchema)) {
|
|
72
69
|
try {
|
|
73
|
-
args = tool.inputSchema
|
|
70
|
+
args = parseWithStandardSchema(tool.inputSchema, args);
|
|
74
71
|
} catch (validationError) {
|
|
72
|
+
const message = validationError instanceof Error ? validationError.message : "Validation failed";
|
|
75
73
|
throw new Error(
|
|
76
|
-
`Input validation failed for tool ${tool.name}: ${
|
|
74
|
+
`Input validation failed for tool ${tool.name}: ${message}`
|
|
77
75
|
);
|
|
78
76
|
}
|
|
79
77
|
}
|
|
80
78
|
let result = await tool.execute(args);
|
|
81
|
-
if (tool.outputSchema &&
|
|
79
|
+
if (tool.outputSchema && isStandardSchema(tool.outputSchema) && result !== void 0 && result !== null) {
|
|
82
80
|
try {
|
|
83
|
-
result = tool.outputSchema
|
|
81
|
+
result = parseWithStandardSchema(tool.outputSchema, result);
|
|
84
82
|
} catch (validationError) {
|
|
83
|
+
const message = validationError instanceof Error ? validationError.message : "Validation failed";
|
|
85
84
|
throw new Error(
|
|
86
|
-
`Output validation failed for tool ${tool.name}: ${
|
|
85
|
+
`Output validation failed for tool ${tool.name}: ${message}`
|
|
87
86
|
);
|
|
88
87
|
}
|
|
89
88
|
}
|
|
90
89
|
toolResultContent = typeof result === "string" ? result : JSON.stringify(result);
|
|
91
90
|
} catch (error) {
|
|
92
|
-
|
|
91
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
92
|
+
toolResultContent = `Error executing tool: ${message}`;
|
|
93
93
|
}
|
|
94
94
|
} else {
|
|
95
95
|
toolResultContent = `Tool ${toolCall.function.name} does not have an execute function`;
|
|
@@ -146,15 +146,16 @@ async function executeToolCalls(toolCalls, tools, approvals = /* @__PURE__ */ ne
|
|
|
146
146
|
throw new Error(`Failed to parse tool arguments as JSON: ${argsStr}`);
|
|
147
147
|
}
|
|
148
148
|
}
|
|
149
|
-
if (tool.inputSchema &&
|
|
149
|
+
if (tool.inputSchema && isStandardSchema(tool.inputSchema)) {
|
|
150
150
|
try {
|
|
151
|
-
input = tool.inputSchema
|
|
151
|
+
input = parseWithStandardSchema(tool.inputSchema, input);
|
|
152
152
|
} catch (validationError) {
|
|
153
|
+
const message = validationError instanceof Error ? validationError.message : "Validation failed";
|
|
153
154
|
results.push({
|
|
154
155
|
toolCallId: toolCall.id,
|
|
155
156
|
toolName,
|
|
156
157
|
result: {
|
|
157
|
-
error: `Input validation failed for tool ${tool.name}: ${
|
|
158
|
+
error: `Input validation failed for tool ${tool.name}: ${message}`
|
|
158
159
|
},
|
|
159
160
|
state: "output-error"
|
|
160
161
|
});
|
|
@@ -222,15 +223,8 @@ async function executeToolCalls(toolCalls, tools, approvals = /* @__PURE__ */ ne
|
|
|
222
223
|
try {
|
|
223
224
|
let result = await tool.execute(input);
|
|
224
225
|
const duration = Date.now() - startTime2;
|
|
225
|
-
if (tool.outputSchema &&
|
|
226
|
-
|
|
227
|
-
if (parsed.success) {
|
|
228
|
-
result = parsed.data;
|
|
229
|
-
} else {
|
|
230
|
-
throw new Error(
|
|
231
|
-
`Output validation failed for tool ${tool.name}: ${parsed.error.message}`
|
|
232
|
-
);
|
|
233
|
-
}
|
|
226
|
+
if (tool.outputSchema && isStandardSchema(tool.outputSchema) && result !== void 0 && result !== null) {
|
|
227
|
+
result = parseWithStandardSchema(tool.outputSchema, result);
|
|
234
228
|
}
|
|
235
229
|
results.push({
|
|
236
230
|
toolCallId: toolCall.id,
|
|
@@ -240,10 +234,11 @@ async function executeToolCalls(toolCalls, tools, approvals = /* @__PURE__ */ ne
|
|
|
240
234
|
});
|
|
241
235
|
} catch (error) {
|
|
242
236
|
const duration = Date.now() - startTime2;
|
|
237
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
243
238
|
results.push({
|
|
244
239
|
toolCallId: toolCall.id,
|
|
245
240
|
toolName,
|
|
246
|
-
result: { error:
|
|
241
|
+
result: { error: message },
|
|
247
242
|
state: "output-error",
|
|
248
243
|
duration
|
|
249
244
|
});
|
|
@@ -270,15 +265,8 @@ async function executeToolCalls(toolCalls, tools, approvals = /* @__PURE__ */ ne
|
|
|
270
265
|
try {
|
|
271
266
|
let result = await tool.execute(input);
|
|
272
267
|
const duration = Date.now() - startTime;
|
|
273
|
-
if (tool.outputSchema &&
|
|
274
|
-
|
|
275
|
-
if (parsed.success) {
|
|
276
|
-
result = parsed.data;
|
|
277
|
-
} else {
|
|
278
|
-
throw new Error(
|
|
279
|
-
`Output validation failed for tool ${tool.name}: ${parsed.error.message}`
|
|
280
|
-
);
|
|
281
|
-
}
|
|
268
|
+
if (tool.outputSchema && isStandardSchema(tool.outputSchema) && result !== void 0 && result !== null) {
|
|
269
|
+
result = parseWithStandardSchema(tool.outputSchema, result);
|
|
282
270
|
}
|
|
283
271
|
results.push({
|
|
284
272
|
toolCallId: toolCall.id,
|
|
@@ -288,10 +276,11 @@ async function executeToolCalls(toolCalls, tools, approvals = /* @__PURE__ */ ne
|
|
|
288
276
|
});
|
|
289
277
|
} catch (error) {
|
|
290
278
|
const duration = Date.now() - startTime;
|
|
279
|
+
const message = error instanceof Error ? error.message : "Unknown error";
|
|
291
280
|
results.push({
|
|
292
281
|
toolCallId: toolCall.id,
|
|
293
282
|
toolName,
|
|
294
|
-
result: { error:
|
|
283
|
+
result: { error: message },
|
|
295
284
|
state: "output-error",
|
|
296
285
|
duration
|
|
297
286
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool-calls.js","sources":["../../../../../src/activities/chat/tools/tool-calls.ts"],"sourcesContent":["import type {\n DoneStreamChunk,\n ModelMessage,\n SchemaInput,\n Tool,\n ToolCall,\n ToolResultStreamChunk,\n} from '../../../types'\nimport type { z } from 'zod'\n\n/**\n * Check if a value is a Zod schema by looking for Zod-specific internals.\n */\nfunction isZodSchema(schema: SchemaInput | undefined): schema is z.ZodType {\n if (!schema) return false\n return '_zod' in schema && typeof (schema as any)._zod === 'object'\n}\n\n/**\n * Manages tool call accumulation and execution for the chat() method's automatic tool execution loop.\n *\n * Responsibilities:\n * - Accumulates streaming tool call chunks (ID, name, arguments)\n * - Validates tool calls (filters out incomplete ones)\n * - Executes tool `execute` functions with parsed arguments\n * - Emits `tool_result` chunks for client visibility\n * - Returns tool result messages for conversation history\n *\n * This class is used internally by the AI.chat() method to handle the automatic\n * tool execution loop. It can also be used independently for custom tool execution logic.\n *\n * @example\n * ```typescript\n * const manager = new ToolCallManager(tools);\n *\n * // During streaming, accumulate tool calls\n * for await (const chunk of stream) {\n * if (chunk.type === \"tool_call\") {\n * manager.addToolCallChunk(chunk);\n * }\n * }\n *\n * // After stream completes, execute tools\n * if (manager.hasToolCalls()) {\n * const toolResults = yield* manager.executeTools(doneChunk);\n * messages = [...messages, ...toolResults];\n * manager.clear();\n * }\n * ```\n */\nexport class ToolCallManager {\n private toolCallsMap = new Map<number, ToolCall>()\n private tools: ReadonlyArray<Tool>\n\n constructor(tools: ReadonlyArray<Tool>) {\n this.tools = tools\n }\n\n /**\n * Add a tool call chunk to the accumulator\n * Handles streaming tool calls by accumulating arguments\n */\n addToolCallChunk(chunk: {\n toolCall: {\n id: string\n type: 'function'\n function: {\n name: string\n arguments: string\n }\n }\n index: number\n }): void {\n const index = chunk.index\n const existing = this.toolCallsMap.get(index)\n\n if (!existing) {\n // Only create entry if we have a tool call ID and name\n if (chunk.toolCall.id && chunk.toolCall.function.name) {\n this.toolCallsMap.set(index, {\n id: chunk.toolCall.id,\n type: 'function',\n function: {\n name: chunk.toolCall.function.name,\n arguments: chunk.toolCall.function.arguments || '',\n },\n })\n }\n } else {\n // Update name if it wasn't set before\n if (chunk.toolCall.function.name && !existing.function.name) {\n existing.function.name = chunk.toolCall.function.name\n }\n // Accumulate arguments for streaming tool calls\n if (chunk.toolCall.function.arguments) {\n existing.function.arguments += chunk.toolCall.function.arguments\n }\n }\n }\n\n /**\n * Check if there are any complete tool calls to execute\n */\n hasToolCalls(): boolean {\n return this.getToolCalls().length > 0\n }\n\n /**\n * Get all complete tool calls (filtered for valid ID and name)\n */\n getToolCalls(): Array<ToolCall> {\n return Array.from(this.toolCallsMap.values()).filter(\n (tc) => tc.id && tc.function.name && tc.function.name.trim().length > 0,\n )\n }\n\n /**\n * Execute all tool calls and return tool result messages\n * Also yields tool_result chunks for streaming\n */\n async *executeTools(\n doneChunk: DoneStreamChunk,\n ): AsyncGenerator<ToolResultStreamChunk, Array<ModelMessage>, void> {\n const toolCallsArray = this.getToolCalls()\n const toolResults: Array<ModelMessage> = []\n\n for (const toolCall of toolCallsArray) {\n const tool = this.tools.find((t) => t.name === toolCall.function.name)\n\n let toolResultContent: string\n if (tool?.execute) {\n try {\n // Parse arguments\n let args: any\n try {\n args = JSON.parse(toolCall.function.arguments)\n } catch (parseError) {\n throw new Error(\n `Failed to parse tool arguments as JSON: ${toolCall.function.arguments}`,\n )\n }\n\n // Validate input against inputSchema (only for Zod schemas)\n if (tool.inputSchema && isZodSchema(tool.inputSchema)) {\n try {\n args = tool.inputSchema.parse(args)\n } catch (validationError: any) {\n throw new Error(\n `Input validation failed for tool ${tool.name}: ${validationError.message}`,\n )\n }\n }\n\n // Execute the tool\n let result = await tool.execute(args)\n\n // Validate output against outputSchema if provided (only for Zod schemas)\n if (\n tool.outputSchema &&\n isZodSchema(tool.outputSchema) &&\n result !== undefined &&\n result !== null\n ) {\n try {\n result = tool.outputSchema.parse(result)\n } catch (validationError: any) {\n throw new Error(\n `Output validation failed for tool ${tool.name}: ${validationError.message}`,\n )\n }\n }\n\n toolResultContent =\n typeof result === 'string' ? result : JSON.stringify(result)\n } catch (error: any) {\n // If tool execution fails, add error message\n toolResultContent = `Error executing tool: ${error.message}`\n }\n } else {\n // Tool doesn't have execute function, add placeholder\n toolResultContent = `Tool ${toolCall.function.name} does not have an execute function`\n }\n\n // Emit tool_result chunk so callers can track tool execution\n yield {\n type: 'tool_result',\n id: doneChunk.id,\n model: doneChunk.model,\n timestamp: Date.now(),\n toolCallId: toolCall.id,\n content: toolResultContent,\n }\n\n // Add tool result message\n toolResults.push({\n role: 'tool',\n content: toolResultContent,\n toolCallId: toolCall.id,\n })\n }\n\n return toolResults\n }\n\n /**\n * Clear the tool calls map for the next iteration\n */\n clear(): void {\n this.toolCallsMap.clear()\n }\n}\n\nexport interface ToolResult {\n toolCallId: string\n toolName: string\n result: any\n state?: 'output-available' | 'output-error'\n /** Duration of tool execution in milliseconds (only for server-executed tools) */\n duration?: number\n}\n\nexport interface ApprovalRequest {\n toolCallId: string\n toolName: string\n input: any\n approvalId: string\n}\n\nexport interface ClientToolRequest {\n toolCallId: string\n toolName: string\n input: any\n}\n\ninterface ExecuteToolCallsResult {\n /** Tool results ready to send to LLM */\n results: Array<ToolResult>\n /** Tools that need user approval before execution */\n needsApproval: Array<ApprovalRequest>\n /** Tools that need client-side execution */\n needsClientExecution: Array<ClientToolRequest>\n}\n\n/**\n * Execute tool calls based on their configuration\n *\n * Handles three cases:\n * 1. Client tools (no execute) - request client to execute\n * 2. Server tools with approval - check approval before executing\n * 3. Normal server tools - execute immediately\n *\n * @param toolCalls - Tool calls from the LLM\n * @param tools - Available tools with their configurations\n * @param approvals - Map of approval decisions (approval.id -> approved boolean)\n * @param clientResults - Map of client-side execution results (toolCallId -> result)\n */\nexport async function executeToolCalls(\n toolCalls: Array<ToolCall>,\n tools: ReadonlyArray<Tool>,\n approvals: Map<string, boolean> = new Map(),\n clientResults: Map<string, any> = new Map(),\n): Promise<ExecuteToolCallsResult> {\n const results: Array<ToolResult> = []\n const needsApproval: Array<ApprovalRequest> = []\n const needsClientExecution: Array<ClientToolRequest> = []\n\n // Create tool lookup map\n const toolMap = new Map<string, Tool>()\n for (const tool of tools) {\n toolMap.set(tool.name, tool)\n }\n\n for (const toolCall of toolCalls) {\n const tool = toolMap.get(toolCall.function.name)\n const toolName = toolCall.function.name\n\n if (!tool) {\n // Unknown tool - return error\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: `Unknown tool: ${toolName}` },\n state: 'output-error',\n })\n continue\n }\n\n // Parse arguments, throwing error if invalid JSON\n let input: any = {}\n const argsStr = toolCall.function.arguments.trim() || '{}'\n if (argsStr) {\n try {\n input = JSON.parse(argsStr)\n } catch (parseError) {\n // If parsing fails, throw error to fail fast\n throw new Error(`Failed to parse tool arguments as JSON: ${argsStr}`)\n }\n }\n\n // Validate input against inputSchema (only for Zod schemas)\n if (tool.inputSchema && isZodSchema(tool.inputSchema)) {\n try {\n input = tool.inputSchema.parse(input)\n } catch (validationError: any) {\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: {\n error: `Input validation failed for tool ${tool.name}: ${validationError.message}`,\n },\n state: 'output-error',\n })\n continue\n }\n }\n\n // CASE 1: Client-side tool (no execute function)\n if (!tool.execute) {\n // Check if tool needs approval\n if (tool.needsApproval) {\n const approvalId = `approval_${toolCall.id}`\n\n // Check if approval decision exists\n if (approvals.has(approvalId)) {\n const approved = approvals.get(approvalId)\n\n if (approved) {\n // Approved - check if client has executed\n if (clientResults.has(toolCall.id)) {\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: clientResults.get(toolCall.id),\n })\n } else {\n // Approved but not executed yet - request client execution\n needsClientExecution.push({\n toolCallId: toolCall.id,\n toolName,\n input,\n })\n }\n } else {\n // User declined\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: 'User declined tool execution' },\n state: 'output-error',\n })\n }\n } else {\n // Need approval first\n needsApproval.push({\n toolCallId: toolCall.id,\n toolName: toolCall.function.name,\n input,\n approvalId,\n })\n }\n } else {\n // No approval needed - check if client has executed\n if (clientResults.has(toolCall.id)) {\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: clientResults.get(toolCall.id),\n })\n } else {\n // Request client execution\n needsClientExecution.push({\n toolCallId: toolCall.id,\n toolName,\n input,\n })\n }\n }\n continue\n }\n\n // CASE 2: Server tool with approval required\n if (tool.needsApproval) {\n const approvalId = `approval_${toolCall.id}`\n\n // Check if approval decision exists\n if (approvals.has(approvalId)) {\n const approved = approvals.get(approvalId)\n\n if (approved) {\n // Execute after approval\n const startTime = Date.now()\n try {\n let result = await tool.execute(input)\n const duration = Date.now() - startTime\n\n // Validate output against outputSchema if provided (only for Zod schemas)\n if (\n tool.outputSchema &&\n isZodSchema(tool.outputSchema) &&\n result !== undefined &&\n result !== null\n ) {\n const parsed = tool.outputSchema.safeParse(result)\n if (parsed.success) {\n result = parsed.data\n } else {\n throw new Error(\n `Output validation failed for tool ${tool.name}: ${parsed.error.message}`,\n )\n }\n }\n\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result:\n typeof result === 'string'\n ? JSON.parse(result)\n : result || null,\n duration,\n })\n } catch (error: any) {\n const duration = Date.now() - startTime\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: error.message },\n state: 'output-error',\n duration,\n })\n }\n } else {\n // User declined\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: 'User declined tool execution' },\n state: 'output-error',\n })\n }\n } else {\n // Need approval\n needsApproval.push({\n toolCallId: toolCall.id,\n toolName,\n input,\n approvalId,\n })\n }\n continue\n }\n\n // CASE 3: Normal server tool - execute immediately\n const startTime = Date.now()\n try {\n let result = await tool.execute(input)\n const duration = Date.now() - startTime\n\n // Validate output against outputSchema if provided (only for Zod schemas)\n if (\n tool.outputSchema &&\n isZodSchema(tool.outputSchema) &&\n result !== undefined &&\n result !== null\n ) {\n const parsed = tool.outputSchema.safeParse(result)\n if (parsed.success) {\n result = parsed.data\n } else {\n throw new Error(\n `Output validation failed for tool ${tool.name}: ${parsed.error.message}`,\n )\n }\n }\n\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result:\n typeof result === 'string' ? JSON.parse(result) : result || null,\n duration,\n })\n } catch (error: any) {\n const duration = Date.now() - startTime\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: error.message },\n state: 'output-error',\n duration,\n })\n }\n }\n\n return { results, needsApproval, needsClientExecution }\n}\n"],"names":["startTime"],"mappings":"AAaA,SAAS,YAAY,QAAsD;AACzE,MAAI,CAAC,OAAQ,QAAO;AACpB,SAAO,UAAU,UAAU,OAAQ,OAAe,SAAS;AAC7D;AAkCO,MAAM,gBAAgB;AAAA,EAI3B,YAAY,OAA4B;AAHxC,SAAQ,mCAAmB,IAAA;AAIzB,SAAK,QAAQ;AAAA,EACf;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,iBAAiB,OAUR;AACP,UAAM,QAAQ,MAAM;AACpB,UAAM,WAAW,KAAK,aAAa,IAAI,KAAK;AAE5C,QAAI,C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QACX,YAAY,SAAS;AAAA,QACrB;AAAA,QACA,QACE,OAAO,WAAW,WAAW,KAAK,MAAM,MAAM,IAAI,UAAU;AAAA,QAC9D;AAAA,MAAA,CACD;AAAA,IACH,SAAS,OAAY;AACnB,YAAM,WAAW,KAAK,IAAA,IAAQ;AAC9B,cAAQ,KAAK;AAAA,QACX,YAAY,SAAS;AAAA,QACrB;AAAA,QACA,QAAQ,EAAE,OAAO,MAAM,QAAA;AAAA,QACvB,OAAO;AAAA,QACP;AAAA,MAAA,CACD;AAAA,IACH;AAAA,EACF;AAEA,SAAO,EAAE,SAAS,eAAe,qBAAA;AACnC;"}
|
|
1
|
+
{"version":3,"file":"tool-calls.js","sources":["../../../../../src/activities/chat/tools/tool-calls.ts"],"sourcesContent":["import { isStandardSchema, parseWithStandardSchema } from './schema-converter'\nimport type {\n DoneStreamChunk,\n ModelMessage,\n Tool,\n ToolCall,\n ToolResultStreamChunk,\n} from '../../../types'\n\n/**\n * Manages tool call accumulation and execution for the chat() method's automatic tool execution loop.\n *\n * Responsibilities:\n * - Accumulates streaming tool call chunks (ID, name, arguments)\n * - Validates tool calls (filters out incomplete ones)\n * - Executes tool `execute` functions with parsed arguments\n * - Emits `tool_result` chunks for client visibility\n * - Returns tool result messages for conversation history\n *\n * This class is used internally by the AI.chat() method to handle the automatic\n * tool execution loop. It can also be used independently for custom tool execution logic.\n *\n * @example\n * ```typescript\n * const manager = new ToolCallManager(tools);\n *\n * // During streaming, accumulate tool calls\n * for await (const chunk of stream) {\n * if (chunk.type === \"tool_call\") {\n * manager.addToolCallChunk(chunk);\n * }\n * }\n *\n * // After stream completes, execute tools\n * if (manager.hasToolCalls()) {\n * const toolResults = yield* manager.executeTools(doneChunk);\n * messages = [...messages, ...toolResults];\n * manager.clear();\n * }\n * ```\n */\nexport class ToolCallManager {\n private toolCallsMap = new Map<number, ToolCall>()\n private tools: ReadonlyArray<Tool>\n\n constructor(tools: ReadonlyArray<Tool>) {\n this.tools = tools\n }\n\n /**\n * Add a tool call chunk to the accumulator\n * Handles streaming tool calls by accumulating arguments\n */\n addToolCallChunk(chunk: {\n toolCall: {\n id: string\n type: 'function'\n function: {\n name: string\n arguments: string\n }\n }\n index: number\n }): void {\n const index = chunk.index\n const existing = this.toolCallsMap.get(index)\n\n if (!existing) {\n // Only create entry if we have a tool call ID and name\n if (chunk.toolCall.id && chunk.toolCall.function.name) {\n this.toolCallsMap.set(index, {\n id: chunk.toolCall.id,\n type: 'function',\n function: {\n name: chunk.toolCall.function.name,\n arguments: chunk.toolCall.function.arguments || '',\n },\n })\n }\n } else {\n // Update name if it wasn't set before\n if (chunk.toolCall.function.name && !existing.function.name) {\n existing.function.name = chunk.toolCall.function.name\n }\n // Accumulate arguments for streaming tool calls\n if (chunk.toolCall.function.arguments) {\n existing.function.arguments += chunk.toolCall.function.arguments\n }\n }\n }\n\n /**\n * Check if there are any complete tool calls to execute\n */\n hasToolCalls(): boolean {\n return this.getToolCalls().length > 0\n }\n\n /**\n * Get all complete tool calls (filtered for valid ID and name)\n */\n getToolCalls(): Array<ToolCall> {\n return Array.from(this.toolCallsMap.values()).filter(\n (tc) => tc.id && tc.function.name && tc.function.name.trim().length > 0,\n )\n }\n\n /**\n * Execute all tool calls and return tool result messages\n * Also yields tool_result chunks for streaming\n */\n async *executeTools(\n doneChunk: DoneStreamChunk,\n ): AsyncGenerator<ToolResultStreamChunk, Array<ModelMessage>, void> {\n const toolCallsArray = this.getToolCalls()\n const toolResults: Array<ModelMessage> = []\n\n for (const toolCall of toolCallsArray) {\n const tool = this.tools.find((t) => t.name === toolCall.function.name)\n\n let toolResultContent: string\n if (tool?.execute) {\n try {\n // Parse arguments\n let args: unknown\n try {\n args = JSON.parse(toolCall.function.arguments)\n } catch (parseError) {\n throw new Error(\n `Failed to parse tool arguments as JSON: ${toolCall.function.arguments}`,\n )\n }\n\n // Validate input against inputSchema (for Standard Schema compliant schemas)\n if (tool.inputSchema && isStandardSchema(tool.inputSchema)) {\n try {\n args = parseWithStandardSchema(tool.inputSchema, args)\n } catch (validationError: unknown) {\n const message =\n validationError instanceof Error\n ? validationError.message\n : 'Validation failed'\n throw new Error(\n `Input validation failed for tool ${tool.name}: ${message}`,\n )\n }\n }\n\n // Execute the tool\n let result = await tool.execute(args)\n\n // Validate output against outputSchema if provided (for Standard Schema compliant schemas)\n if (\n tool.outputSchema &&\n isStandardSchema(tool.outputSchema) &&\n result !== undefined &&\n result !== null\n ) {\n try {\n result = parseWithStandardSchema(tool.outputSchema, result)\n } catch (validationError: unknown) {\n const message =\n validationError instanceof Error\n ? validationError.message\n : 'Validation failed'\n throw new Error(\n `Output validation failed for tool ${tool.name}: ${message}`,\n )\n }\n }\n\n toolResultContent =\n typeof result === 'string' ? result : JSON.stringify(result)\n } catch (error: unknown) {\n // If tool execution fails, add error message\n const message =\n error instanceof Error ? error.message : 'Unknown error'\n toolResultContent = `Error executing tool: ${message}`\n }\n } else {\n // Tool doesn't have execute function, add placeholder\n toolResultContent = `Tool ${toolCall.function.name} does not have an execute function`\n }\n\n // Emit tool_result chunk so callers can track tool execution\n yield {\n type: 'tool_result',\n id: doneChunk.id,\n model: doneChunk.model,\n timestamp: Date.now(),\n toolCallId: toolCall.id,\n content: toolResultContent,\n }\n\n // Add tool result message\n toolResults.push({\n role: 'tool',\n content: toolResultContent,\n toolCallId: toolCall.id,\n })\n }\n\n return toolResults\n }\n\n /**\n * Clear the tool calls map for the next iteration\n */\n clear(): void {\n this.toolCallsMap.clear()\n }\n}\n\nexport interface ToolResult {\n toolCallId: string\n toolName: string\n result: any\n state?: 'output-available' | 'output-error'\n /** Duration of tool execution in milliseconds (only for server-executed tools) */\n duration?: number\n}\n\nexport interface ApprovalRequest {\n toolCallId: string\n toolName: string\n input: any\n approvalId: string\n}\n\nexport interface ClientToolRequest {\n toolCallId: string\n toolName: string\n input: any\n}\n\ninterface ExecuteToolCallsResult {\n /** Tool results ready to send to LLM */\n results: Array<ToolResult>\n /** Tools that need user approval before execution */\n needsApproval: Array<ApprovalRequest>\n /** Tools that need client-side execution */\n needsClientExecution: Array<ClientToolRequest>\n}\n\n/**\n * Execute tool calls based on their configuration\n *\n * Handles three cases:\n * 1. Client tools (no execute) - request client to execute\n * 2. Server tools with approval - check approval before executing\n * 3. Normal server tools - execute immediately\n *\n * @param toolCalls - Tool calls from the LLM\n * @param tools - Available tools with their configurations\n * @param approvals - Map of approval decisions (approval.id -> approved boolean)\n * @param clientResults - Map of client-side execution results (toolCallId -> result)\n */\nexport async function executeToolCalls(\n toolCalls: Array<ToolCall>,\n tools: ReadonlyArray<Tool>,\n approvals: Map<string, boolean> = new Map(),\n clientResults: Map<string, any> = new Map(),\n): Promise<ExecuteToolCallsResult> {\n const results: Array<ToolResult> = []\n const needsApproval: Array<ApprovalRequest> = []\n const needsClientExecution: Array<ClientToolRequest> = []\n\n // Create tool lookup map\n const toolMap = new Map<string, Tool>()\n for (const tool of tools) {\n toolMap.set(tool.name, tool)\n }\n\n for (const toolCall of toolCalls) {\n const tool = toolMap.get(toolCall.function.name)\n const toolName = toolCall.function.name\n\n if (!tool) {\n // Unknown tool - return error\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: `Unknown tool: ${toolName}` },\n state: 'output-error',\n })\n continue\n }\n\n // Parse arguments, throwing error if invalid JSON\n let input: unknown = {}\n const argsStr = toolCall.function.arguments.trim() || '{}'\n if (argsStr) {\n try {\n input = JSON.parse(argsStr)\n } catch (parseError) {\n // If parsing fails, throw error to fail fast\n throw new Error(`Failed to parse tool arguments as JSON: ${argsStr}`)\n }\n }\n\n // Validate input against inputSchema (for Standard Schema compliant schemas)\n if (tool.inputSchema && isStandardSchema(tool.inputSchema)) {\n try {\n input = parseWithStandardSchema(tool.inputSchema, input)\n } catch (validationError: unknown) {\n const message =\n validationError instanceof Error\n ? validationError.message\n : 'Validation failed'\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: {\n error: `Input validation failed for tool ${tool.name}: ${message}`,\n },\n state: 'output-error',\n })\n continue\n }\n }\n\n // CASE 1: Client-side tool (no execute function)\n if (!tool.execute) {\n // Check if tool needs approval\n if (tool.needsApproval) {\n const approvalId = `approval_${toolCall.id}`\n\n // Check if approval decision exists\n if (approvals.has(approvalId)) {\n const approved = approvals.get(approvalId)\n\n if (approved) {\n // Approved - check if client has executed\n if (clientResults.has(toolCall.id)) {\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: clientResults.get(toolCall.id),\n })\n } else {\n // Approved but not executed yet - request client execution\n needsClientExecution.push({\n toolCallId: toolCall.id,\n toolName,\n input,\n })\n }\n } else {\n // User declined\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: 'User declined tool execution' },\n state: 'output-error',\n })\n }\n } else {\n // Need approval first\n needsApproval.push({\n toolCallId: toolCall.id,\n toolName: toolCall.function.name,\n input,\n approvalId,\n })\n }\n } else {\n // No approval needed - check if client has executed\n if (clientResults.has(toolCall.id)) {\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: clientResults.get(toolCall.id),\n })\n } else {\n // Request client execution\n needsClientExecution.push({\n toolCallId: toolCall.id,\n toolName,\n input,\n })\n }\n }\n continue\n }\n\n // CASE 2: Server tool with approval required\n if (tool.needsApproval) {\n const approvalId = `approval_${toolCall.id}`\n\n // Check if approval decision exists\n if (approvals.has(approvalId)) {\n const approved = approvals.get(approvalId)\n\n if (approved) {\n // Execute after approval\n const startTime = Date.now()\n try {\n let result = await tool.execute(input)\n const duration = Date.now() - startTime\n\n // Validate output against outputSchema if provided (for Standard Schema compliant schemas)\n if (\n tool.outputSchema &&\n isStandardSchema(tool.outputSchema) &&\n result !== undefined &&\n result !== null\n ) {\n result = parseWithStandardSchema(tool.outputSchema, result)\n }\n\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result:\n typeof result === 'string'\n ? JSON.parse(result)\n : result || null,\n duration,\n })\n } catch (error: unknown) {\n const duration = Date.now() - startTime\n const message =\n error instanceof Error ? error.message : 'Unknown error'\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: message },\n state: 'output-error',\n duration,\n })\n }\n } else {\n // User declined\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: 'User declined tool execution' },\n state: 'output-error',\n })\n }\n } else {\n // Need approval\n needsApproval.push({\n toolCallId: toolCall.id,\n toolName,\n input,\n approvalId,\n })\n }\n continue\n }\n\n // CASE 3: Normal server tool - execute immediately\n const startTime = Date.now()\n try {\n let result = await tool.execute(input)\n const duration = Date.now() - startTime\n\n // Validate output against outputSchema if provided (for Standard Schema compliant schemas)\n if (\n tool.outputSchema &&\n isStandardSchema(tool.outputSchema) &&\n result !== undefined &&\n result !== null\n ) {\n result = parseWithStandardSchema(tool.outputSchema, result)\n }\n\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result:\n typeof result === 'string' ? JSON.parse(result) : result || null,\n duration,\n })\n } catch (error: unknown) {\n const duration = Date.now() - startTime\n const message = error instanceof Error ? error.message : 'Unknown error'\n results.push({\n toolCallId: toolCall.id,\n toolName,\n result: { error: message },\n state: 'output-error',\n duration,\n })\n }\n }\n\n return { results, needsApproval, needsClientExecution }\n}\n"],"names":["startTime"],"mappings":";AAyCO,MAAM,gBAAgB;AAAA,EAI3B,YAAY,OAA4B;AAHxC,SAAQ,mCAAmB,IAAA;AAIzB,SAAK,QAAQ;AAAA,EACf;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,iBAAiB,OAUR;AACP,UAAM,QAAQ,MAAM;AACpB,UAAM,WAAW,KAAK,aAAa,IAAI,KAAK;AAE5C,QAAI,CAAC,UAAU;AAEb,UAAI,MAAM,SAAS,MAAM,MAAM,SAAS,SAAS,MAAM;AACrD,aAAK,aAAa,IAAI,OAAO;AAAA,UAC3B,IAAI,MAAM,SAAS;AAAA,UACnB,MAAM;AAAA,UACN,UAAU;AAAA,YACR,MAAM,MAAM,SAAS,SAAS;AAAA,YAC9B,WAAW,MAAM,SAAS,SAAS,aAAa;AAAA,UAAA;AAAA,QAClD,CACD;AAAA,MACH;AAAA,IACF,OAAO;A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,SAAS,eAAe,qBAAA;AACnC;"}
|
|
@@ -1,34 +1,34 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { StandardJSONSchemaV1 } from '@standard-schema/spec';
|
|
2
2
|
import { InferSchemaType, JSONSchema, SchemaInput, Tool } from '../../../types.js';
|
|
3
3
|
/**
|
|
4
4
|
* Marker type for server-side tools
|
|
5
5
|
*/
|
|
6
|
-
export interface ServerTool<TInput extends SchemaInput =
|
|
6
|
+
export interface ServerTool<TInput extends SchemaInput = SchemaInput, TOutput extends SchemaInput = SchemaInput, TName extends string = string> extends Tool<TInput, TOutput, TName> {
|
|
7
7
|
__toolSide: 'server';
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
10
|
* Marker type for client-side tools
|
|
11
11
|
*/
|
|
12
|
-
export interface ClientTool<TInput extends SchemaInput =
|
|
12
|
+
export interface ClientTool<TInput extends SchemaInput = SchemaInput, TOutput extends SchemaInput = SchemaInput, TName extends string = string> {
|
|
13
13
|
__toolSide: 'client';
|
|
14
14
|
name: TName;
|
|
15
15
|
description: string;
|
|
16
16
|
inputSchema?: TInput;
|
|
17
17
|
outputSchema?: TOutput;
|
|
18
18
|
needsApproval?: boolean;
|
|
19
|
-
metadata?: Record<string,
|
|
19
|
+
metadata?: Record<string, unknown>;
|
|
20
20
|
execute?: (args: InferSchemaType<TInput>) => Promise<InferSchemaType<TOutput>> | InferSchemaType<TOutput>;
|
|
21
21
|
}
|
|
22
22
|
/**
|
|
23
23
|
* Tool definition that can be used directly or instantiated for server/client
|
|
24
24
|
*/
|
|
25
|
-
export interface ToolDefinitionInstance<TInput extends SchemaInput =
|
|
25
|
+
export interface ToolDefinitionInstance<TInput extends SchemaInput = SchemaInput, TOutput extends SchemaInput = SchemaInput, TName extends string = string> extends Tool<TInput, TOutput, TName> {
|
|
26
26
|
__toolSide: 'definition';
|
|
27
27
|
}
|
|
28
28
|
/**
|
|
29
29
|
* Union type for any kind of client-side tool (client tool or definition)
|
|
30
30
|
*/
|
|
31
|
-
export type AnyClientTool = ClientTool<
|
|
31
|
+
export type AnyClientTool = ClientTool<SchemaInput, SchemaInput> | ToolDefinitionInstance<SchemaInput, SchemaInput>;
|
|
32
32
|
/**
|
|
33
33
|
* Extract the tool name as a literal type
|
|
34
34
|
*/
|
|
@@ -36,32 +36,32 @@ export type InferToolName<T> = T extends {
|
|
|
36
36
|
name: infer N;
|
|
37
37
|
} ? N : never;
|
|
38
38
|
/**
|
|
39
|
-
* Extract the input type from a tool (inferred from
|
|
39
|
+
* Extract the input type from a tool (inferred from Standard JSON Schema, or `unknown` for plain JSONSchema)
|
|
40
40
|
*/
|
|
41
41
|
export type InferToolInput<T> = T extends {
|
|
42
42
|
inputSchema?: infer TInput;
|
|
43
|
-
} ? TInput extends
|
|
43
|
+
} ? TInput extends StandardJSONSchemaV1<infer TInferred, unknown> ? TInferred : TInput extends JSONSchema ? unknown : unknown : unknown;
|
|
44
44
|
/**
|
|
45
|
-
* Extract the output type from a tool (inferred from
|
|
45
|
+
* Extract the output type from a tool (inferred from Standard JSON Schema, or `unknown` for plain JSONSchema)
|
|
46
46
|
*/
|
|
47
47
|
export type InferToolOutput<T> = T extends {
|
|
48
48
|
outputSchema?: infer TOutput;
|
|
49
|
-
} ? TOutput extends
|
|
49
|
+
} ? TOutput extends StandardJSONSchemaV1<infer TInferred, unknown> ? TInferred : TOutput extends JSONSchema ? unknown : unknown : unknown;
|
|
50
50
|
/**
|
|
51
51
|
* Tool definition configuration
|
|
52
52
|
*/
|
|
53
|
-
export interface ToolDefinitionConfig<TInput extends SchemaInput =
|
|
53
|
+
export interface ToolDefinitionConfig<TInput extends SchemaInput = SchemaInput, TOutput extends SchemaInput = SchemaInput, TName extends string = string> {
|
|
54
54
|
name: TName;
|
|
55
55
|
description: string;
|
|
56
56
|
inputSchema?: TInput;
|
|
57
57
|
outputSchema?: TOutput;
|
|
58
58
|
needsApproval?: boolean;
|
|
59
|
-
metadata?: Record<string,
|
|
59
|
+
metadata?: Record<string, unknown>;
|
|
60
60
|
}
|
|
61
61
|
/**
|
|
62
62
|
* Tool definition builder that allows creating server or client tools from a shared definition
|
|
63
63
|
*/
|
|
64
|
-
export interface ToolDefinition<TInput extends SchemaInput =
|
|
64
|
+
export interface ToolDefinition<TInput extends SchemaInput = SchemaInput, TOutput extends SchemaInput = SchemaInput, TName extends string = string> extends ToolDefinitionInstance<TInput, TOutput, TName> {
|
|
65
65
|
/**
|
|
66
66
|
* Create a server-side tool with execute function
|
|
67
67
|
*/
|
|
@@ -79,11 +79,15 @@ export interface ToolDefinition<TInput extends SchemaInput = z.ZodType, TOutput
|
|
|
79
79
|
* 2. Instantiated as a server tool with .server()
|
|
80
80
|
* 3. Instantiated as a client tool with .client()
|
|
81
81
|
*
|
|
82
|
+
* Supports any Standard JSON Schema compliant library (Zod v4+, ArkType, Valibot, etc.)
|
|
83
|
+
* or plain JSON Schema objects.
|
|
84
|
+
*
|
|
82
85
|
* @example
|
|
83
86
|
* ```typescript
|
|
84
87
|
* import { toolDefinition } from '@tanstack/ai';
|
|
85
88
|
* import { z } from 'zod';
|
|
86
89
|
*
|
|
90
|
+
* // Using Zod (natively supports Standard JSON Schema)
|
|
87
91
|
* const addToCartTool = toolDefinition({
|
|
88
92
|
* name: 'addToCart',
|
|
89
93
|
* description: 'Add a guitar to the shopping cart (requires approval)',
|
|
@@ -122,4 +126,4 @@ export interface ToolDefinition<TInput extends SchemaInput = z.ZodType, TOutput
|
|
|
122
126
|
* });
|
|
123
127
|
* ```
|
|
124
128
|
*/
|
|
125
|
-
export declare function toolDefinition<TInput extends SchemaInput =
|
|
129
|
+
export declare function toolDefinition<TInput extends SchemaInput = SchemaInput, TOutput extends SchemaInput = SchemaInput, TName extends string = string>(config: ToolDefinitionConfig<TInput, TOutput, TName>): ToolDefinition<TInput, TOutput, TName>;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool-definition.js","sources":["../../../../../src/activities/chat/tools/tool-definition.ts"],"sourcesContent":["import type {
|
|
1
|
+
{"version":3,"file":"tool-definition.js","sources":["../../../../../src/activities/chat/tools/tool-definition.ts"],"sourcesContent":["import type { StandardJSONSchemaV1 } from '@standard-schema/spec'\nimport type {\n InferSchemaType,\n JSONSchema,\n SchemaInput,\n Tool,\n} from '../../../types'\n\n/**\n * Marker type for server-side tools\n */\nexport interface ServerTool<\n TInput extends SchemaInput = SchemaInput,\n TOutput extends SchemaInput = SchemaInput,\n TName extends string = string,\n> extends Tool<TInput, TOutput, TName> {\n __toolSide: 'server'\n}\n\n/**\n * Marker type for client-side tools\n */\nexport interface ClientTool<\n TInput extends SchemaInput = SchemaInput,\n TOutput extends SchemaInput = SchemaInput,\n TName extends string = string,\n> {\n __toolSide: 'client'\n name: TName\n description: string\n inputSchema?: TInput\n outputSchema?: TOutput\n needsApproval?: boolean\n metadata?: Record<string, unknown>\n execute?: (\n args: InferSchemaType<TInput>,\n ) => Promise<InferSchemaType<TOutput>> | InferSchemaType<TOutput>\n}\n\n/**\n * Tool definition that can be used directly or instantiated for server/client\n */\nexport interface ToolDefinitionInstance<\n TInput extends SchemaInput = SchemaInput,\n TOutput extends SchemaInput = SchemaInput,\n TName extends string = string,\n> extends Tool<TInput, TOutput, TName> {\n __toolSide: 'definition'\n}\n\n/**\n * Union type for any kind of client-side tool (client tool or definition)\n */\nexport type AnyClientTool =\n | ClientTool<SchemaInput, SchemaInput>\n | ToolDefinitionInstance<SchemaInput, SchemaInput>\n\n/**\n * Extract the tool name as a literal type\n */\nexport type InferToolName<T> = T extends { name: infer N } ? N : never\n\n/**\n * Extract the input type from a tool (inferred from Standard JSON Schema, or `unknown` for plain JSONSchema)\n */\nexport type InferToolInput<T> = T extends { inputSchema?: infer TInput }\n ? TInput extends StandardJSONSchemaV1<infer TInferred, unknown>\n ? TInferred\n : TInput extends JSONSchema\n ? unknown\n : unknown\n : unknown\n\n/**\n * Extract the output type from a tool (inferred from Standard JSON Schema, or `unknown` for plain JSONSchema)\n */\nexport type InferToolOutput<T> = T extends { outputSchema?: infer TOutput }\n ? TOutput extends StandardJSONSchemaV1<infer TInferred, unknown>\n ? TInferred\n : TOutput extends JSONSchema\n ? unknown\n : unknown\n : unknown\n\n/**\n * Tool definition configuration\n */\nexport interface ToolDefinitionConfig<\n TInput extends SchemaInput = SchemaInput,\n TOutput extends SchemaInput = SchemaInput,\n TName extends string = string,\n> {\n name: TName\n description: string\n inputSchema?: TInput\n outputSchema?: TOutput\n needsApproval?: boolean\n metadata?: Record<string, unknown>\n}\n\n/**\n * Tool definition builder that allows creating server or client tools from a shared definition\n */\nexport interface ToolDefinition<\n TInput extends SchemaInput = SchemaInput,\n TOutput extends SchemaInput = SchemaInput,\n TName extends string = string,\n> extends ToolDefinitionInstance<TInput, TOutput, TName> {\n /**\n * Create a server-side tool with execute function\n */\n server: (\n execute: (\n args: InferSchemaType<TInput>,\n ) => Promise<InferSchemaType<TOutput>> | InferSchemaType<TOutput>,\n ) => ServerTool<TInput, TOutput, TName>\n\n /**\n * Create a client-side tool with optional execute function\n */\n client: (\n execute?: (\n args: InferSchemaType<TInput>,\n ) => Promise<InferSchemaType<TOutput>> | InferSchemaType<TOutput>,\n ) => ClientTool<TInput, TOutput, TName>\n}\n\n/**\n * Create an isomorphic tool definition that can be used directly or instantiated for server/client\n *\n * The definition contains all tool metadata (name, description, schemas) and can be:\n * 1. Used directly in chat() on the server (as a tool definition without execute)\n * 2. Instantiated as a server tool with .server()\n * 3. Instantiated as a client tool with .client()\n *\n * Supports any Standard JSON Schema compliant library (Zod v4+, ArkType, Valibot, etc.)\n * or plain JSON Schema objects.\n *\n * @example\n * ```typescript\n * import { toolDefinition } from '@tanstack/ai';\n * import { z } from 'zod';\n *\n * // Using Zod (natively supports Standard JSON Schema)\n * const addToCartTool = toolDefinition({\n * name: 'addToCart',\n * description: 'Add a guitar to the shopping cart (requires approval)',\n * needsApproval: true,\n * inputSchema: z.object({\n * guitarId: z.string(),\n * quantity: z.number(),\n * }),\n * outputSchema: z.object({\n * success: z.boolean(),\n * cartId: z.string(),\n * totalItems: z.number(),\n * }),\n * });\n *\n * // Use directly in chat (server-side, no execute function)\n * chat({\n * tools: [addToCartTool],\n * // ...\n * });\n *\n * // Or create server-side implementation\n * const addToCartServer = addToCartTool.server(async (args) => {\n * // args is typed as { guitarId: string; quantity: number }\n * return {\n * success: true,\n * cartId: 'CART_' + Date.now(),\n * totalItems: args.quantity,\n * };\n * });\n *\n * // Or create client-side implementation\n * const addToCartClient = addToCartTool.client(async (args) => {\n * // Client-specific logic (e.g., localStorage)\n * return { success: true, cartId: 'local', totalItems: 1 };\n * });\n * ```\n */\nexport function toolDefinition<\n TInput extends SchemaInput = SchemaInput,\n TOutput extends SchemaInput = SchemaInput,\n TName extends string = string,\n>(\n config: ToolDefinitionConfig<TInput, TOutput, TName>,\n): ToolDefinition<TInput, TOutput, TName> {\n const definition: ToolDefinition<TInput, TOutput, TName> = {\n __toolSide: 'definition',\n ...config,\n server(\n execute: (\n args: InferSchemaType<TInput>,\n ) => Promise<InferSchemaType<TOutput>> | InferSchemaType<TOutput>,\n ): ServerTool<TInput, TOutput, TName> {\n return {\n __toolSide: 'server',\n ...config,\n execute,\n }\n },\n\n client(\n execute?: (\n args: InferSchemaType<TInput>,\n ) => Promise<InferSchemaType<TOutput>> | InferSchemaType<TOutput>,\n ): ClientTool<TInput, TOutput, TName> {\n return {\n __toolSide: 'client',\n ...config,\n execute,\n }\n },\n }\n\n return definition\n}\n"],"names":[],"mappings":"AAsLO,SAAS,eAKd,QACwC;AACxC,QAAM,aAAqD;AAAA,IACzD,YAAY;AAAA,IACZ,GAAG;AAAA,IACH,OACE,SAGoC;AACpC,aAAO;AAAA,QACL,YAAY;AAAA,QACZ,GAAG;AAAA,QACH;AAAA,MAAA;AAAA,IAEJ;AAAA,IAEA,OACE,SAGoC;AACpC,aAAO;AAAA,QACL,YAAY;AAAA,QACZ,GAAG;AAAA,QACH;AAAA,MAAA;AAAA,IAEJ;AAAA,EAAA;AAGF,SAAO;AACT;"}
|
package/dist/esm/index.d.ts
CHANGED
|
@@ -7,8 +7,8 @@ export { createSpeechOptions } from './activities/generateSpeech/index.js';
|
|
|
7
7
|
export { createTranscriptionOptions } from './activities/generateTranscription/index.js';
|
|
8
8
|
export type { AIAdapter, ImageAdapter, AnyImageAdapter, TextAdapter, AnyTextAdapter, AnySummarizeAdapter, SummarizeAdapter, AnyTTSAdapter, TTSAdapter, AnyTranscriptionAdapter, TranscriptionAdapter, AnyVideoAdapter, VideoAdapter, } from './activities/index.js';
|
|
9
9
|
export { toolDefinition, type ToolDefinition, type ToolDefinitionInstance, type ToolDefinitionConfig, type ServerTool, type ClientTool, type AnyClientTool, type InferToolName, type InferToolInput, type InferToolOutput, } from './activities/chat/tools/tool-definition.js';
|
|
10
|
-
export {
|
|
11
|
-
export { streamToText, toServerSentEventsStream,
|
|
10
|
+
export { convertSchemaToJsonSchema } from './activities/chat/tools/schema-converter.js';
|
|
11
|
+
export { streamToText, toServerSentEventsStream, toServerSentEventsResponse, toHttpStream, toHttpResponse, } from './stream-to-response.js';
|
|
12
12
|
export { ToolCallManager } from './activities/chat/tools/tool-calls.js';
|
|
13
13
|
export { maxIterations, untilFinishReason, combineStrategies, } from './activities/chat/agent-loop-strategies.js';
|
|
14
14
|
export * from './types.js';
|
package/dist/esm/index.js
CHANGED
|
@@ -5,8 +5,8 @@ import { createVideoOptions, generateVideo, getVideoJobStatus } from "./activiti
|
|
|
5
5
|
import { createSpeechOptions, generateSpeech } from "./activities/generateSpeech/index.js";
|
|
6
6
|
import { createTranscriptionOptions, generateTranscription } from "./activities/generateTranscription/index.js";
|
|
7
7
|
import { toolDefinition } from "./activities/chat/tools/tool-definition.js";
|
|
8
|
-
import {
|
|
9
|
-
import { streamToText, toHttpStream,
|
|
8
|
+
import { convertSchemaToJsonSchema } from "./activities/chat/tools/schema-converter.js";
|
|
9
|
+
import { streamToText, toHttpResponse, toHttpStream, toServerSentEventsResponse, toServerSentEventsStream } from "./stream-to-response.js";
|
|
10
10
|
import { ToolCallManager } from "./activities/chat/tools/tool-calls.js";
|
|
11
11
|
import { combineStrategies, maxIterations, untilFinishReason } from "./activities/chat/agent-loop-strategies.js";
|
|
12
12
|
import { aiEventClient } from "./event-client.js";
|
|
@@ -27,7 +27,7 @@ export {
|
|
|
27
27
|
chat,
|
|
28
28
|
combineStrategies,
|
|
29
29
|
convertMessagesToModelMessages,
|
|
30
|
-
|
|
30
|
+
convertSchemaToJsonSchema,
|
|
31
31
|
createChatOptions,
|
|
32
32
|
createImageOptions,
|
|
33
33
|
createReplayStream,
|
|
@@ -49,9 +49,10 @@ export {
|
|
|
49
49
|
parsePartialJSON,
|
|
50
50
|
streamToText,
|
|
51
51
|
summarize,
|
|
52
|
+
toHttpResponse,
|
|
52
53
|
toHttpStream,
|
|
54
|
+
toServerSentEventsResponse,
|
|
53
55
|
toServerSentEventsStream,
|
|
54
|
-
toStreamResponse,
|
|
55
56
|
toolDefinition,
|
|
56
57
|
uiMessageToModelMessages,
|
|
57
58
|
untilFinishReason
|
|
@@ -31,15 +31,29 @@ export declare function streamToText(stream: AsyncIterable<StreamChunk>): Promis
|
|
|
31
31
|
* @param stream - AsyncIterable of StreamChunks from chat()
|
|
32
32
|
* @param abortController - Optional AbortController to abort when stream is cancelled
|
|
33
33
|
* @returns ReadableStream in Server-Sent Events format
|
|
34
|
+
*/
|
|
35
|
+
export declare function toServerSentEventsStream(stream: AsyncIterable<StreamChunk>, abortController?: AbortController): ReadableStream<Uint8Array>;
|
|
36
|
+
/**
|
|
37
|
+
* Convert a StreamChunk async iterable to a Response in Server-Sent Events format
|
|
38
|
+
*
|
|
39
|
+
* This creates a Response that emits chunks in SSE format:
|
|
40
|
+
* - Each chunk is prefixed with "data: "
|
|
41
|
+
* - Each chunk is followed by "\n\n"
|
|
42
|
+
* - Stream ends with "data: [DONE]\n\n"
|
|
43
|
+
*
|
|
44
|
+
* @param stream - AsyncIterable of StreamChunks from chat()
|
|
45
|
+
* @param init - Optional Response initialization options (including `abortController`)
|
|
46
|
+
* @returns Response in Server-Sent Events format
|
|
34
47
|
*
|
|
35
48
|
* @example
|
|
36
49
|
* ```typescript
|
|
37
50
|
* const stream = chat({ adapter: openaiText(), model: "gpt-4o", messages: [...] });
|
|
38
|
-
*
|
|
39
|
-
* // Use with Response, or any API that accepts ReadableStream
|
|
51
|
+
* return toServerSentEventsResponse(stream, { abortController });
|
|
40
52
|
* ```
|
|
41
53
|
*/
|
|
42
|
-
export declare function
|
|
54
|
+
export declare function toServerSentEventsResponse(stream: AsyncIterable<StreamChunk>, init?: ResponseInit & {
|
|
55
|
+
abortController?: AbortController;
|
|
56
|
+
}): Response;
|
|
43
57
|
/**
|
|
44
58
|
* Convert a StreamChunk async iterable to a ReadableStream in HTTP stream format (newline-delimited JSON)
|
|
45
59
|
*
|
|
@@ -65,31 +79,24 @@ export declare function toServerSentEventsStream(stream: AsyncIterable<StreamChu
|
|
|
65
79
|
*/
|
|
66
80
|
export declare function toHttpStream(stream: AsyncIterable<StreamChunk>, abortController?: AbortController): ReadableStream<Uint8Array>;
|
|
67
81
|
/**
|
|
68
|
-
*
|
|
69
|
-
*
|
|
82
|
+
* Convert a StreamChunk async iterable to a Response in HTTP stream format (newline-delimited JSON)
|
|
83
|
+
*
|
|
84
|
+
* This creates a Response that emits chunks in HTTP stream format:
|
|
85
|
+
* - Each chunk is JSON.stringify'd and followed by "\n"
|
|
86
|
+
* - No SSE formatting (no "data: " prefix)
|
|
70
87
|
*
|
|
71
|
-
*
|
|
88
|
+
* This format is compatible with `fetchHttpStream` connection adapter.
|
|
72
89
|
*
|
|
73
90
|
* @param stream - AsyncIterable of StreamChunks from chat()
|
|
74
|
-
* @param init - Optional Response initialization options
|
|
75
|
-
* @
|
|
76
|
-
* @returns Response object with SSE headers and streaming body
|
|
91
|
+
* @param init - Optional Response initialization options (including `abortController`)
|
|
92
|
+
* @returns Response in HTTP stream format (newline-delimited JSON)
|
|
77
93
|
*
|
|
78
94
|
* @example
|
|
79
95
|
* ```typescript
|
|
80
|
-
*
|
|
81
|
-
*
|
|
82
|
-
* const abortController = new AbortController();
|
|
83
|
-
* const stream = chat({
|
|
84
|
-
* adapter: openaiText(),
|
|
85
|
-
* model: "gpt-4o",
|
|
86
|
-
* messages,
|
|
87
|
-
* options: { abortSignal: abortController.signal }
|
|
88
|
-
* });
|
|
89
|
-
* return toStreamResponse(stream, undefined, abortController);
|
|
90
|
-
* }
|
|
96
|
+
* const stream = chat({ adapter: openaiText(), model: "gpt-4o", messages: [...] });
|
|
97
|
+
* return toHttpResponse(stream, { abortController });
|
|
91
98
|
* ```
|
|
92
99
|
*/
|
|
93
|
-
export declare function
|
|
100
|
+
export declare function toHttpResponse(stream: AsyncIterable<StreamChunk>, init?: ResponseInit & {
|
|
94
101
|
abortController?: AbortController;
|
|
95
102
|
}): Response;
|
|
@@ -52,6 +52,24 @@ function toServerSentEventsStream(stream, abortController) {
|
|
|
52
52
|
}
|
|
53
53
|
});
|
|
54
54
|
}
|
|
55
|
+
function toServerSentEventsResponse(stream, init) {
|
|
56
|
+
const { headers, abortController, ...responseInit } = init ?? {};
|
|
57
|
+
const mergedHeaders = new Headers({
|
|
58
|
+
"Content-Type": "text/event-stream",
|
|
59
|
+
"Cache-Control": "no-cache",
|
|
60
|
+
Connection: "keep-alive"
|
|
61
|
+
});
|
|
62
|
+
if (headers) {
|
|
63
|
+
const userHeaders = new Headers(headers);
|
|
64
|
+
userHeaders.forEach((value, key) => {
|
|
65
|
+
mergedHeaders.set(key, value);
|
|
66
|
+
});
|
|
67
|
+
}
|
|
68
|
+
return new Response(toServerSentEventsStream(stream, abortController), {
|
|
69
|
+
...responseInit,
|
|
70
|
+
headers: mergedHeaders
|
|
71
|
+
});
|
|
72
|
+
}
|
|
55
73
|
function toHttpStream(stream, abortController) {
|
|
56
74
|
const encoder = new TextEncoder();
|
|
57
75
|
return new ReadableStream({
|
|
@@ -92,27 +110,16 @@ function toHttpStream(stream, abortController) {
|
|
|
92
110
|
}
|
|
93
111
|
});
|
|
94
112
|
}
|
|
95
|
-
function
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
"`toStreamResponse` is deprecated. Use `toServerSentEventsStream` instead. Example:\n const readableStream = toServerSentEventsStream(stream, abortController);\n return new Response(readableStream, {\n headers: { 'Content-Type': 'text/event-stream', 'Cache-Control': 'no-cache', Connection: 'keep-alive' }\n });"
|
|
99
|
-
);
|
|
100
|
-
}
|
|
101
|
-
const { headers, abortController, ...responseInit } = init ?? {};
|
|
102
|
-
return new Response(toServerSentEventsStream(stream, abortController), {
|
|
103
|
-
...responseInit,
|
|
104
|
-
headers: {
|
|
105
|
-
"Content-Type": "text/event-stream",
|
|
106
|
-
"Cache-Control": "no-cache",
|
|
107
|
-
Connection: "keep-alive",
|
|
108
|
-
...headers || {}
|
|
109
|
-
}
|
|
113
|
+
function toHttpResponse(stream, init) {
|
|
114
|
+
return new Response(toHttpStream(stream, init?.abortController), {
|
|
115
|
+
...init
|
|
110
116
|
});
|
|
111
117
|
}
|
|
112
118
|
export {
|
|
113
119
|
streamToText,
|
|
120
|
+
toHttpResponse,
|
|
114
121
|
toHttpStream,
|
|
115
|
-
|
|
116
|
-
|
|
122
|
+
toServerSentEventsResponse,
|
|
123
|
+
toServerSentEventsStream
|
|
117
124
|
};
|
|
118
125
|
//# sourceMappingURL=stream-to-response.js.map
|