@alexsun-top/opencode-chat-room 0.7.8 → 0.8.0

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.
Files changed (67) hide show
  1. package/CHANGELOG.md +14 -1
  2. package/LICENSE +21 -0
  3. package/README.md +45 -36
  4. package/README.zh-CN.md +50 -41
  5. package/dist/cli.js +2629 -350
  6. package/package.json +13 -3
  7. package/packages/chat-room-channeld/README.md +242 -0
  8. package/packages/chat-room-channeld/dist/feishu-adapter.js +499 -0
  9. package/packages/chat-room-channeld/dist/mock-adapter.js +295 -0
  10. package/packages/chat-room-channeld/src/adapter.ts +475 -0
  11. package/packages/chat-room-channeld/src/cli.ts +124 -0
  12. package/packages/chat-room-channeld/src/config.ts +207 -0
  13. package/packages/chat-room-channeld/src/delivery.ts +158 -0
  14. package/packages/chat-room-channeld/src/feishu-adapter.ts +473 -0
  15. package/packages/chat-room-channeld/src/framing.ts +128 -0
  16. package/packages/chat-room-channeld/src/host-helpers.ts +41 -0
  17. package/packages/chat-room-channeld/src/host.ts +509 -0
  18. package/packages/chat-room-channeld/src/ingest-gate.ts +108 -0
  19. package/packages/chat-room-channeld/src/log.ts +26 -0
  20. package/packages/chat-room-channeld/src/mock-adapter.ts +206 -0
  21. package/packages/chat-room-channeld/src/protocol.ts +75 -0
  22. package/packages/chat-room-channeld/src/published.ts +37 -0
  23. package/packages/chat-room-channeld/src/rules.ts +127 -0
  24. package/packages/chat-room-channeld/src/state.ts +485 -0
  25. package/packages/chat-room-channeld/src/stats.ts +167 -0
  26. package/plugin.ts +22 -5
  27. package/server/addressing-routes.ts +130 -0
  28. package/server/assets/NOTICE.txt +2 -2
  29. package/server/assets/SHIKI-LICENSE +22 -0
  30. package/server/assets/base.css +9 -1
  31. package/server/assets/chat/doc-preview.js +3 -4
  32. package/server/assets/chat/forward.js +69 -0
  33. package/server/assets/chat/index.js +2 -0
  34. package/server/assets/chat/locales/de.js +5 -0
  35. package/server/assets/chat/locales/es.js +5 -0
  36. package/server/assets/chat/locales/fr.js +5 -0
  37. package/server/assets/chat/locales/it.js +5 -0
  38. package/server/assets/chat/locales/ja.js +5 -0
  39. package/server/assets/chat/locales/ko.js +5 -0
  40. package/server/assets/chat/markdown-footnotes.js +115 -0
  41. package/server/assets/chat/markdown.js +24 -35
  42. package/server/assets/chat/palette.js +1 -1
  43. package/server/assets/chat/replies.js +7 -1
  44. package/server/assets/chat/shiki.js +87 -0
  45. package/server/assets/chat/side-pane.js +8 -1
  46. package/server/assets/chat/state.js +14 -1
  47. package/server/assets/chat/system-messages.js +2 -0
  48. package/server/assets/code-highlight.css +20 -31
  49. package/server/assets/conversation.css +3 -2
  50. package/server/assets/doc-preview.css +2 -1
  51. package/server/assets/responsive.css +5 -0
  52. package/server/assets/shiki.bundle.js +152 -0
  53. package/server/channels.ts +314 -0
  54. package/server/chat-server.ts +27 -4
  55. package/server/chat.html +10 -3
  56. package/server/sw.js +1 -2
  57. package/skills/chatroom/SKILL.md +1 -1
  58. package/src/center/center-client.ts +79 -1
  59. package/src/config/file-config.ts +34 -0
  60. package/src/runtime/v2-plugin.ts +8 -2
  61. package/src/runtime/watch.ts +3 -2
  62. package/src/state/sqlite-state.ts +42 -11
  63. package/src/state/store.ts +9 -3
  64. package/src/tui/http.mjs +16 -2
  65. package/src/tui/main.mjs +1 -1
  66. package/server/assets/HIGHLIGHT-LICENSE +0 -29
  67. package/server/assets/highlight-10.7.3.min.js +0 -1358
