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/lib/client.js ADDED
@@ -0,0 +1,506 @@
1
+ // deep-toolbox client bundle (esbuild CJS + __ModuleLoader__ banner). Do not edit.
2
+ window.__ModuleLoader__.load({id:"deep-toolbox",factory:Pt=>{
3
+ var module={exports:{}},exports=module.exports,require=Pt;
4
+ "use strict";
5
+ var __defProp = Object.defineProperty;
6
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
7
+ var __getOwnPropNames = Object.getOwnPropertyNames;
8
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
9
+ var __export = (target, all) => {
10
+ for (var name in all)
11
+ __defProp(target, name, { get: all[name], enumerable: true });
12
+ };
13
+ var __copyProps = (to, from, except, desc) => {
14
+ if (from && typeof from === "object" || typeof from === "function") {
15
+ for (let key of __getOwnPropNames(from))
16
+ if (!__hasOwnProp.call(to, key) && key !== except)
17
+ __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
18
+ }
19
+ return to;
20
+ };
21
+ var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
22
+
23
+ // src/client/index.tsx
24
+ var index_exports = {};
25
+ __export(index_exports, {
26
+ TAB_ID: () => TAB_ID,
27
+ apply: () => apply,
28
+ inject: () => inject
29
+ });
30
+ module.exports = __toCommonJS(index_exports);
31
+ var import_react = require("react");
32
+
33
+ // src/client/locales.ts
34
+ var zh = {
35
+ "nav": "\u5DE5\u5177\u7528\u91CF\u4E0E\u542F\u52A8\u6210\u672C",
36
+ "subtitle": "\u5DF2\u5B89\u88C5\u7684\u6BCF\u4E2A\u5DE5\u5177\u88AB\u8C03\u7528\u4E86\u591A\u5C11\u6B21\uFF0C\u4EE5\u53CA\u5B83\u5728\u6BCF\u4E2A\u65B0\u5BF9\u8BDD\u7684\u542F\u52A8\u4E0A\u4E0B\u6587\u4E2D\u5360\u7528\u591A\u5C11 token\u3002",
37
+ "range.days": "{n} \u5929",
38
+ "filter.removed": "\u663E\u793A\u5DF2\u79FB\u9664",
39
+ "card.unused": "\u95F2\u7F6E\u5DE5\u5177\u7684\u6210\u672C",
40
+ "card.errors": "\u5931\u8D25\u7684\u8C03\u7528",
41
+ "card.unusedHint": "{n} \u4E2A\u5DE5\u5177\uFF0C{days} \u5929\u672A\u7528",
42
+ "col.tool": "\u5DE5\u5177",
43
+ "col.calls": "\u8C03\u7528",
44
+ "col.errors": "\u9519\u8BEF",
45
+ "col.time": "\u5E73\u5747\u8017\u65F6",
46
+ "col.result": "\u7ED3\u679C\u5927\u5C0F",
47
+ "col.cost": "\u6BCF\u4E2A\u5BF9\u8BDD",
48
+ "col.last": "\u6700\u8FD1\u4F7F\u7528",
49
+ "badge.unused": "\u672A\u4F7F\u7528",
50
+ "badge.removed": "\u5DF2\u79FB\u9664",
51
+ "badge.plugin": "\u63D2\u4EF6",
52
+ "badge.errors": "\u9519\u8BEF\u591A",
53
+ "group.builtin": "\u5185\u7F6E",
54
+ "errors.title": "\u6700\u8FD1\u9519\u8BEF",
55
+ "errors.none": "\u65E0\u9519\u8BEF\u8BB0\u5F55",
56
+ "never": "\u4ECE\u672A",
57
+ "empty": "\u6CA1\u6709\u7B26\u5408\u6761\u4EF6\u7684\u5DE5\u5177\u3002",
58
+ "loading": "\u6B63\u5728\u8BFB\u53D6\u4F1A\u8BDD\u65E5\u5FD7\u2026",
59
+ "error": "\u8BFB\u53D6\u5931\u8D25",
60
+ "retry": "\u91CD\u8BD5",
61
+ "refresh": "\u5237\u65B0",
62
+ "how": "token \u6570\u4E3A\u4F30\u7B97\uFF08\u5B57\u7B26 \xF7 4\uFF09\u3002\u201C\u5DF2\u53D1\u9001\u201D\u662F\u7F13\u5B58\u524D\u7684\u91CF\u3002",
63
+ "startup.title": "\u65B0\u5BF9\u8BDD\u7684\u542F\u52A8\u4E0A\u4E0B\u6587",
64
+ "startup.total": "\u7EA6 {tok} token",
65
+ "startup.hint": "\u6BCF\u4E2A\u65B0\u5BF9\u8BDD\u5728\u4F60\u8F93\u5165\u4E4B\u524D\u5C31\u5DF2\u5E26\u4E0A\u8FD9\u4E9B\u5185\u5BB9\uFF08\u53D6\u81EA\u6700\u8FD1\u4E00\u6B21\u4E3B\u5BF9\u8BDD\u7684\u7B2C\u4E00\u6B21\u8BF7\u6C42\uFF09\u3002",
66
+ "card.startup": "\u65B0\u5BF9\u8BDD\u8D77\u6B65",
67
+ "card.startupHint": "\u5176\u4E2D\u5DE5\u5177\u5B9A\u4E49 {tok}",
68
+ "card.callsHint": "{n} \u6B21\u8C03\u7528 \xB7 {s} \u4E2A\u4F1A\u8BDD",
69
+ "tips.title": "\u8981\u70B9",
70
+ "tips.how": "\u8981\u5173\u95ED\u67D0\u4E2A\u6765\u6E90\uFF1A\u63D2\u4EF6\u9875 \u203A \u5DF2\u5B89\u88C5\uFF0C\u6216\u8BA9\u667A\u80FD\u4F53\u7528 plugin_manager\uFF08\u4F1A\u5148\u95EE\u4F60\uFF09\u3002",
71
+ "tip.never": "{src} \u6BCF\u4E2A\u65B0\u5BF9\u8BDD\u90FD\u5360 {tok} token\uFF0C\u4F46\u6700\u8FD1 {days} \u5929\u4ECE\u672A\u4F7F\u7528\u3002\u53EF\u4EE5\u8003\u8651\u5173\u95ED\u3002",
72
+ "tip.rare": "{src} \u6700\u8FD1 {days} \u5929\u53EA\u7528\u4E86 {n} \u6B21\uFF0C\u5374\u5728\u6BCF\u4E2A\u65B0\u5BF9\u8BDD\u5360 {tok} token\u3002",
73
+ "tip.fails": "{tool} \u5728 {of} \u6B21\u8C03\u7528\u4E2D\u5931\u8D25 {n} \u6B21\uFF1A{why}",
74
+ "tip.builtin": "{n} \u4E2A\u5185\u7F6E\u5DE5\u5177\u6700\u8FD1 {days} \u5929\u4ECE\u672A\u4F7F\u7528\uFF0C\u5408\u8BA1\u6BCF\u4E2A\u5BF9\u8BDD {tok} token\u3002",
75
+ "tip.ok": "\u4E00\u5207\u6B63\u5E38\uFF1A\u6CA1\u6709\u95F2\u7F6E\u7684\u5927\u5DE5\u5177\u6765\u6E90\uFF0C\u4E5F\u6CA1\u6709\u7ECF\u5E38\u5931\u8D25\u7684\u5DE5\u5177\u3002",
76
+ "sources.title": "\u5DE5\u5177\u6765\u6E90",
77
+ "source.tool": "{n} \u4E2A\u5DE5\u5177",
78
+ "source.tools": "{n} \u4E2A\u5DE5\u5177",
79
+ "source.call": "{n} \u6B21\u8C03\u7528",
80
+ "source.calls": "{n} \u6B21\u8C03\u7528",
81
+ "source.cost": "\u6BCF\u4E2A\u5BF9\u8BDD {tok} token",
82
+ "verdict.busy": "\u5E38\u7528",
83
+ "verdict.rare": "\u5F88\u5C11\u7528",
84
+ "verdict.never": "\u4ECE\u672A\u4F7F\u7528"
85
+ };
86
+ var en = {
87
+ "nav": "Tool usage & startup cost",
88
+ "subtitle": "How often each installed tool is used, and how many tokens it adds to the startup context of every new chat.",
89
+ "range.days": "{n} days",
90
+ "filter.removed": "Show removed",
91
+ "card.unused": "Paid for, never used",
92
+ "card.errors": "Failed calls",
93
+ "card.unusedHint": "{n} tools not used in {days} days",
94
+ "col.tool": "Tool",
95
+ "col.calls": "Calls",
96
+ "col.errors": "Errors",
97
+ "col.time": "Avg time",
98
+ "col.result": "Result",
99
+ "col.cost": "Per chat",
100
+ "col.last": "Last used",
101
+ "badge.unused": "unused",
102
+ "badge.removed": "removed",
103
+ "badge.plugin": "Plugin",
104
+ "badge.errors": "often fails",
105
+ "group.builtin": "Built-in",
106
+ "errors.title": "Recent errors",
107
+ "errors.none": "No errors recorded",
108
+ "never": "never",
109
+ "empty": "No tools match.",
110
+ "loading": "Reading session logs\u2026",
111
+ "error": "Could not read stats",
112
+ "retry": "Retry",
113
+ "refresh": "Refresh",
114
+ "how": 'Token numbers are estimates (chars \xF7 4). "Sent" is before prompt caching.',
115
+ "startup.title": "Startup context of a new chat",
116
+ "startup.total": "\u2248 {tok} tok",
117
+ "startup.hint": "What every new chat carries before you type anything (from the newest main chat's first request).",
118
+ "card.startup": "A new chat starts with",
119
+ "card.startupHint": "{tok} of it are tool definitions",
120
+ "card.callsHint": "{n} calls \xB7 {s} sessions",
121
+ "tips.title": "What stands out",
122
+ "tips.how": "To turn a source off: Plugins \u203A Installed, or ask an agent to use plugin_manager (it asks you first).",
123
+ "tip.never": "{src} adds {tok} tok to every new chat but was not used in the last {days} days. Consider turning it off.",
124
+ "tip.rare": "{src} was used only {n} times in {days} days, but adds {tok} tok to every new chat.",
125
+ "tip.fails": "{tool} failed {n} of {of} calls: {why}",
126
+ "tip.builtin": "{n} built-in tools were never used in {days} days ({tok} tok per chat together).",
127
+ "tip.ok": "All good: no big unused tool sources and no tools that fail often.",
128
+ "sources.title": "Tool sources",
129
+ "source.tool": "{n} tool",
130
+ "source.tools": "{n} tools",
131
+ "source.call": "{n} call",
132
+ "source.calls": "{n} calls",
133
+ "source.cost": "{tok} tok per chat",
134
+ "verdict.busy": "used a lot",
135
+ "verdict.rare": "rarely used",
136
+ "verdict.never": "never used"
137
+ };
138
+
139
+ // src/client/index.tsx
140
+ var import_jsx_runtime = require("react/jsx-runtime");
141
+ var TAB_ID = "deep-toolbox";
142
+ var inject = ["slots", "locale"];
143
+ var PART_COLORS = ["#4f8fd9", "#e07b39", "#3fa37a", "#a66bd6", "#d9a441", "#d1566f", "#2fb3b8", "#8a9a3b", "#7c6f64", "#5b6ee1"];
144
+ var partColor = (label, i) => label === "Other" ? "var(--dtbx-c3)" : PART_COLORS[i % PART_COLORS.length];
145
+ var STYLE_ID = "dtbx-styles";
146
+ var CSS = `
147
+ .dtbx-root {
148
+ --dtbx-line: color-mix(in srgb, var(--dsw-alias-label-primary) 9%, transparent);
149
+ --dtbx-fill: var(--dsw-alias-bg-skeleton);
150
+ --dtbx-hover: var(--dsw-alias-interactive-bg-hover, var(--dsw-alias-bg-skeleton));
151
+ --dtbx-bar: color-mix(in srgb, var(--dsw-alias-label-primary) 55%, transparent);
152
+ --dtbx-warn: var(--dsw-alias-state-warning-primary, #d29922);
153
+ --dtbx-err: var(--dsw-alias-state-error-primary, #e5484d);
154
+ --dtbx-ok: var(--dsw-alias-state-success-primary, #30a46c);
155
+ --dtbx-c1: #8e7cc3; --dtbx-c2: #4f8fd9; --dtbx-c3: #9aa4b1; --dtbx-c4: #d9a441; --dtbx-c5: #3fa37a;
156
+ font-family: inherit; color: var(--dsw-alias-label-primary);
157
+ }
158
+ .dtbx-root svg { fill: currentColor; }
159
+ .dtbx-root button, .dtbx-root input { font-family: inherit; }
160
+ .dtbx-inner { width: 100%; max-width: 1080px; margin: 0 auto; box-sizing: border-box; padding: 8px 0 32px; display: flex; flex-direction: column; gap: 16px; }
161
+ .dtbx-sub { margin: 0; font-size: 13.5px; color: var(--dsw-alias-label-caption); }
162
+ .dtbx-toolbar { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
163
+ .dtbx-tabs { display: flex; gap: 4px; flex-wrap: wrap; }
164
+ .dtbx-tab { border: none; background: transparent; padding: 6px 12px; border-radius: 8px; cursor: pointer; font-size: 13.5px; color: var(--dsw-alias-label-secondary); }
165
+ .dtbx-tab:hover { background: var(--dtbx-hover); color: var(--dsw-alias-label-primary); }
166
+ .dtbx-tab[aria-selected="true"] { background: var(--dtbx-fill); color: var(--dsw-alias-label-primary); }
167
+ .dtbx-end { display: flex; align-items: center; gap: 6px; }
168
+ .dtbx-check { display: inline-flex; align-items: center; gap: 6px; font-size: 13px; color: var(--dsw-alias-label-secondary); cursor: pointer; user-select: none; }
169
+ .dtbx-cards { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; }
170
+ .dtbx-card { display: flex; flex-direction: column; gap: 4px; padding: 14px 16px; border-radius: 12px; background: var(--dtbx-fill); }
171
+ .dtbx-card-label { font-size: 12.5px; color: var(--dsw-alias-label-caption); }
172
+ .dtbx-card-value { font-size: 24px; font-weight: 600; font-variant-numeric: tabular-nums; }
173
+ .dtbx-card-hint { font-size: 12px; color: var(--dsw-alias-label-caption); }
174
+ .dtbx-card-warn .dtbx-card-value { color: var(--dtbx-warn); }
175
+ .dtbx-group-name { font-weight: 600; }
176
+ .dtbx-group-meta { margin-left: auto; display: flex; gap: 14px; font-size: 12.5px; color: var(--dsw-alias-label-caption); font-variant-numeric: tabular-nums; }
177
+ .dtbx-table { width: 100%; border-collapse: collapse; font-size: 13px; font-variant-numeric: tabular-nums; }
178
+ .dtbx-table th { text-align: right; font-weight: 500; font-size: 12px; color: var(--dsw-alias-label-caption); padding: 6px 14px; border-bottom: 1px solid var(--dtbx-line); white-space: nowrap; }
179
+ .dtbx-table th:first-child, .dtbx-table td:first-child { text-align: left; }
180
+ .dtbx-table td { text-align: right; padding: 7px 14px; border-bottom: 1px solid var(--dtbx-line); white-space: nowrap; }
181
+ .dtbx-table tr:last-child td { border-bottom: none; }
182
+ .dtbx-row { cursor: pointer; }
183
+ .dtbx-row:hover td { background: var(--dtbx-hover); }
184
+ .dtbx-name { display: flex; align-items: center; gap: 8px; min-width: 0; max-width: 360px; }
185
+ .dtbx-name-text { overflow: hidden; text-overflow: ellipsis; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12.5px; }
186
+ .dtbx-calls { display: inline-flex; align-items: center; gap: 8px; justify-content: flex-end; }
187
+ .dtbx-track { width: 64px; height: 6px; border-radius: 3px; background: var(--dtbx-line); overflow: hidden; }
188
+ .dtbx-fillbar { height: 100%; border-radius: 3px; background: var(--dtbx-bar); }
189
+ .dtbx-dim { color: var(--dsw-alias-label-caption); }
190
+ .dtbx-badge { flex: none; font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--dtbx-fill); color: var(--dsw-alias-label-caption); }
191
+ .dtbx-badge-unused { color: var(--dtbx-warn); background: color-mix(in srgb, var(--dtbx-warn) 12%, transparent); }
192
+ .dtbx-badge-err { color: var(--dtbx-err); background: color-mix(in srgb, var(--dtbx-err) 10%, transparent); }
193
+ .dtbx-err-num { color: var(--dtbx-err); }
194
+ .dtbx-errors { padding: 8px 14px 12px; background: color-mix(in srgb, var(--dtbx-fill) 50%, transparent); }
195
+ .dtbx-errors-title { font-size: 12px; color: var(--dsw-alias-label-caption); margin-bottom: 6px; }
196
+ .dtbx-error { display: flex; gap: 10px; font-size: 12.5px; padding: 3px 0; white-space: normal; text-align: left; }
197
+ .dtbx-error time { flex: none; color: var(--dsw-alias-label-caption); font-variant-numeric: tabular-nums; }
198
+ .dtbx-error span { overflow-wrap: anywhere; font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 12px; }
199
+ .dtbx-panel { display: flex; flex-direction: column; gap: 10px; }
200
+ .dtbx-tips { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
201
+ .dtbx-tip { display: flex; align-items: baseline; gap: 10px; padding: 10px 14px; border-radius: 10px; font-size: 13.5px; line-height: 1.45; background: var(--dtbx-fill); }
202
+ .dtbx-tip-dot { flex: none; width: 8px; height: 8px; border-radius: 50%; transform: translateY(-1px); background: var(--dtbx-ok); }
203
+ .dtbx-tip-warn .dtbx-tip-dot { background: var(--dtbx-warn); }
204
+ .dtbx-tip-err .dtbx-tip-dot { background: var(--dtbx-err); }
205
+ .dtbx-sources { display: flex; flex-direction: column; border: 1px solid var(--dtbx-line); border-radius: 12px; overflow: hidden; }
206
+ .dtbx-table-wrap { overflow-x: auto; border-top: 1px solid var(--dtbx-line); }
207
+ .dtbx-source + .dtbx-source { border-top: 1px solid var(--dtbx-line); }
208
+ .dtbx-source-head { display: flex; align-items: center; gap: 10px; width: 100%; padding: 11px 14px; border: none; background: transparent; cursor: pointer; font-size: 13.5px; color: var(--dsw-alias-label-primary); text-align: left; }
209
+ .dtbx-source-head:hover { background: var(--dtbx-hover); }
210
+ .dtbx-chev { display: inline-block; width: 10px; color: var(--dsw-alias-label-caption); transition: transform 120ms ease; }
211
+ .dtbx-chev-open { transform: rotate(90deg); }
212
+ .dtbx-strong { color: var(--dsw-alias-label-primary); font-weight: 550; }
213
+ .dtbx-verdict-busy { color: var(--dtbx-ok); background: color-mix(in srgb, var(--dtbx-ok) 12%, transparent); }
214
+ .dtbx-verdict-rare { color: var(--dtbx-warn); background: color-mix(in srgb, var(--dtbx-warn) 12%, transparent); }
215
+ .dtbx-verdict-never { color: var(--dtbx-err); background: color-mix(in srgb, var(--dtbx-err) 10%, transparent); }
216
+ .dtbx-link { align-self: flex-start; border: none; background: transparent; padding: 0; cursor: pointer; font-size: 13px; color: var(--dsw-alias-label-secondary); text-decoration: underline; text-underline-offset: 2px; }
217
+ .dtbx-startup { display: flex; flex-direction: column; gap: 10px; padding: 14px 16px; border-radius: 12px; border: 1px solid var(--dtbx-line); }
218
+ .dtbx-startup-head { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
219
+ .dtbx-h2 { margin: 0; font-size: 15px; font-weight: 600; }
220
+ .dtbx-stack { display: flex; height: 10px; border-radius: 5px; overflow: hidden; background: var(--dtbx-line); }
221
+ .dtbx-stack span { height: 100%; }
222
+ .dtbx-legend { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 4px 24px; font-size: 13px; }
223
+ .dtbx-legend-row { display: flex; align-items: center; gap: 8px; min-width: 0; }
224
+ .dtbx-dot { flex: none; width: 8px; height: 8px; border-radius: 2px; }
225
+ .dtbx-legend-label { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
226
+ .dtbx-legend-num { flex: none; color: var(--dsw-alias-label-caption); font-variant-numeric: tabular-nums; }
227
+ .dtbx-note { font-size: 12px; color: var(--dsw-alias-label-caption); }
228
+ .dtbx-notice { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 10px 14px; border-radius: 10px; font-size: 13px; color: var(--dtbx-err); background: color-mix(in srgb, var(--dtbx-err) 8%, transparent); }
229
+ .dtbx-btn { border: 1px solid var(--dtbx-line); background: transparent; border-radius: 8px; padding: 5px 10px; font-size: 13px; cursor: pointer; color: var(--dsw-alias-label-primary); }
230
+ .dtbx-btn:hover { background: var(--dtbx-hover); }
231
+ .dtbx-btn:disabled { opacity: 0.5; cursor: default; }
232
+ .dtbx-muted { color: var(--dsw-alias-label-caption); font-size: 13px; }
233
+ @media (max-width: 760px) {
234
+ .dtbx-cards { grid-template-columns: 1fr; }
235
+ .dtbx-group-meta span:not(.dtbx-strong) { display: none; }
236
+ .dtbx-legend { grid-template-columns: 1fr; }
237
+ .dtbx-hide-narrow { display: none; }
238
+ }
239
+ `;
240
+ function ensureStyles() {
241
+ if (typeof document === "undefined" || document.getElementById(STYLE_ID)) return;
242
+ const el = document.createElement("style");
243
+ el.id = STYLE_ID;
244
+ el.textContent = CSS;
245
+ document.head.appendChild(el);
246
+ }
247
+ var RANGES = [7, 30, 90];
248
+ var fmt = (n) => n.toLocaleString();
249
+ var compact = (n) => new Intl.NumberFormat(void 0, { notation: "compact", maximumFractionDigits: 1 }).format(n);
250
+ var secs = (ms) => ms === null ? "\u2013" : ms < 1e3 ? `${ms} ms` : `${(ms / 1e3).toFixed(1)} s`;
251
+ var failsOften = (r) => r.calls >= 3 && r.errors / r.calls >= 0.2;
252
+ function shortName(r) {
253
+ return r.group === "built-in" ? r.name : r.name.split("__").slice(2).join("__") || r.name;
254
+ }
255
+ function ago(ts, t) {
256
+ if (!ts) return t("never");
257
+ const days = Math.floor((Date.now() - ts) / 864e5);
258
+ if (days < 7) return new Intl.RelativeTimeFormat(void 0, { numeric: "auto" }).format(-days, "day");
259
+ return new Date(ts).toLocaleDateString(void 0, { day: "numeric", month: "short" });
260
+ }
261
+ var verdictOf = (calls) => calls === 0 ? "never" : calls < 10 ? "rare" : "busy";
262
+ function sourcesOf(report, t) {
263
+ var _a;
264
+ const by = /* @__PURE__ */ new Map();
265
+ for (const r of report.rows) by.set(r.group, [...(_a = by.get(r.group)) != null ? _a : [], r]);
266
+ return [...by].map(([group, rows]) => {
267
+ const cur = rows.filter((r) => r.current);
268
+ const calls = cur.reduce((n, r) => n + r.calls, 0);
269
+ return {
270
+ group,
271
+ rows,
272
+ calls,
273
+ kind: group === "built-in" ? "built-in" : group.startsWith("mcp:") ? "mcp" : "plugin",
274
+ label: group === "built-in" ? t("group.builtin") : group.slice(group.indexOf(":") + 1),
275
+ errors: cur.reduce((n, r) => n + r.errors, 0),
276
+ cost: cur.reduce((n, r) => n + r.cost, 0),
277
+ tools: cur.length,
278
+ sent: rows.reduce((n, r) => n + r.sentTok, 0),
279
+ verdict: verdictOf(calls)
280
+ };
281
+ }).filter((s) => s.tools > 0 || s.rows.some((r) => r.calls > 0)).sort((a, b) => b.cost - a.cost);
282
+ }
283
+ function tipsOf(report, sources, t) {
284
+ var _a, _b;
285
+ const tips = [];
286
+ const fill = (k, v) => Object.entries(v).reduce((s, [a, b]) => s.replace(`{${a}}`, () => b), t(k));
287
+ const days = String(report.days);
288
+ for (const r of report.rows.filter((x) => x.current && failsOften(x)).sort((a, b) => b.errors - a.errors)) {
289
+ tips.push({ level: "err", text: fill("tip.fails", { tool: r.name, n: String(r.errors), of: String(r.calls), why: (_b = (_a = r.recentErrors[0]) == null ? void 0 : _a.text.slice(0, 110)) != null ? _b : "" }) });
290
+ }
291
+ for (const s of sources.filter((x) => x.kind !== "built-in" && x.tools > 0 && x.verdict === "never")) {
292
+ tips.push({ level: "warn", text: fill("tip.never", { src: s.label, tok: compact(s.cost), days }) });
293
+ }
294
+ for (const s of sources.filter((x) => x.kind !== "built-in" && x.tools > 0 && x.verdict === "rare" && x.cost >= 1e3)) {
295
+ tips.push({ level: "warn", text: fill("tip.rare", { src: s.label, n: String(s.calls), tok: compact(s.cost), days }) });
296
+ }
297
+ const builtinUnused = report.rows.filter((r) => r.current && r.group === "built-in" && r.calls === 0);
298
+ if (builtinUnused.length >= 5) {
299
+ tips.push({ level: "warn", text: fill("tip.builtin", { n: String(builtinUnused.length), tok: compact(builtinUnused.reduce((n, r) => n + r.cost, 0)), days }) });
300
+ }
301
+ if (tips.length === 0) tips.push({ level: "ok", text: t("tip.ok") });
302
+ return tips;
303
+ }
304
+ function ToolboxPage({ ctx }) {
305
+ const t = (0, import_react.useMemo)(() => ctx.locale.bind("toolbox"), [ctx]);
306
+ const [days, setDays] = (0, import_react.useState)(30);
307
+ const [report, setReport] = (0, import_react.useState)(null);
308
+ const [error, setError] = (0, import_react.useState)(false);
309
+ const [loading, setLoading] = (0, import_react.useState)(false);
310
+ const [openSource, setOpenSource] = (0, import_react.useState)(null);
311
+ const [openTool, setOpenTool] = (0, import_react.useState)(null);
312
+ const [showRemoved, setShowRemoved] = (0, import_react.useState)(false);
313
+ const [tick, setTick] = (0, import_react.useState)(0);
314
+ const load = (0, import_react.useCallback)(() => setTick((n) => n + 1), []);
315
+ (0, import_react.useEffect)(() => {
316
+ const ac = new AbortController();
317
+ setLoading(true);
318
+ setError(false);
319
+ fetch(`/api/deep-toolbox/stats?days=${days}`, { headers: { accept: "application/json" }, signal: ac.signal }).then((r) => r.json()).then((d) => {
320
+ if (d == null ? void 0 : d.ok) setReport(d);
321
+ else setError(true);
322
+ }).catch(() => {
323
+ if (!ac.signal.aborted) setError(true);
324
+ }).finally(() => {
325
+ if (!ac.signal.aborted) setLoading(false);
326
+ });
327
+ return () => ac.abort();
328
+ }, [days, tick]);
329
+ const sources = (0, import_react.useMemo)(() => report ? sourcesOf(report, t) : [], [report, t]);
330
+ const tips = (0, import_react.useMemo)(() => report ? tipsOf(report, sources, t) : [], [report, sources, t]);
331
+ const totals = (0, import_react.useMemo)(() => {
332
+ var _a;
333
+ const rows = (_a = report == null ? void 0 : report.rows) != null ? _a : [];
334
+ const calls = rows.reduce((n, r) => n + r.calls, 0);
335
+ const errors = rows.reduce((n, r) => n + r.errors, 0);
336
+ return { calls, errors, maxCalls: Math.max(1, ...rows.map((r) => r.calls)) };
337
+ }, [report]);
338
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dtbx-root", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dtbx-inner", children: [
339
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dtbx-toolbar", children: [
340
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { className: "dtbx-sub", children: t("subtitle") }),
341
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dtbx-end", children: [
342
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dtbx-tabs", role: "tablist", children: RANGES.map((n) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", role: "tab", "aria-selected": days === n, className: "dtbx-tab", onClick: () => setDays(n), children: t("range.days").replace("{n}", String(n)) }, n)) }),
343
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", className: "dtbx-btn", onClick: load, disabled: loading, children: t("refresh") })
344
+ ] })
345
+ ] }),
346
+ error && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dtbx-notice", children: [
347
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: t("error") }),
348
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("button", { type: "button", className: "dtbx-btn", onClick: load, children: t("retry") })
349
+ ] }),
350
+ !report && !error && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { className: "dtbx-muted", children: t("loading") }),
351
+ report && /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
352
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dtbx-cards", children: [
353
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
354
+ Card,
355
+ {
356
+ label: t("card.startup"),
357
+ value: `${compact(report.startup.total || report.current.cost)} tok`,
358
+ hint: t("card.startupHint").replace("{tok}", compact(report.startup.parts.filter((p) => p.kind === "tools").reduce((n, p) => n + p.tokens, 0)))
359
+ }
360
+ ),
361
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
362
+ Card,
363
+ {
364
+ warn: report.current.unusedCost >= 2e3,
365
+ label: t("card.unused"),
366
+ value: `${compact(report.current.unusedCost)} tok`,
367
+ hint: t("card.unusedHint").replace("{n}", fmt(report.current.unused)).replace("{days}", String(report.days))
368
+ }
369
+ ),
370
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)(
371
+ Card,
372
+ {
373
+ warn: totals.calls > 0 && totals.errors / totals.calls >= 0.05,
374
+ label: t("card.errors"),
375
+ value: totals.calls ? `${(totals.errors / totals.calls * 100).toFixed(1)}%` : "\u2013",
376
+ hint: t("card.callsHint").replace("{n}", fmt(totals.calls)).replace("{s}", fmt(report.sessions))
377
+ }
378
+ )
379
+ ] }),
380
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("section", { className: "dtbx-panel", children: [
381
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h2", { className: "dtbx-h2", children: t("tips.title") }),
382
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("ul", { className: "dtbx-tips", children: tips.map((tip, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("li", { className: `dtbx-tip dtbx-tip-${tip.level}`, children: [
383
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-tip-dot" }),
384
+ tip.text
385
+ ] }, i)) }),
386
+ tips.some((x) => x.level === "warn") && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-note", children: t("tips.how") })
387
+ ] }),
388
+ report.startup.total > 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Startup, { t, startup: report.startup }),
389
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("section", { className: "dtbx-panel", children: [
390
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dtbx-startup-head", children: [
391
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h2", { className: "dtbx-h2", children: t("sources.title") }),
392
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("label", { className: "dtbx-check", children: [
393
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("input", { type: "checkbox", checked: showRemoved, onChange: (e) => setShowRemoved(e.target.checked) }),
394
+ t("filter.removed")
395
+ ] })
396
+ ] }),
397
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dtbx-sources", children: sources.filter((s) => showRemoved || s.tools > 0).map((s) => {
398
+ const open = openSource === s.group;
399
+ const rows = s.rows.filter((r) => showRemoved || r.current);
400
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dtbx-source", children: [
401
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("button", { type: "button", className: "dtbx-source-head", "aria-expanded": open, onClick: () => setOpenSource(open ? null : s.group), children: [
402
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: open ? "dtbx-chev dtbx-chev-open" : "dtbx-chev", children: "\u203A" }),
403
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-group-name", children: s.label }),
404
+ s.kind !== "built-in" && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-badge", children: s.kind === "mcp" ? "MCP" : t("badge.plugin") }),
405
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: `dtbx-badge dtbx-verdict-${s.verdict}`, children: t(`verdict.${s.verdict}`) }),
406
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: "dtbx-group-meta", children: [
407
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: t(s.tools === 1 ? "source.tool" : "source.tools").replace("{n}", fmt(s.tools)) }),
408
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: t(s.calls === 1 ? "source.call" : "source.calls").replace("{n}", fmt(s.calls)) }),
409
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-strong", children: t("source.cost").replace("{tok}", compact(s.cost)) })
410
+ ] })
411
+ ] }),
412
+ open && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dtbx-table-wrap", children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("table", { className: "dtbx-table", children: [
413
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("thead", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("tr", { children: [
414
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("th", { children: t("col.tool") }),
415
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("th", { children: t("col.calls") }),
416
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("th", { children: t("col.errors") }),
417
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("th", { className: "dtbx-hide-narrow", children: t("col.time") }),
418
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("th", { className: "dtbx-hide-narrow", children: t("col.result") }),
419
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("th", { children: t("col.cost") }),
420
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("th", { children: t("col.last") })
421
+ ] }) }),
422
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("tbody", { children: [
423
+ rows.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("tr", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("td", { colSpan: 7, className: "dtbx-muted", children: t("empty") }) }),
424
+ rows.map((r) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(Row, { r, t, max: totals.maxCalls, open: openTool === r.name, onToggle: () => setOpenTool(openTool === r.name ? null : r.name) }, r.name))
425
+ ] })
426
+ ] }) })
427
+ ] }, s.group);
428
+ }) })
429
+ ] }),
430
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("p", { className: "dtbx-note", children: t("how") })
431
+ ] })
432
+ ] }) });
433
+ }
434
+ function Startup({ t, startup }) {
435
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("section", { className: "dtbx-startup", children: [
436
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dtbx-startup-head", children: [
437
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("h2", { className: "dtbx-h2", children: t("startup.title") }),
438
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-muted", children: t("startup.total").replace("{tok}", fmt(startup.total)) })
439
+ ] }),
440
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dtbx-stack", role: "img", "aria-label": t("startup.title"), children: startup.parts.map((p, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { title: `${p.label}: ${fmt(p.tokens)} tok`, style: { width: `${p.tokens / startup.total * 100}%`, background: partColor(p.label, i) } }, i)) }),
441
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dtbx-legend", children: startup.parts.map((p, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dtbx-legend-row", children: [
442
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-dot", style: { background: partColor(p.label, i) } }),
443
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-legend-label", title: p.label, children: p.label }),
444
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: "dtbx-legend-num", children: [
445
+ compact(p.tokens),
446
+ " \xB7 ",
447
+ Math.round(p.tokens / startup.total * 100),
448
+ "%"
449
+ ] })
450
+ ] }, i)) }),
451
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-note", children: t("startup.hint") })
452
+ ] });
453
+ }
454
+ function Card({ label, value, hint, warn }) {
455
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: warn ? "dtbx-card dtbx-card-warn" : "dtbx-card", children: [
456
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-card-label", children: label }),
457
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-card-value", children: value }),
458
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-card-hint", children: hint })
459
+ ] });
460
+ }
461
+ function Row({ r, t, max, open, onToggle }) {
462
+ const width = r.calls ? Math.max(3, Math.sqrt(r.calls / max) * 100) : 0;
463
+ return /* @__PURE__ */ (0, import_jsx_runtime.jsxs)(import_jsx_runtime.Fragment, { children: [
464
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("tr", { className: "dtbx-row", onClick: onToggle, "aria-expanded": open, children: [
465
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("td", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: "dtbx-name", title: r.name, children: [
466
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-name-text", children: shortName(r) }),
467
+ !r.current && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-badge", children: t("badge.removed") }),
468
+ r.current && r.calls === 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-badge dtbx-badge-unused", children: t("badge.unused") }),
469
+ failsOften(r) && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-badge dtbx-badge-err", children: t("badge.errors") })
470
+ ] }) }),
471
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("td", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("span", { className: "dtbx-calls", children: [
472
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-track", children: /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { className: "dtbx-fillbar", style: { width: `${width}%`, display: "block" } }) }),
473
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: fmt(r.calls) })
474
+ ] }) }),
475
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("td", { className: r.errors ? "dtbx-err-num" : "dtbx-dim", children: r.errors ? fmt(r.errors) : "\u2013" }),
476
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("td", { className: "dtbx-hide-narrow dtbx-dim", children: secs(r.avgMs) }),
477
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("td", { className: "dtbx-hide-narrow dtbx-dim", children: r.avgResultTok === null ? "\u2013" : `${compact(r.avgResultTok)} tok` }),
478
+ /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("td", { children: [
479
+ fmt(r.cost),
480
+ " tok"
481
+ ] }),
482
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("td", { className: "dtbx-dim", children: ago(r.lastUsed, t) })
483
+ ] }),
484
+ open && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("tr", { children: /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("td", { colSpan: 7, className: "dtbx-errors", children: [
485
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dtbx-errors-title", children: t("errors.title") }),
486
+ r.recentErrors.length === 0 && /* @__PURE__ */ (0, import_jsx_runtime.jsx)("div", { className: "dtbx-muted", children: t("errors.none") }),
487
+ r.recentErrors.slice(0, 8).map((e, i) => /* @__PURE__ */ (0, import_jsx_runtime.jsxs)("div", { className: "dtbx-error", children: [
488
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("time", { children: new Date(e.time).toLocaleString(void 0, { month: "short", day: "numeric", hour: "2-digit", minute: "2-digit" }) }),
489
+ /* @__PURE__ */ (0, import_jsx_runtime.jsx)("span", { children: e.text })
490
+ ] }, i))
491
+ ] }) })
492
+ ] });
493
+ }
494
+ function apply(ctx) {
495
+ ensureStyles();
496
+ const Page = () => /* @__PURE__ */ (0, import_jsx_runtime.jsx)(ToolboxPage, { ctx });
497
+ const t = ctx.locale.bind("toolbox");
498
+ ctx.effect(() => ctx.locale.register("toolbox", { zh, en }), "deep-toolbox: dictionaries");
499
+ ctx.effect(
500
+ () => ctx.slots.inject("plugins.tab", () => ctx.slots.register({ name: "plugins.tab", id: TAB_ID, order: 50, label: () => t("nav"), locale: "toolbox" }, Page)),
501
+ "deep-toolbox: plugins tab"
502
+ );
503
+ }
504
+
505
+ return module.exports;
506
+ }});