@anna-ai/app-runtime 0.13.0 → 0.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.js +245 -10
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,37 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Anna App Runtime SDK — v0.
|
|
2
|
+
* Anna App Runtime SDK — v0.15.0
|
|
3
|
+
*
|
|
4
|
+
* v0.15.0 — Adds the async tool-job family (design
|
|
5
|
+
* anna-app-tools-invoke-async-jobs.md §3): `anna.tools.invokeAsync`
|
|
6
|
+
* (returns `{jobId, state:"queued", deadlineMs}` immediately — no
|
|
7
|
+
* long-held HTTP request), `anna.tools.getJob({jobId, sinceSeq?, limit?})`
|
|
8
|
+
* (authoritative snapshot + incremental progress; also the reload-recovery
|
|
9
|
+
* read), `anna.tools.cancelJob({jobId, reason?})` (idempotent) and
|
|
10
|
+
* `anna.tools.listJobs({tool_id?, state?, clientTag?, since?, limit?})`.
|
|
11
|
+
* Plus the high-level `anna.tools.invokeAsyncAwait(args, opts?)` —
|
|
12
|
+
* invokeAsync + `tool_job` host-event subscription + adaptive polling
|
|
13
|
+
* fallback (2s before the first event, 15s once events flow) +
|
|
14
|
+
* `opts.onProgress(ev)` + `opts.signal` (abort → cancelJob) + wall-clock
|
|
15
|
+
* `opts.timeoutMs` (default job deadline + 30s); resolves with the plugin
|
|
16
|
+
* result payload, rejects with `.code` = `tool_failed` / `tool_timeout` /
|
|
17
|
+
* `cancelled` / `wait_timeout`. Use the async family for any tool call
|
|
18
|
+
* that may exceed the 90s synchronous ceiling.
|
|
19
|
+
* Coordinated versions: dispatcher_version 0.19.0 + schema 0.19.0 +
|
|
20
|
+
* anna-app-core 0.18.0 (all shipped in nexus ≥ 1.1.0-beta.108; jobs
|
|
21
|
+
* execute end-to-end from beta.110). Additive: old hosts reject the new
|
|
22
|
+
* methods with `not_implemented`.
|
|
23
|
+
*
|
|
24
|
+
* v0.14.0 — Adds `anna.agent.session.catalog()` — pre-create discovery of
|
|
25
|
+
* the platform-tool universe for agent sessions. Returns
|
|
26
|
+
* `{platform_tools: [{name, description, dev_safe, declared_in_manifest,
|
|
27
|
+
* user_granted, eligible, blocked_by?}], inherit_host_tools_granted,
|
|
28
|
+
* manifest_declared, user_granted}` so apps can learn which names are
|
|
29
|
+
* legal in create-time `quotaCaps.allowed_tools` / per-run
|
|
30
|
+
* `allowed_tools` — previously only discoverable after-the-fact via the
|
|
31
|
+
* create response's `granted_tools` echo. Read-only; mints no session.
|
|
32
|
+
* Coordinated bump: wire method `agent/session.catalog` —
|
|
33
|
+
* dispatcher_version 0.18.0 + schema 0.18.0 + anna-app-core 0.17.0.
|
|
34
|
+
* Additive: old hosts reject with `not_implemented`.
|
|
3
35
|
*
|
|
4
36
|
* v0.11.0 — Adds the `anna.credentials.*` namespace — platform-credentials
|
|
5
37
|
* multi-account access (design platform-credentials-multi-account.md §7).
|
|
@@ -200,21 +232,213 @@
|
|
|
200
232
|
);
|
|
201
233
|
}
|
|
202
234
|
|
|
203
|
-
// ---- Tools namespace (v0.12.0) ----
|
|
235
|
+
// ---- Tools namespace (v0.12.0; async job family v0.15.0) ----
|
|
204
236
|
//
|
|
205
|
-
// Plain RPC proxy (`anna.tools.invoke`, `anna.tools.list
|
|
206
|
-
//
|
|
237
|
+
// Plain RPC proxy (`anna.tools.invoke`, `anna.tools.list`,
|
|
238
|
+
// `anna.tools.invokeAsync`, `anna.tools.getJob`, `anna.tools.cancelJob`,
|
|
239
|
+
// `anna.tools.listJobs`) plus two client-side helpers:
|
|
207
240
|
//
|
|
208
241
|
// const off = anna.tools.onChanged(({ client_id, deployed }) => {
|
|
209
242
|
// // deploy reconciliation just landed tools on the agent —
|
|
210
243
|
// // re-run anna.tools.list() and re-enable buttons.
|
|
211
244
|
// });
|
|
212
245
|
//
|
|
213
|
-
//
|
|
214
|
-
//
|
|
215
|
-
//
|
|
216
|
-
//
|
|
217
|
-
//
|
|
246
|
+
// const result = await anna.tools.invokeAsyncAwait(
|
|
247
|
+
// { tool_id, method, args, timeoutMs: 300000, clientTag: "render-1" },
|
|
248
|
+
// { onProgress: (ev) => bar.update(ev), signal: abortCtl.signal },
|
|
249
|
+
// );
|
|
250
|
+
//
|
|
251
|
+
// `invokeAsyncAwait` = invokeAsync + `tool_job` host-event subscription +
|
|
252
|
+
// adaptive polling fallback + abort→cancelJob. It resolves with the
|
|
253
|
+
// plugin's result payload and rejects with `.code` set to `tool_failed`,
|
|
254
|
+
// `tool_timeout` (job expired), `cancelled` or `wait_timeout` (client
|
|
255
|
+
// wall clock elapsed — the job may still be running; keep the `.jobId`
|
|
256
|
+
// property and recover via getJob/listJobs).
|
|
257
|
+
|
|
258
|
+
const JOB_TERMINAL_STATES = new Set([
|
|
259
|
+
"succeeded",
|
|
260
|
+
"failed",
|
|
261
|
+
"cancelled",
|
|
262
|
+
"expired",
|
|
263
|
+
]);
|
|
264
|
+
// Poll cadence for invokeAsyncAwait: tight before the first host event
|
|
265
|
+
// proves the push channel is alive, relaxed afterwards (events reset the
|
|
266
|
+
// timer, so polling is purely a safety net once they flow).
|
|
267
|
+
const JOB_POLL_NO_EVENTS_MS = 2000;
|
|
268
|
+
const JOB_POLL_WITH_EVENTS_MS = 15000;
|
|
269
|
+
|
|
270
|
+
function jobErrorFromSnapshot(snap) {
|
|
271
|
+
const e = snap.error || {};
|
|
272
|
+
const code =
|
|
273
|
+
e.code ||
|
|
274
|
+
(snap.state === "expired"
|
|
275
|
+
? "tool_timeout"
|
|
276
|
+
: snap.state === "cancelled"
|
|
277
|
+
? "cancelled"
|
|
278
|
+
: "tool_failed");
|
|
279
|
+
const err = new Error(
|
|
280
|
+
e.message || `tool job ${snap.jobId} ended as ${snap.state}`
|
|
281
|
+
);
|
|
282
|
+
err.code = code;
|
|
283
|
+
err.details = e.details;
|
|
284
|
+
err.jobId = snap.jobId;
|
|
285
|
+
err.state = snap.state;
|
|
286
|
+
return err;
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
function invokeAsyncAwaitImpl(rt, args, opts = {}) {
|
|
290
|
+
const onProgress =
|
|
291
|
+
typeof opts.onProgress === "function" ? opts.onProgress : null;
|
|
292
|
+
const signal = opts.signal || null;
|
|
293
|
+
|
|
294
|
+
return new Promise((resolve, reject) => {
|
|
295
|
+
let settled = false;
|
|
296
|
+
let jobId = null;
|
|
297
|
+
let lastSeq = 0;
|
|
298
|
+
let eventSeen = false;
|
|
299
|
+
let pollTimer = null;
|
|
300
|
+
let wallTimer = null;
|
|
301
|
+
let pollInFlight = false;
|
|
302
|
+
let offEvent = null;
|
|
303
|
+
let onAbort = null;
|
|
304
|
+
|
|
305
|
+
const cleanup = () => {
|
|
306
|
+
if (pollTimer) clearTimeout(pollTimer);
|
|
307
|
+
if (wallTimer) clearTimeout(wallTimer);
|
|
308
|
+
if (offEvent) offEvent();
|
|
309
|
+
if (signal && onAbort) signal.removeEventListener("abort", onAbort);
|
|
310
|
+
};
|
|
311
|
+
const settleOk = (v) => {
|
|
312
|
+
if (settled) return;
|
|
313
|
+
settled = true;
|
|
314
|
+
cleanup();
|
|
315
|
+
resolve(v);
|
|
316
|
+
};
|
|
317
|
+
const settleErr = (e) => {
|
|
318
|
+
if (settled) return;
|
|
319
|
+
settled = true;
|
|
320
|
+
cleanup();
|
|
321
|
+
if (jobId && e && typeof e === "object" && !e.jobId) e.jobId = jobId;
|
|
322
|
+
reject(e);
|
|
323
|
+
};
|
|
324
|
+
|
|
325
|
+
const deliverProgress = (events) => {
|
|
326
|
+
for (const ev of events || []) {
|
|
327
|
+
if (!ev || typeof ev.seq !== "number" || ev.seq <= lastSeq) continue;
|
|
328
|
+
lastSeq = ev.seq;
|
|
329
|
+
if (onProgress) {
|
|
330
|
+
try {
|
|
331
|
+
onProgress(ev);
|
|
332
|
+
} catch (_) {}
|
|
333
|
+
}
|
|
334
|
+
}
|
|
335
|
+
};
|
|
336
|
+
|
|
337
|
+
const schedulePoll = (delayMs) => {
|
|
338
|
+
if (settled) return;
|
|
339
|
+
if (pollTimer) clearTimeout(pollTimer);
|
|
340
|
+
const delay = Number.isFinite(delayMs)
|
|
341
|
+
? delayMs
|
|
342
|
+
: eventSeen
|
|
343
|
+
? JOB_POLL_WITH_EVENTS_MS
|
|
344
|
+
: JOB_POLL_NO_EVENTS_MS;
|
|
345
|
+
pollTimer = setTimeout(poll, delay);
|
|
346
|
+
};
|
|
347
|
+
|
|
348
|
+
const poll = async () => {
|
|
349
|
+
if (settled || pollInFlight || !jobId) return;
|
|
350
|
+
pollInFlight = true;
|
|
351
|
+
try {
|
|
352
|
+
const snap = await rt.call("tools", "getJob", {
|
|
353
|
+
jobId,
|
|
354
|
+
sinceSeq: lastSeq,
|
|
355
|
+
});
|
|
356
|
+
deliverProgress(snap.progress);
|
|
357
|
+
if (JOB_TERMINAL_STATES.has(snap.state)) {
|
|
358
|
+
if (snap.state === "succeeded") settleOk(snap.result ?? {});
|
|
359
|
+
else settleErr(jobErrorFromSnapshot(snap));
|
|
360
|
+
return;
|
|
361
|
+
}
|
|
362
|
+
} catch (e) {
|
|
363
|
+
// job_not_found after creation = retention raced us — fatal;
|
|
364
|
+
// transient RPC errors just wait for the next cycle.
|
|
365
|
+
if (e && e.code === "job_not_found") {
|
|
366
|
+
settleErr(e);
|
|
367
|
+
return;
|
|
368
|
+
}
|
|
369
|
+
} finally {
|
|
370
|
+
pollInFlight = false;
|
|
371
|
+
}
|
|
372
|
+
schedulePoll();
|
|
373
|
+
};
|
|
374
|
+
|
|
375
|
+
const onJobEvent = (payload) => {
|
|
376
|
+
if (settled || !payload || payload.job_id !== jobId) return;
|
|
377
|
+
eventSeen = true;
|
|
378
|
+
if (
|
|
379
|
+
payload.event &&
|
|
380
|
+
typeof payload.seq === "number" &&
|
|
381
|
+
payload.seq > lastSeq
|
|
382
|
+
) {
|
|
383
|
+
deliverProgress([
|
|
384
|
+
{ seq: payload.seq, ts: null, type: payload.event.type, data: payload.event.data || {} },
|
|
385
|
+
]);
|
|
386
|
+
}
|
|
387
|
+
// Terminal push → fetch the authoritative snapshot immediately
|
|
388
|
+
// (the event never carries the result); otherwise just reset the
|
|
389
|
+
// poll backoff — events are flowing, polling is a safety net.
|
|
390
|
+
schedulePoll(payload.terminal ? 0 : JOB_POLL_WITH_EVENTS_MS);
|
|
391
|
+
};
|
|
392
|
+
|
|
393
|
+
const start = async () => {
|
|
394
|
+
if (signal && signal.aborted) {
|
|
395
|
+
const err = new Error("invokeAsyncAwait aborted before start");
|
|
396
|
+
err.code = "cancelled";
|
|
397
|
+
settleErr(err);
|
|
398
|
+
return;
|
|
399
|
+
}
|
|
400
|
+
let created;
|
|
401
|
+
try {
|
|
402
|
+
created = await rt.call("tools", "invokeAsync", args || {});
|
|
403
|
+
} catch (e) {
|
|
404
|
+
settleErr(e);
|
|
405
|
+
return;
|
|
406
|
+
}
|
|
407
|
+
jobId = created.jobId;
|
|
408
|
+
const wallMs = Number.isFinite(opts.timeoutMs)
|
|
409
|
+
? opts.timeoutMs
|
|
410
|
+
: Math.max(0, created.deadlineMs - Date.now()) + 30000;
|
|
411
|
+
wallTimer = setTimeout(() => {
|
|
412
|
+
const err = new Error(
|
|
413
|
+
`invokeAsyncAwait wall clock elapsed after ${wallMs}ms; ` +
|
|
414
|
+
`job ${jobId} may still be running — recover via ` +
|
|
415
|
+
`anna.tools.getJob({jobId})`
|
|
416
|
+
);
|
|
417
|
+
err.code = "wait_timeout";
|
|
418
|
+
settleErr(err);
|
|
419
|
+
}, wallMs);
|
|
420
|
+
offEvent = rt.on("tool_job", onJobEvent);
|
|
421
|
+
if (signal) {
|
|
422
|
+
onAbort = () => {
|
|
423
|
+
// Fire-and-forget cancel; the job transitions to cancelled and
|
|
424
|
+
// the terminal event/poll settles the promise with .code
|
|
425
|
+
// "cancelled". Errors here are non-fatal (job may already be
|
|
426
|
+
// terminal — cancelJob is idempotent).
|
|
427
|
+
rt.call("tools", "cancelJob", {
|
|
428
|
+
jobId,
|
|
429
|
+
reason: "aborted by app (AbortSignal)",
|
|
430
|
+
}).catch(() => {});
|
|
431
|
+
schedulePoll(0);
|
|
432
|
+
};
|
|
433
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
434
|
+
}
|
|
435
|
+
schedulePoll(JOB_POLL_NO_EVENTS_MS);
|
|
436
|
+
};
|
|
437
|
+
|
|
438
|
+
start();
|
|
439
|
+
});
|
|
440
|
+
}
|
|
441
|
+
|
|
218
442
|
function buildToolsNamespace(rt) {
|
|
219
443
|
return new Proxy(
|
|
220
444
|
{},
|
|
@@ -224,6 +448,15 @@
|
|
|
224
448
|
if (method === "onChanged") {
|
|
225
449
|
return (fn) => rt.on("tools.changed", fn);
|
|
226
450
|
}
|
|
451
|
+
if (method === "onJobEvent") {
|
|
452
|
+
// Low-level escape hatch: raw `tool_job` host events
|
|
453
|
+
// ({job_id, tool_id, state, seq, event, terminal}). Most apps
|
|
454
|
+
// should prefer invokeAsyncAwait / getJob.
|
|
455
|
+
return (fn) => rt.on("tool_job", fn);
|
|
456
|
+
}
|
|
457
|
+
if (method === "invokeAsyncAwait") {
|
|
458
|
+
return (args, opts) => invokeAsyncAwaitImpl(rt, args, opts || {});
|
|
459
|
+
}
|
|
227
460
|
return (args, opts) => rt.call("tools", method, args || {}, opts);
|
|
228
461
|
},
|
|
229
462
|
}
|
|
@@ -265,7 +498,9 @@
|
|
|
265
498
|
// lives only here, not on the per-session AgentSession wrapper. `refresh`
|
|
266
499
|
// is identity-authed (re-mints token + slides idle window by uuid) and is
|
|
267
500
|
// also exposed as `.refresh()` sugar on the AgentSession handle.
|
|
268
|
-
|
|
501
|
+
// `catalog` is the pre-create discovery surface (platform-tool universe
|
|
502
|
+
// + per-app eligibility) — read-only, no session required.
|
|
503
|
+
for (const m of ["create", "run", "cancel", "history", "delete", "list", "refresh", "catalog"]) {
|
|
269
504
|
ns.session[m] = sessionRpcs[m];
|
|
270
505
|
}
|
|
271
506
|
// `attach` re-wraps an EXISTING session (known uuid) as an AgentSession
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anna-ai/app-runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.15.0",
|
|
4
4
|
"description": "Anna App browser runtime SDK. Loaded by every Anna App iframe; provides typed proxy over the postMessage RPC bridge to the host (Anna dashboard / `anna-app dev` harness).",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"type": "module",
|