agentengine-sdk-python 0.8.8__py3-none-any.whl → 0.8.9__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {agentengine_sdk_python-0.8.8.dist-info → agentengine_sdk_python-0.8.9.dist-info}/METADATA +12 -15
- {agentengine_sdk_python-0.8.8.dist-info → agentengine_sdk_python-0.8.9.dist-info}/RECORD +181 -173
- ksadk/_build_provenance.json +1 -1
- ksadk/agui/agent.py +1 -1
- ksadk/builders/framework_requirements.py +1 -1
- ksadk/cli/cmd_run.py +4 -0
- ksadk/cli/cmd_web.py +4 -0
- ksadk/cli/runtime_logging.py +30 -0
- ksadk/context_engine/capabilities.py +13 -9
- ksadk/conversations/context.py +5 -0
- ksadk/conversations/message_projection.py +28 -19
- ksadk/conversations/runtime_compaction.py +6 -2
- ksadk/conversations/runtime_input.py +4 -0
- ksadk/conversations/runtime_invocation.py +5 -1
- ksadk/conversations/runtime_metadata.py +6 -7
- ksadk/conversations/runtime_payloads.py +5 -1
- ksadk/conversations/runtime_persistence.py +23 -0
- ksadk/conversations/runtime_preparation.py +128 -5
- ksadk/events/adapters/langgraph.py +53 -1
- ksadk/kernel/control.py +16 -22
- ksadk/kernel/history_projection.py +191 -58
- ksadk/kernel/ingress.py +16 -2
- ksadk/kernel/postgres_event_log.py +10 -2
- ksadk/kernel/postgres_store.py +81 -2
- ksadk/kernel/run_references.py +38 -0
- ksadk/kernel/worker_controls.py +11 -1
- ksadk/mcp_runtime/oauth/kernel.py +5 -0
- ksadk/mcp_runtime/oauth/local/broker.py +18 -8
- ksadk/mcp_runtime/oauth/local/protocol.py +3 -11
- ksadk/mcp_runtime/oauth/local/transport.py +9 -2
- ksadk/resource_runtime/contracts.py +39 -1
- ksadk/resource_runtime/managed_projection.py +8 -6
- ksadk/resource_runtime/worker.py +8 -1
- ksadk/runners/_adk_canonical_stream.py +16 -15
- ksadk/runners/_langgraph_answer_contract.py +1 -7
- ksadk/runners/_langgraph_canonical_stream.py +13 -6
- ksadk/runners/_langgraph_context.py +265 -0
- ksadk/runners/_langgraph_model_budget.py +397 -0
- ksadk/runners/_langgraph_runner_streams.py +7 -6
- ksadk/runners/adk_runner.py +13 -21
- ksadk/runners/langgraph_runner.py +130 -8
- ksadk/runners/usage_accumulator.py +40 -0
- ksadk/runtime/conversation_execution.py +26 -4
- ksadk/server/factory.py +3 -1
- ksadk/server/routes/control.py +24 -8
- ksadk/server/routes/history_items.py +157 -0
- ksadk/server/routes/kernel_ingress.py +51 -12
- ksadk/server/routes/local_kernel_control.py +16 -0
- ksadk/server/routes/models.py +26 -0
- ksadk/server/routes/openai_compat.py +10 -3
- ksadk/server/routes/projection.py +4 -0
- ksadk/server/routes/run.py +4 -2
- ksadk/server/routes/sessions.py +20 -1
- ksadk/server/static/assets/{ArtifactsPanel-Cdr5C_vM.js → ArtifactsPanel-iZ_6CAQ2.js} +1 -1
- ksadk/server/static/assets/{CodeBlock-BWRafud9.js → CodeBlock-D6uglm5n.js} +1 -1
- ksadk/server/static/assets/{MathMessageMarkdown-CFKeJIvy.js → MathMessageMarkdown-D6ojdxCG.js} +1 -1
- ksadk/server/static/assets/{MermaidBlock-BneAGUOd.js → MermaidBlock-Csk2ivT7.js} +6 -6
- ksadk/server/static/assets/{NativeTerminalPanel-BHI85zDO.js → NativeTerminalPanel-CCJA1XSH.js} +1 -1
- ksadk/server/static/assets/{OfficialA2uiRenderer-BOmT36lJ.js → OfficialA2uiRenderer-C06jus9N.js} +1 -1
- ksadk/server/static/assets/{abnfDiagram-VCTEODGH-DQvlOsBI.js → abnfDiagram-VCTEODGH-BhrF5tmL.js} +1 -1
- ksadk/server/static/assets/{arc-CGx0e4pN.js → arc-Btsh3kfy.js} +1 -1
- ksadk/server/static/assets/architecture-7GRP2DOG-BSXho3cq.js +1 -0
- ksadk/server/static/assets/{architectureDiagram-5GKGNRK7-WyybcNKX.js → architectureDiagram-5GKGNRK7-Cl8MfAmp.js} +1 -1
- ksadk/server/static/assets/{blockDiagram-NRAW4CY4-C9Cx_GVs.js → blockDiagram-NRAW4CY4-CeHSr8E_.js} +1 -1
- ksadk/server/static/assets/{c4Diagram-UCG6FXSJ-Bh7X3bGr.js → c4Diagram-UCG6FXSJ-BSD7RUKo.js} +1 -1
- ksadk/server/static/assets/channel-BpyDVKis.js +1 -0
- ksadk/server/static/assets/chunk-2Q5K7J3B-HLcpmPI2.js +1 -0
- ksadk/server/static/assets/{chunk-5VM5RSS4-cP9XupZj.js → chunk-5VM5RSS4-wz0pkBjp.js} +1 -1
- ksadk/server/static/assets/{chunk-F27PBJKO-Bm77fvR9.js → chunk-F27PBJKO-shjQ7cPM.js} +1 -1
- ksadk/server/static/assets/{chunk-G27WJ6UU-DDKNPesQ.js → chunk-G27WJ6UU-DL_MfUsu.js} +1 -1
- ksadk/server/static/assets/{chunk-JWPE2WC7-BqiUDB7s.js → chunk-JWPE2WC7-moUdwYpb.js} +1 -1
- ksadk/server/static/assets/{chunk-LCL6LL3I-C528mMZy.js → chunk-LCL6LL3I-B3QcDsZM.js} +1 -1
- ksadk/server/static/assets/{chunk-POPQ4Y6H-CzZ9g_Uf.js → chunk-POPQ4Y6H-BLBKb48A.js} +1 -1
- ksadk/server/static/assets/{chunk-RHFEMEQ7-CXj4tUZ-.js → chunk-RHFEMEQ7-CXR6ZzOx.js} +1 -1
- ksadk/server/static/assets/{chunk-SVP7TREG-BHyd85pS.js → chunk-SVP7TREG-DoR8BxkO.js} +1 -1
- ksadk/server/static/assets/{chunk-XXDRQBXY-Qe29PHGe.js → chunk-XXDRQBXY-8HJaIq0q.js} +1 -1
- ksadk/server/static/assets/classDiagram-DTDB5LWJ-DlNiRtSN.js +1 -0
- ksadk/server/static/assets/classDiagram-v2-JRS7N3AN-DlNiRtSN.js +1 -0
- ksadk/server/static/assets/{cose-bilkent-JH36ORCC-maRKgeec.js → cose-bilkent-JH36ORCC-DnSSHHqS.js} +1 -1
- ksadk/server/static/assets/cynefin-OW5HDTMX-DxNym3qg.js +1 -0
- ksadk/server/static/assets/{cynefinDiagram-5FMLGOSQ-CoD0bEvn.js → cynefinDiagram-5FMLGOSQ-4eCi_Rw5.js} +1 -1
- ksadk/server/static/assets/{dagre-3AP2YEHR-BkpgQqyW.js → dagre-3AP2YEHR-DpPk9jsh.js} +1 -1
- ksadk/server/static/assets/{dagre-BUaTFrYa.js → dagre-G789Mawt.js} +1 -1
- ksadk/server/static/assets/{diagram-S7CK7UJ4-25HdTbjE.js → diagram-S7CK7UJ4-7x2FScRJ.js} +1 -1
- ksadk/server/static/assets/{diagram-UQ7AKVKN-KO-WqOIg.js → diagram-UQ7AKVKN-DGvliWDD.js} +1 -1
- ksadk/server/static/assets/{diagram-VSXAHHWV-B360BkDQ.js → diagram-VSXAHHWV-C0i4uEyS.js} +1 -1
- ksadk/server/static/assets/{diagram-VX7I27RA-COr1Oqsg.js → diagram-VX7I27RA-C7SmgIl5.js} +1 -1
- ksadk/server/static/assets/{diagram-Z3DM3KII-CgaKrIwB.js → diagram-Z3DM3KII-Xca7TB4U.js} +1 -1
- ksadk/server/static/assets/{dist-By-MVndb.js → dist-B3rsrE8r.js} +1 -1
- ksadk/server/static/assets/{dist-Bsuusoi0.js → dist-BM6KIyhr.js} +1 -1
- ksadk/server/static/assets/{dist-DzdXSBJb.js → dist-BMZKBrmN.js} +1 -1
- ksadk/server/static/assets/{dist-DmWw0IXB.js → dist-BUGNcOx8.js} +1 -1
- ksadk/server/static/assets/{dist-DXOlCAay.js → dist-BbIN_x76.js} +1 -1
- ksadk/server/static/assets/{dist--HftQyp9.js → dist-CGB89Aru.js} +1 -1
- ksadk/server/static/assets/{dist-DKG0OYnk.js → dist-CGIWL8mC.js} +1 -1
- ksadk/server/static/assets/{dist-DgJ57sRe.js → dist-CHeuPGXZ.js} +1 -1
- ksadk/server/static/assets/{dist-_0Xgu_07.js → dist-CMJq3QL-.js} +1 -1
- ksadk/server/static/assets/{dist-CwnkOyDn.js → dist-CiUeIDKz.js} +1 -1
- ksadk/server/static/assets/{dist-BoRn8a_t.js → dist-CmaZEf5M.js} +1 -1
- ksadk/server/static/assets/{dist-BQ116UdZ.js → dist-Cq4qlsg3.js} +1 -1
- ksadk/server/static/assets/{dist-CBo9AHUv.js → dist-DDVDyH7h.js} +1 -1
- ksadk/server/static/assets/{dist-DSw5Ju6-.js → dist-DDi9q5bO.js} +1 -1
- ksadk/server/static/assets/{dist-CxIl4t92.js → dist-DJ8mj6p4.js} +1 -1
- ksadk/server/static/assets/{dist-Brbrdmff.js → dist-DRGym1nc.js} +1 -1
- ksadk/server/static/assets/{dist-BEbS8Bag.js → dist-DcrTYAeW.js} +1 -1
- ksadk/server/static/assets/{dist-C9BTMVwS.js → dist-DpuxDDLa.js} +1 -1
- ksadk/server/static/assets/{dist-B3Jp5r1e.js → dist-EFGQ7G_k.js} +1 -1
- ksadk/server/static/assets/{dist-HHvdjADB.js → dist-IyCF0NNA.js} +1 -1
- ksadk/server/static/assets/{dist-Bxcu8Ca0.js → dist-Ny95vkRV.js} +1 -1
- ksadk/server/static/assets/{dist-CR61WJFC.js → dist-s5cohO5C.js} +1 -1
- ksadk/server/static/assets/{ebnfDiagram-PWID7BFC-DogDP2db.js → ebnfDiagram-PWID7BFC-DNwVczAJ.js} +1 -1
- ksadk/server/static/assets/{erDiagram-SSCWMZ5O-CeBDnUjR.js → erDiagram-SSCWMZ5O-OxFlhx6v.js} +1 -1
- ksadk/server/static/assets/eventmodeling-NTZA5JFV-C-gXDupK.js +1 -0
- ksadk/server/static/assets/flowDiagram-A5DVABFB-CkpCYD4e.js +1 -0
- ksadk/server/static/assets/{ganttDiagram-EL5Y4UJY-oIQL3MCp.js → ganttDiagram-EL5Y4UJY-D4krNgUz.js} +1 -1
- ksadk/server/static/assets/gitGraph-4MIJSDKK-0RSkEgC5.js +1 -0
- ksadk/server/static/assets/{gitGraphDiagram-WWUBYQGX-B7qz0cmZ.js → gitGraphDiagram-WWUBYQGX-BUSVPN1s.js} +1 -1
- ksadk/server/static/assets/index-B9UP7He9.css +2 -0
- ksadk/server/static/assets/index-COn4kn-G.js +1135 -0
- ksadk/server/static/assets/info-A6RAGUB7-C6KX7WO4.js +1 -0
- ksadk/server/static/assets/{infoDiagram-RXCK75RN-yQRC41Ya.js → infoDiagram-RXCK75RN-b6w3u3-O.js} +1 -1
- ksadk/server/static/assets/{ishikawaDiagram-5VMMS53U-BCu4HiwL.js → ishikawaDiagram-5VMMS53U-CsC7gzRt.js} +1 -1
- ksadk/server/static/assets/{journeyDiagram-EYS64GPL-JaqmO73K.js → journeyDiagram-EYS64GPL-BATLNoy1.js} +1 -1
- ksadk/server/static/assets/{kanban-definition-3QL26DDD-DQo4X8NO.js → kanban-definition-3QL26DDD-C49zL_s0.js} +1 -1
- ksadk/server/static/assets/{katex-t_DWylQw.js → katex-CDVgGGy2.js} +1 -1
- ksadk/server/static/assets/{linear-DWdnI35B.js → linear-DkyDumSy.js} +1 -1
- ksadk/server/static/assets/{mermaid-parser.core-BHrT2Pfe.js → mermaid-parser.core-BDX-LqSy.js} +2 -2
- ksadk/server/static/assets/{mindmap-definition-FBJOCRG2-PBVAhZYV.js → mindmap-definition-FBJOCRG2-DRPnzL2n.js} +1 -1
- ksadk/server/static/assets/packet-AYTQ26CC-CMMupxPW.js +1 -0
- ksadk/server/static/assets/{pegDiagram-XKGWAZYB-CU_cLfow.js → pegDiagram-XKGWAZYB-Zm-xrNrr.js} +1 -1
- ksadk/server/static/assets/pie-WAS4IAKB-Bfo0pa9v.js +1 -0
- ksadk/server/static/assets/{pieDiagram-E7YTZNPT-B1h0-_pW.js → pieDiagram-E7YTZNPT-a8C1Jxo4.js} +1 -1
- ksadk/server/static/assets/{quadrantDiagram-AXDQQJYC-DEMoGo5a.js → quadrantDiagram-AXDQQJYC-ihH8znBv.js} +1 -1
- ksadk/server/static/assets/radar-RG4KPBEZ-PRBiHoJx.js +1 -0
- ksadk/server/static/assets/railroad-74A4TZTK-BCJ-XMRJ.js +1 -0
- ksadk/server/static/assets/railroad-abnf-HS5TGJTU-Bbttj0Jz.js +1 -0
- ksadk/server/static/assets/railroad-ebnf-LZEXJU2U-Bo18264J.js +1 -0
- ksadk/server/static/assets/railroad-peg-WCYAUIDC-xZDJTFLD.js +1 -0
- ksadk/server/static/assets/{railroadDiagram-O6MQD6OU-BTAob2wg.js → railroadDiagram-O6MQD6OU-DD9lFfXo.js} +1 -1
- ksadk/server/static/assets/{requirementDiagram-EFPCY7ZU-Dl0Q2SuO.js → requirementDiagram-EFPCY7ZU-CGn93Iyb.js} +1 -1
- ksadk/server/static/assets/{sankeyDiagram-P5KCCOFB-DeaTWkur.js → sankeyDiagram-P5KCCOFB-BuyjrHuW.js} +1 -1
- ksadk/server/static/assets/{sequenceDiagram-WJ2MYXX4-gFQYrsOd.js → sequenceDiagram-WJ2MYXX4-D-Iw-30A.js} +1 -1
- ksadk/server/static/assets/{sizeCapture-X5ZJPWSS-pGQzddMB.js → sizeCapture-X5ZJPWSS-B0EHLwrD.js} +1 -1
- ksadk/server/static/assets/{stateDiagram-HBIQ2CUA-DmpYOZt1.js → stateDiagram-HBIQ2CUA-k9Zyg69R.js} +1 -1
- ksadk/server/static/assets/stateDiagram-v2-4QOOHH4V-tUEVnq9-.js +1 -0
- ksadk/server/static/assets/{swimlanes-XN3QIQJK-BfuSAsrr.js → swimlanes-XN3QIQJK-D2BewKmp.js} +1 -1
- ksadk/server/static/assets/swimlanesDiagram-VK2B7HYN-CGBmsrCQ.js +8 -0
- ksadk/server/static/assets/{timeline-definition-24CTP7MA-CgJwrrxf.js → timeline-definition-24CTP7MA-czSzsYbz.js} +1 -1
- ksadk/server/static/assets/treeView-Q6P3EWNA-CrEvfC8M.js +1 -0
- ksadk/server/static/assets/treemap-WGGIJYW6-D1TmShtn.js +1 -0
- ksadk/server/static/assets/{vennDiagram-4TSXK5OY-CBSAgXDd.js → vennDiagram-4TSXK5OY-BJF_qZLF.js} +1 -1
- ksadk/server/static/assets/wardley-WFR3VGLG-0gZiSyL0.js +1 -0
- ksadk/server/static/assets/{wardleyDiagram-VM6X3IG4-013TZ2JZ.js → wardleyDiagram-VM6X3IG4-BfwUuJpr.js} +1 -1
- ksadk/server/static/assets/{xychartDiagram-S5SC5T6Z-u6Frp8xp.js → xychartDiagram-S5SC5T6Z-Ci6Nsp4p.js} +1 -1
- ksadk/server/static/index.html +2 -2
- ksadk/server/static/ui-source.json +6 -6
- ksadk/sessions/_postgres_history_backfill.py +80 -0
- ksadk/sessions/_postgres_schema.py +117 -0
- ksadk/sessions/_postgres_tables.py +6 -0
- ksadk/sessions/base.py +24 -0
- ksadk/sessions/history_tool_refs.py +244 -0
- ksadk/sessions/in_memory.py +42 -0
- ksadk/sessions/local_service.py +78 -0
- ksadk/sessions/model_history.py +20 -0
- ksadk/sessions/postgres_service.py +215 -19
- ksadk/sessions/resilient.py +24 -0
- ksadk/studio/static/assets/{ChannelsPage-Do_mRmVu.js → ChannelsPage-Ot9iV_QP.js} +1 -1
- ksadk/studio/static/assets/{CodeBlock-DMIf4h33-CsO7rJKv.js → CodeBlock-DMIf4h33-Cfs6se0v.js} +1 -1
- ksadk/studio/static/assets/{MathMessageMarkdown-BesSeCYl-CiB60plG.js → MathMessageMarkdown-BesSeCYl-BIBDiQAl.js} +1 -1
- ksadk/studio/static/assets/{MessageMarkdown-CwMTvgFj-BzEmQkHH.js → MessageMarkdown-CwMTvgFj-CWM4n-9K.js} +2 -2
- ksadk/studio/static/assets/{OfficialA2uiRenderer-DI49Kpxu-BBxfdj3P.js → OfficialA2uiRenderer-DI49Kpxu-BZSumE9D.js} +1 -1
- ksadk/studio/static/assets/{TeamsPage-qNVYpcox.js → TeamsPage-CVOpMIMf.js} +2 -2
- ksadk/studio/static/assets/index-CW1D-Kqg.css +2 -0
- ksadk/studio/static/assets/index-DMexeQ1-.js +1155 -0
- ksadk/studio/static/index.html +3 -3
- ksadk/studio/static/ui-source.json +6 -6
- ksadk/version.py +1 -1
- ksadk/server/static/assets/architecture-7GRP2DOG-CFWjFkDL.js +0 -1
- ksadk/server/static/assets/channel-D6Dp_l74.js +0 -1
- ksadk/server/static/assets/chunk-2Q5K7J3B-CwmjhFYK.js +0 -1
- ksadk/server/static/assets/classDiagram-DTDB5LWJ-DsbvkCAV.js +0 -1
- ksadk/server/static/assets/classDiagram-v2-JRS7N3AN-DsbvkCAV.js +0 -1
- ksadk/server/static/assets/cynefin-OW5HDTMX-CBZRVuc4.js +0 -1
- ksadk/server/static/assets/eventmodeling-NTZA5JFV-vXZWMkpn.js +0 -1
- ksadk/server/static/assets/flowDiagram-A5DVABFB-5LRgzKLv.js +0 -1
- ksadk/server/static/assets/gitGraph-4MIJSDKK-BUWSkncI.js +0 -1
- ksadk/server/static/assets/index-4g1ndaxW.js +0 -1132
- ksadk/server/static/assets/index-DEVfafsU.css +0 -2
- ksadk/server/static/assets/info-A6RAGUB7-CzJ_YrOs.js +0 -1
- ksadk/server/static/assets/packet-AYTQ26CC-CXoos6fx.js +0 -1
- ksadk/server/static/assets/pie-WAS4IAKB-CjF9NoIt.js +0 -1
- ksadk/server/static/assets/radar-RG4KPBEZ-BQGWs9nw.js +0 -1
- ksadk/server/static/assets/railroad-74A4TZTK-B6znvLUK.js +0 -1
- ksadk/server/static/assets/railroad-abnf-HS5TGJTU-Bzquz0As.js +0 -1
- ksadk/server/static/assets/railroad-ebnf-LZEXJU2U-1IikGkb5.js +0 -1
- ksadk/server/static/assets/railroad-peg-WCYAUIDC-BKKgHzdc.js +0 -1
- ksadk/server/static/assets/stateDiagram-v2-4QOOHH4V-Dolxgkw3.js +0 -1
- ksadk/server/static/assets/swimlanesDiagram-VK2B7HYN-BuI1erYj.js +0 -8
- ksadk/server/static/assets/treeView-Q6P3EWNA-DtMdDH8N.js +0 -1
- ksadk/server/static/assets/treemap-WGGIJYW6-VoU8QtLZ.js +0 -1
- ksadk/server/static/assets/wardley-WFR3VGLG-YWSRB0Zm.js +0 -1
- ksadk/studio/static/assets/index-Cp9Unfkn.css +0 -2
- ksadk/studio/static/assets/index-HepnaEeR.js +0 -1152
- {agentengine_sdk_python-0.8.8.dist-info → agentengine_sdk_python-0.8.9.dist-info}/WHEEL +0 -0
- {agentengine_sdk_python-0.8.8.dist-info → agentengine_sdk_python-0.8.9.dist-info}/entry_points.txt +0 -0
- {agentengine_sdk_python-0.8.8.dist-info → agentengine_sdk_python-0.8.9.dist-info}/licenses/LICENSE +0 -0
- {agentengine_sdk_python-0.8.8.dist-info → agentengine_sdk_python-0.8.9.dist-info}/top_level.txt +0 -0
ksadk/_build_provenance.json
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"schemaVersion":1,"sourceCommit":"
|
|
1
|
+
{"schemaVersion":1,"sourceCommit":"36eab1449e509cf3012e874f4107243bd976e66a","sourceTree":"clean","version":"0.8.9"}
|
ksadk/agui/agent.py
CHANGED
|
@@ -416,7 +416,7 @@ class KsadkAGUIAgent(AGUIKernelMixin, AGUIResumeMixin, AGUIProjectionMixin):
|
|
|
416
416
|
return
|
|
417
417
|
try:
|
|
418
418
|
service = factory()
|
|
419
|
-
session = await service.
|
|
419
|
+
session = await service.get_session_metadata(session_id)
|
|
420
420
|
if session is not None:
|
|
421
421
|
await prime_session_metadata_for_user_turn(
|
|
422
422
|
service=service,
|
|
@@ -24,7 +24,7 @@ LANGCHAIN_ECOSYSTEM_REQUIREMENTS = (
|
|
|
24
24
|
"langgraph-checkpoint-sqlite>=2.0.0",
|
|
25
25
|
)
|
|
26
26
|
|
|
27
|
-
DEEPAGENTS_REQUIREMENTS = ("deepagents>=0.6.
|
|
27
|
+
DEEPAGENTS_REQUIREMENTS = ("deepagents>=0.6.8,<1.0.0",)
|
|
28
28
|
|
|
29
29
|
# codex runtime:openai-codex SDK(自带 codex CLI 二进制,见 PyPI cli-bin wheel)。
|
|
30
30
|
# 版本必须与 pyproject.toml 的 ksadk[codex] extra 保持一致:适配层
|
ksadk/cli/cmd_run.py
CHANGED
ksadk/cli/cmd_web.py
CHANGED
|
@@ -251,6 +251,10 @@ def web(agent_dir: str, port: int, host: str, model: str, no_open: bool):
|
|
|
251
251
|
|
|
252
252
|
setup_environment(agent_path)
|
|
253
253
|
|
|
254
|
+
from ksadk.cli.runtime_logging import configure_runtime_logging
|
|
255
|
+
|
|
256
|
+
configure_runtime_logging()
|
|
257
|
+
|
|
254
258
|
# 托管 Code 包直接启动 web,必须在加载 Agent 前初始化采集与上报。
|
|
255
259
|
try:
|
|
256
260
|
from ksadk.tracing import setup_tracing
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"""Defaults for CLI-hosted business logs, without claiming the root handler."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import logging
|
|
6
|
+
import os
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class _RuntimeFallback(logging.StreamHandler):
|
|
10
|
+
"""Used by logging only when no logger in the propagation chain has a handler."""
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def configure_runtime_logging() -> None:
|
|
14
|
+
root = logging.getLogger()
|
|
15
|
+
# Leave configured applications alone, including a custom/disabled lastResort.
|
|
16
|
+
# Keeping root.handlers empty lets an agent's later basicConfig take ownership.
|
|
17
|
+
if root.handlers or logging.lastResort is not logging._defaultLastResort:
|
|
18
|
+
return
|
|
19
|
+
value = os.getenv("LOG_LEVEL", "").strip().upper() or "INFO"
|
|
20
|
+
level = logging.getLevelNamesMapping().get(value)
|
|
21
|
+
invalid = level is None
|
|
22
|
+
if invalid:
|
|
23
|
+
level = logging.INFO
|
|
24
|
+
handler = _RuntimeFallback()
|
|
25
|
+
handler.setFormatter(logging.Formatter("%(asctime)s %(levelname)s %(name)s: %(message)s"))
|
|
26
|
+
logging.lastResort = handler
|
|
27
|
+
if root.level == logging.WARNING:
|
|
28
|
+
root.setLevel(level)
|
|
29
|
+
if invalid:
|
|
30
|
+
logging.getLogger(__name__).warning("Invalid LOG_LEVEL; using INFO")
|
|
@@ -304,14 +304,19 @@ def _durable_checkpoint_active(runner: Any) -> bool:
|
|
|
304
304
|
def _downgrade_compaction_owner_for_checkpoint_hook(
|
|
305
305
|
runner: Any, caps: ContextCapabilities
|
|
306
306
|
) -> ContextCapabilities:
|
|
307
|
-
"""
|
|
307
|
+
"""Standard message graphs receive SDK context at the Runner boundary.
|
|
308
308
|
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
``context_checkpoint`` 事件,对模型输入零贡献——继续声明 ``compaction_owner="ksadk"``
|
|
312
|
-
会让 compaction 门控在每个双阈值触发点白耗一次摘要 LLM 调用。降级后
|
|
313
|
-
``_plan_compaction`` 落 ``none`` 带,不产生 LLM 调用。其余字段不变。
|
|
309
|
+
Graphs with explicit framework summarization retain framework ownership.
|
|
310
|
+
Nonstandard state/hooks retain the existing capability contract.
|
|
314
311
|
"""
|
|
312
|
+
uses_model_context = getattr(runner, "uses_ksadk_model_context", None)
|
|
313
|
+
if callable(uses_model_context):
|
|
314
|
+
if uses_model_context():
|
|
315
|
+
return replace(caps, compaction_owner="ksadk")
|
|
316
|
+
from ksadk.runners._langgraph_context import framework_summary
|
|
317
|
+
|
|
318
|
+
if framework_summary(getattr(runner, "_agent", None)):
|
|
319
|
+
return replace(caps, compaction_owner="framework")
|
|
315
320
|
if caps.compaction_owner != "ksadk":
|
|
316
321
|
return caps
|
|
317
322
|
if not _prepare_state_hook_exported(runner) or not _durable_checkpoint_active(runner):
|
|
@@ -327,9 +332,8 @@ def capabilities_for_runner(runner: Any | None) -> ContextCapabilities:
|
|
|
327
332
|
``describe_context_capabilities``
|
|
328
333
|
走 ``_capabilities_for_detection_type``,故本函数对 BaseRunner 子类不会递归。已被 compaction
|
|
329
334
|
门控(``runtime_preparation`` proactive compaction)与 shadow plan / conformance 测试消费。
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
``_downgrade_compaction_owner_for_checkpoint_hook``)。
|
|
335
|
+
标准消息图由共用 Runner 接入 SDK 压缩;显式框架摘要保留 framework ownership。
|
|
336
|
+
非标准图继续使用原有 checkpoint + hook 降级规则。
|
|
333
337
|
"""
|
|
334
338
|
if runner is None:
|
|
335
339
|
return DEFAULT_CONTEXT_CAPABILITIES()
|
ksadk/conversations/context.py
CHANGED
|
@@ -22,6 +22,7 @@ CANONICAL_EVENT_TYPES = {
|
|
|
22
22
|
"approval_request",
|
|
23
23
|
"approval_response",
|
|
24
24
|
"attachment_ref",
|
|
25
|
+
"progress",
|
|
25
26
|
"reasoning",
|
|
26
27
|
"run_status",
|
|
27
28
|
"run_checkpoint",
|
|
@@ -87,6 +88,10 @@ def extract_event_text(event: SessionEvent) -> str:
|
|
|
87
88
|
这里优先取 `agent_input`,因为它通常比展示给 UI 的 display text 更接近
|
|
88
89
|
真实 prompt;如果没有,再退回到 parts/text。
|
|
89
90
|
"""
|
|
91
|
+
# Legacy progress rows can carry model-authored text for history replay.
|
|
92
|
+
# They contribute no model input, including when building a compaction prompt.
|
|
93
|
+
if str(event.event_type or "").strip().lower() == "progress":
|
|
94
|
+
return ""
|
|
90
95
|
metadata = event.metadata or {}
|
|
91
96
|
if metadata.get("agent_input"):
|
|
92
97
|
return sanitize_event_text_for_context(metadata["agent_input"])
|
|
@@ -269,7 +269,7 @@ def _project_tool_events(
|
|
|
269
269
|
approval_responses: Mapping[str, Mapping[str, Any]],
|
|
270
270
|
) -> list[dict[str, Any]]:
|
|
271
271
|
projected: list[dict[str, Any]] = []
|
|
272
|
-
pending_calls: list[dict[str, Any]] = []
|
|
272
|
+
pending_calls: list[tuple[str, Any, dict[str, Any]]] = []
|
|
273
273
|
pending_by_key: dict[str, dict[str, Any]] = {}
|
|
274
274
|
seen_result_receipts: set[str] = set()
|
|
275
275
|
|
|
@@ -288,7 +288,7 @@ def _project_tool_events(
|
|
|
288
288
|
continue
|
|
289
289
|
entry = _tool_event_from_call(event, metadata)
|
|
290
290
|
projected.append(entry)
|
|
291
|
-
pending_calls.append(entry)
|
|
291
|
+
pending_calls.append((pair_key, metadata.get("run_id"), entry))
|
|
292
292
|
if pair_key:
|
|
293
293
|
pending_by_key[pair_key] = entry
|
|
294
294
|
continue
|
|
@@ -308,19 +308,22 @@ def _project_tool_events(
|
|
|
308
308
|
pair_key = _tool_pair_key(metadata)
|
|
309
309
|
match = pending_by_key.get(pair_key) if pair_key else None
|
|
310
310
|
if match is None or match["Status"] != "running":
|
|
311
|
-
|
|
312
|
-
|
|
313
|
-
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
)
|
|
317
|
-
|
|
318
|
-
|
|
311
|
+
# Old runtimes sometimes only recorded a tool name. Use it when
|
|
312
|
+
# the remaining association is unambiguous, never across known IDs.
|
|
313
|
+
candidates = [
|
|
314
|
+
item for key, run_id, item in pending_calls
|
|
315
|
+
if item["Name"] == name and item["Status"] == "running"
|
|
316
|
+
and not (pair_key.startswith("call:") and key.startswith("call:"))
|
|
317
|
+
and not (run_id and metadata.get("run_id") and run_id != metadata["run_id"])
|
|
318
|
+
]
|
|
319
|
+
match = candidates[0] if len(candidates) == 1 else None
|
|
319
320
|
if match is None:
|
|
320
321
|
entry = _tool_event_from_call(event, metadata)
|
|
321
322
|
projected.append(entry)
|
|
322
323
|
else:
|
|
323
324
|
entry = match
|
|
325
|
+
if result_call_id := _explicit_tool_call_id(metadata):
|
|
326
|
+
entry["ToolCallId"] = result_call_id
|
|
324
327
|
output = metadata.get("tool_output", _event_text(event))
|
|
325
328
|
entry["Status"] = (
|
|
326
329
|
"failed" if metadata.get("tool_is_error") is True
|
|
@@ -335,13 +338,16 @@ def _project_tool_events(
|
|
|
335
338
|
return projected
|
|
336
339
|
|
|
337
340
|
|
|
341
|
+
def _explicit_tool_call_id(metadata: Mapping[str, Any]) -> Any:
|
|
342
|
+
receipt = metadata.get("tool_receipt")
|
|
343
|
+
return (metadata.get("call_id") or metadata.get("tool_call_id")
|
|
344
|
+
or (receipt.get("tool_call_id") if isinstance(receipt, Mapping) else None))
|
|
345
|
+
|
|
346
|
+
|
|
338
347
|
def _tool_pair_key(metadata: Mapping[str, Any]) -> str:
|
|
339
|
-
call_id = metadata
|
|
348
|
+
call_id = _explicit_tool_call_id(metadata)
|
|
340
349
|
if call_id:
|
|
341
350
|
return f"call:{call_id}"
|
|
342
|
-
tool_receipt = metadata.get("tool_receipt")
|
|
343
|
-
if isinstance(tool_receipt, Mapping) and tool_receipt.get("tool_call_id"):
|
|
344
|
-
return f"call:{tool_receipt['tool_call_id']}"
|
|
345
351
|
run_id = metadata.get("run_id")
|
|
346
352
|
if run_id:
|
|
347
353
|
return f"run:{run_id}:{metadata.get('tool_name') or 'tool'}"
|
|
@@ -349,10 +355,7 @@ def _tool_pair_key(metadata: Mapping[str, Any]) -> str:
|
|
|
349
355
|
|
|
350
356
|
|
|
351
357
|
def _tool_event_from_call(event: Mapping[str, Any], metadata: Mapping[str, Any]) -> dict[str, Any]:
|
|
352
|
-
|
|
353
|
-
call_id = metadata.get("call_id") or metadata.get("run_id")
|
|
354
|
-
if not call_id and isinstance(tool_receipt, Mapping):
|
|
355
|
-
call_id = tool_receipt.get("tool_call_id")
|
|
358
|
+
call_id = _explicit_tool_call_id(metadata) or metadata.get("run_id")
|
|
356
359
|
return {
|
|
357
360
|
"SeqId": event.get("SeqId"),
|
|
358
361
|
"Type": "tool_call",
|
|
@@ -696,12 +699,18 @@ def _normalize_canonical_event(
|
|
|
696
699
|
part = parts[0] if isinstance(parts, list) and parts else {}
|
|
697
700
|
call_id = part.get("call_id", "")
|
|
698
701
|
result = part.get("result", "")
|
|
702
|
+
name = normalized_metadata.get("tool_name")
|
|
703
|
+
if not name and isinstance(source_metadata, Mapping):
|
|
704
|
+
name = source_metadata.get("tool_name")
|
|
705
|
+
name = name if isinstance(name, str) else ""
|
|
699
706
|
normalized["EventType"] = "tool_result"
|
|
700
|
-
normalized["Content"] = {"call_id": call_id, "name":
|
|
707
|
+
normalized["Content"] = {"call_id": call_id, "name": name, "result": result}
|
|
701
708
|
normalized_metadata.update({
|
|
702
709
|
"call_id": call_id, "tool_output": result,
|
|
703
710
|
"tool_is_error": part.get("is_error") is True,
|
|
704
711
|
})
|
|
712
|
+
if name:
|
|
713
|
+
normalized_metadata["tool_name"] = name
|
|
705
714
|
elif item_kind == "data" and source.get("protocol") == "a2ui":
|
|
706
715
|
source_metadata = source.get("metadata")
|
|
707
716
|
if (
|
|
@@ -42,6 +42,7 @@ from ksadk.conversations.semantic_summary import (
|
|
|
42
42
|
summarize_compaction,
|
|
43
43
|
)
|
|
44
44
|
from ksadk.sessions import SessionEvent, resolve_session_service
|
|
45
|
+
from ksadk.sessions.model_history import read_model_history
|
|
45
46
|
|
|
46
47
|
logger = logging.getLogger(__name__)
|
|
47
48
|
|
|
@@ -213,6 +214,7 @@ async def preview_auto_compaction(
|
|
|
213
214
|
model_metadata: Mapping[str, Any] | None = None,
|
|
214
215
|
session_service_provider: Callable[[], Any] | None = None,
|
|
215
216
|
prompt_integration_mode: str = "",
|
|
217
|
+
compaction_owner: str = "",
|
|
216
218
|
) -> CompactionPlan:
|
|
217
219
|
"""在真正写入 turn 之前预估是否会触发自动压缩。
|
|
218
220
|
|
|
@@ -276,13 +278,14 @@ async def preview_auto_compaction(
|
|
|
276
278
|
attachments=effective_attachments,
|
|
277
279
|
attachment_results=effective_attachment_results,
|
|
278
280
|
)
|
|
279
|
-
events = await service
|
|
281
|
+
events = await read_model_history(service, session_id)
|
|
280
282
|
return _plan_compaction(
|
|
281
283
|
events,
|
|
282
284
|
model=model,
|
|
283
285
|
model_metadata=resolved_model_metadata,
|
|
284
286
|
pending_events=[pending_event],
|
|
285
287
|
prompt_integration_mode=prompt_integration_mode,
|
|
288
|
+
compaction_owner=compaction_owner,
|
|
286
289
|
)
|
|
287
290
|
|
|
288
291
|
|
|
@@ -370,6 +373,7 @@ async def compact_conversation_history(
|
|
|
370
373
|
session_service_provider: Callable[[], Any] | None = None,
|
|
371
374
|
prompt_integration_mode: str = "",
|
|
372
375
|
compaction_owner: str = "",
|
|
376
|
+
preloaded_events: list[SessionEvent] | None = None,
|
|
373
377
|
) -> SessionEvent | None:
|
|
374
378
|
"""把旧轮次折叠为 checkpoint。
|
|
375
379
|
|
|
@@ -380,7 +384,7 @@ async def compact_conversation_history(
|
|
|
380
384
|
"""
|
|
381
385
|
provider = session_service_provider or resolve_session_service
|
|
382
386
|
service = provider()
|
|
383
|
-
events = await service
|
|
387
|
+
events = preloaded_events if preloaded_events is not None else await read_model_history(service, session_id)
|
|
384
388
|
header_reader = getattr(service, "get_session_header", None) or getattr(
|
|
385
389
|
service, "get_session_metadata", None) or service.get_session
|
|
386
390
|
session = await header_reader(session_id)
|
|
@@ -505,6 +505,10 @@ def _build_runner_request_payload(
|
|
|
505
505
|
# (for example, its conversation approval profile) without leaking
|
|
506
506
|
# caller public metadata into the agent payload.
|
|
507
507
|
payload["request_metadata"] = dict(prepared.request_metadata)
|
|
508
|
+
uses_model_context = getattr(runner, "uses_ksadk_model_context", None)
|
|
509
|
+
if (prepared.langgraph_model_context is not None and callable(uses_model_context)
|
|
510
|
+
and uses_model_context() and prepared.resume_input is None):
|
|
511
|
+
payload["langgraph_model_context"] = dict(prepared.langgraph_model_context)
|
|
508
512
|
# PR E:hosted pipeline 接管(最高优先级)。当 build_run_input 产出 assembled_input 时,
|
|
509
513
|
# 用组装好的 system/input/history 直接覆盖 payload——它已含 compiled_prompt + working_state
|
|
510
514
|
# + planner 决策后的有序 messages。此分支满足后不再走 PR B/D2(避免双重注入)。
|
|
@@ -285,7 +285,11 @@ async def invoke_conversation_once(
|
|
|
285
285
|
)
|
|
286
286
|
raise
|
|
287
287
|
except Exception as exc:
|
|
288
|
-
if
|
|
288
|
+
if (
|
|
289
|
+
attempt == 0
|
|
290
|
+
and _is_prompt_too_long_error(exc)
|
|
291
|
+
and not getattr(exc, "ksadk_model_boundary_failure", False)
|
|
292
|
+
):
|
|
289
293
|
_baseline_ptl = True
|
|
290
294
|
_baseline_attempts = attempt + 1
|
|
291
295
|
try:
|
|
@@ -93,16 +93,15 @@ async def _update_session_metadata_after_assistant_turn(
|
|
|
93
93
|
model: str | None,
|
|
94
94
|
) -> None:
|
|
95
95
|
summary = _truncate_text(strip_reasoning_markup(assistant_text), SESSION_SUMMARY_MAX_CHARS)
|
|
96
|
-
if summary:
|
|
97
|
-
await service.update_session_metadata(session_id, summary=summary)
|
|
98
|
-
|
|
99
|
-
session = await service.get_session(session_id)
|
|
100
|
-
if not session:
|
|
96
|
+
if not summary:
|
|
101
97
|
return
|
|
98
|
+
# The update returns the session metadata. Reloading a full session here
|
|
99
|
+
# would replay its transcript before the caller can close the stream.
|
|
100
|
+
session = await service.update_session_metadata(session_id, summary=summary)
|
|
102
101
|
if (session.title_source or "").strip() != "fallback_first_prompt":
|
|
103
102
|
return
|
|
104
103
|
first_prompt = str(session.first_prompt or "").strip()
|
|
105
|
-
if not first_prompt
|
|
104
|
+
if not first_prompt:
|
|
106
105
|
return
|
|
107
106
|
|
|
108
107
|
next_title = build_heuristic_title(first_prompt=first_prompt, assistant_text=summary)
|
|
@@ -156,7 +155,7 @@ async def _refine_session_title_in_background(
|
|
|
156
155
|
|
|
157
156
|
if not title or is_low_quality_title(title, first_prompt=first_prompt):
|
|
158
157
|
return
|
|
159
|
-
session = await service.
|
|
158
|
+
session = await service.get_session_metadata(session_id)
|
|
160
159
|
if not session:
|
|
161
160
|
return
|
|
162
161
|
if title == (session.title or "").strip():
|
|
@@ -94,6 +94,9 @@ class PreparedConversationTurn:
|
|
|
94
94
|
flush_before_compaction: bool = True
|
|
95
95
|
provider_ref: str = "local-default"
|
|
96
96
|
session_context: dict[str, Any] = field(default_factory=dict)
|
|
97
|
+
# Internal new-turn context consumed by the shared LangGraphRunner.
|
|
98
|
+
# Native graph state and model nodes do not need an SDK-specific schema.
|
|
99
|
+
langgraph_model_context: dict[str, Any] | None = None
|
|
97
100
|
|
|
98
101
|
|
|
99
102
|
@dataclass
|
|
@@ -273,8 +276,9 @@ def build_chat_completions_payload(
|
|
|
273
276
|
model: Optional[str],
|
|
274
277
|
session_id: str,
|
|
275
278
|
metadata: Mapping[str, Any] | None = None,
|
|
279
|
+
usage: Mapping[str, Any] | None = None,
|
|
276
280
|
) -> dict[str, Any]:
|
|
277
|
-
usage = _chat_usage_payload(_usage_from_metadata(metadata))
|
|
281
|
+
usage = _chat_usage_payload(usage or _usage_from_metadata(metadata))
|
|
278
282
|
payload = {
|
|
279
283
|
"id": f"chatcmpl-{uuid.uuid4()}",
|
|
280
284
|
"object": "chat.completion",
|
|
@@ -22,6 +22,7 @@ from ksadk.conversations.runtime_constants import (
|
|
|
22
22
|
from ksadk.conversations.runtime_observability import _extract_deferred_tool_names
|
|
23
23
|
from ksadk.runtime_context import TRUSTED_IDENTITY_METADATA_KEY, PlatformIdentityContext
|
|
24
24
|
from ksadk.sessions import Session, SessionEvent, resolve_session_service
|
|
25
|
+
from ksadk.sessions.base import SessionEventQuery
|
|
25
26
|
from ksadk.sessions.agent_identity import session_agent_ids
|
|
26
27
|
from ksadk.sessions.invocation_identity import (
|
|
27
28
|
bind_or_validate_session_identity,
|
|
@@ -185,7 +186,27 @@ async def _find_latest_session_event(
|
|
|
185
186
|
service: Any,
|
|
186
187
|
session_id: str,
|
|
187
188
|
predicate: Callable[[SessionEvent], bool],
|
|
189
|
+
*,
|
|
190
|
+
invocation_id: str | None = None,
|
|
191
|
+
event_type: str | None = None,
|
|
188
192
|
) -> SessionEvent | None:
|
|
193
|
+
if invocation_id and event_type and callable(getattr(service, "query_events", None)):
|
|
194
|
+
before = None
|
|
195
|
+
try:
|
|
196
|
+
while True:
|
|
197
|
+
page = await service.query_events(SessionEventQuery(
|
|
198
|
+
session_ids=[session_id], invocation_id=invocation_id,
|
|
199
|
+
event_types=[event_type], before_seq_id=before,
|
|
200
|
+
order_by_seq=True, limit=EVENT_SCAN_PAGE_SIZE,
|
|
201
|
+
))
|
|
202
|
+
for event in reversed(page):
|
|
203
|
+
if predicate(event):
|
|
204
|
+
return event
|
|
205
|
+
if len(page) < EVENT_SCAN_PAGE_SIZE:
|
|
206
|
+
return None
|
|
207
|
+
before = min(event.seq_id for event in page)
|
|
208
|
+
except NotImplementedError:
|
|
209
|
+
pass
|
|
189
210
|
total = await service.count_events(session_id)
|
|
190
211
|
offset = 0
|
|
191
212
|
while offset < total:
|
|
@@ -238,6 +259,8 @@ async def append_run_status_event(
|
|
|
238
259
|
)
|
|
239
260
|
== status
|
|
240
261
|
),
|
|
262
|
+
invocation_id=invocation_id,
|
|
263
|
+
event_type="run_status",
|
|
241
264
|
)
|
|
242
265
|
if existing is not None:
|
|
243
266
|
return existing
|
|
@@ -2,6 +2,7 @@ from __future__ import annotations
|
|
|
2
2
|
|
|
3
3
|
import logging
|
|
4
4
|
import os
|
|
5
|
+
import uuid
|
|
5
6
|
from typing import Any, Callable, Dict, Mapping, Optional, Sequence
|
|
6
7
|
|
|
7
8
|
from ksadk.context_engine.shadow_plan import (
|
|
@@ -79,10 +80,60 @@ from ksadk.model_policy import model_policy_options_for_model
|
|
|
79
80
|
from ksadk.runtime_context import TRUSTED_IDENTITY_METADATA_KEY, PlatformIdentityContext
|
|
80
81
|
from ksadk.session_context import split_session_context
|
|
81
82
|
from ksadk.sessions import SessionEvent, resolve_session_service
|
|
83
|
+
from ksadk.sessions.model_history import read_model_history
|
|
82
84
|
|
|
83
85
|
logger = logging.getLogger(__name__)
|
|
84
86
|
|
|
85
87
|
|
|
88
|
+
def _langgraph_model_context(
|
|
89
|
+
events: list[SessionEvent], request_history: list[dict[str, str]], invocation_id: str
|
|
90
|
+
) -> dict[str, Any]:
|
|
91
|
+
# Compaction runs after persisting the current input. Its summary can share
|
|
92
|
+
# this invocation_id, but the current user/tool/answer events are not history.
|
|
93
|
+
previous = [
|
|
94
|
+
event
|
|
95
|
+
for event in events
|
|
96
|
+
if event.invocation_id != invocation_id
|
|
97
|
+
or canonical_event_type(event.event_type) == "context_checkpoint"
|
|
98
|
+
]
|
|
99
|
+
history = build_history_from_events(previous)
|
|
100
|
+
checkpoints = [
|
|
101
|
+
event
|
|
102
|
+
for event in previous
|
|
103
|
+
if canonical_event_type(event.event_type) == "context_checkpoint"
|
|
104
|
+
]
|
|
105
|
+
seeded = any(
|
|
106
|
+
(event.metadata or {}).get("context_source") == "request_history" for event in previous
|
|
107
|
+
)
|
|
108
|
+
if not checkpoints and not seeded:
|
|
109
|
+
history = _merge_request_history_with_session_history(request_history, history)
|
|
110
|
+
# Once a summary covers a prefix, client replay must not resurrect it.
|
|
111
|
+
boundary = max(
|
|
112
|
+
(int((event.metadata or {}).get("compacted_until_seq_id") or 0) for event in checkpoints),
|
|
113
|
+
default=0,
|
|
114
|
+
)
|
|
115
|
+
turns: dict[str, list[SessionEvent]] = {}
|
|
116
|
+
for event in previous:
|
|
117
|
+
if (
|
|
118
|
+
event.seq_id > boundary
|
|
119
|
+
and event.invocation_id
|
|
120
|
+
and canonical_event_type(event.event_type) != "context_checkpoint"
|
|
121
|
+
):
|
|
122
|
+
turns.setdefault(event.invocation_id, []).append(event)
|
|
123
|
+
return {
|
|
124
|
+
"version": 1,
|
|
125
|
+
"invocation_id": invocation_id,
|
|
126
|
+
"history": history,
|
|
127
|
+
"compacted_until_seq_id": boundary,
|
|
128
|
+
"uncompacted": not checkpoints,
|
|
129
|
+
"turns": [
|
|
130
|
+
{"invocation_id": turn, "history": build_history_from_events(items)}
|
|
131
|
+
for turn, items in turns.items()
|
|
132
|
+
if any(canonical_event_type(event.event_type) == "user_message" for event in items)
|
|
133
|
+
],
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
|
|
86
137
|
async def build_run_input(
|
|
87
138
|
*,
|
|
88
139
|
agent_id: str,
|
|
@@ -141,6 +192,10 @@ async def build_run_input(
|
|
|
141
192
|
resolved_user_id = session.user_id
|
|
142
193
|
resolved_session_id = session.id
|
|
143
194
|
resolved_invocation_id = str(invocation_id or new_run_id(resolved_session_id))
|
|
195
|
+
uses_model_context = getattr(runner, "uses_ksadk_model_context", None)
|
|
196
|
+
integrated_model_context = (
|
|
197
|
+
callable(uses_model_context) and uses_model_context()
|
|
198
|
+
) or (runner is None and runtime_type in {"langgraph", "langchain"})
|
|
144
199
|
resolved_model_metadata = await _resolve_runtime_model_metadata(
|
|
145
200
|
model,
|
|
146
201
|
model_metadata=model_metadata,
|
|
@@ -203,7 +258,7 @@ async def build_run_input(
|
|
|
203
258
|
run_mode=caller_run_mode,
|
|
204
259
|
run_trigger=RUN_TRIGGER_CHECKPOINT_RESUME,
|
|
205
260
|
)
|
|
206
|
-
event_history = await service
|
|
261
|
+
event_history = await read_model_history(service, resolved_session_id)
|
|
207
262
|
history = build_history_from_events(event_history)
|
|
208
263
|
responses_history = project_responses_history(event_history)
|
|
209
264
|
return PreparedConversationTurn(
|
|
@@ -399,7 +454,7 @@ async def build_run_input(
|
|
|
399
454
|
"_ksadk_gateway_approval_resume": True,
|
|
400
455
|
}
|
|
401
456
|
del existing_events
|
|
402
|
-
event_history = await service
|
|
457
|
+
event_history = await read_model_history(service, resolved_session_id)
|
|
403
458
|
history = build_history_from_events(event_history)
|
|
404
459
|
responses_history = project_responses_history(event_history)
|
|
405
460
|
return PreparedConversationTurn(
|
|
@@ -475,7 +530,8 @@ async def build_run_input(
|
|
|
475
530
|
|
|
476
531
|
if normalized_instructions:
|
|
477
532
|
event_metadata["instructions"] = normalized_instructions
|
|
478
|
-
|
|
533
|
+
previous_events = await read_model_history(service, resolved_session_id)
|
|
534
|
+
deferred_tool_names = _latest_deferred_tool_names(previous_events)
|
|
479
535
|
if deferred_tool_names and "deferred_tool_names" not in normalized_request_metadata:
|
|
480
536
|
normalized_request_metadata["deferred_tool_names"] = deferred_tool_names
|
|
481
537
|
if normalized_custom_metadata:
|
|
@@ -483,6 +539,26 @@ async def build_run_input(
|
|
|
483
539
|
if normalized_request_metadata:
|
|
484
540
|
event_metadata["runtime_metadata"] = normalized_request_metadata
|
|
485
541
|
|
|
542
|
+
if integrated_model_context and not build_history_from_events(previous_events):
|
|
543
|
+
# A new session may arrive with caller-provided history. Seed it once so
|
|
544
|
+
# later current-only requests and compaction share the same source.
|
|
545
|
+
seed_turn = 0
|
|
546
|
+
for index, item in enumerate(build_request_history(normalized_messages[:-1])):
|
|
547
|
+
if item["role"] == "user":
|
|
548
|
+
seed_turn += 1
|
|
549
|
+
await append_conversation_event(
|
|
550
|
+
session_id=resolved_session_id,
|
|
551
|
+
author="user" if item["role"] == "user" else agent_id,
|
|
552
|
+
role=item["role"],
|
|
553
|
+
text=item["content"],
|
|
554
|
+
invocation_id=f"{resolved_invocation_id}:history:{seed_turn}",
|
|
555
|
+
event_id=str(
|
|
556
|
+
uuid.uuid5(uuid.NAMESPACE_URL, f"ksadk:{resolved_session_id}:history:{index}")
|
|
557
|
+
),
|
|
558
|
+
metadata={"context_source": "request_history"},
|
|
559
|
+
session_service_provider=provider,
|
|
560
|
+
)
|
|
561
|
+
|
|
486
562
|
await append_conversation_event(
|
|
487
563
|
session_id=resolved_session_id,
|
|
488
564
|
author="user",
|
|
@@ -517,9 +593,17 @@ async def build_run_input(
|
|
|
517
593
|
if runner is not None
|
|
518
594
|
else capabilities_for_runtime_type(runtime_type)
|
|
519
595
|
)
|
|
596
|
+
# The history prefix consists of committed, immutable events. Read only
|
|
597
|
+
# its suffix after persisting the input and after committing a summary.
|
|
598
|
+
history_cursor = max((event.seq_id for event in previous_events), default=0)
|
|
599
|
+
event_history = previous_events + await read_model_history(
|
|
600
|
+
service, resolved_session_id, after_seq_id=history_cursor,
|
|
601
|
+
)
|
|
602
|
+
history_cursor = max((event.seq_id for event in event_history), default=0)
|
|
520
603
|
checkpoint = await _compact_conversation_history_with_governance(
|
|
521
604
|
governance_state,
|
|
522
605
|
session_id=resolved_session_id,
|
|
606
|
+
preloaded_events=event_history,
|
|
523
607
|
author=agent_id,
|
|
524
608
|
invocation_id=resolved_invocation_id,
|
|
525
609
|
model=model,
|
|
@@ -529,7 +613,7 @@ async def build_run_input(
|
|
|
529
613
|
prompt_integration_mode=prompt_integration_mode,
|
|
530
614
|
compaction_owner=_caps.compaction_owner,
|
|
531
615
|
)
|
|
532
|
-
event_history
|
|
616
|
+
event_history.extend(await read_model_history(service, resolved_session_id, after_seq_id=history_cursor))
|
|
533
617
|
history = build_history_from_events(event_history)
|
|
534
618
|
responses_history = project_responses_history(event_history)
|
|
535
619
|
request_history = build_request_history(normalized_messages[:-1])
|
|
@@ -556,6 +640,12 @@ async def build_run_input(
|
|
|
556
640
|
[event for event in event_history if event.invocation_id != resolved_invocation_id]
|
|
557
641
|
),
|
|
558
642
|
)
|
|
643
|
+
model_context = (
|
|
644
|
+
_langgraph_model_context(event_history, request_history, resolved_invocation_id)
|
|
645
|
+
if integrated_model_context else None
|
|
646
|
+
)
|
|
647
|
+
if model_context is not None:
|
|
648
|
+
hosted_history = model_context["history"]
|
|
559
649
|
# PR D2:取最新 checkpoint 的 WorkingState(仅 ksadk_hosted 路径重注入)。
|
|
560
650
|
# 非 ksadk_hosted → working_state=None(零注入,Runner 输入与旧逻辑一致)。
|
|
561
651
|
# PTL retry 后 _refresh_history 也会重读 events,但此处 build_run_input 首次构建时取一次即可;
|
|
@@ -566,6 +656,7 @@ async def build_run_input(
|
|
|
566
656
|
|
|
567
657
|
prepared = PreparedConversationTurn(
|
|
568
658
|
session_context=session_context.to_payload(),
|
|
659
|
+
langgraph_model_context=model_context,
|
|
569
660
|
session_id=resolved_session_id,
|
|
570
661
|
invocation_id=resolved_invocation_id,
|
|
571
662
|
user_id=resolved_user_id,
|
|
@@ -747,6 +838,13 @@ async def _maybe_fill_hosted_pipeline(
|
|
|
747
838
|
"estimated_tokens": result.assembled.estimated_tokens,
|
|
748
839
|
"warnings": list(result.assembled.warnings),
|
|
749
840
|
}
|
|
841
|
+
if prepared.langgraph_model_context is not None:
|
|
842
|
+
from ksadk.context_engine.hosted_pipeline import assembled_to_payload
|
|
843
|
+
|
|
844
|
+
selected = assembled_to_payload(result.assembled)["history"]
|
|
845
|
+
if selected != prepared.langgraph_model_context["history"]:
|
|
846
|
+
prepared.langgraph_model_context["uncompacted"] = False
|
|
847
|
+
prepared.langgraph_model_context["history"] = selected
|
|
750
848
|
|
|
751
849
|
|
|
752
850
|
def _latest_checkpoint_working_state(events: Sequence[SessionEvent]) -> dict[str, Any] | None:
|
|
@@ -772,7 +870,7 @@ async def _refresh_history(
|
|
|
772
870
|
"""在 compaction 后刷新 prepared turn 的 history 视图。"""
|
|
773
871
|
provider = session_service_provider or resolve_session_service
|
|
774
872
|
service = provider()
|
|
775
|
-
event_history = await service
|
|
873
|
+
event_history = await read_model_history(service, prepared.session_id)
|
|
776
874
|
prepared.history = _merge_request_history_with_session_history(
|
|
777
875
|
prepared.request_history,
|
|
778
876
|
build_history_from_events(event_history),
|
|
@@ -781,4 +879,29 @@ async def _refresh_history(
|
|
|
781
879
|
prepared.request_responses_history,
|
|
782
880
|
project_responses_history(event_history),
|
|
783
881
|
)
|
|
882
|
+
if prepared.langgraph_model_context is not None:
|
|
883
|
+
prepared.langgraph_model_context = _langgraph_model_context(
|
|
884
|
+
event_history, prepared.request_history, prepared.invocation_id
|
|
885
|
+
)
|
|
886
|
+
if prepared.assembled_input is not None:
|
|
887
|
+
# PTL retries must not reuse the pre-compaction planner selection.
|
|
888
|
+
budget = (prepared.context_plan or {}).get("budget") or {}
|
|
889
|
+
await _maybe_fill_hosted_pipeline(
|
|
890
|
+
prepared,
|
|
891
|
+
compiled_prompt=prepared.compiled_prompt,
|
|
892
|
+
user_input=prepared.user_input,
|
|
893
|
+
history=prepared.langgraph_model_context["history"],
|
|
894
|
+
working_state=prepared.working_state,
|
|
895
|
+
model_metadata=prepared.model_metadata,
|
|
896
|
+
prompt_integration_mode=prepared.prompt_integration_mode,
|
|
897
|
+
context_engine_rollout="enabled",
|
|
898
|
+
memory_recall_enabled=prepared.memory_recall_enabled,
|
|
899
|
+
runtime_type="langgraph",
|
|
900
|
+
session_id=prepared.session_id,
|
|
901
|
+
invocation_id=prepared.invocation_id,
|
|
902
|
+
user_id=prepared.user_id,
|
|
903
|
+
agent_id=prepared.agent_id,
|
|
904
|
+
agent_max_input_tokens=budget.get("max_input_tokens"),
|
|
905
|
+
agent_reserve_output_tokens=budget.get("reserved_output_tokens"),
|
|
906
|
+
)
|
|
784
907
|
return prepared
|