gemini

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Oct 14, 2026 License: MIT Imports: 33 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (

	// ChromeUserAgent 是所有上游请求使用的 Chrome 稳定版 User-Agent
	ChromeUserAgent = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/155.0.0.0 Safari/537.36"
)
View Source
const ImageModelID = "gemini-3.1-flash-image"

ImageModelID 是官网 Create image 工具的公开模型 ID

Variables

View Source
var ErrUpstreamFailureText = errors.New("gemini web returned a fixed failure text")

ErrUpstreamFailureText 表示 Gemini Web 用固定失败文字代替回答

Functions

func NewHTTPClient added in v0.1.2

func NewHTTPClient(timeoutSeconds int, proxyURL string) (tls_client.HttpClient, error)

NewHTTPClient 创建 TLS 与 HTTP/2 握手和 Chrome 稳定版一致的客户端

Types

type AccountSource

type AccountSource struct {
	ID       string
	Cookies  []Cookie
	Language string
	Save     func([]Cookie) error
	Refresh  func(context.Context) ([]Cookie, error)
}

AccountSource 表示账号固定身份和 Cookie 持久化入口

type Bootstrap

type Bootstrap struct {
	SNlM0e string
	BL     string
	FSID   string
	// Language 为首页 TuX5cc 给出的账号界面语言,用于 hl 与生成载荷语言槽
	Language string
	// UploadURL、UploadPushID、UploadClientContext 为首页 h1eoVe、qKIAYe、Ylro7b 给出的附件上传端点与请求头
	UploadURL           string
	UploadPushID        string
	UploadClientContext string
	// UploadTermsWindow 为首页实验 45725082 给出的上传条款确认有效期
	UploadTermsWindow time.Duration
	// TermsToken 为首页 thykhd 给出的服务条款同意令牌
	TermsToken  string
	Catalog     ModelCatalog
	Preferences Preferences
}

Bootstrap 表示首页和模型目录共同产生的运行参数

type Citation

type Citation struct {
	ID        int    `json:"id"`
	SourceID  string `json:"source_id,omitempty"`
	Claim     string `json:"claim,omitempty"`
	Title     string `json:"title,omitempty"`
	URL       string `json:"url"`
	Favicon   string `json:"favicon,omitempty"`
	Snippet   string `json:"snippet,omitempty"`
	Publisher string `json:"publisher,omitempty"`
	Start     int    `json:"start"`
	End       int    `json:"end"`
}

Citation 表示正文引用标记对应的网页来源

type Client

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

Client 表示与单一账号身份绑定的 Gemini Web 协议客户端

func NewClient

func NewClient(source AccountSource, proxyURL string, saveHistory bool) (*Client, error)

NewClient 创建固定 Cookie、代理和语言的账号客户端

func (*Client) AcquireRequest

func (c *Client) AcquireRequest(ctx context.Context) (func(), error)

AcquireRequest 独占同一账号的完整上游请求链,后台生成的轮询阶段让出

func (*Client) Busy added in v0.1.2

func (c *Client) Busy() bool

Busy 报告账号请求槽是否被占用或有请求在排队

func (*Client) CloseIdleConnections

func (c *Client) CloseIdleConnections()

CloseIdleConnections 关闭协议客户端的空闲连接

func (*Client) ContextWindow

func (c *Client) ContextWindow() int

ContextWindow 返回账号套餐对应的网页输入窗口

func (*Client) FetchMedia

func (c *Client) FetchMedia(ctx context.Context, mediaURL string) ([]byte, error)

FetchMedia 使用账号固定身份和 Cookie 下载媒体内容

func (*Client) FetchMediaContent added in v0.1.2

func (c *Client) FetchMediaContent(ctx context.Context, media Media) ([]byte, error)

FetchMediaContent 下载媒体内容,生成图片按官网“下载原图”取推荐尺寸原图

func (*Client) FetchUsage

func (c *Client) FetchUsage(ctx context.Context) (UsageInfo, error)

FetchUsage 读取官网当前账号用量

func (*Client) Init

func (c *Client) Init(ctx context.Context) error

