@alexsun-top/opencode-chat-room 0.1.0 → 0.1.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.
package/README.md CHANGED
@@ -53,16 +53,21 @@ Push notifications travel over each session's embedded HTTP server. On one machi
53
53
 
54
54
  For several machines: run one central server, point every client at it. This is the only per-client configuration.
55
55
 
56
- On the server machine, run any one of these commands to start the local central server:
56
+ On the server machine, install Bun first. `npx`, `pnpx`, and `bunx` only execute the package; the CLI itself runs on Bun. CLI options override environment variables.
57
57
 
58
58
  ```bash
59
- npx @alexsun-top/opencode-chat-room
60
- pnpx @alexsun-top/opencode-chat-room
61
- bunx @alexsun-top/opencode-chat-room
59
+ # Show CLI options:
60
+ npx --yes --package=@alexsun-top/opencode-chat-room@0.1.1 -- opencode-chat-room --help
62
61
 
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
62
+ # Start the local central server (choose any one runner):
63
+ npx --yes --package=@alexsun-top/opencode-chat-room@0.1.1 -- opencode-chat-room --host 0.0.0.0 --port 4399
64
+ pnpx --package=@alexsun-top/opencode-chat-room@0.1.1 opencode-chat-room --host 0.0.0.0 --port 4399
65
+ bunx @alexsun-top/opencode-chat-room@0.1.1 --host 0.0.0.0 --port 4399 --token secret
66
+
67
+ # Environment variables also work; CLI options take precedence:
68
+ CHAT_ROOM_SERVER_HOST=127.0.0.1 CHAT_ROOM_SERVER_PORT=4399 \
69
+ CHAT_ROOM_SERVER_TOKEN=secret \
70
+ bunx @alexsun-top/opencode-chat-room@0.1.1
66
71
 
67
72
  # Local checkout:
68
73
  bun install
@@ -86,7 +91,10 @@ Then start opencode normally. Room state lives on the central server. Clients on
86
91
  |---|---|---|---|
87
92
  | `CHAT_ROOM_SERVER_URL` | client | Central server URL; when set, central mode is active | unset (standalone) |
88
93
  | `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` |
94
+ | `CHAT_ROOM_SERVER_HOST` | server | Bind hostname; takes precedence over `HOST` | `0.0.0.0` |
95
+ | `HOST` | server | Host alias, used when `CHAT_ROOM_SERVER_HOST` is unset | unset |
96
+ | `CHAT_ROOM_SERVER_PORT` | server | Central server listen port; takes precedence over `PORT` | `4399` |
97
+ | `PORT` | server | Port alias, used when `CHAT_ROOM_SERVER_PORT` is unset | unset |
90
98
  | `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
99
  | `OPENCODE_SERVER_PASSWORD` | both | opencode server password; used for Basic auth on cross-session pushes | none |
92
100
 
package/README.zh-CN.md CHANGED
@@ -53,16 +53,21 @@
53
53
 
54
54
  多台机器时:跑一个中心服务器,所有客户端指向它。**每个客户端只需要这一个环境变量**。
55
55
 
56
- 在服务器机器上,任选一个命令启动本地中心服务器:
56
+ 在服务器机器上请先安装 Bun。`npx`、`pnpx`、`bunx` 只是包执行器,CLI 本身运行在 Bun 上;CLI 参数优先于环境变量。
57
57
 
