diff --git a/frontend/src/app/workspace/component/agent/agent-panel/agent-chat/agent-chat.component.spec.ts b/frontend/src/app/workspace/component/agent/agent-panel/agent-chat/agent-chat.component.spec.ts index e2f70a6dc37..4fb82fd4de5 100644 --- a/frontend/src/app/workspace/component/agent/agent-panel/agent-chat/agent-chat.component.spec.ts +++ b/frontend/src/app/workspace/component/agent/agent-panel/agent-chat/agent-chat.component.spec.ts @@ -101,6 +101,9 @@ function makeStep(overrides: Partial = {}): ReActStep { }; } +/** What the workspace knows about the workflow it has open, apart from its content. */ +const OPEN_WORKFLOW = { wid: 42, name: "wf" }; + describe("AgentChatComponent", () => { let fixture: ComponentFixture; let component: AgentChatComponent; @@ -139,7 +142,7 @@ describe("AgentChatComponent", () => { // so the real service is required to render the system-info modal. NzModalService, { provide: AgentService, useValue: agentService }, - { provide: WorkflowActionService, useValue: { reloadWorkflow } }, + { provide: WorkflowActionService, useValue: { reloadWorkflow, getWorkflowMetadata: () => OPEN_WORKFLOW } }, { provide: NotificationService, useValue: notification }, { provide: WorkflowPersistService, useValue: persist }, ...commonTestProviders, @@ -341,6 +344,18 @@ describe("AgentChatComponent", () => { } as unknown as Workflow); expect(reloadWorkflow).toHaveBeenCalledTimes(2); }); + + it("keeps the open workflow's id and name when the agent's workflow carries only its content", () => { + // A step can reach the page before the workflow itself has been fetched, and then all there + // is to put on the stream is the content. Reloading that as it came wiped the workspace's + // workflow id and name, and the agent's own tab locked as "a different workflow". + createComponent(makeAgentInfo(), true); + const content = { operators: [{ operatorID: "op-9" }] }; + + agentService.workflowSubject.next({ content } as unknown as Workflow); + + expect(reloadWorkflow).toHaveBeenCalledWith({ ...OPEN_WORKFLOW, content }, false, false); + }); }); describe("sendMessage / onEnterPress", () => { diff --git a/frontend/src/app/workspace/component/agent/agent-panel/agent-chat/agent-chat.component.ts b/frontend/src/app/workspace/component/agent/agent-panel/agent-chat/agent-chat.component.ts index 867e21aa7ee..f7d21c84e83 100644 --- a/frontend/src/app/workspace/component/agent/agent-panel/agent-chat/agent-chat.component.ts +++ b/frontend/src/app/workspace/component/agent/agent-panel/agent-chat/agent-chat.component.ts @@ -284,7 +284,15 @@ export class AgentChatComponent implements OnInit, AfterViewChecked, OnDestroy, ) .subscribe(workflow => { if (workflow) { - this.workflowActionService.reloadWorkflow(workflow, false, false); + // The agent's stream can carry the workflow's content alone: a step that arrives before + // the workflow itself has been fetched has nothing else to send. The id, name and the + // rest are then the open workflow's own. Reloading without them made the workspace + // forget which workflow it had open, and this agent's tab locked as "a different one". + this.workflowActionService.reloadWorkflow( + { ...this.workflowActionService.getWorkflowMetadata(), ...workflow }, + false, + false + ); } }); } diff --git a/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.html b/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.html index a2154d8e96f..241731b5aef 100644 --- a/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.html +++ b/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.html @@ -72,10 +72,13 @@ AI Agents - + @@ -97,8 +100,7 @@ + [nzForceRender]="true"> @@ -131,8 +133,29 @@ + + +
+

+ This agent is working on a different workflow: {{ agent.delegate?.workflowName || "#" + workflowId }}. +

+ + Open that workflow + +
+
diff --git a/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.scss b/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.scss index 80c3ec719d0..50499947c4f 100644 --- a/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.scss +++ b/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.scss @@ -25,9 +25,12 @@ z-index: 3; } +// Placed against the viewport, as the property editor is. The host is as wide as the viewport +// but starts where the workspace does, to the right of the dashboard sidebar, so a box put at +// the host's right edge hangs off the screen by the sidebar's width. #agent-container { - position: absolute; - top: calc(-100% + 80px); + position: fixed; + top: 80px; right: 0; z-index: 3; background: white; @@ -136,6 +139,24 @@ } } +// Shown in place of the chat of an agent that works on another workflow. +.other-workflow-notice { + height: 100%; + display: flex; + flex-direction: column; + align-items: center; + justify-content: center; + gap: 16px; + padding: 20px; + text-align: center; + font-size: 14px; + color: #595959; + + p { + margin: 0; + } +} + .agent-tab-close { padding: 0 !important; width: 20px !important; diff --git a/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.spec.ts b/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.spec.ts index c732e899a6a..ae2d8a2c936 100644 --- a/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.spec.ts +++ b/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.spec.ts @@ -17,7 +17,7 @@ * under the License. */ -import { Component, EventEmitter, Input, Output } from "@angular/core"; +import { ANIMATION_MODULE_TYPE, Component, EventEmitter, Input, Output } from "@angular/core"; import { ComponentFixture, TestBed } from "@angular/core/testing"; import { HttpClientTestingModule } from "@angular/common/http/testing"; import { NoopAnimationsModule } from "@angular/platform-browser/animations"; @@ -125,7 +125,6 @@ describe("AgentPanelComponent", () => { providers: [ { provide: AgentService, useValue: agentService }, { provide: WorkflowActionService, useValue: workflowAction }, - { provide: NotificationService, useValue: notification }, ...commonTestProviders, ], }).compileComponents(); @@ -304,6 +303,22 @@ describe("AgentPanelComponent", () => { expect(component.width).toBe(0); expect(fixture.nativeElement.querySelector("#agent-docked-button")).toBeTruthy(); }); + + it("hides an agent's pane at once while docked, so it cannot stay over the registration tab", () => { + // These specs run with NoopAnimationsModule; the app does not. With pane animations on, + // ng-zorro hides an inactive pane only once its fade-out transition has ended, and none + // ends inside the docked panel's hidden body (nor in jsdom). The pane then stayed, + // invisible, over the registration tab and took every click. + TestBed.overrideProvider(ANIMATION_MODULE_TYPE, { useValue: "BrowserAnimations" }); + agentService.agentList = [makeAgent("a")]; + createComponent(); + expect((fixture.nativeElement.querySelector("#content") as HTMLElement).hidden).toBe(true); + + const panes = Array.from(fixture.nativeElement.querySelectorAll(".ant-tabs-tabpane")) as HTMLElement[]; + expect(panes.length).toBe(2); + expect(panes[0].classList.contains("ant-tabs-tabpane-active")).toBe(true); + expect(panes[1].classList.contains("ant-tabs-tabpane-hidden")).toBe(true); + }); }); describe("onAgentCreated", () => { @@ -395,18 +410,18 @@ describe("AgentPanelComponent", () => { expect(component.selectedTabIndex).toBe(2); }); - it("blocks switching to an agent bound to a different workflow and warns", () => { - agentService.agentList = [makeDelegateAgent("foreign", 99)]; + it("selects the tab of an agent bound to a different workflow without activating it", () => { + agentService.agentList = [makeAgent("local"), makeDelegateAgent("foreign", 99)]; createComponent(); + component.onTabSelectChange(1); // "local" active - component.onTabSelectChange(1); + component.onTabSelectChange(2); - expect(notification.warning).toHaveBeenCalledTimes(1); - const message = notification.warning.mock.calls[0][0] as string; - expect(message).toContain('Cannot switch to agent "Agent foreign"'); - expect(message).toContain("Open workflow #99"); - expect(agentService.activateAgent).not.toHaveBeenCalled(); - expect(component.selectedTabIndex).toBe(0); + // An active agent loads its workflow into the canvas, here over the open one. + expect(agentService.activateAgent).not.toHaveBeenCalledWith("foreign"); + expect(agentService.deactivateAgent).toHaveBeenCalledWith("local"); + expect(component.activeAgentId).toBeNull(); + expect(component.selectedTabIndex).toBe(2); }); it("allows switching when the agent's workflow matches the current one", () => { @@ -415,7 +430,6 @@ describe("AgentPanelComponent", () => { component.onTabSelectChange(1); - expect(notification.warning).not.toHaveBeenCalled(); expect(agentService.activateAgent).toHaveBeenCalledWith("bound"); expect(component.selectedTabIndex).toBe(1); }); @@ -478,6 +492,28 @@ describe("AgentPanelComponent", () => { expect(titles[0].classList.contains("workflow-mismatch")).toBe(false); expect(titles[1].classList.contains("workflow-mismatch")).toBe(true); }); + + it("gives an agent from another workflow a link to that workflow in place of a chat", () => { + agentService.agentList = [ + makeAgent("local"), + makeAgent("named", { delegate: { userInfo: USER_INFO, workflowId: 99, workflowName: "Orders" } }), + makeDelegateAgent("unnamed", 7), + ]; + createComponent(); + + const chats = fixture.debugElement.queryAll(By.directive(StubAgentChatComponent)); + expect(chats.map(chat => chat.componentInstance.agentInfo.id)).toEqual(["local"]); + + const notices = Array.from(fixture.nativeElement.querySelectorAll(".other-workflow-notice")) as HTMLElement[]; + expect(notices.map(notice => notice.querySelector("p")?.textContent?.trim())).toEqual([ + "This agent is working on a different workflow: Orders.", + "This agent is working on a different workflow: #7.", + ]); + expect(notices.map(notice => notice.querySelector("a")?.getAttribute("href"))).toEqual([ + "/user/workflow/99", + "/user/workflow/7", + ]); + }); }); describe("deleteAgent", () => { @@ -831,21 +867,23 @@ describe("AgentPanelComponent", () => { expect(chats().map(c => c.isActive)).toEqual([false, true]); }); - it("disables the tab of an agent bound to another workflow so a click cannot select it", () => { + it("clicking the tab of an agent bound to another workflow shows the link to it, not a chat", async () => { service.agentList = [makeAgent("local"), makeDelegateAgent("foreign", 99)]; createComponent(); - const headers = tabHeaders(); - expect(headers[1].classList.contains("ant-tabs-tab-disabled")).toBe(false); - expect(headers[2].classList.contains("ant-tabs-tab-disabled")).toBe(true); - - (headers[2].querySelector(".ant-tabs-tab-btn") as HTMLElement).click(); + (tabHeaders()[2].querySelector(".ant-tabs-tab-btn") as HTMLElement).click(); + fixture.detectChanges(); + await settle(); fixture.detectChanges(); - // Disabled by the template, so onTabSelectChange never runs and never warns. - expect(notification.warning).not.toHaveBeenCalled(); + expect(component.selectedTabIndex).toBe(2); + const selectedPane = fixture.nativeElement.querySelector(".ant-tabs-tabpane-active") as HTMLElement; + expect(selectedPane.querySelector("a")?.getAttribute("href")).toBe("/user/workflow/99"); + expect(selectedPane.querySelector("texera-agent-chat")).toBeNull(); + // The agent stays inactive, and the only chat is that of the agent on this workflow. expect(service.activateAgent).not.toHaveBeenCalled(); - expect(chats().map(c => c.isActive)).toEqual([false, false]); + expect(chats().map(c => c.agentInfo.id)).toEqual(["local"]); + expect(chats().map(c => c.isActive)).toEqual([false]); }); it("the close button on a tab header deletes that agent without selecting its tab", () => { diff --git a/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.ts b/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.ts index 3a5e1650171..fe617ebe557 100644 --- a/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.ts +++ b/frontend/src/app/workspace/component/agent/agent-panel/agent-panel.component.ts @@ -22,7 +22,7 @@ import { UntilDestroy, untilDestroyed } from "@ngneat/until-destroy"; import { NzResizeEvent, NzResizableDirective, NzResizeHandlesComponent } from "ng-zorro-antd/resizable"; import { AgentService, AgentInfo } from "../../../service/agent/agent.service"; import { WorkflowActionService } from "../../../service/workflow-graph/model/workflow-action.service"; -import { NotificationService } from "../../../../common/service/notification/notification.service"; +import { workspaceCanvasUrl } from "../../../../app-routing.constant"; import { NgIf, NgFor } from "@angular/common"; import { NzSpaceCompactItemDirective } from "ng-zorro-antd/space"; import { NzButtonComponent } from "ng-zorro-antd/button"; @@ -66,6 +66,7 @@ import { AgentChatComponent } from "./agent-chat/agent-chat.component"; }) export class AgentPanelComponent implements OnInit, OnDestroy, OnChanges { protected readonly window = window; + protected readonly workspaceCanvasUrl = workspaceCanvasUrl; private static readonly MIN_PANEL_WIDTH = 400; private static readonly MIN_PANEL_HEIGHT = 450; @@ -91,8 +92,7 @@ export class AgentPanelComponent implements OnInit, OnDestroy, OnChanges { constructor( private agentService: AgentService, - private workflowActionService: WorkflowActionService, - private notificationService: NotificationService + private workflowActionService: WorkflowActionService ) {} ngOnInit(): void { @@ -210,7 +210,7 @@ export class AgentPanelComponent implements OnInit, OnDestroy, OnChanges { } /** - * Handle tab selection change - validates workflow compatibility before switching + * Handle tab selection change - activates the tab's agent only on the workflow it works on */ public onTabSelectChange(index: number): void { // Tab 0 is registration - always allow @@ -227,19 +227,13 @@ export class AgentPanelComponent implements OnInit, OnDestroy, OnChanges { } const agent = this.agents[agentIndex]; - const agentWorkflowId = agent.delegate?.workflowId; - const currentWorkflowId = this.workflowActionService.getWorkflowMetadata().wid; - // If agent has a workflow ID, check if it matches the current workflow - if (agentWorkflowId !== undefined && agentWorkflowId !== 0) { - if (currentWorkflowId !== agentWorkflowId) { - // Block switching - workflow mismatch - this.notificationService.warning( - `Cannot switch to agent "${agent.name}": It's working on a different workflow. ` + - `Open workflow #${agentWorkflowId} to interact with this agent.` - ); - return; - } + // An agent that works on another workflow is shown, not activated: its tab holds a link to + // that workflow in place of the chat, and an active agent loads its workflow into the canvas. + if (!this.canSwitchToAgent(agent)) { + this.deactivateCurrentAgent(); + this.selectedTabIndex = index; + return; } // Workflow matches or agent has no workflow - allow switch diff --git a/frontend/src/app/workspace/component/agent/agent-panel/agent-registration/agent-registration.component.scss b/frontend/src/app/workspace/component/agent/agent-panel/agent-registration/agent-registration.component.scss index 639dcb78eb0..541a7e46169 100644 --- a/frontend/src/app/workspace/component/agent/agent-panel/agent-registration/agent-registration.component.scss +++ b/frontend/src/app/workspace/component/agent/agent-panel/agent-registration/agent-registration.component.scss @@ -137,9 +137,16 @@ } } +// The computing-unit notice goes above the button at full width. Side by side, the button's +// minimum width left the notice a sliver to wrap its one sentence into. .action-buttons { display: flex; - justify-content: center; + flex-direction: column; + align-items: center; + + nz-alert { + align-self: stretch; + } button { min-width: 200px;