@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.
Files changed (183) hide show
  1. package/CHANGELOG.md +43 -1
  2. package/LICENSE +21 -0
  3. package/README.md +77 -36
  4. package/README.zh-CN.md +82 -41
  5. package/cli.ts +27 -1
  6. package/dist/cli.js +6905 -2764
  7. package/package.json +33 -3
  8. package/packages/chat-room-channeld/README.md +315 -0
  9. package/packages/chat-room-channeld/dist/feishu-adapter.js +1262 -0
  10. package/packages/chat-room-channeld/dist/mock-adapter.js +295 -0
  11. package/packages/chat-room-channeld/src/adapter.ts +509 -0
  12. package/packages/chat-room-channeld/src/cli.ts +124 -0
  13. package/packages/chat-room-channeld/src/config.ts +269 -0
  14. package/packages/chat-room-channeld/src/delivery.ts +163 -0
  15. package/packages/chat-room-channeld/src/feishu-adapter.ts +303 -0
  16. package/packages/chat-room-channeld/src/feishu-card.ts +27 -0
  17. package/packages/chat-room-channeld/src/feishu-inbound.ts +158 -0
  18. package/packages/chat-room-channeld/src/feishu-live.ts +355 -0
  19. package/packages/chat-room-channeld/src/feishu-send.ts +342 -0
  20. package/packages/chat-room-channeld/src/feishu-ws-frames.ts +173 -0
  21. package/packages/chat-room-channeld/src/framing.ts +128 -0
  22. package/packages/chat-room-channeld/src/host-helpers.ts +52 -0
  23. package/packages/chat-room-channeld/src/host.ts +627 -0
  24. package/packages/chat-room-channeld/src/inbound.ts +26 -0
  25. package/packages/chat-room-channeld/src/ingest-gate.ts +108 -0
  26. package/packages/chat-room-channeld/src/log.ts +26 -0
  27. package/packages/chat-room-channeld/src/mock-adapter.ts +206 -0
  28. package/packages/chat-room-channeld/src/protocol.ts +75 -0
  29. package/packages/chat-room-channeld/src/published.ts +37 -0
  30. package/packages/chat-room-channeld/src/rules.ts +127 -0
  31. package/packages/chat-room-channeld/src/state.ts +502 -0
  32. package/packages/chat-room-channeld/src/stats.ts +209 -0
  33. package/plugin.ts +22 -5
  34. package/server/addressing-routes.ts +130 -0
  35. package/server/admin-state.ts +13 -13
  36. package/server/app.ts +95 -0
  37. package/server/assets/NOTICE.txt +3 -2
  38. package/server/assets/SHIKI-LICENSE +22 -0
  39. package/server/assets/base.css +24 -10
  40. package/server/assets/chat/announcements.js +70 -7
  41. package/server/assets/chat/doc-preview.js +187 -39
  42. package/server/assets/chat/events.js +3 -0
  43. package/server/assets/chat/files.js +46 -4
  44. package/server/assets/chat/forward.js +69 -0
  45. package/server/assets/chat/index.js +4 -0
  46. package/server/assets/chat/integration-editor.js +163 -0
  47. package/server/assets/chat/integration-values.js +124 -0
  48. package/server/assets/chat/integrations.js +498 -0
  49. package/server/assets/chat/locales/de.js +45 -4
  50. package/server/assets/chat/locales/es.js +45 -4
  51. package/server/assets/chat/locales/fr.js +45 -4
  52. package/server/assets/chat/locales/it.js +45 -4
  53. package/server/assets/chat/locales/ja.js +45 -4
  54. package/server/assets/chat/locales/ko.js +45 -4
  55. package/server/assets/chat/markdown-footnotes.js +115 -0
  56. package/server/assets/chat/markdown.js +46 -43
  57. package/server/assets/chat/messages.js +5 -5
  58. package/server/assets/chat/office-preview.js +69 -0
  59. package/server/assets/chat/palette.js +1 -1
  60. package/server/assets/chat/panels.js +36 -0
  61. package/server/assets/chat/recall.js +58 -7
  62. package/server/assets/chat/receipts.js +15 -3
  63. package/server/assets/chat/reminders.js +69 -8
  64. package/server/assets/chat/replies.js +7 -1
  65. package/server/assets/chat/resource-format.js +11 -0
  66. package/server/assets/chat/settings.js +30 -2
  67. package/server/assets/chat/shiki.js +87 -0
  68. package/server/assets/chat/side-pane.js +9 -3
  69. package/server/assets/chat/state.js +107 -11
  70. package/server/assets/chat/system-events.js +32 -0
  71. package/server/assets/chat/system-messages.js +23 -12
  72. package/server/assets/chat/task-row.js +0 -6
  73. package/server/assets/chat/tasks.js +121 -33
  74. package/server/assets/code-highlight.css +74 -62
  75. package/server/assets/conversation.css +126 -25
  76. package/server/assets/doc-preview.css +36 -3
  77. package/server/assets/files.css +5 -0
  78. package/server/assets/fluent-icons.svg +2 -0
  79. package/server/assets/monaco/LICENSE +21 -0
  80. package/server/assets/monaco/ThirdPartyNotices.txt +448 -0
  81. package/server/assets/monaco/codicon-jdswhhtn.ttf +0 -0
  82. package/server/assets/monaco/editor.worker.js +45 -0
  83. package/server/assets/monaco/integration-rules.css +1 -0
  84. package/server/assets/monaco/integration-rules.js +1297 -0
  85. package/server/assets/monaco/json.worker.js +77 -0
  86. package/server/assets/office/THIRD-PARTY-NOTICES.txt +11 -0
  87. package/server/assets/office/docx.bundle.js +33 -0
  88. package/server/assets/office/licenses/docx-preview-LICENSE.txt +201 -0
  89. package/server/assets/office/licenses/echarts-LICENSE.txt +222 -0
  90. package/server/assets/office/licenses/jszip-LICENSE.txt +651 -0
  91. package/server/assets/office/licenses/lodash-LICENSE.txt +47 -0
  92. package/server/assets/office/licenses/pptx-preview-LICENSE.txt +8 -0
  93. package/server/assets/office/licenses/tslib-LICENSE.txt +12 -0
  94. package/server/assets/office/licenses/uuid-LICENSE.txt +9 -0
  95. package/server/assets/office/licenses/xlsx-LICENSE.txt +201 -0
  96. package/server/assets/{HIGHLIGHT-LICENSE → office/licenses/zrender-LICENSE.txt} +2 -2
  97. package/server/assets/office/pptx.bundle.js +104 -0
  98. package/server/assets/office/xlsx.bundle.js +141 -0
  99. package/server/assets/recall.css +5 -4
  100. package/server/assets/responsive.css +52 -8
  101. package/server/assets/settings.css +73 -1
  102. package/server/assets/shiki.bundle.js +152 -0
  103. package/server/assets/tasks.css +57 -7
  104. package/server/channel-integration.ts +519 -0
  105. package/server/channel-settings.ts +77 -0
  106. package/server/channels.ts +557 -0
  107. package/server/chat-server.ts +33 -558
  108. package/server/chat.html +195 -22
  109. package/server/http-server.ts +3 -182
  110. package/server/resource-routes.ts +16 -16
  111. package/server/routes/inbox.ts +100 -0
  112. package/server/routes/pages.ts +83 -0
  113. package/server/routes/questions.ts +39 -0
  114. package/server/routes/room-actions.ts +162 -0
  115. package/server/routes/rooms.ts +160 -0
  116. package/server/startup.ts +84 -0
  117. package/server/sw.js +5 -2
  118. package/server/task-routes.ts +1 -1
  119. package/skills/chatroom/SKILL.md +1 -1
  120. package/src/center/center-client.ts +71 -1
  121. package/src/config/file-config.ts +29 -42
  122. package/src/platform/http-server.ts +182 -0
  123. package/src/protocol/addressing.ts +21 -0
  124. package/src/protocol/config.ts +58 -0
  125. package/src/protocol/room.ts +51 -0
  126. package/src/rooms/resources.ts +26 -16
  127. package/src/rooms/room-service.ts +2 -48
  128. package/src/rooms/storage.ts +329 -0
  129. package/src/rooms/tasks.ts +21 -5
  130. package/src/runtime/v2-plugin.ts +8 -2
  131. package/src/runtime/watch.ts +4 -3
  132. package/src/state/pg-database.ts +183 -0
  133. package/src/state/pg-migrate.ts +175 -0
  134. package/src/state/pg-sql.mjs +175 -0
  135. package/src/state/pg-worker.mjs +148 -0
  136. package/src/state/sqlite-announcements.ts +25 -0
  137. package/src/state/sqlite-database.ts +121 -0
  138. package/src/state/sqlite-legacy.ts +90 -0
  139. package/src/state/sqlite-members.ts +155 -0
  140. package/src/state/sqlite-messages.ts +288 -0
  141. package/src/state/sqlite-resources.ts +39 -0
  142. package/src/state/sqlite-rooms.ts +87 -0
  143. package/src/state/sqlite-schema.ts +80 -0
  144. package/src/state/sqlite-state-store.ts +33 -0
  145. package/src/state/sqlite-state.ts +15 -866
  146. package/src/state/sqlite-tasks.ts +65 -0
  147. package/src/state/sqlite-transaction.ts +13 -0
  148. package/src/state/sqlite.ts +21 -1
  149. package/src/state/store.ts +13 -3
  150. package/src/tui/app.mjs +32 -19
  151. package/src/tui/config.mjs +12 -4
  152. package/src/tui/http.mjs +16 -2
  153. package/src/tui/i18n.mjs +38 -0
  154. package/src/tui/info-app.mjs +50 -51
  155. package/src/tui/info-content.mjs +79 -44
  156. package/src/tui/join-app.mjs +9 -9
  157. package/src/tui/locales/en.mjs +349 -0
  158. package/src/tui/locales/zh-CN.mjs +349 -0
  159. package/src/tui/main.mjs +85 -37
  160. package/src/tui/markdown.mjs +3 -2
  161. package/src/tui/mentions.mjs +3 -1
  162. package/src/tui/message-app.mjs +22 -21
  163. package/src/tui/message-view.mjs +2 -1
  164. package/src/tui/palette-app.mjs +9 -8
  165. package/src/tui/palette.mjs +30 -24
  166. package/src/tui/prefs.mjs +92 -0
  167. package/src/tui/reminder-app.mjs +18 -17
  168. package/src/tui/room-settings-app.mjs +30 -28
  169. package/src/tui/settings-app.mjs +153 -0
  170. package/src/tui/ui-content.mjs +65 -86
  171. package/src/tui/ui-help.mjs +73 -0
  172. package/src/tui/ui-overlay-view.mjs +23 -7
  173. package/src/tui/ui-suggest.mjs +6 -5
  174. package/src/tui/ui-theme.mjs +37 -33
  175. package/src/tui/ui-view.mjs +22 -7
  176. package/src/tui/ui.mjs +15 -3
  177. package/src/web/editor.worker.ts +1 -0
  178. package/src/web/integration-rules-editor.ts +439 -0
  179. package/src/web/integration-rules.ts +22 -0
  180. package/src/web/integration-schema.ts +228 -0
  181. package/src/web/json.worker.ts +1 -0
  182. package/src/web/office-viewer.ts +221 -0
  183. 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.1",
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,31 @@
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/startup.ts",
38
+ "server/routes",
39
+ "server/app.ts",
40
+ "server/channels.ts",
41
+ "server/channel-settings.ts",
42
+ "server/channel-integration.ts",
32
43
  "server/http-server.ts",
