返回 DeepSeek-Reasonix
goal_common.go
根目录 / internal / tool / builtin / goal_common.go
1 package builtin
2
3 import (
4 "bytes"
5 "context"
6 "encoding/json"
7 "fmt"
8 "strings"
9
10 goaldomain "reasonix/internal/goal"
11 "reasonix/internal/tool"
12 )
13
14 type goalToolValue struct {
15 Goal *goaldomain.Snapshot `json:"goal"`
16 Activation goaldomain.Activation `json:"activation,omitempty"`
17 StopReason string `json:"stopReason,omitempty"`
18 Instruction string `json:"instruction,omitempty"`
19 }
20
21 type optionalRoundLimit struct {
22 Present bool
23 Raw json.RawMessage
24 }
25
26 func (o *optionalRoundLimit) UnmarshalJSON(data []byte) error {
27 o.Present = true
28 o.Raw = append(o.Raw[:0], data...)
29 return nil
30 }
31
32 func goalToolResult(view *goaldomain.View) (string, error) {
33 return goalToolResultWithInstruction(view, "")
34 }
35
36 func goalToolResultWithInstruction(view *goaldomain.View, instruction string) (string, error) {
37 value := goalToolValue{}
38 if view != nil {
39 snapshot := view.Snapshot
40 // Work duration is host/UI metadata, not model-visible goal state.
41 snapshot.WorkDurationMs = 0
42 value.Goal = &snapshot
43 value.Activation = view.Activation
44 value.StopReason = view.StopReason
45 }
46 value.Instruction = instruction
47 encoded, err := json.Marshal(value)
48 if err != nil {
49 return "", fmt.Errorf("encode goal result: %w", err)
50 }
51 return string(encoded), nil
52 }
53
54 func goalBinding(ctx context.Context) (tool.GoalLifecycleBinding, error) {
55 binding, ok := tool.GoalLifecycleFromContext(ctx)
56 if !ok {
57 return tool.GoalLifecycleBinding{}, fmt.Errorf("goal tool requires a current host-attested goal context")
58 }
59 return binding, nil
60 }
61
62 func goalToolError(operation string, err error) error {
63 if err == nil {
64 return nil
65 }
66 if code := goaldomain.ErrorCodeOf(err); code != "" {
67 return fmt.Errorf("%s [%s]: %w", operation, code, err)
68 }
69 return fmt.Errorf("%s: %w", operation, err)
70 }
71
72 func decodeGoalArgs(args json.RawMessage, target any, toolName string) error {
73 if len(bytes.TrimSpace(args)) == 0 {
74 args = json.RawMessage(`{}`)
75 }
76 decoder := json.NewDecoder(bytes.NewReader(args))
77 decoder.DisallowUnknownFields()
78 if err := decoder.Decode(target); err != nil {
79 return fmt.Errorf("invalid %s arguments: %w", toolName, err)
80 }
81 if err := ensureJSONEnd(decoder); err != nil {
82 return fmt.Errorf("invalid %s arguments: %w", toolName, err)
83 }
84 return nil
85 }
86
87 func parseRoundLimit(raw json.RawMessage) (*uint64, error) {
88 if bytes.Equal(bytes.TrimSpace(raw), []byte("null")) {
89 return nil, nil
90 }
91 var value uint64
92 if err := json.Unmarshal(raw, &value); err != nil || value == 0 {
93 return nil, fmt.Errorf("max_goal_rounds must be null or a positive integer")
94 }
95 return &value, nil
96 }
97
98 func trimmedRequired(value, field string) (string, error) {
99 value = strings.TrimSpace(value)
100 if value == "" {
101 return "", fmt.Errorf("%s is required and must be non-empty", field)
102 }
103 return value, nil
104 }
105
105 lines GO