@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 74 lines 2.98 KB
Raw
1 import { Option } from 'effect';
2
3 /** The environment variable that chose the backend, when one did. */
4 export type BackendVariable = 'COMPOSIO_BASE_URL' | 'COMPOSIO_ENVIRONMENT';
5
6 /** The backend the environment selects (`APP_CONFIG.BACKEND`). */
7 export interface EnvBackend {
8 /** `COMPOSIO_BASE_URL`, else the `COMPOSIO_ENVIRONMENT` default, else production. */
9 readonly baseURL: string;
10 /** `COMPOSIO_WEB_URL`, else the `COMPOSIO_ENVIRONMENT` default, else production. */
11 readonly webURL: string;
12 /** `COMPOSIO_BASE_URL` when set, else `COMPOSIO_ENVIRONMENT` when set. */
13 readonly selectedBy: Option.Option<BackendVariable>;
14 /** Whether `COMPOSIO_WEB_URL` is set. */
15 readonly webURLSet: boolean;
16 }
17
18 /** The backend a stored login was issued by, as `composio login` recorded it. */
19 export interface StoredBackend {
20 readonly baseURL: string;
21 readonly webURL: Option.Option<string>;
22 }
23
24 export interface Backend {
25 readonly baseURL: string;
26 readonly webURL: string;
27 }
28
29 /**
30 * The backend the CLI talks to. An env var that selects a backend wins; otherwise a stored
31 * login keeps using the backend that issued its key, so a staging login keeps calling staging
32 * after `COMPOSIO_ENVIRONMENT` is unset. Pass `Option.none()` as `stored` for a key that is not
33 * the stored login's (`COMPOSIO_USER_API_KEY`) or when nobody is logged in.
34 */
35 export const resolveBackend = (env: EnvBackend, stored: Option.Option<StoredBackend>): Backend => {
36 const login = Option.isSome(env.selectedBy) ? Option.none<StoredBackend>() : stored;
37 return {
38 baseURL: Option.match(login, { onNone: () => env.baseURL, onSome: s => s.baseURL }),
39 webURL: env.webURLSet
40 ? env.webURL
41 : Option.flatMap(login, s => s.webURL).pipe(Option.getOrElse(() => env.webURL)),
42 };
43 };
44
45 export interface BackendMismatch {
46 readonly variable: BackendVariable;
47 /** The backend that issued the stored key. */
48 readonly issuedBy: string;
49 /** The backend the env var sends it to instead. */
50 readonly selected: string;
51 }
52
53 export const withoutTrailingSlashes = (url: string) => url.replace(/\/+$/u, '');
54
55 /** Set when an env var sends the stored login's key to a backend other than its issuer. */
56 export const backendMismatch = (
57 env: EnvBackend,
58 stored: Option.Option<StoredBackend>
59 ): Option.Option<BackendMismatch> =>
60 Option.flatMap(stored, login =>
61 Option.flatMap(env.selectedBy, variable =>
62 withoutTrailingSlashes(login.baseURL) === withoutTrailingSlashes(env.baseURL)
63 ? Option.none()
64 : Option.some({
65 variable,
66 issuedBy: withoutTrailingSlashes(login.baseURL),
67 selected: withoutTrailingSlashes(env.baseURL),
68 })
69 )
70 );
71
72 export const formatBackendMismatch = (mismatch: BackendMismatch) =>
73 `${mismatch.variable} sends your stored API key to ${mismatch.selected}, but it was issued by ${mismatch.issuedBy}. ` +
74 `Unset ${mismatch.variable} to use the backend you logged in to, or run \`composio logout && composio login\` to log in to ${mismatch.selected} instead.`;