Skip to content

Commit 9696734

Browse files
authored
Merge pull request #985 from krissetto/thinking-budgets-in-v2-types
Make sure thinking budgets in v2 configs get serialized correctly
2 parents 30b08e0 + 790d8cd commit 9696734

1 file changed

Lines changed: 32 additions & 0 deletions

File tree

pkg/config/v2/types.go

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package v2
22

33
import (
4+
"encoding/json"
45
"fmt"
56

67
"github.com/docker/cagent/pkg/config/types"
@@ -194,6 +195,37 @@ func (t *ThinkingBudget) UnmarshalYAML(unmarshal func(any) error) error {
194195
return nil
195196
}
196197

198+
// MarshalJSON implements custom marshaling to output simple string or int format
199+
// This ensures JSON serialization during config upgrades preserves the value correctly
200+
func (t ThinkingBudget) MarshalJSON() ([]byte, error) {
201+
// If Effort string is set (non-empty), marshal as string
202+
if t.Effort != "" {
203+
return []byte(fmt.Sprintf("%q", t.Effort)), nil
204+
}
205+
206+
// Otherwise marshal as integer (includes 0, -1, and positive values)
207+
return []byte(fmt.Sprintf("%d", t.Tokens)), nil
208+
}
209+
210+
// UnmarshalJSON implements custom unmarshaling to accept simple string or int format
211+
func (t *ThinkingBudget) UnmarshalJSON(data []byte) error {
212+
// Try integer tokens first
213+
var n int
214+
if err := json.Unmarshal(data, &n); err == nil {
215+
*t = ThinkingBudget{Tokens: n}
216+
return nil
217+
}
218+
219+
// Try string level
220+
var s string
221+
if err := json.Unmarshal(data, &s); err == nil {
222+
*t = ThinkingBudget{Effort: s}
223+
return nil
224+
}
225+
226+
return nil
227+
}
228+
197229
// StructuredOutput defines a JSON schema for structured output
198230
type StructuredOutput struct {
199231
// Name is the name of the response format

0 commit comments

Comments
 (0)