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

89.09% Statements 49/55
80% Branches 36/45
100% Functions 15/15
89.09% Lines 49/55

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 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180                                                  2209x         2209x     57x       57x 57x             2209x     19x       19x       2209x 29x 29x       2209x 3x     3x       2209x 15x 15x         2209x       15x     15x     2209x 29x 29x 29x         2209x                   4x 4x 4x         2209x                       6x       5x 5x         2209x 4x 4x     2209x 29x     29x 29x               2209x 11x     11x 11x         2209x 4x     4x     2209x       4x     4x      
import {
  Knowledge,
  KnowledgeCategoryKey,
  KnowledgeRepository,
  KnowledgeUsage,
  RepositoryAgentGrant,
  RepositoryStats,
  RepositoryStatsDetail,
  RepositoryTrust,
  SourceMap,
} from "@/objects/Knowledge";
import { Dictionary } from "@/types/ext";
import { AxiosRequestConfig } from "axios";
import { HistoricalAPIRouter } from "../HistoricalApiRouter";
import { isObjectWith } from "./KnowledgeRouter";
 
/** Client for /app/repositories — knowledge repositories (collections of
 *  knowledge base sources that can be attached to agents as one unit). */
export class RepositoryRouter extends HistoricalAPIRouter {
  constructor(
    host: string,
    token: string | null = null,
    default_config: AxiosRequestConfig | null = null,
    headers: Dictionary<string> = {},
  ) {
    super("repositories", host, token, default_config, headers);
  }
 
  /** `includeSystem` adds the platform's read-only repositories (the CHAIN-R
   *  User Guide) — for pickers that grant a repository to an agent. */
  list = async ({
    includeSystem = false,
  }: { includeSystem?: boolean } = {}): Promise<KnowledgeRepository[]> => {
    const res = await this.api.get<KnowledgeRepository[]>(
      this.prefix,
      includeSystem ? { params: { include_system: true } } : undefined,
    );
    Eif (super.validRes(res)) {
      return res.data;
    }
    return [];
  };
 
  /** The figures of every repository the listing shows (same parameters),
   *  read at once for the cards. */
  stats = async ({
    includeSystem = false,
  }: { includeSystem?: boolean } = {}): Promise<RepositoryStats[]> => {
    const res = await this.api.get<RepositoryStats[]>(
      this.endpoint("stats"),
      includeSystem ? { params: { include_system: true } } : undefined,
    );
    return super.validRes(res) ? res.data : [];
  };
 
  /** One repository's figures, with each of its sources'. */
  repositoryStats = async (id: string): Promise<RepositoryStatsDetail | null> => {
    const res = await this.api.get<RepositoryStatsDetail>(this.endpoint(`${id}/stats`));
    return super.validRes(res) && isObjectWith(res.data, "repository") ? res.data : null;
  };
 
  /** The answers in this workspace citing any of its sources over `days`. */
  usage = async (id: string, days = 90): Promise<KnowledgeUsage | null> => {
    const res = await this.api.get<KnowledgeUsage>(this.endpoint(`${id}/usage`), {
      params: { days },
    });
    return super.validRes(res) && isObjectWith(res.data, "daily") ? res.data : null;
  };
 
  /** Each source's trust signals, and how many are in each state. */
  trust = async (id: string): Promise<RepositoryTrust | null> => {
    const res = await this.api.get<RepositoryTrust>(this.endpoint(`${id}/trust`));
    return super.validRes(res) && isObjectWith(res.data, "summary") ? res.data : null;
  };
 
  /** Its sources connected by evidence, with the agents granted it and the
   *  workspace's sources the evidence ties to it. */
  map = async (
    id: string,
    { agents = true, suggestions = true }: { agents?: boolean; suggestions?: boolean } = {},
  ): Promise<SourceMap | null> => {
    const res = await this.api.get<SourceMap>(this.endpoint(`${id}/map`), {
      params: { agents, suggestions },
    });
    return super.validRes(res) && isObjectWith(res.data, "nodes") ? res.data : null;
  };
 
  getById = async (id: string): Promise<KnowledgeRepository | null> => {
    const res = await this.api.get<KnowledgeRepository>(this.endpoint(id));
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
 
  create = async (body: {
    name: string;
    description?: string;
    icon?: string;
    accent?: string;
    /** What kind of knowledge it holds (`knowledge/categories.ts`). */
    category?: KnowledgeCategoryKey;
    owner_organisation_id?: string | null;
    owner_project_id?: string | null;
  }): Promise<KnowledgeRepository | null> => {
    const res = await this.api.post<KnowledgeRepository>(this.prefix, body);
    Eif (super.validRes(res)) {
      return res.data;
    }
    return null;
  };
 
  update = async (
    id: string,
    body: {
      name?: string;
      description?: string;
      icon?: string;
      accent?: string;
      /** Refused (409) while one of its sources is in a repository of the
       *  kind it is leaving. */
      category?: KnowledgeCategoryKey;
    },
  ): Promise<KnowledgeRepository | null> => {
    const res = await this.api.patch<KnowledgeRepository>(
      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;
  };
 
  listKnowledge = async (id: string): Promise<Knowledge[]> => {
    const res = await this.api.get<Knowledge[]>(
      this.endpoint(`${id}/knowledge`),
    );
    Eif (super.validRes(res)) {
      return res.data;
    }
    return [];
  };
 
  /** The caller's own agents this repository is granted to. Granting and
   *  revoking go through the agent (`AgentRouter.addAgentRepository`), since
   *  a grant is a change to the agent and is versioned on it. */
  listAgents = async (id: string): Promise<RepositoryAgentGrant[]> => {
    const res = await this.api.get<RepositoryAgentGrant[]>(
      this.endpoint(`${id}/agents`),
    );
    Eif (super.validRes(res)) {
      return res.data;
    }
    return [];
  };
 
  addKnowledge = async (id: string, knowledgeId: string): Promise<boolean> => {
    const res = await this.api.post(this.endpoint(`${id}/knowledge`), {
      knowledge_id: knowledgeId,
    });
    return super.validRes(res);
  };
 
  removeKnowledge = async (
    id: string,
    knowledgeId: string,
  ): Promise<boolean> => {
    const res = await this.api.delete(
      this.endpoint(`${id}/knowledge/${knowledgeId}`),
    );
    return res.status === 204;
  };
}