moi-computer 0.2.0 → 0.3.0-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/README.md CHANGED
@@ -1,7 +1,7 @@
1
1
  ```
2
- ██████ ██████ ██
3
- ██░░██ ██░░██ ██
4
- ██░░██ ██████ ██
2
+ ██████████ ██████ ██
3
+ ██░░██░░██ ██░░██ ██
4
+ ██░░██░░██ ██████ ██
5
5
  ```
6
6
 
7
7
  A local workspace where AI agents build their own UI.
@@ -17,7 +17,7 @@ moi start # http://localhost:13337
17
17
  Or paste this into Claude Code and it will set everything up for you:
18
18
 
19
19
  ```
20
- Set up the MOI workspace for this project. Fetch https://moi.computer/CC-INSTALL.md, and follow the steps.
20
+ Set up the moi workspace for this project. Fetch https://moi.computer/CC-INSTALL.md, and follow the steps.
21
21
  ```
22
22
 
23
23
  Hacking on it:
@@ -3,6 +3,7 @@ import { Fragment, type ReactElement, useCallback, useEffect, useRef, useState }
3
3
  import {
4
4
  type Editor,
5
5
  type StyleProp,
6
+ type TLAssetStore,
6
7
  type TLComponents,
7
8
  type TLDefaultColorStyle,
8
9
  type TLDefaultDashStyle,
@@ -85,6 +86,34 @@ function makeExecutor(editor: Editor) {
85
86
  }
86
87
  }
87
88
 
89
+ // File-backed assets: without this, tldraw inlines every pasted/dropped image as
90
+ // a base64 data URL inside the document — megabytes re-serialized into
91
+ // `.moi/.scratchpad.json` on every autosave and shipped over every GET/PUT.
92
+ // Instead `upload` POSTs the bytes once and stores a tiny `asset:<file>` src on
93
+ // the record; `resolve` maps it back to the serving URL at render time. Legacy
94
+ // snapshots still holding data URLs pass through `resolve` untouched (the
95
+ // server extracts them to files on the next save). See server/scratchpad-assets.ts.
96
+ function makeAssetStore(workspaceId: string): TLAssetStore {
97
+ const base = `/api/workspaces/${workspaceId}/scratchpad/assets`
98
+ return {
99
+ async upload(_asset, file) {
100
+ const res = await fetch(base, {
101
+ method: 'POST',
102
+ headers: { 'Content-Type': file.type || 'application/octet-stream' },
103
+ body: file
104
+ })
105
+ if (!res.ok) throw new Error(`Upload failed: ${await res.text()}`)
106
+ const { src } = (await res.json()) as { src: string }
107
+ return { src }
108
+ },
109
+ resolve(asset) {
110
+ const src = asset.props.src
111
+ if (src?.startsWith('asset:')) return `${base}/${src.slice('asset:'.length)}`
112
+ return src
113
+ }
114
+ }
115
+ }
116
+
88
117
  // --- Scratchpad UI shell --------------------------------------------------
89
118
  // We replace tldraw's full UI with a curated subset (see docs/moi-scratchpad.md
90
119
  // for the prioritization). Backbone v1: a custom vertical tool bar on the left,
