moi-computer 0.0.2 → 0.1.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.
Files changed (246) hide show
  1. package/bin/moi.mjs +3 -1
  2. package/bunfig.toml +4 -0
  3. package/client/api/workspaces.ts +277 -6
  4. package/client/assets/mcp/agentation.png +0 -0
  5. package/client/assets/mcp/airtable.png +0 -0
  6. package/client/assets/mcp/apollo.png +0 -0
  7. package/client/assets/mcp/asana.png +0 -0
  8. package/client/assets/mcp/box.png +0 -0
  9. package/client/assets/mcp/chrome.svg +33 -0
  10. package/client/assets/mcp/clickup.png +0 -0
  11. package/client/assets/mcp/cloudflare.png +0 -0
  12. package/client/assets/mcp/context7.png +0 -0
  13. package/client/assets/mcp/datadog.png +0 -0
  14. package/client/assets/mcp/dropbox.png +0 -0
  15. package/client/assets/mcp/elevenlabs.png +0 -0
  16. package/client/assets/mcp/exa.png +0 -0
  17. package/client/assets/mcp/firecrawl.png +0 -0
  18. package/client/assets/mcp/gmail.png +0 -0
  19. package/client/assets/mcp/googlecalendar.png +0 -0
  20. package/client/assets/mcp/googledrive.png +0 -0
  21. package/client/assets/mcp/grafana.png +0 -0
  22. package/client/assets/mcp/huggingface.png +0 -0
  23. package/client/assets/mcp/intercom.png +0 -0
  24. package/client/assets/mcp/monday.png +0 -0
  25. package/client/assets/mcp/mongodb.png +0 -0
  26. package/client/assets/mcp/paper.png +0 -0
  27. package/client/assets/mcp/paypal.png +0 -0
  28. package/client/assets/mcp/perplexity.png +0 -0
  29. package/client/assets/mcp/postgresql.png +0 -0
  30. package/client/assets/mcp/posthog.png +0 -0
  31. package/client/assets/mcp/puppeteer.png +0 -0
  32. package/client/assets/mcp/railway.png +0 -0
  33. package/client/assets/mcp/redis.png +0 -0
  34. package/client/assets/mcp/salesforce.png +0 -0
  35. package/client/assets/mcp/shopify.png +0 -0
  36. package/client/assets/mcp/snowflake.png +0 -0
  37. package/client/assets/mcp/telegram.png +0 -0
  38. package/client/assets/mcp/twilio.png +0 -0
  39. package/client/assets/mcp/zapier.png +0 -0
  40. package/client/components/App.tsx +7 -1
  41. package/client/components/BottomPanel.tsx +2 -2
  42. package/client/components/ChatInput.tsx +195 -14
  43. package/client/components/ChatPanel.tsx +118 -49
  44. package/client/components/ChatPopup.tsx +2 -2
  45. package/client/components/CustomizePanel.tsx +8 -8
  46. package/client/components/HiddenPanel.tsx +1 -1
  47. package/client/components/HomePage.tsx +1 -1
  48. package/client/components/IconMcp.tsx +21 -0
  49. package/client/components/MarkdownContent.tsx +2 -2
  50. package/client/components/McpMenu.tsx +125 -0
  51. package/client/components/ModelPicker.tsx +147 -0
  52. package/client/components/Scratchpad.tsx +775 -0
  53. package/client/components/ShimmerText.tsx +20 -0
  54. package/client/components/ThreadSelector.tsx +2 -2
  55. package/client/components/TurnView.tsx +114 -534
  56. package/client/components/WidgetErrorBoundary.tsx +2 -2
  57. package/client/components/WidgetFrame.tsx +1 -1
  58. package/client/components/WidgetShell.tsx +2 -2
  59. package/client/components/Widgets.tsx +2 -2
  60. package/client/components/WorkspacePreview.tsx +3 -3
  61. package/client/components/WorkspacesPage.tsx +28 -20
  62. package/client/components/layout/SidebarLayout.tsx +24 -15
  63. package/client/components/playground/PixelPainter.tsx +2 -2
  64. package/client/components/playground/PlaygroundPage.tsx +1 -1
  65. package/client/components/playground/ToolCallsPage.tsx +81 -0
  66. package/client/components/routes/workspace/[id].tsx +534 -141
  67. package/client/components/routes/workspace/create.tsx +155 -0
  68. package/client/components/settings/IconPicker.tsx +590 -0
  69. package/client/components/settings/SettingsPages.tsx +307 -0
  70. package/client/components/settings/WorkspaceSettings.tsx +92 -0
  71. package/client/components/tool-group/CodeBlock.tsx +41 -0
  72. package/client/components/tool-group/Collapse.tsx +28 -0
  73. package/client/components/tool-group/ReadImagePreview.tsx +46 -0
  74. package/client/components/tool-group/SubagentCard.tsx +135 -0
  75. package/client/components/tool-group/TimelineRow.tsx +147 -0
  76. package/client/components/tool-group/ToolCallGroup.tsx +231 -0
  77. package/client/components/tool-group/ToolOutput.tsx +139 -0
  78. package/client/components/tool-group/detect.ts +93 -0
  79. package/client/components/tool-group/fixtures.ts +315 -0
  80. package/client/components/tool-group/format.ts +211 -0
  81. package/client/components/tool-group/index.ts +9 -0
  82. package/client/components/ui/button.tsx +4 -4
  83. package/client/components/ui/dialog.tsx +58 -0
  84. package/client/components/ui/dropdown-menu.tsx +9 -9
  85. package/client/components/ui/hover-card.tsx +44 -0
  86. package/client/components/ui/popover.tsx +1 -1
  87. package/client/components/ui/switch.tsx +23 -0
  88. package/client/components/ui/tooltip.tsx +54 -0
  89. package/client/externalize-react.ts +11 -5
  90. package/client/hooks/useApplet.ts +117 -0
  91. package/client/hooks/useChat.ts +162 -62
  92. package/client/hooks/useMeiEvents.ts +9 -2
  93. package/client/hooks/useStickToBottom.ts +86 -0
  94. package/client/hooks/useWorkspaceTheme.ts +8 -2
  95. package/client/index.css +49 -3
  96. package/client/index.html +22 -18
  97. package/client/index.tsx +29 -2
  98. package/client/lib/WorkspaceContext.tsx +5 -8
  99. package/client/lib/WorkspaceLayoutContext.tsx +30 -10
  100. package/client/lib/applet-cache.ts +54 -0
  101. package/client/lib/connection.ts +237 -0
  102. package/client/lib/flags.ts +10 -0
  103. package/client/lib/mcp-icons.ts +142 -10
  104. package/client/lib/preview-turn.ts +37 -0
  105. package/client/lib/render-icon.ts +173 -0
  106. package/client/lib/scratch-executor.ts +20 -0
  107. package/client/lib/uploads.ts +18 -0
  108. package/client/main.tsx +5 -0
  109. package/client/store/live.ts +282 -0
  110. package/client/structured-clone-shim.ts +14 -0
  111. package/client/theme.css +2 -2
  112. package/client/vendor/emojibase/en/data.json +1 -0
  113. package/client/vendor/emojibase/en/messages.json +1 -0
  114. package/client/vendor/react/development/_impl/react-dom-client.js +15871 -0
  115. package/client/vendor/react/development/_impl/react-dom.js +223 -0
  116. package/client/vendor/react/development/_impl/react-jsx-dev-runtime.js +256 -0
  117. package/client/vendor/react/development/_impl/react-jsx-runtime.js +260 -0
  118. package/client/vendor/react/development/_impl/react.js +863 -0
  119. package/client/vendor/react/development/react-dom-client.js +6 -0
  120. package/client/vendor/react/development/react-dom.js +17 -0
  121. package/client/vendor/react/development/react-jsx-dev-runtime.js +5 -0
  122. package/client/vendor/react/development/react-jsx-runtime.js +6 -0
  123. package/client/vendor/react/development/react.js +47 -0
  124. package/client/vendor/react/production/_impl/react-dom-client.js +10361 -0
  125. package/client/vendor/react/production/_impl/react-dom.js +184 -0
  126. package/client/vendor/react/production/_impl/react-jsx-dev-runtime.js +37 -0
  127. package/client/vendor/react/production/_impl/react-jsx-runtime.js +60 -0
  128. package/client/vendor/react/production/_impl/react.js +428 -0
  129. package/client/vendor/react/production/react-dom-client.js +6 -0
  130. package/client/vendor/react/production/react-dom.js +17 -0
  131. package/client/vendor/react/production/react-jsx-dev-runtime.js +5 -0
  132. package/client/vendor/react/production/react-jsx-runtime.js +6 -0
  133. package/client/vendor/react/production/react.js +47 -0
  134. package/dist/agentation-4s0nn45p.png +0 -0
  135. package/dist/airtable-hsa66v6q.png +0 -0
  136. package/dist/apollo-2ed5dy2d.png +0 -0
  137. package/dist/asana-tvgtwbx2.png +0 -0
  138. package/dist/atlassian-b3tq39hs.png +0 -0
  139. package/dist/box-w3n36t5g.png +0 -0
  140. package/dist/chrome-tspm94rw.svg +33 -0
  141. package/dist/chunk-kc6e9nd4.css +1 -0
  142. package/dist/chunk-yf3p7qg7.js +673 -0
  143. package/dist/claude-z2xnay0d.svg +3 -0
  144. package/dist/clickup-qqrqqvqb.png +0 -0
  145. package/dist/cloudflare-j9p931v6.png +0 -0
  146. package/dist/context7-8ht0xjfy.png +0 -0
  147. package/dist/datadog-70ne3yzt.png +0 -0
  148. package/dist/discord-vejhptwx.png +0 -0
  149. package/dist/dropbox-4m5s9mgr.png +0 -0
  150. package/dist/elevenlabs-2nzzej1t.png +0 -0
  151. package/dist/exa-qqj3vr0x.png +0 -0
  152. package/dist/favicon-nbmyjfjh.png +0 -0
  153. package/dist/figma-y9898rry.png +0 -0
  154. package/dist/firecrawl-w2t8nehx.png +0 -0
  155. package/dist/github-av5r5y3e.png +0 -0
  156. package/dist/gitlab-4pv5vm6b.png +0 -0
  157. package/dist/gmail-sg6a5v3x.png +0 -0
  158. package/dist/googlecalendar-nqhh2v7r.png +0 -0
  159. package/dist/googledrive-6s8xyab7.png +0 -0
  160. package/dist/grafana-1yfsnwr6.png +0 -0
  161. package/dist/granola-7j3b0nk2.png +0 -0
  162. package/dist/hermes-nous-pznd63x9.png +0 -0
  163. package/dist/hubspot-hghcmc2t.png +0 -0
  164. package/dist/huggingface-kxzaxj83.png +0 -0
  165. package/dist/index.html +47 -0
  166. package/dist/intercom-td763jt8.png +0 -0
  167. package/dist/linear-v9s3az2d.png +0 -0
  168. package/dist/monday-jdbs4wwf.png +0 -0
  169. package/dist/mongodb-emzxvhd5.png +0 -0
  170. package/dist/neon-zj4qar44.png +0 -0
  171. package/dist/notion-w034c0b8.png +0 -0
  172. package/dist/openclaw-a2pyfhpn.svg +22 -0
  173. package/dist/paper-zbt34fab.png +0 -0
  174. package/dist/paypal-kwwf7xsc.png +0 -0
  175. package/dist/perplexity-4y63rxvj.png +0 -0
  176. package/dist/postgresql-qsspbsr1.png +0 -0
  177. package/dist/posthog-sphppc4w.png +0 -0
  178. package/dist/puppeteer-2xjt1a9b.png +0 -0
  179. package/dist/railway-834de8d2.png +0 -0
  180. package/dist/redis-spm6w2m1.png +0 -0
  181. package/dist/salesforce-1mk56v4z.png +0 -0
  182. package/dist/sentry-hdwpmvek.png +0 -0
  183. package/dist/shopify-90dadaav.png +0 -0
  184. package/dist/slack-1c1wj62s.png +0 -0
  185. package/dist/snowflake-hfpykpgy.png +0 -0
  186. package/dist/stripe-zrfaqbj3.png +0 -0
  187. package/dist/supabase-4vg33zf2.png +0 -0
  188. package/dist/telegram-v6tjyrs7.png +0 -0
  189. package/dist/twilio-dpyjdwpj.png +0 -0
  190. package/dist/vercel-7d0ssw28.png +0 -0
  191. package/dist/zapier-p67qfch0.png +0 -0
  192. package/lib/attachment-note.ts +48 -0
  193. package/lib/claude-adapter.ts +219 -29
  194. package/lib/format.ts +32 -0
  195. package/lib/types.ts +314 -6
  196. package/package.json +21 -31
  197. package/server/CLAUDE.md +3 -0
  198. package/server/agent.ts +25 -118
  199. package/server/api.ts +593 -0
  200. package/server/applets.ts +396 -0
  201. package/server/build-applet.ts +580 -0
  202. package/server/cc-session.ts +611 -0
  203. package/server/cli-colors.ts +17 -0
  204. package/server/cli-ui.ts +52 -0
  205. package/server/cli.ts +970 -72
  206. package/server/control.ts +166 -19
  207. package/server/debug.ts +10 -0
  208. package/server/events.ts +24 -0
  209. package/server/functions-worker.ts +18 -2
  210. package/server/functions.ts +59 -12
  211. package/server/icon.ts +13 -0
  212. package/server/layout.ts +25 -3
  213. package/server/mcp.ts +125 -0
  214. package/server/moi-scaffold.ts +85 -0
  215. package/server/openclaw-session.ts +41 -10
  216. package/server/openclaw.ts +22 -0
  217. package/server/preview.ts +70 -0
  218. package/server/registry.ts +32 -2
  219. package/server/scratchpad-executor.ts +380 -0
  220. package/server/scratchpad-relay.ts +45 -0
  221. package/server/scratchpad.ts +171 -0
  222. package/server/skill-version.ts +146 -0
  223. package/server/skills-template.ts +6 -1
  224. package/server/state.ts +22 -36
  225. package/server/static.ts +23 -0
  226. package/server/status.ts +94 -0
  227. package/server/thread-config.ts +125 -0
  228. package/server/uploads.ts +0 -0
  229. package/server/vendor.ts +63 -0
  230. package/server/views.ts +0 -0
  231. package/server/web.ts +113 -223
  232. package/server/widgets.ts +52 -142
  233. package/server/workspace-config.ts +37 -0
  234. package/server/workspace-env.ts +407 -0
  235. package/server/workspace-init.ts +66 -0
  236. package/workspace/.claude/skills/moi-workspace/DESIGN.md +109 -0
  237. package/workspace/.claude/skills/moi-workspace/SCRATCHPAD.md +57 -0
  238. package/workspace/.claude/skills/moi-workspace/SKILL.md +252 -0
  239. package/workspace/.claude/skills/moi-workspace/VIEW-DESIGN.md +22 -0
  240. package/client/hooks/useWidget.ts +0 -72
  241. package/client/hooks/useWidgetList.ts +0 -30
  242. package/client/lib/ws.ts +0 -129
  243. package/client/store/chat.tsx +0 -162
  244. package/server/build-widget.ts +0 -343
  245. package/workspace/.claude/skills/widgets/DESIGN.md +0 -630
  246. package/workspace/.claude/skills/widgets/SKILL.md +0 -176