Init 刷新首页动态参数并获取当前账号模型目录与偏好

func (*Client) Models

func (c *Client) Models() []Model

Models 返回当前账号初始化得到的模型目录

func (*Client) ResolveModel

func (c *Client) ResolveModel(id string) (Model, error)

ResolveModel 在当前账号模型目录中解析公开 ID

func (*Client) Stream

func (c *Client) Stream(ctx context.Context, request GenerateRequest, emit func(Event) error) error

Stream 执行一次生成并只输出规范事件,Deep Research 按官网计划、确认与报告流程执行

type CodeEventType

type CodeEventType string

CodeEventType 表示 Gemini Web 内置代码执行事件类型

const (
	CodeReference CodeEventType = "code_reference"
	CodeStdout    CodeEventType = "code_stdout"
	CodeStderr    CodeEventType = "code_stderr"
)

type CodeExecutionEvent

type CodeExecutionEvent struct {
	Index    int
	Type     CodeEventType
	Language string
	Offset   int
	Complete bool
}

CodeExecutionEvent 表示 Google 已执行的代码及其结果

type ConversationSnapshot

type ConversationSnapshot struct {
	CID  string
	RID  string
	RCID string
	// Token 为回答 payload[2]["26"] 给出的续接令牌
	Token     string
	ModelMode int
	Turns     int
}

ConversationSnapshot 表示可序列化的网页会话三元组、续接令牌、上一轮使用的模型 mode 与截至该回答的轮数

type ConversationState

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

ConversationState 保存线程安全的网页会话状态

func NewConversationState

func NewConversationState() *ConversationState

NewConversationState 创建空网页会话状态

func NewConversationStateFrom

func NewConversationStateFrom(snapshot ConversationSnapshot) *ConversationState

NewConversationStateFrom 从已有三元组创建网页会话状态

func (*ConversationState) Snapshot

Snapshot 返回当前会话状态副本

func (*ConversationState) Update

func (s *ConversationState) Update(snapshot ConversationSnapshot)

Update 原子替换非空会话字段

type Cookie struct {
	Name     string `json:"name"`
	Value    string `json:"value"`
	Domain   string `json:"domain"`
	Path     string `json:"path"`
	Expires  int64  `json:"expires"`
	HTTPOnly bool   `json:"http_only"`
	Secure   bool   `json:"secure"`
	SameSite string `json:"same_site"`
}

Cookie 表示可无损写回认证状态的 Cookie

type CookieRotator added in v0.1.2

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

CookieRotator 以官网轮换页的方式为一个账号轮换 Cookie,同一时间只由一个调用方使用

func NewCookieRotator added in v0.1.2

func NewCookieRotator(client *Client) *CookieRotator

NewCookieRotator 创建账号的 Cookie 轮换器

func (*CookieRotator) Rotate added in v0.1.2

func (r *CookieRotator) Rotate(ctx context.Context) (time.Duration, error)

Rotate 轮换 __Secure-1PSIDTS 并经 ListAccounts 确认会话有效,释放请求槽后重读账号状态,返回下次轮换前的等待时间;会话失效且账号带续签材料时在请求槽内续签并重读

type Event

type Event struct {
	Kind         EventKind
	Candidate    int
	Operation    SnapshotOperation
	Delta        string
	Snapshot     string
	PrefixLength int
	Phase        Phase
	Session      ConversationSnapshot
	Code         *CodeExecutionEvent
	Citations    []Citation
	Media        *Media
	Metadata     *EventMetadataData
	Usage        *Usage
	FinishReason FinishReason
	Err          *ProtocolError
}

Event 表示所有公开适配器共享的上游事件

type EventKind

type EventKind string

EventKind 表示规范化协议事件类型

const (
	EventSession       EventKind = "session"
	EventText          EventKind = "text"
	EventThought       EventKind = "thought"
	EventCode          EventKind = "code"
	EventCitations     EventKind = "citations"
	EventMedia         EventKind = "media"
	EventImageProgress EventKind = "image_progress"
	EventPhase         EventKind = "phase"
	EventMetadata      EventKind = "metadata"
	EventError         EventKind = "error"
	EventDone          EventKind = "done"
)

