84 lines
2.4 KiB
Go
84 lines
2.4 KiB
Go
package contracts
|
|
|
|
import "errors"
|
|
|
|
// ErrInvalidStructuredOutput identifies a provider response that cannot satisfy
|
|
// the caller's declared structured-output contract.
|
|
var ErrInvalidStructuredOutput = errors.New("invalid structured output")
|
|
|
|
// ErrLLMCapacityExceeded identifies backend admission exhaustion before model
|
|
// generation begins.
|
|
var ErrLLMCapacityExceeded = errors.New("LLM capacity exceeded")
|
|
|
|
// ErrLLMGeneration identifies a provider generation failure.
|
|
var ErrLLMGeneration = errors.New("LLM generation failed")
|
|
|
|
type LLMGenerationFailureKind string
|
|
|
|
const (
|
|
LLMGenerationFailureGeneration LLMGenerationFailureKind = "generation"
|
|
LLMGenerationFailureTransport LLMGenerationFailureKind = "transport"
|
|
LLMGenerationFailureProviderStatus LLMGenerationFailureKind = "provider_status"
|
|
LLMGenerationFailureMalformedResponse LLMGenerationFailureKind = "malformed_response"
|
|
)
|
|
|
|
type LLMGenerationMetadata struct {
|
|
FailureKind LLMGenerationFailureKind
|
|
RetriesExhausted bool
|
|
Attempts uint64
|
|
}
|
|
|
|
type LLMGenerationError struct {
|
|
status int
|
|
diagnostic string
|
|
failureKind LLMGenerationFailureKind
|
|
exhausted bool
|
|
attempts uint64
|
|
}
|
|
|
|
func NewLLMGenerationError(status int, diagnostic string) *LLMGenerationError {
|
|
return NewLLMGenerationErrorWithMetadata(status, diagnostic, LLMGenerationMetadata{})
|
|
}
|
|
|
|
func NewLLMGenerationErrorWithMetadata(status int, diagnostic string, metadata LLMGenerationMetadata) *LLMGenerationError {
|
|
if status < 0 {
|
|
status = 0
|
|
}
|
|
kind := metadata.FailureKind
|
|
if kind == "" {
|
|
kind = LLMGenerationFailureGeneration
|
|
}
|
|
return &LLMGenerationError{status: status, diagnostic: diagnostic, failureKind: kind, exhausted: metadata.RetriesExhausted, attempts: metadata.Attempts}
|
|
}
|
|
func (e *LLMGenerationError) Error() string {
|
|
if e == nil || e.diagnostic == "" {
|
|
return ErrLLMGeneration.Error()
|
|
}
|
|
return e.diagnostic
|
|
}
|
|
func (e *LLMGenerationError) Unwrap() error { return ErrLLMGeneration }
|
|
func (e *LLMGenerationError) StatusCode() int {
|
|
if e == nil {
|
|
return 0
|
|
}
|
|
return e.status
|
|
}
|
|
|
|
func (e *LLMGenerationError) FailureKind() LLMGenerationFailureKind {
|
|
if e == nil || e.failureKind == "" {
|
|
return LLMGenerationFailureGeneration
|
|
}
|
|
return e.failureKind
|
|
}
|
|
|
|
func (e *LLMGenerationError) RetriesExhausted() bool {
|
|
return e != nil && e.exhausted
|
|
}
|
|
|
|
func (e *LLMGenerationError) Attempts() uint64 {
|
|
if e == nil {
|
|
return 0
|
|
}
|
|
return e.attempts
|
|
}
|