JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.
Python
feat(memory): 根据检视意见修改
zhouguohao5dd31bcc4b44
diff --git a/jiuwenswarm/agents/harness/common/rails/office_ace_user_profile/fetcher.py b/jiuwenswarm/agents/harness/common/rails/office_ace_user_profile/fetcher.pyindex 73a1bc9404..7a9c02a85e 100644--- a/jiuwenswarm/agents/harness/common/rails/office_ace_user_profile/fetcher.py+++ b/jiuwenswarm/agents/harness/common/rails/office_ace_user_profile/fetcher.py@@ -84,7 +84,7 @@ class UserProfileFetcher:config: UserProfileConfig,cache_dir: Optional[Path] = None,) -> None:- self._config = config+ self.config = configself._cache_dir = cache_dir or _default_cache_dir(config.user_id)# ------------------------------------------------------------------@@ -119,13 +119,13 @@ class UserProfileFetcher:return Noneheaders = {- "Authorization": f"Bearer {self._config.api_key}",+ "Authorization": f"Bearer {self.config.api_key}","Accept": "application/json",}# pc 端 appapi 用 X-Chat-User-Id 头传递用户身份;cloud 端 internal 用 path actor_id。if not self._is_cloud():- headers["X-Chat-User-Id"] = self._config.user_id- headers["Authorization"] = f"OfficeAceToken {self._config.api_key}"+ headers["X-Chat-User-Id"] = self.config.user_id+ headers["Authorization"] = f"OfficeAceToken {self.config.api_key}"url = self._build_url()last_err: Optional[str] = None@@ -136,7 +136,7 @@ class UserProfileFetcher:async with httpx.AsyncClient(trust_env=False,verify=False,- timeout=self._config.timeout_seconds,+ timeout=self.config.timeout_seconds,) as client:resp = await client.get(url, headers=headers)return self._handle_response(resp)@@ -160,7 +160,7 @@ class UserProfileFetcher:logger.info("[UserProfileFetcher] fetch failed after retries (user=%s): %s",- self._config.user_id,+ self.config.user_id,last_err,)return None@@ -175,12 +175,12 @@ class UserProfileFetcher:except OSError as exc:logger.debug("[UserProfileFetcher] read cache failed (user=%s): %s",- self._config.user_id,+ self.config.user_id,exc,)return None- def _is_cache_fresh(self) -> bool:+ def is_cache_fresh(self) -> bool:"""本地缓存是否新鲜(距上次写入 < fetch_interval)。读缓存文件 mtime(``cache_path.stat().st_mtime``)+ ``fetch_interval_minutes`` 判断。@@ -191,12 +191,12 @@ class UserProfileFetcher:except OSError as exc:logger.debug("[UserProfileFetcher] stat cache failed (user=%s): %s",- self._config.user_id,+ self.config.user_id,exc,)return Falseelapsed = time.time() - mtime- return elapsed < self._config.fetch_interval_minutes * 60+ return elapsed < self.config.fetch_interval_minutes * 60# ------------------------------------------------------------------# Internal@@ -206,15 +206,15 @@ class UserProfileFetcher:# cloud 端 internal 接口还需 space_id(path 参数)if self._is_cloud():return bool(- self._config.endpoint- and self._config.api_key- and self._config.user_id- and self._config.space_id,+ self.config.endpoint+ and self.config.api_key+ and self.config.user_id+ and self.config.space_id,)return bool(- self._config.endpoint- and self._config.api_key- and self._config.user_id,+ self.config.endpoint+ and self.config.api_key+ and self.config.user_id,)@staticmethod@@ -223,13 +223,13 @@ class UserProfileFetcher:return os.environ.get("OFFICE_ACE_DEPLOYMENT", "pc").strip().lower() == "cloud"def _build_url(self) -> str:- base = self._config.endpoint.rstrip("/")+ base = self.config.endpoint.rstrip("/")if self._is_cloud():# api-memory-access-internal.yaml:# GET /v1/core/internal/spaces/{space_id}/memory-user-profile/{actor_id}- actor_id = self._config.user_id+ actor_id = self.config.user_idreturn (- f"{base}/v1/core/internal/spaces/{self._config.space_id}"+ f"{base}/v1/core/internal/spaces/{self.config.space_id}"f"/memory-user-profile/{actor_id}")# chat-service-app-api-memory.yaml:GET /v1/appapi/memory/user-profile@@ -248,7 +248,7 @@ class UserProfileFetcher:logger.info("[UserProfileFetcher] business status %d (user=%s): %s",resp.status_code,- self._config.user_id,+ self.config.user_id,self._safe_body_snippet(resp),)return None@@ -257,7 +257,7 @@ class UserProfileFetcher:logger.info("[UserProfileFetcher] unexpected status %d (user=%s)",resp.status_code,- self._config.user_id,+ self.config.user_id,)return None@@ -273,14 +273,14 @@ class UserProfileFetcher:"[UserProfileFetcher] profile too large: %d bytes > %d (user=%s)",len(body_bytes),_MAX_CONTENT_BYTES,- self._config.user_id,+ self.config.user_id,)return Noneself._write_cache_atomic(body)logger.info("[UserProfileFetcher] fetched ok (user=%s, %d chars)",- self._config.user_id,+ self.config.user_id,len(body),)return body@@ -306,7 +306,7 @@ class UserProfileFetcher:if not isinstance(content, str) or not content.strip():logger.info("[UserProfileFetcher] empty/missing 'content' field (user=%s)",- self._config.user_id,+ self.config.user_id,)return Nonereturn content@@ -319,7 +319,7 @@ class UserProfileFetcher:except OSError as exc:logger.warning("[UserProfileFetcher] write cache failed (user=%s): %s",- self._config.user_id,+ self.config.user_id,exc,)diff --git a/jiuwenswarm/agents/harness/common/rails/office_ace_user_profile/office_ace_user_profile_rail.py b/jiuwenswarm/agents/harness/common/rails/office_ace_user_profile/office_ace_user_profile_rail.pyindex 3314015115..73a9b4f86e 100644--- a/jiuwenswarm/agents/harness/common/rails/office_ace_user_profile/office_ace_user_profile_rail.py+++ b/jiuwenswarm/agents/harness/common/rails/office_ace_user_profile/office_ace_user_profile_rail.py@@ -92,25 +92,25 @@ class OfficeAceUserProfileRail(DeepAgentRail):# 缓存不存在或已过期才同步触发一次拉取(带超时,不阻塞对话)。# 拉取失败/业务态无内容时降级用过期缓存(可能略旧)而非清空,零阻断。content = self._fetcher.read_cache()- if content is None or not self._fetcher._is_cache_fresh(): # noqa: SLF001+ if content is None or not self._fetcher.is_cache_fresh():stale = content # 旧缓存(可能为 None),fetch 失败时降级用try:content = await asyncio.wait_for(self._fetcher.fetch_once(),- timeout=self._fetcher._config.timeout_seconds, # noqa: SLF001+ timeout=self._fetcher.config.timeout_seconds,)except asyncio.TimeoutError:logger.info("[OfficeAceUserProfileRail] sync fetch timed out (user=%s); ""falling back to stale cache",- self._fetcher._config.user_id, # noqa: SLF001+ self._fetcher.config.user_id,)content = staleexcept Exception as exc: # noqa: BLE001logger.debug("[OfficeAceUserProfileRail] sync fetch failed (user=%s): %s; ""falling back to stale cache",- self._fetcher._config.user_id, # noqa: SLF001+ self._fetcher.config.user_id,exc,)content = stale@@ -130,7 +130,7 @@ class OfficeAceUserProfileRail(DeepAgentRail):return# 截断到 max_chars- max_chars = self._fetcher._config.max_chars # noqa: SLF001+ max_chars = self._fetcher.config.max_charsif len(content) > max_chars:content = content[:max_chars] + "\n\n[...已截断...]"diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_code.py b/jiuwenswarm/server/runtime/agent_adapter/interface_code.pyindex cafd2a2cfc..e1762a5c48 100644--- a/jiuwenswarm/server/runtime/agent_adapter/interface_code.py+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_code.py@@ -943,7 +943,8 @@ class JiuwenSwarmCodeAdapter(JiuWenSwarmDeepAdapter):)return None- def _build_office_ace_user_profile_rail(self) -> OfficeAceUserProfileRail | None:+ @staticmethod+ def _build_office_ace_user_profile_rail() -> OfficeAceUserProfileRail | None:"""Build OfficeAceUserProfileRail to inject cloud-side user profile.启用条件由 external memory 开关 + provider=officeace_cloud + 凭证齐全隐含控制diff --git a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.pyindex 858ad93f53..4a6d084d63 100644--- a/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py+++ b/jiuwenswarm/server/runtime/agent_adapter/interface_deep.py@@ -21880,7 +21880,8 @@ class JiuWenSwarmDeepAdapter:self._external_memory_rail = Noneself._external_memory_rail_registered = False- def _build_office_ace_user_profile_rail(self):+ @staticmethod+ def _build_office_ace_user_profile_rail():"""Build OfficeAceUserProfileRail from config, or None if disabled/failed.启用条件由 external memory 开关 + provider=officeace_cloud + 凭证齐全隐含控制