agentengine-sdk-python 0.8.1__py3-none-any.whl → 0.8.3__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.
Files changed (476) hide show
  1. {agentengine_sdk_python-0.8.1.dist-info → agentengine_sdk_python-0.8.3.dist-info}/METADATA +25 -12
  2. {agentengine_sdk_python-0.8.1.dist-info → agentengine_sdk_python-0.8.3.dist-info}/RECORD +421 -232
  3. ksadk/_build_provenance.json +1 -0
  4. ksadk/a2a/_space_client_events.py +381 -0
  5. ksadk/a2a/event_adapter.py +156 -64
  6. ksadk/a2a/executor.py +179 -63
  7. ksadk/a2a/space_client.py +6 -333
  8. ksadk/a2ui/core.py +222 -53
  9. ksadk/agui/_agent_helpers.py +247 -0
  10. ksadk/agui/a2ui_projection.py +12 -5
  11. ksadk/agui/agent.py +443 -259
  12. ksadk/api/client.py +528 -27
  13. ksadk/builders/code_builder.py +284 -8
  14. ksadk/builders/container_builder.py +8 -1
  15. ksadk/builders/managed_runtime_builder.py +42 -35
  16. ksadk/cli/__init__.py +23 -0
  17. ksadk/cli/cmd_dashboard.py +15 -1
  18. ksadk/cli/cmd_deploy.py +10 -2
  19. ksadk/cli/cmd_eval.py +66 -9
  20. ksadk/cli/cmd_evalset.py +284 -0
  21. ksadk/cli/cmd_files.py +5 -1
  22. ksadk/cli/cmd_hermes.py +1 -126
  23. ksadk/cli/cmd_invoke.py +28 -38
  24. ksadk/cli/cmd_managed_runtime.py +95 -0
  25. ksadk/cli/cmd_observe.py +85 -0
  26. ksadk/cli/cmd_openclaw.py +1 -435
  27. ksadk/cli/cmd_plugin.py +657 -0
  28. ksadk/cli/cmd_replay.py +28 -4
  29. ksadk/cli/cmd_studio.py +99 -8
  30. ksadk/cli/cmd_web.py +10 -2
  31. ksadk/cli/hermes_env.py +151 -0
  32. ksadk/cli/invoke_payload.py +49 -0
  33. ksadk/cli/openclaw_env.py +458 -0
  34. ksadk/codex/client.py +180 -30
  35. ksadk/codex/runtime.py +475 -331
  36. ksadk/configs/env_registry.py +94 -12
  37. ksadk/configs/env_registry_pcm.py +181 -0
  38. ksadk/configs/env_var_spec.py +18 -0
  39. ksadk/configs/global_config.py +5 -0
  40. ksadk/configs/settings.py +9 -0
  41. ksadk/context_engine/__init__.py +98 -0
  42. ksadk/context_engine/assembler.py +176 -0
  43. ksadk/context_engine/baseline.py +419 -0
  44. ksadk/context_engine/cache_observability.py +228 -0
  45. ksadk/context_engine/capabilities.py +443 -0
  46. ksadk/context_engine/contributors.py +409 -0
  47. ksadk/context_engine/hosted_pipeline.py +417 -0
  48. ksadk/context_engine/models.py +117 -0
  49. ksadk/context_engine/planner.py +698 -0
  50. ksadk/context_engine/policies.py +319 -0
  51. ksadk/context_engine/projection.py +31 -0
  52. ksadk/context_engine/shadow_plan.py +266 -0
  53. ksadk/context_engine/tokenizer.py +164 -0
  54. ksadk/conversations/__init__.py +19 -0
  55. ksadk/conversations/context.py +230 -30
  56. ksadk/conversations/contracts.py +266 -0
  57. ksadk/conversations/message_projection.py +308 -26
  58. ksadk/conversations/model_context.py +49 -3
  59. ksadk/conversations/projector.py +616 -0
  60. ksadk/conversations/reducer.py +98 -0
  61. ksadk/conversations/runtime_compaction.py +207 -5
  62. ksadk/conversations/runtime_input.py +184 -2
  63. ksadk/conversations/runtime_invocation.py +81 -3
  64. ksadk/conversations/runtime_observability.py +141 -0
  65. ksadk/conversations/runtime_payloads.py +42 -0
  66. ksadk/conversations/runtime_preparation.py +272 -2
  67. ksadk/conversations/runtime_resume.py +20 -1
  68. ksadk/conversations/runtime_stream_events.py +94 -9
  69. ksadk/conversations/semantic_summary.py +311 -1
  70. ksadk/conversations/session_lock.py +51 -0
  71. ksadk/deployment/env_forward.py +72 -0
  72. ksadk/deployment/managed_runtime.py +5 -0
  73. ksadk/deployment/providers/serverless.py +143 -69
  74. ksadk/evaluation/__init__.py +32 -0
  75. ksadk/evaluation/a2a_adapter.py +9 -1
  76. ksadk/evaluation/adapters.py +18 -5
  77. ksadk/evaluation/agent_eval_client.py +395 -0
  78. ksadk/evaluation/cloud_binding.py +80 -0
  79. ksadk/evaluation/cloud_converter.py +194 -0
  80. ksadk/evaluation/cloud_service.py +199 -0
  81. ksadk/evaluation/contracts.py +73 -4
  82. ksadk/evaluation/evaluators.py +183 -17
  83. ksadk/evaluation/evidence.py +258 -0
  84. ksadk/evaluation/executor.py +31 -6
  85. ksadk/evaluation/local_adapter.py +550 -0
  86. ksadk/evaluation/service_env.py +27 -0
  87. ksadk/evaluation/studio_build_adapter.py +253 -0
  88. ksadk/evaluation/target.py +15 -6
  89. ksadk/events/__init__.py +13 -13
  90. ksadk/events/_v1_compat/__init__.py +1 -0
  91. ksadk/events/_v1_compat/models.py +299 -0
  92. ksadk/events/_v1_compat/parser.py +247 -0
  93. ksadk/events/_v1_compat/projection.py +755 -0
  94. ksadk/events/adapters/__init__.py +34 -0
  95. ksadk/events/adapters/_a2a_snapshot.py +622 -0
  96. ksadk/events/adapters/_a2a_support.py +275 -0
  97. ksadk/events/adapters/_codex_interactions.py +375 -0
  98. ksadk/events/adapters/_codex_items.py +801 -0
  99. ksadk/events/adapters/_codex_validators.py +311 -0
  100. ksadk/events/adapters/_langgraph_support.py +785 -0
  101. ksadk/events/adapters/a2a.py +841 -0
  102. ksadk/events/adapters/adk.py +503 -0
  103. ksadk/events/adapters/codex.py +907 -0
  104. ksadk/events/adapters/langgraph.py +745 -0
  105. ksadk/events/canonical.py +440 -0
  106. ksadk/events/canonical_replay.py +497 -0
  107. ksadk/events/canonical_store.py +468 -0
  108. ksadk/events/cold_recovery.py +257 -0
  109. ksadk/events/content.py +89 -0
  110. ksadk/events/identity.py +86 -0
  111. ksadk/events/pipeline.py +455 -0
  112. ksadk/events/projections.py +66 -0
  113. ksadk/events/reducer.py +661 -0
  114. ksadk/events/replay.py +3 -36
  115. ksadk/events/runtime_event.py +20 -265
  116. ksadk/events/session_event.py +324 -0
  117. ksadk/events/store.py +3 -316
  118. ksadk/events/v1_compat.py +41 -0
  119. ksadk/harness/__init__.py +45 -35
  120. ksadk/harness/runtime.py +220 -58
  121. ksadk/identity/resolver.py +4 -3
  122. ksadk/interaction/__init__.py +28 -0
  123. ksadk/interaction/contracts.py +136 -0
  124. ksadk/interaction/ledger.py +189 -0
  125. ksadk/interaction/provider.py +122 -0
  126. ksadk/interaction/providers/__init__.py +52 -0
  127. ksadk/interaction/providers/adk.py +40 -0
  128. ksadk/interaction/providers/codex.py +94 -0
  129. ksadk/interaction/providers/langgraph.py +58 -0
  130. ksadk/kernel/__init__.py +117 -0
  131. ksadk/kernel/authorization.py +258 -0
  132. ksadk/kernel/bootstrap.py +1074 -0
  133. ksadk/kernel/contract_fingerprints.py +62 -0
  134. ksadk/kernel/contracts.py +370 -0
  135. ksadk/kernel/control.py +220 -0
  136. ksadk/kernel/errors.py +79 -0
  137. ksadk/kernel/ingress.py +1123 -0
  138. ksadk/kernel/mapping.py +111 -0
  139. ksadk/kernel/memory_store.py +1134 -0
  140. ksadk/kernel/postgres_store.py +1757 -0
  141. ksadk/kernel/recovery.py +452 -0
  142. ksadk/kernel/runtime_identity.py +103 -0
  143. ksadk/kernel/sql/001_agent_kernel.sql +120 -0
  144. ksadk/kernel/sqlite_store.py +1411 -0
  145. ksadk/kernel/state.py +145 -0
  146. ksadk/kernel/store.py +232 -0
  147. ksadk/kernel/worker.py +964 -0
  148. ksadk/knowledge_base/client.py +7 -0
  149. ksadk/knowledge_base/service.py +32 -5
  150. ksadk/memory/__init__.py +61 -0
  151. ksadk/memory/adk/backends/base_ltm_backend.py +96 -1
  152. ksadk/memory/adk/backends/http_ltm_backend.py +14 -5
  153. ksadk/memory/adk/backends/inmemory_ltm_backend.py +136 -3
  154. ksadk/memory/adk/backends/sdk_ltm_backend.py +384 -2
  155. ksadk/memory/adk/backends/sqlite_ltm_backend.py +95 -0
  156. ksadk/memory/coordinator.py +461 -0
  157. ksadk/memory/events.py +202 -0
  158. ksadk/memory/extraction.py +228 -0
  159. ksadk/memory/ltm_backend_factory.py +16 -3
  160. ksadk/memory/models.py +316 -0
  161. ksadk/memory/policy.py +215 -0
  162. ksadk/memory/provider.py +47 -0
  163. ksadk/memory/provider_adapter.py +159 -0
  164. ksadk/memory/provider_resolver.py +71 -0
  165. ksadk/memory/providers/__init__.py +5 -0
  166. ksadk/memory/providers/local_sqlite.py +497 -0
  167. ksadk/memory/resolved_policy.py +145 -0
  168. ksadk/memory/service.py +188 -6
  169. ksadk/model_proxy/bootstrap.py +16 -9
  170. ksadk/model_proxy/detect.py +202 -9
  171. ksadk/model_proxy/namespace.py +39 -15
  172. ksadk/model_proxy/server.py +24 -5
  173. ksadk/model_proxy/transform.py +60 -2
  174. ksadk/observability/__init__.py +25 -0
  175. ksadk/observability/session_log.py +420 -0
  176. ksadk/observability/trajectory.py +105 -0
  177. ksadk/plugins/__init__.py +167 -0
  178. ksadk/plugins/bridges/__init__.py +45 -0
  179. ksadk/plugins/bridges/codex.py +552 -0
  180. ksadk/plugins/bridges/dsh.py +1116 -0
  181. ksadk/plugins/builtins.py +747 -0
  182. ksadk/plugins/bundle.py +246 -0
  183. ksadk/plugins/bundle_security.py +167 -0
  184. ksadk/plugins/composition.py +529 -0
  185. ksadk/plugins/context_contributor.py +437 -0
  186. ksadk/plugins/contracts.py +451 -0
  187. ksadk/plugins/dsh_toolchain.py +935 -0
  188. ksadk/plugins/ecosystem_bridge.py +1105 -0
  189. ksadk/plugins/ecosystem_probe.py +191 -0
  190. ksadk/plugins/host.py +724 -0
  191. ksadk/plugins/providers/__init__.py +85 -0
  192. ksadk/plugins/providers/bundles/ksadk-codex/cordis.patch.yml +3 -0
  193. ksadk/plugins/providers/bundles/ksadk-codex/index.mjs +18 -0
  194. ksadk/plugins/providers/bundles/ksadk-codex/package.json +19 -0
  195. ksadk/plugins/providers/bundles/ksadk-harness/cordis.patch.yml +3 -0
  196. ksadk/plugins/providers/bundles/ksadk-harness/index.mjs +19 -0
  197. ksadk/plugins/providers/bundles/ksadk-harness/package.json +19 -0
  198. ksadk/plugins/providers/codex.py +722 -0
  199. ksadk/plugins/providers/codex_dsh.py +83 -0
  200. ksadk/plugins/providers/dsh.py +951 -0
  201. ksadk/plugins/providers/dsh_descriptor_host.py +226 -0
  202. ksadk/plugins/providers/harness.py +627 -0
  203. ksadk/plugins/providers/harness_dsh.py +91 -0
  204. ksadk/plugins/providers/legacy.py +215 -0
  205. ksadk/plugins/providers/legacy_catalog.py +100 -0
  206. ksadk/plugins/providers/shipped_dsh.py +370 -0
  207. ksadk/plugins/resolver.py +331 -0
  208. ksadk/plugins/subagent_providers/__init__.py +12 -0
  209. ksadk/plugins/subagent_providers/codex.py +443 -0
  210. ksadk/plugins/subagents.py +333 -0
  211. ksadk/prompts/__init__.py +71 -0
  212. ksadk/prompts/compiler.py +215 -0
  213. ksadk/prompts/models.py +82 -0
  214. ksadk/prompts/projection.py +89 -0
  215. ksadk/prompts/resolved.py +137 -0
  216. ksadk/prompts/sources.py +233 -0
  217. ksadk/runners/_langgraph_runner_streams.py +822 -0
  218. ksadk/runners/adk_runner.py +5 -5
  219. ksadk/runners/base_runner.py +16 -0
  220. ksadk/runners/factory.py +1 -2
  221. ksadk/runners/langgraph_runner.py +198 -437
  222. ksadk/runtime/_runner_adapter/__init__.py +1 -0
  223. ksadk/runtime/_runner_adapter/stream_mapping.py +797 -0
  224. ksadk/runtime/adapter.py +119 -2
  225. ksadk/runtime/conversation_execution.py +316 -182
  226. ksadk/runtime/executor.py +144 -3
  227. ksadk/runtime/factory.py +268 -8
  228. ksadk/runtime/framework_adapters.py +18 -0
  229. ksadk/runtime/hosted_finalizer.py +268 -0
  230. ksadk/runtime/launch.py +14 -1
  231. ksadk/runtime/preprocessing.py +25 -3
  232. ksadk/runtime/runner_adapter.py +293 -508
  233. ksadk/runtime/usage.py +33 -0
  234. ksadk/sandbox/__init__.py +2 -1
  235. ksadk/sandbox/backends/e2b.py +35 -2
  236. ksadk/sandbox/factory.py +72 -0
  237. ksadk/scheduler/__init__.py +18 -0
  238. ksadk/scheduler/calendar.py +102 -0
  239. ksadk/scheduler/contracts.py +254 -0
  240. ksadk/scheduler/dispatcher.py +129 -0
  241. ksadk/scheduler/engine.py +416 -0
  242. ksadk/scheduler/sqlite_store.py +563 -0
  243. ksadk/server/composition.py +8 -0
  244. ksadk/server/factory.py +69 -6
  245. ksadk/server/routes/kernel_ingress.py +258 -0
  246. ksadk/server/routes/openai_compat.py +88 -8
  247. ksadk/server/routes/projection.py +35 -20
  248. ksadk/server/routes/run.py +94 -0
  249. ksadk/server/routes/sessions.py +2 -0
  250. ksadk/server/routes/streaming.py +5 -2
  251. ksadk/server/static/assets/{ArtifactsPanel-CakCqxEN.js → ArtifactsPanel-10GPXIJ6.js} +1 -1
  252. ksadk/server/static/assets/{CodeBlock-CynLknyO.js → CodeBlock-BXk1l-G1.js} +4 -4
  253. ksadk/server/static/assets/MathMessageMarkdown-BorAY7qD.css +1 -0
  254. ksadk/server/static/assets/{MathMessageMarkdown-3sQdOysS.js → MathMessageMarkdown-D9uB55Oh.js} +4 -4
  255. ksadk/server/static/assets/MermaidBlock--OEYoXIJ.js +312 -0
  256. ksadk/server/static/assets/NativeTerminalPanel-DrEe1syq.js +1 -0
  257. ksadk/server/static/assets/abnfDiagram-VCTEODGH-eeTFJLvo.js +1 -0
  258. ksadk/server/static/assets/arc-S3ZNbIAd.js +1 -0
  259. ksadk/server/static/assets/architecture-7GRP2DOG-Dk9nRQ6-.js +1 -0
  260. ksadk/server/static/assets/architectureDiagram-5GKGNRK7-CRjgEJDX.js +36 -0
  261. ksadk/server/static/assets/blockDiagram-NRAW4CY4-C7bsBoNH.js +129 -0
  262. ksadk/server/static/assets/c4Diagram-UCG6FXSJ-BX3c33Ob.js +38 -0
  263. ksadk/server/static/assets/channel-s354Yo6o.js +1 -0
  264. ksadk/server/static/assets/chunk-2Q5K7J3B-CDpPpKR5.js +1 -0
  265. ksadk/server/static/assets/{chunk-FMBD7UC4-B_fH2YuH.js → chunk-5VM5RSS4-bZ7ws-8m.js} +1 -1
  266. ksadk/server/static/assets/{chunk-ND2GUHAM-D6rFLaVV.js → chunk-F27PBJKO-D2r0pvhY.js} +1 -1
  267. ksadk/server/static/assets/chunk-G27WJ6UU-CZwF3oUP.js +231 -0
  268. ksadk/server/static/assets/{chunk-4BX2VUAB-B9drTgOW.js → chunk-JWPE2WC7-DigFYCML.js} +1 -1
  269. ksadk/server/static/assets/chunk-LCL6LL3I-CnorGV5q.js +206 -0
  270. ksadk/server/static/assets/chunk-POPQ4Y6H-CexntQA-.js +1 -0
  271. ksadk/server/static/assets/chunk-RHFEMEQ7-CHTC7UxA.js +168 -0
  272. ksadk/server/static/assets/chunk-SVP7TREG-BC1EWt_-.js +88 -0
  273. ksadk/server/static/assets/chunk-XXDRQBXY-BTG24xN0.js +1 -0
  274. ksadk/server/static/assets/classDiagram-DTDB5LWJ-Dd0nucl9.js +1 -0
  275. ksadk/server/static/assets/classDiagram-v2-JRS7N3AN-Dd0nucl9.js +1 -0
  276. ksadk/server/static/assets/{cose-bilkent-S5V4N54A-ChNs_Mdk.js → cose-bilkent-JH36ORCC-CkonRdAS.js} +1 -1
  277. ksadk/server/static/assets/cynefin-OW5HDTMX-BBlT4uMf.js +1 -0
  278. ksadk/server/static/assets/cynefinDiagram-5FMLGOSQ-DEOCBe15.js +62 -0
  279. ksadk/server/static/assets/cytoscape.esm-CyCl8rPi.js +321 -0
  280. ksadk/server/static/assets/dagre-3AP2YEHR-DBThPuLa.js +4 -0
  281. ksadk/server/static/assets/dagre-ClTPrTht.js +1 -0
  282. ksadk/server/static/assets/diagram-S7CK7UJ4-DsZHrnpJ.js +30 -0
  283. ksadk/server/static/assets/diagram-UQ7AKVKN-BJR4nhVr.js +41 -0
  284. ksadk/server/static/assets/diagram-VSXAHHWV-xzcERX0a.js +3 -0
  285. ksadk/server/static/assets/diagram-VX7I27RA-tn2xM1xu.js +24 -0
  286. ksadk/server/static/assets/diagram-Z3DM3KII-BZPGBkw5.js +24 -0
  287. ksadk/server/static/assets/{dist-gLnrGsoU.js → dist-4zN1_tq5.js} +1 -1
  288. ksadk/server/static/assets/{dist-C1sz5Eo3.js → dist-8VWI-2bZ.js} +1 -1
  289. ksadk/server/static/assets/{dist-BjOdlDjj.js → dist-B-46q3Hk.js} +1 -1
  290. ksadk/server/static/assets/{dist-o2OVmKvt.js → dist-B0gdk4rl.js} +1 -1
  291. ksadk/server/static/assets/{dist-4N2NMVYg.js → dist-B1oWRmrH.js} +1 -1
  292. ksadk/server/static/assets/{dist-CmZHzlwZ.js → dist-BI1qQH4n.js} +1 -1
  293. ksadk/server/static/assets/{dist-X4TnqwOK.js → dist-BJLUwH6W.js} +1 -1
  294. ksadk/server/static/assets/{dist-B5oS1zcj.js → dist-Bbwqa8TG.js} +1 -1
  295. ksadk/server/static/assets/{dist-j6Dxmf_j.js → dist-Btyo4vX4.js} +1 -1
  296. ksadk/server/static/assets/{dist-DQU9EWw-.js → dist-C71CsujO.js} +1 -1
  297. ksadk/server/static/assets/{dist-BIlKoW0k.js → dist-CXpH_Rdd.js} +1 -1
  298. ksadk/server/static/assets/{dist-DJN_6Y39.js → dist-Cq4OL6nO.js} +1 -1
  299. ksadk/server/static/assets/{dist-Dli7qW-B.js → dist-D1V3f1B4.js} +1 -1
  300. ksadk/server/static/assets/{dist-fhOuWds5.js → dist-DJmCPRVB.js} +1 -1
  301. ksadk/server/static/assets/{dist-CWV4lDr1.js → dist-DQAB0qn_.js} +2 -2
  302. ksadk/server/static/assets/{dist-D6MpCZtz.js → dist-DUwKa_l6.js} +1 -1
  303. ksadk/server/static/assets/dist-DdRBIceA.js +1 -0
  304. ksadk/server/static/assets/{dist-DWKByxWV.js → dist-DfGFrb0v.js} +1 -1
  305. ksadk/server/static/assets/{dist-DsHPdk3W.js → dist-DjhwGUgZ.js} +1 -1
  306. ksadk/server/static/assets/{dist-B-jNR6Vn.js → dist-DyqV4fL4.js} +1 -1
  307. ksadk/server/static/assets/{dist-BB3-OwTQ.js → dist-M67Hwu0n.js} +1 -1
  308. ksadk/server/static/assets/{dist-_pHPTyZ-.js → dist-u8tVH5Ak.js} +1 -1
  309. ksadk/server/static/assets/ebnfDiagram-PWID7BFC-DVfMsLge.js +1 -0
  310. ksadk/server/static/assets/erDiagram-SSCWMZ5O-CWEZSYaw.js +99 -0
  311. ksadk/server/static/assets/eventmodeling-NTZA5JFV-Dr5okd8V.js +1 -0
  312. ksadk/server/static/assets/flowDiagram-A5DVABFB-VEO-cVcO.js +1 -0
  313. ksadk/server/static/assets/ganttDiagram-EL5Y4UJY-TWIxoyYL.js +292 -0
  314. ksadk/server/static/assets/gitGraph-4MIJSDKK-CKiKY9Cf.js +1 -0
  315. ksadk/server/static/assets/gitGraphDiagram-WWUBYQGX-DDh2hd2S.js +106 -0
  316. ksadk/server/static/assets/index-B2k_urY8.js +241 -0
  317. ksadk/server/static/assets/index-ILPt6IWF.css +2 -0
  318. ksadk/server/static/assets/info-A6RAGUB7-ubA4Nm_p.js +1 -0
  319. ksadk/server/static/assets/infoDiagram-RXCK75RN-CgoyWiDR.js +2 -0
  320. ksadk/server/static/assets/{ishikawaDiagram-YF4QCWOH-K0OOfVna.js → ishikawaDiagram-5VMMS53U-CyRIu_om.js} +6 -6
  321. ksadk/server/static/assets/{journeyDiagram-JHISSGLW-8LeOJaZp.js → journeyDiagram-EYS64GPL-DN5JXHzN.js} +6 -6
  322. ksadk/server/static/assets/{kanban-definition-UN3LZRKU-C6uCgZrW.js → kanban-definition-3QL26DDD-CNP3ui66.js} +10 -10
  323. ksadk/server/static/assets/katex-DWEzQy1d.js +257 -0
  324. ksadk/server/static/assets/{linear-hdBcYzM8.js → linear-C-qHp7d2.js} +1 -1
  325. ksadk/server/static/assets/mermaid-parser.core-Cl-K943T.js +166 -0
  326. ksadk/server/static/assets/{mindmap-definition-RKZ34NQL-Bjy_2v4_.js → mindmap-definition-FBJOCRG2-D5DB5mYm.js} +29 -29
  327. ksadk/server/static/assets/packet-AYTQ26CC-C7R3NUlK.js +1 -0
  328. ksadk/server/static/assets/pegDiagram-XKGWAZYB-BQVc9ia_.js +1 -0
  329. ksadk/server/static/assets/pie-WAS4IAKB-BIT6qjr3.js +1 -0
  330. ksadk/server/static/assets/pieDiagram-E7YTZNPT-B6UCAvtY.js +39 -0
  331. ksadk/server/static/assets/{quadrantDiagram-W4KKPZXB-BmkRbwCg.js → quadrantDiagram-AXDQQJYC-mIr2d3eb.js} +6 -6
  332. ksadk/server/static/assets/radar-RG4KPBEZ-l5noef9X.js +1 -0
  333. ksadk/server/static/assets/railroad-74A4TZTK-Baea6ZvM.js +1 -0
  334. ksadk/server/static/assets/railroad-abnf-HS5TGJTU-OKn8zZnU.js +1 -0
  335. ksadk/server/static/assets/railroad-ebnf-LZEXJU2U-DdKAKC4l.js +1 -0
  336. ksadk/server/static/assets/railroad-peg-WCYAUIDC-Bds8udlG.js +1 -0
  337. ksadk/server/static/assets/railroadDiagram-O6MQD6OU-Dt3FFDmK.js +1 -0
  338. ksadk/server/static/assets/requirementDiagram-EFPCY7ZU-Cz_vQtgF.js +84 -0
  339. ksadk/server/static/assets/{sankeyDiagram-5OEKKPKP-l6iXmU65.js → sankeyDiagram-P5KCCOFB-wlO1TsH5.js} +7 -7
  340. ksadk/server/static/assets/{sequenceDiagram-3UESZ5HK-Cq6UBeQB.js → sequenceDiagram-WJ2MYXX4-CHRbElYq.js} +10 -10
  341. ksadk/server/static/assets/sizeCapture-X5ZJPWSS-DXX54iyO.js +1 -0
  342. ksadk/server/static/assets/stateDiagram-HBIQ2CUA-Dp_lw9II.js +1 -0
  343. ksadk/server/static/assets/stateDiagram-v2-4QOOHH4V-5b-8o2iS.js +1 -0
  344. ksadk/server/static/assets/swimlanes-XN3QIQJK-D8O5UlLG.js +1 -0
  345. ksadk/server/static/assets/swimlanesDiagram-VK2B7HYN-uf6WPRA4.js +8 -0
  346. ksadk/server/static/assets/{timeline-definition-PNZ67QCA-DEz24vV0.js → timeline-definition-24CTP7MA-8mfKcxvG.js} +7 -7
  347. ksadk/server/static/assets/treeView-Q6P3EWNA-CK5CSrSO.js +1 -0
  348. ksadk/server/static/assets/treemap-WGGIJYW6-Ch2BKgAM.js +1 -0
  349. ksadk/server/static/assets/vennDiagram-4TSXK5OY-EnNl0DE7.js +34 -0
  350. ksadk/server/static/assets/wardley-WFR3VGLG-YW3Q0ml_.js +1 -0
  351. ksadk/server/static/assets/wardleyDiagram-VM6X3IG4-DBu5fbYr.js +78 -0
  352. ksadk/server/static/assets/xychartDiagram-S5SC5T6Z-BSVXs4IY.js +7 -0
  353. ksadk/server/static/index.html +2 -2
  354. ksadk/sessions/__init__.py +26 -9
  355. ksadk/sessions/_local_service_sync.py +688 -0
  356. ksadk/sessions/_local_tables.py +21 -0
  357. ksadk/sessions/_postgres_schema.py +305 -0
  358. ksadk/sessions/_postgres_tables.py +15 -0
  359. ksadk/sessions/base.py +80 -1
  360. ksadk/sessions/in_memory.py +53 -2
  361. ksadk/sessions/local_service.py +63 -604
  362. ksadk/sessions/postgres_service.py +58 -148
  363. ksadk/sessions/resilient.py +35 -1
  364. ksadk/skills/service_client.py +48 -0
  365. ksadk/studio/agent_lifecycle.py +26 -20
  366. ksadk/studio/api.py +1063 -69
  367. ksadk/studio/api_catalog_routes.py +5 -0
  368. ksadk/studio/api_contracts.py +148 -6
  369. ksadk/studio/api_memory_routes.py +82 -0
  370. ksadk/studio/api_plugin_routes.py +480 -0
  371. ksadk/studio/attachment_store.py +187 -0
  372. ksadk/studio/authoring.py +259 -28
  373. ksadk/studio/authoring_coordinator.py +809 -67
  374. ksadk/studio/builder.py +197 -40
  375. ksadk/studio/capabilities.py +17 -0
  376. ksadk/studio/cloud.py +1920 -95
  377. ksadk/studio/codex_agent_service.py +137 -15
  378. ksadk/studio/codex_authoring.py +469 -0
  379. ksadk/studio/codex_builder.py +177 -25
  380. ksadk/studio/codex_manifest.py +71 -22
  381. ksadk/studio/codex_run.py +59 -4
  382. ksadk/studio/compatibility_report.py +445 -0
  383. ksadk/studio/compiler.py +7 -1
  384. ksadk/studio/contracts.py +234 -4
  385. ksadk/studio/dsh_provider_registration.py +768 -0
  386. ksadk/studio/event_store.py +79 -101
  387. ksadk/studio/framework_run.py +264 -3
  388. ksadk/studio/hosted_kernel.py +317 -0
  389. ksadk/studio/manifest_resolver.py +131 -0
  390. ksadk/studio/model_client.py +95 -9
  391. ksadk/studio/model_profile_service.py +1 -1
  392. ksadk/studio/operations.py +47 -5
  393. ksadk/studio/otel_trace.py +348 -42
  394. ksadk/studio/pcm_memory.py +85 -0
  395. ksadk/studio/plugin_composition.py +301 -0
  396. ksadk/studio/plugin_kernel_adapter.py +128 -0
  397. ksadk/studio/plugin_runtime.py +652 -0
  398. ksadk/studio/repository.py +23 -2
  399. ksadk/studio/resource_catalog.py +49 -1
  400. ksadk/studio/run_service.py +1266 -202
  401. ksadk/studio/runtime_source.py +51 -17
  402. ksadk/studio/scheduler_runtime.py +374 -0
  403. ksadk/studio/scheduler_service.py +211 -0
  404. ksadk/studio/service.py +1815 -117
  405. ksadk/studio/shared_web.py +199 -50
  406. ksadk/studio/soul.py +52 -0
  407. ksadk/studio/static/assets/index-DXs1Eq4M.css +1 -0
  408. ksadk/studio/static/assets/index-DeDqwTsD.js +283 -0
  409. ksadk/studio/static/index.html +2 -2
  410. ksadk/studio/templates.py +100 -8
  411. ksadk/studio/validator.py +20 -1
  412. ksadk/studio/workspace.py +31 -32
  413. ksadk/tracing/setup.py +304 -3
  414. ksadk/version.py +1 -1
  415. ksadk_runtime_common/schemas/runtime_event_v1.json +37 -0
  416. ksadk_runtime_common/schemas/runtime_event_v2.json +271 -0
  417. ksadk_runtime_common/workspace_files/path_utils.py +41 -0
  418. ksadk/events/parser.py +0 -191
  419. ksadk/server/static/assets/MathMessageMarkdown-DeYh8QPr.css +0 -1
  420. ksadk/server/static/assets/MermaidBlock-NERD_ef7.js +0 -303
  421. ksadk/server/static/assets/NativeTerminalPanel-CiHWf82q.js +0 -1
  422. ksadk/server/static/assets/arc-DrgjcKhl.js +0 -1
  423. ksadk/server/static/assets/architecture-7EHR7CIX-CpZLRCcY.js +0 -1
  424. ksadk/server/static/assets/architectureDiagram-3BPJPVTR-BXoVRbQ6.js +0 -36
  425. ksadk/server/static/assets/blockDiagram-GPEHLZMM-BnaB3Ywv.js +0 -132
  426. ksadk/server/static/assets/c4Diagram-AAUBKEIU-Dw4weZcp.js +0 -10
  427. ksadk/server/static/assets/channel-Dt3rQTFI.js +0 -1
  428. ksadk/server/static/assets/chunk-2J33WTMH-ChjpLK7j.js +0 -1
  429. ksadk/server/static/assets/chunk-55IACEB6-CK4ROATy.js +0 -1
  430. ksadk/server/static/assets/chunk-727SXJPM-B9KtTTgw.js +0 -206
  431. ksadk/server/static/assets/chunk-AQP2D5EJ-1O1mki3m.js +0 -231
  432. ksadk/server/static/assets/chunk-QZHKN3VN-iXEZtKQc.js +0 -1
  433. ksadk/server/static/assets/classDiagram-4FO5ZUOK-DIveV17Q.js +0 -1
  434. ksadk/server/static/assets/classDiagram-v2-Q7XG4LA2-DIveV17Q.js +0 -1
  435. ksadk/server/static/assets/cytoscape.esm-DelgaX4f.js +0 -321
  436. ksadk/server/static/assets/dagre-BM42HDAG-D0QXg_MQ.js +0 -4
  437. ksadk/server/static/assets/dagre-Bx709z4p.js +0 -1
  438. ksadk/server/static/assets/diagram-2AECGRRQ-DBDDKwBQ.js +0 -43
  439. ksadk/server/static/assets/diagram-5GNKFQAL-BkwDOxmO.js +0 -10
  440. ksadk/server/static/assets/diagram-KO2AKTUF-Bv8waYvY.js +0 -3
  441. ksadk/server/static/assets/diagram-LMA3HP47-e1VECDdu.js +0 -24
  442. ksadk/server/static/assets/diagram-OG6HWLK6-Ds9adfRl.js +0 -24
  443. ksadk/server/static/assets/dist-B5J2uhNk.js +0 -1
  444. ksadk/server/static/assets/erDiagram-TEJ5UH35-BPItFKH6.js +0 -85
  445. ksadk/server/static/assets/eventmodeling-FCH6USID-DYFSxbqr.js +0 -1
  446. ksadk/server/static/assets/flowDiagram-I6XJVG4X-BbYB9w5Y.js +0 -162
  447. ksadk/server/static/assets/ganttDiagram-6RSMTGT7-TQXa0KXv.js +0 -292
  448. ksadk/server/static/assets/gitGraph-WXDBUCRP-DyNYp6BN.js +0 -1
  449. ksadk/server/static/assets/gitGraphDiagram-PVQCEYII-bKblCnpa.js +0 -106
  450. ksadk/server/static/assets/graphlib-B8gBHxth.js +0 -1
  451. ksadk/server/static/assets/index-R52pJJvg.js +0 -177
  452. ksadk/server/static/assets/index-jJr3_955.css +0 -2
  453. ksadk/server/static/assets/info-J43DQDTF-WUx7qTde.js +0 -1
  454. ksadk/server/static/assets/infoDiagram-5YYISTIA-BGVT7J_i.js +0 -2
  455. ksadk/server/static/assets/katex-CYWseY5E.js +0 -265
  456. ksadk/server/static/assets/mermaid-parser.core-BWrDjSZM.js +0 -161
  457. ksadk/server/static/assets/packet-YPE3B663-BN13y_xl.js +0 -1
  458. ksadk/server/static/assets/pie-LRSECV5Y-ftSLXhhc.js +0 -1
  459. ksadk/server/static/assets/pieDiagram-4H26LBE5-GmmqNiVH.js +0 -30
  460. ksadk/server/static/assets/radar-GUYGQ44K-jaICkIeO.js +0 -1
  461. ksadk/server/static/assets/requirementDiagram-4Y6WPE33-DSWFqL7c.js +0 -84
  462. ksadk/server/static/assets/stateDiagram-AJRCARHV-qzTQpg3Y.js +0 -1
  463. ksadk/server/static/assets/stateDiagram-v2-BHNVJYJU-BYCoYGAN.js +0 -1
  464. ksadk/server/static/assets/treeView-BLDUP644-Cpib_vlz.js +0 -1
  465. ksadk/server/static/assets/treemap-LRROVOQU-DQxfmVHK.js +0 -1
  466. ksadk/server/static/assets/vennDiagram-CIIHVFJN-CMCixki2.js +0 -34
  467. ksadk/server/static/assets/wardley-L42UT6IY-BQYHnnkU.js +0 -1
  468. ksadk/server/static/assets/wardleyDiagram-YWT4CUSO-CVzB8F9K.js +0 -78
  469. ksadk/server/static/assets/xychartDiagram-2RQKCTM6-BFBqBLc1.js +0 -7
  470. ksadk/studio/evaluation.py +0 -192
  471. ksadk/studio/static/assets/index-CaEN0eTR.css +0 -1
  472. ksadk/studio/static/assets/index-DI2eY_NH.js +0 -252
  473. {agentengine_sdk_python-0.8.1.dist-info → agentengine_sdk_python-0.8.3.dist-info}/WHEEL +0 -0
  474. {agentengine_sdk_python-0.8.1.dist-info → agentengine_sdk_python-0.8.3.dist-info}/entry_points.txt +0 -0
  475. {agentengine_sdk_python-0.8.1.dist-info → agentengine_sdk_python-0.8.3.dist-info}/licenses/LICENSE +0 -0
  476. {agentengine_sdk_python-0.8.1.dist-info → agentengine_sdk_python-0.8.3.dist-info}/top_level.txt +0 -0
