Skip to content

Commit 6ba6c5b

Browse files
committed
refactor: Move API config to api-config.ts and update imports
1 parent 1096cbf commit 6ba6c5b

File tree

10 files changed

+51
-16
lines changed

10 files changed

+51
-16
lines changed
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,15 @@ export const COPILOT_API_CONFIG = {
88
} as const
99

1010
export const COPILOT_API_BASE_URL = "https://api.individual.githubcopilot.com"
11-
export const COPILOT_API_HEADERS = {
11+
const COPILOT_API_SPOOF_HEADERS = {
1212
"copilot-integration-id": "vscode-chat",
13-
"editor-version": "vscode/1.98.0-insider",
1413
}
1514

15+
export const buildCopilotHeaders = (token: string) => ({
16+
Authorization: `token ${token}`,
17+
...COPILOT_API_SPOOF_HEADERS,
18+
})
19+
1620
export const GITHUB_API_BASE_URL = "https://api.github.com"
1721

1822
export const GITHUB_BASE_URL = "https://github.com"

src/routes/embeddings/route.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ import { FetchError } from "ofetch"
44

55
import type { EmbeddingRequest } from "~/services/copilot/embedding/types"
66

7-
import { embedding } from "~/services/copilot/embedding/service"
7+
import { createEmbeddings } from "~/services/copilot/embedding/service"
88

99
export const embeddingRoutes = new Hono()
1010

1111
embeddingRoutes.post("/", async (c) => {
1212
try {
13-
const embeddings = await embedding(await c.req.json<EmbeddingRequest>())
13+
const embeddings = await createEmbeddings(await c.req.json<EmbeddingRequest>())
1414
return c.json(embeddings)
1515
} catch (error) {
1616
if (error instanceof FetchError) {

src/services/api-instance.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import consola from "consola"
22
import { FetchError, ofetch } from "ofetch"
33

4-
import { COPILOT_API_CONFIG } from "~/lib/constants"
4+
import { COPILOT_API_CONFIG } from "~/lib/api-config"
55
import { modelsCache } from "~/lib/models"
66
import { tokenService } from "~/lib/token"
77

src/services/copilot/chat-completions/service-streaming.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { stream } from "fetch-event-stream"
22

3-
import { COPILOT_API_CONFIG } from "~/lib/constants"
3+
import { COPILOT_API_CONFIG } from "~/lib/api-config"
44
import { tokenService } from "~/lib/token"
55

66
import type { ChatCompletionsPayload } from "./types"
Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,42 @@
1+
import { COPILOT_API_BASE_URL } from "~/lib/api-config"
2+
import { state } from "~/lib/state"
3+
14
import type { EmbeddingRequest, EmbeddingResponse } from "./types"
25

36
import { copilot } from "../../api-instance"
47

5-
export const embedding = (payload: EmbeddingRequest) =>
6-
copilot<EmbeddingResponse>("/embeddings", {
7-
method: "POST",
8-
body: {
9-
...payload,
8+
export const createEmbeddings = (payload: EmbeddingRequest) => {
9+
const response = await fetch(`${COPILOT_API_BASE_URL}/embeddings`, {
10+
headers: {
11+
authorization: `token ${state.copilotToken}`,
1012
},
1113
})
14+
}
15+
16+
copilot<EmbeddingResponse>("/embeddings", {
17+
method: "POST",
18+
body: {
19+
...payload,
20+
},
21+
})
22+
23+
export interface EmbeddingRequest {
24+
input: string | Array<string>
25+
model: string
26+
}
27+
28+
export interface Embedding {
29+
object: string
30+
embedding: Array<number>
31+
index: number
32+
}
33+
34+
export interface EmbeddingResponse {
35+
object: string
36+
data: Array<Embedding>
37+
model: string
38+
usage: {
39+
prompt_tokens: number
40+
total_tokens: number
41+
}
42+
}

src/services/copilot/get-copilot-token.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GITHUB_API_BASE_URL } from "~/lib/constants"
1+
import { GITHUB_API_BASE_URL } from "~/lib/api-config"
22
import { state } from "~/lib/state"
33

44
export const getCopilotToken = async () => {

src/services/copilot/get-models.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { COPILOT_API_BASE_URL } from "~/lib/constants"
1+
import { COPILOT_API_BASE_URL } from "~/lib/api-config"
22
import { state } from "~/lib/state"
33

44
export const getModels = async () => {

src/services/github/get-device-code.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from "~/lib/constants"
1+
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from "~/lib/api-config"
22

33
export async function getDeviceCode(): Promise<DeviceCodeResponse> {
44
const response = await fetch(`${GITHUB_BASE_URL}/login/device/code`, {

src/services/github/get-user.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GITHUB_API_BASE_URL } from "~/lib/constants"
1+
import { GITHUB_API_BASE_URL } from "~/lib/api-config"
22
import { state } from "~/lib/state"
33

44
export async function getGitHubUser() {

src/services/github/poll-access-token.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from "~/lib/constants"
1+
import { GITHUB_BASE_URL, GITHUB_CLIENT_ID } from "~/lib/api-config"
22
import { sleep } from "~/lib/sleep"
33

44
import type { DeviceCodeResponse } from "./get-device-code"

0 commit comments

Comments
 (0)