@base44-preview/sdk 0.8.37-pr.226.6c62736 → 0.8.37-pr.228.93ccd45
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
|
@@ -151,7 +151,7 @@ export function createClient(config) {
|
|
|
151
151
|
serverUrl,
|
|
152
152
|
token,
|
|
153
153
|
}),
|
|
154
|
-
aiGateway: createAiGatewayModule({ serverUrl, token
|
|
154
|
+
aiGateway: createAiGatewayModule({ serverUrl, token }),
|
|
155
155
|
appLogs: createAppLogsModule(axiosClient, appId),
|
|
156
156
|
users: createUsersModule(axiosClient, appId),
|
|
157
157
|
analytics: createAnalyticsModule({
|
|
@@ -194,7 +194,7 @@ export function createClient(config) {
|
|
|
194
194
|
serverUrl,
|
|
195
195
|
token,
|
|
196
196
|
}),
|
|
197
|
-
aiGateway: createAiGatewayModule({ serverUrl, token: serviceToken
|
|
197
|
+
aiGateway: createAiGatewayModule({ serverUrl, token: serviceToken }),
|
|
198
198
|
appLogs: createAppLogsModule(serviceRoleAxiosClient, appId),
|
|
199
199
|
cleanup: () => {
|
|
200
200
|
if (socket) {
|
|
@@ -346,6 +346,7 @@ export function createClientFromRequest(request) {
|
|
|
346
346
|
const serverUrlHeader = request.headers.get("Base44-Api-Url");
|
|
347
347
|
const functionsVersion = request.headers.get("Base44-Functions-Version");
|
|
348
348
|
const stateHeader = request.headers.get("Base44-State");
|
|
349
|
+
const dataEnvHeader = request.headers.get("X-Data-Env");
|
|
349
350
|
if (!appId) {
|
|
350
351
|
throw new Error("Base44-App-Id header is required, but is was not found on the request");
|
|
351
352
|
}
|
|
@@ -373,6 +374,14 @@ export function createClientFromRequest(request) {
|
|
|
373
374
|
if (stateHeader) {
|
|
374
375
|
additionalHeaders["Base44-State"] = stateHeader;
|
|
375
376
|
}
|
|
377
|
+
// Propagate the data environment so entity operations from the function stay
|
|
378
|
+
// in the same environment (e.g. test data) as the triggering request. This
|
|
379
|
+
// matters for the user-scoped client: unlike the service token, the user JWT
|
|
380
|
+
// carries no data-env, so without forwarding this header the callbacks fall
|
|
381
|
+
// back to production data even when the app runs in test-data mode.
|
|
382
|
+
if (dataEnvHeader) {
|
|
383
|
+
additionalHeaders["X-Data-Env"] = dataEnvHeader;
|
|
384
|
+
}
|
|
376
385
|
return createClient({
|
|
377
386
|
serverUrl: serverUrlHeader || "https://base44.app",
|
|
378
387
|
appId,
|
|
@@ -1,2 +1,2 @@
|
|
|
1
1
|
import { AiGatewayModule, AiGatewayModuleConfig } from "./ai-gateway.types.js";
|
|
2
|
-
export declare function createAiGatewayModule({ serverUrl, token,
|
|
2
|
+
export declare function createAiGatewayModule({ serverUrl, token, }: AiGatewayModuleConfig): AiGatewayModule;
|
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import { getAccessToken } from "../utils/auth-utils.js";
|
|
2
|
-
export function createAiGatewayModule({ serverUrl, token,
|
|
2
|
+
export function createAiGatewayModule({ serverUrl, token, }) {
|
|
3
3
|
const connection = () => {
|
|
4
4
|
var _a;
|
|
5
5
|
return ({
|
|
6
|
-
baseURL: `${serverUrl}/api/
|
|
6
|
+
baseURL: `${serverUrl}/api/ai/openai/v1`,
|
|
7
7
|
token: (_a = token !== null && token !== void 0 ? token : getAccessToken()) !== null && _a !== void 0 ? _a : "",
|
|
8
8
|
});
|
|
9
9
|
};
|
|
@@ -1,8 +1,9 @@
|
|
|
1
1
|
/**
|
|
2
2
|
* A connection to the Base44 AI Gateway.
|
|
3
3
|
*
|
|
4
|
-
* Contains the base URL and bearer token to use with any OpenAI-compatible
|
|
5
|
-
*
|
|
4
|
+
* Contains the base URL and bearer token to use with any OpenAI-compatible client
|
|
5
|
+
* (OpenAI SDK, Mastra, Vercel AI SDK, and others) pointed at the Base44 AI
|
|
6
|
+
* Gateway.
|
|
6
7
|
*/
|
|
7
8
|
export interface AiGatewayConnection {
|
|
8
9
|
/** Base URL of the gateway's OpenAI-compatible endpoint. */
|
|
@@ -19,15 +20,13 @@ export interface AiGatewayModuleConfig {
|
|
|
19
20
|
serverUrl?: string;
|
|
20
21
|
/** Authentication token */
|
|
21
22
|
token?: string;
|
|
22
|
-
/** Application ID */
|
|
23
|
-
appId: string;
|
|
24
23
|
}
|
|
25
24
|
/**
|
|
26
25
|
* AI Gateway module for calling Base44's managed AI models from your own code.
|
|
27
26
|
*
|
|
28
27
|
* The gateway exposes an OpenAI-compatible Chat Completions endpoint, so any
|
|
29
28
|
* OpenAI-compatible SDK works against it:
|
|
30
|
-
* - Build custom AI agents
|
|
29
|
+
* - Build custom AI agents with agent SDKs such as Mastra or the Vercel AI SDK
|
|
31
30
|
* - Uses your app's models, billing, and credit quota, no API key to manage
|
|
32
31
|
*
|
|
33
32
|
* Available in user authentication mode (`base44.aiGateway`) and with the
|
|
@@ -47,25 +46,54 @@ export interface AiGatewayModule {
|
|
|
47
46
|
*
|
|
48
47
|
* @example
|
|
49
48
|
* ```typescript
|
|
50
|
-
* //
|
|
49
|
+
* // Build an AI agent with Mastra on top of the gateway, inside a backend function
|
|
51
50
|
* import { createClientFromRequest } from 'npm:@base44/sdk';
|
|
51
|
+
* import { Agent } from 'npm:@mastra/core/agent';
|
|
52
|
+
* import { createTool } from 'npm:@mastra/core/tools';
|
|
53
|
+
* import { createOpenAICompatible } from 'npm:@ai-sdk/openai-compatible';
|
|
54
|
+
* import { z } from 'npm:zod';
|
|
52
55
|
*
|
|
53
56
|
* Deno.serve(async (req) => {
|
|
54
57
|
* const base44 = createClientFromRequest(req);
|
|
55
58
|
* const { baseURL, token } = base44.aiGateway.connection();
|
|
59
|
+
* const models = createOpenAICompatible({ name: 'base44', baseURL, apiKey: token });
|
|
56
60
|
*
|
|
57
|
-
*
|
|
58
|
-
*
|
|
59
|
-
*
|
|
60
|
-
*
|
|
61
|
-
*
|
|
62
|
-
*
|
|
63
|
-
*
|
|
64
|
-
*
|
|
65
|
-
*
|
|
61
|
+
* const agent = new Agent({
|
|
62
|
+
* id: 'order-helper',
|
|
63
|
+
* name: 'order-helper',
|
|
64
|
+
* instructions: 'Help the user with their orders.',
|
|
65
|
+
* model: models('claude_sonnet_4_6'),
|
|
66
|
+
* tools: {
|
|
67
|
+
* lookupOrder: createTool({
|
|
68
|
+
* id: 'lookup-order',
|
|
69
|
+
* description: 'Fetch an order by id',
|
|
70
|
+
* inputSchema: z.object({ orderId: z.string() }),
|
|
71
|
+
* execute: async ({ orderId }) => base44.entities.Order.get(orderId),
|
|
72
|
+
* }),
|
|
73
|
+
* },
|
|
66
74
|
* });
|
|
67
|
-
*
|
|
68
|
-
*
|
|
75
|
+
*
|
|
76
|
+
* const { text } = await agent.generate('Where is order 123?');
|
|
77
|
+
* return Response.json({ text });
|
|
78
|
+
* });
|
|
79
|
+
* ```
|
|
80
|
+
*
|
|
81
|
+
* @example
|
|
82
|
+
* ```typescript
|
|
83
|
+
* // Call a model directly with the OpenAI SDK
|
|
84
|
+
* import { createClientFromRequest } from 'npm:@base44/sdk';
|
|
85
|
+
* import OpenAI from 'npm:openai';
|
|
86
|
+
*
|
|
87
|
+
* Deno.serve(async (req) => {
|
|
88
|
+
* const base44 = createClientFromRequest(req);
|
|
89
|
+
* const { baseURL, token } = base44.aiGateway.connection();
|
|
90
|
+
*
|
|
91
|
+
* const openai = new OpenAI({ baseURL, apiKey: token });
|
|
92
|
+
* const res = await openai.chat.completions.create({
|
|
93
|
+
* model: 'claude_sonnet_4_6',
|
|
94
|
+
* messages: [{ role: 'user', content: 'Hello!' }],
|
|
95
|
+
* });
|
|
96
|
+
* return Response.json({ text: res.choices[0].message.content });
|
|
69
97
|
* });
|
|
70
98
|
* ```
|
|
71
99
|
*/
|