Skip to content

Commit ebb301c

Browse files
fix(moonshot): forward abort signal through Kimi web search provider (#105125)
1 parent 81e392b commit ebb301c

3 files changed

Lines changed: 45 additions & 22 deletions

File tree

‎extensions/moonshot/src/kimi-web-search-provider.runtime.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ async function runKimiSearch(params: {
216216
baseUrl: string;
217217
model: string;
218218
timeoutSeconds: number;
219+
signal?: AbortSignal;
219220
}): Promise<KimiSearchResult> {
220221
const endpoint = `${params.baseUrl.trim().replace(/\/$/, "")}/chat/completions`;
221222
const messages: Array<Record<string, unknown>> = [{ role: "user", content: params.query }];
@@ -227,6 +228,7 @@ async function runKimiSearch(params: {
227228
{
228229
url: endpoint,
229230
timeoutSeconds: params.timeoutSeconds,
231+
signal: params.signal,
230232
init: {
231233
method: "POST",
232234
headers: {
@@ -341,6 +343,7 @@ async function runKimiSearch(params: {
341343
export async function executeKimiWebSearchProviderTool(
342344
ctx: { config?: OpenClawConfig; searchConfig?: SearchConfigRecord },
343345
args: Record<string, unknown>,
346+
opts?: { signal?: AbortSignal },
344347
): Promise<Record<string, unknown>> {
345348
const searchConfig = mergeScopedSearchConfig(
346349
ctx.searchConfig,
@@ -392,6 +395,7 @@ export async function executeKimiWebSearchProviderTool(
392395
baseUrl,
393396
model,
394397
timeoutSeconds: resolveSearchTimeoutSeconds(searchConfig),
398+
signal: opts?.signal,
395399
});
396400
if (!result.grounded) {
397401
return {

‎extensions/moonshot/src/kimi-web-search-provider.test.ts‎

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,12 @@
11
// Moonshot tests cover kimi web search provider plugin behavior.
22
import type { OpenClawConfig } from "openclaw/plugin-sdk/provider-onboard";
3-
import { withEnvAsync } from "openclaw/plugin-sdk/test-env";
3+
import { withEnv, withEnvAsync } from "openclaw/plugin-sdk/test-env";
44
import { afterEach, describe, expect, it, vi } from "vitest";
55
import { testing } from "../test-api.js";
66
import { createKimiWebSearchProvider } from "./kimi-web-search-provider.js";
77

88
const kimiApiKeyEnv = ["KIMI_API", "KEY"].join("_");
99

10-
function withEnv(overrides: Record<string, string>, run: () => void): void {
11-
const previous = new Map<string, string | undefined>();
12-
for (const [key, value] of Object.entries(overrides)) {
13-
previous.set(key, process.env[key]);
14-
process.env[key] = value;
15-
}
16-
try {
17-
run();
18-
} finally {
19-
for (const [key, value] of previous) {
20-
if (value === undefined) {
21-
delete process.env[key];
22-
} else {
23-
process.env[key] = value;
24-
}
25-
}
26-
}
27-
}
28-
2910
function jsonResponse(body: unknown): Response {
3011
return new Response(JSON.stringify(body), {
3112
status: 200,
@@ -286,6 +267,42 @@ describe("kimi web search provider", () => {
286267
).toBeUndefined();
287268
});
288269

270+
it("forwards the execution abort signal to an in-flight Kimi search", async () => {
271+
const fetchMock = vi.fn(
272+
(_url: string, init?: RequestInit) =>
273+
new Promise<Response>((_resolve, reject) => {
274+
init?.signal?.addEventListener(
275+
"abort",
276+
() => reject(new Error(String(init.signal?.reason ?? "Aborted"))),
277+
{
278+
once: true,
279+
},
280+
);
281+
}),
282+
);
283+
vi.stubGlobal("fetch", fetchMock);
284+
285+
await withEnvAsync({ KIMI_API_KEY: "kimi-test-key" }, async () => {
286+
const controller = new AbortController();
287+
const tool = createKimiWebSearchProvider().createTool({ config: {}, searchConfig: {} });
288+
if (!tool) {
289+
throw new Error("Expected tool definition");
290+
}
291+
292+
const search = tool.execute(
293+
{ query: "unique Kimi abort regression" },
294+
{
295+
signal: controller.signal,
296+
},
297+
);
298+
await vi.waitFor(() => expect(fetchMock).toHaveBeenCalledOnce());
299+
controller.abort(new Error("Kimi search cancelled"));
300+
301+
await expect(search).rejects.toThrow("Kimi search cancelled");
302+
expect(fetchMock.mock.calls[0]?.[1]?.signal?.aborted).toBe(true);
303+
});
304+
});
305+
289306
it("uses config apiKey when provided", () => {
290307
expect(testing.resolveKimiApiKey({ apiKey: "kimi-test-key" })).toBe("kimi-test-key");
291308
});

‎extensions/moonshot/src/kimi-web-search-provider.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ export function createKimiWebSearchProvider(): WebSearchProviderPlugin {
6060
description:
6161
"Search the web using Kimi by Moonshot. Returns AI-synthesized answers with citations from native $web_search.",
6262
parameters: KimiSearchSchema,
63-
execute: async (args) => {
63+
execute: async (args, context) => {
6464
const { executeKimiWebSearchProviderTool } = await loadKimiWebSearchProviderRuntime();
65-
return await executeKimiWebSearchProviderTool(ctx, args);
65+
return await executeKimiWebSearchProviderTool(ctx, args, {
66+
signal: context?.signal,
67+
});
6668
},
6769
}),
6870
};

0 commit comments

Comments
 (0)