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 113 lines 4.5 KB
Raw
1 import { Array as Arr } from 'effect';
2 import type { OrganizationSummary } from 'src/services/composio-account';
3
4 // --- usage messages ---------------------------------------------------------------------------
5
6 export const KEY_AND_USER_API_KEY_MESSAGE = 'Use either `--key` or `--user-api-key`, not both.';
7 export const POLL_WITH_OTHER_FLAGS_MESSAGE =
8 '`--poll` cannot be combined with browser, session, direct-login, or agent flags.';
9 export const AGENT_WITH_OTHER_FLAGS_MESSAGE =
10 '`--agent` cannot be combined with browser, session, or direct-login flags.';
11 export const ORG_WITHOUT_USER_API_KEY_MESSAGE = '`--org` requires `--user-api-key`.';
12 export const USER_API_KEY_WITH_SESSION_FLAGS_MESSAGE =
13 '`--user-api-key` is a direct login path and cannot be combined with browser or session flags.';
14 export const TIMEOUT_WITHOUT_POLL_MESSAGE = '`--timeout` requires `--poll`.';
15
16 /** `--timeout` names something other than a whole number of seconds a pending login can last. */
17 export const invalidTimeoutMessage = (maxSeconds: number) =>
18 `Invalid --timeout value. Use a whole number of seconds from 1 to ${maxSeconds}.`;
19
20 // --- stderr -----------------------------------------------------------------------------------
21
22 export const ALREADY_LOGGED_IN_MESSAGE = `You're already logged in!`;
23 export const ALREADY_LOGGED_IN_OUTRO =
24 'If you want to log in with a different account, please run `composio logout` first.';
25 export const REAUTHENTICATING_MESSAGE = 'Re-authenticating for multi-project support...';
26
27 export const SESSION_NOT_FOUND_MESSAGE =
28 'Session not found or expired. Run `composio login --no-wait` to get a new session.';
29 export const SESSION_NOT_LINKED_MESSAGE =
30 'Login not complete. Open the URL and finish authentication.';
31 export const SESSION_NOT_LINKED_HINT = 'Then run `composio login --key <key>` again.';
32
33 /**
34 * What a bounded wait answers when it runs out on a session that is still pending: the URL that
35 * finishes the login, and the command that picks the same login up again. The URL and the
36 * command are part of the message because a caller reading stderr through a pipe gets no
37 * decoration and no spinner — this text is the whole answer.
38 */
39 export const formatLoginStillPending = (params: {
40 readonly status: string;
41 readonly loginUrl: string;
42 readonly resumeCommand: string;
43 }) => `Login still pending: session status is '${params.status}', waiting for 'linked'.
44
45 Open this URL and finish authenticating:
46
47 ${params.loginUrl}
48
49 Then run this again to complete the login:
50
51 ${params.resumeCommand}`;
52
53 export const organizationNotFoundMessage = (requestedOrg: string) =>
54 `Organization "${requestedOrg}" was not found for this API key.`;
55 export const INVALID_ORGANIZATION_MESSAGE =
56 'Invalid organization. Run `composio orgs list` to inspect available orgs.';
57
58 /** What `composio login --poll` completed: the account, its current org, and the others. */
59 export type PollLoginSummary = {
60 readonly email?: string;
61 readonly defaultOrgId: string;
62 readonly defaultOrgName?: string;
63 readonly organizations: ReadonlyArray<OrganizationSummary>;
64 };
65
66 const SWITCH_ORG_COMMAND = 'composio orgs switch --org-id <org_id>';
67
68 /** The note `composio login --poll` prints once the login completes. */
69 export const formatPollLoginComplete = (summary: PollLoginSummary) => {
70 const orgLines = Arr.match(summary.organizations, {
71 onEmpty: () => ' No organizations were returned for this account.',
72 onNonEmpty: organizations =>
73 organizations
74 .map(org => ` ${org.id === summary.defaultOrgId ? '*' : '-'} ${org.name} (${org.id})`)
75 .join('\n'),
76 });
77
78 return `Login complete${summary.email ? ` for ${summary.email}` : ''}.
79
80 Current org:
81
82 ${summary.defaultOrgName ?? summary.defaultOrgId} (${summary.defaultOrgId})
83
84 Available organizations:
85
86 ${orgLines}
87
88 The CLI selected the first organization as your current org. To choose a different current org, run:
89
90 ${SWITCH_ORG_COMMAND}`;
91 };
92
93 // --- stdout -----------------------------------------------------------------------------------
94
95 /** The stdout payload of `composio login --poll`. */
96 export const formatPollLoginJson = (summary: PollLoginSummary) =>
97 JSON.stringify(
98 {
99 email: summary.email ?? null,
100 current_org: {
101 id: summary.defaultOrgId,
102 name: summary.defaultOrgName ?? summary.defaultOrgId,
103 },
104 organizations: summary.organizations.map(org => ({
105 id: org.id,
106 name: org.name,
107 selected: org.id === summary.defaultOrgId,
108 })),
109 switch_command: SWITCH_ORG_COMMAND,
110 },
111 null,
112 2
113 );