Integrate backend APIs and move review to center editor tab
This commit is contained in:
45
src/core/ChatClientApi.js
Normal file
45
src/core/ChatClientApi.js
Normal file
@@ -0,0 +1,45 @@
|
||||
import { ApiHttpClient } from "./ApiHttpClient.js";
|
||||
|
||||
export class ChatClientApi {
|
||||
constructor(http = new ApiHttpClient(), pollMs = 700, timeoutMs = 120000) {
|
||||
this.http = http;
|
||||
this.pollMs = pollMs;
|
||||
this.timeoutMs = timeoutMs;
|
||||
}
|
||||
|
||||
async createDialog(ragSessionId) {
|
||||
const response = await this.http.request("/api/chat/dialogs", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({ rag_session_id: ragSessionId })
|
||||
});
|
||||
return response.dialog_session_id;
|
||||
}
|
||||
|
||||
async sendMessage(payload) {
|
||||
const queued = await this.http.request("/api/chat/messages", {
|
||||
method: "POST",
|
||||
body: JSON.stringify({
|
||||
dialog_session_id: payload.dialog_session_id,
|
||||
rag_session_id: payload.rag_session_id,
|
||||
message: payload.message,
|
||||
attachments: payload.attachments || []
|
||||
})
|
||||
});
|
||||
|
||||
const taskId = queued.task_id;
|
||||
const started = Date.now();
|
||||
while (Date.now() - started < this.timeoutMs) {
|
||||
const status = await this.http.request(`/api/tasks/${encodeURIComponent(taskId)}`);
|
||||
if (status.status === "done") return status;
|
||||
if (status.status === "error") {
|
||||
throw new Error(status.error?.desc || "Task failed");
|
||||
}
|
||||
await this.#sleep(this.pollMs);
|
||||
}
|
||||
throw new Error("Task polling timeout");
|
||||
}
|
||||
|
||||
#sleep(ms) {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user