Skip to content

armgit / dify

Public Imported from GitHub
0

Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.

1.5 KB
Raw Blame
import { consoleClient } from '@/service/console'

export type KnowledgeFsUploadItem = {
  file: File
  id: string
  uploadId: string
}

type UploadProgressEntry = {
  phase: KnowledgeFsUploadPhase
}

export type KnowledgeFsUploadPhase = 'completed' | 'pending'
export type KnowledgeFsUploadProgress = Map<string, UploadProgressEntry>

export async function stageKnowledgeFsDocument(file: File, signal?: AbortSignal) {
  const staged = await consoleClient.knowledgeFs.uploads.post(
    {
      body: { file },
    },
    { context: { silent: true }, signal },
  )
  return staged.id
}

export async function discardKnowledgeFsStagedUpload(uploadId: string) {
  await consoleClient.knowledgeFs.uploads.byUploadId.delete(
    { params: { upload_id: uploadId } },
    { context: { silent: true } },
  )
}

export async function uploadKnowledgeFsDocuments(
  controlSpaceId: string,
  uploads: KnowledgeFsUploadItem[],
  progress: KnowledgeFsUploadProgress = new Map(),
  onProgress?: (file: File, phase: KnowledgeFsUploadPhase) => void,
) {
  for (const upload of uploads) {
    if (progress.get(upload.id)?.phase === 'completed') continue
    progress.set(upload.id, { phase: 'pending' })
    onProgress?.(upload.file, 'pending')
    await consoleClient.knowledgeFs.spaces.byControlSpaceId.documents.post(
      {
        body: { upload_id: upload.uploadId },
        params: { control_space_id: controlSpaceId },
      },
      { context: { silent: true } },
    )
    progress.set(upload.id, { phase: 'completed' })
    onProgress?.(upload.file, 'completed')
  }
}