Let a Composio toolkit sign in with its own scheme, and stop adopting a replaced app's leftovers

This commit is contained in:
2026-11-22 17:20:41 +01:00
parent 60022d8220
commit fdd1e8a757
5 changed files with 167 additions and 11 deletions
@@ -40,4 +40,25 @@ describe("shouldAdoptConnectorAccount", () => {
false,
);
});
test("skips a stranger once the provider lists none of the app's accounts", () => {
expect(
shouldAdoptConnectorAccount({
status: "connected",
reconnect: false,
claimed: false,
free_slot: true,
wiped: true,
}),
).toBe(false);
expect(
shouldAdoptConnectorAccount({
status: "connected",
reconnect: false,
claimed: true,
free_slot: true,
wiped: true,
}),
).toBe(true);
});
});
@@ -12,6 +12,12 @@ type Params = {
* its own sign-in (claimed).
*/
free_slot: boolean;
/**
* The provider lists none of the accounts the app knows: they were
* replaced there, so no stranger takes their place (the cards offer
* Reconnect instead).
*/
wiped?: boolean;
};
/**
@@ -21,8 +27,15 @@ type Params = {
* provider can't pile up as four rows of the same mailbox; a second account
* appears by signing in again, which claims its own row.
*/
export default function shouldAdoptConnectorAccount({ status, reconnect, claimed, free_slot }: Params) {
export default function shouldAdoptConnectorAccount({
status,
reconnect,
claimed,
free_slot,
wiped = false,
}: Params) {
if (reconnect || claimed) return true;
if (wiped) return false;
return status == "connected" && free_slot;
}
@@ -67,6 +67,12 @@ export default async function syncConnectorAccounts({ user_id, connector_id, lab
const seen = new Set(accounts.map((account) => account.provider_account_id));
const now = Date.now();
/**
* The provider lists none of the accounts the app knows: they were
* replaced there (a sign-in made outside the app), so its leftover takes
* no card and the known ones offer Reconnect.
*/
const wiped = local.length > 0 && !local.some((row) => seen.has(String(row.provider_account_id || "")));
/**
* The app's one free card: a live row still listed by the provider holds
* it. An account arriving without a sign-in here (an older attempt at
@@ -102,6 +108,7 @@ export default async function syncConnectorAccounts({ user_id, connector_id, lab
reconnect: Boolean(reconnect_id) || (account.status == "connected" && Boolean(reconnect_row)),
claimed: claimed.has(provider_account_id),
free_slot,
wiped,
})
)
continue;
@@ -156,6 +156,7 @@ describe("composioConnectorProvider", () => {
const session = calls.find((call) => call.url.pathname.endsWith("/tool_router/session"))!;
expect(session.body.multi_account).toEqual({ enable: true, max_accounts_per_toolkit: 5 });
expect(session.body.auth_configs).toEqual({ gmail: "ac_1" });
const link = calls.find((call) => call.url.pathname.endsWith("/tool_router/session/trs_1/link"))!;
@@ -190,8 +191,69 @@ describe("composioConnectorProvider", () => {
expect(made.body.use_composio_managed_auth).toBe(true);
});
test("makes a custom sign-in setup for a toolkit Composio manages no credentials for", async () => {
const { fetchImpl, calls } = stub({
answer: ({ url, method }) => {
if (url.pathname.endsWith("/toolkits/gitea")) {
return { body: { composio_managed_auth_schemes: [], auth_config_details: [{ mode: "API_KEY" }] } };
}
if (url.pathname.endsWith("/tool_router/session")) return { status: 201, body: { session_id: "trs_1" } };
if (url.pathname.endsWith("/link")) {
return { status: 201, body: { redirect_url: "https://connect/gitea" } };
}
if (!url.pathname.endsWith("/auth_configs")) return { body: {} };
return method == "POST" ? { status: 201, body: { auth_config: { id: "ac_gitea" } } } : { body: { items: [] } };
},
});
const started = await composioConnectorProvider({ key: "k", fetchImpl }).startConnect({
app: "gitea",
user_id: 4,
label: "Self-hosted",
});
expect(started.auth_config_id).toBe("ac_gitea");
const made = calls.find((call) => call.url.pathname.endsWith("/auth_configs") && call.method == "POST")!;
expect(made.body).toEqual({
toolkit: { slug: "gitea" },
auth_config: { type: "use_custom_auth", authScheme: "API_KEY" },
});
const session = calls.find((call) => call.url.pathname.endsWith("/tool_router/session"))!;
expect(session.body.auth_configs).toEqual({ gitea: "ac_gitea" });
});
test("reuses a sign-in setup the toolkit can use, whatever its scheme", async () => {
const { fetchImpl, calls } = stub({
answer: ({ url }) => {
if (url.pathname.endsWith("/toolkits/gitea")) {
return { body: { composio_managed_auth_schemes: [], auth_config_details: [{ mode: "API_KEY" }] } };
}
if (url.pathname.endsWith("/tool_router/session")) return { status: 201, body: { session_id: "trs_1" } };
if (url.pathname.endsWith("/link")) {
return { status: 201, body: { redirect_url: "https://connect/gitea" } };
}
return { body: { items: [{ id: "ac_dash", auth_scheme: "API_KEY" }] } };
},
});
const started = await composioConnectorProvider({ key: "k", fetchImpl }).startConnect({
app: "gitea",
user_id: 4,
label: "Work",
});
expect(started.auth_config_id).toBe("ac_dash");
expect(calls.some((call) => call.url.pathname.endsWith("/auth_configs") && call.method == "POST")).toBe(false);
});
test("maps account statuses and keeps the provider's id", async () => {
const { fetchImpl } = stub({
const { fetchImpl, calls } = stub({
answer: () => ({
body: {
items: [
@@ -208,6 +270,7 @@ describe("composioConnectorProvider", () => {
{ provider_account_id: "ca_1", status: "connected", label: "work@x" },
{ provider_account_id: "ca_2", status: "expired", label: undefined },
]);
expect(calls[0]!.url.searchParams.get("toolkit_slugs")).toBe("gmail");
});
test("executes one tool with the account named in the call", async () => {
+61 -9
View File
@@ -128,29 +128,76 @@ export default function composioConnectorProvider({ key, base_url, fetchImpl }:
return items;
}
/**
* How a toolkit can be signed in: the schemes Composio manages itself and
* every scheme it accepts. Empty when the toolkit can't be read, which
* leaves the managed OAuth setup that every OAuth app has.
*/
async function toolkitAuthMethods({ app }: { app: string }): Promise<{ managed: string[]; schemes: string[] }> {
try {
const toolkit = await request<any>({
path: `/toolkits/${encodeURIComponent(app)}`,
what: `the ${app} sign-in methods`,
});
return {
managed: (toolkit.composio_managed_auth_schemes || []).map((scheme: string) =>
String(scheme).toUpperCase(),
),
schemes: (toolkit.auth_config_details || [])
.map((detail: any) => String(detail.mode || "").toUpperCase())
.filter(Boolean),
};
} catch {
return { managed: [], schemes: [] };
}
}
/**
* The sign-in setup a link belongs to. Composio wants an auth config on
* every link, and makes a managed one for a toolkit that has none yet,
* so a fresh install can connect an app without visiting its dashboard.
* A toolkit Composio manages no credentials for (Gitea, whose key and
* instance URL are the user's own) gets a custom setup of its own scheme
* instead, which the hosted sign-in page fills in.
*/
async function authConfigFor({ app, auth_config_id }: { app: string; auth_config_id?: string | null }) {
if (auth_config_id) return auth_config_id;
const existing = await listAllPages<any>({
path: "/auth_configs",
query: { toolkit_slug: app },
what: `the ${app} sign-in setups`,
});
const usable = existing.find((item) => item.auth_scheme == "OAUTH2") || existing[0];
const [existing, methods] = await Promise.all([
listAllPages<any>({
path: "/auth_configs",
query: { toolkit_slug: app },
what: `the ${app} sign-in setups`,
}),
toolkitAuthMethods({ app }),
]);
const usable = (item: any) =>
!methods.schemes.length || methods.schemes.includes(String(item.auth_scheme || "").toUpperCase());
const reusable =
existing.find((item) => usable(item) && String(item.auth_scheme).toUpperCase() == "OAUTH2") ||
existing.find(usable);
if (usable?.id) return String(usable.id);
if (reusable?.id) return String(reusable.id);
// Nothing known about the toolkit means an OAuth app that Composio
// manages, as before.
const managed = !methods.schemes.length || methods.managed.length > 0;
try {
const created = await request<any>({
path: "/auth_configs",
method: "POST",
what: `a sign-in setup for ${app}`,
body: { toolkit: { slug: app }, auth_scheme: "OAUTH2", use_composio_managed_auth: true },
body: managed
? {
toolkit: { slug: app },
auth_scheme: methods.managed.includes("OAUTH2")
? "OAUTH2"
: methods.managed[0] || "OAUTH2",
use_composio_managed_auth: true,
}
: { toolkit: { slug: app }, auth_config: { type: "use_custom_auth", authScheme: methods.schemes[0] } },
});
const id = created.auth_config?.id || created.id;
@@ -246,6 +293,9 @@ export default function composioConnectorProvider({ key, base_url, fetchImpl }:
body: {
user_id: `cobble-user-${user_id}`,
toolkits: { enable: [app] },
// The link names no auth config, so the session is what
// pins the setup a toolkit with several of them uses.
auth_configs: { [app]: config },
multi_account: { enable: true, max_accounts_per_toolkit: MaxAccountsPerToolkit },
},
});
@@ -280,7 +330,9 @@ export default function composioConnectorProvider({ key, base_url, fetchImpl }:
async accounts({ app, user_id }) {
const items = await listAllPages<any>({
path: "/connected_accounts",
query: { user_id: `cobble-user-${user_id}`, toolkit_slug: app },
// 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 },
what: `the ${app} accounts`,
});