chat: root the OpenCode server at the chat cwd (email chat now runs in the account dir)

OpenCode has no per-session `directory` — a session inherits the server's cwd (POST
/session ignores extra fields). So the previous "pass directory on create" was a no-op
and every OpenCode chat ran in the fixed server's dir (~), including the email chat.

Fix: hybrid server model. server-manager.ensureServer(cwd?, home?) returns the fixed
pm2 server (OPENCODE_SERVER_URL, :4096) for the general /chat, but for a context-scoped
cwd (email account dir, project dir) it spawns/pools an `opencode serve` rooted at that
directory — so the session's agent actually operates there. send-opencode passes the
resolved cwd + the user's home; createSession drops the ignored directory param.

Verified live: a cwd-scoped server reports the session directory as the target dir
(not ~), while /chat still uses :4096.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-24 20:20:15 +00:00
co-authored by Claude Opus 4.8
parent cbe83f39a9
commit d60c73b3a3
3 changed files with 124 additions and 18 deletions
+7 -3
View File
@@ -4,6 +4,7 @@ import { ensureServer } from '@@/api/chat/opencode/server-manager';
import { getConnection } from '@@/api/chat/opencode/client';
import { createEventMapper } from '@@/api/chat/opencode/event-mapper';
import { getOpenCodeSession, setOpenCodeSession } from '@@/api/chat/opencode/state';
import { getHomeDirForRole } from '../data-path';
// The OpenCode analog of send-claude-code.ts's streaming path. Drives a turn against a warm
// `opencode serve` over HTTP + SSE, mapping events to the shared ChatEvent contract.
@@ -33,9 +34,11 @@ function splitModel(model: string): { providerID: string; modelID: string } {
}
export async function sendOpenCodeStreaming(params: OpenCodeStreamingParams): Promise<OpenCodeStreamingHandle> {
logger.info('OpenCode streaming exec', { sessionKey: params.sessionKey, model: params.model });
logger.info('OpenCode streaming exec', { sessionKey: params.sessionKey, model: params.model, cwd: params.cwd });
const { baseUrl } = await ensureServer();
// params.cwd set (email/project) → a server rooted there; unset (/chat) → the fixed pm2 server.
const home = getHomeDirForRole(params.email, params.role ?? '');
const { baseUrl } = await ensureServer(params.cwd, home);
const conn = getConnection(baseUrl);
// Resolve the OpenCode session: a known mapping, or — when resuming from history — the sessionKey is
@@ -45,7 +48,8 @@ export async function sendOpenCodeStreaming(params: OpenCodeStreamingParams): Pr
(params.sessionKey.startsWith('ses_') ? params.sessionKey : undefined) ??
params.resumeSessionId;
if (!opencodeSessionId) {
opencodeSessionId = await conn.createSession(params.cwd);
// The working dir comes from the server (rooted at cwd above), not a per-session param.
opencodeSessionId = await conn.createSession();
}
setOpenCodeSession(params.sessionKey, opencodeSessionId);
const sessionId = opencodeSessionId;