@base44/sdk 0.7.0 → 0.7.3

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.d.ts CHANGED
@@ -1,3 +1,7 @@
1
+ export type CreateClientOptions = {
2
+ onError?: (error: Error) => void;
3
+ };
4
+ export type Base44Client = ReturnType<typeof createClient>;
1
5
  /**
2
6
  * Create a Base44 client instance
3
7
  * @param {Object} config - Client configuration
@@ -14,6 +18,9 @@ export declare function createClient(config: {
14
18
  token?: string;
15
19
  serviceToken?: string;
16
20
  requiresAuth?: boolean;
21
+ functionsVersion?: string;
22
+ options?: CreateClientOptions;
23
+ onRedirectToLogin?: () => void;
17
24
  }): {
18
25
  /**
19
26
  * Set authentication token for all requests
@@ -38,6 +45,18 @@ export declare function createClient(config: {
38
45
  functions: {
39
46
  invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
40
47
  };
48
+ agents: {
49
+ getConversations: () => Promise<import("./types.js").AgentConversation[]>;
50
+ getConversation: (conversationId: string) => Promise<import("./types.js").AgentConversation | undefined>;
51
+ listConversations: (filterParams: import("./types.js").ModelFilterParams) => Promise<import("./types.js").AgentConversation[]>;
52
+ createConversation: (conversation: {
53
+ agent_name: string;
54
+ metadata?: Record<string, any>;
55
+ }) => Promise<import("./types.js").AgentConversation>;
56
+ addMessage: (conversation: import("./types.js").AgentConversation, message: import("./types.js").AgentMessage) => Promise<import("./types.js").AgentMessage>;
57
+ subscribeToConversation: (conversationId: string, onUpdate?: (conversation: import("./types.js").AgentConversation) => void) => () => void;
58
+ };
59
+ cleanup: () => void;
41
60
  };
42
61
  entities: {};
43
62
  integrations: {};
@@ -56,6 +75,18 @@ export declare function createClient(config: {
56
75
  functions: {
57
76
  invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
58
77
  };
78
+ agents: {
79
+ getConversations: () => Promise<import("./types.js").AgentConversation[]>;
80
+ getConversation: (conversationId: string) => Promise<import("./types.js").AgentConversation | undefined>;
81
+ listConversations: (filterParams: import("./types.js").ModelFilterParams) => Promise<import("./types.js").AgentConversation[]>;
82
+ createConversation: (conversation: {
83
+ agent_name: string;
84
+ metadata?: Record<string, any>;
85
+ }) => Promise<import("./types.js").AgentConversation>;
86
+ addMessage: (conversation: import("./types.js").AgentConversation, message: import("./types.js").AgentMessage) => Promise<import("./types.js").AgentMessage>;
87
+ subscribeToConversation: (conversationId: string, onUpdate?: (conversation: import("./types.js").AgentConversation) => void) => () => void;
88
+ };
89
+ cleanup: () => void;
59
90
  };
60
91
  export declare function createClientFromRequest(request: Request): {
61
92
  /**
@@ -81,6 +112,18 @@ export declare function createClientFromRequest(request: Request): {
81
112
  functions: {
82
113
  invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
83
114
  };
115
+ agents: {
116
+ getConversations: () => Promise<import("./types.js").AgentConversation[]>;
117
+ getConversation: (conversationId: string) => Promise<import("./types.js").AgentConversation | undefined>;
118
+ listConversations: (filterParams: import("./types.js").ModelFilterParams) => Promise<import("./types.js").AgentConversation[]>;
119
+ createConversation: (conversation: {
120
+ agent_name: string;
121
+ metadata?: Record<string, any>;
122
+ }) => Promise<import("./types.js").AgentConversation>;
123
+ addMessage: (conversation: import("./types.js").AgentConversation, message: import("./types.js").AgentMessage) => Promise<import("./types.js").AgentMessage>;
124
+ subscribeToConversation: (conversationId: string, onUpdate?: (conversation: import("./types.js").AgentConversation) => void) => () => void;
125
+ };
126
+ cleanup: () => void;
84
127
  };
85
128
  entities: {};
86
129
  integrations: {};
@@ -99,4 +142,16 @@ export declare function createClientFromRequest(request: Request): {
99
142
  functions: {
100
143
  invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
101
144
  };
145
+ agents: {
146
+ getConversations: () => Promise<import("./types.js").AgentConversation[]>;
147
+ getConversation: (conversationId: string) => Promise<import("./types.js").AgentConversation | undefined>;
148
+ listConversations: (filterParams: import("./types.js").ModelFilterParams) => Promise<import("./types.js").AgentConversation[]>;
149
+ createConversation: (conversation: {
150
+ agent_name: string;
151
+ metadata?: Record<string, any>;
152
+ }) => Promise<import("./types.js").AgentConversation>;
153
+ addMessage: (conversation: import("./types.js").AgentConversation, message: import("./types.js").AgentMessage) => Promise<import("./types.js").AgentMessage>;
154
+ subscribeToConversation: (conversationId: string, onUpdate?: (conversation: import("./types.js").AgentConversation) => void) => () => void;
155
+ };
156
+ cleanup: () => void;
102
157
  };
package/dist/client.js CHANGED
@@ -5,6 +5,8 @@ import { createAuthModule } from "./modules/auth.js";
5
5
  import { createSsoModule } from "./modules/sso.js";
6
6
  import { getAccessToken } from "./utils/auth-utils.js";
7
7
  import { createFunctionsModule } from "./modules/functions.js";
8
+ import { createAgentsModule } from "./modules/agents.js";
9
+ import { RoomsSocket } from "./utils/socket-utils.js";
8
10
  /**
9
11
  * Create a Base44 client instance
10
12
  * @param {Object} config - Client configuration
@@ -16,58 +18,95 @@ import { createFunctionsModule } from "./modules/functions.js";
16
18
  * @returns {Object} Base44 client instance
17
19
  */
