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
2,918 changes: 2,733 additions & 185 deletions src/api/providers/__tests__/opencode-go.spec.ts

Large diffs are not rendered by default.

1,142 changes: 1,138 additions & 4 deletions src/api/providers/__tests__/unbound.spec.ts

Large diffs are not rendered by default.

773 changes: 771 additions & 2 deletions src/api/providers/__tests__/vercel-ai-gateway.spec.ts

Large diffs are not rendered by default.

821 changes: 820 additions & 1 deletion src/api/providers/__tests__/zoo-gateway.spec.ts

Large diffs are not rendered by default.

501 changes: 404 additions & 97 deletions src/api/providers/opencode-go.ts

Large diffs are not rendered by default.

178 changes: 142 additions & 36 deletions src/api/providers/unbound.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Anthropic } from "@anthropic-ai/sdk"
import OpenAI from "openai"
import OpenAI, { APIConnectionTimeoutError } from "openai"

import {
type ModelInfo,
Expand Down Expand Up @@ -29,6 +29,15 @@ import type {
} from "../index"
import { handleOpenAIError } from "./utils/error-handler"
import { applyRouterToolPreferences } from "./utils/router-tool-preferences"
import {
bridgeExternalAbortSignal,
createAbortError,
hasUsableTimeout,
isRequestAborted,
resolveModelWithAbort,
settleOnAbort,
toSdkTimeoutMs,
} from "./utils/abort-signal"
import { extractReasoningFromDelta } from "./utils/extract-reasoning"

// Unbound usage includes extra fields for Anthropic cache tokens.
Expand Down Expand Up @@ -152,14 +161,21 @@ export class UnboundHandler extends BaseProvider implements SingleCompletionHand
messages: Anthropic.Messages.MessageParam[],
metadata?: ApiHandlerCreateMessageMetadata,
): ApiStream {
// Establish the cancellation scope around model resolution: a
// pre-aborted signal rejects before the lookup starts, and a signal
// that fires while model metadata is loading settles on the
// standardized AbortError; any other resolution failure propagates
// unchanged.
const externalAbortSignal = metadata?.abortSignal
const resolved = await resolveModelWithAbort(() => this.fetchModel(), externalAbortSignal, "Unbound")
const {
id: model,
info,
maxTokens: max_tokens,
temperature,
reasoningEffort: reasoning_effort,
reasoning: thinking,
} = await this.fetchModel()
} = resolved

const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [
{ role: "system", content: systemPrompt },
Expand All @@ -185,51 +201,109 @@ export class UnboundHandler extends BaseProvider implements SingleCompletionHand
tool_choice: metadata?.tool_choice,
}

let stream
// Per-request signal bridged from the caller's abort signal via the shared helper
// (pre-aborted guard, once-only listener, retained cleanup). The never-aborting
// fallback preserves the previous always-signal behaviour when the caller has no
// external signal, and dispose() detaches the listener when the request ends.
const { signal: bridgedSignal, dispose: detachExternalAbortSignal } =
bridgeExternalAbortSignal(externalAbortSignal)
const requestSignal = bridgedSignal ?? new AbortController().signal

try {
stream = await this.client.chat.completions.create(completionParams)
} catch (error) {
throw handleOpenAIError(error, this.providerName)
}
let lastUsage: any = undefined
let stream
try {
const pendingStream = this.client.chat.completions.create(completionParams, { signal: requestSignal })
// Race the create: the SDK sleeps out a Retry-After backoff without observing the signal,
// so awaiting it alone can outlive the cancel (same shape as zoo-gateway.ts's completion path).
stream = await settleOnAbort(pendingStream, bridgedSignal, "Unbound")
} catch (error) {
// Preserve abort identity (series standard): a cancelled request
// must surface as a DOM-standard AbortError, not a wrapped
// completion error.
if (isRequestAborted(error, externalAbortSignal)) {
throw createAbortError("Unbound")
}
throw handleOpenAIError(error, this.providerName)
}
let lastUsage: OpenAI.Completions.CompletionUsage | undefined

