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
+64
View File
@@ -0,0 +1,64 @@
package types
import "time"
// ProcessingStage names — kept as a closed set on the Go side so the
// frontend timeline can rely on exactly five segments. Adding a new stage
// requires a coordinated frontend release; rename in this file is breaking.
const (
ProcessingStageDocReader = "docreader"
ProcessingStageChunking = "chunking"
ProcessingStageEmbedding = "embedding"
ProcessingStageMultimodal = "multimodal"
ProcessingStagePostProcess = "postprocess"
)
// AllProcessingStages is the canonical, ordered list. Used by the API
// to fill in pending entries when no row exists yet for a stage so the
// timeline always renders five segments even before parsing starts.
var AllProcessingStages = []string{
ProcessingStageDocReader,
ProcessingStageChunking,
ProcessingStageEmbedding,
ProcessingStageMultimodal,
ProcessingStagePostProcess,
}
// ProcessingStageStatus values. Distinct from Knowledge.parse_status —
// each stage row tracks its own state independently of the parent.
const (
ProcessingStagePending = "pending" // not started yet
ProcessingStageRunning = "running" // currently executing
ProcessingStageDone = "done"
ProcessingStageFailed = "failed"
ProcessingStageSkipped = "skipped" // e.g. multimodal skipped for image-free docs
)
// KnowledgeProcessingStage is one row in knowledge_processing_stages.
//
// We intentionally keep it as a flat struct with both Go and JSON tags so
// the same type round-trips through GORM (DB layer) and the API response.
// Sensitive ErrorDetail (full stack) is excluded from the default JSON
// output via "-" — handlers must opt in explicitly for admin views.
type KnowledgeProcessingStage struct {
ID int64 `gorm:"primaryKey;column:id" json:"-"`
KnowledgeID string `gorm:"column:knowledge_id;index" json:"knowledge_id"`
Stage string `gorm:"column:stage;size:32" json:"stage"`
Status string `gorm:"column:status;size:16" json:"status"`
StartedAt *time.Time `gorm:"column:started_at" json:"started_at,omitempty"`
FinishedAt *time.Time `gorm:"column:finished_at" json:"finished_at,omitempty"`
DurationMs int64 `gorm:"column:duration_ms" json:"duration_ms,omitempty"`
ErrorCode string `gorm:"column:error_code;size:64" json:"error_code,omitempty"`
ErrorMessage string `gorm:"column:error_message;type:text" json:"error_message,omitempty"`
ErrorDetail string `gorm:"column:error_detail;type:text" json:"-"`
Attempt int `gorm:"column:attempt" json:"attempt"`
CreatedAt time.Time `gorm:"column:created_at;autoCreateTime" json:"created_at"`
UpdatedAt time.Time `gorm:"column:updated_at;autoUpdateTime" json:"updated_at"`
}
// TableName pins the GORM table since the natural pluralization
// ("knowledge_processing_stages") happens to match what the migration
// creates — but explicit beats implicit here.
func (KnowledgeProcessingStage) TableName() string {
return "knowledge_processing_stages"
}