moi-computer 0.1.4 → 0.2.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 CHANGED
@@ -1,71 +1,45 @@
1
- # moi
2
-
3
- Local AI workspace powered by the Claude Agent SDK, Bun, React, and Tailwind.
4
-
5
- > Published on npm as **`moi-computer`**; the installed command is **`moi`**.
6
-
7
- > **Requires [Bun](https://bun.sh).** moi is Bun-only — it uses `Bun.serve`, the
8
- > Bun bundler, and other Bun APIs. Running it under Node prints an install hint
9
- > and exits.
1
+ ```
2
+ ██████ ██████ ██
3
+ ██░░██ ██░░██ ██
4
+ ██░░██ ██████ ██
5
+ ```
10
6
 
11
- ## Install
7
+ A local workspace where AI agents build their own UI.
8
+ See [moi.computer](https://moi.computer) for what it is and how it works.
12
9
 
13
- Install globally so the `moi` command is always on your PATH — the agent calls
14
- `moi` from your workspaces, so this is the recommended setup:
10
+ Needs [Bun](https://bun.sh). The npm package is `moi-computer`, the command is `moi`:
15
11
 
16
12
  ```sh
17
13
  bun i -g moi-computer
18
- moi start
14
+ moi start # http://localhost:13337
19
15
  ```
20
16
 
21
- If you don't have Bun yet:
17
+ Or paste this into Claude Code and it will set everything up for you:
22
18
 
23
- ```sh
24
- curl -fsSL https://bun.sh/install | bash
25
19
  ```
26
-
27
- You can also run it once without installing:
28
-
29
- ```sh
30
- bunx moi-computer init [dir]
20
+ Set up the MOI workspace for this project. Fetch https://moi.computer/CC-INSTALL.md, and follow the steps.
31
21
  ```
32
22
 
33
- `bunx` works for a single invocation but leaves no persistent `moi` command, so
34
- it prints a reminder to install globally.
35
-
36
- ## Usage
37
-
38
- Initialize a workspace directory:
23
+ Hacking on it:
39
24
 
40
25
  ```sh
41
- moi init [dir] # scaffold .claude/skills into dir (default: .)
42
- moi init [dir] --web # also start the server and open the browser
43
- ```
44
-
45
- Start the web server:
46
-
47
- ```sh
48
- moi start # http://localhost:13337
49
- moi start --port=4000 # custom port
26
+ bun install
27
+ bun run dev
50
28
  ```
51
29
 
52
- Other commands:
30
+ ## OpenClaw (experimental)
53
31
 
54
- ```sh
55
- moi bundle [dir] # rebuild changed widgets
56
- moi refresh # refresh widget data without rebuilding
57
- moi theme [dir] # show or set font/color themes (--font=<key> --color=<key>)
58
- moi status # show server status and registered workspaces
59
- moi openclaw init # install moi skills into an OpenClaw agent workspace
60
- ```
32
+ moi also works with OpenClaw agents — `moi openclaw init` installs the moi
33
+ skills into an OpenClaw agent workspace. Integration notes live in
34
+ [docs/OPENCLAW.md](docs/OPENCLAW.md).
61
35
 
62
- ## Development (from source)
36
+ ## No crypto token
63
37
 
64
- ```sh
65
- bun install
66
- bun run dev # watch-and-restart dev server on port 13337
67
- ```
38
+ moi has **no** cryptocurrency, token, coin, or NFT — official or otherwise —
39
+ on any chain, and never will. Any token using the moi name, logo, or the
40
+ maintainer's name (on pump.fun, Solana, or anywhere else) is an unauthorized
41
+ scam with no affiliation to, endorsement from, or benefit to this project.
42
+ The maintainer will never announce, endorse, or accept proceeds from a token.
43
+ Don't buy it, and report it to the platform where it's listed.
68
44
 
69
- `bun run dev` starts a supervisor that hot-reloads the frontend in place and
70
- full-restarts the server on changes to `server/` or `lib/`. Do not run the
71
- server with `bun --hot` — see `CLAUDE.md` for why.
45
+ License: [Elastic 2.0](LICENSE).
@@ -3,7 +3,6 @@ import { useMutation, useQuery, useQueryClient } from '@tanstack/react-query'
3
3
  import { applyEvents } from '@/lib/format'
4
4
  import type {
5
5
  DiscoveredWorkspace,
6
- EnvScope,
7
6
  McpServer,
8
7
  SessionInfo,
9
8
  StreamEvent,
@@ -242,7 +241,7 @@ export function useSaveLayout(workspaceId: string) {
242
241
  })
243
242
  }
244
243
 
245
- // A workspace's effective env view (masked values + scopes + discovered `.env`).
244
+ // A workspace's effective env view (masked values + discovered `.env`).
246
245
  export function useWorkspaceEnv(workspaceId: string) {
247
246
  return useQuery<WorkspaceEnvView>({
248
247
  queryKey: workspaceKeys.env(workspaceId),
@@ -251,12 +250,11 @@ export function useWorkspaceEnv(workspaceId: string) {
251
250
  })
252
251
  }
253
252
 
254
- // Patch of a workspace's env: set/remove custom secrets, change scopes, or
255
- // toggle `.env` inheritance. The PUT returns the fresh view (see useUpdateEnv).
253
+ // Patch of a workspace's env: set/remove custom secrets, or toggle `.env`
254
+ // inheritance. The PUT returns the fresh view (see useUpdateEnv).
256
255
  export type EnvPatch = {
257
256
  set?: Record<string, string>
258
257
  remove?: string[]
259
- scopes?: Record<string, EnvScope>
260
258
  inheritDotenv?: boolean
261
259
  }
262
260
 
@@ -9,6 +9,7 @@ import {
9
9
  type TLDefaultFillStyle,
10
10
  type TLDefaultSizeStyle,
11
11
  type TLEditorSnapshot,
12
+ type TLStore,
12
13
  type TLUiOverrides,
13
14
  DefaultColorStyle,
14
15
  DefaultDashStyle,
@@ -19,6 +20,9 @@ import {
19
20
  DefaultVerticalAlignStyle,
20
21
  GeoShapeGeoStyle,
21
22
  Tldraw,
23
+ createTLStore,
24
+ defaultBindingUtils,
25
+ defaultShapeUtils,
22
26
  getSnapshot,
23
27
  loadSnapshot,
24
28
  react
@@ -35,14 +39,16 @@ import {
35
39
  IconSketching,
36
40
  IconSquare,
37
41
  IconSticker2,
38
- IconTypography
42
+ IconTypography,
43
+ IconVersions
39
44
  } from '@tabler/icons-react'
40
45
 
41
46
  import { cn } from '@/client/lib/cn'
42
47
  import { useWorkspaceId } from '@/client/lib/WorkspaceContext'
43
48
  import { setScratchExecutor } from '@/client/lib/scratch-executor'
44
49
  import { type MeiEvent, useMeiEvent } from '@/client/hooks/useMeiEvents'
45
- import type { ScratchOp, ScratchOpResult } from '@/lib/types'
50
+ import { describeNewerWriter, sequencesAhead } from '@/lib/scratchpad-skew'
51
+ import type { ScratchOp, ScratchOpResult, ScratchpadWriter } from '@/lib/types'
46
52
 
47
53
  // Identifies this tab's writes so it can ignore the `scratchpad:updated` echo of
48
54
  // its own save (see the MEI reload below). Per page load.
@@ -595,28 +601,86 @@ function ScratchStyleBar({ editor }: ScratchStyleBarProps) {
595
601
  )
596
602
  }
597
603
 
604
+ // --- Version-skew pre-flight ------------------------------------------------
605
+ // tldraw snapshots migrate forward only: a canvas saved by a newer tldraw can
606
+ // never load here, and handing it to <Tldraw> anyway lands on tldraw's crash
607
+ // screen — whose main button is a destructive "Reset data". So every fetched
608
+ // document is pre-flighted against this bundle's schema first; on failure we
609
+ // render a friendly read-only notice and never mount the editor, which is what
610
+ // guarantees this stale client can't autosave over the newer file. See
611
+ // lib/scratchpad-skew.ts and docs/moi-scratchpad.md § Version skew.
612
+
613
+ // What GET /api/workspaces/:id/scratchpad returns (see server/scratchpad.ts).
614
+ type ScratchpadFetch = {
615
+ document: TLEditorSnapshot['document'] | null
616
+ writer?: ScratchpadWriter
617
+ }
618
+
619
+ // Why the canvas can't be shown: a newer writer (version skew) or a snapshot
620
+ // that fails to load for some other reason. Either way the file is left alone.
621
+ type ScratchpadSkew = { newer: boolean; writer?: ScratchpadWriter; detail: string }
622
+
623
+ // This bundle's schema, built once and lazily — the same store config the
624
+ // editor and the server executor use, so the pre-flight verdict matches what
625
+ // <Tldraw> would do.
626
+ let runtimeSchemaCache: TLStore['schema'] | null = null
627
+ function runtimeSchema(): TLStore['schema'] {
628
+ if (!runtimeSchemaCache) {
629
+ runtimeSchemaCache = createTLStore({
630
+ shapeUtils: defaultShapeUtils,
631
+ bindingUtils: defaultBindingUtils
632
+ }).schema
633
+ }
634
+ return runtimeSchemaCache
635
+ }
636
+
637
+ // Dry-run the migration <Tldraw>/`loadSnapshot` would perform. Returns null when
638
+ // the document is safe to mount, else the skew to display.
639
+ function detectSkew(
640
+ document: NonNullable<TLEditorSnapshot['document']>,
641
+ writer: ScratchpadWriter | undefined
642
+ ): ScratchpadSkew | null {
643
+ const schema = runtimeSchema()
644
+ try {
645
+ if (schema.migrateStoreSnapshot(document).type === 'success') return null
646
+ } catch {}
647
+ const ahead = sequencesAhead(document.schema, schema.serialize().sequences)
648
+ if (ahead.length > 0) return { newer: true, writer, detail: describeNewerWriter(writer, ahead) }
649
+ return { newer: false, writer, detail: 'The saved snapshot failed to load.' }
650
+ }
651
+
598
652
  // Hydrate the saved canvas once per workspace from the REST snapshot endpoint.
599
653
  // Returns a STABLE snapshot reference (held in a ref, never a fresh object per
600
654
  // render): tldraw rebuilds its entire store whenever the `snapshot` prop identity
601
655
  // changes, so handing it a new object each render loops it forever — remounting the
602
656
  // store and re-fetching fonts/translations endlessly. `document: null` → empty
603
- // canvas; `loaded` gates the placeholder until the fetch settles.
657
+ // canvas; `loaded` gates the placeholder until the fetch settles. A document that
658
+ // fails the pre-flight sets `skew` instead of the snapshot; `flagSkew` lets the
659
+ // remote-reload path flip the same state mid-session.
604
660
  function useScratchpadSnapshot(workspaceId: string): {
605
661
  loaded: boolean
606
662
  snapshot: Partial<TLEditorSnapshot> | undefined
663
+ skew: ScratchpadSkew | null
664
+ flagSkew: (skew: ScratchpadSkew) => void
607
665
  } {
608
666
  const [loaded, setLoaded] = useState(false)
667
+ const [skew, setSkew] = useState<ScratchpadSkew | null>(null)
609
668
  const snapshot = useRef<Partial<TLEditorSnapshot> | undefined>(undefined)
610
669
 
611
670
  useEffect(() => {
612
671
  let cancelled = false
613
672
  setLoaded(false)
673
+ setSkew(null)
614
674
  snapshot.current = undefined
615
675
  fetch(`/api/workspaces/${workspaceId}/scratchpad`)
616
676
  .then(r => r.json())
617
- .then((d: { document: TLEditorSnapshot['document'] | null }) => {
677
+ .then((d: ScratchpadFetch) => {
618
678
  if (cancelled) return
619
- if (d?.document) snapshot.current = { document: d.document }
679
+ if (d?.document) {
680
+ const found = detectSkew(d.document, d.writer)
681
+ if (found) setSkew(found)
682
+ else snapshot.current = { document: d.document }
683
+ }
620
684
  setLoaded(true)
621
685
  })
622
686
  .catch(() => {
@@ -627,7 +691,41 @@ function useScratchpadSnapshot(workspaceId: string): {
627
691
  }
628
692
  }, [workspaceId])
629
693
 
630
- return { loaded, snapshot: snapshot.current }
694
+ return { loaded, snapshot: snapshot.current, skew, flagSkew: setSkew }
695
+ }
696
+
697
+ type ScratchpadSkewNoticeProps = { skew: ScratchpadSkew }
698
+
699
+ // The read-only stand-in for an unloadable canvas. Deliberately mounts no
700
+ // editor and offers no "load anyway": with no store there is nothing to
701
+ // autosave, so the newer file on disk stays byte-for-byte intact.
702
+ function ScratchpadSkewNotice({ skew }: ScratchpadSkewNoticeProps) {
703
+ return (
704
+ <div className="flex min-h-0 flex-1 items-center justify-center bg-muted/40 bg-[radial-gradient(var(--color-border)_1px,transparent_1px)] [background-size:16px_16px] p-6">
705
+ <div className="flex max-w-md animate-in flex-col gap-3 rounded-md bg-background p-6 shadow-xs duration-200 fade-in-0 zoom-in-95">
706
+ <div className="flex items-center gap-2">
707
+ <IconVersions size={20} stroke={1.5} className="shrink-0 text-amber-600" />
708
+ <h2 className="font-medium text-foreground">
709
+ {skew.newer ? 'This canvas needs a newer moi' : 'This canvas couldn’t be loaded'}
710
+ </h2>
711
+ </div>
712
+ {skew.newer ? (
713
+ <>
714
+ <p className="text-sm text-pretty text-muted-foreground">
715
+ This scratchpad was saved by a newer version of moi
716
+ {skew.writer ? ` (v${skew.writer.moi})` : ''}.<br /> Please update moi and restart the
717
+ server:
718
+ </p>
719
+ <code className="self-start rounded bg-muted px-2 py-1 font-mono text-xs text-foreground">
720
+ bun install -g moi-computer@latest
721
+ </code>
722
+ </>
723
+ ) : (
724
+ <p className="text-sm text-muted-foreground">{skew.detail}</p>
725
+ )}
726
+ </div>
727
+ </div>
728
+ )
631
729
  }
632
730
 
633
731
  // The Scratchpad surface: a real tldraw editor, hydrated from and autosaved to
@@ -649,7 +747,7 @@ export function Scratchpad() {
649
747
  const applyingRemote = useRef(false)
650
748
  // Reactive handle to the mounted editor, used to render the custom tool bar.
651
749
  const [editor, setEditor] = useState<Editor | null>(null)
652
- const { loaded, snapshot } = useScratchpadSnapshot(workspaceId)
750
+ const { loaded, snapshot, skew, flagSkew } = useScratchpadSnapshot(workspaceId)
653
751
 
654
752
  const save = useCallback(() => {
655
753
  const editor = editorRef.current
@@ -668,16 +766,26 @@ export function Scratchpad() {
668
766
  }, [workspaceId])
669
767
 
670
768
  // A remote save (another tab, or an agent draw landing in another tab) — pull
671
- // the new snapshot and load it into the live store. Skip our own echo.
769
+ // the new snapshot and load it into the live store. Skip our own echo. The
770
+ // fetched document is pre-flighted like the initial load: if the server was
771
+ // swapped for a newer moi mid-session, `loadSnapshot` here would throw — flip
772
+ // the skew notice instead (unmounting the editor, which also stops the
773
+ // autosave timer) so this stale tab can't save over the newer file.
672
774
  useMeiEvent((e: MeiEvent) => {
673
775
  if (e.type !== 'scratchpad:updated' || e.workspaceId !== workspaceId) return
674
776
  if (e.origin && e.origin === ORIGIN_ID) return
675
777
  if (!editorRef.current) return
676
778
  fetch(`/api/workspaces/${workspaceId}/scratchpad`)
677
779
  .then(r => r.json())
678
- .then((d: { document: TLEditorSnapshot['document'] | null }) => {
780
+ .then((d: ScratchpadFetch) => {
679
781
  const editor = editorRef.current
680
782
  if (!editor || !d?.document) return
783
+ const found = detectSkew(d.document, d.writer)
784
+ if (found) {
785
+ if (saveTimer.current) clearTimeout(saveTimer.current)
786
+ flagSkew(found)
787
+ return
788
+ }
681
789
  applyingRemote.current = true
682
790
  loadSnapshot(editor.store, { document: d.document })
683
791
  })
@@ -750,6 +858,7 @@ export function Scratchpad() {
750
858
  )
751
859
 
752
860
  if (!loaded) return <div className="min-h-0 flex-1 bg-muted/40" />
861
+ if (skew) return <ScratchpadSkewNotice skew={skew} />
753
862
 
754
863
  return (
755
864
  <div ref={rootRef} className="relative min-h-0 flex-1 overflow-hidden">
@@ -1,21 +1,15 @@
1
1
  import { type ReactNode, useState } from 'react'
2
2
 
3
- import { IconChevronDown, IconLoader2, IconPlus, IconTrash } from '@tabler/icons-react'
3
+ import { IconLoader2, IconPlus, IconTrash } from '@tabler/icons-react'
4
4
 
5
- import { useUpdateEnv, useSaveWorkspaceName, useWorkspaceEnv } from '@/client/api/workspaces'
6
- import {
7
- DropdownMenu,
8
- DropdownMenuContent,
9
- DropdownMenuRadioGroup,
10
- DropdownMenuRadioItem,
11
- DropdownMenuTrigger
12
- } from '@/client/components/ui/dropdown-menu'
5
+ import { useSaveWorkspaceName } from '@/client/api/workspaces'
6
+ import { useEnvVars } from '@/client/hooks/useEnvVars'
13
7
  import { Button } from '@/client/components/ui/button'
14
8
  import { Input } from '@/client/components/ui/input'
15
9
  import { Switch } from '@/client/components/ui/switch'
16
10
  import { useWorkspaceLayoutCtx } from '@/client/lib/WorkspaceLayoutContext'
17
11
  import { cn } from '@/client/lib/cn'
18
- import type { EnvScope, WorkspaceEnvVar } from '@/lib/types'
12
+ import type { WorkspaceEnvVar } from '@/lib/types'
19
13
 
20
14
  import { IconPicker } from './IconPicker'
21
15
 
@@ -123,12 +117,6 @@ export function GeneralSettings() {
123
117
 
124
118
  // ── Environment ─────────────────────────────────────────────────────────────
125
119
 
126
- const SCOPE_LABEL: Record<EnvScope, string> = {
127
- widgets: 'Widgets',
128
- agent: 'Agent',
129
- both: 'Both'
130
- }
131
-
132
120
  // Source badge copy: where the key actually comes from.
133
121
  const SOURCE_LABEL: Record<WorkspaceEnvVar['source'], string> = {
134
122
  dotenv: '.env',
@@ -138,43 +126,12 @@ const SOURCE_LABEL: Record<WorkspaceEnvVar['source'], string> = {
138
126
 
139
127
  const ENV_KEY_RE = /^[A-Za-z_][A-Za-z0-9_]*$/
140
128
 
141
- type ScopeSelectProps = {
142
- value: EnvScope
143
- onChange: (scope: EnvScope) => void
144
- }
145
-
146
- function ScopeSelect({ value, onChange }: ScopeSelectProps) {
147
- return (
148
- <DropdownMenu>
149
- <DropdownMenuTrigger
150
- render={
151
- <Button variant="outline" size="sm" className="h-7 gap-1 px-2 font-normal">
152
- {SCOPE_LABEL[value]}
153
- <IconChevronDown stroke={1.75} className="text-muted-foreground" />
154
- </Button>
155
- }
156
- />
157
- <DropdownMenuContent align="end" className="w-32">
158
- <DropdownMenuRadioGroup value={value} onValueChange={v => onChange(v as EnvScope)}>
159
- {(Object.keys(SCOPE_LABEL) as EnvScope[]).map(scope => (
160
- <DropdownMenuRadioItem key={scope} value={scope}>
161
- {SCOPE_LABEL[scope]}
162
- </DropdownMenuRadioItem>
163
- ))}
164
- </DropdownMenuRadioGroup>
165
- </DropdownMenuContent>
166
- </DropdownMenu>
167
- )
168
- }
169
-
170
129
  export function EnvironmentSettings() {
171
130
  const { workspaceId } = useWorkspaceLayoutCtx()
172
- const env = useWorkspaceEnv(workspaceId)
173
- const update = useUpdateEnv(workspaceId)
131
+ const { env, update } = useEnvVars(workspaceId)
174
132
 
175
133
  const [draftKey, setDraftKey] = useState('')
176
134
  const [draftValue, setDraftValue] = useState('')
177
- const [draftScope, setDraftScope] = useState<EnvScope>('both')
178
135
 
179
136
  const keyValid = ENV_KEY_RE.test(draftKey)
180
137
  const canAdd = keyValid && draftValue.length > 0 && !update.isPending
@@ -182,12 +139,11 @@ export function EnvironmentSettings() {
182
139
  const addVar = () => {
183
140
  if (!canAdd) return
184
141
  update.mutate(
185
- { set: { [draftKey]: draftValue }, scopes: { [draftKey]: draftScope } },
142
+ { set: { [draftKey]: draftValue } },
186
143
  {
187
144
  onSuccess: () => {
188
145
  setDraftKey('')
189
146
  setDraftValue('')
190
- setDraftScope('both')
191
147
  }
192
148
  }
193
149
  )
@@ -230,15 +186,8 @@ export function EnvironmentSettings() {
230
186
  >
231
187
  {SOURCE_LABEL[v.source]}
232
188
  </span>
233
- {editable ? (
234
- <ScopeSelect
235
- value={v.scope ?? 'both'}
236
- onChange={scope => update.mutate({ scopes: { [v.key]: scope } })}
237
- />
238
- ) : (
239
- <span className="w-[60px] text-right text-[11px] text-muted-foreground/60">
240
- from file
241
- </span>
189
+ {!editable && (
190
+ <span className="text-right text-[11px] text-muted-foreground/60">from file</span>
242
191
  )}
243
192
  <button
244
193
  type="button"
@@ -273,7 +222,6 @@ export function EnvironmentSettings() {
273
222
  placeholder="value"
274
223
  className="h-7 flex-1 text-xs"
275
224
  />
276
- <ScopeSelect value={draftScope} onChange={setDraftScope} />
277
225
  <Button variant="outline" size="sm" className="h-7" onClick={addVar} disabled={!canAdd}>
278
226
  {update.isPending ? (
279
227
  <IconLoader2 className="animate-spin" />