diff --git a/AGENTS.md b/AGENTS.md index ba43957e..d7656804 100644 --- a/AGENTS.md +++ b/AGENTS.md @@ -214,7 +214,7 @@ A user's own running browser, reached over a browser MCP server they run beside ### Connectors (`connector_connections`, `connectors`, `bot_connectors`, `workspace_connectors`, `connector_accounts`, `connector_sessions`, `connector_requests`) -A user's access to an outside app (Gmail, Slack, ...) as themselves, reached through a provider that holds the OAuth app and the connected account (Settings > Connectors). Composio is the first provider: one file under `src/harness/connectors/providers/` and the preset in `src/dict/connector-dict.ts`. **A connection is one provider account, not an app**: `connector_connections` has one row per provider account (Composio Personal, Composio Work, ...), each with its own encrypted `api_key`, added in Settings > Connectors; the form's **paradigm** (`provider`, Composio the first) decides how that connection's apps are reached, and more paradigms can be added; that form names the key a paradigm wants and links where to make it (`key_help`, `key_url`, `key_url_title` in the preset: Composio wants a project API key `ak_...` from its Project Settings > API Keys, not a `ck_...` key made for `connect.composio.dev/mcp`, whose router signs everyone in as the key's owner); `connectorProviderFor` builds the provider from that key, so no install-wide key is read from Environment. `syncConnectorConnection({ connection_id })` reads every app the connection offers into `connectors` (`connectors.connection_id`; `planConnectorSync` derives each slug and names a second connection's app after it, `gmail-work`), so nothing is added or edited app by app; a stored app follows the provider's name, description and logo only, keeping its slug, reach and accounts. A thousand apps cost nothing until one is used: its tools are read when it is first given to a bot or workspace (`syncConnector` from `set-bot-connector`/`set-connector-access` when `tools` is empty) and by the cron for assigned apps alone. **An account is a person's own** (`connector_accounts.user_id`), several per app, and its owner's runs always reach it; its owner or a super admin can also give it out (`connector_accounts.all_bots`, `connector_account_workspaces`, `connector_account_bots`, written by `setConnectorAccountAccess`), so every run in a workspace or on a bot reaches it too, whoever signed in (and giving an account out brings its app along to that bot or workspace, since `botConnectorsSql` counts a connected account given there), and the session is built as the account's owner (`openBotConnectors` passes `account.user_id`, and a shared line names whose account it is). A sign-in the provider no longer lists reads revoked, and its modal offers **Reconnect**, which starts a fresh sign-in for that row (`connector_requests.account_id`), so the finished one replaces it in place and keeps its label and namespace. A reconnect reuses that row, never a second one: a pending account no request here started (an abandoned or replaced sign-in at the provider) never becomes a row, and a reconnect the provider gave no id for lands on the row it replaces, which its request names (`connector_requests.account_id`, read in `syncConnectorAccounts`). **One card per app unless the user signs in again:** a connected account the provider holds but no request here made fills the app's one free card and is left at the provider after that, so leftovers from earlier attempts cannot pile up as four rows of the same mailbox. The account name is only a label: the grant is whichever account was chosen on the provider's page, which the connect modal says. Its owner or a super admin renames it (`saveConnectorAccount`, which regenerates the slug, so a namespace follows its label); a member can only give an account to places they reach, and only a super admin sets `all_bots`. **A second account is a new sign-in, never the first one reused:** `startConnectorConnection` always makes a link (an account already connected never stands in for the new one), and a request is settled only by the account it made (`connector_requests.provider_account_id`, matched in `settleRequests` and `grabConnectorRequestView`), so a request the user has not finished cannot show the label of an account that was already there. The sign-in itself must never replace the app's earlier account: Composio's plain `connected_accounts/link` does exactly that, so the adapter first opens a multi-account tool-router session and takes its link (`/tool_router/session//link`), which keeps the other sign-ins; tool sessions stay single-account, pinning one in `connected_accounts`, since a multi-account session exposes only the router's meta tools and refuses `preload`. Credentials never reach this app or a model, and a run gets tools from an endpoint the provider builds for one account. **Where a connector reaches:** `all_bots` (every bot, every user) and `workspace_connectors` are the install's and only a super admin sets them; `bot_connectors.user_id` is NOT NULL, so a bot row is always personal to whoever made it and shows only in their runs (`botConnectorsSql`). Runs get deferred tools through `openBotConnectors` (`__`, or `-__` when the user has more than one account) and `ConnectorsNote`, opened beside `openBotExtensions`/`openBotBrowsers` in replies and task runs. Tool names and the action cap (`connectors_actions_per_run`, an `app_settings` row, 50 by default) come from the app. Transport is the MCP lane; when a provider can't pin an account at a session endpoint (`endpoint` throws), `ensure-connector-endpoint` records `session_id = "rest"` and the tools call `provider.execute({tool, args, account})` with the account, so the model's surface never changes. Composio's tool router answers with a URL and no headers and rejects a keyless call (401 "API key is required"), so the adapter sends the connection's project key as `x-api-key` on every session call; a cached session whose stored headers can't be read is rebuilt, never used keyless. An account the provider no longer has is refused, never guessed: a session pinned to a deleted id makes the provider answer from whichever account it still holds, so `ensure-connector-endpoint` checks the account is still listed, marks the row revoked and stops the call with `connectorAccountGoneMessage` (Reconnect is then on its card); the `*/15` cron re-checks connected accounts every hour for the same reason. **Bots never make or change a connector:** a missing account in an attended reply makes the app create `connector_requests` and show `[connect:ID]` (`ConnectRefCard`, the sign-in opened from there, then read by asking the provider's accounts), and a note line says so; a task, routine, hook call or crew request never gets the card. **The settings page lists connections only** (super admin: add, edit, delete, sync; everyone: their own sign-in); each connection's apps live at `/admin/settings/connectors/`, `ConnectorAppsPageSize` (24) a page, searched on the server over name, app and description by `grabConnectorApps`, so a connection holding 1,842 apps never renders them all; that read puts apps with a connected account the viewer can see first and filters to `all` / `connected` / `unconnected` (`ConnectorAppFilters`), returning each tab's count with the page. A row's one assignment control is who gets the app; its account cards (`ConnectorAccounts`) carry the label, one status, a refresh action (re-check a live account, reconnect a dead one) and disconnect, never where the account reaches (that lives in its modal), "New connection" opens the sign-in modal (the app's logo beside its title, Connect the only primary button), an account chip opens its own modal: the name, `all_bots` and the bots and workspaces it reaches (reach-checked lists), and Disconnect; the row's New connection line leads with how many of the app's accounts are connected (`N accounts`, in the success colour, and nothing while none is). A conversation's composer tools modal ends with a **Connectors** section (`ComposerConnectorsSection`), read by `grabBotConnectors` when the modal opens (never by the page): each app the bot reaches there (its title tagged with the connection's name, and with the account's label when an account given here is what brings it), where it comes from (every bot, this workspace, only you, or an account given here) and the accounts that reach there (this user's own, whatever their status, and connected ones given here, with their owner), or that none is connected yet, with a Manage link to Settings > Connectors, and Add/remove: Add opens a modal that walks down from the connection, to the app (searched on the server through `grabAttachableConnectors` with its `connection_id`, 20 at a time, listing only apps with a connected account of the user's and never one they already attached to this bot), to that app's own accounts (`grabConnectorOwnAccounts`, which leaves out an account already given here) or All accounts for the app alone, and gives the app to this bot for this user (`connectors/set-bot`, so every account they have of it reaches the bot, ones connected later included), plus the picked account to this bot (`connectors/set-account-access`); Remove is the row's X (shown when `entry.removable`) and calls `connectors/detach`, taking away this user's own attachment there and their own account given to that bot alone, while a workspace's, the install's or an account given to every bot shows no X. The composer's Tools button draws that bot's connected apps as up to five small circles before the spanner (one per app, `connector_apps` from `grabBotExtensionsOverview`, kept live with the count when the modal reads them), so a conversation shows what it can reach at a glance. `sync-connectors` (`*/15`) refreshes every connection's apps, assigned apps' tools and the accounts a sign-in left waiting (pending requests and any account not yet `connected`, so an OAuth callback that lands after the poll is picked up); `prune-connector-requests` takes old requests. A request is only settled as connected once the provider reports an account connected, never on a sync that found none, and a listing that names no account revokes no card: the provider failing to answer is not every account deleted there. Deleting a connection removes its apps, sessions, requests, accounts and grants explicitly. Connectors travel in no extension bundle. +A user's access to an outside app (Gmail, Slack, ...) as themselves, reached through a provider that holds the OAuth app and the connected account (Settings > Connectors). Composio is the first provider: one file under `src/harness/connectors/providers/` and the preset in `src/dict/connector-dict.ts`. **A connection is one provider account, not an app**: `connector_connections` has one row per provider account (Composio Personal, Composio Work, ...), each with its own encrypted `api_key`, added in Settings > Connectors; the form's **paradigm** (`provider`, Composio the first) decides how that connection's apps are reached, and more paradigms can be added; that form names the key a paradigm wants and links where to make it (`key_help`, `key_url`, `key_url_title` in the preset: Composio wants a project API key `ak_...` from its Project Settings > API Keys, not a `ck_...` key made for `connect.composio.dev/mcp`, whose router signs everyone in as the key's owner); `connectorProviderFor` builds the provider from that key, so no install-wide key is read from Environment. `syncConnectorConnection({ connection_id })` reads every app the connection offers into `connectors` (`connectors.connection_id`; `planConnectorSync` derives each slug and names a second connection's app after it, `gmail-work`), so nothing is added or edited app by app; a stored app follows the provider's name, description and logo only, keeping its slug, reach and accounts. A thousand apps cost nothing until one is used: its tools are read when it is first given to a bot or workspace (`syncConnector` from `set-bot-connector`/`set-connector-access` when `tools` is empty) and by the cron for assigned apps alone. **An account is a person's own** (`connector_accounts.user_id`), several per app, and its owner's runs always reach it; its owner or a super admin can also give it out (`connector_accounts.all_bots`, `connector_account_workspaces`, `connector_account_bots`, written by `setConnectorAccountAccess`), so every run in a workspace or on a bot reaches it too, whoever signed in (and giving an account out brings its app along to that bot or workspace, since `botConnectorsSql` counts a connected account given there), and the session is built as the account's owner (`openBotConnectors` passes `account.user_id`, and a shared line names whose account it is). A sign-in the provider no longer lists reads revoked, and its modal offers **Reconnect**, which starts a fresh sign-in for that row (`connector_requests.account_id`), so the finished one replaces it in place and keeps its label and namespace. A reconnect reuses that row, never a second one: a pending account no request here started (an abandoned or replaced sign-in at the provider) never becomes a row, and a reconnect the provider gave no id for lands on the row it replaces, which its request names (`connector_requests.account_id`, read in `syncConnectorAccounts`). **One card per app unless the user signs in again:** a connected account the provider holds but no request here made fills the app's one free card and is left at the provider after that, so leftovers from earlier attempts cannot pile up as four rows of the same mailbox. The account name is only a label: the grant is whichever account was chosen on the provider's page, which the connect modal says. Its owner or a super admin renames it (`saveConnectorAccount`, which regenerates the slug, so a namespace follows its label); a member can only give an account to places they reach, and only a super admin sets `all_bots`. **A second account is a new sign-in, never the first one reused:** `startConnectorConnection` always makes a link (an account already connected never stands in for the new one), and a request is settled only by the account it made (`connector_requests.provider_account_id`, matched in `settleRequests` and `grabConnectorRequestView`), so a request the user has not finished cannot show the label of an account that was already there. The sign-in itself must never replace the app's earlier account: Composio's plain `connected_accounts/link` does exactly that, so the adapter first opens a multi-account tool-router session and takes its link (`/tool_router/session//link`), which keeps the other sign-ins; tool sessions stay single-account, pinning one in `connected_accounts`, since a multi-account session exposes only the router's meta tools and refuses `preload`. Credentials never reach this app or a model, and a run gets tools from an endpoint the provider builds for one account. **Where a connector reaches:** `all_bots` (every bot, every user) and `workspace_connectors` are the install's and only a super admin sets them; `bot_connectors.user_id` is NOT NULL, so a bot row is always personal to whoever made it and shows only in their runs (`botConnectorsSql`). A workspace's grant is wider than a bot's: every connected account of a connector given to a workspace reaches every run there, whoever connected it (`connectorAccountReachSql`, in `grab-run-connectors.ts` and `grab-bot-connectors.ts`), the run acting as that account's owner and naming them; `all_bots` hands over no accounts, only the app. Runs get deferred tools through `openBotConnectors` (`__`, or `-__` when the user has more than one account) and `ConnectorsNote`, opened beside `openBotExtensions`/`openBotBrowsers` in replies and task runs. Tool names and the action cap (`connectors_actions_per_run`, an `app_settings` row, 50 by default) come from the app. Transport is the MCP lane; when a provider can't pin an account at a session endpoint (`endpoint` throws), `ensure-connector-endpoint` records `session_id = "rest"` and the tools call `provider.execute({tool, args, account})` with the account, so the model's surface never changes. Composio's tool router answers with a URL and no headers and rejects a keyless call (401 "API key is required"), so the adapter sends the connection's project key as `x-api-key` on every session call; a cached session whose stored headers can't be read is rebuilt, never used keyless. An account the provider no longer has is refused, never guessed: a session pinned to a deleted id makes the provider answer from whichever account it still holds, so `ensure-connector-endpoint` checks the account is still listed, marks the row revoked and stops the call with `connectorAccountGoneMessage` (Reconnect is then on its card); the `*/15` cron re-checks connected accounts every hour for the same reason. Composio's account listing filters on plural parameter names only (`toolkit_slugs`, `user_ids`): a singular one is ignored and answers with the whole project's accounts, which once filled one user's card with another user's sign-in. **Bots never make or change a connector:** a missing account in an attended reply makes the app create `connector_requests` and show `[connect:ID]` (`ConnectRefCard`, the sign-in opened from there, then read by asking the provider's accounts), and a note line says so; a task, routine, hook call or crew request never gets the card. **The settings page lists connections only** (super admin: add, edit, delete, sync; everyone: their own sign-in); each connection's apps live at `/admin/settings/connectors/`, `ConnectorAppsPageSize` (24) a page, searched on the server over name, app and description by `grabConnectorApps`, so a connection holding 1,842 apps never renders them all; that read puts apps with a connected account the viewer can see first and filters to `all` / `connected` / `unconnected` (`ConnectorAppFilters`), returning each tab's count with the page. A row's one assignment control is who gets the app; its account cards (`ConnectorAccounts`) carry the label, one status, a refresh action (re-check a live account, reconnect a dead one) and disconnect, never where the account reaches (that lives in its modal), "New connection" opens the sign-in modal (the app's logo beside its title, Connect the only primary button), an account chip opens its own modal: the name, `all_bots` and the bots and workspaces it reaches (reach-checked lists), and Disconnect; the row's New connection line leads with how many of the app's accounts are connected (`N accounts`, in the success colour, and nothing while none is). A conversation's composer tools modal ends with a **Connectors** section (`ComposerConnectorsSection`), read by `grabBotConnectors` when the modal opens (never by the page): each app the bot reaches there (its title tagged with the connection's name, and with the account's label when an account given here is what brings it), where it comes from (every bot, this workspace, only you, or an account given here) and the accounts that reach there (this user's own, whatever their status, and connected ones given here, with their owner), or that none is connected yet, with a Manage link to Settings > Connectors, and Add/remove: Add opens a modal that walks down from the connection, to the app (searched on the server through `grabAttachableConnectors` with its `connection_id`, 20 at a time, listing only apps with a connected account of the user's and never one they already attached to this bot), to that app's own accounts (`grabConnectorOwnAccounts`, which leaves out an account already given here) or All accounts for the app alone, and gives the app to this bot for this user (`connectors/set-bot`, so every account they have of it reaches the bot, ones connected later included), plus the picked account to this bot (`connectors/set-account-access`); Remove is the row's X (shown when `entry.removable`) and calls `connectors/detach`, taking away this user's own attachment there and their own account given to that bot alone, while a workspace's, the install's or an account given to every bot shows no X. The composer's Tools button draws that bot's connected apps as up to five small circles before the spanner (one per app, `connector_apps` from `grabBotExtensionsOverview`, kept live with the count when the modal reads them), so a conversation shows what it can reach at a glance. `sync-connectors` (`*/15`) refreshes every connection's apps, assigned apps' tools and the accounts a sign-in left waiting (pending requests and any account not yet `connected`, so an OAuth callback that lands after the poll is picked up); `prune-connector-requests` takes old requests. A request is only settled as connected once the provider reports an account connected, never on a sync that found none, and a listing that names no account revokes no card: the provider failing to answer is not every account deleted there. Deleting a connection removes its apps, sessions, requests, accounts and grants explicitly. Connectors travel in no extension bundle. ### Knowledgebases diff --git a/src/functions/backend/db/connectors/grab-bot-connectors.ts b/src/functions/backend/db/connectors/grab-bot-connectors.ts index 82aa66cf..1e1a9248 100644 --- a/src/functions/backend/db/connectors/grab-bot-connectors.ts +++ b/src/functions/backend/db/connectors/grab-bot-connectors.ts @@ -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({ @@ -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})`, diff --git a/src/functions/backend/db/connectors/grab-run-connectors.ts b/src/functions/backend/db/connectors/grab-run-connectors.ts index 8100180d..662929b6 100644 --- a/src/functions/backend/db/connectors/grab-run-connectors.ts +++ b/src/functions/backend/db/connectors/grab-run-connectors.ts @@ -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 { 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({ @@ -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 || []; diff --git a/src/harness/connectors/providers/composio.test.ts b/src/harness/connectors/providers/composio.test.ts index 6ca2bb83..ad6c1e84 100644 --- a/src/harness/connectors/providers/composio.test.ts +++ b/src/harness/connectors/providers/composio.test.ts @@ -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 () => { diff --git a/src/harness/connectors/providers/composio.ts b/src/harness/connectors/providers/composio.ts index 034af47f..b3ae4174 100644 --- a/src/harness/connectors/providers/composio.ts +++ b/src/harness/connectors/providers/composio.ts @@ -330,9 +330,11 @@ export default function composioConnectorProvider({ key, base_url, fetchImpl }: async accounts({ app, user_id }) { const items = await listAllPages({ 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`, }); diff --git a/src/pages/admin/settings/connectors/(partials)/connector-access-modal.tsx b/src/pages/admin/settings/connectors/(partials)/connector-access-modal.tsx index d4ac7977..c98dd43c 100644 --- a/src/pages/admin/settings/connectors/(partials)/connector-access-modal.tsx +++ b/src/pages/admin/settings/connectors/(partials)/connector-access-modal.tsx @@ -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." } >
diff --git a/src/pages/admin/settings/connectors/(sections)/connector-apps-section.tsx b/src/pages/admin/settings/connectors/(sections)/connector-apps-section.tsx index b3b0002f..56354b99 100644 --- a/src/pages/admin/settings/connectors/(sections)/connector-apps-section.tsx +++ b/src/pages/admin/settings/connectors/(sections)/connector-apps-section.tsx @@ -75,7 +75,7 @@ export default function ConnectorAppsSection() { return ( diff --git a/src/pages/admin/settings/connectors/(sections)/connectors-settings-section.tsx b/src/pages/admin/settings/connectors/(sections)/connectors-settings-section.tsx index 2524884e..a420c6cc 100644 --- a/src/pages/admin/settings/connectors/(sections)/connectors-settings-section.tsx +++ b/src/pages/admin/settings/connectors/(sections)/connectors-settings-section.tsx @@ -46,7 +46,7 @@ export default function ConnectorsSettingsSection() { return ( {connections.map((connection) => ( diff --git a/src/utils/connector-account-reach-sql.test.ts b/src/utils/connector-account-reach-sql.test.ts new file mode 100644 index 00000000..aa074fcd --- /dev/null +++ b/src/utils/connector-account-reach-sql.test.ts @@ -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: [] }); + }); +}); \ No newline at end of file diff --git a/src/utils/connector-account-reach-sql.ts b/src/utils/connector-account-reach-sql.ts new file mode 100644 index 00000000..7f52ae13 --- /dev/null +++ b/src/utils/connector-account-reach-sql.ts @@ -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)], + }; +} \ No newline at end of file