openaishared

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 13, 2026 License: Apache-2.0 Imports: 9 Imported by: 0

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

Constants

This section is empty.

Variables

This section is empty.

Functions

func Clamp32

func Clamp32(n int64) int32

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 F64

func F64(v *float32) *float64

F64 widens an optional float32.

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 NewCallID

func NewCallID() string

NewCallID returns a fresh tool-call id.

func NonNilMap

func NonNilMap(m map[string]any) map[string]any

NonNilMap returns m, or an empty map for nil, so a tool call with no arguments marshals as {}.

func Ptr

func Ptr[T any](v T) *T

Ptr returns a pointer to v.

func StampNotes

func StampNotes(r *llm.Response, notes map[string]any)

StampNotes adds a request's notes (a downgraded tool choice, say) to a response's metadata.

func StampRetry

func StampRetry(r *llm.Response, rec *retry.Record)

StampRetry records the retries a call took under retry.MetadataKey, when it took any.

func TextOf

func TextOf(c *genai.Content) string

TextOf joins a content's non-thought text parts.

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 counted inside Output, as both dialects report it.
	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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL