Skip to content

Commit b120f38

Browse files
committed
Add HTTP-Referer field
1 parent 8024f02 commit b120f38

2 files changed

Lines changed: 32 additions & 16 deletions

File tree

Tool/Sources/OpenAIService/APIs/OpenAIChatCompletionsService.swift

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -233,8 +233,8 @@ actor OpenAIChatCompletionsService: ChatCompletionsStreamAPI, ChatCompletionsAPI
233233
let encoder = JSONEncoder()
234234
request.httpBody = try encoder.encode(requestBody)
235235
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
236-
237-
Self.setupRequestTitle(&request)
236+
237+
Self.setupAppInformation(&request)
238238
Self.setupAPIKey(&request, model: model, apiKey: apiKey)
239239

240240
let (result, response) = try await URLSession.shared.bytes(for: request)
@@ -284,8 +284,8 @@ actor OpenAIChatCompletionsService: ChatCompletionsStreamAPI, ChatCompletionsAPI
284284
let encoder = JSONEncoder()
285285
request.httpBody = try encoder.encode(requestBody)
286286
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
287-
288-
Self.setupRequestTitle(&request)
287+
288+
Self.setupAppInformation(&request)
289289
Self.setupAPIKey(&request, model: model, apiKey: apiKey)
290290

291291
let (result, response) = try await URLSession.shared.data(for: request)
@@ -307,19 +307,27 @@ actor OpenAIChatCompletionsService: ChatCompletionsStreamAPI, ChatCompletionsAPI
307307
throw error
308308
}
309309
}
310-
311-
static func setupRequestTitle(_ request: inout URLRequest) {
310+
311+
static func setupAppInformation(_ request: inout URLRequest) {
312312
if #available(macOS 13.0, *) {
313313
if request.url?.host == "openrouter.ai" {
314314
request.setValue("Copilot for Xcode", forHTTPHeaderField: "X-Title")
315+
request.setValue(
316+
"https://github.com/intitni/CopilotForXcode",
317+
forHTTPHeaderField: "HTTP-Referer"
318+
)
315319
}
316320
} else {
317321
if request.url?.host == "openrouter.ai" {
318322
request.setValue("Copilot for Xcode", forHTTPHeaderField: "X-Title")
323+
request.setValue(
324+
"https://github.com/intitni/CopilotForXcode",
325+
forHTTPHeaderField: "HTTP-Referer"
326+
)
319327
}
320328
}
321329
}
322-
330+
323331
static func setupAPIKey(_ request: inout URLRequest, model: ChatModel, apiKey: String) {
324332
if !apiKey.isEmpty {
325333
switch model.format {

Tool/Sources/OpenAIService/APIs/OpenAIEmbeddingService.swift

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ struct OpenAIEmbeddingService: EmbeddingAPI {
1212
var input: [[Int]]
1313
var model: String
1414
}
15-
15+
1616
let apiKey: String
1717
let model: EmbeddingModel
1818
let endpoint: String
19-
19+
2020
public func embed(text: String) async throws -> EmbeddingResponse {
2121
return try await embed(texts: [text])
2222
}
@@ -31,8 +31,8 @@ struct OpenAIEmbeddingService: EmbeddingAPI {
3131
model: model.info.modelName
3232
))
3333
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
34-
35-
Self.setupRequestTitle(&request)
34+
35+
Self.setupAppInformation(&request)
3636
Self.setupAPIKey(&request, model: model, apiKey: apiKey)
3737

3838
let (result, response) = try await URLSession.shared.data(for: request)
@@ -72,8 +72,8 @@ struct OpenAIEmbeddingService: EmbeddingAPI {
7272
model: model.info.modelName
7373
))
7474
request.setValue("application/json", forHTTPHeaderField: "Content-Type")
75-
76-
Self.setupRequestTitle(&request)
75+
76+
Self.setupAppInformation(&request)
7777
Self.setupAPIKey(&request, model: model, apiKey: apiKey)
7878

7979
let (result, response) = try await URLSession.shared.data(for: request)
@@ -102,19 +102,27 @@ struct OpenAIEmbeddingService: EmbeddingAPI {
102102
#endif
103103
return embeddingResponse
104104
}
105-
106-
static func setupRequestTitle(_ request: inout URLRequest) {
105+
106+
static func setupAppInformation(_ request: inout URLRequest) {
107107
if #available(macOS 13.0, *) {
108108
if request.url?.host == "openrouter.ai" {
109109
request.setValue("Copilot for Xcode", forHTTPHeaderField: "X-Title")
110+
request.setValue(
111+
"https://github.com/intitni/CopilotForXcode",
112+
forHTTPHeaderField: "HTTP-Referer"
113+
)
110114
}
111115
} else {
112116
if request.url?.host == "openrouter.ai" {
113117
request.setValue("Copilot for Xcode", forHTTPHeaderField: "X-Title")
118+
request.setValue(
119+
"https://github.com/intitni/CopilotForXcode",
120+
forHTTPHeaderField: "HTTP-Referer"
121+
)
114122
}
115123
}
116124
}
117-
125+
118126
static func setupAPIKey(_ request: inout URLRequest, model: EmbeddingModel, apiKey: String) {
119127
if !apiKey.isEmpty {
120128
switch model.format {

0 commit comments

Comments
 (0)