A local-first multi-agent app development platform, built on PenguinHarness
feat(core): add x-opencode-session header support (#612)
paskaa9e73026f48f6
diff --git a/changelog/unreleased/2026-09-14-opencode-session-header.md b/changelog/unreleased/2026-09-14-opencode-session-header.mdnew file mode 100644index 00000000..e8b85bee--- /dev/null+++ b/changelog/unreleased/2026-09-14-opencode-session-header.md@@ -0,0 +1,28 @@+# OpenCode requests carry the Session's id++- **Date:** 2026-09-14+- **Type:** feature+- **Scope:** `core`+- **PR:** [#612](https://github.com/Prism-Shadow/penguin-harness/pull/612)++[中文版](2026-09-14-opencode-session-header.zh.md)++A request sent to OpenCode's gateway now names the conversation it belongs to. The gateway keys its+backend routing on an `x-opencode-session` header and wants one value that holds still across a+conversation's requests; `attributionHeaders` supplies the Session's own id there, alongside the app+attribution it already sends OpenRouter and TokenDance.++## Details++- **The id comes from the Session, through `GenerativeModelConfig.sessionId`.** It is threaded at+ all three `GenerativeModel` construction sites — the context's own LLM object, the bare LLM behind+ meta requests such as title generation, and the vision describer — so everything one Session sends+ carries the same value, and a context rotation does not change it.+- **The endpoint host decides, as it does for the other two schemes.** Matching is suffix-anchored+ on `opencode.ai`, so a subdomain of the gateway counts and a lookalike domain does not. No+ built-in catalog row points at OpenCode; the header reaches the wire through a `custom` model+ entry carrying that `base_url`.+- **No Session id, no header.** Nothing stands in for a missing one — a constant would file every+ conversation at the gateway under a single session.+- OpenRouter's three headers and TokenDance's `X-App-URL` are unchanged, with or without a Session+ id in hand.diff --git a/changelog/unreleased/2026-09-14-opencode-session-header.zh.md b/changelog/unreleased/2026-09-14-opencode-session-header.zh.mdnew file mode 100644index 00000000..c2308214--- /dev/null+++ b/changelog/unreleased/2026-09-14-opencode-session-header.zh.md@@ -0,0 +1,22 @@+# 发往 OpenCode 的请求带上 Session id++- **Date:** 2026-09-14+- **Type:** feature+- **Scope:** `core`+- **PR:** [#612](https://github.com/Prism-Shadow/penguin-harness/pull/612)++[English](2026-09-14-opencode-session-header.md)++发往 OpenCode 网关的请求现在会说明自己属于哪一场对话。该网关以 `x-opencode-session` 请求头为键做后端路由,+要求同一场对话的各次请求给出同一个值;`attributionHeaders` 为此送出本 Session 自己的 id,与它已经发给+OpenRouter 和 TokenDance 的应用归属信息并列。++## 细节++- **id 来自 Session,经 `GenerativeModelConfig.sessionId` 传入。** 三处 `GenerativeModel` 构造点都已串起+ ——模型上下文自己的 LLM 对象、承载生成标题等 meta 请求的裸 LLM,以及视觉描述器——因此同一个 Session 发出的+ 一切请求都带同一个值,上下文轮换也不会改变它。+- **由端点主机决定,与另外两套方案一致。** 匹配以后缀锚定在 `opencode.ai` 上,网关的子域算数,形似的域名不算。+ 内置模型目录中没有指向 OpenCode 的条目;请求头是经由填了该 `base_url` 的 `custom` 模型条目送上线的。+- **没有 Session id 就不发这个请求头。** 缺失时不以任何值顶替——固定常量会让所有对话在网关那里归入同一个 session。+- OpenRouter 的三个请求头与 TokenDance 的 `X-App-URL` 不受影响,无论手上有没有 Session id。diff --git a/packages/core/src/agent.ts b/packages/core/src/agent.tsindex 76e474dc..117b2c1b 100644--- a/packages/core/src/agent.ts+++ b/packages/core/src/agent.ts@@ -1132,6 +1132,7 @@ export class Agent {...(visionEntry.context_window !== undefined? { contextWindow: visionEntry.context_window }: {}),+ sessionId,requestTimeoutMs: 60_000,}),};@@ -1208,6 +1209,7 @@ export class Agent {// model that rejects it....(modelEntry.fast_mode === true ? { fastMode: true } : {}),...(context.thinkingLevel !== undefined ? { thinkingLevel: context.thinkingLevel } : {}),+ sessionId,...(context.requestTimeoutMs !== undefined? { requestTimeoutMs: context.requestTimeoutMs }: {}),@@ -1277,6 +1279,7 @@ export class Agent {...(modelEntry.context_window !== undefined? { contextWindow: modelEntry.context_window }: {}),+ sessionId,requestTimeoutMs: 30_000,});diff --git a/packages/core/src/interfaces/llm.ts b/packages/core/src/interfaces/llm.tsindex 2e82a9c3..2502437d 100644--- a/packages/core/src/interfaces/llm.ts+++ b/packages/core/src/interfaces/llm.ts@@ -65,6 +65,13 @@ export interface GenerativeModelConfig {* inside the first-event gap.*/requestTimeoutMs?: number;+ /**+ * The Session's id, for endpoints whose attribution scheme identifies the conversation+ * rather than the app (OpenCode's `x-opencode-session`; see state/model-catalog.ts+ * `attributionHeaders`). Unset: no such header is sent — a placeholder id would file every+ * conversation at the gateway under one session, which is worse than naming none.+ */+ sessionId?: string;/*** tool_call_id uniqueness registry (Session-level). Pass the same instance when rebuilding a new* GenerativeModel on compaction so the uniqueness scope covers the whole Session; defaults to a freshdiff --git a/packages/core/src/llm/generative-model.ts b/packages/core/src/llm/generative-model.tsindex de1df1a8..41a0fc25 100644--- a/packages/core/src/llm/generative-model.ts+++ b/packages/core/src/llm/generative-model.ts@@ -981,8 +981,9 @@ export class GenerativeModel implements LLMInterface {// explicitly for custom-named models. `defaultHeaders` carries the app attribution the// configured endpoint reads (see attributionHeaders); AgentHub hands it to every request// the routed client makes, and endpoints with no attribution scheme get no extra headers- // at all.- const headers = attributionHeaders(config.baseUrl);+ // at all. The Session's id rides along for the schemes that name the conversation rather+ // than the app.+ const headers = attributionHeaders(config.baseUrl, config.sessionId);this.client = new AutoLLMClient({model: config.modelId,...(config.apiKey !== undefined ? { apiKey: config.apiKey } : {}),diff --git a/packages/core/src/state/model-catalog.ts b/packages/core/src/state/model-catalog.tsindex ba701cb1..c8ed9db7 100644--- a/packages/core/src/state/model-catalog.ts+++ b/packages/core/src/state/model-catalog.ts@@ -2400,9 +2400,15 @@ function hostMatches(host: string, domain: string): boolean {* - TokenDance (https://tokendance.space/docs/app-attribution): `X-App-URL` alone, and it* takes priority over any App URL recorded on the API key — the same key may be in use by* other tools, so the per-request value is the accurate one.+ * - OpenCode (https://opencode.ai): `x-opencode-session` alone, and it names the conversation+ * rather than the app — the gateway keys its backend routing on it, so the value has to hold+ * still across a Session's requests and differ between Sessions. It is therefore sent only+ * when a Session id is at hand: a stand-in constant would file every conversation under one+ * session, which serves the gateway worse than naming none.*/export function attributionHeaders(baseUrl: string | undefined,+ sessionId?: string,): Record<string, string> | undefined {const host = endpointHost(baseUrl);if (!host) return undefined;@@ -2414,5 +2420,6 @@ export function attributionHeaders(};}if (hostMatches(host, "tokendance.space")) return { "X-App-URL": APP_URL };+ if (hostMatches(host, "opencode.ai") && sessionId) return { "x-opencode-session": sessionId };return undefined;}diff --git a/packages/core/test/model-catalog.test.ts b/packages/core/test/model-catalog.test.tsindex 0576bf77..6689189a 100644--- a/packages/core/test/model-catalog.test.ts+++ b/packages/core/test/model-catalog.test.ts@@ -1373,6 +1373,37 @@ describe("attributionHeaders (how the harness names itself to the gateways that});});+ it("OpenCode gets the session header, and only when there is a session id to put in it", () => {+ const sessionId = "session-2026-09-14-10-30-00-a1b2c3d4";+ expect(attributionHeaders("https://opencode.ai/zen/v1", sessionId)).toEqual({+ "x-opencode-session": sessionId,+ });+ // Same suffix-anchored host rule as the other two: a subdomain is the same gateway.+ expect(attributionHeaders("https://api.opencode.ai/v1", sessionId)).toEqual({+ "x-opencode-session": sessionId,+ });+ // No id, no header. A stand-in constant would file every conversation at the gateway+ // under one session, which serves it worse than naming none.+ expect(attributionHeaders("https://opencode.ai/zen/v1")).toBeUndefined();+ expect(attributionHeaders("https://opencode.ai/zen/v1", "")).toBeUndefined();+ // A host outside the scheme gets nothing, whatever path it serves: third-party mirrors of+ // a gateway are not part of the built-in attribution, and naming one here would put it in+ // the repository just as surely as listing it would.+ expect(attributionHeaders("https://gateway.example.com/zen/go", sessionId)).toBeUndefined();+ });++ it("a session id leaves the app-attribution gateways exactly as they were", () => {+ const sessionId = "session-2026-09-14-10-30-00-a1b2c3d4";+ expect(attributionHeaders("https://openrouter.ai/api/v1", sessionId)).toEqual({+ "HTTP-Referer": "https://penguin.ooo/",+ "X-OpenRouter-Title": "PenguinHarness",+ "X-OpenRouter-Categories": "cli-agent,personal-agent",+ });+ expect(attributionHeaders("https://tokendance.space/gateway/v1", sessionId)).toEqual({+ "X-App-URL": "https://penguin.ooo/",+ });+ });+it("every other endpoint gets no extra headers, and a blank or unparseable base URL is inert", () => {expect(attributionHeaders("https://api.deepseek.com")).toBeUndefined();expect(attributionHeaders("https://api.siliconflow.cn/v1")).toBeUndefined();