Context Assembly
| 块 | 定义 |
|---|---|
system_instruction | 系统提示。Langfuse/本地模板 + <<占位符>> 替换。记忆检索还会往这里写 |
contents | 对话数组。本页重点 |
tools | 工具 schema。不进 contents |
1. before_model 之前(动态加载)
| 何时 | 做什么 | 落到哪 |
|---|---|---|
HTTP /run | 若开了系统提醒,把 <system-reminder> 前置进用户原文(写进 session,后面每轮都会带着) | 当前轮 user 文本最前面 |
| ADK Runner 组请求 | 用 session.events + 本轮 new_message 填 contents | 尚未规范化的 contents |
ADK 拉工具(get_tools) | Skill 工具集发现系统 skill + 用户 skill,把目录写进请求级缓存 | 还不进 contents;等 build_and_inject_context 才贴 |
| Router 并行预取 | 记忆 / 知识库 / 会话相关历史 / 时间解析 / 工具预选,和路由模型一起跑 | 请求级缓存或 session.state,等流水线消费 |
Skill 正文(SKILL.md)不在首轮 preamble 里。只有模型调用 load_skill 之后,正文作为工具返回进 session.events,下一轮重建历史时才出现。
2. before_model pipeline(静态顺序)
每次模型调用都跑一遍
名单是静态的。content_normalization 清空 contents 再按 session.events 重建,所以自己的拼接必须排在它后面。
| # | 步骤 | 代码 | 改 contents? | 配置 |
|---|---|---|---|---|
| 1 | 指标 | metrics_and_state_reset | 否 | — |
| 2 | 追踪 | observability_init | 否 | — |
| 3 | 取消 | execution_context_callback | 否 | — |
| 4 | web_search 短路 | ensure_ppl_agent_search_tool_choice | 否 | — |
| 5 | Router 强制选工具 | router_tool_choice | 否 | — |
| 6 | 系统提示 + 记忆占位符 | prompt_and_memory | 否。写 system_instruction;记忆结果放请求级缓存 | inject_dict + 系统提示模板 <<占位符>>。不读 contextSections |
| 7 | 工具定义 | tool_definitions | 否。改 tools[] | Langfuse 覆盖描述;动态工具召回 |
| 8 | 凭据候选抓取 | credential_context_capture | 否 | 有凭据文本才抓 |
| 9 | 会话相关历史(持久化模式) | qrc_persist_inject | 改 session.events | queryRelatedContextInjectorMode=persistent_injection。默认关,走 qrc_inject |
| 10 | 工具预选注入 | tool_prefilter_inject | 改 session.events | toolPrefilter.enabled,sg-test 默认关 |
| 11 | 重建滑动窗口 | content_normalization | 清空 contents,从 session.events 重建;当前轮补消息时间;文件/图片 | 历史压缩开关关则跳过重建 |
| 12 | 转账成功清袋 | bank_transfer_event_clear | 条件改 | bank_event success |
| 13 | 记忆编辑目标 | memory_edit_target_inject | 当前轮 user 末尾加 <memory_edit_target> | 前端预选了记忆才有 |
| 14 | lite 历史改写 | lite_history_rewrite | 改历史 tool 形态 | 只 assistant_lite |
| 15 | 权限 | permission_status_injection | 否。写入请求级缓存 | 预取有结果才有 |
| 16 | Shopee 账号 | shopee_account_context_injection | 否。写入请求级缓存 | 预取有结果才有 |
| 17 | 知识库预取 | knowledge | 否。写入请求级缓存 | 预取有结果才有 |
| 18 | 拼 preamble / query-context | build_and_inject_context | 头部插入 preamble;当前轮 user 文本前拼接 query-context / 知识库 | contextSections + skills,见 §4 |
| 19 | 会话相关历史(默认模式) | qrc_inject | 插到 preamble 后面、滑动窗口前面 | enableQueryRelatedContext;Router 跳过 |
| 20 | 延迟工具名披露 | deferred_tools_delta | 头部(或尾部变更提示)加 <system-reminder> | tool_search 对当前用户可用 |
| 21 | 搜索结果(改写器) | inject_search_results_for_synth | 条件改 | 只 web_search 改写器 |
| 22 | 凭据打码 | credential_context_redaction | 改文本,不增语义块 | — |
| 23 | 供应商格式 | model_adaptation | 改文本,不增语义块 | — |
| 24 | 压图 | compress_oversized_inline_images | 改图片,不增语义块 | — |
| 25 | 时间锚点 | temporal_anchor_callback | 在「当前最后一条 user」末尾再开一个 part,贴 <temporal_context> | temporalAnchor.enabled + agentWhitelist。sg-test 开;Bank UAT 关 |
| 26 | 原文再贴一份 | ensure_user_query_tail | 时间块后再开一个 part,<recency_query> | 只贴在「最后一条 user 仍是用户原话」时;工具返回那一轮不贴 |
| 27 | 校验截断 | context_validation_and_limit | 截断,不增语义块 | — |
| 28 | 调用次数 | llm_call_count_limit | 短路,不增语义块 | — |
| 29 | 自部署 Router | router_self_deployed_adapter | 短路,不增语义块 | — |
2.1 用户原话,工具返回的注入
不走 pipeline。ADK 写入 session.events,第 11 步拷进 contents。
| 谁写入 events | 形态 | |
|---|---|---|
| 用户原话 | runner.run_async(new_message=),一次 /run 一条 | role=user 文本 |
| 工具返回 | ADK 跑完工具后追加,每轮工具一条 | role=user 的 function_response JSON |
3. 具体 context
首轮(用户刚说话,还没调工具)
[0] user preamble(一段文本里按序):
<skills> 系统 skill + 用户 skill 目录
<connectors> 连接器目录(有才有)
<permissions> 设备权限(有才有)
<shopee_account_actions>(有才有)
<user-context> 与当前问句无关的画像/记忆(白名单裁)
[1] model 固定一句确认,给请求缓存当断点
[2..K] 会话相关历史(有召回才有)
[K+1..N] 滑动窗口:更早的 user / model / 工具调用 / 工具返回
[N+1] user 同一个 text part 里从前到后:
(可能有)<system-reminder>
<query-context>
<queryed_profile> 跟当前问句相关的画像/记忆
<queryed_memory> 预取的详细记忆(实验组)
<knowledge-context> 或 Bank 资源知识库(有才有)
<msg_time>
用户原文
后面另开的 part(有才有):
<memory_edit_target>
<temporal_context>
<recency_query> ← 再 copy 一份原文
4. Bank assistant_professional 配置
| 位置 | 段名 contextSections | Bank |
|---|---|---|
<user-context> | high_level_profile | 开 |
<user-context> | communication_style | 开 |
<user-context> | real_time_update_from_chat | 开 |
<user-context> | chat_messages_memories | 开 |
<user-context> | memory_retention | 开 |
<user-context> | user_emails | 关 |
<user-context> | personal_particulars | 关 |
<user-context> | state_of_mind | 关 |
<user-context> | dynamic_context | 关 |
<user-context> | explicit_memories | 关 |
<query-context> | time_summary | 开 |
<query-context> | chat_topic | 开 |
<query-context> | predicted_query_memory | 开 |
<query-context> | detailed_profile | 开 |
<query-context> | fact_memory | 开 |
<query-context> | truth | 开 |
<query-context> | contact_list | 开 |
<query-context> | personal_particulars_details | 关 |
<query-context> | state_of_mind_schedule | 关 |