47 lines
1.6 KiB
JavaScript
47 lines
1.6 KiB
JavaScript
import { ApiHttpClient } from "./ApiHttpClient.js";
|
|
|
|
export class IndexingClientApi {
|
|
constructor(http = new ApiHttpClient(), pollMs = 700, timeoutMs = 120000) {
|
|
this.http = http;
|
|
this.pollMs = pollMs;
|
|
this.timeoutMs = timeoutMs;
|
|
}
|
|
|
|
async submitSnapshot(projectId, files) {
|
|
const queued = await this.http.request("/api/rag/sessions", {
|
|
method: "POST",
|
|
body: JSON.stringify({ project_id: projectId, files })
|
|
});
|
|
const status = await this.#pollRagJob(queued.rag_session_id, queued.index_job_id);
|
|
return { ...status, rag_session_id: queued.rag_session_id };
|
|
}
|
|
|
|
async submitChanges(ragSessionId, changedFiles) {
|
|
const queued = await this.http.request(`/api/rag/sessions/${encodeURIComponent(ragSessionId)}/changes`, {
|
|
method: "POST",
|
|
body: JSON.stringify({ changed_files: changedFiles })
|
|
});
|
|
const status = await this.#pollRagJob(ragSessionId, queued.index_job_id);
|
|
return { ...status, rag_session_id: ragSessionId };
|
|
}
|
|
|
|
async #pollRagJob(ragSessionId, jobId) {
|
|
const started = Date.now();
|
|
while (Date.now() - started < this.timeoutMs) {
|
|
const status = await this.http.request(
|
|
`/api/rag/sessions/${encodeURIComponent(ragSessionId)}/jobs/${encodeURIComponent(jobId)}`
|
|
);
|
|
if (status.status === "done") return status;
|
|
if (status.status === "error") {
|
|
throw new Error(status.error?.desc || "Indexing failed");
|
|
}
|
|
await this.#sleep(this.pollMs);
|
|
}
|
|
throw new Error("Index polling timeout");
|
|
}
|
|
|
|
#sleep(ms) {
|
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
}
|
|
}
|