@kadaliao/dsh-bots 0.6.1 → 0.7.0

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/usage.mjs ADDED
@@ -0,0 +1,267 @@
1
+ /**
2
+ * The usage ledger: one row per model call a bot made (its conversation turns,
3
+ * the Harness compacting its conversation, and the plugin's own memory work),
4
+ * and one row per finished task. A task is everything a bot does from one
5
+ * request (a message from the user or a chat app, a scheduled run, a DM from
6
+ * another bot) until it is idle again.
7
+ *
8
+ * Files under the bots cache:
9
+ * - `usage.jsonl`: `{t, b, s, k?, r, p, m, i, o, cr, cw}` — time, bot, session,
10
+ * task, kind ('chat' | 'compaction' | 'memory' | 'dream'), provider, model,
11
+ * uncached input, output, cache-read and cache-write tokens.
12
+ * - `tasks.jsonl`: `{id, b, s, trigger, via?, from?, sched?, title, start, end, status, topic?}`.
13
+ *
14
+ * Rows older than {@link KEEP_DAYS} are dropped when the file is compacted.
15
+ *
16
+ * @module @kadaliao/dsh-bots/lib/usage
17
+ */
18
+
19
+ import { appendFileSync, mkdirSync, readFileSync, statSync, writeFileSync } from 'node:fs';
20
+ import { join } from 'node:path';
21
+ import { costOf } from './prices.mjs';
22
+
23
+ export const KEEP_DAYS = 400;
24
+ const DAY = 86_400_000;
25
+ const HOUR = 3_600_000;
26
+ /** Compact once the file holds this many more rows than the kept window. */
27
+ const COMPACT_SLACK = 5000;
28
+
29
+ const int = (v) => (Number.isFinite(Number(v)) && Number(v) > 0 ? Math.round(Number(v)) : 0);
30
+
31
+ /** A ledger row from a Harness TokenUsage. */
32
+ export function usageRow({ at, botId, sessionId, taskId, kind = 'chat', provider, model, usage }) {
33
+ return {
34
+ t: Number(at) || Date.now(),
35
+ b: botId,
36
+ s: sessionId ?? '',
37
+ ...(taskId ? { k: taskId } : {}),
38
+ r: kind,
39
+ p: String(provider ?? ''),
40
+ m: String(model ?? ''),
41
+ i: int(usage?.inputTokens),
42
+ o: int(usage?.outputTokens),
43
+ cr: int(usage?.cacheReadTokens),
44
+ cw: int(usage?.cacheWriteTokens),
45
+ };
46
+ }
47
+
48
+ /** Prompt size of one call: everything the model read. */
49
+ export const promptTokens = (u) => int(u?.inputTokens) + int(u?.cacheReadTokens) + int(u?.cacheWriteTokens);
50
+ const rowTokens = (r) => r.i + r.o + r.cr + r.cw;
51
+
52
+ function readJsonl(file) {
53
+ const out = [];
54
+ try {
55
+ for (const line of readFileSync(file, 'utf8').split('\n')) {
56
+ if (!line) continue;
57
+ try {
58
+ out.push(JSON.parse(line));
59
+ } catch {
60
+ // A torn line.
61
+ }
62
+ }
63
+ } catch {
64
+ // No file yet.
65
+ }
66
+ return out;
67
+ }
68
+
69
+ /** One append-only JSONL file with an in-memory copy, re-read when another process wrote it. */
70
+ function jsonlFile(file, keep) {
71
+ let rows = null;
72
+ let seen = -1;
73
+ const stamp = () => {
74
+ try {
75
+ const s = statSync(file);
76
+ return s.mtimeMs * 1e6 + s.size;
77
+ } catch {
78
+ return -1;
79
+ }
80
+ };
81
+ return {
82
+ all() {
83
+ const now = stamp();
84
+ if (rows === null || now !== seen) {
85
+ rows = readJsonl(file);
86
+ seen = now;
87
+ }
88
+ return rows;
89
+ },
90
+ append(row) {
91
+ const all = this.all();
92
+ all.push(row);
93
+ try {
94
+ mkdirSync(join(file, '..'), { recursive: true });
95
+ const cutoff = Date.now() - KEEP_DAYS * DAY;
96
+ const old = all.findIndex((r) => (r.t ?? r.end ?? 0) >= cutoff);
97
+ if (old > COMPACT_SLACK || (keep && all.length > keep + COMPACT_SLACK)) {
98
+ rows = all.slice(Math.max(old, keep ? all.length - keep : 0));
99
+ writeFileSync(file, rows.map((r) => JSON.stringify(r)).join('\n') + '\n', 'utf8');
100
+ } else appendFileSync(file, JSON.stringify(row) + '\n', 'utf8');
101
+ seen = stamp();
102
+ } catch {
103
+ // Usage is a convenience; a write failure loses one row.
104
+ }
105
+ },
106
+ };
107
+ }
108
+
109
+ /** The ledger under `dir`. */
110
+ export function createLedger(dir) {
111
+ const calls = jsonlFile(join(dir, 'usage.jsonl'));
112
+ const tasks = jsonlFile(join(dir, 'tasks.jsonl'), 50_000);
113
+ return {
114
+ record(row) {
115
+ calls.append(row);
116
+ return row;
117
+ },
118
+ finishTask(task) {
119
+ tasks.append(task);
120
+ return task;
121
+ },
122
+ calls: () => calls.all(),
123
+ tasks: () => tasks.all(),
124
+ };
125
+ }
126
+
127
+ /** The [from, to) window and bucket of a named range, in the viewer's zone (`tz` = minutes east of UTC). */
128
+ export function rangeOf(range, tz = 0, now = Date.now()) {
129
+ const off = tz * 60_000;
130
+ const dayStart = Math.floor((now + off) / DAY) * DAY - off;
131
+ switch (range) {
132
+ case 'today':
133
+ return { from: dayStart, to: dayStart + DAY, bucket: HOUR };
134
+ case '30d':
135
+ return { from: dayStart - 29 * DAY, to: dayStart + DAY, bucket: DAY };
136
+ case '90d':
137
+ return { from: dayStart - 89 * DAY, to: dayStart + DAY, bucket: DAY };
138
+ case 'all':
139
+ return { from: 0, to: dayStart + DAY, bucket: DAY };
140
+ case '7d':
141
+ default:
142
+ return { from: dayStart - 6 * DAY, to: dayStart + DAY, bucket: DAY };
143
+ }
144
+ }
145
+
146
+ const addCost = (acc, c) => {
147
+ if (c) acc[c.currency] = (acc[c.currency] ?? 0) + c.amount;
148
+ };
149
+ const blank = () => ({ tokens: 0, input: 0, output: 0, cacheRead: 0, cacheWrite: 0, calls: 0, cost: {}, unpriced: 0 });
150
+ function add(acc, r, cost) {
151
+ acc.tokens += rowTokens(r);
152
+ acc.input += r.i;
153
+ acc.output += r.o;
154
+ acc.cacheRead += r.cr;
155
+ acc.cacheWrite += r.cw;
156
+ acc.calls += 1;
157
+ if (cost) addCost(acc.cost, cost);
158
+ else acc.unpriced += rowTokens(r);
159
+ return acc;
160
+ }
161
+ const round = (cost) => Object.fromEntries(Object.entries(cost).map(([k, v]) => [k, Number(v.toFixed(6))]));
162
+
163
+ /**
164
+ * A usage report for the page.
165
+ * @param {{calls: object[], tasks: object[], running?: object[]}} data
166
+ * @param {{range?: string, bot?: string, tz?: number, now?: number, price: (model: string) => object|null, schedules?: Map<string,string>}} opts
167
+ */
168
+ export function report(data, opts) {
169
+ const now = opts.now ?? Date.now();
170
+ const bot = opts.bot || '';
171
+ const { from, to, bucket: named } = rangeOf(opts.range ?? '7d', opts.tz ?? 0, now);
172
+ // 'all' starts at the first row and turns weekly past half a year.
173
+ const earliest = from === 0 ? data.calls.reduce((m, r) => ((!bot || r.b === bot) && r.t < m ? r.t : m), to - named) : from;
174
+ const bucket = from === 0 && to - earliest > 180 * DAY ? 7 * DAY : named;
175
+ /** Buckets are aligned to the end of the range, which is the viewer's midnight. */
176
+ const keyOf = (t) => to - Math.ceil((to - t) / bucket) * bucket;
177
+ const priceCache = new Map();
178
+ const price = (m) => {
179
+ if (!priceCache.has(m)) priceCache.set(m, opts.price(m));
180
+ return priceCache.get(m);
181
+ };
182
+ const inBot = (r) => !bot || r.b === bot;
183
+ const calls = data.calls.filter((r) => inBot(r));
184
+ const span = to - from;
185
+ const totals = blank();
186
+ const prev = blank();
187
+ const byBot = new Map();
188
+ const byModel = new Map();
189
+ const byKind = new Map();
190
+ const byTask = new Map();
191
+ const series = new Map();
192
+ for (const r of calls) {
193
+ if (r.t >= from - span && r.t < from) add(prev, r, costOf(price(r.m), r));
194
+ if (r.t < from || r.t >= to) continue;
195
+ const cost = costOf(price(r.m), r);
196
+ add(totals, r, cost);
197
+ add(byBot.get(r.b) ?? byBot.set(r.b, blank()).get(r.b), r, cost);
198
+ const mk = `${r.p}\n${r.m}`;
199
+ add(byModel.get(mk) ?? byModel.set(mk, { ...blank(), provider: r.p, model: r.m }).get(mk), r, cost);
200
+ add(byKind.get(r.r) ?? byKind.set(r.r, blank()).get(r.r), r, cost);
201
+ if (r.k) add(byTask.get(r.k) ?? byTask.set(r.k, blank()).get(r.k), r, cost);
202
+ const b = keyOf(r.t);
203
+ const s = series.get(b) ?? series.set(b, { t: b, tokens: 0, byBot: {}, cost: {} }).get(b);
204
+ s.tokens += rowTokens(r);
205
+ s.byBot[r.b] = (s.byBot[r.b] ?? 0) + rowTokens(r);
206
+ addCost(s.cost, cost);
207
+ }
208
+ const start = keyOf(Math.max(from, earliest));
209
+ const points = [];
210
+ for (let b = start; b < to; b += bucket) {
211
+ const s = series.get(b);
212
+ points.push(s ? { ...s, cost: round(s.cost) } : { t: b, tokens: 0, byBot: {}, cost: {} });
213
+ }
214
+ const tasks = data.tasks.filter((t) => inBot(t) && (t.end ?? t.start) >= from && (t.end ?? t.start) < to);
215
+ const running = (data.running ?? []).filter(inBot);
216
+ const taskView = (t) => {
217
+ const u = byTask.get(t.id) ?? blank();
218
+ return {
219
+ id: t.id,
220
+ bot: t.b,
221
+ trigger: t.trigger,
222
+ ...(t.via ? { via: t.via } : {}),
223
+ ...(t.from ? { from: t.from } : {}),
224
+ ...(t.sched ? { sched: t.sched } : {}),
225
+ title: t.title,
226
+ start: t.start,
227
+ end: t.end ?? null,
228
+ status: t.status ?? (t.end ? 'done' : 'running'),
229
+ tokens: u.tokens,
230
+ calls: u.calls,
231
+ cost: round(u.cost),
232
+ };
233
+ };
234
+ const finished = tasks.map(taskView);
235
+ const live = running.map(taskView);
236
+ const taskCount = new Map();
237
+ for (const t of tasks) taskCount.set(t.b, (taskCount.get(t.b) ?? 0) + 1);
238
+ const sched = new Map();
239
+ for (const t of finished.filter((x) => x.sched)) {
240
+ const s = sched.get(t.sched) ?? sched.set(t.sched, { id: t.sched, title: opts.schedules?.get(t.sched) ?? t.title, bot: t.bot, runs: 0, tokens: 0, cost: {} }).get(t.sched);
241
+ s.runs += 1;
242
+ s.tokens += t.tokens;
243
+ for (const [c, v] of Object.entries(t.cost)) s.cost[c] = (s.cost[c] ?? 0) + v;
244
+ }
245
+ const fin = (o) => ({ ...o, cost: round(o.cost) });
246
+ const sortTokens = (a, b) => b.tokens - a.tokens;
247
+ return {
248
+ range: { name: opts.range ?? '7d', from: start, to, bucket },
249
+ totals: { ...fin(totals), tasks: tasks.length, cacheRate: totals.input + totals.cacheRead > 0 ? totals.cacheRead / (totals.input + totals.cacheRead + totals.cacheWrite) : 0 },
250
+ prev: { ...fin(prev), tasks: data.tasks.filter((t) => inBot(t) && (t.end ?? t.start) >= from - span && (t.end ?? t.start) < from).length },
251
+ series: points,
252
+ bots: [...byBot].map(([id, v]) => ({ bot: id, ...fin(v), tasks: taskCount.get(id) ?? 0 })).sort(sortTokens),
253
+ models: [...byModel.values()].map((v) => ({ ...fin(v), priced: Boolean(price(v.model)), priceSource: price(v.model)?.source ?? null })).sort(sortTokens),
254
+ kinds: [...byKind].map(([kind, v]) => ({ kind, ...fin(v) })).sort(sortTokens),
255
+ schedules: [...sched.values()].map((s) => ({ ...s, cost: round(s.cost) })).sort(sortTokens),
256
+ tasks: [...live, ...finished.slice().sort((a, b) => (b.end ?? b.start) - (a.end ?? a.start))].slice(0, 60),
257
+ topTasks: finished.slice().sort(sortTokens).slice(0, 10),
258
+ };
259
+ }
260
+
261
+ /** Today's tokens per bot, for the roster. */
262
+ export function todayByBot(calls, tz = 0, now = Date.now()) {
263
+ const { from, to } = rangeOf('today', tz, now);
264
+ const out = {};
265
+ for (const r of calls) if (r.t >= from && r.t < to) out[r.b] = (out[r.b] ?? 0) + rowTokens(r);
266
+ return out;
267
+ }
package/locale/en.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "Bots",
4
- "description": "Grok-style bots: each with its own avatar, persona, skills, apps and scheduled tasks. Bots message each other, and a dispatcher bot hands out the work."
4
+ "description": "Grok-style bots: each with its own avatar, persona, model, skills, apps, scheduled tasks and memory. Bots message each other, a dispatcher hands out the work, and a usage view counts tokens by bot, task and day."
5
5
  }
