moi-computer 0.11.1-next.2 → 0.11.1-next.4

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 (222) hide show
  1. package/README.md +2 -2
  2. package/client/app/AppRouter.tsx +2 -2
  3. package/client/app/routes/WorkspaceRoute.tsx +7 -9
  4. package/client/app/shell/SidebarLayout.tsx +18 -15
  5. package/client/components/shared/BottomPanel.tsx +26 -14
  6. package/client/components/shared/PanelHeader.tsx +1 -1
  7. package/client/components/shared/ReorderableList.tsx +7 -2
  8. package/client/components/shared/WorkspaceIcon.tsx +92 -0
  9. package/client/components/ui/alert-dialog.tsx +1 -0
  10. package/client/components/ui/checkbox.tsx +1 -0
  11. package/client/components/ui/inline-input.tsx +84 -0
  12. package/client/components/ui/toast.tsx +2 -2
  13. package/client/features/applets/AppletMount.tsx +1 -1
  14. package/client/features/applets/applet-cache.ts +3 -3
  15. package/client/features/applets/applet-runtime.ts +147 -108
  16. package/client/features/applets/applet-styles.ts +2 -2
  17. package/client/features/applets/applet-thumbnail.ts +6 -8
  18. package/client/features/applets/useApplet.ts +10 -9
  19. package/client/features/chat/ChatPanel.tsx +19 -10
  20. package/client/features/chat/ChatPopup.tsx +7 -2
  21. package/client/features/chat/applet-chat-intents.ts +170 -0
  22. package/client/features/chat/chat-send.ts +103 -62
  23. package/client/features/chat/chat-store.ts +50 -59
  24. package/client/features/chat/composer/ChatComposer.tsx +43 -132
  25. package/client/features/chat/composer/ModelPicker.tsx +3 -16
  26. package/client/features/chat/composer/attachments/AttachmentChip.tsx +99 -0
  27. package/client/features/chat/composer/attachments/DrawingAttachmentChip.tsx +37 -0
  28. package/client/features/chat/composer/attachments/FileAttachmentChip.tsx +22 -0
  29. package/client/features/chat/composer/attachments/TextAttachmentChip.tsx +11 -0
  30. package/client/features/chat/composer/attachments/draft-attachments.ts +168 -0
  31. package/client/features/chat/composer/attachments/types.ts +25 -0
  32. package/client/features/chat/composer/attachments/uploads.ts +26 -0
  33. package/client/features/chat/{chat-connection.ts → connection/chat-connection.ts} +1 -1
  34. package/client/features/chat/{chat-frames.ts → connection/chat-frames.ts} +2 -2
  35. package/client/features/chat/{ChatEmptyState.tsx → messages/ChatEmptyState.tsx} +16 -11
  36. package/client/features/chat/{MarkdownContent.tsx → messages/MarkdownContent.tsx} +16 -1
  37. package/client/features/chat/{TurnView.tsx → messages/TurnView.tsx} +22 -15
  38. package/client/features/chat/{tool-group → messages/tool-group}/SubagentCard.tsx +1 -1
  39. package/client/features/chat/{tool-group → messages/tool-group}/TimelineRow.tsx +2 -2
  40. package/client/features/chat/{tool-group → messages/tool-group}/ToolCallGroup.tsx +1 -1
  41. package/client/features/chat/{ChatSelector.tsx → sessions/ChatSelector.tsx} +3 -3
  42. package/client/features/chat/{api.ts → sessions/api.ts} +1 -1
  43. package/client/features/chat/useChat.ts +32 -15
  44. package/client/features/dev/ChatStatesPage.tsx +5 -6
  45. package/client/features/dev/HarnessDebugPage.tsx +10 -7
  46. package/client/features/dev/ToolCallsPage.tsx +1 -1
  47. package/client/features/dev/UiComponentPreviews.tsx +21 -0
  48. package/client/features/dev/chat-states-fixtures.ts +13 -2
  49. package/client/features/drawings/useChatAnnotation.ts +17 -17
  50. package/client/features/drawings/useDrawingLayer.ts +7 -9
  51. package/client/features/drawings/useViewBuilderSketch.ts +18 -15
  52. package/client/features/home/HomePage.tsx +8 -7
  53. package/client/features/home/WorkspacePreview.tsx +1 -1
  54. package/client/features/home/workspace-presentation.tsx +1 -11
  55. package/client/features/home/workspace-setup/WorkspaceAgentStep.tsx +2 -4
  56. package/client/features/overview/HiddenPanel.tsx +109 -0
  57. package/client/features/overview/Overview.tsx +302 -0
  58. package/client/features/overview/WidgetCanvas.tsx +164 -0
  59. package/client/features/{widgets → overview}/WidgetFrame.tsx +24 -16
  60. package/client/features/overview/WorkspaceName.tsx +60 -0
  61. package/client/features/overview/api.ts +30 -0
  62. package/client/features/overview/default/ViewsWidget.tsx +252 -0
  63. package/client/features/overview/default/registry.tsx +36 -0
  64. package/client/features/{widgets → overview}/grid.ts +36 -0
  65. package/client/features/overview/useGridReconcile.ts +94 -0
  66. package/client/features/scratchpad/Scratchpad.tsx +7 -8
  67. package/client/features/scratchpad/ScratchpadControls.tsx +0 -1
  68. package/client/features/scratchpad/useScratchpadSnapshot.tsx +23 -13
  69. package/client/features/settings/GeneralSettings.tsx +55 -58
  70. package/client/features/settings/SettingsLayout.tsx +2 -2
  71. package/client/features/settings/WorkspaceIconPicker.tsx +318 -0
  72. package/client/features/settings/WorkspaceSettingsDialog.tsx +65 -0
  73. package/client/features/settings/api.ts +48 -24
  74. package/client/features/views/ViewBuilder.tsx +5 -4
  75. package/client/features/views/ViewManager.tsx +11 -13
  76. package/client/features/views/api.ts +36 -2
  77. package/client/features/views/useViewBuilderActions.ts +8 -5
  78. package/client/features/views/useViewBuilderDrafts.ts +23 -21
  79. package/client/features/views/view-presentation.ts +13 -0
  80. package/client/features/workspace/{CustomizePanel.tsx → ThemePanel.tsx} +26 -31
  81. package/client/features/workspace/UnavailablePage.tsx +21 -0
  82. package/client/features/workspace/WorkspaceLayoutContext.tsx +4 -7
  83. package/client/features/workspace/WorkspaceScreen.tsx +205 -161
  84. package/client/features/workspace/WorkspaceSplitLayout.tsx +1 -1
  85. package/client/features/workspace/WorkspaceTabs.tsx +2 -0
  86. package/client/features/workspace/api.ts +0 -26
  87. package/client/features/workspace/moi-context.ts +8 -9
  88. package/client/features/workspace/tab-resolution.ts +10 -33
  89. package/client/features/workspace/useWorkspaceNavigation.ts +164 -101
  90. package/client/index.css +1 -1
  91. package/client/lib/app-icon-registry.ts +2 -0
  92. package/client/lib/use-latest-ref.ts +11 -0
  93. package/client/main.tsx +1 -1
  94. package/client/runtime/useWorkspaceEvents.ts +59 -14
  95. package/client/theme.css +5 -5
  96. package/dist/chunk-2gs8jjx4.css +1 -0
  97. package/dist/chunk-7vvg5fyz.js +68 -0
  98. package/dist/chunk-94m3q9ce.js +1 -0
  99. package/dist/{chunk-w482ew2c.js → chunk-brr0zbeh.js} +73 -73
  100. package/dist/chunk-by2wqgak.js +1 -0
  101. package/dist/chunk-m1fvqssh.js +2 -0
  102. package/dist/chunk-p34hvqt3.js +1 -0
  103. package/dist/{chunk-4s15axjs.js → chunk-p83f0jnj.js} +2 -2
  104. package/dist/{chunk-j3wvxjr7.js → chunk-tzz101ea.js} +1 -1
  105. package/dist/chunk-xg01f6pn.js +10 -0
  106. package/dist/{chunk-53a7pxcz.js → chunk-zs4mte79.js} +1 -1
  107. package/dist/index.html +1 -1
  108. package/lib/app-icons.ts +1 -0
  109. package/lib/applet-selector.ts +24 -0
  110. package/lib/chat-title.ts +6 -1
  111. package/lib/default-widgets.ts +32 -0
  112. package/lib/format.ts +12 -1
  113. package/lib/message-attachments.ts +40 -0
  114. package/lib/moi-attachments.ts +204 -0
  115. package/lib/moi-context.ts +20 -35
  116. package/lib/navigation.ts +84 -0
  117. package/lib/types.ts +81 -25
  118. package/lib/workspace-icon.ts +20 -0
  119. package/lib/workspace-layout.ts +22 -2
  120. package/lib/workspace-tabs.ts +9 -33
  121. package/package.json +27 -27
  122. package/registry.json +13 -0
  123. package/server/AGENTS.md +1 -0
  124. package/server/api.ts +114 -37
  125. package/server/applet-typecheck.ts +95 -0
  126. package/server/{bundler → applets}/build-applet.ts +13 -246
  127. package/server/applets/config.ts +226 -0
  128. package/server/applets/dependencies.ts +206 -0
  129. package/server/{applets.ts → applets/index.ts} +15 -161
  130. package/server/applets/runtime/base.ts +4 -0
  131. package/server/applets/runtime/moi.ts +42 -0
  132. package/server/applets/runtime/rpc.ts +17 -0
  133. package/server/applets/view-source.ts +67 -0
  134. package/server/attachment-message.ts +95 -0
  135. package/server/cli.ts +163 -97
  136. package/server/control.ts +63 -42
  137. package/server/harness/README.md +4 -2
  138. package/server/harness/acp/adapter.ts +7 -21
  139. package/server/harness/acp/session.ts +31 -50
  140. package/server/harness/claude-code/NOTES.md +8 -2
  141. package/server/harness/claude-code/adapter.ts +8 -25
  142. package/server/harness/claude-code/index.ts +2 -0
  143. package/server/harness/claude-code/session.ts +42 -47
  144. package/server/harness/claude-code/sessions.ts +9 -4
  145. package/server/harness/codex/adapter.ts +8 -7
  146. package/server/harness/codex/index.ts +1 -0
  147. package/server/harness/codex/permissions.ts +1 -1
  148. package/server/harness/codex/session.ts +20 -47
  149. package/server/harness/hermes/NOTES.md +1 -1
  150. package/server/harness/hermes/index.ts +4 -0
  151. package/server/harness/openclaw/NOTES.md +1 -1
  152. package/server/harness/openclaw/adapter.ts +22 -4
  153. package/server/harness/openclaw/gateway.ts +5 -3
  154. package/server/harness/openclaw/session.ts +27 -27
  155. package/server/harness/types.ts +5 -3
  156. package/server/layout.ts +9 -14
  157. package/server/moi-scaffold.ts +21 -13
  158. package/server/navigation-relay.ts +110 -0
  159. package/server/registry.ts +8 -0
  160. package/server/selected-session.ts +24 -4
  161. package/server/skill-update.ts +5 -3
  162. package/server/skill-version.ts +4 -7
  163. package/server/skills-template.ts +48 -9
  164. package/server/tabs.ts +19 -33
  165. package/server/uploads.ts +0 -0
  166. package/server/view-builders.ts +18 -1
  167. package/server/views.ts +126 -6
  168. package/server/web.ts +35 -9
  169. package/server/widgets.ts +12 -5
  170. package/server/workspace-config.ts +13 -9
  171. package/ui-components/button.tsx +2 -2
  172. package/ui-components/docs/kbd.md +191 -0
  173. package/ui-components/dropdown-menu.tsx +1 -1
  174. package/ui-components/kbd.tsx +32 -0
  175. package/ui-components/slider.tsx +1 -1
  176. package/ui-components/switch.tsx +1 -1
  177. package/ui-components/tabs.tsx +1 -1
  178. package/workspace/.claude/skills/moi-workspace/SKILL.md +284 -228
  179. package/workspace/.claude/skills/moi-workspace/references/DESIGN.md +100 -153
  180. package/workspace/.claude/skills/moi-workspace/references/INTENTS.md +156 -0
  181. package/client/features/chat/attachment-staging.ts +0 -153
  182. package/client/features/chat/uploads.ts +0 -18
  183. package/client/features/chat/useAppletChatMessage.ts +0 -68
  184. package/client/features/settings/IconPicker.tsx +0 -442
  185. package/client/features/settings/WorkspaceSettings.tsx +0 -95
  186. package/client/features/settings/render-icon.ts +0 -173
  187. package/client/features/widgets/HiddenPanel.tsx +0 -80
  188. package/client/features/widgets/WidgetGrid.tsx +0 -123
  189. package/client/features/widgets/Widgets.tsx +0 -262
  190. package/client/features/widgets/useGridReconcile.ts +0 -73
  191. package/dist/chunk-245wt82v.css +0 -1
  192. package/dist/chunk-87thzbtw.js +0 -2
  193. package/dist/chunk-a6jad1a6.js +0 -1
  194. package/dist/chunk-dghwstd2.js +0 -9
  195. package/dist/chunk-eg120aca.js +0 -1
  196. package/dist/chunk-j7hh2ab4.js +0 -70
  197. package/dist/chunk-y8eyahwx.js +0 -1
  198. /package/client/features/chat/{ChatPromptBubbles.tsx → messages/ChatPromptBubbles.tsx} +0 -0
  199. /package/client/features/chat/{ShimmerText.tsx → messages/ShimmerText.tsx} +0 -0
  200. /package/client/features/chat/{group-turns.ts → messages/group-turns.ts} +0 -0
  201. /package/client/features/chat/{interleave-notices.ts → messages/interleave-notices.ts} +0 -0
  202. /package/client/features/chat/{preview-turn.ts → messages/preview-turn.ts} +0 -0
  203. /package/client/features/chat/{tool-group → messages/tool-group}/CodeBlock.tsx +0 -0
  204. /package/client/features/chat/{tool-group → messages/tool-group}/Collapse.tsx +0 -0
  205. /package/client/features/chat/{tool-group → messages/tool-group}/ReadImagePreview.tsx +0 -0
  206. /package/client/features/chat/{tool-group → messages/tool-group}/ToolOutput.tsx +0 -0
  207. /package/client/features/chat/{tool-group → messages/tool-group}/detect.ts +0 -0
  208. /package/client/features/chat/{tool-group → messages/tool-group}/fixtures.ts +0 -0
  209. /package/client/features/chat/{tool-group → messages/tool-group}/format/claude.ts +0 -0
  210. /package/client/features/chat/{tool-group → messages/tool-group}/format/codex.ts +0 -0
  211. /package/client/features/chat/{tool-group → messages/tool-group}/format/hermes.ts +0 -0
  212. /package/client/features/chat/{tool-group → messages/tool-group}/format/index.ts +0 -0
  213. /package/client/features/chat/{tool-group → messages/tool-group}/format/mcp.ts +0 -0
  214. /package/client/features/chat/{tool-group → messages/tool-group}/format/openclaw.ts +0 -0
  215. /package/client/features/chat/{tool-group → messages/tool-group}/format/shared.ts +0 -0
  216. /package/client/features/chat/{tool-group → messages/tool-group}/format/shell.ts +0 -0
  217. /package/client/features/chat/{tool-group → messages/tool-group}/index.ts +0 -0
  218. /package/client/features/chat/{useStickToBottom.ts → messages/useStickToBottom.ts} +0 -0
  219. /package/client/features/chat/{session-view.ts → sessions/session-view.ts} +0 -0
  220. /package/client/features/chat/{useSelectedSession.ts → sessions/useSelectedSession.ts} +0 -0
  221. /package/server/{bundler → applets}/applet-css.ts +0 -0
  222. /package/server/{bundler → applets}/build-worker.ts +0 -0
