|
| 1 | +import Foundation |
| 2 | + |
| 3 | +/// An LLM client for Anthropic Claude models served via Vertex AI. |
| 4 | +/// |
| 5 | +/// Uses OAuth2 Bearer token authentication (via ``GoogleAuthService`` or a custom |
| 6 | +/// token provider closure) instead of Anthropic API key authentication. |
| 7 | +/// |
| 8 | +/// The wire format is the standard Anthropic Messages API with a |
| 9 | +/// `"anthropic_version": "vertex-2023-10-16"` field injected into the request body. |
| 10 | +/// Response parsing and SSE streaming are delegated to an internal ``AnthropicClient``. |
| 11 | +/// |
| 12 | +/// ```swift |
| 13 | +/// let auth = try GoogleAuthService() |
| 14 | +/// let client = VertexAnthropicClient( |
| 15 | +/// projectID: "my-project", |
| 16 | +/// location: "us-east5", |
| 17 | +/// model: "claude-sonnet-4-6", |
| 18 | +/// authService: auth |
| 19 | +/// ) |
| 20 | +/// ``` |
| 21 | +public struct VertexAnthropicClient: LLMClient, Sendable { |
| 22 | + public let contextWindowSize: Int? |
| 23 | + |
| 24 | + let anthropic: AnthropicClient |
| 25 | + private let projectID: String |
| 26 | + private let location: String |
| 27 | + private let model: String |
| 28 | + private let tokenProvider: @Sendable () async throws -> String |
| 29 | + private let session: URLSession |
| 30 | + private let retryPolicy: RetryPolicy |
| 31 | + |
| 32 | + public init( |
| 33 | + projectID: String, |
| 34 | + location: String, |
| 35 | + model: String, |
| 36 | + tokenProvider: @Sendable @escaping () async throws -> String, |
| 37 | + maxTokens: Int = 8192, |
| 38 | + contextWindowSize: Int? = nil, |
| 39 | + session: URLSession = .shared, |
| 40 | + retryPolicy: RetryPolicy = .default, |
| 41 | + reasoningConfig: ReasoningConfig? = nil, |
| 42 | + interleavedThinking: Bool = true, |
| 43 | + cachingEnabled: Bool = false |
| 44 | + ) { |
| 45 | + self.projectID = projectID |
| 46 | + self.location = location |
| 47 | + self.model = model |
| 48 | + self.tokenProvider = tokenProvider |
| 49 | + self.session = session |
| 50 | + self.retryPolicy = retryPolicy |
| 51 | + self.contextWindowSize = contextWindowSize |
| 52 | + anthropic = AnthropicClient( |
| 53 | + apiKey: "", |
| 54 | + model: model, |
| 55 | + maxTokens: maxTokens, |
| 56 | + contextWindowSize: contextWindowSize, |
| 57 | + session: session, |
| 58 | + retryPolicy: retryPolicy, |
| 59 | + reasoningConfig: reasoningConfig, |
| 60 | + interleavedThinking: interleavedThinking, |
| 61 | + cachingEnabled: cachingEnabled |
| 62 | + ) |
| 63 | + } |
| 64 | + |
| 65 | + /// Convenience initializer that uses a ``GoogleAuthService`` for authentication. |
| 66 | + public init( |
| 67 | + projectID: String, |
| 68 | + location: String, |
| 69 | + model: String, |
| 70 | + authService: GoogleAuthService, |
| 71 | + maxTokens: Int = 8192, |
| 72 | + contextWindowSize: Int? = nil, |
| 73 | + session: URLSession = .shared, |
| 74 | + retryPolicy: RetryPolicy = .default, |
| 75 | + reasoningConfig: ReasoningConfig? = nil, |
| 76 | + interleavedThinking: Bool = true, |
| 77 | + cachingEnabled: Bool = false |
| 78 | + ) { |
| 79 | + self.init( |
| 80 | + projectID: projectID, |
| 81 | + location: location, |
| 82 | + model: model, |
| 83 | + tokenProvider: { try await authService.accessToken() }, |
| 84 | + maxTokens: maxTokens, |
| 85 | + contextWindowSize: contextWindowSize, |
| 86 | + session: session, |
| 87 | + retryPolicy: retryPolicy, |
| 88 | + reasoningConfig: reasoningConfig, |
| 89 | + interleavedThinking: interleavedThinking, |
| 90 | + cachingEnabled: cachingEnabled |
| 91 | + ) |
| 92 | + } |
| 93 | + |
| 94 | + // MARK: - LLMClient |
| 95 | + |
| 96 | + public func generate( |
| 97 | + messages: [ChatMessage], |
| 98 | + tools: [ToolDefinition], |
| 99 | + responseFormat: ResponseFormat?, |
| 100 | + requestContext: RequestContext? |
| 101 | + ) async throws -> AssistantMessage { |
| 102 | + if responseFormat != nil { |
| 103 | + throw AgentError.llmError(.other("VertexAnthropicClient does not support responseFormat")) |
| 104 | + } |
| 105 | + let request = try anthropic.buildRequest( |
| 106 | + messages: messages, |
| 107 | + tools: tools, |
| 108 | + extraFields: requestContext?.extraFields ?? [:] |
| 109 | + ) |
| 110 | + let token = try await tokenProvider() |
| 111 | + let urlRequest = try buildVertexURLRequest( |
| 112 | + VertexAnthropicRequest(inner: request), stream: false, token: token |
| 113 | + ) |
| 114 | + let (data, httpResponse) = try await HTTPRetry.performData( |
| 115 | + urlRequest: urlRequest, session: session, retryPolicy: retryPolicy |
| 116 | + ) |
| 117 | + requestContext?.onResponse?(httpResponse) |
| 118 | + return try anthropic.parseResponse(data) |
| 119 | + } |
| 120 | + |
| 121 | + public func stream( |
| 122 | + messages: [ChatMessage], |
| 123 | + tools: [ToolDefinition], |
| 124 | + requestContext: RequestContext? |
| 125 | + ) -> AsyncThrowingStream<StreamDelta, Error> { |
| 126 | + AsyncThrowingStream { continuation in |
| 127 | + let task = Task { |
| 128 | + do { |
| 129 | + try await performStreamRequest( |
| 130 | + messages: messages, |
| 131 | + tools: tools, |
| 132 | + extraFields: requestContext?.extraFields ?? [:], |
| 133 | + onResponse: requestContext?.onResponse, |
| 134 | + continuation: continuation |
| 135 | + ) |
| 136 | + } catch { |
| 137 | + continuation.finish(throwing: error) |
| 138 | + } |
| 139 | + } |
| 140 | + continuation.onTermination = { _ in task.cancel() } |
| 141 | + } |
| 142 | + } |
| 143 | + |
| 144 | + // MARK: - Streaming |
| 145 | + |
| 146 | + private func performStreamRequest( |
| 147 | + messages: [ChatMessage], |
| 148 | + tools: [ToolDefinition], |
| 149 | + extraFields: [String: JSONValue], |
| 150 | + onResponse: (@Sendable (HTTPURLResponse) -> Void)?, |
| 151 | + continuation: AsyncThrowingStream<StreamDelta, Error>.Continuation |
| 152 | + ) async throws { |
| 153 | + let request = try anthropic.buildRequest( |
| 154 | + messages: messages, tools: tools, |
| 155 | + stream: true, extraFields: extraFields |
| 156 | + ) |
| 157 | + let token = try await tokenProvider() |
| 158 | + let urlRequest = try buildVertexURLRequest( |
| 159 | + VertexAnthropicRequest(inner: request), stream: true, token: token |
| 160 | + ) |
| 161 | + let (bytes, httpResponse) = try await HTTPRetry.performStream( |
| 162 | + urlRequest: urlRequest, session: session, retryPolicy: retryPolicy |
| 163 | + ) |
| 164 | + onResponse?(httpResponse) |
| 165 | + |
| 166 | + let state = AnthropicStreamState() |
| 167 | + |
| 168 | + try await processSSEStream( |
| 169 | + bytes: bytes, |
| 170 | + stallTimeout: retryPolicy.streamStallTimeout |
| 171 | + ) { line in |
| 172 | + try await anthropic.handleSSELine( |
| 173 | + line, state: state, continuation: continuation |
| 174 | + ) |
| 175 | + } |
| 176 | + continuation.finish() |
| 177 | + } |
| 178 | + |
| 179 | + // MARK: - URL Construction |
| 180 | + |
| 181 | + func buildVertexURLRequest( |
| 182 | + _ request: VertexAnthropicRequest, |
| 183 | + stream: Bool, |
| 184 | + token: String |
| 185 | + ) throws -> URLRequest { |
| 186 | + let action = stream ? "streamRawPredict" : "rawPredict" |
| 187 | + let basePath = "v1/projects/\(projectID)/locations/\(location)" |
| 188 | + + "/publishers/anthropic/models/\(model):\(action)" |
| 189 | + let baseURL = URL(string: "https://\(location)-aiplatform.googleapis.com")! |
| 190 | + let url = baseURL.appendingPathComponent(basePath) |
| 191 | + |
| 192 | + let headers = ["Authorization": "Bearer \(token)"] |
| 193 | + return try buildJSONPostRequest(url: url, body: request, headers: headers) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +// MARK: - Vertex Anthropic Request Wrapper |
| 198 | + |
| 199 | +/// Wraps an ``AnthropicRequest`` and injects `"anthropic_version": "vertex-2023-10-16"` |
| 200 | +/// into the encoded JSON body for Vertex AI compatibility. |
| 201 | +struct VertexAnthropicRequest: Encodable { |
| 202 | + static let vertexAnthropicVersion = "vertex-2023-10-16" |
| 203 | + |
| 204 | + let inner: AnthropicRequest |
| 205 | + |
| 206 | + func encode(to encoder: any Encoder) throws { |
| 207 | + try inner.encode(to: encoder) |
| 208 | + var container = encoder.container(keyedBy: DynamicCodingKey.self) |
| 209 | + try container.encode( |
| 210 | + Self.vertexAnthropicVersion, |
| 211 | + forKey: DynamicCodingKey("anthropic_version") |
| 212 | + ) |
| 213 | + } |
| 214 | +} |
0 commit comments