Skip to content

Commit 43b3d29

Browse files
committed
refactor: Use fetch instead of copilot instance for chat completions
1 parent 1d99f2d commit 43b3d29

File tree

3 files changed

+31
-11
lines changed

3 files changed

+31
-11
lines changed

src/routes/chat-completions/handler.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import type { ChatCompletionChunk } from "~/services/copilot/chat-completions/ty
99
import { isNullish } from "~/lib/is-nullish"
1010
import { logger } from "~/lib/logger"
1111
import { modelsCache } from "~/lib/models"
12-
import { chatCompletions } from "~/services/copilot/chat-completions/service"
12+
import { createChatCompletions } from "~/services/copilot/chat-completions/service"
1313
import { chatCompletionsStream } from "~/services/copilot/chat-completions/service-streaming"
1414

1515
function createCondensedStreamingResponse(
@@ -85,7 +85,7 @@ function handleStreaming(c: Context, payload: ChatCompletionsPayload) {
8585
}
8686

8787
async function handleNonStreaming(c: Context, payload: ChatCompletionsPayload) {
88-
const response = await chatCompletions(payload)
88+
const response = await createChatCompletions(payload)
8989

9090
// Get response headers if any
9191
const responseHeaders = {} // Empty placeholder for response headers
Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,34 @@
1+
import { buildCopilotHeaders, COPILOT_API_BASE_URL } from "~/lib/api-config"
2+
import { state } from "~/lib/state"
13
import { copilot } from "~/services/api-instance"
24

35
import type { ChatCompletionResponse, ChatCompletionsPayload } from "./types"
46

5-
export const chatCompletions = (payload: ChatCompletionsPayload) =>
6-
copilot<ChatCompletionResponse>("/chat/completions", {
7+
export const createChatCompletions = async (
8+
payload: ChatCompletionsPayload,
9+
) => {
10+
if (!state.copilotToken) throw new Error("Copilot token not found")
11+
12+
const response = await fetch(`${COPILOT_API_BASE_URL}/chat/completions`, {
713
method: "POST",
8-
body: {
9-
...payload,
10-
stream: false,
11-
},
14+
headers: buildCopilotHeaders(state.copilotToken),
15+
body: JSON.stringify(payload),
1216
})
17+
18+
if (!response.ok) {
19+
throw new Error("Failed to create chat completions", {
20+
cause: await response.json(),
21+
})
22+
}
23+
24+
if (payload.stream) {
25+
}
26+
}
27+
28+
copilot<ChatCompletionResponse>("/chat/completions", {
29+
method: "POST",
30+
body: {
31+
...payload,
32+
stream: false,
33+
},
34+
})

src/services/copilot/create-embeddings.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ export const createEmbeddings = async (payload: EmbeddingRequest) => {
66

77
const response = await fetch(`${COPILOT_API_BASE_URL}/embeddings`, {
88
method: "POST",
9-
headers: {
10-
...buildCopilotHeaders(state.copilotToken),
11-
},
9+
headers: buildCopilotHeaders(state.copilotToken),
1210
body: JSON.stringify(payload),
1311
})
1412

0 commit comments

Comments
 (0)