123 lines
4.6 KiB
Go
123 lines
4.6 KiB
Go
|
|
package artifacts
|
||
|
|
|
||
|
|
import (
|
||
|
|
"encoding/json"
|
||
|
|
"strings"
|
||
|
|
)
|
||
|
|
|
||
|
|
// ExtractionReferenceIdentity identifies one prepared reference in metadata and fingerprints.
|
||
|
|
type ExtractionReferenceIdentity struct {
|
||
|
|
Selector string `json:"selector"`
|
||
|
|
SourceID string `json:"source_id"`
|
||
|
|
Path string `json:"path"`
|
||
|
|
Checksum string `json:"checksum"`
|
||
|
|
SizeBytes int64 `json:"size_bytes"`
|
||
|
|
}
|
||
|
|
|
||
|
|
// ExtractionCompletion is the native completion evidence and its diagnostic fields.
|
||
|
|
type ExtractionCompletion struct {
|
||
|
|
Pipeline string
|
||
|
|
ArtifactCount int
|
||
|
|
OutputDirectory string
|
||
|
|
DebugDirectory string
|
||
|
|
FallbackCount int
|
||
|
|
}
|
||
|
|
|
||
|
|
// ExtractionMetadata is the persisted control evidence for generated extraction.
|
||
|
|
type ExtractionMetadata struct {
|
||
|
|
RunID string
|
||
|
|
ConfigurationFingerprint string
|
||
|
|
Input ExtractionInputIdentity
|
||
|
|
InputPresent bool
|
||
|
|
References []ExtractionReferenceIdentity
|
||
|
|
ReferenceCount int
|
||
|
|
Completion ExtractionCompletion
|
||
|
|
CompletionPresent bool
|
||
|
|
}
|
||
|
|
|
||
|
|
func extractionString(fields map[string]any, key string) string {
|
||
|
|
value, _ := fields[key].(string)
|
||
|
|
return strings.TrimSpace(value)
|
||
|
|
}
|
||
|
|
|
||
|
|
func extractionNumber(value any) int64 {
|
||
|
|
switch v := value.(type) {
|
||
|
|
case int:
|
||
|
|
return int64(v)
|
||
|
|
case int64:
|
||
|
|
return v
|
||
|
|
case float64:
|
||
|
|
if v >= 0 && v == float64(int64(v)) {
|
||
|
|
return int64(v)
|
||
|
|
}
|
||
|
|
case json.Number:
|
||
|
|
n, err := v.Int64()
|
||
|
|
if err == nil {
|
||
|
|
return n
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
func extractionFallbackCount(value any) int {
|
||
|
|
switch v := value.(type) {
|
||
|
|
case int:
|
||
|
|
return v
|
||
|
|
case float64:
|
||
|
|
if v > 0 && v == float64(int(v)) {
|
||
|
|
return int(v)
|
||
|
|
}
|
||
|
|
case json.Number:
|
||
|
|
parsed, err := v.Int64()
|
||
|
|
if err == nil && parsed > 0 && int64(int(parsed)) == parsed {
|
||
|
|
return int(parsed)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return 0
|
||
|
|
}
|
||
|
|
|
||
|
|
// DecodeExtractionMetadata reads known fields from direct or JSON-decoded maps.
|
||
|
|
func DecodeExtractionMetadata(metadata map[string]any) ExtractionMetadata {
|
||
|
|
out := ExtractionMetadata{RunID: extractionString(metadata, "narratio_run_id"), ConfigurationFingerprint: extractionString(metadata, "configuration_fingerprint"), ReferenceCount: int(extractionNumber(metadata["reference_count"]))}
|
||
|
|
if input, ok := metadata[extractionMetadataInput].(map[string]any); ok {
|
||
|
|
out.InputPresent = true
|
||
|
|
out.Input = ExtractionInputIdentity{Checksum: extractionString(input, "checksum"), SourceID: extractionString(input, "source_id"), ProducerStage: extractionString(input, "producer_stage"), OutputKind: extractionString(input, "output_kind"), ProducerRunID: extractionString(input, "producer_run_id"), Provenance: extractionString(input, "provenance")}
|
||
|
|
}
|
||
|
|
if completion, ok := metadata["completion"].(map[string]any); ok {
|
||
|
|
out.CompletionPresent = true
|
||
|
|
out.Completion = ExtractionCompletion{Pipeline: extractionString(completion, "pipeline"), ArtifactCount: int(extractionNumber(completion["artifact_count"])), OutputDirectory: extractionString(completion, "output_directory"), DebugDirectory: extractionString(completion, "debug_directory"), FallbackCount: extractionFallbackCount(completion["fallback_count"])}
|
||
|
|
}
|
||
|
|
appendReference := func(row map[string]any) {
|
||
|
|
out.References = append(out.References, ExtractionReferenceIdentity{Selector: extractionString(row, "selector"), SourceID: extractionString(row, "source_id"), Path: extractionString(row, "path"), Checksum: extractionString(row, "checksum"), SizeBytes: extractionNumber(row["size_bytes"])})
|
||
|
|
}
|
||
|
|
switch rows := metadata["references"].(type) {
|
||
|
|
case []map[string]any:
|
||
|
|
for _, row := range rows {
|
||
|
|
appendReference(row)
|
||
|
|
}
|
||
|
|
case []any:
|
||
|
|
for _, raw := range rows {
|
||
|
|
if row, ok := raw.(map[string]any); ok {
|
||
|
|
appendReference(row)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return out
|
||
|
|
}
|
||
|
|
|
||
|
|
// EncodeExtractionMetadata preserves the existing generated evidence map shape.
|
||
|
|
func EncodeExtractionMetadata(e ExtractionMetadata) map[string]any {
|
||
|
|
refs := make([]map[string]any, 0, len(e.References))
|
||
|
|
for _, r := range e.References {
|
||
|
|
refs = append(refs, map[string]any{"selector": r.Selector, "source_id": r.SourceID, "path": r.Path, "checksum": r.Checksum, "size_bytes": r.SizeBytes})
|
||
|
|
}
|
||
|
|
completion := map[string]any{"pipeline": e.Completion.Pipeline, "artifact_count": e.Completion.ArtifactCount, "output_directory": e.Completion.OutputDirectory}
|
||
|
|
if e.Completion.DebugDirectory != "" {
|
||
|
|
completion["debug_directory"] = e.Completion.DebugDirectory
|
||
|
|
}
|
||
|
|
if e.Completion.FallbackCount > 0 {
|
||
|
|
completion["fallback_count"] = e.Completion.FallbackCount
|
||
|
|
}
|
||
|
|
return map[string]any{"narratio_run_id": e.RunID, "configuration_fingerprint": e.ConfigurationFingerprint, "direct_input": e.Input.Metadata(), "reference_count": e.ReferenceCount, "references": refs, "completion": completion}
|
||
|
|
}
|