@@ -1,28 +1,38 @@
1
1
  // The workspace applet runtime — the host side of the applet `moi` module.
2
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
3
+ // Every applet bundle inlines its own copy of the `moi` runtime module (see
4
+ // server/applets/runtime/moi.ts), so each loaded module instance holds a
5
+ // private `bridge` slot. Right after the dynamic import, the host connects that
6
+ // instance to the workspace's runtime by attaching a thin
7
7
  // bridge (`attachAppletBridge`); invalidation disposes it (`disposeAppletBridge`),
8
8
  // leaving a stale module instance — old timers, old listeners — inert instead
9
9
  // of steering the app. One runtime per workspace id.
10
10
  //
11
11
  // Applet calls surface as runtime EVENTS: the bridge validates the untrusted
12
12
  // args, then emits, and each host feature subscribes to its own concern with
13
- // `useAppletEvent` (navigation owns `focusTab`; chat will own `sendChatMessage`)
13
+ // `useAppletEvent` (navigation owns `navigate`; chat will own `sendChatMessage`)
14
14
  // — no central handlers object assembled by the screen. Applet → host only;
15
15
  // if a host → applet direction is ever added (`moi.on(...)`), `dispose` must
16
16
  // also unbind those listeners or a disposed module leaks.
17
- import { useEffect, useRef } from 'react'
17
+ import {
18
+ MAX_ATTACHMENT_LABEL_CHARS,
19
+ MAX_TEXT_ATTACHMENT_CHARS,
20
+ snapshotTextAttachment
21
+ } from '@/lib/moi-attachments'
22
+ import type { AppletBridge, AppletKind, AttachmentInput, AttachmentOrigin } from '@/lib/types'
23
+ import { isWorkspaceAttachmentPath, MAX_UPLOAD_BYTES } from '@/lib/message-attachments'
24
+ import { useEffect } from 'react'
18
25
 
