|
| 1 | +import Foundation |
| 2 | +import GitHubCopilotService |
| 3 | +import Logger |
| 4 | +import Terminal |
| 5 | + |
| 6 | +public struct HeaderValueParser { |
| 7 | + public enum Placeholder: String { |
| 8 | + case gitHubCopilotOBearerToken = "github_copilot_bearer_token" |
| 9 | + case apiKey = "api_key" |
| 10 | + case modelName = "model_name" |
| 11 | + } |
| 12 | + |
| 13 | + public struct Context { |
| 14 | + public var modelName: String |
| 15 | + public var apiKey: String |
| 16 | + public var gitHubCopilotToken: () async -> GitHubCopilotExtension.Token? |
| 17 | + public var shellEnvironmentVariable: (_ key: String) async -> String? |
| 18 | + |
| 19 | + public init( |
| 20 | + modelName: String, |
| 21 | + apiKey: String, |
| 22 | + gitHubCopilotToken: (() async -> GitHubCopilotExtension.Token?)? = nil, |
| 23 | + shellEnvironmentVariable: ((_: String) async -> String?)? = nil |
| 24 | + ) { |
| 25 | + self.modelName = modelName |
| 26 | + self.apiKey = apiKey |
| 27 | + self.gitHubCopilotToken = gitHubCopilotToken ?? { |
| 28 | + try? await GitHubCopilotExtension.fetchToken() |
| 29 | + } |
| 30 | + self.shellEnvironmentVariable = shellEnvironmentVariable ?? { p in |
| 31 | + let shell = ProcessInfo.processInfo.environment["SHELL"] ?? "/bin/bash" |
| 32 | + let terminal = Terminal() |
| 33 | + return try? await terminal.runCommand( |
| 34 | + shell, |
| 35 | + arguments: ["-i", "-l", "-c", "echo $\(p)"], |
| 36 | + environment: [:] |
| 37 | + ) |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + public init() {} |
| 43 | + |
| 44 | + /// Replace `{{PlaceHolder}}` with exact values. |
| 45 | + public func parse(_ value: String, context: Context) async -> String { |
| 46 | + var parsedValue = value |
| 47 | + let placeholderRanges = findPlaceholderRanges(in: parsedValue) |
| 48 | + |
| 49 | + for (range, placeholderText) in placeholderRanges.reversed() { |
| 50 | + let cleanPlaceholder = placeholderText |
| 51 | + .trimmingCharacters(in: CharacterSet(charactersIn: "{}")) |
| 52 | + |
| 53 | + var replacement: String? |
| 54 | + if let knownPlaceholder = Placeholder(rawValue: cleanPlaceholder) { |
| 55 | + async let token = context.gitHubCopilotToken() |
| 56 | + switch knownPlaceholder { |
| 57 | + case .gitHubCopilotOBearerToken: |
| 58 | + replacement = await token?.token |
| 59 | + case .apiKey: |
| 60 | + replacement = context.apiKey |
| 61 | + case .modelName: |
| 62 | + replacement = context.modelName |
| 63 | + } |
| 64 | + } else { |
| 65 | + replacement = await context.shellEnvironmentVariable(cleanPlaceholder) |
| 66 | + } |
| 67 | + |
| 68 | + if let replacement { |
| 69 | + parsedValue.replaceSubrange(range, with: replacement) |
| 70 | + } else { |
| 71 | + parsedValue.replaceSubrange(range, with: "none") |
| 72 | + } |
| 73 | + } |
| 74 | + |
| 75 | + return parsedValue |
| 76 | + } |
| 77 | + |
| 78 | + private func findPlaceholderRanges(in string: String) -> [(Range<String.Index>, String)] { |
| 79 | + var ranges: [(Range<String.Index>, String)] = [] |
| 80 | + let pattern = #"\{\{[^}]+\}\}"# |
| 81 | + |
| 82 | + do { |
| 83 | + let regex = try NSRegularExpression(pattern: pattern) |
| 84 | + let matches = regex.matches( |
| 85 | + in: string, |
| 86 | + range: NSRange(string.startIndex..., in: string) |
| 87 | + ) |
| 88 | + |
| 89 | + for match in matches { |
| 90 | + if let range = Range(match.range, in: string) { |
| 91 | + ranges.append((range, String(string[range]))) |
| 92 | + } |
| 93 | + } |
| 94 | + } catch { |
| 95 | + Logger.service.error("Failed to find placeholders in string: \(string)") |
| 96 | + } |
| 97 | + |
| 98 | + return ranges |
| 99 | + } |
| 100 | +} |
| 101 | + |
0 commit comments