Keine Beschreibung
Du kannst nicht mehr als 25 Themen auswählen Themen müssen mit entweder einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

project-tab.component.ts 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. import { Component, OnInit, OnDestroy, HostListener, Input } from '@angular/core';
  2. import { CommonModule } from '@angular/common';
  3. import { ActivatedRoute } from '@angular/router';
  4. import { MatIcon } from '@angular/material/icon';
  5. import { MatButtonModule } from '@angular/material/button';
  6. import { MatCardModule } from '@angular/material/card';
  7. import { MatListModule } from '@angular/material/list';
  8. import { MatExpansionModule } from '@angular/material/expansion';
  9. import { MatChipsModule } from '@angular/material/chips';
  10. import { MatProgressBarModule } from '@angular/material/progress-bar';
  11. import { MatTooltipModule } from '@angular/material/tooltip';
  12. import { Subscription } from 'rxjs';
  13. import { ConversationComponent } from './conversation.component';
  14. import { DocumentListComponent } from './document-list.component';
  15. import { AIResponseComponent } from './ai-response.component';
  16. import { TabService, Tab } from '../services/tab.service';
  17. import { SessionService } from '../services/session.service';
  18. import { AgentService } from '../services/agent.service';
  19. import { MockDataService } from '../services/mock-data.service';
  20. import { EventService } from '../services/event.service';
  21. import { Session, SessionStatus } from '../models/session.model';
  22. import { DocumentType, DocumentSession } from '../models/document.model';
  23. @Component({
  24. selector: 'app-project-tab',
  25. standalone: true,
  26. imports: [
  27. CommonModule,
  28. MatIcon,
  29. MatButtonModule,
  30. MatCardModule,
  31. MatListModule,
  32. MatExpansionModule,
  33. MatChipsModule,
  34. MatProgressBarModule,
  35. MatTooltipModule,
  36. ConversationComponent,
  37. DocumentListComponent,
  38. AIResponseComponent
  39. ],
  40. templateUrl: './project-tab.component.html',
  41. styleUrl: './project-tab.component.scss',
  42. })
  43. export class ProjectTabComponent implements OnInit, OnDestroy {
  44. @Input() tabId?: string;
  45. project: Session | null = null;
  46. activeTab: Tab | null = null;
  47. // 文档数据
  48. documentSessions: DocumentSession[] = [];
  49. leftWidth = 320;
  50. middleWidth = 500;
  51. isDragging = false;
  52. isDraggingMiddle = false;
  53. minLeftWidth = 280;
  54. maxLeftWidth = 500;
  55. minMiddleWidth = 400;
  56. maxMiddleWidth = 800;
  57. private subscriptions: Subscription = new Subscription();
  58. private eventSubscription?: Subscription;
  59. constructor(
  60. private tabService: TabService,
  61. private sessionService: SessionService,
  62. private agentService: AgentService,
  63. private mockDataService: MockDataService,
  64. private eventService: EventService,
  65. private route: ActivatedRoute
  66. ) {}
  67. ngOnInit() {
  68. // 从本地存储恢复宽度
  69. const savedLeftWidth = localStorage.getItem('project_tab_leftWidth');
  70. if (savedLeftWidth) {
  71. this.leftWidth = parseInt(savedLeftWidth, 10);
  72. }
  73. const savedMiddleWidth = localStorage.getItem('project_tab_middleWidth');
  74. if (savedMiddleWidth) {
  75. this.middleWidth = parseInt(savedMiddleWidth, 10);
  76. }
  77. // 监听路由参数变化
  78. this.subscriptions.add(
  79. this.route.paramMap.subscribe(params => {
  80. const projectId = params.get('projectId');
  81. if (projectId) {
  82. this.loadTabByProjectId(projectId);
  83. }
  84. })
  85. );
  86. // 获取活动页签
  87. this.subscriptions.add(
  88. this.tabService.activeTab$.subscribe(tab => {
  89. this.activeTab = tab || null;
  90. if (tab) {
  91. this.project = tab.session;
  92. // 设置活动会话
  93. this.sessionService.setActiveSession(this.project);
  94. } else {
  95. this.project = null;
  96. }
  97. })
  98. );
  99. // 如果指定了tabId,直接获取
  100. if (this.tabId) {
  101. const tab = this.tabService.getTabById(this.tabId);
  102. if (tab) {
  103. this.activeTab = tab;
  104. this.project = tab.session;
  105. this.sessionService.setActiveSession(this.project);
  106. this.loadDocumentSessions();
  107. }
  108. }
  109. // 加载文档数据
  110. this.loadDocumentSessions();
  111. }
  112. private loadTabByProjectId(projectId: string) {
  113. const tab = this.tabService.getTabByProjectId(projectId);
  114. if (tab) {
  115. this.activeTab = tab;
  116. this.project = tab.session;
  117. this.sessionService.setActiveSession(this.project);
  118. } else {
  119. console.warn('找不到项目ID对应的标签页:', projectId);
  120. // 可能标签页已被关闭,尝试从会话服务加载会话
  121. this.subscriptions.add(
  122. this.sessionService.sessions$.subscribe(sessions => {
  123. const session = sessions.find(s => s.project_id === projectId || s.id === projectId);
  124. if (session) {
  125. // 创建临时标签页
  126. this.project = session;
  127. this.sessionService.setActiveSession(session);
  128. }
  129. })
  130. );
  131. }
  132. }
  133. getAgentDisplayName(agentId: string): string {
  134. return this.agentService.getAgentDisplayName(agentId);
  135. }
  136. getStatusDisplayName(status: string): string {
  137. const statusMap: Record<string, string> = {
  138. 'requirement_document': '需求文档',
  139. 'technical_document': '技术文档',
  140. 'code': '代码开发',
  141. 'test': '测试',
  142. 'release': '发布'
  143. };
  144. return statusMap[status] || status;
  145. }
  146. getStatusClass(status: string): string {
  147. return `status-${status}`;
  148. }
  149. formatDate(dateString?: string): string {
  150. if (!dateString) return '未知';
  151. const date = new Date(dateString);
  152. return date.toLocaleDateString('zh-CN');
  153. }
  154. getProgressValue(status: string): number {
  155. const progressMap: Record<string, number> = {
  156. 'requirement_document': 20,
  157. 'technical_document': 40,
  158. 'code': 60,
  159. 'test': 80,
  160. 'release': 100
  161. };
  162. return progressMap[status] || 0;
  163. }
  164. getProgressText(status: string): string {
  165. const progressMap: Record<string, string> = {
  166. 'requirement_document': '20% - 需求分析',
  167. 'technical_document': '40% - 技术设计',
  168. 'code': '60% - 代码开发',
  169. 'test': '80% - 测试验证',
  170. 'release': '100% - 发布完成'
  171. };
  172. return progressMap[status] || '0% - 未开始';
  173. }
  174. focusRequirementInput() {
  175. // TODO: 聚焦到对话输入框,并提示输入需求
  176. console.log('聚焦需求输入');
  177. // 可以通过服务或事件通知ConversationComponent
  178. }
  179. startDrag(event: MouseEvent) {
  180. event.preventDefault();
  181. this.isDragging = true;
  182. document.body.style.cursor = 'col-resize';
  183. document.body.style.userSelect = 'none';
  184. }
  185. startMiddleDrag(event: MouseEvent) {
  186. event.preventDefault();
  187. this.isDraggingMiddle = true;
  188. document.body.style.cursor = 'col-resize';
  189. document.body.style.userSelect = 'none';
  190. }
  191. @HostListener('document:mousemove', ['$event'])
  192. onDrag(event: MouseEvent) {
  193. if (this.isDragging) {
  194. const newWidth = event.clientX;
  195. if (newWidth >= this.minLeftWidth && newWidth <= this.maxLeftWidth) {
  196. this.leftWidth = newWidth;
  197. }
  198. }
  199. if (this.isDraggingMiddle) {
  200. const newWidth = event.clientX - this.leftWidth - 8;
  201. if (newWidth >= this.minMiddleWidth && newWidth <= this.maxMiddleWidth) {
  202. this.middleWidth = newWidth;
  203. }
  204. }
  205. }
  206. @HostListener('document:mouseup')
  207. stopDrag() {
  208. if (this.isDragging) {
  209. this.isDragging = false;
  210. localStorage.setItem('project_tab_leftWidth', this.leftWidth.toString());
  211. }
  212. if (this.isDraggingMiddle) {
  213. this.isDraggingMiddle = false;
  214. localStorage.setItem('project_tab_middleWidth', this.middleWidth.toString());
  215. }
  216. document.body.style.cursor = '';
  217. document.body.style.userSelect = '';
  218. }
  219. /**
  220. * 加载文档会话数据
  221. */
  222. private loadDocumentSessions() {
  223. if (!this.project) return;
  224. const projectId = this.project.project_id || this.project.id;
  225. this.mockDataService.getProjectDocuments(projectId).subscribe({
  226. next: (documents) => {
  227. this.documentSessions = documents;
  228. // 设置文档会话的事件订阅
  229. this.setupDocumentEventSubscriptions();
  230. },
  231. error: (error) => {
  232. console.error('加载文档数据失败:', error);
  233. // 使用空文档作为回退
  234. import('../models/document.model').then(module => {
  235. this.documentSessions = module.createEmptyDocumentSessions(projectId);
  236. // 即使使用空文档,也尝试设置事件订阅
  237. this.setupDocumentEventSubscriptions();
  238. });
  239. }
  240. });
  241. }
  242. /**
  243. * 设置文档会话的事件订阅
  244. */
  245. private setupDocumentEventSubscriptions() {
  246. // 取消现有的事件订阅
  247. if (this.eventSubscription) {
  248. this.eventSubscription.unsubscribe();
  249. this.eventSubscription = undefined;
  250. }
  251. // 获取有效的文档会话ID
  252. const sessionIds = this.getDocumentSessionIds();
  253. if (sessionIds.length === 0) {
  254. console.log('没有有效的文档会话ID可订阅');
  255. return;
  256. }
  257. console.log(`设置文档事件订阅,会话ID:`, sessionIds);
  258. // 连接到多会话事件流
  259. this.eventService.connectToMultipleSessions(sessionIds);
  260. // 订阅多会话事件
  261. this.eventSubscription = this.eventService.multiSessionEvents$.subscribe({
  262. next: ({ sessionId, event }) => {
  263. this.handleDocumentEvent(sessionId, event);
  264. },
  265. error: (error) => {
  266. console.error('文档事件订阅错误:', error);
  267. }
  268. });
  269. }
  270. /**
  271. * 处理文档事件,更新文档内容
  272. */
  273. private handleDocumentEvent(sessionId: string, event: any) {
  274. // 找到对应的文档
  275. const docIndex = this.documentSessions.findIndex(doc => doc.sessionId === sessionId);
  276. if (docIndex === -1) {
  277. // 未找到对应文档,可能是其他事件
  278. return;
  279. }
  280. const payload = event.payload;
  281. console.log(`处理文档事件,会话ID: ${sessionId}, 类型: ${payload.type}`);
  282. // 提取内容
  283. let content = '';
  284. const properties = payload.properties || {};
  285. if (payload.type === 'message.updated') {
  286. // 处理消息更新事件
  287. const messageInfo = properties.info || {};
  288. if (messageInfo.content) {
  289. content = messageInfo.content;
  290. }
  291. } else if (properties.content) {
  292. content = properties.content;
  293. } else if (properties.message) {
  294. content = properties.message;
  295. } else if (properties.info && properties.info.content) {
  296. content = properties.info.content;
  297. }
  298. if (content) {
  299. // 更新文档内容
  300. this.documentSessions[docIndex] = {
  301. ...this.documentSessions[docIndex],
  302. content: content,
  303. lastUpdated: new Date(),
  304. hasContent: true,
  305. isLoading: false
  306. };
  307. // 触发变更检测(使用展开操作符创建新数组)
  308. this.documentSessions = [...this.documentSessions];
  309. console.log(`文档 ${this.documentSessions[docIndex].title} 内容已更新,长度: ${content.length} 字符`);
  310. }
  311. }
  312. /**
  313. * 获取所有文档会话ID(过滤空值)
  314. */
  315. getDocumentSessionIds(): string[] {
  316. return this.documentSessions
  317. .map(doc => doc.sessionId)
  318. .filter(sessionId => sessionId && sessionId.trim() !== '');
  319. }
  320. ngOnDestroy() {
  321. this.subscriptions.unsubscribe();
  322. // 取消文档事件订阅
  323. if (this.eventSubscription) {
  324. this.eventSubscription.unsubscribe();
  325. this.eventSubscription = undefined;
  326. }
  327. }
  328. }