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 * as BunServices from '@effect/platform-bun/BunServices'; |
| 2 | import { Effect, Stream } from 'effect'; |
| 3 | import { ChildProcess, ChildProcessSpawner } from 'effect/process'; |
| 4 | import { HOST_CONFIG, type CallerAgentSignals } from 'src/config'; |
| 5 | |
| 6 | export type PermissionCallerAgent = 'claude' | 'codex' | 'openclaw' | 'composio'; |
| 7 | |
| 8 | export const isInteractivePermissionUiDisabled: Effect.Effect<boolean> = Effect.orDie( |
| 9 | HOST_CONFIG.INTERACTIVE_PERMISSION_UI_DISABLED |
| 10 | ); |
| 11 | |
| 12 | // Enhanced controls are not offered on Intel Macs (#3421). |
| 13 | export const isEnhancedControlsPlatformSupported = (): boolean => |
| 14 | !(process.platform === 'darwin' && process.arch === 'x64'); |
| 15 | |
| 16 | const normalizeCallerAgent = (value?: string): PermissionCallerAgent | undefined => { |
| 17 | const normalized = value?.toLowerCase().replace(/[^a-z]/g, ''); |
| 18 | if (normalized === 'claude' || normalized === 'codex' || normalized === 'openclaw') { |
| 19 | return normalized; |
| 20 | } |
| 21 | return undefined; |
| 22 | }; |
| 23 | |
| 24 | const PS_TREE_MAX_DEPTH = 8; |
| 25 | |
| 26 | // Mirrors the former execFileSync('ps', ...) lookup: any spawn failure (or a |
| 27 | // non-zero exit, which leaves stdout empty) resolves to undefined. |
| 28 | const psParentEntry = ( |
| 29 | pid: number |
| 30 | ): Effect.Effect<string | undefined, never, ChildProcessSpawner.ChildProcessSpawner> => |
| 31 | Effect.scoped( |
| 32 | Effect.gen(function* () { |
| 33 | // The child never reads interactive input: hand it an immediately-closed |
| 34 | // stdin pipe (EOF), matching the previous `stdio: ['ignore', ...]` spawn. |
| 35 | const handle = yield* ChildProcess.make( |
| 36 | 'ps', |
| 37 | ['-o', 'ppid=', '-o', 'comm=', '-p', String(pid)], |
| 38 | { stdin: Stream.empty, extendEnv: true } |
| 39 | ); |
| 40 | const output = yield* Stream.mkString(Stream.decodeText(handle.stdout)); |
| 41 | return output.trim(); |
| 42 | }) |
| 43 | ).pipe(Effect.orElseSucceed(() => undefined)); |
| 44 | |
| 45 | const detectCallerAgentFromProcessTree: Effect.Effect< |
| 46 | PermissionCallerAgent | undefined, |
| 47 | never, |
| 48 | ChildProcessSpawner.ChildProcessSpawner |
| 49 | > = Effect.gen(function* () { |
| 50 | if (process.platform === 'win32') return undefined; |
| 51 | |
| 52 | let pid = process.ppid; |
| 53 | for (let depth = 0; depth < PS_TREE_MAX_DEPTH && pid > 1; depth += 1) { |
| 54 | const output = yield* psParentEntry(pid); |
| 55 | if (output === undefined || output === '') return undefined; |
| 56 | |
| 57 | const match = output.match(/^(\d+)\s+(.+)$/); |
| 58 | if (!match) return undefined; |
| 59 | |
| 60 | const command = match[2]?.toLowerCase() ?? ''; |
| 61 | if (command.includes('openclaw') || command.includes('open-claw')) return 'openclaw'; |
| 62 | if (command.includes('claude')) return 'claude'; |
| 63 | if (command.includes('codex')) return 'codex'; |
| 64 | |
| 65 | pid = Number(match[1]); |
| 66 | } |
| 67 | |
| 68 | return undefined; |
| 69 | }); |
| 70 | |
| 71 | const detectCallerAgentFromSignals = ( |
| 72 | signals: CallerAgentSignals |
| 73 | ): PermissionCallerAgent | undefined => { |
| 74 | const explicit = normalizeCallerAgent(signals.explicit); |
| 75 | if (explicit) return explicit; |
| 76 | |
| 77 | if (signals.openclaw) return 'openclaw'; |
| 78 | if (signals.claude) return 'claude'; |
| 79 | if (signals.codex) return 'codex'; |
| 80 | |
| 81 | return undefined; |
| 82 | }; |
| 83 | |
| 84 | export const detectPermissionCallerAgentEffect = ( |
| 85 | providedSignals?: CallerAgentSignals |
| 86 | ): Effect.Effect<PermissionCallerAgent, never, ChildProcessSpawner.ChildProcessSpawner> => |
| 87 | Effect.gen(function* () { |
| 88 | const signals = providedSignals ?? (yield* Effect.orDie(HOST_CONFIG.CALLER_AGENT_SIGNALS)); |
| 89 | const fromEnv = detectCallerAgentFromSignals(signals); |
| 90 | if (fromEnv !== undefined) return fromEnv; |
| 91 | |
| 92 | return (yield* detectCallerAgentFromProcessTree) ?? 'composio'; |
| 93 | }); |
| 94 | |
| 95 | export const detectPermissionCallerAgent = (): Promise<PermissionCallerAgent> => |
| 96 | Effect.runPromise(Effect.provide(detectPermissionCallerAgentEffect(), BunServices.layer)); |