package service import ( "fmt" "log" "time" "github.com/kazier/backend/extract" "github.com/kazier/backend/repository" ) // Extract derives generic facts from OCR text (V3 S2) — dates, amounts, // issuers, references, suggested doc type. Rules-based (package extract), // no ML, no network : it runs inside the OCR worker right after Tesseract // and stores origin='auto' rows (user corrections, origin='user', survive // every re-run — see UpsertAutoFacts). type Extract struct { Repository *repository.Repository } func NewExtract(repo *repository.Repository) *Extract { return &Extract{Repository: repo} } // FactDTO serializes one row of GET /files/:id/facts. type FactDTO struct { FactType string `json:"factType"` ValueText string `json:"valueText,omitempty"` ValueNum *float64 `json:"valueNum,omitempty"` ValueDate string `json:"valueDate,omitempty"` Confidence float64 `json:"confidence"` Origin string `json:"origin"` } // ProcessText extracts facts from text and replaces the resource's auto // facts. Empty text clears stale auto facts. Never touches user corrections. // Afterwards the deterministic links are reconciled (same_issuer / // same_reference may have appeared or vanished) — best-effort. func (e *Extract) ProcessText(ownerID, resourceID, text string) error { raw := extract.FromText(text) params := make([]repository.FactParam, 0, len(raw)) for _, f := range raw { var num *float64 if f.HasNum { n := f.ValueNum num = &n } var dat *string if f.ValueDate != "" { d := f.ValueDate dat = &d } text := f.ValueText // Amounts carry a display string ("1234.56 €") — keep the numeric // column as the machine value, the text as the human one. params = append(params, repository.FactParam{ Type: string(f.Type), ValueText: text, ValueNum: num, ValueDate: dat, Confidence: f.Confidence, }) } if err := e.Repository.Resources.UpsertAutoFacts(resourceID, params); err != nil { return err } if err := e.Repository.Resources.ReconcileLinks(ownerID, resourceID); err != nil { log.Printf("extract: reconcile %s: %v", resourceID, err) } return nil } // Backfill processes files with OCR text but no auto facts yet (one boot // pass, bounded batches). Returns the number of resources processed. func (e *Extract) Backfill(batch int) (int, error) { total := 0 for { pending, err := e.Repository.Resources.ListNeedingExtract(batch) if err != nil { return total, err } if len(pending) == 0 { return total, nil } for _, p := range pending { if err := e.ProcessText(p.UserID, p.ResourceID, p.Text); err != nil { log.Printf("extract: backfill %s: %v", p.ResourceID, err) continue } total++ } } } // GetFacts returns the facts of a visible resource (viewer+, own or shared — // same scope as GetFileOcr, facts derive from the OCR text). Trashed/absent/ // foreign → repository.ErrNotFound. func (s *Resources) GetFacts(userID, resourceID string) ([]FactDTO, error) { if _, err := s.Repo.GetFileVisible(userID, resourceID); err != nil { return nil, err } rows, err := s.Repository.Resources.ListFacts(resourceID) if err != nil { return nil, err } out := make([]FactDTO, 0, len(rows)) for _, row := range rows { out = append(out, FactDTO{ FactType: row.Type, ValueText: row.ValueText, ValueNum: row.ValueNum, ValueDate: derefString(row.ValueDate), Confidence: row.Confidence, Origin: row.Origin, }) } return out, nil } // validFactTypes mirrors the resource_facts CHECK (migration 000019). // Validated in Go so violations map to INVALID_REQUEST, never 500. var validFactTypes = map[string]bool{ "date": true, "due_date": true, "period": true, "amount": true, "issuer": true, "reference": true, "doc_type": true, } // ErrInvalidFact marks client fact-validation failures (400 INVALID_REQUEST, // never 500 — cf. FEEDBACK #8). Wrapped by validateFactParam. var ErrInvalidFact = fmt.Errorf("invalid fact") // CorrectFacts replaces the user's correction set on a resource (S3 : // full-replace, not a diff). Requires editor+ like update_metadata // (A8-style : viewer- ne peut pas corriger → NOT_FOUND halt, pas d'oracle). // Absent/trashed/foreign → repository.ErrNotFound. func (s *Resources) CorrectFacts(userID, resourceID string, facts []FactDTO) error { access, err := s.Repository.Shares.EffectiveAccess(userID, resourceID) if err != nil { return err } if access.EffectiveRank < repository.AccessEditor { return repository.ErrNotFound } params := make([]repository.FactParam, 0, len(facts)) for i, f := range facts { p, err := validateFactParam(i, f) if err != nil { return err } params = append(params, p) } if err := s.Repository.Resources.CorrectFacts(resourceID, params); err != nil { return err } // Les faits ont changé : les arêtes same_issuer/same_reference suivent. if err := s.Repository.Resources.ReconcileLinks(userID, resourceID); err != nil { log.Printf("extract: reconcile %s: %v", resourceID, err) } return nil } func validateFactParam(i int, f FactDTO) (repository.FactParam, error) { if !validFactTypes[f.FactType] { return repository.FactParam{}, fmt.Errorf("facts[%d].factType invalide: %w", i, ErrInvalidFact) } if f.Confidence < 0 || f.Confidence > 1 { return repository.FactParam{}, fmt.Errorf("facts[%d].confidence hors bornes [0,1]: %w", i, ErrInvalidFact) } var num *float64 if f.ValueNum != nil { n := *f.ValueNum num = &n } var dat *string if f.ValueDate != "" { if _, err := time.Parse("2006-01-02", f.ValueDate); err != nil { return repository.FactParam{}, fmt.Errorf("facts[%d].valueDate doit être ISO YYYY-MM-DD: %w", i, ErrInvalidFact) } d := f.ValueDate dat = &d } if f.ValueText == "" && num == nil && dat == nil { return repository.FactParam{}, fmt.Errorf("facts[%d] sans valeur (valueText|valueNum|valueDate requis): %w", i, ErrInvalidFact) } conf := f.Confidence if conf == 0 { // Correction manuelle sans confiance explicite = affirmation. conf = 1.0 } return repository.FactParam{ Type: f.FactType, ValueText: f.ValueText, ValueNum: num, ValueDate: dat, Confidence: conf, }, nil }