()
| 302 | } |
| 303 | |
| 304 | export async function fetchUserAgentGroups(): Promise<MemoryGroup[]> { |
| 305 | // Parallel requests: user memory + full Agent list |
| 306 | const [{ items }, agentsRes] = await Promise.all([ |
| 307 | listMemories("user_agent"), |
| 308 | fetchAllAgents(), |
| 309 | ]); |
| 310 | |
| 311 | const groupMap: Record<string, MemoryItem[]> = {}; |
| 312 | items.forEach((item) => { |
| 313 | if (!item.agent_id) return; |
| 314 | if (!groupMap[item.agent_id]) groupMap[item.agent_id] = []; |
| 315 | groupMap[item.agent_id].push(item); |
| 316 | }); |
| 317 | |
| 318 | const agentList: Array<{ |
| 319 | agent_id: string | number; |
| 320 | name?: string; |
| 321 | display_name?: string; |
| 322 | }> = (agentsRes as any)?.success ? (agentsRes as any).data : []; |
| 323 | |
| 324 | const groups: MemoryGroup[] = []; |
| 325 | |
| 326 | agentList.forEach((agent) => { |
| 327 | const agentId = String(agent.agent_id); |
| 328 | const list = groupMap[agentId] || []; |
| 329 | groups.push({ |
| 330 | title: i18next.t("memoryService.userAgentGroupTitle", { |
| 331 | agentName: agent.display_name || agent.name || agentId, |
| 332 | }), |
| 333 | key: `user-agent-${agentId}`, |
| 334 | items: list, |
| 335 | }); |
| 336 | }); |
| 337 | |
| 338 | Object.entries(groupMap).forEach(([agentId, list]) => { |
| 339 | if (!agentList.some((a) => String(a.agent_id) === agentId)) { |
| 340 | groups.push({ |
| 341 | title: i18next.t("memoryService.userAgentGroupTitle", { |
| 342 | agentName: list[0]?.agent_name || agentId, |
| 343 | }), |
| 344 | key: `user-agent-${agentId}`, |
| 345 | items: list, |
| 346 | }); |
| 347 | } |
| 348 | }); |
| 349 | |
| 350 | if (groups.length === 0) { |
| 351 | return [ |
| 352 | { |
| 353 | title: i18next.t("memoryService.userAgentPlaceholder"), |
| 354 | key: "user-agent-placeholder", |
| 355 | items: [], |
| 356 | }, |
| 357 | ]; |
| 358 | } |
| 359 | return groups; |
| 360 | } |
| 361 |
no test coverage detected