omp-superagent 0.1.6 → 0.1.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/DESIGN.md +36 -20
- package/README.ko.md +140 -33
- package/README.md +165 -41
- package/THIRD_PARTY_NOTICES.md +9 -7
- package/config.example.json +3 -1
- package/docs/OPERATIONS.md +395 -157
- package/docs/design/execution.md +156 -22
- package/docs/design/messaging.md +258 -52
- package/docs/discord-voice.md +8 -4
- package/docs/releases/0.1.7.md +25 -0
- package/docs/releases/0.1.8.md +21 -0
- package/gateway/bin/omp-gateway-darwin-arm64 +0 -0
- package/gateway/bin/omp-gateway-darwin-x64 +0 -0
- package/gateway/bin/omp-gateway-linux-arm64 +0 -0
- package/gateway/bin/omp-gateway-linux-x64 +0 -0
- package/host-owner/bin/omp-host-owner-darwin-arm64 +0 -0
- package/host-owner/bin/omp-host-owner-darwin-x64 +0 -0
- package/host-owner/bin/omp-host-owner-linux-arm64 +0 -0
- package/host-owner/bin/omp-host-owner-linux-x64 +0 -0
- package/package.json +9 -9
- package/scripts/ci.ts +4 -4
- package/scripts/test-omp.sh +2 -1
- package/skills/head/SKILL.md +26 -5
- package/skills/head/references/routing.md +68 -33
- package/skills/head/references/supervision.md +20 -4
- package/skills/lane/references/workflow.md +16 -7
- package/src/board.ts +5 -3
- package/src/cli.ts +161 -19
- package/src/config.ts +21 -1
- package/src/contracts.ts +63 -8
- package/src/cron-jobs.ts +443 -19
- package/src/cron-pr-intake.ts +257 -0
- package/src/discord-markdown.ts +350 -123
- package/src/extension.ts +78 -13
- package/src/gateway-client.ts +8 -2
- package/src/head-quota.ts +80 -0
- package/src/head-recovery.ts +40 -1
- package/src/host-extension.ts +122 -4
- package/src/host-inventory-manifest.ts +2 -2
- package/src/host-protocol.ts +11 -0
- package/src/host-sdk-helper.ts +121 -12
- package/src/host.ts +175 -57
- package/src/installed-omp.ts +116 -4
- package/src/lane-rows.ts +44 -4
- package/src/lane-watch.ts +339 -0
- package/src/managed-memory-helper.ts +13 -10
- package/src/managed-memory.ts +62 -27
- package/src/managed-ulw-probe.ts +75 -20
- package/src/managed-ulw.ts +174 -21
- package/src/messenger-command-catalog.json +2 -2
- package/src/native-launch-profile.ts +4 -2
- package/src/native-presets.ts +32 -5
- package/src/native-sessions.ts +3 -1
- package/src/native-settings.ts +47 -9
- package/src/observation-request.ts +23 -0
- package/src/omp-usage.ts +254 -36
- package/src/pr-github-schema.ts +5 -1
- package/src/pr-github.ts +68 -19
- package/src/pr-intake-automation.ts +316 -0
- package/src/pr-intake-comment.ts +226 -0
- package/src/pr-intake-fix.ts +233 -0
- package/src/pr-intake-github.ts +480 -0
- package/src/pr-intake-material.ts +230 -0
- package/src/pr-intake-mode.ts +59 -0
- package/src/pr-intake-review.ts +397 -0
- package/src/pr-intake-tree.ts +69 -0
- package/src/pr-intake-work.ts +29 -0
- package/src/pr-watch.ts +91 -32
- package/src/runtime.ts +2702 -391
- package/src/setup-judge.ts +1 -1
- package/src/setup.ts +12 -16
- package/src/store.ts +429 -86
- package/src/topic-history.ts +243 -47
- package/src/worker-checkout.ts +106 -0
- package/src/worker-destination.ts +10 -0
- package/src/worker-doctor.ts +66 -0
- package/src/worker-enrollment.ts +165 -0
- package/src/worker-host.ts +180 -0
- package/src/worker-ledger.ts +188 -0
- package/src/worker-placement.ts +78 -0
- package/src/worker-profile.ts +274 -0
- package/src/worker-registration.ts +288 -0
- package/src/worker-repository.ts +233 -0
- package/src/worker-request.ts +74 -0
- package/src/worker-service.ts +230 -0
- package/src/worker-stdio.ts +61 -0
- package/src/worker-trust.ts +36 -0
- package/src/worker-wire.ts +204 -0
- package/src/workflow.ts +81 -32
- package/src/setup-memory.ts +0 -21
package/src/discord-markdown.ts
CHANGED
|
@@ -2,171 +2,398 @@
|
|
|
2
2
|
* Markdown table rendering for the Discord transport.
|
|
3
3
|
*
|
|
4
4
|
* Discord has no pipe-table support: a table sent verbatim arrives as literal
|
|
5
|
-
* `| a | b |` rows.
|
|
6
|
-
*
|
|
7
|
-
*
|
|
8
|
-
* Column padding uses display width, not string length, so CJK and other wide
|
|
9
|
-
* characters line up. Bun.stringWidth supplies the display width directly;
|
|
10
|
-
* no OMP source checkout or private module is needed.
|
|
5
|
+
* `| a | b |` rows. Labeled lists preserve inline Markdown and wrap on
|
|
6
|
+
* narrow screens without requiring horizontal scrolling.
|
|
11
7
|
*/
|
|
12
8
|
|
|
13
|
-
type Align = "left" | "center" | "right";
|
|
14
|
-
|
|
15
9
|
const FENCE_OPEN = /^ {0,3}(`{3,}|~{3,})/;
|
|
16
10
|
const FENCE_CLOSE = /^ {0,3}(`{3,}|~{3,})[ \t\r]*$/;
|
|
17
11
|
const BLOCK_START = /^ {0,3}(?:>|#{1,6}(?:[ \t]|$)|(?:[-+*]|\d{1,9}[.)])(?:[ \t]|$))/;
|
|
12
|
+
const INDENTED_CODE = /^(?: {4}| {0,3}\t)/;
|
|
18
13
|
const DELIMITER_CELL = /^:?-+:?$/;
|
|
14
|
+
const LIST_MARKER = /^ {0,3}(?:[-+*]|\d{1,9}[.)])(?=[ \t]|$)/;
|
|
15
|
+
const THEMATIC_BREAK = /^ {0,3}(?:(?:-[ \t]*){3,}|(?:\*[ \t]*){3,})\r?$/;
|
|
16
|
+
|
|
17
|
+
function opensFence(line: string): string | undefined {
|
|
18
|
+
const match = FENCE_OPEN.exec(line);
|
|
19
|
+
const marker = match?.[1];
|
|
20
|
+
// An info string containing backticks makes a backtick opener inline code.
|
|
21
|
+
if (match && marker?.startsWith("`") && line.slice(match[0].length).includes("`")) return undefined;
|
|
22
|
+
return marker;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function relativeLine(line: string | undefined, margin: number): string | undefined {
|
|
26
|
+
const indent = line?.match(/^ */)?.[0].length ?? 0;
|
|
27
|
+
return indent >= margin ? line?.slice(margin) : undefined;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
function listMargin(line: string): number | undefined {
|
|
31
|
+
const marker = LIST_MARKER.exec(line)?.[0];
|
|
32
|
+
if (!marker || THEMATIC_BREAK.test(line)) return undefined;
|
|
33
|
+
const spaces = line.slice(marker.length).match(/^ */)?.[0].length ?? 0;
|
|
34
|
+
return marker.length + (spaces >= 1 && spaces <= 4 ? spaces : 1);
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function indentColumns(line: string): number {
|
|
38
|
+
let columns = 0;
|
|
39
|
+
for (const char of line) {
|
|
40
|
+
if (char === " ") columns++;
|
|
41
|
+
else if (char === "\t") columns += 4 - columns % 4;
|
|
42
|
+
else break;
|
|
43
|
+
}
|
|
44
|
+
return columns;
|
|
45
|
+
}
|
|
19
46
|
|
|
20
47
|
/**
|
|
21
|
-
* Split
|
|
22
|
-
*
|
|
23
|
-
* table body. `\|` and pipes inside backtick spans never split a cell, matching
|
|
24
|
-
* how GFM reads a row.
|
|
48
|
+
* Split on structural pipes only. Inside inline code a table's pipe escape
|
|
49
|
+
* becomes a literal pipe; outside code Discord still interprets the escape.
|
|
25
50
|
*/
|
|
26
|
-
function splitRow(line: string): string[] | null {
|
|
51
|
+
function splitRow(line: string | undefined): string[] | null {
|
|
52
|
+
if (line === undefined || !line.includes("|") || INDENTED_CODE.test(line)) return null;
|
|
53
|
+
// Unmatched backtick runs are literal prose and must not hide separators.
|
|
54
|
+
const lastTicks = new Map<number, number>();
|
|
55
|
+
for (const match of line.matchAll(/`+/g)) lastTicks.set(match[0].length, match.index);
|
|
27
56
|
const cells: string[] = [];
|
|
28
|
-
let
|
|
57
|
+
let start = 0;
|
|
29
58
|
let pipe = false;
|
|
30
|
-
let
|
|
31
|
-
let
|
|
59
|
+
let decoded = "";
|
|
60
|
+
let ticks = 0;
|
|
32
61
|
for (let index = 0; index < line.length; index++) {
|
|
33
|
-
const char = line[index]
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
// it stays the literal `|` the author asked to display.
|
|
37
|
-
cell += "|";
|
|
62
|
+
const char = line[index];
|
|
63
|
+
const following = line[index + 1];
|
|
64
|
+
if (ticks === 0 && char === "\\" && (following === "|" || following === "`" || following === "\\")) {
|
|
38
65
|
index++;
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
66
|
+
} else if (ticks !== 0 && char === "\\" && following === "|") {
|
|
67
|
+
decoded += line.slice(start, index) + "|";
|
|
68
|
+
index++;
|
|
69
|
+
start = index + 1;
|
|
70
|
+
} else if (char === "`") {
|
|
71
|
+
const first = index;
|
|
72
|
+
while (line[index + 1] === "`") index++;
|
|
73
|
+
const run = index - first + 1;
|
|
74
|
+
if (ticks === run) ticks = 0;
|
|
75
|
+
else if (ticks === 0) {
|
|
76
|
+
if ((lastTicks.get(run) ?? first) > first) ticks = run;
|
|
77
|
+
else {
|
|
78
|
+
decoded += line.slice(start, first) + "\\`".repeat(run);
|
|
79
|
+
start = index + 1;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
} else if (char === "|" && ticks === 0) {
|
|
83
|
+
const cell = (decoded + line.slice(start, index)).trim();
|
|
84
|
+
if (pipe || cell !== "") cells.push(cell);
|
|
51
85
|
pipe = true;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
continue;
|
|
86
|
+
decoded = "";
|
|
87
|
+
start = index + 1;
|
|
55
88
|
}
|
|
56
|
-
cell += char;
|
|
57
89
|
}
|
|
58
90
|
if (!pipe) return null;
|
|
59
|
-
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
return cells.length > 0 ? cells.map(value => value.trim()) : null;
|
|
91
|
+
const tail = (decoded + line.slice(start)).trim();
|
|
92
|
+
if (tail !== "") cells.push(tail);
|
|
93
|
+
return cells.length > 0 ? cells : null;
|
|
63
94
|
}
|
|
64
95
|
|
|
65
|
-
/**
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
96
|
+
/**
|
|
97
|
+
* Rewrite Markdown pipe tables as one labeled list item per row.
|
|
98
|
+
*
|
|
99
|
+
* Everything else passes through byte for byte. If the complete message would
|
|
100
|
+
* exceed the byte budget, return the original text for the existing delivery path.
|
|
101
|
+
*/
|
|
102
|
+
export function renderDiscordMarkdown(text: string, maxBytes: number): string {
|
|
103
|
+
if (!text.includes("|") || Buffer.byteLength(text, "utf8") > maxBytes) return text;
|
|
104
|
+
const lines = text.split("\n");
|
|
105
|
+
const out: string[] = [];
|
|
106
|
+
let remaining = maxBytes;
|
|
107
|
+
const append = (line: string): boolean => {
|
|
108
|
+
const bytes = Buffer.byteLength(line, "utf8") + (out.length === 0 ? 0 : 1);
|
|
109
|
+
if (bytes > remaining) return false;
|
|
110
|
+
remaining -= bytes;
|
|
111
|
+
out.push(line);
|
|
112
|
+
return true;
|
|
113
|
+
};
|
|
114
|
+
const margins: number[] = [];
|
|
115
|
+
let fence: { readonly marker: string; readonly margin: number } | undefined;
|
|
116
|
+
for (let index = 0; index < lines.length; index++) {
|
|
117
|
+
const line = lines[index];
|
|
118
|
+
if (fence) {
|
|
119
|
+
const columns = indentColumns(line);
|
|
120
|
+
if (columns < fence.margin && line.trim()) {
|
|
121
|
+
fence = undefined; // The containing list item ended.
|
|
122
|
+
} else {
|
|
123
|
+
const close = columns >= fence.margin && columns < fence.margin + 4
|
|
124
|
+
? FENCE_CLOSE.exec(line.replace(/^[ \t]*/, ""))?.[1]
|
|
125
|
+
: undefined;
|
|
126
|
+
if (close && close[0] === fence.marker[0] && close.length >= fence.marker.length) fence = undefined;
|
|
127
|
+
if (!append(line)) return text;
|
|
128
|
+
continue;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
if (line.trim()) {
|
|
132
|
+
let margin = margins.at(-1);
|
|
133
|
+
while (margin !== undefined && relativeLine(line, margin) === undefined) {
|
|
134
|
+
margins.pop();
|
|
135
|
+
margin = margins.at(-1);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
const margin = margins.at(-1) ?? 0;
|
|
139
|
+
const scoped = relativeLine(line, margin) ?? line;
|
|
140
|
+
const marker = opensFence(scoped);
|
|
141
|
+
const indent = listMargin(scoped);
|
|
142
|
+
if (marker) {
|
|
143
|
+
fence = { marker, margin };
|
|
144
|
+
} else if (indent !== undefined) {
|
|
145
|
+
const itemMargin = margin + indent;
|
|
146
|
+
margins.push(itemMargin);
|
|
147
|
+
const itemFence = opensFence(scoped.slice(indent));
|
|
148
|
+
if (itemFence) fence = { marker: itemFence, margin: itemMargin };
|
|
149
|
+
}
|
|
150
|
+
const delimiter = !marker && indent === undefined && scoped.includes("|") && !BLOCK_START.test(scoped)
|
|
151
|
+
? splitRow(relativeLine(lines[index + 1], margin))
|
|
152
|
+
: null;
|
|
153
|
+
const header = delimiter?.every(cell => DELIMITER_CELL.test(cell)) ? splitRow(scoped) : null;
|
|
154
|
+
if (!header || header.length !== delimiter?.length) {
|
|
155
|
+
if (!append(line)) return text;
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (out.at(-1)?.trim() && !append("")) return text;
|
|
159
|
+
const indentation = line.slice(0, line.length - scoped.length);
|
|
160
|
+
const first = index + 2;
|
|
161
|
+
const bodyRow = (at: number) => {
|
|
162
|
+
const row = relativeLine(lines[at], margin);
|
|
163
|
+
return row === undefined || BLOCK_START.test(row) || opensFence(row) ? null : splitRow(row);
|
|
164
|
+
};
|
|
165
|
+
let next = first;
|
|
166
|
+
let row = bodyRow(next);
|
|
167
|
+
do {
|
|
168
|
+
if (next !== first && !append("")) return text;
|
|
169
|
+
const columns = Math.max(header.length, row?.length ?? 0);
|
|
170
|
+
for (let column = 0; column < columns; column++) {
|
|
171
|
+
const prefix = column === 0 ? "- " : " - ";
|
|
172
|
+
const label = header[column] || `Column ${column + 1}`;
|
|
173
|
+
const value = row?.[column];
|
|
174
|
+
if (!append(`${indentation}${prefix}${label}:${value ? ` ${value}` : ""}`)) return text;
|
|
175
|
+
}
|
|
176
|
+
if (row) row = bodyRow(++next);
|
|
177
|
+
} while (row);
|
|
178
|
+
let following = next;
|
|
179
|
+
while (following < lines.length && !lines[following]?.trim()) following++;
|
|
180
|
+
const firstFollowing = lines[following];
|
|
181
|
+
if (firstFollowing !== undefined && indentColumns(firstFollowing) >= margin + 4
|
|
182
|
+
&& (!append("") || !append(`${indentation}---`))) return text;
|
|
183
|
+
if (lines[next]?.trim() && !append("")) return text;
|
|
184
|
+
index = next - 1;
|
|
75
185
|
}
|
|
76
|
-
return
|
|
186
|
+
return out.join("\n");
|
|
77
187
|
}
|
|
78
188
|
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
189
|
+
export interface DiscordEmbedField { name: string; value: string; inline: boolean }
|
|
190
|
+
export interface DiscordEmbed {
|
|
191
|
+
title?: string;
|
|
192
|
+
description?: string;
|
|
193
|
+
color?: number;
|
|
194
|
+
fields?: DiscordEmbedField[];
|
|
195
|
+
image?: { url: string };
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
/** Discord's documented embed limits, counted in UTF-16 units like message text. */
|
|
199
|
+
const EMBEDS_PER_MESSAGE = 10;
|
|
200
|
+
const EMBED_TOTAL = 6000;
|
|
201
|
+
const EMBED_TITLE = 256;
|
|
202
|
+
const EMBED_DESCRIPTION = 4096;
|
|
203
|
+
const EMBED_FIELDS = 25;
|
|
204
|
+
const FIELD_NAME = 256;
|
|
205
|
+
const FIELD_VALUE = 1024;
|
|
206
|
+
const IMAGE_URL = 2048;
|
|
207
|
+
const CARD_DEPTH = 16;
|
|
208
|
+
const DIVIDER = "\u2500".repeat(16);
|
|
209
|
+
const CARD_OPEN = /^ {0,3}(`{3,}|~{3,})[ \t]*card[ \t\r]*$/;
|
|
210
|
+
|
|
211
|
+
class InvalidCard extends Error {}
|
|
212
|
+
|
|
213
|
+
function object(value: unknown): Record<string, unknown> | null {
|
|
214
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value as Record<string, unknown> : null;
|
|
215
|
+
}
|
|
216
|
+
|
|
217
|
+
function text(props: Record<string, unknown>, key: string, required: boolean): string | undefined {
|
|
218
|
+
const value = props[key];
|
|
219
|
+
if (value === undefined && !required) return undefined;
|
|
220
|
+
if (typeof value !== "string" || !value.trim()) throw new InvalidCard();
|
|
221
|
+
return value;
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
function embedSize(embed: DiscordEmbed): number {
|
|
225
|
+
return (embed.title?.length ?? 0) + (embed.description?.length ?? 0)
|
|
226
|
+
+ (embed.fields ?? []).reduce((sum, field) => sum + field.name.length + field.value.length, 0);
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
/**
|
|
230
|
+
* Convert one json-render spec (`{ root, elements }`) whose root is a Card.
|
|
231
|
+
* Any structural error, unknown component, reuse/cycle or Discord limit makes
|
|
232
|
+
* the whole spec invalid; the caller then keeps the original fence as text.
|
|
233
|
+
*/
|
|
234
|
+
function cardEmbed(source: string): DiscordEmbed {
|
|
235
|
+
let spec: unknown;
|
|
236
|
+
try { spec = JSON.parse(source); } catch { throw new InvalidCard(); }
|
|
237
|
+
const root = object(spec);
|
|
238
|
+
const elements = root && object(root.elements);
|
|
239
|
+
if (!root || !elements || typeof root.root !== "string") throw new InvalidCard();
|
|
240
|
+
const parts: string[] = [];
|
|
241
|
+
const fields: DiscordEmbedField[] = [];
|
|
242
|
+
const embed: DiscordEmbed = {};
|
|
243
|
+
const seen = new Set<string>();
|
|
244
|
+
const visit = (id: unknown, depth: number, parent: string): void => {
|
|
245
|
+
if (typeof id !== "string" || depth > CARD_DEPTH || seen.has(id) || !Object.hasOwn(elements, id)) throw new InvalidCard();
|
|
246
|
+
seen.add(id);
|
|
247
|
+
const element = object(elements[id]);
|
|
248
|
+
if (!element) throw new InvalidCard();
|
|
249
|
+
const props = element.props === undefined ? {} : object(element.props);
|
|
250
|
+
const children = element.children === undefined ? [] : element.children;
|
|
251
|
+
if (!props || !Array.isArray(children)) throw new InvalidCard();
|
|
252
|
+
const type = element.type;
|
|
253
|
+
if (depth === 0 ? type !== "Card" : parent === "Fields" ? type !== "Field" : false) throw new InvalidCard();
|
|
254
|
+
switch (type) {
|
|
255
|
+
case "Card":
|
|
256
|
+
case "Section": {
|
|
257
|
+
const title = text(props, "title", false);
|
|
258
|
+
if (depth === 0) {
|
|
259
|
+
if (title !== undefined) embed.title = title;
|
|
260
|
+
const color = props.color;
|
|
261
|
+
if (typeof color === "number" && Number.isSafeInteger(color) && color >= 0 && color <= 0xffffff) embed.color = color;
|
|
262
|
+
else if (typeof color === "string" && /^#[0-9a-fA-F]{6}$/.test(color)) embed.color = Number.parseInt(color.slice(1), 16);
|
|
263
|
+
else if (color !== undefined) throw new InvalidCard();
|
|
264
|
+
} else if (title !== undefined) parts.push(`**${title}**`);
|
|
265
|
+
const description = type === "Card" ? text(props, "description", false) : undefined;
|
|
266
|
+
if (description !== undefined) parts.push(description);
|
|
267
|
+
break;
|
|
268
|
+
}
|
|
269
|
+
case "Text": parts.push(text(props, "text", true)!); break;
|
|
270
|
+
case "Fields": break;
|
|
271
|
+
case "Field": {
|
|
272
|
+
const inline = props.inline === undefined ? false : props.inline;
|
|
273
|
+
if (typeof inline !== "boolean") throw new InvalidCard();
|
|
274
|
+
fields.push({ name: text(props, "label", true)!, value: text(props, "value", true)!, inline });
|
|
275
|
+
break;
|
|
276
|
+
}
|
|
277
|
+
case "Table": {
|
|
278
|
+
const columns = props.columns;
|
|
279
|
+
const rows = props.rows;
|
|
280
|
+
if (!Array.isArray(columns) || columns.length === 0 || !columns.every(cell => typeof cell === "string")
|
|
281
|
+
|| !Array.isArray(rows) || !rows.every(row => Array.isArray(row) && row.length === columns.length
|
|
282
|
+
&& row.every(cell => typeof cell === "string"))) throw new InvalidCard();
|
|
283
|
+
const used = parts.reduce((sum, part) => sum + part.length, 0) + parts.length;
|
|
284
|
+
const table = cardTable([columns as string[], ...rows as string[][]], EMBED_DESCRIPTION - used);
|
|
285
|
+
if (!table) throw new InvalidCard();
|
|
286
|
+
parts.push(table);
|
|
287
|
+
break;
|
|
288
|
+
}
|
|
289
|
+
case "Divider": parts.push(DIVIDER); break;
|
|
290
|
+
case "Image": {
|
|
291
|
+
const url = text(props, "url", true)!;
|
|
292
|
+
if (embed.image || !url.startsWith("https://") || /\s/.test(url) || url.length > IMAGE_URL || !URL.canParse(url)) throw new InvalidCard();
|
|
293
|
+
embed.image = { url };
|
|
294
|
+
break;
|
|
295
|
+
}
|
|
296
|
+
default: throw new InvalidCard();
|
|
297
|
+
}
|
|
298
|
+
if (children.length > 0 && !["Card", "Section", "Fields"].includes(type)) throw new InvalidCard();
|
|
299
|
+
for (const child of children) visit(child, depth + 1, type);
|
|
300
|
+
};
|
|
301
|
+
visit(root.root, 0, "");
|
|
302
|
+
if (parts.length > 0) embed.description = parts.join("\n");
|
|
303
|
+
if (fields.length > 0) embed.fields = fields;
|
|
304
|
+
if ((embed.title?.length ?? 0) > EMBED_TITLE || (embed.description?.length ?? 0) > EMBED_DESCRIPTION
|
|
305
|
+
|| fields.length > EMBED_FIELDS || fields.some(field => field.name.length > FIELD_NAME || field.value.length > FIELD_VALUE)
|
|
306
|
+
|| embedSize(embed) > EMBED_TOTAL || !embed.title && !embed.description && !embed.fields && !embed.image) throw new InvalidCard();
|
|
307
|
+
return embed;
|
|
88
308
|
}
|
|
89
309
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
310
|
+
function cardTable(rows: string[][], budget: number): string | null {
|
|
311
|
+
const widths = rows[0]!.map(() => 3);
|
|
312
|
+
let delta = 0;
|
|
93
313
|
for (const row of rows) {
|
|
94
|
-
for (
|
|
95
|
-
|
|
314
|
+
for (const [i, cell] of row.entries()) {
|
|
315
|
+
const columns = Bun.stringWidth(cell);
|
|
316
|
+
widths[i] = Math.max(widths[i]!, columns);
|
|
317
|
+
delta += cell.length - columns;
|
|
96
318
|
}
|
|
97
319
|
}
|
|
98
|
-
const
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
for (const row of rows) {
|
|
105
|
-
for (const cell of row) bytes += Buffer.byteLength(cell, "utf8") - Bun.stringWidth(cell);
|
|
320
|
+
const rowLength = 2 + 3 * widths.length + widths.reduce((sum, width) => sum + width, 0);
|
|
321
|
+
if (7 + (rows.length + 1) * rowLength + delta > budget) return null;
|
|
322
|
+
const lines = ["```"];
|
|
323
|
+
for (const [i, row] of rows.entries()) {
|
|
324
|
+
lines.push("| " + row.map((cell, column) => cell + " ".repeat(widths[column]! - Bun.stringWidth(cell))).join(" | ") + " |");
|
|
325
|
+
if (i === 0) lines.push("| " + widths.map(width => "-".repeat(width)).join(" | ") + " |");
|
|
106
326
|
}
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
`| ${widths.map((width, column) => pad(cells[column] ?? "", width, alignOf(column))).join(" | ")} |`;
|
|
110
|
-
// Pure dashes keep the grid exact; alignment is already visible in the padded
|
|
111
|
-
// cells, and `:` markers would only shift every following column by one.
|
|
112
|
-
const separator = `| ${widths.map(width => "-".repeat(width)).join(" | ")} |`;
|
|
113
|
-
const lines = [fence, line(rows[0]!), separator];
|
|
114
|
-
for (let index = 1; index < rows.length; index++) lines.push(line(rows[index]!));
|
|
115
|
-
lines.push(fence);
|
|
116
|
-
return { lines, bytes };
|
|
327
|
+
lines.push("```");
|
|
328
|
+
return lines.join("\n");
|
|
117
329
|
}
|
|
118
330
|
|
|
119
331
|
/**
|
|
120
|
-
*
|
|
332
|
+
* Move valid ```card fences out of the text and into Discord embeds.
|
|
121
333
|
*
|
|
122
|
-
*
|
|
123
|
-
*
|
|
124
|
-
*
|
|
125
|
-
*
|
|
126
|
-
* message's byte budget, return the original text for the existing delivery path.
|
|
334
|
+
* Fences nested in another code block are content, not cards. A spec that is
|
|
335
|
+
* invalid, or would exceed the message's 10 embeds or 6000 embed characters,
|
|
336
|
+
* stays verbatim as its original code block, which is also what Telegram and
|
|
337
|
+
* Slack show because they never call this.
|
|
127
338
|
*/
|
|
128
|
-
export function
|
|
129
|
-
if (!
|
|
130
|
-
|
|
131
|
-
if (bytes > maxBytes) return text;
|
|
132
|
-
const lines = text.split("\n");
|
|
339
|
+
export function extractDiscordCards(source: string): { text: string; embeds: DiscordEmbed[] } {
|
|
340
|
+
if (!source.includes("card")) return { text: source, embeds: [] };
|
|
341
|
+
const lines = source.split("\n");
|
|
133
342
|
const out: string[] = [];
|
|
134
|
-
|
|
343
|
+
const embeds: DiscordEmbed[] = [];
|
|
344
|
+
let total = 0;
|
|
345
|
+
const margins: number[] = [];
|
|
346
|
+
let fence: { marker: string; margin: number } | undefined;
|
|
135
347
|
for (let index = 0; index < lines.length; index++) {
|
|
136
348
|
const line = lines[index]!;
|
|
137
|
-
const
|
|
349
|
+
const columns = indentColumns(line);
|
|
350
|
+
if (fence && columns < fence.margin && line.trim()) fence = undefined;
|
|
138
351
|
if (fence) {
|
|
352
|
+
const close = columns >= fence.margin && columns < fence.margin + 4
|
|
353
|
+
? FENCE_CLOSE.exec(line.replace(/^[ \t]*/, ""))?.[1] : undefined;
|
|
354
|
+
if (close && close[0] === fence.marker[0] && close.length >= fence.marker.length) fence = undefined;
|
|
139
355
|
out.push(line);
|
|
140
|
-
const close = FENCE_CLOSE.exec(line)?.[1];
|
|
141
|
-
if (close && close[0] === fence.char && close.length >= fence.length) fence = null;
|
|
142
356
|
continue;
|
|
143
357
|
}
|
|
144
|
-
if (
|
|
145
|
-
|
|
146
|
-
out.push(line);
|
|
147
|
-
continue;
|
|
358
|
+
if (line.trim()) {
|
|
359
|
+
while (margins.length && relativeLine(line, margins.at(-1)!) === undefined) margins.pop();
|
|
148
360
|
}
|
|
149
|
-
const
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
361
|
+
const margin = margins.at(-1) ?? 0;
|
|
362
|
+
const scoped = relativeLine(line, margin) ?? line;
|
|
363
|
+
const card = margin === 0 ? CARD_OPEN.exec(scoped)?.[1] : undefined;
|
|
364
|
+
if (card) {
|
|
365
|
+
let end = index + 1;
|
|
366
|
+
for (; end < lines.length; end++) {
|
|
367
|
+
const close = FENCE_CLOSE.exec(lines[end]!)?.[1];
|
|
368
|
+
if (close && close[0] === card[0] && close.length >= card.length) break;
|
|
369
|
+
}
|
|
370
|
+
if (end < lines.length && embeds.length < EMBEDS_PER_MESSAGE) {
|
|
371
|
+
try {
|
|
372
|
+
const embed = cardEmbed(lines.slice(index + 1, end).join("\n"));
|
|
373
|
+
if (total + embedSize(embed) <= EMBED_TOTAL) {
|
|
374
|
+
embeds.push(embed);
|
|
375
|
+
total += embedSize(embed);
|
|
376
|
+
index = end;
|
|
377
|
+
continue;
|
|
378
|
+
}
|
|
379
|
+
} catch (error) { if (!(error instanceof InvalidCard)) throw error; }
|
|
380
|
+
}
|
|
154
381
|
}
|
|
155
|
-
const
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
if (
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
382
|
+
const marker = opensFence(scoped);
|
|
383
|
+
if (marker) fence = { marker, margin };
|
|
384
|
+
else {
|
|
385
|
+
const indent = listMargin(scoped);
|
|
386
|
+
if (indent !== undefined) {
|
|
387
|
+
const itemMargin = margin + indent;
|
|
388
|
+
margins.push(itemMargin);
|
|
389
|
+
const itemFence = opensFence(scoped.slice(indent));
|
|
390
|
+
if (itemFence) fence = { marker: itemFence, margin: itemMargin };
|
|
391
|
+
}
|
|
164
392
|
}
|
|
165
|
-
|
|
166
|
-
if (!table) return text;
|
|
167
|
-
bytes += table.bytes - sourceBytes;
|
|
168
|
-
for (const rendered of table.lines) out.push(rendered);
|
|
169
|
-
index = next - 1;
|
|
393
|
+
out.push(line);
|
|
170
394
|
}
|
|
171
|
-
|
|
395
|
+
if (embeds.length === 0) return { text: source, embeds };
|
|
396
|
+
const occupied = (line: string) => !/^[ \t\r]*$/.test(line);
|
|
397
|
+
const start = out.findIndex(occupied);
|
|
398
|
+
return { text: start < 0 ? "" : out.slice(start, out.findLastIndex(occupied) + 1).join("\n"), embeds };
|
|
172
399
|
}
|