Skip to content

Commit a0117fb

Browse files
feat(api): add shared isRequestAborted and createAbortError helpers to abort-signal utils
The OpenAI-family provider PRs (Zoo-Code-Org#1309, Zoo-Code-Org#1311) carry per-provider copies of the same abort-detection helper (isRequestAborted) and the same abort-error constructor (createAbortError); only the provider name in the message differs. Per the CodeRabbit maintainability finding on Zoo-Code-Org#1309 (extract the shared abort helpers into utils/abort-signal.ts), these are now shared in the foundation utility: - isRequestAborted(error, signal?) - true when the caller signal fired, a native AbortError / OpenAI SDK APIUserAbortError was raised, or the message is exactly "Request was aborted." (exact match; a substring match would misclassify unrelated errors that merely mention aborting) - createAbortError(providerName) - fresh error with name === "AbortError" and message "The <providerName> request was aborted", satisfying the Task.ts abort contract - exported OpenAiRequestOptions type 7 new tests (isRequestAborted 4, createAbortError 3).
1 parent e61feb1 commit a0117fb

2 files changed

Lines changed: 101 additions & 1 deletion

File tree

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

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { mergeAbortSignalAndTimeout, mergeAbortSignals, throwIfAborted } from "../abort-signal"
1+
import {
2+
createAbortError,
3+
isRequestAborted,
4+
mergeAbortSignalAndTimeout,
5+
mergeAbortSignals,
6+
throwIfAborted,
7+
} from "../abort-signal"
28

39
describe("abort-signal utilities", () => {
410
describe("mergeAbortSignalAndTimeout", () => {
@@ -126,4 +132,57 @@ describe("abort-signal utilities", () => {
126132
expect((caught as Error).name).toBe("AbortError")
127133
})
128134
})
135+
136+
describe("isRequestAborted", () => {
137+
it("returns true when the caller signal is aborted", () => {
138+
const controller = new AbortController()
139+
controller.abort()
140+
141+
expect(isRequestAborted(new Error("boom"), controller.signal)).toBe(true)
142+
expect(isRequestAborted(undefined, controller.signal)).toBe(true)
143+
})
144+
145+
it("returns true for a native AbortError or the OpenAI SDK APIUserAbortError", () => {
146+
const native = new Error("This operation was aborted")
147+
native.name = "AbortError"
148+
expect(isRequestAborted(native)).toBe(true)
149+
150+
const sdk = new Error("whatever")
151+
sdk.name = "APIUserAbortError"
152+
expect(isRequestAborted(sdk)).toBe(true)
153+
})
154+
155+
it("matches the OpenAI SDK abort message exactly, not as a substring", () => {
156+
expect(isRequestAborted(new Error("Request was aborted."))).toBe(true)
157+
expect(isRequestAborted(new Error("Request was aborted"))).toBe(false)
158+
expect(isRequestAborted(new Error("Request was aborted. Please retry"))).toBe(false)
159+
})
160+
161+
it("returns false for unrelated errors, nullish errors, and live signals", () => {
162+
expect(isRequestAborted(new Error("the abort failed"))).toBe(false)
163+
expect(isRequestAborted(undefined)).toBe(false)
164+
expect(isRequestAborted(null)).toBe(false)
165+
166+
const controller = new AbortController()
167+
expect(isRequestAborted(new Error("boom"), controller.signal)).toBe(false)
168+
})
169+
})
170+
171+
describe("createAbortError", () => {
172+
it("builds an error satisfying the Task.ts abort contract", () => {
173+
const error = createAbortError("LM Studio")
174+
175+
expect(error).toBeInstanceOf(Error)
176+
expect(error.name).toBe("AbortError")
177+
expect(error.message).toBe("The LM Studio request was aborted")
178+
})
179+
180+
it("interpolates the provider name", () => {
181+
expect(createAbortError("Qwen Code").message).toBe("The Qwen Code request was aborted")
182+
})
183+
184+
it("returns a fresh error on each call", () => {
185+
expect(createAbortError("X")).not.toBe(createAbortError("X"))
186+
})
187+
})
129188
})

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

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,3 +52,44 @@ export function throwIfAborted(signal?: AbortSignal): void {
5252
abortError.name = "AbortError"
5353
throw abortError
5454
}
55+
56+
/**
57+
* Request options this series passes to the OpenAI SDK call. The SDK's
58+
* `RequestOptions` declares `signal` as `AbortSignal | null | undefined`,
59+
* which does not satisfy the builder's base constraint, so the builder is
60+
* typed with only the options this series sets. The built config is still
61+
* assignable to the SDK's `RequestOptions`.
62+
*/
63+
export type OpenAiRequestOptions = {
64+
signal?: AbortSignal
65+
}
66+
67+
/**
68+
* Whether a failure indicates an aborted request: the caller's signal fired,
69+
* the SDK raised a native abort error, or the error carries the OpenAI SDK
70+
* abort error message (exactly "Request was aborted."). The message check
71+
* is an exact match on purpose: a substring match would misclassify
72+
* unrelated errors that merely mention aborting.
73+
*/
74+
export function isRequestAborted(error: unknown, signal?: AbortSignal): boolean {
75+
const candidate = error as { name?: string; message?: string }
76+
return (
77+
Boolean(signal?.aborted) ||
78+
candidate?.name === "AbortError" ||
79+
candidate?.name === "APIUserAbortError" ||
80+
candidate?.message === "Request was aborted."
81+
)
82+
}
83+
84+
/**
85+
* Fresh error satisfying the Task.ts abort contract: `name ===
86+
* "AbortError"` and a message ending in "aborted" (no trailing period). The
87+
* OpenAI SDK's own abort error does not satisfy this contract (name "Error",
88+
* message "Request was aborted."), so raw SDK abort errors must be
89+
* normalized instead of rethrown.
90+
*/
91+
export function createAbortError(providerName: string): Error {
92+
const abortError = new Error(`The ${providerName} request was aborted`)
93+
abortError.name = "AbortError"
94+
return abortError
95+
}

0 commit comments

Comments
 (0)