-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcli.ts
More file actions
1276 lines (1215 loc) · 47.4 KB
/
Copy pathcli.ts
File metadata and controls
1276 lines (1215 loc) · 47.4 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
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env node
// deepdive CLI entry.
//
// deepdive "how does claude's rate limiter work"
// deepdive "..." --model=claude-opus-4-7 --search=brave --out=report.md
// deepdive "..." --deep=2 --concurrency=6 --json
// deepdive --help
//
// Prints the cited markdown report to stdout (or JSON with --json). Progress
// events go to stderr when --verbose is set or DEEPDIVE_VERBOSE=1.
import { writeFileSync } from "node:fs";
import { resolve } from "node:path";
import { fileURLToPath } from "node:url";
import { resolveConfig, type CLIFlags } from "./config.js";
import { parseMaxCost, BudgetExceededError } from "./budget.js";
import { resolveSearchAdapter } from "./search.js";
import { runAgent, type AgentEvent } from "./agent.js";
import { createCache } from "./cache.js";
import { createRobotsCache } from "./robots.js";
import { renderSourcesMarkdown, renderAnswerMarkdown } from "./citations.js";
import { synthesize } from "./synthesize.js";
import { verifyCitations as runVerify, type VerificationReport } from "./verify.js";
import {
formatCostLine,
looksLikeDario,
estimateCost,
type CostEstimate,
} from "./pricing.js";
import {
generateSessionId,
saveSession,
loadSession,
listSessions,
resolveSessionId,
renderSessionsList,
deleteSession,
pruneSessions,
parseDuration,
} from "./sessions.js";
import { renderHtmlReport } from "./html-export.js";
import { assessConfidence, formatConfidenceLine } from "./confidence.js";
import { loadConfigFile, fileConfigToEnv } from "./config-file.js";
import { resolveProfile } from "./profiles.js";
import { completionScript, type Shell } from "./completion.js";
import {
diffSessions,
renderDiffText,
DIFF_NARRATE_SYSTEM,
buildDiffNarrateUser,
} from "./diff.js";
import { callLLM } from "./llm.js";
import {
runDoctor,
renderDoctorText,
renderDoctorJson,
exitCodeFor,
scrubPath,
} from "./doctor.js";
const USAGE = `deepdive — local research agent
Usage:
deepdive "<question>" [flags] Run the research agent
deepdive doctor [flags] Health check — paste the output when filing issues
deepdive sessions ls List saved sessions, newest first
deepdive show <id> Print a saved session's markdown answer
deepdive resume <id> [<question>] Re-synthesize against a saved session's
sources (cheap iteration; no re-fetching)
deepdive continue <id> [<question>] Full agent run seeded with the saved session's
sources (plans + searches + fetches new pages;
saved as a new session linked via parentId)
deepdive export <id> [--format=html|md] Render a saved session as a shareable artifact
(--out=report.html). Format inferred from --out.
deepdive diff <id-a> <id-b> [--narrate] Show how the answer + source set changed between
two saved runs. --narrate adds an LLM summary.
deepdive sessions rm <id> [<id>...] Delete one or more saved sessions
deepdive sessions prune --older-than=30d Delete old sessions (and/or --keep=<n> newest;
--dry-run to preview)
deepdive completion <bash|zsh|fish> Print a shell completion script
deepdive --help Show this help
Flags:
--base-url=<url> LLM endpoint. Default: http://localhost:3456 (dario)
--api-key=<key> LLM API key. Default: dario
--model=<name> Model to use. Default: claude-sonnet-4-6
--plan-model=<name> Override model for planner stage only (cheap option:
claude-haiku-4-5). Default: same as --model.
--synth-model=<name> Override model for synthesizer stage only. Default: same as --model.
--critic-model=<name> Override model for critic stage only (cheap option:
claude-haiku-4-5). Default: same as --model.
--max-cost=<$X.YY> Abort the run before the next LLM call would exceed this
dollar cap. e.g. --max-cost=$0.50 or --max-cost=5.
Env: DEEPDIVE_MAX_COST. Exit code 2 on cap-hit.
--max-tokens=<n> Output max tokens per LLM call. Default: 4096
--search=<adapter> Search adapter: duckduckgo | searxng | brave | tavily | exa |
auto | wikipedia | arxiv | github | hackernews |
stackexchange | pubmed
Default: duckduckgo (no key required). wikipedia, arxiv,
hackernews, stackexchange, and pubmed need no key; github
works keyless (DEEPDIVE_GITHUB_TOKEN raises the limit).
'auto' runs DDG first, Brave fallback (if DEEPDIVE_BRAVE_KEY).
--results-per-query=<n> Results per sub-query. Default: 5
--max-sources=<n> Total sources to fetch. Default: 12
--max-words-per-source=<n> Per-source content cap before synthesis. Default: 2000
--timeout-ms=<ms> Per-fetch (browser) timeout. Default: 30000
--browser-cdp-endpoint=<url> Attach to a running CDP browser (e.g. http://host:9222)
instead of launching Chromium. Skips the Playwright
browser download. Env: DEEPDIVE_BROWSER_CDP_ENDPOINT
--llm-timeout-ms=<ms> Per-LLM-call timeout. Default: 120000 (2 min)
--llm-attempts=<n> Max LLM attempts per call (with exponential
backoff on 5xx/429/network errors). Default: 3
--deep[=<n>] Iterative research: run N additional critic-driven
rounds after the first synthesis. Default when
bare: 2. No deep pass when flag absent.
--profile=<name> Apply a named preset: deep | thorough | fast | cheap |
strict, or one defined in your config file. Layered
beneath env + flags. See ~/.deepdive/config.json.
--concurrency=<n> Parallel fetches. Default: 4
--no-cache Disable the on-disk page cache (default: enabled)
--cache-ttl-ms=<ms> Page cache TTL. Default: 3600000 (1 hour)
--ignore-robots Bypass robots.txt checks (default: respect them)
--no-verify-cites Skip lexical citation verification (default: on)
--strict-cites Exit non-zero if any citation is unsupported
--cite-min-recall=<0..1> Threshold for citation support. Default: 0.4
--no-cost Suppress the end-of-run cost summary on stderr
--include=<paths> Comma-separated list of local files / dirs to
ingest as sources (.pdf, .md, .txt, .html).
PDFs require pdfjs-dist installed.
--pdf-max-pages=<n> Cap pages parsed per PDF. Default: 50
--allow-domain=<list> Comma-separated hostname suffixes to keep
exclusively (e.g. github.com,docs.anthropic.com).
--deny-domain=<list> Comma-separated hostname suffixes to drop
(e.g. pinterest.com,quora.com).
--since=<date|duration> Drop sources published before this — an absolute
date (2024, 2024-06, 2024-06-15) or a duration
(30d, 12h, 2w = that long ago). Sources with no
detectable date are kept. Env: DEEPDIVE_SINCE.
--api-format=<anthropic|openai>
Wire format for the LLM endpoint. Default:
auto-detected from --base-url (api.openai.com,
:11434 (Ollama), :8000 default to openai;
everything else to anthropic).
--tldr Lead the answer with a one-paragraph TL;DR (env: DEEPDIVE_TLDR)
--json Emit a JSON result to stdout instead of markdown
--out=<path> Write the output (markdown or json) to a file too
--format=<html|md> export: output format (default: inferred from --out, else html)
--narrate diff: add a one-shot LLM summary of what changed
--older-than=<dur> sessions prune: age cutoff — 30d, 12h, 90m, 2w
--keep=<n> sessions prune: always retain the newest <n> sessions
--dry-run sessions prune: report what would be deleted, delete nothing
--verbose, -v Stream progress events to stderr
--no-stream Buffer the final answer instead of streaming
tokens to stdout (auto-off for --json and
non-TTY stdout)
--no-sessions Do not persist this run to ~/.deepdive/sessions/
--help, -h Show this help
Environment:
DEEPDIVE_BASE_URL, DEEPDIVE_API_KEY, DEEPDIVE_MODEL, DEEPDIVE_SEARCH,
DEEPDIVE_SEARXNG_URL, DEEPDIVE_BRAVE_KEY, DEEPDIVE_TAVILY_KEY, DEEPDIVE_EXA_KEY,
DEEPDIVE_WIKIPEDIA_LANG, DEEPDIVE_GITHUB_TOKEN, DEEPDIVE_STACKEXCHANGE_SITE,
DEEPDIVE_MAX_SOURCES, DEEPDIVE_FETCH_TIMEOUT_MS, DEEPDIVE_HEADED,
DEEPDIVE_DEEP_ROUNDS, DEEPDIVE_CONCURRENCY, DEEPDIVE_NO_CACHE,
DEEPDIVE_CACHE_DIR, DEEPDIVE_CACHE_TTL_MS, DEEPDIVE_JSON, DEEPDIVE_VERBOSE, DEEPDIVE_TLDR,
DEEPDIVE_LLM_TIMEOUT_MS, DEEPDIVE_LLM_ATTEMPTS,
DEEPDIVE_NO_VERIFY_CITES, DEEPDIVE_STRICT_CITES, DEEPDIVE_CITE_MIN_RECALL,
DEEPDIVE_NO_COST, DEEPDIVE_PRICE_INPUT_PER_MTOK, DEEPDIVE_PRICE_OUTPUT_PER_MTOK,
DEEPDIVE_INCLUDE, DEEPDIVE_PDF_MAX_PAGES,
DEEPDIVE_ALLOW_DOMAIN, DEEPDIVE_DENY_DOMAIN, DEEPDIVE_SINCE, DEEPDIVE_API_FORMAT,
DEEPDIVE_NO_SESSIONS, DEEPDIVE_SESSIONS_DIR, DEEPDIVE_CONFIG
Config file:
~/.deepdive/config.json (override path with DEEPDIVE_CONFIG) — JSON object of
default settings (friendly keys: model, search, deep, concurrency, …), an
optional "profiles" map, and an optional "defaultProfile". Precedence:
CLI flags > env vars > --profile > config-file base > built-in defaults.
`;
interface ParsedArgs {
// For the default research case, this is the user's question. For
// subcommand cases (doctor / sessions / show / resume), this is the
// subcommand verb.
question?: string;
// Subcommand-only — extra positional arguments after the verb.
// Empty for the default research case.
extras: string[];
outPath?: string;
flags: CLIFlags;
help: boolean;
}
// Verbs that accept additional positional arguments. Anything else
// triggers the "wrap your question in quotes" error when more than one
// positional shows up.
const SUBCOMMAND_VERBS = new Set([
"doctor",
"sessions",
"show",
"resume",
"continue",
"export",
"diff",
"completion",
]);
// Exported for unit tests.
export function parseArgs(argv: string[]): ParsedArgs {
const flags: CLIFlags = {};
let question: string | undefined;
const extras: string[] = [];
let outPath: string | undefined;
let help = false;
for (const a of argv) {
if (a === "--help" || a === "-h") {
help = true;
continue;
}
if (a === "--verbose" || a === "-v") {
flags.verbose = true;
continue;
}
if (a === "--no-cache") {
flags.noCache = true;
continue;
}
if (a === "--ignore-robots") {
flags.ignoreRobots = true;
continue;
}
if (a === "--no-verify-cites") {
flags.noVerifyCites = true;
continue;
}
if (a === "--strict-cites") {
flags.strictCites = true;
continue;
}
if (a === "--no-cost") {
flags.noCost = true;
continue;
}
if (a === "--no-sessions") {
flags.noSessions = true;
continue;
}
if (a === "--json") {
flags.json = true;
continue;
}
if (a === "--no-stream") {
flags.noStream = true;
continue;
}
if (a === "--tldr") {
flags.tldr = true;
continue;
}
if (a === "--narrate") {
flags.narrate = true;
continue;
}
if (a === "--dry-run") {
flags.dryRun = true;
continue;
}
if (a === "--deep") {
flags.deepRounds = 2;
continue;
}
const m = /^--([a-z0-9-]+)=(.*)$/.exec(a);
if (m) {
const [, key, rawValue] = m;
const value = rawValue.trim();
switch (key) {
case "base-url":
flags.baseUrl = value;
break;
case "api-key":
flags.apiKey = value;
break;
case "model":
flags.model = value;
break;
case "plan-model":
flags.planModel = value;
break;
case "synth-model":
flags.synthModel = value;
break;
case "critic-model":
flags.criticModel = value;
break;
case "max-cost":
{
const parsed = parseMaxCost(value);
if (parsed === undefined) {
throw new Error(
`--max-cost must be a positive dollar amount (e.g. --max-cost=\$0.50 or --max-cost=5); got: ${value}`,
);
}
flags.maxCostUsd = parsed;
}
break;
case "max-tokens":
flags.maxTokens = parsePositiveInt(value);
break;
case "search":
flags.search = value.toLowerCase();
break;
case "results-per-query":
flags.resultsPerQuery = parsePositiveInt(value);
break;
case "max-sources":
flags.maxSources = parsePositiveInt(value);
break;
case "max-words-per-source":
flags.maxWordsPerSource = parsePositiveInt(value);
break;
case "timeout-ms":
flags.timeoutMs = parsePositiveInt(value);
break;
case "browser-cdp-endpoint":
flags.browserCdpEndpoint = value;
break;
case "llm-timeout-ms":
flags.llmTimeoutMs = parsePositiveInt(value);
break;
case "llm-attempts":
flags.llmAttempts = parsePositiveInt(value);
break;
case "deep":
flags.deepRounds = parseNonNegativeInt(value);
break;
case "concurrency":
flags.concurrency = parsePositiveInt(value);
break;
case "cache-ttl-ms":
flags.cacheTtlMs = parsePositiveInt(value);
break;
case "cite-min-recall":
flags.citeMinRecall = parseUnitFloat(value);
break;
case "pdf-max-pages":
flags.pdfMaxPages = parsePositiveInt(value);
break;
case "include":
flags.include = value
.split(",")
.map((s) => s.trim())
.filter((s) => s.length > 0);
break;
case "allow-domain":
flags.allowDomain = value
.split(",")
.map((s) => s.trim())
.filter((s) => s.length > 0);
break;
case "deny-domain":
flags.denyDomain = value
.split(",")
.map((s) => s.trim())
.filter((s) => s.length > 0);
break;
case "api-format":
if (value !== "anthropic" && value !== "openai") {
throw new Error(
`--api-format must be 'anthropic' or 'openai' (got: ${value})`,
);
}
flags.apiFormat = value;
break;
case "out":
outPath = value;
break;
case "profile":
flags.profile = value;
break;
case "since":
flags.since = value;
break;
case "format":
flags.format = value.toLowerCase();
break;
case "older-than":
flags.olderThan = value;
break;
case "keep":
flags.keep = parseNonNegativeInt(value);
break;
default:
throw new Error(`unknown flag: --${key}`);
}
continue;
}
if (a.startsWith("--")) {
throw new Error(
`flags must be in --key=value form (got: ${a}). See --help.`,
);
}
if (question === undefined) {
question = a;
continue;
}
// Subcommand verbs (doctor / sessions / show / resume) take extra
// positional arguments — capture them. Everything else gets the
// "wrap the question in quotes" error so users don't accidentally
// run an unquoted multi-word question.
if (question !== undefined && SUBCOMMAND_VERBS.has(question)) {
extras.push(a);
continue;
}
throw new Error(
`unexpected positional argument: ${JSON.stringify(a)}. Wrap the question in quotes.`,
);
}
return { question, extras, outPath, flags, help };
}
function parsePositiveInt(s: string): number | undefined {
if (!/^\d+$/.test(s)) return undefined;
const n = Number(s);
return Number.isFinite(n) && n > 0 ? n : undefined;
}
function parseNonNegativeInt(s: string): number | undefined {
if (!/^\d+$/.test(s)) return undefined;
const n = Number(s);
return Number.isFinite(n) && n >= 0 ? n : undefined;
}
function parseUnitFloat(s: string): number | undefined {
if (!/^\d+(\.\d+)?$|^\.\d+$/.test(s)) return undefined;
const n = Number(s);
return Number.isFinite(n) && n >= 0 && n <= 1 ? n : undefined;
}
function renderEvent(e: AgentEvent): string {
switch (e.type) {
case "plan.start":
return ` plan planning sub-queries for: ${ellipsize(e.question, 60)}`;
case "plan.done":
return ` plan ${e.plan.queries.length} sub-queries`;
case "round.start":
return ` round ${e.round === 0 ? "initial" : "deep " + e.round} · ${e.queries.length} quer${e.queries.length === 1 ? "y" : "ies"}`;
case "search.start":
return ` search ${e.query}`;
case "search.done":
return ` ${e.count} result${e.count === 1 ? "" : "s"}`;
case "fetch.start":
return ` fetch ${e.cached ? "(cached) " : ""}${e.url}`;
case "fetch.done":
return ` ${e.ok ? "OK " : "!! "}${e.status} · ${e.words} words${e.cached ? " · cache" : ""}`;
case "fetch.skipped":
return ` fetch skipped (${e.reason}) ${e.url}`;
case "include.done":
return ` include ${e.ingested} ingested${e.skipped ? ` · ${e.skipped} skipped` : ""}`;
case "synthesize.start":
return ` synth round ${e.round} · ${e.sourceCount} source${e.sourceCount === 1 ? "" : "s"}`;
case "synthesize.done":
return ` synth round ${e.round} done`;
case "critique.start":
return ` critic reviewing round ${e.round}`;
case "critique.done":
return e.critique.done
? ` critic answer complete (${e.critique.reasoning || "no reasoning given"})`
: ` critic ${e.critique.queries.length} follow-up quer${e.critique.queries.length === 1 ? "y" : "ies"}`;
case "verify.done": {
const r = e.report;
if (r.unsupported.length === 0) {
return ` verify ${r.supportedCitations}/${r.totalCitations} citations supported`;
}
const lines = [
` verify ⚠ ${r.totalCitations - r.supportedCitations}/${r.totalCitations} citations weak (threshold ${r.threshold})`,
];
for (const c of r.unsupported) {
const worst = c.unsupportedIds
.map((id) => `[${id}] ${c.recallByCite[id].toFixed(2)}`)
.join(", ");
lines.push(` ⚠ "${ellipsize(c.sentence, 80)}" — ${worst}`);
}
return lines.join("\n");
}
case "llm.call":
return ` llm ${e.phase.padEnd(8)} ${e.inputTokens} in / ${e.outputTokens} out`;
}
}
function ellipsize(s: string, max: number): string {
return s.length <= max ? s : s.slice(0, max - 1) + "…";
}
// Renders the end-of-run cost summary line for stderr. Two-line variant
// when the LLM endpoint looks like dario (the "$0 on Max" hint applies),
// one-line otherwise. Exported for unit tests.
export function renderCostSummary(
cost: CostEstimate,
model: string,
baseUrl: string,
): string {
const head = "cost · " + formatCostLine(cost, model);
if (!looksLikeDario(baseUrl)) return head;
return head + "\n (≈ at API list price; $0 on Claude Max via dario)";
}
/**
* Multi-model cost summary. Used when the run spread requests across
* models (v0.10.0 per-stage overrides). When only one model was used,
* delegates to `renderCostSummary` so the output is identical to
* pre-v0.10.0. When two or three models were used, prints the aggregate
* line followed by one indented per-model line so the operator can see
* where the dollars actually went.
*/
export function renderMultiModelCostSummary(
cost: import("./pricing.js").MultiModelCostEstimate,
baseUrl: string,
): string {
if (cost.byModel.length <= 1) {
const single = cost.byModel[0]?.model ?? "(no calls)";
return renderCostSummary(cost, single, baseUrl);
}
// Aggregate line with no model name (multiple); each per-model line
// breaks out tokens + dollars.
const head = "cost · " + formatCostLine(cost, "multi-model");
const breakdown = cost.byModel
.map((m) => " · " + formatCostLine(m.estimate, m.model))
.join("\n");
const lines = [head, breakdown];
if (looksLikeDario(baseUrl)) {
lines.push(" (≈ at API list price; $0 on Claude Max via dario)");
}
return lines.join("\n");
}
// Renders a small markdown footer when the verification report has any
// unsupported citations. Returns "" otherwise so clean runs stay clean.
// Exported for unit tests.
export function renderCitationHealthFooter(
report: VerificationReport | undefined,
): string {
if (!report || report.unsupported.length === 0) return "";
const weak = report.totalCitations - report.supportedCitations;
return (
`\n## Citation health\n\n` +
`⚠ ${weak} of ${report.totalCitations} citations have low lexical support ` +
`in their cited source (threshold ${report.threshold}). ` +
`Run with \`--verbose\` to see which.\n`
);
}
// Exported for unit tests. User-facing error rendering at the CLI boundary.
// Runs the error message through scrubPath so the user's home directory can
// never end up in a bug report.
export function safeErrorMessage(err: unknown): string {
const raw =
err instanceof Error ? err.message : String(err ?? "unknown error");
return scrubPath(raw);
}
// Matches the escaping used by renderAnswerMarkdown for the H1 heading — keep
// the streaming header identical to the buffered one so users can diff them.
function escapeHeader(s: string): string {
return s.replace(/[\r\n]+/g, " ").replace(/\[/g, "(").replace(/\]/g, ")");
}
async function main(argv: string[]): Promise<number> {
let parsed: ParsedArgs;
try {
parsed = parseArgs(argv);
} catch (err) {
process.stderr.write(`deepdive: ${safeErrorMessage(err)}\n\n${USAGE}`);
return 2;
}
if (parsed.help) {
process.stdout.write(USAGE);
return 0;
}
// `completion` needs no config; everything else gets config-file + profile
// defaults layered beneath env (real env wins). Do this before any
// resolveConfig so all subcommands see the same effective settings.
if (parsed.question === "completion") {
return completionCommand(parsed);
}
const cfgErr = applyConfigToEnv(parsed.flags);
if (cfgErr) {
process.stderr.write(`deepdive: ${cfgErr}\n`);
return 2;
}
if (parsed.question === "doctor") {
const config = resolveConfig(parsed.flags, process.env);
const useColor = process.stdout.isTTY && !process.env.NO_COLOR;
const report = await runDoctor({ config, env: process.env });
const out = config.jsonOutput
? renderDoctorJson(report)
: renderDoctorText(report, { color: useColor }) + "\n";
process.stdout.write(out);
return exitCodeFor(report);
}
if (parsed.question === "sessions") {
return await sessionsCommand(parsed);
}
if (parsed.question === "show") {
return await showCommand(parsed);
}
if (parsed.question === "resume") {
return await resumeCommand(parsed);
}
if (parsed.question === "continue") {
return await continueCommand(parsed);
}
if (parsed.question === "export") {
return await exportCommand(parsed);
}
if (parsed.question === "diff") {
return await diffCommand(parsed);
}
if (!parsed.question) {
process.stderr.write(`deepdive: missing question.\n\n${USAGE}`);
return 2;
}
const config = resolveConfig(parsed.flags, process.env);
return await runResearch({ question: parsed.question, parsed, config });
}
// Layer config-file base + selected-profile settings into process.env, filling
// only keys the real environment hasn't already set — so the effective
// precedence is: CLI flags > env vars > profile > config-file base > defaults.
// Returns an error string for a fatal problem (unknown profile); a malformed
// config file is a non-fatal warning. Mutating process.env keeps every
// downstream resolveConfig(flags, process.env) call config-aware with no
// plumbing changes.
function applyConfigToEnv(flags: CLIFlags): string | undefined {
const loaded = loadConfigFile(process.env);
if (loaded.error) {
process.stderr.write(`deepdive: warning: ${loaded.error}; ignoring config file\n`);
}
const profileName = flags.profile ?? loaded.defaultProfile;
let profileEnv: Record<string, string> = {};
if (profileName) {
try {
profileEnv = fileConfigToEnv(resolveProfile(profileName, loaded.profiles));
} catch (err) {
return safeErrorMessage(err);
}
}
// Profile wins over the file base within the file layer.
const fileEnv = { ...fileConfigToEnv(loaded.base), ...profileEnv };
for (const [k, v] of Object.entries(fileEnv)) {
if (process.env[k] === undefined) process.env[k] = v;
}
return undefined;
}
function completionCommand(parsed: ParsedArgs): number {
const shell = parsed.extras[0];
if (shell !== "bash" && shell !== "zsh" && shell !== "fish") {
process.stderr.write(
`deepdive: completion requires a shell: bash | zsh | fish\n` +
` e.g. source <(deepdive completion bash)\n`,
);
return 2;
}
process.stdout.write(completionScript(shell as Shell));
return 0;
}
// v0.12.0 — the shared research path used by both the default
// `deepdive "<question>"` invocation and `deepdive continue <id>`.
// Continue threads `preKept` (saved sources from the parent session)
// and `parentId` (lineage backlink for the new record) without
// duplicating the streaming / JSON / persistence / cost / SIGINT
// plumbing that the default path already implements.
interface RunResearchOptions {
question: string;
parsed: ParsedArgs;
config: import("./config.js").RuntimeConfig;
preKept?: import("./synthesize.js").SourceWithContent[];
parentId?: string;
}
async function runResearch(opts: RunResearchOptions): Promise<number> {
const { question, parsed, config, preKept, parentId } = opts;
// A --since value that was supplied but didn't parse is a user error — fail
// loud rather than silently running with no recency filter.
if (config.sinceRaw && config.sinceMs === undefined) {
process.stderr.write(
`deepdive: --since must be a date (2024, 2024-06, 2024-06-15) or a duration ` +
`(30d, 12h, 2w); got: ${config.sinceRaw}\n`,
);
return 2;
}
const search = await resolveSearchAdapter(config.searchAdapter, process.env);
const cache = config.cache.enabled
? createCache({ dir: config.cache.dir, ttlMs: config.cache.ttlMs })
: undefined;
const ac = new AbortController();
const sigint = () => ac.abort();
process.on("SIGINT", sigint);
process.on("SIGTERM", sigint);
// Live-streaming requires an attached TTY: escape sequences, partial line
// writes, and interactive buffering behave weirdly when stdout is a pipe.
// Env-var-only: tests can set FORCE_TTY=1 to exercise the streaming path
// without a real terminal.
const streaming =
config.streamEnabled &&
(process.stdout.isTTY || process.env.DEEPDIVE_FORCE_STREAM === "1");
let streamed = false;
try {
if (streaming) {
process.stdout.write(`# ${escapeHeader(question)}\n\n`);
}
const result = await runAgent(
question,
{
llm: config.llm,
models: config.models,
maxCostUsd: config.maxCostUsd,
preKept,
search,
browser: config.browser,
resultsPerQuery: config.resultsPerQuery,
maxSources: config.maxSources,
maxWordsPerSource: config.maxWordsPerSource,
deepRounds: config.deepRounds,
concurrency: config.concurrency,
cache,
respectRobots: config.respectRobots,
// Per-run in-memory robots.txt cache so each origin's robots.txt is
// fetched once, not once per URL (dropped previously — the CLI never
// supplied one, so canFetch's cache-miss path re-fetched every time).
robotsCache: createRobotsCache(),
verifyCitations: config.verifyCitations,
citeMinRecall: config.citeMinRecall,
pdfMaxPages: config.pdfMaxPages,
include: config.include,
domainFilter: config.domainFilter,
tldr: config.tldr,
sinceMs: config.sinceMs,
env: process.env,
onEvent: (e) => {
if (config.verbose) process.stderr.write(renderEvent(e) + "\n");
// In --deep streaming mode, prefix each round-after-the-first
// synth with a separator + header so users can tell where one
// draft ends and the next begins. Round 0's header is the
// question (already printed above).
if (
streaming &&
e.type === "synthesize.start" &&
e.round > 0
) {
process.stdout.write(
`\n\n---\n\n## Round ${e.round} (deep)\n\n`,
);
}
},
onSynthesizeToken: streaming
? (chunk) => {
streamed = true;
process.stdout.write(chunk);
}
: undefined,
},
ac.signal,
);
const citeFooter = renderCitationHealthFooter(result.verification);
const strictFail =
config.strictCitations &&
(result.verification?.unsupported.length ?? 0) > 0;
// Persist the session before printing output. We do this even when
// --strict-cites is going to make us exit 1 — the run happened, the
// sources are real, and the user might want to inspect via `show`.
let sessionId: string | undefined;
if (config.sessions.enabled) {
try {
sessionId = await persistSession(question, result, config, parentId);
} catch (err) {
// Persistence failure is non-fatal — surface a warning to stderr
// but don't break the run.
process.stderr.write(
`deepdive: warning: failed to save session (${safeErrorMessage(err)})\n`,
);
}
}
// Cost summary lands on stderr regardless of stdout mode (suppressed by
// --no-cost / DEEPDIVE_NO_COST=1, and skipped for --json since the data
// is in the JSON envelope already).
// v0.10.0: result.cost is a MultiModelCostEstimate (superset of
// CostEstimate). Per-stage models → multi-line breakdown; single
// model → identical output to pre-v0.10.0.
const costLine =
config.costEnabled && !config.jsonOutput
? renderMultiModelCostSummary(result.cost, config.llm.baseUrl)
: "";
// Coverage/confidence signal — computed always (goes into --json), shown on
// stderr alongside the cost summary (suppressed by --no-cost / --json).
const confidence = assessConfidence({
sources: result.usage.kept,
citationsTotal: result.usage.citationsTotal,
citationsSupported: result.usage.citationsSupported,
});
const confidenceLine =
config.costEnabled && !config.jsonOutput ? formatConfidenceLine(confidence) : "";
if (streaming && streamed) {
// Streaming mode already wrote the header + answer tokens. Close with
// the sources block, optional citation-health footer, and (if
// requested) write the full markdown to the output file too.
const tail = "\n\n" + renderSourcesMarkdown(result.sources) + citeFooter;
process.stdout.write(tail);
if (!tail.endsWith("\n")) process.stdout.write("\n");
if (parsed.outPath) {
const path = resolve(parsed.outPath);
writeFileSync(path, result.markdown + citeFooter, "utf-8");
process.stderr.write(`\nwrote ${path}\n`);
}
if (costLine) process.stderr.write(costLine + "\n");
if (confidenceLine) process.stderr.write(confidenceLine + "\n");
if (sessionId) writeSessionHint(sessionId);
return strictFail ? 1 : 0;
}
const output = config.jsonOutput
? JSON.stringify(
{
question: result.question,
plan: result.plan,
rounds: result.rounds,
sources: result.sources.map((s) => ({
id: s.id,
url: s.url,
title: s.title,
fetchedAt: s.fetchedAt,
publishedAt: s.publishedAt,
})),
answer: result.answer,
verification: result.verification,
cost: result.cost,
usage: result.usage,
confidence,
},
null,
2,
) + "\n"
: result.markdown +
(result.markdown.endsWith("\n") ? "" : "\n") +
citeFooter;
process.stdout.write(output);
if (parsed.outPath) {
const path = resolve(parsed.outPath);
writeFileSync(path, output, "utf-8");
process.stderr.write(`\nwrote ${path}\n`);
}
if (costLine) process.stderr.write(costLine + "\n");
if (confidenceLine) process.stderr.write(confidenceLine + "\n");
if (sessionId) writeSessionHint(sessionId);
return strictFail ? 1 : 0;
} catch (err) {
// v0.11.0 — distinct exit code for "we deliberately stopped because
// the budget cap was hit". Wrapping scripts can branch on `=== 2`
// (cap) vs `=== 1` (real error).
if (err instanceof BudgetExceededError) {
process.stderr.write(`deepdive: ${err.message}\n`);
return 2;
}
process.stderr.write(`deepdive: ${safeErrorMessage(err)}\n`);
return 1;
} finally {
process.off("SIGINT", sigint);
process.off("SIGTERM", sigint);
}
}
// Persist a finished agent run as a session record. Returns the new id.
// v0.12.0 — pass `parentId` to record the lineage when this run was
// invoked via `deepdive continue <id>`.
async function persistSession(
question: string,
result: import("./agent.js").AgentResult,
config: import("./config.js").RuntimeConfig,
parentId?: string,
): Promise<string> {
const id = generateSessionId();
const record = {
schema: 1 as const,
id,
createdAt: Date.now(),
question,
plan: result.plan,
rounds: result.rounds,
sources: result.sources,
answer: result.answer,
verification: result.verification,
cost: result.cost,
llm: { baseUrl: config.llm.baseUrl, model: config.llm.model },
...(parentId ? { parentId } : {}),
};
await saveSession(record, { dir: config.sessions.dir });
return id;
}
function writeSessionHint(id: string): void {
process.stderr.write(`session ${id} (deepdive resume ${id})\n`);
}
async function sessionsCommand(parsed: ParsedArgs): Promise<number> {
const config = resolveConfig(parsed.flags, process.env);
const sub = parsed.extras[0] ?? "ls";
switch (sub) {
case "ls":
return await sessionsLs(config);
case "rm":
return await sessionsRm(parsed, config);
case "prune":
return await sessionsPrune(parsed, config);
default:
process.stderr.write(
`deepdive: unknown sessions sub-command: ${sub} (try: ls | rm | prune)\n`,
);
return 2;
}
}
async function sessionsLs(
config: import("./config.js").RuntimeConfig,
): Promise<number> {
const { sessions, bad } = await listSessions({ dir: config.sessions.dir });
if (config.jsonOutput) {
process.stdout.write(JSON.stringify({ sessions, bad }, null, 2) + "\n");
return 0;
}
process.stdout.write(renderSessionsList(sessions) + "\n");
if (bad.length > 0) {
process.stderr.write(
`\n(${bad.length} session file${bad.length === 1 ? "" : "s"} could not be parsed)\n`,
);
}
return 0;
}
async function sessionsRm(
parsed: ParsedArgs,
config: import("./config.js").RuntimeConfig,
): Promise<number> {
const idArgs = parsed.extras.slice(1);
if (idArgs.length === 0) {
process.stderr.write(
`deepdive: sessions rm requires at least one session id (try \`deepdive sessions ls\`)\n`,
);
return 2;
}
let failed = 0;
for (const idArg of idArgs) {
try {
const id = await resolveSessionId(idArg, { dir: config.sessions.dir });
await deleteSession(id, { dir: config.sessions.dir });
process.stdout.write(`removed ${id}\n`);
} catch (err) {
failed++;
process.stderr.write(`deepdive: ${safeErrorMessage(err)}\n`);
}
}
return failed > 0 ? 1 : 0;
}
async function sessionsPrune(
parsed: ParsedArgs,
config: import("./config.js").RuntimeConfig,
): Promise<number> {
const { olderThan, keep, dryRun } = parsed.flags;
if (olderThan === undefined && keep === undefined) {
process.stderr.write(
`deepdive: sessions prune needs --older-than=<dur> and/or --keep=<n>\n` +
` e.g. deepdive sessions prune --older-than=30d\n` +
` deepdive sessions prune --keep=20\n` +
` deepdive sessions prune --older-than=7d --keep=5 --dry-run\n`,
);
return 2;
}
let olderThanMs: number | undefined;
if (olderThan !== undefined) {
olderThanMs = parseDuration(olderThan);
if (olderThanMs === undefined) {
process.stderr.write(
`deepdive: --older-than must be a duration like 30d, 12h, 90m, 2w (got: ${olderThan})\n`,
);
return 2;
}
}