omp-superagent 0.1.8 → 0.1.10
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 +16 -8
- package/README.ko.md +28 -374
- package/README.md +30 -421
- package/THIRD_PARTY_NOTICES.md +8 -8
- package/config.example.json +7 -0
- package/docs/OPERATIONS.en.md +2223 -0
- package/docs/OPERATIONS.md +164 -23
- package/docs/design/execution.md +164 -18
- package/docs/design/messaging.md +78 -17
- package/docs/kakao-bridge-protocol.md +193 -0
- package/docs/releases/0.1.10.md +17 -0
- package/docs/releases/0.1.9.md +18 -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/notices/darwin-arm64/Cargo-LICENSES.txt +2 -0
- package/host-owner/bin/notices/darwin-arm64/Cargo-SOURCES.txt +2 -0
- package/host-owner/bin/notices/darwin-arm64/crates/bitflags-2.13.2/LICENSE-APACHE +201 -0
- package/host-owner/bin/notices/darwin-arm64/crates/bitflags-2.13.2/LICENSE-MIT +25 -0
- package/host-owner/bin/notices/darwin-arm64/crates/rustix-1.1.6/COPYRIGHT +29 -0
- package/host-owner/bin/notices/darwin-arm64/crates/rustix-1.1.6/LICENSE-APACHE +201 -0
- package/host-owner/bin/notices/darwin-arm64/crates/rustix-1.1.6/LICENSE-Apache-2.0_WITH_LLVM-exception +220 -0
- package/host-owner/bin/notices/darwin-arm64/crates/rustix-1.1.6/LICENSE-MIT +23 -0
- package/host-owner/bin/notices/darwin-x64/Cargo-LICENSES.txt +2 -0
- package/host-owner/bin/notices/darwin-x64/Cargo-SOURCES.txt +2 -0
- package/host-owner/bin/notices/darwin-x64/crates/bitflags-2.13.2/LICENSE-APACHE +201 -0
- package/host-owner/bin/notices/darwin-x64/crates/bitflags-2.13.2/LICENSE-MIT +25 -0
- package/host-owner/bin/notices/darwin-x64/crates/rustix-1.1.6/COPYRIGHT +29 -0
- package/host-owner/bin/notices/darwin-x64/crates/rustix-1.1.6/LICENSE-APACHE +201 -0
- package/host-owner/bin/notices/darwin-x64/crates/rustix-1.1.6/LICENSE-Apache-2.0_WITH_LLVM-exception +220 -0
- package/host-owner/bin/notices/darwin-x64/crates/rustix-1.1.6/LICENSE-MIT +23 -0
- package/host-owner/bin/notices/linux-arm64/Cargo-LICENSES.txt +3 -0
- package/host-owner/bin/notices/linux-arm64/Cargo-SOURCES.txt +3 -0
- package/host-owner/bin/notices/linux-arm64/crates/bitflags-2.13.2/LICENSE-APACHE +201 -0
- package/host-owner/bin/notices/linux-arm64/crates/bitflags-2.13.2/LICENSE-MIT +25 -0
- package/host-owner/bin/notices/linux-arm64/crates/linux-raw-sys-0.12.1/COPYRIGHT +29 -0
- package/host-owner/bin/notices/linux-arm64/crates/linux-raw-sys-0.12.1/LICENSE-APACHE +201 -0
- package/host-owner/bin/notices/linux-arm64/crates/linux-raw-sys-0.12.1/LICENSE-Apache-2.0_WITH_LLVM-exception +220 -0
- package/host-owner/bin/notices/linux-arm64/crates/linux-raw-sys-0.12.1/LICENSE-MIT +23 -0
- package/host-owner/bin/notices/linux-arm64/crates/rustix-1.1.6/COPYRIGHT +29 -0
- package/host-owner/bin/notices/linux-arm64/crates/rustix-1.1.6/LICENSE-APACHE +201 -0
- package/host-owner/bin/notices/linux-arm64/crates/rustix-1.1.6/LICENSE-Apache-2.0_WITH_LLVM-exception +220 -0
- package/host-owner/bin/notices/linux-arm64/crates/rustix-1.1.6/LICENSE-MIT +23 -0
- package/host-owner/bin/notices/linux-x64/Cargo-LICENSES.txt +3 -0
- package/host-owner/bin/notices/linux-x64/Cargo-SOURCES.txt +3 -0
- package/host-owner/bin/notices/linux-x64/crates/bitflags-2.13.2/LICENSE-APACHE +201 -0
- package/host-owner/bin/notices/linux-x64/crates/bitflags-2.13.2/LICENSE-MIT +25 -0
- package/host-owner/bin/notices/linux-x64/crates/linux-raw-sys-0.12.1/COPYRIGHT +29 -0
- package/host-owner/bin/notices/linux-x64/crates/linux-raw-sys-0.12.1/LICENSE-APACHE +201 -0
- package/host-owner/bin/notices/linux-x64/crates/linux-raw-sys-0.12.1/LICENSE-Apache-2.0_WITH_LLVM-exception +220 -0
- package/host-owner/bin/notices/linux-x64/crates/linux-raw-sys-0.12.1/LICENSE-MIT +23 -0
- package/host-owner/bin/notices/linux-x64/crates/rustix-1.1.6/COPYRIGHT +29 -0
- package/host-owner/bin/notices/linux-x64/crates/rustix-1.1.6/LICENSE-APACHE +201 -0
- package/host-owner/bin/notices/linux-x64/crates/rustix-1.1.6/LICENSE-Apache-2.0_WITH_LLVM-exception +220 -0
- package/host-owner/bin/notices/linux-x64/crates/rustix-1.1.6/LICENSE-MIT +23 -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 +10 -8
- package/skills/head/SKILL.md +28 -14
- package/skills/head/references/routing.md +23 -17
- package/skills/head/references/supervision.md +22 -1
- package/skills/lane/SKILL.md +11 -0
- package/skills/lane/references/workflow.md +17 -8
- package/src/cli.ts +6 -4
- package/src/config.ts +44 -3
- package/src/contracts.ts +101 -8
- package/src/discord-command-registration.ts +34 -186
- package/src/extension.ts +44 -6
- package/src/gateway-client.ts +11 -59
- package/src/gateway-protocol.ts +35 -1
- package/src/head-recovery.ts +226 -17
- package/src/host-extension.ts +132 -17
- package/src/host-protocol.ts +6 -9
- package/src/host-reply-voice.ts +181 -0
- package/src/lane-extension.ts +46 -8
- package/src/lane-models.ts +8 -0
- package/src/lane-report.ts +22 -0
- package/src/managed-ulw-probe.ts +22 -20
- package/src/message-order.ts +2 -1
- package/src/messenger-command-catalog.json +6 -3
- package/src/messenger-commands.ts +9 -2
- package/src/operator.ts +77 -0
- package/src/pr-github-schema.ts +2 -0
- package/src/pr-intake-automation.ts +1 -2
- package/src/pr-intake-comment.ts +1 -2
- package/src/pr-intake-fix.ts +1 -2
- package/src/pr-intake-github.ts +1 -2
- package/src/pr-intake-material.ts +1 -2
- package/src/pr-intake-tree.ts +1 -2
- package/src/pr-review-models.ts +28 -0
- package/src/result-attachment.ts +60 -79
- package/src/runtime.ts +1862 -298
- package/src/setup-profiles.ts +26 -65
- package/src/setup.ts +2 -1
- package/src/slack-command-registration.ts +48 -197
- package/src/soul.ts +214 -0
- package/src/store.ts +643 -55
- package/src/superkit-skills.ts +22 -0
- package/src/telegram-command-languages.json +17 -0
- package/src/telegram-command-registration.ts +32 -134
- package/src/worker-bridge.ts +21 -0
- package/src/worker-enrollment.ts +2 -1
- package/src/worker-host.ts +0 -24
- package/src/worker-serve-prepare.ts +19 -0
- package/src/worker-service.ts +51 -47
- package/scripts/ci.ts +0 -44
- package/scripts/release.ts +0 -112
- package/scripts/test-omp.sh +0 -44
- package/src/discord-commands.ts +0 -29
- package/src/discord-markdown.ts +0 -399
- package/src/slack-commands.ts +0 -18
- package/src/telegram-signal.ts +0 -14
- package/src/worker-stdio.ts +0 -61
package/scripts/test-omp.sh
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env bash
|
|
2
|
-
# Run CI with a stock release in a private directory, without replacing the user's OMP.
|
|
3
|
-
set -euo pipefail
|
|
4
|
-
cd -- "$(dirname -- "${BASH_SOURCE[0]}")/.."
|
|
5
|
-
|
|
6
|
-
if [[ $# -gt 1 || ${OMP_TEST_COMMAND+x} ]]; then
|
|
7
|
-
echo 'Usage: bash scripts/test-omp.sh [release-tag]; use bun run test:ci for OMP_TEST_COMMAND overrides.' >&2
|
|
8
|
-
exit 2
|
|
9
|
-
fi
|
|
10
|
-
tag=${1-"$(bun --no-env-file -p '"v" + require("./package.json").dependencies["@oh-my-pi/pi-coding-agent"]')"}
|
|
11
|
-
if [[ ! $tag =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
|
|
12
|
-
echo "Expected an exact OMP release tag; got '$tag'." >&2
|
|
13
|
-
exit 2
|
|
14
|
-
fi
|
|
15
|
-
# Install dependencies and build both Superagent executables before the test CLI exists.
|
|
16
|
-
bun install --frozen-lockfile
|
|
17
|
-
platform=$(bun --no-env-file -p '`${process.platform}-${process.arch}`')
|
|
18
|
-
for binary in "host-owner/bin/omp-host-owner-$platform" "gateway/bin/omp-gateway-$platform"; do
|
|
19
|
-
if [[ ! -x $binary ]]; then
|
|
20
|
-
echo "Source installation did not create an executable $binary." >&2
|
|
21
|
-
exit 1
|
|
22
|
-
fi
|
|
23
|
-
done
|
|
24
|
-
asset=$(bun --no-env-file -p '`omp-${process.platform}-${process.arch}`')
|
|
25
|
-
directory=$(mktemp -d "${TMPDIR:-/tmp}/omp-sa-test-cli.XXXXXXXX")
|
|
26
|
-
trap 'rm -rf -- "$directory"' EXIT
|
|
27
|
-
url="https://github.com/can1357/oh-my-pi/releases/download/$tag"
|
|
28
|
-
echo "Testing stock OMP $tag ($asset)"
|
|
29
|
-
for file in SHA256SUMS.txt "$asset"; do
|
|
30
|
-
curl --fail --silent --show-error --location "$url/$file" --output "$directory/$file"
|
|
31
|
-
done
|
|
32
|
-
(
|
|
33
|
-
cd -- "$directory"
|
|
34
|
-
grep -E "^[[:xdigit:]]{64} $asset\$" SHA256SUMS.txt | shasum -a 256 -c -
|
|
35
|
-
mv -- "$asset" omp
|
|
36
|
-
chmod 700 omp
|
|
37
|
-
)
|
|
38
|
-
actual=$("$directory/omp" --version)
|
|
39
|
-
if [[ $actual != "omp/${tag#v}" ]]; then
|
|
40
|
-
echo "OMP release $tag reported '$actual'; refusing to test a different version." >&2
|
|
41
|
-
exit 1
|
|
42
|
-
fi
|
|
43
|
-
# The frozen source install above already built and staged both native binaries.
|
|
44
|
-
PATH="$directory:$PATH" bun --no-env-file scripts/ci.ts
|
package/src/discord-commands.ts
DELETED
|
@@ -1,29 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
ApplicationCommandOptionType, ApplicationCommandType, ApplicationIntegrationType, InteractionContextType,
|
|
3
|
-
type RESTPostAPIChatInputApplicationCommandsJSONBody,
|
|
4
|
-
} from "discord.js";
|
|
5
|
-
import { MESSENGER_COMMANDS } from "./messenger-commands";
|
|
6
|
-
|
|
7
|
-
/** The complete supported slash-command picker, also used for registration. */
|
|
8
|
-
export const DISCORD_COMMANDS: readonly RESTPostAPIChatInputApplicationCommandsJSONBody[] = MESSENGER_COMMANDS.map(
|
|
9
|
-
(command): RESTPostAPIChatInputApplicationCommandsJSONBody => ({
|
|
10
|
-
type: ApplicationCommandType.ChatInput, name: command.name, description: command.description,
|
|
11
|
-
contexts: [InteractionContextType.Guild, InteractionContextType.BotDM],
|
|
12
|
-
integration_types: [ApplicationIntegrationType.GuildInstall],
|
|
13
|
-
...(command.text !== undefined ? { options: [{ type: ApplicationCommandOptionType.String, name: "text", description: command.text, required: false }] } : {}),
|
|
14
|
-
}));
|
|
15
|
-
|
|
16
|
-
/** Reject stale registrations/options instead of silently executing a different command. */
|
|
17
|
-
export function parseDiscordCommand(name: string, options: unknown): string | null {
|
|
18
|
-
const command = DISCORD_COMMANDS.find(command => command.name === name);
|
|
19
|
-
if (!command || !Array.isArray(options)) return null;
|
|
20
|
-
if (options.length === 0) return `/${name}`;
|
|
21
|
-
if (!command.options?.length || options.length !== 1) return null;
|
|
22
|
-
const option: unknown = options[0];
|
|
23
|
-
if (!option || typeof option !== "object" || Array.isArray(option)) return null;
|
|
24
|
-
const value = option as Record<string, unknown>;
|
|
25
|
-
if (Object.keys(value).some(key => key !== "name" && key !== "type" && key !== "value") ||
|
|
26
|
-
value.name !== "text" || value.type !== ApplicationCommandOptionType.String || typeof value.value !== "string") return null;
|
|
27
|
-
const text = value.value.trim();
|
|
28
|
-
return `/${name}${text ? ` ${text}` : ""}`;
|
|
29
|
-
}
|
package/src/discord-markdown.ts
DELETED
|
@@ -1,399 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Markdown table rendering for the Discord transport.
|
|
3
|
-
*
|
|
4
|
-
* Discord has no pipe-table support: a table sent verbatim arrives as literal
|
|
5
|
-
* `| a | b |` rows. Labeled lists preserve inline Markdown and wrap on
|
|
6
|
-
* narrow screens without requiring horizontal scrolling.
|
|
7
|
-
*/
|
|
8
|
-
|
|
9
|
-
const FENCE_OPEN = /^ {0,3}(`{3,}|~{3,})/;
|
|
10
|
-
const FENCE_CLOSE = /^ {0,3}(`{3,}|~{3,})[ \t\r]*$/;
|
|
11
|
-
const BLOCK_START = /^ {0,3}(?:>|#{1,6}(?:[ \t]|$)|(?:[-+*]|\d{1,9}[.)])(?:[ \t]|$))/;
|
|
12
|
-
const INDENTED_CODE = /^(?: {4}| {0,3}\t)/;
|
|
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
|
-
}
|
|
46
|
-
|
|
47
|
-
/**
|
|
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.
|
|
50
|
-
*/
|
|
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);
|
|
56
|
-
const cells: string[] = [];
|
|
57
|
-
let start = 0;
|
|
58
|
-
let pipe = false;
|
|
59
|
-
let decoded = "";
|
|
60
|
-
let ticks = 0;
|
|
61
|
-
for (let index = 0; index < line.length; index++) {
|
|
62
|
-
const char = line[index];
|
|
63
|
-
const following = line[index + 1];
|
|
64
|
-
if (ticks === 0 && char === "\\" && (following === "|" || following === "`" || following === "\\")) {
|
|
65
|
-
index++;
|
|
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);
|
|
85
|
-
pipe = true;
|
|
86
|
-
decoded = "";
|
|
87
|
-
start = index + 1;
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
if (!pipe) return null;
|
|
91
|
-
const tail = (decoded + line.slice(start)).trim();
|
|
92
|
-
if (tail !== "") cells.push(tail);
|
|
93
|
-
return cells.length > 0 ? cells : null;
|
|
94
|
-
}
|
|
95
|
-
|
|
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;
|
|
185
|
-
}
|
|
186
|
-
return out.join("\n");
|
|
187
|
-
}
|
|
188
|
-
|
|
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;
|
|
308
|
-
}
|
|
309
|
-
|
|
310
|
-
function cardTable(rows: string[][], budget: number): string | null {
|
|
311
|
-
const widths = rows[0]!.map(() => 3);
|
|
312
|
-
let delta = 0;
|
|
313
|
-
for (const row of rows) {
|
|
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;
|
|
318
|
-
}
|
|
319
|
-
}
|
|
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(" | ") + " |");
|
|
326
|
-
}
|
|
327
|
-
lines.push("```");
|
|
328
|
-
return lines.join("\n");
|
|
329
|
-
}
|
|
330
|
-
|
|
331
|
-
/**
|
|
332
|
-
* Move valid ```card fences out of the text and into Discord embeds.
|
|
333
|
-
*
|
|
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.
|
|
338
|
-
*/
|
|
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");
|
|
342
|
-
const out: string[] = [];
|
|
343
|
-
const embeds: DiscordEmbed[] = [];
|
|
344
|
-
let total = 0;
|
|
345
|
-
const margins: number[] = [];
|
|
346
|
-
let fence: { marker: string; margin: number } | undefined;
|
|
347
|
-
for (let index = 0; index < lines.length; index++) {
|
|
348
|
-
const line = lines[index]!;
|
|
349
|
-
const columns = indentColumns(line);
|
|
350
|
-
if (fence && columns < fence.margin && line.trim()) fence = undefined;
|
|
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;
|
|
355
|
-
out.push(line);
|
|
356
|
-
continue;
|
|
357
|
-
}
|
|
358
|
-
if (line.trim()) {
|
|
359
|
-
while (margins.length && relativeLine(line, margins.at(-1)!) === undefined) margins.pop();
|
|
360
|
-
}
|
|
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
|
-
}
|
|
381
|
-
}
|
|
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
|
-
}
|
|
392
|
-
}
|
|
393
|
-
out.push(line);
|
|
394
|
-
}
|
|
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 };
|
|
399
|
-
}
|
package/src/slack-commands.ts
DELETED
|
@@ -1,18 +0,0 @@
|
|
|
1
|
-
import { MESSENGER_COMMANDS } from "./messenger-commands";
|
|
2
|
-
|
|
3
|
-
/** Slack reserves /help and /status, so every supported picker entry is namespaced. */
|
|
4
|
-
export const SLACK_COMMANDS = MESSENGER_COMMANDS.map(command => ({
|
|
5
|
-
command: `/superagent-${command.name}`,
|
|
6
|
-
description: command.description,
|
|
7
|
-
...(command.text !== undefined ? { usage_hint: command.text } : {}),
|
|
8
|
-
should_escape: false,
|
|
9
|
-
}));
|
|
10
|
-
|
|
11
|
-
/** Only registered names are accepted; invalid no-argument invocations are not commands. */
|
|
12
|
-
export function parseSlackCommand(name: string, text: string): string | null {
|
|
13
|
-
const command = MESSENGER_COMMANDS.find(command => name === `/superagent-${command.name}`);
|
|
14
|
-
if (!command) return null;
|
|
15
|
-
const argument = text.trim();
|
|
16
|
-
if (argument && command.text === undefined) return null;
|
|
17
|
-
return `/${command.name}${argument ? ` ${argument}` : ""}`;
|
|
18
|
-
}
|
package/src/telegram-signal.ts
DELETED
|
@@ -1,14 +0,0 @@
|
|
|
1
|
-
import { AbortController as ApiAbortController, type AbortSignal as ApiAbortSignal } from "abort-controller";
|
|
2
|
-
|
|
3
|
-
/** grammY's Node entrypoint requires its SDK signal; keep caller cancellation scoped to the API call. */
|
|
4
|
-
export async function withTelegramSignal<T>(
|
|
5
|
-
signal: AbortSignal | undefined, call: (signal: ApiAbortSignal | undefined) => Promise<T>,
|
|
6
|
-
): Promise<T> {
|
|
7
|
-
if (!signal) return call(undefined);
|
|
8
|
-
const controller = new ApiAbortController();
|
|
9
|
-
const abort = () => controller.abort();
|
|
10
|
-
signal.addEventListener("abort", abort, { once: true });
|
|
11
|
-
if (signal.aborted) abort();
|
|
12
|
-
try { return await call(controller.signal); }
|
|
13
|
-
finally { signal.removeEventListener("abort", abort); }
|
|
14
|
-
}
|
package/src/worker-stdio.ts
DELETED
|
@@ -1,61 +0,0 @@
|
|
|
1
|
-
import type { Socket } from "node:net";
|
|
2
|
-
import type { Readable, Writable } from "node:stream";
|
|
3
|
-
import { WorkerRefusal } from "./worker-request";
|
|
4
|
-
import { WORKER_HEADER_BYTES } from "./worker-wire";
|
|
5
|
-
|
|
6
|
-
export function workerHeader(input: Readable): Promise<Buffer> {
|
|
7
|
-
return new Promise((resolve, reject) => {
|
|
8
|
-
const chunks: Buffer[] = [];
|
|
9
|
-
let length = 0;
|
|
10
|
-
const stop = () => {
|
|
11
|
-
clearTimeout(timer);
|
|
12
|
-
input.off("readable", read).off("end", closed).off("close", closed).off("error", fail);
|
|
13
|
-
};
|
|
14
|
-
const fail = (error: Error) => { stop(); reject(error); };
|
|
15
|
-
const closed = () => fail(new WorkerRefusal("Worker request ended before its header"));
|
|
16
|
-
const read = () => {
|
|
17
|
-
let chunk: Buffer | null;
|
|
18
|
-
while ((chunk = input.read()) !== null) {
|
|
19
|
-
const end = chunk.indexOf(10);
|
|
20
|
-
length += end < 0 ? chunk.length : end + 1;
|
|
21
|
-
if (length > WORKER_HEADER_BYTES) { fail(new WorkerRefusal("Worker request exceeds header limit")); return; }
|
|
22
|
-
chunks.push(end < 0 ? chunk : chunk.subarray(0, end));
|
|
23
|
-
if (end < 0) continue;
|
|
24
|
-
stop();
|
|
25
|
-
if (end + 1 < chunk.length) input.unshift(chunk.subarray(end + 1));
|
|
26
|
-
resolve(Buffer.concat(chunks, length - 1));
|
|
27
|
-
return;
|
|
28
|
-
}
|
|
29
|
-
};
|
|
30
|
-
const timer = setTimeout(() => fail(new WorkerRefusal("Worker request header timed out")), 10_000);
|
|
31
|
-
input.once("end", closed).once("close", closed).once("error", fail).on("readable", read);
|
|
32
|
-
});
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function workerReply(output: Writable, reply: { readonly ok: true; readonly value: unknown } | { readonly ok: false; readonly error: string }): Promise<void> {
|
|
36
|
-
const line = `${JSON.stringify(reply)}\n`;
|
|
37
|
-
const framed = Buffer.byteLength(line) <= WORKER_HEADER_BYTES ? line
|
|
38
|
-
: `${JSON.stringify({ ok: false, error: "Worker response exceeds header limit" })}\n`;
|
|
39
|
-
return new Promise((resolve, reject) => output.write(framed, error => error ? reject(error) : resolve()));
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/** An EOF on either transport detaches only this client. It is not a native exit receipt. */
|
|
43
|
-
export async function bridgeWorker(input: Readable, output: Writable, socket: Socket): Promise<void> {
|
|
44
|
-
const { promise, resolve } = Promise.withResolvers<void>();
|
|
45
|
-
const detach = () => resolve();
|
|
46
|
-
input.once("end", detach).once("close", detach).once("error", detach);
|
|
47
|
-
output.once("close", detach).once("error", detach);
|
|
48
|
-
socket.once("close", detach).once("error", detach);
|
|
49
|
-
try {
|
|
50
|
-
if (input.destroyed || input.readableEnded || output.destroyed || socket.destroyed) return;
|
|
51
|
-
socket.pipe(output, { end: false });
|
|
52
|
-
input.pipe(socket, { end: false });
|
|
53
|
-
await promise;
|
|
54
|
-
} finally {
|
|
55
|
-
input.unpipe(socket).pause();
|
|
56
|
-
socket.unpipe(output).destroy();
|
|
57
|
-
input.off("end", detach).off("close", detach).off("error", detach);
|
|
58
|
-
output.off("close", detach).off("error", detach);
|
|
59
|
-
socket.off("close", detach).off("error", detach);
|
|
60
|
-
}
|
|
61
|
-
}
|