This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/main/pr-9426-716ab3c1e834350332c83753357440ec7e27c39d in repository https://gitbox.apache.org/repos/asf/texera.git
commit 44f7abf88309800c3dab8ba1b562d2165dda91f4 Author: Eugene Gu <[email protected]> AuthorDate: Sun Nov 15 18:01:12 2026 +0000 feat(frontend): open agent chat links in a new tab (#9426) ### What changes were proposed in this PR? A link in an agent chat message opened in the workflow tab, so one click replaced the editor with the linked page. The chat renders each message with `<markdown>`, and the rendered `<a>` had no `target`. Links in a chat message now open in a new tab. - `agent-chat.component.html` binds the `ready` output of the message's `<markdown>` to a new `openLinksInNewTab` handler. `ready` fires after every render, so a message that is rendered again with new text is handled again. - The handler sets `target="_blank"` and `rel="noopener noreferrer"` on every link that resolves to an http or https address. That covers links to other sites, bare URLs that markdown turns into links, and also anchors, links without a protocol, absolute paths and full links to the app. A rendered link is not a router link and the app has `<base href="/">`, so each of those would load another page of the app in the editor's tab (`#summary` resolves to the app root). - Links with another scheme, such as `mailto:`, and links whose address cannot be parsed are left as they were. - A `target` or `rel` the link already carried is replaced, so a message cannot keep `rel="opener"`. The change is local to chat messages. The other markdown views (the Form View instructions, the dataset file preview, the dataset, model and hub workflow descriptions, and the system prompt tab of the agent's info dialog) render links the same way and are not changed here. They are a possible follow-up. Before (`main`). The agent's answer holds a link to a documentation page. Clicking it loads the documentation in the same tab, so the editor and the conversation are gone. | Before the click | After the click, the same tab | |---|---| | <img width="1510" height="951" alt="links-1-main-before-click" src="https://github.com/user-attachments/assets/3ff9ec8d-6b27-465c-a2bf-470685135eb6" /> | <img width="1512" height="950" alt="links-2-main-after-click-same-tab-is-docs" src="https://github.com/user-attachments/assets/6625ca15-16e1-46f5-9abf-63b456a0b4a6" />| After (this PR). The same click on the same conversation opens the documentation in a second tab. The editor's tab stays as it was. <img width="1507" height="852" alt="links-3-this-pr-after-click-docs-in-new-tab" src="https://github.com/user-attachments/assets/c188be2b-563b-478d-a720-b4bd9acbe93a" /> ### Any related issues, documentation, discussions? Closes #9422. ### How was this PR tested? `cd frontend && NODE_OPTIONS="--no-experimental-webstorage" npx ng test --watch=false --include src/app/workspace/component/agent/agent-panel/agent-chat/agent-chat.component.spec.ts`: 67 passed (52 before this PR). The 15 new tests render real messages through `<markdown>` and read the resulting links. - A link to another site over https and over http, a protocol-relative link and a bare URL open in a new tab with `rel="noopener noreferrer"`. - An anchor, a link with an empty address, a link without a protocol, an absolute path, a full link to the app and a link to the same host on another port open in a new tab as well. - A `mailto:` link, a `javascript:` link (which Angular has already rewritten to `unsafe:javascript:`) and a link whose address cannot be parsed are untouched. - A `target` and `rel` already on a link are replaced. - A message without links renders as before. Several links in one message are each judged on their own, and so are links in every message of a conversation and in a message the user typed. - A message rendered again gets its new link handled, and a link that becomes a `mailto:` link loses the new-tab marks. Also `agent-panel.component.spec.ts` (49 passed), which renders the chat inside the panel, `npx prettier --check` and `npx eslint` on the three touched files, and `npx tsc --noEmit -p src/tsconfig.app.json`. Four mutants were each caught by a test: an unparsable address treated as a link, every scheme treated as a web link, a link with an empty address skipped, and `rel` extended instead of replaced. Live check on a local stack with the frontend served from this branch. One typed message held an anchor link, an absolute path, a `mailto:` link and a bare `http://localhost:8080` URL, and the agent's answer held a link to a documentation page. In the rendered chat every link except the `mailto:` one had `target="_blank"` and `rel="noopener noreferrer"`. Clicking the anchor link left the workflow editor and the conversation in place, where before the same click loaded the app root in the editor's tab. ### Was this PR authored or co-authored using generative AI tooling? Yes. Generated-by: Claude Code (claude-fable-5-2). Co-authored with Claude; the author reviewed the change before submission. --- .../agent-chat/agent-chat.component.html | 7 +- .../agent-chat/agent-chat.component.spec.ts | 191 +++++++++++++++++++++ .../agent-panel/agent-chat/agent-chat.component.ts | 20 +++ 3 files changed, 216 insertions(+), 2 deletions(-) diff --git a/frontend/src/app/workspace/component/agent/agent-panel/agent-chat/agent-chat.component.html b/frontend/src/app/workspace/component/agent/agent-panel/agent-chat/agent-chat.component.html index 62de7296be..bf28945a7b 100644 --- a/frontend/src/app/workspace/component/agent/agent-panel/agent-chat/agent-chat.component.html +++ b/frontend/src/app/workspace/component/agent/agent-panel/agent-chat/agent-chat.component.html @@ -115,8 +115,11 @@ style="position: relative"> <div *ngIf="response.content" - class="message-content"> - <markdown [data]="response.content"></markdown> + class="message-content" + #messageText> + <markdown + [data]="response.content" + (ready)="openLinksInNewTab(messageText)"></markdown> </div> <div *ngIf="response.toolCalls && response.toolCalls.length > 0" 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 ad78b7eda9..e2f70a6dc3 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 @@ -712,6 +712,197 @@ describe("AgentChatComponent", () => { expect(messages[1].querySelector("button")).toBeTruthy(); }); + describe("links in a message", () => { + // <markdown> fills its element after an awaited parse(), so a macrotask has to pass + // before the rendered links exist. + const flushMarkdown = async (): Promise<void> => { + fixture.detectChanges(); + await new Promise(resolve => setTimeout(resolve, 0)); + fixture.detectChanges(); + }; + + const renderMessage = async (content: string, role: "user" | "agent" = "agent"): Promise<void> => { + createComponent(); + agentService.stepsSubject.next([makeStep({ messageId: "m1", stepId: 1, role, content, isBegin: true })]); + await flushMarkdown(); + }; + + const links = (): HTMLAnchorElement[] => + Array.from(fixture.nativeElement.querySelectorAll(".messages-container a") as NodeListOf<HTMLAnchorElement>); + + const linkNamed = (text: string): HTMLAnchorElement => { + const found = links().find(a => a.textContent === text); + expect(found, `expected a rendered link with the text "${text}"`).toBeTruthy(); + return found!; + }; + + const opensInNewTab = (a: HTMLAnchorElement): boolean => + a.getAttribute("target") === "_blank" && a.getAttribute("rel") === "noopener noreferrer"; + + const untouched = (a: HTMLAnchorElement): boolean => !a.hasAttribute("target") && !a.hasAttribute("rel"); + + it("opens a web link in a new tab without handing it the opener", async () => { + await renderMessage( + "See the [Limit docs](https://texera.apache.org/docs/latest/reference/operators/data-cleaning/limit/) and [plain http](http://example.com/a?b=1#c)." + ); + + const docs = linkNamed("Limit docs"); + expect(docs.getAttribute("href")).toBe( + "https://texera.apache.org/docs/latest/reference/operators/data-cleaning/limit/" + ); + expect(opensInNewTab(docs)).toBe(true); + expect(opensInNewTab(linkNamed("plain http"))).toBe(true); + }); + + it("opens a protocol-relative link and a bare URL in the text in a new tab", async () => { + await renderMessage("Try [cdn](//cdn.example.com/lib.js) or https://texera.apache.org/docs/latest/ directly."); + + expect(opensInNewTab(linkNamed("cdn"))).toBe(true); + expect(opensInNewTab(linkNamed("https://texera.apache.org/docs/latest/"))).toBe(true); + }); + + // A rendered link is not a router link and the app has <base href="/">, so each of these + // would load another page of the app in the editor's tab. + it("opens an anchor link in a new tab, since it resolves to the app root", async () => { + await renderMessage("Jump to [the summary](#summary)."); + + const anchor = linkNamed("the summary"); + expect(anchor.getAttribute("href")).toBe("#summary"); + expect(opensInNewTab(anchor)).toBe(true); + }); + + it("opens a link without a protocol and an absolute path in a new tab", async () => { + await renderMessage("Open [the docs](texera.apache.org/docs) or [your datasets](/dashboard/user/dataset)."); + + expect(opensInNewTab(linkNamed("the docs"))).toBe(true); + expect(opensInNewTab(linkNamed("your datasets"))).toBe(true); + }); + + it("opens a full link to this app, and to this host on another port, in a new tab", async () => { + await renderMessage( + `Back to [this workflow](${window.location.origin}/user/workflow/9) or [the server](http://${window.location.hostname}:8080).` + ); + + expect(opensInNewTab(linkNamed("this workflow"))).toBe(true); + expect(opensInNewTab(linkNamed("the server"))).toBe(true); + }); + + it("opens a link with an empty address in a new tab, since it resolves to the app root", async () => { + await renderMessage("An [empty]() link."); + + const empty = linkNamed("empty"); + expect(empty.getAttribute("href")).toBe(""); + expect(opensInNewTab(empty)).toBe(true); + }); + + it("leaves a link whose address cannot be parsed as it is", async () => { + await renderMessage("A [broken](http://) link."); + + expect(untouched(linkNamed("broken"))).toBe(true); + }); + + it("replaces the target and rel a link already carries", async () => { + await renderMessage('Raw <a href="https://example.org/" target="_self" rel="opener">html link</a>.'); + + const raw = linkNamed("html link"); + expect(raw.getAttribute("target")).toBe("_blank"); + expect(raw.getAttribute("rel")).toBe("noopener noreferrer"); + }); + + it("leaves a link that is not http as it is", async () => { + await renderMessage("Write to [the list](mailto:[email protected])."); + + const mail = linkNamed("the list"); + expect(mail.getAttribute("href")).toBe("mailto:[email protected]"); + expect(untouched(mail)).toBe(true); + }); + + it("leaves a javascript link as it is", async () => { + // Angular warns when it defuses the address. + vi.spyOn(console, "warn").mockImplementation(() => {}); + await renderMessage("Do not [run this](javascript:void(0))."); + + const script = linkNamed("run this"); + expect(script.getAttribute("href")).toBe("unsafe:javascript:void(0)"); + expect(untouched(script)).toBe(true); + }); + + it("renders a message without links unchanged", async () => { + await renderMessage("No links here, only **bold** text."); + + expect(links()).toEqual([]); + expect(fixture.nativeElement.querySelector(".messages-container strong")?.textContent).toBe("bold"); + }); + + it("handles each link of a message on its own", async () => { + await renderMessage( + "[out](https://example.org/) then [mail](mailto:[email protected]) then [out again](https://example.net/x)" + ); + + expect(links().map(a => [a.textContent, opensInNewTab(a)])).toEqual([ + ["out", true], + ["mail", false], + ["out again", true], + ]); + }); + + it("applies to a link the user typed as well", async () => { + await renderMessage("what is https://texera.apache.org/docs/latest/ about?", "user"); + + expect(opensInNewTab(linkNamed("https://texera.apache.org/docs/latest/"))).toBe(true); + }); + + it("handles the links again when a message is rendered again with new text", async () => { + await renderMessage("Looking it up."); + expect(links()).toEqual([]); + + // The same step arrives again with more text. + agentService.stepsSubject.next([ + makeStep({ + messageId: "m1", + stepId: 1, + role: "agent", + content: "Looking it up. Found the [guide](https://texera.apache.org/docs/latest/tutorials/).", + isBegin: true, + }), + ]); + await flushMarkdown(); + expect(opensInNewTab(linkNamed("guide"))).toBe(true); + + // A later render turns the link into a mail link: the new-tab marks must not stick. + agentService.stepsSubject.next([ + makeStep({ + messageId: "m1", + stepId: 1, + role: "agent", + content: "Ask [guide](mailto:[email protected]).", + isBegin: true, + }), + ]); + await flushMarkdown(); + expect(untouched(linkNamed("guide"))).toBe(true); + }); + + it("handles the links of every message in the conversation", async () => { + createComponent(); + agentService.stepsSubject.next([ + makeStep({ messageId: "m1", stepId: 0, role: "user", content: "where are the docs?" }), + makeStep({ + messageId: "m1", + stepId: 1, + role: "agent", + content: "[first](https://a.example/)", + isBegin: true, + }), + makeStep({ messageId: "m1", stepId: 2, role: "agent", content: "[second](https://b.example/)", isEnd: true }), + ]); + await flushMarkdown(); + + expect(opensInNewTab(linkNamed("first"))).toBe(true); + expect(opensInNewTab(linkNamed("second"))).toBe(true); + }); + }); + it("pluralises the tool-call summary and opens the details modal from the hover button", () => { createComponent(); // A pure tool-call step carries no text, so nothing here waits on <markdown>. 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 536111d50c..867e21aa7e 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 @@ -60,6 +60,16 @@ import { NzInputNumberComponent } from "ng-zorro-antd/input-number"; import { NzTagComponent } from "ng-zorro-antd/tag"; import { NzSwitchComponent } from "ng-zorro-antd/switch"; +/** True for a link the browser would load as a page: one that resolves to an http or https address. */ +function isWebLink(href: string): boolean { + try { + const url = new URL(href, document.baseURI); + return url.protocol === "http:" || url.protocol === "https:"; + } catch { + return false; + } +} + @UntilDestroy() @Component({ selector: "texera-agent-chat", @@ -305,6 +315,16 @@ export class AgentChatComponent implements OnInit, AfterViewChecked, OnDestroy, } } + /** Makes the web links of a rendered message open in a new tab, so a click does not replace the editor. */ + public openLinksInNewTab(message: HTMLElement): void { + message.querySelectorAll<HTMLAnchorElement>("a[href]").forEach(link => { + if (isWebLink(link.getAttribute("href") ?? "")) { + link.target = "_blank"; + link.rel = "noopener noreferrer"; + } + }); + } + public setHoveredMessage(index: number | null): void { // When unhovered (null), automatically revert to latest step if (index === null && this.visibleSteps.length > 0) {
