repoburg 1.3.188 → 1.3.196
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/agent-pane/.prettierignore +5 -0
- package/agent-pane/.prettierrc +8 -0
- package/agent-pane/backend/coverage/base.css +224 -0
- package/agent-pane/backend/coverage/block-navigation.js +87 -0
- package/agent-pane/backend/coverage/clover.xml +887 -0
- package/agent-pane/backend/coverage/coverage-final.json +10 -0
- package/agent-pane/backend/coverage/favicon.png +0 -0
- package/agent-pane/backend/coverage/index.html +176 -0
- package/agent-pane/backend/coverage/prettify.css +1 -0
- package/agent-pane/backend/coverage/prettify.js +2 -0
- package/agent-pane/backend/coverage/sort-arrow-sprite.png +0 -0
- package/agent-pane/backend/coverage/sorter.js +210 -0
- package/agent-pane/backend/coverage/src/codex/codex-provider.ts.html +1402 -0
- package/agent-pane/backend/coverage/src/codex/index.html +131 -0
- package/agent-pane/backend/coverage/src/codex/item-mapper.ts.html +331 -0
- package/agent-pane/backend/coverage/src/db/index.html +131 -0
- package/agent-pane/backend/coverage/src/db/index.ts.html +436 -0
- package/agent-pane/backend/coverage/src/db/schema.ts.html +175 -0
- package/agent-pane/backend/coverage/src/index.html +116 -0
- package/agent-pane/backend/coverage/src/index.ts.html +262 -0
- package/agent-pane/backend/coverage/src/routes/index.html +146 -0
- package/agent-pane/backend/coverage/src/routes/sessions.ts.html +991 -0
- package/agent-pane/backend/coverage/src/routes/turns.ts.html +256 -0
- package/agent-pane/backend/coverage/src/routes/workspaces.ts.html +406 -0
- package/agent-pane/backend/coverage/src/ws/index.html +116 -0
- package/agent-pane/backend/coverage/src/ws/index.ts.html +238 -0
- package/agent-pane/backend/data.db +0 -0
- package/agent-pane/backend/data.db-shm +0 -0
- package/agent-pane/backend/data.db-wal +0 -0
- package/agent-pane/backend/dist/index.js +70869 -0
- package/agent-pane/backend/package-lock.json +4529 -1013
- package/agent-pane/backend/package.json +10 -6
- package/agent-pane/backend/src/claude/claude-message-mapper.test.ts +163 -0
- package/agent-pane/backend/src/claude/claude-message-mapper.ts +154 -0
- package/agent-pane/backend/src/claude/claude-provider.test.ts +65 -0
- package/agent-pane/backend/src/claude/claude-provider.ts +482 -0
- package/agent-pane/backend/src/claude/claude-types.ts +33 -0
- package/agent-pane/backend/src/codex/app-server-client.test.ts +183 -0
- package/agent-pane/backend/src/codex/app-server-client.ts +225 -0
- package/agent-pane/backend/src/codex/app-server-types.ts +259 -0
- package/agent-pane/backend/src/codex/codex-provider-runtime-config.test.ts +56 -0
- package/agent-pane/backend/src/codex/codex-provider.test.ts +60 -0
- package/agent-pane/backend/src/codex/codex-provider.ts +521 -265
- package/agent-pane/backend/src/codex/item-mapper.test.ts +221 -0
- package/agent-pane/backend/src/codex/item-mapper.ts +35 -8
- package/agent-pane/backend/src/codex/shared-app-server.ts +25 -0
- package/agent-pane/backend/src/db/index.test.ts +125 -0
- package/agent-pane/backend/src/db/index.ts +39 -8
- package/agent-pane/backend/src/db/schema.ts +28 -74
- package/agent-pane/backend/src/index.ts +29 -2
- package/agent-pane/backend/src/integration/__fixtures__/cancel-turn.json +29 -0
- package/agent-pane/backend/src/integration/__fixtures__/delta-streaming.json +40 -0
- package/agent-pane/backend/src/integration/__fixtures__/failed-turn.json +16 -0
- package/agent-pane/backend/src/integration/__fixtures__/mcp-tool-call.json +60 -0
- package/agent-pane/backend/src/integration/__fixtures__/missing-item-started.json +34 -0
- package/agent-pane/backend/src/integration/__fixtures__/multi-item-turn.json +85 -0
- package/agent-pane/backend/src/integration/__fixtures__/multi-turn.json +48 -0
- package/agent-pane/backend/src/integration/__fixtures__/simple-turn.json +35 -0
- package/agent-pane/backend/src/integration/delta-streaming.test.ts +130 -0
- package/agent-pane/backend/src/integration/helpers/app-server-mock.ts +272 -0
- package/agent-pane/backend/src/integration/helpers/test-app.ts +108 -0
- package/agent-pane/backend/src/integration/helpers/ws-capture.ts +64 -0
- package/agent-pane/backend/src/integration/item-lifecycle.test.ts +133 -0
- package/agent-pane/backend/src/integration/mcp-tool-call.test.ts +102 -0
- package/agent-pane/backend/src/integration/missing-item-started.test.ts +91 -0
- package/agent-pane/backend/src/integration/multi-turn.test.ts +121 -0
- package/agent-pane/backend/src/integration/session-operations.test.ts +168 -0
- package/agent-pane/backend/src/integration/title-generation.test.ts +109 -0
- package/agent-pane/backend/src/integration/turn-cancel.test.ts +111 -0
- package/agent-pane/backend/src/integration/turn-failure.test.ts +97 -0
- package/agent-pane/backend/src/integration/turn-lifecycle.test.ts +141 -0
- package/agent-pane/backend/src/providers/index.ts +14 -0
- package/agent-pane/backend/src/providers/registry.test.ts +92 -0
- package/agent-pane/backend/src/providers/registry.ts +43 -0
- package/agent-pane/backend/src/providers/runtime-provider.ts +25 -0
- package/agent-pane/backend/src/routes/files.ts +31 -0
- package/agent-pane/backend/src/routes/mcp.ts +33 -0
- package/agent-pane/backend/src/routes/models.test.ts +92 -0
- package/agent-pane/backend/src/routes/models.ts +16 -0
- package/agent-pane/backend/src/routes/plugins.ts +40 -0
- package/agent-pane/backend/src/routes/providers.test.ts +69 -0
- package/agent-pane/backend/src/routes/providers.ts +57 -0
- package/agent-pane/backend/src/routes/sessions.test.ts +424 -0
- package/agent-pane/backend/src/routes/sessions.ts +295 -42
- package/agent-pane/backend/src/routes/skills.test.ts +114 -0
- package/agent-pane/backend/src/routes/skills.ts +70 -0
- package/agent-pane/backend/src/routes/turns.test.ts +152 -0
- package/agent-pane/backend/src/routes/turns.ts +36 -16
- package/agent-pane/backend/src/routes/workspaces.test.ts +197 -0
- package/agent-pane/backend/src/routes/workspaces.ts +73 -55
- package/agent-pane/backend/src/ws/index.test.ts +107 -0
- package/agent-pane/backend/src/ws/index.ts +19 -11
- package/agent-pane/backend/tsconfig.json +4 -2
- package/agent-pane/backend/vitest.config.ts +20 -0
- package/agent-pane/eslint.config.js +33 -0
- package/agent-pane/frontend/coverage/base.css +224 -0
- package/agent-pane/frontend/coverage/block-navigation.js +87 -0
- package/agent-pane/frontend/coverage/clover.xml +2962 -0
- package/agent-pane/frontend/coverage/coverage-final.json +37 -0
- package/agent-pane/frontend/coverage/favicon.png +0 -0
- package/agent-pane/frontend/coverage/index.html +251 -0
- package/agent-pane/frontend/coverage/prettify.css +1 -0
- package/agent-pane/frontend/coverage/prettify.js +2 -0
- package/agent-pane/frontend/coverage/sort-arrow-sprite.png +0 -0
- package/agent-pane/frontend/coverage/sorter.js +210 -0
- package/agent-pane/frontend/coverage/src/App.tsx.html +217 -0
- package/agent-pane/frontend/coverage/src/AppInitializer.tsx.html +223 -0
- package/agent-pane/frontend/coverage/src/api/client.ts.html +187 -0
- package/agent-pane/frontend/coverage/src/api/index.html +161 -0
- package/agent-pane/frontend/coverage/src/api/sessions.ts.html +316 -0
- package/agent-pane/frontend/coverage/src/api/websocket.ts.html +544 -0
- package/agent-pane/frontend/coverage/src/api/workspaces.ts.html +199 -0
- package/agent-pane/frontend/coverage/src/components/CollapsibleToolCalls.tsx.html +226 -0
- package/agent-pane/frontend/coverage/src/components/CommandInputBar.tsx.html +382 -0
- package/agent-pane/frontend/coverage/src/components/CommandPalette.tsx.html +715 -0
- package/agent-pane/frontend/coverage/src/components/IdeWorkspace.tsx.html +160 -0
- package/agent-pane/frontend/coverage/src/components/ModelSelector.tsx.html +478 -0
- package/agent-pane/frontend/coverage/src/components/NewSessionDialog.tsx.html +472 -0
- package/agent-pane/frontend/coverage/src/components/RenderArea.tsx.html +574 -0
- package/agent-pane/frontend/coverage/src/components/RightPanel.tsx.html +205 -0
- package/agent-pane/frontend/coverage/src/components/RuntimeIcon.tsx.html +133 -0
- package/agent-pane/frontend/coverage/src/components/SessionTabs.tsx.html +217 -0
- package/agent-pane/frontend/coverage/src/components/StatusBar.tsx.html +271 -0
- package/agent-pane/frontend/coverage/src/components/StatusDot.tsx.html +109 -0
- package/agent-pane/frontend/coverage/src/components/Toast.tsx.html +313 -0
- package/agent-pane/frontend/coverage/src/components/TurnContent.tsx.html +1216 -0
- package/agent-pane/frontend/coverage/src/components/WorkspaceSidebar.tsx.html +1138 -0
- package/agent-pane/frontend/coverage/src/components/index.html +326 -0
- package/agent-pane/frontend/coverage/src/components/panels/ChangesPanel.tsx.html +247 -0
- package/agent-pane/frontend/coverage/src/components/panels/LogsPanel.tsx.html +160 -0
- package/agent-pane/frontend/coverage/src/components/panels/MetricsPanel.tsx.html +427 -0
- package/agent-pane/frontend/coverage/src/components/panels/TurnsPanel.tsx.html +397 -0
- package/agent-pane/frontend/coverage/src/components/panels/index.html +161 -0
- package/agent-pane/frontend/coverage/src/constants/index.html +116 -0
- package/agent-pane/frontend/coverage/src/constants/models.ts.html +406 -0
- package/agent-pane/frontend/coverage/src/hooks/index.html +146 -0
- package/agent-pane/frontend/coverage/src/hooks/useEscapeKey.ts.html +124 -0
- package/agent-pane/frontend/coverage/src/hooks/useOutsideClick.ts.html +142 -0
- package/agent-pane/frontend/coverage/src/hooks/useSessionStatus.ts.html +187 -0
- package/agent-pane/frontend/coverage/src/index.html +146 -0
- package/agent-pane/frontend/coverage/src/main.tsx.html +115 -0
- package/agent-pane/frontend/coverage/src/store/index.html +116 -0
- package/agent-pane/frontend/coverage/src/store/store.ts.html +2404 -0
- package/agent-pane/frontend/coverage/src/test/component-helpers.ts.html +190 -0
- package/agent-pane/frontend/coverage/src/test/fixtures.ts.html +367 -0
- package/agent-pane/frontend/coverage/src/test/index.html +131 -0
- package/agent-pane/frontend/coverage/src/types/index.html +116 -0
- package/agent-pane/frontend/coverage/src/types/index.ts.html +466 -0
- package/agent-pane/frontend/coverage/src/utils/changes.ts.html +202 -0
- package/agent-pane/frontend/coverage/src/utils/duration.ts.html +127 -0
- package/agent-pane/frontend/coverage/src/utils/index.html +131 -0
- package/agent-pane/frontend/dist/assets/index-Bg0PF2aV.css +1 -0
- package/agent-pane/frontend/dist/assets/index-BlybeX5e.js +281 -0
- package/agent-pane/frontend/dist/index.html +19 -0
- package/agent-pane/frontend/package-lock.json +2434 -213
- package/agent-pane/frontend/package.json +10 -3
- package/agent-pane/frontend/src/App.test.tsx +74 -0
- package/agent-pane/frontend/src/App.tsx +1 -1
- package/agent-pane/frontend/src/AppInitializer.test.tsx +142 -0
- package/agent-pane/frontend/src/AppInitializer.tsx +1 -1
- package/agent-pane/frontend/src/api/client.test.ts +126 -0
- package/agent-pane/frontend/src/api/client.ts +22 -29
- package/agent-pane/frontend/src/api/models.ts +7 -0
- package/agent-pane/frontend/src/api/providers.ts +12 -0
- package/agent-pane/frontend/src/api/sessions.test.ts +241 -0
- package/agent-pane/frontend/src/api/sessions.ts +43 -1
- package/agent-pane/frontend/src/api/skills.ts +12 -0
- package/agent-pane/frontend/src/api/websocket.test.ts +288 -0
- package/agent-pane/frontend/src/api/websocket.ts +27 -2
- package/agent-pane/frontend/src/api/workspaces.test.ts +112 -0
- package/agent-pane/frontend/src/api/workspaces.ts +17 -3
- package/agent-pane/frontend/src/components/CollapsibleToolCalls.test.tsx +49 -0
- package/agent-pane/frontend/src/components/CollapsibleToolCalls.tsx +2 -5
- package/agent-pane/frontend/src/components/CommandInputBar.test.tsx +133 -0
- package/agent-pane/frontend/src/components/CommandInputBar.tsx +296 -17
- package/agent-pane/frontend/src/components/CommandPalette.test.tsx +230 -0
- package/agent-pane/frontend/src/components/CommandPalette.tsx +26 -23
- package/agent-pane/frontend/src/components/IdeWorkspace.tsx +1 -1
- package/agent-pane/frontend/src/components/ModelSelector.test.tsx +129 -0
- package/agent-pane/frontend/src/components/ModelSelector.tsx +191 -0
- package/agent-pane/frontend/src/components/NewSessionDialog.test.tsx +95 -0
- package/agent-pane/frontend/src/components/NewSessionDialog.tsx +58 -13
- package/agent-pane/frontend/src/components/RenderArea.test.tsx +160 -0
- package/agent-pane/frontend/src/components/RenderArea.tsx +18 -25
- package/agent-pane/frontend/src/components/RightPanel.test.tsx +43 -0
- package/agent-pane/frontend/src/components/RightPanel.tsx +1 -1
- package/agent-pane/frontend/src/components/RuntimeIcon.test.tsx +32 -0
- package/agent-pane/frontend/src/components/RuntimeIcon.tsx +1 -1
- package/agent-pane/frontend/src/components/SessionTabs.test.tsx +72 -0
- package/agent-pane/frontend/src/components/SessionTabs.tsx +12 -12
- package/agent-pane/frontend/src/components/StatusBar.test.tsx +56 -0
- package/agent-pane/frontend/src/components/StatusBar.tsx +25 -31
- package/agent-pane/frontend/src/components/StatusDot.test.tsx +25 -0
- package/agent-pane/frontend/src/components/StatusDot.tsx +2 -7
- package/agent-pane/frontend/src/components/Toast.test.tsx +79 -0
- package/agent-pane/frontend/src/components/Toast.tsx +2 -6
- package/agent-pane/frontend/src/components/TurnContent.test.tsx +287 -0
- package/agent-pane/frontend/src/components/TurnContent.tsx +107 -24
- package/agent-pane/frontend/src/components/WorkspaceSidebar.test.tsx +297 -0
- package/agent-pane/frontend/src/components/WorkspaceSidebar.tsx +33 -32
- package/agent-pane/frontend/src/components/input/OptionSubPopup.tsx +32 -0
- package/agent-pane/frontend/src/components/input/SkillMentionPopup.test.tsx +63 -0
- package/agent-pane/frontend/src/components/input/SkillMentionPopup.tsx +78 -0
- package/agent-pane/frontend/src/components/input/SlashPopup.test.tsx +96 -0
- package/agent-pane/frontend/src/components/input/SlashPopup.tsx +63 -0
- package/agent-pane/frontend/src/components/panels/ChangesPanel.test.tsx +61 -0
- package/agent-pane/frontend/src/components/panels/ChangesPanel.tsx +12 -39
- package/agent-pane/frontend/src/components/panels/LogsPanel.test.tsx +58 -0
- package/agent-pane/frontend/src/components/panels/LogsPanel.tsx +1 -1
- package/agent-pane/frontend/src/components/panels/MetricsPanel.test.tsx +71 -0
- package/agent-pane/frontend/src/components/panels/MetricsPanel.tsx +63 -31
- package/agent-pane/frontend/src/components/panels/TurnsPanel.test.tsx +83 -0
- package/agent-pane/frontend/src/components/panels/TurnsPanel.tsx +66 -7
- package/agent-pane/frontend/src/constants/models.test.ts +97 -0
- package/agent-pane/frontend/src/constants/models.ts +129 -0
- package/agent-pane/frontend/src/hooks/useEscapeKey.test.ts +42 -0
- package/agent-pane/frontend/src/hooks/useEscapeKey.ts +13 -0
- package/agent-pane/frontend/src/hooks/useOutsideClick.test.ts +92 -0
- package/agent-pane/frontend/src/hooks/useOutsideClick.ts +19 -0
- package/agent-pane/frontend/src/hooks/useProvider.ts +44 -0
- package/agent-pane/frontend/src/hooks/useSessionStatus.test.ts +114 -0
- package/agent-pane/frontend/src/hooks/useSessionStatus.ts +34 -0
- package/agent-pane/frontend/src/main.test.tsx +37 -0
- package/agent-pane/frontend/src/main.tsx +1 -1
- package/agent-pane/frontend/src/store/sessionSlice.ts +417 -0
- package/agent-pane/frontend/src/store/store.test.ts +1284 -0
- package/agent-pane/frontend/src/store/store.ts +16 -617
- package/agent-pane/frontend/src/store/uiSlice.ts +83 -0
- package/agent-pane/frontend/src/store/workspaceSlice.ts +77 -0
- package/agent-pane/frontend/src/store/wsSlice.ts +292 -0
- package/agent-pane/frontend/src/styles/app.css +113 -7
- package/agent-pane/frontend/src/test/component-helpers.ts +36 -0
- package/agent-pane/frontend/src/test/fixtures.ts +96 -0
- package/agent-pane/frontend/src/test-setup.ts +5 -0
- package/agent-pane/frontend/src/types/index.test.ts +128 -0
- package/agent-pane/frontend/src/types/index.ts +80 -104
- package/agent-pane/frontend/src/utils/changes.test.ts +81 -0
- package/agent-pane/frontend/src/utils/changes.ts +39 -0
- package/agent-pane/frontend/src/utils/duration.test.ts +71 -0
- package/agent-pane/frontend/src/utils/duration.ts +14 -0
- package/agent-pane/frontend/tsconfig.json +4 -1
- package/agent-pane/frontend/vite.config.ts +6 -0
- package/agent-pane/frontend/vitest.config.ts +29 -0
- package/agent-pane/package-lock.json +1527 -0
- package/agent-pane/package.json +19 -0
- package/agent-pane/shared/provider-types.ts +53 -0
- package/agent-pane/shared/types.ts +107 -0
- package/agentpane-branch-session.png +0 -0
- package/agentpane-expanded-all.png +0 -0
- package/agentpane-fixed-rendering.png +0 -0
- package/agentpane-focus-check.png +0 -0
- package/agentpane-initial.png +0 -0
- package/agentpane-mcp-detail.png +0 -0
- package/agentpane-no-focus-ring.png +0 -0
- package/agentpane-soft-focus.png +0 -0
- package/backend/dist/src/llm-provider/ollama-llm.provider.js +1 -0
- package/backend/dist/src/llm-provider/ollama-llm.provider.js.map +1 -1
- package/backend/dist/tsconfig.build.tsbuildinfo +1 -1
- package/data.db +0 -0
- package/empty-state.png +0 -0
- package/frontend-home.png +0 -0
- package/model-selector-open.png +0 -0
- package/package.json +1 -1
- package/slash-popup-working.png +0 -0
- package/agent-pane/frontend/src/components/DiffViewer.tsx +0 -27
|
@@ -1,46 +1,156 @@
|
|
|
1
|
-
import
|
|
1
|
+
import type { SessionRuntimeConfig, ModelInfo } from '@agentpane/shared';
|
|
2
2
|
import { v4 as uuidv4 } from 'uuid';
|
|
3
|
-
import Database from 'better-sqlite3';
|
|
4
|
-
import { readFileSync, writeFileSync, unlinkSync } from 'node:fs';
|
|
3
|
+
import type Database from 'better-sqlite3';
|
|
5
4
|
import { getDb } from '../db/index.ts';
|
|
6
5
|
import { broadcast } from '../ws/index.ts';
|
|
7
6
|
import { mapItemData, getItemStatus } from './item-mapper.ts';
|
|
7
|
+
import { AppServerClient } from './app-server-client.ts';
|
|
8
|
+
import type {
|
|
9
|
+
ItemNotification,
|
|
10
|
+
TurnFailedNotification,
|
|
11
|
+
ThreadStartResult,
|
|
12
|
+
TurnStartResult,
|
|
13
|
+
AppServerItem,
|
|
14
|
+
AgentMessageDeltaNotification,
|
|
15
|
+
TokenUsageNotification,
|
|
16
|
+
ModelListResult,
|
|
17
|
+
} from './app-server-types.ts';
|
|
18
|
+
import { mapTokenUsage } from './app-server-types.ts';
|
|
19
|
+
import { getSharedAppServer, closeSharedAppServer } from './shared-app-server.ts';
|
|
8
20
|
import type { Session, Turn, Item, ItemStatus } from '../db/schema.ts';
|
|
21
|
+
import type { RuntimeProvider } from '../providers/runtime-provider.ts';
|
|
9
22
|
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
23
|
+
/** Per-session app-server client + thread state. */
|
|
24
|
+
interface SessionEntry {
|
|
25
|
+
client: AppServerClient;
|
|
26
|
+
threadId: string;
|
|
27
|
+
turnId: string | null;
|
|
14
28
|
}
|
|
15
29
|
|
|
16
|
-
const
|
|
30
|
+
const sessionMap = new Map<string, SessionEntry>();
|
|
31
|
+
|
|
32
|
+
/** Update the running state and status of a session. */
|
|
33
|
+
function setSessionStatus(
|
|
34
|
+
db: Database.Database,
|
|
35
|
+
sessionId: string,
|
|
36
|
+
isRunning: 0 | 1,
|
|
37
|
+
status: string,
|
|
38
|
+
now: string,
|
|
39
|
+
hasUnreviewed = false,
|
|
40
|
+
): void {
|
|
41
|
+
if (hasUnreviewed) {
|
|
42
|
+
db.prepare('UPDATE sessions SET is_running = ?, status = ?, has_unreviewed = 1, updated_at = ? WHERE id = ?').run(
|
|
43
|
+
isRunning,
|
|
44
|
+
status,
|
|
45
|
+
now,
|
|
46
|
+
sessionId,
|
|
47
|
+
);
|
|
48
|
+
} else {
|
|
49
|
+
db.prepare('UPDATE sessions SET is_running = ?, status = ?, updated_at = ? WHERE id = ?').run(
|
|
50
|
+
isRunning,
|
|
51
|
+
status,
|
|
52
|
+
now,
|
|
53
|
+
sessionId,
|
|
54
|
+
);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
17
57
|
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
58
|
+
/** Parse runtime_config JSON from the session row. Returns null on parse failure. */
|
|
59
|
+
function parseRuntimeConfig(session: Session): SessionRuntimeConfig | null {
|
|
60
|
+
if (!session.runtime_config) return null;
|
|
61
|
+
try {
|
|
62
|
+
return JSON.parse(session.runtime_config) as SessionRuntimeConfig;
|
|
63
|
+
} catch {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
26
66
|
}
|
|
27
67
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
68
|
+
/** Map runtime config sandbox mode to app-server sandbox type string (kebab-case). */
|
|
69
|
+
function sandboxType(config: SessionRuntimeConfig | null): 'read-only' | 'workspace-write' | 'danger-full-access' {
|
|
70
|
+
switch (config?.sandboxMode) {
|
|
71
|
+
case 'read-only':
|
|
72
|
+
return 'read-only';
|
|
73
|
+
case 'danger-full-access':
|
|
74
|
+
return 'danger-full-access';
|
|
75
|
+
default:
|
|
76
|
+
return 'workspace-write';
|
|
32
77
|
}
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/** Convert app-server camelCase item type to DB snake_case item type. */
|
|
81
|
+
function normalizeItemType(type: string): string {
|
|
82
|
+
const map: Record<string, string> = {
|
|
83
|
+
agentMessage: 'agent_message',
|
|
84
|
+
userMessage: 'user_message',
|
|
85
|
+
agentReasoning: 'reasoning',
|
|
86
|
+
commandExecution: 'command_execution',
|
|
87
|
+
fileChange: 'file_change',
|
|
88
|
+
mcpToolCall: 'mcp_tool_call',
|
|
89
|
+
webSearch: 'web_search',
|
|
90
|
+
todoList: 'todo_list',
|
|
91
|
+
error: 'error',
|
|
92
|
+
};
|
|
93
|
+
return map[type] ?? type;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** Check if an item type should be skipped (not stored in DB). */
|
|
97
|
+
function shouldSkipItem(type: string): boolean {
|
|
98
|
+
return type === 'userMessage' || type === 'user_message';
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Check if a reasoning item has empty content (app-server sends summary: [], content: []). */
|
|
102
|
+
function isEmptyReasoning(item: AppServerItem): boolean {
|
|
103
|
+
const summary = Array.isArray(item.summary) ? item.summary : [];
|
|
104
|
+
const content = Array.isArray(item.content) ? item.content : [];
|
|
105
|
+
const text = item.text ?? '';
|
|
106
|
+
return summary.length === 0 && content.length === 0 && text.length === 0;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
/** Create or resume a thread for the session via a per-session app-server client. */
|
|
110
|
+
async function getOrCreateThread(session: Session): Promise<SessionEntry> {
|
|
111
|
+
const existing = sessionMap.get(session.id);
|
|
112
|
+
if (existing) return existing;
|
|
113
|
+
|
|
114
|
+
const cwd = session.cwd ?? process.cwd();
|
|
115
|
+
const client = new AppServerClient({ cwd });
|
|
116
|
+
await client.initialize();
|
|
33
117
|
|
|
34
|
-
const
|
|
35
|
-
|
|
118
|
+
const config = parseRuntimeConfig(session);
|
|
119
|
+
const sandbox = sandboxType(config);
|
|
120
|
+
|
|
121
|
+
let threadId: string;
|
|
36
122
|
|
|
37
123
|
if (session.external_thread_id) {
|
|
38
|
-
|
|
124
|
+
const result = await client.request<ThreadStartResult>('thread/resume', {
|
|
125
|
+
threadId: session.external_thread_id,
|
|
126
|
+
cwd,
|
|
127
|
+
approvalPolicy: 'never',
|
|
128
|
+
sandbox,
|
|
129
|
+
model: session.model ?? undefined,
|
|
130
|
+
});
|
|
131
|
+
threadId = result.thread.id;
|
|
39
132
|
} else {
|
|
40
|
-
|
|
133
|
+
const result = await client.request<ThreadStartResult>('thread/start', {
|
|
134
|
+
cwd,
|
|
135
|
+
approvalPolicy: 'never',
|
|
136
|
+
sandbox,
|
|
137
|
+
model: session.model ?? undefined,
|
|
138
|
+
});
|
|
139
|
+
threadId = result.thread.id;
|
|
140
|
+
|
|
141
|
+
// Save the thread ID to the database
|
|
142
|
+
const db = getDb();
|
|
143
|
+
const now = new Date().toISOString();
|
|
144
|
+
db.prepare('UPDATE sessions SET external_thread_id = ?, updated_at = ? WHERE id = ?').run(
|
|
145
|
+
threadId,
|
|
146
|
+
now,
|
|
147
|
+
session.id,
|
|
148
|
+
);
|
|
41
149
|
}
|
|
42
150
|
|
|
43
|
-
|
|
151
|
+
const entry: SessionEntry = { client, threadId, turnId: null };
|
|
152
|
+
sessionMap.set(session.id, entry);
|
|
153
|
+
return entry;
|
|
44
154
|
}
|
|
45
155
|
|
|
46
156
|
/** Start a Codex turn. Fire-and-forget — streams events via WS. */
|
|
@@ -48,316 +158,462 @@ export async function startTurn(session: Session, turn: Turn): Promise<void> {
|
|
|
48
158
|
const db = getDb();
|
|
49
159
|
|
|
50
160
|
// Phase 1: create or resume thread
|
|
51
|
-
let
|
|
52
|
-
let thread: Thread;
|
|
53
|
-
|
|
161
|
+
let entry: SessionEntry;
|
|
54
162
|
try {
|
|
55
|
-
|
|
56
|
-
codex = result.codex;
|
|
57
|
-
thread = result.thread;
|
|
163
|
+
entry = await getOrCreateThread(session);
|
|
58
164
|
} catch (err) {
|
|
59
|
-
const errMsg = `Failed to create
|
|
165
|
+
const errMsg = `Failed to create app-server thread: ${(err as Error).message}`;
|
|
60
166
|
const now = new Date().toISOString();
|
|
61
|
-
db.prepare('UPDATE turns SET status = ?, error = ?, updated_at = ? WHERE id = ?')
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
167
|
+
db.prepare('UPDATE turns SET status = ?, error = ?, updated_at = ? WHERE id = ?').run(
|
|
168
|
+
'failed',
|
|
169
|
+
errMsg,
|
|
170
|
+
now,
|
|
171
|
+
turn.id,
|
|
172
|
+
);
|
|
173
|
+
setSessionStatus(db, session.id, 0, 'failed', now);
|
|
65
174
|
broadcast('turn:failed', { sessionId: session.id, turnId: turn.id, error: errMsg });
|
|
66
175
|
return;
|
|
67
176
|
}
|
|
68
177
|
|
|
69
|
-
// Phase 2:
|
|
70
|
-
|
|
71
|
-
threadMap.set(session.id, { codex, thread, abortController });
|
|
72
|
-
|
|
178
|
+
// Phase 2: update status
|
|
179
|
+
entry.turnId = turn.id;
|
|
73
180
|
const now = new Date().toISOString();
|
|
74
|
-
db.prepare('UPDATE turns SET status = ?, updated_at = ? WHERE id = ?')
|
|
75
|
-
|
|
76
|
-
db.prepare('UPDATE sessions SET is_running = 1, status = ?, updated_at = ? WHERE id = ?')
|
|
77
|
-
.run('running', now, session.id);
|
|
78
|
-
|
|
181
|
+
db.prepare('UPDATE turns SET status = ?, updated_at = ? WHERE id = ?').run('running', now, turn.id);
|
|
182
|
+
setSessionStatus(db, session.id, 1, 'running', now);
|
|
79
183
|
broadcast('turn:started', { sessionId: session.id, turnId: turn.id });
|
|
80
184
|
|
|
81
|
-
// Phase 3:
|
|
82
|
-
|
|
83
|
-
|
|
185
|
+
// Phase 3: register notification handlers and send turn/start
|
|
186
|
+
const { client, threadId } = entry;
|
|
187
|
+
const config = parseRuntimeConfig(session);
|
|
188
|
+
const sandbox = sandboxType(config);
|
|
84
189
|
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
}
|
|
88
|
-
} catch (err) {
|
|
89
|
-
const isAbort = abortController.signal.aborted || (err as Error).name === 'AbortError';
|
|
90
|
-
const errMsg = (err as Error).message;
|
|
91
|
-
const now2 = new Date().toISOString();
|
|
190
|
+
// Flag to prevent duplicate handler execution after turn ends
|
|
191
|
+
let turnDone = false;
|
|
92
192
|
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
193
|
+
// Register handlers for this turn (scoped by threadId — one active turn per session)
|
|
194
|
+
const handleItemStarted = (params: unknown) => {
|
|
195
|
+
if (turnDone) return;
|
|
196
|
+
const p = params as ItemNotification;
|
|
197
|
+
if (p.threadId !== threadId) return;
|
|
198
|
+
handleItemEvent(p, session.id, turn.id, 'in_progress', 'item:started');
|
|
199
|
+
};
|
|
200
|
+
const handleItemUpdated = (params: unknown) => {
|
|
201
|
+
if (turnDone) return;
|
|
202
|
+
const p = params as ItemNotification;
|
|
203
|
+
if (p.threadId !== threadId) return;
|
|
204
|
+
handleItemEvent(p, session.id, turn.id, 'in_progress', 'item:updated');
|
|
205
|
+
};
|
|
206
|
+
const handleItemCompleted = (params: unknown) => {
|
|
207
|
+
if (turnDone) return;
|
|
208
|
+
const p = params as ItemNotification;
|
|
209
|
+
if (p.threadId !== threadId) return;
|
|
210
|
+
const status = getItemStatus(p.item);
|
|
211
|
+
handleItemEvent(p, session.id, turn.id, status, 'item:completed');
|
|
212
|
+
};
|
|
213
|
+
const handleTurnCompleted = (params: unknown) => {
|
|
214
|
+
if (turnDone) return;
|
|
215
|
+
const p = params as { threadId: string; turn?: { id: string; status: string }; usage?: unknown };
|
|
216
|
+
if (p.threadId !== threadId) return;
|
|
217
|
+
turnDone = true;
|
|
218
|
+
const db = getDb();
|
|
219
|
+
const now = new Date().toISOString();
|
|
220
|
+
// Token usage may be in params (mock) or already saved via thread/tokenUsage/updated (real app-server).
|
|
221
|
+
// When p.usage is undefined, preserve existing token_usage (set by thread/tokenUsage/updated handler).
|
|
222
|
+
if (p.usage !== undefined) {
|
|
223
|
+
db.prepare('UPDATE turns SET status = ?, token_usage = ?, updated_at = ? WHERE id = ?').run(
|
|
224
|
+
'completed',
|
|
225
|
+
JSON.stringify(p.usage),
|
|
226
|
+
now,
|
|
227
|
+
turn.id,
|
|
228
|
+
);
|
|
229
|
+
broadcast('turn:completed', { sessionId: session.id, turnId: turn.id, usage: p.usage, hasUnreviewed: true });
|
|
99
230
|
} else {
|
|
100
|
-
db.prepare('UPDATE turns SET status = ?,
|
|
101
|
-
|
|
102
|
-
db.prepare('UPDATE sessions SET is_running = 0, status = ?, updated_at = ? WHERE id = ?')
|
|
103
|
-
.run('failed', now2, session.id);
|
|
104
|
-
broadcast('turn:failed', { sessionId: session.id, turnId: turn.id, error: errMsg });
|
|
105
|
-
}
|
|
106
|
-
} finally {
|
|
107
|
-
const entry = threadMap.get(session.id);
|
|
108
|
-
if (entry) {
|
|
109
|
-
threadMap.set(session.id, { ...entry, abortController: null });
|
|
231
|
+
db.prepare('UPDATE turns SET status = ?, updated_at = ? WHERE id = ?').run('completed', now, turn.id);
|
|
232
|
+
broadcast('turn:completed', { sessionId: session.id, turnId: turn.id, hasUnreviewed: true });
|
|
110
233
|
}
|
|
111
|
-
|
|
112
|
-
|
|
234
|
+
setSessionStatus(db, session.id, 0, 'active', now, true);
|
|
235
|
+
syncThreadTitle(session.id);
|
|
236
|
+
};
|
|
237
|
+
const handleTurnFailed = (params: unknown) => {
|
|
238
|
+
if (turnDone) return;
|
|
239
|
+
const p = params as TurnFailedNotification;
|
|
240
|
+
if (p.threadId !== threadId) return;
|
|
241
|
+
turnDone = true;
|
|
242
|
+
const db = getDb();
|
|
243
|
+
const now = new Date().toISOString();
|
|
244
|
+
db.prepare('UPDATE turns SET status = ?, error = ?, updated_at = ? WHERE id = ?').run(
|
|
245
|
+
'failed',
|
|
246
|
+
p.error.message,
|
|
247
|
+
now,
|
|
248
|
+
turn.id,
|
|
249
|
+
);
|
|
250
|
+
setSessionStatus(db, session.id, 0, 'failed', now);
|
|
251
|
+
broadcast('turn:failed', { sessionId: session.id, turnId: turn.id, error: p.error.message });
|
|
252
|
+
};
|
|
113
253
|
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
const
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
return
|
|
254
|
+
// Stream incremental text deltas to frontend — no DB write per delta (performance).
|
|
255
|
+
// The final text is written by handleItemEvent on item/completed.
|
|
256
|
+
const handleAgentMessageDelta = (params: unknown) => {
|
|
257
|
+
if (turnDone) return;
|
|
258
|
+
const p = params as AgentMessageDeltaNotification;
|
|
259
|
+
if (p.threadId !== threadId) return;
|
|
260
|
+
broadcast('item:delta', {
|
|
261
|
+
sessionId: session.id,
|
|
262
|
+
turnId: turn.id,
|
|
263
|
+
itemId: p.itemId,
|
|
264
|
+
delta: p.delta,
|
|
265
|
+
});
|
|
266
|
+
};
|
|
267
|
+
|
|
268
|
+
// Save token usage from thread/tokenUsage/updated — no turnDone guard (may arrive before or after turn/completed).
|
|
269
|
+
const handleTokenUsageUpdated = (params: unknown) => {
|
|
270
|
+
const p = params as TokenUsageNotification;
|
|
271
|
+
if (p.threadId !== threadId) return;
|
|
272
|
+
const usage = mapTokenUsage(p.tokenUsage.total);
|
|
273
|
+
const db = getDb();
|
|
274
|
+
const now = new Date().toISOString();
|
|
275
|
+
db.prepare('UPDATE turns SET token_usage = ?, updated_at = ? WHERE id = ?').run(
|
|
276
|
+
JSON.stringify(usage),
|
|
277
|
+
now,
|
|
278
|
+
turn.id,
|
|
279
|
+
);
|
|
280
|
+
broadcast('turn:tokenUsage', { sessionId: session.id, turnId: turn.id, usage });
|
|
281
|
+
};
|
|
282
|
+
|
|
283
|
+
// Note: onNotification adds to a list, so we register before sending turn/start
|
|
284
|
+
client.onNotification('item/started', handleItemStarted);
|
|
285
|
+
client.onNotification('item/updated', handleItemUpdated);
|
|
286
|
+
client.onNotification('item/completed', handleItemCompleted);
|
|
287
|
+
client.onNotification('item/agentMessage/delta', handleAgentMessageDelta);
|
|
288
|
+
client.onNotification('turn/completed', handleTurnCompleted);
|
|
289
|
+
client.onNotification('turn/failed', handleTurnFailed);
|
|
290
|
+
client.onNotification('thread/tokenUsage/updated', handleTokenUsageUpdated);
|
|
291
|
+
|
|
292
|
+
// Phase 4: send turn/start
|
|
293
|
+
try {
|
|
294
|
+
await client.request<TurnStartResult>('turn/start', {
|
|
295
|
+
threadId,
|
|
296
|
+
input: [{ type: 'text', text: turn.user_prompt }],
|
|
297
|
+
approvalPolicy: 'never',
|
|
298
|
+
sandbox: { type: sandbox },
|
|
299
|
+
model: session.model ?? undefined,
|
|
300
|
+
effort: session.model_reasoning_effort ?? undefined,
|
|
301
|
+
});
|
|
302
|
+
|
|
303
|
+
// The turn runs asynchronously — events arrive via notifications
|
|
304
|
+
// If the process closes unexpectedly, handle cleanup
|
|
305
|
+
client.events.once('closed', () => {
|
|
306
|
+
const db = getDb();
|
|
307
|
+
const now = new Date().toISOString();
|
|
308
|
+
const currentTurn = db.prepare('SELECT status FROM turns WHERE id = ?').get(turn.id) as
|
|
309
|
+
| { status: string }
|
|
310
|
+
| undefined;
|
|
311
|
+
if (currentTurn && currentTurn.status === 'running') {
|
|
312
|
+
db.prepare('UPDATE turns SET status = ?, error = ?, updated_at = ? WHERE id = ?').run(
|
|
313
|
+
'failed',
|
|
314
|
+
'App-server process closed unexpectedly',
|
|
315
|
+
now,
|
|
316
|
+
turn.id,
|
|
317
|
+
);
|
|
318
|
+
setSessionStatus(db, session.id, 0, 'failed', now);
|
|
319
|
+
broadcast('turn:failed', {
|
|
320
|
+
sessionId: session.id,
|
|
321
|
+
turnId: turn.id,
|
|
322
|
+
error: 'App-server process closed unexpectedly',
|
|
323
|
+
});
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
} catch (err) {
|
|
327
|
+
const errMsg = (err as Error).message;
|
|
328
|
+
const now2 = new Date().toISOString();
|
|
329
|
+
db.prepare('UPDATE turns SET status = ?, error = ?, updated_at = ? WHERE id = ?').run(
|
|
330
|
+
'failed',
|
|
331
|
+
errMsg,
|
|
332
|
+
now2,
|
|
333
|
+
turn.id,
|
|
334
|
+
);
|
|
335
|
+
setSessionStatus(db, session.id, 0, 'failed', now2);
|
|
336
|
+
broadcast('turn:failed', { sessionId: session.id, turnId: turn.id, error: errMsg });
|
|
337
|
+
turnDone = true;
|
|
120
338
|
}
|
|
121
|
-
return false;
|
|
122
339
|
}
|
|
123
340
|
|
|
124
|
-
|
|
341
|
+
/** Handle an item notification — insert or update DB row and broadcast. */
|
|
342
|
+
function handleItemEvent(
|
|
343
|
+
params: ItemNotification,
|
|
344
|
+
sessionId: string,
|
|
345
|
+
turnId: string,
|
|
346
|
+
status: ItemStatus,
|
|
347
|
+
broadcastEvent: string,
|
|
348
|
+
): void {
|
|
125
349
|
const db = getDb();
|
|
126
350
|
const now = new Date().toISOString();
|
|
351
|
+
const item = params.item;
|
|
127
352
|
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
db.prepare('UPDATE sessions SET external_thread_id = ?, updated_at = ? WHERE id = ?')
|
|
131
|
-
.run(event.thread_id, now, sessionId);
|
|
132
|
-
break;
|
|
133
|
-
}
|
|
353
|
+
// Skip user messages — they are not agent output
|
|
354
|
+
if (shouldSkipItem(item.type)) return;
|
|
134
355
|
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
}
|
|
356
|
+
// Skip reasoning items with empty content — app-server sends summary: [], content: []
|
|
357
|
+
// These have no displayable text and would render as empty cards
|
|
358
|
+
if ((item.type === 'reasoning' || item.type === 'agentReasoning') && isEmptyReasoning(item)) return;
|
|
139
359
|
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
const itemId = uuidv4();
|
|
143
|
-
const data = mapItemData(item);
|
|
144
|
-
db.prepare(
|
|
145
|
-
'INSERT INTO items (id, turn_id, external_item_id, type, data, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
|
|
146
|
-
).run(itemId, turnId, item.id, item.type, JSON.stringify(data), 'in_progress', now, now);
|
|
360
|
+
const data = mapItemData(item);
|
|
361
|
+
const normalizedType = normalizeItemType(item.type);
|
|
147
362
|
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
363
|
+
// Try to update existing row
|
|
364
|
+
const result = db
|
|
365
|
+
.prepare('UPDATE items SET data = ?, status = ?, updated_at = ? WHERE external_item_id = ? AND turn_id = ?')
|
|
366
|
+
.run(JSON.stringify(data), status, now, item.id, turnId);
|
|
151
367
|
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
368
|
+
let itemId: string;
|
|
369
|
+
let createdAt: string;
|
|
370
|
+
|
|
371
|
+
if (result.changes === 0) {
|
|
372
|
+
// Item not in DB yet — insert
|
|
373
|
+
itemId = uuidv4();
|
|
374
|
+
createdAt = now;
|
|
375
|
+
db.prepare(
|
|
376
|
+
'INSERT INTO items (id, turn_id, external_item_id, type, data, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
|
|
377
|
+
).run(itemId, turnId, item.id, normalizedType as Item['type'], JSON.stringify(data), status, now, now);
|
|
378
|
+
} else {
|
|
379
|
+
const row = db
|
|
380
|
+
.prepare('SELECT id, created_at FROM items WHERE external_item_id = ? AND turn_id = ?')
|
|
381
|
+
.get(item.id, turnId) as { id: string; created_at: string };
|
|
382
|
+
itemId = row.id;
|
|
383
|
+
createdAt = row.created_at;
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
// Update turn-level fields on item completion
|
|
387
|
+
if (status === 'completed' || status === 'in_progress') {
|
|
388
|
+
if (item.type === 'agentMessage' || item.type === 'agent_message') {
|
|
389
|
+
db.prepare('UPDATE turns SET agent_response = ?, updated_at = ? WHERE id = ?').run(item.text, now, turnId);
|
|
390
|
+
}
|
|
391
|
+
if (item.type === 'reasoning' || item.type === 'agentReasoning') {
|
|
392
|
+
const summary = Array.isArray(item.summary) ? (item.summary as string[]).join('\n') : '';
|
|
393
|
+
const thoughts = summary || item.text || '';
|
|
394
|
+
if (thoughts) {
|
|
395
|
+
db.prepare('UPDATE turns SET thoughts = ?, updated_at = ? WHERE id = ?').run(thoughts, now, turnId);
|
|
171
396
|
}
|
|
172
|
-
break;
|
|
173
397
|
}
|
|
398
|
+
}
|
|
174
399
|
|
|
175
|
-
|
|
176
|
-
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
if (result.changes === 0) {
|
|
186
|
-
itemId = uuidv4();
|
|
187
|
-
createdAt = now;
|
|
188
|
-
db.prepare(
|
|
189
|
-
'INSERT INTO items (id, turn_id, external_item_id, type, data, status, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?)',
|
|
190
|
-
).run(itemId, turnId, item.id, item.type, JSON.stringify(data), status, now, now);
|
|
191
|
-
} else {
|
|
192
|
-
const row = db.prepare('SELECT id, created_at FROM items WHERE external_item_id = ? AND turn_id = ?')
|
|
193
|
-
.get(item.id, turnId) as { id: string; created_at: string };
|
|
194
|
-
itemId = row.id;
|
|
195
|
-
createdAt = row.created_at;
|
|
196
|
-
}
|
|
400
|
+
const wsItem: Item = {
|
|
401
|
+
id: itemId,
|
|
402
|
+
turn_id: turnId,
|
|
403
|
+
external_item_id: item.id,
|
|
404
|
+
type: normalizedType as Item['type'],
|
|
405
|
+
data,
|
|
406
|
+
status,
|
|
407
|
+
created_at: createdAt,
|
|
408
|
+
updated_at: now,
|
|
409
|
+
};
|
|
197
410
|
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
db.prepare('UPDATE turns SET agent_response = ?, updated_at = ? WHERE id = ?')
|
|
201
|
-
.run(item.text, now, turnId);
|
|
202
|
-
}
|
|
203
|
-
if (item.type === 'reasoning') {
|
|
204
|
-
const existing = db.prepare('SELECT thoughts FROM turns WHERE id = ?').get(turnId) as { thoughts: string | null };
|
|
205
|
-
const thoughts = existing.thoughts ? existing.thoughts + '\n' + item.text : item.text;
|
|
206
|
-
db.prepare('UPDATE turns SET thoughts = ?, updated_at = ? WHERE id = ?')
|
|
207
|
-
.run(thoughts, now, turnId);
|
|
208
|
-
}
|
|
411
|
+
broadcast(broadcastEvent, { sessionId, turnId, item: wsItem });
|
|
412
|
+
}
|
|
209
413
|
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
414
|
+
/** Cancel a running turn by session ID. Returns true if a turn was cancelled. */
|
|
415
|
+
export async function interruptTurn(sessionId: string): Promise<boolean> {
|
|
416
|
+
const entry = sessionMap.get(sessionId);
|
|
417
|
+
if (!entry || !entry.turnId) return false;
|
|
213
418
|
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
.run('active', now, sessionId);
|
|
220
|
-
broadcast('turn:completed', { sessionId, turnId, usage, hasUnreviewed: true });
|
|
221
|
-
|
|
222
|
-
// Auto-generate session title from first turn (if title is still null)
|
|
223
|
-
syncThreadTitle(sessionId);
|
|
224
|
-
break;
|
|
225
|
-
}
|
|
419
|
+
try {
|
|
420
|
+
await entry.client.request('turn/interrupt', {
|
|
421
|
+
threadId: entry.threadId,
|
|
422
|
+
turnId: entry.turnId,
|
|
423
|
+
});
|
|
226
424
|
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
|
|
230
|
-
|
|
231
|
-
|
|
232
|
-
|
|
233
|
-
break;
|
|
234
|
-
}
|
|
425
|
+
// Update DB and broadcast — the app-server may not send a turn/failed notification
|
|
426
|
+
const db = getDb();
|
|
427
|
+
const now = new Date().toISOString();
|
|
428
|
+
db.prepare('UPDATE turns SET status = ?, updated_at = ? WHERE id = ?').run('aborted', now, entry.turnId);
|
|
429
|
+
setSessionStatus(db, sessionId, 0, 'active', now);
|
|
430
|
+
broadcast('turn:cancelled', { sessionId, turnId: entry.turnId });
|
|
235
431
|
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
break;
|
|
243
|
-
}
|
|
432
|
+
// Clear turn ID so further notifications are ignored
|
|
433
|
+
entry.turnId = null;
|
|
434
|
+
|
|
435
|
+
return true;
|
|
436
|
+
} catch {
|
|
437
|
+
return false;
|
|
244
438
|
}
|
|
245
439
|
}
|
|
246
440
|
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
)
|
|
255
|
-
|
|
256
|
-
id,
|
|
257
|
-
turn_id: turnId,
|
|
258
|
-
external_item_id: sdkItem.id,
|
|
259
|
-
type: sdkItem.type,
|
|
260
|
-
data: mapItemData(sdkItem),
|
|
261
|
-
status,
|
|
262
|
-
created_at: createdAt,
|
|
263
|
-
updated_at: updatedAt,
|
|
264
|
-
};
|
|
441
|
+
/** Remove a session's app-server client from the in-memory map. Used after rewind/branch. */
|
|
442
|
+
export function clearThread(sessionId: string): void {
|
|
443
|
+
const entry = sessionMap.get(sessionId);
|
|
444
|
+
if (entry) {
|
|
445
|
+
entry.client.close().catch(() => {
|
|
446
|
+
// Ignore close errors
|
|
447
|
+
});
|
|
448
|
+
sessionMap.delete(sessionId);
|
|
449
|
+
}
|
|
265
450
|
}
|
|
266
451
|
|
|
452
|
+
/** Clear all session entries. Used in test cleanup. */
|
|
453
|
+
export function clearAllThreads(): void {
|
|
454
|
+
for (const [, entry] of sessionMap) {
|
|
455
|
+
entry.client.close().catch(() => {});
|
|
456
|
+
}
|
|
457
|
+
sessionMap.clear();
|
|
458
|
+
titleGenerating.clear();
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
// --- Title generation ---
|
|
462
|
+
|
|
267
463
|
/** Tracks sessions currently generating titles — prevents duplicate throwaway threads. */
|
|
268
464
|
const titleGenerating = new Set<string>();
|
|
269
465
|
|
|
270
|
-
/** Generate a title for the session using a throwaway
|
|
271
|
-
* Only runs once per session — guarded by title === null AND titleGenerating set. */
|
|
466
|
+
/** Generate a title for the session using a throwaway app-server thread. */
|
|
272
467
|
function syncThreadTitle(sessionId: string): void {
|
|
273
468
|
const db = getDb();
|
|
274
|
-
const session = db.prepare('SELECT title FROM sessions WHERE id = ?')
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
if (
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
const turn = db
|
|
469
|
+
const session = db.prepare('SELECT title FROM sessions WHERE id = ?').get(sessionId) as
|
|
470
|
+
| { title: string | null }
|
|
471
|
+
| undefined;
|
|
472
|
+
if (!session || session.title !== null) return;
|
|
473
|
+
if (titleGenerating.has(sessionId)) return;
|
|
474
|
+
|
|
475
|
+
const turn = db
|
|
476
|
+
.prepare('SELECT user_prompt, agent_response FROM turns WHERE session_id = ? ORDER BY created_at ASC LIMIT 1')
|
|
281
477
|
.get(sessionId) as { user_prompt: string; agent_response: string | null } | undefined;
|
|
282
478
|
if (!turn) return;
|
|
283
479
|
|
|
284
|
-
// Mark as generating — prevents concurrent title generation for same session
|
|
285
480
|
titleGenerating.add(sessionId);
|
|
286
|
-
|
|
287
|
-
// Fire-and-forget — don't block the turn:completed handler
|
|
288
481
|
generateTitleAsync(sessionId, turn.user_prompt, turn.agent_response).finally(() => {
|
|
289
482
|
titleGenerating.delete(sessionId);
|
|
290
483
|
});
|
|
291
484
|
}
|
|
292
485
|
|
|
293
|
-
/** Create a throwaway
|
|
294
|
-
async function generateTitleAsync(
|
|
295
|
-
|
|
486
|
+
/** Create a throwaway app-server thread, ask for a title, update the session. */
|
|
487
|
+
async function generateTitleAsync(
|
|
488
|
+
sessionId: string,
|
|
489
|
+
userPrompt: string,
|
|
490
|
+
agentResponse: string | null,
|
|
491
|
+
): Promise<void> {
|
|
492
|
+
let client: AppServerClient | null = null;
|
|
296
493
|
try {
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
494
|
+
client = new AppServerClient({ cwd: process.cwd() });
|
|
495
|
+
await client.initialize();
|
|
496
|
+
|
|
497
|
+
const initResult = await client.request<ThreadStartResult>('thread/start', {
|
|
498
|
+
cwd: process.cwd(),
|
|
499
|
+
approvalPolicy: 'never',
|
|
500
|
+
sandbox: 'read-only',
|
|
501
|
+
});
|
|
502
|
+
const threadId = initResult.thread.id;
|
|
300
503
|
|
|
301
504
|
const truncatedPrompt = userPrompt.slice(0, 500);
|
|
302
505
|
const truncatedResponse = (agentResponse || '').slice(0, 500);
|
|
303
|
-
|
|
304
506
|
const titlePrompt = `Generate a concise 3-5 word title for this conversation. Reply with only the title, nothing else.\n\nUser asked: "${truncatedPrompt}"\n\nAgent responded: "${truncatedResponse}"`;
|
|
305
507
|
|
|
306
|
-
|
|
307
|
-
|
|
508
|
+
// Set up promise to capture the title response
|
|
509
|
+
let titleResolve!: (value: string) => void;
|
|
510
|
+
let titleReject!: (error: Error) => void;
|
|
511
|
+
const titlePromise = new Promise<string>((resolve, reject) => {
|
|
512
|
+
titleResolve = resolve;
|
|
513
|
+
titleReject = reject;
|
|
514
|
+
});
|
|
515
|
+
|
|
516
|
+
// Capture agent_message text from item/completed
|
|
517
|
+
let titleResponse = '';
|
|
518
|
+
client.onNotification('item/completed', (params: unknown) => {
|
|
519
|
+
const p = params as ItemNotification;
|
|
520
|
+
if (p.threadId !== threadId) return;
|
|
521
|
+
if ((p.item.type === 'agentMessage' || p.item.type === 'agent_message') && p.item.text) {
|
|
522
|
+
titleResponse = p.item.text;
|
|
523
|
+
}
|
|
524
|
+
});
|
|
525
|
+
|
|
526
|
+
// Resolve on turn/completed
|
|
527
|
+
client.onNotification('turn/completed', (params: unknown) => {
|
|
528
|
+
const p = params as { threadId: string };
|
|
529
|
+
if (p.threadId !== threadId) return;
|
|
530
|
+
titleResolve(titleResponse);
|
|
531
|
+
});
|
|
532
|
+
|
|
533
|
+
// Reject on turn/failed
|
|
534
|
+
client.onNotification('turn/failed', (params: unknown) => {
|
|
535
|
+
const p = params as TurnFailedNotification;
|
|
536
|
+
if (p.threadId !== threadId) return;
|
|
537
|
+
titleReject(new Error(p.error.message));
|
|
538
|
+
});
|
|
308
539
|
|
|
309
|
-
|
|
540
|
+
// Set a timeout (30s)
|
|
541
|
+
const timeout = setTimeout(() => {
|
|
542
|
+
titleReject(new Error('Title generation timed out'));
|
|
543
|
+
}, 30_000);
|
|
544
|
+
|
|
545
|
+
// Send turn/start — events arrive via notifications
|
|
546
|
+
await client.request<TurnStartResult>('turn/start', {
|
|
547
|
+
threadId,
|
|
548
|
+
input: [{ type: 'text', text: titlePrompt }],
|
|
549
|
+
approvalPolicy: 'never',
|
|
550
|
+
sandbox: { type: 'read-only' },
|
|
551
|
+
});
|
|
552
|
+
|
|
553
|
+
const title = await titlePromise;
|
|
554
|
+
clearTimeout(timeout);
|
|
555
|
+
|
|
556
|
+
const cleanTitle = title.trim().replace(/^["']|["']$/g, '').slice(0, 100);
|
|
557
|
+
|
|
558
|
+
if (cleanTitle) {
|
|
310
559
|
const db = getDb();
|
|
311
560
|
const now = new Date().toISOString();
|
|
312
|
-
db.prepare('UPDATE sessions SET title = ?, updated_at = ? WHERE id = ?').run(
|
|
313
|
-
broadcast('session:title', { sessionId, title });
|
|
561
|
+
db.prepare('UPDATE sessions SET title = ?, updated_at = ? WHERE id = ?').run(cleanTitle, now, sessionId);
|
|
562
|
+
broadcast('session:title', { sessionId, title: cleanTitle });
|
|
314
563
|
}
|
|
315
564
|
} catch {
|
|
316
565
|
// Silently skip on error — session keeps null title
|
|
317
566
|
} finally {
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
cleanupThrowawayThread(throwawayThreadId);
|
|
567
|
+
if (client) {
|
|
568
|
+
await client.close().catch(() => {});
|
|
321
569
|
}
|
|
322
570
|
}
|
|
323
571
|
}
|
|
324
572
|
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
573
|
+
// --- Provider interface ---
|
|
574
|
+
|
|
575
|
+
/** Map SDK effort values to UI labels. */
|
|
576
|
+
const EFFORT_LABELS: Record<string, string> = {
|
|
577
|
+
minimal: 'Minimal',
|
|
578
|
+
low: 'Low',
|
|
579
|
+
medium: 'Medium',
|
|
580
|
+
high: 'High',
|
|
581
|
+
xhigh: 'Extra High',
|
|
582
|
+
max: 'Max',
|
|
583
|
+
ultra: 'Ultra',
|
|
584
|
+
};
|
|
585
|
+
|
|
586
|
+
/** Fetch available models from the shared app-server and map to ModelInfo[]. */
|
|
587
|
+
async function getModels(): Promise<ModelInfo[]> {
|
|
330
588
|
try {
|
|
331
|
-
const
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
}
|
|
589
|
+
const result = await getSharedAppServer().request<ModelListResult>('model/list', {});
|
|
590
|
+
return (result.data ?? []).map((m) => ({
|
|
591
|
+
slug: m.id,
|
|
592
|
+
displayName: m.displayName,
|
|
593
|
+
description: m.description,
|
|
594
|
+
defaultReasoning: m.defaultReasoningEffort,
|
|
595
|
+
reasoningLevels: (m.supportedReasoningEfforts ?? []).map((r) => ({
|
|
596
|
+
effort: r.reasoningEffort,
|
|
597
|
+
label: EFFORT_LABELS[r.reasoningEffort] ?? r.reasoningEffort,
|
|
598
|
+
})),
|
|
599
|
+
}));
|
|
343
600
|
} catch {
|
|
344
|
-
|
|
601
|
+
return [];
|
|
345
602
|
}
|
|
603
|
+
}
|
|
346
604
|
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
351
|
-
|
|
352
|
-
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
|
|
357
|
-
|
|
358
|
-
|
|
359
|
-
|
|
360
|
-
|
|
361
|
-
|
|
362
|
-
}
|
|
363
|
-
}
|
|
605
|
+
/** Graceful shutdown — close shared metadata process and all per-session clients. */
|
|
606
|
+
async function shutdown(): Promise<void> {
|
|
607
|
+
await closeSharedAppServer();
|
|
608
|
+
clearAllThreads();
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
/** Codex provider — implements RuntimeProvider interface. */
|
|
612
|
+
export const codexProvider: RuntimeProvider = {
|
|
613
|
+
startTurn,
|
|
614
|
+
interruptTurn,
|
|
615
|
+
clearThread,
|
|
616
|
+
clearAllThreads,
|
|
617
|
+
getModels,
|
|
618
|
+
shutdown,
|
|
619
|
+
};
|