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
10 changes: 7 additions & 3 deletions packages/core/src/aisdk.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ export interface LanguageEvent {
language?: LanguageModelV3
}

function wrapSSE(res: Response, ms: number, ctl: AbortController) {
function wrapSSE(res: Response, ms: number, ctl: AbortController, extraContentTypes: string[] = []) {
if (typeof ms !== "number" || ms <= 0) return res
if (!res.body) return res
if (!res.headers.get("content-type")?.includes("text/event-stream")) return res
const contentType = res.headers.get("content-type") ?? ""
const chunkedContentTypes = ["text/event-stream", "eventstream", ...extraContentTypes]
if (!chunkedContentTypes.some((type) => contentType.includes(type))) return res

const reader = res.body.getReader()
const body = new ReadableStream<Uint8Array>({
Expand Down Expand Up @@ -82,6 +84,8 @@ function prepareOptions(model: ModelV2.Info, pkg: string) {
const customFetch = options.fetch
const chunkTimeout = options.chunkTimeout
delete options.chunkTimeout
const chunkTimeoutContentTypes = options.chunkTimeoutContentTypes
delete options.chunkTimeoutContentTypes
options.fetch = async (input: Parameters<typeof fetch>[0], init?: RequestInit) => {
const opts = { ...(init ?? {}) }
const signals = [
Expand Down Expand Up @@ -115,7 +119,7 @@ function prepareOptions(model: ModelV2.Info, pkg: string) {
timeout: false,
})
if (!chunkAbortCtl || typeof chunkTimeout !== "number") return res
return wrapSSE(res, chunkTimeout, chunkAbortCtl)
return wrapSSE(res, chunkTimeout, chunkAbortCtl, Array.isArray(chunkTimeoutContentTypes) ? chunkTimeoutContentTypes : [])
}

return options
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/v1/config/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,10 @@ export const Info = Schema.Struct({
description:
"Timeout in milliseconds between streamed SSE chunks for this provider. If no chunk arrives within this window, the request is aborted.",
}),
chunkTimeoutContentTypes: Schema.optional(Schema.mutable(Schema.Array(Schema.String))).annotate({
description:
"Additional response content-type substrings (besides text/event-stream and eventstream) that should be monitored by chunkTimeout. Useful for providers using non-standard streaming content types.",
}),
}),
[Schema.Record(Schema.String, Schema.Any)],
),
Expand Down
10 changes: 7 additions & 3 deletions packages/opencode/src/provider/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ import { ProviderError } from "./error"

const OPENAI_HEADER_TIMEOUT_DEFAULT = 10_000

function wrapSSE(res: Response, ms: number, ctl: AbortController) {
function wrapSSE(res: Response, ms: number, ctl: AbortController, extraContentTypes: string[] = []) {
if (typeof ms !== "number" || ms <= 0) return res
if (!res.body) return res
if (!res.headers.get("content-type")?.includes("text/event-stream")) return res
const contentType = res.headers.get("content-type") ?? ""
const chunkedContentTypes = ["text/event-stream", "eventstream", ...extraContentTypes]
if (!chunkedContentTypes.some((type) => contentType.includes(type))) return res

const reader = res.body.getReader()
const body = new ReadableStream<Uint8Array>({
Expand Down Expand Up @@ -1709,6 +1711,8 @@ const layer = Layer.effect(
const headerTimeout = options["headerTimeout"]
delete options["chunkTimeout"]
delete options["headerTimeout"]
const chunkTimeoutContentTypes = options["chunkTimeoutContentTypes"]
delete options["chunkTimeoutContentTypes"]

options["fetch"] = async (input: any, init?: BunFetchRequestInit) => {
const fetchFn = customFetch ?? fetch
Expand All @@ -1734,7 +1738,7 @@ const layer = Layer.effect(
}).finally(() => headerTimeoutCtl?.clear())

if (!chunkAbortCtl) return res
return wrapSSE(res, chunkTimeout, chunkAbortCtl)
return wrapSSE(res, chunkTimeout, chunkAbortCtl, Array.isArray(chunkTimeoutContentTypes) ? chunkTimeoutContentTypes : [])
}

const bundledLoader = BUNDLED_PROVIDERS[model.api.npm]
Expand Down
Loading