bobbai00 commented on code in PR #4020: URL: https://github.com/apache/texera/pull/4020#discussion_r2562621188
########## frontend/src/app/workspace/component/agent-panel/agent-chat/agent-chat.component.ts: ########## @@ -0,0 +1,236 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +import { Component, ViewChild, ElementRef, Input, OnInit, AfterViewChecked } from "@angular/core"; +import { UntilDestroy, untilDestroyed } from "@ngneat/until-destroy"; +import { CopilotState, AgentUIMessage } from "../../../service/copilot/texera-copilot"; +import { AgentInfo, TexeraCopilotManagerService } from "../../../service/copilot/texera-copilot-manager.service"; +import { NotificationService } from "../../../../common/service/notification/notification.service"; + +@UntilDestroy() +@Component({ + selector: "texera-agent-chat", + templateUrl: "agent-chat.component.html", + styleUrls: ["agent-chat.component.scss"], +}) +export class AgentChatComponent implements OnInit, AfterViewChecked { + @Input() agentInfo!: AgentInfo; + @ViewChild("messageContainer", { static: false }) messageContainer?: ElementRef; + @ViewChild("messageInput", { static: false }) messageInput?: ElementRef; + + public agentResponses: AgentUIMessage[] = []; + public currentMessage = ""; + private shouldScrollToBottom = false; + public isDetailsModalVisible = false; + public selectedResponse: AgentUIMessage | null = null; + public hoveredMessageIndex: number | null = null; + public isSystemInfoModalVisible = false; + public systemPrompt: string = ""; + public availableTools: Array<{ name: string; description: string; inputSchema: any }> = []; + public agentState: CopilotState = CopilotState.UNAVAILABLE; + + constructor( + private copilotManagerService: TexeraCopilotManagerService, + private notificationService: NotificationService + ) {} + + ngOnInit(): void { + if (!this.agentInfo) { + return; + } + + // Subscribe to agent responses + this.copilotManagerService + .getAgentResponsesObservable(this.agentInfo.id) + .pipe(untilDestroyed(this)) + .subscribe(responses => { + this.agentResponses = responses; + this.shouldScrollToBottom = true; + }); + + // Subscribe to agent state changes + this.copilotManagerService + .getAgentStateObservable(this.agentInfo.id) + .pipe(untilDestroyed(this)) + .subscribe(state => { + this.agentState = state; + }); + } + + ngAfterViewChecked(): void { + if (this.shouldScrollToBottom) { + this.scrollToBottom(); + this.shouldScrollToBottom = false; + } + } + + public setHoveredMessage(index: number | null): void { + this.hoveredMessageIndex = index; + } + + public showResponseDetails(response: AgentUIMessage): void { + this.selectedResponse = response; + this.isDetailsModalVisible = true; + } + + public closeDetailsModal(): void { + this.isDetailsModalVisible = false; + this.selectedResponse = null; + } + + public showSystemInfo(): void { + this.copilotManagerService + .getSystemInfo(this.agentInfo.id) + .pipe(untilDestroyed(this)) + .subscribe(systemInfo => { + this.systemPrompt = systemInfo.systemPrompt; + this.availableTools = systemInfo.tools; + this.isSystemInfoModalVisible = true; + }); + } + + public closeSystemInfoModal(): void { + this.isSystemInfoModalVisible = false; + } + + public formatJson(data: any): string { + return JSON.stringify(data, null, 2); + } + + public getToolResult(response: AgentUIMessage, toolCallIndex: number): any { + if (!response.toolResults || toolCallIndex >= response.toolResults.length) { + return null; + } + const toolResult = response.toolResults[toolCallIndex]; + return toolResult.output || toolResult.result || toolResult; + } + + public getTotalInputTokens(): number { + for (let i = this.agentResponses.length - 1; i >= 0; i--) { + const response = this.agentResponses[i]; + if (response.usage?.inputTokens !== undefined) { + return response.usage.inputTokens; + } + } + return 0; + } + + public getTotalOutputTokens(): number { + for (let i = this.agentResponses.length - 1; i >= 0; i--) { + const response = this.agentResponses[i]; + if (response.usage?.outputTokens !== undefined) { + return response.usage.outputTokens; + } + } + return 0; + } + + /** + * Send a message to the agent via the copilot manager service. + */ + public sendMessage(): void { + if (!this.currentMessage.trim() || !this.canSendMessage()) { + return; + } + + const userMessage = this.currentMessage.trim(); + this.currentMessage = ""; + + // Send to copilot via manager service + this.copilotManagerService + .sendMessage(this.agentInfo.id, userMessage) + .pipe(untilDestroyed(this)) + .subscribe({ + error: (error: unknown) => { + this.notificationService.error(`Error sending message: ${error}`); + }, + }); + } + + /** + * Check if messages can be sent (only when agent is available). + */ + public canSendMessage(): boolean { + return this.agentState === CopilotState.AVAILABLE; + } + + /** + * Get the state icon URL based on current agent state. + * Uses the same icons as workflow operators for consistency. + */ + public getStateIconUrl(): string { + return this.agentState === CopilotState.AVAILABLE ? "assets/svg/done.svg" : "assets/gif/loading.gif"; + } + + /** + * Get the tooltip text for the state icon. + */ + public getStateTooltip(): string { + switch (this.agentState) { + case CopilotState.AVAILABLE: + return "Agent is ready"; + case CopilotState.GENERATING: + return "Agent is generating response..."; + case CopilotState.STOPPING: + return "Agent is stopping..."; + case CopilotState.UNAVAILABLE: + return "Agent is unavailable"; + default: + return "Agent status unknown"; + } + } + + public onEnterPress(event: KeyboardEvent): void { + if (!event.shiftKey) { + event.preventDefault(); + this.sendMessage(); + } + } + + private scrollToBottom(): void { + if (this.messageContainer) { + const element = this.messageContainer.nativeElement; + element.scrollTop = element.scrollHeight; + } + } + + public stopGeneration(): void { + this.copilotManagerService.stopGeneration(this.agentInfo.id).pipe(untilDestroyed(this)).subscribe(); + } + + public clearMessages(): void { + this.copilotManagerService.clearMessages(this.agentInfo.id).pipe(untilDestroyed(this)).subscribe(); + } + + public isGenerating(): boolean { + return this.agentState === CopilotState.GENERATING; + } + + public isStopping(): boolean { Review Comment: removed ########## frontend/src/app/workspace/component/workflow-editor/context-menu/context-menu/context-menu.component.html: ########## @@ -139,7 +139,7 @@ <!-- Delete option for links only --> <li nz-menu-item - *ngIf="hasHighlightedLinks() && + *ngIf="hasHighlightedLinks() && Review Comment: removed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
