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.

32 changes: 32 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
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
19 changes: 9 additions & 10 deletions src/api/providers/utils/__tests__/abort-signal.spec.ts

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

24 changes: 24 additions & 0 deletions src/api/providers/utils/__tests__/request-timeout.spec.ts

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

11 changes: 0 additions & 11 deletions src/api/providers/utils/abort-signal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,17 +296,6 @@ export function settleOnAbort<T>(
return signal ? rejectOnAbort(pending, signal, providerName) : pending
}

/**
* A per-request timeout is not a caller cancellation: report it as the distinct TimeoutError so
* callers can tell a stalled request from a user stop. A caller stop keeps using
* {@link createAbortError}; this error is only for a deadline the request itself hit.
*/
export function createTimeoutError(providerName: string): Error {
const timeoutError = new Error(`The ${providerName} completion request timed out`)
timeoutError.name = "TimeoutError"
return timeoutError
}

/**
* Whether an abort-shaped failure came from the merged signal's own timeout rather than from the
* caller's stop signal.
Expand Down
19 changes: 16 additions & 3 deletions src/api/providers/utils/request-timeout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,23 @@ export function getRequestTimeoutMs(timeoutMs?: number): number | undefined {
* Fresh error for a per-request deadline elapsing in a given phase.
*
* Distinct from the abort contract error on purpose: a timeout is not a user
* cancellation, and callers treat the two differently.
* cancellation, and callers treat the two differently. This is the only place the
* timeout error is built: `abort-signal.ts` used to carry a second, phase-less
* definition, so callers of the two modules assembled the same report from
* different code.
*
* `cause` carries the failure a deadline was detected through - a stalled response
* body, an SDK rejection raised after the timer fired - so the report keeps both
* the deadline that elapsed and what the request was waiting on. It is left unset
* when the caller has no underlying failure to report.
*/
export function createTimeoutError(providerName: string, phase: "model discovery" | "completion request"): Error {
const timeoutError = new Error(`The ${providerName} ${phase} timed out`)
export function createTimeoutError(
providerName: string,
phase: "model discovery" | "completion request",
cause?: unknown,
): Error {
const message = `The ${providerName} ${phase} timed out`
const timeoutError = cause === undefined ? new Error(message) : new Error(message, { cause })
timeoutError.name = "TimeoutError"
return timeoutError
}
Loading