58
58
  ```bash
59
- npx @alexsun-top/opencode-chat-room
60
- pnpx @alexsun-top/opencode-chat-room
61
- bunx @alexsun-top/opencode-chat-room
59
+ # 查看 CLI 选项:
60
+ npx --yes --package=@alexsun-top/opencode-chat-room@0.1.1 -- opencode-chat-room --help
62
61
 
63
- # 可选:自定义端口并启用 token 认证:
64
- CHAT_ROOM_SERVER_PORT=4399 CHAT_ROOM_SERVER_TOKEN=secret \
65
- bunx @alexsun-top/opencode-chat-room
62
+ # 启动本地中心服务器(三选一):
63
+ npx --yes --package=@alexsun-top/opencode-chat-room@0.1.1 -- opencode-chat-room --host 0.0.0.0 --port 4399
64
+ pnpx --package=@alexsun-top/opencode-chat-room@0.1.1 opencode-chat-room --host 0.0.0.0 --port 4399
65
+ bunx @alexsun-top/opencode-chat-room@0.1.1 --host 0.0.0.0 --port 4399 --token secret
66
+
67
+ # 也可以使用环境变量;CLI 参数优先:
68
+ CHAT_ROOM_SERVER_HOST=127.0.0.1 CHAT_ROOM_SERVER_PORT=4399 \
69
+ CHAT_ROOM_SERVER_TOKEN=secret \
70
+ bunx @alexsun-top/opencode-chat-room@0.1.1
66
71
 
67
72
  # 本地 checkout:
68
73
  bun install
@@ -86,7 +91,10 @@ export CHAT_ROOM_SERVER_TOKEN=secret # 仅当服务器设置了 token 时
86
91
  |---|---|---|---|
87
92
  | `CHAT_ROOM_SERVER_URL` | 客户端 | 中心服务器地址;设置后即进入中心模式 | 未设置(单机模式) |
88
93
  | `CHAT_ROOM_SERVER_TOKEN` | 双端 | 中心服务器的 Bearer 认证令牌(可选) | 无(开放) |
89
- | `CHAT_ROOM_SERVER_PORT` | 服务器 | 中心服务器监听端口 | `4399` |
94
+ | `CHAT_ROOM_SERVER_HOST` | 服务器 | 监听主机名;优先于 `HOST` | `0.0.0.0` |
95
+ | `HOST` | 服务器 | 主机名别名,仅在未设置 `CHAT_ROOM_SERVER_HOST` 时使用 | 未设置 |
96
+ | `CHAT_ROOM_SERVER_PORT` | 服务器 | 中心服务器监听端口;优先于 `PORT` | `4399` |
97
+ | `PORT` | 服务器 | 端口别名,仅在未设置 `CHAT_ROOM_SERVER_PORT` 时使用 | 未设置 |
90
98
  | `CHAT_ROOM_STATE_DIR` | 单机模式 | `rooms.json`/`registry.json` 所在目录——多主机单机模式可指向同一共享挂载 | `~/.config/opencode/chat-room/` |
91
99
  | `OPENCODE_SERVER_PASSWORD` | 双端 | opencode 服务器密码;跨会话推送时用作 Basic 认证 | 无 |
92
100
 
package/cli.ts CHANGED
@@ -1,5 +1,99 @@
1
1
  #!/usr/bin/env bun
2
2
 
3
- import server from "./server/chat-server";
3
+ const usage = `Usage: opencode-chat-room [options]
4
4
 
