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

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.
@@ -0,0 +1,56 @@
1
+ import "react";
2
+ import { getAuthTokenAsync as a } from "./bridge-BKcAlLAd.js";
3
+ const r = "0.2.7-dev.6", i = {
4
+ version: r
5
+ }, o = i.version, c = "@aippy/runtime";
6
+ function w() {
7
+ return {
8
+ name: c,
9
+ version: o,
10
+ buildTime: (/* @__PURE__ */ new Date()).toISOString()
11
+ };
12
+ }
13
+ function p(n) {
14
+ const { token: t, existingHeaders: e } = n, s = new Headers(e);
15
+ return s.set("Aippy-Runtime-Authorization", `Bearer ${t}`), s.set("Aippy-Runtime-SDK-Version", o), typeof navigator < "u" && navigator.userAgent && s.set("Aippy-Runtime-UA", navigator.userAgent), s;
16
+ }
17
+ async function m(n) {
18
+ const t = await a();
19
+ return p({ token: t, existingHeaders: n });
20
+ }
21
+ function u(n) {
22
+ if (!(typeof window > "u"))
23
+ try {
24
+ const t = window.webkit?.messageHandlers?.aippyListener;
25
+ if (t)
26
+ try {
27
+ const e = {
28
+ command: "container.message",
29
+ parameters: n
30
+ };
31
+ t.postMessage(e), console.log("📤 [Container Message] Sent message to iOS app container:", n);
32
+ } catch (e) {
33
+ console.warn("❌ [Container Message] Failed to send message to iOS app:", e);
34
+ }
35
+ if (window.parent && window.parent !== window)
36
+ try {
37
+ const e = {
38
+ type: "container.message",
39
+ data: n
40
+ };
41
+ window.parent.postMessage(e, "*"), console.log("📤 [Container Message] Sent message to parent window:", n);
42
+ } catch (e) {
43
+ console.warn("❌ [Container Message] Failed to send message to parent window:", e);
44
+ }
45
+ } catch (t) {
46
+ console.warn("⚠️ [Container Message] Failed to send message:", t);
47
+ }
48
+ }
49
+ export {
50
+ c as S,
51
+ o as V,
52
+ m as a,
53
+ p as c,
54
+ w as g,
55
+ u as s
56
+ };
@@ -0,0 +1,31 @@
1
+ /**
2
+ * Aippy Runtime Headers utilities.
3
+ * Shared header generation for all SDK modules.
4
+ */
5
+ export interface CreateAippyHeadersOptions {
6
+ /** Auth token for Authorization header */
7
+ token: string;
8
+ /** Optional existing headers to extend */
9
+ existingHeaders?: HeadersInit;
10
+ }
11
+ /**
12
+ * Creates Aippy runtime headers with authentication and metadata.
13
+ * Can be used by any module to create consistent headers.
14
+ *
15
+ * Headers set:
16
+ * - Aippy-Runtime-Authorization: Bearer token for authentication
17
+ * - Aippy-Runtime-SDK-Version: SDK version number
18
+ * - Aippy-Runtime-UA: User agent from the shell app
19
+ *
20
+ * @param options - Options for creating headers
21
+ * @returns Headers object with Aippy runtime headers
22
+ */
23
+ export declare function createAippyHeaders(options: CreateAippyHeadersOptions): Headers;
24
+ /**
25
+ * Async version that automatically gets the auth token.
26
+ * Convenient for most use cases.
27
+ *
28
+ * @param existingHeaders - Optional existing headers to extend
29
+ * @returns Promise resolving to Headers object with Aippy runtime headers
30
+ */
31
+ export declare function createAippyHeadersAsync(existingHeaders?: HeadersInit): Promise<Headers>;
@@ -2,5 +2,6 @@ export * from './types';
2
2
  export * from './config';
3
3
  export * from './errors';
4
4
  export * from './version';
5
+ export * from './headers';
5
6
  export * from './container-message';
6
7
  export * from './runtime';
