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

88.88% Statements 32/36
69.23% Branches 9/13
100% Functions 10/10
88.88% Lines 32/36

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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113                                        2209x     2209x 3x 3x 3x         2209x 3x 3x 3x         2209x       3x 3x 3x         2209x       3x 3x 3x         2209x 3x 3x     2209x 3x     3x     2209x       3x     3x                 2209x         3x         3x     2209x     3x       3x      
import {
  CreateToolkitFromConfigRequest,
  CreateToolkitFromConfigResponse,
  GenerateToolkitRequest,
  ToolkitGenerationProposal,
} from "@/objects/ToolkitSetup";
import { Toolkit } from "@/objects/Toolkit";
import { Dictionary } from "@/types/ext";
import { AxiosRequestConfig } from "axios";
import { HistoricalAPIRouter } from "../HistoricalApiRouter";
 
/** Client for /app/toolkits — named bundles of tools and MCP servers that
 *  can be attached to agents as one unit. */
export class ToolkitRouter extends HistoricalAPIRouter {
  constructor(
    host: string,
    token: string | null = null,
    default_config: AxiosRequestConfig | null = null,
    headers: Dictionary<string> = {},
  ) {
    super("toolkits", host, token, default_config, headers);
  }
 
  list = async (): Promise<Toolkit[]> => {
    const res = await this.api.get<Toolkit[]>(this.prefix);
    Eif (super.validRes(res)) {
      return res.data;
    }
    return [];
  };
 
  getById = async (id: string): Promise<Toolkit | null> => {
    const res = await this.api.get<Toolkit>(this.endpoint(id));
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
 
  create = async (body: {
    name: string;
    description?: string;
  }): Promise<Toolkit | null> => {
    const res = await this.api.post<Toolkit>(this.prefix, body);
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
 
  update = async (
    id: string,
    body: { name?: string; description?: string },
  ): Promise<Toolkit | null> => {
    const res = await this.api.patch<Toolkit>(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;
  };
 
  addMCPServer = async (id: string, mcpServerId: string): Promise<boolean> => {
    const res = await this.api.post(this.endpoint(`${id}/mcp_servers`), {
      mcp_server_id: mcpServerId,
    });
    return super.validRes(res);
  };
 
  removeMCPServer = async (
    id: string,
    mcpServerId: string,
  ): Promise<boolean> => {
    const res = await this.api.delete(
      this.endpoint(`${id}/mcp_servers/${mcpServerId}`),
    );
    return res.status === 204;
  };
 
  // ── Generative setup ───────────────────────────────────────────────────────
  //
  // Existing-resources-only: unlike the skillset generative flow, this has
  // no proposed-new-resource concept at all — a generated toolkit can only
  // ever select/bundle tools/MCP servers that already exist.
 
  generateConfig = async (
    body: GenerateToolkitRequest,
  ): Promise<ToolkitGenerationProposal> => {
    // Generation is a single long LLM call (30-90s); spread the default
    // config so withCredentials survives the per-request timeout override.
    const res = await this.api.post<ToolkitGenerationProposal>(
      this.endpoint("generate"),
      body,
      { ...(this.api.default_config ?? {}), timeout: 180000 },
    );
    return res.data;
  };
 
  createFromConfig = async (
    body: CreateToolkitFromConfigRequest,
  ): Promise<CreateToolkitFromConfigResponse> => {
    const res = await this.api.post<CreateToolkitFromConfigResponse>(
      this.endpoint("create-from-config"),
      body,
    );
    return res.data;
  };
}