@anna-ai/app-runtime 0.2.0 → 0.5.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.
Files changed (3) hide show
  1. package/README.md +28 -0
  2. package/dist/index.js +109 -18
  3. package/package.json +2 -1
package/README.md CHANGED
@@ -37,6 +37,34 @@ anna.on("entry_payload", (p) => console.log(p));
37
37
  The host validates every call against the app's manifest ACL before
38
38
  forwarding to the backend.
39
39
 
40
+ ## RPC timeouts
41
+
42
+ Each call has a per-namespace default; apps can override per call:
43
+
44
+ ```js
45
+ // Use the namespace default (image: 180s)
46
+ const out = await anna.image.generate({ prompt: "..." });
47
+
48
+ // Override for one call (e.g. high-res / many images)
49
+ const out = await anna.image.generate(
50
+ { prompt: "...", n: 4, size: "2048x2048" },
51
+ { timeoutMs: 240000 }
52
+ );
53
+ ```
54
+
55
+ | Namespace | Default | Rationale |
56
+ | --- | --- | --- |
57
+ | `image.*` | 180 s | Matches host's upstream provider timeout (Seedream / Qwen / native) |
58
+ | `upload.*` | 120 s | Large multipart / negotiate+confirm |
59
+ | `agent.*` | 300 s | `session.run` loops model + tools many turns |
60
+ | `llm.*` | 180 s | Long completions / large context |
61
+ | everything else | 30 s | storage / chat / window / prefs / fs / tools / artifact |
62
+
63
+ The timeout is a *client-side* deadline. The host bridge fetch has no
64
+ abort signal — the backend always runs to completion, but a timed-out
65
+ promise rejects with `Error("RPC <ns>.<method> timed out after Nms")`
66
+ and the eventual response is discarded.
67
+
40
68
  ## Where the script comes from
41
69
 
42
70
  | Environment | Source |
package/dist/index.js CHANGED
@@ -1,15 +1,44 @@
1
1
  /**
2
- * Anna App Runtime SDK — v0.2.0
2
+ * Anna App Runtime SDK — v0.4.1
3
3
  *
4
- * Loaded by every Anna App iframe. Provides a typed proxy over the
5
- * postMessage RPC bridge to the host (Anna dashboard). All host calls go
6
- * through the host page (parent window), which validates them against the
7
- * app's manifest ACL before forwarding to the backend.
4
+ * v0.4.1 — Per-namespace default RPC timeouts and per-call override.
5
+ * image.* / upload.* / agent.* / llm.* used to share the 30s default,
6
+ * but real LLM / image-gen latency easily exceeds that on high-res
7
+ * prompts (Seedream 5.0 @ 2048x2048 can take 60-120s). New defaults:
8
+ * image: 180s upload: 120s agent: 300s llm: 180s
9
+ * storage: 30s chat/window/...: 30s (unchanged)
10
+ * Any call can override via a second arg, e.g.
11
+ * await anna.image.generate(args, { timeoutMs: 240000 })
12
+ *
13
+ * Loaded by every Anna App iframe as a native ES module via
14
+ * `import { AnnaAppRuntime } from "/static/anna-apps/_sdk/latest/index.js"`.
15
+ * Provides a typed proxy over the postMessage RPC bridge to the host
16
+ * (Anna dashboard). All host calls go through the host page (parent
17
+ * window), which validates them against the app's manifest ACL before
18
+ * forwarding to the backend.
19
+ *
20
+ * v0.4.0 — BREAKING: removed the cross-origin HTTP shim and the
21
+ * `AnnaHostError` class. `anna.image.{generate,edit}` and
22
+ * `anna.upload.{inline,negotiate,confirm}` now go through the same
23
+ * postMessage → `/api/v1/anna-apps/runtime/rpc` dispatcher channel as
24
+ * every other namespace (symmetric with `anna.storage.*` /
25
+ * `anna.llm.complete`). The dispatcher mints the underlying
26
+ * `app_session_token` server-side; the iframe never holds capability
27
+ * credentials. Errors arrive as the standard RPC error shape:
28
+ * `Error` with `.code` (e.g. `APP_ERR_NOT_GRANTED`,
29
+ * `APP_ERR_QUOTA_EXCEEDED`, `not_implemented`) and `.details`. Apps
30
+ * that previously branched on `err.status === 403/415/429` must rewrite
31
+ * to branch on `err.code` (see VERSIONS.md changelog for the mapping).
32
+ *
33
+ * v0.3.1 — packaged as pure ESM (no IIFE / window-global). Consumers must
34
+ * use `<script type="module">` + `import`. Legacy `<script src=...>`
35
+ * loading is no longer supported.
8
36
  *
9
37
  * Envelope: { kind:"req"|"res"|"event", id, ns, method, args | result | error }
10
38
  *
11
39
  * Usage inside the iframe:
12
40
  *
41
+ * import { AnnaAppRuntime } from "/static/anna-apps/_sdk/latest/index.js";
13
42
  * const anna = await AnnaAppRuntime.connect();
14
43
  * // Mint-only tool_ids (tool-{handle}-{slug}-{uniq}) carry no separator,
15
44
  * // so the target plugin method is supplied via `method`:
@@ -22,24 +51,43 @@
22
51
  * anna.on("entry_payload", (p) => console.log(p));
23
52
  */
