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

+6 -12
View File
@@ -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'
);
});
+2 -8
View File
@@ -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<string, never>;
export class BuildProjectTool implements Tool<BuildProjectParams> {
export class BuildProjectTool implements Tool<void> {
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<string> {
async execute(): Promise<string> {
try {
// Check if we're in a valid project directory
try {
+2 -6
View File
@@ -1,22 +1,18 @@
import { z } from "zod";
import type { Tool } from "./Tool";
import { NIPsClient } from "../NIPsClient";
export class NostrReadNipsIndexTool implements Tool<object> {
export class NostrReadNipsIndexTool implements Tool<void> {
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<string> {
async execute(): Promise<string> {
try {
const text = await this.nipsClient.readIndex();
return text;
+1 -1
View File
@@ -2,6 +2,6 @@ import type z from "zod";
export interface Tool<TParams> {
description: string;
inputSchema: z.ZodType<TParams>;
inputSchema?: z.ZodType<TParams>;
execute(args: TParams): Promise<string>;
}
+4 -10
View File
@@ -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.'
);
});
+3 -9
View File
@@ -1,28 +1,22 @@
import { z } from "zod";
import type { Tool } from "./Tool";
import type { JSRuntimeFS } from "../JSRuntime";
type TypecheckParams = Record<string, never>;
export class TypecheckTool implements Tool<TypecheckParams> {
export class TypecheckTool implements Tool<void> {
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<string> {
async execute(): Promise<string> {
// 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 {
+14 -5
View File
@@ -5,13 +5,22 @@ import { Tool } from './Tool';
* Convert a custom Tool to OpenAI's ChatCompletionTool format
*/
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 {
type: 'function',
function: {
name,
description: tool.description,
parameters: zodSchemaToJsonSchema(tool.inputSchema)
}
function: functionDef
};
}