@alexsun-top/opencode-chat-room 0.8.5 → 0.8.7
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 +23 -2
- package/README.md +1 -1
- package/README.zh-CN.md +1 -1
- package/cli.ts +19 -0
- package/dist/cli.js +4058 -1531
- package/package.json +7 -1
- package/packages/chat-room-channeld/README.md +13 -8
- package/packages/chat-room-channeld/src/adapter.ts +52 -0
- package/packages/chat-room-channeld/src/config.ts +6 -1
- package/packages/chat-room-channeld/src/delivery.ts +0 -4
- package/packages/chat-room-channeld/src/host.ts +66 -44
- package/packages/chat-room-channeld/src/inbound.ts +14 -1
- package/packages/chat-room-channeld/src/pg/database.ts +141 -49
- package/packages/chat-room-channeld/src/pg/worker.mjs +46 -5
- package/packages/chat-room-channeld/src/pg/worker.mts +41 -6
- package/packages/chat-room-channeld/src/protocol.ts +38 -1
- package/packages/chat-room-channeld/src/state-rows.ts +132 -0
- package/packages/chat-room-channeld/src/state.ts +129 -157
- package/packages/chat-room-channeld/src/stats.ts +4 -0
- package/plugin.ts +26 -5
- package/server/app.ts +15 -0
- package/server/assets/base.css +22 -2
- package/server/assets/chat/admin.js +13 -0
- package/server/assets/chat/announcements.js +76 -34
- package/server/assets/chat/confirm-dialog.js +3 -0
- package/server/assets/chat/create-room-dialog.js +7 -0
- package/server/assets/chat/diagram-viewer.js +4 -8
- package/server/assets/chat/doc-preview.js +70 -30
- package/server/assets/chat/editor.js +38 -3
- package/server/assets/chat/events.js +11 -2
- package/server/assets/chat/files.js +28 -26
- package/server/assets/chat/history-loader.js +31 -15
- package/server/assets/chat/integration-editor.js +25 -1
- package/server/assets/chat/integrations.js +107 -15
- package/server/assets/chat/join-requests.js +39 -1
- package/server/assets/chat/locales/de.js +5 -3
- package/server/assets/chat/locales/es.js +5 -3
- package/server/assets/chat/locales/fr.js +5 -3
- package/server/assets/chat/locales/it.js +5 -3
- package/server/assets/chat/locales/ja.js +5 -3
- package/server/assets/chat/locales/ko.js +5 -3
- package/server/assets/chat/markdown-footnotes.js +19 -8
- package/server/assets/chat/markdown.js +0 -7
- package/server/assets/chat/members.js +31 -19
- package/server/assets/chat/message-cache.js +8 -2
- package/server/assets/chat/message-polling.js +8 -2
- package/server/assets/chat/message-window.js +57 -0
- package/server/assets/chat/messages.js +79 -40
- package/server/assets/chat/operator.js +624 -0
- package/server/assets/chat/palette.js +2 -7
- package/server/assets/chat/panels.js +31 -8
- package/server/assets/chat/questions.js +72 -56
- package/server/assets/chat/recall.js +14 -5
- package/server/assets/chat/receipts.js +63 -7
- package/server/assets/chat/refresh-timing.js +6 -0
- package/server/assets/chat/reminders.js +44 -13
- package/server/assets/chat/replies.js +33 -22
- package/server/assets/chat/resource-format.js +4 -1
- package/server/assets/chat/room-list.js +202 -3
- package/server/assets/chat/room-watcher.js +3 -1
- package/server/assets/chat/rooms.js +22 -3
- package/server/assets/chat/settings.js +33 -3
- package/server/assets/chat/state.js +21 -7
- package/server/assets/chat/tasks.js +33 -11
- package/server/assets/chat/window-controls.js +25 -16
- package/server/assets/code-highlight.css +0 -13
- package/server/assets/conversation.css +30 -2
- package/server/assets/doc-preview.css +3 -0
- package/server/assets/files.css +4 -0
- package/server/assets/monaco/integration-rules.js +1 -1
- package/server/assets/responsive.css +12 -2
- package/server/assets/settings.css +3 -1
- package/server/assets/tasks.css +6 -1
- package/server/assets/window-controls.css +3 -1
- package/server/channels.ts +24 -6
- package/server/chat-server.ts +4 -0
- package/server/chat.html +7 -2
- package/server/inbox.ts +3 -1
- package/server/join-requests.ts +8 -1
- package/server/mcp-http.ts +88 -0
- package/server/member-routes.ts +16 -2
- package/server/newrooms.ts +2 -13
- package/server/operator-auth.ts +21 -0
- package/server/operator-routes.ts +150 -0
- package/server/operator-state.ts +64 -0
- package/server/operator.css +139 -0
- package/server/room-owner.ts +17 -0
- package/server/routes/inbox.ts +15 -5
- package/server/routes/pages.ts +12 -2
- package/server/routes/room-actions.ts +4 -3
- package/server/system.html +109 -0
- package/server/wake.ts +1 -1
- package/skills/chatroom/SKILL.md +19 -3
- package/src/acp/client.ts +254 -0
- package/src/acp/protocol.ts +300 -0
- package/src/acp/watch.ts +498 -0
- package/src/agent/agent-receipts.ts +87 -5
- package/src/center/center-client.ts +18 -1
- package/src/center/center-inbox.ts +54 -7
- package/src/center/center-wake.ts +57 -3
- package/src/center/forward-client.ts +48 -0
- package/src/center/mentions.ts +16 -3
- package/src/center/notification-delay.ts +139 -0
- package/src/center/queue.ts +3 -0
- package/src/config/file-config.ts +25 -0
- package/src/mcp/server.ts +407 -0
- package/src/protocol/config.ts +7 -0
- package/src/runtime/plugin-notifications.ts +43 -9
- package/src/runtime/v2-plugin.ts +20 -4
- package/src/runtime/v2-receiver.ts +10 -2
- package/src/runtime/watch.ts +5 -1
- package/src/state/pg-database.ts +133 -49
- package/src/state/pg-worker.mjs +46 -5
- package/src/state/pg-worker.mts +41 -6
- package/src/state/receiver-state.ts +47 -0
- package/src/state/sqlite-database.ts +6 -2
- package/src/state/sqlite-members.ts +48 -4
- package/src/state/sqlite-messages.ts +25 -12
- package/src/state/sqlite-schema.ts +4 -1
- package/src/state/sqlite-state.ts +1 -1
- package/src/state/store.ts +10 -4
- package/src/tui/app.mjs +57 -10
- package/src/tui/app.mts +54 -11
- package/src/tui/room-settings-app.mjs +27 -9
- package/src/tui/room-settings-app.mts +26 -9
- package/src/web/integration-rules-editor.ts +25 -2
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alexsun-top/opencode-chat-room",
|
|
3
|
-
"version": "0.8.
|
|
3
|
+
"version": "0.8.7",
|
|
4
4
|
"description": "A chat-room server, web client, terminal client, and OpenCode/Codex/dsh plugins.",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"repository": {
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"server/routes",
|
|
42
42
|
"server/app.ts",
|
|
43
43
|
"server/channels.ts",
|
|
44
|
+
"server/mcp-http.ts",
|
|
44
45
|
"server/channel-attachments.ts",
|
|
45
46
|
"server/channel-settings.ts",
|
|
46
47
|
"server/channel-integration.ts",
|
|
@@ -52,6 +53,9 @@
|
|
|
52
53
|
"server/message-recall-routes.ts",
|
|
53
54
|
"server/room-payload.ts",
|
|
54
55
|
"server/room-owner.ts",
|
|
56
|
+
"server/operator-auth.ts",
|
|
57
|
+
"server/operator-routes.ts",
|
|
58
|
+
"server/operator-state.ts",
|
|
55
59
|
"server/newrooms.ts",
|
|
56
60
|
"server/join-requests.ts",
|
|
57
61
|
"server/room-join.ts",
|
|
@@ -65,6 +69,8 @@
|
|
|
65
69
|
"server/room-reminders.ts",
|
|
66
70
|
"server/wake.ts",
|
|
67
71
|
"server/chat.html",
|
|
72
|
+
"server/system.html",
|
|
73
|
+
"server/operator.css",
|
|
68
74
|
"server/manifest.webmanifest",
|
|
69
75
|
"server/sw.js",
|
|
70
76
|
"server/assets",
|
|
@@ -224,19 +224,24 @@ The child process reads:
|
|
|
224
224
|
- `FEISHU_HTTP_TIMEOUT_MS` (optional, default `10000`)
|
|
225
225
|
- `FEISHU_CARD_VERSION` (optional, `1.0` default or `2.0`)
|
|
226
226
|
- `FEISHU_INBOUND` (optional, `1` enables the event long connection)
|
|
227
|
-
- `FEISHU_INBOUND_CHATS` (
|
|
228
|
-
|
|
227
|
+
- `FEISHU_INBOUND_CHATS` (comma-separated group `chat_id` allowlist)
|
|
228
|
+
- `FEISHU_INBOUND_DM_CHAT` and `FEISHU_INBOUND_DM_OPEN_ID` (set both with
|
|
229
|
+
`FEISHU_INBOUND=1` to receive only one private chat from one app-scoped user;
|
|
230
|
+
this ignores the group allowlist)
|
|
229
231
|
|
|
230
232
|
Set these in the host environment; the host passes its environment to adapter
|
|
231
233
|
child processes.
|
|
232
234
|
|
|
233
235
|
Inbound requires the app to use long-connection event subscription in the Feishu
|
|
234
|
-
developer console and to subscribe `im.message.receive_v1
|
|
235
|
-
|
|
236
|
+
developer console and to subscribe `im.message.receive_v1`. The bot must be in
|
|
237
|
+
each allowlisted group; private chat requires the app's bot DM read permission
|
|
238
|
+
(`im:message.p2p_msg:readonly` or `im:message.p2p_msg`). Add a server `inbound`
|
|
239
|
+
route for the exact source account and chat to relay it into ChatRoom. The
|
|
240
|
+
adapter obtains the WebSocket URL from
|
|
236
241
|
`POST /callback/ws/endpoint`, pings, reassembles fragmented frames, and
|
|
237
|
-
acknowledges only after the host commits the ingest.
|
|
238
|
-
messages from
|
|
239
|
-
other chats, other event types, other apps, and unsupported message types are
|
|
242
|
+
acknowledges only after the host commits the ingest. Supported `text`, `post`,
|
|
243
|
+
`image`, and `file` messages must come from an allowed human sender and chat.
|
|
244
|
+
Bot/self senders, other chats, other event types, other apps, and unsupported message types are
|
|
240
245
|
acknowledged without ingest. A long-connection start failure logs a warning,
|
|
241
246
|
keeps the account started for outbound sends, and reports a sanitized
|
|
242
247
|
`inbound failed: ...` in the account status; outbound forwarding keeps working.
|
|
@@ -340,4 +345,4 @@ same terminal result event and message.
|
|
|
340
345
|
- no full backpressure (high-water marks, pausing ingress, disk quotas) — only per-account queue/process isolation and `rate_limited` backoff;
|
|
341
346
|
- no attachments, no rule UI, no multi-tenant permission model, no exactly-once
|
|
342
347
|
guarantee (inbound room notifications are idempotent per event and route, but
|
|
343
|
-
a crash between the ACK and Feishu's next retry can still lose an event).
|
|
348
|
+
a crash between the ACK and Feishu's next retry can still lose an event).
|
|
@@ -13,9 +13,11 @@ import {
|
|
|
13
13
|
rpcError,
|
|
14
14
|
rpcRequest,
|
|
15
15
|
rpcResult,
|
|
16
|
+
FEATURE_VERSION_PATTERN,
|
|
16
17
|
type Endpoint,
|
|
17
18
|
type AccountField,
|
|
18
19
|
type AdapterCapabilities,
|
|
20
|
+
type FeatureDeclaration,
|
|
19
21
|
type IngestEvent,
|
|
20
22
|
type JsonRpcId,
|
|
21
23
|
type MessageRef,
|
|
@@ -157,9 +159,50 @@ function readCapabilities(result: unknown, accountId: string): AdapterCapabiliti
|
|
|
157
159
|
const value = (send as Record<string, unknown>).idempotency;
|
|
158
160
|
if (typeof value === "string" && value.trim()) capabilities.send = { idempotency: value.trim() };
|
|
159
161
|
}
|
|
162
|
+
const features = readFeatures(entry.features);
|
|
163
|
+
if (features) capabilities.features = features;
|
|
160
164
|
return Object.keys(capabilities).length ? capabilities : undefined;
|
|
161
165
|
}
|
|
162
166
|
|
|
167
|
+
const FEATURE_ID_PATTERN = /^[a-z][a-z0-9]*(?:[.-][a-z0-9]+)*$/;
|
|
168
|
+
|
|
169
|
+
function readFeatures(raw: unknown): Record<string, FeatureDeclaration[]> | undefined {
|
|
170
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
|
|
171
|
+
const features: Record<string, FeatureDeclaration[]> = {};
|
|
172
|
+
for (const [id, value] of Object.entries(raw as Record<string, unknown>)) {
|
|
173
|
+
if (!FEATURE_ID_PATTERN.test(id)) continue;
|
|
174
|
+
const declared = (Array.isArray(value) ? value : [value])
|
|
175
|
+
.map(readFeature)
|
|
176
|
+
.filter((feature): feature is FeatureDeclaration => feature !== undefined);
|
|
177
|
+
if (declared.length) features[id] = declared;
|
|
178
|
+
}
|
|
179
|
+
return Object.keys(features).length ? features : undefined;
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
function readFeature(raw: unknown): FeatureDeclaration | undefined {
|
|
183
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
|
|
184
|
+
const entry = raw as Record<string, unknown>;
|
|
185
|
+
if (typeof entry.version !== "string" || !FEATURE_VERSION_PATTERN.test(entry.version)) return undefined;
|
|
186
|
+
const feature: FeatureDeclaration = { version: entry.version };
|
|
187
|
+
if (entry.direction === "receive" || entry.direction === "send" || entry.direction === "both") {
|
|
188
|
+
feature.direction = entry.direction;
|
|
189
|
+
}
|
|
190
|
+
const limits = readLimits(entry.limits);
|
|
191
|
+
if (limits) feature.limits = limits;
|
|
192
|
+
return feature;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
function readLimits(raw: unknown): Record<string, string | number | boolean> | undefined {
|
|
196
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
|
|
197
|
+
const limits: Record<string, string | number | boolean> = {};
|
|
198
|
+
for (const [key, value] of Object.entries(raw as Record<string, unknown>)) {
|
|
199
|
+
if (typeof value === "string" || typeof value === "number" || typeof value === "boolean") {
|
|
200
|
+
limits[key] = value;
|
|
201
|
+
}
|
|
202
|
+
}
|
|
203
|
+
return Object.keys(limits).length ? limits : undefined;
|
|
204
|
+
}
|
|
205
|
+
|
|
163
206
|
export class AdapterProcess {
|
|
164
207
|
readonly #account: AccountConfig;
|
|
165
208
|
readonly #onIngest: AdapterContext["onIngest"];
|
|
@@ -244,6 +287,15 @@ export class AdapterProcess {
|
|
|
244
287
|
log.info("adapter started", { account: this.#account.id });
|
|
245
288
|
}
|
|
246
289
|
|
|
290
|
+
/**
|
|
291
|
+
* Platform operation declared by the adapter (history, members, ...).
|
|
292
|
+
* The adapter rejects ops it did not declare or lacks permission for.
|
|
293
|
+
*/
|
|
294
|
+
async op(op: string, params: Record<string, unknown> = {}): Promise<unknown> {
|
|
295
|
+
if (!this.#started) throw new Error(`adapter ${this.#account.id} is not started`);
|
|
296
|
+
return this.#call("op", { op, ...params }, this.#sendTimeoutMs);
|
|
297
|
+
}
|
|
298
|
+
|
|
247
299
|
/**
|
|
248
300
|
* Read-only handshake used when a plugin is uploaded: initialize only, then
|
|
249
301
|
* kill. It does not start the account and does not need credentials.
|
|
@@ -47,6 +47,8 @@ export interface ChannelConfig {
|
|
|
47
47
|
rules: RuleConfig[];
|
|
48
48
|
/** Room notifications for inbound events; never matched by outbound rules. */
|
|
49
49
|
inbound: InboundRoute[];
|
|
50
|
+
/** Optional room that receives inbound events with no matching route. */
|
|
51
|
+
inboundFallback?: string;
|
|
50
52
|
}
|
|
51
53
|
|
|
52
54
|
type RecordValue = Record<string, unknown>;
|
|
@@ -244,7 +246,10 @@ export function parseConfig(value: unknown): ChannelConfig {
|
|
|
244
246
|
if (ruleKeys.has(key)) throw new ConfigError(`config: duplicate rule "${rule.id}" version ${rule.version}`);
|
|
245
247
|
ruleKeys.add(key);
|
|
246
248
|
}
|
|
247
|
-
|
|
249
|
+
const inboundFallback = raw.inboundFallback === undefined
|
|
250
|
+
? undefined
|
|
251
|
+
: stringValue(raw.inboundFallback, "inboundFallback");
|
|
252
|
+
return { stateFile, accounts, rules, inbound, ...(inboundFallback ? { inboundFallback } : {}) };
|
|
248
253
|
}
|
|
249
254
|
|
|
250
255
|
/** Load a config file. YAML parsing also accepts JSON documents. */
|
|
@@ -67,10 +67,6 @@ export function applyReceipt(
|
|
|
67
67
|
|
|
68
68
|
switch (receipt.state) {
|
|
69
69
|
case "sent": {
|
|
70
|
-
state.markDelivery(row.deliveryId, "sent", {
|
|
71
|
-
providerMessage: providerMessage ?? null,
|
|
72
|
-
lastError: null,
|
|
73
|
-
});
|
|
74
70
|
const event = state.getEvent(row.eventId);
|
|
75
71
|
const echo = receipt.providerMessage?.nativeMessageId && receipt.providerMessage.endpoint
|
|
76
72
|
? {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import type { AccountConfig, ChannelConfig
|
|
1
|
+
import type { AccountConfig, ChannelConfig } from "./config.ts";
|
|
2
2
|
import { AdapterProcess, type SendReceipt } from "./adapter.ts";
|
|
3
3
|
import { applyReceipt, notifyDeliveryResult, type DeliveryResultHandler } from "./delivery.ts";
|
|
4
4
|
import { parseTarget, startFailureReason, toEndpoint, validateTargets } from "./host-helpers.ts";
|
|
@@ -8,7 +8,7 @@ import type { AdapterCapabilities, Endpoint, IngestEvent, MessageRef } from "./p
|
|
|
8
8
|
import { matchRules, planPayload, sameEndpoint, targetKey } from "./rules.ts";
|
|
9
9
|
import type { ChannelAccountRuntime } from "./stats.ts";
|
|
10
10
|
import { sanitizeAccountError } from "./stats.ts";
|
|
11
|
-
import { openState, type ChannelState, type DeliveryRow } from "./state.ts";
|
|
11
|
+
import { openState, type ChannelState, type DeliveryRow, type PlannedDelivery } from "./state.ts";
|
|
12
12
|
|
|
13
13
|
export type { IngestResult } from "./ingest-gate.ts";
|
|
14
14
|
|
|
@@ -23,6 +23,8 @@ export interface AdapterHandle {
|
|
|
23
23
|
target: Endpoint,
|
|
24
24
|
payload: unknown,
|
|
25
25
|
): Promise<SendReceipt>;
|
|
26
|
+
/** Optional platform operation declared through capabilities. */
|
|
27
|
+
op?(op: string, params: Record<string, unknown>): Promise<unknown>;
|
|
26
28
|
stop(): Promise<void>;
|
|
27
29
|
}
|
|
28
30
|
|
|
@@ -115,6 +117,14 @@ export class ChannelHost {
|
|
|
115
117
|
}
|
|
116
118
|
|
|
117
119
|
/** Read-only view for routes: unknown ids or platform mismatches are missing. */
|
|
120
|
+
/** Run a capability-declared platform operation on a started adapter. */
|
|
121
|
+
async runOp(accountId: string, op: string, params: Record<string, unknown> = {}): Promise<unknown> {
|
|
122
|
+
const adapter = this.#adapters.get(accountId);
|
|
123
|
+
if (!adapter) throw new Error(`adapter ${accountId} is not running`);
|
|
124
|
+
if (!adapter.op) throw new Error(`adapter ${accountId} does not support operations`);
|
|
125
|
+
return adapter.op(op, params);
|
|
126
|
+
}
|
|
127
|
+
|
|
118
128
|
targetStatus(platform: string, accountId: string): "ready" | "disabled" | "missing" {
|
|
119
129
|
const account = this.#config.accounts.find((item) => item.id === accountId);
|
|
120
130
|
if (!account || account.platform !== platform) return "missing";
|
|
@@ -213,12 +223,9 @@ export class ChannelHost {
|
|
|
213
223
|
.map((account) => `${account.platform}/${account.id}`);
|
|
214
224
|
const recovered = state.recoverInFlightDeliveries(idempotentKeys);
|
|
215
225
|
if (recovered > 0) {
|
|
226
|
+
// Every started account already owns one delivery loop, so recovery only
|
|
227
|
+
// makes rows pending; scheduling again would create a second loop.
|
|
216
228
|
log.warn(`recovered ${recovered} stale in-flight deliveries`);
|
|
217
|
-
for (const account of this.#config.accounts) {
|
|
218
|
-
if (!this.#disabled.has(account.id) && idempotentKeys.includes(`${account.platform}/${account.id}`)) {
|
|
219
|
-
this.#schedule(account.id);
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
229
|
}
|
|
223
230
|
|
|
224
231
|
const usable = started.filter((accountId) => !this.#disabled.has(accountId));
|
|
@@ -264,7 +271,14 @@ export class ChannelHost {
|
|
|
264
271
|
}
|
|
265
272
|
}
|
|
266
273
|
|
|
267
|
-
|
|
274
|
+
// The event and its frozen outbound plan commit together. Classification and
|
|
275
|
+
// planning run only for a new insert, on the stored event with its ingressSeq;
|
|
276
|
+
// the frozen decision also governs whether a consumer error rolls the event back.
|
|
277
|
+
let notificationOnly = false;
|
|
278
|
+
const { result, created } = state.commitIngestWithPlan(event, (storedEvent) => {
|
|
279
|
+
notificationOnly = this.#notificationOnly?.(storedEvent) ?? false;
|
|
280
|
+
return notificationOnly ? [] : this.#planDeliveries(storedEvent);
|
|
281
|
+
});
|
|
268
282
|
if (result.duplicate) {
|
|
269
283
|
log.info("duplicate ingest skipped", { event: result.existingEventId });
|
|
270
284
|
return { duplicate: true, eventId: result.existingEventId };
|
|
@@ -279,7 +293,7 @@ export class ChannelHost {
|
|
|
279
293
|
const message = error instanceof Error ? error.message : String(error);
|
|
280
294
|
// An inbound notification owns the ACK: roll back and fail the ingest so
|
|
281
295
|
// the provider retries. Other consumer failures stay contained as before.
|
|
282
|
-
if (
|
|
296
|
+
if (notificationOnly) {
|
|
283
297
|
state.deleteIngestEvent(stored.eventId);
|
|
284
298
|
log.error(`inbound commit rolled back: ${message}`, { event: stored.eventId });
|
|
285
299
|
throw error;
|
|
@@ -288,58 +302,64 @@ export class ChannelHost {
|
|
|
288
302
|
}
|
|
289
303
|
}
|
|
290
304
|
|
|
291
|
-
if (
|
|
305
|
+
if (notificationOnly) {
|
|
292
306
|
log.info("inbound notification only; outbound rules skipped", { event: stored.eventId });
|
|
293
307
|
return { duplicate: false, eventId: stored.eventId };
|
|
294
308
|
}
|
|
295
309
|
|
|
296
|
-
|
|
297
|
-
const
|
|
310
|
+
for (const delivery of created) {
|
|
311
|
+
const endpoint = parseTarget(delivery.target);
|
|
312
|
+
if (endpoint && this.#disabled.has(endpoint.accountId)) {
|
|
313
|
+
state.markDelivery(delivery.deliveryId, "paused", {
|
|
314
|
+
lastError: "account disabled until restart",
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
log.info(`delivery enqueued rule=${delivery.ruleId}@${delivery.ruleVersion}`, {
|
|
318
|
+
delivery: delivery.deliveryId,
|
|
319
|
+
event: stored.eventId,
|
|
320
|
+
});
|
|
321
|
+
}
|
|
322
|
+
return { duplicate: false, eventId: stored.eventId };
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/** Freeze matching rules into delivery plans; runs inside the ingest transaction. */
|
|
326
|
+
#planDeliveries(event: IngestEvent): PlannedDelivery[] {
|
|
327
|
+
if (event.kind === "message.forward_requested") {
|
|
328
|
+
const selected = event.selectedMessage;
|
|
298
329
|
const reason = !selected
|
|
299
330
|
? "selectedMessage is required"
|
|
300
|
-
: !sameEndpoint(selected.endpoint,
|
|
331
|
+
: !sameEndpoint(selected.endpoint, event.source)
|
|
301
332
|
? "selectedMessage is outside the source room"
|
|
302
333
|
: undefined;
|
|
303
334
|
if (reason) {
|
|
304
|
-
log.warn(`forward rejected: ${reason}`, { event:
|
|
305
|
-
return
|
|
335
|
+
log.warn(`forward rejected: ${reason}`, { event: event.eventId });
|
|
336
|
+
return [];
|
|
306
337
|
}
|
|
307
338
|
}
|
|
308
339
|
|
|
309
|
-
const requestedTarget =
|
|
310
|
-
|
|
311
|
-
|
|
312
|
-
const payloads: string[] = [];
|
|
340
|
+
const requestedTarget = event.kind === "message.forward_requested" ? event.requestedTarget : undefined;
|
|
341
|
+
const planned: PlannedDelivery[] = [];
|
|
342
|
+
for (const rule of matchRules(this.#config, event)) {
|
|
313
343
|
for (const target of rule.to) {
|
|
314
344
|
const endpoint = toEndpoint(this.#config, rule.id, target);
|
|
315
345
|
if (requestedTarget && !sameEndpoint(endpoint, requestedTarget)) continue;
|
|
316
|
-
const plan = planPayload(rule,
|
|
346
|
+
const plan = planPayload(rule, event, endpoint);
|
|
317
347
|
if ("dropped" in plan) {
|
|
318
348
|
log.warn(
|
|
319
349
|
`rule ${rule.id}@${rule.version} dropped delivery to ${targetKey(endpoint)}: ${plan.dropped}`,
|
|
320
|
-
{ event:
|
|
350
|
+
{ event: event.eventId },
|
|
321
351
|
);
|
|
322
352
|
continue;
|
|
323
353
|
}
|
|
324
|
-
|
|
325
|
-
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
for (const delivery of created) {
|
|
330
|
-
const endpoint = parseTarget(delivery.target);
|
|
331
|
-
if (endpoint && this.#disabled.has(endpoint.accountId)) {
|
|
332
|
-
state.markDelivery(delivery.deliveryId, "paused", {
|
|
333
|
-
lastError: "account disabled until restart",
|
|
334
|
-
});
|
|
335
|
-
}
|
|
336
|
-
log.info(`delivery enqueued rule=${rule.id}@${rule.version}`, {
|
|
337
|
-
delivery: delivery.deliveryId,
|
|
338
|
-
event: stored.eventId,
|
|
354
|
+
planned.push({
|
|
355
|
+
ruleId: rule.id,
|
|
356
|
+
ruleVersion: rule.version,
|
|
357
|
+
target: targetKey(endpoint),
|
|
358
|
+
payload: JSON.stringify(plan.payload),
|
|
339
359
|
});
|
|
340
360
|
}
|
|
341
361
|
}
|
|
342
|
-
return
|
|
362
|
+
return planned;
|
|
343
363
|
}
|
|
344
364
|
|
|
345
365
|
async stop(options: { abortInFlight?: boolean } = {}): Promise<void> {
|
|
@@ -375,6 +395,7 @@ export class ChannelHost {
|
|
|
375
395
|
|
|
376
396
|
#schedule(accountId: string): void {
|
|
377
397
|
if (!this.#running || this.#disabled.has(accountId)) return;
|
|
398
|
+
if (this.#timers.has(accountId)) return;
|
|
378
399
|
const timer = setTimeout(() => {
|
|
379
400
|
this.#timers.delete(accountId);
|
|
380
401
|
if (!this.#running) return;
|
|
@@ -508,13 +529,12 @@ export class ChannelHost {
|
|
|
508
529
|
if (this.#disabled.has(accountId)) return;
|
|
509
530
|
const account = this.#config.accounts.find((item) => item.id === accountId);
|
|
510
531
|
if (!account) return;
|
|
511
|
-
const key = `${account.platform}/${account.id}`;
|
|
512
532
|
if (status.inbound === "failed") {
|
|
513
533
|
const detail = sanitizeAccountError(`inbound failed: ${status.detail ?? "unknown"}`);
|
|
514
|
-
this.#
|
|
534
|
+
this.#setAccountState(account, "started", detail);
|
|
515
535
|
log.warn(detail, { account: accountId });
|
|
516
536
|
} else if (status.inbound === "ready") {
|
|
517
|
-
this.#
|
|
537
|
+
this.#setAccountState(account, "started");
|
|
518
538
|
}
|
|
519
539
|
}
|
|
520
540
|
|
|
@@ -523,10 +543,12 @@ export class ChannelHost {
|
|
|
523
543
|
state: ChannelAccountRuntime["state"],
|
|
524
544
|
lastError?: string,
|
|
525
545
|
): void {
|
|
526
|
-
this.#
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
546
|
+
const features = this.#adapters.get(account.id)?.capabilities?.features;
|
|
547
|
+
this.#accountStates.set(`${account.platform}/${account.id}`, {
|
|
548
|
+
state,
|
|
549
|
+
...(lastError ? { lastError } : {}),
|
|
550
|
+
...(features ? { features } : {}),
|
|
551
|
+
});
|
|
530
552
|
}
|
|
531
553
|
|
|
532
554
|
#disableAccount(accountId: string, reason: string): void {
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { ChannelConfig, InboundRoute } from "./config.ts";
|
|
2
2
|
import type { IngestEvent } from "./protocol.ts";
|
|
3
3
|
|
|
4
|
-
const MAX_RELAY_TEXT =
|
|
4
|
+
const MAX_RELAY_TEXT = 5000;
|
|
5
5
|
|
|
6
6
|
/** Inbound routes that turn one committed external event into a room notification. */
|
|
7
7
|
export function matchInboundRoutes(config: ChannelConfig, event: IngestEvent): InboundRoute[] {
|
|
@@ -13,6 +13,19 @@ export function matchInboundRoutes(config: ChannelConfig, event: IngestEvent): I
|
|
|
13
13
|
);
|
|
14
14
|
}
|
|
15
15
|
|
|
16
|
+
/** Inbound message kinds; action events such as message.forward_requested are internal. */
|
|
17
|
+
const INBOUND_KINDS = new Set(["message.created", "message"]);
|
|
18
|
+
|
|
19
|
+
/** Matched routes, or one fallback route for an unmatched inbound source. */
|
|
20
|
+
export function inboundRoutes(config: ChannelConfig, event: IngestEvent): InboundRoute[] {
|
|
21
|
+
const matched = matchInboundRoutes(config, event);
|
|
22
|
+
if (matched.length > 0 || !config.inboundFallback || !INBOUND_KINDS.has(event.kind)) return matched;
|
|
23
|
+
return [{
|
|
24
|
+
from: { platform: event.source.platform, account: event.source.accountId, room: event.source.roomId },
|
|
25
|
+
to: { room: config.inboundFallback },
|
|
26
|
+
}];
|
|
27
|
+
}
|
|
28
|
+
|
|
16
29
|
export function platformLabel(platform: string): string {
|
|
17
30
|
const value = platform.trim();
|
|
18
31
|
return value ? value[0]!.toUpperCase() + value.slice(1) : "Channel";
|