@base44-preview/sdk 0.7.0-pr.27.96b80f1 → 0.7.0-pr.27.b5c0834

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 Base44SDK = ReturnType<typeof createClient>;
1
5
  /**
2
6
  * Create a Base44 client instance
3
7
  * @param {Object} config - Client configuration
@@ -14,6 +18,7 @@ export declare function createClient(config: {
14
18
  token?: string;
15
19
  serviceToken?: string;
16
20
  requiresAuth?: boolean;
21
+ options?: CreateClientOptions;
17
22
  }): {
18
23
  /**
19
24
  * Set authentication token for all requests
@@ -39,14 +44,17 @@ export declare function createClient(config: {
39
44
  invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
40
45
  };
41
46
  agents: {
42
- getConversations: () => Promise<import("axios").AxiosResponse<any, any>>;
43
- getConversation: (conversationId: string) => Promise<import("axios").AxiosResponse<any, any>>;
44
- listConversations: (filterParams: any) => Promise<import("axios").AxiosResponse<any, any>>;
45
- createConversation: (conversation: any) => Promise<import("axios").AxiosResponse<any, any>>;
46
- addMessage: (conversation: any, message: any) => Promise<import("axios").AxiosResponse<any, any>>;
47
- subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
48
- updateConfig: (config: Partial<import("./modules/agents.js").AgentsModuleConfig>) => void;
47
+ getConversations: () => Promise<import("./types.js").AgentConversation[]>;
48
+ getConversation: (conversationId: string) => Promise<import("./types.js").AgentConversation | undefined>;
49
+ listConversations: (filterParams: import("./types.js").ModelFilterParams) => Promise<import("./types.js").AgentConversation[]>;
50
+ createConversation: (conversation: {
51
+ agent_name: string;
52
+ metadata?: Record<string, any>;
53
+ }) => Promise<import("./types.js").AgentConversation>;
54
+ addMessage: (conversation: import("./types.js").AgentConversation, message: import("./types.js").AgentMessage) => Promise<import("./types.js").AgentMessage>;
55
+ subscribeToConversation: (conversationId: string, onUpdate?: (conversation: import("./types.js").AgentConversation) => void) => () => void;
49
56
  };
57
+ cleanup: () => void;
50
58
  };
51
59
  entities: {};
52
60
  integrations: {};
@@ -66,14 +74,17 @@ export declare function createClient(config: {
66
74
  invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
67
75
  };
68
76
  agents: {
69
- getConversations: () => Promise<import("axios").AxiosResponse<any, any>>;
70
- getConversation: (conversationId: string) => Promise<import("axios").AxiosResponse<any, any>>;
71
- listConversations: (filterParams: any) => Promise<import("axios").AxiosResponse<any, any>>;
72
- createConversation: (conversation: any) => Promise<import("axios").AxiosResponse<any, any>>;
73
- addMessage: (conversation: any, message: any) => Promise<import("axios").AxiosResponse<any, any>>;
74
- subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
75
- updateConfig: (config: Partial<import("./modules/agents.js").AgentsModuleConfig>) => void;
77
+ getConversations: () => Promise<import("./types.js").AgentConversation[]>;
78
+ getConversation: (conversationId: string) => Promise<import("./types.js").AgentConversation | undefined>;
79
+ listConversations: (filterParams: import("./types.js").ModelFilterParams) => Promise<import("./types.js").AgentConversation[]>;
80
+ createConversation: (conversation: {
81
+ agent_name: string;
82
+ metadata?: Record<string, any>;
83
+ }) => Promise<import("./types.js").AgentConversation>;
84
+ addMessage: (conversation: import("./types.js").AgentConversation, message: import("./types.js").AgentMessage) => Promise<import("./types.js").AgentMessage>;
85
+ subscribeToConversation: (conversationId: string, onUpdate?: (conversation: import("./types.js").AgentConversation) => void) => () => void;
76
86
  };
87
+ cleanup: () => void;
77
88
  };
78
89
  export declare function createClientFromRequest(request: Request): {
79
90
  /**
@@ -100,14 +111,17 @@ export declare function createClientFromRequest(request: Request): {
100
111
  invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
101
112
  };
102
113
  agents: {
103
- getConversations: () => Promise<import("axios").AxiosResponse<any, any>>;
104
- getConversation: (conversationId: string) => Promise<import("axios").AxiosResponse<any, any>>;
105
- listConversations: (filterParams: any) => Promise<import("axios").AxiosResponse<any, any>>;
106
- createConversation: (conversation: any) => Promise<import("axios").AxiosResponse<any, any>>;
107
- addMessage: (conversation: any, message: any) => Promise<import("axios").AxiosResponse<any, any>>;
108
- subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
109
- updateConfig: (config: Partial<import("./modules/agents.js").AgentsModuleConfig>) => void;
114
+ getConversations: () => Promise<import("./types.js").AgentConversation[]>;
115
+ getConversation: (conversationId: string) => Promise<import("./types.js").AgentConversation | undefined>;
116
+ listConversations: (filterParams: import("./types.js").ModelFilterParams) => Promise<import("./types.js").AgentConversation[]>;
117
+ createConversation: (conversation: {
118
+ agent_name: string;
119
+ metadata?: Record<string, any>;
120
+ }) => Promise<import("./types.js").AgentConversation>;
121
+ addMessage: (conversation: import("./types.js").AgentConversation, message: import("./types.js").AgentMessage) => Promise<import("./types.js").AgentMessage>;
122
+ subscribeToConversation: (conversationId: string, onUpdate?: (conversation: import("./types.js").AgentConversation) => void) => () => void;
110
123
  };
124
+ cleanup: () => void;
111
125
  };
112
126
  entities: {};
113
127
  integrations: {};
@@ -127,12 +141,15 @@ export declare function createClientFromRequest(request: Request): {
127
141
  invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
128
142
  };
129
143
  agents: {
130
- getConversations: () => Promise<import("axios").AxiosResponse<any, any>>;
131
- getConversation: (conversationId: string) => Promise<import("axios").AxiosResponse<any, any>>;
132
- listConversations: (filterParams: any) => Promise<import("axios").AxiosResponse<any, any>>;
133
- createConversation: (conversation: any) => Promise<import("axios").AxiosResponse<any, any>>;
134
- addMessage: (conversation: any, message: any) => Promise<import("axios").AxiosResponse<any, any>>;
135
- subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
136
- updateConfig: (config: Partial<import("./modules/agents.js").AgentsModuleConfig>) => void;
144
+ getConversations: () => Promise<import("./types.js").AgentConversation[]>;
145
+ getConversation: (conversationId: string) => Promise<import("./types.js").AgentConversation | undefined>;
146
+ listConversations: (filterParams: import("./types.js").ModelFilterParams) => Promise<import("./types.js").AgentConversation[]>;
147
+ createConversation: (conversation: {
148
+ agent_name: string;
149
+ metadata?: Record<string, any>;
150
+ }) => Promise<import("./types.js").AgentConversation>;
151
+ addMessage: (conversation: import("./types.js").AgentConversation, message: import("./types.js").AgentMessage) => Promise<import("./types.js").AgentMessage>;
152
+ subscribeToConversation: (conversationId: string, onUpdate?: (conversation: import("./types.js").AgentConversation) => void) => () => void;
137
153
  };
154
+ cleanup: () => void;
138
155
  };
package/dist/client.js CHANGED
@@ -6,6 +6,7 @@ import { createSsoModule } from "./modules/sso.js";
6
6
  import { getAccessToken } from "./utils/auth-utils.js";
7
7
  import { createFunctionsModule } from "./modules/functions.js";
8
8
  import { createAgentsModule } from "./modules/agents.js";
9
+ import { RoomsSocket } from "./utils/socket-utils.js";
9
10
  /**
10
11
  * Create a Base44 client instance
11
12
  * @param {Object} config - Client configuration
@@ -17,7 +18,17 @@ import { createAgentsModule } from "./modules/agents.js";
17
18
  * @returns {Object} Base44 client instance
18
19
  */
