@aippy/runtime 0.2.7-dev.2 → 0.2.7-dev.4

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.
@@ -5,6 +5,43 @@ import { AISDKError } from 'ai';
5
5
  * If the backend error response includes container message data (e.g., `appMessage` field),
6
6
  * it will be automatically sent to the app container for user-facing UI (e.g., payment dialog,
7
7
  * error popup, success notification).
8
+ *
9
+ * @example appMessage structure:
10
+ * ```ts
11
+ * interface AppMessage {
12
+ * // Message type determines UI behavior
13
+ * type: 'error' | 'success' | 'warning' | 'info';
14
+ *
15
+ * // Required: User-facing title and message
16
+ * title: string;
17
+ * message: string;
18
+ *
19
+ * // Optional: Action to trigger in the container
20
+ * action?: string; // e.g., 'showPaymentDialog', 'showUpgradeDialog', 'redirect'
21
+ *
22
+ * // Optional: URL for redirect actions
23
+ * redirectUrl?: string;
24
+ *
25
+ * // Optional: Custom button text for action dialogs
26
+ * buttonText?: string;
27
+ *
28
+ * // Optional: Auto-dismiss duration in milliseconds (for success/info messages)
29
+ * duration?: number;
30
+ *
31
+ * // Optional: Additional metadata for container processing
32
+ * metadata?: Record<string, unknown>;
33
+ * }
34
+ *
35
+ * // Example error message with payment action:
36
+ * {
37
+ * type: 'error',
38
+ * title: 'Insufficient Balance',
39
+ * message: 'Your account balance is insufficient. Please top up to continue.',
40
+ * action: 'showPaymentDialog',
41
+ * redirectUrl: '/payment',
42
+ * buttonText: 'Top Up Now'
43
+ * }
44
+ * ```
8
45
  */
9
46
  export declare function normalizeError(response: Response, body?: unknown): AISDKError;
