Preserve selected collections in materialized references

This commit is contained in:
2026-11-29 17:59:01 +00:00
parent 10221b0624
commit d406aa6cd2
33 changed files with 251 additions and 2 deletions

View File

@@ -109,6 +109,7 @@ type ReferenceProducer struct {
type ReferenceItem struct {
SlotName string `json:"slot_name"`
Collections []string `json:"collections,omitempty"`
MediaType string `json:"media_type,omitempty"`
Content []byte `json:"-"`
Digest string `json:"digest,omitempty"`
@@ -122,6 +123,9 @@ type ReferenceItem struct {
func CloneReferenceItem(item ReferenceItem) ReferenceItem {
item.Content = append([]byte(nil), item.Content...)
if item.Collections != nil {
item.Collections = append([]string{}, item.Collections...)
}
item.ArtifactSchema = CloneArtifactSchema(item.ArtifactSchema)
return item
}
@@ -129,6 +133,7 @@ func CloneReferenceItem(item ReferenceItem) ReferenceItem {
func (item ReferenceItem) MarshalJSON() ([]byte, error) {
type referenceItemJSON struct {
SlotName string `json:"slot_name"`
Collections *[]string `json:"collections,omitempty"`
MediaType string `json:"media_type,omitempty"`
Digest string `json:"digest,omitempty"`
Origin ReferenceOrigin `json:"origin"`
@@ -147,6 +152,9 @@ func (item ReferenceItem) MarshalJSON() ([]byte, error) {
BindingSource: item.BindingSource,
ArtifactKind: item.ArtifactKind,
}
if item.Collections != nil {
encoded.Collections = &item.Collections
}
if item.ArtifactSchema.ID != "" || item.ArtifactSchema.Name != "" || item.ArtifactSchema.Version != "" || len(item.ArtifactSchema.JSONSchema) > 0 {
schema := CloneArtifactSchema(item.ArtifactSchema)
encoded.ArtifactSchema = &schema

View File

@@ -123,6 +123,34 @@ func TestReferenceItemJSONOmitsContent(t *testing.T) {
}
}
func TestReferenceItemCollectionSelectionCopyAndJSON(t *testing.T) {
for _, selection := range [][]string{nil, {}, {"campaign"}} {
item := ReferenceItem{SlotName: "glossary", Collections: selection}
clone := CloneReferenceItem(item)
if (clone.Collections == nil) != (selection == nil) || len(clone.Collections) != len(selection) {
t.Fatalf("cloned selection = %#v, want %#v", clone.Collections, selection)
}
encoded, err := json.Marshal(item)
if err != nil {
t.Fatal(err)
}
var fields map[string]any
if err := json.Unmarshal(encoded, &fields); err != nil {
t.Fatal(err)
}
_, present := fields["collections"]
if present != (selection != nil) {
t.Fatalf("encoded selection = %s", encoded)
}
if len(selection) > 0 {
clone.Collections[0] = "changed"
if selection[0] != "campaign" {
t.Fatal("copy mutated original selection")
}
}
}
}
func TestLLMInputMaterialCopiesContentAndOmitsContentFromJSON(t *testing.T) {
content := []byte("raw source bytes")
material := NewLLMInputMaterial("transcript", "application/json", content, "sha256:source", "file:///tmp/source.json")