Skip to content

🤯 LobeHub - Super-Agents on Your Team. Add super-agents to the chats your team already uses.They take on work by themselves and get better every week.

5.3 KB
Raw Blame
import type { ChatSemanticSearchChunk, FileSearchResult } from '@lobechat/types';

export const KnowledgeBaseIdentifier = 'lobe-knowledge-base';

export const KnowledgeBaseApiName = {
  addFiles: 'addFiles',
  createDocument: 'createDocument',
  createKnowledgeBase: 'createKnowledgeBase',
  deleteKnowledgeBase: 'deleteKnowledgeBase',
  getFileDetail: 'getFileDetail',
  listFiles: 'listFiles',
  listKnowledgeBases: 'listKnowledgeBases',
  readKnowledge: 'readKnowledge',
  removeFiles: 'removeFiles',
  searchKnowledgeBase: 'searchKnowledgeBase',
  updateDocument: 'updateDocument',
  viewKnowledgeBase: 'viewKnowledgeBase',
};

// ============ Search & Read ============

export interface SearchKnowledgeBaseArgs {
  query: string;
  topK?: number;
}

/**
 * BM25 hit on a document inside a knowledge base. Covers both inline
 * `custom/document` pages and file-backed documents (parsed PDFs and the like).
 * Mirrors database/repositories/ftsSearch FtsSearchKnowledgeBaseDocumentHit; redeclared
 * here to keep this package decoupled from server-only types.
 */
export interface KnowledgeBaseDocumentResult {
  documentId: string;
  fileId?: string;
  knowledgeBaseId: string;
  relevance: number;
  snippet: string;
  title: string;
  updatedAt: Date | string;
}

export interface SearchKnowledgeBaseState {
  chunks: ChatSemanticSearchChunk[];
  documents: KnowledgeBaseDocumentResult[];
  errors?: { bm25?: string; vector?: string };
  fileResults: FileSearchResult[];
  /**
   * How many hits `fileResults` held, pinned by the read-path projector before
   * it drops the three lists. The collapsed chip shows only this number; the
   * card hydrates the real hits when the row is expanded.
   */
  resultCount?: number;
  /**
   * `'none'` when no knowledge base was in scope, so nothing was searched at
   * all — distinct from a search that ran and matched nothing.
   */
  scope?: 'none';
  totalResults: number;
}

export interface ReadKnowledgeArgs {
  fileIds: string[];
  /** Maximum number of lines to return per file (default 400, max 2000). Some providers send numbers as strings. */
  limit?: number | string;
  /** 1-based line number to start reading from (default 1). Some providers send numbers as strings. */
  offset?: number | string;
}

export interface FileContentDetail {
  /** 1-based inclusive end line of the returned window; `0` when empty. */
  endLine?: number;
  error?: string;
  fileId: string;
  filename: string;
  preview?: string;
  /** 1-based inclusive start line of the returned window. */
  startLine?: number;
  totalCharCount?: number;
  totalLineCount?: number;
  /** True when more lines follow `endLine` and another call with a higher `offset` is needed. */
  truncated?: boolean;
}

export interface ReadKnowledgeState {
  files: FileContentDetail[];
}

// ============ P0: Knowledge Base Visibility ============

export interface ListKnowledgeBasesArgs {}

export interface KnowledgeBaseInfo {
  avatar: string | null;
  description?: string | null;
  id: string;
  name: string;
  updatedAt: Date;
}

export interface ListKnowledgeBasesState {
  knowledgeBases: KnowledgeBaseInfo[];
  total: number;
}

export interface ViewKnowledgeBaseArgs {
  id: string;
  limit?: number;
  offset?: number;
}

export interface KnowledgeBaseFileInfo {
  fileType: string;
  id: string;
  name: string;
  size: number;
  sourceType: string;
  updatedAt: Date;
}

export interface ViewKnowledgeBaseState {
  files: KnowledgeBaseFileInfo[];
  hasMore: boolean;
  knowledgeBase: KnowledgeBaseInfo;
  total: number;
}

// ============ P1: Knowledge Base Management ============

export interface CreateKnowledgeBaseArgs {
  description?: string;
  name: string;
}

export interface CreateKnowledgeBaseState {
  id: string;
}

export interface DeleteKnowledgeBaseArgs {
  id: string;
}

export interface CreateDocumentArgs {
  content: string;
  knowledgeBaseId: string;
  parentId?: string;
  title: string;
}

export interface CreateDocumentState {
  id: string;
}

/**
 * Edit an existing knowledge base document in place, keeping its ID. At least
 * one of `title`, `content` or `parentId` must be set; an omitted field is left
 * unchanged.
 */
export interface UpdateDocumentArgs {
  /** Full replacement body (Markdown). Omit to keep the current body. */
  content?: string;
  documentId: string;
  /** Folder or document in the same knowledge base to move the document under. */
  parentId?: string;
  title?: string;
}

export interface UpdateDocumentState {
  id: string;
}

export interface AddFilesArgs {
  fileIds: string[];
  knowledgeBaseId: string;
}

export interface RemoveFilesArgs {
  fileIds: string[];
  knowledgeBaseId: string;
}

// ============ Resource Library Files ============

export interface ListFilesArgs {
  category?: string;
  limit?: number;
  offset?: number;
  q?: string;
}

export interface FileInfo {
  createdAt: Date;
  fileType: string;
  id: string;
  name: string;
  size: number;
  sourceType: string;
  url: string;
}

export interface ListFilesState {
  files: FileInfo[];
  hasMore: boolean;
  total: number;
}

export interface GetFileDetailArgs {
  id: string;
}

export interface FileDetail {
  createdAt: Date;
  fileType: string;
  id: string;
  metadata?: Record<string, any> | null;
  name: string;
  size: number;
  sourceType: string;
  updatedAt: Date;
  url: string;
}

export interface GetFileDetailState {
  file: FileDetail;
}