@@ -0,0 +1,18 @@
1
+ import type { UploadInfo } from '@/lib/types'
2
+
3
+ // POST one or more files to a workspace's upload endpoint and return the server's
4
+ // `UploadInfo` for each (in request order). The chat composer calls this as soon
5
+ // as files are added (drop/paste/pick) so the upload ids are ready by send time.
6
+ export async function uploadFiles(workspaceId: string, files: File[]): Promise<UploadInfo[]> {
7
+ const form = new FormData()
8
+ for (const f of files) form.append('files', f)
9
+ const res = await fetch(`/api/workspaces/${workspaceId}/uploads`, {
10
+ method: 'POST',
11
+ body: form
12
+ })
13
+ if (!res.ok) {
14
+ const detail = await res.text().catch(() => '')
15
+ throw new Error(detail || `Upload failed (${res.status})`)
16
+ }
17
+ return res.json()
18
+ }
package/client/main.tsx CHANGED
@@ -3,10 +3,15 @@ import { Agentation } from 'agentation'
3
3
  import { createRoot } from 'react-dom/client'
4
4
  import { Router } from 'wouter'
5
5
 
6
+ import { initConnection } from './lib/connection'
6
7
  import { AppRouter } from './components/App'
7
8
 
8
9
  const queryClient = new QueryClient()
