Skip to content

Commit 14fa7ab

Browse files
committed
✨ 新增智谱(Zhipu)AI Provider 支持
1 parent 9666d0e commit 14fa7ab

4 files changed

Lines changed: 52 additions & 29 deletions

File tree

src/app/service/agent/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ export type ChatRequest = {
167167
export type AgentModelConfig = {
168168
id: string; // 唯一标识
169169
name: string; // 用户自定义名称(如 "GPT-4o", "Claude Sonnet")
170-
provider: "openai" | "anthropic";
170+
provider: "openai" | "anthropic" | "zhipu";
171171
apiBaseUrl: string;
172172
apiKey: string;
173173
model: string;

src/app/service/service_worker/agent.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2329,7 +2329,13 @@ export class AgentService {
23292329
const { url, init } =
23302330
model.provider === "anthropic"
23312331
? buildAnthropicRequest(model, chatRequest, attachmentResolver)
2332-
: buildOpenAIRequest(model, chatRequest, attachmentResolver);
2332+
: buildOpenAIRequest(
2333+
model.provider === "zhipu"
2334+
? { ...model, apiBaseUrl: model.apiBaseUrl || "https://open.bigmodel.cn/api/paas/v4" }
2335+
: model,
2336+
chatRequest,
2337+
attachmentResolver
2338+
);
23332339

23342340
// 带重试的 LLM 调用,最多重试 5 次,间隔递增:10s, 10s, 20s, 20s, 30s
23352341
const RETRY_DELAYS = [10_000, 10_000, 20_000, 20_000, 30_000];

src/pages/options/routes/AgentChat/model_utils.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ export function detectProvider(model: AgentModelConfig): ProviderInfo {
7575
if (model.provider === "anthropic") {
7676
return { key: "anthropic", label: "Anthropic", order: 2 };
7777
}
78+
if (model.provider === "zhipu") {
79+
return { key: "zhipu", label: "Zhipu", order: 12 };
80+
}
7881
// 默认归入 OpenAI Compatible
7982
return { key: "other", label: "Other", order: 99 };
8083
}

src/pages/options/routes/AgentProvider.tsx

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,19 @@ function AgentProvider() {
338338
loadData();
339339
};
340340

341+
const getDefaultBaseUrl = (provider: string) => {
342+
switch (provider) {
343+
case "anthropic":
344+
return "https://api.anthropic.com";
345+
case "zhipu":
346+
return "https://open.bigmodel.cn/api/paas/v4";
347+
default:
348+
return "https://api.openai.com/v1";
349+
}
350+
};
351+
341352
const buildProviderRequest = (m: AgentModelConfig) => {
342-
const baseUrl =
343-
m.apiBaseUrl || (m.provider === "openai" ? "https://api.openai.com/v1" : "https://api.anthropic.com");
353+
const baseUrl = m.apiBaseUrl || getDefaultBaseUrl(m.provider);
344354
const headers: Record<string, string> = {};
345355
let modelsUrl: string;
346356
if (m.provider === "anthropic") {
@@ -361,9 +371,7 @@ function AgentProvider() {
361371
setTestingConnection(true);
362372
setTestReply("");
363373
try {
364-
const baseUrl =
365-
editingModel.apiBaseUrl ||
366-
(editingModel.provider === "openai" ? "https://api.openai.com/v1" : "https://api.anthropic.com");
374+
const baseUrl = editingModel.apiBaseUrl || getDefaultBaseUrl(editingModel.provider);
367375
const headers: Record<string, string> = { "Content-Type": "application/json" };
368376
let chatUrl: string;
369377
let body: string;
@@ -383,8 +391,9 @@ function AgentProvider() {
383391
if (editingModel.apiKey) {
384392
headers["Authorization"] = `Bearer ${editingModel.apiKey}`;
385393
}
394+
const defaultModel = editingModel.provider === "zhipu" ? "glm-4-flash" : "gpt-4o-mini";
386395
body = JSON.stringify({
387-
model: editingModel.model || "gpt-4o-mini",
396+
model: editingModel.model || defaultModel,
388397
max_tokens: 256,
389398
messages: [{ role: "user", content: "hi" }],
390399
});
@@ -490,25 +499,32 @@ function AgentProvider() {
490499
<Select
491500
value={editingModel.provider}
492501
onChange={(value) => setEditingModel((prev) => ({ ...prev, provider: value }))}
493-
renderFormat={(_option, value) => (
494-
<span className="tw-inline-flex tw-items-center tw-gap-2">
495-
<ProviderIcon providerKey={String(value)} size={14} />
496-
<span>{value === "anthropic" ? "Anthropic" : "OpenAI"}</span>
497-
</span>
498-
)}
502+
renderFormat={(_option, value) => {
503+
const labels: Record<string, string> = {
504+
openai: "OpenAI",
505+
anthropic: "Anthropic",
506+
zhipu: "Zhipu (智谱)",
507+
};
508+
return (
509+
<span className="tw-inline-flex tw-items-center tw-gap-2">
510+
<ProviderIcon providerKey={String(value)} size={14} />
511+
<span>{labels[String(value)] || String(value)}</span>
512+
</span>
513+
);
514+
}}
499515
>
500-
<Select.Option value="openai">
501-
<span className="tw-inline-flex tw-items-center tw-gap-2">
502-
<ProviderIcon providerKey="openai" size={14} />
503-
<span>{"OpenAI"}</span>
504-
</span>
505-
</Select.Option>
506-
<Select.Option value="anthropic">
507-
<span className="tw-inline-flex tw-items-center tw-gap-2">
508-
<ProviderIcon providerKey="anthropic" size={14} />
509-
<span>{"Anthropic"}</span>
510-
</span>
511-
</Select.Option>
516+
{[
517+
{ value: "openai", label: "OpenAI" },
518+
{ value: "anthropic", label: "Anthropic" },
519+
{ value: "zhipu", label: "Zhipu (智谱)" },
520+
].map((item) => (
521+
<Select.Option key={item.value} value={item.value}>
522+
<span className="tw-inline-flex tw-items-center tw-gap-2">
523+
<ProviderIcon providerKey={item.value} size={14} />
524+
<span>{item.label}</span>
525+
</span>
526+
</Select.Option>
527+
))}
512528
</Select>
513529
</div>
514530

@@ -519,9 +535,7 @@ function AgentProvider() {
519535
</div>
520536
<Input
521537
value={editingModel.apiBaseUrl}
522-
placeholder={
523-
editingModel.provider === "openai" ? "https://api.openai.com/v1" : "https://api.anthropic.com"
524-
}
538+
placeholder={getDefaultBaseUrl(editingModel.provider)}
525539
onChange={(value) => setEditingModel((prev) => ({ ...prev, apiBaseUrl: value }))}
526540
/>
527541
</div>

0 commit comments

Comments
 (0)