moi-computer 0.9.1-next.4 → 0.10.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.
- package/README.md +124 -58
- package/client/app/shell/SidebarLayout.tsx +114 -69
- package/client/assets/floppy-disk.webp +0 -0
- package/client/assets.d.ts +5 -0
- package/client/components/ui/button.tsx +2 -1
- package/client/features/chat/ChatEmptyState.tsx +70 -34
- package/client/features/chat/ChatPanel.tsx +8 -1
- package/client/features/chat/chat-store.ts +4 -0
- package/client/features/chat/tool-group/format/claude.ts +2 -1
- package/client/features/chat/tool-group/format/codex.ts +2 -1
- package/client/features/chat/tool-group/format/hermes.ts +2 -1
- package/client/features/chat/tool-group/format/mcp.ts +7 -3
- package/client/features/chat/tool-group/format/openclaw.ts +6 -2
- package/client/features/chat/tool-group/format/shell.ts +162 -0
- package/client/features/dev/DevIndexPage.tsx +5 -0
- package/client/features/dev/DevRoutes.tsx +2 -0
- package/client/features/dev/TextureLabPage.tsx +149 -0
- package/client/features/home/HomePage.tsx +5 -5
- package/client/features/home/workspace-setup/CreateWorkspaceDialog.tsx +3 -3
- package/client/features/home/workspace-setup/DemoDialog.tsx +239 -0
- package/client/features/home/workspace-setup/WorkspaceAgentStep.tsx +22 -5
- package/client/features/settings/GeneralSettings.tsx +27 -23
- package/client/features/update/Update.tsx +78 -0
- package/client/features/update/api.ts +35 -0
- package/client/features/widgets/WidgetFrame.tsx +1 -0
- package/client/features/workspace/WorkspaceScreen.tsx +15 -0
- package/client/index.css +2 -2
- package/client/runtime/workspace-theme.ts +12 -4
- package/client/theme.css +26 -22
- package/dist/{chunk-zbt8emsw.js → chunk-0set7tmf.js} +2 -2
- package/dist/chunk-4057kxht.css +1 -0
- package/dist/{chunk-zcyvyebg.js → chunk-5t06zksf.js} +2 -2
- package/dist/{chunk-zbeq02yc.js → chunk-97eg9mjn.js} +1 -1
- package/dist/chunk-b5c62p8h.js +1 -0
- package/dist/chunk-dfe7qnrp.js +1 -0
- package/dist/{chunk-b959mdkj.js → chunk-efdyz6xp.js} +1 -1
- package/dist/chunk-f6xmz3tn.js +83 -0
- package/dist/chunk-h2kj1ysj.js +1 -0
- package/dist/{chunk-zfz3b4xg.js → chunk-ht1f95h9.js} +2 -2
- package/dist/chunk-qtc5a8qa.js +10 -0
- package/dist/floppy-disk-wmjs0n85.webp +0 -0
- package/dist/index.html +1 -1
- package/lib/ids.ts +27 -0
- package/lib/themes.ts +12 -12
- package/lib/types.ts +11 -2
- package/lib/update.ts +2 -0
- package/package.json +2 -1
- package/server/api.ts +47 -1
- package/server/app-config.ts +2 -2
- package/server/bundler/build-applet.ts +2 -3
- package/server/cli.ts +30 -7
- package/server/constants.ts +4 -2
- package/server/registry.ts +37 -2
- package/server/update.ts +293 -5
- package/server/web.ts +1 -1
- package/client/features/home/workspace-setup/InstallMoiDialog.tsx +0 -245
- package/dist/chunk-87q78504.js +0 -1
- package/dist/chunk-8s41nd3g.js +0 -1
- package/dist/chunk-cagtq45k.js +0 -1
- package/dist/chunk-ek6zde7t.js +0 -10
- package/dist/chunk-n8tq2494.js +0 -82
- package/dist/chunk-v82nefjq.css +0 -1
|
Binary file
|
package/dist/index.html
CHANGED
|
@@ -21,7 +21,7 @@
|
|
|
21
21
|
}
|
|
22
22
|
</script>
|
|
23
23
|
|
|
24
|
-
<link rel="stylesheet" crossorigin href="/chunk-
|
|
24
|
+
<link rel="stylesheet" crossorigin href="/chunk-4057kxht.css"><script type="module" crossorigin src="/chunk-b5c62p8h.js"></script></head>
|
|
25
25
|
<body>
|
|
26
26
|
<div id="root"></div>
|
|
27
27
|
|
package/lib/ids.ts
CHANGED
|
@@ -8,6 +8,33 @@ const ALPHABET = '0123456789abcdefghijklmnopqrstuvwxyz'
|
|
|
8
8
|
// whole life. 10 chars (~52 bits) stays collision-free at any realistic count.
|
|
9
9
|
export const newWorkspaceId = customAlphabet(ALPHABET, 10)
|
|
10
10
|
|
|
11
|
+
// Ids no workspace can hold, because a URL carrying one never reaches it:
|
|
12
|
+
// `/api/workspaces/:id` is registered after the literal collection routes, so
|
|
13
|
+
// `create`, `discover` and `order` match those instead, and `ws` is claimed by
|
|
14
|
+
// the live-event WebSocket upgrade in `server/web.ts` before Hono sees the
|
|
15
|
+
// request. A workspace registered under one of these would persist fine and
|
|
16
|
+
// then be unreachable in the app. `server/api-reserved-ids.test.ts` re-derives
|
|
17
|
+
// this list from the router so a new collection route cannot outgrow it.
|
|
18
|
+
export const RESERVED_WORKSPACE_IDS = ['create', 'discover', 'order', 'ws']
|
|
19
|
+
|
|
20
|
+
// Ids may also be chosen by hand (`moi init --id <id>`), so they are validated
|
|
21
|
+
// rather than assumed. Slightly wider than the generated alphabet — dashes and
|
|
22
|
+
// underscores read well in a URL — but never leading with a dash, so an id
|
|
23
|
+
// cannot be mistaken for a CLI flag.
|
|
24
|
+
export function validateWorkspaceId(id: string): string | null {
|
|
25
|
+
if (!id) return 'Workspace id is required'
|
|
26
|
+
if (id.length > 64) return 'Workspace id is too long (max 64 characters)'
|
|
27
|
+
if (!/^[A-Za-z0-9][A-Za-z0-9_-]*$/.test(id)) {
|
|
28
|
+
return 'Use letters, numbers, dashes and underscores, starting with a letter or number'
|
|
29
|
+
}
|
|
30
|
+
// Case-insensitive: only the exact-case spelling collides with a route, but
|
|
31
|
+
// accepting `Create` while refusing `create` reads as a bug, not a rule.
|
|
32
|
+
if (RESERVED_WORKSPACE_IDS.includes(id.toLowerCase())) {
|
|
33
|
+
return `Workspace id ${id} is reserved (${RESERVED_WORKSPACE_IDS.join(', ')})`
|
|
34
|
+
}
|
|
35
|
+
return null
|
|
36
|
+
}
|
|
37
|
+
|
|
11
38
|
// Builder handles are copied onto the command line (`moi builder set …
|
|
12
39
|
// --builder <id>`), and only need to be unique among a workspace's handful of
|
|
13
40
|
// builders — so 6 chars (~31 bits) keeps the command short while staying
|
package/lib/themes.ts
CHANGED
|
@@ -104,24 +104,24 @@ const THEME_COLOR_DERIVATIONS = {
|
|
|
104
104
|
workspace: {
|
|
105
105
|
primary: primary => primary,
|
|
106
106
|
primaryForeground: primary => foregroundForPrimary(primary),
|
|
107
|
-
background: () => 'color-mix(in
|
|
108
|
-
foreground: () => 'color-mix(in
|
|
109
|
-
muted: () => 'color-mix(in
|
|
110
|
-
mutedForeground: () => 'color-mix(in
|
|
111
|
-
accent: () => 'color-mix(in
|
|
107
|
+
background: () => 'color-mix(in srgb, var(--primary) 3%, oklch(1 0 0) 97%)',
|
|
108
|
+
foreground: () => 'color-mix(in srgb, var(--primary) 20%, oklch(0 0 0) 80%)',
|
|
109
|
+
muted: () => 'color-mix(in srgb, var(--background) 97%, var(--foreground) 3%)',
|
|
110
|
+
mutedForeground: () => 'color-mix(in srgb, var(--background) 50%, var(--foreground) 50%)',
|
|
111
|
+
accent: () => 'color-mix(in srgb, var(--primary) 4%, var(--foreground) 4%)',
|
|
112
112
|
accentForeground: () => 'var(--foreground)',
|
|
113
|
-
border: () => 'color-mix(in
|
|
113
|
+
border: () => 'color-mix(in srgb, var(--foreground) 7%, transparent)'
|
|
114
114
|
},
|
|
115
115
|
widget: {
|
|
116
116
|
background: primary => primary,
|
|
117
117
|
foreground: primary => foregroundForPrimary(primary),
|
|
118
|
-
primary: () => 'color-mix(in
|
|
119
|
-
primaryForeground: () => 'color-mix(in
|
|
120
|
-
muted: () => 'color-mix(in
|
|
121
|
-
mutedForeground: () => 'color-mix(in
|
|
122
|
-
accent: () => 'color-mix(in
|
|
118
|
+
primary: () => 'color-mix(in srgb, var(--background) 3%, oklch(1 0 0) 97%)',
|
|
119
|
+
primaryForeground: () => 'color-mix(in srgb, var(--background) 20%, oklch(0 0 0) 80%)',
|
|
120
|
+
muted: () => 'color-mix(in srgb, var(--background) 97%, var(--foreground) 3%)',
|
|
121
|
+
mutedForeground: () => 'color-mix(in srgb, var(--background) 50%, var(--foreground) 50%)',
|
|
122
|
+
accent: () => 'color-mix(in srgb, var(--primary) 4%, var(--foreground) 4%)',
|
|
123
123
|
accentForeground: () => 'var(--foreground)',
|
|
124
|
-
border: () => 'color-mix(in
|
|
124
|
+
border: () => 'color-mix(in srgb, var(--foreground) 15%, transparent)'
|
|
125
125
|
}
|
|
126
126
|
} satisfies Record<ThemeColorMode, Record<string, (primary: string) => string>>
|
|
127
127
|
|
package/lib/types.ts
CHANGED
|
@@ -291,14 +291,23 @@ export type AppSettings = {
|
|
|
291
291
|
// forever. See server/app-config.ts.
|
|
292
292
|
export type ClientAppConfig = {
|
|
293
293
|
// Cloud demo deployment: workspace creation is blocked; the UI offers the
|
|
294
|
-
//
|
|
294
|
+
// cloud-demo promo dialog instead.
|
|
295
295
|
cloudDemo: boolean
|
|
296
296
|
// Enabled experimental features, checked by slug.
|
|
297
297
|
experiments: string[]
|
|
298
|
-
// Link target for the
|
|
298
|
+
// Link target for the cloud-demo promo dialog.
|
|
299
299
|
demoInstallUrl: string
|
|
300
300
|
}
|
|
301
301
|
|
|
302
|
+
export type UpdateStatus = {
|
|
303
|
+
runningVersion: string
|
|
304
|
+
availableVersion: string | null
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
export type UpdateResult = {
|
|
308
|
+
installedVersion: string
|
|
309
|
+
}
|
|
310
|
+
|
|
302
311
|
// Re-export the display format
|
|
303
312
|
export type {
|
|
304
313
|
Citation,
|
package/lib/update.ts
ADDED
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "moi-computer",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
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",
|
|
@@ -94,6 +94,7 @@
|
|
|
94
94
|
"remark-gfm": "^4.0.1",
|
|
95
95
|
"shadcn": "^4.13.0",
|
|
96
96
|
"sharp": "^0.35.1",
|
|
97
|
+
"shell-quote": "^1.10.0",
|
|
97
98
|
"sugar-high": "^1.2.1",
|
|
98
99
|
"tailwind-merge": "^3.6.0",
|
|
99
100
|
"tailwindcss": "^4.3.3",
|
package/server/api.ts
CHANGED
|
@@ -4,6 +4,7 @@ import { createMiddleware } from 'hono/factory'
|
|
|
4
4
|
import { existsSync } from 'node:fs'
|
|
5
5
|
import { basename, join, resolve } from 'node:path'
|
|
6
6
|
|
|
7
|
+
import { UPDATE_ACTIVE_AGENT_MESSAGE } from '@/lib/update'
|
|
7
8
|
import type {
|
|
8
9
|
AppletKind,
|
|
9
10
|
AppletThumbnailBatch,
|
|
@@ -84,6 +85,14 @@ import { resolveWorkspaceImportMetadata } from './workspace-import'
|
|
|
84
85
|
import type { WorkspaceImportMetadata } from './workspace-import'
|
|
85
86
|
import { getWorkspaceEnvView, isValidEnvKey, updateWorkspaceEnv } from './workspace-env'
|
|
86
87
|
import type { EnvUpdate } from './workspace-env'
|
|
88
|
+
import {
|
|
89
|
+
getCachedUpdateStatus,
|
|
90
|
+
hasRunningAgentSessions,
|
|
91
|
+
installUpdate,
|
|
92
|
+
restartPendingForUpdate,
|
|
93
|
+
scheduleRestartForUpdate,
|
|
94
|
+
updateInProgress
|
|
95
|
+
} from './update'
|
|
87
96
|
|
|
88
97
|
// The resolved workspace is stashed on the context by `withWorkspace`, so every
|
|
89
98
|
// `/api/workspaces/:id/*` handler can read it without re-querying the registry.
|
|
@@ -945,7 +954,7 @@ workspaces.put('/order', async c => {
|
|
|
945
954
|
|
|
946
955
|
// One message for both creation routes: in the cloud demo the instance is
|
|
947
956
|
// provisioned with its workspaces; the UI never sends these requests (it opens
|
|
948
|
-
// the
|
|
957
|
+
// the cloud demo promo dialog instead), so a 403 here means the API was called
|
|
949
958
|
// directly.
|
|
950
959
|
const DEMO_CREATE_BLOCKED = 'Workspace creation is not available in the moi demo'
|
|
951
960
|
|
|
@@ -1059,6 +1068,43 @@ api.route('/api/workspaces', workspaces)
|
|
|
1059
1068
|
// subset. Immutable for the process lifetime — clients cache it forever.
|
|
1060
1069
|
api.get('/api/config', c => c.json(clientAppConfig()))
|
|
1061
1070
|
|
|
1071
|
+
// Served from a lazily-refreshed cache: this handler is a memory read, and the
|
|
1072
|
+
// registry is consulted on the schedule `getCachedUpdateStatus` owns rather
|
|
1073
|
+
// than once per request. `no-store` keeps the browser from adding a second,
|
|
1074
|
+
// unrelated cache on top of it.
|
|
1075
|
+
api.get('/api/update', async c => {
|
|
1076
|
+
c.header('Cache-Control', 'no-store')
|
|
1077
|
+
return c.json(await getCachedUpdateStatus())
|
|
1078
|
+
})
|
|
1079
|
+
|
|
1080
|
+
api.post('/api/update', async c => {
|
|
1081
|
+
if (updateInProgress()) {
|
|
1082
|
+
return c.text(
|
|
1083
|
+
restartPendingForUpdate()
|
|
1084
|
+
? 'An update is installed — moi is restarting.'
|
|
1085
|
+
: 'An update is already in progress.',
|
|
1086
|
+
409
|
|
1087
|
+
)
|
|
1088
|
+
}
|
|
1089
|
+
|
|
1090
|
+
const agentRunning = hasRunningAgentSessions(
|
|
1091
|
+
allHarnesses().flatMap(harness => harness.activeSessions())
|
|
1092
|
+
)
|
|
1093
|
+
if (agentRunning) {
|
|
1094
|
+
return c.text(UPDATE_ACTIVE_AGENT_MESSAGE, 409)
|
|
1095
|
+
}
|
|
1096
|
+
|
|
1097
|
+
try {
|
|
1098
|
+
const result = await installUpdate()
|
|
1099
|
+
if (!result) return c.text('This moi install cannot be updated from the app.', 409)
|
|
1100
|
+
const response = c.json(result)
|
|
1101
|
+
scheduleRestartForUpdate()
|
|
1102
|
+
return response
|
|
1103
|
+
} catch (error) {
|
|
1104
|
+
return c.text(error instanceof Error ? error.message : 'Could not update moi.', 500)
|
|
1105
|
+
}
|
|
1106
|
+
})
|
|
1107
|
+
|
|
1062
1108
|
// App-wide settings (settings.json in the data dir). GET returns every key
|
|
1063
1109
|
// with defaults applied; PATCH merges a partial body and returns the result.
|
|
1064
1110
|
api.get('/api/settings', c => c.json(getAppSettings()))
|
package/server/app-config.ts
CHANGED
|
@@ -16,11 +16,11 @@ import { DATA_DIR } from './data-dir'
|
|
|
16
16
|
// per-key, so a broken config never takes the CLI or server down with it.
|
|
17
17
|
export type AppConfig = {
|
|
18
18
|
// Cloud demo deployment: workspace creation is blocked (UI shows the
|
|
19
|
-
//
|
|
19
|
+
// cloud-demo promo dialog instead) and `moi` system commands are disabled.
|
|
20
20
|
cloudDemo: boolean
|
|
21
21
|
// Gated experimental features, checked by slug.
|
|
22
22
|
experiments: string[]
|
|
23
|
-
// Link target for the
|
|
23
|
+
// Link target for the cloud-demo promo dialog.
|
|
24
24
|
demoInstallUrl: string
|
|
25
25
|
}
|
|
26
26
|
|
|
@@ -452,9 +452,8 @@ async function writeSyntheticTailwindCss(
|
|
|
452
452
|
// back to `@media (prefers-color-scheme: dark)`, which diverges from the
|
|
453
453
|
// host and injects OS-preference media queries into the page.
|
|
454
454
|
`@custom-variant dark (&:is(.dark *));`,
|
|
455
|
-
//
|
|
456
|
-
|
|
457
|
-
`@custom-variant dark-or-widget (&:is(.dark *, [data-widget-chrome] *));`,
|
|
455
|
+
// Dark themes and vivid surfaces use the stronger texture treatment.
|
|
456
|
+
`@custom-variant dark-or-vivid (&:is(.dark *, [data-vivid], [data-vivid] *));`,
|
|
458
457
|
`@source "${sourceDir}";`
|
|
459
458
|
].join('\n')
|
|
460
459
|
await Bun.write(cssPath, contents)
|
package/server/cli.ts
CHANGED
|
@@ -58,7 +58,12 @@ import {
|
|
|
58
58
|
matchHermesProfile
|
|
59
59
|
} from './harness/hermes/discovery'
|
|
60
60
|
import { type OpenClawAgent, discoverOpenClawAgents } from './harness/openclaw/discovery'
|
|
61
|
-
import {
|
|
61
|
+
import {
|
|
62
|
+
assertWorkspaceIdAvailable,
|
|
63
|
+
liftToWorkspaceRoot,
|
|
64
|
+
listWorkspaces,
|
|
65
|
+
registerWorkspace
|
|
66
|
+
} from './registry'
|
|
62
67
|
import { serverCwd } from './server-cwd'
|
|
63
68
|
import {
|
|
64
69
|
isBehind,
|
|
@@ -86,6 +91,7 @@ import {
|
|
|
86
91
|
isNewer,
|
|
87
92
|
manualUpdateLines,
|
|
88
93
|
runPackageManager,
|
|
94
|
+
superviseServerUpdates,
|
|
89
95
|
updateArgv
|
|
90
96
|
} from './update'
|
|
91
97
|
import { VERSION, isPrerelease, versionWithCommit } from './version'
|
|
@@ -324,6 +330,11 @@ const init = defineCommand({
|
|
|
324
330
|
type: 'boolean',
|
|
325
331
|
default: false,
|
|
326
332
|
description: 'Start the web server if not already running'
|
|
333
|
+
},
|
|
334
|
+
id: {
|
|
335
|
+
type: 'string',
|
|
336
|
+
description:
|
|
337
|
+
'Register under this id instead of a generated one — fails if the workspace is already registered'
|
|
327
338
|
}
|
|
328
339
|
},
|
|
329
340
|
async run({ args }) {
|
|
@@ -358,6 +369,17 @@ const init = defineCommand({
|
|
|
358
369
|
)
|
|
359
370
|
}
|
|
360
371
|
|
|
372
|
+
// A chosen id is only assignable at first registration, so refuse it here —
|
|
373
|
+
// before any skills are copied or `.moi/` is scaffolded.
|
|
374
|
+
if (args.id) {
|
|
375
|
+
try {
|
|
376
|
+
await assertWorkspaceIdAvailable(target, args.id)
|
|
377
|
+
} catch (err) {
|
|
378
|
+
console.error('\n' + pc.red('✗') + ' ' + (err as Error).message + '\n')
|
|
379
|
+
process.exit(1)
|
|
380
|
+
}
|
|
381
|
+
}
|
|
382
|
+
|
|
361
383
|
const projectRoot = join(import.meta.dir, '..')
|
|
362
384
|
const isInteractive = process.stdout.isTTY
|
|
363
385
|
|
|
@@ -383,6 +405,7 @@ const init = defineCommand({
|
|
|
383
405
|
|
|
384
406
|
// Always register the workspace in the persistent registry
|
|
385
407
|
const entry = await registerWorkspace(target, {
|
|
408
|
+
...(args.id ? { id: args.id } : {}),
|
|
386
409
|
type,
|
|
387
410
|
...(agent
|
|
388
411
|
? {
|
|
@@ -408,7 +431,8 @@ const init = defineCommand({
|
|
|
408
431
|
|
|
409
432
|
if (!running && args.web) {
|
|
410
433
|
console.log(pc.dim(' Starting server…'))
|
|
411
|
-
const
|
|
434
|
+
const cwd = serverCwd(projectRoot, false)
|
|
435
|
+
const proc = spawnServer(cwd)
|
|
412
436
|
running = await waitForServer()
|
|
413
437
|
if (!running) {
|
|
414
438
|
console.error(pc.red(' Server failed to start\n'))
|
|
@@ -420,8 +444,7 @@ const init = defineCommand({
|
|
|
420
444
|
if (isInteractive) console.log(' Opening ' + pc.bold(url))
|
|
421
445
|
console.log(pc.dim(' Press Ctrl+C to stop\n'))
|
|
422
446
|
if (isInteractive) await openBrowser(url)
|
|
423
|
-
await proc
|
|
424
|
-
process.exit(proc.exitCode ?? 0)
|
|
447
|
+
process.exit(await superviseServerUpdates(proc, () => spawnServer(cwd)))
|
|
425
448
|
}
|
|
426
449
|
|
|
427
450
|
if (running) {
|
|
@@ -506,9 +529,9 @@ const start = defineCommand({
|
|
|
506
529
|
await runDevSupervisor(projectRoot, env)
|
|
507
530
|
return
|
|
508
531
|
}
|
|
509
|
-
const
|
|
510
|
-
|
|
511
|
-
process.exit(proc
|
|
532
|
+
const cwd = serverCwd(projectRoot, dev)
|
|
533
|
+
const proc = spawnServer(cwd, env)
|
|
534
|
+
process.exit(await superviseServerUpdates(proc, () => spawnServer(cwd, env)))
|
|
512
535
|
}
|
|
513
536
|
|
|
514
537
|
// This IS the server process (MOI_SERVER=1). cwd is the package root when the
|
package/server/constants.ts
CHANGED
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
export const PORT = Number(process.env.PORT) || 13337
|
|
2
|
-
// M=13, E=5, I=9 -> 13059
|
|
3
|
-
|
|
2
|
+
// M=13, E=5, I=9 -> 13059. Overridable like PORT so a test can bring up a
|
|
3
|
+
// throwaway server beside a real one; server and CLI read this same module, so
|
|
4
|
+
// a process given the override binds and dials the same port either way.
|
|
5
|
+
export const CONTROL_PORT = Number(process.env.MOI_CONTROL_PORT) || 13059
|
|
4
6
|
// The address the control server binds AND the one every client dials — never
|
|
5
7
|
// the name `localhost`. Resolution of that name is not guaranteed: container
|
|
6
8
|
// images ship an /etc/hosts without a `localhost` entry (Bun's resolver, unlike
|
package/server/registry.ts
CHANGED
|
@@ -2,7 +2,7 @@ import { mkdir } from 'node:fs/promises'
|
|
|
2
2
|
import { homedir } from 'node:os'
|
|
3
3
|
import { join, resolve, sep } from 'path'
|
|
4
4
|
|
|
5
|
-
import { newWorkspaceId } from '@/lib/ids'
|
|
5
|
+
import { newWorkspaceId, validateWorkspaceId } from '@/lib/ids'
|
|
6
6
|
import type { DiscoveredWorkspace, WorkspaceEntry, WorkspaceType } from '@/lib/types'
|
|
7
7
|
import { orderWorkspaceTypes } from '@/lib/workspace-types'
|
|
8
8
|
|
|
@@ -48,6 +48,9 @@ async function writeRegistry(entries: WorkspaceEntry[]): Promise<void> {
|
|
|
48
48
|
}
|
|
49
49
|
|
|
50
50
|
export type RegisterOptions = {
|
|
51
|
+
// Register under this id instead of a generated one. Only accepted while the
|
|
52
|
+
// workspace is unregistered — see `workspaceIdError`.
|
|
53
|
+
id?: string
|
|
51
54
|
type?: WorkspaceType
|
|
52
55
|
name?: string
|
|
53
56
|
agentId?: string
|
|
@@ -55,16 +58,48 @@ export type RegisterOptions = {
|
|
|
55
58
|
lastRunAt?: string
|
|
56
59
|
}
|
|
57
60
|
|
|
61
|
+
// Why a chosen id can be refused. An id is baked into `/workspace/<id>` URLs
|
|
62
|
+
// and outlives the registration, so it is assigned once: a workspace that
|
|
63
|
+
// already carries an id keeps it, and no two workspaces may share one.
|
|
64
|
+
function workspaceIdError(
|
|
65
|
+
entries: WorkspaceEntry[],
|
|
66
|
+
normalPath: string,
|
|
67
|
+
id: string
|
|
68
|
+
): string | null {
|
|
69
|
+
const invalid = validateWorkspaceId(id)
|
|
70
|
+
if (invalid) return invalid
|
|
71
|
+
const existing = entries.find(e => e.path === normalPath)
|
|
72
|
+
if (existing && existing.id !== id) {
|
|
73
|
+
return `${normalPath} is already registered with id ${existing.id}`
|
|
74
|
+
}
|
|
75
|
+
if (entries.some(e => e.id === id && e.path !== normalPath)) {
|
|
76
|
+
return `Workspace id ${id} is already taken`
|
|
77
|
+
}
|
|
78
|
+
return null
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// Check a chosen id against the registry without registering anything, so a
|
|
82
|
+
// caller can fail before doing expensive setup work. `registerWorkspace`
|
|
83
|
+
// re-checks, so this is a courtesy, not the enforcement point.
|
|
84
|
+
export async function assertWorkspaceIdAvailable(absPath: string, id: string): Promise<void> {
|
|
85
|
+
const error = workspaceIdError(await readRegistry(), resolve(absPath), id)
|
|
86
|
+
if (error) throw new Error(error)
|
|
87
|
+
}
|
|
88
|
+
|
|
58
89
|
export async function registerWorkspace(
|
|
59
90
|
absPath: string,
|
|
60
91
|
opts: RegisterOptions = {}
|
|
61
92
|
): Promise<WorkspaceEntry> {
|
|
62
93
|
const normalPath = resolve(absPath)
|
|
63
94
|
const entries = await readRegistry()
|
|
95
|
+
if (opts.id) {
|
|
96
|
+
const error = workspaceIdError(entries, normalPath, opts.id)
|
|
97
|
+
if (error) throw new Error(error)
|
|
98
|
+
}
|
|
64
99
|
const existing = entries.find(e => e.path === normalPath)
|
|
65
100
|
if (existing) return existing
|
|
66
101
|
const entry: WorkspaceEntry = {
|
|
67
|
-
id: newWorkspaceId(),
|
|
102
|
+
id: opts.id ?? newWorkspaceId(),
|
|
68
103
|
path: normalPath,
|
|
69
104
|
addedAt: new Date().toISOString(),
|
|
70
105
|
...(opts.type ? { type: opts.type } : {}),
|