33
44
  "server/json-body.ts",
34
45
  "server/inbox.ts",
35
46
  "server/history-route.ts",
47
+ "server/addressing-routes.ts",
36
48
  "server/message-recall-routes.ts",
37
49
  "server/room-payload.ts",
38
50
  "server/room-owner.ts",
@@ -63,7 +75,11 @@
63
75
  },
64
76
  "scripts": {
65
77
  "build:cli": "bun build cli.ts --target=node --packages external --outfile dist/cli.js >&2",
66
- "prepack": "bun run build:cli",
78
+ "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",
79
+ "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",
80
+ "build:monaco": "bun scripts/build-monaco.ts",
81
+ "build:office": "bun scripts/build-office.ts",
82
+ "prepack": "bun run build:cli && bun run build:channeld-mock && bun run build:channeld-feishu && bun scripts/build-monaco.ts --quiet && bun scripts/build-office.ts --quiet",
67
83
  "server": "node cli.ts",
68
84
  "server:bun": "bun cli.ts",
69
85
  "server:deno": "deno run --allow-env --allow-read --allow-write --allow-net cli.ts",
@@ -76,17 +92,31 @@
76
92
  "devDependencies": {
77
93
  "@opencode/plugin": "2.0.29",
78
94
  "@types/bun": "latest",
79
- "playwright": "1.64.1"
95
+ "@types/pg": "^8.23.1",
96
+ "docx-preview": "0.4.1",
97
+ "jszip": "3.10.2",
98
+ "monaco-editor": "0.58.0",
99
+ "playwright": "1.64.1",
100
+ "pptx-preview": "1.0.7",
101
+ "shiki": "4.6.0",
102
+ "xlsx": "https://cdn.sheetjs.com/xlsx-0.20.3/xlsx-0.20.3.tgz"
80
103
  },
81
104
  "peerDependencies": {
82
105
  "typescript": "^5"
83
106
  },
84
107
  "dependencies": {
108
+ "@aws-sdk/client-s3": "^3.1178.0",
85
109
  "@opencode-ai/plugin": "^1.18.29",
86
110
  "@opencode/client": "2.0.32",
87
111
  "@opentui/core": "0.2.16",
88
112
  "croner": "11.0.0",
113
+ "pg": "^8.23.1",
89
114
  "yaml": "^2.9.0",
90
115
  "zod": "4.1.8"
116
+ },
117
+ "overrides": {
118
+ "echarts": "6.1.0",
119
+ "lodash": "4.18.1",
120
+ "uuid": "11.1.1"
91
121
  }
92
122
  }
