@devries-cameron20 / composio Mirror · read-only
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.

Mirrored from https://github.com/Camo67/composio.git

Synced Nov 21, 04:45 UTC

next
ts 61 lines 2.2 KB
Raw
1 import { Effect, Option } from 'effect';
2 import { notLoggedIn } from 'src/commands/errors';
3 import { linkAnalyticsIdentityForOrg } from 'src/effects/link-analytics-identity';
4 import { runOrgSelection } from 'src/effects/select-org-project';
5 import { TerminalUI } from 'src/services/terminal-ui';
6 import { ComposioUserContext } from 'src/services/user-context';
7 import {
8 CURRENT_ORG_NOTE,
9 CURRENT_ORG_NOTE_TITLE,
10 NO_ORG_SELECTED_OUTRO,
11 ORGS_SWITCH_INTRO,
12 ORG_UPDATED_OUTRO,
13 formatSwitchedOrgJson,
14 updatedOrgMessage,
15 } from './orgs.switch.format';
16
17 /** The flags of `composio orgs switch`. */
18 export type OrgsSwitchParams = {
19 readonly orgId: Option.Option<string>;
20 readonly limit: number;
21 };
22
23 /**
24 * Stores `--org-id`, or the org picked from the user's organizations, as the global default.
25 * The stored API key and test user id are kept; analytics is linked to the new membership.
26 */
27 export const runOrgsSwitch = ({ orgId, limit }: OrgsSwitchParams) =>
28 Effect.gen(function* () {
29 const ui = yield* TerminalUI;
30 const ctx = yield* ComposioUserContext;
31 // `requiresLogin` on the `orgs` group has already proven the key is there; the
32 // filter also fails a stored empty key, which the login check counts as present.
33 const apiKey = yield* Effect.fromOption(
34 Option.filter(ctx.data.apiKey, key => key.length > 0),
35 notLoggedIn
36 );
37
38 yield* ui.intro(ORGS_SWITCH_INTRO);
39 yield* ui.note(CURRENT_ORG_NOTE, CURRENT_ORG_NOTE_TITLE);
40
41 const selected = yield* runOrgSelection({
42 apiKey,
43 explicitOrgId: Option.getOrUndefined(orgId),
44 currentOrgId: Option.getOrUndefined(ctx.data.orgId),
45 limit,
46 });
47
48 yield* Option.match(Option.fromUndefinedOr(selected), {
49 onNone: () => ui.outro(NO_ORG_SELECTED_OUTRO),
50 onSome: org =>
51 Effect.gen(function* () {
52 // Same key, new org: the login's recorded backend stays as it is.
53 yield* ctx.update({ orgId: Option.some(org.id) });
54 yield* linkAnalyticsIdentityForOrg({ apiKey, orgId: org.id });
55
56 yield* ui.log.success(updatedOrgMessage(org.name));
57 yield* ui.output(formatSwitchedOrgJson(org.id));
58 yield* ui.outro(ORG_UPDATED_OUTRO);
59 }),
60 });
61 });