@hanzo/app 0.3.92 → 0.3.94

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,
@@ -61,8 +61,14 @@ export function ComputerDialog({ open, onOpenChange, bot, box, onUpdated, }) {
61
61
  const [vaultOpen, setVaultOpen] = useState(false);
62
62
  const [vaultItems, setVaultItems] = useState([]);
63
63
  useEffect(() => {
64
- if (box)
65
- 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
+ });
66
72
  }, [box]);
67
73
  const activeBox = localBox ?? box;
68
74
  const awake = activeBox?.status === 'running';
@@ -326,13 +332,27 @@ export function ComputerDialog({ open, onOpenChange, bot, box, onUpdated, }) {
326
332
  if (control === 'controlling')
327
333
  toggleControl('watching');
328
334
  try {
329
- if (!bot.tools?.length) {
330
- 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();
331
355
  }
332
- 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}`;
333
- const ran = await client.agents.run(bot.name, directive);
334
- setStatusToast(ran.output || ran.error || t('Task completed on desktop computer.'));
335
- await onUpdated();
336
356
  }
337
357
  catch (err) {
338
358
  setError(say(err, t('desktop computer task'), 'save', t));
@@ -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
  /**
@@ -236,6 +237,15 @@ export interface ComputerActionOut {
236
237
  height?: number;
237
238
  }
238
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
+ }>;
239
249
  export interface AutomationSchedule {
240
250
  kind: 'manual' | 'hourly' | 'daily' | 'weekdays' | 'weekly' | 'cron' | 'once';
241
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({
@@ -11,10 +11,19 @@ export declare function nextWeek(now: number): string;
11
11
  /** When a cap's period after `now` begins: calendar-aligned in UTC, as the platform counts it. */
12
12
  export declare function nextPeriod(now: number, period?: string): string;
13
13
  /**
14
- * A refusal about money, as a notice: the gateway's billing envelope (its
15
- * message, when it lifts, its ways past), the bot's own weekly budget spent, or
16
- * the run's 402 for an org with no balance with the credits page it names (when
17
- * that page is one of ours). Any other failure is not a notice.
14
+ * Where a reader adds credit: pay's amount step, for this browser's
15
+ * organization, coming back to this page — the address every other Add credits
16
+ * in the app opens (lib/limits.ts), and never the console page the platform
17
+ * names. Read at the moment of the refusal, since the organization moves with
18
+ * the switcher.
19
+ */
20
+ export declare function topup(t?: Say): LimitAction;
21
+ /**
22
+ * A refusal about money, as a notice: no money (an empty balance, no plan and
23
+ * no credit, a spent Free allowance) in the app's words with pay's Add credits,
24
+ * the gateway's billing envelope (its message, when it lifts, its ways past,
25
+ * any top-up among them sent to pay), or the bot's own weekly budget spent.
26
+ * Any other failure is not a notice.
18
27
  */
19
28
  export declare function refusal(e: unknown, name: string, now: number, t?: Say, period?: string): Notice | null;
20
29
  /**
@@ -1,6 +1,8 @@
1
1
  // A refusal about money, read from what the platform answered.
2
2
  import { english } from '@hanzo/i18n';
3
3
  import { billed, ours, say } from '../failure.js';
4
+ import { payUrl } from '../lib/pay.js';
5
+ import { org } from '../lib/session.js';
4
6
  /** The agents route's code for a bot whose own period cap is spent (apps/agents budget.go). */
5
7
  const BUDGET = 'budget_exceeded';
6
8
  /** Monday 00:00 UTC after `now`: a weekly cap's period is the calendar week in UTC. */
@@ -20,20 +22,70 @@ export function nextPeriod(now, period = 'week') {
20
22
  return nextWeek(now);
21
23
  }
22
24
  /**
23
- * A refusal about money, as a notice: the gateway's billing envelope (its
24
- * message, when it lifts, its ways past), the bot's own weekly budget spent, or
25
- * the run's 402 for an org with no balance with the credits page it names (when
26
- * that page is one of ours). Any other failure is not a notice.
25
+ * The refusals money clears (cloud sdk/errmap.go `funded`): no balance, no
26
+ * subscription with no credit, and a spent Free-plan allowance. The platform
27
+ * says each with its console's credits page in the sentence and as
28
+ * `billingUrl`; a reader is told in the app's own words instead, with the one
29
+ * way past it, adding credit.
30
+ */
31
+ const EMPTY = new Set(['insufficient_balance', 'payment_required']);
32
+ const SPENT = 'allowance_spent';
33
+ /**
34
+ * Where a reader adds credit: pay's amount step, for this browser's
35
+ * organization, coming back to this page — the address every other Add credits
36
+ * in the app opens (lib/limits.ts), and never the console page the platform
37
+ * names. Read at the moment of the refusal, since the organization moves with
38
+ * the switcher.
39
+ */
40
+ export function topup(t = english) {
41
+ const here = typeof window === 'undefined' ? undefined : window.location.href;
42
+ return { kind: 'topup', label: t('Add credits'), url: payUrl('/amount/card', { org: org(), returnUrl: here }) };
43
+ }
44
+ /**
45
+ * Hanzo's console, as a reader reaches it: platform.hanzo.ai. The platform
46
+ * names console.hanzo.ai in the pages its refusals send a person to, in the
47
+ * sentence and as `billingUrl`; both are read with the host a reader knows.
48
+ */
49
+ const platform = (s) => s.replace(/https:\/\/console\.hanzo\.ai(?=[/?#\s.,;)]|$)/g, 'https://platform.hanzo.ai');
50
+ /** What an out-of-money refusal says, and its way past, or null for any other code. */
51
+ function broke(code, reason, resets, t) {
52
+ // An unresolved 402 is billing that could not be checked, not money missing: its cure is a retry.
53
+ if (typeof code !== 'string' || reason === 'unresolved')
54
+ return null;
55
+ if (EMPTY.has(code))
56
+ return { message: t('Your balance is empty.'), resets: null, actions: [topup(t)] };
57
+ if (code === SPENT)
58
+ return { message: t('Usage reached.'), resets, actions: [topup(t)] };
59
+ return null;
60
+ }
61
+ /**
62
+ * A refusal about money, as a notice: no money (an empty balance, no plan and
63
+ * no credit, a spent Free allowance) in the app's words with pay's Add credits,
64
+ * the gateway's billing envelope (its message, when it lifts, its ways past,
65
+ * any top-up among them sent to pay), or the bot's own weekly budget spent.
66
+ * Any other failure is not a notice.
27
67
  */
28
68
  export function refusal(e, name, now, t = english, period = 'week') {
29
69
  const no = billed(e);
70
+ // The gateway's envelope keeps its own words and ways, save an empty balance
71
+ // (whose sentence names the console) and a top-up, which is pay's.
30
72
  if (no)
31
- return { message: no.message || say(e, t('your message to {name}', { name }), 'save', t), resets: no.resets_at, actions: no.actions };
73
+ return EMPTY.has(no.code)
74
+ ? broke(no.code, undefined, no.resets_at, t)
75
+ : {
76
+ message: no.message ? platform(no.message) : say(e, t('your message to {name}', { name }), 'save', t),
77
+ resets: no.resets_at,
78
+ actions: no.actions.map((a) => (a.kind === 'topup' ? { ...a, url: topup(t).url } : a)),
79
+ };
32
80
  const status = e?.status;
33
81
  if (status !== 402 && status !== 429)
34
82
  return null;
35
83
  const raw = e?.body;
36
84
  const body = raw && typeof raw.error === 'object' && raw.error ? raw.error : raw ?? {};
85
+ const resets = typeof body.resets_at === 'string' ? body.resets_at : null;
86
+ const money = broke(body.code, body.reason, resets, t);
87
+ if (money)
88
+ return money;
37
89
  if (body.code === BUDGET) {
38
90
  const spent = period === 'day'
39
91
  ? t('{name} has spent today’s budget.', { name })
@@ -42,11 +94,13 @@ export function refusal(e, name, now, t = english, period = 'week') {
42
94
  : t('{name} has spent this week’s budget.', { name });
43
95
  return { message: spent, resets: nextPeriod(now, period), actions: [] };
44
96
  }
97
+ // Any other refusal is the platform's own words, and the console page it names
98
+ // (a spend cap's budgets) is offered as what it is, never as Add credits.
45
99
  const url = ours(body.billingUrl);
46
100
  return {
47
- message: typeof body.message === 'string' && body.message ? body.message : say(e, t('your message to {name}', { name }), 'save', t),
48
- resets: typeof body.resets_at === 'string' ? body.resets_at : null,
49
- actions: url ? [{ kind: 'topup', label: t('Add credits'), url }] : [],
101
+ message: typeof body.message === 'string' && body.message ? platform(body.message) : say(e, t('your message to {name}', { name }), 'save', t),
102
+ resets,
103
+ actions: url ? [{ kind: 'upgrade', label: body.code === 'spend_cap_exceeded' ? t('Raise the spend cap') : t('Open billing'), url: platform(url) }] : [],
50
104
  };
51
105
  }
52
106
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@hanzo/app",
3
- "version": "0.3.92",
3
+ "version": "0.3.94",
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",