@hmharness/codexhost-bridge 0.5.2

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 hmharness contributors
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,36 @@
1
+ # @hmharness/codexhost-bridge
2
+
3
+ HMHarness 的 CodexHost 机器可读桥接包,提供全局命令 `hmh-codexhost`。它把 HMHarness 的 provider/model 发现和任务执行封装为稳定的 NDJSON 输出,供 CodexHost 或其他桌面宿主消费。
4
+
5
+ ## 安装 / Install
6
+
7
+ ```bash
8
+ npm install -g @hmharness/codexhost-bridge
9
+ hmh-codexhost --version
10
+ ```
11
+
12
+ CodexHost 会自动发现 PATH 中的 `hmh-codexhost`。请先完成 HMHarness 配置(`hmh init`),再在 CodexHost 中选择 HMHarness。
13
+
14
+ ## 协议 / Protocol
15
+
16
+ - `hmh-codexhost --version` 输出 JSON 版本信息。
17
+ - `hmh-codexhost providers --json` 输出 provider/model 目录,不包含 API key。
18
+ - 任务文本通过 stdin 传入,stdout 逐行输出 NDJSON:
19
+ - `{"type":"delta","text":"..."}`:最终回答增量;
20
+ - `{"type":"tool",...}`:工具调用状态;
21
+ - `{"type":"line","text":"..."}`:执行循环状态;
22
+ - `{"text":"...","sessionId":"..."}`:权威最终记录。
23
+
24
+ 最终记录输出后进程立即退出,避免宿主 UI 永远停留在“正在思考”。
25
+
26
+ ## 验证 / Verification
27
+
28
+ ```bash
29
+ npm test -w @hmharness/codexhost-bridge
30
+ npm run build
31
+ node scripts/publish-preflight.cjs codexhost-bridge
32
+ ```
33
+
34
+ ## License
35
+
36
+ [MIT](./LICENSE)
@@ -0,0 +1,131 @@
1
+ #!/usr/bin/env node
2
+ // hmh-codexhost bridge - streaming protocol v2
3
+ //
4
+ // Protocol (stdout, NDJSON):
5
+ // {"type":"delta","text":"..."} - final-answer text increment (coalesced, lossless)
6
+ // {"type":"tool","phase":"call"|"done","name":"...","args":"..."} - tool activity
7
+ // {"type":"line","text":"..."} - agent loop status line
8
+ // {"text":"...","sessionId":...} - FINAL line (no .type) - authoritative result
9
+ //
10
+ // The FINAL line is emitted exactly once, then the process exits. Any
11
+ // background work inside the agent (self-evolution cycles etc.) must not
12
+ // keep this one-shot process alive - we exit hard after the final line.
13
+ import process from "node:process";
14
+ import { createRequire } from "node:module";
15
+
16
+ const require = createRequire(import.meta.url);
17
+ const { version } = require("../package.json");
18
+ const { buildRegistry, runAgentTask } = await import("@hmharness/agent");
19
+ const { loadConfig, listProviders, homeDir } = await import("@hmharness/kernel");
20
+
21
+ function fail(error) {
22
+ process.stderr.write(`${error instanceof Error ? error.message : String(error)}\n`);
23
+ process.exit(1);
24
+ }
25
+
26
+ function chatProvider(config) {
27
+ const name = process.env.HMH_CHAT_PROVIDER;
28
+ if (!name) return config;
29
+ const provider = config.providers?.[name];
30
+ if (!provider) throw new Error(`unknown provider "${name}"`);
31
+ return { ...config, provider, routing: { ...config.routing, chat: name } };
32
+ }
33
+
34
+ function emit(value) {
35
+ process.stdout.write(`${JSON.stringify(value)}\n`);
36
+ }
37
+
38
+ async function readStdin() {
39
+ const chunks = [];
40
+ for await (const chunk of process.stdin) chunks.push(chunk);
41
+ return Buffer.concat(chunks).toString("utf8");
42
+ }
43
+
44
+ const [command = "run", ...arguments_] = process.argv.slice(2);
45
+
46
+ try {
47
+ if (command === "--version" || command === "-v") {
48
+ emit({ version });
49
+ } else if (command === "providers") {
50
+ if (arguments_.includes("--json")) {
51
+ const config = await loadConfig();
52
+ emit({
53
+ version,
54
+ chat: config.routing?.chat ?? (config.providers && Object.keys(config.providers).length > 0 ? null : "default"),
55
+ providers: listProviders(config).map(({ name, model, purposes }) => ({ name, model, purposes })),
56
+ });
57
+ } else {
58
+ process.stderr.write("usage: hmh-codexhost providers --json\n");
59
+ process.exitCode = 2;
60
+ }
61
+ } else if (command === "run") {
62
+ const task = (await readStdin()).trim();
63
+ if (!task) throw new Error("task must not be empty");
64
+ const config = chatProvider(await loadConfig());
65
+ const { reg, clients } = await buildRegistry({ mcp: false, announce: false });
66
+ const wantsStream = process.env.HMH_CODEXHOST_STREAM !== "0";
67
+ // Lossless coalescing: deltas buffer and flush on a 40ms cadence; the
68
+ // pending remainder flushes before tool events and the final line, so
69
+ // concatenated delta text === final text (the adapter compares them).
70
+ let pending = "";
71
+ let lastFlush = 0;
72
+ const flush = (force = false) => {
73
+ if (!pending) return;
74
+ const now = Date.now();
75
+ if (!force && now - lastFlush < 40) return;
76
+ lastFlush = now;
77
+ emit({ type: "delta", text: pending });
78
+ pending = "";
79
+ };
80
+ try {
81
+ const result = await runAgentTask({
82
+ task,
83
+ registry: reg,
84
+ cfg: config,
85
+ ctx: { cwd: process.cwd(), home: homeDir() },
86
+ yes: true,
87
+ events: wantsStream
88
+ ? {
89
+ onLine: (line) => {
90
+ flush(true);
91
+ emit({ type: "line", text: String(line).slice(0, 400) });
92
+ },
93
+ onDelta: (kind, chunk) => {
94
+ if (kind === "reasoning" || !chunk) return;
95
+ pending += chunk;
96
+ flush();
97
+ },
98
+ onToolCall: (name, args) => {
99
+ flush(true);
100
+ emit({ type: "tool", phase: "call", name, args: JSON.stringify(args ?? {}).slice(0, 300) });
101
+ },
102
+ onToolResult: (name) => {
103
+ flush(true);
104
+ emit({ type: "tool", phase: "done", name });
105
+ },
106
+ }
107
+ : {},
108
+ });
109
+ flush(true);
110
+ emit({
111
+ text: result.text,
112
+ sessionId: result.sessionId,
113
+ turns: result.turns,
114
+ toolUses: result.toolUses,
115
+ toolsUsed: result.toolsUsed,
116
+ });
117
+ } finally {
118
+ for (const client of clients) client.close();
119
+ }
120
+ // One-shot process: exit right after the final line. Background agent
121
+ // work (fire-and-forget self-evolution) must not pin the event loop -
122
+ // otherwise the host turn never completes and the UI spins forever.
123
+ await new Promise((resolve) => setImmediate(resolve));
124
+ process.exit(0);
125
+ } else {
126
+ process.stderr.write("usage: hmh-codexhost [--version | providers --json | run < stdin]\n");
127
+ process.exitCode = 2;
128
+ }
129
+ } catch (error) {
130
+ fail(error);
131
+ }
package/package.json ADDED
@@ -0,0 +1,40 @@
1
+ {
2
+ "name": "@hmharness/codexhost-bridge",
3
+ "version": "0.5.2",
4
+ "description": "Machine-readable hmharness bridge for CodexHost and other agent desktop hosts.",
5
+ "type": "module",
6
+ "bin": {
7
+ "hmh-codexhost": "bin/hmh-codexhost.mjs"
8
+ },
9
+ "files": [
10
+ "bin",
11
+ "README.md",
12
+ "LICENSE"
13
+ ],
14
+ "scripts": {
15
+ "test": "node --test test/*.test.mjs"
16
+ },
17
+ "dependencies": {
18
+ "@hmharness/agent": "0.5.2",
19
+ "@hmharness/kernel": "0.5.2"
20
+ },
21
+ "engines": {
22
+ "node": ">=22"
23
+ },
24
+ "keywords": [
25
+ "hmharness",
26
+ "codexhost",
27
+ "agent",
28
+ "bridge",
29
+ "ndjson",
30
+ "cli"
31
+ ],
32
+ "license": "MIT",
33
+ "repository": {
34
+ "type": "git",
35
+ "url": "git+https://github.com/swsgbl/hmharness.git",
36
+ "directory": "packages/codexhost-bridge"
37
+ },
38
+ "homepage": "https://github.com/swsgbl/hmharness/tree/main/packages/codexhost-bridge",
39
+ "bugs": "https://github.com/swsgbl/hmharness/issues"
40
+ }