Skip to content

Commit e57e88b

Browse files
refactor(api): use shared abort helpers from foundation utils in lm-studio and qwen-code
The per-provider copies of isRequestAborted / createAbortError / OpenAiRequestOptions were extracted into src/api/providers/utils/abort-signal.ts by foundation PR #1288 (commit a0117fb) following the CodeRabbit maintainability finding on this PR; the providers now import the shared helpers. createAbortError takes the provider name as a parameter; provider behavior and abort messages are unchanged.
1 parent 56b10d5 commit e57e88b

2 files changed

Lines changed: 21 additions & 91 deletions

File tree

‎src/api/providers/lm-studio.ts‎

Lines changed: 11 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -21,51 +21,16 @@ import { BaseProvider } from "./base-provider"
2121
import { RequestConfigBuilder } from "./config-builder/request-config-builder"
2222
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../index"
2323
import { getModelsFromCache } from "./fetchers/modelCache"
24-
import { mergeAbortSignalAndTimeout, throwIfAborted } from "./utils/abort-signal"
24+
import {
25+
mergeAbortSignalAndTimeout,
26+
throwIfAborted,
27+
createAbortError,
28+
isRequestAborted,
29+
type OpenAiRequestOptions,
30+
} from "./utils/abort-signal"
2531
import { handleOpenAIError } from "./utils/error-handler"
2632
import { extractReasoningFromDelta } from "./utils/extract-reasoning"
2733