@@ -128,6 +157,15 @@ const SCRATCH_OVERRIDES: TLUiOverrides = {
128
157
  // One page only — disables the page selector and all multi-page UI.
129
158
  const SCRATCH_OPTIONS = { maxPages: 1 }
130
159
 
160
+ // Browser drag/drop/paste limits (tldraw defaults: reject >10MB, no rescale). We
161
+ // accept larger drops and rescale big images to fit so a phone-sized photo lands
162
+ // as a lightweight asset instead of bloating the snapshot's sidecar files. The
163
+ // size check runs BEFORE the rescale, so a 32MB drop is admitted and then shrunk.
164
+ // (The agent's `moi scratch add image` path has its own presets — see the
165
+ // executor's IMAGE_PRESETS; this only governs what the browser accepts.)
166
+ const MAX_DROP_BYTES = 32 * 1024 * 1024
167
+ const MAX_IMAGE_DIMENSION = 2048
168
+
131
169
  type IconC = typeof IconPointer2
132
170
 
133
171
  // The curated left-bar entries, grouped by `sep` dividers. `tool` selects a
@@ -748,6 +786,14 @@ export function Scratchpad() {
748
786
  // Reactive handle to the mounted editor, used to render the custom tool bar.
749
787
  const [editor, setEditor] = useState<Editor | null>(null)
750
788
  const { loaded, snapshot, skew, flagSkew } = useScratchpadSnapshot(workspaceId)
789
+ // Stable identity per workspace, held in a ref rather than useMemo (which React
790
+ // may discard): a fresh `assets` identity makes <Tldraw> rebuild its store and
791
+ // remount the editor, dropping unsaved edits and resetting the camera.
792
+ const assetStoreRef = useRef<{ id: string; store: TLAssetStore } | null>(null)
793
+ if (assetStoreRef.current?.id !== workspaceId) {
794
+ assetStoreRef.current = { id: workspaceId, store: makeAssetStore(workspaceId) }
795
+ }
796
+ const assetStore = assetStoreRef.current.store
751
797
 
752
798
  const save = useCallback(() => {
753
799
  const editor = editorRef.current
@@ -865,11 +911,16 @@ export function Scratchpad() {
865
911
  <div className="absolute inset-0">
866
912
  <Tldraw
867
913
  snapshot={snapshot}
914
+ assets={assetStore}
868
915
  licenseKey={LICENSE_KEY}
869
916
  onMount={onMount}
870
917
  components={SCRATCH_COMPONENTS}
871
918
  overrides={SCRATCH_OVERRIDES}
872
919
  options={SCRATCH_OPTIONS}
920
+ // Accept drops up to 32MB and rescale big images to fit (tldraw checks
921
+ // the size before rescaling, so a large photo is admitted then shrunk).
922
+ maxAssetSize={MAX_DROP_BYTES}
923
+ maxImageDimension={MAX_IMAGE_DIMENSION}
873
924
  // Hand focus control entirely to us: tldraw's own `autoFocus` only seeds
874
925
  // the `isFocused` flag (hotkeys would fire window-wide, even from the
875
926
  // chat) without ever DOM-focusing the canvas. Instead onMount focuses
@@ -7,8 +7,7 @@ import {
7
7
  IconLayoutSidebar,
8
8
  IconLayoutSidebarLeftCollapse,
9
9
  IconLayoutSidebarLeftExpand,
10
- IconPlus,
11
- IconSettings
10
+ IconPlus
12
11
  } from '@tabler/icons-react'
13
12
  import { useQueryClient } from '@tanstack/react-query'
14
13
  import { Link } from 'wouter'
@@ -216,16 +215,6 @@ function Sidebar({ collapsed, workspaces, logoEffect }: SidebarProps) {
216
215
 
217
216
  {/* Footer */}
218
217
  <div className="flex items-center justify-between gap-2 py-3">
219
- <button
220
- type="button"
221
- className={cn(
222
- 'flex h-8 items-center gap-2 rounded-sm pl-1.5 text-sm text-foreground hover:bg-foreground/[0.05] hover:text-foreground',
223
- collapsed ? 'pr-1.5' : 'pr-2.5'
224
- )}
225
- >
226
- <IconSettings size={18} className="shrink-0" />
227
- {!collapsed && <span>Settings</span>}
228
- </button>
229
218
  {/* Temporarily hidden */}
230
219
  <button
231
220
  type="button"
@@ -1,5 +1,6 @@
1
1
  import { useEffect, useRef } from 'react'
2
2
 
3
+ import { wsUrl } from '@/client/lib/ws-url'
3
4
  import type { ViewInfo, WidgetInfo } from '@/lib/types'
4
5
 
5
6
  export type MeiEvent =
@@ -26,7 +27,7 @@ function ensureConnection() {
26
27
  if (ws || connecting) return
27
28
  connecting = true
28
29
 
29
- const socket = new WebSocket(`ws://${location.host}/api/workspaces/ws`)
30
+ const socket = new WebSocket(wsUrl('/api/workspaces/ws'))
30
31
 
31
32
  socket.onopen = () => {
32
33
  ws = socket
@@ -2,6 +2,7 @@ import type { QueryClient } from '@tanstack/react-query'
2
2
 
3
3
  import { workspaceKeys } from '@/client/api/workspaces'
4
4
  import { getScratchExecutor } from '@/client/lib/scratch-executor'
5
+ import { wsUrl } from '@/client/lib/ws-url'
5
6
  import { liveStore } from '@/client/store/live'
6
7
  import { applyEvent } from '@/lib/format'
7
8
  import type {
@@ -55,7 +56,7 @@ function connect() {
55
56
  reconnectTimer = null
56
57
  }
57
58
 
58
- const s = new WebSocket(`ws://${location.host}/ws`)
59
+ const s = new WebSocket(wsUrl('/ws'))
59
60
  socket = s
60
61
 
61
62
  s.onopen = () => {
@@ -0,0 +1,10 @@
1
+ // Build a WebSocket URL for a same-origin path, deriving the scheme from the
2
+ // page protocol: `wss://` when the page is served over HTTPS, `ws://` otherwise.
3
+ // Hardcoding `ws://` breaks live updates behind any TLS-terminating proxy
4
+ // (Cloudflare Tunnel, nginx, ngrok) — the browser blocks the insecure socket as
5
+ // mixed content. The proxy forwards `wss://` to the origin's plain `ws://`, so
6
+ // no server change is needed.
7
+ export function wsUrl(path: string) {
8
+ const scheme = location.protocol === 'https:' ? 'wss' : 'ws'
9
+ return `${scheme}://${location.host}${path}`
10
+ }