@pixelbyte-software/pixcode 1.51.1 → 1.51.3

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