adapter

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Oct 15, 2026 License: MIT Imports: 53 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

This section is empty.

Variables

This section is empty.

Functions

func AuthMiddleware

func AuthMiddleware(apiKey func() string) gin.HandlerFunc

AuthMiddleware 校验服务公开 API key,apiKey 返回当前生效的密钥,多个 key 以逗号分隔,任一匹配即通过;未设密钥时拒绝 null 来源与非本机网页发起的请求,以 IP 地址打开的本服务页面照常访问,断点续传的后续请求凭 upload_id 授权

func CORSMiddleware

func CORSMiddleware() gin.HandlerFunc

CORSMiddleware 允许任意网页跨域调用公开 API,预检回显请求的请求头并暴露全部响应头,管理端 /api 不允许跨域

func ChatCompletionHandler

func ChatCompletionHandler(pool *balancer.AccountPool) gin.HandlerFunc

ChatCompletionHandler 处理 OpenAI Chat Completions

func ClaudeCountTokensHandler

func ClaudeCountTokensHandler(pool *balancer.AccountPool) gin.HandlerFunc

ClaudeCountTokensHandler 处理 Anthropic 输入 token 计数

func ClaudeMessagesHandler

func ClaudeMessagesHandler(pool *balancer.AccountPool) gin.HandlerFunc

ClaudeMessagesHandler 处理 Anthropic Messages

func CompletionHandler added in v0.1.3

func CompletionHandler(pool *balancer.AccountPool) gin.HandlerFunc

CompletionHandler 处理 OpenAI /v1/completions:prompt 与 suffix 作为会话发送,每个 prompt 逐个生成 n 个 choice

func ConfigureSessionTTL

func ConfigureSessionTTL(ttl time.Duration)

ConfigureSessionTTL 设置显式会话状态的有效期

func FileContentHandler added in v0.1.3

func FileContentHandler(store *requestdb.Store) gin.HandlerFunc

FileContentHandler 返回文件内容

func FileDeleteHandler added in v0.1.3

func FileDeleteHandler(store *requestdb.Store) gin.HandlerFunc

FileDeleteHandler 删除文件,Anthropic 返回 file_deleted 对象

func FileListHandler added in v0.1.3

func FileListHandler(store *requestdb.Store) gin.HandlerFunc

FileListHandler 列出文件:OpenAI 按 purpose、order、after、limit 分页,Anthropic 按 page、limit 分页

func FileRetrieveHandler added in v0.1.3

func FileRetrieveHandler(store *requestdb.Store) gin.HandlerFunc

FileRetrieveHandler 返回 OpenAI 或 Anthropic 文件元数据

func FileUploadHandler added in v0.1.3

func FileUploadHandler(store *requestdb.Store) gin.HandlerFunc

FileUploadHandler 处理 OpenAI 与 Anthropic 文件上传,带 anthropic-version 头时返回 Anthropic 文件对象

func GeminiFileDeleteHandler added in v0.1.3

func GeminiFileDeleteHandler(store *requestdb.Store) gin.HandlerFunc

GeminiFileDeleteHandler 删除 Gemini 文件并返回空对象

func GeminiFileGetHandler added in v0.1.3

func GeminiFileGetHandler(store *requestdb.Store) gin.HandlerFunc

GeminiFileGetHandler 返回 Gemini 文件资源,files/{name}:download 返回内容

func GeminiFileListHandler added in v0.1.3

func GeminiFileListHandler(store *requestdb.Store) gin.HandlerFunc

GeminiFileListHandler 按 pageSize 与 pageToken 列出 Gemini 文件

func GeminiFileUploadHandler added in v0.1.3

func GeminiFileUploadHandler(store *requestdb.Store) gin.HandlerFunc

GeminiFileUploadHandler 处理 /upload/v1beta/files 的断点续传、multipart 与单次媒体上传,创建时删除上次运行遗留的续传临时文件

func GeminiGetModelHandler added in v0.1.3

func GeminiGetModelHandler(pool *balancer.AccountPool) gin.HandlerFunc

GeminiGetModelHandler 返回单个 Gemini 模型

func GeminiListModelsHandler

