sync update from LAN repo (source dhk-1 @ 3b1ec5b4)
This commit is contained in:
1 parent
da6521b6cb
commit
264e2200d6
995 files changed
+148105
-13399
No files matched your search
+41
-8
@@ -2,7 +2,7 @@
|
||||
|
||||
from __future__ import annotations
|
||||
|
||||
from functools import lru_cache
|
||||
import asyncio
|
||||
|
||||
import httpx
|
||||
|
||||
@@ -53,16 +53,49 @@ def build_weknora_client(
|
||||
)
|
||||
|
||||
|
||||
@lru_cache(maxsize=4)
|
||||
def _shared_weknora_client(
|
||||
base_url: str,
|
||||
admin_email: str,
|
||||
admin_password: str,
|
||||
) -> WeKnoraClient:
|
||||
"""Reuse auth state for equivalent runtime configurations."""
|
||||
"""Reuse auth state only inside the current event loop.
|
||||
|
||||
return WeKnoraClient(
|
||||
base_url=base_url,
|
||||
admin_email=admin_email,
|
||||
admin_password=admin_password,
|
||||
)
|
||||
``WeKnoraClient`` owns asyncio primitives (an authentication lock and a
|
||||
cached directory task). A process-wide ``lru_cache`` therefore lets a
|
||||
client first used by the Gateway loop escape into a LangGraph/tool loop.
|
||||
Once either primitive has been awaited, reusing it from the other loop can
|
||||
fail with ``... is bound to a different event loop``. Smart retrieval
|
||||
used to normalize that internal exception to ``PROVIDER_UNAVAILABLE``, so
|
||||
an otherwise healthy selected knowledge base appeared empty.
|
||||
|
||||
Store the small configuration cache on the loop itself. The loop owns the
|
||||
clients for exactly its lifetime, equivalent configurations still share
|
||||
their bearer token and knowledge-base directory within that loop, and no
|
||||
async primitive crosses loop boundaries. Synchronous callers have no
|
||||
safe loop ownership yet, so they receive an uncached client.
|
||||
"""
|
||||
|
||||
try:
|
||||
loop = asyncio.get_running_loop()
|
||||
except RuntimeError:
|
||||
return WeKnoraClient(
|
||||
base_url=base_url,
|
||||
admin_email=admin_email,
|
||||
admin_password=admin_password,
|
||||
)
|
||||
|
||||
cache_name = "_deerflow_weknora_clients"
|
||||
cache = getattr(loop, cache_name, None)
|
||||
if not isinstance(cache, dict):
|
||||
cache = {}
|
||||
setattr(loop, cache_name, cache)
|
||||
key = (base_url, admin_email, admin_password)
|
||||
client = cache.get(key)
|
||||
if client is None:
|
||||
client = WeKnoraClient(
|
||||
base_url=base_url,
|
||||
admin_email=admin_email,
|
||||
admin_password=admin_password,
|
||||
)
|
||||
cache[key] = client
|
||||
return client
|
||||
Reference in new issue
Block a user