@@ -1,53 +1,23 @@
1
- import { A as d, E as v, c as m } from "../errors-CDEBaBxB.js";
2
- import { s as A } from "../container-message-DGrno17o.js";
3
- import { A as g, C as R, R as I, a as P, p as l } from "../runtime-CmoG3v2m.js";
4
- const s = {
5
- mode: "development",
6
- debug: !1,
7
- apiBaseUrl: void 0,
8
- headers: {}
9
- };
10
- function o() {
11
- const e = {};
12
- return typeof process < "u" && process.env && (process.env.NODE_ENV && (e.mode = process.env.NODE_ENV), process.env.AIPPY_DEBUG && (e.debug = process.env.AIPPY_DEBUG === "true"), process.env.AIPPY_API_BASE_URL && (e.apiBaseUrl = process.env.AIPPY_API_BASE_URL)), e;
13
- }
14
- function p(e) {
15
- const n = o();
16
- return {
17
- ...s,
18
- ...n,
19
- ...e,
20
- headers: {
21
- ...s.headers,
22
- ...n.headers,
23
- ...e?.headers
24
- }
25
- };
26
- }
27
- const r = "0.2.7-dev.4", a = {
28
- version: r
29
- }, t = a.version, i = "@aippy/runtime";
30
- function c() {
31
- return {
32
- name: i,
33
- version: t,
34
- buildTime: (/* @__PURE__ */ new Date()).toISOString()
35
- };
36
- }
1
+ import { D as s, g as r, m as o } from "../config-B0A7gHQM.js";
2
+ import { A as p, E as t, c as i } from "../errors-CDEBaBxB.js";
3
+ import { S as c, V as A, c as E, a as R, g, s as C } from "../container-message-WJolNXso.js";
4
+ import { A as y, C as D, R as l, a as x, p as O } from "../runtime-CmoG3v2m.js";
37
5
  export {
38
- g as AippyRuntime,
39
- d as AippyRuntimeError,
40
- R as Cancellable,
6
+ y as AippyRuntime,
7
+ p as AippyRuntimeError,
8
+ D as Cancellable,
41
9
  s as DEFAULT_CONFIG,
42
- v as ERROR_CODES,
43
- I as ReceiveChannel,
44
- i as SDK_NAME,
45
- t as VERSION,
46
- P as aippyRuntime,
47
- m as createError,
48
- o as getConfigFromEnv,
49
- c as getVersionInfo,
50
- p as mergeConfig,
51
- l as processMotionData,
52
- A as sendMessageToContainer
10
+ t as ERROR_CODES,
11
+ l as ReceiveChannel,
12
+ c as SDK_NAME,
13
+ A as VERSION,
14
+ x as aippyRuntime,
15
+ E as createAippyHeaders,
16
+ R as createAippyHeadersAsync,
17
+ i as createError,
18
+ r as getConfigFromEnv,
19
+ g as getVersionInfo,
20
+ o as mergeConfig,
21
+ O as processMotionData,
22
+ C as sendMessageToContainer
53
23
  };
