feat(knowledge): track per-stage parsing progress with /stages API

Adds a five-segment progress model for the document parsing pipeline so
the UI (PR③) can render a timeline showing where each document is
(DocReader → Chunking → Embedding → Multimodal → PostProcess) and
which stage failed with what error code.

- New table `knowledge_processing_stages` (migration 000052) with one
  row per (knowledge_id, stage). UPSERT on Begin/Done/Fail bumps an
  attempt counter so re-parses don't lose history.

- StageTracker service exposes Begin/Done/Fail/Skip; all calls are
  best-effort and never break the pipeline if persistence fails.

- Stable error codes (DOCREADER_TIMEOUT / EMBEDDING_RATE_LIMIT /
  VECTORSTORE_WRITE_FAILED / ...) the UI can map to localized
  remediation hints.

- Tracker call sites added at the four meaningful failure points:
  convert (DocReader), CreateChunks (Chunking), BatchIndex (Embedding),
  enqueueImageMultimodalTasks (Multimodal start),
  KnowledgePostProcess.Handle (Multimodal close + PostProcess).

- New endpoint `GET /api/v1/knowledge/:id/stages` returns the five
  canonical stages — missing rows are synthesized as "pending" so
  the timeline always renders five segments. Includes current_stage
  and last_error block.
This commit is contained in:
wizardchen
2026-05-27 13:31:06 +08:00
committed by lyingbug
parent 3ae3ea97c5
commit 04f56f9cda
12 changed files with 666 additions and 6 deletions
+18
View File
@@ -64,6 +64,12 @@ type knowledgeService struct {
memFAQRunningImport sync.Map // kbID -> *runningFAQImportInfo
wikiRepo interfaces.WikiPageRepository
wikiService interfaces.WikiPageService
// stageTracker records per-stage progress for the parsing pipeline.
// Best-effort: a nil tracker (e.g. test harness) is safely handled
// because every call site goes through the StageTracker interface
// which has a no-op fallback. See knowledge_stage_tracker.go.
stageTracker StageTracker
}
const (
@@ -96,6 +102,7 @@ func NewKnowledgeService(
wikiRepo interfaces.WikiPageRepository,
wikiService interfaces.WikiPageService,
taskPendingRepo interfaces.TaskPendingOpsRepository,
stageTracker StageTracker,
) (interfaces.KnowledgeService, error) {
return &knowledgeService{
config: config,
@@ -120,9 +127,20 @@ func NewKnowledgeService(
wikiRepo: wikiRepo,
wikiService: wikiService,
taskPendingRepo: taskPendingRepo,
stageTracker: stageTracker,
}, nil
}
// stage returns a usable StageTracker — falls back to a no-op when the
// service was constructed without one (test harness, lite mode w/o repo).
// All pipeline call sites go through this so they never need a nil check.
func (s *knowledgeService) stage() StageTracker {
if s.stageTracker == nil {
return noopStageTracker{}
}
return s.stageTracker
}
// getParserEngineOverridesFromContext returns parser engine overrides from tenant in context (e.g. MinerU endpoint, API key).
// Used when building document ReadRequest so UI-configured values take precedence over env.
func (s *knowledgeService) getParserEngineOverridesFromContext(ctx context.Context) map[string]string {