9
10
 
11
+ // Open the single app-wide chat WebSocket once and hand it the query client so
12
+ // live frames fold into the RQ transcript cache. Lives for the page's lifetime.
13
+ initConnection(queryClient)
14
+
10
15
  export function mount(el: HTMLElement) {
11
16
  function Root() {
12
17
  return (
@@ -0,0 +1,282 @@
1
+ import { useStore } from 'zustand'
2
+ import { createStore } from 'zustand/vanilla'
3
+
4
+ import type { PreviewBlock, PreviewFrame, UploadInfo } from '@/lib/types'
5
+
6
+ // One composer attachment, tracked per thread until the message is sent. It is
7
+ // uploaded as soon as it's added (drop/paste/pick); `status` reflects that
8
+ // in-flight upload, and `upload` holds the server handle once ready. `previewUrl`
9
+ // is a local object URL for image thumbnails (revoked on remove/clear).
10
+ export type ChatAttachment = {
11
+ localId: string
12
+ name: string
13
+ mediaType: string
14
+ previewUrl?: string
15
+ status: 'uploading' | 'ready' | 'error'
16
+ upload?: UploadInfo
17
+ error?: string
18
+ }
19
+
20
+ // App-level ephemeral chat state — the bits that are *pushed* from the server
21
+ // over the WebSocket and can't be re-fetched as request/response data:
22
+ // - which thread is active per workspace (a UI selection),
23
+ // - per-session `processing` (spinner) flags,
24
+ // - per-session error banners.
25
+ //
26
+ // The durable message transcripts live in the React Query cache (see
27
+ // useSessionView), which is also app-level — so nothing here needs to mirror
28
+ // them. This is a single module-singleton store (not React-context scoped), so
29
+ // it survives route navigation: leaving and re-entering a workspace keeps the
30
+ // active thread and any in-flight spinner intact.
31
+ //
32
+ // Per-session entries are keyed `${workspaceId}:${sessionId}`.
33
+
34
+ function key(workspaceId: string, sessionId: string): string {
35
+ return `${workspaceId}:${sessionId}`
36
+ }
37
+
38
+ // Draft text is keyed per thread. A brand-new chat has no session id yet, so it
39
+ // gets a stable `'new'` sentinel; once `send` mints the real id the input reads
40
+ // the (empty) draft under the new key. Exported so `ChatInput` builds the same
41
+ // key it reads as the store writes.
42
+ export function draftKey(workspaceId: string, sessionId: string | null): string {
43
+ return key(workspaceId, sessionId ?? 'new')
44
+ }
45
+
46
+ // A live streaming preview held in the store, keyed by API message id. Ephemeral
47
+ // and disposable — see PreviewFrame. `updatedAt` drives the TTL sweep that reaps
48
+ // any preview a clear signal somehow never reached.
49
+ export type LivePreview = {
50
+ workspaceId: string
51
+ sessionId: string
52
+ parentToolUseId: string | null
53
+ blocks: PreviewBlock[]
54
+ updatedAt: number
55
+ }
56
+
57
+ export type LiveStore = {
58
+ activeByWorkspace: Record<string, string | null>
59
+ processing: Record<string, boolean>
60
+ errors: Record<string, string | null>
61
+ // Live token-streaming previews, keyed by `messageId` (the API `msg_...` id)
62
+ // so concurrent streams never collide. Reconciled against the durable
63
+ // transcript: a preview is dropped the instant its finalized turn arrives.
64
+ previews: Record<string, LivePreview>
65
+ // Unsent composer text, keyed per thread (`${workspaceId}:${sessionId}`, with
66
+ // a `'new'` sentinel for a not-yet-created thread). Lives here — not in the
67
+ // chat component — so a keystroke re-renders only the composer (which alone
68
+ // subscribes), not the whole workspace, and the draft survives the chat
69
+ // panel's remounts on mode switch.
70
+ drafts: Record<string, string>
71
+ // Composer attachments, keyed per thread exactly like `drafts` (so they follow
72
+ // the active thread and survive composer remounts). Cleared on send.
73
+ attachments: Record<string, ChatAttachment[]>
74
+
75
+ setActive: (workspaceId: string, sessionId: string | null) => void
76
+ setProcessing: (workspaceId: string, sessionId: string, value: boolean) => void
77
+ // Authoritative reconcile from a server `status_snapshot`: exactly the listed
78
+ // sessions are processing; everything else is cleared (fixes a spinner whose
79
+ // terminal status was emitted while we were disconnected).
80
+ reconcileProcessing: (running: { workspaceId: string; sessionId: string }[]) => void
81
+ setError: (workspaceId: string, sessionId: string, message: string | null) => void
82
+ setDraft: (workspaceId: string, sessionId: string | null, value: string) => void
83
+ addAttachments: (workspaceId: string, sessionId: string | null, items: ChatAttachment[]) => void
84
+ updateAttachment: (
85
+ workspaceId: string,
86
+ sessionId: string | null,
87
+ localId: string,
88
+ patch: Partial<ChatAttachment>
89
+ ) => void
90
+ removeAttachment: (workspaceId: string, sessionId: string | null, localId: string) => void
91
+ clearAttachments: (workspaceId: string, sessionId: string | null) => void
92
+ renameSession: (workspaceId: string, from: string, to: string) => void
93
+
94
+ // Upsert a preview snapshot (last write wins — blocks are cumulative).
95
+ setPreview: (frame: Omit<PreviewFrame, 'type'>) => void
96
+ // Drop one preview by message id — used the instant its real turn lands.
97
+ clearPreview: (messageId: string) => void
98
+ // Drop every preview for a session — belt for run end / stop / error, where a
99
+ // per-message clear might be missed (e.g. a turn without an apiMessageId).
100
+ clearPreviewsForSession: (workspaceId: string, sessionId: string) => void
101
+ // Drop everything — used on socket reconnect, where any in-flight preview is
102
+ // definitionally superseded by the /events refetch.
103
+ clearAllPreviews: () => void
104
+ // Reap previews older than `maxAgeMs` (TTL backstop against a missed clear).
105
+ sweepPreviews: (maxAgeMs: number, now: number) => void
106
+ }
107
+
108
+ export const liveStore = createStore<LiveStore>()(set => ({
109
+ activeByWorkspace: {},
110
+ processing: {},
111
+ errors: {},
112
+ drafts: {},
113
+ previews: {},
114
+ attachments: {},
115
+
116
+ setActive: (workspaceId, sessionId) =>
117
+ set(s => ({ activeByWorkspace: { ...s.activeByWorkspace, [workspaceId]: sessionId } })),
118
+
119
+ setProcessing: (workspaceId, sessionId, value) =>
120
+ set(s => ({ processing: { ...s.processing, [key(workspaceId, sessionId)]: value } })),
121
+
122
+ reconcileProcessing: running =>
123
+ set(() => ({
124
+ processing: Object.fromEntries(running.map(r => [key(r.workspaceId, r.sessionId), true]))
125
+ })),
126
+
127
+ setError: (workspaceId, sessionId, message) =>
128
+ set(s => ({ errors: { ...s.errors, [key(workspaceId, sessionId)]: message } })),
129
+
130
+ setDraft: (workspaceId, sessionId, value) =>
131
+ set(s => ({ drafts: { ...s.drafts, [draftKey(workspaceId, sessionId)]: value } })),
132
+
133
+ setPreview: frame =>
134
+ set(s => ({
135
+ previews: {
136
+ ...s.previews,
137
+ [frame.messageId]: {
138
+ workspaceId: frame.workspaceId,
139
+ sessionId: frame.sessionId,
140
+ parentToolUseId: frame.parentToolUseId,
141
+ blocks: frame.blocks,
142
+ updatedAt: Date.now()
143
+ }
144
+ }
145
+ })),
146
+
147
+ clearPreview: messageId =>
148
+ set(s => {
149
+ if (!(messageId in s.previews)) return s
150
+ const { [messageId]: _drop, ...rest } = s.previews
151
+ return { previews: rest }
152
+ }),
153
+
154
+ clearPreviewsForSession: (workspaceId, sessionId) =>
155
+ set(s => {
156
+ const rest: Record<string, LivePreview> = {}
157
+ let changed = false
158
+ for (const [id, p] of Object.entries(s.previews)) {
159
+ if (p.workspaceId === workspaceId && p.sessionId === sessionId) changed = true
160
+ else rest[id] = p
161
+ }
162
+ return changed ? { previews: rest } : s
163
+ }),
164
+
165
+ clearAllPreviews: () => set(s => (Object.keys(s.previews).length ? { previews: {} } : s)),
166
+
167
+ sweepPreviews: (maxAgeMs, now) =>
168
+ set(s => {
169
+ const rest: Record<string, LivePreview> = {}
170
+ let changed = false
171
+ for (const [id, p] of Object.entries(s.previews)) {
172
+ if (now - p.updatedAt > maxAgeMs) changed = true
173
+ else rest[id] = p
174
+ }
175
+ return changed ? { previews: rest } : s
176
+ }),
177
+
178
+ addAttachments: (workspaceId, sessionId, items) =>
179
+ set(s => {
180
+ const k = draftKey(workspaceId, sessionId)
181
+ return { attachments: { ...s.attachments, [k]: [...(s.attachments[k] ?? []), ...items] } }
182
+ }),
183
+
184
+ updateAttachment: (workspaceId, sessionId, localId, patch) =>
185
+ set(s => {
186
+ const k = draftKey(workspaceId, sessionId)
187
+ const list = s.attachments[k]
188
+ if (!list) return {}
189
+ return {
190
+ attachments: {
191
+ ...s.attachments,
192
+ [k]: list.map(a => (a.localId === localId ? { ...a, ...patch } : a))
193
+ }
194
+ }
195
+ }),
196
+
197
+ removeAttachment: (workspaceId, sessionId, localId) =>
198
+ set(s => {
199
+ const k = draftKey(workspaceId, sessionId)
200
+ const list = s.attachments[k]
201
+ if (!list) return {}
202
+ const target = list.find(a => a.localId === localId)
203
+ if (target?.previewUrl) URL.revokeObjectURL(target.previewUrl)
204
+ return { attachments: { ...s.attachments, [k]: list.filter(a => a.localId !== localId) } }
205
+ }),
206
+
207
+ clearAttachments: (workspaceId, sessionId) =>
208
+ set(s => {
209
+ const k = draftKey(workspaceId, sessionId)
210
+ for (const a of s.attachments[k] ?? []) {
211
+ if (a.previewUrl) URL.revokeObjectURL(a.previewUrl)
212
+ }
213
+ const next = { ...s.attachments }
214
+ delete next[k]
215
+ return { attachments: next }
216
+ }),
217
+
218
+ renameSession: (workspaceId, from, to) =>
219
+ set(s => {
220
+ const fromKey = key(workspaceId, from)
221
+ const toKey = key(workspaceId, to)
222
+ const processing = { ...s.processing }
223
+ const errors = { ...s.errors }
224
+ if (fromKey in processing) {
225
+ processing[toKey] = processing[fromKey]
226
+ delete processing[fromKey]
227
+ }
228
+ if (fromKey in errors) {
229
+ errors[toKey] = errors[fromKey]
230
+ delete errors[fromKey]
231
+ }
232
+ const activeByWorkspace =
233
+ s.activeByWorkspace[workspaceId] === from
234
+ ? { ...s.activeByWorkspace, [workspaceId]: to }
235
+ : s.activeByWorkspace
236
+ // Retarget any in-flight previews from the temp id to the real one so a
237
+ // preview that arrived before the rename keeps routing to the thread.
238
+ let previews = s.previews
239
+ let previewsChanged = false
240
+ for (const [id, p] of Object.entries(s.previews)) {
241
+ if (p.workspaceId === workspaceId && p.sessionId === from) {
242
+ if (!previewsChanged) {
243
+ previews = { ...s.previews }
244
+ previewsChanged = true
245
+ }
246
+ previews[id] = { ...p, sessionId: to }
247
+ }
248
+ }
249
+ return { processing, errors, activeByWorkspace, previews }
250
+ })
251
+ }))
252
+
253
+ // Select the live previews for a thread, split into the root (top-level
254
+ // assistant) stream and per-subagent streams. Takes the raw `previews` record
255
+ // (a stable store slice) so callers select that slice and run this inside a
256
+ // `useMemo` — never return a fresh object straight from a zustand selector.
257
+ // `null` sessionId yields empties.
258
+ export function selectPreviews(
259
+ previews: Record<string, LivePreview>,
260
+ workspaceId: string,
261
+ sessionId: string | null
262
+ ): { root: LivePreview | null; byParent: Record<string, LivePreview> } {
263
+ if (!sessionId) return { root: null, byParent: {} }
264
+ let root: LivePreview | null = null
265
+ const byParent: Record<string, LivePreview> = {}
266
+ for (const p of Object.values(previews)) {
267
+ if (p.workspaceId !== workspaceId || p.sessionId !== sessionId) continue
268
+ if (p.parentToolUseId === null) {
269
+ // At most one root stream is active at a time; keep the freshest.
270
+ if (!root || p.updatedAt > root.updatedAt) root = p
271
+ } else {
272
+ const prev = byParent[p.parentToolUseId]
273
+ if (!prev || p.updatedAt > prev.updatedAt) byParent[p.parentToolUseId] = p
274
+ }
275
+ }
276
+ return { root, byParent }
277
+ }
278
+
279
+ // Reactive selector hook bound to the singleton store.
280
+ export function useLive<T>(selector: (state: LiveStore) => T): T {
281
+ return useStore(liveStore, selector)
282
+ }
@@ -0,0 +1,14 @@
1
+ // Re-bind `globalThis.structuredClone` to the global object up front.
2
+ //
3
+ // Bun compiles `import { structuredClone } from '@tldraw/utils'; structuredClone(x)`
4
+ // into a namespace-member call — `utils.structuredClone(x)` — which invokes the
5
+ // native function with `this` set to the module namespace object. V8 brand-checks
6
+ // `structuredClone` against the global and throws "Illegal invocation", crashing
7
+ // tldraw on mount during its user-preferences init. tldraw captures
8
+ // `globalThis.structuredClone` once at module-eval time, so binding it here —
9
+ // imported before any tldraw module loads — makes the receiver irrelevant. Native
10
+ // structuredClone ignores `this` when called correctly, so this is transparent to
11
+ // every other caller.
12
+ if (typeof globalThis.structuredClone === 'function') {
13
+ globalThis.structuredClone = globalThis.structuredClone.bind(globalThis)
14
+ }
package/client/theme.css CHANGED
@@ -1,7 +1,7 @@
1
1
  /*
2
2
  * Shared design-token map for both the host app (client/index.css) and
3
- * widget bundles (server/build-widget.ts emits an `@import` to this file
4
- * into the widget's synthetic CSS).
3
+ * applet bundles (server/build-applet.ts emits an `@import` to this file
4
+ * into the applet's synthetic CSS).
5
5
  *
6
6
  * The actual CSS variables (`--background`, `--foreground`, `--card`, etc.)
7
7
  * are owned by the host and declared on `:root` / `.dark` in