@anytowork/cli 0.12.66 → 0.12.67
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 +71 -9
- package/src/mt/sample.js +4 -1
- package/src/mt/text.js +4 -0
package/package.json
CHANGED
package/src/mt/recipes.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
// v0.38.
|
|
1
|
+
// v0.38.71 (CLI 0.12.67): a recipe-driven log reader.
|
|
2
2
|
//
|
|
3
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
4
|
// data: where its logs are, which file name to take, which lines carry a model call, where the numbers sit, and which
|
|
@@ -21,6 +21,10 @@
|
|
|
21
21
|
// required fields a matching line must carry (a missing value is refused, never counted as 0)
|
|
22
22
|
// modelSuffixes trailing tags that do not change the price (thinking effort); stripped from the end, repeatedly
|
|
23
23
|
// sessionFrom "dir" = the first folder under the root is the conversation id
|
|
24
|
+
// sniff (optional) a function(headText) -> bool. A recipe with sniff has no default folder and is never found by
|
|
25
|
+
// file name or folder walk: it is used only for a single --from file whose first line passes sniff
|
|
26
|
+
// strictLines (optional) every non-empty line must be a JSON object; anything else is refused, never skipped
|
|
27
|
+
// dedupeRequired (optional) a matching line without the dedupeKey value is refused
|
|
24
28
|
//
|
|
25
29
|
// Adding an Agent: append one object to RECIPES. No other file changes.
|
|
26
30
|
|
|
@@ -51,8 +55,43 @@ export const RECIPES = [
|
|
|
51
55
|
modelSuffixes: ["-low", "-medium", "-high", "-xhigh", "-sonar", "-tiered"],
|
|
52
56
|
sessionFrom: "dir",
|
|
53
57
|
},
|
|
58
|
+
// v0.38.71: a file an Agent exported by hand (its usage lives in a runtime database, not in a local log). One JSON object
|
|
59
|
+
// per line: message_id (the call, written twice keeps the last), model, input_tokens, output_tokens, created_at (unix
|
|
60
|
+
// seconds or ISO time), optional cache_read_tokens / cache_write_tokens; any other field is ignored. Never scanned by
|
|
61
|
+
// default. The platform is told reader "export" and marks the check low-trust: an exported file is the Agent own copy,
|
|
62
|
+
// so it must not pass for a log the CLI read on this machine.
|
|
63
|
+
{
|
|
64
|
+
id: "export",
|
|
65
|
+
client: null,
|
|
66
|
+
roots: [],
|
|
67
|
+
glob: "*.jsonl",
|
|
68
|
+
fields: {
|
|
69
|
+
model: "model",
|
|
70
|
+
input: "input_tokens",
|
|
71
|
+
output: "output_tokens",
|
|
72
|
+
cacheRead: "cache_read_tokens",
|
|
73
|
+
cacheWrite: "cache_write_tokens",
|
|
74
|
+
time: "created_at",
|
|
75
|
+
},
|
|
76
|
+
dedupeKey: "message_id",
|
|
77
|
+
dedupeRequired: true,
|
|
78
|
+
strictLines: true,
|
|
79
|
+
required: ["model", "time", "input", "output"],
|
|
80
|
+
modelSuffixes: [],
|
|
81
|
+
sniff: sniffExport,
|
|
82
|
+
},
|
|
54
83
|
];
|
|
55
84
|
|
|
85
|
+
/** The first non-empty line of an export file: a JSON object carrying all of message_id / model / created_at / both token counts. */
|
|
86
|
+
function sniffExport(head) {
|
|
87
|
+
const first = String(head || "").split("\n").find((l) => l.trim() !== "");
|
|
88
|
+
if (!first || first.trim()[0] !== "{") return false;
|
|
89
|
+
let o;
|
|
90
|
+
try { o = JSON.parse(first); } catch { return false; }
|
|
91
|
+
return !!o && typeof o === "object"
|
|
92
|
+
&& ["message_id", "model", "created_at", "input_tokens", "output_tokens"].every((k) => k in o);
|
|
93
|
+
}
|
|
94
|
+
|
|
56
95
|
export function recipeById(id) {
|
|
57
96
|
return RECIPES.find((r) => r.id === id) || null;
|
|
58
97
|
}
|
|
@@ -98,7 +137,8 @@ function isEmpty(v) {
|
|
|
98
137
|
|
|
99
138
|
function toMs(v) {
|
|
100
139
|
if (v == null || v === "") return null;
|
|
101
|
-
const
|
|
140
|
+
const num = typeof v === "number" ? v : (/^\s*\d+(\.\d+)?\s*$/.test(String(v)) ? Number(v) : null); // unix seconds / ms, also as text
|
|
141
|
+
const ms = num != null ? (num < 1e12 ? num * 1000 : num) : Date.parse(String(v));
|
|
102
142
|
return Number.isFinite(ms) ? ms : null;
|
|
103
143
|
}
|
|
104
144
|
|
|
@@ -186,6 +226,7 @@ function rowOf(recipe, obj, entry, lineNo) {
|
|
|
186
226
|
const cacheRead = countAt(recipe, obj, "cacheRead", entry.file, lineNo);
|
|
187
227
|
const cacheWrite = countAt(recipe, obj, "cacheWrite", entry.file, lineNo);
|
|
188
228
|
const step = recipe.dedupeKey ? fieldOf(obj, recipe.dedupeKey) : undefined;
|
|
229
|
+
if (recipe.dedupeRequired && isEmpty(step)) bad(recipe, entry.file, lineNo, "missing " + recipe.dedupeKey);
|
|
189
230
|
const id = isEmpty(step) ? path.basename(entry.file) + ":" + lineNo : String(step);
|
|
190
231
|
return {
|
|
191
232
|
call_id: (entry.session || path.basename(entry.file)) + ":" + id,
|
|
@@ -261,11 +302,19 @@ export function readRecipeCalls(recipe, { home = os.homedir(), sinceMs = 0, root
|
|
|
261
302
|
const list = text.split("\n");
|
|
262
303
|
for (let i = 0; i < list.length; i += 1) {
|
|
263
304
|
const raw = list[i];
|
|
264
|
-
if (
|
|
305
|
+
if (recipe.strictLines && raw.trim() === "") continue;
|
|
306
|
+
if (!raw || (!recipe.strictLines && raw[0] !== "{")) continue;
|
|
265
307
|
if (needle && raw.indexOf(needle) === -1) continue;
|
|
266
308
|
let obj;
|
|
267
|
-
try { obj = JSON.parse(raw); } catch {
|
|
268
|
-
|
|
309
|
+
try { obj = JSON.parse(raw); } catch {
|
|
310
|
+
if (recipe.strictLines) bad(recipe, entry.file, i + 1, "not a JSON line");
|
|
311
|
+
continue;
|
|
312
|
+
}
|
|
313
|
+
if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
|
|
314
|
+
if (recipe.strictLines) bad(recipe, entry.file, i + 1, "not a JSON object");
|
|
315
|
+
continue;
|
|
316
|
+
}
|
|
317
|
+
if (!matchesFilter(recipe, obj)) continue;
|
|
269
318
|
lines += 1;
|
|
270
319
|
const row = rowOf(recipe, obj, entry, i + 1);
|
|
271
320
|
const step = recipe.dedupeKey ? fieldOf(obj, recipe.dedupeKey) : undefined;
|
|
@@ -299,21 +348,34 @@ export function recipeForPath(abs, st) {
|
|
|
299
348
|
const as = (r) => ({ reader: "recipe", recipe: r.id, client: r.client || null, roots: [abs], root: abs });
|
|
300
349
|
if (st.isFile()) {
|
|
301
350
|
const name = path.basename(abs);
|
|
302
|
-
const r = RECIPES.find((x) => nameMatches(x.glob, name));
|
|
303
|
-
|
|
351
|
+
const r = RECIPES.find((x) => !x.sniff && nameMatches(x.glob, name));
|
|
352
|
+
if (r) return as(r);
|
|
353
|
+
// a file with its own shape check (an Agent export): only a .jsonl file whose first line passes
|
|
354
|
+
const s = RECIPES.find((x) => x.sniff && abs.endsWith(".jsonl") && sniffFile(x, abs));
|
|
355
|
+
return s ? as(s) : null;
|
|
304
356
|
}
|
|
305
357
|
if (!st.isDirectory()) return null;
|
|
306
|
-
for (const r of RECIPES) {
|
|
358
|
+
for (const r of RECIPES.filter((x) => !x.sniff)) {
|
|
307
359
|
for (const root of expandRoots(r, os.homedir())) {
|
|
308
360
|
if (abs === root || abs.startsWith(root + path.sep)) return as(r);
|
|
309
361
|
}
|
|
310
362
|
}
|
|
311
|
-
for (const r of RECIPES) {
|
|
363
|
+
for (const r of RECIPES.filter((x) => !x.sniff)) {
|
|
312
364
|
if (hasFileBelow(abs, r, 3)) return as(r);
|
|
313
365
|
}
|
|
314
366
|
return null;
|
|
315
367
|
}
|
|
316
368
|
|
|
369
|
+
function sniffFile(recipe, abs) {
|
|
370
|
+
try {
|
|
371
|
+
const fd = fs.openSync(abs, "r");
|
|
372
|
+
try {
|
|
373
|
+
const b = Buffer.alloc(65536);
|
|
374
|
+
return !!recipe.sniff(b.toString("utf8", 0, fs.readSync(fd, b, 0, b.length, 0)));
|
|
375
|
+
} finally { fs.closeSync(fd); }
|
|
376
|
+
} catch { return false; }
|
|
377
|
+
}
|
|
378
|
+
|
|
317
379
|
function hasFileBelow(dir, recipe, depth) {
|
|
318
380
|
let entries;
|
|
319
381
|
try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return false; }
|
package/src/mt/sample.js
CHANGED
|
@@ -521,7 +521,8 @@ export function buildSample({ reader, calls, tz, nowMs }) {
|
|
|
521
521
|
return {
|
|
522
522
|
kind: KIND,
|
|
523
523
|
tz,
|
|
524
|
-
|
|
524
|
+
// an Agent export file is the Agent own copy: it never passes for a log read on this computer
|
|
525
|
+
source: reader === 'export' ? 'agent-export' : 'local-log',
|
|
525
526
|
reader,
|
|
526
527
|
calls: inWin.slice(-RECENT_CALLS).map((c) => ({
|
|
527
528
|
at: isoLocal(c.atMs, tz), model: c.model, input_tokens: c.input_tokens, output_tokens: c.output_tokens,
|
|
@@ -677,6 +678,7 @@ export async function cmdSample(c) {
|
|
|
677
678
|
}
|
|
678
679
|
if (drops && (drops.old || drops.nomodel)) c.log(t('sample.dry_dropped', drops, env));
|
|
679
680
|
}
|
|
681
|
+
if (body.reader === 'export') c.log(t('sample.export_note', {}, env));
|
|
680
682
|
c.log(t('sample.own_only', {}, env));
|
|
681
683
|
return 0;
|
|
682
684
|
}
|
|
@@ -701,5 +703,6 @@ export async function cmdSample(c) {
|
|
|
701
703
|
: t('sample.delivered', { reader: body.reader, days: summary.days, calls: summary.calls }, env));
|
|
702
704
|
for (const l of clientLine) c.log(l);
|
|
703
705
|
for (const l of resultLines(d, env)) c.log(l);
|
|
706
|
+
if (body.reader === 'export') c.log(t('sample.export_note', {}, env));
|
|
704
707
|
return 0;
|
|
705
708
|
}
|
package/src/mt/text.js
CHANGED
|
@@ -137,6 +137,10 @@ const TEXT = {
|
|
|
137
137
|
zh: '试读(不上传):读不到可用的用量({reason})。正式运行会把这句原因交给平台。',
|
|
138
138
|
en: 'Trial read (nothing sent): no usable usage ({reason}). A real run sends this reason to the platform.',
|
|
139
139
|
},
|
|
140
|
+
'sample.export_note': {
|
|
141
|
+
zh: '来源是 Agent 导出的文件,平台会标为低可信来源。',
|
|
142
|
+
en: 'The source is a file exported by the Agent; the platform marks it as a low-trust source.',
|
|
143
|
+
},
|
|
140
144
|
'sample.own_only': {
|
|
141
145
|
zh: '只算你自己这个程序的用量:如果读到的不是你自己这个程序的日志,用 --from <你自己这个程序的日志位置> 指定。',
|
|
142
146
|
en: 'Only the usage of your own program counts: if this is not the log of your own program, point to it with --from <your own log path>.',
|