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
|
@@ -0,0 +1,994 @@
|
|
|
1
|
+
"""Project AgentKit Studio runs onto Responses and compatibility API contracts."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import asyncio
|
|
6
|
+
import json
|
|
7
|
+
from collections.abc import AsyncIterator
|
|
8
|
+
from datetime import datetime, timezone
|
|
9
|
+
from typing import Any
|
|
10
|
+
from uuid import uuid4
|
|
11
|
+
|
|
12
|
+
from ksadk.studio.contracts import BuildStatus, OperationStatus, RunRecord, RunStatus
|
|
13
|
+
from ksadk.studio.errors import StudioError, not_found
|
|
14
|
+
from ksadk.studio.service import StudioService
|
|
15
|
+
from ksadk.tools.gateway import tool_approval_capability
|
|
16
|
+
|
|
17
|
+
_TERMINAL_OPERATIONS = {
|
|
18
|
+
OperationStatus.SUCCEEDED,
|
|
19
|
+
OperationStatus.FAILED,
|
|
20
|
+
OperationStatus.CANCELLED,
|
|
21
|
+
OperationStatus.INTERRUPTED,
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
class StudioSharedWebBridge:
|
|
26
|
+
"""Project Studio repositories onto Responses and legacy action surfaces."""
|
|
27
|
+
|
|
28
|
+
def __init__(self, studio: StudioService) -> None:
|
|
29
|
+
self.studio = studio
|
|
30
|
+
self._operations_by_invocation: dict[str, str] = {}
|
|
31
|
+
self._response_runs: dict[str, str] = {}
|
|
32
|
+
self._run_ids_by_invocation: dict[str, str] = {}
|
|
33
|
+
|
|
34
|
+
def resolve_agent_id(self, requested: str | None = None) -> str:
|
|
35
|
+
if requested:
|
|
36
|
+
self.studio.agent_detail(requested)
|
|
37
|
+
return requested
|
|
38
|
+
agents = self.studio.list_agents(limit=1)
|
|
39
|
+
if not agents:
|
|
40
|
+
raise not_found("agent", "")
|
|
41
|
+
return agents[0].metadata.id
|
|
42
|
+
|
|
43
|
+
def bootstrap(self, agent_id: str) -> dict[str, Any]:
|
|
44
|
+
draft = self._draft(agent_id)
|
|
45
|
+
model = self._model_descriptor(agent_id)
|
|
46
|
+
return {
|
|
47
|
+
"Agent": {
|
|
48
|
+
"AgentId": agent_id,
|
|
49
|
+
"Name": draft.metadata.name,
|
|
50
|
+
"Framework": self.studio.agent_runtime_type(agent_id),
|
|
51
|
+
},
|
|
52
|
+
"AccessMode": "Owner",
|
|
53
|
+
"ApiFormats": ["responses"],
|
|
54
|
+
"Model": model,
|
|
55
|
+
"Capabilities": {
|
|
56
|
+
"HostedChat": {
|
|
57
|
+
"Enabled": True,
|
|
58
|
+
"ApiFormats": ["responses"],
|
|
59
|
+
"PreferredTransport": "responses",
|
|
60
|
+
"Transports": [
|
|
61
|
+
{
|
|
62
|
+
"Protocol": "responses",
|
|
63
|
+
"Runtime": "agentkit-studio",
|
|
64
|
+
"Endpoint": "/agentengine/api/v1/RunAgent",
|
|
65
|
+
"Version": "v1",
|
|
66
|
+
"Capabilities": {
|
|
67
|
+
"A2UI": True,
|
|
68
|
+
"Interrupt": True,
|
|
69
|
+
"Cancel": True,
|
|
70
|
+
},
|
|
71
|
+
}
|
|
72
|
+
],
|
|
73
|
+
},
|
|
74
|
+
"RunLifecycle": {
|
|
75
|
+
"Enabled": True,
|
|
76
|
+
"Resume": True,
|
|
77
|
+
"Abort": True,
|
|
78
|
+
"Checkpoints": False,
|
|
79
|
+
"CheckpointResume": False,
|
|
80
|
+
"CheckpointResumePreview": False,
|
|
81
|
+
},
|
|
82
|
+
"ApprovalPolicy": tool_approval_capability(),
|
|
83
|
+
"WorkspaceFiles": {"Enabled": False},
|
|
84
|
+
"NativeDashboard": {"Enabled": False},
|
|
85
|
+
"NativeTerminal": {"Enabled": False},
|
|
86
|
+
"Thinking": False,
|
|
87
|
+
},
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
def list_models(self, agent_id: str) -> dict[str, Any]:
|
|
91
|
+
models = self._model_descriptors(agent_id)
|
|
92
|
+
model = self._model_descriptor(agent_id, models)
|
|
93
|
+
return {
|
|
94
|
+
"Models": models,
|
|
95
|
+
"Current": model["id"],
|
|
96
|
+
"Source": "agentkit-studio",
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
def select_model(self, agent_id: str, requested: str | None = None) -> str:
|
|
100
|
+
"""Resolve and authorize the actual model used by the next turn."""
|
|
101
|
+
|
|
102
|
+
return self._select_model(agent_id, str(requested or ""))
|
|
103
|
+
|
|
104
|
+
def list_sessions(
|
|
105
|
+
self,
|
|
106
|
+
agent_id: str,
|
|
107
|
+
*,
|
|
108
|
+
page: int = 1,
|
|
109
|
+
page_size: int = 30,
|
|
110
|
+
) -> dict[str, Any]:
|
|
111
|
+
sessions = self._sessions(agent_id)
|
|
112
|
+
safe_page = max(1, page)
|
|
113
|
+
safe_size = min(100, max(1, page_size))
|
|
114
|
+
start = (safe_page - 1) * safe_size
|
|
115
|
+
return {
|
|
116
|
+
"Sessions": sessions[start : start + safe_size],
|
|
117
|
+
"Total": len(sessions),
|
|
118
|
+
"Page": safe_page,
|
|
119
|
+
"PageSize": safe_size,
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
def create_session(self, agent_id: str) -> dict[str, Any]:
|
|
123
|
+
self._draft(agent_id)
|
|
124
|
+
session_id = f"ses_{uuid4().hex}"
|
|
125
|
+
now = datetime.now(timezone.utc).isoformat()
|
|
126
|
+
return {
|
|
127
|
+
"Session": {
|
|
128
|
+
"SessionId": session_id,
|
|
129
|
+
"AgentId": agent_id,
|
|
130
|
+
"UserId": "local-user",
|
|
131
|
+
"Title": "新会话",
|
|
132
|
+
"CreatedAt": now,
|
|
133
|
+
"UpdatedAt": now,
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
def get_session(self, session_id: str) -> dict[str, Any]:
|
|
138
|
+
runs = self.studio.event_store.list_runs(session_id=session_id)
|
|
139
|
+
if not runs:
|
|
140
|
+
raise not_found("session", session_id)
|
|
141
|
+
return {"Session": self._session_record(runs)}
|
|
142
|
+
|
|
143
|
+
def delete_session(self, session_id: str) -> dict[str, Any]:
|
|
144
|
+
self.studio.event_store.delete_session(session_id)
|
|
145
|
+
return {}
|
|
146
|
+
|
|
147
|
+
def list_messages(
|
|
148
|
+
self,
|
|
149
|
+
session_id: str,
|
|
150
|
+
*,
|
|
151
|
+
after_seq_id: int | None = None,
|
|
152
|
+
before_seq_id: int | None = None,
|
|
153
|
+
limit: int = 50,
|
|
154
|
+
) -> dict[str, Any]:
|
|
155
|
+
runs = self.studio.event_store.list_runs(session_id=session_id)
|
|
156
|
+
messages: list[dict[str, Any]] = []
|
|
157
|
+
sequence = 0
|
|
158
|
+
for run in runs:
|
|
159
|
+
sequence += 1
|
|
160
|
+
messages.append(
|
|
161
|
+
{
|
|
162
|
+
"MessageId": f"{run.id}:user",
|
|
163
|
+
"Role": "user",
|
|
164
|
+
"Content": {"text": run.input},
|
|
165
|
+
"Timestamp": self._timestamp(run.started_at),
|
|
166
|
+
"SeqId": sequence,
|
|
167
|
+
"InvocationId": run.id,
|
|
168
|
+
}
|
|
169
|
+
)
|
|
170
|
+
sequence += 1
|
|
171
|
+
messages.append(
|
|
172
|
+
{
|
|
173
|
+
"MessageId": f"{run.id}:assistant",
|
|
174
|
+
"Role": "assistant",
|
|
175
|
+
"Content": {"text": self._run_output(run)},
|
|
176
|
+
"Timestamp": self._timestamp(run.completed_at or run.started_at),
|
|
177
|
+
"SeqId": sequence,
|
|
178
|
+
"InvocationId": run.id,
|
|
179
|
+
"Activities": self._run_activities(run),
|
|
180
|
+
}
|
|
181
|
+
)
|
|
182
|
+
|
|
183
|
+
latest_seq_id = sequence
|
|
184
|
+
if after_seq_id is not None:
|
|
185
|
+
messages = [item for item in messages if item["SeqId"] > after_seq_id]
|
|
186
|
+
if before_seq_id is not None:
|
|
187
|
+
messages = [item for item in messages if item["SeqId"] < before_seq_id]
|
|
188
|
+
safe_limit = min(200, max(1, limit))
|
|
189
|
+
has_more = len(messages) > safe_limit
|
|
190
|
+
selected = messages[-safe_limit:]
|
|
191
|
+
return {
|
|
192
|
+
"Messages": selected,
|
|
193
|
+
"LatestSeqId": latest_seq_id,
|
|
194
|
+
"HasMore": has_more,
|
|
195
|
+
"NextCursor": selected[0]["SeqId"] if has_more and selected else None,
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
def list_session_events(self, session_id: str) -> dict[str, Any]:
|
|
199
|
+
runs = self.studio.event_store.list_runs(session_id=session_id)
|
|
200
|
+
events: list[dict[str, Any]] = []
|
|
201
|
+
sequence = 0
|
|
202
|
+
for run in runs:
|
|
203
|
+
for event in self.studio.event_store.events(run.id):
|
|
204
|
+
sequence += 1
|
|
205
|
+
events.append(
|
|
206
|
+
{
|
|
207
|
+
"SeqId": sequence,
|
|
208
|
+
"EventType": event.type,
|
|
209
|
+
"InvocationId": run.id,
|
|
210
|
+
"Content": event.data,
|
|
211
|
+
"Timestamp": self._timestamp(event.created_at),
|
|
212
|
+
}
|
|
213
|
+
)
|
|
214
|
+
return {
|
|
215
|
+
"Events": events,
|
|
216
|
+
"Total": len(events),
|
|
217
|
+
"Offset": 0,
|
|
218
|
+
"Limit": len(events),
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
def cancel_run(self, invocation_id: str) -> dict[str, Any]:
|
|
222
|
+
operation_id = self._operations_by_invocation.get(invocation_id)
|
|
223
|
+
if operation_id:
|
|
224
|
+
self.studio.operations.cancel(operation_id)
|
|
225
|
+
return {"InvocationId": invocation_id, "Cancelled": bool(operation_id)}
|
|
226
|
+
|
|
227
|
+
async def pause_run(self, invocation_id: str) -> dict[str, Any]:
|
|
228
|
+
run_id = self._run_ids_by_invocation.get(invocation_id)
|
|
229
|
+
if not run_id:
|
|
230
|
+
raise StudioError("RUN_NOT_READY", "运行尚未创建,请稍后重试", status_code=409)
|
|
231
|
+
return await self.studio.run_service.pause_run(run_id)
|
|
232
|
+
|
|
233
|
+
async def resume_run(self, invocation_id: str) -> dict[str, Any]:
|
|
234
|
+
run_id = self._run_ids_by_invocation.get(invocation_id)
|
|
235
|
+
if not run_id:
|
|
236
|
+
raise StudioError("RUN_NOT_FOUND", "未找到可继续的运行", status_code=404)
|
|
237
|
+
return await self.studio.run_service.resume_run(run_id)
|
|
238
|
+
|
|
239
|
+
def response_session_id(self, response_id: str) -> str:
|
|
240
|
+
run_id = self._response_runs.get(response_id, response_id)
|
|
241
|
+
return self.studio.event_store.get(run_id).session_id
|
|
242
|
+
|
|
243
|
+
async def stream_run(self, payload: dict[str, Any]) -> AsyncIterator[str]:
|
|
244
|
+
agent_id = self.resolve_agent_id(str(payload.get("AgentId") or "") or None)
|
|
245
|
+
session_id = str(payload.get("SessionId") or f"ses_{uuid4().hex}")
|
|
246
|
+
invocation_id = str(payload.get("InvocationId") or f"resp_{uuid4().hex}")
|
|
247
|
+
prompt = self._input_text(payload)
|
|
248
|
+
runtime_input = self._runtime_input(payload)
|
|
249
|
+
model = self._select_model(agent_id, str(payload.get("Model") or ""))
|
|
250
|
+
approval_mode = str(payload.get("ApprovalMode") or "")
|
|
251
|
+
collaboration_mode = str(payload.get("CollaborationMode") or "")
|
|
252
|
+
goal_objective = str(payload.get("GoalObjective") or "")
|
|
253
|
+
execution = asyncio.create_task(
|
|
254
|
+
self._execute_run(
|
|
255
|
+
agent_id=agent_id,
|
|
256
|
+
session_id=session_id,
|
|
257
|
+
invocation_id=invocation_id,
|
|
258
|
+
prompt=prompt,
|
|
259
|
+
runtime_input=runtime_input,
|
|
260
|
+
model=model,
|
|
261
|
+
approval_mode=approval_mode,
|
|
262
|
+
collaboration_mode=collaboration_mode,
|
|
263
|
+
goal_objective=goal_objective,
|
|
264
|
+
)
|
|
265
|
+
)
|
|
266
|
+
|
|
267
|
+
def release_operation(task: asyncio.Task[RunRecord]) -> None:
|
|
268
|
+
self._operations_by_invocation.pop(invocation_id, None)
|
|
269
|
+
if not task.cancelled():
|
|
270
|
+
task.exception()
|
|
271
|
+
|
|
272
|
+
execution.add_done_callback(release_operation)
|
|
273
|
+
|
|
274
|
+
yield self._sse(
|
|
275
|
+
"response.created",
|
|
276
|
+
{
|
|
277
|
+
"type": "response.created",
|
|
278
|
+
"response": self._response_shell(invocation_id, model=model),
|
|
279
|
+
},
|
|
280
|
+
)
|
|
281
|
+
try:
|
|
282
|
+
yield self._sse(
|
|
283
|
+
"response.in_progress",
|
|
284
|
+
{
|
|
285
|
+
"type": "response.in_progress",
|
|
286
|
+
"response": self._response_shell(invocation_id, model=model),
|
|
287
|
+
},
|
|
288
|
+
)
|
|
289
|
+
seen_events: set[tuple[str, int]] = set()
|
|
290
|
+
emitted_text = ""
|
|
291
|
+
idle_polls = 0
|
|
292
|
+
while not execution.done():
|
|
293
|
+
projected = self._project_response_events(
|
|
294
|
+
session_id=session_id,
|
|
295
|
+
agent_id=agent_id,
|
|
296
|
+
invocation_id=invocation_id,
|
|
297
|
+
seen=seen_events,
|
|
298
|
+
)
|
|
299
|
+
if projected:
|
|
300
|
+
idle_polls = 0
|
|
301
|
+
for event_name, event_payload in projected:
|
|
302
|
+
if event_name == "response.output_text.delta":
|
|
303
|
+
emitted_text += str(event_payload.get("delta") or "")
|
|
304
|
+
yield self._sse(event_name, event_payload)
|
|
305
|
+
else:
|
|
306
|
+
idle_polls += 1
|
|
307
|
+
if idle_polls >= 20:
|
|
308
|
+
idle_polls = 0
|
|
309
|
+
yield ": keep-alive\n\n"
|
|
310
|
+
await asyncio.sleep(0.05)
|
|
311
|
+
run = await execution
|
|
312
|
+
for event_name, event_payload in self._project_response_events(
|
|
313
|
+
session_id=session_id,
|
|
314
|
+
agent_id=agent_id,
|
|
315
|
+
invocation_id=invocation_id,
|
|
316
|
+
seen=seen_events,
|
|
317
|
+
):
|
|
318
|
+
if event_name == "response.output_text.delta":
|
|
319
|
+
emitted_text += str(event_payload.get("delta") or "")
|
|
320
|
+
yield self._sse(event_name, event_payload)
|
|
321
|
+
remaining = (
|
|
322
|
+
run.output[len(emitted_text) :] if run.output.startswith(emitted_text) else ""
|
|
323
|
+
)
|
|
324
|
+
if remaining:
|
|
325
|
+
yield self._response_delta_sse(
|
|
326
|
+
"response.output_text.delta",
|
|
327
|
+
invocation_id=invocation_id,
|
|
328
|
+
delta=remaining,
|
|
329
|
+
)
|
|
330
|
+
yield self._sse(
|
|
331
|
+
"response.completed",
|
|
332
|
+
{
|
|
333
|
+
"type": "response.completed",
|
|
334
|
+
"response": self._response_payload(
|
|
335
|
+
run,
|
|
336
|
+
model=model,
|
|
337
|
+
response_id=invocation_id,
|
|
338
|
+
),
|
|
339
|
+
},
|
|
340
|
+
)
|
|
341
|
+
except StudioError as exc:
|
|
342
|
+
yield self._failed_sse(invocation_id, exc.message)
|
|
343
|
+
except Exception:
|
|
344
|
+
yield self._failed_sse(invocation_id, "本地 Agent 运行失败")
|
|
345
|
+
finally:
|
|
346
|
+
if execution.done():
|
|
347
|
+
self._operations_by_invocation.pop(invocation_id, None)
|
|
348
|
+
|
|
349
|
+
async def invoke_response(self, payload: dict[str, Any]) -> dict[str, Any]:
|
|
350
|
+
"""Run once and return an OpenAI Responses-compatible JSON object."""
|
|
351
|
+
|
|
352
|
+
agent_id = self.resolve_agent_id(str(payload.get("AgentId") or "") or None)
|
|
353
|
+
session_id = str(payload.get("SessionId") or f"ses_{uuid4().hex}")
|
|
354
|
+
invocation_id = str(payload.get("InvocationId") or f"resp_{uuid4().hex}")
|
|
355
|
+
model = self._select_model(agent_id, str(payload.get("Model") or ""))
|
|
356
|
+
approval_mode = str(payload.get("ApprovalMode") or "")
|
|
357
|
+
collaboration_mode = str(payload.get("CollaborationMode") or "")
|
|
358
|
+
goal_objective = str(payload.get("GoalObjective") or "")
|
|
359
|
+
try:
|
|
360
|
+
run = await self._execute_run(
|
|
361
|
+
agent_id=agent_id,
|
|
362
|
+
session_id=session_id,
|
|
363
|
+
invocation_id=invocation_id,
|
|
364
|
+
prompt=self._input_text(payload),
|
|
365
|
+
runtime_input=self._runtime_input(payload),
|
|
366
|
+
model=model,
|
|
367
|
+
approval_mode=approval_mode,
|
|
368
|
+
collaboration_mode=collaboration_mode,
|
|
369
|
+
goal_objective=goal_objective,
|
|
370
|
+
)
|
|
371
|
+
return self._response_payload(
|
|
372
|
+
run,
|
|
373
|
+
model=model,
|
|
374
|
+
response_id=invocation_id,
|
|
375
|
+
)
|
|
376
|
+
finally:
|
|
377
|
+
self._operations_by_invocation.pop(invocation_id, None)
|
|
378
|
+
|
|
379
|
+
async def _execute_run(
|
|
380
|
+
self,
|
|
381
|
+
*,
|
|
382
|
+
agent_id: str,
|
|
383
|
+
session_id: str,
|
|
384
|
+
invocation_id: str,
|
|
385
|
+
prompt: str,
|
|
386
|
+
runtime_input: Any,
|
|
387
|
+
model: str,
|
|
388
|
+
approval_mode: str = "",
|
|
389
|
+
collaboration_mode: str = "",
|
|
390
|
+
goal_objective: str = "",
|
|
391
|
+
) -> RunRecord:
|
|
392
|
+
build = await self._ensure_build(agent_id)
|
|
393
|
+
|
|
394
|
+
def observe(event: Any) -> None:
|
|
395
|
+
if event.type == "run.created":
|
|
396
|
+
run_id = str(event.data.get("runId") or "")
|
|
397
|
+
if run_id:
|
|
398
|
+
self._run_ids_by_invocation[invocation_id] = run_id
|
|
399
|
+
|
|
400
|
+
operation = self.studio.submit_studio_run(
|
|
401
|
+
build.id,
|
|
402
|
+
prompt,
|
|
403
|
+
session_id=session_id,
|
|
404
|
+
model=model,
|
|
405
|
+
approval_mode=approval_mode or None,
|
|
406
|
+
collaboration_mode=collaboration_mode or None,
|
|
407
|
+
goal_objective=goal_objective or None,
|
|
408
|
+
runtime_input=runtime_input or None,
|
|
409
|
+
idempotency_key=f"responses:{invocation_id}",
|
|
410
|
+
on_event=observe,
|
|
411
|
+
)
|
|
412
|
+
self._operations_by_invocation[invocation_id] = operation.id
|
|
413
|
+
while True:
|
|
414
|
+
current = self.studio.operations.get(operation.id)
|
|
415
|
+
if current.status in _TERMINAL_OPERATIONS:
|
|
416
|
+
break
|
|
417
|
+
await asyncio.sleep(0.05)
|
|
418
|
+
if current.status != OperationStatus.SUCCEEDED:
|
|
419
|
+
message = str((current.error or {}).get("message") or "Agent 运行失败")
|
|
420
|
+
raise StudioError("RUN_FAILED", message, status_code=500)
|
|
421
|
+
run = self.studio.event_store.get(current.resource_id)
|
|
422
|
+
if run.status != RunStatus.COMPLETED:
|
|
423
|
+
raise StudioError("RUN_FAILED", self._run_output(run), status_code=500)
|
|
424
|
+
self._response_runs[invocation_id] = run.id
|
|
425
|
+
return run
|
|
426
|
+
|
|
427
|
+
@staticmethod
|
|
428
|
+
def _response_shell(response_id: str, *, model: str) -> dict[str, Any]:
|
|
429
|
+
return {
|
|
430
|
+
"id": response_id,
|
|
431
|
+
"object": "response",
|
|
432
|
+
"status": "in_progress",
|
|
433
|
+
"model": model,
|
|
434
|
+
"output": [],
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
def _project_response_events(
|
|
438
|
+
self,
|
|
439
|
+
*,
|
|
440
|
+
session_id: str,
|
|
441
|
+
agent_id: str,
|
|
442
|
+
invocation_id: str,
|
|
443
|
+
seen: set[tuple[str, int]],
|
|
444
|
+
) -> list[tuple[str, dict[str, Any]]]:
|
|
445
|
+
projected: list[tuple[str, dict[str, Any]]] = []
|
|
446
|
+
current_run_id = self._run_ids_by_invocation.get(invocation_id)
|
|
447
|
+
if not current_run_id:
|
|
448
|
+
return projected
|
|
449
|
+
for run in self.studio.event_store.list_runs(session_id=session_id):
|
|
450
|
+
if run.agent_id != agent_id or run.id != current_run_id:
|
|
451
|
+
continue
|
|
452
|
+
events = self.studio.event_store.events(run.id)
|
|
453
|
+
starts = {
|
|
454
|
+
str(event.data.get("callId") or event.data.get("call_id") or ""): event.data
|
|
455
|
+
for event in events
|
|
456
|
+
if event.type in {"command.started", "tool.started", "tool.requested"}
|
|
457
|
+
}
|
|
458
|
+
for event in events:
|
|
459
|
+
key = (run.id, event.id)
|
|
460
|
+
if key in seen:
|
|
461
|
+
continue
|
|
462
|
+
seen.add(key)
|
|
463
|
+
if event.type in {"message.delta", "thinking.delta"}:
|
|
464
|
+
delta = str(event.data.get("text") or event.data.get("delta") or "")
|
|
465
|
+
if not delta:
|
|
466
|
+
continue
|
|
467
|
+
event_name = (
|
|
468
|
+
"response.output_text.delta"
|
|
469
|
+
if event.type == "message.delta"
|
|
470
|
+
else "response.reasoning_summary_text.delta"
|
|
471
|
+
)
|
|
472
|
+
projected.append(
|
|
473
|
+
(
|
|
474
|
+
event_name,
|
|
475
|
+
self._response_delta_payload(
|
|
476
|
+
event_name,
|
|
477
|
+
invocation_id=invocation_id,
|
|
478
|
+
delta=delta,
|
|
479
|
+
),
|
|
480
|
+
)
|
|
481
|
+
)
|
|
482
|
+
continue
|
|
483
|
+
if event.type.startswith("a2ui."):
|
|
484
|
+
projected.append(
|
|
485
|
+
(
|
|
486
|
+
event.type,
|
|
487
|
+
{
|
|
488
|
+
"type": event.type,
|
|
489
|
+
"runId": run.id,
|
|
490
|
+
**event.data,
|
|
491
|
+
},
|
|
492
|
+
)
|
|
493
|
+
)
|
|
494
|
+
continue
|
|
495
|
+
if event.type == "run.paused":
|
|
496
|
+
projected.append(
|
|
497
|
+
(
|
|
498
|
+
"response.paused",
|
|
499
|
+
{
|
|
500
|
+
"type": "response.paused",
|
|
501
|
+
"response_id": invocation_id,
|
|
502
|
+
"runId": run.id,
|
|
503
|
+
},
|
|
504
|
+
)
|
|
505
|
+
)
|
|
506
|
+
continue
|
|
507
|
+
if event.type == "run.resumed":
|
|
508
|
+
projected.append(
|
|
509
|
+
(
|
|
510
|
+
"response.resumed",
|
|
511
|
+
{
|
|
512
|
+
"type": "response.resumed",
|
|
513
|
+
"response_id": invocation_id,
|
|
514
|
+
"runId": run.id,
|
|
515
|
+
},
|
|
516
|
+
)
|
|
517
|
+
)
|
|
518
|
+
continue
|
|
519
|
+
item_event = self._response_item_event(event.type, event.data, starts)
|
|
520
|
+
if item_event is not None:
|
|
521
|
+
projected.append(item_event)
|
|
522
|
+
return projected
|
|
523
|
+
|
|
524
|
+
@staticmethod
|
|
525
|
+
def _response_item_event(
|
|
526
|
+
event_type: str,
|
|
527
|
+
data: dict[str, Any],
|
|
528
|
+
starts: dict[str, dict[str, Any]],
|
|
529
|
+
) -> tuple[str, dict[str, Any]] | None:
|
|
530
|
+
call_id = str(data.get("callId") or data.get("call_id") or "")
|
|
531
|
+
started = starts.get(call_id, {})
|
|
532
|
+
done = event_type in {
|
|
533
|
+
"command.completed",
|
|
534
|
+
"tool.completed",
|
|
535
|
+
"command.failed",
|
|
536
|
+
"tool.failed",
|
|
537
|
+
}
|
|
538
|
+
if event_type.startswith("command."):
|
|
539
|
+
command = str(data.get("command") or started.get("command") or "执行命令")
|
|
540
|
+
item = {
|
|
541
|
+
"id": call_id or f"shell_{uuid4().hex}",
|
|
542
|
+
"call_id": call_id,
|
|
543
|
+
"type": "shell_call",
|
|
544
|
+
"status": "failed"
|
|
545
|
+
if event_type.endswith("failed") or data.get("exitCode") not in {None, 0}
|
|
546
|
+
else "completed"
|
|
547
|
+
if done
|
|
548
|
+
else "in_progress",
|
|
549
|
+
"action": {
|
|
550
|
+
"commands": [command],
|
|
551
|
+
"cwd": data.get("cwd") or started.get("cwd") or "",
|
|
552
|
+
},
|
|
553
|
+
"exit_code": data.get("exitCode"),
|
|
554
|
+
"output": data.get("output") or "",
|
|
555
|
+
}
|
|
556
|
+
elif event_type.startswith("tool."):
|
|
557
|
+
name = str(data.get("tool") or data.get("name") or started.get("tool") or "调用工具")
|
|
558
|
+
args = data.get("args", started.get("args"))
|
|
559
|
+
item = {
|
|
560
|
+
"id": call_id or f"tool_{uuid4().hex}",
|
|
561
|
+
"call_id": call_id,
|
|
562
|
+
"type": "function_call",
|
|
563
|
+
"name": name,
|
|
564
|
+
"arguments": json.dumps(args, ensure_ascii=False) if args is not None else "",
|
|
565
|
+
"status": "failed"
|
|
566
|
+
if event_type.endswith("failed")
|
|
567
|
+
else "completed"
|
|
568
|
+
if done
|
|
569
|
+
else "in_progress",
|
|
570
|
+
"output": data.get("output") or data.get("result") or "",
|
|
571
|
+
}
|
|
572
|
+
elif event_type == "approval.requested":
|
|
573
|
+
item = {
|
|
574
|
+
"id": call_id or f"approval_{uuid4().hex}",
|
|
575
|
+
"call_id": call_id,
|
|
576
|
+
"type": "approval_request",
|
|
577
|
+
"status": "in_progress",
|
|
578
|
+
"action": data,
|
|
579
|
+
}
|
|
580
|
+
else:
|
|
581
|
+
return None
|
|
582
|
+
response_event = "response.output_item.done" if done else "response.output_item.added"
|
|
583
|
+
return response_event, {"type": response_event, "item": item}
|
|
584
|
+
|
|
585
|
+
@classmethod
|
|
586
|
+
def _response_delta_sse(
|
|
587
|
+
cls,
|
|
588
|
+
event_name: str,
|
|
589
|
+
*,
|
|
590
|
+
invocation_id: str,
|
|
591
|
+
delta: str,
|
|
592
|
+
) -> str:
|
|
593
|
+
payload = cls._response_delta_payload(
|
|
594
|
+
event_name,
|
|
595
|
+
invocation_id=invocation_id,
|
|
596
|
+
delta=delta,
|
|
597
|
+
)
|
|
598
|
+
return cls._sse(event_name, payload)
|
|
599
|
+
|
|
600
|
+
@staticmethod
|
|
601
|
+
def _response_delta_payload(
|
|
602
|
+
event_name: str,
|
|
603
|
+
*,
|
|
604
|
+
invocation_id: str,
|
|
605
|
+
delta: str,
|
|
606
|
+
) -> dict[str, Any]:
|
|
607
|
+
return {
|
|
608
|
+
"type": event_name,
|
|
609
|
+
"item_id": f"msg_{invocation_id}",
|
|
610
|
+
"output_index": 0,
|
|
611
|
+
"content_index": 0,
|
|
612
|
+
"delta": delta,
|
|
613
|
+
}
|
|
614
|
+
|
|
615
|
+
@staticmethod
|
|
616
|
+
def _response_payload(
|
|
617
|
+
run: RunRecord,
|
|
618
|
+
*,
|
|
619
|
+
model: str,
|
|
620
|
+
response_id: str | None = None,
|
|
621
|
+
) -> dict[str, Any]:
|
|
622
|
+
return {
|
|
623
|
+
"id": response_id or run.id,
|
|
624
|
+
"object": "response",
|
|
625
|
+
"status": "completed",
|
|
626
|
+
"model": run.model or model,
|
|
627
|
+
"output": [
|
|
628
|
+
{
|
|
629
|
+
"id": f"msg_{response_id or run.id}",
|
|
630
|
+
"type": "message",
|
|
631
|
+
"status": "completed",
|
|
632
|
+
"role": "assistant",
|
|
633
|
+
"content": [
|
|
634
|
+
{
|
|
635
|
+
"type": "output_text",
|
|
636
|
+
"text": run.output,
|
|
637
|
+
"annotations": [],
|
|
638
|
+
}
|
|
639
|
+
],
|
|
640
|
+
}
|
|
641
|
+
],
|
|
642
|
+
"usage": {
|
|
643
|
+
"input_tokens": run.usage.input_tokens,
|
|
644
|
+
"output_tokens": run.usage.output_tokens,
|
|
645
|
+
"total_tokens": run.usage.total_tokens,
|
|
646
|
+
},
|
|
647
|
+
"metadata": {
|
|
648
|
+
"session_id": run.session_id,
|
|
649
|
+
"trace_id": run.trace_id,
|
|
650
|
+
"agent_id": run.agent_id,
|
|
651
|
+
"runtime_run_id": run.id,
|
|
652
|
+
},
|
|
653
|
+
}
|
|
654
|
+
|
|
655
|
+
async def _ensure_build(self, agent_id: str):
|
|
656
|
+
if self.studio.is_codex_agent(agent_id):
|
|
657
|
+
self.studio.codex_agent_detail(agent_id)
|
|
658
|
+
builds = [
|
|
659
|
+
record
|
|
660
|
+
for record in self.studio.codex_builds.list()
|
|
661
|
+
if record.agent_name == agent_id and self.studio.codex_builder.is_current(record)
|
|
662
|
+
]
|
|
663
|
+
if builds:
|
|
664
|
+
return builds[0]
|
|
665
|
+
return await asyncio.to_thread(self.studio.codex_builder.build, agent_id)
|
|
666
|
+
for record in self.studio.builds.list_for_agent(agent_id):
|
|
667
|
+
if record.status == BuildStatus.SUCCEEDED:
|
|
668
|
+
return record
|
|
669
|
+
draft = self.studio.drafts.get(agent_id)
|
|
670
|
+
if draft.spec.model is None and not draft.spec.bindings.model_profile_id:
|
|
671
|
+
raise StudioError(
|
|
672
|
+
"AGENT_MODEL_REQUIRED",
|
|
673
|
+
"当前 Agent 未绑定 Model Profile,请先在 Agent 配置中选择模型;"
|
|
674
|
+
"API Key 只提供访问凭证,不会自动绑定模型。",
|
|
675
|
+
status_code=422,
|
|
676
|
+
field="spec.bindings.modelProfileId",
|
|
677
|
+
)
|
|
678
|
+
return await asyncio.to_thread(self.studio.builder.build, draft)
|
|
679
|
+
|
|
680
|
+
def _sessions(self, agent_id: str) -> list[dict[str, Any]]:
|
|
681
|
+
grouped: dict[str, list[RunRecord]] = {}
|
|
682
|
+
for run in self.studio.event_store.list_runs():
|
|
683
|
+
if run.agent_id != agent_id:
|
|
684
|
+
continue
|
|
685
|
+
grouped.setdefault(run.session_id, []).append(run)
|
|
686
|
+
records = [self._session_record(runs) for runs in grouped.values()]
|
|
687
|
+
records.sort(key=lambda item: item["UpdatedAt"], reverse=True)
|
|
688
|
+
return records
|
|
689
|
+
|
|
690
|
+
def _session_record(self, runs: list[RunRecord]) -> dict[str, Any]:
|
|
691
|
+
ordered = sorted(
|
|
692
|
+
runs,
|
|
693
|
+
key=lambda run: (
|
|
694
|
+
run.started_at or run.completed_at or datetime.min.replace(tzinfo=timezone.utc)
|
|
695
|
+
),
|
|
696
|
+
)
|
|
697
|
+
first = ordered[0]
|
|
698
|
+
latest = ordered[-1]
|
|
699
|
+
usage = {
|
|
700
|
+
"input_tokens": sum(run.usage.input_tokens for run in ordered),
|
|
701
|
+
"output_tokens": sum(run.usage.output_tokens for run in ordered),
|
|
702
|
+
"total_tokens": sum(run.usage.total_tokens for run in ordered),
|
|
703
|
+
"turns": len(ordered),
|
|
704
|
+
"last_response_id": latest.id,
|
|
705
|
+
}
|
|
706
|
+
return {
|
|
707
|
+
"SessionId": first.session_id,
|
|
708
|
+
"AgentId": first.agent_id,
|
|
709
|
+
"UserId": "local-user",
|
|
710
|
+
"Title": self._short_title(first.input),
|
|
711
|
+
"FirstPrompt": first.input,
|
|
712
|
+
"LastPrompt": latest.input,
|
|
713
|
+
"CreatedAt": self._timestamp(first.started_at),
|
|
714
|
+
"UpdatedAt": self._timestamp(latest.completed_at or latest.started_at),
|
|
715
|
+
"ActiveRunStatus": self._active_status(latest.status),
|
|
716
|
+
"ActiveInvocationId": latest.id if latest.status == RunStatus.RUNNING else "",
|
|
717
|
+
"TokenUsage": usage,
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
def _model_descriptor(
|
|
721
|
+
self, agent_id: str, models: list[dict[str, Any]] | None = None
|
|
722
|
+
) -> dict[str, Any]:
|
|
723
|
+
if models is None:
|
|
724
|
+
models = self._model_descriptors(agent_id)
|
|
725
|
+
if self.studio.is_codex_agent(agent_id):
|
|
726
|
+
default_model = self.studio.codex_manifests.load(agent_id).manifest.model
|
|
727
|
+
return next(
|
|
728
|
+
(item for item in models if item["id"] == default_model),
|
|
729
|
+
models[0],
|
|
730
|
+
)
|
|
731
|
+
return models[0]
|
|
732
|
+
|
|
733
|
+
def _model_descriptors(self, agent_id: str) -> list[dict[str, Any]]:
|
|
734
|
+
draft = self._draft(agent_id)
|
|
735
|
+
if self.studio.is_codex_agent(agent_id):
|
|
736
|
+
manifest = self.studio.codex_manifests.load(agent_id).manifest
|
|
737
|
+
# catalog.list 每次调用都要全量扫描(内建工具/持久化目录/Skill),
|
|
738
|
+
# 按 allowed_models 逐个扫会成倍放大,这里一次取出后复用。
|
|
739
|
+
catalog_models = self.studio.catalog.list(kind="model", limit=500)
|
|
740
|
+
codex_descriptors = [
|
|
741
|
+
self._model_descriptor_for_name(draft, model, catalog_models)
|
|
742
|
+
for model in manifest.allowed_models
|
|
743
|
+
]
|
|
744
|
+
return codex_descriptors
|
|
745
|
+
|
|
746
|
+
binding_ids = list(draft.spec.bindings.model_profile_ids)
|
|
747
|
+
if not binding_ids and draft.spec.bindings.model_profile_id:
|
|
748
|
+
binding_ids = [draft.spec.bindings.model_profile_id]
|
|
749
|
+
descriptors: list[dict[str, Any]] = []
|
|
750
|
+
for binding_id in binding_ids:
|
|
751
|
+
spec = self.studio.catalog.resolve_model(
|
|
752
|
+
draft.spec.bindings.model_copy(
|
|
753
|
+
update={"model_profile_id": binding_id, "model_profile_ids": []}
|
|
754
|
+
)
|
|
755
|
+
)
|
|
756
|
+
if spec is not None:
|
|
757
|
+
descriptors.append(
|
|
758
|
+
self._model_descriptor_from_spec(
|
|
759
|
+
draft,
|
|
760
|
+
spec,
|
|
761
|
+
display_name=self.studio.catalog.get(binding_id).display_name,
|
|
762
|
+
)
|
|
763
|
+
)
|
|
764
|
+
if descriptors:
|
|
765
|
+
default_id = draft.spec.bindings.model_profile_id
|
|
766
|
+
if default_id and default_id in binding_ids:
|
|
767
|
+
default_index = binding_ids.index(default_id)
|
|
768
|
+
descriptors.insert(0, descriptors.pop(default_index))
|
|
769
|
+
return descriptors
|
|
770
|
+
if draft.spec.model is not None:
|
|
771
|
+
return [self._model_descriptor_from_spec(draft, draft.spec.model)]
|
|
772
|
+
return [self._unconfigured_model_descriptor(draft)]
|
|
773
|
+
|
|
774
|
+
def _model_descriptor_for_name(
|
|
775
|
+
self, draft, model_name: str, catalog_models: list | None = None
|
|
776
|
+
) -> dict[str, Any]:
|
|
777
|
+
if catalog_models is None:
|
|
778
|
+
catalog_models = self.studio.catalog.list(kind="model", limit=500)
|
|
779
|
+
for descriptor in catalog_models:
|
|
780
|
+
try:
|
|
781
|
+
from ksadk.studio.contracts import ModelSpec
|
|
782
|
+
|
|
783
|
+
spec = ModelSpec.model_validate(descriptor.contract)
|
|
784
|
+
except ValueError:
|
|
785
|
+
continue
|
|
786
|
+
if spec.model == model_name:
|
|
787
|
+
return self._model_descriptor_from_spec(
|
|
788
|
+
draft,
|
|
789
|
+
spec,
|
|
790
|
+
display_name=descriptor.display_name,
|
|
791
|
+
)
|
|
792
|
+
return {
|
|
793
|
+
"id": model_name,
|
|
794
|
+
"display_name": model_name,
|
|
795
|
+
"source": "agentkit-studio",
|
|
796
|
+
"context_window_tokens": draft.spec.context.max_input_tokens,
|
|
797
|
+
"max_output_tokens": 2048,
|
|
798
|
+
"capabilities": {
|
|
799
|
+
"function_calling": True,
|
|
800
|
+
"structured_output": True,
|
|
801
|
+
},
|
|
802
|
+
}
|
|
803
|
+
|
|
804
|
+
@staticmethod
|
|
805
|
+
def _model_descriptor_from_spec(
|
|
806
|
+
draft,
|
|
807
|
+
spec,
|
|
808
|
+
*,
|
|
809
|
+
display_name: str | None = None,
|
|
810
|
+
) -> dict[str, Any]:
|
|
811
|
+
metadata: dict[str, Any] = {}
|
|
812
|
+
model_id = spec.model
|
|
813
|
+
metadata = dict(spec.metadata or {})
|
|
814
|
+
resolved_display_name = display_name or spec.model
|
|
815
|
+
max_output_tokens = spec.parameters.max_tokens
|
|
816
|
+
return {
|
|
817
|
+
**metadata,
|
|
818
|
+
"id": model_id,
|
|
819
|
+
"display_name": resolved_display_name,
|
|
820
|
+
"source": "agentkit-studio",
|
|
821
|
+
"context_window_tokens": metadata.get("context_window_tokens")
|
|
822
|
+
or draft.spec.context.max_input_tokens,
|
|
823
|
+
"max_output_tokens": max_output_tokens,
|
|
824
|
+
"capabilities": {
|
|
825
|
+
**dict(metadata.get("capabilities") or {}),
|
|
826
|
+
"function_calling": True,
|
|
827
|
+
"structured_output": True,
|
|
828
|
+
},
|
|
829
|
+
}
|
|
830
|
+
|
|
831
|
+
@staticmethod
|
|
832
|
+
def _unconfigured_model_descriptor(draft) -> dict[str, Any]:
|
|
833
|
+
model_id = str(
|
|
834
|
+
draft.metadata.labels.get("agentkit.ksyun.com/model") or "unconfigured-model"
|
|
835
|
+
)
|
|
836
|
+
return {
|
|
837
|
+
"id": model_id,
|
|
838
|
+
"display_name": model_id if model_id != "unconfigured-model" else "未配置模型",
|
|
839
|
+
"source": "agentkit-studio",
|
|
840
|
+
"context_window_tokens": draft.spec.context.max_input_tokens,
|
|
841
|
+
"max_output_tokens": 2048,
|
|
842
|
+
"capabilities": {
|
|
843
|
+
"function_calling": True,
|
|
844
|
+
"structured_output": True,
|
|
845
|
+
},
|
|
846
|
+
}
|
|
847
|
+
|
|
848
|
+
def _select_model(self, agent_id: str, requested: str) -> str:
|
|
849
|
+
models = self._model_descriptors(agent_id)
|
|
850
|
+
allowed = [str(item["id"]) for item in models]
|
|
851
|
+
default = str(self._model_descriptor(agent_id, models)["id"])
|
|
852
|
+
selected = requested.strip() or default
|
|
853
|
+
if selected not in allowed:
|
|
854
|
+
raise StudioError(
|
|
855
|
+
"MODEL_NOT_BOUND",
|
|
856
|
+
"请求模型未绑定到当前 Agent Build",
|
|
857
|
+
status_code=422,
|
|
858
|
+
details={"model": selected, "allowedModels": allowed},
|
|
859
|
+
)
|
|
860
|
+
return selected
|
|
861
|
+
|
|
862
|
+
def _draft(self, agent_id: str):
|
|
863
|
+
return self.studio.agent_detail(agent_id)["draft"]
|
|
864
|
+
|
|
865
|
+
def _run_activities(self, run: RunRecord) -> list[dict[str, Any]]:
|
|
866
|
+
activities: list[dict[str, Any]] = []
|
|
867
|
+
for event in self.studio.event_store.events(run.id):
|
|
868
|
+
operations = event.data.get("a2ui_operations") or event.data.get("operations")
|
|
869
|
+
if not isinstance(operations, list):
|
|
870
|
+
continue
|
|
871
|
+
surface_id = str(
|
|
872
|
+
event.data.get("surfaceId") or event.data.get("surface_id") or f"{run.id}-surface"
|
|
873
|
+
)
|
|
874
|
+
activities.append(
|
|
875
|
+
{
|
|
876
|
+
"SeqId": event.id,
|
|
877
|
+
"Type": event.type,
|
|
878
|
+
"MessageId": f"{run.id}:assistant",
|
|
879
|
+
"SurfaceId": surface_id,
|
|
880
|
+
"Content": {"a2ui_operations": operations},
|
|
881
|
+
}
|
|
882
|
+
)
|
|
883
|
+
return activities
|
|
884
|
+
|
|
885
|
+
@staticmethod
|
|
886
|
+
def _input_text(payload: dict[str, Any]) -> str:
|
|
887
|
+
sources = payload.get("ResponsesInput") or payload.get("Messages") or []
|
|
888
|
+
if not isinstance(sources, list):
|
|
889
|
+
raise StudioError(
|
|
890
|
+
"RUN_INPUT_INVALID",
|
|
891
|
+
"会话输入格式无效",
|
|
892
|
+
status_code=422,
|
|
893
|
+
)
|
|
894
|
+
for message in reversed(sources):
|
|
895
|
+
if not isinstance(message, dict):
|
|
896
|
+
continue
|
|
897
|
+
content = message.get("content")
|
|
898
|
+
if isinstance(content, str) and content.strip():
|
|
899
|
+
return content.strip()
|
|
900
|
+
if not isinstance(content, list):
|
|
901
|
+
continue
|
|
902
|
+
texts = [
|
|
903
|
+
str(part.get("text") or "")
|
|
904
|
+
for part in content
|
|
905
|
+
if isinstance(part, dict) and str(part.get("type") or "") in {"input_text", "text"}
|
|
906
|
+
]
|
|
907
|
+
value = "\n".join(text.strip() for text in texts if text.strip())
|
|
908
|
+
if value:
|
|
909
|
+
return value
|
|
910
|
+
raise StudioError(
|
|
911
|
+
"RUN_INPUT_REQUIRED",
|
|
912
|
+
"请输入消息后再发送",
|
|
913
|
+
status_code=422,
|
|
914
|
+
)
|
|
915
|
+
|
|
916
|
+
@staticmethod
|
|
917
|
+
def _runtime_input(payload: dict[str, Any]) -> list[dict[str, str]]:
|
|
918
|
+
"""Project the latest Responses user message into native multimodal input."""
|
|
919
|
+
|
|
920
|
+
sources = payload.get("ResponsesInput") or payload.get("Messages") or []
|
|
921
|
+
if not isinstance(sources, list):
|
|
922
|
+
return []
|
|
923
|
+
for message in reversed(sources):
|
|
924
|
+
if not isinstance(message, dict) or str(message.get("role") or "user") != "user":
|
|
925
|
+
continue
|
|
926
|
+
content = message.get("content")
|
|
927
|
+
if isinstance(content, str) and content.strip():
|
|
928
|
+
return []
|
|
929
|
+
if not isinstance(content, list):
|
|
930
|
+
continue
|
|
931
|
+
items: list[dict[str, str]] = []
|
|
932
|
+
has_image = False
|
|
933
|
+
for part in content:
|
|
934
|
+
if not isinstance(part, dict):
|
|
935
|
+
continue
|
|
936
|
+
kind = str(part.get("type") or "")
|
|
937
|
+
if kind in {"input_text", "text"}:
|
|
938
|
+
text = str(part.get("text") or "")
|
|
939
|
+
if text:
|
|
940
|
+
items.append({"type": "text", "text": text})
|
|
941
|
+
elif kind in {"input_image", "image"}:
|
|
942
|
+
url = str(
|
|
943
|
+
part.get("image_url") or part.get("imageUrl") or part.get("url") or ""
|
|
944
|
+
)
|
|
945
|
+
if url:
|
|
946
|
+
items.append({"type": "image", "url": url})
|
|
947
|
+
has_image = True
|
|
948
|
+
if items and has_image:
|
|
949
|
+
return items
|
|
950
|
+
return []
|
|
951
|
+
|
|
952
|
+
@staticmethod
|
|
953
|
+
def _active_status(status: RunStatus) -> str:
|
|
954
|
+
return "running" if status == RunStatus.RUNNING else ""
|
|
955
|
+
|
|
956
|
+
@staticmethod
|
|
957
|
+
def _run_output(run: RunRecord) -> str:
|
|
958
|
+
if run.output:
|
|
959
|
+
return run.output
|
|
960
|
+
if run.error:
|
|
961
|
+
return str(run.error.get("message") or "Agent 运行失败")
|
|
962
|
+
return f"运行状态:{run.status.value}"
|
|
963
|
+
|
|
964
|
+
@staticmethod
|
|
965
|
+
def _short_title(value: str, limit: int = 36) -> str:
|
|
966
|
+
text = " ".join(value.split())
|
|
967
|
+
return text if len(text) <= limit else f"{text[:limit]}..."
|
|
968
|
+
|
|
969
|
+
@staticmethod
|
|
970
|
+
def _timestamp(value: datetime | None) -> str:
|
|
971
|
+
return (value or datetime.now(timezone.utc)).isoformat()
|
|
972
|
+
|
|
973
|
+
@staticmethod
|
|
974
|
+
def _sse(event: str, payload: dict[str, Any]) -> str:
|
|
975
|
+
data = json.dumps(payload, ensure_ascii=False, separators=(",", ":"))
|
|
976
|
+
return f"event: {event}\ndata: {data}\n\n"
|
|
977
|
+
|
|
978
|
+
@classmethod
|
|
979
|
+
def _failed_sse(cls, invocation_id: str, message: str) -> str:
|
|
980
|
+
return cls._sse(
|
|
981
|
+
"response.failed",
|
|
982
|
+
{
|
|
983
|
+
"type": "response.failed",
|
|
984
|
+
"response": {
|
|
985
|
+
"id": invocation_id,
|
|
986
|
+
"status": "failed",
|
|
987
|
+
"error": {"message": message},
|
|
988
|
+
},
|
|
989
|
+
"error": {"message": message},
|
|
990
|
+
},
|
|
991
|
+
)
|
|
992
|
+
|
|
993
|
+
|
|
994
|
+
__all__ = ["StudioSharedWebBridge"]
|