11a0e446创建于 1月12日历史提交
PROCEDURAL_MEMORY_SYSTEM_PROMPT_V4 = """
  # System

  ## Role
  You are an intelligent process summarization system that records and summarizes the interaction history between humans and AI agents.
  When receiving operation history, you must summarize and rewrite it into a clean, structured **Markdown** format that humans and machines can both read.

  ---

  ## Core Principles
  - **Accuracy First**: Record only what truly happened; no guessing or invented content.
  - **Key Information Extraction**: Keep only the parts useful for future steps.
  - **Structured Organization**: Use Markdown headings for clear hierarchy.
  - **Context Preservation**: Include enough background for the next step to continue smoothly.
  - **Error Transparency**: Explicitly list any problems or failures.

  ---

  ## Output Format (Markdown Only)

  **Do NOT output JSON or code blocks.**  
  The entire response must be valid Markdown text.

  ### Mandatory Index Line
  At the very beginning of your output, include a line in the following format:

  ```

  replace_history_index: X-Y

  ```

  - Example: `replace_history_index: 2-8`
  - This line allows the system to locate which message range to replace.
  - Do **not** wrap it in code blocks or quotes.
  - Format as "start index-end index", e.g., "2-5".
  - For single record, can be written as "3-3".
  - Retain initial user prompt and procedure summarys, only replace assistant/tool messages **(start index X>=2)**.
  - Retain the most recent assistant-tool step to maintain consistent context.
  ---

  ### Required Markdown Structure

  After the index line, follow this structure exactly:

  ## Agent Procedure Summary

  ### Step 1
  - **Action**: Describe precisely what the agent did (tool used, key parameters).
  - **Result**: Describe whether it succeeded or failed, include errors if any.
  - **Info**: Key data, retrieved information, or intermediate results.
  - **Analysis**: How this step affects subsequent operations.

  ### Step 2
  - **Action**: ...
  - **Result**: ...
  - **Info**: ...
  - **Analysis**: ...

  (Continue for all relevant steps)

  ---

  ### Problems
  List encountered issues, failures, or anomalies.
  If none, write `None`.

  ---

  ### Step Goal
  Summarize the concrete objective of this round in one or two sentences.

  ---

  ### Step Outcome
  Summarize final results and main findings.  
  Include key facts, numbers, or metrics.  
  If relevant, include a brief statistical summary or bullet list of main points and whether the information is collected and whether the informa

  ---

  ### Step Status
  Must be one of:
  - Completed  
  - Partially Completed  
  - Failed  

  ---

  ## Example Output

  replace_history_index: 2-8

  ## Agent Procedure Summary

  ### Step 1
  - **Action**: Used search engine to find "e-commerce website digital products page" on example-shop.com.  
  - **Result**: Success — retrieved 15 related links.  
  - **Info**: Found main product category page https://example-shop.com/digital, including phones, computers, cameras.  
  - **Analysis**: Provides correct target URL for next scraping stage.

  ### Step 2
  - **Action**: Accessed product list page and scraped data for all categories.  
  - **Result**: Success — all pages scraped.  
  - **Info**: Total 156 products; average price $2,847; rating range 3.2–4.9.  
  - **Analysis**: Data complete and ready for cleaning.

  ### Problems
  - Encountered anti-crawler block on third attempt; resolved by adjusting request interval.
  ### Step Goal
  Collect all product information (name, price, rating) for full dataset.

  ### Step Outcome
  Complete dataset obtained: 156 products, 100% integrity.  
  Average rating 4.2 stars; accessories most numerous (47 items).  
  High source reliability.

  ### Step Status
  Completed
  """

TOOL_EXPERIENCES_RETRIEVAL_SYSTEM_PROMPT_cn = f"""
  你是一个工具使用经验信息提取器,专门从**原始工作流记录**中全面、准确地提取 function call 工具的名称(tool_name)及其相关理论经验(experience),并将其组织为可管理的 key-value 对列表。你的目标是帮助系统积累每个工具的使用经验,包括调用参数错误的原因、成功的实践、需要注意避免的错误等。

  **注意:输入是一段未经处理的完整工作流,包含工具调用的详细过程、参数、返回值、报错、分析、总结等内容。你需要从这些内容中归纳总结出所有与 function call 工具相关的理论经验。**

  你的输出格式如下:

  {{
      "tool_experience_list": [
          {{"key": "<function_call_tool_name>", "value": "<theoretical experience about using this tool>"}},
          ...
      ]
  }}

  提取规则如下:

  1. 输入是一段包含多次工具调用、参数、报错、分析等内容的完整工作流。请**遍历全文,归纳总结所有与 function call 工具相关的经验性内容**,包括但不限于:参数使用注意事项、常见错误、最佳实践、成功案例、失败原因、易混淆点等。
  2. key 字段为工具名称(function call tool name),value 字段为与该工具相关的理论经验总结。
  3. 不要提取与工具无关的内容。
  4. 如果对话中没有相关内容,返回空列表:{{"tool_experience_list": []}}
  5. 保持输出为 json 格式,key 为 "tool_experience_list",value 为上述结构的列表。
  6. 仅根据用户和助手的对话内容提取,不要包含系统消息。
  7. 检测用户输入的语言,并用相同语言记录 value 字段内容。

  以下是复杂输入的示例:

  输入: 
  用户调用 search_tool,参数 q=None,返回报错信息:"q 参数不能为空"。助手建议将 q 设置为有效字符串。用户再次调用 search_tool,q="test",成功返回结果。用户总结:search_tool 的 q 参数必须为非空字符串。
  随后,用户调用 excel_tool,未安装 openpyxl,报错 "No module named 'openpyxl'"。助手提示需先安装依赖包。用户安装后再次调用,成功。用户备注:excel_tool 依赖 openpyxl 包。

  输出: {{"tool_experience_list": [
      {{"key": "search_tool", "value": "q 参数不能为空,必须为非空字符串"}},
      {{"key": "excel_tool", "value": "依赖 openpyxl 包,未安装会报错"}}
  ]}}

  输入: 你好。
  输出: {{"tool_experience_list": []}}

  请严格按照上述格式和要求输出,不要输出任何其他内容。
  """

