@base44-preview/sdk 0.8.48-pr.282.7c5a459 → 0.8.48-pr.282.86ef42a

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/dist/client.js CHANGED
@@ -5,7 +5,7 @@ import { createAuthModule } from "./modules/auth.js";
5
5
  import { createSsoModule } from "./modules/sso.js";
6
6
  import { createConnectorsModule, createUserConnectorsModule, } from "./modules/connectors.js";
7
7
  import { getAccessToken } from "./utils/auth-utils.js";
8
- import { exchangeEmbedToken, frameSession, takeEmbedTokenFromUrl, } from "./utils/embed-session.js";
8
+ import { exchangeEmbedToken, takeEmbedTokenFromUrl, } from "./utils/embed-session.js";
9
9
  import { createFetchWithAuth } from "./utils/fetch-with-auth.js";
10
10
  import { createFunctionsModule } from "./modules/functions.js";
11
11
  import { createAgentsModule } from "./modules/agents.js";
@@ -59,7 +59,32 @@ export function createClient(config) {
59
59
  // Normalize appBaseUrl to always be a string (empty if not provided or invalid)
60
60
  const normalizedAppBaseUrl = typeof appBaseUrl === "string" ? appBaseUrl : "";
61
61
  const embedOtt = takeEmbedTokenFromUrl();
62
- // In a frame the caller's token is the OTT itself, which is not a session.
62
+ // Asked again on every use, so a session that arrives later — from the
63
+ // exchange, or from a login — reaches the socket and every other module.
64
+ // A declaration, not a const, so this block can stay above the auth module.
65
+ function getToken() {
66
+ var _a;
67
+ return (_a = userAuthModule.getToken()) !== null && _a !== void 0 ? _a : (embedOtt ? null : getAccessToken());
68
+ }
69
+ const socketConfig = {
70
+ serverUrl,
71
+ mountPath: "/ws-user-apps/socket.io/",
72
+ transports: ["websocket"],
73
+ appId,
74
+ getToken,
75
+ };
76
+ let socket = null;
77
+ const getSocket = () => {
78
+ if (!socket) {
79
+ socket = RoomsSocket({
80
+ config: socketConfig,
81
+ });
82
+ }
83
+ return socket;
84
+ };
85
+ // Apps read getAccessToken() as they load and pass the result in as `token`,
86
+ // so on an embedded load this is the OTT: proof of an identity, not one to
87
+ // send as a bearer. The exchange below is what turns it into a session.
63
88
  const token = embedOtt ? undefined : config.token;
64
89
  const headers = {
65
90
  ...optionalHeaders,
@@ -109,9 +134,6 @@ export function createClient(config) {
109
134
  baseURL: `${serverUrl}/api`,
110
135
  headers,
111
136
  });
112
- // Declared before the auth module so `onSessionChange` can reach it; the
113
- // socket itself is still only built on first use, below.
114
- let socket = null;
115
137
  const userAuthModule = createAuthModule(axiosClient, functionsAxiosClient, appId, {
116
138
  appBaseUrl: normalizedAppBaseUrl,
117
139
  serverUrl,
@@ -134,27 +156,9 @@ export function createClient(config) {
134
156
  userAuthModule.setToken(accessToken);
135
157
  }
136
158
  }
137
- // Live token for every module; outside a frame it still falls back to storage.
138
- const getToken = () => { var _a; return (_a = userAuthModule.getToken()) !== null && _a !== void 0 ? _a : (embedOtt ? null : getAccessToken()); };
139
- const socketConfig = {
140
- serverUrl,
141
- mountPath: "/ws-user-apps/socket.io/",
142
- transports: ["websocket"],
143
- appId,
144
- getToken,
145
- };
146
- const getSocket = () => {
147
- if (!socket) {
148
- socket = RoomsSocket({ config: socketConfig });
149
- }
150
- return socket;
151
- };
152
- // The exchange belongs to the frame, not to one client: the token is off the
153
- // URL after the first `createClient`, so a second one in the same document
154
- // joins the session already being negotiated rather than going anonymous.
155
159
  const session = embedOtt
156
- ? frameSession(appId, () => exchangeEmbedToken({ serverUrl, appId, ott: embedOtt }))
157
- : frameSession(appId);
160
+ ? exchangeEmbedToken({ serverUrl, appId, ott: embedOtt })
161
+ : null;
158
162
  // Settles once the exchanged session is applied (memory only); at once
159
163
  // otherwise. Never rejects: every request waits on it, so a failure here
160
164
  // must not turn into a rejection on each of them.
@@ -11,8 +11,9 @@ interface ActorsConfig {
11
11
  appId: string;
12
12
  /** Current user access token, if authenticated. Rides the WS query on the
13
13
  * proxy-fallback path so the platform proxy can authenticate the connection;
14
- * anonymous connects omit it. */
15
- getAuthToken(): string | null | undefined | Promise<string | null | undefined>;
14
+ * anonymous connects omit it. Awaited per dial, so a session still being
15
+ * exchanged is in hand before the URL is built. */
16
+ getAuthToken(): Promise<string | null | undefined>;
16
17
  /** Same semantics as function calls: editors with a non-prod version get the
17
18
  * draft actor script; everyone else gets the published one. */
18
19
  functionsVersion?: string;
@@ -161,7 +161,7 @@ export interface AgentsModuleConfig {
161
161
  /** Server URL */
162
162
  serverUrl?: string;
163
163
  /** Returns the current authentication token, if any */
164
- getToken: () => string | null | undefined;
164
+ getToken: () => string | null;
165
165
  }
166
166
  /**
167
167
  * Agents module for managing AI agent conversations.
@@ -360,7 +360,9 @@ export interface AgentsModule {
360
360
  * Gets WhatsApp connection URL for an agent.
361
361
  *
362
362
  * Generates a URL that users can use to connect with the agent through WhatsApp.
363
- * The URL includes authentication if a token is available.
363
+ * The URL includes authentication if a token is available. In an app a
364
+ * platform has embedded, that is only once the session has been exchanged —
365
+ * await a call such as `base44.auth.me()` before building the URL.
364
366
  *
365
367
  * @param agentName - The name of the agent.
366
368
  * @returns WhatsApp connection URL.
@@ -378,7 +380,9 @@ export interface AgentsModule {
378
380
  * Gets Telegram connection URL for an agent.
379
381
  *
380
382
  * Generates a URL that users can use to connect with the agent through Telegram.
381
- * The URL includes authentication if a token is available. When the user opens
383
+ * The URL includes authentication if a token is available. In an app a
384
+ * platform has embedded, that is only once the session has been exchanged —
385
+ * await a call such as `base44.auth.me()` before building the URL. When the user opens
382
386
  * this URL, they are redirected to the agent's Telegram bot with an activation
383
387
  * code that securely links their account.
384
388
  *
@@ -9,7 +9,9 @@
9
9
  *
10
10
  * Taking the token off the URL is unconditional — a one-time token must not be
11
11
  * left in the address bar, in history, or in a shared link — but it is only
12
- * reported back inside a frame, the only place one is redeemed.
12
+ * reported back inside a frame, the only place one is redeemed. It is gone once
13
+ * the first client has taken it, so the session belongs to that client — an app
14
+ * creates one.
13
15
  *
14
16
  * The exchange endpoint is rate limited per app, and its limiter refuses before
15
17
  * the one-time token is redeemed — so a 429 leaves the token still valid and is
@@ -19,8 +21,6 @@
19
21
  */
20
22
  export declare const EMBED_TOKEN_PARAM = "ott";
21
23
  /** @internal */
22
- export declare function frameSession(appId: string, start?: () => Promise<string | null>): Promise<string | null> | null;
23
- /** @internal */
24
24
  export declare function takeEmbedTokenFromUrl(): string | null;
25
25
  /** @internal */
26
26
  export declare function isFramed(): boolean;
@@ -9,7 +9,9 @@
9
9
  *
10
10
  * Taking the token off the URL is unconditional — a one-time token must not be
11
11
  * left in the address bar, in history, or in a shared link — but it is only
12
- * reported back inside a frame, the only place one is redeemed.
12
+ * reported back inside a frame, the only place one is redeemed. It is gone once
13
+ * the first client has taken it, so the session belongs to that client — an app
14
+ * creates one.
13
15
  *
14
16
  * The exchange endpoint is rate limited per app, and its limiter refuses before
15
17
  * the one-time token is redeemed — so a 429 leaves the token still valid and is
@@ -24,20 +26,6 @@ const RATE_LIMITED = 429;
24
26
  const RATE_LIMIT_RETRY_MS = 1000;
25
27
  const EXCHANGE_TIMEOUT_MS = 15000;
26
28
  const SESSION_ENDED_ELEMENT_ID = "base44-embed-session-ended";
27
- const FRAME_SESSIONS_KEY = "__base44EmbedSessions";
28
- /** @internal */
29
- export function frameSession(appId, start) {
30
- var _a, _b;
31
- var _c;
32
- if (typeof window === "undefined") {
33
- return start ? start() : null;
34
- }
35
- const sessions = ((_a = (_c = window)[FRAME_SESSIONS_KEY]) !== null && _a !== void 0 ? _a : (_c[FRAME_SESSIONS_KEY] = {}));
36
- if (!sessions[appId] && start) {
37
- sessions[appId] = start();
38
- }
39
- return (_b = sessions[appId]) !== null && _b !== void 0 ? _b : null;
40
- }
41
29
  /** @internal */
42
30
  export function takeEmbedTokenFromUrl() {
43
31
  if (typeof window === "undefined" || !window.location) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.8.48-pr.282.7c5a459",
3
+ "version": "0.8.48-pr.282.86ef42a",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",