moi-computer 0.11.0 → 0.11.1-next.1
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/client/AGENTS.md +2 -2
- package/client/components/ui/button.tsx +1 -53
- package/client/components/ui/collapsible.tsx +1 -15
- package/client/components/ui/dialog.tsx +1 -58
- package/client/components/ui/dropdown-menu.tsx +1 -254
- package/client/components/ui/hover-card.tsx +1 -44
- package/client/components/ui/input.tsx +1 -21
- package/client/components/ui/popover.tsx +1 -88
- package/client/components/ui/resizable.tsx +1 -40
- package/client/components/ui/skeleton.tsx +1 -13
- package/client/components/ui/slider.tsx +1 -70
- package/client/components/ui/spinner.tsx +1 -18
- package/client/components/ui/switch.tsx +1 -23
- package/client/components/ui/tabs.tsx +1 -0
- package/client/components/ui/textarea.tsx +1 -18
- package/client/components/ui/tooltip.tsx +1 -51
- package/client/features/chat/ChatPanel.tsx +6 -6
- package/client/features/chat/ChatPopup.tsx +3 -4
- package/client/features/chat/TurnView.tsx +5 -4
- package/client/features/chat/api.ts +5 -14
- package/client/features/chat/chat-frames.ts +5 -2
- package/client/features/chat/composer/ChatComposer.tsx +5 -1
- package/client/features/chat/composer/ModelPicker.tsx +4 -3
- package/client/features/chat/composer/banners/ErrorBanner.tsx +20 -12
- package/client/features/chat/composer/useWorkspaceComposerState.tsx +15 -7
- package/client/features/chat/group-turns.ts +11 -0
- package/client/features/chat/session-view.ts +53 -0
- package/client/features/chat/useChat.ts +5 -1
- package/client/features/dev/ChatStatesPage.tsx +3 -1
- package/client/features/dev/DevIndexPage.tsx +5 -0
- package/client/features/dev/DevRoutes.tsx +2 -0
- package/client/features/dev/HarnessDebugPage.tsx +1 -1
- package/client/features/dev/UiComponentPreviews.tsx +834 -0
- package/client/features/dev/UiComponentsPage.tsx +60 -0
- package/client/features/home/WorkspacePreview.tsx +1 -1
- package/client/features/home/workspace-setup/DemoDialog.tsx +3 -2
- package/client/features/home/workspace-setup/ImportWorkspaceDialog.tsx +5 -8
- package/client/features/home/workspace-setup/WorkspaceDialogContent.tsx +7 -20
- package/client/features/settings/SettingsLayout.tsx +1 -1
- package/client/features/settings/WorkspaceSettings.tsx +4 -1
- package/client/features/views/ViewManager.tsx +14 -9
- package/client/features/widgets/WidgetFrame.tsx +1 -1
- package/client/features/widgets/Widgets.tsx +1 -3
- package/client/features/workspace/WorkspaceScreen.tsx +5 -1
- package/client/features/workspace/WorkspaceSplitLayout.tsx +6 -1
- package/client/features/workspace/api.ts +2 -4
- package/client/index.css +22 -29
- package/dist/{chunk-tny85yt2.js → chunk-4s15axjs.js} +2 -2
- package/dist/{chunk-c9pjqx9m.js → chunk-53a7pxcz.js} +1 -1
- package/dist/chunk-6664vxpk.css +1 -0
- package/dist/{chunk-104m57qw.js → chunk-87thzbtw.js} +2 -2
- package/dist/chunk-a6jad1a6.js +1 -0
- package/dist/chunk-dghwstd2.js +9 -0
- package/dist/chunk-eg120aca.js +1 -0
- package/dist/{chunk-v7wr1df4.js → chunk-j3wvxjr7.js} +1 -1
- package/dist/chunk-vwjeezeb.js +1 -0
- package/dist/chunk-w482ew2c.js +546 -0
- package/dist/chunk-yc471zah.js +70 -0
- package/dist/index.html +1 -1
- package/lib/format.ts +49 -1
- package/lib/themes.ts +6 -6
- package/lib/types.ts +5 -7
- package/package.json +17 -7
- package/registry.json +643 -0
- package/server/AGENTS.md +1 -0
- package/server/agent.ts +1 -1
- package/server/api.ts +25 -29
- package/server/applets.ts +30 -29
- package/server/bundler/applet-css.ts +6 -1
- package/server/bundler/build-applet.ts +5 -1
- package/server/bundler/build-worker.ts +178 -0
- package/server/cli-ui-components.ts +28 -33
- package/server/cli.ts +6 -11
- package/server/data-dir.ts +1 -1
- package/server/harness/README.md +13 -9
- package/server/harness/acp/NOTES.md +426 -0
- package/server/harness/acp/discovery.ts +27 -30
- package/server/harness/acp/model-state.ts +57 -0
- package/server/harness/acp/session.ts +18 -6
- package/server/harness/claude-code/NOTES.md +47 -12
- package/server/harness/claude-code/cli.ts +40 -0
- package/server/harness/claude-code/index.ts +11 -5
- package/server/harness/claude-code/models.ts +72 -19
- package/server/harness/claude-code/session.ts +408 -299
- package/server/harness/hermes/NOTES.md +18 -0
- package/server/harness/hermes/discovery.ts +20 -0
- package/server/harness/hermes/index.ts +9 -0
- package/server/harness/hermes/models.ts +28 -8
- package/server/harness/types.ts +4 -2
- package/server/moi-scaffold.ts +1 -1
- package/server/preview.ts +3 -1
- package/server/registry.ts +63 -41
- package/server/scratchpad-executor.ts +79 -56
- package/server/scratchpad-shape-defaults.ts +82 -0
- package/server/tldraw-package.d.ts +4 -0
- package/server/ui-components.ts +101 -434
- package/server/view-builders.ts +2 -1
- package/server/web.ts +9 -5
- package/server/workspace-env.ts +3 -2
- package/tsconfig.json +0 -1
- package/ui-components/accordion.tsx +71 -0
- package/ui-components/alert-dialog.tsx +165 -0
- package/ui-components/alert.tsx +68 -0
- package/ui-components/attachment.tsx +195 -0
- package/ui-components/avatar.tsx +90 -0
- package/ui-components/badge.tsx +49 -0
- package/ui-components/button-group.tsx +78 -0
- package/ui-components/button.tsx +55 -0
- package/ui-components/calendar.tsx +180 -0
- package/ui-components/card.tsx +84 -0
- package/ui-components/carousel.tsx +235 -0
- package/ui-components/chart.tsx +338 -0
- package/ui-components/checkbox.tsx +27 -0
- package/ui-components/collapsible.tsx +15 -0
- package/ui-components/combobox.tsx +265 -0
- package/ui-components/context-menu.tsx +244 -0
- package/ui-components/dialog.tsx +138 -0
- package/ui-components/docs/accordion.md +463 -0
- package/ui-components/docs/alert-dialog.md +481 -0
- package/ui-components/docs/alert.md +301 -0
- package/ui-components/docs/attachment.md +689 -0
- package/ui-components/docs/avatar.md +495 -0
- package/ui-components/docs/badge.md +258 -0
- package/ui-components/docs/button-group.md +879 -0
- package/ui-components/docs/button.md +26 -0
- package/ui-components/docs/calendar.md +862 -0
- package/ui-components/docs/card.md +265 -0
- package/ui-components/docs/carousel.md +627 -0
- package/ui-components/docs/chart.md +1256 -0
- package/ui-components/docs/checkbox.md +477 -0
- package/ui-components/docs/collapsible.md +446 -0
- package/ui-components/docs/combobox.md +1003 -0
- package/ui-components/docs/context-menu.md +860 -0
- package/ui-components/docs/data-table.md +1580 -0
- package/ui-components/docs/date-picker.md +614 -0
- package/ui-components/docs/dialog.md +414 -0
- package/ui-components/docs/drawer.md +51 -0
- package/ui-components/docs/dropdown-menu.md +1000 -0
- package/ui-components/docs/field.md +1192 -0
- package/ui-components/docs/hover-card.md +275 -0
- package/ui-components/docs/input-group.md +866 -0
- package/ui-components/docs/input.md +456 -0
- package/ui-components/docs/label.md +295 -0
- package/ui-components/docs/pagination.md +416 -0
- package/ui-components/docs/popover.md +348 -0
- package/ui-components/docs/progress.md +214 -0
- package/ui-components/docs/radio-group.md +389 -0
- package/ui-components/docs/resizable.md +265 -0
- package/ui-components/docs/select.md +616 -0
- package/ui-components/docs/separator.md +216 -0
- package/ui-components/docs/skeleton.md +205 -0
- package/ui-components/docs/slider.md +226 -0
- package/ui-components/docs/spinner.md +332 -0
- package/ui-components/docs/switch.md +236 -0
- package/ui-components/docs/table.md +551 -0
- package/ui-components/docs/tabs.md +379 -0
- package/ui-components/docs/textarea.md +187 -0
- package/ui-components/docs/toggle-group.md +370 -0
- package/ui-components/docs/toggle.md +91 -0
- package/ui-components/docs/tooltip.md +290 -0
- package/ui-components/drawer.tsx +213 -0
- package/ui-components/dropdown-menu.tsx +263 -0
- package/ui-components/field.tsx +224 -0
- package/ui-components/hover-card.tsx +46 -0
- package/ui-components/input-group.tsx +146 -0
- package/ui-components/input.tsx +20 -0
- package/ui-components/label.tsx +17 -0
- package/ui-components/pagination.tsx +124 -0
- package/ui-components/popover.tsx +81 -0
- package/ui-components/progress.tsx +63 -0
- package/ui-components/radio-group.tsx +37 -0
- package/ui-components/resizable.tsx +40 -0
- package/ui-components/scoped-portal.tsx +31 -0
- package/ui-components/select.tsx +195 -0
- package/ui-components/separator.tsx +18 -0
- package/ui-components/skeleton.tsx +15 -0
- package/ui-components/slider.tsx +75 -0
- package/ui-components/spinner.tsx +20 -0
- package/ui-components/switch.tsx +26 -0
- package/ui-components/table.tsx +86 -0
- package/ui-components/tabs.tsx +75 -0
- package/ui-components/textarea.tsx +17 -0
- package/ui-components/toggle-group.tsx +85 -0
- package/ui-components/toggle.tsx +42 -0
- package/ui-components/tooltip.tsx +61 -0
- package/ui-components/utils.ts +8 -0
- package/workspace/.claude/skills/moi-workspace/SKILL.md +11 -20
- package/workspace/.claude/skills/moi-workspace/references/DESIGN.md +4 -3
- package/workspace/.claude/skills/moi-workspace/references/UI-COMPONENTS.md +67 -203
- package/dist/chunk-59zf0m6n.js +0 -1
- package/dist/chunk-bj3akjs2.js +0 -1
- package/dist/chunk-j2w2vak4.js +0 -546
- package/dist/chunk-k2r9c95m.js +0 -10
- package/dist/chunk-kmbxhgwv.js +0 -1
- package/dist/chunk-p1yqa71k.js +0 -83
- package/dist/chunk-r5xrenjy.css +0 -1
|
@@ -182,6 +182,24 @@ what moi's current backends expose — it already carries `cwd`, `git_branch`,
|
|
|
182
182
|
`git_repo_root`, `parent_session_id`, per-session token counts and
|
|
183
183
|
`estimated_cost_usd`/`actual_cost_usd`.
|
|
184
184
|
|
|
185
|
+
#### 3.2.2 The default model rides on `currentModelId`, and the catalog is cached
|
|
186
|
+
|
|
187
|
+
`session/new` also returns `currentModelId` — the model Hermes resolves from
|
|
188
|
+
the profile's `config.yaml` (`model.default` + `model.provider`, rewritten by
|
|
189
|
+
`hermes model`). `./models.ts` maps it onto a synthetic `default` row with
|
|
190
|
+
`resolvedModel`, the same convention the Claude and Codex harnesses use, so
|
|
191
|
+
the picker preselects the model a promptless chat would actually run.
|
|
192
|
+
Verified against v0.20.0 (`xai-oauth:grok-build-0.1`, 67 raw rows).
|
|
193
|
+
|
|
194
|
+
The catalog only ever arrives on `session/new`, and every such call persists a
|
|
195
|
+
zero-message row in `state.db` that `hermes sessions list` shows as a blank
|
|
196
|
+
entry (moi's own list hides them). So the state is cached per workspace
|
|
197
|
+
(`../acp/model-state.ts`): the first picker snapshot opens one throwaway
|
|
198
|
+
session, every real chat start refreshes the cache from its own `session/new`,
|
|
199
|
+
and the cache is stamped with the mtimes of the profile's `config.yaml` and
|
|
200
|
+
`.env` (`hermesConfigFingerprint`) so a default changed outside moi is
|
|
201
|
+
rediscovered on the next snapshot instead of on every one.
|
|
202
|
+
|
|
185
203
|
### 3.3 The prompt stream
|
|
186
204
|
|
|
187
205
|
`session/prompt` streams `session/update` notifications and resolves with
|
|
@@ -153,3 +153,23 @@ export async function resolveHermesProfile(
|
|
|
153
153
|
}
|
|
154
154
|
return profiles.find(p => p.path === workspacePath) ?? null
|
|
155
155
|
}
|
|
156
|
+
|
|
157
|
+
// Version stamp for the profile inputs that decide its default model:
|
|
158
|
+
// config.yaml (`model.default`, rewritten by `hermes model`) and .env
|
|
159
|
+
// (provider keys, which gate which entries the catalog lists at all). Mtimes,
|
|
160
|
+
// not contents — the ACP session reports the real resolved state, this only
|
|
161
|
+
// says when to ask again. A missing file stamps as such, so creating it later
|
|
162
|
+
// still counts as a change.
|
|
163
|
+
export async function hermesConfigFingerprint(home: string | null): Promise<string | undefined> {
|
|
164
|
+
if (!home) return undefined
|
|
165
|
+
const stamps = await Promise.all(
|
|
166
|
+
['config.yaml', '.env'].map(async file => {
|
|
167
|
+
try {
|
|
168
|
+
return String((await stat(join(home, file))).mtimeMs)
|
|
169
|
+
} catch {
|
|
170
|
+
return 'missing'
|
|
171
|
+
}
|
|
172
|
+
})
|
|
173
|
+
)
|
|
174
|
+
return stamps.join(' ')
|
|
175
|
+
}
|
|
@@ -26,6 +26,7 @@ import { getAcpProcessInfo, killAcpWorkspace, killAllAcpClients } from '../acp/c
|
|
|
26
26
|
import {
|
|
27
27
|
DEFAULT_PROFILE,
|
|
28
28
|
discoverHermesProfiles,
|
|
29
|
+
hermesConfigFingerprint,
|
|
29
30
|
profileHomeFromWorkspace,
|
|
30
31
|
resolveHermesProfile
|
|
31
32
|
} from './discovery'
|
|
@@ -42,6 +43,14 @@ const config: AcpProviderConfig = {
|
|
|
42
43
|
provider: 'hermes',
|
|
43
44
|
noPromptModeId: NO_PROMPT_MODE,
|
|
44
45
|
mapModels: hermesModels,
|
|
46
|
+
// The profile's default model (`currentModelId` on `session/new`) follows
|
|
47
|
+
// config.yaml, which `hermes model` and hand edits change while moi runs.
|
|
48
|
+
// Stamp the cache with the config mtimes so such a change is picked up on
|
|
49
|
+
// the next snapshot without opening a throwaway session every time.
|
|
50
|
+
modelStateFingerprint: async ctx => {
|
|
51
|
+
const profile = await resolveHermesProfile(ctx.workspacePath, ctx.agentId)
|
|
52
|
+
return hermesConfigFingerprint(profile?.home ?? profileHomeFromWorkspace(ctx.workspacePath))
|
|
53
|
+
},
|
|
45
54
|
supportsImages: true,
|
|
46
55
|
async spawn(ctx) {
|
|
47
56
|
const bin = findHarnessExecutable('hermes')
|
|
@@ -15,10 +15,11 @@
|
|
|
15
15
|
// in `description` alone, which is why the group comes from that field.
|
|
16
16
|
import type { Model } from '@/lib/types'
|
|
17
17
|
|
|
18
|
-
import type { AcpModelInfo } from '../acp/wire'
|
|
18
|
+
import type { AcpModelInfo, AcpModelState } from '../acp/wire'
|
|
19
19
|
|
|
20
20
|
const PROVIDER_PREFIX = /^Provider:\s*/
|
|
21
|
-
//
|
|
21
|
+
// This is display cleanup only. The default comes from structured
|
|
22
|
+
// `currentModelId`, never by inferring it from this suffix.
|
|
22
23
|
const CURRENT_SUFFIX = /\s*•\s*current$/
|
|
23
24
|
|
|
24
25
|
function providerOf(description: string | undefined): string | undefined {
|
|
@@ -65,15 +66,34 @@ function hermesModel(info: AcpModelInfo): Model {
|
|
|
65
66
|
// — for the same endpoint and model. Both spellings work, so keep the first
|
|
66
67
|
// and drop rows the picker would render as an exact duplicate. Keyed on
|
|
67
68
|
// `providerId` where there is one, since both spellings normalize to it.
|
|
68
|
-
export function hermesModels(
|
|
69
|
-
const seen = new
|
|
69
|
+
export function hermesModels(state: AcpModelState): Model[] {
|
|
70
|
+
const seen = new Map<string, number>()
|
|
70
71
|
const out: Model[] = []
|
|
71
|
-
for (const info of
|
|
72
|
+
for (const info of state.availableModels ?? []) {
|
|
72
73
|
const model = hermesModel(info)
|
|
73
74
|
const key = `${model.providerId ?? model.group ?? ''} ${model.displayName}`
|
|
74
|
-
|
|
75
|
-
|
|
75
|
+
const existingIndex = seen.get(key)
|
|
76
|
+
if (existingIndex !== undefined) {
|
|
77
|
+
// If Hermes names a duplicate spelling as current, retain that exact id
|
|
78
|
+
// so the structured default can resolve to a visible picker row.
|
|
79
|
+
if (info.modelId === state.currentModelId) out[existingIndex] = model
|
|
80
|
+
continue
|
|
81
|
+
}
|
|
82
|
+
seen.set(key, out.length)
|
|
76
83
|
out.push(model)
|
|
77
84
|
}
|
|
78
|
-
|
|
85
|
+
|
|
86
|
+
if (!state.currentModelId) return out
|
|
87
|
+
const currentIndex = out.findIndex(model => model.value === state.currentModelId)
|
|
88
|
+
if (currentIndex === -1) return out
|
|
89
|
+
|
|
90
|
+
out[currentIndex] = { ...out[currentIndex], resolvedModel: state.currentModelId }
|
|
91
|
+
return [
|
|
92
|
+
{
|
|
93
|
+
value: 'default',
|
|
94
|
+
resolvedModel: state.currentModelId,
|
|
95
|
+
displayName: 'Default (from Hermes)'
|
|
96
|
+
},
|
|
97
|
+
...out
|
|
98
|
+
]
|
|
79
99
|
}
|
package/server/harness/types.ts
CHANGED
|
@@ -72,6 +72,8 @@ export type DiscoveredWorkspaceCandidate = {
|
|
|
72
72
|
type: WorkspaceType
|
|
73
73
|
}
|
|
74
74
|
|
|
75
|
+
export type HarnessWorkspace = Pick<WorkspaceEntry, 'id' | 'path' | 'type' | 'agentId'>
|
|
76
|
+
|
|
75
77
|
export type Harness = {
|
|
76
78
|
id: WorkspaceType
|
|
77
79
|
capabilities: HarnessCapabilities
|
|
@@ -104,9 +106,9 @@ export type Harness = {
|
|
|
104
106
|
discoverWorkspaces?(registeredPaths: Set<string>): Promise<DiscoveredWorkspaceCandidate[]>
|
|
105
107
|
// Without a workspace, only check whether the runtime is installed. With a
|
|
106
108
|
// workspace, also check provider state such as authentication.
|
|
107
|
-
availability?(ws?:
|
|
109
|
+
availability?(ws?: HarnessWorkspace): Promise<HarnessAvailability>
|
|
108
110
|
// Optional recovery flow the host can start without a terminal.
|
|
109
|
-
startLogin?(ws:
|
|
111
|
+
startLogin?(ws: HarnessWorkspace): Promise<HarnessLogin>
|
|
110
112
|
|
|
111
113
|
// -- host integration hooks -------------------------------------------------
|
|
112
114
|
// Env is frozen at spawn everywhere; this reaps idle sessions/processes so
|
package/server/moi-scaffold.ts
CHANGED
|
@@ -20,7 +20,7 @@ export const MOI_PACKAGE_JSON = {
|
|
|
20
20
|
// Base UI primitives and the cn() stack, so one install at workspace
|
|
21
21
|
// creation covers the common path. Component-specific deps (recharts,
|
|
22
22
|
// embla, …) stay agent-installed on demand — `add` prints them.
|
|
23
|
-
'@base-ui/react': '^1.
|
|
23
|
+
'@base-ui/react': '^1.8.0',
|
|
24
24
|
'class-variance-authority': '^0.7.1',
|
|
25
25
|
clsx: '^2.1.1',
|
|
26
26
|
'tailwind-merge': '^3.3.1'
|
package/server/preview.ts
CHANGED
|
@@ -61,7 +61,9 @@ export async function serveWorkspaceImagePreview(
|
|
|
61
61
|
.resize(PREVIEW_MAX_EDGE, PREVIEW_MAX_EDGE, { fit: 'inside', withoutEnlargement: true })
|
|
62
62
|
.webp({ quality: 80 })
|
|
63
63
|
.toBuffer()
|
|
64
|
-
return new Response(data, {
|
|
64
|
+
return new Response(new Uint8Array(data), {
|
|
65
|
+
headers: { ...headers, 'Content-Type': 'image/webp' }
|
|
66
|
+
})
|
|
65
67
|
} catch {
|
|
66
68
|
// Undecodable (corrupt, or an unsupported codec despite the extension) —
|
|
67
69
|
// there's no preview to show; the client hides the <img> on error.
|
package/server/registry.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { rename, rm } from 'node:fs/promises'
|
|
2
2
|
import { homedir } from 'node:os'
|
|
3
3
|
import { join, resolve, sep } from 'path'
|
|
4
4
|
|
|
@@ -43,8 +43,23 @@ async function readRegistry(): Promise<WorkspaceEntry[]> {
|
|
|
43
43
|
}
|
|
44
44
|
|
|
45
45
|
async function writeRegistry(entries: WorkspaceEntry[]): Promise<void> {
|
|
46
|
-
|
|
47
|
-
|
|
46
|
+
const temporaryPath = `${_registryPath}.${crypto.randomUUID()}.tmp`
|
|
47
|
+
try {
|
|
48
|
+
await Bun.write(temporaryPath, JSON.stringify(entries, null, 2))
|
|
49
|
+
await rename(temporaryPath, _registryPath)
|
|
50
|
+
} finally {
|
|
51
|
+
await rm(temporaryPath, { force: true })
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Lock the whole read-modify-write operation, including validation. Atomic
|
|
56
|
+
// replacement keeps readers safe; the queue keeps concurrent writers safe.
|
|
57
|
+
let writeChain: Promise<unknown> = Promise.resolve()
|
|
58
|
+
|
|
59
|
+
function locked<T>(operation: () => Promise<T>): Promise<T> {
|
|
60
|
+
const run = writeChain.then(operation)
|
|
61
|
+
writeChain = run.catch(() => {})
|
|
62
|
+
return run
|
|
48
63
|
}
|
|
49
64
|
|
|
50
65
|
export type RegisterOptions = {
|
|
@@ -90,26 +105,28 @@ export async function registerWorkspace(
|
|
|
90
105
|
absPath: string,
|
|
91
106
|
opts: RegisterOptions = {}
|
|
92
107
|
): Promise<WorkspaceEntry> {
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
108
|
+
return locked(async () => {
|
|
109
|
+
const normalPath = resolve(absPath)
|
|
110
|
+
const entries = await readRegistry()
|
|
111
|
+
if (opts.id) {
|
|
112
|
+
const error = workspaceIdError(entries, normalPath, opts.id)
|
|
113
|
+
if (error) throw new Error(error)
|
|
114
|
+
}
|
|
115
|
+
const existing = entries.find(e => e.path === normalPath)
|
|
116
|
+
if (existing) return existing
|
|
117
|
+
const entry: WorkspaceEntry = {
|
|
118
|
+
id: opts.id ?? newWorkspaceId(),
|
|
119
|
+
path: normalPath,
|
|
120
|
+
addedAt: new Date().toISOString(),
|
|
121
|
+
...(opts.type ? { type: opts.type } : {}),
|
|
122
|
+
...(opts.name ? { name: opts.name } : {}),
|
|
123
|
+
...(opts.agentId ? { agentId: opts.agentId } : {}),
|
|
124
|
+
...(opts.isDefault ? { isDefault: opts.isDefault } : {}),
|
|
125
|
+
...(opts.lastRunAt ? { lastRunAt: opts.lastRunAt } : {})
|
|
126
|
+
}
|
|
127
|
+
await writeRegistry([entry, ...entries])
|
|
128
|
+
return withDisplayPath(entry)
|
|
129
|
+
})
|
|
113
130
|
}
|
|
114
131
|
|
|
115
132
|
export async function getWorkspace(id: string): Promise<WorkspaceEntry | null> {
|
|
@@ -118,11 +135,13 @@ export async function getWorkspace(id: string): Promise<WorkspaceEntry | null> {
|
|
|
118
135
|
}
|
|
119
136
|
|
|
120
137
|
export async function removeWorkspace(id: string): Promise<boolean> {
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
138
|
+
return locked(async () => {
|
|
139
|
+
const entries = await readRegistry()
|
|
140
|
+
const next = entries.filter(e => e.id !== id)
|
|
141
|
+
if (next.length === entries.length) return false
|
|
142
|
+
await writeRegistry(next)
|
|
143
|
+
return true
|
|
144
|
+
})
|
|
126
145
|
}
|
|
127
146
|
|
|
128
147
|
export async function listWorkspaces(): Promise<WorkspaceEntry[]> {
|
|
@@ -131,19 +150,22 @@ export async function listWorkspaces(): Promise<WorkspaceEntry[]> {
|
|
|
131
150
|
}
|
|
132
151
|
|
|
133
152
|
export async function reorderWorkspaces(ids: string[]): Promise<WorkspaceEntry[]> {
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
153
|
+
return locked(async () => {
|
|
154
|
+
const entries = await readRegistry()
|
|
155
|
+
if (ids.length !== entries.length)
|
|
156
|
+
throw new Error('Workspace order must include every workspace')
|
|
157
|
+
|
|
158
|
+
const unique = new Set(ids)
|
|
159
|
+
if (unique.size !== ids.length) throw new Error('Workspace order contains duplicate ids')
|
|
160
|
+
|
|
161
|
+
const byId = new Map(entries.map(e => [e.id, e]))
|
|
162
|
+
const next = ids.map(id => byId.get(id))
|
|
163
|
+
if (next.some(e => !e)) throw new Error('Workspace order contains unknown ids')
|
|
164
|
+
|
|
165
|
+
const ordered = next as WorkspaceEntry[]
|
|
166
|
+
await writeRegistry(ordered)
|
|
167
|
+
return ordered.map(withDisplayPath)
|
|
168
|
+
})
|
|
147
169
|
}
|
|
148
170
|
|
|
149
171
|
// Resolve a path to the registered workspace that *contains* it: the entry
|
|
@@ -1,23 +1,19 @@
|
|
|
1
1
|
import { basename } from 'path'
|
|
2
2
|
import sharp from 'sharp'
|
|
3
3
|
|
|
4
|
+
import { Store } from '@tldraw/store'
|
|
4
5
|
import {
|
|
5
|
-
type IndexKey,
|
|
6
6
|
type TLPageId,
|
|
7
7
|
type TLRecord,
|
|
8
|
-
type
|
|
8
|
+
type TLStoreProps,
|
|
9
|
+
type TLStoreSnapshot,
|
|
9
10
|
AssetRecordType,
|
|
10
|
-
ZERO_INDEX_KEY,
|
|
11
11
|
createBindingId,
|
|
12
12
|
createShapeId,
|
|
13
|
-
|
|
14
|
-
defaultBindingUtils,
|
|
15
|
-
defaultShapeUtils,
|
|
16
|
-
getIndexAbove,
|
|
17
|
-
getSnapshot,
|
|
18
|
-
loadSnapshot,
|
|
13
|
+
createTLSchema,
|
|
19
14
|
toRichText
|
|
20
|
-
} from 'tldraw'
|
|
15
|
+
} from '@tldraw/tlschema'
|
|
16
|
+
import { type IndexKey, ZERO_INDEX_KEY, getIndexAbove } from '@tldraw/utils'
|
|
21
17
|
|
|
22
18
|
import { describeNewerWriter, sequencesAhead } from '@/lib/scratchpad-skew'
|
|
23
19
|
import type {
|
|
@@ -36,6 +32,7 @@ import {
|
|
|
36
32
|
saveScratchpadDoc
|
|
37
33
|
} from './scratchpad'
|
|
38
34
|
import { storeScratchpadAsset } from './scratchpad-assets'
|
|
35
|
+
import { SHAPE_DEFAULTS } from './scratchpad-shape-defaults'
|
|
39
36
|
|
|
40
37
|
// Server-side Scratchpad writer. The browser is no longer required to draw: we run
|
|
41
38
|
// the same ops against a *headless* tldraw store here, persist the snapshot, and
|
|
@@ -47,24 +44,42 @@ import { storeScratchpadAsset } from './scratchpad-assets'
|
|
|
47
44
|
// DOM + text measurement that don't exist in the server runtime. The store
|
|
48
45
|
// validates every `put`, so a malformed record throws instead of corrupting the
|
|
49
46
|
// file — that validation is what keeps hand-built records honest.
|
|
47
|
+
//
|
|
48
|
+
// Deliberately built from `@tldraw/store` + `@tldraw/tlschema` (+ `@tldraw/utils`),
|
|
49
|
+
// NOT the `tldraw` package: the `tldraw` entry point evaluates the whole React
|
|
50
|
+
// editor, and `react-dom` (19.2+) throws at load time whenever the `react` it
|
|
51
|
+
// resolves is a different version — a real hazard for a global `bun i -g`
|
|
52
|
+
// install, whose shared, hoisted `node_modules` can pair moi's `react-dom` with a
|
|
53
|
+
// `react` some other global package locked earlier. That crash used to take the
|
|
54
|
+
// entire server down at `moi start`, before it served a single request. The
|
|
55
|
+
// sub-packages import no React at all, so the server never resolves it. A test
|
|
56
|
+
// (`server/test/server-react-free.test.ts`) keeps this module React-free.
|
|
57
|
+
|
|
58
|
+
// The headless store. `TLStoreProps` is what a schema built by `createTLSchema`
|
|
59
|
+
// expects, but the only prop anything reads here is `defaultName` (the
|
|
60
|
+
// integrity checker, when it seeds the document record). The rest — asset
|
|
61
|
+
// upload, user resolution, editor mount — exists for a mounted `Editor`, which
|
|
62
|
+
// this store never gets.
|
|
63
|
+
type HeadlessStore = Store<TLRecord, TLStoreProps>
|
|
64
|
+
|
|
65
|
+
const unsupported = (what: string) => (): never => {
|
|
66
|
+
throw new Error(`${what} is not available on the server-side scratchpad store`)
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
// Cast: the editor-facing props are unreachable on a store that never mounts an
|
|
70
|
+
// `Editor`; `defaultName` is the one field the store itself consumes.
|
|
71
|
+
const HEADLESS_STORE_PROPS = {
|
|
72
|
+
defaultName: '',
|
|
73
|
+
assets: { upload: unsupported('Asset upload'), resolve: () => null, remove: async () => {} },
|
|
74
|
+
users: { currentUser: null, resolve: () => null },
|
|
75
|
+
onMount: () => {}
|
|
76
|
+
} as unknown as TLStoreProps
|
|
50
77
|
|
|
51
|
-
//
|
|
52
|
-
// is an instance method but doesn't touch the editor for the shapes we create, so a
|
|
53
|
-
// throwaway instance is enough; the result is static per type, so we cache it.
|
|
54
|
-
const shapeDefaults = new Map<string, Record<string, unknown>>()
|
|
78
|
+
// A fresh copy of a shape type's default props (see scratchpad-shape-defaults.ts).
|
|
55
79
|
function defaultProps(type: string): Record<string, unknown> {
|
|
56
|
-
|
|
57
|
-
if (!
|
|
58
|
-
|
|
59
|
-
if (!Util) throw new Error(`Unknown shape type "${type}"`)
|
|
60
|
-
// eslint-disable-next-line new-cap -- Util is a class constructor pulled from a list
|
|
61
|
-
const util = new (Util as unknown as new (editor: unknown) => {
|
|
62
|
-
getDefaultProps(): Record<string, unknown>
|
|
63
|
-
})({})
|
|
64
|
-
cached = util.getDefaultProps()
|
|
65
|
-
shapeDefaults.set(type, cached)
|
|
66
|
-
}
|
|
67
|
-
return { ...cached }
|
|
80
|
+
const defaults = SHAPE_DEFAULTS[type]
|
|
81
|
+
if (!defaults) throw new Error(`Unknown shape type "${type}"`)
|
|
82
|
+
return { ...defaults }
|
|
68
83
|
}
|
|
69
84
|
|
|
70
85
|
// Map an op's optional color/size/fill onto tldraw shape props (omitted → tldraw default).
|
|
@@ -81,7 +96,7 @@ function styleProps(style: ScratchStyle): Record<string, unknown> {
|
|
|
81
96
|
// itself, which reaches the `moi scratch` caller ahead of — and contradicting —
|
|
82
97
|
// the actionable error we throw below ("the file is intact"). Whatever it says
|
|
83
98
|
// is replayed if the load actually succeeds, so only the swallowed-by-a-throw
|
|
84
|
-
// case is lost. Safe to swap `console.error` around: `
|
|
99
|
+
// case is lost. Safe to swap `console.error` around: `loadStoreSnapshot` is sync.
|
|
85
100
|
function loadQuietly(load: () => void): void {
|
|
86
101
|
const original = console.error
|
|
87
102
|
const held: unknown[][] = []
|
|
@@ -102,36 +117,44 @@ function loadQuietly(load: () => void): void {
|
|
|
102
117
|
// so a snapshot written by a newer tldraw is unreadable here — but intact.
|
|
103
118
|
// That gets a loud, actionable error instead of tldraw's bare `migration-error`
|
|
104
119
|
// (which surfaces verbatim through the control port to the `moi scratch` CLI).
|
|
105
|
-
function buildStore(
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
120
|
+
function buildStore(
|
|
121
|
+
doc: ScratchpadDoc | null,
|
|
122
|
+
writer: ScratchpadWriter | undefined
|
|
123
|
+
): HeadlessStore {
|
|
124
|
+
// `createTLSchema()` with no arguments is tldraw's default shape/binding/asset
|
|
125
|
+
// set — the same schema the browser's `createTLStore` builds from
|
|
126
|
+
// `defaultShapeUtils` + `defaultBindingUtils` (a test pins the two serialized
|
|
127
|
+
// schemas equal), so a snapshot written here round-trips into the live canvas.
|
|
128
|
+
const store: HeadlessStore = new Store({ schema: createTLSchema(), props: HEADLESS_STORE_PROPS })
|
|
129
|
+
// `loadStoreSnapshot` migrates the records forward to this schema, replaces the
|
|
130
|
+
// store's contents, and seeds whatever a usable document still lacks (the
|
|
131
|
+
// document/page records of a fresh or partial canvas) — so an empty canvas
|
|
132
|
+
// loads an empty snapshot rather than skipping the load.
|
|
133
|
+
const snapshot = doc?.store
|
|
134
|
+
? (doc as unknown as TLStoreSnapshot)
|
|
135
|
+
: { store: {}, schema: store.schema.serialize() }
|
|
136
|
+
try {
|
|
137
|
+
loadQuietly(() => store.loadStoreSnapshot(snapshot))
|
|
138
|
+
} catch (err) {
|
|
139
|
+
const ahead = sequencesAhead(doc?.schema, store.schema.serialize().sequences)
|
|
140
|
+
if (ahead.length > 0) {
|
|
123
141
|
throw new Error(
|
|
124
|
-
`Scratchpad
|
|
125
|
-
`
|
|
142
|
+
`Scratchpad was written by a newer moi (${describeNewerWriter(writer, ahead)}); ` +
|
|
143
|
+
`this server has tldraw ${SCRATCHPAD_WRITER.tldraw}. Restart the newer server or ` +
|
|
144
|
+
`update this install (bun install -g moi-computer@latest). ` +
|
|
145
|
+
`The canvas file is intact — do not reset it.`
|
|
126
146
|
)
|
|
127
147
|
}
|
|
148
|
+
// Not skew — the snapshot itself is bad. Keep the original cause visible.
|
|
149
|
+
throw new Error(
|
|
150
|
+
`Scratchpad snapshot failed to load (not a version mismatch — the file may be ` +
|
|
151
|
+
`corrupted): ${err instanceof Error ? err.message : String(err)}`
|
|
152
|
+
)
|
|
128
153
|
}
|
|
129
|
-
// Seed the document/page records a fresh (or partial) store needs to be valid.
|
|
130
|
-
store.ensureStoreIsUsable()
|
|
131
154
|
return store
|
|
132
155
|
}
|
|
133
156
|
|
|
134
|
-
function firstPageId(store:
|
|
157
|
+
function firstPageId(store: HeadlessStore): TLPageId {
|
|
135
158
|
for (const record of store.allRecords()) {
|
|
136
159
|
if (record.typeName === 'page') return record.id
|
|
137
160
|
}
|
|
@@ -141,7 +164,7 @@ function firstPageId(store: TLStore): TLPageId {
|
|
|
141
164
|
|
|
142
165
|
// The next fractional index above every shape on the page, so a new shape lands on
|
|
143
166
|
// top. IndexKeys sort lexicographically, so a string max is a valid ordering.
|
|
144
|
-
function nextIndex(store:
|
|
167
|
+
function nextIndex(store: HeadlessStore, pageId: TLPageId): IndexKey {
|
|
145
168
|
let max: IndexKey = ZERO_INDEX_KEY
|
|
146
169
|
for (const record of store.allRecords()) {
|
|
147
170
|
if (record.typeName === 'shape' && record.parentId === pageId && record.index > max) {
|
|
@@ -231,7 +254,7 @@ async function processCanvasImage(
|
|
|
231
254
|
// reference — never a base64 blob (see scratchpad-assets.ts). Async — unlike
|
|
232
255
|
// the other ops — because it decodes and resizes the image first.
|
|
233
256
|
async function applyAddImage(
|
|
234
|
-
store:
|
|
257
|
+
store: HeadlessStore,
|
|
235
258
|
workspacePath: string,
|
|
236
259
|
op: Extract<ScratchOp, { kind: 'add-image' }>
|
|
237
260
|
): Promise<ScratchOpResult> {
|
|
@@ -275,7 +298,7 @@ async function applyAddImage(
|
|
|
275
298
|
|
|
276
299
|
// Apply one mutating op to the store. `read` and `view` are handled elsewhere
|
|
277
300
|
// (disk / browser) and never reach here. Returns the op's result.
|
|
278
|
-
function applyOp(store:
|
|
301
|
+
function applyOp(store: HeadlessStore, op: ScratchOp): ScratchOpResult {
|
|
279
302
|
const pageId = firstPageId(store)
|
|
280
303
|
const requireShape = (name: string) => {
|
|
281
304
|
const shape = store.get(createShapeId(name))
|
|
@@ -412,7 +435,7 @@ function applyOp(store: TLStore, op: ScratchOp): ScratchOpResult {
|
|
|
412
435
|
// True if any shape still references the given asset id. tldraw lets several
|
|
413
436
|
// shapes share one asset (e.g. a duplicated image), so delete/replace only drops
|
|
414
437
|
// the asset record when its last user is gone — leaving the file for the sweep.
|
|
415
|
-
function anyShapeUsesAsset(store:
|
|
438
|
+
function anyShapeUsesAsset(store: HeadlessStore, assetId: string): boolean {
|
|
416
439
|
for (const record of store.allRecords()) {
|
|
417
440
|
if (record.typeName !== 'shape') continue
|
|
418
441
|
const props = (record as unknown as { props?: { assetId?: unknown } }).props
|
|
@@ -422,7 +445,7 @@ function anyShapeUsesAsset(store: TLStore, assetId: string): boolean {
|
|
|
422
445
|
}
|
|
423
446
|
|
|
424
447
|
// Ids of bindings whose start/end is the named shape.
|
|
425
|
-
function bindingsTouching(store:
|
|
448
|
+
function bindingsTouching(store: HeadlessStore, name: string): TLRecord['id'][] {
|
|
426
449
|
const target = createShapeId(name)
|
|
427
450
|
const ids: TLRecord['id'][] = []
|
|
428
451
|
for (const record of store.allRecords()) {
|
|
@@ -459,7 +482,7 @@ export function executeScratchOp(
|
|
|
459
482
|
// add-image decodes/resizes the file first, so it's the one async op.
|
|
460
483
|
const result =
|
|
461
484
|
op.kind === 'add-image' ? await applyAddImage(store, workspacePath, op) : applyOp(store, op)
|
|
462
|
-
const next =
|
|
485
|
+
const next = store.getStoreSnapshot() as unknown as ScratchpadDoc
|
|
463
486
|
await saveScratchpadDoc(next, workspacePath)
|
|
464
487
|
publishEvent({ type: 'scratchpad:updated', workspaceId })
|
|
465
488
|
return result
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import { toRichText } from '@tldraw/tlschema'
|
|
2
|
+
|
|
3
|
+
// Default props for every shape type the headless scratchpad writer creates
|
|
4
|
+
// (see scratchpad-executor.ts). Copied from each shape's `ShapeUtil.getDefaultProps()`
|
|
5
|
+
// in the `tldraw` package — which the server deliberately does not import, because
|
|
6
|
+
// its entry point drags in the React editor (see the executor's header comment).
|
|
7
|
+
// The values are static per tldraw version, and a test
|
|
8
|
+
// (`server/test/scratchpad-executor.test.ts`) asserts each entry still equals what
|
|
9
|
+
// the pinned `tldraw` returns, so a bump that changes a default fails loudly
|
|
10
|
+
// instead of writing records the browser would render differently.
|
|
11
|
+
export const SHAPE_DEFAULTS: Record<string, Record<string, unknown>> = {
|
|
12
|
+
geo: {
|
|
13
|
+
w: 100,
|
|
14
|
+
h: 100,
|
|
15
|
+
geo: 'rectangle',
|
|
16
|
+
dash: 'draw',
|
|
17
|
+
growY: 0,
|
|
18
|
+
url: '',
|
|
19
|
+
scale: 1,
|
|
20
|
+
color: 'black',
|
|
21
|
+
labelColor: 'black',
|
|
22
|
+
fill: 'none',
|
|
23
|
+
size: 'm',
|
|
24
|
+
font: 'draw',
|
|
25
|
+
align: 'middle',
|
|
26
|
+
verticalAlign: 'middle',
|
|
27
|
+
richText: toRichText('')
|
|
28
|
+
},
|
|
29
|
+
text: {
|
|
30
|
+
color: 'black',
|
|
31
|
+
size: 'm',
|
|
32
|
+
w: 8,
|
|
33
|
+
font: 'draw',
|
|
34
|
+
textAlign: 'start',
|
|
35
|
+
autoSize: true,
|
|
36
|
+
scale: 1,
|
|
37
|
+
richText: toRichText('')
|
|
38
|
+
},
|
|
39
|
+
note: {
|
|
40
|
+
color: 'black',
|
|
41
|
+
richText: toRichText(''),
|
|
42
|
+
size: 'm',
|
|
43
|
+
font: 'draw',
|
|
44
|
+
align: 'middle',
|
|
45
|
+
verticalAlign: 'middle',
|
|
46
|
+
labelColor: 'black',
|
|
47
|
+
growY: 0,
|
|
48
|
+
fontSizeAdjustment: 1,
|
|
49
|
+
url: '',
|
|
50
|
+
scale: 1,
|
|
51
|
+
textLastEditedBy: null
|
|
52
|
+
},
|
|
53
|
+
arrow: {
|
|
54
|
+
kind: 'arc',
|
|
55
|
+
elbowMidPoint: 0.5,
|
|
56
|
+
dash: 'draw',
|
|
57
|
+
size: 'm',
|
|
58
|
+
fill: 'none',
|
|
59
|
+
color: 'black',
|
|
60
|
+
labelColor: 'black',
|
|
61
|
+
bend: 0,
|
|
62
|
+
start: { x: 0, y: 0 },
|
|
63
|
+
end: { x: 2, y: 0 },
|
|
64
|
+
arrowheadStart: 'none',
|
|
65
|
+
arrowheadEnd: 'arrow',
|
|
66
|
+
richText: toRichText(''),
|
|
67
|
+
labelPosition: 0.5,
|
|
68
|
+
font: 'draw',
|
|
69
|
+
scale: 1
|
|
70
|
+
},
|
|
71
|
+
image: {
|
|
72
|
+
w: 100,
|
|
73
|
+
h: 100,
|
|
74
|
+
assetId: null,
|
|
75
|
+
playing: true,
|
|
76
|
+
url: '',
|
|
77
|
+
crop: null,
|
|
78
|
+
flipX: false,
|
|
79
|
+
flipY: false,
|
|
80
|
+
altText: ''
|
|
81
|
+
}
|
|
82
|
+
}
|