Skip to content

Commit 465ef73

Browse files
committed
feat: Enable streaming by default for chat completions
1 parent ec7f858 commit 465ef73

3 files changed

Lines changed: 13 additions & 37 deletions

File tree

src/lib/cli.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ const args = {
1010
stream: {
1111
alias: "s",
1212
type: "boolean",
13-
default: false,
13+
default: true,
1414
description: "Enable streaming response for chat completions",
1515
},
1616
port: {

src/routes/chat-completions/route.ts

Lines changed: 10 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,21 @@
11
import consola from "consola"
22
import { Hono } from "hono"
3-
import { streamSSE } from "hono/streaming"
43
import { FetchError } from "ofetch"
54

6-
import type { ChatCompletionsPayload } from "~/services/copilot/chat-completions/types"
5+
import { ENV } from "~/config/env"
76

8-
import { chatCompletions } from "~/services/copilot/chat-completions/service"
7+
import { handler } from "./handler"
8+
import { handlerStreaming } from "./handler-streaming"
99

10-
import { createContentChunk, createFinalChunk, segmentResponse } from "./utils"
10+
export const completionRoutes = new Hono()
1111

12-
export const chatCompletionsRoutes = new Hono()
13-
14-
chatCompletionsRoutes.post("/chat/completions", async (c) => {
12+
completionRoutes.post("/chat/completions", (c) => {
1513
try {
16-
const payload = await c.req.json<ChatCompletionsPayload>()
17-
payload.stream = false
18-
19-
consola.info(`Received request: ${JSON.stringify(payload).slice(0, 500)}`)
20-
21-
const response = await chatCompletions(payload)
22-
consola.info(`Response from Copilot: ${JSON.stringify(response)}`)
23-
24-
const segments = segmentResponse(response.choices[0].message.content)
25-
const chunks = segments.map((segment) =>
26-
createContentChunk(segment, response, payload.model),
27-
)
28-
29-
chunks.push(createFinalChunk(response, payload.model))
30-
31-
consola.info(
32-
`Streaming response, first chunk: ${JSON.stringify(chunks.at(0))}`,
33-
)
34-
35-
return streamSSE(c, async (stream) => {
36-
for (const chunk of chunks) {
37-
await stream.writeSSE({
38-
data: JSON.stringify(chunk.data),
39-
})
40-
await stream.sleep(1) // Simulated latency
41-
}
42-
})
14+
if (ENV.ENABLE_STREAMING) {
15+
return handlerStreaming(c)
16+
} else {
17+
return handler(c)
18+
}
4319
} catch (error) {
4420
if (error instanceof FetchError) {
4521
consola.error(`Request failed: ${error.message}`, error.response?._data)

src/server.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Hono } from "hono"
22
import { cors } from "hono/cors"
33
import { logger } from "hono/logger"
44

5-
import { chatCompletionsRoutes } from "./routes/chat-completions/route"
5+
import { completionRoutes } from "./routes/chat-completions/route"
66
import { modelRoutes } from "./routes/models/route"
77

88
export const server = new Hono()
@@ -11,5 +11,5 @@ server.use(logger())
1111
server.use(cors())
1212

1313
server.get("/", (c) => c.text("Server running"))
14-
server.route("/", chatCompletionsRoutes)
14+
server.route("/", completionRoutes)
1515
server.route("/", modelRoutes)

0 commit comments

Comments
 (0)