|
| 1 | +import { Option } from "effect"; |
| 2 | +import type { OutputFormat } from "../output/types.ts"; |
| 3 | + |
| 4 | +type LegacyOutputFormat = "env" | "pretty" | "json" | "toml" | "yaml"; |
| 5 | +type AgentOverride = "auto" | "yes" | "no"; |
| 6 | + |
| 7 | +interface AgentOutputOptions { |
| 8 | + readonly explicitOutputFormat: Option.Option<OutputFormat>; |
| 9 | + readonly legacyOutputFormat?: Option.Option<LegacyOutputFormat>; |
| 10 | + readonly agentOverride?: AgentOverride; |
| 11 | + readonly detectedAgentName?: Option.Option<string>; |
| 12 | + readonly isBuiltInTextRequest?: boolean; |
| 13 | +} |
| 14 | + |
| 15 | +function readLongFlag(args: ReadonlyArray<string>, name: string): string | undefined { |
| 16 | + const prefix = `${name}=`; |
| 17 | + for (let i = 0; i < args.length; i++) { |
| 18 | + const arg = args[i]; |
| 19 | + if (arg === undefined) { |
| 20 | + continue; |
| 21 | + } |
| 22 | + if (arg === name) { |
| 23 | + return args[i + 1]; |
| 24 | + } |
| 25 | + if (arg.startsWith(prefix)) { |
| 26 | + return arg.slice(prefix.length); |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +function readOutputFlag(args: ReadonlyArray<string>): string | undefined { |
| 32 | + for (let i = 0; i < args.length; i++) { |
| 33 | + const arg = args[i]; |
| 34 | + if (arg === undefined) { |
| 35 | + continue; |
| 36 | + } |
| 37 | + if (arg === "--output" || arg === "-o") { |
| 38 | + return args[i + 1]; |
| 39 | + } |
| 40 | + if (arg.startsWith("--output=")) { |
| 41 | + return arg.slice("--output=".length); |
| 42 | + } |
| 43 | + if (arg.startsWith("-o=")) { |
| 44 | + return arg.slice("-o=".length); |
| 45 | + } |
| 46 | + if (arg.length > 2 && arg.startsWith("-o")) { |
| 47 | + return arg.slice("-o".length); |
| 48 | + } |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +function outputFormatFromArg(value: string | undefined): Option.Option<OutputFormat> { |
| 53 | + switch (value) { |
| 54 | + case "text": |
| 55 | + case "json": |
| 56 | + case "stream-json": |
| 57 | + return Option.some(value); |
| 58 | + default: |
| 59 | + return Option.none(); |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +function legacyOutputFormatFromArg(value: string | undefined): Option.Option<LegacyOutputFormat> { |
| 64 | + switch (value) { |
| 65 | + case "env": |
| 66 | + case "pretty": |
| 67 | + case "json": |
| 68 | + case "toml": |
| 69 | + case "yaml": |
| 70 | + return Option.some(value); |
| 71 | + default: |
| 72 | + return Option.none(); |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +function agentOverrideFromArg(value: string | undefined): AgentOverride { |
| 77 | + switch (value) { |
| 78 | + case "yes": |
| 79 | + case "no": |
| 80 | + return value; |
| 81 | + default: |
| 82 | + return "auto"; |
| 83 | + } |
| 84 | +} |
| 85 | + |
| 86 | +function isRootValueFlag(arg: string): boolean { |
| 87 | + return ( |
| 88 | + arg === "--output-format" || |
| 89 | + arg === "--output" || |
| 90 | + arg === "-o" || |
| 91 | + arg === "--profile" || |
| 92 | + arg === "--workdir" || |
| 93 | + arg === "--network-id" || |
| 94 | + arg === "--dns-resolver" || |
| 95 | + arg === "--agent" |
| 96 | + ); |
| 97 | +} |
| 98 | + |
| 99 | +function isRootValueFlagWithInlineValue(arg: string): boolean { |
| 100 | + return ( |
| 101 | + arg.startsWith("--output-format=") || |
| 102 | + arg.startsWith("--output=") || |
| 103 | + arg.startsWith("-o=") || |
| 104 | + (arg.length > 2 && arg.startsWith("-o")) || |
| 105 | + arg.startsWith("--profile=") || |
| 106 | + arg.startsWith("--workdir=") || |
| 107 | + arg.startsWith("--network-id=") || |
| 108 | + arg.startsWith("--dns-resolver=") || |
| 109 | + arg.startsWith("--agent=") |
| 110 | + ); |
| 111 | +} |
| 112 | + |
| 113 | +function isRootBooleanFlag(arg: string): boolean { |
| 114 | + return ( |
| 115 | + arg === "--debug" || arg === "--experimental" || arg === "--yes" || arg === "--create-ticket" |
| 116 | + ); |
| 117 | +} |
| 118 | + |
| 119 | +function hasRootVersionRequest(args: ReadonlyArray<string>): boolean { |
| 120 | + for (let i = 0; i < args.length; i++) { |
| 121 | + const arg = args[i]; |
| 122 | + if (arg === undefined || arg === "--") { |
| 123 | + return false; |
| 124 | + } |
| 125 | + if (arg === "--version" || arg === "-v") { |
| 126 | + return true; |
| 127 | + } |
| 128 | + if (isRootValueFlag(arg)) { |
| 129 | + i++; |
| 130 | + continue; |
| 131 | + } |
| 132 | + if (isRootValueFlagWithInlineValue(arg) || isRootBooleanFlag(arg)) { |
| 133 | + continue; |
| 134 | + } |
| 135 | + return false; |
| 136 | + } |
| 137 | + return false; |
| 138 | +} |
| 139 | + |
| 140 | +function hasHelpRequest(args: ReadonlyArray<string>): boolean { |
| 141 | + for (const arg of args) { |
| 142 | + if (arg === "--") return false; |
| 143 | + if (arg === "--help" || arg === "-h") return true; |
| 144 | + } |
| 145 | + return false; |
| 146 | +} |
| 147 | + |
| 148 | +export function isBuiltInTextRequest(args: ReadonlyArray<string>): boolean { |
| 149 | + return hasHelpRequest(args) || hasRootVersionRequest(args); |
| 150 | +} |
| 151 | + |
| 152 | +export function resolveAgentOutputFormat(options: AgentOutputOptions): OutputFormat { |
| 153 | + const legacyOutputFormat = options.legacyOutputFormat ?? Option.none<LegacyOutputFormat>(); |
| 154 | + const detectedAgentName = options.detectedAgentName ?? Option.none<string>(); |
| 155 | + const agentOverride = options.agentOverride ?? "auto"; |
| 156 | + const isCodingAgent = |
| 157 | + agentOverride === "yes" || (agentOverride === "auto" && Option.isSome(detectedAgentName)); |
| 158 | + |
| 159 | + return Option.getOrElse(options.explicitOutputFormat, () => |
| 160 | + isCodingAgent && Option.isNone(legacyOutputFormat) && !options.isBuiltInTextRequest |
| 161 | + ? "json" |
| 162 | + : "text", |
| 163 | + ); |
| 164 | +} |
| 165 | + |
| 166 | +export function resolveAgentOutputFormatFromArgs( |
| 167 | + args: ReadonlyArray<string>, |
| 168 | + detectedAgentName: Option.Option<string>, |
| 169 | +): OutputFormat { |
| 170 | + const explicitOutputFormat = outputFormatFromArg(readLongFlag(args, "--output-format")); |
| 171 | + const legacyOutputFormat = legacyOutputFormatFromArg(readOutputFlag(args)); |
| 172 | + const agentOverride = agentOverrideFromArg(readLongFlag(args, "--agent")); |
| 173 | + |
| 174 | + return resolveAgentOutputFormat({ |
| 175 | + explicitOutputFormat, |
| 176 | + legacyOutputFormat, |
| 177 | + agentOverride, |
| 178 | + detectedAgentName, |
| 179 | + isBuiltInTextRequest: isBuiltInTextRequest(args), |
| 180 | + }); |
| 181 | +} |
0 commit comments