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.
TypeScript adhdagent-orchestrationai-agentscanvasclaude-codecodexcoding-agentsdeveloper-toolselectrongeminilinuxmacosmultiplexeropencodeparallel-agentsterminalterminal-emulatorterminal-multiplexertmuxworkspace-manager
feat(chat): renderer chat state — pure reducer core + transient store
enescdb324f3c005
diff --git a/src/renderer/state/chatSessions.ts b/src/renderer/state/chatSessions.tsnew file mode 100644index 000000000..bec644e4f--- /dev/null+++ b/src/renderer/state/chatSessions.ts@@ -0,0 +1,47 @@+// src/renderer/state/chatSessions.ts+// Transient per-chat-node conversation state (agentNodes.ts pattern): fed by chat:event+// IPC, seeded from the on-disk transcript, never persisted (the transcript already is).+import { create } from 'zustand'+import type { ChatEvent, ChatMessage } from '@shared/types'+import { applyChatEvent, emptyChatNodeState, type ChatNodeState } from './chatSessionsCore'++interface ChatSessionsStore {+ byId: Record<string, ChatNodeState>+ apply(nodeId: string, e: ChatEvent): void+ seed(nodeId: string, messages: ChatMessage[]): void+ addLocalUser(nodeId: string, text: string): void+ clearError(nodeId: string): void+ drop(nodeId: string): void+}++export const useChatSessions = create<ChatSessionsStore>((set) => ({+ byId: {},+ apply: (nodeId, e) =>+ set((st) => ({ byId: { ...st.byId, [nodeId]: applyChatEvent(st.byId[nodeId] ?? emptyChatNodeState, e) } })),+ seed: (nodeId, messages) =>+ set((st) => ({+ byId: { ...st.byId, [nodeId]: { ...(st.byId[nodeId] ?? emptyChatNodeState), messages } }+ })),+ addLocalUser: (nodeId, text) =>+ set((st) => {+ const s = st.byId[nodeId] ?? emptyChatNodeState+ return {+ byId: {+ ...st.byId,+ [nodeId]: { ...s, messages: [...s.messages, { role: 'user', parts: [{ kind: 'text', text }] }] }+ }+ }+ }),+ clearError: (nodeId) =>+ set((st) => {+ const s = st.byId[nodeId]+ if (!s) return {}+ return { byId: { ...st.byId, [nodeId]: { ...s, error: undefined } } }+ }),+ drop: (nodeId) =>+ set((st) => {+ const byId = { ...st.byId }+ delete byId[nodeId]+ return { byId }+ })+}))diff --git a/src/renderer/state/chatSessionsCore.test.ts b/src/renderer/state/chatSessionsCore.test.tsnew file mode 100644index 000000000..889866de2--- /dev/null+++ b/src/renderer/state/chatSessionsCore.test.ts@@ -0,0 +1,55 @@+// src/renderer/state/chatSessionsCore.test.ts+import { describe, expect, it } from 'vitest'+import { applyChatEvent, emptyChatNodeState } from './chatSessionsCore'++const s0 = emptyChatNodeState++describe('applyChatEvent', () => {+ it('accumulates deltas into the stream buffers', () => {+ let s = applyChatEvent(s0, { kind: 'delta', block: 'text', text: 'he' })+ s = applyChatEvent(s, { kind: 'delta', block: 'text', text: 'y' })+ s = applyChatEvent(s, { kind: 'delta', block: 'thinking', text: 'hm' })+ expect(s.streamText).toBe('hey')+ expect(s.streamThinking).toBe('hm')+ })++ it('message commits and clears the stream buffers (reconcile)', () => {+ let s = applyChatEvent(s0, { kind: 'delta', block: 'text', text: 'partial…' })+ s = applyChatEvent(s, { kind: 'message', msg: { role: 'assistant', parts: [{ kind: 'text', text: 'full' }] } })+ expect(s.streamText).toBe('')+ expect(s.messages.at(-1)?.parts).toEqual([{ kind: 'text', text: 'full' }])+ })++ it('tool + tool-result pair up by toolUseId', () => {+ let s = applyChatEvent(s0, { kind: 'tool', toolUseId: 't1', name: 'Bash', arg: 'ls' })+ s = applyChatEvent(s, { kind: 'tool-result', toolUseId: 't1', result: 'ok' })+ expect(s.tools.t1).toMatchObject({ name: 'Bash', result: 'ok' })+ expect(s.toolOrder).toEqual(['t1'])+ })++ it('turn-done accumulates cost, clears working + per-turn tool order', () => {+ let s = { ...s0, working: true, toolOrder: ['t1'] }+ s = applyChatEvent(s, { kind: 'turn-done', costUsd: 0.1 })+ s = applyChatEvent(s, { kind: 'turn-done', costUsd: 0.05 })+ expect(s.costUsd).toBeCloseTo(0.15)+ expect(s.working).toBe(false)+ expect(s.toolOrder).toEqual([])+ })++ it('permission set + cleared', () => {+ let s = applyChatEvent(s0, { kind: 'permission', requestId: 'r1', toolName: 'Bash', input: {} })+ expect(s.permission?.requestId).toBe('r1')+ s = applyChatEvent(s, { kind: 'permission-done', requestId: 'r1' })+ expect(s.permission).toBeUndefined()+ })++ it('session stores id + slash commands; queue replaces; error stores', () => {+ let s = applyChatEvent(s0, { kind: 'session', sessionId: 'abc', slashCommands: ['/compact'] })+ s = applyChatEvent(s, { kind: 'queue', items: [{ id: 'q-1', text: 'next' }] })+ s = applyChatEvent(s, { kind: 'error', message: 'boom', fatal: true })+ expect(s.sessionId).toBe('abc')+ expect(s.slashCommands).toEqual(['/compact'])+ expect(s.queue).toEqual([{ id: 'q-1', text: 'next' }])+ expect(s.error).toEqual({ message: 'boom', fatal: true })+ })+})diff --git a/src/renderer/state/chatSessionsCore.ts b/src/renderer/state/chatSessionsCore.tsnew file mode 100644index 000000000..92a931d20--- /dev/null+++ b/src/renderer/state/chatSessionsCore.ts@@ -0,0 +1,62 @@+// src/renderer/state/chatSessionsCore.ts+// Pure reducer for chat node state — kept out of the zustand store so it's unit-testable+// (same split as noteLink.ts / agentNodes patterns).+import type { ChatEvent, ChatMessage, ChatQueueItem, ChatToolSummary } from '@shared/types'++export interface ChatNodeState {+ messages: ChatMessage[]+ streamText: string+ streamThinking: string+ tools: Record<string, { name: string; arg: string; summary?: ChatToolSummary; result?: string }>+ toolOrder: string[]+ queue: ChatQueueItem[]+ permission?: { requestId: string; toolName: string; input: unknown }+ slashCommands: string[]+ sessionId?: string+ costUsd: number+ working: boolean+ error?: { message: string; fatal?: boolean }+}++export const emptyChatNodeState: ChatNodeState = {+ messages: [], streamText: '', streamThinking: '', tools: {}, toolOrder: [],+ queue: [], slashCommands: [], costUsd: 0, working: false+}++export function applyChatEvent(s: ChatNodeState, e: ChatEvent): ChatNodeState {+ switch (e.kind) {+ case 'session':+ return { ...s, sessionId: e.sessionId, slashCommands: e.slashCommands }+ case 'delta':+ return e.block === 'text'+ ? { ...s, streamText: s.streamText + e.text, working: true }+ : { ...s, streamThinking: s.streamThinking + e.text, working: true }+ case 'message':+ // The completed assistant message is authoritative — replace the delta-built buffer.+ return { ...s, messages: [...s.messages, e.msg], streamText: '', streamThinking: '' }+ case 'tool':+ return {+ ...s,+ tools: { ...s.tools, [e.toolUseId]: { name: e.name, arg: e.arg, summary: e.summary } },+ toolOrder: [...s.toolOrder, e.toolUseId]+ }+ case 'tool-result': {+ const t = s.tools[e.toolUseId]+ if (!t) return s+ return { ...s, tools: { ...s.tools, [e.toolUseId]: { ...t, result: e.result } } }+ }+ case 'permission':+ return { ...s, permission: { requestId: e.requestId, toolName: e.toolName, input: e.input } }+ case 'permission-done':+ return s.permission?.requestId === e.requestId ? { ...s, permission: undefined } : s+ case 'turn-done':+ return {+ ...s, working: false, toolOrder: [], streamText: '', streamThinking: '',+ costUsd: s.costUsd + (e.costUsd ?? 0)+ }+ case 'queue':+ return { ...s, queue: e.items }+ case 'error':+ return { ...s, error: { message: e.message, fatal: e.fatal } }+ }+}