mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Upgrade to zod 4
This commit is contained in:
5 files changed
+65
-169
No files matched your search
@@ -22,7 +22,7 @@ describe('openai-adapter', () => {
|
||||
expect(result.type).toBe('function');
|
||||
expect(result.function.name).toBe('test_tool');
|
||||
expect(result.function.description).toBe('Test tool');
|
||||
expect(result.function.parameters).toEqual({
|
||||
expect(result.function.parameters).toMatchObject({
|
||||
type: 'object',
|
||||
properties: {
|
||||
message: {
|
||||
@@ -50,7 +50,7 @@ describe('openai-adapter', () => {
|
||||
|
||||
const result = toolToOpenAI('user_tool', tool) as OpenAI.Chat.Completions.ChatCompletionTool & { type: 'function' };
|
||||
|
||||
expect(result.function.parameters).toEqual({
|
||||
expect(result.function.parameters).toMatchObject({
|
||||
type: 'object',
|
||||
properties: {
|
||||
user: {
|
||||
@@ -92,7 +92,7 @@ describe('openai-adapter', () => {
|
||||
|
||||
const result = toolToOpenAI('todo_tool', tool) as OpenAI.Chat.Completions.ChatCompletionTool & { type: 'function' };
|
||||
|
||||
expect(result.function.parameters).toEqual({
|
||||
expect(result.function.parameters).toMatchObject({
|
||||
type: 'object',
|
||||
properties: {
|
||||
todos: {
|
||||
@@ -137,7 +137,7 @@ describe('openai-adapter', () => {
|
||||
|
||||
const result = toolToOpenAI('test_tool', tool) as OpenAI.Chat.Completions.ChatCompletionTool & { type: 'function' };
|
||||
|
||||
expect(result.function.parameters).toEqual({
|
||||
expect(result.function.parameters).toMatchObject({
|
||||
type: 'object',
|
||||
properties: {
|
||||
required: {
|
||||
@@ -182,7 +182,7 @@ describe('openai-adapter', () => {
|
||||
|
||||
const result = toolToOpenAI('priority_tool', tool) as OpenAI.Chat.Completions.ChatCompletionTool & { type: 'function' };
|
||||
|
||||
expect(result.function.parameters).toEqual({
|
||||
expect(result.function.parameters).toMatchObject({
|
||||
type: 'object',
|
||||
properties: {
|
||||
priority: {
|
||||
|
||||
@@ -15,7 +15,7 @@ export function toolToOpenAI<TParams>(name: string, tool: Tool<TParams>): OpenAI
|
||||
};
|
||||
|
||||
if (tool.inputSchema) {
|
||||
functionDef.parameters = zodSchemaToJsonSchema(tool.inputSchema);
|
||||
functionDef.parameters = tool.inputSchema.toJSONSchema();
|
||||
} else {
|
||||
functionDef.parameters = { type: "object", properties: {} };
|
||||
}
|
||||
@@ -25,125 +25,3 @@ export function toolToOpenAI<TParams>(name: string, tool: Tool<TParams>): OpenAI
|
||||
function: functionDef
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert a Zod schema to JSON Schema format for OpenAI
|
||||
*/
|
||||
function zodSchemaToJsonSchema(schema: unknown): Record<string, unknown> {
|
||||
// This is a simplified conversion. For a full implementation,
|
||||
// you might want to use a library like zod-to-json-schema
|
||||
const schemaObj = schema as { _def?: { typeName?: string; shape?: () => Record<string, unknown> } };
|
||||
|
||||
if (schemaObj._def?.typeName === 'ZodObject') {
|
||||
const properties: Record<string, unknown> = {};
|
||||
const required: string[] = [];
|
||||
|
||||
const shape = schemaObj._def.shape?.();
|
||||
if (shape) {
|
||||
for (const [key, value] of Object.entries(shape)) {
|
||||
const fieldSchema = value as { _def?: { typeName?: string } };
|
||||
properties[key] = convertZodField(fieldSchema);
|
||||
|
||||
if (!fieldSchema._def?.typeName?.includes('Optional')) {
|
||||
required.push(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'object',
|
||||
properties,
|
||||
required: required.length > 0 ? required : undefined
|
||||
};
|
||||
}
|
||||
|
||||
return { type: 'object' };
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert individual Zod field to JSON Schema
|
||||
*/
|
||||
function convertZodField(field: unknown): Record<string, unknown> {
|
||||
const fieldObj = field as {
|
||||
_def?: {
|
||||
typeName?: string;
|
||||
description?: string;
|
||||
checks?: Array<{ kind: string; value: unknown }>;
|
||||
innerType?: unknown;
|
||||
type?: unknown;
|
||||
shape?: () => Record<string, unknown>;
|
||||
values?: unknown[];
|
||||
}
|
||||
};
|
||||
|
||||
const typeName = fieldObj._def?.typeName;
|
||||
|
||||
switch (typeName) {
|
||||
case 'ZodString':
|
||||
return {
|
||||
type: 'string',
|
||||
description: fieldObj._def?.description
|
||||
};
|
||||
case 'ZodNumber':
|
||||
return {
|
||||
type: 'number',
|
||||
description: fieldObj._def?.description,
|
||||
minimum: fieldObj._def?.checks?.find((c) => c.kind === 'min')?.value
|
||||
};
|
||||
case 'ZodBoolean':
|
||||
return {
|
||||
type: 'boolean',
|
||||
description: fieldObj._def?.description
|
||||
};
|
||||
case 'ZodOptional': {
|
||||
const innerSchema = convertZodField(fieldObj._def?.innerType);
|
||||
// Preserve description from ZodOptional if it exists
|
||||
if (fieldObj._def?.description && !innerSchema.description) {
|
||||
innerSchema.description = fieldObj._def.description;
|
||||
}
|
||||
return innerSchema;
|
||||
}
|
||||
case 'ZodArray':
|
||||
return {
|
||||
type: 'array',
|
||||
items: convertZodField(fieldObj._def?.type),
|
||||
description: fieldObj._def?.description
|
||||
};
|
||||
case 'ZodObject': {
|
||||
const properties: Record<string, unknown> = {};
|
||||
const required: string[] = [];
|
||||
|
||||
const shape = fieldObj._def?.shape?.();
|
||||
if (shape) {
|
||||
for (const [key, value] of Object.entries(shape)) {
|
||||
const fieldSchema = value as { _def?: { typeName?: string } };
|
||||
properties[key] = convertZodField(fieldSchema);
|
||||
|
||||
if (!fieldSchema._def?.typeName?.includes('Optional')) {
|
||||
required.push(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
type: 'object',
|
||||
properties,
|
||||
required: required.length > 0 ? required : undefined,
|
||||
description: fieldObj._def?.description
|
||||
};
|
||||
}
|
||||
case 'ZodEnum': {
|
||||
const values = fieldObj._def?.values as string[] | undefined;
|
||||
return {
|
||||
type: 'string',
|
||||
enum: values || [],
|
||||
description: fieldObj._def?.description
|
||||
};
|
||||
}
|
||||
default:
|
||||
return {
|
||||
type: 'string',
|
||||
description: fieldObj._def?.description || ''
|
||||
};
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user