




























@@ -68,6 +68,13 @@ function getError(result: ToolResultObject): string | undefined {
6868return result.error;
6969}
707071+function runSdkTool(tool: SdkTool, args: unknown, invocation = makeInvocation()) {
72+if (!tool.handler) {
73+throw new Error(`SDK tool '${tool.name}' has no handler`);
74+}
75+return tool.handler(args, invocation);
76+}
77+7178afterEach(() => {
7279vi.restoreAllMocks();
7380});
@@ -1109,7 +1116,7 @@ describe("convertOpenClawToolToSdkTool", () => {
11091116const sourceTool = makeTool();
11101117const sdkTool = convertOpenClawToolToSdkTool(sourceTool, { abortSignal: controller.signal });
111111181112-const result = await sdkTool.handler({}, makeInvocation());
1119+const result = await runSdkTool(sdkTool, {});
1113112011141121expect(sourceTool.execute).toHaveBeenCalledTimes(0);
11151122expect(result).toMatchObject({
@@ -1128,7 +1135,7 @@ describe("convertOpenClawToolToSdkTool", () => {
11281135const invocation = makeInvocation({ toolCallId: "call-42" });
11291136const args = { value: "input" };
113011371131-await sdkTool.handler(args, invocation);
1138+await runSdkTool(sdkTool, args, invocation);
1132113911331140expect(beforeExecute).toHaveBeenCalledTimes(1);
11341141expect(beforeExecute).toHaveBeenCalledWith({
@@ -1152,7 +1159,7 @@ describe("convertOpenClawToolToSdkTool", () => {
11521159}),
11531160});
115411611155-const result = await sdkTool.handler({}, makeInvocation());
1162+const result = await runSdkTool(sdkTool, {});
1156116311571164expect(sourceTool.execute).toHaveBeenCalledTimes(0);
11581165expect(result).toMatchObject({
@@ -1169,7 +1176,7 @@ describe("convertOpenClawToolToSdkTool", () => {
11691176const sourceTool = makeTool({ prepareArguments });
11701177const sdkTool = convertOpenClawToolToSdkTool(sourceTool, {});
117111781172-await sdkTool.handler({ value: "raw" }, makeInvocation({ toolCallId: "call-99" }));
1179+await runSdkTool(sdkTool, { value: "raw" }, makeInvocation({ toolCallId: "call-99" }));
1173118011741181expect(prepareArguments).toHaveBeenCalledTimes(1);
11751182expect(prepareArguments).toHaveBeenCalledWith({ value: "raw" });
@@ -1185,7 +1192,7 @@ describe("convertOpenClawToolToSdkTool", () => {
11851192});
11861193const sdkTool = convertOpenClawToolToSdkTool(sourceTool, {});
118711941188-const result = await sdkTool.handler({}, makeInvocation());
1195+const result = await runSdkTool(sdkTool, {});
1189119611901197expect(sourceTool.execute).toHaveBeenCalledTimes(0);
11911198expect(result).toMatchObject({
@@ -1199,7 +1206,7 @@ describe("convertOpenClawToolToSdkTool", () => {
11991206const sourceTool = makeTool({}, { details: null });
12001207const sdkTool = convertOpenClawToolToSdkTool(sourceTool, {});
120112081202-const result = await sdkTool.handler({}, makeInvocation());
1209+const result = await runSdkTool(sdkTool, {});
1203121012041211expect(result).toEqual({ resultType: "success", textResultForLlm: "" });
12051212});
@@ -1210,7 +1217,7 @@ describe("convertOpenClawToolToSdkTool", () => {
12101217{},
12111218);
121212191213-const result = await sdkTool.handler({}, makeInvocation());
1220+const result = await runSdkTool(sdkTool, {});
1214122112151222expect(result).toEqual({ resultType: "success", textResultForLlm: "hello" });
12161223});
@@ -1231,7 +1238,7 @@ describe("convertOpenClawToolToSdkTool", () => {
12311238{},
12321239);
123312401234-const result = await sdkTool.handler({}, makeInvocation());
1241+const result = await runSdkTool(sdkTool, {});
1235124212361243expect(result).toEqual({ resultType: "success", textResultForLlm: "first\nsecond\nthird" });
12371244});
@@ -1251,7 +1258,7 @@ describe("convertOpenClawToolToSdkTool", () => {
12511258{},
12521259);
125312601254-const result = await sdkTool.handler({}, makeInvocation());
1261+const result = await runSdkTool(sdkTool, {});
1255126212561263expect(result).toEqual({
12571264binaryResultsForLlm: [
@@ -1279,7 +1286,7 @@ describe("convertOpenClawToolToSdkTool", () => {
12791286{},
12801287);
128112881282-const result = await sdkTool.handler({}, makeInvocation());
1289+const result = await runSdkTool(sdkTool, {});
1283129012841291expect(result).toMatchObject({
12851292resultType: "failure",
@@ -1299,7 +1306,7 @@ describe("convertOpenClawToolToSdkTool", () => {
12991306});
13001307const sdkTool = convertOpenClawToolToSdkTool(sourceTool, {});
130113081302-const result = await sdkTool.handler({}, makeInvocation());
1309+const result = await runSdkTool(sdkTool, {});
1303131013041311expect(result).toMatchObject({
13051312resultType: "failure",
@@ -1324,8 +1331,8 @@ describe("convertOpenClawToolToSdkTool", () => {
13241331const sourceTool = makeTool({ execute });
13251332const sdkTool = convertOpenClawToolToSdkTool(sourceTool, {});
132613331327-const firstRun = sdkTool.handler({}, makeInvocation({ toolCallId: "call-1" }));
1328-const secondRun = sdkTool.handler({}, makeInvocation({ toolCallId: "call-2" }));
1334+const firstRun = runSdkTool(sdkTool, {}, makeInvocation({ toolCallId: "call-1" }));
1335+const secondRun = runSdkTool(sdkTool, {}, makeInvocation({ toolCallId: "call-2" }));
13291336await flushAsync();
1330133713311338expect(execute).toHaveBeenCalledTimes(2);
@@ -1354,8 +1361,8 @@ describe("convertOpenClawToolToSdkTool", () => {
13541361const sourceTool = makeTool({ execute, executionMode: "sequential" });
13551362const sdkTool = convertOpenClawToolToSdkTool(sourceTool, {});
135613631357-const firstRun = sdkTool.handler({}, makeInvocation({ toolCallId: "call-1" }));
1358-const secondRun = sdkTool.handler({}, makeInvocation({ toolCallId: "call-2" }));
1364+const firstRun = runSdkTool(sdkTool, {}, makeInvocation({ toolCallId: "call-1" }));
1365+const secondRun = runSdkTool(sdkTool, {}, makeInvocation({ toolCallId: "call-2" }));
13591366await flushAsync();
1360136713611368expect(execute).toHaveBeenCalledTimes(1);
@@ -1389,7 +1396,7 @@ describe("convertOpenClawToolToSdkTool", () => {
13891396});
13901397const sdkTool = convertOpenClawToolToSdkTool(sourceTool, { abortSignal: controller.signal });
139113981392-const resultPromise = sdkTool.handler({}, makeInvocation());
1399+const resultPromise = runSdkTool(sdkTool, {});
13931400await flushAsync();
13941401controller.abort();
13951402const result = await resultPromise;
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。