@vatio-ai/cli 0.88.0 → 0.89.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/commands/deploy.mjs +161 -20
- package/lib/config.mjs +33 -0
- package/lib/help.mjs +8 -2
- package/lib/http.mjs +12 -0
- package/lib/pull.mjs +605 -0
- package/lib/support.mjs +17 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -71,6 +71,7 @@ login | logout Authorize this machine in a browser, or revoke and f
|
|
|
71
71
|
doctor Node, config, workspace and token status
|
|
72
72
|
|
|
73
73
|
push Validate the workspace, then update preview
|
|
74
|
+
pull [--env NAME] Write the deployed agent back into vatio.yml and tools/
|
|
74
75
|
publish Take preview live, once `vatio test` has tested it
|
|
75
76
|
diff [--env NAME] What this directory would change
|
|
76
77
|
status Preview and live deployment state
|
package/bin/vatio.mjs
CHANGED
|
@@ -10,7 +10,7 @@ import { UserError, fail } from "../lib/support.mjs";
|
|
|
10
10
|
import { helpText } from "../lib/help.mjs";
|
|
11
11
|
import { login, logout } from "../lib/commands/auth.mjs";
|
|
12
12
|
import { configCommand, doctor, init, version, whoami } from "../lib/commands/misc.mjs";
|
|
13
|
-
import { diff, publish, push, rollback, status } from "../lib/commands/deploy.mjs";
|
|
13
|
+
import { diff, publish, pull, push, rollback, status } from "../lib/commands/deploy.mjs";
|
|
14
14
|
import { env } from "../lib/commands/env.mjs";
|
|
15
15
|
import { keys, secrets, tokens } from "../lib/commands/workspace-admin.mjs";
|
|
16
16
|
import { templates, webhooks } from "../lib/commands/webhooks.mjs";
|
|
@@ -29,9 +29,6 @@ import { beginRun, isCodingAgent, reportRun, telemetryEnabled } from "../lib/tel
|
|
|
29
29
|
// Commands that existed and are gone, answered by name rather than as
|
|
30
30
|
// unknown: removed from the product, or what npm does now.
|
|
31
31
|
const RETIRED = {
|
|
32
|
-
pull: "was removed: a deployed manifest only ever comes from a push, so it could never\n" +
|
|
33
|
-
"hand back anything your repository did not already have. Use `vatio diff --env live`,\n" +
|
|
34
|
-
"or read the manifest at GET /api/developer/v1/:slug/manifest.",
|
|
35
32
|
issue: "was removed: report a problem on the public repository it belongs to --\n" +
|
|
36
33
|
"https://github.com/vatio-ai/cli/issues (or sdk, ios, ruby under github.com/vatio-ai).",
|
|
37
34
|
widget: "was removed: `vatio env` prints the snippet and its token, and the look is edited in\n" +
|
|
@@ -81,6 +78,8 @@ async function run(command, args) {
|
|
|
81
78
|
return await init(config, args);
|
|
82
79
|
case "push":
|
|
83
80
|
return await push(config, args);
|
|
81
|
+
case "pull":
|
|
82
|
+
return await pull(config, args);
|
|
84
83
|
case "publish":
|
|
85
84
|
return await publish(config, args);
|
|
86
85
|
case "rollback":
|
package/lib/api-client.mjs
CHANGED
|
@@ -39,13 +39,26 @@ export class ApiClient extends HttpClient {
|
|
|
39
39
|
return this.post("/check", body);
|
|
40
40
|
}
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
// `baseDeploymentId`: the deployment the files were last made to match. The
|
|
43
|
+
// platform refuses the push (409 preview_edited_outside) while preview holds
|
|
44
|
+
// edits made outside the files since then, or any at all without one,
|
|
45
|
+
// unless `force`.
|
|
46
|
+
pushPreview({
|
|
47
|
+
manifest,
|
|
48
|
+
gitSha = null,
|
|
49
|
+
gitBranch = null,
|
|
50
|
+
createdBy = null,
|
|
51
|
+
environment = null,
|
|
52
|
+
force = false,
|
|
53
|
+
baseDeploymentId = null
|
|
54
|
+
}) {
|
|
43
55
|
const body = { manifest };
|
|
44
56
|
if (gitSha) body.git_sha = gitSha;
|
|
45
57
|
if (gitBranch) body.git_branch = gitBranch;
|
|
46
58
|
if (createdBy) body.created_by = createdBy;
|
|
47
59
|
if (environment) body.environment = environment;
|
|
48
60
|
if (force) body.force = true;
|
|
61
|
+
if (Number.isInteger(baseDeploymentId)) body.base_deployment_id = baseDeploymentId;
|
|
49
62
|
return this.put("/push", body);
|
|
50
63
|
}
|
|
51
64
|
|
package/lib/commands/deploy.mjs
CHANGED
|
@@ -1,24 +1,35 @@
|
|
|
1
|
-
// push / publish / rollback / status / diff.
|
|
1
|
+
// push / pull / publish / rollback / status / diff.
|
|
2
2
|
//
|
|
3
3
|
// Every one of them is HTTP. The workspace is parsed by the platform
|
|
4
4
|
// (POST /deploy/check), so what is left here is: read the directory, send it,
|
|
5
5
|
// print what came back.
|
|
6
6
|
//
|
|
7
|
-
// `pull` is
|
|
8
|
-
//
|
|
9
|
-
//
|
|
10
|
-
//
|
|
11
|
-
//
|
|
12
|
-
//
|
|
13
|
-
//
|
|
7
|
+
// `pull` is the exception, the one command that writes over the developer's
|
|
8
|
+
// own files. It is back because the files stopped being the only writer: from
|
|
9
|
+
// the Vatio MCP, in Claude or ChatGPT, people edit the agent itself, and each
|
|
10
|
+
// edit lands in preview as a deployment of its own. What it writes is narrow
|
|
11
|
+
// on purpose (lib/pull.mjs): each tools/KEY.yml exactly as the platform holds
|
|
12
|
+
// it, and only the `agent:` block of the vatio.yml already on disk, key by
|
|
13
|
+
// key, because a revision keeps neither that file's comments nor `auth:` the
|
|
14
|
+
// way the file spells it. And it wants a clean git tree, so that git, not this
|
|
15
|
+
// CLI, is what gives back whatever it replaced.
|
|
16
|
+
//
|
|
17
|
+
// The two directions meet in the base deployment: the one the files were last
|
|
18
|
+
// made to match, by a push or a pull, kept in the config file. A push sends
|
|
19
|
+
// it, so the platform can refuse to overwrite edits the files have never seen.
|
|
20
|
+
|
|
21
|
+
import { mkdirSync, rmSync, writeFileSync } from "node:fs";
|
|
22
|
+
import { dirname, join } from "node:path";
|
|
14
23
|
|
|
15
24
|
import { stringify as stringifyYaml } from "yaml";
|
|
16
25
|
|
|
17
26
|
import { buildBundle, BundleError } from "../bundle.mjs";
|
|
18
27
|
import { ApiClient, CliClient } from "../api-client.mjs";
|
|
19
|
-
import { NotFoundError, UnprocessableError } from "../http.mjs";
|
|
28
|
+
import { ConflictError, NotFoundError, UnprocessableError } from "../http.mjs";
|
|
29
|
+
import { MANIFEST_FILE } from "../workspace.mjs";
|
|
30
|
+
import { PullError, planPull } from "../pull.mjs";
|
|
20
31
|
import { deviceLogin } from "./auth.mjs";
|
|
21
|
-
import { detectGitBranch, detectGitSha, fail, paint, takeEnv, takeFlag, takeValue } from "../support.mjs";
|
|
32
|
+
import { detectGitBranch, detectGitSha, fail, gitChanges, paint, takeEnv, takeFlag, takeValue } from "../support.mjs";
|
|
22
33
|
|
|
23
34
|
// The first command that makes something exist on a server, so the first one
|
|
24
35
|
// that needs to know whose it is.
|
|
@@ -87,8 +98,9 @@ export async function push(config, args) {
|
|
|
87
98
|
// explicitly would make a bare `vatio push` and `vatio push --env preview`
|
|
88
99
|
// two different requests that only happen to agree today.
|
|
89
100
|
const environment = takeEnv(args, { fallback: null });
|
|
90
|
-
// Replaces an unpublished push from another branch,
|
|
91
|
-
// otherwise refuses
|
|
101
|
+
// Replaces an unpublished push from another branch, or edits made outside
|
|
102
|
+
// the files, which the platform otherwise refuses
|
|
103
|
+
// (preview_has_unpublished_push, preview_edited_outside).
|
|
92
104
|
const force = takeFlag(args, "--force");
|
|
93
105
|
const workspace = config.resolveWorkspaceRequired();
|
|
94
106
|
|
|
@@ -110,14 +122,24 @@ export async function push(config, args) {
|
|
|
110
122
|
if (!check.ok) fail(`push aborted: check failed (${asArray(check.errors).length} error(s))`);
|
|
111
123
|
|
|
112
124
|
const client = deployClient(config, workspace);
|
|
113
|
-
const
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
125
|
+
const baseUrl = config.resolveBaseUrl();
|
|
126
|
+
let payload;
|
|
127
|
+
try {
|
|
128
|
+
payload = await client.pushPreview({
|
|
129
|
+
manifest: check.manifest,
|
|
130
|
+
gitSha: detectGitSha(config.workspaceRootRequired()),
|
|
131
|
+
gitBranch: detectGitBranch(config.workspaceRootRequired()),
|
|
132
|
+
createdBy: process.env.USER ?? null,
|
|
133
|
+
environment,
|
|
134
|
+
force,
|
|
135
|
+
baseDeploymentId: config.baseDeploymentFor({ baseUrl, workspace })
|
|
136
|
+
});
|
|
137
|
+
} catch (error) {
|
|
138
|
+
if (error instanceof ConflictError && error.errorKey === "preview_edited_outside") fail(editedOutsideMessage(error));
|
|
139
|
+
throw error;
|
|
140
|
+
}
|
|
141
|
+
// What the files match now, so the next push is built on it.
|
|
142
|
+
config.rememberBaseDeployment({ baseUrl, workspace, deploymentId: payload.deployment_id });
|
|
121
143
|
|
|
122
144
|
const landedOn = payload.environment ?? "preview";
|
|
123
145
|
console.log(`Pushed deployment #${payload.deployment_id} to ${landedOn} (git_sha=${JSON.stringify(payload.git_sha ?? null)})`);
|
|
@@ -144,6 +166,114 @@ function printNotices(payload) {
|
|
|
144
166
|
}
|
|
145
167
|
}
|
|
146
168
|
|
|
169
|
+
// Refused because preview holds edits the files have never seen: which, and
|
|
170
|
+
// the two ways forward. The platform's message says all of it in one line,
|
|
171
|
+
// for releases that print only that; with the list to go by, this one says
|
|
172
|
+
// it a line per edit instead of repeating it.
|
|
173
|
+
function editedOutsideMessage(error) {
|
|
174
|
+
const edits = asArray(error.body.edits);
|
|
175
|
+
const count = edits.length === 1 ? "a change" : `${edits.length} changes`;
|
|
176
|
+
const lines = edits.length === 0 ? [error.message] : [`Preview has ${count} made outside your files that this push would overwrite:`];
|
|
177
|
+
for (const edit of edits) lines.push(` ${editLine(edit)}`);
|
|
178
|
+
const them = edits.length === 1 ? "it" : "them";
|
|
179
|
+
lines.push(
|
|
180
|
+
"",
|
|
181
|
+
`Run \`vatio pull\` to bring ${them} into your files, review with \`git diff\`, and push again.`,
|
|
182
|
+
`\`vatio push --force\` overwrites ${them} instead.`
|
|
183
|
+
);
|
|
184
|
+
return lines.join("\n");
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
// `#42 by ivan@x.cl via Claude, 2026-10-16T12:00:00Z: instructions, tool my_orders`
|
|
188
|
+
function editLine(edit) {
|
|
189
|
+
const who = edit?.created_by ? ` by ${edit.created_by}` : "";
|
|
190
|
+
const when = edit?.created_at ? `, ${edit.created_at}` : "";
|
|
191
|
+
const what = edit?.summary ? `: ${edit.summary}` : "";
|
|
192
|
+
return `#${edit?.deployment_id ?? "?"}${who}${when}${what}`;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
// The deployed agent, back in the files: what an edit made from the Vatio MCP
|
|
196
|
+
// changed, so the next push carries it rather than undoing it. Preview by
|
|
197
|
+
// default, since that is where those edits land.
|
|
198
|
+
export async function pull(config, args) {
|
|
199
|
+
const environment = takeEnv(args, { fallback: "preview" });
|
|
200
|
+
// Writes even over uncommitted changes, or outside git.
|
|
201
|
+
const force = takeFlag(args, "--force");
|
|
202
|
+
if (args.length > 0) fail(`vatio pull: unexpected ${JSON.stringify(args[0])} (it takes --env NAME and --force)`);
|
|
203
|
+
|
|
204
|
+
const workspace = config.resolveWorkspaceRequired();
|
|
205
|
+
const root = config.workspaceRootRequired();
|
|
206
|
+
const client = deployClient(config, workspace);
|
|
207
|
+
if (!force) refuseUnkeptChanges(root);
|
|
208
|
+
|
|
209
|
+
let payload;
|
|
210
|
+
try {
|
|
211
|
+
payload = await client.deployedManifest({ environment });
|
|
212
|
+
} catch (error) {
|
|
213
|
+
if (error instanceof NotFoundError) {
|
|
214
|
+
fail(`Nothing to pull: ${workspace} has no ${environment} deployment yet. \`vatio push\` makes one from your files.`);
|
|
215
|
+
}
|
|
216
|
+
throw error;
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
let changes;
|
|
220
|
+
try {
|
|
221
|
+
changes = planPull(root, payload.manifest);
|
|
222
|
+
} catch (error) {
|
|
223
|
+
if (error instanceof PullError) fail(`vatio pull: ${error.message}`);
|
|
224
|
+
throw error;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
const deployment = `deployment #${payload.deployment_id} (${payload.environment ?? environment})`;
|
|
228
|
+
const edits = asArray(payload.edited_outside_push);
|
|
229
|
+
if (edits.length > 0) {
|
|
230
|
+
console.log(`Edits made outside your files, in ${deployment}:`);
|
|
231
|
+
for (const edit of edits) console.log(` ${editLine(edit)}`);
|
|
232
|
+
console.log("");
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
for (const change of changes) {
|
|
236
|
+
const path = join(root, change.path);
|
|
237
|
+
if (change.action === "deleted") {
|
|
238
|
+
rmSync(path, { force: true });
|
|
239
|
+
} else {
|
|
240
|
+
mkdirSync(dirname(path), { recursive: true });
|
|
241
|
+
writeFileSync(path, change.content);
|
|
242
|
+
}
|
|
243
|
+
console.log(`${change.action} ${change.path}`);
|
|
244
|
+
}
|
|
245
|
+
config.rememberBaseDeployment({ baseUrl: config.resolveBaseUrl(), workspace, deploymentId: payload.deployment_id });
|
|
246
|
+
|
|
247
|
+
if (changes.length === 0) {
|
|
248
|
+
console.log(`Your files already match ${deployment}.`);
|
|
249
|
+
} else {
|
|
250
|
+
console.log(`Your files now match ${deployment}. Review with \`git diff\`, then \`vatio push\`.`);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
// Pull writes over vatio.yml and tools/, so what they hold now has to be
|
|
255
|
+
// somewhere it can come back from: committed to git.
|
|
256
|
+
function refuseUnkeptChanges(root) {
|
|
257
|
+
const changed = gitChanges(root, [MANIFEST_FILE, "tools"]);
|
|
258
|
+
if (changed === null) {
|
|
259
|
+
fail(
|
|
260
|
+
`vatio pull writes over ${MANIFEST_FILE} and tools/, and this workspace is not in a git repository,\n` +
|
|
261
|
+
"so nothing could give you back what they hold now. Commit them to git first, or run\n" +
|
|
262
|
+
"`vatio pull --force` to overwrite them anyway."
|
|
263
|
+
);
|
|
264
|
+
}
|
|
265
|
+
if (changed.length > 0) {
|
|
266
|
+
fail(
|
|
267
|
+
[
|
|
268
|
+
`vatio pull writes over ${MANIFEST_FILE} and tools/, and they have changes git has not kept:`,
|
|
269
|
+
...changed.map((line) => ` ${line}`),
|
|
270
|
+
"",
|
|
271
|
+
"Commit or stash them first, so git keeps what you had, or run `vatio pull --force` to overwrite them."
|
|
272
|
+
].join("\n")
|
|
273
|
+
);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
|
|
147
277
|
export async function publish(config, args) {
|
|
148
278
|
// Here `--env` names the environment being promoted, not a destination: the
|
|
149
279
|
// destination of a publish is always live, and what goes there is preview,
|
|
@@ -212,6 +342,17 @@ export async function status(config) {
|
|
|
212
342
|
`reads it — add \`knowledge: [${base.name}]\` to vatio.yml and push.`
|
|
213
343
|
);
|
|
214
344
|
}
|
|
345
|
+
// Edits from the Vatio MCP that preview carries, no push has written over
|
|
346
|
+
// and these files have not pulled: the next push is refused until they are.
|
|
347
|
+
const base = config.baseDeploymentFor({ baseUrl: config.resolveBaseUrl(), workspace });
|
|
348
|
+
const edits = asArray(payload.preview_edited_outside_push).filter(
|
|
349
|
+
(edit) => base == null || Number(edit.deployment_id) > base
|
|
350
|
+
);
|
|
351
|
+
if (edits.length > 0) {
|
|
352
|
+
console.error(`Preview has ${edits.length === 1 ? "a change" : `${edits.length} changes`} made outside your files:`);
|
|
353
|
+
for (const edit of edits) console.error(` ${editLine(edit)}`);
|
|
354
|
+
console.error(" `vatio pull` brings them into your files before your next push.");
|
|
355
|
+
}
|
|
215
356
|
printVatioSide(payload.vatio);
|
|
216
357
|
}
|
|
217
358
|
|
package/lib/config.mjs
CHANGED
|
@@ -177,6 +177,35 @@ export class Config {
|
|
|
177
177
|
return this.save(data);
|
|
178
178
|
}
|
|
179
179
|
|
|
180
|
+
// The deployment a workspace's files were last made to match, by a push or
|
|
181
|
+
// a pull, which the next push says it is built on: the platform refuses one
|
|
182
|
+
// that would overwrite edits made since outside the files. Per server and
|
|
183
|
+
// workspace, since a deployment id only means anything on the server that
|
|
184
|
+
// issued it; null when this machine never pushed or pulled it.
|
|
185
|
+
baseDeploymentFor({ baseUrl, workspace }) {
|
|
186
|
+
const all = this.load().base_deployments;
|
|
187
|
+
const id = plainObject(all) && plainObject(all[stripSlash(baseUrl)]) ? all[stripSlash(baseUrl)][workspace] : null;
|
|
188
|
+
return Number.isInteger(id) && id > 0 ? id : null;
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
// Quietly does nothing when the file cannot be written: the push or pull it
|
|
192
|
+
// follows already happened, and the cost of forgetting is one refused push
|
|
193
|
+
// that says to pull.
|
|
194
|
+
rememberBaseDeployment({ baseUrl, workspace, deploymentId }) {
|
|
195
|
+
const id = Number(deploymentId);
|
|
196
|
+
const server = stripSlash(baseUrl);
|
|
197
|
+
if (!Number.isInteger(id) || id <= 0 || !server || !workspace) return;
|
|
198
|
+
try {
|
|
199
|
+
const data = this.load();
|
|
200
|
+
const all = plainObject(data.base_deployments) ? data.base_deployments : {};
|
|
201
|
+
const forServer = plainObject(all[server]) ? all[server] : {};
|
|
202
|
+
data.base_deployments = { ...all, [server]: { ...forServer, [workspace]: id } };
|
|
203
|
+
this.save(data);
|
|
204
|
+
} catch {
|
|
205
|
+
// See above.
|
|
206
|
+
}
|
|
207
|
+
}
|
|
208
|
+
|
|
180
209
|
displayHash() {
|
|
181
210
|
const out = {};
|
|
182
211
|
const base = this.resolveBaseUrl();
|
|
@@ -238,6 +267,10 @@ function stripSlash(value) {
|
|
|
238
267
|
return value == null ? null : value.replace(/\/$/, "");
|
|
239
268
|
}
|
|
240
269
|
|
|
270
|
+
function plainObject(value) {
|
|
271
|
+
return value !== null && typeof value === "object" && !Array.isArray(value);
|
|
272
|
+
}
|
|
273
|
+
|
|
241
274
|
function maskSecret(value) {
|
|
242
275
|
if (!value) return null;
|
|
243
276
|
return value.length <= 8 ? value : `${value.slice(0, 4)}…${value.slice(-4)}`;
|
package/lib/help.mjs
CHANGED
|
@@ -38,8 +38,14 @@ Commands default as listed.
|
|
|
38
38
|
Deploy:
|
|
39
39
|
push [--force] Validate the workspace, then update preview; runs
|
|
40
40
|
no tests (the first push signs in like \`login\`).
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
Refused over edits made outside your files (pull
|
|
42
|
+
them first) and over an unpublished push from
|
|
43
|
+
another git branch; --force replaces either
|
|
44
|
+
pull [--env NAME] [--force]
|
|
45
|
+
Write the deployed agent into vatio.yml's agent:
|
|
46
|
+
block and tools/, edits from the Vatio MCP
|
|
47
|
+
included (default: preview). Wants vatio.yml and
|
|
48
|
+
tools/ committed to git; --force writes anyway
|
|
43
49
|
publish [--force] Take preview live, with its knowledge changes.
|
|
44
50
|
Once published, refused until \`vatio test\` has
|
|
45
51
|
tested preview's version, and while it fails a
|
package/lib/http.mjs
CHANGED
|
@@ -21,6 +21,17 @@ export class UnauthorizedError extends HttpError {}
|
|
|
21
21
|
export class ForbiddenError extends HttpError {}
|
|
22
22
|
export class NotFoundError extends HttpError {}
|
|
23
23
|
|
|
24
|
+
// 409: the call would overwrite something it has not seen -- a push over edits
|
|
25
|
+
// made outside the files (preview_edited_outside), or over another branch's
|
|
26
|
+
// unpublished push. `errorKey` says which, for the command that knows what to
|
|
27
|
+
// do next.
|
|
28
|
+
export class ConflictError extends HttpError {
|
|
29
|
+
constructor(message, options) {
|
|
30
|
+
super(message, options);
|
|
31
|
+
this.errorKey = typeof options.body?.error_key === "string" ? options.body.error_key : null;
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
|
|
24
35
|
// The platform takes calls only from the CLI's current release
|
|
25
36
|
// (RequiresLatestCli on the backend): `latest` is the version to run instead.
|
|
26
37
|
// The refused call did nothing, so bin/vatio.mjs runs the whole command again
|
|
@@ -150,6 +161,7 @@ export class HttpClient {
|
|
|
150
161
|
if (status === 401) throw new UnauthorizedError(message, options);
|
|
151
162
|
if (status === 403) throw new ForbiddenError(message, options);
|
|
152
163
|
if (status === 404) throw new NotFoundError(message, options);
|
|
164
|
+
if (status === 409) throw new ConflictError(message, options);
|
|
153
165
|
if (status === 422) throw new UnprocessableError(body);
|
|
154
166
|
throw new HttpError(`HTTP ${status}: ${message}`, options);
|
|
155
167
|
}
|
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;
|