@@ -37,74 +37,54 @@ function createCondensedStreamingResponse(
3737 }
3838}
3939
40- export async function handlerStreaming ( c : Context ) {
41- const models = modelsCache . getModels ( )
42- let payload = await c . req . json < ChatCompletionsPayload > ( )
43-
44- if ( isNullish ( payload . max_tokens ) ) {
45- const selectedModel = models ?. data . find (
46- ( model ) => model . id === payload . model ,
47- )
48-
49- payload = {
50- ...payload ,
51- max_tokens : selectedModel ?. capabilities . limits . max_output_tokens ,
52- }
53- }
54-
55- // Convert request headers to a regular object from Headers
56- const requestHeaders = c . req . header ( )
57-
58- // Log the request at the beginning for both streaming and non-streaming cases
59- await logger . logRequest ( "/chat/completions" , "POST" , payload , requestHeaders )
60-
61- if ( payload . stream ) {
40+ function handleStreaming ( c : Context , payload : ChatCompletionsPayload ) {
41+ return streamSSE ( c , async ( stream ) => {
6242 const response = await chatCompletionsStream ( payload )
6343
6444 // For collecting the complete streaming response
6545 let collectedContent = ""
6646 let finalChunk : ChatCompletionChunk | null = null
6747
68- return streamSSE ( c , async ( stream ) => {
69- for await ( const chunk of response ) {
70- await stream . writeSSE ( chunk as SSEMessage )
48+ for await ( const chunk of response ) {
49+ await stream . writeSSE ( chunk as SSEMessage )
7150
72- if ( ! logger . options . enabled ) continue
51+ if ( ! logger . options . enabled ) continue
7352
74- // Check if chunk data is "DONE" or not a valid JSON string
75- if ( ! chunk . data || chunk . data === "[DONE]" ) {
76- continue // Skip processing this chunk for logging
77- }
53+ // Check if chunk data is "DONE" or not a valid JSON string
54+ if ( ! chunk . data || chunk . data === "[DONE]" ) {
55+ continue // Skip processing this chunk for logging
56+ }
7857
79- try {
80- const data = JSON . parse ( chunk . data ) as ChatCompletionChunk
58+ try {
59+ const data = JSON . parse ( chunk . data ) as ChatCompletionChunk
8160
82- // Keep track of the latest chunk for metadata
83- finalChunk = data
61+ // Keep track of the latest chunk for metadata
62+ finalChunk = data
8463
85- // Accumulate content from each delta
86- if ( typeof data . choices [ 0 ] . delta . content === "string" ) {
87- collectedContent += data . choices [ 0 ] . delta . content
88- }
89- } catch ( error ) {
90- // Handle JSON parsing errors gracefully
91- consola . error ( `Error parsing SSE chunk data` , error )
92- // Continue processing other chunks
64+ // Accumulate content from each delta
65+ if ( typeof data . choices [ 0 ] . delta . content === "string" ) {
66+ collectedContent += data . choices [ 0 ] . delta . content
9367 }
68+ } catch ( error ) {
69+ // Handle JSON parsing errors gracefully
70+ consola . error ( `Error parsing SSE chunk data` , error )
71+ // Continue processing other chunks
9472 }
73+ }
9574
96- // After streaming completes, log the condensed response
97- if ( finalChunk ) {
98- const condensedResponse = createCondensedStreamingResponse (
99- finalChunk ,
100- collectedContent ,
101- )
75+ // After streaming completes, log the condensed response
76+ if ( finalChunk ) {
77+ const condensedResponse = createCondensedStreamingResponse (
78+ finalChunk ,
79+ collectedContent ,
80+ )
10281
103- await logger . logResponse ( "/chat/completions" , condensedResponse , { } )
104- }
105- } )
106- }
82+ await logger . logResponse ( "/chat/completions" , condensedResponse , { } )
83+ }
84+ } )
85+ }
10786
87+ async function handleNonStreaming ( c : Context , payload : ChatCompletionsPayload ) {
10888 const response = await chatCompletions ( payload )
10989
11090 // Get response headers if any
@@ -115,3 +95,31 @@ export async function handlerStreaming(c: Context) {
11595
11696 return c . json ( response )
11797}
98+
99+ export async function handleCompletion ( c : Context ) {
100+ const models = modelsCache . getModels ( )
101+ let payload = await c . req . json < ChatCompletionsPayload > ( )
102+
103+ if ( isNullish ( payload . max_tokens ) ) {
104+ const selectedModel = models ?. data . find (
105+ ( model ) => model . id === payload . model ,
106+ )
107+
108+ payload = {
109+ ...payload ,
110+ max_tokens : selectedModel ?. capabilities . limits . max_output_tokens ,
111+ }
112+ }
113+
114+ // Convert request headers to a regular object from Headers
115+ const requestHeaders = c . req . header ( )
116+
117+ // Log the request at the beginning for both streaming and non-streaming cases
118+ await logger . logRequest ( "/chat/completions" , "POST" , payload , requestHeaders )
119+
120+ if ( payload . stream ) {
121+ return handleStreaming ( c , payload )
122+ }
123+
124+ return handleNonStreaming ( c , payload )
125+ }
0 commit comments