Nav apraksta
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

agent.service.ts 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import { Injectable } from '@angular/core';
  2. import { HttpClient } from '@angular/common/http';
  3. import { Observable } from 'rxjs';
  4. import { map } from 'rxjs/operators';
  5. import { Agent, AgentDisplayName } from '../models/session.model';
  6. // 智能体项接口(对应后端AgentItem)
  7. export interface AgentItem {
  8. id: string; // 智能体ID
  9. name: string; // 智能体名称(标题)
  10. }
  11. // 智能体列表响应
  12. interface AgentListResponse {
  13. success: boolean;
  14. message?: string;
  15. data?: AgentItem[];
  16. }
  17. @Injectable({
  18. providedIn: 'root'
  19. })
  20. export class AgentService {
  21. constructor(private http: HttpClient) {}
  22. /**
  23. * 获取智能体列表
  24. */
  25. getAgents(): Observable<AgentItem[]> {
  26. return this.http.get<AgentListResponse>('/api/agents').pipe(
  27. map(response => {
  28. if (response.success && response.data) {
  29. return response.data;
  30. } else {
  31. throw new Error(response.message || '获取智能体列表失败');
  32. }
  33. })
  34. );
  35. }
  36. /**
  37. * 获取智能体显示名称
  38. * @param agentId 智能体ID
  39. */
  40. getAgentDisplayName(agentId: string): string {
  41. // 首先尝试从映射中获取
  42. if (agentId in AgentDisplayName) {
  43. return AgentDisplayName[agentId as Agent];
  44. }
  45. // 如果映射中没有,返回ID本身
  46. return agentId;
  47. }
  48. /**
  49. * 获取智能体选项列表(用于下拉框)
  50. */
  51. getAgentOptions(): Observable<{value: string, label: string}[]> {
  52. return this.getAgents().pipe(
  53. map(agents => agents.map(agent => ({
  54. value: agent.id,
  55. label: this.getAgentDisplayName(agent.id)
  56. })))
  57. );
  58. }
  59. /**
  60. * 验证智能体ID是否有效
  61. */
  62. isValidAgent(agentId: string): boolean {
  63. return agentId in AgentDisplayName;
  64. }
  65. /**
  66. * 获取默认智能体(第一个智能体)
  67. */
  68. getDefaultAgent(): string {
  69. const agents = Object.keys(AgentDisplayName);
  70. return agents.length > 0 ? agents[0] : 'report';
  71. }
  72. }