feat: adapt pullfrog for gitea + ollama
This commit is contained in:
+8
-19
@@ -1,10 +1,11 @@
|
||||
import type { StandardSchemaV1 } from "@standard-schema/spec";
|
||||
import { encode as toonEncode } from "@toon-format/toon";
|
||||
import type { FastMCP, Tool } from "fastmcp";
|
||||
import { formatJsonValue, log } from "../utils/cli.ts";
|
||||
import { isGeminiRouted, sanitizeToolForGemini } from "./geminiSanitizer.ts";
|
||||
import type { ToolContext } from "./server.ts";
|
||||
|
||||
// Tool<any, any> is intentional: the tools array is a heterogeneous collection
|
||||
// where each tool has a different typed params schema. TypeScript's contravariance
|
||||
// rules make it impossible to express this without any in the generic position.
|
||||
export const tool = <const params>(
|
||||
toolDef: Tool<any, StandardSchemaV1<params>>
|
||||
): Tool<any, StandardSchemaV1<params>> => toolDef;
|
||||
@@ -17,8 +18,8 @@ export interface ToolResult {
|
||||
isError?: boolean;
|
||||
}
|
||||
|
||||
export const handleToolSuccess = (data: Record<string, any> | string): ToolResult => {
|
||||
const text = typeof data === "string" ? data : toonEncode(data);
|
||||
export const handleToolSuccess = (data: Record<string, unknown> | string): ToolResult => {
|
||||
const text = typeof data === "string" ? data : JSON.stringify(data, null, 2);
|
||||
return {
|
||||
content: [{ type: "text", text }],
|
||||
};
|
||||
@@ -27,23 +28,12 @@ export const handleToolSuccess = (data: Record<string, any> | string): ToolResul
|
||||
export const handleToolError = (error: unknown): ToolResult => {
|
||||
const errorMessage = error instanceof Error ? error.message : String(error);
|
||||
return {
|
||||
content: [
|
||||
{
|
||||
type: "text",
|
||||
text: `Error: ${errorMessage}`,
|
||||
},
|
||||
],
|
||||
content: [{ type: "text", text: `Error: ${errorMessage}` }],
|
||||
isError: true,
|
||||
};
|
||||
};
|
||||
|
||||
/**
|
||||
* Helper to wrap a tool execute function with error handling.
|
||||
* Captures ctx in closure so tools don't need to handle try/catch.
|
||||
* @param fn - the function to execute
|
||||
* @param toolName - optional tool name for error logging
|
||||
*/
|
||||
export const execute = <T, R extends Record<string, any> | string>(
|
||||
export const execute = <T, R extends Record<string, unknown> | string>(
|
||||
fn: (params: T) => Promise<R>,
|
||||
toolName?: string
|
||||
) => {
|
||||
@@ -63,9 +53,8 @@ export const execute = <T, R extends Record<string, any> | string>(
|
||||
};
|
||||
|
||||
export const addTools = (ctx: ToolContext, server: FastMCP<any>, tools: Tool<any, any>[]) => {
|
||||
const shouldSanitize = isGeminiRouted(ctx);
|
||||
for (const tool of tools) {
|
||||
server.addTool(shouldSanitize ? sanitizeToolForGemini(tool) : tool);
|
||||
server.addTool(tool);
|
||||
}
|
||||
return server;
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user