Documentation
¶
Overview ¶
Package google adapts the Google Gemini generateContent and streamGenerateContent APIs to the protocol-independent kernel model (spec §19.1). It depends only on the standard library and internal/kernel, and must not import store or session.
Gemini has no tool call ids. Decode synthesizes one for every functionCall part, "gc:<name>:<n>", where n counts the calls with that name from the start of the contents. A functionResponse is linked to the oldest unanswered call with the same name (FIFO), so ids are stable as long as Decode sees the full history, which the proxy always sends. The ids are never sent upstream.
Index ¶
- Constants
- func Encode(req *Request, window []kernel.Message) ([]byte, error)
- func HasTool(body []byte, name string) bool
- func InjectTools(body []byte) ([]byte, error)
- func IsInternalTool(name string) bool
- func ParseRef(s string) (int64, error)
- func ToolDefinitions() [][]byte
- type CompressArgs
- type CompressFold
- type CompressRange
- type DecompressArgs
- type Request
- type RetrieveArgs
- type SearchArgs
- type StreamCall
- type StreamSummary
- type Usage
Constants ¶
const ( ToolCompress = "compress" ToolDecompress = "decompress" ToolSearchContext = "search_context" ToolRetrieve = "retrieve" )
Tool names exposed to the model (spec §16).
Variables ¶
This section is empty.
Functions ¶
func Encode ¶
Encode rebuilds the request body with its contents replaced by the rendered window. Every other top-level field is copied byte for byte, and so are the parts that were not changed, including unknown part members such as thoughtSignature. Text gets its reference label.
The window is made valid for the API: a functionCall is kept only when its functionResponse appears later in the window, and a functionResponse only when its call is kept. Duplicate calls and responses are dropped. Reasoning has no wire form when fresh and is not sent back. Contents left without parts are dropped. A window message that did not come from the request (a summary block, an intent ledger, or a tool call or result the window rewrote) is sent as a user or model part. A fresh system message is sent as a user part, because Gemini contents have no system role.
func HasTool ¶
HasTool reports whether the request body declares a function with the given name. Malformed bodies report false.
func InjectTools ¶
InjectTools adds the compress and decompress declarations to the request's tools array. A declaration already present under the same name is skipped. The missing declarations are appended to the first existing tool entry that has a functionDeclarations array, so the tools array keeps its shape. When there is no such entry, a new {"functionDeclarations":[...]} entry is appended. Existing tools and every other field are copied byte for byte. When nothing is added the body is returned unchanged, so the call is idempotent.
func IsInternalTool ¶
IsInternalTool reports whether name is one of the proxy's own tools (spec §16). Their calls never reach the client.
func ParseRef ¶
ParseRef parses a message reference such as "m00042", "m42", "#42" or "42" into its ledger sequence number. The number must be at least 1.
func ToolDefinitions ¶
func ToolDefinitions() [][]byte
ToolDefinitions returns fresh copies of the compress and decompress function declarations, each one a member of a functionDeclarations array.
Types ¶
type CompressArgs ¶
type CompressArgs struct {
Ranges []CompressRange
Folds []CompressFold
CloseIntents []int64
}
CompressArgs is the normalized input of a compress function call.
func ParseCompressArgs ¶
func ParseCompressArgs(args []byte) (CompressArgs, error)
ParseCompressArgs parses the args of a compress functionCall. Gemini sends args as a JSON object, so that is the normal input. A JSON string that holds the object (double encoding) is accepted once. The alternate field names that models produce are accepted too (for example start/end, text/content, title, close_intents), and refs may be strings or numbers.
type CompressFold ¶
CompressFold is one fold of a compress call (spec §12): the active blocks one tier below Tier, from StartBlock to EndBlock, become one block of Tier.
type CompressRange ¶
CompressRange is one summarized range of ledger sequence numbers, inclusive.
type DecompressArgs ¶
DecompressArgs is the normalized input of a decompress function call.
func ParseDecompressArgs ¶
func ParseDecompressArgs(args []byte) (DecompressArgs, error)
ParseDecompressArgs parses the args of a decompress functionCall. The same rules as ParseCompressArgs apply. A missing mode defaults to context, and an unknown mode is an error.
type Request ¶
type Request struct {
// contains filtered or unexported fields
}
Request is a decoded generateContent request.
type RetrieveArgs ¶
RetrieveArgs is the normalized input of a retrieve call.
func ParseRetrieveArgs ¶
func ParseRetrieveArgs(args []byte) (RetrieveArgs, error)
ParseRetrieveArgs parses the arguments of a retrieve call. The recovery rules are those of toolargs.ParseRetrieve.
type SearchArgs ¶
SearchArgs is the normalized input of a search_context call.
func ParseSearchArgs ¶
func ParseSearchArgs(args []byte) (SearchArgs, error)
ParseSearchArgs parses the arguments of a search_context call. The recovery rules are those of toolargs.ParseSearch.
type StreamCall ¶
type StreamCall struct {
Candidate int
Part int // index of the part within the candidate, over the whole stream
Name string
Arguments string // compact JSON object
Compress *CompressArgs // set when Name is compress and parsing succeeded
Decompress *DecompressArgs // set when Name is decompress and parsing succeeded
Search *SearchArgs // set when Name is search_context and parsing succeeded
Retrieve *RetrieveArgs // set when Name is retrieve and parsing succeeded
Err error // argument parse error
}
StreamCall is a compress or decompress functionCall found in an SSE stream. Gemini sends a functionCall whole inside one part, so each call is complete when it is observed. A streamed call has no id, because the id is synthesized by Decode from the position in the history, and the caller assigns it when the call comes back in the next request.
type StreamSummary ¶
type StreamSummary struct {
Usage Usage
HasUsage bool
Calls []StreamCall
// Complete is true when a candidate reported a finishReason.
Complete bool
}
StreamSummary is what StreamPassthrough observed while copying the stream.
func StreamPassthrough ¶
StreamPassthrough copies a streamGenerateContent SSE stream (alt=sse) from src to dst byte for byte, line by line, and observes it on the way. It records the compress and decompress functionCall parts and collects usageMetadata from the chunks that carry it. A malformed data line is copied through and ignored. Argument parse errors are reported in StreamCall.Err and do not stop the stream.
type Usage ¶
type Usage struct {
PromptTokens int
CompletionTokens int
TotalTokens int
CachedTokens int // usageMetadata.cachedContentTokenCount
}
Usage holds the token counters of a generateContent response. PromptTokens already includes CachedTokens, as in the OpenAI counters.
func ParseUsage ¶
ParseUsage extracts usageMetadata from a non-streaming generateContent response. The bool is false when the response has no usageMetadata object.
func (Usage) UncachedPromptTokens ¶
UncachedPromptTokens returns the prompt tokens that were not served from the cache. It never returns a negative number.