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
package/server/web.ts CHANGED
@@ -1,61 +1,69 @@
1
1
  import type { ClientMessage } from '@/lib/types'
2
2
 
3
3
  import index from '../client/index.html'
4
- import { getMcpStatus, handleChat, stopChat } from './agent'
4
+ import { api } from './api'
5
+ import {
6
+ getCCRunningSessions,
7
+ interruptCCSession,
8
+ killAllCCSessions,
9
+ sendCCMessage
10
+ } from './cc-session'
5
11
  import { PORT } from './constants'
6
12
  import { control } from './control'
7
- import { callFunction, killAllWorkers } from './functions'
8
- import { getWorkspacePreview, loadLayout, saveLayout } from './layout'
9
- import { getOpenClawSessionMessages, getOpenClawSessions } from './openclaw'
10
- import { toSessionInfo, toStreamEvents } from './openclaw-adapter'
13
+ import { EVENTS_TOPIC, setEventServer } from './events'
14
+ import { killAllWorkers } from './functions'
15
+ import { resolveScratchOp } from './scratchpad-relay'
11
16
  import {
12
17
  abortOpenClawRun,
13
- ensureOpenClawSessionLive,
14
- getLiveOpenClawEvents,
15
- getOpenClawProcessingSessions,
18
+ getOpenClawRunningSessions,
16
19
  sendOpenClawMessage
17
20
  } from './openclaw-session'
18
- import {
19
- discoverWorkspaces,
20
- getWorkspace,
21
- listWorkspaces,
22
- registerWorkspace,
23
- removeWorkspace
24
- } from './registry'
25
- import {
26
- addClient,
27
- getProcessingSessions,
28
- getSessionEvents,
29
- getSessions,
30
- removeClient,
31
- sendToClient
32
- } from './state'
33
- import { listWidgets, serveWidget } from './widgets'
21
+ import { getWorkspace } from './registry'
22
+ import { addClient, removeClient, sendToClient } from './state'
23
+ import { distShell, prebuilt } from './static'
24
+ import { renderStatus } from './status'
25
+ import { serveVendorEmojibase, serveVendorReact } from './vendor'
34
26
 
35
- const MEI_TOPIC = 'mei'
36
-
37
- type WsData = { channel: 'chat' | 'mei'; workspaceId: string }
27
+ type WsData = { channel: 'chat' | 'events'; workspaceId: string }
38
28
 
39
29
  function isClientMessage(value: unknown): value is ClientMessage {
40
30
  if (typeof value !== 'object' || value === null || !('type' in value)) return false
41
31
  const v = value as {
42
32
  type: string
33
+ workspaceId?: unknown
43
34
  sessionId?: unknown
44
35
  content?: unknown
45
36
  isNew?: unknown
46
37
  optimisticId?: unknown
38
+ model?: unknown
39
+ effort?: unknown
40
+ stream?: unknown
41
+ attachments?: unknown
42
+ opId?: unknown
47
43
  }
48
44
  if (v.type === 'chat')
49
45
  return (
46
+ typeof v.workspaceId === 'string' &&
50
47
  typeof v.content === 'string' &&
51
48
  typeof v.sessionId === 'string' &&
52
49
  typeof v.isNew === 'boolean' &&
53
- (v.optimisticId === undefined || typeof v.optimisticId === 'string')
50
+ (v.optimisticId === undefined || typeof v.optimisticId === 'string') &&
51
+ (v.model === undefined || typeof v.model === 'string') &&
52
+ (v.effort === undefined || typeof v.effort === 'string') &&
53
+ (v.stream === undefined || typeof v.stream === 'boolean') &&
54
+ (v.attachments === undefined ||
55
+ (Array.isArray(v.attachments) && v.attachments.every(a => typeof a === 'string')))
54
56
  )
55
- if (v.type === 'stop') return typeof v.sessionId === 'string'
57
+ if (v.type === 'stop') return typeof v.workspaceId === 'string' && typeof v.sessionId === 'string'
58
+ if (v.type === 'scratchpad:op-result') return typeof v.opId === 'string'
56
59
  return false
57
60
  }
58
61
 
