All files / src/services/api/routers WorkerRouter.tsx

82.75% Statements 24/29
66.66% Branches 10/15
100% Functions 7/7
82.75% Lines 24/29

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86                          2209x     2209x       3x 3x 3x         2209x       3x 3x 3x         2209x     3x 3x 3x         2209x 3x 3x 3x         2209x                                 3x 3x 3x         2209x 3x 3x      
import { HistoricalAPIRouter } from "../HistoricalApiRouter";
import { Dictionary } from "@/types/ext";
import { AxiosRequestConfig } from "axios";
import { AgentDefinition } from "@/objects/Skill";
import { CustomWorker } from "@/objects/Worker";
 
export class WorkerRouter extends HistoricalAPIRouter {
  constructor(
    host: string,
    token: string | null = null,
    default_config: AxiosRequestConfig | null = null,
    headers: Dictionary<string> = {},
  ) {
    super("workers", host, token, default_config, headers);
  }
 
  available = async (params?: {
    role?: string;
    tag?: string;
  }): Promise<AgentDefinition[]> => {
    const res = await this.api.get(this.endpoint("available"), { params });
    Eif (super.validRes(res)) {
      return (res.data as { definitions: AgentDefinition[] }).definitions ?? [];
    }
    return [];
  };
 
  list = async (params?: {
    owner_organisation_id?: string;
    owner_project_id?: string;
  }): Promise<CustomWorker[]> => {
    const res = await this.api.get(this.prefix, { params });
    Eif (super.validRes(res)) {
      return res.data as CustomWorker[];
    }
    return [];
  };
 
  create = async (
    body: Partial<Omit<CustomWorker, "id" | "created_at">> & { name: string },
  ): Promise<CustomWorker | null> => {
    const res = await this.api.post(this.prefix, body);
    Eif (super.validRes(res)) {
      return res.data as CustomWorker;
    }
    return null;
  };
 
  getById = async (id: string): Promise<CustomWorker | null> => {
    const res = await this.api.get(this.endpoint(id));
    Eif (super.validRes(res)) {
      return res.data as CustomWorker;
    }
    return null;
  };
 
  update = async (
    id: string,
    body: Partial<
      Pick<
        CustomWorker,
        | "name"
        | "description"
        | "system_prompt"
        | "tags"
        | "model_overrides"
        | "tools_enabled"
        | "mcp_server_ids"
        | "input_schema"
        | "output_schema"
      >
    >,
  ): Promise<CustomWorker | null> => {
    const res = await this.api.patch<CustomWorker>(this.endpoint(id), body);
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
 
  delete = async (id: string): Promise<boolean> => {
    const res = await this.api.delete(this.endpoint(id));
    return res.status === 204;
  };
}