-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepl.ts
More file actions
963 lines (910 loc) · 34.7 KB
/
Copy pathrepl.ts
File metadata and controls
963 lines (910 loc) · 34.7 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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
// CLI REPL — readline-based interactive loop.
// Spec: docs/DEVELOPMENT_PLAN.md §5
import {
BashTool,
CredentialsStore,
DeepSeekProvider,
EFFORT_PARAMS,
HookDispatcher,
ReadTool,
RuntimeHost,
SessionManager,
TaskManager,
ToolRegistry,
WebFetchTool,
WriteTool,
appendAllowMatcher,
applyStyle,
buildSkillsDescriptionBlock,
closeAllMcpServers,
connectAllMcpServers,
expandMcpResourceRefs,
getMcpPrompt,
mcpPromptCommands,
resolveMcpPromptInvocation,
type McpElicitHandler,
expandCommandBody,
findCustomCommand,
findStyle,
installToolSearch,
loadMemory,
rememberFact,
loadOutputStyles,
loadSettings,
gateUntrustedSettings,
loadSkills,
loadSlashCommands,
contextWindowFor,
makeSkillTool,
resolveCredentials,
settingsPaths,
wirePlugins,
collectPluginContributions,
type Effort,
type McpClientHandle,
type Mode,
type AgentEvent,
type SessionMeta,
type StoredMessage,
type WireResult,
} from '@deepcode/core';
import { createInterface } from 'node:readline/promises';
import type { Readable, Writable } from 'node:stream';
import { CommandRegistry, type SessionContext } from './commands.js';
import { captureVoice } from './voice-capture.js';
import { resolveEffort } from './parse-args.js';
import { TrustStore } from './trust.js';
import { resolveBuiltinSkillsDir } from './builtin-skills.js';
export interface ReplOpts {
input: Readable;
output: Writable;
cwd: string;
/** Override $HOME for tests. */
home?: string;
/** Initial mode (overrides settings). */
mode?: string;
/** Initial model (overrides settings). */
model?: string;
/** Initial effort (overrides settings). */
effort?: Effort;
// M3c CLI flag wiring
/** Replace the default system prompt entirely. */
systemPromptOverride?: string;
/** Append text to the system prompt. */
appendSystemPrompt?: string;
/** Path to a file whose contents are appended to system prompt. */
appendSystemPromptFile?: string;
/** Whitelist of tool names — only these are loaded. */
allowedTools?: string[];
/** Blacklist of tool names — these are removed. */
disallowedTools?: string[];
/** Cap on agent loop turns. */
maxTurns?: number;
// Session resume (--resume / --continue / --fork-session)
/** `--resume` with no id → pick a session interactively. */
resume?: boolean;
/** `--resume <id>` → resume this specific session (append to it). */
resumeId?: string;
/** `--continue` → resume the most recently updated session in this cwd. */
continueSession?: boolean;
/** `--fork-session` → resume into a NEW session, leaving the source intact. */
forkSession?: boolean;
/** `--bare` → suppress the startup banner (scripting / minimal output). */
bare?: boolean;
/** `--no-plugins` → skip discovering + wiring installed plugins. */
noPlugins?: boolean;
/** `--settings <file>` → a settings file that wins over discovered layers. */
settingsPath?: string;
}
const DEFAULT_SYSTEM_PROMPT = `You are DeepCode, an AI coding assistant powered by DeepSeek. Help the user with their codebase using the available tools (Read, Write, Edit, Bash, Grep, Glob). Be concise and accurate. When you modify files, briefly explain what you changed and why.`;
export interface SessionResolution {
session: SessionMeta;
/** Prior messages to seed into the agent's context (empty for a fresh session). */
seededHistory: StoredMessage[];
/** One-line status to print (resumed / forked / fell back to fresh). */
notice?: string;
}
/**
* Decide which session a REPL launch should use:
* - `resumeId` → resume that exact session (append to it)
* - `continueSession` → resume the most-recently-updated session in `cwd`
* - `forkSession` → resume into a NEW session seeded with a copy of the
* source history, leaving the original untouched
* - otherwise → a fresh session
* Pure over a `SessionManager`, so it's unit-testable without a live REPL.
*/
export async function resolveSession(
sessions: SessionManager,
cwd: string,
model: string,
opts: { resumeId?: string; continueSession?: boolean; forkSession?: boolean },
): Promise<SessionResolution> {
let sourceId = opts.resumeId;
if (!sourceId && opts.continueSession) {
// Most recent session in THIS directory (list() is updatedAt-desc).
const inCwd = (await sessions.list()).filter((m) => m.cwd === cwd);
if (inCwd.length === 0) {
return {
session: await sessions.create(cwd, { model }),
seededHistory: [],
notice: 'No previous session in this directory — starting a new one.',
};
}
sourceId = inCwd[0]!.id;
}
if (sourceId) {
const loaded = await sessions.load(sourceId);
if (!loaded) {
return {
session: await sessions.create(cwd, { model }),
seededHistory: [],
notice: `Session ${sourceId} not found — starting a new one.`,
};
}
const n = loaded.messages.length;
const plural = n === 1 ? '' : 's';
if (opts.forkSession) {
const forked = await sessions.create(cwd, {
model: loaded.meta.model ?? model,
title: loaded.meta.title,
});
for (const m of loaded.messages) await sessions.append(forked.id, m);
return {
session: forked,
seededHistory: loaded.messages,
notice: `⎇ Forked ${sourceId} → ${forked.id} (${n} message${plural} copied).`,
};
}
return {
session: loaded.meta,
seededHistory: loaded.messages,
notice: `↻ Resumed ${sourceId} (${n} message${plural}).`,
};
}
return { session: await sessions.create(cwd, { model }), seededHistory: [] };
}
/**
* Interactive `--resume` with no id: list recent sessions and read a choice.
* Returns the chosen session id, or undefined to start fresh.
*/
async function pickSessionId(
sessions: SessionManager,
input: Readable,
output: Writable,
): Promise<string | undefined> {
const list = (await sessions.list()).slice(0, 20);
if (list.length === 0) {
output.write(' No sessions to resume — starting a new one.\n');
return undefined;
}
output.write('\n Resume which session?\n');
list.forEach((m, i) => {
const when = m.updatedAt.slice(0, 16).replace('T', ' ');
const label = m.title?.trim() ? m.title.trim() : m.id;
output.write(` ${String(i + 1).padStart(2)}. ${label} · ${when}\n`);
});
const picker = createInterface({ input, output, terminal: false });
const answer = (await picker.question(' Number (blank = new session): ')).trim();
picker.close();
const n = Number(answer);
if (!Number.isInteger(n) || n < 1 || n > list.length) {
if (answer) output.write(' No match — starting a new one.\n');
return undefined;
}
return list[n - 1]!.id;
}
export async function startRepl(opts: ReplOpts): Promise<number> {
const { output, cwd } = opts;
// Load config + creds. Trust-gate first: in an untrusted directory, project
// /local hooks·mcpServers·apiKeyHelper·statusLine are stripped (the user-global
// layer is always trusted) so a freshly-cloned repo can't run code on launch.
const loaded = await loadSettings({ cwd, home: opts.home, settingsPath: opts.settingsPath });
const trustStore = new TrustStore({ home: opts.home });
const trustStatus = await trustStore.statusFor(cwd);
const { settings, gated } = gateUntrustedSettings(loaded, trustStatus);
if (gated.length > 0) {
output.write(
` ⚠ Untrusted directory — ignoring project ${gated.join(', ')} (can execute code).\n` +
` Run \`deepcode trust\` here to enable them.\n`,
);
}
const credsStore = new CredentialsStore({ home: opts.home });
const creds = await resolveCredentials({
store: credsStore,
apiKeyHelper: settings.apiKeyHelper,
});
if (!creds.apiKey && !creds.authToken) {
output.write(
'No DeepSeek credentials found. Run `deepcode` (no args) to onboard, or set DEEPSEEK_API_KEY.\n',
);
return 1;
}
const model = opts.model ?? settings.model ?? 'deepseek-chat';
const mode = (opts.mode ?? settings.permissions?.defaultMode ?? 'default') as Mode;
// Precedence: --effort flag → DEEPCODE_EFFORT_LEVEL env → settings.effortLevel → default.
// Spec: docs/DEVELOPMENT_PLAN.md §3.13c. (/effort runtime switch and skill
// frontmatter override happen later in the loop, not at construction time.)
const effort = resolveEffort({
cliFlag: opts.effort,
envVar: process.env.DEEPCODE_EFFORT_LEVEL,
settingsLevel: settings.effortLevel,
});
const { maxTokens, temperature } = EFFORT_PARAMS[effort as Effort] ?? EFFORT_PARAMS.medium;
const sessions = new SessionManager();
// Resolve which session to use: resume an explicit id, continue the most
// recent in this cwd, fork either into a new session, or start fresh.
let resumeId = opts.resumeId;
if (opts.resume && !resumeId && !opts.continueSession) {
resumeId = await pickSessionId(sessions, opts.input, output);
}
const resolved = await resolveSession(sessions, cwd, model, {
resumeId,
continueSession: opts.continueSession,
forkSession: opts.forkSession,
});
const session = resolved.session;
if (resolved.notice) output.write(` ${resolved.notice}\n`);
const provider = new DeepSeekProvider({
apiKey: creds.apiKey ?? '',
authToken: creds.authToken,
baseURL: creds.baseURL ?? settings.baseURL,
});
// M3c: --allowedTools / --disallowedTools filtering BEFORE registry construction
let tools: ToolRegistry;
if (opts.allowedTools || opts.disallowedTools) {
const { BUILTIN_TOOLS } = await import('@deepcode/core');
const allowSet = opts.allowedTools ? new Set(opts.allowedTools) : null;
const denySet = new Set(opts.disallowedTools ?? []);
const filtered = BUILTIN_TOOLS.filter((t) => {
if (denySet.has(t.name)) return false;
if (allowSet && !allowSet.has(t.name)) return false;
return true;
});
tools = new ToolRegistry(filtered);
} else {
tools = new ToolRegistry();
}
const commands = new CommandRegistry();
// Trusted+enabled plugins contribute skills / sub-agents / commands (their
// dirs) + MCP servers. Hooks are merged separately by wirePlugins.
// `--no-plugins` skips discovery entirely (faster start / bypass a bad plugin).
const emptyContrib: Awaited<ReturnType<typeof collectPluginContributions>> = {
dirs: [],
mcpServers: {},
};
const pluginContrib = opts.noPlugins
? emptyContrib
: await collectPluginContributions({
home: opts.home,
disabled: settings.disabledPlugins,
});
// Custom prompt-template commands from plugin + user + project commands dirs.
const customCommands = await loadSlashCommands({
cwd,
home: opts.home,
pluginDirs: pluginContrib.dirs,
});
// M5: load memory, skills, output style — assemble final system prompt
const memory = await loadMemory({
cwd,
home: opts.home,
maxBytes: (settings.memoryLoadCapKB ?? 100) * 1024,
});
// Locate built-in skills dir (packaged with @deepcode/core)
const builtinSkillsDir = await resolveBuiltinSkillsDir();
const skills = await loadSkills({
cwd,
home: opts.home,
builtinDir: builtinSkillsDir,
pluginDirs: pluginContrib.dirs,
overrides: settings.skillOverrides,
});
const styles = await loadOutputStyles({ cwd, home: opts.home });
const activeStyle = findStyle(styles, settings.outputStyle ?? 'default');
// Register Skill tool (M5)
if (skills.length > 0) {
tools.register(makeSkillTool(skills));
}
// M3c: connect MCP servers (best-effort; individual failures don't abort)
let mcpServers: McpClientHandle[] = [];
let mcpErrors: Array<{ serverName: string; error: string }> = [];
// Elicitation handler holder — filled once the readline interface exists
// (below). Until then (and there's nothing to elicit at connect time) it
// cancels. Servers see the `elicitation` capability via this passthrough.
const elicitHolder: { fn?: McpElicitHandler } = {};
const elicitForServers: McpElicitHandler = (req) =>
elicitHolder.fn ? elicitHolder.fn(req) : Promise.resolve({ action: 'cancel' });
// Plugin-contributed MCP servers + the user's settings (user wins on a clash).
const allMcpServers = { ...pluginContrib.mcpServers, ...(settings.mcpServers ?? {}) };
if (Object.keys(allMcpServers).length > 0) {
const enabled = settings.enabledMcpjsonServers;
const disabled = settings.disabledMcpjsonServers ?? [];
const result = await connectAllMcpServers(allMcpServers, {
enabledOnly: enabled,
disabled,
elicit: elicitForServers,
});
mcpServers = result.handles;
mcpErrors = result.errors;
// Register MCP tools. Servers default to eager; a server with
// `alwaysLoad: false` (the opt-out) has its tools deferred behind ToolSearch
// so a large toolkit doesn't bloat the tool list — the agent loads them on
// demand. The servers are already connected, so expand() just returns the
// already-built handler.
const deferredMcpTools = [];
for (const handle of mcpServers) {
const defer = settings.mcpServers?.[handle.serverName]?.alwaysLoad === false;
for (const tool of handle.tools) {
if (defer) {
deferredMcpTools.push({
name: tool.name,
description: tool.definition.description,
expand: () => tool,
});
} else {
tools.register(tool);
}
}
}
const deferredNames = installToolSearch(tools, deferredMcpTools);
if (mcpServers.length > 0) {
const eager = mcpServers.reduce((n, h) => n + h.tools.length, 0) - deferredNames.length;
const resourceCount = mcpServers.reduce((n, h) => n + h.resources.length, 0);
const promptCmds = mcpPromptCommands(mcpServers);
output.write(
` ⊞ MCP: ${mcpServers.length} server(s) connected (${eager} tools` +
(deferredNames.length > 0 ? `, ${deferredNames.length} deferred behind ToolSearch` : '') +
(resourceCount > 0 ? `, ${resourceCount} resources` : '') +
(promptCmds.length > 0 ? `, ${promptCmds.length} prompts` : '') +
`)\n`,
);
if (promptCmds.length > 0) {
output.write(` ⊞ MCP prompts: ${promptCmds.map((c) => c.command).join(', ')}\n`);
}
}
if (mcpErrors.length > 0) {
output.write(` ⊞ MCP: ${mcpErrors.length} server(s) failed (see /mcp)\n`);
}
}
// Build the composite system prompt
// M3c: honor --system-prompt (replaces default) + --append-system-prompt /
// --append-system-prompt-file (appended after memory/skills/style).
let systemPrompt = opts.systemPromptOverride ?? DEFAULT_SYSTEM_PROMPT;
if (memory.text) systemPrompt += '\n\n' + memory.text;
const skillsBlock = buildSkillsDescriptionBlock(skills);
if (skillsBlock) systemPrompt += '\n\n' + skillsBlock;
systemPrompt = applyStyle(systemPrompt, activeStyle);
if (opts.appendSystemPrompt) systemPrompt += '\n\n' + opts.appendSystemPrompt;
if (opts.appendSystemPromptFile) {
try {
const { readFile } = await import('node:fs/promises');
systemPrompt += '\n\n' + (await readFile(opts.appendSystemPromptFile, 'utf8'));
} catch (err) {
output.write(`⚠ Could not read --append-system-prompt-file: ${(err as Error).message}\n`);
}
}
// Hook dispatcher (M3 + M3c-ext)
const hooks = new HookDispatcher({
hooks: settings.hooks,
disableAllHooks: settings.disableAllHooks,
allowedHttpHookUrls: settings.allowedHttpHookUrls,
});
// M5.2: wire installed plugins (discover + spawn + merge contributed hooks).
// `--no-plugins` skips this entirely.
let pluginsWire: WireResult | null = null;
if (!opts.noPlugins) {
try {
pluginsWire = await wirePlugins({
home: opts.home,
disabled: settings.disabledPlugins,
hooks,
capabilities: buildPluginCapabilities(cwd),
sandbox: settings.sandbox,
log: (s) => output.write(s + '\n'),
});
} catch (err) {
output.write(` ⊞ Plugins: wire-up failed — ${(err as Error).message}\n`);
}
}
let history: StoredMessage[] = resolved.seededHistory;
const runtime = new RuntimeHost({
provider,
tools,
cwd,
mode,
permissions: settings.permissions,
hooks,
pluginDirs: pluginContrib.dirs,
autoMode: settings.autoMode,
sandboxConfig: settings.sandbox,
});
const ctx: SessionContext = {
cwd,
model,
mode,
effort,
settings,
creds,
credsStore,
userSettingsPath: settingsPaths({ cwd, home: opts.home }).userPath,
home: opts.home,
sessionId: session.id,
sessions,
usage: { inputTokens: 0, outputTokens: 0, reasoningTokens: 0, cacheReadTokens: 0 },
mcpServers,
mcpErrors,
wiredPlugins: pluginsWire?.plugins.map((p) => ({
name: p.plugin.manifest.name,
version: p.plugin.manifest.version,
contributedHookEvents: p.contributedHookEvents,
})),
pluginWarnings: [
...(pluginsWire?.hashMismatches ?? []),
...(pluginsWire?.spawnFailures.map((n) => `${n}: failed to start`) ?? []),
],
initFlow: () => runInitFlow({ cwd, output, rl, provider, model, maxTokens, temperature }),
// M8: /voice records from the mic + transcribes via whisper.cpp, then the
// loop pre-fills the next input line with the transcript (rl is created below).
voiceCapture: () => captureVoice({ rl, output, settings, home: opts.home }),
// M7: /rewind needs access to history + provider.
provider,
history,
};
// Session-scoped background-task manager (M3.15.3 / parity: /tasks, /background).
// ONE manager for the whole REPL session so tasks persist across turns and are
// visible to both the agent (via TaskCreate) and slash commands. Each turn's
// runAgent attaches a richer runner (named sub-agents + SubagentStop). This
// baseline runner only handles `/background` started before the first turn:
// it runs the prompt as a depth-1 sub-agent (clean context, no nested tasks),
// reading ctx.model/ctx.mode live so /model and /mode switches are honored.
const tasks = new TaskManager((spec) => {
const ac = new AbortController();
const done = runtime
.run({
systemPrompt,
userMessage: spec.prompt,
model: ctx.model,
maxTokens,
temperature,
cwd: ctx.cwd,
signal: ac.signal,
modeOverride: ctx.mode as Mode,
subAgentDepth: 1,
systemReminders: false,
})
.then((r) => assistantText(r.history));
return { done, abort: () => ac.abort() };
});
ctx.tasks = tasks;
if (!opts.bare) {
output.write(
`\n ▎ DeepCode · ${ctx.model} · mode: ${ctx.mode} · effort: ${ctx.effort}\n`,
);
output.write(` Working in ${cwd}\n`);
output.write(` Type /help for commands, /exit to quit.\n\n`);
}
const rl = createInterface({ input: opts.input, output, terminal: true });
// Now that readline exists, let MCP servers elicit structured input from the
// user: print the server's message, prompt for each requested field.
elicitHolder.fn = async (req) => {
output.write(`\n ⊞ ${req.server} requests input: ${req.message}\n`);
const props = (req.requestedSchema.properties ?? {}) as Record<
string,
{ description?: string }
>;
const content: Record<string, string> = {};
for (const [key, spec] of Object.entries(props)) {
const label = spec.description ? `${key} (${spec.description})` : key;
const ans = (await rl.question(` ${label}: `)).trim();
if (ans) content[key] = ans;
}
return Object.keys(content).length > 0 ? { action: 'accept', content } : { action: 'cancel' };
};
let ctrlCCount = 0;
rl.on('SIGINT', () => {
ctrlCCount++;
if (ctrlCCount >= 2) {
output.write('\nGoodbye.\n');
rl.close();
return;
}
output.write('\n(Press Ctrl+C again to exit.)\n');
setTimeout(() => {
ctrlCCount = 0;
}, 2000);
});
// Session-lifecycle hooks (the agent loop fires the per-turn ones).
const fireLifecycle = async (
event: 'SessionStart' | 'SessionEnd' | 'Notification',
payload: Record<string, unknown> = {},
): Promise<void> => {
try {
await hooks.dispatch({
event,
cwd: ctx.cwd,
triggeredAt: new Date().toISOString(),
payload,
});
} catch {
/* hook failure must not break the REPL */
}
};
await fireLifecycle('SessionStart', { sessionId: session.id, source: 'cli' });
// Text to inject into the next prompt's input buffer (e.g. a /voice transcript
// the user can edit before submitting). Written right after the prompt renders.
let pendingPrefill: string | undefined;
while (true) {
let userInput: string;
try {
const question = rl.question('› ');
if (pendingPrefill !== undefined) {
rl.write(pendingPrefill);
pendingPrefill = undefined;
}
userInput = await question;
} catch {
break;
}
ctrlCCount = 0;
if (!userInput.trim()) continue;
// `#<text>` — remember a fact to project memory (no agent turn).
if (userInput.trim().startsWith('#')) {
const fact = userInput.trim().slice(1).trim();
if (fact) {
try {
const path = await rememberFact(ctx.cwd, fact, opts.home);
output.write(` ✓ Remembered to ${path}\n\n`);
} catch (e) {
output.write(` ⚠ Could not save memory: ${(e as Error).message}\n\n`);
}
}
continue;
}
// Slash command?
const match = commands.match(userInput);
if (match) {
// Refresh ctx.history snapshot before running — /rewind reads it.
ctx.history = history;
const lines = await Promise.resolve(match.cmd.run(match.args, ctx));
for (const line of lines) output.write(line + '\n');
output.write('\n');
if (ctx.clearHistory) {
history = [];
ctx.clearHistory = false;
}
if (ctx.newHistory) {
history = ctx.newHistory;
ctx.newHistory = undefined;
}
if (ctx.prefillInput) {
pendingPrefill = ctx.prefillInput;
ctx.prefillInput = undefined;
}
if (ctx.exitRequested) break;
continue;
}
// MCP prompt command (`/mcp__<server>__<prompt> [args]`)? Fetch the rendered
// prompt from the server and submit it as the user prompt.
if (userInput.trim().startsWith('/mcp__') && mcpServers.length > 0) {
const inv = resolveMcpPromptInvocation(userInput, mcpServers);
if (inv) {
try {
userInput = await getMcpPrompt(inv.handle, inv.prompt, inv.args);
output.write(` ▸ /mcp__${inv.handle.serverName}__${inv.prompt} (MCP prompt)\n\n`);
} catch (err) {
output.write(` ⚠ MCP prompt failed: ${(err as Error).message}\n`);
continue;
}
}
}
// Custom prompt-template command (.deepcode/commands/<name>.md)? Expand its
// body with the args and submit it to the agent as the user prompt.
if (userInput.trim().startsWith('/')) {
const parts = userInput.trim().split(/\s+/);
const custom = findCustomCommand(customCommands, parts[0]!);
if (custom) {
userInput = expandCommandBody(custom.body, parts.slice(1));
output.write(` ▸ ${custom.name} (${custom.source} command)\n\n`);
}
}
// Expand `@server:scheme://path` MCP resource references — read each resource
// and append its content as a tagged block the model can use.
if (mcpServers.length > 0) {
const { text, resolved, errors } = await expandMcpResourceRefs(userInput, mcpServers);
userInput = text;
for (const r of resolved) output.write(` ⊞ resource @${r.server}:${r.uri}\n`);
for (const e of errors) {
output.write(` ⚠ resource @${e.ref.server}:${e.ref.uri} — ${e.error}\n`);
}
}
// Otherwise: send to agent (with mode/permission/hooks gating from M3b)
const result = await runtime.run({
systemPrompt,
userMessage: userInput,
history,
model: ctx.model,
maxTokens,
temperature,
maxTurns: opts.maxTurns,
cwd: ctx.cwd,
// ctx.sessionId (not the launch `session.id`) so a live `/resume <id>`
// switch redirects new messages to the resumed session.
session: { manager: sessions, id: ctx.sessionId },
modeOverride: ctx.mode as Mode,
autoCompact: { contextWindow: contextWindowFor(ctx.model), threshold: 0.8 },
// Session-scoped manager: the agent's TaskCreate calls land here too, so
// background tasks persist across turns and show up in /tasks.
taskManager: tasks,
approval: async (toolName, _input, verdict) => {
output.write(`\n ⏸ Approve ${toolName}? Reason: ${verdict.reason}\n`);
const answer = (await rl.question(' [y]es / [n]o / [a]lways: ')).trim().toLowerCase();
if (answer === 'a' || answer === 'always') {
// Persist a bare-tool matcher to project-local settings so the next
// run of this tool from this project skips the prompt.
try {
const { localPath } = settingsPaths({ cwd: ctx.cwd });
await appendAllowMatcher(localPath, toolName);
output.write(` ✓ Added "${toolName}" to ${localPath} permissions.allow\n`);
} catch (err) {
output.write(` ⚠ Could not persist always-allow: ${(err as Error).message}\n`);
}
return 'always';
}
return answer === 'y' || answer === 'yes';
},
askUser: async (req) => {
output.write(`\n ❓ ${req.question}\n`);
const opts = req.options ?? [];
opts.forEach((o, i) => {
output.write(` ${i + 1}. ${o.label}${o.description ? ` — ${o.description}` : ''}\n`);
});
if (opts.length === 0) {
return (await rl.question(' Answer: ')).trim();
}
const reply = (
await rl.question(` Pick 1-${opts.length} (or type free text): `)
).trim();
const n = Number(reply);
if (Number.isInteger(n) && n >= 1 && n <= opts.length) {
return opts[n - 1]!.label;
}
return `Other: ${reply}`;
},
onEvent: (e: AgentEvent) => formatEvent(output, e),
});
history = result.history;
ctx.usage.inputTokens += result.usage.inputTokens;
ctx.usage.outputTokens += result.usage.outputTokens;
ctx.usage.reasoningTokens += result.usage.reasoningTokens;
ctx.usage.cacheReadTokens += result.usage.cacheReadTokens;
// M3c-rest: honor ExitPlanMode tool signal — flip plan → default
if (result.modeSignal?.exitPlanMode && ctx.mode === 'plan') {
ctx.mode = 'default';
output.write('\n ▶ Exited plan mode (agent will now execute).\n');
}
// Honor EnterPlanMode tool signal — flip into plan mode (writes blocked).
if (result.modeSignal?.enterPlanMode && ctx.mode !== 'plan') {
ctx.mode = 'plan';
output.write('\n ◐ Entered plan mode (write tools blocked until you exit).\n');
}
output.write('\n');
if (result.stopReason === 'error') {
output.write(' ✕ Error during agent loop. Try again or /status to inspect.\n\n');
}
// Notification hook — the turn finished and control returns to the user.
await fireLifecycle('Notification', {
message: 'DeepCode finished responding — awaiting your input.',
stopReason: result.stopReason,
});
}
await fireLifecycle('SessionEnd', { sessionId: session.id });
rl.close();
// Clean up MCP server connections
if (mcpServers.length > 0) {
await closeAllMcpServers(mcpServers);
}
// Shut down plugin subprocesses
if (pluginsWire) await pluginsWire.shutdown();
return 0;
}
function formatEvent(out: Writable, e: AgentEvent): void {
switch (e.type) {
case 'text_delta':
out.write(e.text);
return;
case 'thinking_delta':
return;
case 'tool_use':
out.write(`\n ● ${e.name} ${formatToolInput(e.input)}\n`);
return;
case 'tool_result':
if (e.result.isError) out.write(` ✕ ${truncate(e.result.content, 200)}\n`);
else out.write(` ✓ ${truncate(e.result.content, 200)}\n`);
return;
case 'usage':
case 'model_step_complete':
return;
case 'error':
out.write(`\n ✕ ${e.error}\n`);
return;
case 'turn_complete':
return;
}
}
/** Flatten an agent run's assistant text — the result of a background task. */
function assistantText(history: StoredMessage[]): string {
return history
.filter((m) => m.role === 'assistant')
.flatMap((m) => m.content)
.filter((b): b is Extract<typeof b, { type: 'text' }> => b.type === 'text')
.map((b) => b.text)
.join('\n')
.trim();
}
function formatToolInput(input: Record<string, unknown>): string {
for (const key of ['file_path', 'command', 'pattern', 'path']) {
const v = input[key];
if (typeof v === 'string') return v;
}
return JSON.stringify(input).slice(0, 80);
}
function truncate(s: string, n: number): string {
return s.length > n ? s.slice(0, n) + '…' : s;
}
/**
* Multi-phase /init flow — scans the project, asks the LLM to draft an
* AGENTS.md, shows the draft, and asks the user to approve. Returns the
* path written, or null if the user said no.
*/
async function runInitFlow(args: {
cwd: string;
output: Writable;
rl: { question: (q: string) => Promise<string> };
provider: DeepSeekProvider;
model: string;
maxTokens?: number;
temperature?: number;
}): Promise<string | null> {
const { cwd, output, rl, provider, model, maxTokens, temperature } = args;
const path = await import('node:path');
const fsp = await import('node:fs/promises');
const target = path.join(cwd, 'AGENTS.md');
// Phase 1: scan
output.write(' ▎ /init — Phase 1/3: scanning project...\n');
const summary = await buildProjectSummary(cwd);
// Phase 2: propose
output.write(' ▎ /init — Phase 2/3: asking model to draft AGENTS.md...\n');
const draft = await draftAgentsMd(provider, model, summary, maxTokens, temperature);
// Phase 3: approve
output.write('\n ▎ Proposed AGENTS.md:\n');
output.write(' ┌─────────────────────────────────────────\n');
for (const line of draft.split('\n').slice(0, 40)) {
output.write(` │ ${line}\n`);
}
if (draft.split('\n').length > 40) output.write(' │ ... (truncated)\n');
output.write(' └─────────────────────────────────────────\n');
let exists = false;
try {
await fsp.access(target);
exists = true;
} catch {
/* none */
}
const verb = exists ? 'Overwrite' : 'Write';
const ans = (await rl.question(` ${verb} ${target}? [y]es / [n]o: `)).trim().toLowerCase();
if (ans !== 'y' && ans !== 'yes') return null;
await fsp.writeFile(target, draft, 'utf8');
return target;
}
async function buildProjectSummary(cwd: string): Promise<string> {
const path = await import('node:path');
const fsp = await import('node:fs/promises');
const parts: string[] = [];
// Top-level listing
try {
const entries = await fsp.readdir(cwd, { withFileTypes: true });
parts.push('Top-level entries:');
for (const e of entries.slice(0, 40)) {
parts.push(` ${e.isDirectory() ? 'd' : '-'} ${e.name}`);
}
} catch {
/* ignore */
}
// Pick up to 3 well-known files
for (const f of ['package.json', 'README.md', 'pyproject.toml', 'Cargo.toml', 'go.mod']) {
try {
const raw = await fsp.readFile(path.join(cwd, f), 'utf8');
parts.push(`\n=== ${f} (first 30 lines) ===`);
parts.push(raw.split('\n').slice(0, 30).join('\n'));
} catch {
/* not present */
}
}
return parts.join('\n');
}
async function draftAgentsMd(
provider: DeepSeekProvider,
model: string,
summary: string,
maxTokens?: number,
temperature?: number,
): Promise<string> {
const sys = `You are drafting an AGENTS.md (the per-project agent-instructions file). Output ONLY the Markdown — no preface, no fences. Sections to include:
1. Project name and one-line description
2. Tech stack
3. How to install / build / test
4. Code style conventions (if discernible)
5. Where the entry points / important files live
6. Any "do/don't" notes specific to this project
Keep it under 80 lines.`;
const r = await provider.runTurn({
model,
systemPrompt: sys,
tools: [],
messages: [
{
role: 'user',
content: [{ type: 'text', text: `Project scan:\n${summary}` }],
},
],
maxTokens: maxTokens ?? 2048,
temperature: temperature ?? 0.3,
});
const text = r.content
.filter((c) => c.type === 'text')
.map((c) => (c as { text: string }).text)
.join('');
return text.trim() || '# AGENTS.md\n\n(The model returned an empty draft.)\n';
}
/**
* Build the capability bridge passed to plugin subprocesses (M5.2).
*
* Each capability invokes the host's existing tool implementation — which
* means plugin calls flow through the SAME read/write/exec gates as the
* agent. (mode + permissions + sandbox come from the ToolContext we pass in.)
*
* NOTE: this bridge does NOT carry mode/permissions yet — that's M5.2-ext.
* Today the plugin's `bash` calls are unsandboxed because we don't have a
* sandboxConfig in ctx here. Callers wanting hardening should set
* settings.sandbox.enabled and pass sandboxConfig in a later iteration.
*/
function buildPluginCapabilities(cwd: string): {
fs_read: (path: string) => Promise<string>;
fs_write: (path: string, content: string) => Promise<void>;
bash: (cmd: string) => Promise<{ stdout: string; stderr: string; exitCode: number }>;
fetch: (url: string, opts?: { method?: string; body?: string }) => Promise<string>;
} {
const ctx = { cwd };
return {
fs_read: async (path: string) => {
const r = await ReadTool.execute({ file_path: path }, ctx);
if (r.isError) throw new Error(r.content);
return r.content;
},
fs_write: async (path: string, content: string) => {
const r = await WriteTool.execute({ file_path: path, content }, ctx);
if (r.isError) throw new Error(r.content);
},
bash: async (cmd: string) => {
const r = await BashTool.execute({ command: cmd }, ctx);
const d = (r.data ?? {}) as { stderr?: string; exitCode?: number };
return {
stdout: r.content ?? '',
stderr: d.stderr ?? '',
exitCode: d.exitCode ?? (r.isError ? 1 : 0),
};
},
fetch: async (url: string, fopts?: { method?: string; body?: string }) => {
void fopts; // method/body deferred — WebFetch is GET-only
const r = await WebFetchTool.execute({ url }, ctx);
if (r.isError) throw new Error(r.content);
return r.content;
},
};
}
/**
* Find the bundled built-in skills directory.
* In dev: <repo>/packages/core/skills/.
* In published package: packaged inside @deepcode/core/skills/.
* Returns undefined if not found.
*/