@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 48 lines 1.59 KB
Raw
1 import { Context, Effect, Layer, Option } from 'effect';
2 import { formatBackendMismatch } from 'src/config/backend';
3 import type { InvocationHookShape } from './invocation-hook';
4 import { TerminalUI } from './terminal-ui';
5 import { ComposioUserContext } from './user-context';
6
7 export type BackendMismatchWarningShape = InvocationHookShape;
8
9 /**
10 * Warns, before any request, when an env var sends the stored key to another backend: as a
11 * decorated warning on a terminal, as a plain `Warning:` line on a redirected stderr.
12 */
13 export class BackendMismatchWarning extends Context.Service<
14 BackendMismatchWarning,
15 BackendMismatchWarningShape
16 >()('services/BackendMismatchWarning') {
17 static readonly Default: Layer.Layer<
18 BackendMismatchWarning,
19 never,
20 ComposioUserContext | TerminalUI
21 > = Layer.effect(
22 BackendMismatchWarning,
23 Effect.gen(function* () {
24 const ctx = yield* ComposioUserContext;
25 const ui = yield* TerminalUI;
26
27 const warn = (message: string) =>
28 Effect.gen(function* () {
29 if ((yield* ui.capabilities).canDecorate) {
30 yield* ui.log.warn(message);
31 } else {
32 yield* ui.error(`Warning: ${message}`);
33 }
34 });
35
36 return BackendMismatchWarning.of({
37 // `backendMismatch` is read per run: it reflects the environment and stored login now.
38 run: () =>
39 Effect.suspend(() =>
40 Option.match(ctx.backendMismatch, {
41 onNone: () => Effect.void,
42 onSome: mismatch => warn(formatBackendMismatch(mismatch)),
43 })
44 ),
45 });
46 })
47 );
48 }