Skip to content

Commit f3615ee

Browse files
committed
refactor: Move types to create-chat-completions from separate files
1 parent bb3462b commit f3615ee

File tree

4 files changed

+64
-151
lines changed

4 files changed

+64
-151
lines changed

src/routes/chat-completions/handler.ts

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ import type { Context } from "hono"
22

33
import { streamSSE, type SSEMessage } from "hono/streaming"
44

5-
import type {
6-
ChatCompletionResponse,
7-
ChatCompletionsPayload,
8-
} from "~/services/copilot/chat-completions/types"
9-
105
import { isNullish } from "~/lib/is-nullish"
116
import { state } from "~/lib/state"
12-
import { createChatCompletions } from "~/services/copilot/create-chat-completions"
7+
import {
8+
createChatCompletions,
9+
type ChatCompletionResponse,
10+
type ChatCompletionsPayload,
11+
} from "~/services/copilot/create-chat-completions"
1312

1413
export async function handleCompletion(c: Context) {
1514
let payload = await c.req.json<ChatCompletionsPayload>()

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

Lines changed: 0 additions & 73 deletions
This file was deleted.

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

Lines changed: 0 additions & 67 deletions
This file was deleted.

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

Lines changed: 59 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@ import { copilotHeaders, COPILOT_API_BASE_URL } from "~/lib/api-config"
44
import { HTTPError } from "~/lib/http-error"
55
import { state } from "~/lib/state"
66

7-
import type {
8-
ChatCompletionResponse,
9-
ChatCompletionsPayload,
10-
} from "./chat-completions/types"
11-
127
export const createChatCompletions = async (
138
payload: ChatCompletionsPayload,
149
) => {
@@ -29,3 +24,62 @@ export const createChatCompletions = async (
2924

3025
return (await response.json()) as ChatCompletionResponse
3126
}
27+
28+
// Streaming types
29+
30+
export interface ChatCompletionChunk {
31+
choices: [Choice]
32+
created: number
33+
object: "chat.completion.chunk"
34+
id: string
35+
model: string
36+
}
37+
38+
interface Delta {
39+
content?: string
40+
role?: string
41+
}
42+
43+
interface Choice {
44+
index: number
45+
delta: Delta
46+
finish_reason: "stop" | null
47+
logprobs: null
48+
}
49+
50+
// Non-streaming types
51+
52+
export interface ChatCompletionResponse {
53+
id: string
54+
object: string
55+
created: number
56+
model: string
57+
choices: [ChoiceNonStreaming]
58+
}
59+
60+
interface ChoiceNonStreaming {
61+
index: number
62+
message: Message
63+
logprobs: null
64+
finish_reason: "stop"
65+
}
66+
67+
// Payload types
68+
69+
export interface ChatCompletionsPayload {
70+
messages: Array<Message>
71+
model: string
72+
temperature?: number
73+
top_p?: number
74+
max_tokens?: number
75+
stop?: Array<string>
76+
n?: number
77+
stream?: boolean
78+
}
79+
80+
export interface Message {
81+
role: "user" | "assistant" | "system"
82+
content: string
83+
}
84+
85+
// https://platform.openai.com/docs/api-reference

0 commit comments

Comments
 (0)