Skip to content

armgit / qm

Public Imported from GitHub
0

Multiplayer agent harness for work.

Reuse OAuth connection cards for Composio links (#1210)

Joshua Francea2a647cae68f
diff --git a/plugins/web-ui/src/chat.ts b/plugins/web-ui/src/chat.ts
index 2e931306d..3c05e0a68 100644
--- a/plugins/web-ui/src/chat.ts
+++ b/plugins/web-ui/src/chat.ts
@@ -1694,10 +1694,11 @@ export function createChatSurface(
}
function connectorWidget(link: ConnectorLink): TemplateResult {
+ const composio = link.provider === "composio";
const name =
- CONNECTOR_NAMES[link.provider] ??
+ (composio ? "your account" : CONNECTOR_NAMES[link.provider]) ??
(link.provider ? link.provider[0]!.toUpperCase() + link.provider.slice(1) : "your account");
- if (link.provider && connectedConnectors.has(link.provider)) {
+ if (!composio && link.provider && connectedConnectors.has(link.provider)) {
return html`<div class="connector-widget connected" role="status">
<span class="connector-widget-icon">${icon(Check, 18)}</span>
<span class="connector-widget-text"
@@ -1705,10 +1706,16 @@ export function createChatSurface(
>
</div>`;
}
- return html`<a class="connector-widget" href=${withReturnTo(link.url)} target="_blank" rel="noreferrer">
+ return html`<a
+ class="connector-widget"
+ href=${composio ? link.url : withReturnTo(link.url)}
+ target="_blank"
+ rel="noreferrer"
+ >
<span class="connector-widget-icon">${icon(Plug, 18)}</span>
<span class="connector-widget-text"
- ><strong>Connect ${name}</strong><small>Authorize access in a new tab</small></span
+ ><strong>${(composio && link.label) || `Connect ${name}`}</strong
+ ><small>${composio ? "Authorize access via Composio" : "Authorize access in a new tab"}</small></span
>
${icon(ChevronRight, 16)}
</a>`;
@@ -1765,7 +1772,7 @@ export function createChatSurface(
if (chunk.type === "text") {
const shown = assistantDisplayText(chunk.text);
const links = shown.trim() ? connectorLinksIn(shown, location.origin) : [];
- const body = links.length ? stripConnectorLinks(shown) : shown;
+ const body = links.length ? stripConnectorLinks(shown, links) : shown;
if (body.trim())
parts.push(
html`<div class="streaming-text ${isStreaming ? "live-stream" : ""}" dir="auto">${markdown(body)}</div>`,
diff --git a/plugins/web-ui/src/connector-link.ts b/plugins/web-ui/src/connector-link.ts
index a44236321..529317d0b 100644
--- a/plugins/web-ui/src/connector-link.ts
+++ b/plugins/web-ui/src/connector-link.ts
@@ -1,5 +1,4 @@
-const CONNECTOR_REDEEM_RE =
- /https?:\/\/[^\s)\]]+\/(?:connect\/redeem|v1\/connectors\/oauth\/consent\/redeem)\/[^\s)\]]+/gi;
+const LINK_RE = /https?:\/\/[^\s<>()[\]]+/gi;
export const CONNECTOR_NAMES: Record<string, string> = {
google: "Google Workspace",
@@ -14,11 +13,17 @@ export const CONNECTOR_NAMES: Record<string, string> = {
export interface ConnectorLink {
provider: string;
url: string;
+ label?: string;
}
export function connectorLinksIn(text: string, trustedOrigin?: string): ConnectorLink[] {
+ const labels = new Map<string, string>();
+ for (const m of text.matchAll(/\[([^\]\n]*)\]\(\s*<?(https?:\/\/[^\s)>]+)>?\s*\)/gi)) {
+ const label = m[1]!.trim();
+ if (label && !labels.has(m[2]!)) labels.set(m[2]!, label);
+ }
const out: ConnectorLink[] = [];
- for (const m of text.matchAll(CONNECTOR_REDEEM_RE)) {
+ for (const m of text.matchAll(LINK_RE)) {
const url = m[0].replace(/[*_]+$/, "");
let parsed: URL;
try {
@@ -26,8 +31,14 @@ export function connectorLinksIn(text: string, trustedOrigin?: string): Connecto
} catch {
continue;
}
- if (trustedOrigin && parsed.origin !== trustedOrigin) continue;
if (parsed.username || parsed.password) continue;
+ if (parsed.origin === "https://connect.composio.dev" && /^\/link\/lk_[A-Za-z0-9_-]+$/.test(parsed.pathname)) {
+ const label = labels.get(url);
+ if (!out.some((l) => l.url === url)) out.push({ provider: "composio", url, ...(label ? { label } : {}) });
+ continue;
+ }
+ if (trustedOrigin && parsed.origin !== trustedOrigin) continue;
+ if (!/^\/(?:connect\/redeem|v1\/connectors\/oauth\/consent\/redeem)\/[^/]+$/.test(parsed.pathname)) continue;
const provider = parsed.searchParams.get("p") ?? "";
if (!CONNECTOR_NAMES[provider]) continue;
if (!out.some((l) => l.url === url)) out.push({ provider, url });
@@ -37,16 +48,12 @@ export function connectorLinksIn(text: string, trustedOrigin?: string): Connecto
const EMPH = String.raw`(?:\*\*|\*|__|_)`;
-export function stripConnectorLinks(text: string): string {
- return text
- .replace(
- new RegExp(
- `${EMPH}?\\[[^\\]]*\\]\\(\\s*<?https?:\\/\\/[^\\s)]+\\/(?:connect\\/redeem|v1\\/connectors\\/oauth\\/consent\\/redeem)\\/[^\\s)>]+>?\\s*\\)${EMPH}?`,
- "gi",
- ),
- "",
- )
- .replace(new RegExp(`${EMPH}?${CONNECTOR_REDEEM_RE.source}${EMPH}?`, "gi"), "")
- .replace(/[ \t]+$/gm, "")
- .trim();
+export function stripConnectorLinks(text: string, links = connectorLinksIn(text)): string {
+ for (const { url } of links) {
+ const escaped = url.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
+ text = text
+ .replace(new RegExp(`${EMPH}?\\[[^\\]]*\\]\\(\\s*<?${escaped}>?\\s*\\)${EMPH}?`, "g"), "")
+ .replace(new RegExp(`${EMPH}?<?${escaped}(?=$|[\\s<>()[\\]*_])>?${EMPH}?`, "g"), "");
+ }
+ return text.replace(/[ \t]+$/gm, "").trim();
}
diff --git a/plugins/web-ui/src/shell.css b/plugins/web-ui/src/shell.css
index 224394fce..d338359d8 100644
--- a/plugins/web-ui/src/shell.css
+++ b/plugins/web-ui/src/shell.css
@@ -4510,6 +4510,7 @@ a.chat-row-open {
line-height: 1.3;
}
.connector-widget-text strong {
+ overflow-wrap: anywhere;
font-size: 14px;
font-weight: 600;
}
diff --git a/plugins/web-ui/test/connector-link-strip.test.ts b/plugins/web-ui/test/connector-link-strip.test.ts
index 3f7413512..4790ad870 100644
--- a/plugins/web-ui/test/connector-link-strip.test.ts
+++ b/plugins/web-ui/test/connector-link-strip.test.ts
@@ -65,3 +65,88 @@ test("a managed-auth login link is left untouched — it must render as an ordin
const text = `Here's your secure sign-in link:\n\n${AUTH_URL}\n\nTell me once you're through.`;
assert.equal(stripConnectorLinks(text), text);
});
+
+const COMPOSIO_URL = "https://connect.composio.dev/link/lk_example123";
+
+test("Composio hosted consent links reuse the connector widget", () => {
+ assert.deepEqual(connectorLinksIn(COMPOSIO_URL, "https://agent.example.com"), [
+ { provider: "composio", url: COMPOSIO_URL },
+ ]);
+});
+
+test("Composio supports bare, markdown, autolink and emphasized links", () => {
+ for (const text of [
+ COMPOSIO_URL,
+ `**${COMPOSIO_URL}**`,
+ `<${COMPOSIO_URL}>`,
+ `[Connect Gmail](${COMPOSIO_URL})`,
+ `**[Connect Gmail](<${COMPOSIO_URL}>)**`,
+ ]) {
+ const links = connectorLinksIn(text, "https://agent.example.com");
+ assert.deepEqual(links, [
+ {
+ provider: "composio",
+ url: COMPOSIO_URL,
+ ...(text.includes("[Connect Gmail]") ? { label: "Connect Gmail" } : {}),
+ },
+ ]);
+ assert.equal(stripConnectorLinks(text, links), "");
+ }
+});
+
+test("Composio URLs retain query parameters and deduplicate", () => {
+ const url = `${COMPOSIO_URL}?callback_url=https%3A%2F%2Fexample.com%2Fdone`;
+ assert.deepEqual(connectorLinksIn(`${url}\n${url}`), [{ provider: "composio", url }]);
+});
+
+test("only HTTPS Composio hosted link URLs become cards", () => {
+ for (const url of [
+ "http://connect.composio.dev/link/lk_example123",
+ "https://connect.composio.dev.evil.example/link/lk_example123",
+ "https://evil.example/link/lk_example123",
+ "https://[email protected]/link/lk_example123",
+ "https://connect.composio.dev:444/link/lk_example123",
+ "https://connect.composio.dev/other/lk_example123",
+ "https://connect.composio.dev/link/",
+ "https://connect.composio.dev/link/lk_example123/extra",
+ ]) {
+ assert.deepEqual(connectorLinksIn(url, "https://agent.example.com"), [], url);
+ assert.equal(stripConnectorLinks(url), url);
+ }
+});
+
+test("stripping removes only links actually rendered as cards", () => {
+ const foreign = "https://evil.example/connect/redeem/x?p=google";
+ const text = `${COMPOSIO_URL}\n${foreign}`;
+ assert.equal(stripConnectorLinks(text, connectorLinksIn(text, "https://agent.example.com")), foreign);
+});
+
+test("stripping a card does not remove the prefix of an unrecognized URL", () => {
+ const other = `${URL}unknown`;
+ const text = `${URL}\n${other}`;
+ assert.equal(stripConnectorLinks(text, connectorLinksIn(text, "https://agent.example.com")), other);
+});
+
+test("multiple Composio cards retain their own arbitrary link labels and order", () => {
+ const labels = ["Connect Calendar", "Connect Gmail", "Connect Google Drive", "Authorize Paper Lantern 🌙"];
+ const text = labels.map((label, i) => `[${label}](https://connect.composio.dev/link/lk_test${i})`).join("\n");
+ assert.deepEqual(
+ connectorLinksIn(text),
+ labels.map((label, i) => ({
+ provider: "composio",
+ url: `https://connect.composio.dev/link/lk_test${i}`,
+ label,
+ })),
+ );
+ assert.equal(stripConnectorLinks(text), "");
+});
+
+test("a labeled duplicate gives an earlier bare URL its label without another card", () => {
+ const text = `${COMPOSIO_URL}\n[ Authorize anything ](${COMPOSIO_URL})\n[Other label](${COMPOSIO_URL})`;
+ assert.deepEqual(connectorLinksIn(text), [{ provider: "composio", url: COMPOSIO_URL, label: "Authorize anything" }]);
+});
+
+test("empty labels retain the generic fallback and native names cannot be relabeled", () => {
+ assert.deepEqual(connectorLinksIn(`[ ](${COMPOSIO_URL})`), [{ provider: "composio", url: COMPOSIO_URL }]);
+ assert.deepEqual(connectorLinksIn(`[Something else](${URL})`), [{ provider: "google", url: URL }]);
+});