From 14f1009ddc7c467bdc1f1a18ed98130b026f227a Mon Sep 17 00:00:00 2001 From: Liang Jiaqing Date: Sun, 8 Mar 2026 10:24:54 +0800 Subject: [PATCH] fix: unified auto_make_url for all API base_url formats, closes #12 --- launch.pyw | 2 +- sidercall.py | 19 ++++++++----------- 2 files changed, 9 insertions(+), 12 deletions(-) diff --git a/launch.pyw b/launch.pyw index 6850e46..272c0d3 100644 --- a/launch.pyw +++ b/launch.pyw @@ -1,6 +1,6 @@ import webview, threading, subprocess, sys, time, os, ctypes, atexit, socket, random -WINDOW_WIDTH, WINDOW_HEIGHT, RIGHT_PADDING, TOP_PADDING = 600, 900, 0, 300 +WINDOW_WIDTH, WINDOW_HEIGHT, RIGHT_PADDING, TOP_PADDING = 600, 900, 0, 100 def find_free_port(lo=18501, hi=18599): ports = list(range(lo, hi+1)); random.shuffle(ports) diff --git a/sidercall.py b/sidercall.py index 94ede5e..942dbe8 100644 --- a/sidercall.py +++ b/sidercall.py @@ -27,6 +27,11 @@ def compress_history_tags(messages, keep_recent=4, max_len=200): ) return messages +def auto_make_url(base, path): + b, p = base.rstrip('/'), path.strip('/') + if b.endswith('$'): return b[:-1].rstrip('/') + return b if b.endswith(p) else f"{b}/{p}" if re.search(r'/v\d+$', b) else f"{b}/v1/{p}" + class SiderLLMSession: def __init__(self, sider_cookie, default_model="gemini-3.0-flash"): from sider_ai_api import Session # 不使用sider的话没必要安装这个包 @@ -61,7 +66,7 @@ class ClaudeSession: headers = {"x-api-key": self.api_key, "Content-Type": "application/json", "anthropic-version": "2023-06-01"} payload = {"model": model, "messages": messages, "temperature": temperature, "max_tokens": max_tokens, "stream": True} try: - with requests.post(f"{self.api_base}/v1/messages", headers=headers, json=payload, stream=True, timeout=(5,30)) as r: + with requests.post(auto_make_url(self.api_base, "messages"), headers=headers, json=payload, stream=True, timeout=(5,30)) as r: r.raise_for_status() for line in r.iter_lines(): if not line: continue @@ -104,14 +109,6 @@ class LLMSession: if mode in ["responses", "response"]: self.api_mode = "responses" else: self.api_mode = "chat_completions" - def _endpoint(self, path): - # 处理火山引擎API,它已经包含完整路径 - if 'ark.cn-beijing.volces.com' in self.api_base or 'ark.cn-shanghai.volces.com' in self.api_base: - return f"{self.api_base}/{path.lstrip('/')}" - if self.api_base.endswith('/v1'): return f"{self.api_base}/{path.lstrip('/')}" - if self.api_base.endswith('$'): return f"{self.api_base.rstrip('$')}/{path.lstrip('/')}" - return f"{self.api_base}/v1/{path.lstrip('/')}" - def _retry_delay(self, resp, attempt): retry_after = None try: @@ -152,10 +149,10 @@ class LLMSession: if model is None: model = self.default_model headers = {"Authorization": f"Bearer {self.api_key}", "Content-Type": "application/json", "Accept": "text/event-stream"} if self.api_mode == "responses": - url = self._endpoint("responses") + url = auto_make_url(self.api_base, "responses") payload = {"model": model, "input": self._to_responses_input(messages), "temperature": temperature, "stream": True} else: - url = self._endpoint("chat/completions") + url = auto_make_url(self.api_base, "chat/completions") payload = {"model": model, "messages": messages, "temperature": temperature, "stream": True} for attempt in range(self.max_retries + 1): streamed_any = False