19
26
  import { createNanoEvents } from 'nanoevents'
20
27
 
21
28
  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'
29
+ import { toast } from '@/client/components/ui/toast'
30
+ import { createRateLimiter, type RateLimiter } from '@/client/lib/rate-limit'
31
+ import { useLatestRef } from '@/client/lib/use-latest-ref'
32
+ import { isParamsRecord } from '@/lib/workspace-tabs'
33
+ import { resolveWorkspaceHref } from '@/lib/navigation'
34
+
35
+ export type { AppletBridge } from '@/lib/types'
26
36
 
27
37
  // Which applet a bridge belongs to, supplied by the host at attach time.
28
38
  export type AppletIdentity = { kind: AppletKind; name: string }
@@ -35,39 +45,24 @@ export const appletSource = ({ kind, name }: AppletIdentity): string => `${kind}
35
45
  export type AppletChatMessage = {
36
46
  message: string
37
47
  source: string
38
- context?: Record<string, unknown>
48
+ attachments: (AttachmentInput & AttachmentOrigin)[]
39
49
  }
40
50
 
41
51
  // Events a workspace runtime emits — already validated, typed for host code.
42
52
  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
53
+ addChatAttachment: (attachment: AttachmentInput & AttachmentOrigin) => void
54
+ navigate: (href: string) => void
47
55
  // 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.
56
+ // `message`, with attachments prepared before the send.
49
57
  sendChatMessage: (message: AppletChatMessage) => void
50
58
  }
51
59
 
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
60
  // A message longer than this is a bug, not a chat message — it would land in
61
61
  // a bubble verbatim.
62
62
  const MAX_MESSAGE_CHARS = 1000
63
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.
64
+ // Chat messages start an agent run. The cooldown collapses identical calls and
65
+ // the window cap bounds loops that vary their payload.
71
66
  const CHAT_LIMITS = { cooldownMs: 2_000, windowMs: 60_000, maxPerWindow: 10, maxKeys: 64 }
72
67
 
73
68
  function createRuntime(workspaceId: string) {
@@ -88,26 +83,27 @@ function createRuntime(workspaceId: string) {
88
83
  })
89
84
  }
90
85
 
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.
86
+ const admit = (
87
+ limiter: RateLimiter,
88
+ identity: AppletIdentity,
89
+ key: string,
90
+ cooldownReason: string,
91
+ windowReason: string
92
+ ): boolean => {
93
+ const verdict = limiter.admit(key)
94
+ if (verdict === 'ok') return true
95
+ drop(identity, verdict === 'cooldown' ? cooldownReason : windowReason)
96
+ return false
97
+ }
98
+
94
99
  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
100
+ return admit(
101
+ chatLimiter,
102
+ identity,
103
+ `${source}\0${text}`,
104
+ `sendChatMessage() for "${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.`,
105
+ `sendChatMessage() for "${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.`
106
+ )
111
107
  }