18
20
  export function createClient(config) {
19
- const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, } = config;
21
+ const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, options, functionsVersion, onRedirectToLogin, } = config;
22
+ const socketConfig = {
23
+ serverUrl,
24
+ mountPath: "/ws-user-apps/socket.io/",
25
+ transports: ["websocket"],
26
+ appId,
27
+ token,
28
+ };
29
+ const socket = RoomsSocket({
30
+ config: socketConfig,
31
+ });
32
+ const headers = {
33
+ "X-App-Id": String(appId),
34
+ };
35
+ const functionHeaders = functionsVersion
36
+ ? {
37
+ ...headers,
38
+ "Base44-Functions-Version": functionsVersion,
39
+ }
40
+ : headers;
20
41
  const axiosClient = createAxiosClient({
21
42
  baseURL: `${serverUrl}/api`,
22
- headers: {
23
- "X-App-Id": String(appId),
24
- },
43
+ headers,
25
44
  token,
26
45
  requiresAuth,
27
46
  appId,
28
47
  serverUrl,
48
+ onError: options === null || options === void 0 ? void 0 : options.onError,
49
+ onRedirectToLogin,
29
50
  });
30
51
  const functionsAxiosClient = createAxiosClient({
31
52
  baseURL: `${serverUrl}/api`,
32
- headers: {
33
- "X-App-Id": String(appId),
34
- },
53
+ headers: functionHeaders,
35
54
  token,
36
55
  requiresAuth,
37
56
  appId,
38
57
  serverUrl,
39
58
  interceptResponses: false,
59
+ onError: options === null || options === void 0 ? void 0 : options.onError,
60
+ onRedirectToLogin,
40
61
  });
41
62
  const serviceRoleAxiosClient = createAxiosClient({
42
63
  baseURL: `${serverUrl}/api`,
43
- headers: {
44
- "X-App-Id": String(appId),
45
- },
64
+ headers,
46
65
  token: serviceToken,
47
66
  serverUrl,
48
67
  appId,
68
+ onError: options === null || options === void 0 ? void 0 : options.onError,
69
+ onRedirectToLogin,
49
70
  });
50
71
  const serviceRoleFunctionsAxiosClient = createAxiosClient({
51
72
  baseURL: `${serverUrl}/api`,
52
- headers: {
53
- "X-App-Id": String(appId),
54
- },
73
+ headers: functionHeaders,
55
74
  token: serviceToken,
56
75
  serverUrl,
57
76
  appId,
58
77
  interceptResponses: false,
78
+ onRedirectToLogin,
59
79
  });
