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

next
ts 109 lines 4.7 KB
Raw
1 import { Command } from 'effect/cli';
2 import { Effect, Option } from 'effect';
3 import { notLoggedIn } from 'src/commands/errors';
4 import { getSessionInfoByUserApiKey } from 'src/services/composio-account';
5 import { linkApolloIdentityForAnalytics } from 'src/analytics/dispatch';
6 import { ComposioUserContext } from 'src/services/user-context';
7 import { TerminalUI } from 'src/services/terminal-ui';
8 import { commandHintStep } from 'src/services/command-hints';
9 import { readStoredAgentIdentity } from 'src/services/agents';
10 import { getOrgEnhancedControlsStatus } from 'src/services/tool-permissions';
11 import { handleClientApiError } from 'src/effects/handle-http-error';
12 import { CLIENT_API_ERROR_TAGS } from 'src/services/composio-errors';
13
14 /**
15 * CLI command to display your account information.
16 * Never prints or exposes API keys. Without a login it fails with `NotLoggedInError`. When the
17 * account cannot be read (a rejected key, no connection, a server error) it reports why and
18 * fails, so its exit code tells whether the login works.
19 *
20 * @example
21 * ```bash
22 * composio whoami
23 * ```
24 */
25 export const whoamiCmd = Command.make('whoami', {}).pipe(
26 Command.withDescription('Display your account information.'),
27 Command.withHandler(() =>
28 Effect.gen(function* () {
29 const ui = yield* TerminalUI;
30 const ctx = yield* ComposioUserContext;
31
32 yield* ctx.data.apiKey.pipe(
33 Option.match({
34 onNone: () => Effect.fail(notLoggedIn()),
35 onSome: apiKey =>
36 Effect.gen(function* () {
37 // A session the CLI cannot fully read still proves the key works, so its fields
38 // print as unknown. Any other failure (a rejected key, no connection, a 5xx)
39 // means this command cannot vouch for the login, so it fails.
40 const sessionInfo = yield* getSessionInfoByUserApiKey({
41 userApiKey: apiKey,
42 // Reflect the org selected via `composio orgs switch`, not the key's home org.
43 orgId: Option.getOrUndefined(ctx.data.orgId),
44 }).pipe(
45 Effect.map(Option.some),
46 Effect.catchTag('services/HttpDecodingError', () => Effect.succeedNone),
47 Effect.catchTag(
48 CLIENT_API_ERROR_TAGS,
49 handleClientApiError(ui, {
50 fallbackMessage: 'Could not read your account from Composio.',
51 hint: 'Check your network connection, then retry:\n> composio whoami',
52 })
53 )
54 );
55 yield* Option.match(sessionInfo, {
56 onNone: () => Effect.void,
57 onSome: info => linkApolloIdentityForAnalytics(info.org_member.id, apiKey),
58 });
59 const email = Option.map(sessionInfo, info => info.org_member.email).pipe(
60 Option.getOrUndefined
61 );
62 const orgName = Option.map(sessionInfo, info => info.project.org.name).pipe(
63 Option.getOrUndefined
64 );
65 const enhancedControls = yield* Option.match(sessionInfo, {
66 onNone: () => Effect.succeed(undefined),
67 onSome: info =>
68 getOrgEnhancedControlsStatus({
69 orgId: info.project.org.id,
70 projectId: info.project.nano_id,
71 }),
72 });
73 const storedAgent = yield* readStoredAgentIdentity;
74 const isStoredAgentKey = Option.match(storedAgent, {
75 onNone: () => false,
76 onSome: agent => agent.composio?.user_api_key === apiKey,
77 });
78 const accountType =
79 isStoredAgentKey || email?.endsWith('@agent.composio.ai') ? 'agent' : 'human';
80
81 yield* ui.note(
82 [
83 `Type: ${accountType}`,
84 `Email: ${email ?? 'unknown'}`,
85 `Current Org: ${orgName ?? 'unknown'}`,
86 `Enhanced Controls: ${enhancedControls?.remoteEnabled ? 'on' : 'off'}`,
87 ].join('\n'),
88 'Global User Context'
89 );
90 yield* ui.log.step(
91 [
92 commandHintStep('To switch orgs', 'root.orgs.switch'),
93 commandHintStep('To set up developer project context', 'dev.init'),
94 ].join('\n\n')
95 );
96 yield* ui.output(
97 JSON.stringify({
98 account_type: accountType,
99 email: email ?? null,
100 current_org_name: orgName ?? null,
101 enhanced_controls_enabled: enhancedControls?.remoteEnabled ?? null,
102 })
103 );
104 }),
105 })
106 );
107 })
108 )
109 );