func GeminiListModelsHandler(pool *balancer.AccountPool) gin.HandlerFunc

GeminiListModelsHandler 返回 Gemini 模型目录

func GeminiLiveHandler added in v0.1.3

func GeminiLiveHandler(c *gin.Context)

GeminiLiveHandler 处理 Gemini Live API 的 BidiGenerateContent 连接,Gemini Web 没有该能力时返回 404 NOT_FOUND

func GeminiOperationHandler added in v0.1.3

func GeminiOperationHandler(store *requestdb.Store) gin.HandlerFunc

GeminiOperationHandler 返回 predictLongRunning 的长时操作,路径为 models/{model}/operations/{id} 或 operations/{id}

func GeminiRouterHandler

func GeminiRouterHandler(pool *balancer.AccountPool, store *requestdb.Store) gin.HandlerFunc

GeminiRouterHandler 处理 Gemini 原生 models/{model}:{method},生成、流式生成、计数与视频长时操作之外的方法返回 404

func ImageEditHandler

func ImageEditHandler(pool *balancer.AccountPool) gin.HandlerFunc

ImageEditHandler 处理 OpenAI 图片编辑

func ImageGenerationHandler

func ImageGenerationHandler(pool *balancer.AccountPool) gin.HandlerFunc

ImageGenerationHandler 处理 OpenAI 图片生成

func ListModelsHandler

func ListModelsHandler(pool *balancer.AccountPool) gin.HandlerFunc

ListModelsHandler 返回 OpenAI 模型目录,带 anthropic-version 头时返回 Anthropic 格式

func LoopbackHost added in v0.1.3

func LoopbackHost(host string) bool

LoopbackHost 报告 Host 或 Origin 主机名为 localhost 或回环地址

func RequestMiddleware added in v0.1.3

func RequestMiddleware(store *requestdb.Store) gin.HandlerFunc

RequestMiddleware 为公开 API 请求分配 ID、跟踪生成请求的阶段,结束时输出结果并把生成请求写入账本

func ResponseCancelHandler added in v0.1.3

func ResponseCancelHandler(store *requestdb.Store) gin.HandlerFunc

ResponseCancelHandler 取消后台响应:进行中的响应停止生成并标为 cancelled,已结束的响应原样返回

func ResponseDeleteHandler added in v0.1.3

func ResponseDeleteHandler(store *requestdb.Store) gin.HandlerFunc

ResponseDeleteHandler 删除保存的响应,进行中的后台响应同时停止

func ResponseInputItemsHandler added in v0.1.3

func ResponseInputItemsHandler(store *requestdb.Store) gin.HandlerFunc

ResponseInputItemsHandler 按 after、before、limit 与 order 分页返回生成该响应时的输入项

func ResponseRetrieveHandler added in v0.1.3

func ResponseRetrieveHandler(store *requestdb.Store) gin.HandlerFunc

ResponseRetrieveHandler 返回保存的响应对象;stream=true 时等后台响应结束,再按创建时的流式事件回放,starting_after 之前的事件不输出

func ResponsesCompactHandler added in v0.1.3

func ResponsesCompactHandler(pool *balancer.AccountPool, store *requestdb.Store) gin.HandlerFunc

ResponsesCompactHandler 处理 /v1/responses/compact:较早的条目由模型摘要,最近的条目原样保留,返回保留的 user 消息与一个 compaction 项

func ResponsesHandler

func ResponsesHandler(pool *balancer.AccountPool, store *requestdb.Store) gin.HandlerFunc

ResponsesHandler 处理 OpenAI Responses API,store 不为 false 的响应写入账本库,background 为 true 时立即返回 queued

func ResponsesInputTokensHandler added in v0.1.3

func ResponsesInputTokensHandler(pool *balancer.AccountPool) gin.HandlerFunc

ResponsesInputTokensHandler 以与生成相同的提示词编码计算 Responses 请求的输入 token,续接时计入会话已有上下文

func RetrieveModelHandler added in v0.1.3

func RetrieveModelHandler(pool *balancer.AccountPool) gin.HandlerFunc

RetrieveModelHandler 返回单个模型,带 anthropic-version 头时返回 Anthropic 格式

