@alexsun-top/opencode-chat-room 0.8.0 → 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 (161) hide show
  1. package/CHANGELOG.md +29 -0
  2. package/LICENSE +2 -2
  3. package/README.md +38 -6
  4. package/README.zh-CN.md +38 -6
  5. package/cli.ts +27 -1
  6. package/dist/cli.js +5074 -3212
  7. package/package.json +23 -3
  8. package/packages/chat-room-channeld/README.md +98 -25
  9. package/packages/chat-room-channeld/dist/feishu-adapter.js +912 -149
  10. package/packages/chat-room-channeld/src/adapter.ts +36 -2
  11. package/packages/chat-room-channeld/src/config.ts +63 -1
  12. package/packages/chat-room-channeld/src/delivery.ts +6 -1
  13. package/packages/chat-room-channeld/src/feishu-adapter.ts +122 -292
  14. package/packages/chat-room-channeld/src/feishu-card.ts +27 -0
  15. package/packages/chat-room-channeld/src/feishu-inbound.ts +158 -0
  16. package/packages/chat-room-channeld/src/feishu-live.ts +355 -0
  17. package/packages/chat-room-channeld/src/feishu-send.ts +342 -0
  18. package/packages/chat-room-channeld/src/feishu-ws-frames.ts +173 -0
  19. package/packages/chat-room-channeld/src/host-helpers.ts +11 -0
  20. package/packages/chat-room-channeld/src/host.ts +120 -2
  21. package/packages/chat-room-channeld/src/inbound.ts +26 -0
  22. package/packages/chat-room-channeld/src/state.ts +17 -0
  23. package/packages/chat-room-channeld/src/stats.ts +43 -1
  24. package/server/admin-state.ts +13 -13
  25. package/server/app.ts +95 -0
  26. package/server/assets/NOTICE.txt +2 -1
  27. package/server/assets/base.css +15 -9
  28. package/server/assets/chat/announcements.js +70 -7
  29. package/server/assets/chat/doc-preview.js +184 -35
  30. package/server/assets/chat/events.js +3 -0
  31. package/server/assets/chat/files.js +46 -4
  32. package/server/assets/chat/index.js +2 -0
  33. package/server/assets/chat/integration-editor.js +163 -0
  34. package/server/assets/chat/integration-values.js +124 -0
  35. package/server/assets/chat/integrations.js +498 -0
  36. package/server/assets/chat/locales/de.js +40 -4
  37. package/server/assets/chat/locales/es.js +40 -4
  38. package/server/assets/chat/locales/fr.js +40 -4
  39. package/server/assets/chat/locales/it.js +40 -4
  40. package/server/assets/chat/locales/ja.js +40 -4
  41. package/server/assets/chat/locales/ko.js +40 -4
  42. package/server/assets/chat/markdown.js +22 -8
  43. package/server/assets/chat/messages.js +5 -5
  44. package/server/assets/chat/office-preview.js +69 -0
  45. package/server/assets/chat/panels.js +36 -0
  46. package/server/assets/chat/recall.js +58 -7
  47. package/server/assets/chat/receipts.js +15 -3
  48. package/server/assets/chat/reminders.js +69 -8
  49. package/server/assets/chat/resource-format.js +11 -0
  50. package/server/assets/chat/settings.js +30 -2
  51. package/server/assets/chat/side-pane.js +1 -2
  52. package/server/assets/chat/state.js +93 -10
  53. package/server/assets/chat/system-events.js +32 -0
  54. package/server/assets/chat/system-messages.js +23 -14
  55. package/server/assets/chat/task-row.js +0 -6
  56. package/server/assets/chat/tasks.js +121 -33
  57. package/server/assets/code-highlight.css +55 -32
  58. package/server/assets/conversation.css +123 -23
  59. package/server/assets/doc-preview.css +35 -3
  60. package/server/assets/files.css +5 -0
  61. package/server/assets/fluent-icons.svg +2 -0
  62. package/server/assets/monaco/LICENSE +21 -0
  63. package/server/assets/monaco/ThirdPartyNotices.txt +448 -0
  64. package/server/assets/monaco/codicon-jdswhhtn.ttf +0 -0
  65. package/server/assets/monaco/editor.worker.js +45 -0
  66. package/server/assets/monaco/integration-rules.css +1 -0
  67. package/server/assets/monaco/integration-rules.js +1297 -0
  68. package/server/assets/monaco/json.worker.js +77 -0
  69. package/server/assets/office/THIRD-PARTY-NOTICES.txt +11 -0
  70. package/server/assets/office/docx.bundle.js +33 -0
  71. package/server/assets/office/licenses/docx-preview-LICENSE.txt +201 -0
  72. package/server/assets/office/licenses/echarts-LICENSE.txt +222 -0
  73. package/server/assets/office/licenses/jszip-LICENSE.txt +651 -0
  74. package/server/assets/office/licenses/lodash-LICENSE.txt +47 -0
  75. package/server/assets/office/licenses/pptx-preview-LICENSE.txt +8 -0
  76. package/server/assets/office/licenses/tslib-LICENSE.txt +12 -0
  77. package/server/assets/office/licenses/uuid-LICENSE.txt +9 -0
  78. package/server/assets/office/licenses/xlsx-LICENSE.txt +201 -0
  79. package/server/assets/office/licenses/zrender-LICENSE.txt +29 -0
  80. package/server/assets/office/pptx.bundle.js +104 -0
  81. package/server/assets/office/xlsx.bundle.js +141 -0
  82. package/server/assets/recall.css +5 -4
  83. package/server/assets/responsive.css +50 -11
  84. package/server/assets/settings.css +73 -1
  85. package/server/assets/tasks.css +57 -7
  86. package/server/channel-integration.ts +519 -0
  87. package/server/channel-settings.ts +77 -0
  88. package/server/channels.ts +261 -18
  89. package/server/chat-server.ts +32 -580
  90. package/server/chat.html +187 -21
  91. package/server/http-server.ts +3 -182
  92. package/server/resource-routes.ts +16 -16
  93. package/server/routes/inbox.ts +100 -0
  94. package/server/routes/pages.ts +83 -0
  95. package/server/routes/questions.ts +39 -0
  96. package/server/routes/room-actions.ts +162 -0
  97. package/server/routes/rooms.ts +160 -0
  98. package/server/startup.ts +84 -0
  99. package/server/sw.js +4 -0
  100. package/server/task-routes.ts +1 -1
  101. package/src/center/center-client.ts +2 -10
  102. package/src/config/file-config.ts +7 -54
  103. package/src/platform/http-server.ts +182 -0
  104. package/src/protocol/addressing.ts +21 -0
  105. package/src/protocol/config.ts +58 -0
  106. package/src/protocol/room.ts +51 -0
  107. package/src/rooms/resources.ts +26 -16
  108. package/src/rooms/room-service.ts +2 -48
  109. package/src/rooms/storage.ts +329 -0
  110. package/src/rooms/tasks.ts +21 -5
  111. package/src/runtime/watch.ts +1 -1
  112. package/src/state/pg-database.ts +183 -0
  113. package/src/state/pg-migrate.ts +175 -0
  114. package/src/state/pg-sql.mjs +175 -0
  115. package/src/state/pg-worker.mjs +148 -0
  116. package/src/state/sqlite-announcements.ts +25 -0
  117. package/src/state/sqlite-database.ts +121 -0
  118. package/src/state/sqlite-legacy.ts +90 -0
  119. package/src/state/sqlite-members.ts +155 -0
  120. package/src/state/sqlite-messages.ts +288 -0
  121. package/src/state/sqlite-resources.ts +39 -0
  122. package/src/state/sqlite-rooms.ts +87 -0
  123. package/src/state/sqlite-schema.ts +80 -0
  124. package/src/state/sqlite-state-store.ts +33 -0
  125. package/src/state/sqlite-state.ts +15 -897
  126. package/src/state/sqlite-tasks.ts +65 -0
  127. package/src/state/sqlite-transaction.ts +13 -0
  128. package/src/state/sqlite.ts +21 -1
  129. package/src/state/store.ts +5 -1
  130. package/src/tui/app.mjs +32 -19
  131. package/src/tui/config.mjs +12 -4
  132. package/src/tui/i18n.mjs +38 -0
  133. package/src/tui/info-app.mjs +50 -51
  134. package/src/tui/info-content.mjs +79 -44
  135. package/src/tui/join-app.mjs +9 -9
  136. package/src/tui/locales/en.mjs +349 -0
  137. package/src/tui/locales/zh-CN.mjs +349 -0
  138. package/src/tui/main.mjs +85 -37
  139. package/src/tui/markdown.mjs +3 -2
  140. package/src/tui/mentions.mjs +3 -1
  141. package/src/tui/message-app.mjs +22 -21
  142. package/src/tui/message-view.mjs +2 -1
  143. package/src/tui/palette-app.mjs +9 -8
  144. package/src/tui/palette.mjs +30 -24
  145. package/src/tui/prefs.mjs +92 -0
  146. package/src/tui/reminder-app.mjs +18 -17
  147. package/src/tui/room-settings-app.mjs +30 -28
  148. package/src/tui/settings-app.mjs +153 -0
  149. package/src/tui/ui-content.mjs +65 -86
  150. package/src/tui/ui-help.mjs +73 -0
  151. package/src/tui/ui-overlay-view.mjs +23 -7
  152. package/src/tui/ui-suggest.mjs +6 -5
  153. package/src/tui/ui-theme.mjs +37 -33
  154. package/src/tui/ui-view.mjs +22 -7
  155. package/src/tui/ui.mjs +15 -3
  156. package/src/web/editor.worker.ts +1 -0
  157. package/src/web/integration-rules-editor.ts +439 -0
  158. package/src/web/integration-rules.ts +22 -0
  159. package/src/web/integration-schema.ts +228 -0
  160. package/src/web/json.worker.ts +1 -0
  161. package/src/web/office-viewer.ts +221 -0
