@anytowork/cli 0.12.65 → 0.12.66
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/package.json +1 -1
- package/src/mt/recipes.js +327 -0
- package/src/mt/sample.js +59 -8
- package/src/mt/text.js +3 -0
package/package.json
CHANGED
|
@@ -0,0 +1,327 @@
|
|
|
1
|
+
// v0.38.69 (CLI 0.12.66): a recipe-driven log reader.
|
|
2
|
+
//
|
|
3
|
+
// Why: every Agent used to need its own hand-written reader in readers.js. A new Agent now only needs a recipe, which is
|
|
4
|
+
// data: where its logs are, which file name to take, which lines carry a model call, where the numbers sit, and which
|
|
5
|
+
// trailing model tags do not change the price. The five hand-written readers stay untouched (they carry per-log
|
|
6
|
+
// precision this layer does not try to copy); everything here is additive.
|
|
7
|
+
//
|
|
8
|
+
// What a recipe can never do: supply totals. The numbers are always summed by the CLI from the raw files; a recipe only
|
|
9
|
+
// says where to look and how to read a line. A line that matches but is broken (a missing required field, a token count
|
|
10
|
+
// that is not a number or is negative) is refused with the recipe name, the file and the line number, never counted as 0.
|
|
11
|
+
//
|
|
12
|
+
// A recipe:
|
|
13
|
+
// id internal name; shown in the usage sample diagnostics only
|
|
14
|
+
// client the program name shown in the client line (null when the logs do not name the program)
|
|
15
|
+
// roots default folders, relative to the home folder (an absolute folder is allowed)
|
|
16
|
+
// glob file name to read inside a root ("transcript_full.jsonl", or "*.jsonl")
|
|
17
|
+
// excludeDirs folder names never walked into (shards / caches) so the same call is not counted twice
|
|
18
|
+
// lineFilter {field: value}: a line counts only when every pair matches (nested fields use dotted paths)
|
|
19
|
+
// fields where each value sits (dotted paths): model / input / output / cacheRead / cacheWrite / time
|
|
20
|
+
// dedupeKey field naming one call inside one file; a step written again keeps the last line
|
|
21
|
+
// required fields a matching line must carry (a missing value is refused, never counted as 0)
|
|
22
|
+
// modelSuffixes trailing tags that do not change the price (thinking effort); stripped from the end, repeatedly
|
|
23
|
+
// sessionFrom "dir" = the first folder under the root is the conversation id
|
|
24
|
+
//
|
|
25
|
+
// Adding an Agent: append one object to RECIPES. No other file changes.
|
|
26
|
+
|
|
27
|
+
import fs from "node:fs";
|
|
28
|
+
import os from "node:os";
|
|
29
|
+
import path from "node:path";
|
|
30
|
+
import { guessProvider } from "./readers.js";
|
|
31
|
+
|
|
32
|
+
const MAX_DEPTH = 6;
|
|
33
|
+
|
|
34
|
+
export const RECIPES = [
|
|
35
|
+
{
|
|
36
|
+
id: "antigravity",
|
|
37
|
+
client: "Antigravity",
|
|
38
|
+
roots: [".gemini/antigravity/brain", ".gemini/antigravity-cli/brain"],
|
|
39
|
+
glob: "transcript_full.jsonl",
|
|
40
|
+
excludeDirs: ["chunks"],
|
|
41
|
+
lineFilter: { source: "MODEL", type: "PLANNER_RESPONSE" },
|
|
42
|
+
fields: {
|
|
43
|
+
model: "model",
|
|
44
|
+
input: "input_tokens",
|
|
45
|
+
output: "output_tokens",
|
|
46
|
+
cacheRead: "cache_read_tokens",
|
|
47
|
+
time: "created_at",
|
|
48
|
+
},
|
|
49
|
+
dedupeKey: "step_index",
|
|
50
|
+
required: ["model", "time", "input", "output"],
|
|
51
|
+
modelSuffixes: ["-low", "-medium", "-high", "-xhigh", "-sonar", "-tiered"],
|
|
52
|
+
sessionFrom: "dir",
|
|
53
|
+
},
|
|
54
|
+
];
|
|
55
|
+
|
|
56
|
+
export function recipeById(id) {
|
|
57
|
+
return RECIPES.find((r) => r.id === id) || null;
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
/** The folders of a recipe: relative entries become paths under the home folder; absolute entries stay as they are. */
|
|
61
|
+
export function expandRoots(recipe, home) {
|
|
62
|
+
return (recipe.roots || []).map((r) => (path.isAbsolute(r) ? r : path.join(home, r)));
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function isDir(p) {
|
|
66
|
+
try { return fs.statSync(p).isDirectory(); } catch { return false; }
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
function isFile(p) {
|
|
70
|
+
try { return fs.statSync(p).isFile(); } catch { return false; }
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** The existing default folders of a recipe. */
|
|
74
|
+
export function recipeRoots(recipe, home) {
|
|
75
|
+
return expandRoots(recipe, home).filter(isDir);
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/** A file name pattern: "*" is any run of characters, everything else is literal. */
|
|
79
|
+
export function nameMatches(pattern, name) {
|
|
80
|
+
if (!pattern || pattern === "*") return true;
|
|
81
|
+
if (!pattern.includes("*")) return name === pattern;
|
|
82
|
+
const re = new RegExp("^" + pattern.split("*").map((s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join(".*") + "$");
|
|
83
|
+
return re.test(name);
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function fieldOf(obj, dotted) {
|
|
87
|
+
let v = obj;
|
|
88
|
+
for (const k of String(dotted).split(".")) {
|
|
89
|
+
if (v == null || typeof v !== "object") return undefined;
|
|
90
|
+
v = v[k];
|
|
91
|
+
}
|
|
92
|
+
return v;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
function isEmpty(v) {
|
|
96
|
+
return v === undefined || v === null || (typeof v === "string" && v.trim() === "");
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
function toMs(v) {
|
|
100
|
+
if (v == null || v === "") return null;
|
|
101
|
+
const ms = typeof v === "number" ? (v < 1e12 ? v * 1000 : v) : Date.parse(String(v));
|
|
102
|
+
return Number.isFinite(ms) ? ms : null;
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/** A recipe or a log line that cannot be used: names the recipe and, for a line, the file and the line number. */
|
|
106
|
+
export class RecipeError extends Error {
|
|
107
|
+
constructor(info) {
|
|
108
|
+
super("recipe " + info.recipe + (info.file ? " (" + info.file + " line " + info.line + ")" : "") + ": " + info.message);
|
|
109
|
+
this.name = "RecipeError";
|
|
110
|
+
this.code = "recipe_bad_line";
|
|
111
|
+
this.info = info;
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function bad(recipe, file, line, message) {
|
|
116
|
+
throw new RecipeError({ recipe: recipe.id, file, line, message });
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/** A model name without its thinking-effort tags ("gemini-4-carbon-medium" -> "gemini-4-carbon"). */
|
|
120
|
+
export function normalizeModel(model, suffixes = []) {
|
|
121
|
+
let m = String(model == null ? "" : model).trim();
|
|
122
|
+
const list = [...suffixes].sort((a, b) => b.length - a.length);
|
|
123
|
+
let again = true;
|
|
124
|
+
while (again) {
|
|
125
|
+
again = false;
|
|
126
|
+
const low = m.toLowerCase();
|
|
127
|
+
for (const s of list) {
|
|
128
|
+
if (low.length > s.length && low.endsWith(s.toLowerCase())) {
|
|
129
|
+
m = m.slice(0, m.length - s.length);
|
|
130
|
+
again = true;
|
|
131
|
+
break;
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
return m;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
function checkRecipe(recipe) {
|
|
139
|
+
if (!recipe || !recipe.id) throw new RecipeError({ recipe: "?", file: null, line: 0, message: "recipe has no id" });
|
|
140
|
+
for (const k of ["model", "time", "input", "output"]) {
|
|
141
|
+
if (!recipe.fields || !recipe.fields[k]) bad(recipe, null, 0, "recipe is missing fields." + k);
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
function matchesFilter(recipe, obj) {
|
|
146
|
+
const f = recipe.lineFilter || {};
|
|
147
|
+
for (const [k, v] of Object.entries(f)) {
|
|
148
|
+
if (String(fieldOf(obj, k)) !== String(v)) return false;
|
|
149
|
+
}
|
|
150
|
+
return true;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function countAt(recipe, obj, key, file, line) {
|
|
154
|
+
const dotted = recipe.fields[key];
|
|
155
|
+
if (!dotted) return null;
|
|
156
|
+
const v = fieldOf(obj, dotted);
|
|
157
|
+
const required = (recipe.required || []).includes(key);
|
|
158
|
+
if (isEmpty(v)) {
|
|
159
|
+
if (required) bad(recipe, file, line, "missing " + key + " (" + dotted + ")");
|
|
160
|
+
return null;
|
|
161
|
+
}
|
|
162
|
+
const n = typeof v === "number" ? v : Number(String(v).trim());
|
|
163
|
+
if (!Number.isFinite(n)) bad(recipe, file, line, key + " (" + dotted + ") is not a number: " + JSON.stringify(v));
|
|
164
|
+
if (n < 0) bad(recipe, file, line, key + " (" + dotted + ") is negative: " + n);
|
|
165
|
+
return Math.floor(n);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
function sessionOf(recipe, file, root) {
|
|
169
|
+
if (recipe.sessionFrom !== "dir") return null;
|
|
170
|
+
if (file === root) return null;
|
|
171
|
+
const rel = path.relative(root, file);
|
|
172
|
+
if (!rel || rel.startsWith("..")) return null;
|
|
173
|
+
const first = rel.split(path.sep)[0];
|
|
174
|
+
return first && first !== path.basename(file) ? first : null;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
function rowOf(recipe, obj, entry, lineNo) {
|
|
178
|
+
const modelRaw = String(fieldOf(obj, recipe.fields.model) ?? "").trim();
|
|
179
|
+
if (!modelRaw) bad(recipe, entry.file, lineNo, "missing model (" + recipe.fields.model + ")");
|
|
180
|
+
const model = normalizeModel(modelRaw, recipe.modelSuffixes);
|
|
181
|
+
const atRaw = fieldOf(obj, recipe.fields.time);
|
|
182
|
+
const atMs = toMs(atRaw);
|
|
183
|
+
if (atMs == null) bad(recipe, entry.file, lineNo, "bad time (" + recipe.fields.time + "): " + JSON.stringify(atRaw));
|
|
184
|
+
const input = countAt(recipe, obj, "input", entry.file, lineNo);
|
|
185
|
+
const output = countAt(recipe, obj, "output", entry.file, lineNo);
|
|
186
|
+
const cacheRead = countAt(recipe, obj, "cacheRead", entry.file, lineNo);
|
|
187
|
+
const cacheWrite = countAt(recipe, obj, "cacheWrite", entry.file, lineNo);
|
|
188
|
+
const step = recipe.dedupeKey ? fieldOf(obj, recipe.dedupeKey) : undefined;
|
|
189
|
+
const id = isEmpty(step) ? path.basename(entry.file) + ":" + lineNo : String(step);
|
|
190
|
+
return {
|
|
191
|
+
call_id: (entry.session || path.basename(entry.file)) + ":" + id,
|
|
192
|
+
conversation_id: entry.session || null,
|
|
193
|
+
provider: guessProvider(model),
|
|
194
|
+
model,
|
|
195
|
+
started_at: new Date(atMs).toISOString().replace(/\.\d{3}Z$/, "Z"),
|
|
196
|
+
ended_at: null,
|
|
197
|
+
input_tokens: input,
|
|
198
|
+
output_tokens: output,
|
|
199
|
+
cache_write_tokens: cacheWrite,
|
|
200
|
+
cache_read_tokens: cacheRead,
|
|
201
|
+
cwd: null,
|
|
202
|
+
seenMs: atMs,
|
|
203
|
+
atMs,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function walk(dir, root, depth, out, recipe, sinceMs) {
|
|
208
|
+
let entries;
|
|
209
|
+
try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; }
|
|
210
|
+
for (const e of entries) {
|
|
211
|
+
const p = path.join(dir, e.name);
|
|
212
|
+
if (e.isDirectory()) {
|
|
213
|
+
if (depth >= MAX_DEPTH) continue;
|
|
214
|
+
if ((recipe.excludeDirs || []).includes(e.name)) continue;
|
|
215
|
+
walk(p, root, depth + 1, out, recipe, sinceMs);
|
|
216
|
+
continue;
|
|
217
|
+
}
|
|
218
|
+
if (!e.isFile() || !nameMatches(recipe.glob, e.name)) continue;
|
|
219
|
+
try { if (sinceMs && fs.statSync(p).mtimeMs < sinceMs) continue; } catch { continue; }
|
|
220
|
+
out.push({ file: p, root, session: sessionOf(recipe, p, root) });
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Read one recipe. `roots` overrides the recipe folders (used by --from). Returns
|
|
226
|
+
* { found, calls, files, lines, models, sample, client }. Throws RecipeError when a matching line is broken.
|
|
227
|
+
*/
|
|
228
|
+
export function readRecipeCalls(recipe, { home = os.homedir(), sinceMs = 0, roots = null } = {}) {
|
|
229
|
+
checkRecipe(recipe);
|
|
230
|
+
const baseRoots = roots || expandRoots(recipe, home);
|
|
231
|
+
const files = [];
|
|
232
|
+
const seenFiles = new Set();
|
|
233
|
+
for (const root of baseRoots) {
|
|
234
|
+
if (isFile(root)) {
|
|
235
|
+
if (nameMatches(recipe.glob, path.basename(root)) && !seenFiles.has(root)) {
|
|
236
|
+
seenFiles.add(root);
|
|
237
|
+
files.push({ file: root, root, session: null });
|
|
238
|
+
}
|
|
239
|
+
continue;
|
|
240
|
+
}
|
|
241
|
+
if (!isDir(root)) continue;
|
|
242
|
+
const found = [];
|
|
243
|
+
walk(root, root, 0, found, recipe, sinceMs);
|
|
244
|
+
for (const f of found) {
|
|
245
|
+
if (seenFiles.has(f.file)) continue;
|
|
246
|
+
seenFiles.add(f.file);
|
|
247
|
+
files.push(f);
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
const calls = [];
|
|
251
|
+
const models = new Set();
|
|
252
|
+
// a line that cannot match need not be parsed: every matching line holds the first filter value somewhere
|
|
253
|
+
const needle = String(Object.values(recipe.lineFilter || {})[0] || "");
|
|
254
|
+
let lines = 0;
|
|
255
|
+
let sample = null;
|
|
256
|
+
for (const entry of files) {
|
|
257
|
+
let text;
|
|
258
|
+
try { text = fs.readFileSync(entry.file, "utf8"); } catch { continue; }
|
|
259
|
+
const rows = [];
|
|
260
|
+
const at = new Map();
|
|
261
|
+
const list = text.split("\n");
|
|
262
|
+
for (let i = 0; i < list.length; i += 1) {
|
|
263
|
+
const raw = list[i];
|
|
264
|
+
if (!raw || raw[0] !== "{") continue;
|
|
265
|
+
if (needle && raw.indexOf(needle) === -1) continue;
|
|
266
|
+
let obj;
|
|
267
|
+
try { obj = JSON.parse(raw); } catch { continue; }
|
|
268
|
+
if (!obj || typeof obj !== "object" || !matchesFilter(recipe, obj)) continue;
|
|
269
|
+
lines += 1;
|
|
270
|
+
const row = rowOf(recipe, obj, entry, i + 1);
|
|
271
|
+
const step = recipe.dedupeKey ? fieldOf(obj, recipe.dedupeKey) : undefined;
|
|
272
|
+
const key = isEmpty(step) ? entry.file + ":" + (i + 1) : entry.file + ":" + String(step);
|
|
273
|
+
if (at.has(key)) rows[at.get(key)] = row; // a step written again: the last line wins
|
|
274
|
+
else { at.set(key, rows.length); rows.push(row); }
|
|
275
|
+
}
|
|
276
|
+
for (const row of rows) {
|
|
277
|
+
calls.push(row);
|
|
278
|
+
models.add(row.model);
|
|
279
|
+
if (!sample) sample = row;
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
return {
|
|
283
|
+
found: files.length > 0,
|
|
284
|
+
calls,
|
|
285
|
+
files: files.length,
|
|
286
|
+
lines,
|
|
287
|
+
models: [...models],
|
|
288
|
+
sample: sample ? {
|
|
289
|
+
model: sample.model, at: sample.started_at, input_tokens: sample.input_tokens,
|
|
290
|
+
output_tokens: sample.output_tokens, cache_read_tokens: sample.cache_read_tokens,
|
|
291
|
+
} : null,
|
|
292
|
+
client: recipe.client || null,
|
|
293
|
+
};
|
|
294
|
+
}
|
|
295
|
+
|
|
296
|
+
/** A --from path that belongs to a recipe, or null. Checked before the hand-written path shapes. */
|
|
297
|
+
export function recipeForPath(abs, st) {
|
|
298
|
+
if (!st) return null;
|
|
299
|
+
const as = (r) => ({ reader: "recipe", recipe: r.id, client: r.client || null, roots: [abs], root: abs });
|
|
300
|
+
if (st.isFile()) {
|
|
301
|
+
const name = path.basename(abs);
|
|
302
|
+
const r = RECIPES.find((x) => nameMatches(x.glob, name));
|
|
303
|
+
return r ? as(r) : null;
|
|
304
|
+
}
|
|
305
|
+
if (!st.isDirectory()) return null;
|
|
306
|
+
for (const r of RECIPES) {
|
|
307
|
+
for (const root of expandRoots(r, os.homedir())) {
|
|
308
|
+
if (abs === root || abs.startsWith(root + path.sep)) return as(r);
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
for (const r of RECIPES) {
|
|
312
|
+
if (hasFileBelow(abs, r, 3)) return as(r);
|
|
313
|
+
}
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
function hasFileBelow(dir, recipe, depth) {
|
|
318
|
+
let entries;
|
|
319
|
+
try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return false; }
|
|
320
|
+
for (const e of entries) {
|
|
321
|
+
if (e.isFile() && nameMatches(recipe.glob, e.name)) return true;
|
|
322
|
+
if (e.isDirectory() && depth > 0 && !(recipe.excludeDirs || []).includes(e.name)) {
|
|
323
|
+
if (hasFileBelow(path.join(dir, e.name), recipe, depth - 1)) return true;
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
return false;
|
|
327
|
+
}
|
package/src/mt/sample.js
CHANGED
|
@@ -27,6 +27,7 @@ import { pickLang } from '../model-pledge.js';
|
|
|
27
27
|
import { t } from './text.js';
|
|
28
28
|
import { listJsonl, loadSqlite } from './readers.js';
|
|
29
29
|
import { dshRoots, readDshCalls, looksLikeDsh } from './dsh.js';
|
|
30
|
+
import { RECIPES, recipeById, expandRoots, recipeForPath, readRecipeCalls } from './recipes.js';
|
|
30
31
|
|
|
31
32
|
export const KIND = 'job_usage_sample';
|
|
32
33
|
export const JOB_CHECK_PATH = '/api/v1/agent/hire/job-check';
|
|
@@ -395,13 +396,20 @@ export function defaultSources(home, env = {}) {
|
|
|
395
396
|
if (env.HERMES_HOME) sdb.push(path.join(String(env.HERMES_HOME), 'state.db'));
|
|
396
397
|
sdb.push(path.join(home, '.hermes', 'state.db'));
|
|
397
398
|
const agents = path.join(home, '.openclaw', 'agents');
|
|
398
|
-
|
|
399
|
+
const out = [
|
|
399
400
|
{ reader: 'msg_log', roots: [...new Set(msg)] },
|
|
400
401
|
{ reader: 'rollout', roots: [...new Set(roll)] },
|
|
401
402
|
{ reader: 'dsh_log', roots: dshRoots(home) },
|
|
402
403
|
{ reader: 'session_db', files: [...new Set(sdb)] },
|
|
403
404
|
{ reader: 'run_db', files: agentsDbFiles(agents), root: agents },
|
|
404
405
|
];
|
|
406
|
+
// v0.38.68: recipe sources. A recipe with no folder on this computer is left out, so a machine without that program
|
|
407
|
+
// reads exactly the same logs as before.
|
|
408
|
+
for (const recipe of RECIPES) {
|
|
409
|
+
const roots = expandRoots(recipe, home).filter(isDir);
|
|
410
|
+
if (roots.length) out.push({ reader: 'recipe', recipe: recipe.id, client: recipe.client || null, roots });
|
|
411
|
+
}
|
|
412
|
+
return out;
|
|
405
413
|
}
|
|
406
414
|
|
|
407
415
|
/** `--from <path>`: the reader picked by the shape of the path, or null when nothing there looks like a log. */
|
|
@@ -409,6 +417,9 @@ export function sourceFromPath(p) {
|
|
|
409
417
|
const abs = path.resolve(String(p));
|
|
410
418
|
let st;
|
|
411
419
|
try { st = fs.statSync(abs); } catch { return null; }
|
|
420
|
+
// v0.38.68: a recipe path wins over the hand-written shapes (a recipe folder would otherwise look like a plain jsonl folder).
|
|
421
|
+
const byRecipe = recipeForPath(abs, st);
|
|
422
|
+
if (byRecipe) return byRecipe;
|
|
412
423
|
if (st.isFile()) {
|
|
413
424
|
if (abs.endsWith('.sqlite')) return { reader: 'run_db', files: [abs], root: abs };
|
|
414
425
|
if (abs.endsWith('.db')) return { reader: 'session_db', files: [abs], root: abs };
|
|
@@ -448,7 +459,17 @@ export function sourceFromPath(p) {
|
|
|
448
459
|
return null;
|
|
449
460
|
}
|
|
450
461
|
|
|
451
|
-
export async function readSource(src, { sinceMs, sqlite }) {
|
|
462
|
+
export async function readSource(src, { sinceMs, sqlite, home, env } = {}) {
|
|
463
|
+
if (src.reader === 'recipe') {
|
|
464
|
+
const recipe = recipeById(src.recipe);
|
|
465
|
+
const r = readRecipeCalls(recipe, { home, env, sinceMs, roots: src.roots });
|
|
466
|
+
return {
|
|
467
|
+
found: r.found,
|
|
468
|
+
calls: r.calls,
|
|
469
|
+
client: r.client,
|
|
470
|
+
stats: { recipe: recipe.id, files: r.files, lines: r.lines, models: r.models, sample: r.sample },
|
|
471
|
+
};
|
|
472
|
+
}
|
|
452
473
|
switch (src.reader) {
|
|
453
474
|
case 'msg_log': return readMsgLogSample({ roots: src.roots, sinceMs });
|
|
454
475
|
case 'rollout': return readRolloutSample({ roots: src.roots, sinceMs });
|
|
@@ -524,8 +545,14 @@ export function summarize(sample) {
|
|
|
524
545
|
// ---------------------------------------------------------------- the command
|
|
525
546
|
|
|
526
547
|
function shortRoot(src, home) {
|
|
548
|
+
const short = (r) => (home && r.startsWith(home) ? `~${r.slice(home.length)}` : r);
|
|
549
|
+
// a recipe can hold several folders (one program, two layouts): show every folder it found, not just the first
|
|
550
|
+
if (src.reader === 'recipe' && Array.isArray(src.roots) && src.roots.length > 1) {
|
|
551
|
+
const found = src.roots.filter(isDir).map(short);
|
|
552
|
+
if (found.length) return found.join(' + ');
|
|
553
|
+
}
|
|
527
554
|
const r = src.root || (src.roots && src.roots.find(isDir)) || (src.files && src.files.find(isFile)) || '';
|
|
528
|
-
return
|
|
555
|
+
return short(r);
|
|
529
556
|
}
|
|
530
557
|
|
|
531
558
|
function errorCodeOf(e) {
|
|
@@ -545,12 +572,18 @@ async function gatherSample(c, tz) {
|
|
|
545
572
|
let dbError = false;
|
|
546
573
|
let oldNode = false;
|
|
547
574
|
for (const src of sources) {
|
|
548
|
-
|
|
575
|
+
let r;
|
|
576
|
+
try {
|
|
577
|
+
r = await readSource(src, { sinceMs, sqlite: c.sqlite, home: c.home, env: c.env });
|
|
578
|
+
} catch (e) {
|
|
579
|
+
if (e && e.code === 'recipe_bad_line') return { bad: e.info };
|
|
580
|
+
throw e;
|
|
581
|
+
}
|
|
549
582
|
found = found || r.found;
|
|
550
583
|
dbError = dbError || !!r.dbError;
|
|
551
584
|
oldNode = oldNode || !!r.oldNode;
|
|
552
585
|
const inWin = windowCalls(r.calls, tz, nowMs);
|
|
553
|
-
if (inWin.length) got.push({ src, calls: r.calls, inWin, entries: r.entries || [] });
|
|
586
|
+
if (inWin.length) got.push({ src, calls: r.calls, inWin, entries: r.entries || [], stats: r.stats || null });
|
|
554
587
|
}
|
|
555
588
|
if (!got.length) return { unreadable: oldNode ? 'node_old' : dbError ? 'db' : (found ? 'empty' : 'no_log') };
|
|
556
589
|
if (got.length > 1) {
|
|
@@ -564,7 +597,10 @@ async function gatherSample(c, tz) {
|
|
|
564
597
|
const g = got[0];
|
|
565
598
|
if (g.inWin.every((x) => unpriceable(x.model))) return { unreadable: 'model' };
|
|
566
599
|
const noModel = g.inWin.filter((x) => String(x.model || '').trim() === '').length;
|
|
567
|
-
|
|
600
|
+
const client = clientOf(g.src.reader, g.entries)
|
|
601
|
+
|| (g.src.reader === 'recipe' && g.src.client ? { name: g.src.client, entries: [] } : null);
|
|
602
|
+
const readerName = g.src.reader === 'recipe' ? g.src.recipe : g.src.reader;
|
|
603
|
+
return { sample: buildSample({ reader: readerName, calls: g.calls, tz, nowMs }), client, stats: g.stats || null, drops: { old: g.calls.length - g.inWin.length, nomodel: noModel } };
|
|
568
604
|
}
|
|
569
605
|
|
|
570
606
|
function resultLines(d, env) {
|
|
@@ -590,6 +626,7 @@ export async function cmdSample(c) {
|
|
|
590
626
|
let summary = null;
|
|
591
627
|
let client = null;
|
|
592
628
|
let drops = null;
|
|
629
|
+
let stats = null;
|
|
593
630
|
if (c.flags.unreadable != null) {
|
|
594
631
|
const reason = String(c.flags.unreadable).replace(/\s+/g, ' ').trim();
|
|
595
632
|
if (!reason || reason.length > REASON_MAX) return c.fail(2, 'bad_reason', t('sample.bad_reason', { max: REASON_MAX }, env));
|
|
@@ -601,6 +638,14 @@ export async function cmdSample(c) {
|
|
|
601
638
|
c.fromSource = sourceFromPath(p);
|
|
602
639
|
}
|
|
603
640
|
const g = c.flags.from != null && !c.fromSource ? { unreadable: 'no_log' } : await gatherSample(c, tz);
|
|
641
|
+
if (g.bad) {
|
|
642
|
+
const msg = t('sample.recipe_bad', { recipe: g.bad.recipe, file: g.bad.file || '-', line: g.bad.line, msg: g.bad.message }, env);
|
|
643
|
+
if (c.flags.json) {
|
|
644
|
+
c.log(JSON.stringify({ ok: false, error: 'recipe_bad_line', recipe: g.bad.recipe, file: g.bad.file || null, line: g.bad.line, message: g.bad.message }));
|
|
645
|
+
return 2;
|
|
646
|
+
}
|
|
647
|
+
return c.fail(2, 'recipe_bad_line', msg);
|
|
648
|
+
}
|
|
604
649
|
if (g.several) {
|
|
605
650
|
if (c.flags.json) {
|
|
606
651
|
c.log(JSON.stringify({ ok: false, error: 'several_sources', message: t('sample.several', {}, env), sources: g.several }));
|
|
@@ -611,10 +656,12 @@ export async function cmdSample(c) {
|
|
|
611
656
|
return 2;
|
|
612
657
|
}
|
|
613
658
|
body = g.sample || { kind: KIND, unreadable: true, reason: reasonText(g.unreadable, env) };
|
|
614
|
-
if (g.sample) { summary = summarize(g.sample); client = g.client; drops = g.drops || null; }
|
|
659
|
+
if (g.sample) { summary = summarize(g.sample); client = g.client; drops = g.drops || null; stats = g.stats || null; }
|
|
615
660
|
}
|
|
616
661
|
const clientField = client ? { client } : {};
|
|
617
|
-
const clientLine = client
|
|
662
|
+
const clientLine = client
|
|
663
|
+
? [t('sample.client', { v: client.entries.length ? client.name + ' · ' + client.entries.join(', ') : client.name }, env)]
|
|
664
|
+
: [];
|
|
618
665
|
|
|
619
666
|
if (c.flags.dryRun) {
|
|
620
667
|
if (c.flags.json) { c.log(JSON.stringify({ ok: true, dry_run: true, sample: body, ...clientField })); return 0; }
|
|
@@ -624,6 +671,10 @@ export async function cmdSample(c) {
|
|
|
624
671
|
c.log(t('sample.dry_title', { reader: body.reader, days: summary.days, calls: summary.calls }, env));
|
|
625
672
|
for (const l of clientLine) c.log(l);
|
|
626
673
|
c.log(t('sample.dry_models', { v: summary.models.map((m) => `${m.model} ${m.share}%`).join(', ') }, env));
|
|
674
|
+
if (stats) {
|
|
675
|
+
c.log(t('sample.dry_recipe', { recipe: stats.recipe, files: stats.files, lines: stats.lines, models: stats.models.join(', ') || '-' }, env));
|
|
676
|
+
if (stats.sample) c.log(t('sample.dry_row', { v: JSON.stringify(stats.sample) }, env));
|
|
677
|
+
}
|
|
627
678
|
if (drops && (drops.old || drops.nomodel)) c.log(t('sample.dry_dropped', drops, env));
|
|
628
679
|
}
|
|
629
680
|
c.log(t('sample.own_only', {}, env));
|
package/src/mt/text.js
CHANGED
|
@@ -130,6 +130,9 @@ const TEXT = {
|
|
|
130
130
|
'sample.dry_title': { zh: '试读(不上传):{reader},{days} 天,{calls} 次调用', en: 'Trial read (nothing sent): {reader}, {days} days, {calls} calls' },
|
|
131
131
|
'sample.client': { zh: ' 客户端:{v}', en: ' Client: {v}' },
|
|
132
132
|
'sample.dry_models': { zh: ' 模型(按输入 + 输出 token 占比):{v}', en: ' Models (share of input + output tokens): {v}' },
|
|
133
|
+
'sample.dry_recipe': { zh: ' 配方 {recipe}:{files} 个文件,{lines} 行命中,模型 {models}', en: ' Recipe {recipe}: {files} files, {lines} lines matched, models {models}' },
|
|
134
|
+
'sample.dry_row': { zh: ' 样例:{v}', en: ' Sample: {v}' },
|
|
135
|
+
'sample.recipe_bad': { zh: '配方 {recipe} 读不了:{file} 第 {line} 行,{msg}', en: 'Recipe {recipe} cannot be read: {file} line {line}, {msg}' },
|
|
133
136
|
'sample.dry_unreadable': {
|
|
134
137
|
zh: '试读(不上传):读不到可用的用量({reason})。正式运行会把这句原因交给平台。',
|
|
135
138
|
en: 'Trial read (nothing sent): no usable usage ({reason}). A real run sends this reason to the platform.',
|