Skip to content

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 test user id fallback on login (#2775)

Musthaq Ahamad501521063e80
diff --git a/.changeset/salty-olives-guess.md b/.changeset/salty-olives-guess.md
new file mode 100644
index 000000000..3f87f3886
--- /dev/null
+++ b/.changeset/salty-olives-guess.md
@@ -0,0 +1,5 @@
+---
+'@composio/cli': patch
+---
+
+Fallback to global context if project apikeys not found
diff --git a/ts/packages/cli/src/commands/login.cmd.ts b/ts/packages/cli/src/commands/login.cmd.ts
index af91264df..365011cae 100644
--- a/ts/packages/cli/src/commands/login.cmd.ts
+++ b/ts/packages/cli/src/commands/login.cmd.ts
@@ -95,6 +95,10 @@ const storeCredentials = (params: {
// identifiers rather than the actual org/project IDs).
const orgId = sessionInfo?.project.org.id ?? initialOrgId;
const projectId = sessionInfo?.project.nano_id ?? initialProjectId;
+ const sessionUserId = sessionInfo?.org_member.user_id ?? sessionInfo?.org_member.id;
+ const testUserId = sessionUserId
+ ? `pg-test-${sessionUserId}`
+ : Option.getOrUndefined(ctx.data.testUserId);
if (sessionInfo) {
if (initialOrgId !== orgId) {
@@ -108,7 +112,7 @@ const storeCredentials = (params: {
}
// Store UAK + org/project IDs in user_data.json
- yield* ctx.login(uakApiKey, orgId, projectId);
+ yield* ctx.login(uakApiKey, orgId, projectId, testUserId);
const email = sessionInfo?.org_member.email || fallbackEmail || undefined;
yield* ui.log.success(email ? `Logged in as ${email}` : 'Logged in successfully');
diff --git a/ts/packages/cli/src/models/user-data.ts b/ts/packages/cli/src/models/user-data.ts
index 7ac051ac3..52e273a43 100644
--- a/ts/packages/cli/src/models/user-data.ts
+++ b/ts/packages/cli/src/models/user-data.ts
@@ -37,6 +37,13 @@ export const UserData = Schema.Struct({
projectId: Schema.propertySignature(OptionFromNullishOr(Schema.String, null)).pipe(
Schema.fromKey('project_id')
),
+
+ /**
+ * Optional global test user identifier used by CLI/e2e flows.
+ */
+ testUserId: Schema.propertySignature(OptionFromNullishOr(Schema.String, null)).pipe(
+ Schema.fromKey('test_user_id')
+ ),
}).annotations({
identifier: 'UserData',
description: 'User data storage for the Composio CLI',
diff --git a/ts/packages/cli/src/services/composio-clients.ts b/ts/packages/cli/src/services/composio-clients.ts
index bbcc95774..04fcf0444 100644
--- a/ts/packages/cli/src/services/composio-clients.ts
+++ b/ts/packages/cli/src/services/composio-clients.ts
@@ -1103,13 +1103,21 @@ export class ComposioClientSingleton extends Effect.Service<ComposioClientSingle
const apiKey =
typeof rawApiKey === 'string' && rawApiKey.trim().length > 0 ? rawApiKey : undefined;
const baseURL = ctx.data.baseURL;
+ const globalOrgId = Option.getOrUndefined(ctx.data.orgId);
+ const globalProjectId = Option.getOrUndefined(ctx.data.projectId);
const resolvedProjectContext = yield* Option.match(projectContextOpt, {
onNone: () => Effect.succeed(Option.none()),
onSome: projectContext =>
projectContext.resolve.pipe(Effect.catchAll(() => Effect.succeed(Option.none()))),
});
const defaultHeaders = Option.match(resolvedProjectContext, {
- onNone: () => undefined,
+ onNone: () =>
+ globalOrgId && globalProjectId
+ ? ({
+ 'x-org-id': globalOrgId,
+ 'x-project-id': globalProjectId,
+ } satisfies Record<string, string>)
+ : undefined,
onSome: keys =>
({
'x-org-id': keys.orgId,
diff --git a/ts/packages/cli/src/services/user-context.ts b/ts/packages/cli/src/services/user-context.ts
index 5b8f8a65e..b19ad9d3e 100644
--- a/ts/packages/cli/src/services/user-context.ts
+++ b/ts/packages/cli/src/services/user-context.ts
@@ -34,7 +34,8 @@ export class ComposioUserContext extends Context.Tag('ComposioUserData')<
login: (
apiKey: string,
orgId?: string,
- projectId?: string
+ projectId?: string,
+ testUserId?: string
) => Effect.Effect<void, ParseError | PlatformError, never>;
/**
@@ -64,6 +65,7 @@ export const ComposioUserContextLive = Layer.effect(
webURL: Option.some(webURL),
orgId: Option.none(),
projectId: Option.none(),
+ testUserId: Option.none(),
});
const logout = Effect.gen(function* () {
@@ -73,10 +75,11 @@ export const ComposioUserContextLive = Layer.effect(
webURL: Option.some(webURL),
orgId: Option.none(),
projectId: Option.none(),
+ testUserId: Option.none(),
});
});
- const login = (apiKey: string, orgId?: string, projectId?: string) =>
+ const login = (apiKey: string, orgId?: string, projectId?: string, testUserId?: string) =>
Effect.gen(function* () {
yield* update({
apiKey: Option.some(apiKey),
@@ -84,6 +87,7 @@ export const ComposioUserContextLive = Layer.effect(
webURL: Option.some(webURL),
orgId: Option.fromNullable(orgId),
projectId: Option.fromNullable(projectId),
+ testUserId: Option.fromNullable(testUserId),
});
});
@@ -117,6 +121,7 @@ export const ComposioUserContextLive = Layer.effect(
webURL: Option.some(webURL),
orgId: parsedUserData.orgId,
projectId: parsedUserData.projectId,
+ testUserId: parsedUserData.testUserId,
} satisfies UserData;
yield* Effect.logDebug('User data (overridden from env vars):', overriddenUserData);
@@ -151,6 +156,7 @@ export const ComposioUserContextLive = Layer.effect(
webURL: Option.getOrElse(userData.webURL, () => webURL),
orgId: userData.orgId,
projectId: userData.projectId,
+ testUserId: userData.testUserId,
};
return ComposioUserContext.of({
diff --git a/ts/packages/cli/test/src/commands/logout.cmd.test.ts b/ts/packages/cli/test/src/commands/logout.cmd.test.ts
index 365edbd1a..0b1e84731 100644
--- a/ts/packages/cli/test/src/commands/logout.cmd.test.ts
+++ b/ts/packages/cli/test/src/commands/logout.cmd.test.ts
@@ -19,6 +19,7 @@ describe('CLI: composio logout', () => {
webURL: 'https://platform.composio.dev',
orgId: Option.none(),
projectId: Option.none(),
+ testUserId: Option.none(),
});
expect(ctx.data).toMatchObject(expectedUserData);
@@ -55,6 +56,7 @@ describe('CLI: composio logout', () => {
webURL: 'https://platform.composio.dev',
orgId: Option.none(),
projectId: Option.none(),
+ testUserId: Option.none(),
})
);
diff --git a/ts/packages/cli/test/src/services/user-context.test.ts b/ts/packages/cli/test/src/services/user-context.test.ts
index 1cc55a67f..fda15e08f 100644
--- a/ts/packages/cli/test/src/services/user-context.test.ts
+++ b/ts/packages/cli/test/src/services/user-context.test.ts
@@ -35,6 +35,7 @@ describe('ComposioUserContext', () => {
webURL: 'https://platform.composio.dev',
orgId: Option.none(),
projectId: Option.none(),
+ testUserId: Option.none(),
});
assertEquals(Data.struct(ctx.data), Data.struct(expectedUserData));
assertEquals(ctx.isLoggedIn(), false);
@@ -65,6 +66,7 @@ describe('ComposioUserContext', () => {
webURL: 'https://platform.composio.dev',
orgId: Option.none(),
projectId: Option.none(),
+ testUserId: Option.none(),
});
assertEquals(Data.struct(ctx.data), Data.struct(expectedUserData));
assertEquals(ctx.isLoggedIn(), true);
@@ -93,6 +95,7 @@ describe('ComposioUserContext', () => {
webURL: Option.some('https://platform.composio.dev'),
orgId: Option.none(),
projectId: Option.none(),
+ testUserId: Option.none(),
});
const userDataAsJson = yield* userDataToJSON(expectedUserData);
@@ -132,6 +135,7 @@ describe('ComposioUserContext', () => {
webURL: Option.some('https://platform.composio.dev'),
orgId: Option.none(),
projectId: Option.none(),
+ testUserId: Option.none(),
});
const userDataAsJson = yield* userDataToJSON(expectedUserData);
@@ -180,6 +184,7 @@ describe('ComposioUserContext', () => {
webURL: 'https://platform.composio.dev',
orgId: Option.none(),
projectId: Option.none(),
+ testUserId: Option.none(),
});
assertEquals(Data.struct(ctx.data), Data.struct(expectedUserData));
assertEquals(ctx.isLoggedIn(), false);
@@ -224,6 +229,7 @@ describe('ComposioUserContext', () => {
webURL: 'https://platform.composio.dev',
orgId: Option.none(),
projectId: Option.none(),
+ testUserId: Option.none(),
});
assertEquals(Data.struct(ctx.data), Data.struct(expectedUserData));
assertEquals(ctx.isLoggedIn(), false);
@@ -260,6 +266,7 @@ describe('ComposioUserContext', () => {
webURL: 'https://platform.composio.dev',
orgId: Option.none(),
projectId: Option.none(),
+ testUserId: Option.none(),
});
assertEquals(Data.struct(ctx.data), Data.struct(expectedUserData));
assertEquals(ctx.isLoggedIn(), false);