repoburg 1.3.199 → 1.3.201
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/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 +1425 -272
- package/agent-pane/backend/src/claude/claude-provider.test.ts +393 -3
- package/agent-pane/backend/src/claude/claude-provider.ts +53 -50
- package/agent-pane/backend/src/db/index.ts +6 -0
- package/agent-pane/backend/src/index.ts +5 -2
- package/agent-pane/backend/src/integration/threads-claude-fork.test.ts +241 -0
- package/agent-pane/backend/src/providers/index.ts +3 -1
- package/agent-pane/backend/src/repoburg/action-mapping.ts +216 -0
- package/agent-pane/backend/src/repoburg/agent-runtime-client.test.ts +271 -0
- package/agent-pane/backend/src/repoburg/agent-runtime-client.ts +262 -0
- package/agent-pane/backend/src/repoburg/backend-resolver.test.ts +271 -0
- package/agent-pane/backend/src/repoburg/backend-resolver.ts +246 -0
- package/agent-pane/backend/src/repoburg/repoburg-client.test.ts +159 -0
- package/agent-pane/backend/src/repoburg/repoburg-client.ts +181 -0
- package/agent-pane/backend/src/repoburg/repoburg-provider.test.ts +723 -0
- package/agent-pane/backend/src/repoburg/repoburg-provider.ts +422 -0
- package/agent-pane/backend/src/repoburg/turn-projector.test.ts +475 -0
- package/agent-pane/backend/src/repoburg/turn-projector.ts +537 -0
- package/agent-pane/backend/src/routes/providers.test.ts +18 -1
- package/agent-pane/backend/src/routes/providers.ts +19 -1
- package/agent-pane/backend/src/routes/sessions.ts +12 -0
- package/agent-pane/backend/src/routes/threads.ts +21 -6
- package/agent-pane/backend/src/routes/turns.test.ts +45 -1
- package/agent-pane/frontend/dist/assets/{index-Dwwhx7YN.css → index-34WaZYsW.css} +1 -1
- package/agent-pane/frontend/dist/assets/index-y7xlT86e.js +362 -0
- package/agent-pane/frontend/dist/index.html +2 -2
- package/agent-pane/frontend/src/components/CommentableText.test.tsx +196 -0
- package/agent-pane/frontend/src/components/CommentableText.tsx +109 -21
- package/agent-pane/frontend/src/components/IdeWorkspace.test.tsx +39 -0
- package/agent-pane/frontend/src/components/IdeWorkspace.tsx +28 -2
- package/agent-pane/frontend/src/components/ResizeHandle.tsx +29 -0
- package/agent-pane/frontend/src/components/panels/ThreadsPanel.tsx +35 -3
- package/agent-pane/frontend/src/constants/models.ts +37 -0
- package/agent-pane/frontend/src/hooks/useResizable.test.ts +97 -0
- package/agent-pane/frontend/src/hooks/useResizable.ts +75 -0
- package/agent-pane/frontend/src/store/store.test.ts +35 -0
- package/agent-pane/frontend/src/store/uiSlice.ts +50 -0
- package/agent-pane/frontend/src/styles/app.css +10 -1
- package/agent-pane/frontend/src/test/component-helpers.ts +2 -0
- package/agent-pane-bug-repro-no-agent-reply.png +0 -0
- package/backend/dist/src/agent-runtime/agent-runtime-event-emitter.d.ts +20 -0
- package/backend/dist/src/agent-runtime/agent-runtime-event-emitter.js +112 -0
- package/backend/dist/src/agent-runtime/agent-runtime-event-emitter.js.map +1 -0
- package/backend/dist/src/agent-runtime/agent-runtime.controller.d.ts +11 -0
- package/backend/dist/src/agent-runtime/agent-runtime.controller.js +91 -0
- package/backend/dist/src/agent-runtime/agent-runtime.controller.js.map +1 -0
- package/backend/dist/src/agent-runtime/agent-runtime.module.d.ts +2 -0
- package/backend/dist/src/agent-runtime/agent-runtime.module.js +26 -0
- package/backend/dist/src/agent-runtime/agent-runtime.module.js.map +1 -0
- package/backend/dist/src/agent-runtime/agent-runtime.types.d.ts +77 -0
- package/backend/dist/src/agent-runtime/agent-runtime.types.js +3 -0
- package/backend/dist/src/agent-runtime/agent-runtime.types.js.map +1 -0
- package/backend/dist/src/app.module.js +4 -0
- package/backend/dist/src/app.module.js.map +1 -1
- package/backend/dist/src/interactive-chat/chat.module.js +4 -0
- package/backend/dist/src/interactive-chat/chat.module.js.map +1 -1
- package/backend/dist/src/interactive-chat/chat.service.d.ts +3 -1
- package/backend/dist/src/interactive-chat/chat.service.js +33 -6
- package/backend/dist/src/interactive-chat/chat.service.js.map +1 -1
- package/backend/dist/src/llm-orchestration/hooks/frontend-notification.hook.d.ts +3 -1
- package/backend/dist/src/llm-orchestration/hooks/frontend-notification.hook.js +20 -2
- package/backend/dist/src/llm-orchestration/hooks/frontend-notification.hook.js.map +1 -1
- package/backend/dist/src/llm-orchestration/llm-orchestration.module.js +2 -0
- package/backend/dist/src/llm-orchestration/llm-orchestration.module.js.map +1 -1
- package/backend/dist/src/llm-orchestration/llm-turn-processor.service.d.ts +4 -1
- package/backend/dist/src/llm-orchestration/llm-turn-processor.service.js +35 -2
- package/backend/dist/src/llm-orchestration/llm-turn-processor.service.js.map +1 -1
- package/backend/dist/src/llm-provider/ollama-llm.provider.d.ts +1 -0
- package/backend/dist/src/llm-provider/ollama-llm.provider.js +19 -1
- package/backend/dist/src/llm-provider/ollama-llm.provider.js.map +1 -1
- package/backend/dist/src/llm-responses/llm-responses.module.js +4 -0
- package/backend/dist/src/llm-responses/llm-responses.module.js.map +1 -1
- package/backend/dist/src/llm-responses/llm-responses.service.d.ts +4 -1
- package/backend/dist/src/llm-responses/llm-responses.service.js +52 -2
- package/backend/dist/src/llm-responses/llm-responses.service.js.map +1 -1
- package/backend/dist/tsconfig.build.tsbuildinfo +1 -1
- package/daemon/dist/index.js +0 -0
- package/package.json +1 -1
- package/agent-pane/frontend/dist/assets/index-BYo-lnxm.js +0 -362
|
@@ -0,0 +1,537 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Turn projector — maps the agent-runtime message stream (Claude-Code-style)
|
|
3
|
+
* onto agent-pane items and turn lifecycle.
|
|
4
|
+
*
|
|
5
|
+
* Projection (idempotent upsert by external ids):
|
|
6
|
+
* - stream_event(text/thinking) → agent_message / reasoning items (live deltas)
|
|
7
|
+
* - assistant → completes the round's text/thoughts items
|
|
8
|
+
* - user(tool_result) → file_change / command_execution / mcp_tool_call
|
|
9
|
+
* / web_search items from the action data
|
|
10
|
+
* - result → terminal: turn completed / failed / cancelled
|
|
11
|
+
*
|
|
12
|
+
* Watch-dog (Claude Code stream-watchdog analog): no `result` within
|
|
13
|
+
* RESULT_TIMEOUT after the last MESSAGE (heartbeats do not reset it) →
|
|
14
|
+
* HTTP snapshot reconcile (GET /sessions/:id/inputs incl. aiActions) fills
|
|
15
|
+
* missing rounds; a still-running session re-arms the watchdog up to
|
|
16
|
+
* MAX_RESULT_EXTENSIONS times, then the turn fails with a timeout error.
|
|
17
|
+
*/
|
|
18
|
+
|
|
19
|
+
import { getDb } from '../db/index.ts';
|
|
20
|
+
import { broadcast } from '../ws/index.ts';
|
|
21
|
+
import type { Item } from '../db/schema.ts';
|
|
22
|
+
import type { RepoburgRuntimeClient, RepoburgSessionInput } from './repoburg-client.ts';
|
|
23
|
+
import {
|
|
24
|
+
completeItem,
|
|
25
|
+
insertItem,
|
|
26
|
+
mapActionToItem,
|
|
27
|
+
mapUsage,
|
|
28
|
+
setSessionStatus,
|
|
29
|
+
} from './action-mapping.ts';
|
|
30
|
+
import type {
|
|
31
|
+
AgentRuntimeMessage,
|
|
32
|
+
AssistantMessage,
|
|
33
|
+
ResultMessage,
|
|
34
|
+
StreamEventMessage,
|
|
35
|
+
UserMessage,
|
|
36
|
+
} from './agent-runtime-client.ts';
|
|
37
|
+
|
|
38
|
+
/** No result message within this window after the last message → reconcile. */
|
|
39
|
+
export const RESULT_TIMEOUT_MS = 15 * 60_000;
|
|
40
|
+
/** Reconcile extensions while the backend reports the session still running. */
|
|
41
|
+
export const MAX_RESULT_EXTENSIONS = 3;
|
|
42
|
+
|
|
43
|
+
/** Per-round projected state. */
|
|
44
|
+
interface RoundInfo {
|
|
45
|
+
messageItemId: string | null;
|
|
46
|
+
reasoningItemId: string | null;
|
|
47
|
+
messageExternalId: string;
|
|
48
|
+
reasoningExternalId: string;
|
|
49
|
+
text: string;
|
|
50
|
+
thoughts: string;
|
|
51
|
+
finalPlain: string | null;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export interface ProjectorDeps {
|
|
55
|
+
agentPaneSessionId: string;
|
|
56
|
+
turnId: string;
|
|
57
|
+
repoburgSessionId: string;
|
|
58
|
+
client: RepoburgRuntimeClient;
|
|
59
|
+
/** Optional hook: called once when the turn reaches a terminal state. */
|
|
60
|
+
onFinished?: () => void;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
export class TurnProjector {
|
|
64
|
+
private readonly rounds = new Map<string, RoundInfo>();
|
|
65
|
+
private done = false;
|
|
66
|
+
private resultTimer: ReturnType<typeof setTimeout> | null = null;
|
|
67
|
+
private extensions = 0;
|
|
68
|
+
|
|
69
|
+
constructor(private readonly deps: ProjectorDeps) {
|
|
70
|
+
this.armResultWatchdog();
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
get isDone(): boolean {
|
|
74
|
+
return this.done;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
/** Main entry — dispatch one agent-runtime message. */
|
|
78
|
+
handle(message: AgentRuntimeMessage): void {
|
|
79
|
+
if (this.done) return;
|
|
80
|
+
switch (message.type) {
|
|
81
|
+
case 'system':
|
|
82
|
+
break; // init — connection marker only
|
|
83
|
+
case 'stream_event':
|
|
84
|
+
this.handleStreamEvent(message);
|
|
85
|
+
break;
|
|
86
|
+
case 'assistant':
|
|
87
|
+
this.handleAssistant(message);
|
|
88
|
+
break;
|
|
89
|
+
case 'user':
|
|
90
|
+
this.handleUser(message);
|
|
91
|
+
break;
|
|
92
|
+
case 'result':
|
|
93
|
+
this.handleResult(message);
|
|
94
|
+
break;
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Reset the result watchdog (any MESSAGE resets it; heartbeats do not). */
|
|
99
|
+
touch(): void {
|
|
100
|
+
if (this.done) return;
|
|
101
|
+
this.armResultWatchdog();
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/** Stop the projector (turn finalized externally). Clears timers. */
|
|
105
|
+
close(): void {
|
|
106
|
+
this.clearWatchdog();
|
|
107
|
+
this.done = true;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
// -------------------------------------------------------------------------
|
|
111
|
+
// Message handlers
|
|
112
|
+
// -------------------------------------------------------------------------
|
|
113
|
+
|
|
114
|
+
private round(inputId: string): RoundInfo {
|
|
115
|
+
let info = this.rounds.get(inputId);
|
|
116
|
+
if (!info) {
|
|
117
|
+
info = {
|
|
118
|
+
messageItemId: null,
|
|
119
|
+
reasoningItemId: null,
|
|
120
|
+
messageExternalId: `${inputId}-text`,
|
|
121
|
+
reasoningExternalId: `${inputId}-thoughts`,
|
|
122
|
+
text: '',
|
|
123
|
+
thoughts: '',
|
|
124
|
+
finalPlain: null,
|
|
125
|
+
};
|
|
126
|
+
this.rounds.set(inputId, info);
|
|
127
|
+
}
|
|
128
|
+
return info;
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
private handleStreamEvent(msg: StreamEventMessage): void {
|
|
132
|
+
const db = getDb();
|
|
133
|
+
const info = this.round(msg.inputId);
|
|
134
|
+
const kind = msg.delta.type === 'thinking' ? 'thoughts' : 'text';
|
|
135
|
+
|
|
136
|
+
if (kind === 'text') info.text += msg.delta.text;
|
|
137
|
+
else info.thoughts += msg.delta.text;
|
|
138
|
+
|
|
139
|
+
const externalId = kind === 'text' ? info.messageExternalId : info.reasoningExternalId;
|
|
140
|
+
const type: Item['type'] = kind === 'text' ? 'agent_message' : 'reasoning';
|
|
141
|
+
const itemId = kind === 'text' ? info.messageItemId : info.reasoningItemId;
|
|
142
|
+
|
|
143
|
+
if (!itemId) {
|
|
144
|
+
const item = insertItem(
|
|
145
|
+
db,
|
|
146
|
+
this.deps.agentPaneSessionId,
|
|
147
|
+
this.deps.turnId,
|
|
148
|
+
externalId,
|
|
149
|
+
type,
|
|
150
|
+
{ text: kind === 'text' ? info.text : info.thoughts },
|
|
151
|
+
'in_progress',
|
|
152
|
+
'item:started',
|
|
153
|
+
);
|
|
154
|
+
if (kind === 'text') info.messageItemId = item.id;
|
|
155
|
+
else info.reasoningItemId = item.id;
|
|
156
|
+
} else {
|
|
157
|
+
// Subsequent chunks stream as deltas (frontend matches by external id).
|
|
158
|
+
broadcast('item:delta', {
|
|
159
|
+
sessionId: this.deps.agentPaneSessionId,
|
|
160
|
+
turnId: this.deps.turnId,
|
|
161
|
+
itemId: externalId,
|
|
162
|
+
delta: msg.delta.text,
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
this.touch();
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
private handleAssistant(msg: AssistantMessage): void {
|
|
169
|
+
const info = this.round(msg.inputId);
|
|
170
|
+
|
|
171
|
+
// The assistant message is the AUTHORITATIVE round content — replace the
|
|
172
|
+
// accumulated deltas (they may be partial when deltas were off/missed).
|
|
173
|
+
const text = msg.content
|
|
174
|
+
.filter((b) => b.type === 'text')
|
|
175
|
+
.map((b) => (b as { text: string }).text)
|
|
176
|
+
.join('');
|
|
177
|
+
const thinking = msg.content
|
|
178
|
+
.filter((b) => b.type === 'thinking')
|
|
179
|
+
.map((b) => (b as { thinking: string }).thinking)
|
|
180
|
+
.join('');
|
|
181
|
+
if (text) info.text = text;
|
|
182
|
+
if (thinking) info.thoughts = thinking;
|
|
183
|
+
|
|
184
|
+
// Complete (or create) the round's items — deltas may have been off/missed.
|
|
185
|
+
this.upsertCompletedTextItem(info, 'message', info.text);
|
|
186
|
+
this.upsertCompletedTextItem(info, 'reasoning', info.thoughts);
|
|
187
|
+
this.touch();
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/** Complete a round's text/thoughts item — creates it when deltas were
|
|
191
|
+
* off or missed, otherwise completes the streamed in_progress item. */
|
|
192
|
+
private upsertCompletedTextItem(
|
|
193
|
+
info: RoundInfo,
|
|
194
|
+
kind: 'message' | 'reasoning',
|
|
195
|
+
text: string,
|
|
196
|
+
): void {
|
|
197
|
+
if (!text) return;
|
|
198
|
+
const db = getDb();
|
|
199
|
+
const externalId = kind === 'message' ? info.messageExternalId : info.reasoningExternalId;
|
|
200
|
+
const itemId = kind === 'message' ? info.messageItemId : info.reasoningItemId;
|
|
201
|
+
const type: Item['type'] = kind === 'message' ? 'agent_message' : 'reasoning';
|
|
202
|
+
|
|
203
|
+
if (itemId) {
|
|
204
|
+
const itemRow = db.prepare('SELECT * FROM items WHERE id = ?').get(itemId) as
|
|
205
|
+
| Item
|
|
206
|
+
| undefined;
|
|
207
|
+
if (itemRow) {
|
|
208
|
+
completeItem(db, this.deps.agentPaneSessionId, this.deps.turnId, itemRow, { text }, 'completed');
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
// Idempotent: skip when an item with this external id already exists
|
|
213
|
+
// (e.g. replayed after reconnect).
|
|
214
|
+
const existing = db
|
|
215
|
+
.prepare('SELECT id FROM items WHERE turn_id = ? AND external_item_id = ?')
|
|
216
|
+
.get(this.deps.turnId, externalId);
|
|
217
|
+
if (existing) return;
|
|
218
|
+
const item = insertItem(
|
|
219
|
+
db,
|
|
220
|
+
this.deps.agentPaneSessionId,
|
|
221
|
+
this.deps.turnId,
|
|
222
|
+
externalId,
|
|
223
|
+
type,
|
|
224
|
+
{ text },
|
|
225
|
+
'completed',
|
|
226
|
+
'item:completed',
|
|
227
|
+
);
|
|
228
|
+
if (kind === 'message') info.messageItemId = item.id;
|
|
229
|
+
else info.reasoningItemId = item.id;
|
|
230
|
+
}
|
|
231
|
+
|
|
232
|
+
private handleUser(msg: UserMessage): void {
|
|
233
|
+
const db = getDb();
|
|
234
|
+
for (const block of msg.content) {
|
|
235
|
+
const info = this.round(msg.inputId);
|
|
236
|
+
// Synthesize the RepoburgAction shape from the tool_result payload —
|
|
237
|
+
// failed actions carry their text as the error, successful as output.
|
|
238
|
+
const action = {
|
|
239
|
+
...block.action,
|
|
240
|
+
executionLogs: [
|
|
241
|
+
{
|
|
242
|
+
output: block.is_error ? '' : block.content,
|
|
243
|
+
error_message: block.is_error ? block.content : null,
|
|
244
|
+
},
|
|
245
|
+
],
|
|
246
|
+
};
|
|
247
|
+
if (action.action_type === 'final') info.finalPlain = action.plain ?? null;
|
|
248
|
+
|
|
249
|
+
const externalId = `${msg.inputId}-action-${action.id}`;
|
|
250
|
+
const mapped = mapActionToItem(action);
|
|
251
|
+
if (!mapped) continue;
|
|
252
|
+
|
|
253
|
+
// Idempotent upsert — skip when already projected (reconnect replay
|
|
254
|
+
// or snapshot overlap).
|
|
255
|
+
const existing = db
|
|
256
|
+
.prepare('SELECT id FROM items WHERE turn_id = ? AND external_item_id = ?')
|
|
257
|
+
.get(this.deps.turnId, externalId);
|
|
258
|
+
if (existing) continue;
|
|
259
|
+
|
|
260
|
+
insertItem(
|
|
261
|
+
db,
|
|
262
|
+
this.deps.agentPaneSessionId,
|
|
263
|
+
this.deps.turnId,
|
|
264
|
+
externalId,
|
|
265
|
+
mapped.type,
|
|
266
|
+
mapped.data,
|
|
267
|
+
mapped.status,
|
|
268
|
+
'item:completed',
|
|
269
|
+
);
|
|
270
|
+
}
|
|
271
|
+
this.touch();
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
private handleResult(msg: ResultMessage): void {
|
|
275
|
+
switch (msg.subtype) {
|
|
276
|
+
case 'success':
|
|
277
|
+
this.finishSuccess(msg);
|
|
278
|
+
break;
|
|
279
|
+
case 'aborted':
|
|
280
|
+
this.finishAborted();
|
|
281
|
+
break;
|
|
282
|
+
case 'error_during_execution':
|
|
283
|
+
default:
|
|
284
|
+
this.finishFailed((msg.errors ?? []).join('; ') || 'Agent turn failed.');
|
|
285
|
+
break;
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
// -------------------------------------------------------------------------
|
|
290
|
+
// Terminal states
|
|
291
|
+
// -------------------------------------------------------------------------
|
|
292
|
+
|
|
293
|
+
private finishSuccess(msg: ResultMessage): void {
|
|
294
|
+
const db = getDb();
|
|
295
|
+
const now = new Date().toISOString();
|
|
296
|
+
const lastRound = [...this.rounds.values()].pop();
|
|
297
|
+
|
|
298
|
+
const responseText = msg.result || lastRound?.text || lastRound?.finalPlain || '';
|
|
299
|
+
const usage = msg.usage
|
|
300
|
+
? mapUsage({
|
|
301
|
+
input: msg.usage.input_tokens,
|
|
302
|
+
cached: msg.usage.cache_read_input_tokens,
|
|
303
|
+
output: msg.usage.output_tokens,
|
|
304
|
+
})
|
|
305
|
+
: null;
|
|
306
|
+
|
|
307
|
+
this.closeOpenItems('completed');
|
|
308
|
+
db.prepare(
|
|
309
|
+
'UPDATE turns SET status = ?, agent_response = ?, token_usage = ?, updated_at = ? WHERE id = ?',
|
|
310
|
+
).run('completed', responseText || null, usage ? JSON.stringify(usage) : null, now, this.deps.turnId);
|
|
311
|
+
setSessionStatus(db, this.deps.agentPaneSessionId, 0, 'active', now, true);
|
|
312
|
+
|
|
313
|
+
broadcast('turn:completed', {
|
|
314
|
+
sessionId: this.deps.agentPaneSessionId,
|
|
315
|
+
turnId: this.deps.turnId,
|
|
316
|
+
...(usage ? { usage } : {}),
|
|
317
|
+
hasUnreviewed: true,
|
|
318
|
+
});
|
|
319
|
+
void this.syncTitle();
|
|
320
|
+
this.finalize();
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
/** Fire-and-forget: copy the runtime session title once (new sessions). */
|
|
324
|
+
private async syncTitle(): Promise<void> {
|
|
325
|
+
try {
|
|
326
|
+
const db = getDb();
|
|
327
|
+
const session = db.prepare('SELECT title FROM sessions WHERE id = ?').get(
|
|
328
|
+
this.deps.agentPaneSessionId,
|
|
329
|
+
) as { title: string | null } | undefined;
|
|
330
|
+
if (!session || session.title) return;
|
|
331
|
+
|
|
332
|
+
const runtimeSession = await this.deps.client.getSession(this.deps.repoburgSessionId);
|
|
333
|
+
const title = (runtimeSession.session_title || '').trim();
|
|
334
|
+
if (!title) return;
|
|
335
|
+
|
|
336
|
+
const now = new Date().toISOString();
|
|
337
|
+
db.prepare('UPDATE sessions SET title = ?, updated_at = ? WHERE id = ?').run(
|
|
338
|
+
title,
|
|
339
|
+
now,
|
|
340
|
+
this.deps.agentPaneSessionId,
|
|
341
|
+
);
|
|
342
|
+
broadcast('session:title', { sessionId: this.deps.agentPaneSessionId, title });
|
|
343
|
+
} catch {
|
|
344
|
+
// Title sync is best-effort.
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
|
|
348
|
+
private finishFailed(error: string): void {
|
|
349
|
+
const db = getDb();
|
|
350
|
+
const now = new Date().toISOString();
|
|
351
|
+
this.closeOpenItems('failed');
|
|
352
|
+
db.prepare('UPDATE turns SET status = ?, error = ?, updated_at = ? WHERE id = ?').run(
|
|
353
|
+
'failed',
|
|
354
|
+
error,
|
|
355
|
+
now,
|
|
356
|
+
this.deps.turnId,
|
|
357
|
+
);
|
|
358
|
+
setSessionStatus(db, this.deps.agentPaneSessionId, 0, 'failed', now);
|
|
359
|
+
broadcast('turn:failed', {
|
|
360
|
+
sessionId: this.deps.agentPaneSessionId,
|
|
361
|
+
turnId: this.deps.turnId,
|
|
362
|
+
error,
|
|
363
|
+
});
|
|
364
|
+
this.finalize();
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
private finishAborted(): void {
|
|
368
|
+
const db = getDb();
|
|
369
|
+
const now = new Date().toISOString();
|
|
370
|
+
this.closeOpenItems('completed');
|
|
371
|
+
db.prepare('UPDATE turns SET status = ?, updated_at = ? WHERE id = ?').run(
|
|
372
|
+
'aborted',
|
|
373
|
+
now,
|
|
374
|
+
this.deps.turnId,
|
|
375
|
+
);
|
|
376
|
+
setSessionStatus(db, this.deps.agentPaneSessionId, 0, 'active', now);
|
|
377
|
+
broadcast('turn:cancelled', {
|
|
378
|
+
sessionId: this.deps.agentPaneSessionId,
|
|
379
|
+
turnId: this.deps.turnId,
|
|
380
|
+
});
|
|
381
|
+
this.finalize();
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
private finalize(): void {
|
|
385
|
+
this.clearWatchdog();
|
|
386
|
+
this.done = true;
|
|
387
|
+
this.deps.onFinished?.();
|
|
388
|
+
}
|
|
389
|
+
|
|
390
|
+
/** Close every still in_progress item of this turn with the given status. */
|
|
391
|
+
private closeOpenItems(status: Item['status']): void {
|
|
392
|
+
const db = getDb();
|
|
393
|
+
const now = new Date().toISOString();
|
|
394
|
+
db.prepare('UPDATE items SET status = ?, updated_at = ? WHERE turn_id = ? AND status = ?').run(
|
|
395
|
+
status,
|
|
396
|
+
now,
|
|
397
|
+
this.deps.turnId,
|
|
398
|
+
'in_progress',
|
|
399
|
+
);
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
// -------------------------------------------------------------------------
|
|
403
|
+
// Watch-dog + snapshot reconcile
|
|
404
|
+
// -------------------------------------------------------------------------
|
|
405
|
+
|
|
406
|
+
private armResultWatchdog(): void {
|
|
407
|
+
if (this.done) return;
|
|
408
|
+
this.clearWatchdog();
|
|
409
|
+
this.resultTimer = setTimeout(() => {
|
|
410
|
+
void this.reconcileFromSnapshot();
|
|
411
|
+
}, RESULT_TIMEOUT_MS);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
private clearWatchdog(): void {
|
|
415
|
+
if (this.resultTimer) {
|
|
416
|
+
clearTimeout(this.resultTimer);
|
|
417
|
+
this.resultTimer = null;
|
|
418
|
+
}
|
|
419
|
+
}
|
|
420
|
+
|
|
421
|
+
/** Snapshot reconcile: project missing rounds from the runtime's HTTP
|
|
422
|
+
* snapshot, then complete the turn when the session stopped running. */
|
|
423
|
+
private async reconcileFromSnapshot(): Promise<void> {
|
|
424
|
+
if (this.done) return;
|
|
425
|
+
const { client, repoburgSessionId } = this.deps;
|
|
426
|
+
try {
|
|
427
|
+
const [inputs, session] = await Promise.all([
|
|
428
|
+
client.getSessionInputs(repoburgSessionId),
|
|
429
|
+
client.getSession(repoburgSessionId),
|
|
430
|
+
]);
|
|
431
|
+
if (this.done) return; // A result arrived while fetching.
|
|
432
|
+
|
|
433
|
+
for (const input of inputs) {
|
|
434
|
+
if (input.role === 'model') this.projectSnapshotRound(input);
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
if (!session.is_running) {
|
|
438
|
+
// The loop is over — finish from the snapshot's last round.
|
|
439
|
+
const modelInputs = inputs.filter((i) => i.role === 'model');
|
|
440
|
+
const last = modelInputs[modelInputs.length - 1];
|
|
441
|
+
const lastRound = [...this.rounds.values()].pop();
|
|
442
|
+
const responseText =
|
|
443
|
+
lastRound?.text ||
|
|
444
|
+
lastRound?.finalPlain ||
|
|
445
|
+
extractFinalPlain(last) ||
|
|
446
|
+
last?.raw_llm_response ||
|
|
447
|
+
'';
|
|
448
|
+
const usage = last
|
|
449
|
+
? mapUsage({
|
|
450
|
+
input: last.input_token_count ?? 0,
|
|
451
|
+
cached: last.cached_token_count ?? 0,
|
|
452
|
+
output: last.output_token_count ?? 0,
|
|
453
|
+
})
|
|
454
|
+
: null;
|
|
455
|
+
const db = getDb();
|
|
456
|
+
const now = new Date().toISOString();
|
|
457
|
+
this.closeOpenItems('completed');
|
|
458
|
+
db.prepare(
|
|
459
|
+
'UPDATE turns SET status = ?, agent_response = ?, token_usage = ?, updated_at = ? WHERE id = ?',
|
|
460
|
+
).run('completed', responseText || null, usage ? JSON.stringify(usage) : null, now, this.deps.turnId);
|
|
461
|
+
setSessionStatus(db, this.deps.agentPaneSessionId, 0, 'active', now, true);
|
|
462
|
+
broadcast('turn:completed', {
|
|
463
|
+
sessionId: this.deps.agentPaneSessionId,
|
|
464
|
+
turnId: this.deps.turnId,
|
|
465
|
+
...(usage ? { usage } : {}),
|
|
466
|
+
hasUnreviewed: true,
|
|
467
|
+
});
|
|
468
|
+
void this.syncTitle();
|
|
469
|
+
this.finalize();
|
|
470
|
+
return;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
// Still running — re-arm or give up.
|
|
474
|
+
if (this.extensions < MAX_RESULT_EXTENSIONS) {
|
|
475
|
+
this.extensions += 1;
|
|
476
|
+
this.armResultWatchdog();
|
|
477
|
+
} else {
|
|
478
|
+
this.finishFailed(
|
|
479
|
+
`No result within ${(RESULT_TIMEOUT_MS / 60_000) * (MAX_RESULT_EXTENSIONS + 1)} minutes — the runtime session is still running. Check repoburg logs.`,
|
|
480
|
+
);
|
|
481
|
+
}
|
|
482
|
+
} catch (err) {
|
|
483
|
+
console.error('[repoburg] snapshot reconcile failed:', err);
|
|
484
|
+
if (this.extensions < MAX_RESULT_EXTENSIONS) {
|
|
485
|
+
this.extensions += 1;
|
|
486
|
+
this.armResultWatchdog();
|
|
487
|
+
} else {
|
|
488
|
+
this.finishFailed(`Snapshot reconcile failed: ${(err as Error).message}`);
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
}
|
|
492
|
+
|
|
493
|
+
/** Project one round from the snapshot (idempotent — live-projected
|
|
494
|
+
* rounds already exist and are skipped). */
|
|
495
|
+
private projectSnapshotRound(input: RepoburgSessionInput): void {
|
|
496
|
+
if (!input.raw_llm_response && !input.thoughts && !input.aiActions?.length) return;
|
|
497
|
+
const info = this.round(input.id);
|
|
498
|
+
info.text = info.text || (input.raw_llm_response ?? '');
|
|
499
|
+
info.thoughts = info.thoughts || (input.thoughts ?? '');
|
|
500
|
+
|
|
501
|
+
this.upsertCompletedTextItem(info, 'message', info.text);
|
|
502
|
+
this.upsertCompletedTextItem(info, 'reasoning', info.thoughts);
|
|
503
|
+
|
|
504
|
+
const actions = [...(input.aiActions ?? [])].sort(
|
|
505
|
+
(a, b) => (a.order_of_execution ?? 0) - (b.order_of_execution ?? 0),
|
|
506
|
+
);
|
|
507
|
+
const db = getDb();
|
|
508
|
+
for (const action of actions) {
|
|
509
|
+
if (action.action_type === 'final') info.finalPlain = action.plain ?? null;
|
|
510
|
+
const mapped = mapActionToItem(action);
|
|
511
|
+
if (!mapped) continue;
|
|
512
|
+
const externalId = `${input.id}-action-${action.id}`;
|
|
513
|
+
const existing = db
|
|
514
|
+
.prepare('SELECT id FROM items WHERE turn_id = ? AND external_item_id = ?')
|
|
515
|
+
.get(this.deps.turnId, externalId);
|
|
516
|
+
if (existing) continue;
|
|
517
|
+
insertItem(
|
|
518
|
+
db,
|
|
519
|
+
this.deps.agentPaneSessionId,
|
|
520
|
+
this.deps.turnId,
|
|
521
|
+
externalId,
|
|
522
|
+
mapped.type,
|
|
523
|
+
mapped.data,
|
|
524
|
+
mapped.status,
|
|
525
|
+
'item:completed',
|
|
526
|
+
);
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
|
|
531
|
+
/** Extract the plain text of a final/html_final action from a snapshot input. */
|
|
532
|
+
function extractFinalPlain(input: RepoburgSessionInput | undefined): string | null {
|
|
533
|
+
const action = input?.aiActions?.find(
|
|
534
|
+
(a) => a.action_type === 'final' || a.action_type === 'html_final',
|
|
535
|
+
);
|
|
536
|
+
return action?.plain ?? null;
|
|
537
|
+
}
|
|
@@ -15,11 +15,28 @@ describe('Provider routes', () => {
|
|
|
15
15
|
const res = await app.inject({ method: 'GET', url: '/api/providers' });
|
|
16
16
|
expect(res.statusCode).toBe(200);
|
|
17
17
|
const providers = res.json();
|
|
18
|
-
expect(providers).toHaveLength(
|
|
18
|
+
expect(providers).toHaveLength(3);
|
|
19
19
|
expect(providers[0].id).toBe('codex');
|
|
20
20
|
expect(providers[0].available).toBe(true);
|
|
21
21
|
expect(providers[1].id).toBe('claude');
|
|
22
22
|
expect(providers[1].available).toBe(true);
|
|
23
|
+
expect(providers[2].id).toBe('repoburg');
|
|
24
|
+
expect(providers[2].available).toBe(true);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it('repoburg provider has correct capabilities', async () => {
|
|
28
|
+
const res = await app.inject({ method: 'GET', url: '/api/providers' });
|
|
29
|
+
const repoburg = res.json().find((p: { id: string }) => p.id === 'repoburg');
|
|
30
|
+
expect(repoburg.label).toBe('Repoburg');
|
|
31
|
+
expect(repoburg.capabilities.dynamicModels).toBe(true);
|
|
32
|
+
expect(repoburg.capabilities.skills).toBe(false);
|
|
33
|
+
expect(repoburg.capabilities.plugins).toBe(false);
|
|
34
|
+
expect(repoburg.capabilities.mcpServers).toBe(false);
|
|
35
|
+
expect(repoburg.capabilities.imageAttachments).toBe(false);
|
|
36
|
+
expect(repoburg.capabilities.sandbox).toBe(false);
|
|
37
|
+
expect(repoburg.capabilities.webSearch).toBe(false);
|
|
38
|
+
expect(repoburg.capabilities.networkAccess).toBe(false);
|
|
39
|
+
expect(repoburg.capabilities.fileReferences).toBe(false);
|
|
23
40
|
});
|
|
24
41
|
|
|
25
42
|
it('codex provider has all capabilities enabled', async () => {
|
|
@@ -38,7 +38,25 @@ const CLAUDE_PROVIDER: ProviderDefinition = {
|
|
|
38
38
|
},
|
|
39
39
|
};
|
|
40
40
|
|
|
41
|
-
|
|
41
|
+
/** Repoburg provider — the repoburg backend's full agent engine via HTTP/WS. */
|
|
42
|
+
const REPOBURG_PROVIDER: ProviderDefinition = {
|
|
43
|
+
id: 'repoburg',
|
|
44
|
+
label: 'Repoburg',
|
|
45
|
+
available: true,
|
|
46
|
+
capabilities: {
|
|
47
|
+
dynamicModels: true,
|
|
48
|
+
skills: false,
|
|
49
|
+
plugins: false,
|
|
50
|
+
mcpServers: false,
|
|
51
|
+
imageAttachments: false,
|
|
52
|
+
sandbox: false,
|
|
53
|
+
webSearch: false,
|
|
54
|
+
networkAccess: false,
|
|
55
|
+
fileReferences: false,
|
|
56
|
+
},
|
|
57
|
+
};
|
|
58
|
+
|
|
59
|
+
const PROVIDERS: ProviderDefinition[] = [CODEX_PROVIDER, CLAUDE_PROVIDER, REPOBURG_PROVIDER];
|
|
42
60
|
|
|
43
61
|
/** Register provider routes. */
|
|
44
62
|
export async function registerProviderRoutes(fastify: FastifyInstance): Promise<void> {
|
|
@@ -87,6 +87,18 @@ export async function registerSessionRoutes(fastify: FastifyInstance): Promise<v
|
|
|
87
87
|
}));
|
|
88
88
|
}
|
|
89
89
|
|
|
90
|
+
// Restore persisted context-window usage so the status-bar counter survives reload.
|
|
91
|
+
const cuRow = db.prepare('SELECT context_usage FROM sessions WHERE id = ?').get(id) as
|
|
92
|
+
| { context_usage: string | null }
|
|
93
|
+
| undefined;
|
|
94
|
+
if (cuRow?.context_usage) {
|
|
95
|
+
try {
|
|
96
|
+
detail.contextUsage = JSON.parse(cuRow.context_usage) as SessionDetail['contextUsage'];
|
|
97
|
+
} catch {
|
|
98
|
+
// Corrupt JSON — leave contextUsage unset.
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
|
|
90
102
|
return detail;
|
|
91
103
|
});
|
|
92
104
|
|
|
@@ -72,16 +72,31 @@ export async function registerThreadRoutes(fastify: FastifyInstance): Promise<vo
|
|
|
72
72
|
const provider = getProvider(session.runtime_type);
|
|
73
73
|
if (provider.forkSession) {
|
|
74
74
|
try {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
75
|
+
if (!session.external_thread_id) {
|
|
76
|
+
// No main session to fork from — the thread will start a fresh
|
|
77
|
+
// isolated session (startTurn skips the resume fallback), so the
|
|
78
|
+
// main session is never contaminated.
|
|
79
|
+
console.warn(`[threads] cannot fork for thread ${threadId}: session ${sessionId} has no external_thread_id — thread will start a fresh isolated session`);
|
|
80
|
+
} else {
|
|
81
|
+
const anchorTurn = db.prepare('SELECT * FROM turns WHERE id = ?').get(turnId) as Turn | undefined;
|
|
82
|
+
if (anchorTurn) {
|
|
83
|
+
const forkedSessionId = await provider.forkSession(session, anchorTurn);
|
|
84
|
+
if (forkedSessionId) {
|
|
85
|
+
db.prepare('UPDATE discussion_threads SET external_thread_id = ? WHERE id = ?').run(forkedSessionId, threadId);
|
|
86
|
+
console.log(`[threads] forked to ${forkedSessionId} for thread ${threadId} (session ${sessionId})`);
|
|
87
|
+
} else {
|
|
88
|
+
// forkSession returned null — thread will start a fresh isolated
|
|
89
|
+
// session in startTurn (NOT the main session), so isolation holds.
|
|
90
|
+
console.warn(`[threads] forkSession returned null for thread ${threadId} (external_thread_id=${session.external_thread_id}, upToMessageId=${anchorTurn.external_turn_id}) — thread will start a fresh isolated session`);
|
|
91
|
+
}
|
|
92
|
+
} else {
|
|
93
|
+
console.warn(`[threads] anchor turn ${turnId} not found for thread ${threadId} — cannot fork`);
|
|
80
94
|
}
|
|
81
95
|
}
|
|
82
96
|
} catch (err) {
|
|
83
97
|
console.error(`[threads] forkSession failed for thread ${threadId}:`, err);
|
|
84
|
-
// Non-fatal — thread falls back to
|
|
98
|
+
// Non-fatal — thread falls back to a fresh isolated session (NOT the
|
|
99
|
+
// main session) so the main conversation is never contaminated.
|
|
85
100
|
}
|
|
86
101
|
}
|
|
87
102
|
|