@anna-ai/app-runtime 0.17.0 → 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.
Files changed (2) hide show
  1. package/dist/index.js +120 -5
  2. package/package.json +1 -1
package/dist/index.js CHANGED
@@ -1,5 +1,29 @@
1
1
  /**
2
- * Anna App Runtime SDK — v0.17.0
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.
3
27
  *
4
28
  * v0.17.0 — `anna.llm.complete(req)` transparently delivers LONG outputs over
5
29
  * the streaming channel: when `req.maxTokens > 8192` (the platform's sync
@@ -535,9 +559,14 @@
535
559
  // also exposed as `.refresh()` sugar on the AgentSession handle.
536
560
  // `catalog` is the pre-create discovery surface (platform-tool universe
537
561
  // + per-app eligibility) — read-only, no session required.
538
- 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"]) {
539
563
  ns.session[m] = sessionRpcs[m];
540
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);
541
570
  // `attach` re-wraps an EXISTING session (known uuid) as an AgentSession
542
571
  // handle WITHOUT minting a new one — pure client-side, no RPC. Accepts a
543
572
  // bare uuid string or an info object (e.g. a row from `list()`).
@@ -731,8 +760,69 @@
731
760
  this._buffer = new Map(); // seq -> payload (out-of-order holding)
732
761
  this._nextSeq = 1;
733
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
+ }
734
819
  }
735
820
  _push(frame) {
821
+ this._gotFrame = true;
822
+ if (this._pollTimer) {
823
+ clearTimeout(this._pollTimer);
824
+ this._pollTimer = null;
825
+ }
736
826
  const seq = Number(frame.seq || 0);
737
827
  const payload = frame.payload;
738
828
  const done = !!frame.done;
@@ -741,7 +831,7 @@
741
831
  return;
742
832
  }
743
833
  this._emit(payload, done);
744
- this._nextSeq += 1;
834
+ if (seq) this._nextSeq += 1;
745
835
  // Drain consecutive buffered frames.
746
836
  while (this._buffer.has(this._nextSeq)) {
747
837
  const next = this._buffer.get(this._nextSeq);
@@ -752,7 +842,13 @@
752
842
  }
753
843
  _emit(payload, done) {
754
844
  if (payload != null) this._queue.push(payload);
755
- if (done) this._done = true;
845
+ if (done) {
846
+ this._done = true;
847
+ if (this._pollTimer) {
848
+ clearTimeout(this._pollTimer);
849
+ this._pollTimer = null;
850
+ }
851
+ }
756
852
  while (this._waiters.length && (this._queue.length || this._done)) {
757
853
  const w = this._waiters.shift();
758
854
  if (this._queue.length) w({ value: this._queue.shift(), done: false });
@@ -771,6 +867,10 @@
771
867
  },
772
868
  return() {
773
869
  self._done = true;
870
+ if (self._pollTimer) {
871
+ clearTimeout(self._pollTimer);
872
+ self._pollTimer = null;
873
+ }
774
874
  while (self._waiters.length) self._waiters.shift()({ value: undefined, done: true });
775
875
  return Promise.resolve({ value: undefined, done: true });
776
876
  },
@@ -809,9 +909,12 @@
809
909
  info.session_expires_in ?? this.session_expires_in ?? null;
810
910
  }
811
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 || {};
812
915
  const args = Object.assign(
813
916
  { app_session_uuid: this.app_session_uuid },
814
- opts
917
+ runOpts
815
918
  );
816
919
  const stream = new AgentRunStream(null, args.run_id || null);
817
920
  // Kick off the RPC; once the host returns {stream_id, run_id} we
@@ -823,6 +926,9 @@
823
926
  if (stream.streamId) this._rt._registerStream(stream.streamId, stream);
824
927
  // If host returns done immediately (e.g. cached error), close.
825
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);
826
932
  },
827
933
  (err) => {
828
934
  stream._emit({ event: "error", code: err.code, message: err.message }, true);
@@ -836,6 +942,15 @@
836
942
  run_id: runId,
837
943
  });
838
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
+ }
839
954
  history(opts = {}) {
840
955
  return this._rt.call("agent", "session.history", Object.assign(
841
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.17.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",