import { Injectable } from '@angular/core'; import { HttpClient } from '@angular/common/http'; import { Observable } from 'rxjs'; import { map } from 'rxjs/operators'; import { Agent, AgentDisplayName } from '../models/session.model'; // 智能体项接口(对应后端AgentItem) export interface AgentItem { id: string; // 智能体ID name: string; // 智能体名称(标题) } // 智能体列表响应 interface AgentListResponse { success: boolean; message?: string; data?: AgentItem[]; } @Injectable({ providedIn: 'root' }) export class AgentService { constructor(private http: HttpClient) {} /** * 获取智能体列表 */ getAgents(): Observable { return this.http.get('/api/agents').pipe( map(response => { if (response.success && response.data) { return response.data; } else { throw new Error(response.message || '获取智能体列表失败'); } }) ); } /** * 获取智能体显示名称 * @param agentId 智能体ID */ getAgentDisplayName(agentId: string): string { // 首先尝试从映射中获取 if (agentId in AgentDisplayName) { return AgentDisplayName[agentId as Agent]; } // 如果映射中没有,返回ID本身 return agentId; } /** * 获取智能体选项列表(用于下拉框) */ getAgentOptions(): Observable<{value: string, label: string}[]> { return this.getAgents().pipe( map(agents => agents.map(agent => ({ value: agent.id, label: this.getAgentDisplayName(agent.id) }))) ); } /** * 验证智能体ID是否有效 */ isValidAgent(agentId: string): boolean { return agentId in AgentDisplayName; } /** * 获取默认智能体(第一个智能体) */ getDefaultAgent(): string { const agents = Object.keys(AgentDisplayName); return agents.length > 0 ? agents[0] : 'report'; } }