hoi4-agent-tools 4.6.5 → 4.7.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.
Files changed (80) hide show
  1. package/CHANGELOG.md +16 -5
  2. package/dist/hoi4_agent_tools/core/engine-client.js +273 -0
  3. package/dist/hoi4_agent_tools/core/operation-result.js +24 -5
  4. package/dist/hoi4_agent_tools/focus/compact.js +197 -18
  5. package/dist/hoi4_agent_tools/focus/engine-layout.js +201 -0
  6. package/dist/hoi4_agent_tools/focus/job-operations.js +3 -1
  7. package/dist/hoi4_agent_tools/focus/layout.js +3 -2
  8. package/dist/hoi4_agent_tools/focus/quality/branches.js +32 -0
  9. package/dist/hoi4_agent_tools/focus/quality/calibration.js +364 -0
  10. package/dist/hoi4_agent_tools/focus/quality/delta.js +85 -0
  11. package/dist/hoi4_agent_tools/focus/quality/focus-quality-calibration.json +2265 -0
  12. package/dist/hoi4_agent_tools/focus/quality/graph.js +170 -0
  13. package/dist/hoi4_agent_tools/focus/quality/index.js +24 -0
  14. package/dist/hoi4_agent_tools/focus/quality/inline-images.js +132 -0
  15. package/dist/hoi4_agent_tools/focus/quality/metrics.js +424 -0
  16. package/dist/hoi4_agent_tools/focus/quality/orthogonal.js +202 -0
  17. package/dist/hoi4_agent_tools/focus/quality/positions.js +41 -0
  18. package/dist/hoi4_agent_tools/focus/quality/schematic.js +241 -0
  19. package/dist/hoi4_agent_tools/focus/quality/scorecard.js +549 -0
  20. package/dist/hoi4_agent_tools/focus/quality/stored-calibration.js +7 -0
  21. package/dist/hoi4_agent_tools/focus/quality/tool-output.js +113 -0
  22. package/dist/hoi4_agent_tools/focus/quality/types.js +5 -0
  23. package/dist/hoi4_agent_tools/focus/tool-operations.js +206 -22
  24. package/dist/hoi4_agent_tools/focus/tool-support.js +1 -1
  25. package/dist/hoi4_agent_tools/mcp/server/instructions.js +1 -1
  26. package/dist/hoi4_agent_tools/mcp/tools/focus.js +76 -4
  27. package/dist/hoi4_agent_tools/reference/game-text.js +109 -0
  28. package/dist/hoi4_agent_tools/reference/ranking.js +60 -2
  29. package/dist/hoi4_agent_tools/reference/service.js +92 -21
  30. package/dist/hoi4_agent_tools/schemas/focus-requests.js +64 -7
  31. package/dist/hoi4_agent_tools/schemas/focus.js +121 -6
  32. package/dist/hoi4_agent_tools/version.js +1 -1
  33. package/docs/configuration.md +17 -17
  34. package/docs/focus.md +100 -4
  35. package/docs/reference.md +1 -1
  36. package/docs/research/reference-benchmark.md +111 -1
  37. package/docs/setup.md +1 -1
  38. package/docs/tools.md +5 -5
  39. package/npm-shrinkwrap.json +2 -2
  40. package/package.json +2 -1
  41. package/schemas/configuration.schema.json +1 -1
  42. package/schemas/continuous-focus-palette.schema.json +1 -1
  43. package/schemas/custom-weighted-pool.schema.json +1 -1
  44. package/schemas/decision-inspect-input.schema.json +1 -1
  45. package/schemas/error-log-input.schema.json +1 -1
  46. package/schemas/event-feature-manifest.schema.json +1 -1
  47. package/schemas/focus-plan.schema.json +146 -120
  48. package/schemas/focus-planning-sidecar.schema.json +15 -7
  49. package/schemas/geography-model.schema.json +1 -1
  50. package/schemas/gui-animation-source.schema.json +1 -1
  51. package/schemas/gui-helper.schema.json +1 -1
  52. package/schemas/gui-scenario.schema.json +1 -1
  53. package/schemas/helper-expansion-request.schema.json +1 -1
  54. package/schemas/helper-expansion-summary.schema.json +1 -1
  55. package/schemas/impact-inspect-input.schema.json +1 -1
  56. package/schemas/map-operation.schema.json +1 -1
  57. package/schemas/mechanic-test-input.schema.json +1 -1
  58. package/schemas/mod-index-input.schema.json +1 -1
  59. package/schemas/operation-result.schema.json +1 -1
  60. package/schemas/package-check-input.schema.json +1 -1
  61. package/schemas/package-manifest.schema.json +1 -1
  62. package/schemas/probability-analysis-result.schema.json +1 -1
  63. package/schemas/probability-compare-input.schema.json +1 -1
  64. package/schemas/probability-evaluate-input.schema.json +1 -1
  65. package/schemas/probability-inspect-input.schema.json +1 -1
  66. package/schemas/probability-render-input.schema.json +1 -1
  67. package/schemas/probability-scenario-set.schema.json +1 -1
  68. package/schemas/probability-sequence-input.schema.json +1 -1
  69. package/schemas/probability-simulate-input.schema.json +1 -1
  70. package/schemas/probability-sweep-input.schema.json +1 -1
  71. package/schemas/reference-context-input.schema.json +1 -1
  72. package/schemas/reference-read-input.schema.json +1 -1
  73. package/schemas/reference-search-input.schema.json +1 -1
  74. package/schemas/reference-sheets.schema.json +1 -1
  75. package/schemas/scenario-suite.schema.json +1 -1
  76. package/schemas/scenario-test-input.schema.json +1 -1
  77. package/schemas/script-validate-input.schema.json +1 -1
  78. package/schemas/shared-scenario.schema.json +1 -1
  79. package/schemas/source-lookup-input.schema.json +1 -1
  80. package/server.json +2 -2
