Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/api/providers/__tests__/friendli.spec.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

67 changes: 67 additions & 0 deletions src/api/providers/__tests__/openai-codex.spec.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions src/api/providers/__tests__/qwen-code.spec.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 7 additions & 3 deletions src/api/providers/friendli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@
import {
bridgeExternalAbortSignal,
createAbortError,
createTimeoutError,
hasUsableTimeout,
isRequestAborted,
isTimeoutAbort,
mergeAbortSignalAndTimeout,
toSdkTimeoutMs,
} from "./utils/abort-signal"
import { createTimeoutError } from "./utils/request-timeout"
import { NOT_PROVIDED } from "./constants"
import { getModels } from "./fetchers/modelCache"
import { getFriendliModels } from "./fetchers/friendli"
Expand Down Expand Up @@ -109,7 +109,8 @@
} catch {
// Classified below: only a cut-short wait is fatal.
}
if (isTimeoutAbort(signal)) throw createTimeoutError(this.providerName)
// The wait is the model catalog load, so a deadline seen here is model discovery.
if (isTimeoutAbort(signal)) throw createTimeoutError(this.providerName, "model discovery")
if (signal?.aborted) throw createAbortError(this.providerName)
}

Expand Down Expand Up @@ -272,7 +273,10 @@
const deadline = hasUsableTimeout(timeoutMs) ? Date.now() + timeoutMs : undefined
await this.waitForCatalog(options?.abortSignal, timeoutMs)
const remainingMs = deadline === undefined ? undefined : deadline - Date.now()
if (remainingMs !== undefined && remainingMs <= 0) throw createTimeoutError(this.providerName)
// The wait succeeded but spent the shared deadline: the completion never got a turn.
if (remainingMs !== undefined && remainingMs <= 0) {

Check warning on line 277 in src/api/providers/friendli.ts

View workflow job for this annotation

GitHub Actions / mutation-diff

Mutation test advisory

src/api/providers/friendli.ts:277: Survived ConditionalExpression mutant (replacement: true). See the job summary for the complete list and resolution guidance.
throw createTimeoutError(this.providerName, "completion request")
}
const { id: modelId } = this.getModel()
const friendliExtra = this.buildFriendliReasoningParams()

Expand Down
6 changes: 4 additions & 2 deletions src/api/providers/lm-studio.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ import {
mergeAbortSignalAndTimeout,
throwIfAborted,
createAbortError,
createTimeoutError,
isRequestAborted,
isTimeoutAbort,
settleOnAbort,
type OpenAiRequestOptions,
} from "./utils/abort-signal"
import { createTimeoutError } from "./utils/request-timeout"
import { handleOpenAIError } from "./utils/error-handler"
import { extractReasoningFromDelta } from "./utils/extract-reasoning"

Expand Down Expand Up @@ -330,7 +330,9 @@ export class LmStudioHandler extends BaseProvider implements SingleCompletionHan
* and the outer completion catch, so it lives in one helper.
*/
private toCompletionAbortError(requestSignal: AbortSignal | undefined): Error {
return isTimeoutAbort(requestSignal) ? createTimeoutError("LM Studio") : createAbortError("LM Studio")
return isTimeoutAbort(requestSignal)
? createTimeoutError("LM Studio", "completion request")
: createAbortError("LM Studio")
}

async completePrompt(prompt: string, options?: CompletePromptOptions): Promise<string> {
Expand Down
31 changes: 14 additions & 17 deletions src/api/providers/openai-codex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import { sanitizeOpenAiCallId } from "../../utils/tool-id"
import { openAiCodexOAuthManager } from "../../integrations/openai-codex/oauth"
import { t } from "../../i18n"
import {
bridgeExternalAbortSignal,
createAbortError,
isRequestAborted,
mergeAbortSignalAndTimeout,
Expand Down Expand Up @@ -510,22 +511,14 @@ export class OpenAiCodexHandler extends BaseProvider implements SingleCompletion
effectiveSessionId: string,
abortSignal?: AbortSignal,
): ApiStream {
// Request-local controller: a stale abort arriving after this request finishes must never
// reach the controller of a later request, so the bridge listener captures this controller
// directly.
const abortController = new AbortController()

// A caller's signal has to be linked rather than used directly, since both transports below
// abort through the request controller. Without this the signal never reaches the wire.
const abortFromCaller = () => abortController.abort()

if (abortSignal) {
if (abortSignal.aborted) {
abortController.abort()
} else {
abortSignal.addEventListener("abort", abortFromCaller, { once: true })
}
}
// A caller's signal has to be linked rather than used directly, since both transports
// below abort through one request-local controller: a stale abort arriving after this
// request finishes must never reach the controller of a later one. The bridge hands
// back that controller, relays a caller abort into it (a caller that already aborted
// reaches it the same way a live one does) and returns the handle the `finally` below
// uses to detach it.
const { controller: abortController, dispose: removeExternalAbortListener } =
bridgeExternalAbortSignal(abortSignal)

try {
// Prefer OpenAI SDK streaming (same approach as openai-native) so event handling
Expand Down Expand Up @@ -609,7 +602,11 @@ export class OpenAiCodexHandler extends BaseProvider implements SingleCompletion
)
}
} finally {
abortSignal?.removeEventListener("abort", abortFromCaller)
// Detach the bridge's listener from the caller's long-lived signal: this runs on
// completion, on failure, and on the consumer stopping the iteration early, so a
// finished request never leaves a listener (and this request's controller) retained
// by a signal that outlives it.
removeExternalAbortListener()
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/api/providers/qwen-code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ import {
isRequestAborted,
settleOnAbort,
type OpenAiRequestOptions,
createTimeoutError,
isTimeoutAbort,
} from "./utils/abort-signal"
import { createTimeoutError } from "./utils/request-timeout"
import type { SingleCompletionHandler, ApiHandlerCreateMessageMetadata, CompletePromptOptions } from "../index"

const QWEN_OAUTH_BASE_URL = "https://chat.qwen.ai"
Expand Down Expand Up @@ -492,7 +492,9 @@ export class QwenCodeHandler extends BaseProvider implements SingleCompletionHan
// TimeoutError when the merged signal ended because its own timer
// elapsed. The auth wait and the request both settle through here.
if (isRequestAborted(error, requestSignal) && isTimeoutAbort(requestSignal)) {
throw createTimeoutError("Qwen Code")
// The deadline was classified from an abort-shaped failure already in hand;
// keep it as cause so the report holds both the deadline and what it was seen through.
throw createTimeoutError("Qwen Code", "completion request", error)
}
throw error
}
Expand Down
Loading
Loading