moi-computer 0.9.1-next.2 → 0.9.1-next.4
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 +4 -1
- package/package.json +1 -1
- package/server/cli.ts +117 -28
- package/server/harness/README.md +2 -0
- package/server/harness/codex/client.ts +23 -0
- package/server/harness/codex/index.ts +8 -8
- package/server/harness/codex/session.ts +12 -4
- package/server/harness/codex/status.ts +31 -0
- package/server/harness/detect.ts +169 -0
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
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
|
-
//
|
|
263
|
-
//
|
|
264
|
-
//
|
|
265
|
-
//
|
|
266
|
-
const agent =
|
|
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
|
-
|
|
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
|
-
: {
|
|
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
|
-
(
|
|
402
|
+
(type === 'claude-code' ? 'Claude' : 'your agent') +
|
|
314
403
|
' to build a widget to get started\n'
|
|
315
404
|
)
|
|
316
405
|
|
package/server/harness/README.md
CHANGED
|
@@ -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
|
|
@@ -44,6 +44,12 @@ export type CodexProcessInfo = {
|
|
|
44
44
|
binary: string | null
|
|
45
45
|
}
|
|
46
46
|
|
|
47
|
+
export type CodexProcessSnapshot = {
|
|
48
|
+
workspacePath: string
|
|
49
|
+
pid: number
|
|
50
|
+
startedAt: number
|
|
51
|
+
}
|
|
52
|
+
|
|
47
53
|
export function getCodexProcessInfo(workspacePath: string): Promise<CodexProcessInfo> {
|
|
48
54
|
const rec = clients.get(workspacePath)
|
|
49
55
|
const binary = findHarnessExecutable('codex')
|
|
@@ -87,11 +93,26 @@ type ClientRecord = {
|
|
|
87
93
|
}
|
|
88
94
|
|
|
89
95
|
const clients = new Map<string, Promise<ClientRecord>>() // key: workspacePath
|
|
96
|
+
const liveProcesses = new Map<
|
|
97
|
+
string,
|
|
98
|
+
{ proc: ReturnType<typeof Bun.spawn>; startedAt: number; isAlive: () => boolean }
|
|
99
|
+
>()
|
|
100
|
+
|
|
101
|
+
export function getCodexProcessSnapshot(): CodexProcessSnapshot[] {
|
|
102
|
+
return [...liveProcesses.entries()]
|
|
103
|
+
.filter(([, process]) => process.isAlive())
|
|
104
|
+
.map(([workspacePath, process]) => ({
|
|
105
|
+
workspacePath,
|
|
106
|
+
pid: process.proc.pid,
|
|
107
|
+
startedAt: process.startedAt
|
|
108
|
+
}))
|
|
109
|
+
}
|
|
90
110
|
|
|
91
111
|
async function startClient(workspacePath: string): Promise<ClientRecord> {
|
|
92
112
|
const bin = requireHarnessExecutable('codex')
|
|
93
113
|
|
|
94
114
|
const workspaceEnv = await resolveWorkspaceEnv(workspacePath)
|
|
115
|
+
const startedAt = Date.now()
|
|
95
116
|
const proc = Bun.spawn([bin, 'app-server'], {
|
|
96
117
|
cwd: workspacePath,
|
|
97
118
|
stdin: 'pipe',
|
|
@@ -202,6 +223,7 @@ async function startClient(workspacePath: string): Promise<ClientRecord> {
|
|
|
202
223
|
} finally {
|
|
203
224
|
alive = false
|
|
204
225
|
if (clients.get(workspacePath) === recordPromise) clients.delete(workspacePath)
|
|
226
|
+
if (liveProcesses.get(workspacePath)?.proc === proc) liveProcesses.delete(workspacePath)
|
|
205
227
|
for (const [, p] of pending) {
|
|
206
228
|
clearTimeout(p.timer)
|
|
207
229
|
p.reject(new Error('codex app-server exited'))
|
|
@@ -237,6 +259,7 @@ async function startClient(workspacePath: string): Promise<ClientRecord> {
|
|
|
237
259
|
}
|
|
238
260
|
const record: ClientRecord = { client, proc }
|
|
239
261
|
const recordPromise = Promise.resolve(record)
|
|
262
|
+
liveProcesses.set(workspacePath, { proc, startedAt, isAlive: client.isAlive })
|
|
240
263
|
|
|
241
264
|
void readLoop()
|
|
242
265
|
void drainStderr()
|
|
@@ -8,6 +8,7 @@ import {
|
|
|
8
8
|
getCodexMcpStatus,
|
|
9
9
|
getCodexModels,
|
|
10
10
|
getCodexProcessInfo,
|
|
11
|
+
getCodexProcessSnapshot,
|
|
11
12
|
getCodexSessions,
|
|
12
13
|
getCodexThreadEvents,
|
|
13
14
|
getCodexWorkspacePreview,
|
|
@@ -22,6 +23,7 @@ import {
|
|
|
22
23
|
interruptCodexRun,
|
|
23
24
|
sendCodexMessage
|
|
24
25
|
} from './session'
|
|
26
|
+
import { formatCodexStatusLines } from './status'
|
|
25
27
|
|
|
26
28
|
export const codexHarness: Harness = {
|
|
27
29
|
id: 'codex',
|
|
@@ -72,12 +74,10 @@ export const codexHarness: Harness = {
|
|
|
72
74
|
debugInfo: ws => getCodexProcessInfo(ws.path),
|
|
73
75
|
wireScope: ws => ws.path,
|
|
74
76
|
|
|
75
|
-
statusLines: () =>
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
]
|
|
82
|
-
}
|
|
77
|
+
statusLines: () =>
|
|
78
|
+
formatCodexStatusLines({
|
|
79
|
+
executable: findHarnessExecutable('codex'),
|
|
80
|
+
processes: getCodexProcessSnapshot(),
|
|
81
|
+
active: getCodexActiveSessions()
|
|
82
|
+
})
|
|
83
83
|
}
|
|
@@ -108,19 +108,27 @@ function liveKey(workspaceId: string, sessionId: string): string {
|
|
|
108
108
|
return real ? recKey(workspaceId, real) : direct
|
|
109
109
|
}
|
|
110
110
|
|
|
111
|
-
export
|
|
111
|
+
export type CodexActiveSession = {
|
|
112
112
|
workspaceId: string
|
|
113
|
+
workspacePath: string
|
|
113
114
|
sessionId: string
|
|
114
115
|
activity: SessionActivity
|
|
115
|
-
}
|
|
116
|
-
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
export function getCodexActiveSessions(): CodexActiveSession[] {
|
|
119
|
+
const out: CodexActiveSession[] = []
|
|
117
120
|
for (const s of sessions.values()) {
|
|
118
121
|
// Codex never surfaces `requires-action`: approvals are designed out
|
|
119
122
|
// (APPROVAL_POLICY 'never' + transport auto-accept in client.ts). If that
|
|
120
123
|
// policy is ever relaxed, `*requestApproval` / `elicitation` server
|
|
121
124
|
// requests are the signals to map to it.
|
|
122
125
|
if (s.processing) {
|
|
123
|
-
out.push({
|
|
126
|
+
out.push({
|
|
127
|
+
workspaceId: s.workspaceId,
|
|
128
|
+
workspacePath: s.workspacePath,
|
|
129
|
+
sessionId: s.sessionId,
|
|
130
|
+
activity: 'running'
|
|
131
|
+
})
|
|
124
132
|
}
|
|
125
133
|
}
|
|
126
134
|
return out
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import type { CodexProcessSnapshot } from './client'
|
|
2
|
+
import type { CodexActiveSession } from './session'
|
|
3
|
+
|
|
4
|
+
type CodexStatusInput = {
|
|
5
|
+
executable: string | null
|
|
6
|
+
processes: CodexProcessSnapshot[]
|
|
7
|
+
active: CodexActiveSession[]
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export function formatCodexStatusLines({
|
|
11
|
+
executable,
|
|
12
|
+
processes,
|
|
13
|
+
active
|
|
14
|
+
}: CodexStatusInput): string[] {
|
|
15
|
+
const busyWorkspacePaths = new Set(active.map(run => run.workspacePath))
|
|
16
|
+
const busyProcesses = processes.filter(process => busyWorkspacePaths.has(process.workspacePath))
|
|
17
|
+
const processLines = processes
|
|
18
|
+
.toSorted((a, b) => a.workspacePath.localeCompare(b.workspacePath))
|
|
19
|
+
.map(process => {
|
|
20
|
+
const state = busyWorkspacePaths.has(process.workspacePath) ? '▶ busy' : '○ idle'
|
|
21
|
+
return ` ${state} ws=${process.workspacePath} pid=${process.pid}`
|
|
22
|
+
})
|
|
23
|
+
|
|
24
|
+
return [
|
|
25
|
+
`codex executable ${executable ?? '(not found)'}`,
|
|
26
|
+
`codex app-servers ${processes.length} (${busyProcesses.length} busy, ${processes.length - busyProcesses.length} idle)`,
|
|
27
|
+
...(processLines.length ? processLines : [' (none running)']),
|
|
28
|
+
`active Codex turns ${active.length}`,
|
|
29
|
+
...active.map(run => ` ▶ busy ws=${run.workspaceId} session=${run.sessionId}`)
|
|
30
|
+
]
|
|
31
|
+
}
|
|
@@ -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
|
+
}
|