mirror of
https://gitlab.com/soapbox-pub/shakespeare.git
synced 2026-12-06 00:27:24 +00:00
Fix tool argument parsing
This commit is contained in:
5 files changed
+64
-67
No files matched your search
@@ -0,0 +1,21 @@
|
||||
import OpenAI from "openai";
|
||||
|
||||
export function assistantContentEmpty(
|
||||
content: OpenAI.ChatCompletionAssistantMessageParam['content'],
|
||||
): boolean {
|
||||
if (!content) return true;
|
||||
const text: string[] = [];
|
||||
|
||||
if (typeof content === 'string') {
|
||||
text.push(content);
|
||||
} else if (Array.isArray(content)) {
|
||||
for (const part of content) {
|
||||
if (part.type === 'text') {
|
||||
text.push(part.text);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const result = text.join('\n\n');
|
||||
return !result.trim();
|
||||
}
|
||||
@@ -3,5 +3,5 @@ import type z from "zod";
|
||||
export interface Tool<TParams> {
|
||||
description: string;
|
||||
inputSchema: z.ZodType<TParams>;
|
||||
execute(args: TParams): Promise<unknown>;
|
||||
execute(args: TParams): Promise<string>;
|
||||
}
|
||||
@@ -96,27 +96,4 @@ function convertZodField(field: unknown): Record<string, unknown> {
|
||||
description: fieldObj._def?.description || ''
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Execute a tool with the given arguments
|
||||
*/
|
||||
export async function executeTool<TParams>(
|
||||
tool: Tool<TParams>,
|
||||
args: unknown
|
||||
): Promise<string> {
|
||||
try {
|
||||
// Parse and validate arguments using the tool's schema
|
||||
const validatedArgs = tool.inputSchema.parse(args);
|
||||
const result = await tool.execute(validatedArgs);
|
||||
|
||||
// Convert result to string for the AI
|
||||
if (typeof result === 'string') {
|
||||
return result;
|
||||
}
|
||||
|
||||
return JSON.stringify(result, null, 2);
|
||||
} catch (error) {
|
||||
throw new Error(`Tool execution failed: ${error instanceof Error ? error.message : 'Unknown error'}`);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user