moi-computer 0.2.0 → 0.3.0-next.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 +4 -4
- package/client/components/Scratchpad.tsx +51 -0
- package/client/components/layout/SidebarLayout.tsx +1 -12
- package/dist/chunk-07qf2tv1.css +1 -0
- package/dist/{chunk-90bceh8y.js → chunk-c3zjp9ez.js} +83 -83
- package/dist/index.html +1 -1
- package/package.json +1 -1
- package/server/api.ts +38 -0
- package/server/cli-colors.ts +6 -3
- package/server/cli-env.ts +1 -2
- package/server/cli-pc.ts +20 -0
- package/server/cli.ts +10 -3
- package/server/scratchpad-assets.ts +223 -0
- package/server/scratchpad-executor.ts +49 -17
- package/server/scratchpad.ts +66 -17
- package/workspace/.claude/skills/moi-workspace/SCRATCHPAD.md +5 -3
- package/workspace/.claude/skills/moi-workspace/SKILL.md +1 -0
- package/dist/chunk-9mezmjpz.css +0 -1
- /package/client/{CLAUDE.md → AGENTS.md} +0 -0
- /package/server/{CLAUDE.md → AGENTS.md} +0 -0
package/server/scratchpad.ts
CHANGED
|
@@ -5,12 +5,19 @@ import tldrawPkg from 'tldraw/package.json'
|
|
|
5
5
|
import type { ScratchpadWriter } from '@/lib/types'
|
|
6
6
|
|
|
7
7
|
import moiPkg from '../package.json'
|
|
8
|
+
import {
|
|
9
|
+
assetSrcFileName,
|
|
10
|
+
extractInlineAssets,
|
|
11
|
+
scratchpadAssetFile,
|
|
12
|
+
sweepOrphanAssets
|
|
13
|
+
} from './scratchpad-assets'
|
|
8
14
|
|
|
9
15
|
// The Scratchpad is a shared tldraw canvas per workspace, persisted as a tldraw
|
|
10
16
|
// *document* snapshot here (the per-tab `session` is intentionally dropped). Two
|
|
11
17
|
// writers: the browser autosaves on user edits, and the server writes on agent
|
|
12
18
|
// draws (see scratchpad-executor.ts). This module owns the on-disk shape: load,
|
|
13
|
-
// save, and the `moi scratch read` parser.
|
|
19
|
+
// save, and the `moi scratch read` parser. Image bytes live beside the snapshot
|
|
20
|
+
// as files, not inside it (see scratchpad-assets.ts). See docs/moi-scratchpad.md.
|
|
14
21
|
|
|
15
22
|
// A tldraw document snapshot: `getSnapshot(store).document`. Opaque to us apart
|
|
16
23
|
// from `.store` (the record map) which `read` walks. `null` means empty canvas.
|
|
@@ -36,9 +43,15 @@ export type ScratchShape = {
|
|
|
36
43
|
w?: number
|
|
37
44
|
h?: number
|
|
38
45
|
text?: string
|
|
39
|
-
// Image/asset src
|
|
40
|
-
//
|
|
46
|
+
// Image/asset src: an `asset:` file reference or an https URL, passed through
|
|
47
|
+
// as-is. A legacy inline base64 blob is omitted (see omitBase64) — the agent
|
|
48
|
+
// calls `moi scratch read-image`/`view` to actually see pixels.
|
|
41
49
|
src?: string
|
|
50
|
+
// True when the shape references an asset that can't be resolved to pixels: an
|
|
51
|
+
// `asset:` file gone from .moi/.scratchpad (sidecar dir lost or pruned), or the
|
|
52
|
+
// asset record absent entirely — `read-image` will error and the canvas shows a
|
|
53
|
+
// broken image. Absent otherwise.
|
|
54
|
+
missing?: true
|
|
42
55
|
}
|
|
43
56
|
|
|
44
57
|
// The snapshot is a hidden dotfile (like `.moi/.workspace.json`): it's moi-internal
|
|
@@ -73,8 +86,17 @@ export async function saveScratchpadDoc(
|
|
|
73
86
|
workspacePath: string
|
|
74
87
|
): Promise<void> {
|
|
75
88
|
const path = getScratchpadPath(workspacePath)
|
|
89
|
+
// Every writer funnels through here, so this is where inline base64 assets
|
|
90
|
+
// (legacy snapshots, or a stale tab still holding blobs in its live store)
|
|
91
|
+
// get extracted to `.moi/.scratchpad/` files — the snapshot on disk
|
|
92
|
+
// never carries image bytes. The sweep after the write reclaims files the
|
|
93
|
+
// saved document no longer references (see scratchpad-assets.ts).
|
|
94
|
+
document = await extractInlineAssets(document, workspacePath)
|
|
76
95
|
await backupOnSchemaChange(path, document)
|
|
77
96
|
await Bun.write(path, JSON.stringify({ document, writer: SCRATCHPAD_WRITER }, null, 2))
|
|
97
|
+
// The `.bak` path keeps the sweep from orphaning the schema-change backup's
|
|
98
|
+
// images — a restored .bak should still render.
|
|
99
|
+
await sweepOrphanAssets(workspacePath, document, `${path}.bak`)
|
|
78
100
|
}
|
|
79
101
|
|
|
80
102
|
// When a save is about to overwrite a snapshot with a *different* schema (i.e.
|
|
@@ -91,11 +113,12 @@ async function backupOnSchemaChange(path: string, document: ScratchpadDoc): Prom
|
|
|
91
113
|
} catch {}
|
|
92
114
|
}
|
|
93
115
|
|
|
94
|
-
//
|
|
95
|
-
//
|
|
116
|
+
// Asset records now reference image bytes as `asset:` files (see
|
|
117
|
+
// scratchpad-assets.ts), but base64 data URLs can still appear — in a legacy
|
|
118
|
+
// snapshot not yet re-saved, or inline in rich text. Those blobs are huge and
|
|
96
119
|
// useless for reasoning about structure, so we replace each one with a short
|
|
97
|
-
// marker — the agent calls `moi scratch view` when it actually
|
|
98
|
-
//
|
|
120
|
+
// marker — the agent calls `moi scratch read-image`/`view` when it actually
|
|
121
|
+
// needs the pixels. Every other src (`asset:`, https) passes through untouched.
|
|
99
122
|
const BASE64_DATA_URL_RE = /data:[\w.+-]*\/?[\w.+-]*;base64,[A-Za-z0-9+/=]+/g
|
|
100
123
|
function omitBase64(text: string): string {
|
|
101
124
|
return text.replace(BASE64_DATA_URL_RE, 'base64:omitted')
|
|
@@ -124,11 +147,13 @@ function extractText(props: unknown): string | undefined {
|
|
|
124
147
|
}
|
|
125
148
|
|
|
126
149
|
// Resolve a single image shape's source by id, straight off the disk snapshot —
|
|
127
|
-
// no browser. The shape references an `asset` record by `props.assetId`;
|
|
128
|
-
//
|
|
129
|
-
//
|
|
130
|
-
//
|
|
131
|
-
//
|
|
150
|
+
// no browser. The shape references an `asset` record by `props.assetId`; that
|
|
151
|
+
// asset's `src` is an `asset:` file reference (read back off disk and returned
|
|
152
|
+
// as a `data:` URL so the CLI's decoding keeps working), an `https:` URL
|
|
153
|
+
// (returned as-is), or — in a legacy snapshot — an inline `data:` URL.
|
|
154
|
+
// `moi scratch read` never carries the pixels, so this is how the agent pulls
|
|
155
|
+
// them for one image. Ids match with or without the `shape:` prefix (read
|
|
156
|
+
// surfaces them stripped).
|
|
132
157
|
export async function readScratchpadImage(
|
|
133
158
|
workspacePath: string,
|
|
134
159
|
id: string
|
|
@@ -155,7 +180,14 @@ export async function readScratchpadImage(
|
|
|
155
180
|
if (!record || typeof record !== 'object') continue
|
|
156
181
|
const a = record as { typeName?: string; id?: string; props?: { src?: unknown } }
|
|
157
182
|
if (a.typeName === 'asset' && a.id === assetId && typeof a.props?.src === 'string') {
|
|
158
|
-
|
|
183
|
+
const fileName = assetSrcFileName(a.props.src)
|
|
184
|
+
if (!fileName) return { src: a.props.src }
|
|
185
|
+
const resolved = scratchpadAssetFile(workspacePath, fileName)
|
|
186
|
+
if (!resolved || !(await resolved.file.exists())) {
|
|
187
|
+
return { error: `Image "${id}" file is missing from .moi/.scratchpad` }
|
|
188
|
+
}
|
|
189
|
+
const bytes = Buffer.from(await resolved.file.arrayBuffer())
|
|
190
|
+
return { src: `data:${resolved.mimeType};base64,${bytes.toString('base64')}` }
|
|
159
191
|
}
|
|
160
192
|
}
|
|
161
193
|
return { error: `Image "${id}" has no stored data` }
|
|
@@ -170,13 +202,23 @@ export async function readScratchpadShapes(workspacePath: string): Promise<Scrat
|
|
|
170
202
|
|
|
171
203
|
// Images live as `asset` records (typeName 'asset'); a shape references one by
|
|
172
204
|
// `props.assetId`. Index asset src first so we can surface it on the shape —
|
|
173
|
-
// with base64 blobs omitted — without dumping the asset record itself.
|
|
205
|
+
// with base64 blobs omitted — without dumping the asset record itself. A
|
|
206
|
+
// file-backed src whose file is gone is flagged so the agent learns it's
|
|
207
|
+
// dangling from `read` instead of from a failing `read-image` later.
|
|
174
208
|
const assetSrc = new Map<string, string>()
|
|
209
|
+
const assetGone = new Set<string>()
|
|
175
210
|
for (const record of Object.values(store)) {
|
|
176
211
|
if (!record || typeof record !== 'object') continue
|
|
177
212
|
const a = record as { typeName?: string; id?: string; props?: { src?: unknown } }
|
|
178
213
|
if (a.typeName !== 'asset' || typeof a.id !== 'string') continue
|
|
179
|
-
if (typeof a.props?.src === 'string')
|
|
214
|
+
if (typeof a.props?.src === 'string') {
|
|
215
|
+
assetSrc.set(a.id, a.props.src)
|
|
216
|
+
const fileName = assetSrcFileName(a.props.src)
|
|
217
|
+
if (fileName) {
|
|
218
|
+
const resolved = scratchpadAssetFile(workspacePath, fileName)
|
|
219
|
+
if (!resolved || !(await resolved.file.exists())) assetGone.add(a.id)
|
|
220
|
+
}
|
|
221
|
+
}
|
|
180
222
|
}
|
|
181
223
|
|
|
182
224
|
const shapes: ScratchShape[] = []
|
|
@@ -193,7 +235,13 @@ export async function readScratchpadShapes(workspacePath: string): Promise<Scrat
|
|
|
193
235
|
if (r.typeName !== 'shape') continue
|
|
194
236
|
const w = typeof r.props?.w === 'number' ? r.props.w : undefined
|
|
195
237
|
const h = typeof r.props?.h === 'number' ? r.props.h : undefined
|
|
196
|
-
const
|
|
238
|
+
const assetId = typeof r.props?.assetId === 'string' ? r.props.assetId : undefined
|
|
239
|
+
const rawSrc = assetId !== undefined ? assetSrc.get(assetId) : undefined
|
|
240
|
+
// Flag `missing` when a shape references an asset we can't resolve to pixels:
|
|
241
|
+
// its backing file is gone (assetGone), or the asset record is absent / has a
|
|
242
|
+
// non-string src (rawSrc undefined). Either way `read-image` can't return
|
|
243
|
+
// bytes, so warn from `read` rather than let it fail later.
|
|
244
|
+
const missing = assetId !== undefined && (assetGone.has(assetId) || rawSrc === undefined)
|
|
197
245
|
shapes.push({
|
|
198
246
|
id: (r.id ?? '').replace(/^shape:/, ''),
|
|
199
247
|
type: r.type ?? 'unknown',
|
|
@@ -205,7 +253,8 @@ export async function readScratchpadShapes(workspacePath: string): Promise<Scrat
|
|
|
205
253
|
const text = extractText(r.props)
|
|
206
254
|
return text !== undefined ? { text: omitBase64(text) } : {}
|
|
207
255
|
})(),
|
|
208
|
-
...(rawSrc !== undefined ? { src: omitBase64(rawSrc) } : {})
|
|
256
|
+
...(rawSrc !== undefined ? { src: omitBase64(rawSrc) } : {}),
|
|
257
|
+
...(missing ? { missing: true as const } : {})
|
|
209
258
|
})
|
|
210
259
|
}
|
|
211
260
|
return shapes
|
|
@@ -12,9 +12,11 @@ tldraw's and shifts without notice, blobs are stripped, and `moi scratch read` a
|
|
|
12
12
|
clean, agent-friendly view. Treat it exactly like `.moi/.workspace.json` — CLI only.
|
|
13
13
|
|
|
14
14
|
- `moi scratch read` — dump the canvas as JSON: each shape's `id`, `type`, position, size, and
|
|
15
|
-
text. Off disk, so it works whether or not a browser tab is open.
|
|
16
|
-
|
|
17
|
-
|
|
15
|
+
text. Off disk, so it works whether or not a browser tab is open. An image shape's `src` is a
|
|
16
|
+
reference (`asset:...` or a URL), never the pixels; `missing: true` on a shape means its image
|
|
17
|
+
file is gone, so don't bother calling `read-image` on it.
|
|
18
|
+
- `moi scratch read-image <id>` — save one image shape to a file (its actual bytes; `read` only
|
|
19
|
+
carries the reference). Off disk too.
|
|
18
20
|
- `moi scratch view` — render the whole canvas to a PNG. Needs an open Scratchpad tab.
|
|
19
21
|
|
|
20
22
|
`read` is for logic; `view` / `read-image` are for vision.
|
|
@@ -84,6 +84,7 @@ my-agent-folder/
|
|
|
84
84
|
package.json <- Applet dependencies that you manage
|
|
85
85
|
.workspace.json <- Auto-generated. Do NOT read, edit, or `cat` this file. Use Moi CLI instead.
|
|
86
86
|
.scratchpad.json <- Scratchpad canvas snapshot. Internal — inspect only via `moi scratch read`, never open it.
|
|
87
|
+
.scratchpad/ <- Scratchpad image files. Internal — pull pixels via `moi scratch read-image`, never open it.
|
|
87
88
|
```
|
|
88
89
|
|
|
89
90
|
# Build environment
|