@devries-cameron20 / composio-1 / commits / 69a73648b
Fork 0

Composio powers 1000+ toolkits, tool search, context management, authentication, and a sandboxed workbench to help you build AI agents that turn intent into action.

fix(cli): preserve selected org on manual link

Kshitij Jhunjhunwala committed Jul 30, 2026 at 11:13 UTC 69a73648b372e95e7e1382677989b9f3ca3bdd6d
2 files changed +82
ts/packages/cli/src/commands/connected-accounts/commands/connected-accounts.link.cmd.ts
+1
@@ -239,6 +239,7 @@ const handleNoManagedAuth = (ui: TerminalUI, toolkitSlug: string, noBrowser: boo
239 const sessionInfo = yield* getSessionInfoByUserApiKey({
240 baseURL: userContext.data.baseURL,
241 userApiKey: apiKey,
242 + orgId: Option.getOrUndefined(userContext.data.orgId),
243 }).pipe(Effect.catchAll(() => Effect.succeed(null)));
244
245 if (sessionInfo) {
ts/packages/cli/test/src/commands/connected-accounts/connected-accounts.link.cmd.test.ts
+81
@@ -6,6 +6,7 @@ import { cli, TestLive, MockConsole } from 'test/__utils__';
6 import type { TestLiveInput } from 'test/__utils__/services/test-layer';
7 import type { ConnectedAccountItem } from 'src/models/connected-accounts';
8 import { getTerminalCapabilities, TerminalUI } from 'src/services/terminal-ui';
9 +import { ComposioUserContext } from 'src/services/user-context';
10 import open from 'open';
11 import { afterEach, vi } from 'vitest';
12
@@ -115,6 +116,7 @@ describe('CLI: composio dev connected-accounts link', () => {
116 }));
117
118 afterEach(() => {
119 + vi.restoreAllMocks();
120 vi.clearAllMocks();
121 });
122
@@ -344,6 +346,85 @@ describe('CLI: composio dev connected-accounts link', () => {
346 );
347 });
348
349 + layer(
350 + TestLive({
351 + baseConfigProvider: testConfigProvider,
352 + connectedAccountsData: makeConnectedAccountsData(),
353 + fixture: 'global-test-user-id',
354 + toolRouter: {
355 + link: async () => {
356 + throw Object.assign(new Error('No managed auth'), {
357 + slug: 'ToolRouterV2_NoManagedAuth',
358 + });
359 + },
360 + },
361 + })
362 + )('[Given] unmanaged auth after an org switch [Then] resolves the selected membership', it => {
363 + it.scoped('passes the active org to session info before linking analytics identity', () =>
364 + Effect.gen(function* () {
365 + const userContext = yield* ComposioUserContext;
366 + yield* userContext.login('test_api_key', 'org_selected', 'consumer-user-org_selected');
367 +
368 + const originalFetch = globalThis.fetch;
369 + const sessionInfoRequests: Headers[] = [];
370 + vi.spyOn(globalThis, 'fetch').mockImplementation(
371 + async (requestInput: RequestInfo | URL, init?: RequestInit) => {
372 + const url =
373 + typeof requestInput === 'string'
374 + ? requestInput
375 + : requestInput instanceof URL
376 + ? requestInput.toString()
377 + : requestInput.url;
378 +
379 + if (url.includes('/api/v3/auth/session/info')) {
380 + const headers = new Headers(init?.headers);
381 + sessionInfoRequests.push(headers);
382 + return new Response(
383 + JSON.stringify({
384 + project: {
385 + name: 'Selected Project',
386 + id: 'project_id_selected',
387 + org_id: 'org_selected',
388 + nano_id: 'project_selected',
389 + email: 'project@example.com',
390 + created_at: '2026-01-01T00:00:00.000Z',
391 + updated_at: '2026-01-01T00:00:00.000Z',
392 + org: {
393 + id: 'org_selected',
394 + name: 'Selected Org',
395 + plan: 'enterprise',
396 + },
397 + },
398 + org_member: {
399 + id: 'member_selected',
400 + user_id: 'user_123',
401 + email: 'cli@example.com',
402 + name: 'CLI User',
403 + role: 'admin',
404 + },
405 + api_key: null,
406 + }),
407 + {
408 + status: 200,
409 + headers: { 'Content-Type': 'application/json' },
410 + }
411 + );
412 + }
413 +
414 + return originalFetch(requestInput, init);
415 + }
416 + );
417 +
418 + yield* cli(['link', 'gmail', '--no-browser']);
419 +
420 + expect(sessionInfoRequests).toHaveLength(1);
421 + expect(sessionInfoRequests[0].get('x-org-id')).toBe('org_selected');
422 + const output = (yield* MockConsole.getLines({ stripAnsi: true })).join('\n');
423 + expect(output).toContain('/Selected%20Org/~/connect/apps/gmail?open=true');
424 + })
425 + );
426 + });
427 +
428 layer(
429 TestLive({
430 baseConfigProvider: testConfigProvider,