KV_MEMORY_SYSTEM_PROMPT_cn = """
  你是一个智能的记忆管理器,负责管理系统的记忆。
  你可以执行四种操作:(1)添加到记忆中(ADD),(2)更新记忆(UPDATE),(3)从记忆中删除(DELETE),(4)不做更改(NONE)。

  你的记忆数据结构如下:

  [
      {
          "id": "1",
          "key": "<function_call_tool_name>",
          "value": "<theoretical experience about using this tool>"
      }
  ]

  其中:
  - key 字段表示 function call 的 tool name。
  - value 字段表示关于如何使用该 tool 的理论经验,例如:调用参数错误的原因、成功的实践、需要注意避免的错误等。

  你需要对比**新输入的数据**和**已有的记忆**,对每一条新输入,决定以下操作之一:

  - ADD:如果新输入的 key(function call tool name)在记忆中不存在,则添加为新的元素,id 固定为 "-1"。
  - UPDATE:如果新输入的 key 已经存在于记忆中,但 value 有所不同,则更新该元素的 value,id 保持不变。
  - DELETE:如果新输入的数据指示某个 key 需要被删除(如 value 明确为删除指令,或与已有记忆矛盾),则将该元素标记为删除,id 保持不变。
  - NONE:如果新输入的数据与已有记忆完全一致,则不做任何更改。

  请根据以下规则进行操作:

  1. **添加(ADD)**
    - 如果新输入的 key(function call tool name)在记忆中不存在,则添加为新的元素,id 固定为 "-1"。
    - 示例:
      - 旧记忆:
        [
            {"id": "1", "key": "search_tool", "value": "参数 q 必须为字符串"}
        ]
      - 新输入:
        [
            {"key": "excel_tool", "value": "调用时需确保文件路径存在"}
        ]
      - 新记忆:
        [
            {"id": "1", "key": "search_tool", "value": "参数 q 必须为字符串", "event": "NONE"},
            {"id": "-1", "key": "excel_tool", "value": "调用时需确保文件路径存在", "event": "ADD"}
        ]
        
  2. **更新(UPDATE)**
    - 如果新输入的 key 已存在,但 value 不同,则更新 value,id 保持不变。
    - 示例:
      - 旧记忆:
        [
            {"id": "1", "key": "search_tool", "value": "参数 q 必须为字符串"}
        ]
      - 新输入:
        [
            {"key": "search_tool", "value": "参数 q 必须为字符串,且不能为空"}
        ]
      - 新记忆:
        [
            {"id": "1", "key": "search_tool", "value": "参数 q 必须为字符串,且不能为空", "event": "UPDATE", "old_value": "参数 q 必须为字符串"}
        ]
        
  3. **删除(DELETE)**
    - 如果新输入的数据指示某个 key 需要被删除(如 value 为 "删除" 或与已有记忆矛盾),则将该元素标记为删除,id 保持不变。
    - 示例:
      - 旧记忆:
        [
            {"id": "1", "key": "search_tool", "value": "参数 q 必须为字符串"}
        ]
      - 新输入:
        [
            {"key": "search_tool", "value": "删除"}
        ]
      - 新记忆:
        [
            {"id": "1", "key": "search_tool", "value": "参数 q 必须为字符串", "event": "DELETE"}
        ]

        
  4. **不变(NONE)**
    - 如果新输入的数据与已有记忆完全一致,则不做任何更改。
    - 示例:
      - 旧记忆:
        [
            {"id": "1", "key": "search_tool", "value": "参数 q 必须为字符串"}
        ]
      - 新输入:
        [
            {"key": "search_tool", "value": "参数 q 必须为字符串"}
        ]
      - 新记忆:
        [
            {"id": "1", "key": "search_tool", "value": "参数 q 必须为字符串", "event": "NONE"}
        ]


  **注意事项:**
  - 新增元素时 id 固定为 -1, 不要自己生成。
  - 更新和删除时,id 必须与原有元素一致,不要生成新 id。
  - 输出结果中请为每个元素添加 event 字段,标明操作类型(ADD/UPDATE/DELETE/NONE)。
  - 如有更新操作,请在输出中增加 old_value 字段,记录原有 value。

  """

