Files
pipelines/web/src/repository.js
T

76 lines
2.0 KiB
JavaScript

// Typed access to the existing WebSocket protocol. Request builders and id
// matching live here so the app never constructs frames by hand or invents
// divergent field names. Mirrors android/.../data/PipelinesRepository.kt.
import { Protocol } from "./protocol.js";
import { ProtocolError } from "./client.js";
export class PipelinesRepository {
#client;
constructor(client) {
this.#client = client;
}
get connectionState() {
return this.#client.state;
}
connect(validated) {
this.#client.connect(validated);
}
disconnect() {
this.#client.disconnect();
}
ping() {
return this.#client.call({ type: Protocol.PING });
}
async listPipelines() {
return (await this.#client.call({ type: Protocol.LIST })).pipelines ?? [];
}
async getPipeline(id, version) {
const response = await this.#client.call({
type: Protocol.GET_PIPELINE,
pipeline_id: id,
version,
});
if (!response.detail) {
throw new ProtocolError("The server returned no pipeline detail.");
}
return response.detail;
}
async listTemplates() {
return (await this.#client.call({ type: Protocol.LIST_TEMPLATES })).templates ?? [];
}
async validate(source) {
const response = await this.#client.call({ type: Protocol.VALIDATE, source });
// A server error envelope already rejects inside client.call, so a reply
// here is `validated`; a missing graph is still an unusable definition.
if (!response.graph) {
throw new ProtocolError("The server returned no graph.");
}
return response.graph;
}
async listRuns() {
const response = await this.#client.call({ type: Protocol.LIST_RUNS });
return { runs: response.runs ?? [], usage: response.usage ?? null };
}
async getRun(id) {
const response = await this.#client.call({
type: Protocol.GET_RUN,
run_id: id,
});
if (!response.run_detail) {
throw new ProtocolError("The server returned no run detail.");
}
return response.run_detail;
}
}