10
47
  /**
package/dist/ai/index.js CHANGED
@@ -1,6 +1,6 @@
1
- import { A as i, D as A, d as n, e, c as E, U as C, f as p, a as g, a as t, b as I, j as T, i as _, l as f, m as U, g as d, n as D, h as L, p as l, s as m, v as F } from "../helper-CsFko67T.js";
1
+ import { A as i, D as A, d as n, e, c as E, U as C, f as p, a as g, a as t, b as I, j as T, i as _, l as f, m as U, g as d, n as D, h as L, p as l, s as m, v as F } from "../helper-CIt1_T-j.js";
2
2
  import "react";
3
- import "../bridge-N9ELFpfV.js";
3
+ import "../bridge-Ca3H2iN1.js";
4
4
  export {
5
5
  i as AIConfigValidationError,
6
6
  A as DEFAULT_BASE_URL,
@@ -19,6 +19,12 @@ export declare function joinUrl(baseUrl: string, path: string): string;
19
19
  * Creates a fetch wrapper that injects Authorization header with a fresh token.
20
20
  * Token is fetched asynchronously on every request to ensure it's always current.
21
21
  *
22
+ * This wrapper also automatically handles error responses by:
23
+ * 1. Detecting error responses (!response.ok)
24
+ * 2. Parsing error body and calling normalizeError() to extract appMessage
25
+ * 3. Sending appMessage to container if present
26
+ * 4. Returning the original response for downstream error handling
27
+ *
22
28
  * @returns A fetch function compatible with globalThis.fetch signature
23
29
  *
24
30
  * @example
@@ -0,0 +1,197 @@
1
+ import { a as y } from "./runtime-CmoG3v2m.js";
2
+ import { p as T, s as v, i as f } from "./ui-y5N62DqC.js";
3
+ const m = {
4
+ apiBaseUrl: "https://api.aippy.dev/api",
5
+ authToken: "",
6
+ currentUserId: null
7
+ };
8
+ function b(n) {
9
+ Object.assign(m, n);
10
+ }
11
+ function P(n) {
12
+ m.authToken = n;
13
+ }
14
+ function O(n) {
15
+ m.currentUserId = n;
16
+ }
17
+ const k = { iOS: "1.6.6", Android: "1.1.8" }, E = 3e3, S = 50, l = 500, a = { uid: "", token: "" };
18
+ let d = null, _ = !1, u = null, w = !1, o = null;
19
+ function g(n) {
20
+ if (!n || typeof n != "object") return null;
21
+ let e = n;
22
+ if (e.credentials && typeof e.credentials == "object" && (e = e.credentials), e.user && typeof e.user == "object" && e.token) {
23
+ const c = e.user;
24
+ e = { uid: c.uid || c.userId || c.id, token: e.token, apiBaseUrl: e.apiBaseUrl };
25
+ }
26
+ const s = e.uid || e.userId || e.id || "", t = e.token || e.authToken || "", r = e.apiBaseUrl;
27
+ if (!s || !t) return null;
28
+ const i = { uid: String(s), token: t, apiBaseUrl: r };
29
+ return O(i.uid), P(t), r && b({ apiBaseUrl: r }), d = i, _ = !0, i;
30
+ }
31
+ function h(n) {
32
+ if (!n || typeof n != "object") return null;
33
+ let e = n;
34
+ e.userInfo && typeof e.userInfo == "object" && (e = e.userInfo), e.user && typeof e.user == "object" && (e = e.user);
35
+ const s = e.uid || e.userId || e.id || "";
36
+ if (!s) return null;
37
+ const t = { uid: String(s), ...e };
38
+ return u = t, w = !0, t;
39
+ }
40
+ function p(n) {
41
+ throw console.warn(`[UserSDK] ${n}`), v("Your app version is outdated. Please upgrade to the latest version."), new Error("App version too old");
42
+ }
43
+ function L(n = l) {
44
+ return _ && d ? Promise.resolve(d) : new Promise((e) => {
45
+ const s = window.webkit?.messageHandlers?.aippyListener;
46
+ if (!s) {
47
+ e(a);
48
+ return;
49
+ }
50
+ const t = setTimeout(() => e(a), n);
51
+ y.receiveChannel.once("user.credentials", (r) => {
52
+ clearTimeout(t), e(g(r) || a);
53
+ });
54
+ try {
55
+ s.postMessage({
56
+ command: "user.getCredentials",
57
+ parameters: JSON.stringify({ timestamp: Date.now(), endpoint: "user.credentials" })
58
+ });
59
+ } catch {
60
+ clearTimeout(t), e(a);
61
+ }
62
+ });
63
+ }
64
+ function C(n = l) {
65
+ return new Promise((e) => {
66
+ if (!f()) {
67
+ e(a);
68
+ return;
69
+ }
70
+ const s = setTimeout(() => {
71
+ window.removeEventListener("message", t), e(a);
72
+ }, n), t = (r) => {
73
+ r.data?.type === "user-credentials-response" && (clearTimeout(s), window.removeEventListener("message", t), e(g(r.data) || a));
74
+ };
75
+ window.addEventListener("message", t);
76
+ try {
77
+ window.parent.postMessage({ type: "user-credentials-request", timestamp: Date.now() }, "*");
78
+ } catch {
79
+ clearTimeout(s), window.removeEventListener("message", t), e(a);
80
+ }
81
+ });
82
+ }
83
+ function D(n = !1, e = l) {
84
+ return w && u && !n ? Promise.resolve(u) : o ? new Promise((s) => {
85
+ const t = o.resolve;
86
+ o.resolve = (r) => {
87
+ t(r), s(r);
88
+ };
89
+ }) : new Promise((s) => {
90
+ const t = window.webkit?.messageHandlers?.aippyListener;
91
+ if (!t) {
92
+ s(null);
93
+ return;
94
+ }
95
+ const r = setTimeout(() => {
96
+ o = null, s(null);
97
+ }, e);
98
+ o = { resolve: (i) => {
99
+ clearTimeout(r), o = null, s(i);
100
+ } }, y.receiveChannel.once("user.info", (i) => {
101
+ console.log("[UserSDK] Received user.info via receiveChannel:", i), clearTimeout(r);
102
+ const c = h(i);
103
+ o && o.resolve(c);
104
+ });
105
+ try {
106
+ console.log("[UserSDK] Requesting user info from iOS"), t.postMessage({
107
+ command: "user.getUserInfo",
108
+ parameters: JSON.stringify({ timestamp: Date.now(), endpoint: "user.info" })
109
+ });
110
+ } catch {
111
+ clearTimeout(r), o = null, s(null);
112
+ }
113
+ });
114
+ }
115
+ function A(n = l) {
116
+ return new Promise((e) => {
117
+ if (!f()) {
118
+ e(null);
119
+ return;
120
+ }
121
+ const s = setTimeout(() => {
122
+ window.removeEventListener("message", t), e(null);
123
+ }, n), t = (r) => {
124
+ r.data?.type === "user-info-response" && (clearTimeout(s), window.removeEventListener("message", t), e(h(r.data)));
125
+ };
126
+ window.addEventListener("message", t);
127
+ try {
128
+ window.parent.postMessage({ type: "user-info-request", timestamp: Date.now() }, "*");
129
+ } catch {
130
+ clearTimeout(s), window.removeEventListener("message", t), e(null);
131
+ }
132
+ });
133
+ }
134
+ async function M() {
135
+ const n = T.checkAppEnvironment(k);
136
+ switch (n.type) {
137
+ case "new_app": {
138
+ n.isValid || p(n.message || "App version too old"), await L(E);
139
+ const e = d?.token || "";
140
+ if (!e) throw new Error("Token unavailable in app");
141
+ return e;
142
+ }
143
+ case "old_ios_app":
144
+ case "old_android_app":
145
+ throw p(n.message || "Old app version"), new Error("Token unavailable: Old app version not supported");
146
+ case "iframe": {
147
+ const e = await C(S);
148
+ if (e?.token) return e.token;
149
+ throw v("Please log in to use this feature."), new Error("Token unavailable: User needs to login");
150
+ }
151
+ default:
152
+ return "";
153
+ }
154
+ }
155
+ async function F(n = !1) {
156
+ if (w && u && !n) return u;
157
+ const e = T.checkAppEnvironment(k);
158
+ switch (e.type) {
159
+ case "new_app":
160
+ return e.isValid || p(e.message || "App version too old"), await D(n, E);
161
+ case "old_ios_app":
162
+ case "old_android_app":
163
+ return p(e.message || "Old app version"), u;
164
+ case "iframe":
165
+ return await A(S);
166
+ default:
167
+ return u;
168
+ }
169
+ }
170
+ async function B(n = l) {
171
+ return f() ? await A(n) : null;
172
+ }
173
+ function I(n) {
174
+ g(n);
175
+ }
176
+ function U(n) {
177
+ console.log("[UserSDK] processUserInfo called with:", n);
178
+ const e = h(n);
179
+ o && (o.resolve(e), o = null);
180
+ }
181
+ function j() {
182
+ typeof window > "u" || (window.processUserCredentials = I, window.processUserInfo = U, window.addEventListener("message", (n) => {
183
+ n.data?.type === "user-credentials" && I(n.data), n.data?.type === "user-info" && U(n.data);
184
+ }));
185
+ }
186
+ j();
187
+ M().catch((n) => {
188
+ console.warn("[UserSDK] Auth check on load:", n.message);
189
+ });
190
+ export {
191
+ M as getAuthTokenAsync,
192
+ F as getUserInfoAsync,
193
+ B as getUserInfoFromParent,
194
+ f as isInIframe,
195
+ I as processUserCredentials,
196
+ U as processUserInfo
197
+ };
@@ -24,7 +24,7 @@ function p(e) {
24
24
  }
25
25
  };
26
26
  }
27
- const r = "0.2.7-dev.2", a = {
27
+ const r = "0.2.7-dev.4", a = {
28
28
  version: r
29
29
  }, t = a.version, i = "@aippy/runtime";
30
30
  function c() {
@@ -0,0 +1,275 @@
1
+ import { createOpenAICompatible as l } from "@ai-sdk/openai-compatible";
2
+ import "react";
3
+ import { getAuthTokenAsync as d } from "./bridge-Ca3H2iN1.js";
4
+ import { AISDKError as p, DefaultChatTransport as y } from "ai";
5
+ import { s as c } from "./container-message-DGrno17o.js";
6
+ const f = "https://api.aippy.dev", h = `${f}/aisdk/v1/`, A = `${f}/aisdk/v1/ui/`, b = "gpt-5-nano", w = "";
7
+ function E(t = {}) {
8
+ return {
9
+ baseUrl: t.baseUrl ?? h
10
+ };
11
+ }
12
+ function I(t = {}) {
13
+ return {
14
+ baseUrl: t.baseUrl ?? A
15
+ };
16
+ }
17
+ function v(t, e) {
18
+ const s = t.status;
19
+ if (e && typeof e == "object" && "error" in e) {
20
+ const r = e, o = r.error.code, n = o != null ? `AippyAIError_${o}` : `AippyAIError_${s}`, a = e;
21
+ return "appMessage" in a && a.appMessage !== void 0 && c(a.appMessage), new p({
22
+ name: n,
23
+ message: r.error.message,
24
+ cause: {
25
+ type: r.error.type ?? void 0,
26
+ param: r.error.param ?? void 0,
27
+ status: s
28
+ }
29
+ });
30
+ }
31
+ if (e && typeof e == "object" && e !== null) {
32
+ const r = e;
33
+ "appMessage" in r && r.appMessage !== void 0 && c(r.appMessage);
34
+ }
35
+ return new p({
36
+ name: `AippyAIError_${s}`,
37
+ message: `Request failed with status ${s}`,
38
+ cause: { status: s }
39
+ });
40
+ }
41
+ function F() {
42
+ return new p({
43
+ name: "AippyAIError_MISSING_TOKEN",
44
+ message: "User token is required. Ensure user credentials are available via initUserBridge()."
45
+ });
46
+ }
47
+ function L() {
48
+ return new p({
49
+ name: "AippyAIError_REQUEST_ABORTED",
50
+ message: "Request was aborted"
51
+ });
52
+ }
53
+ function k(t) {
54
+ const e = t instanceof Error ? t.message : "Network request failed";
55
+ return new p({
56
+ name: "AippyAIError_NETWORK_ERROR",
57
+ message: e,
58
+ cause: t instanceof Error ? t : String(t)
59
+ });
60
+ }
61
+ function D(t) {
62
+ return new p({
63
+ name: "AippyAIError_PARSE_ERROR",
64
+ message: `Failed to parse response: ${t}`
65
+ });
66
+ }
67
+ function _(t, e) {
68
+ const s = t.endsWith("/") ? t : `${t}/`, r = e.startsWith("/") ? e.slice(1) : e;
69
+ return new URL(r, s).href;
70
+ }
71
+ function m() {
72
+ return async (t, e) => {
73
+ const s = await d(), r = new Headers(e?.headers);
74
+ r.set("Aippy-Runtime-Authorization", `Bearer ${s}`);
75
+ const o = await globalThis.fetch(t, { ...e, headers: r });
76
+ if (!o.ok) {
77
+ const a = await o.clone().text().catch(() => null);
78
+ if (a)
79
+ try {
80
+ const i = JSON.parse(a);
81
+ v(o, i);
82
+ } catch {
83
+ }
84
+ }
85
+ return o;
86
+ };
87
+ }
88
+ const S = [
89
+ "gpt-image-1",
90
+ "gpt-image-1-mini",
91
+ "gpt-image-1.5"
92
+ ];
93
+ function O(t) {
94
+ try {
95
+ const e = JSON.parse(t);
96
+ if (e.model && S.some((s) => e.model.startsWith(s))) {
97
+ const { response_format: s, ...r } = e;
98
+ return JSON.stringify(r);
99
+ }
100
+ } catch {
101
+ }
102
+ return t;
103
+ }
104
+ function B(t = {}) {
105
+ const { baseUrl: e } = E(t), s = m();
106
+ return l({
107
+ name: "aippy",
108
+ baseURL: e,
109
+ fetch: async (o, n) => o.toString().includes("/images/generations") && n?.method?.toUpperCase() === "POST" && n?.body ? s(o, {
110
+ ...n,
111
+ body: O(n.body)
112
+ }) : s(o, n),
113
+ // Enable structured outputs support (json_schema response format)
114
+ // This is required for Output.object() and Output.array() to work properly
115
+ supportsStructuredOutputs: !0
116
+ });
117
+ }
118
+ const C = "/chat";
119
+ function J(t = {}) {
120
+ const { baseUrl: e } = I(t), s = t.model ?? b, r = t.system ?? w, o = t.api ?? _(e, C), n = { model: s, system: r }, a = m();
121
+ return {
122
+ transport: new y({
123
+ api: o,
124
+ body: n,
125
+ // Ensure token is fetched fresh for every request
126
+ fetch: a
127
+ })
128
+ };
129
+ }
130
+ class u extends Error {
131
+ constructor(e, s) {
132
+ super(e), this.errors = s, this.name = "AIConfigValidationError";
133
+ }
134
+ }
135
+ function g(t) {
136
+ const e = [];
137
+ if (!t || typeof t != "object")
138
+ throw new u("AIConfig must be an object", []);
139
+ const s = t;
140
+ for (const [r, o] of Object.entries(s)) {
141
+ if (!o || typeof o != "object") {
142
+ e.push({ key: r, message: "Item must be an object" });
143
+ continue;
144
+ }
145
+ const n = o;
146
+ typeof n.index != "number" && e.push({ key: r, message: "index must be a number" }), typeof n.name != "string" && e.push({ key: r, message: "name must be a string" }), typeof n.description != "string" && e.push({ key: r, message: "description must be a string" });
147
+ const a = n.type;
148
+ if (!["number", "boolean", "text", "enum"].includes(a)) {
149
+ e.push({
150
+ key: r,
151
+ message: `type must be one of: number, boolean, text, enum (got: ${a})`
152
+ });
153
+ continue;
154
+ }
155
+ if (a === "number")
156
+ typeof n.value != "number" && e.push({ key: r, message: 'value must be a number for type "number"' }), n.min !== void 0 && typeof n.min != "number" && e.push({ key: r, message: "min must be a number" }), n.max !== void 0 && typeof n.max != "number" && e.push({ key: r, message: "max must be a number" }), n.step !== void 0 && typeof n.step != "number" && e.push({ key: r, message: "step must be a number" });
157
+ else if (a === "boolean")
158
+ typeof n.value != "boolean" && e.push({ key: r, message: 'value must be a boolean for type "boolean"' });
159
+ else if (a === "text")
160
+ typeof n.value != "string" && e.push({ key: r, message: 'value must be a string for type "text"' });
161
+ else if (a === "enum") {
162
+ if (!Array.isArray(n.options))
163
+ e.push({ key: r, message: 'options must be an array for type "enum"' });
164
+ else if (n.options.length === 0)
165
+ e.push({ key: r, message: 'options array cannot be empty for type "enum"' });
166
+ else
167
+ for (let i = 0; i < n.options.length; i++)
168
+ typeof n.options[i] != "string" && e.push({
169
+ key: r,
170
+ message: `options[${i}] must be a string`
171
+ });
172
+ typeof n.value != "string" ? e.push({ key: r, message: 'value must be a string for type "enum"' }) : Array.isArray(n.options) && !n.options.includes(n.value) && e.push({
173
+ key: r,
174
+ message: `value "${n.value}" is not in options: [${n.options.join(", ")}]`
175
+ });
176
+ }
177
+ n.group !== void 0 && typeof n.group != "string" && e.push({ key: r, message: "group must be a string" });
178
+ }
179
+ if (e.length > 0)
180
+ throw new u(
181
+ `AIConfig validation failed with ${e.length} error(s)`,
182
+ e
183
+ );
184
+ }
185
+ function P(t) {
186
+ let e;
187
+ try {
188
+ e = JSON.parse(t);
189
+ } catch (s) {
190
+ throw new u("Invalid JSON", [
191
+ {
192
+ key: "",
193
+ message: `JSON parse error: ${s instanceof Error ? s.message : String(s)}`
194
+ }
195
+ ]);
196
+ }
197
+ return g(e), e;
198
+ }
199
+ function R(t) {
200
+ return g(t), t;
201
+ }
202
+ function T(t, e) {
203
+ const s = t[e];
204
+ if (!s)
205
+ throw new Error(`AIConfig key "${e}" not found`);
206
+ return s.value;
207
+ }
208
+ function U(t) {
209
+ if (!(typeof window > "u"))
210
+ try {
211
+ const e = window.webkit?.messageHandlers?.aippyListener;
212
+ if (e)
213
+ try {
214
+ const s = {
215
+ command: "ai.initialize",
216
+ parameters: JSON.stringify(t)
217
+ };
218
+ e.postMessage(s);
219
+ } catch (s) {
220
+ console.warn("❌ [Aippy AI Config] Failed to send config to iOS app:", s);
221
+ }
222
+ if (window.parent && window.parent !== window)
223
+ try {
224
+ const s = {
225
+ type: "ai.initialize",
226
+ config: JSON.stringify(t)
227
+ };
228
+ window.parent.postMessage(s, "*");
229
+ } catch (s) {
230
+ console.warn("❌ [Aippy AI Config] Failed to send config to parent window:", s);
231
+ }
232
+ } catch (e) {
233
+ console.warn("⚠️ [Aippy AI Config] Failed to send config:", e);
234
+ }
235
+ }
236
+ function z(t) {
237
+ const e = R(t);
238
+ return U(e), new Proxy({}, {
239
+ get(s, r) {
240
+ if (typeof r == "string")
241
+ try {
242
+ return T(e, r);
243
+ } catch {
244
+ return;
245
+ }
246
+ },
247
+ has(s, r) {
248
+ return typeof r != "string" ? !1 : r in e;
249
+ },
250
+ ownKeys(s) {
251
+ return Object.keys(e);
252
+ }
253
+ });
254
+ }
255
+ export {
256
+ u as A,
257
+ h as D,
258
+ C as U,
259
+ B as a,
260
+ J as b,
261
+ A as c,
262
+ b as d,
263
+ w as e,
264
+ L as f,
265
+ k as g,
266
+ P as h,
267
+ T as i,
268
+ z as j,
269
+ R as l,
270
+ F as m,
271
+ v as n,
272
+ D as p,
273
+ U as s,
274
+ g as v
275
+ };
@@ -0,0 +1,46 @@
1
+ import { useState as l, useEffect as c } from "react";
2
+ let n = null, r = null;
3
+ function U() {
4
+ const [s, i] = l(n || {
5
+ avatar: "",
6
+ nickName: "用户",
7
+ username: "",
8
+ uid: ""
9
+ });
10
+ return c(() => {
11
+ let t = !0;
12
+ return (async () => {
13
+ try {
14
+ if (n) {
15
+ t && i(n);
16
+ return;
17
+ }
18
+ if (r) {
19
+ await r, t && n && i(n);
20
+ return;
21
+ }
22
+ r = (async () => {
23
+ const { getUserInfoAsync: a, getUserInfoFromParent: o, isInIframe: f } = await import("./bridge-Ca3H2iN1.js"), { hasNativeBridge: u } = await import("./native-bridge-BnvipFJc.js");
24
+ let e = null;
25
+ u() ? e = await a() : f() ? e = await o() : e = await a(), e && (n = {
26
+ avatar: String(e.avatar || e.photoUrl || ""),
27
+ nickName: String(e.nickName || e.displayName || "用户"),
28
+ username: String(e.username || ""),
29
+ uid: String(e.uid || "")
30
+ });
31
+ })(), await r, r = null, t && n && i(n);
32
+ } catch (a) {
33
+ console.error("[useUserInfo] Failed:", a), r = null;
34
+ }
35
+ })(), () => {
36
+ t = !1;
37
+ };
38
+ }, []), s;
39
+ }
40
+ function d() {
41
+ n = null, r = null;
42
+ }
43
+ export {
44
+ d as c,
45
+ U as u
46
+ };