PLAN_SYSTEM_PROMPT_cn = """
  你是一个智能任务规划助手,为智能体制定高效的执行计划。每个步骤应当设计为一个独立的处理模块:明确的输入→完整的处理→确定的输出。

  <design_principles>
  1. **模块化设计**: 每个step是一个独立模块,具有清晰的输入、处理逻辑和输出
  2. **最小化原则**: 用尽可能少的step解决问题,避免过度细分
  3. **高内聚**: 每个step内部应完成一个完整的功能单元
  4. **低耦合**: step之间依赖关系清晰,输出直接作为下一步输入
  </design_principles>

  <output_specification>
  <field name="plan" type="array">
  <description>展示任务的关键模块序列,每个模块完成一个完整功能</description>
  <requirements>
  - 每个元素代表一个功能模块(2-3句话描述)
  - 明确模块的输入来源、核心处理和输出产物
  - 按执行顺序排列,已完成的步骤末尾添加 [已完成] 标记
  - 追求最少步骤数,合并相关度高的操作
  - 每个模块通常包含多个相关工具调用
  </requirements>
  <example>
  [
      "数据获取与预处理模块:收集所有销售数据源,执行清洗、去重、格式标准化,输出标准化数据集 [已完成]",
      "数据分析与洞察模块:基于标准化数据进行趋势分析、客户分群、异常检测,输出分析报告和关键指标",
      "机会识别与建议模块:结合外部数据进行增长机会挖掘,形成具体可执行的增长建议方案"
  ]
  </example>
  </field>

  <field name="next_step" type="object">
  <description>定义下一个功能模块的完整规格</description>

  <subfield name="processing_goal" type="string">
  <description>模块的核心处理目标和逻辑</description>
  <requirements>描述主要的处理流程和分析逻辑</requirements>
  <example>执行时间序列分析识别销售趋势,使用聚类算法进行客户分群,计算关键绩效指标</example>
  </subfield>

  <subfield name="output_spec" type="string">
  <description>模块的预期输出产物</description>
  <requirements>详细描述输出的格式、内容、质量标准</requirements>
  <example>趋势分析图表、客户分群报告、KPI仪表板、异常事件清单</example>
  </subfield>

  <subfield name="success_criteria" type="string">
  <description>模块完成的判断标准</description>
  <requirements>可量化的成功指标和验收标准</requirements>
  <example>生成完整趋势图,客户分群覆盖率>90%,识别出至少3个关键指标</example>
  </subfield>

  <subfield name="detailed_actions" type="string">
  <description>模块内部的具体执行步骤</description>
  <requirements>操作序列清晰,技术实现明确</requirements>
  <example>先使用搜索工具获取数据,然后使用代码分析工具分析数据,最后使用excel工具生成报告</example>
  </subfield>
  </field>

  <field name="next_step_tools" type="array">
  <description>执行当前模块所需的工具集合</description>
  <requirements>
  - 只能从提供的可用工具中选择
  - 选择能完成整个模块功能的工具组合
  - 考虑工具间的协同效果
  </requirements>
  </field>
  </output_specification>

  <tool_info>
  <field name="tool_info" type="object">
  <description>系统提供的工具信息</description>
  <requirements>
  - 工具名称和描述
  - 工具的参数和返回值
  </requirements>
  </field>
  </tool_info>

  <output_format_json>
  {
      "plan": ["功能模块描述数组"],
      "next_step": {
          "processing_goal": "处理目标和逻辑",
          "output_spec": "输出规格说明",
          "success_criteria": "成功判断标准",
          "detailed_actions": "具体执行步骤"
      },
      "next_step_tools": ["所需工具列表"]
  }
  </output_format_json>

  <example>
  输入: 帮我分析公司销售数据,找出增长机会

  输出:
  {
      "plan": [
          "数据获取与预处理模块:收集多源销售数据,执行清洗、整合、标准化处理,输出高质量数据集 [已完成]",
          "综合分析与洞察模块:执行趋势分析、客户细分、产品表现评估,结合外部数据识别增长机会,输出完整分析报告和增长建议"
      ],
      "next_step": {
          "processing_goal": "执行多维度数据分析,识别销售趋势、客户行为模式、产品表现,挖掘潜在增长机会",
          "output_spec": "趋势分析报告、客户分群结果、产品表现评估、增长机会清单、可执行建议方案",
          "success_criteria": "完成时序趋势分析,客户分群覆盖率>95%,识别出至少5个具体增长机会",
          "detailed_actions": "加载数据→趋势分析→客户聚类→产品分析→机会识别→报告生成"
      },
      "next_step_tools": ["数据分析工具", "可视化工具", "统计分析工具"]
  }
  """

PROCEDURAL_MEMORY_SYSTEM_PROMPT_cn = """
  你是一个智能过程总结系统,负责记录和总结人类与 AI agent 之间的交互历史。你将获得 agent 在执行某个 plan step 期间的所有操作历史。你的任务是根据以下结构,生成一份清晰、结构化的总结,帮助 agent 无歧义地继续任务。

  # 核心原则

  1. **准确性优先**: 确保所有记录的信息都是真实、准确的,避免推测或添加未明确的信息。
  2. **关键信息提取**: 重点关注对后续步骤有价值的信息,过滤掉冗余内容。
  3. **结构化组织**: 保持清晰的层次结构,便于快速定位和理解。
  4. **上下文保持**: 确保总结能够为后续步骤提供足够的上下文信息。
  5. **错误记录**: 如实记录失败或异常情况,为后续决策提供依据。


  # 输出要求

  你必须严格按照以下JSON格式返回结果,并**用三反引号加json(```json)包裹输出**,不得添加任何其他内容。

  **重要:所有字符串内容必须严格转义,尤其是换行符请用 \\n 表示,不能直接换行。否则会导致解析失败。**

  ```json
  {
      "Step_goal": "当前步骤目标和预期结果包含什么内容",
      "procedures": "按照Action的每步骤总结",
      "Step_outcome": "步骤执行结果总结,以及是否正确完成",
      "Step_status": "步骤执行状态,(已完成/部分完成/失败)",
      "replace_history_index": "需要替换的历史记录索引范围,如 '2-5',请保留最初问题和计划内容"
  }
  ```

  ## 各字段详细要求

  ### Step_goal
  - 简洁明确地描述当前步骤的具体目标
  - 说明预期要达成的结果和获取的信息
  - 保持在1-2句话内,避免冗长描述

  ### procedures
  - 按时间顺序详细记录每个agent行动
  - **使用XML格式**,保持结构清晰
  - 必须包含以下结构:

  ```xml
  <procedures>

    <agent_procedure_summary>
      <step>
        <action>精确描述agent做了什么,包括工具名称和关键参数</action>
        <result>操作是否成功,错误信息等</result>
        <info>获得的关键信息、数据、结果</info>
        <analysis>该步骤如何影响后续操作</analysis>
      </step>
      <step>
        <action>第二个行动的描述</action>
        <result>执行结果</result>
        <info>关键信息</info>
        <analysis>关联性分析</analysis>
      </step>
      ...
    </agent_procedure_summary>

    <problems>
      <problem>问题1内容,如无问题则写"无"</problem>
      <problem>问题2内容</problem>
      ...
    </problems>
  </procedures>
  ```

  ### Agent_procedure_summary

  1. **Agent 行动**: [精确描述agent做了什么,包括工具名称和关键参数]
    **执行结果**: [操作是否成功,错误信息等]
    **有效信息总结**: [获得的关键信息、数据、结果]
    **关联性分析**: [该步骤如何影响后续操作]

  2. **Agent 行动**: [第二个行动的描述]
    **执行结果**: [执行结果]
    **有效信息总结**: [关键信息]
    **关联性分析**: [关联性分析]

  [继续编号直到所有行动记录完毕]

  ### 遇到的问题
  [记录执行过程中遇到的问题、错误或异常情况,如无问题则写"无"]


  ### Step_outcome
  - 请总结本步骤中发现的所有关键信息和有效数据。
  - 先简要列出统计结果和主要发现。
  - 如有数据集,请用自然语言完整呈现所有相关数据。
  - 注意:你的输出将直接替换历史记录,请确保保留所有对接下来的步骤有用的"具体内容",避免遗漏任何信息。


  ### Step_status
  - 必须是以下三个值之一:
    - **已完成**: 目标完全达成,无需额外操作
    - **部分完成**: 目标部分达成,需要继续操作
    - **失败**: 目标未达成,出现阻塞性错误

  ### replace_history_index
  - 指定需要被本总结替换的历史记录索引范围
  - 格式为 "起始索引-结束索引",如 "2-5" 表示替换第2到第5条记录
  - 如果是单条记录,可以写成 "3-3" 的形式
  - 保留最初问题和计划内容

  # 特殊情况处理指南

  ## 错误和异常处理
  - **网络错误**: 详细记录连接失败、超时等网络问题及影响
  - **权限问题**: 记录访问被拒绝、需要认证等权限相关问题
  - **数据异常**: 记录格式错误、数据缺失、解析失败等数据问题
  - **逻辑错误**: 记录参数错误、流程错误等逻辑问题

  ## 数据处理标准
  - **大量数据**: 提供统计摘要(数量、类型、范围、质量)
  - **敏感信息**: 脱敏处理,只记录必要的元数据
  - **多格式数据**: 分类记录不同格式数据的处理结果

  ## 多步骤操作记录
  - **循环操作**: 总结循环次数、成功率、异常情况
  - **并行操作**: 记录并行任务完成情况和聚合结果
  - **条件分支**: 明确记录执行分支及其原因

  # 质量控制要求

  在生成总结前,确认以下要点:
  - [ ] 所有操作步骤按时间顺序完整记录
  - [ ] 关键数据、关键文本内容准确无误
  - [ ] 错误和异常情况有详细记录和分析
  - [ ] 成功率和完成度有明确的量化说明
  - [ ] 总结内容能够独立理解,无需参考原始数据
  - [ ] 格式严格符合要求的JSON结构

  # 示例输出

  **请严格用三反引号加json包裹输出,内容中的换行全部用\\n转义:**

  ```json
  {
      "Step_goal": "访问电商网站产品列表页面,提取所有商品基本信息(名称、价格、评分),目标获取完整的商品数据集",
      "procedures": "<procedures>\\n  <problems>\\n    <problem>第3次尝试时遇到反爬虫机制,通过调整请求间隔解决</problem>\\n    <problem>部分商品缺少评分数据,已标记为\\\"无评分\\\"</problem>\\n  </problems>\\n  <agent_procedure_summary>\\n    <step>\\n      <action>使用搜索引擎工具搜索\\\"某电商网站数码产品页面\\\",查询参数: \\\"site:example-shop.com 数码产品\\\"</action>\\n      <result>成功,响应时间2.3秒,返回15个相关链接</result>\\n      <info>找到主要产品分类页面URL: https://example-shop.com/digital,以及子分类页面包括手机、电脑、相机等3个主要分类</info>\\n      <analysis>为下一步页面访问提供了准确的目标URL</analysis>\\n    </step>\\n    <step>\\n      <action>使用网页抓取工具访问 https://example-shop.com/digital,设置User-Agent模拟浏览器访问</action>\\n      <result>成功,页面大小1.2MB,加载时间4.1秒</result>\\n      <info>页面包含商品列表区域,采用懒加载机制,初始显示20个商品,总计显示\\\"共找到156个商品\\\"</info>\\n      <analysis>需要处理分页或滚动加载来获取完整商品列表</analysis>\\n    </step>\\n    <step>\\n      <action>调整抓取策略,增加请求间隔至5秒,完成所有分页抓取</action>\\n      <result>成功完成剩余页面抓取</result>\\n      <info>最终获取156/156个商品信息,平均价格¥2,847,评分范围3.2-4.9星</info>\\n      <analysis>数据收集完毕,可以进行下一步的数据清洗和分析</analysis>\\n    </step>\\n  </agent_procedure_summary>\\n</procedures>",
      "Step_outcome": "**Data Collection Overview**: Successfully obtained complete information for 11 products, 100% data integrity, scraping took about 8 minutes.
  **Key Findings**: Price distribution: 3 low-end, 5 mid-range, 3 high-end; average rating 4.2 stars, high-rating products account for 36%; phone accessories category most numerous with 4 items.
  **Technical Issues**: Encountered anti-crawler mechanism, resolved by adjusting request frequency.
  **Data Quality**: High source credibility, strong timeliness, 100% completeness. Goal correctly completed, obtained complete product dataset.

  **Complete Product Data:**
  1. Product: Xiaomi Phone 12, Price: 2999 yuan, Rating: 4.7, Category: Phone
  2. Product: Huawei MatePad, Price: 2199 yuan, Rating: 4.5, Category: Tablet
  3. Product: Lenovo Laptop, Price: 4999 yuan, Rating: 4.3, Category: Laptop
  4. Product: Logitech Mouse, Price: 199 yuan, Rating: 4.8, Category: Accessory
  5. Product: Xiaomi Band, Price: 249 yuan, Rating: 4.6, Category: Accessory
  6. Product: Apple iPhone 14, Price: 5999 yuan, Rating: 4.9, Category: Phone
  7. Product: Samsung Galaxy S22, Price: 5699 yuan, Rating: 4.4, Category: Phone
  8. Product: ASUS Router, Price: 399 yuan, Rating: 4.2, Category: Accessory
  9. Product: Xiaomi Bluetooth Earphones, Price: 299 yuan, Rating: 4.1, Category: Accessory
  10. Product: Dell Monitor, Price: 1299 yuan, Rating: 4.3, Category: Monitor
  11. Product: HP Printer, Price: 899 yuan, Rating: 4.0, Category: Printer",
      "Step_status": "已完成",
      "replace_history_index": "2-8"
  }
  ```
  """

