From 69c10fc67477ebe61649aefaa899b79d71fe654e Mon Sep 17 00:00:00 2001 From: Eason Liang <136036952+easonLiangWorldedtech@users.noreply.github.com> Date: Thu, 19 Nov 2026 04:41:54 +0800 Subject: [PATCH] feat(api): cancel Kenari requests through the shared abort helpers Wire the Kenari handler to the shared abort-signal contract so caller cancellation and the configured request timeout actually reach the OpenAI SDK: - createMessage: race the model lookup through resolveModelWithAbort, hand the SDK a bridged per-request signal, and normalize an abort - whether the SDK rejects at call time or mid-iteration - into the provider-scoped AbortError, leaving ordinary failures untouched; a stream that ends because the caller cancelled surfaces as the same AbortError instead of completing normally. - The SDK adds an abort listener to whatever signal it is handed (openai client.mjs) and never removes it, so the stream bridges the caller signal like completePrompt does and detaches it on every exit path: the generator's finally after the request starts, and the lookup's own rejection handler before it, so a catalog failure with a live caller signal leaves no listener and no controller behind. - The create() await races the bridged signal through rejectOnAbort, as completePrompt does, so a request that never returns settles as the contract AbortError on cancel instead of hanging until the transport gives up. - Guard the stream loop so a cancel leaks no further output: skip a buffered chunk at the top of the loop and refuse the remaining tool calls of the chunk being emitted, which mirrors the in-loop guards openai-codex already ships; the tail check keeps turning a quiet post-cancel stream end into the contract AbortError. - completePrompt: bridge the caller signal (bridgeExternalAbortSignal) so the SDK listener never sticks to the caller's long-lived signal, merge timeoutMs into that same signal, race the completion because the SDK ignores the signal while it retries, and detach the bridge in finally. - The completion catch recognises an abort through isRequestAborted, the way requesty and zoo-gateway do, so an SDK-side abort surfaces as the contract AbortError instead of being wrapped as a completion failure; ordinary Error wrapping and non-Error propagation stay unchanged. - Keep the guarded block around the response extraction so a malformed body still surfaces as `Kenari completion error: ...`, resolve an empty `choices` to an empty string, drop a completion that resolves just after the cancel, and send request options only when a signal exists, which preserves the one-argument SDK call when neither cancellation nor a usable timeout is configured. - Spec: mockCreate is typed to the SDK call signature (no `as never`), each case installs its own stream or completion fixture, the stream cases assert the bridged signal and the added-vs-removed abort listeners, and the detach cases assert the AbortError identity before checking that the bridge listener was removed. Refs: #2499 (gap), #2486 (umbrella + split plan), #616 (abort pass-through). --- src/api/providers/__tests__/kenari.spec.ts | 345 ++++++++++++++++++++- src/api/providers/kenari.ts | 146 ++++++--- 2 files changed, 452 insertions(+), 39 deletions(-) diff --git a/src/api/providers/__tests__/kenari.spec.ts b/src/api/providers/__tests__/kenari.spec.ts index 0ba360a069..a51c0c4875 100644 --- a/src/api/providers/__tests__/kenari.spec.ts +++ b/src/api/providers/__tests__/kenari.spec.ts @@ -1,5 +1,4 @@ // npx vitest run src/api/providers/__tests__/kenari.spec.ts - // Mock vscode first to avoid import errors vitest.mock("vscode", () => ({ workspace: { @@ -17,6 +16,7 @@ import { kenariDefaultModelId } from "@roo-code/types" import { KenariHandler } from "../kenari" import { getModels } from "../fetchers/modelCache" import { ApiHandlerOptions } from "../../../shared/api" +import { makeCreateMessageMetadata } from "../../../test-utils/api" import { asyncStreamFrom, collectStream } from "../../../test-utils/stream" import { clearAllMocks } from "../../../test-utils/reset" @@ -41,7 +41,10 @@ vitest.mock("../fetchers/modelCache", () => ({ getModelsFromCache: vitest.fn().mockReturnValue(undefined), })) -const mockCreate = vitest.fn() +// Typed to the SDK call shape so the callbacks and the asserted options are checked; the +// return stays loose because the fixtures are deliberate partial doubles. +const mockCreate = + vitest.fn<(body: OpenAI.Chat.Completions.ChatCompletionCreateParams, options?: OpenAI.RequestOptions) => unknown>() ;(OpenAI as any).mockImplementation(function () { return { @@ -343,6 +346,12 @@ describe("KenariHandler", () => { mockCreate.mockRejectedValue(new Error("boom")) const handler = new KenariHandler(mockOptions) await expect(handler.completePrompt("ping")).rejects.toThrow("Kenari completion error: boom") + + mockCreate.mockResolvedValue({}) + await expect(handler.completePrompt("ping")).rejects.toThrow("Kenari completion error:") + const live = new AbortController() // a non-Error rejection passes through even with a live signal + mockCreate.mockRejectedValue("string failure") + await expect(handler.completePrompt("ping", { abortSignal: live.signal })).rejects.toBe("string failure") }) it("re-throws non-Error rejections unchanged", async () => { @@ -372,6 +381,338 @@ describe("KenariHandler", () => { }) }) + describe("cancellation", () => { + const messages: Anthropic.Messages.MessageParam[] = [{ role: "user", content: "Hi" }] + const aborted = (provider: string) => ({ name: "AbortError", message: `The ${provider} request was aborted` }) + + describe("createMessage", () => { + const streamWith = (handler: KenariHandler, abortSignal?: AbortSignal) => + handler.createMessage( + "sys", + messages, + abortSignal ? makeCreateMessageMetadata({ abortSignal }) : undefined, + ) + + const okStream = () => asyncStreamFrom([{ choices: [{ delta: { content: "ok" }, index: 0 }], usage: null }]) + function yieldsThenThrows(failure: Error) { + return (async function* () { + yield { choices: [{ delta: { content: "Hello" }, index: 0 }], usage: null } + throw failure + })() + } + + it("forwards the caller abort signal to the SDK request options", async () => { + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + let requestOptions: OpenAI.RequestOptions | undefined + mockCreate.mockImplementation((_body, options) => { + requestOptions = options + return Promise.resolve(okStream()) // the real SDK returns a thenable, which the race awaits + }) + + const chunks = await collectStream(streamWith(handler, caller.signal)) + expect(requestOptions?.signal).toBeInstanceOf(AbortSignal) + expect(requestOptions?.signal).not.toBe(caller.signal) + expect(chunks).toEqual([{ type: "text", text: "ok" }]) + }) + + it("detaches the SDK abort listener the stream request attached", async () => { + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + const added = vitest.spyOn(caller.signal, "addEventListener") + const removed = vitest.spyOn(caller.signal, "removeEventListener") + mockCreate.mockResolvedValue(okStream()) + const stream = streamWith(handler, caller.signal) + + expect(await collectStream(stream)).toEqual([{ type: "text", text: "ok" }]) + const attached = added.mock.calls.filter(([type]) => type === "abort").map(([, l]) => l) + expect(attached).toHaveLength(1) + expect(removed.mock.calls.filter(([type]) => type === "abort").map(([, l]) => l)).toEqual(attached) + }) + + it("rejects a stream that never starts once the caller cancels", async () => { + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + mockCreate.mockImplementation(() => new Promise(() => {})) + const pending = collectStream(streamWith(handler, caller.signal)) + await vitest.waitFor(() => expect(mockCreate).toHaveBeenCalled()) + caller.abort() + + await expect(pending).rejects.toMatchObject(aborted("Kenari")) + }) + + it("detaches the bridge when the catalog rejects with the caller still live", async () => { + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + const added = vitest.spyOn(caller.signal, "addEventListener") + const removed = vitest.spyOn(caller.signal, "removeEventListener") + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockRejectedValueOnce(new Error("catalog unavailable")) + + await expect(collectStream(streamWith(handler, caller.signal))).rejects.toThrow("catalog unavailable") + + const attached = added.mock.calls.filter(([type]) => type === "abort").map(([, l]) => l) + expect(attached).toHaveLength(1) + expect(removed.mock.calls.filter(([type]) => type === "abort").map(([, l]) => l)).toEqual(attached) + }) + + it("keeps the one-argument SDK call when the caller gives no signal", async () => { + const handler = new KenariHandler(mockOptions) + mockCreate.mockResolvedValue(okStream()) + expect(await collectStream(streamWith(handler))).toEqual([{ type: "text", text: "ok" }]) + expect(mockCreate.mock.calls[0]).toHaveLength(1) + }) + + it("rejects a pre-aborted caller before the model lookup and the SDK call", async () => { + const { getModels } = await import("../fetchers/modelCache") + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + caller.abort() + + await expect(collectStream(streamWith(handler, caller.signal))).rejects.toMatchObject(aborted("Kenari")) + expect(getModels).not.toHaveBeenCalled() + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("rejects with an AbortError when the signal aborts during model lookup", async () => { + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockImplementationOnce(() => new Promise(() => {})) + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + const pending = collectStream(streamWith(handler, caller.signal)) + await vitest.waitFor(() => expect(getModels).toHaveBeenCalled()) + caller.abort() + + await expect(pending).rejects.toMatchObject(aborted("Kenari")) + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("normalizes an SDK abort at stream creation into the provider AbortError", async () => { + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + mockCreate.mockImplementation(() => Promise.reject(new Error("Request was aborted."))) + + await expect(collectStream(streamWith(handler, caller.signal))).rejects.toMatchObject(aborted("Kenari")) + }) + + it("normalizes an abort raised mid-iteration after the chunks that already arrived", async () => { + const handler = new KenariHandler(mockOptions) + const stream = streamWith(handler, new AbortController().signal) + mockCreate.mockResolvedValue(yieldsThenThrows(new Error("Request was aborted."))) + + await expect(stream.next()).resolves.toMatchObject({ + done: false, + value: { type: "text", text: "Hello" }, + }) + await expect(stream.next()).rejects.toMatchObject(aborted("Kenari")) + }) + + it("rejects with an AbortError when the caller aborts just as the stream completes", async () => { + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + const stream = streamWith(handler, caller.signal) + mockCreate.mockResolvedValue( + asyncStreamFrom([{ choices: [{ delta: { content: "ok" }, index: 0 }], usage: null }]), + ) + + await expect(stream.next()).resolves.toMatchObject({ done: false, value: { type: "text", text: "ok" } }) + caller.abort() + await expect(stream.next()).rejects.toMatchObject(aborted("Kenari")) + }) + it("refuses to emit the remaining tool calls of a chunk once the caller cancels", async () => { + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + const call = (index: number) => ({ index, id: "c" + index, function: { arguments: "{}" } }) + const toolChunk = { choices: [{ delta: { tool_calls: [call(0), call(1)] }, index: 0 }], usage: null } + mockCreate.mockResolvedValue(asyncStreamFrom([toolChunk])) + const stream = streamWith(handler, caller.signal) + + await expect(stream.next()).resolves.toMatchObject({ value: { type: "tool_call_partial", index: 0 } }) + caller.abort() + await expect(stream.next()).rejects.toMatchObject(aborted("Kenari")) + }) + + it("does not pull a buffered chunk once the caller cancels", async () => { + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + const buffered = { choices: [{ delta: { content: "buffered" }, index: 0 }], usage: null } + mockCreate.mockResolvedValue( + asyncStreamFrom([{ choices: [{ delta: { content: "ok" }, index: 0 }], usage: null }, buffered]), + ) + const stream = streamWith(handler, caller.signal) + + await expect(stream.next()).resolves.toMatchObject({ value: { type: "text", text: "ok" } }) + caller.abort() + await expect(stream.next()).rejects.toMatchObject(aborted("Kenari")) + }) + it("propagates an ordinary mid-iteration failure unchanged while the caller signal is live", async () => { + const handler = new KenariHandler(mockOptions) + const failure = new Error("upstream returned 500") + const stream = streamWith(handler, new AbortController().signal) + mockCreate.mockResolvedValue(yieldsThenThrows(failure)) + + await expect(collectStream(stream)).rejects.toBe(failure) + }) + }) + + describe("completePrompt", () => { + beforeEach(() => { + mockCreate.mockResolvedValue({ choices: [{ message: { role: "assistant", content: "the answer" } }] }) + }) + + it("bridges the caller signal, with or without a timeout, before handing it to the SDK", async () => { + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + + const withTimeout = await handler.completePrompt("ping", { + abortSignal: caller.signal, + timeoutMs: 5_000, + }) + const requestOptions = mockCreate.mock.calls[0][1] + expect(requestOptions?.signal).toBeInstanceOf(AbortSignal) + expect(requestOptions?.signal).not.toBe(caller.signal) + expect(requestOptions?.timeout).toBe(5_000) + const signalOnly = await handler.completePrompt("ping", { abortSignal: new AbortController().signal }) + const signalOnlyOptions = mockCreate.mock.calls[1][1] + expect(signalOnlyOptions?.signal).toBeInstanceOf(AbortSignal) + expect(signalOnlyOptions?.timeout).toBeUndefined() + expect([withTimeout, signalOnly]).toEqual(["the answer", "the answer"]) + }) + + it("sends no request options with no caller signal or no usable timeout", async () => { + const handler = new KenariHandler(mockOptions) + expect(await handler.completePrompt("ping")).toBe("the answer") + expect(mockCreate.mock.calls[0]).toHaveLength(1) + expect(await handler.completePrompt("ping", { timeoutMs: 0 })).toBe("the answer") + expect(mockCreate.mock.calls[1]).toHaveLength(1) + }) + + it("forwards a timeout alone as an SDK timeout and its own abort signal", async () => { + const handler = new KenariHandler(mockOptions) + + expect(await handler.completePrompt("ping", { timeoutMs: 1_200 })).toBe("the answer") + + const requestOptions = mockCreate.mock.calls[0][1] as { signal?: AbortSignal; timeout?: number } + expect(requestOptions.timeout).toBe(1_200) + expect(requestOptions.signal).toBeInstanceOf(AbortSignal) + }) + + it("returns an empty string when the completion carries no choices", async () => { + const handler = new KenariHandler(mockOptions) + mockCreate.mockResolvedValue({ choices: [] }) + + await expect(handler.completePrompt("ping")).resolves.toBe("") + }) + + it("rejects a pre-aborted signal before the model lookup and the SDK call", async () => { + const { getModels } = await import("../fetchers/modelCache") + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + caller.abort() + + await expect(handler.completePrompt("ping", { abortSignal: caller.signal })).rejects.toMatchObject( + aborted("Kenari"), + ) + expect(getModels).not.toHaveBeenCalled() + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("rejects with an AbortError when the timeout alone expires during the SDK call", async () => { + const handler = new KenariHandler(mockOptions) + let sdkSignal: AbortSignal | null | undefined + mockCreate.mockImplementation((_body, options) => { + sdkSignal = options?.signal + return new Promise(() => {}) + }) + + await expect(handler.completePrompt("ping", { timeoutMs: 20 })).rejects.toMatchObject(aborted("Kenari")) + expect(sdkSignal?.aborted).toBe(true) + }) + + it("rejects with an AbortError when the timeout alone expires during model lookup", async () => { + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockImplementationOnce(() => new Promise(() => {})) + const handler = new KenariHandler(mockOptions) + + await expect(handler.completePrompt("ping", { timeoutMs: 20 })).rejects.toMatchObject(aborted("Kenari")) + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("rejects with an AbortError when the signal aborts during model lookup", async () => { + const { getModels } = await import("../fetchers/modelCache") + vitest.mocked(getModels).mockImplementationOnce(() => new Promise(() => {})) + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + + const pending = handler.completePrompt("ping", { abortSignal: caller.signal }) + await vitest.waitFor(() => expect(getModels).toHaveBeenCalled()) + caller.abort() + + await expect(pending).rejects.toMatchObject(aborted("Kenari")) + expect(mockCreate).not.toHaveBeenCalled() + }) + + it("does not return a completion that resolves just before the caller aborts", async () => { + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + let resolveCompletion: (value: unknown) => void = () => {} + const completion = new Promise((resolve) => { + resolveCompletion = resolve + }) + mockCreate.mockImplementation(() => completion) + + const pending = handler.completePrompt("ping", { abortSignal: caller.signal }) + await vitest.waitFor(() => expect(mockCreate).toHaveBeenCalled()) + void completion.then(() => caller.abort()) + resolveCompletion({ choices: [{ message: { role: "assistant", content: "late" } }] }) + + await expect(pending).rejects.toMatchObject(aborted("Kenari")) + }) + + it("reports an SDK abort error after the caller aborts as an AbortError, not a completion error", async () => { + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + mockCreate.mockImplementation(async () => { + caller.abort() + throw new Error("Request was aborted.") + }) + + await expect(handler.completePrompt("ping", { abortSignal: caller.signal })).rejects.toMatchObject( + { name: "AbortError" }, // the entry guard rethrows the caller's own abort reason + ) + }) + + it("normalizes an SDK abort raised by the completion into the provider AbortError", async () => { + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() // the caller signal stays live while the SDK aborts on its own + mockCreate.mockRejectedValue(new Error("Request was aborted.")) + const pending = handler.completePrompt("ping", { abortSignal: caller.signal }) + + await expect(pending).rejects.toMatchObject(aborted("Kenari")) + }) + it("detaches the bridge after completion, failure, and cancellation alike", async () => { + const handler = new KenariHandler(mockOptions) + const caller = new AbortController() + const liveCall = () => handler.completePrompt("ping", { abortSignal: caller.signal }) + const added = vitest.spyOn(caller.signal, "addEventListener") + const removed = vitest.spyOn(caller.signal, "removeEventListener") + + await expect(liveCall()).resolves.toBe("the answer") + expect(caller.signal.aborted).toBe(false) // the success and failure rows never cancel + mockCreate.mockRejectedValue(new Error("boom")) + await expect(liveCall()).rejects.toThrow("Kenari completion error: boom") + mockCreate.mockImplementation(() => new Promise(() => {})) + const pending = liveCall().catch((error: unknown) => error) + await vitest.waitFor(() => expect(mockCreate).toHaveBeenCalledTimes(3)) + caller.abort() + expect(await pending).toMatchObject(aborted("Kenari")) // the cancel must surface as AbortError, not any rejection + const aborts = added.mock.calls.filter(([type]) => type === "abort").map(([, listener]) => listener) + expect(aborts).toHaveLength(3) + expect(removed.mock.calls.filter(([type]) => type === "abort").map(([, l]) => l)).toEqual(aborts) + }) + }) + }) + describe("model catalog", () => { it("reports its cache scope", () => { const handler = new KenariHandler(mockOptions) diff --git a/src/api/providers/kenari.ts b/src/api/providers/kenari.ts index 22ca50c916..f93a09ef33 100644 --- a/src/api/providers/kenari.ts +++ b/src/api/providers/kenari.ts @@ -23,8 +23,20 @@ import type { } from "../index" import { RouterProvider } from "./router-provider" import { getKenariModels } from "./fetchers/kenari" +import { + bridgeExternalAbortSignal, + createAbortError, + hasUsableTimeout, + isRequestAborted, + mergeAbortSignalAndTimeout, + rejectOnAbort, + resolveModelWithAbort, + toSdkTimeoutMs, +} from "./utils/abort-signal" import { extractReasoningFromDelta } from "./utils/extract-reasoning" +const KENARI_PROVIDER_NAME = "Kenari" + /** * API handler for Kenari, an Indonesian OpenAI-compatible AI gateway billed * in Rupiah (IDR). @@ -72,7 +84,19 @@ export class KenariHandler extends RouterProvider implements SingleCompletionHan messages: Anthropic.Messages.MessageParam[], metadata?: ApiHandlerCreateMessageMetadata, ): ApiStream { - const { id: modelId, info } = await this.fetchModel() + const callerSignal = metadata?.abortSignal + // The SDK adds an abort listener to whatever signal it is handed and never removes it, so this + // generator bridges the caller signal and detaches the bridge in its finally, as completePrompt does. + const { signal: bridgedCallerSignal, dispose: removeCallerAbortListener } = + bridgeExternalAbortSignal(callerSignal) + const { id: modelId, info } = await resolveModelWithAbort( + () => this.fetchModel(), + bridgedCallerSignal, + KENARI_PROVIDER_NAME, + ).catch((error: unknown) => { + removeCallerAbortListener() + throw error + }) const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [ { role: "system", content: systemPrompt }, @@ -93,57 +117,89 @@ export class KenariHandler extends RouterProvider implements SingleCompletionHan parallel_tool_calls: metadata?.parallelToolCalls ?? true, } - const completion = await this.client.chat.completions.create(body) + const streamRequestOptions: [] | [OpenAI.RequestOptions] = bridgedCallerSignal + ? [{ signal: bridgedCallerSignal }] + : [] - for await (const chunk of completion) { - const delta = chunk.choices[0]?.delta + try { + const pendingCompletion = this.client.chat.completions.create(body, ...streamRequestOptions) + const completion = bridgedCallerSignal + ? await rejectOnAbort(pendingCompletion, bridgedCallerSignal, KENARI_PROVIDER_NAME) + : await pendingCompletion + + for await (const chunk of completion) { + if (callerSignal?.aborted) break // the tail check turns this into the contract AbortError + const delta = chunk.choices[0]?.delta + + // Several Kenari models (GLM, DeepSeek) stream reasoning via reasoning_content, + // with an OpenRouter-style `reasoning` fallback; the shared helper handles both. + const reasoningText = extractReasoningFromDelta(delta) + if (reasoningText) { + yield { type: "reasoning", text: reasoningText } + } - // Several Kenari models (GLM, DeepSeek) stream reasoning via reasoning_content, - // with an OpenRouter-style `reasoning` fallback; the shared helper handles both. - const reasoningText = extractReasoningFromDelta(delta) - if (reasoningText) { - yield { type: "reasoning", text: reasoningText } - } + if (delta?.content) { + yield { type: "text", text: delta.content } + } - if (delta?.content) { - yield { type: "text", text: delta.content } - } + // Emit raw tool call chunks - NativeToolCallParser handles state management. + if (delta?.tool_calls) { + for (const toolCall of delta.tool_calls) { + if (callerSignal?.aborted) throw createAbortError(KENARI_PROVIDER_NAME) // a cancel must not emit the rest of this chunk + yield { + type: "tool_call_partial", + index: toolCall.index, + id: toolCall.id, + name: toolCall.function?.name, + arguments: toolCall.function?.arguments, + } + } + } - // Emit raw tool call chunks - NativeToolCallParser handles state management. - if (delta?.tool_calls) { - for (const toolCall of delta.tool_calls) { + if (chunk.usage) { yield { - type: "tool_call_partial", - index: toolCall.index, - id: toolCall.id, - name: toolCall.function?.name, - arguments: toolCall.function?.arguments, + type: "usage", + inputTokens: chunk.usage.prompt_tokens || 0, + outputTokens: chunk.usage.completion_tokens || 0, + cacheReadTokens: chunk.usage.prompt_tokens_details?.cached_tokens || undefined, } } } - - if (chunk.usage) { - yield { - type: "usage", - inputTokens: chunk.usage.prompt_tokens || 0, - outputTokens: chunk.usage.completion_tokens || 0, - cacheReadTokens: chunk.usage.prompt_tokens_details?.cached_tokens || undefined, - } + } catch (error) { + if (isRequestAborted(error, callerSignal)) { + throw createAbortError(KENARI_PROVIDER_NAME) } + throw error + } finally { + removeCallerAbortListener() } + if (callerSignal?.aborted) throw createAbortError(KENARI_PROVIDER_NAME) // a stream that ended because the caller cancelled } /** * Performs a non-streaming chat completion and returns the full response text. * + * Cancellation follows the shared abort-signal contract (see zoo-gateway.ts and + * requesty.ts): the caller signal is bridged to a per-request signal, the timeout joins + * that same signal, and `finally` detaches the bridge on every exit path. * @param prompt - The user prompt to send as a single user message. + * @param options - Caller cancellation and request timeout, per `CompletePromptOptions`. * @returns The model's reply text, or an empty string if no content is returned. * @throws Error with a Kenari-specific prefix if the request fails. */ - async completePrompt(prompt: string, _options?: CompletePromptOptions): Promise { - const { id: modelId, info } = await this.fetchModel() + async completePrompt(prompt: string, options?: CompletePromptOptions): Promise { + const { signal: bridgedCallerSignal, dispose: removeExternalAbortListener } = bridgeExternalAbortSignal( + options?.abortSignal, + ) + const requestAbortSignal = mergeAbortSignalAndTimeout(bridgedCallerSignal, options?.timeoutMs) try { + const { id: modelId, info } = await resolveModelWithAbort( + () => this.fetchModel(), + requestAbortSignal, + KENARI_PROVIDER_NAME, + ) + const requestOptions: OpenAI.Chat.ChatCompletionCreateParams = { model: modelId, messages: [{ role: "user", content: prompt }], @@ -156,13 +212,29 @@ export class KenariHandler extends RouterProvider implements SingleCompletionHan requestOptions.max_completion_tokens = info.maxTokens - const response = await this.client.chat.completions.create(requestOptions) - return response.choices[0]?.message.content || "" - } catch (error) { - if (error instanceof Error) { - throw new Error(`Kenari completion error: ${error.message}`) + const createOptions: OpenAI.RequestOptions = { + ...(requestAbortSignal && { signal: requestAbortSignal }), + ...(hasUsableTimeout(options?.timeoutMs) && { timeout: toSdkTimeoutMs(options.timeoutMs) }), } - throw error + const requestOptionsTail: [] | [OpenAI.RequestOptions] = requestAbortSignal ? [createOptions] : [] + + try { + const pendingCompletion = this.client.chat.completions.create(requestOptions, ...requestOptionsTail) + // Race the completion too: the SDK ignores the signal while it retries. + const response = requestAbortSignal + ? await rejectOnAbort(pendingCompletion, requestAbortSignal, KENARI_PROVIDER_NAME) + : await pendingCompletion + if (requestAbortSignal?.aborted) throw createAbortError(KENARI_PROVIDER_NAME) // a completion that lands after the cancel is dropped + return response.choices[0]?.message.content || "" + } catch (error) { + if (isRequestAborted(error, requestAbortSignal)) throw createAbortError(KENARI_PROVIDER_NAME) + if (error instanceof Error) { + throw new Error(`Kenari completion error: ${error.message}`) + } + throw error + } + } finally { + removeExternalAbortListener() } } }