in src/features/chat-page/chat-services/chat-api/open-ai-stream.ts [19:108]
async start(controller) {
const streamResponse = (event: string, value: string) => {
controller.enqueue(encoder.encode(`event: ${event} \n`));
controller.enqueue(encoder.encode(`data: ${value} \n\n`));
};
let lastMessage = "";
runner
.on("content", (content) => {
const completion = runner.currentChatCompletionSnapshot;
if (completion) {
const response: AzureChatCompletion = {
type: "content",
response: completion,
};
lastMessage = completion.choices[0].message.content ?? "";
streamResponse(response.type, JSON.stringify(response));
}
})
.on("functionCall", async (functionCall) => {
await CreateChatMessage({
name: functionCall.name,
content: functionCall.arguments,
role: "function",
chatThreadId: chatThread.id,
});
const response: AzureChatCompletion = {
type: "functionCall",
response: functionCall,
};
streamResponse(response.type, JSON.stringify(response));
})
.on("functionCallResult", async (functionCallResult) => {
const response: AzureChatCompletion = {
type: "functionCallResult",
response: functionCallResult,
};
await CreateChatMessage({
name: "tool",
content: functionCallResult,
role: "function",
chatThreadId: chatThread.id,
});
streamResponse(response.type, JSON.stringify(response));
})
.on("abort", (error) => {
const response: AzureChatCompletionAbort = {
type: "abort",
response: "Chat aborted",
};
streamResponse(response.type, JSON.stringify(response));
controller.close();
})
.on("error", async (error) => {
console.log("🔴 error", error);
const response: AzureChatCompletion = {
type: "error",
response: error.message,
};
// if there is an error still save the last message even though it is not complete
await CreateChatMessage({
name: AI_NAME,
content: lastMessage,
role: "assistant",
chatThreadId: props.chatThread.id,
});
streamResponse(response.type, JSON.stringify(response));
controller.close();
})
.on("finalContent", async (content: string) => {
await CreateChatMessage({
name: AI_NAME,
content: content,
role: "assistant",
chatThreadId: props.chatThread.id,
});
const response: AzureChatCompletion = {
type: "finalContent",
response: content,
};
streamResponse(response.type, JSON.stringify(response));
controller.close();
});
},