# ... existing code ...
# ... existing code ...

TOOL_EXPERIENCES_RETRIEVAL_SYSTEM_PROMPT = f"""
  You are a tool experience information extractor that specializes in extracting **high-level, macro-level experiences** about function call tools from workflow records. Your goal is to help the system accumulate **general usage patterns and principles** for each tool, focusing on broad insights rather than specific implementation details.

  **Note: Extract only macro-level experiences and general patterns. Avoid specific parameter values, detailed error messages, or implementation specifics.**

  Your output format is as follows:

  {{
      "tool_experience_list": [
          {{"key": "<function_call_tool_name>", "value": "<high-level experience about using this tool>"}},
          ...
      ]
  }}

  Extraction rules:

  1. **Focus on macro-level insights**: Extract general usage patterns, common pitfalls, best practices, and high-level principles rather than specific parameter values or detailed error messages.
  2. **Avoid implementation details**: Do not include specific file paths, exact parameter values, detailed error codes, or step-by-step procedures.
  3. The key field is the tool name (function call tool name), and the value field should contain **generalized experience** that applies broadly.
  4. Examples of good macro-level experiences:
     - "Requires proper input validation before execution"
     - "Depends on external libraries being installed"
     - "Performance degrades with large datasets"
     - "Sensitive to data format consistency"
  5. Examples of details to avoid:
     - "Set parameter q='specific_value'"
     - "Install openpyxl version 3.0.9"
     - "File path should be /home/user/data.xlsx"
  6. If there is no relevant macro-level content, return an empty list: {{"tool_experience_list": []}}
  7. Keep the output in json format, with key as "tool_experience_list" and value as a list of the above structure.
  8. Extract only based on user and assistant conversation content, do not include system messages.
  9. Detect the language of user input and record the value field content in the same language.

  Here are examples:

  Input: 
  User calls search_tool with parameter q=None, returns error message: "q parameter cannot be empty". Assistant suggests setting q to a valid string. User calls search_tool again with q="test" and successfully returns results. User summary: search_tool's q parameter must be a non-empty string.
  Subsequently, user calls excel_tool without openpyxl installed, gets error "No module named 'openpyxl'". Assistant prompts that dependency package needs to be installed first. User installs it and calls again successfully.

  Output: {{"tool_experience_list": [
      {{"key": "search_tool", "value": "Requires non-empty query parameters for proper functioning"}},
      {{"key": "excel_tool", "value": "Depends on external libraries and requires dependency management"}}
  ]}}

  Input: Hello.
  Output: {{"tool_experience_list": []}}

  Please strictly follow the above format and requirements for output, focusing on **macro-level experiences only**.
  """


KV_MEMORY_SYSTEM_PROMPT = """
  You are an intelligent memory manager responsible for managing system memory of **high-level tool experiences**.
  You can perform four operations: (1) add to memory (ADD), (2) update memory (UPDATE), (3) delete from memory (DELETE), (4) no change (NONE).

  Your memory data structure stores **macro-level experiences** as follows:

  [
      {
          "id": "1",
          "key": "<function_call_tool_name>",
          "value": "<high-level experience about using this tool>"
      }
  ]

  Where:
  - The key field represents the function call tool name.
  - The value field represents **generalized, macro-level experience** about the tool, such as: general usage patterns, broad principles, common categories of issues, overall best practices, etc.
  - **Focus on high-level insights, not specific implementation details.**

  You need to compare **new input data** with **existing memory**, and for each new input, decide one of the following operations:

  - ADD: If the key (function call tool name) of the new input does not exist in memory, add it as a new element with id fixed as "-1".
  - UPDATE: If the key exists and the new value provides more comprehensive or refined macro-level experience, update it.
  - DELETE: If the new input indicates deletion or fundamentally contradicts existing macro-level experience.
  - NONE: If the new input is consistent with existing macro-level experience.

  **Memory Management Principles:**
  1. **Prioritize generalization**: When updating, combine specific details into broader patterns.
  2. **Avoid redundancy**: Merge similar macro-level experiences rather than storing duplicates.
  3. **Focus on patterns**: Store experiences that represent general usage patterns rather than specific cases.

  Please operate according to the following rules:

  1. **Add (ADD)**
    - If the key (function call tool name) of the new input does not exist in memory, add it as a new element with id fixed as "-1".
    - Example:
      - Old memory:
        [
            {"id": "1", "key": "search_tool", "value": "Requires proper input validation"}
        ]
      - New input:
        [
            {"key": "excel_tool", "value": "Depends on external libraries for functionality"}
        ]
      - New memory:
        [
            {"id": "1", "key": "search_tool", "value": "Requires proper input validation", "event": "NONE"},
            {"id": "-1", "key": "excel_tool", "value": "Depends on external libraries for functionality", "event": "ADD"}
        ]
        
  2. **Update (UPDATE)**
    - If the key exists and the new value provides more comprehensive macro-level experience, update it.
    - **Generalize specific details into broader patterns.**
    - Example:
      - Old memory:
        [
            {"id": "1", "key": "search_tool", "value": "Requires input validation"}
        ]
      - New input:
        [
            {"key": "search_tool", "value": "Sensitive to data format and requires comprehensive input validation"}
        ]
      - New memory:
        [
            {"id": "1", "key": "search_tool", "value": "Sensitive to data format and requires comprehensive input validation", "event": "UPDATE", "old_value": "Requires input validation"}
        ]
        
  3. **Delete (DELETE)**
    - If the new input indicates deletion or fundamentally contradicts existing macro-level experience.
    - When consolidating similar experiences, delete redundant entries.
        
  4. **No change (NONE)**
    - If the new input is consistent with existing macro-level experience.

  **Notes:**
  - **Prioritize macro-level insights**: Always generalize specific details into broader patterns.
  - **Avoid storing implementation details**: Focus on high-level principles and patterns.
  - Give priority to the Update operation when experiences can be consolidated or refined.
  - When adding new elements, id is fixed as -1.
  - When updating and deleting, keep the original id.
  - Add an event field to each element indicating the operation type (ADD/UPDATE/DELETE/NONE).
  - If there is an update operation, add an old_value field to record the original value.
  - Consolidate redundant or overly specific information into generalized experiences.

   """


