@alexsun-top/opencode-chat-room 0.7.8 → 0.8.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 +43 -1
- package/LICENSE +21 -0
- package/README.md +77 -36
- package/README.zh-CN.md +82 -41
- package/cli.ts +27 -1
- package/dist/cli.js +6905 -2764
- package/package.json +33 -3
- package/packages/chat-room-channeld/README.md +315 -0
- package/packages/chat-room-channeld/dist/feishu-adapter.js +1262 -0
- package/packages/chat-room-channeld/dist/mock-adapter.js +295 -0
- package/packages/chat-room-channeld/src/adapter.ts +509 -0
- package/packages/chat-room-channeld/src/cli.ts +124 -0
- package/packages/chat-room-channeld/src/config.ts +269 -0
- package/packages/chat-room-channeld/src/delivery.ts +163 -0
- package/packages/chat-room-channeld/src/feishu-adapter.ts +303 -0
- package/packages/chat-room-channeld/src/feishu-card.ts +27 -0
- package/packages/chat-room-channeld/src/feishu-inbound.ts +158 -0
- package/packages/chat-room-channeld/src/feishu-live.ts +355 -0
- package/packages/chat-room-channeld/src/feishu-send.ts +342 -0
- package/packages/chat-room-channeld/src/feishu-ws-frames.ts +173 -0
- package/packages/chat-room-channeld/src/framing.ts +128 -0
- package/packages/chat-room-channeld/src/host-helpers.ts +52 -0
- package/packages/chat-room-channeld/src/host.ts +627 -0
- package/packages/chat-room-channeld/src/inbound.ts +26 -0
- package/packages/chat-room-channeld/src/ingest-gate.ts +108 -0
- package/packages/chat-room-channeld/src/log.ts +26 -0
- package/packages/chat-room-channeld/src/mock-adapter.ts +206 -0
- package/packages/chat-room-channeld/src/protocol.ts +75 -0
- package/packages/chat-room-channeld/src/published.ts +37 -0
- package/packages/chat-room-channeld/src/rules.ts +127 -0
- package/packages/chat-room-channeld/src/state.ts +502 -0
- package/packages/chat-room-channeld/src/stats.ts +209 -0
- package/plugin.ts +22 -5
- package/server/addressing-routes.ts +130 -0
- package/server/admin-state.ts +13 -13
- package/server/app.ts +95 -0
- package/server/assets/NOTICE.txt +3 -2
- package/server/assets/SHIKI-LICENSE +22 -0
- package/server/assets/base.css +24 -10
- package/server/assets/chat/announcements.js +70 -7
- package/server/assets/chat/doc-preview.js +187 -39
- package/server/assets/chat/events.js +3 -0
- package/server/assets/chat/files.js +46 -4
- package/server/assets/chat/forward.js +69 -0
- package/server/assets/chat/index.js +4 -0
- package/server/assets/chat/integration-editor.js +163 -0
- package/server/assets/chat/integration-values.js +124 -0
- package/server/assets/chat/integrations.js +498 -0
- package/server/assets/chat/locales/de.js +45 -4
- package/server/assets/chat/locales/es.js +45 -4
- package/server/assets/chat/locales/fr.js +45 -4
- package/server/assets/chat/locales/it.js +45 -4
- package/server/assets/chat/locales/ja.js +45 -4
- package/server/assets/chat/locales/ko.js +45 -4
- package/server/assets/chat/markdown-footnotes.js +115 -0
- package/server/assets/chat/markdown.js +46 -43
- package/server/assets/chat/messages.js +5 -5
- package/server/assets/chat/office-preview.js +69 -0
- package/server/assets/chat/palette.js +1 -1
- package/server/assets/chat/panels.js +36 -0
- package/server/assets/chat/recall.js +58 -7
- package/server/assets/chat/receipts.js +15 -3
- package/server/assets/chat/reminders.js +69 -8
- package/server/assets/chat/replies.js +7 -1
- package/server/assets/chat/resource-format.js +11 -0
- package/server/assets/chat/settings.js +30 -2
- package/server/assets/chat/shiki.js +87 -0
- package/server/assets/chat/side-pane.js +9 -3
- package/server/assets/chat/state.js +107 -11
- package/server/assets/chat/system-events.js +32 -0
- package/server/assets/chat/system-messages.js +23 -12
- package/server/assets/chat/task-row.js +0 -6
- package/server/assets/chat/tasks.js +121 -33
- package/server/assets/code-highlight.css +74 -62
- package/server/assets/conversation.css +126 -25
- package/server/assets/doc-preview.css +36 -3
- package/server/assets/files.css +5 -0
- package/server/assets/fluent-icons.svg +2 -0
- package/server/assets/monaco/LICENSE +21 -0
- package/server/assets/monaco/ThirdPartyNotices.txt +448 -0
- package/server/assets/monaco/codicon-jdswhhtn.ttf +0 -0
- package/server/assets/monaco/editor.worker.js +45 -0
- package/server/assets/monaco/integration-rules.css +1 -0
- package/server/assets/monaco/integration-rules.js +1297 -0
- package/server/assets/monaco/json.worker.js +77 -0
- package/server/assets/office/THIRD-PARTY-NOTICES.txt +11 -0
- package/server/assets/office/docx.bundle.js +33 -0
- package/server/assets/office/licenses/docx-preview-LICENSE.txt +201 -0
- package/server/assets/office/licenses/echarts-LICENSE.txt +222 -0
- package/server/assets/office/licenses/jszip-LICENSE.txt +651 -0
- package/server/assets/office/licenses/lodash-LICENSE.txt +47 -0
- package/server/assets/office/licenses/pptx-preview-LICENSE.txt +8 -0
- package/server/assets/office/licenses/tslib-LICENSE.txt +12 -0
- package/server/assets/office/licenses/uuid-LICENSE.txt +9 -0
- package/server/assets/office/licenses/xlsx-LICENSE.txt +201 -0
- package/server/assets/{HIGHLIGHT-LICENSE → office/licenses/zrender-LICENSE.txt} +2 -2
- package/server/assets/office/pptx.bundle.js +104 -0
- package/server/assets/office/xlsx.bundle.js +141 -0
- package/server/assets/recall.css +5 -4
- package/server/assets/responsive.css +52 -8
- package/server/assets/settings.css +73 -1
- package/server/assets/shiki.bundle.js +152 -0
- package/server/assets/tasks.css +57 -7
- package/server/channel-integration.ts +519 -0
- package/server/channel-settings.ts +77 -0
- package/server/channels.ts +557 -0
- package/server/chat-server.ts +33 -558
- package/server/chat.html +195 -22
- package/server/http-server.ts +3 -182
- package/server/resource-routes.ts +16 -16
- package/server/routes/inbox.ts +100 -0
- package/server/routes/pages.ts +83 -0
- package/server/routes/questions.ts +39 -0
- package/server/routes/room-actions.ts +162 -0
- package/server/routes/rooms.ts +160 -0
- package/server/startup.ts +84 -0
- package/server/sw.js +5 -2
- package/server/task-routes.ts +1 -1
- package/skills/chatroom/SKILL.md +1 -1
- package/src/center/center-client.ts +71 -1
- package/src/config/file-config.ts +29 -42
- package/src/platform/http-server.ts +182 -0
- package/src/protocol/addressing.ts +21 -0
- package/src/protocol/config.ts +58 -0
- package/src/protocol/room.ts +51 -0
- package/src/rooms/resources.ts +26 -16
- package/src/rooms/room-service.ts +2 -48
- package/src/rooms/storage.ts +329 -0
- package/src/rooms/tasks.ts +21 -5
- package/src/runtime/v2-plugin.ts +8 -2
- package/src/runtime/watch.ts +4 -3
- package/src/state/pg-database.ts +183 -0
- package/src/state/pg-migrate.ts +175 -0
- package/src/state/pg-sql.mjs +175 -0
- package/src/state/pg-worker.mjs +148 -0
- package/src/state/sqlite-announcements.ts +25 -0
- package/src/state/sqlite-database.ts +121 -0
- package/src/state/sqlite-legacy.ts +90 -0
- package/src/state/sqlite-members.ts +155 -0
- package/src/state/sqlite-messages.ts +288 -0
- package/src/state/sqlite-resources.ts +39 -0
- package/src/state/sqlite-rooms.ts +87 -0
- package/src/state/sqlite-schema.ts +80 -0
- package/src/state/sqlite-state-store.ts +33 -0
- package/src/state/sqlite-state.ts +15 -866
- package/src/state/sqlite-tasks.ts +65 -0
- package/src/state/sqlite-transaction.ts +13 -0
- package/src/state/sqlite.ts +21 -1
- package/src/state/store.ts +13 -3
- package/src/tui/app.mjs +32 -19
- package/src/tui/config.mjs +12 -4
- package/src/tui/http.mjs +16 -2
- package/src/tui/i18n.mjs +38 -0
- package/src/tui/info-app.mjs +50 -51
- package/src/tui/info-content.mjs +79 -44
- package/src/tui/join-app.mjs +9 -9
- package/src/tui/locales/en.mjs +349 -0
- package/src/tui/locales/zh-CN.mjs +349 -0
- package/src/tui/main.mjs +85 -37
- package/src/tui/markdown.mjs +3 -2
- package/src/tui/mentions.mjs +3 -1
- package/src/tui/message-app.mjs +22 -21
- package/src/tui/message-view.mjs +2 -1
- package/src/tui/palette-app.mjs +9 -8
- package/src/tui/palette.mjs +30 -24
- package/src/tui/prefs.mjs +92 -0
- package/src/tui/reminder-app.mjs +18 -17
- package/src/tui/room-settings-app.mjs +30 -28
- package/src/tui/settings-app.mjs +153 -0
- package/src/tui/ui-content.mjs +65 -86
- package/src/tui/ui-help.mjs +73 -0
- package/src/tui/ui-overlay-view.mjs +23 -7
- package/src/tui/ui-suggest.mjs +6 -5
- package/src/tui/ui-theme.mjs +37 -33
- package/src/tui/ui-view.mjs +22 -7
- package/src/tui/ui.mjs +15 -3
- package/src/web/editor.worker.ts +1 -0
- package/src/web/integration-rules-editor.ts +439 -0
- package/src/web/integration-rules.ts +22 -0
- package/src/web/integration-schema.ts +228 -0
- package/src/web/json.worker.ts +1 -0
- package/src/web/office-viewer.ts +221 -0
- package/server/assets/highlight-10.7.3.min.js +0 -1358
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Minimal protobuf codec for Feishu's pbbp2 WebSocket frame. The schema is fixed
|
|
3
|
+
* (fields 1-9), so a dependency-free codec beats shipping a protobuf runtime.
|
|
4
|
+
*/
|
|
5
|
+
export interface WsHeader {
|
|
6
|
+
key: string;
|
|
7
|
+
value: string;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export interface WsFrame {
|
|
11
|
+
SeqID?: number;
|
|
12
|
+
LogID?: number;
|
|
13
|
+
service?: number;
|
|
14
|
+
method?: number;
|
|
15
|
+
headers?: WsHeader[];
|
|
16
|
+
payload_encoding?: string;
|
|
17
|
+
payload_type?: string;
|
|
18
|
+
payload?: Uint8Array;
|
|
19
|
+
LogIDNew?: string;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
const utf8 = new TextEncoder();
|
|
23
|
+
|
|
24
|
+
function varint(value: number): number[] {
|
|
25
|
+
let rest = BigInt(Math.max(0, Math.trunc(value)));
|
|
26
|
+
const out: number[] = [];
|
|
27
|
+
while (rest > 0x7fn) {
|
|
28
|
+
out.push(Number((rest & 0x7fn) | 0x80n));
|
|
29
|
+
rest >>= 7n;
|
|
30
|
+
}
|
|
31
|
+
out.push(Number(rest));
|
|
32
|
+
return out;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
function fieldKey(field: number, wire: number): number[] {
|
|
36
|
+
return varint((field << 3) | wire);
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
function bytesField(field: number, value: Uint8Array): number[] {
|
|
40
|
+
return [...fieldKey(field, 2), ...varint(value.length), ...value];
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
function stringField(field: number, value: string): number[] {
|
|
44
|
+
return bytesField(field, utf8.encode(value));
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
function numberField(field: number, value: number): number[] {
|
|
48
|
+
return [...fieldKey(field, 0), ...varint(value)];
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function encodeFrame(frame: WsFrame): Uint8Array {
|
|
52
|
+
const out: number[] = [];
|
|
53
|
+
out.push(...numberField(1, frame.SeqID ?? 0));
|
|
54
|
+
out.push(...numberField(2, frame.LogID ?? 0));
|
|
55
|
+
out.push(...numberField(3, frame.service ?? 0));
|
|
56
|
+
out.push(...numberField(4, frame.method ?? 0));
|
|
57
|
+
for (const header of frame.headers ?? []) {
|
|
58
|
+
const entry = [...stringField(1, header.key), ...stringField(2, header.value)];
|
|
59
|
+
out.push(...bytesField(5, Uint8Array.from(entry)));
|
|
60
|
+
}
|
|
61
|
+
if (frame.payload_encoding) out.push(...stringField(6, frame.payload_encoding));
|
|
62
|
+
if (frame.payload_type) out.push(...stringField(7, frame.payload_type));
|
|
63
|
+
if (frame.payload) out.push(...bytesField(8, frame.payload));
|
|
64
|
+
if (frame.LogIDNew) out.push(...stringField(9, frame.LogIDNew));
|
|
65
|
+
return Uint8Array.from(out);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
class Reader {
|
|
69
|
+
#bytes: Uint8Array;
|
|
70
|
+
#offset = 0;
|
|
71
|
+
|
|
72
|
+
constructor(bytes: Uint8Array) {
|
|
73
|
+
this.#bytes = bytes;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
get done(): boolean {
|
|
77
|
+
return this.#offset >= this.#bytes.length;
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
varint(): bigint {
|
|
81
|
+
let value = 0n;
|
|
82
|
+
let shift = 0n;
|
|
83
|
+
for (;;) {
|
|
84
|
+
if (this.#offset >= this.#bytes.length) throw new Error("truncated varint");
|
|
85
|
+
const byte = this.#bytes[this.#offset++]!;
|
|
86
|
+
value |= BigInt(byte & 0x7f) << shift;
|
|
87
|
+
if ((byte & 0x80) === 0) return value;
|
|
88
|
+
shift += 7n;
|
|
89
|
+
if (shift > 70n) throw new Error("varint is too long");
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
bytes(length: number): Uint8Array {
|
|
94
|
+
if (this.#offset + length > this.#bytes.length) throw new Error("truncated frame field");
|
|
95
|
+
const slice = this.#bytes.subarray(this.#offset, this.#offset + length);
|
|
96
|
+
this.#offset += length;
|
|
97
|
+
return slice;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
skip(wire: number): void {
|
|
101
|
+
if (wire === 0) this.varint();
|
|
102
|
+
else if (wire === 1) this.bytes(8);
|
|
103
|
+
else if (wire === 2) this.bytes(Number(this.varint()));
|
|
104
|
+
else if (wire === 5) this.bytes(4);
|
|
105
|
+
else throw new Error(`unsupported wire type ${wire}`);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
function decodeHeader(bytes: Uint8Array): WsHeader {
|
|
110
|
+
const reader = new Reader(bytes);
|
|
111
|
+
let key = "";
|
|
112
|
+
let value = "";
|
|
113
|
+
while (!reader.done) {
|
|
114
|
+
const tag = Number(reader.varint());
|
|
115
|
+
const field = tag >> 3;
|
|
116
|
+
const wire = tag & 7;
|
|
117
|
+
if (field === 1 && wire === 2) key = new TextDecoder().decode(reader.bytes(Number(reader.varint())));
|
|
118
|
+
else if (field === 2 && wire === 2) value = new TextDecoder().decode(reader.bytes(Number(reader.varint())));
|
|
119
|
+
else reader.skip(wire);
|
|
120
|
+
}
|
|
121
|
+
return { key, value };
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
export function decodeFrame(bytes: Uint8Array): WsFrame {
|
|
125
|
+
const reader = new Reader(bytes);
|
|
126
|
+
const frame: WsFrame = {};
|
|
127
|
+
while (!reader.done) {
|
|
128
|
+
const tag = Number(reader.varint());
|
|
129
|
+
const field = tag >> 3;
|
|
130
|
+
const wire = tag & 7;
|
|
131
|
+
switch (field) {
|
|
132
|
+
case 1:
|
|
133
|
+
frame.SeqID = Number(reader.varint());
|
|
134
|
+
break;
|
|
135
|
+
case 2:
|
|
136
|
+
frame.LogID = Number(reader.varint());
|
|
137
|
+
break;
|
|
138
|
+
case 3:
|
|
139
|
+
frame.service = Number(reader.varint());
|
|
140
|
+
break;
|
|
141
|
+
case 4:
|
|
142
|
+
frame.method = Number(reader.varint());
|
|
143
|
+
break;
|
|
144
|
+
case 5:
|
|
145
|
+
frame.headers = [...(frame.headers ?? []), decodeHeader(reader.bytes(Number(reader.varint())))];
|
|
146
|
+
break;
|
|
147
|
+
case 6:
|
|
148
|
+
frame.payload_encoding = new TextDecoder().decode(reader.bytes(Number(reader.varint())));
|
|
149
|
+
break;
|
|
150
|
+
case 7:
|
|
151
|
+
frame.payload_type = new TextDecoder().decode(reader.bytes(Number(reader.varint())));
|
|
152
|
+
break;
|
|
153
|
+
case 8:
|
|
154
|
+
frame.payload = reader.bytes(Number(reader.varint()));
|
|
155
|
+
break;
|
|
156
|
+
case 9:
|
|
157
|
+
frame.LogIDNew = new TextDecoder().decode(reader.bytes(Number(reader.varint())));
|
|
158
|
+
break;
|
|
159
|
+
default:
|
|
160
|
+
reader.skip(wire);
|
|
161
|
+
break;
|
|
162
|
+
}
|
|
163
|
+
}
|
|
164
|
+
return frame;
|
|
165
|
+
}
|
|
166
|
+
|
|
167
|
+
export function headerValue(frame: WsFrame, key: string): string | undefined {
|
|
168
|
+
return frame.headers?.find((header) => header.key === key)?.value;
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
export function jsonPayload(value: unknown): Uint8Array {
|
|
172
|
+
return utf8.encode(JSON.stringify(value));
|
|
173
|
+
}
|
|
@@ -0,0 +1,128 @@
|
|
|
1
|
+
import { StringDecoder } from "node:string_decoder";
|
|
2
|
+
import { MAX_FRAME_BYTES } from "./protocol.ts";
|
|
3
|
+
|
|
4
|
+
export type FrameMessage = Record<string, unknown>;
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* Parse one newline-delimited JSON frame. Frames must be JSON objects; a
|
|
8
|
+
* scalar or array is a protocol violation.
|
|
9
|
+
*/
|
|
10
|
+
export function parseFrame(line: string): FrameMessage {
|
|
11
|
+
const text = line.trim();
|
|
12
|
+
if (!text) throw new Error("empty frame");
|
|
13
|
+
let value: unknown;
|
|
14
|
+
try {
|
|
15
|
+
value = JSON.parse(text);
|
|
16
|
+
} catch (error) {
|
|
17
|
+
throw new Error(`invalid JSON frame: ${error instanceof Error ? error.message : String(error)}`);
|
|
18
|
+
}
|
|
19
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
20
|
+
throw new Error("frame must be a JSON object");
|
|
21
|
+
}
|
|
22
|
+
return value as FrameMessage;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Feed Buffer chunks into the returned function. Complete lines are handed to
|
|
27
|
+
* onMessage; parse failures and oversize frames go to onError. An oversize
|
|
28
|
+
* frame is dropped up to its newline and never partially emitted.
|
|
29
|
+
*
|
|
30
|
+
* Byte length is checked on the raw chunk segments, before UTF-8 decoding, so
|
|
31
|
+
* a frame of multi-byte characters is measured in bytes rather than UTF-16
|
|
32
|
+
* code units.
|
|
33
|
+
*/
|
|
34
|
+
export function createFrameReader(
|
|
35
|
+
onMessage: (message: FrameMessage) => void,
|
|
36
|
+
onError: (error: Error) => void,
|
|
37
|
+
): (chunk: Buffer) => void {
|
|
38
|
+
const decoder = new StringDecoder("utf8");
|
|
39
|
+
let parts: string[] = [];
|
|
40
|
+
let bytes = 0;
|
|
41
|
+
let discarding = false;
|
|
42
|
+
|
|
43
|
+
const dispatch = (): void => {
|
|
44
|
+
const text = discarding ? "" : parts.join("");
|
|
45
|
+
const wasDiscarding = discarding;
|
|
46
|
+
parts = [];
|
|
47
|
+
bytes = 0;
|
|
48
|
+
discarding = false;
|
|
49
|
+
if (wasDiscarding || !text.trim()) return;
|
|
50
|
+
try {
|
|
51
|
+
onMessage(parseFrame(text));
|
|
52
|
+
} catch (error) {
|
|
53
|
+
onError(error instanceof Error ? error : new Error(String(error)));
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
const consume = (segment: Buffer): void => {
|
|
58
|
+
const decoded = decoder.write(segment);
|
|
59
|
+
if (discarding) return;
|
|
60
|
+
if (bytes + segment.length > MAX_FRAME_BYTES) {
|
|
61
|
+
parts = [];
|
|
62
|
+
bytes = 0;
|
|
63
|
+
discarding = true;
|
|
64
|
+
onError(new Error(`frame exceeds MAX_FRAME_BYTES (${MAX_FRAME_BYTES} bytes)`));
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
bytes += segment.length;
|
|
68
|
+
parts.push(decoded);
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
return (chunk: Buffer): void => {
|
|
72
|
+
let start = 0;
|
|
73
|
+
while (start <= chunk.length) {
|
|
74
|
+
const newline = chunk.indexOf(0x0a, start);
|
|
75
|
+
const end = newline === -1 ? chunk.length : newline;
|
|
76
|
+
consume(chunk.subarray(start, end));
|
|
77
|
+
if (newline === -1) return;
|
|
78
|
+
dispatch();
|
|
79
|
+
start = newline + 1;
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
export class AdapterGoneError extends Error {
|
|
85
|
+
readonly code = "ADAPTER_GONE";
|
|
86
|
+
|
|
87
|
+
constructor(message: string) {
|
|
88
|
+
super(message);
|
|
89
|
+
this.name = "AdapterGoneError";
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const GONE_CODES = new Set(["EPIPE", "ERR_STREAM_DESTROYED", "ERR_STREAM_WRITE_AFTER_END"]);
|
|
94
|
+
|
|
95
|
+
function writeError(error: unknown): Error {
|
|
96
|
+
const code = (error as { code?: string } | null)?.code;
|
|
97
|
+
if (code && GONE_CODES.has(code)) return new AdapterGoneError(`adapter stream is gone (${code})`);
|
|
98
|
+
return error instanceof Error ? error : new Error(String(error));
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** Write one JSON line. Protocol streams only carry JSON objects. */
|
|
102
|
+
export function writeFrame(stream: NodeJS.WritableStream, value: unknown): Promise<void> {
|
|
103
|
+
if (!value || typeof value !== "object" || Array.isArray(value)) {
|
|
104
|
+
throw new TypeError("frame must be a JSON object");
|
|
105
|
+
}
|
|
106
|
+
const writable = stream as NodeJS.WritableStream & {
|
|
107
|
+
destroyed?: boolean;
|
|
108
|
+
writableEnded?: boolean;
|
|
109
|
+
};
|
|
110
|
+
if (writable.destroyed || writable.writableEnded) {
|
|
111
|
+
throw new AdapterGoneError("adapter stream is closed");
|
|
112
|
+
}
|
|
113
|
+
const line = `${JSON.stringify(value)}\n`;
|
|
114
|
+
if (Buffer.byteLength(line, "utf8") > MAX_FRAME_BYTES) {
|
|
115
|
+
throw new RangeError(`frame exceeds MAX_FRAME_BYTES (${MAX_FRAME_BYTES} bytes)`);
|
|
116
|
+
}
|
|
117
|
+
return new Promise((resolve, reject) => {
|
|
118
|
+
const fail = (error: unknown): void => reject(writeError(error));
|
|
119
|
+
try {
|
|
120
|
+
stream.write(line, (error?: Error | null) => {
|
|
121
|
+
if (error) fail(error);
|
|
122
|
+
else resolve();
|
|
123
|
+
});
|
|
124
|
+
} catch (error) {
|
|
125
|
+
fail(error);
|
|
126
|
+
}
|
|
127
|
+
});
|
|
128
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import type { ChannelConfig, RuleTarget } from "./config.ts";
|
|
2
|
+
import type { Endpoint } from "./protocol.ts";
|
|
3
|
+
|
|
4
|
+
export function parseTarget(value: string): Endpoint | undefined {
|
|
5
|
+
const parts = value.split("/");
|
|
6
|
+
if (parts.length !== 3) return undefined;
|
|
7
|
+
const [platform, accountId, roomId] = parts;
|
|
8
|
+
if (!platform || !accountId || !roomId) return undefined;
|
|
9
|
+
return { platform, accountId, roomId };
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export function startFailureReason(accountId: string, error: unknown): string {
|
|
13
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
14
|
+
const prefix = `adapter ${accountId} failed to start: `;
|
|
15
|
+
return message.startsWith(prefix) ? message.slice(prefix.length) : message;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
export function validateTargets(config: ChannelConfig): void {
|
|
19
|
+
const accounts = new Map(config.accounts.map((account) => [account.id, account]));
|
|
20
|
+
for (const rule of config.rules) {
|
|
21
|
+
for (const target of rule.to) {
|
|
22
|
+
const account = accounts.get(target.account);
|
|
23
|
+
if (!account) {
|
|
24
|
+
throw new Error(`config: rule "${rule.id}" targets unknown account "${target.account}"`);
|
|
25
|
+
}
|
|
26
|
+
if (account.platform !== target.platform) {
|
|
27
|
+
throw new Error(
|
|
28
|
+
`config: rule "${rule.id}" targets platform "${target.platform}" but account "${target.account}" is "${account.platform}"`,
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
for (const route of config.inbound) {
|
|
34
|
+
const account = accounts.get(route.from.account);
|
|
35
|
+
if (!account) {
|
|
36
|
+
throw new Error(`config: inbound route sources unknown account "${route.from.account}"`);
|
|
37
|
+
}
|
|
38
|
+
if (account.platform !== route.from.platform) {
|
|
39
|
+
throw new Error(
|
|
40
|
+
`config: inbound route sources platform "${route.from.platform}" but account "${route.from.account}" is "${account.platform}"`,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
export function toEndpoint(config: ChannelConfig, ruleId: string, target: RuleTarget): Endpoint {
|
|
47
|
+
const account = config.accounts.find((item) => item.id === target.account);
|
|
48
|
+
if (!account) {
|
|
49
|
+
throw new Error(`config: rule "${ruleId}" targets unknown account "${target.account}"`);
|
|
50
|
+
}
|
|
51
|
+
return { platform: target.platform, accountId: account.id, roomId: target.room };
|
|
52
|
+
}
|