knowledge-service.ts


Overview

knowledge-service.ts is a service module that provides a comprehensive API client layer for managing knowledge bases, documents, chunks, tags, and related data within an application. It acts as an abstraction over HTTP requests to backend endpoints, centralizing and standardizing interactions with knowledge-related APIs.

This file defines a collection of REST API method configurations and exports a registered service instance (kbService) with strongly-typed method keys for convenient use. Additionally, it exports several helper functions for tag management, knowledge graph operations, dataset listing, and document filtering.

The module plays a pivotal role in the system by enabling frontend or other clients to perform CRUD operations and other business logic on knowledge bases, documents, and their components seamlessly.


Detailed Explanation

1. Imported Interfaces and Utilities


2. methods Object

This object maps method names to their corresponding API endpoint URLs and HTTP methods. It is organized into domains:

Each entry contains:


3. kbService

const kbService = registerServer<keyof typeof methods>(methods, request);

4. Exported Helper Functions

These functions provide more specific API interactions that often require parameters or more complex request bodies:

listTag

export const listTag = (knowledgeId: string) => request.get(api.listTag(knowledgeId));

removeTag

export const removeTag = (knowledgeId: string, tags: string[]) => post(api.removeTag(knowledgeId), { tags });

renameTag

export const renameTag = (knowledgeId: string, { fromTag, toTag }: IRenameTag) => post(api.renameTag(knowledgeId), { fromTag, toTag });

getKnowledgeGraph

export function getKnowledgeGraph(knowledgeId: string) {
  return request.get(api.getKnowledgeGraph(knowledgeId));
}

deleteKnowledgeGraph

export function deleteKnowledgeGraph(knowledgeId: string) {
  return request.delete(api.getKnowledgeGraph(knowledgeId));
}

listDataset

export const listDataset = (
  params?: IFetchKnowledgeListRequestParams,
  body?: IFetchKnowledgeListRequestBody,
) => request.post(api.kb_list, { data: body || {}, params });

listDocument

export const listDocument = (
  params?: IFetchKnowledgeListRequestParams,
  body?: IFetchDocumentListRequestBody,
) => request.post(api.get_document_list, { data: body || {}, params });

documentFilter

export const documentFilter = (kb_id: string) =>
  request.post(api.get_dataset_filter, { kb_id });

Important Implementation Details


Interaction With Other System Parts


Usage Examples

import kbService, { listTag, renameTag } from '@/services/knowledge-service';

// Create a new knowledge base
kbService.createKb({ name: 'New KB', description: 'Sample knowledge base' })
  .then(response => console.log('Created:', response));

// List tags for a knowledge base
listTag('kb123')
  .then(tags => console.log('Tags:', tags));

// Rename a tag
renameTag('kb123', { fromTag: 'oldTag', toTag: 'newTag' })
  .then(() => console.log('Tag renamed successfully'));

Mermaid Class Diagram

classDiagram
    class kbService {
        <<registered service>>
        +createKb(data)
        +updateKb(data)
        +rmKb(data)
        +get_kb_detail(params)
        +getList(data)
        +get_document_list(params)
        +document_change_status(data)
        +document_rm(data)
        +document_rename(data)
        +document_create(data)
        +document_run(data)
        +document_change_parser(data)
        +document_thumbnails(params)
        +document_upload(data)
        +web_crawl(data)
        +document_infos(data)
        +setMeta(data)
        +chunk_list(data)
        +create_chunk(data)
        +set_chunk(data)
        +get_chunk(params)
        +switch_chunk(data)
        +rm_chunk(data)
        +retrieval_test(data)
        +knowledge_graph(params)
        +document_delete(params)
        +upload_and_parse(data)
        +listTagByKnowledgeIds(params)
        +documentFilter(data)
        +getMeta(params)
        +retrievalTestShare(data)
    }

    class HelperFunctions {
        +listTag(knowledgeId)
        +removeTag(knowledgeId, tags)
        +renameTag(knowledgeId, {fromTag, toTag})
        +getKnowledgeGraph(knowledgeId)
        +deleteKnowledgeGraph(knowledgeId)
        +listDataset(params?, body?)
        +listDocument(params?, body?)
        +documentFilter(kb_id)
    }

    kbService <.. HelperFunctions : uses

Summary

The knowledge-service.ts file is a core service module that encapsulates all knowledge base and document management HTTP interactions. It provides a strongly-typed, well-organized, and reusable API layer that facilitates CRUD and advanced operations on knowledge entities, chunks, tags, and retrieval tests. By centralizing endpoint calls and request configurations, it improves maintainability and developer productivity across the application.