moi-computer 0.9.1-next.2 → 0.9.1-next.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.
package/README.md CHANGED
@@ -77,7 +77,10 @@ Then bring in your project, either way works:
77
77
 
78
78
  - open [http://localhost:13337](http://localhost:13337) and create a workspace
79
79
  (or import one moi found on this computer) right from the web UI;
80
- - or run `moi init` inside a folder to turn it into a workspace.
80
+ - or run `moi init` inside a folder to turn it into a workspace. It picks the
81
+ agent backend up from the folder itself — a Hermes profile, an OpenClaw
82
+ agent, or Codex/Claude Code history — and asks for
83
+ `--harness=claude-code|codex|hermes|openclaw` when it can't tell.
81
84
 
82
85
  ## Run as a service
83
86
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "moi-computer",
3
- "version": "0.9.1-next.2",
3
+ "version": "0.9.1-next.3",
4
4
  "description": "Local AI workspace powered by the Claude Agent SDK, Bun, React, and Tailwind. Requires Bun.",
5
5
  "license": "Elastic-2.0",
6
6
  "type": "module",
package/server/cli.ts CHANGED
@@ -30,7 +30,8 @@ import type {
30
30
  ScratchSize,
31
31
  ScratchStyle,
32
32
  WorkspaceEntry,
33
- WorkspaceSkillStatus
33
+ WorkspaceSkillStatus,
34
+ WorkspaceType
34
35
  } from '@/lib/types'
35
36
 
36
37
  import {
@@ -43,13 +44,21 @@ import {
43
44
  import { columns, keyValue } from './cli-ui'
44
45
  import { CONTROL_HOST, CONTROL_PORT, CONTROL_URL, PORT } from './constants'
45
46
  import { type ControlProbe, controlFailureMessage, probeControlServer } from './control-client'
47
+ import {
48
+ HARNESS_TYPES,
49
+ type DetectedAgent,
50
+ agentBindingFor,
51
+ detectHarness,
52
+ harnessLabel,
53
+ isHarnessName
54
+ } from './harness/detect'
46
55
  import {
47
56
  type HermesProfile,
48
57
  discoverHermesProfiles,
49
58
  matchHermesProfile
50
59
  } from './harness/hermes/discovery'
51
60
  import { type OpenClawAgent, discoverOpenClawAgents } from './harness/openclaw/discovery'
52
- import { liftToWorkspaceRoot, registerWorkspace } from './registry'
61
+ import { liftToWorkspaceRoot, listWorkspaces, registerWorkspace } from './registry'
53
62
  import { serverCwd } from './server-cwd'
54
63
  import {
55
64
  isBehind,
@@ -207,6 +216,95 @@ async function runDevSupervisor(
207
216
 
208
217
  // ---- commands ---------------------------------------------------------------
209
218
 
219
+ // `moi init`'s harness decision: an explicit `--harness` wins, otherwise
220
+ // auto-detection (harness/detect.ts). Exits when neither settles it — silently
221
+ // defaulting to Claude Code would install skills into `.claude/skills/` for an
222
+ // agent that reads from somewhere else entirely, which fails later and quietly.
223
+ async function resolveInitHarness(
224
+ target: string,
225
+ requested: string | undefined
226
+ ): Promise<{ type: WorkspaceType; agent: DetectedAgent | null }> {
227
+ if (requested !== undefined) {
228
+ const value = requested.trim()
229
+ if (!isHarnessName(value)) {
230
+ console.error('\n' + pc.red('✗') + ' Unknown harness: ' + pc.bold(requested))
231
+ console.error(pc.dim(' Valid values: ' + HARNESS_TYPES.join(', ')) + '\n')
232
+ process.exit(1)
233
+ }
234
+ // An explicit choice still picks up the agent binding when the provider
235
+ // can confirm it. It often can't — OpenClaw needs a running gateway — so a
236
+ // miss warns rather than blocking a deliberate override.
237
+ const agent = await agentBindingFor(value, target)
238
+ if (!agent && (value === 'openclaw' || value === 'hermes')) {
239
+ const noun = value === 'hermes' ? 'profile' : 'agent'
240
+ console.log(
241
+ '\n' +
242
+ pc.yellow('⚠') +
243
+ ' No ' +
244
+ harnessLabel[value] +
245
+ ' ' +
246
+ noun +
247
+ ' found at this path — registering without one.\n' +
248
+ pc.dim(' Run ') +
249
+ pc.bold('moi ' + value + ' init') +
250
+ pc.dim(' to bind it once ' + harnessLabel[value] + ' can see the workspace.')
251
+ )
252
+ }
253
+ return { type: value, agent }
254
+ }
255
+
256
+ // A registered workspace has already answered this question, and may carry no
257
+ // on-disk signal at all (a folder created from the UI whose agent has never
258
+ // run in it), so the registry outranks detection when re-running `moi init`
259
+ // to refresh skills.
260
+ const known = (await listWorkspaces()).find(e => e.path === target)
261
+ if (known) {
262
+ const type = known.type ?? 'claude-code'
263
+ console.log(
264
+ '\n' + pc.dim(' Already registered — initializing for ') + pc.bold(harnessLabel[type])
265
+ )
266
+ return {
267
+ type,
268
+ agent: known.agentId
269
+ ? {
270
+ agentId: known.agentId,
271
+ ...(known.name ? { name: known.name } : {}),
272
+ isDefault: known.isDefault ?? false,
273
+ ...(known.lastRunAt ? { lastRunAt: known.lastRunAt } : {})
274
+ }
275
+ : null
276
+ }
277
+ }
278
+
279
+ const detected = await detectHarness(target)
280
+ if (detected) {
281
+ console.log(
282
+ '\n' +
283
+ pc.yellow('◆') +
284
+ ' Detected ' +
285
+ detected.reason +
286
+ ' — initializing for ' +
287
+ pc.bold(harnessLabel[detected.type]) +
288
+ '.'
289
+ )
290
+ return { type: detected.type, agent: detected.agent ?? null }
291
+ }
292
+
293
+ console.error('\n' + pc.red('✗') + ' No agent harness detected in ' + pc.bold(target))
294
+ console.error(
295
+ pc.dim(' Looked for a Hermes profile, an OpenClaw agent, and Codex or Claude Code history.\n')
296
+ )
297
+ console.error(' Pick one:\n')
298
+ console.error(
299
+ keyValue(
300
+ HARNESS_TYPES.map(t => [pc.bold('moi init --harness=' + t), harnessLabel[t]]),
301
+ ' '
302
+ )
303
+ )
304
+ console.error()
305
+ process.exit(1)
306
+ }
307
+
210
308
  const init = defineCommand({
211
309
  meta: {
212
310
  name: 'init',
@@ -218,6 +316,10 @@ const init = defineCommand({
218
316
  default: '.',
219
317
  description: 'Target directory (default: current)'
220
318
  },
319
+ harness: {
320
+ type: 'string',
321
+ description: 'Agent harness: ' + HARNESS_TYPES.join(', ') + ' (default: auto-detect)'
322
+ },
221
323
  web: {
222
324
  type: 'boolean',
223
325
  default: false,
@@ -259,28 +361,16 @@ const init = defineCommand({
259
361
  const projectRoot = join(import.meta.dir, '..')
260
362
  const isInteractive = process.stdout.isTTY
261
363
 
262
- // Detect an OpenClaw agent workspace before provisioning: skills must land
263
- // in `skills/` (not `.claude/skills/`) and the registry entry must carry
264
- // the agent metadata — same as `moi openclaw init <agent>`. Discovery
265
- // returns [] when the gateway is down, so this never blocks a plain init.
266
- const agent = (await discoverOpenClawAgents()).find(a => a.path === target) ?? null
267
- if (agent) {
268
- console.log(
269
- '\n' +
270
- pc.yellow('◆') +
271
- ' Detected an OpenClaw agent workspace ' +
272
- pc.dim('(' + agent.agentId + (agent.name ? ', ' + agent.name : '') + ')') +
273
- ' — initializing for OpenClaw.'
274
- )
275
- }
364
+ // Which backend this workspace is for has to be settled before
365
+ // provisioning: each harness reads skills from a different directory, and
366
+ // agent-owned ones (OpenClaw, Hermes) need their agent metadata on the
367
+ // registry entry — same as `moi <provider> init`.
368
+ const { type, agent } = await resolveInitHarness(target, args.harness)
276
369
 
277
370
  // Provision: bundled skills + the `.moi/` bootstrap (widgets dir +
278
371
  // package.json + bun install). An existing `.moi/` is left untouched.
279
372
  console.log()
280
- const { scaffold, skillsDir } = await provisionWorkspace(
281
- target,
282
- agent ? 'openclaw' : 'claude-code'
283
- )
373
+ const { scaffold, skillsDir } = await provisionWorkspace(target, type)
284
374
  if (scaffold !== 'exists') {
285
375
  if (scaffold === 'installing') {
286
376
  console.log(pc.dim(' Widget dependencies still installing in .moi/ (background)'))
@@ -292,25 +382,24 @@ const init = defineCommand({
292
382
  }
293
383
 
294
384
  // Always register the workspace in the persistent registry
295
- const entry = await registerWorkspace(
296
- target,
297
- agent
385
+ const entry = await registerWorkspace(target, {
386
+ type,
387
+ ...(agent
298
388
  ? {
299
- type: 'openclaw',
300
389
  name: agent.name,
301
390
  agentId: agent.agentId,
302
391
  isDefault: agent.isDefault,
303
392
  lastRunAt: agent.lastRunAt
304
393
  }
305
- : { type: 'claude-code' }
306
- )
394
+ : {})
395
+ })
307
396
 
308
- console.log(pc.green('✓') + ' Initialized ' + pc.bold(target))
397
+ console.log(pc.green('✓') + ' Initialized ' + pc.bold(target) + pc.dim(' (' + type + ')'))
309
398
  console.log(
310
399
  ' Skills installed to ' +
311
400
  pc.dim(skillsDir) +
312
401
  ' — ask ' +
313
- (agent ? 'your agent' : 'Claude') +
402
+ (type === 'claude-code' ? 'Claude' : 'your agent') +
314
403
  ' to build a widget to get started\n'
315
404
  )
316
405
 
@@ -68,6 +68,8 @@ Socket-protocol notes the layers rely on (all defined in `lib/types.ts`):
68
68
  server/harness/
69
69
  README.md this file
70
70
  debug.ts shared debug taps (wire + client-frame rings) for /dev/harness
71
+ detect.ts which harness owns a directory — `moi init`'s auto-detection
72
+ (Hermes profile → OpenClaw agent → Codex → Claude Code)
71
73
  <name>/
72
74
  adapter.ts pure wire → display mapping (layer 1 → 2); owns the
73
75
  hand-written wire types it consumes
@@ -0,0 +1,169 @@
1
+ // Which harness owns a directory — the auto-detection behind `moi init`.
2
+ //
3
+ // Precedence is deliberate. Hermes profiles and OpenClaw agents are STRUCTURAL
4
+ // workspaces: the provider owns the path and hands us an agent id, so a match
5
+ // there is definitive. Codex and Claude Code only leave history behind, which
6
+ // says "this CLI ran here once", not "this directory belongs to it" — so they
7
+ // come last. Nothing matching means moi genuinely cannot tell, and `moi init`
8
+ // asks for `--harness` rather than guessing: each backend reads skills from a
9
+ // different directory, so a wrong guess installs them where the agent never
10
+ // looks.
11
+ import { existsSync } from 'node:fs'
12
+ import { join, resolve } from 'node:path'
13
+
14
+ import type { WorkspaceType } from '@/lib/types'
15
+
16
+ import { discoverCodexWorkspaces } from './codex/discovery'
17
+ import { type HermesProfile, discoverHermesProfiles } from './hermes/discovery'
18
+ import { type OpenClawAgent, discoverOpenClawAgents } from './openclaw/discovery'
19
+
20
+ // Harness names accepted by `--harness`, in the order they are listed to the
21
+ // user. Detection order is separate (see detectHarness).
22
+ export const HARNESS_TYPES: readonly WorkspaceType[] = [
23
+ 'claude-code',
24
+ 'codex',
25
+ 'hermes',
26
+ 'openclaw'
27
+ ]
28
+
29
+ export const harnessLabel: Record<WorkspaceType, string> = {
30
+ 'claude-code': 'Claude Code',
31
+ codex: 'Codex',
32
+ hermes: 'Hermes',
33
+ openclaw: 'OpenClaw'
34
+ }
35
+
36
+ export function isHarnessName(value: string): value is WorkspaceType {
37
+ return (HARNESS_TYPES as readonly string[]).includes(value)
38
+ }
39
+
40
+ // The provider binding a registry entry carries for agent-owned workspaces
41
+ // (OpenClaw agents, Hermes profiles). Mirrors the fields registerWorkspace
42
+ // stores; plain directory harnesses have none.
43
+ export type DetectedAgent = {
44
+ agentId: string
45
+ name?: string
46
+ isDefault: boolean
47
+ lastRunAt?: string
48
+ }
49
+
50
+ export type HarnessDetection = {
51
+ type: WorkspaceType
52
+ agent?: DetectedAgent
53
+ // What gave it away, as a noun phrase for the CLI's "Detected …" line.
54
+ reason: string
55
+ }
56
+
57
+ // Injection seam: the real sources talk to a gateway, the filesystem and the
58
+ // agent SDK, so tests supply fakes instead of a provisioned machine.
59
+ export type DetectionSources = {
60
+ hermesProfiles: () => Promise<HermesProfile[]>
61
+ openclawAgents: () => Promise<OpenClawAgent[]>
62
+ // Directories each CLI has run in, from its own session history.
63
+ codexWorkspaces: () => Promise<string[]>
64
+ claudeCodeWorkspaces: () => Promise<string[]>
65
+ hasClaudeConfigDir: (path: string) => boolean
66
+ }
67
+
68
+ const defaultSources: DetectionSources = {
69
+ hermesProfiles: discoverHermesProfiles,
70
+ openclawAgents: discoverOpenClawAgents,
71
+ codexWorkspaces: async () => (await discoverCodexWorkspaces(new Set())).map(c => c.path),
72
+ // Lazy: the Claude Code harness pulls in the agent SDK, and this module is
73
+ // imported by the CLI, whose startup pays for every static import.
74
+ claudeCodeWorkspaces: async () => {
75
+ const { claudeCodeHarness } = await import('./claude-code')
76
+ const found = (await claudeCodeHarness.discoverWorkspaces?.(new Set())) ?? []
77
+ return found.map(c => c.path)
78
+ },
79
+ hasClaudeConfigDir: path => existsSync(join(path, '.claude'))
80
+ }
81
+
82
+ // A provider being unreachable (gateway down, SDK throwing) is a miss, not a
83
+ // failure — detection falls through to the next candidate.
84
+ async function safely<T>(fn: () => Promise<T>, fallback: T): Promise<T> {
85
+ try {
86
+ return await fn()
87
+ } catch {
88
+ return fallback
89
+ }
90
+ }
91
+
92
+ function detectionForProfile(profile: HermesProfile): HarnessDetection {
93
+ return {
94
+ type: 'hermes',
95
+ agent: {
96
+ agentId: profile.agentId,
97
+ ...(profile.name ? { name: profile.name } : {}),
98
+ isDefault: profile.isDefault
99
+ },
100
+ reason: `a Hermes profile workspace (${profile.agentId})`
101
+ }
102
+ }
103
+
104
+ function detectionForAgent(agent: OpenClawAgent): HarnessDetection {
105
+ return {
106
+ type: 'openclaw',
107
+ agent: {
108
+ agentId: agent.agentId,
109
+ ...(agent.name ? { name: agent.name } : {}),
110
+ isDefault: agent.isDefault,
111
+ ...(agent.lastRunAt ? { lastRunAt: agent.lastRunAt } : {})
112
+ },
113
+ reason: `an OpenClaw agent workspace (${agent.agentId}${agent.name ? ', ' + agent.name : ''})`
114
+ }
115
+ }
116
+
117
+ export async function detectHarness(
118
+ workspacePath: string,
119
+ sources: Partial<DetectionSources> = {}
120
+ ): Promise<HarnessDetection | null> {
121
+ const src = { ...defaultSources, ...sources }
122
+ const path = resolve(workspacePath)
123
+
124
+ const profile = (await safely(src.hermesProfiles, [])).find(p => p.path === path)
125
+ if (profile) return detectionForProfile(profile)
126
+
127
+ const agent = (await safely(src.openclawAgents, [])).find(a => a.path === path)
128
+ if (agent) return detectionForAgent(agent)
129
+
130
+ if ((await safely(src.codexWorkspaces, [])).includes(path)) {
131
+ return { type: 'codex', reason: 'Codex session history here' }
132
+ }
133
+
134
+ // Session history first, then the config directory: history means the CLI
135
+ // actually ran here, while `.claude/` can be committed by someone else. Note
136
+ // the history scan drops linked git worktrees, so a worktree is recognized
137
+ // by its `.claude/` directory or not at all.
138
+ if ((await safely(src.claudeCodeWorkspaces, [])).includes(path)) {
139
+ return { type: 'claude-code', reason: 'Claude Code session history here' }
140
+ }
141
+ if (src.hasClaudeConfigDir(path)) {
142
+ return { type: 'claude-code', reason: 'a .claude/ directory here' }
143
+ }
144
+
145
+ return null
146
+ }
147
+
148
+ // The agent/profile bound to a path for an explicitly chosen harness, so an
149
+ // overridden `moi init` still registers the provider metadata auto-detection
150
+ // would have found. Null for the directory-only harnesses, and for a provider
151
+ // that cannot confirm the path (gateway down, profile removed).
152
+ export async function agentBindingFor(
153
+ type: WorkspaceType,
154
+ workspacePath: string,
155
+ sources: Partial<DetectionSources> = {}
156
+ ): Promise<DetectedAgent | null> {
157
+ const src = { ...defaultSources, ...sources }
158
+ const path = resolve(workspacePath)
159
+
160
+ if (type === 'hermes') {
161
+ const profile = (await safely(src.hermesProfiles, [])).find(p => p.path === path)
162
+ return profile ? (detectionForProfile(profile).agent ?? null) : null
163
+ }
164
+ if (type === 'openclaw') {
165
+ const agent = (await safely(src.openclawAgents, [])).find(a => a.path === path)
166
+ return agent ? (detectionForAgent(agent).agent ?? null) : null
167
+ }
168
+ return null
169
+ }