Skip to content

Open-source LLM knowledge platform: turn raw documents into a queryable RAG, an autonomous reasoning agent, and a self-maintaining Wiki.

6.3 KB
Raw Blame
import { get, post, put, del, postChat, getDown } from "../../utils/request";

export type SessionKind = '' | 'assistant';

export interface SessionSummary {
  id: string;
  kind: SessionKind;
  title: string;
  created_at: string;
  updated_at: string;
}

export interface CreateSessionRequest {
  title?: string;
  description?: string;
  project_dir?: string;
  kind?: SessionKind;
}

export async function createSessions(data: CreateSessionRequest = {}) {
  return post("/api/v1/sessions", data);
}

export async function getSessionsList(page: number, page_size: number, source?: string, kind?: SessionKind) {
  const params = new URLSearchParams({ page: String(page), page_size: String(page_size) });
  if (source) {
    params.set("source", source);
  }
  if (kind) params.set("kind", kind);
  return get(`/api/v1/sessions?${params.toString()}`);
}

export async function pinSession(session_id: string) {
  return post(`/api/v1/sessions/${session_id}/pin`, {});
}

export async function unpinSession(session_id: string) {
  return del(`/api/v1/sessions/${session_id}/pin`);
}

export async function generateSessionsTitle(session_id: string, data: any) {
  return post(`/api/v1/sessions/${session_id}/generate_title`, data);
}

export async function updateSession(session_id: string, data: { title: string; description?: string }) {
  return put(`/api/v1/sessions/${session_id}`, data);
}

export async function forkSession(
  session_id: string,
  data: { message_id: string; title?: string },
) {
  return post(`/api/v1/sessions/${session_id}/fork`, data, { timeout: 180000 });
}

export async function rewindSession(
  session_id: string,
  data: { message_id: string },
) {
  return post(`/api/v1/sessions/${session_id}/rewind`, data, { timeout: 180000 });
}

export async function knowledgeChat(data: { session_id: string; query: string; }) {
  return postChat(`/api/v1/knowledge-chat/${data.session_id}`, { query: data.query, channel: "web" });
}

// Agent chat with streaming support
export async function agentChat(data: { 
  session_id: string; 
  query: string;
  knowledge_base_ids?: string[];
  agent_enabled: boolean;
}) {
  return postChat(`/api/v1/agent-chat/${data.session_id}`, { 
    query: data.query,
    knowledge_base_ids: data.knowledge_base_ids,
    agent_enabled: data.agent_enabled,
    channel: "web"
  });
}

export async function getMessageList(data: { session_id: string; limit: number, created_at: string }) {
  if (data.created_at) {
    return get(`/api/v1/messages/${data.session_id}/load?before_time=${encodeURIComponent(data.created_at)}&limit=${data.limit}`);
  } else {
    return get(`/api/v1/messages/${data.session_id}/load?limit=${data.limit}`);
  }
}

export async function delSession(session_id: string) {
  return del(`/api/v1/sessions/${session_id}`);
}

export async function batchDelSessions(ids: string[]) {
  return del(`/api/v1/sessions/batch`, { ids });
}

export async function deleteAllSessions() {
  return del(`/api/v1/sessions/batch`, { delete_all: true });
}

export async function getSession(session_id: string) {
  return get(`/api/v1/sessions/${session_id}`);
}

export async function stopSession(session_id: string, message_id: string) {
  return post(`/api/v1/sessions/${session_id}/stop`, { message_id });
}

export async function clearSessionMessages(session_id: string) {
  return del(`/api/v1/sessions/${session_id}/messages`);
}

// ---------------------------------------------------------------------------
// Skill artifacts (files generated by skill scripts during agent turns)
//
// The backend keeps the physical storage path private and indexes each file by
// its position (:index) in the owning assistant message. The list endpoints
// return metadata only — clients must go through downloadArtifact to fetch
// the actual bytes so ownership checks always run server-side.
// ---------------------------------------------------------------------------

export interface ArtifactMeta {
  index: number;
  /**
   * `resource://<handle>` — the artifact's stable identity, and the destination
   * the answer body references. Empty when the deployment runs without a
   * resource catalog, in which case the body references files by name.
   */
  handle?: string;
  file_name: string;
  file_type: string;
  file_size: number;
  source_path: string;
  mod_time: string;
  created_at: string;
  /**
   * Set when the user deleted the file. The entry stays in the list so the
   * artifacts after it keep their index — which is their download address —
   * so clients filter on this rather than on position.
   */
  deleted_at?: string;
}

// listMessageArtifacts returns the artifacts attached to a single assistant
// message. Used by the drawer opened from the message action toolbar.
export async function listMessageArtifacts(session_id: string, message_id: string) {
  return get(
    `/api/v1/sessions/${session_id}/messages/${message_id}/artifacts`,
  );
}

// listSessionArtifacts returns every artifact recorded against the whole
// session, in creation order. Powers "download everything generated in this
// chat" drawers.
export async function listSessionArtifacts(session_id: string) {
  return get(`/api/v1/sessions/${session_id}/artifacts`);
}

// downloadArtifact streams a single artifact's bytes as a Blob so the caller
// can trigger a browser download. A plain `<a href>` cannot be used because
// axios attaches the Bearer token per request; issuing the download via
// `getDown` keeps auth intact and lets the response be turned into an
// object URL.
export async function downloadArtifact(
  session_id: string,
  message_id: string,
  index: number,
): Promise<Blob> {
  return getDown(
    `/api/v1/sessions/${session_id}/messages/${message_id}/artifacts/${index}/download`,
  );
}

// deleteMessageArtifact removes a generated file from the session. The stored
// bytes are reclaimed, so this is not reversible; callers confirm first.
//
// The in-chat panel lists every regeneration of a file as its own row, so it
// deletes exactly the row the user clicked. The artifact library folds them
// into one entry and passes all_versions.
export async function deleteMessageArtifact(
  session_id: string,
  message_id: string,
  index: number,
  allVersions = false,
) {
  const query = allVersions ? '?all_versions=true' : '';
  return del(
    `/api/v1/sessions/${session_id}/messages/${message_id}/artifacts/${index}${query}`,
  );
}