28-
/**
29-
* Minimal request-options shape for the generic RequestConfigBuilder. The
30-
* SDK's `RequestOptions` declares `signal` as `AbortSignal | null | undefined`,
31-
* which does not satisfy the builder's base constraint, so the builder is typed
32-
* with only the options this provider sets. The built config is still
33-
* assignable to the SDK's `RequestOptions`.
34-
*/
35-
type OpenAiRequestOptions = {
36-
signal?: AbortSignal
37-
}
38-
39-
/**
40-
* Whether a failure indicates an aborted request: the caller's signal fired,
41-
* the SDK raised a native abort error, or the error carries the OpenAI SDK
42-
* abort error message (exactly "Request was aborted."). The message check
43-
* is an exact match on purpose: a substring match would misclassify
44-
* unrelated errors that merely mention aborting.
45-
*/
46-
function isRequestAborted(error: unknown, signal?: AbortSignal): boolean {
47-
const candidate = error as { name?: string; message?: string }
48-
return (
49-
Boolean(signal?.aborted) ||
50-
candidate?.name === "AbortError" ||
51-
candidate?.name === "APIUserAbortError" ||
52-
candidate?.message === "Request was aborted."
53-
)
54-
}
55-
56-
/**
57-
* Fresh error satisfying the Task.ts abort contract: `name ===
58-
* "AbortError"` and a message ending in "aborted" (no trailing period). The
59-
* OpenAI SDK's own abort error does not satisfy this contract (name "Error",
60-
* message "Request was aborted."), so raw SDK abort errors must be
61-
* normalized instead of rethrown.
62-
*/
63-
function createAbortError(): Error {
64-
const abortError = new Error("The LM Studio request was aborted")
65-
abortError.name = "AbortError"
66-
return abortError
67-
}
68-
6934
export class LmStudioHandler extends BaseProvider implements SingleCompletionHandler {
7035
protected options: ApiHandlerOptions
7136
private client: OpenAI
@@ -171,7 +136,7 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
171136
results = await this.client.chat.completions.create(params, createOptions)
172137
} catch (error) {
173138
if (isRequestAborted(error, externalSignal)) {
174-
throw createAbortError()
139+
throw createAbortError("LM Studio")
175140
}
176141
throw handleOpenAIError(error, this.providerName)
177142
}
@@ -248,7 +213,7 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
248213
} as const
249214
} catch (error) {
250215
if (isRequestAborted(error, externalSignal)) {
251-
throw createAbortError()
216+
throw createAbortError("LM Studio")
252217
}
253218
throw new Error(
254219
"Please check the LM Studio developer logs to debug what went wrong. You may need to load the model with a larger context length to work with Zoo Code's prompts.",
@@ -317,14 +282,14 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
317282
response = await this.client.chat.completions.create(params, createOptions)
318283
} catch (error) {
319284
if (isRequestAborted(error, requestSignal)) {
320-
throw createAbortError()
285+
throw createAbortError("LM Studio")
321286
}
322287
throw handleOpenAIError(error, this.providerName)
323288
}
324289
return response.choices[0]?.message.content || ""
325290
} catch (error) {
326291
if (isRequestAborted(error, requestSignal)) {
327-
throw createAbortError()
292+
throw createAbortError("LM Studio")
328293
}
329294
throw new Error(
330295
"Please check the LM Studio developer logs to debug what went wrong. You may need to load the model with a larger context length to work with Zoo Code's prompts.",

‎src/api/providers/qwen-code.ts‎

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,13 @@ import { ApiStream } from "../transform/stream"
1616
import { BaseProvider } from "./base-provider"
1717
import { RequestConfigBuilder } from "./config-builder/request-config-builder"
1818
import { extractReasoningFromDelta } from "./utils/extract-reasoning"
19-
import { mergeAbortSignalAndTimeout, throwIfAborted } from "./utils/abort-signal"
19+
import {
20+
mergeAbortSignalAndTimeout,
21+
throwIfAborted,
22+
createAbortError,
23+
isRequestAborted,
24+
type OpenAiRequestOptions,
25+
} from "./utils/abort-signal"
2026
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../index"
2127

2228
const QWEN_OAUTH_BASE_URL = "https://chat.qwen.ai"
@@ -54,47 +60,6 @@ function objectToUrlEncoded(data: Record<string, string>): string {
5460
.join("&")
5561
}
5662

57-
/**
58-
* Minimal request-options shape for the generic RequestConfigBuilder. The
59-
* SDK’s `RequestOptions` declares `signal` as `AbortSignal | null | undefined`,
60-
* which does not satisfy the builder’s base constraint, so the builder is typed
61-
* with only the options this provider sets. The built config is still
62-
* assignable to the SDK’s `RequestOptions`.
63-
*/
64-
type OpenAiRequestOptions = {
65-
signal?: AbortSignal
66-
}
67-
68-
/**
69-
* Whether a failure indicates an aborted request: the caller’s signal fired,
70-
* the SDK raised a native abort error, or the error carries the OpenAI SDK
71-
* abort error message (exactly "Request was aborted."). The message check
72-
* is an exact match on purpose: a substring match would misclassify
73-
* unrelated errors that merely mention aborting.
74-
*/
75-
function isRequestAborted(error: unknown, signal?: AbortSignal): boolean {
76-
const candidate = error as { name?: string; message?: string }
77-
return (
78-
Boolean(signal?.aborted) ||
79-
candidate?.name === "AbortError" ||
80-
candidate?.name === "APIUserAbortError" ||
81-
candidate?.message === "Request was aborted."
82-
)
83-
}
84-
85-
/**
86-
* Fresh error satisfying the Task.ts abort contract: `name ===
87-
* "AbortError"` and a message ending in "aborted" (no trailing period). The
88-
* OpenAI SDK’s own abort error does not satisfy this contract (name "Error",
89-
* message "Request was aborted."), so raw SDK abort errors must be
90-
* normalized instead of rethrown.
91-
*/
92-
function createAbortError(): Error {
93-
const abortError = new Error("The Qwen Code request was aborted")
94-
abortError.name = "AbortError"
95-
return abortError
96-
}
97-
9863
export class QwenCodeHandler extends BaseProvider implements SingleCompletionHandler {
9964
protected options: QwenCodeHandlerOptions
10065
private credentials: QwenOAuthCredentials | null = null
@@ -245,7 +210,7 @@ export class QwenCodeHandler extends BaseProvider implements SingleCompletionHan
245210
// Task.ts abort contract (name "AbortError", message ending in
246211
// "aborted") instead of rethrowing the raw SDK abort error.
247212
if (isRequestAborted(error, externalSignal)) {
248-
throw createAbortError()
213+
throw createAbortError("Qwen Code")
249214
}
250215
if (error.status === 401) {
251216
// Token expired, refresh and retry. The retry reuses apiCall’s
@@ -256,7 +221,7 @@ export class QwenCodeHandler extends BaseProvider implements SingleCompletionHan
256221
// A stop can land while the refresh await is in flight — re-check
257222
// before the retried request goes out so it is not sent.
258223
if (externalSignal?.aborted) {
259-
throw createAbortError()
224+
throw createAbortError("Qwen Code")
260225
}
261226
const client = this.ensureClient()
262227
client.apiKey = this.credentials.access_token
@@ -402,7 +367,7 @@ export class QwenCodeHandler extends BaseProvider implements SingleCompletionHan
402367
}
403368
} catch (error) {
404369
if (isRequestAborted(error, externalSignal)) {
405-
throw createAbortError()
370+
throw createAbortError("Qwen Code")
406371
}
407372
throw error
408373
} finally {

0 commit comments

Comments
 (0)