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.
- package/bin/moi.mjs +3 -1
- package/bunfig.toml +4 -0
- package/client/api/workspaces.ts +277 -6
- package/client/assets/mcp/agentation.png +0 -0
- package/client/assets/mcp/airtable.png +0 -0
- package/client/assets/mcp/apollo.png +0 -0
- package/client/assets/mcp/asana.png +0 -0
- package/client/assets/mcp/box.png +0 -0
- package/client/assets/mcp/chrome.svg +33 -0
- package/client/assets/mcp/clickup.png +0 -0
- package/client/assets/mcp/cloudflare.png +0 -0
- package/client/assets/mcp/context7.png +0 -0
- package/client/assets/mcp/datadog.png +0 -0
- package/client/assets/mcp/dropbox.png +0 -0
- package/client/assets/mcp/elevenlabs.png +0 -0
- package/client/assets/mcp/exa.png +0 -0
- package/client/assets/mcp/firecrawl.png +0 -0
- package/client/assets/mcp/gmail.png +0 -0
- package/client/assets/mcp/googlecalendar.png +0 -0
- package/client/assets/mcp/googledrive.png +0 -0
- package/client/assets/mcp/grafana.png +0 -0
- package/client/assets/mcp/huggingface.png +0 -0
- package/client/assets/mcp/intercom.png +0 -0
- package/client/assets/mcp/monday.png +0 -0
- package/client/assets/mcp/mongodb.png +0 -0
- package/client/assets/mcp/paper.png +0 -0
- package/client/assets/mcp/paypal.png +0 -0
- package/client/assets/mcp/perplexity.png +0 -0
- package/client/assets/mcp/postgresql.png +0 -0
- package/client/assets/mcp/posthog.png +0 -0
- package/client/assets/mcp/puppeteer.png +0 -0
- package/client/assets/mcp/railway.png +0 -0
- package/client/assets/mcp/redis.png +0 -0
- package/client/assets/mcp/salesforce.png +0 -0
- package/client/assets/mcp/shopify.png +0 -0
- package/client/assets/mcp/snowflake.png +0 -0
- package/client/assets/mcp/telegram.png +0 -0
- package/client/assets/mcp/twilio.png +0 -0
- package/client/assets/mcp/zapier.png +0 -0
- package/client/components/App.tsx +7 -1
- package/client/components/BottomPanel.tsx +2 -2
- package/client/components/ChatInput.tsx +195 -14
- package/client/components/ChatPanel.tsx +118 -49
- package/client/components/ChatPopup.tsx +2 -2
- package/client/components/CustomizePanel.tsx +8 -8
- package/client/components/HiddenPanel.tsx +1 -1
- package/client/components/HomePage.tsx +1 -1
- package/client/components/IconMcp.tsx +21 -0
- package/client/components/MarkdownContent.tsx +2 -2
- package/client/components/McpMenu.tsx +125 -0
- package/client/components/ModelPicker.tsx +147 -0
- package/client/components/Scratchpad.tsx +775 -0
- package/client/components/ShimmerText.tsx +20 -0
- package/client/components/ThreadSelector.tsx +2 -2
- package/client/components/TurnView.tsx +114 -534
- package/client/components/WidgetErrorBoundary.tsx +2 -2
- package/client/components/WidgetFrame.tsx +1 -1
- package/client/components/WidgetShell.tsx +2 -2
- package/client/components/Widgets.tsx +2 -2
- package/client/components/WorkspacePreview.tsx +3 -3
- package/client/components/WorkspacesPage.tsx +28 -20
- package/client/components/layout/SidebarLayout.tsx +24 -15
- package/client/components/playground/PixelPainter.tsx +2 -2
- package/client/components/playground/PlaygroundPage.tsx +1 -1
- package/client/components/playground/ToolCallsPage.tsx +81 -0
- package/client/components/routes/workspace/[id].tsx +534 -141
- package/client/components/routes/workspace/create.tsx +155 -0
- package/client/components/settings/IconPicker.tsx +590 -0
- package/client/components/settings/SettingsPages.tsx +307 -0
- package/client/components/settings/WorkspaceSettings.tsx +92 -0
- package/client/components/tool-group/CodeBlock.tsx +41 -0
- package/client/components/tool-group/Collapse.tsx +28 -0
- package/client/components/tool-group/ReadImagePreview.tsx +46 -0
- package/client/components/tool-group/SubagentCard.tsx +135 -0
- package/client/components/tool-group/TimelineRow.tsx +147 -0
- package/client/components/tool-group/ToolCallGroup.tsx +231 -0
- package/client/components/tool-group/ToolOutput.tsx +139 -0
- package/client/components/tool-group/detect.ts +93 -0
- package/client/components/tool-group/fixtures.ts +315 -0
- package/client/components/tool-group/format.ts +211 -0
- package/client/components/tool-group/index.ts +9 -0
- package/client/components/ui/button.tsx +4 -4
- package/client/components/ui/dialog.tsx +58 -0
- package/client/components/ui/dropdown-menu.tsx +9 -9
- package/client/components/ui/hover-card.tsx +44 -0
- package/client/components/ui/popover.tsx +1 -1
- package/client/components/ui/switch.tsx +23 -0
- package/client/components/ui/tooltip.tsx +54 -0
- package/client/externalize-react.ts +11 -5
- package/client/hooks/useApplet.ts +117 -0
- package/client/hooks/useChat.ts +162 -62
- package/client/hooks/useMeiEvents.ts +9 -2
- package/client/hooks/useStickToBottom.ts +86 -0
- package/client/hooks/useWorkspaceTheme.ts +8 -2
- package/client/index.css +49 -3
- package/client/index.html +22 -18
- package/client/index.tsx +29 -2
- package/client/lib/WorkspaceContext.tsx +5 -8
- package/client/lib/WorkspaceLayoutContext.tsx +30 -10
- package/client/lib/applet-cache.ts +54 -0
- package/client/lib/connection.ts +237 -0
- package/client/lib/flags.ts +10 -0
- package/client/lib/mcp-icons.ts +142 -10
- package/client/lib/preview-turn.ts +37 -0
- package/client/lib/render-icon.ts +173 -0
- package/client/lib/scratch-executor.ts +20 -0
- package/client/lib/uploads.ts +18 -0
- package/client/main.tsx +5 -0
- package/client/store/live.ts +282 -0
- package/client/structured-clone-shim.ts +14 -0
- package/client/theme.css +2 -2
- package/client/vendor/emojibase/en/data.json +1 -0
- package/client/vendor/emojibase/en/messages.json +1 -0
- package/client/vendor/react/development/_impl/react-dom-client.js +15871 -0
- package/client/vendor/react/development/_impl/react-dom.js +223 -0
- package/client/vendor/react/development/_impl/react-jsx-dev-runtime.js +256 -0
- package/client/vendor/react/development/_impl/react-jsx-runtime.js +260 -0
- package/client/vendor/react/development/_impl/react.js +863 -0
- package/client/vendor/react/development/react-dom-client.js +6 -0
- package/client/vendor/react/development/react-dom.js +17 -0
- package/client/vendor/react/development/react-jsx-dev-runtime.js +5 -0
- package/client/vendor/react/development/react-jsx-runtime.js +6 -0
- package/client/vendor/react/development/react.js +47 -0
- package/client/vendor/react/production/_impl/react-dom-client.js +10361 -0
- package/client/vendor/react/production/_impl/react-dom.js +184 -0
- package/client/vendor/react/production/_impl/react-jsx-dev-runtime.js +37 -0
- package/client/vendor/react/production/_impl/react-jsx-runtime.js +60 -0
- package/client/vendor/react/production/_impl/react.js +428 -0
- package/client/vendor/react/production/react-dom-client.js +6 -0
- package/client/vendor/react/production/react-dom.js +17 -0
- package/client/vendor/react/production/react-jsx-dev-runtime.js +5 -0
- package/client/vendor/react/production/react-jsx-runtime.js +6 -0
- package/client/vendor/react/production/react.js +47 -0
- package/dist/agentation-4s0nn45p.png +0 -0
- package/dist/airtable-hsa66v6q.png +0 -0
- package/dist/apollo-2ed5dy2d.png +0 -0
- package/dist/asana-tvgtwbx2.png +0 -0
- package/dist/atlassian-b3tq39hs.png +0 -0
- package/dist/box-w3n36t5g.png +0 -0
- package/dist/chrome-tspm94rw.svg +33 -0
- package/dist/chunk-kc6e9nd4.css +1 -0
- package/dist/chunk-yf3p7qg7.js +673 -0
- package/dist/claude-z2xnay0d.svg +3 -0
- package/dist/clickup-qqrqqvqb.png +0 -0
- package/dist/cloudflare-j9p931v6.png +0 -0
- package/dist/context7-8ht0xjfy.png +0 -0
- package/dist/datadog-70ne3yzt.png +0 -0
- package/dist/discord-vejhptwx.png +0 -0
- package/dist/dropbox-4m5s9mgr.png +0 -0
- package/dist/elevenlabs-2nzzej1t.png +0 -0
- package/dist/exa-qqj3vr0x.png +0 -0
- package/dist/favicon-nbmyjfjh.png +0 -0
- package/dist/figma-y9898rry.png +0 -0
- package/dist/firecrawl-w2t8nehx.png +0 -0
- package/dist/github-av5r5y3e.png +0 -0
- package/dist/gitlab-4pv5vm6b.png +0 -0
- package/dist/gmail-sg6a5v3x.png +0 -0
- package/dist/googlecalendar-nqhh2v7r.png +0 -0
- package/dist/googledrive-6s8xyab7.png +0 -0
- package/dist/grafana-1yfsnwr6.png +0 -0
- package/dist/granola-7j3b0nk2.png +0 -0
- package/dist/hermes-nous-pznd63x9.png +0 -0
- package/dist/hubspot-hghcmc2t.png +0 -0
- package/dist/huggingface-kxzaxj83.png +0 -0
- package/dist/index.html +47 -0
- package/dist/intercom-td763jt8.png +0 -0
- package/dist/linear-v9s3az2d.png +0 -0
- package/dist/monday-jdbs4wwf.png +0 -0
- package/dist/mongodb-emzxvhd5.png +0 -0
- package/dist/neon-zj4qar44.png +0 -0
- package/dist/notion-w034c0b8.png +0 -0
- package/dist/openclaw-a2pyfhpn.svg +22 -0
- package/dist/paper-zbt34fab.png +0 -0
- package/dist/paypal-kwwf7xsc.png +0 -0
- package/dist/perplexity-4y63rxvj.png +0 -0
- package/dist/postgresql-qsspbsr1.png +0 -0
- package/dist/posthog-sphppc4w.png +0 -0
- package/dist/puppeteer-2xjt1a9b.png +0 -0
- package/dist/railway-834de8d2.png +0 -0
- package/dist/redis-spm6w2m1.png +0 -0
- package/dist/salesforce-1mk56v4z.png +0 -0
- package/dist/sentry-hdwpmvek.png +0 -0
- package/dist/shopify-90dadaav.png +0 -0
- package/dist/slack-1c1wj62s.png +0 -0
- package/dist/snowflake-hfpykpgy.png +0 -0
- package/dist/stripe-zrfaqbj3.png +0 -0
- package/dist/supabase-4vg33zf2.png +0 -0
- package/dist/telegram-v6tjyrs7.png +0 -0
- package/dist/twilio-dpyjdwpj.png +0 -0
- package/dist/vercel-7d0ssw28.png +0 -0
- package/dist/zapier-p67qfch0.png +0 -0
- package/lib/attachment-note.ts +48 -0
- package/lib/claude-adapter.ts +219 -29
- package/lib/format.ts +32 -0
- package/lib/types.ts +314 -6
- package/package.json +21 -31
- package/server/CLAUDE.md +3 -0
- package/server/agent.ts +25 -118
- package/server/api.ts +593 -0
- package/server/applets.ts +396 -0
- package/server/build-applet.ts +580 -0
- package/server/cc-session.ts +611 -0
- package/server/cli-colors.ts +17 -0
- package/server/cli-ui.ts +52 -0
- package/server/cli.ts +970 -72
- package/server/control.ts +166 -19
- package/server/debug.ts +10 -0
- package/server/events.ts +24 -0
- package/server/functions-worker.ts +18 -2
- package/server/functions.ts +59 -12
- package/server/icon.ts +13 -0
- package/server/layout.ts +25 -3
- package/server/mcp.ts +125 -0
- package/server/moi-scaffold.ts +85 -0
- package/server/openclaw-session.ts +41 -10
- package/server/openclaw.ts +22 -0
- package/server/preview.ts +70 -0
- package/server/registry.ts +32 -2
- package/server/scratchpad-executor.ts +380 -0
- package/server/scratchpad-relay.ts +45 -0
- package/server/scratchpad.ts +171 -0
- package/server/skill-version.ts +146 -0
- package/server/skills-template.ts +6 -1
- package/server/state.ts +22 -36
- package/server/static.ts +23 -0
- package/server/status.ts +94 -0
- package/server/thread-config.ts +125 -0
- package/server/uploads.ts +0 -0
- package/server/vendor.ts +63 -0
- package/server/views.ts +0 -0
- package/server/web.ts +113 -223
- package/server/widgets.ts +52 -142
- package/server/workspace-config.ts +37 -0
- package/server/workspace-env.ts +407 -0
- package/server/workspace-init.ts +66 -0
- package/workspace/.claude/skills/moi-workspace/DESIGN.md +109 -0
- package/workspace/.claude/skills/moi-workspace/SCRATCHPAD.md +57 -0
- package/workspace/.claude/skills/moi-workspace/SKILL.md +252 -0
- package/workspace/.claude/skills/moi-workspace/VIEW-DESIGN.md +22 -0
- package/client/hooks/useWidget.ts +0 -72
- package/client/hooks/useWidgetList.ts +0 -30
- package/client/lib/ws.ts +0 -129
- package/client/store/chat.tsx +0 -162
- package/server/build-widget.ts +0 -343
- package/workspace/.claude/skills/widgets/DESIGN.md +0 -630
- 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 {
|
|
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 {
|
|
8
|
-
import {
|
|
9
|
-
import {
|
|
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
|
-
|
|
14
|
-
getLiveOpenClawEvents,
|
|
15
|
-
getOpenClawProcessingSessions,
|
|
18
|
+
getOpenClawRunningSessions,
|
|
16
19
|
sendOpenClawMessage
|
|
17
20
|
} from './openclaw-session'
|
|
18
|
-
import {
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
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
|
-
|
|
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
|
-
|
|
81
|
+
// HMR only in dev; prod serves prebuilt static assets (no bundler).
|
|
82
|
+
development: prebuilt ? false : { hmr: true },
|
|
71
83
|
routes: {
|
|
72
|
-
//
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
'/
|
|
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
|
|
179
|
-
//
|
|
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: '
|
|
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
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
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(
|
|
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
|
-
|
|
236
|
-
|
|
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:
|
|
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
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
data.
|
|
253
|
-
|
|
254
|
-
|
|
255
|
-
data.
|
|
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(
|
|
170
|
+
const workspace = await getWorkspace(data.workspaceId)
|
|
261
171
|
if (workspace?.type === 'openclaw') {
|
|
262
|
-
abortOpenClawRun({ workspaceId:
|
|
172
|
+
abortOpenClawRun({ workspaceId: data.workspaceId, sessionId: data.sessionId })
|
|
263
173
|
} else {
|
|
264
|
-
|
|
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
|
|
271
|
-
else ws.unsubscribe(
|
|
185
|
+
if (ws.data.channel === 'chat') removeClient(ws)
|
|
186
|
+
else ws.unsubscribe(EVENTS_TOPIC)
|
|
272
187
|
}
|
|
273
188
|
}
|
|
274
189
|
})
|
|
275
190
|
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
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
|
}
|