codeharbor 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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,228 @@
1
+ # CodeHarbor
2
+
3
+ CodeHarbor is an instant-messaging bridge for `codex CLI`.
4
+ Users send messages in Matrix, CodeHarbor routes each message to a Codex session, then sends the final result back to the same Matrix room.
5
+
6
+ ## What It Does
7
+
8
+ - Matrix channel adapter (receive + reply)
9
+ - Session-to-Codex mapping via persistent SQLite state
10
+ - One-time migration import from legacy `state.json`
11
+ - Duplicate Matrix event protection
12
+ - Context-aware trigger (DM direct chat + group mention/reply + active session window)
13
+ - Room-level trigger policy overrides
14
+ - Real `/stop` cancellation (kills in-flight Codex process)
15
+ - Session runtime workers (logical worker per `channel:room:user`, with worker stats in `/status`)
16
+ - Rate limiting + concurrency guardrails (user/room/global)
17
+ - Progress + typing updates with group notice coalescing (`m.replace` edit)
18
+ - CLI-compat mode (`cli_compat_mode`) for minimal prompt rewriting + raw event passthrough
19
+ - Attachment metadata passthrough from Matrix events into prompt context
20
+ - Request observability (request_id, queue/exec/send durations, status counters)
21
+ - NPM-distributed CLI (`codeharbor`)
22
+
23
+ ## Architecture
24
+
25
+ ```text
26
+ Matrix Room -> MatrixChannel -> Orchestrator -> CodexExecutor (codex exec/resume)
27
+ |
28
+ -> StateStore (SQLite)
29
+ ```
30
+
31
+ ## Implementation Status
32
+
33
+ - Primary runtime: TypeScript/Node (`src/`, `dist/`, `npm run ...`)
34
+ - Legacy/reference implementation: Python (`app/`, `tests/`)
35
+ - New features and fixes target the TypeScript runtime.
36
+ - Python code is kept as legacy reference only (maintenance mode).
37
+
38
+ ## Prerequisites
39
+
40
+ - Node.js 20+
41
+ - `codex` CLI installed and authenticated (`codex login`)
42
+ - A Matrix bot user + access token
43
+
44
+ ## Install
45
+
46
+ Install globally from npm (after publish):
47
+
48
+ ```bash
49
+ npm install -g codeharbor
50
+ ```
51
+
52
+ Install directly from GitHub:
53
+
54
+ ```bash
55
+ npm install -g github:biglone/CodeHarbor
56
+ ```
57
+
58
+ Build a local package tarball and install it:
59
+
60
+ ```bash
61
+ npm pack
62
+ npm install -g ./codeharbor-<version>.tgz
63
+ ```
64
+
65
+ ## Quick Start
66
+
67
+ For local development from source:
68
+
69
+ 1. Install dependencies:
70
+
71
+ ```bash
72
+ npm install
73
+ ```
74
+
75
+ 2. Configure environment:
76
+
77
+ ```bash
78
+ cp .env.example .env
79
+ ```
80
+
81
+ Required values:
82
+
83
+ - `MATRIX_HOMESERVER`
84
+ - `MATRIX_USER_ID`
85
+ - `MATRIX_ACCESS_TOKEN`
86
+
87
+ 3. Run in dev mode:
88
+
89
+ ```bash
90
+ npm run dev
91
+ ```
92
+
93
+ 4. Build and run as CLI:
94
+
95
+ ```bash
96
+ npm run build
97
+ node dist/cli.js start
98
+ ```
99
+
100
+ ## Commands
101
+
102
+ - `codeharbor start`: start service
103
+ - `codeharbor doctor`: check `codex` and Matrix connectivity
104
+
105
+ ## Message Rules
106
+
107
+ - Direct Message (DM)
108
+ - all text messages are processed by default (no prefix required)
109
+ - Group Room
110
+ - processed when **any allowed trigger** matches:
111
+ - message mentions bot user id
112
+ - message replies to a bot message
113
+ - sender has an active conversation window
114
+ - optional explicit prefix match (`MATRIX_COMMAND_PREFIX`)
115
+ - Trigger Policy
116
+ - global defaults via `GROUP_TRIGGER_ALLOW_*`
117
+ - per-room overrides via `ROOM_TRIGGER_POLICY_JSON`
118
+ - Active Conversation Window
119
+ - each accepted request activates the sender's conversation in that room
120
+ - activation TTL: `SESSION_ACTIVE_WINDOW_MINUTES` (default: `20`)
121
+ - Control commands
122
+ - `/status` show session + limiter + metrics + runtime worker status
123
+ - `/reset` clear bound Codex session and keep conversation active
124
+ - `/stop` cancel in-flight execution (if running) and reset session context
125
+
126
+ ## CLI Compatibility Mode
127
+
128
+ To make IM behavior closer to local `codex` CLI interaction, enable:
129
+
130
+ - `CLI_COMPAT_MODE=true`
131
+ - preserve user prompt whitespace
132
+ - avoid stripping `@bot` mention text in prompt body
133
+ - enable richer raw event passthrough summaries
134
+ - `CLI_COMPAT_PASSTHROUGH_EVENTS=true`
135
+ - emit raw event summaries from codex JSON stream
136
+ - `CLI_COMPAT_PRESERVE_WHITESPACE=true`
137
+ - keep incoming Matrix message body untrimmed for execution
138
+ - `CLI_COMPAT_DISABLE_REPLY_CHUNK_SPLIT=true|false`
139
+ - optionally send one full message chunk to Matrix without auto split
140
+ - `CLI_COMPAT_PROGRESS_THROTTLE_MS`
141
+ - lower update throttle for near-real-time progress
142
+ - `CLI_COMPAT_FETCH_MEDIA=true|false`
143
+ - download Matrix `mxc://` media (image) to temp file and pass it to codex via `--image`
144
+ - `CLI_COMPAT_RECORD_PATH=/abs/path/records.jsonl`
145
+ - append executed prompts as JSONL for replay benchmarking
146
+
147
+ Note: execution still uses `codex exec/resume` per request; compatibility mode focuses on behavior parity and reduced middleware interference.
148
+
149
+ ## Persistence
150
+
151
+ - `STATE_DB_PATH=data/state.db`
152
+ - SQLite store for sessions + processed event ids
153
+ - `STATE_PATH=data/state.json`
154
+ - legacy JSON source for one-time migration import when SQLite is empty
155
+ - `MAX_SESSION_AGE_DAYS=30`
156
+ - prune stale sessions by age
157
+ - `MAX_SESSIONS=5000`
158
+ - prune least-recently-updated sessions when over limit
159
+
160
+ ## Rate Limiting
161
+
162
+ - `RATE_LIMIT_WINDOW_SECONDS`
163
+ - `RATE_LIMIT_MAX_REQUESTS_PER_USER`
164
+ - `RATE_LIMIT_MAX_REQUESTS_PER_ROOM`
165
+ - `RATE_LIMIT_MAX_CONCURRENT_GLOBAL`
166
+ - `RATE_LIMIT_MAX_CONCURRENT_PER_USER`
167
+ - `RATE_LIMIT_MAX_CONCURRENT_PER_ROOM`
168
+
169
+ Set a value to `0` to disable a specific limiter.
170
+
171
+ ## Codex CLI Alignment
172
+
173
+ Use these to align runtime with your terminal CLI profile:
174
+
175
+ - `CODEX_SANDBOX_MODE`
176
+ - `CODEX_APPROVAL_POLICY`
177
+ - `CODEX_EXTRA_ARGS`
178
+ - `CODEX_EXTRA_ENV_JSON`
179
+
180
+ When image attachments are present and `CLI_COMPAT_FETCH_MEDIA=true`, CodeHarbor will:
181
+
182
+ 1. download `mxc://` media to a temp file
183
+ 2. pass local file paths as `--image` to codex exec
184
+ 3. best-effort cleanup temp files after the request
185
+ 4. optional prompt record append (`CLI_COMPAT_RECORD_PATH`) for deterministic replay input
186
+
187
+ ## Replay Benchmark
188
+
189
+ Replay recorded prompts directly against codex CLI to quantify drift and latency:
190
+
191
+ ```bash
192
+ npm run replay:cli-compat -- --input data/cli-compat-record.jsonl --out data/replay-report.json --max 50
193
+ ```
194
+
195
+ Useful flags:
196
+
197
+ - `--model <name>`
198
+ - `--workdir <path>`
199
+ - `--timeout-ms <n>`
200
+ - `--sandbox <mode>`
201
+ - `--approval <policy>`
202
+ - `--dangerous`
203
+
204
+ ## Progress + Output
205
+
206
+ - `MATRIX_PROGRESS_UPDATES=true`
207
+ - emit stage progress updates (for example reasoning/thinking snippets)
208
+ - `MATRIX_PROGRESS_MIN_INTERVAL_MS=2500`
209
+ - minimum interval between progress updates
210
+ - `MATRIX_TYPING_TIMEOUT_MS=10000`
211
+ - typing indicator timeout; CodeHarbor refreshes typing state while handling a request
212
+ - Group rooms use notice edit (`m.replace`) to coalesce progress and reduce spam.
213
+ - Reply chunking is paragraph/code-block aware to avoid cutting fenced blocks when possible.
214
+
215
+ ## Tests
216
+
217
+ ```bash
218
+ npm run typecheck
219
+ npm test
220
+ npm run build
221
+ npm run test:legacy
222
+ ```
223
+
224
+ ## Legacy Runtime
225
+
226
+ - Legacy Python runtime exists in `app/` and `tests/`.
227
+ - It is not part of default release/CI gates.
228
+ - Use `npm run test:legacy` for optional regression checks.
package/dist/cli.d.ts ADDED
@@ -0,0 +1 @@
1
+ #!/usr/bin/env node