Skip to content

Commit 3aeaa8f

Browse files
committed
feat: better openai types
1 parent e8e28ab commit 3aeaa8f

1 file changed

Lines changed: 81 additions & 36 deletions

File tree

src/services/copilot/create-chat-completions.ts

Lines changed: 81 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,10 @@ export const createChatCompletions = async (
99
) => {
1010
if (!state.copilotToken) throw new Error("Copilot token not found")
1111

12-
for (const message of payload.messages) {
13-
intoCopilotMessage(message)
14-
}
15-
1612
const visionEnable = payload.messages.some(
1713
(x) =>
1814
typeof x.content !== "string"
19-
&& x.content.some((x) => x.type === "image_url"),
15+
&& x.content?.some((x) => x.type === "image_url"),
2016
)
2117

2218
const response = await fetch(`${copilotBaseUrl(state)}/chat/completions`, {
@@ -35,77 +31,126 @@ export const createChatCompletions = async (
3531
return (await response.json()) as ChatCompletionResponse
3632
}
3733

38-
const intoCopilotMessage = (message: Message) => {
39-
if (typeof message.content === "string") return false
40-
41-
for (const part of message.content) {
42-
if (part.type === "input_image") part.type = "image_url"
43-
}
44-
}
45-
4634
// Streaming types
4735

4836
export interface ChatCompletionChunk {
49-
choices: [Choice]
50-
created: number
51-
object: "chat.completion.chunk"
5237
id: string
38+
object: "chat.completion.chunk"
39+
created: number
5340
model: string
41+
choices: [Choice]
42+
system_fingerprint?: string
5443
}
5544

5645
interface Delta {
57-
content?: string
58-
role?: string
46+
content?: string | null
47+
role?: "user" | "assistant" | "system" | "tool"
48+
tool_calls?: Array<{
49+
index: number
50+
id?: string
51+
type?: "function"
52+
function?: {
53+
name?: string
54+
arguments?: string
55+
}
56+
}>
5957
}
6058

6159
interface Choice {
6260
index: number
6361
delta: Delta
64-
finish_reason: "stop" | null
62+
finish_reason: "stop" | "length" | "tool_calls" | "content_filter" | null
6563
logprobs: null
6664
}
6765

6866
// Non-streaming types
6967

7068
export interface ChatCompletionResponse {
7169
id: string
72-
object: string
70+
object: "chat.completion"
7371
created: number
7472
model: string
7573
choices: [ChoiceNonStreaming]
74+
system_fingerprint?: string
75+
usage?: {
76+
prompt_tokens: number
77+
completion_tokens: number
78+
total_tokens: number
79+
}
7680
}
7781

7882
interface ChoiceNonStreaming {
7983
index: number
8084
message: Message
8185
logprobs: null
82-
finish_reason: "stop"
86+
finish_reason: "stop" | "length" | "tool_calls" | "content_filter"
8387
}
8488

8589
// Payload types
8690

8791
export interface ChatCompletionsPayload {
8892
messages: Array<Message>
8993
model: string
90-
temperature?: number
91-
top_p?: number
92-
max_tokens?: number
93-
stop?: Array<string>
94-
n?: number
95-
stream?: boolean
94+
temperature?: number | null
95+
top_p?: number | null
96+
max_tokens?: number | null
97+
stop?: string | Array<string> | null
98+
n?: number | null
99+
stream?: boolean | null
100+
101+
frequency_penalty?: number | null
102+
presence_penalty?: number | null
103+
logit_bias?: Record<string, number> | null
104+
logprobs?: boolean | null
105+
response_format?: { type: "json_object" } | null
106+
seed?: number | null
107+
tools?: Array<Tool> | null
108+
tool_choice?:
109+
| "none"
110+
| "auto"
111+
| { type: "function"; function: { name: string } }
112+
| null
113+
user?: string | null
114+
}
115+
116+
export interface Tool {
117+
type: "function"
118+
function: {
119+
name: string
120+
description?: string
121+
parameters: Record<string, unknown>
122+
}
96123
}
97124

98125
export interface Message {
99-
role: "user" | "assistant" | "system"
100-
content: string | Array<ContentPart>
126+
role: "user" | "assistant" | "system" | "tool"
127+
content: string | Array<ContentPart> | null
128+
129+
name?: string
130+
tool_calls?: Array<ToolCall>
131+
tool_call_id?: string
132+
}
133+
134+
export interface ToolCall {
135+
id: string
136+
type: "function"
137+
function: {
138+
name: string
139+
arguments: string
140+
}
101141
}
102142

103-
// https://platform.openai.com/docs/api-reference
143+
export type ContentPart = TextPart | ImagePart
104144

105-
export interface ContentPart {
106-
type: "input_image" | "input_text" | "image_url"
107-
text?: string
108-
image_url?: string
145+
export interface TextPart {
146+
type: "text"
147+
text: string
148+
}
149+
150+
export interface ImagePart {
151+
type: "image_url"
152+
image_url: {
153+
url: string
154+
detail?: "low" | "high" | "auto"
155+
}
109156
}
110-
// https://platform.openai.com/docs/guides/images-vision#giving-a-model-images-as-input
111-
// Note: copilot use "image_url", but openai use "input_image"

0 commit comments

Comments
 (0)