func SpeechHandler added in v0.1.3

func SpeechHandler(pool *balancer.AccountPool) gin.HandlerFunc

SpeechHandler 处理 OpenAI /v1/audio/speech:以官网回答菜单的 Listen 朗读合成音频,model 选择执行朗读的账号

func TranscriptionHandler added in v0.1.3

func TranscriptionHandler(pool *balancer.AccountPool) gin.HandlerFunc

TranscriptionHandler 处理 OpenAI /v1/audio/transcriptions

func TranslationHandler added in v0.1.3

func TranslationHandler(pool *balancer.AccountPool) gin.HandlerFunc

TranslationHandler 处理 OpenAI /v1/audio/translations:把音频中的语音翻译为英文

func UnavailableOpenAIHandler added in v0.1.3

func UnavailableOpenAIHandler(protocol string, capability string) gin.HandlerFunc

UnavailableOpenAIHandler 处理 Gemini Web 没有对应能力的 OpenAI 端点,按官方模型不可用错误返回 404 model_not_found

func VideoContentHandler added in v0.1.3

func VideoContentHandler(store *requestdb.Store) gin.HandlerFunc

VideoContentHandler 返回已完成视频的 MP4 内容

func VideoCreateHandler added in v0.1.3

func VideoCreateHandler(pool *balancer.AccountPool, store *requestdb.Store) gin.HandlerFunc

VideoCreateHandler 处理 OpenAI POST /v1/videos:立即返回 queued 视频对象,由官网 Create video 在后台生成

func VideoDeleteHandler added in v0.1.3

func VideoDeleteHandler(store *requestdb.Store) gin.HandlerFunc

VideoDeleteHandler 删除视频任务与成品,进行中的生成同时停止

func VideoListHandler added in v0.1.3

func VideoListHandler(store *requestdb.Store) gin.HandlerFunc

VideoListHandler 按 after、limit 与 order 分页列出 OpenAI 视频对象

func VideoRetrieveHandler added in v0.1.3

func VideoRetrieveHandler(store *requestdb.Store) gin.HandlerFunc

VideoRetrieveHandler 返回 OpenAI 视频对象

func WriteGeminiNotFound added in v0.1.3

func WriteGeminiNotFound(c *gin.Context)

WriteGeminiNotFound 以 Google 错误体返回未知路由

func WriteOpenAINotFound added in v0.1.3

func WriteOpenAINotFound(c *gin.Context)

WriteOpenAINotFound 以 OpenAI 错误体返回未知路由

Types

type CandidateOutput

type CandidateOutput struct {
	Text      string
	Thought   string
	Codes     []CodeOutput
	Citations []gemini.Citation
	Media     []gemini.Media
}

CandidateOutput 保存单个候选项的权威累计输出

type CatalogModel added in v0.1.3

type CatalogModel struct {
	gemini.Model
	MinInputTokenLimit int
	MaxInputTokenLimit int
	AccountCount       int
	// Accounts 为提供该模型的已就绪账号
	Accounts []string
	// AliasOf 为 MODEL_MAPPING 别名指向的模型 ID
	AliasOf string
}

CatalogModel 是公开目录中的一个模型及其在账号间的上下文窗口范围

func Catalog added in v0.1.3

func Catalog(pool *balancer.AccountPool) []CatalogModel

Catalog 返回公开模型目录

type ChatAudioOptions added in v0.1.3

type ChatAudioOptions struct {
	Format string `json:"format"`
	Voice  string `json:"voice"`
}

ChatAudioOptions 表示 OpenAI Chat 的音频输出参数

type ChatMessage

type ChatMessage struct {
	Role                string            `json:"role"`
	Content             json.RawMessage   `json:"content"`
	Name                string            `json:"name,omitempty"`
	ToolCallID          string            `json:"tool_call_id,omitempty"`
	ToolCalls           []OpenAIToolCall  `json:"tool_calls,omitempty"`
	ReasoningContent    json.RawMessage   `json:"reasoning_content,omitempty"`
	TranscriptReasoning json.RawMessage   `json:"-"`
	TranscriptItems     []json.RawMessage `json:"-"`
}

