@vatio-ai/cli 0.88.0 → 0.90.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/README.md +1 -0
- package/bin/vatio.mjs +3 -4
- package/lib/api-client.mjs +14 -1
- package/lib/claude-session.mjs +3 -2
- package/lib/codex-session.mjs +223 -0
- package/lib/commands/deploy.mjs +161 -20
- package/lib/commands/support-commands.mjs +57 -23
- package/lib/config.mjs +33 -0
- package/lib/help.mjs +10 -4
- package/lib/http.mjs +12 -0
- package/lib/pull.mjs +605 -0
- package/lib/support.mjs +17 -0
- package/lib/telemetry.mjs +4 -1
- package/package.json +1 -1
package/lib/pull.mjs
ADDED
|
@@ -0,0 +1,605 @@
|
|
|
1
|
+
// `vatio pull`: a deployed agent, written back into the files it came from.
|
|
2
|
+
//
|
|
3
|
+
// A revision is a manifest, not a directory, and only half of it can be
|
|
4
|
+
// written back as it is. A tool keeps its whole file (`source`), byte for
|
|
5
|
+
// byte, so tools/KEY.yml is that text. vatio.yml keeps neither its comments
|
|
6
|
+
// nor its key order, and the deployed `auth:` holds the key itself where the
|
|
7
|
+
// file names a path. So vatio.yml is never regenerated: the file already on
|
|
8
|
+
// disk is edited, inside its `agent:` block only, and only at the keys whose
|
|
9
|
+
// value changed. Everything else in it -- `workspace:`, `auth:`, every
|
|
10
|
+
// comment, a hand-wrapped paragraph nobody edited -- stays byte for byte.
|
|
11
|
+
//
|
|
12
|
+
// Nothing here writes. planPull reads the directory and says what each file
|
|
13
|
+
// would become, so a deployment that cannot be written back fails before any
|
|
14
|
+
// file is touched, and the command decides whether to write at all.
|
|
15
|
+
|
|
16
|
+
import { existsSync, readdirSync, readFileSync } from "node:fs";
|
|
17
|
+
import { join } from "node:path";
|
|
18
|
+
import { isDeepStrictEqual } from "node:util";
|
|
19
|
+
import { Document, Pair, Scalar, YAMLMap, YAMLSeq, isMap, isScalar, isSeq, parseDocument } from "yaml";
|
|
20
|
+
|
|
21
|
+
import { MANIFEST_FILE } from "./workspace.mjs";
|
|
22
|
+
|
|
23
|
+
export class PullError extends Error {
|
|
24
|
+
constructor(message) {
|
|
25
|
+
super(message);
|
|
26
|
+
this.name = "PullError";
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
// The order a person reads an agent in (RevisionFiles::AGENT_KEY_ORDER on the
|
|
31
|
+
// platform). A key the file does not have yet goes in at its place in it.
|
|
32
|
+
const AGENT_KEY_ORDER = ["name", "instructions", "personality", "tools", "knowledge", "links"];
|
|
33
|
+
// What the platform drops from `agent:` on the way in, so a deployed agent
|
|
34
|
+
// never has them: the file's to keep, not a deletion to mirror.
|
|
35
|
+
const LOCAL_ONLY_KEYS = new Set(["key", "hours", "handoff"]);
|
|
36
|
+
// On every deployed agent whether the file declared them or not. Written only
|
|
37
|
+
// when they say something or the file already has them, so a vatio.yml that
|
|
38
|
+
// never mentioned them does not grow `knowledge: []`.
|
|
39
|
+
const ALWAYS_DEPLOYED = new Set(["knowledge", "links"]);
|
|
40
|
+
// VatioToolSpecParser::KEY_FORMAT. A key is a file name here, so one that
|
|
41
|
+
// does not match is never joined to a path.
|
|
42
|
+
const TOOL_KEY_FORMAT = /^[a-z0-9]+(?:[-_][a-z0-9]+)*$/;
|
|
43
|
+
const TOOL_FILE = /^(.+)\.(yml|yaml)$/;
|
|
44
|
+
// Where a rewritten `>` paragraph wraps, counting its indentation.
|
|
45
|
+
const FOLD_WIDTH = 80;
|
|
46
|
+
|
|
47
|
+
// [{ path, action: "created" | "changed" | "deleted", content }], one per file
|
|
48
|
+
// that differs from the deployment, paths relative to the workspace root.
|
|
49
|
+
export function planPull(root, manifest) {
|
|
50
|
+
const changes = [];
|
|
51
|
+
const before = readFileSync(join(root, MANIFEST_FILE), "utf8");
|
|
52
|
+
const after = mergeAgent(before, deployedAgent(manifest));
|
|
53
|
+
if (after !== before) changes.push({ path: MANIFEST_FILE, action: "changed", content: after });
|
|
54
|
+
return changes.concat(planTools(root, manifest?.tools));
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// The workspace's one agent, whatever slug an older revision stored it under.
|
|
58
|
+
function deployedAgent(manifest) {
|
|
59
|
+
const agents = manifest?.agents;
|
|
60
|
+
if (isPlainObject(agents)) {
|
|
61
|
+
const slug = Object.hasOwn(agents, "main") ? "main" : Object.keys(agents).sort()[0];
|
|
62
|
+
if (isPlainObject(agents[slug])) return agents[slug];
|
|
63
|
+
}
|
|
64
|
+
throw new PullError("the deployment has no agent to write back");
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
function planTools(root, tools) {
|
|
68
|
+
if (tools != null && !Array.isArray(tools)) throw new PullError("the deployment's tools are not a list");
|
|
69
|
+
|
|
70
|
+
const local = localToolFiles(join(root, "tools"));
|
|
71
|
+
const deployed = new Set();
|
|
72
|
+
const changes = [];
|
|
73
|
+
|
|
74
|
+
for (const tool of tools ?? []) {
|
|
75
|
+
const key = String(tool?.key ?? "");
|
|
76
|
+
if (!TOOL_KEY_FORMAT.test(key)) {
|
|
77
|
+
throw new PullError(`the deployment has a tool named ${JSON.stringify(key)}, which is not a tool file name`);
|
|
78
|
+
}
|
|
79
|
+
if (deployed.has(key)) throw new PullError(`the deployment lists tool ${key} twice`);
|
|
80
|
+
if (typeof tool.source !== "string") {
|
|
81
|
+
throw new PullError(`the deployment does not hold the file of tool ${key}, so it cannot be written back`);
|
|
82
|
+
}
|
|
83
|
+
deployed.add(key);
|
|
84
|
+
|
|
85
|
+
// tools/KEY.yaml stays .yaml: renaming it would be a change nobody made.
|
|
86
|
+
const names = local.get(key) ?? [];
|
|
87
|
+
const name = names.length > 0 && !names.includes(`${key}.yml`) ? names[0] : `${key}.yml`;
|
|
88
|
+
const path = `tools/${name}`;
|
|
89
|
+
const current = names.includes(name) ? readFileSync(join(root, path), "utf8") : null;
|
|
90
|
+
if (current === tool.source) continue;
|
|
91
|
+
changes.push({ path, action: current === null ? "created" : "changed", content: tool.source });
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
for (const [key, names] of local) {
|
|
95
|
+
if (deployed.has(key)) continue;
|
|
96
|
+
for (const name of names) changes.push({ path: `tools/${name}`, action: "deleted" });
|
|
97
|
+
}
|
|
98
|
+
return changes;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// key -> its file names in tools/: what the platform reads as tools
|
|
102
|
+
// (tools/*.{yml,yaml}), and nothing else in the directory.
|
|
103
|
+
function localToolFiles(dir) {
|
|
104
|
+
const files = new Map();
|
|
105
|
+
if (!existsSync(dir)) return files;
|
|
106
|
+
for (const entry of readdirSync(dir, { withFileTypes: true })) {
|
|
107
|
+
const match = TOOL_FILE.exec(entry.name);
|
|
108
|
+
if (!entry.isFile() || entry.name.startsWith(".") || !match) continue;
|
|
109
|
+
files.set(match[1], [...(files.get(match[1]) ?? []), entry.name].sort());
|
|
110
|
+
}
|
|
111
|
+
return files;
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// vatio.yml's text with its `agent:` block saying what `agent` says. The same
|
|
115
|
+
// text, unchanged, when it already does.
|
|
116
|
+
export function mergeAgent(source, agent) {
|
|
117
|
+
const doc = parseDocument(source);
|
|
118
|
+
if (doc.errors.length > 0) throw new PullError(`${MANIFEST_FILE}: ${doc.errors[0].message}`);
|
|
119
|
+
if (!isMap(doc.contents)) throw new PullError(`${MANIFEST_FILE} must be a mapping`);
|
|
120
|
+
|
|
121
|
+
const local = doc.toJS()?.agent;
|
|
122
|
+
const plan = agentPlan(isPlainObject(local) ? local : {}, agent);
|
|
123
|
+
if (plan.set.size === 0 && plan.remove.length === 0 && isPlainObject(local)) return source;
|
|
124
|
+
|
|
125
|
+
// Edited in place first. If that ever reads back as anything but the
|
|
126
|
+
// intended agent, yaml writes the whole agent block instead, and then the
|
|
127
|
+
// whole file: comments survive both, though a hand-wrapped `>` paragraph is
|
|
128
|
+
// wrapped again.
|
|
129
|
+
for (const write of [spliced, rewrittenAgent, rewritten]) {
|
|
130
|
+
let text = null;
|
|
131
|
+
try {
|
|
132
|
+
text = write(source, plan);
|
|
133
|
+
} catch {
|
|
134
|
+
text = null;
|
|
135
|
+
}
|
|
136
|
+
if (text !== null && says(text, source, plan.expected)) return text;
|
|
137
|
+
}
|
|
138
|
+
throw new PullError(`could not rewrite the agent: block of ${MANIFEST_FILE} safely; it is unchanged`);
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Which keys of the file's agent change, which go, and the agent it should
|
|
142
|
+
// read as afterwards.
|
|
143
|
+
function agentPlan(local, server) {
|
|
144
|
+
const order = [
|
|
145
|
+
...AGENT_KEY_ORDER.filter((key) => Object.hasOwn(server, key)),
|
|
146
|
+
...Object.keys(server).filter((key) => !AGENT_KEY_ORDER.includes(key) && key !== "key")
|
|
147
|
+
];
|
|
148
|
+
const set = new Map();
|
|
149
|
+
for (const key of order) {
|
|
150
|
+
const has = Object.hasOwn(local, key);
|
|
151
|
+
const value = presented(key, server[key], has ? local[key] : undefined);
|
|
152
|
+
if (!has && ALWAYS_DEPLOYED.has(key) && isEmpty(value)) continue;
|
|
153
|
+
if (has && isDeepStrictEqual(value, local[key])) continue;
|
|
154
|
+
set.set(key, value);
|
|
155
|
+
}
|
|
156
|
+
const remove = Object.keys(local).filter((key) => !Object.hasOwn(server, key) && !LOCAL_ONLY_KEYS.has(key));
|
|
157
|
+
|
|
158
|
+
const expected = { ...local };
|
|
159
|
+
for (const key of remove) delete expected[key];
|
|
160
|
+
for (const [key, value] of set) expected[key] = value;
|
|
161
|
+
return { order, set, remove, expected };
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
// The deployed value in the form the file already says it, when the two mean
|
|
165
|
+
// the same: the platform stores `knowledge: docs` as ["docs"], and a bare url
|
|
166
|
+
// under `links:` as { url }.
|
|
167
|
+
function presented(key, value, mine) {
|
|
168
|
+
if (key === "knowledge" && mine !== undefined && isDeepStrictEqual(knowledgeNames(mine), value)) return mine;
|
|
169
|
+
if (key === "links" && isPlainObject(value)) {
|
|
170
|
+
const links = {};
|
|
171
|
+
for (const [name, entry] of Object.entries(value)) {
|
|
172
|
+
const own = isPlainObject(mine) ? mine[name] : undefined;
|
|
173
|
+
links[name] = own !== undefined && isDeepStrictEqual(linkEntry(own), linkEntry(entry)) ? own : compactLink(entry);
|
|
174
|
+
}
|
|
175
|
+
return links;
|
|
176
|
+
}
|
|
177
|
+
return value;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// VatioManifestDirectory.knowledge_names_from.
|
|
181
|
+
function knowledgeNames(raw) {
|
|
182
|
+
if (raw == null) return [];
|
|
183
|
+
const names = (Array.isArray(raw) ? raw : [raw]).map((name) => String(name ?? "").trim()).filter(Boolean);
|
|
184
|
+
return [...new Set(names)];
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// VatioManifestDirectory.link_entry, without its validation.
|
|
188
|
+
function linkEntry(raw) {
|
|
189
|
+
const row = typeof raw === "string" ? { url: raw } : raw;
|
|
190
|
+
if (!isPlainObject(row)) return raw;
|
|
191
|
+
const entry = { url: String(row.url ?? "").trim() };
|
|
192
|
+
const when = String(row.when ?? "").trim();
|
|
193
|
+
if (when !== "") entry.when = when;
|
|
194
|
+
if (isPlainObject(row.values) && Object.keys(row.values).length > 0) entry.values = row.values;
|
|
195
|
+
return entry;
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
// A link that is only a url is written as one, the way people write it.
|
|
199
|
+
function compactLink(entry) {
|
|
200
|
+
return isPlainObject(entry) && Object.keys(entry).length === 1 && typeof entry.url === "string" ? entry.url : entry;
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// The source with every changed key's lines replaced, a removed key's lines
|
|
204
|
+
// (and the comment right above it) taken out, and a new key's lines put in
|
|
205
|
+
// after the key that comes before it -- and not one other byte moved. Null
|
|
206
|
+
// when the file is laid out in a way this cannot edit line by line.
|
|
207
|
+
function spliced(source, plan) {
|
|
208
|
+
const doc = parseDocument(source);
|
|
209
|
+
const lines = new Lines(source);
|
|
210
|
+
const pair = doc.contents.items.find((item) => keyOf(item) === "agent");
|
|
211
|
+
if (!pair || !isMap(pair.value) || pair.value.flow || pair.value.items.length === 0) {
|
|
212
|
+
return wholeAgent(source, doc, lines, pair, plan);
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
// Each key's lines, and where the key before it ends: a comment above a
|
|
216
|
+
// removed key goes with it, but never one inside the value before.
|
|
217
|
+
const regions = new Map();
|
|
218
|
+
let floor = lines.at(pair.key.range[0]);
|
|
219
|
+
for (const item of pair.value.items) {
|
|
220
|
+
const key = keyOf(item);
|
|
221
|
+
const region = lines.region(item);
|
|
222
|
+
if (key === null || !region) return null;
|
|
223
|
+
regions.set(key, { ...region, floor });
|
|
224
|
+
floor = region.last;
|
|
225
|
+
}
|
|
226
|
+
const column = [...regions.values()][0].column;
|
|
227
|
+
if (column === 0 || [...regions.values()].some((region) => region.column !== column)) return null;
|
|
228
|
+
const style = {
|
|
229
|
+
column,
|
|
230
|
+
indent: column,
|
|
231
|
+
indentSeq: seqIndented(pair.value, lines, column),
|
|
232
|
+
padFlow: flowPadding(source),
|
|
233
|
+
eol: lines.eol,
|
|
234
|
+
doc
|
|
235
|
+
};
|
|
236
|
+
|
|
237
|
+
const edits = [];
|
|
238
|
+
for (const key of plan.remove) {
|
|
239
|
+
const region = regions.get(key);
|
|
240
|
+
edits.push({ from: lines.start(lines.commentsAbove(region, column)), to: lines.end(region.last), text: "" });
|
|
241
|
+
}
|
|
242
|
+
for (const [key, value] of plan.set) {
|
|
243
|
+
const region = regions.get(key);
|
|
244
|
+
if (region) {
|
|
245
|
+
edits.push({
|
|
246
|
+
from: lines.start(region.first),
|
|
247
|
+
to: lines.end(region.last),
|
|
248
|
+
text: renderPair(key, value, region.pair.value, { ...style, old: { lines, region } })
|
|
249
|
+
});
|
|
250
|
+
continue;
|
|
251
|
+
}
|
|
252
|
+
const at = insertionPoint(key, plan, regions, lines, column);
|
|
253
|
+
edits.push({ from: at, to: at, text: renderPair(key, value, null, style) });
|
|
254
|
+
}
|
|
255
|
+
return applyEdits(source, edits, lines.eol);
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
// After the closest key before this one in reading order that the file keeps;
|
|
259
|
+
// before the first key when there is none.
|
|
260
|
+
function insertionPoint(key, plan, regions, lines, column) {
|
|
261
|
+
const before = plan.order.slice(0, plan.order.indexOf(key)).reverse();
|
|
262
|
+
for (const anchor of before) {
|
|
263
|
+
if (regions.has(anchor) && !plan.remove.includes(anchor)) return lines.end(regions.get(anchor).last);
|
|
264
|
+
}
|
|
265
|
+
return lines.start(lines.commentsAbove([...regions.values()][0], column));
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
// A file with no `agent:` block, or one that is not an indented mapping (`agent:
|
|
269
|
+
// {name: x}`, a bare `agent:`), gets the whole block written out.
|
|
270
|
+
function wholeAgent(source, doc, lines, pair, plan) {
|
|
271
|
+
const agent = orderedAgent(plan);
|
|
272
|
+
const style = { column: 0, indent: 2, indentSeq: true, padFlow: flowPadding(source), eol: lines.eol, doc };
|
|
273
|
+
const text = renderPair("agent", agent, pair?.value ?? null, style, { block: true });
|
|
274
|
+
if (pair) {
|
|
275
|
+
const region = lines.region(pair);
|
|
276
|
+
if (!region || region.column !== 0) return null;
|
|
277
|
+
return applyEdits(source, [{ from: lines.start(region.first), to: lines.end(region.last), text }], lines.eol);
|
|
278
|
+
}
|
|
279
|
+
const workspace = doc.contents.items.find((item) => keyOf(item) === "workspace");
|
|
280
|
+
const region = workspace && lines.region(workspace);
|
|
281
|
+
const at = region && region.column === 0 ? lines.end(region.last) : source.length;
|
|
282
|
+
return applyEdits(source, [{ from: at, to: at, text: `${lines.eol}${text}` }], lines.eol);
|
|
283
|
+
}
|
|
284
|
+
|
|
285
|
+
function orderedAgent(plan) {
|
|
286
|
+
const agent = {};
|
|
287
|
+
for (const key of plan.order) if (Object.hasOwn(plan.expected, key)) agent[key] = plan.expected[key];
|
|
288
|
+
for (const [key, value] of Object.entries(plan.expected)) if (!Object.hasOwn(agent, key)) agent[key] = value;
|
|
289
|
+
return agent;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// The fallback: the same changes made to the parsed document, and yaml writing
|
|
293
|
+
// all of it back out.
|
|
294
|
+
function rewritten(source, plan) {
|
|
295
|
+
const doc = parseDocument(source);
|
|
296
|
+
const pair = doc.contents.items.find((item) => keyOf(item) === "agent");
|
|
297
|
+
if (!pair || !isMap(pair.value)) {
|
|
298
|
+
const node = updateNode(pair?.value ?? null, orderedAgent(plan), doc);
|
|
299
|
+
if (isMap(node)) node.flow = false;
|
|
300
|
+
if (pair) pair.value = node;
|
|
301
|
+
else doc.contents.items.push(new Pair(new Scalar("agent"), node));
|
|
302
|
+
} else {
|
|
303
|
+
const map = pair.value;
|
|
304
|
+
map.items = map.items.filter((item) => !plan.remove.includes(keyOf(item)));
|
|
305
|
+
for (const [key, value] of plan.set) {
|
|
306
|
+
const item = map.items.find((candidate) => keyOf(candidate) === key);
|
|
307
|
+
if (item) item.value = updateNode(item.value, value, doc);
|
|
308
|
+
else map.items.push(new Pair(new Scalar(key), fresh(value)));
|
|
309
|
+
}
|
|
310
|
+
}
|
|
311
|
+
const text = doc.toString({ flowCollectionPadding: flowPadding(source) });
|
|
312
|
+
return source.includes("\r\n") ? text.replace(/\r?\n/g, "\r\n") : text;
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// That rewrite's `agent:` block in place of the file's, so what is outside it
|
|
316
|
+
// still does not move.
|
|
317
|
+
function rewrittenAgent(source, plan) {
|
|
318
|
+
const text = rewritten(source, plan);
|
|
319
|
+
const regionIn = (body) => {
|
|
320
|
+
const pair = parseDocument(body).contents?.items?.find((item) => keyOf(item) === "agent");
|
|
321
|
+
const lines = new Lines(body);
|
|
322
|
+
const region = pair && lines.region(pair);
|
|
323
|
+
return region && region.column === 0 ? { from: lines.start(region.first), to: lines.end(region.last) } : null;
|
|
324
|
+
};
|
|
325
|
+
const was = regionIn(source);
|
|
326
|
+
const now = regionIn(text);
|
|
327
|
+
if (!was || !now) return null;
|
|
328
|
+
return applyEdits(source, [{ from: was.from, to: was.to, text: text.slice(now.from, now.to) }], new Lines(source).eol);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
// Whether `text` reads as the original file with only its agent changed, and
|
|
332
|
+
// changed into `expected`.
|
|
333
|
+
function says(text, source, expected) {
|
|
334
|
+
const after = parseDocument(text);
|
|
335
|
+
if (after.errors.length > 0) return false;
|
|
336
|
+
const now = after.toJS();
|
|
337
|
+
const was = parseDocument(source).toJS();
|
|
338
|
+
if (!isPlainObject(now) || !isDeepStrictEqual(now.agent, expected)) return false;
|
|
339
|
+
const rest = (data) => Object.entries(data).filter(([key]) => key !== "agent");
|
|
340
|
+
return isDeepStrictEqual(rest(now), rest(was));
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
// `key: value` as the lines that go into the file at `column`, in the file's
|
|
344
|
+
// own indentation. The node it was is reused where the value did not change,
|
|
345
|
+
// so what is left of it keeps its comments and its style.
|
|
346
|
+
function renderPair(key, value, old, style, { block = false } = {}) {
|
|
347
|
+
const node = updateNode(old, value, style.doc);
|
|
348
|
+
if (block && isMap(node)) node.flow = false;
|
|
349
|
+
// Without indentSeq yaml counts the `- ` as two columns of the indentation,
|
|
350
|
+
// so a list written flush under its key needs a step of exactly two.
|
|
351
|
+
const flush = isSeq(node) && !node.flow && !style.indentSeq;
|
|
352
|
+
const indent = flush ? 2 : (nestedIndent(old, style) ?? style.indent);
|
|
353
|
+
const folded = isScalar(node) && node.type === Scalar.BLOCK_FOLDED;
|
|
354
|
+
|
|
355
|
+
const map = new YAMLMap();
|
|
356
|
+
map.items.push(new Pair(new Scalar(key), node));
|
|
357
|
+
const mini = new Document();
|
|
358
|
+
mini.contents = map;
|
|
359
|
+
let rendered = mini
|
|
360
|
+
.toString({
|
|
361
|
+
indent,
|
|
362
|
+
indentSeq: style.indentSeq,
|
|
363
|
+
flowCollectionPadding: style.padFlow,
|
|
364
|
+
lineWidth: folded ? Math.max(FOLD_WIDTH - style.column, 40) : 0
|
|
365
|
+
})
|
|
366
|
+
.replace(/\n$/, "")
|
|
367
|
+
.split("\n");
|
|
368
|
+
if (folded && style.old && isScalar(old) && old.type === Scalar.BLOCK_FOLDED) {
|
|
369
|
+
rendered = keepParagraphs(rendered, node.value, style.old, style.column, indent);
|
|
370
|
+
}
|
|
371
|
+
const pad = " ".repeat(style.column);
|
|
372
|
+
return rendered.map((line) => (line === "" ? "" : `${pad}${line}`)).join(style.eol) + style.eol;
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
// How far the old value's own lines were indented past its key, so a
|
|
376
|
+
// rewritten block lines up with what the file did.
|
|
377
|
+
function nestedIndent(old, style) {
|
|
378
|
+
if (!style.old || !old?.range || (isSeq(old) && !style.indentSeq)) return null;
|
|
379
|
+
const { lines, region } = style.old;
|
|
380
|
+
// A block scalar's first line is its header; its body starts below.
|
|
381
|
+
for (let line = lines.at(old.range[0]) + (isScalar(old) ? 1 : 0); line <= region.last; line += 1) {
|
|
382
|
+
if (lines.blank(line) || line === region.first) continue;
|
|
383
|
+
const step = indentOf(lines.line(line)) - style.column;
|
|
384
|
+
return step > 0 ? step : null;
|
|
385
|
+
}
|
|
386
|
+
return null;
|
|
387
|
+
}
|
|
388
|
+
|
|
389
|
+
// A rewritten `>` scalar wraps every paragraph again, so an edit to one
|
|
390
|
+
// sentence would show as the whole instructions changing. A paragraph whose
|
|
391
|
+
// text did not change gets the file's own lines back instead.
|
|
392
|
+
//
|
|
393
|
+
// `rendered` is relative to the key's column, its body `indent` deep; the
|
|
394
|
+
// file's lines are where they are, `column` further in.
|
|
395
|
+
function keepParagraphs(rendered, value, { lines, region }, column, indent) {
|
|
396
|
+
const header = rendered[0];
|
|
397
|
+
if (!/: >[-+]?$/.test(header)) return rendered;
|
|
398
|
+
const pieces = value.replace(/\n+$/, "").split("\n");
|
|
399
|
+
const groups = paragraphs(rendered.slice(1), indent);
|
|
400
|
+
if (!groups || groups.length !== pieces.length || groups.some((group, i) => fold(group) !== pieces[i])) return rendered;
|
|
401
|
+
|
|
402
|
+
const body = [];
|
|
403
|
+
for (let line = region.first + 1; line <= region.last; line += 1) body.push(lines.line(line));
|
|
404
|
+
const kept = new Map();
|
|
405
|
+
for (const group of paragraphs(body, column + indent) ?? []) {
|
|
406
|
+
kept.set(fold(group), group.map((line) => line.slice(column)));
|
|
407
|
+
}
|
|
408
|
+
return [header, ...groups.flatMap((group, i) => [...(i > 0 ? [""] : []), ...(kept.get(pieces[i]) ?? group)])];
|
|
409
|
+
}
|
|
410
|
+
|
|
411
|
+
// Lines split at single blank lines into paragraphs, each a run of plain text
|
|
412
|
+
// lines at exactly `column`: anything else (a more-indented line, trailing
|
|
413
|
+
// space, two blank lines in a row) is null, because folding it is not simply
|
|
414
|
+
// joining with spaces.
|
|
415
|
+
function paragraphs(body, column) {
|
|
416
|
+
const groups = [[]];
|
|
417
|
+
for (const line of body) {
|
|
418
|
+
if (line === "" || line.trim() === "") {
|
|
419
|
+
if (groups.at(-1).length === 0) return null;
|
|
420
|
+
groups.push([]);
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
if (indentOf(line) !== column || /\s$/.test(line) || line.includes("\t")) return null;
|
|
424
|
+
groups.at(-1).push(line);
|
|
425
|
+
}
|
|
426
|
+
if (groups.at(-1).length === 0) return null;
|
|
427
|
+
return groups;
|
|
428
|
+
}
|
|
429
|
+
|
|
430
|
+
function fold(group) {
|
|
431
|
+
return group.map((line) => line.trim()).join(" ");
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
// The value as a node, reusing `old` and whatever inside it is unchanged.
|
|
435
|
+
function updateNode(old, value, doc) {
|
|
436
|
+
if (Array.isArray(value)) {
|
|
437
|
+
if (!isSeq(old)) return fresh(value);
|
|
438
|
+
const pool = [...old.items];
|
|
439
|
+
old.items = value.map((item) => {
|
|
440
|
+
const index = pool.findIndex((node) => isDeepStrictEqual(jsOf(node, doc), item));
|
|
441
|
+
return index === -1 ? fresh(item) : pool.splice(index, 1)[0];
|
|
442
|
+
});
|
|
443
|
+
return old;
|
|
444
|
+
}
|
|
445
|
+
if (isPlainObject(value)) {
|
|
446
|
+
if (!isMap(old) || old.items.some((item) => keyOf(item) === null || keyOf(item) === "<<")) return fresh(value);
|
|
447
|
+
const kept = old.items.filter((item) => Object.hasOwn(value, keyOf(item)));
|
|
448
|
+
for (const item of kept) item.value = updateNode(item.value, value[keyOf(item)], doc);
|
|
449
|
+
const added = Object.keys(value)
|
|
450
|
+
.filter((key) => !kept.some((item) => keyOf(item) === key))
|
|
451
|
+
.map((key) => new Pair(new Scalar(key), fresh(value[key])));
|
|
452
|
+
old.items = [...kept, ...added];
|
|
453
|
+
return old;
|
|
454
|
+
}
|
|
455
|
+
|
|
456
|
+
if (isScalar(old) && isDeepStrictEqual(old.value, value)) return old;
|
|
457
|
+
const node = new Scalar(value);
|
|
458
|
+
if (typeof value === "string" && value !== "") {
|
|
459
|
+
const was = isScalar(old) && typeof old.value === "string" ? old.type : null;
|
|
460
|
+
if (was === Scalar.BLOCK_LITERAL || was === Scalar.BLOCK_FOLDED) node.type = was;
|
|
461
|
+
else if (value.includes("\n")) node.type = Scalar.BLOCK_LITERAL;
|
|
462
|
+
else if (was) node.type = was;
|
|
463
|
+
}
|
|
464
|
+
if (isScalar(old) && old.comment) node.comment = old.comment;
|
|
465
|
+
return node;
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
// A node for a value the file never had: a multi-line string as a `|` block,
|
|
469
|
+
// never as one long quoted line.
|
|
470
|
+
function fresh(value) {
|
|
471
|
+
if (Array.isArray(value)) {
|
|
472
|
+
const seq = new YAMLSeq();
|
|
473
|
+
seq.items = value.map(fresh);
|
|
474
|
+
return seq;
|
|
475
|
+
}
|
|
476
|
+
if (isPlainObject(value)) {
|
|
477
|
+
const map = new YAMLMap();
|
|
478
|
+
map.items = Object.entries(value).map(([key, item]) => new Pair(new Scalar(key), fresh(item)));
|
|
479
|
+
return map;
|
|
480
|
+
}
|
|
481
|
+
const node = new Scalar(value);
|
|
482
|
+
if (typeof value === "string" && value.includes("\n")) node.type = Scalar.BLOCK_LITERAL;
|
|
483
|
+
return node;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
// Whether a block list under a key is indented past it (`tools:\n - a`), the
|
|
487
|
+
// way this file writes it. yaml's default when the agent has no block list.
|
|
488
|
+
function seqIndented(map, lines, column) {
|
|
489
|
+
for (const item of map.items) {
|
|
490
|
+
const seq = item.value;
|
|
491
|
+
if (!isSeq(seq) || seq.flow || seq.items.length === 0 || !seq.range) continue;
|
|
492
|
+
return seq.range[0] - lines.start(lines.at(seq.range[0])) > column;
|
|
493
|
+
}
|
|
494
|
+
return true;
|
|
495
|
+
}
|
|
496
|
+
|
|
497
|
+
// `[ a, b ]` or `[a, b]`, whichever the file writes; the second when it has
|
|
498
|
+
// no flow collection to go by.
|
|
499
|
+
function flowPadding(source) {
|
|
500
|
+
return /[[{] [^\s\]}]/.test(source);
|
|
501
|
+
}
|
|
502
|
+
|
|
503
|
+
function applyEdits(source, edits, eol) {
|
|
504
|
+
let text = "";
|
|
505
|
+
let cursor = 0;
|
|
506
|
+
for (const edit of [...edits].sort((a, b) => a.from - b.from)) {
|
|
507
|
+
if (edit.from < cursor && edit.to > edit.from) return null;
|
|
508
|
+
const gap = source.slice(cursor, edit.from);
|
|
509
|
+
text += gap;
|
|
510
|
+
// A file whose last line has no newline still ends that line before
|
|
511
|
+
// anything is added after it.
|
|
512
|
+
if (edit.text !== "" && edit.from === source.length && !(text === "" || text.endsWith("\n"))) text += eol;
|
|
513
|
+
text += edit.text;
|
|
514
|
+
cursor = Math.max(cursor, edit.to);
|
|
515
|
+
}
|
|
516
|
+
return text + source.slice(cursor);
|
|
517
|
+
}
|
|
518
|
+
|
|
519
|
+
// The source as lines, for edits that never split one.
|
|
520
|
+
class Lines {
|
|
521
|
+
constructor(text) {
|
|
522
|
+
this.text = text;
|
|
523
|
+
this.eol = text.includes("\r\n") ? "\r\n" : "\n";
|
|
524
|
+
this.starts = [0];
|
|
525
|
+
for (let i = 0; i < text.length; i += 1) if (text[i] === "\n") this.starts.push(i + 1);
|
|
526
|
+
}
|
|
527
|
+
|
|
528
|
+
at(offset) {
|
|
529
|
+
let low = 0;
|
|
530
|
+
let high = this.starts.length - 1;
|
|
531
|
+
while (low < high) {
|
|
532
|
+
const mid = Math.ceil((low + high) / 2);
|
|
533
|
+
if (this.starts[mid] <= offset) low = mid;
|
|
534
|
+
else high = mid - 1;
|
|
535
|
+
}
|
|
536
|
+
return low;
|
|
537
|
+
}
|
|
538
|
+
|
|
539
|
+
start(line) {
|
|
540
|
+
return this.starts[line];
|
|
541
|
+
}
|
|
542
|
+
|
|
543
|
+
// Just past the line's newline.
|
|
544
|
+
end(line) {
|
|
545
|
+
return line + 1 < this.starts.length ? this.starts[line + 1] : this.text.length;
|
|
546
|
+
}
|
|
547
|
+
|
|
548
|
+
line(line) {
|
|
549
|
+
return this.text.slice(this.start(line), this.end(line)).replace(/\r?\n$/, "");
|
|
550
|
+
}
|
|
551
|
+
|
|
552
|
+
blank(line) {
|
|
553
|
+
return this.line(line).trim() === "";
|
|
554
|
+
}
|
|
555
|
+
|
|
556
|
+
// The lines a key and its value take, from the key's line to the value's
|
|
557
|
+
// last, blank lines after it left out: those separate it from the next key.
|
|
558
|
+
// Null when the key does not start its line.
|
|
559
|
+
region(pair) {
|
|
560
|
+
if (!pair.key?.range) return null;
|
|
561
|
+
const keyStart = pair.key.range[0];
|
|
562
|
+
const first = this.at(keyStart);
|
|
563
|
+
if (this.text.slice(this.start(first), keyStart).trim() !== "") return null;
|
|
564
|
+
const end = Math.max(pair.key.range[1], pair.value?.range?.[1] ?? 0);
|
|
565
|
+
let last = this.at(Math.max(end - 1, keyStart));
|
|
566
|
+
while (last > first && this.blank(last)) last -= 1;
|
|
567
|
+
return { first, last, column: keyStart - this.start(first), pair };
|
|
568
|
+
}
|
|
569
|
+
|
|
570
|
+
// The first of the comment lines right above a key's region, indented as
|
|
571
|
+
// the key is and below its `floor` line: what goes with the key when it is
|
|
572
|
+
// removed. One indented further is about the value before it.
|
|
573
|
+
commentsAbove({ first: line, floor }, column) {
|
|
574
|
+
let first = line;
|
|
575
|
+
while (first - 1 > floor) {
|
|
576
|
+
const text = this.line(first - 1);
|
|
577
|
+
if (!/^\s*#/.test(text) || indentOf(text) !== column) break;
|
|
578
|
+
first -= 1;
|
|
579
|
+
}
|
|
580
|
+
return first;
|
|
581
|
+
}
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
function keyOf(pair) {
|
|
585
|
+
return isScalar(pair?.key) ? String(pair.key.value) : null;
|
|
586
|
+
}
|
|
587
|
+
|
|
588
|
+
function jsOf(node, doc) {
|
|
589
|
+
return node == null ? null : node.toJS(doc);
|
|
590
|
+
}
|
|
591
|
+
|
|
592
|
+
function indentOf(line) {
|
|
593
|
+
return line.length - line.trimStart().length;
|
|
594
|
+
}
|
|
595
|
+
|
|
596
|
+
function isEmpty(value) {
|
|
597
|
+
if (value == null) return true;
|
|
598
|
+
if (Array.isArray(value)) return value.length === 0;
|
|
599
|
+
if (isPlainObject(value)) return Object.keys(value).length === 0;
|
|
600
|
+
return false;
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
function isPlainObject(value) {
|
|
604
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
605
|
+
}
|
package/lib/support.mjs
CHANGED
|
@@ -86,6 +86,23 @@ export function detectGitBranch(cwd) {
|
|
|
86
86
|
return git(cwd, ["symbolic-ref", "--short", "-q", "HEAD"]);
|
|
87
87
|
}
|
|
88
88
|
|
|
89
|
+
// What `git status` says is uncommitted under `paths` (relative to `cwd`), one
|
|
90
|
+
// porcelain line each, untracked files included: [] when nothing is. Null
|
|
91
|
+
// outside a work tree or without git, which is not the same as clean.
|
|
92
|
+
export function gitChanges(cwd, paths) {
|
|
93
|
+
if (git(cwd, ["rev-parse", "--is-inside-work-tree"]) !== "true") return null;
|
|
94
|
+
try {
|
|
95
|
+
const output = execFileSync("git", ["status", "--porcelain", "--", ...paths], {
|
|
96
|
+
cwd,
|
|
97
|
+
encoding: "utf8",
|
|
98
|
+
stdio: ["ignore", "pipe", "ignore"]
|
|
99
|
+
});
|
|
100
|
+
return output.split("\n").filter((line) => line.trim() !== "");
|
|
101
|
+
} catch {
|
|
102
|
+
return null;
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
|
|
89
106
|
function git(cwd, args) {
|
|
90
107
|
try {
|
|
91
108
|
return execFileSync("git", args, { cwd, encoding: "utf8", stdio: ["ignore", "pipe", "ignore"] }).trim() || null;
|
package/lib/telemetry.mjs
CHANGED
|
@@ -57,7 +57,10 @@ export function codingAgent() {
|
|
|
57
57
|
if (env.VATIO_AGENT) return String(env.VATIO_AGENT).slice(0, 64);
|
|
58
58
|
if (env.CLAUDECODE) return "claude-code";
|
|
59
59
|
if (env.CURSOR_AGENT || env.CURSOR_TRACE_ID) return "cursor";
|
|
60
|
-
|
|
60
|
+
// CODEX_THREAD_ID and CODEX_SESSION_ID are on every command Codex runs, on
|
|
61
|
+
// every platform; the other two are not on Windows, where a whole session
|
|
62
|
+
// of Codex's once reported as "unknown".
|
|
63
|
+
if (env.CODEX_THREAD_ID || env.CODEX_SESSION_ID || env.CODEX_SANDBOX || env.CODEX_MANAGED_BY_NPM) return "codex";
|
|
61
64
|
if (env.GEMINI_CLI) return "gemini-cli";
|
|
62
65
|
if (env.CI) return "ci";
|
|
63
66
|
return process.stdout.isTTY ? "human" : "unknown";
|