moi-computer 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (106) hide show
  1. package/LICENSE +95 -0
  2. package/README.md +71 -0
  3. package/bin/moi.mjs +25 -0
  4. package/bunfig.toml +3 -0
  5. package/client/CLAUDE.md +12 -0
  6. package/client/api/workspaces.ts +158 -0
  7. package/client/assets/claude.svg +3 -0
  8. package/client/assets/favicon.png +0 -0
  9. package/client/assets/hermes-nous.png +0 -0
  10. package/client/assets/mcp/.gitkeep +0 -0
  11. package/client/assets/mcp/atlassian.png +0 -0
  12. package/client/assets/mcp/discord.png +0 -0
  13. package/client/assets/mcp/figma.png +0 -0
  14. package/client/assets/mcp/github.png +0 -0
  15. package/client/assets/mcp/gitlab.png +0 -0
  16. package/client/assets/mcp/granola.png +0 -0
  17. package/client/assets/mcp/hubspot.png +0 -0
  18. package/client/assets/mcp/linear.png +0 -0
  19. package/client/assets/mcp/neon.png +0 -0
  20. package/client/assets/mcp/notion.png +0 -0
  21. package/client/assets/mcp/sentry.png +0 -0
  22. package/client/assets/mcp/slack.png +0 -0
  23. package/client/assets/mcp/stripe.png +0 -0
  24. package/client/assets/mcp/supabase.png +0 -0
  25. package/client/assets/mcp/vercel.png +0 -0
  26. package/client/assets/moi-logo.svg +11 -0
  27. package/client/assets/openclaw.svg +22 -0
  28. package/client/components/App.tsx +35 -0
  29. package/client/components/BottomPanel.tsx +33 -0
  30. package/client/components/ChatInput.tsx +62 -0
  31. package/client/components/ChatPanel.tsx +206 -0
  32. package/client/components/ChatPopup.tsx +60 -0
  33. package/client/components/CustomizePanel.tsx +117 -0
  34. package/client/components/HiddenPanel.tsx +79 -0
  35. package/client/components/HomePage.tsx +17 -0
  36. package/client/components/MarkdownContent.tsx +41 -0
  37. package/client/components/ThreadSelector.tsx +88 -0
  38. package/client/components/TurnView.tsx +618 -0
  39. package/client/components/WidgetErrorBoundary.tsx +46 -0
  40. package/client/components/WidgetFrame.tsx +66 -0
  41. package/client/components/WidgetGrid.tsx +113 -0
  42. package/client/components/WidgetShell.tsx +44 -0
  43. package/client/components/Widgets.tsx +132 -0
  44. package/client/components/WorkspacePreview.tsx +48 -0
  45. package/client/components/WorkspacesPage.tsx +237 -0
  46. package/client/components/layout/SidebarLayout.tsx +260 -0
  47. package/client/components/playground/LedLogo.tsx +181 -0
  48. package/client/components/playground/PixelPainter.tsx +105 -0
  49. package/client/components/playground/PlaygroundPage.tsx +71 -0
  50. package/client/components/routes/workspace/[id].tsx +282 -0
  51. package/client/components/ui/button.tsx +48 -0
  52. package/client/components/ui/dropdown-menu.tsx +257 -0
  53. package/client/components/ui/input.tsx +21 -0
  54. package/client/components/ui/popover.tsx +79 -0
  55. package/client/components/ui/textarea.tsx +18 -0
  56. package/client/externalize-react.ts +29 -0
  57. package/client/hooks/useChat.ts +88 -0
  58. package/client/hooks/useFitsSidebar.ts +34 -0
  59. package/client/hooks/useGridReconcile.ts +73 -0
  60. package/client/hooks/useMeiEvents.ts +66 -0
  61. package/client/hooks/useScrollFade.ts +38 -0
  62. package/client/hooks/useWidget.ts +72 -0
  63. package/client/hooks/useWidgetList.ts +30 -0
  64. package/client/hooks/useWorkspaceTheme.ts +76 -0
  65. package/client/index.css +182 -0
  66. package/client/index.html +43 -0
  67. package/client/index.tsx +19 -0
  68. package/client/lib/WorkspaceContext.tsx +24 -0
  69. package/client/lib/WorkspaceLayoutContext.tsx +92 -0
  70. package/client/lib/cn.ts +6 -0
  71. package/client/lib/grid-pack.ts +35 -0
  72. package/client/lib/group-turns.ts +44 -0
  73. package/client/lib/mcp-icons.ts +53 -0
  74. package/client/lib/ws.ts +129 -0
  75. package/client/main.tsx +23 -0
  76. package/client/store/chat.tsx +162 -0
  77. package/client/store/ui.ts +21 -0
  78. package/client/theme.css +66 -0
  79. package/lib/claude-adapter.ts +602 -0
  80. package/lib/format.ts +212 -0
  81. package/lib/themes.ts +89 -0
  82. package/lib/types.ts +144 -0
  83. package/package.json +117 -0
  84. package/server/CLAUDE.md +6 -0
  85. package/server/agent.ts +132 -0
  86. package/server/build-widget.ts +343 -0
  87. package/server/cli.ts +632 -0
  88. package/server/constants.ts +3 -0
  89. package/server/control.ts +98 -0
  90. package/server/functions-worker.ts +89 -0
  91. package/server/functions.ts +171 -0
  92. package/server/layout.ts +83 -0
  93. package/server/openclaw-adapter.ts +242 -0
  94. package/server/openclaw-gateway.ts +255 -0
  95. package/server/openclaw-session.ts +485 -0
  96. package/server/openclaw-strip.ts +134 -0
  97. package/server/openclaw.ts +206 -0
  98. package/server/registry.ts +150 -0
  99. package/server/skills-template.ts +21 -0
  100. package/server/state.ts +87 -0
  101. package/server/theme.ts +55 -0
  102. package/server/web.ts +321 -0
  103. package/server/widgets.ts +254 -0
  104. package/tsconfig.json +21 -0
  105. package/workspace/.claude/skills/widgets/DESIGN.md +630 -0
  106. package/workspace/.claude/skills/widgets/SKILL.md +176 -0
