fix: TypeScript function args were being passed as objects rather than direct parameters

This commit is contained in:
2026-04-09 13:32:16 -06:00
parent 7839e1dbd9
commit 6b4a45874f
2 changed files with 52 additions and 2 deletions
+26 -1
View File
@@ -92,6 +92,29 @@ function loadEnv(filePath: string): void {
} }
} }
function extractParamNames(fn: Function): string[] {
const src = fn.toString();
const match = src.match(/^(?:async\s+)?function\s*\w*\s*\(([^)]*)\)/);
if (!match) {
return [];
}
return match[1]
.split(",")
.map((p) => p.trim().replace(/[:=?].*/s, "").trim())
.filter(Boolean);
}
function spreadArgs(
fn: Function,
data: Record<string, unknown>,
): unknown[] {
const names = extractParamNames(fn);
if (names.length === 0) {
return [];
}
return names.map((name) => data[name]);
}
async function run( async function run(
agentPath: string, agentPath: string,
agentFunc: string, agentFunc: string,
@@ -103,7 +126,9 @@ async function run(
throw new Error(`No module function '${agentFunc}' at '${agentPath}'`); throw new Error(`No module function '${agentFunc}' at '${agentPath}'`);
} }
const value = await mod[agentFunc](agentData); const fn = mod[agentFunc] as Function;
const args = spreadArgs(fn, agentData);
const value = await fn(...args);
returnToLlm(value); returnToLlm(value);
dumpResult(`{agent_name}:${agentFunc}`); dumpResult(`{agent_name}:${agentFunc}`);
} }
+26 -1
View File
@@ -87,6 +87,29 @@ function loadEnv(filePath: string): void {
} }
} }
function extractParamNames(fn: Function): string[] {
const src = fn.toString();
const match = src.match(/^(?:async\s+)?function\s*\w*\s*\(([^)]*)\)/);
if (!match) {
return [];
}
return match[1]
.split(",")
.map((p) => p.trim().replace(/[:=?].*/s, "").trim())
.filter(Boolean);
}
function spreadArgs(
fn: Function,
data: Record<string, unknown>,
): unknown[] {
const names = extractParamNames(fn);
if (names.length === 0) {
return [];
}
return names.map((name) => data[name]);
}
async function run( async function run(
toolPath: string, toolPath: string,
toolFunc: string, toolFunc: string,
@@ -98,7 +121,9 @@ async function run(
throw new Error(`No module function '${toolFunc}' at '${toolPath}'`); throw new Error(`No module function '${toolFunc}' at '${toolPath}'`);
} }
const value = await mod[toolFunc](toolData); const fn = mod[toolFunc] as Function;
const args = spreadArgs(fn, toolData);
const value = await fn(...args);
returnToLlm(value); returnToLlm(value);
dumpResult("{function_name}"); dumpResult("{function_name}");
} }