@cavelon/cli 0.1.4 → 0.1.6
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 +58 -22
- package/dist/choose.js +8 -4
- package/dist/code-hints.js +74 -0
- package/dist/commands/api.js +83 -44
- package/dist/commands/async.js +26 -5
- package/dist/commands/chat.js +177 -0
- package/dist/commands/docs.js +240 -37
- package/dist/commands/fmt.js +120 -0
- package/dist/commands/index.js +9 -1
- package/dist/commands/init.js +64 -30
- package/dist/commands/inventory.js +48 -5
- package/dist/commands/limits-set.js +9 -6
- package/dist/commands/loops.js +15 -9
- package/dist/commands/models.js +16 -7
- package/dist/commands/sandboxes.js +8 -4
- package/dist/commands/schema.js +361 -0
- package/dist/commands/session.js +148 -24
- package/dist/commands/setup.js +22 -6
- package/dist/commands/solution.js +537 -76
- package/dist/commands/tenants.js +156 -15
- package/dist/commands/values.js +18 -9
- package/dist/commands/work.js +343 -35
- package/dist/confirm-token.js +72 -0
- package/dist/contracts.js +3 -0
- package/dist/default-route.js +21 -0
- package/dist/errors.js +4 -0
- package/dist/git.js +14 -5
- package/dist/harness-ref.js +14 -1
- package/dist/http.js +27 -4
- package/dist/invoke.js +8 -4
- package/dist/kit-codes.js +91 -0
- package/dist/limits.js +12 -1
- package/dist/local-state.js +32 -7
- package/dist/main.js +52 -4
- package/dist/mcp.js +72 -15
- package/dist/openapi.js +77 -21
- package/dist/operations.js +4 -3
- package/dist/package-check.js +361 -12
- package/dist/package-files.js +92 -45
- package/dist/package-format.js +222 -0
- package/dist/package-references.js +298 -0
- package/dist/preview-report.js +152 -0
- package/dist/results.js +21 -1
- package/dist/skills/cavelon-authoring/SKILL.md +98 -13
- package/dist/skills/cavelon-long-running/SKILL.md +3 -1
- package/dist/skills/cavelon-loop/SKILL.md +129 -21
- package/dist/skills/cavelon-testing/SKILL.md +130 -10
- package/dist/tenant-choice.js +31 -5
- package/dist/trace-view.js +172 -0
- package/package.json +1 -1
package/dist/package-files.js
CHANGED
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { promises as fs } from "node:fs";
|
|
2
2
|
import path from "node:path";
|
|
3
|
-
import { LineCounter, parseDocument
|
|
3
|
+
import { LineCounter, parseDocument } from "yaml";
|
|
4
4
|
import { CavelonError, ExitCode } from "./errors.js";
|
|
5
5
|
import { readTextFile, withoutBom, writeFileAtomic } from "./fsutil.js";
|
|
6
|
+
import { canonical, PERSONA_SECTION, sameEntry, sameSectionValue, sectionContent, sectionFields, toYaml } from "./package-format.js";
|
|
7
|
+
export { canonical, toYaml };
|
|
6
8
|
export const DEFAULT_LAYOUT = { package: "package", items: {} };
|
|
7
9
|
const PACKAGE_FILE = /\.(ya?ml|json)$/i;
|
|
8
10
|
export function layoutFrom(raw) {
|
|
@@ -34,6 +36,15 @@ export function defaultLayoutFor(schema) {
|
|
|
34
36
|
export function schemaSections(schema) {
|
|
35
37
|
return Object.keys(schema?.properties ?? {});
|
|
36
38
|
}
|
|
39
|
+
const SCOPE_KEY = "x-cavelon-scope";
|
|
40
|
+
const TENANT_WIDE_UNMARKED = ["tenant_settings", "model_registry"];
|
|
41
|
+
export function tenantWideSections(schema) {
|
|
42
|
+
const properties = (schema?.properties ?? {});
|
|
43
|
+
const marked = Object.entries(properties).filter(([, node]) => node && typeof node[SCOPE_KEY] === "string");
|
|
44
|
+
if (marked.length)
|
|
45
|
+
return new Set(marked.filter(([, node]) => node[SCOPE_KEY] === "tenant").map(([section]) => section));
|
|
46
|
+
return new Set(TENANT_WIDE_UNMARKED.filter((section) => section in properties));
|
|
47
|
+
}
|
|
37
48
|
function rel(root, file) {
|
|
38
49
|
return path.relative(root, file).split(path.sep).join("/");
|
|
39
50
|
}
|
|
@@ -113,7 +124,7 @@ function parseFile(root, file, text) {
|
|
|
113
124
|
code: "package_file_invalid",
|
|
114
125
|
severity: "error",
|
|
115
126
|
file: relative,
|
|
116
|
-
line: first.linePos?.[0]?.line,
|
|
127
|
+
line: first.linePos?.[0]?.line ?? (typeof first.pos?.[0] === "number" ? lines.linePos(first.pos[0]).line : undefined),
|
|
117
128
|
message: `Not valid YAML: ${first.message.split("\n")[0]}`,
|
|
118
129
|
},
|
|
119
130
|
};
|
|
@@ -124,10 +135,10 @@ export async function readPackage(root, layout) {
|
|
|
124
135
|
const pkg = {};
|
|
125
136
|
const sources = {};
|
|
126
137
|
const findings = [];
|
|
138
|
+
const unreadable = new Set();
|
|
127
139
|
const dir = path.join(root, layout.package);
|
|
128
140
|
let empty = true;
|
|
129
141
|
for (const name of await listFiles(dir)) {
|
|
130
|
-
empty = false;
|
|
131
142
|
const section = name.replace(PACKAGE_FILE, "");
|
|
132
143
|
const file = path.join(dir, name);
|
|
133
144
|
if (section in sources) {
|
|
@@ -143,12 +154,19 @@ export async function readPackage(root, layout) {
|
|
|
143
154
|
if ("finding" in read) {
|
|
144
155
|
sources[section] = { file: rel(root, file) };
|
|
145
156
|
findings.push(read.finding);
|
|
157
|
+
unreadable.add(section);
|
|
158
|
+
empty = false;
|
|
146
159
|
continue;
|
|
147
160
|
}
|
|
148
161
|
const parsed = parseFile(root, file, read.text);
|
|
149
162
|
sources[section] = parsed.source;
|
|
150
|
-
if (parsed.finding)
|
|
163
|
+
if (!parsed.finding && section === PERSONA_SECTION && (parsed.value === null || parsed.value === undefined))
|
|
164
|
+
continue;
|
|
165
|
+
empty = false;
|
|
166
|
+
if (parsed.finding) {
|
|
151
167
|
findings.push(parsed.finding);
|
|
168
|
+
unreadable.add(section);
|
|
169
|
+
}
|
|
152
170
|
else
|
|
153
171
|
pkg[section] = parsed.value;
|
|
154
172
|
}
|
|
@@ -176,21 +194,23 @@ export async function readPackage(root, layout) {
|
|
|
176
194
|
const file = path.join(itemDir, name);
|
|
177
195
|
const read = await readSource(root, file);
|
|
178
196
|
if ("finding" in read) {
|
|
179
|
-
itemSources.push({ file: rel(root, file) });
|
|
180
197
|
findings.push(read.finding);
|
|
198
|
+
unreadable.add(section);
|
|
181
199
|
continue;
|
|
182
200
|
}
|
|
183
201
|
const parsed = parseFile(root, file, read.text);
|
|
184
|
-
|
|
185
|
-
if (parsed.finding)
|
|
202
|
+
if (parsed.finding) {
|
|
186
203
|
findings.push(parsed.finding);
|
|
187
|
-
|
|
188
|
-
|
|
204
|
+
unreadable.add(section);
|
|
205
|
+
continue;
|
|
206
|
+
}
|
|
207
|
+
itemSources.push(parsed.source);
|
|
208
|
+
items.push(parsed.value);
|
|
189
209
|
}
|
|
190
210
|
sources[section] = itemSources;
|
|
191
211
|
pkg[section] = items;
|
|
192
212
|
}
|
|
193
|
-
return { package: pkg, sources, findings, empty };
|
|
213
|
+
return { package: pkg, sources, findings, empty, unreadable: [...unreadable] };
|
|
194
214
|
}
|
|
195
215
|
export function locate(disk, pointer) {
|
|
196
216
|
const keys = pointer
|
|
@@ -234,17 +254,6 @@ function lineOf(source, keys) {
|
|
|
234
254
|
export function safeSectionName(section) {
|
|
235
255
|
return /^[A-Za-z0-9][A-Za-z0-9_.-]{0,99}$/.test(section) && !section.includes("..");
|
|
236
256
|
}
|
|
237
|
-
export function canonical(value) {
|
|
238
|
-
return JSON.stringify(value, (_key, v) => {
|
|
239
|
-
if (v && typeof v === "object" && !Array.isArray(v)) {
|
|
240
|
-
return Object.fromEntries(Object.entries(v).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
241
|
-
}
|
|
242
|
-
return v;
|
|
243
|
-
});
|
|
244
|
-
}
|
|
245
|
-
export function toYaml(value) {
|
|
246
|
-
return stringify(value, { lineWidth: 0, aliasDuplicateObjects: false });
|
|
247
|
-
}
|
|
248
257
|
async function readValue(root, file) {
|
|
249
258
|
const target = await contentPath(root, file);
|
|
250
259
|
const text = target === undefined ? undefined : await readTextFile(target);
|
|
@@ -259,9 +268,18 @@ async function readValue(root, file) {
|
|
|
259
268
|
return { ok: false };
|
|
260
269
|
}
|
|
261
270
|
}
|
|
262
|
-
function
|
|
271
|
+
export async function placeholdersOnly(root, relative) {
|
|
272
|
+
if (path.posix.basename(relative).replace(PACKAGE_FILE, "") !== PERSONA_SECTION)
|
|
273
|
+
return false;
|
|
274
|
+
const old = await readValue(root, path.join(root, relative));
|
|
275
|
+
return old.ok && (old.value === null || old.value === undefined);
|
|
276
|
+
}
|
|
277
|
+
export function entryLabel(item) {
|
|
263
278
|
const record = item && typeof item === "object" && !Array.isArray(item) ? item : {};
|
|
264
|
-
|
|
279
|
+
return [record.slug, record.name, record.key].find((v) => typeof v === "string" && v.trim() !== "");
|
|
280
|
+
}
|
|
281
|
+
function itemFileName(item, index, taken) {
|
|
282
|
+
const label = entryLabel(item);
|
|
265
283
|
const base = trimDashesAndDots((label ?? `item-${index + 1}`)
|
|
266
284
|
.toLowerCase()
|
|
267
285
|
.normalize("NFKD")
|
|
@@ -275,7 +293,8 @@ function itemFileName(item, index, taken) {
|
|
|
275
293
|
}
|
|
276
294
|
export async function writePackage(root, layout, pkg, schema, options = {}) {
|
|
277
295
|
const dryRun = options.dryRun === true;
|
|
278
|
-
const
|
|
296
|
+
const skip = options.skip ?? new Set();
|
|
297
|
+
const report = { written: [], unchanged: [], removed: [], kept: [], refused: [], tenant_wide: [] };
|
|
279
298
|
const known = new Set(schemaSections(schema));
|
|
280
299
|
const required = new Set(schema?.required ?? []);
|
|
281
300
|
const dir = path.join(root, layout.package);
|
|
@@ -285,7 +304,14 @@ export async function writePackage(root, layout, pkg, schema, options = {}) {
|
|
|
285
304
|
const plans = [];
|
|
286
305
|
const same = [];
|
|
287
306
|
const removals = [];
|
|
288
|
-
|
|
307
|
+
const sections = Object.entries(pkg);
|
|
308
|
+
if (!(PERSONA_SECTION in pkg) && sectionFields(schema, PERSONA_SECTION))
|
|
309
|
+
sections.push([PERSONA_SECTION, null]);
|
|
310
|
+
for (const [section, value] of sections) {
|
|
311
|
+
if (skip.has(section)) {
|
|
312
|
+
report.tenant_wide.push(section);
|
|
313
|
+
continue;
|
|
314
|
+
}
|
|
289
315
|
if (section in layout.items && Array.isArray(value))
|
|
290
316
|
continue;
|
|
291
317
|
if (!safeSectionName(section)) {
|
|
@@ -295,42 +321,58 @@ export async function writePackage(root, layout, pkg, schema, options = {}) {
|
|
|
295
321
|
const current = existing.get(section);
|
|
296
322
|
const file = path.join(dir, current ?? `${section}.yaml`);
|
|
297
323
|
const old = await readValue(root, file);
|
|
298
|
-
if (old.ok &&
|
|
324
|
+
if (old.ok && sameSectionValue(section, old.value, value, schema))
|
|
299
325
|
same.push({ file, section });
|
|
300
326
|
else
|
|
301
|
-
plans.push({ file, section, content: /\.json$/i.test(file)
|
|
327
|
+
plans.push({ file, section, content: sectionContent(section, value, schema, /\.json$/i.test(file)) });
|
|
302
328
|
}
|
|
303
329
|
for (const [section, folder] of Object.entries(layout.items)) {
|
|
304
330
|
const value = pkg[section];
|
|
305
|
-
if (!Array.isArray(value))
|
|
331
|
+
if (!Array.isArray(value) || skip.has(section))
|
|
306
332
|
continue;
|
|
307
333
|
const itemDir = path.join(root, folder);
|
|
334
|
+
const names = await listFiles(itemDir);
|
|
308
335
|
const before = new Map();
|
|
309
|
-
for (const name of
|
|
336
|
+
for (const name of names) {
|
|
310
337
|
const old = await readValue(root, path.join(itemDir, name));
|
|
311
338
|
if (old.ok)
|
|
312
339
|
before.set(name, old.value);
|
|
313
340
|
}
|
|
314
341
|
const taken = new Set();
|
|
315
|
-
const
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
}
|
|
323
|
-
}
|
|
324
|
-
return undefined;
|
|
342
|
+
const claim = (name) => {
|
|
343
|
+
taken.add(name);
|
|
344
|
+
return name;
|
|
345
|
+
};
|
|
346
|
+
const holding = value.map((item) => {
|
|
347
|
+
const name = [...before].find(([name, old]) => !taken.has(name) && sameEntry(section, old, item, schema))?.[0];
|
|
348
|
+
return name && claim(name);
|
|
325
349
|
});
|
|
350
|
+
const recorded = options.itemFiles?.[section] ?? {};
|
|
351
|
+
const home = value.map((item, index) => {
|
|
352
|
+
if (holding[index])
|
|
353
|
+
return holding[index];
|
|
354
|
+
const label = entryLabel(item);
|
|
355
|
+
if (label === undefined)
|
|
356
|
+
return undefined;
|
|
357
|
+
const byName = [...before].find(([name, old]) => !taken.has(name) && entryLabel(old) === label)?.[0];
|
|
358
|
+
if (byName)
|
|
359
|
+
return claim(byName);
|
|
360
|
+
const last = recorded[label];
|
|
361
|
+
return last && !taken.has(last) && names.includes(last) && !before.has(last) ? claim(last) : undefined;
|
|
362
|
+
});
|
|
363
|
+
const placedHere = {};
|
|
326
364
|
value.forEach((item, index) => {
|
|
327
|
-
const name =
|
|
328
|
-
|
|
365
|
+
const name = home[index] ?? itemFileName(item, index, taken);
|
|
366
|
+
const label = entryLabel(item);
|
|
367
|
+
if (label !== undefined)
|
|
368
|
+
placedHere[label] = name;
|
|
369
|
+
if (holding[index])
|
|
329
370
|
same.push({ file: path.join(itemDir, name), section });
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
plans.push({ file: path.join(itemDir, itemFileName(item, index, taken)), section, content: toYaml(item) });
|
|
371
|
+
else
|
|
372
|
+
plans.push({ file: path.join(itemDir, name), section, content: toYaml(item) });
|
|
333
373
|
});
|
|
374
|
+
if (options.placed)
|
|
375
|
+
options.placed[section] = placedHere;
|
|
334
376
|
for (const name of before.keys())
|
|
335
377
|
if (!taken.has(name))
|
|
336
378
|
removals.push(path.join(itemDir, name));
|
|
@@ -338,7 +380,12 @@ export async function writePackage(root, layout, pkg, schema, options = {}) {
|
|
|
338
380
|
await fs.mkdir(itemDir, { recursive: true });
|
|
339
381
|
}
|
|
340
382
|
for (const [section, name] of existing) {
|
|
341
|
-
if (section
|
|
383
|
+
if (skip.has(section)) {
|
|
384
|
+
if (!report.tenant_wide.includes(section))
|
|
385
|
+
report.tenant_wide.push(section);
|
|
386
|
+
continue;
|
|
387
|
+
}
|
|
388
|
+
if (section in pkg || section === PERSONA_SECTION)
|
|
342
389
|
continue;
|
|
343
390
|
const file = path.join(dir, name);
|
|
344
391
|
if (known.has(section))
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
import { stringify } from "yaml";
|
|
2
|
+
export const PERSONA_SECTION = "persona";
|
|
3
|
+
export function toYaml(value) {
|
|
4
|
+
return stringify(value, { lineWidth: 0, aliasDuplicateObjects: false });
|
|
5
|
+
}
|
|
6
|
+
const isObject = (v) => Boolean(v) && typeof v === "object" && !Array.isArray(v);
|
|
7
|
+
function resolve(schema, node) {
|
|
8
|
+
let current = isObject(node) ? node : undefined;
|
|
9
|
+
for (let hops = 0; current && typeof current.$ref === "string" && hops < 10; hops++) {
|
|
10
|
+
const ref = current.$ref;
|
|
11
|
+
if (!ref.startsWith("#/$defs/"))
|
|
12
|
+
return undefined;
|
|
13
|
+
current = schema.$defs?.[ref.slice("#/$defs/".length)];
|
|
14
|
+
}
|
|
15
|
+
return current;
|
|
16
|
+
}
|
|
17
|
+
function branches(schema, node, depth = 0) {
|
|
18
|
+
const list = (Array.isArray(node.anyOf) ? node.anyOf : Array.isArray(node.oneOf) ? node.oneOf : undefined);
|
|
19
|
+
if (!list || depth > 10)
|
|
20
|
+
return [node];
|
|
21
|
+
return list.map((b) => resolve(schema, b)).flatMap((b) => (b ? branches(schema, b, depth + 1) : []));
|
|
22
|
+
}
|
|
23
|
+
const propertiesOf = (node) => (isObject(node?.properties) ? node.properties : undefined);
|
|
24
|
+
function allowed(schema, node, value) {
|
|
25
|
+
const field = resolve(schema, node);
|
|
26
|
+
if (!field)
|
|
27
|
+
return true;
|
|
28
|
+
if ("const" in field)
|
|
29
|
+
return field.const === value;
|
|
30
|
+
return Array.isArray(field.enum) ? field.enum.includes(value) : true;
|
|
31
|
+
}
|
|
32
|
+
function fits(schema, shape, value) {
|
|
33
|
+
const props = propertiesOf(shape);
|
|
34
|
+
const required = Array.isArray(shape.required) ? shape.required : [];
|
|
35
|
+
return Object.entries(value).every(([k, v]) => k in props && allowed(schema, props[k], v)) && required.every((k) => k in value);
|
|
36
|
+
}
|
|
37
|
+
function objectBranch(schema, node, value) {
|
|
38
|
+
const objects = branches(schema, node).filter((b) => propertiesOf(b));
|
|
39
|
+
if (objects.length === 1) {
|
|
40
|
+
const only = objects[0];
|
|
41
|
+
return only.additionalProperties === false && Object.keys(value).some((k) => !(k in propertiesOf(only))) ? undefined : only;
|
|
42
|
+
}
|
|
43
|
+
const fitting = objects.filter((b) => fits(schema, b, value));
|
|
44
|
+
return fitting.length === 1 ? fitting[0] : undefined;
|
|
45
|
+
}
|
|
46
|
+
function arrayBranch(schema, node) {
|
|
47
|
+
const arrays = branches(schema, node).filter((b) => b.type === "array");
|
|
48
|
+
return arrays.length === 1 ? arrays[0] : undefined;
|
|
49
|
+
}
|
|
50
|
+
export function sectionFields(schema, section) {
|
|
51
|
+
if (!schema)
|
|
52
|
+
return undefined;
|
|
53
|
+
const node = resolve(schema, schema.properties?.[section]);
|
|
54
|
+
if (!node)
|
|
55
|
+
return undefined;
|
|
56
|
+
const object = branches(schema, node).find((b) => propertiesOf(b));
|
|
57
|
+
const props = propertiesOf(object);
|
|
58
|
+
if (!props)
|
|
59
|
+
return undefined;
|
|
60
|
+
return Object.fromEntries(Object.entries(props).map(([k, v]) => [k, resolve(schema, v) ?? {}]));
|
|
61
|
+
}
|
|
62
|
+
export function canonical(value) {
|
|
63
|
+
return JSON.stringify(value, (_key, v) => {
|
|
64
|
+
if (v && typeof v === "object" && !Array.isArray(v)) {
|
|
65
|
+
return Object.fromEntries(Object.entries(v).sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0)));
|
|
66
|
+
}
|
|
67
|
+
return v;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
const requiredOf = (shape) => new Set(Array.isArray(shape?.required) ? shape.required : []);
|
|
71
|
+
function nullable(schema, field) {
|
|
72
|
+
if (field.type === "null" || (Array.isArray(field.type) && field.type.includes("null")))
|
|
73
|
+
return true;
|
|
74
|
+
return branches(schema, field).some((b) => b.type === "null");
|
|
75
|
+
}
|
|
76
|
+
function implicitDefault(schema, key, sub, required, position) {
|
|
77
|
+
const field = resolve(schema, sub);
|
|
78
|
+
if (!field || required.has(key))
|
|
79
|
+
return undefined;
|
|
80
|
+
if (position !== undefined && key.endsWith("_order") && field.type === "integer" && typeof field.default === "number")
|
|
81
|
+
return field.default + position;
|
|
82
|
+
if (field.default !== undefined)
|
|
83
|
+
return field.default === null ? undefined : field.default;
|
|
84
|
+
if (nullable(schema, field))
|
|
85
|
+
return undefined;
|
|
86
|
+
if (field.type === "array")
|
|
87
|
+
return [];
|
|
88
|
+
if (field.type === "object" || propertiesOf(field))
|
|
89
|
+
return {};
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
export function exportForm(schema, node, value, depth = 0, position) {
|
|
93
|
+
const resolved = resolve(schema, node);
|
|
94
|
+
if (!resolved || depth > 40)
|
|
95
|
+
return value;
|
|
96
|
+
if (Array.isArray(value)) {
|
|
97
|
+
const array = arrayBranch(schema, resolved);
|
|
98
|
+
return array?.items ? value.map((item, i) => exportForm(schema, array.items, item, depth + 1, i)) : value;
|
|
99
|
+
}
|
|
100
|
+
if (!isObject(value))
|
|
101
|
+
return value;
|
|
102
|
+
const object = objectBranch(schema, resolved, value);
|
|
103
|
+
const props = propertiesOf(object);
|
|
104
|
+
if (!props)
|
|
105
|
+
return value;
|
|
106
|
+
const required = requiredOf(object);
|
|
107
|
+
const out = {};
|
|
108
|
+
for (const [key, sub] of Object.entries(props)) {
|
|
109
|
+
if (key in value)
|
|
110
|
+
out[key] = exportForm(schema, sub, value[key], depth + 1);
|
|
111
|
+
else {
|
|
112
|
+
const fallback = implicitDefault(schema, key, sub, required, position);
|
|
113
|
+
if (fallback !== undefined)
|
|
114
|
+
out[key] = structuredClone(fallback);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
for (const [key, inner] of Object.entries(value))
|
|
118
|
+
if (!(key in out) && !(key in props))
|
|
119
|
+
out[key] = inner;
|
|
120
|
+
return out;
|
|
121
|
+
}
|
|
122
|
+
export function packageExportForm(schema, pkg) {
|
|
123
|
+
const out = {};
|
|
124
|
+
for (const [section, value] of Object.entries(pkg)) {
|
|
125
|
+
const node = schema.properties?.[section];
|
|
126
|
+
out[section] = node ? exportForm(schema, node, value) : value;
|
|
127
|
+
}
|
|
128
|
+
return out;
|
|
129
|
+
}
|
|
130
|
+
export function withoutNulls(value) {
|
|
131
|
+
if (Array.isArray(value))
|
|
132
|
+
return value.map(withoutNulls);
|
|
133
|
+
if (!isObject(value))
|
|
134
|
+
return value;
|
|
135
|
+
const out = {};
|
|
136
|
+
for (const [key, inner] of Object.entries(value))
|
|
137
|
+
if (inner !== null)
|
|
138
|
+
out[key] = withoutNulls(inner);
|
|
139
|
+
return out;
|
|
140
|
+
}
|
|
141
|
+
function isEmpty(value) {
|
|
142
|
+
return value === null || value === undefined || (isObject(value) && Object.keys(value).length === 0);
|
|
143
|
+
}
|
|
144
|
+
const isUnset = (value) => isEmpty(value) || (Array.isArray(value) && value.length === 0);
|
|
145
|
+
export function settledForm(schema, node, value, depth = 0) {
|
|
146
|
+
const resolved = resolve(schema, node);
|
|
147
|
+
if (!resolved || depth > 40)
|
|
148
|
+
return withoutNulls(value);
|
|
149
|
+
if (Array.isArray(value)) {
|
|
150
|
+
const array = arrayBranch(schema, resolved);
|
|
151
|
+
return array?.items ? value.map((item) => settledForm(schema, array.items, item, depth + 1)) : withoutNulls(value);
|
|
152
|
+
}
|
|
153
|
+
if (!isObject(value))
|
|
154
|
+
return value;
|
|
155
|
+
const object = objectBranch(schema, resolved, value);
|
|
156
|
+
const props = propertiesOf(object);
|
|
157
|
+
if (!props)
|
|
158
|
+
return withoutNulls(value);
|
|
159
|
+
const required = requiredOf(object);
|
|
160
|
+
const out = {};
|
|
161
|
+
for (const [key, inner] of Object.entries(value)) {
|
|
162
|
+
if (!(key in props)) {
|
|
163
|
+
if (inner !== null)
|
|
164
|
+
out[key] = withoutNulls(inner);
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const settled = settledForm(schema, props[key], inner, depth + 1);
|
|
168
|
+
if (!required.has(key)) {
|
|
169
|
+
const fallback = implicitDefault(schema, key, props[key], required, undefined);
|
|
170
|
+
if (isUnset(settled) && (fallback === undefined || isUnset(fallback)))
|
|
171
|
+
continue;
|
|
172
|
+
if (fallback !== undefined && canonical(settled) === canonical(settledForm(schema, props[key], fallback, depth + 1)))
|
|
173
|
+
continue;
|
|
174
|
+
}
|
|
175
|
+
out[key] = settled;
|
|
176
|
+
}
|
|
177
|
+
return out;
|
|
178
|
+
}
|
|
179
|
+
export function sameSectionValue(section, a, b, schema = null) {
|
|
180
|
+
const node = schema?.properties?.[section];
|
|
181
|
+
const left = schema && node ? settledForm(schema, node, a) : withoutNulls(a);
|
|
182
|
+
const right = schema && node ? settledForm(schema, node, b) : withoutNulls(b);
|
|
183
|
+
if (section === PERSONA_SECTION && isEmpty(left) && isEmpty(right))
|
|
184
|
+
return true;
|
|
185
|
+
return canonical(left) === canonical(right);
|
|
186
|
+
}
|
|
187
|
+
export function sameEntry(section, a, b, schema = null) {
|
|
188
|
+
const node = schema?.properties?.[section];
|
|
189
|
+
if (!schema || !node)
|
|
190
|
+
return canonical(withoutNulls(a)) === canonical(withoutNulls(b));
|
|
191
|
+
return canonical(settledForm(schema, node, [a])) === canonical(settledForm(schema, node, [b]));
|
|
192
|
+
}
|
|
193
|
+
function placeholder(key, field) {
|
|
194
|
+
const fallback = field.default !== undefined ? field.default : field.type === "array" ? [] : null;
|
|
195
|
+
return `# ${toYaml({ [key]: fallback }).trimEnd().split("\n").join("\n# ")}`;
|
|
196
|
+
}
|
|
197
|
+
const PERSONA_HEADER = [
|
|
198
|
+
"# The persona: who the assistant is, shared by every agent of this solution (an agent's",
|
|
199
|
+
"# system_prompt says what that agent does). A commented field is not set; remove its #",
|
|
200
|
+
"# to set it. Write the greeting and fallback texts in the language the assistant answers in.",
|
|
201
|
+
];
|
|
202
|
+
export function personaYaml(value, fields) {
|
|
203
|
+
const set = isObject(value) ? value : {};
|
|
204
|
+
const lines = [...PERSONA_HEADER];
|
|
205
|
+
for (const [key, field] of Object.entries(fields)) {
|
|
206
|
+
if (set[key] === undefined || set[key] === null)
|
|
207
|
+
lines.push(placeholder(key, field));
|
|
208
|
+
else
|
|
209
|
+
lines.push(toYaml({ [key]: set[key] }).trimEnd());
|
|
210
|
+
}
|
|
211
|
+
for (const [key, inner] of Object.entries(set)) {
|
|
212
|
+
if (!(key in fields) && inner !== undefined)
|
|
213
|
+
lines.push(toYaml({ [key]: inner }).trimEnd());
|
|
214
|
+
}
|
|
215
|
+
return lines.join("\n") + "\n";
|
|
216
|
+
}
|
|
217
|
+
export function sectionContent(section, value, schema, json) {
|
|
218
|
+
if (json)
|
|
219
|
+
return JSON.stringify(value, null, 2) + "\n";
|
|
220
|
+
const fields = section === PERSONA_SECTION ? sectionFields(schema, section) : undefined;
|
|
221
|
+
return fields ? personaYaml(value, fields) : toYaml(value);
|
|
222
|
+
}
|