import { Component, Input, Output, EventEmitter, OnInit } from '@angular/core'; import { CommonModule } from '@angular/common'; import { Router } from '@angular/router'; import { FormsModule } from '@angular/forms'; import { MatSelectModule } from '@angular/material/select'; import { MatInputModule } from '@angular/material/input'; import { MatFormFieldModule } from '@angular/material/form-field'; import { MatButtonModule } from '@angular/material/button'; import { MatIconModule } from '@angular/material/icon'; import { MatProgressSpinnerModule } from '@angular/material/progress-spinner'; import { AgentService } from '../services/agent.service'; import { SessionService } from '../services/session.service'; import { MenuService } from '../services/menu.service'; import { ProjectCreateRequest } from '../models/session.model'; @Component({ selector: 'app-new-project-modal', standalone: true, imports: [ CommonModule, FormsModule, MatSelectModule, MatInputModule, MatFormFieldModule, MatButtonModule, MatIconModule, MatProgressSpinnerModule ], templateUrl: './new-project-modal.component.html', styleUrl: './new-project-modal.component.scss' }) export class NewProjectModalComponent implements OnInit { @Input() visible = false; @Output() visibleChange = new EventEmitter(); @Output() projectCreated = new EventEmitter(); agents: {value: string, label: string}[] = []; projectData: ProjectCreateRequest = { title: '', agent_name: '', description: '' }; isLoading = false; constructor( private agentService: AgentService, private sessionService: SessionService, private menuService: MenuService, private router: Router ) {} ngOnInit() { this.loadAgents(); } loadAgents() { this.agentService.getAgentOptions().subscribe({ next: (agents) => { this.agents = agents; // 设置默认智能体 if (agents.length > 0 && !this.projectData.agent_name) { this.projectData.agent_name = agents[0].value; } }, error: (error) => { console.error('加载智能体列表失败:', error); // 使用本地映射作为备选 this.agents = Object.entries(this.agentService.getAgentDisplayName('')).map(([value, label]) => ({ value, label })); } }); } createProject() { if (!this.projectData.title.trim() || !this.projectData.agent_name) { return; } this.isLoading = true; // 生成项目ID const projectId = `proj_${crypto.randomUUID()}`; const request: ProjectCreateRequest = { ...this.projectData, project_id: projectId }; this.sessionService.createProject(request).subscribe({ next: (session) => { console.log('项目创建成功:', session); this.isLoading = false; // 通知父组件 this.projectCreated.emit(session); this.close(); this.resetForm(); }, error: (error) => { console.error('创建项目失败:', error); alert(`创建项目失败: ${error.message}`); this.isLoading = false; } }); } close() { this.visible = false; this.visibleChange.emit(false); this.resetForm(); } resetForm() { this.projectData = { title: '', agent_name: this.agents.length > 0 ? this.agents[0].value : '', description: '' }; } }