type EventMetadataData

type EventMetadataData struct {
	Title        string
	ControlToken string
	ModelHash    string
	ModelName    string
}

EventMetadataData 表示页面协议返回的附加元数据

type Feature added in v0.1.2

type Feature int

Feature 表示网页输入框工具菜单选中的功能,取值为官网 featureMode 编号

const (
	// FeatureDeepResearch 是 Deep Research 研究报告,只能在普通会话中使用
	FeatureDeepResearch Feature = 1
	// FeatureCanvas 是 Canvas 文档工具
	FeatureCanvas Feature = 2
	// FeatureVideo 是 Create video 视频生成,成品在生成流结束后由会话读取
	FeatureVideo Feature = 11
	// FeatureImage 是 Create image 生图工具
	FeatureImage Feature = 14
	// FeatureMusic 是 Create music 音乐生成
	FeatureMusic Feature = 21
	// FeatureLearning 是 Guided Learning 引导式学习
	FeatureLearning Feature = 24
	// FeatureTask 是 Task 模式,只能在普通会话中使用
	FeatureTask Feature = 40
)

type FileData

type FileData struct {
	// Data 为待上传的文件内容,生成前上传
	Data []byte
	// URL 为上传服务返回的文件路径
	URL      string
	FileName string
	MimeType string
	// FileID 为 ProcessFile 返回的文件 ID,生成前登记
	FileID string
}

FileData 表示生成请求携带的附件

type FinishReason

type FinishReason string

FinishReason 表示规范化结束原因

const (
	FinishUnknown   FinishReason = "unknown"
	FinishStop      FinishReason = "stop"
	FinishCancelled FinishReason = "cancelled"
	FinishError     FinishReason = "error"
)

type FrameDecoder

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

FrameDecoder 将 Google 累计帧转换为唯一规范事件流

func NewFrameDecoder

func NewFrameDecoder() *FrameDecoder

NewFrameDecoder 创建独立的单响应解码器

func (*FrameDecoder) Decode

func (d *FrameDecoder) Decode(reader io.Reader, state *ConversationState, emit func(Event) error) error

Decode 增量解析协议帧并原子更新会话状态

type GenerateRequest

type GenerateRequest struct {
	Prompt        string
	Model         string
	ModelMode     int
	ThinkingLevel ThinkingLevel
	Feature       Feature
	// AspectRatio 为生成媒体请求的宽高比,例如 16:9 或 1536x1024,空值使用官网默认
	AspectRatio string

	Files        []FileData
	Conversation *ConversationState
	// Branch 表示从会话中较早的回答继续,官网对应编辑该回答之后的提问
	Branch bool
	// contains filtered or unexported fields
}

GenerateRequest 表示一次网页协议生成请求

type Media

type Media struct {
	Type        MediaType `json:"type"`
	URL         string    `json:"url"`
	Title       string    `json:"title,omitempty"`
	Alt         string    `json:"alt,omitempty"`
	FileName    string    `json:"file_name,omitempty"`
	MIMEType    string    `json:"mime_type,omitempty"`
	Generator   string    `json:"generator,omitempty"`
	Placeholder string    `json:"placeholder,omitempty"`
	Width       int       `json:"width,omitempty"`
	Height      int       `json:"height,omitempty"`
	SizeBytes   int       `json:"size_bytes,omitempty"`
	Offset      int       `json:"offset"`
	// contains filtered or unexported fields
}

Media 表示规范事件携带的网页或生成媒体

type MediaType

type MediaType string

MediaType 表示 Gemini Web 媒体类型

const (
	// MediaGeneratedImage 是 Create image 生成的图片
	MediaGeneratedImage MediaType = "generated_image"
	// MediaGeneratedVideo 是 Create video 生成的视频
	MediaGeneratedVideo MediaType = "generated_video"
	// MediaGeneratedAudio 是 Create music 生成的音频
	MediaGeneratedAudio MediaType = "generated_audio"
)

type Model

