-
Notifications
You must be signed in to change notification settings - Fork 10.5k
Expand file tree
/
Copy pathindex.ts
More file actions
524 lines (499 loc) · 17.2 KB
/
Copy pathindex.ts
File metadata and controls
524 lines (499 loc) · 17.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
import { randomUUID } from 'node:crypto';
import { createInterface } from 'node:readline';
import type { Readable } from 'node:stream';
import type { Context } from '@deepseek-ai/cordis';
import type {} from '@deepseek-ai/cordis-plugin-loader';
import {
installModelSelection,
type AgentHandle,
type ModelSelectionRef,
} from '@deepseek-ai/dsh-agent';
import type {} from '@deepseek-ai/dsh-agent-default-model';
import {
createUserMessage,
ReasoningEffortId,
type ContentBlock,
type TokenUsage,
} from '@deepseek-ai/dsh-llm';
import { SessionId, type SessionEvent, type TurnEndReason } from '@deepseek-ai/dsh-session';
import type {} from '@deepseek-ai/dsh-cmdline';
import {
CAPABILITIES,
identityFrame,
modelsFrame,
parseHostCommand,
type ExecuteCommand,
type ModelCatalogEntry,
} from './protocol.js';
export const name = 'open-design-runtime';
export const inject = [
'openDesignStartup',
'agentDefaultModel',
'agents',
'llm',
'sessions',
'sessionPersistence',
];
const PLUGIN_VERSION = '0.1.0';
type Output = { write(chunk: string): unknown };
type ExitFallbackTimer = { unref(): unknown };
type ExitFallbackScheduler = (callback: () => void, delayMs: number) => ExitFallbackTimer;
type CancellationLatch = {
activate(requestId: string, controller: AbortController): void;
cancel(requestId: string): void;
};
const PROCESS_EXIT_FALLBACK_MS = 1_000;
function writeFrame(output: Output, frame: unknown): void {
output.write(`${JSON.stringify(frame)}\n`);
}
function errorFacts(error: unknown, fallbackCode: string): { code: string; message: string } {
const candidate = typeof error === 'object' && error !== null
? error as { code?: unknown; message?: unknown }
: undefined;
const code = typeof candidate?.code === 'string' && candidate.code !== ''
? candidate.code
: fallbackCode;
if (error instanceof Error && error.message !== '') return { code, message: error.message };
if (typeof candidate?.message === 'string' && candidate.message !== '') {
return { code, message: candidate.message };
}
if (typeof error === 'string' && error !== '') return { code, message: error };
return { code, message: 'DeepSeek Harness reported an execution error.' };
}
function contentText(content: readonly ContentBlock[]): string {
const text: string[] = [];
const visit = (blocks: readonly ContentBlock[]) => {
for (const block of blocks) {
if (block.type === 'text' || block.type === 'reasoning') text.push(block.text);
if (block.type === 'tool-result') visit(block.content);
}
};
visit(content);
return text.join('');
}
function resultStatus(reason: TurnEndReason | undefined): 'completed' | 'cancelled' | 'failed' {
if (reason?.kind === 'completed' || reason?.kind === 'max-tokens') return 'completed';
if (reason?.kind === 'aborted') return 'cancelled';
return 'failed';
}
function resultError(reason: TurnEndReason | undefined): { code: string; message: string } | undefined {
if (resultStatus(reason) !== 'failed') return undefined;
if (!reason) {
return {
code: 'DSH_PROFILE_MISSING_TURN_END',
message: 'DeepSeek Harness became idle without reporting how the turn ended.',
};
}
if (reason.kind === 'error') return errorFacts(reason.error, 'DSH_PROFILE_TURN_FAILED');
if (reason.kind === 'blocked') {
return {
code: 'DSH_PROFILE_TURN_BLOCKED',
message: 'DeepSeek Harness blocked the turn before it completed.',
};
}
return {
code: 'DSH_PROFILE_TURN_FAILED',
message: `DeepSeek Harness ended the turn with reason "${reason.kind}".`,
};
}
function terminalOutput(output: string): { output: string } | Record<string, never> {
return output === '' ? {} : { output };
}
function writeCancelledResult(output: Output, requestId: string, sessionId: string): void {
writeFrame(output, {
v: 1,
type: 'result',
request_id: requestId,
status: 'cancelled',
session_id: sessionId,
resume_rejected: false,
});
}
function requestProfileExit(
exit: (code: number) => void,
forceExit: () => void = () => process.exit(0),
schedule: ExitFallbackScheduler = (callback, delayMs) => setTimeout(callback, delayMs),
): void {
exit(0);
// rc.6 can finish root disposal before its launcher-owned file watchers are
// attached, leaving a one-shot profile alive when the host keeps stdin open.
// The agent handle and session flush have already settled here, so retain a
// bounded process fallback below OD's three-second cancellation grace.
schedule(forceExit, PROCESS_EXIT_FALLBACK_MS).unref();
}
function createCancellationLatch(cancelActiveAgent: () => void): CancellationLatch {
const pendingRequestIds = new Set<string>();
let activeRequest: { requestId: string; controller: AbortController } | undefined;
const cancelActive = () => {
if (!activeRequest || activeRequest.controller.signal.aborted) return;
activeRequest.controller.abort();
cancelActiveAgent();
};
return {
activate(requestId, controller) {
activeRequest = { requestId, controller };
if (pendingRequestIds.delete(requestId)) cancelActive();
},
cancel(requestId) {
if (activeRequest?.requestId === requestId) {
cancelActive();
return;
}
// The host can cancel during the child-spawn/profile-handshake window,
// before the execute line has created its AbortController. Retain that
// request-scoped intent and replay it as soon as execute becomes active.
pendingRequestIds.add(requestId);
},
};
}
function usageFrame(requestId: string, provider: string, model: string, usage: TokenUsage) {
return {
v: 1,
type: 'usage',
request_id: requestId,
provider,
model,
input_tokens: usage.inputTokens,
output_tokens: usage.outputTokens,
...(usage.cacheReadTokens === undefined ? {} : { cache_read_tokens: usage.cacheReadTokens }),
...(usage.cacheWriteTokens === undefined ? {} : { cache_write_tokens: usage.cacheWriteTokens }),
};
}
async function listModelCatalog(ctx: Context): Promise<ModelCatalogEntry[]> {
const catalog: ModelCatalogEntry[] = [];
for (const provider of ctx.llm.listProviders()) {
try {
for (const model of await ctx.llm.listModels(provider.id)) {
const resolved = await ctx.llm.resolveModelInfo(provider.id, model.id).catch(() => null);
catalog.push({
provider: provider.id,
provider_name: provider.name,
id: model.id,
name: model.name,
...(resolved?.reasoning?.efforts.length
? {
reasoning_options: resolved.reasoning.efforts.map((effort) => ({
id: String(effort.id),
name: effort.name,
...(resolved.reasoning?.defaultEffort === effort.id ? { default: true } : {}),
})),
}
: {}),
});
}
} catch {
ctx.logger.warn(`open-design-runtime: could not list models for provider "${provider.id}"`);
}
}
return catalog;
}
function emitSessionEvent(
output: Output,
request: ExecuteCommand,
provider: string,
model: string,
event: SessionEvent,
): void {
switch (event.type) {
case 'assistant/chunk': {
const chunk = event.data.chunk;
if (chunk.type === 'text-delta' && chunk.text !== '') {
writeFrame(output, { v: 1, type: 'text', request_id: request.request_id, content: chunk.text });
} else if (chunk.type === 'reasoning-delta' && chunk.text !== '') {
writeFrame(output, { v: 1, type: 'thinking', request_id: request.request_id, content: chunk.text });
}
return;
}
case 'tool/call':
writeFrame(output, {
v: 1,
type: 'tool_call',
request_id: request.request_id,
call_id: String(event.data.callId),
name: event.data.name,
arguments: event.data.arguments,
});
return;
case 'tool/result':
writeFrame(output, {
v: 1,
type: 'tool_result',
request_id: request.request_id,
call_id: String(event.data.message.content[0].toolCallId),
name: event.data.error?.name ?? 'tool',
output: contentText(event.data.message.content[0].content),
is_error: event.data.message.content[0].isError === true,
});
return;
case 'assistant/message':
if (event.data.usage) writeFrame(output, usageFrame(request.request_id, provider, model, event.data.usage));
return;
default:
return;
}
}
async function execute(
ctx: Context,
request: ExecuteCommand,
output: Output,
onHandle: (handle: AgentHandle | undefined) => void,
signal: AbortSignal,
): Promise<void> {
// Model selection derivation (provider/id/resume-id → resolved selection)
// runs before the try below, so a host-supplied value that the harness
// rejects (e.g. an unknown reasoning_effort) would throw OUTSIDE the
// handler's error path and reject the task promise with no result frame —
// `serve` would then exit the process silently and the host would wait on
// a response that never comes. Derive inside its own guard and emit an
// explicit failed frame so the protocol contract (every execute gets
// exactly one result) holds even for malformed selections.
let selection: ModelSelectionRef['current'];
try {
const defaultSelection = ctx.agentDefaultModel.currentSelection();
const baseSelection = request.model
? { provider: request.model.provider, model: request.model.id }
: defaultSelection;
selection = request.reasoning_effort
? { ...baseSelection, reasoningEffort: ReasoningEffortId(request.reasoning_effort) }
: baseSelection;
} catch (error: unknown) {
writeFrame(output, {
v: 1,
type: 'result',
request_id: request.request_id,
status: 'failed',
session_id: String(SessionId(request.resume_session_id ?? `od-${randomUUID()}`)),
resume_rejected: false,
error: errorFacts(error, 'DSH_PROFILE_INVALID_MODEL_SELECTION'),
});
return;
}
const sessionId = SessionId(request.resume_session_id ?? `od-${randomUUID()}`);
let handle: AgentHandle | undefined;
let firstSeq = Number.POSITIVE_INFINITY;
let turnEnd: SessionEvent<'turn/end'> | undefined;
let assistantOutput = '';
const setup = (agentCtx: Context) => {
const selected: ModelSelectionRef = { current: selection, assembled: undefined };
installModelSelection(agentCtx, selected);
};
let disposeEvent = () => {};
const onSessionEvent = (session: { id: unknown }, event: SessionEvent) => {
if (String(session.id) !== String(sessionId) || event.seq < firstSeq) return;
emitSessionEvent(output, request, selection.provider, selection.model, event);
if (event.type === 'assistant/chunk' && event.data.chunk.type === 'text-delta') {
assistantOutput += event.data.chunk.text;
}
if (event.type === 'turn/end') turnEnd = event;
};
try {
try {
handle = request.resume_session_id
? await ctx.agents.resume({ resumeSessionId: sessionId, agentOptions: selection, setup, signal })
: await ctx.agents.create({
sessionId,
meta: { cwd: request.cwd },
agentOptions: selection,
setup,
signal,
});
onHandle(handle);
} catch (error: unknown) {
if (signal.aborted) {
writeCancelledResult(output, request.request_id, String(sessionId));
return;
}
const facts = errorFacts(error, request.resume_session_id
? 'DSH_PROFILE_RESUME_REJECTED'
: 'DSH_PROFILE_SESSION_CREATE_FAILED');
writeFrame(output, {
v: 1,
type: 'result',
request_id: request.request_id,
status: 'failed',
session_id: String(sessionId),
resume_rejected: Boolean(request.resume_session_id),
error: facts,
});
return;
}
writeFrame(output, {
v: 1,
type: 'session',
request_id: request.request_id,
session_id: String(sessionId),
resumed: Boolean(request.resume_session_id),
});
await handle.agent.whenIdle();
if (signal.aborted) {
writeCancelledResult(output, request.request_id, String(sessionId));
return;
}
firstSeq = handle.agent.session.seq + 1;
disposeEvent = ctx.on('session/event', onSessionEvent);
await handle.agent.followup(createUserMessage({
content: [{ type: 'text', text: request.prompt }],
source: { kind: 'user' },
}));
await handle.agent.whenIdle();
await ctx.sessions.flush(handle.agent.session);
if (signal.aborted) {
writeCancelledResult(output, request.request_id, String(sessionId));
return;
}
const reason = turnEnd?.data.reason;
const status = resultStatus(reason);
const failed = resultError(reason);
writeFrame(output, {
v: 1,
type: 'result',
request_id: request.request_id,
status,
session_id: String(sessionId),
...terminalOutput(assistantOutput),
stop_reason: reason?.kind ?? 'unknown',
resume_rejected: false,
...(failed === undefined ? {} : { error: failed }),
});
} catch (error: unknown) {
if (signal.aborted) {
writeCancelledResult(output, request.request_id, String(sessionId));
return;
}
writeFrame(output, {
v: 1,
type: 'result',
request_id: request.request_id,
status: 'failed',
session_id: String(sessionId),
resume_rejected: false,
error: errorFacts(error, 'DSH_PROFILE_EXECUTION_FAILED'),
});
} finally {
disposeEvent();
await handle?.dispose().catch(() => undefined);
onHandle(undefined);
}
}
async function serve(
ctx: Context,
output: Output,
exit: (code: number) => void,
input: Readable = process.stdin,
finishProfile: (exitCallback: (code: number) => void) => void = requestProfileExit,
): Promise<void> {
writeFrame(output, identityFrame('ready', PLUGIN_VERSION));
const lines = createInterface({ input, crlfDelay: Infinity });
let requestId: string | null = null;
let handle: AgentHandle | undefined;
let task: Promise<void> | undefined;
const cancellation = createCancellationLatch(() => {
handle?.agent.cancel({ kind: 'user' });
});
await new Promise<void>((resolve) => {
let settled = false;
const settle = () => {
if (settled) return;
settled = true;
lines.close();
input.destroy();
resolve();
};
lines.on('line', (line) => {
if (line.trim() === '') return;
let command;
try {
command = parseHostCommand(JSON.parse(line) as unknown);
} catch {
writeFrame(output, {
v: 1,
type: 'protocol_error',
...(requestId ? { request_id: requestId } : {}),
code: 'DSH_PROFILE_INVALID_COMMAND',
message: 'OpenDesign sent an invalid profile command.',
});
return;
}
if (command.type === 'cancel') {
cancellation.cancel(command.request_id);
return;
}
if (task) {
writeFrame(output, {
v: 1,
type: 'protocol_error',
request_id: command.request_id,
code: 'DSH_PROFILE_BUSY',
message: 'This profile process accepts exactly one execute command.',
});
return;
}
requestId = command.request_id;
const taskAbort = new AbortController();
cancellation.activate(requestId, taskAbort);
task = execute(
ctx,
command,
output,
(nextHandle) => { handle = nextHandle; },
taskAbort.signal,
);
// Defense in depth: `execute` reports every failure via a result frame
// and resolves, so a rejection here would mean a bug in that contract.
// Still convert it into an explicit failed frame instead of an
// unhandled rejection (which would make the process exit silently and
// leave the host waiting on a result that never arrives).
void task
.catch((error: unknown) => {
writeFrame(output, {
v: 1,
type: 'result',
request_id: requestId ?? command.request_id,
status: 'failed',
session_id: `od-${randomUUID()}`,
resume_rejected: false,
error: errorFacts(error, 'DSH_PROFILE_EXECUTION_FAILED'),
});
})
.finally(settle);
});
lines.on('close', () => {
if (!task) settle();
});
});
finishProfile(exit);
}
export function apply(ctx: Context): void {
const startup = ctx.openDesignStartup;
const exit = ctx.get('appExit');
if (!startup || !exit) throw new Error('open-design-runtime requires startup and appExit services');
if (startup.mode === 'probe') {
writeFrame(process.stdout, identityFrame('probe', PLUGIN_VERSION));
exit(0);
return;
}
void ctx.get('loader')?.await().then(async () => {
if (startup.mode === 'models') {
writeFrame(process.stdout, modelsFrame(await listModelCatalog(ctx)));
exit(0);
return;
}
await serve(ctx, process.stdout, exit);
}).catch((error: unknown) => {
process.stderr.write(`open-design-runtime: ${error instanceof Error ? error.message : String(error)}\n`);
exit(1);
});
}
export const internals = {
contentText,
createCancellationLatch,
errorFacts,
emitSessionEvent,
execute,
listModelCatalog,
requestProfileExit,
resultStatus,
resultError,
serve,
terminalOutput,
};