Skip to content

Commit fef9822

Browse files
author
Taois
committed
fix: 打包后desktop不可用问题
1 parent 6ece296 commit fef9822

3 files changed

Lines changed: 20 additions & 4 deletions

File tree

astrbot/core/provider/sources/anthropic_source.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import anthropic
77
import httpx
88
from anthropic import AsyncAnthropic
9+
from anthropic._base_client import httpx as _anthropic_httpx
910
from anthropic.types import Message
1011
from anthropic.types.message_delta_usage import MessageDeltaUsage
1112
from anthropic.types.usage import Usage
@@ -113,6 +114,7 @@ def _create_http_client(self, provider_config: dict) -> httpx.AsyncClient:
113114
"Anthropic",
114115
provider_config.get("proxy", ""),
115116
headers=self.custom_headers,
117+
httpx_module=_anthropic_httpx,
116118
)
117119

118120
def _apply_thinking_config(self, payloads: dict) -> None:

astrbot/core/provider/sources/openai_source.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
import httpx
1616
from openai import AsyncAzureOpenAI, AsyncOpenAI
17+
from openai._base_client import httpx as _openai_httpx
1718
from openai._exceptions import NotFoundError
1819
from openai.lib.streaming.chat._completions import ChatCompletionStreamState
1920
from openai.types.chat.chat_completion import ChatCompletion
@@ -439,9 +440,13 @@ async def _fallback_to_text_only_and_retry(
439440
)
440441

441442
def _create_http_client(self, provider_config: dict) -> httpx.AsyncClient:
442-
"""创建带代理的 HTTP 客户端"""
443+
"""创建带代理的 HTTP 客户端
444+
445+
使用 openai 库内部引用的 httpx 模块来创建客户端实例,
446+
避免打包后 httpx 被重复收集导致 isinstance 校验失败。
447+
"""
443448
proxy = provider_config.get("proxy", "")
444-
return create_proxy_client("OpenAI", proxy)
449+
return create_proxy_client("OpenAI", proxy, httpx_module=_openai_httpx)
445450

446451
def __init__(self, provider_config, provider_settings) -> None:
447452
super().__init__(provider_config, provider_settings)

astrbot/core/utils/network_utils.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""Network error handling utilities for providers."""
22

33
import ssl
4+
import types
45

56
import httpx
67

@@ -89,6 +90,7 @@ def create_proxy_client(
8990
proxy: str | None = None,
9091
headers: dict[str, str] | None = None,
9192
verify: ssl.SSLContext | str | bool | None = None,
93+
httpx_module: types.ModuleType | None = None,
9294
) -> httpx.AsyncClient:
9395
"""Create an httpx AsyncClient with proxy configuration if provided.
9496
@@ -105,12 +107,19 @@ def create_proxy_client(
105107
headers: Optional custom headers to include in every request
106108
verify: Optional override for TLS verification. Defaults to the shared
107109
system SSL context when not provided.
110+
httpx_module: Optional httpx module to use for creating the client.
111+
In packaged environments (PyInstaller, Nuitka, etc.), the packaging
112+
tool may collect httpx as two separate copies. When the caller needs
113+
the created client to pass isinstance checks inside another library
114+
(e.g., openai), pass that library's own ``httpx`` module here so
115+
the client is created from the same class object.
108116
109117
Returns:
110118
An httpx.AsyncClient created with the shared system SSL context; the proxy is applied only if one is provided.
111119
"""
120+
_httpx = httpx_module or httpx
112121
resolved_verify = _SYSTEM_SSL_CTX if verify is None else verify
113122
if proxy:
114123
logger.info(f"[{provider_label}] 使用代理: {proxy}")
115-
return httpx.AsyncClient(proxy=proxy, verify=resolved_verify, headers=headers)
116-
return httpx.AsyncClient(verify=resolved_verify, headers=headers)
124+
return _httpx.AsyncClient(proxy=proxy, verify=resolved_verify, headers=headers)
125+
return _httpx.AsyncClient(verify=resolved_verify, headers=headers)

0 commit comments

Comments
 (0)