60
80
  const userModules = {
61
81
  entities: createEntitiesModule(axiosClient, appId),
62
82
  integrations: createIntegrationsModule(axiosClient, appId),
63
- auth: createAuthModule(axiosClient, functionsAxiosClient, appId),
83
+ auth: createAuthModule(axiosClient, functionsAxiosClient, appId, {
84
+ onRedirectToLogin,
85
+ serverUrl,
86
+ }),
64
87
  functions: createFunctionsModule(functionsAxiosClient, appId),
88
+ agents: createAgentsModule({
89
+ axios: axiosClient,
90
+ socket,
91
+ appId,
92
+ }),
93
+ cleanup: () => {
94
+ socket.disconnect();
95
+ },
65
96
  };
66
97
  const serviceRoleModules = {
67
98
  entities: createEntitiesModule(serviceRoleAxiosClient, appId),
68
99
  integrations: createIntegrationsModule(serviceRoleAxiosClient, appId),
69
100
  sso: createSsoModule(serviceRoleAxiosClient, appId, token),
70
101
  functions: createFunctionsModule(serviceRoleFunctionsAxiosClient, appId),
102
+ agents: createAgentsModule({
103
+ axios: serviceRoleAxiosClient,
104
+ socket,
105
+ appId,
106
+ }),
107
+ cleanup: () => {
108
+ socket.disconnect();
109
+ },
71
110
  };
72
111
  // Always try to get token from localStorage or URL parameters
73
112
  if (typeof window !== "undefined") {
@@ -102,6 +141,9 @@ export function createClient(config) {
102
141
  */
103
142
  setToken(newToken) {
104
143
  userModules.auth.setToken(newToken);
144
+ socket.updateConfig({
145
+ token: newToken,
146
+ });
105
147
  },
106
148
  /**
107
149
  * Get current configuration
@@ -120,10 +162,10 @@ export function createClient(config) {
120
162
  */
121
163
  get asServiceRole() {
122
164
  if (!serviceToken) {
123
- throw new Error('Service token is required to use asServiceRole. Please provide a serviceToken when creating the client.');
165
+ throw new Error("Service token is required to use asServiceRole. Please provide a serviceToken when creating the client.");
124
166
  }
125
167
  return serviceRoleModules;
126
- }
168
+ },
127
169
  };
128
170
  return client;
129
171
  }
