@silvana-one/coordination
Version:
Silvana Coordination Client
168 lines • 5.88 kB
JavaScript
import { Transaction } from "@mysten/sui/transactions";
import { SUI_CLOCK_OBJECT_ID } from "@mysten/sui/utils";
import { silvanaRegistryPackage } from "./package.js";
import { fetchSuiDynamicField, fetchSuiObject } from "./fetch.js";
export class JobManager {
constructor(params) {
this.jobs = params.jobs;
}
static createJobs(maxAttempts) {
const tx = new Transaction();
tx.moveCall({
target: `${silvanaRegistryPackage}::jobs::create_jobs`,
arguments: [
tx.pure.option("u8", maxAttempts ?? null),
],
});
return tx;
}
createJob(params) {
const { description, developer, agent, agentMethod, app, appInstance, appInstanceMethod, sequences, data, } = params;
const tx = new Transaction();
tx.moveCall({
target: `${silvanaRegistryPackage}::jobs::create_job`,
arguments: [
tx.object(this.jobs),
tx.pure.option("string", description ?? null),
tx.pure.string(developer),
tx.pure.string(agent),
tx.pure.string(agentMethod),
tx.pure.string(app),
tx.pure.string(appInstance),
tx.pure.string(appInstanceMethod),
tx.pure.option("vector<u64>", sequences ?? null),
tx.pure.vector("u8", Array.from(data)),
tx.object(SUI_CLOCK_OBJECT_ID),
],
});
return tx;
}
startJob(jobId) {
const tx = new Transaction();
tx.moveCall({
target: `${silvanaRegistryPackage}::jobs::start_job`,
arguments: [
tx.object(this.jobs),
tx.pure.u64(jobId),
tx.object(SUI_CLOCK_OBJECT_ID),
],
});
return tx;
}
completeJob(jobId) {
const tx = new Transaction();
tx.moveCall({
target: `${silvanaRegistryPackage}::jobs::complete_job`,
arguments: [
tx.object(this.jobs),
tx.pure.u64(jobId),
],
});
return tx;
}
failJob(params) {
const { jobId, error } = params;
const tx = new Transaction();
tx.moveCall({
target: `${silvanaRegistryPackage}::jobs::fail_job`,
arguments: [
tx.object(this.jobs),
tx.pure.u64(jobId),
tx.pure.string(error),
tx.object(SUI_CLOCK_OBJECT_ID),
],
});
return tx;
}
updateMaxAttempts(maxAttempts) {
const tx = new Transaction();
tx.moveCall({
target: `${silvanaRegistryPackage}::jobs::update_max_attempts`,
arguments: [
tx.object(this.jobs),
tx.pure.u64(maxAttempts),
],
});
return tx;
}
async getJob(jobId) {
try {
const jobsObject = await fetchSuiObject(this.jobs);
if (!jobsObject)
return undefined;
const jobsTableId = jobsObject?.jobs?.fields?.id?.id;
if (!jobsTableId)
return undefined;
const job = await fetchSuiDynamicField({
parentID: jobsTableId,
fieldName: "jobs",
type: "u64",
key: String(jobId),
});
if (!job)
return undefined;
const parseStatus = (status) => {
if (status?.Pending !== undefined)
return { type: "Pending" };
if (status?.Running !== undefined)
return { type: "Running" };
if (status?.Failed !== undefined)
return { type: "Failed", error: status.Failed };
return { type: "Pending" };
};
return {
id: job?.id?.id,
jobId: Number(job.job_id),
description: job?.description ?? undefined,
developer: job.developer,
agent: job.agent,
agentMethod: job.agent_method,
app: job.app,
appInstance: job.app_instance,
appInstanceMethod: job.app_instance_method,
sequences: job?.sequences?.map((s) => Number(s)) ?? undefined,
data: new Uint8Array(job.data),
status: parseStatus(job.status),
attempts: Number(job.attempts),
createdAt: Number(job.created_at),
updatedAt: Number(job.updated_at),
};
}
catch (error) {
console.error("Error fetching job:", error);
return undefined;
}
}
async getPendingJobs() {
try {
const jobsObject = await fetchSuiObject(this.jobs);
if (!jobsObject)
return [];
const pendingJobs = jobsObject?.pending_jobs?.fields?.contents;
if (!Array.isArray(pendingJobs))
return [];
return pendingJobs.map((id) => Number(id));
}
catch (error) {
console.error("Error fetching pending jobs:", error);
return [];
}
}
async getNextPendingJob() {
const pendingJobs = await this.getPendingJobs();
return pendingJobs.length > 0 ? pendingJobs[0] : undefined;
}
async getMaxAttempts() {
try {
const jobsObject = await fetchSuiObject(this.jobs);
if (!jobsObject)
return 3; // default value
return Number(jobsObject?.max_attempts ?? 3);
}
catch (error) {
console.error("Error fetching max attempts:", error);
return 3;
}
}
}
//# sourceMappingURL=job.js.map