Make Tool inputSchema optional

This commit is contained in:
Alex Gleason committed 2025-09-11 16:03:06 -05:00
1 parent ba2cfe7b68
commit 0f67f64936
8 files changed
+40 -52

No files matched your search

+8 -1
View File
@@ -325,7 +325,14 @@ export class SessionManager {
} }
try { 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); const content = await tool.execute(toolArgs);
await this.addToolMessage(projectId, functionToolCall.id, content); await this.addToolMessage(projectId, functionToolCall.id, content);
} catch (error) { } catch (error) {
+6 -12
View File
@@ -27,20 +27,14 @@ describe('BuildProjectTool', () => {
tool = new BuildProjectTool(mockFS, '/test/project'); 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.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 () => { it('should fail if package.json does not exist', async () => {
vi.mocked(mockFS.readFile).mockRejectedValue(new Error('File not found')); 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.' '❌ 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 .mockResolvedValueOnce('{"name": "test"}') // package.json exists
.mockRejectedValueOnce(new Error('File not found')); // index.html doesn't exist .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.' '❌ 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(mockFS.readdir).mockRejectedValue(new Error('Directory not found'));
vi.mocked(buildProject).mockResolvedValue(mockDist); vi.mocked(buildProject).mockResolvedValue(mockDist);
const result = await tool.execute({}); const result = await tool.execute();
expect(buildProject).toHaveBeenCalledWith({ expect(buildProject).toHaveBeenCalledWith({
fs: mockFS, fs: mockFS,
@@ -104,7 +98,7 @@ describe('BuildProjectTool', () => {
vi.mocked(mockFS.readdir).mockResolvedValue(['old-file.js', 'old-style.css']); vi.mocked(mockFS.readdir).mockResolvedValue(['old-file.js', 'old-style.css']);
vi.mocked(buildProject).mockResolvedValue(mockDist); vi.mocked(buildProject).mockResolvedValue(mockDist);
await tool.execute({}); await tool.execute();
expect(mockFS.readdir).toHaveBeenCalledWith('/test/project/dist'); expect(mockFS.readdir).toHaveBeenCalledWith('/test/project/dist');
expect(mockFS.unlink).toHaveBeenCalledWith('/test/project/dist/old-file.js'); 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')); 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' '❌ Build failed: Error: Build compilation failed'
); );
}); });
+2 -8
View File
@@ -1,25 +1,19 @@
import { z } from "zod";
import type { Tool } from "./Tool"; import type { Tool } from "./Tool";
import type { JSRuntimeFS } from "../JSRuntime"; import type { JSRuntimeFS } from "../JSRuntime";
import { buildProject } from "../build"; import { buildProject } from "../build";
type BuildProjectParams = Record<string, never>; export class BuildProjectTool implements Tool<void> {
export class BuildProjectTool implements Tool<BuildProjectParams> {
private fs: JSRuntimeFS; private fs: JSRuntimeFS;
private cwd: string; private cwd: string;
readonly description = "Build the project using esbuild. Creates optimized production files in the dist directory."; readonly description = "Build the project using esbuild. Creates optimized production files in the dist directory.";
readonly inputSchema = z.object({});
constructor(fs: JSRuntimeFS, cwd: string) { constructor(fs: JSRuntimeFS, cwd: string) {
this.fs = fs; this.fs = fs;
this.cwd = cwd; this.cwd = cwd;
} }
async execute(_args: BuildProjectParams): Promise<string> { async execute(): Promise<string> {
try { try {
// Check if we're in a valid project directory // Check if we're in a valid project directory
try { try {
+2 -6
View File
@@ -1,22 +1,18 @@
import { z } from "zod";
import type { Tool } from "./Tool"; import type { Tool } from "./Tool";
import { NIPsClient } from "../NIPsClient"; import { NIPsClient } from "../NIPsClient";
export class NostrReadNipsIndexTool implements Tool<object> { export class NostrReadNipsIndexTool implements Tool<void> {
private nipsClient: NIPsClient; private nipsClient: NIPsClient;
readonly description = "Read the full list of NIPs, kinds, and tags"; readonly description = "Read the full list of NIPs, kinds, and tags";
readonly inputSchema = z.object({});
constructor() { constructor() {
this.nipsClient = new NIPsClient({ this.nipsClient = new NIPsClient({
urlTemplate: 'https://raw.githubusercontent.com/nostr-protocol/nips/refs/heads/master/{nip}.md', urlTemplate: 'https://raw.githubusercontent.com/nostr-protocol/nips/refs/heads/master/{nip}.md',
}); });
} }
async execute(_args: object): Promise<string> { async execute(): Promise<string> {
try { try {
const text = await this.nipsClient.readIndex(); const text = await this.nipsClient.readIndex();
return text; return text;
+1 -1
View File
@@ -2,6 +2,6 @@ import type z from "zod";
export interface Tool<TParams> { export interface Tool<TParams> {
description: string; description: string;
inputSchema: z.ZodType<TParams>; inputSchema?: z.ZodType<TParams>;
execute(args: TParams): Promise<string>; execute(args: TParams): Promise<string>;
} }
+4 -10
View File
@@ -16,20 +16,14 @@ describe('TypecheckTool', () => {
tool = new TypecheckTool(mockFS, '/test/project'); 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.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 () => { it('should fail if tsconfig.json does not exist', async () => {
vi.mocked(mockFS.readFile).mockRejectedValue(new Error('File not found')); 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.' '❌ 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 () => { it('should successfully return no type errors (stub implementation)', async () => {
vi.mocked(mockFS.readFile).mockResolvedValue('{"compilerOptions": {}}'); // tsconfig.json exists 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('✅ No type errors found.');
expect(result).toContain('🔍 TypeScript compilation completed successfully.'); expect(result).toContain('🔍 TypeScript compilation completed successfully.');
@@ -47,7 +41,7 @@ describe('TypecheckTool', () => {
it('should handle file system errors gracefully', async () => { it('should handle file system errors gracefully', async () => {
vi.mocked(mockFS.readFile).mockRejectedValue(new Error('Permission denied')); 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.' '❌ Could not find tsconfig.json at /test/project. Make sure you\'re in a valid TypeScript project.'
); );
}); });
+3 -9
View File
@@ -1,28 +1,22 @@
import { z } from "zod";
import type { Tool } from "./Tool"; import type { Tool } from "./Tool";
import type { JSRuntimeFS } from "../JSRuntime"; import type { JSRuntimeFS } from "../JSRuntime";
type TypecheckParams = Record<string, never>; export class TypecheckTool implements Tool<void> {
export class TypecheckTool implements Tool<TypecheckParams> {
private fs: JSRuntimeFS; private fs: JSRuntimeFS;
private cwd: string; private cwd: string;
readonly description = "Run TypeScript type checking on the project to verify there are no type errors."; 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) { constructor(fs: JSRuntimeFS, cwd: string) {
this.fs = fs; this.fs = fs;
this.cwd = cwd; this.cwd = cwd;
} }
async execute(_args: TypecheckParams): Promise<string> { async execute(): Promise<string> {
// TODO: Add actual typechecking functionality // TODO: Add actual typechecking functionality
// This should run TypeScript compiler in --noEmit mode to check for type errors // This should run TypeScript compiler in --noEmit mode to check for type errors
// Similar to how the test script runs: tsc --noEmit // Similar to how the test script runs: tsc --noEmit
try { try {
// Check if we're in a valid TypeScript project // Check if we're in a valid TypeScript project
try { try {
+14 -5
View File
@@ -5,13 +5,22 @@ import { Tool } from './Tool';
* Convert a custom Tool to OpenAI's ChatCompletionTool format * Convert a custom Tool to OpenAI's ChatCompletionTool format
*/ */
export function toolToOpenAI<TParams>(name: string, tool: Tool<TParams>): OpenAI.Chat.Completions.ChatCompletionTool { export function toolToOpenAI<TParams>(name: string, tool: Tool<TParams>): OpenAI.Chat.Completions.ChatCompletionTool {
const functionDef: {
name: string;
description: string;
parameters?: Record<string, unknown>;
} = {
name,
description: tool.description,
};
if (tool.inputSchema) {
functionDef.parameters = zodSchemaToJsonSchema(tool.inputSchema);
}
return { return {
type: 'function', type: 'function',
function: { function: functionDef
name,
description: tool.description,
parameters: zodSchemaToJsonSchema(tool.inputSchema)
}
}; };
} }