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 { Array as Arr } from 'effect'; |
| 2 | import { RUN_FILE_FLAGS, RUN_KNOWN_BOOLEAN_FLAGS, RUN_KNOWN_VALUE_FLAGS } from './run/run.cmd'; |
| 3 | import { rootCommandIndex } from 'src/utils/cli-args'; |
| 4 | |
| 5 | export const normalizeListenStreamFlag = (argv: ReadonlyArray<string>): ReadonlyArray<string> => { |
| 6 | const head = Arr.take(argv, 2); |
| 7 | const args = Arr.drop(argv, 2); |
| 8 | const isListen = args[rootCommandIndex(args)] === 'listen'; |
| 9 | if (!isListen) { |
| 10 | return argv; |
| 11 | } |
| 12 | |
| 13 | // Effect string flags require a value. An empty path means stream the whole payload. |
| 14 | return Arr.appendAll( |
| 15 | head, |
| 16 | Arr.flatMap(args, (token, index) => { |
| 17 | const next = args[index + 1]; |
| 18 | return token === '--stream' && (next === undefined || next.startsWith('-')) |
| 19 | ? ['--stream', ''] |
| 20 | : [token]; |
| 21 | }) |
| 22 | ); |
| 23 | }; |
| 24 | |
| 25 | // A token shaped like a lone option (`--name`, `--name=value`, `-n`), as opposed to source code. |
| 26 | const FLAG_SHAPED_TOKEN = /^--?[A-Za-z][A-Za-z0-9_-]*(=|$)/; |
| 27 | |
| 28 | // Preserve the legacy undelimited script syntax by inserting Effect's native `--` |
| 29 | // before the script tail. The framework then parses every operand itself. |
| 30 | // |
| 31 | // Without `--file` the first operand is inline source. An unknown option in that position is |
| 32 | // left ahead of the boundary so the framework rejects it by name instead of running it as code. |
| 33 | // Source that really starts that way needs an explicit `--`. |
| 34 | export const normalizeRunScriptArgs = (argv: ReadonlyArray<string>): ReadonlyArray<string> => { |
| 35 | const commandIndex = 2 + rootCommandIndex(argv.slice(2)); |
| 36 | if (argv[commandIndex] !== 'run') return argv; |
| 37 | |
| 38 | let index = commandIndex + 1; |
| 39 | let hasFile = false; |
| 40 | while (index < argv.length) { |
| 41 | const token = argv[index]; |
| 42 | const equalsIndex = token.indexOf('='); |
| 43 | const flagName = equalsIndex === -1 ? token : token.slice(0, equalsIndex); |
| 44 | if (RUN_KNOWN_VALUE_FLAGS.has(flagName)) { |
| 45 | hasFile ||= RUN_FILE_FLAGS.has(flagName); |
| 46 | index += equalsIndex === -1 ? 2 : 1; |
| 47 | } else if (RUN_KNOWN_BOOLEAN_FLAGS.has(flagName)) { |
| 48 | index += 1; |
| 49 | } else { |
| 50 | break; |
| 51 | } |
| 52 | } |
| 53 | if (index >= argv.length) return argv; |
| 54 | if (!hasFile && FLAG_SHAPED_TOKEN.test(argv[index])) return argv; |
| 55 | |
| 56 | const tail = argv.slice(index); |
| 57 | // The first user delimiter marks the script boundary; subsequent delimiters |
| 58 | // are script arguments. Move that boundary ahead of all script operands. |
| 59 | const separator = tail.indexOf('--'); |
| 60 | if (separator !== -1) tail.splice(separator, 1); |
| 61 | return [...argv.slice(0, index), '--', ...tail]; |
| 62 | }; |