Skip to content
Merged
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
35 changes: 35 additions & 0 deletions packages/teamcode/src/session/llm-headers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
export namespace LLMHeaders {
export type Input = {
providerID: string
sessionID: string
requestID: string
client: string
version: string
projectID?: string
parentSessionID?: string
}

const ZEN_PROVIDER_PREFIX = "opencode"

export function isZenProvider(providerID: string) {
return providerID.startsWith(ZEN_PROVIDER_PREFIX)
}

export function build(input: Input): Record<string, string> {
const headers: Record<string, string> = isZenProvider(input.providerID)
? {
"x-opencode-session": input.sessionID,
"x-opencode-request": input.requestID,
"x-opencode-client": input.client,
"User-Agent": `opencode/${input.version}`,
}
: {
"x-session-affinity": input.sessionID,
"X-Session-Id": input.sessionID,
"User-Agent": `teamcode/${input.version}`,
}
if (isZenProvider(input.providerID) && input.projectID) headers["x-opencode-project"] = input.projectID
if (input.parentSessionID) headers["x-parent-session-id"] = input.parentSessionID
return headers
}
}
40 changes: 22 additions & 18 deletions packages/teamcode/src/session/llm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { Wildcard } from "@/util/wildcard"
import { SessionID } from "@/session/schema"
import { Auth } from "@/auth"
import { InstallationVersion } from "@teamcode-ai/core/installation/version"
import { LLMHeaders } from "./llm-headers"
import { EffectBridge } from "@/effect/bridge"
import { RuntimeFlags } from "@/effect/runtime-flags"
const log = Log.create({ service: "llm" })
Expand All @@ -28,6 +29,13 @@ type Result = Awaited<ReturnType<typeof streamText>>
const mergeOptions = (target: Record<string, any>, source: Record<string, any> | undefined): Record<string, any> =>
mergeDeep(target, source ?? {}) as Record<string, any>

const approvalTitle = (parsed: unknown): string => {
if (typeof parsed !== "object" || parsed === null) return ""
const record: Record<string, unknown> = Object.fromEntries(Object.entries(parsed))
const candidate = record.title ?? record.name ?? ""
return typeof candidate === "string" ? candidate : ""
}

export type StreamInput = {
user: MessageV2.User
sessionID: string
Expand Down Expand Up @@ -83,7 +91,7 @@ const live: Layer.Layer<
providerID: input.model.providerID,
})

const [language, cfg, item, info] = yield* Effect.all(
const [language, , item, info] = yield* Effect.all(
[
provider.getLanguage(input.model),
config.get(),
Expand Down Expand Up @@ -225,7 +233,7 @@ const live: Layer.Layer<
return { result: "", error: `Unknown tool: ${toolName}` }
}
try {
const result = await t.execute!(JSON.parse(argsJson), {
const result = await t.execute(JSON.parse(argsJson), {
toolCallId: _requestID,
messages: input.messages,
abortSignal: input.abort,
Expand Down Expand Up @@ -265,8 +273,8 @@ const live: Layer.Layer<
})
const toolPatterns = approvalTools.map((t: { name: string; args: string }) => {
try {
const parsed = JSON.parse(t.args) as Record<string, unknown>
const title = (parsed?.title ?? parsed?.name ?? "") as string
const parsed: unknown = JSON.parse(t.args)
const title = approvalTitle(parsed)
return title ? `${t.name}: ${title}` : t.name
} catch {
return t.name
Expand Down Expand Up @@ -295,7 +303,7 @@ const live: Layer.Layer<
})
}

const opencodeProjectID = input.model.providerID.startsWith("teamcode")
const opencodeProjectID = LLMHeaders.isZenProvider(input.model.providerID)
? (yield* InstanceState.context).project.id
: undefined

Expand Down Expand Up @@ -373,19 +381,15 @@ const live: Layer.Layer<
maxOutputTokens: params.maxOutputTokens,
abortSignal: input.abort,
headers: {
...(input.model.providerID.startsWith("teamcode")
? {
"x-teamcode-project": opencodeProjectID,
"x-teamcode-session": input.sessionID,
"x-teamcode-request": input.user.id,
"x-teamcode-client": flags.client,
"User-Agent": `teamcode/${InstallationVersion}`,
}
: {
"x-session-affinity": input.sessionID,
...(input.parentSessionID ? { "x-parent-session-id": input.parentSessionID } : {}),
"User-Agent": `teamcode/${InstallationVersion}`,
}),
...LLMHeaders.build({
providerID: input.model.providerID,
sessionID: input.sessionID,
parentSessionID: input.parentSessionID,
requestID: input.user.id,
client: flags.client,
version: InstallationVersion,
projectID: opencodeProjectID,
}),
...input.model.headers,
...headers,
},
Expand Down
51 changes: 51 additions & 0 deletions packages/teamcode/test/session/llm-headers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { describe, expect, test } from "bun:test"
import { LLMHeaders } from "../../src/session/llm-headers"

const base = {
sessionID: "ses_123",
requestID: "msg_456",
client: "cli",
version: "9.9.9",
}

describe("session.llm-headers", () => {
test("opencode zen providers send x-opencode-* headers and opencode user agent", () => {
const headers = LLMHeaders.build({ ...base, providerID: "opencode", projectID: "prj_1" })
expect(headers).toEqual({
"x-opencode-project": "prj_1",
"x-opencode-session": "ses_123",
"x-opencode-request": "msg_456",
"x-opencode-client": "cli",
"User-Agent": "opencode/9.9.9",
})
})

test("opencode-go is treated as an opencode provider", () => {
const headers = LLMHeaders.build({ ...base, providerID: "opencode-go", projectID: "prj_1" })
expect(headers["x-opencode-session"]).toBe("ses_123")
expect(headers["User-Agent"]).toBe("opencode/9.9.9")
})

test("opencode providers omit x-opencode-project when project id is missing", () => {
const headers = LLMHeaders.build({ ...base, providerID: "opencode" })
expect(headers).not.toHaveProperty("x-opencode-project")
expect(headers["x-opencode-session"]).toBe("ses_123")
})

test("other providers send session affinity and teamcode user agent", () => {
const headers = LLMHeaders.build({ ...base, providerID: "anthropic" })
expect(headers).toEqual({
"x-session-affinity": "ses_123",
"X-Session-Id": "ses_123",
"User-Agent": "teamcode/9.9.9",
})
expect(headers).not.toHaveProperty("x-opencode-session")
})

test("parent session id is forwarded for every provider", () => {
const zen = LLMHeaders.build({ ...base, providerID: "opencode", parentSessionID: "ses_parent" })
const other = LLMHeaders.build({ ...base, providerID: "anthropic", parentSessionID: "ses_parent" })
expect(zen["x-parent-session-id"]).toBe("ses_parent")
expect(other["x-parent-session-id"]).toBe("ses_parent")
})
})
Loading