try {
for await (const chunk of stream) {
// The SDK swallows the body-read abort and can end the iterator normally, so leave the loop
// here and let the check below raise the contract AbortError.
if (externalAbortSignal?.aborted) {
break
}

for await (const chunk of stream) {
const delta = chunk.choices[0]?.delta
const delta = chunk.choices[0]?.delta

const reasoningText = extractReasoningFromDelta(delta)
if (reasoningText) {
yield { type: "reasoning", text: reasoningText }
}
const reasoningText = extractReasoningFromDelta(delta)
if (reasoningText) {
yield { type: "reasoning", text: reasoningText }
}

if (delta?.content) {
yield { type: "text", text: delta.content }
}
if (delta?.content) {
if (externalAbortSignal?.aborted) {
throw createAbortError("Unbound") // a cancel must not emit this frame's text
}

yield { type: "text", text: delta.content }
Comment thread
easonLiangWorldedtech marked this conversation as resolved.
Resolved
}

// Handle native tool calls
if (delta && "tool_calls" in delta && Array.isArray(delta.tool_calls)) {
for (const toolCall of delta.tool_calls) {
if (externalAbortSignal?.aborted) {
throw createAbortError("Unbound") // a cancel must not emit the rest of this chunk
}

yield {
type: "tool_call_partial",
index: toolCall.index,
id: toolCall.id,
name: toolCall.function?.name,
arguments: toolCall.function?.arguments,
}
}
}

// Handle native tool calls
if (delta && "tool_calls" in delta && Array.isArray(delta.tool_calls)) {
for (const toolCall of delta.tool_calls) {
yield {
type: "tool_call_partial",
index: toolCall.index,
id: toolCall.id,
name: toolCall.function?.name,
arguments: toolCall.function?.arguments,
if (chunk.usage) {
lastUsage = chunk.usage
}
}
}

if (chunk.usage) {
lastUsage = chunk.usage
}
}
// A cancel the SDK swallowed ends the iterator without a throw: check the caller signal
// before emitting the retained usage, so a cancelled turn does not read as completed.
if (externalAbortSignal?.aborted) {
throw createAbortError("Unbound")
}

if (lastUsage) {
yield this.processUsageMetrics(lastUsage, info)
if (lastUsage) {
yield this.processUsageMetrics(lastUsage, info)
}
} catch (error) {
// Preserve abort identity (series standard): a cancellation that
// surfaces after the stream has started must also normalize to
// the standardized AbortError, not the raw SDK rejection.
if (isRequestAborted(error, externalAbortSignal)) {
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
throw createAbortError("Unbound")
}
throw error
}
} finally {
detachExternalAbortSignal()
}
}

async completePrompt(prompt: string, _options?: CompletePromptOptions): Promise<string> {
const { id: model, maxTokens: max_tokens, temperature } = await this.fetchModel()
async completePrompt(prompt: string, options?: CompletePromptOptions): Promise<string> {
// Model resolution must honour the caller's cancellation scope the same
// way createMessage does: a signal that fires during the lookup settles on
// the standardized AbortError instead of waiting for the catalog to finish.
const {
id: model,
maxTokens: max_tokens,
temperature,
} = await resolveModelWithAbort(() => this.fetchModel(), options?.abortSignal, "Unbound")

const openAiMessages: OpenAI.Chat.ChatCompletionMessageParam[] = [{ role: "system", content: prompt }]

Expand All @@ -239,12 +313,44 @@ export class UnboundHandler extends BaseProvider implements SingleCompletionHand
messages: openAiMessages,
temperature: temperature,
}
// Build request options with abortSignal and/or timeout.
// timeoutMs <= 0 means "no explicit timeout": omit the SDK timeout
// option entirely — the OpenAI SDK treats timeout: 0 as an immediate
// abort, which would cancel the request right away.
const createOptions: OpenAI.RequestOptions = {}
// The SDK adds an abort listener to whatever signal it is handed and never removes it, so the
// caller's signal is bridged to a per-request signal that the finally below disposes.
const { signal: bridgedSignal, dispose: detachExternalAbortSignal } = bridgeExternalAbortSignal(
options?.abortSignal,
)
if (bridgedSignal) {
createOptions.signal = bridgedSignal
}
const timeoutMs = options?.timeoutMs
if (hasUsableTimeout(timeoutMs)) {
createOptions.timeout = toSdkTimeoutMs(timeoutMs)
Comment thread
easonLiangWorldedtech marked this conversation as resolved.
Resolved
}
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved

let response: OpenAI.Chat.ChatCompletion
try {
response = await this.client.chat.completions.create(completionParams)
const pendingCompletion = this.client.chat.completions.create(completionParams, createOptions)
// Race the completion: the SDK does not observe the signal while it waits out a retry delay.
response = await settleOnAbort(pendingCompletion, bridgedSignal, "Unbound")
Comment thread
easonLiangWorldedtech marked this conversation as resolved.
Resolved
} catch (error) {
// Preserve abort identity (series standard): caller-initiated
// cancellations and request timeouts must surface as a
// DOM-standard AbortError, not a wrapped completion error. The
// OpenAI SDK reports both with messages ending in a period
// ("Request was aborted.", "Request timed out."), which would not
// match task-level abort detection (message ending in "aborted").
// SDK request timeouts are not aborts, but the series standard maps
// them to the same AbortError identity as caller cancellations.
if (isRequestAborted(error, options?.abortSignal) || error instanceof APIConnectionTimeoutError) {
throw createAbortError("Unbound")
}
throw handleOpenAIError(error, this.providerName)
} finally {
detachExternalAbortSignal()
}
return response.choices[0]?.message.content || ""
}
Expand Down
Loading
Loading