moi-computer 0.12.1-next.3 → 0.13.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/bunfig.toml +1 -5
- package/client/api/app-config.ts +1 -2
- package/client/api/app-ui-keys.ts +3 -1
- package/client/api/http.ts +4 -1
- package/client/api/workspace-keys.ts +3 -3
- package/client/app/app-provider.tsx +22 -2
- package/client/app/app-router.tsx +4 -0
- package/client/app/routes/home-route.tsx +14 -0
- package/client/app/routes/onboarding-route.tsx +11 -0
- package/client/app/routes/settings-route.tsx +1 -1
- package/client/app/routes/workspace-route.tsx +21 -19
- package/client/components/shared/screen-shell.tsx +23 -0
- package/client/components/shared/ultrafast-icon.tsx +61 -0
- package/client/components/shared/workspace-icon.tsx +2 -2
- package/client/features/account/account-test-utils.tsx +4 -3
- package/client/features/app-settings/api.ts +33 -0
- package/client/features/app-settings/general-settings.tsx +39 -0
- package/client/features/{settings → app-settings}/settings-page.tsx +5 -1
- package/client/features/chat/applet-chat-intents.ts +4 -20
- package/client/features/chat/chat-send.ts +17 -13
- package/client/features/chat/composer/model-order.ts +8 -4
- package/client/features/chat/composer/model-picker.tsx +88 -34
- package/client/features/chat/composer/use-workspace-composer-state.tsx +11 -3
- package/client/features/chat/composer/use-workspace-skill-updates.ts +1 -1
- package/client/features/chat/connection/chat-frames.ts +3 -15
- package/client/features/chat/messages/chat-empty-state.tsx +1 -4
- package/client/features/chat/messages/fork-history.ts +5 -5
- package/client/features/chat/messages/tool-group/subagent-card.tsx +1 -0
- package/client/features/chat/messages/turn-view.tsx +3 -12
- package/client/features/chat/messages/user-message-bubble.tsx +63 -0
- package/client/features/chat/sessions/api.ts +121 -80
- package/client/features/chat/sessions/browser-tab-state.ts +14 -12
- package/client/features/chat/sessions/chat-selector.tsx +5 -6
- package/client/features/chat/sessions/use-selected-session.ts +172 -257
- package/client/features/chat/use-chat.ts +14 -19
- package/client/features/collab/components/user-avatar.tsx +4 -10
- package/client/features/collab/workspace-users-menu.tsx +1 -1
- package/client/features/connectors/api.ts +12 -4
- package/client/features/connectors/mcp-server-cards.tsx +17 -7
- package/client/features/connectors/read-connectors.ts +35 -0
- package/client/features/demo/demo-dialog.tsx +11 -3
- package/client/features/dev/collab/local-user-settings.tsx +1 -1
- package/client/features/dev/dev-index-page.tsx +5 -0
- package/client/features/dev/dev-routes.tsx +2 -0
- package/client/features/dev/harness-debug-page.tsx +3 -3
- package/client/features/dev/onboarding-preview-page.tsx +76 -0
- package/client/features/dev/workspace-setup-mocks.ts +16 -14
- package/client/features/dev/workspace-setup-preview.tsx +11 -17
- package/client/features/harness/api.ts +45 -15
- package/client/features/harness/device-login-steps.tsx +9 -3
- package/client/features/harness/harness-login-panel.tsx +228 -0
- package/client/features/harness/harness-presentation.ts +15 -0
- package/client/features/harness/harness-selector.tsx +119 -0
- package/client/features/harness/use-harness-login.ts +77 -0
- package/client/features/home/home-page.tsx +7 -7
- package/client/features/onboarding/agent-setup-step.tsx +57 -0
- package/client/features/onboarding/api.ts +42 -0
- package/client/features/onboarding/onboarding-idea-disk.tsx +129 -0
- package/client/features/onboarding/onboarding-page.tsx +32 -0
- package/client/features/onboarding/onboarding-screen.tsx +20 -0
- package/client/features/onboarding/onboarding-status.tsx +24 -0
- package/client/features/onboarding/workspace-setup-step.tsx +239 -0
- package/client/features/overview/default/registry.tsx +2 -2
- package/client/features/overview/default/views-widget.tsx +6 -5
- package/client/features/overview/overview.tsx +9 -11
- package/client/features/overview/widget-canvas.tsx +1 -1
- package/client/features/overview/workspace-name.tsx +2 -2
- package/client/features/views/api.ts +69 -16
- package/client/features/views/pending-view-screen.tsx +30 -9
- package/client/features/views/use-view-actions.ts +19 -20
- package/client/features/views/view-presentation.ts +2 -2
- package/client/features/views/view-submission.ts +28 -41
- package/client/features/workspace/add/api.ts +3 -23
- package/client/features/workspace/add/create-workspace-dialog.tsx +6 -5
- package/client/features/workspace/add/import-workspace-dialog.tsx +3 -3
- package/client/features/workspace/add/workspace-agent-options.ts +11 -12
- package/client/features/workspace/add/workspace-agent-selector.tsx +3 -132
- package/client/features/workspace/api.ts +32 -36
- package/client/features/workspace/harness-transition.ts +89 -0
- package/client/features/{settings → workspace/settings}/api.ts +1 -34
- package/client/features/{settings → workspace/settings}/connectors-settings.tsx +31 -10
- package/client/features/workspace/settings/general-settings.tsx +180 -0
- package/client/features/{settings → workspace/settings}/use-env-vars.ts +1 -1
- package/client/features/{settings → workspace/settings}/workspace-icon-picker.tsx +3 -3
- package/client/features/workspace/setup/workspace-login-state.tsx +37 -156
- package/client/features/workspace/setup/workspace-recovery-states.tsx +0 -12
- package/client/features/workspace/setup/workspace-setup.tsx +34 -14
- package/client/features/workspace/setup/workspace-welcome-state.tsx +2 -2
- package/client/features/workspace/use-workspace-navigation.ts +25 -9
- package/client/features/workspace/workspace-content.tsx +11 -8
- package/client/features/workspace/workspace-frame.tsx +2 -1
- package/client/features/workspace/workspace-layout-context.tsx +5 -2
- package/client/features/workspace/workspace-menu.tsx +12 -8
- package/client/features/workspace/workspace-pending-screen.tsx +10 -10
- package/client/features/workspace/workspace-presentation.tsx +5 -13
- package/client/features/workspace/workspace-state.ts +169 -0
- package/client/features/workspace/workspace-tabs.tsx +22 -2
- package/client/index.css +2 -0
- package/client/index.tsx +3 -3
- package/client/lib/flags.ts +1 -1
- package/client/lib/{app-icon-registry.ts → identity-icon-registry.ts} +10 -14
- package/client/runtime/workspace-events.ts +21 -4
- package/client/runtime/workspace-theme.ts +9 -0
- package/client/tailwind-config.ts +43 -0
- package/dist/chunk-2y59vrxg.js +5 -0
- package/dist/chunk-58dyt9bk.js +2 -0
- package/dist/{chunk-tqd3pkg8.css → chunk-60q353jq.css} +1 -1
- package/dist/chunk-bncc7heg.js +1 -0
- package/dist/{chunk-11cab5dq.css → chunk-cq2r11fg.css} +1 -1
- package/dist/chunk-cqt3s31j.js +546 -0
- package/dist/chunk-dcmks9sm.js +75 -0
- package/dist/chunk-md45evr2.css +1 -0
- package/dist/chunk-n0nab26d.js +1 -0
- package/dist/chunk-nf2wkvf9.js +14 -0
- package/dist/chunk-nmzhgyp9.js +2 -0
- package/dist/chunk-q37knr3m.js +2 -0
- package/dist/chunk-s90m4qkz.js +1 -0
- package/dist/chunk-wjvd4byz.js +2 -0
- package/dist/index.html +1 -1
- package/lib/harness-instructions.ts +1 -1
- package/lib/{app-icons.ts → identity-icons.ts} +6 -7
- package/lib/onboarding.ts +41 -0
- package/lib/speed-mode.ts +5 -0
- package/lib/types.ts +75 -64
- package/lib/view-build-directives.ts +1 -2
- package/lib/workspace-icon.ts +2 -2
- package/lib/workspace-layout.ts +1 -1
- package/lib/workspace-name.ts +15 -12
- package/package.json +11 -8
- package/server/README.md +27 -25
- package/server/api/harnesses.ts +31 -8
- package/server/api/index.ts +189 -127
- package/server/api/onboarding.ts +46 -0
- package/server/app-config.ts +16 -24
- package/server/app-settings.ts +6 -2
- package/server/applets/config.ts +5 -5
- package/server/applets/views.ts +19 -17
- package/server/cli/commands/init.ts +3 -3
- package/server/cli/commands/skill.ts +16 -17
- package/server/cli/commands/start.ts +8 -34
- package/server/cli/server.ts +18 -6
- package/server/collab/config.ts +1 -2
- package/server/collab/manager.ts +4 -1
- package/server/collab/skill/index.ts +1 -1
- package/server/control.ts +3 -3
- package/server/data-dir.ts +1 -1
- package/server/harness/README.md +24 -8
- package/server/harness/acp/session.ts +8 -4
- package/server/harness/auth.ts +14 -17
- package/server/harness/claude-code/NOTES.md +4 -3
- package/server/harness/claude-code/index.ts +5 -6
- package/server/harness/claude-code/mcp.ts +17 -0
- package/server/harness/claude-code/models.ts +2 -20
- package/server/harness/claude-code/run-action.ts +123 -0
- package/server/harness/claude-code/session-title.ts +5 -7
- package/server/harness/claude-code/session.ts +24 -16
- package/server/harness/codex/NOTES.md +22 -10
- package/server/harness/codex/adapter.ts +14 -17
- package/server/harness/codex/client.ts +8 -5
- package/server/harness/codex/connectors.ts +92 -0
- package/server/harness/codex/index.ts +4 -1
- package/server/harness/codex/pagination.ts +8 -5
- package/server/harness/codex/run-action.ts +93 -0
- package/server/harness/codex/session-title.ts +6 -8
- package/server/harness/codex/session.ts +11 -11
- package/server/harness/codex/transport.ts +13 -6
- package/server/harness/command.ts +45 -0
- package/server/harness/executable.ts +5 -0
- package/server/harness/fork.ts +6 -4
- package/server/harness/install.ts +49 -0
- package/server/harness/openclaw/session.ts +1 -2
- package/server/harness/run-action.ts +85 -0
- package/server/harness/session-title.ts +0 -46
- package/server/harness/types.ts +30 -3
- package/server/onboarding/brief.ts +29 -0
- package/server/onboarding/ideas.ts +48 -0
- package/server/onboarding/jobs.ts +291 -0
- package/server/onboarding/prompt.md +49 -0
- package/server/sessions/archive.ts +5 -14
- package/server/sessions/config.ts +17 -16
- package/server/sessions/lifecycle.ts +15 -13
- package/server/sessions/pending-views.ts +79 -108
- package/server/sessions/selected.ts +75 -161
- package/server/sessions/store.ts +40 -113
- package/server/sessions/view-sessions.ts +128 -111
- package/server/skills/locations.ts +82 -0
- package/server/skills/lock.ts +29 -0
- package/server/skills/template.ts +36 -25
- package/server/skills/update.ts +52 -5
- package/server/skills/version.ts +17 -22
- package/server/skills/workspace.ts +121 -0
- package/server/web.ts +22 -8
- package/server/workspace-state/file.ts +177 -0
- package/server/workspace-state/index.ts +162 -0
- package/server/workspace-state/legacy-migration.ts +79 -0
- package/server/workspace-state/lock.ts +42 -0
- package/server/workspace-state/types.ts +16 -0
- package/server/workspaces/change-harness.ts +88 -0
- package/server/workspaces/harness-transition.ts +29 -0
- package/server/workspaces/init.ts +29 -6
- package/server/workspaces/layout.ts +3 -0
- package/server/workspaces/registry.ts +27 -1
- package/{workspace/.claude/skills/moi-workspace → skill}/SKILL.md +7 -5
- package/{workspace/.claude/skills/moi-workspace → skill}/references/COLLAB.md +4 -8
- package/skill/references/DESIGN.md +167 -0
- package/{workspace/.claude/skills/moi-workspace → skill}/references/SCRATCHPAD.md +2 -2
- package/{workspace/.claude/skills/moi-workspace → skill}/references/UI-COMPONENTS.md +3 -0
- package/ui-components/badge.tsx +1 -1
- package/client/accept-hmr-dependencies.ts +0 -22
- package/client/features/settings/general-settings.tsx +0 -103
- package/client/features/workspace/setup/workspace-setup-panel.tsx +0 -18
- package/dist/chunk-20ynagd4.js +0 -2
- package/dist/chunk-42tde9a6.js +0 -1
- package/dist/chunk-9w543s0r.js +0 -2
- package/dist/chunk-a5372n61.js +0 -546
- package/dist/chunk-aja9y1vn.js +0 -68
- package/dist/chunk-fvcvrh3d.css +0 -1
- package/dist/chunk-gtfjw3nx.js +0 -1
- package/dist/chunk-j39y2aky.js +0 -14
- package/dist/chunk-pcnahct1.js +0 -5
- package/dist/chunk-pja30km3.js +0 -1
- package/dist/chunk-rm894bc2.js +0 -2
- package/dist/chunk-tevvtfbk.js +0 -10
- package/dist/chunk-tgt9zd0r.js +0 -2
- package/dist/chunk-tv5k5z00.js +0 -1
- package/dist/chunk-zq2ae5dx.js +0 -2
- package/lib/experimental.ts +0 -14
- package/workspace/.claude/skills/moi-workspace/references/DESIGN.md +0 -157
- /package/client/features/{settings → app-settings}/profile-settings.tsx +0 -0
- /package/client/features/{settings → app-settings}/settings-section.tsx +0 -0
- /package/client/features/{settings → app-settings}/users-settings.tsx +0 -0
- /package/client/features/{settings → workspace/settings}/environment-settings.tsx +0 -0
- /package/client/features/{settings → workspace/settings}/icon-picker-options.ts +0 -0
- /package/client/features/{settings → workspace/settings}/settings-layout.tsx +0 -0
- /package/client/features/{settings → workspace/settings}/workspace-settings-dialog.tsx +0 -0
- /package/{workspace/.claude/skills/moi-workspace → skill}/references/INTENTS.md +0 -0
|
@@ -5,28 +5,26 @@
|
|
|
5
5
|
import { cp, lstat, mkdir, readFile, readdir, readlink } from 'node:fs/promises'
|
|
6
6
|
import { join } from 'node:path'
|
|
7
7
|
|
|
8
|
-
//
|
|
9
|
-
|
|
10
|
-
export const TEMPLATE_DIR = join(import.meta.dir, '..', '..', 'workspace')
|
|
8
|
+
// Resolve relative to this file so source checkouts and published installs agree.
|
|
9
|
+
const BUNDLED_SKILL_DIR = join(import.meta.dir, '..', '..', 'skill')
|
|
11
10
|
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
11
|
+
export function bundledSkillDirectory(name: string): string {
|
|
12
|
+
if (name !== 'moi-workspace') throw new Error(`Unknown bundled skill: ${name}`)
|
|
13
|
+
return BUNDLED_SKILL_DIR
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export async function bundledSkillNames(): Promise<string[]> {
|
|
17
|
+
return ['moi-workspace']
|
|
18
|
+
}
|
|
16
19
|
|
|
17
|
-
export const COLLAB_REFERENCE_SOURCE_PATH = join(
|
|
18
|
-
BUNDLED_SKILLS_DIR,
|
|
19
|
-
'moi-workspace',
|
|
20
|
-
'references',
|
|
21
|
-
'COLLAB.md'
|
|
22
|
-
)
|
|
20
|
+
export const COLLAB_REFERENCE_SOURCE_PATH = join(BUNDLED_SKILL_DIR, 'references', 'COLLAB.md')
|
|
23
21
|
|
|
24
22
|
// The guide lives beside the other references, but only explicit collab init installs it.
|
|
25
23
|
function isDefaultSkillFile(source: string): boolean {
|
|
26
24
|
return source !== COLLAB_REFERENCE_SOURCE_PATH
|
|
27
25
|
}
|
|
28
26
|
|
|
29
|
-
async function matchesBundledPath(source: string, target: string): Promise<boolean> {
|
|
27
|
+
async function matchesBundledPath(source: string, target: string, exact = false): Promise<boolean> {
|
|
30
28
|
const sourceStat = await lstat(source)
|
|
31
29
|
const targetStat = await lstat(target).catch(() => null)
|
|
32
30
|
if (!targetStat) return false
|
|
@@ -34,8 +32,9 @@ async function matchesBundledPath(source: string, target: string): Promise<boole
|
|
|
34
32
|
if (sourceStat.isDirectory()) {
|
|
35
33
|
if (!targetStat.isDirectory()) return false
|
|
36
34
|
const entries = (await readdir(source)).filter(entry => isDefaultSkillFile(join(source, entry)))
|
|
35
|
+
if (exact && (await readdir(target)).length !== entries.length) return false
|
|
37
36
|
const matches = await Promise.all(
|
|
38
|
-
entries.map(entry => matchesBundledPath(join(source, entry), join(target, entry)))
|
|
37
|
+
entries.map(entry => matchesBundledPath(join(source, entry), join(target, entry), exact))
|
|
39
38
|
)
|
|
40
39
|
return matches.every(Boolean)
|
|
41
40
|
}
|
|
@@ -55,6 +54,10 @@ async function matchesBundledPath(source: string, target: string): Promise<boole
|
|
|
55
54
|
return false
|
|
56
55
|
}
|
|
57
56
|
|
|
57
|
+
export function matchesBundledSkill(name: string, target: string): Promise<boolean> {
|
|
58
|
+
return matchesBundledPath(bundledSkillDirectory(name), target, true)
|
|
59
|
+
}
|
|
60
|
+
|
|
58
61
|
// Copy changed shipped skill folders into `targetSkillsDir`. Leaves identical
|
|
59
62
|
// skills untouched, along with unrelated directories and extra local files.
|
|
60
63
|
// Creates the target if missing and returns the names whose bundled files changed.
|
|
@@ -66,17 +69,25 @@ async function matchesBundledPath(source: string, target: string): Promise<boole
|
|
|
66
69
|
export async function installBundledSkills(targetSkillsDir: string): Promise<string[]> {
|
|
67
70
|
await mkdir(targetSkillsDir, { recursive: true })
|
|
68
71
|
const changedSkills: string[] = []
|
|
69
|
-
const
|
|
70
|
-
|
|
71
|
-
for (const entry of skillEntries) {
|
|
72
|
-
if (!entry.isDirectory()) continue
|
|
73
|
-
const source = join(BUNDLED_SKILLS_DIR, entry.name)
|
|
74
|
-
const target = join(targetSkillsDir, entry.name)
|
|
75
|
-
if (await matchesBundledPath(source, target)) continue
|
|
76
|
-
|
|
77
|
-
await cp(source, target, { recursive: true, force: true, filter: isDefaultSkillFile })
|
|
78
|
-
changedSkills.push(entry.name)
|
|
72
|
+
for (const name of await bundledSkillNames()) {
|
|
73
|
+
if (await installBundledSkill(name, join(targetSkillsDir, name))) changedSkills.push(name)
|
|
79
74
|
}
|
|
80
75
|
|
|
81
76
|
return changedSkills
|
|
82
77
|
}
|
|
78
|
+
|
|
79
|
+
// Update one resolved destination without installing other skills beside it.
|
|
80
|
+
export async function installBundledSkill(name: string, target: string): Promise<boolean> {
|
|
81
|
+
const source = bundledSkillDirectory(name)
|
|
82
|
+
if (await matchesBundledPath(source, target)) return false
|
|
83
|
+
// Publish the version only after all references copied successfully, so a
|
|
84
|
+
// partial update cannot hide an outdated installation behind a fresh marker.
|
|
85
|
+
const marker = join(source, 'SKILL.md')
|
|
86
|
+
await cp(source, target, {
|
|
87
|
+
recursive: true,
|
|
88
|
+
force: true,
|
|
89
|
+
filter: path => path !== marker && isDefaultSkillFile(path)
|
|
90
|
+
})
|
|
91
|
+
await cp(marker, join(target, 'SKILL.md'), { force: true })
|
|
92
|
+
return true
|
|
93
|
+
}
|
package/server/skills/update.ts
CHANGED
|
@@ -1,15 +1,24 @@
|
|
|
1
|
+
import { basename } from 'node:path'
|
|
2
|
+
|
|
1
3
|
import type { WorkspaceSkillsStatus, WorkspaceSkillStatus, WorkspaceType } from '@/lib/types'
|
|
2
4
|
|
|
3
5
|
import type { AppletDeclarationFile } from '../applets/declarations'
|
|
4
6
|
import { syncAppletDeclarations } from '../applets/declarations'
|
|
5
|
-
import { isBehind, skillStatuses } from './version'
|
|
6
|
-
import {
|
|
7
|
-
import {
|
|
7
|
+
import { isBehind, skillStatuses, skillStatusesForLocations } from './version'
|
|
8
|
+
import { installBundledSkill } from './template'
|
|
9
|
+
import { resolveSkillLocations } from './locations'
|
|
10
|
+
import {
|
|
11
|
+
installWorkspaceSkillFiles,
|
|
12
|
+
usesSharedWorkspaceSkill,
|
|
13
|
+
workspaceSkillLockPath
|
|
14
|
+
} from './workspace'
|
|
15
|
+
import { withSkillLock } from './lock'
|
|
8
16
|
|
|
9
17
|
export type WorkspaceSkillUpdateResult = {
|
|
10
18
|
before: WorkspaceSkillStatus[]
|
|
11
19
|
status: WorkspaceSkillsStatus
|
|
12
20
|
changedSkills: string[]
|
|
21
|
+
changedSkillPaths: string[]
|
|
13
22
|
updatedAppletTypes: AppletDeclarationFile[]
|
|
14
23
|
}
|
|
15
24
|
|
|
@@ -31,8 +40,45 @@ export async function updateWorkspaceSkills(
|
|
|
31
40
|
workspaceRoot: string,
|
|
32
41
|
type: WorkspaceType = 'claude-code'
|
|
33
42
|
): Promise<WorkspaceSkillUpdateResult> {
|
|
34
|
-
|
|
35
|
-
|
|
43
|
+
return withSkillLock(workspaceSkillLockPath(workspaceRoot, type), () =>
|
|
44
|
+
updateWorkspaceSkillFiles(workspaceRoot, type)
|
|
45
|
+
)
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
async function updateWorkspaceSkillFiles(
|
|
49
|
+
workspaceRoot: string,
|
|
50
|
+
type: WorkspaceType
|
|
51
|
+
): Promise<WorkspaceSkillUpdateResult> {
|
|
52
|
+
const locations = await resolveSkillLocations(workspaceRoot, type)
|
|
53
|
+
const before = await skillStatusesForLocations(locations)
|
|
54
|
+
let changedSkillPaths: string[]
|
|
55
|
+
if (usesSharedWorkspaceSkill(type)) {
|
|
56
|
+
changedSkillPaths = await installWorkspaceSkillFiles(workspaceRoot, type)
|
|
57
|
+
} else {
|
|
58
|
+
for (const location of locations) {
|
|
59
|
+
if (location.brokenLink) {
|
|
60
|
+
throw new Error(
|
|
61
|
+
`Cannot update skill at ${location.path}: broken symlink ${location.brokenLink}`
|
|
62
|
+
)
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
const changedTargets = new Map<string, boolean>()
|
|
66
|
+
for (const location of locations) {
|
|
67
|
+
if (changedTargets.has(location.targetPath)) continue
|
|
68
|
+
try {
|
|
69
|
+
changedTargets.set(
|
|
70
|
+
location.targetPath,
|
|
71
|
+
await installBundledSkill(location.name, location.targetPath)
|
|
72
|
+
)
|
|
73
|
+
} catch (cause) {
|
|
74
|
+
throw new Error(`Cannot update skill at ${location.path}: ${String(cause)}`, { cause })
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
changedSkillPaths = locations
|
|
78
|
+
.filter(location => changedTargets.get(location.targetPath))
|
|
79
|
+
.map(location => location.path)
|
|
80
|
+
}
|
|
81
|
+
const changedSkills = [...new Set(changedSkillPaths.map(path => basename(path)))]
|
|
36
82
|
// Applet declarations ship with the CLI, so refresh installed copies along
|
|
37
83
|
// with skills. The optional collab declaration is only updated if present.
|
|
38
84
|
const updatedAppletTypes = await syncAppletDeclarations(workspaceRoot)
|
|
@@ -42,6 +88,7 @@ export async function updateWorkspaceSkills(
|
|
|
42
88
|
before,
|
|
43
89
|
status: summarizeSkillStatuses(skills),
|
|
44
90
|
changedSkills,
|
|
91
|
+
changedSkillPaths,
|
|
45
92
|
updatedAppletTypes
|
|
46
93
|
}
|
|
47
94
|
}
|
package/server/skills/version.ts
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
// Skill versioning for a workspace's installed skill copy drifting behind the
|
|
2
2
|
// version this moi installation ships. The marker in each skill's `SKILL.md`
|
|
3
3
|
// drives both the CLI status and the frontend's automatic update check.
|
|
4
|
-
import { readdir } from 'node:fs/promises'
|
|
5
4
|
import { join } from 'node:path'
|
|
6
5
|
|
|
7
6
|
import type { WorkspaceSkillStatus, WorkspaceType } from '@/lib/types'
|
|
8
7
|
|
|
9
8
|
import { findWorkspaceForPath, liftToWorkspaceRoot, listWorkspaces } from '../workspaces/registry'
|
|
10
|
-
import {
|
|
11
|
-
import {
|
|
9
|
+
import { bundledSkillDirectory } from './template'
|
|
10
|
+
import { displaySkillPath, resolveSkillLocations } from './locations'
|
|
11
|
+
import type { SkillLocation } from './locations'
|
|
12
12
|
|
|
13
13
|
// The version is stamped as a self-contained marker in SKILL.md, e.g.
|
|
14
14
|
// `<moi-skill version="0.1.0" />`, rather than in YAML frontmatter. SKILL.md
|
|
@@ -71,30 +71,23 @@ export const isMinorBehind = (installed: string | null, bundled: string | null):
|
|
|
71
71
|
export const isBehind = (installed: string | null, bundled: string | null): boolean =>
|
|
72
72
|
compare(installed, bundled).behind
|
|
73
73
|
|
|
74
|
-
//
|
|
75
|
-
export async function bundledSkillNames(): Promise<string[]> {
|
|
76
|
-
try {
|
|
77
|
-
const entries = await readdir(BUNDLED_SKILLS_DIR, { withFileTypes: true })
|
|
78
|
-
return entries.filter(e => e.isDirectory()).map(e => e.name)
|
|
79
|
-
} catch {
|
|
80
|
-
return []
|
|
81
|
-
}
|
|
82
|
-
}
|
|
83
|
-
|
|
84
|
-
// Per-skill installed-vs-bundled versions for a workspace root. `type` picks
|
|
85
|
-
// the installed-skills location (OpenClaw keeps them in `skills/`, not
|
|
86
|
-
// `.claude/skills/`) — pass the registry entry's type when known.
|
|
74
|
+
// Report every installed copy, plus the required primary harness destination.
|
|
87
75
|
export async function skillStatuses(
|
|
88
76
|
workspaceRoot: string,
|
|
89
77
|
type?: WorkspaceType
|
|
90
78
|
): Promise<WorkspaceSkillStatus[]> {
|
|
91
|
-
|
|
92
|
-
|
|
79
|
+
return skillStatusesForLocations(await resolveSkillLocations(workspaceRoot, type))
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
export async function skillStatusesForLocations(
|
|
83
|
+
locations: SkillLocation[]
|
|
84
|
+
): Promise<WorkspaceSkillStatus[]> {
|
|
93
85
|
return Promise.all(
|
|
94
|
-
|
|
86
|
+
locations.map(async ({ name, path, brokenLink }) => ({
|
|
95
87
|
name,
|
|
96
|
-
|
|
97
|
-
|
|
88
|
+
path,
|
|
89
|
+
bundled: await readSkillVersion(join(bundledSkillDirectory(name), 'SKILL.md')),
|
|
90
|
+
installed: brokenLink ? null : await readSkillVersion(join(path, 'SKILL.md'))
|
|
98
91
|
}))
|
|
99
92
|
)
|
|
100
93
|
}
|
|
@@ -126,7 +119,9 @@ export async function staleSkillNotice(cwd: string): Promise<string | null> {
|
|
|
126
119
|
isMinorBehind(s.installed, s.bundled)
|
|
127
120
|
)
|
|
128
121
|
if (stale.length === 0) return null
|
|
129
|
-
const parts = stale.map(
|
|
122
|
+
const parts = stale.map(
|
|
123
|
+
s => `${displaySkillPath(root, s.path)} (${s.installed ?? 'none'} → ${s.bundled})`
|
|
124
|
+
)
|
|
130
125
|
return `⚠ Moi skill outdated: ${parts.join(', ')} — run \`moi skill update\` to refresh.`
|
|
131
126
|
} catch {
|
|
132
127
|
return null
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import { lstat, mkdir, mkdtemp, readlink, realpath, rename, rm, symlink } from 'node:fs/promises'
|
|
2
|
+
import { dirname, join, relative, resolve } from 'node:path'
|
|
3
|
+
|
|
4
|
+
import type { WorkspaceType } from '@/lib/types'
|
|
5
|
+
|
|
6
|
+
import { installBundledSkill, installBundledSkills, matchesBundledSkill } from './template'
|
|
7
|
+
import { skillsDirFor } from '../workspaces/init'
|
|
8
|
+
import { withSkillLock } from './lock'
|
|
9
|
+
|
|
10
|
+
const SKILL_NAME = 'moi-workspace'
|
|
11
|
+
|
|
12
|
+
export function usesSharedWorkspaceSkill(type?: WorkspaceType): boolean {
|
|
13
|
+
return type === undefined || type === 'claude-code' || type === 'codex'
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
async function entryAt(path: string) {
|
|
17
|
+
return lstat(path).catch((error: unknown) => {
|
|
18
|
+
if ((error as NodeJS.ErrnoException).code === 'ENOENT') return undefined
|
|
19
|
+
throw error
|
|
20
|
+
})
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
// Replacing a whole-folder link would also change unrelated skills.
|
|
24
|
+
async function checkSkillsDirectory(path: string): Promise<void> {
|
|
25
|
+
if ((await entryAt(path))?.isSymbolicLink()) {
|
|
26
|
+
throw new Error(
|
|
27
|
+
`Cannot install workspace skill: ${path} is a symlink; use a real skills directory`
|
|
28
|
+
)
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export async function installWorkspaceSkills(
|
|
33
|
+
root: string,
|
|
34
|
+
type?: WorkspaceType
|
|
35
|
+
): Promise<string[]> {
|
|
36
|
+
return withSkillLock(workspaceSkillLockPath(root, type), () =>
|
|
37
|
+
installWorkspaceSkillFiles(root, type)
|
|
38
|
+
)
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
export function workspaceSkillLockPath(root: string, type?: WorkspaceType): string {
|
|
42
|
+
return usesSharedWorkspaceSkill(type) ? join(root, '.agents', 'skills') : skillsDirFor(root, type)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
// Callers must hold the workspace skill lock across any surrounding status reads.
|
|
46
|
+
export async function installWorkspaceSkillFiles(
|
|
47
|
+
root: string,
|
|
48
|
+
type?: WorkspaceType
|
|
49
|
+
): Promise<string[]> {
|
|
50
|
+
if (usesSharedWorkspaceSkill(type)) return resetWorkspaceSkill(root)
|
|
51
|
+
const directory = skillsDirFor(root, type)
|
|
52
|
+
return (await installBundledSkills(directory)).map(name => join(directory, name))
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Both init and updates reset the owned skill. Local additions (including the
|
|
56
|
+
// experimental collab guide) are intentionally discarded; sibling skills stay.
|
|
57
|
+
async function resetWorkspaceSkill(workspaceRoot: string): Promise<string[]> {
|
|
58
|
+
const root = resolve(workspaceRoot)
|
|
59
|
+
const canonical = join(root, '.agents', 'skills', SKILL_NAME)
|
|
60
|
+
const claude = join(root, '.claude', 'skills', SKILL_NAME)
|
|
61
|
+
try {
|
|
62
|
+
await checkSkillsDirectory(dirname(canonical))
|
|
63
|
+
await checkSkillsDirectory(dirname(claude))
|
|
64
|
+
await mkdir(dirname(canonical), { recursive: true })
|
|
65
|
+
await mkdir(dirname(claude), { recursive: true })
|
|
66
|
+
const sharedDirectory =
|
|
67
|
+
(await realpath(dirname(canonical))) === (await realpath(dirname(claude)))
|
|
68
|
+
if (!sharedDirectory && (await entryAt(join(root, '.claude')))?.isSymbolicLink()) {
|
|
69
|
+
throw new Error(
|
|
70
|
+
'The .claude directory is a symlink; point it to .agents or use a real .claude directory'
|
|
71
|
+
)
|
|
72
|
+
}
|
|
73
|
+
const linkTarget = relative(dirname(claude), canonical)
|
|
74
|
+
const claudeEntry = await entryAt(claude)
|
|
75
|
+
const linked =
|
|
76
|
+
sharedDirectory || (claudeEntry?.isSymbolicLink() && (await readlink(claude)) === linkTarget)
|
|
77
|
+
const matches = await matchesBundledSkill(SKILL_NAME, canonical)
|
|
78
|
+
if (matches && linked) return []
|
|
79
|
+
|
|
80
|
+
// Prepare the new files and link before removing either old skill. A copy
|
|
81
|
+
// or symlink-permission failure leaves the installed copies available.
|
|
82
|
+
const staging = await mkdtemp(join(dirname(canonical), '.moi-workspace-'))
|
|
83
|
+
try {
|
|
84
|
+
const skill = join(staging, SKILL_NAME)
|
|
85
|
+
const link = join(staging, 'claude-link')
|
|
86
|
+
if (!matches) await installBundledSkill(SKILL_NAME, skill)
|
|
87
|
+
if (!linked) {
|
|
88
|
+
try {
|
|
89
|
+
await symlink(linkTarget, link, 'dir')
|
|
90
|
+
} catch (cause) {
|
|
91
|
+
const code = (cause as NodeJS.ErrnoException).code
|
|
92
|
+
if (process.platform === 'win32' && (code === 'EPERM' || code === 'EACCES')) {
|
|
93
|
+
throw new Error(
|
|
94
|
+
'Windows could not create the workspace skill symlink; enable Developer Mode or run Moi as administrator, then retry',
|
|
95
|
+
{ cause }
|
|
96
|
+
)
|
|
97
|
+
}
|
|
98
|
+
throw cause
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (!matches) {
|
|
102
|
+
await rm(canonical, { recursive: true, force: true })
|
|
103
|
+
await rename(skill, canonical)
|
|
104
|
+
}
|
|
105
|
+
if (!linked) {
|
|
106
|
+
await rm(claude, { recursive: true, force: true })
|
|
107
|
+
await rename(link, claude)
|
|
108
|
+
}
|
|
109
|
+
} finally {
|
|
110
|
+
await rm(staging, { recursive: true, force: true })
|
|
111
|
+
}
|
|
112
|
+
return matches ? [claude] : [canonical, claude]
|
|
113
|
+
} catch (cause) {
|
|
114
|
+
throw new Error(
|
|
115
|
+
`Cannot reset workspace skill at ${canonical} and ${claude}: ${String(cause)}`,
|
|
116
|
+
{
|
|
117
|
+
cause
|
|
118
|
+
}
|
|
119
|
+
)
|
|
120
|
+
}
|
|
121
|
+
}
|
package/server/web.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { isSpeedMode } from '@/lib/speed-mode'
|
|
1
2
|
import type { UserProfile } from '@/lib/collab/types'
|
|
2
3
|
import { isMessageAttachments } from '@/lib/message-attachments'
|
|
3
4
|
import type { ClientMessage, StatusSnapshotMessage } from '@/lib/types'
|
|
@@ -10,7 +11,6 @@ import devSandbox from '../client/dev/index.html'
|
|
|
10
11
|
import { api } from './api'
|
|
11
12
|
import { getAppConfig } from './app-config'
|
|
12
13
|
import { harnessStore } from './harness/auth'
|
|
13
|
-
import { AttachmentUploadError } from './sessions/attachment-message'
|
|
14
14
|
import { PORT } from './constants'
|
|
15
15
|
import { control } from './control'
|
|
16
16
|
import { proxyUserState } from './collab/cloudflare-access'
|
|
@@ -25,7 +25,8 @@ import { resolveScratchOp } from './scratchpad/relay'
|
|
|
25
25
|
import { allHarnesses, harnessFor } from './harness/registry'
|
|
26
26
|
import { getWorkspace, listWorkspaces } from './workspaces/registry'
|
|
27
27
|
import { failInterruptedViewStarts, sendWorkspaceMessage } from './sessions/view-sessions'
|
|
28
|
-
import {
|
|
28
|
+
import { prepareWorkspaceStates } from './workspace-state'
|
|
29
|
+
import { migrateLegacyWorkspaceState } from './workspace-state/legacy-migration'
|
|
29
30
|
import {
|
|
30
31
|
addClient,
|
|
31
32
|
broadcast,
|
|
@@ -59,7 +60,7 @@ function isClientMessage(value: unknown): value is ClientMessage {
|
|
|
59
60
|
optimisticId?: unknown
|
|
60
61
|
model?: unknown
|
|
61
62
|
effort?: unknown
|
|
62
|
-
|
|
63
|
+
speedMode?: unknown
|
|
63
64
|
stream?: unknown
|
|
64
65
|
context?: unknown
|
|
65
66
|
attachments?: unknown
|
|
@@ -77,7 +78,7 @@ function isClientMessage(value: unknown): value is ClientMessage {
|
|
|
77
78
|
(v.optimisticId === undefined || typeof v.optimisticId === 'string') &&
|
|
78
79
|
(v.model === undefined || typeof v.model === 'string') &&
|
|
79
80
|
(v.effort === undefined || typeof v.effort === 'string') &&
|
|
80
|
-
(v.
|
|
81
|
+
(v.speedMode === undefined || isSpeedMode(v.speedMode)) &&
|
|
81
82
|
(v.stream === undefined || typeof v.stream === 'boolean') &&
|
|
82
83
|
(v.context === undefined || isMoiContext(v.context)) &&
|
|
83
84
|
(v.attachments === undefined || isMessageAttachments(v.attachments))
|
|
@@ -110,7 +111,10 @@ function upgrade(server: Upgradable, req: Request, data: WsData) {
|
|
|
110
111
|
// Bun owns the fullstack surface: the HTML shell + dev bundler/HMR, and the two
|
|
111
112
|
// WebSocket channels (which need Bun's native `server.upgrade` + pub/sub). Every
|
|
112
113
|
// HTTP API route is delegated to the Hono app (`./api`) via `fetch`.
|
|
113
|
-
|
|
114
|
+
const registeredWorkspaces = await listWorkspaces()
|
|
115
|
+
await migrateLegacyWorkspaceState(registeredWorkspaces)
|
|
116
|
+
await prepareWorkspaceStates(registeredWorkspaces)
|
|
117
|
+
await Promise.all(registeredWorkspaces.map(failInterruptedViewStarts))
|
|
114
118
|
|
|
115
119
|
// Managed machines serve the web side on a Unix socket file that only the
|
|
116
120
|
// machine's gateway can open, so nothing else running there reaches the API
|
|
@@ -158,6 +162,7 @@ export const app = Bun.serve<WsData>({
|
|
|
158
162
|
: devSandbox,
|
|
159
163
|
'/workspace/*': shell,
|
|
160
164
|
'/settings': shell,
|
|
165
|
+
'/onboarding': shell,
|
|
161
166
|
|
|
162
167
|
// Chat websocket — app-wide (one per client, not per workspace). Each chat
|
|
163
168
|
// frame carries its own workspaceId.
|
|
@@ -192,7 +197,16 @@ export const app = Bun.serve<WsData>({
|
|
|
192
197
|
},
|
|
193
198
|
// Anything not matched above (the whole HTTP API + prod static assets + 404)
|
|
194
199
|
// is handled by Hono.
|
|
195
|
-
fetch: req =>
|
|
200
|
+
fetch: (req, server) => {
|
|
201
|
+
// Downloads can exceed the normal HTTP idle timeout; the installer has its own deadline.
|
|
202
|
+
if (
|
|
203
|
+
req.method === 'POST' &&
|
|
204
|
+
/^\/api\/harnesses\/(claude-code|codex)\/auth\/install$/.test(new URL(req.url).pathname)
|
|
205
|
+
) {
|
|
206
|
+
server.timeout(req, 0)
|
|
207
|
+
}
|
|
208
|
+
return api.fetch(req)
|
|
209
|
+
},
|
|
196
210
|
websocket: {
|
|
197
211
|
open(ws) {
|
|
198
212
|
if (ws.data.channel === 'collab') {
|
|
@@ -239,12 +253,12 @@ export const app = Bun.serve<WsData>({
|
|
|
239
253
|
optimisticId: data.optimisticId,
|
|
240
254
|
model: data.model,
|
|
241
255
|
effort: data.effort,
|
|
242
|
-
|
|
256
|
+
speedMode: data.speedMode,
|
|
243
257
|
stream: data.stream,
|
|
244
258
|
context: data.context,
|
|
245
259
|
agentId: workspace.agentId
|
|
246
260
|
}).catch(error => {
|
|
247
|
-
if (error instanceof
|
|
261
|
+
if (error instanceof Error) {
|
|
248
262
|
broadcast(workspace.id, {
|
|
249
263
|
kind: 'error',
|
|
250
264
|
sessionId: data.sessionId,
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { isSpeedMode } from '@/lib/speed-mode'
|
|
2
|
+
import { mkdir, rename, rm } from 'node:fs/promises'
|
|
3
|
+
import { dirname } from 'node:path'
|
|
4
|
+
|
|
5
|
+
import type { PendingView, SessionFork, WorkspaceSessionState } from '@/lib/types'
|
|
6
|
+
import { isWorkspaceTabId } from '@/lib/workspace-tabs'
|
|
7
|
+
import { LockTimeoutError, type Lease } from './lock'
|
|
8
|
+
import { WORKSPACE_STATE_VERSION, type StoredWorkspaceState } from './types'
|
|
9
|
+
|
|
10
|
+
type PendingViewState = Omit<PendingView, 'id'>
|
|
11
|
+
|
|
12
|
+
export type LoadedState = {
|
|
13
|
+
state: StoredWorkspaceState
|
|
14
|
+
// False for a file written by a newer Moi: read what we understand, never
|
|
15
|
+
// overwrite what we don't.
|
|
16
|
+
writable: boolean
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export class WorkspaceStateVersionError extends Error {
|
|
20
|
+
constructor(path: string) {
|
|
21
|
+
super(`${path} was written by a newer version of Moi. Update Moi to change this workspace.`)
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
const PENDING_VIEW_STATUSES = new Set<string>(['draft', 'starting', 'submitted', 'failed'])
|
|
26
|
+
|
|
27
|
+
export function emptyWorkspaceState(workspacePath: string): StoredWorkspaceState {
|
|
28
|
+
return {
|
|
29
|
+
version: WORKSPACE_STATE_VERSION,
|
|
30
|
+
workspacePath,
|
|
31
|
+
revision: 0,
|
|
32
|
+
selected: {},
|
|
33
|
+
sessions: {},
|
|
34
|
+
pendingViews: {}
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function isRecord(value: unknown): value is Record<string, unknown> {
|
|
39
|
+
return typeof value === 'object' && value !== null && !Array.isArray(value)
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function optionalString(value: unknown): string | undefined {
|
|
43
|
+
return typeof value === 'string' ? value : undefined
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
function normalizeFork(value: unknown): SessionFork | undefined {
|
|
47
|
+
if (!isRecord(value) || typeof value.fromSessionId !== 'string') return undefined
|
|
48
|
+
const noticeIds = Array.isArray(value.noticeIds)
|
|
49
|
+
? value.noticeIds.filter((id): id is string => typeof id === 'string')
|
|
50
|
+
: undefined
|
|
51
|
+
return {
|
|
52
|
+
fromSessionId: value.fromSessionId,
|
|
53
|
+
...(typeof value.throughMessageId === 'string'
|
|
54
|
+
? { throughMessageId: value.throughMessageId }
|
|
55
|
+
: {}),
|
|
56
|
+
...(noticeIds?.length ? { noticeIds } : {})
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
function normalizeSession(value: unknown): WorkspaceSessionState {
|
|
61
|
+
if (!isRecord(value)) return {}
|
|
62
|
+
const session: WorkspaceSessionState = {}
|
|
63
|
+
if (isWorkspaceTabId(value.tabId)) session.tabId = value.tabId
|
|
64
|
+
if (isRecord(value.config)) {
|
|
65
|
+
const { model, effort, speedMode } = value.config
|
|
66
|
+
const config = {
|
|
67
|
+
...(typeof model === 'string' ? { model } : {}),
|
|
68
|
+
...(typeof effort === 'string' ? { effort } : {}),
|
|
69
|
+
...(isSpeedMode(speedMode) ? { speedMode } : {})
|
|
70
|
+
}
|
|
71
|
+
if (Object.keys(config).length) session.config = config
|
|
72
|
+
}
|
|
73
|
+
const fork = normalizeFork(value.fork)
|
|
74
|
+
if (fork) session.fork = fork
|
|
75
|
+
return session
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
function normalizePendingView(value: unknown): PendingViewState | undefined {
|
|
79
|
+
if (!isRecord(value) || !PENDING_VIEW_STATUSES.has(String(value.status))) return undefined
|
|
80
|
+
const view: PendingViewState = {
|
|
81
|
+
status: value.status as PendingViewState['status'],
|
|
82
|
+
requirements: optionalString(value.requirements) ?? ''
|
|
83
|
+
}
|
|
84
|
+
for (const key of ['title', 'icon', 'buildSessionId', 'error'] as const) {
|
|
85
|
+
const field = optionalString(value[key])
|
|
86
|
+
if (field !== undefined) view[key] = field
|
|
87
|
+
}
|
|
88
|
+
return view
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// Keep every entry we can make sense of and drop the rest, so one bad field
|
|
92
|
+
// never costs the whole workspace its state.
|
|
93
|
+
export function normalizeWorkspaceState(
|
|
94
|
+
value: Record<string, unknown>,
|
|
95
|
+
workspacePath: string
|
|
96
|
+
): StoredWorkspaceState {
|
|
97
|
+
const state = emptyWorkspaceState(optionalString(value.workspacePath) ?? workspacePath)
|
|
98
|
+
if (typeof value.revision === 'number' && Number.isSafeInteger(value.revision))
|
|
99
|
+
state.revision = value.revision
|
|
100
|
+
const pinned = optionalString(value.pinnedSessionId)
|
|
101
|
+
if (pinned) state.pinnedSessionId = pinned
|
|
102
|
+
if (isRecord(value.selected)) {
|
|
103
|
+
for (const [tabId, sessionId] of Object.entries(value.selected)) {
|
|
104
|
+
if (isWorkspaceTabId(tabId) && typeof sessionId === 'string')
|
|
105
|
+
state.selected[tabId] = sessionId
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
if (isRecord(value.sessions)) {
|
|
109
|
+
for (const [sessionId, session] of Object.entries(value.sessions))
|
|
110
|
+
state.sessions[sessionId] = normalizeSession(session)
|
|
111
|
+
}
|
|
112
|
+
if (isRecord(value.pendingViews)) {
|
|
113
|
+
for (const [viewId, view] of Object.entries(value.pendingViews)) {
|
|
114
|
+
const normalized = normalizePendingView(view)
|
|
115
|
+
if (normalized) state.pendingViews[viewId] = normalized
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
return compactWorkspaceState(state)
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
// Empty session entries and settings carry no information; leaving them out
|
|
122
|
+
// keeps the file readable.
|
|
123
|
+
export function compactWorkspaceState<T extends StoredWorkspaceState>(state: T): T {
|
|
124
|
+
for (const [sessionId, session] of Object.entries(state.sessions)) {
|
|
125
|
+
for (const key of Object.keys(session) as (keyof WorkspaceSessionState)[])
|
|
126
|
+
if (session[key] === undefined) delete session[key]
|
|
127
|
+
if (session.config && Object.keys(session.config).length === 0) delete session.config
|
|
128
|
+
if (Object.keys(session).length === 0) delete state.sessions[sessionId]
|
|
129
|
+
}
|
|
130
|
+
if (!state.pinnedSessionId) delete state.pinnedSessionId
|
|
131
|
+
return state
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
async function setAside(path: string, reason: string): Promise<undefined> {
|
|
135
|
+
const aside = `${path}.corrupt-${Date.now()}`
|
|
136
|
+
await rename(path, aside).catch(() => {})
|
|
137
|
+
console.error(`[workspace-state] ${reason}; moved ${path} to ${aside} and started empty`)
|
|
138
|
+
return undefined
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
// Reads one state file. A missing file reads as undefined. A damaged file is
|
|
142
|
+
// moved aside instead of being overwritten by the next write.
|
|
143
|
+
export async function readStateFile(
|
|
144
|
+
path: string,
|
|
145
|
+
workspacePath: string
|
|
146
|
+
): Promise<LoadedState | undefined> {
|
|
147
|
+
const file = Bun.file(path)
|
|
148
|
+
if (!(await file.exists())) return undefined
|
|
149
|
+
let parsed: unknown
|
|
150
|
+
try {
|
|
151
|
+
parsed = JSON.parse(await file.text())
|
|
152
|
+
} catch {
|
|
153
|
+
return setAside(path, 'Unreadable JSON')
|
|
154
|
+
}
|
|
155
|
+
if (!isRecord(parsed) || typeof parsed.version !== 'number')
|
|
156
|
+
return setAside(path, 'Not a workspace state file')
|
|
157
|
+
const writable = parsed.version <= WORKSPACE_STATE_VERSION
|
|
158
|
+
if (!writable) console.warn(`[workspace-state] ${new WorkspaceStateVersionError(path).message}`)
|
|
159
|
+
return { state: normalizeWorkspaceState(parsed, workspacePath), writable }
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
let tempSequence = 0
|
|
163
|
+
|
|
164
|
+
// Write to a sibling temp file, then rename over the target: readers see the
|
|
165
|
+
// old file or the new one, never a partial write. A holder whose lock expired
|
|
166
|
+
// gives up before the rename, so a late write cannot replace a newer one.
|
|
167
|
+
export async function writeStateFile(path: string, state: StoredWorkspaceState, lease: Lease) {
|
|
168
|
+
await mkdir(dirname(path), { recursive: true })
|
|
169
|
+
const temp = `${path}.${process.pid}.${++tempSequence}.tmp`
|
|
170
|
+
try {
|
|
171
|
+
await Bun.write(temp, `${JSON.stringify(state, null, 2)}\n`)
|
|
172
|
+
if (!lease.isCurrent()) throw new LockTimeoutError(path)
|
|
173
|
+
await rename(temp, path)
|
|
174
|
+
} finally {
|
|
175
|
+
await rm(temp, { force: true })
|
|
176
|
+
}
|
|
177
|
+
}
|