onionskin-cli 0.0.0-stage → 0.0.1
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/CHANGELOG.md +136 -0
- package/INSTALL.md +17 -0
- package/LICENSE +21 -0
- package/NOTICE +46 -0
- package/README.md +88 -2
- package/docs/contracts/batch.schema.json +839 -0
- package/docs/contracts/history.schema.json +126 -0
- package/docs/contracts/ops.schema.json +541 -0
- package/package.json +54 -4
- package/scripts/checker/check.py +411 -0
- package/scripts/checker/requirements.txt +5 -0
- package/scripts/install-check.mjs +9 -0
- package/scripts/requirements.txt +5 -0
- package/scripts/rewriter/apple/main.swift +135 -0
- package/scripts/rewriter/apple/onionskin-fm +0 -0
- package/scripts/rewriter/apple/onionskin-fm.json +6 -0
- package/skills/onionskin/SKILL.md +117 -0
- package/skills/onionskin/reference/batch.md +94 -0
- package/skills/onionskin/reference/formats.md +15 -0
- package/skills/onionskin/reference/ops.md +253 -0
- package/skills/onionskin/reference/troubleshooting.md +73 -0
- package/skills/onionskin/reference/voice-guide.md +86 -0
- package/skills/onionskin/voice.example.md +51 -0
- package/src/agent-detect.js +149 -0
- package/src/agent-events.js +159 -0
- package/src/agent-prompt.js +27 -0
- package/src/agent-runner.js +593 -0
- package/src/analyze.js +17 -0
- package/src/anchors.js +190 -0
- package/src/apply.js +387 -0
- package/src/auth.js +130 -0
- package/src/batch.js +322 -0
- package/src/check-worker.js +8 -0
- package/src/checker.js +293 -0
- package/src/cli.js +516 -0
- package/src/constants.js +20 -0
- package/src/diff.js +267 -0
- package/src/doctor.js +247 -0
- package/src/docx.js +438 -0
- package/src/fmt.js +30 -0
- package/src/fsutil.js +348 -0
- package/src/history-worker.js +16 -0
- package/src/history.js +576 -0
- package/src/lease.js +79 -0
- package/src/local.js +242 -0
- package/src/merge-pptx.js +198 -0
- package/src/merge-xlsx.js +453 -0
- package/src/merge.js +410 -0
- package/src/offload.js +34 -0
- package/src/ops/common.js +48 -0
- package/src/ops/docx-table.js +323 -0
- package/src/ops/docx.js +503 -0
- package/src/ops/engine.js +41 -0
- package/src/ops/pptx.js +336 -0
- package/src/ops/run.js +115 -0
- package/src/ops/runs.js +127 -0
- package/src/ops/schema.js +109 -0
- package/src/ops/xlsx.js +277 -0
- package/src/package.js +189 -0
- package/src/paths.js +64 -0
- package/src/person.js +66 -0
- package/src/pptx.js +194 -0
- package/src/question.js +47 -0
- package/src/quick-plan.js +107 -0
- package/src/quick.js +164 -0
- package/src/redline-check.js +251 -0
- package/src/redline-worker.js +29 -0
- package/src/redline.js +438 -0
- package/src/reply.js +410 -0
- package/src/review.js +274 -0
- package/src/rewrite-check.js +1014 -0
- package/src/rewriter-cleanup.js +105 -0
- package/src/rewriter-setup.js +104 -0
- package/src/rewriter.js +396 -0
- package/src/sanitize.js +42 -0
- package/src/sentence-span.js +25 -0
- package/src/server-entry.js +13 -0
- package/src/server.js +576 -0
- package/src/session.js +1910 -0
- package/src/settings.js +161 -0
- package/src/setup.js +160 -0
- package/src/stamp-words.js +10 -0
- package/src/stamps.js +59 -0
- package/src/style.js +110 -0
- package/src/suggest-reply.js +84 -0
- package/src/suggest.js +261 -0
- package/src/text.js +31 -0
- package/src/tracked-words.js +43 -0
- package/src/voice-guide.js +182 -0
- package/src/voice-learn.js +314 -0
- package/src/voice-sample-worker.js +95 -0
- package/src/voice-samples.js +329 -0
- package/src/voice-store.js +305 -0
- package/src/voice-worker.js +28 -0
- package/src/voice.js +297 -0
- package/src/xlsx.js +454 -0
- package/src/xml-tree.js +118 -0
- package/src/xml.js +127 -0
- package/src/zip-write.js +74 -0
- package/src/zip.js +88 -0
- package/vendor/engine/DocViewer-BZLXUcxJ.js +15137 -0
- package/vendor/engine/THIRD_PARTY_LICENSES.txt +79 -0
- package/vendor/engine/collab-BVynXMqc.js +2591 -0
- package/vendor/engine/collab.js +2 -0
- package/vendor/engine/core.js +4 -0
- package/vendor/engine/default-worker-DogNT09A.js +35625 -0
- package/vendor/engine/embed.js +107 -0
- package/vendor/engine/index.js +5 -0
- package/vendor/engine/office-engine.embed.js +184 -0
- package/vendor/engine/office-engine.worker.js +36030 -0
- package/vendor/engine/xlsx-ops-SYAv6V0f.js +939 -0
- package/vendor/engine-node/LICENSE +21 -0
- package/vendor/engine-node/engine-node.mjs +46618 -0
- package/vendor/engine.lock.json +26 -0
- package/web/dist/assets/index-CFISATkn.js +197 -0
- package/web/dist/assets/index-spXE4oTz.css +1 -0
- package/web/dist/index.html +14 -0
package/src/review.js
ADDED
|
@@ -0,0 +1,274 @@
|
|
|
1
|
+
import crypto from "node:crypto";
|
|
2
|
+
import { readUnits } from "./anchors.js";
|
|
3
|
+
import { findAll } from "./ops/runs.js";
|
|
4
|
+
import { untrusted } from "./sanitize.js";
|
|
5
|
+
import { exactHash, normText } from "./text.js";
|
|
6
|
+
import { a1, a1Range } from "./xlsx.js";
|
|
7
|
+
|
|
8
|
+
// Idea 21: the agent reviews first. The person taps one of three fixed requests; the batch carries that fixed sentence in
|
|
9
|
+
// `instructions.review` and a read-only outline of the file (`untrusted_document_evidence.units[]`, built here by code). The agent answers
|
|
10
|
+
// with suggestions, which become cards: an edit card is one find-and-replace inside one DOCX paragraph or one PPTX shape, a note card is a
|
|
11
|
+
// concern with nothing to apply (spreadsheets get note cards only). Nothing the agent writes ever moves into a batch: a card's op is built
|
|
12
|
+
// here from the server's own record of the unit, and only the person's tap runs it, through the same apply path as an agent's ops.
|
|
13
|
+
// `Sessions.done()` is the trust boundary (cleanSuggestion + checkOnDisk); `planReplies` runs the same checks only to tell the agent what to fix.
|
|
14
|
+
|
|
15
|
+
export const REVIEW_KINDS = ["proofread", "skeptical_cfo", "check_numbers"];
|
|
16
|
+
/** The fixed sentence each button sends. Code constants: the only text a review puts in `instructions`. */
|
|
17
|
+
export const REVIEW_ASKS = {
|
|
18
|
+
proofread: "Proofread the document. Suggest fixes for spelling, grammar and punctuation only.",
|
|
19
|
+
skeptical_cfo: "Read the document as a skeptical finance chief would. Point out claims without support, numbers that do not add up, and risks left unsaid.",
|
|
20
|
+
check_numbers: "Check every number in the document: totals, percentages, dates and units. Suggest a fix only when a number is clearly wrong. Otherwise point out what to check.",
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
export const UNITS_MAX = 300; // units in one review batch; the rest is counted in `units_more` (a long file is cut off)
|
|
24
|
+
export const UNIT_TEXT_MAX = 1000;
|
|
25
|
+
export const UNIT_LABEL_MAX = 120;
|
|
26
|
+
// The complete outline file of a batch for an agent Onionskin started: at most this many units (one per line). 2,000 covers a document of
|
|
27
|
+
// a few hundred pages; a line stays under 1,900 characters, so the worst file is about 3.8 MB and a typical one (150-character paragraphs)
|
|
28
|
+
// about 0.6 MB. Beyond it, `outline_more` counts the units the agent cannot see.
|
|
29
|
+
export const OUTLINE_FILE_MAX = 2000;
|
|
30
|
+
export const OUTLINE_LINE_MAX = 1900;
|
|
31
|
+
export const SUGGESTIONS_MAX = 20;
|
|
32
|
+
export const FIND_MAX = 200;
|
|
33
|
+
export const REPLACE_MAX = 300;
|
|
34
|
+
export const WHY_MAX = 100;
|
|
35
|
+
export const NOTE_MAX = 200;
|
|
36
|
+
const EXCERPT_MAX = 160; // the card's "before" and "after": a code excerpt around the change
|
|
37
|
+
export const SUGGEST_FIX = `At most ${SUGGESTIONS_MAX} items, each {unit, find, replace, why} or {unit, note}: find at most ${FIND_MAX} characters and once in that unit, replace ${REPLACE_MAX}, why ${WHY_MAX}, note ${NOTE_MAX}, one line, no links. Spreadsheets take notes only.`;
|
|
38
|
+
|
|
39
|
+
// a link: a scheme, www., an email, or a bare domain (letters, a dot, two or more letters: "evil.example", not "a.m." or "3.5%"; the
|
|
40
|
+
// pattern src/rewrite-check.js uses). Stricter than question cards (src/question.js): an accepted `replace` goes into the file.
|
|
41
|
+
// Ceiling: it also refuses a file name ("deck.pptx") or a missing space after a period ("end.Next"); those go unsuggested.
|
|
42
|
+
const LINK = /:\/\/|www\.|@|(?:[\p{L}\p{N}-]+\.)+\p{L}{2,}/iu;
|
|
43
|
+
// what untrusted() keeps but a reader cannot see (the set src/voice.js refuses in examples): Accept would put it in the file unseen
|
|
44
|
+
const INVISIBLE = /[\p{Default_Ignorable_Code_Point}\p{Cf}\p{Co}\p{Cn}\p{Cs}]/u;
|
|
45
|
+
const len = (s) => [...s].length;
|
|
46
|
+
const isObj = (v) => v && typeof v === "object" && !Array.isArray(v);
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* The outline of a file for a review: DOCX paragraphs with text, PPTX text shapes, XLSX non-empty cells, in document order. Returns
|
|
50
|
+
* {units: [{id, label, text, protected}] (at most UNITS_MAX, for the batch), more, index: {id -> where the unit is and its text hash}} (the
|
|
51
|
+
* index stays on the server). A unit an edit cannot address (a DOCX paragraph with no body path, a PPTX shape whose id is shared on its
|
|
52
|
+
* slide) is marked protected: suggestions on it are refused.
|
|
53
|
+
* Ceiling: every paragraph, shape or cell is read and sorted on the server's main thread before the cut to UNITS_MAX, so a very large
|
|
54
|
+
* workbook pauses other requests while a review is built. Upgrade path: stop reading at UNITS_MAX + a count, or build in a worker.
|
|
55
|
+
*/
|
|
56
|
+
export function unitsOf(format, bytes) {
|
|
57
|
+
const raw = rawUnits(format, bytes);
|
|
58
|
+
const units = [];
|
|
59
|
+
const index = {};
|
|
60
|
+
raw.slice(0, UNITS_MAX).forEach((x, i) => {
|
|
61
|
+
const id = `u${i + 1}`;
|
|
62
|
+
const label = untrusted(x.label, UNIT_LABEL_MAX);
|
|
63
|
+
units.push({ id, label, text: untrusted(x.text, UNIT_TEXT_MAX), protected: x.protected });
|
|
64
|
+
index[id] = { label, protected: x.protected, ...x.at };
|
|
65
|
+
});
|
|
66
|
+
return { units, more: Math.max(0, raw.length - UNITS_MAX), index };
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
/** Every unit of the file in document order: {label, text (the full text, not cleaned), protected, at (where it is and its text hash)}. */
|
|
70
|
+
function rawUnits(format, bytes) {
|
|
71
|
+
const raw = [];
|
|
72
|
+
const u = readUnits(format, bytes);
|
|
73
|
+
if (format === "docx") {
|
|
74
|
+
for (const p of u.paragraphs) {
|
|
75
|
+
if (!normText(p.text)) continue;
|
|
76
|
+
const where = !p.path ? `paragraph ${p.ordinal + 1} (in a text box)` : p.path.length === 1 ? `paragraph ${p.path[0] + 1}` : `table ${p.tableNo ?? p.path[0] + 1}, row ${p.path[1] + 1}, column ${p.path[2] + 1}`;
|
|
77
|
+
raw.push({ label: where, text: p.text, protected: !!p.protected || !p.path, at: { ordinal: p.ordinal, para_id: p.paraId || null, body_path: p.path || null, text_hash: p.hash } });
|
|
78
|
+
}
|
|
79
|
+
} else if (format === "pptx") {
|
|
80
|
+
u.deck.ensure();
|
|
81
|
+
for (const s of u.deck.slides) {
|
|
82
|
+
const counts = new Map();
|
|
83
|
+
for (const sh of s.shapes) counts.set(sh.id, (counts.get(sh.id) || 0) + 1);
|
|
84
|
+
for (const sh of s.shapes) {
|
|
85
|
+
if (sh.kind !== "shape" || !normText(sh.text)) continue;
|
|
86
|
+
const addressable = /^\d{1,10}$/.test(String(s.id ?? "")) && Number.isInteger(sh.id) && sh.id >= 0 && counts.get(sh.id) === 1;
|
|
87
|
+
raw.push({ label: `slide ${s.index + 1}, ${sh.name ? `"${sh.name}"` : `shape ${sh.id}`}`, text: sh.text, protected: !addressable, at: { slide_id: String(s.id), slide_index: s.index, shape_id: sh.id, text_hash: sh.hash } });
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
} else if (format === "xlsx") {
|
|
91
|
+
for (const sheet of u.wb.sheets) {
|
|
92
|
+
const cells = [...u.wb.sheetCells(sheet).cells.values()].sort((x, y) => x.r - y.r || x.c - y.c);
|
|
93
|
+
const sd = u.wb.sheetCells(sheet);
|
|
94
|
+
for (const c of cells) {
|
|
95
|
+
const text = c.k === "formula" && c.cached ? `${c.text} (shows ${c.cached})` : c.text;
|
|
96
|
+
const m = sd.merges.find((q) => q.r0 === c.r && q.c0 === c.c);
|
|
97
|
+
raw.push({ label: `${sheet.name}!${a1(c.r, c.c)}`, text, protected: false, ...(m ? { merged: a1Range(m) } : {}), at: { sheet: sheet.name, range: a1(c.r, c.c), text_hash: exactHash(c.text) } });
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
} else throw new Error(`unknown format ${format}`);
|
|
101
|
+
return raw;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
/**
|
|
105
|
+
* The outline an agent Onionskin started gets with every edit batch (it cannot open the file): the units of `unitsOf`, each with the
|
|
106
|
+
* `target` and `expect` an op needs to address it, copied as they are. DOCX `{para_id?, body_path, text_hash}` (replace_text and the other
|
|
107
|
+
* paragraph ops), PPTX `{slide_id, shape_id}` (replace_text_in_shape, set_shape_text), XLSX `{sheet, range}` (set_cells); `expect` is
|
|
108
|
+
* `{text_hash}` of the whole unit. A protected unit has no target. `text_exact: false` marks a text that is not the unit's exact text (cut
|
|
109
|
+
* at UNIT_TEXT_MAX, or cleaned by untrusted(): whitespace runs, control characters): a `find` must use words seen in it, and a whole
|
|
110
|
+
* rewrite would lose what is not shown. Returns {outline, more} with at most `max` entries and `more` the units past them; the session
|
|
111
|
+
* cuts the inline part further to fit, and writes all of them to the outline file when they do not fit (see OUTLINE_FILE_MAX).
|
|
112
|
+
* Every entry, as JSON, stays under OUTLINE_LINE_MAX characters (its text is cut further when quotes or backslashes double in JSON), so
|
|
113
|
+
* the outline file, one entry per line, can be paged with Claude Code's Read tool (which cuts lines at 2,000 characters).
|
|
114
|
+
*/
|
|
115
|
+
export function outlineOf(format, bytes, max = UNITS_MAX) {
|
|
116
|
+
const raw = rawUnits(format, bytes);
|
|
117
|
+
const outline = raw.slice(0, max).map((x, i) => {
|
|
118
|
+
const target = x.protected ? null : targetOf(x.at, format);
|
|
119
|
+
const entry = (n) => {
|
|
120
|
+
const text = untrusted(x.text, n);
|
|
121
|
+
return {
|
|
122
|
+
id: `u${i + 1}`,
|
|
123
|
+
label: untrusted(x.label, UNIT_LABEL_MAX),
|
|
124
|
+
text,
|
|
125
|
+
...(text === x.text ? {} : { text_exact: false }),
|
|
126
|
+
protected: x.protected,
|
|
127
|
+
...(x.merged ? { merged: x.merged } : {}),
|
|
128
|
+
...(target ? { target, expect: { text_hash: x.at.text_hash } } : {}),
|
|
129
|
+
};
|
|
130
|
+
};
|
|
131
|
+
let e = entry(UNIT_TEXT_MAX);
|
|
132
|
+
for (let n = UNIT_TEXT_MAX - 100; JSON.stringify(e).length >= OUTLINE_LINE_MAX && n > 0; n -= 100) e = entry(n);
|
|
133
|
+
return e;
|
|
134
|
+
});
|
|
135
|
+
return { outline, more: Math.max(0, raw.length - max) };
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** One agent string: refused (never cut or cleaned into shape) unless it is already one clean line within `max`, and not a link. */
|
|
139
|
+
function strict(v, max, what, { empty = false } = {}) {
|
|
140
|
+
if (typeof v !== "string") return `${what} must be a string`;
|
|
141
|
+
if (!v && !empty) return `${what} is empty`;
|
|
142
|
+
if (len(v) > max) return `${what} is longer than ${max} characters`;
|
|
143
|
+
if (untrusted(v, Number.MAX_SAFE_INTEGER) !== v) return `${what} must be one line of plain text (no line breaks, control characters, or spaces at the ends)`;
|
|
144
|
+
if (INVISIBLE.test(v)) return `${what} holds a character that cannot be seen`;
|
|
145
|
+
if (LINK.test(v.normalize("NFKC"))) return `${what} contains a link`; // full-width letters count as their ASCII twins
|
|
146
|
+
return null;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* The shape and caps of one suggestion, against the units of the batch (`units`: [{id, protected}]). Pure. Returns
|
|
151
|
+
* {item: {unit, kind: "edit", find, replace, why} | {unit, kind: "note", note}} or {reason}.
|
|
152
|
+
*/
|
|
153
|
+
export function cleanSuggestion(raw, units, format) {
|
|
154
|
+
if (!isObj(raw)) return { reason: "a suggestion must be an object" };
|
|
155
|
+
const unit = typeof raw.unit === "string" ? raw.unit : "";
|
|
156
|
+
const u = units.find((x) => x.id === unit);
|
|
157
|
+
// reasons are code text only (they reach the page as flags): no string the agent wrote is echoed
|
|
158
|
+
if (!u) return { reason: "no such unit in this review" };
|
|
159
|
+
if (u.protected) return { reason: "this unit is protected: it cannot be changed or commented on here" };
|
|
160
|
+
const isNote = "note" in raw;
|
|
161
|
+
const allowed = isNote ? ["unit", "note"] : ["unit", "find", "replace", "why"];
|
|
162
|
+
const extra = Object.keys(raw).filter((k) => !allowed.includes(k));
|
|
163
|
+
if (extra.length) return { reason: "unexpected fields (give {unit, find, replace, why} or {unit, note})" };
|
|
164
|
+
if (isNote) {
|
|
165
|
+
const bad = strict(raw.note, NOTE_MAX, "note");
|
|
166
|
+
return bad ? { reason: bad } : { item: { unit, kind: "note", note: raw.note } };
|
|
167
|
+
}
|
|
168
|
+
if (format === "xlsx") return { reason: "a spreadsheet takes notes only: {unit, note}" };
|
|
169
|
+
const bad = strict(raw.find, FIND_MAX, "find") || strict(raw.replace, REPLACE_MAX, "replace") || (raw.why === undefined ? null : strict(raw.why, WHY_MAX, "why", { empty: true }));
|
|
170
|
+
if (bad) return { reason: bad };
|
|
171
|
+
if (raw.find === raw.replace) return { reason: "find and replace are the same" };
|
|
172
|
+
return { item: { unit, kind: "edit", find: raw.find, replace: raw.replace, why: raw.why ?? "" } };
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/** The unit's current full text, read by code from `units` (readUnits of the bytes on disk), or null when it is not where the index says. */
|
|
176
|
+
function textNow(units, at) {
|
|
177
|
+
if (units.format === "docx") {
|
|
178
|
+
const p = units.paragraphs[at.ordinal];
|
|
179
|
+
return p && p.hash === at.text_hash ? p.text : null;
|
|
180
|
+
}
|
|
181
|
+
if (units.format === "pptx") {
|
|
182
|
+
units.deck.ensure();
|
|
183
|
+
const sh = units.deck.slides.find((s) => String(s.id) === at.slide_id)?.shapes.filter((x) => x.id === at.shape_id);
|
|
184
|
+
return sh?.length === 1 && sh[0].hash === at.text_hash ? sh[0].text : null;
|
|
185
|
+
}
|
|
186
|
+
const sheet = units.wb.sheets.find((s) => s.name === at.sheet);
|
|
187
|
+
if (!sheet) return null;
|
|
188
|
+
const c = [...units.wb.sheetCells(sheet).cells.values()].find((x) => a1(x.r, x.c) === at.range);
|
|
189
|
+
return c && exactHash(c.text) === at.text_hash ? c.text : null;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// Never cut what Accept removes or writes: `find` (FIND_MAX) and `replace` (REPLACE_MAX) are shown whole; only the file's words around them are cut.
|
|
193
|
+
function excerpt(text, at, n, replace) {
|
|
194
|
+
if (n >= EXCERPT_MAX) return { before: untrusted(text.slice(at, at + n), Number.MAX_SAFE_INTEGER), after: untrusted(replace, Number.MAX_SAFE_INTEGER) };
|
|
195
|
+
const room = EXCERPT_MAX - n;
|
|
196
|
+
let s = Math.max(0, at - Math.floor(room / 2));
|
|
197
|
+
const e = Math.min(text.length, s + EXCERPT_MAX);
|
|
198
|
+
s = Math.max(0, e - EXCERPT_MAX);
|
|
199
|
+
const head = s > 0 ? "…" : "";
|
|
200
|
+
const tail = e < text.length ? "…" : "";
|
|
201
|
+
return {
|
|
202
|
+
before: head + untrusted(text.slice(s, e), Number.MAX_SAFE_INTEGER) + tail,
|
|
203
|
+
after: head + untrusted(text.slice(s, at) + replace + text.slice(at + n, e), Number.MAX_SAFE_INTEGER) + tail,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* A cleaned suggestion against the file as it is now (`units`: readUnits of the bytes on disk) and the server's index. Returns {card} or
|
|
209
|
+
* {reason}. The unit must still hold the text it had when the batch was built, and an edit's `find` must occur exactly once in its full
|
|
210
|
+
* text (case-sensitive, as apply matches). The card's op is built here, from the index; never from the agent.
|
|
211
|
+
*/
|
|
212
|
+
export function cardFor(item, units, index, { reviewId, format }) {
|
|
213
|
+
const at = index[item.unit];
|
|
214
|
+
if (!at || at.protected) return { reason: "unknown or protected unit" };
|
|
215
|
+
const text = textNow(units, at);
|
|
216
|
+
if (text == null) return { reason: "the unit changed since the review started" };
|
|
217
|
+
const id = `g_${crypto.randomBytes(6).toString("hex")}`;
|
|
218
|
+
const base = { id, reviewId, kind: item.kind, label: at.label, status: "open", at: placeOf(at, format) };
|
|
219
|
+
if (item.kind === "note") return { card: { ...base, before: untrusted(text, EXCERPT_MAX), note: item.note } };
|
|
220
|
+
const hits = findAll(text, item.find, true);
|
|
221
|
+
if (hits.length !== 1) return { reason: hits.length ? `the text to find occurs ${hits.length} times in the unit` : "the text to find does not occur in the unit" };
|
|
222
|
+
const ex = excerpt(text, hits[0], item.find.length, item.replace);
|
|
223
|
+
return { card: { ...base, before: ex.before, after: ex.after, why: item.why, op: opFor(at, format, id, item) } };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
/** Where the page shows a card ("Show me"): code-built, no document text. */
|
|
227
|
+
function placeOf(at, format) {
|
|
228
|
+
if (format === "docx") return { format, ordinal: at.ordinal, para_id: at.para_id, body_path: at.body_path };
|
|
229
|
+
if (format === "pptx") return { format, slide_id: at.slide_id, slide_index: at.slide_index, shape_id: at.shape_id };
|
|
230
|
+
return { format, sheet: at.sheet, range: at.range };
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
/** The op Accept runs: one find-and-replace in one paragraph or shape, with the unit's text hash as `expect`. */
|
|
234
|
+
export function opFor(at, format, id, { find, replace }) {
|
|
235
|
+
if (format === "docx") return { op: "replace_text", id, target: targetOf(at, format), expect: { text_hash: at.text_hash }, find, replace };
|
|
236
|
+
if (format === "pptx") return { op: "replace_text_in_shape", id, target: targetOf(at, format), expect: { text_hash: at.text_hash }, find, replace };
|
|
237
|
+
return null;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/** The explicit op target of a unit (ops.schema.json docx_target, pptx_target, xlsx_range_target). */
|
|
241
|
+
function targetOf(at, format) {
|
|
242
|
+
if (format === "docx") return at.para_id ? { para_id: at.para_id, body_path: at.body_path, text_hash: at.text_hash } : { body_path: at.body_path, text_hash: at.text_hash };
|
|
243
|
+
if (format === "pptx") return { slide_id: at.slide_id, shape_id: at.shape_id };
|
|
244
|
+
return { sheet: at.sheet, range: at.range };
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* The agent's suggestions -> {cards, dropped: [{index, reason}]}. More than SUGGESTIONS_MAX stores none (refused, never the first 20).
|
|
249
|
+
* `bytes` is the file now; `units` the batch's outline; `index` the server's record.
|
|
250
|
+
*/
|
|
251
|
+
export function cardsFrom(list, { bytes, format, units, index, reviewId }) {
|
|
252
|
+
if (list === undefined || list === null) return { cards: [], dropped: [] };
|
|
253
|
+
if (!Array.isArray(list)) return { cards: [], dropped: [{ index: null, reason: "suggestions must be a list" }] };
|
|
254
|
+
if (list.length > SUGGESTIONS_MAX) return { cards: [], dropped: list.map((_, i) => ({ index: i, reason: `more than ${SUGGESTIONS_MAX} suggestions` })) };
|
|
255
|
+
let now;
|
|
256
|
+
try {
|
|
257
|
+
now = readUnits(format, bytes);
|
|
258
|
+
} catch {
|
|
259
|
+
return { cards: [], dropped: list.map((_, i) => ({ index: i, reason: "the file could not be read" })) };
|
|
260
|
+
}
|
|
261
|
+
const cards = [];
|
|
262
|
+
const dropped = [];
|
|
263
|
+
list.forEach((raw, i) => {
|
|
264
|
+
const c = cleanSuggestion(raw, units, format);
|
|
265
|
+
const r = c.item ? cardFor(c.item, now, index, { reviewId, format }) : c;
|
|
266
|
+
if (r.card) cards.push(r.card);
|
|
267
|
+
else dropped.push({ index: i, reason: r.reason });
|
|
268
|
+
});
|
|
269
|
+
return { cards, dropped };
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** The activity summary of a review (code text, never the agent's). */
|
|
273
|
+
export const reviewSummary = (n) => (n ? `The agent made ${n} suggestion${n === 1 ? "" : "s"}.` : "The agent had no suggestions.");
|
|
274
|
+
|