@chosengeneration/light-code 0.59.6 → 0.59.8

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/dist/cli.js CHANGED
@@ -44989,6 +44989,8 @@ var ALWAYS_ASK_TOOLS = /* @__PURE__ */ new Set([
44989
44989
  * human sees the source once, and no category toggle can stand in for that.
44990
44990
  */
44991
44991
  "excel_write_macro",
44992
+ // Changes a workbook somebody has open and has not saved, with no undo this product owns.
44993
+ "excel_write_range",
44992
44994
  /*
44993
44995
  * Running a macro executes somebody else's VBA as the user: it can rewrite the workbook, write
44994
44996
  * files, or send mail. That is not something a category toggle should ever cover, and the
@@ -47697,6 +47699,98 @@ function createExcelReadMacroTool(options) {
47697
47699
  }
47698
47700
  };
47699
47701
  }
47702
+ var writeRangeSchema = external_exports.object({
47703
+ workbook: external_exports.string().optional().describe("Which open workbook. Defaults to the active one."),
47704
+ sheet: external_exports.string().optional().describe("Which sheet. Defaults to the active one."),
47705
+ cell: external_exports.string().describe('The top-left cell to start writing at, such as "A1". The block extends right and down from there.'),
47706
+ values: external_exports.array(external_exports.array(external_exports.union([external_exports.string(), external_exports.number(), external_exports.boolean(), external_exports.null()]))).min(1).describe('Rows of cell contents, every row the same width. A string starting with "=" is written as a formula and Excel calculates it; anything else is written as a plain value.')
47707
+ });
47708
+ function createExcelWriteRangeTool(options) {
47709
+ return {
47710
+ name: "excel_write_range",
47711
+ group: "edit",
47712
+ description: 'Write a block of values or formulas into an open workbook, starting at a cell. Rows of equal width; a string beginning with "=" becomes a formula Excel calculates. The workbook is left unsaved so the user can look at the result before keeping it.',
47713
+ parametersSchema: writeRangeSchema,
47714
+ async preview(params) {
47715
+ const width = params.values[0]?.length ?? 0;
47716
+ const target = `${params.values.length} row(s) x ${String(width)} column(s) from ${params.cell}`;
47717
+ const proposed = params.values.map((row) => row.map((cell) => cell === null ? "" : String(cell)).join(" | ")).join("\n");
47718
+ let existing = "Could not read the current contents of that range.";
47719
+ try {
47720
+ const current = await options.bridge.request({
47721
+ op: "excel.readRange",
47722
+ ...params.workbook !== void 0 ? { workbook: params.workbook } : {},
47723
+ ...params.sheet !== void 0 ? { sheet: params.sheet } : {},
47724
+ range: rangeFor(params.cell, params.values.length, width)
47725
+ });
47726
+ const filled = current.cells.filter((cell) => cell.value !== null && cell.value !== void 0 && String(cell.value) !== "");
47727
+ existing = filled.length === 0 ? "Those cells are all empty." : filled.map((cell) => `${cell.address}: ${String(cell.text ?? cell.value)}`).join("\n");
47728
+ } catch {
47729
+ }
47730
+ return {
47731
+ kind: "text",
47732
+ text: [
47733
+ `Write ${target} into ${params.sheet ?? "the active sheet"} of ${params.workbook ?? "the active workbook"}.`,
47734
+ "",
47735
+ "--- what is there now ---",
47736
+ existing,
47737
+ "",
47738
+ "--- what will replace it ---",
47739
+ proposed,
47740
+ "",
47741
+ "The workbook stays open and unsaved, so nothing reaches disk until the user saves it."
47742
+ ].join("\n")
47743
+ };
47744
+ },
47745
+ async execute(params) {
47746
+ try {
47747
+ const result = await options.bridge.request({ op: "excel.writeRange", ...params });
47748
+ const errors = result.cells.filter((cell) => String(cell.value).startsWith("#"));
47749
+ const sample = result.cells.slice(0, 40).map((cell) => `${cell.address}: ${cell.value === null ? "" : String(cell.value)}`).join("\n");
47750
+ return {
47751
+ content: [
47752
+ `Wrote ${String(result.written)} cell(s) to ${result.sheet}!${result.range} in ${result.workbook}.`,
47753
+ result.overwritten.length > 0 ? `Replaced ${String(result.overwritten.length)} cell(s) that had contents.` : "Every target cell was empty.",
47754
+ errors.length > 0 ? `${String(errors.length)} cell(s) evaluated to an error: ${errors.map((cell) => `${cell.address} ${String(cell.value)}`).join(", ")}` : "",
47755
+ "",
47756
+ sample,
47757
+ result.cells.length > 40 ? `...and ${String(result.cells.length - 40)} more` : "",
47758
+ "",
47759
+ "The workbook is NOT saved; tell the user to save it in Excel if they want to keep it."
47760
+ ].filter((line) => line !== "").join("\n"),
47761
+ isError: false
47762
+ };
47763
+ } catch (error51) {
47764
+ return { content: message(error51), isError: true };
47765
+ }
47766
+ }
47767
+ };
47768
+ }
47769
+ function rangeFor(cell, rows, columns) {
47770
+ const match = /^\$?([A-Za-z]+)\$?(\d+)$/.exec(cell.trim());
47771
+ if (match === null)
47772
+ return cell;
47773
+ const startColumn = columnNumber(match[1] ?? "A");
47774
+ const startRow = Number(match[2]);
47775
+ return `${match[1]?.toUpperCase() ?? "A"}${String(startRow)}:${columnLetters(startColumn + columns - 1)}${String(startRow + rows - 1)}`;
47776
+ }
47777
+ function columnNumber(letters) {
47778
+ let total = 0;
47779
+ for (const character of letters.toUpperCase()) {
47780
+ total = total * 26 + (character.charCodeAt(0) - 64);
47781
+ }
47782
+ return total;
47783
+ }
47784
+ function columnLetters(column) {
47785
+ let remaining = column;
47786
+ let letters = "";
47787
+ while (remaining > 0) {
47788
+ const remainder = (remaining - 1) % 26;
47789
+ letters = String.fromCharCode(65 + remainder) + letters;
47790
+ remaining = Math.floor((remaining - 1) / 26);
47791
+ }
47792
+ return letters;
47793
+ }
47700
47794
  var writeMacroSchema = macroSchema.extend({
47701
47795
  code: external_exports.string().describe("The complete new contents of the module. It replaces what is there.")
47702
47796
  });
@@ -49139,6 +49233,7 @@ var NEVER_AVAILABLE_TO_SCHEDULES = [
49139
49233
  // Same reasoning, and sharper unattended: installing a macro is authorising a capability, and
49140
49234
  // section 13 requires approval showing the source, which cannot happen with nobody there.
49141
49235
  "excel_write_macro",
49236
+ "excel_write_range",
49142
49237
  // And running one: unattended execution of arbitrary VBA, with nobody to read the source.
49143
49238
  "excel_run_macro",
49144
49239
  /*
@@ -58780,6 +58875,96 @@ function Invoke-ExcelSessions {\r
58780
58875
  happened, because a currency column silently losing its currency is the kind of difference\r
58781
58876
  someone should be told about rather than left to notice.\r
58782
58877
  #>\r
58878
+ # Writes a block of values or formulas into a live workbook.\r
58879
+ #\r
58880
+ # Bulk, not cell by cell. Reading 400 cells one at a time measured 315x slower than the array\r
58881
+ # property and ran past the timeout entirely; writing has exactly the same shape, so the whole\r
58882
+ # block goes in one assignment.\r
58883
+ #\r
58884
+ # \`.Formula\` rather than \`.Value2\`, so a string beginning with = becomes a real formula that\r
58885
+ # Excel calculates. A plain value assigned through Formula stays a plain value, so one property\r
58886
+ # covers both and the caller does not have to say which it meant.\r
58887
+ #\r
58888
+ # What was there is read *before* the write and returned, so the result tells the truth about what\r
58889
+ # was overwritten even if the sheet changed between the approval and the call.\r
58890
+ function Invoke-ExcelWriteRange {\r
58891
+ param($Request)\r
58892
+ \r
58893
+ $app = Get-OfficeApp -ProgId 'Excel.Application' -AttachOnly $true\r
58894
+ $wb = Get-Workbook -App $app -Name $Request.workbook\r
58895
+ $sheet = Get-Worksheet -Workbook $wb -Name $Request.sheet\r
58896
+ \r
58897
+ $rows = @($Request.values)\r
58898
+ $rowCount = $rows.Count\r
58899
+ if ($rowCount -eq 0) { throw 'No values were given to write.' }\r
58900
+ $columnCount = @($rows[0]).Count\r
58901
+ if ($columnCount -eq 0) { throw 'The first row is empty, so there is nothing to write.' }\r
58902
+ \r
58903
+ $limit = 5000\r
58904
+ if (($rowCount * $columnCount) -gt $limit) {\r
58905
+ throw "That is $($rowCount * $columnCount) cells; write at most $limit at a time."\r
58906
+ }\r
58907
+ \r
58908
+ # A ragged block would silently write nulls into the short rows, so it is refused instead.\r
58909
+ for ($r = 0; $r -lt $rowCount; $r++) {\r
58910
+ $width = @($rows[$r]).Count\r
58911
+ if ($width -ne $columnCount) {\r
58912
+ throw "Row $($r + 1) has $width value(s) but the first row has $columnCount; every row must be the same width."\r
58913
+ }\r
58914
+ }\r
58915
+ \r
58916
+ $target = $sheet.Range($Request.cell).Resize($rowCount, $columnCount)\r
58917
+ \r
58918
+ # What is being replaced, captured before anything is written.\r
58919
+ $existing = $target.Formula\r
58920
+ $before = @()\r
58921
+ for ($r = 1; $r -le $rowCount; $r++) {\r
58922
+ for ($c = 1; $c -le $columnCount; $c++) {\r
58923
+ $was = if ($rowCount -eq 1 -and $columnCount -eq 1) { $existing } else { $existing[$r, $c] }\r
58924
+ if ($null -ne $was -and "$was" -ne '') {\r
58925
+ $before += [ordered]@{\r
58926
+ address = (Get-CellAddress -Row ($target.Row + $r - 1) -Column ($target.Column + $c - 1))\r
58927
+ was = [string]$was\r
58928
+ }\r
58929
+ }\r
58930
+ }\r
58931
+ }\r
58932
+ \r
58933
+ $block = New-Object 'object[,]' $rowCount, $columnCount\r
58934
+ for ($r = 0; $r -lt $rowCount; $r++) {\r
58935
+ $row = @($rows[$r])\r
58936
+ for ($c = 0; $c -lt $columnCount; $c++) {\r
58937
+ $block[$r, $c] = $row[$c]\r
58938
+ }\r
58939
+ }\r
58940
+ \r
58941
+ $target.Formula = $block\r
58942
+ \r
58943
+ # Read back what Excel made of it. A formula that lands as #N/A or #REF! is the whole reason\r
58944
+ # this is reported rather than assumed - it would otherwise sit there unnoticed.\r
58945
+ $after = @()\r
58946
+ $computed = $target.Value2\r
58947
+ for ($r = 1; $r -le $rowCount; $r++) {\r
58948
+ for ($c = 1; $c -le $columnCount; $c++) {\r
58949
+ $value = if ($rowCount -eq 1 -and $columnCount -eq 1) { $computed } else { $computed[$r, $c] }\r
58950
+ $converted = Convert-ExcelValue -Value $value\r
58951
+ $after += [ordered]@{\r
58952
+ address = (Get-CellAddress -Row ($target.Row + $r - 1) -Column ($target.Column + $c - 1))\r
58953
+ value = $converted\r
58954
+ }\r
58955
+ }\r
58956
+ }\r
58957
+ \r
58958
+ return @{\r
58959
+ workbook = $wb.Name\r
58960
+ sheet = $sheet.Name\r
58961
+ range = $target.Address(0, 0)\r
58962
+ written = $rowCount * $columnCount\r
58963
+ overwritten = $before\r
58964
+ cells = $after\r
58965
+ }\r
58966
+ }\r
58967
+ \r
58783
58968
  function Invoke-ExcelReadRange {\r
58784
58969
  param($Request)\r
58785
58970
  \r
@@ -59899,6 +60084,7 @@ function Invoke-Request {\r
59899
60084
  'excel.sessions' { return Invoke-ExcelSessions }\r
59900
60085
  'excel.diagnose' { return Invoke-ExcelDiagnose }\r
59901
60086
  'excel.readRange' { return Invoke-ExcelReadRange -Request $Request }\r
60087
+ 'excel.writeRange' { return Invoke-ExcelWriteRange -Request $Request }\r
59902
60088
  'excel.trace' { return Invoke-ExcelTrace -Request $Request }\r
59903
60089
  'excel.listMacros' { return Invoke-ExcelListMacros -Request $Request }\r
59904
60090
  'excel.readMacro' { return Invoke-ExcelReadMacro -Request $Request }\r
@@ -62166,6 +62352,7 @@ ${preview.text}` } : {}
62166
62352
  combined.register(createExcelListMacrosTool(officeOptions));
62167
62353
  combined.register(createExcelReadMacroTool(officeOptions));
62168
62354
  combined.register(createExcelWriteMacroTool(officeOptions));
62355
+ combined.register(createExcelWriteRangeTool(officeOptions));
62169
62356
  combined.register(createExcelCheckMacroTool(officeOptions));
62170
62357
  combined.register(createExcelEvaluateTool(officeOptions));
62171
62358
  combined.register(createExcelRunMacroTool(officeOptions));
@@ -69894,6 +70081,18 @@ var CONTENT_TYPES = {
69894
70081
  // through <img> cannot run script, whatever it contains.
69895
70082
  ".svg": "image/svg+xml"
69896
70083
  };
70084
+ var STREAM_PADDING_BYTES = 8192;
70085
+ var STREAM_PADDING = `: ${(() => {
70086
+ let filler = "";
70087
+ let seed = 1;
70088
+ while (filler.length < STREAM_PADDING_BYTES) {
70089
+ seed = (seed * 1103515245 + 12345) % 2147483648;
70090
+ filler += (seed % 36).toString(36);
70091
+ }
70092
+ return filler;
70093
+ })()}
70094
+
70095
+ `;
69897
70096
  async function startServer(options) {
69898
70097
  const log = options.logSink ?? ((line) => process.stderr.write(`${line}
69899
70098
  `));
@@ -70134,6 +70333,7 @@ async function startServer(options) {
70134
70333
  "Cache-Control": "no-store, no-transform",
70135
70334
  "X-Accel-Buffering": "no"
70136
70335
  });
70336
+ response.write(STREAM_PADDING);
70137
70337
  response.write(": connected\n\n");
70138
70338
  const viaAdminUrl = url2.searchParams.get("view") === "admin";
70139
70339
  if (viaAdminUrl) adminConnections.add(principal.id);
@@ -70380,7 +70580,7 @@ function respondJson(response, status, body) {
70380
70580
  }
70381
70581
 
70382
70582
  // src/cli.ts
70383
- var VERSION = true ? "0.59.6" : "unknown (not a packaged build)";
70583
+ var VERSION = true ? "0.59.8" : "unknown (not a packaged build)";
70384
70584
  var compat = installNodeCompat();
70385
70585
  function lastMeaningfulLine(problem) {
70386
70586
  const lines = problem.split(String.fromCharCode(10)).map((line) => line.trim()).filter((line) => line.length > 0 && !line.endsWith(":"));