@@ -2,27 +2,14 @@ import type { Context } from "hono"
22
33import { streamSSE , type SSEMessage } from "hono/streaming"
44
5- import type { ChatCompletionsPayload } from "~/services/copilot/chat-completions/types"
5+ import type {
6+ ChatCompletionResponse ,
7+ ChatCompletionsPayload ,
8+ } from "~/services/copilot/chat-completions/types"
69
710import { isNullish } from "~/lib/is-nullish"
811import { state } from "~/lib/state"
9- import { createChatCompletions } from "~/services/copilot/chat-completions/service"
10- import { chatCompletionsStream } from "~/services/copilot/chat-completions/service-streaming"
11-
12- function handleStreaming ( c : Context , payload : ChatCompletionsPayload ) {
13- return streamSSE ( c , async ( stream ) => {
14- const response = await chatCompletionsStream ( payload )
15-
16- for await ( const chunk of response ) {
17- await stream . writeSSE ( chunk as SSEMessage )
18- }
19- } )
20- }
21-
22- async function handleNonStreaming ( c : Context , payload : ChatCompletionsPayload ) {
23- const response = await createChatCompletions ( payload )
24- return c . json ( response )
25- }
12+ import { createChatCompletions } from "~/services/copilot/create-chat-completions"
2613
2714export async function handleCompletion ( c : Context ) {
2815 let payload = await c . req . json < ChatCompletionsPayload > ( )
@@ -38,9 +25,19 @@ export async function handleCompletion(c: Context) {
3825 }
3926 }
4027
41- if ( payload . stream ) {
42- return handleStreaming ( c , payload )
28+ const response = await createChatCompletions ( payload )
29+
30+ if ( isNonStreaming ( response ) ) {
31+ return c . json ( response )
4332 }
4433
45- return handleNonStreaming ( c , payload )
34+ return streamSSE ( c , async ( stream ) => {
35+ for await ( const chunk of response ) {
36+ await stream . writeSSE ( chunk as SSEMessage )
37+ }
38+ } )
4639}
40+
41+ const isNonStreaming = (
42+ response : Awaited < ReturnType < typeof createChatCompletions > > ,
43+ ) : response is ChatCompletionResponse => Object . hasOwn ( response , "choices" )
0 commit comments