xonecas.io
53129
Pa

PAT / commit 68f55a4

feat: add runtime validation for tool call arguments

xonecas2025-12-19 68f55a4c1661536d92f9199645ac876ba7c9d0d1 parent d57a8e9

1 file changed

src/lib/ollama/tools/tool-runner.ts+103−6
@@ -4,6 +4,95 @@ import { type Message, type ToolCall } from "ollama";
44 import { debug } from "@/src/lib/debug";
55 import { queryDuckDuckGo, fetchPage, queryWeather, queryWebSearch } from "@/src/lib/ollama/tools";
66
7+/**
8+ * Validates that a value is a non-empty string.
9+ */
10+function isValidString(value: unknown): value is string {
11+ return typeof value === "string" && value.trim().length > 0;
12+}
13+
14+/**
15+ * Validates that a value is a valid timezone string or "auto".
16+ */
17+function isValidTimezone(value: unknown): value is string {
18+ return typeof value === "string" && (value === "auto" || value.trim().length > 0);
19+}
20+
21+/**
22+ * Validates that a value is a valid forecast days number (1-16).
23+ */
24+function isValidForecastDays(value: unknown): value is number {
25+ return typeof value === "number" && Number.isInteger(value) && value >= 1 && value <= 16;
26+}
27+
28+/**
29+ * Validates that a value is a valid URL string.
30+ */
31+function isValidUrl(value: unknown): value is string {
32+ if (!isValidString(value)) {
33+ return false;
34+ }
35+ try {
36+ new URL(value);
37+ return true;
38+ } catch {
39+ return false;
40+ }
41+}
42+
43+/**
44+ * Validates tool call arguments based on the tool name.
45+ * Returns a validation error message if invalid, or null if valid.
46+ */
47+function validateToolArguments(
48+ name: string,
49+ args: unknown
50+): { query?: string; url?: string; timezone?: string; forecastDays?: number } | string {
51+ if (typeof args !== "object" || args === null) {
52+ return `Invalid arguments: expected object, got ${typeof args}`;
53+ }
54+
55+ const argObj = args as Record<string, unknown>;
56+
57+ if (name === "query_weather") {
58+ if (!isValidString(argObj.query)) {
59+ return "Missing or invalid 'query' parameter (must be a non-empty string)";
60+ }
61+ const validated: { query: string; timezone?: string; forecastDays?: number } = {
62+ query: argObj.query,
63+ };
64+ if (argObj.timezone !== undefined) {
65+ if (!isValidTimezone(argObj.timezone)) {
66+ return "Invalid 'timezone' parameter (must be a string or 'auto')";
67+ }
68+ validated.timezone = argObj.timezone;
69+ }
70+ if (argObj.forecastDays !== undefined) {
71+ if (!isValidForecastDays(argObj.forecastDays)) {
72+ return "Invalid 'forecastDays' parameter (must be an integer between 1 and 16)";
73+ }
74+ validated.forecastDays = argObj.forecastDays;
75+ }
76+ return validated;
77+ }
78+
79+ if (name === "fetch_page") {
80+ if (!isValidUrl(argObj.url)) {
81+ return "Missing or invalid 'url' parameter (must be a valid URL string)";
82+ }
83+ return { url: argObj.url };
84+ }
85+
86+ if (name === "query_web_search" || name === "query_duckduckgo") {
87+ if (!isValidString(argObj.query)) {
88+ return "Missing or invalid 'query' parameter (must be a non-empty string)";
89+ }
90+ return { query: argObj.query };
91+ }
92+
93+ return `Unknown tool: ${name}`;
94+}
95+
796 /**
897 * Executes a tool call and returns the result.
998 *
@@ -12,12 +101,20 @@ import { queryDuckDuckGo, fetchPage, queryWeather, queryWebSearch } from "@/src/
12101 */
13102 export async function executeToolCall(toolCall: ToolCall): Promise<Message> {
14103 const { name, arguments: args } = toolCall.function;
15- const { query, url, timezone, forecastDays } = args as {
16- query?: string;
17- url?: string;
18- timezone?: string;
19- forecastDays?: number;
20- };
104+
105+ // Validate arguments before use
106+ const validationResult = validateToolArguments(name, args);
107+ if (typeof validationResult === "string") {
108+ const errorMessage = `Tool validation error: ${validationResult}`;
109+ debug(`[Tool] ${name} validation failed: ${validationResult}`);
110+ return {
111+ role: "tool",
112+ tool_name: name,
113+ content: `Error: ${errorMessage}`,
114+ };
115+ }
116+
117+ const { query, url, timezone, forecastDays } = validationResult;
21118
22119 type ToolName = "query_web_search" | "query_weather" | "query_duckduckgo" | "fetch_page";
23120 const tools: Record<ToolName, (query: string) => Promise<string>> = {