24
53
 
25
- (function (global) {
26
- "use strict";
27
-
28
- // ---- Internals ----
54
+ // ---- Internals ----
29
55
  function uuid() {
30
56
  return (
31
57
  Date.now().toString(36) + "-" + Math.random().toString(36).slice(2, 10)
32
58
  );
33
59
  }
34
60
 
35
- // Build a target proxy that exposes namespace.method(args) calls.
61
+ // Per-namespace default RPC timeout (ms). Picked to comfortably cover
62
+ // upstream provider P99 latency (host bridge fetch is unbounded; backend
63
+ // httpx is 180s for image). Apps can override per-call via the second
64
+ // arg: `anna.image.generate(args, { timeoutMs: 240000 })`.
65
+ const DEFAULT_TIMEOUTS_BY_NS = {
66
+ image: 180000, // image gen/edit; matches src/tools/image_generation_tool.py httpx=180s
67
+ upload: 120000, // large multipart / negotiate+confirm
68
+ agent: 300000, // session.run can loop through model+tools many turns
69
+ llm: 180000, // long completions / large context
70
+ // `tools.invoke` host bound = 65s plugin + 2s grace + 3s SDK margin.
71
+ // Apps requesting longer per call must pass { timeoutMs }; bounded
72
+ // server-side by ANNA_APP_TOOLS_INVOKE_TIMEOUT_MAX_MS.
73
+ tools: 70000,
74
+ // `files.*` covers R2 presign + DB write (upload_init/finalize); the
75
+ // browser→R2 PUT itself is direct (not via this SDK), but the host
76
+ // round-trip for finalize needs DB+R2 head ~ up to a few seconds.
77
+ files: 60000,
78
+ };
79
+ const DEFAULT_TIMEOUT_MS = 30000;
80
+
81
+ // Build a target proxy that exposes namespace.method(args, opts?) calls.
82
+ // The optional second arg accepts `{ timeoutMs }`; missing → namespace
83
+ // default → 30000 fallback.
36
84
  function buildNamespace(rt, ns) {
37
85
  return new Proxy(
38
86
  {},
39
87
  {
40
88
  get(_, method) {
41
89
  if (typeof method !== "string") return undefined;
42
- return (args) => rt.call(ns, method, args || {});
90
+ return (args, opts) => rt.call(ns, method, args || {}, opts);
43
91
  },
44
92
  }
45
93
  );
@@ -62,7 +110,8 @@
62
110
  {
63
111
  get(_, method) {
64
112
  if (typeof method !== "string") return undefined;
65
- return (args) => rt.call("agent", "session." + method, args || {});
113
+ return (args, opts) =>
114
+ rt.call("agent", "session." + method, args || {}, opts);
66
115
  },
67
116
  }
68
117
  );
@@ -81,6 +130,15 @@
81
130
  return ns;
82
131
  }
83
132
 
133
+ // ---- HTTP-backed namespaces removed in v0.4.0 ----
134
+ //
135
+ // `anna.image.*` and `anna.upload.*` are now plain postMessage RPCs
136
+ // (see the dispatcher's `image.*` / `upload.*` handlers in
137
+ // anna-app-core 0.3.0+). The dispatcher mints the underlying
138
+ // app_session_token in-process; the iframe never holds capability
139
+ // credentials. Errors arrive on the standard RPC envelope with
140
+ // `.code` set to the facade's `APP_ERR_*` value.
141
+
84
142
  // AsyncIterable view over `rpc.stream` event chunks. Reassembles by `seq`
85
143
  // so out-of-order arrivals are normalised; back-pressure is bounded only
86
144
  // by the iframe's heap (events are small JSON blobs).
@@ -224,6 +282,24 @@
224
282
  // a higher-level `anna.agent.session(opts)` helper that returns an
225
283
  // AgentSession bound to a fresh AnnaAppSession.
226
284
  this.agent = buildAgentNamespace(this);
285
+
286
+ // ---- image / upload namespaces (v0.4.0; postMessage) ----
287
+ // anna.image.generate({prompt, n?, size?, reference_image_urls?, metadata?})
288
+ // anna.image.edit({image_url, prompt, n?, size?, mask_url?, metadata?})
289
+ // anna.upload.inline({filename, mime_type, content_b64, purpose?, metadata?})
290
+ // anna.upload.negotiate({filename, mime_type, size_bytes, purpose?, metadata?})
291
+ // anna.upload.confirm({r2_key})
292
+ this.image = buildNamespace(this, "image");
293
+ this.upload = buildNamespace(this, "upload");
294
+
295
+ // ---- files namespace (APS object/blob API; v0.5.x) ----
296
+ // anna.files.upload_init({path, content_type, size, ttl_seconds?, metadata?, tags?, if_match?})
297
+ // anna.files.upload_finalize({path, etag, size_bytes, metadata?})
298
+ // anna.files.download_url({path, ttl_seconds?, disposition?})
299
+ // anna.files.list({prefix?, cursor?, limit?})
300
+ // anna.files.delete({path, if_match?})
301
+ this.files = buildNamespace(this, "files");
302
+
227
303
  // Reassembly buffers for `rpc.stream` events. Map<stream_id, AgentRunStream>.
228
304
  this._streams = new Map();
229
305
  this.on("rpc.stream", (payload) => this._onStreamFrame(payload || {}));
@@ -295,13 +371,22 @@
295
371
  return () => set.delete(fn);
296
372
  }
