Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions agent/response.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,11 @@ func (resp *Response) Update(update *ResponseUpdate) {
msg.CreatedAt = update.CreatedAt
}
msg.Contents = append(msg.Contents, update.Contents...)
if update.AdditionalProperties != nil {
// AdditionalProperties are scoped to the message when the update carries a
// MessageID, otherwise to the response (below), matching .NET's
// ChatResponseExtensions.ProcessUpdate. Copying to both would duplicate and
// mis-attribute provider metadata.
if update.AdditionalProperties != nil && update.MessageID != "" {
if msg.AdditionalProperties == nil {
msg.AdditionalProperties = make(map[string]any)
}
Expand All @@ -213,7 +217,7 @@ func (resp *Response) Update(update *ResponseUpdate) {
if !isValidCreatedAt(resp.CreatedAt) && isValidCreatedAt(update.CreatedAt) {
resp.CreatedAt = update.CreatedAt
}
if update.AdditionalProperties != nil {
if update.AdditionalProperties != nil && update.MessageID == "" {
if resp.AdditionalProperties == nil {
resp.AdditionalProperties = make(map[string]any)
}
Expand Down
32 changes: 32 additions & 0 deletions agent/response_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,38 @@ func TestResponse_CreatedAt(t *testing.T) {
}
}

// AdditionalProperties are scoped to the message when the update carries a
// MessageID and to the response otherwise, matching .NET's ProcessUpdate. They
// must not land on both.
func TestResponse_Update_AdditionalPropertiesScopedByMessageID(t *testing.T) {
// With a MessageID -> message only.
withID := &agent.Response{}
withID.Update(&agent.ResponseUpdate{
MessageID: "msg1",
AdditionalProperties: map[string]any{"k": "v"},
Contents: message.Contents{&message.TextContent{Text: "hi"}},
})
if withID.Messages[0].AdditionalProperties["k"] != "v" {
t.Errorf("message AdditionalProperties = %v, want k=v", withID.Messages[0].AdditionalProperties)
}
if len(withID.AdditionalProperties) != 0 {
t.Errorf("response AdditionalProperties = %v, want empty (scoped to message)", withID.AdditionalProperties)
}

// Without a MessageID -> response only.
noID := &agent.Response{}
noID.Update(&agent.ResponseUpdate{
AdditionalProperties: map[string]any{"k": "v"},
Contents: message.Contents{&message.TextContent{Text: "hi"}},
})
if noID.AdditionalProperties["k"] != "v" {
t.Errorf("response AdditionalProperties = %v, want k=v", noID.AdditionalProperties)
}
if len(noID.Messages[0].AdditionalProperties) != 0 {
t.Errorf("message AdditionalProperties = %v, want empty (scoped to response)", noID.Messages[0].AdditionalProperties)
}
}

func TestResponse_Update_AdditionalProperties(t *testing.T) {
resp := &agent.Response{}

Expand Down
Loading