Skip to content

Commit 62f596c

Browse files
committed
refactor(api): share the abort-signal resolution guard, race, and normalization
Move the entry guard, the rejectOnAbort race, and the abort-normalizing catch out of the four gateway createMessage bodies and into a single resolveModelWithAbort helper in utils/abort-signal.ts. Each provider now calls it in one line, so the cancellation semantics around model resolution are implemented once and exercised through every provider's spec. Also compact the completePrompt request-options blocks (conditional spreads instead of if-assigned mutable objects) and align the streamResponsesMessage/streamAnthropicMessage pre-stream guards on the shared isRequestAborted predicate.
1 parent 85b180c commit 62f596c

5 files changed

Lines changed: 78 additions & 130 deletions

File tree

‎src/api/providers/opencode-go.ts‎

Lines changed: 23 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ import {
3636
convertOpenAIToolsToAnthropic,
3737
convertOpenAIToolChoiceToAnthropic,
3838
} from "../../core/prompts/tools/native-tools/converters"
39-
import { createAbortError, isRequestAborted, rejectOnAbort } from "./utils/abort-signal"
39+
import { createAbortError, isRequestAborted, resolveModelWithAbort } from "./utils/abort-signal"
4040

4141
/**
4242
* The wire formats exposed by the Opencode Go gateway:
@@ -203,33 +203,13 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio
203203
messages: Anthropic.Messages.MessageParam[],
204204
metadata?: ApiHandlerCreateMessageMetadata,
205205
): ApiStream {
206-
// Fail fast when the task is already cancelled before any model-catalog
207-
// work starts: the standardized AbortError must win over any failure the
208-
// fallible resolution could raise.
206+
// Establish the cancellation scope around model resolution: a
207+
// pre-aborted signal rejects before the lookup starts, and a signal
208+
// that fires while model metadata is loading settles on the
209+
// standardized AbortError; any other resolution failure propagates
210+
// unchanged.
209211
const externalAbortSignal = metadata?.abortSignal
210-
if (externalAbortSignal?.aborted) {
211-
throw createAbortError("Opencode Go")
212-
}
213-
214-
// Establish the cancellation scope before model resolution: a pre-aborted
215-
// call, or one aborted while model metadata is loading, must reject
216-
// promptly instead of waiting for the catalog lookup to settle. Abort
217-
// failures from the lookup itself are normalized to the provider
218-
// AbortError; any other resolution failure propagates unchanged.
219-
let resolved: Awaited<ReturnType<OpencodeGoHandler["resolveModel"]>>
220-
try {
221-
if (externalAbortSignal) {
222-
// Stryker disable next-line StringLiteral: the raw rejectOnAbort rejection is re-stamped by the catch's createAbortError below (its name "AbortError" always matches isRequestAborted), so this provider-name literal is unobservable
223-
resolved = await rejectOnAbort(this.resolveModel(), externalAbortSignal, "Opencode Go")
224-
} else {
225-
resolved = await this.resolveModel()
226-
}
227-
} catch (error) {
228-
if (isRequestAborted(error, externalAbortSignal)) {
229-
throw createAbortError("Opencode Go")
230-
}
231-
throw error
232-
}
212+
const resolved = await resolveModelWithAbort(() => this.resolveModel(), externalAbortSignal, "Opencode Go")
233213
const { id: modelId, info, format, temperature, reasoningEffort, maxTokens } = resolved
234214

235215
// Per-request controller so an external abort signal (e.g. task
@@ -493,11 +473,10 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio
493473
headers: metadata?.taskId ? { "x-opencode-session": metadata.taskId } : undefined,
494474
})
495475
} catch (error) {
496-
if (
497-
abortSignal.aborted ||
498-
error instanceof APIUserAbortError ||
499-
(error instanceof Error && error.name === "AbortError")
500-
) {
476+
// isRequestAborted covers the signal-aborted case plus the OpenAI
477+
// SDK's APIUserAbortError (name "APIUserAbortError" / "Request was
478+
// aborted.").
479+
if (isRequestAborted(error, abortSignal)) {
501480
throw createAbortError("Opencode Go")
502481
}
503482
if (error instanceof Error) {
@@ -635,12 +614,9 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio
635614
} catch (error) {
636615
// Preserve abort identity (series standard): a cancelled request
637616
// must surface as a DOM-standard AbortError, not a wrapped
638-
// completion error.
639-
if (
640-
abortSignal.aborted ||
641-
error instanceof AnthropicAbortError ||
642-
(error instanceof Error && error.name === "AbortError")
643-
) {
617+
// completion error. isRequestAborted also covers the Anthropic
618+
// SDK's APIUserAbortError (name "APIUserAbortError").
619+
if (isRequestAborted(error, abortSignal)) {
644620
throw createAbortError("Opencode Go")
645621
}
646622
if (error instanceof Error) {
@@ -831,12 +807,9 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio
831807
// timeoutMs <= 0 means "no explicit timeout": omit the SDK timeout
832808
// option entirely — the SDKs treat timeout: 0 as an immediate
833809
// abort, which would cancel the request right away.
834-
const requestOptions: Anthropic.RequestOptions = {}
835-
if (options?.abortSignal) {
836-
requestOptions.signal = options.abortSignal
837-
}
838-
if (options?.timeoutMs !== undefined && options.timeoutMs > 0) {
839-
requestOptions.timeout = options.timeoutMs
810+
const requestOptions: Anthropic.RequestOptions = {
811+
...(options?.abortSignal && { signal: options.abortSignal }),
812+
...(options?.timeoutMs !== undefined && options.timeoutMs > 0 && { timeout: options.timeoutMs }),
840813
}
841814

842815
const message = await this.anthropicClient.messages.create(
@@ -887,12 +860,9 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio
887860
// standard): timeoutMs <= 0 means "no explicit timeout" — the
888861
// OpenAI SDK treats timeout: 0 as an immediate abort, so the SDK
889862
// timeout option is omitted unless the value is positive.
890-
const createOptions: OpenAI.RequestOptions = {}
891-
if (options?.abortSignal) {
892-
createOptions.signal = options.abortSignal
893-
}
894-
if (options?.timeoutMs !== undefined && options.timeoutMs > 0) {
895-
createOptions.timeout = options.timeoutMs
863+
const createOptions: OpenAI.RequestOptions = {
864+
...(options?.abortSignal && { signal: options.abortSignal }),
865+
...(options?.timeoutMs !== undefined && options.timeoutMs > 0 && { timeout: options.timeoutMs }),
896866
}
897867

898868
const response = await this.client.responses.create(
@@ -967,12 +937,9 @@ export class OpencodeGoHandler extends RouterProvider implements SingleCompletio
967937
// timeoutMs <= 0 means "no explicit timeout": omit the SDK timeout
968938
// option entirely — the OpenAI SDK treats timeout: 0 as an immediate
969939
// abort, which would cancel the request right away.
970-
const createOptions: OpenAI.RequestOptions = {}
971-
if (options?.abortSignal) {
972-
createOptions.signal = options.abortSignal
973-
}
974-
if (options?.timeoutMs !== undefined && options.timeoutMs > 0) {
975-
createOptions.timeout = options.timeoutMs
940+
const createOptions: OpenAI.RequestOptions = {
941+
...(options?.abortSignal && { signal: options.abortSignal }),
942+
...(options?.timeoutMs !== undefined && options.timeoutMs > 0 && { timeout: options.timeoutMs }),
976943
}
977944

978945
const response = await this.client.chat.completions.create(requestOptions, createOptions)

‎src/api/providers/unbound.ts‎

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import { BaseProvider } from "./base-provider"
2323
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../index"
2424
import { handleOpenAIError } from "./utils/error-handler"
2525
import { applyRouterToolPreferences } from "./utils/router-tool-preferences"
26-
import { createAbortError, isRequestAborted, rejectOnAbort } from "./utils/abort-signal"
26+
import { createAbortError, isRequestAborted, resolveModelWithAbort } from "./utils/abort-signal"
2727
import { extractReasoningFromDelta } from "./utils/extract-reasoning"
2828

2929
// Unbound usage includes extra fields for Anthropic cache tokens.
@@ -126,33 +126,13 @@ export class UnboundHandler extends BaseProvider implements SingleCompletionHand
126126
messages: Anthropic.Messages.MessageParam[],
127127
metadata?: ApiHandlerCreateMessageMetadata,
128128
): ApiStream {
129-
// Fail fast when the task is already cancelled before any model-catalog
130-
// work starts: the standardized AbortError wins over any failure the
131-
// fallible resolution could raise.
129+
// Establish the cancellation scope around model resolution: a
130+
// pre-aborted signal rejects before the lookup starts, and a signal
131+
// that fires while model metadata is loading settles on the
132+
// standardized AbortError; any other resolution failure propagates
133+
// unchanged.
132134
const externalAbortSignal = metadata?.abortSignal
133-
if (externalAbortSignal?.aborted) {
134-
throw createAbortError("Unbound")
135-
}
136-
137-
// Establish the cancellation scope before model resolution: a call
138-
// aborted while model metadata is loading must reject promptly instead
139-
// of waiting for the catalog lookup to settle. Abort failures from the
140-
// lookup itself are normalized to the provider AbortError; any other
141-
// resolution failure propagates unchanged.
142-
let resolved: Awaited<ReturnType<UnboundHandler["fetchModel"]>>
143-
try {
144-
if (externalAbortSignal) {
145-
// Stryker disable next-line StringLiteral: the raw rejectOnAbort rejection is re-stamped by the catch's createAbortError below (its name "AbortError" always matches isRequestAborted), so this provider-name literal is unobservable
146-
resolved = await rejectOnAbort(this.fetchModel(), externalAbortSignal, "Unbound")
147-
} else {
148-
resolved = await this.fetchModel()
149-
}
150-
} catch (error) {
151-
if (isRequestAborted(error, externalAbortSignal)) {
152-
throw createAbortError("Unbound")
153-
}
154-
throw error
155-
}
135+
const resolved = await resolveModelWithAbort(() => this.fetchModel(), externalAbortSignal, "Unbound")
156136
const {
157137
id: model,
158138
info,

‎src/api/providers/utils/abort-signal.ts‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,3 +109,38 @@ export function rejectOnAbort<T>(pending: Promise<T>, signal: AbortSignal, provi
109109
)
110110
})
111111
}
112+
113+
/**
114+
* Resolve a provider's model metadata inside a cancellation scope.
115+
*
116+
* A pre-aborted signal rejects immediately with the standardized AbortError
117+
* before any lookup starts; a signal that fires while the lookup is pending
118+
* settles via {@link rejectOnAbort} instead of waiting for the catalog to
119+
* resolve. Abort failures from the lookup itself are normalized to the
120+
* provider AbortError; any other resolution failure propagates unchanged.
121+
*
122+
* Providers pass their own resolution step, so the entry guard, the race, and
123+
* the normalization logic exist once and are exercised through every
124+
* provider's spec.
125+
*/
126+
export async function resolveModelWithAbort<T>(
127+
fetchModel: () => Promise<T>,
128+
abortSignal: AbortSignal | undefined,
129+
providerName: string,
130+
): Promise<T> {
131+
if (abortSignal?.aborted) {
132+
throw createAbortError(providerName)
133+
}
134+
135+
try {
136+
if (abortSignal) {
137+
return await rejectOnAbort(fetchModel(), abortSignal, providerName)
138+
}
139+
return await fetchModel()
140+
} catch (error) {
141+
if (isRequestAborted(error, abortSignal)) {
142+
throw createAbortError(providerName)
143+
}
144+
throw error
145+
}
146+
}

