@anna-ai/app-runtime 0.16.1 → 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 +104 -20
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,5 +1,20 @@
1
1
  /**
2
- * Anna App Runtime SDK — v0.16.1
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.
3
18
  *
4
19
  * v0.16.1 — invokeAsyncAwait's wait_timeout message no longer embeds a
5
20
  * call-shaped literal (`anna.tools.getJob({jobId})`): naïve source
@@ -553,6 +568,85 @@
553
568
  // `stream(req)` takes the SAME request shape as `complete(req)`
554
569
  // (`{messages, maxTokens?, temperature?, systemPrompt?, stopSequences?,
555
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
+
556
650
  function buildLlmNamespace(rt) {
557
651
  return new Proxy(
558
652
  {},
@@ -560,25 +654,15 @@
560
654
  get(_, method) {
561
655
  if (typeof method !== "string") return undefined;
562
656
  if (method === "stream") {
563
- return (req = {}) => {
564
- const stream = new AgentRunStream(null, req.run_id || null);
565
- rt.call("llm", "stream", req || {}).then(
566
- (res) => {
567
- stream.streamId = res.stream_id || stream.streamId;
568
- stream.runId = res.run_id || stream.runId;
569
- if (stream.streamId)
570
- rt._registerStream(stream.streamId, stream);
571
- // Host returned no stream_id (e.g. cached error) → close.
572
- if (!stream.streamId) stream._emit(null, true);
573
- },
574
- (err) => {
575
- stream._emit(
576
- { event: "error", code: err.code, message: err.message },
577
- true
578
- );
579
- }
580
- );
581
- 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);
582
666
  };
583
667
  }
584
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.1",
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",