62
+ // The SPA shell: prebuilt index.html in prod (served statically), the
63
+ // live-bundled HTML import in dev (Bun.serve's bundler + HMR). The HTML import
64
+ // stays here, in the routes table, so Bun's dev bundler keys off it.
65
+ const shell = prebuilt ? distShell : index
66
+
59
67
  type Upgradable = { upgrade(req: Request, opts: { data: WsData }): boolean }
60
68
 
61
69
  function upgrade(server: Upgradable, req: Request, data: WsData) {
@@ -64,166 +72,60 @@ function upgrade(server: Upgradable, req: Request, data: WsData) {
64
72
  : new Response('Upgrade failed', { status: 500 })
65
73
  }
66
74
 
75
+ // Bun owns the fullstack surface: the HTML shell + dev bundler/HMR, and the two
76
+ // WebSocket channels (which need Bun's native `server.upgrade` + pub/sub). Every
77
+ // HTTP API route is delegated to the Hono app (`./api`) via `fetch`.
67
78
  export const app = Bun.serve<WsData>({
68
79
  port: PORT,
69
80
  hostname: process.env.HOST ?? '127.0.0.1',
70
- development: { hmr: true },
81
+ // HMR only in dev; prod serves prebuilt static assets (no bundler).
82
+ development: prebuilt ? false : { hmr: true },
71
83
  routes: {
72
- // Client-side routes — serve the SPA shell
73
- '/': index,
74
- '/playground': index,
75
- '/workspace/*': index,
84
+ // Plain-text server introspection — live sessions held in cache, connected
85
+ // tabs, last message per thread. A quick peek into the process when a chat
86
+ // seems stuck. Served as text/plain so curl/browser both show it raw.
87
+ '/status': () =>
88
+ new Response(renderStatus(), {
89
+ headers: { 'Content-Type': 'text/plain; charset=utf-8', 'Cache-Control': 'no-store' }
90
+ }),
91
+
92
+ // Locally-vendored React ESM (offline; no CDN). The importmap in
93
+ // client/index.html points here; the handler picks dev/prod per env.
94
+ '/vendor/react/*': req => serveVendorReact(req),
95
+
96
+ // Vendored emojibase-data for the settings emoji picker (offline; no CDN).
97
+ '/vendor/emojibase/*': req => serveVendorEmojibase(req),
98
+
99
+ // Client-side routes — serve the SPA shell.
100
+ '/': shell,
101
+ '/playground': shell,
102
+ '/playground/*': shell,
103
+ '/workspace/*': shell,
104
+
105
+ // Chat websocket — app-wide (one per client, not per workspace). Each chat
106
+ // frame carries its own workspaceId.
107
+ '/ws': (req, server) => upgrade(server, req, { channel: 'chat', workspaceId: '' }),
76
108
 
77
- // Workspace registry API
78
- '/api/workspaces': async req => {
79
- if (req.method === 'GET') return Response.json(await listWorkspaces())
80
- if (req.method === 'POST') {
81
- const body = await req.json()
82
- if (!body?.path) return new Response('Missing path', { status: 400 })
83
- const rawType = body?.type
84
- const type = rawType === 'claude-code' || rawType === 'openclaw' ? rawType : undefined
85
- const entry = await registerWorkspace(String(body.path), {
86
- type,
87
- name: typeof body?.name === 'string' ? body.name : undefined,
88
- agentId: typeof body?.agentId === 'string' ? body.agentId : undefined,
89
- isDefault: typeof body?.isDefault === 'boolean' ? body.isDefault : undefined,
90
- lastRunAt: typeof body?.lastRunAt === 'string' ? body.lastRunAt : undefined
91
- })
92
- return Response.json(entry, { status: 201 })
93
- }
94
- return new Response('Method not allowed', { status: 405 })
95
- },
96
- '/api/workspaces/discover': async () => Response.json(await discoverWorkspaces()),
97
- '/api/workspaces/:id/preview': async req => {
98
- const ws = await getWorkspace(req.params.id)
99
- if (!ws) return Response.json({ cols: 4, items: [] })
100
- return Response.json(await getWorkspacePreview(ws.path))
101
- },
102
- '/api/workspaces/:id/layout': async req => {
103
- const ws = await getWorkspace(req.params.id)
104
- if (!ws) return new Response('Workspace not found', { status: 404 })
105
- if (req.method === 'GET') {
106
- return Response.json({
107
- ...(await loadLayout(ws.path)),
108
- cwd: ws.path,
109
- name: ws.name,
110
- agentId: ws.agentId
111
- })
112
- }
113
- if (req.method === 'PUT') {
114
- const body = await req.json()
115
- if (!body || body.version !== 1) return new Response('Bad request', { status: 400 })
116
- await saveLayout(body, ws.path)
117
- return new Response(null, { status: 204 })
118
- }
119
- return new Response('Method not allowed', { status: 405 })
120
- },
121
- '/api/workspaces/:id/widgets': async req => {
122
- const ws = await getWorkspace(req.params.id)
123
- if (!ws) return new Response('Workspace not found', { status: 404 })
124
- return listWidgets(ws.path)
125
- },
126
- '/api/workspaces/:id/sessions': async req => {
127
- const ws = await getWorkspace(req.params.id)
128
- if (!ws) return new Response('Workspace not found', { status: 404 })
129
- if (ws.type === 'openclaw') {
130
- const rows = await getOpenClawSessions(ws.path, ws.agentId)
131
- return Response.json(rows.map(r => toSessionInfo(r, ws.path)))
132
- }
133
- return Response.json(await getSessions(ws.path))
134
- },
135
- // Widget bundle: the compiled ESM module for one widget, dynamically
136
- // imported by the client (useWidget). Sits beside the GET .../widgets list
137
- // above — the exact path lists, `/*` serves a bundle.
138
- '/api/workspaces/:id/widgets/*': async req => {
139
- const ws = await getWorkspace(req.params.id)
140
- if (!ws) return new Response('Workspace not found', { status: 404 })
141
- const name = new URL(req.url).pathname.split('/').pop()?.replace(/\.js$/, '')
142
- return name ? serveWidget(name, ws.path) : new Response('Not found', { status: 404 })
143
- },
144
- '/api/workspaces/:id/sessions/:sessionId/events': async req => {
145
- const ws = await getWorkspace(req.params.id)
146
- if (!ws) return new Response('Workspace not found', { status: 404 })
147
- if (ws.type === 'openclaw') {
148
- // Prefer the live view if we already hold one — keeps REST + WS in
149
- // agreement for any reload that lands while a run is active. The
150
- // first cold call also primes the live subscription so subsequent
151
- // WS frames upsert into the same view.
152
- const live = getLiveOpenClawEvents(req.params.id, req.params.sessionId)
153
- if (live) return Response.json(live)
154
- if (ws.agentId) {
155
- try {
156
- const evs = await ensureOpenClawSessionLive({
157
- workspaceId: req.params.id,
158
- workspacePath: ws.path,
159
- agentId: ws.agentId,
160
- sessionId: req.params.sessionId
161
- })
162
- return Response.json(evs)
163
- } catch {
164
- // fall through to static path
165
- }
166
- }
167
- const preview = await getOpenClawSessionMessages(req.params.sessionId, ws.path, ws.agentId)
168
- return Response.json(toStreamEvents(preview))
169
- }
170
- return Response.json(await getSessionEvents(req.params.sessionId, ws.path))
171
- },
172
- '/api/workspaces/:id/mcp': async req => {
173
- const ws = await getWorkspace(req.params.id)
174
- if (!ws) return new Response('Workspace not found', { status: 404 })
175
- return Response.json(await getMcpStatus(ws.path))
176
- },
177
109
  // Live widget-event stream (build/refresh pushes). A static path, so Bun
178
- // routes it ahead of the `/api/workspaces/:id` param route below; the
179
- // upgrade happens in-handler via the route's `server` argument.
110
+ // routes it ahead of the Hono-served `/api/workspaces/:id`; the upgrade
111
+ // happens in-handler via the route's `server` argument.
180
112
  '/api/workspaces/ws': (req, server) =>
181
- upgrade(server, req, { channel: 'mei', workspaceId: '' }),
182
- '/api/workspaces/:id': async req => {
183
- if (req.method === 'DELETE') {
184
- const ok = await removeWorkspace(req.params.id)
185
- if (!ok) return new Response('Workspace not found', { status: 404 })
186
- return new Response(null, { status: 204 })
187
- }
188
- return new Response('Method not allowed', { status: 405 })
189
- },
190
-
191
- // Widget RPC: a widget bundle POSTs here to call one of its `.server.ts`
192
- // functions. Kept on its own internal `/_rpc/` prefix, off the public API.
193
- '/_rpc/:wid/fn/*': async req => {
194
- if (req.method !== 'POST') return new Response('Method not allowed', { status: 405 })
195
- const ws = await getWorkspace(req.params.wid)
196
- if (!ws) return new Response('Workspace not found', { status: 404 })
197
- const prefix = `/_rpc/${req.params.wid}/fn/`
198
- const tail = new URL(req.url).pathname.slice(prefix.length)
199
- return handleFunctionCall(req, tail, ws.path)
200
- }
201
- },
202
- fetch(req, server) {
203
- const url = new URL(req.url)
204
- // Chat websocket (per-workspace). The widget-event websocket lives in the
205
- // routes table at /api/workspaces/ws.
206
- if (url.pathname === '/ws') {
207
- const workspaceId = url.searchParams.get('workspace') ?? ''
208
- return upgrade(server, req, { channel: 'chat', workspaceId })
209
- }
210
- return new Response('Not found', { status: 404 })
113
+ upgrade(server, req, { channel: 'events', workspaceId: '' })
211
114
  },
115
+ // Anything not matched above (the whole HTTP API + prod static assets + 404)
116
+ // is handled by Hono.
117
+ fetch: req => api.fetch(req),
212
118
  websocket: {
213
119
  open(ws) {
214
120
  if (ws.data.channel === 'chat') {
215
- addClient(ws.data.workspaceId, ws)
216
- const seen = new Set<string>()
217
- for (const sid of getProcessingSessions(ws.data.workspaceId)) {
218
- seen.add(sid)
219
- sendToClient(ws, { type: 'status', sessionId: sid, processing: true })
220
- }
221
- for (const sid of getOpenClawProcessingSessions(ws.data.workspaceId)) {
222
- if (seen.has(sid)) continue
223
- sendToClient(ws, { type: 'status', sessionId: sid, processing: true })
224
- }
121
+ addClient(ws)
122
+ // Authoritative snapshot of every running session (CC + OpenClaw) so the
123
+ // client can light/clear spinners correctly even for runs whose status
124
+ // transitions it missed while disconnected.
125
+ const running = [...getCCRunningSessions(), ...getOpenClawRunningSessions()]
126
+ sendToClient(ws, { type: 'status_snapshot', running })
225
127
  } else {
226
- ws.subscribe(MEI_TOPIC)
128
+ ws.subscribe(EVENTS_TOPIC)
227
129
  }
228
130
  },
229
131
  async message(ws, message) {
@@ -232,77 +134,64 @@ export const app = Bun.serve<WsData>({
232
134
  const data = JSON.parse(String(message))
233
135
  if (!isClientMessage(data)) return
234
136
 
235
- if (data.type === 'chat' && data.content?.trim()) {
236
- const workspace = await getWorkspace(ws.data.workspaceId)
137
+ // Allow a message that carries only attachments (no text).
138
+ if (data.type === 'chat' && (data.content?.trim() || data.attachments?.length)) {
139
+ const workspace = await getWorkspace(data.workspaceId)
237
140
  if (!workspace) return
238
141
  if (workspace.type === 'openclaw' && workspace.agentId) {
239
142
  sendOpenClawMessage({
240
- workspaceId: ws.data.workspaceId,
143
+ workspaceId: data.workspaceId,
241
144
  workspacePath: workspace.path,
242
145
  agentId: workspace.agentId,
243
146
  sessionId: data.sessionId,
244
147
  isNew: data.isNew,
245
148
  content: data.content.trim(),
149
+ attachments: data.attachments,
246
150
  optimisticId: data.optimisticId
247
151
  })
248
152
  } else {
249
- handleChat(
250
- data.content.trim(),
251
- data.sessionId,
252
- data.isNew,
253
- ws.data.workspaceId,
254
- workspace.path,
255
- data.optimisticId
256
- )
153
+ // `stream` is only wired for the Claude Code path; the OpenClaw
154
+ // branch above never receives it (unsupported provider).
155
+ sendCCMessage({
156
+ workspaceId: data.workspaceId,
157
+ workspacePath: workspace.path,
158
+ sessionId: data.sessionId,
159
+ isNew: data.isNew,
160
+ content: data.content.trim(),
161
+ attachments: data.attachments,
162
+ optimisticId: data.optimisticId,
163
+ model: data.model,
164
+ effort: data.effort,
165
+ stream: data.stream
166
+ })
257
167
  }
258
168
  }
259
169
  if (data.type === 'stop') {
260
- const workspace = await getWorkspace(ws.data.workspaceId)
170
+ const workspace = await getWorkspace(data.workspaceId)
261
171
  if (workspace?.type === 'openclaw') {
262
- abortOpenClawRun({ workspaceId: ws.data.workspaceId, sessionId: data.sessionId })
172
+ abortOpenClawRun({ workspaceId: data.workspaceId, sessionId: data.sessionId })
263
173
  } else {
264
- stopChat(data.sessionId, ws.data.workspaceId)
174
+ interruptCCSession(data.workspaceId, data.sessionId)
265
175
  }
266
176
  }
177
+ // A tab's reply to a relayed Scratchpad op — settle the pending CLI
178
+ // request (first reply wins; later/duplicate replies are ignored).
179
+ if (data.type === 'scratchpad:op-result') {
180
+ resolveScratchOp(data.opId, data.result, data.error)
181
+ }
267
182
  } catch {}
268
183
  },
269
184
  close(ws) {
270
- if (ws.data.channel === 'chat') removeClient(ws.data.workspaceId, ws)
271
- else ws.unsubscribe(MEI_TOPIC)
185
+ if (ws.data.channel === 'chat') removeClient(ws)
186
+ else ws.unsubscribe(EVENTS_TOPIC)
272
187
  }
273
188
  }
274
189
  })
275
190
 
276
- async function handleFunctionCall(
277
- req: Request,
278
- tail: string,
279
- workspacePath: string
280
- ): Promise<Response> {
281
- const parts = tail.split('/')
282
- if (parts.length !== 2) return new Response('Bad request', { status: 400 })
283
-
284
- const [module, name] = parts
285
- if (!/^[a-zA-Z0-9_$-]+$/.test(module) || !/^[a-zA-Z0-9_$]+$/.test(name)) {
286
- return new Response('Invalid module or function name', { status: 400 })
287
- }
288
-
289
- try {
290
- const contentLength = Number(req.headers.get('content-length') ?? 0)
291
- if (contentLength > 1_000_000) {
292
- return new Response('Request body too large', { status: 413 })
293
- }
294
- const args = await req.text()
295
- const result = await callFunction(module, name, args, workspacePath)
296
- return new Response(result, { headers: { 'Content-Type': 'application/json' } })
297
- } catch (err) {
298
- const message = err instanceof Error ? err.message : 'Unknown error'
299
- return new Response(message, { status: 500 })
300
- }
301
- }
302
-
303
- export function publishMei(msg: unknown) {
304
- app.publish(MEI_TOPIC, JSON.stringify(msg))
305
- }
191
+ // Wire the live-event publisher (`publishEvent`) to this server instance now
192
+ // that it exists. Kept in ./events so control.ts and ./api can publish without
193
+ // importing web.ts (which binds ports on load).
194
+ setEventServer(app)
306
195
 
307
196
  // Graceful shutdown. In dev the supervisor sends SIGTERM on server-file
308
197
  // changes; in any context Ctrl-C sends SIGINT. Close both servers and kill the
@@ -314,6 +203,7 @@ function shutdown() {
314
203
  try {
315
204
  control.stop(true)
316
205
  } catch {}
206
+ killAllCCSessions()
317
207
  killAllWorkers()
318
208
  process.exit(0)
319
209
  }