@anna-ai/app-runtime 0.16.0 → 0.17.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 (2) hide show
  1. package/dist/index.js +111 -22
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,5 +1,25 @@
1
1
  /**
2
- * Anna App Runtime SDK — v0.16.0
2
+ * Anna App Runtime SDK — v0.17.0
3
+ *
4
+ * v0.17.0 — `anna.llm.complete(req)` transparently delivers LONG outputs over
5
+ * the streaming channel: when `req.maxTokens > 8192` (the platform's sync
6
+ * output ceiling — nexus `AppLlmLimits.SYNC_OUTPUT_CEILING`) the SDK
7
+ * routes the call through `llm.stream`, accumulates the `model_token`
8
+ * frames, and resolves with the exact `llm.complete` response shape
9
+ * (`{role, content:{type:'text',text}, model, stopReason, usage, _meta}`).
10
+ * Single-call semantics are unchanged — no code change needed in apps.
11
+ * Terminal frames with `_meta.textOmitted` (host slims frames > 48KB) are
12
+ * reassembled from the accumulated token text and verified against
13
+ * `_meta.textLength`. Stream `error` frames reject with the same
14
+ * `.code`-carrying Error shape as a plain RPC failure. Sync-path calls
15
+ * (`maxTokens` ≤ 8192 or omitted) are byte-identical to v0.16.x.
16
+ * Pure client-side sugar — no host/dispatcher change; design:
17
+ * matrix-nexus docs/design/app-llm-output-cap-unification.md Phase F.
18
+ *
19
+ * v0.16.1 — invokeAsyncAwait's wait_timeout message no longer embeds a
20
+ * call-shaped literal (`anna.tools.getJob({jobId})`): naïve source
21
+ * scanners (grep-class) treated the diagnostic string as a real host
22
+ * API call (forum #296). Wording only — no behavior change.
3
23
  *
4
24
  * v0.16.0 — Adds the `anna.mobile.*` native-bridge namespace (design
5
25
  * anna-app-mobile-runtime-phase3.md P1): `anna.mobile.share({title?,
@@ -426,8 +446,8 @@
426
446
  wallTimer = setTimeout(() => {
427
447
  const err = new Error(
428
448
  `invokeAsyncAwait wall clock elapsed after ${wallMs}ms; ` +
429
- `job ${jobId} may still be running — recover via ` +
430
- `anna.tools.getJob({jobId})`
449
+ `job ${jobId} may still be running — recover it with the ` +
450
+ `tools.getJob host method using this jobId`
431
451
  );
432
452
  err.code = "wait_timeout";
433
453
  settleErr(err);
@@ -548,6 +568,85 @@
548
568
  // `stream(req)` takes the SAME request shape as `complete(req)`
549
569
  // (`{messages, maxTokens?, temperature?, systemPrompt?, stopSequences?,
550
570
  // modelPreferences?, metadata?}`).
571
+ //
572
+ // v0.17.0: `complete(req)` with `maxTokens > SYNC_OUTPUT_CEILING` is
573
+ // transparently delivered over `llm.stream` (the held-HTTP sync channel
574
+ // is edge-capped at 8192 output tokens server-side) and reshaped back to
575
+ // the single-result `complete` contract. See header changelog.
576
+ //
577
+ // ⚠ Locked to nexus src/core/constants.py AppLlmLimits.SYNC_OUTPUT_CEILING
578
+ // (app-llm-output-cap-unification §2.2) — change both together.
579
+ const LLM_SYNC_OUTPUT_CEILING = 8192;
580
+
581
+ function startLlmStream(rt, req) {
582
+ const stream = new AgentRunStream(null, (req && req.run_id) || null);
583
+ rt.call("llm", "stream", req || {}).then(
584
+ (res) => {
585
+ stream.streamId = res.stream_id || stream.streamId;
586
+ stream.runId = res.run_id || stream.runId;
587
+ if (stream.streamId) rt._registerStream(stream.streamId, stream);
588
+ // Host returned no stream_id (e.g. cached error) → close.
589
+ if (!stream.streamId) stream._emit(null, true);
590
+ },
591
+ (err) => {
592
+ stream._emit(
593
+ { event: "error", code: err.code, message: err.message },
594
+ true
595
+ );
596
+ }
597
+ );
598
+ return stream;
599
+ }
600
+
601
+ async function completeOverStream(rt, req) {
602
+ let text = "";
603
+ for await (const ev of startLlmStream(rt, req)) {
604
+ if (!ev || typeof ev !== "object") continue;
605
+ if (ev.event === "model_token") {
606
+ if (ev.text) text += ev.text;
607
+ } else if (ev.event === "complete") {
608
+ const meta = ev._meta || {};
609
+ // Slimmed terminal frame (>48KB): the accumulated token text is
610
+ // authoritative; textLength (chars) is the integrity check.
611
+ let finalText =
612
+ (ev.content && typeof ev.content.text === "string" && ev.content.text) ||
613
+ text;
614
+ if (meta.textOmitted) {
615
+ finalText = text;
616
+ if (
617
+ typeof meta.textLength === "number" &&
618
+ meta.textLength !== text.length
619
+ ) {
620
+ const err = new Error(
621
+ `llm.complete (over stream): accumulated ${text.length} chars ` +
622
+ `but host reported ${meta.textLength} — dropped token frames`
623
+ );
624
+ err.code = "stream_incomplete";
625
+ throw err;
626
+ }
627
+ }
628
+ return {
629
+ role: ev.role || "assistant",
630
+ content: { type: "text", text: finalText },
631
+ model: ev.model,
632
+ stopReason: ev.stopReason,
633
+ usage: ev.usage,
634
+ _meta: meta,
635
+ };
636
+ } else if (ev.event === "error") {
637
+ const err = new Error(ev.message || "llm.stream failed");
638
+ if (ev.code != null) err.code = ev.code;
639
+ throw err;
640
+ }
641
+ // status frames (queued/started/keepalive/end) are transport noise here
642
+ }
643
+ const err = new Error(
644
+ "llm.complete (over stream): stream ended without a terminal complete frame"
645
+ );
646
+ err.code = "stream_closed";
647
+ throw err;
648
+ }
649
+
551
650
  function buildLlmNamespace(rt) {
552
651
  return new Proxy(
553
652
  {},
@@ -555,25 +654,15 @@
555
654
  get(_, method) {
556
655
  if (typeof method !== "string") return undefined;
557
656
  if (method === "stream") {
558
- return (req = {}) => {
559
- const stream = new AgentRunStream(null, req.run_id || null);
560
- rt.call("llm", "stream", req || {}).then(
561
- (res) => {
562
- stream.streamId = res.stream_id || stream.streamId;
563
- stream.runId = res.run_id || stream.runId;
564
- if (stream.streamId)
565
- rt._registerStream(stream.streamId, stream);
566
- // Host returned no stream_id (e.g. cached error) → close.
567
- if (!stream.streamId) stream._emit(null, true);
568
- },
569
- (err) => {
570
- stream._emit(
571
- { event: "error", code: err.code, message: err.message },
572
- true
573
- );
574
- }
575
- );
576
- return stream;
657
+ return (req = {}) => startLlmStream(rt, req);
658
+ }
659
+ if (method === "complete") {
660
+ return (args, opts) => {
661
+ const requested = Number(args && args.maxTokens);
662
+ if (requested > LLM_SYNC_OUTPUT_CEILING) {
663
+ return completeOverStream(rt, args || {});
664
+ }
665
+ return rt.call("llm", method, args || {}, opts);
577
666
  };
578
667
  }
579
668
  return (args, opts) => rt.call("llm", method, args || {}, opts);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anna-ai/app-runtime",
3
- "version": "0.16.0",
3
+ "version": "0.17.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",