diff --git a/legacy/packages/core/src/create-ai-chain.ts b/legacy/packages/core/src/create-ai-chain.ts index d321416f..1ecce4a5 100644 --- a/legacy/packages/core/src/create-ai-chain.ts +++ b/legacy/packages/core/src/create-ai-chain.ts @@ -195,14 +195,8 @@ export function createAIChain({ } else if (Msg.isRefusal(message)) { throw new AbortError(`Model refusal: ${message.refusal}`) } else if (Msg.isAssistant(message)) { - if (schema && schema.validate) { - const result = schema.validate(message.content) - - if (result.success) { - return result.data - } - - throw new Error(result.error) + if (schema) { + return schema.parse(message.content) } else { return message.content as Result } diff --git a/legacy/packages/core/src/create-ai-function.ts b/legacy/packages/core/src/create-ai-function.ts index b0edea82..af00da33 100644 --- a/legacy/packages/core/src/create-ai-function.ts +++ b/legacy/packages/core/src/create-ai-function.ts @@ -20,7 +20,7 @@ export function createAIFunction, Output>( name: string /** Description of the function. */ description?: string - /** Zod schema for the arguments string. */ + /** Zod schema for the function parameters. */ inputSchema: InputSchema /** * Whether or not to enable structured output generation based on the given diff --git a/legacy/packages/core/src/schema.ts b/legacy/packages/core/src/schema.ts index efb5bc06..53ab248e 100644 --- a/legacy/packages/core/src/schema.ts +++ b/legacy/packages/core/src/schema.ts @@ -1,7 +1,7 @@ import type { z } from 'zod' import type * as types from './types' -import { safeParseStructuredOutput } from './parse-structured-output' +import { parseStructuredOutput } from './parse-structured-output' import { stringifyForModel } from './utils' import { zodToJsonSchema } from './zod-to-json-schema' @@ -9,7 +9,6 @@ import { zodToJsonSchema } from './zod-to-json-schema' * Used to mark schemas so we can support both Zod and custom schemas. */ export const schemaSymbol = Symbol('agentic.schema') -export const validatorSymbol = Symbol('agentic.validator') export type Schema = { /** @@ -18,10 +17,18 @@ export type Schema = { readonly jsonSchema: types.JSONSchema /** - * Optional. Validates that the structure of a value matches this schema, - * and returns a typed version of the value if it does. + * Parses the value, validates that it matches this schema, and returns a + * typed version of the value if it does. Throw an error if the value does + * not match the schema. */ - readonly validate?: types.ValidatorFn + readonly parse: types.ParseFn + + /** + * Parses the value, validates that it matches this schema, and returns a + * typed version of the value if it does. Returns an error message if the + * value does not match the schema, and will never throw an error. + */ + readonly safeParse: types.SafeParseFn /** * Used to mark schemas so we can support both Zod and custom schemas. @@ -41,7 +48,7 @@ export function isSchema(value: unknown): value is Schema { schemaSymbol in value && value[schemaSymbol] === true && 'jsonSchema' in value && - 'validate' in value + 'parse' in value ) } @@ -71,16 +78,28 @@ export function asSchema( export function createSchema( jsonSchema: types.JSONSchema, { - validate + parse = (value) => value as TData, + safeParse }: { - validate?: types.ValidatorFn + parse?: types.ParseFn + safeParse?: types.SafeParseFn } = {} ): Schema { + safeParse ??= (value: unknown) => { + try { + const result = parse(value) + return { success: true, data: result } + } catch (err: any) { + return { success: false, error: err.message ?? String(err) } + } + } + return { [schemaSymbol]: true, _type: undefined as TData, jsonSchema, - validate + parse, + safeParse } } @@ -89,8 +108,8 @@ export function createSchemaFromZodSchema( opts: { strict?: boolean } = {} ): Schema { return createSchema(zodToJsonSchema(zodSchema, opts), { - validate: (value) => { - return safeParseStructuredOutput(value, zodSchema) + parse: (value) => { + return parseStructuredOutput(value, zodSchema) } }) } diff --git a/legacy/packages/core/src/types.ts b/legacy/packages/core/src/types.ts index 0db309a7..92424e4f 100644 --- a/legacy/packages/core/src/types.ts +++ b/legacy/packages/core/src/types.ts @@ -196,4 +196,5 @@ export type SafeParseResult = error: string } -export type ValidatorFn = (value: unknown) => SafeParseResult +export type ParseFn = (value: unknown) => TData +export type SafeParseFn = (value: unknown) => SafeParseResult