ChatMessage 表示 OpenAI Chat 消息

type ChatRequest

type ChatRequest struct {
	Messages            []ChatMessage      `json:"messages"`
	Stream              bool               `json:"stream"`
	StreamOptions       *ChatStreamOptions `json:"stream_options,omitempty"`
	Model               string             `json:"model"`
	Tools               []OpenAITool       `json:"tools,omitempty"`
	ToolChoice          json.RawMessage    `json:"tool_choice,omitempty"`
	ResponseFormat      json.RawMessage    `json:"response_format,omitempty"`
	ReasoningEffort     string             `json:"reasoning_effort,omitempty"`
	Reasoning           json.RawMessage    `json:"reasoning,omitempty"`
	ConversationID      string             `json:"conversation_id,omitempty"`
	PreviousResponseID  string             `json:"previous_response_id,omitempty"`
	MaxTokens           *int               `json:"max_tokens,omitempty"`
	MaxCompletionTokens *int               `json:"max_completion_tokens,omitempty"`
	Temperature         *float64           `json:"temperature,omitempty"`
	TopP                *float64           `json:"top_p,omitempty"`
	Stop                json.RawMessage    `json:"stop,omitempty"`
	PresencePenalty     *float64           `json:"presence_penalty,omitempty"`
	FrequencyPenalty    *float64           `json:"frequency_penalty,omitempty"`
	Seed                *int               `json:"seed,omitempty"`
	N                   *int               `json:"n,omitempty"`
	WebSearchOptions    json.RawMessage    `json:"web_search_options,omitempty"`
	Audio               *ChatAudioOptions  `json:"audio,omitempty"`
}

ChatRequest 表示 OpenAI Chat 请求

type ChatStreamOptions

type ChatStreamOptions struct {
	IncludeUsage bool `json:"include_usage"`
}

ChatStreamOptions 表示 OpenAI Chat 流选项

type CodeOutput

type CodeOutput struct {
	Index    int
	Type     gemini.CodeEventType
	Language string
	Offset   int
	Complete bool
	Content  string
	Order    int
}

CodeOutput 保存一段 Google 已执行代码的权威快照

type CompletionRequest added in v0.1.3

type CompletionRequest struct {
	Model            string             `json:"model"`
	Prompt           json.RawMessage    `json:"prompt"`
	Suffix           string             `json:"suffix"`
	Stream           bool               `json:"stream"`
	StreamOptions    *ChatStreamOptions `json:"stream_options"`
	Echo             bool               `json:"echo"`
	N                *int               `json:"n"`
	BestOf           *int               `json:"best_of"`
	MaxTokens        *int               `json:"max_tokens"`
	Temperature      *float64           `json:"temperature"`
	TopP             *float64           `json:"top_p"`
	Stop             json.RawMessage    `json:"stop"`
	PresencePenalty  *float64           `json:"presence_penalty"`
	FrequencyPenalty *float64           `json:"frequency_penalty"`
	Logprobs         *int               `json:"logprobs"`
	LogitBias        json.RawMessage    `json:"logit_bias"`
	Seed             *int               `json:"seed"`
}

CompletionRequest 表示 OpenAI 旧版 Completions 请求

type EventAccumulator

type EventAccumulator struct {
	Candidates   map[int]*CandidateOutput
	Session      gemini.ConversationSnapshot
	Metadata     *gemini.EventMetadataData
	Usage        *gemini.Usage
	FinishReason gemini.FinishReason
}

EventAccumulator 将规范事件还原为最终响应

func NewEventAccumulator

func NewEventAccumulator() *EventAccumulator

NewEventAccumulator 创建规范事件累加器

func (*EventAccumulator) Apply

func (a *EventAccumulator) Apply(event gemini.Event) error

Apply 应用一个规范事件

func (*EventAccumulator) Primary

func (a *EventAccumulator) Primary() CandidateOutput

Primary 返回首个候选项

type GeminiCodeExecutionResult

type GeminiCodeExecutionResult struct {
	ID      string `json:"id,omitempty"`
	Outcome string `json:"outcome"`
	Output  string `json:"output"`
}

GeminiCodeExecutionResult 表示 Gemini 代码执行结果

