@@ -22,6 +22,7 @@ import (
2222 "os"
2323
2424 schemaapi "github.com/chainloop-dev/chainloop/app/controlplane/api/workflowcontract/v1"
25+ "github.com/chainloop-dev/chainloop/internal/aiagentconfig"
2526 "github.com/chainloop-dev/chainloop/internal/schemavalidators"
2627 api "github.com/chainloop-dev/chainloop/pkg/attestation/crafter/api/attestation/v1"
2728 "github.com/chainloop-dev/chainloop/pkg/casclient"
@@ -55,12 +56,28 @@ func (c *ChainloopAIAgentConfigCrafter) Craft(ctx context.Context, artifactPath
5556 return nil , fmt .Errorf ("can't open the file: %w" , err )
5657 }
5758
58- var rawData any
59- if err := json .Unmarshal (f , & rawData ); err != nil {
59+ // Unmarshal envelope, keeping data as raw JSON for schema validation
60+ var envelope struct {
61+ Data json.RawMessage `json:"data"`
62+ }
63+ if err := json .Unmarshal (f , & envelope ); err != nil {
6064 c .logger .Debug ().Err (err ).Msg ("error decoding file" )
6165 return nil , fmt .Errorf ("invalid JSON format: %w" , err )
6266 }
6367
68+ // Unmarshal data into typed struct for agent name extraction
69+ var data aiagentconfig.Data
70+ if err := json .Unmarshal (envelope .Data , & data ); err != nil {
71+ c .logger .Debug ().Err (err ).Msg ("error decoding data field" )
72+ return nil , fmt .Errorf ("failed to unmarshal data: %w" , err )
73+ }
74+
75+ // Validate using raw JSON to preserve unknown fields for strict schema validation
76+ var rawData any
77+ if err := json .Unmarshal (envelope .Data , & rawData ); err != nil {
78+ return nil , fmt .Errorf ("failed to unmarshal data for validation: %w" , err )
79+ }
80+
6481 if err := schemavalidators .ValidateAIAgentConfig (rawData , schemavalidators .AIAgentConfigVersion0_1 ); err != nil {
6582 c .logger .Debug ().Err (err ).Msg ("schema validation failed" )
6683 return nil , fmt .Errorf ("AI agent config validation failed: %w" , err )
@@ -71,14 +88,9 @@ func (c *ChainloopAIAgentConfigCrafter) Craft(ctx context.Context, artifactPath
7188 return nil , err
7289 }
7390
74- // Extract agent name from the validated JSON and surface it as an annotation
75- var envelope struct {
76- Agent struct {
77- Name string `json:"name"`
78- } `json:"agent"`
79- }
80- if err := json .Unmarshal (f , & envelope ); err == nil && envelope .Agent .Name != "" {
81- material .Annotations [annotationAIAgentName ] = envelope .Agent .Name
91+ // Surface agent name as an annotation
92+ if data .Agent .Name != "" {
93+ material .Annotations [annotationAIAgentName ] = data .Agent .Name
8294 }
8395
8496 return material , nil
0 commit comments