moi-computer 0.9.1-next.0 → 0.9.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/api/app-config.ts +51 -0
- package/client/api/workspace-keys.ts +1 -0
- package/client/app/routes/WorkspaceRoute.tsx +4 -2
- package/client/features/applets/api.ts +38 -0
- package/client/features/applets/applet-thumbnail.ts +208 -0
- package/client/features/chat/ChatEmptyState.tsx +5 -3
- package/client/features/chat/ChatPanel.tsx +2 -8
- package/client/features/chat/ChatSelector.tsx +29 -10
- package/client/features/chat/MarkdownContent.tsx +2 -2
- package/client/features/home/HomePage.tsx +13 -0
- package/client/features/home/WorkspacePreview.tsx +10 -5
- package/client/features/home/workspace-setup/CreateWorkspaceDialog.tsx +9 -0
- package/client/features/home/workspace-setup/InstallMoiDialog.tsx +245 -0
- package/client/features/home/workspace-setup/WorkspaceAgentStep.tsx +1 -1
- package/client/features/home/workspace-setup/WorkspaceDialogContent.tsx +1 -1
- package/client/features/views/ViewBuilder.tsx +18 -4
- package/client/features/views/ViewManager.tsx +18 -2
- package/client/features/widgets/WidgetFrame.tsx +1 -1
- package/client/features/widgets/WidgetGrid.tsx +1 -1
- package/client/features/widgets/Widgets.tsx +16 -15
- package/client/features/workspace/CustomizePanel.tsx +29 -8
- package/client/features/workspace/WorkspaceScreen.tsx +8 -14
- package/client/index.tsx +6 -1
- package/client/runtime/useWorkspaceEvents.ts +17 -0
- package/client/runtime/workspace-theme.ts +29 -6
- package/dist/chunk-5c19v3zr.js +10 -0
- package/dist/chunk-6wx91x8j.js +1 -0
- package/dist/chunk-8s41nd3g.js +1 -0
- package/dist/{chunk-6w6j9g4b.js → chunk-a7fzp0z0.js} +2 -2
- package/dist/{chunk-pp62fnxh.js → chunk-b959mdkj.js} +1 -1
- package/dist/chunk-cagtq45k.js +1 -0
- package/dist/chunk-cva7jpt4.js +82 -0
- package/dist/chunk-sapvd7he.css +1 -0
- package/dist/{chunk-sxc24vfp.js → chunk-zbeq02yc.js} +1 -1
- package/dist/chunk-zbt8emsw.js +2 -0
- package/dist/chunk-zfz3b4xg.js +2 -0
- package/dist/index.html +1 -1
- package/lib/types.ts +60 -41
- package/package.json +1 -1
- package/server/api.ts +69 -31
- package/server/app-config.ts +144 -0
- package/server/applets.ts +17 -0
- package/server/cli.ts +47 -11
- package/server/layout.ts +40 -45
- package/server/moi-scaffold.ts +37 -1
- package/server/thumbnails.ts +197 -0
- package/server/views.ts +10 -1
- package/server/widgets.ts +4 -18
- package/workspace/.claude/skills/moi-workspace/SKILL.md +13 -1
- package/workspace/.claude/skills/moi-workspace/references/DESIGN.md +153 -253
- package/client/features/drawings/annotation-availability.ts +0 -10
- package/client/features/widgets/api.ts +0 -47
- package/client/features/widgets/useWidgetThumbnails.ts +0 -107
- package/client/features/widgets/widget-snapshot.ts +0 -71
- package/dist/chunk-3becmq75.js +0 -81
- package/dist/chunk-8ew39cvq.js +0 -10
- package/dist/chunk-c8gtrjwp.js +0 -1
- package/dist/chunk-v964b5g7.js +0 -1
- package/dist/chunk-zx33ts9d.css +0 -1
package/server/layout.ts
CHANGED
|
@@ -1,9 +1,11 @@
|
|
|
1
1
|
import { join } from 'path'
|
|
2
2
|
|
|
3
|
-
import type { WorkspaceLayout, WorkspacePreview } from '@/lib/types'
|
|
3
|
+
import type { AppletKind, WorkspaceLayout, WorkspacePreview } from '@/lib/types'
|
|
4
4
|
import { createDefaultWorkspaceLayout, createDefaultWorkspaceTabs } from '@/lib/workspace-layout'
|
|
5
5
|
import { isWorkspaceTabId } from '@/lib/workspace-tabs'
|
|
6
6
|
|
|
7
|
+
import { getCapturedThumbnails } from './thumbnails'
|
|
8
|
+
|
|
7
9
|
function normalizeTabs(value: unknown): WorkspaceLayout['tabs'] {
|
|
8
10
|
if (!value || typeof value !== 'object') return createDefaultWorkspaceTabs()
|
|
9
11
|
const raw = value as Record<string, unknown>
|
|
@@ -22,6 +24,12 @@ function normalizeLayout(parsed: Record<string, unknown>): WorkspaceLayout {
|
|
|
22
24
|
layout.layoutMode = defaults.layoutMode
|
|
23
25
|
}
|
|
24
26
|
layout.tabs = normalizeTabs(layout.tabs)
|
|
27
|
+
// Legacy thumbnail caches are intentionally dropped, not migrated — images
|
|
28
|
+
// now live as files under `.moi/.cache/thumbnails` (server/thumbnails.ts)
|
|
29
|
+
// and applets are simply captured again there.
|
|
30
|
+
delete layout.widgetThumbnails
|
|
31
|
+
delete layout.viewThumbnails
|
|
32
|
+
delete layout.appletThumbnails
|
|
25
33
|
delete layout.sectionMode
|
|
26
34
|
delete layout.chatMode
|
|
27
35
|
return layout as unknown as WorkspaceLayout
|
|
@@ -54,53 +62,24 @@ export async function saveLayout(layout: WorkspaceLayout, workspacePath: string)
|
|
|
54
62
|
// A blind overwrite therefore erases a `moi config`-set name (and could revert an
|
|
55
63
|
// icon). So drop whatever identity the body carries and re-apply the server-owned
|
|
56
64
|
// fields from `existing` — conditionally, so an absent field never serializes
|
|
57
|
-
// as `name: undefined`.
|
|
58
|
-
// write: they have their own endpoint (saveWidgetThumbnails), and the layout
|
|
59
|
-
// GET doesn't even ship the map — a round-trip would erase it.
|
|
65
|
+
// as `name: undefined`.
|
|
60
66
|
export function mergeLayoutForSave(
|
|
61
67
|
existing: WorkspaceLayout,
|
|
62
68
|
body: WorkspaceLayout
|
|
63
69
|
): WorkspaceLayout {
|
|
64
|
-
const { name: _name, icon: _icon,
|
|
70
|
+
const { name: _name, icon: _icon, ...editor } = body
|
|
71
|
+
// Stale clients may still round-trip the pre-`.cache` thumbnail records;
|
|
72
|
+
// never let a layout PUT resurrect them in `.workspace.json`.
|
|
73
|
+
delete (editor as Record<string, unknown>).appletThumbnails
|
|
65
74
|
return {
|
|
66
75
|
...editor,
|
|
67
76
|
...(existing.name !== undefined && { name: existing.name }),
|
|
68
|
-
...(existing.icon !== undefined && { icon: existing.icon })
|
|
69
|
-
...(existing.widgetThumbnails !== undefined && {
|
|
70
|
-
widgetThumbnails: existing.widgetThumbnails
|
|
71
|
-
})
|
|
77
|
+
...(existing.icon !== undefined && { icon: existing.icon })
|
|
72
78
|
}
|
|
73
79
|
}
|
|
74
80
|
|
|
75
|
-
//
|
|
76
|
-
//
|
|
77
|
-
// theme saves never carry the base64 map, and a thumbnail save can't touch
|
|
78
|
-
// the grid. Entries merge over the existing map — removed widgets keep their
|
|
79
|
-
// last image.
|
|
80
|
-
export async function saveWidgetThumbnails(
|
|
81
|
-
workspacePath: string,
|
|
82
|
-
key: string,
|
|
83
|
-
images: Record<string, string>
|
|
84
|
-
): Promise<void> {
|
|
85
|
-
const existing = await loadLayout(workspacePath)
|
|
86
|
-
await saveLayout(
|
|
87
|
-
{
|
|
88
|
-
...existing,
|
|
89
|
-
widgetThumbnails: {
|
|
90
|
-
key,
|
|
91
|
-
// Server clock, so age-based invalidation doesn't trust client time.
|
|
92
|
-
at: new Date().toISOString(),
|
|
93
|
-
images: { ...existing.widgetThumbnails?.images, ...images }
|
|
94
|
-
}
|
|
95
|
-
},
|
|
96
|
-
workspacePath
|
|
97
|
-
)
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
// Thumbnails for the home screen's workspace card: a few captured widget
|
|
101
|
-
// images from the stored layout (see saveWidgetThumbnails). The card renders
|
|
102
|
-
// them as a loose stack, so the cap just keeps the payload small — anything
|
|
103
|
-
// past it would hide under the pile anyway.
|
|
81
|
+
// Thumbnails for the home screen's workspace card. Widgets take the front
|
|
82
|
+
// slots in grid order, then recent views fill what remains.
|
|
104
83
|
const PREVIEW_LIMIT = 3
|
|
105
84
|
const PREVIEW_MESSAGE_LIMIT = 240
|
|
106
85
|
|
|
@@ -111,21 +90,37 @@ function normalizePreviewMessage(message: string | undefined): string | undefine
|
|
|
111
90
|
return `${normalized.slice(0, PREVIEW_MESSAGE_LIMIT - 1).trimEnd()}…`
|
|
112
91
|
}
|
|
113
92
|
|
|
114
|
-
export
|
|
115
|
-
workspacePath: string,
|
|
93
|
+
export type WorkspacePreviewOptions = {
|
|
116
94
|
getProviderPreview?: (includeFirstUserMessage: boolean) => Promise<{
|
|
117
95
|
firstUserMessage?: string
|
|
118
96
|
updatedAt?: number
|
|
119
97
|
}>
|
|
98
|
+
viewIds?: readonly string[]
|
|
99
|
+
// Maps a captured applet to the image URL the home card should render —
|
|
100
|
+
// the API layer owns the route shape (GET .../applet-thumbnails/:kind/:id).
|
|
101
|
+
thumbnailUrl: (kind: AppletKind, id: string) => string
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export async function getWorkspacePreview(
|
|
105
|
+
workspacePath: string,
|
|
106
|
+
{ getProviderPreview, viewIds = [], thumbnailUrl }: WorkspacePreviewOptions
|
|
120
107
|
): Promise<WorkspacePreview> {
|
|
121
108
|
try {
|
|
122
109
|
const layout = await loadLayout(workspacePath)
|
|
123
|
-
const
|
|
124
|
-
const
|
|
110
|
+
const captured = await getCapturedThumbnails(workspacePath)
|
|
111
|
+
const validViewIds = new Set(viewIds)
|
|
112
|
+
const capturedWidgets = new Set(
|
|
113
|
+
captured.filter(record => record.kind === 'widget').map(record => record.id)
|
|
114
|
+
)
|
|
115
|
+
const widgetImages = [...layout.widgetGrid]
|
|
125
116
|
.sort((a, b) => a.y - b.y || a.x - b.x)
|
|
126
|
-
.
|
|
127
|
-
.
|
|
128
|
-
|
|
117
|
+
.filter(item => capturedWidgets.has(item.i))
|
|
118
|
+
.map(item => thumbnailUrl('widget', item.i))
|
|
119
|
+
const viewImages = captured
|
|
120
|
+
.filter(record => record.kind === 'view' && validViewIds.has(record.id))
|
|
121
|
+
.sort((a, b) => Date.parse(b.viewedAt) - Date.parse(a.viewedAt))
|
|
122
|
+
.map(record => thumbnailUrl('view', record.id))
|
|
123
|
+
const thumbnails = [...widgetImages, ...viewImages].slice(0, PREVIEW_LIMIT)
|
|
129
124
|
// The message bubble is the card's fallback for "nothing to show": gate on
|
|
130
125
|
// captured thumbnails, not grid emptiness — a workspace with widgets that
|
|
131
126
|
// were never captured (never opened in a browser) still renders an empty
|
package/server/moi-scaffold.ts
CHANGED
|
@@ -49,6 +49,36 @@ async function runBunInstall(moiDir: string): Promise<number> {
|
|
|
49
49
|
return install.exited
|
|
50
50
|
}
|
|
51
51
|
|
|
52
|
+
// Keep machine-local state out of workspaces that are git repos: build output,
|
|
53
|
+
// derived caches (applet thumbnails), and installed dependencies are all
|
|
54
|
+
// re-creatable and would otherwise churn or bloat the repo. The applet sources,
|
|
55
|
+
// package.json, and lockfile stay committable — they ARE the workspace.
|
|
56
|
+
const REQUIRED_IGNORES = ['.build/', '.cache/', 'node_modules/'] as const
|
|
57
|
+
|
|
58
|
+
export const MOI_GITIGNORE = `# moi internals — machine-local, re-creatable state
|
|
59
|
+
${REQUIRED_IGNORES.join('\n')}
|
|
60
|
+
`
|
|
61
|
+
|
|
62
|
+
// Create `.moi/.gitignore` when missing, or append required entries a
|
|
63
|
+
// pre-gitignore or hand-edited file lacks. Never rewrites existing content —
|
|
64
|
+
// user additions survive. Safe to call often; no-ops once the file is right.
|
|
65
|
+
export async function ensureMoiGitignore(workspacePath: string): Promise<void> {
|
|
66
|
+
const moiDir = join(workspacePath, '.moi')
|
|
67
|
+
if (!(await isDirectory(moiDir))) return
|
|
68
|
+
const path = join(moiDir, '.gitignore')
|
|
69
|
+
const file = Bun.file(path)
|
|
70
|
+
if (!(await file.exists())) {
|
|
71
|
+
await Bun.write(path, MOI_GITIGNORE)
|
|
72
|
+
return
|
|
73
|
+
}
|
|
74
|
+
const text = await file.text()
|
|
75
|
+
// Match entries with or without the trailing slash or a leading slash.
|
|
76
|
+
const present = new Set(text.split('\n').map(line => line.trim().replace(/^\/|\/$/g, '')))
|
|
77
|
+
const missing = REQUIRED_IGNORES.filter(entry => !present.has(entry.replace(/\/$/, '')))
|
|
78
|
+
if (missing.length === 0) return
|
|
79
|
+
await Bun.write(path, `${text.replace(/\n*$/, '\n')}${missing.join('\n')}\n`)
|
|
80
|
+
}
|
|
81
|
+
|
|
52
82
|
// Ambient types for applets (widgets & views). Editor DX only — the moi
|
|
53
83
|
// bundler resolves the `moi` module and asset imports at build time without any
|
|
54
84
|
// declarations. Lives at `.moi/` root, NOT inside widgets/views, so it isn't
|
|
@@ -134,12 +164,18 @@ export async function scaffoldMoiDir(
|
|
|
134
164
|
}
|
|
135
165
|
const moiDir = join(workspacePath, '.moi')
|
|
136
166
|
const packagePath = join(moiDir, 'package.json')
|
|
137
|
-
if (await Bun.file(packagePath).exists())
|
|
167
|
+
if (await Bun.file(packagePath).exists()) {
|
|
168
|
+
// Repair path: workspaces scaffolded before `.moi/.gitignore` existed pick
|
|
169
|
+
// it up on the next `moi init` instead of leaking cache files into git.
|
|
170
|
+
await ensureMoiGitignore(workspacePath)
|
|
171
|
+
return 'exists'
|
|
172
|
+
}
|
|
138
173
|
// A bare `.moi/` dir without package.json counts as not-bootstrapped —
|
|
139
174
|
// fill in the missing pieces.
|
|
140
175
|
|
|
141
176
|
await mkdir(join(moiDir, 'widgets'), { recursive: true })
|
|
142
177
|
await Bun.write(packagePath, JSON.stringify(MOI_PACKAGE_JSON, null, 2) + '\n')
|
|
178
|
+
await ensureMoiGitignore(workspacePath)
|
|
143
179
|
await writeAppletEnvDts(workspacePath)
|
|
144
180
|
|
|
145
181
|
const exited = installDependencies(moiDir)
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
// Applet thumbnail store: `.moi/.cache/thumbnails/` holds one image file per
|
|
2
|
+
// captured applet plus an `index.json` of freshness records. Kept out of
|
|
3
|
+
// `.workspace.json` on purpose — images are heavy, machine-local derived state,
|
|
4
|
+
// and the layout file is meant to be small and shareable. Everything here is
|
|
5
|
+
// re-creatable by opening the workspace in a browser, so `.moi/.cache` is safe
|
|
6
|
+
// to delete (and to gitignore) wholesale.
|
|
7
|
+
import { mkdir, rename, rm } from 'node:fs/promises'
|
|
8
|
+
import { join } from 'node:path'
|
|
9
|
+
|
|
10
|
+
import type { AppletKind, AppletThumbnail, AppletThumbnailBatch } from '@/lib/types'
|
|
11
|
+
|
|
12
|
+
import { ensureMoiGitignore } from './moi-scaffold'
|
|
13
|
+
|
|
14
|
+
// Index records carry the image file name (`widget-clock.webp`) so existence
|
|
15
|
+
// and content type never need an fs probe. Server-internal — the API strips it.
|
|
16
|
+
type StoredAppletThumbnail = AppletThumbnail & { file?: string }
|
|
17
|
+
|
|
18
|
+
const INDEX_FILE = 'index.json'
|
|
19
|
+
|
|
20
|
+
// Applet ids become file names under the cache dir, so anything outside this
|
|
21
|
+
// set (`../`, separators) is a path-traversal attempt. Enforced on every
|
|
22
|
+
// entry point that derives a path — the PUT batch, prune, and serve.
|
|
23
|
+
const APPLET_ID_RE = /^[a-zA-Z0-9_-]+$/
|
|
24
|
+
|
|
25
|
+
export function isValidAppletId(id: string): boolean {
|
|
26
|
+
return APPLET_ID_RE.test(id)
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
const DATA_URL_RE = /^data:(image\/(?:webp|png|jpeg));base64,([A-Za-z0-9+/=]+)$/
|
|
30
|
+
|
|
31
|
+
const EXT_BY_MIME: Record<string, string> = {
|
|
32
|
+
'image/webp': 'webp',
|
|
33
|
+
'image/png': 'png',
|
|
34
|
+
'image/jpeg': 'jpg'
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const MIME_BY_EXT: Record<string, string> = {
|
|
38
|
+
webp: 'image/webp',
|
|
39
|
+
png: 'image/png',
|
|
40
|
+
jpg: 'image/jpeg'
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
export function getThumbnailsDir(workspacePath: string): string {
|
|
44
|
+
return join(workspacePath, '.moi', '.cache', 'thumbnails')
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
async function loadIndex(dir: string): Promise<StoredAppletThumbnail[]> {
|
|
48
|
+
try {
|
|
49
|
+
const parsed: unknown = JSON.parse(await Bun.file(join(dir, INDEX_FILE)).text())
|
|
50
|
+
if (Array.isArray(parsed)) return parsed as StoredAppletThumbnail[]
|
|
51
|
+
} catch {}
|
|
52
|
+
return []
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
// Write through a temp sibling + atomic rename so a crash mid-write never
|
|
56
|
+
// leaves a truncated index or image (same recipe as scratchpad assets).
|
|
57
|
+
async function writeAtomic(dir: string, name: string, data: string | Uint8Array): Promise<void> {
|
|
58
|
+
const tmp = join(dir, `.tmp-${crypto.randomUUID()}`)
|
|
59
|
+
await Bun.write(tmp, data)
|
|
60
|
+
await rename(tmp, join(dir, name))
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
async function saveIndex(dir: string, records: StoredAppletThumbnail[]): Promise<void> {
|
|
64
|
+
await writeAtomic(dir, INDEX_FILE, JSON.stringify(records, null, 2))
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export async function getAppletThumbnailRecords(workspacePath: string): Promise<AppletThumbnail[]> {
|
|
68
|
+
const records = await loadIndex(getThumbnailsDir(workspacePath))
|
|
69
|
+
return records.map(({ file: _file, ...record }) => record)
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// The widget and view capture passes can land concurrently; serialize the
|
|
73
|
+
// read-modify-write per workspace so neither batch clobbers the other's index.
|
|
74
|
+
const saveQueues = new Map<string, Promise<void>>()
|
|
75
|
+
|
|
76
|
+
function enqueue(workspacePath: string, task: () => Promise<void>): Promise<void> {
|
|
77
|
+
const next = (saveQueues.get(workspacePath) ?? Promise.resolve()).then(task, task)
|
|
78
|
+
saveQueues.set(workspacePath, next)
|
|
79
|
+
return next
|
|
80
|
+
}
|
|
81
|
+
|
|
82
|
+
// Merge a settled capture batch into the store. A data-URL image replaces the
|
|
83
|
+
// applet's file, null records a failed or skipped attempt while keeping any
|
|
84
|
+
// previous file, and an omitted image only updates visit recency.
|
|
85
|
+
export function saveAppletThumbnails(
|
|
86
|
+
workspacePath: string,
|
|
87
|
+
kind: AppletKind,
|
|
88
|
+
updates: AppletThumbnailBatch['thumbnails']
|
|
89
|
+
): Promise<void> {
|
|
90
|
+
return enqueue(workspacePath, async () => {
|
|
91
|
+
const dir = getThumbnailsDir(workspacePath)
|
|
92
|
+
await mkdir(dir, { recursive: true })
|
|
93
|
+
// The cache must be ignored the moment it can hold data — this is also the
|
|
94
|
+
// backfill path for workspaces scaffolded before `.moi/.gitignore` existed.
|
|
95
|
+
await ensureMoiGitignore(workspacePath)
|
|
96
|
+
const now = new Date().toISOString()
|
|
97
|
+
const records = await loadIndex(dir)
|
|
98
|
+
|
|
99
|
+
for (const update of updates) {
|
|
100
|
+
if (!isValidAppletId(update.id)) continue
|
|
101
|
+
const index = records.findIndex(record => record.kind === kind && record.id === update.id)
|
|
102
|
+
const previous = index >= 0 ? records[index] : undefined
|
|
103
|
+
|
|
104
|
+
if (update.image === undefined) {
|
|
105
|
+
if (previous) records[index] = { ...previous, viewedAt: now }
|
|
106
|
+
continue
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
let file = previous?.file
|
|
110
|
+
if (typeof update.image === 'string') {
|
|
111
|
+
const parsed = DATA_URL_RE.exec(update.image)
|
|
112
|
+
if (!parsed) continue
|
|
113
|
+
const next = `${kind}-${update.id}.${EXT_BY_MIME[parsed[1]]}`
|
|
114
|
+
await writeAtomic(dir, next, Buffer.from(parsed[2], 'base64'))
|
|
115
|
+
if (previous?.file && previous.file !== next) {
|
|
116
|
+
await rm(join(dir, previous.file), { force: true }).catch(() => {})
|
|
117
|
+
}
|
|
118
|
+
file = next
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const record: StoredAppletThumbnail = {
|
|
122
|
+
kind,
|
|
123
|
+
id: update.id,
|
|
124
|
+
revision: update.revision,
|
|
125
|
+
capturedAt: now,
|
|
126
|
+
viewedAt: now,
|
|
127
|
+
...(update.captureMs !== undefined && { captureMs: update.captureMs }),
|
|
128
|
+
...(file !== undefined && { file })
|
|
129
|
+
}
|
|
130
|
+
if (previous) records[index] = record
|
|
131
|
+
else records.push(record)
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
await saveIndex(dir, records)
|
|
135
|
+
})
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
// Drop records (and their image files) for applets of `kind` that no longer
|
|
139
|
+
// exist. Called from the build pipeline beside the stale-bundle prune, so a
|
|
140
|
+
// deleted applet's thumbnail dies with its bundle.
|
|
141
|
+
export function pruneAppletThumbnails(
|
|
142
|
+
workspacePath: string,
|
|
143
|
+
kind: AppletKind,
|
|
144
|
+
keepIds: readonly string[]
|
|
145
|
+
): Promise<void> {
|
|
146
|
+
return enqueue(workspacePath, async () => {
|
|
147
|
+
const dir = getThumbnailsDir(workspacePath)
|
|
148
|
+
const records = await loadIndex(dir)
|
|
149
|
+
const keep = new Set(keepIds)
|
|
150
|
+
const stale = records.filter(record => record.kind === kind && !keep.has(record.id))
|
|
151
|
+
if (stale.length === 0) return
|
|
152
|
+
for (const record of stale) {
|
|
153
|
+
if (record.file) await rm(join(dir, record.file), { force: true }).catch(() => {})
|
|
154
|
+
}
|
|
155
|
+
await saveIndex(
|
|
156
|
+
dir,
|
|
157
|
+
records.filter(record => !stale.includes(record))
|
|
158
|
+
)
|
|
159
|
+
})
|
|
160
|
+
}
|
|
161
|
+
|
|
162
|
+
// Records that actually have an image on disk, for the home-card preview.
|
|
163
|
+
export async function getCapturedThumbnails(
|
|
164
|
+
workspacePath: string
|
|
165
|
+
): Promise<{ kind: AppletKind; id: string; viewedAt: string }[]> {
|
|
166
|
+
const records = await loadIndex(getThumbnailsDir(workspacePath))
|
|
167
|
+
return records
|
|
168
|
+
.filter(record => record.file !== undefined)
|
|
169
|
+
.map(({ kind, id, viewedAt }) => ({ kind, id, viewedAt }))
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
// One thumbnail image. Same caching story as applet bundles: strong ETag from
|
|
173
|
+
// size+mtime with `private, no-cache`, so an unchanged file costs a bodyless
|
|
174
|
+
// 304 and a re-capture busts every consumer on the next revalidation.
|
|
175
|
+
export async function serveAppletThumbnail(
|
|
176
|
+
workspacePath: string,
|
|
177
|
+
kind: AppletKind,
|
|
178
|
+
id: string,
|
|
179
|
+
ifNoneMatch?: string | null
|
|
180
|
+
): Promise<Response> {
|
|
181
|
+
if (!isValidAppletId(id)) return new Response('Invalid id', { status: 400 })
|
|
182
|
+
const dir = getThumbnailsDir(workspacePath)
|
|
183
|
+
const records = await loadIndex(dir)
|
|
184
|
+
const record = records.find(item => item.kind === kind && item.id === id)
|
|
185
|
+
if (!record?.file) return new Response('Not found', { status: 404 })
|
|
186
|
+
const file = Bun.file(join(dir, record.file))
|
|
187
|
+
if (!(await file.exists())) return new Response('Not found', { status: 404 })
|
|
188
|
+
|
|
189
|
+
const etag = `"${file.size}-${Math.trunc(file.lastModified)}"`
|
|
190
|
+
const headers = {
|
|
191
|
+
ETag: etag,
|
|
192
|
+
'Cache-Control': 'private, no-cache',
|
|
193
|
+
'Content-Type': MIME_BY_EXT[record.file.split('.').pop() ?? ''] ?? 'application/octet-stream'
|
|
194
|
+
}
|
|
195
|
+
if (ifNoneMatch === etag) return new Response(null, { status: 304, headers })
|
|
196
|
+
return new Response(file, { headers })
|
|
197
|
+
}
|
package/server/views.ts
CHANGED
|
@@ -3,7 +3,14 @@ import { existsSync } from 'node:fs'
|
|
|
3
3
|
import type { ViewConfig, ViewInfo } from '@/lib/types'
|
|
4
4
|
|
|
5
5
|
import { syncAppletLogAfterBuild } from './applet-log'
|
|
6
|
-
import {
|
|
6
|
+
import {
|
|
7
|
+
buildApplets,
|
|
8
|
+
getAppletPaths,
|
|
9
|
+
getAppletRevision,
|
|
10
|
+
listBuilt,
|
|
11
|
+
scanSources,
|
|
12
|
+
serveApplet
|
|
13
|
+
} from './applets'
|
|
7
14
|
import { reloadModules } from './functions'
|
|
8
15
|
import { markViewBuilderReady } from './view-builders'
|
|
9
16
|
|
|
@@ -106,8 +113,10 @@ export async function getViewList(workspacePath: string): Promise<ViewInfo[]> {
|
|
|
106
113
|
const raw = manifest.config[id] ?? {}
|
|
107
114
|
const icon = typeof raw.icon === 'string' && raw.icon ? raw.icon : undefined
|
|
108
115
|
const requiredEnv = Array.isArray(raw.requiredEnv) ? raw.requiredEnv : undefined
|
|
116
|
+
const revision = getAppletRevision(workspacePath, 'view', id)
|
|
109
117
|
return {
|
|
110
118
|
id,
|
|
119
|
+
...(revision ? { revision } : {}),
|
|
111
120
|
config: {
|
|
112
121
|
title: raw.title || id,
|
|
113
122
|
...(icon ? { icon } : {}),
|
package/server/widgets.ts
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
|
-
import { existsSync
|
|
2
|
-
import { join } from 'path'
|
|
1
|
+
import { existsSync } from 'node:fs'
|
|
3
2
|
|
|
4
3
|
import type { WidgetConfig, WidgetInfo } from '@/lib/types'
|
|
5
4
|
|
|
6
5
|
import { syncAppletLogAfterBuild } from './applet-log'
|
|
7
|
-
import { buildApplets, getAppletPaths, listBuilt, serveApplet } from './applets'
|
|
6
|
+
import { buildApplets, getAppletPaths, getAppletRevision, listBuilt, serveApplet } from './applets'
|
|
8
7
|
import { reloadModules } from './functions'
|
|
9
8
|
|
|
10
9
|
const DEFAULT_CONFIG: WidgetConfig = { rowSpan: 1, colSpan: 2 }
|
|
@@ -79,34 +78,21 @@ export function listBuiltWidgets(workspacePath: string): Promise<string[]> {
|
|
|
79
78
|
return listBuilt(buildDir)
|
|
80
79
|
}
|
|
81
80
|
|
|
82
|
-
// Content tag of a widget's built bundle: size+mtime of its entry, the same
|
|
83
|
-
// recipe as the serve ETag. Changes on every rebundle, so clients can use it
|
|
84
|
-
// to invalidate cached artifacts (widget thumbnails).
|
|
85
|
-
function widgetTag(buildDir: string, id: string): string | undefined {
|
|
86
|
-
try {
|
|
87
|
-
const stat = statSync(join(buildDir, id, 'index.js'))
|
|
88
|
-
return `${stat.size}-${Math.trunc(stat.mtimeMs)}`
|
|
89
|
-
} catch {
|
|
90
|
-
return undefined
|
|
91
|
-
}
|
|
92
|
-
}
|
|
93
|
-
|
|
94
81
|
async function getWidgetList(workspacePath: string): Promise<WidgetInfo[]> {
|
|
95
82
|
const [names, manifest] = await Promise.all([
|
|
96
83
|
listBuiltWidgets(workspacePath),
|
|
97
84
|
readManifest(workspacePath)
|
|
98
85
|
])
|
|
99
|
-
const { buildDir } = getAppletPaths(workspacePath, 'widget')
|
|
100
86
|
return names.map(id => {
|
|
101
87
|
const raw = manifest[id] ?? {}
|
|
102
88
|
const rowSpan = VALID_SPANS.includes(raw.rowSpan) ? raw.rowSpan : DEFAULT_CONFIG.rowSpan
|
|
103
89
|
const colSpan = VALID_SPANS.includes(raw.colSpan) ? raw.colSpan : DEFAULT_CONFIG.colSpan
|
|
104
90
|
const requiredEnv = Array.isArray(raw.requiredEnv) ? raw.requiredEnv : undefined
|
|
105
|
-
const
|
|
91
|
+
const revision = getAppletRevision(workspacePath, 'widget', id)
|
|
106
92
|
return {
|
|
107
93
|
id,
|
|
108
94
|
config: { rowSpan, colSpan, ...(requiredEnv ? { requiredEnv } : {}) } as WidgetConfig,
|
|
109
|
-
...(
|
|
95
|
+
...(revision ? { revision } : {})
|
|
110
96
|
}
|
|
111
97
|
})
|
|
112
98
|
}
|
|
@@ -90,6 +90,15 @@ my-agent-folder/
|
|
|
90
90
|
.scratchpad/ <- Scratchpad image files. Internal — pull pixels via `moi scratch read-image`, never open it.
|
|
91
91
|
```
|
|
92
92
|
|
|
93
|
+
Every dot-prefixed file or folder inside `.moi/` (`.build/`, `.cache/`, `.workspace.json`,
|
|
94
|
+
`.scratchpad*`, `.gitignore`, …) is a moi internal: auto-generated and liable to change format
|
|
95
|
+
without notice. Avoid them as much as possible — do not read, edit, or delete them, and never point
|
|
96
|
+
tooling at them; go through the `moi` CLI instead. Version control needs no special handling: the
|
|
97
|
+
scaffolded `.moi/.gitignore` already excludes the machine-local entries (`.build/`, `.cache/`,
|
|
98
|
+
`node_modules/`), while `.workspace.json` and `.scratchpad.json` are workspace state that ships
|
|
99
|
+
with the repo — commit them as-is, just never hand-edit them. Your surface is the non-dot files:
|
|
100
|
+
`widgets/`, `views/`, `package.json`, and code you place under `.moi/` yourself.
|
|
101
|
+
|
|
93
102
|
# Build environment
|
|
94
103
|
|
|
95
104
|
- Bun is the required dependency of moi, so it must be installed
|
|
@@ -338,6 +347,9 @@ After the final successful bundle and any checks, always make tab focus the fina
|
|
|
338
347
|
- After building or editing a view, run `moi tab focus view:<view-id>`, using its file name or claimed
|
|
339
348
|
builder id.
|
|
340
349
|
|
|
350
|
+
The focused applet is the handoff. Keep the final reply brief and user-facing. Do not include file
|
|
351
|
+
or storage links, file paths, or bundle, test, and runtime-log summaries.
|
|
352
|
+
|
|
341
353
|
# Debugging applets
|
|
342
354
|
|
|
343
355
|
`moi bundle` only proves an applet compiles — it can still fail to load in the browser, crash on
|
|
@@ -406,4 +418,4 @@ This skill is installed with moi (via the CLI or the UI) and can fall behind whe
|
|
|
406
418
|
- **Then** — if you updated, mention it.
|
|
407
419
|
|
|
408
420
|
<!-- moi skill version marker — read by `moi skill` to detect drift; do not edit by hand -->
|
|
409
|
-
<moi-skill version="0.
|
|
421
|
+
<moi-skill version="0.13.0" />
|