From 0f67f6493683bf241de14dcb84ffefc8f6df4e50 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 11 Sep 2025 16:03:06 -0500 Subject: [PATCH] Make Tool inputSchema optional --- src/lib/SessionManager.ts | 9 ++++++++- src/lib/tools/BuildProjectTool.test.ts | 18 ++++++------------ src/lib/tools/BuildProjectTool.ts | 10 ++-------- src/lib/tools/NostrReadNipsIndexTool.ts | 8 ++------ src/lib/tools/Tool.ts | 2 +- src/lib/tools/TypecheckTool.test.ts | 14 ++++---------- src/lib/tools/TypecheckTool.ts | 12 +++--------- src/lib/tools/openai-adapter.ts | 19 ++++++++++++++----- 8 files changed, 40 insertions(+), 52 deletions(-) diff --git a/src/lib/SessionManager.ts b/src/lib/SessionManager.ts index bdd0d744..9ad7c1c3 100644 --- a/src/lib/SessionManager.ts +++ b/src/lib/SessionManager.ts @@ -325,7 +325,14 @@ export class SessionManager { } try { - const toolArgs = tool.inputSchema.parse(JSON.parse(functionToolCall.function.arguments)); + let toolArgs: unknown; + if (tool.inputSchema) { + toolArgs = tool.inputSchema.parse(JSON.parse(functionToolCall.function.arguments)); + } else { + // For tools without inputSchema, use empty object or parsed arguments as-is + const rawArgs = functionToolCall.function.arguments; + toolArgs = rawArgs ? JSON.parse(rawArgs) : {}; + } const content = await tool.execute(toolArgs); await this.addToolMessage(projectId, functionToolCall.id, content); } catch (error) { diff --git a/src/lib/tools/BuildProjectTool.test.ts b/src/lib/tools/BuildProjectTool.test.ts index acf481ac..b6aad023 100644 --- a/src/lib/tools/BuildProjectTool.test.ts +++ b/src/lib/tools/BuildProjectTool.test.ts @@ -27,20 +27,14 @@ describe('BuildProjectTool', () => { tool = new BuildProjectTool(mockFS, '/test/project'); }); - it('should have correct description and schema', () => { + it('should have correct description', () => { expect(tool.description).toBe('Build the project using esbuild. Creates optimized production files in the dist directory.'); - expect(tool.inputSchema).toBeDefined(); - }); - - it('should validate input schema correctly', () => { - // Valid inputs (empty object only) - expect(() => tool.inputSchema.parse({})).not.toThrow(); }); it('should fail if package.json does not exist', async () => { vi.mocked(mockFS.readFile).mockRejectedValue(new Error('File not found')); - await expect(tool.execute({})).rejects.toThrow( + await expect(tool.execute()).rejects.toThrow( '❌ Could not find package.json at /test/project. Make sure you\'re in a valid project directory.' ); }); @@ -50,7 +44,7 @@ describe('BuildProjectTool', () => { .mockResolvedValueOnce('{"name": "test"}') // package.json exists .mockRejectedValueOnce(new Error('File not found')); // index.html doesn't exist - await expect(tool.execute({})).rejects.toThrow( + await expect(tool.execute()).rejects.toThrow( '❌ Could not find index.html at /test/project. This is required for building the project.' ); }); @@ -69,7 +63,7 @@ describe('BuildProjectTool', () => { vi.mocked(mockFS.readdir).mockRejectedValue(new Error('Directory not found')); vi.mocked(buildProject).mockResolvedValue(mockDist); - const result = await tool.execute({}); + const result = await tool.execute(); expect(buildProject).toHaveBeenCalledWith({ fs: mockFS, @@ -104,7 +98,7 @@ describe('BuildProjectTool', () => { vi.mocked(mockFS.readdir).mockResolvedValue(['old-file.js', 'old-style.css']); vi.mocked(buildProject).mockResolvedValue(mockDist); - await tool.execute({}); + await tool.execute(); expect(mockFS.readdir).toHaveBeenCalledWith('/test/project/dist'); expect(mockFS.unlink).toHaveBeenCalledWith('/test/project/dist/old-file.js'); @@ -118,7 +112,7 @@ describe('BuildProjectTool', () => { vi.mocked(buildProject).mockRejectedValue(new Error('Build compilation failed')); - await expect(tool.execute({})).rejects.toThrow( + await expect(tool.execute()).rejects.toThrow( '❌ Build failed: Error: Build compilation failed' ); }); diff --git a/src/lib/tools/BuildProjectTool.ts b/src/lib/tools/BuildProjectTool.ts index 4907b0e8..3f58102c 100644 --- a/src/lib/tools/BuildProjectTool.ts +++ b/src/lib/tools/BuildProjectTool.ts @@ -1,25 +1,19 @@ -import { z } from "zod"; - import type { Tool } from "./Tool"; import type { JSRuntimeFS } from "../JSRuntime"; import { buildProject } from "../build"; -type BuildProjectParams = Record; - -export class BuildProjectTool implements Tool { +export class BuildProjectTool implements Tool { private fs: JSRuntimeFS; private cwd: string; readonly description = "Build the project using esbuild. Creates optimized production files in the dist directory."; - readonly inputSchema = z.object({}); - constructor(fs: JSRuntimeFS, cwd: string) { this.fs = fs; this.cwd = cwd; } - async execute(_args: BuildProjectParams): Promise { + async execute(): Promise { try { // Check if we're in a valid project directory try { diff --git a/src/lib/tools/NostrReadNipsIndexTool.ts b/src/lib/tools/NostrReadNipsIndexTool.ts index f5727534..4f0a3d6a 100644 --- a/src/lib/tools/NostrReadNipsIndexTool.ts +++ b/src/lib/tools/NostrReadNipsIndexTool.ts @@ -1,22 +1,18 @@ -import { z } from "zod"; - import type { Tool } from "./Tool"; import { NIPsClient } from "../NIPsClient"; -export class NostrReadNipsIndexTool implements Tool { +export class NostrReadNipsIndexTool implements Tool { private nipsClient: NIPsClient; readonly description = "Read the full list of NIPs, kinds, and tags"; - readonly inputSchema = z.object({}); - constructor() { this.nipsClient = new NIPsClient({ urlTemplate: 'https://raw.githubusercontent.com/nostr-protocol/nips/refs/heads/master/{nip}.md', }); } - async execute(_args: object): Promise { + async execute(): Promise { try { const text = await this.nipsClient.readIndex(); return text; diff --git a/src/lib/tools/Tool.ts b/src/lib/tools/Tool.ts index 451c87af..c0da6080 100644 --- a/src/lib/tools/Tool.ts +++ b/src/lib/tools/Tool.ts @@ -2,6 +2,6 @@ import type z from "zod"; export interface Tool { description: string; - inputSchema: z.ZodType; + inputSchema?: z.ZodType; execute(args: TParams): Promise; } diff --git a/src/lib/tools/TypecheckTool.test.ts b/src/lib/tools/TypecheckTool.test.ts index f4a92cc5..a22dcd5c 100644 --- a/src/lib/tools/TypecheckTool.test.ts +++ b/src/lib/tools/TypecheckTool.test.ts @@ -16,20 +16,14 @@ describe('TypecheckTool', () => { tool = new TypecheckTool(mockFS, '/test/project'); }); - it('should have correct description and schema', () => { + it('should have correct description', () => { expect(tool.description).toBe('Run TypeScript type checking on the project to verify there are no type errors.'); - expect(tool.inputSchema).toBeDefined(); - }); - - it('should validate input schema correctly', () => { - // Valid inputs (empty object only) - expect(() => tool.inputSchema.parse({})).not.toThrow(); }); it('should fail if tsconfig.json does not exist', async () => { vi.mocked(mockFS.readFile).mockRejectedValue(new Error('File not found')); - await expect(tool.execute({})).rejects.toThrow( + await expect(tool.execute()).rejects.toThrow( '❌ Could not find tsconfig.json at /test/project. Make sure you\'re in a valid TypeScript project.' ); }); @@ -37,7 +31,7 @@ describe('TypecheckTool', () => { it('should successfully return no type errors (stub implementation)', async () => { vi.mocked(mockFS.readFile).mockResolvedValue('{"compilerOptions": {}}'); // tsconfig.json exists - const result = await tool.execute({}); + const result = await tool.execute(); expect(result).toContain('✅ No type errors found.'); expect(result).toContain('🔍 TypeScript compilation completed successfully.'); @@ -47,7 +41,7 @@ describe('TypecheckTool', () => { it('should handle file system errors gracefully', async () => { vi.mocked(mockFS.readFile).mockRejectedValue(new Error('Permission denied')); - await expect(tool.execute({})).rejects.toThrow( + await expect(tool.execute()).rejects.toThrow( '❌ Could not find tsconfig.json at /test/project. Make sure you\'re in a valid TypeScript project.' ); }); diff --git a/src/lib/tools/TypecheckTool.ts b/src/lib/tools/TypecheckTool.ts index 6cb2af45..629e4a81 100644 --- a/src/lib/tools/TypecheckTool.ts +++ b/src/lib/tools/TypecheckTool.ts @@ -1,28 +1,22 @@ -import { z } from "zod"; - import type { Tool } from "./Tool"; import type { JSRuntimeFS } from "../JSRuntime"; -type TypecheckParams = Record; - -export class TypecheckTool implements Tool { +export class TypecheckTool implements Tool { private fs: JSRuntimeFS; private cwd: string; readonly description = "Run TypeScript type checking on the project to verify there are no type errors."; - readonly inputSchema = z.object({}); - constructor(fs: JSRuntimeFS, cwd: string) { this.fs = fs; this.cwd = cwd; } - async execute(_args: TypecheckParams): Promise { + async execute(): Promise { // TODO: Add actual typechecking functionality // This should run TypeScript compiler in --noEmit mode to check for type errors // Similar to how the test script runs: tsc --noEmit - + try { // Check if we're in a valid TypeScript project try { diff --git a/src/lib/tools/openai-adapter.ts b/src/lib/tools/openai-adapter.ts index acdab324..3ae99d7c 100644 --- a/src/lib/tools/openai-adapter.ts +++ b/src/lib/tools/openai-adapter.ts @@ -5,13 +5,22 @@ import { Tool } from './Tool'; * Convert a custom Tool to OpenAI's ChatCompletionTool format */ export function toolToOpenAI(name: string, tool: Tool): OpenAI.Chat.Completions.ChatCompletionTool { + const functionDef: { + name: string; + description: string; + parameters?: Record; + } = { + name, + description: tool.description, + }; + + if (tool.inputSchema) { + functionDef.parameters = zodSchemaToJsonSchema(tool.inputSchema); + } + return { type: 'function', - function: { - name, - description: tool.description, - parameters: zodSchemaToJsonSchema(tool.inputSchema) - } + function: functionDef }; }