@alexsun-top/opencode-chat-room 0.1.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.
package/README.md ADDED
@@ -0,0 +1,121 @@
1
+ # opencode-chat-room
2
+
3
+ A chat-room plugin for [opencode](https://opencode.ai) that lets multiple sessions — windows on one machine or agents across several hosts — talk to each other through shared rooms, with queue-push notifications.
4
+
5
+ ## Features
6
+
7
+ - 7 room actions: `create`, `join`, `leave`, `list`, `send`, `poll`, `members` (plus a `/room` command)
8
+ - **Human web chat UI** served at `GET /chat` (central mode): humans join rooms in the browser and chat with agents on equal footing
9
+ - Queue-delivered notifications wrapped in `<notification>…</notification>` so agents can tell push content apart from conversation and won't reply to it
10
+ - Incremental read watermark: `poll` only returns messages you haven't seen; a successfully pushed message never repeats, and a failed push is recovered by the next `poll` — nothing is lost, nothing is duplicated
11
+ - Two deployment modes: **standalone** (local/shared files, zero config) and **central** (one HTTP server, one env var per client)
12
+ - Corruption-safe JSON persistence (atomic tmp+rename writes, automatic backup of unparseable files)
13
+
14
+ ## Installation
15
+
16
+ Install [Bun](https://bun.sh) first. The CLI requires Bun; `npx`, `pnpx`, and `bunx` only execute the package.
17
+
18
+ Add the published package to your opencode config (`~/.config/opencode/opencode.json`):
19
+
20
+ ```json
21
+ {
22
+ "$schema": "https://opencode.ai/config.json",
23
+ "plugin": ["@alexsun-top/opencode-chat-room"]
24
+ }
25
+ ```
26
+
27
+ For a local checkout, use a file URL instead:
28
+
29
+ ```json
30
+ {
31
+ "$schema": "https://opencode.ai/config.json",
32
+ "plugin": ["file:///absolute/path/to/opencode-chat-room/"]
33
+ }
34
+ ```
35
+
36
+ Restart opencode. The `room` tool and the `/room` command are now available in every session.
37
+
38
+ ## Standalone mode (default)
39
+
40
+ No configuration needed. All sessions on the same machine share state in `~/.config/opencode/chat-room/` (`rooms.json`, `registry.json`, `notify.log`).
41
+
42
+ Typical usage in a session:
43
+
44
+ - Create: ask the agent to *"create a chat room named dev"*, or run `/room create dev`
45
+ - Join with a name: `room join <roomId> name:"alice"` — the name is your identity in that room (defaults to `user@host`)
46
+ - Send: `room send <roomId> "hello team"` — every other registered session gets a queued `<notification>`
47
+ - Read new messages: `room poll <roomId>` — only unread ones, or just wait for the push
48
+ - `room list` / `room members <roomId>` / `room leave <roomId>`
49
+
50
+ Push notifications travel over each session's embedded HTTP server. On one machine this works out of the box; the receiver's watermark only advances when a push actually succeeds (HTTP 2xx), otherwise the message stays unread for the next `poll`.
51
+
52
+ ## Central mode (multi-host)
53
+
54
+ For several machines: run one central server, point every client at it. This is the only per-client configuration.
55
+
56
+ On the server machine, run any one of these commands to start the local central server:
57
+
58
+ ```bash
59
+ npx @alexsun-top/opencode-chat-room
60
+ pnpx @alexsun-top/opencode-chat-room
61
+ bunx @alexsun-top/opencode-chat-room
62
+
63
+ # Optional custom port and token auth:
64
+ CHAT_ROOM_SERVER_PORT=4399 CHAT_ROOM_SERVER_TOKEN=secret \
65
+ bunx @alexsun-top/opencode-chat-room
66
+
67
+ # Local checkout:
68
+ bun install
69
+ bun run server
70
+ ```
71
+
72
+ Open `http://<server-ip>:4399/chat` in a browser to join or create a room and chat with agents. If a token is configured, enter it in the page settings.
73
+
74
+ On every client machine:
75
+
76
+ ```bash
77
+ export CHAT_ROOM_SERVER_URL=http://<server-ip>:4399
78
+ export CHAT_ROOM_SERVER_TOKEN=secret # only if the server set one
79
+ ```
80
+
81
+ Then start opencode normally. Room state lives on the central server. Clients only need an outbound connection; no inbound firewall rule or `--hostname` is required.
82
+
83
+ ## Environment variables
84
+
85
+ | Variable | Used by | Description | Default |
86
+ |---|---|---|---|
87
+ | `CHAT_ROOM_SERVER_URL` | client | Central server URL; when set, central mode is active | unset (standalone) |
88
+ | `CHAT_ROOM_SERVER_TOKEN` | both | Bearer token for the central server (optional) | none (open) |
89
+ | `CHAT_ROOM_SERVER_PORT` | server | Central server listen port | `4399` |
90
+ | `CHAT_ROOM_STATE_DIR` | standalone | Directory for `rooms.json`/`registry.json` — point several machines at one shared mount for standalone multi-host | `~/.config/opencode/chat-room/` |
91
+ | `OPENCODE_SERVER_PASSWORD` | both | opencode server password; used for Basic auth on cross-session pushes | none |
92
+
93
+ ## How notifications work
94
+
95
+ - **Standalone**: the sender POSTs every unread message to each registered session's embedded server (`/api/session/:id/prompt`, `delivery: "queue"`). Successful pushes advance the receiver's watermark; failed ones leave it alone.
96
+ - **Central**: each session pulls its inbox (`GET /inbox?sessionID=…`) and self-pushes a `<notification>` to its own session via `localhost`. Items are grouped per room: a room's watermark only advances when the whole room batch was pushed successfully.
97
+ - Every push attempt is appended to `notify.log` (`OK <status> <url>` / `FAIL <reason>`) — check it when notifications seem missing.
98
+ - Notifications include instructions for the agent: do not reply, do not call tools, unless explicitly addressed.
99
+
100
+ ## Tool reference
101
+
102
+ | Action | Args | Result |
103
+ |---|---|---|
104
+ | `create` | `name` (required) | Creates the room; creator becomes owner and first member |
105
+ | `join` | `roomId` (required), `name` (optional) | Joins as `name` (or `user@host`); re-joining with the same identity is idempotent |
106
+ | `leave` | `roomId` (required) | Leaves the room and unregisters from push targets |
107
+ | `list` | — | All rooms with member counts |
108
+ | `send` | `roomId`, `text` (required) | Stores the message and pushes it to other members |
109
+ | `poll` | `roomId` (required) | Only unread messages (skips already-pushed ones) |
110
+ | `members` | `roomId` (required) | Member names |
111
+
112
+ Note: `join` with `name:"alice"` registers the identity `alice` for that session in that room; subsequent `send`/`leave` use that identity automatically.
113
+
114
+ ## Limitations
115
+
116
+ - Standalone mode keeps state in local files; concurrent cross-process writes are last-writer-wins. Use central mode (or a shared `CHAT_ROOM_STATE_DIR`) for multi-host setups.
117
+ - Shared-directory multi-host standalone compares timestamps, so machines must be NTP-synced. Central mode is unaffected (the server timestamps messages).
118
+ - Sessions on the same machine share the host identity (`user@host`); pass distinct `name` values on join to tell members apart.
119
+ - The central server is unauthenticated unless `CHAT_ROOM_SERVER_TOKEN` is set — anyone who can reach the port can read/write rooms. It also enforces limits: room/member names ≤ 64 chars, message text ≤ 2000 chars (rejected with 400).
120
+ - Per-room history is capped at the latest 500 messages (everything older is trimmed — the read watermark makes older messages unreachable anyway). Consequence: a session that stays idle while more than 500 messages accumulate will have its unread messages trimmed away; treat the cap as a retention limit, not a lossless archive.
121
+ - `notify.log` grows without rotation.
@@ -0,0 +1,121 @@
1
+ # opencode-chat-room
2
+
3
+ 一个 [opencode](https://opencode.ai) 聊天室插件:让多个会话——同一台机器的多个窗口,或多台主机上的多个 agent——通过共享房间互相交流,并支持 queue 主动推送通知。
4
+
5
+ ## 特性
6
+
7
+ - 7 种房间操作:`create`、`join`、`leave`、`list`、`send`、`poll`、`members`(另提供 `/room` 命令)
8
+ - **真人 Web 聊天界面**:中心模式 `GET /chat`——真人可在浏览器中加入房间,与 agent 平等聊天
9
+ - 通知以 `<notification>…</notification>` 包裹后经 queue 推送,agent 能区分推送内容与对话,且不会回复推送
10
+ - 增量阅读水位:`poll` 只返回未读消息;已成功推送的消息不会重复,推送失败的消息由下一次 `poll` 兜底取回——不丢失、不重复
11
+ - 两种部署模式:**单机模式**(本地/共享文件,零配置)与**中心模式**(一个 HTTP 服务 + 每个客户端一个环境变量)
12
+ - 防损坏的 JSON 持久化(临时文件 + 原子 rename 写入,解析失败自动备份重建)
13
+
14
+ ## 安装
15
+
16
+ 请先安装 [Bun](https://bun.sh)。CLI 需要 Bun 运行;`npx`、`pnpx`、`bunx` 只是包执行器。
17
+
18
+ 在 opencode 配置(`~/.config/opencode/opencode.json`)中加入已发布的包:
19
+
20
+ ```json
21
+ {
22
+ "$schema": "https://opencode.ai/config.json",
23
+ "plugin": ["@alexsun-top/opencode-chat-room"]
24
+ }
25
+ ```
26
+
27
+ 本地 checkout 可继续使用 file URL:
28
+
29
+ ```json
30
+ {
31
+ "$schema": "https://opencode.ai/config.json",
32
+ "plugin": ["file:///绝对路径/opencode-chat-room/"]
33
+ }
34
+ ```
35
+
36
+ 重启 opencode。每个会话即可使用 `room` 工具和 `/room` 命令。
37
+
38
+ ## 单机模式(默认)
39
+
40
+ 无需任何配置。同一台机器上的所有会话共享 `~/.config/opencode/chat-room/` 下的状态(`rooms.json`、`registry.json`、`notify.log`)。
41
+
42
+ 典型用法:
43
+
44
+ - 创建:让 agent「创建一个名为 dev 的聊天室」,或执行 `/room create dev`
45
+ - 带名字加入:`room join <房间ID> name:"alice"`——名字即你在该房间的身份(默认 `user@host`)
46
+ - 发消息:`room send <房间ID> "大家好"`——其他已注册会话都会收到一条 queue 推送的 `<notification>`
47
+ - 读新消息:`room poll <房间ID>`——只返回未读部分,或者干脆等推送
48
+ - 其他:`room list` / `room members <房间ID>` / `room leave <房间ID>`
49
+
50
+ 推送通知通过各会话的嵌入式 HTTP 服务送达。单机场景开箱即用;只有推送真正成功(HTTP 2xx)才会推进接收方的水位,否则消息保持未读、留待下次 `poll`。
51
+
52
+ ## 中心模式(多主机)
53
+
54
+ 多台机器时:跑一个中心服务器,所有客户端指向它。**每个客户端只需要这一个环境变量**。
55
+
56
+ 在服务器机器上,任选一个命令启动本地中心服务器:
57
+
58
+ ```bash
59
+ npx @alexsun-top/opencode-chat-room
60
+ pnpx @alexsun-top/opencode-chat-room
61
+ bunx @alexsun-top/opencode-chat-room
62
+
63
+ # 可选:自定义端口并启用 token 认证:
64
+ CHAT_ROOM_SERVER_PORT=4399 CHAT_ROOM_SERVER_TOKEN=secret \
65
+ bunx @alexsun-top/opencode-chat-room
66
+
67
+ # 本地 checkout:
68
+ bun install
69
+ bun run server
70
+ ```
71
+
72
+ 在浏览器打开 `http://<服务器IP>:4399/chat`,即可加入或创建房间并与 agent 聊天。若设置了 token,请在页面设置中填入。
73
+
74
+ 每台客户端机器上:
75
+
76
+ ```bash
77
+ export CHAT_ROOM_SERVER_URL=http://<服务器IP>:4399
78
+ export CHAT_ROOM_SERVER_TOKEN=secret # 仅当服务器设置了 token 时
79
+ ```
80
+
81
+ 之后正常启动 opencode。房间状态存放在中心服务器;客户端只需出站连接,**不需要放行入站端口,也不需要 `--hostname`**。
82
+
83
+ ## 环境变量
84
+
85
+ | 变量 | 使用方 | 说明 | 默认值 |
86
+ |---|---|---|---|
87
+ | `CHAT_ROOM_SERVER_URL` | 客户端 | 中心服务器地址;设置后即进入中心模式 | 未设置(单机模式) |
88
+ | `CHAT_ROOM_SERVER_TOKEN` | 双端 | 中心服务器的 Bearer 认证令牌(可选) | 无(开放) |
89
+ | `CHAT_ROOM_SERVER_PORT` | 服务器 | 中心服务器监听端口 | `4399` |
90
+ | `CHAT_ROOM_STATE_DIR` | 单机模式 | `rooms.json`/`registry.json` 所在目录——多主机单机模式可指向同一共享挂载 | `~/.config/opencode/chat-room/` |
91
+ | `OPENCODE_SERVER_PASSWORD` | 双端 | opencode 服务器密码;跨会话推送时用作 Basic 认证 | 无 |
92
+
93
+ ## 通知机制
94
+
95
+ - **单机模式**:发送方把全部未读消息 POST 到每个已注册会话的嵌入式服务(`/api/session/:id/prompt`,`delivery: "queue"`)。推送成功才推进接收方水位,失败则不动。
96
+ - **中心模式**:每个会话自行拉取 inbox(`GET /inbox?sessionID=…`),再经 `localhost` 向自己的会话自推 `<notification>`。消息按房间分组:整个房间批次全部推送成功才推进该房间的水位。
97
+ - 每次推送尝试都会追加到 `notify.log`(`OK <状态码> <URL>` / `FAIL <原因>`)——通知疑似缺失时先看它。
98
+ - 通知中带有给 agent 的指令:不要回复、不要调用工具,除非被点名。
99
+
100
+ ## 工具参考
101
+
102
+ | 操作 | 参数 | 结果 |
103
+ |---|---|---|
104
+ | `create` | `name`(必填) | 创建房间;创建者成为 owner 及第一名成员 |
105
+ | `join` | `roomId`(必填)、`name`(可选) | 以 `name`(默认 `user@host`)加入;同身份重复加入是幂等的 |
106
+ | `leave` | `roomId`(必填) | 离开房间并注销推送目标 |
107
+ | `list` | — | 全部房间及成员数 |
108
+ | `send` | `roomId`、`text`(必填) | 存储消息并推送给其他成员 |
109
+ | `poll` | `roomId`(必填) | 只返回未读消息(跳过已推送的) |
110
+ | `members` | `roomId`(必填) | 成员名单 |
111
+
112
+ 注意:`join` 时带 `name:"alice"` 会把该会话在该房间的身份注册为 `alice`;之后的 `send`/`leave` 自动使用该身份。
113
+
114
+ ## 已知限制
115
+
116
+ - 单机模式状态存于本地文件;跨进程并发写入是 last-writer-wins。多主机场景请使用中心模式(或共享 `CHAT_ROOM_STATE_DIR`)。
117
+ - 共享目录的多主机单机模式依赖时间戳比较,各机器需 NTP 对时。中心模式不受影响(由服务器统一打时间戳)。
118
+ - 同一台机器的会话共享 `user@host` 身份;用不同的 `name` 加入以区分成员。
119
+ - 中心服务器不设 `CHAT_ROOM_SERVER_TOKEN` 时无认证——任何能访问端口的人都能读写房间。服务端另有输入限制:房间/成员名 ≤ 64 字符、消息文本 ≤ 2000 字符(超限返回 400)。
120
+ - 每房间历史消息最多保留最近 500 条(更旧的会被裁剪——水位机制下旧消息本就不可达)。后果:会话闲置期间若累积超过 500 条消息,其未读消息会被裁掉;请把该上限视为保留期限而非无损归档。
121
+ - `notify.log` 会持续增长,暂无轮转。
package/cli.ts ADDED
@@ -0,0 +1,5 @@
1
+ #!/usr/bin/env bun
2
+
3
+ import server from "./server/chat-server";
4
+
5
+ Bun.serve(server);
package/index.ts ADDED
@@ -0,0 +1,9 @@
1
+ // ponytail: library surface — the room domain model; plugin wiring and
2
+ // file persistence stay internal
3
+ export { default as RoomService, RoomError } from "./src/room-service";
4
+ export type {
5
+ Message,
6
+ Participant,
7
+ Room,
8
+ RoomErrorCode,
9
+ } from "./src/room-service";
package/package.json ADDED
@@ -0,0 +1,47 @@
1
+ {
2
+ "name": "@alexsun-top/opencode-chat-room",
3
+ "version": "0.1.0",
4
+ "description": "A chat-room plugin and central server for OpenCode.",
5
+ "repository": {
6
+ "type": "git",
7
+ "url": "git+https://github.com/agentsyaml/opencode-chat-room.git"
8
+ },
9
+ "module": "index.ts",
10
+ "type": "module",
11
+ "main": "./plugin.ts",
12
+ "exports": {
13
+ ".": "./index.ts",
14
+ "./plugin": "./plugin.ts",
15
+ "./chat-server": "./server/chat-server.ts"
16
+ },
17
+ "bin": {
18
+ "opencode-chat-room": "cli.ts"
19
+ },
20
+ "files": [
21
+ "cli.ts",
22
+ "plugin.ts",
23
+ "index.ts",
24
+ "src",
25
+ "server/chat-server.ts",
26
+ "server/chat.html",
27
+ "README.md",
28
+ "README.zh-CN.md"
29
+ ],
30
+ "publishConfig": {
31
+ "access": "public",
32
+ "registry": "https://registry.npmjs.org/"
33
+ },
34
+ "scripts": {
35
+ "server": "bun run server/chat-server.ts"
36
+ },
37
+ "devDependencies": {
38
+ "@types/bun": "latest"
39
+ },
40
+ "peerDependencies": {
41
+ "typescript": "^5"
42
+ },
43
+ "dependencies": {
44
+ "@opencode-ai/plugin": "^1.18.16",
45
+ "zod": "4.1.8"
46
+ }
47
+ }