Documentation
¶
Overview ¶
Package openaishared holds what the two OpenAI-shaped dialects (openai-chat and openai-responses) do identically: usage mapping, error-envelope parsing, tool-call id assignment and the small helpers around them. One copy, so the dialects cannot drift on the rules they share (usage.Detail's "not reported is not zero" above all).
Index ¶
- func Clamp32(n int64) int32
- func CodeString(raw json.RawMessage) string
- func F64(v *float32) *float64
- func MapUsage(u *Usage, s Stamp, md map[string]any) (*genai.GenerateContentResponseUsageMetadata, map[string]any)
- func NewCallID() string
- func NonNilMap(m map[string]any) map[string]any
- func Ptr[T any](v T) *T
- func StampNotes(r *llm.Response, notes map[string]any)
- func StampRetry(r *llm.Response, rec *retry.Record)
- func TextOf(c *genai.Content) string
- type ErrorFields
- type IDAssigner
- type Stamp
- type Usage
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Clamp32 ¶
Clamp32 narrows a count to genai's int32 fields, saturating rather than wrapping: an absurd count gets a ceiling, never a negative number. Detail keeps the full int64.
func CodeString ¶
func CodeString(raw json.RawMessage) string
CodeString renders an error code that a server sends as a string or as a number.
func MapUsage ¶
func MapUsage(u *Usage, s Stamp, md map[string]any) (*genai.GenerateContentResponseUsageMetadata, map[string]any)
MapUsage maps u onto genai's struct and attaches a usage.Detail to md, returning both. A count the server did not send is absent from both, never a zero; a nil u gives no genai usage at all. genai counts reasoning apart from candidates, so it is subtracted where reported.
func NonNilMap ¶
NonNilMap returns m, or an empty map for nil, so a tool call with no arguments marshals as {}.
func StampNotes ¶
StampNotes adds a request's notes (a downgraded tool choice, say) to a response's metadata.
func StampRetry ¶
StampRetry records the retries a call took under retry.MetadataKey, when it took any.
Types ¶
type ErrorFields ¶
type ErrorFields struct {
Type, Code, Message, Param string
}
ErrorFields is what an error answer says about itself.
func ParseError ¶
func ParseError(raw []byte) ErrorFields
ParseError reads an error answer's body: OpenAI's {"error": {...}}, the same wrapped in a one-element array (Vertex AI), or a bare message or detail. With nothing recognizable, the body itself is the message.
type IDAssigner ¶
type IDAssigner struct {
// contains filtered or unexported fields
}
IDAssigner gives every tool call an id and every tool result the id of the call it answers. History written by another provider (Gemini does not set ids) has none; such calls get a generated id and their results are matched to them by name, in order.
func NewIDAssigner ¶
func NewIDAssigner() *IDAssigner
NewIDAssigner returns an empty IDAssigner, one per conversation.
func (*IDAssigner) Call ¶
func (a *IDAssigner) Call(id, name string) string
Call returns a call's id, generating one when it has none.
func (*IDAssigner) Response ¶
func (a *IDAssigner) Response(id, name string) string
Response returns the id a result answers: its own, or the oldest unanswered generated id for the same tool name.
type Stamp ¶
type Stamp struct {
RequestID, ServedModel, Backend, Region string
// CachedUnreliable records cache counts as not reported even when
// the server sends them (a profile's usage.cached_tokens: unreliable).
CachedUnreliable bool
}
Stamp says what the Detail beside the usage names.
type Usage ¶
type Usage struct {
Input, Output, Total *int64
// Reasoning is usually counted inside Output (OpenAI); some servers
// (xAI) count it beside Output. MapUsage tells them apart by Total.
Reasoning *int64
// CacheRead and CacheWrite are subsets of Input.
CacheRead, CacheWrite *int64
}
Usage is a server's token counts, normalized from either dialect's wire shape. A nil count was not reported.