Skip to content
Source on GitHub (opens in a new tab)
import "github.com/barevoidco/zelk/core/provider/gemini"

Package gemini is the provider adapter for the Gemini API’s streamGenerateContent. It translates zelk’s portable history to Gemini contents and back, and sends each model turn’s thought signatures back exactly as they arrived, which Gemini requires for tool calling to continue. Open tool schemas retain their server contract with non-strict rendering; canonical tools retain the adapter’s existing schema dialect.

const BaseURL = "https://generativelanguage.googleapis.com/v1beta"

BaseURL is the Gemini API’s endpoint.

type Client struct {
// contains filtered or unexported fields
}

Client calls the Gemini API’s streamGenerateContent. It is safe for concurrent use.

func New(baseURL string, opts ...Option) (*Client, error)

New creates a client for baseURL, normally BaseURL. It performs no I/O and reads no process configuration.

Example

package gemini_test
import (
"errors"
"fmt"
"github.com/barevoidco/zelk/core/provider"
"github.com/barevoidco/zelk/core/provider/gemini"
)
func asProviderError(err error, target **provider.Error) bool { return errors.As(err, target) }
func ExampleNew() {
c, err := gemini.New(gemini.BaseURL, gemini.WithAPIKey("host-supplied-key"))
if err != nil {
fmt.Println(err)
return
}
defer c.Close()
fmt.Println(c.Family())
// Output: gemini
}

Output:

gemini
func (c *Client) Capabilities(ctx context.Context, model string) (provider.Caps, error)

Capabilities returns the declared capability snapshot without network I/O.

func (c *Client) Close() error

Close releases idle connections owned by this client. Injected clients are left to their owners. The client may be used again after Close.

func (c *Client) Family() history.Family

Family identifies the Gemini generateContent wire format.

func (c *Client) Models(ctx context.Context) ([]provider.Model, error)

Models returns the models declared with WithModels.

func (c *Client) Reservation(req *provider.Request, caps provider.Caps) event.Tokens

Reservation conservatively bounds the exact rendered body when the host has recorded a byte input bound. Otherwise it reserves the whole context window.

func (c *Client) SearchBackend(model string) (*Search, error)

SearchBackend returns a backend using model and this client’s credentials. The caller must keep the client open until all searches finish. The model must support the google_search tool; no request is made here.

func (c *Client) Stream(ctx context.Context, req *provider.Request) (provider.Stream, error)

Stream prepares a lazy single-use stream. Iteration owns and closes its HTTP response; breaking iteration or cancelling ctx stops the call.

type Option func(*Client)

Option configures a Client.

func WithAPIKey(key string) Option

WithAPIKey sets the API key, sent only to the provider, in the x-goog-api-key header: never in the URL, where it would reach logs and error messages.

func WithCapabilities(caps provider.Caps) Option

WithCapabilities sets the capabilities of models not declared with WithModels.

func WithHTTPClient(hc *http.Client) Option

WithHTTPClient injects the caller-owned HTTP client. It must be safe for concurrent use. Without it the client makes one that reads no proxy settings from the environment.

func WithModels(models ...provider.Model) Option

WithModels declares the models the client offers, each with its own capabilities, for Capabilities and Models to report. Capabilities remain those of any model not declared.

func WithStreamIdleTimeout(d time.Duration) Option

WithStreamIdleTimeout sets the maximum interval without response bytes on a stream, including with an injected HTTP client. It must be positive and defaults to five minutes. Expiry closes the body and returns ErrorTransient.

Example

client, err := gemini.New("https://example.test/v1",
gemini.WithStreamIdleTimeout(2*time.Minute))
if err != nil {
panic(err)
}
defer client.Close()
fmt.Println("client configured")
// Output: client configured

Output:

client configured
type Search struct {
// contains filtered or unexported fields
}

Search is a native web search backend: Grounding with Google Search on generateContent. It borrows its client’s transport and credentials, and is safe for concurrent use.

func (s *Search) Limit() event.Tokens

Limit bounds the entire native request, including server-supplied search context. Unknown model limits are zero and cannot support hard token budgets.

func (s *Search) Search(ctx context.Context, q search.Query) (search.Response, error)

Search makes one bounded, stateless request with Google Search enabled.

The API links every source through a Google redirect. Each is resolved to the page it names, with one request to Google that isn’t followed to the page, so WebFetch and its domain rules see the real site, as with other providers’ results. The API takes no domain filter, so domains are asked for in the prompt and enforced here on each source’s host, or, when its redirect couldn’t be resolved, on its site as the API gives it in the title; a source whose site can’t be read is dropped when domains are filtered, since it can’t be shown to match.