hoi4-agent-tools 4.12.0 → 4.14.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 +14 -0
- package/README.md +3 -3
- package/dist/bin/setup.js +38 -0
- package/dist/bin/stdio.js +5 -1
- package/dist/hoi4_agent_tools/core/configuration.js +16 -0
- package/dist/hoi4_agent_tools/core/domain-tools.js +4 -0
- package/dist/hoi4_agent_tools/core/engine-check.js +87 -0
- package/dist/hoi4_agent_tools/core/engine-client.js +1 -1
- package/dist/hoi4_agent_tools/core/job-operation-registration.js +2 -0
- package/dist/hoi4_agent_tools/localisation/job-operations.js +42 -0
- package/dist/hoi4_agent_tools/localisation/write-diff.js +96 -0
- package/dist/hoi4_agent_tools/localisation/write-format.js +329 -0
- package/dist/hoi4_agent_tools/localisation/write-operations.js +213 -0
- package/dist/hoi4_agent_tools/localisation/write-plan.js +557 -0
- package/dist/hoi4_agent_tools/map/gameplay/crossing-validation.js +17 -0
- package/dist/hoi4_agent_tools/map/model.js +31 -1
- package/dist/hoi4_agent_tools/map/operations.js +2 -1
- package/dist/hoi4_agent_tools/map/world-edge.js +0 -25
- package/dist/hoi4_agent_tools/map/world.js +124 -34
- package/dist/hoi4_agent_tools/mcp/server/argument-errors.js +5 -0
- package/dist/hoi4_agent_tools/mcp/server/create-modern-operations.js +8 -3
- package/dist/hoi4_agent_tools/mcp/server/create.js +5 -1
- package/dist/hoi4_agent_tools/mcp/server/request-lifecycle.js +10 -2
- package/dist/hoi4_agent_tools/mcp/server/task-tool-catalog.js +2 -0
- package/dist/hoi4_agent_tools/mcp/server/tool-profiles.js +83 -0
- package/dist/hoi4_agent_tools/mcp/tools/localisation-write.js +21 -0
- package/dist/hoi4_agent_tools/mcp/tools/reference.js +34 -0
- package/dist/hoi4_agent_tools/mcp/transports/http.js +6 -1
- package/dist/hoi4_agent_tools/reference/compat-patch.js +555 -0
- package/dist/hoi4_agent_tools/reference/compat.js +255 -0
- package/dist/hoi4_agent_tools/reference/docs-pack.js +164 -0
- package/dist/hoi4_agent_tools/reference/docs-pack.json +339 -0
- package/dist/hoi4_agent_tools/reference/documented-commands.js +26 -0
- package/dist/hoi4_agent_tools/reference/error-explain.js +235 -0
- package/dist/hoi4_agent_tools/reference/error-explanations.json +262 -0
- package/dist/hoi4_agent_tools/reference/error-log.js +30 -10
- package/dist/hoi4_agent_tools/reference/mod-health.js +255 -0
- package/dist/hoi4_agent_tools/reference/operations.js +435 -21
- package/dist/hoi4_agent_tools/reference/script-guard.js +109 -0
- package/dist/hoi4_agent_tools/reference/service.js +58 -20
- package/dist/hoi4_agent_tools/runtime.js +11 -0
- package/dist/hoi4_agent_tools/schemas/compat.js +153 -0
- package/dist/hoi4_agent_tools/schemas/localisation-write.js +154 -0
- package/dist/hoi4_agent_tools/schemas/map.js +13 -1
- package/dist/hoi4_agent_tools/schemas/modder-tools.js +191 -0
- package/dist/hoi4_agent_tools/version.js +1 -1
- package/docs/configuration.md +4 -1
- package/docs/jobs.md +2 -1
- package/docs/map.md +4 -2
- package/docs/reference.md +45 -5
- package/docs/setup.md +23 -3
- package/docs/tools.md +16 -1
- package/engine/bin/manifest.json +1 -1
- package/engine/bin/win32-x64/hoi4-engine.exe +0 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
- package/schemas/configuration.schema.json +140 -113
- 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 +170 -139
- 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
|
@@ -0,0 +1,329 @@
|
|
|
1
|
+
import { isUtf8 } from 'node:buffer';
|
|
2
|
+
const UTF8_BOM = Buffer.from([0xef, 0xbb, 0xbf]);
|
|
3
|
+
export function decodeLocBytes(bytes) {
|
|
4
|
+
const buffer = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
5
|
+
let encoding;
|
|
6
|
+
let text;
|
|
7
|
+
if (buffer.subarray(0, 3).equals(UTF8_BOM)) {
|
|
8
|
+
const rest = buffer.subarray(3);
|
|
9
|
+
if (!isUtf8(rest))
|
|
10
|
+
return undefined;
|
|
11
|
+
encoding = 'utf8-bom';
|
|
12
|
+
text = rest.toString('utf8');
|
|
13
|
+
}
|
|
14
|
+
else if (buffer[0] === 0xff && buffer[1] === 0xfe) {
|
|
15
|
+
if (buffer.length % 2 !== 0)
|
|
16
|
+
return undefined;
|
|
17
|
+
encoding = 'utf16le';
|
|
18
|
+
text = buffer.subarray(2).toString('utf16le');
|
|
19
|
+
}
|
|
20
|
+
else if (buffer[0] === 0xfe && buffer[1] === 0xff) {
|
|
21
|
+
if (buffer.length % 2 !== 0)
|
|
22
|
+
return undefined;
|
|
23
|
+
encoding = 'utf16be';
|
|
24
|
+
text = Buffer.from(buffer.subarray(2)).swap16().toString('utf16le');
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
if (!isUtf8(buffer))
|
|
28
|
+
return undefined;
|
|
29
|
+
encoding = 'utf8';
|
|
30
|
+
text = buffer.toString('utf8');
|
|
31
|
+
}
|
|
32
|
+
const lines = [];
|
|
33
|
+
const breaks = /\r\n|\r|\n/gu;
|
|
34
|
+
let start = 0;
|
|
35
|
+
for (let match = breaks.exec(text); match !== null; match = breaks.exec(text)) {
|
|
36
|
+
const content = text.slice(start, match.index);
|
|
37
|
+
lines.push({
|
|
38
|
+
content,
|
|
39
|
+
eol: match[0],
|
|
40
|
+
orig: lines.length,
|
|
41
|
+
origContent: content,
|
|
42
|
+
origEol: match[0],
|
|
43
|
+
});
|
|
44
|
+
start = match.index + match[0].length;
|
|
45
|
+
}
|
|
46
|
+
if (start < text.length) {
|
|
47
|
+
const content = text.slice(start);
|
|
48
|
+
lines.push({ content, eol: '', orig: lines.length, origContent: content, origEol: '' });
|
|
49
|
+
}
|
|
50
|
+
return { encoding, target: encoding, lines };
|
|
51
|
+
}
|
|
52
|
+
/** A file that does not exist yet: UTF-8 with a BOM, LF, no lines. */
|
|
53
|
+
export function newLocDocument() {
|
|
54
|
+
return { encoding: 'new', target: 'utf8-bom', lines: [] };
|
|
55
|
+
}
|
|
56
|
+
export function encodeLocDocument(document) {
|
|
57
|
+
const text = document.lines.map(({ content, eol }) => content + eol).join('');
|
|
58
|
+
switch (document.target) {
|
|
59
|
+
case 'utf8-bom':
|
|
60
|
+
return Buffer.concat([UTF8_BOM, Buffer.from(text, 'utf8')]);
|
|
61
|
+
case 'utf8':
|
|
62
|
+
return Buffer.from(text, 'utf8');
|
|
63
|
+
case 'utf16le':
|
|
64
|
+
return Buffer.concat([Buffer.from([0xff, 0xfe]), Buffer.from(text, 'utf16le')]);
|
|
65
|
+
case 'utf16be':
|
|
66
|
+
return Buffer.concat([Buffer.from([0xfe, 0xff]), Buffer.from(text, 'utf16le').swap16()]);
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
/** The newline most lines end with; LF when the file has none or the counts tie. */
|
|
70
|
+
export function dominantNewline(document) {
|
|
71
|
+
const counts = new Map();
|
|
72
|
+
for (const { eol } of document.lines)
|
|
73
|
+
if (eol !== '')
|
|
74
|
+
counts.set(eol, (counts.get(eol) ?? 0) + 1);
|
|
75
|
+
let best = '\n';
|
|
76
|
+
let bestCount = 0;
|
|
77
|
+
for (const [eol, count] of [...counts].sort(([a], [b]) => (a < b ? -1 : a > b ? 1 : 0))) {
|
|
78
|
+
if (count > bestCount) {
|
|
79
|
+
best = eol;
|
|
80
|
+
bestCount = count;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
return best;
|
|
84
|
+
}
|
|
85
|
+
export function documentChanged(document) {
|
|
86
|
+
return (document.encoding === 'new' ||
|
|
87
|
+
document.target !== document.encoding ||
|
|
88
|
+
document.lines.some((line) => line.orig === null || line.content !== line.origContent || line.eol !== line.origEol));
|
|
89
|
+
}
|
|
90
|
+
// ---------------------------------------------------------------------------------------------
|
|
91
|
+
// Lines
|
|
92
|
+
const HEADER = /^[ \t]*l_([a-z_]+):[ \t]*(?:#.*)?$/u;
|
|
93
|
+
const ENTRY = /^([ \t]*)([A-Za-z0-9_.-]+):(\d*)([ \t]*)(.*)$/u;
|
|
94
|
+
const BLANK_OR_COMMENT = /^[ \t]*(?:#.*)?$/u;
|
|
95
|
+
const TAIL = /^[ \t]*(?:#.*)?$/u;
|
|
96
|
+
const TYPOGRAPHIC_QUOTES = new Set(['“', '”', '„', '‟']);
|
|
97
|
+
/** Index of the closing quote of `rest`, which starts with `"`; -1 when it has none. */
|
|
98
|
+
function findClosingQuote(rest) {
|
|
99
|
+
for (let index = 1; index < rest.length; index += 1) {
|
|
100
|
+
const char = rest[index];
|
|
101
|
+
if (char === '\\') {
|
|
102
|
+
index += 1;
|
|
103
|
+
continue;
|
|
104
|
+
}
|
|
105
|
+
if (char === '"' && TAIL.test(rest.slice(index + 1)))
|
|
106
|
+
return index;
|
|
107
|
+
}
|
|
108
|
+
return -1;
|
|
109
|
+
}
|
|
110
|
+
export function classifyLine(content) {
|
|
111
|
+
if (BLANK_OR_COMMENT.test(content))
|
|
112
|
+
return { kind: 'blank' };
|
|
113
|
+
const header = HEADER.exec(content);
|
|
114
|
+
if (header !== null)
|
|
115
|
+
return { kind: 'header', language: header[1] };
|
|
116
|
+
const match = ENTRY.exec(content);
|
|
117
|
+
if (match === null)
|
|
118
|
+
return { kind: 'other' };
|
|
119
|
+
const [, indent = '', key = '', version = '', gap = '', rest = ''] = match;
|
|
120
|
+
const prefix = `${indent}${key}:${version}${gap}`;
|
|
121
|
+
if (rest.startsWith('"')) {
|
|
122
|
+
const close = findClosingQuote(rest);
|
|
123
|
+
if (close >= 0)
|
|
124
|
+
return {
|
|
125
|
+
kind: 'entry',
|
|
126
|
+
entry: {
|
|
127
|
+
prefix,
|
|
128
|
+
indent,
|
|
129
|
+
key,
|
|
130
|
+
version,
|
|
131
|
+
value: rest.slice(1, close),
|
|
132
|
+
tail: rest.slice(close + 1),
|
|
133
|
+
},
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
return { kind: 'broken', prefix, key, rest };
|
|
137
|
+
}
|
|
138
|
+
/** A value as it is written between the quotes: a bare `"` becomes `\"`. */
|
|
139
|
+
export function escapeLocText(text) {
|
|
140
|
+
let result = '';
|
|
141
|
+
for (let index = 0; index < text.length; index += 1) {
|
|
142
|
+
const char = text[index];
|
|
143
|
+
if (char === '\\' && index + 1 < text.length) {
|
|
144
|
+
result += char + text[index + 1];
|
|
145
|
+
index += 1;
|
|
146
|
+
}
|
|
147
|
+
else if (char === '"')
|
|
148
|
+
result += '\\"';
|
|
149
|
+
else
|
|
150
|
+
result += char;
|
|
151
|
+
}
|
|
152
|
+
return result;
|
|
153
|
+
}
|
|
154
|
+
/** True when the text ends in a backslash that would escape the closing quote. */
|
|
155
|
+
export function endsWithLoneBackslash(text) {
|
|
156
|
+
let count = 0;
|
|
157
|
+
for (let index = text.length - 1; index >= 0 && text[index] === '\\'; index -= 1)
|
|
158
|
+
count += 1;
|
|
159
|
+
return count % 2 === 1;
|
|
160
|
+
}
|
|
161
|
+
export function formatEntryLine(style, key, rawValue) {
|
|
162
|
+
return `${style.indent}${key}:${style.version} "${rawValue}"`;
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* New files use what most game files use: one leading space and no version number. On the
|
|
166
|
+
* installed 1.19.4.1 English files 116,765 entry lines are ` key: "text"` and 2,067 are
|
|
167
|
+
* ` key:0 "text"`; all 208 files use LF.
|
|
168
|
+
*/
|
|
169
|
+
export const NEW_FILE_STYLE = { indent: ' ', version: '' };
|
|
170
|
+
export function detectStyle(document) {
|
|
171
|
+
let indent;
|
|
172
|
+
const versions = new Map();
|
|
173
|
+
let entries = 0;
|
|
174
|
+
for (const { content } of document.lines) {
|
|
175
|
+
const kind = classifyLine(content);
|
|
176
|
+
if (kind.kind !== 'entry')
|
|
177
|
+
continue;
|
|
178
|
+
entries += 1;
|
|
179
|
+
indent ??= kind.entry.indent;
|
|
180
|
+
if (kind.entry.version !== '')
|
|
181
|
+
versions.set(kind.entry.version, (versions.get(kind.entry.version) ?? 0) + 1);
|
|
182
|
+
}
|
|
183
|
+
let version = '';
|
|
184
|
+
let withVersion = 0;
|
|
185
|
+
for (const count of versions.values())
|
|
186
|
+
withVersion += count;
|
|
187
|
+
if (withVersion * 2 > entries) {
|
|
188
|
+
let best = 0;
|
|
189
|
+
for (const [candidate, count] of [...versions].sort(([a], [b]) => (a < b ? -1 : 1)))
|
|
190
|
+
if (count > best) {
|
|
191
|
+
version = candidate;
|
|
192
|
+
best = count;
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
return { indent: indent ?? NEW_FILE_STYLE.indent, version };
|
|
196
|
+
}
|
|
197
|
+
export function scanDocument(document) {
|
|
198
|
+
const entries = [];
|
|
199
|
+
const headers = [];
|
|
200
|
+
let language = null;
|
|
201
|
+
for (const [lineIndex, { content }] of document.lines.entries()) {
|
|
202
|
+
const kind = classifyLine(content);
|
|
203
|
+
if (kind.kind === 'header') {
|
|
204
|
+
language = kind.language;
|
|
205
|
+
headers.push({ lineIndex, language });
|
|
206
|
+
}
|
|
207
|
+
else if (kind.kind === 'entry')
|
|
208
|
+
entries.push({ lineIndex, language, entry: kind.entry });
|
|
209
|
+
}
|
|
210
|
+
return { entries, headers };
|
|
211
|
+
}
|
|
212
|
+
/**
|
|
213
|
+
* Add entry lines to the section of `language`: after its last entry, or after its header, or
|
|
214
|
+
* (when the file has no such header) under a new header at the end. Returns the 1-based line
|
|
215
|
+
* numbers of the new entry lines. The file's newline style is kept, and a file that had no final
|
|
216
|
+
* newline still has none after the last line.
|
|
217
|
+
*/
|
|
218
|
+
export function appendEntries(document, language, lines) {
|
|
219
|
+
if (lines.length === 0)
|
|
220
|
+
return [];
|
|
221
|
+
const newline = dominantNewline(document);
|
|
222
|
+
const scanned = scanDocument(document);
|
|
223
|
+
const headerIndex = scanned.headers.findIndex((header) => header.language === language);
|
|
224
|
+
const added = [...lines];
|
|
225
|
+
let insertAt;
|
|
226
|
+
if (headerIndex < 0) {
|
|
227
|
+
added.unshift(`l_${language}:`);
|
|
228
|
+
insertAt = document.lines.length;
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
const header = scanned.headers[headerIndex];
|
|
232
|
+
const nextHeader = scanned.headers[headerIndex + 1]?.lineIndex ?? document.lines.length;
|
|
233
|
+
const last = scanned.entries
|
|
234
|
+
.filter(({ lineIndex }) => lineIndex > header.lineIndex && lineIndex < nextHeader)
|
|
235
|
+
.at(-1);
|
|
236
|
+
insertAt = (last?.lineIndex ?? header.lineIndex) + 1;
|
|
237
|
+
}
|
|
238
|
+
const atEnd = insertAt === document.lines.length;
|
|
239
|
+
const previous = insertAt === 0 ? undefined : document.lines.at(insertAt - 1);
|
|
240
|
+
const keepNoFinalNewline = atEnd && previous?.eol === '';
|
|
241
|
+
if (keepNoFinalNewline)
|
|
242
|
+
previous.eol = newline;
|
|
243
|
+
const inserted = added.map((content, index) => ({
|
|
244
|
+
content,
|
|
245
|
+
eol: index === added.length - 1 && keepNoFinalNewline ? '' : newline,
|
|
246
|
+
orig: null,
|
|
247
|
+
origContent: '',
|
|
248
|
+
origEol: '',
|
|
249
|
+
}));
|
|
250
|
+
document.lines.splice(insertAt, 0, ...inserted);
|
|
251
|
+
const first = insertAt + (headerIndex < 0 ? 1 : 0);
|
|
252
|
+
return lines.map((_line, index) => first + index + 1);
|
|
253
|
+
}
|
|
254
|
+
/** `#` that starts a trailing comment: after whitespace and followed by whitespace or the end. */
|
|
255
|
+
const COMMENT_START = /[ \t]#(?=[ \t]|$)/u;
|
|
256
|
+
function hasTypographicQuote(text) {
|
|
257
|
+
for (const char of text)
|
|
258
|
+
if (TYPOGRAPHIC_QUOTES.has(char))
|
|
259
|
+
return true;
|
|
260
|
+
return false;
|
|
261
|
+
}
|
|
262
|
+
function isQuote(char) {
|
|
263
|
+
return char === '"' || TYPOGRAPHIC_QUOTES.has(char);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Repair a broken value only where the intent is unambiguous: a missing closing quote gets one
|
|
267
|
+
* at the end of the value (before a trailing comment), and typographic quotes that delimit the
|
|
268
|
+
* value become ASCII quotes. Everything else is reported, never guessed.
|
|
269
|
+
*/
|
|
270
|
+
export function repairValue(line, nextContent) {
|
|
271
|
+
const { prefix, rest } = line;
|
|
272
|
+
if (rest === '')
|
|
273
|
+
return { fixed: false, reason: 'the entry has no value' };
|
|
274
|
+
const first = rest[0];
|
|
275
|
+
if (!isQuote(first))
|
|
276
|
+
return { fixed: false, reason: 'the value does not start with a quote' };
|
|
277
|
+
// Closing candidates: a quote character after which only blanks or a comment follow.
|
|
278
|
+
const candidates = [];
|
|
279
|
+
for (let index = 1; index < rest.length; index += 1) {
|
|
280
|
+
const char = rest[index];
|
|
281
|
+
if (char === '\\') {
|
|
282
|
+
index += 1;
|
|
283
|
+
continue;
|
|
284
|
+
}
|
|
285
|
+
if (isQuote(char) && TAIL.test(rest.slice(index + 1)))
|
|
286
|
+
candidates.push(index);
|
|
287
|
+
}
|
|
288
|
+
const typographic = first !== '"' || candidates.some((index) => rest[index] !== '"');
|
|
289
|
+
if (typographic) {
|
|
290
|
+
if (candidates.length === 1) {
|
|
291
|
+
const close = candidates[0];
|
|
292
|
+
const inner = rest.slice(1, close);
|
|
293
|
+
// A plain opening quote with a typographic closing one is clear only when no other
|
|
294
|
+
// typographic quote sits inside.
|
|
295
|
+
const mixed = first === '"' && hasTypographicQuote(inner);
|
|
296
|
+
if (!mixed)
|
|
297
|
+
return { fixed: true, content: `${prefix}"${inner}"${rest.slice(close + 1)}` };
|
|
298
|
+
}
|
|
299
|
+
return {
|
|
300
|
+
fixed: false,
|
|
301
|
+
reason: candidates.length === 0
|
|
302
|
+
? 'typographic quotes without a clear closing quote'
|
|
303
|
+
: 'more than one possible closing quote',
|
|
304
|
+
};
|
|
305
|
+
}
|
|
306
|
+
// An ASCII opening quote with no closing quote. Another quote inside makes it unclear.
|
|
307
|
+
for (let index = 1; index < rest.length; index += 1) {
|
|
308
|
+
if (rest[index] === '\\')
|
|
309
|
+
index += 1;
|
|
310
|
+
else if (rest[index] === '"')
|
|
311
|
+
return { fixed: false, reason: 'the value has a quote that is not at its end' };
|
|
312
|
+
}
|
|
313
|
+
if (nextContent !== undefined) {
|
|
314
|
+
const next = classifyLine(nextContent);
|
|
315
|
+
const startsEntry = next.kind === 'blank' ||
|
|
316
|
+
next.kind === 'header' ||
|
|
317
|
+
next.kind === 'entry' ||
|
|
318
|
+
(next.kind === 'broken' && (next.rest === '' || isQuote(next.rest[0])));
|
|
319
|
+
if (!startsEntry)
|
|
320
|
+
return { fixed: false, reason: 'the value may continue on the next line' };
|
|
321
|
+
}
|
|
322
|
+
const comment = COMMENT_START.exec(rest);
|
|
323
|
+
const valuePart = comment === null ? rest : rest.slice(0, comment.index);
|
|
324
|
+
const after = comment === null ? '' : rest.slice(comment.index);
|
|
325
|
+
const trimmed = valuePart.trimEnd();
|
|
326
|
+
if (endsWithLoneBackslash(trimmed))
|
|
327
|
+
return { fixed: false, reason: 'the value ends with a backslash that escapes the quote' };
|
|
328
|
+
return { fixed: true, content: `${prefix}${trimmed}"${valuePart.slice(trimmed.length)}${after}` };
|
|
329
|
+
}
|
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
import { compareCodeUnits } from '../core/canonical.js';
|
|
2
|
+
import { emptyServiceResult } from '../core/result.js';
|
|
3
|
+
import { autonomousResultArtifacts, } from '../core/transaction-execution.js';
|
|
4
|
+
import { localisationWriteDataSchema, } from '../schemas/localisation-write.js';
|
|
5
|
+
import { documentDiff } from './write-diff.js';
|
|
6
|
+
import { planAdd, planFix, } from './write-plan.js';
|
|
7
|
+
const SHOWN_FILES = 10;
|
|
8
|
+
const SHOWN_CHANGES = 6;
|
|
9
|
+
const SHOWN_LISTED = 15;
|
|
10
|
+
const DIFF_BUDGET_TOTAL = 10_000;
|
|
11
|
+
/** The whole result must stay under the 32 KiB wire budget with its envelope. */
|
|
12
|
+
const DATA_BUDGET_BYTES = 22_000;
|
|
13
|
+
const MAX_INLINE_ARTIFACTS = 2;
|
|
14
|
+
const MAX_INLINE_PATHS = 64;
|
|
15
|
+
const MAX_CONSULTED_DEPENDENCIES = 8_000;
|
|
16
|
+
export async function computeLocalisationPlan(engine, input, context) {
|
|
17
|
+
const options = {
|
|
18
|
+
workspace: engine.resolver.get(context.workspaceId, context.principal),
|
|
19
|
+
...(context.signal === undefined ? {} : { signal: context.signal }),
|
|
20
|
+
...(context.undefinedKeys === undefined ? {} : { undefinedKeys: context.undefinedKeys }),
|
|
21
|
+
};
|
|
22
|
+
return input.mode === 'add' ? planAdd(options, input) : planFix(options, input);
|
|
23
|
+
}
|
|
24
|
+
function summarizeFile(file, diffBudget) {
|
|
25
|
+
const { diff, truncated } = documentDiff(file.document, file.path, diffBudget);
|
|
26
|
+
const shown = file.changes.slice(0, SHOWN_CHANGES);
|
|
27
|
+
return {
|
|
28
|
+
path: file.path,
|
|
29
|
+
action: file.action,
|
|
30
|
+
diff,
|
|
31
|
+
diffTruncated: truncated,
|
|
32
|
+
changes: shown.map(({ kind, key, line }) => ({
|
|
33
|
+
kind,
|
|
34
|
+
...(key === undefined ? {} : { key }),
|
|
35
|
+
...(line === undefined ? {} : { line }),
|
|
36
|
+
})),
|
|
37
|
+
changesOmitted: file.changes.length - shown.length,
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
/** The tool's data, bounded so the whole result stays under the wire budget. */
|
|
41
|
+
export function summarizePlan(mode, plan, applied) {
|
|
42
|
+
const shownFiles = plan.files.slice(0, SHOWN_FILES);
|
|
43
|
+
let budget = Math.floor(DIFF_BUDGET_TOTAL / Math.max(1, shownFiles.length));
|
|
44
|
+
const build = (diffBudget) => ({
|
|
45
|
+
mode,
|
|
46
|
+
applied,
|
|
47
|
+
files: shownFiles.map((file) => summarizeFile(file, diffBudget)),
|
|
48
|
+
skipped: plan.skipped.slice(0, SHOWN_LISTED).map(({ key, reason }) => ({
|
|
49
|
+
key,
|
|
50
|
+
reason: reason.slice(0, 300),
|
|
51
|
+
})),
|
|
52
|
+
unfixable: plan.unfixable.slice(0, SHOWN_LISTED).map((item) => ({
|
|
53
|
+
kind: item.kind,
|
|
54
|
+
...(item.path === undefined ? {} : { path: item.path.slice(0, 1024) }),
|
|
55
|
+
...(item.key === undefined ? {} : { key: item.key }),
|
|
56
|
+
reason: item.reason.slice(0, 300),
|
|
57
|
+
})),
|
|
58
|
+
notes: plan.notes.slice(0, 10).map((note) => note.slice(0, 400)),
|
|
59
|
+
counts: {
|
|
60
|
+
filesChanged: plan.files.length,
|
|
61
|
+
filesCreated: plan.files.filter(({ action }) => action === 'create').length,
|
|
62
|
+
filesOmitted: plan.files.length - shownFiles.length,
|
|
63
|
+
keysAdded: plan.counts.keysAdded,
|
|
64
|
+
keysReplaced: plan.counts.keysReplaced,
|
|
65
|
+
keysOverridden: plan.counts.keysOverridden,
|
|
66
|
+
keysSkipped: plan.skipped.length,
|
|
67
|
+
skippedOmitted: Math.max(0, plan.skipped.length - SHOWN_LISTED),
|
|
68
|
+
bomsAdded: plan.counts.bomsAdded,
|
|
69
|
+
quotesFixed: plan.counts.quotesFixed,
|
|
70
|
+
keysCopied: plan.counts.keysCopied,
|
|
71
|
+
unfixable: plan.unfixable.length,
|
|
72
|
+
unfixableOmitted: Math.max(0, plan.unfixable.length - SHOWN_LISTED),
|
|
73
|
+
},
|
|
74
|
+
});
|
|
75
|
+
let data = build(budget);
|
|
76
|
+
for (let round = 0; round < 5 && JSON.stringify(data).length > DATA_BUDGET_BYTES; round += 1) {
|
|
77
|
+
budget = Math.floor(budget / 2);
|
|
78
|
+
data = build(budget);
|
|
79
|
+
}
|
|
80
|
+
return localisationWriteDataSchema.parse(data);
|
|
81
|
+
}
|
|
82
|
+
function inlineDiagnostics(diagnostics) {
|
|
83
|
+
const hard = (diagnostic) => diagnostic.severity === 'error' || diagnostic.severity === 'blocker';
|
|
84
|
+
return [...diagnostics.filter(hard), ...diagnostics.filter((entry) => !hard(entry))].slice(0, 20);
|
|
85
|
+
}
|
|
86
|
+
function resultFor(workspaceId, data, paths, outcome) {
|
|
87
|
+
const result = emptyServiceResult(workspaceId, data);
|
|
88
|
+
result.code =
|
|
89
|
+
outcome === 'preview'
|
|
90
|
+
? paths.length === 0
|
|
91
|
+
? 'LOCALISATION_NOTHING_TO_CHANGE'
|
|
92
|
+
: 'LOCALISATION_PREVIEW'
|
|
93
|
+
: outcome === 'applied'
|
|
94
|
+
? 'LOCALISATION_CHANGES_APPLIED'
|
|
95
|
+
: outcome === 'unchanged'
|
|
96
|
+
? 'LOCALISATION_CHANGES_UNCHANGED'
|
|
97
|
+
: 'LOCALISATION_CHANGES_BLOCKED';
|
|
98
|
+
result.status = outcome === 'blocked' ? 'blocked' : 'ok';
|
|
99
|
+
result.proposedFiles = paths.slice(0, MAX_INLINE_PATHS);
|
|
100
|
+
return result;
|
|
101
|
+
}
|
|
102
|
+
/** `apply: false`: the diff of every file the call would change, and nothing written. */
|
|
103
|
+
export async function previewLocalisationWrite(engine, input, context) {
|
|
104
|
+
await context.progress?.(0, 2, "Reading the mod's localisation");
|
|
105
|
+
const plan = await computeLocalisationPlan(engine, input, context);
|
|
106
|
+
const result = resultFor(context.workspaceId, summarizePlan(input.mode, plan, false), plan.files.map(({ path }) => path), 'preview');
|
|
107
|
+
await context.progress?.(2, 2, 'Localisation preview ready');
|
|
108
|
+
return result;
|
|
109
|
+
}
|
|
110
|
+
/** `apply: true`: the same plan, as one validated transaction for atomic replacement. */
|
|
111
|
+
export async function prepareLocalisationWrite(engine, input, context) {
|
|
112
|
+
await context.progress?.(0, 3, "Reading the mod's localisation");
|
|
113
|
+
const plan = await computeLocalisationPlan(engine, input, context);
|
|
114
|
+
const data = summarizePlan(input.mode, plan, true);
|
|
115
|
+
const paths = plan.files.map(({ path }) => path);
|
|
116
|
+
if (plan.files.length === 0)
|
|
117
|
+
return {
|
|
118
|
+
readOnlyPreview: resultFor(context.workspaceId, { ...data, applied: false }, paths, 'unchanged'),
|
|
119
|
+
};
|
|
120
|
+
const workspace = engine.resolver.get(context.workspaceId, context.principal);
|
|
121
|
+
const modRoot = workspace.roots.find((root) => root.kind === 'mod');
|
|
122
|
+
const kinds = new Map();
|
|
123
|
+
const changes = plan.files.map((file) => {
|
|
124
|
+
for (const change of file.changes)
|
|
125
|
+
kinds.set(change.kind, (kinds.get(change.kind) ?? 0) + 1);
|
|
126
|
+
return {
|
|
127
|
+
relativePath: file.path,
|
|
128
|
+
content: file.after,
|
|
129
|
+
operationIds: [...new Set(file.changes.map(({ kind }) => `localisation-${kind}`))].sort(compareCodeUnits),
|
|
130
|
+
mediaType: 'text/plain',
|
|
131
|
+
};
|
|
132
|
+
});
|
|
133
|
+
const changedPaths = new Set(paths);
|
|
134
|
+
const consulted = plan.consulted.filter(({ path }) => plan.consulted.length <= MAX_CONSULTED_DEPENDENCIES ? true : changedPaths.has(path));
|
|
135
|
+
await context.progress?.(1, 3, 'Preparing the localisation transaction');
|
|
136
|
+
const transaction = await engine.transactions.plan({
|
|
137
|
+
workspaceId: context.workspaceId,
|
|
138
|
+
...(context.principal === undefined ? {} : { principal: context.principal }),
|
|
139
|
+
operationKind: 'localisation-changes',
|
|
140
|
+
operations: [...kinds]
|
|
141
|
+
.sort(([a], [b]) => compareCodeUnits(a, b))
|
|
142
|
+
.map(([kind, count]) => ({
|
|
143
|
+
id: `localisation-${kind}`,
|
|
144
|
+
kind: `localisation-${kind}`,
|
|
145
|
+
summary: `${kind.replaceAll('_', ' ')}: ${count}`,
|
|
146
|
+
data: { count },
|
|
147
|
+
})),
|
|
148
|
+
changes,
|
|
149
|
+
readDependencies: consulted.map(({ path, sha256 }) => ({
|
|
150
|
+
rootKind: 'mod',
|
|
151
|
+
loadOrder: modRoot?.loadOrder ?? 0,
|
|
152
|
+
relativePath: path,
|
|
153
|
+
sha256,
|
|
154
|
+
})),
|
|
155
|
+
validate: (proposed, signal) => {
|
|
156
|
+
signal?.throwIfAborted();
|
|
157
|
+
const matches = changes.every(({ relativePath, content }) => {
|
|
158
|
+
const bytes = proposed.get(relativePath);
|
|
159
|
+
return (bytes !== undefined &&
|
|
160
|
+
bytes !== null &&
|
|
161
|
+
content !== null &&
|
|
162
|
+
Buffer.from(bytes).equals(Buffer.from(content)));
|
|
163
|
+
});
|
|
164
|
+
return Promise.resolve({
|
|
165
|
+
diagnostics: matches
|
|
166
|
+
? []
|
|
167
|
+
: [
|
|
168
|
+
{
|
|
169
|
+
code: 'LOCALISATION_TRANSACTION_BYTES_MISMATCH',
|
|
170
|
+
severity: 'blocker',
|
|
171
|
+
category: 'validation',
|
|
172
|
+
message: 'Transaction dry-run bytes differ from the planned localisation files',
|
|
173
|
+
},
|
|
174
|
+
],
|
|
175
|
+
checks: [
|
|
176
|
+
{
|
|
177
|
+
id: 'localisation-proposed-bytes-match',
|
|
178
|
+
passed: matches,
|
|
179
|
+
message: matches
|
|
180
|
+
? 'Transaction bytes exactly match the planned localisation files'
|
|
181
|
+
: 'Transaction bytes differ from the planned localisation files',
|
|
182
|
+
},
|
|
183
|
+
],
|
|
184
|
+
});
|
|
185
|
+
},
|
|
186
|
+
...(context.signal === undefined ? {} : { signal: context.signal }),
|
|
187
|
+
});
|
|
188
|
+
await context.progress?.(2, 3, 'Localisation change prepared for journaled application');
|
|
189
|
+
return { transaction, recipe: { data, paths } };
|
|
190
|
+
}
|
|
191
|
+
export function completeLocalisationWrite(workspaceId, execution, recipe) {
|
|
192
|
+
const parsed = recipe;
|
|
193
|
+
const data = localisationWriteDataSchema.parse({
|
|
194
|
+
...parsed.data,
|
|
195
|
+
applied: execution.outcome === 'applied',
|
|
196
|
+
});
|
|
197
|
+
const transaction = execution.transaction;
|
|
198
|
+
const result = resultFor(workspaceId, data, parsed.paths, execution.outcome);
|
|
199
|
+
result.status = transaction.validation.passed ? 'ok' : 'blocked';
|
|
200
|
+
result.changedFiles = transaction.appliedFiles.slice(0, MAX_INLINE_PATHS);
|
|
201
|
+
result.diagnostics = inlineDiagnostics(transaction.diagnostics);
|
|
202
|
+
result.artifacts = autonomousResultArtifacts(execution).slice(0, MAX_INLINE_ARTIFACTS);
|
|
203
|
+
result.validation = transaction.validation;
|
|
204
|
+
result.blockers = transaction.diagnostics
|
|
205
|
+
.filter(({ severity }) => severity === 'error' || severity === 'blocker')
|
|
206
|
+
.slice(0, 20)
|
|
207
|
+
.map(({ code, message, details }) => ({
|
|
208
|
+
code,
|
|
209
|
+
message,
|
|
210
|
+
...(details === undefined ? {} : { details }),
|
|
211
|
+
}));
|
|
212
|
+
return result;
|
|
213
|
+
}
|