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
4 changes: 4 additions & 0 deletions packages/opencode/src/agent/agent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import { LocationServiceMap, locationServiceMapLayer } from "@opencode-ai/core/l
import { Reference } from "@opencode-ai/core/reference"
import { Location } from "@opencode-ai/core/location"
import { PluginV2 } from "@opencode-ai/core/plugin"
import { SessionID } from "@/session/schema"

export const Info = Schema.Struct({
name: Schema.String,
Expand Down Expand Up @@ -386,6 +387,9 @@ const layer = Layer.effect(
const isOpenaiOauth = model.providerID === "openai" && authInfo?.type === "oauth"

const params = {
headers: model.providerID.startsWith("opencode")
? { "x-opencode-session": SessionID.descending() }
: undefined,
experimental_telemetry: {
isEnabled: cfg.experimental?.openTelemetry,
tracer,
Expand Down
72 changes: 71 additions & 1 deletion packages/opencode/test/agent/agent.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { afterEach, expect } from "bun:test"
import { LayerNode } from "@opencode-ai/core/effect/layer-node"
import { Cause, Effect, Exit, Layer } from "effect"
import path from "path"
import { disposeAllInstances, TestInstance } from "../fixture/fixture"
import { disposeAllInstances, withTmpdirInstance, TestInstance } from "../fixture/fixture"
import { testEffect } from "../lib/effect"
import { Agent } from "../../src/agent/agent"
import { Auth } from "../../src/auth"
Expand All @@ -15,6 +15,8 @@ import { Plugin } from "../../src/plugin"
import { Provider } from "../../src/provider/provider"
import { Skill } from "../../src/skill"
import { Truncate } from "../../src/tool/truncate"
import { ProviderV2 } from "@opencode-ai/core/provider"
import { ModelV2 } from "@opencode-ai/core/model"

const agentLayer = (flags: Partial<RuntimeFlags.Info> = {}) =>
LayerNode.compile(
Expand Down Expand Up @@ -44,6 +46,74 @@ afterEach(async () => {
await disposeAllInstances()
})

it.live(
"agent generation identifies its OpenCode Go request",
() =>
Effect.gen(function* () {
const headers: string[] = []
const server = yield* Effect.acquireRelease(
Effect.sync(() =>
Bun.serve({
port: 0,
fetch(request) {
const session = request.headers.get("x-opencode-session")
headers.push(session ?? "")
if (!session) return new Response("missing x-opencode-session", { status: 400 })
return Response.json({
id: "chatcmpl-agent-test",
object: "chat.completion",
created: 1,
model: "test-model",
choices: [
{
index: 0,
message: {
role: "assistant",
content: JSON.stringify({
identifier: "test-agent",
whenToUse: "Use for test work",
systemPrompt: "You are a test agent",
}),
},
finish_reason: "stop",
},
],
usage: { prompt_tokens: 1, completion_tokens: 1, total_tokens: 2 },
})
},
}),
),
(server) => Effect.sync(() => server.stop()),
)

const result = yield* Agent.Service.use((svc) =>
svc.generate({
description: "Create a test agent",
model: { providerID: ProviderV2.ID.make("opencode-go"), modelID: ModelV2.ID.make("test-model") },
}),
).pipe(
withTmpdirInstance({
config: {
provider: {
"opencode-go": {
name: "Test OpenCode Go",
npm: "@ai-sdk/openai-compatible",
api: `http://127.0.0.1:${server.port}/v1`,
models: { "test-model": { name: "Test Model" } },
options: { apiKey: "test-key" },
},
},
},
}),
)

expect(result.identifier).toBe("test-agent")
expect(headers).toHaveLength(1)
expect(headers[0]).toMatch(/^ses_/)
}),
30000,
)

it.instance("returns default native agents when no config", () =>
Effect.gen(function* () {
const agents = yield* load((svc) => svc.list())
Expand Down
Loading