297
373
 
298
- call(ns, method, args, { timeoutMs = 30000 } = {}) {
374
+ call(ns, method, args, opts) {
299
375
  if (this._closed) return Promise.reject(new Error("runtime closed"));
376
+ const timeoutMs =
377
+ (opts && Number.isFinite(opts.timeoutMs) ? opts.timeoutMs : null) ??
378
+ DEFAULT_TIMEOUTS_BY_NS[ns] ??
379
+ DEFAULT_TIMEOUT_MS;
300
380
  const id = uuid();
301
381
  return new Promise((resolve, reject) => {
302
382
  const timer = setTimeout(() => {
303
383
  this._pending.delete(id);
304
- reject(new Error(`RPC ${ns}.${method} timed out`));
384
+ reject(
385
+ new Error(
386
+ `RPC ${ns}.${method} timed out after ${timeoutMs}ms` +
387
+ ` (pass { timeoutMs } as the 2nd arg to override)`
388
+ )
389
+ );
305
390
  }, timeoutMs);
306
391
  this._pending.set(id, {
307
392
  resolve: (v) => { clearTimeout(timer); resolve(v); },
@@ -311,6 +396,8 @@
311
396
  });
312
397
  }
313
398
 
399
+ // (HTTP shim removed in v0.4.0 — image/upload now use postMessage.)
400
+
314
401
  _startHeartbeat() {
315
402
  this._heartbeatTimer = setInterval(() => {
316
403
  if (this._closed) return;
@@ -376,7 +463,11 @@
376
463
  }
377
464
  }
378
465
 
379
- global.AnnaAppRuntime = AnnaAppRuntime;
380
- global.AnnaAppRuntime.AgentSession = AgentSession;
381
- global.AnnaAppRuntime.AgentRunStream = AgentRunStream;
382
- })(typeof window !== "undefined" ? window : globalThis);
466
+ // Convenience static references (mirrors pre-0.3.1 layout where these
467
+ // hung off the AnnaAppRuntime global). `AnnaHostError` was removed in
468
+ // v0.4.0; apps should now branch on the standard RPC error `.code`.
469
+ AnnaAppRuntime.AgentSession = AgentSession;
470
+ AnnaAppRuntime.AgentRunStream = AgentRunStream;
471
+
472
+ export { AnnaAppRuntime, AgentSession, AgentRunStream };
473
+ export default AnnaAppRuntime;
package/package.json CHANGED
@@ -1,8 +1,9 @@
1
1
  {
2
2
  "name": "@anna-ai/app-runtime",
3
- "version": "0.2.0",
3
+ "version": "0.5.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
+ "type": "module",
6
7
  "main": "./dist/index.js",
7
8
  "exports": {
8
9
  ".": {