moi-computer 0.8.1-next.0 → 0.9.1-next.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (103) hide show
  1. package/client/assets/hermes.png +0 -0
  2. package/client/components/shared/IconGhost.tsx +15 -18
  3. package/client/components/ui/button.tsx +2 -2
  4. package/client/components/ui/hover-card.tsx +1 -1
  5. package/client/components/ui/popover.tsx +3 -1
  6. package/client/features/chat/ChatEmptyState.tsx +25 -7
  7. package/client/features/chat/ChatPanel.tsx +70 -28
  8. package/client/features/chat/ChatPopup.tsx +3 -2
  9. package/client/features/chat/ChatPromptBubbles.tsx +1 -1
  10. package/client/features/chat/MarkdownContent.tsx +1 -1
  11. package/client/features/chat/attachment-staging.ts +153 -0
  12. package/client/features/chat/chat-send.ts +55 -0
  13. package/client/features/chat/chat-store.ts +32 -8
  14. package/client/features/chat/composer/ChatComposer.tsx +190 -77
  15. package/client/features/chat/{ModelPicker.tsx → composer/ModelPicker.tsx} +29 -25
  16. package/client/features/chat/composer/banners/ComposerBanner.tsx +1 -1
  17. package/client/features/chat/composer/banners/WorkspaceAgentAvailabilityBanner.tsx +4 -13
  18. package/client/features/chat/{model-order.ts → composer/model-order.ts} +16 -0
  19. package/client/features/chat/composer/useWorkspaceComposerState.tsx +9 -4
  20. package/client/features/chat/tool-group/format/claude.ts +48 -0
  21. package/client/features/chat/tool-group/format/codex.ts +126 -0
  22. package/client/features/chat/tool-group/format/hermes.ts +68 -0
  23. package/client/features/chat/tool-group/format/index.ts +34 -0
  24. package/client/features/chat/tool-group/format/mcp.ts +64 -0
  25. package/client/features/chat/tool-group/format/openclaw.ts +116 -0
  26. package/client/features/chat/tool-group/format/shared.ts +49 -0
  27. package/client/features/chat/useChat.ts +5 -10
  28. package/client/features/dev/ChatStatesPage.tsx +40 -2
  29. package/client/features/drawings/DrawingLayer.tsx +49 -0
  30. package/client/features/drawings/DrawingToolbar.tsx +95 -0
  31. package/client/features/drawings/annotation-availability.ts +10 -0
  32. package/client/features/drawings/capture-element.ts +45 -0
  33. package/client/features/drawings/useChatAnnotation.ts +222 -0
  34. package/client/features/drawings/useDrawingLayer.ts +758 -0
  35. package/client/features/drawings/useViewBuilderSketch.ts +151 -0
  36. package/client/features/home/workspace-presentation.tsx +6 -2
  37. package/client/features/home/workspace-setup/WorkspaceAgentStep.tsx +12 -6
  38. package/client/features/scratchpad/ScratchpadControls.tsx +19 -5
  39. package/client/features/views/ViewBuilder.tsx +224 -0
  40. package/client/features/views/api.ts +1 -0
  41. package/client/features/views/useViewBuilderActions.ts +24 -3
  42. package/client/features/views/useViewBuilderDrafts.ts +66 -0
  43. package/client/features/widgets/Widgets.tsx +23 -39
  44. package/client/features/workspace/CustomizePanel.tsx +1 -1
  45. package/client/features/workspace/WorkspaceScreen.tsx +157 -57
  46. package/client/features/workspace/WorkspaceTabs.tsx +3 -3
  47. package/client/features/workspace/useFitsSplitLayout.ts +14 -8
  48. package/client/index.css +8 -0
  49. package/client/store/ui.ts +19 -0
  50. package/client/theme.css +33 -8
  51. package/dist/chunk-3becmq75.js +81 -0
  52. package/dist/{chunk-kkj86722.js → chunk-6w6j9g4b.js} +78 -78
  53. package/dist/{chunk-xeyxk7ep.js → chunk-8ew39cvq.js} +4 -4
  54. package/dist/chunk-c8gtrjwp.js +1 -0
  55. package/dist/{chunk-2vaafjp4.js → chunk-pp62fnxh.js} +1 -1
  56. package/dist/{chunk-35fq49qa.js → chunk-sxc24vfp.js} +1 -1
  57. package/dist/{chunk-xzx7c9hq.js → chunk-v964b5g7.js} +1 -1
  58. package/dist/chunk-zx33ts9d.css +1 -0
  59. package/dist/hermes-pznd63x9.png +0 -0
  60. package/dist/index.html +1 -1
  61. package/lib/format.ts +21 -7
  62. package/lib/themes.ts +8 -8
  63. package/lib/types.ts +8 -1
  64. package/lib/workspace-types.ts +2 -1
  65. package/package.json +6 -4
  66. package/server/api.ts +21 -2
  67. package/server/bundler/build-applet.ts +35 -44
  68. package/server/cli-env.ts +2 -2
  69. package/server/cli.ts +227 -56
  70. package/server/constants.ts +8 -0
  71. package/server/control-client.ts +124 -0
  72. package/server/control.ts +2 -2
  73. package/server/harness/README.md +74 -23
  74. package/server/harness/acp/adapter.ts +220 -0
  75. package/server/harness/acp/archived.ts +88 -0
  76. package/server/harness/acp/client.ts +328 -0
  77. package/server/harness/acp/discovery.ts +130 -0
  78. package/server/harness/acp/run-durations.ts +68 -0
  79. package/server/harness/acp/session.ts +788 -0
  80. package/server/harness/acp/wire.ts +132 -0
  81. package/server/harness/executable.ts +15 -1
  82. package/server/harness/hermes/NOTES.md +815 -0
  83. package/server/harness/hermes/discovery.ts +155 -0
  84. package/server/harness/hermes/index.ts +155 -0
  85. package/server/harness/hermes/models.ts +79 -0
  86. package/server/harness/openclaw/NOTES.md +196 -94
  87. package/server/harness/openclaw/adapter.ts +28 -55
  88. package/server/harness/openclaw/compat.ts +4 -8
  89. package/server/harness/openclaw/session.ts +257 -238
  90. package/server/harness/openclaw/strip.ts +10 -34
  91. package/server/harness/openclaw/thinking.ts +5 -53
  92. package/server/harness/registry.ts +3 -1
  93. package/server/service.ts +2 -2
  94. package/server/view-builders.ts +4 -2
  95. package/server/workspace-import.ts +30 -1
  96. package/workspace/.claude/skills/moi-workspace/SKILL.md +6 -6
  97. package/client/features/chat/tool-group/format.ts +0 -359
  98. package/client/features/views/ViewBuilderTab.tsx +0 -152
  99. package/dist/chunk-gny990qx.css +0 -1
  100. package/dist/chunk-gt7n06ef.js +0 -1
  101. package/dist/chunk-n7pga5x3.js +0 -80
  102. /package/workspace/.claude/skills/moi-workspace/{DESIGN.md → references/DESIGN.md} +0 -0
  103. /package/workspace/.claude/skills/moi-workspace/{SCRATCHPAD.md → references/SCRATCHPAD.md} +0 -0
