Files
archived-gitea-ai-assistant/src/review/tools/registry.ts
jeffusion 318e6d3688 build: replace tslint with Biome for code quality
- Add @biomejs/biome as dev dependency
- Remove deprecated tslint dependency
- Add biome.json with project-specific rules
- Update lint script to use Biome
- Apply Biome auto-fixes across codebase
2026-03-03 17:03:23 +08:00

53 lines
1.5 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { z } from 'zod';
import zodToJsonSchema from 'zod-to-json-schema';
import type { JsonSchema7Type } from 'zod-to-json-schema';
import { Tool } from './types';
export class ToolRegistry {
private tools = new Map<string, Tool>();
register(tool: Tool): void {
this.tools.set(tool.name, tool);
}
get(name: string): Tool | undefined {
return this.tools.get(name);
}
getAll(): Tool[] {
return Array.from(this.tools.values());
}
// 转换为OpenAI Function定义
toOpenAIFunctions() {
return this.getAll().map((tool) => ({
type: 'function' as const,
function: {
name: tool.name,
description: tool.description,
parameters: this.zodToJsonSchema(tool.parameters),
},
}));
}
private zodToJsonSchema(schema: z.ZodTypeAny): JsonSchema7Type {
/**
* 使用zod-to-json-schema库转换Zod schema为JSON Schema。
*
* 注意该库v3.25.1使用了复杂的条件类型推断,会导致 TS2589
* "Type instantiation is excessively deep" 错误。这是库的已知限制,
* 见 https://github.com/StefanTerdell/zod-to-json-schema/issues
*
* 类型安全保证:
* - 输入z.ZodTypeAny 确保只接受Zod schema
* - 输出JsonSchema7Type 明确返回类型
* - 运行时行为:库本身经过充分测试,转换逻辑正确
*/
// @ts-expect-error TS2589: zod-to-json-schema v3.25.1 的条件类型过于复杂
return zodToJsonSchema(schema, {
target: 'openApi3',
$refStrategy: 'none',
});
}
}