forked from intitni/CopilotForXcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAIResponsesRawService.swift
More file actions
235 lines (209 loc) · 8.19 KB
/
OpenAIResponsesRawService.swift
File metadata and controls
235 lines (209 loc) · 8.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
import AIModel
import AsyncAlgorithms
import ChatBasic
import Foundation
import GitHubCopilotService
import JoinJSON
import Logger
import Preferences
/// https://platform.openai.com/docs/api-reference/responses/create
public actor OpenAIResponsesRawService {
struct CompletionAPIError: Error, Decodable, LocalizedError {
struct ErrorDetail: Decodable {
var message: String
var type: String?
var param: String?
var code: String?
}
struct MistralAIErrorMessage: Decodable {
struct Detail: Decodable {
var msg: String?
}
var message: String?
var msg: String?
var detail: [Detail]?
}
enum Message {
case raw(String)
case mistralAI(MistralAIErrorMessage)
}
var error: ErrorDetail?
var message: Message
var errorDescription: String? {
if let message = error?.message { return message }
switch message {
case let .raw(string):
return string
case let .mistralAI(mistralAIErrorMessage):
return mistralAIErrorMessage.message
?? mistralAIErrorMessage.msg
?? mistralAIErrorMessage.detail?.first?.msg
?? "Unknown Error"
}
}
enum CodingKeys: String, CodingKey {
case error
case message
}
init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
error = try container.decode(ErrorDetail.self, forKey: .error)
message = {
if let e = try? container.decode(MistralAIErrorMessage.self, forKey: .message) {
return CompletionAPIError.Message.mistralAI(e)
}
if let e = try? container.decode(String.self, forKey: .message) {
return .raw(e)
}
return .raw("Unknown Error")
}()
}
}
var apiKey: String
var endpoint: URL
var requestBody: [String: Any]
var model: ChatModel
let requestModifier: ((inout URLRequest) -> Void)?
public init(
apiKey: String,
model: ChatModel,
endpoint: URL,
requestBody: Data,
requestModifier: ((inout URLRequest) -> Void)? = nil
) {
self.apiKey = apiKey
self.endpoint = endpoint
self.requestBody = (
try? JSONSerialization.jsonObject(with: requestBody) as? [String: Any]
) ?? [:]
self.requestBody["model"] = model.info.modelName
self.model = model
self.requestModifier = requestModifier
}
public func callAsFunction() async throws
-> URLSession.AsyncBytes
{
requestBody["stream"] = true
var request = URLRequest(url: endpoint)
request.httpMethod = "POST"
request.httpBody = try JSONSerialization.data(
withJSONObject: requestBody,
options: []
)
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
Self.setupAppInformation(&request)
await Self.setupAPIKey(&request, model: model, apiKey: apiKey)
Self.setupGitHubCopilotVisionField(&request, model: model)
await Self.setupExtraHeaderFields(&request, model: model, apiKey: apiKey)
requestModifier?(&request)
let (result, response) = try await URLSession.shared.bytes(for: request)
guard let response = response as? HTTPURLResponse else {
throw ChatGPTServiceError.responseInvalid
}
guard response.statusCode == 200 else {
let text = try await result.lines.reduce(into: "") { partialResult, current in
partialResult += current
}
guard let data = text.data(using: .utf8)
else { throw ChatGPTServiceError.responseInvalid }
if response.statusCode == 403 {
throw ChatGPTServiceError.unauthorized(text)
}
let decoder = JSONDecoder()
let error = try? decoder.decode(CompletionAPIError.self, from: data)
throw error ?? ChatGPTServiceError.otherError(
text +
"\n\nPlease check your model settings, some capabilities may not be supported by the model."
)
}
return result
}
public func callAsFunction() async throws -> Data {
let stream: URLSession.AsyncBytes = try await callAsFunction()
return try await stream.reduce(into: Data()) { partialResult, byte in
partialResult.append(byte)
}
}
static func setupAppInformation(_ request: inout URLRequest) {
if #available(macOS 13.0, *) {
if request.url?.host == "openrouter.ai" {
request.setValue("Copilot for Xcode", forHTTPHeaderField: "X-Title")
request.setValue(
"https://github.com/intitni/CopilotForXcode",
forHTTPHeaderField: "HTTP-Referer"
)
}
} else {
if request.url?.host == "openrouter.ai" {
request.setValue("Copilot for Xcode", forHTTPHeaderField: "X-Title")
request.setValue(
"https://github.com/intitni/CopilotForXcode",
forHTTPHeaderField: "HTTP-Referer"
)
}
}
}
static func setupAPIKey(_ request: inout URLRequest, model: ChatModel, apiKey: String) async {
if !apiKey.isEmpty {
switch model.format {
case .openAI:
if !model.info.openAIInfo.organizationID.isEmpty {
request.setValue(
model.info.openAIInfo.organizationID,
forHTTPHeaderField: "OpenAI-Organization"
)
}
if !model.info.openAIInfo.projectID.isEmpty {
request.setValue(
model.info.openAIInfo.projectID,
forHTTPHeaderField: "OpenAI-Project"
)
}
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
case .openAICompatible:
request.setValue("Bearer \(apiKey)", forHTTPHeaderField: "Authorization")
case .azureOpenAI:
request.setValue(apiKey, forHTTPHeaderField: "api-key")
case .gitHubCopilot:
break
case .googleAI:
assertionFailure("Unsupported")
case .ollama:
assertionFailure("Unsupported")
case .claude:
assertionFailure("Unsupported")
}
}
if model.format == .gitHubCopilot,
let token = try? await GitHubCopilotExtension.fetchToken()
{
request.setValue(
"Copilot for Xcode/\(Bundle.main.infoDictionary?["CFBundleShortVersionString"] as? String ?? "unknown")",
forHTTPHeaderField: "Editor-Version"
)
request.setValue("Bearer \(token.token)", forHTTPHeaderField: "Authorization")
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
request.setValue("vscode-chat", forHTTPHeaderField: "Copilot-Integration-Id")
request.setValue("2023-07-07", forHTTPHeaderField: "X-Github-Api-Version")
}
}
static func setupGitHubCopilotVisionField(_ request: inout URLRequest, model: ChatModel) {
if model.info.supportsImage {
request.setValue("true", forHTTPHeaderField: "copilot-vision-request")
}
}
static func setupExtraHeaderFields(
_ request: inout URLRequest,
model: ChatModel,
apiKey: String
) async {
let parser = HeaderValueParser()
for field in model.info.customHeaderInfo.headers where !field.key.isEmpty {
let value = await parser.parse(
field.value,
context: .init(modelName: model.info.modelName, apiKey: apiKey)
)
request.setValue(value, forHTTPHeaderField: field.key)
}
}
}