package/server/cli.ts CHANGED
@@ -39,8 +39,14 @@ import {
39
39
  renderEnvView,
40
40
  resolveCwdWorkspace
41
41
  } from './cli-env'
42
- import { columns } from './cli-ui'
43
- import { CONTROL_PORT, PORT } from './constants'
42
+ import { columns, keyValue } from './cli-ui'
43
+ import { CONTROL_HOST, CONTROL_PORT, CONTROL_URL, PORT } from './constants'
44
+ import { type ControlProbe, controlFailureMessage, probeControlServer } from './control-client'
45
+ import {
46
+ type HermesProfile,
47
+ discoverHermesProfiles,
48
+ matchHermesProfile
49
+ } from './harness/hermes/discovery'
44
50
  import { type OpenClawAgent, discoverOpenClawAgents } from './harness/openclaw/discovery'
45
51
  import { liftToWorkspaceRoot, registerWorkspace } from './registry'
46
52
  import { serverCwd } from './server-cwd'
@@ -83,22 +89,15 @@ import { provisionWorkspace } from './workspace-init'
83
89
  // ---- helpers ----------------------------------------------------------------
84
90
 
85
91
  async function isServerRunning(): Promise<boolean> {
86
- return new Promise(resolve => {
87
- const ws = new WebSocket(`ws://localhost:${CONTROL_PORT}`)
88
- const timer = setTimeout(() => {
89
- ws.close()
90
- resolve(false)
91
- }, 600)
92
- ws.onopen = () => {
93
- clearTimeout(timer)
94
- ws.close()
95
- resolve(true)
96
- }
97
- ws.onerror = () => {
98
- clearTimeout(timer)
99
- resolve(false)
100
- }
101
- })
92
+ return (await probeControlServer()).status === 'running'
93
+ }
94
+
95
+ // Print why the control socket could not be opened, then exit. Classifies the
96
+ // failure first, so "no server is listening" and "the server is up but the
97
+ // address is unreachable" no longer read as the same problem.
98
+ async function exitControlUnreachable(): Promise<never> {
99
+ console.error(controlFailureMessage(await probeControlServer()))
100
+ process.exit(1)
102
101
  }