type GeminiContent

type GeminiContent struct {
	Role  string       `json:"role,omitempty"`
	Parts []GeminiPart `json:"parts"`
}

GeminiContent 表示 Gemini 消息

type GeminiExecutableCode

type GeminiExecutableCode struct {
	ID       string `json:"id,omitempty"`
	Language string `json:"language"`
	Code     string `json:"code"`
}

GeminiExecutableCode 表示 Gemini 已执行代码

type GeminiFileData

type GeminiFileData struct {
	MimeType string `json:"mimeType"`
	FileURI  string `json:"fileUri"`
}

GeminiFileData 表示 Gemini 远程文件

type GeminiFunctionCall

type GeminiFunctionCall struct {
	Name string                 `json:"name"`
	ID   string                 `json:"id,omitempty"`
	Args map[string]interface{} `json:"args"`
}

GeminiFunctionCall 表示 Gemini 函数调用

type GeminiFunctionResponse

type GeminiFunctionResponse struct {
	ID       string                 `json:"id,omitempty"`
	Name     string                 `json:"name"`
	Response map[string]interface{} `json:"response"`
}

GeminiFunctionResponse 表示 Gemini 函数结果

type GeminiGenerateContentRequest

type GeminiGenerateContentRequest struct {
	Contents           []GeminiContent `json:"contents"`
	SystemInstruction  *GeminiContent  `json:"systemInstruction,omitempty"`
	GenerationConfig   json.RawMessage `json:"generationConfig,omitempty"`
	Tools              json.RawMessage `json:"tools,omitempty"`
	ToolConfig         json.RawMessage `json:"toolConfig,omitempty"`
	ConversationID     string          `json:"conversation_id,omitempty"`
	PreviousResponseID string          `json:"previous_response_id,omitempty"`
	// GenerateContentRequest 为 countTokens 请求包装的完整生成请求
	GenerateContentRequest *GeminiGenerateContentRequest `json:"generateContentRequest,omitempty"`
}

GeminiGenerateContentRequest 表示 Gemini 生成请求

type GeminiInlineData

type GeminiInlineData struct {
	MimeType string `json:"mimeType"`
	Data     string `json:"data"`
}

GeminiInlineData 表示 Gemini 内联文件

type GeminiPart

type GeminiPart struct {
	Text             string                     `json:"text,omitempty"`
	Thought          bool                       `json:"thought,omitempty"`
	ThoughtSignature string                     `json:"thoughtSignature,omitempty"`
	InlineData       *GeminiInlineData          `json:"inlineData,omitempty"`
	FileData         *GeminiFileData            `json:"fileData,omitempty"`
	FunctionCall     *GeminiFunctionCall        `json:"functionCall,omitempty"`
	FunctionResponse *GeminiFunctionResponse    `json:"functionResponse,omitempty"`
	ExecutableCode   *GeminiExecutableCode      `json:"executableCode,omitempty"`
	CodeResult       *GeminiCodeExecutionResult `json:"codeExecutionResult,omitempty"`
}

GeminiPart 表示 Gemini 内容段

type ImageGenerationRequest

type ImageGenerationRequest struct {
	Prompt            string `json:"prompt"`
	Model             string `json:"model,omitempty"`
	N                 int    `json:"n,omitempty"`
	Size              string `json:"size,omitempty"`
	ResponseFormat    string `json:"response_format,omitempty"`
	Quality           string `json:"quality,omitempty"`
	Style             string `json:"style,omitempty"`
	Stream            bool   `json:"stream,omitempty"`
	PartialImages     *int   `json:"partial_images,omitempty"`
	OutputFormat      string `json:"output_format,omitempty"`
	OutputCompression *int   `json:"output_compression,omitempty"`
	Background        string `json:"background,omitempty"`
	User              string `json:"user,omitempty"`
	InputFidelity     string `json:"input_fidelity,omitempty"`
	Moderation        string `json:"moderation,omitempty"`
}

ImageGenerationRequest 表示 OpenAI 图片生成请求

type OpenAITool

