-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patherrors.go
More file actions
162 lines (132 loc) · 4.31 KB
/
Copy patherrors.go
File metadata and controls
162 lines (132 loc) · 4.31 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
package agent
import (
"errors"
"fmt"
"time"
"github.com/google/uuid"
"github.com/tailored-agentic-units/protocol/config"
)
// ErrorType categorizes agent errors by their source.
type ErrorType string
const (
// ErrorTypeInit indicates initialization or configuration errors.
ErrorTypeInit ErrorType = "init"
// ErrorTypeLLM indicates errors from LLM interactions.
ErrorTypeLLM ErrorType = "llm"
)
// Sentinel errors for the agent registry.
var (
ErrAgentNotFound = errors.New("agent not found")
ErrAgentExists = errors.New("agent already registered")
ErrEmptyAgentName = errors.New("agent name is empty")
)
// AgentError provides detailed error information for agent operations.
// Includes error categorization, unique identification, and contextual metadata.
type AgentError struct {
// Type categorizes the error (init or llm).
Type ErrorType `json:"type"`
// ID is a unique identifier for this error instance.
ID uuid.UUID `json:"uuid,omitempty"`
// Name identifies the agent that generated the error.
Name string `json:"name,omitempty"`
// Code is an application-specific error code.
Code string `json:"code,omitempty"`
// Message describes what went wrong.
Message string `json:"message"`
// Cause is the underlying error that caused this error.
Cause error `json:"-"`
// Client identifies the provider/model combination.
Client string `json:"client,omitempty"`
// Timestamp records when the error occurred.
Timestamp time.Time `json:"timestamp"`
}
// NewAgentError creates a new AgentError with the specified type and message.
// Optional ErrorOption functions can be provided to set additional fields.
func NewAgentError(errorType ErrorType, message string, options ...ErrorOption) *AgentError {
err := &AgentError{
Type: errorType,
Message: message,
Timestamp: time.Now(),
}
for _, option := range options {
option(err)
}
return err
}
// Error returns a formatted error message.
// Format varies based on available context (client, name).
func (e *AgentError) Error() string {
if e.Client != "" && e.Name != "" {
return fmt.Sprintf("Agent error [%s/%s]: %s", e.Client, e.Name, e.Message)
}
if e.Name != "" {
return fmt.Sprintf("Agent error [%s]: %s", e.Name, e.Message)
}
return fmt.Sprintf("Agent error: %s", e.Message)
}
// Unwrap returns the underlying cause error.
// Implements the error unwrapping interface for errors.Is and errors.As.
func (e *AgentError) Unwrap() error {
return e.Cause
}
// ErrorOption is a function that modifies an AgentError.
// Used with NewAgentError to set optional fields.
type ErrorOption func(*AgentError)
// WithCode sets the error code.
func WithCode(code string) ErrorOption {
return func(e *AgentError) {
e.Code = code
}
}
// WithCause sets the underlying cause error.
func WithCause(cause error) ErrorOption {
return func(e *AgentError) {
e.Cause = cause
}
}
// WithName sets the agent name that generated the error.
func WithName(name string) ErrorOption {
return func(e *AgentError) {
e.Name = name
}
}
// WithAgent extracts identification from agent configuration.
// Creates a string in the format "provider/model", "provider", or "model"
// depending on available information.
func WithAgent(cfg *config.AgentConfig) ErrorOption {
return func(e *AgentError) {
providerName := ""
modelName := ""
if cfg.Provider != nil {
providerName = cfg.Provider.Name
}
if cfg.Model != nil {
modelName = cfg.Model.Name
}
if providerName != "" && modelName != "" {
e.Client = fmt.Sprintf("%s/%s", providerName, modelName)
} else if providerName != "" {
e.Client = providerName
} else if modelName != "" {
e.Client = modelName
} else {
e.Client = "unknown"
}
}
}
// WithID sets a unique identifier for this error instance.
func WithID(id uuid.UUID) ErrorOption {
return func(e *AgentError) {
e.ID = id
}
}
// NewAgentInitError creates an initialization error.
// Shorthand for NewAgentError(ErrorTypeInit, message, options...).
func NewAgentInitError(message string, options ...ErrorOption) *AgentError {
return NewAgentError(ErrorTypeInit, message, options...)
}
// NewAgentLLMError creates an LLM interaction error.
// Shorthand for NewAgentError(ErrorTypeLLM, message, options...).
func NewAgentLLMError(message string, options ...ErrorOption) *AgentError {
return NewAgentError(ErrorTypeLLM, message, options...)
}