fix(agent): Agent 对话 - 修复无效 URL 与上游取消日志

This commit is contained in:
ddCat
2026-08-19 11:36:41 +08:00
parent c7452b701d
commit de00e211b3
4 changed files with 62 additions and 6 deletions

View File

@@ -17,7 +17,8 @@ export function channelRequest(config: RelayConfig, upstreamUrl: string, headers
return { url: upstreamUrl, headers: Object.fromEntries(normalizedHeaders.entries()), credentials: "include" };
}
const normalizedUpstreamUrl = new URL(upstreamUrl).toString();
const normalizedBaseUrl = requireHttpUrl(runtimeConfig.baseUrl, "当前模型渠道 Base URL");
const normalizedUpstreamUrl = requireHttpUrl(upstreamUrl, "当前模型请求地址");
normalizedHeaders.delete("X-Canvas-Upstream-Headers");
normalizedHeaders.delete("x-goog-api-key");
normalizedHeaders.set("Authorization", `Bearer ${runtimeConfig.apiKey}`);
@@ -25,7 +26,7 @@ export function channelRequest(config: RelayConfig, upstreamUrl: string, headers
normalizedHeaders.set("X-Canvas-Upstream-Format", runtimeConfig.apiFormat === "gemini" ? "gemini" : "openai");
if (runtimeConfig.allowLocalChannel === true) {
normalizedHeaders.set("X-Canvas-Allow-Local-Channel", "1");
normalizedHeaders.set("X-Canvas-Upstream-Base-URL", new URL(runtimeConfig.baseUrl).toString());
normalizedHeaders.set("X-Canvas-Upstream-Base-URL", normalizedBaseUrl);
} else {
normalizedHeaders.delete("X-Canvas-Allow-Local-Channel");
normalizedHeaders.delete("X-Canvas-Upstream-Base-URL");
@@ -38,6 +39,20 @@ export function channelRequest(config: RelayConfig, upstreamUrl: string, headers
};
}
function requireHttpUrl(value: string, label: string) {
const normalized = value.trim();
let parsed: URL;
try {
parsed = new URL(normalized);
} catch {
throw new Error(`${label} 无效,请填写完整地址,例如:https://api.example.com/v1`);
}
if (!parsed.hostname || (parsed.protocol !== "http:" && parsed.protocol !== "https:")) {
throw new Error(`${label} 无效,请填写完整地址,例如:https://api.example.com/v1`);
}
return parsed.toString();
}
function encodeChannelHeaders(headers: ChannelHeader[]) {
const bytes = new TextEncoder().encode(JSON.stringify(headers));
let binary = "";