feat(llm): 支持原生 Google Gemini API 格式

- 新增 agent/gemini.go 原生 Gemini generateContent / streamGenerateContent?alt=sse llm.Provider,支持流式/非流式、工具调用与 JSON Schema 清洗、thoughtSignature 回传、thinkingConfig(Gemini 2.5 thinkingBudget / Gemini 3+ thinkingLevel)、建连/空响应重试及限流/抓包透传。
- agent.ConfigFrom / FromEnv / Provider() 支持 gemini 格式与 BaseURL 归一化(自动补全 /v1beta 并容忍粘贴完整端点 URL),环境变量支持 GEMINI_API_KEY / GOOGLE_API_KEY。
- db/schema.sql 放开 llm_profiles.format CHECK 约束加入 gemini 并幂等迁移旧库。
- server/server_mgmt.go 模型列表接口支持 Gemini /models(剥离 models/ 前缀并过滤支持 generateContent 的模型)。
- web LLM 配置页与类型/国际化新增 Google Gemini 协议选项及占位提示。
- 补充 agent/gemini_test.go、server/gemini_models_test.go 与限流重试测试。

Fixes #170
This commit is contained in:
ji ruo committed 2026-11-23 18:24:43 +08:00
1 parent 7b196bf683
commit bdd2da919c
13 files changed
+1810 -30

No files matched your search

+15 -6
View File
@@ -49,11 +49,12 @@ const MAX_TOKENS_FIELDS = (ui: UITranslate): { value: string; label: string }[]
{ value: NONE, label: ui("screen.af7202f4ddd9ce") },
{ value: "max_completion_tokens", label: "max_completion_tokens" },
];
// 另外两种格式各自定死了字段名,选项对它们无意义,说明文案里直接讲清楚。
// 另外几种格式各自定死了字段名,选项对它们无意义,说明文案里直接讲清楚。
const MAX_TOKENS_FIELD_HINTS = (ui: UITranslate): Record<string, string> => ({
openai: ui("screen.8565b2d7398535"),
anthropic: ui("screen.55138cdb37fe16"),
"openai-responses": ui("screen.82b05dbd905574"),
gemini: ui("llm.maxTokensFieldHint.gemini"),
});
const EFFORT_LEVELS = (ui: UITranslate): { value: string; label: string }[] => [
{ value: NONE, label: ui("screen.d703a9bcd8b626") },
@@ -325,7 +326,9 @@ function ProfileSheet({
const isNew = !profile;
const [name, setName] = React.useState("");
const [format, setFormat] = React.useState<"anthropic" | "openai" | "openai-responses" | "jev">("anthropic");
const [format, setFormat] = React.useState<"anthropic" | "openai" | "openai-responses" | "gemini" | "jev">(
"anthropic",
);
const [model, setModel] = React.useState("");
const [baseUrl, setBaseUrl] = React.useState("");
const [proxy, setProxy] = React.useState("");
@@ -355,7 +358,10 @@ function ProfileSheet({
if (!open) return;
setName(profile?.name ?? "");
setFormat(
profile?.format === "openai" || profile?.format === "openai-responses" || profile?.format === "jev"
profile?.format === "openai" ||
profile?.format === "openai-responses" ||
profile?.format === "gemini" ||
profile?.format === "jev"
? profile.format
: "anthropic",
);
@@ -509,7 +515,7 @@ function ProfileSheet({
<Select
value={format}
onValueChange={(v) => {
const next = v as "anthropic" | "openai" | "openai-responses" | "jev";
const next = v as "anthropic" | "openai" | "openai-responses" | "gemini" | "jev";
setFormat(next);
if (next === "jev" && !model.trim()) setModel("jev-latest");
}}
@@ -521,6 +527,7 @@ function ProfileSheet({
<SelectItem value="anthropic">Anthropic</SelectItem>
<SelectItem value="openai">OpenAI (Chat Completions)</SelectItem>
<SelectItem value="openai-responses">OpenAI (Responses API)</SelectItem>
<SelectItem value="gemini">Google Gemini</SelectItem>
<SelectItem value="jev">{ui("screen.1959e996efa50a")}</SelectItem>
</SelectContent>
</Select>
@@ -533,7 +540,7 @@ function ProfileSheet({
<Input
id="p-model"
className="font-mono"
placeholder="claude-opus-4-8"
placeholder={format === "gemini" ? "gemini-2.5-pro" : "claude-opus-4-8"}
value={model}
onChange={(e) => setModel(e.target.value)}
/>
@@ -585,7 +592,9 @@ function ProfileSheet({
<Input
id="p-base-url"
className="font-mono"
placeholder="https://api.openai.com/v1"
placeholder={
format === "gemini" ? "https://generativelanguage.googleapis.com/v1beta" : "https://api.openai.com/v1"
}
value={baseUrl}
onChange={(e) => setBaseUrl(e.target.value)}
/>
+4
View File
@@ -214,6 +214,10 @@ export const messages = {
],
"ui.ascending": ["正序", "ascending"],
"ui.descending": ["倒序", "descending"],
"llm.maxTokensFieldHint.gemini": [
"仅 openai 格式可选。Gemini 的字段名固定为 maxOutputTokens。",
"Only configurable for the openai format. Gemini uses maxOutputTokens.",
],
} satisfies Record<string, readonly [string, string]>;
const statuses: Record<string, readonly (readonly [string, string, string])[]> = {
+1 -1
View File
@@ -1154,7 +1154,7 @@ export interface NotificationDelivery {
export interface LLMProfile {
id: string;
name: string;
format: "openai" | "anthropic" | "openai-responses" | "jev";
format: "openai" | "anthropic" | "openai-responses" | "gemini" | "jev";
base_url?: string;
proxy?: string;
model: string;