@@ -132,6 +174,7 @@ export function createClientFromRequest(request) {
132
174
  const serviceRoleAuthHeader = request.headers.get("Base44-Service-Authorization");
133
175
  const appId = request.headers.get("Base44-App-Id");
134
176
  const serverUrlHeader = request.headers.get("Base44-Api-Url");
177
+ const functionsVersion = request.headers.get("Base44-Functions-Version");
135
178
  if (!appId) {
136
179
  throw new Error("Base44-App-Id header is required, but is was not found on the request");
137
180
  }
@@ -139,21 +182,26 @@ export function createClientFromRequest(request) {
139
182
  let serviceRoleToken;
140
183
  let userToken;
141
184
  if (serviceRoleAuthHeader !== null) {
142
- if (serviceRoleAuthHeader === '' || !serviceRoleAuthHeader.startsWith('Bearer ') || serviceRoleAuthHeader.split(' ').length !== 2) {
185
+ if (serviceRoleAuthHeader === "" ||
186
+ !serviceRoleAuthHeader.startsWith("Bearer ") ||
187
+ serviceRoleAuthHeader.split(" ").length !== 2) {
143
188
  throw new Error('Invalid authorization header format. Expected "Bearer <token>"');
144
189
  }
145
- serviceRoleToken = serviceRoleAuthHeader.split(' ')[1];
190
+ serviceRoleToken = serviceRoleAuthHeader.split(" ")[1];
146
191
  }
147
192
  if (authHeader !== null) {
148
- if (authHeader === '' || !authHeader.startsWith('Bearer ') || authHeader.split(' ').length !== 2) {
193
+ if (authHeader === "" ||
194
+ !authHeader.startsWith("Bearer ") ||
195
+ authHeader.split(" ").length !== 2) {
149
196
  throw new Error('Invalid authorization header format. Expected "Bearer <token>"');
150
197
  }
151
- userToken = authHeader.split(' ')[1];
198
+ userToken = authHeader.split(" ")[1];
152
199
  }
153
200
  return createClient({
154
201
  serverUrl: serverUrlHeader || "https://base44.app",
155
202
  appId,
156
203
  token: userToken,
157
204
  serviceToken: serviceRoleToken,
205
+ functionsVersion: functionsVersion !== null && functionsVersion !== void 0 ? functionsVersion : undefined,
158
206
  });
159
207
  }
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- import { createClient, createClientFromRequest } from "./client.js";
1
+ import { createClient, createClientFromRequest, type Base44Client } from "./client.js";
2
2
  import { Base44Error } from "./utils/axios-client.js";
3
3
  import { getAccessToken, saveAccessToken, removeAccessToken, getLoginUrl } from "./utils/auth-utils.js";
4
4
  export { createClient, createClientFromRequest, Base44Error, getAccessToken, saveAccessToken, removeAccessToken, getLoginUrl, };
5
+ export type { Base44Client };
6
+ export * from "./types.js";
package/dist/index.js CHANGED
@@ -2,3 +2,4 @@ import { createClient, createClientFromRequest } from "./client.js";
2
2
  import { Base44Error } from "./utils/axios-client.js";
3
3
  import { getAccessToken, saveAccessToken, removeAccessToken, getLoginUrl, } from "./utils/auth-utils.js";
4
4
  export { createClient, createClientFromRequest, Base44Error, getAccessToken, saveAccessToken, removeAccessToken, getLoginUrl, };
5
+ export * from "./types.js";
@@ -0,0 +1,20 @@
1
+ import { RoomsSocket } from "../utils/socket-utils.js";
2
+ import { AgentConversation, AgentMessage } from "./agents.types.js";
3
+ import { AxiosInstance } from "axios";
4
+ import { ModelFilterParams } from "../types.js";
5
+ export type AgentsModuleConfig = {
6
+ axios: AxiosInstance;
7
+ socket: ReturnType<typeof RoomsSocket>;
8
+ appId: string;
9
+ };
10
+ export declare function createAgentsModule({ axios, socket, appId, }: AgentsModuleConfig): {
11
+ getConversations: () => Promise<AgentConversation[]>;
12
+ getConversation: (conversationId: string) => Promise<AgentConversation | undefined>;
13
+ listConversations: (filterParams: ModelFilterParams) => Promise<AgentConversation[]>;
14
+ createConversation: (conversation: {
15
+ agent_name: string;
16
+ metadata?: Record<string, any>;
17
+ }) => Promise<AgentConversation>;
18
+ addMessage: (conversation: AgentConversation, message: AgentMessage) => Promise<AgentMessage>;
19
+ subscribeToConversation: (conversationId: string, onUpdate?: (conversation: AgentConversation) => void) => () => void;
20
+ };
@@ -0,0 +1,43 @@
1
+ export function createAgentsModule({ axios, socket, appId, }) {
2
+ const baseURL = `/apps/${appId}/agents`;
3
+ const getConversations = () => {
4
+ return axios.get(`${baseURL}/conversations`);
5
+ };
6
+ const getConversation = (conversationId) => {
7
+ return axios.get(`${baseURL}/conversations/${conversationId}`);
8
+ };
9
+ const listConversations = (filterParams) => {
10
+ return axios.get(`${baseURL}/conversations`, {
11
+ params: filterParams,
12
+ });
13
+ };
14
+ const createConversation = (conversation) => {
15
+ return axios.post(`${baseURL}/conversations`, conversation);
16
+ };
17
+ const addMessage = async (conversation, message) => {
18
+ const room = `/agent-conversations/${conversation.id}`;
19
+ await socket.updateModel(room, {
20
+ ...conversation,
21
+ messages: [...(conversation.messages || []), message],
22
+ });
23
+ return axios.post(`${baseURL}/conversations/${conversation.id}/messages`, message);
24
+ };
25
+ const subscribeToConversation = (conversationId, onUpdate) => {
26
+ const room = `/agent-conversations/${conversationId}`;
27
+ return socket.subscribeToRoom(room, {
28
+ connect: () => { },
29
+ update_model: ({ data: jsonStr }) => {
30
+ const conv = JSON.parse(jsonStr);
31
+ onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(conv);
32
+ },
33
+ });
34
+ };
35
+ return {
36
+ getConversations,
37
+ getConversation,
38
+ listConversations,
39
+ createConversation,
40
+ addMessage,
41
+ subscribeToConversation,
42
+ };
43
+ }
@@ -0,0 +1,44 @@
1
+ export type AgentConversation = {
2
+ id: string;
3
+ app_id: string;
4
+ agent_name: string;
5
+ created_by_id: string;
6
+ messages: AgentMessage[];
7
+ metadata?: Record<string, any>;
8
+ };
9
+ export type AgentMessage = {
10
+ id: string;
11
+ role: "user" | "assistant" | "system";
12
+ reasoning?: {
13
+ start_date: string;
14
+ end_date?: string;
15
+ content: string;
16
+ };
17
+ content?: string | Record<string, any> | null;
18
+ file_urls?: string[] | null;
19
+ tool_calls?: {
20
+ id: string;
21
+ name: string;
22
+ arguments_string: string;
23
+ status: "running" | "success" | "error" | "stopped";
24
+ results?: string | null;
25
+ }[] | null;
26
+ usage?: {
27
+ prompt_tokens?: number;
28
+ completion_tokens?: number;
29
+ } | null;
30
+ hidden?: boolean;
31
+ custom_context?: {
32
+ message: string;
33
+ data: Record<string, any>;
34
+ type: string;
35
+ }[] | null;
36
+ model?: string | null;
37
+ checkpoint_id?: string | null;
38
+ metadata?: {
39
+ created_date: string;
40
+ created_by_email: string;
41
+ created_by_full_name: string | null;
42
+ };
43
+ additional_message_params?: Record<string, any>;
44
+ };
@@ -0,0 +1 @@
1
+ export {};
@@ -0,0 +1,115 @@
1
+ export interface AppMessageContent {
2
+ content?: string;
3
+ file_urls?: string[];
4
+ custom_context?: unknown;
5
+ additional_message_params?: Record<string, unknown>;
6
+ [key: string]: unknown;
7
+ }
8
+ export interface AppConversationMessage extends AppMessageContent {
9
+ id?: string | null;
10
+ role?: "user" | "assistant" | string;
11
+ }
12
+ export interface AppConversationLike {
13
+ id?: string | null;
14
+ messages?: AppMessageContent[] | null;
15
+ model?: string;
16
+ functions_fail_silently?: boolean;
17
+ }
18
+ export interface DenoProjectLike {
19
+ project_id: string;
20
+ project_name: string;
21
+ app_id: string;
22
+ deployment_name_to_info: Record<string, {
23
+ id: string;
24
+ code: string;
25
+ }>;
26
+ }
27
+ export interface AppLike {
28
+ id?: string;
29
+ conversation?: AppConversationLike | null;
30
+ app_stage?: "pending" | "product_flows" | "ready" | string;
31
+ created_date?: string;
32
+ updated_date?: string;
33
+ created_by?: string;
34
+ organization_id?: string;
35
+ name?: string;
36
+ user_description?: string;
37
+ entities?: Record<string, any>;
38
+ additional_user_data_schema?: any;
39
+ pages?: {
40
+ [key: string]: string;
41
+ };
42
+ components: {
43
+ [key: string]: any;
44
+ };
45
+ layout?: string;
46
+ globals_css?: string;
47
+ agents?: Record<string, any>;
48
+ logo_url?: string;
49
+ slug?: string;
50
+ public_settings?: "private_with_login" | "public_with_login" | "public_without_login" | "workspace_with_login" | string;
51
+ is_blocked?: boolean;
52
+ github_repo_url?: string;
53
+ main_page?: string;
54
+ installable_integrations?: any;
55
+ backend_project?: DenoProjectLike;
56
+ last_deployed_at?: string;
57
+ is_remixable?: boolean;
58
+ remixed_from_app_id?: string;
59
+ hide_entity_created_by?: boolean;
60
+ platform_version?: number;
61
+ enable_username_password?: boolean;
62
+ auth_config?: AuthConfigLike;
63
+ status?: {
64
+ state?: string;
65
+ details?: any;
66
+ last_updated_date?: string;
67
+ };
68
+ custom_instructions?: any;
69
+ frozen_files?: string[];
70
+ deep_coding_mode?: boolean;
71
+ needs_to_add_diff?: boolean;
72
+ installed_integration_context_items?: any[];
73
+ model?: string;
74
+ is_starred?: boolean;
75
+ agents_enabled?: boolean;
76
+ categories?: string[];
77
+ functions?: any;
78
+ function_names?: string[];
79
+ user_entity?: UserEntityLike;
80
+ app_code_hash?: string;
81
+ has_backend_functions_enabled?: boolean;
82
+ }
83
+ export interface UserLike {
84
+ id?: string | null;
85
+ }
86
+ export interface UserEntityLike {
87
+ type: string;
88
+ name: string;
89
+ title?: string;
90
+ properties?: {
91
+ role?: {
92
+ type?: string;
93
+ description?: string;
94
+ enum?: ("admin" | "user" | string)[];
95
+ };
96
+ email?: {
97
+ type?: string;
98
+ description?: string;
99
+ };
100
+ full_name?: {
101
+ type?: string;
102
+ description?: string;
103
+ };
104
+ };
105
+ required: string[];
106
+ }
107
+ export interface AuthConfigLike {
108
+ enable_username_password?: boolean;
109
+ enable_google_login?: boolean;
110
+ enable_microsoft_login?: boolean;
111
+ enable_facebook_login?: boolean;
112
+ sso_provider_name?: string;
113
+ enable_sso_login?: boolean;
114
+ }
115
+ export type LoginInfoResponse = Pick<AppLike, "id" | "name" | "slug" | "logo_url" | "user_description" | "updated_date" | "created_date" | "auth_config" | "platform_version">;
@@ -0,0 +1 @@
1
+ export {};
@@ -6,7 +6,10 @@ import { AxiosInstance } from "axios";
6
6
  * @param {string} serverUrl - Server URL
7
7
  * @returns {Object} Auth module with authentication methods
8
8
  */
9
- export declare function createAuthModule(axios: AxiosInstance, functionsAxiosClient: AxiosInstance, appId: string): {
9
+ export declare function createAuthModule(axios: AxiosInstance, functionsAxiosClient: AxiosInstance, appId: string, options: {
10
+ serverUrl: string;
11
+ onRedirectToLogin?: () => void;
12
+ }): {
10
13
  /**
11
14
  * Get current user information
12
15
  * @returns {Promise<Object>} Current user data
@@ -5,7 +5,7 @@
5
5
  * @param {string} serverUrl - Server URL
6
6
  * @returns {Object} Auth module with authentication methods
7
7
  */
8
- export function createAuthModule(axios, functionsAxiosClient, appId) {
8
+ export function createAuthModule(axios, functionsAxiosClient, appId, options) {
9
9
  return {
10
10
  /**
11
11
  * Get current user information
@@ -32,6 +32,10 @@ export function createAuthModule(axios, functionsAxiosClient, appId) {
32
32
  if (typeof window === "undefined") {
33
33
  throw new Error("Login method can only be used in a browser environment");
34
34
  }
35
+ if (options.onRedirectToLogin) {
36
+ options.onRedirectToLogin();
37
+ return;
38
+ }
35
39
  // If nextUrl is not provided, use the current URL
36
40
  const redirectUrl = nextUrl || window.location.href;
37
41
  // Build the login URL
@@ -77,6 +81,7 @@ export function createAuthModule(axios, functionsAxiosClient, appId) {
77
81
  setToken(token, saveToStorage = true) {
78
82
  if (!token)
79
83
  return;
84
+ // handle token change for axios clients
80
85
  axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
81
86
  functionsAxiosClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
82
87
  // Save token to localStorage if requested
@@ -0,0 +1,2 @@
1
+ export * from "./app.types.js";
2
+ export * from "./agents.types.js";
@@ -0,0 +1,2 @@
1
+ export * from "./app.types.js";
2
+ export * from "./agents.types.js";
@@ -0,0 +1,9 @@
1
+ export * from "./modules/types.js";
2
+ export type ModelFilterParams = {
3
+ q?: Record<string, any>;
4
+ sort?: string | null;
5
+ sort_by?: string | null;
6
+ limit?: number | null;
7
+ skip?: number | null;
8
+ fields?: string[] | null;
9
+ };
package/dist/types.js ADDED
@@ -0,0 +1 @@
1
+ export * from "./modules/types.js";
@@ -23,7 +23,7 @@ export declare class Base44Error extends Error {
23
23
  * @param {string} options.serverUrl - Server URL (needed for login redirect)
24
24
  * @returns {import('axios').AxiosInstance} Configured axios instance
25
25
  */
26
- export declare function createAxiosClient({ baseURL, headers, token, requiresAuth, appId, serverUrl, interceptResponses, }: {
26
+ export declare function createAxiosClient({ baseURL, headers, token, requiresAuth, appId, serverUrl, interceptResponses, onError, onRedirectToLogin, }: {
27
27
  baseURL: string;
28
28
  headers?: Record<string, string>;
29
29
  token?: string;
@@ -31,4 +31,6 @@ export declare function createAxiosClient({ baseURL, headers, token, requiresAut
31
31
  appId: string;
32
32
  serverUrl: string;
33
33
  interceptResponses?: boolean;
34
+ onError?: (error: Error) => void;
35
+ onRedirectToLogin?: () => void;
34
36
  }): import("axios").AxiosInstance;
@@ -64,7 +64,7 @@ function redirectToLogin(serverUrl, appId) {
64
64
  * @param {string} options.serverUrl - Server URL (needed for login redirect)
65
65
  * @returns {import('axios').AxiosInstance} Configured axios instance
66
66
  */
67
- export function createAxiosClient({ baseURL, headers = {}, token, requiresAuth = false, appId, serverUrl, interceptResponses = true, }) {
67
+ export function createAxiosClient({ baseURL, headers = {}, token, requiresAuth = false, appId, serverUrl, interceptResponses = true, onError, onRedirectToLogin, }) {
68
68
  const client = axios.create({
69
69
  baseURL,
70
70
  headers: {
@@ -104,9 +104,12 @@ export function createAxiosClient({ baseURL, headers = {}, token, requiresAuth =
104
104
  console.log("Authentication required. Redirecting to login...");
105
105
  // Use a slight delay to allow the error to propagate first
106
106
  setTimeout(() => {
107
- redirectToLogin(serverUrl, appId);
107
+ onRedirectToLogin
108
+ ? onRedirectToLogin()
109
+ : redirectToLogin(serverUrl, appId);
108
110
  }, 100);
109
111
  }
112
+ onError === null || onError === void 0 ? void 0 : onError(base44Error);
110
113
  return Promise.reject(base44Error);
111
114
  });
112
115
  }
@@ -0,0 +1,47 @@
1
+ import { Socket } from "socket.io-client";
2
+ export type RoomsSocketConfig = {
3
+ serverUrl: string;
4
+ mountPath: string;
5
+ transports: string[];
6
+ appId: string;
7
+ token?: string;
8
+ };
9
+ export type TSocketRoom = string;
10
+ export type TJsonStr = string;
11
+ type RoomsSocketEventsMap = {
12
+ listen: {
13
+ connect: () => Promise<void> | void;
14
+ update_model: (msg: {
15
+ room: string;
16
+ data: TJsonStr;
17
+ }) => Promise<void> | void;
18
+ error: (error: Error) => Promise<void> | void;
19
+ };
20
+ emit: {
21
+ join: (room: string) => void;
22
+ leave: (room: string) => void;
23
+ };
24
+ };
25
+ type TEvent = keyof RoomsSocketEventsMap["listen"];
26
+ type THandler<E extends TEvent> = RoomsSocketEventsMap["listen"][E];
27
+ export type RoomsSocket = ReturnType<typeof RoomsSocket>;
28
+ export declare function RoomsSocket({ config }: {
29
+ config: RoomsSocketConfig;
30
+ }): {
31
+ socket: Socket<{
32
+ connect: () => Promise<void> | void;
33
+ update_model: (msg: {
34
+ room: string;
35
+ data: TJsonStr;
36
+ }) => Promise<void> | void;
37
+ error: (error: Error) => Promise<void> | void;
38
+ }, {
39
+ join: (room: string) => void;
40
+ leave: (room: string) => void;
41
+ }>;
42
+ subscribeToRoom: (room: TSocketRoom, handlers: Partial<{ [k in TEvent]: THandler<k>; }>) => () => void;
43
+ updateConfig: (config: Partial<RoomsSocketConfig>) => void;
44
+ updateModel: (room: string, data: any) => Promise<void>;
45
+ disconnect: () => void;
46
+ };
47
+ export {};
@@ -0,0 +1,115 @@
1
+ import { io } from "socket.io-client";
2
+ import { getAccessToken } from "./auth-utils.js";
3
+ function initializeSocket(config, handlers) {
4
+ var _a;
5
+ const socket = io(config.serverUrl, {
6
+ path: config.mountPath,
7
+ transports: config.transports,
8
+ query: {
9
+ app_id: config.appId,
10
+ token: (_a = config.token) !== null && _a !== void 0 ? _a : getAccessToken(),
11
+ },
12
+ });
13
+ socket.on("connect", async () => {
14
+ var _a;
15
+ console.log("connect", socket.id);
16
+ return (_a = handlers.connect) === null || _a === void 0 ? void 0 : _a.call(handlers);
17
+ });
18
+ socket.on("update_model", async (msg) => {
19
+ var _a;
20
+ return (_a = handlers.update_model) === null || _a === void 0 ? void 0 : _a.call(handlers, msg);
21
+ });
22
+ socket.on("error", async (error) => {
23
+ var _a;
24
+ return (_a = handlers.error) === null || _a === void 0 ? void 0 : _a.call(handlers, error);
25
+ });
26
+ socket.on("connect_error", async (error) => {
27
+ var _a;
28
+ console.error("connect_error", error);
29
+ return (_a = handlers.error) === null || _a === void 0 ? void 0 : _a.call(handlers, error);
30
+ });
31
+ return socket;
32
+ }
33
+ export function RoomsSocket({ config }) {
34
+ let currentConfig = { ...config };
35
+ const roomsToListeners = {};
36
+ const handlers = {
37
+ connect: async () => {
38
+ const promises = [];
39
+ Object.keys(roomsToListeners).forEach((room) => {
40
+ joinRoom(room);
41
+ const listeners = getListeners(room);
42
+ listeners === null || listeners === void 0 ? void 0 : listeners.forEach(({ connect }) => {
43
+ const promise = async () => connect === null || connect === void 0 ? void 0 : connect();
44
+ promises.push(promise());
45
+ });
46
+ });
47
+ await Promise.all(promises);
48
+ },
49
+ update_model: async (msg) => {
50
+ const listeners = getListeners(msg.room);
51
+ const promises = listeners.map((listener) => { var _a; return (_a = listener.update_model) === null || _a === void 0 ? void 0 : _a.call(listener, msg); });
52
+ await Promise.all(promises);
53
+ },
54
+ error: async (error) => {
55
+ console.error("error", error);
56
+ const promises = Object.values(roomsToListeners)
57
+ .flat()
58
+ .map((listener) => { var _a; return (_a = listener.error) === null || _a === void 0 ? void 0 : _a.call(listener, error); });
59
+ await Promise.all(promises);
60
+ },
61
+ };
62
+ let socket = initializeSocket(config, handlers);
63
+ function cleanup() {
64
+ disconnect();
65
+ }
66
+ function disconnect() {
67
+ if (socket) {
68
+ socket.disconnect();
69
+ }
70
+ }
71
+ function updateConfig(config) {
72
+ cleanup();
73
+ currentConfig = {
74
+ ...currentConfig,
75
+ ...config,
76
+ };
77
+ socket = initializeSocket(currentConfig, handlers);
78
+ }
79
+ function joinRoom(room) {
80
+ socket.emit("join", room);
81
+ }
82
+ function leaveRoom(room) {
83
+ socket.emit("leave", room);
84
+ }
85
+ async function updateModel(room, data) {
86
+ var _a;
87
+ const dataStr = JSON.stringify(data);
88
+ return (_a = handlers.update_model) === null || _a === void 0 ? void 0 : _a.call(handlers, { room, data: dataStr });
89
+ }
90
+ function getListeners(room) {
91
+ return roomsToListeners[room];
92
+ }
93
+ const subscribeToRoom = (room, handlers) => {
94
+ if (!roomsToListeners[room]) {
95
+ joinRoom(room);
96
+ roomsToListeners[room] = [];
97
+ }
98
+ roomsToListeners[room].push(handlers);
99
+ return () => {
100
+ var _a, _b;
101
+ roomsToListeners[room] =
102
+ (_b = (_a = roomsToListeners[room]) === null || _a === void 0 ? void 0 : _a.filter((listener) => listener !== handlers)) !== null && _b !== void 0 ? _b : [];
103
+ if (roomsToListeners[room].length === 0) {
104
+ leaveRoom(room);
105
+ }
106
+ };
107
+ };
108
+ return {
109
+ socket,
110
+ subscribeToRoom,
111
+ updateConfig,
112
+ updateModel,
113
+ disconnect,
114
+ };
115
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44/sdk",
3
- "version": "0.7.0",
3
+ "version": "0.7.3",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
@@ -19,7 +19,8 @@
19
19
  "prepublishOnly": "npm run build"
20
20
  },
21
21
  "dependencies": {
22
- "axios": "^1.6.2"
22
+ "axios": "^1.6.2",
23
+ "socket.io-client": "^4.7.5"
23
24
  },
24
25
  "devDependencies": {
25
26
  "vitest": "^1.0.0",