hoi4-agent-tools 4.20.2 → 4.21.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/CHANGELOG.md +22 -0
- package/dist/bin/stdio.js +5 -2
- package/dist/hoi4_agent_tools/characters/write-operations.js +2 -2
- package/dist/hoi4_agent_tools/characters/write-plan.js +21 -205
- package/dist/hoi4_agent_tools/core/artifacts-engine.js +272 -0
- package/dist/hoi4_agent_tools/core/configuration.js +6 -0
- package/dist/hoi4_agent_tools/core/engine.js +77 -17
- package/dist/hoi4_agent_tools/core/idle-cache-lifetime.js +7 -0
- package/dist/hoi4_agent_tools/core/operation-tasks.js +3 -1
- package/dist/hoi4_agent_tools/core/transactions.js +6 -0
- package/dist/hoi4_agent_tools/core/workspace.js +130 -19
- package/dist/hoi4_agent_tools/decision/write-operations.js +7 -8
- package/dist/hoi4_agent_tools/decision/write-plan.js +25 -245
- package/dist/hoi4_agent_tools/doctrine/write-operations.js +2 -2
- package/dist/hoi4_agent_tools/doctrine/write-plan.js +21 -180
- package/dist/hoi4_agent_tools/event/graph.js +3 -3
- package/dist/hoi4_agent_tools/event/job-operations.js +2 -2
- package/dist/hoi4_agent_tools/event/write-job-operations.js +2 -2
- package/dist/hoi4_agent_tools/event/write-operations.js +9 -10
- package/dist/hoi4_agent_tools/event/write-plan.js +28 -278
- package/dist/hoi4_agent_tools/focus/job-operations.js +2 -2
- package/dist/hoi4_agent_tools/gui/assets.js +3 -13
- package/dist/hoi4_agent_tools/gui/job-operations.js +2 -2
- package/dist/hoi4_agent_tools/gui/layout.js +2 -4
- package/dist/hoi4_agent_tools/gui/renderer.js +2 -1
- package/dist/hoi4_agent_tools/gui/source-graph.js +16 -0
- package/dist/hoi4_agent_tools/gui/studio.js +31 -0
- package/dist/hoi4_agent_tools/ideas/write-operations.js +7 -8
- package/dist/hoi4_agent_tools/ideas/write-plan.js +24 -304
- package/dist/hoi4_agent_tools/map/engine-validation.js +230 -0
- package/dist/hoi4_agent_tools/map/job-operations.js +2 -2
- package/dist/hoi4_agent_tools/map/model.js +17 -3
- package/dist/hoi4_agent_tools/map/validation.js +12 -18
- package/dist/hoi4_agent_tools/mcp/server/create-modern-operations.js +9 -0
- package/dist/hoi4_agent_tools/mcp/server/request-lifecycle.js +4 -0
- package/dist/hoi4_agent_tools/mcp/server/startup-failure.js +19 -0
- package/dist/hoi4_agent_tools/mcp/transports/negotiated-stdio.js +24 -16
- package/dist/hoi4_agent_tools/probability/job-operations.js +2 -2
- package/dist/hoi4_agent_tools/runtime.js +85 -19
- package/dist/hoi4_agent_tools/schemas/transaction.js +3 -2
- package/dist/hoi4_agent_tools/script-write/engine-plan.js +45 -0
- package/dist/hoi4_agent_tools/technology/engine-graph.js +156 -0
- package/dist/hoi4_agent_tools/technology/job-operations.js +2 -2
- package/dist/hoi4_agent_tools/technology/operations.js +1 -2
- package/dist/hoi4_agent_tools/technology/service.js +30 -59
- package/dist/hoi4_agent_tools/technology/source-analysis.js +3 -1
- package/dist/hoi4_agent_tools/technology/write-job-operations.js +2 -2
- package/dist/hoi4_agent_tools/technology/write-operations.js +2 -2
- package/dist/hoi4_agent_tools/technology/write-plan.js +21 -207
- package/dist/hoi4_agent_tools/version.js +1 -1
- package/docs/configuration.md +11 -10
- package/docs/troubleshooting.md +11 -11
- package/engine/bin/darwin-arm64/hoi4-engine +0 -0
- package/engine/bin/linux-arm64/hoi4-engine +0 -0
- package/engine/bin/linux-x64/hoi4-engine +0 -0
- package/engine/bin/manifest.json +8 -8
- package/engine/bin/win32-x64/hoi4-engine.exe +0 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +2 -2
- package/schemas/configuration.schema.json +143 -134
- package/schemas/continuous-focus-palette.schema.json +1 -1
- package/schemas/custom-weighted-pool.schema.json +1 -1
- package/schemas/decision-inspect-input.schema.json +1 -1
- package/schemas/error-log-input.schema.json +1 -1
- package/schemas/event-feature-manifest.schema.json +1 -1
- package/schemas/focus-plan.schema.json +1 -1
- package/schemas/focus-planning-sidecar.schema.json +1 -1
- package/schemas/geography-model.schema.json +1 -1
- package/schemas/gui-animation-source.schema.json +1 -1
- package/schemas/gui-helper.schema.json +1 -1
- package/schemas/gui-scenario.schema.json +1 -1
- package/schemas/helper-expansion-request.schema.json +1 -1
- package/schemas/helper-expansion-summary.schema.json +1 -1
- package/schemas/impact-inspect-input.schema.json +1 -1
- package/schemas/map-operation.schema.json +1 -1
- package/schemas/mechanic-test-input.schema.json +1 -1
- package/schemas/mod-index-input.schema.json +1 -1
- package/schemas/operation-result.schema.json +1 -1
- package/schemas/package-check-input.schema.json +1 -1
- package/schemas/package-manifest.schema.json +1 -1
- package/schemas/probability-analysis-result.schema.json +1 -1
- package/schemas/probability-compare-input.schema.json +1 -1
- package/schemas/probability-evaluate-input.schema.json +1 -1
- package/schemas/probability-inspect-input.schema.json +1 -1
- package/schemas/probability-render-input.schema.json +1 -1
- package/schemas/probability-scenario-set.schema.json +1 -1
- package/schemas/probability-sequence-input.schema.json +1 -1
- package/schemas/probability-simulate-input.schema.json +1 -1
- package/schemas/probability-sweep-input.schema.json +1 -1
- package/schemas/reference-context-input.schema.json +1 -1
- package/schemas/reference-read-input.schema.json +1 -1
- package/schemas/reference-search-input.schema.json +1 -1
- package/schemas/reference-sheets.schema.json +1 -1
- package/schemas/scenario-suite.schema.json +1 -1
- package/schemas/scenario-test-input.schema.json +1 -1
- package/schemas/script-validate-input.schema.json +1 -1
- package/schemas/shared-scenario.schema.json +1 -1
- package/schemas/source-lookup-input.schema.json +1 -1
- package/server.json +2 -2
|
@@ -1,252 +1,32 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
function
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
: []);
|
|
21
|
-
}
|
|
22
|
-
/** One level of indentation, read from the file's own decisions, tabs when it has none. */
|
|
23
|
-
function indentUnit(document, located) {
|
|
24
|
-
for (const { decision, category } of located) {
|
|
25
|
-
const outer = lineIndent(document.text, category.start);
|
|
26
|
-
const inner = lineIndent(document.text, decision.start);
|
|
27
|
-
if (inner.length > outer.length && inner.startsWith(outer))
|
|
28
|
-
return inner.slice(outer.length);
|
|
29
|
-
}
|
|
30
|
-
return '\t';
|
|
31
|
-
}
|
|
32
|
-
/** The body, checked on its own as the block it will become. */
|
|
33
|
-
function assertBodyIsOneBlock(id, body) {
|
|
34
|
-
const probe = parseClausewitz(Buffer.from(`${id} = {\n${body}\n}\n`, 'utf8'), 'decision-body');
|
|
35
|
-
const only = probe.root.entries;
|
|
36
|
-
const single = only.length === 1 && only[0]?.type === 'assignment' && only[0].key.value === id;
|
|
37
|
-
if (hasBlockingDiagnostics(probe.diagnostics) || !single || only[0]?.type !== 'assignment')
|
|
38
|
-
fail('DECISION_WRITE_BODY_INVALID', 'The body must be the text between the braces of one decision: balanced braces and no definition of its own', { diagnosticCodes: probe.diagnostics.map(({ code }) => code) });
|
|
39
|
-
if (only[0].value.type !== 'block' || only[0].value.entries.length === 0)
|
|
40
|
-
fail('DECISION_WRITE_BODY_INVALID', 'The body of a decision cannot be empty');
|
|
41
|
-
}
|
|
42
|
-
function definitionTexts(document, skip) {
|
|
43
|
-
const byKey = new Map();
|
|
44
|
-
for (const { decision, category } of definitions(document)) {
|
|
45
|
-
if (decision.start === skip?.start)
|
|
46
|
-
continue;
|
|
47
|
-
const key = `${category.key.value}:${decision.key.value}`;
|
|
48
|
-
byKey.set(key, [...(byKey.get(key) ?? []), document.text.slice(decision.start, decision.end)]);
|
|
49
|
-
}
|
|
50
|
-
return byKey;
|
|
51
|
-
}
|
|
52
|
-
function checkRequest(input) {
|
|
53
|
-
if (!ID_PATTERN.test(input.id))
|
|
54
|
-
fail('DECISION_WRITE_ID_INVALID', `${JSON.stringify(input.id)} is not a valid decision id`, {
|
|
55
|
-
id: input.id,
|
|
56
|
-
});
|
|
57
|
-
if (input.operation === 'remove' && input.body !== undefined)
|
|
58
|
-
fail('DECISION_WRITE_BODY_UNEXPECTED', 'remove takes no body');
|
|
59
|
-
if (input.operation !== 'remove' && (input.body === undefined || input.body.trim() === ''))
|
|
60
|
-
fail('DECISION_WRITE_BODY_REQUIRED', `${input.operation} needs the body of the decision`);
|
|
61
|
-
if (input.operation !== 'add' && input.createCategory === true)
|
|
62
|
-
fail('DECISION_WRITE_CATEGORY_UNEXPECTED', 'createCategory applies to add only');
|
|
63
|
-
if (input.category !== undefined && !ID_PATTERN.test(input.category))
|
|
64
|
-
fail('DECISION_WRITE_CATEGORY_INVALID', `${JSON.stringify(input.category)} is not a valid category name`, { category: input.category });
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
import { callSourceQuery, callWritePlan, replacementSchema, } from '../script-write/engine-plan.js';
|
|
3
|
+
const planSchema = z.object({
|
|
4
|
+
operation: z.enum(['add', 'replace', 'remove']),
|
|
5
|
+
id: z.string(),
|
|
6
|
+
category: z.string(),
|
|
7
|
+
path: z.string(),
|
|
8
|
+
edit: replacementSchema,
|
|
9
|
+
untouchedDefinitions: z.number().int().nonnegative(),
|
|
10
|
+
});
|
|
11
|
+
async function plan(path, input, source) {
|
|
12
|
+
const answer = await callWritePlan({
|
|
13
|
+
op: 'decision_write.plan',
|
|
14
|
+
path,
|
|
15
|
+
input: { ...input },
|
|
16
|
+
...(source === undefined ? {} : { source }),
|
|
17
|
+
result: planSchema,
|
|
18
|
+
});
|
|
19
|
+
return { ...answer.result, text: answer.text, bytes: answer.bytes, diff: answer.diff };
|
|
65
20
|
}
|
|
66
|
-
/**
|
|
67
|
-
* The plan that creates a new `common/decisions` file holding one category block with one new
|
|
68
|
-
* decision, for `createFile: true`. The file is UTF-8 without a byte order mark and uses tabs and LF.
|
|
69
|
-
*/
|
|
21
|
+
/** The plan that creates a new file holding one new decision, for `createFile: true`. */
|
|
70
22
|
export function planNewDecisionFile(path, input) {
|
|
71
|
-
|
|
72
|
-
if (input.operation !== 'add')
|
|
73
|
-
fail('DECISION_WRITE_FILE_NOT_FOUND', `${path} does not exist, and only add can create a file`);
|
|
74
|
-
if (input.category === undefined)
|
|
75
|
-
fail('DECISION_WRITE_CATEGORY_REQUIRED', 'a new file needs the category block the decision goes in');
|
|
76
|
-
assertBodyIsOneBlock(input.id, input.body);
|
|
77
|
-
const text = [
|
|
78
|
-
`${input.category} = {`,
|
|
79
|
-
`\t${input.id} = {`,
|
|
80
|
-
indentedBody(input.body, '\t\t', '\n'),
|
|
81
|
-
'\t}',
|
|
82
|
-
'}',
|
|
83
|
-
'',
|
|
84
|
-
].join('\n');
|
|
85
|
-
const written = parseClausewitz(Buffer.from(text, 'utf8'), path);
|
|
86
|
-
if (hasBlockingDiagnostics(written.diagnostics))
|
|
87
|
-
fail('DECISION_WRITE_RESULT_INVALID', 'The new file would have syntax errors', {
|
|
88
|
-
diagnosticCodes: written.diagnostics.map(({ code }) => code),
|
|
89
|
-
});
|
|
90
|
-
return {
|
|
91
|
-
operation: 'add',
|
|
92
|
-
id: input.id,
|
|
93
|
-
category: input.category,
|
|
94
|
-
path,
|
|
95
|
-
text,
|
|
96
|
-
bytes: Buffer.from(text, 'utf8'),
|
|
97
|
-
edit: { start: 0, end: 0, text, description: `create ${path} with ${input.id}` },
|
|
98
|
-
diff: unifiedTextDiff('', text, `a/${path}`, `b/${path}`, { context: DIFF_CONTEXT_LINES }),
|
|
99
|
-
untouchedDefinitions: 0,
|
|
100
|
-
};
|
|
23
|
+
return plan(path, input, undefined);
|
|
101
24
|
}
|
|
102
25
|
export function planDecisionWrite(source, input) {
|
|
103
|
-
|
|
104
|
-
const document = parseClausewitz(source.bytes, source.path);
|
|
105
|
-
if (hasBlockingDiagnostics(document.diagnostics))
|
|
106
|
-
fail('DECISION_WRITE_SOURCE_INVALID', 'The source has syntax errors and cannot be edited safely', { path: source.path, diagnosticCodes: document.diagnostics.map(({ code }) => code) });
|
|
107
|
-
const text = document.text;
|
|
108
|
-
const newline = document.newline;
|
|
109
|
-
const located = definitions(document);
|
|
110
|
-
if (input.body !== undefined)
|
|
111
|
-
assertBodyIsOneBlock(input.id, input.body);
|
|
112
|
-
const unit = indentUnit(document, located);
|
|
113
|
-
const existing = located.filter(({ decision }) => decision.key.value === input.id);
|
|
114
|
-
let edit;
|
|
115
|
-
let category;
|
|
116
|
-
let skip;
|
|
117
|
-
if (input.operation === 'add') {
|
|
118
|
-
if (existing.length > 0)
|
|
119
|
-
fail('DECISION_WRITE_DUPLICATE_ID', `${input.id} is already defined in this file`, {
|
|
120
|
-
id: input.id,
|
|
121
|
-
category: existing[0]?.category.key.value,
|
|
122
|
-
line: existing[0]?.decision.key.token.line,
|
|
123
|
-
});
|
|
124
|
-
if (input.category === undefined)
|
|
125
|
-
fail('DECISION_WRITE_CATEGORY_REQUIRED', 'add needs the category block the decision goes in');
|
|
126
|
-
const wanted = input.category;
|
|
127
|
-
const matching = categoryBlocks(document).filter(({ key }) => key.value === wanted);
|
|
128
|
-
if (matching.length === 0 && input.createCategory !== true)
|
|
129
|
-
fail('DECISION_WRITE_CATEGORY_NOT_FOUND', `The file has no ${wanted} category block; pass createCategory: true to create it`, {
|
|
130
|
-
category: wanted,
|
|
131
|
-
categories: [...new Set(categoryBlocks(document).map(({ key }) => key.value))],
|
|
132
|
-
});
|
|
133
|
-
if (matching.length > 1)
|
|
134
|
-
fail('DECISION_WRITE_CATEGORY_AMBIGUOUS', `The file has ${matching.length} ${wanted} category blocks`, { category: wanted, lines: matching.map(({ key }) => key.token.line) });
|
|
135
|
-
if (matching.length === 0) {
|
|
136
|
-
const lead = text === '' ? '' : `${text.endsWith('\n') ? '' : newline}${newline}`;
|
|
137
|
-
edit = {
|
|
138
|
-
start: text.length,
|
|
139
|
-
end: text.length,
|
|
140
|
-
text: `${lead}${wanted} = {${newline}${unit}${input.id} = {${newline}${indentedBody(input.body, unit + unit, newline)}${newline}${unit}}${newline}}${newline}`,
|
|
141
|
-
description: `add ${input.id} in the new category ${wanted}`,
|
|
142
|
-
};
|
|
143
|
-
}
|
|
144
|
-
else {
|
|
145
|
-
const target = matching[0];
|
|
146
|
-
const block = target.value;
|
|
147
|
-
const close = block.close;
|
|
148
|
-
if (close === undefined)
|
|
149
|
-
fail('DECISION_WRITE_LAYOUT_UNSUPPORTED', `The ${wanted} category has no closing brace`);
|
|
150
|
-
const closeLineStart = text.lastIndexOf('\n', close.start - 1) + 1;
|
|
151
|
-
if (!/^[ \t]*$/u.test(text.slice(closeLineStart, close.start)))
|
|
152
|
-
fail('DECISION_WRITE_LAYOUT_UNSUPPORTED', `The closing brace of ${wanted} shares its line with other text, so the new decision has no line to go on`, { category: wanted, line: close.line });
|
|
153
|
-
const decisionIndent = lineIndent(text, target.start) + unit;
|
|
154
|
-
const decisionText = `${decisionIndent}${input.id} = {${newline}${indentedBody(input.body, decisionIndent + unit, newline)}${newline}${decisionIndent}}${newline}`;
|
|
155
|
-
edit = {
|
|
156
|
-
start: closeLineStart,
|
|
157
|
-
end: closeLineStart,
|
|
158
|
-
text: `${block.entries.length > 0 ? newline : ''}${decisionText}`,
|
|
159
|
-
description: `add ${input.id} to ${wanted}`,
|
|
160
|
-
};
|
|
161
|
-
}
|
|
162
|
-
category = wanted;
|
|
163
|
-
}
|
|
164
|
-
else {
|
|
165
|
-
if (existing.length === 0)
|
|
166
|
-
fail('DECISION_WRITE_NOT_FOUND', `${input.id} is not defined in this file`, {
|
|
167
|
-
id: input.id,
|
|
168
|
-
});
|
|
169
|
-
if (existing.length > 1)
|
|
170
|
-
fail('DECISION_WRITE_DUPLICATE_ID', `${input.id} is defined ${existing.length} times in this file`, { id: input.id, lines: existing.map(({ decision }) => decision.key.token.line) });
|
|
171
|
-
const found = existing[0];
|
|
172
|
-
category = found.category.key.value;
|
|
173
|
-
if (input.category !== undefined && input.category !== category)
|
|
174
|
-
fail('DECISION_WRITE_CATEGORY_MISMATCH', `${input.id} is in ${category}, and moving a decision to another category is not supported`, { id: input.id, category });
|
|
175
|
-
skip = found.decision;
|
|
176
|
-
const decisionIndent = lineIndent(text, found.decision.start);
|
|
177
|
-
if (input.operation === 'replace') {
|
|
178
|
-
edit = {
|
|
179
|
-
start: found.decision.start,
|
|
180
|
-
end: found.decision.end,
|
|
181
|
-
text: `${input.id} = {${newline}${indentedBody(input.body, decisionIndent + unit, newline)}${newline}${decisionIndent}}`,
|
|
182
|
-
description: `replace ${input.id}`,
|
|
183
|
-
};
|
|
184
|
-
}
|
|
185
|
-
else {
|
|
186
|
-
const lineStart = text.lastIndexOf('\n', found.decision.start - 1) + 1;
|
|
187
|
-
const lineEndAt = text.indexOf('\n', found.decision.end);
|
|
188
|
-
const lineEnd = lineEndAt === -1 ? text.length : lineEndAt + 1;
|
|
189
|
-
const alone = /^[ \t]*$/u.test(text.slice(lineStart, found.decision.start)) &&
|
|
190
|
-
/^[ \t]*\r?$/u.test(text.slice(found.decision.end, lineEndAt === -1 ? text.length : lineEndAt));
|
|
191
|
-
if (!alone) {
|
|
192
|
-
edit = {
|
|
193
|
-
start: found.decision.start,
|
|
194
|
-
end: found.decision.end,
|
|
195
|
-
text: '',
|
|
196
|
-
description: `remove ${input.id}`,
|
|
197
|
-
};
|
|
198
|
-
}
|
|
199
|
-
else {
|
|
200
|
-
// A blank line that only separated this decision from the one before it goes with it.
|
|
201
|
-
const before = text.slice(0, lineStart);
|
|
202
|
-
const blankBefore = /(?:^|\n)[ \t]*\r?\n$/u.test(before) && before.length > 0;
|
|
203
|
-
const after = text.slice(lineEnd);
|
|
204
|
-
const nextIsBlankOrClose = /^[ \t]*(?:\r?\n|\})/u.test(after) || after === '';
|
|
205
|
-
let start = lineStart;
|
|
206
|
-
if (blankBefore && nextIsBlankOrClose)
|
|
207
|
-
start = before.lastIndexOf('\n', before.length - 2) + 1;
|
|
208
|
-
edit = { start, end: lineEnd, text: '', description: `remove ${input.id}` };
|
|
209
|
-
}
|
|
210
|
-
}
|
|
211
|
-
}
|
|
212
|
-
const bytes = applyReplacements(document, [edit]);
|
|
213
|
-
const after = text.slice(0, edit.start) + edit.text + text.slice(edit.end);
|
|
214
|
-
const written = parseClausewitz(bytes, source.path);
|
|
215
|
-
if (hasBlockingDiagnostics(written.diagnostics))
|
|
216
|
-
fail('DECISION_WRITE_RESULT_INVALID', 'The edit would leave the file with syntax errors', {
|
|
217
|
-
diagnosticCodes: written.diagnostics.map(({ code }) => code),
|
|
218
|
-
});
|
|
219
|
-
const before = definitionTexts(document, skip);
|
|
220
|
-
const afterTexts = definitionTexts(written, undefined);
|
|
221
|
-
let untouched = 0;
|
|
222
|
-
for (const [key, texts] of before) {
|
|
223
|
-
const now = afterTexts.get(key) ?? [];
|
|
224
|
-
for (const textOfDecision of texts)
|
|
225
|
-
if (now.includes(textOfDecision))
|
|
226
|
-
untouched += 1;
|
|
227
|
-
else
|
|
228
|
-
fail('DECISION_WRITE_RESULT_INVALID', `The edit would change ${key}, which it must not touch`, { key });
|
|
229
|
-
}
|
|
230
|
-
if (written.text !== after)
|
|
231
|
-
fail('DECISION_WRITE_RESULT_INVALID', 'The written file does not match the planned edit');
|
|
232
|
-
return {
|
|
233
|
-
operation: input.operation,
|
|
234
|
-
id: input.id,
|
|
235
|
-
category,
|
|
236
|
-
path: source.path,
|
|
237
|
-
text: after,
|
|
238
|
-
bytes,
|
|
239
|
-
edit,
|
|
240
|
-
diff: unifiedTextDiff(text, after, `a/${source.path}`, `b/${source.path}`, {
|
|
241
|
-
context: DIFF_CONTEXT_LINES,
|
|
242
|
-
}),
|
|
243
|
-
untouchedDefinitions: untouched,
|
|
244
|
-
};
|
|
26
|
+
return plan(source.path, input, source.bytes);
|
|
245
27
|
}
|
|
246
28
|
/** The category blocks of a `common/decisions` file, or none when the file cannot be parsed. */
|
|
247
|
-
export function decisionCategories(source) {
|
|
248
|
-
const
|
|
249
|
-
|
|
250
|
-
return [];
|
|
251
|
-
return [...new Set(categoryBlocks(document).map(({ key }) => key.value))];
|
|
29
|
+
export async function decisionCategories(source) {
|
|
30
|
+
const answer = await callSourceQuery('decision_write.categories', source.path, source.bytes, z.object({ categories: z.array(z.string()) }));
|
|
31
|
+
return answer.categories;
|
|
252
32
|
}
|
|
@@ -146,7 +146,7 @@ export async function computeDoctrineWrite(engine, input, context, applied) {
|
|
|
146
146
|
let plan;
|
|
147
147
|
if ('file' in target) {
|
|
148
148
|
file = target.file;
|
|
149
|
-
plan = planDoctrineWrite({ bytes: file.bytes, path: file.relativePath }, planInput);
|
|
149
|
+
plan = await planDoctrineWrite({ bytes: file.bytes, path: file.relativePath }, planInput);
|
|
150
150
|
}
|
|
151
151
|
else {
|
|
152
152
|
const inMod = snapshot.index
|
|
@@ -157,7 +157,7 @@ export async function computeDoctrineWrite(engine, input, context, applied) {
|
|
|
157
157
|
id: input.id,
|
|
158
158
|
path: inMod.path,
|
|
159
159
|
});
|
|
160
|
-
plan = planNewDoctrineFile(target.newPath, planInput);
|
|
160
|
+
plan = await planNewDoctrineFile(target.newPath, planInput);
|
|
161
161
|
}
|
|
162
162
|
const missing = {
|
|
163
163
|
localisation: [],
|
|
@@ -1,185 +1,26 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
}
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
const first = doctrine.value.entries[0];
|
|
20
|
-
if (first === undefined)
|
|
21
|
-
continue;
|
|
22
|
-
const inner = lineIndent(text, first.start);
|
|
23
|
-
const outer = lineIndent(text, doctrine.start);
|
|
24
|
-
if (inner.length > outer.length && inner.startsWith(outer))
|
|
25
|
-
return inner.slice(outer.length);
|
|
26
|
-
}
|
|
27
|
-
return '\t';
|
|
28
|
-
}
|
|
29
|
-
/** The body, checked on its own as the block it will become. */
|
|
30
|
-
function assertBodyIsOneBlock(id, body) {
|
|
31
|
-
const probe = parseClausewitz(Buffer.from(`${id} = {\n${body}\n}\n`, 'utf8'), 'doctrine-body');
|
|
32
|
-
const only = probe.root.entries;
|
|
33
|
-
const single = only.length === 1 && only[0]?.type === 'assignment' && only[0].key.value === id;
|
|
34
|
-
if (hasBlockingDiagnostics(probe.diagnostics) || !single || only[0]?.type !== 'assignment')
|
|
35
|
-
fail('DOCTRINE_WRITE_BODY_INVALID', 'The body must be the text between the braces of one doctrine: balanced braces and no definition of its own', { diagnosticCodes: probe.diagnostics.map(({ code }) => code) });
|
|
36
|
-
if (only[0].value.type !== 'block' || only[0].value.entries.length === 0)
|
|
37
|
-
fail('DOCTRINE_WRITE_BODY_INVALID', 'The body of a doctrine cannot be empty');
|
|
38
|
-
}
|
|
39
|
-
function definitionTexts(document, skip) {
|
|
40
|
-
const byKey = new Map();
|
|
41
|
-
for (const doctrine of definitions(document)) {
|
|
42
|
-
if (doctrine.start === skip?.start)
|
|
43
|
-
continue;
|
|
44
|
-
const key = doctrine.key.value;
|
|
45
|
-
byKey.set(key, [...(byKey.get(key) ?? []), document.text.slice(doctrine.start, doctrine.end)]);
|
|
46
|
-
}
|
|
47
|
-
return byKey;
|
|
48
|
-
}
|
|
49
|
-
function checkRequest(input) {
|
|
50
|
-
if (!ID_PATTERN.test(input.id))
|
|
51
|
-
fail('DOCTRINE_WRITE_ID_INVALID', `${JSON.stringify(input.id)} is not a valid doctrine id`, {
|
|
52
|
-
id: input.id,
|
|
53
|
-
});
|
|
54
|
-
if (input.operation === 'remove' && input.body !== undefined)
|
|
55
|
-
fail('DOCTRINE_WRITE_BODY_UNEXPECTED', 'remove takes no body');
|
|
56
|
-
if (input.operation !== 'remove' && (input.body === undefined || input.body.trim() === ''))
|
|
57
|
-
fail('DOCTRINE_WRITE_BODY_REQUIRED', `${input.operation} needs the body of the doctrine`);
|
|
1
|
+
import { z } from 'zod/v4';
|
|
2
|
+
import { callWritePlan, replacementSchema, } from '../script-write/engine-plan.js';
|
|
3
|
+
const planSchema = z.object({
|
|
4
|
+
operation: z.enum(['add', 'replace', 'remove']),
|
|
5
|
+
id: z.string(),
|
|
6
|
+
path: z.string(),
|
|
7
|
+
edit: replacementSchema,
|
|
8
|
+
untouchedDefinitions: z.number().int().nonnegative(),
|
|
9
|
+
});
|
|
10
|
+
async function plan(path, input, source) {
|
|
11
|
+
const answer = await callWritePlan({
|
|
12
|
+
op: 'doctrine_write.plan',
|
|
13
|
+
path,
|
|
14
|
+
input: { ...input },
|
|
15
|
+
...(source === undefined ? {} : { source }),
|
|
16
|
+
result: planSchema,
|
|
17
|
+
});
|
|
18
|
+
return { ...answer.result, text: answer.text, bytes: answer.bytes, diff: answer.diff };
|
|
58
19
|
}
|
|
59
|
-
/**
|
|
60
|
-
* The plan that creates a new doctrine file holding one new doctrine, for `createFile: true`. The file
|
|
61
|
-
* is UTF-8 without a byte order mark and uses tabs and LF, as the game's own doctrine files do.
|
|
62
|
-
*/
|
|
20
|
+
/** The plan that creates a new file holding one new doctrine, for `createFile: true`. */
|
|
63
21
|
export function planNewDoctrineFile(path, input) {
|
|
64
|
-
|
|
65
|
-
if (input.operation !== 'add')
|
|
66
|
-
fail('DOCTRINE_WRITE_FILE_NOT_FOUND', `${path} does not exist, and only add can create a file`);
|
|
67
|
-
assertBodyIsOneBlock(input.id, input.body);
|
|
68
|
-
const text = [`${input.id} = {`, indentedBody(input.body, '\t', '\n'), '}', ''].join('\n');
|
|
69
|
-
const written = parseClausewitz(Buffer.from(text, 'utf8'), path);
|
|
70
|
-
if (hasBlockingDiagnostics(written.diagnostics))
|
|
71
|
-
fail('DOCTRINE_WRITE_RESULT_INVALID', 'The new file would have syntax errors', {
|
|
72
|
-
diagnosticCodes: written.diagnostics.map(({ code }) => code),
|
|
73
|
-
});
|
|
74
|
-
return {
|
|
75
|
-
operation: 'add',
|
|
76
|
-
id: input.id,
|
|
77
|
-
path,
|
|
78
|
-
text,
|
|
79
|
-
bytes: Buffer.from(text, 'utf8'),
|
|
80
|
-
edit: { start: 0, end: 0, text, description: `create ${path} with ${input.id}` },
|
|
81
|
-
diff: unifiedTextDiff('', text, `a/${path}`, `b/${path}`, { context: DIFF_CONTEXT_LINES }),
|
|
82
|
-
untouchedDefinitions: 0,
|
|
83
|
-
};
|
|
22
|
+
return plan(path, input, undefined);
|
|
84
23
|
}
|
|
85
24
|
export function planDoctrineWrite(source, input) {
|
|
86
|
-
|
|
87
|
-
const document = parseClausewitz(source.bytes, source.path);
|
|
88
|
-
if (hasBlockingDiagnostics(document.diagnostics))
|
|
89
|
-
fail('DOCTRINE_WRITE_SOURCE_INVALID', 'The source has syntax errors and cannot be edited safely', { path: source.path, diagnosticCodes: document.diagnostics.map(({ code }) => code) });
|
|
90
|
-
const text = document.text;
|
|
91
|
-
const newline = document.newline;
|
|
92
|
-
if (input.body !== undefined)
|
|
93
|
-
assertBodyIsOneBlock(input.id, input.body);
|
|
94
|
-
const located = definitions(document);
|
|
95
|
-
const unit = indentUnit(located, text);
|
|
96
|
-
const existing = located.filter(({ key }) => key.value === input.id);
|
|
97
|
-
let edit;
|
|
98
|
-
let skip;
|
|
99
|
-
if (input.operation === 'add') {
|
|
100
|
-
if (existing.length > 0)
|
|
101
|
-
fail('DOCTRINE_WRITE_DUPLICATE_ID', `${input.id} is already defined in this file`, {
|
|
102
|
-
id: input.id,
|
|
103
|
-
line: existing[0]?.key.token.line,
|
|
104
|
-
});
|
|
105
|
-
const lead = text === '' ? '' : `${text.endsWith('\n') ? '' : newline}${newline}`;
|
|
106
|
-
edit = {
|
|
107
|
-
start: text.length,
|
|
108
|
-
end: text.length,
|
|
109
|
-
text: `${lead}${input.id} = {${newline}${indentedBody(input.body, unit, newline)}${newline}}${newline}`,
|
|
110
|
-
description: `add ${input.id}`,
|
|
111
|
-
};
|
|
112
|
-
}
|
|
113
|
-
else {
|
|
114
|
-
if (existing.length === 0)
|
|
115
|
-
fail('DOCTRINE_WRITE_NOT_FOUND', `${input.id} is not defined in this file`, {
|
|
116
|
-
id: input.id,
|
|
117
|
-
});
|
|
118
|
-
if (existing.length > 1)
|
|
119
|
-
fail('DOCTRINE_WRITE_DUPLICATE_ID', `${input.id} is defined ${existing.length} times in this file`, { id: input.id, lines: existing.map(({ key }) => key.token.line) });
|
|
120
|
-
const found = existing[0];
|
|
121
|
-
skip = found;
|
|
122
|
-
const indent = lineIndent(text, found.start);
|
|
123
|
-
if (input.operation === 'replace') {
|
|
124
|
-
edit = {
|
|
125
|
-
start: found.start,
|
|
126
|
-
end: found.end,
|
|
127
|
-
text: `${input.id} = {${newline}${indentedBody(input.body, indent + unit, newline)}${newline}${indent}}`,
|
|
128
|
-
description: `replace ${input.id}`,
|
|
129
|
-
};
|
|
130
|
-
}
|
|
131
|
-
else {
|
|
132
|
-
const lineStart = text.lastIndexOf('\n', found.start - 1) + 1;
|
|
133
|
-
const lineEndAt = text.indexOf('\n', found.end);
|
|
134
|
-
const lineEnd = lineEndAt === -1 ? text.length : lineEndAt + 1;
|
|
135
|
-
const alone = /^[ \t]*$/u.test(text.slice(lineStart, found.start)) &&
|
|
136
|
-
/^[ \t]*\r?$/u.test(text.slice(found.end, lineEndAt === -1 ? text.length : lineEndAt));
|
|
137
|
-
if (!alone) {
|
|
138
|
-
edit = { start: found.start, end: found.end, text: '', description: `remove ${input.id}` };
|
|
139
|
-
}
|
|
140
|
-
else {
|
|
141
|
-
// A blank line that only separated this doctrine from the one before it goes with it.
|
|
142
|
-
const before = text.slice(0, lineStart);
|
|
143
|
-
const blankBefore = /(?:^|\n)[ \t]*\r?\n$/u.test(before) && before.length > 0;
|
|
144
|
-
const after = text.slice(lineEnd);
|
|
145
|
-
const nextIsBlankOrEnd = /^[ \t]*\r?(?:\n|$)/u.test(after);
|
|
146
|
-
let start = lineStart;
|
|
147
|
-
if (blankBefore && nextIsBlankOrEnd)
|
|
148
|
-
start = before.lastIndexOf('\n', before.length - 2) + 1;
|
|
149
|
-
edit = { start, end: lineEnd, text: '', description: `remove ${input.id}` };
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
}
|
|
153
|
-
const bytes = applyReplacements(document, [edit]);
|
|
154
|
-
const after = text.slice(0, edit.start) + edit.text + text.slice(edit.end);
|
|
155
|
-
const written = parseClausewitz(bytes, source.path);
|
|
156
|
-
if (hasBlockingDiagnostics(written.diagnostics))
|
|
157
|
-
fail('DOCTRINE_WRITE_RESULT_INVALID', 'The edit would leave the file with syntax errors', {
|
|
158
|
-
diagnosticCodes: written.diagnostics.map(({ code }) => code),
|
|
159
|
-
});
|
|
160
|
-
const before = definitionTexts(document, skip);
|
|
161
|
-
const afterTexts = definitionTexts(written, undefined);
|
|
162
|
-
let untouched = 0;
|
|
163
|
-
for (const [key, texts] of before) {
|
|
164
|
-
const now = afterTexts.get(key) ?? [];
|
|
165
|
-
for (const textOfDoctrine of texts)
|
|
166
|
-
if (now.includes(textOfDoctrine))
|
|
167
|
-
untouched += 1;
|
|
168
|
-
else
|
|
169
|
-
fail('DOCTRINE_WRITE_RESULT_INVALID', `The edit would change ${key}, which it must not touch`, { key });
|
|
170
|
-
}
|
|
171
|
-
if (written.text !== after)
|
|
172
|
-
fail('DOCTRINE_WRITE_RESULT_INVALID', 'The written file does not match the planned edit');
|
|
173
|
-
return {
|
|
174
|
-
operation: input.operation,
|
|
175
|
-
id: input.id,
|
|
176
|
-
path: source.path,
|
|
177
|
-
text: after,
|
|
178
|
-
bytes,
|
|
179
|
-
edit,
|
|
180
|
-
diff: unifiedTextDiff(text, after, `a/${source.path}`, `b/${source.path}`, {
|
|
181
|
-
context: DIFF_CONTEXT_LINES,
|
|
182
|
-
}),
|
|
183
|
-
untouchedDefinitions: untouched,
|
|
184
|
-
};
|
|
25
|
+
return plan(source.path, input, source.bytes);
|
|
185
26
|
}
|
|
@@ -25,7 +25,7 @@ function stableUnique(values) {
|
|
|
25
25
|
function worstConfidence(...values) {
|
|
26
26
|
return values.reduce((worst, value) => confidenceRank[value] > confidenceRank[worst] ? value : worst);
|
|
27
27
|
}
|
|
28
|
-
function activeFiles(snapshot) {
|
|
28
|
+
export function activeFiles(snapshot) {
|
|
29
29
|
return snapshot.files
|
|
30
30
|
.filter(({ relativePath, shadowedBy }) => shadowedBy === undefined &&
|
|
31
31
|
/\.(?:txt|ya?ml)$/iu.test(relativePath) &&
|
|
@@ -33,14 +33,14 @@ function activeFiles(snapshot) {
|
|
|
33
33
|
.sort((left, right) => compareCodeUnits(left.displayPath, right.displayPath));
|
|
34
34
|
}
|
|
35
35
|
const EVENT_SETUP_CALL_PATTERN = /\b(?:country_event|news_event|state_event|unit_leader_event|operative_leader_event|fire_event|event)\s*=/iu;
|
|
36
|
-
function shouldAnalyzeEventFile(file) {
|
|
36
|
+
export function shouldAnalyzeEventFile(file) {
|
|
37
37
|
const sourcePath = file.relativePath.replaceAll('\\', '/').toLowerCase();
|
|
38
38
|
if (!sourcePath.startsWith('history/countries/') && !sourcePath.startsWith('history/states/')) {
|
|
39
39
|
return true;
|
|
40
40
|
}
|
|
41
41
|
return EVENT_SETUP_CALL_PATTERN.test(file.bytes.toString('utf8'));
|
|
42
42
|
}
|
|
43
|
-
function catalogFor(snapshot) {
|
|
43
|
+
export function catalogFor(snapshot) {
|
|
44
44
|
const unshadowedEventSymbols = snapshot.index
|
|
45
45
|
.findAll('event')
|
|
46
46
|
.filter(({ sourceShadowed }) => !sourceShadowed);
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from 'zod/v4';
|
|
2
|
-
import {
|
|
2
|
+
import { idleCacheLifetime } from '../core/idle-cache-lifetime.js';
|
|
3
3
|
import { errorResult, toolResult } from '../core/operation-result.js';
|
|
4
4
|
import { ServiceError } from '../core/result.js';
|
|
5
5
|
import { eventInspectRequestSchema, eventRenderRequestSchema } from '../schemas/event.js';
|
|
@@ -9,7 +9,7 @@ const wireResult = z.record(z.string(), z.json());
|
|
|
9
9
|
/** Registers typed event operations without invoking transport handlers. */
|
|
10
10
|
export function registerEventJobs(operations, engine) {
|
|
11
11
|
const viewer = new EventChainViewer(engine);
|
|
12
|
-
const lifetime =
|
|
12
|
+
const lifetime = idleCacheLifetime(engine, () => viewer.clearCaches());
|
|
13
13
|
function register(name, schema, normalize, execute, message) {
|
|
14
14
|
operations.registerRead(name, schema, async (input, context) => {
|
|
15
15
|
const release = lifetime.begin();
|
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { z } from 'zod/v4';
|
|
2
|
-
import {
|
|
2
|
+
import { idleCacheLifetime } from '../core/idle-cache-lifetime.js';
|
|
3
3
|
import { toolResult } from '../core/operation-result.js';
|
|
4
4
|
import { ServiceError } from '../core/result.js';
|
|
5
5
|
import { eventWriteRequestSchema } from '../schemas/event-write.js';
|
|
@@ -12,7 +12,7 @@ function wire(result) {
|
|
|
12
12
|
/** Registers `hoi4.event_write`: a read-only preview, or one journaled transaction. */
|
|
13
13
|
export function registerEventWriteJobs(operations, engine) {
|
|
14
14
|
const viewer = new EventChainViewer(engine);
|
|
15
|
-
const lifetime =
|
|
15
|
+
const lifetime = idleCacheLifetime(engine, () => viewer.clearCaches());
|
|
16
16
|
operations.registerWrite('hoi4.event_write', eventWriteRequestSchema, async (input, context) => {
|
|
17
17
|
if (input.workspaceId !== 'current' && input.workspaceId !== context.workspaceId)
|
|
18
18
|
throw new ServiceError('JOB_ARGUMENT_SCOPE_MISMATCH', 'The request workspace does not match the authorized job workspace');
|
|
@@ -23,16 +23,15 @@ function normalized(path) {
|
|
|
23
23
|
return path.replaceAll('\\', '/').replace(/^\.\//u, '').toLowerCase();
|
|
24
24
|
}
|
|
25
25
|
/** The mod's own `events` files, the ones this tool may write, with what each holds. */
|
|
26
|
-
function modEventFiles(snapshot) {
|
|
27
|
-
|
|
28
|
-
.filter(({ rootKind, shadowedBy, relativePath }) => rootKind === 'mod' &&
|
|
26
|
+
async function modEventFiles(snapshot) {
|
|
27
|
+
const own = snapshot.files.filter(({ rootKind, shadowedBy, relativePath }) => rootKind === 'mod' &&
|
|
29
28
|
shadowedBy === undefined &&
|
|
30
29
|
normalized(relativePath).startsWith('events/') &&
|
|
31
|
-
relativePath.toLowerCase().endsWith('.txt'))
|
|
32
|
-
|
|
30
|
+
relativePath.toLowerCase().endsWith('.txt'));
|
|
31
|
+
return Promise.all(own.map(async (file) => ({
|
|
33
32
|
file,
|
|
34
|
-
...eventFileContents({ bytes: file.bytes, path: file.relativePath }),
|
|
35
|
-
}));
|
|
33
|
+
...(await eventFileContents({ bytes: file.bytes, path: file.relativePath })),
|
|
34
|
+
})));
|
|
36
35
|
}
|
|
37
36
|
/** A path createFile may make: a .txt file under events, inside the mod. */
|
|
38
37
|
function newEventFilePath(file) {
|
|
@@ -134,7 +133,7 @@ function referencesOf(kind, body) {
|
|
|
134
133
|
}
|
|
135
134
|
export async function computeEventWrite(engine, input, context, applied) {
|
|
136
135
|
const snapshot = await engine.scan(context.workspaceId, { patterns: WRITE_PATTERNS }, context.principal, context.signal);
|
|
137
|
-
const files = modEventFiles(snapshot);
|
|
136
|
+
const files = await modEventFiles(snapshot);
|
|
138
137
|
const target = targetFile(snapshot, files, input);
|
|
139
138
|
const namespace = namespaceOf(input.id);
|
|
140
139
|
const planInput = {
|
|
@@ -149,7 +148,7 @@ export async function computeEventWrite(engine, input, context, applied) {
|
|
|
149
148
|
let plan;
|
|
150
149
|
if ('file' in target) {
|
|
151
150
|
file = target.file;
|
|
152
|
-
plan = planEventWrite({ bytes: file.bytes, path: file.relativePath }, planInput);
|
|
151
|
+
plan = await planEventWrite({ bytes: file.bytes, path: file.relativePath }, planInput);
|
|
153
152
|
}
|
|
154
153
|
else {
|
|
155
154
|
const inMod = snapshot.index
|
|
@@ -160,7 +159,7 @@ export async function computeEventWrite(engine, input, context, applied) {
|
|
|
160
159
|
id: input.id,
|
|
161
160
|
path: inMod.path,
|
|
162
161
|
});
|
|
163
|
-
plan = planNewEventFile(target.newPath, planInput);
|
|
162
|
+
plan = await planNewEventFile(target.newPath, planInput);
|
|
164
163
|
}
|
|
165
164
|
const missing = { localisation: [], picture: [] };
|
|
166
165
|
const diagnostics = [];
|