112
108
 
113
109
  return {
@@ -115,39 +111,77 @@ function createRuntime(workspaceId: string) {
115
111
  return emitter.on(event, cb)
116
112
  },
117
113
  // One connection per loaded module instance. The bridge validates every
118
- // call — a malformed tab id or params shape from applet code drops the
114
+ // call — a malformed address or chat input from applet code drops the
119
115
  // call instead of being emitted — and `dispose` flips the connection dead
120
116
  // so a disposed module can never act again. Emitting with no subscribers
121
117
  // (workspace screen unmounted) is a no-op by nanoevents semantics.
122
- connect(identity: AppletIdentity) {
118
+ connect(identity: AppletIdentity, base = '') {
123
119
  let alive = true
124
120
  const source = appletSource(identity)
125
121
  const bridge: AppletBridge = {
126
- focusTab(tab, params) {
122
+ addChatAttachment(input) {
127
123
  if (!alive) return
128
- if (!isWorkspaceTabId(tab)) return
129
- emitter.emit('focusTab', tab, isParamsRecord(params) ? params : undefined)
124
+ try {
125
+ const attachment = snapshotAttachmentInput(input, source)
126
+ emitter.emit('addChatAttachment', attachment)
127
+ } catch (error) {
128
+ const message = errorMessage(error)
129
+ toast.add({ title: 'Couldn’t add attachment', description: message, type: 'error' })
130
+ drop(identity, `addChatAttachment() was dropped: ${message}`)
131
+ }
132
+ },
133
+ navigate(href) {
134
+ if (!alive) return
135
+ try {
136
+ if (typeof href !== 'string') throw new Error('Navigation needs a URL')
137
+ resolveWorkspaceHref(workspaceId, href, base)
138
+ emitter.emit('navigate', href)
139
+ } catch (error) {
140
+ drop(identity, `navigate() was dropped: ${errorMessage(error)}`)
141
+ }
130
142
  },
131
- sendChatMessage(message, context) {
143
+ resolveHref(href) {
144
+ if (!alive) return ''
145
+ if (typeof href !== 'string') throw new Error('A URL is required')
146
+ return resolveWorkspaceHref(workspaceId, href, base)
147
+ },
148
+ sendChatMessage(input, legacyContext) {
132
149
  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.`
150
+ try {
151
+ // Runtime compatibility for older bundles. Both forms use the same
152
+ // attachment validation and send path; only the object API is public.
153
+ if (typeof input === 'string') {
154
+ input = {
155
+ message: input,
156
+ attachments:
157
+ legacyContext === undefined
158
+ ? []
159
+ : [{ type: 'text', label: 'Context', text: JSON.stringify(legacyContext) }]
160
+ }
161
+ }
162
+ if (!isParamsRecord(input) || typeof input.message !== 'string') {
163
+ throw new Error('This message uses an unsupported format')
164
+ }
165
+ if ('context' in input) {
166
+ throw new Error('This message uses an outdated format')
167
+ }
168
+ const message = input.message.trim()
169
+ if (!message) throw new Error('The message is empty')
170
+ if (message.length > MAX_MESSAGE_CHARS)
171
+ throw new Error('Messages can be up to 1,000 characters')
172
+ if (input.attachments !== undefined && !Array.isArray(input.attachments)) {
173
+ throw new Error('The attachments use an unsupported format')
174
+ }
175
+ const attachments = Array.from(input.attachments ?? [], item =>
176
+ snapshotAttachmentInput(item, source)
140
177
  )
141
- return
178
+ if (!admitChatMessage(identity, source, message)) return
179
+ emitter.emit('sendChatMessage', { message, source, attachments })
180
+ } catch (error) {
181
+ const message = errorMessage(error)
182
+ toast.add({ title: 'Couldn’t send message', description: message, type: 'error' })
183
+ drop(identity, `sendChatMessage() was dropped: ${message}`)
142
184
  }
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
185
  }
152
186
  }
153
187
  return {
@@ -160,37 +194,42 @@ function createRuntime(workspaceId: string) {
160
194
  }
161
195
  }
162
196
 
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
197
+ // Copy the caller's fields now so later applet mutations cannot change a send
198
+ // or staged attachment. File contents are immutable and can be retained as-is.
199
+ function snapshotAttachmentInput(
200
+ input: unknown,
201
+ source: string
202
+ ): AttachmentInput & AttachmentOrigin {
203
+ if (!isParamsRecord(input)) throw new Error('This attachment isn’t supported')
204
+ if (input.type === 'text') {
205
+ const snapshot = snapshotTextAttachment(input)
206
+ if (snapshot) return { type: 'text', ...snapshot, source }
207
+ if (typeof input.label !== 'string' || !input.label.trim())
208
+ throw new Error('Add a label to this text attachment')
209
+ if (input.label.trim().length > MAX_ATTACHMENT_LABEL_CHARS)
210
+ throw new Error(`Attachment labels can be up to ${MAX_ATTACHMENT_LABEL_CHARS} characters`)
211
+ if (typeof input.text !== 'string' || !input.text.trim())
212
+ throw new Error('Add some text to this attachment')
213
+ if (input.text.length > MAX_TEXT_ATTACHMENT_CHARS)
214
+ throw new Error(
215
+ `Text attachments can be up to ${MAX_TEXT_ATTACHMENT_CHARS.toLocaleString('en-US')} characters`
216
+ )
217
+ } else if (input.type === 'file') {
218
+ if (input.file instanceof File && input.path === undefined) {
219
+ if (input.file.size > MAX_UPLOAD_BYTES) throw new Error('Files can be up to 32 MB')
220
+ return { type: 'file', file: input.file, source }
221
+ }
222
+ if (typeof input.path === 'string' && input.file === undefined) {
223
+ if (!isWorkspaceAttachmentPath(input.path))
224
+ throw new Error('Choose a file from this workspace')
225
+ return { type: 'file', path: input.path, source }
226
+ }
192
227
  }
193
- return context
228
+ throw new Error('This attachment isn’t supported')
229
+ }
230
+
231
+ function errorMessage(error: unknown): string {
232
+ return error instanceof Error ? error.message : String(error)
194
233
  }
195
234
 
196
235
  type AppletRuntime = ReturnType<typeof createRuntime>
@@ -215,8 +254,7 @@ export function useAppletEvent<K extends keyof AppletEvents>(
215
254
  event: K,
216
255
  handler: AppletEvents[K]
217
256
  ): void {
218
- const latest = useRef(handler)
219
- latest.current = handler
257
+ const latest = useLatestRef(handler)
220
258
 
221
259
  useEffect(() => {
222
260
  // TS can't call a generic indexed function type with its own Parameters
@@ -224,11 +262,11 @@ export function useAppletEvent<K extends keyof AppletEvents>(
224
262
  const forward = ((...args: unknown[]) =>
225
263
  (latest.current as (...forwarded: unknown[]) => void)(...args)) as AppletEvents[K]
226
264
  return appletRuntime(workspaceId).on(event, forward)
227
- }, [workspaceId, event])
265
+ }, [workspaceId, event, latest])
228
266
  }
229
267
 
230
268
  // The shape of the host wiring every bundle entry re-exports (see the entry
231
- // plugin in server/bundler/build-applet.ts).
269
+ // plugin in server/applets/build-applet.ts).
232
270
  type BridgeModule = {
233
271
  __attachBridge?: (bridge: AppletBridge) => void
234
272
  }
@@ -249,14 +287,15 @@ export function attachAppletBridge(
249
287
  mod: unknown,
250
288
  workspaceId: string,
251
289
  key: string,
252
- identity: AppletIdentity
290
+ identity: AppletIdentity,
291
+ base = ''
253
292
  ): void {
254
293
  const attach = (mod as BridgeModule).__attachBridge
255
294
  if (typeof attach !== 'function') return
256
295
  // A key is re-attached only after invalidation disposed it, but never leave
257
296
  // a live orphan connection behind if that ordering ever changes.
258
297
  connections.get(key)?.()
259
- const { bridge, dispose } = appletRuntime(workspaceId).connect(identity)
298
+ const { bridge, dispose } = appletRuntime(workspaceId).connect(identity, base)
260
299
  connections.set(key, dispose)
261
300
  attach(bridge)
262
301
  }
@@ -5,7 +5,7 @@ import { useInsertionEffect } from 'react'
5
5
  // re-evaluated, let alone disposed). Instead its module side effect registers
6
6
  // the compiled, container-scoped CSS on `window.__moiAppletCss`, keyed by the
7
7
  // bundle-dir path (`/api/workspaces/<id>/<segment>/<name>` — see injectCss in
8
- // server/bundler/build-applet.ts and `appletStyleKey`). The host injects a <style> for
8
+ // server/applets/build-applet.ts and `appletStyleKey`). The host injects a <style> for
9
9
  // that key while at least one instance of the applet is mounted, and removes it
10
10
  // when the last one unmounts.
11
11
  declare global {
@@ -55,7 +55,7 @@ export function acquireAppletStyle(key: string): () => void {
55
55
  // Move an applet's <style> to the end of <head>. Everything in an applet sheet
56
56
  // is scoped to its `[data-applet]` container and so can't collide — except the
57
57
  // names of global at-rules (`@keyframes`, `@property`, `@font-face`), which the
58
- // scoper leaves alone because they aren't selectors (server/bundler/applet-css.ts).
58
+ // scoper leaves alone because they aren't selectors (server/applets/applet-css.ts).
59
59
  // Two applets defining `@keyframes pulse` differently therefore share one
60
60
  // document-global name, and the last definition wins for both. Raising the
61
61
  // applet the user is looking at makes the winner the one on screen.
@@ -1,7 +1,8 @@
1
- import { useEffect, useRef } from 'react'
1
+ import { useEffect } from 'react'
2
2
 
3
3
  import { useAppletThumbnailRecords, useSaveAppletThumbnails } from '@/client/features/applets/api'
4
4
  import { useWorkspaceId } from '@/client/features/workspace/WorkspaceContext'
5
+ import { useLatestRef } from '@/client/lib/use-latest-ref'
5
6
  import type { AppletKind, AppletThumbnail, AppletThumbnailUpdate } from '@/lib/types'
6
7
 
7
8
  export const THUMBNAIL_MAX_EDGE = 1_000
@@ -239,12 +240,9 @@ export function useAppletThumbnails({ kind, enabled, targets }: UseAppletThumbna
239
240
  const workspaceId = useWorkspaceId()
240
241
  const records = useAppletThumbnailRecords(workspaceId).data
241
242
  const save = useSaveAppletThumbnails(workspaceId)
242
- const targetsRef = useRef(targets)
243
- targetsRef.current = targets
244
- const recordsRef = useRef(records ?? [])
245
- recordsRef.current = records ?? []
246
- const saveRef = useRef(save.mutate)
247
- saveRef.current = save.mutate
243
+ const targetsRef = useLatestRef(targets)
244
+ const recordsRef = useLatestRef(records ?? [])
245
+ const saveRef = useLatestRef(save.mutate)
248
246
  const targetKey = targets.map(target => `${target.id}@${target.revision ?? ''}`).join('\n')
249
247
  // Hold the first pass until the stored records arrive — capturing against an
250
248
  // unloaded store would re-screenshot everything on every page load.
@@ -291,5 +289,5 @@ export function useAppletThumbnails({ kind, enabled, targets }: UseAppletThumbna
291
289
  if (timer !== undefined) clearTimeout(timer)
292
290
  document.removeEventListener('visibilitychange', schedule)
293
291
  }
294
- }, [enabled, kind, ready, targetKey])
292
+ }, [enabled, kind, ready, recordsRef, saveRef, targetKey, targetsRef])
295
293
  }
@@ -1,4 +1,5 @@
1
1
  import type { ComponentType } from 'react'
2
+ import { useRouter } from 'wouter'
2
3
  import { useCallback, useEffect, useState } from 'react'
3
4
 
4
5
  import {
@@ -16,12 +17,10 @@ import { useWorkspaceId } from '@/client/features/workspace/WorkspaceContext'
16
17
  import { type WorkspaceEvent, useWorkspaceEvent } from '@/client/runtime/useWorkspaceEvents'
17
18
  import type { AppletKind } from '@/lib/types'
18
19
 
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.
20
+ // Views receive URL query params. Widgets mount without params; applets must
21
+ // render sensibly with missing fields.
23
22
  export type AppletComponentProps = {
24
- params?: Record<string, unknown>
23
+ params?: Record<string, string>
25
24
  }
26
25
 
27
26
  export type AppletState =
@@ -59,7 +58,8 @@ const VIEW_KIND: AppletKindSpec = {
59
58
  function loadApplet(
60
59
  kind: AppletKindSpec,
61
60
  workspaceId: string,
62
- name: string
61
+ name: string,
62
+ base: string
63
63
  ): Promise<ComponentType<AppletComponentProps>> {
64
64
  const { segment } = kind
65
65
  const key = appletKey(segment, workspaceId, name)
@@ -75,7 +75,7 @@ function loadApplet(
75
75
  // runtime — the only moment the namespace is in hand, before React renders
76
76
  // the component. Disposal rides invalidateApplet (same key). The identity
77
77
  // passed here is what the applet's chat messages are attributed to.
78
- attachAppletBridge(mod, workspaceId, key, { kind: kind.kind, name })
78
+ attachAppletBridge(mod, workspaceId, key, { kind: kind.kind, name }, base)
79
79
  return mod.default as ComponentType<AppletComponentProps>
80
80
  })
81
81
 
@@ -85,11 +85,12 @@ function loadApplet(
85
85
 
86
86
  function useApplet(kind: AppletKindSpec, name: string): AppletState {
87
87
  const workspaceId = useWorkspaceId()
88
+ const { base } = useRouter()
88
89
  const [state, setState] = useState<AppletState>({ status: 'loading', version: 0 })
89
90
 
90
91
  const load = useCallback(() => {
91
92
  setState(prev => ({ status: 'loading', version: prev.version }))
92
- loadApplet(kind, workspaceId, name)
93
+ loadApplet(kind, workspaceId, name, base)
93
94
  .then(Component =>
94
95
  setState(prev => ({ status: 'ready', Component, version: prev.version + 1 }))
95
96
  )
@@ -105,7 +106,7 @@ function useApplet(kind: AppletKindSpec, name: string): AppletState {
105
106
  })
106
107
  setState(prev => ({ status: 'error', error: String(err), version: prev.version + 1 }))
107
108
  })
108
- }, [kind, workspaceId, name])
109
+ }, [base, kind, workspaceId, name])
109
110
 
110
111
  useEffect(() => {
111
112
  load()
@@ -4,20 +4,27 @@ import { IconChevronDown, IconChevronsRight, IconX } from '@tabler/icons-react'
4
4
 
5
5
  import { canSubmitComposerAction, focusComposer } from '@/client/components/shared/Composer'
6
6
  import { AgentBlobatar } from '@/client/components/shared/AgentBlobatar'
7
- import { useStickToBottom } from '@/client/features/chat/useStickToBottom'
8
- import { appendPreviewTurn, groupTurns } from '@/client/features/chat/group-turns'
9
- import { chatNoticeLabel, interleaveNotices } from '@/client/features/chat/interleave-notices'
7
+ import { useStickToBottom } from '@/client/features/chat/messages/useStickToBottom'
8
+ import { appendPreviewTurn, groupTurns } from '@/client/features/chat/messages/group-turns'
9
+ import {
10
+ chatNoticeLabel,
11
+ interleaveNotices
12
+ } from '@/client/features/chat/messages/interleave-notices'
10
13
  import { attachmentKey, useLive } from '@/client/features/chat/chat-store'
11
- import type { ChatPromptBubble } from '@/client/features/chat/ChatPromptBubbles'
14
+ import type { ChatPromptBubble } from '@/client/features/chat/messages/ChatPromptBubbles'
12
15
  import type { ChatSendOptions } from '@/client/features/chat/chat-send'
13
16
  import { useWorkspaceId } from '@/client/features/workspace/WorkspaceContext'
14
17
  import type { AgentTheme, SystemNotice, Turn, ViewState } from '@/lib/types'
15
18
 
16
19
  import type { ComposerBanner } from './composer/banners/ComposerBanner'
17
20
  import { ChatComposer, type ComposerAnnotationControls } from './composer/ChatComposer'
18
- import { ChatEmptyState, type WelcomeDestination, resolveChatEmptyState } from './ChatEmptyState'
19
- import { ChatSelector } from './ChatSelector'
20
- import { TurnView } from './TurnView'
21
+ import {
22
+ ChatEmptyState,
23
+ type WelcomeDestination,
24
+ resolveChatEmptyState
25
+ } from './messages/ChatEmptyState'
26
+ import { ChatSelector } from './sessions/ChatSelector'
27
+ import { TurnView } from './messages/TurnView'
21
28
  import { Button } from '@/client/components/ui/button'
22
29
  import { Tooltip, TooltipContent, TooltipTrigger } from '@/client/components/ui/tooltip'
23
30
  import { cn } from '@/client/lib/cn'
@@ -98,7 +105,7 @@ export function ChatPanel({
98
105
  )
99
106
  const attachmentsUploading = useLive(state =>
100
107
  (state.attachments[attachmentKey(workspaceId, effectiveSessionId)] ?? []).some(
101
- attachment => attachment.status === 'uploading'
108
+ attachment => attachment.kind !== 'text' && attachment.status === 'uploading'
102
109
  )
103
110
  )
104
111
  const promptDisabled = !canSubmitComposerAction(true, attachmentsUploading, agentAvailability)
@@ -163,8 +170,10 @@ export function ChatPanel({
163
170
 
164
171
  return (
165
172
  <div className="flex min-h-0 flex-1 flex-col pt-2 pb-3">
166
- <header className="mx-auto flex w-full max-w-[calc(var(--chat-max-container)+40px)] items-center justify-between pr-2 pb-2 pl-2">
167
- <ChatSelector isViewBuilder={!!builderDraft} />
173
+ <header className="mx-auto flex w-full max-w-[calc(var(--chat-max-container)+40px)] min-w-0 items-center justify-between pr-2 pb-2 pl-2">
174
+ <div className="min-w-0 flex-1">
175
+ <ChatSelector isViewBuilder={!!builderDraft} />
176
+ </div>
168
177
  {onClose && docked && (
169
178
  <Tooltip>
170
179
  <TooltipTrigger
@@ -29,7 +29,12 @@ export function ChatPopup({
29
29
  nextOpen,
30
30
  eventDetails
31
31
  ) => {
32
- if (!nextOpen && eventDetails.reason === 'outside-press') {
32
+ // Applet actions move focus outside before their click handler runs.
33
+ // Keep chat open so adding context only refocuses the existing composer.
34
+ if (
35
+ !nextOpen &&
36
+ (eventDetails.reason === 'outside-press' || eventDetails.reason === 'focus-out')
37
+ ) {
33
38
  eventDetails.cancel()
34
39
  return
35
40
  }
@@ -59,7 +64,7 @@ export function ChatPopup({
59
64
  >
60
65
  <button
61
66
  type="button"
62
- className="group block cursor-pointer rounded-xl p-0 drop-shadow-[0_0_4px_var(--primary-foreground)] outline-none focus-visible:ring-3 focus-visible:ring-ring/50"
67
+ className="group block cursor-pointer p-0 drop-shadow-[0_0_4px_var(--background)] outline-none focus-visible:ring-3 focus-visible:ring-ring/50"
63
68
  aria-label="Agent"
64
69
  >
65
70
  <AgentBlobatar