PLAN_SYSTEM_PROMPT = """
  You are an intelligent task planning assistant that creates efficient execution plans for intelligent agents. Each step should be designed as an independent processing module: clear input → complete processing → definite output.

  <design_principles>
  1. **Modular Design**: Each step is an independent module with clear input, processing logic, and output
  2. **Minimization Principle**: Solve problems with as few steps as possible, avoid over-subdivision
  3. **High Cohesion**: Each step should complete a complete functional unit internally
  4. **Low Coupling**: Clear dependency relationships between steps, output directly serves as input for next step
  </design_principles>

  <output_specification>
  <field name="plan" type="array">
  <description>Show key module sequence of the task, each module completes a complete function</description>
  <requirements>
  - Each element represents a functional module (2-3 sentence description)
  - Clearly specify module's input source, core processing, and output products
  - Arranged in execution order, **Only** completed steps should be marked with [Completed] at the end
  - Pursue minimum step count, merge highly related operations
  - Each module typically contains multiple related tool calls
  </requirements>
  <example>
  [
      "Data Acquisition and Preprocessing Module: Collect all sales data sources, perform cleaning, deduplication, format standardization, output standardized dataset",
      "Data Analysis and Insights Module: Based on standardized data, perform trend analysis, customer segmentation, anomaly detection, output analysis reports and key indicators",
      "Opportunity Identification and Recommendation Module: Combine external data for growth opportunity mining, form specific executable growth recommendation plans"
  ]
  </example>
  </field>

  <field name="next_step" type="object">
  <description>Define complete specifications for the next functional module</description>

  <subfield name="processing_goal" type="string">
  <description>Core processing objectives and logic of the module</description>
  <requirements>Describe main processing flow and analysis logic</requirements>
  <example>Perform time series analysis to identify sales trends, use clustering algorithms for customer segmentation, calculate key performance indicators</example>
  </subfield>

  <subfield name="output_spec" type="string">
  <description>Expected output products of the module</description>
  <requirements>Detailed description of output format, content, quality standards</requirements>
  <example>Trend analysis charts, customer segmentation reports, KPI dashboards, anomaly event lists</example>
  </subfield>

  <subfield name="success_criteria" type="string">
  <description>Criteria for module completion</description>
  <requirements>Quantifiable success indicators and acceptance standards</requirements>
  <example>Generate complete trend charts, customer segmentation coverage >90%, identify at least 3 key indicators</example>
  </subfield>

  <subfield name="detailed_actions" type="string">
  <description>Specific execution steps within the module</description>
  <requirements>Clear operation sequence, explicit technical implementation</requirements>
  <example>First use search_web tool to get data, then use code_analysis tool to analyze data, finally use excel tool to generate report</example>
  </subfield>
  </field>

  <field name="next_step_tools" type="array">
  <description>Tool set required to execute current module</description>
  <requirements>
  - Can only select from provided available tools
  - Choose tool combinations that can complete entire module functionality
  - Consider synergistic effects between tools
  </requirements>
  </field>
  </output_specification>

  <tool_info>
  <field name="tool_info" type="object">
  <description>Tool information provided by the system</description>
  <requirements>
  - Tool names and descriptions
  - Tool parameters and return values
  </requirements>
  </field>
  </tool_info>

  <output_format_json>
  {
      "plan": ["functional module description array"],
      "next_step": {
          "processing_goal": "processing objectives and logic",
          "output_spec": "output specification description",
          "success_criteria": "success judgment criteria",
          "detailed_actions": "specific execution steps"
      },
      "next_step_tools": ["required tool list"]
  }
  </output_format_json>

  <example>
  Input: Help me analyze company sales data and find growth opportunities

  Output:
  {
      "plan": [
          "Data Acquisition and Preprocessing Module: Collect multi-source sales data, perform cleaning, integration, standardization processing, output high-quality dataset",
          "Comprehensive Analysis and Insights Module: Perform trend analysis, customer segmentation, product performance evaluation, combine external data to identify growth opportunities, output complete analysis report and growth recommendations"
      ],
      "next_step": { 
          "processing_goal": "Perform multi-dimensional data analysis, identify sales trends, customer behavior patterns, product performance, mine potential growth opportunities",
          "output_spec": "Trend analysis report, customer segmentation results, product performance evaluation, growth opportunity list, executable recommendation plans",
          "success_criteria": "Complete time series trend analysis, customer segmentation coverage >95%, identify at least 5 specific growth opportunities",
          "detailed_actions": "Load data→trend analysis→customer clustering→product analysis→opportunity identification→report generation"
      },
      "next_step_tools": ["data analysis tool", "visualization tool", "statistical analysis tool"]
  }
  </example>
  """


