@anna-ai/app-runtime 0.16.1 → 0.18.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 +223 -24
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,5 +1,44 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Anna App Runtime SDK — v0.
|
|
2
|
+
* Anna App Runtime SDK — v0.18.0
|
|
3
|
+
*
|
|
4
|
+
* v0.18.0 — Run-recovery pull surface (forum #341 P3: push pairs with pull).
|
|
5
|
+
* Adds `anna.agent.session.runStatus({app_session_uuid, run_id})` →
|
|
6
|
+
* `{status: "queued"|"running"|"completed"|"failed"|"cancelled", task_id,
|
|
7
|
+
* stream_id, started_at?, finished_at?, model?, error?}` (wire method
|
|
8
|
+
* `agent/session.run.status`; also `.runStatus(runId)` sugar on the
|
|
9
|
+
* AgentSession handle) and `anna.agent.session.frames({stream_id,
|
|
10
|
+
* after_seq?, limit?})` → `{frames, complete}` (archived rpc.stream
|
|
11
|
+
* replay; rate limit 30/min/user shared with the dashboard backfill
|
|
12
|
+
* endpoint; ~1h archive window). Check `runStatus` BEFORE re-running a
|
|
13
|
+
* silent stream — `completed` means the run executed and was charged;
|
|
14
|
+
* recover its output via `frames` instead of re-running.
|
|
15
|
+
* `AgentSession.run()` additionally arms an automatic first-frame
|
|
16
|
+
* fallback: if NO frame arrives within `opts.firstFrameTimeoutMs`
|
|
17
|
+
* (default 20s, stripped from the wire args) the stream self-recovers by
|
|
18
|
+
* polling `agent.session.frames` (3s→30s backoff, seq-deduped against
|
|
19
|
+
* late realtime frames) until a `done` frame — a dead realtime leg
|
|
20
|
+
* degrades to "late frames", never "permanent silence".
|
|
21
|
+
* Fix: seq=0 status frames (queued / watchdog terminal) no longer advance
|
|
22
|
+
* the reassembly cursor — previously ONE seq=0 frame silently dropped the
|
|
23
|
+
* first worker frame (started) via an off-by-one in `_push`.
|
|
24
|
+
* Coordinated versions: dispatcher_version 0.24.0 + schema 0.24.0 +
|
|
25
|
+
* anna-app-core 0.22.0. Additive: old hosts reject the new methods with
|
|
26
|
+
* `unknown_method`; the fallback then disarms silently.
|
|
27
|
+
*
|
|
28
|
+
* v0.17.0 — `anna.llm.complete(req)` transparently delivers LONG outputs over
|
|
29
|
+
* the streaming channel: when `req.maxTokens > 8192` (the platform's sync
|
|
30
|
+
* output ceiling — nexus `AppLlmLimits.SYNC_OUTPUT_CEILING`) the SDK
|
|
31
|
+
* routes the call through `llm.stream`, accumulates the `model_token`
|
|
32
|
+
* frames, and resolves with the exact `llm.complete` response shape
|
|
33
|
+
* (`{role, content:{type:'text',text}, model, stopReason, usage, _meta}`).
|
|
34
|
+
* Single-call semantics are unchanged — no code change needed in apps.
|
|
35
|
+
* Terminal frames with `_meta.textOmitted` (host slims frames > 48KB) are
|
|
36
|
+
* reassembled from the accumulated token text and verified against
|
|
37
|
+
* `_meta.textLength`. Stream `error` frames reject with the same
|
|
38
|
+
* `.code`-carrying Error shape as a plain RPC failure. Sync-path calls
|
|
39
|
+
* (`maxTokens` ≤ 8192 or omitted) are byte-identical to v0.16.x.
|
|
40
|
+
* Pure client-side sugar — no host/dispatcher change; design:
|
|
41
|
+
* matrix-nexus docs/design/app-llm-output-cap-unification.md Phase F.
|
|
3
42
|
*
|
|
4
43
|
* v0.16.1 — invokeAsyncAwait's wait_timeout message no longer embeds a
|
|
5
44
|
* call-shaped literal (`anna.tools.getJob({jobId})`): naïve source
|
|
@@ -520,9 +559,14 @@
|
|
|
520
559
|
// also exposed as `.refresh()` sugar on the AgentSession handle.
|
|
521
560
|
// `catalog` is the pre-create discovery surface (platform-tool universe
|
|
522
561
|
// + per-app eligibility) — read-only, no session required.
|
|
523
|
-
for (const m of ["create", "run", "cancel", "history", "delete", "list", "refresh", "catalog"]) {
|
|
562
|
+
for (const m of ["create", "run", "cancel", "history", "delete", "list", "refresh", "catalog", "frames"]) {
|
|
524
563
|
ns.session[m] = sessionRpcs[m];
|
|
525
564
|
}
|
|
565
|
+
// Wire method `session.run.status` contains a dot — expose it as the
|
|
566
|
+
// camelCase `runStatus` (the proxy would map `runStatus` →
|
|
567
|
+
// `session.runStatus`, which is not a wire method).
|
|
568
|
+
ns.session.runStatus = (args, opts) =>
|
|
569
|
+
rt.call("agent", "session.run.status", args || {}, opts);
|
|
526
570
|
// `attach` re-wraps an EXISTING session (known uuid) as an AgentSession
|
|
527
571
|
// handle WITHOUT minting a new one — pure client-side, no RPC. Accepts a
|
|
528
572
|
// bare uuid string or an info object (e.g. a row from `list()`).
|
|
@@ -553,6 +597,85 @@
|
|
|
553
597
|
// `stream(req)` takes the SAME request shape as `complete(req)`
|
|
554
598
|
// (`{messages, maxTokens?, temperature?, systemPrompt?, stopSequences?,
|
|
555
599
|
// modelPreferences?, metadata?}`).
|
|
600
|
+
//
|
|
601
|
+
// v0.17.0: `complete(req)` with `maxTokens > SYNC_OUTPUT_CEILING` is
|
|
602
|
+
// transparently delivered over `llm.stream` (the held-HTTP sync channel
|
|
603
|
+
// is edge-capped at 8192 output tokens server-side) and reshaped back to
|
|
604
|
+
// the single-result `complete` contract. See header changelog.
|
|
605
|
+
//
|
|
606
|
+
// ⚠ Locked to nexus src/core/constants.py AppLlmLimits.SYNC_OUTPUT_CEILING
|
|
607
|
+
// (app-llm-output-cap-unification §2.2) — change both together.
|
|
608
|
+
const LLM_SYNC_OUTPUT_CEILING = 8192;
|
|
609
|
+
|
|
610
|
+
function startLlmStream(rt, req) {
|
|
611
|
+
const stream = new AgentRunStream(null, (req && req.run_id) || null);
|
|
612
|
+
rt.call("llm", "stream", req || {}).then(
|
|
613
|
+
(res) => {
|
|
614
|
+
stream.streamId = res.stream_id || stream.streamId;
|
|
615
|
+
stream.runId = res.run_id || stream.runId;
|
|
616
|
+
if (stream.streamId) rt._registerStream(stream.streamId, stream);
|
|
617
|
+
// Host returned no stream_id (e.g. cached error) → close.
|
|
618
|
+
if (!stream.streamId) stream._emit(null, true);
|
|
619
|
+
},
|
|
620
|
+
(err) => {
|
|
621
|
+
stream._emit(
|
|
622
|
+
{ event: "error", code: err.code, message: err.message },
|
|
623
|
+
true
|
|
624
|
+
);
|
|
625
|
+
}
|
|
626
|
+
);
|
|
627
|
+
return stream;
|
|
628
|
+
}
|
|
629
|
+
|
|
630
|
+
async function completeOverStream(rt, req) {
|
|
631
|
+
let text = "";
|
|
632
|
+
for await (const ev of startLlmStream(rt, req)) {
|
|
633
|
+
if (!ev || typeof ev !== "object") continue;
|
|
634
|
+
if (ev.event === "model_token") {
|
|
635
|
+
if (ev.text) text += ev.text;
|
|
636
|
+
} else if (ev.event === "complete") {
|
|
637
|
+
const meta = ev._meta || {};
|
|
638
|
+
// Slimmed terminal frame (>48KB): the accumulated token text is
|
|
639
|
+
// authoritative; textLength (chars) is the integrity check.
|
|
640
|
+
let finalText =
|
|
641
|
+
(ev.content && typeof ev.content.text === "string" && ev.content.text) ||
|
|
642
|
+
text;
|
|
643
|
+
if (meta.textOmitted) {
|
|
644
|
+
finalText = text;
|
|
645
|
+
if (
|
|
646
|
+
typeof meta.textLength === "number" &&
|
|
647
|
+
meta.textLength !== text.length
|
|
648
|
+
) {
|
|
649
|
+
const err = new Error(
|
|
650
|
+
`llm.complete (over stream): accumulated ${text.length} chars ` +
|
|
651
|
+
`but host reported ${meta.textLength} — dropped token frames`
|
|
652
|
+
);
|
|
653
|
+
err.code = "stream_incomplete";
|
|
654
|
+
throw err;
|
|
655
|
+
}
|
|
656
|
+
}
|
|
657
|
+
return {
|
|
658
|
+
role: ev.role || "assistant",
|
|
659
|
+
content: { type: "text", text: finalText },
|
|
660
|
+
model: ev.model,
|
|
661
|
+
stopReason: ev.stopReason,
|
|
662
|
+
usage: ev.usage,
|
|
663
|
+
_meta: meta,
|
|
664
|
+
};
|
|
665
|
+
} else if (ev.event === "error") {
|
|
666
|
+
const err = new Error(ev.message || "llm.stream failed");
|
|
667
|
+
if (ev.code != null) err.code = ev.code;
|
|
668
|
+
throw err;
|
|
669
|
+
}
|
|
670
|
+
// status frames (queued/started/keepalive/end) are transport noise here
|
|
671
|
+
}
|
|
672
|
+
const err = new Error(
|
|
673
|
+
"llm.complete (over stream): stream ended without a terminal complete frame"
|
|
674
|
+
);
|
|
675
|
+
err.code = "stream_closed";
|
|
676
|
+
throw err;
|
|
677
|
+
}
|
|
678
|
+
|
|
556
679
|
function buildLlmNamespace(rt) {
|
|
557
680
|
return new Proxy(
|
|
558
681
|
{},
|
|
@@ -560,25 +683,15 @@
|
|
|
560
683
|
get(_, method) {
|
|
561
684
|
if (typeof method !== "string") return undefined;
|
|
562
685
|
if (method === "stream") {
|
|
563
|
-
return (req = {}) =>
|
|
564
|
-
|
|
565
|
-
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
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;
|
|
686
|
+
return (req = {}) => startLlmStream(rt, req);
|
|
687
|
+
}
|
|
688
|
+
if (method === "complete") {
|
|
689
|
+
return (args, opts) => {
|
|
690
|
+
const requested = Number(args && args.maxTokens);
|
|
691
|
+
if (requested > LLM_SYNC_OUTPUT_CEILING) {
|
|
692
|
+
return completeOverStream(rt, args || {});
|
|
693
|
+
}
|
|
694
|
+
return rt.call("llm", method, args || {}, opts);
|
|
582
695
|
};
|
|
583
696
|
}
|
|
584
697
|
return (args, opts) => rt.call("llm", method, args || {}, opts);
|
|
@@ -647,8 +760,69 @@
|
|
|
647
760
|
this._buffer = new Map(); // seq -> payload (out-of-order holding)
|
|
648
761
|
this._nextSeq = 1;
|
|
649
762
|
this._done = false;
|
|
763
|
+
this._gotFrame = false; // any frame received (cancels the fallback timer)
|
|
764
|
+
this._pollTimer = null; // first-frame fallback (forum #341 P3)
|
|
765
|
+
this._polling = false;
|
|
766
|
+
this._rt = null;
|
|
767
|
+
}
|
|
768
|
+
// First-frame fallback (v0.18.0): if no frame arrives within timeoutMs,
|
|
769
|
+
// poll `agent.session.frames` until done. Layered with the host shells'
|
|
770
|
+
// own gate-level fallback — seq dedup makes double delivery harmless.
|
|
771
|
+
_armPollFallback(rt, timeoutMs) {
|
|
772
|
+
if (this._pollTimer || this._polling || this._done || this._gotFrame) return;
|
|
773
|
+
this._rt = rt;
|
|
774
|
+
const deadline =
|
|
775
|
+
typeof timeoutMs === "number" && timeoutMs > 0 ? timeoutMs : 20000;
|
|
776
|
+
this._pollTimer = setTimeout(() => {
|
|
777
|
+
this._pollTimer = null;
|
|
778
|
+
if (!this._gotFrame && !this._done) this._pollLoop();
|
|
779
|
+
}, deadline);
|
|
780
|
+
}
|
|
781
|
+
async _pollLoop() {
|
|
782
|
+
if (this._polling || !this._rt || !this.streamId) return;
|
|
783
|
+
this._polling = true;
|
|
784
|
+
let interval = 3000;
|
|
785
|
+
try {
|
|
786
|
+
while (!this._done) {
|
|
787
|
+
try {
|
|
788
|
+
const res = await this._rt.call("agent", "session.frames", {
|
|
789
|
+
stream_id: this.streamId,
|
|
790
|
+
after_seq: this._nextSeq - 1,
|
|
791
|
+
limit: 500,
|
|
792
|
+
});
|
|
793
|
+
const frames = (res && Array.isArray(res.frames) ? res.frames : [])
|
|
794
|
+
.filter((f) => f && typeof f.seq === "number" && f.seq >= this._nextSeq)
|
|
795
|
+
.sort((a, b) => a.seq - b.seq);
|
|
796
|
+
for (const f of frames) {
|
|
797
|
+
this._push({ seq: f.seq, payload: f.payload, done: !!f.done });
|
|
798
|
+
if (this._done) return;
|
|
799
|
+
}
|
|
800
|
+
interval = frames.length ? 3000 : Math.min(interval * 2, 30000);
|
|
801
|
+
} catch (e) {
|
|
802
|
+
// Old host (`unknown_method`/`not_implemented`) → disarm; the
|
|
803
|
+
// archive window aged out (`not_found`) → nothing to recover.
|
|
804
|
+
const code = e && e.code;
|
|
805
|
+
if (
|
|
806
|
+
code === "unknown_method" ||
|
|
807
|
+
code === "not_implemented" ||
|
|
808
|
+
code === "not_found"
|
|
809
|
+
) {
|
|
810
|
+
return;
|
|
811
|
+
}
|
|
812
|
+
interval = Math.min(interval * 2, 30000);
|
|
813
|
+
}
|
|
814
|
+
await new Promise((r) => setTimeout(r, interval));
|
|
815
|
+
}
|
|
816
|
+
} finally {
|
|
817
|
+
this._polling = false;
|
|
818
|
+
}
|
|
650
819
|
}
|
|
651
820
|
_push(frame) {
|
|
821
|
+
this._gotFrame = true;
|
|
822
|
+
if (this._pollTimer) {
|
|
823
|
+
clearTimeout(this._pollTimer);
|
|
824
|
+
this._pollTimer = null;
|
|
825
|
+
}
|
|
652
826
|
const seq = Number(frame.seq || 0);
|
|
653
827
|
const payload = frame.payload;
|
|
654
828
|
const done = !!frame.done;
|
|
@@ -657,7 +831,7 @@
|
|
|
657
831
|
return;
|
|
658
832
|
}
|
|
659
833
|
this._emit(payload, done);
|
|
660
|
-
this._nextSeq += 1;
|
|
834
|
+
if (seq) this._nextSeq += 1;
|
|
661
835
|
// Drain consecutive buffered frames.
|
|
662
836
|
while (this._buffer.has(this._nextSeq)) {
|
|
663
837
|
const next = this._buffer.get(this._nextSeq);
|
|
@@ -668,7 +842,13 @@
|
|
|
668
842
|
}
|
|
669
843
|
_emit(payload, done) {
|
|
670
844
|
if (payload != null) this._queue.push(payload);
|
|
671
|
-
if (done)
|
|
845
|
+
if (done) {
|
|
846
|
+
this._done = true;
|
|
847
|
+
if (this._pollTimer) {
|
|
848
|
+
clearTimeout(this._pollTimer);
|
|
849
|
+
this._pollTimer = null;
|
|
850
|
+
}
|
|
851
|
+
}
|
|
672
852
|
while (this._waiters.length && (this._queue.length || this._done)) {
|
|
673
853
|
const w = this._waiters.shift();
|
|
674
854
|
if (this._queue.length) w({ value: this._queue.shift(), done: false });
|
|
@@ -687,6 +867,10 @@
|
|
|
687
867
|
},
|
|
688
868
|
return() {
|
|
689
869
|
self._done = true;
|
|
870
|
+
if (self._pollTimer) {
|
|
871
|
+
clearTimeout(self._pollTimer);
|
|
872
|
+
self._pollTimer = null;
|
|
873
|
+
}
|
|
690
874
|
while (self._waiters.length) self._waiters.shift()({ value: undefined, done: true });
|
|
691
875
|
return Promise.resolve({ value: undefined, done: true });
|
|
692
876
|
},
|
|
@@ -725,9 +909,12 @@
|
|
|
725
909
|
info.session_expires_in ?? this.session_expires_in ?? null;
|
|
726
910
|
}
|
|
727
911
|
run(opts = {}) {
|
|
912
|
+
// `firstFrameTimeoutMs` is SDK-local (first-frame fallback arm delay,
|
|
913
|
+
// v0.18.0) — strip it from the wire args.
|
|
914
|
+
const { firstFrameTimeoutMs, ...runOpts } = opts || {};
|
|
728
915
|
const args = Object.assign(
|
|
729
916
|
{ app_session_uuid: this.app_session_uuid },
|
|
730
|
-
|
|
917
|
+
runOpts
|
|
731
918
|
);
|
|
732
919
|
const stream = new AgentRunStream(null, args.run_id || null);
|
|
733
920
|
// Kick off the RPC; once the host returns {stream_id, run_id} we
|
|
@@ -739,6 +926,9 @@
|
|
|
739
926
|
if (stream.streamId) this._rt._registerStream(stream.streamId, stream);
|
|
740
927
|
// If host returns done immediately (e.g. cached error), close.
|
|
741
928
|
if (!stream.streamId) stream._emit(null, true);
|
|
929
|
+
// forum #341 P3: no first frame within the deadline → the stream
|
|
930
|
+
// self-recovers by polling agent.session.frames until done.
|
|
931
|
+
else stream._armPollFallback(this._rt, firstFrameTimeoutMs);
|
|
742
932
|
},
|
|
743
933
|
(err) => {
|
|
744
934
|
stream._emit({ event: "error", code: err.code, message: err.message }, true);
|
|
@@ -752,6 +942,15 @@
|
|
|
752
942
|
run_id: runId,
|
|
753
943
|
});
|
|
754
944
|
}
|
|
945
|
+
// Point-in-time run status (v0.18.0, forum #341 P3). Check this BEFORE
|
|
946
|
+
// re-running after a silent stream — `completed` means the run executed
|
|
947
|
+
// and was charged; recover its output via `agent.session.frames`.
|
|
948
|
+
runStatus(runId) {
|
|
949
|
+
return this._rt.call("agent", "session.run.status", {
|
|
950
|
+
app_session_uuid: this.app_session_uuid,
|
|
951
|
+
run_id: runId,
|
|
952
|
+
});
|
|
953
|
+
}
|
|
755
954
|
history(opts = {}) {
|
|
756
955
|
return this._rt.call("agent", "session.history", Object.assign(
|
|
757
956
|
{ app_session_uuid: this.app_session_uuid }, opts
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anna-ai/app-runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.18.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",
|