dsh-artifact-studio 0.8.0 → 0.9.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.
@@ -0,0 +1,384 @@
1
+ /**
2
+ * Structural edits to a sheet workbook: the operations behind
3
+ * artifact_sheet_update and the panel's own mutations. Every operation is
4
+ * validated before it touches the model, and the result is re-validated as a
5
+ * whole, so a bad path cannot leave a half-valid workbook in memory.
6
+ */
7
+ import { SHEET_LIMITS, SheetError, checkSheetName, cloneCell, cloneWorkbook, columnIndex, columnName, formatCellRef, formatRangeRef, mergeStyle, normalizeSheetWorkbook, parseCellRef, parseRangeRef, parseSheetWorkbook, rangeCells, rangeSize, sheetOf, } from './model.js';
8
+ const HEX = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/i;
9
+ const NUMBER_FORMATS = ['general', 'number', 'integer', 'currency', 'percent', 'date'];
10
+ const ALIGNS = ['left', 'center', 'right'];
11
+ function isFields(value) {
12
+ return value !== null && typeof value === 'object' && !Array.isArray(value);
13
+ }
14
+ function fail(message) {
15
+ throw new SheetError(message, 'sheet/operation');
16
+ }
17
+ function onlyKeys(value, allowed, at) {
18
+ for (const key of Object.keys(value)) {
19
+ if (!allowed.includes(key))
20
+ fail(at + ' has an unknown field "' + key + '". Use ' + allowed.join(', ') + '.');
21
+ }
22
+ }
23
+ function colorOf(raw, at) {
24
+ if (typeof raw !== 'string' || !HEX.test(raw.trim()))
25
+ fail(at + ' must be a hex color such as #1f2933.');
26
+ const color = raw.trim().toLowerCase();
27
+ if (color.length === 4)
28
+ return '#' + color[1] + color[1] + color[2] + color[2] + color[3] + color[3];
29
+ return color;
30
+ }
31
+ function stylePatch(raw, at) {
32
+ if (!isFields(raw))
33
+ fail(at + ' must be an object of style fields.');
34
+ const allowed = ['bold', 'italic', 'underline', 'strike', 'align', 'color', 'background', 'numberFormat'];
35
+ onlyKeys(raw, allowed, at);
36
+ const patch = {};
37
+ for (const key of ['bold', 'italic', 'underline', 'strike']) {
38
+ if (raw[key] === undefined)
39
+ continue;
40
+ if (raw[key] !== null && typeof raw[key] !== 'boolean')
41
+ fail(at + '.' + key + ' must be true, false, or null.');
42
+ patch[key] = raw[key] === null ? null : raw[key] === true;
43
+ }
44
+ if (raw.align !== undefined) {
45
+ if (raw.align !== null && (typeof raw.align !== 'string' || !ALIGNS.includes(raw.align)))
46
+ fail(at + '.align must be left, center, right, or null.');
47
+ patch.align = raw.align === null ? null : raw.align;
48
+ }
49
+ if (raw.numberFormat !== undefined) {
50
+ if (raw.numberFormat !== null && (typeof raw.numberFormat !== 'string' || !NUMBER_FORMATS.includes(raw.numberFormat)))
51
+ fail(at + '.numberFormat must be ' + NUMBER_FORMATS.join(', ') + ', or null.');
52
+ patch.numberFormat = raw.numberFormat === null ? null : raw.numberFormat;
53
+ }
54
+ if (raw.color !== undefined)
55
+ patch.color = raw.color === null ? null : colorOf(raw.color, at + '.color');
56
+ if (raw.background !== undefined)
57
+ patch.background = raw.background === null ? null : colorOf(raw.background, at + '.background');
58
+ if (Object.keys(patch).length === 0)
59
+ fail(at + ' must set at least one style field.');
60
+ return patch;
61
+ }
62
+ function scalarOf(raw, at) {
63
+ if (raw === null || raw === undefined)
64
+ return null;
65
+ if (typeof raw === 'string') {
66
+ if (raw.length > SHEET_LIMITS.maxCellText)
67
+ fail(at + ' is longer than ' + String(SHEET_LIMITS.maxCellText) + ' characters.');
68
+ return raw;
69
+ }
70
+ if (typeof raw === 'number') {
71
+ if (!Number.isFinite(raw))
72
+ fail(at + ' must be a finite number.');
73
+ return raw;
74
+ }
75
+ if (typeof raw === 'boolean')
76
+ return raw;
77
+ return fail(at + ' must be a string, number, boolean, or null.');
78
+ }
79
+ function formulaOf(raw, at) {
80
+ if (typeof raw !== 'string')
81
+ fail(at + ' must be a formula string.');
82
+ let formula = raw.trim();
83
+ if (formula === '' || formula === '=')
84
+ fail(at + ' is empty.');
85
+ if (!formula.startsWith('='))
86
+ formula = '=' + formula;
87
+ if (formula.length > SHEET_LIMITS.maxFormula)
88
+ fail(at + ' is longer than ' + String(SHEET_LIMITS.maxFormula) + ' characters.');
89
+ return formula;
90
+ }
91
+ function sheetAt(workbook, name) {
92
+ return sheetOf(workbook, name);
93
+ }
94
+ function ensureRoom(sheet, extra) {
95
+ const cells = Object.keys(sheet.cells).length;
96
+ if (cells + extra > SHEET_LIMITS.maxCells)
97
+ fail('A sheet artifact holds at most ' + String(SHEET_LIMITS.maxCells) + ' cells.');
98
+ }
99
+ function setCell(sheet, ref, cell) {
100
+ if (cell === undefined)
101
+ delete sheet.cells[ref];
102
+ else
103
+ sheet.cells[ref] = cell;
104
+ }
105
+ function applySet(sheet, op) {
106
+ const index = parseCellRef(op.ref);
107
+ if (index === undefined)
108
+ fail('"' + op.ref + '" is not a valid cell reference.');
109
+ const ref = formatCellRef(index.row, index.column);
110
+ if (op.value !== undefined && op.formula !== undefined)
111
+ fail('A cell cannot set both value and formula.');
112
+ if (op.value === undefined && op.formula === undefined && op.style === undefined)
113
+ fail('A set operation needs value, formula, or style.');
114
+ const existing = sheet.cells[ref];
115
+ if (existing === undefined)
116
+ ensureRoom(sheet, 1);
117
+ const next = existing === undefined ? {} : cloneCell(existing);
118
+ if (op.formula !== undefined) {
119
+ next.formula = formulaOf(op.formula, 'The formula');
120
+ delete next.value;
121
+ }
122
+ else if (op.value !== undefined) {
123
+ if (op.value === null)
124
+ delete next.value;
125
+ else
126
+ next.value = op.value;
127
+ delete next.formula;
128
+ }
129
+ if (op.style !== undefined) {
130
+ const merged = mergeStyle(next.style, op.style);
131
+ if (merged === undefined)
132
+ delete next.style;
133
+ else
134
+ next.style = merged;
135
+ }
136
+ const hasContent = next.formula !== undefined || next.value !== undefined || next.style !== undefined;
137
+ setCell(sheet, ref, hasContent ? next : undefined);
138
+ }
139
+ function applyWrite(sheet, op) {
140
+ const start = parseCellRef(op.ref);
141
+ if (start === undefined)
142
+ fail('"' + op.ref + '" is not a valid cell reference.');
143
+ const blockCells = op.values.reduce((total, row) => total + row.length, 0);
144
+ ensureRoom(sheet, blockCells);
145
+ op.values.forEach((values, rowOffset) => {
146
+ values.forEach((raw, columnOffset) => {
147
+ const row = start.row + rowOffset;
148
+ const column = start.column + columnOffset;
149
+ if (row > SHEET_LIMITS.maxRows || column >= SHEET_LIMITS.maxColumns)
150
+ fail('The written block leaves the sheet bounds.');
151
+ const ref = formatCellRef(row, column);
152
+ const existing = sheet.cells[ref];
153
+ if (raw === null) {
154
+ if (existing === undefined)
155
+ return;
156
+ const next = {};
157
+ if (existing.style !== undefined)
158
+ next.style = { ...existing.style };
159
+ setCell(sheet, ref, next.style === undefined ? undefined : next);
160
+ }
161
+ else if (typeof raw === 'string' && raw.startsWith('=')) {
162
+ setCell(sheet, ref, { formula: formulaOf(raw, 'A written cell') });
163
+ }
164
+ else {
165
+ setCell(sheet, ref, { value: raw });
166
+ }
167
+ });
168
+ });
169
+ }
170
+ function applyFormat(sheet, op) {
171
+ const range = parseRangeRef(op.range);
172
+ if (range === undefined)
173
+ fail('"' + op.range + '" is not a valid range.');
174
+ if (rangeSize(range) > SHEET_LIMITS.maxOperationCells)
175
+ fail('A format range may cover at most ' + String(SHEET_LIMITS.maxOperationCells) + ' cells.');
176
+ let added = 0;
177
+ for (const cell of rangeCells(range)) {
178
+ const ref = formatCellRef(cell.row, cell.column);
179
+ const existing = sheet.cells[ref];
180
+ if (existing === undefined)
181
+ added += 1;
182
+ const next = existing === undefined ? {} : cloneCell(existing);
183
+ const merged = mergeStyle(next.style, op.style);
184
+ if (merged === undefined)
185
+ delete next.style;
186
+ else
187
+ next.style = merged;
188
+ const hasContent = next.formula !== undefined || next.value !== undefined || next.style !== undefined;
189
+ setCell(sheet, ref, hasContent ? next : undefined);
190
+ }
191
+ ensureRoom(sheet, added);
192
+ }
193
+ function applyClear(sheet, range) {
194
+ for (const ref of Object.keys(sheet.cells)) {
195
+ const index = parseCellRef(ref);
196
+ if (index === undefined)
197
+ continue;
198
+ if (index.row >= range.row1 && index.row <= range.row2 && index.column >= range.column1 && index.column <= range.column2)
199
+ delete sheet.cells[ref];
200
+ }
201
+ }
202
+ /**
203
+ * Validate the operations argument.
204
+ * @param value the raw tool value.
205
+ * @returns the validated operations.
206
+ */
207
+ export function parseSheetOperations(value) {
208
+ if (!Array.isArray(value) || value.length === 0)
209
+ fail('Operations must be a non-empty array.');
210
+ if (value.length > SHEET_LIMITS.maxOperations)
211
+ fail('At most ' + String(SHEET_LIMITS.maxOperations) + ' operations are allowed in one call.');
212
+ return value.map((raw, index) => {
213
+ const at = 'operations[' + String(index) + ']';
214
+ if (!isFields(raw))
215
+ fail(at + ' must be an object.');
216
+ const op = raw.op;
217
+ if (typeof op !== 'string')
218
+ fail(at + '.op must be one of set, write, clear, format, add-sheet, rename-sheet, delete-sheet, set-column-width.');
219
+ switch (op) {
220
+ case 'set': {
221
+ onlyKeys(raw, ['op', 'sheet', 'ref', 'value', 'formula', 'style'], at);
222
+ const sheet = raw.sheet === undefined ? undefined : sheetName(raw.sheet, at + '.sheet');
223
+ if (typeof raw.ref !== 'string')
224
+ fail(at + '.ref must be a cell reference.');
225
+ const value = raw.value === undefined ? undefined : scalarOf(raw.value, at + '.value');
226
+ const formula = raw.formula === undefined ? undefined : formulaOf(raw.formula, at + '.formula');
227
+ const style = raw.style === undefined ? undefined : stylePatch(raw.style, at + '.style');
228
+ return {
229
+ op: 'set',
230
+ ...(sheet === undefined ? {} : { sheet }),
231
+ ref: raw.ref,
232
+ ...(value === undefined ? {} : { value }),
233
+ ...(formula === undefined ? {} : { formula }),
234
+ ...(style === undefined ? {} : { style }),
235
+ };
236
+ }
237
+ case 'write': {
238
+ onlyKeys(raw, ['op', 'sheet', 'ref', 'values'], at);
239
+ const sheet = raw.sheet === undefined ? undefined : sheetName(raw.sheet, at + '.sheet');
240
+ if (typeof raw.ref !== 'string')
241
+ fail(at + '.ref must be a cell reference.');
242
+ if (!Array.isArray(raw.values) || raw.values.length === 0 || raw.values.length > 200)
243
+ fail(at + '.values must be a non-empty array of up to 200 rows.');
244
+ let total = 0;
245
+ const values = raw.values.map((row, rowIndex) => {
246
+ if (!Array.isArray(row) || row.length === 0 || row.length > 100)
247
+ fail(at + '.values[' + String(rowIndex) + '] must be a non-empty array of up to 100 cells.');
248
+ total += row.length;
249
+ if (total > SHEET_LIMITS.maxOperationCells)
250
+ fail(at + ' writes at most ' + String(SHEET_LIMITS.maxOperationCells) + ' cells.');
251
+ return row.map((cell, cellIndex) => scalarOf(cell, at + '.values[' + String(rowIndex) + '][' + String(cellIndex) + ']'));
252
+ });
253
+ return { op: 'write', ...(sheet === undefined ? {} : { sheet }), ref: raw.ref, values };
254
+ }
255
+ case 'clear': {
256
+ onlyKeys(raw, ['op', 'sheet', 'range'], at);
257
+ const sheet = raw.sheet === undefined ? undefined : sheetName(raw.sheet, at + '.sheet');
258
+ if (typeof raw.range !== 'string' || parseRangeRef(raw.range) === undefined)
259
+ fail(at + '.range must be a range such as A1:C4.');
260
+ return { op: 'clear', ...(sheet === undefined ? {} : { sheet }), range: raw.range };
261
+ }
262
+ case 'format': {
263
+ onlyKeys(raw, ['op', 'sheet', 'range', 'style'], at);
264
+ const sheet = raw.sheet === undefined ? undefined : sheetName(raw.sheet, at + '.sheet');
265
+ if (typeof raw.range !== 'string' || parseRangeRef(raw.range) === undefined)
266
+ fail(at + '.range must be a range such as A1:C4.');
267
+ return { op: 'format', ...(sheet === undefined ? {} : { sheet }), range: raw.range, style: stylePatch(raw.style, at + '.style') };
268
+ }
269
+ case 'add-sheet': {
270
+ onlyKeys(raw, ['op', 'name'], at);
271
+ return { op: 'add-sheet', name: checkSheetName(raw.name, at + '.name') };
272
+ }
273
+ case 'rename-sheet': {
274
+ onlyKeys(raw, ['op', 'from', 'to'], at);
275
+ return { op: 'rename-sheet', from: checkSheetName(raw.from, at + '.from'), to: checkSheetName(raw.to, at + '.to') };
276
+ }
277
+ case 'delete-sheet': {
278
+ onlyKeys(raw, ['op', 'name'], at);
279
+ return { op: 'delete-sheet', name: checkSheetName(raw.name, at + '.name') };
280
+ }
281
+ case 'set-column-width': {
282
+ onlyKeys(raw, ['op', 'sheet', 'column', 'width'], at);
283
+ const sheet = raw.sheet === undefined ? undefined : sheetName(raw.sheet, at + '.sheet');
284
+ if (typeof raw.column !== 'string' || columnIndex(raw.column) === undefined)
285
+ fail(at + '.column must be one to three letters, such as A or AB.');
286
+ if (typeof raw.width !== 'number' || !Number.isInteger(raw.width) || raw.width < SHEET_LIMITS.minColumnWidth || raw.width > SHEET_LIMITS.maxColumnWidth) {
287
+ fail(at + '.width must be a whole number from ' + String(SHEET_LIMITS.minColumnWidth) + ' to ' + String(SHEET_LIMITS.maxColumnWidth) + '.');
288
+ }
289
+ return { op: 'set-column-width', ...(sheet === undefined ? {} : { sheet }), column: raw.column, width: raw.width };
290
+ }
291
+ default:
292
+ fail(at + '.op must be one of set, write, clear, format, add-sheet, rename-sheet, delete-sheet, set-column-width.');
293
+ }
294
+ });
295
+ }
296
+ function sheetName(raw, at) {
297
+ return checkSheetName(raw, at);
298
+ }
299
+ /**
300
+ * Apply validated operations to a workbook.
301
+ * @param text the current stored content.
302
+ * @param operations the raw operations argument.
303
+ * @returns the next workbook and one note per operation.
304
+ */
305
+ export function applySheetOperations(text, operations) {
306
+ const workbook = cloneWorkbook(parseSheetWorkbook(text));
307
+ const parsed = parseSheetOperations(operations);
308
+ const notes = [];
309
+ for (const entry of parsed) {
310
+ switch (entry.op) {
311
+ case 'set': {
312
+ const sheet = sheetAt(workbook, entry.sheet);
313
+ applySet(sheet, entry);
314
+ notes.push('set ' + sheet.name + '!' + formatCellRef(parseCellRef(entry.ref)?.row ?? 1, parseCellRef(entry.ref)?.column ?? 0));
315
+ break;
316
+ }
317
+ case 'write': {
318
+ const sheet = sheetAt(workbook, entry.sheet);
319
+ applyWrite(sheet, entry);
320
+ notes.push('wrote a block at ' + sheet.name + '!' + entry.ref);
321
+ break;
322
+ }
323
+ case 'clear': {
324
+ const sheet = sheetAt(workbook, entry.sheet);
325
+ const range = parseRangeRef(entry.range);
326
+ if (range === undefined)
327
+ fail('"' + entry.range + '" is not a valid range.');
328
+ applyClear(sheet, range);
329
+ notes.push('cleared ' + sheet.name + '!' + formatRangeRef(range));
330
+ break;
331
+ }
332
+ case 'format': {
333
+ const sheet = sheetAt(workbook, entry.sheet);
334
+ applyFormat(sheet, entry);
335
+ notes.push('formatted ' + sheet.name + '!' + entry.range);
336
+ break;
337
+ }
338
+ case 'add-sheet': {
339
+ if (workbook.sheets.length >= SHEET_LIMITS.maxSheets)
340
+ fail('A workbook holds at most ' + String(SHEET_LIMITS.maxSheets) + ' sheets.');
341
+ if (workbook.sheets.some((sheet) => sheet.name.toLowerCase() === entry.name.toLowerCase()))
342
+ fail('A sheet is already named "' + entry.name + '".');
343
+ workbook.sheets.push({ name: entry.name, cells: {} });
344
+ notes.push('added sheet ' + entry.name);
345
+ break;
346
+ }
347
+ case 'rename-sheet': {
348
+ const sheet = sheetOf(workbook, entry.from);
349
+ const next = checkSheetName(entry.to);
350
+ if (workbook.sheets.some((item) => item !== sheet && item.name.toLowerCase() === next.toLowerCase()))
351
+ fail('A sheet is already named "' + next + '".');
352
+ sheet.name = next;
353
+ notes.push('renamed sheet ' + entry.from + ' to ' + next);
354
+ break;
355
+ }
356
+ case 'delete-sheet': {
357
+ const index = workbook.sheets.findIndex((sheet) => sheet.name.toLowerCase() === entry.name.toLowerCase());
358
+ if (index === -1)
359
+ fail('No sheet is named "' + entry.name + '".');
360
+ if (workbook.sheets.length === 1)
361
+ fail('A workbook needs at least one sheet.');
362
+ workbook.sheets.splice(index, 1);
363
+ notes.push('deleted sheet ' + entry.name);
364
+ break;
365
+ }
366
+ case 'set-column-width': {
367
+ const sheet = sheetAt(workbook, entry.sheet);
368
+ const column = columnIndex(entry.column);
369
+ if (column === undefined)
370
+ fail('"' + entry.column + '" is not a column.');
371
+ const widths = sheet.columnWidths ?? {};
372
+ widths[columnName(column)] = entry.width;
373
+ sheet.columnWidths = widths;
374
+ notes.push('set column ' + columnName(column) + ' on ' + sheet.name + ' to ' + String(entry.width) + 'px');
375
+ break;
376
+ }
377
+ default: {
378
+ const exhaustive = entry;
379
+ fail('Unknown operation ' + String(exhaustive) + '.');
380
+ }
381
+ }
382
+ }
383
+ return { workbook: normalizeSheetWorkbook(workbook), notes };
384
+ }
@@ -0,0 +1,170 @@
1
+ /**
2
+ * Read-only views of a sheet workbook: the bounds and cells a preview or an
3
+ * export shows, a CSV rendering, a selection quote for an agent request, and
4
+ * the advisory formula diagnostics a write returns.
5
+ */
6
+ import { SHEET_LIMITS, formatCellRef, formatRangeRef, parseRangeRef, sheetOf, usedBounds } from './model.js';
7
+ import { evaluateWorkbook } from './formula.js';
8
+ /**
9
+ * The used bounds of a sheet as one-based rows and columns.
10
+ * @param sheet the sheet.
11
+ * @returns the bounds.
12
+ */
13
+ export function sheetBounds(sheet) {
14
+ const bounds = usedBounds(sheet);
15
+ return { rows: Math.max(1, bounds.rows), columns: Math.max(1, bounds.columns) };
16
+ }
17
+ /**
18
+ * Build a rectangular table for a panel cover or an HTML export.
19
+ * @param workbook the workbook.
20
+ * @param evaluated the evaluated cells.
21
+ * @param sheetName the sheet, first when omitted.
22
+ * @param maxRows the most rows to include.
23
+ * @param maxColumns the most columns to include.
24
+ * @returns the table.
25
+ */
26
+ export function sheetTable(workbook, evaluated, sheetName, maxRows = SHEET_LIMITS.maxRows, maxColumns = SHEET_LIMITS.maxColumns) {
27
+ const sheet = sheetOf(workbook, sheetName);
28
+ const bounds = sheetBounds(sheet);
29
+ const rows = Math.min(bounds.rows, maxRows);
30
+ const columns = Math.min(bounds.columns, maxColumns);
31
+ const results = evaluated.results[sheet.name] ?? {};
32
+ const cells = [];
33
+ for (let row = 1; row <= rows; row += 1) {
34
+ for (let column = 0; column < columns; column += 1) {
35
+ const ref = formatCellRef(row, column);
36
+ const cell = sheet.cells[ref];
37
+ const result = results[ref];
38
+ const display = result?.display ?? '';
39
+ if (cell === undefined && display === '')
40
+ continue;
41
+ cells.push({
42
+ row,
43
+ column,
44
+ ref,
45
+ display,
46
+ value: result?.value ?? null,
47
+ ...(result?.error === undefined ? {} : { error: result.error }),
48
+ ...(cell?.style === undefined ? {} : { style: cell.style }),
49
+ });
50
+ }
51
+ }
52
+ return { name: sheet.name, rows, columns, cells };
53
+ }
54
+ /** The overview rows a tool result carries; it has no cell content. */
55
+ export function sheetOverview(workbook) {
56
+ return workbook.sheets.map((sheet) => {
57
+ const bounds = sheetBounds(sheet);
58
+ let formulas = 0;
59
+ for (const cell of Object.values(sheet.cells))
60
+ if (cell.formula !== undefined)
61
+ formulas += 1;
62
+ return { name: sheet.name, rows: bounds.rows, columns: bounds.columns, cells: Object.keys(sheet.cells).length, formulas };
63
+ });
64
+ }
65
+ function csvCell(text) {
66
+ if (/[",\r\n]/.test(text))
67
+ return '"' + text.replace(/"/g, '""') + '"';
68
+ return text;
69
+ }
70
+ /**
71
+ * Render one sheet as RFC 4180 CSV. Values are the displayed values, so a
72
+ * formula exports its result and a formatted number exports its format.
73
+ * @param workbook the workbook.
74
+ * @param evaluated the evaluated cells.
75
+ * @param sheetName the sheet, first when omitted.
76
+ * @returns the CSV text.
77
+ */
78
+ export function sheetToCsv(workbook, evaluated, sheetName) {
79
+ const table = sheetTable(workbook, evaluated, sheetName);
80
+ const byPosition = new Map();
81
+ for (const cell of table.cells)
82
+ byPosition.set(String(cell.row) + ',' + String(cell.column), cell);
83
+ const lines = [];
84
+ for (let row = 1; row <= table.rows; row += 1) {
85
+ const values = [];
86
+ for (let column = 0; column < table.columns; column += 1) {
87
+ const cell = byPosition.get(String(row) + ',' + String(column));
88
+ values.push(csvCell(cell?.display ?? ''));
89
+ }
90
+ lines.push(values.join(','));
91
+ }
92
+ return lines.join('\r\n') + '\r\n';
93
+ }
94
+ /**
95
+ * A quote for an agent request: the range and its displayed values as TSV.
96
+ * It never carries the whole sheet.
97
+ * @param workbook the workbook.
98
+ * @param evaluated the evaluated cells.
99
+ * @param sheetName the sheet.
100
+ * @param rangeRef the selected range.
101
+ * @returns the bounded quote.
102
+ */
103
+ export function selectionQuote(workbook, evaluated, sheetName, rangeRef) {
104
+ const sheet = sheetOf(workbook, sheetName);
105
+ const range = parseRangeRef(rangeRef);
106
+ if (range === undefined)
107
+ return sheetName + '!' + rangeRef;
108
+ const results = evaluated.results[sheet.name] ?? {};
109
+ const lastRow = Math.min(range.row2, range.row1 + 19);
110
+ const lastColumn = Math.min(range.column2, range.column1 + 11);
111
+ const lines = [sheet.name + '!' + formatRangeRef(range)];
112
+ for (let row = range.row1; row <= lastRow; row += 1) {
113
+ const values = [];
114
+ for (let column = range.column1; column <= lastColumn; column += 1) {
115
+ const ref = formatCellRef(row, column);
116
+ values.push((results[ref]?.display ?? '').replace(/\t/g, ' ').replace(/\r?\n/g, ' '));
117
+ }
118
+ lines.push(values.join('\t'));
119
+ }
120
+ let text = lines.join('\n').trimEnd();
121
+ if (lastRow < range.row2 || lastColumn < range.column2)
122
+ text += '\n...';
123
+ if (text.length > SHEET_LIMITS.maxSelectionQuote)
124
+ text = text.slice(0, SHEET_LIMITS.maxSelectionQuote - 3).trimEnd() + '...';
125
+ return text;
126
+ }
127
+ /**
128
+ * Advisory warnings for a written workbook. A malformed formula is stored so
129
+ * the user can fix it, and the model is told what it produced.
130
+ * @param workbook the workbook.
131
+ * @returns one message per problem, each starting with its code.
132
+ */
133
+ export function checkSheetWorkbook(workbook) {
134
+ return evaluateWorkbook(workbook).warnings;
135
+ }
136
+ /**
137
+ * Evaluate a workbook once for an export or a print copy.
138
+ * @param workbook the workbook.
139
+ * @returns the evaluated cells.
140
+ */
141
+ export function evaluateForWrite(workbook) {
142
+ return evaluateWorkbook(workbook);
143
+ }
144
+ /**
145
+ * The result of one cell, for a panel that keeps its own evaluated map.
146
+ * @param evaluated the evaluated cells.
147
+ * @param sheetName the sheet.
148
+ * @param ref the cell.
149
+ * @returns the result, or undefined.
150
+ */
151
+ export function resultOf(evaluated, sheetName, ref) {
152
+ return evaluated.results[sheetName]?.[ref];
153
+ }
154
+ /**
155
+ * The display string a preview uses for one cell, without re-evaluating.
156
+ * @param cell the stored cell.
157
+ * @param result the evaluated result.
158
+ * @returns the display text.
159
+ */
160
+ export function displayOf(cell, result) {
161
+ if (result !== undefined)
162
+ return result.display;
163
+ if (cell === undefined)
164
+ return '';
165
+ if (cell.formula !== undefined)
166
+ return cell.formula;
167
+ if (cell.value === undefined)
168
+ return '';
169
+ return String(cell.value);
170
+ }
package/package.json CHANGED
@@ -1,8 +1,8 @@
1
1
  {
2
2
  "name": "dsh-artifact-studio",
3
- "version": "0.8.0",
3
+ "version": "0.9.0",
4
4
  "type": "module",
5
- "description": "Local-first artifacts for DeepSeek Harness: the agent creates pages and documents as versioned files, and you open, edit, restore, and export them beside the chat.",
5
+ "description": "Local-first artifacts for DeepSeek Harness: the agent creates pages, documents, spreadsheets, and mockups as versioned files, and you open, edit, restore, and export them beside the chat.",
6
6
  "keywords": [
7
7
  "dsh",
8
8
  "dsh-plugin",
@@ -10,6 +10,8 @@
10
10
  "deepseek",
11
11
  "artifacts",
12
12
  "documents",
13
+ "spreadsheets",
14
+ "sheets",
13
15
  "design-system",
14
16
  "local-first"
15
17
  ],
@@ -28,6 +30,7 @@
28
30
  "client.js",
29
31
  "client.doc.js",
30
32
  "client.canvas.js",
33
+ "client.sheet.js",
31
34
  "assets",
32
35
  "examples/page-design-system.html",
33
36
  "cordis.patch.yml",
@@ -66,6 +69,8 @@
66
69
  "@open-pencil/core": "0.16.0",
67
70
  "@open-pencil/scene-graph": "0.16.0",
68
71
  "docx": "9.15.3",
72
+ "fast-formula-parser": "^1.0.19",
73
+ "fflate": "^0.8.3",
69
74
  "marked": "^17.0.1"
70
75
  },
71
76
  "devDependencies": {