@altopelago/sansa 0.9.0 → 0.10.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.
@@ -41,11 +41,11 @@ $.result:number|nan
41
41
  ---
42
42
  $.inventory:list<string>
43
43
  ---
44
- $.version:sep[.]
44
+ $.version:sep["."]
45
45
  ---
46
46
  $.inventory:csv[","]
47
47
  ---
48
- $.claim:type<type>[arg]
48
+ $.claim:type<type>["arg"]
49
49
  ---
50
50
  $#record%object
51
51
  ---
@@ -61,7 +61,7 @@ $:document<invoice>
61
61
  ---
62
62
  $.["list"][2]:rule["+"]
63
63
  ---
64
- $.key:string[","]["."]
64
+ $.key:string[",","."]
65
65
  ---
66
66
  $.["list"][2][2]
67
67
  ---
@@ -0,0 +1,370 @@
1
+ import { defaultPolicy, firstMutateExampleId, mutateExampleGroups, mutateExamples as examples } from './examples.mjs';
2
+ const sourceInput = document.querySelector('#sourceInput');
3
+ const sourceResultOutput = document.querySelector('#sourceResultOutput');
4
+ const requestInput = document.querySelector('#requestInput');
5
+ const requestInputLabel = document.querySelector('#requestInputLabel');
6
+ const resultOutput = document.querySelector('#resultOutput');
7
+ const sourceStatus = document.querySelector('#sourceStatus');
8
+ const requestStatus = document.querySelector('#requestStatus');
9
+ const resultStatus = document.querySelector('#resultStatus');
10
+ const exampleSelect = document.querySelector('#exampleSelect');
11
+ const resetButton = document.querySelector('#resetButton');
12
+ const planButton = document.querySelector('#planButton');
13
+ const applyButton = document.querySelector('#applyButton');
14
+ const requireAtomicInput = document.querySelector('#requireAtomic');
15
+ const recheckPreconditionsInput = document.querySelector('#recheckPreconditions');
16
+ const enforcePolicyInput = document.querySelector('#enforcePolicy');
17
+ const targetFormatInput = document.querySelector('#targetFormat');
18
+ const policyInput = document.querySelector('#policyInput');
19
+ const policyStatus = document.querySelector('#policyStatus');
20
+ const policyTabButton = document.querySelector('#detailTabPolicy');
21
+ const maxOperationsInput = document.querySelector('#maxOperations');
22
+ const maxPreconditionsInput = document.querySelector('#maxPreconditions');
23
+ const maxValueNodesInput = document.querySelector('#maxValueNodes');
24
+ const maxValueDepthInput = document.querySelector('#maxValueDepth');
25
+ const maxStringLengthInput = document.querySelector('#maxStringLength');
26
+ const maxPositionIndexInput = document.querySelector('#maxPositionIndex');
27
+ const optionInputs = Array.from(document.querySelectorAll('.budget-row input'));
28
+ const sourceTabButtons = Array.from(document.querySelectorAll('[data-source-tab]'));
29
+ const detailTabButtons = Array.from(document.querySelectorAll('[data-detail-tab]'));
30
+ const requestKindInputs = Array.from(document.querySelectorAll('input[name="requestKind"]'));
31
+
32
+ let defaultSource = '';
33
+ let lastPayload = null;
34
+ let activeSourceTab = 'input';
35
+ let activeDetailTab = 'diagnostics';
36
+
37
+ await loadDefaults();
38
+ renderExamples();
39
+ policyInput.value = JSON.stringify(defaultPolicy, null, 2);
40
+ renderPolicyControls();
41
+ renderDetailPanel();
42
+ setExample(firstMutateExampleId());
43
+ await runMutation('plan');
44
+
45
+ exampleSelect.addEventListener('change', () => {
46
+ setExample(exampleSelect.value);
47
+ void runMutation('plan');
48
+ });
49
+
50
+ resetButton.addEventListener('click', () => {
51
+ sourceInput.value = defaultSource;
52
+ sourceStatus.textContent = 'default aeon';
53
+ setExample(exampleSelect.value);
54
+ void runMutation('plan');
55
+ });
56
+
57
+ planButton.addEventListener('click', () => {
58
+ void runMutation('plan');
59
+ });
60
+
61
+ applyButton.addEventListener('click', () => {
62
+ void runMutation('apply');
63
+ });
64
+
65
+ document.querySelectorAll('input[name="outputMode"]').forEach((input) => {
66
+ input.addEventListener('change', () => {
67
+ renderPayload(lastPayload);
68
+ });
69
+ });
70
+
71
+ targetFormatInput.addEventListener('change', () => {
72
+ void runMutation('plan');
73
+ });
74
+
75
+ for (const input of requestKindInputs) {
76
+ input.addEventListener('change', () => {
77
+ setExampleVariant(currentExample(), input.value);
78
+ void runMutation('plan');
79
+ });
80
+ }
81
+
82
+ enforcePolicyInput.addEventListener('change', () => {
83
+ activeDetailTab = enforcePolicyInput.checked ? 'policy' : 'diagnostics';
84
+ renderPolicyControls();
85
+ renderDetailPanel();
86
+ void runMutation('plan');
87
+ });
88
+
89
+ policyInput.addEventListener('input', () => {
90
+ policyStatus.textContent = enforcePolicyInput.checked ? 'edited policy' : 'disabled';
91
+ });
92
+
93
+ for (const button of sourceTabButtons) {
94
+ button.addEventListener('click', () => {
95
+ activeSourceTab = button.dataset.sourceTab;
96
+ renderSourcePanel();
97
+ });
98
+ }
99
+
100
+ for (const button of detailTabButtons) {
101
+ button.addEventListener('click', () => {
102
+ activeDetailTab = button.dataset.detailTab;
103
+ renderDetailPanel();
104
+ });
105
+ }
106
+
107
+ sourceInput.addEventListener('input', () => {
108
+ sourceStatus.textContent = 'edited source';
109
+ sourceResultOutput.textContent = 'Run Plan or Apply to render the current source result.';
110
+ });
111
+
112
+ requestInput.addEventListener('input', () => {
113
+ requestStatus.textContent = 'edited request';
114
+ });
115
+
116
+ requestInput.addEventListener('keydown', (event) => {
117
+ if ((event.metaKey || event.ctrlKey) && event.key === 'Enter') {
118
+ event.preventDefault();
119
+ void runMutation('plan');
120
+ }
121
+ });
122
+
123
+ async function loadDefaults() {
124
+ defaultSource = await fetchText('/fixtures/query-inventory.aeon', [
125
+ 'inventory = {',
126
+ ' items:list<object> = [',
127
+ ' { sku:string = "A-100" name:string = "Adapter" qty:int = 1 }',
128
+ ' { sku:string = "B-200" name:string = "Bolt" qty:int = 3 }',
129
+ ' ]',
130
+ '}',
131
+ ].join('\n'));
132
+ sourceInput.value = defaultSource;
133
+ sourceStatus.textContent = 'default aeon';
134
+ }
135
+
136
+ function renderExamples() {
137
+ exampleSelect.replaceChildren(...mutateExampleGroups.map(({ label, examples: groupExamples }) => {
138
+ const group = document.createElement('optgroup');
139
+ group.label = label;
140
+ group.replaceChildren(...groupExamples.map((example) => {
141
+ const option = document.createElement('option');
142
+ option.value = example.id;
143
+ option.textContent = example.label;
144
+ return option;
145
+ }));
146
+ return group;
147
+ }));
148
+ }
149
+
150
+ function setExample(id) {
151
+ const example = examples.find((entry) => entry.id === id) ?? examples[0];
152
+ exampleSelect.value = example.id;
153
+ const kind = requestKindForExample(example, requestKind());
154
+ setExampleVariant(example, kind);
155
+ }
156
+
157
+ function setExampleVariant(example, kind) {
158
+ if (!example) return;
159
+ const selectedKind = requestKindForExample(example, kind);
160
+ const variant = example.variants[selectedKind];
161
+ updateRequestKindAvailability(example);
162
+ setRequestKind(selectedKind);
163
+ requestInput.value = selectedKind === 'instruction'
164
+ ? variant.request
165
+ : JSON.stringify(variant.request, null, 2);
166
+ requestStatus.textContent = exampleVariantStatus(example);
167
+ setOptionInputs(variant.options ?? {});
168
+ }
169
+
170
+ async function runMutation(mode) {
171
+ resultStatus.textContent = mode === 'apply' ? 'applying' : 'planning';
172
+ resultStatus.dataset.phase = mode === 'apply' ? 'apply' : 'plan';
173
+ const payload = await mutateApi({
174
+ source: sourceInput.value,
175
+ requestSource: requestInput.value,
176
+ requestKind: requestKind(),
177
+ mode,
178
+ options: mutateOptions(),
179
+ });
180
+ lastPayload = payload;
181
+ const status = mutationStatus(payload, mode);
182
+ resultStatus.textContent = status.label;
183
+ resultStatus.dataset.phase = status.phase;
184
+ renderSourceResult(payload);
185
+ renderPayload(payload);
186
+ }
187
+
188
+ async function mutateApi(payload) {
189
+ try {
190
+ const response = await fetch('/api/mutate', {
191
+ method: 'POST',
192
+ headers: { 'content-type': 'application/json' },
193
+ body: JSON.stringify(payload),
194
+ });
195
+ return await response.json();
196
+ } catch (error) {
197
+ return {
198
+ ok: false,
199
+ text: `${error.name}: ${error.message}`,
200
+ errors: [{ code: 'SANSA_MUTATE_WORKBENCH_API_ERROR', message: error.message }],
201
+ };
202
+ }
203
+ }
204
+
205
+ function renderPayload(payload) {
206
+ if (!payload) {
207
+ resultOutput.textContent = '';
208
+ return;
209
+ }
210
+ switch (outputMode()) {
211
+ case 'json':
212
+ resultOutput.textContent = JSON.stringify(payload, null, 2);
213
+ break;
214
+ default:
215
+ resultOutput.textContent = payload.text ?? JSON.stringify(payload, null, 2);
216
+ break;
217
+ }
218
+ }
219
+
220
+ function mutationStatus(payload, requestedMode) {
221
+ if (payload?.ok) {
222
+ const phase = requestedMode === 'apply' ? 'apply' : 'plan';
223
+ return { phase, label: `${phase} ok` };
224
+ }
225
+ const phase = payload?.phase ?? payload?.errors?.[0]?.phase ?? 'error';
226
+ const count = payload?.errors?.length ?? 0;
227
+ const suffix = count > 1 ? ` ${count}` : '';
228
+ return { phase, label: `${phase} error${suffix}` };
229
+ }
230
+
231
+ function renderSourcePanel() {
232
+ for (const button of sourceTabButtons) {
233
+ const selected = button.dataset.sourceTab === activeSourceTab;
234
+ button.setAttribute('aria-selected', selected ? 'true' : 'false');
235
+ }
236
+ sourceInput.classList.toggle('hidden', activeSourceTab !== 'input');
237
+ sourceResultOutput.classList.toggle('hidden', activeSourceTab !== 'result');
238
+ }
239
+
240
+ function renderSourceResult(payload) {
241
+ sourceResultOutput.textContent = payload?.source
242
+ ?? 'Run Plan or Apply to render the current source result.';
243
+ }
244
+
245
+ function renderDetailPanel() {
246
+ if (!enforcePolicyInput.checked && activeDetailTab === 'policy') {
247
+ activeDetailTab = 'diagnostics';
248
+ }
249
+ for (const button of detailTabButtons) {
250
+ const tab = button.dataset.detailTab;
251
+ const selected = tab === activeDetailTab;
252
+ button.setAttribute('aria-selected', selected ? 'true' : 'false');
253
+ }
254
+ resultOutput.classList.toggle('hidden', activeDetailTab !== 'diagnostics');
255
+ policyInput.classList.toggle('hidden', activeDetailTab !== 'policy');
256
+ }
257
+
258
+ function mutateOptions() {
259
+ const options = {
260
+ requireAtomic: requireAtomicInput.checked,
261
+ recheckPreconditions: recheckPreconditionsInput.checked,
262
+ targetFormat: targetFormatInput.value,
263
+ };
264
+ if (enforcePolicyInput.checked) {
265
+ options.policySource = policyInput.value;
266
+ }
267
+ for (const input of optionInputs) {
268
+ const value = input.value.trim();
269
+ if (value.length === 0) continue;
270
+ const number = Number(value);
271
+ if (Number.isSafeInteger(number) && number >= 0) options[input.id] = number;
272
+ }
273
+ return options;
274
+ }
275
+
276
+ function renderPolicyControls() {
277
+ policyTabButton.classList.toggle('hidden', !enforcePolicyInput.checked);
278
+ policyStatus.textContent = enforcePolicyInput.checked ? 'enabled' : 'disabled';
279
+ }
280
+
281
+ function requestKind() {
282
+ return document.querySelector('input[name="requestKind"]:checked')?.value ?? 'structured';
283
+ }
284
+
285
+ function currentExample() {
286
+ return examples.find((entry) => entry.id === exampleSelect.value) ?? examples[0];
287
+ }
288
+
289
+ function requestKindForExample(example, preferredKind) {
290
+ if (example.variants[preferredKind]) return preferredKind;
291
+ if (example.variants.structured) return 'structured';
292
+ if (example.variants.instruction) return 'instruction';
293
+ return 'structured';
294
+ }
295
+
296
+ function updateRequestKindAvailability(example) {
297
+ for (const input of requestKindInputs) {
298
+ const available = Boolean(example.variants[input.value]);
299
+ input.disabled = !available;
300
+ const label = input.closest('label');
301
+ label?.classList.toggle('is-disabled', !available);
302
+ if (label) {
303
+ label.title = available
304
+ ? ''
305
+ : `${input.value === 'instruction' ? 'Instruction' : 'Structured JSON'} is not available for this example`;
306
+ }
307
+ }
308
+ }
309
+
310
+ function exampleVariantStatus(example) {
311
+ const hasStructured = Boolean(example.variants.structured);
312
+ const hasInstruction = Boolean(example.variants.instruction);
313
+ if (hasStructured && hasInstruction) return 'example loaded';
314
+ if (hasStructured) return 'structured-only example';
315
+ if (hasInstruction) return 'instruction-only example';
316
+ return 'example loaded';
317
+ }
318
+
319
+ function setRequestKind(kind) {
320
+ for (const input of requestKindInputs) {
321
+ input.checked = input.value === kind;
322
+ }
323
+ updateRequestKindLabel();
324
+ }
325
+
326
+ function updateRequestKindLabel() {
327
+ requestInputLabel.textContent = requestKind() === 'instruction'
328
+ ? 'SANSA Instruction'
329
+ : 'Mutation Request JSON';
330
+ }
331
+
332
+ function requestKindLabel() {
333
+ return requestKind() === 'instruction' ? 'instruction' : 'structured JSON';
334
+ }
335
+
336
+ function setOptionInputs(options) {
337
+ targetFormatInput.value = options.targetFormat ?? 'aeon';
338
+ if (options.policySource !== undefined) {
339
+ enforcePolicyInput.checked = true;
340
+ policyInput.value = options.policySource;
341
+ activeDetailTab = 'policy';
342
+ } else {
343
+ enforcePolicyInput.checked = false;
344
+ policyInput.value = JSON.stringify(defaultPolicy, null, 2);
345
+ if (activeDetailTab === 'policy') activeDetailTab = 'diagnostics';
346
+ }
347
+ renderPolicyControls();
348
+ renderDetailPanel();
349
+ maxOperationsInput.value = options.maxOperations ?? '';
350
+ maxPreconditionsInput.value = options.maxPreconditions ?? '';
351
+ maxValueNodesInput.value = options.maxValueNodes ?? '';
352
+ maxValueDepthInput.value = options.maxValueDepth ?? '';
353
+ maxStringLengthInput.value = options.maxStringLength ?? '';
354
+ maxPositionIndexInput.value = options.maxPositionIndex ?? '';
355
+ }
356
+
357
+ function outputMode() {
358
+ return document.querySelector('input[name="outputMode"]:checked')?.value ?? 'text';
359
+ }
360
+
361
+ async function fetchText(path, fallback) {
362
+ try {
363
+ const response = await fetch(path);
364
+ if (!response.ok) throw new Error(`${response.status} ${response.statusText}`);
365
+ return await response.text();
366
+ } catch (error) {
367
+ resultOutput.textContent = `SANSA_MUTATE_WORKBENCH_FIXTURE_LOAD: ${error.message}`;
368
+ return fallback;
369
+ }
370
+ }