@duckmind/dm-windows-x64 0.60.6 → 0.60.9
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/dm.exe +0 -0
- package/extensions/.dm-extensions.json +70 -123
- package/extensions/dm-9router-ext/src/index.js +410 -5
- package/extensions/dm-caveman/extensions/caveman.js +283 -12
- package/extensions/dm-cliproxy/index.js +182 -2
- package/extensions/dm-cliproxy/scripts/check-config-migration.js +66 -8
- package/extensions/dm-cliproxy/src/apply.js +228 -1
- package/extensions/dm-cliproxy/src/cache.js +42 -1
- package/extensions/dm-cliproxy/src/commands.js +50 -2
- package/extensions/dm-cliproxy/src/compat.js +81 -1
- package/extensions/dm-cliproxy/src/config.js +192 -2
- package/extensions/dm-cliproxy/src/conflicts.js +46 -1
- package/extensions/dm-cliproxy/src/fetch-models.js +190 -1
- package/extensions/dm-cliproxy/src/fetch-usage.js +41 -1
- package/extensions/dm-cliproxy/src/log.js +23 -1
- package/extensions/dm-cliproxy/src/status-quota.js +77 -1
- package/extensions/dm-cliproxy/src/ui-frame.js +50 -1
- package/extensions/dm-cliproxy/src/ui-hub/hub.js +199 -2
- package/extensions/dm-cliproxy/src/ui-hub/index.js +26 -2
- package/extensions/dm-cliproxy/src/ui-hub/shell.js +46 -1
- package/extensions/dm-cliproxy/src/ui-hub/view-diagnostics.js +69 -1
- package/extensions/dm-cliproxy/src/ui-hub/view-models.js +379 -1
- package/extensions/dm-cliproxy/src/ui-hub/view-usage.js +106 -1
- package/extensions/dm-cliproxy/src/ui-picker/catalog.js +41 -1
- package/extensions/dm-cliproxy/src/ui-picker/mutate.js +115 -1
- package/extensions/dm-cliproxy/src/ui-picker/prompt-confirm.js +35 -1
- package/extensions/dm-cliproxy/src/ui-picker/prompt-name.js +62 -1
- package/extensions/dm-cliproxy/src/ui-picker/providers.js +45 -1
- package/extensions/dm-cliproxy/src/ui-picker/render-text.js +34 -1
- package/extensions/dm-cliproxy/src/ui-picker/rows.js +57 -1
- package/extensions/dm-cliproxy/src/ui-setup.js +260 -2
- package/extensions/dm-cliproxy/src/ui-usage.js +151 -1
- package/extensions/dm-cliproxy/src/usage-shared-cache.js +97 -1
- package/extensions/dm-context/src/context.js +144 -1
- package/extensions/dm-context/src/index.js +339 -7
- package/extensions/dm-context/src/utils.js +9 -1
- package/extensions/dm-cua/bin/browser-cua.mjs +73 -8
- package/extensions/dm-cua/index.js +75 -6
- package/extensions/dm-cua/src/browser-cua-lib.mjs +490 -6
- package/extensions/dm-cua/src/browser-install.mjs +331 -2
- package/extensions/dm-fff/src/index.js +688 -12
- package/extensions/dm-fff/src/query.js +60 -1
- package/extensions/dm-goal/src/goal.js +894 -23
- package/extensions/dm-image2/index.js +103 -9
- package/extensions/dm-image2/src/image-lib.mjs +1275 -8
- package/extensions/dm-subagents/install.mjs +62 -8
- package/extensions/dm-subagents/src/agents/agent-management.js +1190 -36
- package/extensions/dm-subagents/src/agents/agent-memory.js +216 -6
- package/extensions/dm-subagents/src/agents/agent-scope.js +5 -1
- package/extensions/dm-subagents/src/agents/agent-selection.js +20 -1
- package/extensions/dm-subagents/src/agents/agent-serializer.js +120 -5
- package/extensions/dm-subagents/src/agents/agents.js +1139 -11
- package/extensions/dm-subagents/src/agents/chain-serializer.js +299 -11
- package/extensions/dm-subagents/src/agents/frontmatter.js +65 -6
- package/extensions/dm-subagents/src/agents/identity.js +29 -1
- package/extensions/dm-subagents/src/agents/proactive-skills.js +141 -1
- package/extensions/dm-subagents/src/agents/skills.js +614 -6
- package/extensions/dm-subagents/src/extension/config.js +35 -2
- package/extensions/dm-subagents/src/extension/control-notices.js +69 -4
- package/extensions/dm-subagents/src/extension/doctor.js +172 -15
- package/extensions/dm-subagents/src/extension/fanout-child.js +158 -231
- package/extensions/dm-subagents/src/extension/index.js +521 -359
- package/extensions/dm-subagents/src/extension/rpc.js +266 -7
- package/extensions/dm-subagents/src/extension/schemas.js +275 -1
- package/extensions/dm-subagents/src/extension/tool-description.js +111 -6
- package/extensions/dm-subagents/src/intercom/intercom-bridge.js +126 -4
- package/extensions/dm-subagents/src/intercom/native-supervisor-channel.js +452 -5
- package/extensions/dm-subagents/src/intercom/result-intercom.js +319 -3
- package/extensions/dm-subagents/src/profiles/profiles.js +458 -3
- package/extensions/dm-subagents/src/runs/background/async-execution.js +834 -40
- package/extensions/dm-subagents/src/runs/background/async-job-tracker.js +435 -14
- package/extensions/dm-subagents/src/runs/background/async-resume.js +334 -8
- package/extensions/dm-subagents/src/runs/background/async-status.js +313 -12
- package/extensions/dm-subagents/src/runs/background/chain-append.js +245 -2
- package/extensions/dm-subagents/src/runs/background/chain-root-attachment.js +136 -1
- package/extensions/dm-subagents/src/runs/background/completion-batcher.js +94 -1
- package/extensions/dm-subagents/src/runs/background/completion-dedupe.js +54 -1
- package/extensions/dm-subagents/src/runs/background/control-channel.js +190 -1
- package/extensions/dm-subagents/src/runs/background/fleet-view.js +483 -17
- package/extensions/dm-subagents/src/runs/background/notify.js +129 -3
- package/extensions/dm-subagents/src/runs/background/parallel-groups.js +34 -1
- package/extensions/dm-subagents/src/runs/background/result-watcher.js +236 -6
- package/extensions/dm-subagents/src/runs/background/run-id-resolver.js +76 -4
- package/extensions/dm-subagents/src/runs/background/run-status.js +427 -23
- package/extensions/dm-subagents/src/runs/background/scheduled-runs.js +487 -4
- package/extensions/dm-subagents/src/runs/background/stale-run-reconciler.js +306 -9
- package/extensions/dm-subagents/src/runs/background/subagent-runner.js +2849 -73
- package/extensions/dm-subagents/src/runs/background/top-level-async.js +5 -1
- package/extensions/dm-subagents/src/runs/background/wait.js +206 -11
- package/extensions/dm-subagents/src/runs/foreground/chain-clarify.js +1013 -12
- package/extensions/dm-subagents/src/runs/foreground/chain-execution.js +980 -101
- package/extensions/dm-subagents/src/runs/foreground/execution.js +1165 -45
- package/extensions/dm-subagents/src/runs/foreground/subagent-executor.js +3157 -222
- package/extensions/dm-subagents/src/runs/shared/acceptance.js +835 -3
- package/extensions/dm-subagents/src/runs/shared/chain-outputs.js +104 -1
- package/extensions/dm-subagents/src/runs/shared/completion-guard.js +116 -3
- package/extensions/dm-subagents/src/runs/shared/dm-args.js +208 -1
- package/extensions/dm-subagents/src/runs/shared/dm-spawn.js +90 -1
- package/extensions/dm-subagents/src/runs/shared/dynamic-fanout.js +282 -1
- package/extensions/dm-subagents/src/runs/shared/long-running-guard.js +148 -1
- package/extensions/dm-subagents/src/runs/shared/mcp-direct-tool-allowlist.js +305 -1
- package/extensions/dm-subagents/src/runs/shared/model-fallback.js +194 -1
- package/extensions/dm-subagents/src/runs/shared/model-scope.js +65 -1
- package/extensions/dm-subagents/src/runs/shared/nested-events.js +851 -8
- package/extensions/dm-subagents/src/runs/shared/nested-path.js +41 -1
- package/extensions/dm-subagents/src/runs/shared/nested-render.js +105 -1
- package/extensions/dm-subagents/src/runs/shared/parallel-utils.js +81 -4
- package/extensions/dm-subagents/src/runs/shared/run-history.js +51 -4
- package/extensions/dm-subagents/src/runs/shared/single-output.js +149 -8
- package/extensions/dm-subagents/src/runs/shared/structured-output.js +58 -1
- package/extensions/dm-subagents/src/runs/shared/subagent-control.js +166 -5
- package/extensions/dm-subagents/src/runs/shared/subagent-prompt-runtime.js +329 -13
- package/extensions/dm-subagents/src/runs/shared/tool-budget.js +73 -1
- package/extensions/dm-subagents/src/runs/shared/turn-budget.js +47 -4
- package/extensions/dm-subagents/src/runs/shared/workflow-graph.js +196 -1
- package/extensions/dm-subagents/src/runs/shared/worktree.js +435 -3
- package/extensions/dm-subagents/src/shared/artifacts.js +92 -2
- package/extensions/dm-subagents/src/shared/atomic-json.js +55 -1
- package/extensions/dm-subagents/src/shared/child-transcript.js +167 -5
- package/extensions/dm-subagents/src/shared/file-coalescer.js +25 -1
- package/extensions/dm-subagents/src/shared/fork-context.js +147 -4
- package/extensions/dm-subagents/src/shared/formatters.js +98 -7
- package/extensions/dm-subagents/src/shared/jsonl-writer.js +56 -2
- package/extensions/dm-subagents/src/shared/model-info.js +62 -1
- package/extensions/dm-subagents/src/shared/post-exit-stdio-guard.js +68 -1
- package/extensions/dm-subagents/src/shared/session-identity.js +6 -1
- package/extensions/dm-subagents/src/shared/session-tokens.js +39 -2
- package/extensions/dm-subagents/src/shared/settings.js +198 -11
- package/extensions/dm-subagents/src/shared/status-format.js +53 -1
- package/extensions/dm-subagents/src/shared/types.js +184 -6
- package/extensions/dm-subagents/src/shared/utils.js +462 -2
- package/extensions/dm-subagents/src/slash/prompt-template-bridge.js +288 -1
- package/extensions/dm-subagents/src/slash/prompt-workflows.js +297 -7
- package/extensions/dm-subagents/src/slash/slash-bridge.js +118 -1
- package/extensions/dm-subagents/src/slash/slash-commands.js +1287 -31
- package/extensions/dm-subagents/src/slash/slash-live-state.js +240 -4
- package/extensions/dm-subagents/src/tui/render-helpers.js +64 -1
- package/extensions/dm-subagents/src/tui/render.js +1542 -4
- package/extensions/dm-usage/index.js +1294 -9
- package/extensions/greedysearch-dm/bin/cdp-greedy.mjs +40 -9
- package/extensions/greedysearch-dm/bin/cdp-headless.mjs +5 -2
- package/extensions/greedysearch-dm/bin/cdp-visible.mjs +5 -2
- package/extensions/greedysearch-dm/bin/cdp.mjs +896 -30
- package/extensions/greedysearch-dm/bin/gschrome.mjs +30 -2
- package/extensions/greedysearch-dm/bin/kill-visible.mjs +7 -2
- package/extensions/greedysearch-dm/bin/launch-visible.mjs +13 -2
- package/extensions/greedysearch-dm/bin/launch.mjs +282 -10
- package/extensions/greedysearch-dm/bin/mcp.mjs +386 -361
- package/extensions/greedysearch-dm/bin/search.mjs +620 -540
- package/extensions/greedysearch-dm/bin/visible.mjs +22 -2
- package/extensions/greedysearch-dm/extractors/bing-copilot.mjs +329 -579
- package/extensions/greedysearch-dm/extractors/chatgpt.mjs +301 -583
- package/extensions/greedysearch-dm/extractors/common.mjs +408 -32
- package/extensions/greedysearch-dm/extractors/consensus.mjs +376 -365
- package/extensions/greedysearch-dm/extractors/consent.mjs +303 -14
- package/extensions/greedysearch-dm/extractors/gemini.mjs +228 -592
- package/extensions/greedysearch-dm/extractors/google-ai.mjs +78 -499
- package/extensions/greedysearch-dm/extractors/logically.mjs +270 -347
- package/extensions/greedysearch-dm/extractors/perplexity.mjs +243 -581
- package/extensions/greedysearch-dm/extractors/selectors.mjs +32 -1
- package/extensions/greedysearch-dm/extractors/semantic-scholar.mjs +130 -317
- package/extensions/greedysearch-dm/index.js +123 -23
- package/extensions/greedysearch-dm/src/fetcher.mjs +576 -2
- package/extensions/greedysearch-dm/src/formatters/results.js +95 -10
- package/extensions/greedysearch-dm/src/formatters/sources.js +57 -1
- package/extensions/greedysearch-dm/src/formatters/synthesis.js +49 -1
- package/extensions/greedysearch-dm/src/github.mjs +222 -7
- package/extensions/greedysearch-dm/src/reddit.mjs +145 -14
- package/extensions/greedysearch-dm/src/search/browser-lifecycle.mjs +340 -9
- package/extensions/greedysearch-dm/src/search/challenge-detect.mjs +112 -4
- package/extensions/greedysearch-dm/src/search/chrome.mjs +486 -285
- package/extensions/greedysearch-dm/src/search/constants.mjs +109 -8
- package/extensions/greedysearch-dm/src/search/defaults.mjs +10 -1
- package/extensions/greedysearch-dm/src/search/engines.mjs +79 -9
- package/extensions/greedysearch-dm/src/search/fetch-source.mjs +441 -349
- package/extensions/greedysearch-dm/src/search/file-sources.mjs +29 -7
- package/extensions/greedysearch-dm/src/search/minimize.mjs +86 -1
- package/extensions/greedysearch-dm/src/search/output.mjs +51 -5
- package/extensions/greedysearch-dm/src/search/paths.mjs +48 -1
- package/extensions/greedysearch-dm/src/search/pdf.mjs +63 -2
- package/extensions/greedysearch-dm/src/search/port-pid.mjs +69 -1
- package/extensions/greedysearch-dm/src/search/progress.mjs +109 -2
- package/extensions/greedysearch-dm/src/search/query.mjs +21 -1
- package/extensions/greedysearch-dm/src/search/recovery.mjs +49 -1
- package/extensions/greedysearch-dm/src/search/research.mjs +2227 -458
- package/extensions/greedysearch-dm/src/search/scale-aware.mjs +61 -11
- package/extensions/greedysearch-dm/src/search/simple-research.mjs +396 -805
- package/extensions/greedysearch-dm/src/search/sources.mjs +412 -1
- package/extensions/greedysearch-dm/src/search/synthesis-runner.mjs +127 -12
- package/extensions/greedysearch-dm/src/search/synthesis.mjs +202 -12
- package/extensions/greedysearch-dm/src/tools/greedy-search-handler.js +209 -23
- package/extensions/greedysearch-dm/src/tools/shared.js +226 -10
- package/extensions/greedysearch-dm/src/utils/content.mjs +35 -4
- package/extensions/greedysearch-dm/src/utils/helpers.js +22 -1
- package/extensions/greedysearch-dm/src/utils/node-runtime.mjs +10 -1
- package/extensions/greedysearch-dm/src/utils/system-cmds.mjs +61 -1
- package/package.json +1 -1
|
@@ -1 +1,151 @@
|
|
|
1
|
-
|
|
1
|
+
const C = {
|
|
2
|
+
reset: "\x1B[0m",
|
|
3
|
+
bold: "\x1B[1m",
|
|
4
|
+
dim: "\x1B[2m",
|
|
5
|
+
accent: "\x1B[38;5;111m",
|
|
6
|
+
muted: "\x1B[38;5;245m",
|
|
7
|
+
dim2: "\x1B[38;5;240m",
|
|
8
|
+
green: "\x1B[38;5;114m",
|
|
9
|
+
yellow: "\x1B[38;5;179m",
|
|
10
|
+
red: "\x1B[38;5;167m",
|
|
11
|
+
greenBg: "\x1B[48;5;22m",
|
|
12
|
+
yellowBg: "\x1B[48;5;94m",
|
|
13
|
+
redBg: "\x1B[48;5;52m"
|
|
14
|
+
};
|
|
15
|
+
const BAR_WIDTH = 18;
|
|
16
|
+
const MAX_LABEL_WIDTH = 28;
|
|
17
|
+
export function renderUsage(doc, opts = {}) {
|
|
18
|
+
const lines = [];
|
|
19
|
+
const byProvider = new Map;
|
|
20
|
+
let hiddenDisabled = 0;
|
|
21
|
+
for (const a of doc.accounts) {
|
|
22
|
+
if (!opts.showDisabled && a.disabled) {
|
|
23
|
+
hiddenDisabled++;
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
const arr = byProvider.get(a.provider) ?? [];
|
|
27
|
+
arr.push(a);
|
|
28
|
+
byProvider.set(a.provider, arr);
|
|
29
|
+
}
|
|
30
|
+
lines.push(`${C.dim}generated ${doc.generatedAt}${C.reset}`);
|
|
31
|
+
if (doc.unsupportedProviders.length > 0) {
|
|
32
|
+
lines.push(`${C.dim}providers without quota lookup:${C.reset} ${doc.unsupportedProviders.join(", ")}`);
|
|
33
|
+
}
|
|
34
|
+
if (hiddenDisabled > 0) {
|
|
35
|
+
lines.push(`${C.dim}hidden disabled accounts:${C.reset} ${hiddenDisabled} ${C.dim2}(press d to show)${C.reset}`);
|
|
36
|
+
}
|
|
37
|
+
for (const [provider, accounts] of Array.from(byProvider.entries()).sort()) {
|
|
38
|
+
lines.push("");
|
|
39
|
+
lines.push(`${C.bold}${C.accent}▌ ${provider}${C.reset}`);
|
|
40
|
+
for (const a of accounts) {
|
|
41
|
+
lines.push(formatAccountHeader(a));
|
|
42
|
+
if (!a.supported) {
|
|
43
|
+
lines.push(` ${C.dim2}(provider not quota-aware)${C.reset}`);
|
|
44
|
+
continue;
|
|
45
|
+
}
|
|
46
|
+
if (a.error) {
|
|
47
|
+
if (opts.verbose) {
|
|
48
|
+
for (const ln of wrapText(a.error, 88)) {
|
|
49
|
+
lines.push(` ${C.red}${ln}${C.reset}`);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
continue;
|
|
53
|
+
}
|
|
54
|
+
const groups = a.groups ?? [];
|
|
55
|
+
if (groups.length === 0) {
|
|
56
|
+
lines.push(` ${C.dim2}(no active quota windows)${C.reset}`);
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
for (const g of groups)
|
|
60
|
+
lines.push(formatGroup(g));
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
lines.push("");
|
|
64
|
+
lines.push(`${C.dim}legend:${C.reset} ` + `${ICON_OK_DOT} ok ` + `${ICON_WARN} error/unavailable ` + `${ICON_DISABLED} disabled ` + `${C.green}✓N${C.reset}/${C.red}✗N${C.reset} request counters ` + `${C.dim}v = verbose, d = show disabled${C.reset}`);
|
|
65
|
+
return lines;
|
|
66
|
+
}
|
|
67
|
+
function wrapText(s, max) {
|
|
68
|
+
if (s.length <= max)
|
|
69
|
+
return [s];
|
|
70
|
+
const out = [];
|
|
71
|
+
let rest = s;
|
|
72
|
+
while (rest.length > max) {
|
|
73
|
+
let cut = rest.lastIndexOf(" ", max);
|
|
74
|
+
if (cut < max * 0.6)
|
|
75
|
+
cut = max;
|
|
76
|
+
out.push(rest.slice(0, cut));
|
|
77
|
+
rest = rest.slice(cut).replace(/^\s+/, "");
|
|
78
|
+
}
|
|
79
|
+
if (rest)
|
|
80
|
+
out.push(rest);
|
|
81
|
+
return out;
|
|
82
|
+
}
|
|
83
|
+
function formatAccountHeader(a) {
|
|
84
|
+
const status = accountStatus(a);
|
|
85
|
+
const counters = `${C.green}✓${a.success}${C.reset} ${C.red}✗${a.failed}${C.reset}`;
|
|
86
|
+
const label = truncate(a.label, 36);
|
|
87
|
+
return ` ${status} ${C.bold}${label}${C.reset} ${counters}`;
|
|
88
|
+
}
|
|
89
|
+
const ICON_OK_DOT = `${C.green}●${C.reset}`;
|
|
90
|
+
const ICON_WARN = `${C.yellow}⚠${C.reset}`;
|
|
91
|
+
const ICON_DISABLED = `${C.dim2}⊘${C.reset}`;
|
|
92
|
+
function accountStatus(a) {
|
|
93
|
+
if (a.disabled)
|
|
94
|
+
return ICON_DISABLED;
|
|
95
|
+
if (a.unavailable || a.error || a.status && a.status !== "active") {
|
|
96
|
+
return ICON_WARN;
|
|
97
|
+
}
|
|
98
|
+
return ICON_OK_DOT;
|
|
99
|
+
}
|
|
100
|
+
function formatGroup(g) {
|
|
101
|
+
const f = clamp(g.remainingFraction);
|
|
102
|
+
const pct = Math.round(f * 100);
|
|
103
|
+
const bar = renderBar(f);
|
|
104
|
+
const reset = g.resetTime ? ` ${C.dim}reset ${humanizeReset(g.resetTime)}${C.reset}` : "";
|
|
105
|
+
const label = truncate(g.label, MAX_LABEL_WIDTH).padEnd(MAX_LABEL_WIDTH, " ");
|
|
106
|
+
return ` ${bar} ${formatPct(pct, f)} ${label}${reset}`;
|
|
107
|
+
}
|
|
108
|
+
function renderBar(fraction) {
|
|
109
|
+
const f = clamp(fraction);
|
|
110
|
+
const filled = Math.round(f * BAR_WIDTH);
|
|
111
|
+
const color = colorForFraction(f);
|
|
112
|
+
const dimColor = C.dim2;
|
|
113
|
+
const full = "█".repeat(filled);
|
|
114
|
+
const empty = "░".repeat(BAR_WIDTH - filled);
|
|
115
|
+
return `${dimColor}[${color}${full}${dimColor}${empty}]${C.reset}`;
|
|
116
|
+
}
|
|
117
|
+
function formatPct(pct, fraction) {
|
|
118
|
+
const color = colorForFraction(fraction);
|
|
119
|
+
return `${color}${String(pct).padStart(3)}%${C.reset}`;
|
|
120
|
+
}
|
|
121
|
+
function colorForFraction(f) {
|
|
122
|
+
if (f >= 0.4)
|
|
123
|
+
return C.green;
|
|
124
|
+
if (f >= 0.15)
|
|
125
|
+
return C.yellow;
|
|
126
|
+
return C.red;
|
|
127
|
+
}
|
|
128
|
+
function humanizeReset(iso) {
|
|
129
|
+
const t = Date.parse(iso);
|
|
130
|
+
if (!Number.isFinite(t))
|
|
131
|
+
return iso;
|
|
132
|
+
const ms = t - Date.now();
|
|
133
|
+
if (ms <= 0)
|
|
134
|
+
return "now";
|
|
135
|
+
const min = Math.round(ms / 60000);
|
|
136
|
+
if (min < 60)
|
|
137
|
+
return `${min}m`;
|
|
138
|
+
const hr = Math.round(min / 60);
|
|
139
|
+
if (hr < 48)
|
|
140
|
+
return `${hr}h`;
|
|
141
|
+
const d = Math.round(hr / 24);
|
|
142
|
+
return `${d}d`;
|
|
143
|
+
}
|
|
144
|
+
function clamp(f) {
|
|
145
|
+
return Math.max(0, Math.min(1, f));
|
|
146
|
+
}
|
|
147
|
+
function truncate(s, max) {
|
|
148
|
+
if (s.length <= max)
|
|
149
|
+
return s;
|
|
150
|
+
return `${s.slice(0, max - 1)}…`;
|
|
151
|
+
}
|
|
@@ -1 +1,97 @@
|
|
|
1
|
-
import
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import {
|
|
3
|
+
existsSync,
|
|
4
|
+
readFileSync,
|
|
5
|
+
statSync,
|
|
6
|
+
unlinkSync,
|
|
7
|
+
writeFileSync
|
|
8
|
+
} from "node:fs";
|
|
9
|
+
import { dirname, join } from "node:path";
|
|
10
|
+
import {
|
|
11
|
+
CONFIG_DIR,
|
|
12
|
+
ensurePrivateDirectory,
|
|
13
|
+
ensurePrivateFile
|
|
14
|
+
} from "./config.js";
|
|
15
|
+
import { log } from "./log.js";
|
|
16
|
+
export const USAGE_CACHE_PATH = join(CONFIG_DIR, "usage-cache.json");
|
|
17
|
+
export const USAGE_LOCK_PATH = join(CONFIG_DIR, "usage-cache.lock");
|
|
18
|
+
export const USAGE_CACHE_TTL_MS = 120000;
|
|
19
|
+
const LOCK_STALE_MS = 30000;
|
|
20
|
+
export function usageCachePaths(endpoint, resolvedUsageKey, configDir = CONFIG_DIR) {
|
|
21
|
+
const scope = createHash("sha256").update(endpoint.trim().replace(/\/+$/, "")).update("\x00").update(resolvedUsageKey).digest("hex").slice(0, 24);
|
|
22
|
+
return {
|
|
23
|
+
cachePath: join(configDir, `usage-cache-${scope}.json`),
|
|
24
|
+
lockPath: join(configDir, `usage-cache-${scope}.lock`)
|
|
25
|
+
};
|
|
26
|
+
}
|
|
27
|
+
export function readUsageCache(cachePath = USAGE_CACHE_PATH) {
|
|
28
|
+
if (!existsSync(cachePath))
|
|
29
|
+
return null;
|
|
30
|
+
try {
|
|
31
|
+
ensurePrivateDirectory(dirname(cachePath));
|
|
32
|
+
ensurePrivateFile(cachePath);
|
|
33
|
+
const env = JSON.parse(readFileSync(cachePath, "utf8"));
|
|
34
|
+
if (!env || typeof env.fetchedAt !== "number" || !env.doc || !Array.isArray(env.doc.accounts)) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
return { doc: env.doc, ageMs: Date.now() - env.fetchedAt };
|
|
38
|
+
} catch (err) {
|
|
39
|
+
log.warn("failed to read usage cache:", err.message);
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
export function isUsageFresh(ageMs) {
|
|
44
|
+
return ageMs < USAGE_CACHE_TTL_MS;
|
|
45
|
+
}
|
|
46
|
+
export function writeUsageCache(doc, cachePath = USAGE_CACHE_PATH) {
|
|
47
|
+
try {
|
|
48
|
+
ensurePrivateDirectory(dirname(cachePath));
|
|
49
|
+
const env = { fetchedAt: Date.now(), doc };
|
|
50
|
+
writeFileSync(cachePath, JSON.stringify(env), {
|
|
51
|
+
encoding: "utf8",
|
|
52
|
+
mode: 384
|
|
53
|
+
});
|
|
54
|
+
ensurePrivateFile(cachePath);
|
|
55
|
+
} catch (err) {
|
|
56
|
+
log.warn("failed to write usage cache:", err.message);
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export function tryAcquireUsageLock(lockPath = USAGE_LOCK_PATH) {
|
|
60
|
+
const token = `${process.pid}@${Date.now()}`;
|
|
61
|
+
if (existsSync(lockPath)) {
|
|
62
|
+
try {
|
|
63
|
+
ensurePrivateDirectory(dirname(lockPath));
|
|
64
|
+
ensurePrivateFile(lockPath);
|
|
65
|
+
const stat = statSync(lockPath);
|
|
66
|
+
if (Date.now() - stat.mtimeMs > LOCK_STALE_MS) {
|
|
67
|
+
log.debug("removing stale usage lock");
|
|
68
|
+
unlinkSync(lockPath);
|
|
69
|
+
} else {
|
|
70
|
+
return null;
|
|
71
|
+
}
|
|
72
|
+
} catch {
|
|
73
|
+
return null;
|
|
74
|
+
}
|
|
75
|
+
}
|
|
76
|
+
try {
|
|
77
|
+
ensurePrivateDirectory(dirname(lockPath));
|
|
78
|
+
writeFileSync(lockPath, token, { flag: "wx", mode: 384 });
|
|
79
|
+
ensurePrivateFile(lockPath);
|
|
80
|
+
return token;
|
|
81
|
+
} catch {
|
|
82
|
+
return null;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
export function releaseUsageLock(ourToken, lockPath = USAGE_LOCK_PATH) {
|
|
86
|
+
try {
|
|
87
|
+
ensurePrivateFile(lockPath);
|
|
88
|
+
const content = readFileSync(lockPath, "utf8").trim();
|
|
89
|
+
if (content === ourToken) {
|
|
90
|
+
unlinkSync(lockPath);
|
|
91
|
+
return true;
|
|
92
|
+
}
|
|
93
|
+
return false;
|
|
94
|
+
} catch {
|
|
95
|
+
return false;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
@@ -1 +1,144 @@
|
|
|
1
|
-
import
|
|
1
|
+
import {
|
|
2
|
+
DynamicBorder
|
|
3
|
+
} from "@duckmind/dm-coding-agent";
|
|
4
|
+
import { Container, Text, Spacer } from "@duckmind/dm-tui";
|
|
5
|
+
import { formatTokens } from "./utils.js";
|
|
6
|
+
export default function context_default(pi) {
|
|
7
|
+
pi.registerCommand("context", {
|
|
8
|
+
description: "Show context usage visualization",
|
|
9
|
+
handler: async (args, ctx) => {
|
|
10
|
+
const usage = await ctx.getContextUsage();
|
|
11
|
+
if (!usage) {
|
|
12
|
+
ctx.ui.notify("Context usage info not available.", "warning");
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
const sm = ctx.sessionManager;
|
|
16
|
+
const branch = sm.getBranch();
|
|
17
|
+
const systemPrompt = ctx.getSystemPrompt();
|
|
18
|
+
const tools = pi.getActiveTools();
|
|
19
|
+
const allTools = pi.getAllTools();
|
|
20
|
+
const activeToolDefs = allTools.filter((t) => tools.includes(t.name));
|
|
21
|
+
const estimateTokens = (text) => Math.ceil(text.length / 4);
|
|
22
|
+
let msgTokensRaw = 0;
|
|
23
|
+
let toolUseTokensRaw = 0;
|
|
24
|
+
let toolResultTokensRaw = 0;
|
|
25
|
+
for (const entry of branch) {
|
|
26
|
+
if (entry.type === "message") {
|
|
27
|
+
const m = entry.message;
|
|
28
|
+
if (m.role === "user") {
|
|
29
|
+
if (typeof m.content === "string")
|
|
30
|
+
msgTokensRaw += estimateTokens(m.content);
|
|
31
|
+
else if (Array.isArray(m.content)) {
|
|
32
|
+
for (const p of m.content)
|
|
33
|
+
if (p.type === "text")
|
|
34
|
+
msgTokensRaw += estimateTokens(p.text);
|
|
35
|
+
}
|
|
36
|
+
} else if (m.role === "assistant") {
|
|
37
|
+
if (typeof m.content === "string")
|
|
38
|
+
msgTokensRaw += estimateTokens(m.content);
|
|
39
|
+
else if (Array.isArray(m.content)) {
|
|
40
|
+
for (const p of m.content) {
|
|
41
|
+
if (p.type === "text")
|
|
42
|
+
msgTokensRaw += estimateTokens(p.text);
|
|
43
|
+
if (p.type === "toolCall")
|
|
44
|
+
toolUseTokensRaw += estimateTokens(JSON.stringify(p));
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
} else if (m.role === "toolResult") {
|
|
48
|
+
if (Array.isArray(m.content)) {
|
|
49
|
+
for (const p of m.content)
|
|
50
|
+
if (p.type === "text")
|
|
51
|
+
toolResultTokensRaw += estimateTokens(p.text);
|
|
52
|
+
}
|
|
53
|
+
} else if (m.role === "bashExecution") {
|
|
54
|
+
toolUseTokensRaw += estimateTokens(m.command || "");
|
|
55
|
+
}
|
|
56
|
+
} else if (entry.type === "branch_summary" || entry.type === "compaction") {
|
|
57
|
+
msgTokensRaw += estimateTokens(entry.summary || "");
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
const systemTokensRaw = estimateTokens(systemPrompt);
|
|
61
|
+
const toolDefTokensRaw = estimateTokens(JSON.stringify(activeToolDefs));
|
|
62
|
+
const totalActual = usage.tokens;
|
|
63
|
+
const limit = usage.contextWindow;
|
|
64
|
+
if (totalActual == null || limit == null || usage.percent == null) {
|
|
65
|
+
ctx.ui.notify("Context usage info not available.", "warning");
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const usagePercent = usage.percent;
|
|
69
|
+
const totalRaw = systemTokensRaw + toolDefTokensRaw + msgTokensRaw + toolUseTokensRaw + toolResultTokensRaw;
|
|
70
|
+
const ratio = totalRaw > 0 ? totalActual / totalRaw : 1;
|
|
71
|
+
const systemTokens = Math.round(systemTokensRaw * ratio);
|
|
72
|
+
const toolDefTokens = Math.round(toolDefTokensRaw * ratio);
|
|
73
|
+
const msgTokens = Math.round(msgTokensRaw * ratio);
|
|
74
|
+
const toolUseTokens = Math.round(toolUseTokensRaw * ratio);
|
|
75
|
+
const toolResultTokens = Math.round(toolResultTokensRaw * ratio);
|
|
76
|
+
await ctx.ui.custom((tui, theme, kb, done) => {
|
|
77
|
+
const container = new Container;
|
|
78
|
+
container.addChild(new DynamicBorder((s) => theme.fg("accent", s)));
|
|
79
|
+
container.addChild(new Text(theme.fg("accent", theme.bold(" Context Usage")), 1, 0));
|
|
80
|
+
container.addChild(new Spacer(1));
|
|
81
|
+
const categories = [
|
|
82
|
+
{ label: "System Prompt", value: systemTokens, color: "muted" },
|
|
83
|
+
{ label: "System Tools", value: toolDefTokens, color: "dim" },
|
|
84
|
+
{ label: "Tool Call", value: toolUseTokens + toolResultTokens, color: "success" },
|
|
85
|
+
{ label: "Messages", value: msgTokens, color: "accent" }
|
|
86
|
+
];
|
|
87
|
+
const otherTokens = Math.max(0, totalActual - (systemTokens + toolDefTokens + msgTokens + toolUseTokens + toolResultTokens));
|
|
88
|
+
if (otherTokens > 10)
|
|
89
|
+
categories.push({ label: "Other", value: otherTokens, color: "dim" });
|
|
90
|
+
categories.push({ label: "Available", value: Math.max(0, limit - totalActual), color: "borderMuted" });
|
|
91
|
+
const gridWidth = 10;
|
|
92
|
+
const gridHeight = 5;
|
|
93
|
+
const totalBlocks = gridWidth * gridHeight;
|
|
94
|
+
const blocks = [];
|
|
95
|
+
categories.forEach((cat) => {
|
|
96
|
+
if (cat.label === "Available")
|
|
97
|
+
return;
|
|
98
|
+
let count = Math.round(cat.value / limit * totalBlocks);
|
|
99
|
+
if (count === 0 && cat.value > 0)
|
|
100
|
+
count = 1;
|
|
101
|
+
for (let i = 0;i < count && blocks.length < totalBlocks; i++) {
|
|
102
|
+
blocks.push({ color: cat.color, filled: true });
|
|
103
|
+
}
|
|
104
|
+
});
|
|
105
|
+
while (blocks.length < totalBlocks) {
|
|
106
|
+
blocks.push({ color: "borderMuted", filled: false });
|
|
107
|
+
}
|
|
108
|
+
const gridLines = [];
|
|
109
|
+
for (let r = 0;r < gridHeight; r++) {
|
|
110
|
+
let rowStr = "";
|
|
111
|
+
for (let c = 0;c < gridWidth; c++) {
|
|
112
|
+
const b = blocks[r * gridWidth + c];
|
|
113
|
+
rowStr += theme.fg(b.color, b.filled ? "■ " : "□ ");
|
|
114
|
+
}
|
|
115
|
+
gridLines.push(rowStr.trimEnd());
|
|
116
|
+
}
|
|
117
|
+
const totalUsageTitle = `${theme.fg("text", theme.bold("Total Usage".padEnd(16)))} ${theme.fg("text", theme.bold(formatTokens(totalActual).padStart(7)))} ${theme.fg("text", theme.bold(`(${usagePercent.toFixed(1).padStart(5)}%)`))}`;
|
|
118
|
+
const catDetailLines = categories.map((cat) => {
|
|
119
|
+
const labelStr = cat.label.padEnd(14);
|
|
120
|
+
const valStr = formatTokens(cat.value).padStart(7);
|
|
121
|
+
const rowPercent = (cat.value / limit * 100).toFixed(1).padStart(5);
|
|
122
|
+
const icon = cat.label === "Available" ? "□" : "■";
|
|
123
|
+
return `${theme.fg(cat.color, icon)} ${theme.fg("text", labelStr)} ${theme.fg("accent", valStr)} (${rowPercent}%)`;
|
|
124
|
+
});
|
|
125
|
+
const allDetailLines = [totalUsageTitle, "", ...catDetailLines];
|
|
126
|
+
const leftSideWidth = 20;
|
|
127
|
+
const maxH = Math.max(gridLines.length, allDetailLines.length);
|
|
128
|
+
for (let i = 0;i < maxH; i++) {
|
|
129
|
+
const left = (gridLines[i] || "").padEnd(leftSideWidth);
|
|
130
|
+
const right = allDetailLines[i] || "";
|
|
131
|
+
container.addChild(new Text(` ${left} ${right}`, 1, 0));
|
|
132
|
+
}
|
|
133
|
+
container.addChild(new Spacer(1));
|
|
134
|
+
container.addChild(new Text(theme.fg("dim", " Press any key to close"), 1, 0));
|
|
135
|
+
container.addChild(new DynamicBorder((s) => theme.fg("accent", s)));
|
|
136
|
+
return {
|
|
137
|
+
render: (w) => container.render(w),
|
|
138
|
+
invalidate: () => container.invalidate(),
|
|
139
|
+
handleInput: (data) => done(undefined)
|
|
140
|
+
};
|
|
141
|
+
}, { overlay: true });
|
|
142
|
+
}
|
|
143
|
+
});
|
|
144
|
+
}
|