-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagentService.ts
85 lines (74 loc) · 2.37 KB
/
agentService.ts
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { apiRequest } from './apiUtils';
import { IAgent } from '../models/IAgent';
export const fetchAgent = async (agentId: string) => {
return apiRequest<IAgent>(`/api/agent/${agentId}`);
};
export const fetchUserAgents = async (userId: string) => {
return apiRequest<IAgent[]>(`/api/agent?userId=${userId}`);
};
export const updateAgent = async (agentId: string, updateData: any) => {
return apiRequest<IAgent>(`/api/agent/${agentId}`, {
method: 'PUT',
body: JSON.stringify(updateData),
});
};
export const deleteAgent = async (agentId: string) => {
return apiRequest(`/api/agent/${agentId}`, {
method: 'DELETE',
});
};
export const saveFlowConfig = async (agentId: string, flowConfig: string) => {
return apiRequest(`/api/agent/${agentId}`, {
method: 'PUT',
body: JSON.stringify({ flowConfig }),
});
};
// Get agent count
export const getAgentCount = async (): Promise<number> => {
try {
const response = await apiRequest<{ count: number }>(`/api/agent/count`, {
method: 'GET',
});
return response.count;
} catch (error: unknown) {
console.error('Error fetching agent count:', error);
return 0;
}
};
// Create a new agent
export const createAgent = async (agentData: { name: string; description: string; isActive: boolean; ownerType: string; userId?: string; teamId?: string; flowConfig?: string }): Promise<IAgent> => {
return apiRequest<IAgent>('/api/agent', {
method: 'POST',
body: JSON.stringify(agentData),
});
};
/**
* Fetch an agent's flow configuration
*/
export async function fetchFlowConfig(agentId: string): Promise<string> {
try {
const data = await apiRequest<any>(`/api/agent/${agentId}/flow`, {
method: 'GET',
});
return data.flowConfig || '{"nodes":[],"edges":[]}';
} catch (error: unknown) {
console.error('Error fetching agent flow config:', error);
throw error;
}
}
/**
* Fetch a flow configuration by agent ID - used by server-side API routes
* @param agentId The ID of the agent
* @returns The flow configuration as a string
*/
export async function getFlowById(agentId: string): Promise<string> {
try {
const data = await apiRequest<any>(`/api/agent/${agentId}/flow`, {
method: 'GET',
});
return data.flowConfig || '{"nodes":[],"edges":[]}';
} catch (error: unknown) {
console.error('Error fetching flow by ID:', error);
throw error;
}
}