5
+ Options:
6
+ -h, --help Show this help
7
+ -v, --version Show the package version
8
+ -H, --host <host> Bind hostname
9
+ -p, --port <port> Bind port (1-65535)
10
+ -t, --token <token> Enable bearer token authentication`;
11
+
12
+ type ValueOption = "host" | "port" | "token";
13
+ const values: Partial<Record<ValueOption, string>> = {};
14
+ let help = false;
15
+ let version = false;
16
+
17
+ function fail(message: string): never {
18
+ console.error(`Error: ${message}\n\n${usage}`);
19
+ process.exit(1);
20
+ }
21
+
22
+ function setValue(name: ValueOption, value: string | undefined): void {
23
+ if (value === undefined || value === "") {
24
+ fail(`Missing value for --${name}`);
25
+ }
26
+ values[name] = value;
27
+ }
28
+
29
+ function validatePort(value: string): void {
30
+ const port = Number(value);
31
+ if (!Number.isInteger(port) || port < 1 || port > 65535) {
32
+ fail(`Invalid port "${value}": expected an integer from 1 to 65535`);
33
+ }
34
+ }
35
+
36
+ const args = process.argv.slice(2);
37
+ for (let i = 0; i < args.length; i += 1) {
38
+ const arg = args[i]!;
39
+ if (arg === "-h" || arg === "--help") {
40
+ help = true;
41
+ continue;
42
+ }
43
+ if (arg === "-v" || arg === "--version") {
44
+ version = true;
45
+ continue;
46
+ }
47
+
48
+ const equals = arg.match(/^--(host|port|token)=(.*)$/);
49
+ if (equals) {
50
+ const name = equals[1] as ValueOption;
51
+ setValue(name, equals[2]);
52
+ continue;
53
+ }
54
+
55
+ const name =
56
+ arg === "-H" || arg === "--host"
57
+ ? "host"
58
+ : arg === "-p" || arg === "--port"
59
+ ? "port"
60
+ : arg === "-t" || arg === "--token"
61
+ ? "token"
62
+ : undefined;
63
+ if (name) {
64
+ const value = args[++i];
65
+ if (value?.startsWith("-") && !(name === "port" && /^-\d+$/.test(value))) {
66
+ fail(`Missing value for --${name}`);
67
+ }
68
+ setValue(name, value);
69
+ continue;
70
+ }
71
+ fail(`Unknown option: ${arg}`);
72
+ }
73
+
74
+ if (help) {
75
+ console.log(usage);
76
+ process.exit(0);
77
+ }
78
+
79
+ if (version) {
80
+ const packageJson = (await Bun.file(
81
+ new URL("./package.json", import.meta.url),
82
+ ).json()) as { version?: string };
83
+ console.log(packageJson.version ?? "unknown");
84
+ process.exit(0);
85
+ }
86
+
87
+ if (values.port !== undefined) {
88
+ validatePort(values.port);
89
+ process.env.CHAT_ROOM_SERVER_PORT = values.port;
90
+ }
91
+ if (values.host !== undefined) {
92
+ process.env.CHAT_ROOM_SERVER_HOST = values.host;
93
+ }
94
+ if (values.token !== undefined) {
95
+ process.env.CHAT_ROOM_SERVER_TOKEN = values.token;
96
+ }
97
+
98
+ const { default: server } = await import("./server/chat-server");
5
99
  Bun.serve(server);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@alexsun-top/opencode-chat-room",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "A chat-room plugin and central server for OpenCode.",
5
5
  "repository": {
6
6
  "type": "git",
@@ -15,7 +15,22 @@ import {
15
15
  type RegistryEntry,
16
16
  } from "../src/store";
17
17
 
18
- const port = Number(process.env.CHAT_ROOM_SERVER_PORT ?? 4399);
18
+ function parsePort(value: string): number {
19
+ const port = Number(value);
20
+ if (!Number.isInteger(port) || port < 1 || port > 65535) {
21
+ throw new Error(
22
+ `Invalid server port "${value}": expected an integer from 1 to 65535`,
23
+ );
24
+ }
25
+ return port;
26
+ }
27
+
28
+ const hostname =
29
+ process.env.CHAT_ROOM_SERVER_HOST ?? process.env.HOST ?? "0.0.0.0";
30
+ const displayHostname = hostname.includes(":") ? `[${hostname}]` : hostname;
31
+ const port = parsePort(
32
+ process.env.CHAT_ROOM_SERVER_PORT ?? process.env.PORT ?? "4399",
33
+ );
19
34
  const token = process.env.CHAT_ROOM_SERVER_TOKEN;
20
35
 
21
36
  // ponytail: 部署者显式配置的 serve 地址——消息到达时立即唤起被 @ 的已退出
@@ -534,6 +549,7 @@ async function handle(req: Request): Promise<Response> {
534
549
  }
535
550
 
536
551
  export default {
552
+ hostname,
537
553
  port,
538
554
  fetch: handle,
539
555
  // ponytail: bun's default idleTimeout (10s) would close our long-poll
@@ -543,7 +559,7 @@ export default {
543
559
  };
544
560
 
545
561
  console.log(
546
- `chat-room center listening on http://localhost:${port}${token ? " (token auth on)" : ""}`,
562
+ `chat-room center listening on http://${displayHostname}:${port}${token ? " (token auth on)" : ""}`,
547
563
  );
548
564
  if (!token) {
549
565
  // ponytail: default is open — anyone who can reach the port can read/write