Let a connector given to a workspace reach every account connected for it

A workspace's connector gave every member its tools but no account, so a
member with no sign-in of their own was told nothing was connected while
the super admin's account sat unused. Account reach now follows the
workspace grant, whoever connected the account, and the run names them.

The Composio listing also filtered on `user_id`, which it ignores, so one
person's sync adopted the whole project's accounts: it takes `user_ids`.
This commit is contained in:
2026-11-22 18:04:48 +01:00
parent 3f551e7352
commit 51747c069f
11 changed files with 79 additions and 22 deletions
+1 -1
View File
File diff suppressed because one or more lines are too long
@@ -2,6 +2,7 @@ import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_CONNECTOR_ACCOUNTS, BUN_SQLITE_COBBLE_CONNECTORS } from "@/db/types/db";
import type { BotConnectorEntry, ConnectorAccountRecord, ConnectorRecord } from "@/src/types";
import botConnectorsSql from "@/src/utils/bot-connectors-sql";
import connectorAccountReachSql from "@/src/utils/connector-account-reach-sql";
import attachConnectorAccountScopes from "./attach-connector-account-scopes";
import parseConnectorAccountRow from "./parse-connector-account-row";
import parseConnectorRow from "./parse-connector-row";
@@ -18,10 +19,11 @@ type ConnectorRow = BUN_SQLITE_COBBLE_CONNECTORS & { connection_name?: string };
/**
* The connectors a conversation's bot can use for this user, with the
* accounts that reach there: this user's own (whatever their status, so a
* pending sign-in still shows), and any connected account given to this
* bot, this workspace or every bot, named with its owner. Each app says
* where it comes from: the install, this workspace, this user's own
* attachment, or an account given here.
* pending sign-in still shows), every connected account of a connector given
* to this workspace, and any connected account given to this bot, this
* workspace or every bot, named with its owner. Each app says where it comes
* from: the install, this workspace, this user's own attachment, or an
* account given here.
*/
export default async function grabBotConnectors({
user_id,
@@ -44,6 +46,7 @@ export default async function grabBotConnectors({
const connection_names = new Map(found.map((row) => [Number(row.id), row.connection_name || ""]));
const ids = connectors.map((connector) => connector.id);
const placeholders = ids.map(() => "?").join(", ");
const workspace_reach = connectorAccountReachSql({ workspace_id });
const [account_res, workspace_res, bot_res] = await Promise.all([
BunSQLite.sql<AccountRow>({
@@ -57,13 +60,18 @@ export default async function grabBotConnectors({
a.all_bots = 1
OR EXISTS (SELECT 1 FROM connector_account_bots ab WHERE ab.account_id = a.id AND ab.bot_id = ?)
${workspace_id ? `OR EXISTS (SELECT 1 FROM connector_account_workspaces aw WHERE aw.account_id = a.id AND aw.workspace_id = ?)` : ""}
${workspace_reach.sql}
)
)
)
ORDER BY a.id ASC`,
values: workspace_id
? [...ids, Number(user_id), Number(bot_id), Number(workspace_id)]
: [...ids, Number(user_id), Number(bot_id)],
values: [
...ids,
Number(user_id),
Number(bot_id),
...(workspace_id ? [Number(workspace_id)] : []),
...workspace_reach.values,
],
}),
BunSQLite.sql<{ connector_id: number }>({
sql: `SELECT connector_id FROM workspace_connectors WHERE connector_id IN (${placeholders})`,
@@ -2,6 +2,7 @@ import BunSQLite from "@moduletrace/bun-sqlite";
import type { BUN_SQLITE_COBBLE_CONNECTOR_ACCOUNTS, BUN_SQLITE_COBBLE_CONNECTORS } from "@/db/types/db";
import type { ConnectorAccountRecord, ConnectorRecord } from "@/src/types";
import botConnectorsSql from "@/src/utils/bot-connectors-sql";
import connectorAccountReachSql from "@/src/utils/connector-account-reach-sql";
import attachConnectorAccountScopes from "./attach-connector-account-scopes";
import parseConnectorAccountRow from "./parse-connector-account-row";
import parseConnectorRow from "./parse-connector-row";
@@ -19,9 +20,10 @@ type Params = {
export type RunConnector = {
connector: ConnectorRecord;
/**
* The accounts the run may use for it: the user's own, and every one
* given to this bot, this workspace or every bot. Empty means nothing
* to call with: the card asks them to connect one.
* The accounts the run may use for it: the user's own, every connected
* account of a connector given to this workspace, and every one given to
* this bot, this workspace or every bot. Empty means nothing to call
* with: the card asks them to connect one.
*/
accounts: ConnectorAccountRecord[];
};
@@ -31,9 +33,9 @@ type AccountRow = BUN_SQLITE_COBBLE_CONNECTOR_ACCOUNTS & { owner_name?: string }
/**
* The connectors one run reaches (`botConnectorsSql`) with the accounts it
* may act through: its own user's, never another person's unless that
* account was given to this bot, this workspace or every bot. A connector
* reaches a run without any account; it only becomes callable once there is
* one.
* account was given to this bot, this workspace or every bot, or its app was
* given to this workspace. A connector reaches a run without any account; it
* only becomes callable once there is one.
*/
export default async function grabRunConnectors({ user_id, bot_id, workspace_id }: Params): Promise<RunConnector[]> {
const found =
@@ -47,6 +49,7 @@ export default async function grabRunConnectors({ user_id, bot_id, workspace_id
const connectors = found.map((row) => parseConnectorRow({ row }));
const ids = connectors.map((connector) => connector.id);
const workspace_reach = connectorAccountReachSql({ workspace_id });
const rows =
(
await BunSQLite.sql<AccountRow>({
@@ -60,9 +63,10 @@ export default async function grabRunConnectors({ user_id, bot_id, workspace_id
OR a.all_bots = 1
OR EXISTS (SELECT 1 FROM connector_account_bots b WHERE b.account_id = a.id AND b.bot_id = ?)
OR EXISTS (SELECT 1 FROM connector_account_workspaces w WHERE w.account_id = a.id AND w.workspace_id = ?)
${workspace_reach.sql}
)
ORDER BY a.id ASC`,
values: [...ids, Number(user_id), Number(bot_id), Number(workspace_id || 0)],
values: [...ids, Number(user_id), Number(bot_id), Number(workspace_id || 0), ...workspace_reach.values],
})
).payload || [];
@@ -271,6 +271,8 @@ describe("composioConnectorProvider", () => {
{ provider_account_id: "ca_2", status: "expired", label: undefined },
]);
expect(calls[0]!.url.searchParams.get("toolkit_slugs")).toBe("gmail");
expect(calls[0]!.url.searchParams.get("user_ids")).toBe("cobble-user-4");
expect(calls[0]!.url.searchParams.get("user_id")).toBeNull();
});
test("executes one tool with the account named in the call", async () => {
+5 -3
View File
@@ -330,9 +330,11 @@ export default function composioConnectorProvider({ key, base_url, fetchImpl }:
async accounts({ app, user_id }) {
const items = await listAllPages<any>({
path: "/connected_accounts",
// The listing filters on the plural name only: `toolkit_slug`
// is ignored and answers with the whole project's accounts.
query: { user_id: `cobble-user-${user_id}`, toolkit_slugs: app },
// The listing filters on the plural names only: `toolkit_slug`
// and `user_id` are ignored and answer with the whole
// project's accounts, which is how one user's sync took over
// another's account.
query: { user_ids: `cobble-user-${user_id}`, toolkit_slugs: app },
what: `the ${app} accounts`,
});
@@ -107,7 +107,7 @@ export default function ConnectorAccessModal({
setOpen={(next) => !next && onCancel()}
title={`Who gets ${connector.name}`}
description={
"A bot picked here is yours: it reaches the connector only in your runs there. A workspace reaches everyone working in it; every bot reaches the whole install."
"A bot picked here is yours: it reaches the connector only in your runs there, with your own accounts. A workspace reaches everyone working in it, with every account connected for this connector. Every bot reaches the whole install, each user with their own accounts."
}
>
<Form
@@ -80,7 +80,7 @@ export default function ConnectorConnectionModal({ connection, onSaved, onCancel
description={
connection
? "Renaming keeps its apps. A new key lists them again."
: "Each connection is one provider account with its own key, so a work account and a personal one live side by side. Once saved, every app that key holds is listed here: give one to every bot, a workspace or your own runs, and each person signs in to their own account for it."
: "Each connection is one provider account with its own key, so a work account and a personal one live side by side. Once saved, every app that key holds is listed here: give one to every bot, a workspace or your own runs, and a workspace's apps use every account connected for them, whoever connected them."
}
>
<Form className="w-full gap-4! items-stretch pt-4" onSubmit={submit}>
@@ -75,7 +75,7 @@ export default function ConnectorAppsSection() {
return (
<SettingsShell
title={connection.name}
description={`The apps ${connection.name} offers. Give one to every bot, to a workspace, or to your own runs on a bot; each person then signs in to their own account for it.`}
description={`The apps ${connection.name} offers. Give one to every bot, to a workspace, or to your own runs on a bot; a workspace uses every account connected for that app, whoever connected it.`}
>
<Row className="w-full items-center gap-2!">
<Link href="/admin/settings/connectors" className="text-sm">
@@ -46,7 +46,7 @@ export default function ConnectorsSettingsSection() {
return (
<SettingsGroup
title="Connectors"
description="Connect a paradigm once and every app it offers is listed under it. Open a connection to search its apps and give one to every bot, to a workspace, or to your own runs on a bot; each person then signs in to their own account for it."
description="Connect a paradigm once and every app it offers is listed under it. Open a connection to search its apps and give one to every bot, to a workspace, or to your own runs on a bot; a workspace uses every account connected for that app, whoever connected it."
>
<SettingsList is_empty={!connections.length} empty="No connectors yet">
{connections.map((connection) => (
@@ -0,0 +1,23 @@
import { describe, expect, test } from "bun:test";
import connectorAccountReachSql from "./connector-account-reach-sql";
describe("connectorAccountReachSql", () => {
test("a connector given to a workspace hands every connected account of it to that workspace", () => {
const { sql, values } = connectorAccountReachSql({ workspace_id: 4 });
expect(sql).toContain("SELECT 1 FROM workspace_connectors wc");
expect(sql).toContain("wc.connector_id = a.connector_id");
expect(sql).toContain("wc.workspace_id = ?");
expect(values).toEqual([4]);
});
test("the global scope brings no accounts, only the workspace grant does", () => {
expect(connectorAccountReachSql({ workspace_id: 4 }).sql).not.toContain("all_bots");
});
test("a run outside a workspace gets no clause", () => {
expect(connectorAccountReachSql({})).toEqual({ sql: "", values: [] });
expect(connectorAccountReachSql({ workspace_id: null })).toEqual({ sql: "", values: [] });
expect(connectorAccountReachSql({ workspace_id: 0 })).toEqual({ sql: "", values: [] });
});
});
+18
View File
@@ -0,0 +1,18 @@
type Params = {
workspace_id?: number | string | null;
};
/**
* The extra account clause a workspace brings: a connector given to a
* workspace reaches everyone working in it, so every connected account of
* that connector does too, whoever connected it. Empty outside a workspace,
* and the global scope brings no accounts (only the workspace grant does).
*/
export default function connectorAccountReachSql({ workspace_id }: Params): { sql: string; values: number[] } {
if (!workspace_id) return { sql: "", values: [] };
return {
sql: `OR EXISTS (SELECT 1 FROM workspace_connectors wc WHERE wc.connector_id = a.connector_id AND wc.workspace_id = ?)`,
values: [Number(workspace_id)],
};
}