返回 DeepSeek-Reasonix
format.go
根目录 / internal / agent / format.go
1 package agent
2
3 import (
4 "context"
5 "strings"
6
7 "reasonix/internal/provider"
8 )
9
10 // responseFormatContextKey carries a per-turn structured-output request through
11 // Run's context. The agent reads it when building the provider.Request so a
12 // turn can ask for json_object output without changing persistent state.
13 type responseFormatContextKey struct{}
14
15 // WithResponseFormat attaches a structured-output format to a turn context.
16 // Empty format is a no-op returning ctx unchanged (byte-stable default path).
17 func WithResponseFormat(ctx context.Context, format string) context.Context {
18 if strings.TrimSpace(format) == "" {
19 return ctx
20 }
21 return context.WithValue(ctx, responseFormatContextKey{}, strings.TrimSpace(format))
22 }
23
24 // responseFormatFromContext returns the turn-scoped structured-output format
25 // (e.g. "json_object") or "" when none was requested.
26 func responseFormatFromContext(ctx context.Context) string {
27 if ctx == nil {
28 return ""
29 }
30 f, _ := ctx.Value(responseFormatContextKey{}).(string)
31 return f
32 }
33
34 // ResponseFormatFromRequest is the exported form of responseFormatFromRequest:
35 // control tests assert the turn-bound format actually reaches the agent
36 // request path (review #7234 — format bound to turn, not global slot).
37 func ResponseFormatFromRequest(ctx context.Context) *provider.ResponseFormat {
38 return responseFormatFromRequest(ctx)
39 }
40
41 // responseFormatFromRequest returns the turn-scoped structured-output format
42 // (e.g. "json_object") as a Request field, or nil when none was requested
43 // (nil keeps the wire byte-stable for prompt caching).
44 func responseFormatFromRequest(ctx context.Context) *provider.ResponseFormat {
45 if f := responseFormatFromContext(ctx); f != "" {
46 return &provider.ResponseFormat{Type: f}
47 }
48 return nil
49 }
50
50 lines GO