agent.ts
5.29 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
import { apiClient, type PaginatedResponse } from './client';
export type AgentStatus = 'enable' | 'disable';
export interface AgentPosition extends Record<string, unknown> {
id: number;
name: string;
}
export interface AgentListItem extends Record<string, unknown> {
agent_id: number;
name: string;
code: string;
is_steward: boolean;
positions: AgentPosition[];
position_names: string[];
skills_preview: string[];
skill_codes: string[];
skill_count: number;
status: AgentStatus;
creator_name: string;
created_at: string;
operations: string[];
}
export interface AgentListQuery {
keyword?: string;
position_id?: number;
status?: AgentStatus;
page: number;
page_size: number;
}
export interface AgentListResponse extends PaginatedResponse<AgentListItem> {}
export interface AgentDetailSkill {
skill_code: string;
skill_name: string;
}
export interface AgentDetail {
agent_id: number;
name: string;
code: string;
description: string;
positions: AgentPosition[];
skills: AgentDetailSkill[];
skill_ids: string[];
status: AgentStatus;
managed_user: { user_id: number; user_name: string } | null;
session_count: number;
created_at: string;
updated_at: string;
}
export function isStewardAgent(detail: AgentDetail): boolean {
return detail.code === 'steward';
}
export interface CreateAgentPayload {
name: string;
position_ids: number[];
managed_user_id: number | string;
description: string;
skill_codes: string[];
status: AgentStatus;
}
export interface CreateAgentResponse {
agent_id: number;
name: string;
code: string;
position_ids: number[];
skill_codes: string[];
created_at: string;
}
export interface UpdateAgentPayload {
name?: string;
position_ids?: number[];
managed_user_id?: number | string;
description?: string;
skill_codes?: string[];
status?: AgentStatus;
}
export interface UpdateAgentStatusPayload {
status: AgentStatus;
}
export interface DeleteAgentResponse {
success: boolean;
deleted_at: string;
}
/**
* 根据状态推断可用操作
* - enable 状态: 可查看、编辑、停用、删除
* - disable 状态: 可查看、编辑、上架、删除
* - 管家 Agent (is_steward === true): 不可停用、不可删除
*/
function inferAgentOperations(status: AgentStatus, isSteward: boolean): string[] {
const baseOps = status === 'enable'
? ['view', 'edit', 'disable', 'delete']
: ['view', 'edit', 'enable', 'delete'];
// 管家 Agent 不允许停用和删除
if (isSteward) {
return baseOps.filter(op => op !== 'disable' && op !== 'delete');
}
return baseOps;
}
/**
* 为列表项填充默认的 operations 字段
* 如果后端返回空数组,前端也会根据状态和 is_steward 推断
*/
function fillAgentListItem(item: Partial<AgentListItem>): AgentListItem {
const status = item.status ?? 'enable';
const isSteward = item.is_steward ?? false;
const operations = item.operations?.length > 0 ? item.operations : inferAgentOperations(status, isSteward);
return {
agent_id: item.agent_id ?? 0,
name: item.name ?? '',
code: item.code ?? '',
is_steward: isSteward,
positions: item.positions ?? [],
position_names: item.position_names ?? [],
skills_preview: item.skills_preview ?? [],
skill_codes: item.skill_codes ?? [],
skill_count: item.skill_count ?? 0,
status,
creator_name: item.creator_name ?? '',
created_at: item.created_at ?? '',
operations,
};
}
export async function getAgentList(query: AgentListQuery): Promise<AgentListResponse> {
const response = await apiClient.get<AgentListResponse>('/agents', { params: query });
return {
...response,
list: response.list.map(fillAgentListItem),
};
}
export async function getAgentDetail(agentId: number): Promise<AgentDetail> {
return apiClient.get<AgentDetail>(`/agents/${agentId}`);
}
export async function createAgent(payload: CreateAgentPayload): Promise<CreateAgentResponse> {
return apiClient.post<CreateAgentResponse>('/agents', payload);
}
export async function updateAgent(agentId: number, payload: UpdateAgentPayload): Promise<AgentDetail> {
return apiClient.put<AgentDetail>(`/agents/${agentId}`, payload);
}
export async function updateAgentStatus(agentId: number, status: AgentStatus): Promise<{ agent_id: number; status: AgentStatus }> {
return apiClient.put<{ agent_id: number; status: AgentStatus }>(`/agents/${agentId}/status`, { status });
}
export async function deleteAgent(agentId: number): Promise<DeleteAgentResponse> {
return apiClient.delete<DeleteAgentResponse>(`/agents/${agentId}`);
}
export interface EmployeeMountAgent {
agent_id: number;
agent_name: string;
status: AgentStatus;
}
export interface EmployeeMountQrcode {
qrcode_id: number;
qrcode_name: string;
channel_code: string;
status: AgentStatus;
position_id: number;
position_name: string;
}
export interface EmployeeMountItem {
employee_id: string;
employee_name: string;
wechat_user_id: string;
agent_count: number;
qrcode_count: number;
agents: EmployeeMountAgent[];
qrcodes: EmployeeMountQrcode[];
}
export interface EmployeeMountsResponse {
employee_count: number;
employees: EmployeeMountItem[];
}
export async function getEmployeeMounts(): Promise<EmployeeMountsResponse> {
return apiClient.get<EmployeeMountsResponse>('/agent-group/employees/mounts');
}