thread_runner.py 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820
  1. from functools import partial
  2. import logging
  3. import json
  4. from typing import List
  5. from concurrent.futures import Executor
  6. from sqlalchemy.orm import Session
  7. from config.config import settings
  8. from config.llm import llm_settings, tool_settings
  9. from app.core.runner.llm_backend import LLMBackend
  10. from app.core.runner.llm_callback_handler import LLMCallbackHandler
  11. from app.core.runner.memory import Memory, find_memory
  12. from app.core.runner.pub_handler import StreamEventHandler
  13. from app.core.runner.utils import message_util as msg_util
  14. from app.core.runner.utils.tool_call_util import (
  15. tool_call_recognize,
  16. internal_tool_call_invoke,
  17. tool_call_request,
  18. tool_call_id,
  19. tool_call_output,
  20. )
  21. from app.core.tools import find_tools, BaseTool
  22. from app.libs.thread_executor import get_executor_for_config, run_with_executor
  23. from app.models.message import Message, MessageUpdate
  24. from app.models.run import Run
  25. from app.models.run_step import RunStep
  26. from app.models.token_relation import RelationType
  27. from app.services.assistant.assistant import AssistantService
  28. from app.services.file.file import FileService
  29. from app.services.message.message import MessageService
  30. from app.services.run.run import RunService
  31. from app.services.run.run_step import RunStepService
  32. from app.services.token.token import TokenService
  33. from app.services.token.token_relation import TokenRelationService
  34. class ThreadRunner:
  35. """
  36. ThreadRunner 封装 run 的执行逻辑
  37. """
  38. tool_executor: Executor = get_executor_for_config(
  39. tool_settings.TOOL_WORKER_NUM, "tool_worker_"
  40. )
  41. # ---- token 估算常量(chars/4 启发式)----
  42. CHARS_PER_TOKEN = 4
  43. ROLE_OVERHEAD_TOKENS = 4
  44. # ---- 上下文预算(软阈值,基于 token)----
  45. MAX_CHAT_TOKENS = 30000
  46. KEEP_RECENT_TOKENS = 6000
  47. MIN_KEEP_TURNS = 1
  48. MAX_CHAT_IMAGES = 6
  49. MAX_SINGLE_TEXT_TOKENS = 2000
  50. SUMMARY_MAX_TOKENS = 500
  51. TOOL_OUTPUT_MAX_TOKENS = 1500
  52. def __init__(
  53. self, run_id: str, token_id: str, session: Session, stream: bool = False
  54. ):
  55. self.run_id = run_id
  56. self.token_id = token_id
  57. self.session = session
  58. self.stream = stream
  59. self.max_step = llm_settings.LLM_MAX_STEP
  60. self.event_handler: StreamEventHandler = None
  61. def run(self):
  62. try:
  63. run = RunService.get_run_sync(session=self.session, run_id=self.run_id)
  64. self.event_handler = StreamEventHandler(
  65. run_id=self.run_id, is_stream=self.stream
  66. )
  67. run = RunService.to_in_progress(session=self.session, run_id=self.run_id)
  68. self.event_handler.pub_run_in_progress(run)
  69. logging.info("processing ThreadRunner task, run_id: %s", self.run_id)
  70. ast = AssistantService.get_assistant_sync(
  71. session=self.session, assistant_id=run.assistant_id
  72. )
  73. metadata = ast.metadata_ or {}
  74. memory = find_memory(metadata.get("memory", {}))
  75. instructions = (
  76. [run.instructions or ""]
  77. if run.instructions
  78. else [ast.instructions or ""]
  79. )
  80. asst_ids = []
  81. ids = []
  82. if ast.tool_resources and "file_search" in ast.tool_resources:
  83. ids = (
  84. ast.tool_resources.get("file_search")
  85. .get("vector_stores")[0]
  86. .get("folder_ids")
  87. )
  88. if ids:
  89. asst_ids += ids
  90. ids = (
  91. ast.tool_resources.get("file_search")
  92. .get("vector_stores")[0]
  93. .get("file_ids")
  94. )
  95. if ids:
  96. asst_ids += ids
  97. if len(asst_ids) > 0:
  98. if not run.tools:
  99. run.tools = []
  100. if run.file_ids and len(run.file_ids) > 0:
  101. if not any(
  102. t.get("type") == "knowledge_search" for t in run.tools
  103. ):
  104. run.tools.append({"type": "knowledge_search"})
  105. else:
  106. for tool in run.tools:
  107. if tool.get("type") == "file_search":
  108. tool["type"] = "knowledge_search"
  109. tools = find_tools(run, self.session)
  110. for tool in tools:
  111. tool.configure(session=self.session, run=run)
  112. instruction_supplement = tool.instruction_supplement()
  113. if instruction_supplement:
  114. instructions += [instruction_supplement or ""]
  115. instruction = "\n".join(instructions)
  116. llm = self.__init_llm_backend(run.assistant_id)
  117. loop = True
  118. while loop:
  119. run_steps = RunStepService.get_run_step_list(
  120. session=self.session, run_id=self.run_id, thread_id=run.thread_id
  121. )
  122. loop = self.__run_step(
  123. llm, run, run_steps, instruction, tools, memory
  124. )
  125. latest_run = RunService.get_run_sync(
  126. session=self.session, run_id=self.run_id
  127. )
  128. if latest_run.status == "requires_action":
  129. self.event_handler.pub_done()
  130. return
  131. self.event_handler.pub_run_completed(latest_run)
  132. self.event_handler.pub_done()
  133. except Exception as e:
  134. logging.exception("ThreadRunner run failed, run_id=%s", self.run_id)
  135. try:
  136. RunService.to_failed(
  137. session=self.session, run_id=self.run_id, last_error=e
  138. )
  139. except Exception:
  140. logging.exception("mark run failed error, run_id=%s", self.run_id)
  141. try:
  142. if self.event_handler:
  143. self.event_handler.pub_done()
  144. except Exception:
  145. logging.exception("pub_done error, run_id=%s", self.run_id)
  146. raise
  147. # ------------------------------------------------------------------
  148. # token 估算
  149. # ------------------------------------------------------------------
  150. @classmethod
  151. def _estimate_text_tokens(cls, text: str) -> int:
  152. if not text:
  153. return 0
  154. return (len(text) + cls.CHARS_PER_TOKEN - 1) // cls.CHARS_PER_TOKEN
  155. @classmethod
  156. def _estimate_message_tokens(cls, message: dict) -> int:
  157. content = message.get("content")
  158. if isinstance(content, str):
  159. return cls._estimate_text_tokens(content) + cls.ROLE_OVERHEAD_TOKENS
  160. if isinstance(content, list):
  161. total = cls.ROLE_OVERHEAD_TOKENS
  162. for c in content:
  163. if c.get("type") == "text":
  164. total += cls._estimate_text_tokens(c.get("text", ""))
  165. elif c.get("type") in ("image_url", "input_audio"):
  166. total += 256
  167. return total
  168. return cls.ROLE_OVERHEAD_TOKENS
  169. @staticmethod
  170. def _dedupe_tools(tools_param):
  171. if not tools_param:
  172. return tools_param
  173. seen = set()
  174. result = []
  175. for t in tools_param:
  176. name = (t.get("function") or {}).get("name") or t.get("name")
  177. if name:
  178. if name in seen:
  179. logging.warning("duplicate tool name removed: %s", name)
  180. continue
  181. seen.add(name)
  182. result.append(t)
  183. return result
  184. @staticmethod
  185. def _sanitize_extra_body(extra_body, model):
  186. if not extra_body:
  187. return extra_body
  188. cleaned = dict(extra_body)
  189. removed = []
  190. for k in list(cleaned):
  191. if "reasoning" in k.lower() or "thinking" in k.lower():
  192. removed.append(k)
  193. cleaned.pop(k)
  194. if removed:
  195. logging.warning(
  196. "stripped reasoning params from extra_body, model=%s, keys=%s",
  197. model,
  198. removed,
  199. )
  200. return cleaned or None
  201. def __run_step(
  202. self,
  203. llm: LLMBackend,
  204. run: Run,
  205. run_steps: List[RunStep],
  206. instruction: str,
  207. tools: List[BaseTool],
  208. memory: Memory,
  209. ):
  210. logging.debug("step %s is running", run_steps)
  211. if instruction == "":
  212. instruction = (
  213. "You are a multilingual AI assistant.\n"
  214. + "- Detect user language; reply in same language unless told otherwise.\n"
  215. + "- Default to English if detection is unclear.\n"
  216. + "- Give concise, accurate, and safe answers; admit when unsure.\n"
  217. + "- Keep tone and style consistent; adapt examples to user's context.\n"
  218. + "- For code, include explanations and comments in user's language.\n"
  219. + "- If a question is ambiguous, ask for clarification.\n"
  220. )
  221. assistant_system_message = [msg_util.system_message(instruction)]
  222. chat_messages = self.__generate_chat_messages(
  223. MessageService.get_message_list(
  224. session=self.session, thread_id=run.thread_id
  225. ),
  226. run,
  227. )
  228. tool_call_messages = []
  229. for step in run_steps:
  230. logging.debug("stepstep %s is running", step)
  231. if step.type == "tool_calls" and step.status == "completed":
  232. tool_call_messages += (
  233. self.__convert_assistant_tool_calls_to_chat_messages(step)
  234. )
  235. tool_call_messages = self._prune_tool_outputs(tool_call_messages)
  236. messages = (
  237. assistant_system_message
  238. + memory.integrate_context(chat_messages)
  239. + tool_call_messages
  240. )
  241. logging.debug("messages: run %s", run)
  242. logging.debug(messages)
  243. logging.debug(tools)
  244. use_tools = len(run_steps) < self.max_step
  245. tool_choice = "auto" if use_tools else "none"
  246. tools_param = (
  247. self._dedupe_tools([tool.openai_function for tool in tools])
  248. if use_tools
  249. else None
  250. )
  251. stream_options = run.stream_options if self.stream else None
  252. extra_body = self._sanitize_extra_body(run.extra_body, run.model)
  253. logging.info(
  254. "llm request: model=%s extra_body=%s tools_count=%s",
  255. run.model,
  256. extra_body,
  257. len(tools_param or []),
  258. )
  259. response_stream = llm.run(
  260. messages=messages,
  261. model=run.model,
  262. tools=tools_param,
  263. tool_choice=tool_choice,
  264. stream=self.stream,
  265. stream_options=stream_options,
  266. extra_body=extra_body,
  267. temperature=run.temperature or 1.0,
  268. top_p=run.top_p,
  269. response_format=run.response_format,
  270. parallel_tool_calls=run.parallel_tool_calls if use_tools else None,
  271. audio=run.audio,
  272. modalities=run.modalities,
  273. )
  274. create_message_callback = partial(
  275. MessageService.new_message,
  276. session=self.session,
  277. assistant_id=run.assistant_id,
  278. thread_id=run.thread_id,
  279. run_id=run.id,
  280. role="assistant",
  281. )
  282. def _create_message_creation_run_step(message_id):
  283. return RunStepService.new_run_step(
  284. session=self.session,
  285. type="message_creation",
  286. assistant_id=run.assistant_id,
  287. thread_id=run.thread_id,
  288. run_id=run.id,
  289. step_details={
  290. "type": "message_creation",
  291. "message_creation": {"message_id": message_id},
  292. },
  293. )
  294. llm_callback_handler = LLMCallbackHandler(
  295. run_id=run.id,
  296. on_step_create_func=_create_message_creation_run_step,
  297. on_message_create_func=create_message_callback,
  298. event_handler=self.event_handler,
  299. )
  300. if self.stream == False and hasattr(response_stream, "choices"):
  301. response_stream = [response_stream]
  302. response_msg = llm_callback_handler.handle_llm_response(response_stream)
  303. message_creation_run_step = llm_callback_handler.step
  304. logging.debug("chat_response_message: %s", response_msg)
  305. if msg_util.is_tool_call(response_msg):
  306. tool_calls = []
  307. for tc in response_msg.tool_calls:
  308. recognized = tool_call_recognize(tc, tools)
  309. if (
  310. not recognized
  311. or not isinstance(recognized, (tuple, list))
  312. or len(recognized) != 2
  313. ):
  314. logging.warning(
  315. "invalid tool_call_recognize result: %r", recognized
  316. )
  317. continue
  318. tool_calls.append(recognized)
  319. new_run_step = RunStepService.new_run_step(
  320. session=self.session,
  321. type="tool_calls",
  322. assistant_id=run.assistant_id,
  323. thread_id=run.thread_id,
  324. run_id=run.id,
  325. step_details={
  326. "reasoning_content": response_msg.reasoning_content,
  327. "type": "tool_calls",
  328. "tool_calls": [
  329. tool_call_dict for _, tool_call_dict in tool_calls
  330. ],
  331. },
  332. )
  333. self.event_handler.pub_run_step_created(new_run_step)
  334. self.event_handler.pub_run_step_in_progress(new_run_step)
  335. internal_tool_calls = [tc for tc in tool_calls if tc[0] is not None]
  336. external_tool_call_dict = [
  337. tc[1] for tc in tool_calls if tc[0] is None
  338. ]
  339. if internal_tool_calls:
  340. try:
  341. tool_calls_with_outputs = run_with_executor(
  342. executor=ThreadRunner.tool_executor,
  343. func=internal_tool_call_invoke,
  344. tasks=internal_tool_calls,
  345. timeout=tool_settings.TOOL_WORKER_EXECUTION_TIMEOUT,
  346. )
  347. new_run_step = RunStepService.update_step_details(
  348. session=self.session,
  349. run_step_id=new_run_step.id,
  350. step_details={
  351. "type": "tool_calls",
  352. "tool_calls": tool_calls_with_outputs,
  353. },
  354. completed=not external_tool_call_dict,
  355. )
  356. self.event_handler.pub_message_delta_tool(
  357. message_id=llm_callback_handler.message.id,
  358. index=0,
  359. content=json.dumps(tool_calls_with_outputs),
  360. )
  361. except Exception as e:
  362. RunStepService.to_failed(
  363. session=self.session,
  364. run_step_id=new_run_step.id,
  365. last_error=e,
  366. )
  367. try:
  368. RunService.to_failed(
  369. session=self.session,
  370. run_id=run.id,
  371. last_error=e,
  372. )
  373. except Exception:
  374. logging.exception(
  375. "mark run failed error, run_id=%s", run.id
  376. )
  377. raise e
  378. if external_tool_call_dict:
  379. run = RunService.to_requires_action(
  380. session=self.session,
  381. run_id=run.id,
  382. required_action={
  383. "type": "submit_tool_outputs",
  384. "submit_tool_outputs": {
  385. "tool_calls": external_tool_call_dict
  386. },
  387. },
  388. )
  389. self.event_handler.pub_run_step_delta(
  390. step_id=new_run_step.id,
  391. step_details={
  392. "type": "tool_calls",
  393. "tool_calls": external_tool_call_dict,
  394. },
  395. )
  396. self.event_handler.pub_run_requires_action(run)
  397. else:
  398. self.event_handler.pub_run_step_completed(new_run_step)
  399. return True
  400. else:
  401. if response_msg.content == "":
  402. response_msg.content = (
  403. '[{"text": {"value": "", "annotations": []}, "type": "text"}]'
  404. )
  405. if response_msg.reasoning_content == "":
  406. response_msg.reasoning_content = None
  407. new_message = MessageService.modify_message_sync(
  408. session=self.session,
  409. thread_id=run.thread_id,
  410. message_id=llm_callback_handler.message.id,
  411. body=MessageUpdate(
  412. content=response_msg.content,
  413. reasoning_content=response_msg.reasoning_content,
  414. ),
  415. )
  416. self.event_handler.pub_message_completed(new_message)
  417. new_step = RunStepService.update_step_details(
  418. session=self.session,
  419. run_step_id=message_creation_run_step.id,
  420. step_details={
  421. "type": "message_creation",
  422. "message_creation": {"message_id": new_message.id},
  423. },
  424. completed=True,
  425. )
  426. RunService.to_completed(session=self.session, run_id=run.id)
  427. self.event_handler.pub_run_step_completed(new_step)
  428. return False
  429. def __init_llm_backend(self, assistant_id):
  430. if settings.AUTH_ENABLE:
  431. if self.token_id:
  432. token_id = self.token_id
  433. else:
  434. token_id = TokenRelationService.get_token_id_by_relation(
  435. session=self.session,
  436. relation_type=RelationType.Assistant,
  437. relation_id=assistant_id,
  438. )
  439. try:
  440. if token_id is not None and len(token_id) > 0:
  441. token = TokenService.get_token_by_id(self.session, token_id)
  442. return LLMBackend(
  443. base_url=token.llm_base_url, api_key=token.llm_api_key
  444. )
  445. except Exception as e:
  446. print(e)
  447. token = {
  448. "llm_base_url": "http://172.16.12.13:3000/v1",
  449. "llm_api_key": "sk-vTqeBKDC2j6osbGt89A2202dAd1c4fE8B1D294388b569e54",
  450. }
  451. return LLMBackend(
  452. base_url=token.get("llm_base_url"),
  453. api_key=token.get("llm_api_key"),
  454. )
  455. else:
  456. return LLMBackend(
  457. base_url=llm_settings.OPENAI_API_BASE,
  458. api_key=llm_settings.OPENAI_API_KEY,
  459. )
  460. # ------------------------------------------------------------------
  461. # 工具结果剪枝
  462. # ------------------------------------------------------------------
  463. def _prune_tool_outputs(self, tool_call_messages: List[dict]) -> List[dict]:
  464. if not tool_call_messages:
  465. return tool_call_messages
  466. result = []
  467. for msg in tool_call_messages:
  468. content = msg.get("content")
  469. if isinstance(content, str):
  470. tokens = self._estimate_text_tokens(content)
  471. if tokens > self.TOOL_OUTPUT_MAX_TOKENS:
  472. msg = dict(msg)
  473. msg["content"] = (
  474. f"[cwl-stub: truncated {tokens} tokens]\n"
  475. + self._truncate_text_by_tokens(
  476. content, self.TOOL_OUTPUT_MAX_TOKENS
  477. )
  478. )
  479. elif isinstance(content, list):
  480. needs_prune = any(
  481. c.get("type") == "text"
  482. and self._estimate_text_tokens(c.get("text", ""))
  483. > self.TOOL_OUTPUT_MAX_TOKENS
  484. for c in content
  485. )
  486. if needs_prune:
  487. msg = dict(msg)
  488. new_content = []
  489. for c in content:
  490. if c.get("type") == "text":
  491. text = c.get("text", "")
  492. tokens = self._estimate_text_tokens(text)
  493. if tokens > self.TOOL_OUTPUT_MAX_TOKENS:
  494. text = (
  495. f"[cwl-stub: truncated {tokens} tokens]\n"
  496. + self._truncate_text_by_tokens(
  497. text, self.TOOL_OUTPUT_MAX_TOKENS
  498. )
  499. )
  500. new_content.append({"type": "text", "text": text})
  501. else:
  502. new_content.append(c)
  503. msg["content"] = new_content
  504. result.append(msg)
  505. return result
  506. # ------------------------------------------------------------------
  507. # 上下文压缩(token 预算 + 轮次配对)
  508. # ------------------------------------------------------------------
  509. def _truncate_text_by_tokens(self, text: str, max_tokens: int) -> str:
  510. limit_chars = max_tokens * self.CHARS_PER_TOKEN
  511. if len(text) <= limit_chars:
  512. return text
  513. head = limit_chars * 2 // 3
  514. tail = limit_chars - head
  515. removed = len(text) - limit_chars
  516. marker = f"\n...[truncated {removed} chars]...\n"
  517. if tail <= 0:
  518. return text[:head] + marker
  519. return text[:head] + marker + text[-tail:]
  520. @staticmethod
  521. def _content_to_text(content) -> str:
  522. if isinstance(content, str):
  523. return content
  524. if isinstance(content, list):
  525. return " ".join(
  526. c.get("text", "")
  527. for c in content
  528. if c.get("type") == "text" and c.get("text")
  529. )
  530. return ""
  531. def _normalize_history(self, messages: List[Message], run: Run) -> List[dict]:
  532. ordered = list(reversed(messages))
  533. image_slots = []
  534. for mi, m in enumerate(ordered):
  535. if m.role != "user":
  536. continue
  537. for ci, c in enumerate(m.content or []):
  538. if c.get("type") == "image_url" and run.audio is None:
  539. image_slots.append((mi, ci))
  540. if len(image_slots) > self.MAX_CHAT_IMAGES:
  541. keep_image_slots = set(image_slots[-self.MAX_CHAT_IMAGES:])
  542. else:
  543. keep_image_slots = set(image_slots)
  544. normalized: List[dict] = []
  545. is_audio_num = 0
  546. for mi, m in enumerate(ordered):
  547. role = m.role
  548. if role == "user":
  549. contents = []
  550. for ci, c in enumerate(m.content or []):
  551. ctype = c.get("type")
  552. if ctype == "text":
  553. text = (c.get("text") or {}).get("value") or ""
  554. text = self._truncate_text_by_tokens(
  555. text, self.MAX_SINGLE_TEXT_TOKENS
  556. )
  557. if text:
  558. contents.append({"type": "text", "text": text})
  559. elif ctype == "image_url" and run.audio is None:
  560. if (mi, ci) in keep_image_slots:
  561. contents.append(c)
  562. else:
  563. contents.append(
  564. {"type": "text", "text": "[image omitted]"}
  565. )
  566. elif (
  567. ctype == "input_audio"
  568. and run.audio is not None
  569. and is_audio_num < 2
  570. ):
  571. contents.append(c)
  572. is_audio_num += 1
  573. if contents:
  574. normalized.append(
  575. {
  576. "role": "user",
  577. "content": contents,
  578. "reasoning_content": None,
  579. }
  580. )
  581. elif role == "assistant":
  582. text_parts = []
  583. for c in m.content or []:
  584. if c.get("type") == "text":
  585. text_parts.append(
  586. (c.get("text") or {}).get("value") or ""
  587. )
  588. assistant_content = "".join(text_parts)
  589. if not assistant_content:
  590. continue
  591. assistant_content = self._truncate_text_by_tokens(
  592. assistant_content, self.MAX_SINGLE_TEXT_TOKENS
  593. )
  594. reasoning = m.reasoning_content
  595. if reasoning:
  596. reasoning = self._truncate_text_by_tokens(
  597. reasoning, self.MAX_SINGLE_TEXT_TOKENS
  598. )
  599. normalized.append(
  600. {
  601. "role": "assistant",
  602. "content": assistant_content,
  603. "reasoning_content": reasoning,
  604. }
  605. )
  606. return normalized
  607. @staticmethod
  608. def _group_turns(normalized: List[dict]) -> List[List[dict]]:
  609. turns: List[List[dict]] = []
  610. for m in normalized:
  611. if m["role"] == "user":
  612. if turns and turns[-1][-1]["role"] == "assistant":
  613. turns.append([m])
  614. elif turns:
  615. turns[-1].append(m)
  616. else:
  617. turns.append([m])
  618. else:
  619. if turns:
  620. turns[-1].append(m)
  621. else:
  622. turns.append([m])
  623. return turns
  624. def _build_summary(self, old_messages: List[dict]) -> str:
  625. if not old_messages:
  626. return ""
  627. parts = []
  628. for m in old_messages:
  629. text = self._content_to_text(m["content"]).strip()
  630. if not text:
  631. continue
  632. prefix = "User" if m["role"] == "user" else "Assistant"
  633. parts.append(f"{prefix}: {text}")
  634. summary = "\n".join(parts)
  635. summary = self._truncate_text_by_tokens(
  636. summary, self.SUMMARY_MAX_TOKENS
  637. )
  638. return summary
  639. def _compress_history(self, normalized: List[dict]) -> List[dict]:
  640. if not normalized:
  641. return normalized
  642. if len(normalized) <= max(self.MIN_KEEP_TURNS * 2, 2):
  643. return normalized
  644. total_tokens = sum(
  645. self._estimate_message_tokens(m) for m in normalized
  646. )
  647. if total_tokens <= self.MAX_CHAT_TOKENS:
  648. return normalized
  649. turns = self._group_turns(normalized)
  650. if not turns:
  651. return normalized
  652. keep_idx = len(turns) - 1
  653. kept_tokens = sum(
  654. self._estimate_message_tokens(m) for m in turns[-1]
  655. )
  656. for i in range(len(turns) - 2, -1, -1):
  657. turn = turns[i]
  658. turn_tokens = sum(
  659. self._estimate_message_tokens(m) for m in turn
  660. )
  661. if kept_tokens + turn_tokens > self.KEEP_RECENT_TOKENS:
  662. break
  663. keep_idx = i
  664. kept_tokens += turn_tokens
  665. old_turns = turns[:keep_idx]
  666. recent_turns = turns[keep_idx:]
  667. while recent_turns and recent_turns[0][0]["role"] == "assistant":
  668. old_turns.append(recent_turns.pop(0))
  669. while len(recent_turns) < self.MIN_KEEP_TURNS and old_turns:
  670. recent_turns.insert(0, old_turns.pop())
  671. summary = ""
  672. if old_turns:
  673. old_messages = [m for turn in old_turns for m in turn]
  674. summary = self._build_summary(old_messages)
  675. result: List[dict] = []
  676. if summary:
  677. summary_text = f"[Earlier conversation summary]\n{summary}"
  678. if recent_turns and recent_turns[0][0]["role"] == "user":
  679. first_msg = dict(recent_turns[0][0])
  680. first_content = list(first_msg.get("content") or [])
  681. first_msg["content"] = [
  682. {"type": "text", "text": summary_text + "\n\n---\n\n"}
  683. ] + first_content
  684. recent_turns[0] = [first_msg] + list(recent_turns[0][1:])
  685. else:
  686. result.append(
  687. {
  688. "role": "user",
  689. "content": [{"type": "text", "text": summary_text}],
  690. "reasoning_content": None,
  691. }
  692. )
  693. for turn in recent_turns:
  694. result.extend(turn)
  695. if result and result[0]["role"] == "assistant":
  696. result.insert(
  697. 0,
  698. {
  699. "role": "user",
  700. "content": [
  701. {"type": "text", "text": "[Earlier context omitted]"}
  702. ],
  703. "reasoning_content": None,
  704. },
  705. )
  706. return result
  707. @staticmethod
  708. def _build_chat_messages(normalized: List[dict]) -> List[dict]:
  709. chat_messages = []
  710. for m in normalized:
  711. if m["role"] == "user":
  712. chat_messages.append(
  713. msg_util.new_message("user", m["content"])
  714. )
  715. else:
  716. chat_messages.append(
  717. msg_util.new_message(
  718. "assistant", m["content"], m["reasoning_content"]
  719. )
  720. )
  721. return chat_messages
  722. def __generate_chat_messages(self, messages: List[Message], run: Run):
  723. normalized = self._normalize_history(messages, run)
  724. normalized = self._compress_history(normalized)
  725. return self._build_chat_messages(normalized)
  726. def __convert_assistant_tool_calls_to_chat_messages(self, run_step: RunStep):
  727. tool_calls = run_step.step_details["tool_calls"]
  728. reasoning_content = run_step.step_details.get("reasoning_content")
  729. tool_call_requests = [
  730. msg_util.tool_calls(
  731. [tool_call_request(tool_call) for tool_call in tool_calls],
  732. reasoning_content,
  733. )
  734. ]
  735. logging.debug("tool_calls: %s", tool_calls)
  736. tool_call_outputs = [
  737. msg_util.tool_call_result(
  738. tool_call_id(tool_call), tool_call_output(tool_call)
  739. )
  740. for tool_call in tool_calls
  741. ]
  742. logging.debug("tool_call_outputs: %s", tool_call_outputs)
  743. return tool_call_requests + tool_call_outputs