@anna-ai/app-runtime 0.18.0 → 0.20.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/README.md +32 -0
- package/dist/index.js +237 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -100,3 +100,35 @@ procedure.
|
|
|
100
100
|
Single file `dist/index.js`. Native ES module (ends in
|
|
101
101
|
`export { AnnaAppRuntime, ... }`), no build step, runs directly in any modern
|
|
102
102
|
browser via `<script type="module">` / `import`.
|
|
103
|
+
|
|
104
|
+
## Media generation (v0.20.0)
|
|
105
|
+
|
|
106
|
+
Async video jobs + sync audio, gated by the per-app `media_grant`
|
|
107
|
+
(manifest must declare `ui.host_api.video` / `.audio`):
|
|
108
|
+
|
|
109
|
+
```js
|
|
110
|
+
// One-shot sugar: submit + media_job events + getJob polling fallback
|
|
111
|
+
const job = await anna.video.generateAwait(
|
|
112
|
+
{ prompt: "Aerial drone shot over a misty pine forest", durationSec: 5,
|
|
113
|
+
resolution: "768p", clientTag: "scene-3-broll" },
|
|
114
|
+
{ onProgress: (p) => bar.update(p), signal: abortController.signal }
|
|
115
|
+
);
|
|
116
|
+
video.src = job.result.url; // presigned GET, ~30 min TTL
|
|
117
|
+
|
|
118
|
+
// URL expired (403)? Re-sign via getJob:
|
|
119
|
+
const fresh = await anna.video.getJob({ jobId: job.jobId });
|
|
120
|
+
|
|
121
|
+
// Raw job API: generate / getJob / cancelJob / listJobs
|
|
122
|
+
const { jobId, estimatedCostCU } = await anna.video.generate({ prompt: "..." });
|
|
123
|
+
|
|
124
|
+
// Sync TTS / STT
|
|
125
|
+
const speech = await anna.audio.speak({ text: "Welcome!", voice: "Cherry" });
|
|
126
|
+
const { text } = await anna.audio.transcribe({ audioUrl: "https://..." });
|
|
127
|
+
|
|
128
|
+
// Capability discovery: models, parameter space, CU pricing, remaining quota
|
|
129
|
+
const catalog = await anna.llm.catalog({ serviceType: "video_gen" });
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
Costs are **pre-charged** from the estimate shown in `estimatedCostCU` and
|
|
133
|
+
fully refunded on failure/cancel/expiry. Aborting `generateAwait` cancels
|
|
134
|
+
the job (queued = full refund; running cancel is best-effort).
|
package/dist/index.js
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
/**
|
|
2
|
-
* Anna App Runtime SDK — v0.
|
|
2
|
+
* Anna App Runtime SDK — v0.20.0
|
|
3
|
+
*
|
|
4
|
+
* v0.20.0 — Media generation surface (design:
|
|
5
|
+
* platform-media-generation-video-audio.md §7/§9/§10).
|
|
6
|
+
* Adds `anna.video.{generate,getJob,cancelJob,listJobs}` (async video
|
|
7
|
+
* jobs; `generate` returns `{jobId, state, estimatedCostCU, deadlineAt}`),
|
|
8
|
+
* `anna.video.generateAwait(args, {onProgress, signal, timeoutMs})` —
|
|
9
|
+
* submit + `media_job` host-event subscription + adaptive `getJob`
|
|
10
|
+
* polling fallback, mirroring `tools.invokeAsyncAwait`; resolves with the
|
|
11
|
+
* terminal job view (`result.url` = presigned GET, ~30 min TTL — re-sign
|
|
12
|
+
* by calling `getJob` again on 403). AbortSignal → `video.cancelJob`
|
|
13
|
+
* (queued = full refund; running cancel is best-effort).
|
|
14
|
+
* Adds `anna.audio.{speak,transcribe}` (sync TTS ≤ model char cap / STT
|
|
15
|
+
* ≤10 min) and `anna.llm.catalog({serviceType?})` capability discovery
|
|
16
|
+
* (models + CU pricing + remaining quota). All gated by the per-app
|
|
17
|
+
* `media_grant`; manifest must declare `ui.host_api.video` / `.audio`.
|
|
18
|
+
* Coordinated versions: dispatcher_version 0.26.0 + schema 0.26.0.
|
|
19
|
+
* Additive: old hosts reject the new namespaces with `unknown_method`.
|
|
3
20
|
*
|
|
4
21
|
* v0.18.0 — Run-recovery pull surface (forum #341 P3: push pairs with pull).
|
|
5
22
|
* Adds `anna.agent.session.runStatus({app_session_uuid, run_id})` →
|
|
@@ -220,6 +237,9 @@
|
|
|
220
237
|
* });
|
|
221
238
|
* await anna.window.set_title({ title: "My App" });
|
|
222
239
|
* anna.on("entry_payload", (p) => console.log(p));
|
|
240
|
+
* // Host pushes `entry_payload` when a single-instance view is re-invoked
|
|
241
|
+
* // with a fresh payload (forum #376); `p` is the full merged payload and
|
|
242
|
+
* // `anna.entryPayload` is refreshed automatically (SDK >= 0.19.0).
|
|
223
243
|
*/
|
|
224
244
|
|
|
225
245
|
// ---- Internals ----
|
|
@@ -522,6 +542,194 @@
|
|
|
522
542
|
);
|
|
523
543
|
}
|
|
524
544
|
|
|
545
|
+
// ---- Video namespace (v0.20.0) ----
|
|
546
|
+
//
|
|
547
|
+
// `anna.video.generateAwait(args, {onProgress, signal, timeoutMs})` =
|
|
548
|
+
// video.generate + `media_job` host-event subscription + adaptive
|
|
549
|
+
// video.getJob polling fallback — the media twin of
|
|
550
|
+
// `tools.invokeAsyncAwait`. Resolves with the terminal job view
|
|
551
|
+
// ({jobId, state:"succeeded", result:{url, mimeType, durationSec, ...},
|
|
552
|
+
// billedCostCU}); `result.url` is a ~30 min presigned GET — call
|
|
553
|
+
// `anna.video.getJob({jobId})` again to re-sign after expiry.
|
|
554
|
+
|
|
555
|
+
function mediaJobErrorFromView(view) {
|
|
556
|
+
const e = view.error || {};
|
|
557
|
+
const code =
|
|
558
|
+
e.code ||
|
|
559
|
+
(view.state === "expired"
|
|
560
|
+
? "video_timeout"
|
|
561
|
+
: view.state === "cancelled"
|
|
562
|
+
? "cancelled"
|
|
563
|
+
: "video_failed");
|
|
564
|
+
const err = new Error(
|
|
565
|
+
e.message || `media job ${view.jobId} ended as ${view.state}`
|
|
566
|
+
);
|
|
567
|
+
err.code = code;
|
|
568
|
+
err.jobId = view.jobId;
|
|
569
|
+
err.state = view.state;
|
|
570
|
+
return err;
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
function videoGenerateAwaitImpl(rt, args, opts = {}) {
|
|
574
|
+
const onProgress =
|
|
575
|
+
typeof opts.onProgress === "function" ? opts.onProgress : null;
|
|
576
|
+
const signal = opts.signal || null;
|
|
577
|
+
|
|
578
|
+
return new Promise((resolve, reject) => {
|
|
579
|
+
let settled = false;
|
|
580
|
+
let jobId = null;
|
|
581
|
+
let lastSeq = 0;
|
|
582
|
+
let eventSeen = false;
|
|
583
|
+
let pollTimer = null;
|
|
584
|
+
let wallTimer = null;
|
|
585
|
+
let pollInFlight = false;
|
|
586
|
+
let offEvent = null;
|
|
587
|
+
let onAbort = null;
|
|
588
|
+
|
|
589
|
+
const cleanup = () => {
|
|
590
|
+
if (pollTimer) clearTimeout(pollTimer);
|
|
591
|
+
if (wallTimer) clearTimeout(wallTimer);
|
|
592
|
+
if (offEvent) offEvent();
|
|
593
|
+
if (signal && onAbort) signal.removeEventListener("abort", onAbort);
|
|
594
|
+
};
|
|
595
|
+
const settleOk = (v) => {
|
|
596
|
+
if (settled) return;
|
|
597
|
+
settled = true;
|
|
598
|
+
cleanup();
|
|
599
|
+
resolve(v);
|
|
600
|
+
};
|
|
601
|
+
const settleErr = (e) => {
|
|
602
|
+
if (settled) return;
|
|
603
|
+
settled = true;
|
|
604
|
+
cleanup();
|
|
605
|
+
if (jobId && e && typeof e === "object" && !e.jobId) e.jobId = jobId;
|
|
606
|
+
reject(e);
|
|
607
|
+
};
|
|
608
|
+
|
|
609
|
+
const deliverProgress = (seq, progress, state) => {
|
|
610
|
+
if (typeof seq !== "number" || seq <= lastSeq) return;
|
|
611
|
+
lastSeq = seq;
|
|
612
|
+
if (onProgress) {
|
|
613
|
+
try {
|
|
614
|
+
onProgress({ seq, state: state || null, ...(progress || {}) });
|
|
615
|
+
} catch (_) {}
|
|
616
|
+
}
|
|
617
|
+
};
|
|
618
|
+
|
|
619
|
+
const schedulePoll = (delayMs) => {
|
|
620
|
+
if (settled) return;
|
|
621
|
+
if (pollTimer) clearTimeout(pollTimer);
|
|
622
|
+
const delay = Number.isFinite(delayMs)
|
|
623
|
+
? delayMs
|
|
624
|
+
: eventSeen
|
|
625
|
+
? JOB_POLL_WITH_EVENTS_MS
|
|
626
|
+
: JOB_POLL_NO_EVENTS_MS;
|
|
627
|
+
pollTimer = setTimeout(poll, delay);
|
|
628
|
+
};
|
|
629
|
+
|
|
630
|
+
const poll = async () => {
|
|
631
|
+
if (settled || pollInFlight || !jobId) return;
|
|
632
|
+
pollInFlight = true;
|
|
633
|
+
try {
|
|
634
|
+
const view = await rt.call("video", "getJob", { jobId });
|
|
635
|
+
if (view.progress) {
|
|
636
|
+
deliverProgress(view.progress.seq, view.progress, view.state);
|
|
637
|
+
}
|
|
638
|
+
if (JOB_TERMINAL_STATES.has(view.state)) {
|
|
639
|
+
if (view.state === "succeeded") settleOk(view);
|
|
640
|
+
else settleErr(mediaJobErrorFromView(view));
|
|
641
|
+
return;
|
|
642
|
+
}
|
|
643
|
+
} catch (e) {
|
|
644
|
+
if (e && (e.code === "VIDEO_JOB_NOT_FOUND" || e.code === "job_not_found")) {
|
|
645
|
+
settleErr(e);
|
|
646
|
+
return;
|
|
647
|
+
}
|
|
648
|
+
// transient RPC errors: wait for the next cycle
|
|
649
|
+
} finally {
|
|
650
|
+
pollInFlight = false;
|
|
651
|
+
}
|
|
652
|
+
schedulePoll();
|
|
653
|
+
};
|
|
654
|
+
|
|
655
|
+
const onJobEvent = (payload) => {
|
|
656
|
+
if (settled || !payload || payload.jobId !== jobId) return;
|
|
657
|
+
eventSeen = true;
|
|
658
|
+
deliverProgress(payload.seq, payload.progress, payload.state);
|
|
659
|
+
// Terminal push → fetch the authoritative view immediately (the
|
|
660
|
+
// forwarded event never carries the result / re-signed URL).
|
|
661
|
+
schedulePoll(payload.terminal ? 0 : JOB_POLL_WITH_EVENTS_MS);
|
|
662
|
+
};
|
|
663
|
+
|
|
664
|
+
const start = async () => {
|
|
665
|
+
if (signal && signal.aborted) {
|
|
666
|
+
const err = new Error("video.generateAwait aborted before start");
|
|
667
|
+
err.code = "cancelled";
|
|
668
|
+
settleErr(err);
|
|
669
|
+
return;
|
|
670
|
+
}
|
|
671
|
+
let created;
|
|
672
|
+
try {
|
|
673
|
+
created = await rt.call("video", "generate", args || {});
|
|
674
|
+
} catch (e) {
|
|
675
|
+
settleErr(e);
|
|
676
|
+
return;
|
|
677
|
+
}
|
|
678
|
+
jobId = created.jobId;
|
|
679
|
+
const deadlineMs = created.deadlineAt
|
|
680
|
+
? Date.parse(created.deadlineAt)
|
|
681
|
+
: NaN;
|
|
682
|
+
const wallMs = Number.isFinite(opts.timeoutMs)
|
|
683
|
+
? opts.timeoutMs
|
|
684
|
+
: Number.isFinite(deadlineMs)
|
|
685
|
+
? Math.max(0, deadlineMs - Date.now()) + 30000
|
|
686
|
+
: 35 * 60 * 1000;
|
|
687
|
+
wallTimer = setTimeout(() => {
|
|
688
|
+
const err = new Error(
|
|
689
|
+
`video.generateAwait wall clock elapsed after ${wallMs}ms; ` +
|
|
690
|
+
`job ${jobId} may still be rendering — recover it with ` +
|
|
691
|
+
`anna.video.getJob({jobId})`
|
|
692
|
+
);
|
|
693
|
+
err.code = "wait_timeout";
|
|
694
|
+
settleErr(err);
|
|
695
|
+
}, wallMs);
|
|
696
|
+
offEvent = rt.on("media_job", onJobEvent);
|
|
697
|
+
if (signal) {
|
|
698
|
+
onAbort = () => {
|
|
699
|
+
// queued cancel = full refund; running cancel is best-effort
|
|
700
|
+
// (provider may refuse — the job then runs to terminal).
|
|
701
|
+
rt.call("video", "cancelJob", { jobId }).catch(() => {});
|
|
702
|
+
schedulePoll(0);
|
|
703
|
+
};
|
|
704
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
705
|
+
}
|
|
706
|
+
schedulePoll(JOB_POLL_NO_EVENTS_MS);
|
|
707
|
+
};
|
|
708
|
+
|
|
709
|
+
start();
|
|
710
|
+
});
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
function buildVideoNamespace(rt) {
|
|
714
|
+
return new Proxy(
|
|
715
|
+
{},
|
|
716
|
+
{
|
|
717
|
+
get(_, method) {
|
|
718
|
+
if (typeof method !== "string") return undefined;
|
|
719
|
+
if (method === "onJobEvent") {
|
|
720
|
+
// Raw `media_job` host events ({jobId, state, seq, progress,
|
|
721
|
+
// error, terminal}). Most apps should prefer generateAwait.
|
|
722
|
+
return (fn) => rt.on("media_job", fn);
|
|
723
|
+
}
|
|
724
|
+
if (method === "generateAwait") {
|
|
725
|
+
return (args, opts) => videoGenerateAwaitImpl(rt, args, opts || {});
|
|
726
|
+
}
|
|
727
|
+
return (args, opts) => rt.call("video", method, args || {}, opts);
|
|
728
|
+
},
|
|
729
|
+
}
|
|
730
|
+
);
|
|
731
|
+
}
|
|
732
|
+
|
|
525
733
|
// ---- Agent namespace (v0.2.0) ----
|
|
526
734
|
//
|
|
527
735
|
// Exposes both raw RPCs (`anna.agent.session.create({submode, ...})`,
|
|
@@ -988,6 +1196,7 @@
|
|
|
988
1196
|
this.capabilities = null;
|
|
989
1197
|
this.viewMeta = null;
|
|
990
1198
|
this.entryPayload = null;
|
|
1199
|
+
this.app = null;
|
|
991
1200
|
this.runtimeState = null;
|
|
992
1201
|
this.geometry = null;
|
|
993
1202
|
|
|
@@ -1016,6 +1225,19 @@
|
|
|
1016
1225
|
this.image = buildNamespace(this, "image");
|
|
1017
1226
|
this.upload = buildNamespace(this, "upload");
|
|
1018
1227
|
|
|
1228
|
+
// ---- video / audio namespaces (v0.20.0; media generation) ----
|
|
1229
|
+
// anna.video.generate({prompt, imageUrl?, durationSec?, resolution?,
|
|
1230
|
+
// aspectRatio?, generateAudio?, model?, clientTag?}) → {jobId, ...}
|
|
1231
|
+
// anna.video.{getJob,cancelJob,listJobs}({jobId|filters})
|
|
1232
|
+
// anna.video.generateAwait(args, {onProgress, signal, timeoutMs})
|
|
1233
|
+
// anna.audio.speak({text, voice?, language?, format?, speed?,
|
|
1234
|
+
// delivery?}) → {url|audioBase64, mimeType, charCount, billedCostCU}
|
|
1235
|
+
// anna.audio.transcribe({audioUrl|audioBase64, language?}) → {text, ...}
|
|
1236
|
+
// Capability discovery: anna.llm.catalog({serviceType?}).
|
|
1237
|
+
// Requires manifest ui.host_api.video / .audio + per-app media_grant.
|
|
1238
|
+
this.video = buildVideoNamespace(this);
|
|
1239
|
+
this.audio = buildNamespace(this, "audio");
|
|
1240
|
+
|
|
1019
1241
|
// ---- credentials namespace (v0.11.0; multi-account) ----
|
|
1020
1242
|
// anna.credentials.list_accounts({provider}) — metadata only, no tokens
|
|
1021
1243
|
// anna.credentials.get_token({provider, account_id?}) — short-lived
|
|
@@ -1134,6 +1356,16 @@
|
|
|
1134
1356
|
this.token = msg.token;
|
|
1135
1357
|
return;
|
|
1136
1358
|
}
|
|
1359
|
+
// forum #376: keep the runtime mirrors fresh before dispatching so
|
|
1360
|
+
// handlers (and later reads) observe the updated values.
|
|
1361
|
+
if (msg.event === "entry_payload") {
|
|
1362
|
+
this.entryPayload = msg.payload || {};
|
|
1363
|
+
} else if (msg.event === "runtime_state_synced") {
|
|
1364
|
+
const patch = msg.payload && msg.payload.patch;
|
|
1365
|
+
if (patch) {
|
|
1366
|
+
this.runtimeState = Object.assign({}, this.runtimeState, patch);
|
|
1367
|
+
}
|
|
1368
|
+
}
|
|
1137
1369
|
const set = this._handlers.get(msg.event);
|
|
1138
1370
|
if (set) for (const fn of set) try { fn(msg.payload); } catch (_) {}
|
|
1139
1371
|
}
|
|
@@ -1228,6 +1460,10 @@
|
|
|
1228
1460
|
const hello = await rt.call("window", "hello", {});
|
|
1229
1461
|
rt.capabilities = hello.capabilities;
|
|
1230
1462
|
rt.viewMeta = hello.view_meta;
|
|
1463
|
+
// forum #376 DX: the app's own identity — `{id, slug, name}`. `id` is
|
|
1464
|
+
// the numeric AnnaApp.id used by `open_app_view`; null on hosts older
|
|
1465
|
+
// than dispatcher 0.25.0.
|
|
1466
|
+
rt.app = hello.app || null;
|
|
1231
1467
|
rt.entryPayload = hello.entry_payload || {};
|
|
1232
1468
|
rt.runtimeState = hello.runtime_state || {};
|
|
1233
1469
|
rt.geometry = hello.geometry || {};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anna-ai/app-runtime",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.20.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",
|