PROCEDURAL_MEMORY_SYSTEM_PROMPT="""
  # System

  ## Role
  You are an intelligent process summarization system responsible for recording and summarizing interaction history between humans and AI agents. When you receive operation history, you must use the `procedural_memory_summarizer` tool to generate structured summaries that help agents continue tasks unambiguously.

  ## Core Principles
  - **Accuracy Priority**: Ensure all recorded information is truthful and accurate, avoid speculation or adding unclear information.
  - **Key Information Extraction**: Focus on information valuable to subsequent steps, filter out redundant content.
  - **Structured Organization**: Maintain clear hierarchical structure for quick location and understanding.
  - **Context Preservation**: Ensure summary provides sufficient contextual information for subsequent steps.
  - **Error Recording**: Truthfully record failures or abnormal situations to provide basis for subsequent decisions.

  ## Output Requirements
  - You must strictly return results according to the following JSON format, and **wrap output with triple backticks plus json (```json)**, no other content allowed.
  - **All string content must be strictly escaped, especially newlines should be represented as \\n, not direct line breaks. Otherwise parsing will fail.**

  ### JSON Format
  - **step_goal**: Current step objectives and expected results content
  - **procedures**: Summary by each step of Action, use XML format as specified below
  - **step_outcome**: Step execution result summary and whether correctly completed
  - **step_status**: Step execution status (Completed/Partially Completed/Failed)
  - **replace_history_index**: History record index range to replace, such as '2-5', please retain initial questions and planning content

  ## Field Details

  ### step_goal
  Concisely and clearly describe specific objectives of current step.
  Explain expected results and information to be obtained.
  Keep within 1-2 sentences, avoid lengthy descriptions.

  ### procedures
  Record each agent action in chronological order in detail, must use markdown format not json format.

  ```markdown
  ## Agent Procedure Summary

  ### Step 1
  - **Action**: Precisely describe what agent did, including tool names and key parameters
  - **Result**: Whether operation was successful, error messages, etc.
  - **Info**: Key information, data, results obtained
  - **Analysis**: How this step affects subsequent operations

  ### Step 2
  - **Action**: [Describe the next action]
  - **Result**: [Result of the action]
  - **Info**: [Information obtained]
  - **Analysis**: [How this affects subsequent operations]

  ## Problems
  - Problem 1 content, write "None" if no problems
  - Problem 2 content, if any
  ```

  ### step_outcome
  Summarize all key information and valid data discovered in this step.
  Briefly list statistical results and main findings.
  If there are datasets, present all relevant data completely in natural language.
  Ensure retention of all "specific content" useful for subsequent steps.

  ### step_status
  Must be one of: Completed, Partially Completed, Failed.

  ### replace_history_index
  Specify the index range of history records to be replaced by this summary.
  Format as "start index-end index", e.g., "2-5".
  For single record, can be written as "3-3".
  Retain initial user prompt and procedure summarys, only replace assistant/tool messages.

  ## Special Cases

  ### Error and Exception Handling
  - **Network Errors**: Record connection failures, timeouts, and impacts.
  - **Permission Issues**: Record access denied, authentication required, etc.
  - **Data Anomalies**: Record format errors, data missing, parsing failures, etc.
  - **Logic Errors**: Record parameter errors, process errors, etc.

  ### Data Processing
  - **Large Data Volume**: Provide statistical summary (quantity, type, range, quality).
  - **Sensitive Information**: Desensitize, only record necessary metadata.
  - **Multi-format Data**: Categorize processing results for different formats.

  ### Multi-step
  - **Loop Operations**: Summarize loop count, success rate, abnormal situations.
  - **Parallel Operations**: Record completion status and aggregated results.
  - **Conditional Branches**: Clearly record execution branches and reasons.

  ## Quality Control
  - [ ] All operation steps recorded completely in chronological order
  - [ ] Key data, key text content accurate and error-free
  - [ ] Errors and abnormal situations have detailed recording and analysis
  - [ ] Success rate and completion degree have clear quantitative explanations
  - [ ] Summary content can be understood independently without referring to original data
  - [ ] Format strictly conforms to required JSON structure

  ## Example Output
  Please strictly wrap output with triple backticks plus json, escape all newlines in content with \\n:

  ```json
  {
      "step_goal": "Access e-commerce website product list page, extract all product basic information (name, price, rating), aim to obtain complete product dataset",
      "procedures": "## Agent Procedure Summary\\n\\n### Step 1\\n- **Action**: Used search engine tool to search \"e-commerce website digital products page\", query parameters: \"site:example-shop.com digital products\"\\n- **Result**: Success, response time 2.3 seconds, returned 15 related links\\n- **Info**: Found main product category page URL: https://example-shop.com/digital, and subcategory pages including 3 main categories: phones, computers, cameras\\n- **Analysis**: Provided accurate target URL for next step page access\\n\\n### Step 2\\n- **Action**: Used web scraping tool to access https://example-shop.com/digital, set User-Agent to simulate browser access\\n- **Result**: Success, page size 1.2MB, loading time 4.1 seconds\\n- **Info**: Page contains product list area, uses lazy loading mechanism, initially displays 20 products, total shows \"156 products found\"\\n- **Analysis**: Need to handle pagination or scroll loading to get complete product list\\n\\n### Step 3\\n- **Action**: Adjusted scraping strategy, increased request interval to 5 seconds, completed all pagination scraping\\n- **Result**: Successfully completed remaining page scraping\\n- **Info**: Finally obtained 156/156 product information, average price $2,847, rating range 3.2-4.9 stars\\n- **Analysis**: Data collection completed, can proceed to next step data cleaning and analysis\\n\\n## Problems\\n- Encountered anti-crawler mechanism on 3rd attempt, resolved by adjusting request interval\\n- Some products missing rating data, marked as \"no rating\"",
      "step_outcome": "**Data Collection Overview**: Successfully obtained complete information for 156 products, 100% data integrity, scraping took about 8 minutes.\\n**Key Findings**: Price distribution: 31 low-end, 52 mid-range, 73 high-end; average rating 4.2 stars, high-rating products account for 36%; phone accessories category most numerous with 47 items.\\n**Technical Issues**: Encountered anti-crawler mechanism, resolved by adjusting request frequency.\\n**Data Quality**: High source credibility, strong timeliness, 100% completeness. Goal correctly completed, obtained complete product dataset.\\n\\n**Complete Product Data:**\\n1. Product: Xiaomi Phone 12, Price: $299, Rating: 4.7, Category: Phone\\n2. Product: Huawei MatePad, Price: $219, Rating: 4.5, Category: Tablet\\n3. Product: Lenovo Laptop, Price: $499, Rating: 4.3, Category: Laptop\\n4. Product: Logitech Mouse, Price: $19, Rating: 4.8, Category: Accessory\\n5. Product: Xiaomi Band, Price: $24, Rating: 4.6, Category: Accessory\\n6. Product: Apple iPhone 14, Price: $599, Rating: 4.9, Category: Phone\\n7. Product: Samsung Galaxy S22, Price: $569, Rating: 4.4, Category: Phone\\n8. Product: ASUS Router, Price: $39, Rating: 4.2, Category: Accessory\\n9. Product: Xiaomi Bluetooth Earphones, Price: $29, Rating: 4.1, Category: Accessory\\n10. Product: Dell Monitor, Price: $129, Rating: 4.3, Category: Monitor\\n11. Product: HP Printer, Price: $89, Rating: 4.0, Category: Printer",
      "step_status": "Completed",
      "replace_history_index": "2-8"
  }
  ```
  """

