GeezerVison is showing github.com — feat(core): wire Ark provider into aisdk-native and variant system by drpr · Pull Request #63482 · anomalyco/opencode
feat(core): wire Ark provider into aisdk-native and variant system
#63482
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement . We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Uh oh!
There was an error while loading. Please reload this page.
Show file tree
Hide file tree
Changes from all commits
Commits
File filter
Filter by extension
.json
(1)
.svg
(2)
.ts
(7)
All 3 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Hide whitespace
Diff view
Hide whitespace
There are no files selected for viewing
Original file line number
Diff line number
Diff line change
Expand Up
@@ -35,6 +35,7 @@
"./providers/amazon-bedrock/mantle/responses": "./src/providers/amazon-bedrock/mantle/responses.ts",
"./providers/anthropic": "./src/providers/anthropic.ts",
"./providers/anthropic-compatible": "./src/providers/anthropic-compatible.ts",
"./providers/ark": "./src/providers/ark.ts",
"./providers/assemblyai": "./src/providers/assemblyai.ts",
"./providers/azure": "./src/providers/azure.ts",
"./providers/azure/chat": "./src/providers/azure/chat.ts",
Expand Down
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,112 @@
/**
* Ark — Volcengine Ark Chat API provider.
*
* Shared by three provider IDs that all target the same Ark API surface:
* - `volcengine` (pay-as-you-go, ark.cn-beijing.volces.com/api/v3)
* - `volcengine-coding-plan` (subscription tier, ark.cn-beijing.volces.com/api/coding/v3)
* - `byteplus` (overseas, ark.ap-southeast.bytepluses.com/api/v3)
*
* The Ark API is OpenAI Chat Completions–compatible with two extensions:
* 1. `thinking: { type: "enabled" | "disabled" }` — a toggle for deep
* reasoning, used instead of the OpenAI `reasoning_effort` level.
* `auto` is documented but rejected by every model except
* doubao-seed-1-6-flash, so it is never sent.
* 2. `reasoning_content` in the streaming response — the field that
* carries the reasoning trace (set via `compatibility.reasoningField`).
*
* The base URL below is only a fallback: the model catalog's `api.base_url`
* names the endpoint of each provider (and each coding-plan tier), and the
* catalog value wins over this one.
*/
import { Effect, Schema } from "effect"
import type { ProviderPackage } from "../provider-package.js"
import { OpenAIChat } from "../protocols/openai-chat.js"
import { ProviderShared } from "../protocols/shared.js"
import type { ProviderAuthOption } from "../route/auth-options.js"
import { Route } from "../route/client.js"
import { Endpoint } from "../route/endpoint.js"
import { Protocol } from "../route/protocol.js"
import { ProviderID, type LLMRequest, type ModelID, type RequestOptions } from "../schema/index.js"
import { bearerRouteDefaults, toConfig } from "./settings.js"
export const id = ProviderID.make("ark")
const baseURL = "https://ark.cn-beijing.volces.com/api/v3"
export type ChatOptionsInput = {
/** Ark's deep-reasoning switch. Omitted leaves the model's own default. */
readonly thinking?: { readonly type: "enabled" | "disabled" | (string & {}) }
readonly reasoningEffort?: string
}
export type Config = Omit<RequestOptions, "providerOptions"> &
ProviderAuthOption<"optional"> & {
/** Overrides the selected API's base URL, including its version prefix. */
readonly baseURL?: string
readonly providerOptions?: ChatOptionsInput
}
export type Settings = ProviderPackage.Settings &
ChatOptionsInput & {
readonly apiKey?: string | false
readonly baseURL?: string
}
const ChatOptions = Schema.Struct({
thinking: Schema.optional(Schema.Struct({ type: Schema.String })),
reasoningEffort: Schema.optional(Schema.String),
})
const chatProtocol = Protocol.make({
id: "ark-chat",
body: {
schema: Schema.Struct({ ...OpenAIChat.bodyFields, thinking: ChatOptions.fields.thinking }),
from: Effect.fn("Ark.chatFromRequest")(function* (request: LLMRequest) {
const options = yield* ProviderShared.validateWith(Schema.decodeUnknownEffect(ChatOptions))(
request.providerOptions ?? {},
)
return { ...(yield* OpenAIChat.protocol.body.from(request)), thinking: options.thinking }
}),
},
stream: OpenAIChat.protocol.stream,
})
export const route = Route.make({
id: "ark-chat",
provider: id,
providerMetadataKey: "ark",
protocol: chatProtocol,
endpoint: Endpoint.path("/chat/completions", { baseURL }),
framing: OpenAIChat.framing,
})
export const routes = [route]
export const configure = (input: Config = {}) => {
const configured = route.with(
bearerRouteDefaults(input, ["ARK_API_KEY", "BYTEPLUS_API_KEY", "ARK_CODING_PLAN_API_KEY"], baseURL),
)
return {
id,
model: (modelID: string | ModelID) =>
configured.model<ChatOptionsInput>({
id: modelID,
compatibility: {
reasoningField: "reasoning_content",
maxTokensField: "max_tokens",
},
capabilities: { tools: { strict: false }, promptCacheKey: true },
}),
chat: (modelID: string | ModelID) => configured.model<ChatOptionsInput>({ id: modelID }),
configure,
}
}
export const provider = configure()
export const model: ProviderPackage.Model<Settings, ChatOptionsInput> = (modelID, settings) =>
configure(toConfig(settings)).model(modelID)
export const chat = provider.chat
export * as Ark from "./ark.js"
Original file line number
Diff line number
Diff line change
Expand Up
@@ -6,6 +6,7 @@ export * as AmazonBedrockMantle from "./amazon-bedrock-mantle.js"
export * as AssemblyAI from "./assemblyai.js"
export * as Azure from "./azure.js"
export * as Baseten from "./baseten.js"
export * as Ark from "./ark.js"
export * as Cartesia from "./cartesia.js"
export * as Cerebras from "./cerebras.js"
export * as Cohere from "./cohere.js"
Expand Down
Original file line number
Diff line number
Diff line change
@@ -0,0 +1,100 @@
import { describe, expect, test } from "bun:test"
import { ConfigProvider, Effect } from "effect"
import { Headers } from "effect/http"
import { ai } from "../../src/effect.js"
import { Ark } from "../../src/providers.js"
import { compileRequest } from "../../src/route/client.js"
import { Endpoint } from "../../src/route/endpoint.js"
import { it } from "../lib/effect.js"
describe("Ark provider", () => {
test("owns the Ark endpoint, provider identity and Chat compatibility", () => {
const model = Ark.configure({ apiKey: "fixture" }).model("glm-5-2-260617")
expect(model).toMatchObject({
provider: "ark",
compatibility: {
reasoningField: "reasoning_content",
maxTokensField: "max_tokens",
},
capabilities: { tools: { strict: false }, promptCacheKey: true },
route: { id: "ark-chat", protocol: "ark-chat" },
})
expect(model.route.providerMetadataKey).toBe("ark")
})
it.effect("sends Chat Completions to Ark's base URL", () =>
Effect.gen(function* () {
const model = Ark.configure({ apiKey: "fixture" }).model("glm-5-2-260617")
const request = ai.request({ model, prompt: "Hello" })
const compiled = yield* compileRequest(request)
const url = Endpoint.render(model.route.endpoint, { request, body: compiled.body }).toString()
expect(url).toBe("https://ark.cn-beijing.volces.com/api/v3/chat/completions")
const headers = yield* model.route.auth.apply({
request,
method: "POST",
url,
body: "{}",
headers: Headers.empty,
})
expect(headers.authorization).toBe("Bearer fixture")
}),
)
it.effect("reads any Ark environment variable", () =>
Effect.gen(function* () {
for (const env of [
{ ARK_API_KEY: "ark-secret" },
{ BYTEPLUS_API_KEY: "byteplus-secret" },
{ ARK_CODING_PLAN_API_KEY: "coding-secret" },
]) {
yield* Effect.gen(function* () {
const model = Ark.configure().model("glm-5-2-260617")
const request = ai.request({ model, prompt: "Hello" })
const compiled = yield* compileRequest(request)
const url = Endpoint.render(model.route.endpoint, { request, body: compiled.body }).toString()
const headers = yield* model.route.auth.apply({
request,
method: "POST",
url,
body: "{}",
headers: Headers.empty,
})
expect(headers.authorization).toBe(`Bearer ${Object.values(env)[0]}`)
}).pipe(Effect.provide(ConfigProvider.layer(ConfigProvider.fromEnv({ env }))))
}
}),
)
it.effect("keeps deep reasoning on Ark's own `thinking` switch", () =>
Effect.gen(function* () {
const model = Ark.configure({ apiKey: "fixture" }).model("glm-5-2-260617")
const compiled = yield* compileRequest(
ai.request({
model,
prompt: "Hello",
maxTokens: 128,
providerOptions: { thinking: { type: "disabled" } },
}),
)
expect(compiled.body).toMatchObject({
thinking: { type: "disabled" },
max_tokens: 128,
stream_options: { include_usage: true },
})
expect(compiled.body).not.toHaveProperty("max_completion_tokens")
}),
)
it.effect("honors a custom Ark base URL", () =>
Effect.gen(function* () {
const model = Ark.configure({ apiKey: "fixture", baseURL: "https://gateway.example/api/v3" }).model(
"glm-5-2-260617",
)
const request = ai.request({ model, prompt: "Hello" })
const compiled = yield* compileRequest(request)
expect(Endpoint.render(model.route.endpoint, { request, body: compiled.body }).toString()).toBe(
"https://gateway.example/api/v3/chat/completions",
)
}),
)
})
Original file line number
Diff line number
Diff line change
Expand Up
@@ -147,6 +147,13 @@ const HOSTS: Readonly<Record<string, Readonly<Record<string, string>>>> = {
"alibaba-token-plan": protocols("alibaba"),
"alibaba-token-plan-cn": protocols("alibaba"),
baseten: { "@ai-sdk/openai-compatible": "@opencode/ai/providers/baseten" },
// Volcengine Ark API — three provider IDs share the same Ark Chat protocol
// package. The model catalog (models.dev / local.txt) sets per-model base
// URLs so BytePlus overseas traffic goes to ark.ap-southeast.bytepluses.com
// while mainland traffic goes to ark.cn-beijing.volces.com.
byteplus: { "@ai-sdk/openai-compatible": "@opencode/ai/providers/ark" },
volcengine: { "@ai-sdk/openai-compatible": "@opencode/ai/providers/ark" },
"volcengine-coding-plan": { "@ai-sdk/openai-compatible": "@opencode/ai/providers/ark" },
"cloudflare-ai-gateway": {
"@ai-sdk/anthropic": "@opencode/ai/providers/cloudflare-ai-gateway",
"@ai-sdk/openai": "@opencode/ai/providers/cloudflare-ai-gateway",
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -59,6 +59,7 @@ const builtins = new Map<string, () => Promise<unknown>>([
["@opencode/ai/providers/azure/messages", () => import("@opencode/ai/providers/azure/messages")],
["@opencode/ai/providers/baseten", () => import("@opencode/ai/providers/baseten")],
["@opencode/ai/providers/baseten/decisions", () => import("@opencode/ai/providers/baseten/decisions")],
["@opencode/ai/providers/ark", () => import("@opencode/ai/providers/ark")],
Comment thread
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more .
@opencode/ai/providers/ark doesn't exist on this branch or on dev, so tsc -b in packages/core fails here with TS2307. This needs #63481 to land first, or the two PRs need to land together.
Sorry, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
["@opencode/ai/providers/cerebras", () => import("@opencode/ai/providers/cerebras")],
["@opencode/ai/providers/cloudflare-ai-gateway", () => import("@opencode/ai/providers/cloudflare-ai-gateway")],
[
Expand Down
Original file line number
Diff line number
Diff line change
Expand Up
@@ -149,6 +149,31 @@ const nvidiaTemplate = (chat_template_kwargs: Record<string, unknown>): Overlay
body: { chat_template_kwargs },
})
// Ark's Chat API takes deep reasoning as a `thinking` switch rather than an effort level.
// Shared by volcengine, volcengine-coding-plan, and byteplus (all Ark API).
//
// Ark rejects `thinking.type=disabled` combined with a graded effort, so a variant that
// turns thinking off must not also carry a reasoning effort. The catalog lists the
// effort levels each model separates, and `none` and `minimal` are the off values: a
// model with a toggle declares it, and a model without one relies on the effort alone.
const arkChat: Protocol = (model, support) => {
switch (support.type) {
case "effort":
return efforts(support.values ?? EFFORTS, (effort) => ({
...(effort === "none" || effort === "minimal"
? { body: { thinking: { type: "disabled" } } }
: { settings: { reasoningEffort: effort }, body: { thinking: { type: "enabled" } } }),
}))
case "toggle":
return toggle(
{ body: { thinking: { type: "disabled" } } },
{ body: { thinking: { type: "enabled" } } },
)
case "budget_tokens":
return []
}
}
const nvidiaToggle = (key: string): Variants =>
toggle(nvidiaTemplate({ [key]: false }), nvidiaTemplate({ [key]: true }))
Expand Down
Expand Up
@@ -633,6 +658,7 @@ const PROTOCOLS: Readonly<Record<string, Protocol>> = {
"@opencode/ai/providers/alibaba": alibabaChat,
"@opencode/ai/providers/alibaba/chat": alibabaChat,
"@opencode/ai/providers/baseten": basetenChat,
"@opencode/ai/providers/ark": arkChat,
"@opencode/ai/providers/cerebras": openaiChat,
"@opencode/ai/providers/cloudflare-workers-ai": workersAIChat,
"@opencode/ai/providers/cohere/chat": openaiChat,
Expand Down
Loading
Sorry, something went wrong.
Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong.
Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number
Diff line number
Diff line change
Expand Up
@@ -92,6 +92,7 @@ export const iconNames = [
"claude-agent",
"chutes",
"cerebras",
"byteplus",
"berget",
"baseten",
"bailing",
Expand Down
Loading
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.