@base44-preview/sdk 0.7.0-pr.26.fe973bb → 0.7.0-pr.27.ff44f1a
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 +28 -55
- package/dist/client.js +14 -4
- package/dist/index.d.ts +0 -1
- package/dist/modules/agents.d.ts +14 -84
- package/dist/modules/agents.js +88 -243
- package/dist/modules/agents.types.d.ts +44 -0
- package/dist/modules/agents.types.js +1 -0
- package/dist/utils/socket-utils.d.ts +48 -0
- package/dist/utils/socket-utils.js +73 -0
- package/package.json +3 -2
package/dist/client.d.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { AgentsModuleConfig } from "./modules/agents.js";
|
|
2
1
|
/**
|
|
3
2
|
* Create a Base44 client instance
|
|
4
3
|
* @param {Object} config - Client configuration
|
|
@@ -7,7 +6,6 @@ import { AgentsModuleConfig } from "./modules/agents.js";
|
|
|
7
6
|
* @param {string} [config.token] - Authentication token
|
|
8
7
|
* @param {string} [config.serviceToken] - Service role authentication token
|
|
9
8
|
* @param {boolean} [config.requiresAuth=false] - Whether the app requires authentication
|
|
10
|
-
* @param {AgentsModuleConfig} [config.agents] - Configuration for agents module
|
|
11
9
|
* @returns {Object} Base44 client instance
|
|
12
10
|
*/
|
|
13
11
|
export declare function createClient(config: {
|
|
@@ -16,7 +14,6 @@ export declare function createClient(config: {
|
|
|
16
14
|
token?: string;
|
|
17
15
|
serviceToken?: string;
|
|
18
16
|
requiresAuth?: boolean;
|
|
19
|
-
agents?: AgentsModuleConfig;
|
|
20
17
|
}): {
|
|
21
18
|
/**
|
|
22
19
|
* Set authentication token for all requests
|
|
@@ -42,19 +39,13 @@ export declare function createClient(config: {
|
|
|
42
39
|
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
43
40
|
};
|
|
44
41
|
agents: {
|
|
45
|
-
|
|
46
|
-
getConversation(conversationId: string)
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
getWebSocketStatus(): {
|
|
53
|
-
enabled: boolean;
|
|
54
|
-
connected: boolean;
|
|
55
|
-
};
|
|
56
|
-
connectWebSocket(): Promise<void>;
|
|
57
|
-
disconnectWebSocket(): void;
|
|
42
|
+
getConversations: () => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
43
|
+
getConversation: (conversationId: string) => Promise<import("./modules/agents.types.js").AgentConversation | undefined>;
|
|
44
|
+
listConversations: (filterParams: any) => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
45
|
+
createConversation: (conversation: any) => Promise<import("./modules/agents.types.js").AgentConversation>;
|
|
46
|
+
addMessage: (conversation: any, message: any) => Promise<import("./modules/agents.types.js").AgentMessage>;
|
|
47
|
+
subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
|
|
48
|
+
updateConfig: (config: Partial<import("./modules/agents.js").AgentsModuleConfig>) => void;
|
|
58
49
|
};
|
|
59
50
|
};
|
|
60
51
|
entities: {};
|
|
@@ -75,19 +66,13 @@ export declare function createClient(config: {
|
|
|
75
66
|
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
76
67
|
};
|
|
77
68
|
agents: {
|
|
78
|
-
|
|
79
|
-
getConversation(conversationId: string)
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
getWebSocketStatus(): {
|
|
86
|
-
enabled: boolean;
|
|
87
|
-
connected: boolean;
|
|
88
|
-
};
|
|
89
|
-
connectWebSocket(): Promise<void>;
|
|
90
|
-
disconnectWebSocket(): void;
|
|
69
|
+
getConversations: () => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
70
|
+
getConversation: (conversationId: string) => Promise<import("./modules/agents.types.js").AgentConversation | undefined>;
|
|
71
|
+
listConversations: (filterParams: any) => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
72
|
+
createConversation: (conversation: any) => Promise<import("./modules/agents.types.js").AgentConversation>;
|
|
73
|
+
addMessage: (conversation: any, message: any) => Promise<import("./modules/agents.types.js").AgentMessage>;
|
|
74
|
+
subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
|
|
75
|
+
updateConfig: (config: Partial<import("./modules/agents.js").AgentsModuleConfig>) => void;
|
|
91
76
|
};
|
|
92
77
|
};
|
|
93
78
|
export declare function createClientFromRequest(request: Request): {
|
|
@@ -115,19 +100,13 @@ export declare function createClientFromRequest(request: Request): {
|
|
|
115
100
|
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
116
101
|
};
|
|
117
102
|
agents: {
|
|
118
|
-
|
|
119
|
-
getConversation(conversationId: string)
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
getWebSocketStatus(): {
|
|
126
|
-
enabled: boolean;
|
|
127
|
-
connected: boolean;
|
|
128
|
-
};
|
|
129
|
-
connectWebSocket(): Promise<void>;
|
|
130
|
-
disconnectWebSocket(): void;
|
|
103
|
+
getConversations: () => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
104
|
+
getConversation: (conversationId: string) => Promise<import("./modules/agents.types.js").AgentConversation | undefined>;
|
|
105
|
+
listConversations: (filterParams: any) => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
106
|
+
createConversation: (conversation: any) => Promise<import("./modules/agents.types.js").AgentConversation>;
|
|
107
|
+
addMessage: (conversation: any, message: any) => Promise<import("./modules/agents.types.js").AgentMessage>;
|
|
108
|
+
subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
|
|
109
|
+
updateConfig: (config: Partial<import("./modules/agents.js").AgentsModuleConfig>) => void;
|
|
131
110
|
};
|
|
132
111
|
};
|
|
133
112
|
entities: {};
|
|
@@ -148,18 +127,12 @@ export declare function createClientFromRequest(request: Request): {
|
|
|
148
127
|
invoke(functionName: string, data: Record<string, any>): Promise<import("axios").AxiosResponse<any, any>>;
|
|
149
128
|
};
|
|
150
129
|
agents: {
|
|
151
|
-
|
|
152
|
-
getConversation(conversationId: string)
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
getWebSocketStatus(): {
|
|
159
|
-
enabled: boolean;
|
|
160
|
-
connected: boolean;
|
|
161
|
-
};
|
|
162
|
-
connectWebSocket(): Promise<void>;
|
|
163
|
-
disconnectWebSocket(): void;
|
|
130
|
+
getConversations: () => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
131
|
+
getConversation: (conversationId: string) => Promise<import("./modules/agents.types.js").AgentConversation | undefined>;
|
|
132
|
+
listConversations: (filterParams: any) => Promise<import("./modules/agents.types.js").AgentConversation[]>;
|
|
133
|
+
createConversation: (conversation: any) => Promise<import("./modules/agents.types.js").AgentConversation>;
|
|
134
|
+
addMessage: (conversation: any, message: any) => Promise<import("./modules/agents.types.js").AgentMessage>;
|
|
135
|
+
subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
|
|
136
|
+
updateConfig: (config: Partial<import("./modules/agents.js").AgentsModuleConfig>) => void;
|
|
164
137
|
};
|
|
165
138
|
};
|
package/dist/client.js
CHANGED
|
@@ -14,11 +14,10 @@ import { createAgentsModule } from "./modules/agents.js";
|
|
|
14
14
|
* @param {string} [config.token] - Authentication token
|
|
15
15
|
* @param {string} [config.serviceToken] - Service role authentication token
|
|
16
16
|
* @param {boolean} [config.requiresAuth=false] - Whether the app requires authentication
|
|
17
|
-
* @param {AgentsModuleConfig} [config.agents] - Configuration for agents module
|
|
18
17
|
* @returns {Object} Base44 client instance
|
|
19
18
|
*/
|
|
20
19
|
export function createClient(config) {
|
|
21
|
-
const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false,
|
|
20
|
+
const { serverUrl = "https://base44.app", appId, token, serviceToken, requiresAuth = false, } = config;
|
|
22
21
|
const axiosClient = createAxiosClient({
|
|
23
22
|
baseURL: `${serverUrl}/api`,
|
|
24
23
|
headers: {
|
|
@@ -64,14 +63,22 @@ export function createClient(config) {
|
|
|
64
63
|
integrations: createIntegrationsModule(axiosClient, appId),
|
|
65
64
|
auth: createAuthModule(axiosClient, functionsAxiosClient, appId),
|
|
66
65
|
functions: createFunctionsModule(functionsAxiosClient, appId),
|
|
67
|
-
agents: createAgentsModule(
|
|
66
|
+
agents: createAgentsModule({
|
|
67
|
+
serverUrl,
|
|
68
|
+
appId,
|
|
69
|
+
token,
|
|
70
|
+
}),
|
|
68
71
|
};
|
|
69
72
|
const serviceRoleModules = {
|
|
70
73
|
entities: createEntitiesModule(serviceRoleAxiosClient, appId),
|
|
71
74
|
integrations: createIntegrationsModule(serviceRoleAxiosClient, appId),
|
|
72
75
|
sso: createSsoModule(serviceRoleAxiosClient, appId, token),
|
|
73
76
|
functions: createFunctionsModule(serviceRoleFunctionsAxiosClient, appId),
|
|
74
|
-
agents: createAgentsModule(
|
|
77
|
+
agents: createAgentsModule({
|
|
78
|
+
serverUrl,
|
|
79
|
+
appId,
|
|
80
|
+
token: serviceToken,
|
|
81
|
+
}),
|
|
75
82
|
};
|
|
76
83
|
// Always try to get token from localStorage or URL parameters
|
|
77
84
|
if (typeof window !== "undefined") {
|
|
@@ -79,6 +86,9 @@ export function createClient(config) {
|
|
|
79
86
|
const accessToken = token || getAccessToken();
|
|
80
87
|
if (accessToken) {
|
|
81
88
|
userModules.auth.setToken(accessToken);
|
|
89
|
+
userModules.agents.updateConfig({
|
|
90
|
+
token: accessToken,
|
|
91
|
+
});
|
|
82
92
|
}
|
|
83
93
|
}
|
|
84
94
|
// If authentication is required, verify token and redirect to login if needed
|
package/dist/index.d.ts
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
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
|
-
export type { Message, AgentConversation, CreateConversationPayload, UpdateConversationPayload, FilterParams, AgentsModuleConfig, AgentsModule, } from "./modules/agents.js";
|
|
5
4
|
export { createClient, createClientFromRequest, Base44Error, getAccessToken, saveAccessToken, removeAccessToken, getLoginUrl, };
|
package/dist/modules/agents.d.ts
CHANGED
|
@@ -1,85 +1,15 @@
|
|
|
1
|
-
import {
|
|
2
|
-
export
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
metadata: Record<string, any>;
|
|
16
|
-
created_at?: string;
|
|
17
|
-
updated_at?: string;
|
|
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;
|
|
1
|
+
import { AgentConversation, AgentMessage } from "./agents.types";
|
|
2
|
+
export type AgentsModuleConfig = {
|
|
3
|
+
serverUrl: string;
|
|
4
|
+
appId: string;
|
|
5
|
+
token?: string;
|
|
6
|
+
};
|
|
7
|
+
export declare function createAgentsModule({ appId, serverUrl, token, }: AgentsModuleConfig): {
|
|
8
|
+
getConversations: () => Promise<AgentConversation[]>;
|
|
9
|
+
getConversation: (conversationId: string) => Promise<AgentConversation | undefined>;
|
|
10
|
+
listConversations: (filterParams: any) => Promise<AgentConversation[]>;
|
|
11
|
+
createConversation: (conversation: any) => Promise<AgentConversation>;
|
|
12
|
+
addMessage: (conversation: any, message: any) => Promise<AgentMessage>;
|
|
13
|
+
subscribeToConversation: (conversationId: string, onUpdate: any) => () => void;
|
|
14
|
+
updateConfig: (config: Partial<AgentsModuleConfig>) => void;
|
|
84
15
|
};
|
|
85
|
-
export type AgentsModule = ReturnType<typeof createAgentsModule>;
|
package/dist/modules/agents.js
CHANGED
|
@@ -1,251 +1,96 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
+
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
|
+
};
|
|
31
|
+
const getConversations = () => {
|
|
32
|
+
return axios.get(`/conversations`);
|
|
33
|
+
};
|
|
34
|
+
const getConversation = (conversationId) => {
|
|
35
|
+
return axios.get(`/conversations/${conversationId}`);
|
|
36
|
+
};
|
|
37
|
+
const listConversations = (filterParams) => {
|
|
38
|
+
return axios.get(`/conversations`, {
|
|
39
|
+
params: filterParams,
|
|
58
40
|
});
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
listeners.forEach((callback) => callback(parsedData));
|
|
69
|
-
}
|
|
70
|
-
}
|
|
71
|
-
}
|
|
72
|
-
attemptReconnect() {
|
|
73
|
-
if (this.reconnectAttempts >= this.maxReconnectAttempts) {
|
|
74
|
-
console.error("Max reconnection attempts reached");
|
|
75
|
-
return;
|
|
76
|
-
}
|
|
77
|
-
this.reconnectAttempts++;
|
|
78
|
-
const delay = this.reconnectDelay * Math.pow(2, this.reconnectAttempts - 1);
|
|
79
|
-
setTimeout(() => {
|
|
80
|
-
console.log(`Attempting to reconnect (${this.reconnectAttempts}/${this.maxReconnectAttempts})...`);
|
|
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;
|
|
41
|
+
};
|
|
42
|
+
const createConversation = (conversation) => {
|
|
43
|
+
return axios.post(`/conversations`, conversation);
|
|
44
|
+
};
|
|
45
|
+
const addMessage = (conversation, message) => {
|
|
46
|
+
// this whole trick with current conversation so that we can call the onUpdateModel with the latest messages
|
|
47
|
+
let convLatestMessages = null;
|
|
48
|
+
if (currentConversation && currentConversation.id === conversation.id) {
|
|
49
|
+
convLatestMessages = currentConversation.messages;
|
|
126
50
|
}
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
var _a;
|
|
131
|
-
if (typeof WebSocket === "undefined") {
|
|
132
|
-
return false;
|
|
51
|
+
else {
|
|
52
|
+
currentConversation = conversation;
|
|
53
|
+
convLatestMessages = conversation.messages;
|
|
133
54
|
}
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
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
|
-
}
|
|
151
|
-
return {
|
|
152
|
-
/**
|
|
153
|
-
* List all conversations for the current user
|
|
154
|
-
*/
|
|
155
|
-
async listConversations(filterParams) {
|
|
156
|
-
const response = await axiosClient.get(`/apps/${appId}/agents/conversations`, {
|
|
157
|
-
params: filterParams,
|
|
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) {
|
|
55
|
+
conversation.messages = [...convLatestMessages, message];
|
|
56
|
+
roomSocket.handlers.update_model({
|
|
57
|
+
room: `/agent-conversations/${conversation.id}`,
|
|
58
|
+
data: JSON.stringify(conversation),
|
|
59
|
+
});
|
|
60
|
+
return axios.post(`/conversations/${conversation.id}/messages`, message);
|
|
61
|
+
};
|
|
62
|
+
const subscribeToConversation = (conversationId, onUpdate) => {
|
|
63
|
+
return roomSocket.subscribeToRoom(`/agent-conversations/${conversationId}`, {
|
|
64
|
+
connect: () => { },
|
|
65
|
+
update_model: ({ data: jsonStr }) => {
|
|
66
|
+
const data = JSON.parse(jsonStr);
|
|
67
|
+
if (currentConversation && currentConversation.id === data.id) {
|
|
219
68
|
currentConversation = data;
|
|
220
69
|
}
|
|
221
70
|
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
|
-
},
|
|
71
|
+
},
|
|
72
|
+
});
|
|
250
73
|
};
|
|
74
|
+
return {
|
|
75
|
+
getConversations,
|
|
76
|
+
getConversation,
|
|
77
|
+
listConversations,
|
|
78
|
+
createConversation,
|
|
79
|
+
addMessage,
|
|
80
|
+
subscribeToConversation,
|
|
81
|
+
updateConfig,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
function createAgentsAxiosClient({ serverUrl, appId, token, }) {
|
|
85
|
+
const axios = createAxiosClient({
|
|
86
|
+
baseURL: `${serverUrl}/api/apps/${appId}/agents`,
|
|
87
|
+
appId,
|
|
88
|
+
serverUrl,
|
|
89
|
+
token,
|
|
90
|
+
interceptResponses: true,
|
|
91
|
+
headers: {
|
|
92
|
+
"X-App-Id": String(appId),
|
|
93
|
+
},
|
|
94
|
+
});
|
|
95
|
+
return axios;
|
|
251
96
|
}
|