@@ -0,0 +1,315 @@
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 a Feishu adapter with opt-in inbound long-connection events. Inbound Feishu
10
+ events can be relayed into one chat-room as notifications; they are never matched by
11
+ outbound rules.
12
+
13
+ This package is bundled inside `@alexsun-top/opencode-chat-room` 0.8.0 as
14
+ source plus the Feishu and mock adapter entry points. Standalone npm
15
+ publication, and the `npx @alexsun-top/chat-room-channeld` form, are planned
16
+ for 0.8.x. In 0.8.0 use `GET /channeld/status` on the ChatRoom server, or run
17
+ the CLI from a checkout with Bun.
18
+
19
+ ## Run from a checkout
20
+
21
+ ```sh
22
+ bun src/cli.ts --config channeld.yml
23
+ ```
24
+
25
+ Validate a config without touching the state file:
26
+
27
+ ```sh
28
+ bun src/cli.ts check --config channeld.yml
29
+ # config ok: 1 accounts, 1 rules
30
+ ```
31
+
32
+ Inspect queue and integrity counts without writing the state file:
33
+
34
+ ```sh
35
+ bun src/cli.ts status --config channeld.yml
36
+ ```
37
+
38
+ `status` prints counts and per-account runtime state: `started`, `failed`,
39
+ `disabled`, or `unknown` (no live host is attached to a CLI read), plus a short
40
+ sanitized `lastError` for failed/disabled accounts. It never prints message text,
41
+ rule ids, provider message ids, room ids, or credentials. A missing state file or
42
+ missing channel tables exit 1 with a clear error.
43
+
44
+ Run the mock adapter on stdio (the host normally spawns it):
45
+
46
+ ```sh
47
+ bun src/cli.ts mock
48
+ ```
49
+
50
+ Requires Node.js 22.13 or newer (the state store uses `node:sqlite`).
51
+
52
+ Run the suite from a checkout:
53
+
54
+ ```sh
55
+ bun test tests/*.test.ts
56
+ ```
57
+
58
+ The standalone tarball, once published, includes `tests/` and a self-contained
59
+ `tsconfig.build.json`. To build and test from an installed tarball:
60
+
61
+ ```sh
62
+ cd node_modules/@alexsun-top/chat-room-channeld
63
+ npm install --include=dev
64
+ npm run build
65
+ bun test tests/*.test.ts
66
+ ```
67
+
68
+ Bun is still required for tests.
69
+
70
+ ## Config
71
+
72
+ ```yaml
73
+ stateFile: ./channeld.sqlite
74
+ accounts:
75
+ - id: im-a
76
+ platform: dingtalk
77
+ adapter: { command: ["node", "/opt/chatroom/dist/mock-adapter.js"], runtime: "node" }
78
+ rules:
79
+ - id: project-in
80
+ version: 1
81
+ from: { platform: chatroom, account: lab, room: translation, event: message.created }
82
+ transforms: [ { op: prefix-source }, { op: truncate, max: 2000 } ]
83
+ to: [ { platform: dingtalk, account: im-a, room: project } ]
84
+ onMissingParent: quote_or_unthreaded
85
+ onUnsupported: text_with_notice
86
+ ```
87
+
88
+ Transforms run in order and are limited to `prefix-source`, `trim`,
89
+ `truncate` (adds `…`), and `quote-preview`. Omitting `from.event` matches every
90
+ kind. Account ids, platforms, and room ids must match
91
+ `/^[A-Za-z0-9._-]+$/`; `/` is rejected so endpoint keys stay unambiguous.
92
+ Account ids and rule `(id, version)` pairs must be unique. Only the highest
93
+ configured version of each rule id is active: an explicit newer version
94
+ replaces the older definition.
95
+
96
+ Each account may add an `env` map for secrets and per-platform settings. Keys
97
+ must match `/^[A-Za-z_][A-Za-z0-9_]*$/`, values are strings, and a map holds at
98
+ most 64 variables. The adapter child inherits the host environment, then receives
99
+ the account `env`, then the forced `CHANNELD_ACCOUNT_ID` and `CHANNELD_PLATFORM`
100
+ values; configuring either reserved key is rejected. Status output never includes
101
+ env values.
102
+
103
+ An optional top-level `inbound` array turns external events into room notifications
104
+ in the center. It is separate from `rules`: an inbound event is committed, written
105
+ to its target room once, and then skipped by every outbound rule, so it cannot
106
+ ping-pong back to the source.
107
+
108
+ ```yaml
109
+ inbound:
110
+ - from: { platform: feishu, account: feishu-main, room: oc_xxx }
111
+ to: { room: lab }
112
+ ```
113
+
114
+ `from.room` is the Feishu `chat_id`; `to.room` is the chat-room id. An omitted
115
+ `from.event` matches every kind. A source account must exist and its platform must
116
+ match. The center appends one system-style message, `[Feishu <account>] <open_id>:
117
+ <text>`, with a marker row in the room database; the marker and message share one
118
+ transaction, so a transport retry cannot duplicate it. If the room write fails, the
119
+ center rolls the channel event back and the adapter acknowledges failure, so Feishu
120
+ retries. A room that stays missing keeps failing until it is created.
121
+
122
+ ```yaml
123
+ accounts:
124
+ - id: feishu-main
125
+ platform: feishu
126
+ adapter: { command: ["node", "/opt/chatroom/feishu-adapter.js"] }
127
+ env: { FEISHU_APP_ID: "cli_xxx", FEISHU_APP_SECRET: "xxx" }
128
+ ```
129
+
130
+ ## Adapter protocol
131
+
132
+ The host spawns each adapter with `CHANNELD_ACCOUNT_ID`,
133
+ `CHANNELD_PLATFORM`, and that account's configured `env`. Both directions use
134
+ newline-delimited JSON-RPC 2.0 on stdio. An `initialize` or `start` result may
135
+ include `platform`; when it differs from the configured account platform, that
136
+ adapter is stopped and only that account is marked `failed` (its queue pauses)
137
+ while the host and other accounts keep running. **stdout carries protocol frames only; all adapter logs go to stderr.**
138
+ An invalid stdout frame disables that account, stops its adapter, and leaves its
139
+ deliveries paused; other accounts keep running. Frames are JSON objects limited
140
+ to 1 MiB (`MAX_FRAME_BYTES`). Host request ids start with `host-`, adapter
141
+ request ids with `adapter-`.
142
+
143
+ Host to adapter: `initialize` (protocol/event versions, account, frame limit),
144
+ `start` (accounts, cursors), `send` (delivery id, attempt id, idempotency key,
145
+ target endpoint, frozen payload), `ping`, `shutdown`.
146
+
147
+ Adapter to host: `ingest` (an `IngestEvent`) answered with
148
+ `{committed:true}` or `{duplicate:true}`, and `ping`. Unknown methods get
149
+ JSON-RPC `-32601`.
150
+
151
+ A JSON-RPC `id` only correlates one request/response within a session; it is not
152
+ an idempotency key. Repeating an `id` with different `sourceDedupKey` values is
153
+ two distinct events; repeating the same `sourceDedupKey` is one event.
154
+
155
+ A `send` result is a receipt: `{state, providerMessage?, retryAfterMs?, detail?}`
156
+ where state is `sent`, `retryable`, `rate_limited`, `permanent`, `unsupported`,
157
+ or `unknown`. A request timeout maps to `unknown`; the host never throws for a
158
+ platform outcome.
159
+
160
+ Sends are serial per account. Inbound ingests are buffered behind an in-flight
161
+ send so provider echoes are matched before they can become new events; an
162
+ overflow (100 events) disables that account and is logged as an error. After an
163
+ overflow drain, every delivery for the disabled account is paused, and the
164
+ overflowed event is dropped.
165
+
166
+ When `CHANNELD_MOCK_EMIT=1`, the mock adapter emits one `message.created` event
167
+ from room `in` after starting, so the relay can be exercised end to end.
168
+ `CHANNELD_MOCK_ECHO=1` makes it emit a provider echo right after a `send` result;
169
+ `CHANNELD_MOCK_ECHO_DELAY_MS` delays that result to exercise buffered ingests.
170
+
171
+ ## Feishu adapter
172
+
173
+ `dist/feishu-adapter.js` is a Feishu OpenAPI adapter. Outbound it sends interactive
174
+ cards and can reply natively to a Feishu message; inbound it can receive group
175
+ messages over the event long connection. It speaks the same stdio JSON-RPC protocol
176
+ as the mock adapter and has no room or session concepts.
177
+
178
+ Cards default to card 1.0 with `lark_md`, which keeps the 0.8.0 payload stable.
179
+ Set `FEISHU_CARD_VERSION=2.0` to opt into card 2.0 (`schema: "2.0"` with a
180
+ `markdown` element), where headings, lists, and fenced code render as block
181
+ Markdown. Any other value fails `initialize`.
182
+
183
+ A payload `parent` with `endpoint.platform: "feishu"`, the same account, and a
184
+ non-empty `nativeMessageId` is sent with
185
+ `POST /open-apis/im/v1/messages/{parent_id}/reply`; the body has no `receive_id`.
186
+ Without a usable Feishu parent the send uses the normal chat endpoint. The center
187
+ fills that parent when a forwarded room reply's `answer_to` parent was already
188
+ delivered to the same target (`channel_echoes` keeps the provider native id).
189
+
190
+ The adapter command is resolved from the host process working directory, so
191
+ use an absolute path unless the host starts in this package directory.
192
+
193
+ The child process reads:
194
+
195
+ - `FEISHU_APP_ID` (required)
196
+ - `FEISHU_APP_SECRET` (required)
197
+ - `FEISHU_BASE_URL` (optional, default `https://open.feishu.cn`)
198
+ - `FEISHU_HTTP_TIMEOUT_MS` (optional, default `10000`)
199
+ - `FEISHU_CARD_VERSION` (optional, `1.0` default or `2.0`)
200
+ - `FEISHU_INBOUND` (optional, `1` enables the event long connection)
201
+ - `FEISHU_INBOUND_CHATS` (required with `FEISHU_INBOUND=1`; comma-separated
202
+ group `chat_id` allowlist)
203
+
204
+ Set these in the host environment; the host passes its environment to adapter
205
+ child processes.
206
+
207
+ Inbound requires the app to use long-connection event subscription in the Feishu
208
+ developer console and to subscribe `im.message.receive_v1`; the bot must already
209
+ be in each allowlisted group. The adapter obtains the WebSocket URL from
210
+ `POST /callback/ws/endpoint`, pings, reassembles fragmented frames, and
211
+ acknowledges only after the host commits the ingest. Only group `text` and `post`
212
+ messages from human senders in an allowlisted chat are mapped. Bot/self senders,
213
+ other chats, other event types, other apps, and unsupported message types are
214
+ acknowledged without ingest. A long-connection start failure logs a warning,
215
+ keeps the account started for outbound sends, and reports a sanitized
216
+ `inbound failed: ...` in the account status; outbound forwarding keeps working.
217
+
218
+ ```yaml
219
+ accounts:
220
+ - id: feishu-bot
221
+ platform: feishu
222
+ adapter: { command: ["node", "/opt/chatroom/dist/feishu-adapter.js"], runtime: "node" }
223
+ rules:
224
+ - id: to-feishu
225
+ version: 1
226
+ from: { platform: chatroom, account: lab, room: translation, event: message.created }
227
+ transforms: [ { op: prefix-source }, { op: truncate, max: 2000 } ]
228
+ to: [ { platform: feishu, account: feishu-bot, room: oc_xxx } ]
229
+ ```
230
+
231
+ The target room is the Feishu `chat_id`, so an endpoint
232
+ `feishu/<account>/<chat_id>` maps to one Feishu chat.
233
+
234
+ `initialize` fails when a credential is missing. `start` fetches a tenant access
235
+ token, so rejected credentials disable only this account. Tokens are cached and
236
+ refreshed when less than 30 minutes remain. A send posts an interactive card and
237
+ a `uuid` derived from the delivery idempotency key, so retrying the same delivery
238
+ cannot create a second message. Card 1.0 uses one `lark_md` element; card 2.0
239
+ uses one `markdown` element. A Feishu parent reference turns the send into a native
240
+ reply to that message.
241
+
242
+ Send outcomes:
243
+
244
+ - `230020`, `99991400`, or HTTP 429: `rate_limited`; `Retry-After` accepts
245
+ seconds or an HTTP date and defaults to 1000 ms.
246
+ - `99991663`: force one token refresh and retry the send once, then `permanent`.
247
+ - HTTP 5xx, connection resets, and other fetch network errors: `retryable`.
248
+ - Request timeout or abort: `unknown`, never retried automatically.
249
+ - Other Feishu codes (for example `230002`): `permanent` with code and message.
250
+ - Malformed response: `unknown`.
251
+
252
+ The adapter never logs message text, credentials, tokens, uuids, or full
253
+ payloads. Provider message ids may be logged and returned in the receipt.
254
+
255
+ ## State
256
+
257
+ The SQLite state file has four tables:
258
+
259
+ - `channel_events`: committed ingest events, unique by dedup key.
260
+ - `channel_deliveries`: one frozen delivery per rule target, unique by
261
+ idempotency key (`trigger:rule:version:0:target`).
262
+ - `channel_receipts`: one observation per send attempt.
263
+ - `channel_echoes`: provider message ids that originated from this host, used to
264
+ skip echo ingests.
265
+
266
+ Delivery states: `pending → in_flight → sent`; `retryable` and `rate_limited`
267
+ retry only that account's queue with capped backoff (8 attempts, then
268
+ `exhausted`); `permanent` and `unsupported` are terminal; `unknown` leaves the
269
+ delivery in `unknown` and never retries automatically; `paused` keeps a disabled
270
+ account's rows out of every claim until the next host start. An adapter that
271
+ fails to start or writes non-protocol stdout disables only that account until
272
+ restart; its queued deliveries move to `paused` and return to `pending` when the
273
+ host starts again. Inbound room notifications add one small
274
+ `channel_inbound_relays(event_id, room_id)` marker table to the room database;
275
+ it exists only to make the room append idempotent. Per-account `readChannelStats` states are `started` (adapter
276
+ running), `failed` (startup failure, including a platform mismatch), `disabled`
277
+ (disabled at runtime), or `unknown` without a live host. `lastError` is capped,
278
+ flattened to one line, and redacts credential-shaped `token=…`/`secret=…`
279
+ fragments. On disable or crash, in-flight deliveries move to `unknown`
280
+ with the reason; a deliberate host stop also settles remaining in-flight rows as
281
+ `unknown`, so a centered reload cannot auto-resend them. A host start resets
282
+ stale `in_flight` rows from a previous crash to `pending`. If every account becomes disabled at runtime, the host logs
283
+ `all accounts disabled; host idle until restart` and stays alive; only a startup
284
+ with zero usable adapters fails. `onUnsupported: drop` skips empty renders; otherwise the
285
+ payload carries an `attachment/format unsupported` notice. Replies without a
286
+ target parent mapping are sent unthreaded with `degraded: "unthreaded"`, or
287
+ dropped when `onMissingParent: drop`.
288
+
289
+ ## Center delivery results (M2)
290
+
291
+ `HostDeps.onDeliveryResult(event, delivery, receipt)` fires once per delivery when
292
+ it reaches a terminal state: `sent`, `permanent`, `unsupported`, `exhausted`, or
293
+ `unknown`. `receipt` is present only when a receipt row exists for that attempt.
294
+ When an account is disabled or its adapter crashes, in-flight deliveries settle as
295
+ `unknown` and report with `receipt: undefined`; no receipt row is invented.
296
+ Retryable and rate-limited attempts never report, and a delivery reports at most
297
+ once.
298
+
299
+ Every terminal delivery also records exactly one `result.published` event in
300
+ `channel_events`. Its payload holds ids, delivery state, and target only — never
301
+ message text, error detail, or provider secrets — and `readChannelStats` reports
302
+ it as `publishedResults` while excluding it from source gaps. Rules never match or
303
+ forward `result.published`; the center writes the localized terminal system
304
+ message from that terminal result. Deliveries abandoned at startup because their
305
+ account is no longer configured are also reported as `unknown` and receive the
306
+ same terminal result event and message.
307
+
308
+ ## Non-goals (0.1 MVP)
309
+
310
+ - no config hot reload — rule changes require a restart (each delivery still freezes ruleId + ruleVersion + rendered payload);
311
+ - no one-click manual resend command — `unknown` deliveries only guarantee no duplicate delivery row if resent (unique idempotency key); they are never auto-resent;
312
+ - no full backpressure (high-water marks, pausing ingress, disk quotas) — only per-account queue/process isolation and `rate_limited` backoff;
313
+ - no attachments, no rule UI, no multi-tenant permission model, no exactly-once
314
+ guarantee (inbound room notifications are idempotent per event and route, but
315
+ a crash between the ACK and Feishu's next retry can still lose an event).