@base44-preview/sdk 0.8.40-pr.212.527cb51 → 0.8.40-pr.212.55a476c
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/actor.d.ts +7 -0
- package/dist/actor.js +8 -0
- package/dist/client.js +15 -32
- package/dist/client.types.d.ts +1 -25
- package/dist/modules/actors.d.ts +16 -7
- package/dist/modules/actors.js +44 -17
- package/dist/modules/actors.types.d.ts +1 -1
- package/dist/modules/ai-gateway.types.d.ts +70 -19
- package/dist/modules/sso.types.d.ts +28 -22
- package/package.json +1 -1
package/dist/actor.d.ts
CHANGED
|
@@ -8,6 +8,7 @@
|
|
|
8
8
|
* At deploy time the bundler replaces this import with the compiled
|
|
9
9
|
* Cloudflare Durable Object implementation — this file provides types only.
|
|
10
10
|
*/
|
|
11
|
+
import type { Base44Client } from "./client";
|
|
11
12
|
/**
|
|
12
13
|
* A single client connection. `Send` is the message type this connection accepts
|
|
13
14
|
* via {@link send} — the actor's *outgoing* (server→client) messages.
|
|
@@ -74,4 +75,10 @@ export declare abstract class Actor<Incoming = unknown, Outgoing = unknown> {
|
|
|
74
75
|
protected stopLoop(): Promise<void>;
|
|
75
76
|
protected get instanceId(): string;
|
|
76
77
|
protected get storage(): Storage;
|
|
78
|
+
/**
|
|
79
|
+
* Anonymous Base44 client scoped to this actor instance — no user or service
|
|
80
|
+
* auth, so entity access is RLS-gated (same as a logged-out visitor).
|
|
81
|
+
* Example: `const rows = await this.client.entities.Score.list();`
|
|
82
|
+
*/
|
|
83
|
+
protected get client(): Base44Client;
|
|
77
84
|
}
|
package/dist/actor.js
CHANGED
|
@@ -60,4 +60,12 @@ export class Actor {
|
|
|
60
60
|
get storage() {
|
|
61
61
|
throw new Error("Actor.storage is only available inside a deployed actor");
|
|
62
62
|
}
|
|
63
|
+
/**
|
|
64
|
+
* Anonymous Base44 client scoped to this actor instance — no user or service
|
|
65
|
+
* auth, so entity access is RLS-gated (same as a logged-out visitor).
|
|
66
|
+
* Example: `const rows = await this.client.entities.Score.list();`
|
|
67
|
+
*/
|
|
68
|
+
get client() {
|
|
69
|
+
throw new Error("Actor.client is only available inside a deployed actor");
|
|
70
|
+
}
|
|
63
71
|
}
|
package/dist/client.js
CHANGED
|
@@ -12,7 +12,7 @@ import { createAppLogsModule } from "./modules/app-logs.js";
|
|
|
12
12
|
import { createUsersModule } from "./modules/users.js";
|
|
13
13
|
import { RoomsSocket } from "./utils/socket-utils.js";
|
|
14
14
|
import { createAnalyticsModule } from "./modules/analytics.js";
|
|
15
|
-
import { createActorsModule } from "./modules/actors.js";
|
|
15
|
+
import { createActorsModule, resolveActorsHost } from "./modules/actors.js";
|
|
16
16
|
/**
|
|
17
17
|
* Creates a Base44 client.
|
|
18
18
|
*
|
|
@@ -51,28 +51,10 @@ import { createActorsModule } from "./modules/actors.js";
|
|
|
51
51
|
* ```
|
|
52
52
|
*/
|
|
53
53
|
export function createClient(config) {
|
|
54
|
-
var _a, _b;
|
|
55
|
-
const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, appBaseUrl, options, functionsVersion, headers: optionalHeaders,
|
|
54
|
+
var _a, _b, _c;
|
|
55
|
+
const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, appBaseUrl, options, functionsVersion, headers: optionalHeaders, } = config;
|
|
56
56
|
// Normalize appBaseUrl to always be a string (empty if not provided or invalid)
|
|
57
57
|
const normalizedAppBaseUrl = typeof appBaseUrl === "string" ? appBaseUrl : "";
|
|
58
|
-
// Derive the dispatcher WebSocket URL if not explicitly provided. Default to
|
|
59
|
-
// the app's OWN origin (the app URL proxies /parties to the backend) so the
|
|
60
|
-
// socket is same-origin with the running app, not the API host: prefer an
|
|
61
|
-
// explicit appBaseUrl, then the browser origin, then fall back to serverUrl
|
|
62
|
-
// (Node/SSR, where there's no window). Convert https:// → wss:// (http → ws)
|
|
63
|
-
// and strip the trailing slash.
|
|
64
|
-
const resolvedDispatcherWsUrl = (() => {
|
|
65
|
-
var _a, _b;
|
|
66
|
-
if (dispatcherWsUrl)
|
|
67
|
-
return dispatcherWsUrl.replace(/\/$/, "");
|
|
68
|
-
const appOrigin = normalizedAppBaseUrl ||
|
|
69
|
-
// React Native has a bare `window` with no `location`, so guard both.
|
|
70
|
-
(typeof window !== "undefined" ? (_b = (_a = window.location) === null || _a === void 0 ? void 0 : _a.origin) !== null && _b !== void 0 ? _b : "" : "");
|
|
71
|
-
return (appOrigin || serverUrl)
|
|
72
|
-
.replace(/\/$/, "")
|
|
73
|
-
.replace(/^https:\/\//, "wss://")
|
|
74
|
-
.replace(/^http:\/\//, "ws://");
|
|
75
|
-
})();
|
|
76
58
|
const socketConfig = {
|
|
77
59
|
serverUrl,
|
|
78
60
|
mountPath: "/ws-user-apps/socket.io/",
|
|
@@ -142,6 +124,14 @@ export function createClient(config) {
|
|
|
142
124
|
userAuthModule.setToken(accessToken);
|
|
143
125
|
}
|
|
144
126
|
}
|
|
127
|
+
const actorsModule = createActorsModule({
|
|
128
|
+
appId,
|
|
129
|
+
// serverUrl is often relative/empty (same-origin app); PartySocket needs an
|
|
130
|
+
// absolute host, so fall back to the page origin.
|
|
131
|
+
host: resolveActorsHost(serverUrl, typeof window !== "undefined" ? (_a = window.location) === null || _a === void 0 ? void 0 : _a.origin : undefined),
|
|
132
|
+
functionsVersion,
|
|
133
|
+
getAuthToken: () => token || getAccessToken(),
|
|
134
|
+
});
|
|
145
135
|
const userModules = {
|
|
146
136
|
entities: createEntitiesModule({
|
|
147
137
|
axios: axiosClient,
|
|
@@ -161,7 +151,7 @@ export function createClient(config) {
|
|
|
161
151
|
}
|
|
162
152
|
return headers;
|
|
163
153
|
},
|
|
164
|
-
baseURL: (
|
|
154
|
+
baseURL: (_b = functionsAxiosClient.defaults) === null || _b === void 0 ? void 0 : _b.baseURL,
|
|
165
155
|
}),
|
|
166
156
|
agents: createAgentsModule({
|
|
167
157
|
axios: axiosClient,
|
|
@@ -179,17 +169,10 @@ export function createClient(config) {
|
|
|
179
169
|
appId,
|
|
180
170
|
userAuthModule,
|
|
181
171
|
}),
|
|
182
|
-
actors:
|
|
183
|
-
appId,
|
|
184
|
-
dispatcherWsUrl: resolvedDispatcherWsUrl,
|
|
185
|
-
webSocketImpl,
|
|
186
|
-
functionsVersion,
|
|
187
|
-
// Same credential as function calls; the platform proxy authenticates the
|
|
188
|
-
// WS connection with it (anonymous when absent) — no pre-connect token mint.
|
|
189
|
-
getAuthToken: () => token || getAccessToken(),
|
|
190
|
-
}),
|
|
172
|
+
actors: actorsModule.module,
|
|
191
173
|
cleanup: () => {
|
|
192
174
|
userModules.analytics.cleanup();
|
|
175
|
+
actorsModule.closeAll();
|
|
193
176
|
if (socket) {
|
|
194
177
|
socket.disconnect();
|
|
195
178
|
}
|
|
@@ -213,7 +196,7 @@ export function createClient(config) {
|
|
|
213
196
|
}
|
|
214
197
|
return headers;
|
|
215
198
|
},
|
|
216
|
-
baseURL: (
|
|
199
|
+
baseURL: (_c = serviceRoleFunctionsAxiosClient.defaults) === null || _c === void 0 ? void 0 : _c.baseURL,
|
|
217
200
|
}),
|
|
218
201
|
agents: createAgentsModule({
|
|
219
202
|
axios: serviceRoleAxiosClient,
|
package/dist/client.types.d.ts
CHANGED
|
@@ -74,28 +74,6 @@ export interface CreateClientConfig {
|
|
|
74
74
|
* Additional client options.
|
|
75
75
|
*/
|
|
76
76
|
options?: CreateClientOptions;
|
|
77
|
-
/**
|
|
78
|
-
* Base WebSocket URL for the Cloudflare Durable Object dispatcher.
|
|
79
|
-
*
|
|
80
|
-
* Defaults to the app's own origin (`appBaseUrl`, else the browser's
|
|
81
|
-
* `window.location.origin`, else `serverUrl`) with `https://` replaced by
|
|
82
|
-
* `wss://` (or `http://` by `ws://`) — so the realtime socket is same-origin
|
|
83
|
-
* with the running app, which proxies `/parties` to the backend.
|
|
84
|
-
* Override when the dispatcher lives at a different host than the app.
|
|
85
|
-
*/
|
|
86
|
-
dispatcherWsUrl?: string;
|
|
87
|
-
/**
|
|
88
|
-
* WebSocket implementation for realtime subscriptions in environments
|
|
89
|
-
* without a global `WebSocket` (Node.js < 22). Browsers and Node ≥ 22
|
|
90
|
-
* don't need this.
|
|
91
|
-
*
|
|
92
|
-
* @example
|
|
93
|
-
* ```typescript
|
|
94
|
-
* import WS from "ws";
|
|
95
|
-
* const base44 = createClient({ appId, webSocketImpl: WS });
|
|
96
|
-
* ```
|
|
97
|
-
*/
|
|
98
|
-
webSocketImpl?: unknown;
|
|
99
77
|
}
|
|
100
78
|
/**
|
|
101
79
|
* The Base44 client instance.
|
|
@@ -164,9 +142,7 @@ export interface Base44Client {
|
|
|
164
142
|
functions: FunctionsModule;
|
|
165
143
|
/** {@link IntegrationsModule | Integrations module} with elevated permissions. */
|
|
166
144
|
integrations: IntegrationsModule;
|
|
167
|
-
/** {@link SsoModule | SSO module} for generating SSO tokens.
|
|
168
|
-
* @internal
|
|
169
|
-
*/
|
|
145
|
+
/** {@link SsoModule | SSO module} for generating SSO tokens. */
|
|
170
146
|
sso: SsoModule;
|
|
171
147
|
/** Cleanup function to disconnect WebSocket connections. */
|
|
172
148
|
cleanup: () => void;
|
package/dist/modules/actors.d.ts
CHANGED
|
@@ -1,16 +1,25 @@
|
|
|
1
1
|
import type { ActorRoom } from "./actors.types.js";
|
|
2
2
|
interface ActorsConfig {
|
|
3
3
|
appId: string;
|
|
4
|
-
/** Current user access token, if authenticated. Rides the WS query
|
|
5
|
-
*
|
|
6
|
-
* connection like a backend-function call; anonymous connects omit it. */
|
|
4
|
+
/** Current user access token, if authenticated. Rides the WS query so the
|
|
5
|
+
* platform proxy can authenticate the connection; anonymous connects omit it. */
|
|
7
6
|
getAuthToken(): string | null | undefined;
|
|
8
7
|
/** Same semantics as function calls: editors with a non-prod version get the
|
|
9
8
|
* draft actor script; everyone else gets the published one. */
|
|
10
9
|
functionsVersion?: string;
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
10
|
+
/** Absolute host PartySocket dials (it strips the scheme and connects wss, ws
|
|
11
|
+
* for localhost). Resolved by {@link resolveActorsHost}. */
|
|
12
|
+
host: string;
|
|
14
13
|
}
|
|
15
|
-
|
|
14
|
+
/**
|
|
15
|
+
* Absolute host for the actor WebSocket. PartySocket needs an absolute host and
|
|
16
|
+
* can't resolve a relative/empty `serverUrl` (same-origin apps use a relative
|
|
17
|
+
* `/api`, so `serverUrl` is often `""`), so fall back to the page origin.
|
|
18
|
+
* PartySocket handles the scheme (https→wss, ws for localhost).
|
|
19
|
+
*/
|
|
20
|
+
export declare function resolveActorsHost(serverUrl: string, browserOrigin?: string): string;
|
|
21
|
+
export declare function createActorsModule(config: ActorsConfig): {
|
|
22
|
+
module: Record<string, (instanceId: string) => ActorRoom>;
|
|
23
|
+
closeAll: () => void;
|
|
24
|
+
};
|
|
16
25
|
export {};
|
package/dist/modules/actors.js
CHANGED
|
@@ -1,15 +1,14 @@
|
|
|
1
1
|
import PartySocket from "partysocket";
|
|
2
|
-
// Heartbeat / half-open detection
|
|
3
|
-
//
|
|
4
|
-
// behind proxies/LBs) hangs until the OS idle timeout (~60s). Ping periodically
|
|
5
|
-
// and force a reconnect if nothing comes back within DEAD_MS.
|
|
2
|
+
// Heartbeat / half-open detection: PartySocket only reconnects on a close/error
|
|
3
|
+
// event, so ping periodically and force a reconnect if nothing returns in DEAD_MS.
|
|
6
4
|
const PING_MS = 1000;
|
|
7
5
|
const DEAD_MS = 3000;
|
|
8
6
|
class Room {
|
|
9
|
-
constructor(actorName, instanceId, config) {
|
|
7
|
+
constructor(actorName, instanceId, config, onClose) {
|
|
10
8
|
this.actorName = actorName;
|
|
11
9
|
this.instanceId = instanceId;
|
|
12
10
|
this.config = config;
|
|
11
|
+
this.onClose = onClose;
|
|
13
12
|
this.ws = null;
|
|
14
13
|
this.listeners = new Set();
|
|
15
14
|
this.heartbeat = null;
|
|
@@ -29,14 +28,11 @@ class Room {
|
|
|
29
28
|
const connId = (_a = options === null || options === void 0 ? void 0 : options.id) !== null && _a !== void 0 ? _a : crypto.randomUUID();
|
|
30
29
|
this.connId = connId;
|
|
31
30
|
const ws = new PartySocket({
|
|
32
|
-
host: this.config.
|
|
31
|
+
host: this.config.host,
|
|
33
32
|
party: this.actorName,
|
|
34
33
|
room: this.instanceId,
|
|
35
34
|
id: connId,
|
|
36
|
-
|
|
37
|
-
// Re-read on every (re)connect so a login/logout between reconnects is
|
|
38
|
-
// picked up. The platform proxy authenticates the connection itself —
|
|
39
|
-
// no pre-connect token mint.
|
|
35
|
+
// Re-read on every (re)connect so a login/logout is picked up.
|
|
40
36
|
query: () => {
|
|
41
37
|
const token = this.config.getAuthToken();
|
|
42
38
|
return {
|
|
@@ -58,11 +54,11 @@ class Room {
|
|
|
58
54
|
data = JSON.parse(ev.data);
|
|
59
55
|
}
|
|
60
56
|
catch (_a) {
|
|
61
|
-
return;
|
|
57
|
+
return;
|
|
62
58
|
}
|
|
63
59
|
const msgType = data && typeof data === "object" ? data.type : undefined;
|
|
64
60
|
if (msgType === "__pong")
|
|
65
|
-
return;
|
|
61
|
+
return;
|
|
66
62
|
for (const listener of this.listeners)
|
|
67
63
|
listener(data);
|
|
68
64
|
});
|
|
@@ -73,10 +69,12 @@ class Room {
|
|
|
73
69
|
return;
|
|
74
70
|
}
|
|
75
71
|
try {
|
|
72
|
+
// The deployed shim echoes __ping → __pong (base44-userapp-bundler
|
|
73
|
+
// shim/actor.ts); without that, an idle room reconnects every DEAD_MS.
|
|
76
74
|
ws.send(JSON.stringify({ type: "__ping" }));
|
|
77
75
|
}
|
|
78
76
|
catch (_a) {
|
|
79
|
-
//
|
|
77
|
+
// not open; the watchdog above will reconnect
|
|
80
78
|
}
|
|
81
79
|
}, PING_MS);
|
|
82
80
|
return this;
|
|
@@ -97,7 +95,7 @@ class Room {
|
|
|
97
95
|
this.ws.send(JSON.stringify(data));
|
|
98
96
|
}
|
|
99
97
|
close() {
|
|
100
|
-
var _a;
|
|
98
|
+
var _a, _b;
|
|
101
99
|
if (this.heartbeat) {
|
|
102
100
|
clearInterval(this.heartbeat);
|
|
103
101
|
this.heartbeat = null;
|
|
@@ -105,12 +103,41 @@ class Room {
|
|
|
105
103
|
this.listeners.clear();
|
|
106
104
|
(_a = this.ws) === null || _a === void 0 ? void 0 : _a.close();
|
|
107
105
|
this.ws = null;
|
|
106
|
+
this.connId = null;
|
|
107
|
+
(_b = this.onClose) === null || _b === void 0 ? void 0 : _b.call(this);
|
|
108
108
|
}
|
|
109
109
|
}
|
|
110
|
+
/**
|
|
111
|
+
* Absolute host for the actor WebSocket. PartySocket needs an absolute host and
|
|
112
|
+
* can't resolve a relative/empty `serverUrl` (same-origin apps use a relative
|
|
113
|
+
* `/api`, so `serverUrl` is often `""`), so fall back to the page origin.
|
|
114
|
+
* PartySocket handles the scheme (https→wss, ws for localhost).
|
|
115
|
+
*/
|
|
116
|
+
export function resolveActorsHost(serverUrl, browserOrigin) {
|
|
117
|
+
return serverUrl && !serverUrl.startsWith("/") ? serverUrl : browserOrigin !== null && browserOrigin !== void 0 ? browserOrigin : serverUrl;
|
|
118
|
+
}
|
|
110
119
|
export function createActorsModule(config) {
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
120
|
+
// Live rooms this client opened, so client.cleanup() can reclaim any the app
|
|
121
|
+
// forgot to close() (each room removes itself here on close).
|
|
122
|
+
const rooms = new Set();
|
|
123
|
+
const module = new Proxy({}, {
|
|
124
|
+
get(_, key) {
|
|
125
|
+
// Symbols and `then` resolve to undefined (so the module isn't mistaken
|
|
126
|
+
// for a thenable when awaited); any string key is an actor name.
|
|
127
|
+
if (typeof key !== "string" || key === "then")
|
|
128
|
+
return undefined;
|
|
129
|
+
return (instanceId) => {
|
|
130
|
+
const room = new Room(key, instanceId, config, () => rooms.delete(room));
|
|
131
|
+
rooms.add(room);
|
|
132
|
+
return room;
|
|
133
|
+
};
|
|
114
134
|
},
|
|
115
135
|
});
|
|
136
|
+
return {
|
|
137
|
+
module,
|
|
138
|
+
closeAll: () => {
|
|
139
|
+
for (const room of [...rooms])
|
|
140
|
+
room.close();
|
|
141
|
+
},
|
|
142
|
+
};
|
|
116
143
|
}
|
|
@@ -58,7 +58,7 @@ export interface ActorRoom<N extends string = string> {
|
|
|
58
58
|
connect(options?: ActorConnectOptions): this;
|
|
59
59
|
/** Register a message listener. Multiple are allowed; returns a per-listener unsubscribe. */
|
|
60
60
|
subscribe(callback: (data: ToClientFor<N>) => void): ActorSubscription;
|
|
61
|
-
/** Send a message. Throws before {@link connect}; buffered by the socket
|
|
61
|
+
/** Send a message. Throws before {@link connect}; buffered by the socket once connecting. */
|
|
62
62
|
send(data: ToServerFor<N>): void;
|
|
63
63
|
/** Tear down the socket, heartbeat, and all listeners. */
|
|
64
64
|
close(): void;
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
/**
|
|
2
|
-
*
|
|
3
|
-
*
|
|
4
|
-
* Contains the base URL and bearer token to use with any OpenAI-compatible
|
|
5
|
-
* client pointed at the Base44 AI Gateway.
|
|
2
|
+
* Connection details for the Base44 AI Gateway.
|
|
6
3
|
*/
|
|
7
4
|
export interface AiGatewayConnection {
|
|
8
|
-
/** Base URL of the gateway's OpenAI-compatible endpoint. */
|
|
5
|
+
/** Base URL of the gateway's OpenAI-compatible Chat Completions endpoint. */
|
|
9
6
|
baseURL: string;
|
|
10
|
-
/**
|
|
7
|
+
/**
|
|
8
|
+
* Bearer token that authenticates the request. Empty string when the caller is
|
|
9
|
+
* unauthenticated.
|
|
10
|
+
*/
|
|
11
11
|
token: string;
|
|
12
12
|
}
|
|
13
13
|
/**
|
|
@@ -25,13 +25,45 @@ export interface AiGatewayModuleConfig {
|
|
|
25
25
|
/**
|
|
26
26
|
* AI Gateway module for calling Base44's managed AI models from your own code.
|
|
27
27
|
*
|
|
28
|
-
*
|
|
29
|
-
* OpenAI-compatible
|
|
30
|
-
*
|
|
31
|
-
*
|
|
28
|
+
* `connection()` hands you a `baseURL` and `token` that authenticate as your
|
|
29
|
+
* Base44 app. An OpenAI-compatible client is any library, such as the `openai`
|
|
30
|
+
* SDK or the Vercel AI SDK, that has the same request and response format
|
|
31
|
+
* as OpenAI's Chat Completions API and lets you point it at a custom `baseURL`
|
|
32
|
+
* instead of OpenAI's own servers. Pass `connection()`'s values to one of
|
|
33
|
+
* these clients and it works against Base44's gateway exactly as it would
|
|
34
|
+
* against the provider directly, no separate account, API key, or billing
|
|
35
|
+
* setup with the underlying model provider required.
|
|
36
|
+
*
|
|
37
|
+
* Call `connection()` from a backend function rather than the browser. This
|
|
38
|
+
* keeps your instructions, tools, and business logic server-side, and lets
|
|
39
|
+
* you enforce your own auth, rate, and spend limits around the call. The
|
|
40
|
+
* `token` it returns is the caller's regular session token, the same one
|
|
41
|
+
* used for every other SDK call.
|
|
42
|
+
*
|
|
43
|
+
* ## Models
|
|
44
|
+
*
|
|
45
|
+
* You can use any of the [models available through `InvokeLLM`](/developers/references/sdk/docs/type-aliases/integrations#invokellm).
|
|
46
|
+
* Pass `'automatic'` to let Base44 choose one, or pin a specific model such
|
|
47
|
+
* as `'claude_sonnet_4_6'`, `'gpt_5_5'`, or `'gemini_3_1_pro'`.
|
|
48
|
+
*
|
|
49
|
+
* ## Authentication Modes
|
|
50
|
+
*
|
|
51
|
+
* There's no permission difference between modes. Both just determine which
|
|
52
|
+
* token `connection()` returns:
|
|
53
|
+
*
|
|
54
|
+
* - **User authentication** (`base44.aiGateway`): Returns the signed-in app user's token.
|
|
55
|
+
* - **Service role authentication** (`base44.asServiceRole.aiGateway`): Returns the service-role token instead, for calling the gateway when there's no signed-in user, such as from a scheduled automation.
|
|
56
|
+
*
|
|
57
|
+
* ## Billing and limits
|
|
58
|
+
*
|
|
59
|
+
* Requests are billed to your app's credit quota, which is the same shared
|
|
60
|
+
* quota your app's built-in AI features use, and isn't split per user. If the
|
|
61
|
+
* app runs out of credits, the gateway stops working for every user of the
|
|
62
|
+
* app until the quota resets. A request is rejected before the model runs if
|
|
63
|
+
* the app is out of credits. If you need to cap usage per user, build that
|
|
64
|
+
* check yourself, for example by tracking calls per user in your own entity.
|
|
32
65
|
*
|
|
33
|
-
*
|
|
34
|
-
* service-role token via `base44.asServiceRole.aiGateway`.
|
|
66
|
+
* Streaming responses aren't supported yet, so leave `stream` unset on your requests.
|
|
35
67
|
*/
|
|
36
68
|
export interface AiGatewayModule {
|
|
37
69
|
/**
|
|
@@ -39,19 +71,38 @@ export interface AiGatewayModule {
|
|
|
39
71
|
*
|
|
40
72
|
* Returns the `baseURL` and `token` to pass to any OpenAI-compatible client.
|
|
41
73
|
*
|
|
42
|
-
* The `token` is the current caller's bearer token: the app user's token for
|
|
43
|
-
* `base44.aiGateway`, or the service-role token for `base44.asServiceRole.aiGateway`.
|
|
44
|
-
* When the caller is unauthenticated, `token` is an empty string.
|
|
45
|
-
*
|
|
46
74
|
* @returns The gateway {@linkcode AiGatewayConnection | connection} (`baseURL` and `token`).
|
|
47
75
|
*
|
|
48
76
|
* @example
|
|
49
77
|
* ```typescript
|
|
78
|
+
* // Call a model directly
|
|
79
|
+
* import { createClientFromRequest } from "@base44/sdk";
|
|
80
|
+
* import OpenAI from "openai";
|
|
81
|
+
*
|
|
82
|
+
* // Runs inside a backend function
|
|
83
|
+
* const base44 = createClientFromRequest(request);
|
|
84
|
+
* const { baseURL, token } = base44.aiGateway.connection();
|
|
85
|
+
* const openai = new OpenAI({ baseURL, apiKey: token });
|
|
86
|
+
*
|
|
87
|
+
* const response = await openai.chat.completions.create({
|
|
88
|
+
* model: "automatic",
|
|
89
|
+
* messages: [{ role: "user", content: "Summarize this week's top support tickets." }],
|
|
90
|
+
* });
|
|
91
|
+
*
|
|
92
|
+
* console.log(response.choices[0].message.content);
|
|
93
|
+
* ```
|
|
94
|
+
*
|
|
95
|
+
* @example
|
|
96
|
+
* ```typescript
|
|
97
|
+
* // Use a tool-calling agent
|
|
98
|
+
* import { createClientFromRequest } from "@base44/sdk";
|
|
50
99
|
* import { ToolLoopAgent, tool, stepCountIs, hasToolCall } from "ai";
|
|
51
100
|
* import { createOpenAICompatible } from "@ai-sdk/openai-compatible";
|
|
52
101
|
* import { z } from "zod";
|
|
53
102
|
*
|
|
54
|
-
*
|
|
103
|
+
* // Runs inside a backend function, reviewing a return request
|
|
104
|
+
* const base44 = createClientFromRequest(request);
|
|
105
|
+
* const returnRequest = await base44.entities.ReturnRequest.get(returnId);
|
|
55
106
|
* const { baseURL, token } = base44.aiGateway.connection();
|
|
56
107
|
* // Point any OpenAI-compatible client at `baseURL` with `apiKey: token`.
|
|
57
108
|
* const models = createOpenAICompatible({ name: "base44", baseURL, apiKey: token });
|
|
@@ -66,7 +117,7 @@ export interface AiGatewayModule {
|
|
|
66
117
|
* description: "This customer's past orders, optionally filtered by status",
|
|
67
118
|
* inputSchema: z.object({ status: z.string().optional() }),
|
|
68
119
|
* execute: ({ status }) => {
|
|
69
|
-
* const query = { customer_email:
|
|
120
|
+
* const query = { customer_email: returnRequest.customer_email };
|
|
70
121
|
* if (status) query.status = status;
|
|
71
122
|
* return base44.entities.Order.filter(query, "-created_date", 50);
|
|
72
123
|
* },
|
|
@@ -81,7 +132,7 @@ export interface AiGatewayModule {
|
|
|
81
132
|
* stopWhen: [stepCountIs(8), hasToolCall("submitVerdict")],
|
|
82
133
|
* });
|
|
83
134
|
*
|
|
84
|
-
* await agent.generate({ prompt: `Review this return request: ${JSON.stringify(
|
|
135
|
+
* await agent.generate({ prompt: `Review this return request: ${JSON.stringify(returnRequest)}` });
|
|
85
136
|
* ```
|
|
86
137
|
*/
|
|
87
138
|
connection(): AiGatewayConnection;
|
|
@@ -1,6 +1,5 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* Response from SSO access token endpoint.
|
|
3
|
-
* @internal
|
|
4
3
|
*/
|
|
5
4
|
export interface SsoAccessTokenResponse {
|
|
6
5
|
access_token: string;
|
|
@@ -13,46 +12,53 @@ export interface SsoAccessTokenResponse {
|
|
|
13
12
|
* services.
|
|
14
13
|
*
|
|
15
14
|
* This module is only available to use with a client in service role authentication mode, which means it can only be used in backend environments.
|
|
16
|
-
*
|
|
17
|
-
* @internal
|
|
18
|
-
*
|
|
19
|
-
* @example
|
|
20
|
-
* ```typescript
|
|
21
|
-
* // Access SSO module with service role
|
|
22
|
-
* const response = await base44.asServiceRole.sso.getAccessToken('user_123');
|
|
23
|
-
* console.log(response.data.access_token);
|
|
24
|
-
* ```
|
|
25
15
|
*/
|
|
26
16
|
export interface SsoModule {
|
|
27
17
|
/**
|
|
28
|
-
* Gets SSO access token for
|
|
18
|
+
* Gets an SSO access token for the user who made the current request.
|
|
29
19
|
*
|
|
30
|
-
*
|
|
31
|
-
*
|
|
20
|
+
* Use this token to authenticate the user with external systems or services.
|
|
21
|
+
* This only works for that same user. Create the client with
|
|
22
|
+
* {@link createClientFromRequest} so it acts on behalf of the request's user,
|
|
23
|
+
* then pass that user's ID as `userid`. If `userid` is any other user, the
|
|
24
|
+
* call fails. An expired token is refreshed automatically when a refresh token
|
|
25
|
+
* is available.
|
|
32
26
|
*
|
|
33
|
-
* @param userid - The user
|
|
27
|
+
* @param userid - The ID of the user who made the current request, such as the
|
|
28
|
+
* `id` returned by {@link AuthModule | base44.auth.me()}.
|
|
34
29
|
* @returns Promise resolving to the SSO access token response.
|
|
35
30
|
*
|
|
36
31
|
* @example
|
|
37
32
|
* ```typescript
|
|
38
|
-
* // Get SSO access token
|
|
39
|
-
*
|
|
40
|
-
*
|
|
33
|
+
* // Get the user's SSO access token to call an external system
|
|
34
|
+
* import { createClientFromRequest } from 'npm:@base44/sdk';
|
|
35
|
+
*
|
|
36
|
+
* Deno.serve(async (req) => {
|
|
37
|
+
* const base44 = createClientFromRequest(req);
|
|
38
|
+
* const user = await base44.auth.me();
|
|
39
|
+
* const { access_token } = await base44.asServiceRole.sso.getAccessToken(user.id);
|
|
40
|
+
*
|
|
41
|
+
* return Response.json({ access_token });
|
|
42
|
+
* });
|
|
41
43
|
* ```
|
|
42
44
|
*/
|
|
43
45
|
getAccessToken(userid: string): Promise<SsoAccessTokenResponse>;
|
|
44
46
|
/**
|
|
45
|
-
* Gets the stored SSO OIDC ID token for the current
|
|
47
|
+
* Gets the stored SSO OIDC ID token for the user who made the current request.
|
|
46
48
|
*
|
|
47
|
-
*
|
|
48
|
-
*
|
|
49
|
-
*
|
|
49
|
+
* This only works for that same user, not for arbitrary users. Create the
|
|
50
|
+
* client with {@link createClientFromRequest} so it acts on behalf of the
|
|
51
|
+
* request's user, then pass that user's ID as `userid`. If `userid` is any
|
|
52
|
+
* other user, the call fails. The stored token is returned as-is and is never
|
|
53
|
+
* refreshed, so the call fails if the token has already expired.
|
|
50
54
|
*
|
|
51
|
-
* @param userid - The
|
|
55
|
+
* @param userid - The ID of the user who made the current request, such as the
|
|
56
|
+
* `id` returned by {@link AuthModule | base44.auth.me()}.
|
|
52
57
|
* @returns Promise resolving to the raw ID-token string.
|
|
53
58
|
*
|
|
54
59
|
* @example
|
|
55
60
|
* ```typescript
|
|
61
|
+
* // Get the user's ID token to read identity claims such as email
|
|
56
62
|
* import { createClientFromRequest } from 'npm:@base44/sdk';
|
|
57
63
|
*
|
|
58
64
|
* Deno.serve(async (req) => {
|