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

84% Statements 21/25
81.25% Branches 13/16
83.33% Functions 5/6
85% Lines 17/20

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                          2209x     2209x       77x 77x 77x 77x 77x       2209x 3x     3x             2209x           3x           3x 3x       3x             2209x       13x        
import { Conversation } from "@/objects/Model";
import { Dictionary } from "@/types/ext";
import { AxiosRequestConfig } from "axios";
import { dispositionFilename, saveBlob } from "@/util/download";
import { APIRouter } from "../ApiRouter";
 
export class ConversationRouter extends APIRouter {
  constructor(
    host: string,
    token: string | null = null,
    default_config: AxiosRequestConfig | null = null,
    headers: Dictionary<string> = {},
  ) {
    super("conversations", host, token, default_config, headers);
  }
 
  list = async (options?: {
    agentId?: string;
    modelId?: string;
  }): Promise<Conversation[] | null> => {
    const params: Record<string, string> = {};
    if (options?.agentId) params.agent_id = options.agentId;
    if (options?.modelId) params.ai_model_id = options.modelId;
    const res = await this.api.get<Conversation[]>(this.prefix, { params });
    Eif (super.validRes(res)) return res.data;
    return null;
  };
 
  id = async (conversationId: string): Promise<Conversation | null> => {
    const res = await this.api.get<Conversation>(
      `${this.prefix}/${conversationId}`,
    );
    Eif (super.validRes(res)) return res.data;
    return null;
  };
 
  /** Download the `index`-th document in a stored message as a PDF. svc-core
   *  renders it from the message as saved, so only a persisted reply has
   *  one. Resolves false when the download failed. */
  downloadDocument = async (
    conversationId: string,
    messageId: string,
    index: number,
    fallbackName = "document.pdf",
  ): Promise<boolean> => {
    const res = await this.api
      .get(
        `${this.prefix}/${conversationId}/messages/${messageId}/documents/${index}.pdf`,
        { responseType: "blob", withCredentials: true },
      )
      .catch(() => null);
    Iif (res?.status !== 200) return false;
    saveBlob(
      res.data as Blob,
      dispositionFilename(res.headers?.["content-disposition"]) ?? fallbackName,
    );
    return true;
  };
 
  /** Where an answer's document image is served: the copy svc-ai stored when
   *  it made the document, named by its hash. An `<img src>`, not a request
   *  of this router's own — the browser sends the session cookie. */
  documentImageUrl = (
    conversationId: string,
    messageId: string,
    asset: string,
  ): string =>
    this.api.endpoint(
      `${this.prefix}/${conversationId}/messages/${messageId}/images/${encodeURIComponent(asset)}`,
    );
}