data: {"id":"chatcmpl-mock-123","object":"chat.completion.chunk","created":1790291449054,"choices":[{"index":0,"delta":{"content":"Next "},"finish_reason":null}]}
data: {"id":"chatcmpl-mock-123","object":"chat.completion.chunk","created":1790291449054,"choices":[{"index":0,"delta":{"content":"DevUtilities "},"finish_reason":null}]}
data: {"id":"chatcmpl-mock-123","object":"chat.completion.chunk","created":1790291449054,"choices":[{"index":0,"delta":{"content":"is "},"finish_reason":null}]}
data: {"id":"chatcmpl-mock-123","object":"chat.completion.chunk","created":1790291449054,"choices":[{"index":0,"delta":{"content":"an "},"finish_reason":null}]}
data: {"id":"chatcmpl-mock-123","object":"chat.completion.chunk","created":1790291449054,"choices":[{"index":0,"delta":{"content":"ultra-fast "},"finish_reason":null}]}
data: {"id":"chatcmpl-mock-123","object":"chat.completion.chunk","created":1790291449054,"choices":[{"index":0,"delta":{"content":"developer "},"finish_reason":null}]}
data: {"id":"chatcmpl-mock-123","object":"chat.completion.chunk","created":1790291449054,"choices":[{"index":0,"delta":{"content":"workstation "},"finish_reason":null}]}
data: {"id":"chatcmpl-mock-123","object":"chat.completion.chunk","created":1790291449054,"choices":[{"index":0,"delta":{"content":"with "},"finish_reason":null}]}
data: {"id":"chatcmpl-mock-123","object":"chat.completion.chunk","created":1790291449054,"choices":[{"index":0,"delta":{"content":"over "},"finish_reason":null}]}
data: {"id":"chatcmpl-mock-123","object":"chat.completion.chunk","created":1790291449054,"choices":[{"index":0,"delta":{"content":"170 "},"finish_reason":null}]}
data: {"id":"chatcmpl-mock-123","object":"chat.completion.chunk","created":1790291449054,"choices":[{"index":0,"delta":{"content":"studio "},"finish_reason":null}]}
data: {"id":"chatcmpl-mock-123","object":"chat.completion.chunk","created":1790291449054,"choices":[{"index":0,"delta":{"content":"tools."},"finish_reason":"stop"}]}
data: [DONE]
// app/api/stream/route.ts
export async function GET() {
const encoder = new TextEncoder();
const stream = new ReadableStream({
async start(controller) {
const chunks = ["data: {\"id\":\"chatcmpl-mock-123\",\"object\":\"chat.completion.chunk\",\"created\":1790291449054,\"choices\":[{\"index\":0,\"delta\":{\"content\":\"Next \"},\"finish_reason\":null}]}\n\n","data: {\"id\":\"chatcmpl-mock-123\",\"object\":\"chat.completion.chunk\",\"created\":1790291449054,\"choices\":[{\"index\":0,\"delta\":{\"content\":\"DevUtilities \"},\"finish_reason\":null}]}\n\n","data: {\"id\":\"chatcmpl-mock-123\",\"object\":\"chat.completion.chunk\",\"created\":1790291449054,\"choices\":[{\"index\":0,\"delta\":{\"content\":\"is \"},\"finish_reason\":null}]}\n\n","data: {\"id\":\"chatcmpl-mock-123\",\"object\":\"chat.completion.chunk\",\"created\":1790291449054,\"choices\":[{\"index\":0,\"delta\":{\"content\":\"an \"},\"finish_reason\":null}]}\n\n","data: {\"id\":\"chatcmpl-mock-123\",\"object\":\"chat.completion.chunk\",\"created\":1790291449054,\"choices\":[{\"index\":0,\"delta\":{\"content\":\"ultra-fast \"},\"finish_reason\":null}]}\n\n","data: {\"id\":\"chatcmpl-mock-123\",\"object\":\"chat.completion.chunk\",\"created\":1790291449054,\"choices\":[{\"index\":0,\"delta\":{\"content\":\"developer \"},\"finish_reason\":null}]}\n\n","data: {\"id\":\"chatcmpl-mock-123\",\"object\":\"chat.completion.chunk\",\"created\":1790291449054,\"choices\":[{\"index\":0,\"delta\":{\"content\":\"workstation \"},\"finish_reason\":null}]}\n\n","data: {\"id\":\"chatcmpl-mock-123\",\"object\":\"chat.completion.chunk\",\"created\":1790291449054,\"choices\":[{\"index\":0,\"delta\":{\"content\":\"with \"},\"finish_reason\":null}]}\n\n","data: {\"id\":\"chatcmpl-mock-123\",\"object\":\"chat.completion.chunk\",\"created\":1790291449054,\"choices\":[{\"index\":0,\"delta\":{\"content\":\"over \"},\"finish_reason\":null}]}\n\n","data: {\"id\":\"chatcmpl-mock-123\",\"object\":\"chat.completion.chunk\",\"created\":1790291449054,\"choices\":[{\"index\":0,\"delta\":{\"content\":\"170 \"},\"finish_reason\":null}]}\n\n","data: {\"id\":\"chatcmpl-mock-123\",\"object\":\"chat.completion.chunk\",\"created\":1790291449054,\"choices\":[{\"index\":0,\"delta\":{\"content\":\"studio \"},\"finish_reason\":null}]}\n\n","data: {\"id\":\"chatcmpl-mock-123\",\"object\":\"chat.completion.chunk\",\"created\":1790291449054,\"choices\":[{\"index\":0,\"delta\":{\"content\":\"tools.\"},\"finish_reason\":\"stop\"}]}\n\n","data: [DONE]\n\n"];
for (const chunk of chunks) {
controller.enqueue(encoder.encode(chunk));
await new Promise((r) => setTimeout(r, 40));
}
controller.close();
},
});
return new Response(stream, {
headers: {
'Content-Type': 'text/event-stream',
'Cache-Control': 'no-cache, no-transform',
Connection: 'keep-alive',
},
});
}