moi-computer 0.4.0 → 0.4.1-next.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +41 -5
- package/bin/moi.mjs +11 -0
- package/client/api/workspace-keys.ts +1 -0
- package/client/app/AppRouter.tsx +5 -1
- package/client/app/routes/WorkspaceRoute.tsx +9 -5
- package/client/app/shell/SidebarLayout.tsx +9 -7
- package/client/components/shared/BottomPanel.tsx +24 -5
- package/client/components/shared/Composer.tsx +1 -1
- package/client/components/shared/PanelHeader.tsx +1 -1
- package/client/components/ui/button.tsx +5 -5
- package/client/components/ui/dropdown-menu.tsx +6 -6
- package/client/components/ui/hover-card.tsx +1 -1
- package/client/components/ui/input.tsx +1 -1
- package/client/components/ui/popover.tsx +1 -1
- package/client/components/ui/skeleton.tsx +1 -1
- package/client/components/ui/textarea.tsx +1 -1
- package/client/components/ui/tooltip.tsx +1 -1
- package/client/features/applets/applet-cache.ts +20 -1
- package/client/features/applets/applet-log.ts +10 -30
- package/client/features/applets/applet-runtime.ts +272 -0
- package/client/features/applets/useApplet.ts +36 -10
- package/client/features/chat/ChatComposer.tsx +1 -1
- package/client/features/chat/ChatEmptyState.tsx +186 -0
- package/client/features/chat/ChatPanel.tsx +95 -54
- package/client/features/chat/ChatPopup.tsx +3 -13
- package/client/features/chat/ChatPromptBubbles.tsx +74 -0
- package/client/features/chat/MarkdownContent.tsx +15 -3
- package/client/features/chat/ModelPicker.tsx +1 -1
- package/client/features/chat/TurnView.tsx +6 -17
- package/client/features/chat/chat-send.ts +26 -1
- package/client/features/chat/tool-group/CodeBlock.tsx +1 -1
- package/client/features/chat/tool-group/ReadImagePreview.tsx +1 -1
- package/client/features/chat/tool-group/SubagentCard.tsx +1 -1
- package/client/features/chat/tool-group/TimelineRow.tsx +4 -4
- package/client/features/chat/tool-group/ToolCallGroup.tsx +12 -5
- package/client/features/chat/tool-group/ToolOutput.tsx +2 -2
- package/client/features/chat/tool-group/format.ts +3 -3
- package/client/features/chat/useAppletChatMessage.ts +69 -0
- package/client/features/chat/useChat.ts +49 -14
- package/client/features/chat/useStickToBottom.ts +11 -1
- package/client/features/connectors/McpServerCards.tsx +3 -3
- package/client/features/dev/HarnessDebugPage.tsx +4 -4
- package/client/features/dev/PixelPainter.tsx +1 -1
- package/client/features/dev/PlaygroundPage.tsx +1 -1
- package/client/features/dev/ToolCallsPage.tsx +2 -2
- package/client/features/home/HomePage.tsx +17 -14
- package/client/features/home/WorkspacePreview.tsx +12 -13
- package/client/features/home/api.ts +8 -5
- package/client/features/home/workspace-presentation.tsx +1 -1
- package/client/features/home/workspace-setup/CreateWorkspaceDialog.tsx +1 -1
- package/client/features/home/workspace-setup/WorkspaceAgentStep.tsx +21 -8
- package/client/features/scratchpad/Scratchpad.tsx +41 -2
- package/client/features/scratchpad/ScratchpadControls.tsx +13 -13
- package/client/features/scratchpad/useScratchpadSnapshot.tsx +1 -1
- package/client/features/settings/EnvironmentSettings.tsx +1 -1
- package/client/features/settings/IconPicker.tsx +11 -11
- package/client/features/settings/SettingsLayout.tsx +1 -1
- package/client/features/settings/WorkspaceSettings.tsx +2 -2
- package/client/features/settings/api.ts +33 -1
- package/client/features/widgets/HiddenPanel.tsx +4 -3
- package/client/features/widgets/Widgets.tsx +1 -0
- package/client/features/workspace/CustomizePanel.tsx +96 -82
- package/client/features/workspace/WorkspaceScreen.tsx +129 -107
- package/client/features/workspace/WorkspaceSkillUpdateBanner.tsx +91 -0
- package/client/features/workspace/WorkspaceTabs.tsx +1 -1
- package/client/features/workspace/api.ts +52 -0
- package/client/features/workspace/moi-context.ts +63 -17
- package/client/features/workspace/tab-resolution.ts +75 -0
- package/client/features/workspace/useWorkspaceNavigation.ts +128 -0
- package/client/features/workspace/useWorkspaceSkillUpdates.ts +160 -0
- package/client/index.css +10 -15
- package/client/lib/app-icon-registry.ts +77 -70
- package/client/lib/rate-limit.ts +77 -0
- package/client/runtime/useWorkspaceEvents.ts +15 -2
- package/client/runtime/workspace-theme.ts +115 -0
- package/client/store/ui.ts +42 -17
- package/client/theme.css +20 -15
- package/dist/{chunk-fh2mf6v9.js → chunk-3m1xe1j3.js} +94 -94
- package/dist/chunk-5vaevt8z.js +2 -0
- package/dist/{chunk-69nhbjhy.js → chunk-7bzpmtxp.js} +4 -4
- package/dist/chunk-h0hk61pm.js +9 -0
- package/dist/{chunk-35fq49qa.js → chunk-h3evrtyp.js} +1 -1
- package/dist/chunk-k5sjx9s5.js +1 -0
- package/dist/{chunk-2vaafjp4.js → chunk-m9qja0nx.js} +1 -1
- package/dist/chunk-s7za1qsv.css +1 -0
- package/dist/{chunk-72atbmh3.js → chunk-tcfa3vt3.js} +1 -1
- package/dist/chunk-v797g6ap.js +51 -0
- package/dist/chunk-xyrw8318.js +27 -0
- package/dist/index.html +1 -1
- package/lib/app-icons.ts +73 -0
- package/lib/moi-context.ts +104 -3
- package/lib/themes.ts +83 -45
- package/lib/types.ts +44 -9
- package/lib/workspace-tabs.ts +48 -0
- package/package.json +4 -1
- package/server/api.ts +57 -5
- package/server/app-settings.ts +65 -0
- package/server/applets.ts +132 -22
- package/server/bundler/build-applet.ts +74 -16
- package/server/cli.ts +144 -17
- package/server/control.ts +53 -7
- package/server/data-dir.ts +1 -1
- package/server/events.ts +1 -1
- package/server/functions-worker.ts +15 -1
- package/server/functions.ts +51 -6
- package/server/layout.ts +9 -14
- package/server/moi-scaffold.ts +37 -3
- package/server/registry.ts +1 -1
- package/server/skill-update.ts +47 -0
- package/server/skill-version.ts +2 -11
- package/server/tabs.ts +59 -0
- package/server/theme.ts +8 -14
- package/server/view-builders.ts +6 -2
- package/workspace/.claude/skills/moi-workspace/DESIGN.md +265 -107
- package/workspace/.claude/skills/moi-workspace/SKILL.md +85 -11
- package/client/features/workspace/useWorkspaceTheme.ts +0 -82
- package/dist/chunk-19g80zc9.css +0 -1
- package/dist/chunk-7mkv2gr0.js +0 -2
- package/dist/chunk-gt7n06ef.js +0 -1
- package/dist/chunk-jwmc32rp.js +0 -63
- package/dist/chunk-kh24bstm.js +0 -10
- package/dist/chunk-sfa4mbmx.js +0 -15
- package/workspace/.claude/skills/moi-workspace/VIEW-DESIGN.md +0 -22
|
@@ -0,0 +1,272 @@
|
|
|
1
|
+
// The workspace applet runtime — the host side of the applet `moi` module.
|
|
2
|
+
//
|
|
3
|
+
// Every applet bundle inlines its own copy of the `moi` virtual module (see
|
|
4
|
+
// MOI_MODULE_SOURCE in server/bundler/build-applet.ts), so each loaded module
|
|
5
|
+
// instance holds a private `bridge` slot. Right after the dynamic import, the
|
|
6
|
+
// host connects that instance to the workspace's runtime by attaching a thin
|
|
7
|
+
// bridge (`attachAppletBridge`); invalidation disposes it (`disposeAppletBridge`),
|
|
8
|
+
// leaving a stale module instance — old timers, old listeners — inert instead
|
|
9
|
+
// of steering the app. One runtime per workspace id.
|
|
10
|
+
//
|
|
11
|
+
// Applet calls surface as runtime EVENTS: the bridge validates the untrusted
|
|
12
|
+
// args, then emits, and each host feature subscribes to its own concern with
|
|
13
|
+
// `useAppletEvent` (navigation owns `focusTab`; chat will own `sendChatMessage`)
|
|
14
|
+
// — no central handlers object assembled by the screen. Applet → host only;
|
|
15
|
+
// if a host → applet direction is ever added (`moi.on(...)`), `dispose` must
|
|
16
|
+
// also unbind those listeners or a disposed module leaks.
|
|
17
|
+
import { useEffect, useRef } from 'react'
|
|
18
|
+
|
|
19
|
+
import { createNanoEvents } from 'nanoevents'
|
|
20
|
+
|
|
21
|
+
import { reportAppletError } from '@/client/features/applets/applet-log'
|
|
22
|
+
import { createRateLimiter } from '@/client/lib/rate-limit'
|
|
23
|
+
import { MAX_APPLET_CONTEXT_CHARS } from '@/lib/moi-context'
|
|
24
|
+
import type { AppletKind, WorkspaceTabId } from '@/lib/types'
|
|
25
|
+
import { isParamsRecord, isWorkspaceTabId } from '@/lib/workspace-tabs'
|
|
26
|
+
|
|
27
|
+
// Which applet a bridge belongs to, supplied by the host at attach time.
|
|
28
|
+
export type AppletIdentity = { kind: AppletKind; name: string }
|
|
29
|
+
|
|
30
|
+
export const appletSource = ({ kind, name }: AppletIdentity): string => `${kind}:${name}`
|
|
31
|
+
|
|
32
|
+
// A chat message fired from applet UI. `source` is stamped from the identity
|
|
33
|
+
// the bridge was attached with, so an applet can neither omit it nor claim to
|
|
34
|
+
// be another applet.
|
|
35
|
+
export type AppletChatMessage = {
|
|
36
|
+
message: string
|
|
37
|
+
source: string
|
|
38
|
+
context?: Record<string, unknown>
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
// Events a workspace runtime emits — already validated, typed for host code.
|
|
42
|
+
export type AppletEvents = {
|
|
43
|
+
// Client-local replace-navigation to a workspace tab. `params` reach the
|
|
44
|
+
// target view as its `params` prop via navigation state — JSON-plain only
|
|
45
|
+
// (history state is structured-cloned).
|
|
46
|
+
focusTab: (tab: WorkspaceTabId, params?: Record<string, unknown>) => void
|
|
47
|
+
// A message for the workspace's active chat, sent as if the user typed
|
|
48
|
+
// `message`. `context` rides the `<moi-context>` envelope, not the bubble.
|
|
49
|
+
sendChatMessage: (message: AppletChatMessage) => void
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// What a bundle's `moi` module calls. Args are `unknown` on purpose: they
|
|
53
|
+
// cross the trust boundary from agent-authored code, and the runtime narrows
|
|
54
|
+
// them before emitting.
|
|
55
|
+
export type AppletBridge = {
|
|
56
|
+
focusTab: (tab: unknown, params?: unknown) => void
|
|
57
|
+
sendChatMessage: (message: unknown, context?: unknown) => void
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
// A message longer than this is a bug, not a chat message — it would land in
|
|
61
|
+
// a bubble verbatim.
|
|
62
|
+
const MAX_MESSAGE_CHARS = 1000
|
|
63
|
+
|
|
64
|
+
// `sendChatMessage` starts an agent run, which makes a stuck applet expensive
|
|
65
|
+
// in a way `focusTab` is not: a widget calling it during render fires once per
|
|
66
|
+
// render, and the bridge is per BUNDLE, so two simultaneous mounts of one
|
|
67
|
+
// applet double every call. The cooldown collapses identical messages (which
|
|
68
|
+
// also absorbs the double-mount); the window cap bounds everything else,
|
|
69
|
+
// including a loop that varies its text. Limits are per workspace runtime, so
|
|
70
|
+
// one runaway applet can't mute another workspace.
|
|
71
|
+
const CHAT_LIMITS = { cooldownMs: 2_000, windowMs: 60_000, maxPerWindow: 10, maxKeys: 64 }
|
|
72
|
+
|
|
73
|
+
function createRuntime(workspaceId: string) {
|
|
74
|
+
const emitter = createNanoEvents<AppletEvents>()
|
|
75
|
+
// Keyed by `${source}\0${text}` — same applet, same message.
|
|
76
|
+
const chatLimiter = createRateLimiter(CHAT_LIMITS)
|
|
77
|
+
|
|
78
|
+
// Drops are journaled, never silent: a message the agent never received has
|
|
79
|
+
// to be discoverable in `moi debug logs`, or the applet author sees a dead
|
|
80
|
+
// button with no explanation. `reportAppletError` has its own per-message
|
|
81
|
+
// cooldown, so journaling a per-frame drop can't become a POST storm.
|
|
82
|
+
const drop = (identity: AppletIdentity, reason: string) => {
|
|
83
|
+
reportAppletError(workspaceId, {
|
|
84
|
+
source: 'runtime',
|
|
85
|
+
kind: identity.kind,
|
|
86
|
+
name: identity.name,
|
|
87
|
+
message: reason
|
|
88
|
+
})
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// True when this message may go out; records the send as a side effect. Each
|
|
92
|
+
// tier gets its own explanation — "you called this in render" and "you are
|
|
93
|
+
// sending too much" need different fixes.
|
|
94
|
+
const admitChatMessage = (identity: AppletIdentity, source: string, text: string): boolean => {
|
|
95
|
+
const verdict = chatLimiter.admit(`${source}\0${text}`)
|
|
96
|
+
if (verdict === 'cooldown') {
|
|
97
|
+
drop(
|
|
98
|
+
identity,
|
|
99
|
+
`sendChatMessage("${text}") was dropped: the same message was already sent less than ${CHAT_LIMITS.cooldownMs / 1000}s ago. Call it from an event handler, not during render.`
|
|
100
|
+
)
|
|
101
|
+
return false
|
|
102
|
+
}
|
|
103
|
+
if (verdict === 'window') {
|
|
104
|
+
drop(
|
|
105
|
+
identity,
|
|
106
|
+
`sendChatMessage("${text}") was dropped: more than ${CHAT_LIMITS.maxPerWindow} messages in a minute from this workspace. Each one starts an agent run, so send only on a real user action.`
|
|
107
|
+
)
|
|
108
|
+
return false
|
|
109
|
+
}
|
|
110
|
+
return true
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
on<K extends keyof AppletEvents>(event: K, cb: AppletEvents[K]) {
|
|
115
|
+
return emitter.on(event, cb)
|
|
116
|
+
},
|
|
117
|
+
// One connection per loaded module instance. The bridge validates every
|
|
118
|
+
// call — a malformed tab id or params shape from applet code drops the
|
|
119
|
+
// call instead of being emitted — and `dispose` flips the connection dead
|
|
120
|
+
// so a disposed module can never act again. Emitting with no subscribers
|
|
121
|
+
// (workspace screen unmounted) is a no-op by nanoevents semantics.
|
|
122
|
+
connect(identity: AppletIdentity) {
|
|
123
|
+
let alive = true
|
|
124
|
+
const source = appletSource(identity)
|
|
125
|
+
const bridge: AppletBridge = {
|
|
126
|
+
focusTab(tab, params) {
|
|
127
|
+
if (!alive) return
|
|
128
|
+
if (!isWorkspaceTabId(tab)) return
|
|
129
|
+
emitter.emit('focusTab', tab, isParamsRecord(params) ? params : undefined)
|
|
130
|
+
},
|
|
131
|
+
sendChatMessage(message, context) {
|
|
132
|
+
if (!alive) return
|
|
133
|
+
if (typeof message !== 'string') return
|
|
134
|
+
const text = message.trim()
|
|
135
|
+
if (!text) return
|
|
136
|
+
if (text.length > MAX_MESSAGE_CHARS) {
|
|
137
|
+
drop(
|
|
138
|
+
identity,
|
|
139
|
+
`sendChatMessage() was dropped: the message is ${text.length} characters (max ${MAX_MESSAGE_CHARS}). Put long content in the context argument, not the message.`
|
|
140
|
+
)
|
|
141
|
+
return
|
|
142
|
+
}
|
|
143
|
+
if (!admitChatMessage(identity, source, text)) return
|
|
144
|
+
emitter.emit('sendChatMessage', {
|
|
145
|
+
message: text,
|
|
146
|
+
source,
|
|
147
|
+
...(context !== undefined
|
|
148
|
+
? { context: narrowChatContext(identity, context, drop) }
|
|
149
|
+
: {})
|
|
150
|
+
})
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
return {
|
|
154
|
+
bridge,
|
|
155
|
+
dispose: () => {
|
|
156
|
+
alive = false
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
// The message carries the user's intent, so a bad `context` drops the payload
|
|
164
|
+
// and keeps the message rather than losing the whole call. Returns undefined
|
|
165
|
+
// for anything that can't ride the envelope: a non-record, something that
|
|
166
|
+
// won't serialize (cycles, functions, BigInt), or an oversized blob.
|
|
167
|
+
function narrowChatContext(
|
|
168
|
+
identity: AppletIdentity,
|
|
169
|
+
context: unknown,
|
|
170
|
+
drop: (identity: AppletIdentity, message: string) => void
|
|
171
|
+
): Record<string, unknown> | undefined {
|
|
172
|
+
if (!isParamsRecord(context)) {
|
|
173
|
+
drop(identity, 'sendChatMessage() ignored its context argument: it must be a plain object.')
|
|
174
|
+
return undefined
|
|
175
|
+
}
|
|
176
|
+
let json: string
|
|
177
|
+
try {
|
|
178
|
+
json = JSON.stringify(context)
|
|
179
|
+
} catch {
|
|
180
|
+
drop(
|
|
181
|
+
identity,
|
|
182
|
+
'sendChatMessage() ignored its context argument: it is not JSON-serializable (cycles, functions, or BigInt).'
|
|
183
|
+
)
|
|
184
|
+
return undefined
|
|
185
|
+
}
|
|
186
|
+
if (json.length > MAX_APPLET_CONTEXT_CHARS) {
|
|
187
|
+
drop(
|
|
188
|
+
identity,
|
|
189
|
+
`sendChatMessage() ignored its context argument: ${json.length} characters serialized (max ${MAX_APPLET_CONTEXT_CHARS}). Send an id the agent can look up instead of the whole payload.`
|
|
190
|
+
)
|
|
191
|
+
return undefined
|
|
192
|
+
}
|
|
193
|
+
return context
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
type AppletRuntime = ReturnType<typeof createRuntime>
|
|
197
|
+
|
|
198
|
+
const runtimes = new Map<string, AppletRuntime>()
|
|
199
|
+
|
|
200
|
+
export function appletRuntime(workspaceId: string): AppletRuntime {
|
|
201
|
+
let runtime = runtimes.get(workspaceId)
|
|
202
|
+
if (!runtime) {
|
|
203
|
+
runtime = createRuntime(workspaceId)
|
|
204
|
+
runtimes.set(workspaceId, runtime)
|
|
205
|
+
}
|
|
206
|
+
return runtime
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
// Subscribe a host feature to one applet event for the lifetime of the
|
|
210
|
+
// component. The subscription is stable across re-renders — the listener reads
|
|
211
|
+
// the latest `handler` through a ref, so an inline arrow doesn't churn the
|
|
212
|
+
// emitter — and StrictMode's effect replay just unbinds and re-binds.
|
|
213
|
+
export function useAppletEvent<K extends keyof AppletEvents>(
|
|
214
|
+
workspaceId: string,
|
|
215
|
+
event: K,
|
|
216
|
+
handler: AppletEvents[K]
|
|
217
|
+
): void {
|
|
218
|
+
const latest = useRef(handler)
|
|
219
|
+
latest.current = handler
|
|
220
|
+
|
|
221
|
+
useEffect(() => {
|
|
222
|
+
// TS can't call a generic indexed function type with its own Parameters
|
|
223
|
+
// tuple, so the forwarder is loosely typed inside and cast at the boundary.
|
|
224
|
+
const forward = ((...args: unknown[]) =>
|
|
225
|
+
(latest.current as (...forwarded: unknown[]) => void)(...args)) as AppletEvents[K]
|
|
226
|
+
return appletRuntime(workspaceId).on(event, forward)
|
|
227
|
+
}, [workspaceId, event])
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
// The shape of the host wiring every bundle entry re-exports (see the entry
|
|
231
|
+
// plugin in server/bundler/build-applet.ts).
|
|
232
|
+
type BridgeModule = {
|
|
233
|
+
__attachBridge?: (bridge: AppletBridge) => void
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
// Live connections keyed by applet cache key (`${segment}/${workspaceId}/${name}`)
|
|
237
|
+
// — the same identity `applet-cache.ts` uses for module lifetime, so disposal
|
|
238
|
+
// rides invalidation.
|
|
239
|
+
const connections = new Map<string, () => void>()
|
|
240
|
+
|
|
241
|
+
// Wire a freshly imported applet module to its workspace runtime. Called from
|
|
242
|
+
// the dynamic-import `.then` in useApplet — the only moment the module
|
|
243
|
+
// namespace is in hand, before React ever renders the component. The identity
|
|
244
|
+
// is the host's, not the applet's: it is what `sendChatMessage` self-attributes
|
|
245
|
+
// with, so it must come from the load path that knows which file this is.
|
|
246
|
+
// Bundles built before the bridge exports existed no-op here until
|
|
247
|
+
// `moi bundle` rebuilds them.
|
|
248
|
+
export function attachAppletBridge(
|
|
249
|
+
mod: unknown,
|
|
250
|
+
workspaceId: string,
|
|
251
|
+
key: string,
|
|
252
|
+
identity: AppletIdentity
|
|
253
|
+
): void {
|
|
254
|
+
const attach = (mod as BridgeModule).__attachBridge
|
|
255
|
+
if (typeof attach !== 'function') return
|
|
256
|
+
// A key is re-attached only after invalidation disposed it, but never leave
|
|
257
|
+
// a live orphan connection behind if that ordering ever changes.
|
|
258
|
+
connections.get(key)?.()
|
|
259
|
+
const { bridge, dispose } = appletRuntime(workspaceId).connect(identity)
|
|
260
|
+
connections.set(key, dispose)
|
|
261
|
+
attach(bridge)
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
// Neuter the bridge of the module instance loaded under `key`. The browser
|
|
265
|
+
// keeps the old module alive forever (ES modules are never unloaded), but its
|
|
266
|
+
// calls now stop at the dead connection.
|
|
267
|
+
export function disposeAppletBridge(key: string): void {
|
|
268
|
+
const dispose = connections.get(key)
|
|
269
|
+
if (!dispose) return
|
|
270
|
+
connections.delete(key)
|
|
271
|
+
dispose()
|
|
272
|
+
}
|
|
@@ -7,16 +7,26 @@ import {
|
|
|
7
7
|
appletUrl,
|
|
8
8
|
getCachedApplet,
|
|
9
9
|
invalidateApplet,
|
|
10
|
+
invalidateAppletSegment,
|
|
10
11
|
setCachedApplet
|
|
11
12
|
} from '@/client/features/applets/applet-cache'
|
|
13
|
+
import { attachAppletBridge } from '@/client/features/applets/applet-runtime'
|
|
12
14
|
import { reportAppletError } from '@/client/features/applets/applet-log'
|
|
13
15
|
import { useWorkspaceId } from '@/client/features/workspace/WorkspaceContext'
|
|
14
16
|
import { type WorkspaceEvent, useWorkspaceEvent } from '@/client/runtime/useWorkspaceEvents'
|
|
15
17
|
import type { AppletKind } from '@/lib/types'
|
|
16
18
|
|
|
19
|
+
// The props the host passes to a mounted applet component. Views receive
|
|
20
|
+
// `params` from navigation state (focusTab / `moi tab focus` → the URL's
|
|
21
|
+
// history entry — see ViewApp in WorkspaceScreen.tsx); widgets are mounted
|
|
22
|
+
// bare, so the applet must render sensibly with `params` absent.
|
|
23
|
+
export type AppletComponentProps = {
|
|
24
|
+
params?: Record<string, unknown>
|
|
25
|
+
}
|
|
26
|
+
|
|
17
27
|
type AppletState =
|
|
18
28
|
| { status: 'loading'; version: number }
|
|
19
|
-
| { status: 'ready'; Component: ComponentType
|
|
29
|
+
| { status: 'ready'; Component: ComponentType<AppletComponentProps>; version: number }
|
|
20
30
|
| { status: 'error'; error: string; version: number }
|
|
21
31
|
|
|
22
32
|
// An applet kind (widget / view) differs only in its URL path segment and which
|
|
@@ -34,22 +44,26 @@ const WIDGET_KIND: AppletKindSpec = {
|
|
|
34
44
|
kind: 'widget',
|
|
35
45
|
segment: 'widgets',
|
|
36
46
|
shouldReload: (e, name) =>
|
|
37
|
-
(e.type === 'widget:updated' && e.name === name) ||
|
|
47
|
+
(e.type === 'widget:updated' && e.name === name) ||
|
|
48
|
+
(e.type === 'applets:refresh' && e.only !== 'views')
|
|
38
49
|
}
|
|
39
50
|
|
|
40
51
|
const VIEW_KIND: AppletKindSpec = {
|
|
41
52
|
kind: 'view',
|
|
42
53
|
segment: 'views',
|
|
43
|
-
shouldReload: (e, name) =>
|
|
54
|
+
shouldReload: (e, name) =>
|
|
55
|
+
(e.type === 'view:updated' && e.name === name) ||
|
|
56
|
+
(e.type === 'applets:refresh' && e.only !== 'widgets')
|
|
44
57
|
}
|
|
45
58
|
|
|
46
59
|
function loadApplet(
|
|
47
|
-
|
|
60
|
+
kind: AppletKindSpec,
|
|
48
61
|
workspaceId: string,
|
|
49
62
|
name: string
|
|
50
|
-
): Promise<ComponentType
|
|
63
|
+
): Promise<ComponentType<AppletComponentProps>> {
|
|
64
|
+
const { segment } = kind
|
|
51
65
|
const key = appletKey(segment, workspaceId, name)
|
|
52
|
-
const existing = getCachedApplet(key) as Promise<ComponentType
|
|
66
|
+
const existing = getCachedApplet(key) as Promise<ComponentType<AppletComponentProps>> | undefined
|
|
53
67
|
if (existing) return existing
|
|
54
68
|
|
|
55
69
|
// Import the bundle dir's `index.js` at its current `?v`; the version is bumped
|
|
@@ -57,7 +71,12 @@ function loadApplet(
|
|
|
57
71
|
// while this applet is unmounted — so a backgrounded tab never serves stale.
|
|
58
72
|
const promise = import(/* @vite-ignore */ appletUrl(segment, workspaceId, name)).then(mod => {
|
|
59
73
|
if (!mod.default) throw new Error(`"${name}" has no default export`)
|
|
60
|
-
|
|
74
|
+
// Wire this module instance's `moi` bridge to the workspace's applet
|
|
75
|
+
// runtime — the only moment the namespace is in hand, before React renders
|
|
76
|
+
// the component. Disposal rides invalidateApplet (same key). The identity
|
|
77
|
+
// passed here is what the applet's chat messages are attributed to.
|
|
78
|
+
attachAppletBridge(mod, workspaceId, key, { kind: kind.kind, name })
|
|
79
|
+
return mod.default as ComponentType<AppletComponentProps>
|
|
61
80
|
})
|
|
62
81
|
|
|
63
82
|
setCachedApplet(key, promise)
|
|
@@ -70,7 +89,7 @@ function useApplet(kind: AppletKindSpec, name: string): AppletState {
|
|
|
70
89
|
|
|
71
90
|
const load = useCallback(() => {
|
|
72
91
|
setState(prev => ({ status: 'loading', version: prev.version }))
|
|
73
|
-
loadApplet(kind
|
|
92
|
+
loadApplet(kind, workspaceId, name)
|
|
74
93
|
.then(Component =>
|
|
75
94
|
setState(prev => ({ status: 'ready', Component, version: prev.version + 1 }))
|
|
76
95
|
)
|
|
@@ -92,8 +111,8 @@ function useApplet(kind: AppletKindSpec, name: string): AppletState {
|
|
|
92
111
|
load()
|
|
93
112
|
}, [load])
|
|
94
113
|
|
|
95
|
-
// Reload the MOUNTED applet the moment the server says its bundle changed
|
|
96
|
-
//
|
|
114
|
+
// Reload the MOUNTED applet the moment the server says its bundle changed or
|
|
115
|
+
// `moi refresh` targets its kind. Invalidate first so we don't re-read a stale
|
|
97
116
|
// cache entry — `useAppletCacheInvalidation` may already have done so for the
|
|
98
117
|
// unmounted case, but doing it here keeps this self-sufficient and
|
|
99
118
|
// order-independent. The bumped version remounts the component and re-runs its
|
|
@@ -126,5 +145,12 @@ export function useAppletCacheInvalidation(): void {
|
|
|
126
145
|
useWorkspaceEvent(event => {
|
|
127
146
|
if (event.type === 'view:updated') invalidateApplet('views', workspaceId, event.name)
|
|
128
147
|
else if (event.type === 'widget:updated') invalidateApplet('widgets', workspaceId, event.name)
|
|
148
|
+
else if (event.type === 'applets:refresh') {
|
|
149
|
+
// Segment-wide: `moi refresh` names no applet, so every cached module in
|
|
150
|
+
// the targeted kind(s) is stale — including ones whose tabs are
|
|
151
|
+
// backgrounded right now.
|
|
152
|
+
if (event.only !== 'views') invalidateAppletSegment('widgets')
|
|
153
|
+
if (event.only !== 'widgets') invalidateAppletSegment('views')
|
|
154
|
+
}
|
|
129
155
|
})
|
|
130
156
|
}
|
|
@@ -211,7 +211,7 @@ function AttachmentChip({ attachment, onRemove }: AttachmentChipProps) {
|
|
|
211
211
|
return (
|
|
212
212
|
<div
|
|
213
213
|
className={cn(
|
|
214
|
-
'group relative flex items-center gap-2 overflow-hidden rounded-
|
|
214
|
+
'group relative flex items-center gap-2 overflow-hidden rounded-lg border border-border bg-accent text-accent-foreground',
|
|
215
215
|
isImage ? 'size-14' : 'h-10 max-w-52 pr-2 pl-2',
|
|
216
216
|
status === 'error' && 'border-destructive/50'
|
|
217
217
|
)}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import {
|
|
2
|
+
IconArticle,
|
|
3
|
+
IconBriefcase,
|
|
4
|
+
IconFileSearch,
|
|
5
|
+
IconGhost,
|
|
6
|
+
IconLayout2,
|
|
7
|
+
IconPiano,
|
|
8
|
+
IconSketching,
|
|
9
|
+
IconUmbrella2,
|
|
10
|
+
type TablerIcon
|
|
11
|
+
} from '@tabler/icons-react'
|
|
12
|
+
|
|
13
|
+
import {
|
|
14
|
+
ChatPromptBubble,
|
|
15
|
+
ChatPromptBubbles,
|
|
16
|
+
type ChatPromptBubble as ChatPrompt
|
|
17
|
+
} from '@/client/features/chat/ChatPromptBubbles'
|
|
18
|
+
import { cn } from '@/client/lib/cn'
|
|
19
|
+
|
|
20
|
+
const ONBOARDING_HANDOFF_DIRECTIVE =
|
|
21
|
+
'Keep the final reply brief and user-facing. Do not include file or storage links, file paths, or bundle, test, and runtime-log summaries.'
|
|
22
|
+
|
|
23
|
+
export const CHAT_WELCOME_PROMPTS = [
|
|
24
|
+
{
|
|
25
|
+
label: "What's the weather?",
|
|
26
|
+
prompt:
|
|
27
|
+
"Build me a set of weather widgets that surface current conditions, today's hourly forecast, and a simple weekly outlook at a glance",
|
|
28
|
+
context: [
|
|
29
|
+
'Build this onboarding example immediately without asking follow-up questions.',
|
|
30
|
+
"Create three separate widgets: current conditions, today's hourly forecast, and a seven-day outlook.",
|
|
31
|
+
'Use one shared Open-Meteo server function with no API key and use Berlin as the default location.',
|
|
32
|
+
'Give each widget an appropriate grid size and a compact, visually distinct layout with loading, error, and last-updated states.',
|
|
33
|
+
'Bundle all three widgets, smoke-test the shared weather function, and check runtime logs before finishing.',
|
|
34
|
+
ONBOARDING_HANDOFF_DIRECTIVE
|
|
35
|
+
],
|
|
36
|
+
icon: IconUmbrella2
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
label: 'Build a fun synthesizer',
|
|
40
|
+
prompt:
|
|
41
|
+
'Build me a view with a simple, playful synthesizer featuring a keyboard, five sound controls, and the ability to record, save, and load music files from the workspace',
|
|
42
|
+
context: [
|
|
43
|
+
"Build this onboarding example immediately without asking follow-up questions, and don't use external services.",
|
|
44
|
+
"Create a responsive View that uses the browser's audio capabilities, with an onscreen piano and computer-keyboard controls.",
|
|
45
|
+
'Include five clearly labeled sound controls for waveform, attack, release, filter cutoff, and volume.',
|
|
46
|
+
'Let the user record timed note events, play and stop recordings, give them names, and save and load them as JSON music files in a workspace music folder.',
|
|
47
|
+
'Include clear empty and error states, then bundle the View and check runtime logs before finishing.',
|
|
48
|
+
ONBOARDING_HANDOFF_DIRECTIVE
|
|
49
|
+
],
|
|
50
|
+
icon: IconPiano
|
|
51
|
+
},
|
|
52
|
+
{
|
|
53
|
+
label: 'Make a job tracker',
|
|
54
|
+
prompt:
|
|
55
|
+
'Build me a view with a visual job search board where I can add opportunities by pasting a job link, automatically extract the details, move opportunities through stages, and keep notes and related files in the workspace',
|
|
56
|
+
context: [
|
|
57
|
+
'Build this onboarding example immediately without asking follow-up questions.',
|
|
58
|
+
'Create a visual View with Saved, Applied, Interviewing, Offer, and Closed stages.',
|
|
59
|
+
'Let the user add a job by pasting its URL, and use a server function to parse public metadata for the title, company, and location.',
|
|
60
|
+
'When a page blocks parsing or lacks metadata, keep the URL and provide editable manual fields.',
|
|
61
|
+
'Support moving opportunities between stages, editing their details and notes, and referencing related workspace files.',
|
|
62
|
+
'Persist the board in a workspace-local JSON file and include loading, empty, and error states.',
|
|
63
|
+
'Bundle the View and smoke-test its persistence and parsing functions before finishing.',
|
|
64
|
+
ONBOARDING_HANDOFF_DIRECTIVE
|
|
65
|
+
],
|
|
66
|
+
icon: IconBriefcase
|
|
67
|
+
}
|
|
68
|
+
] satisfies ChatPrompt[]
|
|
69
|
+
|
|
70
|
+
export const WORKSPACE_ANALYSIS_PROMPT = {
|
|
71
|
+
label: 'Explore the workspace',
|
|
72
|
+
prompt: 'Explore this workspace and suggest what moi can build based on its content',
|
|
73
|
+
context: [
|
|
74
|
+
'Explore the existing workspace files before making suggestions.',
|
|
75
|
+
'Briefly explain what the workspace appears to be for and which content informed your ideas.',
|
|
76
|
+
'Propose a focused set of useful widgets or views that fit the work already here.',
|
|
77
|
+
'Wait for me to choose before building anything.'
|
|
78
|
+
],
|
|
79
|
+
icon: IconFileSearch
|
|
80
|
+
} satisfies ChatPrompt
|
|
81
|
+
|
|
82
|
+
export type ChatEmptyStateKind = 'chat-welcome' | 'workspace-welcome' | 'empty'
|
|
83
|
+
|
|
84
|
+
type ResolveChatEmptyStateOptions = {
|
|
85
|
+
hasSentMessageFromMoi: boolean
|
|
86
|
+
isWorkspacePendingAnalysis: boolean
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export function resolveChatEmptyState({
|
|
90
|
+
hasSentMessageFromMoi,
|
|
91
|
+
isWorkspacePendingAnalysis
|
|
92
|
+
}: ResolveChatEmptyStateOptions): ChatEmptyStateKind {
|
|
93
|
+
if (!hasSentMessageFromMoi) return 'chat-welcome'
|
|
94
|
+
if (isWorkspacePendingAnalysis) return 'workspace-welcome'
|
|
95
|
+
return 'empty'
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
const EMPTY_STATE_STYLES = cn('flex flex-1 flex-col items-center justify-center')
|
|
99
|
+
|
|
100
|
+
type ChatEmptyStateProps = {
|
|
101
|
+
kind: ChatEmptyStateKind
|
|
102
|
+
disabled?: boolean
|
|
103
|
+
onSelectPrompt: (prompt: ChatPrompt) => void
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export function ChatEmptyState({ kind, disabled = false, onSelectPrompt }: ChatEmptyStateProps) {
|
|
107
|
+
if (kind === 'chat-welcome') {
|
|
108
|
+
return <ChatWelcome disabled={disabled} onSelectPrompt={onSelectPrompt} />
|
|
109
|
+
}
|
|
110
|
+
if (kind === 'workspace-welcome') {
|
|
111
|
+
return <ChatWorkspaceWelcome disabled={disabled} onSelectPrompt={onSelectPrompt} />
|
|
112
|
+
}
|
|
113
|
+
return <EmptyState />
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
type WelcomeProps = {
|
|
117
|
+
disabled?: boolean
|
|
118
|
+
onSelectPrompt: (prompt: ChatPrompt) => void
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
export function ChatWelcome({ disabled = false, onSelectPrompt }: WelcomeProps) {
|
|
122
|
+
return (
|
|
123
|
+
<div className={cn(EMPTY_STATE_STYLES, 'max-w-md min-w-0 self-center')}>
|
|
124
|
+
<div className="prose prose-sm min-w-0 wrap-anywhere prose-inherit">
|
|
125
|
+
<p>moi is the visual workspace for you and your agent.</p>
|
|
126
|
+
<p>
|
|
127
|
+
It can grow and adapt to the work you're doing. Just describe what you want, and the agent
|
|
128
|
+
will build small apps in the workspace.
|
|
129
|
+
</p>
|
|
130
|
+
<p>
|
|
131
|
+
You start chatting with <WelcomeTerm Icon={IconGhost}>Agent</WelcomeTerm>, where you can
|
|
132
|
+
ask questions and build anything. <WelcomeTerm Icon={IconLayout2}>Widgets</WelcomeTerm>{' '}
|
|
133
|
+
are small apps that surface information and provide quick actions. For more complex tools,
|
|
134
|
+
you can build <WelcomeTerm Icon={IconArticle}>Views</WelcomeTerm> that open in their own
|
|
135
|
+
tabs. <WelcomeTerm Icon={IconSketching}>Scratchpad</WelcomeTerm> is a shared canvas for
|
|
136
|
+
exploring and shaping ideas with your agent.
|
|
137
|
+
</p>
|
|
138
|
+
<p>Give it a try:</p>
|
|
139
|
+
</div>
|
|
140
|
+
<ChatPromptBubbles
|
|
141
|
+
prompts={CHAT_WELCOME_PROMPTS}
|
|
142
|
+
disabled={disabled}
|
|
143
|
+
onSelect={onSelectPrompt}
|
|
144
|
+
/>
|
|
145
|
+
</div>
|
|
146
|
+
)
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
export function ChatWorkspaceWelcome({ disabled = false, onSelectPrompt }: WelcomeProps) {
|
|
150
|
+
return (
|
|
151
|
+
<div className={cn(EMPTY_STATE_STYLES, 'gap-2')}>
|
|
152
|
+
<div className="prose prose-sm max-w-60 min-w-0 text-center wrap-anywhere text-muted-foreground prose-inherit">
|
|
153
|
+
<p>See what moi can build for you</p>
|
|
154
|
+
</div>
|
|
155
|
+
<ChatPromptBubble
|
|
156
|
+
prompt={WORKSPACE_ANALYSIS_PROMPT}
|
|
157
|
+
disabled={disabled}
|
|
158
|
+
onSelect={onSelectPrompt}
|
|
159
|
+
/>
|
|
160
|
+
</div>
|
|
161
|
+
)
|
|
162
|
+
}
|
|
163
|
+
|
|
164
|
+
function EmptyState() {
|
|
165
|
+
return (
|
|
166
|
+
<div className={cn(EMPTY_STATE_STYLES, 'gap-1 text-center')}>
|
|
167
|
+
<p className="mx-auto max-w-sm text-sm text-muted-foreground">
|
|
168
|
+
Chat with your agent, create widgets and views, and manage your workspace context from here
|
|
169
|
+
</p>
|
|
170
|
+
</div>
|
|
171
|
+
)
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
type WelcomeTermProps = {
|
|
175
|
+
Icon: TablerIcon
|
|
176
|
+
children: string
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function WelcomeTerm({ Icon, children }: WelcomeTermProps) {
|
|
180
|
+
return (
|
|
181
|
+
<strong className="inline-flex items-center gap-0.5 align-bottom font-medium">
|
|
182
|
+
<Icon size={16} stroke={2} aria-hidden />
|
|
183
|
+
{children}
|
|
184
|
+
</strong>
|
|
185
|
+
)
|
|
186
|
+
}
|