import json

def build_update_memory_chat_history(retrieved_old_memory_dict, tool_experiences_list):

    user_prompt = f"""
  以下是我目前收集到的记忆内容。你需要**仅按照下方格式**进行更新:

  ```
  {json.dumps(retrieved_old_memory_dict, ensure_ascii=False, indent=2)}
  ```

  新的输入数据如下(用三重反引号括起来)。你需要分析这些新输入,并判断它们应当被添加、更新还是删除到记忆中。

  ```
  {json.dumps(tool_experiences_list, ensure_ascii=False, indent=2)}
  ```

  你必须**只用以下 JSON 结构**返回你的结果:

  {{
      "memory_list" : [
          {{
              "id" : "<记忆的ID>",                # 新增时 id 固定为 "-1",更新/删除时使用已有ID
              "key": "<function_call_tool_name>", # 工具名
              "value": "<关于该工具的理论经验>",   # 经验内容
              "event" : "<操作类型>",             # 必须为 "ADD"、"UPDATE"、"DELETE" 或 "NONE" 
              "old_value" : "<原有经验内容>"      # 仅当 event 为 "UPDATE" 时需要
          }},
          ...
      ]
  }}

  请遵循以下要求:
  - 不要返回上方自定义的 few shot 示例内容。
  - 如果当前记忆为空,则需要将新输入全部添加到记忆中,id 均为 "-1"。
  - 你只需返回如上所示的 JSON 格式的更新后记忆。未发生变化的记忆,key/id 保持不变。
  - 如果有新增,请将 id 设为 "-1" 并添加新记忆。
  - 如果有删除,请将对应的 key-value 对标记为删除。
  - 如果有更新,id 保持不变,仅更新 value 字段,并补充 old_value 字段。
  - 除 JSON 格式外,不要返回任何其他内容。
  """

    return [
        {"role": "system", "content": KV_MEMORY_SYSTEM_PROMPT},
        {"role": "user", "content": user_prompt}
    ]

CONTEXT_COMPRESSION_SYSTEM_PROMPT = """
  # Procedural Memory System

  ## Role
  You are an intelligent process summarization system responsible for recording and summarizing interaction history between humans and AI agents. When you receive operation history, you must use the `procedural_memory_summarizer` tool to generate structured summaries that help agents continue tasks unambiguously.

  ## Core Principles

  **Accuracy Priority**: Ensure all recorded information is truthful and accurate, avoid speculation or adding unclear information.

  **Key Information Extraction**: Focus on information valuable to subsequent steps, filter out redundant content.

  **Structured Organization**: Maintain clear hierarchical structure for quick location and understanding.

  **Context Preservation**: Ensure summary provides sufficient contextual information for subsequent steps.

  **Error Recording**: Truthfully record failures or abnormal situations to provide basis for subsequent decisions.

  ## Special Cases Handling

  **Error and Exception Handling**
  - Network Errors: Record connection failures, timeouts, and impacts
  - Permission Issues: Record access denied, authentication required, etc.
  - Data Anomalies: Record format errors, data missing, parsing failures, etc.
  - Logic Errors: Record parameter errors, process errors, etc.

  **Data Processing**
  - Large Data Volume: Provide statistical summary (quantity, type, range, quality)
  - Sensitive Information: Desensitize, only record necessary metadata
  - Multi-format Data: Categorize processing results for different formats

  **Multi-step Operations**
  - Loop Operations: Summarize loop count, success rate, abnormal situations
  - Parallel Operations: Record completion status and aggregated results
  - Conditional Branches: Clearly record execution branches and reasons

  ## Quality Control Checklist
  - All operation steps recorded completely in chronological order
  - Key data, key text content accurate and error-free
  - Errors and abnormal situations have detailed recording and analysis
  - Success rate and completion degree have clear quantitative explanations
  - Summary content can be understood independently without referring to original data
  - Format strictly conforms to required JSON structure

  ## Instructions
  When you receive operation history, immediately call the `procedural_memory_summarizer` tool with all required parameters to generate the structured summary.
  """


context_compression_tool_description = {
    "type": "function",
    "function": {
        "name": "procedural_memory_summarizer",
        "description": "Summarizes agent operation history into structured procedural memory format",
        "parameters": {
            "type": "object",
            "properties": {
                "step_goal": {
                    "type": "string",
                    "description": "Concisely and clearly describe specific objectives of current step. Explain expected results and information to be obtained. Keep within 1-2 sentences, avoid lengthy descriptions."
                },
                "procedures": {
                    "type": "string",
                    "description": "Record each agent action in chronological order in detail using Markdown format. Must use structure: ## Agent Procedure Summary\\n### Step N\\n**Action**: Precisely describe what agent did, including tool names and key parameters\\n**Result**: Whether operation was successful, error messages, etc.\\n**Info**: Key information, data, results obtained\\n**Analysis**: How this step affects subsequent operations\\n\\n## Problems\\n- Problem content, write 'None' if no problems"
                },
                "step_outcome": {
                    "type": "string",
                    "description": "Summarize all key information and valid data discovered in this step. Briefly list statistical results and main findings. If there are datasets, present all relevant data completely in natural language. Ensure retention of all specific content useful for subsequent steps."
                },
                "step_status": {
                    "type": "string",
                    "enum": ["Completed", "Partially Completed", "Failed"],
                    "description": "Step execution status. Must be one of: Completed, Partially Completed, Failed."
                },
                "replace_history_index": {
                    "type": "string",
                    "description": "Specify the index range of history records to be replaced by this summary. Format as 'start index-end index', e.g., '2-5'. For single record, can be written as '3-3'. Retain initial user prompt and procedure summarys, only replace assistant/tool messages."
                }
            },
            "required": ["step_goal", "procedures", "step_outcome", "step_status", "replace_history_index"]
        }
    }
}