deep-toolbox 0.0.0-stage → 0.1.4
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/LICENSE +21 -0
- package/README.md +100 -2
- package/cordis.patch.yml +7 -0
- package/icon.svg +8 -0
- package/lib/cli.js +396 -0
- package/lib/client.js +506 -0
- package/lib/index.js +494 -0
- package/package.json +87 -4
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Johannes Tebbert
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,101 @@
|
|
|
1
|
-
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/JoblessJoe/deep-toolbox/main/.github/assets/banner.svg" alt="deep-toolbox: tool usage and startup cost for the DeepSeek Harness" width="100%">
|
|
3
|
+
</p>
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
<p align="center">
|
|
6
|
+
<a href="https://www.npmjs.com/package/deep-toolbox"><img src="https://img.shields.io/npm/v/deep-toolbox?color=4d6bfe&label=npm" alt="npm version"></a>
|
|
7
|
+
<a href="https://www.npmjs.com/package/deep-toolbox"><img src="https://img.shields.io/npm/dm/deep-toolbox?color=4d6bfe" alt="npm downloads"></a>
|
|
8
|
+
<img src="https://img.shields.io/badge/dependencies-0-4d6bfe" alt="zero runtime dependencies">
|
|
9
|
+
<a href="https://github.com/JoblessJoe/deep-toolbox/blob/main/LICENSE"><img src="https://img.shields.io/github/license/JoblessJoe/deep-toolbox?color=4d6bfe" alt="MIT license"></a>
|
|
10
|
+
<a href="https://github.com/topics/dsh-plugin"><img src="https://img.shields.io/badge/topic-dsh--plugin-0b6bcb" alt="dsh-plugin topic"></a>
|
|
11
|
+
</p>
|
|
12
|
+
|
|
13
|
+
A [DeepSeek Harness](https://github.com/deepseek-ai/deepseek-harness) (dsh) plugin that shows **which tools your agents actually use, which ones fail, and what each one costs you in every request**. Every tool you install (built-in, plugin or MCP server) sends its full definition with every model request. A 37-tool MCP server can add thousands of tokens to each prompt, even if no agent ever calls it. deep-toolbox finds that dead weight so you can drop it.
|
|
14
|
+
|
|
15
|
+
```bash
|
|
16
|
+
npx @deepseek-ai/dsh plugin --profile web add deep-toolbox
|
|
17
|
+
```
|
|
18
|
+
|
|
19
|
+
Then restart your dsh web server and reload the tab. A **Tool usage & startup cost** tab appears on the Plugins page. Full details under [Install](#install).
|
|
20
|
+
|
|
21
|
+
<p align="center">
|
|
22
|
+
<img src="https://raw.githubusercontent.com/JoblessJoe/deep-toolbox/main/.github/assets/summary.png" alt="The Tool usage & startup cost tab: cards for startup tokens, tokens paid for but never used and failed calls, followed by plain-language tips" width="100%">
|
|
23
|
+
<br><sub>Summary first: what a new chat starts with, what you pay for but never use, and which tools fail.</sub>
|
|
24
|
+
</p>
|
|
25
|
+
|
|
26
|
+
<p align="center">
|
|
27
|
+
<img src="https://raw.githubusercontent.com/JoblessJoe/deep-toolbox/main/.github/assets/sources.png" alt="A stacked bar of the startup context, followed by one row per tool source with its tool count, calls and tokens per chat; the github source is open to show its per-tool table" width="100%">
|
|
28
|
+
<br><sub>Everything a new chat carries before you type, and one row per tool source. Open a source for its per-tool table.</sub>
|
|
29
|
+
</p>
|
|
30
|
+
|
|
31
|
+
## What you see
|
|
32
|
+
|
|
33
|
+
- **A new chat starts with**: the tokens every new chat carries before you type, and how much of that is tool definitions.
|
|
34
|
+
- **Paid for, never used**: tools whose definitions go out with every request, but no agent called them in the chosen range.
|
|
35
|
+
- **Failed calls**: the error rate, with the latest error messages per tool.
|
|
36
|
+
- **What stands out**: plain-language tips, like "math adds 4.6K tok to every new chat but was not used in the last 30 days".
|
|
37
|
+
- **Startup context**: a chart of the built-in tools, each MCP server and plugin, each `AGENTS.md`, the system prompt and the skill list.
|
|
38
|
+
- **Tool sources**: one row per source, tagged *used a lot*, *rarely used* or *never used*. Open one for calls (main agent and subagents), errors, average time, result size, tokens per chat and last use of each tool.
|
|
39
|
+
- **Ranges**: last 7, 30 or 90 days. **Show removed** adds tools you used before but no longer have installed.
|
|
40
|
+
|
|
41
|
+
## For agents
|
|
42
|
+
|
|
43
|
+
Every agent gets a read-only `toolbox_report` tool with the same numbers as short text. An agent can notice "this MCP server costs 4.6K tok per chat and was never used" and offer to turn it off with dsh's built-in `plugin_manager`, which asks you first.
|
|
44
|
+
|
|
45
|
+
## How it works
|
|
46
|
+
|
|
47
|
+
No hooks, no database, no tracking code in the agent loop. dsh already records every tool call, result and request in its session logs (`~/.dsh/sessions/**/session.v*.jsonl.zstd`). deep-toolbox reads those logs when you open the tab and adds them up. Parsed files are cached until they change. Nothing leaves your machine.
|
|
48
|
+
|
|
49
|
+
- **Host**: one JSON route, `/api/deep-toolbox/stats`, plus the `toolbox_report` agent tool. It also notes which plugin registers which tool, so plugin tools don't count as built-in.
|
|
50
|
+
- **Client**: one self-contained bundle on dsh's own design tokens, so it looks native in both themes.
|
|
51
|
+
|
|
52
|
+
Token numbers are estimates (characters ÷ 4). "Sent" is the raw volume before prompt caching.
|
|
53
|
+
|
|
54
|
+
## Install
|
|
55
|
+
|
|
56
|
+
Into your dsh **web profile** (`web` below is the profile name; use whichever profile backs your web GUI). From a source checkout, use `pnpm dsh` instead of `npx @deepseek-ai/dsh`:
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
npx @deepseek-ai/dsh plugin --profile web add deep-toolbox
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
Restart the dsh web server afterwards, then reload the browser tab.
|
|
63
|
+
|
|
64
|
+
Other sources:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
# straight from GitHub
|
|
68
|
+
npx @deepseek-ai/dsh plugin --profile web add github:JoblessJoe/deep-toolbox
|
|
69
|
+
# from a local checkout
|
|
70
|
+
npx @deepseek-ai/dsh plugin --profile web add file:/path/to/deep-toolbox
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
The agent tool also works in headless and TUI profiles. The tab needs the web GUI.
|
|
74
|
+
|
|
75
|
+
**Requirements**: dsh 0.2.2 or newer (`engines.dsh`: `>=0.2.2-rc.1 <0.4.0`), Node 22.15+ (built-in zstd).
|
|
76
|
+
|
|
77
|
+
## CLI
|
|
78
|
+
|
|
79
|
+
The same report in a terminal, or for an agent through `bash`:
|
|
80
|
+
|
|
81
|
+
```bash
|
|
82
|
+
npx deep-toolbox # last 30 days
|
|
83
|
+
npx deep-toolbox 7 # last 7 days
|
|
84
|
+
npx deep-toolbox --errors web_fetch # latest errors of one tool
|
|
85
|
+
npx deep-toolbox --json
|
|
86
|
+
npx deep-toolbox --agent # the toolbox_report text
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
Set `DSH_HOME` (or `DSH_SESSIONS`) if your sessions are not under `~/.dsh`.
|
|
90
|
+
|
|
91
|
+
## Develop
|
|
92
|
+
|
|
93
|
+
```bash
|
|
94
|
+
npm install
|
|
95
|
+
npm run build # lib/index.js (host) + lib/client.js (web) + lib/cli.js
|
|
96
|
+
npm test
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
## License
|
|
100
|
+
|
|
101
|
+
MIT
|
package/cordis.patch.yml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
# deep-toolbox bundle mount.
|
|
2
|
+
# A NEW plugin contributes a brand-new profile entry, so the row is an `insert:`
|
|
3
|
+
# (a bare `- id:` would patch an existing entry by id and fail silently).
|
|
4
|
+
# Adding a new `insert:` requires a web-server restart to take effect.
|
|
5
|
+
- insert:
|
|
6
|
+
- id: deep-toolbox
|
|
7
|
+
name: 'deep-toolbox'
|
package/icon.svg
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 64 64">
|
|
2
|
+
<rect width="64" height="64" rx="14" fill="#1f2430"/>
|
|
3
|
+
<g transform="translate(8 8) scale(2) rotate(45 12 12)">
|
|
4
|
+
<rect x="10.6" y="8" width="2.8" height="14.5" rx="1.4" fill="#c98b4a"/>
|
|
5
|
+
<rect x="4.5" y="3" width="11" height="5.4" rx="1.2" fill="#d7dde8"/>
|
|
6
|
+
<rect x="15" y="1.8" width="4" height="7.8" rx="1" fill="#f6c343"/>
|
|
7
|
+
</g>
|
|
8
|
+
</svg>
|
package/lib/cli.js
ADDED
|
@@ -0,0 +1,396 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/stats.ts
|
|
4
|
+
import { readdirSync, readFileSync as readFileSync2, statSync } from "node:fs";
|
|
5
|
+
import { join as join2 } from "node:path";
|
|
6
|
+
import { homedir as homedir2 } from "node:os";
|
|
7
|
+
import { zstdDecompressSync } from "node:zlib";
|
|
8
|
+
|
|
9
|
+
// src/owners.ts
|
|
10
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
11
|
+
import { dirname, isAbsolute, join } from "node:path";
|
|
12
|
+
import { homedir } from "node:os";
|
|
13
|
+
var ownersFile = () => join(process.env.DSH_HOME ?? join(homedir(), ".dsh"), "deep-toolbox", "owners.json");
|
|
14
|
+
function readOwners(file = ownersFile()) {
|
|
15
|
+
try {
|
|
16
|
+
return JSON.parse(readFileSync(file, "utf8")) ?? {};
|
|
17
|
+
} catch {
|
|
18
|
+
return {};
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// src/stats.ts
|
|
23
|
+
var sessionsRoot = () => process.env.DSH_SESSIONS ?? join2(process.env.DSH_HOME ?? join2(homedir2(), ".dsh"), "sessions");
|
|
24
|
+
var tokens = (value) => Math.round((typeof value === "string" ? value : JSON.stringify(value ?? "")).length / 4);
|
|
25
|
+
var groupOf = (name, owners = {}) => name.startsWith("mcp__") ? `mcp:${name.split("__")[1]}` : owners[name] ? `plugin:${owners[name]}` : "built-in";
|
|
26
|
+
var clampDays = (raw) => {
|
|
27
|
+
const n = Number(raw);
|
|
28
|
+
return Number.isFinite(n) ? Math.min(365, Math.max(1, Math.round(n))) : 30;
|
|
29
|
+
};
|
|
30
|
+
var DAY = 864e5;
|
|
31
|
+
var sourceLabel = (group) => group === "built-in" ? "Built-in tools" : group.startsWith("mcp:") ? `MCP: ${group.slice(4)}` : `Plugin: ${group.slice(7)}`;
|
|
32
|
+
function regroupTools(startup, owners) {
|
|
33
|
+
const groups = /* @__PURE__ */ new Map();
|
|
34
|
+
for (const t of startup.tools) {
|
|
35
|
+
const g = groupOf(t.name, owners);
|
|
36
|
+
groups.set(g, (groups.get(g) ?? 0) + t.tokens);
|
|
37
|
+
}
|
|
38
|
+
return [...startup.parts, ...[...groups].map(([g, tok]) => ({ kind: "tools", label: sourceLabel(g), tokens: tok }))];
|
|
39
|
+
}
|
|
40
|
+
var text = (content) => typeof content === "string" ? content : Array.isArray(content) ? content.map((c) => typeof c?.text === "string" ? c.text : "").join("") : "";
|
|
41
|
+
function instructionParts(body) {
|
|
42
|
+
const parts = [];
|
|
43
|
+
let file = "", buf = "";
|
|
44
|
+
const flush = () => {
|
|
45
|
+
if (buf.trim()) parts.push(file ? { kind: "instructions", label: file, tokens: tokens(buf) } : { kind: "context", label: "Other", tokens: tokens(buf) });
|
|
46
|
+
};
|
|
47
|
+
for (const line of body.split("\n")) {
|
|
48
|
+
const from = line.match(/^Instructions from: (.+)$/);
|
|
49
|
+
if (from) {
|
|
50
|
+
flush();
|
|
51
|
+
file = from[1].trim();
|
|
52
|
+
buf = "";
|
|
53
|
+
}
|
|
54
|
+
buf += line + "\n";
|
|
55
|
+
}
|
|
56
|
+
flush();
|
|
57
|
+
return parts;
|
|
58
|
+
}
|
|
59
|
+
var SOURCE_LABEL = {
|
|
60
|
+
"system-prompt": ["system", "System prompt"],
|
|
61
|
+
"skill-catalog": ["skills", "Skill list"],
|
|
62
|
+
"runtime-context": ["context", "Other"],
|
|
63
|
+
"working-directory": ["context", "Other"],
|
|
64
|
+
"time-context": ["context", "Other"]
|
|
65
|
+
};
|
|
66
|
+
function parseSession(raw) {
|
|
67
|
+
const facts = { depth: 0, calls: [], offered: /* @__PURE__ */ new Map(), latest: null, startup: null };
|
|
68
|
+
const pending = /* @__PURE__ */ new Map();
|
|
69
|
+
let lastTime = 0;
|
|
70
|
+
let early = [];
|
|
71
|
+
for (const line of raw.split("\n")) {
|
|
72
|
+
if (!line) continue;
|
|
73
|
+
let e;
|
|
74
|
+
try {
|
|
75
|
+
e = JSON.parse(line);
|
|
76
|
+
} catch {
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const time = typeof e.time === "number" ? lastTime = e.time : lastTime;
|
|
80
|
+
switch (e.type) {
|
|
81
|
+
case "session":
|
|
82
|
+
facts.depth = e.delegationDepth ?? 0;
|
|
83
|
+
break;
|
|
84
|
+
case "system/message":
|
|
85
|
+
case "user/message": {
|
|
86
|
+
if (!early) break;
|
|
87
|
+
const m = e.data?.message ?? e.data;
|
|
88
|
+
const kind = m?.source?.kind ?? "";
|
|
89
|
+
if (kind === "user") break;
|
|
90
|
+
if (kind === "agent-instructions") {
|
|
91
|
+
early.push(...instructionParts(text(m.content)));
|
|
92
|
+
break;
|
|
93
|
+
}
|
|
94
|
+
const [k, label] = SOURCE_LABEL[kind] ?? ["context", kind || "Other"];
|
|
95
|
+
const tok = tokens(text(m?.content));
|
|
96
|
+
if (tok > 0) early.push({ kind: k, label, tokens: tok });
|
|
97
|
+
break;
|
|
98
|
+
}
|
|
99
|
+
case "request/header": {
|
|
100
|
+
const defs = e.data?.header?.tools ?? [];
|
|
101
|
+
const names = defs.map((d) => d.name);
|
|
102
|
+
const day = Math.floor(time / DAY);
|
|
103
|
+
for (const def of defs) {
|
|
104
|
+
const cost = tokens(def);
|
|
105
|
+
let use = facts.offered.get(def.name);
|
|
106
|
+
if (!use) facts.offered.set(def.name, use = /* @__PURE__ */ new Map());
|
|
107
|
+
const d = use.get(day) ?? { req: 0, sent: 0, cost: 0, at: 0 };
|
|
108
|
+
d.req++;
|
|
109
|
+
d.sent += cost;
|
|
110
|
+
if (time >= d.at) {
|
|
111
|
+
d.cost = cost;
|
|
112
|
+
d.at = time;
|
|
113
|
+
}
|
|
114
|
+
use.set(day, d);
|
|
115
|
+
}
|
|
116
|
+
if (!facts.latest || time >= facts.latest.time) facts.latest = { time, names };
|
|
117
|
+
if (early) {
|
|
118
|
+
if (facts.depth === 0) facts.startup = { time, parts: early, tools: defs.map((d) => ({ name: d.name, tokens: tokens(d) })) };
|
|
119
|
+
early = null;
|
|
120
|
+
}
|
|
121
|
+
break;
|
|
122
|
+
}
|
|
123
|
+
case "tool/call":
|
|
124
|
+
pending.set(e.data?.callId, facts.calls.length);
|
|
125
|
+
facts.calls.push({ name: e.data?.name ?? "?", time, ms: null, resultTok: null, error: null });
|
|
126
|
+
break;
|
|
127
|
+
case "tool/result": {
|
|
128
|
+
const m = e.data?.message;
|
|
129
|
+
const i = pending.get(m?.toolCallId);
|
|
130
|
+
if (i === void 0) break;
|
|
131
|
+
pending.delete(m.toolCallId);
|
|
132
|
+
const call = facts.calls[i];
|
|
133
|
+
call.ms = time - call.time;
|
|
134
|
+
call.resultTok = tokens(m.content);
|
|
135
|
+
if (m.isError) call.error = text(m.content).replace(/\s+/g, " ").trim().slice(0, 300) || "error";
|
|
136
|
+
break;
|
|
137
|
+
}
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
return facts;
|
|
141
|
+
}
|
|
142
|
+
var ZSTD_MAGIC = 4247762216;
|
|
143
|
+
function decodeZstdFrames(buf) {
|
|
144
|
+
const out = [];
|
|
145
|
+
let off = 0;
|
|
146
|
+
while (off + 5 <= buf.length && buf.readUInt32LE(off) === ZSTD_MAGIC) {
|
|
147
|
+
const start = off;
|
|
148
|
+
const fhd = buf.readUInt8(off + 4);
|
|
149
|
+
const fcsFlag = fhd >>> 6, single = (fhd & 32) !== 0, checksum = (fhd & 4) !== 0, dictFlag = fhd & 3;
|
|
150
|
+
off += 5 + (single ? 0 : 1) + (dictFlag === 3 ? 4 : dictFlag) + (fcsFlag === 0 ? single ? 1 : 0 : 1 << fcsFlag);
|
|
151
|
+
for (; ; ) {
|
|
152
|
+
if (off + 3 > buf.length) return Buffer.concat(out).toString("utf8");
|
|
153
|
+
const header = buf.readUIntLE(off, 3);
|
|
154
|
+
off += 3 + ((header >>> 1 & 3) === 1 ? 1 : header >>> 3);
|
|
155
|
+
if (header & 1) break;
|
|
156
|
+
}
|
|
157
|
+
if (checksum) off += 4;
|
|
158
|
+
if (off > buf.length) break;
|
|
159
|
+
out.push(zstdDecompressSync(buf.subarray(start, off)));
|
|
160
|
+
}
|
|
161
|
+
return Buffer.concat(out).toString("utf8");
|
|
162
|
+
}
|
|
163
|
+
var cache = /* @__PURE__ */ new Map();
|
|
164
|
+
var LOG_NAMES = ["session.v4.jsonl.zstd", "session.v3.jsonl.zstd"];
|
|
165
|
+
function* sessionFiles(root) {
|
|
166
|
+
let dirs = [];
|
|
167
|
+
try {
|
|
168
|
+
dirs = readdirSync(root);
|
|
169
|
+
} catch {
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
for (const dir of dirs) {
|
|
173
|
+
let ids = [];
|
|
174
|
+
try {
|
|
175
|
+
ids = readdirSync(join2(root, dir));
|
|
176
|
+
} catch {
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
for (const id of ids) {
|
|
180
|
+
for (const name of LOG_NAMES) {
|
|
181
|
+
const path = join2(root, dir, id, name);
|
|
182
|
+
try {
|
|
183
|
+
const st = statSync(path);
|
|
184
|
+
yield { path, mtimeMs: st.mtimeMs, size: st.size };
|
|
185
|
+
break;
|
|
186
|
+
} catch {
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
function factsFor(file) {
|
|
193
|
+
const key = `${file.mtimeMs}:${file.size}`;
|
|
194
|
+
const hit = cache.get(file.path);
|
|
195
|
+
if (hit?.key === key) return hit.facts;
|
|
196
|
+
try {
|
|
197
|
+
const facts = parseSession(decodeZstdFrames(readFileSync2(file.path)));
|
|
198
|
+
cache.set(file.path, { key, facts });
|
|
199
|
+
return facts;
|
|
200
|
+
} catch {
|
|
201
|
+
return null;
|
|
202
|
+
}
|
|
203
|
+
}
|
|
204
|
+
function collect(days2 = 30, root = sessionsRoot(), now = Date.now(), owners = readOwners()) {
|
|
205
|
+
const since = now - days2 * DAY;
|
|
206
|
+
const sinceDay = Math.floor(since / DAY);
|
|
207
|
+
const rows = /* @__PURE__ */ new Map();
|
|
208
|
+
const row = (name) => {
|
|
209
|
+
let r = rows.get(name);
|
|
210
|
+
if (!r) {
|
|
211
|
+
r = {
|
|
212
|
+
name,
|
|
213
|
+
group: groupOf(name, owners),
|
|
214
|
+
calls: 0,
|
|
215
|
+
subCalls: 0,
|
|
216
|
+
errors: 0,
|
|
217
|
+
avgMs: null,
|
|
218
|
+
avgResultTok: null,
|
|
219
|
+
cost: 0,
|
|
220
|
+
offeredIn: 0,
|
|
221
|
+
offeredRequests: 0,
|
|
222
|
+
sentTok: 0,
|
|
223
|
+
lastUsed: 0,
|
|
224
|
+
current: false,
|
|
225
|
+
recentErrors: [],
|
|
226
|
+
msSum: 0,
|
|
227
|
+
msN: 0,
|
|
228
|
+
resSum: 0,
|
|
229
|
+
resN: 0,
|
|
230
|
+
costAt: 0
|
|
231
|
+
};
|
|
232
|
+
rows.set(name, r);
|
|
233
|
+
}
|
|
234
|
+
return r;
|
|
235
|
+
};
|
|
236
|
+
let sessions = 0;
|
|
237
|
+
let latest = null;
|
|
238
|
+
let startup = null;
|
|
239
|
+
const seen = /* @__PURE__ */ new Set();
|
|
240
|
+
for (const file of sessionFiles(root)) {
|
|
241
|
+
seen.add(file.path);
|
|
242
|
+
if (file.mtimeMs < since) continue;
|
|
243
|
+
const facts = factsFor(file);
|
|
244
|
+
if (!facts) continue;
|
|
245
|
+
sessions++;
|
|
246
|
+
for (const [name, byDay] of facts.offered) {
|
|
247
|
+
let inRange = false;
|
|
248
|
+
for (const [day, d] of byDay) {
|
|
249
|
+
if (day < sinceDay) continue;
|
|
250
|
+
const r = row(name);
|
|
251
|
+
inRange = true;
|
|
252
|
+
r.offeredRequests += d.req;
|
|
253
|
+
r.sentTok += d.sent;
|
|
254
|
+
if (d.at >= r.costAt) {
|
|
255
|
+
r.cost = d.cost;
|
|
256
|
+
r.costAt = d.at;
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
if (inRange) row(name).offeredIn++;
|
|
260
|
+
}
|
|
261
|
+
if (facts.depth === 0 && facts.latest && (!latest || facts.latest.time > latest.time)) latest = facts.latest;
|
|
262
|
+
if (facts.startup && (!startup || facts.startup.time > startup.time)) startup = facts.startup;
|
|
263
|
+
for (const c of facts.calls) {
|
|
264
|
+
if (c.time < since) continue;
|
|
265
|
+
const r = row(c.name);
|
|
266
|
+
r.calls++;
|
|
267
|
+
if (facts.depth > 0) r.subCalls++;
|
|
268
|
+
r.lastUsed = Math.max(r.lastUsed, c.time);
|
|
269
|
+
if (c.ms !== null) {
|
|
270
|
+
r.msSum += c.ms;
|
|
271
|
+
r.msN++;
|
|
272
|
+
}
|
|
273
|
+
if (c.resultTok !== null) {
|
|
274
|
+
r.resSum += c.resultTok;
|
|
275
|
+
r.resN++;
|
|
276
|
+
}
|
|
277
|
+
if (c.error !== null) {
|
|
278
|
+
r.errors++;
|
|
279
|
+
r.recentErrors.push({ time: c.time, text: c.error });
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
for (const path of cache.keys()) if (!seen.has(path)) cache.delete(path);
|
|
284
|
+
const currentNames = new Set(latest?.names ?? []);
|
|
285
|
+
const out = [...rows.values()].map(({ msSum, msN, resSum, resN, costAt, ...r }) => ({
|
|
286
|
+
...r,
|
|
287
|
+
avgMs: msN ? Math.round(msSum / msN) : null,
|
|
288
|
+
avgResultTok: resN ? Math.round(resSum / resN) : null,
|
|
289
|
+
current: currentNames.has(r.name),
|
|
290
|
+
recentErrors: r.recentErrors.sort((a, b) => b.time - a.time).slice(0, 20)
|
|
291
|
+
}));
|
|
292
|
+
out.sort((a, b) => b.calls - a.calls || b.cost - a.cost || a.name.localeCompare(b.name));
|
|
293
|
+
const cur = out.filter((r) => r.current);
|
|
294
|
+
const unused = cur.filter((r) => r.calls === 0);
|
|
295
|
+
const sum = (rs) => rs.reduce((n, r) => n + r.cost, 0);
|
|
296
|
+
const parts = mergeParts(startup ? regroupTools(startup, owners) : []);
|
|
297
|
+
return {
|
|
298
|
+
days: days2,
|
|
299
|
+
generatedAt: now,
|
|
300
|
+
sessions,
|
|
301
|
+
rows: out,
|
|
302
|
+
current: { tools: cur.length, cost: sum(cur), unused: unused.length, unusedCost: sum(unused) },
|
|
303
|
+
startup: { total: parts.reduce((n, p) => n + p.tokens, 0), parts }
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
function mergeParts(raw) {
|
|
307
|
+
const byLabel = /* @__PURE__ */ new Map();
|
|
308
|
+
for (const p of raw) {
|
|
309
|
+
const hit = byLabel.get(p.label);
|
|
310
|
+
if (hit) hit.tokens += p.tokens;
|
|
311
|
+
else byLabel.set(p.label, { ...p });
|
|
312
|
+
}
|
|
313
|
+
const total = [...byLabel.values()].reduce((n, p) => n + p.tokens, 0);
|
|
314
|
+
const keep = [];
|
|
315
|
+
let other = 0;
|
|
316
|
+
for (const p of byLabel.values()) {
|
|
317
|
+
if (p.label === "Other" || p.tokens < total * 0.01) other += p.tokens;
|
|
318
|
+
else keep.push(p);
|
|
319
|
+
}
|
|
320
|
+
keep.sort((a, b) => b.tokens - a.tokens);
|
|
321
|
+
if (other > 0) keep.push({ kind: "context", label: "Other", tokens: other });
|
|
322
|
+
return keep;
|
|
323
|
+
}
|
|
324
|
+
function agentSummary(r, top = 8) {
|
|
325
|
+
const lines = [];
|
|
326
|
+
const k = (n) => n >= 1e3 ? `${(n / 1e3).toFixed(1)}k` : String(n);
|
|
327
|
+
lines.push(`Last ${r.days} days, ${r.sessions} sessions. Every request carries ${r.current.tools} tool definitions = ${k(r.current.cost)} tok.`);
|
|
328
|
+
if (r.startup.total) {
|
|
329
|
+
lines.push(`
|
|
330
|
+
Startup context of a new chat \u2248 ${k(r.startup.total)} tok:`);
|
|
331
|
+
for (const p of r.startup.parts.slice(0, top)) lines.push(`- ${p.label}: ${k(p.tokens)}`);
|
|
332
|
+
}
|
|
333
|
+
const groups = /* @__PURE__ */ new Map();
|
|
334
|
+
for (const row of r.rows.filter((x) => x.offeredRequests > 0)) {
|
|
335
|
+
const g = groups.get(row.group) ?? { calls: 0, cost: 0, tools: 0, current: false };
|
|
336
|
+
g.calls += row.calls;
|
|
337
|
+
g.cost += row.cost;
|
|
338
|
+
g.tools++;
|
|
339
|
+
g.current ||= row.current;
|
|
340
|
+
groups.set(row.group, g);
|
|
341
|
+
}
|
|
342
|
+
lines.push("\nTool sources (tok each adds to every request it is loaded in, calls in range):");
|
|
343
|
+
for (const [name, g] of [...groups].sort((a, b) => b[1].cost - a[1].cost)) {
|
|
344
|
+
lines.push(`- ${sourceLabel(name)}: ${g.tools} tools, ${k(g.cost)} tok/request, ${g.calls} calls${g.calls === 0 ? " \u2190 never used" : ""}${g.current ? "" : " (not in the latest request: removed, or another profile)"}`);
|
|
345
|
+
}
|
|
346
|
+
const unused = r.rows.filter((x) => x.current && x.calls === 0).sort((a, b) => b.cost - a.cost);
|
|
347
|
+
if (unused.length) lines.push(`
|
|
348
|
+
Never called (${unused.length}, ${k(r.current.unusedCost)} tok per request), biggest: ${unused.slice(0, top).map((x) => `${x.name} ${k(x.cost)}`).join(", ")}`);
|
|
349
|
+
const failing = r.rows.filter((x) => x.calls >= 3 && x.errors / x.calls >= 0.2);
|
|
350
|
+
if (failing.length) lines.push(`
|
|
351
|
+
Often failing: ${failing.map((x) => `${x.name} ${x.errors}/${x.calls}, ${k(x.cost)} tok/request (${x.recentErrors[0]?.text.slice(0, 90) ?? ""})`).join("; ")}`);
|
|
352
|
+
lines.push("\nTo turn a source off: plugin_manager list_plugins \u2192 set_plugin enabled=false. Ask the user first; it changes every session.");
|
|
353
|
+
return lines.join("\n");
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
// src/cli.ts
|
|
357
|
+
var args = process.argv.slice(2);
|
|
358
|
+
var take = (flag) => {
|
|
359
|
+
const i = args.indexOf(flag);
|
|
360
|
+
return i < 0 ? null : args.splice(i, 2)[1] ?? "";
|
|
361
|
+
};
|
|
362
|
+
var errorsFor = take("--errors");
|
|
363
|
+
var asJson = args.includes("--json");
|
|
364
|
+
var asAgent = args.includes("--agent");
|
|
365
|
+
var days = clampDays(args.find((a) => /^\d+$/.test(a)) ?? 30);
|
|
366
|
+
var report = collect(days);
|
|
367
|
+
if (errorsFor !== null) {
|
|
368
|
+
const row = report.rows.find((r) => r.name === errorsFor);
|
|
369
|
+
if (!row) console.log(`unknown tool: ${errorsFor}`);
|
|
370
|
+
else console.log(row.recentErrors.map((e) => `${new Date(e.time).toISOString().slice(0, 16)} ${e.text}`).join("\n") || "no errors");
|
|
371
|
+
} else if (asAgent) {
|
|
372
|
+
console.log(agentSummary(report));
|
|
373
|
+
} else if (asJson) {
|
|
374
|
+
console.log(JSON.stringify(report, null, 1));
|
|
375
|
+
} else {
|
|
376
|
+
const pad = (v, n) => String(v).padStart(n);
|
|
377
|
+
const ago = (ms) => ms ? `${Math.round((Date.now() - ms) / 864e5)}d` : "never";
|
|
378
|
+
const groups = /* @__PURE__ */ new Map();
|
|
379
|
+
for (const r of report.rows.filter((r2) => r2.current || r2.calls > 0)) groups.set(r.group, [...groups.get(r.group) ?? [], r]);
|
|
380
|
+
console.log(`deep-toolbox \u2014 last ${days} days, ${report.sessions} sessions
|
|
381
|
+
`);
|
|
382
|
+
console.log(`${"tool".padEnd(44)}${pad("calls", 7)}${pad("err", 5)}${pad("avg s", 7)}${pad("cost", 6)}${pad("last", 7)}`);
|
|
383
|
+
for (const [group, rows] of [...groups].sort((a, b) => sumCalls(b[1]) - sumCalls(a[1]))) {
|
|
384
|
+
console.log(`
|
|
385
|
+
${group} \u2014 ${sumCalls(rows)} calls, ${rows.reduce((n, r) => n + r.cost, 0).toLocaleString()} tok/prompt`);
|
|
386
|
+
for (const r of rows) {
|
|
387
|
+
console.log(` ${r.name.padEnd(42)}${pad(r.calls, 7)}${pad(r.errors || "", 5)}${pad(r.avgMs === null ? "-" : (r.avgMs / 1e3).toFixed(1), 7)}${pad(r.cost, 6)}${pad(ago(r.lastUsed), 7)}${r.current ? "" : " (removed)"}`);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
const c = report.current;
|
|
391
|
+
console.log(`
|
|
392
|
+
Now offered: ${c.tools} tools, ${c.cost.toLocaleString()} tok per prompt. Never used in ${days} days: ${c.unused} tools, ${c.unusedCost.toLocaleString()} tok.`);
|
|
393
|
+
}
|
|
394
|
+
function sumCalls(rows) {
|
|
395
|
+
return rows.reduce((n, r) => n + r.calls, 0);
|
|
396
|
+
}
|