Skip to content

Node-based terminal manager for AI coding agents — tmux-backed terminals and parallel agent sessions as draggable nodes on an infinite pan/zoom canvas. macOS, Linux, and a browser Server Edition.

feat(agents): normalize opencode plugin events to the shared state model

enes651d2ea1d775
diff --git a/src/shared/agents/normalize.test.ts b/src/shared/agents/normalize.test.ts
index 296e6d7a0..f3ea911e0 100644
--- a/src/shared/agents/normalize.test.ts
+++ b/src/shared/agents/normalize.test.ts
@@ -1,5 +1,5 @@
import { describe, it, expect } from 'vitest'
-import { normalizeClaude, type RawHookEnvelope } from './normalize'
+import { normalizeClaude, normalizeFor, type RawHookEnvelope } from './normalize'
function env(payload: Record<string, unknown>): RawHookEnvelope {
return { nodeId: 'n1', agentId: 'claude', payload }
@@ -141,3 +141,47 @@ describe('normalizeClaude — permission signals', () => {
}
})
})
+
+describe('normalizeOpencode', () => {
+ const ocEnv = (payload: Record<string, unknown>): RawHookEnvelope => ({
+ nodeId: 'n1',
+ agentId: 'opencode',
+ payload
+ })
+
+ it('maps session.created to a session start with the id', () => {
+ expect(normalizeFor('opencode', ocEnv({ event: 'session.created', sessionID: 'ses_1' }))).toEqual({
+ nodeId: 'n1',
+ agentId: 'opencode',
+ sessionId: 'ses_1',
+ kind: 'session',
+ sessionPhase: 'start'
+ })
+ })
+
+ it('maps a user message.updated to working + newTurn', () => {
+ expect(
+ normalizeFor('opencode', ocEnv({ event: 'message.updated', role: 'user', sessionID: 'ses_1' }))
+ ).toMatchObject({ kind: 'state', state: 'working', newTurn: true })
+ })
+
+ it('maps tool.execute.before to working (no newTurn)', () => {
+ const e = normalizeFor('opencode', ocEnv({ event: 'tool.execute.before' }))
+ expect(e).toMatchObject({ kind: 'state', state: 'working' })
+ expect(e?.newTurn).toBeUndefined()
+ })
+
+ it('maps permission.asked to blocked and permission.replied back to working', () => {
+ expect(normalizeFor('opencode', ocEnv({ event: 'permission.asked' }))).toMatchObject({ state: 'blocked' })
+ expect(normalizeFor('opencode', ocEnv({ event: 'permission.replied' }))).toMatchObject({ state: 'working' })
+ })
+
+ it('maps session.idle and session.error to done', () => {
+ expect(normalizeFor('opencode', ocEnv({ event: 'session.idle' }))).toMatchObject({ state: 'done' })
+ expect(normalizeFor('opencode', ocEnv({ event: 'session.error' }))).toMatchObject({ state: 'done' })
+ })
+
+ it('ignores unknown events', () => {
+ expect(normalizeFor('opencode', ocEnv({ event: 'tui.toast.show' }))).toBeNull()
+ })
+})
diff --git a/src/shared/agents/normalize.ts b/src/shared/agents/normalize.ts
index 35eba9105..bcda1803f 100644
--- a/src/shared/agents/normalize.ts
+++ b/src/shared/agents/normalize.ts
@@ -243,9 +243,39 @@ export function normalizeGemini(env: RawHookEnvelope): NormalizedAgentEvent | nu
return null
}
+// opencode plugin payload (see core/agents/hooks/opencode.ts). The managed plugin forwards
+// { event, sessionID?, role? } per hook; field names beyond `event` are read defensively —
+// opencode's event payload shapes are not a contract, so the event NAME carries the mapping.
+interface OpencodePayload {
+ event?: string
+ sessionID?: string
+ session_id?: string
+ role?: string
+}
+
+export function normalizeOpencode(env: RawHookEnvelope): NormalizedAgentEvent | null {
+ const p = env.payload as OpencodePayload
+ const base = { nodeId: env.nodeId, agentId: env.agentId, sessionId: p.sessionID ?? p.session_id }
+
+ if (p.event === 'session.created') return { ...base, kind: 'session', sessionPhase: 'start' }
+ // The plugin forwards message.updated only for user messages — opencode's turn start
+ // (mirrors Claude's UserPromptSubmit), so per-turn fan-out clears once per turn.
+ if (p.event === 'message.updated' && p.role === 'user') {
+ return { ...base, kind: 'state', state: 'working', newTurn: true }
+ }
+ if (p.event === 'tool.execute.before') return { ...base, kind: 'state', state: 'working' }
+ if (p.event === 'permission.asked') return { ...base, kind: 'state', state: 'blocked' }
+ if (p.event === 'permission.replied') return { ...base, kind: 'state', state: 'working' }
+ if (p.event === 'session.idle' || p.event === 'session.error') {
+ return { ...base, kind: 'state', state: 'done' }
+ }
+ return null
+}
+
export function normalizeFor(agentId: AgentId, env: RawHookEnvelope): NormalizedAgentEvent | null {
if (agentId === 'claude') return normalizeClaude(env)
if (agentId === 'codex') return normalizeCodex(env)
if (agentId === 'gemini') return normalizeGemini(env)
+ if (agentId === 'opencode') return normalizeOpencode(env)
return null
}