-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate.js
More file actions
61 lines (54 loc) · 1.9 KB
/
Copy pathupdate.js
File metadata and controls
61 lines (54 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
async function update_web_app(params, userSettings, authorizedResources) {
var previous =
(authorizedResources && authorizedResources.previousRunOutput) || '';
if (typeof previous !== 'string' || !previous.length) {
throw new Error(
'No previously rendered web app found to patch. Call `render_web_app` first to render the initial HTML, then use `update_web_app` for follow-up edits.',
);
}
var edits = params && params.edits;
if (!Array.isArray(edits) || edits.length === 0) {
throw new Error(
'`edits` must be a non-empty array of {oldText, newText} objects.',
);
}
var html = previous;
for (var i = 0; i < edits.length; i++) {
var edit = edits[i] || {};
var oldText = edit.oldText;
var newText = edit.newText;
if (typeof oldText !== 'string' || typeof newText !== 'string') {
throw new Error(
'Edit #' +
(i + 1) +
' must have string `oldText` and `newText` properties.',
);
}
if (!oldText.length) {
throw new Error(
'Edit #' +
(i + 1) +
' has an empty `oldText`. Provide a non-empty substring to match.',
);
}
var firstIndex = html.indexOf(oldText);
if (firstIndex === -1) {
throw new Error(
'Edit #' +
(i + 1) +
": `oldText` was not found in the current HTML. Make sure it is copied EXACTLY (including whitespace and casing) from the current HTML, or fall back to `render_web_app` with the full updated HTML.",
);
}
var lastIndex = html.lastIndexOf(oldText);
if (firstIndex !== lastIndex) {
throw new Error(
'Edit #' +
(i + 1) +
": `oldText` matched more than once in the current HTML. Include more surrounding context so it matches exactly once.",
);
}
html =
html.slice(0, firstIndex) + newText + html.slice(firstIndex + oldText.length);
}
return html;
}