agentengine-sdk-python 0.7.0__py3-none-any.whl → 0.8.1__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.7.0.dist-info → agentengine_sdk_python-0.8.1.dist-info}/METADATA +52 -20
- agentengine_sdk_python-0.8.1.dist-info/RECORD +644 -0
- {agentengine_sdk_python-0.7.0.dist-info → agentengine_sdk_python-0.8.1.dist-info}/WHEEL +1 -1
- ksadk/a2a/__init__.py +180 -11
- ksadk/a2a/bootstrap.py +332 -0
- ksadk/a2a/card.py +15 -0
- ksadk/a2a/context_store.py +203 -0
- ksadk/a2a/control_plane.py +1056 -0
- ksadk/a2a/credential.py +99 -0
- ksadk/a2a/event_adapter.py +149 -0
- ksadk/a2a/executor.py +361 -137
- ksadk/a2a/external_transport.py +516 -0
- ksadk/a2a/identity.py +304 -0
- ksadk/a2a/ids.py +22 -0
- ksadk/a2a/langgraph.py +285 -0
- ksadk/a2a/resume_store.py +201 -0
- ksadk/a2a/routes.py +112 -0
- ksadk/a2a/server.py +102 -62
- ksadk/a2a/service_env.py +33 -0
- ksadk/a2a/space_client.py +1122 -0
- ksadk/a2a/task_adapter.py +423 -0
- ksadk/a2a/task_event_dispatcher.py +167 -0
- ksadk/a2a/task_event_outbox.py +326 -0
- ksadk/a2a/task_store.py +206 -0
- ksadk/a2a_card.py +101 -0
- ksadk/a2ui/__init__.py +27 -0
- ksadk/a2ui/core.py +186 -0
- ksadk/a2ui/models.py +166 -0
- ksadk/a2ui/renderer.py +198 -0
- ksadk/agui/__init__.py +9 -0
- ksadk/agui/a2ui_projection.py +92 -0
- ksadk/agui/agent.py +872 -0
- ksadk/agui/config.py +92 -0
- ksadk/agui/routes.py +47 -0
- ksadk/api/client.py +238 -204
- ksadk/api/environment.py +27 -0
- ksadk/builders/__init__.py +2 -0
- ksadk/builders/base.py +16 -14
- ksadk/builders/code_builder.py +375 -270
- ksadk/builders/container_builder.py +240 -200
- ksadk/builders/framework_requirements.py +31 -12
- ksadk/builders/ks3_uploader.py +60 -29
- ksadk/builders/managed_runtime_builder.py +173 -0
- ksadk/builders/mcp_builder.py +91 -92
- ksadk/builders/requirements_utils.py +1 -5
- ksadk/cli/__init__.py +97 -171
- ksadk/cli/cmd_a2a.py +255 -84
- ksadk/cli/cmd_agent.py +19 -5
- ksadk/cli/cmd_build.py +158 -29
- ksadk/cli/cmd_completion.py +47 -35
- ksadk/cli/cmd_config.py +267 -198
- ksadk/cli/cmd_create.py +507 -740
- ksadk/cli/cmd_dashboard.py +120 -45
- ksadk/cli/cmd_deploy.py +135 -59
- ksadk/cli/cmd_destroy.py +44 -14
- ksadk/cli/cmd_eval.py +386 -0
- ksadk/cli/cmd_files.py +63 -24
- ksadk/cli/cmd_hermes.py +313 -135
- ksadk/cli/cmd_invoke.py +184 -122
- ksadk/cli/cmd_launch.py +76 -28
- ksadk/cli/cmd_mcp.py +98 -64
- ksadk/cli/cmd_model.py +14 -5
- ksadk/cli/cmd_openclaw.py +564 -268
- ksadk/cli/cmd_replay.py +71 -0
- ksadk/cli/cmd_run.py +58 -73
- ksadk/cli/cmd_status.py +80 -31
- ksadk/cli/cmd_studio.py +110 -0
- ksadk/cli/cmd_version.py +20 -13
- ksadk/cli/cmd_web.py +95 -18
- ksadk/cli/deploy_utils.py +20 -20
- ksadk/cli/dry_run.py +7 -4
- ksadk/cli/env_options.py +83 -0
- ksadk/cli/error_utils.py +37 -12
- ksadk/cli/global_options.py +4 -3
- ksadk/cli/local_runtime.py +1 -2
- ksadk/cli/model_catalog.py +6 -2
- ksadk/cli/network_options.py +41 -19
- ksadk/cli/resource_common.py +6 -2
- ksadk/cli/runtime_bootstrap.py +51 -0
- ksadk/cli/storage.py +0 -1
- ksadk/cli/ui.py +11 -3
- ksadk/cli/workflow_common.py +52 -18
- ksadk/codex/__init__.py +12 -0
- ksadk/codex/client.py +1042 -0
- ksadk/codex/phase.py +143 -0
- ksadk/codex/runtime.py +940 -0
- ksadk/common/aicp_env.py +0 -1
- ksadk/common/auth.py +2 -2
- ksadk/common/constants.py +1 -3
- ksadk/common/kop_client.py +232 -0
- ksadk/common/llm_utils.py +17 -13
- ksadk/compat/__init__.py +5 -0
- ksadk/compat/adk_compat.py +200 -0
- ksadk/compat/copilotkit_a2ui.py +330 -0
- ksadk/configs/__init__.py +12 -14
- ksadk/configs/env_registry.py +661 -116
- ksadk/configs/global_config.py +3 -2
- ksadk/configs/settings.py +133 -209
- ksadk/conversations/__init__.py +85 -92
- ksadk/conversations/attachment_storage.py +16 -11
- ksadk/conversations/attachments.py +44 -36
- ksadk/conversations/compaction_pipeline.py +12 -12
- ksadk/conversations/compaction_prompt.py +9 -5
- ksadk/conversations/context.py +221 -6
- ksadk/conversations/message_projection.py +438 -51
- ksadk/conversations/model_context.py +12 -6
- ksadk/conversations/model_options.py +0 -1
- ksadk/conversations/normalize.py +20 -16
- ksadk/conversations/reasoning_markup.py +1 -1
- ksadk/conversations/runtime.py +339 -5255
- ksadk/conversations/runtime_compaction.py +333 -0
- ksadk/conversations/runtime_constants.py +27 -0
- ksadk/conversations/runtime_governance.py +185 -0
- ksadk/conversations/runtime_input.py +660 -0
- ksadk/conversations/runtime_invocation.py +395 -0
- ksadk/conversations/runtime_metadata.py +475 -0
- ksadk/conversations/runtime_observability.py +367 -0
- ksadk/conversations/runtime_payloads.py +272 -0
- ksadk/conversations/runtime_persistence.py +440 -0
- ksadk/conversations/runtime_preparation.py +426 -0
- ksadk/conversations/runtime_resume.py +836 -0
- ksadk/conversations/runtime_stream_events.py +902 -0
- ksadk/conversations/runtime_streaming.py +700 -0
- ksadk/conversations/semantic_summary.py +17 -5
- ksadk/conversations/session_title.py +6 -3
- ksadk/deployment/__init__.py +15 -16
- ksadk/deployment/agent_access.py +12 -23
- ksadk/deployment/base.py +51 -38
- ksadk/deployment/managed_runtime.py +33 -0
- ksadk/deployment/manager.py +85 -71
- ksadk/deployment/providers/serverless.py +379 -210
- ksadk/deployment/registry.py +38 -32
- ksadk/deployment/state.py +11 -11
- ksadk/deployment/ui_config.py +1 -1
- ksadk/detection/__init__.py +1 -1
- ksadk/detection/detector.py +146 -83
- ksadk/detection/mcp_detector.py +9 -6
- ksadk/evaluation/__init__.py +84 -0
- ksadk/evaluation/a2a_adapter.py +444 -0
- ksadk/evaluation/adapters.py +54 -0
- ksadk/evaluation/contracts.py +409 -0
- ksadk/evaluation/evalset.py +351 -0
- ksadk/evaluation/evaluators.py +461 -0
- ksadk/evaluation/executor.py +118 -0
- ksadk/evaluation/storage.py +68 -0
- ksadk/evaluation/target.py +73 -0
- ksadk/events/__init__.py +25 -0
- ksadk/events/parser.py +191 -0
- ksadk/events/replay.py +37 -0
- ksadk/events/runtime_event.py +273 -0
- ksadk/events/store.py +317 -0
- ksadk/harness/__init__.py +35 -0
- ksadk/harness/app.py +269 -0
- ksadk/harness/config.py +181 -0
- ksadk/harness/reasoner.py +105 -0
- ksadk/harness/runtime.py +364 -0
- ksadk/harness/sandbox.py +141 -0
- ksadk/harness/tools.py +222 -0
- ksadk/hermes_terminal.py +28 -12
- ksadk/identity/resolver.py +15 -9
- ksadk/ids.py +32 -0
- ksadk/knowledge_base/adk_tool.py +1 -1
- ksadk/knowledge_base/client.py +38 -33
- ksadk/managed_a2a_card.py +160 -0
- ksadk/managed_runtime.py +195 -0
- ksadk/markdown.py +5 -2
- ksadk/mcp_runtime/__init__.py +9 -6
- ksadk/memory/__init__.py +1 -1
- ksadk/memory/adk/__init__.py +2 -0
- ksadk/memory/adk/backends/base_ltm_backend.py +2 -6
- ksadk/memory/adk/backends/http_ltm_backend.py +13 -21
- ksadk/memory/adk/backends/inmemory_ltm_backend.py +7 -13
- ksadk/memory/adk/backends/sdk_ltm_backend.py +36 -36
- ksadk/memory/adk/long_term_memory.py +34 -37
- ksadk/memory/adk/resilient_session_service.py +4 -5
- ksadk/memory/adk/short_term_memory.py +12 -21
- ksadk/memory/adk_tool.py +3 -2
- ksadk/memory/backends/base.py +19 -19
- ksadk/memory/backends/memory.py +24 -25
- ksadk/memory/backends/redis.py +31 -30
- ksadk/memory/langchain_tool.py +2 -1
- ksadk/memory/manager.py +52 -47
- ksadk/memory/service.py +16 -6
- ksadk/memory/tool.py +4 -0
- ksadk/model_policy.py +15 -7
- ksadk/model_proxy/__init__.py +51 -0
- ksadk/model_proxy/bootstrap.py +80 -0
- ksadk/model_proxy/cache.py +165 -0
- ksadk/model_proxy/config.py +66 -0
- ksadk/model_proxy/demo_webui.py +115 -0
- ksadk/model_proxy/detect.py +145 -0
- ksadk/model_proxy/gate.py +68 -0
- ksadk/model_proxy/namespace.py +201 -0
- ksadk/model_proxy/protocol_proxy.py +22 -0
- ksadk/model_proxy/server.py +443 -0
- ksadk/model_proxy/transform.py +735 -0
- ksadk/openclaw_gateway.py +29 -14
- ksadk/runners/adk_runner.py +354 -146
- ksadk/runners/base_runner.py +71 -29
- ksadk/runners/deepagents_runner.py +0 -1
- ksadk/runners/factory.py +18 -5
- ksadk/runners/langchain_runner.py +29 -742
- ksadk/runners/langgraph_runner.py +417 -97
- ksadk/runners/patch_langchain.py +16 -16
- ksadk/runners/remote_runner.py +134 -33
- ksadk/runners/usage_accumulator.py +1 -0
- ksadk/runners/utils/__init__.py +0 -10
- ksadk/runners/utils/loader.py +23 -7
- ksadk/runtime/__init__.py +67 -0
- ksadk/runtime/adapter.py +412 -0
- ksadk/runtime/conversation_execution.py +553 -0
- ksadk/runtime/executor.py +251 -0
- ksadk/runtime/factory.py +156 -0
- ksadk/runtime/framework_adapters.py +118 -0
- ksadk/runtime/launch.py +48 -0
- ksadk/runtime/preprocessing.py +131 -0
- ksadk/runtime/runner_adapter.py +1032 -0
- ksadk/runtime/runner_loading.py +36 -0
- ksadk/runtime_context.py +18 -3
- ksadk/runtime_state.py +0 -1
- ksadk/sandbox/__init__.py +4 -1
- ksadk/sandbox/backends/__init__.py +4 -1
- ksadk/sandbox/backends/e2b.py +13 -5
- ksadk/sandbox/backends/local_process.py +11 -3
- ksadk/sandbox/base.py +10 -15
- ksadk/sandbox/factory.py +14 -4
- ksadk/sandbox/registry.py +57 -6
- ksadk/server/__init__.py +39 -4
- ksadk/server/api_models.py +15 -5
- ksadk/server/app.py +24 -3928
- ksadk/server/composition.py +47 -0
- ksadk/server/factory.py +622 -0
- ksadk/server/routes/__init__.py +1 -0
- ksadk/server/routes/checkpoint_resolution.py +116 -0
- ksadk/server/routes/common.py +682 -0
- ksadk/server/routes/control.py +331 -0
- ksadk/server/routes/dependencies.py +98 -0
- ksadk/server/routes/misc.py +425 -0
- ksadk/server/routes/models.py +451 -0
- ksadk/server/routes/openai_compat.py +229 -0
- ksadk/server/routes/projection.py +848 -0
- ksadk/server/routes/routers.py +52 -0
- ksadk/server/routes/run.py +519 -0
- ksadk/server/routes/sessions.py +644 -0
- ksadk/server/routes/streaming.py +265 -0
- ksadk/server/routes/workspace.py +414 -0
- ksadk/server/static/assets/{ArtifactsPanel-D-SSn2ee.js → ArtifactsPanel-CakCqxEN.js} +1 -1
- ksadk/server/static/assets/CodeBlock-CynLknyO.js +9 -0
- ksadk/server/static/assets/MathMessageMarkdown-3sQdOysS.js +8 -0
- ksadk/server/static/assets/MermaidBlock-NERD_ef7.js +303 -0
- ksadk/server/static/assets/NativeTerminalPanel-CiHWf82q.js +1 -0
- ksadk/server/static/assets/{arc-BA13sHc9.js → arc-DrgjcKhl.js} +1 -1
- ksadk/server/static/assets/architecture-7EHR7CIX-CpZLRCcY.js +1 -0
- ksadk/server/static/assets/{architectureDiagram-3BPJPVTR-DzJzgVoL.js → architectureDiagram-3BPJPVTR-BXoVRbQ6.js} +1 -1
- ksadk/server/static/assets/{blockDiagram-GPEHLZMM-UTdVcZhn.js → blockDiagram-GPEHLZMM-BnaB3Ywv.js} +1 -1
- ksadk/server/static/assets/{c4Diagram-AAUBKEIU-Dix0GD4m.js → c4Diagram-AAUBKEIU-Dw4weZcp.js} +1 -1
- ksadk/server/static/assets/channel-Dt3rQTFI.js +1 -0
- ksadk/server/static/assets/{chunk-2J33WTMH-C2JxuZ42.js → chunk-2J33WTMH-ChjpLK7j.js} +1 -1
- ksadk/server/static/assets/{chunk-4BX2VUAB-Be57QPgD.js → chunk-4BX2VUAB-B9drTgOW.js} +1 -1
- ksadk/server/static/assets/{chunk-55IACEB6-WjSuy4nv.js → chunk-55IACEB6-CK4ROATy.js} +1 -1
- ksadk/server/static/assets/{chunk-727SXJPM-BOovP-b5.js → chunk-727SXJPM-B9KtTTgw.js} +1 -1
- ksadk/server/static/assets/{chunk-AQP2D5EJ-DOmZNmWj.js → chunk-AQP2D5EJ-1O1mki3m.js} +1 -1
- ksadk/server/static/assets/{chunk-FMBD7UC4-DzwlodkU.js → chunk-FMBD7UC4-B_fH2YuH.js} +1 -1
- ksadk/server/static/assets/{chunk-ND2GUHAM-DMqAikEU.js → chunk-ND2GUHAM-D6rFLaVV.js} +1 -1
- ksadk/server/static/assets/chunk-QZHKN3VN-iXEZtKQc.js +1 -0
- ksadk/server/static/assets/classDiagram-4FO5ZUOK-DIveV17Q.js +1 -0
- ksadk/server/static/assets/classDiagram-v2-Q7XG4LA2-DIveV17Q.js +1 -0
- ksadk/server/static/assets/{cose-bilkent-S5V4N54A-Zhy2GBnP.js → cose-bilkent-S5V4N54A-ChNs_Mdk.js} +1 -1
- ksadk/server/static/assets/{dagre-BM42HDAG-CbKrtIUz.js → dagre-BM42HDAG-D0QXg_MQ.js} +1 -1
- ksadk/server/static/assets/{diagram-2AECGRRQ-BRApgQRK.js → diagram-2AECGRRQ-DBDDKwBQ.js} +1 -1
- ksadk/server/static/assets/{diagram-5GNKFQAL-D0xbnDiK.js → diagram-5GNKFQAL-BkwDOxmO.js} +1 -1
- ksadk/server/static/assets/{diagram-KO2AKTUF-CVugVI9L.js → diagram-KO2AKTUF-Bv8waYvY.js} +1 -1
- ksadk/server/static/assets/{diagram-LMA3HP47-DozUGgee.js → diagram-LMA3HP47-e1VECDdu.js} +1 -1
- ksadk/server/static/assets/{diagram-OG6HWLK6-ZOYsKlru.js → diagram-OG6HWLK6-Ds9adfRl.js} +1 -1
- ksadk/server/static/assets/{dist-DpKtt4Bo.js → dist-4N2NMVYg.js} +1 -1
- ksadk/server/static/assets/{dist-CkEKJFtk.js → dist-B-jNR6Vn.js} +1 -1
- ksadk/server/static/assets/{dist-D57wnGWd.js → dist-B5J2uhNk.js} +1 -1
- ksadk/server/static/assets/{dist-8_SfK1IP.js → dist-B5oS1zcj.js} +1 -1
- ksadk/server/static/assets/{dist-DMezvBGJ.js → dist-BB3-OwTQ.js} +1 -1
- ksadk/server/static/assets/{dist-2hInLS_J.js → dist-BIlKoW0k.js} +1 -1
- ksadk/server/static/assets/{dist-BrCcQ47T.js → dist-BjOdlDjj.js} +1 -1
- ksadk/server/static/assets/{dist-BmlMPsCc.js → dist-C1sz5Eo3.js} +1 -1
- ksadk/server/static/assets/{dist-B0NP_Z0z.js → dist-CWV4lDr1.js} +1 -1
- ksadk/server/static/assets/{dist-D-xOD048.js → dist-CmZHzlwZ.js} +1 -1
- ksadk/server/static/assets/{dist-DL_NKc_z.js → dist-D6MpCZtz.js} +1 -1
- ksadk/server/static/assets/{dist-DyOkZmze.js → dist-DJN_6Y39.js} +1 -1
- ksadk/server/static/assets/{dist-DmFaq_Iy.js → dist-DQU9EWw-.js} +1 -1
- ksadk/server/static/assets/{dist-gX6GOLKq.js → dist-DWKByxWV.js} +1 -1
- ksadk/server/static/assets/{dist-DyWsb78Q.js → dist-Dli7qW-B.js} +1 -1
- ksadk/server/static/assets/{dist-BIJVDMuB.js → dist-DsHPdk3W.js} +1 -1
- ksadk/server/static/assets/{dist-Dp0k7bWI.js → dist-X4TnqwOK.js} +1 -1
- ksadk/server/static/assets/{dist-8EEyGgDx.js → dist-_pHPTyZ-.js} +1 -1
- ksadk/server/static/assets/{dist-CPBtqRTW.js → dist-fhOuWds5.js} +1 -1
- ksadk/server/static/assets/{dist-D-JMdWPQ.js → dist-gLnrGsoU.js} +1 -1
- ksadk/server/static/assets/{dist-BssVmjIo.js → dist-j6Dxmf_j.js} +1 -1
- ksadk/server/static/assets/{dist-C3HIIaN5.js → dist-o2OVmKvt.js} +1 -1
- ksadk/server/static/assets/{erDiagram-TEJ5UH35-Bk-9bgE5.js → erDiagram-TEJ5UH35-BPItFKH6.js} +1 -1
- ksadk/server/static/assets/eventmodeling-FCH6USID-DYFSxbqr.js +1 -0
- ksadk/server/static/assets/{flowDiagram-I6XJVG4X-Dn9kZVuZ.js → flowDiagram-I6XJVG4X-BbYB9w5Y.js} +1 -1
- ksadk/server/static/assets/{ganttDiagram-6RSMTGT7-BGxXlJqm.js → ganttDiagram-6RSMTGT7-TQXa0KXv.js} +1 -1
- ksadk/server/static/assets/gitGraph-WXDBUCRP-DyNYp6BN.js +1 -0
- ksadk/server/static/assets/{gitGraphDiagram-PVQCEYII-C7Q0X-wi.js → gitGraphDiagram-PVQCEYII-bKblCnpa.js} +1 -1
- ksadk/server/static/assets/index-R52pJJvg.js +177 -0
- ksadk/server/static/assets/index-jJr3_955.css +2 -0
- ksadk/server/static/assets/info-J43DQDTF-WUx7qTde.js +1 -0
- ksadk/server/static/assets/{infoDiagram-5YYISTIA-BO_CSqq5.js → infoDiagram-5YYISTIA-BGVT7J_i.js} +1 -1
- ksadk/server/static/assets/{ishikawaDiagram-YF4QCWOH-eYLPLd46.js → ishikawaDiagram-YF4QCWOH-K0OOfVna.js} +1 -1
- ksadk/server/static/assets/{journeyDiagram-JHISSGLW-De9geSb0.js → journeyDiagram-JHISSGLW-8LeOJaZp.js} +1 -1
- ksadk/server/static/assets/{kanban-definition-UN3LZRKU-Cs80kQto.js → kanban-definition-UN3LZRKU-C6uCgZrW.js} +1 -1
- ksadk/server/static/assets/{katex-DSrCFD8n.js → katex-CYWseY5E.js} +1 -1
- ksadk/server/static/assets/{linear-CN9sbKTF.js → linear-hdBcYzM8.js} +1 -1
- ksadk/server/static/assets/{mermaid-parser.core-DlgSBU8l.js → mermaid-parser.core-BWrDjSZM.js} +2 -2
- ksadk/server/static/assets/{mindmap-definition-RKZ34NQL-9afz7Oug.js → mindmap-definition-RKZ34NQL-Bjy_2v4_.js} +1 -1
- ksadk/server/static/assets/packet-YPE3B663-BN13y_xl.js +1 -0
- ksadk/server/static/assets/pie-LRSECV5Y-ftSLXhhc.js +1 -0
- ksadk/server/static/assets/{pieDiagram-4H26LBE5-DPv_VstN.js → pieDiagram-4H26LBE5-GmmqNiVH.js} +1 -1
- ksadk/server/static/assets/{quadrantDiagram-W4KKPZXB-o77wZ1XK.js → quadrantDiagram-W4KKPZXB-BmkRbwCg.js} +1 -1
- ksadk/server/static/assets/radar-GUYGQ44K-jaICkIeO.js +1 -0
- ksadk/server/static/assets/{requirementDiagram-4Y6WPE33-BYVw81VC.js → requirementDiagram-4Y6WPE33-DSWFqL7c.js} +1 -1
- ksadk/server/static/assets/{sankeyDiagram-5OEKKPKP-B7nZFMKT.js → sankeyDiagram-5OEKKPKP-l6iXmU65.js} +1 -1
- ksadk/server/static/assets/{sequenceDiagram-3UESZ5HK-BGRRrFw5.js → sequenceDiagram-3UESZ5HK-Cq6UBeQB.js} +1 -1
- ksadk/server/static/assets/{stateDiagram-AJRCARHV-BUfHCoPs.js → stateDiagram-AJRCARHV-qzTQpg3Y.js} +1 -1
- ksadk/server/static/assets/stateDiagram-v2-BHNVJYJU-BYCoYGAN.js +1 -0
- ksadk/server/static/assets/{timeline-definition-PNZ67QCA-FbCTGrf9.js → timeline-definition-PNZ67QCA-DEz24vV0.js} +1 -1
- ksadk/server/static/assets/treeView-BLDUP644-Cpib_vlz.js +1 -0
- ksadk/server/static/assets/treemap-LRROVOQU-DQxfmVHK.js +1 -0
- ksadk/server/static/assets/{vennDiagram-CIIHVFJN-DWgwtPsJ.js → vennDiagram-CIIHVFJN-CMCixki2.js} +1 -1
- ksadk/server/static/assets/wardley-L42UT6IY-BQYHnnkU.js +1 -0
- ksadk/server/static/assets/{wardleyDiagram-YWT4CUSO-2x8G-tTM.js → wardleyDiagram-YWT4CUSO-CVzB8F9K.js} +1 -1
- ksadk/server/static/assets/{xychartDiagram-2RQKCTM6-Drma3AYl.js → xychartDiagram-2RQKCTM6-BFBqBLc1.js} +1 -1
- ksadk/server/static/index.html +2 -2
- ksadk/server/terminal_sessions.py +111 -58
- ksadk/sessions/__init__.py +69 -19
- ksadk/sessions/base.py +31 -11
- ksadk/sessions/continuity.py +2 -3
- ksadk/sessions/in_memory.py +46 -2
- ksadk/sessions/local_service.py +148 -17
- ksadk/sessions/postgres_service.py +143 -25
- ksadk/sessions/resilient.py +36 -8
- ksadk/skills/models.py +17 -5
- ksadk/skills/package_store.py +5 -2
- ksadk/skills/runtime/agent.py +20 -5
- ksadk/skills/runtime/artifacts.py +1 -5
- ksadk/skills/runtime/backends/e2b.py +10 -4
- ksadk/skills/runtime/backends/local.py +15 -3
- ksadk/skills/runtime/base.py +1 -2
- ksadk/skills/runtime/executor.py +10 -3
- ksadk/skills/runtime/factory.py +4 -2
- ksadk/skills/runtime/loader.py +1 -2
- ksadk/skills/runtime/registry.py +15 -10
- ksadk/skills/runtime/request.py +3 -1
- ksadk/skills/service_client.py +47 -16
- ksadk/skills/tool_defs.py +15 -10
- ksadk/studio/__init__.py +5 -0
- ksadk/studio/agent_avatar_assets.py +120 -0
- ksadk/studio/agent_lifecycle.py +51 -0
- ksadk/studio/api.py +1046 -0
- ksadk/studio/api_catalog_routes.py +324 -0
- ksadk/studio/api_contracts.py +177 -0
- ksadk/studio/api_helpers.py +132 -0
- ksadk/studio/authoring.py +524 -0
- ksadk/studio/authoring_coordinator.py +285 -0
- ksadk/studio/builder.py +320 -0
- ksadk/studio/capabilities.py +351 -0
- ksadk/studio/cloud.py +278 -0
- ksadk/studio/codex_agent_service.py +583 -0
- ksadk/studio/codex_builder.py +324 -0
- ksadk/studio/codex_manifest.py +278 -0
- ksadk/studio/codex_run.py +352 -0
- ksadk/studio/compiler.py +189 -0
- ksadk/studio/contracts.py +644 -0
- ksadk/studio/errors.py +47 -0
- ksadk/studio/evaluation.py +192 -0
- ksadk/studio/event_store.py +284 -0
- ksadk/studio/framework_run.py +114 -0
- ksadk/studio/identifiers.py +33 -0
- ksadk/studio/mcp_runtime.py +229 -0
- ksadk/studio/model_client.py +543 -0
- ksadk/studio/model_profile_service.py +214 -0
- ksadk/studio/operations.py +243 -0
- ksadk/studio/otel_trace.py +754 -0
- ksadk/studio/pagination.py +182 -0
- ksadk/studio/python_tool_inspection.py +183 -0
- ksadk/studio/repository.py +306 -0
- ksadk/studio/resource_catalog.py +1217 -0
- ksadk/studio/run_service.py +707 -0
- ksadk/studio/runtime_catalog.py +74 -0
- ksadk/studio/runtime_source.py +339 -0
- ksadk/studio/service.py +1141 -0
- ksadk/studio/shared_web.py +994 -0
- ksadk/studio/skill_discovery.py +628 -0
- ksadk/studio/static/assets/PrismRenderer-IYN-ffww.js +3 -0
- ksadk/studio/static/assets/index-CaEN0eTR.css +1 -0
- ksadk/studio/static/assets/index-DI2eY_NH.js +252 -0
- ksadk/studio/static/assets/react-vendor-BNmTiJ-I.js +9 -0
- ksadk/studio/static/assets/rolldown-runtime-hePW80VL.js +1 -0
- ksadk/studio/static/index.html +15 -0
- ksadk/studio/templates.py +630 -0
- ksadk/studio/validator.py +246 -0
- ksadk/studio/workspace.py +137 -0
- ksadk/terminal_exec_policy.py +3 -4
- ksadk/tools/gateway.py +101 -15
- ksadk/tools/result_budget.py +12 -4
- ksadk/toolsets/__init__.py +161 -37
- ksadk/toolsets/a2a.py +426 -0
- ksadk/toolsets/platform.py +21 -5
- ksadk/toolsets/sandbox.py +82 -16
- ksadk/toolsets/skills.py +200 -39
- ksadk/toolsets/web.py +123 -26
- ksadk/toolsets/workspace.py +212 -101
- ksadk/tracing/__init__.py +19 -21
- ksadk/tracing/exporters/inmemory_exporter.py +62 -51
- ksadk/tracing/setup.py +135 -506
- ksadk/tracing/span_utils.py +0 -41
- ksadk/tui/loop.py +78 -30
- ksadk/tui/stream_render.py +1 -0
- ksadk/ui_config.py +5 -3
- ksadk/version.py +1 -1
- ksadk_runtime_common/memory_backend/registry.py +56 -56
- ksadk_runtime_common/schemas/runtime_event_v1.json +95 -0
- ksadk_runtime_common/workspace_files/router.py +8 -2
- agentengine_sdk_python-0.7.0.dist-info/RECORD +0 -482
- ksadk/a2a/card_builder.py +0 -64
- ksadk/a2a/client.py +0 -309
- ksadk/runners/utils/langfuse.py +0 -230
- ksadk/server/static/assets/CodeBlock-P16VOLYi.js +0 -9
- ksadk/server/static/assets/MathMessageMarkdown-CdGiQWHR.js +0 -8
- ksadk/server/static/assets/MermaidBlock-IOcal8cm.js +0 -303
- ksadk/server/static/assets/NativeTerminalPanel-DGSDWsei.js +0 -1
- ksadk/server/static/assets/architecture-7EHR7CIX-BvUlsA6l.js +0 -1
- ksadk/server/static/assets/channel-CTuydARX.js +0 -1
- ksadk/server/static/assets/chunk-QZHKN3VN-BnCydivS.js +0 -1
- ksadk/server/static/assets/classDiagram-4FO5ZUOK-BNHg45Be.js +0 -1
- ksadk/server/static/assets/classDiagram-v2-Q7XG4LA2-BNHg45Be.js +0 -1
- ksadk/server/static/assets/eventmodeling-FCH6USID-DsAgWnlf.js +0 -1
- ksadk/server/static/assets/gitGraph-WXDBUCRP-BIao9Yjg.js +0 -1
- ksadk/server/static/assets/index-C00Lwg3L.js +0 -165
- ksadk/server/static/assets/index-DAURbCsP.css +0 -1
- ksadk/server/static/assets/info-J43DQDTF-CyipqGxU.js +0 -1
- ksadk/server/static/assets/packet-YPE3B663-CGkyRxlg.js +0 -1
- ksadk/server/static/assets/pie-LRSECV5Y-M0uWAf4n.js +0 -1
- ksadk/server/static/assets/radar-GUYGQ44K-Bdbm24-x.js +0 -1
- ksadk/server/static/assets/stateDiagram-v2-BHNVJYJU-8xBN_gLM.js +0 -1
- ksadk/server/static/assets/treeView-BLDUP644-npMo0ak1.js +0 -1
- ksadk/server/static/assets/treemap-LRROVOQU-CHdzrrdo.js +0 -1
- ksadk/server/static/assets/wardley-L42UT6IY-BEQz7wyy.js +0 -1
- ksadk/tracing/exporters/langfuse_exporter.py +0 -445
- {agentengine_sdk_python-0.7.0.dist-info → agentengine_sdk_python-0.8.1.dist-info}/entry_points.txt +0 -0
- {agentengine_sdk_python-0.7.0.dist-info → agentengine_sdk_python-0.8.1.dist-info}/licenses/LICENSE +0 -0
- {agentengine_sdk_python-0.7.0.dist-info → agentengine_sdk_python-0.8.1.dist-info}/top_level.txt +0 -0
ksadk/agui/agent.py
ADDED
|
@@ -0,0 +1,872 @@
|
|
|
1
|
+
"""AG-UI Agent duck seam backed exclusively by a KSADK RuntimeAdapter."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import copy
|
|
7
|
+
import hashlib
|
|
8
|
+
import json
|
|
9
|
+
import logging
|
|
10
|
+
from dataclasses import dataclass, field
|
|
11
|
+
from importlib import import_module
|
|
12
|
+
from typing import Any, AsyncIterator, Callable, Mapping, Optional, cast
|
|
13
|
+
|
|
14
|
+
from ag_ui.core import (
|
|
15
|
+
ActivitySnapshotEvent,
|
|
16
|
+
BaseEvent,
|
|
17
|
+
Interrupt,
|
|
18
|
+
ReasoningEndEvent,
|
|
19
|
+
ReasoningMessageContentEvent,
|
|
20
|
+
ReasoningMessageEndEvent,
|
|
21
|
+
ReasoningMessageStartEvent,
|
|
22
|
+
ReasoningStartEvent,
|
|
23
|
+
RunAgentInput,
|
|
24
|
+
RunErrorEvent,
|
|
25
|
+
RunFinishedEvent,
|
|
26
|
+
RunFinishedInterruptOutcome,
|
|
27
|
+
RunFinishedSuccessOutcome,
|
|
28
|
+
RunStartedEvent,
|
|
29
|
+
StateSnapshotEvent,
|
|
30
|
+
TextMessageContentEvent,
|
|
31
|
+
TextMessageEndEvent,
|
|
32
|
+
TextMessageStartEvent,
|
|
33
|
+
ToolCallArgsEvent,
|
|
34
|
+
ToolCallEndEvent,
|
|
35
|
+
ToolCallResultEvent,
|
|
36
|
+
ToolCallStartEvent,
|
|
37
|
+
)
|
|
38
|
+
|
|
39
|
+
from ksadk.agui.a2ui_projection import project_a2ui_operations
|
|
40
|
+
from ksadk.conversations.runtime_metadata import (
|
|
41
|
+
_update_session_metadata_after_assistant_turn,
|
|
42
|
+
prime_session_metadata_for_user_turn,
|
|
43
|
+
)
|
|
44
|
+
from ksadk.events.runtime_event import EventType, RuntimeEvent
|
|
45
|
+
from ksadk.runtime.adapter import (
|
|
46
|
+
CancelResult,
|
|
47
|
+
ResumePayload,
|
|
48
|
+
ResumeTarget,
|
|
49
|
+
RunHandle,
|
|
50
|
+
RuntimeLaunchContext,
|
|
51
|
+
StartRequest,
|
|
52
|
+
)
|
|
53
|
+
from ksadk.runtime.executor import RuntimeExecutor
|
|
54
|
+
|
|
55
|
+
EventStoreFactory = Callable[[], Any]
|
|
56
|
+
SessionServiceFactory = Callable[[], Any]
|
|
57
|
+
logger = logging.getLogger(__name__)
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
def _split_a2ui_schema_context(context: list[Any]) -> tuple[Any, list[Any]]:
|
|
61
|
+
"""Call the optional toolkit without requiring its package to ship stubs."""
|
|
62
|
+
module = import_module("ag_ui_a2ui_toolkit")
|
|
63
|
+
splitter = getattr(module, "split_a2ui_schema_context", None)
|
|
64
|
+
if not callable(splitter):
|
|
65
|
+
raise RuntimeError("ag-ui-a2ui-toolkit does not expose split_a2ui_schema_context")
|
|
66
|
+
result = splitter(context)
|
|
67
|
+
if not isinstance(result, tuple) or len(result) != 2 or not isinstance(result[1], list):
|
|
68
|
+
raise RuntimeError("ag-ui-a2ui-toolkit returned an invalid schema context split")
|
|
69
|
+
return result[0], result[1]
|
|
70
|
+
|
|
71
|
+
|
|
72
|
+
@dataclass
|
|
73
|
+
class _PendingInterrupt:
|
|
74
|
+
interrupt: Interrupt
|
|
75
|
+
checkpoint_id: str
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
@dataclass
|
|
79
|
+
class _ThreadRun:
|
|
80
|
+
handle: RunHandle
|
|
81
|
+
active: bool = False
|
|
82
|
+
interrupted: bool = False
|
|
83
|
+
cancel_failed: bool = False
|
|
84
|
+
pending: dict[str, _PendingInterrupt] = field(default_factory=dict)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
@dataclass
|
|
88
|
+
class _SharedRuntime:
|
|
89
|
+
executor: RuntimeExecutor
|
|
90
|
+
launch_context: RuntimeLaunchContext
|
|
91
|
+
event_store_factory: Optional[EventStoreFactory]
|
|
92
|
+
session_service_factory: Optional[SessionServiceFactory]
|
|
93
|
+
threads: dict[str, _ThreadRun] = field(default_factory=dict)
|
|
94
|
+
consumed_resumes: dict[tuple[str, str], Any] = field(default_factory=dict)
|
|
95
|
+
lock: asyncio.Lock = field(default_factory=asyncio.Lock)
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
@dataclass
|
|
99
|
+
class _WireState:
|
|
100
|
+
thread_id: str
|
|
101
|
+
run_id: str
|
|
102
|
+
message_id: str
|
|
103
|
+
reasoning_id: str
|
|
104
|
+
text_open: bool = False
|
|
105
|
+
reasoning_open: bool = False
|
|
106
|
+
terminal: bool = False
|
|
107
|
+
text_content: str = ""
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
class KsadkAGUIAgent:
|
|
111
|
+
"""Implements the endpoint helper's pinned ``name/clone/run`` seam.
|
|
112
|
+
|
|
113
|
+
The official ``ag-ui-langgraph==0.0.42`` helper clones this object per HTTP
|
|
114
|
+
request and encodes the official event models yielded by :meth:`run`. The
|
|
115
|
+
shared state contains only the app-owned RuntimeAdapter and handle registry;
|
|
116
|
+
no native graph or runner-private attribute is exposed here.
|
|
117
|
+
"""
|
|
118
|
+
|
|
119
|
+
def __init__(
|
|
120
|
+
self,
|
|
121
|
+
*,
|
|
122
|
+
name: str,
|
|
123
|
+
executor: RuntimeExecutor | None = None,
|
|
124
|
+
launch_context: RuntimeLaunchContext | None = None,
|
|
125
|
+
event_store_factory: Optional[EventStoreFactory] = None,
|
|
126
|
+
session_service_factory: Optional[SessionServiceFactory] = None,
|
|
127
|
+
_shared: Optional[_SharedRuntime] = None,
|
|
128
|
+
) -> None:
|
|
129
|
+
self.name = name
|
|
130
|
+
if _shared is not None:
|
|
131
|
+
self._shared = _shared
|
|
132
|
+
else:
|
|
133
|
+
if executor is None or launch_context is None:
|
|
134
|
+
raise ValueError("AG-UI requires RuntimeExecutor and RuntimeLaunchContext")
|
|
135
|
+
self._shared = _SharedRuntime(
|
|
136
|
+
executor,
|
|
137
|
+
launch_context,
|
|
138
|
+
event_store_factory,
|
|
139
|
+
session_service_factory,
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
def clone(self) -> "KsadkAGUIAgent":
|
|
143
|
+
return type(self)(name=self.name, _shared=self._shared)
|
|
144
|
+
|
|
145
|
+
async def run(self, input: RunAgentInput) -> AsyncIterator[BaseEvent]:
|
|
146
|
+
wire = _WireState(
|
|
147
|
+
thread_id=input.thread_id,
|
|
148
|
+
run_id=input.run_id,
|
|
149
|
+
message_id=f"{input.run_id}:assistant",
|
|
150
|
+
reasoning_id=f"{input.run_id}:reasoning",
|
|
151
|
+
)
|
|
152
|
+
yield RunStartedEvent(
|
|
153
|
+
thread_id=input.thread_id,
|
|
154
|
+
run_id=input.run_id,
|
|
155
|
+
parent_run_id=input.parent_run_id,
|
|
156
|
+
input=input,
|
|
157
|
+
)
|
|
158
|
+
|
|
159
|
+
run: Optional[_ThreadRun] = None
|
|
160
|
+
try:
|
|
161
|
+
run, duplicate = await self._resolve_run(input)
|
|
162
|
+
if duplicate:
|
|
163
|
+
yield RunFinishedEvent(
|
|
164
|
+
thread_id=input.thread_id,
|
|
165
|
+
run_id=input.run_id,
|
|
166
|
+
outcome=RunFinishedSuccessOutcome(),
|
|
167
|
+
result={"status": "already_resumed"},
|
|
168
|
+
)
|
|
169
|
+
return
|
|
170
|
+
|
|
171
|
+
run.active = True
|
|
172
|
+
async for runtime_event in self._shared.executor.stream(run.handle):
|
|
173
|
+
persisted = await self._persist(self._event_for_persistence(runtime_event, run))
|
|
174
|
+
async for event in self._project(persisted, wire, run):
|
|
175
|
+
yield event
|
|
176
|
+
if not wire.terminal:
|
|
177
|
+
async for event in self._finish_success(wire):
|
|
178
|
+
yield event
|
|
179
|
+
if not run.interrupted:
|
|
180
|
+
await self._update_session_metadata_after_assistant_turn(input, wire)
|
|
181
|
+
await self._close_thread(input.thread_id, run)
|
|
182
|
+
except (asyncio.CancelledError, GeneratorExit):
|
|
183
|
+
if run is not None:
|
|
184
|
+
await self._cancel_and_close(input.thread_id, run)
|
|
185
|
+
raise
|
|
186
|
+
except Exception:
|
|
187
|
+
logger.exception(
|
|
188
|
+
"AG-UI runtime execution failed for thread=%s run=%s",
|
|
189
|
+
input.thread_id,
|
|
190
|
+
input.run_id,
|
|
191
|
+
)
|
|
192
|
+
if run is not None:
|
|
193
|
+
await self._close_thread(input.thread_id, run)
|
|
194
|
+
if not wire.terminal:
|
|
195
|
+
async for event in self._close_open_messages(wire):
|
|
196
|
+
yield event
|
|
197
|
+
yield RunErrorEvent(
|
|
198
|
+
message="Runtime execution failed",
|
|
199
|
+
code="RUNTIME_ERROR",
|
|
200
|
+
)
|
|
201
|
+
finally:
|
|
202
|
+
if run is not None and not run.cancel_failed:
|
|
203
|
+
run.active = False
|
|
204
|
+
|
|
205
|
+
async def _resolve_run(self, input: RunAgentInput) -> tuple[_ThreadRun, bool]:
|
|
206
|
+
async with self._shared.lock:
|
|
207
|
+
if input.resume:
|
|
208
|
+
return await self._resume_locked(input)
|
|
209
|
+
|
|
210
|
+
current = self._shared.threads.get(input.thread_id)
|
|
211
|
+
if current is not None and (current.active or current.interrupted):
|
|
212
|
+
raise ValueError(f"thread {input.thread_id!r} already has an active run")
|
|
213
|
+
|
|
214
|
+
forwarded = input.forwarded_props if isinstance(input.forwarded_props, dict) else {}
|
|
215
|
+
tools = [tool.model_dump(by_alias=True) for tool in input.tools]
|
|
216
|
+
a2ui_schema, regular_context = _split_a2ui_schema_context(input.context)
|
|
217
|
+
ag_ui_state: dict[str, Any] = {
|
|
218
|
+
"tools": tools,
|
|
219
|
+
"context": [item.model_dump(by_alias=True) for item in regular_context],
|
|
220
|
+
}
|
|
221
|
+
if a2ui_schema is not None:
|
|
222
|
+
ag_ui_state["a2ui_schema"] = a2ui_schema
|
|
223
|
+
inject_a2ui_tool = forwarded.get("injectA2UITool", forwarded.get("inject_a2ui_tool"))
|
|
224
|
+
if inject_a2ui_tool is not None:
|
|
225
|
+
ag_ui_state["inject_a2ui_tool"] = inject_a2ui_tool
|
|
226
|
+
request_config = dict(input.state) if isinstance(input.state, dict) else {}
|
|
227
|
+
request_config.update(
|
|
228
|
+
{
|
|
229
|
+
"ag-ui": ag_ui_state,
|
|
230
|
+
"copilotkit": {"actions": tools},
|
|
231
|
+
}
|
|
232
|
+
)
|
|
233
|
+
request = StartRequest(
|
|
234
|
+
input=self._latest_user_input(input),
|
|
235
|
+
user_id=str(forwarded.get("userId") or forwarded.get("user_id") or "agui-user"),
|
|
236
|
+
session_id=input.thread_id,
|
|
237
|
+
agent_id=self.name,
|
|
238
|
+
model=str(forwarded.get("model") or "") or None,
|
|
239
|
+
config=request_config,
|
|
240
|
+
metadata={"invocation_id": input.run_id, "transport": "ag-ui"},
|
|
241
|
+
)
|
|
242
|
+
handle = await self._shared.executor.start(
|
|
243
|
+
self._shared.launch_context,
|
|
244
|
+
request,
|
|
245
|
+
)
|
|
246
|
+
await self._persist_user_input(input, request, handle)
|
|
247
|
+
run = _ThreadRun(handle=handle)
|
|
248
|
+
self._shared.threads[input.thread_id] = run
|
|
249
|
+
return run, False
|
|
250
|
+
|
|
251
|
+
async def _resume_locked(self, input: RunAgentInput) -> tuple[_ThreadRun, bool]:
|
|
252
|
+
entries = input.resume or []
|
|
253
|
+
ids = [entry.interrupt_id for entry in entries]
|
|
254
|
+
if len(ids) != len(set(ids)):
|
|
255
|
+
raise ValueError("resume contains duplicate interruptId values")
|
|
256
|
+
|
|
257
|
+
current = self._shared.threads.get(input.thread_id)
|
|
258
|
+
if current is None or not current.interrupted:
|
|
259
|
+
consumed_fingerprints = {
|
|
260
|
+
entry.interrupt_id: self._resume_fingerprint(entry.status, entry.payload)
|
|
261
|
+
for entry in entries
|
|
262
|
+
}
|
|
263
|
+
if consumed_fingerprints and all(
|
|
264
|
+
self._shared.consumed_resumes.get((input.thread_id, interrupt_id)) == fingerprint
|
|
265
|
+
for interrupt_id, fingerprint in consumed_fingerprints.items()
|
|
266
|
+
):
|
|
267
|
+
return self._duplicate_run(input), True
|
|
268
|
+
current, duplicate = await self._restore_durable_run(input, consumed_fingerprints)
|
|
269
|
+
if duplicate:
|
|
270
|
+
return self._duplicate_run(input), True
|
|
271
|
+
if current is None:
|
|
272
|
+
raise ValueError(f"thread {input.thread_id!r} has no resumable run")
|
|
273
|
+
self._shared.threads[input.thread_id] = current
|
|
274
|
+
|
|
275
|
+
pending_ids = set(current.pending)
|
|
276
|
+
if set(ids) != pending_ids:
|
|
277
|
+
missing = sorted(pending_ids - set(ids))
|
|
278
|
+
unknown = sorted(set(ids) - pending_ids)
|
|
279
|
+
raise ValueError(
|
|
280
|
+
f"resume does not match pending interrupts; missing={missing}, unknown={unknown}"
|
|
281
|
+
)
|
|
282
|
+
|
|
283
|
+
checkpoint_ids = {pending.checkpoint_id for pending in current.pending.values()}
|
|
284
|
+
if len(checkpoint_ids) != 1 or not next(iter(checkpoint_ids), ""):
|
|
285
|
+
raise ValueError("pending interrupts do not share a resumable checkpoint")
|
|
286
|
+
|
|
287
|
+
decisions: dict[str, Any] = {}
|
|
288
|
+
fingerprints: dict[str, Any] = {}
|
|
289
|
+
for entry in entries:
|
|
290
|
+
payload = entry.payload if entry.status == "resolved" else {"type": "reject"}
|
|
291
|
+
decisions[entry.interrupt_id] = payload
|
|
292
|
+
fingerprints[entry.interrupt_id] = self._resume_fingerprint(entry.status, entry.payload)
|
|
293
|
+
|
|
294
|
+
single_id = ids[0] if len(ids) == 1 else None
|
|
295
|
+
payload_data = decisions[single_id] if single_id is not None else decisions
|
|
296
|
+
reservation_created = await self._persist_resolved_approvals(input, current, entries)
|
|
297
|
+
if not reservation_created:
|
|
298
|
+
return self._duplicate_run(input), True
|
|
299
|
+
for interrupt_id, fingerprint in fingerprints.items():
|
|
300
|
+
self._shared.consumed_resumes[(input.thread_id, interrupt_id)] = fingerprint
|
|
301
|
+
|
|
302
|
+
resumed = await self._shared.executor.resume(
|
|
303
|
+
current.handle,
|
|
304
|
+
ResumeTarget(kind="checkpoint_id", id=next(iter(checkpoint_ids))),
|
|
305
|
+
ResumePayload(
|
|
306
|
+
kind="approval_decision",
|
|
307
|
+
call_id=single_id,
|
|
308
|
+
data=payload_data,
|
|
309
|
+
),
|
|
310
|
+
)
|
|
311
|
+
current.handle = resumed
|
|
312
|
+
current.interrupted = False
|
|
313
|
+
current.pending.clear()
|
|
314
|
+
return current, False
|
|
315
|
+
|
|
316
|
+
async def _persist_resolved_approvals(
|
|
317
|
+
self,
|
|
318
|
+
input: RunAgentInput,
|
|
319
|
+
run: _ThreadRun,
|
|
320
|
+
entries: list[Any],
|
|
321
|
+
) -> bool:
|
|
322
|
+
if self._shared.event_store_factory is None:
|
|
323
|
+
return True
|
|
324
|
+
reservation_created = True
|
|
325
|
+
for index, entry in enumerate(entries):
|
|
326
|
+
fingerprint = self._resume_fingerprint(entry.status, entry.payload)
|
|
327
|
+
event_key = json.dumps(
|
|
328
|
+
[input.thread_id, entry.interrupt_id],
|
|
329
|
+
sort_keys=True,
|
|
330
|
+
ensure_ascii=False,
|
|
331
|
+
default=str,
|
|
332
|
+
)
|
|
333
|
+
decision = self._approval_decision_for_audit(entry.status, entry.payload)
|
|
334
|
+
event = RuntimeEvent.create(
|
|
335
|
+
EventType.APPROVAL_RESOLVED,
|
|
336
|
+
event_id=f"evt_agui_resume_{hashlib.sha256(event_key.encode()).hexdigest()}",
|
|
337
|
+
agent_id=str(run.handle.native_ref.get("agent_id") or self.name),
|
|
338
|
+
user_id=str(run.handle.native_ref.get("user_id") or "agui-user"),
|
|
339
|
+
session_id=input.thread_id,
|
|
340
|
+
invocation_id=run.handle.run_id,
|
|
341
|
+
seq_id=0,
|
|
342
|
+
payload={
|
|
343
|
+
"approval_id": entry.interrupt_id,
|
|
344
|
+
"call_id": entry.interrupt_id,
|
|
345
|
+
"decision": decision,
|
|
346
|
+
"resume_fingerprint": fingerprint,
|
|
347
|
+
"protocol": "ag-ui",
|
|
348
|
+
},
|
|
349
|
+
)
|
|
350
|
+
if index == 0:
|
|
351
|
+
_persisted, reservation_created = await self._reserve(event)
|
|
352
|
+
if not reservation_created:
|
|
353
|
+
return False
|
|
354
|
+
else:
|
|
355
|
+
await self._persist(event)
|
|
356
|
+
return reservation_created
|
|
357
|
+
|
|
358
|
+
@staticmethod
|
|
359
|
+
def _approval_decision_for_audit(status: str, payload: Any) -> str:
|
|
360
|
+
"""Persist a stable decision value while accepting official AG-UI envelopes."""
|
|
361
|
+
|
|
362
|
+
if status != "resolved":
|
|
363
|
+
return "rejected"
|
|
364
|
+
if payload is True:
|
|
365
|
+
return "approved"
|
|
366
|
+
if isinstance(payload, Mapping):
|
|
367
|
+
for key in ("approve", "approved"):
|
|
368
|
+
if key in payload:
|
|
369
|
+
return "approved" if bool(payload[key]) else "rejected"
|
|
370
|
+
for key in ("decision", "type"):
|
|
371
|
+
if key in payload:
|
|
372
|
+
return KsadkAGUIAgent._approval_decision_for_audit(status, payload[key])
|
|
373
|
+
return "rejected"
|
|
374
|
+
if isinstance(payload, str) and payload.strip().lower() in {"approve", "approved"}:
|
|
375
|
+
return "approved"
|
|
376
|
+
return "rejected"
|
|
377
|
+
|
|
378
|
+
async def _project(
|
|
379
|
+
self,
|
|
380
|
+
event: RuntimeEvent,
|
|
381
|
+
wire: _WireState,
|
|
382
|
+
run: _ThreadRun,
|
|
383
|
+
) -> AsyncIterator[BaseEvent]:
|
|
384
|
+
payload = event.payload
|
|
385
|
+
event_type = event.event_type
|
|
386
|
+
|
|
387
|
+
if event_type in (EventType.TEXT_DELTA, EventType.TEXT_COMPLETED):
|
|
388
|
+
if not wire.text_open:
|
|
389
|
+
wire.text_open = True
|
|
390
|
+
yield TextMessageStartEvent(message_id=wire.message_id)
|
|
391
|
+
text = str(payload.get("text") or "")
|
|
392
|
+
if event_type == EventType.TEXT_COMPLETED and wire.text_content:
|
|
393
|
+
text = text[len(wire.text_content) :] if text.startswith(wire.text_content) else ""
|
|
394
|
+
if text:
|
|
395
|
+
yield TextMessageContentEvent(message_id=wire.message_id, delta=text)
|
|
396
|
+
wire.text_content += text
|
|
397
|
+
if event_type == EventType.TEXT_COMPLETED:
|
|
398
|
+
wire.text_open = False
|
|
399
|
+
yield TextMessageEndEvent(message_id=wire.message_id)
|
|
400
|
+
return
|
|
401
|
+
|
|
402
|
+
if event_type in (EventType.REASONING_DELTA, EventType.REASONING_COMPLETED):
|
|
403
|
+
if not wire.reasoning_open:
|
|
404
|
+
wire.reasoning_open = True
|
|
405
|
+
yield ReasoningStartEvent(message_id=wire.reasoning_id)
|
|
406
|
+
yield ReasoningMessageStartEvent(message_id=wire.reasoning_id, role="reasoning")
|
|
407
|
+
text = str(payload.get("text") or "")
|
|
408
|
+
if text:
|
|
409
|
+
yield ReasoningMessageContentEvent(message_id=wire.reasoning_id, delta=text)
|
|
410
|
+
if event_type == EventType.REASONING_COMPLETED:
|
|
411
|
+
wire.reasoning_open = False
|
|
412
|
+
yield ReasoningMessageEndEvent(message_id=wire.reasoning_id)
|
|
413
|
+
yield ReasoningEndEvent(message_id=wire.reasoning_id)
|
|
414
|
+
return
|
|
415
|
+
|
|
416
|
+
if event_type == EventType.TOOL_CALL_BEGIN:
|
|
417
|
+
call_id = str(payload.get("call_id") or "tool")
|
|
418
|
+
yield ToolCallStartEvent(
|
|
419
|
+
tool_call_id=call_id,
|
|
420
|
+
tool_call_name=str(payload.get("name") or "tool"),
|
|
421
|
+
parent_message_id=wire.message_id,
|
|
422
|
+
)
|
|
423
|
+
if "args" in payload:
|
|
424
|
+
yield ToolCallArgsEvent(
|
|
425
|
+
tool_call_id=call_id,
|
|
426
|
+
delta=json.dumps(payload.get("args"), ensure_ascii=False, default=str),
|
|
427
|
+
)
|
|
428
|
+
return
|
|
429
|
+
|
|
430
|
+
if event_type == EventType.TOOL_CALL_END:
|
|
431
|
+
call_id = str(payload.get("call_id") or "tool")
|
|
432
|
+
content = (
|
|
433
|
+
payload.get("result") if payload.get("error") is None else payload.get("error")
|
|
434
|
+
)
|
|
435
|
+
# AG-UI closes the active call at TOOL_CALL_END. Sending a result
|
|
436
|
+
# first makes official clients discard the call, then reject this
|
|
437
|
+
# end event as orphaned.
|
|
438
|
+
yield ToolCallEndEvent(tool_call_id=call_id)
|
|
439
|
+
yield ToolCallResultEvent(
|
|
440
|
+
message_id=f"{wire.run_id}:tool:{call_id}",
|
|
441
|
+
tool_call_id=call_id,
|
|
442
|
+
content=json.dumps(content, ensure_ascii=False, default=str),
|
|
443
|
+
role="tool",
|
|
444
|
+
)
|
|
445
|
+
return
|
|
446
|
+
|
|
447
|
+
if event_type == EventType.CHECKPOINT_CREATED:
|
|
448
|
+
yield StateSnapshotEvent(snapshot={"checkpoint": copy.deepcopy(payload)})
|
|
449
|
+
return
|
|
450
|
+
|
|
451
|
+
if event_type == EventType.APPROVAL_REQUESTED:
|
|
452
|
+
interrupt_id = str(payload.get("approval_id") or payload.get("call_id") or "")
|
|
453
|
+
checkpoint_id = str(run.handle.native_ref.get("checkpoint_id") or "")
|
|
454
|
+
if not checkpoint_id:
|
|
455
|
+
known = run.handle.native_ref.get("known_checkpoint_ids") or []
|
|
456
|
+
checkpoint_id = str(known[-1]) if known else ""
|
|
457
|
+
raw_detail = payload.get("detail")
|
|
458
|
+
detail: dict[str, Any] = raw_detail if isinstance(raw_detail, dict) else {}
|
|
459
|
+
interrupt = Interrupt(
|
|
460
|
+
id=interrupt_id,
|
|
461
|
+
reason=str(detail.get("reason") or payload.get("kind") or "approval"),
|
|
462
|
+
message=self._approval_message(detail, payload),
|
|
463
|
+
tool_call_id=str(payload.get("call_id") or "") or None,
|
|
464
|
+
response_schema=detail.get("response_schema"),
|
|
465
|
+
metadata=self._approval_metadata(detail, payload),
|
|
466
|
+
)
|
|
467
|
+
run.pending[interrupt_id] = _PendingInterrupt(interrupt, checkpoint_id)
|
|
468
|
+
return
|
|
469
|
+
|
|
470
|
+
if event_type in {
|
|
471
|
+
EventType.A2UI_SURFACE_BEGIN,
|
|
472
|
+
EventType.A2UI_SURFACE_UPDATE,
|
|
473
|
+
EventType.A2UI_SURFACE_END,
|
|
474
|
+
}:
|
|
475
|
+
operations = project_a2ui_operations(event_type, payload)
|
|
476
|
+
if operations:
|
|
477
|
+
surface_id = str(payload.get("surface_id") or payload.get("surfaceId") or "")
|
|
478
|
+
yield ActivitySnapshotEvent(
|
|
479
|
+
message_id=f"{wire.run_id}:a2ui:{surface_id}",
|
|
480
|
+
activity_type="a2ui-surface",
|
|
481
|
+
# The client renderer addresses a surface by this value.
|
|
482
|
+
# ``message_id`` is a transport id, not the A2UI surface id.
|
|
483
|
+
content={
|
|
484
|
+
"surfaceId": surface_id,
|
|
485
|
+
"a2ui_operations": operations,
|
|
486
|
+
},
|
|
487
|
+
replace=True,
|
|
488
|
+
)
|
|
489
|
+
return
|
|
490
|
+
|
|
491
|
+
if event_type == EventType.RUN_INTERRUPTED:
|
|
492
|
+
async for close_event in self._close_open_messages(wire):
|
|
493
|
+
yield close_event
|
|
494
|
+
if not run.pending:
|
|
495
|
+
raise ValueError("runtime interrupted without a pending approval")
|
|
496
|
+
checkpoint_id = str(run.handle.native_ref.get("checkpoint_id") or "")
|
|
497
|
+
if not checkpoint_id:
|
|
498
|
+
known = run.handle.native_ref.get("known_checkpoint_ids") or []
|
|
499
|
+
checkpoint_id = str(known[-1]) if known else ""
|
|
500
|
+
for pending in run.pending.values():
|
|
501
|
+
if not pending.checkpoint_id:
|
|
502
|
+
pending.checkpoint_id = checkpoint_id
|
|
503
|
+
run.interrupted = True
|
|
504
|
+
wire.terminal = True
|
|
505
|
+
yield RunFinishedEvent(
|
|
506
|
+
thread_id=wire.thread_id,
|
|
507
|
+
run_id=wire.run_id,
|
|
508
|
+
outcome=RunFinishedInterruptOutcome(
|
|
509
|
+
interrupts=[pending.interrupt for pending in run.pending.values()]
|
|
510
|
+
),
|
|
511
|
+
)
|
|
512
|
+
return
|
|
513
|
+
|
|
514
|
+
if event_type == EventType.RUN_COMPLETED:
|
|
515
|
+
async for finish_event in self._finish_success(wire):
|
|
516
|
+
yield finish_event
|
|
517
|
+
return
|
|
518
|
+
|
|
519
|
+
if event_type in (EventType.RUN_FAILED, EventType.RUN_CANCELED):
|
|
520
|
+
async for close_event in self._close_open_messages(wire):
|
|
521
|
+
yield close_event
|
|
522
|
+
wire.terminal = True
|
|
523
|
+
yield RunErrorEvent(
|
|
524
|
+
message=(
|
|
525
|
+
"Runtime run was cancelled"
|
|
526
|
+
if event_type == EventType.RUN_CANCELED
|
|
527
|
+
else "Runtime execution failed"
|
|
528
|
+
),
|
|
529
|
+
code=("CANCELLED" if event_type == EventType.RUN_CANCELED else "RUNTIME_ERROR"),
|
|
530
|
+
)
|
|
531
|
+
|
|
532
|
+
async def _finish_success(self, wire: _WireState) -> AsyncIterator[BaseEvent]:
|
|
533
|
+
async for event in self._close_open_messages(wire):
|
|
534
|
+
yield event
|
|
535
|
+
if not wire.terminal:
|
|
536
|
+
wire.terminal = True
|
|
537
|
+
yield RunFinishedEvent(
|
|
538
|
+
thread_id=wire.thread_id,
|
|
539
|
+
run_id=wire.run_id,
|
|
540
|
+
outcome=RunFinishedSuccessOutcome(),
|
|
541
|
+
)
|
|
542
|
+
|
|
543
|
+
@staticmethod
|
|
544
|
+
async def _close_open_messages(wire: _WireState) -> AsyncIterator[BaseEvent]:
|
|
545
|
+
if wire.reasoning_open:
|
|
546
|
+
wire.reasoning_open = False
|
|
547
|
+
yield ReasoningMessageEndEvent(message_id=wire.reasoning_id)
|
|
548
|
+
yield ReasoningEndEvent(message_id=wire.reasoning_id)
|
|
549
|
+
if wire.text_open:
|
|
550
|
+
wire.text_open = False
|
|
551
|
+
yield TextMessageEndEvent(message_id=wire.message_id)
|
|
552
|
+
|
|
553
|
+
async def _persist(self, event: RuntimeEvent) -> RuntimeEvent:
|
|
554
|
+
factory = self._shared.event_store_factory
|
|
555
|
+
if factory is None:
|
|
556
|
+
return event
|
|
557
|
+
store = factory()
|
|
558
|
+
return cast(RuntimeEvent, await store.append_one(event))
|
|
559
|
+
|
|
560
|
+
async def _reserve(self, event: RuntimeEvent) -> tuple[RuntimeEvent, bool]:
|
|
561
|
+
factory = self._shared.event_store_factory
|
|
562
|
+
if factory is None:
|
|
563
|
+
return event, True
|
|
564
|
+
store = factory()
|
|
565
|
+
reserve = getattr(store, "reserve_once", None)
|
|
566
|
+
if callable(reserve):
|
|
567
|
+
persisted, created = await reserve(event)
|
|
568
|
+
return cast(RuntimeEvent, persisted), bool(created)
|
|
569
|
+
return cast(RuntimeEvent, await store.append_one(event)), True
|
|
570
|
+
|
|
571
|
+
async def _persist_user_input(
|
|
572
|
+
self,
|
|
573
|
+
input: RunAgentInput,
|
|
574
|
+
request: StartRequest,
|
|
575
|
+
handle: RunHandle,
|
|
576
|
+
) -> None:
|
|
577
|
+
event_key = json.dumps([input.thread_id, input.run_id, "user"], ensure_ascii=False)
|
|
578
|
+
await self._persist(
|
|
579
|
+
RuntimeEvent.create(
|
|
580
|
+
EventType.RUN_STARTED,
|
|
581
|
+
event_id=f"evt_agui_input_{hashlib.sha256(event_key.encode()).hexdigest()}",
|
|
582
|
+
agent_id=self.name,
|
|
583
|
+
user_id=request.user_id,
|
|
584
|
+
session_id=input.thread_id,
|
|
585
|
+
invocation_id=input.run_id,
|
|
586
|
+
seq_id=0,
|
|
587
|
+
payload={
|
|
588
|
+
"status": "in_progress",
|
|
589
|
+
"input": self._json_safe(request.input),
|
|
590
|
+
"source": "ag-ui",
|
|
591
|
+
"runtime_type": handle.runtime_type,
|
|
592
|
+
},
|
|
593
|
+
)
|
|
594
|
+
)
|
|
595
|
+
await self._prime_session_metadata_for_user_turn(
|
|
596
|
+
session_id=input.thread_id,
|
|
597
|
+
user_input=self._input_text(request.input),
|
|
598
|
+
)
|
|
599
|
+
|
|
600
|
+
async def _prime_session_metadata_for_user_turn(
|
|
601
|
+
self,
|
|
602
|
+
*,
|
|
603
|
+
session_id: str,
|
|
604
|
+
user_input: str,
|
|
605
|
+
) -> None:
|
|
606
|
+
factory = self._shared.session_service_factory
|
|
607
|
+
if factory is None or not user_input:
|
|
608
|
+
return
|
|
609
|
+
try:
|
|
610
|
+
service = factory()
|
|
611
|
+
session = await service.get_session(session_id)
|
|
612
|
+
if session is not None:
|
|
613
|
+
await prime_session_metadata_for_user_turn(
|
|
614
|
+
service=service,
|
|
615
|
+
session=session,
|
|
616
|
+
user_input=user_input,
|
|
617
|
+
)
|
|
618
|
+
except Exception:
|
|
619
|
+
# Metadata enrichment must not make an otherwise valid agent run fail.
|
|
620
|
+
logger.debug("failed to prime AG-UI session metadata", exc_info=True)
|
|
621
|
+
|
|
622
|
+
async def _update_session_metadata_after_assistant_turn(
|
|
623
|
+
self,
|
|
624
|
+
input: RunAgentInput,
|
|
625
|
+
wire: _WireState,
|
|
626
|
+
) -> None:
|
|
627
|
+
factory = self._shared.session_service_factory
|
|
628
|
+
if factory is None or not wire.text_content:
|
|
629
|
+
return
|
|
630
|
+
try:
|
|
631
|
+
await _update_session_metadata_after_assistant_turn(
|
|
632
|
+
service=factory(),
|
|
633
|
+
session_id=input.thread_id,
|
|
634
|
+
assistant_text=wire.text_content,
|
|
635
|
+
model=None,
|
|
636
|
+
)
|
|
637
|
+
except Exception:
|
|
638
|
+
logger.debug("failed to update AG-UI session metadata", exc_info=True)
|
|
639
|
+
|
|
640
|
+
def _event_for_persistence(self, event: RuntimeEvent, run: _ThreadRun) -> RuntimeEvent:
|
|
641
|
+
if event.event_type in {
|
|
642
|
+
EventType.APPROVAL_REQUESTED,
|
|
643
|
+
EventType.APPROVAL_RESOLVED,
|
|
644
|
+
}:
|
|
645
|
+
return event.model_copy(update={"payload": {**event.payload, "protocol": "ag-ui"}})
|
|
646
|
+
if event.event_type != EventType.RUN_INTERRUPTED:
|
|
647
|
+
return event
|
|
648
|
+
return event.model_copy(
|
|
649
|
+
update={
|
|
650
|
+
"payload": {
|
|
651
|
+
**event.payload,
|
|
652
|
+
"runtime_handle": self._durable_handle(run.handle),
|
|
653
|
+
}
|
|
654
|
+
}
|
|
655
|
+
)
|
|
656
|
+
|
|
657
|
+
async def _restore_durable_run(
|
|
658
|
+
self,
|
|
659
|
+
input: RunAgentInput,
|
|
660
|
+
fingerprints: Mapping[str, str],
|
|
661
|
+
) -> tuple[_ThreadRun | None, bool]:
|
|
662
|
+
events = await self._durable_events(input.thread_id)
|
|
663
|
+
if not events:
|
|
664
|
+
return None, False
|
|
665
|
+
resolved = {
|
|
666
|
+
str(event.payload.get("approval_id") or event.payload.get("call_id") or ""): event
|
|
667
|
+
for event in events
|
|
668
|
+
if event.event_type == EventType.APPROVAL_RESOLVED
|
|
669
|
+
}
|
|
670
|
+
if fingerprints and all(interrupt_id in resolved for interrupt_id in fingerprints):
|
|
671
|
+
for interrupt_id, fingerprint in fingerprints.items():
|
|
672
|
+
persisted = str(resolved[interrupt_id].payload.get("resume_fingerprint") or "")
|
|
673
|
+
if persisted and persisted != fingerprint:
|
|
674
|
+
raise ValueError(f"interrupt {interrupt_id!r} was resolved differently")
|
|
675
|
+
return None, True
|
|
676
|
+
if any(interrupt_id in resolved for interrupt_id in fingerprints):
|
|
677
|
+
raise ValueError("resume set is only partially resolved")
|
|
678
|
+
|
|
679
|
+
interrupted = next(
|
|
680
|
+
(event for event in reversed(events) if event.event_type == EventType.RUN_INTERRUPTED),
|
|
681
|
+
None,
|
|
682
|
+
)
|
|
683
|
+
if interrupted is None:
|
|
684
|
+
return None, False
|
|
685
|
+
raw_handle = interrupted.payload.get("runtime_handle")
|
|
686
|
+
if not isinstance(raw_handle, dict):
|
|
687
|
+
return None, False
|
|
688
|
+
handle = RunHandle.model_validate(raw_handle)
|
|
689
|
+
requests = [
|
|
690
|
+
event
|
|
691
|
+
for event in events
|
|
692
|
+
if event.invocation_id == interrupted.invocation_id
|
|
693
|
+
and event.event_type == EventType.APPROVAL_REQUESTED
|
|
694
|
+
and str(event.payload.get("approval_id") or event.payload.get("call_id") or "")
|
|
695
|
+
not in resolved
|
|
696
|
+
]
|
|
697
|
+
pending: dict[str, _PendingInterrupt] = {}
|
|
698
|
+
checkpoint_id = str(handle.native_ref.get("checkpoint_id") or "")
|
|
699
|
+
if not checkpoint_id:
|
|
700
|
+
known = handle.native_ref.get("known_checkpoint_ids") or []
|
|
701
|
+
checkpoint_id = str(known[-1]) if known else ""
|
|
702
|
+
for request in requests:
|
|
703
|
+
interrupt = self._interrupt_from_payload(request.payload)
|
|
704
|
+
pending[interrupt.id] = _PendingInterrupt(interrupt, checkpoint_id)
|
|
705
|
+
if not pending:
|
|
706
|
+
return None, False
|
|
707
|
+
if not self._shared.executor.is_attached(handle):
|
|
708
|
+
handle = await self._shared.executor.attach(
|
|
709
|
+
self._shared.launch_context,
|
|
710
|
+
handle,
|
|
711
|
+
)
|
|
712
|
+
return _ThreadRun(handle=handle, interrupted=True, pending=pending), False
|
|
713
|
+
|
|
714
|
+
async def _durable_events(self, session_id: str) -> list[RuntimeEvent]:
|
|
715
|
+
factory = self._shared.event_store_factory
|
|
716
|
+
if factory is None:
|
|
717
|
+
return []
|
|
718
|
+
store = factory()
|
|
719
|
+
list_events = getattr(store, "list", None)
|
|
720
|
+
if not callable(list_events):
|
|
721
|
+
return []
|
|
722
|
+
return list(await list_events(session_id))
|
|
723
|
+
|
|
724
|
+
@staticmethod
|
|
725
|
+
def _durable_handle(handle: RunHandle) -> dict[str, Any]:
|
|
726
|
+
allowed = {
|
|
727
|
+
"agent_id",
|
|
728
|
+
"user_id",
|
|
729
|
+
"checkpoint_id",
|
|
730
|
+
"known_checkpoint_ids",
|
|
731
|
+
"pending_approval_ids",
|
|
732
|
+
"framework_ref",
|
|
733
|
+
"thread_id",
|
|
734
|
+
"checkpoint_ns",
|
|
735
|
+
"resume_thread_id",
|
|
736
|
+
}
|
|
737
|
+
native_ref = {key: value for key, value in handle.native_ref.items() if key in allowed}
|
|
738
|
+
return {
|
|
739
|
+
"run_id": handle.run_id,
|
|
740
|
+
"session_id": handle.session_id,
|
|
741
|
+
"runtime_type": handle.runtime_type,
|
|
742
|
+
"native_ref": KsadkAGUIAgent._json_safe(native_ref),
|
|
743
|
+
}
|
|
744
|
+
|
|
745
|
+
@staticmethod
|
|
746
|
+
def _interrupt_from_payload(payload: Mapping[str, Any]) -> Interrupt:
|
|
747
|
+
interrupt_id = str(payload.get("approval_id") or payload.get("call_id") or "")
|
|
748
|
+
raw_detail = payload.get("detail")
|
|
749
|
+
detail = raw_detail if isinstance(raw_detail, dict) else {}
|
|
750
|
+
return Interrupt(
|
|
751
|
+
id=interrupt_id,
|
|
752
|
+
reason=str(detail.get("reason") or payload.get("kind") or "approval"),
|
|
753
|
+
message=KsadkAGUIAgent._approval_message(detail, payload),
|
|
754
|
+
tool_call_id=str(payload.get("call_id") or "") or None,
|
|
755
|
+
response_schema=detail.get("response_schema"),
|
|
756
|
+
metadata=KsadkAGUIAgent._approval_metadata(detail, payload),
|
|
757
|
+
)
|
|
758
|
+
|
|
759
|
+
@staticmethod
|
|
760
|
+
def _duplicate_run(input: RunAgentInput) -> _ThreadRun:
|
|
761
|
+
return _ThreadRun(
|
|
762
|
+
handle=RunHandle(
|
|
763
|
+
run_id=input.run_id,
|
|
764
|
+
session_id=input.thread_id,
|
|
765
|
+
runtime_type="ag-ui-duplicate",
|
|
766
|
+
)
|
|
767
|
+
)
|
|
768
|
+
|
|
769
|
+
@staticmethod
|
|
770
|
+
def _json_safe(value: Any) -> Any:
|
|
771
|
+
return json.loads(json.dumps(value, ensure_ascii=False, default=str))
|
|
772
|
+
|
|
773
|
+
async def _close_thread(self, thread_id: str, run: _ThreadRun) -> None:
|
|
774
|
+
try:
|
|
775
|
+
await self._shared.executor.close(run.handle)
|
|
776
|
+
finally:
|
|
777
|
+
async with self._shared.lock:
|
|
778
|
+
if self._shared.threads.get(thread_id) is run:
|
|
779
|
+
self._shared.threads.pop(thread_id, None)
|
|
780
|
+
|
|
781
|
+
async def _cancel_and_close(self, thread_id: str, run: _ThreadRun) -> CancelResult:
|
|
782
|
+
result = await self._shared.executor.cancel(run.handle)
|
|
783
|
+
if result in {
|
|
784
|
+
CancelResult.INTERRUPTED_ACTIVE_TURN,
|
|
785
|
+
CancelResult.PENDING_CANCEL_RECORDED,
|
|
786
|
+
CancelResult.NOT_RUNNING,
|
|
787
|
+
}:
|
|
788
|
+
await self._close_thread(thread_id, run)
|
|
789
|
+
elif result == CancelResult.FAILED:
|
|
790
|
+
# Keep the handle blocked in the registry: cancellation was not
|
|
791
|
+
# acknowledged, so accepting a replacement run would be dishonest.
|
|
792
|
+
run.cancel_failed = True
|
|
793
|
+
run.active = True
|
|
794
|
+
return result
|
|
795
|
+
|
|
796
|
+
@staticmethod
|
|
797
|
+
def _latest_user_input(input: RunAgentInput) -> Any:
|
|
798
|
+
for message in reversed(input.messages):
|
|
799
|
+
if getattr(message, "role", None) == "user":
|
|
800
|
+
return getattr(message, "content", "")
|
|
801
|
+
return ""
|
|
802
|
+
|
|
803
|
+
@staticmethod
|
|
804
|
+
def _input_text(value: Any) -> str:
|
|
805
|
+
if isinstance(value, str):
|
|
806
|
+
return value.strip()
|
|
807
|
+
if isinstance(value, Mapping):
|
|
808
|
+
return str(value.get("text") or value.get("content") or "").strip()
|
|
809
|
+
return str(value or "").strip()
|
|
810
|
+
|
|
811
|
+
@staticmethod
|
|
812
|
+
def _approval_action(detail: Mapping[str, Any]) -> Mapping[str, Any]:
|
|
813
|
+
nested_request = detail.get("approval_requests")
|
|
814
|
+
actions = (
|
|
815
|
+
nested_request.get("action_requests")
|
|
816
|
+
if isinstance(nested_request, Mapping)
|
|
817
|
+
else detail.get("action_requests")
|
|
818
|
+
)
|
|
819
|
+
if isinstance(actions, list):
|
|
820
|
+
for action in actions:
|
|
821
|
+
if isinstance(action, Mapping):
|
|
822
|
+
return action
|
|
823
|
+
return {}
|
|
824
|
+
|
|
825
|
+
@staticmethod
|
|
826
|
+
def _approval_metadata(
|
|
827
|
+
detail: Mapping[str, Any],
|
|
828
|
+
payload: Mapping[str, Any],
|
|
829
|
+
) -> dict[str, Any]:
|
|
830
|
+
action = KsadkAGUIAgent._approval_action(detail)
|
|
831
|
+
arguments = (
|
|
832
|
+
action.get("args")
|
|
833
|
+
or action.get("arguments")
|
|
834
|
+
or detail.get("arguments")
|
|
835
|
+
or detail.get("args")
|
|
836
|
+
or payload.get("args")
|
|
837
|
+
)
|
|
838
|
+
metadata: dict[str, Any] = {
|
|
839
|
+
"tool_name": str(
|
|
840
|
+
action.get("name")
|
|
841
|
+
or detail.get("tool_name")
|
|
842
|
+
or payload.get("name")
|
|
843
|
+
or payload.get("kind")
|
|
844
|
+
or "approval"
|
|
845
|
+
),
|
|
846
|
+
"arguments": arguments if arguments is not None else {},
|
|
847
|
+
}
|
|
848
|
+
approval_level = (
|
|
849
|
+
action.get("approval_level")
|
|
850
|
+
or detail.get("approval_level")
|
|
851
|
+
or payload.get("approval_level")
|
|
852
|
+
)
|
|
853
|
+
if approval_level:
|
|
854
|
+
metadata["approval_level"] = str(approval_level)
|
|
855
|
+
return metadata
|
|
856
|
+
|
|
857
|
+
@staticmethod
|
|
858
|
+
def _approval_message(detail: Mapping[str, Any], payload: Mapping[str, Any]) -> str:
|
|
859
|
+
action = KsadkAGUIAgent._approval_action(detail)
|
|
860
|
+
return str(
|
|
861
|
+
action.get("description")
|
|
862
|
+
or detail.get("message")
|
|
863
|
+
or payload.get("message")
|
|
864
|
+
or "Approval required"
|
|
865
|
+
)
|
|
866
|
+
|
|
867
|
+
@staticmethod
|
|
868
|
+
def _resume_fingerprint(status: str, payload: Any) -> Any:
|
|
869
|
+
return json.dumps([status, payload], sort_keys=True, ensure_ascii=False, default=str)
|
|
870
|
+
|
|
871
|
+
|
|
872
|
+
__all__ = ["KsadkAGUIAgent"]
|