@aippy/runtime 0.4.2-dev.7 → 0.4.2-dev.8

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 CHANGED
@@ -127,7 +127,7 @@ const image = await generateImage({
127
127
  console.log(image.image?.base64);
128
128
  ```
129
129
 
130
- `@aippy/runtime/ai` also exports staged Viggle generation/recovery and
130
+ `@aippy/runtime/ai` also exports internal-test Viggle generation with automatic recovery and
131
131
  `listViggleModels` for the current user's project history. Project ticket keys
132
132
  are resolved by the backend; applications do not settle tickets or pass SKU
133
133
  constants. Availability depends on the matching backend/shared MiniStore
package/dist/ai/index.js CHANGED
@@ -1,4 +1,4 @@
1
- import { D as o, d as r, e as i, c as n, U as g, V as A, a as t, b as C, o as l, i as I, g as f, h as E, m as _, l as T, k as U, n as p, p as D, r as d, f as L, j as V, s as m, v } from "../helper-DnJHjgMg.js";
1
+ import { D as o, d as r, e as i, c as n, U as g, V as A, a as t, b as C, o as l, i as I, g as f, h as E, m as _, l as T, k as U, n as p, p as D, r as d, f as L, j as V, s as m, v } from "../helper-CQZmdyi3.js";
2
2
  import "../bridge-K3HqQwdj.js";
3
3
  export {
4
4
  o as DEFAULT_BASE_URL,
@@ -1,8 +1,8 @@
1
1
  import { AISDKError } from 'ai';
2
2
  export type ViggleKind = 'character' | 'motion';
3
- export type ViggleStage = 'prepare' | 'authorize' | 'create' | 'poll' | 'resume';
3
+ export type ViggleStage = 'authorize' | 'create' | 'poll' | 'resume';
4
4
  interface GenerationOptions {
5
- /** Save createViggleRequestId() before starting, and reuse it for a retry. */
5
+ /** Optional explicit idempotency key. Normal calls manage recovery internally. */
6
6
  requestId?: string;
7
7
  signal?: AbortSignal;
8
8
  }
@@ -34,8 +34,10 @@ interface Handle {
34
34
  jobConfirmed?: boolean;
35
35
  kind: ViggleKind;
36
36
  stage: ViggleStage;
37
+ persist?: () => void;
38
+ clear?: () => void;
37
39
  }
38
- /** Retain these fields after cancellation, timeout, or a lost HTTP response. */
40
+ /** Recovery is automatic on the next call with the same input. Handles remain available for diagnostics and explicit recovery. */
39
41
  export declare class ViggleGenerationError extends AISDKError {
40
42
  readonly requestId?: string;
41
43
  readonly jobId?: string;
@@ -45,7 +47,13 @@ export declare class ViggleGenerationError extends AISDKError {
45
47
  readonly stage: ViggleStage;
46
48
  readonly reason: string;
47
49
  readonly httpStatus?: number;
48
- constructor(message: string, handle: Handle, reason: string, cause?: unknown, httpStatus?: number);
50
+ readonly activeJob?: {
51
+ jobId: string;
52
+ kind: ViggleKind;
53
+ requestId: string;
54
+ sameInput: boolean;
55
+ };
56
+ constructor(message: string, handle: Handle, reason: string, cause?: unknown, httpStatus?: number, activeJob?: ViggleGenerationError['activeJob']);
49
57
  }
50
58
  /** One key per user action. A retry of the same action must reuse the key. */
51
59
  export declare function createViggleRequestId(): string;
@@ -0,0 +1,12 @@
1
+ export interface SavedGeneration {
2
+ requestId: string;
3
+ jobId?: string;
4
+ }
5
+ export declare function inputDigest(input: unknown): Promise<string>;
6
+ /** Hints only: server ownership, input binding, CAS and purchase authorization
7
+ * remain authoritative. Never persist prompts, images, tokens or billing data. */
8
+ export declare function recoveryKey(kind: string, digest: string): string | undefined;
9
+ export declare function readGeneration(key?: string): SavedGeneration | undefined;
10
+ export declare function saveGeneration(key: string | undefined, saved: SavedGeneration): void;
11
+ export declare function clearGeneration(key: string | undefined, requestId: string): void;
12
+ export declare function singleGeneration<T>(key: string | undefined, run: () => Promise<T>, signal: AbortSignal): Promise<T>;
@@ -1,5 +1,5 @@
1
1
  import { g as o, a as i } from "./bridge-K3HqQwdj.js";
2
- const r = "0.4.2-dev.7", c = {
2
+ const r = "0.4.2-dev.8", c = {
3
3
  version: r
4
4
  }, a = c.version, p = "@aippy/runtime";
5
5
  function u() {
@@ -1,7 +1,7 @@
1
1
  import { D as r, g as s, m as o } from "../config-B0A7gHQM.js";
2
2
  import { _ as t, r as i } from "../bridge-K3HqQwdj.js";
3
3
  import { A as R, a as E, E as c, b as m, c as A } from "../errors-B3bDbQbD.js";
4
- import { S as f, V as g, c as l, a as y, g as _, s as d } from "../container-message-D-wg98Oh.js";
4
+ import { S as f, V as g, c as l, a as y, g as _, s as d } from "../container-message-D_fZlguH.js";
5
5
  import { A as D, C as O, R as F, a as I, p as S } from "../runtime-CmoG3v2m.js";
6
6
  export {
7
7
  D as AippyRuntime,
@@ -1,32 +1,32 @@
1
- var D = Object.defineProperty;
2
- var R = (e, t, n) => t in e ? D(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
- var l = (e, t, n) => R(e, typeof t != "symbol" ? t + "" : t, n);
4
- import { createOpenAICompatible as x } from "@ai-sdk/openai-compatible";
5
- import { s as C, a as E } from "./container-message-D-wg98Oh.js";
6
- import { AISDKError as h, DefaultChatTransport as F } from "ai";
7
- import { E as g, b as m, a as b } from "./errors-B3bDbQbD.js";
8
- import { x as L, M as I, R as z, q as P, r as G, w as B } from "./recharge-BOY8wLs6.js";
9
- import { j as K } from "./url-c26cuIpu.js";
10
- import { r as y, c as j } from "./bridge-K3HqQwdj.js";
11
- const $ = `${y()}/aisdk/v1/`, we = `${y()}/aisdk/v1/ui/`, J = "gpt-5-nano", H = "";
12
- function W(e = {}) {
1
+ var F = Object.defineProperty;
2
+ var z = (e, t, n) => t in e ? F(e, t, { enumerable: !0, configurable: !0, writable: !0, value: n }) : e[t] = n;
3
+ var l = (e, t, n) => z(e, typeof t != "symbol" ? t + "" : t, n);
4
+ import { createOpenAICompatible as G } from "@ai-sdk/openai-compatible";
5
+ import { s as U, a as E } from "./container-message-D_fZlguH.js";
6
+ import { AISDKError as m, DefaultChatTransport as P } from "ai";
7
+ import { E as h, b as w, a as v } from "./errors-B3bDbQbD.js";
8
+ import { x as J, M as O, R as B, q as K, r as H, w as W } from "./recharge-DSMMSwg8.js";
9
+ import { j as Q } from "./url-c26cuIpu.js";
10
+ import { r as j, c as q } from "./bridge-K3HqQwdj.js";
11
+ const T = `${j()}/aisdk/v1/`, Ue = `${j()}/aisdk/v1/ui/`, Y = "gpt-5-nano", X = "";
12
+ function Z(e = {}) {
13
13
  return {
14
- baseUrl: e.baseUrl ?? `${y()}/aisdk/v1/`
14
+ baseUrl: e.baseUrl ?? `${j()}/aisdk/v1/`
15
15
  };
16
16
  }
17
- function Q(e = {}) {
17
+ function ee(e = {}) {
18
18
  return {
19
- baseUrl: e.baseUrl ?? `${y()}/aisdk/v1/ui/`
19
+ baseUrl: e.baseUrl ?? `${j()}/aisdk/v1/ui/`
20
20
  };
21
21
  }
22
- const Y = 100003, X = "insufficient_quota";
23
- function Z(e, t) {
22
+ const te = 100003, re = "insufficient_quota";
23
+ function ne(e, t) {
24
24
  const n = e.status;
25
25
  if (t && typeof t == "object" && "error" in t) {
26
26
  const r = t, s = r.error.code, i = s != null ? `AippyAIError_${s}` : `AippyAIError_${n}`, a = t;
27
- return "appMessage" in a && a.appMessage !== void 0 && C(a.appMessage), ee(r.error) && L(), new h({
27
+ return "appMessage" in a && a.appMessage !== void 0 && U(a.appMessage), se(r.error) && J(), new m({
28
28
  name: i,
29
- message: `${g.RUNTIME} ${r.error.message}`,
29
+ message: `${h.RUNTIME} ${r.error.message}`,
30
30
  cause: {
31
31
  type: r.error.type ?? void 0,
32
32
  param: r.error.param ?? void 0,
@@ -36,18 +36,18 @@ function Z(e, t) {
36
36
  }
37
37
  if (t && typeof t == "object" && t !== null) {
38
38
  const r = t;
39
- "appMessage" in r && r.appMessage !== void 0 && C(r.appMessage);
39
+ "appMessage" in r && r.appMessage !== void 0 && U(r.appMessage);
40
40
  }
41
- return new h({
41
+ return new m({
42
42
  name: `AippyAIError_${n}`,
43
- message: `${g.RUNTIME} Request failed with status ${n}`,
43
+ message: `${h.RUNTIME} Request failed with status ${n}`,
44
44
  cause: { status: n }
45
45
  });
46
46
  }
47
- function ee(e) {
48
- return e.code === Y || e.type === X;
47
+ function se(e) {
48
+ return e.code === te || e.type === re;
49
49
  }
50
- function S() {
50
+ function k() {
51
51
  return async (e, t) => {
52
52
  const n = await E(t?.headers), r = await globalThis.fetch(e, { ...t, headers: n });
53
53
  if (!r.ok) {
@@ -59,20 +59,20 @@ function S() {
59
59
  } catch {
60
60
  i = { message: s };
61
61
  }
62
- throw Z(r, i);
62
+ throw ne(r, i);
63
63
  }
64
64
  return r;
65
65
  };
66
66
  }
67
- const te = [
67
+ const ie = [
68
68
  "gpt-image-1",
69
69
  "gpt-image-1-mini",
70
70
  "gpt-image-1.5"
71
71
  ];
72
- function re(e) {
72
+ function ae(e) {
73
73
  try {
74
74
  const t = JSON.parse(e);
75
- if (t.model && te.some((n) => t.model.startsWith(n))) {
75
+ if (t.model && ie.some((n) => t.model.startsWith(n))) {
76
76
  const { response_format: n, ...r } = t;
77
77
  return JSON.stringify(r);
78
78
  }
@@ -80,25 +80,25 @@ function re(e) {
80
80
  }
81
81
  return e;
82
82
  }
83
- function Ie(e = {}) {
84
- const { baseUrl: t } = W(e), n = S();
85
- return x({
83
+ function Ne(e = {}) {
84
+ const { baseUrl: t } = Z(e), n = k();
85
+ return G({
86
86
  name: "aippy",
87
87
  baseURL: t,
88
88
  fetch: async (s, i) => s.toString().includes("/images/generations") && i?.method?.toUpperCase() === "POST" && i?.body ? n(s, {
89
89
  ...i,
90
- body: re(i.body)
90
+ body: ae(i.body)
91
91
  }) : n(s, i),
92
92
  // Enable structured outputs support (json_schema response format)
93
93
  // This is required for Output.object() and Output.array() to work properly
94
94
  supportsStructuredOutputs: !0
95
95
  });
96
96
  }
97
- const ne = "/chat";
98
- function ve(e = {}) {
99
- const { baseUrl: t } = Q(e), n = e.model ?? J, r = e.system ?? H, s = e.api ?? K(t, ne), i = { model: n, system: r }, a = S();
97
+ const oe = "/chat";
98
+ function ke(e = {}) {
99
+ const { baseUrl: t } = ee(e), n = e.model ?? Y, r = e.system ?? X, s = e.api ?? Q(t, oe), i = { model: n, system: r }, a = k();
100
100
  return {
101
- transport: new F({
101
+ transport: new P({
102
102
  api: s,
103
103
  body: i,
104
104
  // Ensure token is fetched fresh for every request
@@ -106,11 +106,84 @@ function ve(e = {}) {
106
106
  })
107
107
  };
108
108
  }
109
- class f extends h {
110
- constructor(n, r, s, i, a) {
109
+ const b = /* @__PURE__ */ new Map(), I = /* @__PURE__ */ new Map(), ce = /^vg1_[0-9]{10}_[0-9a-f]{32}$/;
110
+ async function S(e) {
111
+ const t = e instanceof Blob ? await e.arrayBuffer() : new TextEncoder().encode(JSON.stringify(e)), n = await globalThis.crypto.subtle.digest("SHA-256", t);
112
+ return Array.from(
113
+ new Uint8Array(n),
114
+ (r) => r.toString(16).padStart(2, "0")
115
+ ).join("");
116
+ }
117
+ function ue(e, t) {
118
+ const n = q();
119
+ if (!n || typeof window > "u") return;
120
+ const r = new URL(window.location.href).origin;
121
+ return `aippy:viggle:${JSON.stringify([T, r, n, e, t])}`;
122
+ }
123
+ function de(e) {
124
+ if (!e) return;
125
+ const t = b.get(e);
126
+ let n = t?.value;
127
+ try {
128
+ if (!t || t.stored) {
129
+ const r = window.localStorage.getItem(e);
130
+ if (r) n = JSON.parse(r);
131
+ else {
132
+ b.delete(e);
133
+ return;
134
+ }
135
+ }
136
+ } catch {
137
+ }
138
+ if (!(!n || !ce.test(n.requestId) || n.jobId !== void 0 && !/^job_[0-9a-f]{32}$/.test(n.jobId)))
139
+ return n;
140
+ }
141
+ function le(e, t) {
142
+ if (!e) return;
143
+ const n = { value: { ...t }, stored: !1 };
144
+ b.set(e, n);
145
+ try {
146
+ window.localStorage.setItem(e, JSON.stringify(t)), n.stored = !0;
147
+ } catch {
148
+ }
149
+ }
150
+ function fe(e, t) {
151
+ if (e) {
152
+ b.get(e)?.value.requestId === t && b.delete(e);
153
+ try {
154
+ const n = window.localStorage.getItem(e);
155
+ n && JSON.parse(n)?.requestId === t && window.localStorage.removeItem(e);
156
+ } catch {
157
+ }
158
+ }
159
+ }
160
+ function ge(e, t, n) {
161
+ if (!e) return t();
162
+ const r = I.get(e);
163
+ if (r) return he(r, n);
164
+ const s = t().finally(() => {
165
+ I.get(e) === s && I.delete(e);
166
+ });
167
+ return I.set(e, s), s;
168
+ }
169
+ function he(e, t) {
170
+ return new Promise((n, r) => {
171
+ const s = () => r(t.reason);
172
+ t.addEventListener("abort", s, { once: !0 }), e.then(
173
+ (i) => {
174
+ t.removeEventListener("abort", s), n(i);
175
+ },
176
+ (i) => {
177
+ t.removeEventListener("abort", s), r(i);
178
+ }
179
+ ), t.aborted && s();
180
+ });
181
+ }
182
+ class g extends m {
183
+ constructor(n, r, s, i, a, o) {
111
184
  super({
112
185
  name: "AippyAIError_viggle",
113
- message: `${g.RUNTIME} ${n}`,
186
+ message: `${h.RUNTIME} ${n}`,
114
187
  cause: i
115
188
  });
116
189
  l(this, "requestId");
@@ -121,108 +194,151 @@ class f extends h {
121
194
  l(this, "stage");
122
195
  l(this, "reason");
123
196
  l(this, "httpStatus");
124
- this.requestId = r.requestId, this.jobId = r.jobId, this.jobConfirmed = r.jobConfirmed === !0, this.kind = r.kind, this.stage = r.stage, this.reason = s, this.httpStatus = a;
197
+ l(this, "activeJob");
198
+ this.requestId = r.requestId, this.jobId = r.jobId, this.jobConfirmed = r.jobConfirmed === !0, this.kind = r.kind, this.stage = r.stage, this.reason = s, this.httpStatus = a, this.activeJob = o;
125
199
  }
126
200
  }
127
- function T() {
201
+ function pe() {
128
202
  const e = new Uint8Array(16);
129
203
  return globalThis.crypto.getRandomValues(e), `vg1_${Math.floor(Date.now() / 1e3)}_${Array.from(e, (t) => t.toString(16).padStart(2, "0")).join("")}`;
130
204
  }
131
- const A = /^vg1_[0-9]{10}_[0-9a-f]{32}$/, se = 900 * 1e3, v = (e) => [...e].length;
132
- async function Ee(e) {
205
+ const A = /^vg1_[0-9]{10}_[0-9a-f]{32}$/, me = 900 * 1e3, $ = (e) => [...e].length;
206
+ async function Me(e) {
133
207
  const t = e.prompt?.trim() ?? "", n = e.imageUrl?.trim() ?? "", r = e.image;
134
- v(t) > 1e3 && d("generateCharacter3D prompt must be at most 1000 characters"), r != null && !(r instanceof Blob) && d("generateCharacter3D image must be a file"), r instanceof Blob && r.size === 0 && d("generateCharacter3D image is empty"), !r && !t && !n && d(
208
+ return $(t) > 1e3 && f("generateCharacter3D prompt must be at most 1000 characters"), r != null && !(r instanceof Blob) && f("generateCharacter3D image must be a file"), r instanceof Blob && r.size === 0 && f("generateCharacter3D image is empty"), !r && !t && !n && f(
135
209
  "generateCharacter3D requires an image, an https imageUrl, or a prompt"
136
- ), !r && n && !n.startsWith("https://") && d("generateCharacter3D imageUrl must be an https URL");
137
- const s = e.requestId ?? T();
138
- let i;
139
- if (r instanceof Blob) {
140
- const a = new FormData();
141
- a.append("requestId", s), a.append(
142
- "image",
143
- r,
144
- typeof File < "u" && r instanceof File && r.name ? r.name : "image.png"
145
- ), t && a.append("prompt", t), i = { body: a };
146
- } else
147
- i = q({
148
- requestId: s,
149
- ...n ? { imageUrl: n } : {},
150
- ...t ? { prompt: t } : {}
151
- });
152
- return k("character", s, i, e);
210
+ ), !r && n && !n.startsWith("https://") && f("generateCharacter3D imageUrl must be an https URL"), M(
211
+ "character",
212
+ async () => S([
213
+ "character",
214
+ t,
215
+ r ? "" : n,
216
+ r ? await S(r) : ""
217
+ ]),
218
+ (s) => {
219
+ let i;
220
+ if (r instanceof Blob) {
221
+ const a = new FormData();
222
+ a.append("requestId", s), a.append(
223
+ "image",
224
+ r,
225
+ typeof File < "u" && r instanceof File && r.name ? r.name : "image.png"
226
+ ), t && a.append("prompt", t), i = { body: a };
227
+ } else
228
+ i = V({
229
+ requestId: s,
230
+ ...n ? { imageUrl: n } : {},
231
+ ...t ? { prompt: t } : {}
232
+ });
233
+ return i;
234
+ },
235
+ e
236
+ );
153
237
  }
154
- async function Ae(e) {
155
- const t = e.text?.trim() ?? "";
156
- (v(t) < 1 || v(t) > 400) && d("generateMotion text must be 1 to 400 characters"), e.durationSeconds != null && (!Number.isInteger(e.durationSeconds) || e.durationSeconds < 3 || e.durationSeconds > 60) && d("generateMotion durationSeconds must be an integer from 3 to 60");
157
- const n = e.requestId ?? T();
158
- return k(
238
+ async function De(e) {
239
+ const t = e.text?.trim() ?? "", n = e.durationSeconds;
240
+ return ($(t) < 1 || $(t) > 400) && f("generateMotion text must be 1 to 400 characters"), n != null && (!Number.isInteger(n) || n < 3 || n > 60) && f("generateMotion durationSeconds must be an integer from 3 to 60"), M(
159
241
  "motion",
160
- n,
161
- q({
162
- requestId: n,
242
+ () => S(["motion", t, n ?? 5]),
243
+ (r) => V({
244
+ requestId: r,
163
245
  text: t,
164
- ...e.durationSeconds != null ? { durationSeconds: e.durationSeconds } : {}
246
+ ...n != null ? { durationSeconds: n } : {}
165
247
  }),
166
248
  e
167
249
  );
168
250
  }
169
- async function k(e, t, n, r) {
170
- A.test(t) || d("Invalid Viggle requestId; use createViggleRequestId()");
171
- const s = { requestId: t, kind: e, stage: "create" };
172
- return V(s, r, async (i) => {
173
- const a = await i.job(U(e), { method: "POST", ...n });
174
- return s.stage = "poll", i.wait(a, !0);
251
+ async function M(e, t, n, r) {
252
+ r = { ...r };
253
+ const s = { kind: e, stage: "create" };
254
+ return x(s, r, async (i) => {
255
+ const a = await t();
256
+ i.check(), await E(), i.check();
257
+ const o = r.requestId ? void 0 : ue(e, a), c = await ge(
258
+ o,
259
+ async () => {
260
+ const u = de(o);
261
+ s.requestId = r.requestId ?? u?.requestId ?? pe(), A.test(s.requestId) || f("Invalid Viggle requestId"), s.jobId = u?.jobId, s.jobConfirmed = !!u?.jobId, s.persist = () => le(o, {
262
+ requestId: s.requestId,
263
+ ...s.jobId ? { jobId: s.jobId } : {}
264
+ }), s.clear = () => fe(o, s.requestId), s.persist();
265
+ try {
266
+ let d;
267
+ if (s.jobId)
268
+ s.stage = "resume", d = await i.get(!0);
269
+ else
270
+ try {
271
+ d = await i.job(D(e), {
272
+ method: "POST",
273
+ ...n(s.requestId)
274
+ });
275
+ } catch (_) {
276
+ const p = _ instanceof g ? _.activeJob : void 0;
277
+ if (!p || !p.sameInput || p.kind !== e) throw _;
278
+ s.requestId = p.requestId, s.jobId = p.jobId, s.jobConfirmed = !0, s.stage = "resume", s.persist(), d = await i.get(!0);
279
+ }
280
+ s.stage = "poll";
281
+ const y = await i.wait(d, !0);
282
+ return s.clear?.(), y;
283
+ } catch (d) {
284
+ throw d instanceof g && (["generation_failed", "request_expired"].includes(d.reason) || d.httpStatus === 404) && s.clear?.(), d;
285
+ }
286
+ },
287
+ i.signal
288
+ );
289
+ return i.check(), c;
175
290
  });
176
291
  }
177
- async function _e(e) {
178
- (!e || !["character", "motion"].includes(e.kind) || !/^job_[0-9a-f]{32}$/.test(e.jobId)) && d("Invalid Viggle jobId or kind"), e.requestId != null && !A.test(e.requestId) && d("Invalid Viggle requestId");
292
+ async function Ve(e) {
293
+ (!e || !["character", "motion"].includes(e.kind) || !/^job_[0-9a-f]{32}$/.test(e.jobId)) && f("Invalid Viggle jobId or kind"), e.requestId != null && !A.test(e.requestId) && f("Invalid Viggle requestId");
179
294
  const t = {
180
295
  requestId: e.requestId,
181
296
  jobId: e.jobId,
182
297
  kind: e.kind,
183
298
  stage: "resume"
184
299
  };
185
- return V(
300
+ return x(
186
301
  t,
187
302
  e,
188
303
  async (n) => n.wait(await n.get(!0), !0)
189
304
  );
190
305
  }
191
- function U(e) {
306
+ function D(e) {
192
307
  return `viggle/${e === "character" ? "characters" : "motions"}`;
193
308
  }
194
- function q(e) {
309
+ function V(e) {
195
310
  return {
196
311
  headers: { "Content-Type": "application/json" },
197
312
  body: JSON.stringify(e)
198
313
  };
199
314
  }
200
- function O(e) {
315
+ function N(e) {
201
316
  return typeof e.benefitKey == "string" && !!e.benefitKey && typeof e.bizOrderNo == "string" && e.bizOrderNo.startsWith("aisdk:managed:") && e.bizOrderNo.length <= 64 && e.quantity === 1 && Number.isSafeInteger(e.priceGEM) && e.priceGEM > 0;
202
317
  }
203
- function N(e) {
318
+ function R(e) {
204
319
  const t = typeof window > "u" ? void 0 : window, n = t?.location?.href;
205
- let r = j();
320
+ let r = q();
206
321
  return () => {
207
322
  if (e.aborted) throw e.reason;
208
323
  if ((typeof window > "u" ? void 0 : window) !== t || t?.location?.href !== n)
209
324
  throw new DOMException("Generation page changed", "AbortError");
210
- const s = j();
325
+ const s = q();
211
326
  if (r === null)
212
327
  r = s;
213
328
  else if (s !== r)
214
329
  throw new DOMException("Generation account changed", "AbortError");
215
330
  };
216
331
  }
217
- async function V(e, t, n) {
332
+ async function x(e, t, n) {
333
+ t = { ...t };
218
334
  const r = new AbortController(), s = () => r.abort(t.signal?.reason);
219
335
  t.signal?.addEventListener("abort", s, { once: !0 }), t.signal?.aborted && s();
220
336
  const i = t, a = setTimeout(
221
337
  () => r.abort(
222
338
  new DOMException("Viggle generation timed out", "TimeoutError")
223
339
  ),
224
- i.timeoutMs ?? se
225
- ), o = new ie(
340
+ i.timeoutMs ?? me
341
+ ), o = new be(
226
342
  e,
227
343
  r.signal,
228
344
  i.pollIntervalMs ?? (e.kind === "character" ? 3e3 : 5e3)
@@ -230,33 +346,32 @@ async function V(e, t, n) {
230
346
  try {
231
347
  return await n(o);
232
348
  } catch (c) {
233
- if (c instanceof f) throw c;
234
- const u = r.signal.aborted ? r.signal.reason : c, p = u instanceof Error && u.name === "TimeoutError" ? "timeout" : r.signal.aborted || u instanceof Error && u.name === "AbortError" || c instanceof I && (c.kind === "abort" || c.type === "closed" || c.type === "cancelled") ? "cancelled" : c instanceof I ? c.type === "timeout" || c.type === "balance_timeout" ? "timeout" : c.reason || (c.kind === "unsupported" ? "authorization_unavailable" : "request_failed") : "request_failed";
235
- throw new f(
349
+ if (c instanceof g) throw c;
350
+ const u = r.signal.aborted ? r.signal.reason : c, d = u instanceof Error && u.name === "TimeoutError" ? "timeout" : r.signal.aborted || u instanceof Error && u.name === "AbortError" || c instanceof O && (c.kind === "abort" || c.type === "closed" || c.type === "cancelled") ? "cancelled" : c instanceof O ? c.type === "timeout" || c.type === "balance_timeout" ? "timeout" : c.reason || (c.kind === "unsupported" ? "authorization_unavailable" : "request_failed") : "request_failed";
351
+ throw new g(
236
352
  u instanceof Error ? u.message : "Viggle request failed",
237
353
  e,
238
- p,
354
+ d,
239
355
  c
240
356
  );
241
357
  } finally {
242
358
  clearTimeout(a), t.signal?.removeEventListener("abort", s);
243
359
  }
244
360
  }
245
- class ie {
361
+ class be {
246
362
  constructor(t, n, r) {
247
363
  l(this, "check");
248
- l(this, "billingVersion");
249
- this.handle = t, this.signal = n, this.interval = r, this.check = N(n);
364
+ this.handle = t, this.signal = n, this.interval = r, this.check = R(n);
250
365
  }
251
366
  miniOptions() {
252
- return { signal: this.signal, [z]: this.check };
367
+ return { signal: this.signal, [B]: this.check };
253
368
  }
254
369
  async send(t, n, r, s = this.signal) {
255
370
  this.check(), r?.();
256
371
  const i = await E(n.headers);
257
372
  if (this.check(), r?.(), s.aborted) throw s.reason;
258
373
  const a = await globalThis.fetch(
259
- `${$.replace(/\/$/, "")}/${t}`,
374
+ `${T.replace(/\/$/, "")}/${t}`,
260
375
  {
261
376
  ...n,
262
377
  headers: i,
@@ -269,38 +384,39 @@ class ie {
269
384
  });
270
385
  if (this.check(), r?.(), a.status !== 200) {
271
386
  const c = o?.error;
272
- throw new f(
387
+ throw new g(
273
388
  typeof c?.message == "string" ? c.message : `Viggle request failed (${a.status})`,
274
389
  this.handle,
275
390
  typeof c?.param?.reason == "string" ? c.param.reason : "http_error",
276
391
  void 0,
277
- a.status
392
+ a.status,
393
+ Ie(c?.param)
278
394
  );
279
395
  }
280
396
  return o;
281
397
  }
282
398
  async job(t, n, r) {
283
- const s = await this.send(t, n, r), i = s?.billingVersion === void 0 ? 1 : s.billingVersion;
284
- if (!s || ![1, 2].includes(i) || this.billingVersion !== void 0 && i !== this.billingVersion || typeof s.id != "string" || !/^job_[0-9a-f]{32}$/.test(s.id) || this.handle.jobId && s.id !== this.handle.jobId || typeof s.requestId != "string" || !A.test(s.requestId) || this.handle.requestId && s.requestId !== this.handle.requestId || !["queued", "processing", "ready", "failed"].includes(s.status))
399
+ const s = await this.send(t, n, r);
400
+ if (!s || s.billingVersion !== 2 || typeof s.id != "string" || !/^job_[0-9a-f]{32}$/.test(s.id) || this.handle.jobId && s.id !== this.handle.jobId || typeof s.requestId != "string" || !A.test(s.requestId) || this.handle.requestId && s.requestId !== this.handle.requestId || !["queued", "processing", "ready", "failed"].includes(s.status))
285
401
  throw new Error("Invalid Viggle job response");
286
- return this.handle.jobId = s.id, this.billingVersion = i, this.handle.requestId = s.requestId, this.handle.jobConfirmed = !0, s;
402
+ return this.handle.jobId = s.id, this.handle.requestId = s.requestId, this.handle.jobConfirmed = !0, this.handle.persist?.(), s;
287
403
  }
288
404
  get(t = !1, n) {
289
405
  return this.job(
290
- `${U(this.handle.kind)}/${encodeURIComponent(this.handle.jobId)}${t ? "?resume=1" : ""}`,
406
+ `${D(this.handle.kind)}/${encodeURIComponent(this.handle.jobId)}${t ? "?resume=1" : ""}`,
291
407
  { method: "GET" },
292
408
  n
293
409
  );
294
410
  }
295
411
  async authorize(t) {
296
- if (!O(t))
412
+ if (!N(t))
297
413
  throw new Error("Invalid Viggle purchase offer");
298
414
  this.check();
299
415
  const n = this.handle.stage;
300
416
  this.handle.stage = "authorize";
301
417
  let r;
302
418
  try {
303
- r = await P(
419
+ r = await K(
304
420
  {
305
421
  operation: "purchase",
306
422
  benefitKey: t.benefitKey,
@@ -313,8 +429,8 @@ class ie {
313
429
  this.miniOptions()
314
430
  );
315
431
  } catch (s) {
316
- if (s instanceof I && ["closed", "cancelled"].includes(s.type ?? "")) {
317
- const i = new f(
432
+ if (s instanceof O && ["closed", "cancelled"].includes(s.type ?? "")) {
433
+ const i = new g(
318
434
  "Viggle purchase authorization was cancelled",
319
435
  this.handle,
320
436
  "cancelled",
@@ -325,7 +441,7 @@ class ie {
325
441
  throw s;
326
442
  }
327
443
  if (this.check(), !r.ok)
328
- throw await this.cancelDraft(), new f(
444
+ throw await this.cancelDraft(), new g(
329
445
  "Viggle purchase authorization was not confirmed",
330
446
  this.handle,
331
447
  r.reason
@@ -349,7 +465,10 @@ class ie {
349
465
  { method: "POST" },
350
466
  void 0,
351
467
  t.signal
352
- ),
468
+ ).then((a) => {
469
+ const o = a;
470
+ o && o.id === this.handle.jobId && o.requestId === this.handle.requestId && o.billingVersion === 2 && o.status === "failed" && this.handle.clear?.();
471
+ }),
353
472
  r
354
473
  ]);
355
474
  } catch {
@@ -361,7 +480,7 @@ class ie {
361
480
  let r = t, s = !1, i = !1;
362
481
  for (; ; ) {
363
482
  if (this.check(), r.status === "ready") {
364
- if (r.billingVersion === 2 && r.billing?.state !== "consumed")
483
+ if (r.billing?.state !== "consumed")
365
484
  throw new Error("Viggle coupon consumption is not confirmed");
366
485
  if (typeof r.url != "string" || !r.url.startsWith("https://"))
367
486
  throw new Error("Viggle generation finished without a file URL");
@@ -375,12 +494,12 @@ class ie {
375
494
  };
376
495
  }
377
496
  if (r.status === "failed")
378
- throw new f(
497
+ throw new g(
379
498
  r.error || "Viggle generation failed",
380
499
  this.handle,
381
500
  "generation_failed"
382
501
  );
383
- const a = r.billing, o = a?.reason, c = r.billingVersion !== 2 || [
502
+ const a = r.billing, o = a?.reason, c = [
384
503
  "awaiting_authorization",
385
504
  "insufficient_balance",
386
505
  "purchase_refused"
@@ -395,13 +514,13 @@ class ie {
395
514
  s = !0, await this.authorize(a), r = await this.get(!0);
396
515
  continue;
397
516
  }
398
- if (n && c && o === "insufficient_balance" && !i && a && O(a)) {
517
+ if (n && c && o === "insufficient_balance" && !i && a && N(a)) {
399
518
  i = !0;
400
- const u = G(this.miniOptions());
519
+ const u = H(this.miniOptions());
401
520
  u.check();
402
- let p = !0;
403
- const _ = await B(
404
- async () => (this.check(), p || (r = await this.get(!0, u.check)), p = !1, r.billing?.reason === "insufficient_balance" ? {
521
+ let d = !0;
522
+ const y = await W(
523
+ async () => (this.check(), d || (r = await this.get(!0, u.check)), d = !1, r.billing?.reason === "insufficient_balance" ? {
405
524
  ok: !1,
406
525
  reason: "insufficient_balance",
407
526
  data: r
@@ -409,19 +528,19 @@ class ie {
409
528
  u,
410
529
  a.bizOrderNo
411
530
  );
412
- if (this.check(), r = _.data, _.ok) continue;
531
+ if (this.check(), r = y.data, y.ok) continue;
413
532
  }
414
- throw new f(
533
+ throw new g(
415
534
  `Viggle purchase needs attention: ${o}`,
416
535
  this.handle,
417
536
  o
418
537
  );
419
538
  }
420
- await ae(this.interval, this.signal), r = await this.get();
539
+ await ye(this.interval, this.signal), r = await this.get();
421
540
  }
422
541
  }
423
542
  }
424
- function ae(e, t) {
543
+ function ye(e, t) {
425
544
  return t.aborted ? Promise.reject(t.reason) : new Promise((n, r) => {
426
545
  const s = () => {
427
546
  clearTimeout(i), r(t.reason);
@@ -431,23 +550,34 @@ function ae(e, t) {
431
550
  t.addEventListener("abort", s, { once: !0 });
432
551
  });
433
552
  }
434
- function d(e) {
435
- throw new h({
553
+ function f(e) {
554
+ throw new m({
436
555
  name: "AippyAIError_viggle_input",
437
- message: `${g.CODE} ${e}`
556
+ message: `${h.CODE} ${e}`
438
557
  });
439
558
  }
440
- async function Ce(e = {}) {
559
+ function Ie(e) {
560
+ if (!e || typeof e != "object") return;
561
+ const t = e;
562
+ if (!(t.reason !== "generation_busy" || typeof t.jobId != "string" || !/^job_[0-9a-f]{32}$/.test(t.jobId) || t.kind !== "motion" && t.kind !== "character" || typeof t.requestId != "string" || !A.test(t.requestId) || typeof t.sameInput != "boolean"))
563
+ return {
564
+ jobId: t.jobId,
565
+ kind: t.kind,
566
+ requestId: t.requestId,
567
+ sameInput: t.sameInput
568
+ };
569
+ }
570
+ async function Re(e = {}) {
441
571
  if (e.kind != null && !["character", "motion"].includes(e.kind) || e.limit != null && (!Number.isInteger(e.limit) || e.limit < 1 || e.limit > 50) || e.cursor != null && e.cursor !== "" && !/^[1-9][0-9]*$/.test(e.cursor))
442
- throw w("Invalid Viggle model-list query", !0);
443
- const t = e.signal ?? new AbortController().signal, n = N(t);
572
+ throw C("Invalid Viggle model-list query", !0);
573
+ const t = e.signal ?? new AbortController().signal, n = R(t);
444
574
  n();
445
575
  const r = new URLSearchParams();
446
576
  e.limit != null && r.set("limit", String(e.limit)), e.kind && r.set("kind", e.kind), e.cursor && r.set("cursor", e.cursor);
447
577
  const s = await E();
448
578
  n();
449
579
  const i = r.size ? `?${r.toString()}` : "", a = await globalThis.fetch(
450
- `${$.replace(/\/$/, "")}/viggle/models${i}`,
580
+ `${T.replace(/\/$/, "")}/viggle/models${i}`,
451
581
  {
452
582
  method: "GET",
453
583
  headers: s,
@@ -459,24 +589,24 @@ async function Ce(e = {}) {
459
589
  ), o = await a.json().catch(() => {
460
590
  });
461
591
  if (n(), a.status !== 200)
462
- throw w(`Viggle models request failed (${a.status})`);
463
- if (!o || !Array.isArray(o.models) || o.models.length > 50 || typeof o.nextCursor != "string" || o.nextCursor !== "" && !/^[1-9][0-9]*$/.test(o.nextCursor) || !o.models.every(oe))
464
- throw w("Invalid Viggle model-list response");
592
+ throw C(`Viggle models request failed (${a.status})`);
593
+ if (!o || !Array.isArray(o.models) || o.models.length > 50 || typeof o.nextCursor != "string" || o.nextCursor !== "" && !/^[1-9][0-9]*$/.test(o.nextCursor) || !o.models.every(we))
594
+ throw C("Invalid Viggle model-list response");
465
595
  return o;
466
596
  }
467
- function oe(e) {
597
+ function we(e) {
468
598
  return !!e && typeof e.recordId == "string" && /^[1-9][0-9]*$/.test(e.recordId) && typeof e.benefitKey == "string" && !!e.benefitKey && typeof e.bizOrderNo == "string" && typeof e.jobId == "string" && /^job_[0-9a-f]{32}$/.test(e.jobId) && ["character", "motion"].includes(e.kind) && typeof e.url == "string" && e.url.startsWith("https://") && typeof e.generatedAt == "string" && typeof e.consumedAt == "string" && !!e.value && e.value.schemaVersion === 1 && e.value.source === "aisdk.viggle" && e.value.provider === "viggle" && e.value.kind === e.kind && e.value.jobId === e.jobId && e.value.asset?.url === e.url;
469
599
  }
470
- function w(e, t = !1) {
471
- return new h({
600
+ function C(e, t = !1) {
601
+ return new m({
472
602
  name: "AippyAIError_viggle_models",
473
- message: `${t ? g.CODE : g.RUNTIME} ${e}`
603
+ message: `${t ? h.CODE : h.RUNTIME} ${e}`
474
604
  });
475
605
  }
476
- function M(e) {
606
+ function L(e) {
477
607
  const t = [];
478
608
  if (!e || typeof e != "object")
479
- throw m("AIConfig must be an object", b.INVALID_CONFIG);
609
+ throw w("AIConfig must be an object", v.INVALID_CONFIG);
480
610
  const n = e;
481
611
  for (const [r, s] of Object.entries(n)) {
482
612
  if (!s || typeof s != "object") {
@@ -518,38 +648,38 @@ function M(e) {
518
648
  i.group !== void 0 && typeof i.group != "string" && t.push({ key: r, message: "group must be a string" });
519
649
  }
520
650
  if (t.length > 0)
521
- throw m(
651
+ throw w(
522
652
  `AIConfig validation failed with ${t.length} error(s)`,
523
- b.INVALID_CONFIG,
653
+ v.INVALID_CONFIG,
524
654
  { errors: t }
525
655
  );
526
656
  }
527
- function je(e) {
657
+ function xe(e) {
528
658
  let t;
529
659
  try {
530
660
  t = JSON.parse(e);
531
661
  } catch (n) {
532
- throw m(
662
+ throw w(
533
663
  `Invalid JSON: ${n instanceof Error ? n.message : String(n)}`,
534
- b.INVALID_CONFIG
664
+ v.INVALID_CONFIG
535
665
  );
536
666
  }
537
- return M(t), t;
667
+ return L(t), t;
538
668
  }
539
- function ce(e) {
540
- return M(e), e;
669
+ function ve(e) {
670
+ return L(e), e;
541
671
  }
542
- function ue(e, t) {
672
+ function Ee(e, t) {
543
673
  const n = e[t];
544
674
  if (!n)
545
- throw m(
675
+ throw w(
546
676
  `AIConfig key "${t}" not found`,
547
- b.INVALID_CONFIG,
677
+ v.INVALID_CONFIG,
548
678
  { key: t }
549
679
  );
550
680
  return n.value;
551
681
  }
552
- function le(e) {
682
+ function je(e) {
553
683
  if (!(typeof window > "u"))
554
684
  try {
555
685
  const t = window.webkit?.messageHandlers?.aippyListener;
@@ -577,13 +707,13 @@ function le(e) {
577
707
  console.warn("⚠️ [Aippy AI Config] Failed to send config:", t);
578
708
  }
579
709
  }
580
- function Oe(e) {
581
- const t = ce(e);
582
- return le(t), new Proxy({}, {
710
+ function Le(e) {
711
+ const t = ve(e);
712
+ return je(t), new Proxy({}, {
583
713
  get(n, r) {
584
714
  if (typeof r == "string")
585
715
  try {
586
- return ue(t, r);
716
+ return Ee(t, r);
587
717
  } catch {
588
718
  return;
589
719
  }
@@ -597,26 +727,26 @@ function Oe(e) {
597
727
  });
598
728
  }
599
729
  export {
600
- $ as D,
601
- ne as U,
602
- f as V,
603
- Ie as a,
604
- ve as b,
605
- we as c,
606
- J as d,
607
- H as e,
608
- Q as f,
609
- Ee as g,
610
- Ae as h,
611
- T as i,
612
- _e as j,
613
- ce as k,
614
- Ce as l,
615
- ue as m,
616
- Z as n,
617
- Oe as o,
618
- je as p,
619
- W as r,
620
- le as s,
621
- M as v
730
+ T as D,
731
+ oe as U,
732
+ g as V,
733
+ Ne as a,
734
+ ke as b,
735
+ Ue as c,
736
+ Y as d,
737
+ X as e,
738
+ ee as f,
739
+ Me as g,
740
+ De as h,
741
+ pe as i,
742
+ Ve as j,
743
+ ve as k,
744
+ Re as l,
745
+ Ee as m,
746
+ ne as n,
747
+ Le as o,
748
+ xe as p,
749
+ Z as r,
750
+ je as s,
751
+ L as v
622
752
  };
@@ -1,7 +1,7 @@
1
1
  import { D as r, g as s, m as o } from "../config-B0A7gHQM.js";
2
2
  import { _ as i, g as n, r as p } from "../bridge-K3HqQwdj.js";
3
3
  import { A, a as f, E as c, b as g, c as l } from "../errors-B3bDbQbD.js";
4
- import { S as C, V as d, c as E, a as I, g as S, s as x } from "../container-message-D-wg98Oh.js";
4
+ import { S as C, V as d, c as E, a as I, g as S, s as x } from "../container-message-D_fZlguH.js";
5
5
  import { A as U, C as _, R as M, a as D, p as T } from "../runtime-CmoG3v2m.js";
6
6
  import { CameraAPI as v, FileSystemAPI as y, GeolocationAPI as F, SensorsAPI as P, camera as O, fileSystem as V, geolocation as b, isMotionSupported as L, isOrientationSupported as w, requestMotionPermission as H, sensors as N, vibrate as k, watchMotion as B, watchOrientation as q } from "../device/index.js";
7
7
  import { P as j, a as z, c as K, b as W, g as X, i as Y, p as J, r as Q, s as Z } from "../app-version-checker-YVIFbytA.js";
@@ -10,11 +10,11 @@ import { j as te } from "../url-c26cuIpu.js";
10
10
  import { a as ne, b as pe } from "../useTweaks-C7BEMpV3.js";
11
11
  import { c as Ae, b as fe, i as ce, p as ge, u as le, a as ue } from "../useSound-z_eIn0KH.js";
12
12
  import { reportScore as de, sendEvent as Ee, updateScore as Ie } from "../leaderboard/index.js";
13
- import { D as xe, d as Re, e as Ue, c as _e, U as Me, V as De, a as Te, b as he, o as ve, i as ye, g as Fe, h as Pe, m as Oe, l as Ve, k as be, n as Le, p as we, r as He, f as Ne, j as ke, s as Be, v as qe } from "../helper-DnJHjgMg.js";
13
+ import { D as xe, d as Re, e as Ue, c as _e, U as Me, V as De, a as Te, b as he, o as ve, i as ye, g as Fe, h as Pe, m as Oe, l as Ve, k as be, n as Le, p as we, r as He, f as Ne, j as ke, s as Be, v as qe } from "../helper-CQZmdyi3.js";
14
14
  import { ministore as je } from "../ministore/index.js";
15
15
  import { c as Ke, u as We } from "../hooks-CMKwSLcQ.js";
16
16
  import { hasNativeBridge as Ye } from "../native-bridge-BnvipFJc.js";
17
- import { M as Qe } from "../recharge-BOY8wLs6.js";
17
+ import { M as Qe } from "../recharge-DSMMSwg8.js";
18
18
  export {
19
19
  U as AippyRuntime,
20
20
  A as AippyRuntimeError,
@@ -1,5 +1,5 @@
1
- import { i as y, a as I, b as B, s as m, r as f, w as h, c as l, d, e as u, f as b, g, p as v, h as z, u as O, j as w, o as N, k as P, l as K, m as S, n as T, q as A, t as V, v as H } from "../recharge-BOY8wLs6.js";
2
- import { M as E } from "../recharge-BOY8wLs6.js";
1
+ import { i as y, a as I, b as B, s as m, r as f, w as h, c as l, d, e as u, f as b, g, p as v, h as z, u as O, j as w, o as N, k as P, l as K, m as S, n as T, q as A, t as V, v as H } from "../recharge-DSMMSwg8.js";
2
+ import { M as E } from "../recharge-DSMMSwg8.js";
3
3
  function p(e) {
4
4
  if (e !== void 0)
5
5
  return y(e, "bizOrderNo", 64), e;
@@ -4,7 +4,7 @@ var z = (e, t, r) => de(e, typeof t != "symbol" ? t + "" : t, r);
4
4
  import { a as V, i as le, r as fe, b as he, c as me } from "./bridge-K3HqQwdj.js";
5
5
  import { a as B, r as D } from "./app-version-checker-YVIFbytA.js";
6
6
  import { E as x } from "./errors-B3bDbQbD.js";
7
- import { a as pe, s as be } from "./container-message-D-wg98Oh.js";
7
+ import { a as pe, s as be } from "./container-message-D_fZlguH.js";
8
8
  import { j as ye } from "./url-c26cuIpu.js";
9
9
  const q = "wallet.showRecharge", we = 3e5;
10
10
  class w extends Error {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@aippy/runtime",
3
- "version": "0.4.2-dev.7",
3
+ "version": "0.4.2-dev.8",
4
4
  "description": "Aippy Runtime SDK - Runtime SDK for Aippy projects",
5
5
  "private": false,
6
6
  "type": "module",