@@ -0,0 +1,797 @@
1
+ """RunnerRuntimeAdapter 的 dict-chunk 退化路径:runner 流竞速与 chunk→canonical 事件映射。
2
+
3
+ 从 ``ksadk.runtime.runner_adapter`` 按职责拆出(纯移动,行为不变)。以 mixin 形式
4
+ 被 :class:`RunnerRuntimeAdapter` 继承,依赖宿主提供 ``_active_runs`` /
5
+ ``_runtime_type`` / ``_runner`` / ``_next_seq`` / ``_canonical_kwargs`` /
6
+ ``_interaction_requested_from_approval`` / ``_coerce``。
7
+ """
8
+
9
+ from __future__ import annotations
10
+
11
+ import asyncio
12
+ import inspect
13
+ import json
14
+ import logging
15
+ import time
16
+ from collections.abc import Mapping
17
+ from contextlib import nullcontext
18
+ from typing import TYPE_CHECKING, Any, AsyncIterator, Optional, cast
19
+
20
+ from pydantic import JsonValue
21
+
22
+ from ksadk.conversations.runtime_input import _runner_name
23
+ from ksadk.conversations.runtime_observability import (
24
+ _set_conversation_input_attributes,
25
+ _set_conversation_output_attributes,
26
+ _set_conversation_span_attributes,
27
+ _set_conversation_usage_attributes,
28
+ )
29
+ from ksadk.events.canonical import (
30
+ ApprovalRequest,
31
+ ContentSnapshot,
32
+ ContinuationCreated,
33
+ ErrorInfo,
34
+ EventEnvelope,
35
+ InteractionRequested,
36
+ ItemCompleted,
37
+ ItemStarted,
38
+ ItemUpdated,
39
+ RunFailed,
40
+ RunProgress,
41
+ RuntimeEvent,
42
+ SourceRef,
43
+ UsageReported,
44
+ )
45
+ from ksadk.events.content import DataContent, TextContent, ToolCallContent, ToolResultContent
46
+ from ksadk.events.identity import stable_event_id, stable_item_id, stable_scope_id
47
+ from ksadk.runtime.adapter import RunHandle
48
+ from ksadk.runtime.preprocessing import PreparedRuntimeStart
49
+ from ksadk.runtime_context import platform_invocation_scope
50
+ from ksadk.tools.gateway import approval_interrupt_info_from_result
51
+
52
+ if TYPE_CHECKING:
53
+ from ksadk.runtime.runner_adapter import _ActiveRun
54
+
55
+ logger = logging.getLogger(__name__)
56
+
57
+ _STREAM_STOP = object()
58
+
59
+
60
+ async def _anext_or_stop(gen: AsyncIterator[Any]) -> Any:
61
+ """取下一个 chunk;流结束返回 _STREAM_STOP sentinel(便于竞速)。"""
62
+ try:
63
+ return await gen.__anext__()
64
+ except StopAsyncIteration:
65
+ return _STREAM_STOP
66
+
67
+
68
+ def _a2ui_surface_event(
69
+ self: Any,
70
+ handle: RunHandle,
71
+ chunk: Any,
72
+ ) -> tuple[RuntimeEvent, ...]:
73
+ """Recognize a validated A2UI tool envelope and emit a canonical data-item event.
74
+
75
+ The dynamic ``generate_a2ui`` tool returns official v0.9 operations as a
76
+ JSON tool result. Tool results are otherwise opaque to the runtime, which
77
+ would leave AG-UI with nothing to project until a page reload reconstructs
78
+ history. Convert exactly that envelope at the runtime boundary so it is
79
+ streamed, persisted, and replayed like every other A2UI surface.
80
+
81
+ In the canonical schema, A2UI surfaces are modeled as ``item_kind="data"``
82
+ items with ``source.protocol="a2ui"``.
83
+ """
84
+
85
+ if not isinstance(chunk, dict):
86
+ return ()
87
+ value = chunk.get("tool_output", chunk.get("output"))
88
+ if value is not None and hasattr(value, "content"):
89
+ value = value.content
90
+ if isinstance(value, str):
91
+ try:
92
+ value = json.loads(value)
93
+ except (TypeError, ValueError):
94
+ return ()
95
+ if not isinstance(value, Mapping):
96
+ return ()
97
+ operations_raw = value.get("a2ui_operations")
98
+ if not isinstance(operations_raw, list) or not operations_raw:
99
+ return ()
100
+ operations = [dict(operation) for operation in operations_raw if isinstance(operation, Mapping)]
101
+ if not operations:
102
+ return ()
103
+
104
+ known: list[tuple[str, str]] = [] # (surface_id, lifecycle)
105
+ for operation in operations:
106
+ for key, lifecycle in (
107
+ ("createSurface", "begin"),
108
+ ("updateComponents", "update"),
109
+ ("updateDataModel", "update"),
110
+ ("deleteSurface", "end"),
111
+ ):
112
+ detail = operation.get(key)
113
+ if isinstance(detail, Mapping) and isinstance(detail.get("surfaceId"), str):
114
+ surface_id = detail["surfaceId"].strip()
115
+ if surface_id:
116
+ known.append((surface_id, lifecycle))
117
+ break
118
+ if not known:
119
+ return ()
120
+ surface_ids = {surface_id for surface_id, _lifecycle in known}
121
+ if len(surface_ids) != 1:
122
+ logger.warning("ignoring A2UI tool result with multiple surfaces")
123
+ return ()
124
+ surface_id = known[0][0]
125
+ lifecycle = "begin" if any(lc == "begin" for _, lc in known) else known[0][1]
126
+
127
+ framework = self._runtime_type
128
+ run_id = handle.run_id
129
+ scope_id = stable_scope_id(framework, run_id)
130
+ # A dynamic A2UI tool result is a complete, immutable operation batch. It
131
+ # must therefore be represented by a closed canonical item. Reusing one
132
+ # long-lived item per surface leaves a create/update-only batch open at the
133
+ # run terminal and makes strict reducers reject the stream.
134
+ start_seq = self._next_seq()
135
+ batch_ref = str(chunk.get("call_id") or chunk.get("run_id") or chunk.get("id") or "")
136
+ if not batch_ref:
137
+ batch_ref = json.dumps(
138
+ operations,
139
+ ensure_ascii=False,
140
+ sort_keys=True,
141
+ separators=(",", ":"),
142
+ )
143
+ item_id = stable_item_id(framework, run_id, "a2ui-batch", surface_id, batch_ref)
144
+ source = SourceRef(
145
+ framework=framework,
146
+ protocol="a2ui",
147
+ native_run_id=run_id,
148
+ metadata={
149
+ "surface_id": surface_id,
150
+ "operation_batch": True,
151
+ "surface_lifecycle": lifecycle,
152
+ },
153
+ )
154
+ # TODO(runtime-event-v2): dict chunk 退化路径,chunk_ordinal 用 seq counter;
155
+ # LangGraph/Codex 切 stream_canonical_events 后清理
156
+ timestamp = time.time()
157
+ snapshot = ContentSnapshot(parts=(DataContent(part_id="a2ui-ops", data=operations),))
158
+ completed_seq = self._next_seq()
159
+ return (
160
+ ItemStarted(
161
+ schema_version=2,
162
+ event_id=stable_event_id(
163
+ framework, scope_id, item_id, "item.started", "a2ui", run_id, start_seq
164
+ ),
165
+ seq=start_seq,
166
+ timestamp=timestamp,
167
+ run_id=run_id,
168
+ scope_id=scope_id,
169
+ source=source,
170
+ item_id=item_id,
171
+ item_kind="data",
172
+ initial=snapshot,
173
+ ),
174
+ ItemCompleted(
175
+ schema_version=2,
176
+ event_id=stable_event_id(
177
+ framework, scope_id, item_id, "item.completed", "a2ui", run_id, completed_seq
178
+ ),
179
+ seq=completed_seq,
180
+ timestamp=timestamp,
181
+ run_id=run_id,
182
+ scope_id=scope_id,
183
+ source=source,
184
+ item_id=item_id,
185
+ item_kind="data",
186
+ snapshot=snapshot,
187
+ ),
188
+ )
189
+
190
+
191
+ class _RunnerStreamMappingMixin:
192
+ """``_map_runner_stream`` / ``_chunk_to_event`` 的实现载体(纯移动自 runner_adapter)。"""
193
+
194
+ async def _map_runner_stream(
195
+ self, handle: RunHandle, runner_input: dict
196
+ ) -> AsyncIterator[RuntimeEvent]:
197
+ run: Optional[_ActiveRun] = self._active_runs.get(handle.run_id) # type: ignore[attr-defined]
198
+ interrupt = run.interrupt_event if run is not None else None
199
+ prepared_start = run.__dict__.get("_prepared_start") if run is not None else None
200
+ invocation_context = (
201
+ prepared_start.context if isinstance(prepared_start, PreparedRuntimeStart) else None
202
+ )
203
+ scope = (
204
+ platform_invocation_scope(invocation_context)
205
+ if invocation_context is not None
206
+ else nullcontext()
207
+ )
208
+ runner_name = _runner_name(self._runner) # type: ignore[attr-defined]
209
+ accumulated_output = ""
210
+ usage: dict[str, Any] = {}
211
+ runner_gen: Optional[AsyncIterator[Any]] = None
212
+ # span scope 经 runner_adapter 模块属性间接解析,保持既有 monkeypatch
213
+ # patch 点(tests/agui/test_runtime_preprocessing.py)继续生效。
214
+ from ksadk.runtime import runner_adapter as _runner_adapter_module
215
+
216
+ async with _runner_adapter_module._conversation_span_scope(runner_name) as span:
217
+ if isinstance(prepared_start, PreparedRuntimeStart):
218
+ _set_conversation_span_attributes(
219
+ span,
220
+ agent_id=str(handle.native_ref.get("agent_id") or "agent"),
221
+ user_id=str(handle.native_ref.get("user_id") or "user"),
222
+ session_id=handle.session_id,
223
+ invocation_id=handle.run_id,
224
+ runner_name=runner_name,
225
+ model=prepared_start.context.model,
226
+ response_id=prepared_start.response_id,
227
+ )
228
+ _set_conversation_input_attributes(span, prepared_start.input_text)
229
+ try:
230
+ with scope:
231
+ canonical_stream = getattr(self._runner, "stream_canonical_events", None) # type: ignore[attr-defined]
232
+ # ToolGateway 语义续跑 runner(gateway approval 可能出现在终态
233
+ # tool result 之后)仍走 chunk 路径:approval 识别逻辑在
234
+ # _chunk_to_events 的 tool_result 分支,canonical 快速路径
235
+ # (stream_canonical_events)不覆盖该语义。
236
+ if getattr(
237
+ self._runner, "supports_gateway_approval_semantic_resume", False # type: ignore[attr-defined]
238
+ ):
239
+ canonical_stream = None
240
+ stream_result = (
241
+ canonical_stream(runner_input)
242
+ if callable(canonical_stream)
243
+ else self._runner.stream(runner_input) # type: ignore[attr-defined]
244
+ )
245
+ if inspect.iscoroutine(stream_result):
246
+ # runner.stream 若声明为 async def -> AsyncIterator(非 async generator),
247
+ # 调用返回 coroutine,需 await 得到迭代器。
248
+ stream_result = await stream_result
249
+ runner_gen = cast(AsyncIterator[Any], stream_result)
250
+ while True:
251
+ # 竞速:下一个 runner chunk vs cancel 中断事件。
252
+ chunk_task = asyncio.ensure_future(_anext_or_stop(runner_gen))
253
+ if run is not None:
254
+ run.chunk_task = chunk_task
255
+ wait_set = {chunk_task}
256
+ interrupt_task = (
257
+ asyncio.ensure_future(interrupt.wait())
258
+ if interrupt is not None
259
+ else None
260
+ )
261
+ if interrupt_task is not None:
262
+ wait_set.add(interrupt_task)
263
+ done, pending = await asyncio.wait(
264
+ wait_set, return_when=asyncio.FIRST_COMPLETED
265
+ )
266
+ for task in pending:
267
+ task.cancel()
268
+ if pending:
269
+ await asyncio.gather(*pending, return_exceptions=True)
270
+ if interrupt_task is not None and interrupt_task in done:
271
+ # cancel 中断:安全关闭 runner 流(同一 task)并停止。
272
+ chunk_task.cancel()
273
+ await asyncio.gather(chunk_task, return_exceptions=True)
274
+ return
275
+ try:
276
+ chunk = chunk_task.result()
277
+ except asyncio.CancelledError:
278
+ if interrupt is not None and interrupt.is_set():
279
+ return
280
+ raise
281
+ finally:
282
+ if run is not None:
283
+ run.chunk_task = None
284
+ if chunk is _STREAM_STOP:
285
+ return
286
+ if isinstance(chunk, EventEnvelope):
287
+ # canonical 事件(来自 stream_canonical_events):直接转发,
288
+ # 追踪 output/usage 供 span 属性。
289
+ if isinstance(chunk, ItemCompleted) and chunk.item_kind == "message":
290
+ accumulated_output = "".join(
291
+ part.text
292
+ for part in chunk.snapshot.parts
293
+ if isinstance(part, TextContent)
294
+ )
295
+ elif isinstance(chunk, UsageReported):
296
+ usage.update(
297
+ {
298
+ "input_tokens": chunk.input_tokens,
299
+ "output_tokens": chunk.output_tokens,
300
+ "total_tokens": chunk.total_tokens,
301
+ "cached_tokens": chunk.cached_tokens,
302
+ "reasoning_tokens": chunk.reasoning_tokens,
303
+ }
304
+ )
305
+ if isinstance(chunk, dict):
306
+ chunk_type = str(chunk.get("type") or "")
307
+ if chunk_type == "final" and run is not None:
308
+ for source_key, target_key in (
309
+ ("duration_ms", "duration_ms"),
310
+ ("started_at", "started_at"),
311
+ ("completed_at", "completed_at"),
312
+ ("metrics_source", "source"),
313
+ ):
314
+ if chunk.get(source_key) is not None:
315
+ run.completion_metrics[target_key] = chunk[source_key]
316
+ if chunk_type in {"final", "text", "text_delta"}:
317
+ text = self._coerce( # type: ignore[attr-defined]
318
+ chunk.get("delta") or chunk.get("output") or chunk.get("data")
319
+ )
320
+ if text:
321
+ if chunk_type == "final" or chunk.get("replace"):
322
+ accumulated_output = text
323
+ else:
324
+ accumulated_output += text
325
+ raw_usage = chunk.get("usage")
326
+ if isinstance(raw_usage, dict):
327
+ usage.update(raw_usage)
328
+ for event in self._chunk_to_event(handle, run, chunk): # type: ignore[attr-defined]
329
+ yield event
330
+ for a2ui_surface in _a2ui_surface_event(self, handle, chunk):
331
+ yield a2ui_surface
332
+ finally:
333
+ if accumulated_output:
334
+ _set_conversation_output_attributes(span, accumulated_output)
335
+ _set_conversation_usage_attributes(span, usage)
336
+ if runner_gen is not None:
337
+ aclose = getattr(runner_gen, "aclose", None)
338
+ if callable(aclose):
339
+ try:
340
+ await aclose()
341
+ except Exception: # noqa: BLE001
342
+ pass
343
+ if run is not None:
344
+ run.cancellation_ack.set()
345
+
346
+ def _chunk_to_event(
347
+ self, handle: RunHandle, run: Optional[_ActiveRun], chunk: Any
348
+ ) -> list[RuntimeEvent]:
349
+ if isinstance(chunk, EventEnvelope):
350
+ # canonical 事件(来自 stream_canonical_events):直接转发,
351
+ # 抑制 runner 自己的 run.started(adapter 已发自己的)。
352
+ if chunk.event_type == "run.started":
353
+ return []
354
+ return [chunk]
355
+ if not isinstance(chunk, dict):
356
+ chunk = {"type": "text", "delta": str(chunk)}
357
+
358
+ framework = self._runtime_type # type: ignore[attr-defined]
359
+ run_id = handle.run_id
360
+ scope_id = stable_scope_id(framework, run_id)
361
+ started = run.started_items if run is not None else set()
362
+
363
+ def ensure_started(
364
+ *,
365
+ item_id: str,
366
+ item_kind: str,
367
+ phase: str | None = None,
368
+ initial: ContentSnapshot | None = None,
369
+ ) -> list[RuntimeEvent]:
370
+ key = (scope_id, item_id)
371
+ if key in started:
372
+ return []
373
+ started.add(key)
374
+ return [
375
+ ItemStarted(
376
+ **self._canonical_kwargs( # type: ignore[attr-defined]
377
+ handle,
378
+ scope_id=scope_id,
379
+ item_id=item_id,
380
+ event_type="item.started",
381
+ part_id="item",
382
+ ),
383
+ item_id=item_id,
384
+ item_kind=item_kind,
385
+ phase=phase,
386
+ initial=initial,
387
+ )
388
+ ]
389
+
390
+ chunk_type = chunk.get("type")
391
+
392
+ # ---- reasoning ----
393
+ if chunk_type in ("reasoning", "reasoning_delta", "thinking"):
394
+ text = self._coerce( # type: ignore[attr-defined]
395
+ chunk.get("delta")
396
+ or chunk.get("content")
397
+ or chunk.get("output")
398
+ or chunk.get("data")
399
+ )
400
+ if not text:
401
+ return []
402
+ item_id = stable_item_id(framework, run_id, "reasoning")
403
+ events: list[RuntimeEvent] = ensure_started(
404
+ item_id=item_id, item_kind="reasoning", phase="commentary"
405
+ )
406
+ if chunk.get("status") in ("completed", "done"):
407
+ events.append(
408
+ ItemCompleted(
409
+ **self._canonical_kwargs( # type: ignore[attr-defined]
410
+ handle,
411
+ scope_id=scope_id,
412
+ item_id=item_id,
413
+ event_type="item.completed",
414
+ part_id="reasoning-text",
415
+ ),
416
+ item_id=item_id,
417
+ item_kind="reasoning",
418
+ snapshot=ContentSnapshot(
419
+ parts=(TextContent(part_id="reasoning-text", text=text),)
420
+ ),
421
+ )
422
+ )
423
+ else:
424
+ events.append(
425
+ ItemUpdated(
426
+ **self._canonical_kwargs( # type: ignore[attr-defined]
427
+ handle,
428
+ scope_id=scope_id,
429
+ item_id=item_id,
430
+ event_type="item.updated",
431
+ part_id="reasoning-text",
432
+ ),
433
+ item_id=item_id,
434
+ item_kind="reasoning",
435
+ op="append",
436
+ update=TextContent(part_id="reasoning-text", text=text),
437
+ )
438
+ )
439
+ return events
440
+
441
+ # ---- tool_call ----
442
+ if chunk_type in ("tool_call", "tool_start"):
443
+ call_id = str(
444
+ chunk.get("tool_call_id")
445
+ or chunk.get("call_id")
446
+ or chunk.get("run_id")
447
+ or chunk.get("id")
448
+ or ""
449
+ )
450
+ name = str(chunk.get("tool_name") or chunk.get("name") or "tool")
451
+ effective_call_id = call_id or name
452
+ item_id = stable_item_id(framework, run_id, effective_call_id, "tool_call")
453
+ part_id = "tool_call"
454
+ tc_content = ToolCallContent(
455
+ part_id=part_id,
456
+ call_id=effective_call_id,
457
+ name=name,
458
+ arguments=cast(JsonValue, chunk.get("tool_args", chunk.get("args")) or {}),
459
+ )
460
+ return [
461
+ ItemStarted(
462
+ **self._canonical_kwargs( # type: ignore[attr-defined]
463
+ handle,
464
+ scope_id=scope_id,
465
+ item_id=item_id,
466
+ event_type="item.started",
467
+ part_id=part_id,
468
+ ),
469
+ item_id=item_id,
470
+ item_kind="tool_call",
471
+ phase="commentary",
472
+ initial=ContentSnapshot(parts=(tc_content,)),
473
+ ),
474
+ ItemCompleted(
475
+ **self._canonical_kwargs( # type: ignore[attr-defined]
476
+ handle,
477
+ scope_id=scope_id,
478
+ item_id=item_id,
479
+ event_type="item.completed",
480
+ part_id=part_id,
481
+ ),
482
+ item_id=item_id,
483
+ item_kind="tool_call",
484
+ snapshot=ContentSnapshot(parts=(tc_content,)),
485
+ ),
486
+ ]
487
+
488
+ # ---- tool_result ----
489
+ if chunk_type in ("tool_result", "tool_end"):
490
+ call_id = str(
491
+ chunk.get("tool_call_id")
492
+ or chunk.get("call_id")
493
+ or chunk.get("run_id")
494
+ or chunk.get("id")
495
+ or ""
496
+ )
497
+ name = str(chunk.get("tool_name") or chunk.get("name") or "tool")
498
+ effective_call_id = call_id or name
499
+ item_id = stable_item_id(framework, run_id, effective_call_id, "tool_result")
500
+ part_id = "tool_result"
501
+ result_data = chunk.get("tool_output", chunk.get("output"))
502
+ # ToolGateway 审批可能出现在"本已终态"的 tool result 里;识别后转为
503
+ # canonical InteractionRequested(语义续跑由 runner 的
504
+ # supports_gateway_approval_semantic_resume 决定)。
505
+ tool_args = chunk.get("tool_args", chunk.get("args"))
506
+ approval_detail = approval_interrupt_info_from_result(
507
+ result_data,
508
+ fallback_tool_name=name,
509
+ tool_args=tool_args,
510
+ run_id=call_id or None,
511
+ )
512
+ if approval_detail is not None:
513
+ return self._interaction_requested_from_approval( # type: ignore[attr-defined]
514
+ handle,
515
+ run,
516
+ detail=approval_detail,
517
+ call_id=call_id,
518
+ )
519
+ tr_content = ToolResultContent(
520
+ part_id=part_id,
521
+ call_id=effective_call_id,
522
+ result=cast(JsonValue, result_data if result_data is not None else {}),
523
+ is_error=bool(chunk.get("error")),
524
+ )
525
+ return [
526
+ ItemStarted(
527
+ **self._canonical_kwargs( # type: ignore[attr-defined]
528
+ handle,
529
+ scope_id=scope_id,
530
+ item_id=item_id,
531
+ event_type="item.started",
532
+ part_id=part_id,
533
+ ),
534
+ item_id=item_id,
535
+ item_kind="tool_result",
536
+ phase="commentary",
537
+ ),
538
+ ItemCompleted(
539
+ **self._canonical_kwargs( # type: ignore[attr-defined]
540
+ handle,
541
+ scope_id=scope_id,
542
+ item_id=item_id,
543
+ event_type="item.completed",
544
+ part_id=part_id,
545
+ ),
546
+ item_id=item_id,
547
+ item_kind="tool_result",
548
+ snapshot=ContentSnapshot(parts=(tr_content,)),
549
+ ),
550
+ ]
551
+
552
+ # ---- interrupt / approval ----
553
+ if chunk_type in ("interrupt", "approval", "approval_required"):
554
+ detail = chunk.get("interrupt_info") or chunk.get("detail") or {}
555
+ detail_id = detail.get("approval_request_id") if isinstance(detail, dict) else None
556
+ call_id = str(
557
+ chunk.get("call_id")
558
+ or chunk.get("approval_id")
559
+ or chunk.get("id")
560
+ or detail_id
561
+ or ""
562
+ )
563
+ if run is not None and call_id:
564
+ run.pending_approvals.add(call_id)
565
+ if call_id:
566
+ pending_approval_ids = handle.native_ref.setdefault("pending_approval_ids", [])
567
+ if call_id not in pending_approval_ids:
568
+ pending_approval_ids.append(call_id)
569
+ interaction_id = call_id or stable_item_id(framework, run_id, "interaction")
570
+ item_id = stable_item_id(framework, run_id, "interaction")
571
+ detail_value: JsonValue = (
572
+ cast(JsonValue, detail)
573
+ if isinstance(detail, (dict, list, str, int, float, bool, type(None)))
574
+ else None
575
+ )
576
+ return [
577
+ InteractionRequested(
578
+ **self._canonical_kwargs( # type: ignore[attr-defined]
579
+ handle,
580
+ scope_id=scope_id,
581
+ item_id=item_id,
582
+ event_type="interaction.requested",
583
+ part_id="interaction",
584
+ ),
585
+ interaction_id=interaction_id,
586
+ interaction_kind="approval",
587
+ request=ApprovalRequest(
588
+ call_id=call_id or None,
589
+ kind="tool",
590
+ detail=detail_value,
591
+ ),
592
+ )
593
+ ]
594
+
595
+ # ---- checkpoint ----
596
+ if chunk_type == "checkpoint":
597
+ raw_metadata = chunk.get("metadata")
598
+ metadata: dict[str, Any] = raw_metadata if isinstance(raw_metadata, dict) else {}
599
+ raw_agentengine = metadata.get("agentengine")
600
+ agentengine: dict[str, Any] = (
601
+ raw_agentengine if isinstance(raw_agentengine, dict) else {}
602
+ )
603
+ ckpt_framework = str(agentengine.get("framework") or self._runtime_type) # type: ignore[attr-defined]
604
+ framework_ref = agentengine.get("framework_ref") or {}
605
+ runtime_ref = (
606
+ framework_ref.get(ckpt_framework) if isinstance(framework_ref, dict) else {}
607
+ ) or {}
608
+ checkpoint_id = str(
609
+ runtime_ref.get("checkpoint_id") if isinstance(runtime_ref, dict) else ""
610
+ )
611
+ if not checkpoint_id:
612
+ return []
613
+ handle.native_ref["checkpoint_id"] = checkpoint_id
614
+ known_checkpoint_ids = handle.native_ref.setdefault("known_checkpoint_ids", [])
615
+ if checkpoint_id not in known_checkpoint_ids:
616
+ known_checkpoint_ids.append(checkpoint_id)
617
+ handle.native_ref["framework_ref"] = framework_ref
618
+ if isinstance(runtime_ref, dict):
619
+ handle.native_ref.update(runtime_ref)
620
+ item_id = stable_item_id(framework, run_id, "$run")
621
+ ref_value = cast(
622
+ JsonValue,
623
+ framework_ref if isinstance(framework_ref, dict) else {},
624
+ )
625
+ return [
626
+ ContinuationCreated(
627
+ **self._canonical_kwargs( # type: ignore[attr-defined]
628
+ handle,
629
+ scope_id=scope_id,
630
+ item_id=item_id,
631
+ event_type="continuation.created",
632
+ part_id="continuation",
633
+ ),
634
+ continuation_id=checkpoint_id,
635
+ continuation_kind="graph_checkpoint",
636
+ resumable=True,
637
+ ref={
638
+ "framework": ckpt_framework,
639
+ "framework_ref": ref_value,
640
+ "resume_target": ref_value,
641
+ },
642
+ )
643
+ ]
644
+
645
+ # ---- graph_update ----
646
+ if chunk_type == "graph_update":
647
+ item_id = stable_item_id(framework, run_id, "$run")
648
+ return [
649
+ RunProgress(
650
+ **self._canonical_kwargs( # type: ignore[attr-defined]
651
+ handle,
652
+ scope_id=scope_id,
653
+ item_id=item_id,
654
+ event_type="run.progress",
655
+ part_id="run",
656
+ ),
657
+ status="running",
658
+ message=str(chunk.get("node") or ""),
659
+ )
660
+ ]
661
+
662
+ # ---- usage ----
663
+ if chunk_type == "usage":
664
+ raw_usage = chunk.get("usage")
665
+ usage_dict: dict[str, Any] = raw_usage if isinstance(raw_usage, dict) else {}
666
+ item_id = stable_item_id(framework, run_id, "$run")
667
+ return [
668
+ UsageReported(
669
+ **self._canonical_kwargs( # type: ignore[attr-defined]
670
+ handle,
671
+ scope_id=scope_id,
672
+ item_id=item_id,
673
+ event_type="usage.reported",
674
+ part_id="usage",
675
+ ),
676
+ input_tokens=int(usage_dict.get("input_tokens") or 0),
677
+ output_tokens=int(usage_dict.get("output_tokens") or 0),
678
+ total_tokens=int(usage_dict.get("total_tokens") or 0),
679
+ cached_tokens=int(usage_dict.get("cached_tokens") or 0),
680
+ reasoning_tokens=int(usage_dict.get("reasoning_tokens") or 0),
681
+ )
682
+ ]
683
+
684
+ # ---- error ----
685
+ if chunk_type == "error":
686
+ error = self._coerce(chunk.get("message") or chunk.get("error")) # type: ignore[attr-defined]
687
+ item_id = stable_item_id(framework, run_id, "$run")
688
+ return [
689
+ RunFailed(
690
+ **self._canonical_kwargs( # type: ignore[attr-defined]
691
+ handle,
692
+ scope_id=scope_id,
693
+ item_id=item_id,
694
+ event_type="run.failed",
695
+ part_id="run",
696
+ ),
697
+ status="failed",
698
+ error=ErrorInfo(
699
+ code="runner_failed",
700
+ message=error or "runner failed",
701
+ source=framework,
702
+ scope_id=scope_id,
703
+ ),
704
+ )
705
+ ]
706
+
707
+ # ---- final ----
708
+ if chunk_type == "final":
709
+ output = self._coerce(chunk.get("output")) # type: ignore[attr-defined]
710
+ item_id = stable_item_id(framework, run_id, "message", "final_answer")
711
+ if run is not None:
712
+ run.final_answer_item_id = item_id
713
+ text_content = TextContent(part_id="text-0", text=output)
714
+ # Auto-close any open explicit commentary/reasoning item before completing
715
+ # final_answer. Text deltas already belong to the final-answer item and must
716
+ # keep that identity through completion.
717
+ # Commentary/thinking deltas create items that are never ItemCompleted;
718
+ # without this, RunCompleted fails _ensure_no_open_items.
719
+ #
720
+ # Close them *before* allocating the final-answer item. Event
721
+ # constructors allocate ``seq`` eagerly, so inserting a later
722
+ # completion at index zero would otherwise return an event list
723
+ # whose physical order disagrees with its sequence numbers.
724
+ events: list[RuntimeEvent] = []
725
+ for close_kind, close_part_id, close_components in (
726
+ ("message", "text-0", ("message", "commentary")),
727
+ ("reasoning", "reasoning-text", ("reasoning",)),
728
+ ):
729
+ close_item_id = stable_item_id(framework, run_id, *close_components)
730
+ close_key = (scope_id, close_item_id)
731
+ if close_key in started:
732
+ started.discard(close_key)
733
+ events.append(
734
+ ItemCompleted(
735
+ **self._canonical_kwargs( # type: ignore[attr-defined]
736
+ handle,
737
+ scope_id=scope_id,
738
+ item_id=close_item_id,
739
+ event_type="item.completed",
740
+ part_id=close_part_id,
741
+ ),
742
+ item_id=close_item_id,
743
+ item_kind=close_kind,
744
+ snapshot=ContentSnapshot(
745
+ parts=(TextContent(part_id=close_part_id, text=""),)
746
+ ),
747
+ ),
748
+ )
749
+ events.extend(
750
+ ensure_started(item_id=item_id, item_kind="message", phase="final_answer")
751
+ )
752
+ events.append(
753
+ ItemCompleted(
754
+ **self._canonical_kwargs( # type: ignore[attr-defined]
755
+ handle,
756
+ scope_id=scope_id,
757
+ item_id=item_id,
758
+ event_type="item.completed",
759
+ part_id="text-0",
760
+ ),
761
+ item_id=item_id,
762
+ item_kind="message",
763
+ snapshot=ContentSnapshot(parts=(text_content,)),
764
+ )
765
+ )
766
+ return events
767
+
768
+ # ---- default: text delta ----
769
+ text = self._coerce(chunk.get("delta") or chunk.get("output") or chunk.get("data")) # type: ignore[attr-defined]
770
+ if not text:
771
+ return []
772
+ is_commentary = chunk_type in {"commentary", "commentary_delta"}
773
+ phase = "commentary" if is_commentary else "final_answer"
774
+ item_id = stable_item_id(framework, run_id, "message", phase)
775
+ if run is not None and not is_commentary:
776
+ run.final_answer_item_id = item_id
777
+ op: str = "replace" if chunk.get("replace") else "append"
778
+ events = ensure_started(item_id=item_id, item_kind="message", phase=phase)
779
+ events.append(
780
+ ItemUpdated(
781
+ **self._canonical_kwargs( # type: ignore[attr-defined]
782
+ handle,
783
+ scope_id=scope_id,
784
+ item_id=item_id,
785
+ event_type="item.updated",
786
+ part_id="text-0",
787
+ ),
788
+ item_id=item_id,
789
+ item_kind="message",
790
+ op=op,
791
+ update=TextContent(part_id="text-0", text=text),
792
+ )
793
+ )
794
+ return events
795
+
796
+
797
+ __all__ = ["_RunnerStreamMappingMixin", "_a2ui_surface_event", "_anext_or_stop", "_STREAM_STOP"]