@@ -0,0 +1,240 @@
1
+ import { createOpenAICompatible as l } from "@ai-sdk/openai-compatible";
2
+ import { s as u, a as y } from "./container-message-WJolNXso.js";
3
+ import { AISDKError as f, DefaultChatTransport as d } from "ai";
4
+ import { j as h } from "./url-c26cuIpu.js";
5
+ const c = "https://api.aippy.dev", b = `${c}/aisdk/v1/`, A = `${c}/aisdk/v1/ui/`, w = "gpt-5-nano", v = "";
6
+ function I(n = {}) {
7
+ return {
8
+ baseUrl: n.baseUrl ?? b
9
+ };
10
+ }
11
+ function C(n = {}) {
12
+ return {
13
+ baseUrl: n.baseUrl ?? A
14
+ };
15
+ }
16
+ function E(n, e) {
17
+ const r = n.status;
18
+ if (e && typeof e == "object" && "error" in e) {
19
+ const t = e, o = t.error.code, s = o != null ? `AippyAIError_${o}` : `AippyAIError_${r}`, a = e;
20
+ return "appMessage" in a && a.appMessage !== void 0 && u(a.appMessage), new f({
21
+ name: s,
22
+ message: t.error.message,
23
+ cause: {
24
+ type: t.error.type ?? void 0,
25
+ param: t.error.param ?? void 0,
26
+ status: r
27
+ }
28
+ });
29
+ }
30
+ if (e && typeof e == "object" && e !== null) {
31
+ const t = e;
32
+ "appMessage" in t && t.appMessage !== void 0 && u(t.appMessage);
33
+ }
34
+ return new f({
35
+ name: `AippyAIError_${r}`,
36
+ message: `Request failed with status ${r}`,
37
+ cause: { status: r }
38
+ });
39
+ }
40
+ function m() {
41
+ return async (n, e) => {
42
+ const r = await y(e?.headers), t = await globalThis.fetch(n, { ...e, headers: r });
43
+ if (!t.ok) {
44
+ const o = await t.text().catch(() => null);
45
+ let s = null;
46
+ if (o)
47
+ try {
48
+ s = JSON.parse(o);
49
+ } catch {
50
+ s = { message: o };
51
+ }
52
+ throw E(t, s);
53
+ }
54
+ return t;
55
+ };
56
+ }
57
+ const S = [
58
+ "gpt-image-1",
59
+ "gpt-image-1-mini",
60
+ "gpt-image-1.5"
61
+ ];
62
+ function O(n) {
63
+ try {
64
+ const e = JSON.parse(n);
65
+ if (e.model && S.some((r) => e.model.startsWith(r))) {
66
+ const { response_format: r, ...t } = e;
67
+ return JSON.stringify(t);
68
+ }
69
+ } catch {
70
+ }
71
+ return n;
72
+ }
73
+ function L(n = {}) {
74
+ const { baseUrl: e } = I(n), r = m();
75
+ return l({
76
+ name: "aippy",
77
+ baseURL: e,
78
+ fetch: async (o, s) => o.toString().includes("/images/generations") && s?.method?.toUpperCase() === "POST" && s?.body ? r(o, {
79
+ ...s,
80
+ body: O(s.body)
81
+ }) : r(o, s),
82
+ // Enable structured outputs support (json_schema response format)
83
+ // This is required for Output.object() and Output.array() to work properly
84
+ supportsStructuredOutputs: !0
85
+ });
86
+ }
87
+ const _ = "/chat";
88
+ function N(n = {}) {
89
+ const { baseUrl: e } = C(n), r = n.model ?? w, t = n.system ?? v, o = n.api ?? h(e, _), s = { model: r, system: t }, a = m();
90
+ return {
91
+ transport: new d({
92
+ api: o,
93
+ body: s,
94
+ // Ensure token is fetched fresh for every request
95
+ fetch: a
96
+ })
97
+ };
98
+ }
99
+ class p extends Error {
100
+ constructor(e, r) {
101
+ super(e), this.errors = r, this.name = "AIConfigValidationError";
102
+ }
103
+ }
104
+ function g(n) {
105
+ const e = [];
106
+ if (!n || typeof n != "object")
107
+ throw new p("AIConfig must be an object", []);
108
+ const r = n;
109
+ for (const [t, o] of Object.entries(r)) {
110
+ if (!o || typeof o != "object") {
111
+ e.push({ key: t, message: "Item must be an object" });
112
+ continue;
113
+ }
114
+ const s = o;
115
+ typeof s.index != "number" && e.push({ key: t, message: "index must be a number" }), typeof s.name != "string" && e.push({ key: t, message: "name must be a string" }), typeof s.description != "string" && e.push({ key: t, message: "description must be a string" });
116
+ const a = s.type;
117
+ if (!["number", "boolean", "text", "enum"].includes(a)) {
118
+ e.push({
119
+ key: t,
120
+ message: `type must be one of: number, boolean, text, enum (got: ${a})`
121
+ });
122
+ continue;
123
+ }
124
+ if (a === "number")
125
+ typeof s.value != "number" && e.push({ key: t, message: 'value must be a number for type "number"' }), s.min !== void 0 && typeof s.min != "number" && e.push({ key: t, message: "min must be a number" }), s.max !== void 0 && typeof s.max != "number" && e.push({ key: t, message: "max must be a number" }), s.step !== void 0 && typeof s.step != "number" && e.push({ key: t, message: "step must be a number" });
126
+ else if (a === "boolean")
127
+ typeof s.value != "boolean" && e.push({ key: t, message: 'value must be a boolean for type "boolean"' });
128
+ else if (a === "text")
129
+ typeof s.value != "string" && e.push({ key: t, message: 'value must be a string for type "text"' });
130
+ else if (a === "enum") {
131
+ if (!Array.isArray(s.options))
132
+ e.push({ key: t, message: 'options must be an array for type "enum"' });
133
+ else if (s.options.length === 0)
134
+ e.push({ key: t, message: 'options array cannot be empty for type "enum"' });
135
+ else
136
+ for (let i = 0; i < s.options.length; i++)
137
+ typeof s.options[i] != "string" && e.push({
138
+ key: t,
139
+ message: `options[${i}] must be a string`
140
+ });
141
+ typeof s.value != "string" ? e.push({ key: t, message: 'value must be a string for type "enum"' }) : Array.isArray(s.options) && !s.options.includes(s.value) && e.push({
142
+ key: t,
143
+ message: `value "${s.value}" is not in options: [${s.options.join(", ")}]`
144
+ });
145
+ }
146
+ s.group !== void 0 && typeof s.group != "string" && e.push({ key: t, message: "group must be a string" });
147
+ }
148
+ if (e.length > 0)
149
+ throw new p(
150
+ `AIConfig validation failed with ${e.length} error(s)`,
151
+ e
152
+ );
153
+ }
154
+ function D(n) {
155
+ let e;
156
+ try {
157
+ e = JSON.parse(n);
158
+ } catch (r) {
159
+ throw new p("Invalid JSON", [
160
+ {
161
+ key: "",
162
+ message: `JSON parse error: ${r instanceof Error ? r.message : String(r)}`
163
+ }
164
+ ]);
165
+ }
166
+ return g(e), e;
167
+ }
168
+ function U(n) {
169
+ return g(n), n;
170
+ }
171
+ function T(n, e) {
172
+ const r = n[e];
173
+ if (!r)
174
+ throw new Error(`AIConfig key "${e}" not found`);
175
+ return r.value;
176
+ }
177
+ function x(n) {
178
+ if (!(typeof window > "u"))
179
+ try {
180
+ const e = window.webkit?.messageHandlers?.aippyListener;
181
+ if (e)
182
+ try {
183
+ const r = {
184
+ command: "ai.initialize",
185
+ parameters: JSON.stringify(n)
186
+ };
187
+ e.postMessage(r);
188
+ } catch (r) {
189
+ console.warn("❌ [Aippy AI Config] Failed to send config to iOS app:", r);
190
+ }
191
+ if (window.parent && window.parent !== window)
192
+ try {
193
+ const r = {
194
+ type: "ai.initialize",
195
+ config: JSON.stringify(n)
196
+ };
197
+ window.parent.postMessage(r, "*");
198
+ } catch (r) {
199
+ console.warn("❌ [Aippy AI Config] Failed to send config to parent window:", r);
200
+ }
201
+ } catch (e) {
202
+ console.warn("⚠️ [Aippy AI Config] Failed to send config:", e);
203
+ }
204
+ }
205
+ function J(n) {
206
+ const e = U(n);
207
+ return x(e), new Proxy({}, {
208
+ get(r, t) {
209
+ if (typeof t == "string")
210
+ try {
211
+ return T(e, t);
212
+ } catch {
213
+ return;
214
+ }
215
+ },
216
+ has(r, t) {
217
+ return typeof t != "string" ? !1 : t in e;
218
+ },
219
+ ownKeys(r) {
220
+ return Object.keys(e);
221
+ }
222
+ });
223
+ }
224
+ export {
225
+ p as A,
226
+ b as D,
227
+ _ as U,
228
+ L as a,
229
+ N as b,
230
+ A as c,
231
+ w as d,
232
+ v as e,
233
+ J as f,
234
+ T as g,
235
+ U as l,
236
+ E as n,
237
+ D as p,
238
+ x as s,
239
+ g as v
240
+ };
@@ -0,0 +1,98 @@
1
+ import { useState as U, useEffect as y, useCallback as p } from "react";
2
+ const g = {
3
+ avatar: "",
4
+ nickName: "用户",
5
+ username: "",
6
+ uid: ""
7
+ };
8
+ let s = null, a = null;
9
+ const d = /* @__PURE__ */ new Set();
10
+ function h() {
11
+ d.forEach((n) => n());
12
+ }
13
+ const u = [100, 300, 500, 1e3], w = (n) => new Promise((o) => setTimeout(o, n)), A = () => /Aippy\/[\d.]+\/\d+\/Android/i.test(navigator.userAgent);
14
+ async function v() {
15
+ console.log("🔍 [useUserInfo] fetchUserInfoFromBridge started");
16
+ const n = A();
17
+ try {
18
+ const { getUserInfoAsync: o, getUserInfoFromParent: c, isInIframe: t } = await import("./bridge-BKcAlLAd.js"), { hasNativeBridge: f } = await import("./native-bridge-BnvipFJc.js"), i = f(), l = t();
19
+ console.log("🔍 [useUserInfo] Environment check:", {
20
+ hasNativeBridge: i,
21
+ isInIframe: l,
22
+ isAndroid: n,
23
+ userAgent: navigator.userAgent
24
+ });
25
+ let e = null;
26
+ if (i)
27
+ console.log("📱 [useUserInfo] Using native bridge to get user info..."), e = await o(), console.log("📱 [useUserInfo] Native bridge returned:", e);
28
+ else if (l)
29
+ console.log("🖼️ [useUserInfo] Using iframe parent to get user info..."), e = await c(), console.log("🖼️ [useUserInfo] Iframe parent returned:", e);
30
+ else {
31
+ if (console.log("🌐 [useUserInfo] Bridge not ready, trying getUserInfoAsync..."), e = await o(), !e && n) {
32
+ console.log("🤖 [useUserInfo] Android: First attempt failed, retrying...");
33
+ for (let r = 0; r < u.length; r++) {
34
+ const I = u[r];
35
+ if (console.log(`🔄 [useUserInfo] Android retry ${r + 1}/${u.length} after ${I}ms...`), await w(I), e = await o(), e) {
36
+ console.log(`✅ [useUserInfo] Retry ${r + 1} succeeded:`, e);
37
+ break;
38
+ }
39
+ }
40
+ }
41
+ console.log("🌐 [useUserInfo] Result:", e);
42
+ }
43
+ if (e) {
44
+ const r = {
45
+ avatar: String(e.avatar || e.photoUrl || ""),
46
+ nickName: String(e.nickName || e.nickname || e.displayName || "用户"),
47
+ username: String(e.username || ""),
48
+ uid: String(e.uid || "")
49
+ };
50
+ return console.log("✅ [useUserInfo] Parsed user info:", r), r;
51
+ }
52
+ return console.warn("⚠️ [useUserInfo] No user info received (info is null/undefined)"), null;
53
+ } catch (o) {
54
+ return console.error("❌ [useUserInfo] fetchUserInfoFromBridge failed:", o), null;
55
+ }
56
+ }
57
+ async function m() {
58
+ if (console.log("🔄 [useUserInfo] getOrFetchUserInfo called, cachedUserInfo:", s), s)
59
+ return console.log("✅ [useUserInfo] Returning cached user info"), s;
60
+ if (a) {
61
+ console.log("⏳ [useUserInfo] Already fetching, waiting for existing promise...");
62
+ const n = await a;
63
+ return console.log("⏳ [useUserInfo] Existing promise resolved:", n), n || g;
64
+ }
65
+ console.log("🚀 [useUserInfo] Starting new fetch..."), a = v();
66
+ try {
67
+ const n = await a;
68
+ return console.log("🚀 [useUserInfo] Fetch completed:", n), n && (s = n, h()), n || g;
69
+ } finally {
70
+ a = null;
71
+ }
72
+ }
73
+ function N() {
74
+ const [n, o] = U(s || g), [c, t] = U(!s);
75
+ y(() => {
76
+ const i = () => {
77
+ s && (o(s), t(!1));
78
+ };
79
+ return d.add(i), s || (t(!0), m().then((l) => {
80
+ o(l), t(!1);
81
+ })), () => {
82
+ d.delete(i);
83
+ };
84
+ }, []);
85
+ const f = p(async () => {
86
+ t(!0), s = null, a = null;
87
+ const i = await m();
88
+ o(i), t(!1);
89
+ }, []);
90
+ return { ...n, isLoading: c, refetch: f };
91
+ }
92
+ function S() {
93
+ s = null, a = null, h();
94
+ }
95
+ export {
96
+ S as c,
97
+ N as u
98
+ };