@@ -32,6 +32,8 @@ export interface AdapterContext {
32
32
  onIngest(event: IngestEvent): Promise<{ duplicate: boolean; eventId?: string }>;
33
33
  onExit(code: number | null, signal: NodeJS.Signals | null): void;
34
34
  onUnavailable?(reason: string): void;
35
+ /** Optional adapter health, e.g. inbound long-connection readiness. */
36
+ onStatus?(status: { inbound?: string; detail?: string }): void;
35
37
  }
36
38
 
37
39
  export interface AdapterProcessOptions extends AdapterContext {
@@ -64,6 +66,7 @@ export class AdapterProcess {
64
66
  readonly #onIngest: AdapterContext["onIngest"];
65
67
  readonly #onExit: AdapterContext["onExit"];
66
68
  readonly #onUnavailable: AdapterContext["onUnavailable"];
69
+ readonly #onStatus: AdapterContext["onStatus"];
67
70
  readonly #initializeTimeoutMs: number;
68
71
  readonly #sendTimeoutMs: number;
69
72
  readonly #pending = new Map<string, PendingCall>();
@@ -81,6 +84,7 @@ export class AdapterProcess {
81
84
  this.#onIngest = options.onIngest;
82
85
  this.#onExit = options.onExit;
83
86
  this.#onUnavailable = options.onUnavailable;
87
+ this.#onStatus = options.onStatus;
84
88
  this.#initializeTimeoutMs = options.initializeTimeoutMs ?? DEFAULT_INITIALIZE_TIMEOUT_MS;
85
89
  this.#sendTimeoutMs = options.sendTimeoutMs ?? DEFAULT_SEND_TIMEOUT_MS;
86
90
  }
@@ -97,6 +101,8 @@ export class AdapterProcess {
97
101
  stdio: ["pipe", "pipe", "pipe"],
98
102
  env: {
99
103
  ...process.env,
104
+ ...this.#account.env,
105
+ // Forced after account env: config validation also rejects these keys.
100
106
  CHANNELD_ACCOUNT_ID: this.#account.id,
101
107
  CHANNELD_PLATFORM: this.#account.platform,
102
108
  },
@@ -105,7 +111,7 @@ export class AdapterProcess {
105
111
  this.#attach(child);
106
112
 
107
113
  try {
108
- await this.#call(
114
+ const initialized = await this.#call(
109
115
  "initialize",
110
116
  {
111
117
  protocolVersions: [PROTOCOL_VERSION],
@@ -117,11 +123,13 @@ export class AdapterProcess {
117
123
  },
118
124
  this.#initializeTimeoutMs,
119
125
  );
120
- await this.#call(
126
+ this.#assertReportedPlatform("initialize", initialized);
127
+ const started = await this.#call(
121
128
  "start",
122
129
  { accounts: [this.#account.id], cursors: {} },
123
130
  this.#initializeTimeoutMs,
124
131
  );
132
+ this.#assertReportedPlatform("start", started);
125
133
  } catch (error) {
126
134
  const tail = this.#stderrTail.trim();
127
135
  const message = this.#pollutionError?.message
@@ -212,6 +220,21 @@ export class AdapterProcess {
212
220
  }
213
221
  }
214
222
 
223
+ /** A result may declare its platform; a mismatch makes this account unusable. */
224
+ #assertReportedPlatform(method: string, result: unknown): void {
225
+ if (!result || typeof result !== "object") return;
226
+ const reported = (result as { platform?: unknown }).platform;
227
+ if (reported === undefined) return;
228
+ if (typeof reported !== "string" || !reported.trim()) {
229
+ throw new Error(`${method} returned an invalid platform`);
230
+ }
231
+ if (reported !== this.#account.platform) {
232
+ throw new Error(
233
+ `${method} reported platform "${reported}" but account ${this.#account.id} is configured for "${this.#account.platform}"`,
234
+ );
235
+ }
236
+ }
237
+
215
238
  #attach(child: ChildProcessWithoutNullStreams): void {
216
239
  child.stdin.on("error", (error: Error) => this.#handleStdioError("stdin", error));
217
240
  child.stdout.on("error", (error: Error) => this.#handleStdioError("stdout", error));
@@ -297,6 +320,17 @@ export class AdapterProcess {
297
320
  child.stdin,
298
321
  rpcResult(id, result.duplicate ? { duplicate: true } : { committed: true }),
299
322
  );
323
+ } else if (method === "status") {
324
+ const input = (params ?? {}) as { inbound?: unknown; detail?: unknown };
325
+ try {
326
+ this.#onStatus?.({
327
+ inbound: typeof input.inbound === "string" ? input.inbound : undefined,
328
+ detail: typeof input.detail === "string" ? input.detail : undefined,
329
+ });
330
+ } catch {
331
+ // Status is advisory; a consumer failure must not break the adapter.
332
+ }
333
+ await writeFrame(child.stdin, rpcResult(id, { ok: true }));
300
334
  } else if (method === "ping") {
301
335
  await writeFrame(child.stdin, rpcResult(id, { pong: true }));
302
336
  } else {
@@ -31,12 +31,22 @@ export interface AccountConfig {
31
31
  id: string;
32
32
  platform: string;
33
33
  adapter: { command: string[]; runtime?: string };
34
+ /** Extra environment variables passed only to this account's adapter process. */
35
+ env?: Record<string, string>;
36
+ }
37
+
38
+ /** A center-only route: an inbound IM event becomes a room notification. */
39
+ export interface InboundRoute {
40
+ from: RuleSource;
41
+ to: { room: string };
34
42
  }
35
43
 
36
44
  export interface ChannelConfig {
37
45
  stateFile: string;
38
46
  accounts: AccountConfig[];
39
47
  rules: RuleConfig[];
48
+ /** Room notifications for inbound events; never matched by outbound rules. */
49
+ inbound: InboundRoute[];
40
50
  }
41
51
 
42
52
  type RecordValue = Record<string, unknown>;
@@ -49,6 +59,11 @@ function record(value: unknown, where: string): RecordValue {
49
59
  }
50
60
 
51
61
  const IDENTIFIER = /^[A-Za-z0-9._-]+$/;
62
+ const ENV_KEY = /^[A-Za-z_][A-Za-z0-9_]*$/;
63
+ const MAX_ACCOUNT_ENV = 64;
64
+ const MAX_ENV_VALUE = 8192;
65
+ // The host writes these two after the account env, so a config must not claim them.
66
+ const RESERVED_ENV = new Set(["CHANNELD_ACCOUNT_ID", "CHANNELD_PLATFORM"]);
52
67
 
53
68
  function stringValue(value: unknown, where: string): string {
54
69
  if (typeof value !== "string" || !value.trim()) {
@@ -108,6 +123,34 @@ function parseTransform(value: unknown, where: string): Transform {
108
123
  }
109
124
  }
110
125
 
126
+ function parseAccountEnv(value: unknown, where: string): Record<string, string> | undefined {
127
+ if (value === undefined) return undefined;
128
+ const raw = record(value, where);
129
+ const entries = Object.entries(raw);
130
+ if (entries.length > MAX_ACCOUNT_ENV) {
131
+ throw new ConfigError(`config: ${where} must contain at most ${MAX_ACCOUNT_ENV} variables`);
132
+ }
133
+ const env: Array<[string, string]> = [];
134
+ for (const [key, item] of entries) {
135
+ if (!ENV_KEY.test(key)) {
136
+ throw new ConfigError(
137
+ `config: ${where}.${key} must match /^[A-Za-z_][A-Za-z0-9_]*$/ (letters, digits, underscore; cannot start with a digit)`,
138
+ );
139
+ }
140
+ if (RESERVED_ENV.has(key)) {
141
+ throw new ConfigError(`config: ${where}.${key} is reserved by the channeld protocol`);
142
+ }
143
+ if (typeof item !== "string") {
144
+ throw new ConfigError(`config: ${where}.${key} must be a string`);
145
+ }
146
+ if (item.length > MAX_ENV_VALUE) {
147
+ throw new ConfigError(`config: ${where}.${key} must be at most ${MAX_ENV_VALUE} characters`);
148
+ }
149
+ env.push([key, item]);
150
+ }
151
+ return Object.fromEntries(env);
152
+ }
153
+
111
154
  function parseAccount(value: unknown, where: string): AccountConfig {
112
155
  const raw = record(value, where);
113
156
  const adapter = record(raw.adapter, `${where}.adapter`);
@@ -118,6 +161,20 @@ function parseAccount(value: unknown, where: string): AccountConfig {
118
161
  id: identifierValue(raw.id, `${where}.id`),
119
162
  platform: identifierValue(raw.platform, `${where}.platform`),
120
163
  adapter: { command, runtime: optionalString(adapter.runtime, `${where}.adapter.runtime`) },
164
+ env: parseAccountEnv(raw.env, `${where}.env`),
165
+ };
166
+ }
167
+
168
+ function parseInbound(value: unknown, where: string): InboundRoute {
169
+ const raw = record(value, where);
170
+ const from = record(raw.from, `${where}.from`);
171
+ const to = record(raw.to, `${where}.to`);
172
+ return {
173
+ from: {
174
+ ...parseTarget({ platform: from.platform, account: from.account, room: from.room }, `${where}.from`),
175
+ event: optionalString(from.event, `${where}.from.event`),
176
+ },
177
+ to: { room: stringValue(to.room, `${where}.to.room`) },
121
178
  };
122
179
  }
123
180
 
@@ -170,6 +227,11 @@ export function parseConfig(value: unknown): ChannelConfig {
170
227
  const rules = raw.rules === undefined
171
228
  ? []
172
229
  : arrayValueAllowEmpty(raw.rules, "rules").map((item, index) => parseRule(item, `rules[${index}]`));
230
+ const inbound = raw.inbound === undefined
231
+ ? []
232
+ : arrayValueAllowEmpty(raw.inbound, "inbound").map((item, index) =>
233
+ parseInbound(item, `inbound[${index}]`),
234
+ );
173
235
 
174
236
  const accountIds = new Set<string>();
175
237
  for (const account of accounts) {
@@ -182,7 +244,7 @@ export function parseConfig(value: unknown): ChannelConfig {
182
244
  if (ruleKeys.has(key)) throw new ConfigError(`config: duplicate rule "${rule.id}" version ${rule.version}`);
183
245
  ruleKeys.add(key);
184
246
  }
185
- return { stateFile, accounts, rules };
247
+ return { stateFile, accounts, rules, inbound };
186
248
  }
187
249
 
188
250
  /** Load a config file. YAML parsing also accepts JSON documents. */
@@ -71,11 +71,16 @@ export function applyReceipt(
71
71
  providerMessage: providerMessage ?? null,
72
72
  lastError: null,
73
73
  });
74
+ const event = state.getEvent(row.eventId);
74
75
  const echo = receipt.providerMessage?.nativeMessageId && receipt.providerMessage.endpoint
75
76
  ? {
76
77
  endpoint: targetKey(receipt.providerMessage.endpoint),
77
78
  nativeMessageId: receipt.providerMessage.nativeMessageId,
78
- logicalMessageId: row.eventId,
79
+ // The room's logical id is the selected message for a forward, the
80
+ // provider message for an adapter ingest, and the event id otherwise.
81
+ logicalMessageId: event?.selectedMessage?.nativeMessageId
82
+ ?? event?.message?.nativeMessageId
83
+ ?? row.eventId,
79
84
  }
80
85
  : undefined;
81
86
  state.recordSent({