@myrialabs/clopen 0.3.13 → 0.3.15
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/backend/chat/stream-manager.ts +15 -0
- package/backend/engine/README.md +16 -1669
- package/backend/engine/adapters/claude/message-converter.ts +36 -7
- package/backend/engine/adapters/claude/models.ts +219 -4
- package/backend/engine/docs/adapter-contract.md +183 -0
- package/backend/engine/docs/adding-an-engine.md +150 -0
- package/backend/engine/docs/database.md +123 -0
- package/backend/engine/docs/frontend-and-chat.md +176 -0
- package/backend/engine/docs/lessons-learned.md +715 -0
- package/backend/engine/docs/quick-reference.md +36 -0
- package/backend/engine/docs/system-tools.md +82 -0
- package/backend/engine/docs/websocket-routes.md +240 -0
- package/backend/files/file-archive.test.ts +131 -0
- package/backend/files/file-archive.ts +89 -28
- package/backend/files/file-operations.ts +21 -1
- package/backend/files/file-reading.ts +1 -13
- package/backend/files/file-watcher.ts +117 -3
- package/backend/http/audio.ts +236 -0
- package/backend/index.ts +6 -0
- package/backend/projects/project-cleanup.ts +1 -1
- package/backend/tunnel/cloudflared/index.ts +3 -1
- package/backend/tunnel/cloudflared/tunnel.ts +42 -0
- package/backend/tunnel/global-tunnel-manager.ts +52 -1
- package/backend/utils/ws.ts +9 -0
- package/backend/ws/chat/stream.ts +46 -38
- package/backend/ws/files/search.ts +5 -2
- package/backend/ws/files/watch.ts +48 -19
- package/backend/ws/files/write.ts +17 -6
- package/dist/assets/main-CYVFcz-a.js +358 -0
- package/dist/assets/{main-BqiSLc8F.css → main-DrFILAEB.css} +1 -1
- package/dist/audio/alert.ogg +0 -0
- package/dist/audio/bell.ogg +0 -0
- package/dist/audio/blip.ogg +0 -0
- package/dist/audio/chirp.ogg +0 -0
- package/dist/audio/cloud.ogg +0 -0
- package/dist/audio/glass.ogg +0 -0
- package/dist/audio/message.ogg +0 -0
- package/dist/audio/sparkle.ogg +0 -0
- package/dist/audio/subtle.ogg +0 -0
- package/dist/audio/tap.ogg +0 -0
- package/dist/audio/woodblock.ogg +0 -0
- package/dist/index.html +2 -2
- package/package.json +2 -2
- package/shared/constants/notification-sounds.ts +31 -0
- package/shared/types/stores/settings.ts +4 -0
- package/shared/types/unified/index.ts +6 -0
- package/shared/types/unified/stream.ts +15 -1
- package/shared/types/unified/tool.ts +41 -1
- package/dist/assets/main-DOVudc3_.js +0 -354
- /package/dist/audio/{notification.ogg → chime.ogg} +0 -0
|
@@ -38,6 +38,7 @@ import type {
|
|
|
38
38
|
ErrorResultEvent,
|
|
39
39
|
SystemInitEvent,
|
|
40
40
|
RateLimitEvent,
|
|
41
|
+
NotificationEvent,
|
|
41
42
|
TokenUsage,
|
|
42
43
|
StopReason,
|
|
43
44
|
} from '$shared/types/unified';
|
|
@@ -389,6 +390,27 @@ export function convertSystemInit(msg: SDKSystemMessage): SystemInitEvent {
|
|
|
389
390
|
};
|
|
390
391
|
}
|
|
391
392
|
|
|
393
|
+
/**
|
|
394
|
+
* Convert SDKTaskNotificationMessage (terminal background-task event) →
|
|
395
|
+
* NotificationEvent. Fires when a backgrounded Agent/teammate finishes
|
|
396
|
+
* abnormally (failed | stopped). Successful completion is intentionally
|
|
397
|
+
* not surfaced — the result already lands in the conversation, and a
|
|
398
|
+
* success toast is just noise. The non-terminal task_started /
|
|
399
|
+
* task_progress / task_updated messages are also not surfaced.
|
|
400
|
+
*/
|
|
401
|
+
function convertTaskNotification(
|
|
402
|
+
msg: { session_id: string; status: string; summary?: string; task_id: string },
|
|
403
|
+
): NotificationEvent | null {
|
|
404
|
+
if (msg.status === 'completed') return null;
|
|
405
|
+
return {
|
|
406
|
+
type: 'notification',
|
|
407
|
+
sessionId: msg.session_id,
|
|
408
|
+
level: 'warning',
|
|
409
|
+
title: `Background task ${msg.status}`,
|
|
410
|
+
message: msg.summary || `Task ${msg.task_id} ${msg.status}`,
|
|
411
|
+
};
|
|
412
|
+
}
|
|
413
|
+
|
|
392
414
|
/** Convert SDKCompactBoundaryMessage → CompactBoundaryMessage */
|
|
393
415
|
export function convertCompactBoundary(msg: SDKCompactBoundaryMessage): CompactBoundaryMessage {
|
|
394
416
|
return {
|
|
@@ -476,14 +498,21 @@ function* dispatchSdkMessage(msg: SDKMessage, state: StreamConverterState): Gene
|
|
|
476
498
|
break;
|
|
477
499
|
|
|
478
500
|
case 'system': {
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
501
|
+
// Read subtype as a plain string: the SDKMessage `system` arm covers
|
|
502
|
+
// more subtypes (task_*, etc.) than the SDKSystemMessage |
|
|
503
|
+
// SDKCompactBoundaryMessage union, so narrowing on that union would
|
|
504
|
+
// collapse the task branch to `never`.
|
|
505
|
+
const subtype = (msg as { subtype?: string }).subtype;
|
|
506
|
+
if (subtype === 'init') {
|
|
507
|
+
yield convertSystemInit(msg as SDKSystemMessage);
|
|
508
|
+
} else if (subtype === 'compact_boundary') {
|
|
509
|
+
yield convertCompactBoundary(msg as SDKCompactBoundaryMessage);
|
|
510
|
+
} else if (subtype === 'task_notification') {
|
|
511
|
+
const note = convertTaskNotification(msg as unknown as Parameters<typeof convertTaskNotification>[0]);
|
|
512
|
+
if (note) yield note;
|
|
486
513
|
}
|
|
514
|
+
// task_started / task_progress / task_updated: background-task
|
|
515
|
+
// progress noise — intentionally not surfaced (no tasks panel).
|
|
487
516
|
break;
|
|
488
517
|
}
|
|
489
518
|
|
|
@@ -14,8 +14,8 @@ export const CLAUDE_CODE_MODELS: EngineModel[] = [
|
|
|
14
14
|
type: 'claude-code',
|
|
15
15
|
provider: 'anthropic',
|
|
16
16
|
model: {
|
|
17
|
-
id: 'claude-opus-4-
|
|
18
|
-
name: 'Claude Opus 4.
|
|
17
|
+
id: 'claude-opus-4-8',
|
|
18
|
+
name: 'Claude Opus 4.8',
|
|
19
19
|
},
|
|
20
20
|
account: {
|
|
21
21
|
id: 0,
|
|
@@ -23,7 +23,7 @@ export const CLAUDE_CODE_MODELS: EngineModel[] = [
|
|
|
23
23
|
},
|
|
24
24
|
},
|
|
25
25
|
limit: {
|
|
26
|
-
input:
|
|
26
|
+
input: 1_000_000,
|
|
27
27
|
output: 128_000,
|
|
28
28
|
},
|
|
29
29
|
modalities: {
|
|
@@ -66,7 +66,7 @@ export const CLAUDE_CODE_MODELS: EngineModel[] = [
|
|
|
66
66
|
},
|
|
67
67
|
},
|
|
68
68
|
limit: {
|
|
69
|
-
input:
|
|
69
|
+
input: 1_000_000,
|
|
70
70
|
output: 64_000,
|
|
71
71
|
},
|
|
72
72
|
modalities: {
|
|
@@ -138,4 +138,219 @@ export const CLAUDE_CODE_MODELS: EngineModel[] = [
|
|
|
138
138
|
output: 5,
|
|
139
139
|
},
|
|
140
140
|
},
|
|
141
|
+
{
|
|
142
|
+
engine: {
|
|
143
|
+
type: 'claude-code',
|
|
144
|
+
provider: 'anthropic',
|
|
145
|
+
model: {
|
|
146
|
+
id: 'claude-opus-4-7',
|
|
147
|
+
name: 'Claude Opus 4.7',
|
|
148
|
+
},
|
|
149
|
+
account: {
|
|
150
|
+
id: 0,
|
|
151
|
+
name: '',
|
|
152
|
+
},
|
|
153
|
+
},
|
|
154
|
+
limit: {
|
|
155
|
+
input: 1_000_000,
|
|
156
|
+
output: 128_000,
|
|
157
|
+
},
|
|
158
|
+
modalities: {
|
|
159
|
+
input: {
|
|
160
|
+
text: true,
|
|
161
|
+
image: true,
|
|
162
|
+
audio: false,
|
|
163
|
+
video: false,
|
|
164
|
+
pdf: true,
|
|
165
|
+
},
|
|
166
|
+
output: {
|
|
167
|
+
text: true,
|
|
168
|
+
image: false,
|
|
169
|
+
audio: false,
|
|
170
|
+
video: false,
|
|
171
|
+
pdf: false,
|
|
172
|
+
},
|
|
173
|
+
},
|
|
174
|
+
capabilities: {
|
|
175
|
+
reasoning: true,
|
|
176
|
+
tools: true,
|
|
177
|
+
structuredOutput: true,
|
|
178
|
+
},
|
|
179
|
+
cost: {
|
|
180
|
+
input: 5,
|
|
181
|
+
output: 25,
|
|
182
|
+
},
|
|
183
|
+
},
|
|
184
|
+
{
|
|
185
|
+
engine: {
|
|
186
|
+
type: 'claude-code',
|
|
187
|
+
provider: 'anthropic',
|
|
188
|
+
model: {
|
|
189
|
+
id: 'claude-opus-4-6',
|
|
190
|
+
name: 'Claude Opus 4.6',
|
|
191
|
+
},
|
|
192
|
+
account: {
|
|
193
|
+
id: 0,
|
|
194
|
+
name: '',
|
|
195
|
+
},
|
|
196
|
+
},
|
|
197
|
+
limit: {
|
|
198
|
+
input: 1_000_000,
|
|
199
|
+
output: 128_000,
|
|
200
|
+
},
|
|
201
|
+
modalities: {
|
|
202
|
+
input: {
|
|
203
|
+
text: true,
|
|
204
|
+
image: true,
|
|
205
|
+
audio: false,
|
|
206
|
+
video: false,
|
|
207
|
+
pdf: true,
|
|
208
|
+
},
|
|
209
|
+
output: {
|
|
210
|
+
text: true,
|
|
211
|
+
image: false,
|
|
212
|
+
audio: false,
|
|
213
|
+
video: false,
|
|
214
|
+
pdf: false,
|
|
215
|
+
},
|
|
216
|
+
},
|
|
217
|
+
capabilities: {
|
|
218
|
+
reasoning: true,
|
|
219
|
+
tools: true,
|
|
220
|
+
structuredOutput: true,
|
|
221
|
+
},
|
|
222
|
+
cost: {
|
|
223
|
+
input: 5,
|
|
224
|
+
output: 25,
|
|
225
|
+
},
|
|
226
|
+
},
|
|
227
|
+
{
|
|
228
|
+
engine: {
|
|
229
|
+
type: 'claude-code',
|
|
230
|
+
provider: 'anthropic',
|
|
231
|
+
model: {
|
|
232
|
+
id: 'claude-sonnet-4-5',
|
|
233
|
+
name: 'Claude Sonnet 4.5',
|
|
234
|
+
},
|
|
235
|
+
account: {
|
|
236
|
+
id: 0,
|
|
237
|
+
name: '',
|
|
238
|
+
},
|
|
239
|
+
},
|
|
240
|
+
limit: {
|
|
241
|
+
input: 200_000,
|
|
242
|
+
output: 64_000,
|
|
243
|
+
},
|
|
244
|
+
modalities: {
|
|
245
|
+
input: {
|
|
246
|
+
text: true,
|
|
247
|
+
image: true,
|
|
248
|
+
audio: false,
|
|
249
|
+
video: false,
|
|
250
|
+
pdf: true,
|
|
251
|
+
},
|
|
252
|
+
output: {
|
|
253
|
+
text: true,
|
|
254
|
+
image: false,
|
|
255
|
+
audio: false,
|
|
256
|
+
video: false,
|
|
257
|
+
pdf: false,
|
|
258
|
+
},
|
|
259
|
+
},
|
|
260
|
+
capabilities: {
|
|
261
|
+
reasoning: true,
|
|
262
|
+
tools: true,
|
|
263
|
+
structuredOutput: true,
|
|
264
|
+
},
|
|
265
|
+
cost: {
|
|
266
|
+
input: 3,
|
|
267
|
+
output: 15,
|
|
268
|
+
},
|
|
269
|
+
},
|
|
270
|
+
{
|
|
271
|
+
engine: {
|
|
272
|
+
type: 'claude-code',
|
|
273
|
+
provider: 'anthropic',
|
|
274
|
+
model: {
|
|
275
|
+
id: 'claude-opus-4-5',
|
|
276
|
+
name: 'Claude Opus 4.5',
|
|
277
|
+
},
|
|
278
|
+
account: {
|
|
279
|
+
id: 0,
|
|
280
|
+
name: '',
|
|
281
|
+
},
|
|
282
|
+
},
|
|
283
|
+
limit: {
|
|
284
|
+
input: 200_000,
|
|
285
|
+
output: 64_000,
|
|
286
|
+
},
|
|
287
|
+
modalities: {
|
|
288
|
+
input: {
|
|
289
|
+
text: true,
|
|
290
|
+
image: true,
|
|
291
|
+
audio: false,
|
|
292
|
+
video: false,
|
|
293
|
+
pdf: true,
|
|
294
|
+
},
|
|
295
|
+
output: {
|
|
296
|
+
text: true,
|
|
297
|
+
image: false,
|
|
298
|
+
audio: false,
|
|
299
|
+
video: false,
|
|
300
|
+
pdf: false,
|
|
301
|
+
},
|
|
302
|
+
},
|
|
303
|
+
capabilities: {
|
|
304
|
+
reasoning: true,
|
|
305
|
+
tools: true,
|
|
306
|
+
structuredOutput: true,
|
|
307
|
+
},
|
|
308
|
+
cost: {
|
|
309
|
+
input: 5,
|
|
310
|
+
output: 25,
|
|
311
|
+
},
|
|
312
|
+
},
|
|
313
|
+
{
|
|
314
|
+
engine: {
|
|
315
|
+
type: 'claude-code',
|
|
316
|
+
provider: 'anthropic',
|
|
317
|
+
model: {
|
|
318
|
+
id: 'claude-opus-4-1',
|
|
319
|
+
name: 'Claude Opus 4.1',
|
|
320
|
+
},
|
|
321
|
+
account: {
|
|
322
|
+
id: 0,
|
|
323
|
+
name: '',
|
|
324
|
+
},
|
|
325
|
+
},
|
|
326
|
+
limit: {
|
|
327
|
+
input: 200_000,
|
|
328
|
+
output: 32_000,
|
|
329
|
+
},
|
|
330
|
+
modalities: {
|
|
331
|
+
input: {
|
|
332
|
+
text: true,
|
|
333
|
+
image: true,
|
|
334
|
+
audio: false,
|
|
335
|
+
video: false,
|
|
336
|
+
pdf: true,
|
|
337
|
+
},
|
|
338
|
+
output: {
|
|
339
|
+
text: true,
|
|
340
|
+
image: false,
|
|
341
|
+
audio: false,
|
|
342
|
+
video: false,
|
|
343
|
+
pdf: false,
|
|
344
|
+
},
|
|
345
|
+
},
|
|
346
|
+
capabilities: {
|
|
347
|
+
reasoning: true,
|
|
348
|
+
tools: true,
|
|
349
|
+
structuredOutput: true,
|
|
350
|
+
},
|
|
351
|
+
cost: {
|
|
352
|
+
input: 15,
|
|
353
|
+
output: 75,
|
|
354
|
+
},
|
|
355
|
+
},
|
|
141
356
|
];
|
|
@@ -0,0 +1,183 @@
|
|
|
1
|
+
[← Engine adapter guide](../README.md)
|
|
2
|
+
|
|
3
|
+
## 2. The `backend/engine/` layer — core contract
|
|
4
|
+
|
|
5
|
+
### 2.1 `types.ts` — the `AIEngine` interface
|
|
6
|
+
|
|
7
|
+
```ts
|
|
8
|
+
export interface AIEngine {
|
|
9
|
+
readonly name: EngineType; // 'claude-code' | 'opencode' | 'copilot'
|
|
10
|
+
readonly isInitialized: boolean;
|
|
11
|
+
readonly isActive: boolean;
|
|
12
|
+
|
|
13
|
+
initialize(accountId?: number): Promise<void>; // lazy; idempotent
|
|
14
|
+
dispose(): Promise<void>; // called per-project & on shutdown
|
|
15
|
+
cancel(): Promise<void>; // hard cancel
|
|
16
|
+
interrupt(): Promise<void>; // soft stop
|
|
17
|
+
|
|
18
|
+
streamQuery(options: EngineQueryOptions):
|
|
19
|
+
AsyncGenerator<EngineOutput, void, unknown>;
|
|
20
|
+
|
|
21
|
+
getAvailableModels(): Promise<EngineModel[]>;
|
|
22
|
+
|
|
23
|
+
// Optional
|
|
24
|
+
resolveUserAnswer?(toolUseId: string, answers: Record<string,string>): boolean;
|
|
25
|
+
generateStructured?<T>(options: StructuredGenerationOptions): Promise<T>;
|
|
26
|
+
}
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
Three invariants:
|
|
30
|
+
|
|
31
|
+
1. **`streamQuery` is the only streaming output path.** It is an
|
|
32
|
+
`AsyncGenerator<EngineOutput>`. The adapter translates SDK events into
|
|
33
|
+
`EngineOutput` — that is all.
|
|
34
|
+
2. **Streaming state lives on the instance.** Because `getProjectEngine`
|
|
35
|
+
returns one instance per `(projectId, engineType)`, the abort controller,
|
|
36
|
+
active query, `pendingUserAnswers`, and session ID are automatically
|
|
37
|
+
isolated per-project.
|
|
38
|
+
3. **Init is lazy & concurrency-safe.** The first `streamQuery` /
|
|
39
|
+
`getAvailableModels` call triggers `initialize()`. Multiple parallel
|
|
40
|
+
callers **must** share a single init promise. See:
|
|
41
|
+
- `claude/environment.ts::setupEnvironmentOnce`
|
|
42
|
+
- `opencode/server.ts::ensureClient`
|
|
43
|
+
|
|
44
|
+
### 2.2 `index.ts` — registry & lifecycle
|
|
45
|
+
|
|
46
|
+
Two instance tiers:
|
|
47
|
+
|
|
48
|
+
| Tier | Factory | Used for |
|
|
49
|
+
|--------------------|-------------------------------|-------------------------------------------|
|
|
50
|
+
| Global singleton | `getEngine(type)` | Non-streaming ops: `models:list`, settings|
|
|
51
|
+
| Per-project | `getProjectEngine(projectId, type)` | Streaming chat — isolated per-project|
|
|
52
|
+
|
|
53
|
+
Cleanup:
|
|
54
|
+
- `disposeProjectEngines(projectId)` when a project closes.
|
|
55
|
+
- `disposeAllEngines()` at server shutdown — also calls
|
|
56
|
+
`disposeOpenCodeClient()`. Pattern: when an adapter owns a shared
|
|
57
|
+
subprocess, expose `disposeXxxClient()` from `adapters/<name>/index.ts`
|
|
58
|
+
and call it from `disposeAllProjectEngines()` in `index.ts`.
|
|
59
|
+
|
|
60
|
+
### 2.3 `EngineOutput` — the event contract
|
|
61
|
+
|
|
62
|
+
In `shared/types/unified/stream.ts`:
|
|
63
|
+
|
|
64
|
+
```ts
|
|
65
|
+
type EngineOutput =
|
|
66
|
+
| UserMessage // persisted to DB
|
|
67
|
+
| AssistantMessage // persisted to DB
|
|
68
|
+
| ReasoningMessage // persisted to DB
|
|
69
|
+
| CompactBoundaryMessage
|
|
70
|
+
| StreamEvent // transient: 'start' | 'stop' | 'delta'
|
|
71
|
+
| ResultEvent // transient: success / error_max_turns / etc.
|
|
72
|
+
| SystemInitEvent // transient: model + tools + mcpServers
|
|
73
|
+
| RateLimitEvent; // transient
|
|
74
|
+
```
|
|
75
|
+
|
|
76
|
+
`stream-manager.ts` discriminates on `output.type`:
|
|
77
|
+
|
|
78
|
+
| `type` | Routing |
|
|
79
|
+
|----------------|---------------------------------------------------------|
|
|
80
|
+
| `user`/`assistant`/`reasoning`/`compact_boundary` | save DB + emit `chat:message` |
|
|
81
|
+
| `stream_event` | forward to frontend for live typing (`chat:partial`) |
|
|
82
|
+
| `result` | extract metadata + emit `chat:stream-finished` |
|
|
83
|
+
| `system_init` | emit MCP-failure notification if any |
|
|
84
|
+
| `rate_limit` | emit `chat:notification` |
|
|
85
|
+
|
|
86
|
+
> **Strict rule:** do not invent new types. If an SDK has an event that
|
|
87
|
+
> needs to be propagated, **extend** the union in
|
|
88
|
+
> `shared/types/unified/stream.ts` first, teach `stream-manager` how to
|
|
89
|
+
> route it, and only then emit it from the adapter.
|
|
90
|
+
|
|
91
|
+
### 2.4 `EngineQueryOptions`
|
|
92
|
+
|
|
93
|
+
```ts
|
|
94
|
+
interface EngineQueryOptions {
|
|
95
|
+
projectPath: string;
|
|
96
|
+
prompt: UserMessage;
|
|
97
|
+
resume?: string; // SDK session id for resume / fork
|
|
98
|
+
forkSession?: boolean;
|
|
99
|
+
maxTurns?: number;
|
|
100
|
+
providerSlug: string; // 'anthropic', 'openai', etc — required for opencode
|
|
101
|
+
modelId: string; // 'claude-opus-4-7', 'gpt-5', etc
|
|
102
|
+
includePartialMessages?: boolean;
|
|
103
|
+
abortController?: AbortController;
|
|
104
|
+
accountId?: number; // override credential for a single stream
|
|
105
|
+
mcpContext?: McpExecutionContext; // { projectId, chatSessionId, streamId }
|
|
106
|
+
}
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
`mcpContext` is bound into the MCP handler so a tool call from project A
|
|
110
|
+
**cannot** write into project B. Always forward it: see `claude/stream.ts`
|
|
111
|
+
calling `getEnabledMcpServers(options.mcpContext)`.
|
|
112
|
+
|
|
113
|
+
### 2.5 What an adapter **MUST NOT** do
|
|
114
|
+
|
|
115
|
+
- ❌ Call `ws.emit.*` — that is the stream-manager's job.
|
|
116
|
+
- ❌ Touch chat / message DB (`messageQueries`, `sessionQueries`) —
|
|
117
|
+
persistence belongs to the stream-manager. Adapters **may** read
|
|
118
|
+
credentials via `engineQueries`.
|
|
119
|
+
- ❌ Mutate `process.env` or call `process.chdir()` — pass env & cwd via
|
|
120
|
+
SDK options. Many projects stream concurrently.
|
|
121
|
+
- ❌ Define shared types inline — extend `shared/types/unified/*`.
|
|
122
|
+
- ❌ Use `console.*` — use `debug` from `$shared/utils/logger` with
|
|
123
|
+
category `'engine'`, `'chat'`, or `'mcp'`.
|
|
124
|
+
- ❌ **Build a new MCP HTTP server for the adapter.** Use the existing
|
|
125
|
+
`backend/mcp/remote-server.ts` and add a `getXxxMcpConfig()` helper in
|
|
126
|
+
`backend/mcp/config.ts` returning the URL in the engine's expected
|
|
127
|
+
shape. New MCP servers are added via `defineServer()` — never via a
|
|
128
|
+
parallel HTTP listener.
|
|
129
|
+
- ❌ **Create per-account isolated home directories** to multiplex two
|
|
130
|
+
accounts into the same CLI's shared dotfile. Snapshot the CLI's auth
|
|
131
|
+
file into `engine_accounts.credential` on login; write the chosen
|
|
132
|
+
account's snapshot back into the shared location on switch. See §9.13.
|
|
133
|
+
|
|
134
|
+
### 2.6 Standard files in each adapter
|
|
135
|
+
|
|
136
|
+
Adapters follow a fixed file taxonomy. **Mandatory** files exist in every
|
|
137
|
+
adapter; **optional** files may be omitted, but when present they MUST use
|
|
138
|
+
the canonical name below — never a synonym (`auth.ts`, `provider-catalog.ts`,
|
|
139
|
+
etc.). This keeps `git grep` and the README's quick-reference rows usable.
|
|
140
|
+
|
|
141
|
+
```
|
|
142
|
+
adapters/<name>/
|
|
143
|
+
├── index.ts ← MANDATORY re-exports only, NO logic
|
|
144
|
+
├── stream.ts ← MANDATORY class implements AIEngine
|
|
145
|
+
├── models.ts ← MANDATORY static EngineModel[] OR dynamic fetcher
|
|
146
|
+
├── message-converter.ts ← MANDATORY SDK message → EngineOutput (pure)
|
|
147
|
+
├── error-handler.ts ← MANDATORY SDK error → user-facing string
|
|
148
|
+
├── credential.ts? ← OPTIONAL credential parse / auth-blob swap (codex, qwen)
|
|
149
|
+
├── environment.ts? ← OPTIONAL env / dotfile setup (claude, qwen)
|
|
150
|
+
├── server.ts? ← OPTIONAL subprocess + client lifecycle (opencode)
|
|
151
|
+
├── config.ts? ← OPTIONAL runtime config builder (opencode)
|
|
152
|
+
├── presets.ts? ← OPTIONAL multi-provider/region preset catalog (qwen, opencode)
|
|
153
|
+
└── session-fork.ts? ← OPTIONAL on-disk session fork workaround (codex, qwen)
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
Naming rules — strict, even when an SDK's local jargon differs:
|
|
157
|
+
|
|
158
|
+
| File | Owns |
|
|
159
|
+
|-------------------|-----------------------------------------------------------------------|
|
|
160
|
+
| `credential.ts` | Parse `engine_accounts.credential` (JSON wrapper or raw key); for shared-CLI engines, materialise the auth-blob into the dotfile and snapshot it back. **Never** name this `auth.ts` — credentials are the unified concept (see §9.13). |
|
|
161
|
+
| `error-handler.ts`| Export `handleStreamError(error: unknown, ...): void` that swallows abort errors and re-throws everything else as a sanitised `Error`. Required even when the body is short — `OpenCodeEngine` previously inlined ~50 lines into the catch block; that pattern is no longer accepted. |
|
|
162
|
+
| `presets.ts` | Multi-provider/region picker catalog (Qwen's DashScope/OpenRouter/Fireworks; OpenCode's models.dev cache). Multi-provider engines that lacked a `presets.ts` (OpenCode used to call this `config.ts`) have been migrated. |
|
|
163
|
+
| `config.ts` | Runtime config builder ONLY — turning DB providers + accounts into env vars / spawn options. Catalog data goes in `presets.ts`. |
|
|
164
|
+
|
|
165
|
+
Important conventions:
|
|
166
|
+
- `message-converter.ts` is generally pure. If you need per-stream state
|
|
167
|
+
(e.g. tracking "is the reasoning block already closed?"), expose a
|
|
168
|
+
**factory** `createSdkMessageConverter()` returning a stateful converter
|
|
169
|
+
— see Claude.
|
|
170
|
+
- Tool names **must** be canonicalised via `toCanonicalToolName(...)`
|
|
171
|
+
(`shared/types/unified/tool.ts`) so the frontend renders the same UI for
|
|
172
|
+
tools that have different names across SDKs.
|
|
173
|
+
- `cancel()` ordering: **abort the local controller first**, **then** RPC
|
|
174
|
+
to the SDK/server. RPCs can hang; the local abort cuts the `for await`
|
|
175
|
+
loop deterministically. See `OpenCodeEngine.cancel`,
|
|
176
|
+
`ClaudeCodeEngine.cancel`, `CopilotEngine.cancel`.
|
|
177
|
+
- Dynamic-catalog fetchers in `models.ts` MUST return `EngineModel[]` and
|
|
178
|
+
use `[]` as the failure sentinel (network, auth, parse error). Do **not**
|
|
179
|
+
return `null` — both `fetchOpenCodeModels` and `fetchQwenModels` return
|
|
180
|
+
`[]` on failure so the picker renders empty rather than a stale catalog.
|
|
181
|
+
|
|
182
|
+
---
|
|
183
|
+
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
[← Engine adapter guide](../README.md)
|
|
2
|
+
|
|
3
|
+
## 8. End-to-end checklist for adding a new engine
|
|
4
|
+
|
|
5
|
+
The steps below are the same pattern used when the **`copilot`**
|
|
6
|
+
(`@github/copilot-sdk`) adapter was added to the repo. Use it as a
|
|
7
|
+
blueprint for the next engine you add.
|
|
8
|
+
|
|
9
|
+
### Stage 1 — Shared types
|
|
10
|
+
|
|
11
|
+
- [ ] `shared/types/unified/common.ts`: add `'newengine'` to `EngineType`.
|
|
12
|
+
- [ ] `shared/types/unified/engine.ts`: add `'newengine'` to `EngineInfo.type`.
|
|
13
|
+
- [ ] `shared/types/unified/message.ts`: add `'newengine'` to `MessageEngine.type`.
|
|
14
|
+
- [ ] `shared/constants/engines.ts`: add an entry in `ENGINES[]`
|
|
15
|
+
(`type, name, description, icon.light, icon.dark`).
|
|
16
|
+
- [ ] `backend/engine/adapters/newengine/models.ts`: own the catalog. Static
|
|
17
|
+
engines export a `NEWENGINE_MODELS: EngineModel[]` array; dynamic
|
|
18
|
+
engines export a fetcher (`fetchNewengineModels(...)`) consumed by
|
|
19
|
+
`getAvailableModels()`. Don't add anything to `shared/constants/engines.ts`
|
|
20
|
+
for the catalog itself — the frontend loads it via `models:list`.
|
|
21
|
+
- [ ] (Optional) `backend/engine/adapters/newengine/presets.ts` if the
|
|
22
|
+
engine needs provider/region/BYOK presets exposed to the UI.
|
|
23
|
+
Mirror `qwen/presets.ts`: keep runtime values in the adapter,
|
|
24
|
+
export wire types from `shared/types/unified/engine.ts`, and ship
|
|
25
|
+
a WS endpoint (see Stage 4) — never import these from frontend
|
|
26
|
+
via `$backend`.
|
|
27
|
+
|
|
28
|
+
### Stage 2 — Adapter `backend/engine/`
|
|
29
|
+
|
|
30
|
+
Refer to §2.6 for the full file taxonomy. Every adapter ships these five
|
|
31
|
+
mandatory files; optional files use the canonical names from §2.6.
|
|
32
|
+
|
|
33
|
+
- [ ] Create `backend/engine/adapters/newengine/`
|
|
34
|
+
- [ ] `index.ts` (mandatory) → `export { NewEngineEngine } from './stream';`
|
|
35
|
+
(+ `disposeXxxClient` if there is a subprocess).
|
|
36
|
+
- [ ] `stream.ts` (mandatory) → class `NewEngineEngine implements AIEngine`.
|
|
37
|
+
Pick a template: `claude/stream.ts` (in-process SDK),
|
|
38
|
+
`opencode/stream.ts` (subprocess), `copilot/stream.ts` (in-process
|
|
39
|
+
with construction-time credential), or `qwen/stream.ts` (CLI subprocess
|
|
40
|
+
via SDK with auth-blob swap).
|
|
41
|
+
- [ ] `models.ts` (mandatory) → static `NEWENGINE_MODELS: EngineModel[]`
|
|
42
|
+
OR `fetchNewengineModels(...): Promise<EngineModel[]>` (dynamic;
|
|
43
|
+
return `[]` on failure — see §4.5).
|
|
44
|
+
- [ ] `message-converter.ts` (mandatory) → pure functions SDK → `EngineOutput`.
|
|
45
|
+
Use `toCanonicalToolName()` for tool names.
|
|
46
|
+
- [ ] `error-handler.ts` (mandatory) → exports `handleStreamError(error,
|
|
47
|
+
...): void` (and any helper formatters specific to the SDK's error
|
|
48
|
+
payload — see `opencode/error-handler.ts::formatSessionError` for the
|
|
49
|
+
pattern).
|
|
50
|
+
- [ ] `credential.ts` / `environment.ts` / `server.ts` / `config.ts` /
|
|
51
|
+
`presets.ts` / `session-fork.ts` (optional; canonical names only).
|
|
52
|
+
- [ ] `backend/engine/index.ts`: import + add a case in
|
|
53
|
+
`createEngine(type)`. If there is a shared subprocess, call
|
|
54
|
+
`disposeXxxClient()` from `disposeAllProjectEngines()`.
|
|
55
|
+
|
|
56
|
+
### Stage 3 — Database (if a default provider needs to be seeded)
|
|
57
|
+
|
|
58
|
+
- [ ] Add a new migration in `backend/database/migrations/` (e.g. seed
|
|
59
|
+
`('newengine', 'vendor', 'Vendor', NULL, NULL, '{}', 1)` into
|
|
60
|
+
`engine_providers`). **Do not change the schema** — the tables
|
|
61
|
+
are already generic.
|
|
62
|
+
|
|
63
|
+
### Stage 4 — WebSocket `engine:newengine-*`
|
|
64
|
+
|
|
65
|
+
- [ ] Create `backend/ws/engine/newengine/`:
|
|
66
|
+
- `status.ts` — `engine:newengine-status` (`installed`, `version`, etc).
|
|
67
|
+
- `accounts.ts` or `providers.ts` — account/provider CRUD:
|
|
68
|
+
- List, add, switch, delete, rename.
|
|
69
|
+
- Setup flow: PTY (Claude-style) OR direct API-key/PAT input
|
|
70
|
+
(OpenCode/Copilot-style) depending on the SDK's auth mechanism.
|
|
71
|
+
- (Optional) `presets.ts` — `engine:newengine-presets-list` if the
|
|
72
|
+
adapter has a `presets.ts` module. See `backend/ws/engine/qwen/presets.ts`.
|
|
73
|
+
- `index.ts` — merge the sub-router.
|
|
74
|
+
- [ ] `backend/ws/engine/index.ts`: `.merge(newengineEngineRouter)`.
|
|
75
|
+
- [ ] `backend/ws/chat/stream.ts`: add the `'newengine'` literal to the
|
|
76
|
+
typebox schema for `engine.type`.
|
|
77
|
+
- [ ] `backend/ws/settings/crud.ts`: add the `'newengine'` literal to the
|
|
78
|
+
`models:list` typebox schema.
|
|
79
|
+
|
|
80
|
+
### Stage 5 — Frontend store
|
|
81
|
+
|
|
82
|
+
- [ ] Add `frontend/stores/features/newengine-accounts.svelte.ts` (or
|
|
83
|
+
`newengine-providers.svelte.ts` following OpenCode's style if the
|
|
84
|
+
engine is multi-provider). Mirror the pattern of
|
|
85
|
+
`claudeAccountsStore` / `copilotAccountsStore` / `opencodeProvidersStore`.
|
|
86
|
+
|
|
87
|
+
### Stage 6 — Settings → Engines
|
|
88
|
+
|
|
89
|
+
- [ ] Add a panel to `AIEnginesSettings.svelte`: the card appears
|
|
90
|
+
automatically since `ENGINES` already has the new entry. Implement
|
|
91
|
+
the setup flow:
|
|
92
|
+
- Load status via `ws.http('engine:newengine-status', {})`.
|
|
93
|
+
- Subscribe to server-emit events for login progress.
|
|
94
|
+
- After save: `newengineStore.refresh()` +
|
|
95
|
+
`modelStore.refreshModels('newengine')`.
|
|
96
|
+
|
|
97
|
+
### Stage 7 — Settings → System Tools
|
|
98
|
+
|
|
99
|
+
- [ ] `backend/engine/install-recipes.ts`:
|
|
100
|
+
- Add `'newengine'` to `ToolId`.
|
|
101
|
+
- Write `resolveNewEngineRecipe()` (winget/brew/curl pipe — whatever the
|
|
102
|
+
SDK needs to install its CLI).
|
|
103
|
+
- Add a case in `resolveRecipe()`.
|
|
104
|
+
- [ ] `backend/ws/system-tools/status.ts` & `install.ts`: add
|
|
105
|
+
`'newengine'` to `TOOL_UNION`.
|
|
106
|
+
- [ ] `frontend/components/settings/system-tools/SystemToolsSettings.svelte`:
|
|
107
|
+
`<ToolInstallCard tool="newengine" title="NewEngine CLI" description="..." />`.
|
|
108
|
+
|
|
109
|
+
### Stage 8 — Chat input
|
|
110
|
+
|
|
111
|
+
If the engine uses one-account-per-engine (Claude/Copilot style), wire it
|
|
112
|
+
into `EngineModelPicker.svelte`:
|
|
113
|
+
|
|
114
|
+
- [ ] Import the new store + add a branch to `accountsForEngine`,
|
|
115
|
+
`accountPickerLabel`, `showAccountPicker`, and the two
|
|
116
|
+
`$effect` blocks (auto-fetch and auto-select active account).
|
|
117
|
+
- [ ] No new dropdown markup is required — the existing dropdown iterates
|
|
118
|
+
`accountsForEngine`.
|
|
119
|
+
|
|
120
|
+
If the engine is multi-provider (OpenCode style) the picker logic is
|
|
121
|
+
heavier — copy the OpenCode block.
|
|
122
|
+
|
|
123
|
+
Other auto wiring:
|
|
124
|
+
- [ ] The engine tab appears automatically in `EngineModelPicker` from
|
|
125
|
+
`ENGINES`.
|
|
126
|
+
- [ ] Verify: select engine → model list appears → send message → stream
|
|
127
|
+
runs.
|
|
128
|
+
|
|
129
|
+
### Stage 9 — Manual QA
|
|
130
|
+
|
|
131
|
+
From the project root:
|
|
132
|
+
|
|
133
|
+
```sh
|
|
134
|
+
bun run check && bun run lint
|
|
135
|
+
```
|
|
136
|
+
|
|
137
|
+
Then the minimum UI scenarios that must pass:
|
|
138
|
+
|
|
139
|
+
- [ ] Settings → System Tools → Install NewEngine CLI → exit 0.
|
|
140
|
+
- [ ] Settings → Engines → NewEngine → Add Account → status `Installed,
|
|
141
|
+
Active account: <name>`.
|
|
142
|
+
- [ ] Chat input → pick NewEngine + model → send a message → assistant
|
|
143
|
+
replies → cancel mid-stream → idle → send again → resume works.
|
|
144
|
+
- [ ] AskUserQuestion (if the SDK supports it) → appears in UI → submit
|
|
145
|
+
answer → stream continues.
|
|
146
|
+
- [ ] Restart Clopen → state survives (account, provider, last-used model
|
|
147
|
+
in chat).
|
|
148
|
+
|
|
149
|
+
---
|
|
150
|
+
|