moi-computer 0.10.1-next.4 → 0.10.1-next.6

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 CHANGED
@@ -6,6 +6,8 @@
6
6
  THE UI FOR YOUR AI
7
7
  ```
8
8
 
9
+ [![npm version](https://img.shields.io/npm/v/moi-computer?label=npm&labelColor=555&color=E85D2A)](https://www.npmjs.com/package/moi-computer)  [![Discord](https://img.shields.io/badge/Discord-Join%20server-5865F2?logo=discord&logoColor=white&labelColor=555)](https://discord.gg/nydjacJrT)
10
+
9
11
  An extendable visual workspace where your AI agents build their own UI.
10
12
  See [moi.computer](https://moi.computer) for what it is and how it works.
11
13
 
@@ -17,6 +17,20 @@ const SETTLE_MS = 5_000
17
17
  const ASSET_TIMEOUT_MS = 1_000
18
18
  const CAPTURE_TIMEOUT_MS = 10_000
19
19
 
20
+ // Content outside the capture frame is clipped from the rendered image, so
21
+ // cloning it is pure main-thread cost: the DOM clone + style-inline walk is
22
+ // synchronous and scales with node count, and a view with a few thousand list
23
+ // rows freezes the page for minutes while the user types in the chat. The
24
+ // capture filter keeps only elements that can reach the frame, padded a little
25
+ // so edge shadows survive.
26
+ export const CAPTURE_AREA_PAD_PX = 100
27
+
28
+ // Even in-frame content has a ceiling: past this many kept elements the
29
+ // synchronous clone is guaranteed to jank (measured well over SLOW_CAPTURE_MS),
30
+ // and no thumbnail is worth that. The count uses the same filter with an early
31
+ // bail, so checking is cheap no matter how big the applet's DOM is.
32
+ export const MAX_CAPTURE_NODES = 5_000
33
+
20
34
  export type AppletThumbnailTarget = {
21
35
  id: string
22
36
  revision?: string
@@ -75,12 +89,84 @@ const timeoutNull = (ms: number) =>
75
89
  setTimeout(() => resolve(null), ms)
76
90
  })
77
91
 
92
+ type CaptureRect = Pick<DOMRect, 'top' | 'bottom' | 'left' | 'right' | 'width' | 'height'>
93
+
94
+ type CaptureRectSource = { getBoundingClientRect: () => CaptureRect }
95
+
96
+ // The clone filter: keep an element only when its box can reach the capture
97
+ // frame. Zero-size elements stay unless hidden — `display: contents` wrappers
98
+ // and collapsed positioning hosts report an empty rect while their children
99
+ // are visible, but a `display: none` element heads a subtree where every
100
+ // descendant also reports a zero box, and keeping it would spend the budget
101
+ // and the clone on invisible content. Excluding an element excludes its whole
102
+ // subtree, which is what makes capturing a long scrollable list cheap: every
103
+ // off-screen row costs one rect read instead of a full clone.
104
+ //
105
+ // Known tradeoff: an off-frame ancestor is pruned even if an absolutely- or
106
+ // fixed-positioned descendant would render back inside the frame. Rects are
107
+ // post-transform and zero-size hosts are kept, so in practice this only costs
108
+ // decorative thumbnail fidelity for escape-positioned content — never keep
109
+ // whole off-screen subtrees to chase it; that is the freeze this filter fixes.
110
+ export function captureAreaFilter(
111
+ root: CaptureRectSource,
112
+ pad = CAPTURE_AREA_PAD_PX
113
+ ): (node: Node) => boolean {
114
+ const frame = root.getBoundingClientRect()
115
+ const top = frame.top - pad
116
+ const bottom = frame.bottom + pad
117
+ const left = frame.left - pad
118
+ const right = frame.right + pad
119
+ return node => {
120
+ // Literal ELEMENT_NODE: the global `Node` constant isn't available in the
121
+ // DOM-less test runtime.
122
+ if (node.nodeType !== 1) return true
123
+ const element = node as Element
124
+ const rect = element.getBoundingClientRect()
125
+ if (rect.width === 0 && rect.height === 0) {
126
+ // Optional chains double as the test seam: fake nodes without a document
127
+ // read as structural wrappers and stay kept.
128
+ const display = element.ownerDocument?.defaultView?.getComputedStyle(element).display
129
+ return display !== 'none'
130
+ }
131
+ return rect.bottom > top && rect.top < bottom && rect.right > left && rect.left < right
132
+ }
133
+ }
134
+
135
+ type CaptureTreeNode = { children: Iterable<CaptureTreeNode> } & Node
136
+
137
+ // True when the filtered clone would still visit more than `budget` elements.
138
+ // Walks exactly the subtrees the clone would, bailing out at the budget, so a
139
+ // huge but mostly off-screen DOM is answered in a few thousand rect reads.
140
+ export function exceedsCaptureBudget(
141
+ root: CaptureTreeNode,
142
+ keep: (node: Node) => boolean,
143
+ budget = MAX_CAPTURE_NODES
144
+ ): boolean {
145
+ let remaining = budget
146
+ const walk = (node: CaptureTreeNode): boolean => {
147
+ for (const child of node.children) {
148
+ if (!keep(child)) continue
149
+ if (--remaining < 0) return true
150
+ if (walk(child)) return true
151
+ }
152
+ return false
153
+ }
154
+ return walk(root)
155
+ }
156
+
78
157
  async function captureThumbnail(
79
158
  element: HTMLElement,
80
159
  stripHostChrome: boolean
81
160
  ): Promise<string | null> {
82
161
  const { createContext, destroyContext, domToDataUrl } = await import('modern-screenshot')
83
162
 
163
+ // The budget check runs before the expensive clone; a target too dense even
164
+ // inside the frame skips capture entirely. The null result is recorded like a
165
+ // failed capture — any previous thumbnail is kept, and the next routine
166
+ // attempt re-runs only this cheap check.
167
+ const keep = captureAreaFilter(element)
168
+ if (exceedsCaptureBudget(element, keep)) return null
169
+
84
170
  const run = async () => {
85
171
  const context = await createContext(element, {
86
172
  scale: thumbnailScale(element),
@@ -88,6 +174,7 @@ async function captureThumbnail(
88
174
  quality: 0.8,
89
175
  backgroundColor: '#ffffff',
90
176
  timeout: ASSET_TIMEOUT_MS,
177
+ filter: keep,
91
178
  ...(stripHostChrome && {
92
179
  onCreateForeignObjectSvg: (svg: SVGSVGElement) => {
93
180
  const style = document.createElement('style')
@@ -66,11 +66,12 @@ export function ChatPopup({
66
66
  preset={agent}
67
67
  color="primary"
68
68
  animated={loading}
69
+ size={loading ? 80 : 64}
69
70
  expression={loading ? 'thinking' : undefined}
70
71
  className={cn(
71
72
  'drop-shadow-md drop-shadow-primary/25',
72
73
  'group-hover:drop-shadow-lg group-hover:drop-shadow-primary/40',
73
- 'transition-[filter] duration-400 ease-in-out motion-reduce:transition-none'
74
+ 'transition-[width,height,filter] duration-300 ease-in-out motion-reduce:transition-none'
74
75
  )}
75
76
  />
76
77
  </button>
@@ -296,10 +296,12 @@ export const liveStore = createStore<LiveStore>()(set => ({
296
296
  }))
297
297
 
298
298
  // Select the live previews for a session, split into the root (top-level
299
- // assistant) stream and per-subagent streams. Takes the raw `previews` record
300
- // (a stable store slice) so callers select that slice and run this inside a
301
- // `useMemo` — never return a fresh object straight from a zustand selector.
302
- // `null` sessionId yields empties.
299
+ // assistant) stream and per-subagent streams. The container object is fresh
300
+ // per call, but `root` and the `byParent` values are the STORED preview
301
+ // objects — so a zustand selector may return one of those directly (identity
302
+ // only changes when that stream gets a delta), while returning the container
303
+ // itself from a selector would re-render on every store write. `null`
304
+ // sessionId yields empties.
303
305
  export function selectPreviews(
304
306
  previews: Record<string, LivePreview>,
305
307
  workspaceId: string,
@@ -81,13 +81,13 @@ export function useChat(address: WorkspaceTabAddress) {
81
81
  // The live streaming preview as a synthetic assistant turn, so the ChatPanel
82
82
  // can run it through the SAME groupTurns pipeline as finalized turns — a
83
83
  // thinking-only preview then folds into the current tool group instead of
84
- // rendering as a detached block. Recomputed per delta (the previews slice is
85
- // stable across other updates); null when there's nothing visible yet.
86
- const previews = useLive(s => s.previews)
87
- const previewTurn = useMemo(
88
- () => buildPreviewTurn(selectPreviews(previews, workspaceId, selectedSessionId).root),
89
- [previews, workspaceId, selectedSessionId]
90
- )
84
+ // rendering as a detached block. The selector returns the stored root-preview
85
+ // object for THIS session (not the whole slice): deltas for other sessions
86
+ // and subagent streams keep its identity, so only the selected session's own
87
+ // stream re-renders the screen hosting this hook. Null when there's nothing
88
+ // visible yet.
89
+ const rootPreview = useLive(s => selectPreviews(s.previews, workspaceId, selectedSessionId).root)
90
+ const previewTurn = useMemo(() => buildPreviewTurn(rootPreview), [rootPreview])
91
91
 
92
92
  // The selected session's persisted model/effort. For a brand-new chat (no
93
93
  // session yet) this is empty and `send` falls back to workspace defaults.
@@ -13,7 +13,7 @@
13
13
  // because both builds share the slot's style tag (see ViewSlot), and it is
14
14
  // worth the 200ms — it is the only signal that the thing under your cursor just
15
15
  // changed underneath you.
16
- import { Activity, type ComponentType, useCallback, useEffect, useRef, useState } from 'react'
16
+ import { Activity, type ComponentType, memo, useCallback, useEffect, useRef, useState } from 'react'
17
17
 
18
18
  import { Spinner } from '@/client/components/ui/spinner'
19
19
  import { appletScope, appletStyleKey } from '@/client/features/applets/applet-cache'
@@ -48,7 +48,18 @@ type ViewManagerProps = {
48
48
  params: Record<string, unknown>
49
49
  }
50
50
 
51
- export function ViewManager({ views, activeViewId, params }: ViewManagerProps) {
51
+ // Memoized against the workspace screen's own churn: the screen re-renders on
52
+ // every streaming delta of the selected chat (and on focus/popup state), and
53
+ // without the bail-out each of those re-renders the full applet tree of every
54
+ // resident view — typing next to a streaming agent then janks on a big view.
55
+ // All three props are identity-stable outside real changes: `views` comes from
56
+ // the workspace query cache, `activeViewId` is a string, and `params` is
57
+ // memoized by navigation state.
58
+ export const ViewManager = memo(function ViewManager({
59
+ views,
60
+ activeViewId,
61
+ params
62
+ }: ViewManagerProps) {
52
63
  const residents = useResidentViews(activeViewId, views)
53
64
  // Render in workspace order, not residency order: the policy ranks views by
54
65
  // recency, and reordering the children would make React move live DOM around
@@ -68,7 +79,7 @@ export function ViewManager({ views, activeViewId, params }: ViewManagerProps) {
68
79
  ))}
69
80
  </div>
70
81
  )
71
- }
82
+ })
72
83
 
73
84
  // The views mounted right now: the active one, plus the recently-visited ones
74
85
  // parked offscreen. The policy (and its timing) lives in view-residency.ts.
@@ -1 +1 @@
1
- import{_c as r}from"/chunk-v7wr1df4.js";if(typeof globalThis.structuredClone==="function")globalThis.structuredClone=globalThis.structuredClone.bind(globalThis);async function o(a){let[{mount:t}]=await Promise.all([import("/chunk-jpdn7nx7.js"),import("/chunk-kmbxhgwv.js").then((e)=>e.loadAppConfig())]);t(a)}globalThis.__init=o;
1
+ import{_c as r}from"/chunk-v7wr1df4.js";if(typeof globalThis.structuredClone==="function")globalThis.structuredClone=globalThis.structuredClone.bind(globalThis);async function o(a){let[{mount:t}]=await Promise.all([import("/chunk-vwzshv0d.js"),import("/chunk-kmbxhgwv.js").then((e)=>e.loadAppConfig())]);t(a)}globalThis.__init=o;