runner.py 104 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553
  1. """
  2. Agent Runner - Agent 执行引擎
  3. 核心职责:
  4. 1. 执行 Agent 任务(循环调用 LLM + 工具)
  5. 2. 记录执行轨迹(Trace + Messages + GoalTree)
  6. 3. 加载和注入技能(Skill)
  7. 4. 管理执行计划(GoalTree)
  8. 5. 支持续跑(continue)和回溯重跑(rewind)
  9. 参数分层:
  10. - Infrastructure: AgentRunner 构造时设置(trace_store, llm_call 等)
  11. - RunConfig: 每次 run 时指定(model, trace_id, after_sequence 等)
  12. - Messages: OpenAI SDK 格式的任务消息
  13. """
  14. import asyncio
  15. import json
  16. import logging
  17. import os
  18. import uuid
  19. from dataclasses import dataclass, field
  20. from datetime import datetime
  21. from typing import AsyncIterator, Optional, Dict, Any, List, Callable, Literal, Tuple, Union
  22. from agent.trace.models import Trace, Message
  23. from agent.trace.protocols import TraceStore
  24. from agent.trace.goal_models import GoalTree
  25. from agent.trace.compaction import (
  26. CompressionConfig,
  27. compress_completed_goals,
  28. estimate_tokens,
  29. needs_level2_compression,
  30. build_compression_prompt,
  31. )
  32. from agent.skill.models import Skill
  33. from agent.skill.skill_loader import load_skills_from_dir
  34. from agent.tools import ToolRegistry, get_tool_registry
  35. from agent.tools.builtin.knowledge import KnowledgeConfig
  36. from agent.tools.docker_runner import GatewayExecResolver, active_gateway_exec
  37. from agent.core.prompts import (
  38. DEFAULT_SYSTEM_PREFIX,
  39. TRUNCATION_HINT,
  40. TOOL_INTERRUPTED_MESSAGE,
  41. AGENT_INTERRUPTED_SUMMARY,
  42. AGENT_CONTINUE_HINT_TEMPLATE,
  43. TASK_NAME_GENERATION_SYSTEM_PROMPT,
  44. TASK_NAME_FALLBACK,
  45. SUMMARY_HEADER_TEMPLATE,
  46. build_summary_header,
  47. build_tool_interrupted_message,
  48. build_agent_continue_hint,
  49. )
  50. logger = logging.getLogger(__name__)
  51. @dataclass
  52. class ContextUsage:
  53. """Context 使用情况"""
  54. trace_id: str
  55. message_count: int
  56. token_count: int
  57. max_tokens: int
  58. usage_percent: float
  59. image_count: int = 0
  60. @dataclass
  61. class SideBranchContext:
  62. """侧分支上下文(压缩/反思)"""
  63. type: Literal["compression", "reflection"]
  64. branch_id: str
  65. start_head_seq: int # 侧分支起点的 head_seq
  66. start_sequence: int # 侧分支第一条消息的 sequence
  67. start_history_length: int # 侧分支起点的 history 长度
  68. start_iteration: int # 侧分支开始时的 iteration
  69. max_turns: int = 5 # 最大轮次
  70. def to_dict(self) -> Dict[str, Any]:
  71. """转换为字典(用于持久化和传递给工具)"""
  72. return {
  73. "type": self.type,
  74. "branch_id": self.branch_id,
  75. "start_head_seq": self.start_head_seq,
  76. "start_sequence": self.start_sequence,
  77. "start_iteration": self.start_iteration,
  78. "max_turns": self.max_turns,
  79. "is_side_branch": True,
  80. "started_at": datetime.now().isoformat(),
  81. }
  82. # ===== 运行配置 =====
  83. @dataclass
  84. class RunConfig:
  85. """
  86. 运行参数 — 控制 Agent 如何执行
  87. 分为模型层参数(由上游 agent 或用户决定)和框架层参数(由系统注入)。
  88. """
  89. # --- 模型层参数 ---
  90. model: str = "qwen3.5-flash"
  91. temperature: float = 0.3
  92. max_iterations: int = 200
  93. tools: Optional[List[str]] = None # None = 全部已注册工具
  94. side_branch_max_turns: int = 5 # 侧分支最大轮次(压缩/反思)
  95. goal_compression: Literal["none", "on_complete", "on_overflow"] = "on_overflow" # Goal 压缩模式
  96. # --- 强制侧分支(用于 API 手动触发或自动压缩流程)---
  97. # 使用列表作为侧分支队列,每次完成一个侧分支后 pop(0) 取下一个
  98. force_side_branch: Optional[List[Literal["compression", "reflection"]]] = None
  99. # --- 框架层参数 ---
  100. agent_type: str = "default"
  101. uid: Optional[str] = None
  102. system_prompt: Optional[str] = None # None = 从 skills 自动构建
  103. skills: Optional[List[str]] = None # 注入 system prompt 的 skill 名称列表;None = 按 preset 决定
  104. enable_memory: bool = True
  105. auto_execute_tools: bool = True
  106. name: Optional[str] = None # 显示名称(空则由 utility_llm 自动生成)
  107. enable_prompt_caching: bool = True # 启用 Anthropic Prompt Caching(仅 Claude 模型有效)
  108. # --- Trace 控制 ---
  109. trace_id: Optional[str] = None # None = 新建
  110. parent_trace_id: Optional[str] = None # 子 Agent 专用
  111. parent_goal_id: Optional[str] = None
  112. # --- 续跑控制 ---
  113. after_sequence: Optional[int] = None # 从哪条消息后续跑(message sequence)
  114. # --- 额外 LLM 参数(传给 llm_call 的 **kwargs)---
  115. extra_llm_params: Dict[str, Any] = field(default_factory=dict)
  116. # --- 自定义元数据上下文 ---
  117. context: Dict[str, Any] = field(default_factory=dict)
  118. # --- 研究流程控制 ---
  119. enable_research_flow: bool = True # 是否启用自动研究流程(知识检索→经验检索→调研→计划)
  120. # --- 知识管理配置 ---
  121. knowledge: KnowledgeConfig = field(default_factory=KnowledgeConfig)
  122. # 内置工具列表(始终自动加载)
  123. BUILTIN_TOOLS = [
  124. # 文件操作工具
  125. "read_file",
  126. "edit_file",
  127. "write_file",
  128. "glob_files",
  129. "grep_content",
  130. # 系统工具
  131. "bash_command",
  132. # 技能和目标管理
  133. "skill",
  134. "list_skills",
  135. "goal",
  136. "agent",
  137. "evaluate",
  138. "get_current_context",
  139. # 搜索工具
  140. "search_posts",
  141. "select_post",
  142. "get_search_suggestions",
  143. # 知识管理工具
  144. "knowledge_search",
  145. "knowledge_save",
  146. "knowledge_update",
  147. "knowledge_batch_update",
  148. "knowledge_list",
  149. "knowledge_slim",
  150. # 沙箱工具
  151. # "sandbox_create_environment",
  152. # "sandbox_run_shell",
  153. # "sandbox_rebuild_with_ports",
  154. # "sandbox_destroy_environment",
  155. # 浏览器工具
  156. "browser_get_live_url",
  157. "browser_navigate_to_url",
  158. "browser_search_web",
  159. "browser_go_back",
  160. "browser_wait",
  161. "browser_click_element",
  162. "browser_input_text",
  163. "browser_send_keys",
  164. "browser_upload_file",
  165. "browser_scroll_page",
  166. "browser_find_text",
  167. "browser_screenshot",
  168. "browser_switch_tab",
  169. "browser_close_tab",
  170. "browser_get_dropdown_options",
  171. "browser_select_dropdown_option",
  172. "browser_extract_content",
  173. "browser_read_long_content",
  174. "browser_download_direct_url",
  175. "browser_get_page_html",
  176. "browser_get_visual_selector_map",
  177. "browser_evaluate",
  178. "browser_ensure_login_with_cookies",
  179. # 可以暂时由飞书消息替代
  180. #"browser_wait_for_user_action",
  181. "browser_done",
  182. "browser_export_cookies",
  183. "browser_load_cookies",
  184. # 仅飞书 Node HTTP 适配层:/tools、/tool-call、/tool-calls/batch
  185. "feishu_adapter_list_tools",
  186. "feishu_adapter_tool_call",
  187. "feishu_adapter_tool_calls_batch",
  188. ]
  189. @dataclass
  190. class CallResult:
  191. """单次调用结果"""
  192. reply: str
  193. tool_calls: Optional[List[Dict]] = None
  194. trace_id: Optional[str] = None
  195. step_id: Optional[str] = None
  196. tokens: Optional[Dict[str, int]] = None
  197. cost: float = 0.0
  198. # ===== 执行引擎 =====
  199. CONTEXT_INJECTION_INTERVAL = 10 # 每 N 轮注入一次 GoalTree + Collaborators
  200. class AgentRunner:
  201. """
  202. Agent 执行引擎
  203. 支持三种运行模式(通过 RunConfig 区分):
  204. 1. 新建:trace_id=None
  205. 2. 续跑:trace_id=已有ID, after_sequence=None 或 == head
  206. 3. 回溯:trace_id=已有ID, after_sequence=N(N < head_sequence)
  207. """
  208. def __init__(
  209. self,
  210. trace_store: Optional[TraceStore] = None,
  211. tool_registry: Optional[ToolRegistry] = None,
  212. llm_call: Optional[Callable] = None,
  213. utility_llm_call: Optional[Callable] = None,
  214. skills_dir: Optional[str] = None,
  215. goal_tree: Optional[GoalTree] = None,
  216. debug: bool = False,
  217. ):
  218. """
  219. 初始化 AgentRunner
  220. Args:
  221. trace_store: Trace 存储
  222. tool_registry: 工具注册表(默认使用全局注册表)
  223. llm_call: 主 LLM 调用函数
  224. utility_llm_call: 轻量 LLM(用于生成任务标题等),可选
  225. skills_dir: Skills 目录路径
  226. goal_tree: 初始 GoalTree(可选)
  227. debug: 保留参数(已废弃)
  228. """
  229. self.trace_store = trace_store
  230. self.tools = tool_registry or get_tool_registry()
  231. self.llm_call = llm_call
  232. self.utility_llm_call = utility_llm_call
  233. self.skills_dir = skills_dir
  234. self.goal_tree = goal_tree
  235. self.debug = debug
  236. self.stdin_check: Optional[Callable] = None # 由外部设置,用于子 agent 执行期间检查 stdin
  237. self._cancel_events: Dict[str, asyncio.Event] = {} # trace_id → cancel event
  238. # 知识保存跟踪(每个 trace 独立)
  239. self._saved_knowledge_ids: Dict[str, List[str]] = {} # trace_id → [knowledge_ids]
  240. # Context 使用跟踪
  241. self._context_warned: Dict[str, set] = {} # trace_id → {30, 50, 80} 已警告过的阈值
  242. self._context_usage: Dict[str, ContextUsage] = {} # trace_id → 当前用量快照
  243. # 图片优化缓存(避免重复处理)
  244. # key: 图片内容的 hash, value: {"downscaled": ..., "description": ...}
  245. self._image_opt_cache: Dict[str, Dict[str, Any]] = {}
  246. # ===== 核心公开方法 =====
  247. def get_context_usage(self, trace_id: str) -> Optional[ContextUsage]:
  248. """获取指定 trace 的 context 使用情况"""
  249. return self._context_usage.get(trace_id)
  250. async def run(
  251. self,
  252. messages: List[Dict],
  253. config: Optional[RunConfig] = None,
  254. ) -> AsyncIterator[Union[Trace, Message]]:
  255. """
  256. Agent 模式执行(核心方法)
  257. Args:
  258. messages: OpenAI SDK 格式的输入消息
  259. 新建: 初始任务消息 [{"role": "user", "content": "..."}]
  260. 续跑: 追加的新消息
  261. 回溯: 在插入点之后追加的消息
  262. config: 运行配置
  263. Yields:
  264. Union[Trace, Message]: Trace 对象(状态变化)或 Message 对象(执行过程)
  265. """
  266. if not self.llm_call:
  267. raise ValueError("llm_call function not provided")
  268. config = config or RunConfig()
  269. trace = None
  270. try:
  271. # Phase 1: PREPARE TRACE
  272. trace, goal_tree, sequence = await self._prepare_trace(messages, config)
  273. # 续跑:从 Trace 恢复 agent_type / skills 白名单(新建 Trace 在 _prepare_new_trace 已写入)
  274. if trace.agent_type:
  275. config.agent_type = trace.agent_type
  276. if trace.context:
  277. rs = trace.context.get("run_skills")
  278. if rs is not None and isinstance(rs, list):
  279. config.skills = list(rs)
  280. # 注册取消事件
  281. self._cancel_events[trace.trace_id] = asyncio.Event()
  282. yield trace
  283. # 检查是否有未完成的侧分支(用于用户追加消息场景)
  284. side_branch_ctx_for_build: Optional[SideBranchContext] = None
  285. if trace.context.get("active_side_branch") and messages:
  286. side_branch_data = trace.context["active_side_branch"]
  287. # 创建侧分支上下文(用于标记用户追加的消息)
  288. side_branch_ctx_for_build = SideBranchContext(
  289. type=side_branch_data["type"],
  290. branch_id=side_branch_data["branch_id"],
  291. start_head_seq=side_branch_data["start_head_seq"],
  292. start_sequence=side_branch_data["start_sequence"],
  293. start_history_length=0,
  294. start_iteration=side_branch_data.get("start_iteration", 0),
  295. max_turns=side_branch_data.get("max_turns", config.side_branch_max_turns),
  296. )
  297. # Phase 2: BUILD HISTORY
  298. history, sequence, created_messages, head_seq = await self._build_history(
  299. trace.trace_id, messages, goal_tree, config, sequence, side_branch_ctx_for_build
  300. )
  301. # Update trace's head_sequence in memory
  302. trace.head_sequence = head_seq
  303. for msg in created_messages:
  304. yield msg
  305. # Phase 3: AGENT LOOP
  306. async for event in self._agent_loop(trace, history, goal_tree, config, sequence):
  307. yield event
  308. except Exception as e:
  309. logger.error(f"Agent run failed: {e}")
  310. tid = config.trace_id or (trace.trace_id if trace else None)
  311. if self.trace_store and tid:
  312. # 读取当前 last_sequence 作为 head_sequence,确保续跑时能加载完整历史
  313. current = await self.trace_store.get_trace(tid)
  314. head_seq = current.last_sequence if current else None
  315. await self.trace_store.update_trace(
  316. tid,
  317. status="failed",
  318. head_sequence=head_seq,
  319. error_message=str(e),
  320. completed_at=datetime.now()
  321. )
  322. trace_obj = await self.trace_store.get_trace(tid)
  323. if trace_obj:
  324. yield trace_obj
  325. raise
  326. finally:
  327. # 清理取消事件
  328. if trace:
  329. self._cancel_events.pop(trace.trace_id, None)
  330. async def run_result(
  331. self,
  332. messages: List[Dict],
  333. config: Optional[RunConfig] = None,
  334. on_event: Optional[Callable] = None,
  335. ) -> Dict[str, Any]:
  336. """
  337. 结果模式 — 消费 run(),返回结构化结果。
  338. 主要用于 agent/evaluate 工具内部。
  339. Args:
  340. on_event: 可选回调,每个 Trace/Message 事件触发一次,用于实时输出子 Agent 执行过程。
  341. """
  342. last_assistant_text = ""
  343. final_trace: Optional[Trace] = None
  344. async for item in self.run(messages=messages, config=config):
  345. if on_event:
  346. on_event(item)
  347. if isinstance(item, Message) and item.role == "assistant":
  348. content = item.content
  349. text = ""
  350. if isinstance(content, dict):
  351. text = content.get("text", "") or ""
  352. elif isinstance(content, str):
  353. text = content
  354. if text and text.strip():
  355. last_assistant_text = text
  356. elif isinstance(item, Trace):
  357. final_trace = item
  358. config = config or RunConfig()
  359. if not final_trace and config.trace_id and self.trace_store:
  360. final_trace = await self.trace_store.get_trace(config.trace_id)
  361. status = final_trace.status if final_trace else "unknown"
  362. error = final_trace.error_message if final_trace else None
  363. summary = last_assistant_text
  364. if not summary:
  365. status = "failed"
  366. error = error or "Agent 没有产生 assistant 文本结果"
  367. # 获取保存的知识 ID
  368. trace_id = final_trace.trace_id if final_trace else config.trace_id
  369. saved_knowledge_ids = self._saved_knowledge_ids.get(trace_id, [])
  370. return {
  371. "status": status,
  372. "summary": summary,
  373. "trace_id": trace_id,
  374. "error": error,
  375. "saved_knowledge_ids": saved_knowledge_ids, # 新增:返回保存的知识 ID
  376. "stats": {
  377. "total_messages": final_trace.total_messages if final_trace else 0,
  378. "total_tokens": final_trace.total_tokens if final_trace else 0,
  379. "total_cost": final_trace.total_cost if final_trace else 0.0,
  380. },
  381. }
  382. async def stop(self, trace_id: str) -> bool:
  383. """
  384. 停止运行中的 Trace
  385. 设置取消信号,agent loop 在下一个 LLM 调用前检查并退出。
  386. Trace 状态置为 "stopped"。
  387. Returns:
  388. True 如果成功发送停止信号,False 如果该 trace 不在运行中
  389. """
  390. cancel_event = self._cancel_events.get(trace_id)
  391. if cancel_event is None:
  392. return False
  393. cancel_event.set()
  394. return True
  395. # ===== 单次调用(保留)=====
  396. async def call(
  397. self,
  398. messages: List[Dict],
  399. model: str = "qwen3.5-flash",
  400. tools: Optional[List[str]] = None,
  401. uid: Optional[str] = None,
  402. trace: bool = True,
  403. **kwargs
  404. ) -> CallResult:
  405. """
  406. 单次 LLM 调用(无 Agent Loop)
  407. """
  408. if not self.llm_call:
  409. raise ValueError("llm_call function not provided")
  410. trace_id = None
  411. message_id = None
  412. tool_schemas = self._get_tool_schemas(tools)
  413. if trace and self.trace_store:
  414. trace_obj = Trace.create(mode="call", uid=uid, model=model, tools=tool_schemas, llm_params=kwargs)
  415. trace_id = await self.trace_store.create_trace(trace_obj)
  416. result = await self.llm_call(messages=messages, model=model, tools=tool_schemas, **kwargs)
  417. if trace and self.trace_store and trace_id:
  418. msg = Message.create(
  419. trace_id=trace_id, role="assistant", sequence=1, goal_id=None,
  420. content={"text": result.get("content", ""), "tool_calls": result.get("tool_calls")},
  421. prompt_tokens=result.get("prompt_tokens", 0),
  422. completion_tokens=result.get("completion_tokens", 0),
  423. finish_reason=result.get("finish_reason"),
  424. cost=result.get("cost", 0),
  425. )
  426. message_id = await self.trace_store.add_message(msg)
  427. await self.trace_store.update_trace(trace_id, status="completed", completed_at=datetime.now())
  428. return CallResult(
  429. reply=result.get("content", ""),
  430. tool_calls=result.get("tool_calls"),
  431. trace_id=trace_id,
  432. step_id=message_id,
  433. tokens={"prompt": result.get("prompt_tokens", 0), "completion": result.get("completion_tokens", 0)},
  434. cost=result.get("cost", 0)
  435. )
  436. # ===== Phase 1: PREPARE TRACE =====
  437. async def _prepare_trace(
  438. self,
  439. messages: List[Dict],
  440. config: RunConfig,
  441. ) -> Tuple[Trace, Optional[GoalTree], int]:
  442. """
  443. 准备 Trace:创建新的或加载已有的
  444. Returns:
  445. (trace, goal_tree, next_sequence)
  446. """
  447. if config.trace_id:
  448. return await self._prepare_existing_trace(config)
  449. else:
  450. return await self._prepare_new_trace(messages, config)
  451. async def _prepare_new_trace(
  452. self,
  453. messages: List[Dict],
  454. config: RunConfig,
  455. ) -> Tuple[Trace, Optional[GoalTree], int]:
  456. """创建新 Trace"""
  457. trace_id = str(uuid.uuid4())
  458. # 生成任务名称
  459. task_name = config.name or await self._generate_task_name(messages)
  460. # 准备工具 Schema
  461. tool_schemas = self._get_tool_schemas(config.tools)
  462. trace_ctx = dict(config.context or {})
  463. if config.skills is not None:
  464. trace_ctx["run_skills"] = list(config.skills)
  465. trace_obj = Trace(
  466. trace_id=trace_id,
  467. mode="agent",
  468. task=task_name,
  469. agent_type=config.agent_type,
  470. parent_trace_id=config.parent_trace_id,
  471. parent_goal_id=config.parent_goal_id,
  472. uid=config.uid,
  473. model=config.model,
  474. tools=tool_schemas,
  475. llm_params={"temperature": config.temperature, **config.extra_llm_params},
  476. context=trace_ctx,
  477. status="running",
  478. )
  479. goal_tree = self.goal_tree or GoalTree(mission=task_name)
  480. if self.trace_store:
  481. await self.trace_store.create_trace(trace_obj)
  482. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  483. return trace_obj, goal_tree, 1
  484. async def _prepare_existing_trace(
  485. self,
  486. config: RunConfig,
  487. ) -> Tuple[Trace, Optional[GoalTree], int]:
  488. """加载已有 Trace(续跑或回溯)"""
  489. if not self.trace_store:
  490. raise ValueError("trace_store required for continue/rewind")
  491. trace_obj = await self.trace_store.get_trace(config.trace_id)
  492. if not trace_obj:
  493. raise ValueError(f"Trace not found: {config.trace_id}")
  494. goal_tree = await self.trace_store.get_goal_tree(config.trace_id)
  495. if goal_tree is None:
  496. # 防御性兜底:trace 存在但 goal.json 丢失时,创建空树
  497. goal_tree = GoalTree(mission=trace_obj.task or "Agent task")
  498. await self.trace_store.update_goal_tree(config.trace_id, goal_tree)
  499. # 自动判断行为:after_sequence 为 None 或 == head → 续跑;< head → 回溯
  500. after_seq = config.after_sequence
  501. # 如果 after_seq > head_sequence,说明 generator 被强制关闭时 store 的
  502. # head_sequence 未来得及更新(仍停在 Phase 2 写入的初始值)。
  503. # 用 last_sequence 修正 head_sequence,确保续跑时能看到完整历史。
  504. if after_seq is not None and after_seq > trace_obj.head_sequence:
  505. trace_obj.head_sequence = trace_obj.last_sequence
  506. await self.trace_store.update_trace(
  507. config.trace_id, head_sequence=trace_obj.head_sequence
  508. )
  509. if after_seq is not None and after_seq < trace_obj.head_sequence:
  510. # 回溯模式
  511. sequence = await self._rewind(config.trace_id, after_seq, goal_tree)
  512. else:
  513. # 续跑模式:从 last_sequence + 1 开始
  514. sequence = trace_obj.last_sequence + 1
  515. # 状态置为 running
  516. await self.trace_store.update_trace(
  517. config.trace_id,
  518. status="running",
  519. completed_at=None,
  520. )
  521. trace_obj.status = "running"
  522. # 广播状态变化给前端
  523. try:
  524. from agent.trace.websocket import broadcast_trace_status_changed
  525. await broadcast_trace_status_changed(config.trace_id, "running")
  526. except Exception:
  527. pass
  528. return trace_obj, goal_tree, sequence
  529. # ===== Phase 2: BUILD HISTORY =====
  530. async def _build_history(
  531. self,
  532. trace_id: str,
  533. new_messages: List[Dict],
  534. goal_tree: Optional[GoalTree],
  535. config: RunConfig,
  536. sequence: int,
  537. side_branch_ctx: Optional[SideBranchContext] = None,
  538. ) -> Tuple[List[Dict], int, List[Message], int]:
  539. """
  540. 构建完整的 LLM 消息历史
  541. 1. 从 head_sequence 沿 parent chain 加载主路径消息(续跑/回溯场景)
  542. 2. 构建 system prompt(新建时注入 skills)
  543. 3. 新建时:在第一条 user message 末尾注入当前经验
  544. 4. 追加 input messages(设置 parent_sequence 链接到当前 head)
  545. 5. 如果在侧分支中,追加的消息自动标记为侧分支消息
  546. Returns:
  547. (history, next_sequence, created_messages, head_sequence)
  548. created_messages: 本次新创建并持久化的 Message 列表,供 run() yield 给调用方
  549. head_sequence: 当前主路径头节点的 sequence
  550. """
  551. history: List[Dict] = []
  552. created_messages: List[Message] = []
  553. head_seq: Optional[int] = None # 当前主路径的头节点 sequence
  554. # 1. 加载已有 messages(通过主路径遍历)
  555. if config.trace_id and self.trace_store:
  556. trace_obj = await self.trace_store.get_trace(trace_id)
  557. if trace_obj and trace_obj.head_sequence > 0:
  558. main_path = await self.trace_store.get_main_path_messages(
  559. trace_id, trace_obj.head_sequence
  560. )
  561. # 修复 orphaned tool_calls(中断导致的 tool_call 无 tool_result)
  562. main_path, sequence = await self._heal_orphaned_tool_calls(
  563. main_path, trace_id, goal_tree, sequence,
  564. )
  565. history = [msg.to_llm_dict() for msg in main_path]
  566. if main_path:
  567. head_seq = main_path[-1].sequence
  568. # 2. 构建/注入 skills 到 system prompt
  569. has_system = any(m.get("role") == "system" for m in history)
  570. has_system_in_new = any(m.get("role") == "system" for m in new_messages)
  571. if not has_system:
  572. if has_system_in_new:
  573. # 入参消息已含 system,将 skills 注入其中(在 step 4 持久化之前)
  574. augmented = []
  575. for msg in new_messages:
  576. if msg.get("role") == "system":
  577. base = msg.get("content") or ""
  578. enriched = await self._build_system_prompt(config, base_prompt=base)
  579. augmented.append({**msg, "content": enriched or base})
  580. else:
  581. augmented.append(msg)
  582. new_messages = augmented
  583. else:
  584. # 没有 system,自动构建并插入历史
  585. system_prompt = await self._build_system_prompt(config)
  586. if system_prompt:
  587. history = [{"role": "system", "content": system_prompt}] + history
  588. if self.trace_store:
  589. system_msg = Message.create(
  590. trace_id=trace_id, role="system", sequence=sequence,
  591. goal_id=None, content=system_prompt,
  592. parent_sequence=None, # system message 是 root
  593. )
  594. await self.trace_store.add_message(system_msg)
  595. created_messages.append(system_msg)
  596. head_seq = sequence
  597. sequence += 1
  598. # 3. 追加新 messages(设置 parent_sequence 链接到当前 head)
  599. for msg_dict in new_messages:
  600. history.append(msg_dict)
  601. if self.trace_store:
  602. # 如果在侧分支中,标记为侧分支消息
  603. if side_branch_ctx:
  604. stored_msg = Message.create(
  605. trace_id=trace_id,
  606. role=msg_dict["role"],
  607. sequence=sequence,
  608. goal_id=goal_tree.current_id if goal_tree else None,
  609. parent_sequence=head_seq,
  610. branch_type=side_branch_ctx.type,
  611. branch_id=side_branch_ctx.branch_id,
  612. content=msg_dict.get("content"),
  613. )
  614. logger.info(f"用户在侧分支 {side_branch_ctx.type} 中追加消息")
  615. else:
  616. stored_msg = Message.from_llm_dict(
  617. msg_dict, trace_id=trace_id, sequence=sequence,
  618. goal_id=None, parent_sequence=head_seq,
  619. )
  620. await self.trace_store.add_message(stored_msg)
  621. created_messages.append(stored_msg)
  622. head_seq = sequence
  623. sequence += 1
  624. # 5. 更新 trace 的 head_sequence
  625. if self.trace_store and head_seq is not None:
  626. await self.trace_store.update_trace(trace_id, head_sequence=head_seq)
  627. return history, sequence, created_messages, head_seq or 0
  628. # ===== Phase 3: AGENT LOOP =====
  629. async def _manage_context_usage(
  630. self,
  631. trace_id: str,
  632. history: List[Dict],
  633. goal_tree: Optional[GoalTree],
  634. config: RunConfig,
  635. sequence: int,
  636. head_seq: int,
  637. ) -> Tuple[List[Dict], int, int, bool]:
  638. """
  639. 管理 context 用量:检查、预警、压缩
  640. Returns:
  641. (updated_history, new_head_seq, next_sequence, needs_enter_compression_branch)
  642. """
  643. compression_config = CompressionConfig()
  644. token_count = estimate_tokens(history)
  645. max_tokens = compression_config.get_max_tokens(config.model)
  646. # 计算使用率
  647. progress_pct = (token_count / max_tokens * 100) if max_tokens > 0 else 0
  648. msg_count = len(history)
  649. img_count = sum(
  650. 1 for msg in history
  651. if isinstance(msg.get("content"), list)
  652. for part in msg["content"]
  653. if isinstance(part, dict) and part.get("type") in ("image", "image_url")
  654. )
  655. # 更新 context usage 快照
  656. self._context_usage[trace_id] = ContextUsage(
  657. trace_id=trace_id,
  658. message_count=msg_count,
  659. token_count=token_count,
  660. max_tokens=max_tokens,
  661. usage_percent=progress_pct,
  662. image_count=img_count,
  663. )
  664. # 阈值警告(30%, 50%, 80%)
  665. if trace_id not in self._context_warned:
  666. self._context_warned[trace_id] = set()
  667. for threshold in [30, 50, 80]:
  668. if progress_pct >= threshold and threshold not in self._context_warned[trace_id]:
  669. self._context_warned[trace_id].add(threshold)
  670. logger.warning(
  671. f"Context 使用率达到 {threshold}%: {token_count:,} / {max_tokens:,} tokens ({msg_count} 条消息)"
  672. )
  673. # 检查是否需要压缩(仅基于 token 数量)
  674. needs_compression = token_count > max_tokens
  675. if not needs_compression:
  676. return history, head_seq, sequence, False
  677. # 知识提取:在任何压缩发生前,用完整 history 做反思(进入反思侧分支)
  678. if config.knowledge.enable_extraction and not config.force_side_branch:
  679. # 设置侧分支队列:先反思,再压缩
  680. config.force_side_branch = ["reflection", "compression"]
  681. return history, head_seq, sequence, True
  682. # 以下为未启用反思、需要压缩的情况,直接进行level 1压缩,并检查是否需要进行level 2压缩(进入侧分支)
  683. # Level 1 压缩:Goal 完成压缩
  684. if config.goal_compression != "none" and self.trace_store and goal_tree:
  685. if head_seq > 0:
  686. main_path_msgs = await self.trace_store.get_main_path_messages(
  687. trace_id, head_seq
  688. )
  689. compressed_msgs = compress_completed_goals(main_path_msgs, goal_tree)
  690. if len(compressed_msgs) < len(main_path_msgs):
  691. logger.info(
  692. "Level 1 压缩: %d -> %d 条消息",
  693. len(main_path_msgs), len(compressed_msgs),
  694. )
  695. history = [msg.to_llm_dict() for msg in compressed_msgs]
  696. else:
  697. logger.info(
  698. "Level 1 压缩: 无可过滤消息 (%d 条全部保留)",
  699. len(main_path_msgs),
  700. )
  701. elif needs_compression:
  702. logger.warning(
  703. "Token 数 (%d) 超过阈值,但无法执行 Level 1 压缩(缺少 store 或 goal_tree,或 goal_compression=none)",
  704. token_count,
  705. )
  706. # Level 2 压缩:检查 Level 1 后是否仍超阈值
  707. # 注意:Level 1 压缩后需要重新优化图片并计算 token
  708. optimized_history_after = await self._optimize_images(history, config.model)
  709. token_count_after = estimate_tokens(optimized_history_after)
  710. needs_level2 = token_count_after > max_tokens
  711. if needs_level2:
  712. logger.info(
  713. "Level 1 后仍超阈值 (token=%d/%d),需要进入压缩侧分支",
  714. token_count_after, max_tokens,
  715. )
  716. # 如果还没有设置侧分支(说明没有启用知识提取),直接进入压缩
  717. if not config.force_side_branch:
  718. config.force_side_branch = ["compression"]
  719. # 返回标志,让主循环进入侧分支
  720. return history, head_seq, sequence, True
  721. # 压缩完成后,输出最终发给模型的消息列表
  722. logger.info("Level 1 压缩完成,发送给模型的消息列表:")
  723. for idx, msg in enumerate(history):
  724. role = msg.get("role", "unknown")
  725. content = msg.get("content", "")
  726. if isinstance(content, str):
  727. preview = content[:100] + ("..." if len(content) > 100 else "")
  728. elif isinstance(content, list):
  729. preview = f"[{len(content)} blocks]"
  730. else:
  731. preview = str(content)[:100]
  732. logger.info(f" [{idx}] {role}: {preview}")
  733. return history, head_seq, sequence, False
  734. async def _single_turn_compress(
  735. self,
  736. trace_id: str,
  737. history: List[Dict],
  738. goal_tree: Optional[GoalTree],
  739. config: RunConfig,
  740. ) -> str:
  741. """单次 LLM 调用生成压缩摘要,返回 summary 文本"""
  742. logger.info("执行单次 LLM 压缩")
  743. # 构建压缩 prompt(使用 SINGLE_TURN_PROMPT)
  744. from agent.core.prompts import build_single_turn_prompt
  745. goal_prompt = goal_tree.to_prompt(include_summary=True) if goal_tree else ""
  746. compress_prompt = build_single_turn_prompt(goal_prompt)
  747. compress_messages = list(history) + [
  748. {"role": "user", "content": compress_prompt}
  749. ]
  750. # 应用 Prompt Caching
  751. compress_messages = self._add_cache_control(
  752. compress_messages, config.model, config.enable_prompt_caching
  753. )
  754. # 单次 LLM 调用(无工具)
  755. result = await self.llm_call(
  756. messages=compress_messages,
  757. model=config.model,
  758. tools=[], # 不提供工具
  759. temperature=config.temperature,
  760. **config.extra_llm_params,
  761. )
  762. summary_text = result.get("content", "").strip()
  763. # 提取 [[SUMMARY]] 块
  764. if "[[SUMMARY]]" in summary_text:
  765. summary_text = summary_text[
  766. summary_text.index("[[SUMMARY]]") + len("[[SUMMARY]]"):
  767. ].strip()
  768. return summary_text
  769. @staticmethod
  770. def _try_fix_json(s: str) -> Optional[dict]:
  771. """尝试修复常见的 JSON 截断/格式问题,返回 dict 或 None"""
  772. import re
  773. fixed = s.strip()
  774. # 1. 修复值中未转义的引号(如 "key": "he said "hello" to me")
  775. # 策略:找到 key-value 模式中值字符串内部的裸引号并转义
  776. def _fix_inner_quotes(text: str) -> str:
  777. # 匹配 ": "..." 模式,修复值内部的未转义引号
  778. result = []
  779. i = 0
  780. while i < len(text):
  781. # 找到 ": " 后面的值字符串开头
  782. if text[i] == '"':
  783. # 找到这个引号对应的字符串结束位置
  784. j = i + 1
  785. while j < len(text):
  786. if text[j] == '\\':
  787. j += 2 # 跳过转义字符
  788. continue
  789. if text[j] == '"':
  790. break
  791. j += 1
  792. # 检查引号后面是否是合法的 JSON 分隔符
  793. if j < len(text):
  794. after = j + 1
  795. # 跳过空白
  796. while after < len(text) and text[after] in ' \t\n\r':
  797. after += 1
  798. if after < len(text) and text[after] not in ':,}]\n\r':
  799. # 这个引号不是真正的结束引号,继续往后找
  800. # 找到下一个后面跟合法分隔符的引号
  801. k = j + 1
  802. found_end = False
  803. while k < len(text):
  804. if text[k] == '"':
  805. peek = k + 1
  806. while peek < len(text) and text[peek] in ' \t\n\r':
  807. peek += 1
  808. if peek >= len(text) or text[peek] in ':,}]':
  809. # 这才是真正的结束引号,转义中间的引号
  810. inner = text[i+1:k].replace('"', '\\"')
  811. result.append('"' + inner + '"')
  812. i = k + 1
  813. found_end = True
  814. break
  815. k += 1
  816. if found_end:
  817. continue
  818. result.append(text[i])
  819. i += 1
  820. return ''.join(result)
  821. fixed = _fix_inner_quotes(fixed)
  822. # 2. 去掉尾部多余逗号
  823. fixed = re.sub(r',\s*([}\]])', r'\1', fixed)
  824. # 3. 尝试补全截断的字符串和括号
  825. for suffix in ['', '"', '"}', '"]', '"}]', '"}}']:
  826. try:
  827. attempt = fixed + suffix
  828. open_braces = attempt.count('{') - attempt.count('}')
  829. open_brackets = attempt.count('[') - attempt.count(']')
  830. attempt += '}' * max(0, open_braces) + ']' * max(0, open_brackets)
  831. result = json.loads(attempt)
  832. if isinstance(result, dict):
  833. logger.info(f"[JSON Fix] 成功修复 JSON (suffix={repr(suffix)})")
  834. return result
  835. except json.JSONDecodeError:
  836. continue
  837. return None
  838. async def _agent_loop(
  839. self,
  840. trace: Trace,
  841. history: List[Dict],
  842. goal_tree: Optional[GoalTree],
  843. config: RunConfig,
  844. sequence: int,
  845. ) -> AsyncIterator[Union[Trace, Message]]:
  846. """ReAct 循环"""
  847. trace_id = trace.trace_id
  848. tool_schemas = self._get_tool_schemas(config.tools)
  849. # 当前主路径头节点的 sequence(用于设置 parent_sequence)
  850. head_seq = trace.head_sequence
  851. # 侧分支状态(None = 主路径)
  852. side_branch_ctx: Optional[SideBranchContext] = None
  853. # 检查是否有未完成的侧分支需要恢复
  854. if trace.context.get("active_side_branch"):
  855. side_branch_data = trace.context["active_side_branch"]
  856. branch_id = side_branch_data["branch_id"]
  857. start_sequence = side_branch_data["start_sequence"]
  858. # 从数据库查询侧分支消息(按 sequence 范围)
  859. if self.trace_store:
  860. all_messages = await self.trace_store.get_trace_messages(trace_id)
  861. side_messages = [
  862. m for m in all_messages
  863. if m.sequence >= start_sequence
  864. ]
  865. # 恢复侧分支上下文
  866. side_branch_ctx = SideBranchContext(
  867. type=side_branch_data["type"],
  868. branch_id=branch_id,
  869. start_head_seq=side_branch_data["start_head_seq"],
  870. start_sequence=side_branch_data["start_sequence"],
  871. start_history_length=0, # 稍后重新计算
  872. start_iteration=side_branch_data.get("start_iteration", 0),
  873. max_turns=side_branch_data.get("max_turns", config.side_branch_max_turns),
  874. )
  875. logger.info(
  876. f"恢复未完成的侧分支: {side_branch_ctx.type}, "
  877. f"max_turns={side_branch_ctx.max_turns}"
  878. )
  879. # 将侧分支消息追加到 history
  880. for m in side_messages:
  881. history.append(m.to_llm_dict())
  882. # 重新计算 start_history_length
  883. side_branch_ctx.start_history_length = len(history) - len(side_messages)
  884. break_after_side_branch = False # 侧分支退出后是否 break 主循环
  885. for iteration in range(config.max_iterations):
  886. # 更新活动时间(表明trace正在活跃运行)
  887. if self.trace_store:
  888. await self.trace_store.update_trace(
  889. trace_id,
  890. last_activity_at=datetime.now()
  891. )
  892. # 检查取消信号
  893. cancel_event = self._cancel_events.get(trace_id)
  894. if cancel_event and cancel_event.is_set():
  895. logger.info(f"Trace {trace_id} stopped by user")
  896. if self.trace_store:
  897. await self.trace_store.update_trace(
  898. trace_id,
  899. status="stopped",
  900. head_sequence=head_seq,
  901. completed_at=datetime.now(),
  902. )
  903. trace_obj = await self.trace_store.get_trace(trace_id)
  904. if trace_obj:
  905. yield trace_obj
  906. return
  907. # Context 管理(仅主路径)
  908. needs_enter_side_branch = False
  909. if not side_branch_ctx:
  910. # 侧分支退出后需要 break 主循环
  911. if break_after_side_branch and not config.force_side_branch:
  912. break
  913. # 检查是否强制进入侧分支(API 手动触发或自动压缩流程)
  914. if config.force_side_branch:
  915. needs_enter_side_branch = True
  916. logger.info(f"强制进入侧分支: {config.force_side_branch}")
  917. else:
  918. # 正常的 context 管理逻辑
  919. history, head_seq, sequence, needs_enter_side_branch = await self._manage_context_usage(
  920. trace_id, history, goal_tree, config, sequence, head_seq
  921. )
  922. # 进入侧分支
  923. if needs_enter_side_branch and not side_branch_ctx:
  924. # 从队列中取出第一个侧分支类型
  925. if config.force_side_branch and isinstance(config.force_side_branch, list) and len(config.force_side_branch) > 0:
  926. branch_type = config.force_side_branch.pop(0)
  927. logger.info(f"从队列取出侧分支: {branch_type}, 剩余队列: {config.force_side_branch}")
  928. elif config.knowledge.enable_extraction:
  929. # 兼容旧的单值模式(如果 force_side_branch 是字符串)
  930. branch_type = "reflection"
  931. else:
  932. # 自动触发:压缩
  933. branch_type = "compression"
  934. branch_id = f"{branch_type}_{uuid.uuid4().hex[:8]}"
  935. side_branch_ctx = SideBranchContext(
  936. type=branch_type,
  937. branch_id=branch_id,
  938. start_head_seq=head_seq,
  939. start_sequence=sequence,
  940. start_history_length=len(history),
  941. start_iteration=iteration,
  942. max_turns=config.side_branch_max_turns,
  943. )
  944. # 持久化侧分支状态
  945. if self.trace_store:
  946. trace.context["active_side_branch"] = {
  947. "type": side_branch_ctx.type,
  948. "branch_id": side_branch_ctx.branch_id,
  949. "start_head_seq": side_branch_ctx.start_head_seq,
  950. "start_sequence": side_branch_ctx.start_sequence,
  951. "start_iteration": side_branch_ctx.start_iteration,
  952. "max_turns": side_branch_ctx.max_turns,
  953. "started_at": datetime.now().isoformat(),
  954. }
  955. await self.trace_store.update_trace(
  956. trace_id,
  957. context=trace.context
  958. )
  959. # 追加侧分支 prompt
  960. if branch_type == "reflection":
  961. prompt = config.knowledge.get_reflect_prompt()
  962. else: # compression
  963. from agent.trace.compaction import build_compression_prompt
  964. prompt = build_compression_prompt(goal_tree)
  965. branch_user_msg = Message.create(
  966. trace_id=trace_id,
  967. role="user",
  968. sequence=sequence,
  969. parent_sequence=head_seq,
  970. goal_id=goal_tree.current_id if goal_tree else None,
  971. branch_type=branch_type,
  972. branch_id=branch_id,
  973. content=prompt,
  974. )
  975. if self.trace_store:
  976. await self.trace_store.add_message(branch_user_msg)
  977. history.append(branch_user_msg.to_llm_dict())
  978. head_seq = sequence
  979. sequence += 1
  980. logger.info(f"进入侧分支: {branch_type}, branch_id={branch_id}")
  981. continue # 跳过本轮,下一轮开始侧分支
  982. # 构建 LLM messages(注入上下文)
  983. llm_messages = list(history)
  984. # 优化已处理的图片(分级处理:保留/压缩/描述)
  985. llm_messages = await self._optimize_images(llm_messages, config.model)
  986. # 对历史消息应用 Prompt Caching
  987. llm_messages = self._add_cache_control(
  988. llm_messages,
  989. config.model,
  990. config.enable_prompt_caching
  991. )
  992. # 调用 LLM(等待完成后再检查 cancel 信号,不中断正在进行的调用)
  993. result = await self.llm_call(
  994. messages=llm_messages,
  995. model=config.model,
  996. tools=tool_schemas,
  997. temperature=config.temperature,
  998. **config.extra_llm_params,
  999. )
  1000. response_content = result.get("content", "")
  1001. reasoning_content = result.get("reasoning_content", "")
  1002. tool_calls = result.get("tool_calls")
  1003. finish_reason = result.get("finish_reason")
  1004. prompt_tokens = result.get("prompt_tokens", 0)
  1005. completion_tokens = result.get("completion_tokens", 0)
  1006. step_cost = result.get("cost", 0)
  1007. cache_creation_tokens = result.get("cache_creation_tokens")
  1008. cache_read_tokens = result.get("cache_read_tokens")
  1009. # 周期性自动注入上下文(仅主路径)
  1010. if not side_branch_ctx and iteration % CONTEXT_INJECTION_INTERVAL == 0:
  1011. # 检查是否已经调用了 get_current_context
  1012. if tool_calls:
  1013. has_context_call = any(
  1014. tc.get("function", {}).get("name") == "get_current_context"
  1015. for tc in tool_calls
  1016. )
  1017. else:
  1018. has_context_call = False
  1019. tool_calls = []
  1020. if not has_context_call:
  1021. # 手动添加 get_current_context 工具调用
  1022. context_call_id = f"call_context_{uuid.uuid4().hex[:8]}"
  1023. tool_calls.append({
  1024. "id": context_call_id,
  1025. "type": "function",
  1026. "function": {"name": "get_current_context", "arguments": "{}"}
  1027. })
  1028. logger.info(f"[周期性注入] 自动添加 get_current_context 工具调用 (iteration={iteration})")
  1029. # 按需自动创建 root goal(仅主路径)
  1030. if not side_branch_ctx and goal_tree and not goal_tree.goals and tool_calls:
  1031. has_goal_call = any(
  1032. tc.get("function", {}).get("name") == "goal"
  1033. for tc in tool_calls
  1034. )
  1035. logger.debug(f"[Auto Root Goal] Before tool execution: goal_tree.goals={len(goal_tree.goals)}, has_goal_call={has_goal_call}, tool_calls={[tc.get('function', {}).get('name') for tc in tool_calls]}")
  1036. if not has_goal_call:
  1037. mission = goal_tree.mission
  1038. root_desc = mission[:200] if len(mission) > 200 else mission
  1039. goal_tree.add_goals(
  1040. descriptions=[root_desc],
  1041. reasons=["系统自动创建:Agent 未显式创建目标"],
  1042. parent_id=None
  1043. )
  1044. if self.trace_store:
  1045. await self.trace_store.add_goal(trace_id, goal_tree.goals[0])
  1046. await self.trace_store.update_goal_tree(trace_id, goal_tree)
  1047. logger.info(f"自动创建 root goal: {goal_tree.goals[0].id}(未自动 focus,等待模型决定)")
  1048. else:
  1049. logger.debug(f"[Auto Root Goal] 检测到 goal 工具调用,跳过自动创建")
  1050. # 获取当前 goal_id
  1051. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  1052. # 记录 assistant Message(parent_sequence 指向当前 head)
  1053. assistant_msg = Message.create(
  1054. trace_id=trace_id,
  1055. role="assistant",
  1056. sequence=sequence,
  1057. goal_id=current_goal_id,
  1058. parent_sequence=head_seq if head_seq > 0 else None,
  1059. branch_type=side_branch_ctx.type if side_branch_ctx else None,
  1060. branch_id=side_branch_ctx.branch_id if side_branch_ctx else None,
  1061. content={"text": response_content, "tool_calls": tool_calls, "reasoning_content": reasoning_content or None},
  1062. prompt_tokens=prompt_tokens,
  1063. completion_tokens=completion_tokens,
  1064. cache_creation_tokens=cache_creation_tokens,
  1065. cache_read_tokens=cache_read_tokens,
  1066. finish_reason=finish_reason,
  1067. cost=step_cost,
  1068. )
  1069. if self.trace_store:
  1070. await self.trace_store.add_message(assistant_msg)
  1071. # 记录模型使用
  1072. await self.trace_store.record_model_usage(
  1073. trace_id=trace_id,
  1074. sequence=sequence,
  1075. role="assistant",
  1076. model=config.model,
  1077. prompt_tokens=prompt_tokens,
  1078. completion_tokens=completion_tokens,
  1079. cache_read_tokens=cache_read_tokens or 0,
  1080. )
  1081. # 如果在侧分支,记录到 assistant_msg(已持久化,不需要额外维护)
  1082. yield assistant_msg
  1083. head_seq = sequence
  1084. sequence += 1
  1085. # 检查侧分支是否应该退出
  1086. if side_branch_ctx:
  1087. # 计算侧分支已执行的轮次
  1088. turns_in_branch = iteration - side_branch_ctx.start_iteration
  1089. should_exit = turns_in_branch >= side_branch_ctx.max_turns or not tool_calls
  1090. if turns_in_branch >= side_branch_ctx.max_turns:
  1091. logger.warning(
  1092. f"侧分支 {side_branch_ctx.type} 达到最大轮次 "
  1093. f"{side_branch_ctx.max_turns},强制退出"
  1094. )
  1095. if should_exit and side_branch_ctx.type == "compression":
  1096. # === 压缩侧分支退出(超时 + 正常完成统一处理)===
  1097. summary_text = ""
  1098. # 1. 从当前回复提取
  1099. if response_content:
  1100. if "[[SUMMARY]]" in response_content:
  1101. summary_text = response_content[
  1102. response_content.index("[[SUMMARY]]") + len("[[SUMMARY]]"):
  1103. ].strip()
  1104. elif response_content.strip():
  1105. summary_text = response_content.strip()
  1106. # 2. 从持久化存储按 sequence 范围查询
  1107. if not summary_text and self.trace_store:
  1108. all_messages = await self.trace_store.get_trace_messages(trace_id)
  1109. side_messages = [
  1110. m for m in all_messages
  1111. if m.sequence >= side_branch_ctx.start_sequence
  1112. ]
  1113. for msg in reversed(side_messages):
  1114. if msg.role == "assistant" and isinstance(msg.content, dict):
  1115. text = msg.content.get("text", "")
  1116. if "[[SUMMARY]]" in text:
  1117. summary_text = text[text.index("[[SUMMARY]]") + len("[[SUMMARY]]"):].strip()
  1118. break
  1119. elif text:
  1120. summary_text = text
  1121. break
  1122. # 3. 单次 LLM 调用
  1123. if not summary_text:
  1124. logger.warning("侧分支未生成有效 summary,fallback 到单次 LLM 压缩")
  1125. pre_branch_history = history[:side_branch_ctx.start_history_length]
  1126. summary_text = await self._single_turn_compress(
  1127. trace_id, pre_branch_history, goal_tree, config,
  1128. )
  1129. # 创建主路径 summary 消息并重建 history
  1130. if summary_text:
  1131. # 清理侧分支指令,防止泄露到主分支
  1132. summary_text = summary_text.replace(
  1133. "**生成摘要后立即停止,不要继续执行原有任务。**", ""
  1134. ).strip()
  1135. from agent.core.prompts import build_summary_header
  1136. summary_content = build_summary_header(summary_text)
  1137. if goal_tree and goal_tree.goals:
  1138. goal_tree_detail = goal_tree.to_prompt(include_summary=True)
  1139. summary_content += f"\n\n## Current Plan\n\n{goal_tree_detail}"
  1140. # 找第一条 user message 的 sequence 作为 parent
  1141. # 续跑时 get_main_path_messages 沿 parent 链回溯,
  1142. # 指向 first_user 可以跳过所有被压缩的中间消息
  1143. first_user_seq = None
  1144. if self.trace_store:
  1145. all_msgs = await self.trace_store.get_trace_messages(trace_id)
  1146. for m in all_msgs:
  1147. if m.role == "user":
  1148. first_user_seq = m.sequence
  1149. break
  1150. summary_msg = Message.create(
  1151. trace_id=trace_id,
  1152. role="user",
  1153. sequence=sequence,
  1154. parent_sequence=first_user_seq,
  1155. branch_type=None,
  1156. content=summary_content,
  1157. )
  1158. if self.trace_store:
  1159. await self.trace_store.add_message(summary_msg)
  1160. history = self._rebuild_history_after_compression(
  1161. history, summary_msg.to_llm_dict(), label="压缩侧分支"
  1162. )
  1163. head_seq = sequence
  1164. sequence += 1
  1165. else:
  1166. logger.error("所有压缩方案均未生成有效 summary,跳过压缩")
  1167. # 回退 history 到侧分支开始前,防止侧分支指令泄露到主分支
  1168. history = history[:side_branch_ctx.start_history_length]
  1169. head_seq = side_branch_ctx.start_head_seq
  1170. # 清理
  1171. trace.context.pop("active_side_branch", None)
  1172. config.force_side_branch = None
  1173. if self.trace_store:
  1174. await self.trace_store.update_trace(
  1175. trace_id, context=trace.context, head_sequence=head_seq,
  1176. )
  1177. side_branch_ctx = None
  1178. continue
  1179. elif should_exit and side_branch_ctx.type == "reflection":
  1180. # === 反思侧分支退出(超时 + 正常完成统一处理)===
  1181. logger.info("反思侧分支退出")
  1182. # 恢复主路径
  1183. if self.trace_store:
  1184. main_path_messages = await self.trace_store.get_main_path_messages(
  1185. trace_id, side_branch_ctx.start_head_seq
  1186. )
  1187. history = [m.to_llm_dict() for m in main_path_messages]
  1188. head_seq = side_branch_ctx.start_head_seq
  1189. # 清理
  1190. trace.context.pop("active_side_branch", None)
  1191. if not config.force_side_branch or len(config.force_side_branch) == 0:
  1192. config.force_side_branch = None
  1193. logger.info("反思完成,队列为空")
  1194. if self.trace_store:
  1195. await self.trace_store.update_trace(
  1196. trace_id, context=trace.context, head_sequence=head_seq,
  1197. )
  1198. side_branch_ctx = None
  1199. continue
  1200. # 处理工具调用
  1201. # 截断兜底:finish_reason == "length" 说明响应被 max_tokens 截断,
  1202. # tool call 参数很可能不完整,不应执行,改为提示模型分批操作
  1203. if tool_calls and finish_reason == "length":
  1204. logger.warning(
  1205. "[Runner] 响应被 max_tokens 截断,跳过 %d 个不完整的 tool calls",
  1206. len(tool_calls),
  1207. )
  1208. truncation_hint = TRUNCATION_HINT
  1209. history.append({
  1210. "role": "assistant",
  1211. "content": response_content,
  1212. "tool_calls": tool_calls,
  1213. })
  1214. # 为每个被截断的 tool call 返回错误结果
  1215. for tc in tool_calls:
  1216. history.append({
  1217. "role": "tool",
  1218. "tool_call_id": tc["id"],
  1219. "content": truncation_hint,
  1220. })
  1221. continue
  1222. if tool_calls and config.auto_execute_tools:
  1223. history.append({
  1224. "role": "assistant",
  1225. "content": response_content,
  1226. "tool_calls": tool_calls,
  1227. })
  1228. for tc in tool_calls:
  1229. current_goal_id = goal_tree.current_id if (goal_tree and goal_tree.current_id) else None
  1230. tool_name = tc["function"]["name"]
  1231. tool_args = tc["function"]["arguments"]
  1232. if isinstance(tool_args, str):
  1233. if not tool_args.strip():
  1234. tool_args = {}
  1235. else:
  1236. try:
  1237. tool_args = json.loads(tool_args)
  1238. except json.JSONDecodeError:
  1239. # 尝试修复常见的截断/格式问题
  1240. tool_args = self._try_fix_json(tool_args)
  1241. if tool_args is None:
  1242. logger.warning(f"[Tool Call] JSON 解析失败,跳过工具调用 {tool_name}: {tc['function']['arguments'][:200]}")
  1243. history.append({
  1244. "role": "tool",
  1245. "tool_call_id": tc["id"],
  1246. "content": f"Error: 工具参数 JSON 格式错误,无法解析。请重新生成正确的 JSON 参数调用此工具。原始参数: {tc['function']['arguments'][:200]}",
  1247. })
  1248. yield Message(role="tool", content={
  1249. "tool_name": tool_name,
  1250. "error": "JSON 参数解析失败",
  1251. }, description="工具参数 JSON 格式错误")
  1252. continue
  1253. elif tool_args is None:
  1254. tool_args = {}
  1255. # 记录工具调用(INFO 级别,显示参数)
  1256. args_str = json.dumps(tool_args, ensure_ascii=False)
  1257. args_display = args_str[:100] + "..." if len(args_str) > 100 else args_str
  1258. logger.info(f"[Tool Call] {tool_name}({args_display})")
  1259. _gw_tok = None
  1260. _gw = GatewayExecResolver.for_trace_context(trace.context or {})
  1261. if _gw:
  1262. _gw_tok = active_gateway_exec.set(_gw)
  1263. try:
  1264. tool_result = await self.tools.execute(
  1265. tool_name,
  1266. tool_args,
  1267. uid=config.uid or "",
  1268. context={
  1269. "uid": config.uid or "",
  1270. "store": self.trace_store,
  1271. "trace_id": trace_id,
  1272. "goal_id": current_goal_id,
  1273. "runner": self,
  1274. "goal_tree": goal_tree,
  1275. "knowledge_config": config.knowledge,
  1276. "trace_context": trace.context or {},
  1277. # 新增:侧分支信息
  1278. "side_branch": {
  1279. "type": side_branch_ctx.type,
  1280. "branch_id": side_branch_ctx.branch_id,
  1281. "is_side_branch": True,
  1282. "max_turns": side_branch_ctx.max_turns,
  1283. } if side_branch_ctx else None,
  1284. },
  1285. )
  1286. finally:
  1287. if _gw_tok is not None:
  1288. active_gateway_exec.reset(_gw_tok)
  1289. # 如果是 goal 工具,记录执行后的状态
  1290. if tool_name == "goal" and goal_tree:
  1291. logger.debug(f"[Goal Tool] After execution: goal_tree.goals={len(goal_tree.goals)}, current_id={goal_tree.current_id}")
  1292. # 跟踪保存的知识 ID
  1293. if tool_name == "knowledge_save" and isinstance(tool_result, dict):
  1294. metadata = tool_result.get("metadata", {})
  1295. knowledge_id = metadata.get("knowledge_id")
  1296. if knowledge_id:
  1297. if trace_id not in self._saved_knowledge_ids:
  1298. self._saved_knowledge_ids[trace_id] = []
  1299. self._saved_knowledge_ids[trace_id].append(knowledge_id)
  1300. logger.info(f"[Knowledge Tracking] 记录保存的知识 ID: {knowledge_id}")
  1301. # --- 支持多模态工具反馈 ---
  1302. # execute() 返回 dict{"text","images","tool_usage"} 或 str
  1303. # 统一为dict格式
  1304. if isinstance(tool_result, str):
  1305. tool_result = {"text": tool_result}
  1306. tool_text = tool_result.get("text", str(tool_result))
  1307. tool_images = tool_result.get("images", [])
  1308. tool_usage = tool_result.get("tool_usage") # 新增:提取tool_usage
  1309. # 处理多模态消息
  1310. if tool_images:
  1311. tool_result_text = tool_text
  1312. # 构建多模态消息格式
  1313. tool_content_for_llm = [{"type": "text", "text": tool_text}]
  1314. for img in tool_images:
  1315. if img.get("type") == "base64" and img.get("data"):
  1316. media_type = img.get("media_type", "image/png")
  1317. tool_content_for_llm.append({
  1318. "type": "image_url",
  1319. "image_url": {
  1320. "url": f"data:{media_type};base64,{img['data']}"
  1321. }
  1322. })
  1323. elif img.get("type") == "url" and img.get("url"):
  1324. tool_content_for_llm.append({
  1325. "type": "image_url",
  1326. "image_url": {
  1327. "url": img["url"]
  1328. }
  1329. })
  1330. img_count = len(tool_content_for_llm) - 1 # 减去 text 块
  1331. print(f"[Runner] 多模态工具反馈: tool={tool_name}, images={img_count}, text_len={len(tool_result_text)}")
  1332. else:
  1333. tool_result_text = tool_text
  1334. tool_content_for_llm = tool_text
  1335. tool_msg = Message.create(
  1336. trace_id=trace_id,
  1337. role="tool",
  1338. sequence=sequence,
  1339. goal_id=current_goal_id,
  1340. parent_sequence=head_seq,
  1341. tool_call_id=tc["id"],
  1342. branch_type=side_branch_ctx.type if side_branch_ctx else None,
  1343. branch_id=side_branch_ctx.branch_id if side_branch_ctx else None,
  1344. # 存储完整内容:有图片时保留 list(含 image_url),纯文本时存字符串
  1345. content={"tool_name": tool_name, "result": tool_content_for_llm},
  1346. )
  1347. if self.trace_store:
  1348. await self.trace_store.add_message(tool_msg)
  1349. # 记录工具的模型使用
  1350. if tool_usage:
  1351. await self.trace_store.record_model_usage(
  1352. trace_id=trace_id,
  1353. sequence=sequence,
  1354. role="tool",
  1355. tool_name=tool_name,
  1356. model=tool_usage.get("model"),
  1357. prompt_tokens=tool_usage.get("prompt_tokens", 0),
  1358. completion_tokens=tool_usage.get("completion_tokens", 0),
  1359. cache_read_tokens=tool_usage.get("cache_read_tokens", 0),
  1360. )
  1361. # 截图单独存为同名 PNG 文件
  1362. if tool_images:
  1363. import base64 as b64mod
  1364. for img in tool_images:
  1365. if img.get("data"):
  1366. png_path = self.trace_store._get_messages_dir(trace_id) / f"{tool_msg.message_id}.png"
  1367. png_path.write_bytes(b64mod.b64decode(img["data"]))
  1368. print(f"[Runner] 截图已保存: {png_path.name}")
  1369. break # 只存第一张
  1370. # 如果在侧分支,tool_msg 已持久化(不需要额外维护)
  1371. yield tool_msg
  1372. head_seq = sequence
  1373. sequence += 1
  1374. history.append({
  1375. "role": "tool",
  1376. "tool_call_id": tc["id"],
  1377. "name": tool_name,
  1378. "content": tool_content_for_llm,
  1379. })
  1380. # on_complete 模式:goal(done=...) 后立即压缩该 goal 的消息
  1381. if (
  1382. not side_branch_ctx
  1383. and config.goal_compression == "on_complete"
  1384. and self.trace_store
  1385. and goal_tree
  1386. ):
  1387. has_goal_done = False
  1388. for tc in tool_calls:
  1389. if tc["function"]["name"] != "goal":
  1390. continue
  1391. try:
  1392. raw = tc["function"]["arguments"]
  1393. args = json.loads(raw) if isinstance(raw, str) and raw.strip() else {}
  1394. except (json.JSONDecodeError, TypeError):
  1395. args = {}
  1396. if args.get("done") is not None:
  1397. has_goal_done = True
  1398. break
  1399. if has_goal_done:
  1400. main_path_msgs = await self.trace_store.get_main_path_messages(
  1401. trace_id, head_seq
  1402. )
  1403. compressed_msgs = compress_completed_goals(main_path_msgs, goal_tree)
  1404. if len(compressed_msgs) < len(main_path_msgs):
  1405. logger.info(
  1406. "on_complete 压缩: %d -> %d 条消息",
  1407. len(main_path_msgs), len(compressed_msgs),
  1408. )
  1409. history = [msg.to_llm_dict() for msg in compressed_msgs]
  1410. continue # 继续循环
  1411. # 无工具调用
  1412. # 如果在侧分支中,已经在上面处理过了(不会走到这里)
  1413. # 主路径无工具调用 → 任务完成,检查是否需要完成后反思
  1414. if not side_branch_ctx and config.knowledge.enable_completion_extraction and not break_after_side_branch:
  1415. config.force_side_branch = ["reflection"]
  1416. break_after_side_branch = True
  1417. logger.info("任务完成,进入完成后反思侧分支")
  1418. continue
  1419. break
  1420. # 清理 trace 相关的跟踪数据
  1421. self._context_warned.pop(trace_id, None)
  1422. self._context_usage.pop(trace_id, None)
  1423. self._saved_knowledge_ids.pop(trace_id, None)
  1424. # 更新 head_sequence 并完成 Trace
  1425. if self.trace_store:
  1426. await self.trace_store.update_trace(
  1427. trace_id,
  1428. status="completed",
  1429. head_sequence=head_seq,
  1430. completed_at=datetime.now(),
  1431. )
  1432. trace_obj = await self.trace_store.get_trace(trace_id)
  1433. if trace_obj:
  1434. yield trace_obj
  1435. # ===== 压缩辅助方法 =====
  1436. def _rebuild_history_after_compression(
  1437. self,
  1438. history: List[Dict],
  1439. summary_msg_dict: Dict,
  1440. label: str = "压缩",
  1441. ) -> List[Dict]:
  1442. """
  1443. 压缩后重建 history:system prompt + 第一条 user message + summary
  1444. Args:
  1445. history: 压缩前的 history
  1446. summary_msg_dict: summary 消息的 LLM dict
  1447. label: 日志标签
  1448. Returns:
  1449. 新的 history
  1450. """
  1451. system_msg = None
  1452. first_user_msg = None
  1453. for msg in history:
  1454. if msg.get("role") == "system" and not system_msg:
  1455. system_msg = msg
  1456. elif msg.get("role") == "user" and not first_user_msg:
  1457. first_user_msg = msg
  1458. if system_msg and first_user_msg:
  1459. break
  1460. new_history = []
  1461. if system_msg:
  1462. new_history.append(system_msg)
  1463. if first_user_msg:
  1464. new_history.append(first_user_msg)
  1465. new_history.append(summary_msg_dict)
  1466. logger.info(f"{label}完成: {len(history)} → {len(new_history)} 条消息")
  1467. for idx, msg in enumerate(new_history):
  1468. role = msg.get("role", "unknown")
  1469. content = msg.get("content", "")
  1470. if isinstance(content, str):
  1471. preview = content
  1472. elif isinstance(content, list):
  1473. preview = f"[{len(content)} blocks]"
  1474. else:
  1475. preview = str(content)
  1476. logger.info(f" {label}后[{idx}] {role}: {preview}")
  1477. return new_history
  1478. # ===== 回溯(Rewind)=====
  1479. async def _rewind(
  1480. self,
  1481. trace_id: str,
  1482. after_sequence: int,
  1483. goal_tree: Optional[GoalTree],
  1484. ) -> int:
  1485. """
  1486. 执行回溯:快照 GoalTree,重建干净树,设置 head_sequence
  1487. 新消息的 parent_sequence 将指向 rewind 点,旧消息通过树结构自然脱离主路径。
  1488. Returns:
  1489. 下一个可用的 sequence 号
  1490. """
  1491. if not self.trace_store:
  1492. raise ValueError("trace_store required for rewind")
  1493. # 1. 加载所有 messages(用于 safe cutoff 和 max sequence)
  1494. all_messages = await self.trace_store.get_trace_messages(trace_id)
  1495. if not all_messages:
  1496. return 1
  1497. # 2. 找到安全截断点(确保不截断在 tool_call 和 tool response 之间)
  1498. cutoff = self._find_safe_cutoff(all_messages, after_sequence)
  1499. # 3. 快照并重建 GoalTree
  1500. if goal_tree:
  1501. # 获取截断点消息的 created_at 作为时间界限
  1502. cutoff_msg = None
  1503. for msg in all_messages:
  1504. if msg.sequence == cutoff:
  1505. cutoff_msg = msg
  1506. break
  1507. cutoff_time = cutoff_msg.created_at if cutoff_msg else datetime.now()
  1508. # 快照到 events(含 head_sequence 供前端感知分支切换)
  1509. await self.trace_store.append_event(trace_id, "rewind", {
  1510. "after_sequence": cutoff,
  1511. "head_sequence": cutoff,
  1512. "goal_tree_snapshot": goal_tree.to_dict(),
  1513. })
  1514. # 按时间重建干净的 GoalTree
  1515. new_tree = goal_tree.rebuild_for_rewind(cutoff_time)
  1516. await self.trace_store.update_goal_tree(trace_id, new_tree)
  1517. # 更新内存中的引用
  1518. goal_tree.goals = new_tree.goals
  1519. goal_tree.current_id = new_tree.current_id
  1520. # 4. 更新 head_sequence 到 rewind 点
  1521. await self.trace_store.update_trace(trace_id, head_sequence=cutoff)
  1522. # 5. 返回 next sequence(全局递增,不复用)
  1523. max_seq = max((m.sequence for m in all_messages), default=0)
  1524. return max_seq + 1
  1525. def _find_safe_cutoff(self, messages: List[Message], after_sequence: int) -> int:
  1526. """
  1527. 找到安全的截断点。
  1528. 如果 after_sequence 指向一条带 tool_calls 的 assistant message,
  1529. 则自动扩展到其所有对应的 tool response 之后。
  1530. """
  1531. cutoff = after_sequence
  1532. # 找到 after_sequence 对应的 message
  1533. target_msg = None
  1534. for msg in messages:
  1535. if msg.sequence == after_sequence:
  1536. target_msg = msg
  1537. break
  1538. if not target_msg:
  1539. return cutoff
  1540. # 如果是 assistant 且有 tool_calls,找到所有对应的 tool responses
  1541. if target_msg.role == "assistant":
  1542. content = target_msg.content
  1543. if isinstance(content, dict) and content.get("tool_calls"):
  1544. tool_call_ids = set()
  1545. for tc in content["tool_calls"]:
  1546. if isinstance(tc, dict) and tc.get("id"):
  1547. tool_call_ids.add(tc["id"])
  1548. # 找到这些 tool_call 对应的 tool messages
  1549. for msg in messages:
  1550. if (msg.role == "tool" and msg.tool_call_id
  1551. and msg.tool_call_id in tool_call_ids):
  1552. cutoff = max(cutoff, msg.sequence)
  1553. return cutoff
  1554. async def _heal_orphaned_tool_calls(
  1555. self,
  1556. messages: List[Message],
  1557. trace_id: str,
  1558. goal_tree: Optional[GoalTree],
  1559. sequence: int,
  1560. ) -> tuple:
  1561. """
  1562. 检测并修复消息历史中的 orphaned tool_calls。
  1563. 当 agent 被 stop/crash 中断时,可能有 assistant 的 tool_calls 没有对应的
  1564. tool results(包括多 tool_call 部分完成的情况)。直接发给 LLM 会导致 400。
  1565. 修复策略:为每个缺失的 tool_result 插入合成的"中断通知"消息,而非裁剪。
  1566. - 普通工具:简短中断提示
  1567. - agent/evaluate:包含 sub_trace_id、执行统计、continue_from 指引
  1568. 合成消息持久化到 store,确保幂等(下次续跑不再触发)。
  1569. Returns:
  1570. (healed_messages, next_sequence)
  1571. """
  1572. if not messages:
  1573. return messages, sequence
  1574. # 收集所有 tool_call IDs → (assistant_msg, tool_call_dict)
  1575. tc_map: Dict[str, tuple] = {}
  1576. result_ids: set = set()
  1577. for msg in messages:
  1578. if msg.role == "assistant":
  1579. content = msg.content
  1580. if isinstance(content, dict) and content.get("tool_calls"):
  1581. for tc in content["tool_calls"]:
  1582. tc_id = tc.get("id")
  1583. if tc_id:
  1584. tc_map[tc_id] = (msg, tc)
  1585. elif msg.role == "tool" and msg.tool_call_id:
  1586. result_ids.add(msg.tool_call_id)
  1587. orphaned_ids = [tc_id for tc_id in tc_map if tc_id not in result_ids]
  1588. if not orphaned_ids:
  1589. return messages, sequence
  1590. logger.info(
  1591. "检测到 %d 个 orphaned tool_calls,生成合成中断通知",
  1592. len(orphaned_ids),
  1593. )
  1594. healed = list(messages)
  1595. head_seq = messages[-1].sequence
  1596. for tc_id in orphaned_ids:
  1597. assistant_msg, tc = tc_map[tc_id]
  1598. tool_name = tc.get("function", {}).get("name", "unknown")
  1599. if tool_name in ("agent", "evaluate"):
  1600. result_text = self._build_agent_interrupted_result(
  1601. tc, goal_tree, assistant_msg,
  1602. )
  1603. else:
  1604. result_text = build_tool_interrupted_message(tool_name)
  1605. synthetic_msg = Message.create(
  1606. trace_id=trace_id,
  1607. role="tool",
  1608. sequence=sequence,
  1609. goal_id=assistant_msg.goal_id,
  1610. parent_sequence=head_seq,
  1611. tool_call_id=tc_id,
  1612. content={"tool_name": tool_name, "result": result_text},
  1613. )
  1614. if self.trace_store:
  1615. await self.trace_store.add_message(synthetic_msg)
  1616. healed.append(synthetic_msg)
  1617. head_seq = sequence
  1618. sequence += 1
  1619. # 更新 trace head/last sequence
  1620. if self.trace_store:
  1621. await self.trace_store.update_trace(
  1622. trace_id,
  1623. head_sequence=head_seq,
  1624. last_sequence=max(head_seq, sequence - 1),
  1625. )
  1626. return healed, sequence
  1627. def _build_agent_interrupted_result(
  1628. self,
  1629. tc: Dict,
  1630. goal_tree: Optional[GoalTree],
  1631. assistant_msg: Message,
  1632. ) -> str:
  1633. """为中断的 agent/evaluate 工具调用构建合成结果(对齐正常返回值格式)"""
  1634. args_str = tc.get("function", {}).get("arguments", "{}")
  1635. try:
  1636. args = json.loads(args_str) if isinstance(args_str, str) else args_str
  1637. except json.JSONDecodeError:
  1638. args = {}
  1639. task = args.get("task", "未知任务")
  1640. if isinstance(task, list):
  1641. task = "; ".join(task)
  1642. tool_name = tc.get("function", {}).get("name", "agent")
  1643. mode = "evaluate" if tool_name == "evaluate" else "delegate"
  1644. # 从 goal_tree 查找 sub_trace 信息
  1645. sub_trace_id = None
  1646. stats = None
  1647. if goal_tree and assistant_msg.goal_id:
  1648. goal = goal_tree.find(assistant_msg.goal_id)
  1649. if goal and goal.sub_trace_ids:
  1650. first = goal.sub_trace_ids[0]
  1651. if isinstance(first, dict):
  1652. sub_trace_id = first.get("trace_id")
  1653. elif isinstance(first, str):
  1654. sub_trace_id = first
  1655. if goal.cumulative_stats:
  1656. s = goal.cumulative_stats
  1657. if s.message_count > 0:
  1658. stats = {
  1659. "message_count": s.message_count,
  1660. "total_tokens": s.total_tokens,
  1661. "total_cost": round(s.total_cost, 4),
  1662. }
  1663. result: Dict[str, Any] = {
  1664. "mode": mode,
  1665. "status": "interrupted",
  1666. "summary": AGENT_INTERRUPTED_SUMMARY,
  1667. "task": task,
  1668. }
  1669. if sub_trace_id:
  1670. result["sub_trace_id"] = sub_trace_id
  1671. result["hint"] = build_agent_continue_hint(sub_trace_id)
  1672. if stats:
  1673. result["stats"] = stats
  1674. return json.dumps(result, ensure_ascii=False, indent=2)
  1675. # ===== 上下文注入 =====
  1676. def _build_context_injection(
  1677. self,
  1678. trace: Trace,
  1679. goal_tree: Optional[GoalTree],
  1680. ) -> str:
  1681. """构建周期性注入的上下文(GoalTree + Active Collaborators + Focus 提醒)"""
  1682. from datetime import datetime
  1683. parts = [f"## Current Time\n\n{datetime.now().strftime('%Y-%m-%d %H:%M:%S')}"]
  1684. # GoalTree
  1685. if goal_tree and goal_tree.goals:
  1686. parts.append(f"## Current Plan\n\n{goal_tree.to_prompt()}")
  1687. if goal_tree.current_id:
  1688. # 检测 focus 在有子节点的父目标上:提醒模型 focus 到具体子目标
  1689. children = goal_tree.get_children(goal_tree.current_id)
  1690. pending_children = [c for c in children if c.status in ("pending", "in_progress")]
  1691. if pending_children:
  1692. child_ids = ", ".join(
  1693. goal_tree._generate_display_id(c) for c in pending_children[:3]
  1694. )
  1695. parts.append(
  1696. f"**提醒**:当前焦点在父目标上,建议用 `goal(focus=\"...\")` "
  1697. f"切换到具体子目标(如 {child_ids})再执行。"
  1698. )
  1699. else:
  1700. # 无焦点:提醒模型 focus
  1701. parts.append(
  1702. "**提醒**:当前没有焦点目标。请用 `goal(focus=\"...\")` 选择一个目标开始执行。"
  1703. )
  1704. # Active Collaborators
  1705. collaborators = trace.context.get("collaborators", [])
  1706. if collaborators:
  1707. lines = ["## Active Collaborators"]
  1708. for c in collaborators:
  1709. status_str = c.get("status", "unknown")
  1710. ctype = c.get("type", "agent")
  1711. summary = c.get("summary", "")
  1712. name = c.get("name", "unnamed")
  1713. lines.append(f"- {name} [{ctype}, {status_str}]: {summary}")
  1714. parts.append("\n".join(lines))
  1715. return "\n\n".join(parts)
  1716. # ===== 辅助方法 =====
  1717. async def _optimize_images(self, messages: List[Dict], model: str) -> List[Dict]:
  1718. """
  1719. 分级优化已处理的图片,节省 token
  1720. 策略(基于图片距离最后一条 assistant 的"轮次"):
  1721. 1. 最近 1-2 轮:保留原图
  1722. 2. 3-5 轮:降低分辨率和压缩(节省 token 但保留视觉信息)
  1723. 3. 5 轮以上:调用小模型生成文本描述 + 保留 URL
  1724. 处理结果会缓存,避免重复的 PIL 解码/编码和 LLM 调用。
  1725. Args:
  1726. messages: 原始消息列表
  1727. model: 当前使用的模型(用于选择描述生成模型)
  1728. Returns:
  1729. 优化后的消息列表(深拷贝)
  1730. """
  1731. if not messages:
  1732. return messages
  1733. # 找到最后一条 assistant message 的位置
  1734. last_assistant_idx = -1
  1735. for i in range(len(messages) - 1, -1, -1):
  1736. if messages[i].get("role") == "assistant":
  1737. last_assistant_idx = i
  1738. break
  1739. # 如果没有 assistant message,说明还没开始对话,不优化
  1740. if last_assistant_idx == -1:
  1741. return messages
  1742. # 统计从每个位置到最后一条 assistant 之间的 assistant 数量(作为"轮次")
  1743. assistant_count_after = [0] * len(messages)
  1744. count = 0
  1745. for i in range(len(messages) - 1, -1, -1):
  1746. assistant_count_after[i] = count
  1747. if messages[i].get("role") == "assistant":
  1748. count += 1
  1749. # 深拷贝避免修改原始数据
  1750. import copy
  1751. import hashlib
  1752. import asyncio
  1753. messages = copy.deepcopy(messages)
  1754. # 统计优化情况
  1755. stats = {"kept": 0, "downscaled": 0, "described": 0, "cache_hit": 0}
  1756. # 收集需要降分辨率的图片(用于并发处理)
  1757. downscale_jobs = [] # [(msg_idx, block_idx, image_url, cache_key)]
  1758. # 第一遍:扫描并收集需要处理的图片
  1759. for i in range(last_assistant_idx):
  1760. msg = messages[i]
  1761. if msg.get("role") != "tool":
  1762. continue
  1763. content = msg.get("content")
  1764. if not isinstance(content, list):
  1765. continue
  1766. rounds_ago = assistant_count_after[i]
  1767. for block_idx, block in enumerate(content):
  1768. if isinstance(block, dict) and block.get("type") == "image_url":
  1769. image_url_obj = block.get("image_url", {})
  1770. image_url = image_url_obj.get("url", "")
  1771. if image_url.startswith("data:"):
  1772. cache_key = hashlib.md5(image_url[:200].encode()).hexdigest()
  1773. else:
  1774. cache_key = hashlib.md5(image_url.encode()).hexdigest()
  1775. # 3-5 轮需要降分辨率
  1776. if 2 < rounds_ago <= 5:
  1777. cached = self._image_opt_cache.get(cache_key, {})
  1778. if "downscaled" not in cached and image_url.startswith("data:"):
  1779. downscale_jobs.append((i, block_idx, image_url, cache_key))
  1780. # 并发处理所有降分辨率任务
  1781. if downscale_jobs:
  1782. downscale_results = await asyncio.gather(
  1783. *[self._downscale_image(url) for _, _, url, _ in downscale_jobs],
  1784. return_exceptions=True
  1785. )
  1786. for (_, _, _, cache_key), result in zip(downscale_jobs, downscale_results):
  1787. if not isinstance(result, Exception) and result is not None:
  1788. self._image_opt_cache.setdefault(cache_key, {})["downscaled"] = result
  1789. # 第二遍:应用处理结果
  1790. for i in range(last_assistant_idx):
  1791. msg = messages[i]
  1792. if msg.get("role") != "tool":
  1793. continue
  1794. content = msg.get("content")
  1795. if not isinstance(content, list):
  1796. continue
  1797. # 计算这条消息距离最后一条 assistant 的"轮次"
  1798. rounds_ago = assistant_count_after[i]
  1799. # 处理每个 content block
  1800. new_content = []
  1801. for block in content:
  1802. if isinstance(block, dict) and block.get("type") == "image_url":
  1803. image_url_obj = block.get("image_url", {})
  1804. image_url = image_url_obj.get("url", "")
  1805. # 生成缓存 key(URL 图片用 URL 本身,base64 用前 64 字符 hash)
  1806. if image_url.startswith("data:"):
  1807. cache_key = hashlib.md5(image_url[:200].encode()).hexdigest()
  1808. else:
  1809. cache_key = hashlib.md5(image_url.encode()).hexdigest()
  1810. # 根据距离决定处理策略
  1811. if rounds_ago <= 2:
  1812. # 最近 1-2 轮:保留原图
  1813. new_content.append(block)
  1814. stats["kept"] += 1
  1815. elif rounds_ago <= 5:
  1816. # 3-5 轮:降低分辨率(优先从缓存取)
  1817. cached = self._image_opt_cache.get(cache_key, {})
  1818. if "downscaled" in cached:
  1819. new_content.append({
  1820. "type": "image_url",
  1821. "image_url": {"url": cached["downscaled"]}
  1822. })
  1823. stats["downscaled"] += 1
  1824. stats["cache_hit"] += 1
  1825. elif image_url.startswith("data:"):
  1826. downscaled = await self._downscale_image(image_url)
  1827. if downscaled:
  1828. # 缓存结果
  1829. self._image_opt_cache.setdefault(cache_key, {})["downscaled"] = downscaled
  1830. new_content.append({
  1831. "type": "image_url",
  1832. "image_url": {"url": downscaled}
  1833. })
  1834. stats["downscaled"] += 1
  1835. else:
  1836. new_content.append(block)
  1837. stats["kept"] += 1
  1838. else:
  1839. # URL 图片:无法直接处理,保留原图
  1840. new_content.append(block)
  1841. stats["kept"] += 1
  1842. else:
  1843. # 5 轮以上:生成文本描述(优先从缓存取)
  1844. cached = self._image_opt_cache.get(cache_key, {})
  1845. if "description" in cached:
  1846. new_content.append(cached["description"])
  1847. stats["described"] += 1
  1848. stats["cache_hit"] += 1
  1849. else:
  1850. description = await self._generate_image_description(image_url, model)
  1851. url_info = f" (URL: {image_url[:100]}...)" if not image_url.startswith("data:") else ""
  1852. desc_block = {
  1853. "type": "text",
  1854. "text": f"[Image description: {description}]{url_info}"
  1855. }
  1856. # 缓存结果
  1857. self._image_opt_cache.setdefault(cache_key, {})["description"] = desc_block
  1858. new_content.append(desc_block)
  1859. stats["described"] += 1
  1860. else:
  1861. new_content.append(block)
  1862. msg["content"] = new_content
  1863. print(f"[Image Opt Check] 扫描到 {stats['kept'] + stats['downscaled'] + stats['described']} 张图片上下文")
  1864. if stats["downscaled"] > 0 or stats["described"] > 0:
  1865. logger.info(
  1866. f"[Image Optimization] 保留 {stats['kept']} 张,"
  1867. f"降分辨率 {stats['downscaled']} 张,"
  1868. f"文本描述 {stats['described']} 张,"
  1869. f"缓存命中 {stats['cache_hit']} 次"
  1870. )
  1871. return messages
  1872. async def _downscale_image(self, base64_url: str, max_size: int = 512) -> Optional[str]:
  1873. """
  1874. 降低 base64 图片的分辨率
  1875. Args:
  1876. base64_url: data:image/xxx;base64,... 格式的 URL
  1877. max_size: 最大边长(像素)
  1878. Returns:
  1879. 降分辨率后的 base64 URL,失败返回 None
  1880. """
  1881. try:
  1882. from PIL import Image
  1883. import io
  1884. import base64
  1885. # 解析 base64 数据
  1886. if not base64_url.startswith("data:"):
  1887. return None
  1888. header, data = base64_url.split(",", 1)
  1889. media_type = header.split(";")[0].split(":")[1] # image/png
  1890. # 解码图片
  1891. img_data = base64.b64decode(data)
  1892. img = Image.open(io.BytesIO(img_data))
  1893. # 计算新尺寸(保持宽高比)
  1894. width, height = img.size
  1895. if width <= max_size and height <= max_size:
  1896. return base64_url # 已经够小,不需要缩放
  1897. if width > height:
  1898. new_width = max_size
  1899. new_height = int(height * max_size / width)
  1900. else:
  1901. new_height = max_size
  1902. new_width = int(width * max_size / height)
  1903. # 缩放图片(使用更快的 BILINEAR 算法)
  1904. img_resized = img.resize((new_width, new_height), Image.Resampling.BILINEAR)
  1905. # 转换为 RGB(如果是 RGBA)
  1906. if img_resized.mode == "RGBA":
  1907. img_resized = img_resized.convert("RGB")
  1908. # 重新编码为 JPEG(降低质量以加快速度)
  1909. buffer = io.BytesIO()
  1910. img_resized.save(buffer, format="JPEG", quality=60, optimize=False)
  1911. new_data = base64.b64encode(buffer.getvalue()).decode("utf-8")
  1912. return f"data:image/jpeg;base64,{new_data}"
  1913. except Exception as e:
  1914. logger.warning(f"[Image Downscale] 降分辨率失败: {e}")
  1915. return None
  1916. async def _generate_image_description(self, image_url: str, current_model: str) -> str:
  1917. """
  1918. 使用小模型生成图片的文本描述
  1919. Args:
  1920. image_url: 图片 URL(base64 或 http(s))
  1921. current_model: 当前使用的模型
  1922. Returns:
  1923. 图片描述文本
  1924. """
  1925. try:
  1926. # 使用 qwen-vl-max(通义千问视觉模型)生成描述
  1927. # 注意:qwen-vl 系列专门支持视觉输入
  1928. description_model = "qwen-vl-max"
  1929. # 构建描述请求
  1930. messages = [
  1931. {
  1932. "role": "user",
  1933. "content": [
  1934. {
  1935. "type": "image_url",
  1936. "image_url": {"url": image_url}
  1937. },
  1938. {
  1939. "type": "text",
  1940. "text": "请用 1-2 句话简洁描述这张图片的主要内容。"
  1941. }
  1942. ]
  1943. }
  1944. ]
  1945. # 调用 LLM
  1946. result = await self.llm_call(
  1947. messages=messages,
  1948. model=description_model,
  1949. tools=None,
  1950. temperature=0.3,
  1951. )
  1952. description = result.get("content", "").strip()
  1953. return description if description else "图片内容"
  1954. except Exception as e:
  1955. logger.warning(f"[Image Description] 生成描述失败: {e}")
  1956. return "图片内容"
  1957. def _add_cache_control(
  1958. self,
  1959. messages: List[Dict],
  1960. model: str,
  1961. enable: bool
  1962. ) -> List[Dict]:
  1963. """
  1964. 为支持的模型添加 Prompt Caching 标记
  1965. 策略:固定位置 + 延迟缓存
  1966. 1. 如果有未处理的图片(最后一条 assistant 之后的 tool messages 中有图片),跳过缓存
  1967. 2. system message 添加缓存(如果足够长)
  1968. 3. 固定位置缓存点(20, 40, 60, 80),确保每个缓存点间隔 >= 1024 tokens
  1969. 4. 最多使用 4 个缓存点(含 system)
  1970. Args:
  1971. messages: 原始消息列表
  1972. model: 模型名称
  1973. enable: 是否启用缓存
  1974. Returns:
  1975. 添加了 cache_control 的消息列表(深拷贝)
  1976. """
  1977. if not enable:
  1978. return messages
  1979. # 只对 Claude 模型启用
  1980. if "claude" not in model.lower():
  1981. return messages
  1982. # 延迟缓存:检查是否有未处理的图片
  1983. last_assistant_idx = -1
  1984. for i in range(len(messages) - 1, -1, -1):
  1985. if messages[i].get("role") == "assistant":
  1986. last_assistant_idx = i
  1987. break
  1988. # 检查最后一条 assistant 之后是否有包含图片的 tool messages
  1989. has_unprocessed_images = False
  1990. if last_assistant_idx >= 0:
  1991. for i in range(last_assistant_idx + 1, len(messages)):
  1992. msg = messages[i]
  1993. if msg.get("role") == "tool":
  1994. content = msg.get("content")
  1995. if isinstance(content, list):
  1996. has_unprocessed_images = any(
  1997. isinstance(block, dict) and block.get("type") == "image_url"
  1998. for block in content
  1999. )
  2000. if has_unprocessed_images:
  2001. break
  2002. if has_unprocessed_images:
  2003. logger.debug("[Cache] 检测到未处理的图片,延迟缓存建立")
  2004. return messages
  2005. # 深拷贝避免修改原始数据
  2006. import copy
  2007. messages = copy.deepcopy(messages)
  2008. # 策略 1: 为 system message 添加缓存
  2009. system_cached = False
  2010. for msg in messages:
  2011. if msg.get("role") == "system":
  2012. content = msg.get("content", "")
  2013. if isinstance(content, str) and len(content) > 1000:
  2014. msg["content"] = [{
  2015. "type": "text",
  2016. "text": content,
  2017. "cache_control": {"type": "ephemeral"}
  2018. }]
  2019. system_cached = True
  2020. logger.debug(f"[Cache] 为 system message 添加缓存标记 (len={len(content)})")
  2021. break
  2022. # 策略 2: 固定位置缓存点
  2023. CACHE_INTERVAL = 20
  2024. MAX_POINTS = 3 if system_cached else 4
  2025. MIN_TOKENS = 1024
  2026. AVG_TOKENS_PER_MSG = 70
  2027. total_msgs = len(messages)
  2028. if total_msgs == 0:
  2029. return messages
  2030. cache_positions = []
  2031. last_cache_pos = 0
  2032. for i in range(1, MAX_POINTS + 1):
  2033. target_pos = i * CACHE_INTERVAL - 1 # 19, 39, 59, 79
  2034. if target_pos >= total_msgs:
  2035. break
  2036. # 从目标位置开始查找合适的 user/assistant 消息
  2037. for j in range(target_pos, total_msgs):
  2038. msg = messages[j]
  2039. if msg.get("role") not in ("user", "assistant"):
  2040. continue
  2041. content = msg.get("content", "")
  2042. if not content:
  2043. continue
  2044. # 检查 content 是否非空
  2045. is_valid = False
  2046. if isinstance(content, str):
  2047. is_valid = len(content) > 0
  2048. elif isinstance(content, list):
  2049. is_valid = any(
  2050. isinstance(block, dict) and
  2051. block.get("type") == "text" and
  2052. len(block.get("text", "")) > 0
  2053. for block in content
  2054. )
  2055. if not is_valid:
  2056. continue
  2057. # 检查 token 距离
  2058. msg_count = j - last_cache_pos
  2059. estimated_tokens = msg_count * AVG_TOKENS_PER_MSG
  2060. if estimated_tokens >= MIN_TOKENS:
  2061. cache_positions.append(j)
  2062. last_cache_pos = j
  2063. logger.debug(f"[Cache] 在位置 {j} 添加缓存点 (估算 {estimated_tokens} tokens)")
  2064. break
  2065. # 应用缓存标记
  2066. for idx in cache_positions:
  2067. msg = messages[idx]
  2068. content = msg.get("content", "")
  2069. if isinstance(content, str):
  2070. msg["content"] = [{
  2071. "type": "text",
  2072. "text": content,
  2073. "cache_control": {"type": "ephemeral"}
  2074. }]
  2075. logger.debug(f"[Cache] 为 message[{idx}] ({msg.get('role')}) 添加缓存标记")
  2076. elif isinstance(content, list):
  2077. # 在最后一个 text block 添加 cache_control
  2078. for block in reversed(content):
  2079. if isinstance(block, dict) and block.get("type") == "text":
  2080. block["cache_control"] = {"type": "ephemeral"}
  2081. logger.debug(f"[Cache] 为 message[{idx}] ({msg.get('role')}) 添加缓存标记")
  2082. break
  2083. logger.debug(
  2084. f"[Cache] 总消息: {total_msgs}, "
  2085. f"缓存点: {len(cache_positions)} at {cache_positions}"
  2086. )
  2087. return messages
  2088. def _get_tool_schemas(self, tools: Optional[List[str]]) -> List[Dict]:
  2089. """
  2090. 获取工具 Schema
  2091. - tools=None: 使用 registry 中全部已注册工具(含内置 + 外部注册的)
  2092. - tools=["a", "b"]: 在 BUILTIN_TOOLS 基础上追加指定工具
  2093. """
  2094. if tools is None:
  2095. # 全部已注册工具
  2096. tool_names = self.tools.get_tool_names()
  2097. else:
  2098. # BUILTIN_TOOLS + 显式指定的额外工具
  2099. tool_names = BUILTIN_TOOLS.copy()
  2100. for t in tools:
  2101. if t not in tool_names:
  2102. tool_names.append(t)
  2103. return self.tools.get_schemas(tool_names)
  2104. # 默认 system prompt 前缀(当 config.system_prompt 和前端都未提供 system message 时使用)
  2105. # 注意:此常量已迁移到 agent.core.prompts,这里保留引用以保持向后兼容
  2106. async def _build_system_prompt(self, config: RunConfig, base_prompt: Optional[str] = None) -> Optional[str]:
  2107. """构建 system prompt(注入 skills)
  2108. 优先级:
  2109. 1. base_prompt(来自消息)
  2110. 2. config.system_prompt(显式指定)
  2111. 3. preset.system_prompt(预设的完整 system prompt)
  2112. 4. 默认模板 + skills
  2113. Skills 注入优先级:
  2114. 1. config.skills 显式指定 → 按名称过滤
  2115. 2. config.skills 为 None → 查 preset 的默认 skills 列表
  2116. 3. preset 也无 skills(None)→ 加载全部(向后兼容)
  2117. Args:
  2118. base_prompt: 已有 system 内容(来自消息),
  2119. None 时使用 config.system_prompt 或 preset.system_prompt
  2120. """
  2121. from agent.core.presets import AGENT_PRESETS
  2122. # 确定 system_prompt 来源
  2123. if base_prompt is not None:
  2124. system_prompt = base_prompt
  2125. elif config.system_prompt is not None:
  2126. system_prompt = config.system_prompt
  2127. else:
  2128. # 尝试从 preset 获取 system_prompt
  2129. preset = AGENT_PRESETS.get(config.agent_type)
  2130. system_prompt = preset.system_prompt if preset and preset.system_prompt else None
  2131. # 确定要加载哪些 skills
  2132. skills_filter: Optional[List[str]] = config.skills
  2133. if skills_filter is None:
  2134. preset = AGENT_PRESETS.get(config.agent_type)
  2135. if preset is not None:
  2136. skills_filter = preset.skills # 可能仍为 None(加载全部)
  2137. # 加载并过滤
  2138. all_skills = load_skills_from_dir(self.skills_dir)
  2139. if skills_filter is not None:
  2140. skills = [s for s in all_skills if s.name in skills_filter]
  2141. else:
  2142. skills = all_skills
  2143. skills_text = self._format_skills(skills) if skills else ""
  2144. if system_prompt:
  2145. if skills_text:
  2146. system_prompt += f"\n\n## Skills\n{skills_text}"
  2147. else:
  2148. system_prompt = DEFAULT_SYSTEM_PREFIX
  2149. if skills_text:
  2150. system_prompt += f"\n\n## Skills\n{skills_text}"
  2151. return system_prompt
  2152. async def _generate_task_name(self, messages: List[Dict]) -> str:
  2153. """生成任务名称:优先使用 utility_llm,fallback 到文本截取"""
  2154. # 提取 messages 中的文本内容
  2155. text_parts = []
  2156. for msg in messages:
  2157. content = msg.get("content", "")
  2158. if isinstance(content, str):
  2159. text_parts.append(content)
  2160. elif isinstance(content, list):
  2161. for part in content:
  2162. if isinstance(part, dict) and part.get("type") == "text":
  2163. text_parts.append(part.get("text", ""))
  2164. raw_text = " ".join(text_parts).strip()
  2165. if not raw_text:
  2166. return TASK_NAME_FALLBACK
  2167. # 尝试使用 utility_llm 生成标题
  2168. if self.utility_llm_call:
  2169. try:
  2170. result = await self.utility_llm_call(
  2171. messages=[
  2172. {"role": "system", "content": TASK_NAME_GENERATION_SYSTEM_PROMPT},
  2173. {"role": "user", "content": raw_text[:2000]},
  2174. ],
  2175. model="qwen3.5-flash",
  2176. )
  2177. title = result.get("content", "").strip()
  2178. if title and len(title) < 100:
  2179. return title
  2180. except Exception:
  2181. pass
  2182. # Fallback: 截取前 50 字符
  2183. return raw_text[:50] + ("..." if len(raw_text) > 50 else "")
  2184. def _format_skills(self, skills: List[Skill]) -> str:
  2185. if not skills:
  2186. return ""
  2187. return "\n\n".join(s.to_prompt_text() for s in skills)