agentLocalStore.ts
1.61 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
import type { AgentDetailRecord } from '../../../packages/contracts/agent-editor-mock';
import type { AgentListMockItem } from '../../../packages/contracts/agent-list-mock';
const storageKey = 'agent-admin-saved-agents';
function canUseStorage() {
return typeof window !== 'undefined' && typeof window.localStorage !== 'undefined';
}
export function readSavedAgents() {
if (!canUseStorage()) {
return [] as AgentDetailRecord[];
}
try {
const raw = window.localStorage.getItem(storageKey);
return raw ? (JSON.parse(raw) as AgentDetailRecord[]) : [];
} catch {
return [];
}
}
export function upsertSavedAgent(detail: AgentDetailRecord) {
if (!canUseStorage()) {
return;
}
const current = readSavedAgents();
const next = [...current.filter((item) => item.agent_id !== detail.agent_id), detail];
window.localStorage.setItem(storageKey, JSON.stringify(next));
}
export function findSavedAgent(agentId?: string) {
if (!agentId) {
return undefined;
}
return readSavedAgents().find((item) => String(item.agent_id) === agentId);
}
export function projectDetailToListItem(detail: AgentDetailRecord): AgentListMockItem {
return {
agent_id: detail.agent_id,
name: detail.name,
is_manager: detail.is_manager,
positions: detail.positions,
status: detail.status,
bound_group_count: detail.is_manager ? 12 : 0,
bound_skill_count: detail.is_manager ? 0 : detail.skill_ids.length,
updated_at: '2026-05-20 19:05:00',
available_actions: detail.is_manager ? ['detail', 'edit'] : ['detail', 'edit', detail.status === 'enable' ? 'disable' : 'enable', 'delete'],
};
}