@aippy/runtime 0.2.5-dev.0 → 0.2.6-dev.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.
@@ -0,0 +1,80 @@
1
+ /**
2
+ * User SDK Bridge
3
+ * Handles communication with:
4
+ * - iOS native layer via window.webkit.aippyListener
5
+ * - Parent window via postMessage (iframe scenario)
6
+ */
7
+ /**
8
+ * User credentials
9
+ */
10
+ export interface IOSUserCredentials {
11
+ uid: string;
12
+ token: string;
13
+ apiBaseUrl?: string;
14
+ }
15
+ /**
16
+ * Check if running inside an iframe
17
+ */
18
+ export declare function isInIframe(): boolean;
19
+ /**
20
+ * Request user credentials from parent window (iframe scenario)
21
+ * Parent window should respond with postMessage containing credentials
22
+ *
23
+ * @param timeoutMs - Timeout in milliseconds (default: 5000)
24
+ * @returns Promise resolving to user credentials
25
+ */
26
+ export declare function requestCredentialsFromParent(timeoutMs?: number): Promise<IOSUserCredentials>;
27
+ /**
28
+ * Try to get credentials from parent localStorage directly
29
+ * This works when same-origin, otherwise falls back to postMessage
30
+ *
31
+ * localStorage format:
32
+ * - token: JWT string
33
+ * - user: JSON string with { uid, nickName, ... }
34
+ */
35
+ export declare function tryGetCredentialsFromParentStorage(): IOSUserCredentials | null;
36
+ /**
37
+ * Request user credentials from iOS native layer
38
+ * Uses aippyRuntime's message passing mechanism
39
+ *
40
+ * @param timeoutMs - Timeout in milliseconds (default: 5000)
41
+ * @returns Promise resolving to user credentials (empty if unavailable)
42
+ */
43
+ export declare function requestCredentialsFromiOS(timeoutMs?: number): Promise<IOSUserCredentials>;
44
+ /**
45
+ * Receive credentials directly from iOS or parent window
46
+ * Called via: window.processUserCredentials(data)
47
+ */
48
+ export declare function processUserCredentials(data: unknown): void;
49
+ /**
50
+ * Initialize bridge and expose to window
51
+ */
52
+ export declare function initUserBridge(): void;
53
+ /**
54
+ * Auto-request credentials on SDK load
55
+ * Tries multiple sources in order:
56
+ * 1. iOS native bridge (if available)
57
+ * 2. Parent localStorage (if in iframe, same-origin)
58
+ * 3. Parent postMessage (if in iframe, cross-origin)
59
+ */
60
+ export declare function autoRequestCredentials(): Promise<void>;
61
+ /**
62
+ * Get auth token asynchronously
63
+ * Waits for credentials if not already available
64
+ * @returns Promise resolving to token string, or "" if unavailable
65
+ */
66
+ export declare function getAuthTokenAsync(): Promise<string>;
67
+ /**
68
+ * Get current user ID asynchronously
69
+ * Waits for credentials if not already available
70
+ * @returns Promise resolving to user ID string, or "" if unavailable
71
+ */
72
+ export declare function getCurrentUserIdAsync(): Promise<string>;
73
+ /**
74
+ * Check if credentials have been received
75
+ */
76
+ export declare function hasCredentials(): boolean;
77
+ /**
78
+ * Get cached credentials (synchronous, may be null if not yet received)
79
+ */
80
+ export declare function getCachedCredentials(): IOSUserCredentials | null;
@@ -0,0 +1,21 @@
1
+ import { UserSdkConfig } from './types';
2
+ /**
3
+ * Initialize user SDK configuration
4
+ */
5
+ export declare function initUserSdk(config: Partial<UserSdkConfig>): void;
6
+ /**
7
+ * Get current user SDK configuration
8
+ */
9
+ export declare function getUserSdkConfig(): UserSdkConfig;
10
+ /**
11
+ * Update auth token
12
+ */
13
+ export declare function setAuthToken(token: string): void;
14
+ /**
15
+ * Update current user ID
16
+ */
17
+ export declare function setCurrentUserId(userId: string | null): void;
18
+ /**
19
+ * Get current auth token synchronously
20
+ */
21
+ export declare function getAuthToken(): string;
@@ -0,0 +1,38 @@
1
+ import { UserProfileQueryResult, UserProfilesQueryResult, GetUserProfileByIdOptions, GetJoinedUserProfilesOptions } from './types';
2
+ import { getAuthToken } from './config';
3
+ /**
4
+ * Hook to get current user ID
5
+ * @returns Current user ID or null
6
+ */
7
+ export declare function useCurrentUserId(): string | null;
8
+ /**
9
+ * Hook to get current auth token
10
+ * @returns Current auth token string
11
+ */
12
+ export declare function useAuthToken(): string;
13
+ /**
14
+ * Hook to get current user's profile
15
+ * @returns Query result with current user profile data
16
+ */
17
+ export declare function useGetCurrentUserProfileQuery(): UserProfileQueryResult;
18
+ /**
19
+ * Hook to get user profile by ID
20
+ * @param options - Options containing user ID to fetch
21
+ * @returns Query result with user profile data
22
+ */
23
+ export declare function useGetUserProfileByIdQuery(options: GetUserProfileByIdOptions): UserProfileQueryResult;
24
+ /**
25
+ * Hook to get all joined user profiles
26
+ * @param options - Options for filtering users
27
+ * @returns Query result with user profiles array
28
+ *
29
+ * Note: This is a simplified implementation that only fetches the current user.
30
+ * In production, this would fetch all users from a users list API.
31
+ */
32
+ export declare function useGetJoinedUserProfilesQuery(options?: GetJoinedUserProfilesOptions): UserProfilesQueryResult;
33
+ /**
34
+ * Synchronous function to get current user ID
35
+ * Use this in non-React contexts
36
+ */
37
+ export declare function getCurrentUserId(): string | null;
38
+ export { getAuthToken };
@@ -0,0 +1,11 @@
1
+ /**
2
+ * User SDK module
3
+ * Provides user profile fetching and session management
4
+ */
5
+ export type { UserProfile, UserProfileQueryResult, UserProfilesQueryResult, GetUserProfileByIdOptions, GetJoinedUserProfilesOptions, FormatDateOptions, UserSessionInfoInterface, UserSdkConfig, } from './types';
6
+ export { initUserSdk, getUserSdkConfig, setAuthToken, setCurrentUserId, } from './config';
7
+ export { useCurrentUserId, useAuthToken, useGetCurrentUserProfileQuery, useGetUserProfileByIdQuery, useGetJoinedUserProfilesQuery, getCurrentUserId, getAuthToken, } from './hooks';
8
+ export { fetchUserProfile, clearProfileCache } from './api';
9
+ export { userSessionInfo } from './userSessionInfo';
10
+ export { isInIframe, requestCredentialsFromiOS, requestCredentialsFromParent, tryGetCredentialsFromParentStorage, initUserBridge, autoRequestCredentials, getAuthTokenAsync, getCurrentUserIdAsync, hasCredentials, getCachedCredentials, } from './bridge';
11
+ export type { IOSUserCredentials } from './bridge';
@@ -0,0 +1,28 @@
1
+ import { f as r, b as a, h as t, l as n, j as u, g as i, k as o, e as d, i as C, c as f, d as l, r as g, s as U, a as h, t as c } from "../bridge-DdAH4txB.js";
2
+ import { e as k, f as y, g as P, a as m, u as A, b as S, d as G, c as T, h as q } from "../userSessionInfo-CBk9ywXi.js";
3
+ export {
4
+ r as autoRequestCredentials,
5
+ k as clearProfileCache,
6
+ y as fetchUserProfile,
7
+ a as getAuthToken,
8
+ t as getAuthTokenAsync,
9
+ n as getCachedCredentials,
10
+ P as getCurrentUserId,
11
+ u as getCurrentUserIdAsync,
12
+ i as getUserSdkConfig,
13
+ o as hasCredentials,
14
+ d as initUserBridge,
15
+ C as initUserSdk,
16
+ f as isInIframe,
17
+ l as requestCredentialsFromParent,
18
+ g as requestCredentialsFromiOS,
19
+ U as setAuthToken,
20
+ h as setCurrentUserId,
21
+ c as tryGetCredentialsFromParentStorage,
22
+ m as useAuthToken,
23
+ A as useCurrentUserId,
24
+ S as useGetCurrentUserProfileQuery,
25
+ G as useGetJoinedUserProfilesQuery,
26
+ T as useGetUserProfileByIdQuery,
27
+ q as userSessionInfo
28
+ };
@@ -0,0 +1,111 @@
1
+ /**
2
+ * User SDK types and interfaces
3
+ */
4
+ /**
5
+ * Global type declarations for User SDK iOS bridge
6
+ * Note: webkit.messageHandlers.aippyListener is already declared in tweaks/types.ts
7
+ */
8
+ declare global {
9
+ interface Window {
10
+ /** User credentials callback from iOS */
11
+ processUserCredentials?: (data: unknown) => void;
12
+ }
13
+ }
14
+ /**
15
+ * User profile data structure
16
+ */
17
+ export interface UserProfile {
18
+ /** User unique identifier */
19
+ id: string;
20
+ /** Display name */
21
+ displayName: string;
22
+ /** Username (for @mentions) */
23
+ username: string;
24
+ /** Avatar URL */
25
+ photoUrl?: string;
26
+ /** Online status */
27
+ online?: boolean;
28
+ /** User bio */
29
+ bio?: string;
30
+ /** Cover image URL */
31
+ coverImageUrl?: string;
32
+ /** Additional metadata */
33
+ [key: string]: unknown;
34
+ }
35
+ /**
36
+ * Query result for user profile
37
+ */
38
+ export interface UserProfileQueryResult {
39
+ /** User profile data */
40
+ data: UserProfile | null;
41
+ /** Loading state */
42
+ isLoading: boolean;
43
+ /** Error state */
44
+ isError: boolean;
45
+ /** Error object */
46
+ error: Error | null;
47
+ /** Refetch function */
48
+ refetch: () => Promise<void>;
49
+ }
50
+ /**
51
+ * Options for getting user profile by ID
52
+ */
53
+ export interface GetUserProfileByIdOptions {
54
+ /** User ID to fetch */
55
+ id: string;
56
+ }
57
+ /**
58
+ * Options for getting joined user profiles
59
+ */
60
+ export interface GetJoinedUserProfilesOptions {
61
+ /** Whether to include current user */
62
+ includeSelf?: boolean;
63
+ }
64
+ /**
65
+ * Query result for multiple user profiles
66
+ */
67
+ export interface UserProfilesQueryResult {
68
+ /** User profiles array */
69
+ data: UserProfile[];
70
+ /** Loading state */
71
+ isLoading: boolean;
72
+ /** Error state */
73
+ isError: boolean;
74
+ /** Error object */
75
+ error: Error | null;
76
+ /** Refetch function */
77
+ refetch: () => Promise<void>;
78
+ }
79
+ /**
80
+ * Date format options
81
+ */
82
+ export interface FormatDateOptions {
83
+ /** Timestamp in milliseconds */
84
+ timestampMs: number;
85
+ /** Format string (e.g., 'MMM d, h:mm a') */
86
+ formatString: string;
87
+ }
88
+ /**
89
+ * User session info interface
90
+ */
91
+ export interface UserSessionInfoInterface {
92
+ /** Get current user ID synchronously */
93
+ getCurrentUserId: () => string | null;
94
+ /** Get current timestamp in milliseconds */
95
+ getTimestampInMs: () => number;
96
+ /** Format date with given format string */
97
+ formatDate: (options: FormatDateOptions) => string;
98
+ }
99
+ /**
100
+ * User SDK configuration
101
+ */
102
+ export interface UserSdkConfig {
103
+ /** API base URL */
104
+ apiBaseUrl: string;
105
+ /** Auth token */
106
+ authToken: string;
107
+ /** Current user ID */
108
+ currentUserId: string | null;
109
+ /** Cache time in milliseconds */
110
+ cacheTime?: number;
111
+ }
@@ -0,0 +1,6 @@
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;
package/dist/user.d.ts ADDED
@@ -0,0 +1,2 @@
1
+ export * from './user/index'
2
+ export {}
@@ -0,0 +1,186 @@
1
+ import { useState as s, useEffect as y, useCallback as h } from "react";
2
+ import { g as m, b as E } from "./bridge-DdAH4txB.js";
3
+ const U = /* @__PURE__ */ new Map();
4
+ async function S(e) {
5
+ const t = m(), n = U.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(), o = M(a, e);
22
+ return U.set(e, { data: o, timestamp: Date.now() }), o;
23
+ } catch (r) {
24
+ return console.error("[UserSDK] Failed to fetch user profile:", r), null;
25
+ }
26
+ }
27
+ function M(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 w(e) {
41
+ e ? U.delete(e) : U.clear();
42
+ }
43
+ function I() {
44
+ const [e, t] = s(
45
+ m().currentUserId
46
+ );
47
+ return y(() => {
48
+ t(m().currentUserId);
49
+ }, []), e;
50
+ }
51
+ function T() {
52
+ const [e, t] = s(E());
53
+ return y(() => {
54
+ t(E());
55
+ }, []), e;
56
+ }
57
+ function b() {
58
+ const e = I(), [t, n] = s(null), [r, a] = s(!0), [o, u] = s(!1), [g, f] = s(null), i = h(async () => {
59
+ if (!e) {
60
+ n(null), a(!1);
61
+ return;
62
+ }
63
+ a(!0), u(!1), f(null);
64
+ try {
65
+ const c = await S(e);
66
+ n(c);
67
+ } catch (c) {
68
+ u(!0), f(c instanceof Error ? c : new Error(String(c)));
69
+ } finally {
70
+ a(!1);
71
+ }
72
+ }, [e]);
73
+ y(() => {
74
+ i();
75
+ }, [i]);
76
+ const l = h(async () => {
77
+ e && w(e), await i();
78
+ }, [e, i]);
79
+ return { data: t, isLoading: r, isError: o, error: g, refetch: l };
80
+ }
81
+ function A(e) {
82
+ const { id: t } = e, [n, r] = s(null), [a, o] = s(!0), [u, g] = s(!1), [f, i] = s(null), l = h(async () => {
83
+ if (!t) {
84
+ r(null), o(!1);
85
+ return;
86
+ }
87
+ o(!0), g(!1), i(null);
88
+ try {
89
+ const p = await S(t);
90
+ r(p);
91
+ } catch (p) {
92
+ g(!0), i(p instanceof Error ? p : new Error(String(p)));
93
+ } finally {
94
+ o(!1);
95
+ }
96
+ }, [t]);
97
+ y(() => {
98
+ l();
99
+ }, [l]);
100
+ const c = h(async () => {
101
+ w(t), await l();
102
+ }, [t, l]);
103
+ return { data: n, isLoading: a, isError: u, error: f, refetch: c };
104
+ }
105
+ function L(e = {}) {
106
+ const { includeSelf: t = !0 } = e, n = I(), [r, a] = s([]), [o, u] = s(!0), [g, f] = s(!1), [i, l] = s(null), c = h(async () => {
107
+ u(!0), f(!1), l(null);
108
+ try {
109
+ const d = [];
110
+ if (t && n) {
111
+ const D = await S(n);
112
+ D && d.push(D);
113
+ }
114
+ a(d);
115
+ } catch (d) {
116
+ f(!0), l(d instanceof Error ? d : new Error(String(d)));
117
+ } finally {
118
+ u(!1);
119
+ }
120
+ }, [t, n]);
121
+ y(() => {
122
+ c();
123
+ }, [c]);
124
+ const p = h(async () => {
125
+ w(), await c();
126
+ }, [c]);
127
+ return { data: r, isLoading: o, isError: g, error: i, refetch: p };
128
+ }
129
+ function v() {
130
+ return m().currentUserId;
131
+ }
132
+ function k(e, t) {
133
+ const n = [
134
+ "Jan",
135
+ "Feb",
136
+ "Mar",
137
+ "Apr",
138
+ "May",
139
+ "Jun",
140
+ "Jul",
141
+ "Aug",
142
+ "Sep",
143
+ "Oct",
144
+ "Nov",
145
+ "Dec"
146
+ ], r = e.getHours(), a = r % 12 || 12, o = r >= 12 ? "PM" : "AM";
147
+ 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", o).replace("yyyy", String(e.getFullYear())).replace("yy", String(e.getFullYear()).slice(-2));
148
+ }
149
+ const F = {
150
+ /**
151
+ * Get current user ID synchronously
152
+ */
153
+ getCurrentUserId() {
154
+ return m().currentUserId;
155
+ },
156
+ /**
157
+ * Get current timestamp in milliseconds
158
+ */
159
+ getTimestampInMs() {
160
+ return Date.now();
161
+ },
162
+ /**
163
+ * Format date with given format string
164
+ * @param options - Format options with timestamp and format string
165
+ * @returns Formatted date string
166
+ *
167
+ * @example
168
+ * userSessionInfo.formatDate({ timestampMs: Date.now(), formatString: 'MMM d, h:mm a' })
169
+ * // Returns: "Jan 7, 10:30 PM"
170
+ */
171
+ formatDate(e) {
172
+ const { timestampMs: t, formatString: n } = e, r = new Date(t);
173
+ return k(r, n);
174
+ }
175
+ };
176
+ export {
177
+ T as a,
178
+ b,
179
+ A as c,
180
+ L as d,
181
+ w as e,
182
+ S as f,
183
+ v as g,
184
+ F as h,
185
+ I as u
186
+ };
package/package.json CHANGED
@@ -1,40 +1,48 @@
1
1
  {
2
2
  "name": "@aippy/runtime",
3
- "version": "0.2.5-dev.0",
3
+ "version": "0.2.6-dev.0",
4
4
  "description": "Aippy Runtime SDK - Runtime SDK for Aippy projects",
5
5
  "private": false,
6
6
  "type": "module",
7
- "main": "./dist/index.js",
8
- "module": "./dist/index.js",
7
+ "main": "./dist/index/index.js",
8
+ "module": "./dist/index/index.js",
9
9
  "types": "./dist/index.d.ts",
10
10
  "exports": {
11
11
  ".": {
12
- "import": "./dist/index.js",
12
+ "import": "./dist/index/index.js",
13
13
  "types": "./dist/index.d.ts"
14
14
  },
15
15
  "./core": {
16
16
  "import": "./dist/core/index.js",
17
- "types": "./dist/core/index.d.ts"
17
+ "types": "./dist/core.d.ts"
18
18
  },
19
19
  "./device": {
20
20
  "import": "./dist/device/index.js",
21
- "types": "./dist/device/index.d.ts"
21
+ "types": "./dist/device.d.ts"
22
22
  },
23
23
  "./utils": {
24
24
  "import": "./dist/utils/index.js",
25
- "types": "./dist/utils/index.d.ts"
25
+ "types": "./dist/utils.d.ts"
26
26
  },
27
27
  "./tweaks": {
28
28
  "import": "./dist/tweaks/index.js",
29
- "types": "./dist/tweaks/index.d.ts"
29
+ "types": "./dist/tweaks.d.ts"
30
30
  },
31
31
  "./audio": {
32
32
  "import": "./dist/audio/index.js",
33
- "types": "./dist/audio/index.d.ts"
33
+ "types": "./dist/audio.d.ts"
34
34
  },
35
35
  "./leaderboard": {
36
36
  "import": "./dist/leaderboard/index.js",
37
- "types": "./dist/leaderboard/index.d.ts"
37
+ "types": "./dist/leaderboard.d.ts"
38
+ },
39
+ "./ai": {
40
+ "import": "./dist/ai/index.js",
41
+ "types": "./dist/ai.d.ts"
42
+ },
43
+ "./user": {
44
+ "import": "./dist/user/index.js",
45
+ "types": "./dist/user/index.d.ts"
38
46
  }
39
47
  },
40
48
  "files": [
@@ -70,9 +78,12 @@
70
78
  "react": "^19.1.1",
71
79
  "typescript": "^5.9.2",
72
80
  "vite": "^7.1.7",
73
- "vite-plugin-dts": "^4.5.4"
81
+ "vite-plugin-dts": "^4.5.4",
82
+ "zod": "^3.25.76"
74
83
  },
75
84
  "dependencies": {
85
+ "@ai-sdk/openai-compatible": "^2.0.4",
86
+ "ai": "^6.0.14",
76
87
  "ua-parser-js": "^2.0.5"
77
88
  },
78
89
  "peerDependencies": {
@@ -104,6 +115,8 @@
104
115
  "security:check": "pnpm audit && pnpm outdated",
105
116
  "prerelease": "pnpm run audit && pnpm run type-check && pnpm run lint && pnpm run build",
106
117
  "release:dry": "npm publish --dry-run",
107
- "test:build": "node -e \"require('./dist/index.js')\""
118
+ "test": "pnpm run build && node --test tests/*.test.mjs",
119
+ "test:file": "pnpm run build && node --test",
120
+ "test:build": "node -c dist/index.js"
108
121
  }
109
122
  }