Skip to content

Commit 1f38eb5

Browse files
easonLiangWorldedtecheasonliang28edelauna
authored andcommitted
feat(api): add throwIfAborted helper and completePrompt options regression tests (Zoo-Code-Org#1288)
* feat(api): add throwIfAborted helper and completePrompt options regression tests Add a fast-fail throwIfAborted guard to the shared abort-signal utilities and regression tests for the CompletePromptOptions interface (added by Zoo-Code-Org#901). * 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). * test(abort-signal): assert throwIfAborted error message The mutation-diff gate reports a surviving StringLiteral mutant on the throwIfAborted message literal. Assert the exact message so a blanked literal fails the suite. --------- Co-authored-by: Eason Liang <[email protected]> Co-authored-by: easonLiangWorldedtech <[email protected]> Co-authored-by: edelauna <[email protected]> (cherry picked from commit e12a42e)
1 parent f4287ff commit 1f38eb5

3 files changed

Lines changed: 175 additions & 1 deletion

File tree

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import { describe, it, expect } from "vitest"
2+
3+
import type { CompletePromptOptions } from "../../index"
4+
5+
describe("CompletePromptOptions", () => {
6+
it("should allow abortSignal property", () => {
7+
const controller = new AbortController()
8+
const options: CompletePromptOptions = { abortSignal: controller.signal }
9+
expect(options.abortSignal).toBe(controller.signal)
10+
})
11+
12+
it("should allow timeoutMs property", () => {
13+
const options: CompletePromptOptions = { timeoutMs: 5000 }
14+
expect(options.timeoutMs).toBe(5000)
15+
})
16+
17+
it("should allow both abortSignal and timeoutMs together", () => {
18+
const controller = new AbortController()
19+
const options: CompletePromptOptions = { abortSignal: controller.signal, timeoutMs: 10000 }
20+
expect(options.abortSignal).toBe(controller.signal)
21+
expect(options.timeoutMs).toBe(10000)
22+
})
23+
24+
it("should allow empty options object", () => {
25+
const options: CompletePromptOptions = {}
26+
expect(options.abortSignal).toBeUndefined()
27+
expect(options.timeoutMs).toBeUndefined()
28+
})
29+
})

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

Lines changed: 88 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
import { mergeAbortSignalAndTimeout, mergeAbortSignals } 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", () => {
@@ -99,4 +105,85 @@ describe("abort-signal utilities", () => {
99105
expect(result.aborted).toBe(true)
100106
})
101107
})
108+
109+
describe("throwIfAborted", () => {
110+
it("does not throw when signal is undefined", () => {
111+
expect(() => throwIfAborted()).not.toThrow()
112+
})
113+
114+
it("does not throw when signal is not aborted", () => {
115+
const controller = new AbortController()
116+
117+
expect(() => throwIfAborted(controller.signal)).not.toThrow()
118+
})
119+
120+
it("throws an AbortError when signal is already aborted", () => {
121+
const controller = new AbortController()
122+
controller.abort()
123+
124+
let caught: unknown
125+
try {
126+
throwIfAborted(controller.signal)
127+
} catch (error) {
128+
caught = error
129+
}
130+
131+
expect(caught).toBeInstanceOf(Error)
132+
expect((caught as Error).name).toBe("AbortError")
133+
expect((caught as Error).message).toBe("This operation was aborted")
134+
})
135+
})
136+
137+
describe("isRequestAborted", () => {
138+
it("returns true when the caller signal is aborted", () => {
139+
const controller = new AbortController()
140+
controller.abort()
141+
142+
expect(isRequestAborted(new Error("boom"), controller.signal)).toBe(true)
143+
expect(isRequestAborted(undefined, controller.signal)).toBe(true)
144+
})
145+
146+
it("returns true for a native AbortError or the OpenAI SDK APIUserAbortError", () => {
147+
const native = new Error("This operation was aborted")
148+
native.name = "AbortError"
149+
expect(isRequestAborted(native)).toBe(true)
150+
151+
const sdk = new Error("whatever")
152+
sdk.name = "APIUserAbortError"
153+
expect(isRequestAborted(sdk)).toBe(true)
154+
})
155+
156+
it("matches the OpenAI SDK abort message exactly, not as a substring", () => {
157+
expect(isRequestAborted(new Error("Request was aborted."))).toBe(true)
158+
expect(isRequestAborted(new Error("Request was aborted"))).toBe(false)
159+
expect(isRequestAborted(new Error("Request was aborted. Please retry"))).toBe(false)
160+
})
161+
162+
it("returns false for unrelated errors, nullish errors, and live signals", () => {
163+
expect(isRequestAborted(new Error("the abort failed"))).toBe(false)
164+
expect(isRequestAborted(undefined)).toBe(false)
165+
expect(isRequestAborted(null)).toBe(false)
166+
167+
const controller = new AbortController()
168+
expect(isRequestAborted(new Error("boom"), controller.signal)).toBe(false)
169+
})
170+
})
171+
172+
describe("createAbortError", () => {
173+
it("builds an error satisfying the Task.ts abort contract", () => {
174+
const error = createAbortError("LM Studio")
175+
176+
expect(error).toBeInstanceOf(Error)
177+
expect(error.name).toBe("AbortError")
178+
expect(error.message).toBe("The LM Studio request was aborted")
179+
})
180+
181+
it("interpolates the provider name", () => {
182+
expect(createAbortError("Qwen Code").message).toBe("The Qwen Code request was aborted")
183+
})
184+
185+
it("returns a fresh error on each call", () => {
186+
expect(createAbortError("X")).not.toBe(createAbortError("X"))
187+
})
188+
})
102189
})

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,3 +35,61 @@ export function mergeAbortSignals(primarySignal: AbortSignal, secondarySignal?:
3535

3636
return AbortSignal.any([primarySignal, secondarySignal])
3737
}
38+
39+
/**
40+
* Throw an AbortError if the given signal is already aborted.
41+
*
42+
* Use as a fast-fail guard at the top of request-building code paths so
43+
* callers receive a consistent `name === "AbortError"` when the operation
44+
* was cancelled before it started, without building or issuing the request.
45+
*/
46+
export function throwIfAborted(signal?: AbortSignal): void {
47+
if (!signal?.aborted) {
48+
return
49+
}
50+
51+
const abortError = new Error("This operation was aborted")
52+
abortError.name = "AbortError"
53+
throw abortError
54+
}
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)