@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.
@@ -1,264 +0,0 @@
1
- import { createOpenAICompatible as l } from "@ai-sdk/openai-compatible";
2
- import "react";
3
- import { h as d } from "./bridge-N9ELFpfV.js";
4
- import { DefaultChatTransport as y, AISDKError as i } from "ai";
5
- import { s as u } from "./container-message-DGrno17o.js";
6
- const m = "https://api.aippy.dev", h = `${m}/aisdk/v1/`, A = `${m}/aisdk/v1/ui/`, b = "gpt-5-nano", E = "";
7
- function w(r = {}) {
8
- return {
9
- baseUrl: r.baseUrl ?? h
10
- };
11
- }
12
- function I(r = {}) {
13
- return {
14
- baseUrl: r.baseUrl ?? A
15
- };
16
- }
17
- function v(r, e) {
18
- const n = r.endsWith("/") ? r : `${r}/`, t = e.startsWith("/") ? e.slice(1) : e;
19
- return new URL(t, n).href;
20
- }
21
- function c() {
22
- return async (r, e) => {
23
- const n = await d(), t = new Headers(e?.headers);
24
- return t.set("Authorization", `Bearer ${n}`), globalThis.fetch(r, { ...e, headers: t });
25
- };
26
- }
27
- const _ = [
28
- "gpt-image-1",
29
- "gpt-image-1-mini",
30
- "gpt-image-1.5"
31
- ];
32
- function S(r) {
33
- try {
34
- const e = JSON.parse(r);
35
- if (e.model && _.some((n) => e.model.startsWith(n))) {
36
- const { response_format: n, ...t } = e;
37
- return JSON.stringify(t);
38
- }
39
- } catch {
40
- }
41
- return r;
42
- }
43
- function j(r = {}) {
44
- const { baseUrl: e } = w(r), n = c();
45
- return l({
46
- name: "aippy",
47
- baseURL: e,
48
- fetch: async (a, s) => a.toString().includes("/images/generations") && s?.method?.toUpperCase() === "POST" && s?.body ? n(a, {
49
- ...s,
50
- body: S(s.body)
51
- }) : n(a, s),
52
- // Enable structured outputs support (json_schema response format)
53
- // This is required for Output.object() and Output.array() to work properly
54
- supportsStructuredOutputs: !0
55
- });
56
- }
57
- const C = "/chat";
58
- function F(r = {}) {
59
- const { baseUrl: e } = I(r), n = r.model ?? b, t = r.system ?? E, a = r.api ?? v(e, C), s = { model: n, system: t }, o = c();
60
- return {
61
- transport: new y({
62
- api: a,
63
- body: s,
64
- // Ensure token is fetched fresh for every request
65
- fetch: o
66
- })
67
- };
68
- }
69
- function L(r, e) {
70
- const n = r.status;
71
- if (e && typeof e == "object" && "error" in e) {
72
- const t = e, a = t.error.code, s = a != null ? `AippyAIError_${a}` : `AippyAIError_${n}`, o = e;
73
- return "appMessage" in o && o.appMessage !== void 0 ? u(o.appMessage) : "containerErrorData" in o && o.containerErrorData !== void 0 && u(o.containerErrorData), new i({
74
- name: s,
75
- message: t.error.message,
76
- cause: {
77
- type: t.error.type ?? void 0,
78
- param: t.error.param ?? void 0,
79
- status: n
80
- }
81
- });
82
- }
83
- if (e && typeof e == "object" && e !== null) {
84
- const t = e;
85
- "appMessage" in t && t.appMessage !== void 0 ? u(t.appMessage) : "containerErrorData" in t && t.containerErrorData !== void 0 && u(t.containerErrorData);
86
- }
87
- return new i({
88
- name: `AippyAIError_${n}`,
89
- message: `Request failed with status ${n}`,
90
- cause: { status: n }
91
- });
92
- }
93
- function x() {
94
- return new i({
95
- name: "AippyAIError_MISSING_TOKEN",
96
- message: "User token is required. Ensure user credentials are available via initUserBridge()."
97
- });
98
- }
99
- function k() {
100
- return new i({
101
- name: "AippyAIError_REQUEST_ABORTED",
102
- message: "Request was aborted"
103
- });
104
- }
105
- function B(r) {
106
- const e = r instanceof Error ? r.message : "Network request failed";
107
- return new i({
108
- name: "AippyAIError_NETWORK_ERROR",
109
- message: e,
110
- cause: r instanceof Error ? r : String(r)
111
- });
112
- }
113
- function P(r) {
114
- return new i({
115
- name: "AippyAIError_PARSE_ERROR",
116
- message: `Failed to parse response: ${r}`
117
- });
118
- }
119
- class f extends Error {
120
- constructor(e, n) {
121
- super(e), this.errors = n, this.name = "AIConfigValidationError";
122
- }
123
- }
124
- function g(r) {
125
- const e = [];
126
- if (!r || typeof r != "object")
127
- throw new f("AIConfig must be an object", []);
128
- const n = r;
129
- for (const [t, a] of Object.entries(n)) {
130
- if (!a || typeof a != "object") {
131
- e.push({ key: t, message: "Item must be an object" });
132
- continue;
133
- }
134
- const s = a;
135
- 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" });
136
- const o = s.type;
137
- if (!["number", "boolean", "text", "enum"].includes(o)) {
138
- e.push({
139
- key: t,
140
- message: `type must be one of: number, boolean, text, enum (got: ${o})`
141
- });
142
- continue;
143
- }
144
- if (o === "number")
145
- 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" });
146
- else if (o === "boolean")
147
- typeof s.value != "boolean" && e.push({ key: t, message: 'value must be a boolean for type "boolean"' });
148
- else if (o === "text")
149
- typeof s.value != "string" && e.push({ key: t, message: 'value must be a string for type "text"' });
150
- else if (o === "enum") {
151
- if (!Array.isArray(s.options))
152
- e.push({ key: t, message: 'options must be an array for type "enum"' });
153
- else if (s.options.length === 0)
154
- e.push({ key: t, message: 'options array cannot be empty for type "enum"' });
155
- else
156
- for (let p = 0; p < s.options.length; p++)
157
- typeof s.options[p] != "string" && e.push({
158
- key: t,
159
- message: `options[${p}] must be a string`
160
- });
161
- 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({
162
- key: t,
163
- message: `value "${s.value}" is not in options: [${s.options.join(", ")}]`
164
- });
165
- }
166
- s.group !== void 0 && typeof s.group != "string" && e.push({ key: t, message: "group must be a string" });
167
- }
168
- if (e.length > 0)
169
- throw new f(
170
- `AIConfig validation failed with ${e.length} error(s)`,
171
- e
172
- );
173
- }
174
- function J(r) {
175
- let e;
176
- try {
177
- e = JSON.parse(r);
178
- } catch (n) {
179
- throw new f("Invalid JSON", [
180
- {
181
- key: "",
182
- message: `JSON parse error: ${n instanceof Error ? n.message : String(n)}`
183
- }
184
- ]);
185
- }
186
- return g(e), e;
187
- }
188
- function O(r) {
189
- return g(r), r;
190
- }
191
- function T(r, e) {
192
- const n = r[e];
193
- if (!n)
194
- throw new Error(`AIConfig key "${e}" not found`);
195
- return n.value;
196
- }
197
- function U(r) {
198
- if (!(typeof window > "u"))
199
- try {
200
- const e = window.webkit?.messageHandlers?.aippyListener;
201
- if (e)
202
- try {
203
- const n = {
204
- command: "ai.initialize",
205
- parameters: JSON.stringify(r)
206
- };
207
- e.postMessage(n);
208
- } catch (n) {
209
- console.warn("❌ [Aippy AI Config] Failed to send config to iOS app:", n);
210
- }
211
- if (window.parent && window.parent !== window)
212
- try {
213
- const n = {
214
- type: "ai.initialize",
215
- config: JSON.stringify(r)
216
- };
217
- window.parent.postMessage(n, "*");
218
- } catch (n) {
219
- console.warn("❌ [Aippy AI Config] Failed to send config to parent window:", n);
220
- }
221
- } catch (e) {
222
- console.warn("⚠️ [Aippy AI Config] Failed to send config:", e);
223
- }
224
- }
225
- function z(r) {
226
- const e = O(r);
227
- return U(e), new Proxy({}, {
228
- get(n, t) {
229
- if (typeof t == "string")
230
- try {
231
- return T(e, t);
232
- } catch {
233
- return;
234
- }
235
- },
236
- has(n, t) {
237
- return typeof t != "string" ? !1 : t in e;
238
- },
239
- ownKeys(n) {
240
- return Object.keys(e);
241
- }
242
- });
243
- }
244
- export {
245
- f as A,
246
- h as D,
247
- C as U,
248
- j as a,
249
- F as b,
250
- A as c,
251
- b as d,
252
- E as e,
253
- k as f,
254
- B as g,
255
- J as h,
256
- T as i,
257
- z as j,
258
- O as l,
259
- x as m,
260
- L as n,
261
- P as p,
262
- U as s,
263
- g as v
264
- };
@@ -1,9 +0,0 @@
1
- import { UserProfile } from './types';
2
- /**
3
- * Fetch user profile by ID from API
4
- */
5
- export declare function fetchUserProfile(userId: string): Promise<UserProfile | null>;
6
- /**
7
- * Clear profile cache
8
- */
9
- export declare function clearProfileCache(userId?: string): void;
@@ -1,6 +0,0 @@
1
- import { UserSessionInfoInterface } from './types';
2
- /**
3
- * User session info object
4
- * Provides synchronous access to current user data and utilities
5
- */
6
- export declare const userSessionInfo: UserSessionInfoInterface;
@@ -1,229 +0,0 @@
1
- import { useState as o, useEffect as y, useCallback as U } from "react";
2
- import { g as I, b as k } from "./bridge-N9ELFpfV.js";
3
- const w = /* @__PURE__ */ new Map();
4
- async function S(e) {
5
- const t = I(), n = w.get(e);
6
- if (n && Date.now() - n.timestamp < (t.cacheTime || 3e5))
7
- return n.data;
8
- try {
9
- const r = await fetch(
10
- `${t.apiBaseUrl}/user/profile?targetUid=${e}`,
11
- {
12
- method: "GET",
13
- headers: {
14
- Authorization: `Bearer ${t.authToken}`,
15
- "Content-Type": "application/json"
16
- }
17
- }
18
- );
19
- if (!r.ok)
20
- throw new Error(`Failed to fetch user profile: ${r.status}`);
21
- const a = await r.json(), i = C(a, e);
22
- return w.set(e, { data: i, timestamp: Date.now() }), i;
23
- } catch (r) {
24
- return console.error("[UserSDK] Failed to fetch user profile:", r), null;
25
- }
26
- }
27
- function C(e, t) {
28
- const n = typeof e == "object" && e !== null ? e : {}, r = n.data || n.user || n;
29
- return {
30
- id: r.id || r.uid || t,
31
- displayName: r.displayName || r.nickname || r.name || "Unknown User",
32
- username: r.username || r.id || t,
33
- photoUrl: r.photoUrl || r.avatar || r.avatarUrl,
34
- online: r.online ?? !1,
35
- bio: r.bio,
36
- coverImageUrl: r.coverImageUrl,
37
- ...r
38
- };
39
- }
40
- function D(e) {
41
- e ? w.delete(e) : w.clear();
42
- }
43
- function M() {
44
- const [e, t] = o(
45
- I().currentUserId
46
- );
47
- return y(() => {
48
- t(I().currentUserId);
49
- }, []), e;
50
- }
51
- function N() {
52
- const [e, t] = o(k());
53
- return y(() => {
54
- t(k());
55
- }, []), e;
56
- }
57
- function T() {
58
- const e = M(), [t, n] = o(null), [r, a] = o(!0), [i, u] = o(!1), [h, s] = o(null), l = U(async () => {
59
- if (!e) {
60
- n(null), a(!1);
61
- return;
62
- }
63
- a(!0), u(!1), s(null);
64
- try {
65
- const c = await S(e);
66
- n(c);
67
- } catch (c) {
68
- u(!0), s(c instanceof Error ? c : new Error(String(c)));
69
- } finally {
70
- a(!1);
71
- }
72
- }, [e]);
73
- y(() => {
74
- l();
75
- }, [l]);
76
- const f = U(async () => {
77
- e && D(e), await l();
78
- }, [e, l]);
79
- return { data: t, isLoading: r, isError: i, error: h, refetch: f };
80
- }
81
- function b(e) {
82
- const { id: t } = e, [n, r] = o(null), [a, i] = o(!0), [u, h] = o(!1), [s, l] = o(null), f = U(async () => {
83
- if (!t) {
84
- r(null), i(!1);
85
- return;
86
- }
87
- i(!0), h(!1), l(null);
88
- try {
89
- const d = await S(t);
90
- r(d);
91
- } catch (d) {
92
- h(!0), l(d instanceof Error ? d : new Error(String(d)));
93
- } finally {
94
- i(!1);
95
- }
96
- }, [t]);
97
- y(() => {
98
- f();
99
- }, [f]);
100
- const c = U(async () => {
101
- D(t), await f();
102
- }, [t, f]);
103
- return { data: n, isLoading: a, isError: u, error: s, refetch: c };
104
- }
105
- function A(e = {}) {
106
- const { includeSelf: t = !0 } = e, n = M(), [r, a] = o([]), [i, u] = o(!0), [h, s] = o(!1), [l, f] = o(null), c = U(async () => {
107
- u(!0), s(!1), f(null);
108
- try {
109
- const m = [];
110
- if (t && n) {
111
- const E = await S(n);
112
- E && m.push(E);
113
- }
114
- a(m);
115
- } catch (m) {
116
- s(!0), f(m instanceof Error ? m : new Error(String(m)));
117
- } finally {
118
- u(!1);
119
- }
120
- }, [t, n]);
121
- y(() => {
122
- c();
123
- }, [c]);
124
- const d = U(async () => {
125
- D(), await c();
126
- }, [c]);
127
- return { data: r, isLoading: i, isError: h, error: l, refetch: d };
128
- }
129
- function L() {
130
- return I().currentUserId;
131
- }
132
- let g = null, p = null;
133
- function j() {
134
- const [e, t] = o(g || {
135
- avatar: "",
136
- nickName: "用户",
137
- username: "",
138
- uid: ""
139
- });
140
- return y(() => {
141
- let n = !0;
142
- return (async () => {
143
- try {
144
- if (g) {
145
- console.log("[useUserInfo] Using cached user info"), n && t(g);
146
- return;
147
- }
148
- if (p) {
149
- console.log("[useUserInfo] Waiting for existing request"), await p, n && g && t(g);
150
- return;
151
- }
152
- console.log("[useUserInfo] Fetching user info"), p = (async () => {
153
- const { getUserInfoAsync: a, getUserInfoFromParent: i, isInIframe: u } = await import("./bridge-N9ELFpfV.js").then((l) => l.x), { hasNativeBridge: h } = await import("./native-bridge-BnvipFJc.js");
154
- let s = null;
155
- h() ? s = await a() : u() ? s = await i() : s = await a(), s && (g = {
156
- avatar: String(s.avatar || s.photoUrl || ""),
157
- nickName: String(s.nickName || s.displayName || "用户"),
158
- username: String(s.username || ""),
159
- uid: String(s.uid || "")
160
- });
161
- })(), await p, p = null, n && g && t(g);
162
- } catch (a) {
163
- console.error("[useUserInfo] Failed to fetch user info:", a), p = null;
164
- }
165
- })(), () => {
166
- n = !1;
167
- };
168
- }, []), e;
169
- }
170
- function B() {
171
- g = null, p = null, console.log("[useUserInfo] Cache cleared");
172
- }
173
- function P(e, t) {
174
- const n = [
175
- "Jan",
176
- "Feb",
177
- "Mar",
178
- "Apr",
179
- "May",
180
- "Jun",
181
- "Jul",
182
- "Aug",
183
- "Sep",
184
- "Oct",
185
- "Nov",
186
- "Dec"
187
- ], r = e.getHours(), a = r % 12 || 12, i = r >= 12 ? "PM" : "AM";
188
- return t.replace("MMM", n[e.getMonth()]).replace("dd", String(e.getDate()).padStart(2, "0")).replace("d", String(e.getDate())).replace("hh", String(a).padStart(2, "0")).replace("h", String(a)).replace("mm", String(e.getMinutes()).padStart(2, "0")).replace("a", i).replace("yyyy", String(e.getFullYear())).replace("yy", String(e.getFullYear()).slice(-2));
189
- }
190
- const G = {
191
- /**
192
- * Get current user ID synchronously
193
- */
194
- getCurrentUserId() {
195
- return I().currentUserId;
196
- },
197
- /**
198
- * Get current timestamp in milliseconds
199
- */
200
- getTimestampInMs() {
201
- return Date.now();
202
- },
203
- /**
204
- * Format date with given format string
205
- * @param options - Format options with timestamp and format string
206
- * @returns Formatted date string
207
- *
208
- * @example
209
- * userSessionInfo.formatDate({ timestampMs: Date.now(), formatString: 'MMM d, h:mm a' })
210
- * // Returns: "Jan 7, 10:30 PM"
211
- */
212
- formatDate(e) {
213
- const { timestampMs: t, formatString: n } = e, r = new Date(t);
214
- return P(r, n);
215
- }
216
- };
217
- export {
218
- N as a,
219
- T as b,
220
- b as c,
221
- A as d,
222
- j as e,
223
- B as f,
224
- L as g,
225
- S as h,
226
- D as i,
227
- G as j,
228
- M as u
229
- };