@chosengeneration/light-code 0.59.6 → 0.60.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/dist/cli.js +221 -1
- package/dist/client/client.js +5 -5
- package/dist/server.js +220 -0
- package/package.json +1 -1
package/dist/server.js
CHANGED
|
@@ -44890,6 +44890,8 @@ var ALWAYS_ASK_TOOLS = /* @__PURE__ */ new Set([
|
|
|
44890
44890
|
* human sees the source once, and no category toggle can stand in for that.
|
|
44891
44891
|
*/
|
|
44892
44892
|
"excel_write_macro",
|
|
44893
|
+
// Changes a workbook somebody has open and has not saved, with no undo this product owns.
|
|
44894
|
+
"excel_write_range",
|
|
44893
44895
|
/*
|
|
44894
44896
|
* Running a macro executes somebody else's VBA as the user: it can rewrite the workbook, write
|
|
44895
44897
|
* files, or send mail. That is not something a category toggle should ever cover, and the
|
|
@@ -47598,6 +47600,98 @@ function createExcelReadMacroTool(options) {
|
|
|
47598
47600
|
}
|
|
47599
47601
|
};
|
|
47600
47602
|
}
|
|
47603
|
+
var writeRangeSchema = external_exports.object({
|
|
47604
|
+
workbook: external_exports.string().optional().describe("Which open workbook. Defaults to the active one."),
|
|
47605
|
+
sheet: external_exports.string().optional().describe("Which sheet. Defaults to the active one."),
|
|
47606
|
+
cell: external_exports.string().describe('The top-left cell to start writing at, such as "A1". The block extends right and down from there.'),
|
|
47607
|
+
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.')
|
|
47608
|
+
});
|
|
47609
|
+
function createExcelWriteRangeTool(options) {
|
|
47610
|
+
return {
|
|
47611
|
+
name: "excel_write_range",
|
|
47612
|
+
group: "edit",
|
|
47613
|
+
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.',
|
|
47614
|
+
parametersSchema: writeRangeSchema,
|
|
47615
|
+
async preview(params) {
|
|
47616
|
+
const width = params.values[0]?.length ?? 0;
|
|
47617
|
+
const target = `${params.values.length} row(s) x ${String(width)} column(s) from ${params.cell}`;
|
|
47618
|
+
const proposed = params.values.map((row) => row.map((cell) => cell === null ? "" : String(cell)).join(" | ")).join("\n");
|
|
47619
|
+
let existing = "Could not read the current contents of that range.";
|
|
47620
|
+
try {
|
|
47621
|
+
const current = await options.bridge.request({
|
|
47622
|
+
op: "excel.readRange",
|
|
47623
|
+
...params.workbook !== void 0 ? { workbook: params.workbook } : {},
|
|
47624
|
+
...params.sheet !== void 0 ? { sheet: params.sheet } : {},
|
|
47625
|
+
range: rangeFor(params.cell, params.values.length, width)
|
|
47626
|
+
});
|
|
47627
|
+
const filled = current.cells.filter((cell) => cell.value !== null && cell.value !== void 0 && String(cell.value) !== "");
|
|
47628
|
+
existing = filled.length === 0 ? "Those cells are all empty." : filled.map((cell) => `${cell.address}: ${String(cell.text ?? cell.value)}`).join("\n");
|
|
47629
|
+
} catch {
|
|
47630
|
+
}
|
|
47631
|
+
return {
|
|
47632
|
+
kind: "text",
|
|
47633
|
+
text: [
|
|
47634
|
+
`Write ${target} into ${params.sheet ?? "the active sheet"} of ${params.workbook ?? "the active workbook"}.`,
|
|
47635
|
+
"",
|
|
47636
|
+
"--- what is there now ---",
|
|
47637
|
+
existing,
|
|
47638
|
+
"",
|
|
47639
|
+
"--- what will replace it ---",
|
|
47640
|
+
proposed,
|
|
47641
|
+
"",
|
|
47642
|
+
"The workbook stays open and unsaved, so nothing reaches disk until the user saves it."
|
|
47643
|
+
].join("\n")
|
|
47644
|
+
};
|
|
47645
|
+
},
|
|
47646
|
+
async execute(params) {
|
|
47647
|
+
try {
|
|
47648
|
+
const result = await options.bridge.request({ op: "excel.writeRange", ...params });
|
|
47649
|
+
const errors = result.cells.filter((cell) => String(cell.value).startsWith("#"));
|
|
47650
|
+
const sample = result.cells.slice(0, 40).map((cell) => `${cell.address}: ${cell.value === null ? "" : String(cell.value)}`).join("\n");
|
|
47651
|
+
return {
|
|
47652
|
+
content: [
|
|
47653
|
+
`Wrote ${String(result.written)} cell(s) to ${result.sheet}!${result.range} in ${result.workbook}.`,
|
|
47654
|
+
result.overwritten.length > 0 ? `Replaced ${String(result.overwritten.length)} cell(s) that had contents.` : "Every target cell was empty.",
|
|
47655
|
+
errors.length > 0 ? `${String(errors.length)} cell(s) evaluated to an error: ${errors.map((cell) => `${cell.address} ${String(cell.value)}`).join(", ")}` : "",
|
|
47656
|
+
"",
|
|
47657
|
+
sample,
|
|
47658
|
+
result.cells.length > 40 ? `...and ${String(result.cells.length - 40)} more` : "",
|
|
47659
|
+
"",
|
|
47660
|
+
"The workbook is NOT saved; tell the user to save it in Excel if they want to keep it."
|
|
47661
|
+
].filter((line) => line !== "").join("\n"),
|
|
47662
|
+
isError: false
|
|
47663
|
+
};
|
|
47664
|
+
} catch (error51) {
|
|
47665
|
+
return { content: message(error51), isError: true };
|
|
47666
|
+
}
|
|
47667
|
+
}
|
|
47668
|
+
};
|
|
47669
|
+
}
|
|
47670
|
+
function rangeFor(cell, rows, columns) {
|
|
47671
|
+
const match = /^\$?([A-Za-z]+)\$?(\d+)$/.exec(cell.trim());
|
|
47672
|
+
if (match === null)
|
|
47673
|
+
return cell;
|
|
47674
|
+
const startColumn = columnNumber(match[1] ?? "A");
|
|
47675
|
+
const startRow = Number(match[2]);
|
|
47676
|
+
return `${match[1]?.toUpperCase() ?? "A"}${String(startRow)}:${columnLetters(startColumn + columns - 1)}${String(startRow + rows - 1)}`;
|
|
47677
|
+
}
|
|
47678
|
+
function columnNumber(letters) {
|
|
47679
|
+
let total = 0;
|
|
47680
|
+
for (const character of letters.toUpperCase()) {
|
|
47681
|
+
total = total * 26 + (character.charCodeAt(0) - 64);
|
|
47682
|
+
}
|
|
47683
|
+
return total;
|
|
47684
|
+
}
|
|
47685
|
+
function columnLetters(column) {
|
|
47686
|
+
let remaining = column;
|
|
47687
|
+
let letters = "";
|
|
47688
|
+
while (remaining > 0) {
|
|
47689
|
+
const remainder = (remaining - 1) % 26;
|
|
47690
|
+
letters = String.fromCharCode(65 + remainder) + letters;
|
|
47691
|
+
remaining = Math.floor((remaining - 1) / 26);
|
|
47692
|
+
}
|
|
47693
|
+
return letters;
|
|
47694
|
+
}
|
|
47601
47695
|
var writeMacroSchema = macroSchema.extend({
|
|
47602
47696
|
code: external_exports.string().describe("The complete new contents of the module. It replaces what is there.")
|
|
47603
47697
|
});
|
|
@@ -49040,6 +49134,7 @@ var NEVER_AVAILABLE_TO_SCHEDULES = [
|
|
|
49040
49134
|
// Same reasoning, and sharper unattended: installing a macro is authorising a capability, and
|
|
49041
49135
|
// section 13 requires approval showing the source, which cannot happen with nobody there.
|
|
49042
49136
|
"excel_write_macro",
|
|
49137
|
+
"excel_write_range",
|
|
49043
49138
|
// And running one: unattended execution of arbitrary VBA, with nobody to read the source.
|
|
49044
49139
|
"excel_run_macro",
|
|
49045
49140
|
/*
|
|
@@ -58681,6 +58776,96 @@ function Invoke-ExcelSessions {\r
|
|
|
58681
58776
|
happened, because a currency column silently losing its currency is the kind of difference\r
|
|
58682
58777
|
someone should be told about rather than left to notice.\r
|
|
58683
58778
|
#>\r
|
|
58779
|
+
# Writes a block of values or formulas into a live workbook.\r
|
|
58780
|
+
#\r
|
|
58781
|
+
# Bulk, not cell by cell. Reading 400 cells one at a time measured 315x slower than the array\r
|
|
58782
|
+
# property and ran past the timeout entirely; writing has exactly the same shape, so the whole\r
|
|
58783
|
+
# block goes in one assignment.\r
|
|
58784
|
+
#\r
|
|
58785
|
+
# \`.Formula\` rather than \`.Value2\`, so a string beginning with = becomes a real formula that\r
|
|
58786
|
+
# Excel calculates. A plain value assigned through Formula stays a plain value, so one property\r
|
|
58787
|
+
# covers both and the caller does not have to say which it meant.\r
|
|
58788
|
+
#\r
|
|
58789
|
+
# What was there is read *before* the write and returned, so the result tells the truth about what\r
|
|
58790
|
+
# was overwritten even if the sheet changed between the approval and the call.\r
|
|
58791
|
+
function Invoke-ExcelWriteRange {\r
|
|
58792
|
+
param($Request)\r
|
|
58793
|
+
\r
|
|
58794
|
+
$app = Get-OfficeApp -ProgId 'Excel.Application' -AttachOnly $true\r
|
|
58795
|
+
$wb = Get-Workbook -App $app -Name $Request.workbook\r
|
|
58796
|
+
$sheet = Get-Worksheet -Workbook $wb -Name $Request.sheet\r
|
|
58797
|
+
\r
|
|
58798
|
+
$rows = @($Request.values)\r
|
|
58799
|
+
$rowCount = $rows.Count\r
|
|
58800
|
+
if ($rowCount -eq 0) { throw 'No values were given to write.' }\r
|
|
58801
|
+
$columnCount = @($rows[0]).Count\r
|
|
58802
|
+
if ($columnCount -eq 0) { throw 'The first row is empty, so there is nothing to write.' }\r
|
|
58803
|
+
\r
|
|
58804
|
+
$limit = 5000\r
|
|
58805
|
+
if (($rowCount * $columnCount) -gt $limit) {\r
|
|
58806
|
+
throw "That is $($rowCount * $columnCount) cells; write at most $limit at a time."\r
|
|
58807
|
+
}\r
|
|
58808
|
+
\r
|
|
58809
|
+
# A ragged block would silently write nulls into the short rows, so it is refused instead.\r
|
|
58810
|
+
for ($r = 0; $r -lt $rowCount; $r++) {\r
|
|
58811
|
+
$width = @($rows[$r]).Count\r
|
|
58812
|
+
if ($width -ne $columnCount) {\r
|
|
58813
|
+
throw "Row $($r + 1) has $width value(s) but the first row has $columnCount; every row must be the same width."\r
|
|
58814
|
+
}\r
|
|
58815
|
+
}\r
|
|
58816
|
+
\r
|
|
58817
|
+
$target = $sheet.Range($Request.cell).Resize($rowCount, $columnCount)\r
|
|
58818
|
+
\r
|
|
58819
|
+
# What is being replaced, captured before anything is written.\r
|
|
58820
|
+
$existing = $target.Formula\r
|
|
58821
|
+
$before = @()\r
|
|
58822
|
+
for ($r = 1; $r -le $rowCount; $r++) {\r
|
|
58823
|
+
for ($c = 1; $c -le $columnCount; $c++) {\r
|
|
58824
|
+
$was = if ($rowCount -eq 1 -and $columnCount -eq 1) { $existing } else { $existing[$r, $c] }\r
|
|
58825
|
+
if ($null -ne $was -and "$was" -ne '') {\r
|
|
58826
|
+
$before += [ordered]@{\r
|
|
58827
|
+
address = (Get-CellAddress -Row ($target.Row + $r - 1) -Column ($target.Column + $c - 1))\r
|
|
58828
|
+
was = [string]$was\r
|
|
58829
|
+
}\r
|
|
58830
|
+
}\r
|
|
58831
|
+
}\r
|
|
58832
|
+
}\r
|
|
58833
|
+
\r
|
|
58834
|
+
$block = New-Object 'object[,]' $rowCount, $columnCount\r
|
|
58835
|
+
for ($r = 0; $r -lt $rowCount; $r++) {\r
|
|
58836
|
+
$row = @($rows[$r])\r
|
|
58837
|
+
for ($c = 0; $c -lt $columnCount; $c++) {\r
|
|
58838
|
+
$block[$r, $c] = $row[$c]\r
|
|
58839
|
+
}\r
|
|
58840
|
+
}\r
|
|
58841
|
+
\r
|
|
58842
|
+
$target.Formula = $block\r
|
|
58843
|
+
\r
|
|
58844
|
+
# Read back what Excel made of it. A formula that lands as #N/A or #REF! is the whole reason\r
|
|
58845
|
+
# this is reported rather than assumed - it would otherwise sit there unnoticed.\r
|
|
58846
|
+
$after = @()\r
|
|
58847
|
+
$computed = $target.Value2\r
|
|
58848
|
+
for ($r = 1; $r -le $rowCount; $r++) {\r
|
|
58849
|
+
for ($c = 1; $c -le $columnCount; $c++) {\r
|
|
58850
|
+
$value = if ($rowCount -eq 1 -and $columnCount -eq 1) { $computed } else { $computed[$r, $c] }\r
|
|
58851
|
+
$converted = Convert-ExcelValue -Value $value\r
|
|
58852
|
+
$after += [ordered]@{\r
|
|
58853
|
+
address = (Get-CellAddress -Row ($target.Row + $r - 1) -Column ($target.Column + $c - 1))\r
|
|
58854
|
+
value = $converted\r
|
|
58855
|
+
}\r
|
|
58856
|
+
}\r
|
|
58857
|
+
}\r
|
|
58858
|
+
\r
|
|
58859
|
+
return @{\r
|
|
58860
|
+
workbook = $wb.Name\r
|
|
58861
|
+
sheet = $sheet.Name\r
|
|
58862
|
+
range = $target.Address(0, 0)\r
|
|
58863
|
+
written = $rowCount * $columnCount\r
|
|
58864
|
+
overwritten = $before\r
|
|
58865
|
+
cells = $after\r
|
|
58866
|
+
}\r
|
|
58867
|
+
}\r
|
|
58868
|
+
\r
|
|
58684
58869
|
function Invoke-ExcelReadRange {\r
|
|
58685
58870
|
param($Request)\r
|
|
58686
58871
|
\r
|
|
@@ -59800,6 +59985,7 @@ function Invoke-Request {\r
|
|
|
59800
59985
|
'excel.sessions' { return Invoke-ExcelSessions }\r
|
|
59801
59986
|
'excel.diagnose' { return Invoke-ExcelDiagnose }\r
|
|
59802
59987
|
'excel.readRange' { return Invoke-ExcelReadRange -Request $Request }\r
|
|
59988
|
+
'excel.writeRange' { return Invoke-ExcelWriteRange -Request $Request }\r
|
|
59803
59989
|
'excel.trace' { return Invoke-ExcelTrace -Request $Request }\r
|
|
59804
59990
|
'excel.listMacros' { return Invoke-ExcelListMacros -Request $Request }\r
|
|
59805
59991
|
'excel.readMacro' { return Invoke-ExcelReadMacro -Request $Request }\r
|
|
@@ -62067,6 +62253,7 @@ ${preview.text}` } : {}
|
|
|
62067
62253
|
combined.register(createExcelListMacrosTool(officeOptions));
|
|
62068
62254
|
combined.register(createExcelReadMacroTool(officeOptions));
|
|
62069
62255
|
combined.register(createExcelWriteMacroTool(officeOptions));
|
|
62256
|
+
combined.register(createExcelWriteRangeTool(officeOptions));
|
|
62070
62257
|
combined.register(createExcelCheckMacroTool(officeOptions));
|
|
62071
62258
|
combined.register(createExcelEvaluateTool(officeOptions));
|
|
62072
62259
|
combined.register(createExcelRunMacroTool(officeOptions));
|
|
@@ -69377,6 +69564,18 @@ var CONTENT_TYPES = {
|
|
|
69377
69564
|
// through <img> cannot run script, whatever it contains.
|
|
69378
69565
|
".svg": "image/svg+xml"
|
|
69379
69566
|
};
|
|
69567
|
+
var STREAM_PADDING_BYTES = 8192;
|
|
69568
|
+
var STREAM_PADDING = `: ${(() => {
|
|
69569
|
+
let filler = "";
|
|
69570
|
+
let seed = 1;
|
|
69571
|
+
while (filler.length < STREAM_PADDING_BYTES) {
|
|
69572
|
+
seed = (seed * 1103515245 + 12345) % 2147483648;
|
|
69573
|
+
filler += (seed % 36).toString(36);
|
|
69574
|
+
}
|
|
69575
|
+
return filler;
|
|
69576
|
+
})()}
|
|
69577
|
+
|
|
69578
|
+
`;
|
|
69380
69579
|
async function startServer(options) {
|
|
69381
69580
|
const log = options.logSink ?? ((line) => process.stderr.write(`${line}
|
|
69382
69581
|
`));
|
|
@@ -69451,6 +69650,17 @@ async function startServer(options) {
|
|
|
69451
69650
|
detach: (response) => {
|
|
69452
69651
|
if (sink === response) sink = void 0;
|
|
69453
69652
|
},
|
|
69653
|
+
drain: () => {
|
|
69654
|
+
return missed.splice(0, missed.length).flatMap((frame) => {
|
|
69655
|
+
const body = frame.startsWith("data: ") ? frame.slice(6).trimEnd() : "";
|
|
69656
|
+
if (body.length === 0) return [];
|
|
69657
|
+
try {
|
|
69658
|
+
return [JSON.parse(body)];
|
|
69659
|
+
} catch {
|
|
69660
|
+
return [];
|
|
69661
|
+
}
|
|
69662
|
+
});
|
|
69663
|
+
},
|
|
69454
69664
|
dispose: () => {
|
|
69455
69665
|
const open = sink;
|
|
69456
69666
|
sink = void 0;
|
|
@@ -69617,6 +69827,7 @@ async function startServer(options) {
|
|
|
69617
69827
|
"Cache-Control": "no-store, no-transform",
|
|
69618
69828
|
"X-Accel-Buffering": "no"
|
|
69619
69829
|
});
|
|
69830
|
+
response.write(STREAM_PADDING);
|
|
69620
69831
|
response.write(": connected\n\n");
|
|
69621
69832
|
const viaAdminUrl = url2.searchParams.get("view") === "admin";
|
|
69622
69833
|
if (viaAdminUrl) adminConnections.add(principal.id);
|
|
@@ -69644,6 +69855,15 @@ async function startServer(options) {
|
|
|
69644
69855
|
request.on("close", cleanup);
|
|
69645
69856
|
return;
|
|
69646
69857
|
}
|
|
69858
|
+
if (url2.pathname === "/api/poll" && request.method === "GET") {
|
|
69859
|
+
let connection = connections.get(principal.id);
|
|
69860
|
+
if (connection === void 0) {
|
|
69861
|
+
connection = await openConnection(principal);
|
|
69862
|
+
connections.set(principal.id, connection);
|
|
69863
|
+
}
|
|
69864
|
+
respondJson(response, 200, { messages: connection.drain() });
|
|
69865
|
+
return;
|
|
69866
|
+
}
|
|
69647
69867
|
if (url2.pathname === "/api/message" && request.method === "POST") {
|
|
69648
69868
|
const connection = connections.get(principal.id);
|
|
69649
69869
|
if (connection === void 0) {
|