Skip to content

Commit f225582

Browse files
committed
feat: non streaming completion
1 parent 943ddb0 commit f225582

4 files changed

Lines changed: 114 additions & 96 deletions

File tree

src/main.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import consola from "consola"
22

33
import { TOKENS } from "./lib/tokens"
4+
import { chatCompletions } from "./services/copilot-vscode/chat-completions/service"
45
import { getCopilotToken } from "./services/copilot-vscode/get-token/copilot-token"
56
import { getGitHubToken } from "./services/copilot-vscode/get-token/github-token"
67

@@ -19,3 +20,18 @@ setInterval(async () => {
1920
const { token: copilotToken } = await getCopilotToken()
2021
TOKENS.COPILOT_TOKEN = copilotToken
2122
}, refreshInterval)
23+
24+
const response = await chatCompletions({
25+
messages: [
26+
{
27+
role: "user",
28+
content: "Write a function that returns the sum of two numbers",
29+
},
30+
],
31+
model: "gpt-4o-mini",
32+
stream: false,
33+
})
34+
35+
console.log(response)
36+
37+
await Bun.write("response.json", JSON.stringify(response, null, 2))
Lines changed: 7 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,54 +1,9 @@
1-
import { FetchError } from "ofetch"
1+
import type { ChatCompletionResponse, ChatCompletionsPayload } from "./types"
22

3-
import type { ChatCompletionsPayload } from "./types"
3+
import { copilotVSCode } from "../api-instance"
44

5-
import {
6-
COPILOT_VSCODE_BASE_URL,
7-
COPILOT_VSCODE_HEADERS,
8-
copilotVSCode,
9-
} from "../api-instance"
10-
11-
export async function* chatCompletions(payload: ChatCompletionsPayload) {
12-
try {
13-
const response = await copilotVSCode.native(
14-
COPILOT_VSCODE_BASE_URL + "/chat/completions",
15-
{
16-
method: "POST",
17-
body: JSON.stringify(payload),
18-
headers: COPILOT_VSCODE_HEADERS,
19-
},
20-
)
21-
22-
console.log(await response.text())
23-
24-
// if (!response.body) {
25-
// throw new Error("No response body")
26-
// }
27-
28-
// const reader = response.body.getReader()
29-
30-
// // eslint-disable-next-line @typescript-eslint/no-unnecessary-condition
31-
// while (true) {
32-
// const { done, value } = await reader.read()
33-
34-
// if (done) {
35-
// console.log("done")
36-
// console.log(value)
37-
// break
38-
// }
39-
40-
// console.log("value", value)
41-
// }
42-
43-
// for await (const chunk of response.body) {
44-
// console.log(chunk)
45-
// }
46-
47-
yield "tono"
48-
} catch (e) {
49-
console.error(e)
50-
if (e instanceof FetchError) {
51-
console.error(e.response?._data)
52-
}
53-
}
54-
}
5+
export const chatCompletions = (payload: ChatCompletionsPayload) =>
6+
copilotVSCode<ChatCompletionResponse>("/chat/completions", {
7+
method: "POST",
8+
body: payload,
9+
})
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
interface ContentFilterResults {
2+
error: {
3+
code: string
4+
message: string
5+
}
6+
hate: {
7+
filtered: boolean
8+
severity: string
9+
}
10+
self_harm: {
11+
filtered: boolean
12+
severity: string
13+
}
14+
sexual: {
15+
filtered: boolean
16+
severity: string
17+
}
18+
violence: {
19+
filtered: boolean
20+
severity: string
21+
}
22+
}
23+
24+
interface ContentFilterOffsets {
25+
check_offset: number
26+
start_offset: number
27+
end_offset: number
28+
}
29+
30+
interface Delta {
31+
content: string | null
32+
role?: string
33+
}
34+
35+
interface Choice {
36+
index: number
37+
content_filter_offsets?: ContentFilterOffsets
38+
content_filter_results?: ContentFilterResults
39+
delta: Delta
40+
finish_reason?: string | null
41+
}
42+
43+
interface PromptFilterResult {
44+
content_filter_results: ContentFilterResults
45+
prompt_index: number
46+
}
47+
48+
interface Usage {
49+
completion_tokens: number
50+
prompt_tokens: number
51+
total_tokens: number
52+
}
53+
54+
interface ChatCompletionResponse {
55+
choices: Array<Choice>
56+
created: number
57+
id: string
58+
model: string
59+
system_fingerprint?: string
60+
prompt_filter_results?: Array<PromptFilterResult>
61+
usage?: Usage
62+
}
63+
64+
export type ChatCompletionsChunk =
65+
| { data: ChatCompletionResponse }
66+
| { data: "[DONE]" }
Lines changed: 25 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
// Request types
2-
31
interface Message {
42
role: string
53
content: string
@@ -18,69 +16,52 @@ export interface ChatCompletionsPayload {
1816

1917
// Response types
2018

21-
interface ContentFilterResults {
22-
error: {
23-
code: string
24-
message: string
25-
}
26-
hate: {
27-
filtered: boolean
28-
severity: string
29-
}
30-
self_harm: {
31-
filtered: boolean
32-
severity: string
33-
}
34-
sexual: {
35-
filtered: boolean
36-
severity: string
37-
}
38-
violence: {
39-
filtered: boolean
40-
severity: string
41-
}
19+
interface ContentFilterResult {
20+
filtered: boolean
21+
severity: "safe" | "low" | "medium" | "high" // Added possible severity values
4222
}
4323

44-
interface ContentFilterOffsets {
45-
check_offset: number
46-
start_offset: number
47-
end_offset: number
48-
}
49-
50-
interface Delta {
51-
content: string | null
52-
role?: string
24+
interface ContentFilterResults {
25+
hate: ContentFilterResult
26+
self_harm: ContentFilterResult
27+
sexual: ContentFilterResult
28+
violence: ContentFilterResult
5329
}
5430

5531
interface Choice {
32+
content_filter_results: ContentFilterResults
33+
finish_reason: string
5634
index: number
57-
content_filter_offsets?: ContentFilterOffsets
58-
content_filter_results?: ContentFilterResults
59-
delta: Delta
60-
finish_reason?: string | null
35+
message: {
36+
content: string
37+
role: "assistant" | "user" // Added possible role values
38+
}
6139
}
6240

6341
interface PromptFilterResult {
6442
content_filter_results: ContentFilterResults
6543
prompt_index: number
6644
}
6745

46+
interface UsageDetails {
47+
reasoning_tokens?: number
48+
cached_tokens?: number
49+
}
50+
6851
interface Usage {
6952
completion_tokens: number
53+
completion_tokens_details: UsageDetails
7054
prompt_tokens: number
55+
prompt_tokens_details: UsageDetails
7156
total_tokens: number
7257
}
7358

74-
interface ChatCompletionResponse {
59+
export interface ChatCompletionResponse {
7560
choices: Array<Choice>
7661
created: number
7762
id: string
7863
model: string
79-
system_fingerprint?: string
80-
prompt_filter_results?: Array<PromptFilterResult>
81-
usage?: Usage
64+
prompt_filter_results: Array<PromptFilterResult>
65+
system_fingerprint: string
66+
usage: Usage
8267
}
83-
84-
export type ChatCompletionsChunk =
85-
| { data: ChatCompletionResponse }
86-
| { data: "[DONE]" }

0 commit comments

Comments
 (0)