Open-source AI browser agent for Chrome and Firefox (monorepo) ðŸ§
JavaScript ai-agentai-agentsai-toolsbrowser-aibrowser-automationbrowser-extensionchrome-aichrome-devtoolschrome-extensionchrome-extensionsfirefox-addonfirefox-addonsfirefox-extensionfirefox-extensionsgemmagemma4minimaxoffline-aioffline-llmqwen
fix(opencode): send x-opencode-session for OpenCode Go
Paul Swartz3c80a39309d0
diff --git a/src/chrome/src/agent/agent.js b/src/chrome/src/agent/agent.jsindex 623a240ac..7f8d1843c 100644--- a/src/chrome/src/agent/agent.js+++ b/src/chrome/src/agent/agent.js@@ -6134,10 +6134,16 @@ export class Agent extends LoopDetector {});}+ // WebBrain Compass collects trace metadata; OpenCode Go needs a stable+ // session id so its gateway can route and cache prompts per chat._cloudGenerationOptions(provider, options = {}, { tabId = null, conversationId = null, generationName = 'main' } = {}) {- if (String(provider?.config?.providerName || '').toLowerCase() !== 'webbrain-cloud') return options;const effectiveConversationId = conversationId || (tabId != null ? this.conversationIds.get(tabId) : null);if (!effectiveConversationId) return options;+ const providerName = String(provider?.config?.providerName || '').toLowerCase();+ if (providerName === 'opencode-go') {+ return { ...options, providerSessionId: String(effectiveConversationId) };+ }+ if (providerName !== 'webbrain-cloud') return options;return {...options,webbrainSessionId: String(effectiveConversationId),diff --git a/src/chrome/src/providers/openai.js b/src/chrome/src/providers/openai.jsindex 81df2e29c..ed0e31bb0 100644--- a/src/chrome/src/providers/openai.js+++ b/src/chrome/src/providers/openai.js@@ -149,7 +149,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {return options.signal ? { signal: options.signal } : {};}- _headers() {+ _headers(options = {}) {const headers = { 'Content-Type': 'application/json' };const providerName = (this.config.providerName || '').toLowerCase();if (this.config.requiresApiKey && !String(this.config.apiKey || '').trim()) {@@ -180,9 +180,25 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {|| (String(this.model || '').trim().startsWith('@cf/') ? 'default' : '');if (gatewayId) headers['cf-aig-gateway-id'] = gatewayId;}+ if (providerName === 'opencode-go') {+ headers['x-opencode-session'] = this._opencodeSessionId(options);+ }return headers;}+ // OpenCode Go rejects requests that omit x-opencode-session. Prefer the+ // per-conversation id from the agent so routing and prompt caching stay+ // stable within a chat; fall back to a per-provider id for calls without a+ // conversation (for example Test connection).+ _opencodeSessionId(options = {}) {+ const provided = String(options.providerSessionId || '').trim();+ if (provided) return provided;+ if (!this._opencodeSessionFallback) {+ this._opencodeSessionFallback = `webbrain-${crypto.randomUUID()}`;+ }+ return this._opencodeSessionFallback;+ }+async sendRuntimeEvents(sessionId, events, { timeoutMs = 2500 } = {}) {if (String(this.config.providerName || '').toLowerCase() !== 'webbrain-cloud') {return { ok: false, retryable: false, status: 0 };@@ -789,7 +805,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {try {res = await fetchWithFallback(url, {method: 'POST',- headers: this._headers(),+ headers: this._headers(options),body: JSON.stringify(this._responsesBody(messages, options, false)),...this._chatAbortOptions(options),});@@ -816,7 +832,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {try {res = await fetchWithFallback(url, {method: 'POST',- headers: this._headers(),+ headers: this._headers(options),body: JSON.stringify(this._responsesBody(messages, options, true)),signal: options.signal,});@@ -968,7 +984,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {try {res = await fetchWithFallback(url, {method: 'POST',- headers: this._headers(),+ headers: this._headers(options),body: JSON.stringify(body),...this._chatAbortOptions(options),});@@ -1011,7 +1027,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {try {res = await fetchWithFallback(streamUrl, {method: 'POST',- headers: this._headers(),+ headers: this._headers(options),body: JSON.stringify(body),signal: options.signal,});diff --git a/src/firefox/src/agent/agent.js b/src/firefox/src/agent/agent.jsindex d7c42a402..73c1e0e97 100644--- a/src/firefox/src/agent/agent.js+++ b/src/firefox/src/agent/agent.js@@ -6156,10 +6156,16 @@ export class Agent extends LoopDetector {});}+ // WebBrain Compass collects trace metadata; OpenCode Go needs a stable+ // session id so its gateway can route and cache prompts per chat._cloudGenerationOptions(provider, options = {}, { tabId = null, conversationId = null, generationName = 'main' } = {}) {- if (String(provider?.config?.providerName || '').toLowerCase() !== 'webbrain-cloud') return options;const effectiveConversationId = conversationId || (tabId != null ? this.conversationIds.get(tabId) : null);if (!effectiveConversationId) return options;+ const providerName = String(provider?.config?.providerName || '').toLowerCase();+ if (providerName === 'opencode-go') {+ return { ...options, providerSessionId: String(effectiveConversationId) };+ }+ if (providerName !== 'webbrain-cloud') return options;return {...options,webbrainSessionId: String(effectiveConversationId),diff --git a/src/firefox/src/providers/openai.js b/src/firefox/src/providers/openai.jsindex b1c69429a..0b0cdbea6 100644--- a/src/firefox/src/providers/openai.js+++ b/src/firefox/src/providers/openai.js@@ -149,7 +149,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {return options.signal ? { signal: options.signal } : {};}- _headers() {+ _headers(options = {}) {const headers = { 'Content-Type': 'application/json' };const providerName = (this.config.providerName || '').toLowerCase();if (this.config.requiresApiKey && !String(this.config.apiKey || '').trim()) {@@ -180,9 +180,25 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {|| (String(this.model || '').trim().startsWith('@cf/') ? 'default' : '');if (gatewayId) headers['cf-aig-gateway-id'] = gatewayId;}+ if (providerName === 'opencode-go') {+ headers['x-opencode-session'] = this._opencodeSessionId(options);+ }return headers;}+ // OpenCode Go rejects requests that omit x-opencode-session. Prefer the+ // per-conversation id from the agent so routing and prompt caching stay+ // stable within a chat; fall back to a per-provider id for calls without a+ // conversation (for example Test connection).+ _opencodeSessionId(options = {}) {+ const provided = String(options.providerSessionId || '').trim();+ if (provided) return provided;+ if (!this._opencodeSessionFallback) {+ this._opencodeSessionFallback = `webbrain-${crypto.randomUUID()}`;+ }+ return this._opencodeSessionFallback;+ }+async sendRuntimeEvents(sessionId, events, { timeoutMs = 2500 } = {}) {if (String(this.config.providerName || '').toLowerCase() !== 'webbrain-cloud') {return { ok: false, retryable: false, status: 0 };@@ -789,7 +805,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {try {res = await fetchWithTimeout(url, {method: 'POST',- headers: this._headers(),+ headers: this._headers(options),body: JSON.stringify(this._responsesBody(messages, options, false)),...this._chatAbortOptions(options),});@@ -816,7 +832,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {try {res = await fetchWithTimeout(url, {method: 'POST',- headers: this._headers(),+ headers: this._headers(options),body: JSON.stringify(this._responsesBody(messages, options, true)),signal: options.signal,});@@ -968,7 +984,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {try {res = await fetchWithTimeout(url, {method: 'POST',- headers: this._headers(),+ headers: this._headers(options),body: JSON.stringify(body),...this._chatAbortOptions(options),});@@ -1011,7 +1027,7 @@ export class OpenAICompatibleProvider extends BaseLLMProvider {try {res = await fetchWithTimeout(streamUrl, {method: 'POST',- headers: this._headers(),+ headers: this._headers(options),body: JSON.stringify(body),signal: options.signal,});diff --git a/test/run.js b/test/run.jsindex 8f6d91020..3878de884 100644--- a/test/run.js+++ b/test/run.js@@ -69042,6 +69042,44 @@ test('OpenCode model prefixes and automatic Responses routing are scoped to the}});+test('OpenCode Go requests carry a stable x-opencode-session header', () => {+ for (const [label, Provider] of [+ ['chrome', OpenAIProviderCh],+ ['firefox', OpenAIProviderFx],+ ]) {+ const go = new Provider({+ providerName: 'opencode-go',+ baseUrl: 'https://opencode.ai/zen/go/v1',+ model: 'deepseek-v4-flash',+ });+ assert.equal(+ go._headers({ providerSessionId: 'conv_tab_1_123' })['x-opencode-session'],+ 'conv_tab_1_123',+ `${label}: the conversation session id should be forwarded`,+ );++ // Calls without a conversation (for example Test connection) still need a+ // session id, and it must stay stable for the provider instance.+ const fallbackA = go._headers()['x-opencode-session'];+ const fallbackB = go._headers()['x-opencode-session'];+ assert.match(fallbackA, /^webbrain-/, `${label}: a fallback session id should be minted`);+ assert.equal(fallbackA, fallbackB, `${label}: the fallback session id must be stable`);++ for (const config of [+ { providerName: 'opencode', baseUrl: 'https://opencode.ai/zen/v1', model: 'muse-spark-1.2-contributor-free' },+ { providerName: 'custom', baseUrl: 'https://opencode.ai/zen/go/v1', model: 'deepseek-v4-flash' },+ { providerName: 'openai', baseUrl: 'https://api.openai.com/v1', model: 'gpt-4o' },+ ]) {+ const other = new Provider(config);+ assert.equal(+ other._headers({ providerSessionId: 'conv_x' })['x-opencode-session'],+ undefined,+ `${label}: ${config.providerName} at ${config.baseUrl} must not receive the Go session header`,+ );+ }+ }+});+test('local OpenAI-compatible servers that require a model throw a clear error when unset', () => {for (const Provider of [OpenAIProviderCh, OpenAIProviderFx]) {for (const providerName of ['ollama', 'jan', 'vllm', 'sglang', 'localai', 'gpt4all', 'local_openai_proxy']) {@@ -70947,6 +70985,9 @@ test('WebBrain Compass groups every generation in a stable conversation sessionassert.deepEqual(byoOptions, { temperature: 0 }, `${label}: BYO provider received Cloud collection fields`);const localOptions = agent._cloudGenerationOptions({ config: { providerName: 'llama.cpp' } }, {}, { tabId, generationName: 'main' });assert.deepEqual(localOptions, {}, `${label}: local provider received Cloud collection fields`);+ const goOptions = agent._cloudGenerationOptions({ config: { providerName: 'opencode-go' } }, { temperature: 0 }, { tabId, generationName: 'main' });+ assert.equal(goOptions.providerSessionId, firstConversationId, `${label}: OpenCode Go should receive the conversation session id`);+ assert.equal(goOptions.webbrainSessionId, undefined, `${label}: OpenCode Go should not receive Cloud collection fields`);agent.clearConversation(tabId);agent.getConversation(tabId, 'ask');