@@ -0,0 +1,241 @@
1
+ import sharp from 'sharp';
2
+ import { compareCodeUnits } from '../../core/canonical.js';
3
+ import { RENDER_MAX_PIXELS } from '../../core/render-budget.js';
4
+ import { DeterministicSvgTextRenderer } from '../../core/svg-text.js';
5
+ import { orthogonalConnector, ORTHOGONAL_PLAQUE_HEIGHT, ORTHOGONAL_PLAQUE_WIDTH, } from './orthogonal.js';
6
+ /**
7
+ * A no-icon schematic of a focus layout in the in-game connector model: right-angle connectors, a
8
+ * plaque 1.6 columns wide and half a row high, red marks between exclusive choices and dotted
9
+ * connectors for alternative prerequisites. It is cheap, and it draws what the metrics measure, so
10
+ * an agent that looks at it judges the same picture the scorecard describes. It is a tool render,
11
+ * never a game screenshot.
12
+ */
13
+ /** Pixels per grid column and per row of the unscaled schematic. */
14
+ const COLUMN_PIXELS = 44;
15
+ const ROW_PIXELS = 88;
16
+ const MARGIN_COLUMNS = 1.5;
17
+ const MARGIN_ROWS = 0.75;
18
+ const MAXIMUM_UPSCALE = 1.5;
19
+ const BRANCH_FILL = [
20
+ '#2f4b6e',
21
+ '#3e5f45',
22
+ '#6b4a3a',
23
+ '#5a3f66',
24
+ '#6a5a2e',
25
+ '#2f5f63',
26
+ '#6e3f4f',
27
+ '#4d5a2e',
28
+ ];
29
+ const SHARED_FILL = '#3a4150';
30
+ /** Largest encoded image the response carries; a bigger one is redrawn at a smaller scale. */
31
+ const IMAGE_BYTE_BUDGET = 700_000;
32
+ export function treeRegion(graph) {
33
+ const xs = graph.nodes.map(({ x }) => x);
34
+ const ys = graph.nodes.map(({ y }) => y);
35
+ return {
36
+ minimumX: Math.min(...xs),
37
+ maximumX: Math.max(...xs),
38
+ minimumY: Math.min(...ys),
39
+ maximumY: Math.max(...ys),
40
+ };
41
+ }
42
+ /** The region around a set of focuses with room for context, never smaller than a readable window. */
43
+ export function regionAround(graph, focusIds, context = { columns: 3, rows: 1 }, minimum = { columns: 14, rows: 3 }) {
44
+ const points = focusIds.flatMap((id) => {
45
+ const point = graph.position.get(id);
46
+ return point === undefined ? [] : [point];
47
+ });
48
+ if (points.length === 0)
49
+ return undefined;
50
+ const xs = points.map(({ x }) => x);
51
+ const ys = points.map(({ y }) => y);
52
+ let region = {
53
+ minimumX: Math.min(...xs) - context.columns,
54
+ maximumX: Math.max(...xs) + context.columns,
55
+ minimumY: Math.min(...ys) - context.rows,
56
+ maximumY: Math.max(...ys) + context.rows,
57
+ };
58
+ const widthShort = minimum.columns - (region.maximumX - region.minimumX);
59
+ const heightShort = minimum.rows - (region.maximumY - region.minimumY);
60
+ if (widthShort > 0)
61
+ region = {
62
+ ...region,
63
+ minimumX: region.minimumX - Math.floor(widthShort / 2),
64
+ maximumX: region.maximumX + Math.ceil(widthShort / 2),
65
+ };
66
+ if (heightShort > 0)
67
+ region = {
68
+ ...region,
69
+ minimumY: region.minimumY - Math.floor(heightShort / 2),
70
+ maximumY: region.maximumY + Math.ceil(heightShort / 2),
71
+ };
72
+ return region;
73
+ }
74
+ function labelLines(id) {
75
+ const parts = id.split('_');
76
+ const lines = [];
77
+ let line = '';
78
+ for (const part of parts) {
79
+ const candidate = line === '' ? part : `${line}_${part}`;
80
+ if (candidate.length <= 12 || line === '')
81
+ line = candidate;
82
+ else {
83
+ lines.push(line);
84
+ line = part;
85
+ }
86
+ }
87
+ lines.push(line);
88
+ const clipped = lines
89
+ .slice(0, 2)
90
+ .map((entry) => (entry.length > 13 ? `${entry.slice(0, 12)}…` : entry));
91
+ if (lines.length > 2)
92
+ clipped[1] = `${clipped[1].replace(/…$/u, '').slice(0, 11)}…`;
93
+ return clipped;
94
+ }
95
+ function escapeXml(value) {
96
+ return value.replace(/[&<>"']/gu, (character) => {
97
+ switch (character) {
98
+ case '&':
99
+ return '&amp;';
100
+ case '<':
101
+ return '&lt;';
102
+ case '>':
103
+ return '&gt;';
104
+ case '"':
105
+ return '&quot;';
106
+ default:
107
+ return '&apos;';
108
+ }
109
+ });
110
+ }
111
+ function schematicDocument(graph, options) {
112
+ const region = options.region ?? treeRegion(graph);
113
+ const columns = region.maximumX - region.minimumX;
114
+ const rows = region.maximumY - region.minimumY;
115
+ const width = Math.ceil((columns + 2 * MARGIN_COLUMNS) * COLUMN_PIXELS + 2 * 2);
116
+ const height = Math.ceil((rows + ORTHOGONAL_PLAQUE_HEIGHT + 2 * MARGIN_ROWS) * ROW_PIXELS);
117
+ const px = (x) => (x - region.minimumX + MARGIN_COLUMNS) * COLUMN_PIXELS + 2;
118
+ const py = (y) => (y - region.minimumY + MARGIN_ROWS) * ROW_PIXELS;
119
+ const plaqueWidth = ORTHOGONAL_PLAQUE_WIDTH * COLUMN_PIXELS;
120
+ const plaqueHeight = ORTHOGONAL_PLAQUE_HEIGHT * ROW_PIXELS;
121
+ const text = new DeterministicSvgTextRenderer();
122
+ const branchColours = new Map();
123
+ for (const branch of new Set(graph.nodes.map(({ id }) => graph.branchOf.get(id) ?? ''))) {
124
+ if (branch === '')
125
+ continue;
126
+ branchColours.set(branch, BRANCH_FILL[branchColours.size % BRANCH_FILL.length]);
127
+ }
128
+ const inRegion = (x, y) => x >= region.minimumX - 2 &&
129
+ x <= region.maximumX + 2 &&
130
+ y >= region.minimumY - 2 &&
131
+ y <= region.maximumY + 2;
132
+ const parts = [];
133
+ // Ruler: column numbers along the top, row numbers down the left, so a picture maps to coordinates.
134
+ const rulerEvery = columns > 60 ? 8 : columns > 24 ? 4 : 2;
135
+ for (let x = Math.ceil(region.minimumX / rulerEvery) * rulerEvery; x <= region.maximumX; x += rulerEvery)
136
+ parts.push(text.render(String(x), { x: px(x), y: 11, fontSize: 9, fill: '#7d8aa0', anchor: 'middle' }), `<line x1="${px(x)}" y1="14" x2="${px(x)}" y2="${height}" stroke="#1b2536" stroke-width="1"/>`);
137
+ for (let y = region.minimumY; y <= region.maximumY; y += 1)
138
+ parts.push(text.render(String(y), {
139
+ x: 4,
140
+ y: py(y) + plaqueHeight / 2 + 3,
141
+ fontSize: 9,
142
+ fill: '#7d8aa0',
143
+ }));
144
+ // Connectors: stem down, bar just above the child's row, stem into the child.
145
+ const connectors = graph.edges.map((edge) => {
146
+ const parent = graph.position.get(edge.parent);
147
+ const child = graph.position.get(edge.child);
148
+ return {
149
+ edge,
150
+ connector: orthogonalConnector({ id: edge.parent, ...parent }, { id: edge.child, ...child }),
151
+ };
152
+ });
153
+ parts.push('<g id="connectors" fill="none" stroke-linejoin="round">');
154
+ for (const { edge, connector } of connectors) {
155
+ if (!inRegion(connector.parentX, connector.parentY) &&
156
+ !inRegion(connector.childX, connector.childY))
157
+ continue;
158
+ const startY = py(connector.parentY) + plaqueHeight;
159
+ const barY = py(connector.barY);
160
+ const endY = py(connector.childY);
161
+ const d = `M ${px(connector.parentX)} ${startY} V ${barY} H ${px(connector.childX)} V ${endY}`;
162
+ parts.push(`<path d="${d}" stroke="${edge.or ? '#8ab4e8' : '#9aa6b8'}" stroke-width="2"${edge.or ? ' stroke-dasharray="3 4"' : ''} data-parent="${escapeXml(edge.parent)}" data-child="${escapeXml(edge.child)}"/>`);
163
+ }
164
+ parts.push('</g>', '<g id="exclusions" fill="none" stroke="#e26d6d" stroke-width="2">');
165
+ for (const [left, right] of graph.exclusions) {
166
+ const a = graph.position.get(left);
167
+ const b = graph.position.get(right);
168
+ if (!inRegion(a.x, a.y) && !inRegion(b.x, b.y))
169
+ continue;
170
+ const sameRow = a.y === b.y;
171
+ const [first, second] = a.x <= b.x ? [a, b] : [b, a];
172
+ if (sameRow) {
173
+ const y = py(a.y) + plaqueHeight / 2;
174
+ parts.push(`<line x1="${px(first.x) + plaqueWidth / 2}" y1="${y}" x2="${px(second.x) - plaqueWidth / 2}" y2="${y}" stroke-dasharray="4 3"/>`);
175
+ }
176
+ else {
177
+ parts.push(`<line x1="${px(a.x)}" y1="${py(a.y) + plaqueHeight / 2}" x2="${px(b.x)}" y2="${py(b.y) + plaqueHeight / 2}" stroke-dasharray="4 3"/>`);
178
+ }
179
+ }
180
+ parts.push('</g>', '<g id="plaques">');
181
+ for (const node of graph.nodes) {
182
+ // Only whole plaques: a focus outside the region would show as a cut-off slice in the margin.
183
+ if (node.x < region.minimumX ||
184
+ node.x > region.maximumX ||
185
+ node.y < region.minimumY ||
186
+ node.y > region.maximumY)
187
+ continue;
188
+ const branch = graph.branchOf.get(node.id) ?? '';
189
+ const fill = branchColours.get(branch) ?? SHARED_FILL;
190
+ const severity = options.highlight?.get(node.id);
191
+ const stroke = severity === 'blocked' ? '#ff5d5d' : severity === undefined ? '#6f7f99' : '#f0b85a';
192
+ const x = px(node.x) - plaqueWidth / 2;
193
+ const y = py(node.y);
194
+ parts.push(`<rect x="${x}" y="${y}" width="${plaqueWidth}" height="${plaqueHeight}" rx="6" fill="${fill}" stroke="${stroke}" stroke-width="${severity === undefined ? 1.5 : 3.5}" data-focus-id="${escapeXml(node.id)}"/>`);
195
+ const lines = labelLines(node.id);
196
+ for (const [index, line] of lines.entries())
197
+ parts.push(text.render(line, {
198
+ x: px(node.x),
199
+ y: y + plaqueHeight / 2 + (lines.length === 1 ? 3 : index === 0 ? -3 : 9),
200
+ fontSize: 9,
201
+ fill: '#eef2f8',
202
+ anchor: 'middle',
203
+ targetWidth: Math.min(plaqueWidth - 6, text.measure(line, 9)),
204
+ }));
205
+ }
206
+ parts.push('</g>');
207
+ if (options.title !== undefined)
208
+ parts.push(text.render(options.title, { x: 28, y: height - 6, fontSize: 10, fill: '#aeb9c8' }));
209
+ return {
210
+ width,
211
+ height,
212
+ svg: (outputWidth, outputHeight) => `<svg xmlns="http://www.w3.org/2000/svg" width="${outputWidth}" height="${outputHeight}" viewBox="0 0 ${width} ${height}" role="img"><defs>${text.definitions()}</defs><rect width="100%" height="100%" fill="#0f1520"/>${parts.join('')}</svg>`,
213
+ };
214
+ }
215
+ /**
216
+ * Renders the layout, or one region of it, to a PNG whose longer edge is at most
217
+ * `maximumEdge` pixels. A small region is enlarged up to 1.5 times so labels stay readable.
218
+ */
219
+ export async function renderSchematicPng(graph, options = {}) {
220
+ const document = schematicDocument(graph, options);
221
+ const maximumEdge = options.maximumEdge ?? 1500;
222
+ let scale = Math.min(MAXIMUM_UPSCALE, maximumEdge / Math.max(document.width, document.height));
223
+ for (let attempt = 0;; attempt += 1) {
224
+ const width = Math.max(1, Math.round(document.width * scale));
225
+ const height = Math.max(1, Math.round(document.height * scale));
226
+ options.budget?.reserve(width, height, 'focus schematic PNG');
227
+ const png = await sharp(Buffer.from(document.svg(width, height), 'utf8'), {
228
+ limitInputPixels: RENDER_MAX_PIXELS,
229
+ })
230
+ .png({ compressionLevel: 9, palette: true })
231
+ .toBuffer();
232
+ if (png.length <= IMAGE_BYTE_BUDGET || attempt >= 3)
233
+ return { png, width, height, region: options.region ?? treeRegion(graph) };
234
+ scale *= 0.7;
235
+ }
236
+ }
237
+ /** Inline image count for one response; the first is always the overview. */
238
+ export const INLINE_IMAGE_LIMIT = 6;
239
+ export function sortedUnique(values) {
240
+ return [...new Set(values)].sort(compareCodeUnits);
241
+ }