@hanzo/app 0.3.91 → 0.3.93

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.
@@ -15,7 +15,7 @@ import { useAi } from '../lib/ai.js';
15
15
  import { say } from '../failure.js';
16
16
  import { called } from '../team.js';
17
17
  import { VaultDialog, readVault } from '../vault.js';
18
- import { actComputer, bootOrResumeComputer, disk, findBotComputer, grant, opened, parkComputer, updateBotMeta, } from './api.js';
18
+ import { actComputer, bootOrResumeComputer, disk, findBotComputer, grant, opened, parkComputer, runDesktopAgent, updateBotMeta, } from './api.js';
19
19
  import { CLIP, PILL, WORD } from './kit.js';
20
20
  import { DIM } from './ink.js';
21
21
  // Always-dark translucent surfaces inside the pure-black desktop window chrome,
@@ -43,10 +43,12 @@ export function ComputerDialog({ open, onOpenChange, bot, box, onUpdated, }) {
43
43
  const displayName = called(bot.name);
44
44
  const iframeRef = useRef(null);
45
45
  const autoBootedRef = useRef(false);
46
+ const controlRef = useRef('watching');
46
47
  const [localBox, setLocalBox] = useState(box);
47
48
  const [expanded, setExpanded] = useState(false);
48
49
  const [mode, setMode] = useState('screen');
49
50
  const [control, setControl] = useState('watching');
51
+ controlRef.current = control;
50
52
  const [phase, setPhase] = useState('idle');
51
53
  const [frameUrl, setFrameUrl] = useState(null);
52
54
  const [snapDataUrl, setSnapDataUrl] = useState(null);
@@ -59,8 +61,14 @@ export function ComputerDialog({ open, onOpenChange, bot, box, onUpdated, }) {
59
61
  const [vaultOpen, setVaultOpen] = useState(false);
60
62
  const [vaultItems, setVaultItems] = useState([]);
61
63
  useEffect(() => {
62
- if (box)
63
- setLocalBox(box);
64
+ if (!box)
65
+ return;
66
+ setLocalBox((prev) => {
67
+ if (prev?.status === 'running' && box.status !== 'running') {
68
+ return prev;
69
+ }
70
+ return box;
71
+ });
64
72
  }, [box]);
65
73
  const activeBox = localBox ?? box;
66
74
  const awake = activeBox?.status === 'running';
@@ -131,7 +139,7 @@ export function ComputerDialog({ open, onOpenChange, bot, box, onUpdated, }) {
131
139
  await onUpdated();
132
140
  if (leased.status === 'running') {
133
141
  const effectiveMode = leased.class === 'desktop' ? mode : 'terminal';
134
- await openStream(leased.id, effectiveMode, control === 'watching');
142
+ await openStream(leased.id, effectiveMode, controlRef.current === 'watching');
135
143
  }
136
144
  else {
137
145
  // Poll up to ~25s while the sandbox container schedules & boots X11
@@ -142,7 +150,7 @@ export function ComputerDialog({ open, onOpenChange, bot, box, onUpdated, }) {
142
150
  setLocalBox(latest);
143
151
  if (latest?.status === 'running') {
144
152
  await onUpdated();
145
- await openStream(latest.id, latest.class === 'desktop' ? mode : 'terminal', control === 'watching');
153
+ await openStream(latest.id, latest.class === 'desktop' ? mode : 'terminal', controlRef.current === 'watching');
146
154
  break;
147
155
  }
148
156
  }
@@ -158,8 +166,10 @@ export function ComputerDialog({ open, onOpenChange, bot, box, onUpdated, }) {
158
166
  finally {
159
167
  setBusy(false);
160
168
  }
161
- }, [client, busy, bot, activeBox, onUpdated, mode, openStream, control, displayName, t]);
169
+ }, [client, busy, bot, activeBox, onUpdated, mode, openStream, displayName, t]);
162
170
  // Auto-open live stream if running, or auto-boot/resume on open!
171
+ // Intentionally reads `controlRef.current` rather than depending on `control` so
172
+ // clicking `Take control` / `Watching` sends instant postMessage without reloading the iframe.
163
173
  useEffect(() => {
164
174
  if (!open) {
165
175
  autoBootedRef.current = false;
@@ -174,14 +184,14 @@ export function ComputerDialog({ open, onOpenChange, bot, box, onUpdated, }) {
174
184
  if (activeBox?.id && activeBox.status === 'running') {
175
185
  autoBootedRef.current = true;
176
186
  const effectiveMode = activeBox.class === 'desktop' ? mode : 'terminal';
177
- void openStream(activeBox.id, effectiveMode, control === 'watching');
187
+ void openStream(activeBox.id, effectiveMode, controlRef.current === 'watching');
178
188
  return;
179
189
  }
180
190
  if (!autoBootedRef.current && client) {
181
191
  autoBootedRef.current = true;
182
192
  void startOrResume(false);
183
193
  }
184
- }, [open, activeBox?.id, activeBox?.status, activeBox?.class, mode, client, openStream, startOrResume, control]);
194
+ }, [open, activeBox?.id, activeBox?.status, activeBox?.class, mode, client, openStream, startOrResume]);
185
195
  const toggleControl = (nextControl) => {
186
196
  setControl(nextControl);
187
197
  const viewOnly = nextControl === 'watching';
@@ -237,16 +247,21 @@ export function ComputerDialog({ open, onOpenChange, bot, box, onUpdated, }) {
237
247
  setError(null);
238
248
  setStatusToast(null);
239
249
  try {
240
- await actComputer(client, {
250
+ const out = await actComputer(client, {
241
251
  id: activeBox.id,
242
252
  action: 'autofill',
243
253
  credential: item.id || item.domain || item.name,
244
254
  field,
245
- submit: field === 'login' || field === 'totp',
255
+ submit: field === 'login' || field === 'login_totp' || field === 'totp',
246
256
  });
247
- setStatusToast(field === 'totp'
248
- ? t('Typed 6-digit 2FA code for {name} via KMS.', { name: item.name })
249
- : t('Autofilled {name} via zero-knowledge KMS.', { name: item.name }));
257
+ if (out.ok === false) {
258
+ setError(out.text || t('Could not autofill {name}.', { name: item.name }));
259
+ return;
260
+ }
261
+ setStatusToast(out.text ||
262
+ (field === 'totp'
263
+ ? t('Typed 6-digit 2FA code for {name} via KMS.', { name: item.name })
264
+ : t('Autofilled {name} via zero-knowledge KMS.', { name: item.name })));
250
265
  }
251
266
  catch (e) {
252
267
  setError(say(e, t('credential autofill'), 'save', t));
@@ -255,6 +270,31 @@ export function ComputerDialog({ open, onOpenChange, bot, box, onUpdated, }) {
255
270
  setRunningTask(false);
256
271
  }
257
272
  };
273
+ const sealStagedAppCredential = async () => {
274
+ if (!client || !activeBox?.id || runningTask)
275
+ return;
276
+ setRunningTask(true);
277
+ setError(null);
278
+ setStatusToast(null);
279
+ try {
280
+ const out = await actComputer(client, {
281
+ id: activeBox.id,
282
+ action: 'save_credential',
283
+ });
284
+ if (out.ok === false) {
285
+ setError(out.text || t('No staged credential found at /dev/shm/.hanzo-credential.json.'));
286
+ return;
287
+ }
288
+ await refreshVault();
289
+ setStatusToast(out.text || t('Sealed sandbox app credential into Hanzo KMS and shredded RAM file.'));
290
+ }
291
+ catch (e) {
292
+ setError(say(e, t('sandbox credential save'), 'save', t));
293
+ }
294
+ finally {
295
+ setRunningTask(false);
296
+ }
297
+ };
258
298
  const captureScreenshot = async () => {
259
299
  if (!client || !activeBox?.id || runningTask)
260
300
  return;
@@ -262,8 +302,12 @@ export function ComputerDialog({ open, onOpenChange, bot, box, onUpdated, }) {
262
302
  setError(null);
263
303
  try {
264
304
  const out = await actComputer(client, { id: activeBox.id, action: 'screenshot' });
305
+ if (out.ok === false) {
306
+ setError(out.text || t('Could not capture desktop screenshot.'));
307
+ return;
308
+ }
265
309
  if (out.image) {
266
- setSnapDataUrl(`data:${out.mimeType || 'image/png'};base64,${out.image}`);
310
+ setSnapDataUrl(out.image.startsWith('data:') ? out.image : `data:${out.mimeType || 'image/jpeg'};base64,${out.image}`);
267
311
  setStatusToast(t('Captured {w}×{h} frame.', { w: out.width || 1280, h: out.height || 800 }));
268
312
  }
269
313
  }
@@ -288,13 +332,27 @@ export function ComputerDialog({ open, onOpenChange, bot, box, onUpdated, }) {
288
332
  if (control === 'controlling')
289
333
  toggleControl('watching');
290
334
  try {
291
- if (!bot.tools?.length) {
292
- await updateBotMeta(client, bot.name, { tools: ['*'], computeRef: activeBox?.id || bot.computeRef }).catch(() => null);
335
+ if (activeBox?.id) {
336
+ const ran = await runDesktopAgent(activeBox.id, prompt, (step) => {
337
+ setStatusToast(step);
338
+ });
339
+ if (ran.savedCredential) {
340
+ await refreshVault();
341
+ }
342
+ setStatusToast(ran.output || t('Task completed on desktop computer.'));
343
+ }
344
+ else {
345
+ if (!bot.tools?.length) {
346
+ await updateBotMeta(client, bot.name, { tools: ['*'], computeRef: bot.computeRef }).catch(() => null);
347
+ }
348
+ const ran = await client.http.json({
349
+ method: 'POST',
350
+ path: `/v1/agent/${encodeURIComponent(bot.name)}/run`,
351
+ body: { input: prompt },
352
+ });
353
+ setStatusToast(ran.output || ran.error || t('Task completed on desktop computer.'));
354
+ await onUpdated();
293
355
  }
294
- const directive = `[Desktop Computer Use] Use your resumable desktop sandbox computer (computer_in_sandbox — screenshot, click, move, drag, type, key, scroll, navigate, autofill for KMS/1Password/LastPass logins, and shell) to complete this task while I watch live:\n\n${prompt}`;
295
- const ran = await client.agents.run(bot.name, directive);
296
- setStatusToast(ran.output || ran.error || t('Task completed on desktop computer.'));
297
- await onUpdated();
298
356
  }
299
357
  catch (err) {
300
358
  setError(say(err, t('desktop computer task'), 'save', t));
@@ -313,11 +371,16 @@ export function ComputerDialog({ open, onOpenChange, bot, box, onUpdated, }) {
313
371
  : `${displayName} · ${t('Desktop')}` }) })] }), _jsxs(XStack, { items: "center", gap: 6, children: [awake && activeBox?.id ? (_jsxs(_Fragment, { children: [_jsxs(XStack, { render: "button", type: 'button', "aria-label": mode === 'screen' ? t('Switch to Terminal') : t('Switch to Desktop GUI'), onClick: () => {
314
372
  setSnapDataUrl(null);
315
373
  setMode((m) => (m === 'screen' ? 'terminal' : 'screen'));
316
- }, height: 26, px: 9, gap: 5, rounded: 999, items: "center", bg: RAISED, borderWidth: 1, borderColor: LINE, cursor: "pointer", children: [mode === 'screen' ? (_jsx(Terminal, { size: 12, color: "var(--pure-white)", "aria-hidden": true })) : (_jsx(Monitor, { size: 12, color: "var(--pure-white)", "aria-hidden": true })), _jsx(Text, { fontSize: 11, lineHeight: 15, color: "var(--pure-white)", opacity: 0.85, children: mode === 'screen' ? t('Terminal') : t('Desktop') })] }), _jsxs(DropdownMenu, { placement: "bottom-end", offset: 6, children: [_jsx(DropdownMenuTrigger, { asChild: true, children: _jsxs(XStack, { render: "button", type: 'button', "aria-label": t('Autofill login or secret'), height: 26, px: 9, gap: 5, rounded: 999, items: "center", bg: RAISED, borderWidth: 1, borderColor: LINE, cursor: "pointer", children: [_jsx(KeyRound, { size: 12, color: "var(--pure-white)", "aria-hidden": true }), _jsx(Text, { fontSize: 11, lineHeight: 15, color: "var(--pure-white)", opacity: 0.85, children: t('Autofill') })] }) }), _jsxs(DropdownMenuContent, { minW: 240, children: [logins.map((item) => (_jsx(DropdownMenuItem, { onSelect: () => void runAutofill(item, 'login'), children: _jsxs(YStack, { gap: 1, children: [_jsx(Text, { fontSize: "$3", fontWeight: "500", color: "$ink", children: `${item.name}${item.domain ? ` (${item.domain})` : ''}` }), _jsx(Text, { fontSize: "$1", color: DIM, children: item.username
317
- ? t('Fill {user} + password via {src}', { user: item.username, src: item.source })
374
+ }, height: 26, px: 9, gap: 5, rounded: 999, items: "center", bg: RAISED, borderWidth: 1, borderColor: LINE, cursor: "pointer", children: [mode === 'screen' ? (_jsx(Terminal, { size: 12, color: "var(--pure-white)", "aria-hidden": true })) : (_jsx(Monitor, { size: 12, color: "var(--pure-white)", "aria-hidden": true })), _jsx(Text, { fontSize: 11, lineHeight: 15, color: "var(--pure-white)", opacity: 0.85, children: mode === 'screen' ? t('Terminal') : t('Desktop') })] }), _jsxs(DropdownMenu, { placement: "bottom-end", offset: 6, children: [_jsx(DropdownMenuTrigger, { asChild: true, children: _jsxs(XStack, { render: "button", type: 'button', "aria-label": t('Autofill login or secret'), height: 26, px: 9, gap: 5, rounded: 999, items: "center", bg: RAISED, borderWidth: 1, borderColor: LINE, cursor: "pointer", children: [_jsx(KeyRound, { size: 12, color: "var(--pure-white)", "aria-hidden": true }), _jsx(Text, { fontSize: 11, lineHeight: 15, color: "var(--pure-white)", opacity: 0.85, children: t('Autofill') })] }) }), _jsxs(DropdownMenuContent, { minW: 260, children: [logins.map((item) => (_jsx(DropdownMenuItem, { onSelect: () => void runAutofill(item, item.totpSecretKey ? 'login_totp' : 'login'), children: _jsxs(YStack, { gap: 1, children: [_jsx(Text, { fontSize: "$3", fontWeight: "500", color: "$ink", children: `${item.name}${item.domain ? ` (${item.domain})` : ''}` }), _jsx(Text, { fontSize: "$1", color: DIM, children: item.username
375
+ ? item.totpSecretKey
376
+ ? t('Fill {user} + password + 2FA via {src}', {
377
+ user: item.username,
378
+ src: item.source,
379
+ })
380
+ : t('Fill {user} + password via {src}', { user: item.username, src: item.source })
318
381
  : t('Fill password via {src}', { src: item.source }) })] }) }, item.id))), logins
319
382
  .filter((i) => Boolean(i.totpSecretKey))
320
- .map((item) => (_jsx(DropdownMenuItem, { onSelect: () => void runAutofill(item, 'totp'), children: _jsx(Text, { fontSize: "$2", color: "$ink", children: t('2FA TOTP code · {name}', { name: item.name }) }) }, `${item.id}-totp`))), otherSecrets.slice(0, 5).map((item) => (_jsx(DropdownMenuItem, { onSelect: () => void runAutofill(item, 'secret'), children: _jsx(Text, { fontSize: "$2", color: "$ink", children: t('Type secret · {name}', { name: item.name }) }) }, item.id))), _jsx(DropdownMenuItem, { onSelect: () => setVaultOpen(true), children: _jsx(Text, { fontSize: "$2", fontWeight: "600", color: "$ink", children: vaultItems.length === 0
383
+ .map((item) => (_jsx(DropdownMenuItem, { onSelect: () => void runAutofill(item, 'totp'), children: _jsx(Text, { fontSize: "$2", color: "$ink", children: t('2FA TOTP code only · {name}', { name: item.name }) }) }, `${item.id}-totp`))), otherSecrets.slice(0, 5).map((item) => (_jsx(DropdownMenuItem, { onSelect: () => void runAutofill(item, 'secret'), children: _jsx(Text, { fontSize: "$2", color: "$ink", children: t('Type secret · {name}', { name: item.name }) }) }, item.id))), _jsx(DropdownMenuItem, { onSelect: () => void sealStagedAppCredential(), children: _jsxs(YStack, { gap: 1, children: [_jsx(Text, { fontSize: "$2", fontWeight: "600", color: "$ink", children: t('Seal staged sandbox app login → KMS') }), _jsx(Text, { fontSize: "$1", color: DIM, children: t('Imports /dev/shm/.hanzo-credential.json & shreds RAM file') })] }) }), _jsx(DropdownMenuItem, { onSelect: () => setVaultOpen(true), children: _jsx(Text, { fontSize: "$2", fontWeight: "600", color: "$ink", children: vaultItems.length === 0
321
384
  ? t('+ Add 1Password, LastPass, or KMS logins…')
322
385
  : t('Manage Vault & Password Managers…') }) })] })] }), _jsx(XStack, { render: "button", type: 'button', "aria-label": t('Paste clipboard to desktop'), onClick: () => void pasteClipboardToDesktop(), width: 26, height: 26, rounded: 999, items: "center", justify: "center", bg: "transparent", borderWidth: 0, cursor: "pointer", hoverStyle: { bg: RAISED }, children: _jsx(ClipboardPaste, { size: 13, color: "var(--pure-white)", opacity: 0.7, "aria-hidden": true }) }), _jsx(XStack, { render: "button", type: 'button', "aria-label": t('Capture screenshot'), onClick: () => void captureScreenshot(), width: 26, height: 26, rounded: 999, items: "center", justify: "center", bg: "transparent", borderWidth: 0, cursor: "pointer", hoverStyle: { bg: RAISED }, children: _jsx(Camera, { size: 13, color: "var(--pure-white)", opacity: 0.7, "aria-hidden": true }) }), _jsx(XStack, { render: "button", type: 'button', "aria-label": t('Reconnect stream'), onClick: () => {
323
386
  setSnapDataUrl(null);
@@ -190,6 +190,7 @@ export declare function opened(url: string, viewOnly?: boolean): string;
190
190
  /**
191
191
  * Finds the bot's existing desktop sandbox across both named project disks (`bot-<name>`)
192
192
  * and Free-tier persistent `disk: true` sandboxes bound via `bot.computeRef`.
193
+ * Always prefers an actively `running` desktop sandbox over a stopped/expired lease.
193
194
  */
194
195
  export declare function findBotComputer(c: Client, bot: BotAgent, signal?: AbortSignal): Promise<Sandbox | null>;
195
196
  /**
@@ -205,7 +206,7 @@ export declare function bootOrResumeComputer(c: Client, bot: BotAgent, existing?
205
206
  export declare function parkComputer(c: Client, id: string): Promise<void>;
206
207
  export interface ComputerActionIn {
207
208
  id: string;
208
- action: 'screenshot' | 'click' | 'double_click' | 'right_click' | 'middle_click' | 'move' | 'drag' | 'scroll' | 'type' | 'key' | 'navigate' | 'wait' | 'shell' | 'autofill';
209
+ action: 'screenshot' | 'click' | 'double_click' | 'right_click' | 'middle_click' | 'move' | 'drag' | 'scroll' | 'type' | 'key' | 'navigate' | 'wait' | 'shell' | 'autofill' | 'save_credential';
209
210
  x?: number;
210
211
  y?: number;
211
212
  toX?: number;
@@ -213,16 +214,20 @@ export interface ComputerActionIn {
213
214
  dx?: number;
214
215
  dy?: number;
215
216
  text?: string;
217
+ keys?: string;
216
218
  key?: string;
217
219
  url?: string;
218
220
  command?: string;
221
+ seconds?: number;
219
222
  ms?: number;
220
223
  credential?: string;
221
- field?: 'login' | 'username' | 'password' | 'totp' | 'secret';
224
+ field?: 'login' | 'login_totp' | 'username' | 'password' | 'totp' | 'secret';
222
225
  submit?: boolean;
223
226
  }
224
227
  export interface ComputerActionOut {
225
- action: string;
228
+ ok?: boolean;
229
+ text?: string;
230
+ action?: string;
226
231
  stdout?: string;
227
232
  stderr?: string;
228
233
  exitCode?: number;
@@ -232,6 +237,15 @@ export interface ComputerActionOut {
232
237
  height?: number;
233
238
  }
234
239
  export declare const actComputer: (c: Client, body: ComputerActionIn) => Promise<ComputerActionOut>;
240
+ /**
241
+ * Streams a live desktop computer-use turn (`POST /v1/sandbox/:id/agent`),
242
+ * executing screenshot, click, type, key, shell, save_credential, and KMS/TOTP
243
+ * autofill directly on the open sandbox while reporting step progress via `onStep`.
244
+ */
245
+ export declare function runDesktopAgent(id: string, message: string, onStep?: (stepText: string) => void, signal?: AbortSignal): Promise<{
246
+ output: string;
247
+ savedCredential: boolean;
248
+ }>;
235
249
  export interface AutomationSchedule {
236
250
  kind: 'manual' | 'hourly' | 'daily' | 'weekdays' | 'weekly' | 'cron' | 'once';
237
251
  at?: string;
package/dist/bots/api.js CHANGED
@@ -70,14 +70,14 @@ export async function bootstrapPrimary(c, name = PRIMARY_BOT, model = 'enso') {
70
70
  }
71
71
  /** Atomically promotes `ref` to be the org's sole Primary Bot (Layer 0). */
72
72
  export const setPrimaryBot = (c, ref) => c.http.json({
73
- method: 'PUT',
74
- path: `/v1/agents/${encodeURIComponent(ref)}`,
73
+ method: 'PATCH',
74
+ path: `/v1/agent/${encodeURIComponent(ref)}`,
75
75
  body: { isPrimary: true, layer: 0 },
76
76
  });
77
77
  /** Updates hierarchy metadata (`groupId`, `layer`, `computeRef`, `tools`) on an existing bot. */
78
78
  export const updateBotMeta = (c, ref, patch) => c.http.json({
79
- method: 'PUT',
80
- path: `/v1/agents/${encodeURIComponent(ref)}`,
79
+ method: 'PATCH',
80
+ path: `/v1/agent/${encodeURIComponent(ref)}`,
81
81
  body: patch,
82
82
  });
83
83
  /** Lists all Bot Groups in the caller's org, ordered by layer. */
@@ -96,7 +96,7 @@ export const createGroup = (c, body) => c.http.json({
96
96
  body,
97
97
  });
98
98
  export const updateGroup = (c, ref, body) => c.http.json({
99
- method: 'PUT',
99
+ method: 'PATCH',
100
100
  path: `/v1/agent/groups/${encodeURIComponent(groupNameOf(ref))}`,
101
101
  body,
102
102
  });
@@ -204,30 +204,42 @@ function bestSandbox(boxes) {
204
204
  /**
205
205
  * Finds the bot's existing desktop sandbox across both named project disks (`bot-<name>`)
206
206
  * and Free-tier persistent `disk: true` sandboxes bound via `bot.computeRef`.
207
+ * Always prefers an actively `running` desktop sandbox over a stopped/expired lease.
207
208
  */
208
209
  export async function findBotComputer(c, bot, signal) {
209
210
  const opts = signal ? { signal } : undefined;
211
+ let projectFallback = null;
210
212
  try {
211
213
  const byProject = await c.sandboxes.list({ project: disk(bot.name) }, opts);
212
214
  const found = bestSandbox(byProject);
213
- if (found)
215
+ if (found?.status === 'running')
214
216
  return found;
217
+ projectFallback = found;
215
218
  }
216
219
  catch {
217
- // continue to computeRef lookup
220
+ // continue to org sandbox lookup
218
221
  }
219
- if (bot.computeRef) {
220
- try {
221
- const all = await c.sandboxes.list(undefined, opts);
222
+ try {
223
+ const all = await c.sandboxes.list(undefined, opts);
224
+ if (bot.computeRef) {
222
225
  const exact = all.find((b) => b.id === bot.computeRef);
223
- if (exact)
226
+ if (exact?.status === 'running')
224
227
  return exact;
228
+ if (exact && !projectFallback)
229
+ projectFallback = exact;
225
230
  }
226
- catch {
227
- // ignore
231
+ // If the Primary Bot has an active desktop sandbox running under the Free-tier
232
+ // fallback (where project name was omitted), prefer that live running desktop!
233
+ if (bot.isPrimary || bot.name === PRIMARY_BOT) {
234
+ const runningDesktop = all.find((b) => b.class === 'desktop' && b.status === 'running');
235
+ if (runningDesktop)
236
+ return runningDesktop;
228
237
  }
229
238
  }
230
- return null;
239
+ catch {
240
+ // ignore
241
+ }
242
+ return projectFallback;
231
243
  }
232
244
  function statusOfError(err) {
233
245
  if (!err || typeof err !== 'object')
@@ -311,6 +323,84 @@ export const actComputer = (c, body) => c.http.json({
311
323
  path: '/v1/sandbox/computer',
312
324
  body,
313
325
  });
326
+ /**
327
+ * Streams a live desktop computer-use turn (`POST /v1/sandbox/:id/agent`),
328
+ * executing screenshot, click, type, key, shell, save_credential, and KMS/TOTP
329
+ * autofill directly on the open sandbox while reporting step progress via `onStep`.
330
+ */
331
+ export async function runDesktopAgent(id, message, onStep, signal) {
332
+ const res = await fetch(`${api()}/v1/sandbox/${encodeURIComponent(id)}/agent`, {
333
+ method: 'POST',
334
+ headers: {
335
+ ...scope(),
336
+ 'Content-Type': 'application/json',
337
+ Accept: 'text/event-stream',
338
+ },
339
+ body: JSON.stringify({ message }),
340
+ signal,
341
+ });
342
+ if (!res.ok) {
343
+ const raw = await res.text().catch(() => '');
344
+ throw new Error(raw || `Desktop agent HTTP ${res.status}`);
345
+ }
346
+ const reader = res.body?.getReader();
347
+ if (!reader) {
348
+ return { output: '', savedCredential: false };
349
+ }
350
+ const dec = new TextDecoder();
351
+ let buf = '';
352
+ const sayings = [];
353
+ let lastResult = '';
354
+ let savedCredential = false;
355
+ while (true) {
356
+ const { value, done } = await reader.read();
357
+ if (done)
358
+ break;
359
+ buf += dec.decode(value, { stream: true });
360
+ let nl = buf.indexOf('\n');
361
+ while (nl >= 0) {
362
+ const line = buf.slice(0, nl).trim();
363
+ buf = buf.slice(nl + 1);
364
+ nl = buf.indexOf('\n');
365
+ if (!line.startsWith('data:'))
366
+ continue;
367
+ const payload = line.slice(5).trim();
368
+ if (!payload)
369
+ continue;
370
+ try {
371
+ const frame = JSON.parse(payload);
372
+ if (frame.type === 'act' && frame.tool) {
373
+ onStep?.(`Running ${frame.tool}…`);
374
+ if (frame.tool === 'save_credential')
375
+ savedCredential = true;
376
+ }
377
+ else if (frame.type === 'result' && frame.text) {
378
+ lastResult = frame.text;
379
+ onStep?.(frame.text);
380
+ if (frame.tool === 'save_credential' && frame.ok !== false) {
381
+ savedCredential = true;
382
+ }
383
+ }
384
+ else if (frame.type === 'say' && frame.text) {
385
+ sayings.push(frame.text);
386
+ onStep?.(frame.text);
387
+ }
388
+ else if (frame.type === 'error' && frame.text) {
389
+ throw new Error(frame.text);
390
+ }
391
+ }
392
+ catch (err) {
393
+ if (err instanceof Error && !err.message.includes('JSON')) {
394
+ throw err;
395
+ }
396
+ }
397
+ }
398
+ }
399
+ return {
400
+ output: sayings.join(' ').trim() || lastResult,
401
+ savedCredential,
402
+ };
403
+ }
314
404
  export async function listBotAutomations(c, botName, signal) {
315
405
  try {
316
406
  const res = await c.http.json({
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hanzo/app",
3
- "version": "0.3.91",
3
+ "version": "0.3.93",
4
4
  "description": "Hanzo's app: the shell and its apps (Home, Chat, Inbox, Contacts, Meet, Calendar, Drive, Board, Work, Bots), once, for every Hanzo surface.",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",