19
20
  export function createClient(config) {
20
- const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, } = config;
21
+ const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, options, } = 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
+ });
21
32
  const axiosClient = createAxiosClient({
22
33
  baseURL: `${serverUrl}/api`,
23
34
  headers: {
@@ -27,6 +38,7 @@ export function createClient(config) {
27
38
  requiresAuth,
28
39
  appId,
29
40
  serverUrl,
41
+ onError: options === null || options === void 0 ? void 0 : options.onError,
30
42
  });
31
43
  const functionsAxiosClient = createAxiosClient({
32
44
  baseURL: `${serverUrl}/api`,
@@ -38,6 +50,7 @@ export function createClient(config) {
38
50
  appId,
39
51
  serverUrl,
40
52
  interceptResponses: false,
53
+ onError: options === null || options === void 0 ? void 0 : options.onError,
41
54
  });
42
55
  const serviceRoleAxiosClient = createAxiosClient({
43
56
  baseURL: `${serverUrl}/api`,
@@ -47,6 +60,7 @@ export function createClient(config) {
47
60
  token: serviceToken,
48
61
  serverUrl,
49
62
  appId,
63
+ onError: options === null || options === void 0 ? void 0 : options.onError,
50
64
  });
51
65
  const serviceRoleFunctionsAxiosClient = createAxiosClient({
52
66
  baseURL: `${serverUrl}/api`,
@@ -64,10 +78,13 @@ export function createClient(config) {
64
78
  auth: createAuthModule(axiosClient, functionsAxiosClient, appId),
65
79
  functions: createFunctionsModule(functionsAxiosClient, appId),
66
80
  agents: createAgentsModule({
67
- serverUrl,
81
+ axios: axiosClient,
82
+ socket,
68
83
  appId,
69
- token,
70
84
  }),
85
+ cleanup: () => {
86
+ socket.disconnect();
87
+ },
71
88
  };
72
89
  const serviceRoleModules = {
73
90
  entities: createEntitiesModule(serviceRoleAxiosClient, appId),
@@ -75,10 +92,13 @@ export function createClient(config) {
75
92
  sso: createSsoModule(serviceRoleAxiosClient, appId, token),
76
93
  functions: createFunctionsModule(serviceRoleFunctionsAxiosClient, appId),
77
94
  agents: createAgentsModule({
78
- serverUrl,
95
+ axios: serviceRoleAxiosClient,
96
+ socket,
79
97
  appId,
80
- token: serviceToken,
81
98
  }),
99
+ cleanup: () => {
100
+ socket.disconnect();
101
+ },
82
102
  };
83
103
  // Always try to get token from localStorage or URL parameters
84
104
  if (typeof window !== "undefined") {
@@ -86,9 +106,6 @@ export function createClient(config) {
86
106
  const accessToken = token || getAccessToken();
87
107
  if (accessToken) {
88
108
  userModules.auth.setToken(accessToken);
89
- userModules.agents.updateConfig({
90
- token: accessToken,
91
- });
92
109
  }
93
110
  }
94
111
  // If authentication is required, verify token and redirect to login if needed
@@ -116,6 +133,9 @@ export function createClient(config) {
116
133
  */
117
134
  setToken(newToken) {
118
135
  userModules.auth.setToken(newToken);
136
+ socket.updateConfig({
137
+ token: newToken,
138
+ });
119
139
  },
120
140
  /**
121
141
  * Get current configuration
package/dist/index.d.ts CHANGED
@@ -1,4 +1,6 @@
1
- import { createClient, createClientFromRequest } from "./client.js";
1
+ import { createClient, createClientFromRequest, Base44SDK } 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 { Base44SDK };
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";
@@ -1,14 +1,20 @@
1
+ import { RoomsSocket } from "../utils/socket-utils.js";
2
+ import { AgentConversation, AgentMessage } from "./types.js";
3
+ import { AxiosInstance } from "axios";
4
+ import { ModelFilterParams } from "../types.js";
1
5
  export type AgentsModuleConfig = {
2
- serverUrl: string;
6
+ axios: AxiosInstance;
7
+ socket: ReturnType<typeof RoomsSocket>;
3
8
  appId: string;
4
- token?: string;
5
9
  };
6
- export declare function createAgentsModule({ appId, serverUrl, token, }: AgentsModuleConfig): {
7
- getConversations: () => Promise<import("axios").AxiosResponse<any, any>>;
8
- getConversation: (conversationId: string) => Promise<import("axios").AxiosResponse<any, any>>;
9
- listConversations: (filterParams: any) => Promise<import("axios").AxiosResponse<any, any>>;
10
- createConversation: (conversation: any) => Promise<import("axios").AxiosResponse<any, any>>;
11
- addMessage: (conversation: any, message: any) => Promise<import("axios").AxiosResponse<any, any>>;
12
- subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
13
- updateConfig: (config: Partial<AgentsModuleConfig>) => void;
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;
14
20
  };
@@ -1,71 +1,34 @@
1
- import { RoomsSocket } from "../utils/socket-utils";
2
- import { createAxiosClient } from "../utils/axios-client";
3
- export function createAgentsModule({ appId, serverUrl, token, }) {
4
- let currentConversation = null;
5
- const socketConfig = {
6
- serverUrl,
7
- mountPath: "/ws-user-apps/socket.io/",
8
- transports: ["websocket"],
9
- query: {
10
- appId,
11
- token,
12
- },
13
- };
14
- const axiosConfig = {
15
- serverUrl,
16
- appId,
17
- token,
18
- };
19
- let axios = createAgentsAxiosClient({
20
- serverUrl,
21
- appId,
22
- token,
23
- });
24
- const roomSocket = RoomsSocket({
25
- config: socketConfig,
26
- });
27
- const updateConfig = (config) => {
28
- axios = createAgentsAxiosClient({ ...axiosConfig, ...config });
29
- roomSocket.updateConfig({ ...socketConfig, ...config });
30
- };
1
+ export function createAgentsModule({ axios, socket, appId, }) {
2
+ const baseURL = `/apps/${appId}/agents`;
31
3
  const getConversations = () => {
32
- return axios.get(`/conversations`);
4
+ return axios.get(`${baseURL}/conversations`);
33
5
  };
34
6
  const getConversation = (conversationId) => {
35
- return axios.get(`/conversations/${conversationId}`);
7
+ return axios.get(`${baseURL}/conversations/${conversationId}`);
36
8
  };
37
9
  const listConversations = (filterParams) => {
38
- return axios.get(`/conversations`, { params: filterParams });
10
+ return axios.get(`${baseURL}/conversations`, {
11
+ params: filterParams,
12
+ });
39
13
  };
40
14
  const createConversation = (conversation) => {
41
- return axios.post(`/conversations`, conversation);
15
+ return axios.post(`${baseURL}/conversations`, conversation);
42
16
  };
43
- const addMessage = (conversation, message) => {
44
- // this whole trick with current conversation so that we can call the onUpdateModel with the latest messages
45
- let convLatestMessages = null;
46
- if (currentConversation && currentConversation.id === conversation.id) {
47
- convLatestMessages = currentConversation.messages;
48
- }
49
- else {
50
- currentConversation = conversation;
51
- convLatestMessages = conversation.messages;
52
- }
53
- conversation.messages = [...convLatestMessages, message];
54
- roomSocket.handlers.update_model({
55
- room: `/agent-conversations/${conversation.id}`,
56
- data: JSON.stringify(conversation),
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],
57
22
  });
58
- return axios.post(`/conversations/${conversation.id}/messages`, message);
23
+ return axios.post(`${baseURL}/conversations/${conversation.id}/messages`, message);
59
24
  };
60
25
  const subscribeToConversation = (conversationId, onUpdate) => {
61
- return roomSocket.subscribeToRoom(`/agent-conversations/${conversationId}`, {
26
+ const room = `/agent-conversations/${conversationId}`;
27
+ return socket.subscribeToRoom(room, {
62
28
  connect: () => { },
63
29
  update_model: ({ data: jsonStr }) => {
64
- const data = JSON.parse(jsonStr);
65
- if (currentConversation && currentConversation.id === data.id) {
66
- currentConversation = data;
67
- }
68
- onUpdate(data);
30
+ const conv = JSON.parse(jsonStr);
31
+ onUpdate === null || onUpdate === void 0 ? void 0 : onUpdate(conv);
69
32
  },
70
33
  });
71
34
  };
@@ -76,19 +39,5 @@ export function createAgentsModule({ appId, serverUrl, token, }) {
76
39
  createConversation,
77
40
  addMessage,
78
41
  subscribeToConversation,
79
- updateConfig,
80
42
  };
81
43
  }
82
- function createAgentsAxiosClient({ serverUrl, appId, token, }) {
83
- const axios = createAxiosClient({
84
- baseURL: `${serverUrl}/api/apps/${appId}/agents`,
85
- appId,
86
- serverUrl,
87
- token,
88
- interceptResponses: false,
89
- headers: {
90
- "X-App-Id": String(appId),
91
- },
92
- });
93
- return axios;
94
- }
@@ -77,6 +77,7 @@ export function createAuthModule(axios, functionsAxiosClient, appId) {
77
77
  setToken(token, saveToStorage = true) {
78
78
  if (!token)
79
79
  return;
80
+ // handle token change for axios clients
80
81
  axios.defaults.headers.common["Authorization"] = `Bearer ${token}`;
81
82
  functionsAxiosClient.defaults.headers.common["Authorization"] = `Bearer ${token}`;
82
83
  // Save token to localStorage if requested
@@ -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,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, }: {
27
27
  baseURL: string;
28
28
  headers?: Record<string, string>;
29
29
  token?: string;
@@ -31,4 +31,5 @@ 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;
34
35
  }): 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, }) {
68
68
  const client = axios.create({
69
69
  baseURL,
70
70
  headers: {
@@ -107,6 +107,7 @@ export function createAxiosClient({ baseURL, headers = {}, token, requiresAuth =
107
107
  redirectToLogin(serverUrl, appId);
108
108
  }, 100);
109
109
  }
110
+ onError === null || onError === void 0 ? void 0 : onError(base44Error);
110
111
  return Promise.reject(base44Error);
111
112
  });
112
113
  }
@@ -3,20 +3,19 @@ export type RoomsSocketConfig = {
3
3
  serverUrl: string;
4
4
  mountPath: string;
5
5
  transports: string[];
6
- query: {
7
- appId: string;
8
- token?: string;
9
- };
6
+ appId: string;
7
+ token?: string;
10
8
  };
11
9
  export type TSocketRoom = string;
12
10
  export type TJsonStr = string;
13
11
  type RoomsSocketEventsMap = {
14
12
  listen: {
15
- connect: () => void;
13
+ connect: () => Promise<void> | void;
16
14
  update_model: (msg: {
17
15
  room: string;
18
16
  data: TJsonStr;
19
- }) => void;
17
+ }) => Promise<void> | void;
18
+ error: (error: Error) => Promise<void> | void;
20
19
  };
21
20
  emit: {
22
21
  join: (room: string) => void;
@@ -24,25 +23,25 @@ type RoomsSocketEventsMap = {
24
23
  };
25
24
  };
26
25
  type TEvent = keyof RoomsSocketEventsMap["listen"];
27
- type THandler<E extends TEvent> = (...args: Parameters<RoomsSocketEventsMap["listen"][E]>) => void;
26
+ type THandler<E extends TEvent> = RoomsSocketEventsMap["listen"][E];
27
+ export type RoomsSocket = ReturnType<typeof RoomsSocket>;
28
28
  export declare function RoomsSocket({ config }: {
29
29
  config: RoomsSocketConfig;
30
30
  }): {
31
- socket: Readonly<Socket<{
32
- connect: () => void;
31
+ socket: Socket<{
32
+ connect: () => Promise<void> | void;
33
33
  update_model: (msg: {
34
34
  room: string;
35
35
  data: TJsonStr;
36
- }) => void;
36
+ }) => Promise<void> | void;
37
+ error: (error: Error) => Promise<void> | void;
37
38
  }, {
38
39
  join: (room: string) => void;
39
40
  leave: (room: string) => void;
40
- }>>;
41
- subscribeToRoom: (room: TSocketRoom, handlers: { [k in TEvent]: THandler<k>; }) => () => void;
42
- updateConfig: (config: RoomsSocketConfig) => void;
43
- handlers: {
44
- connect: THandler<"connect">;
45
- update_model: THandler<"update_model">;
46
- };
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;
47
46
  };
48
47
  export {};
@@ -1,49 +1,78 @@
1
1
  import { io } from "socket.io-client";
2
+ import { getAccessToken } from "./auth-utils.js";
2
3
  function initializeSocket(config, handlers) {
4
+ var _a;
3
5
  const socket = io(config.serverUrl, {
4
6
  path: config.mountPath,
5
7
  transports: config.transports,
6
- query: config.query,
8
+ query: {
9
+ app_id: config.appId,
10
+ token: (_a = config.token) !== null && _a !== void 0 ? _a : getAccessToken(),
11
+ },
7
12
  });
8
13
  socket.on("connect", () => {
14
+ var _a;
9
15
  console.log("connect", socket.id);
10
- handlers.connect();
16
+ (_a = handlers.connect) === null || _a === void 0 ? void 0 : _a.call(handlers);
11
17
  });
12
18
  socket.on("update_model", (msg) => {
13
- handlers.update_model(msg);
19
+ var _a;
20
+ (_a = handlers.update_model) === null || _a === void 0 ? void 0 : _a.call(handlers, msg);
21
+ });
22
+ socket.on("error", (error) => {
23
+ var _a;
24
+ (_a = handlers.error) === null || _a === void 0 ? void 0 : _a.call(handlers, error);
25
+ });
26
+ socket.on("connect_error", (error) => {
27
+ var _a;
28
+ console.error("connect_error", error);
29
+ (_a = handlers.error) === null || _a === void 0 ? void 0 : _a.call(handlers, error);
14
30
  });
15
31
  return socket;
16
32
  }
17
33
  export function RoomsSocket({ config }) {
34
+ let currentConfig = { ...config };
18
35
  const roomsToListeners = {};
19
36
  const handlers = {
20
- connect: () => {
37
+ connect: async () => {
38
+ const promises = [];
21
39
  Object.keys(roomsToListeners).forEach((room) => {
22
- var _a;
23
40
  joinRoom(room);
24
- (_a = getListeners(room)) === null || _a === void 0 ? void 0 : _a.forEach(({ connect: connectHandler }) => {
25
- connectHandler();
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());
26
45
  });
27
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);
28
53
  },
29
- update_model: (msg) => {
54
+ error: async (error) => {
30
55
  var _a;
31
- if (roomsToListeners[msg.room]) {
32
- (_a = getListeners(msg.room)) === null || _a === void 0 ? void 0 : _a.forEach(({ update_model }) => {
33
- update_model(msg);
34
- });
35
- }
56
+ console.error("error", error);
57
+ await ((_a = handlers.error) === null || _a === void 0 ? void 0 : _a.call(handlers, error));
36
58
  },
37
59
  };
38
60
  let socket = initializeSocket(config, handlers);
39
61
  function cleanup() {
62
+ disconnect();
63
+ }
64
+ function disconnect() {
40
65
  if (socket) {
41
66
  socket.disconnect();
42
67
  }
43
68
  }
44
69
  function updateConfig(config) {
45
70
  cleanup();
46
- socket = initializeSocket(config, handlers);
71
+ currentConfig = {
72
+ ...currentConfig,
73
+ ...config,
74
+ };
75
+ socket = initializeSocket(currentConfig, handlers);
47
76
  }
48
77
  function joinRoom(room) {
49
78
  socket.emit("join", room);
@@ -51,6 +80,11 @@ export function RoomsSocket({ config }) {
51
80
  function leaveRoom(room) {
52
81
  socket.emit("leave", room);
53
82
  }
83
+ async function updateModel(room, data) {
84
+ var _a;
85
+ const dataStr = JSON.stringify(data);
86
+ return (_a = handlers.update_model) === null || _a === void 0 ? void 0 : _a.call(handlers, { room, data: dataStr });
87
+ }
54
88
  function getListeners(room) {
55
89
  return roomsToListeners[room];
56
90
  }
@@ -65,9 +99,10 @@ export function RoomsSocket({ config }) {
65
99
  };
66
100
  };
67
101
  return {
68
- socket: Object.freeze(socket),
102
+ socket,
69
103
  subscribeToRoom,
70
104
  updateConfig,
71
- handlers,
105
+ updateModel,
106
+ disconnect,
72
107
  };
73
108
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@base44-preview/sdk",
3
- "version": "0.7.0-pr.27.96b80f1",
3
+ "version": "0.7.0-pr.27.b5c0834",
4
4
  "description": "JavaScript SDK for Base44 API",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",