‎src/api/providers/vercel-ai-gateway.ts‎

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import { addCacheBreakpoints } from "../transform/caching/vercel-ai-gateway"
1919

2020
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../index"
2121
import { RouterProvider } from "./router-provider"
22-
import { createAbortError, isRequestAborted, rejectOnAbort } from "./utils/abort-signal"
22+
import { createAbortError, isRequestAborted, resolveModelWithAbort } from "./utils/abort-signal"
2323

2424
// Extend OpenAI's CompletionUsage to include Vercel AI Gateway specific fields
2525
interface VercelAiGatewayUsage extends OpenAI.CompletionUsage {
@@ -59,33 +59,13 @@ export class VercelAiGatewayHandler extends RouterProvider implements SingleComp
5959
messages: Anthropic.Messages.MessageParam[],
6060
metadata?: ApiHandlerCreateMessageMetadata,
6161
): ApiStream {
62-
// Fail fast when the task is already cancelled before any model-catalog
63-
// work starts: the standardized AbortError wins over any failure the
64-
// fallible resolution could raise.
62+
// Establish the cancellation scope around model resolution: a
63+
// pre-aborted signal rejects before the lookup starts, and a signal
64+
// that fires while model metadata is loading settles on the
65+
// standardized AbortError; any other resolution failure propagates
66+
// unchanged.
6567
const externalAbortSignal = metadata?.abortSignal
66-
if (externalAbortSignal?.aborted) {
67-
throw createAbortError("Vercel AI Gateway")
68-
}
69-
70-
// Establish the cancellation scope before model resolution: a call
71-
// aborted while model metadata is loading must reject promptly instead
72-
// of waiting for the catalog lookup to settle. Abort failures from the
73-
// lookup itself are normalized to the provider AbortError; any other
74-
// resolution failure propagates unchanged.
75-
let resolved: Awaited<ReturnType<VercelAiGatewayHandler["fetchModel"]>>
76-
try {
77-
if (externalAbortSignal) {
78-
// Stryker disable next-line StringLiteral: the raw rejectOnAbort rejection is re-stamped by the catch's createAbortError below (its name "AbortError" always matches isRequestAborted), so this provider-name literal is unobservable
79-
resolved = await rejectOnAbort(this.fetchModel(), externalAbortSignal, "Vercel AI Gateway")
80-
} else {
81-
resolved = await this.fetchModel()
82-
}
83-
} catch (error) {
84-
if (isRequestAborted(error, externalAbortSignal)) {
85-
throw createAbortError("Vercel AI Gateway")
86-
}
87-
throw error
88-
}
68+
const resolved = await resolveModelWithAbort(() => this.fetchModel(), externalAbortSignal, "Vercel AI Gateway")
8969
const { id: modelId, info } = resolved
9070

9171
const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [

‎src/api/providers/zoo-gateway.ts‎

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import { addCacheBreakpoints } from "../transform/caching/vercel-ai-gateway"
2222
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../index"
2323
import { NOT_PROVIDED } from "./constants"
2424
import { RouterProvider } from "./router-provider"
25-
import { createAbortError, isRequestAborted, rejectOnAbort } from "./utils/abort-signal"
25+
import { createAbortError, isRequestAborted, resolveModelWithAbort } from "./utils/abort-signal"
2626

2727
function getApiErrorStatus(error: unknown): number | undefined {
2828
if (typeof error === "object" && error !== null && "status" in error) {
@@ -192,25 +192,11 @@ export class ZooGatewayHandler extends RouterProvider implements SingleCompletio
192192

193193
this.ensureAuthenticated()
194194

195-
// Establish the cancellation scope before model resolution: a call
196-
// aborted while model metadata is loading must reject promptly instead
197-
// of waiting for the catalog lookup to settle. Abort failures from the
198-
// lookup itself are normalized to the provider AbortError; any other
199-
// resolution failure propagates unchanged.
200-
let resolved: Awaited<ReturnType<ZooGatewayHandler["fetchModel"]>>
201-
try {
202-
if (externalAbortSignal) {
203-
// Stryker disable next-line StringLiteral: the raw rejectOnAbort rejection is re-stamped by the catch's createAbortError below (its name "AbortError" always matches isRequestAborted), so this provider-name literal is unobservable
204-
resolved = await rejectOnAbort(this.fetchModel(), externalAbortSignal, "Zoo Gateway")
205-
} else {
206-
resolved = await this.fetchModel()
207-
}
208-
} catch (error) {
209-
if (isRequestAborted(error, externalAbortSignal)) {
210-
throw createAbortError("Zoo Gateway")
211-
}
212-
throw error
213-
}
195+
// Establish the cancellation scope around model resolution: a signal
196+
// that fires while model metadata is loading settles on the
197+
// standardized AbortError; any other resolution failure propagates
198+
// unchanged.
199+
const resolved = await resolveModelWithAbort(() => this.fetchModel(), externalAbortSignal, "Zoo Gateway")
214200
const { id: modelId, info } = resolved
215201

216202
const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [

0 commit comments

Comments
 (0)