9
} from 'src/services/composio-clients';
10
import { ComposioUserContext } from 'src/services/user-context';
11
import { TerminalUI } from 'src/services/terminal-ui';
12
+import { runOrgProjectSelection } from 'src/effects/select-org-project';
13
14
export const noBrowser = Options.boolean('no-browser').pipe(
15
Options.withDefault(false),
16
Options.withDescription('Login without browser interaction')
17
);
18
18
-const apiKeyOpt = Options.text('api-key').pipe(
19
- Options.optional,
20
- Options.withDescription('API key for non-interactive login (agents/CI)')
21
-);
22
-
23
-const orgIdOpt = Options.text('org-id').pipe(
24
- Options.optional,
25
- Options.withDescription('Organization ID for non-interactive login')
26
-);
27
-
28
-const projectIdOpt = Options.text('project-id').pipe(
29
- Options.optional,
30
- Options.withDescription('Project ID for non-interactive login')
19
+const yesOpt = Options.boolean('yes').pipe(
20
+ Options.withAlias('y'),
21
+ Options.withDefault(false),
22
+ Options.withDescription('Skip org/project picker; use session defaults')
23
);
24
25
/**
35
initialOrgId: string;
36
initialProjectId: string;
37
fallbackEmail: string;
46
- /**
47
- * When true, 400/401/403 from session/info will fail the login.
48
- * Used for non-interactive login where the user provides explicit IDs.
49
- * When false, all session/info errors are non-fatal (browser login).
50
- */
51
- strictVerification: boolean;
38
+ /** When true, skip the init/switch hints and outro (shown later after org/project picker). */
39
+ skipHints?: boolean;
40
+ /** When true, skip JSON output (emitted later after org/project picker with final selection). */
41
+ skipOutput?: boolean;
42
}) =>
43
Effect.gen(function* () {
44
const ui = yield* TerminalUI;
50
initialOrgId,
51
initialProjectId,
52
fallbackEmail,
63
- strictVerification,
53
+ skipHints = false,
54
+ skipOutput = false,
55
} = params;
56
57
// Call session/info to enrich the login with org/project metadata.
67
- // In strict mode (non-interactive login), 400/401/403 are hard failures.
68
- // In non-strict mode (browser login), all errors are non-fatal since
69
- // the linked session is already authenticated.
58
+ // All errors are non-fatal (browser login) since the linked session is already authenticated.
59
const sessionInfo: SessionInfoResponse | undefined = yield* getSessionInfo({
60
baseURL,
61
apiKey: uakApiKey,
64
}).pipe(
65
Effect.catchTag('services/HttpServerError', e =>
66
Effect.gen(function* () {
78
- if (strictVerification && e.status && e.status >= 400 && e.status < 500) {
79
- return yield* Effect.fail(e);
80
- }
67
yield* Effect.logDebug(`Session info fetch failed (HTTP ${e.status ?? '?'}):`, e);
68
return undefined;
69
})
102
103
const email = sessionInfo?.org_member.email || fallbackEmail || undefined;
104
yield* ui.log.success(email ? `Logged in as ${email}` : 'Logged in successfully');
119
- yield* ui.log.info('Run `composio init` in your project directory to set up project context.');
120
- yield* ui.log.info(
121
- 'To switch your default global org/project later, run `composio orgs switch`.'
122
- );
105
+ if (!skipHints) {
106
+ yield* ui.log.info(
107
+ 'Run `composio init` in your project directory to set up project context.'
108
+ );
109
+ yield* ui.log.info(
110
+ 'To switch your default global org/project later, run `composio orgs switch`.'
111
+ );
112
+ }
113
114
// Emit structured JSON for piped/scripted consumption (agent-native)
125
- yield* ui.output(
126
- JSON.stringify({
127
- email,
128
- org_id: orgId,
129
- project_id: projectId,
130
- org_name: sessionInfo?.project.org.name ?? '',
131
- project_name: sessionInfo?.project.name ?? '',
132
- })
133
- );
115
+ if (!skipOutput) {
116
+ yield* ui.output(
117
+ JSON.stringify({
118
+ email,
119
+ org_id: orgId,
120
+ project_id: projectId,
121
+ org_name: sessionInfo?.project.org.name ?? '',
122
+ project_name: sessionInfo?.project.name ?? '',
123
+ })
124
+ );
125
+ }
126
135
- yield* ui.outro("You're all set!");
127
+ if (!skipHints) {
128
+ yield* ui.outro("You're all set!");
129
+ }
130
});
131
132
/**
143
scope: 'user' | 'project';
144
/** When true, don't open browser — just show the URL. */
145
noBrowser: boolean;
146
+ /** When true (login only), skip org/project picker and use session defaults. When false, prompt for org/project. */
147
+ skipOrgProjectPicker?: boolean;
148
}) =>
149
Effect.gen(function* () {
150
const ui = yield* TerminalUI;
220
221
yield* Effect.logDebug('UAK session info:', { xProjectId, xOrgId });
222
223
+ const willRunPicker = params.scope === 'user' && !params.skipOrgProjectPicker;
224
yield* storeCredentials({
225
baseURL: ctx.data.baseURL,
226
uakApiKey,
227
initialOrgId: xOrgId,
228
initialProjectId: xProjectId,
229
fallbackEmail: linkedSession.account.email,
233
- strictVerification: false,
230
+ skipHints: willRunPicker,
231
+ skipOutput: willRunPicker,
232
});
233
+
234
+ if (willRunPicker) {
235
+ const result = yield* runOrgProjectSelection({
236
+ apiKey: uakApiKey,
237
+ baseURL: ctx.data.baseURL,
238
+ }).pipe(
239
+ Effect.catchAll(error =>
240
+ Effect.gen(function* () {
241
+ yield* Effect.logDebug('Org/project picker failed:', error);
242
+ yield* ui.log.warn('Could not load org/project list. Using session defaults.');
243
+ return undefined;
244
+ })
245
+ )
246
+ );
247
+ if (result) {
248
+ const sessionUserId = uakSessionInfo.org_member.user_id ?? uakSessionInfo.org_member.id;
249
+ const testUserId = sessionUserId ? `pg-test-${sessionUserId}` : undefined;
250
+ yield* ctx.login(
251
+ uakApiKey,
252
+ result.org.id,
253
+ result.project.id,
254
+ testUserId ?? Option.getOrUndefined(ctx.data.testUserId)
255
+ );
256
+ yield* ui.log.success(
257
+ `Default org/project set to "${result.org.name}" / "${result.project.name}".`
258
+ );
259
+ }
260
+ // Emit JSON with final org/project (from picker or session) for piped/scripted consumption
261
+ const finalOrgId = result?.org.id ?? xOrgId;
262
+ const finalProjectId = result?.project.id ?? xProjectId;
263
+ const finalOrgName = result?.org.name ?? uakSessionInfo.project.org.name ?? '';
264
+ const finalProjectName = result?.project.name ?? uakSessionInfo.project.name ?? '';
265
+ yield* ui.output(
266
+ JSON.stringify({
267
+ email: linkedSession.account.email ?? undefined,
268
+ org_id: finalOrgId,
269
+ project_id: finalProjectId,
270
+ org_name: finalOrgName,
271
+ project_name: finalProjectName,
272
+ })
273
+ );
274
+ yield* ui.log.info(
275
+ 'Run `composio init` in your project directory to set up project context.'
276
+ );
277
+ yield* ui.log.info(
278
+ 'To switch your default global org/project later, run `composio orgs switch`.'
279
+ );
280
+ yield* ui.outro("You're all set!");
281
+ }
282
});
283
284
/**
285
* CLI command to login using Composio's CLI session APIs.
286
*
240
- * Supports two modes:
241
- * 1. Browser-based: Opens browser for OAuth flow (default)
242
- * 2. Non-interactive: Accepts --api-key, --org-id, --project-id flags for agents/CI
287
+ * Browser-based: Opens browser for OAuth flow (default).
288
+ * Use --no-browser to skip auto-opening the browser and print the URL instead.
289
+ * Use -y to skip org/project picker and use session defaults.
290
*
291
* @example
292
* ```bash
293
* composio login
294
* composio login --no-browser
248
- * composio login --api-key uak_xxx --org-id org-id --project-id proj-id
295
+ * composio login -y
296
* ```
297
*/
251
-export const loginCmd = Command.make(
252
- 'login',
253
- { noBrowser, apiKey: apiKeyOpt, orgId: orgIdOpt, projectId: projectIdOpt },
254
- ({ noBrowser, apiKey, orgId, projectId }) =>
255
- Effect.gen(function* () {
256
- const ui = yield* TerminalUI;
257
- const ctx = yield* ComposioUserContext;
258
-
259
- yield* ui.intro('composio login');
298
+export const loginCmd = Command.make('login', { noBrowser, yes: yesOpt }, ({ noBrowser, yes }) =>
299
+ Effect.gen(function* () {
300
+ const ui = yield* TerminalUI;
301
+ const ctx = yield* ComposioUserContext;
302
261
- // Non-interactive path: --api-key, --org-id, --project-id flags skip browser flow.
262
- // All three must be provided together; partial flags are an error.
263
- const nonInteractiveFlags = [apiKey, orgId, projectId];
264
- const anyProvided = nonInteractiveFlags.some(Option.isSome);
265
- const allProvided = nonInteractiveFlags.every(Option.isSome);
303
+ yield* ui.intro('composio login');
304
267
- if (anyProvided && !allProvided) {
268
- const missing = [
269
- Option.isNone(apiKey) && '--api-key',
270
- Option.isNone(orgId) && '--org-id',
271
- Option.isNone(projectId) && '--project-id',
272
- ].filter(Boolean);
273
- yield* ui.log.error(`Missing required flag(s): ${missing.join(', ')}`);
274
- yield* ui.log.info(
275
- 'Non-interactive login requires all three: --api-key, --org-id, --project-id'
305
+ if (ctx.isLoggedIn()) {
306
+ // Allow re-login when orgId/projectId are not yet set (old CLI login without multi-project support)
307
+ if (Option.isSome(ctx.data.orgId) && Option.isSome(ctx.data.projectId)) {
308
+ yield* ui.log.warn(`You're already logged in!`);
309
+ yield* ui.outro(
310
+ 'If you want to log in with a different account, please run `composio logout` first.'
311
);
277
- yield* ui.outro('');
278
- return;
279
- }
280
-
281
- // Strict verification: 400/401/403 from session/info are hard failures since
282
- // the user explicitly provided the IDs.
283
- if (
284
- allProvided &&
285
- Option.isSome(apiKey) &&
286
- Option.isSome(orgId) &&
287
- Option.isSome(projectId)
288
- ) {
289
- yield* Effect.logDebug('Non-interactive login with provided credentials');
290
- yield* storeCredentials({
291
- baseURL: ctx.data.baseURL,
292
- uakApiKey: apiKey.value,
293
- initialOrgId: orgId.value,
294
- initialProjectId: projectId.value,
295
- fallbackEmail: '',
296
- strictVerification: true,
297
- });
312
return;
313
}
314
+ yield* ui.log.step('Re-authenticating for multi-project support...');
315
+ }
316
301
- if (ctx.isLoggedIn()) {
302
- // Allow re-login when orgId/projectId are not yet set (old CLI login without multi-project support)
303
- if (Option.isSome(ctx.data.orgId) && Option.isSome(ctx.data.projectId)) {
304
- yield* ui.log.warn(`You're already logged in!`);
305
- yield* ui.log.info(
306
- `If you want to log in with a different account, please run \`composio logout\` first.`
307
- );
308
- yield* ui.outro('');
309
- return;
310
- }
311
- yield* ui.log.step('Re-authenticating for multi-project support...');
312
- }
313
-
314
- yield* browserLogin({ scope: 'user', noBrowser });
315
- })
317
+ yield* browserLogin({ scope: 'user', noBrowser, skipOrgProjectPicker: yes });
318
+ })
319
).pipe(Command.withDescription('Log in to the Composio SDK.'));