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
| 1 | import { Context, Effect, Layer, Option } from 'effect'; |
| 2 | import { Flag, GlobalFlag } from 'effect/cli'; |
| 3 | import { DEBUG_CONFIG } from 'src/config'; |
| 4 | |
| 5 | export const TELEMETRY_DEBUG_FLAG = '--telemetry-debug'; |
| 6 | |
| 7 | /** |
| 8 | * Read telemetry debugging before constructing services that emit lifecycle events. |
| 9 | * The caller excludes the run script tail. Effect still validates this hidden global flag; |
| 10 | * bootstrap never removes it from argv. |
| 11 | */ |
| 12 | export const readTelemetryDebugOverride = (argv: ReadonlyArray<string>): boolean | undefined => { |
| 13 | for (const token of argv) { |
| 14 | if (token === '--') break; |
| 15 | if (token === TELEMETRY_DEBUG_FLAG || token === `${TELEMETRY_DEBUG_FLAG}=true`) return true; |
| 16 | if (token === '--no-telemetry-debug' || token === `${TELEMETRY_DEBUG_FLAG}=false`) return false; |
| 17 | } |
| 18 | return undefined; |
| 19 | }; |
| 20 | |
| 21 | /** |
| 22 | * Values parsed from the hidden `--perf-debug` / `--tool-debug` flags. |
| 23 | * |
| 24 | * `undefined` means "the flag was absent", which is what lets the corresponding `COMPOSIO_*` |
| 25 | * config value through; an explicit `true`/`false` always wins over it. |
| 26 | */ |
| 27 | export type CliDebugFlagOverrides = { |
| 28 | readonly perfDebug: boolean | undefined; |
| 29 | readonly toolDebug: boolean | undefined; |
| 30 | }; |
| 31 | |
| 32 | export const NO_CLI_DEBUG_FLAG_OVERRIDES: CliDebugFlagOverrides = { |
| 33 | perfDebug: undefined, |
| 34 | toolDebug: undefined, |
| 35 | }; |
| 36 | |
| 37 | /** |
| 38 | * Hidden debug flags of the current invocation. |
| 39 | * |
| 40 | * The command framework parses global settings without rewriting argv. This service supplies |
| 41 | * overrides for direct Effect callers that do not run through the command parser. |
| 42 | */ |
| 43 | export class CliDebugFlags extends Context.Service<CliDebugFlags, CliDebugFlagOverrides>()( |
| 44 | 'services/CliDebugFlags' |
| 45 | ) {} |
| 46 | |
| 47 | export const cliDebugFlagsLayer = ( |
| 48 | overrides: CliDebugFlagOverrides = NO_CLI_DEBUG_FLAG_OVERRIDES |
| 49 | ): Layer.Layer<CliDebugFlags> => Layer.succeed(CliDebugFlags, overrides); |
| 50 | |
| 51 | const debugSetting = <const Name extends string>(name: Name) => |
| 52 | GlobalFlag.Setting(name)({ flag: Flag.Boolean(name).pipe(Flag.optional, Flag.withHidden) }); |
| 53 | |
| 54 | const debugSettings = { |
| 55 | 'perf-debug': debugSetting('perf-debug'), |
| 56 | 'tool-debug': debugSetting('tool-debug'), |
| 57 | 'telemetry-debug': debugSetting('telemetry-debug'), |
| 58 | }; |
| 59 | export const CLI_DEBUG_FLAG_NAMES = Object.keys(debugSettings); |
| 60 | export const CLI_DEBUG_GLOBAL_FLAGS = Object.values(debugSettings); |
| 61 | |
| 62 | const debugFlagOr = <const Name extends string>( |
| 63 | setting: GlobalFlag.Setting<Name, Option.Option<boolean>>, |
| 64 | select: (overrides: CliDebugFlagOverrides) => boolean | undefined, |
| 65 | configured: Effect.Effect<boolean, never, never> |
| 66 | ): Effect.Effect<boolean, never, CliDebugFlags> => |
| 67 | Effect.gen(function* () { |
| 68 | const parsed = Option.flatten(yield* Effect.serviceOption(setting)); |
| 69 | if (Option.isSome(parsed)) return parsed.value; |
| 70 | const overrides = yield* CliDebugFlags; |
| 71 | return select(overrides) ?? (yield* configured); |
| 72 | }); |
| 73 | |
| 74 | export const isPerfDebugEnabled = debugFlagOr( |
| 75 | debugSettings['perf-debug'], |
| 76 | overrides => overrides.perfDebug, |
| 77 | Effect.orDie(DEBUG_CONFIG.PERF_DEBUG) |
| 78 | ); |
| 79 | |
| 80 | export const isToolDebugEnabled = debugFlagOr( |
| 81 | debugSettings['tool-debug'], |
| 82 | overrides => overrides.toolDebug, |
| 83 | Effect.orDie(DEBUG_CONFIG.TOOL_DEBUG) |
| 84 | ); |
| 85 | |
| 86 | /** |
| 87 | * Debug state a parent CLI process resolved for the processes it spawns. |
| 88 | * |
| 89 | * Flags reach the parent as hidden CLI options or `COMPOSIO_*` variables, but a child only sees |
| 90 | * the environment, so every spawn site has to serialize the resolved values back into it. Keeping |
| 91 | * the mapping here means a flag added to this file cannot be forwarded from one spawn site and |
| 92 | * forgotten at another. |
| 93 | */ |
| 94 | export type ChildProcessDebugFlags = { |
| 95 | readonly perfDebug: boolean; |
| 96 | readonly toolDebug: boolean; |
| 97 | readonly telemetryDebug: boolean; |
| 98 | }; |
| 99 | |
| 100 | export const debugFlagsToChildEnv = (flags: ChildProcessDebugFlags): Record<string, string> => ({ |
| 101 | COMPOSIO_PERF_DEBUG: flags.perfDebug ? '1' : '0', |
| 102 | COMPOSIO_TOOL_DEBUG: flags.toolDebug ? '1' : '0', |
| 103 | COMPOSIO_CLI_TELEMETRY_DEBUG: flags.telemetryDebug ? '1' : '0', |
| 104 | }); |
| 105 | |
| 106 | /** Telemetry debug override for service construction and internal worker invocations. */ |
| 107 | export class TelemetryDebugMode extends Context.Service<TelemetryDebugMode, boolean>()( |
| 108 | 'services/TelemetryDebugMode' |
| 109 | ) {} |
| 110 | |
| 111 | export const telemetryDebugModeLayer = (enabled: boolean): Layer.Layer<TelemetryDebugMode> => |
| 112 | Layer.succeed(TelemetryDebugMode, enabled); |
| 113 | |
| 114 | export const isTelemetryDebugEnabled: Effect.Effect<boolean> = Effect.gen(function* () { |
| 115 | const parsed = Option.flatten(yield* Effect.serviceOption(debugSettings['telemetry-debug'])); |
| 116 | if (Option.isSome(parsed)) return parsed.value; |
| 117 | const bootstrap = yield* Effect.serviceOption(TelemetryDebugMode); |
| 118 | return yield* Option.match(bootstrap, { |
| 119 | onSome: Effect.succeed, |
| 120 | onNone: () => Effect.orDie(DEBUG_CONFIG.TELEMETRY_DEBUG), |
| 121 | }); |
| 122 | }); |