-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes.go
More file actions
183 lines (157 loc) · 5.1 KB
/
Copy pathtypes.go
File metadata and controls
183 lines (157 loc) · 5.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package neko
import (
"encoding/json"
"time"
)
// MessageRole represents the role of a message in conversation.
type MessageRole string
const (
RoleSystem MessageRole = "system"
RoleUser MessageRole = "user"
RoleAssistant MessageRole = "assistant"
RoleTool MessageRole = "tool"
)
// Message represents a chat message.
type Message struct {
Role MessageRole `json:"role"`
Content string `json:"content"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
TokenUsage *TokenUsage `json:"token_usage,omitempty"`
Images [][]byte `json:"images,omitempty"`
}
// ToolCall represents a tool invocation.
type ToolCall struct {
ID string `json:"id"`
Name string `json:"name"`
Arguments map[string]any `json:"arguments"`
}
// TokenUsage tracks token consumption.
type TokenUsage struct {
InputTokens int `json:"input_tokens"`
OutputTokens int `json:"output_tokens"`
}
// Total returns total tokens used.
func (t TokenUsage) Total() int {
return t.InputTokens + t.OutputTokens
}
// Timing captures execution timing.
type Timing struct {
StartTime time.Time `json:"start_time"`
EndTime time.Time `json:"end_time"`
Duration time.Duration `json:"duration"`
}
// NewTiming creates timing from start time.
func NewTiming(start time.Time) Timing {
end := time.Now()
return Timing{
StartTime: start,
EndTime: end,
Duration: end.Sub(start),
}
}
// RunResult holds the result of an agent run.
type RunResult struct {
Output any `json:"output"`
State string `json:"state"` // "success" or "max_steps_error"
Steps []Step `json:"steps"`
TokenUsage *TokenUsage `json:"token_usage,omitempty"`
Timing Timing `json:"timing"`
}
// Step is the interface for all step types.
type Step interface {
StepType() string
ToMessages() []Message
}
// ActionStep represents one action taken by the agent.
type ActionStep struct {
StepNumber int `json:"step_number"`
Timing Timing `json:"timing"`
ModelOutput string `json:"model_output,omitempty"`
CodeAction string `json:"code_action,omitempty"`
ToolCalls []ToolCall `json:"tool_calls,omitempty"`
Observations string `json:"observations,omitempty"`
Error error `json:"error,omitempty"`
TokenUsage *TokenUsage `json:"token_usage,omitempty"`
IsFinal bool `json:"is_final_answer"`
}
func (s *ActionStep) StepType() string { return "action" }
func (s *ActionStep) ToMessages() []Message {
var msgs []Message
// Assistant output (model's response text)
if s.ModelOutput != "" {
msgs = append(msgs, Message{Role: RoleAssistant, Content: s.ModelOutput})
}
// Tool calls as text (converted to assistant message)
if len(s.ToolCalls) > 0 {
msgs = append(msgs, Message{Role: RoleAssistant, Content: formatToolCalls(s.ToolCalls)})
}
// Observations as user message
if s.Observations != "" {
msgs = append(msgs, Message{Role: RoleUser, Content: "Observation:\n" + s.Observations})
}
// Errors as user message
if s.Error != nil {
errorMsg := "Error:\n" + s.Error.Error() + "\nPlease try again or use another approach."
msgs = append(msgs, Message{Role: RoleUser, Content: errorMsg})
}
return msgs
}
// formatToolCalls converts tool calls to text representation for message history.
func formatToolCalls(toolCalls []ToolCall) string {
calls := make([]map[string]any, 0, len(toolCalls))
for _, tc := range toolCalls {
calls = append(calls, map[string]any{
"id": tc.ID,
"type": "function",
"function": map[string]any{
"name": tc.Name,
"arguments": tc.Arguments,
},
})
}
data, _ := json.Marshal(calls)
return "Calling tools:\n" + string(data)
}
// TaskStep represents the initial task.
type TaskStep struct {
Task string `json:"task"`
Images [][]byte `json:"images,omitempty"`
}
func (s *TaskStep) StepType() string { return "task" }
func (s *TaskStep) ToMessages() []Message {
return []Message{{Role: RoleUser, Content: "Task:\n" + s.Task}}
}
// PlanningStep represents a planning phase.
type PlanningStep struct {
Plan string `json:"plan"`
Timing Timing `json:"timing"`
TokenUsage *TokenUsage `json:"token_usage,omitempty"`
}
func (s *PlanningStep) StepType() string { return "planning" }
func (s *PlanningStep) ToMessages() []Message {
return []Message{
{Role: RoleAssistant, Content: s.Plan},
{Role: RoleUser, Content: "Now proceed and carry out this plan."},
}
}
// FinalAnswerStep marks the final answer.
type FinalAnswerStep struct {
Output any `json:"output"`
}
func (s *FinalAnswerStep) StepType() string { return "final_answer" }
func (s *FinalAnswerStep) ToMessages() []Message {
return nil
}
// ToolInput describes a tool parameter.
type ToolInput struct {
Type string `json:"type"`
Description string `json:"description"`
Required bool `json:"required,omitempty"`
}
// ToolSchema describes a tool's interface.
type ToolSchema struct {
Name string `json:"name"`
Description string `json:"description"`
Inputs map[string]ToolInput `json:"inputs"`
OutputType string `json:"output_type"`
}