mirror of
https://github.com/anomalyco/opencode.git
synced 2026-05-21 03:15:11 +00:00
feat(llm): support native Zen preview
This commit is contained in:
@@ -12,5 +12,5 @@ Safety boundary:
|
||||
|
||||
- AI SDK remains the default.
|
||||
- `OPENCODE_LLM_RUNTIME=native` is an opt-in hint, not a global replacement.
|
||||
- Native execution currently runs only for OpenAI API-key auth via `@ai-sdk/openai`.
|
||||
- Native execution currently runs only for OpenAI-compatible Responses models exposed through `@ai-sdk/openai`: direct `openai` API-key auth and console-managed `opencode`/Zen API-key config.
|
||||
- Unsupported providers, OpenAI OAuth, and missing API-key cases fall back to AI SDK.
|
||||
|
||||
@@ -2,6 +2,7 @@ import type { Auth } from "@/auth"
|
||||
import type { Provider } from "@/provider/provider"
|
||||
import { ProviderTransform } from "@/provider/transform"
|
||||
import { errorMessage } from "@/util/error"
|
||||
import { isRecord } from "@/util/record"
|
||||
import { asSchema, type ModelMessage, type Tool } from "ai"
|
||||
import { Effect } from "effect"
|
||||
import * as Stream from "effect/Stream"
|
||||
@@ -36,9 +37,10 @@ type StreamInput = {
|
||||
}
|
||||
|
||||
export function status(input: Pick<StreamInput, "model" | "provider" | "auth">): RuntimeStatus {
|
||||
if (input.model.providerID !== "openai") return { type: "unsupported", reason: "provider is not openai" }
|
||||
if (input.model.providerID !== "openai" && !input.model.providerID.startsWith("opencode"))
|
||||
return { type: "unsupported", reason: "provider is not openai or opencode" }
|
||||
if (input.model.api.npm !== "@ai-sdk/openai") return { type: "unsupported", reason: "provider package is not OpenAI" }
|
||||
if (input.auth?.type === "oauth") return { type: "unsupported", reason: "OpenAI OAuth is not supported" }
|
||||
if (input.auth?.type === "oauth") return { type: "unsupported", reason: "OAuth auth is not supported" }
|
||||
|
||||
const apiKey =
|
||||
input.auth?.type === "api"
|
||||
@@ -74,13 +76,20 @@ export function stream(input: StreamInput): StreamResult {
|
||||
topK: input.topK,
|
||||
maxOutputTokens: input.maxOutputTokens,
|
||||
providerOptions: ProviderTransform.providerOptions(input.model, input.providerOptions ?? {}),
|
||||
headers: input.headers,
|
||||
headers: { ...providerHeaders(input.provider.options.headers), ...input.headers },
|
||||
}),
|
||||
tools: nativeTools(input.tools, input),
|
||||
}),
|
||||
}
|
||||
}
|
||||
|
||||
function providerHeaders(value: unknown): Record<string, string> | undefined {
|
||||
if (!isRecord(value)) return undefined
|
||||
return Object.fromEntries(
|
||||
Object.entries(value).filter((entry): entry is [string, string] => typeof entry[1] === "string"),
|
||||
)
|
||||
}
|
||||
|
||||
function nativeSchema(value: unknown): JsonSchema {
|
||||
if (!value || typeof value !== "object") return { type: "object", properties: {} }
|
||||
if ("jsonSchema" in value && value.jsonSchema && typeof value.jsonSchema === "object")
|
||||
|
||||
31
packages/opencode/test/fixtures/recordings/session/native-zen-tool-call.json
vendored
Normal file
31
packages/opencode/test/fixtures/recordings/session/native-zen-tool-call.json
vendored
Normal file
File diff suppressed because one or more lines are too long
@@ -1,5 +1,5 @@
|
||||
import { NodeFileSystem } from "@effect/platform-node"
|
||||
import { HttpRecorder } from "@opencode-ai/http-recorder"
|
||||
import { HttpRecorder, Redactor } from "@opencode-ai/http-recorder"
|
||||
import { describe, expect } from "bun:test"
|
||||
import { tool } from "ai"
|
||||
import { Effect, Layer, Stream } from "effect"
|
||||
@@ -21,14 +21,22 @@ import type { ModelsDev } from "../../src/provider/models"
|
||||
import { TestInstance } from "../fixture/fixture"
|
||||
import { testEffect } from "../lib/effect"
|
||||
|
||||
const CASSETTE = "session/native-openai-tool-call"
|
||||
const OPENAI_CASSETTE = "session/native-openai-tool-call"
|
||||
const ZEN_CASSETTE = "session/native-zen-tool-call"
|
||||
const FIXTURES_DIR = path.join(import.meta.dir, "../fixtures/recordings")
|
||||
const OPENAI_API_KEY = process.env.OPENCODE_RECORD_OPENAI_API_KEY ?? process.env.OPENAI_API_KEY
|
||||
const CONSOLE_TOKEN = process.env.OPENCODE_RECORD_CONSOLE_TOKEN
|
||||
const ZEN_ORG_ID = process.env.OPENCODE_RECORD_ZEN_ORG_ID
|
||||
const ZEN_API_URL =
|
||||
process.env.OPENCODE_RECORD_ZEN_API_URL ?? "https://console.opencode.ai/proxy/connections/fixture/v1"
|
||||
|
||||
const shouldRecord = process.env.RECORD === "true"
|
||||
const canRun = shouldRecord
|
||||
const canRunOpenAI = shouldRecord
|
||||
? Boolean(OPENAI_API_KEY)
|
||||
: HttpRecorder.hasCassetteSync(CASSETTE, { directory: FIXTURES_DIR })
|
||||
: HttpRecorder.hasCassetteSync(OPENAI_CASSETTE, { directory: FIXTURES_DIR })
|
||||
const canRunZen = shouldRecord
|
||||
? Boolean(CONSOLE_TOKEN && ZEN_ORG_ID)
|
||||
: HttpRecorder.hasCassetteSync(ZEN_CASSETTE, { directory: FIXTURES_DIR })
|
||||
|
||||
async function loadFixture(providerID: string, modelID: string) {
|
||||
const data = await Filesystem.readJson<Record<string, ModelsDev.Provider>>(
|
||||
@@ -62,19 +70,47 @@ const openAIConfig = (model: ModelsDev.Provider["models"][string]): Partial<Conf
|
||||
},
|
||||
})
|
||||
|
||||
function recordedNativeLLMLayer() {
|
||||
const zenConfig = (model: ModelsDev.Provider["models"][string]): Partial<Config.Info> => ({
|
||||
enabled_providers: ["opencode"],
|
||||
provider: {
|
||||
opencode: {
|
||||
name: "OpenCode Zen",
|
||||
env: ["OPENCODE_CONSOLE_TOKEN"],
|
||||
npm: "@ai-sdk/openai-compatible",
|
||||
api: ZEN_API_URL,
|
||||
models: {
|
||||
[model.id]: JSON.parse(JSON.stringify(model)) as NonNullable<
|
||||
NonNullable<Config.Info["provider"]>[string]["models"]
|
||||
>[string],
|
||||
},
|
||||
options: {
|
||||
apiKey: CONSOLE_TOKEN ?? "fixture-console-token",
|
||||
headers: {
|
||||
"x-org-id": ZEN_ORG_ID ?? "fixture-org",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
})
|
||||
|
||||
function recordedNativeLLMLayer(cassette: string, metadata: Record<string, unknown>) {
|
||||
const cassetteService = HttpRecorder.Cassette.fileSystem({ directory: FIXTURES_DIR }).pipe(
|
||||
Layer.provide(NodeFileSystem.layer),
|
||||
)
|
||||
// Only the HTTP client is recorded; RequestExecutor and the opencode LLM stack remain real.
|
||||
const recorder = HttpRecorder.recordingLayer(CASSETTE, {
|
||||
const recorder = HttpRecorder.recordingLayer(cassette, {
|
||||
mode: shouldRecord ? "record" : "replay",
|
||||
metadata: {
|
||||
provider: "openai",
|
||||
protocol: "openai-responses",
|
||||
route: "openai-responses",
|
||||
tags: ["opencode", "native", "tool-call"],
|
||||
},
|
||||
metadata,
|
||||
redactor: Redactor.compose(
|
||||
Redactor.defaults({
|
||||
url: {
|
||||
transform: (url) => url.replace(/\/proxy\/connections\/[^/]+\/v1/, "/proxy/connections/{connection}/v1"),
|
||||
},
|
||||
}),
|
||||
{
|
||||
response: (snapshot) => ({ ...snapshot, body: snapshot.body.replace(/wrk_[A-Z0-9]+/g, "wrk_redacted") }),
|
||||
},
|
||||
),
|
||||
}).pipe(Layer.provide(FetchHttpClient.layer))
|
||||
const executor = RequestExecutor.layer.pipe(Layer.provide(recorder))
|
||||
const client = LLMClient.layer.pipe(Layer.provide(executor))
|
||||
@@ -96,14 +132,34 @@ function recordedNativeLLMLayer() {
|
||||
return Layer.mergeAll(providerLayer, llmLayer)
|
||||
}
|
||||
|
||||
const it = testEffect(recordedNativeLLMLayer())
|
||||
const recordedInstance = canRun ? it.instance : it.instance.skip
|
||||
const openAIIt = testEffect(
|
||||
recordedNativeLLMLayer(OPENAI_CASSETTE, {
|
||||
provider: "openai",
|
||||
protocol: "openai-responses",
|
||||
route: "openai-responses",
|
||||
tags: ["opencode", "native", "tool-call"],
|
||||
}),
|
||||
)
|
||||
const zenIt = testEffect(
|
||||
recordedNativeLLMLayer(ZEN_CASSETTE, {
|
||||
provider: "opencode",
|
||||
protocol: "openai-responses",
|
||||
route: "openai-responses",
|
||||
tags: ["opencode", "zen", "native", "tool-call"],
|
||||
}),
|
||||
)
|
||||
const recordedOpenAIInstance = canRunOpenAI ? openAIIt.instance : openAIIt.instance.skip
|
||||
const recordedZenInstance = canRunZen ? zenIt.instance : zenIt.instance.skip
|
||||
|
||||
const writeConfig = (directory: string, model: ModelsDev.Provider["models"][string]) =>
|
||||
const writeConfig = (
|
||||
directory: string,
|
||||
model: ModelsDev.Provider["models"][string],
|
||||
config: (model: ModelsDev.Provider["models"][string]) => Partial<Config.Info> = openAIConfig,
|
||||
) =>
|
||||
Effect.promise(() =>
|
||||
Bun.write(
|
||||
path.join(directory, "opencode.json"),
|
||||
JSON.stringify({ $schema: "https://opencode.ai/config.json", ...openAIConfig(model) }),
|
||||
JSON.stringify({ $schema: "https://opencode.ai/config.json", ...config(model) }),
|
||||
),
|
||||
)
|
||||
|
||||
@@ -136,7 +192,7 @@ const nativeRuntime = <A, E, R>(effect: Effect.Effect<A, E, R>) => {
|
||||
}
|
||||
|
||||
describe("session.llm native recorded", () => {
|
||||
recordedInstance("uses real RequestExecutor with HTTP recorder for native OpenAI tools", () =>
|
||||
recordedOpenAIInstance("uses real RequestExecutor with HTTP recorder for native OpenAI tools", () =>
|
||||
Effect.gen(function* () {
|
||||
const test = yield* TestInstance
|
||||
const model = yield* Effect.promise(() => loadFixture("openai", "gpt-4.1-mini"))
|
||||
@@ -189,4 +245,57 @@ describe("session.llm native recorded", () => {
|
||||
expect(executed).toMatchObject({ args: { query: "weather" }, toolCallId: expect.any(String) })
|
||||
}),
|
||||
)
|
||||
|
||||
recordedZenInstance("uses console-managed Zen config with native OpenAI-compatible tools", () =>
|
||||
Effect.gen(function* () {
|
||||
const test = yield* TestInstance
|
||||
const model = yield* Effect.promise(() => loadFixture("opencode", "gpt-5.2-codex"))
|
||||
yield* writeConfig(test.directory, model, zenConfig)
|
||||
|
||||
const sessionID = SessionID.make("session-recorded-native-zen-tool")
|
||||
const agent = {
|
||||
name: "test",
|
||||
mode: "primary",
|
||||
prompt: "Call tools exactly as instructed.",
|
||||
options: {},
|
||||
permission: [{ permission: "*", pattern: "*", action: "allow" }],
|
||||
} satisfies Agent.Info
|
||||
const resolved = yield* getModel(ProviderID.opencode, ModelID.make(model.id))
|
||||
let executed: unknown
|
||||
|
||||
const events = yield* nativeRuntime(
|
||||
collect({
|
||||
user: {
|
||||
id: MessageID.make("msg_user-recorded-native-zen-tool"),
|
||||
sessionID,
|
||||
role: "user",
|
||||
time: { created: 0 },
|
||||
agent: agent.name,
|
||||
model: { providerID: ProviderID.opencode, modelID: ModelID.make(model.id) },
|
||||
} satisfies MessageV2.User,
|
||||
sessionID,
|
||||
model: resolved,
|
||||
agent,
|
||||
system: ["You must call the lookup tool exactly once with query weather. Do not answer in text."],
|
||||
messages: [{ role: "user", content: "Use lookup." }],
|
||||
toolChoice: "required",
|
||||
tools: {
|
||||
lookup: tool({
|
||||
description: "Lookup data.",
|
||||
inputSchema: z.object({ query: z.string() }),
|
||||
execute: async (args, options) => {
|
||||
executed = { args, toolCallId: options.toolCallId }
|
||||
return { output: "looked up" }
|
||||
},
|
||||
}),
|
||||
},
|
||||
}),
|
||||
)
|
||||
|
||||
expect(events.filter((event) => event.type === "step-finish")).toHaveLength(1)
|
||||
expect(events.filter((event) => event.type === "finish")).toHaveLength(1)
|
||||
expect(events.some((event) => event.type === "tool-result")).toBe(true)
|
||||
expect(executed).toMatchObject({ args: { query: "weather" }, toolCallId: expect.any(String) })
|
||||
}),
|
||||
)
|
||||
})
|
||||
|
||||
@@ -249,20 +249,30 @@ describe("session.llm-native.request", () => {
|
||||
type: "supported",
|
||||
apiKey: "test-openai-key",
|
||||
})
|
||||
expect(
|
||||
LLMNativeRuntime.status({
|
||||
model: { ...baseModel, providerID: ProviderID.make("opencode") },
|
||||
provider: { ...providerInfo, id: ProviderID.make("opencode") },
|
||||
auth: undefined,
|
||||
}),
|
||||
).toMatchObject({
|
||||
type: "supported",
|
||||
apiKey: "test-openai-key",
|
||||
})
|
||||
expect(
|
||||
LLMNativeRuntime.status({
|
||||
model: { ...baseModel, providerID: ProviderID.make("anthropic") },
|
||||
provider: { ...providerInfo, id: ProviderID.make("anthropic") },
|
||||
auth: undefined,
|
||||
}),
|
||||
).toEqual({ type: "unsupported", reason: "provider is not openai" })
|
||||
).toEqual({ type: "unsupported", reason: "provider is not openai or opencode" })
|
||||
expect(
|
||||
LLMNativeRuntime.status({
|
||||
model: baseModel,
|
||||
provider: providerInfo,
|
||||
auth: { type: "oauth", refresh: "refresh", access: "access", expires: 1 },
|
||||
}),
|
||||
).toEqual({ type: "unsupported", reason: "OpenAI OAuth is not supported" })
|
||||
).toEqual({ type: "unsupported", reason: "OAuth auth is not supported" })
|
||||
})
|
||||
|
||||
test("compiles through the native OpenAI Responses route", async () => {
|
||||
|
||||
Reference in New Issue
Block a user