@pixelbyte-software/pixcode 1.51.8 → 1.52.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.
Files changed (307) hide show
  1. package/CODE_OF_CONDUCT.md +41 -41
  2. package/CONTRIBUTING.md +155 -155
  3. package/LICENSE +718 -718
  4. package/README.de.md +169 -169
  5. package/README.ja.md +167 -167
  6. package/README.ko.md +167 -167
  7. package/README.md +419 -419
  8. package/README.ru.md +169 -169
  9. package/README.tr.md +298 -298
  10. package/README.zh-CN.md +167 -167
  11. package/SECURITY.md +46 -46
  12. package/dist/api-automation.html +110 -110
  13. package/dist/api-docs.html +548 -548
  14. package/dist/assets/{index--kNEhygZ.js → index-HFIf2iQq.js} +153 -153
  15. package/dist/clear-cache.html +85 -85
  16. package/dist/convert-icons.md +52 -52
  17. package/dist/docs.html +308 -308
  18. package/dist/favicon.svg +8 -8
  19. package/dist/features.html +133 -133
  20. package/dist/generate-icons.js +48 -48
  21. package/dist/humans.txt +15 -15
  22. package/dist/icons/codex-white.svg +3 -3
  23. package/dist/icons/codex.svg +3 -3
  24. package/dist/icons/cursor-white.svg +11 -11
  25. package/dist/icons/icon-128x128.svg +9 -9
  26. package/dist/icons/icon-144x144.svg +9 -9
  27. package/dist/icons/icon-152x152.svg +9 -9
  28. package/dist/icons/icon-192x192.svg +9 -9
  29. package/dist/icons/icon-384x384.svg +9 -9
  30. package/dist/icons/icon-512x512.svg +9 -9
  31. package/dist/icons/icon-72x72.svg +9 -9
  32. package/dist/icons/icon-96x96.svg +9 -9
  33. package/dist/icons/icon-template.svg +9 -9
  34. package/dist/icons/qwen-logo.svg +14 -14
  35. package/dist/index.html +58 -58
  36. package/dist/landing.html +268 -268
  37. package/dist/llms-full.txt +119 -119
  38. package/dist/llms.txt +53 -53
  39. package/dist/logo.svg +12 -12
  40. package/dist/manifest.json +60 -60
  41. package/dist/openapi.yaml +1696 -1696
  42. package/dist/orchestration.html +125 -125
  43. package/dist/robots.txt +4 -4
  44. package/dist/site.css +692 -692
  45. package/dist/sitemap.xml +51 -51
  46. package/dist/sw.js +132 -132
  47. package/dist-server/server/cli.js +96 -96
  48. package/dist-server/server/daemon/manager.js +33 -33
  49. package/dist-server/server/daemon-manager.js +64 -64
  50. package/dist-server/server/index.js +91 -65
  51. package/dist-server/server/index.js.map +1 -1
  52. package/dist-server/server/routes/commands.js +25 -25
  53. package/dist-server/server/routes/git.js +17 -17
  54. package/dist-server/server/routes/live-view.js +46 -46
  55. package/dist-server/server/services/hermes-gateway.js +5 -5
  56. package/dist-server/server/services/public-api-manifest.js +51 -51
  57. package/package.json +222 -222
  58. package/scripts/fix-node-pty.js +67 -67
  59. package/scripts/github/create-v1.38-issues.mjs +351 -351
  60. package/scripts/github/create-vscode-workbench-issues.mjs +121 -121
  61. package/scripts/hermes/configure-pixcode-mcp.mjs +165 -165
  62. package/scripts/hermes/pixcode-mcp-server.mjs +1009 -1009
  63. package/scripts/smoke/changes-panel-layout.mjs +48 -48
  64. package/scripts/smoke/chat-composer-fixed-layout.mjs +55 -55
  65. package/scripts/smoke/chat-message-timeline-order.mjs +41 -41
  66. package/scripts/smoke/chat-realtime-hydration.mjs +44 -44
  67. package/scripts/smoke/chat-session-provider-pools.mjs +35 -35
  68. package/scripts/smoke/chat-session-state.mjs +19 -19
  69. package/scripts/smoke/code-editor-theme.mjs +55 -55
  70. package/scripts/smoke/code-editor-vscode-engine.mjs +91 -91
  71. package/scripts/smoke/command-center-agent-writes.mjs +79 -79
  72. package/scripts/smoke/command-center-non-git.mjs +46 -46
  73. package/scripts/smoke/context-packet.mjs +43 -43
  74. package/scripts/smoke/control-room-ux-redesign.mjs +91 -91
  75. package/scripts/smoke/daemon-entrypoint.mjs +20 -20
  76. package/scripts/smoke/default-landing-routing.mjs +33 -33
  77. package/scripts/smoke/desktop-native-notifications.mjs +30 -30
  78. package/scripts/smoke/desktop-tray-icon.mjs +33 -33
  79. package/scripts/smoke/discord-release-workflow.mjs +24 -24
  80. package/scripts/smoke/git-install-update.mjs +255 -255
  81. package/scripts/smoke/handoff-artifact-protocol.mjs +50 -50
  82. package/scripts/smoke/hermes-api-install.mjs +56 -56
  83. package/scripts/smoke/hermes-gateway-persistence.mjs +104 -104
  84. package/scripts/smoke/hermes-mcp-pixcode-roundtrip.mjs +426 -426
  85. package/scripts/smoke/hermes-rest-chat-api.mjs +162 -162
  86. package/scripts/smoke/hermes-rest-chat-live.mjs +45 -45
  87. package/scripts/smoke/hermes-rest-codex-launch.mjs +209 -209
  88. package/scripts/smoke/hermes-rest-gateway.mjs +79 -79
  89. package/scripts/smoke/hermes-rest-live.mjs +42 -42
  90. package/scripts/smoke/hermes-roundtrip.mjs +167 -167
  91. package/scripts/smoke/hermes-settings-commands.mjs +349 -349
  92. package/scripts/smoke/hermes-smoke-launcher-guard.mjs +34 -34
  93. package/scripts/smoke/live-view-diagnostics.mjs +53 -53
  94. package/scripts/smoke/live-view-environment.mjs +92 -92
  95. package/scripts/smoke/live-view-integration.mjs +450 -450
  96. package/scripts/smoke/mac-desktop-runtime.mjs +37 -37
  97. package/scripts/smoke/mobile-tunnel-guidance.mjs +29 -29
  98. package/scripts/smoke/model-registry.mjs +36 -36
  99. package/scripts/smoke/multi-project-ui.mjs +45 -45
  100. package/scripts/smoke/multi-worker-slots.mjs +42 -42
  101. package/scripts/smoke/notification-center.mjs +87 -87
  102. package/scripts/smoke/notification-inapp-preference.mjs +23 -23
  103. package/scripts/smoke/notification-taxonomy.mjs +58 -58
  104. package/scripts/smoke/orchestration-api.mjs +172 -172
  105. package/scripts/smoke/orchestration-execution-dashboard.mjs +33 -33
  106. package/scripts/smoke/orchestration-live-run.mjs +176 -176
  107. package/scripts/smoke/orchestration-mobile-scroll.mjs +29 -29
  108. package/scripts/smoke/orchestration-model-sync.mjs +30 -30
  109. package/scripts/smoke/orchestration-permission-fallback.mjs +34 -34
  110. package/scripts/smoke/orchestration-runtime-guards.mjs +48 -48
  111. package/scripts/smoke/orchestration-user-facing-output.mjs +25 -25
  112. package/scripts/smoke/permission-policy.mjs +50 -50
  113. package/scripts/smoke/pixcode-workbench-1-48.mjs +167 -167
  114. package/scripts/smoke/provider-models-opencode-live.mjs +66 -66
  115. package/scripts/smoke/provider-rest-api.mjs +124 -124
  116. package/scripts/smoke/provider-selection-status.mjs +52 -52
  117. package/scripts/smoke/run-state-refresh.mjs +52 -52
  118. package/scripts/smoke/runtime-manager.mjs +99 -99
  119. package/scripts/smoke/shell-manual-disconnect.mjs +30 -30
  120. package/scripts/smoke/side-panel-editor-layout.mjs +34 -34
  121. package/scripts/smoke/static-root-routing.mjs +21 -21
  122. package/scripts/smoke/strict-handoff-compact.mjs +60 -60
  123. package/scripts/smoke/taskmaster-config.mjs +24 -24
  124. package/scripts/smoke/taskmaster-execution-telegram.mjs +3 -3
  125. package/scripts/smoke/taskmaster-onboarding.mjs +3 -3
  126. package/scripts/smoke/taskmaster-run-graph.mjs +3 -3
  127. package/scripts/smoke/telegram-control.mjs +242 -242
  128. package/scripts/smoke/tunnel-persistence.mjs +56 -56
  129. package/scripts/smoke/update-issue-progress.mjs +69 -69
  130. package/scripts/smoke/update-ux.mjs +55 -55
  131. package/scripts/smoke/v138-completion.mjs +132 -132
  132. package/scripts/smoke/v138-desktop-release-hardening.mjs +69 -69
  133. package/scripts/smoke/v138-diagnostics.mjs +63 -63
  134. package/scripts/smoke/v138-issue-planner.mjs +33 -33
  135. package/scripts/smoke/v143-remote-control.mjs +76 -76
  136. package/scripts/smoke/v144-production-loop.mjs +47 -47
  137. package/scripts/smoke/v145-platformization.mjs +46 -46
  138. package/scripts/smoke/v146-control-room-ui.mjs +150 -150
  139. package/scripts/smoke/version-modal-autoshow.mjs +29 -29
  140. package/scripts/smoke/vscode-workbench-layout.mjs +63 -63
  141. package/scripts/smoke/vscode-workbench-polish.mjs +461 -461
  142. package/scripts/smoke/workflow-fallback-replay.mjs +56 -56
  143. package/scripts/smoke/workflow-templates.mjs +43 -43
  144. package/scripts/smoke/workflow-trace-timeline.mjs +46 -46
  145. package/scripts/update-git-install.mjs +283 -283
  146. package/server/claude-sdk.js +920 -920
  147. package/server/cli.js +1035 -1035
  148. package/server/constants/config.js +4 -4
  149. package/server/cursor-cli.js +344 -344
  150. package/server/daemon/manager.js +563 -563
  151. package/server/daemon-manager.js +964 -964
  152. package/server/database/db.js +895 -895
  153. package/server/database/json-store.js +197 -197
  154. package/server/gemini-cli.js +550 -550
  155. package/server/gemini-response-handler.js +79 -79
  156. package/server/index.js +111 -81
  157. package/server/load-env.js +35 -35
  158. package/server/middleware/auth.js +155 -155
  159. package/server/modules/orchestration/a2a/adapter-registry.ts +108 -108
  160. package/server/modules/orchestration/a2a/adapters/abstract-a2a.adapter.ts +63 -63
  161. package/server/modules/orchestration/a2a/adapters/claude-code.adapter.ts +286 -286
  162. package/server/modules/orchestration/a2a/adapters/codex.adapter.ts +244 -244
  163. package/server/modules/orchestration/a2a/adapters/cursor.adapter.ts +249 -249
  164. package/server/modules/orchestration/a2a/adapters/gemini.adapter.ts +248 -248
  165. package/server/modules/orchestration/a2a/adapters/json-event.adapter.ts +100 -100
  166. package/server/modules/orchestration/a2a/adapters/opencode.adapter.ts +248 -248
  167. package/server/modules/orchestration/a2a/adapters/qwen.adapter.ts +248 -248
  168. package/server/modules/orchestration/a2a/agent-card.ts +55 -55
  169. package/server/modules/orchestration/a2a/routes.ts +590 -590
  170. package/server/modules/orchestration/a2a/task-store.ts +178 -178
  171. package/server/modules/orchestration/a2a/types.ts +126 -126
  172. package/server/modules/orchestration/a2a/validator.ts +113 -113
  173. package/server/modules/orchestration/hermes/hermes.routes.ts +642 -642
  174. package/server/modules/orchestration/index.ts +101 -101
  175. package/server/modules/orchestration/preview/port-watcher.ts +112 -112
  176. package/server/modules/orchestration/preview/preview-proxy.ts +60 -60
  177. package/server/modules/orchestration/preview/types.ts +19 -19
  178. package/server/modules/orchestration/security/permission-policy.ts +401 -401
  179. package/server/modules/orchestration/tasks/orchestration-task-store.ts +41 -41
  180. package/server/modules/orchestration/tasks/orchestration-task.routes.ts +64 -64
  181. package/server/modules/orchestration/tasks/orchestration-task.service.ts +209 -209
  182. package/server/modules/orchestration/tasks/orchestration-task.types.ts +40 -40
  183. package/server/modules/orchestration/tasks/task-run-graph.ts +155 -155
  184. package/server/modules/orchestration/workflows/approval-queue.ts +106 -106
  185. package/server/modules/orchestration/workflows/built-in-workflows.ts +127 -127
  186. package/server/modules/orchestration/workflows/context-packet.ts +186 -186
  187. package/server/modules/orchestration/workflows/handoff-artifact.ts +175 -175
  188. package/server/modules/orchestration/workflows/workflow-fallback-policy.ts +161 -161
  189. package/server/modules/orchestration/workflows/workflow-replay.ts +254 -254
  190. package/server/modules/orchestration/workflows/workflow-runner.ts +2070 -2070
  191. package/server/modules/orchestration/workflows/workflow-store.ts +97 -97
  192. package/server/modules/orchestration/workflows/workflow-templates.ts +272 -272
  193. package/server/modules/orchestration/workflows/workflow-trace.ts +424 -424
  194. package/server/modules/orchestration/workflows/workflow.routes.ts +586 -586
  195. package/server/modules/orchestration/workflows/workflow.types.ts +111 -111
  196. package/server/modules/orchestration/workflows/workspace-target.ts +122 -122
  197. package/server/modules/orchestration/workspace/docker-workspace.ts +136 -136
  198. package/server/modules/orchestration/workspace/path-safety.ts +55 -55
  199. package/server/modules/orchestration/workspace/types.ts +52 -52
  200. package/server/modules/orchestration/workspace/workspace-manager.ts +102 -102
  201. package/server/modules/orchestration/workspace/worktree-workspace.ts +126 -126
  202. package/server/modules/providers/index.ts +2 -2
  203. package/server/modules/providers/list/claude/claude-auth.provider.ts +146 -146
  204. package/server/modules/providers/list/claude/claude-mcp.provider.ts +135 -135
  205. package/server/modules/providers/list/claude/claude-sessions.provider.ts +306 -306
  206. package/server/modules/providers/list/claude/claude.provider.ts +15 -15
  207. package/server/modules/providers/list/codex/codex-auth.provider.ts +117 -117
  208. package/server/modules/providers/list/codex/codex-mcp.provider.ts +135 -135
  209. package/server/modules/providers/list/codex/codex-sessions.provider.ts +319 -319
  210. package/server/modules/providers/list/codex/codex.provider.ts +15 -15
  211. package/server/modules/providers/list/cursor/cursor-auth.provider.ts +147 -147
  212. package/server/modules/providers/list/cursor/cursor-mcp.provider.ts +108 -108
  213. package/server/modules/providers/list/cursor/cursor-sessions.provider.ts +421 -421
  214. package/server/modules/providers/list/cursor/cursor.provider.ts +15 -15
  215. package/server/modules/providers/list/gemini/gemini-auth.provider.ts +173 -173
  216. package/server/modules/providers/list/gemini/gemini-mcp.provider.ts +110 -110
  217. package/server/modules/providers/list/gemini/gemini-sessions.provider.ts +227 -227
  218. package/server/modules/providers/list/gemini/gemini.provider.ts +15 -15
  219. package/server/modules/providers/list/opencode/opencode-auth.provider.ts +131 -131
  220. package/server/modules/providers/list/opencode/opencode-mcp.provider.ts +126 -126
  221. package/server/modules/providers/list/opencode/opencode-sessions.provider.ts +286 -286
  222. package/server/modules/providers/list/opencode/opencode.provider.ts +29 -29
  223. package/server/modules/providers/list/qwen/qwen-auth.provider.ts +146 -146
  224. package/server/modules/providers/list/qwen/qwen-mcp.provider.ts +114 -114
  225. package/server/modules/providers/list/qwen/qwen-sessions.provider.ts +265 -265
  226. package/server/modules/providers/list/qwen/qwen.provider.ts +21 -21
  227. package/server/modules/providers/provider.registry.ts +40 -40
  228. package/server/modules/providers/provider.routes.ts +891 -891
  229. package/server/modules/providers/services/mcp.service.ts +86 -86
  230. package/server/modules/providers/services/provider-auth.service.ts +26 -26
  231. package/server/modules/providers/services/sessions.service.ts +45 -45
  232. package/server/modules/providers/shared/base/abstract.provider.ts +20 -20
  233. package/server/modules/providers/shared/mcp/mcp.provider.ts +151 -151
  234. package/server/modules/providers/shared/provider-configs.ts +142 -142
  235. package/server/modules/providers/tests/mcp.test.ts +293 -293
  236. package/server/openai-codex.js +462 -462
  237. package/server/opencode-cli.js +491 -491
  238. package/server/opencode-response-handler.js +111 -111
  239. package/server/projects.js +3008 -3008
  240. package/server/qwen-code-cli.js +410 -410
  241. package/server/qwen-response-handler.js +73 -73
  242. package/server/routes/agent.js +1435 -1435
  243. package/server/routes/auth.js +142 -142
  244. package/server/routes/codex.js +20 -20
  245. package/server/routes/commands.js +538 -538
  246. package/server/routes/cursor.js +61 -61
  247. package/server/routes/diagnostics.js +41 -41
  248. package/server/routes/gemini.js +25 -25
  249. package/server/routes/git.js +1641 -1641
  250. package/server/routes/live-view.js +387 -387
  251. package/server/routes/mcp-utils.js +13 -13
  252. package/server/routes/messages.js +50 -50
  253. package/server/routes/network.js +117 -117
  254. package/server/routes/platformization.js +182 -182
  255. package/server/routes/plugins.js +320 -320
  256. package/server/routes/production-agent-loop.js +90 -90
  257. package/server/routes/projects.js +911 -911
  258. package/server/routes/public-api.js +34 -34
  259. package/server/routes/qwen.js +27 -27
  260. package/server/routes/remote.js +50 -50
  261. package/server/routes/settings.js +321 -321
  262. package/server/routes/telegram.js +140 -140
  263. package/server/routes/user.js +125 -125
  264. package/server/routes/webhooks.js +63 -63
  265. package/server/services/control-room.js +102 -102
  266. package/server/services/diagnostics.js +165 -165
  267. package/server/services/external-access.js +375 -375
  268. package/server/services/hermes-gateway.js +1562 -1562
  269. package/server/services/hermes-install-jobs.js +729 -729
  270. package/server/services/install-jobs.js +715 -715
  271. package/server/services/live-view.js +956 -956
  272. package/server/services/managed-runtimes.js +493 -493
  273. package/server/services/model-registry.js +144 -144
  274. package/server/services/notification-orchestrator.js +365 -365
  275. package/server/services/notification-taxonomy.js +204 -204
  276. package/server/services/platformization.js +773 -773
  277. package/server/services/production-agent-loop.js +248 -248
  278. package/server/services/provider-cli-versions.js +149 -149
  279. package/server/services/provider-credentials.js +189 -189
  280. package/server/services/provider-models.js +396 -396
  281. package/server/services/public-api-manifest.js +190 -190
  282. package/server/services/remote-connection.js +127 -127
  283. package/server/services/runtime-manager.js +323 -323
  284. package/server/services/startup-update.js +234 -234
  285. package/server/services/telegram/bot.js +331 -331
  286. package/server/services/telegram/control-center.js +979 -979
  287. package/server/services/telegram/telegram-http-client.js +151 -151
  288. package/server/services/telegram/translations.js +340 -340
  289. package/server/services/vapid-keys.js +36 -36
  290. package/server/services/webhooks.js +216 -216
  291. package/server/sessionManager.js +225 -225
  292. package/server/shared/interfaces.ts +54 -54
  293. package/server/shared/types.ts +172 -172
  294. package/server/shared/utils.ts +193 -193
  295. package/server/tsconfig.json +36 -36
  296. package/server/utils/colors.js +21 -21
  297. package/server/utils/commandParser.js +305 -305
  298. package/server/utils/frontmatter.js +18 -18
  299. package/server/utils/gitConfig.js +34 -34
  300. package/server/utils/plugin-loader.js +457 -457
  301. package/server/utils/plugin-process-manager.js +185 -185
  302. package/server/utils/port-access.js +209 -209
  303. package/server/utils/runtime-paths.js +37 -37
  304. package/server/utils/url-detection.js +71 -71
  305. package/server/vite-daemon.js +79 -79
  306. package/shared/modelConstants.js +161 -161
  307. package/shared/networkHosts.js +22 -22