type OpenAITool struct {
	Type     string `json:"type"`
	Function struct {
		Name        string          `json:"name"`
		Description string          `json:"description,omitempty"`
		Parameters  json.RawMessage `json:"parameters"`
	} `json:"function"`
}

OpenAITool 表示 OpenAI 函数定义

type OpenAIToolCall

type OpenAIToolCall struct {
	ID       string `json:"id"`
	Type     string `json:"type"`
	Function struct {
		Name      string `json:"name"`
		Arguments string `json:"arguments"`
	} `json:"function"`
}

OpenAIToolCall 表示 OpenAI 函数调用

type ResponsesRequest

type ResponsesRequest struct {
	Model              string          `json:"model"`
	Input              json.RawMessage `json:"input"`
	Instructions       string          `json:"instructions,omitempty"`
	Stream             bool            `json:"stream"`
	Tools              []ResponsesTool `json:"tools,omitempty"`
	ToolChoice         json.RawMessage `json:"tool_choice,omitempty"`
	Text               json.RawMessage `json:"text,omitempty"`
	Conversation       json.RawMessage `json:"conversation,omitempty"`
	PreviousResponseID string          `json:"previous_response_id,omitempty"`
	MaxOutputTokens    *int            `json:"max_output_tokens,omitempty"`
	Temperature        *float64        `json:"temperature,omitempty"`
	TopP               *float64        `json:"top_p,omitempty"`
	Reasoning          json.RawMessage `json:"reasoning,omitempty"`
	Truncation         string          `json:"truncation,omitempty"`
	ParallelToolCalls  *bool           `json:"parallel_tool_calls,omitempty"`
	Store              *bool           `json:"store,omitempty"`
	Background         bool            `json:"background,omitempty"`
	Metadata           json.RawMessage `json:"metadata,omitempty"`
}

ResponsesRequest 表示 OpenAI Responses 请求

type ResponsesTool

type ResponsesTool struct {
	Type              string          `json:"type"`
	Name              string          `json:"name"`
	Description       string          `json:"description,omitempty"`
	Parameters        json.RawMessage `json:"parameters"`
	Action            string          `json:"action,omitempty"`
	Background        string          `json:"background,omitempty"`
	InputFidelity     string          `json:"input_fidelity,omitempty"`
	InputImageMask    json.RawMessage `json:"input_image_mask,omitempty"`
	ImageModel        string          `json:"model,omitempty"`
	Moderation        string          `json:"moderation,omitempty"`
	OutputCompression *int            `json:"output_compression,omitempty"`
	OutputFormat      string          `json:"output_format,omitempty"`
	PartialImages     *int            `json:"partial_images,omitempty"`
	Quality           string          `json:"quality,omitempty"`
	Size              string          `json:"size,omitempty"`
	Tools             []ResponsesTool `json:"tools,omitempty"`
}

ResponsesTool 表示 Responses API 函数工具

type ToolBridge

type ToolBridge struct {
	Definitions            []ToolDefinition
	Choice                 string
	JSONSchema             json.RawMessage
	CodeExecution          bool
	WebSearch              bool
	ImageGeneration        bool
	RequireCodeExecution   bool
	RequireWebSearch       bool
	RequireImageGeneration bool
	// AspectRatio 为生图请求的宽高比
	AspectRatio string
}

ToolBridge 将函数工具和结构化输出转换为严格文本契约

func (ToolBridge) Parse

func (b ToolBridge) Parse(text string) (string, []ToolCall, error)

Parse 解析严格工具调用并验证结构化输出

func (ToolBridge) PromptSuffix

func (b ToolBridge) PromptSuffix() (string, error)

PromptSuffix 返回追加到用户提示词的协议约束

type ToolCall

type ToolCall struct {
	ID        string          `json:"id,omitempty"`
	Name      string          `json:"name"`
	Arguments json.RawMessage `json:"arguments"`
}

ToolCall 描述模型选择执行的函数工具

type ToolDefinition

type ToolDefinition struct {
	Name        string          `json:"name"`
	Description string          `json:"description,omitempty"`
	Parameters  json.RawMessage `json:"parameters"`
	Namespace   string          `json:"-"`
}

ToolDefinition 描述公开协议中的函数工具

Jump to

Keyboard shortcuts

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