package/package.json CHANGED
@@ -1,7 +1,8 @@
1
1
  {
2
2
  "name": "@alexsun-top/opencode-chat-room",
3
- "version": "0.7.8",
3
+ "version": "0.8.0",
4
4
  "description": "A chat-room server, web client, terminal client, and OpenCode/Codex/dsh plugins.",
5
+ "license": "MIT",
5
6
  "repository": {
6
7
  "type": "git",
7
8
  "url": "git+https://github.com/agentsyaml/opencode-chat-room.git"
@@ -19,20 +20,26 @@
19
20
  "opencode-chat-room": "dist/cli.js"
20
21
  },
21
22
  "files": [
23
+ "LICENSE",
22
24
  "cli.ts",
23
25
  "dist",
24
26
  "plugin.ts",
25
27
  "index.ts",
26
28
  "src",
29
+ "packages/chat-room-channeld/src",
30
+ "packages/chat-room-channeld/dist/mock-adapter.js",
31
+ "packages/chat-room-channeld/dist/feishu-adapter.js",
27
32
  "!src/tests",
28
33
  "!src/**/*.test.*",
29
34
  "!src/tui/test",
30
35
  "skills/chatroom/SKILL.md",
31
36
  "server/chat-server.ts",
37
+ "server/channels.ts",
32
38
  "server/http-server.ts",
33
39
  "server/json-body.ts",
34
40
  "server/inbox.ts",
35
41
  "server/history-route.ts",
42
+ "server/addressing-routes.ts",
36
43
  "server/message-recall-routes.ts",
37
44
  "server/room-payload.ts",
38
45
  "server/room-owner.ts",
@@ -63,7 +70,9 @@
63
70
  },
64
71
  "scripts": {
65
72
  "build:cli": "bun build cli.ts --target=node --packages external --outfile dist/cli.js >&2",
66
- "prepack": "bun run build:cli",
73
+ "build:channeld-mock": "bun build packages/chat-room-channeld/src/mock-adapter.ts --target=node --packages external --outfile packages/chat-room-channeld/dist/mock-adapter.js >&2",
74
+ "build:channeld-feishu": "bun build packages/chat-room-channeld/src/feishu-adapter.ts --target=node --packages external --outfile packages/chat-room-channeld/dist/feishu-adapter.js >&2",
75
+ "prepack": "bun run build:cli && bun run build:channeld-mock && bun run build:channeld-feishu",
67
76
  "server": "node cli.ts",
68
77
  "server:bun": "bun cli.ts",
69
78
  "server:deno": "deno run --allow-env --allow-read --allow-write --allow-net cli.ts",
@@ -76,7 +85,8 @@
76
85
  "devDependencies": {
77
86
  "@opencode/plugin": "2.0.29",
78
87
  "@types/bun": "latest",
79
- "playwright": "1.64.1"
88
+ "playwright": "1.64.1",
89
+ "shiki": "4.6.0"
80
90
  },
81
91
  "peerDependencies": {
82
92
  "typescript": "^5"
@@ -0,0 +1,242 @@
1
+ # chat-room-channeld
2
+
3
+ Channel D relay core. One host process connects chat-room endpoints to external
4
+ IM endpoints through adapter child processes. Explicit rules turn a source-room
5
+ event into frozen deliveries for target accounts; each adapter owns one IM
6
+ account and speaks JSON-RPC over stdio.
7
+
8
+ This is the 0.1 MVP: static rules, text messages, outbound adapters with text and Markdown cards, a mock
9
+ adapter, and an outbound Feishu adapter. Inbound Feishu events are not
10
+ implemented yet.
11
+
12
+ This package is bundled inside `@alexsun-top/opencode-chat-room` 0.8.0 as
13
+ source plus the Feishu and mock adapter entry points. Standalone npm
14
+ publication, and the `npx @alexsun-top/chat-room-channeld` form, are planned
15
+ for 0.8.x. In 0.8.0 use `GET /channeld/status` on the ChatRoom server, or run
16
+ the CLI from a checkout with Bun.
17
+
18
+ ## Run from a checkout
19
+
20
+ ```sh
21
+ bun src/cli.ts --config channeld.yml
22
+ ```
23
+
24
+ Validate a config without touching the state file:
25
+
26
+ ```sh
27
+ bun src/cli.ts check --config channeld.yml
28
+ # config ok: 1 accounts, 1 rules
29
+ ```
30
+
31
+ Inspect queue and integrity counts without writing the state file:
32
+
33
+ ```sh
34
+ bun src/cli.ts status --config channeld.yml
35
+ ```
36
+
37
+ `status` prints counts only: queue depth, oldest backlog, unknown, retryable and
38
+ rate-limited deliveries, and source/integrity gaps. It never prints message text,
39
+ rule ids, error details, provider message ids, room ids, or credentials. A missing
40
+ state file or missing channel tables exit 1 with a clear error.
41
+
42
+ Run the mock adapter on stdio (the host normally spawns it):
43
+
44
+ ```sh
45
+ bun src/cli.ts mock
46
+ ```
47
+
48
+ Requires Node.js 22.13 or newer (the state store uses `node:sqlite`).
49
+
50
+ Run the suite from a checkout:
51
+
52
+ ```sh
53
+ bun test tests/*.test.ts
54
+ ```
55
+
56
+ The standalone tarball, once published, includes `tests/` and a self-contained
57
+ `tsconfig.build.json`. To build and test from an installed tarball:
58
+
59
+ ```sh
60
+ cd node_modules/@alexsun-top/chat-room-channeld
61
+ npm install --include=dev
62
+ npm run build
63
+ bun test tests/*.test.ts
64
+ ```
65
+
66
+ Bun is still required for tests.
67
+
68
+ ## Config
69
+
70
+ ```yaml
71
+ stateFile: ./channeld.sqlite
72
+ accounts:
73
+ - id: im-a
74
+ platform: dingtalk
75
+ adapter: { command: ["node", "/opt/chatroom/dist/mock-adapter.js"], runtime: "node" }
76
+ rules:
77
+ - id: project-in
78
+ version: 1
79
+ from: { platform: chatroom, account: lab, room: translation, event: message.created }
80
+ transforms: [ { op: prefix-source }, { op: truncate, max: 2000 } ]
81
+ to: [ { platform: dingtalk, account: im-a, room: project } ]
82
+ onMissingParent: quote_or_unthreaded
83
+ onUnsupported: text_with_notice
84
+ ```
85
+
86
+ Transforms run in order and are limited to `prefix-source`, `trim`,
87
+ `truncate` (adds `…`), and `quote-preview`. Omitting `from.event` matches every
88
+ kind. Account ids, platforms, and room ids must match
89
+ `/^[A-Za-z0-9._-]+$/`; `/` is rejected so endpoint keys stay unambiguous.
90
+ Account ids and rule `(id, version)` pairs must be unique. Only the highest
91
+ configured version of each rule id is active: an explicit newer version
92
+ replaces the older definition.
93
+
94
+ ## Adapter protocol
95
+
96
+ The host spawns each adapter with `CHANNELD_ACCOUNT_ID` and
97
+ `CHANNELD_PLATFORM`. Both directions use newline-delimited JSON-RPC 2.0 on
98
+ stdio. **stdout carries protocol frames only; all adapter logs go to stderr.**
99
+ An invalid stdout frame disables that account, stops its adapter, and leaves its
100
+ deliveries paused; other accounts keep running. Frames are JSON objects limited
101
+ to 1 MiB (`MAX_FRAME_BYTES`). Host request ids start with `host-`, adapter
102
+ request ids with `adapter-`.
103
+
104
+ Host to adapter: `initialize` (protocol/event versions, account, frame limit),
105
+ `start` (accounts, cursors), `send` (delivery id, attempt id, idempotency key,
106
+ target endpoint, frozen payload), `ping`, `shutdown`.
107
+
108
+ Adapter to host: `ingest` (an `IngestEvent`) answered with
109
+ `{committed:true}` or `{duplicate:true}`, and `ping`. Unknown methods get
110
+ JSON-RPC `-32601`.
111
+
112
+ A JSON-RPC `id` only correlates one request/response within a session; it is not
113
+ an idempotency key. Repeating an `id` with different `sourceDedupKey` values is
114
+ two distinct events; repeating the same `sourceDedupKey` is one event.
115
+
116
+ A `send` result is a receipt: `{state, providerMessage?, retryAfterMs?, detail?}`
117
+ where state is `sent`, `retryable`, `rate_limited`, `permanent`, `unsupported`,
118
+ or `unknown`. A request timeout maps to `unknown`; the host never throws for a
119
+ platform outcome.
120
+
121
+ Sends are serial per account. Inbound ingests are buffered behind an in-flight
122
+ send so provider echoes are matched before they can become new events; an
123
+ overflow (100 events) disables that account and is logged as an error. After an
124
+ overflow drain, every delivery for the disabled account is paused, and the
125
+ overflowed event is dropped.
126
+
127
+ When `CHANNELD_MOCK_EMIT=1`, the mock adapter emits one `message.created` event
128
+ from room `in` after starting, so the relay can be exercised end to end.
129
+ `CHANNELD_MOCK_ECHO=1` makes it emit a provider echo right after a `send` result;
130
+ `CHANNELD_MOCK_ECHO_DELAY_MS` delays that result to exercise buffered ingests.
131
+
132
+ ## Feishu outbound adapter
133
+
134
+ `dist/feishu-adapter.js` is an outbound-only adapter for Feishu OpenAPI
135
+ messages. It sends interactive cards using `lark_md` inline formatting and
136
+ declares `reply: "none"`; native Feishu reply/quote requires a Feishu parent
137
+ message and is deferred with inbound support. It speaks the same stdio JSON-RPC protocol as the mock adapter and
138
+ has no room or session concepts. Inbound Feishu events are planned for 0.8.x.
139
+
140
+ The adapter command is resolved from the host process working directory, so
141
+ use an absolute path unless the host starts in this package directory.
142
+
143
+ The child process reads:
144
+
145
+ - `FEISHU_APP_ID` (required)
146
+ - `FEISHU_APP_SECRET` (required)
147
+ - `FEISHU_BASE_URL` (optional, default `https://open.feishu.cn`)
148
+ - `FEISHU_HTTP_TIMEOUT_MS` (optional, default `10000`)
149
+
150
+ Set these in the host environment; the host passes its environment to adapter
151
+ child processes.
152
+
153
+ ```yaml
154
+ accounts:
155
+ - id: feishu-bot
156
+ platform: feishu
157
+ adapter: { command: ["node", "/opt/chatroom/dist/feishu-adapter.js"], runtime: "node" }
158
+ rules:
159
+ - id: to-feishu
160
+ version: 1
161
+ from: { platform: chatroom, account: lab, room: translation, event: message.created }
162
+ transforms: [ { op: prefix-source }, { op: truncate, max: 2000 } ]
163
+ to: [ { platform: feishu, account: feishu-bot, room: oc_xxx } ]
164
+ ```
165
+
166
+ The target room is the Feishu `chat_id`, so an endpoint
167
+ `feishu/<account>/<chat_id>` maps to one Feishu chat.
168
+
169
+ `initialize` fails when a credential is missing. `start` fetches a tenant access
170
+ token, so rejected credentials disable only this account. Tokens are cached and
171
+ refreshed when less than 30 minutes remain. A send posts an interactive
172
+ Markdown card (`msg_type: "interactive"` with a `lark_md` element) and a `uuid`
173
+ derived from the delivery idempotency key, so retrying the same delivery cannot
174
+ create a second message.
175
+
176
+ Send outcomes:
177
+
178
+ - `230020`, `99991400`, or HTTP 429: `rate_limited`; `Retry-After` accepts
179
+ seconds or an HTTP date and defaults to 1000 ms.
180
+ - `99991663`: force one token refresh and retry the send once, then `permanent`.
181
+ - HTTP 5xx, connection resets, and other fetch network errors: `retryable`.
182
+ - Request timeout or abort: `unknown`, never retried automatically.
183
+ - Other Feishu codes (for example `230002`): `permanent` with code and message.
184
+ - Malformed response: `unknown`.
185
+
186
+ The adapter never logs message text, credentials, tokens, uuids, or full
187
+ payloads. Provider message ids may be logged and returned in the receipt.
188
+
189
+ ## State
190
+
191
+ The SQLite state file has four tables:
192
+
193
+ - `channel_events`: committed ingest events, unique by dedup key.
194
+ - `channel_deliveries`: one frozen delivery per rule target, unique by
195
+ idempotency key (`trigger:rule:version:0:target`).
196
+ - `channel_receipts`: one observation per send attempt.
197
+ - `channel_echoes`: provider message ids that originated from this host, used to
198
+ skip echo ingests.
199
+
200
+ Delivery states: `pending → in_flight → sent`; `retryable` and `rate_limited`
201
+ retry only that account's queue with capped backoff (8 attempts, then
202
+ `exhausted`); `permanent` and `unsupported` are terminal; `unknown` leaves the
203
+ delivery in `unknown` and never retries automatically; `paused` keeps a disabled
204
+ account's rows out of every claim until the next host start. An adapter that
205
+ fails to start or writes non-protocol stdout disables only that account until
206
+ restart; its queued deliveries move to `paused` and return to `pending` when the
207
+ host starts again. On disable or crash, in-flight deliveries move to `unknown`
208
+ with the reason; a host start resets stale `in_flight` rows from a previous
209
+ crash to `pending`. If every account becomes disabled at runtime, the host logs
210
+ `all accounts disabled; host idle until restart` and stays alive; only a startup
211
+ with zero usable adapters fails. `onUnsupported: drop` skips empty renders; otherwise the
212
+ payload carries an `attachment/format unsupported` notice. Replies without a
213
+ target parent mapping are sent unthreaded with `degraded: "unthreaded"`, or
214
+ dropped when `onMissingParent: drop`.
215
+
216
+ ## Center delivery results (M2)
217
+
218
+ `HostDeps.onDeliveryResult(event, delivery, receipt)` fires once per delivery when
219
+ it reaches a terminal state: `sent`, `permanent`, `unsupported`, `exhausted`, or
220
+ `unknown`. `receipt` is present only when a receipt row exists for that attempt.
221
+ When an account is disabled or its adapter crashes, in-flight deliveries settle as
222
+ `unknown` and report with `receipt: undefined`; no receipt row is invented.
223
+ Retryable and rate-limited attempts never report, and a delivery reports at most
224
+ once.
225
+
226
+ Every terminal delivery also records exactly one `result.published` event in
227
+ `channel_events`. Its payload holds ids, delivery state, and target only — never
228
+ message text, error detail, or provider secrets — and `readChannelStats` reports
229
+ it as `publishedResults` while excluding it from source gaps. Rules never match or
230
+ forward `result.published`; the center writes the localized terminal system
231
+ message from that terminal result. Deliveries abandoned at startup because their
232
+ account is no longer configured are also reported as `unknown` and receive the
233
+ same terminal result event and message.
234
+
235
+ ## Non-goals (0.1 MVP)
236
+
237
+ - no config hot reload — rule changes require a restart (each delivery still freezes ruleId + ruleVersion + rendered payload);
238
+ - no one-click manual resend command — `unknown` deliveries only guarantee no duplicate delivery row if resent (unique idempotency key); they are never auto-resent;
239
+ - no full backpressure (high-water marks, pausing ingress, disk quotas) — only per-account queue/process isolation and `rate_limited` backoff;
240
+ - no attachments, no rule UI, no multi-tenant permission model, no inbound
241
+ Feishu events (the Feishu adapter is outbound only), no exactly-once
242
+ guarantee.