Skip to content

A desktop coding agent with free access to frontier models.

TypeScript

feat: web search and chat headers on the new engine

ogsamratc1ec5eb9a465
diff --git a/apps/server/src/engine-v2-preview.ts b/apps/server/src/engine-v2-preview.ts
index d2608e44..d94df6fa 100644
--- a/apps/server/src/engine-v2-preview.ts
+++ b/apps/server/src/engine-v2-preview.ts
@@ -20,6 +20,7 @@ import {
import type { EnvService } from "./env-file.js";
import { selectPrimaryCredentialEnvName } from "./managed-provider-auth.js";
import { isReleaseBuild } from "./release-guard.js";
+import { webSearchSetting } from "./web-search.js";
import { engineDataDir } from "./engine-data-home.js";
import { readMigratedSessions, seedV2StoreFromV1 } from "./engine-v2-migration.js";
import type { ServerConfig } from "./types.js";
@@ -469,6 +470,7 @@ export function createEngineV2Preview(options: { config: ServerConfig; env?: Pic
rootDir,
env: { ...opencodeModelsEnv, OMNIRUSH_SERVER_URL: `http://127.0.0.1:${config.port}`, OMNIRUSH_POLICY_TOKEN: managedDesktopPolicy(config).evaluationToken },
permissions: async () => executionRules((await readGlobalRuntimeOpencodeConfig(config)).managedPolicy?.execution),
+ webSearch: () => webSearchSetting({ env: process.env, stateDir: runtimeStorageDir(config) }).enabled,
});
sidecar = managed;
if (!enabled || !allowRunning) {
diff --git a/apps/server/src/managed-opencode-v2.env.test.ts b/apps/server/src/managed-opencode-v2.env.test.ts
index acdcf404..c2c7ebdf 100644
--- a/apps/server/src/managed-opencode-v2.env.test.ts
+++ b/apps/server/src/managed-opencode-v2.env.test.ts
@@ -40,3 +40,21 @@ test("the engine's gates are applied last and a spawn without them is refused",
expect(() => assertEngineGates({ PATH: "/bin" })).toThrow(/gates are missing/);
expect(() => assertEngineGates({ OMNIRUSH_GATE_PROVIDERS: "omnirush", OMNIRUSH_NO_SYSTEM_CONFIG: "1" })).toThrow(/gates are missing/);
});
+
+posix("the stock websearch tool searches through Exa only while the server's switch is on", async () => {
+ const dir = await mkdtemp(join(tmpdir(), "v2-ws-"));
+ try {
+ const bin = join(dir, "fake-v2");
+ await writeFile(bin, "#!/bin/sh\nexit 3\n");
+ await chmod(bin, 0o755);
+ for (const on of [true, false]) {
+ const rootDir = join(dir, String(on));
+ await createManagedOpencodeV2Server({ bin, rootDir, bootTimeoutMs: 5_000, webSearch: () => on }).then((server) => server.close(), () => undefined);
+ const config = JSON.parse(await readFile(join(rootDir, "config", "opencode.json"), "utf8"));
+ expect(config.websearch).toEqual(on ? { provider: "exa" } : undefined);
+ expect(config).toMatchObject({ share: "disabled", update: "disable" });
+ }
+ } finally {
+ await rm(dir, { recursive: true, force: true });
+ }
+});
diff --git a/apps/server/src/managed-opencode-v2.ts b/apps/server/src/managed-opencode-v2.ts
index d9b8496a..ef9a3bda 100644
--- a/apps/server/src/managed-opencode-v2.ts
+++ b/apps/server/src/managed-opencode-v2.ts
@@ -41,6 +41,8 @@ export interface ManagedOpencodeV2ServerOptions {
env?: Record<string, string>;
bootTimeoutMs?: number;
permissions?: () => Promise<EnginePermissionRule[]>;
+ /** Whether the server's web-search switch is on (web-search.ts): the stock websearch tool then searches. */
+ webSearch?: () => boolean;
}
export interface OpencodeV2Health {
@@ -282,6 +284,10 @@ export async function createManagedOpencodeV2Server(
update: "disable",
providers: providerConfig,
...(options.permissions ? { permissions: await options.permissions() } : {}),
+ // On: the stock websearch tool searches through Exa (the engine's keyless default), like the CLI.
+ // Off: no provider is picked (never `false`, which would take the tool out of the stock list).
+ // shortcut: Exa until the gateway serves a search integration of its own.
+ ...(options.webSearch?.() ? { websearch: { provider: "exa" } } : {}),
plugins: await pluginDirectories(join(options.rootDir, "plugins"), [sessionNextPluginPath(), ...(options.env?.OMNIRUSH_SERVER_URL ? [managedPolicyPluginPath(true)] : [])]),
}, null, 2)}\n`, { mode: 0o600 });
}
diff --git a/apps/server/src/model-modes/model-modes.test.ts b/apps/server/src/model-modes/model-modes.test.ts
index f3b2ac6d..0f15aba2 100644
--- a/apps/server/src/model-modes/model-modes.test.ts
+++ b/apps/server/src/model-modes/model-modes.test.ts
@@ -103,6 +103,17 @@ describe("model modes", () => {
expect(((await response.json()) as { error: { message: string } }).error.message).toBe("This model isn't available in sandbox projects.");
});
+ test("broker: the 2.x engine's stock x-session-id names the chat when nothing else does", async () => {
+ const calls: Call[] = [];
+ const b = broker(calls, undefined, false);
+ const tools = [{ type: "function", name: "shell" }];
+ expect((await b.handle(request({ model: "kimi-k3.1-pro", tools }, { "x-session-id": "ses_stock" }), "responses")).status).toBe(200);
+ const refused = await b.handle(request({ model: "gpt-6-astra", tools }, { "x-session-id": "ses_stock" }), "responses");
+ expect(refused.status).toBe(400);
+ // An explicit chat header wins over the stock one.
+ expect((await b.handle(request({ model: "gpt-6-astra", tools }, { "x-session-id": "ses_stock", "x-omnirush-session-id": "ses_other" }), "responses")).status).toBe(200);
+ });
+
test("broker: all context-bearing requests and children inherit the chat model", async () => {
const calls: Call[] = [];
const b = broker(calls, undefined, false);
diff --git a/apps/server/src/omnirush-gateway-broker.ts b/apps/server/src/omnirush-gateway-broker.ts
index 13e411b3..dea5fe9b 100644
--- a/apps/server/src/omnirush-gateway-broker.ts
+++ b/apps/server/src/omnirush-gateway-broker.ts
@@ -1453,6 +1453,13 @@ export class OmniRushGatewayBroker {
return Response.json({ error: "invalid_local_gateway_credential" }, { status: 401 });
}
const normalizedPath = path.replace(/^\/+/, "");
+ // The 2.x engine names its session in x-session-id on every request (the CLI's token proxy maps it the same way).
+ const stockSession = request.headers.get("x-session-id");
+ if (!request.headers.has("x-omnirush-session-id") && stockSession && /^ses_[A-Za-z0-9]{1,64}$/.test(stockSession)) {
+ const headers = new Headers(request.headers);
+ headers.set("x-omnirush-session-id", stockSession);
+ request = new Request(request, { headers });
+ }
const allowed = (request.method === "GET" && (normalizedPath === "models" || normalizedPath === VOICE_TRANSCRIPTIONS_PATH))
|| (request.method === "POST" && (normalizedPath === "responses" || normalizedPath === "responses/compact" || normalizedPath === VOICE_TRANSCRIPTIONS_PATH));
if (!allowed) return Response.json({ error: "unsupported_gateway_path" }, { status: 404 });