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/lib/index.js
ADDED
|
@@ -0,0 +1,494 @@
|
|
|
1
|
+
// src/stats.ts
|
|
2
|
+
import { readdirSync, readFileSync as readFileSync2, statSync } from "node:fs";
|
|
3
|
+
import { join as join2 } from "node:path";
|
|
4
|
+
import { homedir as homedir2 } from "node:os";
|
|
5
|
+
import { zstdDecompressSync } from "node:zlib";
|
|
6
|
+
|
|
7
|
+
// src/owners.ts
|
|
8
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
9
|
+
import { dirname, isAbsolute, join } from "node:path";
|
|
10
|
+
import { homedir } from "node:os";
|
|
11
|
+
import { fileURLToPath } from "node:url";
|
|
12
|
+
var ownersFile = () => join(process.env.DSH_HOME ?? join(homedir(), ".dsh"), "deep-toolbox", "owners.json");
|
|
13
|
+
function readOwners(file = ownersFile()) {
|
|
14
|
+
try {
|
|
15
|
+
return JSON.parse(readFileSync(file, "utf8")) ?? {};
|
|
16
|
+
} catch {
|
|
17
|
+
return {};
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
var isCore = (pkg) => pkg.startsWith("@deepseek-ai/");
|
|
21
|
+
var PLUMBING = /* @__PURE__ */ new Set(["@deepseek-ai/cordis", "@deepseek-ai/dsh-tools", "@deepseek-ai/dsh-scope"]);
|
|
22
|
+
var pkgCache = /* @__PURE__ */ new Map();
|
|
23
|
+
function packageOf(file) {
|
|
24
|
+
let dir = dirname(file);
|
|
25
|
+
const seen = [];
|
|
26
|
+
for (let i = 0; i < 15; i++) {
|
|
27
|
+
if (pkgCache.has(dir)) {
|
|
28
|
+
const hit = pkgCache.get(dir);
|
|
29
|
+
for (const d of seen) pkgCache.set(d, hit);
|
|
30
|
+
return hit;
|
|
31
|
+
}
|
|
32
|
+
seen.push(dir);
|
|
33
|
+
const pj = join(dir, "package.json");
|
|
34
|
+
if (existsSync(pj)) {
|
|
35
|
+
let name;
|
|
36
|
+
try {
|
|
37
|
+
name = JSON.parse(readFileSync(pj, "utf8")).name;
|
|
38
|
+
} catch {
|
|
39
|
+
}
|
|
40
|
+
if (typeof name === "string" && name) {
|
|
41
|
+
for (const d of seen) pkgCache.set(d, name);
|
|
42
|
+
return name;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
const up = dirname(dir);
|
|
46
|
+
if (up === dir) break;
|
|
47
|
+
dir = up;
|
|
48
|
+
}
|
|
49
|
+
for (const d of seen) pkgCache.set(d, void 0);
|
|
50
|
+
return void 0;
|
|
51
|
+
}
|
|
52
|
+
function stackFiles(stack) {
|
|
53
|
+
const files = [];
|
|
54
|
+
for (const raw of stack.split("\n").slice(1)) {
|
|
55
|
+
const m = raw.trim().match(/^at (?:.*?\()?(.+?):\d+:\d+\)?$/);
|
|
56
|
+
if (!m) continue;
|
|
57
|
+
let file = m[1];
|
|
58
|
+
if (file.startsWith("file://")) {
|
|
59
|
+
try {
|
|
60
|
+
file = fileURLToPath(file);
|
|
61
|
+
} catch {
|
|
62
|
+
continue;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
if (isAbsolute(file)) files.push(file);
|
|
66
|
+
}
|
|
67
|
+
return files;
|
|
68
|
+
}
|
|
69
|
+
function callerPackage(stack) {
|
|
70
|
+
for (const file of stackFiles(stack).slice(1)) {
|
|
71
|
+
const pkg = packageOf(file);
|
|
72
|
+
if (pkg && !PLUMBING.has(pkg)) return pkg;
|
|
73
|
+
}
|
|
74
|
+
return void 0;
|
|
75
|
+
}
|
|
76
|
+
var ORIGINAL = /* @__PURE__ */ Symbol.for("cordis.original");
|
|
77
|
+
function trackOwners(ctx, file = ownersFile()) {
|
|
78
|
+
const svc = ctx.get?.("tools", false);
|
|
79
|
+
const target = svc?.[ORIGINAL] ?? svc;
|
|
80
|
+
if (!target || typeof target.register !== "function") return () => {
|
|
81
|
+
};
|
|
82
|
+
const original = target.register;
|
|
83
|
+
const owners = readOwners(file);
|
|
84
|
+
let timer;
|
|
85
|
+
const save = () => {
|
|
86
|
+
clearTimeout(timer);
|
|
87
|
+
timer = setTimeout(() => {
|
|
88
|
+
try {
|
|
89
|
+
mkdirSync(dirname(file), { recursive: true });
|
|
90
|
+
writeFileSync(file, JSON.stringify(owners, null, 1));
|
|
91
|
+
} catch {
|
|
92
|
+
}
|
|
93
|
+
}, 500);
|
|
94
|
+
};
|
|
95
|
+
target.register = function register(definition, ...rest) {
|
|
96
|
+
const name = definition?.name;
|
|
97
|
+
if (typeof name === "string" && !name.startsWith("mcp__")) {
|
|
98
|
+
const pkg = callerPackage(new Error().stack ?? "");
|
|
99
|
+
if (pkg && isCore(pkg)) {
|
|
100
|
+
if (name in owners) {
|
|
101
|
+
delete owners[name];
|
|
102
|
+
save();
|
|
103
|
+
}
|
|
104
|
+
} else if (pkg && owners[name] !== pkg) {
|
|
105
|
+
owners[name] = pkg;
|
|
106
|
+
save();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
return Reflect.apply(original, this, [definition, ...rest]);
|
|
110
|
+
};
|
|
111
|
+
return () => {
|
|
112
|
+
clearTimeout(timer);
|
|
113
|
+
if (target.register !== original) target.register = original;
|
|
114
|
+
};
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
// src/stats.ts
|
|
118
|
+
var sessionsRoot = () => process.env.DSH_SESSIONS ?? join2(process.env.DSH_HOME ?? join2(homedir2(), ".dsh"), "sessions");
|
|
119
|
+
var tokens = (value) => Math.round((typeof value === "string" ? value : JSON.stringify(value ?? "")).length / 4);
|
|
120
|
+
var groupOf = (name, owners = {}) => name.startsWith("mcp__") ? `mcp:${name.split("__")[1]}` : owners[name] ? `plugin:${owners[name]}` : "built-in";
|
|
121
|
+
var clampDays = (raw) => {
|
|
122
|
+
const n = Number(raw);
|
|
123
|
+
return Number.isFinite(n) ? Math.min(365, Math.max(1, Math.round(n))) : 30;
|
|
124
|
+
};
|
|
125
|
+
var DAY = 864e5;
|
|
126
|
+
var sourceLabel = (group) => group === "built-in" ? "Built-in tools" : group.startsWith("mcp:") ? `MCP: ${group.slice(4)}` : `Plugin: ${group.slice(7)}`;
|
|
127
|
+
function regroupTools(startup, owners) {
|
|
128
|
+
const groups = /* @__PURE__ */ new Map();
|
|
129
|
+
for (const t of startup.tools) {
|
|
130
|
+
const g = groupOf(t.name, owners);
|
|
131
|
+
groups.set(g, (groups.get(g) ?? 0) + t.tokens);
|
|
132
|
+
}
|
|
133
|
+
return [...startup.parts, ...[...groups].map(([g, tok]) => ({ kind: "tools", label: sourceLabel(g), tokens: tok }))];
|
|
134
|
+
}
|
|
135
|
+
var text = (content) => typeof content === "string" ? content : Array.isArray(content) ? content.map((c) => typeof c?.text === "string" ? c.text : "").join("") : "";
|
|
136
|
+
function instructionParts(body) {
|
|
137
|
+
const parts = [];
|
|
138
|
+
let file = "", buf = "";
|
|
139
|
+
const flush = () => {
|
|
140
|
+
if (buf.trim()) parts.push(file ? { kind: "instructions", label: file, tokens: tokens(buf) } : { kind: "context", label: "Other", tokens: tokens(buf) });
|
|
141
|
+
};
|
|
142
|
+
for (const line of body.split("\n")) {
|
|
143
|
+
const from = line.match(/^Instructions from: (.+)$/);
|
|
144
|
+
if (from) {
|
|
145
|
+
flush();
|
|
146
|
+
file = from[1].trim();
|
|
147
|
+
buf = "";
|
|
148
|
+
}
|
|
149
|
+
buf += line + "\n";
|
|
150
|
+
}
|
|
151
|
+
flush();
|
|
152
|
+
return parts;
|
|
153
|
+
}
|
|
154
|
+
var SOURCE_LABEL = {
|
|
155
|
+
"system-prompt": ["system", "System prompt"],
|
|
156
|
+
"skill-catalog": ["skills", "Skill list"],
|
|
157
|
+
"runtime-context": ["context", "Other"],
|
|
158
|
+
"working-directory": ["context", "Other"],
|
|
159
|
+
"time-context": ["context", "Other"]
|
|
160
|
+
};
|
|
161
|
+
function parseSession(raw) {
|
|
162
|
+
const facts = { depth: 0, calls: [], offered: /* @__PURE__ */ new Map(), latest: null, startup: null };
|
|
163
|
+
const pending = /* @__PURE__ */ new Map();
|
|
164
|
+
let lastTime = 0;
|
|
165
|
+
let early = [];
|
|
166
|
+
for (const line of raw.split("\n")) {
|
|
167
|
+
if (!line) continue;
|
|
168
|
+
let e;
|
|
169
|
+
try {
|
|
170
|
+
e = JSON.parse(line);
|
|
171
|
+
} catch {
|
|
172
|
+
continue;
|
|
173
|
+
}
|
|
174
|
+
const time = typeof e.time === "number" ? lastTime = e.time : lastTime;
|
|
175
|
+
switch (e.type) {
|
|
176
|
+
case "session":
|
|
177
|
+
facts.depth = e.delegationDepth ?? 0;
|
|
178
|
+
break;
|
|
179
|
+
case "system/message":
|
|
180
|
+
case "user/message": {
|
|
181
|
+
if (!early) break;
|
|
182
|
+
const m = e.data?.message ?? e.data;
|
|
183
|
+
const kind = m?.source?.kind ?? "";
|
|
184
|
+
if (kind === "user") break;
|
|
185
|
+
if (kind === "agent-instructions") {
|
|
186
|
+
early.push(...instructionParts(text(m.content)));
|
|
187
|
+
break;
|
|
188
|
+
}
|
|
189
|
+
const [k, label] = SOURCE_LABEL[kind] ?? ["context", kind || "Other"];
|
|
190
|
+
const tok = tokens(text(m?.content));
|
|
191
|
+
if (tok > 0) early.push({ kind: k, label, tokens: tok });
|
|
192
|
+
break;
|
|
193
|
+
}
|
|
194
|
+
case "request/header": {
|
|
195
|
+
const defs = e.data?.header?.tools ?? [];
|
|
196
|
+
const names = defs.map((d) => d.name);
|
|
197
|
+
const day = Math.floor(time / DAY);
|
|
198
|
+
for (const def of defs) {
|
|
199
|
+
const cost = tokens(def);
|
|
200
|
+
let use = facts.offered.get(def.name);
|
|
201
|
+
if (!use) facts.offered.set(def.name, use = /* @__PURE__ */ new Map());
|
|
202
|
+
const d = use.get(day) ?? { req: 0, sent: 0, cost: 0, at: 0 };
|
|
203
|
+
d.req++;
|
|
204
|
+
d.sent += cost;
|
|
205
|
+
if (time >= d.at) {
|
|
206
|
+
d.cost = cost;
|
|
207
|
+
d.at = time;
|
|
208
|
+
}
|
|
209
|
+
use.set(day, d);
|
|
210
|
+
}
|
|
211
|
+
if (!facts.latest || time >= facts.latest.time) facts.latest = { time, names };
|
|
212
|
+
if (early) {
|
|
213
|
+
if (facts.depth === 0) facts.startup = { time, parts: early, tools: defs.map((d) => ({ name: d.name, tokens: tokens(d) })) };
|
|
214
|
+
early = null;
|
|
215
|
+
}
|
|
216
|
+
break;
|
|
217
|
+
}
|
|
218
|
+
case "tool/call":
|
|
219
|
+
pending.set(e.data?.callId, facts.calls.length);
|
|
220
|
+
facts.calls.push({ name: e.data?.name ?? "?", time, ms: null, resultTok: null, error: null });
|
|
221
|
+
break;
|
|
222
|
+
case "tool/result": {
|
|
223
|
+
const m = e.data?.message;
|
|
224
|
+
const i = pending.get(m?.toolCallId);
|
|
225
|
+
if (i === void 0) break;
|
|
226
|
+
pending.delete(m.toolCallId);
|
|
227
|
+
const call = facts.calls[i];
|
|
228
|
+
call.ms = time - call.time;
|
|
229
|
+
call.resultTok = tokens(m.content);
|
|
230
|
+
if (m.isError) call.error = text(m.content).replace(/\s+/g, " ").trim().slice(0, 300) || "error";
|
|
231
|
+
break;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return facts;
|
|
236
|
+
}
|
|
237
|
+
var ZSTD_MAGIC = 4247762216;
|
|
238
|
+
function decodeZstdFrames(buf) {
|
|
239
|
+
const out = [];
|
|
240
|
+
let off = 0;
|
|
241
|
+
while (off + 5 <= buf.length && buf.readUInt32LE(off) === ZSTD_MAGIC) {
|
|
242
|
+
const start = off;
|
|
243
|
+
const fhd = buf.readUInt8(off + 4);
|
|
244
|
+
const fcsFlag = fhd >>> 6, single = (fhd & 32) !== 0, checksum = (fhd & 4) !== 0, dictFlag = fhd & 3;
|
|
245
|
+
off += 5 + (single ? 0 : 1) + (dictFlag === 3 ? 4 : dictFlag) + (fcsFlag === 0 ? single ? 1 : 0 : 1 << fcsFlag);
|
|
246
|
+
for (; ; ) {
|
|
247
|
+
if (off + 3 > buf.length) return Buffer.concat(out).toString("utf8");
|
|
248
|
+
const header = buf.readUIntLE(off, 3);
|
|
249
|
+
off += 3 + ((header >>> 1 & 3) === 1 ? 1 : header >>> 3);
|
|
250
|
+
if (header & 1) break;
|
|
251
|
+
}
|
|
252
|
+
if (checksum) off += 4;
|
|
253
|
+
if (off > buf.length) break;
|
|
254
|
+
out.push(zstdDecompressSync(buf.subarray(start, off)));
|
|
255
|
+
}
|
|
256
|
+
return Buffer.concat(out).toString("utf8");
|
|
257
|
+
}
|
|
258
|
+
var cache = /* @__PURE__ */ new Map();
|
|
259
|
+
var LOG_NAMES = ["session.v4.jsonl.zstd", "session.v3.jsonl.zstd"];
|
|
260
|
+
function* sessionFiles(root) {
|
|
261
|
+
let dirs = [];
|
|
262
|
+
try {
|
|
263
|
+
dirs = readdirSync(root);
|
|
264
|
+
} catch {
|
|
265
|
+
return;
|
|
266
|
+
}
|
|
267
|
+
for (const dir of dirs) {
|
|
268
|
+
let ids = [];
|
|
269
|
+
try {
|
|
270
|
+
ids = readdirSync(join2(root, dir));
|
|
271
|
+
} catch {
|
|
272
|
+
continue;
|
|
273
|
+
}
|
|
274
|
+
for (const id of ids) {
|
|
275
|
+
for (const name of LOG_NAMES) {
|
|
276
|
+
const path = join2(root, dir, id, name);
|
|
277
|
+
try {
|
|
278
|
+
const st = statSync(path);
|
|
279
|
+
yield { path, mtimeMs: st.mtimeMs, size: st.size };
|
|
280
|
+
break;
|
|
281
|
+
} catch {
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
function factsFor(file) {
|
|
288
|
+
const key = `${file.mtimeMs}:${file.size}`;
|
|
289
|
+
const hit = cache.get(file.path);
|
|
290
|
+
if (hit?.key === key) return hit.facts;
|
|
291
|
+
try {
|
|
292
|
+
const facts = parseSession(decodeZstdFrames(readFileSync2(file.path)));
|
|
293
|
+
cache.set(file.path, { key, facts });
|
|
294
|
+
return facts;
|
|
295
|
+
} catch {
|
|
296
|
+
return null;
|
|
297
|
+
}
|
|
298
|
+
}
|
|
299
|
+
function collect(days = 30, root = sessionsRoot(), now = Date.now(), owners = readOwners()) {
|
|
300
|
+
const since = now - days * DAY;
|
|
301
|
+
const sinceDay = Math.floor(since / DAY);
|
|
302
|
+
const rows = /* @__PURE__ */ new Map();
|
|
303
|
+
const row = (name) => {
|
|
304
|
+
let r = rows.get(name);
|
|
305
|
+
if (!r) {
|
|
306
|
+
r = {
|
|
307
|
+
name,
|
|
308
|
+
group: groupOf(name, owners),
|
|
309
|
+
calls: 0,
|
|
310
|
+
subCalls: 0,
|
|
311
|
+
errors: 0,
|
|
312
|
+
avgMs: null,
|
|
313
|
+
avgResultTok: null,
|
|
314
|
+
cost: 0,
|
|
315
|
+
offeredIn: 0,
|
|
316
|
+
offeredRequests: 0,
|
|
317
|
+
sentTok: 0,
|
|
318
|
+
lastUsed: 0,
|
|
319
|
+
current: false,
|
|
320
|
+
recentErrors: [],
|
|
321
|
+
msSum: 0,
|
|
322
|
+
msN: 0,
|
|
323
|
+
resSum: 0,
|
|
324
|
+
resN: 0,
|
|
325
|
+
costAt: 0
|
|
326
|
+
};
|
|
327
|
+
rows.set(name, r);
|
|
328
|
+
}
|
|
329
|
+
return r;
|
|
330
|
+
};
|
|
331
|
+
let sessions = 0;
|
|
332
|
+
let latest = null;
|
|
333
|
+
let startup = null;
|
|
334
|
+
const seen = /* @__PURE__ */ new Set();
|
|
335
|
+
for (const file of sessionFiles(root)) {
|
|
336
|
+
seen.add(file.path);
|
|
337
|
+
if (file.mtimeMs < since) continue;
|
|
338
|
+
const facts = factsFor(file);
|
|
339
|
+
if (!facts) continue;
|
|
340
|
+
sessions++;
|
|
341
|
+
for (const [name, byDay] of facts.offered) {
|
|
342
|
+
let inRange = false;
|
|
343
|
+
for (const [day, d] of byDay) {
|
|
344
|
+
if (day < sinceDay) continue;
|
|
345
|
+
const r = row(name);
|
|
346
|
+
inRange = true;
|
|
347
|
+
r.offeredRequests += d.req;
|
|
348
|
+
r.sentTok += d.sent;
|
|
349
|
+
if (d.at >= r.costAt) {
|
|
350
|
+
r.cost = d.cost;
|
|
351
|
+
r.costAt = d.at;
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
if (inRange) row(name).offeredIn++;
|
|
355
|
+
}
|
|
356
|
+
if (facts.depth === 0 && facts.latest && (!latest || facts.latest.time > latest.time)) latest = facts.latest;
|
|
357
|
+
if (facts.startup && (!startup || facts.startup.time > startup.time)) startup = facts.startup;
|
|
358
|
+
for (const c of facts.calls) {
|
|
359
|
+
if (c.time < since) continue;
|
|
360
|
+
const r = row(c.name);
|
|
361
|
+
r.calls++;
|
|
362
|
+
if (facts.depth > 0) r.subCalls++;
|
|
363
|
+
r.lastUsed = Math.max(r.lastUsed, c.time);
|
|
364
|
+
if (c.ms !== null) {
|
|
365
|
+
r.msSum += c.ms;
|
|
366
|
+
r.msN++;
|
|
367
|
+
}
|
|
368
|
+
if (c.resultTok !== null) {
|
|
369
|
+
r.resSum += c.resultTok;
|
|
370
|
+
r.resN++;
|
|
371
|
+
}
|
|
372
|
+
if (c.error !== null) {
|
|
373
|
+
r.errors++;
|
|
374
|
+
r.recentErrors.push({ time: c.time, text: c.error });
|
|
375
|
+
}
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
for (const path of cache.keys()) if (!seen.has(path)) cache.delete(path);
|
|
379
|
+
const currentNames = new Set(latest?.names ?? []);
|
|
380
|
+
const out = [...rows.values()].map(({ msSum, msN, resSum, resN, costAt, ...r }) => ({
|
|
381
|
+
...r,
|
|
382
|
+
avgMs: msN ? Math.round(msSum / msN) : null,
|
|
383
|
+
avgResultTok: resN ? Math.round(resSum / resN) : null,
|
|
384
|
+
current: currentNames.has(r.name),
|
|
385
|
+
recentErrors: r.recentErrors.sort((a, b) => b.time - a.time).slice(0, 20)
|
|
386
|
+
}));
|
|
387
|
+
out.sort((a, b) => b.calls - a.calls || b.cost - a.cost || a.name.localeCompare(b.name));
|
|
388
|
+
const cur = out.filter((r) => r.current);
|
|
389
|
+
const unused = cur.filter((r) => r.calls === 0);
|
|
390
|
+
const sum = (rs) => rs.reduce((n, r) => n + r.cost, 0);
|
|
391
|
+
const parts = mergeParts(startup ? regroupTools(startup, owners) : []);
|
|
392
|
+
return {
|
|
393
|
+
days,
|
|
394
|
+
generatedAt: now,
|
|
395
|
+
sessions,
|
|
396
|
+
rows: out,
|
|
397
|
+
current: { tools: cur.length, cost: sum(cur), unused: unused.length, unusedCost: sum(unused) },
|
|
398
|
+
startup: { total: parts.reduce((n, p) => n + p.tokens, 0), parts }
|
|
399
|
+
};
|
|
400
|
+
}
|
|
401
|
+
function mergeParts(raw) {
|
|
402
|
+
const byLabel = /* @__PURE__ */ new Map();
|
|
403
|
+
for (const p of raw) {
|
|
404
|
+
const hit = byLabel.get(p.label);
|
|
405
|
+
if (hit) hit.tokens += p.tokens;
|
|
406
|
+
else byLabel.set(p.label, { ...p });
|
|
407
|
+
}
|
|
408
|
+
const total = [...byLabel.values()].reduce((n, p) => n + p.tokens, 0);
|
|
409
|
+
const keep = [];
|
|
410
|
+
let other = 0;
|
|
411
|
+
for (const p of byLabel.values()) {
|
|
412
|
+
if (p.label === "Other" || p.tokens < total * 0.01) other += p.tokens;
|
|
413
|
+
else keep.push(p);
|
|
414
|
+
}
|
|
415
|
+
keep.sort((a, b) => b.tokens - a.tokens);
|
|
416
|
+
if (other > 0) keep.push({ kind: "context", label: "Other", tokens: other });
|
|
417
|
+
return keep;
|
|
418
|
+
}
|
|
419
|
+
function agentSummary(r, top = 8) {
|
|
420
|
+
const lines = [];
|
|
421
|
+
const k = (n) => n >= 1e3 ? `${(n / 1e3).toFixed(1)}k` : String(n);
|
|
422
|
+
lines.push(`Last ${r.days} days, ${r.sessions} sessions. Every request carries ${r.current.tools} tool definitions = ${k(r.current.cost)} tok.`);
|
|
423
|
+
if (r.startup.total) {
|
|
424
|
+
lines.push(`
|
|
425
|
+
Startup context of a new chat \u2248 ${k(r.startup.total)} tok:`);
|
|
426
|
+
for (const p of r.startup.parts.slice(0, top)) lines.push(`- ${p.label}: ${k(p.tokens)}`);
|
|
427
|
+
}
|
|
428
|
+
const groups = /* @__PURE__ */ new Map();
|
|
429
|
+
for (const row of r.rows.filter((x) => x.offeredRequests > 0)) {
|
|
430
|
+
const g = groups.get(row.group) ?? { calls: 0, cost: 0, tools: 0, current: false };
|
|
431
|
+
g.calls += row.calls;
|
|
432
|
+
g.cost += row.cost;
|
|
433
|
+
g.tools++;
|
|
434
|
+
g.current ||= row.current;
|
|
435
|
+
groups.set(row.group, g);
|
|
436
|
+
}
|
|
437
|
+
lines.push("\nTool sources (tok each adds to every request it is loaded in, calls in range):");
|
|
438
|
+
for (const [name, g] of [...groups].sort((a, b) => b[1].cost - a[1].cost)) {
|
|
439
|
+
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)"}`);
|
|
440
|
+
}
|
|
441
|
+
const unused = r.rows.filter((x) => x.current && x.calls === 0).sort((a, b) => b.cost - a.cost);
|
|
442
|
+
if (unused.length) lines.push(`
|
|
443
|
+
Never called (${unused.length}, ${k(r.current.unusedCost)} tok per request), biggest: ${unused.slice(0, top).map((x) => `${x.name} ${k(x.cost)}`).join(", ")}`);
|
|
444
|
+
const failing = r.rows.filter((x) => x.calls >= 3 && x.errors / x.calls >= 0.2);
|
|
445
|
+
if (failing.length) lines.push(`
|
|
446
|
+
Often failing: ${failing.map((x) => `${x.name} ${x.errors}/${x.calls}, ${k(x.cost)} tok/request (${x.recentErrors[0]?.text.slice(0, 90) ?? ""})`).join("; ")}`);
|
|
447
|
+
lines.push("\nTo turn a source off: plugin_manager list_plugins \u2192 set_plugin enabled=false. Ask the user first; it changes every session.");
|
|
448
|
+
return lines.join("\n");
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
// src/index.ts
|
|
452
|
+
var inject = ["agents"];
|
|
453
|
+
var json = (body, status = 200) => new Response(JSON.stringify(body), { status, headers: { "content-type": "application/json", "cache-control": "no-store" } });
|
|
454
|
+
var reportTool = {
|
|
455
|
+
name: "toolbox_report",
|
|
456
|
+
description: "Tool usage report from this dsh install's session logs: calls, errors, and the tokens each tool definition and startup-context part adds to every request. Use when the user asks about tool/context cost, or to suggest disabling unused tool sources (then ask the user before using plugin_manager).",
|
|
457
|
+
parameters: {
|
|
458
|
+
type: "object",
|
|
459
|
+
properties: { days: { type: "number", description: "Look-back window in days (1-365, default 30)." } }
|
|
460
|
+
},
|
|
461
|
+
output: {
|
|
462
|
+
schema: { type: "object", properties: { report: { type: "string" } }, required: ["report"] },
|
|
463
|
+
render: (_args, value) => [{ type: "text", text: value?.report ?? "" }]
|
|
464
|
+
},
|
|
465
|
+
execute: async (args) => ({ report: agentSummary(collect(clampDays(args?.days ?? 30))) })
|
|
466
|
+
};
|
|
467
|
+
function apply(ctx) {
|
|
468
|
+
ctx.effect(() => trackOwners(ctx), "deep-toolbox.owners");
|
|
469
|
+
ctx.inject(["connection"], (ctx2) => {
|
|
470
|
+
ctx2.effect(() => ctx2.connection.fetch.register({
|
|
471
|
+
path: "/api/deep-toolbox/stats",
|
|
472
|
+
methods: ["GET"],
|
|
473
|
+
requestBody: "buffered",
|
|
474
|
+
// required by dsh's route registry; GET has no body anyway
|
|
475
|
+
fetch: async (request) => {
|
|
476
|
+
try {
|
|
477
|
+
return json({ ok: true, ...collect(clampDays(new URL(request.url, "http://x").searchParams.get("days") ?? 30)) });
|
|
478
|
+
} catch (error) {
|
|
479
|
+
return json({ ok: false, error: error instanceof Error ? error.message : String(error) }, 500);
|
|
480
|
+
}
|
|
481
|
+
}
|
|
482
|
+
}), "deep-toolbox.route/stats");
|
|
483
|
+
});
|
|
484
|
+
const add = (agent) => ctx.effect(() => agent.ctx.tools.register(reportTool), "deep-toolbox.tool.toolbox_report");
|
|
485
|
+
ctx.on("agent/created", ({ agent }) => add(agent));
|
|
486
|
+
try {
|
|
487
|
+
for (const agent of ctx.agents.roots?.() ?? []) add(agent);
|
|
488
|
+
} catch {
|
|
489
|
+
}
|
|
490
|
+
}
|
|
491
|
+
export {
|
|
492
|
+
apply,
|
|
493
|
+
inject
|
|
494
|
+
};
|
package/package.json
CHANGED
|
@@ -1,6 +1,89 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "deep-toolbox",
|
|
3
|
-
"version": "0.
|
|
4
|
-
"
|
|
5
|
-
"
|
|
6
|
-
|
|
3
|
+
"version": "0.1.4",
|
|
4
|
+
"description": "See which tools your DeepSeek Harness agents actually use, how often each one fails, and what its definition costs in every request — so you know which tools and MCP servers to keep or drop.",
|
|
5
|
+
"icon": "./icon.svg",
|
|
6
|
+
"author": "Johannes Tebbert <info@joblessjoe.com> (https://joblessjoe.com)",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "lib/index.js",
|
|
9
|
+
"bin": {
|
|
10
|
+
"deep-toolbox": "lib/cli.js"
|
|
11
|
+
},
|
|
12
|
+
"exports": {
|
|
13
|
+
".": {
|
|
14
|
+
"default": "./lib/index.js"
|
|
15
|
+
},
|
|
16
|
+
"./client": {
|
|
17
|
+
"default": "./lib/client.js"
|
|
18
|
+
},
|
|
19
|
+
"./package.json": "./package.json"
|
|
20
|
+
},
|
|
21
|
+
"dsh": {
|
|
22
|
+
"bundle": {
|
|
23
|
+
"patch": "./cordis.patch.yml"
|
|
24
|
+
},
|
|
25
|
+
"client": {
|
|
26
|
+
"platform": "web"
|
|
27
|
+
},
|
|
28
|
+
"compatibility": {
|
|
29
|
+
"dsh": ">=0.2.2-rc.1 <0.4.0-0"
|
|
30
|
+
}
|
|
31
|
+
},
|
|
32
|
+
"dshhub": {
|
|
33
|
+
"schemaVersion": 1,
|
|
34
|
+
"displayName": "Tool usage & startup cost",
|
|
35
|
+
"summary": "Tool usage, error rates and per-request token cost of every built-in and MCP tool, across all sessions.",
|
|
36
|
+
"categories": [
|
|
37
|
+
"Observability",
|
|
38
|
+
"Web UI",
|
|
39
|
+
"Tools"
|
|
40
|
+
],
|
|
41
|
+
"surfaces": [
|
|
42
|
+
"host",
|
|
43
|
+
"web"
|
|
44
|
+
],
|
|
45
|
+
"compatibility": {
|
|
46
|
+
"dsh": ">=0.2.2-rc.1 <0.4.0-0",
|
|
47
|
+
"node": ">=22.15"
|
|
48
|
+
},
|
|
49
|
+
"permissions": {}
|
|
50
|
+
},
|
|
51
|
+
"files": [
|
|
52
|
+
"lib",
|
|
53
|
+
"cordis.patch.yml",
|
|
54
|
+
"icon.svg"
|
|
55
|
+
],
|
|
56
|
+
"peerDependencies": {
|
|
57
|
+
"react": "^18.0.0 || ^19.0.0"
|
|
58
|
+
},
|
|
59
|
+
"repository": {
|
|
60
|
+
"type": "git",
|
|
61
|
+
"url": "git+https://github.com/JoblessJoe/deep-toolbox.git"
|
|
62
|
+
},
|
|
63
|
+
"keywords": [
|
|
64
|
+
"deepseek",
|
|
65
|
+
"deepseek-harness",
|
|
66
|
+
"dsh",
|
|
67
|
+
"dsh-plugin",
|
|
68
|
+
"client-plugin",
|
|
69
|
+
"mcp",
|
|
70
|
+
"tools",
|
|
71
|
+
"token",
|
|
72
|
+
"cost",
|
|
73
|
+
"usage",
|
|
74
|
+
"observability",
|
|
75
|
+
"工具统计",
|
|
76
|
+
"dsh插件"
|
|
77
|
+
],
|
|
78
|
+
"license": "MIT",
|
|
79
|
+
"engines": {
|
|
80
|
+
"node": ">=22.15"
|
|
81
|
+
},
|
|
82
|
+
"scripts": {
|
|
83
|
+
"build": "node scripts/build.mjs",
|
|
84
|
+
"test": "node test/stats.test.ts"
|
|
85
|
+
},
|
|
86
|
+
"devDependencies": {
|
|
87
|
+
"esbuild": "^0.28.2"
|
|
88
|
+
}
|
|
89
|
+
}
|