代码合并

This commit is contained in:
董宏凯 committed 2026-09-08 09:40:31 +08:00
1 parent b0970de677
commit 8859c13cfb
26 files changed
+1707 -334

No files matched your search

@@ -91,6 +91,62 @@ class AttemptUsage:
error_type: str | None = None
error_message: str | None = None
# AgentScope 的 Agent.compress_context 会读 model.context_size 并调 count_tokens。
_DEFAULT_CONTEXT_SIZE = 128_000
_CJK_RE = re.compile(r"[\u4e00-\u9fff\u3040-\u30ff\uac00-\ud7af]")
def estimate_tokens_for_text(text: str) -> int:
"""CJK-aware local token estimate: ~1 token per CJK char, ~4 bytes otherwise."""
if not text:
return 0
cjk = len(_CJK_RE.findall(text))
other = len(text) - cjk
return cjk + other // 4 + (1 if other % 4 else 0)
def _collect_countable_text(messages: list[Any]) -> str:
parts: list[str] = []
for msg in messages or []:
blocks: Any = None
getter = getattr(msg, "get_content_blocks", None)
if callable(getter):
try:
blocks = getter()
except Exception:
blocks = None
if blocks is not None:
for block in blocks:
if isinstance(block, str):
parts.append(block)
elif isinstance(block, dict):
for key in ("text", "thinking", "hint"):
value = block.get(key)
if isinstance(value, str):
parts.append(value)
elif isinstance(value, list):
parts.extend(str(item) for item in value)
else:
text = getattr(block, "text", None) or getattr(block, "thinking", None)
if text:
parts.append(str(text))
continue
if isinstance(msg, dict):
parts.append(str(msg.get("content") or ""))
else:
raw = getattr(msg, "content", "")
if isinstance(raw, list):
for block in raw:
if isinstance(block, dict):
parts.append(str(block.get("text") or ""))
else:
parts.append(str(getattr(block, "text", block) if not isinstance(block, str) else block))
else:
parts.append(str(raw or ""))
return "\n".join(parts)
def as_dict(self) -> dict[str, Any]:
return redact_mapping(
{
@@ -244,6 +300,21 @@ def to_lc_messages(messages: list[Any]) -> list[Any]:
return converted
def _resolve_context_size(app_config: AppConfig | None, config_name: str) -> int:
"""Best-effort context window from model config extras; sane default otherwise."""
try:
cfg = (app_config or get_app_config()).get_model_config(config_name)
extra = cfg.model_dump() if cfg is not None else {}
for key in ("context_size", "context_length", "context_window", "max_context_tokens"):
value = extra.get(key)
if isinstance(value, (int, float)) and int(value) > 0:
return int(value)
except Exception:
pass
return _DEFAULT_CONTEXT_SIZE
class DeerFlowChatModelAdapter:
"""Frozen DeerFlow-backed chat model. Construct once per node attempt from a snapshot."""
@@ -266,6 +337,21 @@ class DeerFlowChatModelAdapter:
self._llm_factory = llm_factory
self._node_run_id = node_run_id
self.last_usage = AttemptUsage(model_name=snapshot.config_name, node_run_id=node_run_id)
# AgentScope compress_context 契约:读 context_size 判断是否压缩。
self.context_size = _resolve_context_size(app_config, snapshot.config_name)
async def count_tokens(self, messages: list[Any], tools: list[dict[str, Any]] | None = None) -> int:
"""AgentScope 契约方法(compress_context 调用)。本地估算,不打 API。"""
import json as _json
total = estimate_tokens_for_text(_collect_countable_text(messages))
if tools:
try:
total += estimate_tokens_for_text(_json.dumps(tools, ensure_ascii=False, default=str))
except Exception:
total += sum(estimate_tokens_for_text(str(item)) for item in tools)
return total
def _config(self) -> AppConfig:
return self._app_config or get_app_config()