6
6
  }
package/locale/zh.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "meta": {
3
3
  "title": "Bots",
4
- "description": "Grok 风格的 bot:每个 bot 有自己的头像、人设、技能、应用和定时任务;bot 之间私信协作,主 bot 负责分派与调度。"
4
+ "description": "Grok 风格的 bot:每个 bot 有自己的头像、人设、模型、技能、应用、定时任务和记忆;bot 之间私信协作,主 bot 负责分派与调度,用量页按 bot、任务、日期统计 tokens。"
5
5
  }
6
6
  }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "@kadaliao/dsh-bots",
3
- "version": "0.6.1",
3
+ "version": "0.7.0",
4
4
  "type": "module",
5
- "description": "Grok-style bots for DeepSeek Harness: create bots with their own avatar, persona, skills, apps and scheduled tasks; they message each other and a dispatcher bot hands out the work.",
5
+ "description": "Grok-style bots for DeepSeek Harness: create bots with their own avatar, persona, model, skills, apps, scheduled tasks and memory; they message each other, a dispatcher bot hands out the work, and a usage view counts tokens by bot, task and day.",
6
6
  "exports": {
7
7
  ".": "./index.js",
8
8
  "./client": "./client.js",
@@ -49,5 +49,5 @@
49
49
  },
50
50
  "homepage": "https://github.com/kadaliao/dsh-plugins/tree/main/plugins/bots",
51
51
  "license": "MIT",
52
- "keywords": ["dsh", "deepseek-harness", "dsh-plugin", "bots", "agents", "multi-agent", "persona", "schedule", "mcp", "computer-use"]
52
+ "keywords": ["dsh", "deepseek-harness", "dsh-plugin", "bots", "agents", "multi-agent", "persona", "schedule", "mcp", "computer-use", "memory", "usage"]
53
53
  }