Documentation
¶
Index ¶
- Constants
- Variables
- func IsAuthenticationError(err error) bool
- func NewHTTPClient(timeoutSeconds int, proxyURL string) (tls_client.HttpClient, error)
- func ProbeProxy(ctx context.Context, proxyURL string) (time.Duration, error)
- type AccountSource
- type Bootstrap
- type Citation
- type Client
- func (c *Client) AcquireRequest(ctx context.Context) (func(), error)
- func (c *Client) Busy() bool
- func (c *Client) CachedUsage() *UsageInfo
- func (c *Client) CloseIdleConnections()
- func (c *Client) ContextWindow() int
- func (c *Client) Email() string
- func (c *Client) FetchMedia(ctx context.Context, mediaURL string) ([]byte, error)
- func (c *Client) FetchMediaContent(ctx context.Context, media Media) ([]byte, error)
- func (c *Client) FetchRemoteMedia(ctx context.Context, mediaURL string, ...) ([]byte, error)
- func (c *Client) FetchUsage(ctx context.Context) (UsageInfo, error)
- func (c *Client) Guest() bool
- func (c *Client) Init(ctx context.Context) error
- func (c *Client) Models() []Model
- func (c *Client) Pending() int
- func (c *Client) ResolveModel(id string) (Model, error)
- func (c *Client) SetReleaseHook(hook func())
- func (c *Client) Speech(ctx context.Context, text string, language string, format SpeechFormat) ([]byte, error)
- func (c *Client) Stream(ctx context.Context, request GenerateRequest, emit func(Event) error) error
- func (c *Client) TryAcquireRequest() (func(), bool)
- type CodeEventType
- type CodeExecutionEvent
- type ConversationSnapshot
- type ConversationState
- type Cookie
- type CookieRotator
- type Event
- type EventKind
- type EventMetadataData
- type Feature
- type FileData
- type FinishReason
- type FrameDecoder
- type Gem
- type GenerateRequest
- type Media
- type MediaType
- type Model
- type ModelCatalog
- type Phase
- type Preferences
- type ProtocolError
- type SnapshotOperation
- type SpeechFormat
- type ThinkingLevel
- type Usage
- type UsageInfo
- type UsageWindow
Constants ¶
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"
)
const GemModelPrefix = "gem-"
GemModelPrefix 是 Gem 在公开模型目录中的 ID 前缀
const GuestID = "guest"
GuestID 是访客会话在账号池、日志与账本中的 ID
const ImageModelID = "gemini-3.1-flash-image"
ImageModelID 是官网 Create image 工具的公开模型 ID
const SpeechTextLimit = 4096
SpeechTextLimit 是单次朗读接受的最大字符数
Variables ¶
var ErrAccountRestricted = errors.New("Gemini 账号不可用")
ErrAccountRestricted 表示模型目录的账号状态码显示账号暂时不能使用 Gemini,例如待同意条款、地区或监护人限制
var ErrCaptcha = errors.New("请求被 Google 人机验证页拦截")
ErrCaptcha 表示 Google 以人机验证页(/sorry)拦截了请求,出口 IP 与账号会话被判定为异常流量
var ErrConversationLost = errors.New("Gemini Web 网页会话已失效")
ErrConversationLost 表示续接的网页会话在上游已不存在:上游在新会话中作答或返回 1097
var ErrGuestAttachments = &ProtocolError{HTTPStatus: 503, Message: "访客会话不能上传附件,需要可用的登录账号"}
ErrGuestAttachments 表示带附件的请求落到访客会话,官网访客页面的上传入口全部禁用
var ErrModelMismatch = errors.New("gemini web served a different model")
ErrModelMismatch 表示上游实际回答的模型与请求的模型不同,常见于该模型额度用尽后的降级
var ErrSignedOut = errors.New("Gemini Web 会话已失效")
ErrSignedOut 表示账号会话已失效且未能续签,需要重新导入
var ErrTransport = errors.New("gemini web transport failure")
ErrTransport 表示连接、TLS 或代理等网络层失败,与账号状态无关
var ErrUpstreamFailureText = errors.New("gemini web returned a fixed failure text")
ErrUpstreamFailureText 表示 Gemini Web 用固定失败文字代替回答
Functions ¶
func IsAuthenticationError ¶ added in v0.1.3
IsAuthenticationError 判断错误是否表示账号会话失效,包括 HTTP 401、403 与续签失败
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
// AuthUser 是目标账号在多账号 Cookie 中的序号,对应官网地址 /u/N,0 为默认账号
AuthUser int
Save func([]Cookie) error
Refresh func(context.Context) ([]Cookie, error)
// Concurrency 是账号同时进行的上游请求数,零按 1 处理
Concurrency int
}
AccountSource 表示账号固定身份和 Cookie 持久化入口
type Bootstrap ¶
type Bootstrap struct {
// SNlM0e 为登录会话的表单令牌,访客会话为空
SNlM0e string
BL string
FSID string
// Language 为首页 TuX5cc 给出的账号界面语言,用于 hl 与生成载荷语言槽
Language string
// Email 为首页 oPEP7c 给出的当前登录邮箱
Email 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 NewGuestClient ¶ added in v0.1.3
NewGuestClient 创建以未登录会话访问官网的访客客户端
func (*Client) AcquireRequest ¶
AcquireRequest 占用账号的一个请求槽直到完整上游请求链结束,后台生成的轮询阶段让出
func (*Client) CachedUsage ¶ added in v0.1.3
CachedUsage 返回最近一次读取的官网用量,尚未读取时为 nil
func (*Client) CloseIdleConnections ¶
func (c *Client) CloseIdleConnections()
CloseIdleConnections 关闭协议客户端的空闲连接
func (*Client) FetchMedia ¶
FetchMedia 使用账号固定身份和 Cookie 下载媒体内容
func (*Client) FetchMediaContent ¶ added in v0.1.2
FetchMediaContent 下载媒体内容,生成图片按官网“下载原图”取推荐尺寸原图
func (*Client) FetchRemoteMedia ¶ added in v0.1.6
func (c *Client) FetchRemoteMedia(ctx context.Context, mediaURL string, checkHost func(context.Context, string) error, limit int64) ([]byte, error)
FetchRemoteMedia 以账号的浏览器身份与出口、不带账号 Cookie 下载客户端给出的媒体地址,每一跳先以 checkHost 检查目标主机,声明或读到的大小超过 limit 时返回错误
func (*Client) FetchUsage ¶
FetchUsage 读取官网当前账号用量
func (*Client) ResolveModel ¶
ResolveModel 在当前账号模型目录中解析公开 ID
func (*Client) SetReleaseHook ¶ added in v0.1.6
func (c *Client) SetReleaseHook(hook func())
SetReleaseHook 设置请求槽释放后调用的函数
func (*Client) Speech ¶ added in v0.1.3
func (c *Client) Speech(ctx context.Context, text string, language string, format SpeechFormat) ([]byte, error)
Speech 以官网朗读 RPC 把文本合成为音频,language 为空时使用账号界面语言;调用方持有账号请求槽
func (*Client) TryAcquireRequest ¶ added in v0.1.6
TryAcquireRequest 在账号有空闲请求槽时占用一个并返回释放函数
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 ¶
func (s *ConversationState) Snapshot() ConversationSnapshot
Snapshot 返回当前会话状态副本
func (*ConversationState) Update ¶
func (s *ConversationState) Update(snapshot ConversationSnapshot)
Update 原子替换非空会话字段
type Cookie ¶
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 轮换器
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
// LimitNotice 是用量用满后上游随降级回答给出的额度提示
LimitNotice 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 (*FrameDecoder) Decode ¶
func (d *FrameDecoder) Decode(reader io.Reader, state *ConversationState, emit func(Event) error) error
Decode 增量解析 StreamGenerate 协议帧并原子更新会话状态
type GenerateRequest ¶
type GenerateRequest struct {
Prompt string
Model string
ModelMode int
ThinkingLevel ThinkingLevel
Feature Feature
// Gem 为在 Gem 中对话时的 Gem ID
Gem string
// 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"`
// Thumbnail 是网页视频条目给出的缩略图地址
Thumbnail string `json:"thumbnail,omitempty"`
Offset int `json:"offset"`
// contains filtered or unexported fields
}
Media 表示规范事件携带的网页或生成媒体
type Model ¶
type Model struct {
ID string
DisplayName string
Description string
Hash string
Capabilities []string
Mode int
Default bool
// ThinkingLevels 为官网模型菜单中该模型可选的思考强度
ThinkingLevels []ThinkingLevel
// Feature 为该 ID 在生成请求中选中的官网工具,普通对话模型为零
Feature Feature
// Gem 为 Gem 条目的 Gem ID,写入生成请求 19 槽
Gem string
}
Model 表示账号启动时发现的网页模型
type ModelCatalog ¶
type ModelCatalog struct {
// contains filtered or unexported fields
}
ModelCatalog 表示单个账号当前可用的模型目录
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 ¶
ProtocolError 表示 HTTP 或帧内协议错误
type SnapshotOperation ¶
type SnapshotOperation string
SnapshotOperation 表示累计快照相对上一版本的变化
const ( SnapshotAppend SnapshotOperation = "append" SnapshotReplace SnapshotOperation = "replace" SnapshotTruncate SnapshotOperation = "truncate" )
type SpeechFormat ¶ added in v0.1.3
type SpeechFormat int
SpeechFormat 是朗读音频的容器格式,取值写入 XqA3Ic 请求第 4 槽
const ( // SpeechMP3 是 24 kHz 单声道 MPEG-2 Layer III SpeechMP3 SpeechFormat = 1 // SpeechOggOpus 是官网页面播放使用的 Ogg Opus SpeechOggOpus SpeechFormat = 2 )
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 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 表示官网展示的一段用量窗口