feat: prose completion api

This commit is contained in:
Jiang Feng
2025-04-26 23:12:13 +08:00
parent ba8c5fbcd3
commit 66794a4b73
16 changed files with 395 additions and 32 deletions

View File

@@ -9,7 +9,7 @@ import { sleep } from "../utils";
import { resolveServiceURL } from "./resolve-service-url";
import type { ChatEvent } from "./types";
export function chatStream(
export async function* chatStream(
userMessage: string,
params: {
thread_id: string;
@@ -32,13 +32,19 @@ export function chatStream(
if (location.search.includes("mock") || location.search.includes("replay=")) {
return chatReplayStream(userMessage, params, options);
}
return fetchStream<ChatEvent>(resolveServiceURL("chat/stream"), {
const stream = fetchStream(resolveServiceURL("chat/stream"), {
body: JSON.stringify({
messages: [{ role: "user", content: userMessage }],
...params,
}),
signal: options.abortSignal,
});
for await (const event of stream) {
yield {
type: event.event,
data: JSON.parse(event.data),
} as ChatEvent;
}
}
async function* chatReplayStream(