@@ -0,0 +1,255 @@
1
+ // Shared, long-lived OpenClaw gateway client + event multiplexer.
2
+ //
3
+ // Discovery (`server/openclaw.ts`) opens-and-closes a one-shot client per RPC.
4
+ // Streaming needs a single persistent connection so we can subscribe and keep
5
+ // receiving server-pushed `event` frames (`session.message`, `sessions.changed`,
6
+ // `agent`, `chat`). This module owns that connection.
7
+ //
8
+ // Surface:
9
+ // - getGateway() → lazy connect, return { rpc, on, off }
10
+ // - onEvent type → matches `EventFrame` from the SDK, but read defensively
11
+ // - reattach → on reconnect we re-issue every `sessions.subscribe()`
12
+ // and `sessions.messages.subscribe({ key })` we held.
13
+ //
14
+ // Failure mode: if config or the gateway is unreachable, `getGateway()` rejects
15
+ // — callers (chat send, reattach) surface that as an error frame to the client.
16
+ import { readFile } from 'node:fs/promises'
17
+ import { homedir } from 'node:os'
18
+ import { join } from 'node:path'
19
+
20
+ type Rpc = <T>(method: string, params?: Record<string, unknown>) => Promise<T>
21
+ type Listener = (event: string, payload: Record<string, unknown>) => void
22
+
23
+ type ClientCtor = typeof import('openclaw/plugin-sdk/gateway-runtime').GatewayClient
24
+ type GatewayInstance = InstanceType<ClientCtor>
25
+
26
+ const CONNECT_TIMEOUT_MS = 5_000
27
+ const REQUEST_TIMEOUT_MS = 30_000
28
+
29
+ let pending: Promise<GatewayHandle> | null = null
30
+ let current: { handle: GatewayHandle; client: GatewayInstance } | null = null
31
+
32
+ const listeners = new Set<Listener>()
33
+ const sessionSubscriptions = new Set<string>() // sessionKeys we hold open
34
+ let topLevelSubscribed = false
35
+
36
+ // Side-effect hook: callers (the live session module) register a one-time
37
+ // callback that fires after the gateway has reconnected and re-issued every
38
+ // previously-held subscription. Used to run a transcript reconcile so any
39
+ // durable rows committed during the disconnect are merged.
40
+ type ReconnectCallback = () => Promise<void> | void
41
+ const reconnectCallbacks = new Set<ReconnectCallback>()
42
+ export function onGatewayReconnected(cb: ReconnectCallback): () => void {
43
+ reconnectCallbacks.add(cb)
44
+ return () => reconnectCallbacks.delete(cb)
45
+ }
46
+
47
+ let reconnectTimer: ReturnType<typeof setTimeout> | null = null
48
+ let droppedSessionKeys: string[] = []
49
+ const RECONNECT_DELAY_MS = 1500
50
+
51
+ function scheduleReconnect() {
52
+ if (reconnectTimer) return
53
+ reconnectTimer = setTimeout(async () => {
54
+ reconnectTimer = null
55
+ if (current) return // someone else already reconnected via getGateway()
56
+ try {
57
+ const gw = await getGateway()
58
+ // Replay any keys that were held before the drop.
59
+ const keys = droppedSessionKeys
60
+ droppedSessionKeys = []
61
+ for (const key of keys) {
62
+ await gw.ensureSessionSubscribed(key).catch(() => {})
63
+ }
64
+ // Notify session-level holders so they can run a transcript reconcile.
65
+ for (const cb of reconnectCallbacks) {
66
+ try {
67
+ await cb()
68
+ } catch (err) {
69
+ console.error('[openclaw-gateway] reconnect callback threw', err)
70
+ }
71
+ }
72
+ } catch {
73
+ // Connect still failing — schedule another attempt.
74
+ droppedSessionKeys = droppedSessionKeys.length ? droppedSessionKeys : []
75
+ scheduleReconnect()
76
+ }
77
+ }, RECONNECT_DELAY_MS)
78
+ }
79
+
80
+ export type GatewayHandle = {
81
+ rpc: Rpc
82
+ on: (l: Listener) => () => void
83
+ ensureSessionSubscribed: (sessionKey: string) => Promise<void>
84
+ ensureTopLevelSubscribed: () => Promise<void>
85
+ isConnected: () => boolean
86
+ }
87
+
88
+ async function readGatewayConfig(): Promise<{ port: number; token: string } | null> {
89
+ try {
90
+ const raw = await readFile(join(homedir(), '.openclaw/openclaw.json'), 'utf8')
91
+ const cfg = JSON.parse(raw) as {
92
+ gateway?: { port?: number; auth?: { token?: string } }
93
+ }
94
+ const port = cfg.gateway?.port
95
+ const token = cfg.gateway?.auth?.token
96
+ if (typeof port !== 'number' || typeof token !== 'string') return null
97
+ return { port, token }
98
+ } catch {
99
+ return null
100
+ }
101
+ }
102
+
103
+ function fanout(event: string, payload: Record<string, unknown>) {
104
+ for (const l of listeners) {
105
+ try {
106
+ l(event, payload)
107
+ } catch (err) {
108
+ console.error('[openclaw-gateway] listener threw', err)
109
+ }
110
+ }
111
+ }
112
+
113
+ async function startClient(): Promise<{ handle: GatewayHandle; client: GatewayInstance }> {
114
+ const cfg = await readGatewayConfig()
115
+ if (!cfg) throw new Error('openclaw config missing or invalid')
116
+ const { GatewayClient } = await import('openclaw/plugin-sdk/gateway-runtime')
117
+
118
+ let connected = false
119
+ const client = new GatewayClient({
120
+ url: `ws://127.0.0.1:${cfg.port}`,
121
+ token: cfg.token,
122
+ role: 'operator',
123
+ scopes: ['operator.admin', 'operator.read', 'operator.write'],
124
+ requestTimeoutMs: REQUEST_TIMEOUT_MS,
125
+ onEvent: evt => {
126
+ const event = (evt as unknown as { event?: unknown }).event
127
+ const payload = (evt as unknown as { payload?: unknown }).payload
128
+ if (typeof event !== 'string') return
129
+ fanout(
130
+ event,
131
+ (payload && typeof payload === 'object' ? payload : {}) as Record<string, unknown>
132
+ )
133
+ },
134
+ onClose: () => {
135
+ connected = false
136
+ current = null
137
+ pending = null
138
+ // Mark every held subscription as needing re-issue after the next
139
+ // connect. The Set entries themselves stay so we know what to re-issue.
140
+ topLevelSubscribed = false
141
+ const dropped = [...sessionSubscriptions]
142
+ sessionSubscriptions.clear()
143
+ droppedSessionKeys = dropped
144
+ scheduleReconnect()
145
+ }
146
+ })
147
+
148
+ await new Promise<void>((res, rej) => {
149
+ const t = setTimeout(
150
+ () => rej(new Error(`openclaw connect timeout after ${CONNECT_TIMEOUT_MS}ms`)),
151
+ CONNECT_TIMEOUT_MS
152
+ )
153
+ client.opts.onHelloOk = () => {
154
+ clearTimeout(t)
155
+ connected = true
156
+ res()
157
+ }
158
+ client.opts.onConnectError = err => {
159
+ clearTimeout(t)
160
+ rej(err)
161
+ }
162
+ client.start()
163
+ })
164
+
165
+ const rpc: Rpc = (method, params = {}) => client.request(method, params) as Promise<never>
166
+
167
+ const handle: GatewayHandle = {
168
+ rpc,
169
+ on(l) {
170
+ listeners.add(l)
171
+ return () => listeners.delete(l)
172
+ },
173
+ async ensureSessionSubscribed(sessionKey: string) {
174
+ if (sessionSubscriptions.has(sessionKey)) return
175
+ sessionSubscriptions.add(sessionKey)
176
+ try {
177
+ await rpc('sessions.messages.subscribe', { key: sessionKey })
178
+ } catch (err) {
179
+ sessionSubscriptions.delete(sessionKey)
180
+ throw err
181
+ }
182
+ },
183
+ async ensureTopLevelSubscribed() {
184
+ if (topLevelSubscribed) return
185
+ topLevelSubscribed = true
186
+ try {
187
+ await rpc('sessions.subscribe', {})
188
+ } catch (err) {
189
+ topLevelSubscribed = false
190
+ throw err
191
+ }
192
+ },
193
+ isConnected: () => connected
194
+ }
195
+
196
+ // On reconnect (if `GatewayClient` reconnects automatically) re-issue all
197
+ // subscriptions. The SDK does have backoff reconnect; if it fires onClose we
198
+ // tear down `current` so the next getGateway() rebuilds and re-subscribes.
199
+ return { handle, client }
200
+ }
201
+
202
+ export async function getGateway(): Promise<GatewayHandle> {
203
+ if (current) return current.handle
204
+ if (pending) return pending
205
+ pending = (async () => {
206
+ try {
207
+ const built = await startClient()
208
+ current = built
209
+ // Always re-issue the top-level subscription so `sessions.changed`
210
+ // events resume; per-session keys are replayed by `scheduleReconnect`
211
+ // (or by the next caller of ensureSessionSubscribed).
212
+ await built.handle.ensureTopLevelSubscribed().catch(() => {})
213
+ return built.handle
214
+ } catch (err) {
215
+ pending = null
216
+ throw err
217
+ }
218
+ })()
219
+ return pending
220
+ }
221
+
222
+ // Low-level escape hatch for the discovery path; keep a one-shot client
223
+ // available alongside the persistent one so we don't change discovery's
224
+ // failure mode (silent, fast).
225
+ export async function withOneShotGateway<T>(fn: (rpc: Rpc) => Promise<T>): Promise<T | null> {
226
+ const cfg = await readGatewayConfig()
227
+ if (!cfg) return null
228
+ const { GatewayClient } = await import('openclaw/plugin-sdk/gateway-runtime')
229
+ const client = new GatewayClient({
230
+ url: `ws://127.0.0.1:${cfg.port}`,
231
+ token: cfg.token,
232
+ role: 'operator',
233
+ scopes: ['operator.admin', 'operator.read', 'operator.write'],
234
+ requestTimeoutMs: 2000
235
+ })
236
+ try {
237
+ await new Promise<void>((res, rej) => {
238
+ const t = setTimeout(() => rej(new Error('openclaw connect timeout')), 2000)
239
+ client.opts.onHelloOk = () => {
240
+ clearTimeout(t)
241
+ res()
242
+ }
243
+ client.opts.onConnectError = e => {
244
+ clearTimeout(t)
245
+ rej(e)
246
+ }
247
+ client.start()
248
+ })
249
+ return await fn((m, p = {}) => client.request(m, p) as Promise<never>)
250
+ } catch {
251
+ return null
252
+ } finally {
253
+ client.stop()
254
+ }
255
+ }