103
102
 
104
103
  async function waitForServer(timeoutMs = 10_000): Promise<boolean> {
@@ -112,7 +111,7 @@ async function waitForServer(timeoutMs = 10_000): Promise<boolean> {
112
111
 
113
112
  async function registerViaControl(absPath: string): Promise<string> {
114
113
  return new Promise((res, rej) => {
115
- const ws = new WebSocket(`ws://localhost:${CONTROL_PORT}`)
114
+ const ws = new WebSocket(CONTROL_URL)
116
115
  ws.onopen = () => ws.send(JSON.stringify({ type: 'workspace:register', path: absPath }))
117
116
  ws.onmessage = event => {
118
117
  const data = JSON.parse(String(event.data))
@@ -121,7 +120,7 @@ async function registerViaControl(absPath: string): Promise<string> {
121
120
  res(data.id)
122
121
  }
123
122
  }
124
- ws.onerror = () => rej(new Error('Could not connect to control server'))
123
+ ws.onerror = () => void probeControlServer().then(p => rej(new Error(controlFailureMessage(p))))
125
124
  })
126
125
  }
127
126
 
@@ -486,7 +485,7 @@ const bundle = defineCommand({
486
485
  // Computed locally up front so it can ride along in the success output; the
487
486
  // agent reads this and knows to run `moi skill update`.
488
487
  const notice = await staleSkillNotice(path)
489
- const ws = new WebSocket(`ws://localhost:${CONTROL_PORT}`)
488
+ const ws = new WebSocket(CONTROL_URL)
490
489
 
491
490
  ws.onopen = () =>
492
491
  ws.send(
@@ -567,10 +566,7 @@ const bundle = defineCommand({
567
566
  process.exit(failed.length > 0 ? 1 : 0)
568
567
  }
569
568
 
570
- ws.onerror = () => {
571
- console.error('Could not connect to control server. Is the main process running?')
572
- process.exit(1)
573
- }
569
+ ws.onerror = () => void exitControlUnreachable()
574
570
  }
575
571
  })
576
572
 
@@ -594,7 +590,7 @@ const builderSet = defineCommand({
594
590
  },
595
591
  run({ args }) {
596
592
  const path = resolve(args.dir)
597
- const ws = new WebSocket(`ws://localhost:${CONTROL_PORT}`)
593
+ const ws = new WebSocket(CONTROL_URL)
598
594
  ws.onopen = () =>
599
595
  ws.send(
600
596
  JSON.stringify({
@@ -625,10 +621,7 @@ const builderSet = defineCommand({
625
621
  ws.close()
626
622
  process.exit(0)
627
623
  }
628
- ws.onerror = () => {
629
- console.error('Could not connect to control server. Is the main process running?')
630
- process.exit(1)
631
- }
624
+ ws.onerror = () => void exitControlUnreachable()
632
625
  }
633
626
  })
634
627
 
@@ -659,7 +652,7 @@ const refresh = defineCommand({
659
652
  process.exit(1)
660
653
  }
661
654
  const notice = await staleSkillNotice(process.cwd())
662
- const ws = new WebSocket(`ws://localhost:${CONTROL_PORT}`)
655
+ const ws = new WebSocket(CONTROL_URL)
663
656
 
664
657
  ws.onopen = () => ws.send(JSON.stringify({ type: 'applets:refresh', only: args.only }))
665
658
 
@@ -678,10 +671,7 @@ const refresh = defineCommand({
678
671
  process.exit(0)
679
672
  }
680
673
 
681
- ws.onerror = () => {
682
- console.error('Could not connect to control server. Is the main process running?')
683
- process.exit(1)
684
- }
674
+ ws.onerror = () => void exitControlUnreachable()
685
675
  }
686
676
  })
687
677
 
@@ -720,7 +710,7 @@ const theme = defineCommand({
720
710
  async run({ args }) {
721
711
  const path = resolve(args.dir)
722
712
  const notice = await staleSkillNotice(path)
723
- const ws = new WebSocket(`ws://localhost:${CONTROL_PORT}`)
713
+ const ws = new WebSocket(CONTROL_URL)
724
714
 
725
715
  ws.onopen = () =>
726
716
  ws.send(
@@ -834,10 +824,7 @@ const theme = defineCommand({
834
824
  process.exit(0)
835
825
  }
836
826
 
837
- ws.onerror = () => {
838
- console.error('Could not connect to control server. Is the main process running?')
839
- process.exit(1)
840
- }
827
+ ws.onerror = () => void exitControlUnreachable()
841
828
  }
842
829
  })
843
830
 
@@ -858,15 +845,24 @@ function versionMismatchNotice(serverVersion: string): string | null {
858
845
  const status = defineCommand({
859
846
  meta: { name: 'status', description: 'Show server status and registered workspaces' },
860
847
  async run() {
861
- const running = await isServerRunning()
848
+ const probe: ControlProbe = await probeControlServer()
862
849
  const notice = await staleSkillNotice(process.cwd())
863
850
 
864
- if (!running) {
851
+ if (probe.status === 'not-running') {
865
852
  console.log('\n' + pc.dim('○') + ' Server is ' + pc.bold('not running'))
866
853
  console.log(pc.dim(` cli version ${VERSION}`) + '\n')
867
854
  process.exit(0)
868
855
  }
869
856
 
857
+ // The dial failed for a reason other than an empty port, so the server may
858
+ // be up and healthy. Name the real obstacle instead of blaming the process.
859
+ if (probe.status === 'unreachable') {
860
+ console.log('\n' + pc.yellow('◆') + ' Server is ' + pc.bold('unreachable'))
861
+ console.log(pc.dim(` control port ${CONTROL_HOST}:${CONTROL_PORT} — ${probe.reason}`))
862
+ console.log(pc.dim(` cli version ${VERSION}`) + '\n')
863
+ process.exit(1)
864
+ }
865
+
870
866
  const info = await queryServerInfo()
871
867
  console.log(
872
868
  '\n' +
@@ -890,7 +886,7 @@ const status = defineCommand({
890
886
  if (mismatch) console.log('\n' + mismatch)
891
887
 
892
888
  await new Promise<void>((resolve, reject) => {
893
- const ws = new WebSocket(`ws://localhost:${CONTROL_PORT}`)
889
+ const ws = new WebSocket(CONTROL_URL)
894
890
 
895
891
  ws.onopen = () => ws.send(JSON.stringify({ type: 'workspace:list' }))
896
892
 
@@ -904,7 +900,8 @@ const status = defineCommand({
904
900
  resolve()
905
901
  }
906
902
 
907
- ws.onerror = () => reject(new Error('Could not connect to control server'))
903
+ ws.onerror = () =>
904
+ void probeControlServer().then(p => reject(new Error(controlFailureMessage(p))))
908
905
  })
909
906
 
910
907
  if (notice) console.log(pc.yellow(notice) + '\n')
@@ -1070,6 +1067,31 @@ const env = defineCommand({
1070
1067
  }
1071
1068
  })
1072
1069
 
1070
+ // ---- provider commands (openclaw / hermes) ----------------------------------
1071
+
1072
+ // `moi <provider>` on its own lists the commands AND what they can be run
1073
+ // against, so discovering the installable agents never requires `init`.
1074
+ function printProviderCommands(provider: string, argHint: string) {
1075
+ console.log('\n' + pc.bold(' Commands'))
1076
+ console.log(
1077
+ keyValue(
1078
+ [
1079
+ [
1080
+ pc.bold(`moi ${provider} init`) + ' ' + pc.dim(argHint),
1081
+ 'Install moi skills and register the workspace'
1082
+ ]
1083
+ ],
1084
+ ' '
1085
+ )
1086
+ )
1087
+ }
1088
+
1089
+ // The argument is optional when there is exactly one candidate, so the hint
1090
+ // should not demand one that isn't needed.
1091
+ function initHint(provider: string, count: number, label: string): string {
1092
+ return count === 1 ? `moi ${provider} init` : `moi ${provider} init <${label}>`
1093
+ }
1094
+
1073
1095
  // ---- openclaw subcommands ---------------------------------------------------
1074
1096
 
1075
1097
  // Match an `agent` argument against `agentId` (exact) or `name`
@@ -1122,7 +1144,9 @@ const openclawInit = defineCommand({
1122
1144
  process.exit(1)
1123
1145
  }
1124
1146
 
1125
- if (!args.agent) {
1147
+ // No argument: with a single agent there is nothing to choose, so install
1148
+ // into it. Only an ambiguous choice needs the list.
1149
+ if (!args.agent && agents.length > 1) {
1126
1150
  console.log('\n' + pc.bold('OpenClaw agents'))
1127
1151
  console.log(
1128
1152
  pc.dim(' Run ' + pc.bold('moi openclaw init <agentId>') + ' to install skills.\n')
@@ -1132,7 +1156,7 @@ const openclawInit = defineCommand({
1132
1156
  process.exit(0)
1133
1157
  }
1134
1158
 
1135
- const target = findAgent(agents, args.agent)
1159
+ const target = args.agent ? findAgent(agents, args.agent) : agents[0]
1136
1160
  if (!target) {
1137
1161
  console.error('\n' + pc.red('✗') + ' Agent not found: ' + pc.bold(args.agent) + '\n')
1138
1162
  console.log(' Available:\n')
@@ -1196,9 +1220,161 @@ const openclawInit = defineCommand({
1196
1220
 
1197
1221
  const openclaw = defineCommand({
1198
1222
  meta: { name: 'openclaw', description: 'OpenClaw integration commands' },
1223
+ async run({ rawArgs }) {
1224
+ // citty runs the parent even after dispatching a subcommand — the bare
1225
+ // form is the only one this handles.
1226
+ if (rawArgs.some(arg => !arg.startsWith('-'))) return
1227
+ console.log('\n' + pc.bold('OpenClaw'))
1228
+ printProviderCommands('openclaw', '[agent]')
1229
+ const agents = await discoverOpenClawAgents().catch(() => [])
1230
+ if (agents.length === 0) {
1231
+ console.log(
1232
+ '\n ' +
1233
+ pc.dim('No agents discovered — start the OpenClaw gateway and check ') +
1234
+ pc.dim('~/.openclaw/openclaw.json') +
1235
+ '\n'
1236
+ )
1237
+ return
1238
+ }
1239
+ console.log('\n' + pc.bold(' Agents'))
1240
+ printAgentTable(agents)
1241
+ console.log(pc.dim('\n Run ') + pc.bold(initHint('openclaw', agents.length, 'agentId')) + '\n')
1242
+ },
1199
1243
  subCommands: { init: openclawInit }
1200
1244
  })
1201
1245
 
1246
+ // ---- hermes subcommands -----------------------------------------------------
1247
+
1248
+ function printProfileTable(profiles: HermesProfile[]) {
1249
+ console.log(
1250
+ columns(
1251
+ ['', 'profile', 'model', 'workspace'].map(h => pc.dim(h)),
1252
+ profiles.map(p => [
1253
+ p.isDefault ? pc.green('●') : ' ',
1254
+ p.isDefault ? pc.bold(p.agentId) : p.agentId,
1255
+ p.model ?? pc.dim('—'),
1256
+ pc.dim(p.path)
1257
+ ])
1258
+ )
1259
+ )
1260
+ }
1261
+
1262
+ const hermesInit = defineCommand({
1263
+ meta: {
1264
+ name: 'init',
1265
+ description:
1266
+ 'Install moi skills into a Hermes profile workspace. Run without args to list discovered profiles.'
1267
+ },
1268
+ args: {
1269
+ profile: {
1270
+ type: 'positional',
1271
+ required: false,
1272
+ description: 'Profile id or description (omit to list profiles)'
1273
+ }
1274
+ },
1275
+ async run({ args }) {
1276
+ const profiles = await discoverHermesProfiles()
1277
+ if (profiles.length === 0) {
1278
+ console.error(
1279
+ '\n' +
1280
+ pc.red('✗') +
1281
+ ' No Hermes profiles discovered.\n' +
1282
+ pc.dim(' Install Hermes and run ' + pc.bold('hermes setup') + ' first.\n')
1283
+ )
1284
+ process.exit(1)
1285
+ }
1286
+
1287
+ // No argument: with a single profile there is nothing to choose, so
1288
+ // install into it. Only an ambiguous choice needs the list.
1289
+ if (!args.profile && profiles.length > 1) {
1290
+ console.log('\n' + pc.bold('Hermes profiles'))
1291
+ console.log(pc.dim(' Run ' + pc.bold('moi hermes init <profile>') + ' to install skills.\n'))
1292
+ printProfileTable(profiles)
1293
+ console.log()
1294
+ process.exit(0)
1295
+ }
1296
+
1297
+ const target = args.profile ? matchHermesProfile(profiles, args.profile) : profiles[0]
1298
+ if (!target) {
1299
+ console.error('\n' + pc.red('✗') + ' Profile not found: ' + pc.bold(args.profile) + '\n')
1300
+ console.log(' Available:\n')
1301
+ printProfileTable(profiles)
1302
+ console.log()
1303
+ process.exit(1)
1304
+ }
1305
+
1306
+ // Shared provisioning path with `moi init`: skills land in
1307
+ // <profile>/workspace/skills/<name>/ (Hermes resolves the workspace skills
1308
+ // dir with the highest precedence, so these win over bundled ones), plus
1309
+ // the `.moi/` bootstrap. The default profile has no workspace directory
1310
+ // until now — provisionWorkspace creates it.
1311
+ const { scaffold, skillsDir: skillsRoot } = await provisionWorkspace(target.path, 'hermes')
1312
+ if (scaffold !== 'exists') {
1313
+ if (scaffold === 'installing') {
1314
+ console.log('\n' + pc.dim(' Widget dependencies still installing in .moi/ (background)'))
1315
+ } else if (scaffold === 0) {
1316
+ console.log('\n' + pc.dim(' Installed widget dependencies in .moi/'))
1317
+ } else {
1318
+ console.warn('\n' + pc.yellow(' bun install failed — run it manually in .moi/'))
1319
+ }
1320
+ }
1321
+
1322
+ const entry = await registerWorkspace(target.path, {
1323
+ type: 'hermes',
1324
+ name: target.name,
1325
+ agentId: target.agentId,
1326
+ isDefault: target.isDefault
1327
+ })
1328
+
1329
+ console.log('\n' + pc.green('✓') + ' Installed skills to ' + pc.bold(skillsRoot))
1330
+ console.log(
1331
+ pc.dim(' Profile: ') +
1332
+ pc.bold(target.agentId) +
1333
+ (target.model ? pc.dim(' (' + target.model + ')') : '')
1334
+ )
1335
+ const isInteractive = process.stdout.isTTY
1336
+ const running = await isServerRunning()
1337
+ if (running) {
1338
+ const url = `http://localhost:${PORT}/workspace/${entry.id}`
1339
+ await registerViaControl(target.path)
1340
+ if (isInteractive) {
1341
+ console.log(' Opening ' + pc.bold(url) + '\n')
1342
+ await openBrowser(url)
1343
+ } else {
1344
+ console.log(' Ready at ' + pc.bold(url) + '\n')
1345
+ }
1346
+ } else {
1347
+ console.log(' Run ' + pc.bold('moi start') + ' to open in the browser\n')
1348
+ }
1349
+ process.exit(0)
1350
+ }
1351
+ })
1352
+
1353
+ const hermes = defineCommand({
1354
+ meta: { name: 'hermes', description: 'Hermes Agent integration commands' },
1355
+ async run({ rawArgs }) {
1356
+ // citty runs the parent even after dispatching a subcommand — the bare
1357
+ // form is the only one this handles.
1358
+ if (rawArgs.some(arg => !arg.startsWith('-'))) return
1359
+ console.log('\n' + pc.bold('Hermes'))
1360
+ printProviderCommands('hermes', '[profile]')
1361
+ const profiles = await discoverHermesProfiles().catch(() => [])
1362
+ if (profiles.length === 0) {
1363
+ console.log(
1364
+ '\n ' +
1365
+ pc.dim('No profiles discovered — install Hermes and run ') +
1366
+ pc.dim('hermes setup') +
1367
+ '\n'
1368
+ )
1369
+ return
1370
+ }
1371
+ console.log('\n' + pc.bold(' Profiles'))
1372
+ printProfileTable(profiles)
1373
+ console.log(pc.dim('\n Run ') + pc.bold(initHint('hermes', profiles.length, 'profile')) + '\n')
1374
+ },
1375
+ subCommands: { init: hermesInit }
1376
+ })
1377
+
1202
1378
  async function sendConfig(payload: {
1203
1379
  path: string
1204
1380
  name?: string
@@ -1207,7 +1383,7 @@ async function sendConfig(payload: {
1207
1383
  clearIcon?: boolean
1208
1384
  }) {
1209
1385
  const notice = await staleSkillNotice(payload.path)
1210
- const ws = new WebSocket(`ws://localhost:${CONTROL_PORT}`)
1386
+ const ws = new WebSocket(CONTROL_URL)
1211
1387
  ws.onopen = () => ws.send(JSON.stringify({ type: 'config', ...payload }))
1212
1388
  ws.onmessage = event => {
1213
1389
  const res = JSON.parse(String(event.data))
@@ -1240,10 +1416,7 @@ async function sendConfig(payload: {
1240
1416
  ws.close()
1241
1417
  process.exit(0)
1242
1418
  }
1243
- ws.onerror = () => {
1244
- console.error('Could not connect to control server. Is the main process running?')
1245
- process.exit(1)
1246
- }
1419
+ ws.onerror = () => void exitControlUnreachable()
1247
1420
  }
1248
1421
 
1249
1422
  // A terse cheat sheet — one line per command — so an agent can grasp the
@@ -1482,7 +1655,7 @@ function sendControl(
1482
1655
  payload: Record<string, unknown>,
1483
1656
  onResult: (res: Record<string, unknown>) => void | Promise<void>
1484
1657
  ) {
1485
- const ws = new WebSocket(`ws://localhost:${CONTROL_PORT}`)
1658
+ const ws = new WebSocket(CONTROL_URL)
1486
1659
  ws.onopen = () => ws.send(JSON.stringify(payload))
1487
1660
  ws.onmessage = async event => {
1488
1661
  const res = JSON.parse(String(event.data))
@@ -1499,10 +1672,7 @@ function sendControl(
1499
1672
  ws.close()
1500
1673
  process.exit(0)
1501
1674
  }
1502
- ws.onerror = () => {
1503
- console.error('Could not connect to control server. Is the main process running?')
1504
- process.exit(1)
1505
- }
1675
+ ws.onerror = () => void exitControlUnreachable()
1506
1676
  }
1507
1677
 
1508
1678
  function sendScratch(
@@ -2655,6 +2825,7 @@ const systemCommands = {
2655
2825
  service,
2656
2826
  update,
2657
2827
  openclaw,
2828
+ hermes,
2658
2829
  version
2659
2830
  }
2660
2831
 
@@ -1,3 +1,11 @@
1
1
  export const PORT = Number(process.env.PORT) || 13337
2
2
  // M=13, E=5, I=9 -> 13059
3
3
  export const CONTROL_PORT = 13059
4
+ // The address the control server binds AND the one every client dials — never
5
+ // the name `localhost`. Resolution of that name is not guaranteed: container
6
+ // images ship an /etc/hosts without a `localhost` entry (Bun's resolver, unlike
7
+ // glibc, does not special-case it), and where it does resolve it may prefer
8
+ // ::1 while the control server listens on IPv4 only. Bind and dial read this
9
+ // same constant so the two can never drift apart.
10
+ export const CONTROL_HOST = '127.0.0.1'
11
+ export const CONTROL_URL = `ws://${CONTROL_HOST}:${CONTROL_PORT}`
@@ -0,0 +1,124 @@
1
+ import { connect } from 'node:net'
2
+
3
+ import { CONTROL_HOST, CONTROL_PORT, CONTROL_URL } from './constants'
4
+
5
+ // Why the CLI probes the control port instead of just asking "did the socket
6
+ // open?": a failed dial has more than one cause, and they need different
7
+ // answers. Nothing listening means the server is not running (`moi start`).
8
+ // Anything else — the name or address not resolving, the host unreachable, a
9
+ // stranger squatting on the port — means the server may well be running and
10
+ // fine, and the fix is not to restart it. Bun's WebSocket collapses all of
11
+ // these into a single "Failed to connect" error, so the classification is
12
+ // recovered with a second probe at the TCP layer, where the real errno
13
+ // survives (`node:net` reports it; `Bun.connect` reports ECONNREFUSED for
14
+ // everything, so it cannot be used here).
15
+
16
+ export type ControlProbe =
17
+ // A control socket opened — the server is up.
18
+ | { status: 'running' }
19
+ // Connection refused: the port is free, so no server is listening.
20
+ | { status: 'not-running' }
21
+ // Anything else. `reason` names the actual failure for the user.
22
+ | { status: 'unreachable'; reason: string }
23
+
24
+ export type ControlProbeOptions = {
25
+ host?: string
26
+ port?: number
27
+ timeoutMs?: number
28
+ }
29
+
30
+ function closeQuietly(ws: WebSocket): void {
31
+ try {
32
+ ws.close()
33
+ } catch {}
34
+ }
35
+
36
+ // Re-dial the same address at the TCP layer to recover the errno the WebSocket
37
+ // error swallowed. Reaching this means the WebSocket already failed, so a
38
+ // successful TCP connect is itself a finding: something holds the control port
39
+ // but does not speak our protocol.
40
+ function classifyDialFailure(host: string, port: number, timeoutMs: number): Promise<ControlProbe> {
41
+ return new Promise(resolve => {
42
+ let settled = false
43
+ const settle = (probe: ControlProbe) => {
44
+ if (settled) return
45
+ settled = true
46
+ resolve(probe)
47
+ }
48
+ const socket = connect({ host, port })
49
+ socket.setTimeout(timeoutMs)
50
+ socket.on('connect', () => {
51
+ socket.destroy()
52
+ settle({
53
+ status: 'unreachable',
54
+ reason:
55
+ 'the port accepted a connection but refused the control WebSocket — another process may hold it'
56
+ })
57
+ })
58
+ socket.on('timeout', () => {
59
+ socket.destroy()
60
+ settle({ status: 'unreachable', reason: `no answer within ${timeoutMs}ms` })
61
+ })
62
+ socket.on('error', (err: NodeJS.ErrnoException) => {
63
+ socket.destroy()
64
+ if (err.code === 'ECONNREFUSED') {
65
+ settle({ status: 'not-running' })
66
+ return
67
+ }
68
+ if (err.code === 'ENOTFOUND' || err.code === 'EAI_AGAIN') {
69
+ settle({ status: 'unreachable', reason: `could not resolve "${host}" (${err.code})` })
70
+ return
71
+ }
72
+ settle({
73
+ status: 'unreachable',
74
+ reason: err.code ?? err.message
75
+ })
76
+ })
77
+ })
78
+ }
79
+
80
+ // Open a control socket and report what happened. The happy path is one
81
+ // WebSocket connect; the extra TCP probe only runs when that fails.
82
+ export function probeControlServer(options: ControlProbeOptions = {}): Promise<ControlProbe> {
83
+ const { host = CONTROL_HOST, port = CONTROL_PORT, timeoutMs = 1500 } = options
84
+ const url = host === CONTROL_HOST && port === CONTROL_PORT ? CONTROL_URL : `ws://${host}:${port}`
85
+ return new Promise(resolve => {
86
+ let settled = false
87
+ const settle = (probe: ControlProbe) => {
88
+ if (settled) return
89
+ settled = true
90
+ resolve(probe)
91
+ }
92
+ let ws: WebSocket
93
+ try {
94
+ ws = new WebSocket(url)
95
+ } catch (err) {
96
+ settle({ status: 'unreachable', reason: err instanceof Error ? err.message : String(err) })
97
+ return
98
+ }
99
+ const timer = setTimeout(() => {
100
+ closeQuietly(ws)
101
+ // A hung dial is not evidence of an absent server — say so rather than
102
+ // reporting it as "not running" the way a bare timeout used to.
103
+ void classifyDialFailure(host, port, timeoutMs).then(settle)
104
+ }, timeoutMs)
105
+ ws.onopen = () => {
106
+ clearTimeout(timer)
107
+ closeQuietly(ws)
108
+ settle({ status: 'running' })
109
+ }
110
+ ws.onerror = () => {
111
+ clearTimeout(timer)
112
+ void classifyDialFailure(host, port, timeoutMs).then(settle)
113
+ }
114
+ })
115
+ }
116
+
117
+ // The one-line explanation the CLI prints when a command could not reach the
118
+ // control server. `running` never reaches here.
119
+ export function controlFailureMessage(probe: ControlProbe): string {
120
+ if (probe.status === 'unreachable') {
121
+ return `Could not reach the control server at ${CONTROL_HOST}:${CONTROL_PORT} — ${probe.reason}. The server may still be running.`
122
+ }
123
+ return 'Could not connect to control server. Is the main process running?'
124
+ }
package/server/control.ts CHANGED
@@ -7,7 +7,7 @@ import { isParamsRecord } from '@/lib/workspace-tabs'
7
7
 
8
8
  import { clearAppletLog, getAppletLog, getAppletLogCount } from './applet-log'
9
9
  import { serializeWorkspaceBundle } from './bundle-queue'
10
- import { CONTROL_PORT, PORT } from './constants'
10
+ import { CONTROL_HOST, CONTROL_PORT, PORT } from './constants'
11
11
  import { applyEnvChanged } from './env-apply'
12
12
  import { callFunctionEphemeral, parseFunctionPath } from './functions'
13
13
  import { processIcon } from './icon'
@@ -63,7 +63,7 @@ async function resolveWorkspace(
63
63
 
64
64
  export const control = Bun.serve({
65
65
  port: CONTROL_PORT,
66
- hostname: '127.0.0.1',
66
+ hostname: CONTROL_HOST,
67
67
  fetch(req, server) {
68
68
  return server.upgrade(req)
69
69
  ? new Response(null, { status: 101 })