type Model struct {
	ID          string
	DisplayName string
	Description string
	Hash        string
	// Tier 为模型行第 4 槽的账号层级,生成请求模型头第 11 项回传该值
	Tier         int
	Capabilities []string
	Mode         int
	Default      bool
	// ThinkingLevels 为官网模型菜单中该模型可选的思考强度
	ThinkingLevels []ThinkingLevel
	// Feature 为该 ID 在生成请求中选中的官网工具,普通对话模型为零
	Feature Feature
}

Model 表示账号启动时发现的网页模型

type ModelCatalog

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

ModelCatalog 表示单个账号当前可用的模型目录

func (ModelCatalog) Default

func (c ModelCatalog) Default() (Model, error)

Default 返回官方标记的默认模型

func (ModelCatalog) List

func (c ModelCatalog) List() []Model

List 返回稳定排序的模型副本

func (ModelCatalog) Resolve

func (c ModelCatalog) Resolve(value string) (Model, error)

Resolve 根据公开 ID、官方名称或 hash 查找模型

type Phase

type Phase int

Phase 表示候选生成阶段

const (
	PhaseUnknown      Phase = 0
	PhaseGenerating   Phase = 1
	PhaseComplete     Phase = 2
	PhaseToolComplete Phase = 4
)

type Preferences added in v0.1.2

type Preferences []any

Preferences 是 ESY5D 返回的账号偏好消息,字段号超出数组的值位于末尾的稀疏对象

func (Preferences) Bool added in v0.1.2

func (p Preferences) Bool(number int) bool

Bool 按字段号返回布尔偏好,缺失时为 false

func (Preferences) Field added in v0.1.2

func (p Preferences) Field(number int) any

Field 按字段号返回偏好值

type ProtocolError

type ProtocolError struct {
	HTTPStatus int
	Code       int
	Message    string
	Retryable  bool
	Cause      error
}

ProtocolError 表示 HTTP 或帧内协议错误

func (*ProtocolError) Error

func (e *ProtocolError) Error() string

Error 返回适合日志和公开错误映射的描述

func (*ProtocolError) Unwrap

func (e *ProtocolError) Unwrap() error

Unwrap 返回底层错误

type SnapshotOperation

type SnapshotOperation string

SnapshotOperation 表示累计快照相对上一版本的变化

const (
	SnapshotAppend   SnapshotOperation = "append"
	SnapshotReplace  SnapshotOperation = "replace"
	SnapshotTruncate SnapshotOperation = "truncate"
)

type ThinkingLevel added in v0.1.2

type ThinkingLevel int

ThinkingLevel 表示网页模型菜单中的思考强度,取值为官网协议编号

const (
	// ThinkingLow 是低思考强度
	ThinkingLow ThinkingLevel = 1
	// ThinkingMedium 是中等思考强度
	ThinkingMedium ThinkingLevel = 4
	// ThinkingHigh 是高思考强度
	ThinkingHigh ThinkingLevel = 2
)

func (ThinkingLevel) String added in v0.1.2

func (l ThinkingLevel) String() string

String 返回思考强度的小写名称

type Usage

type Usage struct {
	PromptTokens     int
	CompletionTokens int
	ThoughtTokens    int
	TotalTokens      int
}

Usage 表示上游提供的令牌用量

func (Usage) OutputTokens

func (u Usage) OutputTokens() int

OutputTokens 返回包含可见思考的输出 token 数

type UsageInfo

type UsageInfo struct {
	Tier                string       `json:"tier"`
	TierCode            int          `json:"tier_code"`
	Current             *UsageWindow `json:"current,omitempty"`
	Weekly              *UsageWindow `json:"weekly,omitempty"`
	RemainingCredits    *float64     `json:"remaining_credits,omitempty"`
	UseOverageAICredits bool         `json:"use_overage_ai_credits"`
	FetchedAt           time.Time    `json:"fetched_at"`
}

UsageInfo 表示单个账号的官网用量信息

type UsageWindow

type UsageWindow struct {
	UsedRatio float64   `json:"used_ratio"`
	ResetAt   time.Time `json:"reset_at"`
}

UsageWindow 表示官网展示的一段用量窗口

Jump to

Keyboard shortcuts

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