43 lines
957 B
TypeScript
43 lines
957 B
TypeScript
import { z } from "zod";
|
|
import type { Tool } from "./tool.js";
|
|
|
|
export interface OpenAiToolDefinition {
|
|
type: "function";
|
|
function: {
|
|
name: string;
|
|
description: string;
|
|
parameters: unknown;
|
|
};
|
|
}
|
|
|
|
function toToolDescriptor(tool: Tool<unknown, unknown>): {
|
|
name: string;
|
|
description: string;
|
|
parameters: unknown;
|
|
} {
|
|
return {
|
|
name: tool.name,
|
|
description: tool.description,
|
|
parameters: z.toJSONSchema(tool.argsSchema),
|
|
};
|
|
}
|
|
|
|
export function serializeToolsForHermes(
|
|
tools: readonly Tool<unknown, unknown>[],
|
|
): string {
|
|
const descriptors = tools.map(toToolDescriptor);
|
|
return `<tools>\n${JSON.stringify(descriptors, null, 2)}\n</tools>`;
|
|
}
|
|
|
|
export function serializeToolsForOpenAi(
|
|
tools: readonly Tool<unknown, unknown>[],
|
|
): OpenAiToolDefinition[] {
|
|
return tools.map((tool) => {
|
|
const descriptor = toToolDescriptor(tool);
|
|
return {
|
|
type: "function",
|
|
function: descriptor,
|
|
};
|
|
});
|
|
}
|