@farming-labs/docs 0.2.53 → 0.2.55
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/dist/agent-BOOcHVDL.mjs +623 -0
- package/dist/{agent-BMxMpEa0.mjs → agent-DXkXi-pS.mjs} +590 -19
- package/dist/agent-evals-BD17jOqL.mjs +1166 -0
- package/dist/{agent-export--X9Go-wj.mjs → agent-export-BYznnYed.mjs} +9 -8
- package/dist/agent-scope-B8lptqCd.mjs +283 -0
- package/dist/agent-surface-drift-LS8zQgbq.mjs +1429 -0
- package/dist/{agents-CW-zTNKP.mjs → agents-BHzeeY45.mjs} +7 -6
- package/dist/{analytics-BZEwTK-8.mjs → analytics-Bx44lg6d.mjs} +1 -1
- package/dist/cli/index.mjs +29 -29
- package/dist/client/react.d.mts +1 -1
- package/dist/client/react.mjs +1 -1
- package/dist/{cloud-zs0qAnKJ.mjs → cloud-HWFlbfLJ.mjs} +4 -4
- package/dist/{cloud-ask-ai-BFlx8VMO.mjs → cloud-ask-ai-1k0q6OAH.mjs} +1 -1
- package/dist/{cloud-ask-ai-CMzYVzVX.d.mts → cloud-ask-ai-CvRVnwdK.d.mts} +1 -1
- package/dist/code-blocks-C9awWzEQ.mjs +871 -0
- package/dist/codeblocks-BzfkREEC.mjs +250 -0
- package/dist/{config-DhIyFsy9.mjs → config-CydaZ5PB.mjs} +52 -11
- package/dist/{dev-DFeVU2Ui.mjs → dev-BA2kRuCn.mjs} +3 -3
- package/dist/docs-cloud-server.d.mts +2 -2
- package/dist/docs-cloud-server.mjs +2 -2
- package/dist/{doctor-CkM5tKdD.mjs → doctor-CNIPJdxN.mjs} +150 -32
- package/dist/{downgrade-Dj_KXV11.mjs → downgrade-BStNhyCc.mjs} +2 -2
- package/dist/index.d.mts +40 -17
- package/dist/index.mjs +6 -7
- package/dist/{init-Cmz-oqnp.mjs → init-gnHQ_Pz9.mjs} +3 -3
- package/dist/{mcp-B4Q1PN93.mjs → mcp-F9zkBHrU.mjs} +6 -5
- package/dist/mcp.d.mts +22 -4
- package/dist/mcp.mjs +521 -43
- package/dist/{package-version-DxnTZH4m.mjs → package-version-DRIc08EU.mjs} +1 -1
- package/dist/{reading-time-BuK2YL23.mjs → reading-time-BrTd3DIh.mjs} +2 -3
- package/dist/{review-BEsbm0l2.mjs → review-Dlb8dRRx.mjs} +149 -14
- package/dist/{robots-BOwkq9du.mjs → robots-4BUJxlvV.mjs} +4 -5
- package/dist/{robots-C6uQG-fS.mjs → robots-DAptQnkx.mjs} +1 -1
- package/dist/{search-CtAXkk_J.mjs → search-B8cF77_i.mjs} +6 -5
- package/dist/{search-CTu6Bvq-.d.mts → search-BnQKgDbk.d.mts} +1 -1
- package/dist/{search-CJNLkdjX.mjs → search-CEg1omHv.mjs} +54 -11
- package/dist/server.d.mts +108 -5
- package/dist/server.mjs +9 -9
- package/dist/{sitemap-OPb8EdNe.mjs → sitemap-D9ivTLm8.mjs} +8 -7
- package/dist/{sitemap-server-DnilMEiZ.mjs → sitemap-server-wsNLyVkb.mjs} +1 -1
- package/dist/{types-CdKu8KmO.d.mts → types-R-DN23yw.d.mts} +79 -2
- package/dist/{upgrade-BNdBB9tQ.mjs → upgrade-CXYRNV0C.mjs} +2 -2
- package/package.json +2 -2
- package/dist/agent-2t4kZLGo.mjs +0 -10
- package/dist/agent-contract-b0akaPcg.mjs +0 -573
- package/dist/codeblocks-DWM4IJPL.mjs +0 -1710
- package/dist/review-BHdy75TE.mjs +0 -541
- /package/dist/{cloud-analytics-Dlk_byos.mjs → cloud-analytics-CSyFE6SS.mjs} +0 -0
- /package/dist/{errors-BxfYhp1L.mjs → errors-BFUtdZfC.mjs} +0 -0
- /package/dist/{templates-Bty9M6lA.mjs → templates-1Cod8KrJ.mjs} +0 -0
- /package/dist/{utils-CMZTjRQT.mjs → utils-DBCCkkJS.mjs} +0 -0
|
@@ -1,573 +0,0 @@
|
|
|
1
|
-
//#region src/agent-contract.ts
|
|
2
|
-
const STRUCTURED_AGENT_FIELDS = [
|
|
3
|
-
"task",
|
|
4
|
-
"outcome",
|
|
5
|
-
"appliesTo",
|
|
6
|
-
"prerequisites",
|
|
7
|
-
"files",
|
|
8
|
-
"commands",
|
|
9
|
-
"sideEffects",
|
|
10
|
-
"verification",
|
|
11
|
-
"rollback",
|
|
12
|
-
"failureModes"
|
|
13
|
-
];
|
|
14
|
-
const AGENT_FIELDS = ["tokenBudget", ...STRUCTURED_AGENT_FIELDS];
|
|
15
|
-
const APPLIES_TO_FIELDS = [
|
|
16
|
-
"framework",
|
|
17
|
-
"version",
|
|
18
|
-
"package"
|
|
19
|
-
];
|
|
20
|
-
const COMMAND_FIELDS = [
|
|
21
|
-
"run",
|
|
22
|
-
"cwd",
|
|
23
|
-
"description"
|
|
24
|
-
];
|
|
25
|
-
const VERIFICATION_FIELDS = [
|
|
26
|
-
"description",
|
|
27
|
-
"run",
|
|
28
|
-
"expect"
|
|
29
|
-
];
|
|
30
|
-
const FAILURE_MODE_FIELDS = ["symptom", "resolution"];
|
|
31
|
-
const PAGE_AGENT_CONTRACT_START_MARKER = "<!-- farming-labs:agent-contract:start -->";
|
|
32
|
-
const PAGE_AGENT_CONTRACT_END_MARKER = "<!-- farming-labs:agent-contract:end -->";
|
|
33
|
-
function isRecord(value) {
|
|
34
|
-
return Boolean(value) && typeof value === "object" && !Array.isArray(value);
|
|
35
|
-
}
|
|
36
|
-
function normalizeString(value) {
|
|
37
|
-
if (typeof value !== "string") return void 0;
|
|
38
|
-
return value.trim() || void 0;
|
|
39
|
-
}
|
|
40
|
-
function normalizeStringList(value) {
|
|
41
|
-
if (!Array.isArray(value)) return void 0;
|
|
42
|
-
const seen = /* @__PURE__ */ new Set();
|
|
43
|
-
const normalized = [];
|
|
44
|
-
for (const item of value) {
|
|
45
|
-
const text = normalizeString(item);
|
|
46
|
-
if (!text || seen.has(text)) continue;
|
|
47
|
-
seen.add(text);
|
|
48
|
-
normalized.push(text);
|
|
49
|
-
}
|
|
50
|
-
return normalized.length > 0 ? normalized : void 0;
|
|
51
|
-
}
|
|
52
|
-
function normalizeStringOrList(value) {
|
|
53
|
-
const single = normalizeString(value);
|
|
54
|
-
if (single) return [single];
|
|
55
|
-
return normalizeStringList(value);
|
|
56
|
-
}
|
|
57
|
-
function normalizeAppliesTo(value) {
|
|
58
|
-
if (!isRecord(value)) return void 0;
|
|
59
|
-
const framework = normalizeStringOrList(value.framework);
|
|
60
|
-
const version = normalizeStringOrList(value.version);
|
|
61
|
-
const packageNames = normalizeStringOrList(value.package);
|
|
62
|
-
if (!framework && !version && !packageNames) return void 0;
|
|
63
|
-
return {
|
|
64
|
-
...framework ? { framework } : {},
|
|
65
|
-
...version ? { version } : {},
|
|
66
|
-
...packageNames ? { package: packageNames } : {}
|
|
67
|
-
};
|
|
68
|
-
}
|
|
69
|
-
function normalizeCommands(value) {
|
|
70
|
-
if (!Array.isArray(value)) return void 0;
|
|
71
|
-
const commands = [];
|
|
72
|
-
for (const item of value) {
|
|
73
|
-
const shorthand = normalizeString(item);
|
|
74
|
-
if (shorthand) {
|
|
75
|
-
commands.push(shorthand);
|
|
76
|
-
continue;
|
|
77
|
-
}
|
|
78
|
-
if (!isRecord(item)) continue;
|
|
79
|
-
const run = normalizeString(item.run);
|
|
80
|
-
if (!run) continue;
|
|
81
|
-
const cwd = normalizeString(item.cwd);
|
|
82
|
-
const description = normalizeString(item.description);
|
|
83
|
-
commands.push({
|
|
84
|
-
run,
|
|
85
|
-
...cwd ? { cwd } : {},
|
|
86
|
-
...description ? { description } : {}
|
|
87
|
-
});
|
|
88
|
-
}
|
|
89
|
-
return commands.length > 0 ? commands : void 0;
|
|
90
|
-
}
|
|
91
|
-
function normalizeVerification(value) {
|
|
92
|
-
if (!Array.isArray(value)) return void 0;
|
|
93
|
-
const verification = [];
|
|
94
|
-
for (const item of value) {
|
|
95
|
-
const shorthand = normalizeString(item);
|
|
96
|
-
if (shorthand) {
|
|
97
|
-
verification.push(shorthand);
|
|
98
|
-
continue;
|
|
99
|
-
}
|
|
100
|
-
if (!isRecord(item)) continue;
|
|
101
|
-
const description = normalizeString(item.description);
|
|
102
|
-
const run = normalizeString(item.run);
|
|
103
|
-
const expect = normalizeString(item.expect);
|
|
104
|
-
if (!description && !run && !expect) continue;
|
|
105
|
-
verification.push({
|
|
106
|
-
...description ? { description } : {},
|
|
107
|
-
...run ? { run } : {},
|
|
108
|
-
...expect ? { expect } : {}
|
|
109
|
-
});
|
|
110
|
-
}
|
|
111
|
-
return verification.length > 0 ? verification : void 0;
|
|
112
|
-
}
|
|
113
|
-
function normalizeFailureModes(value) {
|
|
114
|
-
if (!Array.isArray(value)) return void 0;
|
|
115
|
-
const failureModes = [];
|
|
116
|
-
for (const item of value) {
|
|
117
|
-
const shorthand = normalizeString(item);
|
|
118
|
-
if (shorthand) {
|
|
119
|
-
failureModes.push(shorthand);
|
|
120
|
-
continue;
|
|
121
|
-
}
|
|
122
|
-
if (!isRecord(item)) continue;
|
|
123
|
-
const symptom = normalizeString(item.symptom);
|
|
124
|
-
if (!symptom) continue;
|
|
125
|
-
const resolution = normalizeString(item.resolution);
|
|
126
|
-
failureModes.push({
|
|
127
|
-
symptom,
|
|
128
|
-
...resolution ? { resolution } : {}
|
|
129
|
-
});
|
|
130
|
-
}
|
|
131
|
-
return failureModes.length > 0 ? failureModes : void 0;
|
|
132
|
-
}
|
|
133
|
-
function normalizeTokenBudget(value) {
|
|
134
|
-
if (typeof value !== "number" || !Number.isFinite(value) || value <= 0) return void 0;
|
|
135
|
-
return Math.ceil(value);
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Normalize untrusted page frontmatter into the stable agent contract shape.
|
|
139
|
-
* Invalid fields are omitted so malformed author input cannot break page delivery.
|
|
140
|
-
*/
|
|
141
|
-
function normalizePageAgentFrontmatter(value) {
|
|
142
|
-
if (!isRecord(value)) return void 0;
|
|
143
|
-
const tokenBudget = normalizeTokenBudget(value.tokenBudget);
|
|
144
|
-
const task = normalizeString(value.task);
|
|
145
|
-
const outcome = normalizeString(value.outcome);
|
|
146
|
-
const appliesTo = normalizeAppliesTo(value.appliesTo);
|
|
147
|
-
const prerequisites = normalizeStringList(value.prerequisites);
|
|
148
|
-
const files = normalizeStringList(value.files);
|
|
149
|
-
const commands = normalizeCommands(value.commands);
|
|
150
|
-
const sideEffects = normalizeStringList(value.sideEffects);
|
|
151
|
-
const verification = normalizeVerification(value.verification);
|
|
152
|
-
const rollback = normalizeStringList(value.rollback);
|
|
153
|
-
const failureModes = normalizeFailureModes(value.failureModes);
|
|
154
|
-
const normalized = {
|
|
155
|
-
...tokenBudget !== void 0 ? { tokenBudget } : {},
|
|
156
|
-
...task ? { task } : {},
|
|
157
|
-
...outcome ? { outcome } : {},
|
|
158
|
-
...appliesTo ? { appliesTo } : {},
|
|
159
|
-
...prerequisites ? { prerequisites } : {},
|
|
160
|
-
...files ? { files } : {},
|
|
161
|
-
...commands ? { commands } : {},
|
|
162
|
-
...sideEffects ? { sideEffects } : {},
|
|
163
|
-
...verification ? { verification } : {},
|
|
164
|
-
...rollback ? { rollback } : {},
|
|
165
|
-
...failureModes ? { failureModes } : {}
|
|
166
|
-
};
|
|
167
|
-
return Object.keys(normalized).length > 0 ? normalized : void 0;
|
|
168
|
-
}
|
|
169
|
-
function hasStructuredPageAgentContract(value) {
|
|
170
|
-
const normalized = normalizePageAgentFrontmatter(value);
|
|
171
|
-
if (!normalized) return false;
|
|
172
|
-
return STRUCTURED_AGENT_FIELDS.some((field) => normalized[field] !== void 0);
|
|
173
|
-
}
|
|
174
|
-
function addStringIssue(issues, object, field) {
|
|
175
|
-
if (!(field in object)) return;
|
|
176
|
-
if (!normalizeString(object[field])) issues.push({
|
|
177
|
-
path: `agent.${field}`,
|
|
178
|
-
message: "must be a non-empty string"
|
|
179
|
-
});
|
|
180
|
-
}
|
|
181
|
-
function addStringListIssues(issues, object, field) {
|
|
182
|
-
if (!(field in object)) return;
|
|
183
|
-
const value = object[field];
|
|
184
|
-
if (!Array.isArray(value) || value.length === 0) {
|
|
185
|
-
issues.push({
|
|
186
|
-
path: `agent.${field}`,
|
|
187
|
-
message: "must be a non-empty string array"
|
|
188
|
-
});
|
|
189
|
-
return;
|
|
190
|
-
}
|
|
191
|
-
value.forEach((item, index) => {
|
|
192
|
-
if (!normalizeString(item)) issues.push({
|
|
193
|
-
path: `agent.${field}[${index}]`,
|
|
194
|
-
message: "must be a non-empty string"
|
|
195
|
-
});
|
|
196
|
-
});
|
|
197
|
-
}
|
|
198
|
-
function editDistance(left, right) {
|
|
199
|
-
const previous = Array.from({ length: right.length + 1 }, (_, index) => index);
|
|
200
|
-
for (let leftIndex = 1; leftIndex <= left.length; leftIndex += 1) {
|
|
201
|
-
const current = [leftIndex];
|
|
202
|
-
for (let rightIndex = 1; rightIndex <= right.length; rightIndex += 1) current[rightIndex] = Math.min((current[rightIndex - 1] ?? 0) + 1, (previous[rightIndex] ?? 0) + 1, (previous[rightIndex - 1] ?? 0) + (left[leftIndex - 1] === right[rightIndex - 1] ? 0 : 1));
|
|
203
|
-
previous.splice(0, previous.length, ...current);
|
|
204
|
-
}
|
|
205
|
-
return previous[right.length] ?? 0;
|
|
206
|
-
}
|
|
207
|
-
function closestKnownField(field, allowed) {
|
|
208
|
-
const closest = allowed.map((candidate) => ({
|
|
209
|
-
candidate,
|
|
210
|
-
distance: editDistance(field, candidate)
|
|
211
|
-
})).sort((left, right) => left.distance - right.distance || left.candidate.localeCompare(right.candidate))[0];
|
|
212
|
-
if (!closest) return void 0;
|
|
213
|
-
const threshold = Math.max(1, Math.min(3, Math.floor(closest.candidate.length / 3)));
|
|
214
|
-
return closest.distance <= threshold ? closest.candidate : void 0;
|
|
215
|
-
}
|
|
216
|
-
function addUnknownKeyIssues(issues, object, allowed, path) {
|
|
217
|
-
for (const field of Object.keys(object)) {
|
|
218
|
-
if (allowed.includes(field)) continue;
|
|
219
|
-
const suggestion = closestKnownField(field, allowed);
|
|
220
|
-
issues.push({
|
|
221
|
-
path: `${path}.${field}`,
|
|
222
|
-
message: suggestion ? `is not recognized; did you mean "${path}.${suggestion}"?` : "is not recognized"
|
|
223
|
-
});
|
|
224
|
-
}
|
|
225
|
-
}
|
|
226
|
-
/** Return author-facing validation issues without throwing. */
|
|
227
|
-
function getPageAgentFrontmatterIssues(value) {
|
|
228
|
-
if (value === void 0) return [];
|
|
229
|
-
if (!isRecord(value)) return [{
|
|
230
|
-
path: "agent",
|
|
231
|
-
message: "must be an object"
|
|
232
|
-
}];
|
|
233
|
-
const issues = [];
|
|
234
|
-
addUnknownKeyIssues(issues, value, AGENT_FIELDS, "agent");
|
|
235
|
-
if ("tokenBudget" in value && (typeof value.tokenBudget !== "number" || !Number.isFinite(value.tokenBudget) || value.tokenBudget <= 0)) issues.push({
|
|
236
|
-
path: "agent.tokenBudget",
|
|
237
|
-
message: "must be a positive finite number"
|
|
238
|
-
});
|
|
239
|
-
addStringIssue(issues, value, "task");
|
|
240
|
-
addStringIssue(issues, value, "outcome");
|
|
241
|
-
if ("appliesTo" in value) if (!isRecord(value.appliesTo)) issues.push({
|
|
242
|
-
path: "agent.appliesTo",
|
|
243
|
-
message: "must be an object"
|
|
244
|
-
});
|
|
245
|
-
else {
|
|
246
|
-
addUnknownKeyIssues(issues, value.appliesTo, APPLIES_TO_FIELDS, "agent.appliesTo");
|
|
247
|
-
for (const field of [
|
|
248
|
-
"framework",
|
|
249
|
-
"version",
|
|
250
|
-
"package"
|
|
251
|
-
]) {
|
|
252
|
-
if (!(field in value.appliesTo)) continue;
|
|
253
|
-
const fieldValue = value.appliesTo[field];
|
|
254
|
-
if (!normalizeStringOrList(fieldValue)) {
|
|
255
|
-
issues.push({
|
|
256
|
-
path: `agent.appliesTo.${field}`,
|
|
257
|
-
message: "must be a non-empty string or string array"
|
|
258
|
-
});
|
|
259
|
-
continue;
|
|
260
|
-
}
|
|
261
|
-
if (Array.isArray(fieldValue)) fieldValue.forEach((item, index) => {
|
|
262
|
-
if (!normalizeString(item)) issues.push({
|
|
263
|
-
path: `agent.appliesTo.${field}[${index}]`,
|
|
264
|
-
message: "must be a non-empty string"
|
|
265
|
-
});
|
|
266
|
-
});
|
|
267
|
-
}
|
|
268
|
-
if (!normalizeAppliesTo(value.appliesTo)) issues.push({
|
|
269
|
-
path: "agent.appliesTo",
|
|
270
|
-
message: "must include framework, version, or package"
|
|
271
|
-
});
|
|
272
|
-
}
|
|
273
|
-
for (const field of [
|
|
274
|
-
"prerequisites",
|
|
275
|
-
"files",
|
|
276
|
-
"sideEffects",
|
|
277
|
-
"rollback"
|
|
278
|
-
]) addStringListIssues(issues, value, field);
|
|
279
|
-
if ("commands" in value) if (!Array.isArray(value.commands) || value.commands.length === 0) issues.push({
|
|
280
|
-
path: "agent.commands",
|
|
281
|
-
message: "must be a non-empty array"
|
|
282
|
-
});
|
|
283
|
-
else value.commands.forEach((command, index) => {
|
|
284
|
-
if (normalizeString(command)) return;
|
|
285
|
-
if (!isRecord(command)) {
|
|
286
|
-
issues.push({
|
|
287
|
-
path: `agent.commands[${index}]`,
|
|
288
|
-
message: "must be a command string or an object with a non-empty run field"
|
|
289
|
-
});
|
|
290
|
-
return;
|
|
291
|
-
}
|
|
292
|
-
addUnknownKeyIssues(issues, command, COMMAND_FIELDS, `agent.commands[${index}]`);
|
|
293
|
-
if (!normalizeString(command.run)) {
|
|
294
|
-
issues.push({
|
|
295
|
-
path: `agent.commands[${index}]`,
|
|
296
|
-
message: "must be a command string or an object with a non-empty run field"
|
|
297
|
-
});
|
|
298
|
-
return;
|
|
299
|
-
}
|
|
300
|
-
for (const field of ["cwd", "description"]) if (field in command && !normalizeString(command[field])) issues.push({
|
|
301
|
-
path: `agent.commands[${index}].${field}`,
|
|
302
|
-
message: "must be a non-empty string"
|
|
303
|
-
});
|
|
304
|
-
});
|
|
305
|
-
if ("verification" in value) if (!Array.isArray(value.verification) || value.verification.length === 0) issues.push({
|
|
306
|
-
path: "agent.verification",
|
|
307
|
-
message: "must be a non-empty array"
|
|
308
|
-
});
|
|
309
|
-
else value.verification.forEach((step, index) => {
|
|
310
|
-
if (normalizeString(step)) return;
|
|
311
|
-
if (!isRecord(step)) {
|
|
312
|
-
issues.push({
|
|
313
|
-
path: `agent.verification[${index}]`,
|
|
314
|
-
message: "must be a string or an object with description, run, or expect"
|
|
315
|
-
});
|
|
316
|
-
return;
|
|
317
|
-
}
|
|
318
|
-
addUnknownKeyIssues(issues, step, VERIFICATION_FIELDS, `agent.verification[${index}]`);
|
|
319
|
-
if (!normalizeVerification([step])) {
|
|
320
|
-
issues.push({
|
|
321
|
-
path: `agent.verification[${index}]`,
|
|
322
|
-
message: "must be a string or an object with description, run, or expect"
|
|
323
|
-
});
|
|
324
|
-
return;
|
|
325
|
-
}
|
|
326
|
-
for (const field of [
|
|
327
|
-
"description",
|
|
328
|
-
"run",
|
|
329
|
-
"expect"
|
|
330
|
-
]) if (field in step && !normalizeString(step[field])) issues.push({
|
|
331
|
-
path: `agent.verification[${index}].${field}`,
|
|
332
|
-
message: "must be a non-empty string"
|
|
333
|
-
});
|
|
334
|
-
});
|
|
335
|
-
if ("failureModes" in value) if (!Array.isArray(value.failureModes) || value.failureModes.length === 0) issues.push({
|
|
336
|
-
path: "agent.failureModes",
|
|
337
|
-
message: "must be a non-empty array"
|
|
338
|
-
});
|
|
339
|
-
else value.failureModes.forEach((mode, index) => {
|
|
340
|
-
if (normalizeString(mode)) return;
|
|
341
|
-
if (!isRecord(mode)) {
|
|
342
|
-
issues.push({
|
|
343
|
-
path: `agent.failureModes[${index}]`,
|
|
344
|
-
message: "must be a string or an object with a non-empty symptom field"
|
|
345
|
-
});
|
|
346
|
-
return;
|
|
347
|
-
}
|
|
348
|
-
addUnknownKeyIssues(issues, mode, FAILURE_MODE_FIELDS, `agent.failureModes[${index}]`);
|
|
349
|
-
if (!normalizeString(mode.symptom)) {
|
|
350
|
-
issues.push({
|
|
351
|
-
path: `agent.failureModes[${index}]`,
|
|
352
|
-
message: "must be a string or an object with a non-empty symptom field"
|
|
353
|
-
});
|
|
354
|
-
return;
|
|
355
|
-
}
|
|
356
|
-
if ("resolution" in mode && !normalizeString(mode.resolution)) issues.push({
|
|
357
|
-
path: `agent.failureModes[${index}].resolution`,
|
|
358
|
-
message: "must be a non-empty string"
|
|
359
|
-
});
|
|
360
|
-
});
|
|
361
|
-
return issues;
|
|
362
|
-
}
|
|
363
|
-
function yamlString(value) {
|
|
364
|
-
return JSON.stringify(value);
|
|
365
|
-
}
|
|
366
|
-
function renderYamlStringList(lines, indent, key, values) {
|
|
367
|
-
lines.push(`${indent}${key}:`);
|
|
368
|
-
for (const value of values) lines.push(`${indent} - ${yamlString(value)}`);
|
|
369
|
-
}
|
|
370
|
-
function renderYamlObjectList(lines, indent, key, values, fields) {
|
|
371
|
-
lines.push(`${indent}${key}:`);
|
|
372
|
-
for (const value of values) {
|
|
373
|
-
if (typeof value === "string") {
|
|
374
|
-
lines.push(`${indent} - ${yamlString(value)}`);
|
|
375
|
-
continue;
|
|
376
|
-
}
|
|
377
|
-
const record = value;
|
|
378
|
-
const firstField = fields.find((field) => normalizeString(record[field]));
|
|
379
|
-
if (!firstField) continue;
|
|
380
|
-
lines.push(`${indent} - ${firstField}: ${yamlString(normalizeString(record[firstField]))}`);
|
|
381
|
-
for (const field of fields) {
|
|
382
|
-
const fieldValue = normalizeString(record[field]);
|
|
383
|
-
if (field === firstField || !fieldValue) continue;
|
|
384
|
-
lines.push(`${indent} ${field}: ${yamlString(fieldValue)}`);
|
|
385
|
-
}
|
|
386
|
-
}
|
|
387
|
-
}
|
|
388
|
-
/** Render normalized `agent` YAML in a stable field order. */
|
|
389
|
-
function renderPageAgentFrontmatterYamlLines(value, indentation = 0) {
|
|
390
|
-
const agent = normalizePageAgentFrontmatter(value);
|
|
391
|
-
if (!agent) return [];
|
|
392
|
-
const indent = " ".repeat(Math.max(0, indentation));
|
|
393
|
-
const child = `${indent} `;
|
|
394
|
-
const lines = [`${indent}agent:`];
|
|
395
|
-
if (agent.tokenBudget !== void 0) lines.push(`${child}tokenBudget: ${agent.tokenBudget}`);
|
|
396
|
-
if (agent.task) lines.push(`${child}task: ${yamlString(agent.task)}`);
|
|
397
|
-
if (agent.outcome) lines.push(`${child}outcome: ${yamlString(agent.outcome)}`);
|
|
398
|
-
if (agent.appliesTo) {
|
|
399
|
-
lines.push(`${child}appliesTo:`);
|
|
400
|
-
const appliesIndent = `${child} `;
|
|
401
|
-
for (const field of [
|
|
402
|
-
"framework",
|
|
403
|
-
"version",
|
|
404
|
-
"package"
|
|
405
|
-
]) {
|
|
406
|
-
const values = normalizeStringOrList(agent.appliesTo[field]);
|
|
407
|
-
if (values) renderYamlStringList(lines, appliesIndent, field, values);
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
if (agent.prerequisites) renderYamlStringList(lines, child, "prerequisites", agent.prerequisites);
|
|
411
|
-
if (agent.files) renderYamlStringList(lines, child, "files", agent.files);
|
|
412
|
-
if (agent.commands) renderYamlObjectList(lines, child, "commands", agent.commands, [
|
|
413
|
-
"run",
|
|
414
|
-
"cwd",
|
|
415
|
-
"description"
|
|
416
|
-
]);
|
|
417
|
-
if (agent.sideEffects) renderYamlStringList(lines, child, "sideEffects", agent.sideEffects);
|
|
418
|
-
if (agent.verification) renderYamlObjectList(lines, child, "verification", agent.verification, [
|
|
419
|
-
"description",
|
|
420
|
-
"run",
|
|
421
|
-
"expect"
|
|
422
|
-
]);
|
|
423
|
-
if (agent.rollback) renderYamlStringList(lines, child, "rollback", agent.rollback);
|
|
424
|
-
if (agent.failureModes) renderYamlObjectList(lines, child, "failureModes", agent.failureModes, ["symptom", "resolution"]);
|
|
425
|
-
return lines;
|
|
426
|
-
}
|
|
427
|
-
function inlineCode(value) {
|
|
428
|
-
const longestRun = Math.max(0, ...[...value.matchAll(/`+/g)].map((match) => match[0].length));
|
|
429
|
-
const fence = "`".repeat(longestRun + 1);
|
|
430
|
-
const padding = value.startsWith("`") || value.endsWith("`") ? " " : "";
|
|
431
|
-
return `${fence}${padding}${value}${padding}${fence}`;
|
|
432
|
-
}
|
|
433
|
-
function renderTextList(lines, title, values, code = false) {
|
|
434
|
-
lines.push("", `### ${title}`, "");
|
|
435
|
-
for (const value of values) lines.push(`- ${code ? inlineCode(value) : value}`);
|
|
436
|
-
}
|
|
437
|
-
/** Render the structured contract as deterministic, compact machine-readable Markdown. */
|
|
438
|
-
function renderPageAgentContractMarkdown(value) {
|
|
439
|
-
const agent = normalizePageAgentFrontmatter(value);
|
|
440
|
-
if (!agent || !hasStructuredPageAgentContract(agent)) return "";
|
|
441
|
-
const lines = [PAGE_AGENT_CONTRACT_START_MARKER, "## Agent Contract"];
|
|
442
|
-
if (agent.task) lines.push("", `Task: ${agent.task}`);
|
|
443
|
-
if (agent.outcome) lines.push(`Outcome: ${agent.outcome}`);
|
|
444
|
-
if (agent.appliesTo) {
|
|
445
|
-
lines.push("", "### Applies To", "");
|
|
446
|
-
for (const [label, field] of [
|
|
447
|
-
["Framework", "framework"],
|
|
448
|
-
["Version", "version"],
|
|
449
|
-
["Package", "package"]
|
|
450
|
-
]) {
|
|
451
|
-
const values = normalizeStringOrList(agent.appliesTo[field]);
|
|
452
|
-
if (values) lines.push(`- ${label}: ${values.map(inlineCode).join(", ")}`);
|
|
453
|
-
}
|
|
454
|
-
}
|
|
455
|
-
if (agent.prerequisites) renderTextList(lines, "Prerequisites", agent.prerequisites);
|
|
456
|
-
if (agent.files) renderTextList(lines, "Files", agent.files, true);
|
|
457
|
-
if (agent.commands) {
|
|
458
|
-
lines.push("", "### Commands", "");
|
|
459
|
-
for (const command of agent.commands) {
|
|
460
|
-
if (typeof command === "string") {
|
|
461
|
-
lines.push(`- ${inlineCode(command)}`);
|
|
462
|
-
continue;
|
|
463
|
-
}
|
|
464
|
-
const details = [command.cwd ? `cwd ${inlineCode(command.cwd)}` : void 0, command.description].filter(Boolean);
|
|
465
|
-
lines.push(`- ${inlineCode(command.run)}${details.length ? ` — ${details.join("; ")}` : ""}`);
|
|
466
|
-
}
|
|
467
|
-
}
|
|
468
|
-
if (agent.sideEffects) renderTextList(lines, "Side Effects", agent.sideEffects);
|
|
469
|
-
if (agent.verification) {
|
|
470
|
-
lines.push("", "### Verification", "");
|
|
471
|
-
for (const step of agent.verification) {
|
|
472
|
-
if (typeof step === "string") {
|
|
473
|
-
lines.push(`- ${step}`);
|
|
474
|
-
continue;
|
|
475
|
-
}
|
|
476
|
-
const summary = step.description ?? (step.run ? `Run ${inlineCode(step.run)}` : step.expect ?? "Verification step");
|
|
477
|
-
lines.push(`- ${summary}`);
|
|
478
|
-
if (step.run && step.description) lines.push(` - Run: ${inlineCode(step.run)}`);
|
|
479
|
-
if (step.expect && step.expect !== summary) lines.push(` - Expected: ${step.expect}`);
|
|
480
|
-
}
|
|
481
|
-
}
|
|
482
|
-
if (agent.rollback) renderTextList(lines, "Rollback", agent.rollback);
|
|
483
|
-
if (agent.failureModes) {
|
|
484
|
-
lines.push("", "### Failure Modes", "");
|
|
485
|
-
for (const mode of agent.failureModes) {
|
|
486
|
-
if (typeof mode === "string") {
|
|
487
|
-
lines.push(`- ${mode}`);
|
|
488
|
-
continue;
|
|
489
|
-
}
|
|
490
|
-
lines.push(`- ${mode.symptom}${mode.resolution ? ` — Recovery: ${mode.resolution}` : ""}`);
|
|
491
|
-
}
|
|
492
|
-
}
|
|
493
|
-
lines.push(PAGE_AGENT_CONTRACT_END_MARKER);
|
|
494
|
-
return lines.join("\n");
|
|
495
|
-
}
|
|
496
|
-
/** Remove generated contract blocks while preserving handwritten guidance. */
|
|
497
|
-
function stripGeneratedPageAgentContractMarkdown(markdown) {
|
|
498
|
-
const newline = markdown.includes("\r\n") ? "\r\n" : "\n";
|
|
499
|
-
const output = [];
|
|
500
|
-
let fence;
|
|
501
|
-
let pendingBlock;
|
|
502
|
-
let skipEmptySeparator = false;
|
|
503
|
-
for (const line of markdown.split(/\r?\n/)) {
|
|
504
|
-
if (pendingBlock) {
|
|
505
|
-
pendingBlock.push(line);
|
|
506
|
-
if (isGeneratedAgentContractMarker(line, PAGE_AGENT_CONTRACT_END_MARKER)) {
|
|
507
|
-
pendingBlock = void 0;
|
|
508
|
-
skipEmptySeparator = true;
|
|
509
|
-
}
|
|
510
|
-
continue;
|
|
511
|
-
}
|
|
512
|
-
if (!fence && isGeneratedAgentContractMarker(line, PAGE_AGENT_CONTRACT_START_MARKER)) {
|
|
513
|
-
pendingBlock = [line];
|
|
514
|
-
continue;
|
|
515
|
-
}
|
|
516
|
-
if (skipEmptySeparator && line.length === 0) continue;
|
|
517
|
-
skipEmptySeparator = false;
|
|
518
|
-
output.push(line);
|
|
519
|
-
fence = advanceMarkdownFence(line, fence);
|
|
520
|
-
}
|
|
521
|
-
if (pendingBlock) output.push(...pendingBlock);
|
|
522
|
-
while (output[0] === "") output.shift();
|
|
523
|
-
return output.join(newline);
|
|
524
|
-
}
|
|
525
|
-
function isGeneratedAgentContractMarker(line, marker) {
|
|
526
|
-
if (line.trim() !== marker) return false;
|
|
527
|
-
const markerIndex = line.indexOf(marker);
|
|
528
|
-
return markerIndex <= 3 && /^ *$/.test(line.slice(0, markerIndex));
|
|
529
|
-
}
|
|
530
|
-
function advanceMarkdownFence(line, fence) {
|
|
531
|
-
if (fence) {
|
|
532
|
-
const closing = /^ {0,3}(`+|~+)[\t ]*$/.exec(line)?.[1];
|
|
533
|
-
if (closing?.[0] === fence.character && closing.length >= fence.length) return;
|
|
534
|
-
return fence;
|
|
535
|
-
}
|
|
536
|
-
const opening = /^ {0,3}(`{3,}|~{3,})(.*)$/.exec(line);
|
|
537
|
-
if (!opening?.[1]) return void 0;
|
|
538
|
-
if (opening[1][0] === "`" && opening[2]?.includes("`")) return void 0;
|
|
539
|
-
return {
|
|
540
|
-
character: opening[1][0],
|
|
541
|
-
length: opening[1].length
|
|
542
|
-
};
|
|
543
|
-
}
|
|
544
|
-
function isAgentContractHeadingText(value) {
|
|
545
|
-
return value.trim().toLowerCase() === "agent contract";
|
|
546
|
-
}
|
|
547
|
-
function hasPageAgentContractHeading(markdown) {
|
|
548
|
-
let fence;
|
|
549
|
-
let previousLine;
|
|
550
|
-
for (const line of markdown.split(/\r?\n/)) {
|
|
551
|
-
const nextFence = advanceMarkdownFence(line, fence);
|
|
552
|
-
if (fence || nextFence) {
|
|
553
|
-
fence = nextFence;
|
|
554
|
-
previousLine = void 0;
|
|
555
|
-
continue;
|
|
556
|
-
}
|
|
557
|
-
const atxText = /^ {0,3}#{1,6}(?:[\t ]+|$)(.*)$/.exec(line)?.[1]?.replace(/[\t ]+#+[\t ]*$/, "").trim();
|
|
558
|
-
if (atxText && isAgentContractHeadingText(atxText)) return true;
|
|
559
|
-
if (previousLine && /^ {0,3}(?:=+|-+)[\t ]*$/.test(line) && isAgentContractHeadingText(previousLine)) return true;
|
|
560
|
-
previousLine = line.trim().length > 0 && !/^ {4}/.test(line) && !/^ {0,3}\t/.test(line) ? line : void 0;
|
|
561
|
-
}
|
|
562
|
-
return false;
|
|
563
|
-
}
|
|
564
|
-
/** Insert one generated contract unless the document already has handwritten contract guidance. */
|
|
565
|
-
function upsertPageAgentContractMarkdown(markdown, value) {
|
|
566
|
-
const cleaned = stripGeneratedPageAgentContractMarkdown(markdown);
|
|
567
|
-
const contract = renderPageAgentContractMarkdown(value);
|
|
568
|
-
if (!contract || hasPageAgentContractHeading(cleaned)) return cleaned;
|
|
569
|
-
return cleaned ? `${contract}\n\n${cleaned.replace(/^\r?\n+/, "")}` : contract;
|
|
570
|
-
}
|
|
571
|
-
|
|
572
|
-
//#endregion
|
|
573
|
-
export { normalizePageAgentFrontmatter as a, stripGeneratedPageAgentContractMarkdown as c, hasStructuredPageAgentContract as i, upsertPageAgentContractMarkdown as l, PAGE_AGENT_CONTRACT_START_MARKER as n, renderPageAgentContractMarkdown as o, getPageAgentFrontmatterIssues as r, renderPageAgentFrontmatterYamlLines as s, PAGE_AGENT_CONTRACT_END_MARKER as t };
|