Finish the references implementation for the extraction module and update roadmap documentation

This commit is contained in:
2026-07-05 10:53:16 -05:00
parent be6803ffa1
commit a516944086
13 changed files with 280 additions and 562 deletions

View File

@@ -4,6 +4,7 @@ import (
"crypto/sha256"
"encoding/hex"
"fmt"
"mime"
"net/url"
"os"
"path/filepath"
@@ -17,7 +18,8 @@ import (
const (
referenceOriginFile = "file"
referenceMediaType = "text/plain; charset=utf-8"
referenceMediaType = "text/plain"
unknownMediaType = "application/octet-stream"
)
type ReferenceMaterializationOptions struct {
@@ -88,6 +90,10 @@ func materializeLaneReferences(
if !utf8.Valid(content) {
return contracts.ReferenceSet{}, nil, fmt.Errorf("pipeline %q lane %q reference slot %q path %q must be UTF-8 text", pipelineID, lane.ID, slotName, path)
}
mediaType := referenceMediaTypeForPath(path)
if !referenceMediaTypeAccepted(mediaType, slot.AcceptedMediaTypes) {
return contracts.ReferenceSet{}, nil, fmt.Errorf("pipeline %q lane %q reference slot %q path %q media type %q is not accepted", pipelineID, lane.ID, slotName, path, mediaType)
}
if slot.MaxBytes > 0 && int64(len(content)) > slot.MaxBytes {
return contracts.ReferenceSet{}, nil, fmt.Errorf("pipeline %q lane %q reference slot %q path %q is %d bytes, limit %d", pipelineID, lane.ID, slotName, path, len(content), slot.MaxBytes)
}
@@ -101,7 +107,7 @@ func materializeLaneReferences(
item := contracts.ReferenceItem{
SlotName: slotName,
MediaType: referenceMediaType,
MediaType: mediaType,
Content: append([]byte(nil), content...),
Digest: referenceDigest(content),
Origin: contracts.ReferenceOrigin{Type: referenceOriginFile, URI: fileURI(path)},
@@ -116,6 +122,43 @@ func materializeLaneReferences(
return set, warnings, nil
}
func referenceMediaTypeForPath(path string) string {
extension := strings.ToLower(filepath.Ext(path))
mediaType := mime.TypeByExtension(extension)
if strings.TrimSpace(mediaType) == "" {
if extension == ".md" || extension == ".markdown" {
return "text/markdown"
}
return unknownMediaType
}
return canonicalMediaType(mediaType)
}
func referenceMediaTypeAccepted(mediaType string, accepted []string) bool {
if len(accepted) == 0 {
return true
}
mediaType = canonicalMediaType(mediaType)
for _, value := range accepted {
if strings.EqualFold(mediaType, canonicalMediaType(value)) {
return true
}
}
return false
}
func canonicalMediaType(mediaType string) string {
trimmed := strings.TrimSpace(mediaType)
if trimmed == "" {
return ""
}
parsed, _, err := mime.ParseMediaType(trimmed)
if err != nil {
return strings.ToLower(trimmed)
}
return strings.ToLower(parsed)
}
func referencePath(binding ReferenceBinding, options ReferenceMaterializationOptions) (string, error) {
source := strings.TrimSpace(binding.Source)
if source == "" {