@anytowork/cli 0.12.72 → 0.12.73
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/README.md +13 -79
- package/package.json +1 -1
- package/src/api.js +2 -50
- package/src/apply-backoff.js +1 -1
- package/src/cli-parse.js +7 -131
- package/src/connect.js +2 -2
- package/src/exit-reasons.js +0 -9
- package/src/format.js +24 -143
- package/src/index.js +7 -99
- package/src/model-pledge.js +6 -6
- package/src/mt/collect.js +2 -26
- package/src/mt/commands.js +3 -84
- package/src/mt/format-check.js +2 -2
- package/src/mt/readers.js +1 -1
- package/src/mt/text.js +20 -89
- package/src/mt/wake-attribution.js +2 -2
- package/src/mt/workdirs.js +1 -18
- package/src/setup-commands.js +6 -6
- package/src/setup-hints.js +3 -19
- package/src/space-commands.js +2 -41
- package/src/space-sync.js +2 -2
- package/src/chat-commands.js +0 -685
- package/src/chat-state.js +0 -77
- package/src/hire-commands.js +0 -706
- package/src/listen-command.js +0 -79
- package/src/listener/commands.js +0 -183
- package/src/listener/config.js +0 -216
- package/src/listener/hire-wake.js +0 -60
- package/src/listener/install.js +0 -233
- package/src/listener/nudge.js +0 -88
- package/src/listener/prompt.js +0 -111
- package/src/listener/queue.js +0 -112
- package/src/listener/run-error.js +0 -81
- package/src/listener/run.js +0 -723
- package/src/listener/text.js +0 -103
- package/src/mt/inrun.js +0 -351
- package/src/mt/recipes.js +0 -478
- package/src/mt/sample.js +0 -708
- package/src/reply-hint.js +0 -21
- package/src/wb-rules-text.js +0 -169
package/src/mt/recipes.js
DELETED
|
@@ -1,478 +0,0 @@
|
|
|
1
|
-
// v0.38.71 (CLI 0.12.67): 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
|
-
// 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
|
|
28
|
-
// program (optional) the program name sent with in-run usage rows (defaults to id)
|
|
29
|
-
// commands (optional) start-command words that name this program (`agy` -> antigravity) for the always-on listener
|
|
30
|
-
// input (optional) {lineFilter, text, time}: which lines are a message from OUTSIDE the Agent (the person / the
|
|
31
|
-
// platform wake-up). With it the recipe also gives the shared event list of wake-attribution.js, so the
|
|
32
|
-
// usage of work done while hired is read live by the always-on listener (inrun.js) with no other code.
|
|
33
|
-
// Without it the recipe is only used by the job-seeking check (and never priced during work).
|
|
34
|
-
//
|
|
35
|
-
// Adding an Agent: append one object to RECIPES. No other file changes (the job-seeking check, the in-run upload of the
|
|
36
|
-
// always-on listener and its later re-send all read RECIPES).
|
|
37
|
-
|
|
38
|
-
import fs from "node:fs";
|
|
39
|
-
import os from "node:os";
|
|
40
|
-
import path from "node:path";
|
|
41
|
-
import { guessProvider } from "./readers.js";
|
|
42
|
-
|
|
43
|
-
const MAX_DEPTH = 6;
|
|
44
|
-
|
|
45
|
-
export const RECIPES = [
|
|
46
|
-
{
|
|
47
|
-
id: "antigravity",
|
|
48
|
-
client: "Antigravity",
|
|
49
|
-
roots: [".gemini/antigravity/brain", ".gemini/antigravity-cli/brain"],
|
|
50
|
-
glob: "transcript_full.jsonl",
|
|
51
|
-
excludeDirs: ["chunks"],
|
|
52
|
-
lineFilter: { source: "MODEL", type: "PLANNER_RESPONSE" },
|
|
53
|
-
fields: {
|
|
54
|
-
model: "model",
|
|
55
|
-
input: "input_tokens",
|
|
56
|
-
output: "output_tokens",
|
|
57
|
-
cacheRead: "cache_read_tokens",
|
|
58
|
-
time: "created_at",
|
|
59
|
-
},
|
|
60
|
-
dedupeKey: "step_index",
|
|
61
|
-
required: ["model", "time", "input", "output"],
|
|
62
|
-
modelSuffixes: ["-low", "-medium", "-high", "-xhigh", "-sonar", "-tiered"],
|
|
63
|
-
sessionFrom: "dir",
|
|
64
|
-
// 2026-11-27: the prompt the listener hands over (it ends with the wake-up number) is the one USER_INPUT line of a run.
|
|
65
|
-
// System lines (SYSTEM_SDK / SYSTEM) and model replies (MODEL GENERIC) are never read for a number.
|
|
66
|
-
program: "antigravity",
|
|
67
|
-
commands: ["agy", "antigravity"],
|
|
68
|
-
input: { lineFilter: { source: "USER_EXPLICIT", type: "USER_INPUT" }, text: "content", time: "created_at" },
|
|
69
|
-
},
|
|
70
|
-
// 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
|
|
71
|
-
// per line: message_id (the call, written twice keeps the last), model, input_tokens, output_tokens, created_at (unix
|
|
72
|
-
// seconds or ISO time), optional cache_read_tokens / cache_write_tokens; any other field is ignored. Never scanned by
|
|
73
|
-
// default. The platform is told reader "export" and marks the check low-trust: an exported file is the Agent own copy,
|
|
74
|
-
// so it must not pass for a log the CLI read on this machine.
|
|
75
|
-
{
|
|
76
|
-
id: "export",
|
|
77
|
-
client: null,
|
|
78
|
-
roots: [],
|
|
79
|
-
glob: "*.jsonl",
|
|
80
|
-
fields: {
|
|
81
|
-
model: "model",
|
|
82
|
-
input: "input_tokens",
|
|
83
|
-
output: "output_tokens",
|
|
84
|
-
cacheRead: "cache_read_tokens",
|
|
85
|
-
cacheWrite: "cache_write_tokens",
|
|
86
|
-
time: "created_at",
|
|
87
|
-
},
|
|
88
|
-
dedupeKey: "message_id",
|
|
89
|
-
dedupeRequired: true,
|
|
90
|
-
strictLines: true,
|
|
91
|
-
required: ["model", "time", "input", "output"],
|
|
92
|
-
modelSuffixes: [],
|
|
93
|
-
sniff: sniffExport,
|
|
94
|
-
},
|
|
95
|
-
];
|
|
96
|
-
|
|
97
|
-
/** The first non-empty line of an export file: a JSON object carrying all of message_id / model / created_at / both token counts. */
|
|
98
|
-
function sniffExport(head) {
|
|
99
|
-
const first = String(head || "").split("\n").find((l) => l.trim() !== "");
|
|
100
|
-
if (!first || first.trim()[0] !== "{") return false;
|
|
101
|
-
let o;
|
|
102
|
-
try { o = JSON.parse(first); } catch { return false; }
|
|
103
|
-
return !!o && typeof o === "object"
|
|
104
|
-
&& ["message_id", "model", "created_at", "input_tokens", "output_tokens"].every((k) => k in o);
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
export function recipeById(id) {
|
|
108
|
-
return RECIPES.find((r) => r.id === id) || null;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
/** The folders of a recipe: relative entries become paths under the home folder; absolute entries stay as they are. */
|
|
112
|
-
export function expandRoots(recipe, home) {
|
|
113
|
-
return (recipe.roots || []).map((r) => (path.isAbsolute(r) ? r : path.join(home, r)));
|
|
114
|
-
}
|
|
115
|
-
|
|
116
|
-
function isDir(p) {
|
|
117
|
-
try { return fs.statSync(p).isDirectory(); } catch { return false; }
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function isFile(p) {
|
|
121
|
-
try { return fs.statSync(p).isFile(); } catch { return false; }
|
|
122
|
-
}
|
|
123
|
-
|
|
124
|
-
/** The existing default folders of a recipe. */
|
|
125
|
-
export function recipeRoots(recipe, home) {
|
|
126
|
-
return expandRoots(recipe, home).filter(isDir);
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/** A file name pattern: "*" is any run of characters, everything else is literal. */
|
|
130
|
-
export function nameMatches(pattern, name) {
|
|
131
|
-
if (!pattern || pattern === "*") return true;
|
|
132
|
-
if (!pattern.includes("*")) return name === pattern;
|
|
133
|
-
const re = new RegExp("^" + pattern.split("*").map((s) => s.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join(".*") + "$");
|
|
134
|
-
return re.test(name);
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
function fieldOf(obj, dotted) {
|
|
138
|
-
let v = obj;
|
|
139
|
-
for (const k of String(dotted).split(".")) {
|
|
140
|
-
if (v == null || typeof v !== "object") return undefined;
|
|
141
|
-
v = v[k];
|
|
142
|
-
}
|
|
143
|
-
return v;
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function isEmpty(v) {
|
|
147
|
-
return v === undefined || v === null || (typeof v === "string" && v.trim() === "");
|
|
148
|
-
}
|
|
149
|
-
|
|
150
|
-
function toMs(v) {
|
|
151
|
-
if (v == null || v === "") return null;
|
|
152
|
-
const num = typeof v === "number" ? v : (/^\s*\d+(\.\d+)?\s*$/.test(String(v)) ? Number(v) : null); // unix seconds / ms, also as text
|
|
153
|
-
const ms = num != null ? (num < 1e12 ? num * 1000 : num) : Date.parse(String(v));
|
|
154
|
-
return Number.isFinite(ms) ? ms : null;
|
|
155
|
-
}
|
|
156
|
-
|
|
157
|
-
/** A recipe or a log line that cannot be used: names the recipe and, for a line, the file and the line number. */
|
|
158
|
-
export class RecipeError extends Error {
|
|
159
|
-
constructor(info) {
|
|
160
|
-
super("recipe " + info.recipe + (info.file ? " (" + info.file + " line " + info.line + ")" : "") + ": " + info.message);
|
|
161
|
-
this.name = "RecipeError";
|
|
162
|
-
this.code = "recipe_bad_line";
|
|
163
|
-
this.info = info;
|
|
164
|
-
}
|
|
165
|
-
}
|
|
166
|
-
|
|
167
|
-
function bad(recipe, file, line, message) {
|
|
168
|
-
throw new RecipeError({ recipe: recipe.id, file, line, message });
|
|
169
|
-
}
|
|
170
|
-
|
|
171
|
-
/** A model name without its thinking-effort tags ("gemini-4-carbon-medium" -> "gemini-4-carbon"). */
|
|
172
|
-
export function normalizeModel(model, suffixes = []) {
|
|
173
|
-
let m = String(model == null ? "" : model).trim();
|
|
174
|
-
const list = [...suffixes].sort((a, b) => b.length - a.length);
|
|
175
|
-
let again = true;
|
|
176
|
-
while (again) {
|
|
177
|
-
again = false;
|
|
178
|
-
const low = m.toLowerCase();
|
|
179
|
-
for (const s of list) {
|
|
180
|
-
if (low.length > s.length && low.endsWith(s.toLowerCase())) {
|
|
181
|
-
m = m.slice(0, m.length - s.length);
|
|
182
|
-
again = true;
|
|
183
|
-
break;
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
return m;
|
|
188
|
-
}
|
|
189
|
-
|
|
190
|
-
function checkRecipe(recipe) {
|
|
191
|
-
if (!recipe || !recipe.id) throw new RecipeError({ recipe: "?", file: null, line: 0, message: "recipe has no id" });
|
|
192
|
-
for (const k of ["model", "time", "input", "output"]) {
|
|
193
|
-
if (!recipe.fields || !recipe.fields[k]) bad(recipe, null, 0, "recipe is missing fields." + k);
|
|
194
|
-
}
|
|
195
|
-
}
|
|
196
|
-
|
|
197
|
-
function matchesFilter(recipe, obj) {
|
|
198
|
-
const f = recipe.lineFilter || {};
|
|
199
|
-
for (const [k, v] of Object.entries(f)) {
|
|
200
|
-
if (String(fieldOf(obj, k)) !== String(v)) return false;
|
|
201
|
-
}
|
|
202
|
-
return true;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
function countAt(recipe, obj, key, file, line) {
|
|
206
|
-
const dotted = recipe.fields[key];
|
|
207
|
-
if (!dotted) return null;
|
|
208
|
-
const v = fieldOf(obj, dotted);
|
|
209
|
-
const required = (recipe.required || []).includes(key);
|
|
210
|
-
if (isEmpty(v)) {
|
|
211
|
-
if (required) bad(recipe, file, line, "missing " + key + " (" + dotted + ")");
|
|
212
|
-
return null;
|
|
213
|
-
}
|
|
214
|
-
const n = typeof v === "number" ? v : Number(String(v).trim());
|
|
215
|
-
if (!Number.isFinite(n)) bad(recipe, file, line, key + " (" + dotted + ") is not a number: " + JSON.stringify(v));
|
|
216
|
-
if (n < 0) bad(recipe, file, line, key + " (" + dotted + ") is negative: " + n);
|
|
217
|
-
return Math.floor(n);
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function sessionOf(recipe, file, root) {
|
|
221
|
-
if (recipe.sessionFrom !== "dir") return null;
|
|
222
|
-
if (file === root) return null;
|
|
223
|
-
const rel = path.relative(root, file);
|
|
224
|
-
if (!rel || rel.startsWith("..")) return null;
|
|
225
|
-
const first = rel.split(path.sep)[0];
|
|
226
|
-
return first && first !== path.basename(file) ? first : null;
|
|
227
|
-
}
|
|
228
|
-
|
|
229
|
-
function rowOf(recipe, obj, entry, lineNo) {
|
|
230
|
-
const modelRaw = String(fieldOf(obj, recipe.fields.model) ?? "").trim();
|
|
231
|
-
if (!modelRaw) bad(recipe, entry.file, lineNo, "missing model (" + recipe.fields.model + ")");
|
|
232
|
-
const model = normalizeModel(modelRaw, recipe.modelSuffixes);
|
|
233
|
-
const atRaw = fieldOf(obj, recipe.fields.time);
|
|
234
|
-
const atMs = toMs(atRaw);
|
|
235
|
-
if (atMs == null) bad(recipe, entry.file, lineNo, "bad time (" + recipe.fields.time + "): " + JSON.stringify(atRaw));
|
|
236
|
-
const input = countAt(recipe, obj, "input", entry.file, lineNo);
|
|
237
|
-
const output = countAt(recipe, obj, "output", entry.file, lineNo);
|
|
238
|
-
const cacheRead = countAt(recipe, obj, "cacheRead", entry.file, lineNo);
|
|
239
|
-
const cacheWrite = countAt(recipe, obj, "cacheWrite", entry.file, lineNo);
|
|
240
|
-
const step = recipe.dedupeKey ? fieldOf(obj, recipe.dedupeKey) : undefined;
|
|
241
|
-
if (recipe.dedupeRequired && isEmpty(step)) bad(recipe, entry.file, lineNo, "missing " + recipe.dedupeKey);
|
|
242
|
-
const id = isEmpty(step) ? path.basename(entry.file) + ":" + lineNo : String(step);
|
|
243
|
-
return {
|
|
244
|
-
call_id: (entry.session || path.basename(entry.file)) + ":" + id,
|
|
245
|
-
source_id: isEmpty(step) ? null : String(step), // the call's own id in the log (message_id / step), null when it has none
|
|
246
|
-
conversation_id: entry.session || null,
|
|
247
|
-
provider: guessProvider(model),
|
|
248
|
-
model,
|
|
249
|
-
started_at: new Date(atMs).toISOString().replace(/\.\d{3}Z$/, "Z"),
|
|
250
|
-
ended_at: null,
|
|
251
|
-
input_tokens: input,
|
|
252
|
-
output_tokens: output,
|
|
253
|
-
cache_write_tokens: cacheWrite,
|
|
254
|
-
cache_read_tokens: cacheRead,
|
|
255
|
-
cwd: null,
|
|
256
|
-
seenMs: atMs,
|
|
257
|
-
atMs,
|
|
258
|
-
};
|
|
259
|
-
}
|
|
260
|
-
|
|
261
|
-
function walk(dir, root, depth, out, recipe, sinceMs) {
|
|
262
|
-
let entries;
|
|
263
|
-
try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return; }
|
|
264
|
-
for (const e of entries) {
|
|
265
|
-
const p = path.join(dir, e.name);
|
|
266
|
-
if (e.isDirectory()) {
|
|
267
|
-
if (depth >= MAX_DEPTH) continue;
|
|
268
|
-
if ((recipe.excludeDirs || []).includes(e.name)) continue;
|
|
269
|
-
walk(p, root, depth + 1, out, recipe, sinceMs);
|
|
270
|
-
continue;
|
|
271
|
-
}
|
|
272
|
-
if (!e.isFile() || !nameMatches(recipe.glob, e.name)) continue;
|
|
273
|
-
try { if (sinceMs && fs.statSync(p).mtimeMs < sinceMs) continue; } catch { continue; }
|
|
274
|
-
out.push({ file: p, root, session: sessionOf(recipe, p, root) });
|
|
275
|
-
}
|
|
276
|
-
}
|
|
277
|
-
|
|
278
|
-
/** The files a recipe reads: `roots` overrides the recipe folders (used by --from). */
|
|
279
|
-
function recipeFiles(recipe, { home = os.homedir(), sinceMs = 0, roots = null } = {}) {
|
|
280
|
-
const baseRoots = roots || expandRoots(recipe, home);
|
|
281
|
-
const files = [];
|
|
282
|
-
const seenFiles = new Set();
|
|
283
|
-
for (const root of baseRoots) {
|
|
284
|
-
if (isFile(root)) {
|
|
285
|
-
if (nameMatches(recipe.glob, path.basename(root)) && !seenFiles.has(root)) {
|
|
286
|
-
seenFiles.add(root);
|
|
287
|
-
files.push({ file: root, root, session: null });
|
|
288
|
-
}
|
|
289
|
-
continue;
|
|
290
|
-
}
|
|
291
|
-
if (!isDir(root)) continue;
|
|
292
|
-
const found = [];
|
|
293
|
-
walk(root, root, 0, found, recipe, sinceMs);
|
|
294
|
-
for (const f of found) {
|
|
295
|
-
if (seenFiles.has(f.file)) continue;
|
|
296
|
-
seenFiles.add(f.file);
|
|
297
|
-
files.push(f);
|
|
298
|
-
}
|
|
299
|
-
}
|
|
300
|
-
return files;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
function textOf(v) {
|
|
304
|
-
if (v == null) return "";
|
|
305
|
-
return typeof v === "string" ? v : JSON.stringify(v);
|
|
306
|
-
}
|
|
307
|
-
|
|
308
|
-
/**
|
|
309
|
-
* Parse the text of one log file with a recipe. `entry` = {file, session}. Returns {rows, inputs, lines}:
|
|
310
|
-
* rows = one per call (a step written again keeps the last line), inputs = outside messages {ms, text} (only with
|
|
311
|
-
* `withInputs` and a recipe that has an `input` rule). Throws RecipeError when a matching call line is broken.
|
|
312
|
-
*/
|
|
313
|
-
export function parseRecipeText(recipe, text, entry, { withInputs = false } = {}) {
|
|
314
|
-
checkRecipe(recipe);
|
|
315
|
-
const inputRule = withInputs && recipe.input ? recipe.input : null;
|
|
316
|
-
// a line that cannot match need not be parsed: every matching line holds the first filter value somewhere
|
|
317
|
-
const needle = String(Object.values(recipe.lineFilter || {})[0] || "");
|
|
318
|
-
const inNeedle = inputRule ? String(Object.values(inputRule.lineFilter || {})[0] || "") : "";
|
|
319
|
-
const rows = [];
|
|
320
|
-
const inputs = [];
|
|
321
|
-
const at = new Map();
|
|
322
|
-
let lines = 0;
|
|
323
|
-
const list = String(text == null ? "" : text).split("\n");
|
|
324
|
-
for (let i = 0; i < list.length; i += 1) {
|
|
325
|
-
const raw = list[i];
|
|
326
|
-
if (recipe.strictLines && raw.trim() === "") continue;
|
|
327
|
-
if (!raw || (!recipe.strictLines && raw[0] !== "{")) continue;
|
|
328
|
-
const maybeCall = !needle || raw.indexOf(needle) !== -1;
|
|
329
|
-
const maybeInput = !!inputRule && (!inNeedle || raw.indexOf(inNeedle) !== -1);
|
|
330
|
-
if (!maybeCall && !maybeInput) continue;
|
|
331
|
-
let obj;
|
|
332
|
-
try { obj = JSON.parse(raw); } catch {
|
|
333
|
-
if (recipe.strictLines) bad(recipe, entry.file, i + 1, "not a JSON line");
|
|
334
|
-
continue;
|
|
335
|
-
}
|
|
336
|
-
if (!obj || typeof obj !== "object" || Array.isArray(obj)) {
|
|
337
|
-
if (recipe.strictLines) bad(recipe, entry.file, i + 1, "not a JSON object");
|
|
338
|
-
continue;
|
|
339
|
-
}
|
|
340
|
-
if (maybeInput && matchesFilter(inputRule, obj)) {
|
|
341
|
-
// an outside message with no usable time carries no number (its calls are then not taken), never an error
|
|
342
|
-
const ms = toMs(fieldOf(obj, inputRule.time || recipe.fields.time));
|
|
343
|
-
if (ms != null) inputs.push({ ms, text: textOf(fieldOf(obj, inputRule.text || "content")) });
|
|
344
|
-
continue;
|
|
345
|
-
}
|
|
346
|
-
if (!maybeCall || !matchesFilter(recipe, obj)) continue;
|
|
347
|
-
lines += 1;
|
|
348
|
-
const row = rowOf(recipe, obj, entry, i + 1);
|
|
349
|
-
const step = recipe.dedupeKey ? fieldOf(obj, recipe.dedupeKey) : undefined;
|
|
350
|
-
const key = isEmpty(step) ? entry.file + ":" + (i + 1) : entry.file + ":" + String(step);
|
|
351
|
-
if (at.has(key)) rows[at.get(key)] = row; // a step written again: the last line wins
|
|
352
|
-
else { at.set(key, rows.length); rows.push(row); }
|
|
353
|
-
}
|
|
354
|
-
return { rows, inputs, lines };
|
|
355
|
-
}
|
|
356
|
-
|
|
357
|
-
/**
|
|
358
|
-
* Read one recipe. `roots` overrides the recipe folders (used by --from). Returns
|
|
359
|
-
* { found, calls, files, lines, models, sample, client }. Throws RecipeError when a matching line is broken.
|
|
360
|
-
*/
|
|
361
|
-
export function readRecipeCalls(recipe, { home = os.homedir(), sinceMs = 0, roots = null } = {}) {
|
|
362
|
-
checkRecipe(recipe);
|
|
363
|
-
const files = recipeFiles(recipe, { home, sinceMs, roots });
|
|
364
|
-
const calls = [];
|
|
365
|
-
const models = new Set();
|
|
366
|
-
let lines = 0;
|
|
367
|
-
let sample = null;
|
|
368
|
-
for (const entry of files) {
|
|
369
|
-
let text;
|
|
370
|
-
try { text = fs.readFileSync(entry.file, "utf8"); } catch { continue; }
|
|
371
|
-
const p = parseRecipeText(recipe, text, entry);
|
|
372
|
-
lines += p.lines;
|
|
373
|
-
for (const row of p.rows) {
|
|
374
|
-
calls.push(row);
|
|
375
|
-
models.add(row.model);
|
|
376
|
-
if (!sample) sample = row;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
return {
|
|
380
|
-
found: files.length > 0,
|
|
381
|
-
calls,
|
|
382
|
-
files: files.length,
|
|
383
|
-
lines,
|
|
384
|
-
models: [...models],
|
|
385
|
-
sample: sample ? {
|
|
386
|
-
model: sample.model, at: sample.started_at, input_tokens: sample.input_tokens,
|
|
387
|
-
output_tokens: sample.output_tokens, cache_read_tokens: sample.cache_read_tokens,
|
|
388
|
-
} : null,
|
|
389
|
-
client: recipe.client || null,
|
|
390
|
-
};
|
|
391
|
-
}
|
|
392
|
-
|
|
393
|
-
/**
|
|
394
|
-
* 2026-11-27: one recipe as the shared event list of wake-attribution.js (what the hand-written readers give), so the
|
|
395
|
-
* always-on listener reads the usage of work done while hired with the same recipe the job-seeking check uses.
|
|
396
|
-
* Returns {calls, events, files, refused}. A recipe without an `input` rule gives no events (nothing can be attributed).
|
|
397
|
-
* A file with a broken call line is left out whole (counted in `refused`, never read as 0) and never blocks the others.
|
|
398
|
-
*/
|
|
399
|
-
export function readRecipeEvents(recipe, { home = os.homedir(), sinceMs = 0, roots = null } = {}) {
|
|
400
|
-
checkRecipe(recipe);
|
|
401
|
-
const files = recipeFiles(recipe, { home, sinceMs, roots });
|
|
402
|
-
const calls = [];
|
|
403
|
-
const events = [];
|
|
404
|
-
let refused = 0;
|
|
405
|
-
for (const entry of files) {
|
|
406
|
-
let text;
|
|
407
|
-
try { text = fs.readFileSync(entry.file, "utf8"); } catch { continue; }
|
|
408
|
-
let p;
|
|
409
|
-
try { p = parseRecipeText(recipe, text, entry, { withInputs: true }); } catch (e) {
|
|
410
|
-
if (e instanceof RecipeError) { refused += 1; continue; }
|
|
411
|
-
throw e;
|
|
412
|
-
}
|
|
413
|
-
const session = entry.session || entry.file;
|
|
414
|
-
for (const row of p.rows) calls.push(row);
|
|
415
|
-
if (!recipe.input) continue;
|
|
416
|
-
for (const inp of p.inputs) events.push({ kind: "input", session, ms: inp.ms, text: inp.text });
|
|
417
|
-
for (const row of p.rows) events.push({ kind: "call", session, ms: row.atMs, call: row });
|
|
418
|
-
}
|
|
419
|
-
return { calls, events, files: files.length, refused };
|
|
420
|
-
}
|
|
421
|
-
|
|
422
|
-
/** Recipes the always-on listener reads while an Agent works: a default folder and an outside-input rule. */
|
|
423
|
-
export function liveRecipes() {
|
|
424
|
-
return RECIPES.filter((r) => !r.sniff && (r.roots || []).length && r.input);
|
|
425
|
-
}
|
|
426
|
-
|
|
427
|
-
/** The program a start-command word names through a recipe (`agy` -> "antigravity"), or null. */
|
|
428
|
-
export function recipeProgramOf(word) {
|
|
429
|
-
const w = String(word || "").toLowerCase();
|
|
430
|
-
const r = RECIPES.find((x) => (x.commands || []).includes(w));
|
|
431
|
-
return r ? (r.program || r.id) : null;
|
|
432
|
-
}
|
|
433
|
-
|
|
434
|
-
/** A --from path that belongs to a recipe, or null. Checked before the hand-written path shapes. */
|
|
435
|
-
export function recipeForPath(abs, st) {
|
|
436
|
-
if (!st) return null;
|
|
437
|
-
const as = (r) => ({ reader: "recipe", recipe: r.id, client: r.client || null, roots: [abs], root: abs });
|
|
438
|
-
if (st.isFile()) {
|
|
439
|
-
const name = path.basename(abs);
|
|
440
|
-
const r = RECIPES.find((x) => !x.sniff && nameMatches(x.glob, name));
|
|
441
|
-
if (r) return as(r);
|
|
442
|
-
// a file with its own shape check (an Agent export): only a .jsonl file whose first line passes
|
|
443
|
-
const s = RECIPES.find((x) => x.sniff && abs.endsWith(".jsonl") && sniffFile(x, abs));
|
|
444
|
-
return s ? as(s) : null;
|
|
445
|
-
}
|
|
446
|
-
if (!st.isDirectory()) return null;
|
|
447
|
-
for (const r of RECIPES.filter((x) => !x.sniff)) {
|
|
448
|
-
for (const root of expandRoots(r, os.homedir())) {
|
|
449
|
-
if (abs === root || abs.startsWith(root + path.sep)) return as(r);
|
|
450
|
-
}
|
|
451
|
-
}
|
|
452
|
-
for (const r of RECIPES.filter((x) => !x.sniff)) {
|
|
453
|
-
if (hasFileBelow(abs, r, 3)) return as(r);
|
|
454
|
-
}
|
|
455
|
-
return null;
|
|
456
|
-
}
|
|
457
|
-
|
|
458
|
-
function sniffFile(recipe, abs) {
|
|
459
|
-
try {
|
|
460
|
-
const fd = fs.openSync(abs, "r");
|
|
461
|
-
try {
|
|
462
|
-
const b = Buffer.alloc(65536);
|
|
463
|
-
return !!recipe.sniff(b.toString("utf8", 0, fs.readSync(fd, b, 0, b.length, 0)));
|
|
464
|
-
} finally { fs.closeSync(fd); }
|
|
465
|
-
} catch { return false; }
|
|
466
|
-
}
|
|
467
|
-
|
|
468
|
-
function hasFileBelow(dir, recipe, depth) {
|
|
469
|
-
let entries;
|
|
470
|
-
try { entries = fs.readdirSync(dir, { withFileTypes: true }); } catch { return false; }
|
|
471
|
-
for (const e of entries) {
|
|
472
|
-
if (e.isFile() && nameMatches(recipe.glob, e.name)) return true;
|
|
473
|
-
if (e.isDirectory() && depth > 0 && !(recipe.excludeDirs || []).includes(e.name)) {
|
|
474
|
-
if (hasFileBelow(path.join(dir, e.name), recipe, depth - 1)) return true;
|
|
475
|
-
}
|
|
476
|
-
}
|
|
477
|
-
return false;
|
|
478
|
-
}
|