@@ -1,1562 +1,1562 @@
1
- import { randomBytes } from 'node:crypto';
2
- import fs from 'node:fs';
3
- import net from 'node:net';
4
- import os from 'node:os';
5
- import path from 'node:path';
6
-
7
- import Database from 'better-sqlite3';
8
- import spawn from 'cross-spawn';
9
-
10
- import {
11
- buildHermesPathEnv,
12
- readHermesInstallStatus,
13
- } from './hermes-install-jobs.js';
14
-
15
- const DEFAULT_HOST = '127.0.0.1';
16
- const DEFAULT_PORT = 8642;
17
- const PORT_SCAN_LIMIT = 80;
18
- const STARTUP_TIMEOUT_MS = 30000;
19
- const FETCH_TIMEOUT_MS = 5000;
20
- const RUN_TIMEOUT_MS = 120000;
21
- const RUN_POLL_INTERVAL_MS = 1000;
22
- const LOG_LIMIT = 800;
23
- const HERMES_DIAGNOSTIC_LOG_BYTES = 120000;
24
- const ALLOWED_GATEWAY_REQUEST_METHODS = new Set(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']);
25
- const EXPECTED_PIXCODE_MCP_TOOLS = [
26
- 'pixcode_list_projects',
27
- 'pixcode_get_provider_status',
28
- 'pixcode_open_cli_terminal',
29
- 'pixcode_read_cli_terminal',
30
- 'pixcode_get_hermes_gateway_status',
31
- 'pixcode_probe_hermes_gateway',
32
- 'pixcode_get_hermes_diagnostics',
33
- 'pixcode_get_api_manifest',
34
- 'pixcode_api_request',
35
- 'pixcode_hermes_gateway_request',
36
- 'pixcode_manage_hermes_cron',
37
- 'pixcode_send_cli_input',
38
- 'pixcode_get_hermes_control_plane',
39
- 'pixcode_repair_hermes_control_plane',
40
- ];
41
- const PIXCODE_MANAGED_HERMES_ENV_PREFIXES = [
42
- 'API_SERVER_',
43
- 'BLUEBUBBLES_',
44
- 'DINGTALK_',
45
- 'DISCORD_',
46
- 'EMAIL_',
47
- 'FEISHU_',
48
- 'MATTERMOST_',
49
- 'MATRIX_',
50
- 'MSGRAPH_',
51
- 'QQ_',
52
- 'SIGNAL_',
53
- 'SLACK_',
54
- 'SMS_',
55
- 'TELEGRAM_',
56
- 'TWILIO_',
57
- 'WECOM_',
58
- 'WEIXIN_',
59
- 'WHATSAPP_',
60
- 'YUANBAO_',
61
- ];
62
-
63
- const gateways = new Map();
64
-
65
- function nowIso() {
66
- return new Date().toISOString();
67
- }
68
-
69
- function normalizeProjectPath(projectPath) {
70
- return path.resolve(projectPath || os.homedir());
71
- }
72
-
73
- function appendGatewayLog(gateway, stream, chunk) {
74
- const entry = { stream, chunk: String(chunk || ''), at: Date.now() };
75
- gateway.logs.push(entry);
76
- if (gateway.logs.length > LOG_LIMIT) {
77
- gateway.logs.splice(0, gateway.logs.length - LOG_LIMIT);
78
- }
79
- }
80
-
81
- function isGatewayRunning(gateway) {
82
- return Boolean(gateway?.child && gateway.exitCode === null && gateway.exitSignal === null);
83
- }
84
-
85
- function gatewayBaseUrl(host, port) {
86
- return `http://${host}:${port}`;
87
- }
88
-
89
- function makeApiServerKey() {
90
- return `pixcode-hermes-${randomBytes(24).toString('hex')}`;
91
- }
92
-
93
- function sleep(ms) {
94
- return new Promise((resolve) => setTimeout(resolve, ms));
95
- }
96
-
97
- function resolveSourceHermesHome(env = process.env) {
98
- if (env.HERMES_HOME?.trim()) {
99
- return path.resolve(env.HERMES_HOME);
100
- }
101
-
102
- const defaultHome = path.join(os.homedir(), '.hermes');
103
- try {
104
- const activeProfile = fs.readFileSync(path.join(defaultHome, 'active_profile'), 'utf8').trim();
105
- if (activeProfile && activeProfile !== 'default' && /^[a-z0-9][a-z0-9_-]{0,63}$/.test(activeProfile)) {
106
- return path.join(defaultHome, 'profiles', activeProfile);
107
- }
108
- } catch {
109
- // Default Hermes profile is fine when no sticky active profile exists.
110
- }
111
-
112
- return defaultHome;
113
- }
114
-
115
- function resolveHermesGatewayHome(env = process.env, options = {}) {
116
- const configured = options.hermesHome || env.PIXCODE_HERMES_GATEWAY_HOME;
117
- if (configured) {
118
- return path.resolve(configured);
119
- }
120
-
121
- return path.join(os.homedir(), '.hermes', 'profiles', 'pixcode');
122
- }
123
-
124
- function copyHermesProfileFile(sourceHome, targetHome, fileName, options = {}) {
125
- const source = path.join(sourceHome, fileName);
126
- const target = path.join(targetHome, fileName);
127
- if (!fs.existsSync(source)) return false;
128
- if (!options.overwrite && fs.existsSync(target)) return false;
129
- fs.mkdirSync(path.dirname(target), { recursive: true });
130
- fs.copyFileSync(source, target);
131
- return true;
132
- }
133
-
134
- function shouldStripManagedGatewayEnvLine(line) {
135
- const match = String(line || '').match(/^\s*(?:export\s+)?([A-Z0-9_]+)\s*=/);
136
- if (!match) return false;
137
- return PIXCODE_MANAGED_HERMES_ENV_PREFIXES.some((prefix) => match[1].startsWith(prefix));
138
- }
139
-
140
- function copyHermesProfileEnv(sourceHome, targetHome) {
141
- const source = path.join(sourceHome, '.env');
142
- const target = path.join(targetHome, '.env');
143
- if (!fs.existsSync(source)) return false;
144
-
145
- const sourceText = fs.readFileSync(source, 'utf8');
146
- const sanitized = sourceText
147
- .split(/\r?\n/)
148
- .filter((line) => !shouldStripManagedGatewayEnvLine(line))
149
- .join('\n')
150
- .replace(/\s*$/, '\n');
151
- fs.mkdirSync(path.dirname(target), { recursive: true });
152
- fs.writeFileSync(target, sanitized);
153
- return true;
154
- }
155
-
156
- function seedHermesGatewayHome({ sourceHome, targetHome, gateway }) {
157
- fs.mkdirSync(targetHome, { recursive: true });
158
- if (path.resolve(sourceHome) === path.resolve(targetHome)) {
159
- appendGatewayLog(gateway, 'meta', `Using Hermes gateway profile at ${targetHome}\n`);
160
- return;
161
- }
162
-
163
- const copied = [];
164
- for (const file of ['config.yaml', 'SOUL.md']) {
165
- if (copyHermesProfileFile(sourceHome, targetHome, file, { overwrite: false })) {
166
- copied.push(file);
167
- }
168
- }
169
- if (copyHermesProfileEnv(sourceHome, targetHome)) {
170
- copied.push('.env (without messaging platform credentials)');
171
- }
172
- for (const file of ['auth.json']) {
173
- if (copyHermesProfileFile(sourceHome, targetHome, file, { overwrite: true })) {
174
- copied.push(file);
175
- }
176
- }
177
-
178
- appendGatewayLog(
179
- gateway,
180
- 'meta',
181
- copied.length > 0
182
- ? `Seeded Pixcode Hermes gateway profile from ${sourceHome}: ${copied.join(', ')}\n`
183
- : `Using Pixcode Hermes gateway profile at ${targetHome}\n`,
184
- );
185
- }
186
-
187
- export function buildHermesGatewayEnv(baseEnv = process.env, options = {}) {
188
- const host = options.host || DEFAULT_HOST;
189
- const port = String(options.port || DEFAULT_PORT);
190
- return buildHermesPathEnv(baseEnv, {
191
- API_SERVER_ENABLED: 'true',
192
- API_SERVER_HOST: host,
193
- API_SERVER_PORT: port,
194
- API_SERVER_KEY: options.apiServerKey || makeApiServerKey(),
195
- API_SERVER_CORS_ORIGINS: options.corsOrigins || options.pixcodeBaseUrl || '',
196
- PIXCODE_BASE_URL: options.pixcodeBaseUrl || '',
197
- PIXCODE_API_KEY: options.pixcodeApiKey || '',
198
- PIXCODE_APP_ROOT: options.appRoot || process.cwd(),
199
- HERMES_HOME: options.hermesHome || '',
200
- HERMES_INSTALL_DIR: options.installDir || '',
201
- });
202
- }
203
-
204
- function isPortAvailable(port, host) {
205
- return new Promise((resolve) => {
206
- const server = net.createServer();
207
- server.once('error', () => resolve(false));
208
- server.once('listening', () => {
209
- server.close(() => resolve(true));
210
- });
211
- server.listen(port, host);
212
- });
213
- }
214
-
215
- async function findAvailablePort(preferredPort, host) {
216
- const start = Number.isFinite(preferredPort) ? preferredPort : DEFAULT_PORT;
217
- for (let offset = 0; offset < PORT_SCAN_LIMIT; offset += 1) {
218
- const port = start + offset;
219
- if (await isPortAvailable(port, host)) {
220
- return port;
221
- }
222
- }
223
- throw new Error(`No available Hermes API server port found from ${start} to ${start + PORT_SCAN_LIMIT - 1}.`);
224
- }
225
-
226
- function fetchJson(url, options = {}) {
227
- const controller = new AbortController();
228
- const timeout = setTimeout(() => controller.abort(), options.timeoutMs || FETCH_TIMEOUT_MS);
229
- return fetch(url, {
230
- ...options,
231
- signal: controller.signal,
232
- headers: {
233
- accept: 'application/json',
234
- ...(options.headers || {}),
235
- },
236
- }).then(async (response) => {
237
- const text = await response.text();
238
- let body = null;
239
- try {
240
- body = text ? JSON.parse(text) : null;
241
- } catch {
242
- body = text;
243
- }
244
-
245
- return {
246
- ok: response.ok,
247
- status: response.status,
248
- body,
249
- };
250
- }).finally(() => clearTimeout(timeout));
251
- }
252
-
253
- async function callGateway(gateway, endpoint, options = {}) {
254
- return fetchJson(`${gateway.baseUrl}${endpoint}`, {
255
- ...options,
256
- headers: {
257
- Authorization: `Bearer ${gateway.apiServerKey}`,
258
- 'content-type': 'application/json',
259
- ...(options.headers || {}),
260
- },
261
- });
262
- }
263
-
264
- function extractRunId(body) {
265
- if (!body || typeof body !== 'object') return null;
266
- return body.run_id || body.runId || body.id || body.run?.id || null;
267
- }
268
-
269
- function extractRunStatus(body) {
270
- if (!body || typeof body !== 'object') return null;
271
- return body.status || body.state || body.run?.status || body.run?.state || null;
272
- }
273
-
274
- function extractTextFromValue(value) {
275
- if (typeof value === 'string') return value;
276
- if (!value) return null;
277
-
278
- if (Array.isArray(value)) {
279
- return value
280
- .map(extractTextFromValue)
281
- .filter(Boolean)
282
- .join('\n')
283
- .trim() || null;
284
- }
285
-
286
- if (typeof value === 'object') {
287
- for (const key of ['text', 'content', 'message', 'output', 'response', 'result', 'final']) {
288
- const text = extractTextFromValue(value[key]);
289
- if (text) return text;
290
- }
291
- }
292
-
293
- return null;
294
- }
295
-
296
- function extractRunOutput(body) {
297
- if (!body || typeof body !== 'object') return null;
298
-
299
- for (const key of ['output_text', 'output', 'response', 'result', 'message', 'messages', 'events', 'final']) {
300
- const text = extractTextFromValue(body[key]);
301
- if (text) return text;
302
- }
303
-
304
- return null;
305
- }
306
-
307
- function extractResponsesOutput(body) {
308
- if (!body || typeof body !== 'object') return null;
309
-
310
- const output = Array.isArray(body.output) ? body.output : [];
311
- for (const item of output) {
312
- if (!item || typeof item !== 'object') continue;
313
- if (item.type === 'message' || item.role === 'assistant') {
314
- const text = extractTextFromValue(item.content);
315
- if (text) return text;
316
- }
317
- const text = extractTextFromValue(item.output_text)
318
- || extractTextFromValue(item.text)
319
- || extractTextFromValue(item.message)
320
- || extractTextFromValue(item.output);
321
- if (text) return text;
322
- }
323
-
324
- return extractTextFromValue(body.output_text)
325
- || extractTextFromValue(body.message)
326
- || extractTextFromValue(body.response)
327
- || null;
328
- }
329
-
330
- function extractChatCompletionOutput(body) {
331
- if (!body || typeof body !== 'object') return null;
332
- const choices = Array.isArray(body.choices) ? body.choices : [];
333
- for (const choice of choices) {
334
- const text = extractTextFromValue(choice?.message?.content)
335
- || extractTextFromValue(choice?.delta?.content)
336
- || extractTextFromValue(choice?.text);
337
- if (text) return text;
338
- }
339
- return extractTextFromValue(body.output_text)
340
- || extractTextFromValue(body.output)
341
- || extractTextFromValue(body.message)
342
- || extractTextFromValue(body.response)
343
- || null;
344
- }
345
-
346
- function recentGatewayLogText(gateway) {
347
- if (!gateway?.logs?.length) return '';
348
- return gateway.logs
349
- .slice(-16)
350
- .map((entry) => String(entry.chunk || '').trim())
351
- .filter(Boolean)
352
- .join('\n')
353
- .trim();
354
- }
355
-
356
- function readFileTail(filePath, maxBytes = HERMES_DIAGNOSTIC_LOG_BYTES) {
357
- try {
358
- const stat = fs.statSync(filePath);
359
- const length = Math.min(maxBytes, stat.size);
360
- const buffer = Buffer.alloc(length);
361
- const fd = fs.openSync(filePath, 'r');
362
- try {
363
- fs.readSync(fd, buffer, 0, length, stat.size - length);
364
- } finally {
365
- fs.closeSync(fd);
366
- }
367
- return buffer.toString('utf8');
368
- } catch {
369
- return '';
370
- }
371
- }
372
-
373
- function readJsonFileSafe(filePath) {
374
- try {
375
- return JSON.parse(fs.readFileSync(filePath, 'utf8'));
376
- } catch {
377
- return null;
378
- }
379
- }
380
-
381
- function redactDiagnosticText(text) {
382
- return String(text || '')
383
- .replace(/\b(px_|ck_|sk-|ghp_|npm_)[A-Za-z0-9._-]+/gu, '$1[redacted]')
384
- .replace(/\b(Bearer\s+)[A-Za-z0-9._~+/=-]+/giu, '$1[redacted]')
385
- .replace(/((?:api[_-]?key|authorization|access[_-]?token|refresh[_-]?token|id[_-]?token|token)\s*[:=]\s*["']?)[^"',\s}]+/giu, '$1[redacted]');
386
- }
387
-
388
- function findRootBlockEnd(lines, startIndex) {
389
- for (let index = startIndex + 1; index < lines.length; index += 1) {
390
- if (/^\S[^:]*:\s*(?:#.*)?$/u.test(lines[index])) {
391
- return index;
392
- }
393
- }
394
- return lines.length;
395
- }
396
-
397
- function readRootList(text, key) {
398
- const lines = String(text || '').split(/\r?\n/);
399
- const start = lines.findIndex((line) => new RegExp(`^${key}:\\s*(?:#.*)?$`, 'u').test(line));
400
- if (start === -1) return [];
401
- const end = findRootBlockEnd(lines, start);
402
- const values = [];
403
- for (let index = start + 1; index < end; index += 1) {
404
- const match = lines[index].match(/^\s*-\s*([^#\s][^#]*?)(?:\s+#.*)?$/u);
405
- if (match) values.push(match[1].trim().replace(/^['"]|['"]$/gu, ''));
406
- }
407
- return values;
408
- }
409
-
410
- function readRootMap(text, key) {
411
- const lines = String(text || '').split(/\r?\n/);
412
- const start = lines.findIndex((line) => new RegExp(`^${key}:\\s*(?:#.*)?$`, 'u').test(line));
413
- if (start === -1) return {};
414
- const end = findRootBlockEnd(lines, start);
415
- const values = {};
416
- for (let index = start + 1; index < end; index += 1) {
417
- const match = lines[index].match(/^\s+([A-Za-z0-9_.-]+):\s*(.*?)(?:\s+#.*)?$/u);
418
- if (!match) continue;
419
- values[match[1]] = match[2].trim().replace(/^['"]|['"]$/gu, '');
420
- }
421
- return values;
422
- }
423
-
424
- function readPixcodeMcpTools(text) {
425
- return Array.from(new Set(
426
- Array.from(String(text || '').matchAll(/^\s*-\s*(pixcode_[A-Za-z0-9_]+)\s*$/gmu))
427
- .map((match) => match[1]),
428
- ));
429
- }
430
-
431
- function readApiServerToolset(text) {
432
- const platformText = String(text || '');
433
- return {
434
- hasHermesApiServer: /^\s*-\s*hermes-api-server\s*$/gmu.test(platformText),
435
- hasPixcodePlatform: /^\s*-\s*pixcode\s*$/gmu.test(platformText),
436
- };
437
- }
438
-
439
- function summarizeHermesConfig(hermesHome) {
440
- const configPath = path.join(hermesHome, 'config.yaml');
441
- const text = readFileTail(configPath, HERMES_DIAGNOSTIC_LOG_BYTES);
442
- const toolsets = readRootList(text, 'toolsets');
443
- const pixcodeTools = readPixcodeMcpTools(text);
444
- const missingPixcodeTools = EXPECTED_PIXCODE_MCP_TOOLS.filter((tool) => !pixcodeTools.includes(tool));
445
- return {
446
- path: configPath,
447
- exists: Boolean(text),
448
- model: readRootMap(text, 'model'),
449
- toolsets,
450
- platformToolsets: readApiServerToolset(text),
451
- pixcodeMcp: {
452
- configured: /mcp_servers:[\s\S]*^\s+pixcode:\s*$/mu.test(text),
453
- enabled: /mcp_servers:[\s\S]*^\s+pixcode:[\s\S]*^\s+enabled:\s*true\s*$/mu.test(text),
454
- toolCount: pixcodeTools.length,
455
- tools: pixcodeTools,
456
- missingTools: missingPixcodeTools,
457
- },
458
- staleToolsetConfig: toolsets.includes('mcp-pixcode') && !toolsets.includes('hermes-cli'),
459
- };
460
- }
461
-
462
- function summarizeHermesAuth(hermesHome, provider) {
463
- const authPath = path.join(hermesHome, 'auth.json');
464
- const auth = readJsonFileSafe(authPath);
465
- const providers = auth && typeof auth === 'object' && auth.providers && typeof auth.providers === 'object'
466
- ? Object.keys(auth.providers)
467
- : [];
468
- const pools = auth && typeof auth === 'object' && auth.credential_pool && typeof auth.credential_pool === 'object'
469
- ? auth.credential_pool
470
- : {};
471
- const selectedProvider = provider || auth?.active_provider || null;
472
- const providerEntry = selectedProvider && auth?.providers && typeof auth.providers === 'object'
473
- ? auth.providers[selectedProvider]
474
- : null;
475
- return {
476
- path: authPath,
477
- exists: Boolean(auth),
478
- activeProvider: auth?.active_provider || null,
479
- providers,
480
- selectedProvider,
481
- selectedProviderConfigured: Boolean(providerEntry),
482
- selectedProviderLastRefresh: providerEntry?.last_refresh || null,
483
- selectedProviderAuthMode: providerEntry?.auth_mode || null,
484
- selectedProviderPoolSize: selectedProvider && Array.isArray(pools?.[selectedProvider])
485
- ? pools[selectedProvider].length
486
- : 0,
487
- };
488
- }
489
-
490
- function summarizeHermesLogs(hermesHomes) {
491
- const files = [];
492
- const seen = new Set();
493
- for (const home of hermesHomes.filter(Boolean)) {
494
- for (const name of ['errors.log', 'agent.log']) {
495
- const filePath = path.join(home, 'logs', name);
496
- if (seen.has(filePath)) continue;
497
- seen.add(filePath);
498
- const text = redactDiagnosticText(readFileTail(filePath));
499
- if (!text) continue;
500
- files.push({
501
- path: filePath,
502
- name,
503
- recent: text.split(/\r?\n/).filter(Boolean).slice(-80),
504
- });
505
- }
506
- }
507
- const combined = files.flatMap((file) => file.recent).join('\n');
508
- return {
509
- files,
510
- signals: {
511
- codexNoneType: /NoneType' object is not iterable|NoneType object is not iterable/iu.test(combined),
512
- codexOauthMissing: /openai-codex requested but no Codex OAuth .*found/iu.test(combined),
513
- mcpTimeout: /MCP call timed out|pixcode_open_cli_terminal call failed/iu.test(combined),
514
- stalePixcodeMcpToolCount: /MCP server 'pixcode'.*registered\s+[0-9]\s+tool\(s\)/iu.test(combined)
515
- && !/registered\s+1[0-9]\s+tool\(s\)/iu.test(combined),
516
- },
517
- };
518
- }
519
-
520
- function listHermesProfileHomes(sourceHermesHome) {
521
- const profiles = [{
522
- name: 'default',
523
- isDefault: true,
524
- path: sourceHermesHome,
525
- }];
526
- const profilesDir = path.join(sourceHermesHome, 'profiles');
527
- try {
528
- for (const name of fs.readdirSync(profilesDir)) {
529
- if (name.startsWith('.') || !/^[a-z0-9][a-z0-9_-]{0,63}$/iu.test(name)) continue;
530
- const profilePath = path.join(profilesDir, name);
531
- if (!fs.statSync(profilePath).isDirectory()) continue;
532
- profiles.push({
533
- name,
534
- isDefault: false,
535
- path: profilePath,
536
- });
537
- }
538
- } catch {
539
- // No named Hermes profiles yet.
540
- }
541
- return profiles;
542
- }
543
-
544
- function readActiveHermesProfile(sourceHermesHome) {
545
- try {
546
- const active = fs.readFileSync(path.join(sourceHermesHome, 'active_profile'), 'utf8').trim();
547
- return active || 'default';
548
- } catch {
549
- return 'default';
550
- }
551
- }
552
-
553
- function readHermesSessionSummary(hermesHome, limit = 5) {
554
- const dbPath = path.join(hermesHome, 'state.db');
555
- if (!fs.existsSync(dbPath)) {
556
- return {
557
- dbPath,
558
- exists: false,
559
- total: 0,
560
- recent: [],
561
- };
562
- }
563
-
564
- let db = null;
565
- try {
566
- db = new Database(dbPath, { readonly: true, fileMustExist: true });
567
- const total = db.prepare('SELECT COUNT(*) AS count FROM sessions').get()?.count ?? 0;
568
- const recent = db.prepare(`
569
- SELECT id, source, started_at, ended_at, message_count, model, title
570
- FROM sessions
571
- ORDER BY started_at DESC
572
- LIMIT ?
573
- `).all(limit).map((row) => ({
574
- id: row.id,
575
- source: row.source || null,
576
- startedAt: row.started_at || null,
577
- endedAt: row.ended_at || null,
578
- messageCount: row.message_count || 0,
579
- model: row.model || null,
580
- title: row.title || null,
581
- }));
582
- return {
583
- dbPath,
584
- exists: true,
585
- total,
586
- recent,
587
- };
588
- } catch (error) {
589
- return {
590
- dbPath,
591
- exists: true,
592
- total: 0,
593
- recent: [],
594
- error: error instanceof Error ? error.message : String(error),
595
- };
596
- } finally {
597
- try {
598
- db?.close();
599
- } catch {
600
- // ignore close errors
601
- }
602
- }
603
- }
604
-
605
- function normalizeCronJob(job) {
606
- if (!job || typeof job !== 'object') return null;
607
- const schedule = typeof job.schedule === 'object' && job.schedule
608
- ? job.schedule.value
609
- : job.schedule;
610
- const enabled = job.enabled !== false;
611
- return {
612
- id: String(job.id || job.name || ''),
613
- name: String(job.name || job.id || '(unnamed)'),
614
- schedule: String(job.schedule_display || schedule || ''),
615
- state: job.state === 'paused' || !enabled
616
- ? 'paused'
617
- : job.state === 'completed'
618
- ? 'completed'
619
- : 'active',
620
- enabled,
621
- nextRunAt: job.next_run_at || null,
622
- lastRunAt: job.last_run_at || null,
623
- lastStatus: job.last_status || null,
624
- lastError: job.last_error || null,
625
- };
626
- }
627
-
628
- function readHermesCronSummary(hermesHome, limit = 6) {
629
- const jobsPath = path.join(hermesHome, 'cron', 'jobs.json');
630
- if (!fs.existsSync(jobsPath)) {
631
- return {
632
- jobsPath,
633
- exists: false,
634
- total: 0,
635
- active: 0,
636
- paused: 0,
637
- completed: 0,
638
- recent: [],
639
- };
640
- }
641
-
642
- try {
643
- const parsed = JSON.parse(fs.readFileSync(jobsPath, 'utf8'));
644
- const rawJobs = Array.isArray(parsed) ? parsed : Array.isArray(parsed.jobs) ? parsed.jobs : [];
645
- const jobs = rawJobs.map(normalizeCronJob).filter(Boolean);
646
- return {
647
- jobsPath,
648
- exists: true,
649
- total: jobs.length,
650
- active: jobs.filter((job) => job.state === 'active').length,
651
- paused: jobs.filter((job) => job.state === 'paused').length,
652
- completed: jobs.filter((job) => job.state === 'completed').length,
653
- recent: jobs.slice(0, limit),
654
- };
655
- } catch (error) {
656
- return {
657
- jobsPath,
658
- exists: true,
659
- total: 0,
660
- active: 0,
661
- paused: 0,
662
- completed: 0,
663
- recent: [],
664
- error: error instanceof Error ? error.message : String(error),
665
- };
666
- }
667
- }
668
-
669
- function summarizeHermesProfile(profile, activeProfile, gatewaySnapshots) {
670
- const config = summarizeHermesConfig(profile.path);
671
- const provider = config.model.provider || null;
672
- const auth = summarizeHermesAuth(profile.path, provider);
673
- const gateway = gatewaySnapshots.find((snapshot) => path.resolve(snapshot.hermesHome || '') === path.resolve(profile.path)) || null;
674
- const sessions = readHermesSessionSummary(profile.path);
675
- const cron = readHermesCronSummary(profile.path);
676
-
677
- return {
678
- name: profile.name,
679
- path: profile.path,
680
- isDefault: profile.isDefault,
681
- isActive: profile.name === activeProfile,
682
- model: {
683
- provider,
684
- default: config.model.default || null,
685
- baseUrl: config.model.base_url || null,
686
- },
687
- auth: {
688
- configured: auth.selectedProviderConfigured,
689
- activeProvider: auth.activeProvider,
690
- selectedProvider: auth.selectedProvider,
691
- poolSize: auth.selectedProviderPoolSize,
692
- lastRefresh: auth.selectedProviderLastRefresh,
693
- },
694
- tools: {
695
- toolsets: config.toolsets,
696
- pixcodeMcpConfigured: config.pixcodeMcp.configured,
697
- pixcodeMcpEnabled: config.pixcodeMcp.enabled,
698
- pixcodeMcpToolCount: config.pixcodeMcp.toolCount,
699
- missingPixcodeMcpTools: config.pixcodeMcp.missingTools,
700
- hermesCliReady: config.toolsets.includes('hermes-cli'),
701
- pixcodeMcpReady: config.toolsets.includes('mcp-pixcode') && config.pixcodeMcp.enabled && config.pixcodeMcp.missingTools.length === 0,
702
- },
703
- sessions,
704
- cron,
705
- gateway,
706
- };
707
- }
708
-
709
- function gatewayExitMessage(gateway, fallback = 'Hermes gateway is not running.') {
710
- if (!gateway) return fallback;
711
- const exit = gateway.exitSignal
712
- ? `Hermes gateway exited with signal ${gateway.exitSignal}.`
713
- : `Hermes gateway exited with code ${gateway.exitCode ?? 'unknown'}.`;
714
- const logs = recentGatewayLogText(gateway);
715
- return logs ? `${exit}\n${logs}` : (gateway.error || exit);
716
- }
717
-
718
- function normalizeGatewayEndpoint(endpoint) {
719
- const value = typeof endpoint === 'string' ? endpoint.trim() : '';
720
- if (!value) {
721
- throw new Error('Hermes gateway endpoint is required.');
722
- }
723
- if (/^[a-z][a-z0-9+.-]*:\/\//iu.test(value) || value.startsWith('//')) {
724
- throw new Error('Hermes gateway endpoint must be local; external URLs are not allowed.');
725
- }
726
- if (!value.startsWith('/')) {
727
- throw new Error('Hermes gateway endpoint must start with /.');
728
- }
729
- if (
730
- value !== '/health' &&
731
- value !== '/health/detailed' &&
732
- !value.startsWith('/v1/') &&
733
- !value.startsWith('/api/')
734
- ) {
735
- throw new Error('Hermes gateway endpoint must be /health, /v1/..., or /api/....');
736
- }
737
- return value;
738
- }
739
-
740
- function normalizeGatewayRequestMethod(method) {
741
- const value = String(method || 'GET').trim().toUpperCase();
742
- if (!ALLOWED_GATEWAY_REQUEST_METHODS.has(value)) {
743
- throw new Error(`Unsupported Hermes gateway HTTP method: ${value || '(empty)'}`);
744
- }
745
- return value;
746
- }
747
-
748
- function makeRunRequest(options) {
749
- const input = String(options.input || '').trim();
750
- return {
751
- session_id: options.sessionId || `pixcode-hermes-chat-${Date.now()}-${randomBytes(4).toString('hex')}`,
752
- input,
753
- instructions: options.instructions || [
754
- 'You are Hermes Agent running inside Pixcode.',
755
- 'Use Pixcode MCP tools when they help inspect projects, launch CLIs, or perform workspace actions.',
756
- 'Keep answers concise and include concrete next steps when work is blocked.',
757
- ].join(' '),
758
- };
759
- }
760
-
761
- function makeChatCompletionRequest(options) {
762
- const input = String(options.input || '').trim();
763
- const messages = Array.isArray(options.messages) ? options.messages : [
764
- {
765
- role: 'system',
766
- content: options.instructions || [
767
- 'You are Hermes Agent running inside Pixcode.',
768
- 'Use Pixcode MCP tools when they help inspect projects, launch CLIs, or perform workspace actions.',
769
- 'Keep answers concise and include concrete next steps when work is blocked.',
770
- ].join(' '),
771
- },
772
- {
773
- role: 'user',
774
- content: input,
775
- },
776
- ];
777
- return {
778
- model: options.model || 'hermes-agent',
779
- messages,
780
- stream: false,
781
- };
782
- }
783
-
784
- function makeResponsesRequest(options) {
785
- const input = String(options.input || '').trim();
786
- return {
787
- model: options.model || 'hermes-agent',
788
- input,
789
- instructions: options.instructions || [
790
- 'You are Hermes Agent running inside Pixcode.',
791
- 'Use Pixcode MCP tools when they help inspect projects, launch CLIs, or perform workspace actions.',
792
- 'Keep answers concise and include concrete next steps when work is blocked.',
793
- ].join(' '),
794
- conversation: options.sessionId || undefined,
795
- store: true,
796
- };
797
- }
798
-
799
- async function waitForGatewayReady(gateway) {
800
- const started = Date.now();
801
- let lastError = null;
802
-
803
- while (Date.now() - started < STARTUP_TIMEOUT_MS) {
804
- if (!isGatewayRunning(gateway)) {
805
- throw new Error(gatewayExitMessage(gateway));
806
- }
807
-
808
- try {
809
- const probe = await probeHermesGateway(gateway.projectPath, { requireRunning: true });
810
- if (probe.ok) {
811
- return probe;
812
- }
813
- lastError = probe.error || 'Gateway probe failed.';
814
- } catch (error) {
815
- lastError = error instanceof Error ? error.message : String(error);
816
- }
817
-
818
- await new Promise((resolve) => setTimeout(resolve, 500));
819
- }
820
-
821
- throw new Error(`Hermes gateway did not become ready within ${STARTUP_TIMEOUT_MS / 1000}s: ${lastError || 'no response'}`);
822
- }
823
-
824
- function runProcess(command, args, options, onData) {
825
- return new Promise((resolve, reject) => {
826
- const child = spawn(command, args, {
827
- ...options,
828
- stdio: ['ignore', 'pipe', 'pipe'],
829
- windowsHide: true,
830
- });
831
- child.stdout?.on('data', (buf) => onData?.('stdout', buf.toString()));
832
- child.stderr?.on('data', (buf) => onData?.('stderr', buf.toString()));
833
- child.on('error', reject);
834
- child.on('close', (code, signal) => {
835
- if (signal) {
836
- reject(new Error(`${command} killed by ${signal}`));
837
- return;
838
- }
839
- resolve(code ?? 0);
840
- });
841
- });
842
- }
843
-
844
- async function configurePixcodeMcp({ appRoot, env, gateway }) {
845
- const configureScript = path.join(appRoot, 'scripts', 'hermes', 'configure-pixcode-mcp.mjs');
846
- const code = await runProcess(process.execPath, [configureScript], {
847
- cwd: appRoot,
848
- env,
849
- }, (stream, chunk) => appendGatewayLog(gateway, stream, chunk));
850
-
851
- if (code !== 0) {
852
- throw new Error(`Pixcode MCP configuration exited with code ${code}`);
853
- }
854
- }
855
-
856
- function snapshotGateway(gateway) {
857
- if (!gateway) {
858
- return {
859
- running: false,
860
- projectPath: null,
861
- baseUrl: null,
862
- hermesHome: null,
863
- host: null,
864
- port: null,
865
- pid: null,
866
- startedAt: null,
867
- exitedAt: null,
868
- exitCode: null,
869
- exitSignal: null,
870
- error: null,
871
- lastProbe: null,
872
- logs: [],
873
- };
874
- }
875
-
876
- return {
877
- running: isGatewayRunning(gateway),
878
- projectPath: gateway.projectPath,
879
- baseUrl: gateway.baseUrl,
880
- hermesHome: gateway.hermesHome,
881
- host: gateway.host,
882
- port: gateway.port,
883
- pid: gateway.child?.pid ?? null,
884
- startedAt: gateway.startedAt,
885
- exitedAt: gateway.exitedAt,
886
- exitCode: gateway.exitCode,
887
- exitSignal: gateway.exitSignal,
888
- error: gateway.error,
889
- lastProbe: gateway.lastProbe,
890
- logs: gateway.logs.slice(-80),
891
- };
892
- }
893
-
894
- export function getHermesGatewayStatus(projectPath) {
895
- if (projectPath) {
896
- return snapshotGateway(gateways.get(normalizeProjectPath(projectPath)));
897
- }
898
-
899
- const active = Array.from(gateways.values()).filter(isGatewayRunning);
900
- return {
901
- running: active.length > 0,
902
- gateways: Array.from(gateways.values()).map(snapshotGateway),
903
- };
904
- }
905
-
906
- export async function ensureHermesGateway(options = {}) {
907
- const projectPath = normalizeProjectPath(options.projectPath);
908
- const existing = gateways.get(projectPath);
909
- if (isGatewayRunning(existing)) {
910
- if (options.probeExisting !== true) {
911
- return {
912
- ...snapshotGateway(existing),
913
- probe: existing.lastProbe,
914
- };
915
- }
916
-
917
- const probe = await probeHermesGateway(projectPath, { requireRunning: true }).catch((error) => ({
918
- ok: false,
919
- error: error instanceof Error ? error.message : String(error),
920
- }));
921
- if (probe.ok || options.replaceUnhealthy !== true) {
922
- return {
923
- ...snapshotGateway(existing),
924
- probe,
925
- };
926
- }
927
-
928
- stopHermesGateway(projectPath);
929
- }
930
-
931
- const host = options.host || DEFAULT_HOST;
932
- const port = await findAvailablePort(Number(options.port || process.env.HERMES_API_SERVER_PORT || DEFAULT_PORT), host);
933
- const apiServerKey = options.apiServerKey || makeApiServerKey();
934
- const appRoot = options.appRoot || process.cwd();
935
- const sourceHermesHome = options.sourceHermesHome || resolveSourceHermesHome(process.env);
936
- const hermesHome = resolveHermesGatewayHome(process.env, options);
937
- const env = buildHermesGatewayEnv(process.env, {
938
- ...options,
939
- host,
940
- port,
941
- apiServerKey,
942
- appRoot,
943
- hermesHome,
944
- });
945
- const installStatus = readHermesInstallStatus(env, {
946
- allowSmokeHermes: options.allowSmokeHermes === true,
947
- repairLaunchers: options.repairLaunchers !== false,
948
- });
949
- if (!installStatus.installed || !installStatus.command) {
950
- throw new Error(installStatus.error || 'Hermes Agent CLI is not installed.');
951
- }
952
-
953
- const gateway = {
954
- id: `${projectPath}:${port}`,
955
- projectPath,
956
- host,
957
- port,
958
- baseUrl: gatewayBaseUrl(host, port),
959
- hermesHome,
960
- apiServerKey,
961
- command: installStatus.command,
962
- child: null,
963
- startedAt: nowIso(),
964
- exitedAt: null,
965
- exitCode: null,
966
- exitSignal: null,
967
- error: null,
968
- lastProbe: null,
969
- logs: [],
970
- };
971
- gateways.set(projectPath, gateway);
972
-
973
- seedHermesGatewayHome({ sourceHome: sourceHermesHome, targetHome: hermesHome, gateway });
974
- await configurePixcodeMcp({ appRoot, env, gateway });
975
-
976
- const gatewayArgs = options.gatewayArgs || ['gateway', 'run', '--replace'];
977
- const child = spawn(installStatus.command, gatewayArgs, {
978
- cwd: projectPath,
979
- env,
980
- stdio: ['ignore', 'pipe', 'pipe'],
981
- windowsHide: true,
982
- });
983
- gateway.child = child;
984
- appendGatewayLog(gateway, 'meta', `$ ${installStatus.command} ${gatewayArgs.join(' ')}\n`);
985
-
986
- child.stdout?.on('data', (buf) => appendGatewayLog(gateway, 'stdout', buf.toString()));
987
- child.stderr?.on('data', (buf) => appendGatewayLog(gateway, 'stderr', buf.toString()));
988
- child.on('error', (error) => {
989
- gateway.error = error instanceof Error ? error.message : String(error);
990
- appendGatewayLog(gateway, 'stderr', `${gateway.error}\n`);
991
- });
992
- child.on('exit', (code, signal) => {
993
- gateway.exitCode = code;
994
- gateway.exitSignal = signal;
995
- gateway.exitedAt = nowIso();
996
- appendGatewayLog(gateway, 'meta', `Hermes gateway exited with code ${code}${signal ? ` (${signal})` : ''}\n`);
997
- });
998
-
999
- const probe = await waitForGatewayReady(gateway);
1000
- return {
1001
- ...snapshotGateway(gateway),
1002
- probe,
1003
- };
1004
- }
1005
-
1006
- export async function probeHermesGateway(projectPath, options = {}) {
1007
- const gateway = projectPath
1008
- ? gateways.get(normalizeProjectPath(projectPath))
1009
- : Array.from(gateways.values()).find(isGatewayRunning);
1010
-
1011
- if (!isGatewayRunning(gateway)) {
1012
- const result = {
1013
- ok: false,
1014
- error: 'Hermes gateway is not running.',
1015
- projectPath: projectPath ? normalizeProjectPath(projectPath) : null,
1016
- baseUrl: null,
1017
- checks: {},
1018
- };
1019
- if (options.requireRunning) return result;
1020
- return result;
1021
- }
1022
-
1023
- const checks = {};
1024
- try {
1025
- checks.health = await fetchJson(`${gateway.baseUrl}/health`);
1026
- } catch (error) {
1027
- checks.health = { ok: false, status: 0, error: error instanceof Error ? error.message : String(error) };
1028
- }
1029
-
1030
- try {
1031
- checks.capabilities = await callGateway(gateway, '/v1/capabilities');
1032
- } catch (error) {
1033
- checks.capabilities = { ok: false, status: 0, error: error instanceof Error ? error.message : String(error) };
1034
- }
1035
-
1036
- try {
1037
- checks.models = await callGateway(gateway, '/v1/models');
1038
- } catch (error) {
1039
- checks.models = { ok: false, status: 0, error: error instanceof Error ? error.message : String(error) };
1040
- }
1041
-
1042
- if (typeof options.input === 'string' && options.input.trim()) {
1043
- try {
1044
- const run = await runHermesGatewayPrompt(gateway.projectPath, {
1045
- input: options.input.trim(),
1046
- sessionId: options.sessionId || `pixcode-probe-${Date.now()}`,
1047
- instructions: options.instructions || 'Respond briefly for a Pixcode REST integration check.',
1048
- timeoutMs: options.runTimeoutMs || 30000,
1049
- });
1050
- checks.run = {
1051
- ok: run.ok,
1052
- status: run.httpStatus || 200,
1053
- body: run,
1054
- error: run.error || null,
1055
- };
1056
- } catch (error) {
1057
- checks.run = { ok: false, status: 0, error: error instanceof Error ? error.message : String(error) };
1058
- }
1059
- }
1060
-
1061
- const ok = Boolean(
1062
- checks.health?.ok &&
1063
- checks.capabilities?.ok &&
1064
- checks.models?.ok &&
1065
- (!checks.run || checks.run.ok),
1066
- );
1067
- const result = {
1068
- ok,
1069
- projectPath: gateway.projectPath,
1070
- baseUrl: gateway.baseUrl,
1071
- checkedAt: nowIso(),
1072
- checks,
1073
- error: ok ? null : 'One or more Hermes REST checks failed.',
1074
- };
1075
- gateway.lastProbe = result;
1076
- return result;
1077
- }
1078
-
1079
- export async function runHermesGatewayPrompt(projectPath, options = {}) {
1080
- const gateway = projectPath
1081
- ? gateways.get(normalizeProjectPath(projectPath))
1082
- : Array.from(gateways.values()).find(isGatewayRunning);
1083
-
1084
- if (!isGatewayRunning(gateway)) {
1085
- throw new Error('Hermes gateway is not running.');
1086
- }
1087
-
1088
- const input = String(options.input || '').trim();
1089
- if (!input) {
1090
- throw new Error('Hermes prompt is required.');
1091
- }
1092
-
1093
- const responsesRequest = makeResponsesRequest({ ...options, input });
1094
- const responseRun = await callGateway(gateway, '/v1/responses', {
1095
- method: 'POST',
1096
- body: JSON.stringify(responsesRequest),
1097
- timeoutMs: options.responsesTimeoutMs || options.timeoutMs || RUN_TIMEOUT_MS,
1098
- }).catch((error) => {
1099
- if (!isGatewayRunning(gateway)) {
1100
- throw new Error(gatewayExitMessage(gateway));
1101
- }
1102
- throw error;
1103
- });
1104
-
1105
- if (!isGatewayRunning(gateway)) {
1106
- throw new Error(gatewayExitMessage(gateway));
1107
- }
1108
-
1109
- if (responseRun.ok) {
1110
- const status = extractRunStatus(responseRun.body) || 'completed';
1111
- const message = extractResponsesOutput(responseRun.body);
1112
- return {
1113
- ok: status === 'completed' || status === 'succeeded',
1114
- projectPath: gateway.projectPath,
1115
- baseUrl: gateway.baseUrl,
1116
- sessionId: options.sessionId || responsesRequest.conversation || null,
1117
- runId: null,
1118
- responseId: responseRun.body?.id || null,
1119
- status,
1120
- message,
1121
- error: (status === 'completed' || status === 'succeeded') ? null : extractTextFromValue(responseRun.body?.error) || message || 'Hermes response failed.',
1122
- raw: responseRun.body,
1123
- transport: 'responses',
1124
- endpoint: '/v1/responses',
1125
- httpStatus: responseRun.status,
1126
- };
1127
- }
1128
-
1129
- if (responseRun.status && responseRun.status !== 404 && responseRun.status !== 405) {
1130
- throw new Error(`Hermes /v1/responses failed with HTTP ${responseRun.status}: ${JSON.stringify(responseRun.body)}`);
1131
- }
1132
-
1133
- const chatRequest = makeChatCompletionRequest({ ...options, input });
1134
- const chat = await callGateway(gateway, '/v1/chat/completions', {
1135
- method: 'POST',
1136
- body: JSON.stringify(chatRequest),
1137
- timeoutMs: options.chatTimeoutMs || options.timeoutMs || RUN_TIMEOUT_MS,
1138
- }).catch((error) => {
1139
- if (!isGatewayRunning(gateway)) {
1140
- throw new Error(gatewayExitMessage(gateway));
1141
- }
1142
- throw error;
1143
- });
1144
-
1145
- if (!isGatewayRunning(gateway)) {
1146
- throw new Error(gatewayExitMessage(gateway));
1147
- }
1148
-
1149
- if (chat.ok) {
1150
- const message = extractChatCompletionOutput(chat.body);
1151
- return {
1152
- ok: true,
1153
- projectPath: gateway.projectPath,
1154
- baseUrl: gateway.baseUrl,
1155
- sessionId: options.sessionId || null,
1156
- runId: null,
1157
- status: 'completed',
1158
- message,
1159
- raw: chat.body,
1160
- transport: 'chat.completions',
1161
- endpoint: '/v1/chat/completions',
1162
- httpStatus: chat.status,
1163
- };
1164
- }
1165
-
1166
- if (chat.status && chat.status !== 404 && chat.status !== 405) {
1167
- throw new Error(`Hermes /v1/chat/completions failed with HTTP ${chat.status}: ${JSON.stringify(chat.body)}`);
1168
- }
1169
-
1170
- const request = makeRunRequest({ ...options, input });
1171
- const create = await callGateway(gateway, '/v1/runs', {
1172
- method: 'POST',
1173
- body: JSON.stringify(request),
1174
- timeoutMs: options.createTimeoutMs || 15000,
1175
- }).catch((error) => {
1176
- if (!isGatewayRunning(gateway)) {
1177
- throw new Error(gatewayExitMessage(gateway));
1178
- }
1179
- throw error;
1180
- });
1181
-
1182
- if (!isGatewayRunning(gateway)) {
1183
- throw new Error(gatewayExitMessage(gateway));
1184
- }
1185
-
1186
- if (!create.ok) {
1187
- throw new Error(`Hermes /v1/runs failed with HTTP ${create.status}: ${JSON.stringify(create.body)}`);
1188
- }
1189
-
1190
- const runId = extractRunId(create.body);
1191
- const initialStatus = extractRunStatus(create.body);
1192
- if (!runId) {
1193
- return {
1194
- ok: true,
1195
- projectPath: gateway.projectPath,
1196
- baseUrl: gateway.baseUrl,
1197
- sessionId: request.session_id,
1198
- runId: null,
1199
- status: initialStatus || 'completed',
1200
- message: extractRunOutput(create.body),
1201
- raw: create.body,
1202
- transport: 'runs',
1203
- endpoint: '/v1/runs',
1204
- httpStatus: create.status,
1205
- };
1206
- }
1207
-
1208
- const terminalStatuses = new Set(['completed', 'failed', 'cancelled', 'canceled']);
1209
- const started = Date.now();
1210
- let latest = create.body;
1211
- let status = initialStatus || 'queued';
1212
-
1213
- while (!terminalStatuses.has(String(status)) && Date.now() - started < (options.timeoutMs || RUN_TIMEOUT_MS)) {
1214
- await sleep(options.pollIntervalMs || RUN_POLL_INTERVAL_MS);
1215
- const poll = await callGateway(gateway, `/v1/runs/${encodeURIComponent(runId)}`, {
1216
- timeoutMs: options.pollTimeoutMs || 15000,
1217
- });
1218
- if (!poll.ok) {
1219
- throw new Error(`Hermes /v1/runs/${runId} failed with HTTP ${poll.status}: ${JSON.stringify(poll.body)}`);
1220
- }
1221
- if (!isGatewayRunning(gateway)) {
1222
- throw new Error(gatewayExitMessage(gateway));
1223
- }
1224
- latest = poll.body;
1225
- status = extractRunStatus(latest) || status;
1226
- }
1227
-
1228
- if (!terminalStatuses.has(String(status))) {
1229
- throw new Error(`Hermes run did not finish within ${Math.round((options.timeoutMs || RUN_TIMEOUT_MS) / 1000)}s: ${runId}`);
1230
- }
1231
-
1232
- const message = extractRunOutput(latest);
1233
- return {
1234
- ok: status === 'completed',
1235
- projectPath: gateway.projectPath,
1236
- baseUrl: gateway.baseUrl,
1237
- sessionId: request.session_id,
1238
- runId,
1239
- status,
1240
- message,
1241
- error: status === 'completed' ? null : extractTextFromValue(latest?.error) || message || 'Hermes run failed.',
1242
- raw: latest,
1243
- transport: 'runs',
1244
- endpoint: '/v1/runs',
1245
- httpStatus: create.status,
1246
- };
1247
- }
1248
-
1249
- export async function requestHermesGateway(projectPath, options = {}) {
1250
- const gateway = projectPath
1251
- ? gateways.get(normalizeProjectPath(projectPath))
1252
- : Array.from(gateways.values()).find(isGatewayRunning);
1253
-
1254
- if (!isGatewayRunning(gateway)) {
1255
- throw new Error('Hermes gateway is not running.');
1256
- }
1257
-
1258
- const endpoint = normalizeGatewayEndpoint(options.endpoint || options.path);
1259
- const method = normalizeGatewayRequestMethod(options.method);
1260
- const requestOptions = {
1261
- method,
1262
- timeoutMs: options.timeoutMs || FETCH_TIMEOUT_MS,
1263
- };
1264
- if (typeof options.body !== 'undefined' && options.body !== null && method !== 'GET') {
1265
- requestOptions.body = JSON.stringify(options.body);
1266
- }
1267
-
1268
- const response = await callGateway(gateway, endpoint, requestOptions);
1269
- return {
1270
- ok: response.ok,
1271
- status: response.status,
1272
- projectPath: gateway.projectPath,
1273
- baseUrl: gateway.baseUrl,
1274
- endpoint,
1275
- method,
1276
- body: response.body,
1277
- error: response.ok ? null : `Hermes gateway ${method} ${endpoint} failed with HTTP ${response.status}.`,
1278
- };
1279
- }
1280
-
1281
- export async function readHermesDiagnostics(options = {}) {
1282
- const projectPath = options.projectPath ? normalizeProjectPath(options.projectPath) : null;
1283
- const gateway = projectPath
1284
- ? gateways.get(projectPath)
1285
- : Array.from(gateways.values()).find(isGatewayRunning) || null;
1286
- const sourceHermesHome = resolveSourceHermesHome(process.env);
1287
- const gatewayHermesHome = resolveHermesGatewayHome(process.env, options);
1288
- const installStatus = readHermesInstallStatus(process.env, {
1289
- allowSmokeHermes: options.allowSmokeHermes === true,
1290
- repairLaunchers: options.repairLaunchers !== false,
1291
- });
1292
- const sourceConfig = summarizeHermesConfig(sourceHermesHome);
1293
- const gatewayConfig = summarizeHermesConfig(gatewayHermesHome);
1294
- const activeConfig = gatewayConfig.exists ? gatewayConfig : sourceConfig;
1295
- const provider = activeConfig.model.provider || sourceConfig.model.provider || null;
1296
- const sourceAuth = summarizeHermesAuth(sourceHermesHome, provider);
1297
- const gatewayAuth = summarizeHermesAuth(gatewayHermesHome, provider);
1298
- const activeAuth = gatewayAuth.exists ? gatewayAuth : sourceAuth;
1299
- const logs = summarizeHermesLogs([sourceHermesHome, gatewayHermesHome]);
1300
- const issues = [];
1301
-
1302
- if (!installStatus.installed) {
1303
- issues.push({
1304
- severity: 'error',
1305
- code: 'HERMES_NOT_INSTALLED',
1306
- message: installStatus.error || 'Hermes Agent CLI is not installed.',
1307
- });
1308
- }
1309
- if (!activeConfig.toolsets.includes('hermes-cli')) {
1310
- issues.push({
1311
- severity: 'error',
1312
- code: 'HERMES_CLI_TOOLSET_MISSING',
1313
- message: 'Hermes CLI toolset is not enabled; cron, file, terminal, skills, and native tools are unavailable.',
1314
- });
1315
- }
1316
- if (!activeConfig.toolsets.includes('mcp-pixcode')) {
1317
- issues.push({
1318
- severity: 'error',
1319
- code: 'PIXCODE_MCP_TOOLSET_MISSING',
1320
- message: 'Pixcode MCP toolset is not enabled in Hermes config.',
1321
- });
1322
- }
1323
- if (activeConfig.pixcodeMcp.missingTools.length > 0) {
1324
- issues.push({
1325
- severity: 'warning',
1326
- code: 'PIXCODE_MCP_TOOLS_STALE',
1327
- message: `Pixcode MCP config is missing ${activeConfig.pixcodeMcp.missingTools.length} current tool(s). Restart Hermes from Pixcode to rewrite the config.`,
1328
- tools: activeConfig.pixcodeMcp.missingTools,
1329
- });
1330
- }
1331
- if (provider === 'openai-codex' && !activeAuth.selectedProviderConfigured) {
1332
- issues.push({
1333
- severity: 'error',
1334
- code: 'OPENAI_CODEX_AUTH_MISSING',
1335
- message: 'Hermes is configured for OpenAI Codex, but Hermes auth.json does not contain an OpenAI Codex OAuth session.',
1336
- });
1337
- }
1338
- if (logs.signals.codexNoneType) {
1339
- issues.push({
1340
- severity: 'error',
1341
- code: 'OPENAI_CODEX_PROVIDER_FAILURE',
1342
- message: 'Recent Hermes logs show OpenAI Codex provider failing with "NoneType object is not iterable" before Pixcode MCP tools run.',
1343
- });
1344
- }
1345
- if (logs.signals.codexOauthMissing) {
1346
- issues.push({
1347
- severity: 'warning',
1348
- code: 'OPENAI_CODEX_OAUTH_WARNING',
1349
- message: 'Recent Hermes logs reported a missing OpenAI Codex OAuth token. Run Hermes model/auth from Settings if prompts fail.',
1350
- });
1351
- }
1352
- if (logs.signals.mcpTimeout) {
1353
- issues.push({
1354
- severity: 'warning',
1355
- code: 'PIXCODE_MCP_TIMEOUT',
1356
- message: 'Recent Hermes logs include Pixcode MCP terminal timeouts; visible CLI readback may still be waiting for provider completion.',
1357
- });
1358
- }
1359
-
1360
- const cron = {
1361
- toolsetAvailable: activeConfig.toolsets.includes('hermes-cli'),
1362
- gatewayJobsApi: null,
1363
- };
1364
- if (isGatewayRunning(gateway)) {
1365
- try {
1366
- const jobs = await callGateway(gateway, '/api/jobs', { timeoutMs: 3000 });
1367
- cron.gatewayJobsApi = {
1368
- ok: jobs.ok,
1369
- status: jobs.status,
1370
- body: jobs.body,
1371
- };
1372
- } catch (error) {
1373
- cron.gatewayJobsApi = {
1374
- ok: false,
1375
- status: 0,
1376
- error: error instanceof Error ? error.message : String(error),
1377
- };
1378
- }
1379
- }
1380
-
1381
- const recommendedActions = [];
1382
- if (issues.some((issue) => issue.code === 'HERMES_CLI_TOOLSET_MISSING' || issue.code === 'PIXCODE_MCP_TOOLS_STALE')) {
1383
- recommendedActions.push('Restart Hermes from Pixcode so configure-pixcode-mcp.mjs rewrites toolsets to hermes-cli + mcp-pixcode and registers all tools.');
1384
- }
1385
- if (issues.some((issue) => issue.code === 'OPENAI_CODEX_AUTH_MISSING' || issue.code === 'OPENAI_CODEX_PROVIDER_FAILURE')) {
1386
- recommendedActions.push('Open Settings > Hermes Agent > Model and provider, reselect OpenAI Codex or another provider, then run Test REST with a short prompt.');
1387
- }
1388
- if (!isGatewayRunning(gateway)) {
1389
- recommendedActions.push('Start REST in Settings > Hermes Agent to enable /v1 and /api/jobs gateway checks for this workspace.');
1390
- }
1391
-
1392
- return {
1393
- ok: installStatus.installed && !issues.some((issue) => issue.severity === 'error'),
1394
- generatedAt: nowIso(),
1395
- install: installStatus,
1396
- hermesHome: {
1397
- source: sourceHermesHome,
1398
- gateway: gatewayHermesHome,
1399
- },
1400
- model: {
1401
- provider,
1402
- default: activeConfig.model.default || null,
1403
- baseUrl: activeConfig.model.base_url || null,
1404
- },
1405
- config: {
1406
- source: sourceConfig,
1407
- gateway: gatewayConfig,
1408
- active: activeConfig,
1409
- activePath: activeConfig.path,
1410
- },
1411
- auth: {
1412
- source: sourceAuth,
1413
- gateway: gatewayAuth,
1414
- active: activeAuth,
1415
- },
1416
- gateway: snapshotGateway(gateway),
1417
- cron,
1418
- logs,
1419
- issues,
1420
- recommendedActions,
1421
- };
1422
- }
1423
-
1424
- export async function readHermesControlPlane(options = {}) {
1425
- const projectPath = options.projectPath ? normalizeProjectPath(options.projectPath) : null;
1426
- const sourceHermesHome = resolveSourceHermesHome(process.env);
1427
- const gatewayHermesHome = resolveHermesGatewayHome(process.env, options);
1428
- const gatewayStatus = getHermesGatewayStatus(projectPath);
1429
- const gatewaySnapshots = projectPath
1430
- ? [gatewayStatus]
1431
- : Array.isArray(gatewayStatus.gateways)
1432
- ? gatewayStatus.gateways
1433
- : [];
1434
- const install = readHermesInstallStatus(process.env, {
1435
- allowSmokeHermes: options.allowSmokeHermes === true,
1436
- repairLaunchers: options.repairLaunchers !== false,
1437
- });
1438
- const activeProfile = readActiveHermesProfile(sourceHermesHome);
1439
- const profileHomes = listHermesProfileHomes(sourceHermesHome);
1440
- const managedProfile = {
1441
- name: 'pixcode',
1442
- isDefault: false,
1443
- path: gatewayHermesHome,
1444
- };
1445
- const hasManagedProfile = profileHomes.some((profile) => path.resolve(profile.path) === path.resolve(gatewayHermesHome));
1446
- const profiles = [
1447
- ...profileHomes,
1448
- ...(hasManagedProfile ? [] : [managedProfile]),
1449
- ].map((profile) => summarizeHermesProfile(profile, activeProfile, gatewaySnapshots));
1450
- const diagnostics = await readHermesDiagnostics({
1451
- ...options,
1452
- projectPath: projectPath ?? undefined,
1453
- });
1454
- const activeProfileSummary = profiles.find((profile) => profile.isActive)
1455
- ?? profiles.find((profile) => profile.name === 'pixcode')
1456
- ?? profiles[0]
1457
- ?? null;
1458
- const managedProfileSummary = profiles.find((profile) => path.resolve(profile.path) === path.resolve(gatewayHermesHome)) ?? null;
1459
- const capabilities = [
1460
- {
1461
- id: 'rest-gateway',
1462
- label: 'Hermes REST gateway',
1463
- ready: Boolean(gatewayStatus.running),
1464
- detail: gatewayStatus.running ? 'Local /v1 and /api endpoints are reachable through Pixcode.' : 'Start REST to enable direct Hermes API control.',
1465
- },
1466
- {
1467
- id: 'pixcode-mcp',
1468
- label: 'Pixcode MCP tools',
1469
- ready: Boolean(managedProfileSummary?.tools.pixcodeMcpReady || activeProfileSummary?.tools.pixcodeMcpReady),
1470
- detail: `${managedProfileSummary?.tools.pixcodeMcpToolCount ?? activeProfileSummary?.tools.pixcodeMcpToolCount ?? 0}/${EXPECTED_PIXCODE_MCP_TOOLS.length} tool(s) registered.`,
1471
- },
1472
- {
1473
- id: 'visible-cli-control',
1474
- label: 'Visible CLI control',
1475
- ready: true,
1476
- detail: 'Hermes can request Pixcode to open and continue provider terminals in the visible workbench.',
1477
- },
1478
- {
1479
- id: 'sessions',
1480
- label: 'Session history',
1481
- ready: profiles.some((profile) => profile.sessions.exists),
1482
- detail: `${profiles.reduce((sum, profile) => sum + Number(profile.sessions.total || 0), 0)} stored Hermes session(s) detected.`,
1483
- },
1484
- {
1485
- id: 'cron',
1486
- label: 'Cron jobs',
1487
- ready: Boolean(managedProfileSummary?.cron.exists || activeProfileSummary?.cron.exists || diagnostics.cron?.toolsetAvailable),
1488
- detail: `${profiles.reduce((sum, profile) => sum + Number(profile.cron.total || 0), 0)} scheduled job(s) detected.`,
1489
- },
1490
- ];
1491
- const recommendations = new Set(diagnostics.recommendedActions || []);
1492
- if (!capabilities.find((capability) => capability.id === 'pixcode-mcp')?.ready) {
1493
- recommendations.add('Repair the Hermes control plane from Pixcode so the managed profile rewrites Pixcode MCP tools.');
1494
- }
1495
- if (!gatewayStatus.running) {
1496
- recommendations.add('Start the Hermes REST gateway for this workspace before running cron, REST, or API-driven agent tasks.');
1497
- }
1498
-
1499
- return {
1500
- ok: install.installed && capabilities.filter((capability) => capability.id !== 'visible-cli-control').every((capability) => capability.ready),
1501
- generatedAt: nowIso(),
1502
- projectPath,
1503
- homes: {
1504
- source: sourceHermesHome,
1505
- managed: gatewayHermesHome,
1506
- },
1507
- install,
1508
- gateway: gatewayStatus,
1509
- activeProfile,
1510
- profiles,
1511
- activeProfileSummary,
1512
- managedProfile: managedProfileSummary,
1513
- capabilities,
1514
- diagnostics,
1515
- recommendations: Array.from(recommendations),
1516
- };
1517
- }
1518
-
1519
- export async function repairHermesControlPlane(options = {}) {
1520
- const projectPath = options.projectPath ? normalizeProjectPath(options.projectPath) : process.cwd();
1521
- if (options.forceRestart === true) {
1522
- stopHermesGateway(projectPath);
1523
- }
1524
-
1525
- const gateway = await ensureHermesGateway({
1526
- appRoot: options.appRoot || process.cwd(),
1527
- pixcodeApiKey: options.pixcodeApiKey,
1528
- pixcodeBaseUrl: options.pixcodeBaseUrl,
1529
- projectPath,
1530
- probeExisting: true,
1531
- replaceUnhealthy: true,
1532
- repairLaunchers: options.repairLaunchers !== false,
1533
- });
1534
- const controlPlane = await readHermesControlPlane({
1535
- ...options,
1536
- projectPath,
1537
- });
1538
-
1539
- return {
1540
- ok: controlPlane.ok,
1541
- repairedAt: nowIso(),
1542
- gateway,
1543
- controlPlane,
1544
- };
1545
- }
1546
-
1547
- export function stopHermesGateway(projectPath) {
1548
- const targets = projectPath
1549
- ? [gateways.get(normalizeProjectPath(projectPath))].filter(Boolean)
1550
- : Array.from(gateways.values());
1551
- let stopped = 0;
1552
- for (const gateway of targets) {
1553
- if (!isGatewayRunning(gateway)) continue;
1554
- try {
1555
- gateway.child.kill();
1556
- stopped += 1;
1557
- } catch (error) {
1558
- gateway.error = error instanceof Error ? error.message : String(error);
1559
- }
1560
- }
1561
- return { stopped };
1562
- }
1
+ import { randomBytes } from 'node:crypto';
2
+ import fs from 'node:fs';
3
+ import net from 'node:net';
4
+ import os from 'node:os';
5
+ import path from 'node:path';
6
+
7
+ import Database from 'better-sqlite3';
8
+ import spawn from 'cross-spawn';
9
+
10
+ import {
11
+ buildHermesPathEnv,
12
+ readHermesInstallStatus,
13
+ } from './hermes-install-jobs.js';
14
+
15
+ const DEFAULT_HOST = '127.0.0.1';
16
+ const DEFAULT_PORT = 8642;
17
+ const PORT_SCAN_LIMIT = 80;
18
+ const STARTUP_TIMEOUT_MS = 30000;
19
+ const FETCH_TIMEOUT_MS = 5000;
20
+ const RUN_TIMEOUT_MS = 120000;
21
+ const RUN_POLL_INTERVAL_MS = 1000;
22
+ const LOG_LIMIT = 800;
23
+ const HERMES_DIAGNOSTIC_LOG_BYTES = 120000;
24
+ const ALLOWED_GATEWAY_REQUEST_METHODS = new Set(['GET', 'POST', 'PUT', 'PATCH', 'DELETE']);
25
+ const EXPECTED_PIXCODE_MCP_TOOLS = [
26
+ 'pixcode_list_projects',
27
+ 'pixcode_get_provider_status',
28
+ 'pixcode_open_cli_terminal',
29
+ 'pixcode_read_cli_terminal',
30
+ 'pixcode_get_hermes_gateway_status',
31
+ 'pixcode_probe_hermes_gateway',
32
+ 'pixcode_get_hermes_diagnostics',
33
+ 'pixcode_get_api_manifest',
34
+ 'pixcode_api_request',
35
+ 'pixcode_hermes_gateway_request',
36
+ 'pixcode_manage_hermes_cron',
37
+ 'pixcode_send_cli_input',
38
+ 'pixcode_get_hermes_control_plane',
39
+ 'pixcode_repair_hermes_control_plane',
40
+ ];
41
+ const PIXCODE_MANAGED_HERMES_ENV_PREFIXES = [
42
+ 'API_SERVER_',
43
+ 'BLUEBUBBLES_',
44
+ 'DINGTALK_',
45
+ 'DISCORD_',
46
+ 'EMAIL_',
47
+ 'FEISHU_',
48
+ 'MATTERMOST_',
49
+ 'MATRIX_',
50
+ 'MSGRAPH_',
51
+ 'QQ_',
52
+ 'SIGNAL_',
53
+ 'SLACK_',
54
+ 'SMS_',
55
+ 'TELEGRAM_',
56
+ 'TWILIO_',
57
+ 'WECOM_',
58
+ 'WEIXIN_',
59
+ 'WHATSAPP_',
60
+ 'YUANBAO_',
61
+ ];
62
+
63
+ const gateways = new Map();
64
+
65
+ function nowIso() {
66
+ return new Date().toISOString();
67
+ }
68
+
69
+ function normalizeProjectPath(projectPath) {
70
+ return path.resolve(projectPath || os.homedir());
71
+ }
72
+
73
+ function appendGatewayLog(gateway, stream, chunk) {
74
+ const entry = { stream, chunk: String(chunk || ''), at: Date.now() };
75
+ gateway.logs.push(entry);
76
+ if (gateway.logs.length > LOG_LIMIT) {
77
+ gateway.logs.splice(0, gateway.logs.length - LOG_LIMIT);
78
+ }
79
+ }
80
+
81
+ function isGatewayRunning(gateway) {
82
+ return Boolean(gateway?.child && gateway.exitCode === null && gateway.exitSignal === null);
83
+ }
84
+
85
+ function gatewayBaseUrl(host, port) {
86
+ return `http://${host}:${port}`;
87
+ }
88
+
89
+ function makeApiServerKey() {
90
+ return `pixcode-hermes-${randomBytes(24).toString('hex')}`;
91
+ }
92
+
93
+ function sleep(ms) {
94
+ return new Promise((resolve) => setTimeout(resolve, ms));
95
+ }
96
+
97
+ function resolveSourceHermesHome(env = process.env) {
98
+ if (env.HERMES_HOME?.trim()) {
99
+ return path.resolve(env.HERMES_HOME);
100
+ }
101
+
102
+ const defaultHome = path.join(os.homedir(), '.hermes');
103
+ try {
104
+ const activeProfile = fs.readFileSync(path.join(defaultHome, 'active_profile'), 'utf8').trim();
105
+ if (activeProfile && activeProfile !== 'default' && /^[a-z0-9][a-z0-9_-]{0,63}$/.test(activeProfile)) {
106
+ return path.join(defaultHome, 'profiles', activeProfile);
107
+ }
108
+ } catch {
109
+ // Default Hermes profile is fine when no sticky active profile exists.
110
+ }
111
+
112
+ return defaultHome;
113
+ }
114
+
115
+ function resolveHermesGatewayHome(env = process.env, options = {}) {
116
+ const configured = options.hermesHome || env.PIXCODE_HERMES_GATEWAY_HOME;
117
+ if (configured) {
118
+ return path.resolve(configured);
119
+ }
120
+
121
+ return path.join(os.homedir(), '.hermes', 'profiles', 'pixcode');
122
+ }
123
+
124
+ function copyHermesProfileFile(sourceHome, targetHome, fileName, options = {}) {
125
+ const source = path.join(sourceHome, fileName);
126
+ const target = path.join(targetHome, fileName);
127
+ if (!fs.existsSync(source)) return false;
128
+ if (!options.overwrite && fs.existsSync(target)) return false;
129
+ fs.mkdirSync(path.dirname(target), { recursive: true });
130
+ fs.copyFileSync(source, target);
131
+ return true;
132
+ }
133
+
134
+ function shouldStripManagedGatewayEnvLine(line) {
135
+ const match = String(line || '').match(/^\s*(?:export\s+)?([A-Z0-9_]+)\s*=/);
136
+ if (!match) return false;
137
+ return PIXCODE_MANAGED_HERMES_ENV_PREFIXES.some((prefix) => match[1].startsWith(prefix));
138
+ }
139
+
140
+ function copyHermesProfileEnv(sourceHome, targetHome) {
141
+ const source = path.join(sourceHome, '.env');
142
+ const target = path.join(targetHome, '.env');
143
+ if (!fs.existsSync(source)) return false;
144
+
145
+ const sourceText = fs.readFileSync(source, 'utf8');
146
+ const sanitized = sourceText
147
+ .split(/\r?\n/)
148
+ .filter((line) => !shouldStripManagedGatewayEnvLine(line))
149
+ .join('\n')
150
+ .replace(/\s*$/, '\n');
151
+ fs.mkdirSync(path.dirname(target), { recursive: true });
152
+ fs.writeFileSync(target, sanitized);
153
+ return true;
154
+ }
155
+
156
+ function seedHermesGatewayHome({ sourceHome, targetHome, gateway }) {
157
+ fs.mkdirSync(targetHome, { recursive: true });
158
+ if (path.resolve(sourceHome) === path.resolve(targetHome)) {
159
+ appendGatewayLog(gateway, 'meta', `Using Hermes gateway profile at ${targetHome}\n`);
160
+ return;
161
+ }
162
+
163
+ const copied = [];
164
+ for (const file of ['config.yaml', 'SOUL.md']) {
165
+ if (copyHermesProfileFile(sourceHome, targetHome, file, { overwrite: false })) {
166
+ copied.push(file);
167
+ }
168
+ }
169
+ if (copyHermesProfileEnv(sourceHome, targetHome)) {
170
+ copied.push('.env (without messaging platform credentials)');
171
+ }
172
+ for (const file of ['auth.json']) {
173
+ if (copyHermesProfileFile(sourceHome, targetHome, file, { overwrite: true })) {
174
+ copied.push(file);
175
+ }
176
+ }
177
+
178
+ appendGatewayLog(
179
+ gateway,
180
+ 'meta',
181
+ copied.length > 0
182
+ ? `Seeded Pixcode Hermes gateway profile from ${sourceHome}: ${copied.join(', ')}\n`
183
+ : `Using Pixcode Hermes gateway profile at ${targetHome}\n`,
184
+ );
185
+ }
186
+
187
+ export function buildHermesGatewayEnv(baseEnv = process.env, options = {}) {
188
+ const host = options.host || DEFAULT_HOST;
189
+ const port = String(options.port || DEFAULT_PORT);
190
+ return buildHermesPathEnv(baseEnv, {
191
+ API_SERVER_ENABLED: 'true',
192
+ API_SERVER_HOST: host,
193
+ API_SERVER_PORT: port,
194
+ API_SERVER_KEY: options.apiServerKey || makeApiServerKey(),
195
+ API_SERVER_CORS_ORIGINS: options.corsOrigins || options.pixcodeBaseUrl || '',
196
+ PIXCODE_BASE_URL: options.pixcodeBaseUrl || '',
197
+ PIXCODE_API_KEY: options.pixcodeApiKey || '',
198
+ PIXCODE_APP_ROOT: options.appRoot || process.cwd(),
199
+ HERMES_HOME: options.hermesHome || '',
200
+ HERMES_INSTALL_DIR: options.installDir || '',
201
+ });
202
+ }
203
+
204
+ function isPortAvailable(port, host) {
205
+ return new Promise((resolve) => {
206
+ const server = net.createServer();
207
+ server.once('error', () => resolve(false));
208
+ server.once('listening', () => {
209
+ server.close(() => resolve(true));
210
+ });
211
+ server.listen(port, host);
212
+ });
213
+ }
214
+
215
+ async function findAvailablePort(preferredPort, host) {
216
+ const start = Number.isFinite(preferredPort) ? preferredPort : DEFAULT_PORT;
217
+ for (let offset = 0; offset < PORT_SCAN_LIMIT; offset += 1) {
218
+ const port = start + offset;
219
+ if (await isPortAvailable(port, host)) {
220
+ return port;
221
+ }
222
+ }
223
+ throw new Error(`No available Hermes API server port found from ${start} to ${start + PORT_SCAN_LIMIT - 1}.`);
224
+ }
225
+
226
+ function fetchJson(url, options = {}) {
227
+ const controller = new AbortController();
228
+ const timeout = setTimeout(() => controller.abort(), options.timeoutMs || FETCH_TIMEOUT_MS);
229
+ return fetch(url, {
230
+ ...options,
231
+ signal: controller.signal,
232
+ headers: {
233
+ accept: 'application/json',
234
+ ...(options.headers || {}),
235
+ },
236
+ }).then(async (response) => {
237
+ const text = await response.text();
238
+ let body = null;
239
+ try {
240
+ body = text ? JSON.parse(text) : null;
241
+ } catch {
242
+ body = text;
243
+ }
244
+
245
+ return {
246
+ ok: response.ok,
247
+ status: response.status,
248
+ body,
249
+ };
250
+ }).finally(() => clearTimeout(timeout));
251
+ }
252
+
253
+ async function callGateway(gateway, endpoint, options = {}) {
254
+ return fetchJson(`${gateway.baseUrl}${endpoint}`, {
255
+ ...options,
256
+ headers: {
257
+ Authorization: `Bearer ${gateway.apiServerKey}`,
258
+ 'content-type': 'application/json',
259
+ ...(options.headers || {}),
260
+ },
261
+ });
262
+ }
263
+
264
+ function extractRunId(body) {
265
+ if (!body || typeof body !== 'object') return null;
266
+ return body.run_id || body.runId || body.id || body.run?.id || null;
267
+ }
268
+
269
+ function extractRunStatus(body) {
270
+ if (!body || typeof body !== 'object') return null;
271
+ return body.status || body.state || body.run?.status || body.run?.state || null;
272
+ }
273
+
274
+ function extractTextFromValue(value) {
275
+ if (typeof value === 'string') return value;
276
+ if (!value) return null;
277
+
278
+ if (Array.isArray(value)) {
279
+ return value
280
+ .map(extractTextFromValue)
281
+ .filter(Boolean)
282
+ .join('\n')
283
+ .trim() || null;
284
+ }
285
+
286
+ if (typeof value === 'object') {
287
+ for (const key of ['text', 'content', 'message', 'output', 'response', 'result', 'final']) {
288
+ const text = extractTextFromValue(value[key]);
289
+ if (text) return text;
290
+ }
291
+ }
292
+
293
+ return null;
294
+ }
295
+
296
+ function extractRunOutput(body) {
297
+ if (!body || typeof body !== 'object') return null;
298
+
299
+ for (const key of ['output_text', 'output', 'response', 'result', 'message', 'messages', 'events', 'final']) {
300
+ const text = extractTextFromValue(body[key]);
301
+ if (text) return text;
302
+ }
303
+
304
+ return null;
305
+ }
306
+
307
+ function extractResponsesOutput(body) {
308
+ if (!body || typeof body !== 'object') return null;
309
+
310
+ const output = Array.isArray(body.output) ? body.output : [];
311
+ for (const item of output) {
312
+ if (!item || typeof item !== 'object') continue;
313
+ if (item.type === 'message' || item.role === 'assistant') {
314
+ const text = extractTextFromValue(item.content);
315
+ if (text) return text;
316
+ }
317
+ const text = extractTextFromValue(item.output_text)
318
+ || extractTextFromValue(item.text)
319
+ || extractTextFromValue(item.message)
320
+ || extractTextFromValue(item.output);
321
+ if (text) return text;
322
+ }
323
+
324
+ return extractTextFromValue(body.output_text)
325
+ || extractTextFromValue(body.message)
326
+ || extractTextFromValue(body.response)
327
+ || null;
328
+ }
329
+
330
+ function extractChatCompletionOutput(body) {
331
+ if (!body || typeof body !== 'object') return null;
332
+ const choices = Array.isArray(body.choices) ? body.choices : [];
333
+ for (const choice of choices) {
334
+ const text = extractTextFromValue(choice?.message?.content)
335
+ || extractTextFromValue(choice?.delta?.content)
336
+ || extractTextFromValue(choice?.text);
337
+ if (text) return text;
338
+ }
339
+ return extractTextFromValue(body.output_text)
340
+ || extractTextFromValue(body.output)
341
+ || extractTextFromValue(body.message)
342
+ || extractTextFromValue(body.response)
343
+ || null;
344
+ }
345
+
346
+ function recentGatewayLogText(gateway) {
347
+ if (!gateway?.logs?.length) return '';
348
+ return gateway.logs
349
+ .slice(-16)
350
+ .map((entry) => String(entry.chunk || '').trim())
351
+ .filter(Boolean)
352
+ .join('\n')
353
+ .trim();
354
+ }
355
+
356
+ function readFileTail(filePath, maxBytes = HERMES_DIAGNOSTIC_LOG_BYTES) {
357
+ try {
358
+ const stat = fs.statSync(filePath);
359
+ const length = Math.min(maxBytes, stat.size);
360
+ const buffer = Buffer.alloc(length);
361
+ const fd = fs.openSync(filePath, 'r');
362
+ try {
363
+ fs.readSync(fd, buffer, 0, length, stat.size - length);
364
+ } finally {
365
+ fs.closeSync(fd);
366
+ }
367
+ return buffer.toString('utf8');
368
+ } catch {
369
+ return '';
370
+ }
371
+ }
372
+
373
+ function readJsonFileSafe(filePath) {
374
+ try {
375
+ return JSON.parse(fs.readFileSync(filePath, 'utf8'));
376
+ } catch {
377
+ return null;
378
+ }
379
+ }
380
+
381
+ function redactDiagnosticText(text) {
382
+ return String(text || '')
383
+ .replace(/\b(px_|ck_|sk-|ghp_|npm_)[A-Za-z0-9._-]+/gu, '$1[redacted]')
384
+ .replace(/\b(Bearer\s+)[A-Za-z0-9._~+/=-]+/giu, '$1[redacted]')
385
+ .replace(/((?:api[_-]?key|authorization|access[_-]?token|refresh[_-]?token|id[_-]?token|token)\s*[:=]\s*["']?)[^"',\s}]+/giu, '$1[redacted]');
386
+ }
387
+
388
+ function findRootBlockEnd(lines, startIndex) {
389
+ for (let index = startIndex + 1; index < lines.length; index += 1) {
390
+ if (/^\S[^:]*:\s*(?:#.*)?$/u.test(lines[index])) {
391
+ return index;
392
+ }
393
+ }
394
+ return lines.length;
395
+ }
396
+
397
+ function readRootList(text, key) {
398
+ const lines = String(text || '').split(/\r?\n/);
399
+ const start = lines.findIndex((line) => new RegExp(`^${key}:\\s*(?:#.*)?$`, 'u').test(line));
400
+ if (start === -1) return [];
401
+ const end = findRootBlockEnd(lines, start);
402
+ const values = [];
403
+ for (let index = start + 1; index < end; index += 1) {
404
+ const match = lines[index].match(/^\s*-\s*([^#\s][^#]*?)(?:\s+#.*)?$/u);
405
+ if (match) values.push(match[1].trim().replace(/^['"]|['"]$/gu, ''));
406
+ }
407
+ return values;
408
+ }
409
+
410
+ function readRootMap(text, key) {
411
+ const lines = String(text || '').split(/\r?\n/);
412
+ const start = lines.findIndex((line) => new RegExp(`^${key}:\\s*(?:#.*)?$`, 'u').test(line));
413
+ if (start === -1) return {};
414
+ const end = findRootBlockEnd(lines, start);
415
+ const values = {};
416
+ for (let index = start + 1; index < end; index += 1) {
417
+ const match = lines[index].match(/^\s+([A-Za-z0-9_.-]+):\s*(.*?)(?:\s+#.*)?$/u);
418
+ if (!match) continue;
419
+ values[match[1]] = match[2].trim().replace(/^['"]|['"]$/gu, '');
420
+ }
421
+ return values;
422
+ }
423
+
424
+ function readPixcodeMcpTools(text) {
425
+ return Array.from(new Set(
426
+ Array.from(String(text || '').matchAll(/^\s*-\s*(pixcode_[A-Za-z0-9_]+)\s*$/gmu))
427
+ .map((match) => match[1]),
428
+ ));
429
+ }
430
+
431
+ function readApiServerToolset(text) {
432
+ const platformText = String(text || '');
433
+ return {
434
+ hasHermesApiServer: /^\s*-\s*hermes-api-server\s*$/gmu.test(platformText),
435
+ hasPixcodePlatform: /^\s*-\s*pixcode\s*$/gmu.test(platformText),
436
+ };
437
+ }
438
+
439
+ function summarizeHermesConfig(hermesHome) {
440
+ const configPath = path.join(hermesHome, 'config.yaml');
441
+ const text = readFileTail(configPath, HERMES_DIAGNOSTIC_LOG_BYTES);
442
+ const toolsets = readRootList(text, 'toolsets');
443
+ const pixcodeTools = readPixcodeMcpTools(text);
444
+ const missingPixcodeTools = EXPECTED_PIXCODE_MCP_TOOLS.filter((tool) => !pixcodeTools.includes(tool));
445
+ return {
446
+ path: configPath,
447
+ exists: Boolean(text),
448
+ model: readRootMap(text, 'model'),
449
+ toolsets,
450
+ platformToolsets: readApiServerToolset(text),
451
+ pixcodeMcp: {
452
+ configured: /mcp_servers:[\s\S]*^\s+pixcode:\s*$/mu.test(text),
453
+ enabled: /mcp_servers:[\s\S]*^\s+pixcode:[\s\S]*^\s+enabled:\s*true\s*$/mu.test(text),
454
+ toolCount: pixcodeTools.length,
455
+ tools: pixcodeTools,
456
+ missingTools: missingPixcodeTools,
457
+ },
458
+ staleToolsetConfig: toolsets.includes('mcp-pixcode') && !toolsets.includes('hermes-cli'),
459
+ };
460
+ }
461
+
462
+ function summarizeHermesAuth(hermesHome, provider) {
463
+ const authPath = path.join(hermesHome, 'auth.json');
464
+ const auth = readJsonFileSafe(authPath);
465
+ const providers = auth && typeof auth === 'object' && auth.providers && typeof auth.providers === 'object'
466
+ ? Object.keys(auth.providers)
467
+ : [];
468
+ const pools = auth && typeof auth === 'object' && auth.credential_pool && typeof auth.credential_pool === 'object'
469
+ ? auth.credential_pool
470
+ : {};
471
+ const selectedProvider = provider || auth?.active_provider || null;
472
+ const providerEntry = selectedProvider && auth?.providers && typeof auth.providers === 'object'
473
+ ? auth.providers[selectedProvider]
474
+ : null;
475
+ return {
476
+ path: authPath,
477
+ exists: Boolean(auth),
478
+ activeProvider: auth?.active_provider || null,
479
+ providers,
480
+ selectedProvider,
481
+ selectedProviderConfigured: Boolean(providerEntry),
482
+ selectedProviderLastRefresh: providerEntry?.last_refresh || null,
483
+ selectedProviderAuthMode: providerEntry?.auth_mode || null,
484
+ selectedProviderPoolSize: selectedProvider && Array.isArray(pools?.[selectedProvider])
485
+ ? pools[selectedProvider].length
486
+ : 0,
487
+ };
488
+ }
489
+
490
+ function summarizeHermesLogs(hermesHomes) {
491
+ const files = [];
492
+ const seen = new Set();
493
+ for (const home of hermesHomes.filter(Boolean)) {
494
+ for (const name of ['errors.log', 'agent.log']) {
495
+ const filePath = path.join(home, 'logs', name);
496
+ if (seen.has(filePath)) continue;
497
+ seen.add(filePath);
498
+ const text = redactDiagnosticText(readFileTail(filePath));
499
+ if (!text) continue;
500
+ files.push({
501
+ path: filePath,
502
+ name,
503
+ recent: text.split(/\r?\n/).filter(Boolean).slice(-80),
504
+ });
505
+ }
506
+ }
507
+ const combined = files.flatMap((file) => file.recent).join('\n');
508
+ return {
509
+ files,
510
+ signals: {
511
+ codexNoneType: /NoneType' object is not iterable|NoneType object is not iterable/iu.test(combined),
512
+ codexOauthMissing: /openai-codex requested but no Codex OAuth .*found/iu.test(combined),
513
+ mcpTimeout: /MCP call timed out|pixcode_open_cli_terminal call failed/iu.test(combined),
514
+ stalePixcodeMcpToolCount: /MCP server 'pixcode'.*registered\s+[0-9]\s+tool\(s\)/iu.test(combined)
515
+ && !/registered\s+1[0-9]\s+tool\(s\)/iu.test(combined),
516
+ },
517
+ };
518
+ }
519
+
520
+ function listHermesProfileHomes(sourceHermesHome) {
521
+ const profiles = [{
522
+ name: 'default',
523
+ isDefault: true,
524
+ path: sourceHermesHome,
525
+ }];
526
+ const profilesDir = path.join(sourceHermesHome, 'profiles');
527
+ try {
528
+ for (const name of fs.readdirSync(profilesDir)) {
529
+ if (name.startsWith('.') || !/^[a-z0-9][a-z0-9_-]{0,63}$/iu.test(name)) continue;
530
+ const profilePath = path.join(profilesDir, name);
531
+ if (!fs.statSync(profilePath).isDirectory()) continue;
532
+ profiles.push({
533
+ name,
534
+ isDefault: false,
535
+ path: profilePath,
536
+ });
537
+ }
538
+ } catch {
539
+ // No named Hermes profiles yet.
540
+ }
541
+ return profiles;
542
+ }
543
+
544
+ function readActiveHermesProfile(sourceHermesHome) {
545
+ try {
546
+ const active = fs.readFileSync(path.join(sourceHermesHome, 'active_profile'), 'utf8').trim();
547
+ return active || 'default';
548
+ } catch {
549
+ return 'default';
550
+ }
551
+ }
552
+
553
+ function readHermesSessionSummary(hermesHome, limit = 5) {
554
+ const dbPath = path.join(hermesHome, 'state.db');
555
+ if (!fs.existsSync(dbPath)) {
556
+ return {
557
+ dbPath,
558
+ exists: false,
559
+ total: 0,
560
+ recent: [],
561
+ };
562
+ }
563
+
564
+ let db = null;
565
+ try {
566
+ db = new Database(dbPath, { readonly: true, fileMustExist: true });
567
+ const total = db.prepare('SELECT COUNT(*) AS count FROM sessions').get()?.count ?? 0;
568
+ const recent = db.prepare(`
569
+ SELECT id, source, started_at, ended_at, message_count, model, title
570
+ FROM sessions
571
+ ORDER BY started_at DESC
572
+ LIMIT ?
573
+ `).all(limit).map((row) => ({
574
+ id: row.id,
575
+ source: row.source || null,
576
+ startedAt: row.started_at || null,
577
+ endedAt: row.ended_at || null,
578
+ messageCount: row.message_count || 0,
579
+ model: row.model || null,
580
+ title: row.title || null,
581
+ }));
582
+ return {
583
+ dbPath,
584
+ exists: true,
585
+ total,
586
+ recent,
587
+ };
588
+ } catch (error) {
589
+ return {
590
+ dbPath,
591
+ exists: true,
592
+ total: 0,
593
+ recent: [],
594
+ error: error instanceof Error ? error.message : String(error),
595
+ };
596
+ } finally {
597
+ try {
598
+ db?.close();
599
+ } catch {
600
+ // ignore close errors
601
+ }
602
+ }
603
+ }
604
+
605
+ function normalizeCronJob(job) {
606
+ if (!job || typeof job !== 'object') return null;
607
+ const schedule = typeof job.schedule === 'object' && job.schedule
608
+ ? job.schedule.value
609
+ : job.schedule;
610
+ const enabled = job.enabled !== false;
611
+ return {
612
+ id: String(job.id || job.name || ''),
613
+ name: String(job.name || job.id || '(unnamed)'),
614
+ schedule: String(job.schedule_display || schedule || ''),
615
+ state: job.state === 'paused' || !enabled
616
+ ? 'paused'
617
+ : job.state === 'completed'
618
+ ? 'completed'
619
+ : 'active',
620
+ enabled,
621
+ nextRunAt: job.next_run_at || null,
622
+ lastRunAt: job.last_run_at || null,
623
+ lastStatus: job.last_status || null,
624
+ lastError: job.last_error || null,
625
+ };
626
+ }
627
+
628
+ function readHermesCronSummary(hermesHome, limit = 6) {
629
+ const jobsPath = path.join(hermesHome, 'cron', 'jobs.json');
630
+ if (!fs.existsSync(jobsPath)) {
631
+ return {
632
+ jobsPath,
633
+ exists: false,
634
+ total: 0,
635
+ active: 0,
636
+ paused: 0,
637
+ completed: 0,
638
+ recent: [],
639
+ };
640
+ }
641
+
642
+ try {
643
+ const parsed = JSON.parse(fs.readFileSync(jobsPath, 'utf8'));
644
+ const rawJobs = Array.isArray(parsed) ? parsed : Array.isArray(parsed.jobs) ? parsed.jobs : [];
645
+ const jobs = rawJobs.map(normalizeCronJob).filter(Boolean);
646
+ return {
647
+ jobsPath,
648
+ exists: true,
649
+ total: jobs.length,
650
+ active: jobs.filter((job) => job.state === 'active').length,
651
+ paused: jobs.filter((job) => job.state === 'paused').length,
652
+ completed: jobs.filter((job) => job.state === 'completed').length,
653
+ recent: jobs.slice(0, limit),
654
+ };
655
+ } catch (error) {
656
+ return {
657
+ jobsPath,
658
+ exists: true,
659
+ total: 0,
660
+ active: 0,
661
+ paused: 0,
662
+ completed: 0,
663
+ recent: [],
664
+ error: error instanceof Error ? error.message : String(error),
665
+ };
666
+ }
667
+ }
668
+
669
+ function summarizeHermesProfile(profile, activeProfile, gatewaySnapshots) {
670
+ const config = summarizeHermesConfig(profile.path);
671
+ const provider = config.model.provider || null;
672
+ const auth = summarizeHermesAuth(profile.path, provider);
673
+ const gateway = gatewaySnapshots.find((snapshot) => path.resolve(snapshot.hermesHome || '') === path.resolve(profile.path)) || null;
674
+ const sessions = readHermesSessionSummary(profile.path);
675
+ const cron = readHermesCronSummary(profile.path);
676
+
677
+ return {
678
+ name: profile.name,
679
+ path: profile.path,
680
+ isDefault: profile.isDefault,
681
+ isActive: profile.name === activeProfile,
682
+ model: {
683
+ provider,
684
+ default: config.model.default || null,
685
+ baseUrl: config.model.base_url || null,
686
+ },
687
+ auth: {
688
+ configured: auth.selectedProviderConfigured,
689
+ activeProvider: auth.activeProvider,
690
+ selectedProvider: auth.selectedProvider,
691
+ poolSize: auth.selectedProviderPoolSize,
692
+ lastRefresh: auth.selectedProviderLastRefresh,
693
+ },
694
+ tools: {
695
+ toolsets: config.toolsets,
696
+ pixcodeMcpConfigured: config.pixcodeMcp.configured,
697
+ pixcodeMcpEnabled: config.pixcodeMcp.enabled,
698
+ pixcodeMcpToolCount: config.pixcodeMcp.toolCount,
699
+ missingPixcodeMcpTools: config.pixcodeMcp.missingTools,
700
+ hermesCliReady: config.toolsets.includes('hermes-cli'),
701
+ pixcodeMcpReady: config.toolsets.includes('mcp-pixcode') && config.pixcodeMcp.enabled && config.pixcodeMcp.missingTools.length === 0,
702
+ },
703
+ sessions,
704
+ cron,
705
+ gateway,
706
+ };
707
+ }
708
+
709
+ function gatewayExitMessage(gateway, fallback = 'Hermes gateway is not running.') {
710
+ if (!gateway) return fallback;
711
+ const exit = gateway.exitSignal
712
+ ? `Hermes gateway exited with signal ${gateway.exitSignal}.`
713
+ : `Hermes gateway exited with code ${gateway.exitCode ?? 'unknown'}.`;
714
+ const logs = recentGatewayLogText(gateway);
715
+ return logs ? `${exit}\n${logs}` : (gateway.error || exit);
716
+ }
717
+
718
+ function normalizeGatewayEndpoint(endpoint) {
719
+ const value = typeof endpoint === 'string' ? endpoint.trim() : '';
720
+ if (!value) {
721
+ throw new Error('Hermes gateway endpoint is required.');
722
+ }
723
+ if (/^[a-z][a-z0-9+.-]*:\/\//iu.test(value) || value.startsWith('//')) {
724
+ throw new Error('Hermes gateway endpoint must be local; external URLs are not allowed.');
725
+ }
726
+ if (!value.startsWith('/')) {
727
+ throw new Error('Hermes gateway endpoint must start with /.');
728
+ }
729
+ if (
730
+ value !== '/health' &&
731
+ value !== '/health/detailed' &&
732
+ !value.startsWith('/v1/') &&
733
+ !value.startsWith('/api/')
734
+ ) {
735
+ throw new Error('Hermes gateway endpoint must be /health, /v1/..., or /api/....');
736
+ }
737
+ return value;
738
+ }
739
+
740
+ function normalizeGatewayRequestMethod(method) {
741
+ const value = String(method || 'GET').trim().toUpperCase();
742
+ if (!ALLOWED_GATEWAY_REQUEST_METHODS.has(value)) {
743
+ throw new Error(`Unsupported Hermes gateway HTTP method: ${value || '(empty)'}`);
744
+ }
745
+ return value;
746
+ }
747
+
748
+ function makeRunRequest(options) {
749
+ const input = String(options.input || '').trim();
750
+ return {
751
+ session_id: options.sessionId || `pixcode-hermes-chat-${Date.now()}-${randomBytes(4).toString('hex')}`,
752
+ input,
753
+ instructions: options.instructions || [
754
+ 'You are Hermes Agent running inside Pixcode.',
755
+ 'Use Pixcode MCP tools when they help inspect projects, launch CLIs, or perform workspace actions.',
756
+ 'Keep answers concise and include concrete next steps when work is blocked.',
757
+ ].join(' '),
758
+ };
759
+ }
760
+
761
+ function makeChatCompletionRequest(options) {
762
+ const input = String(options.input || '').trim();
763
+ const messages = Array.isArray(options.messages) ? options.messages : [
764
+ {
765
+ role: 'system',
766
+ content: options.instructions || [
767
+ 'You are Hermes Agent running inside Pixcode.',
768
+ 'Use Pixcode MCP tools when they help inspect projects, launch CLIs, or perform workspace actions.',
769
+ 'Keep answers concise and include concrete next steps when work is blocked.',
770
+ ].join(' '),
771
+ },
772
+ {
773
+ role: 'user',
774
+ content: input,
775
+ },
776
+ ];
777
+ return {
778
+ model: options.model || 'hermes-agent',
779
+ messages,
780
+ stream: false,
781
+ };
782
+ }
783
+
784
+ function makeResponsesRequest(options) {
785
+ const input = String(options.input || '').trim();
786
+ return {
787
+ model: options.model || 'hermes-agent',
788
+ input,
789
+ instructions: options.instructions || [
790
+ 'You are Hermes Agent running inside Pixcode.',
791
+ 'Use Pixcode MCP tools when they help inspect projects, launch CLIs, or perform workspace actions.',
792
+ 'Keep answers concise and include concrete next steps when work is blocked.',
793
+ ].join(' '),
794
+ conversation: options.sessionId || undefined,
795
+ store: true,
796
+ };
797
+ }
798
+
799
+ async function waitForGatewayReady(gateway) {
800
+ const started = Date.now();
801
+ let lastError = null;
802
+
803
+ while (Date.now() - started < STARTUP_TIMEOUT_MS) {
804
+ if (!isGatewayRunning(gateway)) {
805
+ throw new Error(gatewayExitMessage(gateway));
806
+ }
807
+
808
+ try {
809
+ const probe = await probeHermesGateway(gateway.projectPath, { requireRunning: true });
810
+ if (probe.ok) {
811
+ return probe;
812
+ }
813
+ lastError = probe.error || 'Gateway probe failed.';
814
+ } catch (error) {
815
+ lastError = error instanceof Error ? error.message : String(error);
816
+ }
817
+
818
+ await new Promise((resolve) => setTimeout(resolve, 500));
819
+ }
820
+
821
+ throw new Error(`Hermes gateway did not become ready within ${STARTUP_TIMEOUT_MS / 1000}s: ${lastError || 'no response'}`);
822
+ }
823
+
824
+ function runProcess(command, args, options, onData) {
825
+ return new Promise((resolve, reject) => {
826
+ const child = spawn(command, args, {
827
+ ...options,
828
+ stdio: ['ignore', 'pipe', 'pipe'],
829
+ windowsHide: true,
830
+ });
831
+ child.stdout?.on('data', (buf) => onData?.('stdout', buf.toString()));
832
+ child.stderr?.on('data', (buf) => onData?.('stderr', buf.toString()));
833
+ child.on('error', reject);
834
+ child.on('close', (code, signal) => {
835
+ if (signal) {
836
+ reject(new Error(`${command} killed by ${signal}`));
837
+ return;
838
+ }
839
+ resolve(code ?? 0);
840
+ });
841
+ });
842
+ }
843
+
844
+ async function configurePixcodeMcp({ appRoot, env, gateway }) {
845
+ const configureScript = path.join(appRoot, 'scripts', 'hermes', 'configure-pixcode-mcp.mjs');
846
+ const code = await runProcess(process.execPath, [configureScript], {
847
+ cwd: appRoot,
848
+ env,
849
+ }, (stream, chunk) => appendGatewayLog(gateway, stream, chunk));
850
+
851
+ if (code !== 0) {
852
+ throw new Error(`Pixcode MCP configuration exited with code ${code}`);
853
+ }
854
+ }
855
+
856
+ function snapshotGateway(gateway) {
857
+ if (!gateway) {
858
+ return {
859
+ running: false,
860
+ projectPath: null,
861
+ baseUrl: null,
862
+ hermesHome: null,
863
+ host: null,
864
+ port: null,
865
+ pid: null,
866
+ startedAt: null,
867
+ exitedAt: null,
868
+ exitCode: null,
869
+ exitSignal: null,
870
+ error: null,
871
+ lastProbe: null,
872
+ logs: [],
873
+ };
874
+ }
875
+
876
+ return {
877
+ running: isGatewayRunning(gateway),
878
+ projectPath: gateway.projectPath,
879
+ baseUrl: gateway.baseUrl,
880
+ hermesHome: gateway.hermesHome,
881
+ host: gateway.host,
882
+ port: gateway.port,
883
+ pid: gateway.child?.pid ?? null,
884
+ startedAt: gateway.startedAt,
885
+ exitedAt: gateway.exitedAt,
886
+ exitCode: gateway.exitCode,
887
+ exitSignal: gateway.exitSignal,
888
+ error: gateway.error,
889
+ lastProbe: gateway.lastProbe,
890
+ logs: gateway.logs.slice(-80),
891
+ };
892
+ }
893
+
894
+ export function getHermesGatewayStatus(projectPath) {
895
+ if (projectPath) {
896
+ return snapshotGateway(gateways.get(normalizeProjectPath(projectPath)));
897
+ }
898
+
899
+ const active = Array.from(gateways.values()).filter(isGatewayRunning);
900
+ return {
901
+ running: active.length > 0,
902
+ gateways: Array.from(gateways.values()).map(snapshotGateway),
903
+ };
904
+ }
905
+
906
+ export async function ensureHermesGateway(options = {}) {
907
+ const projectPath = normalizeProjectPath(options.projectPath);
908
+ const existing = gateways.get(projectPath);
909
+ if (isGatewayRunning(existing)) {
910
+ if (options.probeExisting !== true) {
911
+ return {
912
+ ...snapshotGateway(existing),
913
+ probe: existing.lastProbe,
914
+ };
915
+ }
916
+
917
+ const probe = await probeHermesGateway(projectPath, { requireRunning: true }).catch((error) => ({
918
+ ok: false,
919
+ error: error instanceof Error ? error.message : String(error),
920
+ }));
921
+ if (probe.ok || options.replaceUnhealthy !== true) {
922
+ return {
923
+ ...snapshotGateway(existing),
924
+ probe,
925
+ };
926
+ }
927
+
928
+ stopHermesGateway(projectPath);
929
+ }
930
+
931
+ const host = options.host || DEFAULT_HOST;
932
+ const port = await findAvailablePort(Number(options.port || process.env.HERMES_API_SERVER_PORT || DEFAULT_PORT), host);
933
+ const apiServerKey = options.apiServerKey || makeApiServerKey();
934
+ const appRoot = options.appRoot || process.cwd();
935
+ const sourceHermesHome = options.sourceHermesHome || resolveSourceHermesHome(process.env);
936
+ const hermesHome = resolveHermesGatewayHome(process.env, options);
937
+ const env = buildHermesGatewayEnv(process.env, {
938
+ ...options,
939
+ host,
940
+ port,
941
+ apiServerKey,
942
+ appRoot,
943
+ hermesHome,
944
+ });
945
+ const installStatus = readHermesInstallStatus(env, {
946
+ allowSmokeHermes: options.allowSmokeHermes === true,
947
+ repairLaunchers: options.repairLaunchers !== false,
948
+ });
949
+ if (!installStatus.installed || !installStatus.command) {
950
+ throw new Error(installStatus.error || 'Hermes Agent CLI is not installed.');
951
+ }
952
+
953
+ const gateway = {
954
+ id: `${projectPath}:${port}`,
955
+ projectPath,
956
+ host,
957
+ port,
958
+ baseUrl: gatewayBaseUrl(host, port),
959
+ hermesHome,
960
+ apiServerKey,
961
+ command: installStatus.command,
962
+ child: null,
963
+ startedAt: nowIso(),
964
+ exitedAt: null,
965
+ exitCode: null,
966
+ exitSignal: null,
967
+ error: null,
968
+ lastProbe: null,
969
+ logs: [],
970
+ };
971
+ gateways.set(projectPath, gateway);
972
+
973
+ seedHermesGatewayHome({ sourceHome: sourceHermesHome, targetHome: hermesHome, gateway });
974
+ await configurePixcodeMcp({ appRoot, env, gateway });
975
+
976
+ const gatewayArgs = options.gatewayArgs || ['gateway', 'run', '--replace'];
977
+ const child = spawn(installStatus.command, gatewayArgs, {
978
+ cwd: projectPath,
979
+ env,
980
+ stdio: ['ignore', 'pipe', 'pipe'],
981
+ windowsHide: true,
982
+ });
983
+ gateway.child = child;
984
+ appendGatewayLog(gateway, 'meta', `$ ${installStatus.command} ${gatewayArgs.join(' ')}\n`);
985
+
986
+ child.stdout?.on('data', (buf) => appendGatewayLog(gateway, 'stdout', buf.toString()));
987
+ child.stderr?.on('data', (buf) => appendGatewayLog(gateway, 'stderr', buf.toString()));
988
+ child.on('error', (error) => {
989
+ gateway.error = error instanceof Error ? error.message : String(error);
990
+ appendGatewayLog(gateway, 'stderr', `${gateway.error}\n`);
991
+ });
992
+ child.on('exit', (code, signal) => {
993
+ gateway.exitCode = code;
994
+ gateway.exitSignal = signal;
995
+ gateway.exitedAt = nowIso();
996
+ appendGatewayLog(gateway, 'meta', `Hermes gateway exited with code ${code}${signal ? ` (${signal})` : ''}\n`);
997
+ });
998
+
999
+ const probe = await waitForGatewayReady(gateway);
1000
+ return {
1001
+ ...snapshotGateway(gateway),
1002
+ probe,
1003
+ };
1004
+ }
1005
+
1006
+ export async function probeHermesGateway(projectPath, options = {}) {
1007
+ const gateway = projectPath
1008
+ ? gateways.get(normalizeProjectPath(projectPath))
1009
+ : Array.from(gateways.values()).find(isGatewayRunning);
1010
+
1011
+ if (!isGatewayRunning(gateway)) {
1012
+ const result = {
1013
+ ok: false,
1014
+ error: 'Hermes gateway is not running.',
1015
+ projectPath: projectPath ? normalizeProjectPath(projectPath) : null,
1016
+ baseUrl: null,
1017
+ checks: {},
1018
+ };
1019
+ if (options.requireRunning) return result;
1020
+ return result;
1021
+ }
1022
+
1023
+ const checks = {};
1024
+ try {
1025
+ checks.health = await fetchJson(`${gateway.baseUrl}/health`);
1026
+ } catch (error) {
1027
+ checks.health = { ok: false, status: 0, error: error instanceof Error ? error.message : String(error) };
1028
+ }
1029
+
1030
+ try {
1031
+ checks.capabilities = await callGateway(gateway, '/v1/capabilities');
1032
+ } catch (error) {
1033
+ checks.capabilities = { ok: false, status: 0, error: error instanceof Error ? error.message : String(error) };
1034
+ }
1035
+
1036
+ try {
1037
+ checks.models = await callGateway(gateway, '/v1/models');
1038
+ } catch (error) {
1039
+ checks.models = { ok: false, status: 0, error: error instanceof Error ? error.message : String(error) };
1040
+ }
1041
+
1042
+ if (typeof options.input === 'string' && options.input.trim()) {
1043
+ try {
1044
+ const run = await runHermesGatewayPrompt(gateway.projectPath, {
1045
+ input: options.input.trim(),
1046
+ sessionId: options.sessionId || `pixcode-probe-${Date.now()}`,
1047
+ instructions: options.instructions || 'Respond briefly for a Pixcode REST integration check.',
1048
+ timeoutMs: options.runTimeoutMs || 30000,
1049
+ });
1050
+ checks.run = {
1051
+ ok: run.ok,
1052
+ status: run.httpStatus || 200,
1053
+ body: run,
1054
+ error: run.error || null,
1055
+ };
1056
+ } catch (error) {
1057
+ checks.run = { ok: false, status: 0, error: error instanceof Error ? error.message : String(error) };
1058
+ }
1059
+ }
1060
+
1061
+ const ok = Boolean(
1062
+ checks.health?.ok &&
1063
+ checks.capabilities?.ok &&
1064
+ checks.models?.ok &&
1065
+ (!checks.run || checks.run.ok),
1066
+ );
1067
+ const result = {
1068
+ ok,
1069
+ projectPath: gateway.projectPath,
1070
+ baseUrl: gateway.baseUrl,
1071
+ checkedAt: nowIso(),
1072
+ checks,
1073
+ error: ok ? null : 'One or more Hermes REST checks failed.',
1074
+ };
1075
+ gateway.lastProbe = result;
1076
+ return result;
1077
+ }
1078
+
1079
+ export async function runHermesGatewayPrompt(projectPath, options = {}) {
1080
+ const gateway = projectPath
1081
+ ? gateways.get(normalizeProjectPath(projectPath))
1082
+ : Array.from(gateways.values()).find(isGatewayRunning);
1083
+
1084
+ if (!isGatewayRunning(gateway)) {
1085
+ throw new Error('Hermes gateway is not running.');
1086
+ }
1087
+
1088
+ const input = String(options.input || '').trim();
1089
+ if (!input) {
1090
+ throw new Error('Hermes prompt is required.');
1091
+ }
1092
+
1093
+ const responsesRequest = makeResponsesRequest({ ...options, input });
1094
+ const responseRun = await callGateway(gateway, '/v1/responses', {
1095
+ method: 'POST',
1096
+ body: JSON.stringify(responsesRequest),
1097
+ timeoutMs: options.responsesTimeoutMs || options.timeoutMs || RUN_TIMEOUT_MS,
1098
+ }).catch((error) => {
1099
+ if (!isGatewayRunning(gateway)) {
1100
+ throw new Error(gatewayExitMessage(gateway));
1101
+ }
1102
+ throw error;
1103
+ });
1104
+
1105
+ if (!isGatewayRunning(gateway)) {
1106
+ throw new Error(gatewayExitMessage(gateway));
1107
+ }
1108
+
1109
+ if (responseRun.ok) {
1110
+ const status = extractRunStatus(responseRun.body) || 'completed';
1111
+ const message = extractResponsesOutput(responseRun.body);
1112
+ return {
1113
+ ok: status === 'completed' || status === 'succeeded',
1114
+ projectPath: gateway.projectPath,
1115
+ baseUrl: gateway.baseUrl,
1116
+ sessionId: options.sessionId || responsesRequest.conversation || null,
1117
+ runId: null,
1118
+ responseId: responseRun.body?.id || null,
1119
+ status,
1120
+ message,
1121
+ error: (status === 'completed' || status === 'succeeded') ? null : extractTextFromValue(responseRun.body?.error) || message || 'Hermes response failed.',
1122
+ raw: responseRun.body,
1123
+ transport: 'responses',
1124
+ endpoint: '/v1/responses',
1125
+ httpStatus: responseRun.status,
1126
+ };
1127
+ }
1128
+
1129
+ if (responseRun.status && responseRun.status !== 404 && responseRun.status !== 405) {
1130
+ throw new Error(`Hermes /v1/responses failed with HTTP ${responseRun.status}: ${JSON.stringify(responseRun.body)}`);
1131
+ }
1132
+
1133
+ const chatRequest = makeChatCompletionRequest({ ...options, input });
1134
+ const chat = await callGateway(gateway, '/v1/chat/completions', {
1135
+ method: 'POST',
1136
+ body: JSON.stringify(chatRequest),
1137
+ timeoutMs: options.chatTimeoutMs || options.timeoutMs || RUN_TIMEOUT_MS,
1138
+ }).catch((error) => {
1139
+ if (!isGatewayRunning(gateway)) {
1140
+ throw new Error(gatewayExitMessage(gateway));
1141
+ }
1142
+ throw error;
1143
+ });
1144
+
1145
+ if (!isGatewayRunning(gateway)) {
1146
+ throw new Error(gatewayExitMessage(gateway));
1147
+ }
1148
+
1149
+ if (chat.ok) {
1150
+ const message = extractChatCompletionOutput(chat.body);
1151
+ return {
1152
+ ok: true,
1153
+ projectPath: gateway.projectPath,
1154
+ baseUrl: gateway.baseUrl,
1155
+ sessionId: options.sessionId || null,
1156
+ runId: null,
1157
+ status: 'completed',
1158
+ message,
1159
+ raw: chat.body,
1160
+ transport: 'chat.completions',
1161
+ endpoint: '/v1/chat/completions',
1162
+ httpStatus: chat.status,
1163
+ };
1164
+ }
1165
+
1166
+ if (chat.status && chat.status !== 404 && chat.status !== 405) {
1167
+ throw new Error(`Hermes /v1/chat/completions failed with HTTP ${chat.status}: ${JSON.stringify(chat.body)}`);
1168
+ }
1169
+
1170
+ const request = makeRunRequest({ ...options, input });
1171
+ const create = await callGateway(gateway, '/v1/runs', {
1172
+ method: 'POST',
1173
+ body: JSON.stringify(request),
1174
+ timeoutMs: options.createTimeoutMs || 15000,
1175
+ }).catch((error) => {
1176
+ if (!isGatewayRunning(gateway)) {
1177
+ throw new Error(gatewayExitMessage(gateway));
1178
+ }
1179
+ throw error;
1180
+ });
1181
+
1182
+ if (!isGatewayRunning(gateway)) {
1183
+ throw new Error(gatewayExitMessage(gateway));
1184
+ }
1185
+
1186
+ if (!create.ok) {
1187
+ throw new Error(`Hermes /v1/runs failed with HTTP ${create.status}: ${JSON.stringify(create.body)}`);
1188
+ }
1189
+
1190
+ const runId = extractRunId(create.body);
1191
+ const initialStatus = extractRunStatus(create.body);
1192
+ if (!runId) {
1193
+ return {
1194
+ ok: true,
1195
+ projectPath: gateway.projectPath,
1196
+ baseUrl: gateway.baseUrl,
1197
+ sessionId: request.session_id,
1198
+ runId: null,
1199
+ status: initialStatus || 'completed',
1200
+ message: extractRunOutput(create.body),
1201
+ raw: create.body,
1202
+ transport: 'runs',
1203
+ endpoint: '/v1/runs',
1204
+ httpStatus: create.status,
1205
+ };
1206
+ }
1207
+
1208
+ const terminalStatuses = new Set(['completed', 'failed', 'cancelled', 'canceled']);
1209
+ const started = Date.now();
1210
+ let latest = create.body;
1211
+ let status = initialStatus || 'queued';
1212
+
1213
+ while (!terminalStatuses.has(String(status)) && Date.now() - started < (options.timeoutMs || RUN_TIMEOUT_MS)) {
1214
+ await sleep(options.pollIntervalMs || RUN_POLL_INTERVAL_MS);
1215
+ const poll = await callGateway(gateway, `/v1/runs/${encodeURIComponent(runId)}`, {
1216
+ timeoutMs: options.pollTimeoutMs || 15000,
1217
+ });
1218
+ if (!poll.ok) {
1219
+ throw new Error(`Hermes /v1/runs/${runId} failed with HTTP ${poll.status}: ${JSON.stringify(poll.body)}`);
1220
+ }
1221
+ if (!isGatewayRunning(gateway)) {
1222
+ throw new Error(gatewayExitMessage(gateway));
1223
+ }
1224
+ latest = poll.body;
1225
+ status = extractRunStatus(latest) || status;
1226
+ }
1227
+
1228
+ if (!terminalStatuses.has(String(status))) {
1229
+ throw new Error(`Hermes run did not finish within ${Math.round((options.timeoutMs || RUN_TIMEOUT_MS) / 1000)}s: ${runId}`);
1230
+ }
1231
+
1232
+ const message = extractRunOutput(latest);
1233
+ return {
1234
+ ok: status === 'completed',
1235
+ projectPath: gateway.projectPath,
1236
+ baseUrl: gateway.baseUrl,
1237
+ sessionId: request.session_id,
1238
+ runId,
1239
+ status,
1240
+ message,
1241
+ error: status === 'completed' ? null : extractTextFromValue(latest?.error) || message || 'Hermes run failed.',
1242
+ raw: latest,
1243
+ transport: 'runs',
1244
+ endpoint: '/v1/runs',
1245
+ httpStatus: create.status,
1246
+ };
1247
+ }
1248
+
1249
+ export async function requestHermesGateway(projectPath, options = {}) {
1250
+ const gateway = projectPath
1251
+ ? gateways.get(normalizeProjectPath(projectPath))
1252
+ : Array.from(gateways.values()).find(isGatewayRunning);
1253
+
1254
+ if (!isGatewayRunning(gateway)) {
1255
+ throw new Error('Hermes gateway is not running.');
1256
+ }
1257
+
1258
+ const endpoint = normalizeGatewayEndpoint(options.endpoint || options.path);
1259
+ const method = normalizeGatewayRequestMethod(options.method);
1260
+ const requestOptions = {
1261
+ method,
1262
+ timeoutMs: options.timeoutMs || FETCH_TIMEOUT_MS,
1263
+ };
1264
+ if (typeof options.body !== 'undefined' && options.body !== null && method !== 'GET') {
1265
+ requestOptions.body = JSON.stringify(options.body);
1266
+ }
1267
+
1268
+ const response = await callGateway(gateway, endpoint, requestOptions);
1269
+ return {
1270
+ ok: response.ok,
1271
+ status: response.status,
1272
+ projectPath: gateway.projectPath,
1273
+ baseUrl: gateway.baseUrl,
1274
+ endpoint,
1275
+ method,
1276
+ body: response.body,
1277
+ error: response.ok ? null : `Hermes gateway ${method} ${endpoint} failed with HTTP ${response.status}.`,
1278
+ };
1279
+ }
1280
+
1281
+ export async function readHermesDiagnostics(options = {}) {
1282
+ const projectPath = options.projectPath ? normalizeProjectPath(options.projectPath) : null;
1283
+ const gateway = projectPath
1284
+ ? gateways.get(projectPath)
1285
+ : Array.from(gateways.values()).find(isGatewayRunning) || null;
1286
+ const sourceHermesHome = resolveSourceHermesHome(process.env);
1287
+ const gatewayHermesHome = resolveHermesGatewayHome(process.env, options);
1288
+ const installStatus = readHermesInstallStatus(process.env, {
1289
+ allowSmokeHermes: options.allowSmokeHermes === true,
1290
+ repairLaunchers: options.repairLaunchers !== false,
1291
+ });
1292
+ const sourceConfig = summarizeHermesConfig(sourceHermesHome);
1293
+ const gatewayConfig = summarizeHermesConfig(gatewayHermesHome);
1294
+ const activeConfig = gatewayConfig.exists ? gatewayConfig : sourceConfig;
1295
+ const provider = activeConfig.model.provider || sourceConfig.model.provider || null;
1296
+ const sourceAuth = summarizeHermesAuth(sourceHermesHome, provider);
1297
+ const gatewayAuth = summarizeHermesAuth(gatewayHermesHome, provider);
1298
+ const activeAuth = gatewayAuth.exists ? gatewayAuth : sourceAuth;
1299
+ const logs = summarizeHermesLogs([sourceHermesHome, gatewayHermesHome]);
1300
+ const issues = [];
1301
+
1302
+ if (!installStatus.installed) {
1303
+ issues.push({
1304
+ severity: 'error',
1305
+ code: 'HERMES_NOT_INSTALLED',
1306
+ message: installStatus.error || 'Hermes Agent CLI is not installed.',
1307
+ });
1308
+ }
1309
+ if (!activeConfig.toolsets.includes('hermes-cli')) {
1310
+ issues.push({
1311
+ severity: 'error',
1312
+ code: 'HERMES_CLI_TOOLSET_MISSING',
1313
+ message: 'Hermes CLI toolset is not enabled; cron, file, terminal, skills, and native tools are unavailable.',
1314
+ });
1315
+ }
1316
+ if (!activeConfig.toolsets.includes('mcp-pixcode')) {
1317
+ issues.push({
1318
+ severity: 'error',
1319
+ code: 'PIXCODE_MCP_TOOLSET_MISSING',
1320
+ message: 'Pixcode MCP toolset is not enabled in Hermes config.',
1321
+ });
1322
+ }
1323
+ if (activeConfig.pixcodeMcp.missingTools.length > 0) {
1324
+ issues.push({
1325
+ severity: 'warning',
1326
+ code: 'PIXCODE_MCP_TOOLS_STALE',
1327
+ message: `Pixcode MCP config is missing ${activeConfig.pixcodeMcp.missingTools.length} current tool(s). Restart Hermes from Pixcode to rewrite the config.`,
1328
+ tools: activeConfig.pixcodeMcp.missingTools,
1329
+ });
1330
+ }
1331
+ if (provider === 'openai-codex' && !activeAuth.selectedProviderConfigured) {
1332
+ issues.push({
1333
+ severity: 'error',
1334
+ code: 'OPENAI_CODEX_AUTH_MISSING',
1335
+ message: 'Hermes is configured for OpenAI Codex, but Hermes auth.json does not contain an OpenAI Codex OAuth session.',
1336
+ });
1337
+ }
1338
+ if (logs.signals.codexNoneType) {
1339
+ issues.push({
1340
+ severity: 'error',
1341
+ code: 'OPENAI_CODEX_PROVIDER_FAILURE',
1342
+ message: 'Recent Hermes logs show OpenAI Codex provider failing with "NoneType object is not iterable" before Pixcode MCP tools run.',
1343
+ });
1344
+ }
1345
+ if (logs.signals.codexOauthMissing) {
1346
+ issues.push({
1347
+ severity: 'warning',
1348
+ code: 'OPENAI_CODEX_OAUTH_WARNING',
1349
+ message: 'Recent Hermes logs reported a missing OpenAI Codex OAuth token. Run Hermes model/auth from Settings if prompts fail.',
1350
+ });
1351
+ }
1352
+ if (logs.signals.mcpTimeout) {
1353
+ issues.push({
1354
+ severity: 'warning',
1355
+ code: 'PIXCODE_MCP_TIMEOUT',
1356
+ message: 'Recent Hermes logs include Pixcode MCP terminal timeouts; visible CLI readback may still be waiting for provider completion.',
1357
+ });
1358
+ }
1359
+
1360
+ const cron = {
1361
+ toolsetAvailable: activeConfig.toolsets.includes('hermes-cli'),
1362
+ gatewayJobsApi: null,
1363
+ };
1364
+ if (isGatewayRunning(gateway)) {
1365
+ try {
1366
+ const jobs = await callGateway(gateway, '/api/jobs', { timeoutMs: 3000 });
1367
+ cron.gatewayJobsApi = {
1368
+ ok: jobs.ok,
1369
+ status: jobs.status,
1370
+ body: jobs.body,
1371
+ };
1372
+ } catch (error) {
1373
+ cron.gatewayJobsApi = {
1374
+ ok: false,
1375
+ status: 0,
1376
+ error: error instanceof Error ? error.message : String(error),
1377
+ };
1378
+ }
1379
+ }
1380
+
1381
+ const recommendedActions = [];
1382
+ if (issues.some((issue) => issue.code === 'HERMES_CLI_TOOLSET_MISSING' || issue.code === 'PIXCODE_MCP_TOOLS_STALE')) {
1383
+ recommendedActions.push('Restart Hermes from Pixcode so configure-pixcode-mcp.mjs rewrites toolsets to hermes-cli + mcp-pixcode and registers all tools.');
1384
+ }
1385
+ if (issues.some((issue) => issue.code === 'OPENAI_CODEX_AUTH_MISSING' || issue.code === 'OPENAI_CODEX_PROVIDER_FAILURE')) {
1386
+ recommendedActions.push('Open Settings > Hermes Agent > Model and provider, reselect OpenAI Codex or another provider, then run Test REST with a short prompt.');
1387
+ }
1388
+ if (!isGatewayRunning(gateway)) {
1389
+ recommendedActions.push('Start REST in Settings > Hermes Agent to enable /v1 and /api/jobs gateway checks for this workspace.');
1390
+ }
1391
+
1392
+ return {
1393
+ ok: installStatus.installed && !issues.some((issue) => issue.severity === 'error'),
1394
+ generatedAt: nowIso(),
1395
+ install: installStatus,
1396
+ hermesHome: {
1397
+ source: sourceHermesHome,
1398
+ gateway: gatewayHermesHome,
1399
+ },
1400
+ model: {
1401
+ provider,
1402
+ default: activeConfig.model.default || null,
1403
+ baseUrl: activeConfig.model.base_url || null,
1404
+ },
1405
+ config: {
1406
+ source: sourceConfig,
1407
+ gateway: gatewayConfig,
1408
+ active: activeConfig,
1409
+ activePath: activeConfig.path,
1410
+ },
1411
+ auth: {
1412
+ source: sourceAuth,
1413
+ gateway: gatewayAuth,
1414
+ active: activeAuth,
1415
+ },
1416
+ gateway: snapshotGateway(gateway),
1417
+ cron,
1418
+ logs,
1419
+ issues,
1420
+ recommendedActions,
1421
+ };
1422
+ }
1423
+
1424
+ export async function readHermesControlPlane(options = {}) {
1425
+ const projectPath = options.projectPath ? normalizeProjectPath(options.projectPath) : null;
1426
+ const sourceHermesHome = resolveSourceHermesHome(process.env);
1427
+ const gatewayHermesHome = resolveHermesGatewayHome(process.env, options);
1428
+ const gatewayStatus = getHermesGatewayStatus(projectPath);
1429
+ const gatewaySnapshots = projectPath
1430
+ ? [gatewayStatus]
1431
+ : Array.isArray(gatewayStatus.gateways)
1432
+ ? gatewayStatus.gateways
1433
+ : [];
1434
+ const install = readHermesInstallStatus(process.env, {
1435
+ allowSmokeHermes: options.allowSmokeHermes === true,
1436
+ repairLaunchers: options.repairLaunchers !== false,
1437
+ });
1438
+ const activeProfile = readActiveHermesProfile(sourceHermesHome);
1439
+ const profileHomes = listHermesProfileHomes(sourceHermesHome);
1440
+ const managedProfile = {
1441
+ name: 'pixcode',
1442
+ isDefault: false,
1443
+ path: gatewayHermesHome,
1444
+ };
1445
+ const hasManagedProfile = profileHomes.some((profile) => path.resolve(profile.path) === path.resolve(gatewayHermesHome));
1446
+ const profiles = [
1447
+ ...profileHomes,
1448
+ ...(hasManagedProfile ? [] : [managedProfile]),
1449
+ ].map((profile) => summarizeHermesProfile(profile, activeProfile, gatewaySnapshots));
1450
+ const diagnostics = await readHermesDiagnostics({
1451
+ ...options,
1452
+ projectPath: projectPath ?? undefined,
1453
+ });
1454
+ const activeProfileSummary = profiles.find((profile) => profile.isActive)
1455
+ ?? profiles.find((profile) => profile.name === 'pixcode')
1456
+ ?? profiles[0]
1457
+ ?? null;
1458
+ const managedProfileSummary = profiles.find((profile) => path.resolve(profile.path) === path.resolve(gatewayHermesHome)) ?? null;
1459
+ const capabilities = [
1460
+ {
1461
+ id: 'rest-gateway',
1462
+ label: 'Hermes REST gateway',
1463
+ ready: Boolean(gatewayStatus.running),
1464
+ detail: gatewayStatus.running ? 'Local /v1 and /api endpoints are reachable through Pixcode.' : 'Start REST to enable direct Hermes API control.',
1465
+ },
1466
+ {
1467
+ id: 'pixcode-mcp',
1468
+ label: 'Pixcode MCP tools',
1469
+ ready: Boolean(managedProfileSummary?.tools.pixcodeMcpReady || activeProfileSummary?.tools.pixcodeMcpReady),
1470
+ detail: `${managedProfileSummary?.tools.pixcodeMcpToolCount ?? activeProfileSummary?.tools.pixcodeMcpToolCount ?? 0}/${EXPECTED_PIXCODE_MCP_TOOLS.length} tool(s) registered.`,
1471
+ },
1472
+ {
1473
+ id: 'visible-cli-control',
1474
+ label: 'Visible CLI control',
1475
+ ready: true,
1476
+ detail: 'Hermes can request Pixcode to open and continue provider terminals in the visible workbench.',
1477
+ },
1478
+ {
1479
+ id: 'sessions',
1480
+ label: 'Session history',
1481
+ ready: profiles.some((profile) => profile.sessions.exists),
1482
+ detail: `${profiles.reduce((sum, profile) => sum + Number(profile.sessions.total || 0), 0)} stored Hermes session(s) detected.`,
1483
+ },
1484
+ {
1485
+ id: 'cron',
1486
+ label: 'Cron jobs',
1487
+ ready: Boolean(managedProfileSummary?.cron.exists || activeProfileSummary?.cron.exists || diagnostics.cron?.toolsetAvailable),
1488
+ detail: `${profiles.reduce((sum, profile) => sum + Number(profile.cron.total || 0), 0)} scheduled job(s) detected.`,
1489
+ },
1490
+ ];
1491
+ const recommendations = new Set(diagnostics.recommendedActions || []);
1492
+ if (!capabilities.find((capability) => capability.id === 'pixcode-mcp')?.ready) {
1493
+ recommendations.add('Repair the Hermes control plane from Pixcode so the managed profile rewrites Pixcode MCP tools.');
1494
+ }
1495
+ if (!gatewayStatus.running) {
1496
+ recommendations.add('Start the Hermes REST gateway for this workspace before running cron, REST, or API-driven agent tasks.');
1497
+ }
1498
+
1499
+ return {
1500
+ ok: install.installed && capabilities.filter((capability) => capability.id !== 'visible-cli-control').every((capability) => capability.ready),
1501
+ generatedAt: nowIso(),
1502
+ projectPath,
1503
+ homes: {
1504
+ source: sourceHermesHome,
1505
+ managed: gatewayHermesHome,
1506
+ },
1507
+ install,
1508
+ gateway: gatewayStatus,
1509
+ activeProfile,
1510
+ profiles,
1511
+ activeProfileSummary,
1512
+ managedProfile: managedProfileSummary,
1513
+ capabilities,
1514
+ diagnostics,
1515
+ recommendations: Array.from(recommendations),
1516
+ };
1517
+ }
1518
+
1519
+ export async function repairHermesControlPlane(options = {}) {
1520
+ const projectPath = options.projectPath ? normalizeProjectPath(options.projectPath) : process.cwd();
1521
+ if (options.forceRestart === true) {
1522
+ stopHermesGateway(projectPath);
1523
+ }
1524
+
1525
+ const gateway = await ensureHermesGateway({
1526
+ appRoot: options.appRoot || process.cwd(),
1527
+ pixcodeApiKey: options.pixcodeApiKey,
1528
+ pixcodeBaseUrl: options.pixcodeBaseUrl,
1529
+ projectPath,
1530
+ probeExisting: true,
1531
+ replaceUnhealthy: true,
1532
+ repairLaunchers: options.repairLaunchers !== false,
1533
+ });
1534
+ const controlPlane = await readHermesControlPlane({
1535
+ ...options,
1536
+ projectPath,
1537
+ });
1538
+
1539
+ return {
1540
+ ok: controlPlane.ok,
1541
+ repairedAt: nowIso(),
1542
+ gateway,
1543
+ controlPlane,
1544
+ };
1545
+ }
1546
+
1547
+ export function stopHermesGateway(projectPath) {
1548
+ const targets = projectPath
1549
+ ? [gateways.get(normalizeProjectPath(projectPath))].filter(Boolean)
1550
+ : Array.from(gateways.values());
1551
+ let stopped = 0;
1552
+ for (const gateway of targets) {
1553
+ if (!isGatewayRunning(gateway)) continue;
1554
+ try {
1555
+ gateway.child.kill();
1556
+ stopped += 1;
1557
+ } catch (error) {
1558
+ gateway.error = error instanceof Error ? error.message : String(error);
1559
+ }
1560
+ }
1561
+ return { stopped };
1562
+ }