@base44-preview/sdk 0.7.0-pr.26.fe973bb → 0.7.0-pr.27.15b5e4d
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/README.md +100 -549
- package/dist/client.d.ts +45 -55
- package/dist/client.js +34 -4
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -0
- package/dist/modules/agents.d.ts +18 -83
- package/dist/modules/agents.js +39 -247
- package/dist/modules/auth.js +1 -0
- package/dist/modules/types.d.ts +44 -0
- package/dist/modules/types.js +1 -0
- package/dist/types.d.ts +9 -0
- package/dist/types.js +1 -0
- package/dist/utils/axios-client.d.ts +2 -1
- package/dist/utils/axios-client.js +2 -1
- package/dist/utils/socket-utils.d.ts +47 -0
- package/dist/utils/socket-utils.js +108 -0
- package/package.json +3 -2
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,7 @@
|
|
|
1
|
-
|
|
1
|
+
export type CreateClientOptions = {
|
|
2
|
+
onError?: (error: Error) => void;
|
|
3
|
+
};
|
|
4
|
+
export type Base44Client = ReturnType<typeof createClient>;
|
|
2
5
|
/**
|
|
3
6
|
* Create a Base44 client instance
|
|
4
7
|
* @param {Object} config - Client configuration
|
|
@@ -7,7 +10,6 @@ import { AgentsModuleConfig } from "./modules/agents.js";
|
|
|
7
10
|
* @param {string} [config.token] - Authentication token
|
|
8
11
|
* @param {string} [config.serviceToken] - Service role authentication token
|
|
9
12
|
* @param {boolean} [config.requiresAuth=false] - Whether the app requires authentication
|
|
10
|
-
* @param {AgentsModuleConfig} [config.agents] - Configuration for agents module
|
|
11
13
|
* @returns {Object} Base44 client instance
|
|
12
14
|
*/
|
|
13
15
|
export declare function createClient(config: {
|
|
@@ -16,7 +18,7 @@ export declare function createClient(config: {
|
|
|
16
18
|
token?: string;
|
|
17
19
|
serviceToken?: string;
|
|
18
20
|
requiresAuth?: boolean;
|
|
19
|
-
|
|
21
|
+
options?: CreateClientOptions;
|
|
20
22
|
}): {
|
|
21
23
|
/**
|
|
22
24
|
* Set authentication token for all requests
|
|
@@ -42,20 +44,17 @@ export declare function createClient(config: {
|
|
|
42
44
|
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
43
45
|
};
|
|
44
46
|
agents: {
|
|
45
|
-
|
|
46
|
-
getConversation(conversationId: string)
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
connected: boolean;
|
|
55
|
-
};
|
|
56
|
-
connectWebSocket(): Promise<void>;
|
|
57
|
-
disconnectWebSocket(): 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;
|
|
58
56
|
};
|
|
57
|
+
cleanup: () => void;
|
|
59
58
|
};
|
|
60
59
|
entities: {};
|
|
61
60
|
integrations: {};
|
|
@@ -75,20 +74,17 @@ export declare function createClient(config: {
|
|
|
75
74
|
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
76
75
|
};
|
|
77
76
|
agents: {
|
|
78
|
-
|
|
79
|
-
getConversation(conversationId: string)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
connected: boolean;
|
|
88
|
-
};
|
|
89
|
-
connectWebSocket(): Promise<void>;
|
|
90
|
-
disconnectWebSocket(): 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;
|
|
91
86
|
};
|
|
87
|
+
cleanup: () => void;
|
|
92
88
|
};
|
|
93
89
|
export declare function createClientFromRequest(request: Request): {
|
|
94
90
|
/**
|
|
@@ -115,20 +111,17 @@ export declare function createClientFromRequest(request: Request): {
|
|
|
115
111
|
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
116
112
|
};
|
|
117
113
|
agents: {
|
|
118
|
-
|
|
119
|
-
getConversation(conversationId: string)
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
connected: boolean;
|
|
128
|
-
};
|
|
129
|
-
connectWebSocket(): Promise<void>;
|
|
130
|
-
disconnectWebSocket(): 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;
|
|
131
123
|
};
|
|
124
|
+
cleanup: () => void;
|
|
132
125
|
};
|
|
133
126
|
entities: {};
|
|
134
127
|
integrations: {};
|
|
@@ -148,18 +141,15 @@ export declare function createClientFromRequest(request: Request): {
|
|
|
148
141
|
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
149
142
|
};
|
|
150
143
|
agents: {
|
|
151
|
-
|
|
152
|
-
getConversation(conversationId: string)
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
connected: boolean;
|
|
161
|
-
};
|
|
162
|
-
connectWebSocket(): Promise<void>;
|
|
163
|
-
disconnectWebSocket(): 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;
|
|
164
153
|
};
|
|
154
|
+
cleanup: () => void;
|
|
165
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
|
|
@@ -14,11 +15,20 @@ import { createAgentsModule } from "./modules/agents.js";
|
|
|
14
15
|
* @param {string} [config.token] - Authentication token
|
|
15
16
|
* @param {string} [config.serviceToken] - Service role authentication token
|
|
16
17
|
* @param {boolean} [config.requiresAuth=false] - Whether the app requires authentication
|
|
17
|
-
* @param {AgentsModuleConfig} [config.agents] - Configuration for agents module
|
|
18
18
|
* @returns {Object} Base44 client instance
|
|
19
19
|
*/
|
|
20
20
|
export function createClient(config) {
|
|
21
|
-
const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false,
|
|
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
|
+
});
|
|
22
32
|
const axiosClient = createAxiosClient({
|
|
23
33
|
baseURL: `${serverUrl}/api`,
|
|
24
34
|
headers: {
|
|
@@ -28,6 +38,7 @@ export function createClient(config) {
|
|
|
28
38
|
requiresAuth,
|
|
29
39
|
appId,
|
|
30
40
|
serverUrl,
|
|
41
|
+
onError: options === null || options === void 0 ? void 0 : options.onError,
|
|
31
42
|
});
|
|
32
43
|
const functionsAxiosClient = createAxiosClient({
|
|
33
44
|
baseURL: `${serverUrl}/api`,
|
|
@@ -39,6 +50,7 @@ export function createClient(config) {
|
|
|
39
50
|
appId,
|
|
40
51
|
serverUrl,
|
|
41
52
|
interceptResponses: false,
|
|
53
|
+
onError: options === null || options === void 0 ? void 0 : options.onError,
|
|
42
54
|
});
|
|
43
55
|
const serviceRoleAxiosClient = createAxiosClient({
|
|
44
56
|
baseURL: `${serverUrl}/api`,
|
|
@@ -48,6 +60,7 @@ export function createClient(config) {
|
|
|
48
60
|
token: serviceToken,
|
|
49
61
|
serverUrl,
|
|
50
62
|
appId,
|
|
63
|
+
onError: options === null || options === void 0 ? void 0 : options.onError,
|
|
51
64
|
});
|
|
52
65
|
const serviceRoleFunctionsAxiosClient = createAxiosClient({
|
|
53
66
|
baseURL: `${serverUrl}/api`,
|
|
@@ -64,14 +77,28 @@ export function createClient(config) {
|
|
|
64
77
|
integrations: createIntegrationsModule(axiosClient, appId),
|
|
65
78
|
auth: createAuthModule(axiosClient, functionsAxiosClient, appId),
|
|
66
79
|
functions: createFunctionsModule(functionsAxiosClient, appId),
|
|
67
|
-
agents: createAgentsModule(
|
|
80
|
+
agents: createAgentsModule({
|
|
81
|
+
axios: axiosClient,
|
|
82
|
+
socket,
|
|
83
|
+
appId,
|
|
84
|
+
}),
|
|
85
|
+
cleanup: () => {
|
|
86
|
+
socket.disconnect();
|
|
87
|
+
},
|
|
68
88
|
};
|
|
69
89
|
const serviceRoleModules = {
|
|
70
90
|
entities: createEntitiesModule(serviceRoleAxiosClient, appId),
|
|
71
91
|
integrations: createIntegrationsModule(serviceRoleAxiosClient, appId),
|
|
72
92
|
sso: createSsoModule(serviceRoleAxiosClient, appId, token),
|
|
73
93
|
functions: createFunctionsModule(serviceRoleFunctionsAxiosClient, appId),
|
|
74
|
-
agents: createAgentsModule(
|
|
94
|
+
agents: createAgentsModule({
|
|
95
|
+
axios: serviceRoleAxiosClient,
|
|
96
|
+
socket,
|
|
97
|
+
appId,
|
|
98
|
+
}),
|
|
99
|
+
cleanup: () => {
|
|
100
|
+
socket.disconnect();
|
|
101
|
+
},
|
|
75
102
|
};
|
|
76
103
|
// Always try to get token from localStorage or URL parameters
|
|
77
104
|
if (typeof window !== "undefined") {
|
|
@@ -106,6 +133,9 @@ export function createClient(config) {
|
|
|
106
133
|
*/
|
|
107
134
|
setToken(newToken) {
|
|
108
135
|
userModules.auth.setToken(newToken);
|
|
136
|
+
socket.updateConfig({
|
|
137
|
+
token: newToken,
|
|
138
|
+
});
|
|
109
139
|
},
|
|
110
140
|
/**
|
|
111
141
|
* Get current configuration
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +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
|
-
export type { Message, AgentConversation, CreateConversationPayload, UpdateConversationPayload, FilterParams, AgentsModuleConfig, AgentsModule, } from "./modules/agents.js";
|
|
5
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";
|
package/dist/modules/agents.d.ts
CHANGED
|
@@ -1,85 +1,20 @@
|
|
|
1
|
+
import { RoomsSocket } from "../utils/socket-utils.js";
|
|
2
|
+
import { AgentConversation, AgentMessage } from "./types.js";
|
|
1
3
|
import { AxiosInstance } from "axios";
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
}
|
|
19
|
-
export interface CreateConversationPayload {
|
|
20
|
-
agent_name: string;
|
|
21
|
-
metadata?: Record<string, any>;
|
|
22
|
-
}
|
|
23
|
-
export interface UpdateConversationPayload {
|
|
24
|
-
metadata?: Record<string, any>;
|
|
25
|
-
}
|
|
26
|
-
export interface FilterParams {
|
|
27
|
-
query?: Record<string, any>;
|
|
28
|
-
sort?: Record<string, 1 | -1>;
|
|
29
|
-
limit?: number;
|
|
30
|
-
skip?: number;
|
|
31
|
-
}
|
|
32
|
-
export interface AgentsModuleConfig {
|
|
33
|
-
enableWebSocket?: boolean;
|
|
34
|
-
socketUrl?: string;
|
|
35
|
-
}
|
|
36
|
-
/**
|
|
37
|
-
* Create agents module for managing AI agent conversations
|
|
38
|
-
*/
|
|
39
|
-
export declare function createAgentsModule(axiosClient: AxiosInstance, appId: string, config?: AgentsModuleConfig): {
|
|
40
|
-
/**
|
|
41
|
-
* List all conversations for the current user
|
|
42
|
-
*/
|
|
43
|
-
listConversations(filterParams?: FilterParams): Promise<AgentConversation[]>;
|
|
44
|
-
/**
|
|
45
|
-
* Get a specific conversation by ID
|
|
46
|
-
*/
|
|
47
|
-
getConversation(conversationId: string): Promise<AgentConversation>;
|
|
48
|
-
/**
|
|
49
|
-
* Create a new agent conversation
|
|
50
|
-
*/
|
|
51
|
-
createConversation(payload: CreateConversationPayload): Promise<AgentConversation>;
|
|
52
|
-
/**
|
|
53
|
-
* Update conversation metadata
|
|
54
|
-
*/
|
|
55
|
-
updateConversation(conversationId: string, payload: UpdateConversationPayload): Promise<AgentConversation>;
|
|
56
|
-
/**
|
|
57
|
-
* Send a message to an agent and get response
|
|
58
|
-
*/
|
|
59
|
-
sendMessage(conversationId: string, message: Omit<Message, "id">): Promise<Message>;
|
|
60
|
-
/**
|
|
61
|
-
* Delete a message from a conversation
|
|
62
|
-
*/
|
|
63
|
-
deleteMessage(conversationId: string, messageId: string): Promise<void>;
|
|
64
|
-
/**
|
|
65
|
-
* Subscribe to real-time updates for a conversation
|
|
66
|
-
* Requires WebSocket to be enabled in config
|
|
67
|
-
*/
|
|
68
|
-
subscribeToConversation(conversationId: string, onUpdate: (conversation: AgentConversation) => void): () => void;
|
|
69
|
-
/**
|
|
70
|
-
* Get WebSocket connection status
|
|
71
|
-
*/
|
|
72
|
-
getWebSocketStatus(): {
|
|
73
|
-
enabled: boolean;
|
|
74
|
-
connected: boolean;
|
|
75
|
-
};
|
|
76
|
-
/**
|
|
77
|
-
* Manually connect WebSocket
|
|
78
|
-
*/
|
|
79
|
-
connectWebSocket(): Promise<void>;
|
|
80
|
-
/**
|
|
81
|
-
* Disconnect WebSocket
|
|
82
|
-
*/
|
|
83
|
-
disconnectWebSocket(): void;
|
|
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;
|
|
84
20
|
};
|
|
85
|
-
export type AgentsModule = ReturnType<typeof createAgentsModule>;
|
package/dist/modules/agents.js
CHANGED
|
@@ -1,251 +1,43 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
this.appId = appId;
|
|
13
|
-
this.token = token;
|
|
14
|
-
}
|
|
15
|
-
connect() {
|
|
16
|
-
return new Promise((resolve, reject) => {
|
|
17
|
-
var _a;
|
|
18
|
-
// Check if WebSocket is available (browser environment)
|
|
19
|
-
if (typeof WebSocket === "undefined") {
|
|
20
|
-
reject(new Error("WebSocket is not available in this environment"));
|
|
21
|
-
return;
|
|
22
|
-
}
|
|
23
|
-
if (((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN) {
|
|
24
|
-
resolve();
|
|
25
|
-
return;
|
|
26
|
-
}
|
|
27
|
-
try {
|
|
28
|
-
const wsUrl = new URL(this.socketUrl);
|
|
29
|
-
wsUrl.searchParams.set("appId", this.appId);
|
|
30
|
-
if (this.token) {
|
|
31
|
-
wsUrl.searchParams.set("token", this.token);
|
|
32
|
-
}
|
|
33
|
-
this.socket = new WebSocket(wsUrl.toString());
|
|
34
|
-
this.socket.onopen = () => {
|
|
35
|
-
this.reconnectAttempts = 0;
|
|
36
|
-
resolve();
|
|
37
|
-
};
|
|
38
|
-
this.socket.onmessage = (event) => {
|
|
39
|
-
try {
|
|
40
|
-
const data = JSON.parse(event.data);
|
|
41
|
-
this.handleMessage(data);
|
|
42
|
-
}
|
|
43
|
-
catch (error) {
|
|
44
|
-
console.error("Failed to parse WebSocket message:", error);
|
|
45
|
-
}
|
|
46
|
-
};
|
|
47
|
-
this.socket.onclose = () => {
|
|
48
|
-
this.attemptReconnect();
|
|
49
|
-
};
|
|
50
|
-
this.socket.onerror = (error) => {
|
|
51
|
-
console.error("WebSocket error:", error);
|
|
52
|
-
reject(error);
|
|
53
|
-
};
|
|
54
|
-
}
|
|
55
|
-
catch (error) {
|
|
56
|
-
reject(error);
|
|
57
|
-
}
|
|
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,
|
|
58
12
|
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
this.connect().catch((error) => {
|
|
82
|
-
console.error("Reconnection failed:", error);
|
|
83
|
-
});
|
|
84
|
-
}, delay);
|
|
85
|
-
}
|
|
86
|
-
subscribe(room, callback) {
|
|
87
|
-
if (!this.listeners.has(room)) {
|
|
88
|
-
this.listeners.set(room, new Set());
|
|
89
|
-
}
|
|
90
|
-
this.listeners.get(room).add(callback);
|
|
91
|
-
// Send subscription message if connected
|
|
92
|
-
if (typeof WebSocket !== "undefined" &&
|
|
93
|
-
this.socket &&
|
|
94
|
-
this.socket.readyState === WebSocket.OPEN &&
|
|
95
|
-
this.socket.send) {
|
|
96
|
-
this.socket.send(JSON.stringify({
|
|
97
|
-
type: "subscribe",
|
|
98
|
-
room: room,
|
|
99
|
-
}));
|
|
100
|
-
}
|
|
101
|
-
// Return unsubscribe function
|
|
102
|
-
return () => {
|
|
103
|
-
const roomListeners = this.listeners.get(room);
|
|
104
|
-
if (roomListeners) {
|
|
105
|
-
roomListeners.delete(callback);
|
|
106
|
-
if (roomListeners.size === 0) {
|
|
107
|
-
this.listeners.delete(room);
|
|
108
|
-
// Send unsubscribe message if connected
|
|
109
|
-
if (typeof WebSocket !== "undefined" &&
|
|
110
|
-
this.socket &&
|
|
111
|
-
this.socket.readyState === WebSocket.OPEN &&
|
|
112
|
-
this.socket.send) {
|
|
113
|
-
this.socket.send(JSON.stringify({
|
|
114
|
-
type: "unsubscribe",
|
|
115
|
-
room: room,
|
|
116
|
-
}));
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
};
|
|
121
|
-
}
|
|
122
|
-
disconnect() {
|
|
123
|
-
if (this.socket) {
|
|
124
|
-
this.socket.close();
|
|
125
|
-
this.socket = null;
|
|
126
|
-
}
|
|
127
|
-
this.listeners.clear();
|
|
128
|
-
}
|
|
129
|
-
isConnected() {
|
|
130
|
-
var _a;
|
|
131
|
-
if (typeof WebSocket === "undefined") {
|
|
132
|
-
return false;
|
|
133
|
-
}
|
|
134
|
-
return ((_a = this.socket) === null || _a === void 0 ? void 0 : _a.readyState) === WebSocket.OPEN;
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
/**
|
|
138
|
-
* Create agents module for managing AI agent conversations
|
|
139
|
-
*/
|
|
140
|
-
export function createAgentsModule(axiosClient, appId, config = {}) {
|
|
141
|
-
var _a, _b;
|
|
142
|
-
let webSocketManager = null;
|
|
143
|
-
let currentConversation = null;
|
|
144
|
-
// Initialize WebSocket if enabled
|
|
145
|
-
if (config.enableWebSocket) {
|
|
146
|
-
const socketUrl = config.socketUrl || "wss://base44.app/ws";
|
|
147
|
-
// Extract token from axios client if available
|
|
148
|
-
const token = (_b = (_a = axiosClient.defaults.headers.common) === null || _a === void 0 ? void 0 : _a.Authorization) === null || _b === void 0 ? void 0 : _b.toString().replace("Bearer ", "");
|
|
149
|
-
webSocketManager = new AgentWebSocketManager(socketUrl, appId, token);
|
|
150
|
-
}
|
|
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
|
+
};
|
|
151
35
|
return {
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
});
|
|
159
|
-
return response;
|
|
160
|
-
},
|
|
161
|
-
/**
|
|
162
|
-
* Get a specific conversation by ID
|
|
163
|
-
*/
|
|
164
|
-
async getConversation(conversationId) {
|
|
165
|
-
const response = await axiosClient.get(`/apps/${appId}/agents/conversations/${conversationId}`);
|
|
166
|
-
return response;
|
|
167
|
-
},
|
|
168
|
-
/**
|
|
169
|
-
* Create a new agent conversation
|
|
170
|
-
*/
|
|
171
|
-
async createConversation(payload) {
|
|
172
|
-
const response = await axiosClient.post(`/apps/${appId}/agents/conversations`, payload);
|
|
173
|
-
return response;
|
|
174
|
-
},
|
|
175
|
-
/**
|
|
176
|
-
* Update conversation metadata
|
|
177
|
-
*/
|
|
178
|
-
async updateConversation(conversationId, payload) {
|
|
179
|
-
const response = await axiosClient.put(`/apps/${appId}/agents/conversations/${conversationId}`, payload);
|
|
180
|
-
return response;
|
|
181
|
-
},
|
|
182
|
-
/**
|
|
183
|
-
* Send a message to an agent and get response
|
|
184
|
-
*/
|
|
185
|
-
async sendMessage(conversationId, message) {
|
|
186
|
-
// Update current conversation for WebSocket tracking
|
|
187
|
-
if ((currentConversation === null || currentConversation === void 0 ? void 0 : currentConversation.id) === conversationId) {
|
|
188
|
-
currentConversation.messages = [
|
|
189
|
-
...currentConversation.messages,
|
|
190
|
-
{ ...message, id: "temp-" + Date.now() },
|
|
191
|
-
];
|
|
192
|
-
}
|
|
193
|
-
const response = await axiosClient.post(`/apps/${appId}/agents/conversations/${conversationId}/messages`, message);
|
|
194
|
-
return response;
|
|
195
|
-
},
|
|
196
|
-
/**
|
|
197
|
-
* Delete a message from a conversation
|
|
198
|
-
*/
|
|
199
|
-
async deleteMessage(conversationId, messageId) {
|
|
200
|
-
await axiosClient.delete(`/apps/${appId}/agents/conversations/${conversationId}/messages/${messageId}`);
|
|
201
|
-
},
|
|
202
|
-
/**
|
|
203
|
-
* Subscribe to real-time updates for a conversation
|
|
204
|
-
* Requires WebSocket to be enabled in config
|
|
205
|
-
*/
|
|
206
|
-
subscribeToConversation(conversationId, onUpdate) {
|
|
207
|
-
if (!webSocketManager) {
|
|
208
|
-
throw new Error("WebSocket is not enabled. Set enableWebSocket: true in agents config");
|
|
209
|
-
}
|
|
210
|
-
// Connect WebSocket if not already connected
|
|
211
|
-
if (!webSocketManager.isConnected()) {
|
|
212
|
-
webSocketManager.connect().catch((error) => {
|
|
213
|
-
console.error("Failed to connect WebSocket:", error);
|
|
214
|
-
});
|
|
215
|
-
}
|
|
216
|
-
return webSocketManager.subscribe(`/agent-conversations/${conversationId}`, (data) => {
|
|
217
|
-
// Update current conversation reference
|
|
218
|
-
if (data.id === conversationId) {
|
|
219
|
-
currentConversation = data;
|
|
220
|
-
}
|
|
221
|
-
onUpdate(data);
|
|
222
|
-
});
|
|
223
|
-
},
|
|
224
|
-
/**
|
|
225
|
-
* Get WebSocket connection status
|
|
226
|
-
*/
|
|
227
|
-
getWebSocketStatus() {
|
|
228
|
-
return {
|
|
229
|
-
enabled: !!webSocketManager,
|
|
230
|
-
connected: (webSocketManager === null || webSocketManager === void 0 ? void 0 : webSocketManager.isConnected()) || false,
|
|
231
|
-
};
|
|
232
|
-
},
|
|
233
|
-
/**
|
|
234
|
-
* Manually connect WebSocket
|
|
235
|
-
*/
|
|
236
|
-
async connectWebSocket() {
|
|
237
|
-
if (!webSocketManager) {
|
|
238
|
-
throw new Error("WebSocket is not enabled. Set enableWebSocket: true in agents config");
|
|
239
|
-
}
|
|
240
|
-
await webSocketManager.connect();
|
|
241
|
-
},
|
|
242
|
-
/**
|
|
243
|
-
* Disconnect WebSocket
|
|
244
|
-
*/
|
|
245
|
-
disconnectWebSocket() {
|
|
246
|
-
if (webSocketManager) {
|
|
247
|
-
webSocketManager.disconnect();
|
|
248
|
-
}
|
|
249
|
-
},
|
|
36
|
+
getConversations,
|
|
37
|
+
getConversation,
|
|
38
|
+
listConversations,
|
|
39
|
+
createConversation,
|
|
40
|
+
addMessage,
|
|
41
|
+
subscribeToConversation,
|
|
250
42
|
};
|
|
251
43
|
}
|