@kingsoftcloud/ksadk-web 0.3.12 → 0.3.14

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 (360) hide show
  1. package/CHANGELOG.md +37 -0
  2. package/README.md +7 -4
  3. package/RELEASE_PROVENANCE.json +2 -2
  4. package/dist-ksadk/a2ui/basic-v0_9_1.json +1383 -0
  5. package/dist-ksadk/a2ui/common-types-v0_9.json +305 -0
  6. package/dist-ksadk/a2ui/legacy-seven-components.json +230 -0
  7. package/dist-ksadk/a2ui/manifest.json +86 -0
  8. package/dist-ksadk/a2ui/material-icons-OFL-1.1.txt +93 -0
  9. package/dist-ksadk/assets/{ArtifactsPanel-C1YoaKhx.js → ArtifactsPanel-D6LwZ4tH.js} +1 -1
  10. package/dist-ksadk/assets/{CodeBlock-DHuAWfr5.js → CodeBlock-CVzuVnaN.js} +6 -6
  11. package/dist-ksadk/assets/{MathMessageMarkdown-cvdrBwYr.js → MathMessageMarkdown-BV0Wpi0w.js} +7 -7
  12. package/dist-ksadk/assets/{MermaidBlock-DeBkbJvI.js → MermaidBlock-CnBUrMcm.js} +9 -9
  13. package/dist-ksadk/assets/NativeTerminalPanel-JqWiX3NG.js +1 -0
  14. package/dist-ksadk/assets/OfficialA2uiRenderer-jre8Zxwn.js +16 -0
  15. package/dist-ksadk/assets/{abnfDiagram-VCTEODGH-Dhfv25o2.js → abnfDiagram-VCTEODGH-5klLGLXu.js} +1 -1
  16. package/dist-ksadk/assets/{arc-C35w-pn1.js → arc-BZZ76X9T.js} +1 -1
  17. package/dist-ksadk/assets/architecture-7GRP2DOG-HtHCKaib.js +1 -0
  18. package/dist-ksadk/assets/{architectureDiagram-5GKGNRK7-FoMY_u6D.js → architectureDiagram-5GKGNRK7-Z8sis19K.js} +1 -1
  19. package/dist-ksadk/assets/{blockDiagram-NRAW4CY4-zD4Rt7oE.js → blockDiagram-NRAW4CY4-CbvtJUlp.js} +1 -1
  20. package/dist-ksadk/assets/{c4Diagram-UCG6FXSJ-BCtGGkhN.js → c4Diagram-UCG6FXSJ-DRDFUFRO.js} +1 -1
  21. package/dist-ksadk/assets/channel-kuCeovvA.js +1 -0
  22. package/dist-ksadk/assets/chunk-2Q5K7J3B-DW3fGlBY.js +1 -0
  23. package/dist-ksadk/assets/{chunk-5VM5RSS4-C2Ms13_k.js → chunk-5VM5RSS4-DgtggYEo.js} +1 -1
  24. package/dist-ksadk/assets/{chunk-F27PBJKO-BoTj-qgx.js → chunk-F27PBJKO-DBbGWzCA.js} +1 -1
  25. package/dist-ksadk/assets/{chunk-G27WJ6UU-DOMyyjHH.js → chunk-G27WJ6UU-DN3d7UAB.js} +1 -1
  26. package/dist-ksadk/assets/{chunk-JWPE2WC7-DbJJqDli.js → chunk-JWPE2WC7-Bx1EEStb.js} +1 -1
  27. package/dist-ksadk/assets/{chunk-LCL6LL3I-CLuRCt_W.js → chunk-LCL6LL3I-WtVZVwRo.js} +1 -1
  28. package/dist-ksadk/assets/{chunk-POPQ4Y6H-D0Dz847e.js → chunk-POPQ4Y6H-Bws5AdxJ.js} +1 -1
  29. package/dist-ksadk/assets/{chunk-RHFEMEQ7-BWSRzEcd.js → chunk-RHFEMEQ7-XkUQsrvs.js} +1 -1
  30. package/dist-ksadk/assets/{chunk-SVP7TREG-CPoHqkCY.js → chunk-SVP7TREG-Bsl7M9AM.js} +1 -1
  31. package/dist-ksadk/assets/{chunk-XXDRQBXY-Bx1jS-X-.js → chunk-XXDRQBXY-CgtLH0fF.js} +1 -1
  32. package/dist-ksadk/assets/classDiagram-DTDB5LWJ-2ASNdnzY.js +1 -0
  33. package/dist-ksadk/assets/classDiagram-v2-JRS7N3AN-2ASNdnzY.js +1 -0
  34. package/dist-ksadk/assets/{cose-bilkent-JH36ORCC-LzyWm4m0.js → cose-bilkent-JH36ORCC-CeWoENhZ.js} +1 -1
  35. package/dist-ksadk/assets/cynefin-OW5HDTMX-1bdXRZZW.js +1 -0
  36. package/dist-ksadk/assets/{cynefinDiagram-5FMLGOSQ-BlW_bHED.js → cynefinDiagram-5FMLGOSQ-20tItmQc.js} +1 -1
  37. package/dist-ksadk/assets/{dagre-3AP2YEHR-Bu_S86_i.js → dagre-3AP2YEHR-Dbq7kvPA.js} +1 -1
  38. package/dist-ksadk/assets/{dagre-B7nCawed.js → dagre-CDn7V_FV.js} +1 -1
  39. package/dist-ksadk/assets/{diagram-S7CK7UJ4-W_Zow2Km.js → diagram-S7CK7UJ4-BZmC9O9H.js} +1 -1
  40. package/dist-ksadk/assets/{diagram-UQ7AKVKN-CLIKAHON.js → diagram-UQ7AKVKN-Du6_H3qW.js} +1 -1
  41. package/dist-ksadk/assets/{diagram-VSXAHHWV-Dv6_QAlN.js → diagram-VSXAHHWV-C-yzAkOw.js} +1 -1
  42. package/dist-ksadk/assets/{diagram-VX7I27RA-BWAWRDtZ.js → diagram-VX7I27RA-D9uvDFOT.js} +1 -1
  43. package/dist-ksadk/assets/{diagram-Z3DM3KII-XA_rWMss.js → diagram-Z3DM3KII-CU7oDxrX.js} +1 -1
  44. package/dist-ksadk/assets/{dist-BO_UwatH.js → dist--hkDI36a.js} +1 -1
  45. package/dist-ksadk/assets/{dist-BLpRy_Yx.js → dist-A93qMRIG.js} +1 -1
  46. package/dist-ksadk/assets/{dist-2vL2sAdF.js → dist-BAQdZ6nL.js} +1 -1
  47. package/dist-ksadk/assets/{dist-0X_j2Rcc.js → dist-BMr3B20m.js} +1 -1
  48. package/dist-ksadk/assets/{dist-M-SrdhhZ.js → dist-BzJ7YZSp.js} +1 -1
  49. package/dist-ksadk/assets/{dist-CPyieXEf.js → dist-C27hii8p.js} +1 -1
  50. package/dist-ksadk/assets/{dist-CIRMvUM4.js → dist-C6_6Bds1.js} +1 -1
  51. package/dist-ksadk/assets/{dist-D-92S-Fd.js → dist-CCsMtFWS.js} +1 -1
  52. package/dist-ksadk/assets/{dist-B3bA05-e.js → dist-CJWOBYYA.js} +1 -1
  53. package/dist-ksadk/assets/{dist-DyG6Ya-z.js → dist-CKdx7EBd.js} +1 -1
  54. package/dist-ksadk/assets/{dist-t6MzE0jU.js → dist-CYZpFnLj.js} +1 -1
  55. package/dist-ksadk/assets/{dist-C0J5_3qM.js → dist-CicTOm8l.js} +1 -1
  56. package/dist-ksadk/assets/{dist-DppE_r-D.js → dist-D5UMz8J2.js} +1 -1
  57. package/dist-ksadk/assets/{dist-1nXCdsej.js → dist-DGF16E1K.js} +1 -1
  58. package/dist-ksadk/assets/{dist-mqD5TZup.js → dist-DIDBisGY.js} +1 -1
  59. package/dist-ksadk/assets/{dist-qe8xjesy.js → dist-DLUtqcff.js} +1 -1
  60. package/dist-ksadk/assets/{dist-CGt_UGeO.js → dist-DNKlYhx0.js} +1 -1
  61. package/dist-ksadk/assets/{dist-DNKuMzWc.js → dist-DXQ4VAxh.js} +1 -1
  62. package/dist-ksadk/assets/{dist-n3n6t_FO.js → dist-DlTRB6vC.js} +1 -1
  63. package/dist-ksadk/assets/{dist-BdNFAney.js → dist-KZg0gTHo.js} +1 -1
  64. package/dist-ksadk/assets/{dist-DWfdmz9s.js → dist-di2puApf.js} +1 -1
  65. package/dist-ksadk/assets/{dist-CIpWk_5f.js → dist-raxPVjNv.js} +1 -1
  66. package/dist-ksadk/assets/{ebnfDiagram-PWID7BFC-BfCzF1Xi.js → ebnfDiagram-PWID7BFC-DuZG2dXs.js} +1 -1
  67. package/dist-ksadk/assets/{erDiagram-SSCWMZ5O-Dpz9_0zn.js → erDiagram-SSCWMZ5O-BL8xNTj7.js} +1 -1
  68. package/dist-ksadk/assets/eventmodeling-NTZA5JFV-CJmDiSBW.js +1 -0
  69. package/dist-ksadk/assets/flowDiagram-A5DVABFB-CQeF2mVM.js +1 -0
  70. package/dist-ksadk/assets/{ganttDiagram-EL5Y4UJY-DDaG1f4_.js → ganttDiagram-EL5Y4UJY-CrlgL4uW.js} +1 -1
  71. package/dist-ksadk/assets/gitGraph-4MIJSDKK-nZDWMnv0.js +1 -0
  72. package/dist-ksadk/assets/{gitGraphDiagram-WWUBYQGX-DcxzRwYC.js → gitGraphDiagram-WWUBYQGX-rSzorCYl.js} +1 -1
  73. package/dist-ksadk/assets/index-Bdrdzuun.js +1132 -0
  74. package/dist-ksadk/assets/index-DMqhW2cV.css +2 -0
  75. package/dist-ksadk/assets/info-A6RAGUB7-De1Iwui5.js +1 -0
  76. package/dist-ksadk/assets/{infoDiagram-RXCK75RN-CYMdn6lQ.js → infoDiagram-RXCK75RN-CAt8qVFg.js} +1 -1
  77. package/dist-ksadk/assets/{ishikawaDiagram-5VMMS53U-CLHFAD6c.js → ishikawaDiagram-5VMMS53U-VhqJKMYj.js} +1 -1
  78. package/dist-ksadk/assets/{journeyDiagram-EYS64GPL-BHSKQGAl.js → journeyDiagram-EYS64GPL-Bhfu6YF7.js} +1 -1
  79. package/dist-ksadk/assets/{kanban-definition-3QL26DDD-D5W3tjTT.js → kanban-definition-3QL26DDD-7OI9pMVn.js} +1 -1
  80. package/dist-ksadk/assets/{katex-mtjOi4KM.js → katex-Dg-M8r12.js} +1 -1
  81. package/dist-ksadk/assets/{linear-BETqsxE_.js → linear-NMml7t9_.js} +1 -1
  82. package/dist-ksadk/assets/material-icons-latin-400-normal-kAwBdRge.woff2 +0 -0
  83. package/dist-ksadk/assets/{mermaid-parser.core-DPWrvs90.js → mermaid-parser.core-BEmZbnyI.js} +2 -2
  84. package/dist-ksadk/assets/{mindmap-definition-FBJOCRG2-CJG2mfBP.js → mindmap-definition-FBJOCRG2-B0xwWHMG.js} +1 -1
  85. package/dist-ksadk/assets/packet-AYTQ26CC-D1e-X1k6.js +1 -0
  86. package/dist-ksadk/assets/{pegDiagram-XKGWAZYB-GjAPNaqn.js → pegDiagram-XKGWAZYB-Cu7QgjWh.js} +1 -1
  87. package/dist-ksadk/assets/pie-WAS4IAKB-Lxivx4QM.js +1 -0
  88. package/dist-ksadk/assets/{pieDiagram-E7YTZNPT-ToIewrqr.js → pieDiagram-E7YTZNPT-DkoIrDma.js} +1 -1
  89. package/dist-ksadk/assets/{quadrantDiagram-AXDQQJYC-4d78KiEY.js → quadrantDiagram-AXDQQJYC-BNlrjzzI.js} +1 -1
  90. package/dist-ksadk/assets/radar-RG4KPBEZ-BQSVqVrb.js +1 -0
  91. package/dist-ksadk/assets/railroad-74A4TZTK-Cbnh6XZs.js +1 -0
  92. package/dist-ksadk/assets/railroad-abnf-HS5TGJTU-2ECgbBti.js +1 -0
  93. package/dist-ksadk/assets/railroad-ebnf-LZEXJU2U-BfFn9JLY.js +1 -0
  94. package/dist-ksadk/assets/railroad-peg-WCYAUIDC-DyICMM5z.js +1 -0
  95. package/dist-ksadk/assets/{railroadDiagram-O6MQD6OU-DKzx4xfv.js → railroadDiagram-O6MQD6OU-BK0bi_rd.js} +1 -1
  96. package/dist-ksadk/assets/{requirementDiagram-EFPCY7ZU-CJYljxQS.js → requirementDiagram-EFPCY7ZU-B7OeRQPt.js} +1 -1
  97. package/dist-ksadk/assets/{sankeyDiagram-P5KCCOFB-DLU2fV1w.js → sankeyDiagram-P5KCCOFB-dE-_Jk7C.js} +1 -1
  98. package/dist-ksadk/assets/{sequenceDiagram-WJ2MYXX4-CVqysrA9.js → sequenceDiagram-WJ2MYXX4-y0THlktZ.js} +1 -1
  99. package/dist-ksadk/assets/{sizeCapture-X5ZJPWSS-BUxfmTHc.js → sizeCapture-X5ZJPWSS-CROa4oek.js} +1 -1
  100. package/dist-ksadk/assets/{stateDiagram-HBIQ2CUA-CNFgYI9E.js → stateDiagram-HBIQ2CUA-Bz1m4ALw.js} +1 -1
  101. package/dist-ksadk/assets/stateDiagram-v2-4QOOHH4V-D1bq-RBG.js +1 -0
  102. package/dist-ksadk/assets/{swimlanes-XN3QIQJK-B6qCMe8N.js → swimlanes-XN3QIQJK-CvTFLiud.js} +1 -1
  103. package/dist-ksadk/assets/swimlanesDiagram-VK2B7HYN-BoqEqfqH.js +8 -0
  104. package/dist-ksadk/assets/{timeline-definition-24CTP7MA-CMD3x_U_.js → timeline-definition-24CTP7MA-1uvXxAPl.js} +1 -1
  105. package/dist-ksadk/assets/treeView-Q6P3EWNA-BS4HVbiU.js +1 -0
  106. package/dist-ksadk/assets/treemap-WGGIJYW6-D9_U1F01.js +1 -0
  107. package/dist-ksadk/assets/{vennDiagram-4TSXK5OY-BqtUqvXM.js → vennDiagram-4TSXK5OY-ruwLjPyJ.js} +1 -1
  108. package/dist-ksadk/assets/wardley-WFR3VGLG-DAiztg_B.js +1 -0
  109. package/dist-ksadk/assets/{wardleyDiagram-VM6X3IG4-B5KfDjI-.js → wardleyDiagram-VM6X3IG4-DlwZFHFD.js} +1 -1
  110. package/dist-ksadk/assets/{xychartDiagram-S5SC5T6Z-BaUlSZrn.js → xychartDiagram-S5SC5T6Z-pouF3HpF.js} +1 -1
  111. package/dist-ksadk/index.html +2 -2
  112. package/dist-lib/App.d.ts +2 -0
  113. package/dist-lib/App.d.ts.map +1 -1
  114. package/dist-lib/AttachmentPreview-C5ruk44B.js +1545 -0
  115. package/dist-lib/{ConnectedComposer-CeWPgOn2.js → ConnectedComposer-D6eV5hd8.js} +217 -216
  116. package/dist-lib/ConnectedMessageList-YRps8tha.js +1616 -0
  117. package/dist-lib/OfficialA2uiRenderer-DI49Kpxu.js +8 -0
  118. package/dist-lib/{WorkspacePanelContainer-B2mk7udj.js → WorkspacePanelContainer-cAypD1ss.js} +5 -5
  119. package/dist-lib/a2ui/basic-v0_9_1.json +1383 -0
  120. package/dist-lib/a2ui/common-types-v0_9.json +305 -0
  121. package/dist-lib/a2ui/legacy-seven-components.json +230 -0
  122. package/dist-lib/a2ui/manifest.json +86 -0
  123. package/dist-lib/a2ui/material-icons-OFL-1.1.txt +93 -0
  124. package/dist-lib/a2ui-DVGulxKb.js +1547 -0
  125. package/dist-lib/api/client.d.ts.map +1 -1
  126. package/dist-lib/api/messages.d.ts +89 -0
  127. package/dist-lib/api/messages.d.ts.map +1 -0
  128. package/dist-lib/{capabilities-CXle1R-K.js → capabilities-DAeBY-WO.js} +3 -2
  129. package/dist-lib/capabilities.js +1 -1
  130. package/dist-lib/chat-composer.js +2 -2
  131. package/dist-lib/chat-timeline.js +5 -4
  132. package/dist-lib/{cloudMaterials-BlIAT-PA.js → cloudMaterials-D75jTrkQ.js} +823 -628
  133. package/dist-lib/components/chat/A2UIActivityMessage.d.ts +10 -3
  134. package/dist-lib/components/chat/A2UIActivityMessage.d.ts.map +1 -1
  135. package/dist-lib/components/chat/AgentBlockView.d.ts +9 -0
  136. package/dist-lib/components/chat/AgentBlockView.d.ts.map +1 -0
  137. package/dist-lib/components/chat/ChatComposer.d.ts.map +1 -1
  138. package/dist-lib/components/chat/ChatMessageList.d.ts +9 -3
  139. package/dist-lib/components/chat/ChatMessageList.d.ts.map +1 -1
  140. package/dist-lib/components/chat/ConnectedComposer.d.ts +1 -1
  141. package/dist-lib/components/chat/ConnectedComposer.d.ts.map +1 -1
  142. package/dist-lib/components/chat/ConnectedMessageList.d.ts +5 -2
  143. package/dist-lib/components/chat/ConnectedMessageList.d.ts.map +1 -1
  144. package/dist-lib/components/chat/InteractionA2uiSurface.d.ts +2 -1
  145. package/dist-lib/components/chat/InteractionA2uiSurface.d.ts.map +1 -1
  146. package/dist-lib/components/chat/InteractionHistoryAnchor.d.ts.map +1 -1
  147. package/dist-lib/components/chat/InteractionSchemaForm.d.ts.map +1 -1
  148. package/dist-lib/components/chat/InteractionTray.d.ts +2 -1
  149. package/dist-lib/components/chat/InteractionTray.d.ts.map +1 -1
  150. package/dist-lib/components/chat/JsonSchemaInputForm.d.ts +16 -0
  151. package/dist-lib/components/chat/JsonSchemaInputForm.d.ts.map +1 -0
  152. package/dist-lib/components/chat/LegacyA2UISurface.d.ts +14 -0
  153. package/dist-lib/components/chat/LegacyA2UISurface.d.ts.map +1 -0
  154. package/dist-lib/components/chat/OfficialA2uiRenderer.d.ts +3 -0
  155. package/dist-lib/components/chat/OfficialA2uiRenderer.d.ts.map +1 -0
  156. package/dist-lib/components/chat/ProcessingBlocksView.d.ts +6 -2
  157. package/dist-lib/components/chat/ProcessingBlocksView.d.ts.map +1 -1
  158. package/dist-lib/components/chat/RunProgressIndicator.d.ts +4 -0
  159. package/dist-lib/components/chat/RunProgressIndicator.d.ts.map +1 -0
  160. package/dist-lib/components/chat/StatusBanner.d.ts.map +1 -1
  161. package/dist-lib/components/chat/a2ui-response-snapshot.d.ts +25 -0
  162. package/dist-lib/components/chat/a2ui-response-snapshot.d.ts.map +1 -0
  163. package/dist-lib/components/chat/approval-status.d.ts +7 -0
  164. package/dist-lib/components/chat/approval-status.d.ts.map +1 -0
  165. package/dist-lib/components/chat/submitted-interaction-history.d.ts +16 -0
  166. package/dist-lib/components/chat/submitted-interaction-history.d.ts.map +1 -0
  167. package/dist-lib/components/chat/types.d.ts +30 -1
  168. package/dist-lib/components/chat/types.d.ts.map +1 -1
  169. package/dist-lib/components/teams/CloudTeamWorkspace.d.ts.map +1 -1
  170. package/dist-lib/components/teams/TeamWorkspace.d.ts.map +1 -1
  171. package/dist-lib/components.js +34 -34
  172. package/dist-lib/conversation.js +8 -34
  173. package/dist-lib/core/a2ui/catalog.d.ts +250 -0
  174. package/dist-lib/core/a2ui/catalog.d.ts.map +1 -0
  175. package/dist-lib/core/a2ui/history.d.ts +13 -0
  176. package/dist-lib/core/a2ui/history.d.ts.map +1 -0
  177. package/dist-lib/core/a2ui/linking.d.ts +7 -0
  178. package/dist-lib/core/a2ui/linking.d.ts.map +1 -0
  179. package/dist-lib/core/a2ui/provider.d.ts +8 -0
  180. package/dist-lib/core/a2ui/provider.d.ts.map +1 -0
  181. package/dist-lib/core/a2ui/registry.d.ts +45 -0
  182. package/dist-lib/core/a2ui/registry.d.ts.map +1 -0
  183. package/dist-lib/core/a2ui/snapshot.d.ts +4 -0
  184. package/dist-lib/core/a2ui/snapshot.d.ts.map +1 -0
  185. package/dist-lib/core/a2ui/surfaces.d.ts +23 -0
  186. package/dist-lib/core/a2ui/surfaces.d.ts.map +1 -0
  187. package/dist-lib/core/a2ui/transcript.d.ts +14 -0
  188. package/dist-lib/core/a2ui/transcript.d.ts.map +1 -0
  189. package/dist-lib/core/api/facade.d.ts +25 -11
  190. package/dist-lib/core/api/facade.d.ts.map +1 -1
  191. package/dist-lib/core/api/types.d.ts +35 -4
  192. package/dist-lib/core/api/types.d.ts.map +1 -1
  193. package/dist-lib/core/conversation/agent.d.ts +49 -0
  194. package/dist-lib/core/conversation/agent.d.ts.map +1 -0
  195. package/dist-lib/core/conversation/client.d.ts +2 -0
  196. package/dist-lib/core/conversation/client.d.ts.map +1 -1
  197. package/dist-lib/core/conversation/contracts.d.ts.map +1 -1
  198. package/dist-lib/core/conversation/hosted.d.ts.map +1 -1
  199. package/dist-lib/core/conversation/index.d.ts +4 -1
  200. package/dist-lib/core/conversation/index.d.ts.map +1 -1
  201. package/dist-lib/core/conversation/presentation.d.ts.map +1 -1
  202. package/dist-lib/core/conversation/reducer.d.ts.map +1 -1
  203. package/dist-lib/core/conversation/renderer-registry.d.ts +1 -0
  204. package/dist-lib/core/conversation/renderer-registry.d.ts.map +1 -1
  205. package/dist-lib/core/conversation/runtime-ingress.d.ts +22 -0
  206. package/dist-lib/core/conversation/runtime-ingress.d.ts.map +1 -0
  207. package/dist-lib/core/conversation/safe-tool-value.d.ts +8 -0
  208. package/dist-lib/core/conversation/safe-tool-value.d.ts.map +1 -0
  209. package/dist-lib/core/conversation/scoped-cancel.d.ts +16 -0
  210. package/dist-lib/core/conversation/scoped-cancel.d.ts.map +1 -0
  211. package/dist-lib/core/conversation/types.d.ts +6 -1
  212. package/dist-lib/core/conversation/types.d.ts.map +1 -1
  213. package/dist-lib/core/interaction/a2ui-validate.d.ts +6 -3
  214. package/dist-lib/core/interaction/a2ui-validate.d.ts.map +1 -1
  215. package/dist-lib/core/interaction/adapters/legacy-runtime-events.d.ts +8 -0
  216. package/dist-lib/core/interaction/adapters/legacy-runtime-events.d.ts.map +1 -0
  217. package/dist-lib/core/interaction/adapters/normalize.d.ts +3 -0
  218. package/dist-lib/core/interaction/adapters/normalize.d.ts.map +1 -1
  219. package/dist-lib/core/interaction/adapters/responses.d.ts +1 -0
  220. package/dist-lib/core/interaction/adapters/responses.d.ts.map +1 -1
  221. package/dist-lib/core/interaction/adapters/session-events.d.ts.map +1 -1
  222. package/dist-lib/core/interaction/client.d.ts +5 -3
  223. package/dist-lib/core/interaction/client.d.ts.map +1 -1
  224. package/dist-lib/core/interaction/index.d.ts +12 -0
  225. package/dist-lib/core/interaction/index.d.ts.map +1 -1
  226. package/dist-lib/core/interaction/legacy-history.d.ts +5 -0
  227. package/dist-lib/core/interaction/legacy-history.d.ts.map +1 -0
  228. package/dist-lib/core/interaction/response-snapshot.d.ts +13 -0
  229. package/dist-lib/core/interaction/response-snapshot.d.ts.map +1 -0
  230. package/dist-lib/core/interaction/schema.d.ts +7 -0
  231. package/dist-lib/core/interaction/schema.d.ts.map +1 -0
  232. package/dist-lib/core/interaction/store.d.ts +4 -0
  233. package/dist-lib/core/interaction/store.d.ts.map +1 -1
  234. package/dist-lib/core/interaction/types.d.ts +7 -2
  235. package/dist-lib/core/interaction/types.d.ts.map +1 -1
  236. package/dist-lib/core/run/a2ui.d.ts +6 -1
  237. package/dist-lib/core/run/a2ui.d.ts.map +1 -1
  238. package/dist-lib/core/run/agui.d.ts +14 -0
  239. package/dist-lib/core/run/agui.d.ts.map +1 -1
  240. package/dist-lib/core/run/blocks.d.ts +7 -1
  241. package/dist-lib/core/run/blocks.d.ts.map +1 -1
  242. package/dist-lib/core/run/dispatcher.d.ts.map +1 -1
  243. package/dist-lib/core/run/engine.d.ts +2 -0
  244. package/dist-lib/core/run/engine.d.ts.map +1 -1
  245. package/dist-lib/core/run/types.d.ts +24 -1
  246. package/dist-lib/core/run/types.d.ts.map +1 -1
  247. package/dist-lib/core/stream/kernel-events.d.ts +2 -0
  248. package/dist-lib/core/stream/kernel-events.d.ts.map +1 -1
  249. package/dist-lib/core/stream/responses-protocol.d.ts.map +1 -1
  250. package/dist-lib/core/stream/runtime-items.d.ts +2 -2
  251. package/dist-lib/core/stream/runtime-items.d.ts.map +1 -1
  252. package/dist-lib/core/stream/types.d.ts +7 -0
  253. package/dist-lib/core/stream/types.d.ts.map +1 -1
  254. package/dist-lib/core/teams/cloudContracts.d.ts +40 -40
  255. package/dist-lib/core/teams/cloudEffects.d.ts +8 -8
  256. package/dist-lib/core/teams/cloudExtendedContracts.d.ts +74 -74
  257. package/dist-lib/core/teams/cloudExtendedContracts.d.ts.map +1 -1
  258. package/dist-lib/core/teams/cloudMaterials.d.ts +3 -3
  259. package/dist-lib/core/teams/cloudMaterials.d.ts.map +1 -1
  260. package/dist-lib/core/teams/contracts.d.ts +12 -12
  261. package/dist-lib/core/teams/workspaceContracts.d.ts +27 -27
  262. package/dist-lib/{feedback-CuC5jRi_.js → feedback-DRgW061h.js} +108 -86
  263. package/dist-lib/hooks/useAgentChat.d.ts +5 -10
  264. package/dist-lib/hooks/useAgentChat.d.ts.map +1 -1
  265. package/dist-lib/hooks/useFeedback.d.ts +2 -2
  266. package/dist-lib/hooks/useFeedback.d.ts.map +1 -1
  267. package/dist-lib/hooks/useImeComposition.d.ts +21 -0
  268. package/dist-lib/hooks/useImeComposition.d.ts.map +1 -0
  269. package/dist-lib/hooks/useInteractions.d.ts +4 -3
  270. package/dist-lib/hooks/useInteractions.d.ts.map +1 -1
  271. package/dist-lib/hooks/useRunAgent.d.ts +6 -2
  272. package/dist-lib/hooks/useRunAgent.d.ts.map +1 -1
  273. package/dist-lib/hooks/useSessionLifecycle.d.ts +1 -1
  274. package/dist-lib/hooks/useSessionLifecycle.d.ts.map +1 -1
  275. package/dist-lib/hooks.js +27 -25
  276. package/dist-lib/{runtime-items-q6tMjfGU.js → json-value-DnHp0582.js} +0 -0
  277. package/dist-lib/{permission-CcMWqN8Y.js → permission-BT1f0PIC.js} +1 -1
  278. package/dist-lib/persisted-session-history-KtoKbLRA.js +3143 -0
  279. package/dist-lib/public/chat-timeline.d.ts +5 -0
  280. package/dist-lib/public/chat-timeline.d.ts.map +1 -1
  281. package/dist-lib/public/components.d.ts +3 -0
  282. package/dist-lib/public/components.d.ts.map +1 -1
  283. package/dist-lib/public/conversation.d.ts +3 -1
  284. package/dist-lib/public/conversation.d.ts.map +1 -1
  285. package/dist-lib/public/runtime.d.ts +3 -0
  286. package/dist-lib/public/runtime.d.ts.map +1 -1
  287. package/dist-lib/public/types.d.ts +1 -0
  288. package/dist-lib/public/types.d.ts.map +1 -1
  289. package/dist-lib/{reducer-BP8YVv-Q.js → reducer-mbYiX0wz.js} +81 -13
  290. package/dist-lib/runtime.js +320 -254
  291. package/dist-lib/schema-fields-CUpDl1NB.js +41 -0
  292. package/dist-lib/stores/session.d.ts +20 -11
  293. package/dist-lib/stores/session.d.ts.map +1 -1
  294. package/dist-lib/styles.css +1 -1
  295. package/dist-lib/team-components.js +580 -448
  296. package/dist-lib/teams.js +363 -555
  297. package/dist-lib/types/agent-control.d.ts +2 -0
  298. package/dist-lib/types/agent-control.d.ts.map +1 -1
  299. package/dist-lib/types/api.d.ts +2 -0
  300. package/dist-lib/types/api.d.ts.map +1 -1
  301. package/dist-lib/types/capabilities.d.ts +8 -0
  302. package/dist-lib/types/capabilities.d.ts.map +1 -1
  303. package/dist-lib/types/json-value.d.ts +6 -0
  304. package/dist-lib/types/json-value.d.ts.map +1 -0
  305. package/dist-lib/types/session-events.d.ts +1 -0
  306. package/dist-lib/types/session-events.d.ts.map +1 -1
  307. package/dist-lib/useSessionLifecycle-CUf3hB30.js +5200 -0
  308. package/dist-lib/utils/capabilities.d.ts.map +1 -1
  309. package/dist-lib/utils/history-preparation.d.ts +8 -0
  310. package/dist-lib/utils/history-preparation.d.ts.map +1 -0
  311. package/dist-lib/utils/history-tool-facts.d.ts +8 -0
  312. package/dist-lib/utils/history-tool-facts.d.ts.map +1 -0
  313. package/dist-lib/utils/interaction-history.d.ts +11 -0
  314. package/dist-lib/utils/interaction-history.d.ts.map +1 -0
  315. package/dist-lib/utils/kernel-run-history.d.ts +9 -0
  316. package/dist-lib/utils/kernel-run-history.d.ts.map +1 -0
  317. package/dist-lib/utils/messages.d.ts +9 -5
  318. package/dist-lib/utils/messages.d.ts.map +1 -1
  319. package/dist-lib/utils/persisted-session-history.d.ts +1 -1
  320. package/dist-lib/utils/persisted-session-history.d.ts.map +1 -1
  321. package/dist-lib/utils/recovered-run.d.ts.map +1 -1
  322. package/dist-lib/utils/responses-stream.d.ts.map +1 -1
  323. package/dist-lib/utils/run-progress.d.ts +11 -0
  324. package/dist-lib/utils/run-progress.d.ts.map +1 -0
  325. package/dist-lib/utils/session-events.d.ts.map +1 -1
  326. package/dist-lib/utils/transcript-order.d.ts +10 -0
  327. package/dist-lib/utils/transcript-order.d.ts.map +1 -0
  328. package/dist-lib/utils/workspace.d.ts +1 -1
  329. package/package.json +15 -5
  330. package/dist-ksadk/assets/NativeTerminalPanel-oJj44_sV.js +0 -1
  331. package/dist-ksadk/assets/architecture-7GRP2DOG-BCsa26zi.js +0 -1
  332. package/dist-ksadk/assets/channel-_KDifNys.js +0 -1
  333. package/dist-ksadk/assets/chunk-2Q5K7J3B-HdVkJ9F0.js +0 -1
  334. package/dist-ksadk/assets/classDiagram-DTDB5LWJ-BVCW3ZqZ.js +0 -1
  335. package/dist-ksadk/assets/classDiagram-v2-JRS7N3AN-BVCW3ZqZ.js +0 -1
  336. package/dist-ksadk/assets/cynefin-OW5HDTMX-CNXjFctL.js +0 -1
  337. package/dist-ksadk/assets/eventmodeling-NTZA5JFV-B5-8RH9c.js +0 -1
  338. package/dist-ksadk/assets/flowDiagram-A5DVABFB-BppFxEKw.js +0 -1
  339. package/dist-ksadk/assets/gitGraph-4MIJSDKK-B-AhYj4C.js +0 -1
  340. package/dist-ksadk/assets/index-BMd2mucF.css +0 -2
  341. package/dist-ksadk/assets/index-D5qyyhTH.js +0 -244
  342. package/dist-ksadk/assets/info-A6RAGUB7-0uiKrC35.js +0 -1
  343. package/dist-ksadk/assets/packet-AYTQ26CC-BVTmZ_cw.js +0 -1
  344. package/dist-ksadk/assets/pie-WAS4IAKB-DyfxBU7V.js +0 -1
  345. package/dist-ksadk/assets/radar-RG4KPBEZ-FFontjL2.js +0 -1
  346. package/dist-ksadk/assets/railroad-74A4TZTK-D0Uidzen.js +0 -1
  347. package/dist-ksadk/assets/railroad-abnf-HS5TGJTU-BOo_9T2h.js +0 -1
  348. package/dist-ksadk/assets/railroad-ebnf-LZEXJU2U-D6-BjW7Z.js +0 -1
  349. package/dist-ksadk/assets/railroad-peg-WCYAUIDC-CXx__yMm.js +0 -1
  350. package/dist-ksadk/assets/stateDiagram-v2-4QOOHH4V-BXAFScz8.js +0 -1
  351. package/dist-ksadk/assets/swimlanesDiagram-VK2B7HYN-uIQ2lJKH.js +0 -8
  352. package/dist-ksadk/assets/treeView-Q6P3EWNA-CK5GgucZ.js +0 -1
  353. package/dist-ksadk/assets/treemap-WGGIJYW6-Bq1u1SpG.js +0 -1
  354. package/dist-ksadk/assets/wardley-WFR3VGLG-QAt06uEz.js +0 -1
  355. package/dist-lib/AttachmentPreview-HLpbjOnQ.js +0 -535
  356. package/dist-lib/ConnectedMessageList-D3HWHCh2.js +0 -1361
  357. package/dist-lib/InteractionSchemaForm-Du9NT4E8.js +0 -157
  358. package/dist-lib/a2ui-BEQoW34n.js +0 -502
  359. package/dist-lib/persisted-session-history-ClsfDfGs.js +0 -1991
  360. package/dist-lib/useSessionLifecycle-DQXKeBm9.js +0 -4455
@@ -0,0 +1,3143 @@
1
+ import { n as e, t } from "./json-value-DnHp0582.js";
2
+ import { n } from "./tool-display-DTiRdghE.js";
3
+ import { d as r, i, l as a, o, p as s, t as c, u as l } from "./reducer-mbYiX0wz.js";
4
+ //#region src/core/conversation/safe-tool-value.ts
5
+ var u = [
6
+ "authorization",
7
+ "credential",
8
+ "password",
9
+ "secret",
10
+ "token",
11
+ "cookie",
12
+ "apikey",
13
+ "accesskey",
14
+ "secretaccesskey",
15
+ "clientsecret",
16
+ "refreshtoken",
17
+ "accesstoken",
18
+ "setcookie",
19
+ "traceback",
20
+ "stack",
21
+ "exception"
22
+ ];
23
+ function d(e) {
24
+ return e.normalize("NFKC").toLowerCase().replace(/[^a-z0-9]/g, "");
25
+ }
26
+ function f(e) {
27
+ let t = d(e);
28
+ return u.some((e) => t.includes(e));
29
+ }
30
+ function p(e) {
31
+ let t = e.split(".");
32
+ if (t.length > 4 || t.some((e) => e.length === 0)) return null;
33
+ let n = t.map((e) => {
34
+ let t = /^0x/i.test(e) ? 16 : e.length > 1 && e.startsWith("0") ? 8 : 10, n = t === 16 ? e.slice(2) : e;
35
+ if (!n || !RegExp("^[0-9a-f]+$", "i").test(n)) return null;
36
+ let r = Number.parseInt(n, t);
37
+ return Number.isSafeInteger(r) ? r : null;
38
+ });
39
+ if (n.some((e) => e === null)) return null;
40
+ let r = n, i = 2 ** (8 * (5 - r.length));
41
+ return r.slice(0, -1).some((e) => e > 255) || r.at(-1) >= i ? null : r.slice(0, -1).reduce((e, t, n) => e + t * 2 ** (8 * (3 - n)), r.at(-1));
42
+ }
43
+ function m(e) {
44
+ return e >>> 24 == 0 || e >>> 24 == 10 || e >>> 24 == 127 || e >>> 16 == 43518 || e >>> 16 == 49320 || e >>> 20 == 2753;
45
+ }
46
+ function h(e) {
47
+ let t;
48
+ try {
49
+ t = decodeURIComponent(e).toLowerCase().replace(/^\[|\]$/g, "").replace(/\.$/, "");
50
+ } catch {
51
+ return !0;
52
+ }
53
+ if (t === "localhost" || t.endsWith(".localhost") || t.endsWith(".local") || t.endsWith(".internal")) return !0;
54
+ let n = t.match(/^::ffff:(\d+(?:\.\d+){0,3})$/)?.[1], r = t.match(/^::ffff:([0-9a-f]{1,4}):([0-9a-f]{1,4})$/i), i = r ? Number.parseInt(r[1], 16) * 65536 + Number.parseInt(r[2], 16) : p(n || t);
55
+ if (i !== null) return m(i);
56
+ let a = Number.parseInt(t.split(":", 1)[0], 16);
57
+ return t === "::" || t === "::1" || Number.isInteger(a) && (a & 65024) == 64512 || Number.isInteger(a) && (a & 65472) == 65152;
58
+ }
59
+ function g(e) {
60
+ return (e.match(/https?:\/\/\[[^\]]+\](?::\d+)?[^\s<>{}"']*|https?:\/\/[^\s<>{}"'[\]()]+/gi) || []).some((e) => {
61
+ let t = e.replace(/[.,;:!?]+$/, "").replace(/\\/g, "/");
62
+ try {
63
+ let e = new URL(t);
64
+ return !!(e.username || e.password) || h(e.hostname);
65
+ } catch {
66
+ return !0;
67
+ }
68
+ });
69
+ }
70
+ function _(e, t = 0) {
71
+ return y(e, t, 8192);
72
+ }
73
+ function v(e, t, n) {
74
+ return y(e, 0, [
75
+ "langgraph",
76
+ "adk",
77
+ "codex"
78
+ ].includes(String(t)) && !String(n || "").toLowerCase().includes("a2a") ? Infinity : 8192);
79
+ }
80
+ function y(e, t, n) {
81
+ return t > 8 ? "[truncated]" : typeof e == "string" ? /bearer\s+\S+|traceback|(?:token|authorization|api[_ .-]?key|access[_ .-]?token|refresh[_ .-]?token|secretaccesskey|accesstoken|refreshtoken|cookie|password|secret)\s*["']?\s*[=:]/i.test(e) || g(e) ? "[redacted]" : e.length > n ? `${e.slice(0, n)}[truncated]` : e : Array.isArray(e) ? e.slice(0, 128).map((e) => y(e, t + 1, n)) : e && typeof e == "object" ? Object.fromEntries(Object.entries(e).slice(0, 128).filter(([e]) => !f(e)).map(([e, r]) => [e, y(r, t + 1, n)])) : e;
82
+ }
83
+ //#endregion
84
+ //#region src/core/conversation/runtime-ingress.ts
85
+ var b = (e) => e && typeof e == "object" && !Array.isArray(e) ? e : {}, x = (e, t, n) => JSON.stringify([
86
+ e,
87
+ t,
88
+ n
89
+ ]), S = class {
90
+ runtime = new e();
91
+ flatChildren = new c();
92
+ seen = /* @__PURE__ */ new Set();
93
+ parents = /* @__PURE__ */ new Map();
94
+ descriptorItemIds = /* @__PURE__ */ new Map();
95
+ descriptors = /* @__PURE__ */ new Map();
96
+ rootScope = "";
97
+ runId = "";
98
+ sessionId;
99
+ conversation;
100
+ profile;
101
+ constructor(e, t = new c(), n = "flat-v1") {
102
+ this.sessionId = e, this.conversation = t, this.profile = n;
103
+ }
104
+ identity(e, t, n, r = !1) {
105
+ return this.profile === "agent-block-v1" || r ? x(e, t, n) : n;
106
+ }
107
+ snapshot() {
108
+ let e = this.conversation.snapshot();
109
+ if (this.profile !== "flat-v1") return e;
110
+ let t = this.flatChildren.snapshot();
111
+ return {
112
+ items: [...e.items, ...t.items],
113
+ appliedSources: [...e.appliedSources, ...t.appliedSources]
114
+ };
115
+ }
116
+ apply(e) {
117
+ if (e.schema_version !== 2) return null;
118
+ let t = String(e.run_id || ""), n = String(e.scope_id || ""), r = String(e.event_id || ""), i = String(e.event_type || "");
119
+ if (!i.startsWith("item.") && !i.startsWith("run.")) return null;
120
+ if (!t || !n || !r) throw Error("RuntimeEvent identity missing");
121
+ if (this.runId && this.runId !== t) throw Error("RuntimeEvent run identity changed");
122
+ if (this.seen.has(r)) return null;
123
+ let a = String(e.parent_scope_id ?? this.descriptors.get(n)?.parent_scope_id ?? this.parents.get(n) ?? ""), s = b(e.source), c = [
124
+ "adk",
125
+ "langgraph",
126
+ "codex"
127
+ ].includes(String(s.framework)) && !this.descriptors.has(n);
128
+ if (c && a && i === "run.progress") return null;
129
+ if (i.startsWith("run.") && (a || !c && this.rootScope && n !== this.rootScope)) throw Error("Child scope cannot transition root run");
130
+ if (this.parents.has(n) && this.parents.get(n) !== a) throw Error("Scope parent changed");
131
+ let l = String(e.item_id || ""), u = this.identity(t, n, l || r, !!a), d = Object.fromEntries(Object.entries({
132
+ framework: s.framework,
133
+ protocol: s.protocol,
134
+ eventId: s.native_event_id,
135
+ cursor: s.native_cursor,
136
+ runId: s.native_run_id,
137
+ itemId: s.native_item_id,
138
+ scopeId: n,
139
+ runtimeItemId: l || void 0,
140
+ parentScopeId: a || void 0,
141
+ phase: e.phase
142
+ }).filter(([, e]) => e != null)), f = {
143
+ apiVersion: "conversation.ksadk.io/v1",
144
+ kindVersion: 1,
145
+ itemId: u,
146
+ sessionId: this.sessionId,
147
+ runId: t,
148
+ sourceEventIds: [r],
149
+ kind: "progress",
150
+ operation: "replace",
151
+ lifecycle: "streaming",
152
+ visibility: "public",
153
+ payloadSchemaRef: "conversation.item.progress/v1",
154
+ payload: {},
155
+ nativeRef: d
156
+ }, p = b(b(e.source).metadata), m = p.conversation_visibility ?? p.visibility;
157
+ if ([
158
+ "public",
159
+ "internal",
160
+ "hidden"
161
+ ].includes(String(m)) ? f.visibility = m : a && e.item_kind === "reasoning" && (f.visibility = "hidden"), i.startsWith("run.")) this.rootScope = n, f.payload = {
162
+ status: ["run.started", "run.progress"].includes(i) ? "running" : i.slice(4),
163
+ ...e.output_refs ? { outputRefs: e.output_refs } : {}
164
+ }, i === "run.progress" && Object.assign(f.payload, {
165
+ progress: e.progress,
166
+ message: e.message
167
+ }), i === "run.interrupted" && (f.payload.reason = e.reason || ""), f.operation = ["run.started", "run.progress"].includes(i) ? "append" : "completed", [
168
+ "run.failed",
169
+ "run.canceled",
170
+ "run.cancelled"
171
+ ].includes(i) && (f.kind = "error", f.payloadSchemaRef = "conversation.item.error/v1", f.payload.error = b(e.error).message || e.reason || ""), f.lifecycle = ["run.completed", "run.interrupted"].includes(i) ? "completed" : [
172
+ "run.failed",
173
+ "run.canceled",
174
+ "run.cancelled"
175
+ ].includes(i) ? "failed" : "streaming";
176
+ else {
177
+ if (!l) throw Error("RuntimeEvent item identity missing");
178
+ let d = b(e.snapshot || e.initial), p = i === "item.updated" ? [b(e.update)] : Array.isArray(d.parts) ? d.parts : [], h = p.map((e) => {
179
+ let t = b(e);
180
+ return {
181
+ partId: String(t.part_id || "part"),
182
+ contentType: String(t.content_type || "data"),
183
+ ...typeof t.text == "string" ? { text: t.text } : {},
184
+ value: t
185
+ };
186
+ }), g = p.map((e) => b(b(e).data)).find((e) => e.schema === "execution.scope/v1");
187
+ if (a && !g && !c) {
188
+ let e = this.descriptorItemIds.get(n);
189
+ if (!e) throw Error("Child content requires its scope descriptor first");
190
+ f.parentItemId = e;
191
+ }
192
+ if (g) {
193
+ if (this.descriptorItemIds.has(n) && this.descriptorItemIds.get(n) !== u) throw Error("Scope descriptor item identity changed");
194
+ let e = b(g.trigger_ref);
195
+ if (g.scope_id !== n || g.parent_scope_id !== a || e.scope_id !== a || !e.item_id || !e.call_id || !a) throw Error("Invalid scope descriptor locator");
196
+ let t = this.runtime.snapshot().items.find((t) => t.scopeId === e.scope_id && t.itemId === e.item_id);
197
+ if (t && !t.parts.some((t) => b(t.value).call_id === e.call_id)) throw Error("Scope trigger call mismatch");
198
+ let r = this.descriptors.get(n);
199
+ o({
200
+ ...f,
201
+ kind: "agent",
202
+ payloadSchemaRef: "conversation.item.agent/v1",
203
+ payload: g
204
+ }, r ? {
205
+ ...f,
206
+ kind: "agent",
207
+ payload: r
208
+ } : void 0), this.descriptors.set(n, g), this.descriptorItemIds.set(n, u);
209
+ }
210
+ for (let e of this.descriptors.values()) {
211
+ let t = b(e.trigger_ref);
212
+ if (t.scope_id === n && t.item_id === l && h.length && !h.some((e) => b(e.value).call_id === t.call_id)) throw Error("Scope trigger call mismatch");
213
+ }
214
+ let y = this.runtime.snapshot().items.find((e) => e.scopeId === n && e.itemId === l);
215
+ if (a && y && y.status !== "open") {
216
+ if (i === "item.updated" || i === "item.started") throw Error("Remote item cannot reopen");
217
+ if (Array.isArray(d.parts) && JSON.stringify(y.parts) !== JSON.stringify(h)) throw Error("Remote terminal content conflict");
218
+ }
219
+ let x = {
220
+ type: i.replaceAll(".", "_"),
221
+ runId: t,
222
+ scopeId: n,
223
+ itemId: l,
224
+ itemKind: e.item_kind,
225
+ eventId: r,
226
+ initialParts: h,
227
+ phase: e.phase,
228
+ ...Array.isArray(d.parts) ? { parts: h } : {},
229
+ ...i === "item.updated" ? {
230
+ partId: h[0]?.partId,
231
+ part: h[0],
232
+ op: e.op || "replace"
233
+ } : {}
234
+ };
235
+ this.runtime.apply(x);
236
+ let S = this.runtime.snapshot().items.find((e) => e.scopeId === n && e.itemId === l);
237
+ if (!S || a && S.itemKind === "status" && S.status === "open" && !S.parts.length) return null;
238
+ if (a && [
239
+ "artifact",
240
+ "tool_call",
241
+ "tool_result"
242
+ ].includes(S.itemKind) && !S.parts.length) return this.runId = t, this.parents.set(n, a), this.seen.add(r), null;
243
+ let C = S.parts.map((e) => b(e.value)), w = C.map((e) => b(e.data)).find((e) => e.schema === "execution.scope/v1");
244
+ if (f.lifecycle = S.status === "open" ? i === "item.started" ? "pending" : "streaming" : S.status, f.operation = ["completed", "failed"].includes(f.lifecycle) ? "completed" : i === "item.started" ? "append" : "replace", w) f.operation = ["completed", "failed"].includes(f.lifecycle) ? "completed" : "replace", f.kind = "agent", f.payloadSchemaRef = "conversation.item.agent/v1", f.payload = w, f.parentItemId = this.identity(t, String(b(w.trigger_ref).scope_id), String(b(w.trigger_ref).item_id), !!this.parents.get(String(b(w.trigger_ref).scope_id))), f.capabilityRef = "agent.block";
245
+ else if (S.itemKind === "tool_call" || S.itemKind === "tool_result") {
246
+ let e = C.find((e) => e.content_type === "tool_call"), t = C.find((e) => e.content_type === "tool_result");
247
+ f.kind = "tool_call", f.capabilityRef = "tool.inspect", f.payloadSchemaRef = "conversation.item.tool-call/v1", f.payload = {
248
+ ...e ? {
249
+ callId: e.call_id,
250
+ tool: e.name,
251
+ args: _(e.arguments)
252
+ } : {},
253
+ ...t ? {
254
+ callId: t.call_id,
255
+ output: v(t.result, s.framework, s.protocol),
256
+ isError: t.is_error
257
+ } : {},
258
+ executionStatus: S.status === "failed" ? "failed" : t && S.status !== "open" ? t.is_error ? "failed" : "completed" : S.itemKind === "tool_result" ? "unknown" : "running",
259
+ sourceKind: S.itemKind
260
+ };
261
+ } else if (S.itemKind === "data" && s.protocol === "a2ui") {
262
+ let e = C.find((e) => e.content_type === "data")?.data;
263
+ f.kind = "a2ui", f.payloadSchemaRef = "conversation.item.a2ui/v1", f.payload = {
264
+ surfaceId: String(b(s.metadata).surface_id || ""),
265
+ ...typeof b(s.metadata).interaction_id == "string" ? { interactionId: b(s.metadata).interaction_id } : {},
266
+ data: Array.isArray(e) ? e : []
267
+ };
268
+ } else if (S.itemKind === "message" || S.itemKind === "reasoning" || S.itemKind === "artifact" && S.parts.length > 0 && S.parts.every((e) => e.contentType === "text")) {
269
+ f.kind = S.itemKind === "reasoning" ? "reasoning" : b(b(e.source).metadata).native_item_kind === "userMessage" ? "user_message" : "assistant_text", f.payloadSchemaRef = `conversation.item.${f.kind}/v1`, f.payload = { text: S.parts.map((e) => e.text || "").join("") };
270
+ let t = b(s.metadata);
271
+ f.kind === "reasoning" && (t.public_summary === !0 || t.provider_reasoning === !0) && (f.payload.presentationSource = t.public_summary === !0 ? "public_summary" : "provider_reasoning"), i === "item.updated" && e.op === "append" && (f.operation = "append", f.payload.text = b(e.update).text || "");
272
+ } else if (S.itemKind === "artifact") {
273
+ let e = C.find((e) => e.content_type === "artifact") || {};
274
+ f.kind = "artifact", f.payloadSchemaRef = "conversation.item.artifact/v1", f.payload = {
275
+ name: e.name,
276
+ mimeType: e.mime_type,
277
+ uri: e.uri
278
+ };
279
+ } else if (S.status === "failed") {
280
+ let t = b(e.error);
281
+ f.kind = "error", f.payloadSchemaRef = "conversation.item.error/v1", f.payload = { error: String(t.message || t.code || "Remote call failed") };
282
+ } else f.visibility = m === "public" ? "public" : "hidden", f.kind = "unknown", f.payloadSchemaRef = "conversation.item.unknown/v1", f.payload = { summary: "Unsupported runtime content" };
283
+ }
284
+ return this.runId = t, this.parents.set(n, a), this.seen.add(r), (this.profile === "flat-v1" && a ? this.flatChildren : this.conversation).apply(f), f;
285
+ }
286
+ };
287
+ //#endregion
288
+ //#region src/core/a2ui/history.ts
289
+ function C(e, t) {
290
+ let n = e.map((e) => JSON.stringify(e)), r = t.map((e) => JSON.stringify(e));
291
+ return n.every((e, t) => r[t] === e) ? t : r.every((e, t) => n[t] === e) ? e : [...e, ...t];
292
+ }
293
+ function w(e, t, n) {
294
+ let r = {
295
+ ...t.runId || e?.runId ? { runId: t.runId || e?.runId } : {},
296
+ ...t.interactionId || e?.interactionId ? { interactionId: t.interactionId || e?.interactionId } : {},
297
+ ...e?.originMessageId || t.originMessageId ? { originMessageId: e?.originMessageId || t.originMessageId } : {}
298
+ };
299
+ return n && e?.batchIds?.includes(n) ? Object.entries(r).every(([t, n]) => e[t] === n) ? e : {
300
+ ...e,
301
+ ...r
302
+ } : {
303
+ ...t,
304
+ ...r,
305
+ messages: e ? C(e.messages, t.messages) : t.messages,
306
+ ...n || e?.batchIds || t.batchIds ? { batchIds: [...new Set([
307
+ ...e?.batchIds || [],
308
+ ...t.batchIds || [],
309
+ ...n ? [n] : []
310
+ ])] } : {}
311
+ };
312
+ }
313
+ //#endregion
314
+ //#region src/core/a2ui/transcript.ts
315
+ function T(e, t) {
316
+ if (e.activity.surfaceId !== t.activity.surfaceId) return !1;
317
+ let n = e.activity.interactionId, r = t.activity.interactionId;
318
+ if (n && r) return n === r;
319
+ let i = e.activity.runId || e.message.runId || e.message.invocationId, a = t.activity.runId || t.message.runId || t.message.invocationId;
320
+ return i && a ? i === a : (e.activity.originMessageId || e.message.id) === (t.activity.originMessageId || t.message.id);
321
+ }
322
+ function ee(e, t) {
323
+ let n = e.filter((e) => T(e, t));
324
+ return n.length === 1 ? n[0] : void 0;
325
+ }
326
+ function te(e) {
327
+ if (!e.some((e) => e.aguiActivity || e.aguiActivities?.length)) return e;
328
+ let t = /* @__PURE__ */ new Map(), n = !1, r = e.map((e) => {
329
+ let r = (r) => {
330
+ let i = t.get(r.surfaceId) || [], a = ee(i, {
331
+ message: e,
332
+ activity: r
333
+ });
334
+ if (a) {
335
+ let t = a.activity;
336
+ n = !0, t.messages = C(t.messages, r.messages), t.interactionId = r.interactionId || t.interactionId, t.runId ||= r.runId || e.runId || e.invocationId, t.batchIds = [...new Set([...t.batchIds || [], ...r.batchIds || []])];
337
+ return;
338
+ }
339
+ let o = r.runId || e.runId || e.invocationId, s = {
340
+ ...r,
341
+ ...o ? { runId: o } : {}
342
+ };
343
+ return t.set(r.surfaceId, [...i, {
344
+ message: e,
345
+ activity: s
346
+ }]), s;
347
+ };
348
+ return {
349
+ ...e,
350
+ ...e.aguiActivity ? { aguiActivity: r(e.aguiActivity) } : {},
351
+ ...e.aguiActivities ? { aguiActivities: e.aguiActivities.flatMap((e) => {
352
+ let t = r(e);
353
+ return t ? [t] : [];
354
+ }) } : {}
355
+ };
356
+ });
357
+ return n ? r.filter((e) => e.role !== "a2ui" || e.aguiActivity || e.aguiActivities?.length || e.content) : e;
358
+ }
359
+ //#endregion
360
+ //#region src/core/conversation/presentation.ts
361
+ var ne = {
362
+ user_message: "conversation.item.user_message/v1",
363
+ assistant_text: "conversation.item.assistant_text/v1",
364
+ reasoning: "conversation.item.reasoning/v1",
365
+ tool_call: "conversation.item.tool-call/v1",
366
+ agent: "conversation.item.agent/v1",
367
+ approval: "conversation.item.approval/v1",
368
+ artifact: "conversation.item.artifact/v1",
369
+ a2ui: "conversation.item.a2ui/v1",
370
+ error: "conversation.item.error/v1"
371
+ };
372
+ function re(e) {
373
+ return e.lifecycle === "completed" || e.lifecycle === "failed";
374
+ }
375
+ var ie = new Set([
376
+ "aborted",
377
+ "canceled",
378
+ "cancelled",
379
+ "failed",
380
+ "incomplete",
381
+ "interrupted"
382
+ ]);
383
+ function E(e) {
384
+ if (e.parentItemId || e.nativeRef.parentScopeId || e.nativeRef.parent_scope_id || e.kind !== "progress" && e.kind !== "error" || !re(e)) return;
385
+ let t = typeof e.payload.status == "string" ? e.payload.status.toLowerCase() : "";
386
+ if (t === "completed") return "completed";
387
+ if (ie.has(t)) return "failed";
388
+ }
389
+ function ae(e) {
390
+ let t = ne[e.kind];
391
+ return t === void 0 || t === e.payloadSchemaRef;
392
+ }
393
+ function oe(e) {
394
+ if (typeof e != "string" || e.length === 0) return null;
395
+ try {
396
+ let t = new URL(e);
397
+ return t.protocol !== "http:" && t.protocol !== "https:" || t.username || t.password ? null : t.toString();
398
+ } catch {
399
+ return null;
400
+ }
401
+ }
402
+ function se(e) {
403
+ return {
404
+ id: e.itemId,
405
+ parentId: e.parentItemId || null,
406
+ runId: e.runId,
407
+ kind: e.kind,
408
+ text: typeof e.payload.text == "string" ? e.payload.text : "",
409
+ lifecycle: e.lifecycle
410
+ };
411
+ }
412
+ function ce(e) {
413
+ let t = typeof e.payload.artifactId == "string" && e.payload.artifactId ? e.payload.artifactId : e.itemId, n = e.payload.sizeBytes ?? e.payload.size, r = typeof n == "number" && Number.isFinite(n) && n >= 0 ? n : null, i = oe(e.payload.uri);
414
+ return {
415
+ artifactId: t,
416
+ itemId: e.itemId,
417
+ runId: e.runId,
418
+ sourceEventIds: [...e.sourceEventIds],
419
+ id: e.itemId,
420
+ name: typeof e.payload.name == "string" && e.payload.name ? e.payload.name : "Artifact",
421
+ mimeType: typeof e.payload.mimeType == "string" && e.payload.mimeType ? e.payload.mimeType : "application/octet-stream",
422
+ sizeBytes: r,
423
+ status: e.lifecycle === "failed" ? "failed" : e.lifecycle === "completed" ? i ? "ready" : "failed" : "pending",
424
+ uri: i
425
+ };
426
+ }
427
+ function D(e, t) {
428
+ let n = e.payload[t];
429
+ return typeof n == "string" && n ? n : null;
430
+ }
431
+ function le(e) {
432
+ if (e.kind === "tool_call") {
433
+ let t = D(e, "callId");
434
+ if (t) return `tool:${JSON.stringify([
435
+ e.runId,
436
+ e.nativeRef.scopeId || e.nativeRef.scope_id || "",
437
+ t
438
+ ])}`;
439
+ }
440
+ if (e.kind === "approval") {
441
+ let t = D(e, "interactionId");
442
+ if (t) return `approval:${t}`;
443
+ }
444
+ if (e.kind === "a2ui") {
445
+ let t = D(e, "surfaceId");
446
+ if (t) return `a2ui:${JSON.stringify([e.runId, t])}`;
447
+ }
448
+ return `item:${e.itemId}`;
449
+ }
450
+ function ue(e, t) {
451
+ let n = re(e) && !re(t), r = e.kind === "a2ui" && t.kind === "a2ui" && Array.isArray(e.payload.data) && Array.isArray(t.payload.data) ? { data: C(e.payload.data, t.payload.data) } : {};
452
+ return {
453
+ ...e,
454
+ ...t,
455
+ itemId: e.itemId,
456
+ parentItemId: e.parentItemId,
457
+ sourceEventIds: [...new Set([...e.sourceEventIds, ...t.sourceEventIds])],
458
+ payload: {
459
+ ...e.payload,
460
+ ...t.payload,
461
+ ...r,
462
+ ...e.payload.sourceKind === "tool_result" && t.payload.sourceKind === "tool_call" ? {
463
+ executionStatus: e.payload.executionStatus,
464
+ sourceKind: "tool_result"
465
+ } : {}
466
+ },
467
+ nativeRef: {
468
+ ...e.nativeRef,
469
+ ...t.nativeRef
470
+ },
471
+ ...n ? {
472
+ lifecycle: e.lifecycle,
473
+ operation: e.operation
474
+ } : {}
475
+ };
476
+ }
477
+ function de(e) {
478
+ let t = [], n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map();
479
+ for (let i of e) {
480
+ let e = le(i), a = i.kind === "a2ui" ? {
481
+ surfaceId: D(i, "surfaceId") || "",
482
+ messages: [],
483
+ ...D(i, "interactionId") ? { interactionId: D(i, "interactionId") } : {}
484
+ } : null, o = a ? {
485
+ message: {
486
+ id: i.itemId,
487
+ runId: i.runId
488
+ },
489
+ activity: a
490
+ } : null, s = r.get(e) || [], c = o ? ee(s, o) : void 0, l = o ? c?.index : n.get(e);
491
+ if (l === void 0) {
492
+ o ? r.set(e, [...s, {
493
+ ...o,
494
+ index: t.length
495
+ }]) : n.set(e, t.length), t.push({
496
+ key: o ? `${e}:${i.itemId}` : e,
497
+ item: i,
498
+ sourceItemIds: [i.itemId]
499
+ });
500
+ continue;
501
+ }
502
+ c && a?.interactionId && (c.activity.interactionId = a.interactionId);
503
+ let u = t[l];
504
+ t[l] = {
505
+ key: u.key,
506
+ item: ue(u.item, i),
507
+ sourceItemIds: [...new Set([...u.sourceItemIds, i.itemId])]
508
+ };
509
+ }
510
+ return t.map((t) => t.item.kind === "tool_call" ? {
511
+ ...t,
512
+ item: {
513
+ ...t.item,
514
+ payload: {
515
+ ...t.item.payload,
516
+ ...t.item.payload.sourceKind === "tool_result" ? { orphan: !e.some((e) => t.sourceItemIds.includes(e.itemId) && e.payload.sourceKind === "tool_call") } : {}
517
+ }
518
+ }
519
+ } : t);
520
+ }
521
+ function fe(e, t) {
522
+ let n = e.payload.trigger_ref;
523
+ return n ? t.filter((t) => t.kind === "tool_call" && t.runId === e.runId && (t.nativeRef.scopeId || t.nativeRef.scope_id) === n.scope_id && (t.payload.callId === n.call_id || t.itemId === e.parentItemId)) : [];
524
+ }
525
+ function pe(e) {
526
+ let t = e.filter((e) => e.kind === "agent"), n = (e) => String(e.nativeRef.scopeId || e.nativeRef.scope_id || ""), r = (e) => t.find((t) => t.runId === e.runId && t.itemId !== e.itemId && (e.parentItemId === t.itemId || (e.kind === "agent" ? e.payload.parent_scope_id === t.payload.scope_id : n(e) && n(e) === String(t.payload.scope_id)))), i = new Set(t.flatMap((t) => fe(t, e).map((e) => e.itemId))), a = e.filter((e) => !r(e) && !i.has(e.itemId));
527
+ return a.sort((t, n) => {
528
+ let r = (t) => t.kind === "agent" && t.parentItemId && e.some((e) => e.itemId === t.parentItemId) ? e.findIndex((e) => e.itemId === t.parentItemId) : e.indexOf(t);
529
+ return r(t) - r(n);
530
+ }), de(a).map((t) => {
531
+ if (t.item.kind !== "agent") return t;
532
+ let n = de(e.filter((e) => r(e) === t.item && !i.has(e.itemId)));
533
+ return {
534
+ ...t,
535
+ sourceItemIds: [...new Set([...fe(t.item, e).map((e) => e.itemId), ...t.sourceItemIds])],
536
+ children: n
537
+ };
538
+ });
539
+ }
540
+ function me(e, t = {}) {
541
+ let n = e.items.filter((e) => e.visibility === "public" || t.includeInternal === !0 && e.visibility === "internal");
542
+ if (t.profile === "flat-v1" && n.some((e) => e.kind === "agent")) {
543
+ let e = n.filter((e) => e.kind === "agent"), t = new Set(e.flatMap((e) => fe(e, n).map((e) => e.itemId))), r = (t) => e.some((e) => e.runId === t.runId && (t.parentItemId === e.itemId || typeof e.payload.scope_id == "string" && e.payload.scope_id.length > 0 && e.payload.scope_id === (t.nativeRef.scopeId || t.nativeRef.scope_id))), i = n.find((e) => E(e)), a = n.filter((e) => e.kind !== "agent" && (e.kind === "error" || !r(e) && !t.has(e.itemId))), o = i?.payload.outputRefs || i?.payload.output_refs;
544
+ Array.isArray(o) && o.length && (a = a.filter((e) => e.kind !== "assistant_text" || o.some((t) => e.nativeRef.scopeId === t.scope_id && e.nativeRef.runtimeItemId === t.item_id)));
545
+ let s = a.filter((e) => e.kind === "assistant_text");
546
+ if (i && !s.some((e) => e.payload.text) && Array.isArray(o)) {
547
+ let e = o.flatMap((e) => n.filter((t) => t.runId === i.runId && t.nativeRef.scopeId === e.scope_id && t.nativeRef.runtimeItemId === e.item_id && t.kind === "assistant_text"));
548
+ e.length && a.push({
549
+ ...e[0],
550
+ itemId: `${i.runId}:delegated-final`,
551
+ parentItemId: null,
552
+ nativeRef: {},
553
+ payload: { text: e.map((e) => e.payload.text).join("") },
554
+ lifecycle: "completed"
555
+ });
556
+ }
557
+ n = a.filter((e) => e.kind !== "assistant_text" || !!i);
558
+ }
559
+ let r = n.filter(ae), i = n.filter((e) => !ae(e)), a = new Set([
560
+ "user_message",
561
+ "assistant_text",
562
+ "reasoning"
563
+ ]), o = r.filter((e) => a.has(e.kind)).map(se), s = (e) => e.nativeRef.parentScopeId || e.nativeRef.parent_scope_id || r.some((t) => t.kind === "agent" && (e.parentItemId === t.itemId || t.runId === e.runId && t.payload.scope_id === (e.nativeRef.scopeId || e.nativeRef.scope_id))), c = (e) => r.filter((t) => t.kind === e && !s(t)).map((e) => typeof e.payload.text == "string" ? e.payload.text : "").join(""), l = [...r.filter((e) => e.kind === "unknown"), ...i], u = r.filter((e) => e.kind === "error"), d = [...r].reverse().find((e) => E(e) !== void 0);
564
+ return {
565
+ timeline: pe(r.filter((e) => e.kind !== "progress")),
566
+ output: c("assistant_text"),
567
+ reasoning: c("reasoning"),
568
+ textItems: o,
569
+ toolItems: r.filter((e) => e.kind === "tool_call"),
570
+ approvalItems: r.filter((e) => e.kind === "approval"),
571
+ structuredInputItems: r.filter((e) => e.kind === "progress" && e.payloadSchemaRef === "conversation.item.structured-input/v1"),
572
+ a2uiItems: r.filter((e) => e.kind === "a2ui"),
573
+ artifacts: r.filter((e) => e.kind === "artifact").map(ce),
574
+ fallbacks: [...l.map((e) => ({
575
+ id: e.itemId,
576
+ title: "Unsupported content",
577
+ detail: String(e.payload.summary || e.payload.originalKind || e.payloadSchemaRef),
578
+ failed: e.lifecycle === "failed"
579
+ })), ...u.map((e) => ({
580
+ id: e.itemId,
581
+ title: "Run failed",
582
+ detail: String(e.payload.error || "The agent run failed."),
583
+ failed: !0
584
+ }))],
585
+ runId: n.at(-1)?.runId || "",
586
+ terminalStatus: d ? E(d) : void 0
587
+ };
588
+ }
589
+ //#endregion
590
+ //#region src/core/conversation/client.ts
591
+ var O = 8;
592
+ function k(e) {
593
+ return typeof e == "object" && e && !Array.isArray(e) ? e : null;
594
+ }
595
+ function A(e) {
596
+ if (e?.aborted) throw new s("conversation_aborted", "Conversation request was aborted.");
597
+ }
598
+ function he(e, t) {
599
+ return t?.aborted === !0 || e instanceof Error && e.name === "AbortError";
600
+ }
601
+ function ge(e) {
602
+ let t = e.trim().replace(/\/+$/, "");
603
+ if (!t) return "";
604
+ if (!t.includes("://")) return t.startsWith("/") ? t : `/${t}`;
605
+ try {
606
+ let e = new URL(t);
607
+ if (e.protocol !== "http:" && e.protocol !== "https:" || e.username || e.password) throw Error("unsafe URL");
608
+ return e.toString().replace(/\/$/, "");
609
+ } catch (e) {
610
+ throw new s("conversation_contract_mismatch", "Conversation base URL must be an HTTP(S) URL without embedded credentials.", { cause: e });
611
+ }
612
+ }
613
+ function _e(e) {
614
+ return Math.min(200 * 2 ** Math.max(0, e - 1), 2e3);
615
+ }
616
+ function ve(e) {
617
+ return new Promise((t) => {
618
+ setTimeout(t, e);
619
+ });
620
+ }
621
+ async function ye(e, t, n) {
622
+ if (A(n), !n) {
623
+ await e(t);
624
+ return;
625
+ }
626
+ await new Promise((r, i) => {
627
+ let a = () => i(new s("conversation_aborted", "Conversation request was aborted."));
628
+ n.addEventListener("abort", a, { once: !0 }), e(t).then(r, i).finally(() => {
629
+ n.removeEventListener("abort", a);
630
+ });
631
+ });
632
+ }
633
+ function be(e, t, n, r, i) {
634
+ let a = i ? i.snapshot() : e.snapshot();
635
+ return {
636
+ cursor: t,
637
+ runId: n,
638
+ state: a,
639
+ presentation: me(a, { profile: r })
640
+ };
641
+ }
642
+ function xe(e, t) {
643
+ let n, r = [];
644
+ for (let t of e.split(/\r?\n/)) {
645
+ let e = t.endsWith("\r") ? t.slice(0, -1) : t;
646
+ if (!e || e.startsWith(":")) continue;
647
+ let i = e.indexOf(":"), a = i < 0 ? e : e.slice(0, i), o = i < 0 ? "" : e.slice(i + 1), c = o.startsWith(" ") ? o.slice(1) : o;
648
+ if (a === "id") {
649
+ if (!/^\d+$/.test(c) || !Number.isSafeInteger(Number(c))) throw new s("conversation_stream_error", "Conversation stream contains an invalid replay cursor.");
650
+ n = Number(c);
651
+ } else a === "data" && r.push(c);
652
+ }
653
+ if (r.length === 0) return;
654
+ let i = r.join("\n");
655
+ if (!i || i === "[DONE]") return;
656
+ let o;
657
+ try {
658
+ o = JSON.parse(i);
659
+ } catch (e) {
660
+ throw new s("conversation_contract_mismatch", "Conversation stream contains invalid JSON.", { cause: e });
661
+ }
662
+ let c = k(o), l = k(c?.runtimeEvent) || k(c?.runtime_event) || (c?.schema_version === 2 ? c : null);
663
+ if (!c || c.conversationItem === void 0 && !l) {
664
+ n !== void 0 && (t.cursor = Math.max(t.cursor, n));
665
+ return;
666
+ }
667
+ if (n === void 0) throw new s("conversation_stream_error", "Canonical conversation items require an SSE replay cursor.");
668
+ let u = c.conversationItem === void 0 ? "runtime" : "native";
669
+ if (t.lane && t.lane !== u) {
670
+ t.cursor = Math.max(t.cursor, n);
671
+ return;
672
+ }
673
+ let d;
674
+ try {
675
+ d = u === "native" ? a(c.conversationItem) : t.runtime.apply(l);
676
+ } catch (e) {
677
+ throw new s("conversation_contract_mismatch", "Runtime conversation identity or lifecycle invariant failed.", { cause: e });
678
+ }
679
+ if (u === "runtime" && !d) {
680
+ t.cursor = Math.max(t.cursor, n);
681
+ return;
682
+ }
683
+ if (!d) throw new s("conversation_contract_mismatch", "Conversation stream item does not match conversation.ksadk.io/v1.");
684
+ if (t.runId && t.runId !== d.runId) throw new s("conversation_contract_mismatch", "Conversation stream changed canonical run identity.", {
685
+ runId: t.runId,
686
+ cursor: t.cursor
687
+ });
688
+ t.runId = d.runId;
689
+ let f;
690
+ try {
691
+ f = u === "runtime" ? !0 : t.reducer.apply(d);
692
+ } catch (e) {
693
+ throw new s("conversation_contract_mismatch", "Conversation identity or lifecycle invariant failed.", { cause: e });
694
+ }
695
+ t.cursor = Math.max(t.cursor, n), f && !(t.profile === "flat-v1" && d.nativeRef.parentScopeId) && t.options.onItem?.(d), t.options.onUpdate?.(be(t.reducer, t.cursor, t.runId, t.profile, t.lane === "runtime" ? t.runtime : void 0));
696
+ }
697
+ async function Se(e, t) {
698
+ if (!e.body) throw new s("conversation_stream_error", "Conversation response has no event stream body.");
699
+ let n = e.body.getReader(), r = new TextDecoder(), i = "";
700
+ try {
701
+ for (;;) {
702
+ A(t.options.signal);
703
+ let { value: e, done: a } = await n.read();
704
+ if (a) break;
705
+ i += r.decode(e, { stream: !0 });
706
+ let o = /\r?\n\r?\n/.exec(i);
707
+ for (; o?.index !== void 0;) {
708
+ let e = o.index, n = o[0].length;
709
+ xe(i.slice(0, e), t), i = i.slice(e + n), o = /\r?\n\r?\n/.exec(i);
710
+ }
711
+ }
712
+ i += r.decode(), i.trim() && xe(i, t);
713
+ } catch (e) {
714
+ throw e instanceof s ? e : (he(e, t.options.signal) && A(t.options.signal), new s("conversation_stream_error", "Conversation event stream was interrupted.", {
715
+ cause: e,
716
+ runId: t.runId || void 0,
717
+ cursor: t.cursor
718
+ }));
719
+ } finally {
720
+ n.releaseLock();
721
+ }
722
+ }
723
+ function Ce(e) {
724
+ return e.state.items.some((e) => E(e) !== void 0);
725
+ }
726
+ var we = class {
727
+ fetcher;
728
+ ingressLane;
729
+ rendererCatalog;
730
+ baseUrl;
731
+ maxReconnects;
732
+ sleep;
733
+ retryDelayMs;
734
+ constructor(e = {}) {
735
+ if (!Number.isInteger(e.maxReconnects ?? O) || (e.maxReconnects ?? O) < 0 || (e.maxReconnects ?? O) > 32) throw new s("conversation_contract_mismatch", "Conversation reconnect limit must be an integer from 0 to 32.");
736
+ this.fetcher = e.fetch, this.rendererCatalog = e.rendererCatalog, this.ingressLane = e.ingressLane, this.baseUrl = ge(e.baseUrl || ""), this.maxReconnects = e.maxReconnects ?? O, this.sleep = e.sleep || ve, this.retryDelayMs = e.retryDelayMs || _e;
737
+ }
738
+ fetch() {
739
+ if (this.fetcher) return this.fetcher;
740
+ if (typeof globalThis.fetch == "function") return globalThis.fetch.bind(globalThis);
741
+ throw new s("conversation_http_error", "No fetch implementation is available for the conversation client.");
742
+ }
743
+ url(e) {
744
+ return `${this.baseUrl}${e}`;
745
+ }
746
+ async request(e, t, n, r = "conversation_stream_error") {
747
+ A(n);
748
+ try {
749
+ return await this.fetch()(e, t);
750
+ } catch (e) {
751
+ throw e instanceof s ? e : (he(e, n) && A(n), new s(r, "Conversation network request failed.", { cause: e }));
752
+ }
753
+ }
754
+ assertOk(e) {
755
+ if (!e.ok) throw new s("conversation_http_error", `Conversation endpoint returned HTTP ${e.status}.`, { status: e.status });
756
+ }
757
+ async getSurface(e, t, n = {}) {
758
+ if (!e || !t) throw new s("conversation_contract_mismatch", "Agent and session identities are required to get a conversation surface.");
759
+ let r = n.signal ? { signal: n.signal } : void 0, i = await this.request(this.url(`/api/v1/agents/${encodeURIComponent(e)}/conversation-surface?sessionId=${encodeURIComponent(t)}`), r, n.signal, "conversation_http_error");
760
+ this.assertOk(i);
761
+ let a;
762
+ try {
763
+ a = await i.json();
764
+ } catch (e) {
765
+ throw new s("conversation_contract_mismatch", "Conversation surface response is not valid JSON.", { cause: e });
766
+ }
767
+ let o = k(a), c = l(o?.surface);
768
+ if (!o || typeof o.buildId != "string" || !o.buildId || !c) throw new s("conversation_contract_mismatch", "Conversation surface response does not match conversation.ksadk.io/v1.");
769
+ return {
770
+ buildId: o.buildId,
771
+ surface: c
772
+ };
773
+ }
774
+ async streamTurn(e) {
775
+ A(e.signal);
776
+ let t = i(e.bootstrap.surface, this.rendererCatalog), n = {
777
+ ...e.input,
778
+ ...t === "agent-block-v1" || e.input.extensions?.["ksadk.presentation"] ? { extensions: {
779
+ ...e.input.extensions,
780
+ "ksadk.presentation": { profile: t }
781
+ } } : {}
782
+ };
783
+ r(e.bootstrap.surface, n);
784
+ let a = e.bootstrap.surface.outputs.find((e) => e.name === "agent.block"), o = a?.mode === "native" ? "native" : a?.mode === "translated" ? "runtime" : void 0;
785
+ if (o && this.ingressLane && o !== this.ingressLane) throw new s("conversation_contract_mismatch", "Configured ingress lane disagrees with the conversation surface.");
786
+ let l = o || this.ingressLane || "native", u = new c(), d = {
787
+ profile: t,
788
+ lane: l,
789
+ runtime: new S(n.sessionId, u, t),
790
+ cursor: 0,
791
+ runId: "",
792
+ reducer: u,
793
+ options: e
794
+ }, f = {
795
+ method: "POST",
796
+ headers: {
797
+ "Content-Type": "application/json",
798
+ "Idempotency-Key": e.input.idempotencyKey
799
+ },
800
+ body: JSON.stringify({ input: n }),
801
+ ...e.signal ? { signal: e.signal } : {}
802
+ }, p = await this.request(this.url(`/api/v1/builds/${encodeURIComponent(e.bootstrap.buildId)}/conversation:stream`), f, e.signal);
803
+ this.assertOk(p);
804
+ try {
805
+ await Se(p, d);
806
+ } catch (e) {
807
+ if (!(e instanceof s) || e.code !== "conversation_stream_error" || !d.runId) throw e;
808
+ }
809
+ let m = be(d.reducer, d.cursor, d.runId, d.profile, d.lane === "runtime" ? d.runtime : void 0);
810
+ if (Ce(m)) return m;
811
+ if (!d.runId) throw new s("conversation_run_identity_missing", "The stream ended before a canonical item supplied its run identity.", { cursor: d.cursor });
812
+ for (let t = 1; t <= this.maxReconnects; t += 1) {
813
+ await ye(this.sleep, this.retryDelayMs(t), e.signal), A(e.signal);
814
+ let n;
815
+ try {
816
+ n = await this.request(this.url(`/api/v1/runs/${encodeURIComponent(d.runId)}/events?after=${d.cursor}${d.profile === "agent-block-v1" ? "&presentationProfile=agent-block-v1" : ""}`), {
817
+ method: "GET",
818
+ headers: { "Last-Event-ID": String(d.cursor) },
819
+ ...e.signal ? { signal: e.signal } : {}
820
+ }, e.signal), this.assertOk(n), await Se(n, d);
821
+ } catch (e) {
822
+ if (e instanceof s && e.code === "conversation_aborted" || e instanceof s && e.code === "conversation_contract_mismatch" || e instanceof s && e.code === "conversation_http_error" && e.status !== void 0 && e.status < 500) throw e;
823
+ continue;
824
+ }
825
+ if (m = be(d.reducer, d.cursor, d.runId, d.profile, d.lane === "runtime" ? d.runtime : void 0), Ce(m)) return m;
826
+ }
827
+ throw new s("conversation_reconnect_exhausted", "Conversation replay stopped after the configured reconnect limit.", {
828
+ runId: d.runId,
829
+ cursor: d.cursor
830
+ });
831
+ }
832
+ };
833
+ //#endregion
834
+ //#region src/core/conversation/renderer-registry.ts
835
+ function Te(e, t) {
836
+ if (!e.trim()) throw Error(`trusted conversation renderer ${t} must not be empty`);
837
+ }
838
+ function Ee(e) {
839
+ let t = /* @__PURE__ */ new Map();
840
+ for (let n of e) {
841
+ if (Te(n.id, "id"), Te(n.schemaRef, "schemaRef"), !n.kinds.length) throw Error("trusted conversation renderer kinds must not be empty");
842
+ if (t.has(n.schemaRef)) throw Error(`duplicate trusted conversation renderer schemaRef: ${n.schemaRef}`);
843
+ t.set(n.schemaRef, Object.freeze({
844
+ ...n,
845
+ kinds: Object.freeze([...n.kinds])
846
+ }));
847
+ }
848
+ let n = Object.freeze([...t.values()]);
849
+ return Object.freeze({
850
+ resolve(e) {
851
+ let n = t.get(e.payloadSchemaRef);
852
+ return n?.kinds.includes(e.kind) ? n : void 0;
853
+ },
854
+ entries() {
855
+ return n;
856
+ }
857
+ });
858
+ }
859
+ var De = Ee([{
860
+ id: "ksadk.agent-block",
861
+ schemaRef: "conversation.item.agent/v1",
862
+ kinds: ["agent"]
863
+ }]);
864
+ //#endregion
865
+ //#region src/utils/history-tool-facts.ts
866
+ function j(e, t) {
867
+ let n = (e) => e === "completed" || e === "error", r = t.output === void 0 ? e.output : t.output;
868
+ return {
869
+ args: t.args || e.args,
870
+ ...r === void 0 ? {} : { output: r },
871
+ status: n(e.status) && !n(t.status) ? e.status : t.status
872
+ };
873
+ }
874
+ function M(e) {
875
+ let t = e.map((e) => ({
876
+ ...e,
877
+ tools: e.tools ? { ...e.tools } : void 0
878
+ })), n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Map(), i = /* @__PURE__ */ new Map(), a = /* @__PURE__ */ new Set();
879
+ for (let e of t) {
880
+ if (!e.invocationId) continue;
881
+ let t = /* @__PURE__ */ new Set();
882
+ for (let [n, o] of Object.entries(e.tools || {})) {
883
+ if (!o.callId || o.approvalRequestId) continue;
884
+ let s = JSON.stringify([e.invocationId, o.callId]);
885
+ if (t.has(s) && a.add(s), t.add(s), e.scopeId) {
886
+ let t = i.get(s) || /* @__PURE__ */ new Set();
887
+ t.add(e.scopeId), i.set(s, t);
888
+ }
889
+ e.itemId && r.set(s, [...r.get(s) || [], {
890
+ message: e,
891
+ key: n
892
+ }]);
893
+ }
894
+ }
895
+ let o = (e, t, n = !1) => {
896
+ let r = e.message.tools[e.key], i = n ? {
897
+ ...t,
898
+ ...r,
899
+ ...j(t, r)
900
+ } : {
901
+ ...r,
902
+ ...t,
903
+ ...j(r, t)
904
+ };
905
+ e.message.tools[e.key] = i, e.message.blocks = e.message.blocks?.map((e) => e.type === "tool" && e.extra?.callId === t.callId ? {
906
+ ...e,
907
+ args: i.args,
908
+ output: i.output,
909
+ status: i.status
910
+ } : e);
911
+ }, s = (e, t, n) => {
912
+ delete e.tools[t], e.blocks = e.blocks?.filter((e) => e.type !== "tool" || e.extra?.callId !== n);
913
+ }, c = !1;
914
+ for (let e of [...t].sort((e, t) => (e.historyOrder?.seq ?? 0) - (t.historyOrder?.seq ?? 0))) if (!(e.itemId || !e.invocationId)) for (let [t, l] of Object.entries(e.tools || {})) {
915
+ if (!l.callId || l.approvalRequestId) continue;
916
+ let u = JSON.stringify([e.invocationId, l.callId]);
917
+ if (a.has(u)) continue;
918
+ let d = (r.get(u) || []).filter((t) => !e.scopeId || t.message.scopeId === e.scopeId);
919
+ if (d.length > 1) continue;
920
+ if (d.length === 1) {
921
+ o(d[0], l, !0), s(e, t, l.callId), c = !0;
922
+ continue;
923
+ }
924
+ let f = i.get(u), p = e.scopeId || (f?.size === 1 ? [...f][0] : ""), m = JSON.stringify([
925
+ e.invocationId,
926
+ p,
927
+ l.callId
928
+ ]), h = n.get(m);
929
+ if (!h) {
930
+ n.set(m, {
931
+ message: e,
932
+ key: t
933
+ });
934
+ continue;
935
+ }
936
+ o(h, l), s(e, t, l.callId), c = !0;
937
+ }
938
+ return c ? t : e;
939
+ }
940
+ //#endregion
941
+ //#region src/utils/transcript-order.ts
942
+ var N = (e) => e.runId || e.invocationId;
943
+ function P(e) {
944
+ let t = /* @__PURE__ */ new Map();
945
+ for (let n of e) {
946
+ let e = N(n), r = n.historyOrder?.group;
947
+ e && r !== void 0 && t.set(e, Math.min(r, t.get(e) ?? r));
948
+ }
949
+ let n = e.map((e, n) => ({
950
+ message: e,
951
+ index: n,
952
+ seq: e.historyOrder?.seq,
953
+ group: e.historyOrder?.group ?? t.get(N(e) || "") ?? e.historyOrder?.seq,
954
+ part: e.historyOrder?.index || 0
955
+ })), r, i = n.map(() => void 0);
956
+ for (let e = n.length - 1; e >= 0; e--) i[e] = r, n[e].group !== void 0 && (r = n[e].group);
957
+ let a;
958
+ return n.map((e) => e.group === void 0 ? {
959
+ ...e,
960
+ anchor: a ?? i[e.index] ?? 0,
961
+ side: a === void 0 && i[e.index] !== void 0 ? -1 : 1
962
+ } : (a = e.group, {
963
+ ...e,
964
+ anchor: e.group,
965
+ side: 0
966
+ })).sort((e, t) => e.anchor - t.anchor || e.side - t.side || (e.side === 0 ? (e.seq ?? e.anchor) - (t.seq ?? t.anchor) : 0) || (e.side === 0 ? e.part - t.part : 0) || e.index - t.index).map((e) => e.message);
967
+ }
968
+ function Oe(e, t) {
969
+ if (!t) return !0;
970
+ let n = (n) => n === "feedback" ? !1 : n === "historyOrder" ? e.historyOrder?.seq !== t.historyOrder?.seq || e.historyOrder?.index !== t.historyOrder?.index || e.historyOrder?.group !== t.historyOrder?.group : e[n] !== t[n];
971
+ return Object.keys(e).some(n) || Object.keys(t).some(n);
972
+ }
973
+ function ke(e, t, n) {
974
+ let r = new Map(t.map((e) => [e.id, e])), i = n.filter((e) => r.has(e.id) && r.get(e.id)?.feedback !== e.feedback), a = new Map(i.map((e) => [e.id, e.feedback])), o = (e) => JSON.stringify([N(e), e.responseId]), s = new Map(i.filter((e) => e.responseId).map((e) => [o(e), e.feedback])), c = (e) => e.map((e) => {
975
+ if (a.has(e.id)) return {
976
+ ...e,
977
+ feedback: a.get(e.id)
978
+ };
979
+ let t = o(e);
980
+ return e.responseId && s.has(t) ? {
981
+ ...e,
982
+ feedback: s.get(t)
983
+ } : e;
984
+ }), l = n.filter((e) => Oe(e, r.get(e.id)));
985
+ if (!l.length) return te(M(c(e)));
986
+ let u = new Set(l.map(N).filter(Boolean)), d = new Set(l.map((e) => e.id)), f = n.filter((e) => u.has(N(e)) || d.has(e.id)), p = e;
987
+ for (let t of u) {
988
+ let n = f.filter((e) => N(e) === t), r = e.filter((e) => N(e) === t), i = p.findIndex((e) => N(e) === t), a = n.some((e) => e.role === "user") ? [] : r.filter((e) => e.role === "user");
989
+ p = p.filter((e) => N(e) !== t);
990
+ let o = i < 0 ? p.length : Math.min(i, p.length);
991
+ p.splice(o, 0, ...a, ...n.map((e) => ({
992
+ ...e,
993
+ historyOrder: e.historyOrder || r.find((t) => t.id === e.id)?.historyOrder
994
+ })));
995
+ }
996
+ for (let e of f.filter((e) => !N(e))) {
997
+ let t = p.findIndex((t) => t.id === e.id);
998
+ if (t < 0) {
999
+ let t = n.indexOf(e), r = n.slice(t + 1).find((e) => p.some((t) => t.id === e.id)), i = n.slice(0, t).findLast((e) => p.some((t) => t.id === e.id)), a = r ? p.findIndex((e) => e.id === r.id) : i ? p.findIndex((e) => e.id === i.id) + 1 : p.length;
1000
+ p = [
1001
+ ...p.slice(0, a),
1002
+ e,
1003
+ ...p.slice(a)
1004
+ ];
1005
+ } else p = p.map((n, r) => r === t ? {
1006
+ ...e,
1007
+ historyOrder: e.historyOrder || n.historyOrder
1008
+ } : n);
1009
+ }
1010
+ return te(M(c(P(p))));
1011
+ }
1012
+ //#endregion
1013
+ //#region src/core/conversation/hosted.ts
1014
+ var Ae = "conversation_item_v1";
1015
+ function je(e) {
1016
+ return typeof e == "object" && e && !Array.isArray(e) ? e : null;
1017
+ }
1018
+ function F(e) {
1019
+ return typeof e == "string" && e.trim() ? e : null;
1020
+ }
1021
+ function I(e) {
1022
+ if (typeof e == "string") return e;
1023
+ if (e == null) return "";
1024
+ try {
1025
+ return JSON.stringify(e, null, 2);
1026
+ } catch {
1027
+ return String(e);
1028
+ }
1029
+ }
1030
+ function L(e) {
1031
+ return {
1032
+ id: `conversation:${e.runId}:${e.itemId}`,
1033
+ timestamp: 0,
1034
+ eventType: Ae,
1035
+ eventId: e.sourceEventIds.at(-1),
1036
+ runId: e.runId,
1037
+ itemId: e.itemId,
1038
+ status: e.lifecycle === "failed" ? "failed" : e.lifecycle === "completed" ? "completed" : "running"
1039
+ };
1040
+ }
1041
+ function Me(e) {
1042
+ return e.lifecycle === "failed" ? "error" : e.lifecycle === "completed" ? "done" : "streaming";
1043
+ }
1044
+ function Ne(e) {
1045
+ let t = typeof e.payload.text == "string" ? e.payload.text : "";
1046
+ if (e.kind === "user_message") return {
1047
+ ...L(e),
1048
+ role: "user",
1049
+ content: t,
1050
+ eventType: "user_message"
1051
+ };
1052
+ let n = e.kind === "reasoning" ? {
1053
+ id: `conversation-block:${e.itemId}`,
1054
+ type: "thinking",
1055
+ content: t,
1056
+ status: Me(e),
1057
+ ...e.payload.presentationSource === "public_summary" || e.payload.presentationSource === "provider_reasoning" ? { source: e.payload.presentationSource } : {}
1058
+ } : {
1059
+ id: `conversation-block:${e.itemId}`,
1060
+ type: "text",
1061
+ content: t,
1062
+ status: Me(e)
1063
+ };
1064
+ return {
1065
+ ...L(e),
1066
+ role: "model",
1067
+ content: e.kind === "assistant_text" ? t : "",
1068
+ reasoning: e.kind === "reasoning" ? t : void 0,
1069
+ blocks: [n]
1070
+ };
1071
+ }
1072
+ function Pe(e, t = !1) {
1073
+ let n = F(e.payload.tool) || (e.payload.sourceKind === "tool_result" ? "Tool result" : "Tool"), r = F(e.payload.callId), i = I(_(e.payload.args)), a = Object.prototype.hasOwnProperty.call(e.payload, "output") ? I(v(e.payload.output, e.nativeRef.framework, e.nativeRef.protocol)) : void 0, o = {
1074
+ failed: "error",
1075
+ completed: "completed",
1076
+ running: "running",
1077
+ unknown: "unknown"
1078
+ }, s = e.payload.executionStatus, c = typeof s == "string" && Object.hasOwn(o, s) ? o[s] : e.lifecycle === "failed" || e.payload.isError === !0 ? "error" : e.lifecycle === "completed" ? "completed" : "running", l = t && c === "running" ? "unknown" : c;
1079
+ return {
1080
+ ...L(e),
1081
+ role: "model",
1082
+ content: "",
1083
+ blocks: [{
1084
+ id: `conversation-block:${e.itemId}`,
1085
+ type: "tool",
1086
+ toolName: n,
1087
+ args: i,
1088
+ output: a,
1089
+ status: l,
1090
+ ...r ? { extra: { callId: r } } : {}
1091
+ }],
1092
+ tools: { [n]: {
1093
+ name: n,
1094
+ ...r ? { callId: r } : {},
1095
+ args: i,
1096
+ output: a,
1097
+ status: l
1098
+ } }
1099
+ };
1100
+ }
1101
+ function Fe(e) {
1102
+ return e === "structured_input" ? "structured_input" : e === "plan_review" ? "plan_review" : e === "custom" ? "custom" : "approval";
1103
+ }
1104
+ function Ie(e) {
1105
+ let n = F(e.payload.interactionId), r = e.payload.revision;
1106
+ if (!n || !Number.isInteger(r) || Number(r) < 1) return null;
1107
+ let i = I(e.payload.detail), a = F(e.payload.prompt) || i, o = Fe(e.payload.interactionKind), s = e.lifecycle === "completed", c = {};
1108
+ if (s && Object.hasOwn(e.payload, "response")) try {
1109
+ c = { response: t(e.payload.response) };
1110
+ } catch {}
1111
+ return {
1112
+ interactionId: n,
1113
+ sessionId: e.sessionId,
1114
+ runId: e.runId,
1115
+ kind: o,
1116
+ title: F(e.payload.title) || (o === "approval" ? `审批:${F(e.payload.kind) || "操作"}` : "需要补充信息"),
1117
+ message: a || "运行需要人工确认。",
1118
+ requestSchema: je(e.payload.inputSchema),
1119
+ presentation: null,
1120
+ status: s ? "resolved" : "pending",
1121
+ revision: Number(r),
1122
+ createdAt: F(e.payload.createdAt) || "",
1123
+ expiresAt: F(e.payload.expiresAt),
1124
+ resolvedAt: s ? F(e.payload.resolvedAt) : null,
1125
+ actor: s ? F(e.payload.actor) : null,
1126
+ outcome: s && [
1127
+ "approved",
1128
+ "rejected",
1129
+ "submitted",
1130
+ "cancelled",
1131
+ "expired"
1132
+ ].includes(String(e.payload.outcome || "")) ? e.payload.outcome : null,
1133
+ responseSummary: s ? F(e.payload.responseSummary) : null,
1134
+ ...c,
1135
+ source: "interaction_v1",
1136
+ extensions: {
1137
+ conversation_item_id: e.itemId,
1138
+ ...F(e.payload.callId) ? { call_id: F(e.payload.callId) } : {}
1139
+ }
1140
+ };
1141
+ }
1142
+ function Le(e, t) {
1143
+ let n = F(e.payload.kind) || "approval", r = I(e.payload.detail), i = t.status === "resolved" ? t.outcome === "rejected" ? "rejected" : t.outcome === "cancelled" || t.outcome === "expired" ? "cancelled" : "approved" : "pending", a = t.status === "pending" ? "paused" : "completed", o = {
1144
+ approvalRequestId: t.interactionId,
1145
+ approvalStatus: i,
1146
+ approvalMessage: t.message
1147
+ };
1148
+ return {
1149
+ ...L(e),
1150
+ role: "model",
1151
+ content: "",
1152
+ blocks: [{
1153
+ id: `conversation-block:${e.itemId}`,
1154
+ type: "tool",
1155
+ toolName: n,
1156
+ args: r,
1157
+ status: a,
1158
+ extra: o
1159
+ }],
1160
+ tools: { [n]: {
1161
+ name: n,
1162
+ args: r,
1163
+ status: a,
1164
+ approvalRequestId: t.interactionId,
1165
+ approvalStatus: i,
1166
+ approvalMessage: t.message
1167
+ } }
1168
+ };
1169
+ }
1170
+ function Re(e) {
1171
+ for (let t of [
1172
+ "createSurface",
1173
+ "updateComponents",
1174
+ "updateDataModel",
1175
+ "deleteSurface"
1176
+ ]) {
1177
+ let n = F(je(e[t])?.surfaceId);
1178
+ if (n) return n;
1179
+ }
1180
+ return null;
1181
+ }
1182
+ function ze(e) {
1183
+ let t = Array.isArray(e.payload.data) ? e.payload.data : Array.isArray(e.payload.operations) ? e.payload.operations : null;
1184
+ if (!t) return null;
1185
+ let n = t.map(je);
1186
+ if (n.some((e) => e === null)) return null;
1187
+ let r = n, i = F(e.payload.surfaceId) || r.map(Re).find((e) => e !== null) || null;
1188
+ return i ? {
1189
+ ...L(e),
1190
+ role: "a2ui",
1191
+ content: "",
1192
+ aguiActivity: {
1193
+ surfaceId: i,
1194
+ messages: r,
1195
+ ...typeof e.payload.interactionId == "string" ? { interactionId: e.payload.interactionId } : {}
1196
+ }
1197
+ } : null;
1198
+ }
1199
+ function R(e, t, n, r = !1) {
1200
+ return {
1201
+ ...L(e),
1202
+ role: "system",
1203
+ content: n ? `${t}: ${n}` : t,
1204
+ status: r ? "failed" : L(e).status
1205
+ };
1206
+ }
1207
+ function Be(e) {
1208
+ for (let { item: t } of [...e].reverse()) {
1209
+ if (t.visibility !== "public" || t.kind !== "assistant_text" || t.payloadSchemaRef !== "conversation.item.assistant_text/v1" || typeof t.payload.text != "string") continue;
1210
+ let e = _(t.payload.text);
1211
+ if (typeof e != "string" || e === "[redacted]") continue;
1212
+ let n = e.replace(/\s+/g, " ").trim();
1213
+ if (!n) continue;
1214
+ let r = Array.from(n);
1215
+ return r.length > 160 ? `${r.slice(0, 160).join("")}…` : n;
1216
+ }
1217
+ }
1218
+ function z(e) {
1219
+ let t = e.presentation, n = new Map(t.textItems.map((e) => [e.id, e])), r = new Set(t.toolItems.map((e) => e.itemId)), i = new Set(t.approvalItems.map((e) => e.itemId)), a = new Set(t.structuredInputItems.map((e) => e.itemId)), o = new Set(t.a2uiItems.map((e) => e.itemId)), s = new Map(t.artifacts.map((e) => [e.id, e])), c = new Map(t.fallbacks.map((e) => [e.id, e])), l = [], u = [];
1220
+ for (let d of t.timeline) {
1221
+ let { item: f } = d;
1222
+ if (f.kind === "agent") {
1223
+ if (!d.children) {
1224
+ l.push(R(f, "Remote agent", String(f.payload.status || "submitted")));
1225
+ continue;
1226
+ }
1227
+ let n = z({
1228
+ ...e,
1229
+ presentation: {
1230
+ ...t,
1231
+ timeline: d.children || []
1232
+ }
1233
+ });
1234
+ l.push({
1235
+ ...L(f),
1236
+ role: "model",
1237
+ content: "",
1238
+ agentBlock: {
1239
+ item: f,
1240
+ messages: n.messages,
1241
+ summary: Be(d.children || [])
1242
+ },
1243
+ status: f.payload.status === "cancelled" ? "cancelled" : L(f).status
1244
+ }), u.push(...n.interactions);
1245
+ continue;
1246
+ }
1247
+ if (n.has(f.itemId)) {
1248
+ l.push(Ne(f));
1249
+ continue;
1250
+ }
1251
+ if (r.has(f.itemId)) {
1252
+ l.push(Pe(f, !!t.terminalStatus && f.runId === e.runId));
1253
+ continue;
1254
+ }
1255
+ if (i.has(f.itemId) || a.has(f.itemId)) {
1256
+ let e = Ie(f);
1257
+ e ? (u.push(e), l.push(Le(f, e))) : l.push(R(f, "Interaction unavailable", "The server did not provide a durable interaction revision; this card is read-only."));
1258
+ continue;
1259
+ }
1260
+ if (o.has(f.itemId)) {
1261
+ let e = ze(f);
1262
+ l.push(e || R(f, "Unsupported content", "A2UI payload is not a valid passive operation list."));
1263
+ continue;
1264
+ }
1265
+ let p = s.get(f.itemId);
1266
+ if (p) {
1267
+ l.push(p.status === "pending" ? {
1268
+ ...L(f),
1269
+ role: "model",
1270
+ content: p.name,
1271
+ attachments: [{
1272
+ name: p.name,
1273
+ type: p.mimeType,
1274
+ url: "",
1275
+ artifactId: p.artifactId,
1276
+ itemId: p.itemId,
1277
+ runId: p.runId,
1278
+ sizeBytes: p.sizeBytes,
1279
+ status: "pending"
1280
+ }]
1281
+ } : p.status === "ready" && p.uri ? {
1282
+ ...L(f),
1283
+ role: "model",
1284
+ content: p.name,
1285
+ attachments: [{
1286
+ name: p.name,
1287
+ type: p.mimeType,
1288
+ url: p.uri,
1289
+ artifactId: p.artifactId,
1290
+ itemId: p.itemId,
1291
+ runId: p.runId,
1292
+ sizeBytes: p.sizeBytes,
1293
+ status: p.status
1294
+ }]
1295
+ } : R(f, "Artifact unavailable", p.status === "failed" ? "The artifact failed to generate or its URI is not a safe HTTP(S) link." : "The artifact URI is not a safe HTTP(S) link."));
1296
+ continue;
1297
+ }
1298
+ let m = c.get(f.itemId);
1299
+ if (m) {
1300
+ if (f.kind === "error" && ["cancelled", "canceled"].includes(String(f.payload.status || "").toLowerCase())) continue;
1301
+ l.push(R(f, m.title, m.detail, m.failed));
1302
+ }
1303
+ }
1304
+ return {
1305
+ messages: l,
1306
+ interactions: u
1307
+ };
1308
+ }
1309
+ function Ve(e, t, n) {
1310
+ let r = z(t).messages;
1311
+ if (!r.length) return e;
1312
+ let i = e.filter((e) => e.eventType !== "optimistic_assistant_placeholder"), a = (e) => e.runId === t.runId || e.invocationId === t.runId, o = i.findIndex(a), s = n ? i.findIndex((e) => e.id === n) : o < 0 ? i.findLastIndex((e) => e.role === "user" && e.eventType === "optimistic_user_message") : i.findIndex((e) => e.role === "user" && a(e)), c = i[s], l = i.filter((e, t) => t !== s && !a(e)), u = [s, o].filter((e) => e >= 0), d = u.length ? Math.min(...u) : l.length, f = c && !r.some((e) => e.role === "user") ? [{
1313
+ ...c,
1314
+ invocationId: t.runId,
1315
+ eventType: void 0
1316
+ }] : [];
1317
+ return [
1318
+ ...l.slice(0, d),
1319
+ ...f,
1320
+ ...r,
1321
+ ...l.slice(d)
1322
+ ];
1323
+ }
1324
+ //#endregion
1325
+ //#region src/core/run/blocks.ts
1326
+ var He = 0;
1327
+ function B(e) {
1328
+ return He += 1, `${e}-${He}`;
1329
+ }
1330
+ function V(e) {
1331
+ return e.map((e) => e.type === "thinking" && e.status === "streaming" ? {
1332
+ ...e,
1333
+ status: "done"
1334
+ } : e);
1335
+ }
1336
+ function Ue(e, t) {
1337
+ let n = [...e ?? []], r = n[n.length - 1];
1338
+ return r?.type === "thinking" && r.status === "streaming" ? (n[n.length - 1] = {
1339
+ ...r,
1340
+ content: r.content + t
1341
+ }, n) : [...n, {
1342
+ id: B("thinking"),
1343
+ type: "thinking",
1344
+ content: t,
1345
+ status: "streaming"
1346
+ }];
1347
+ }
1348
+ function We(e, t) {
1349
+ let n = [...V(e ?? [])], r = n[n.length - 1];
1350
+ return r?.type === "text" && r.status === "streaming" ? (n[n.length - 1] = {
1351
+ ...r,
1352
+ content: r.content + t
1353
+ }, n) : [...n, {
1354
+ id: B("text"),
1355
+ type: "text",
1356
+ content: t,
1357
+ status: "streaming"
1358
+ }];
1359
+ }
1360
+ function Ge(e, t) {
1361
+ let n = e ?? [], r = n.filter((e) => e.type === "text").map((e) => e.content).join("");
1362
+ if (r === t) return n;
1363
+ let i = n.flatMap((e, t) => e.type === "text" ? [t] : []);
1364
+ if (!i.length) return [...n, {
1365
+ id: B("text"),
1366
+ type: "text",
1367
+ content: t,
1368
+ status: "streaming"
1369
+ }];
1370
+ let a = 0;
1371
+ for (; a < r.length && a < t.length && r[a] === t[a];) a++;
1372
+ let o = 0;
1373
+ for (; o < r.length - a && o < t.length - a && r[r.length - o - 1] === t[t.length - o - 1];) o++;
1374
+ let s = r.length - o, c = t.slice(a, t.length - o), l = 0, u = i.find((e) => {
1375
+ let t = n[e];
1376
+ return l += t.content.length, l > a;
1377
+ }) ?? i[i.length - 1];
1378
+ return l = 0, n.map((e, t) => {
1379
+ if (e.type !== "text") return e;
1380
+ let n = l;
1381
+ l += e.content.length;
1382
+ let r = e.content.slice(0, Math.max(0, Math.min(e.content.length, a - n))), i = e.content.slice(Math.max(0, Math.min(e.content.length, s - n))), o = r + (t === u ? c : "") + i;
1383
+ return o === e.content ? e : {
1384
+ ...e,
1385
+ content: o
1386
+ };
1387
+ });
1388
+ }
1389
+ function Ke(e, t) {
1390
+ let n = V(e ?? []);
1391
+ if (n.filter((e) => e.type === "text").map((e) => e.content).join("") === t) return n.map((e) => e.type === "text" && e.status === "streaming" ? {
1392
+ ...e,
1393
+ status: "done"
1394
+ } : e);
1395
+ let r = [...n], i = r.map((e) => e.type).lastIndexOf("text");
1396
+ return i >= 0 && r[i].type === "text" ? (r[i] = {
1397
+ ...r[i],
1398
+ content: t,
1399
+ status: "done"
1400
+ }, r) : [...r, {
1401
+ id: B("text"),
1402
+ type: "text",
1403
+ content: t,
1404
+ status: "done"
1405
+ }];
1406
+ }
1407
+ function qe(e, t) {
1408
+ if (!t) return [...e ?? []];
1409
+ let n = [...e ?? []], r = n[n.length - 1];
1410
+ if (r?.type !== "thinking" || !r.content.endsWith(t)) return n;
1411
+ let i = r.content.slice(0, -t.length);
1412
+ return i ? (n[n.length - 1] = {
1413
+ ...r,
1414
+ content: i
1415
+ }, n) : (n.pop(), n);
1416
+ }
1417
+ function Je(e, t, n) {
1418
+ let r = [...V(e ?? [])], i = r.findIndex((e) => e.type === "tool" && e.toolName === t);
1419
+ if (i >= 0 && r[i].type === "tool") {
1420
+ let e = r[i], t = e.status === "error" || e.status === "completed", a = n.output !== void 0, o = n.extra && (n.extra.approvalRequestId || n.extra.approvalStatus);
1421
+ if (t && !a && !o) return r;
1422
+ let s = n.extra ?? e.extra;
1423
+ return r[i] = {
1424
+ ...e,
1425
+ ...n,
1426
+ extra: s
1427
+ }, r;
1428
+ }
1429
+ return [...r, {
1430
+ id: B("tool"),
1431
+ type: "tool",
1432
+ toolName: t,
1433
+ args: n.args ?? "",
1434
+ output: n.output,
1435
+ status: n.status ?? "running",
1436
+ extra: n.extra
1437
+ }];
1438
+ }
1439
+ function Ye(e) {
1440
+ return V(e ?? []);
1441
+ }
1442
+ function Xe(e) {
1443
+ let t = [];
1444
+ if (e.reasoning && e.reasoning.trim() && t.push({
1445
+ id: B("thinking"),
1446
+ type: "thinking",
1447
+ content: e.reasoning,
1448
+ status: "done",
1449
+ ...e.reasoningSource ? { source: e.reasoningSource } : {}
1450
+ }), e.tools) for (let n of Object.values(e.tools)) t.push({
1451
+ id: B("tool"),
1452
+ type: "tool",
1453
+ toolName: n.name,
1454
+ args: n.args ?? "",
1455
+ output: n.output,
1456
+ status: n.status ?? "completed",
1457
+ ...n.callId || n.approvalRequestId ? { extra: {
1458
+ ...n.callId ? { callId: n.callId } : {},
1459
+ ...n.approvalRequestId ? { approvalRequestId: n.approvalRequestId } : {}
1460
+ } } : {}
1461
+ });
1462
+ return e.content && e.content.trim() && t.push({
1463
+ id: B("text"),
1464
+ type: "text",
1465
+ content: e.content,
1466
+ status: "done"
1467
+ }), t;
1468
+ }
1469
+ //#endregion
1470
+ //#region src/core/stream/kernel-events.ts
1471
+ var Ze = {
1472
+ "run.started": "in_progress",
1473
+ "run.progress": "in_progress",
1474
+ "run.completed": "completed",
1475
+ "run.failed": "failed",
1476
+ "run.canceled": "cancelled",
1477
+ "run.interrupted": "interrupted"
1478
+ }, Qe = new Set([
1479
+ "completed",
1480
+ "failed",
1481
+ "cancelled",
1482
+ "canceled"
1483
+ ]);
1484
+ function H(e) {
1485
+ return typeof e == "object" && e && !Array.isArray(e) ? e : null;
1486
+ }
1487
+ function $e(e) {
1488
+ let t = H(e.source), n = t ? H(t.metadata) : null;
1489
+ return String(n?.native_item_kind || "");
1490
+ }
1491
+ function U(e, t) {
1492
+ let n = H(e[t]);
1493
+ if (!n) return [];
1494
+ let r = n?.parts;
1495
+ return Array.isArray(r) ? r : t === "update" ? [n] : [];
1496
+ }
1497
+ function et(e) {
1498
+ let t = H(e);
1499
+ if (!t) return "";
1500
+ if (typeof t.text == "string") return t.text;
1501
+ let n = H(t.data);
1502
+ if (n) {
1503
+ let e = n.content;
1504
+ if (Array.isArray(e)) return e.map((e) => String(H(e)?.text || "")).join("");
1505
+ for (let e of [
1506
+ "text",
1507
+ "output",
1508
+ "stdout",
1509
+ "stderr"
1510
+ ]) if (n[e] != null) return String(n[e]);
1511
+ }
1512
+ return "";
1513
+ }
1514
+ function W(e) {
1515
+ return e.map(et).join("");
1516
+ }
1517
+ function tt(e) {
1518
+ return e.some((e) => H(H(e)?.data)?.type === "userMessage");
1519
+ }
1520
+ function G(e) {
1521
+ if (e.length === 1) {
1522
+ let t = H(H(e[0])?.data);
1523
+ if (t) return t;
1524
+ }
1525
+ return W(e) || null;
1526
+ }
1527
+ function nt(e) {
1528
+ let t = H(e);
1529
+ return t ? H(t.data) || t : null;
1530
+ }
1531
+ function rt(e) {
1532
+ let t = nt(e);
1533
+ return String(t?.content_type || t?.type || "").trim().toLowerCase();
1534
+ }
1535
+ function it(e) {
1536
+ for (let t of e) {
1537
+ let e = nt(t);
1538
+ if (!e || rt(t) !== "tool_call") continue;
1539
+ let n = String(e.call_id || "").trim(), r = String(e.name || e.tool_name || "").trim();
1540
+ if (!(!n && !r)) return {
1541
+ callId: n,
1542
+ name: r,
1543
+ arguments: e.arguments ?? e.args ?? {}
1544
+ };
1545
+ }
1546
+ return null;
1547
+ }
1548
+ function at(e) {
1549
+ for (let t of e) {
1550
+ let e = nt(t);
1551
+ if (!e || rt(t) !== "tool_result") continue;
1552
+ let n = String(e.call_id || e.tool_call_id || "").trim(), r = e.is_error === !0 || e.error != null;
1553
+ return {
1554
+ callId: n,
1555
+ result: e.result ?? e.output ?? e.error ?? null,
1556
+ isError: r
1557
+ };
1558
+ }
1559
+ return null;
1560
+ }
1561
+ function K(e, t, n, r) {
1562
+ let i = String(e.item_id || ""), a = String(r || i);
1563
+ return { RuntimeItem: {
1564
+ RunId: String(e.run_id || ""),
1565
+ ScopeId: String(e.scope_id || e.run_id || ""),
1566
+ ItemId: a,
1567
+ ...n ? { PartId: n } : {},
1568
+ Operation: t,
1569
+ SourceEventId: String(e.event_id || ""),
1570
+ ...i && i !== a ? { SourceItemId: i } : {}
1571
+ } };
1572
+ }
1573
+ var ot = class {
1574
+ textByPart = /* @__PURE__ */ new Map();
1575
+ toolNameByCallId = /* @__PURE__ */ new Map();
1576
+ toolCallByItemId = /* @__PURE__ */ new Map();
1577
+ toolResultByItemId = /* @__PURE__ */ new Map();
1578
+ sessionId;
1579
+ constructor(e) {
1580
+ this.sessionId = e;
1581
+ }
1582
+ translate(e) {
1583
+ if (!H(e)) return null;
1584
+ let t = Number(e.seq);
1585
+ if (!Number.isFinite(t) || t <= 0) return null;
1586
+ let n = String(e.family || ""), r = String(e.event_type || ""), i = String(e.run_id || ""), a = () => ({
1587
+ EventId: e.event_id ? String(e.event_id) : `kernel-${t}`,
1588
+ SessionId: this.sessionId,
1589
+ InvocationId: i || void 0,
1590
+ SeqId: t
1591
+ });
1592
+ if (n === "interaction") return {
1593
+ ...a(),
1594
+ EventType: r,
1595
+ payload: e
1596
+ };
1597
+ if (n === "control") {
1598
+ if (r === "control.command_rejected") return {
1599
+ ...a(),
1600
+ EventType: r,
1601
+ Content: e
1602
+ };
1603
+ if (r === "control.run_transition") {
1604
+ let t = String(e.state || "").toLowerCase();
1605
+ if (Qe.has(t)) return {
1606
+ ...a(),
1607
+ EventType: "run_status",
1608
+ Content: { status: t }
1609
+ };
1610
+ }
1611
+ return null;
1612
+ }
1613
+ if (n !== "runtime") return null;
1614
+ let o = H(H(e.source)?.metadata), s = o?.conversation_visibility ?? o?.visibility;
1615
+ if (r.startsWith("item.") && ["internal", "hidden"].includes(String(s))) return null;
1616
+ let c = Ze[r];
1617
+ if (c) return {
1618
+ ...a(),
1619
+ EventType: "run_status",
1620
+ Content: { status: c }
1621
+ };
1622
+ let l = $e(e);
1623
+ if (r === "item.started") {
1624
+ let t = U(e, "initial");
1625
+ if (e.item_kind === "tool_call") {
1626
+ let n = it(t);
1627
+ if (!n) return null;
1628
+ let r = n?.callId || String(e.item_id || ""), o = n?.name || "tool";
1629
+ return r && this.toolNameByCallId.set(r, o), e.item_id && this.toolCallByItemId.set(String(e.item_id), {
1630
+ callId: r,
1631
+ name: o
1632
+ }), {
1633
+ ...a(),
1634
+ EventType: "tool_call",
1635
+ Metadata: {
1636
+ ...K(e, "replace", void 0, r),
1637
+ call_id: r,
1638
+ tool_name: o,
1639
+ run_id: i,
1640
+ tool_args: n?.arguments ?? G(t)
1641
+ }
1642
+ };
1643
+ }
1644
+ return e.item_kind === "tool_result" || l === "userMessage" || tt(t) || l === "reasoning" || e.item_kind === "reasoning" ? null : l === "agentMessage" || !l && e.item_kind === "message" ? {
1645
+ ...a(),
1646
+ EventType: "assistant_stream_snapshot",
1647
+ Content: { parts: [{ text: "" }] },
1648
+ Metadata: K(e, "replace")
1649
+ } : l && l !== "notification" ? {
1650
+ ...a(),
1651
+ EventType: "tool_call",
1652
+ Metadata: {
1653
+ ...K(e, "replace"),
1654
+ call_id: String(e.item_id || ""),
1655
+ tool_name: l,
1656
+ run_id: i,
1657
+ tool_args: G(t)
1658
+ }
1659
+ } : null;
1660
+ }
1661
+ if (r === "item.updated") {
1662
+ let n = U(e, "update");
1663
+ if (e.item_kind === "tool_call") {
1664
+ let t = at(n);
1665
+ if (!t) return null;
1666
+ let r = this.toolCallByItemId.get(String(e.item_id || "")), o = t.callId || r?.callId || String(e.item_id || ""), s = r?.name || this.toolNameByCallId.get(o) || "tool", c = t.isError ? { error: t.result } : t.result;
1667
+ return e.item_id && this.toolResultByItemId.set(String(e.item_id), c), {
1668
+ ...a(),
1669
+ EventType: "tool_result",
1670
+ Metadata: {
1671
+ ...K(e, "completed", void 0, o),
1672
+ call_id: o,
1673
+ tool_name: s,
1674
+ run_id: i,
1675
+ tool_output: c
1676
+ }
1677
+ };
1678
+ }
1679
+ let r = H(n[0]), o = String(r?.part_id || e.item_id || t), s = String(e.op || "replace"), c = W(n), u = this.textByPart.get(o) || "", d = s === "append" ? u + c : c;
1680
+ return this.textByPart.set(o, d), l === "agentMessage" || !l && e.item_kind === "message" ? {
1681
+ ...a(),
1682
+ EventType: "assistant_stream_snapshot",
1683
+ Content: { parts: [{ text: d }] },
1684
+ Metadata: K(e, "replace", o)
1685
+ } : l === "reasoning" || e.item_kind === "reasoning" ? {
1686
+ ...a(),
1687
+ EventType: "reasoning",
1688
+ Content: { parts: [{ text: c }] },
1689
+ Metadata: K(e, s === "append" ? "append" : "replace", o)
1690
+ } : null;
1691
+ }
1692
+ if (r === "item.snapshot_replaced") {
1693
+ let n = U(e, "snapshot"), r = String(H(n[0])?.part_id || e.item_id || t), i = W(n);
1694
+ return this.textByPart.set(r, i), l === "agentMessage" || !l && e.item_kind === "message" ? {
1695
+ ...a(),
1696
+ EventType: "assistant_stream_snapshot",
1697
+ Content: { parts: [{ text: i }] },
1698
+ Metadata: K(e, "replace", r)
1699
+ } : l === "reasoning" || e.item_kind === "reasoning" ? {
1700
+ ...a(),
1701
+ EventType: "reasoning",
1702
+ Content: { parts: [{ text: i }] },
1703
+ Metadata: K(e, "replace", r)
1704
+ } : null;
1705
+ }
1706
+ if (r === "item.completed") {
1707
+ let n = U(e, "snapshot");
1708
+ if (e.item_kind === "tool_call") {
1709
+ let t = it(n), r = at(n), o = this.toolCallByItemId.get(String(e.item_id || "")), s = r?.callId || t?.callId || o?.callId || String(e.item_id || ""), c = t?.name || o?.name || this.toolNameByCallId.get(s) || "tool";
1710
+ if (s && this.toolNameByCallId.set(s, c), r) {
1711
+ let t = r.isError ? { error: r.result } : r.result;
1712
+ return {
1713
+ ...a(),
1714
+ EventType: "tool_result",
1715
+ Metadata: {
1716
+ ...K(e, "completed", void 0, s),
1717
+ call_id: s,
1718
+ tool_name: c,
1719
+ run_id: i,
1720
+ tool_output: t
1721
+ }
1722
+ };
1723
+ }
1724
+ return {
1725
+ ...a(),
1726
+ EventType: "tool_call",
1727
+ Metadata: {
1728
+ ...K(e, "replace", void 0, s),
1729
+ call_id: s,
1730
+ tool_name: c,
1731
+ run_id: i,
1732
+ tool_args: t?.arguments ?? G(n)
1733
+ }
1734
+ };
1735
+ }
1736
+ if (e.item_kind === "tool_result") {
1737
+ let t = at(n), r = t?.callId || String(e.item_id || ""), o = this.toolNameByCallId.get(r) || l || "tool", s = t?.isError ? { error: t.result } : t?.result ?? G(n);
1738
+ return {
1739
+ ...a(),
1740
+ EventType: "tool_result",
1741
+ Metadata: {
1742
+ ...K(e, "completed", void 0, r),
1743
+ call_id: r,
1744
+ tool_name: o,
1745
+ run_id: i,
1746
+ tool_output: s
1747
+ }
1748
+ };
1749
+ }
1750
+ if (l === "userMessage" || tt(n)) return {
1751
+ ...a(),
1752
+ EventType: "user_message",
1753
+ Content: { parts: [{ text: W(n) }] }
1754
+ };
1755
+ if (l === "agentMessage" || !l && e.item_kind === "message") {
1756
+ let r = W(n), i = String(H(n[0])?.part_id || e.item_id || t);
1757
+ return this.textByPart.set(i, r), {
1758
+ ...a(),
1759
+ EventType: "assistant_message",
1760
+ Content: { parts: [{ text: r }] },
1761
+ Metadata: K(e, "completed", i)
1762
+ };
1763
+ }
1764
+ if (l === "reasoning" || e.item_kind === "reasoning") {
1765
+ let r = String(H(n[0])?.part_id || e.item_id || t);
1766
+ return {
1767
+ ...a(),
1768
+ EventType: "reasoning",
1769
+ Content: { parts: [{ text: W(n) }] },
1770
+ Metadata: K(e, "completed", r)
1771
+ };
1772
+ }
1773
+ return l && l !== "notification" ? {
1774
+ ...a(),
1775
+ EventType: "tool_result",
1776
+ Metadata: {
1777
+ ...K(e, "completed"),
1778
+ call_id: String(e.item_id || ""),
1779
+ tool_name: l,
1780
+ run_id: i,
1781
+ tool_output: G(n)
1782
+ }
1783
+ } : null;
1784
+ }
1785
+ if (r === "item.failed") {
1786
+ if (e.item_kind === "tool_call") {
1787
+ let t = this.toolCallByItemId.get(String(e.item_id || "")), n = t?.callId || String(e.item_id || ""), r = t?.name || this.toolNameByCallId.get(n) || "tool", o = this.toolResultByItemId.get(String(e.item_id || ""));
1788
+ return {
1789
+ ...a(),
1790
+ EventType: "tool_result",
1791
+ Metadata: {
1792
+ ...K(e, "completed", void 0, n),
1793
+ call_id: n,
1794
+ tool_name: r,
1795
+ run_id: i,
1796
+ tool_output: o ?? { error: H(e.error) || e.error || "item failed" }
1797
+ }
1798
+ };
1799
+ }
1800
+ return l && l !== "notification" && l !== "agentMessage" && l !== "reasoning" ? {
1801
+ ...a(),
1802
+ EventType: "tool_result",
1803
+ Metadata: {
1804
+ ...K(e, "completed"),
1805
+ call_id: String(e.item_id || ""),
1806
+ tool_name: l,
1807
+ run_id: i,
1808
+ tool_output: { error: H(e.error) || e.error || "item failed" }
1809
+ }
1810
+ } : null;
1811
+ }
1812
+ return null;
1813
+ }
1814
+ };
1815
+ async function st(e) {
1816
+ let t = e.getReader(), n = new TextDecoder(), r = "", i = null, a = !1, o = (e) => e.includes("data:") || e.includes("event:") || e.includes(":");
1817
+ try {
1818
+ for (;;) {
1819
+ let { value: e, done: s } = await t.read();
1820
+ if (e && (r += n.decode(e, { stream: !0 })), s) {
1821
+ a = !0;
1822
+ break;
1823
+ }
1824
+ if (r.trimStart().startsWith("{")) {
1825
+ if (ct(r)) {
1826
+ i = lt(r);
1827
+ break;
1828
+ }
1829
+ } else if (o(r)) break;
1830
+ }
1831
+ } catch (e) {
1832
+ throw t.releaseLock(), e;
1833
+ }
1834
+ let s = new TextEncoder().encode(r), c = new ReadableStream({
1835
+ start(e) {
1836
+ if (s.length > 0 && e.enqueue(s), a || i) {
1837
+ e.close();
1838
+ return;
1839
+ }
1840
+ let n = () => t.read().then(({ value: t, done: r }) => {
1841
+ if (t && e.enqueue(t), r) {
1842
+ e.close();
1843
+ return;
1844
+ }
1845
+ return n();
1846
+ }).catch((t) => {
1847
+ try {
1848
+ e.error(t);
1849
+ } catch {}
1850
+ });
1851
+ n();
1852
+ },
1853
+ cancel() {
1854
+ t.cancel().catch(() => {});
1855
+ }
1856
+ });
1857
+ return {
1858
+ receipt: i,
1859
+ stream: c
1860
+ };
1861
+ }
1862
+ function ct(e) {
1863
+ let t = e.trim();
1864
+ if (!t.endsWith("}")) return !1;
1865
+ let n = 0, r = !1, i = !1;
1866
+ for (let e of t) {
1867
+ if (i) {
1868
+ i = !1;
1869
+ continue;
1870
+ }
1871
+ if (e === "\\") {
1872
+ i = !0;
1873
+ continue;
1874
+ }
1875
+ e === "\"" && (r = !r), !r && ((e === "{" || e === "[") && (n += 1), (e === "}" || e === "]") && --n);
1876
+ }
1877
+ return n === 0;
1878
+ }
1879
+ function lt(e) {
1880
+ try {
1881
+ let t = H(JSON.parse(e).Data), n = String(t?.ReceiptStatus || "");
1882
+ if (!n) return null;
1883
+ let r = H(t?.Error);
1884
+ return {
1885
+ status: n,
1886
+ presentationProfile: t?.PresentationProfile === "agent-block-v1" ? "agent-block-v1" : "flat-v1",
1887
+ commandId: t?.CommandId ?? null,
1888
+ messageId: t?.MessageId ?? null,
1889
+ runId: t?.RunId ?? null,
1890
+ acceptedSeq: t?.AcceptedSeq ?? null,
1891
+ error: r ? {
1892
+ code: String(r.code || ""),
1893
+ message: String(r.message || "")
1894
+ } : null
1895
+ };
1896
+ } catch {
1897
+ return null;
1898
+ }
1899
+ }
1900
+ //#endregion
1901
+ //#region src/utils/responses-stream.js
1902
+ function ut() {
1903
+ return {
1904
+ toolNames: /* @__PURE__ */ new Map(),
1905
+ toolArguments: /* @__PURE__ */ new Map(),
1906
+ callIds: /* @__PURE__ */ new Map(),
1907
+ currentResponseId: "",
1908
+ sawTextDelta: !1,
1909
+ sawReasoningDelta: !1
1910
+ };
1911
+ }
1912
+ function dt(e) {
1913
+ return typeof e == "string" ? e : Array.isArray(e) ? e.map((e) => typeof e == "string" ? e : e && typeof e == "object" && typeof e.text == "string" ? e.text : "").join("") : e && typeof e == "object" && typeof e.text == "string" ? e.text : "";
1914
+ }
1915
+ function q(e) {
1916
+ return e == null ? "" : typeof e == "string" ? e : typeof e == "object" ? JSON.stringify(e, null, 2) : String(e);
1917
+ }
1918
+ function J(...e) {
1919
+ for (let t of e) {
1920
+ let e = dt(t);
1921
+ if (e) return e;
1922
+ }
1923
+ return "";
1924
+ }
1925
+ function ft(e, t) {
1926
+ return String(e?.id || e?.item_id || e?.call_id || t?.item_id || t?.call_id || t?.output_index || "");
1927
+ }
1928
+ function pt(e, t, n, r, i) {
1929
+ t && (e.toolNames.set(t, r), e.toolArguments.set(t, i));
1930
+ let a = String(n?.call_id || "");
1931
+ a && (e.callIds.set(a, t || r), e.toolNames.set(a, r), e.toolArguments.set(a, i));
1932
+ }
1933
+ function Y(e, t, n, r = "tool") {
1934
+ let i = String(n?.call_id || "");
1935
+ return String(n?.name || n?.tool_name || (t ? e.toolNames.get(t) : "") || (i ? e.toolNames.get(i) : "") || r);
1936
+ }
1937
+ function mt(e) {
1938
+ if (!e || typeof e != "object") return "";
1939
+ let t = dt(e.content);
1940
+ return J(e.output_text, e.text, e.summary_text, e.summary, e.delta, t);
1941
+ }
1942
+ function ht(e) {
1943
+ let t = e?.response && typeof e.response == "object" ? e.response : e, n = J(t?.output_text, t?.text);
1944
+ if (n) return n;
1945
+ let r = Array.isArray(t?.output) ? t.output : [];
1946
+ for (let e of r) {
1947
+ let t = mt(e);
1948
+ if (t) return t;
1949
+ }
1950
+ return "";
1951
+ }
1952
+ function gt(e) {
1953
+ let t = e?.response && typeof e.response == "object" ? e.response : e;
1954
+ return Array.isArray(t?.output) ? t.output : [];
1955
+ }
1956
+ function _t(e) {
1957
+ let t = e;
1958
+ if (typeof t == "string") try {
1959
+ t = JSON.parse(t);
1960
+ } catch {
1961
+ return {};
1962
+ }
1963
+ return Array.isArray(t) && (t = t[0] || {}), !t || typeof t != "object" ? {} : t.value && typeof t.value == "object" && !Array.isArray(t.value) ? {
1964
+ ...t.value,
1965
+ approval_request_id: t.value.approval_request_id || t.value.id || t.id
1966
+ } : t;
1967
+ }
1968
+ function vt({ data: e, state: t, status: n }) {
1969
+ let r = e?.item || e?.output_item || e || {}, i = String(r.type || "").trim(), a = ft(r, e);
1970
+ if (i === "function_call") {
1971
+ let e = Y(t, a, r), i = q(r.arguments ?? r.args ?? r.input);
1972
+ return pt(t, a, r, e, i), [{
1973
+ type: "tool_upsert",
1974
+ name: e,
1975
+ args: i,
1976
+ status: n
1977
+ }];
1978
+ }
1979
+ if (i === "function_call_output") return [{
1980
+ type: "tool_result",
1981
+ name: Y(t, a, r),
1982
+ output: q(r.output ?? r.result ?? r.content)
1983
+ }];
1984
+ if (i === "mcp_approval_request") {
1985
+ let n = String(r.name || "approval"), i = q(r.arguments ?? r.args), a = String(r.id || r.approval_request_id || ""), o = String(r.run_id || r.runId || e?.run_id || e?.runId || "");
1986
+ return [{
1987
+ type: "tool_upsert",
1988
+ name: n,
1989
+ args: i,
1990
+ status: "paused",
1991
+ approvalRequestId: a,
1992
+ previousResponseId: String(e?.response_id || t.currentResponseId || ""),
1993
+ serverLabel: String(r.server_label || "")
1994
+ }, {
1995
+ type: "approval_request",
1996
+ approvalRequestId: a,
1997
+ previousResponseId: String(e?.response_id || t.currentResponseId || ""),
1998
+ ...o ? { runId: o } : {},
1999
+ name: n,
2000
+ args: i
2001
+ }];
2002
+ }
2003
+ if (i === "reasoning" || i === "reasoning_summary" || i === "reasoning_summary_text") {
2004
+ let e = mt(r);
2005
+ return e ? [{
2006
+ type: "reasoning_delta",
2007
+ text: e
2008
+ }] : [];
2009
+ }
2010
+ if (i === "message") {
2011
+ let e = mt(r);
2012
+ return e ? [{
2013
+ type: n === "completed" ? "text_final" : "text_delta",
2014
+ text: e
2015
+ }] : [];
2016
+ }
2017
+ return [];
2018
+ }
2019
+ function yt({ eventName: e, data: t, state: n }) {
2020
+ let r = String(t?.type || e || "").trim(), i = String(t?.id || t?.response?.id || t?.response_id || "");
2021
+ if (i.startsWith("resp_") && (n.currentResponseId = i), r === "response.tool_call") return [{
2022
+ type: "tool_upsert",
2023
+ name: String(t?.name || t?.tool_name || "tool"),
2024
+ args: q(t?.args ?? t?.arguments),
2025
+ status: "running"
2026
+ }];
2027
+ if (r === "response.tool_result" || r === "response.ksadk.tool_result") return [{
2028
+ type: "tool_result",
2029
+ name: String(t?.name || t?.tool_name || "tool"),
2030
+ output: q(t?.output ?? t?.result)
2031
+ }];
2032
+ if (r === "response.output_item.added") return vt({
2033
+ data: t,
2034
+ state: n,
2035
+ status: "running"
2036
+ });
2037
+ if (r === "response.output_item.done") return vt({
2038
+ data: t,
2039
+ state: n,
2040
+ status: "completed"
2041
+ });
2042
+ if (r === "response.function_call_arguments.delta") {
2043
+ let e = String(t?.item_id || t?.call_id || ""), r = Y(n, e, t), i = `${n.toolArguments.get(e) || ""}${String(t?.delta || "")}`;
2044
+ return n.toolArguments.set(e, i), [{
2045
+ type: "tool_upsert",
2046
+ name: r,
2047
+ args: i,
2048
+ status: "running"
2049
+ }];
2050
+ }
2051
+ if (r === "response.function_call_arguments.done") {
2052
+ let e = String(t?.item_id || t?.call_id || ""), r = Y(n, e, t), i = q(t?.arguments ?? n.toolArguments.get(e));
2053
+ return n.toolArguments.set(e, i), [{
2054
+ type: "tool_upsert",
2055
+ name: r,
2056
+ args: i,
2057
+ status: "running"
2058
+ }];
2059
+ }
2060
+ if (r === "response.reasoning.delta" || r === "response.reasoning_text.delta" || r === "response.reasoning_summary.delta" || r === "response.reasoning_summary_text.delta") {
2061
+ let e = J(t?.delta, t?.text);
2062
+ return e && (n.sawReasoningDelta = !0), e ? [{
2063
+ type: "reasoning_delta",
2064
+ text: e
2065
+ }] : [];
2066
+ }
2067
+ if (r === "response.output_text.delta") {
2068
+ if (t?.replace === !0 && (typeof t.delta == "string" || typeof t.text == "string")) return n.sawTextDelta = !0, [{
2069
+ type: "text_replace",
2070
+ text: typeof t.delta == "string" ? t.delta : t.text
2071
+ }];
2072
+ let e = J(t?.delta, t?.text);
2073
+ return e && (n.sawTextDelta = !0), e ? [{
2074
+ type: "text_delta",
2075
+ text: e
2076
+ }] : [];
2077
+ }
2078
+ if (r === "response.output_text.done") {
2079
+ let e = J(t?.text, t?.delta);
2080
+ return e ? [{
2081
+ type: "text_final",
2082
+ text: e
2083
+ }] : [];
2084
+ }
2085
+ if (r === "response.content_part.delta") {
2086
+ let e = String(t?.part?.type || t?.delta?.type || t?.content_type || ""), r = J(t?.delta?.text, t?.delta, t?.text);
2087
+ return r ? e.includes("reasoning") ? (n.sawReasoningDelta = !0, [{
2088
+ type: "reasoning_delta",
2089
+ text: r
2090
+ }]) : (n.sawTextDelta = !0, [{
2091
+ type: "text_delta",
2092
+ text: r
2093
+ }]) : [];
2094
+ }
2095
+ if (r === "response.completed") {
2096
+ let e = ht(t), r = gt(t).flatMap((e) => vt({
2097
+ data: {
2098
+ ...t,
2099
+ item: e
2100
+ },
2101
+ state: n,
2102
+ status: "completed"
2103
+ })).filter((e) => !(n.sawTextDelta && e.type === "text_final") && !(n.sawReasoningDelta && e.type === "reasoning_delta")), i = r.some((e) => e.type === "text_final");
2104
+ return [
2105
+ ...r,
2106
+ ...e && !n.sawTextDelta && !i ? [{
2107
+ type: "text_final",
2108
+ text: e
2109
+ }] : [],
2110
+ {
2111
+ type: "terminal",
2112
+ status: "completed"
2113
+ }
2114
+ ];
2115
+ }
2116
+ if (r === "response.failed") return [{
2117
+ type: "failed",
2118
+ message: t?.error?.message || "Agent 运行失败"
2119
+ }, {
2120
+ type: "terminal",
2121
+ status: "failed"
2122
+ }];
2123
+ if (r === "response.incomplete") return [{ type: "incomplete" }, {
2124
+ type: "terminal",
2125
+ status: "incomplete"
2126
+ }];
2127
+ if (r === "response.cancelled") return [{
2128
+ type: "terminal",
2129
+ status: "cancelled"
2130
+ }];
2131
+ if (r === "response.approval_request" || r === "response.ksadk.approval_request") {
2132
+ let e = _t(t?.interrupt_info), r = String(e.approval_request_id || e.id || ""), i = String(t?.response_id || n.currentResponseId || ""), a = String(t?.run_id || t?.runId || ""), o = e.approval_requests && typeof e.approval_requests == "object" ? e.approval_requests : e, s = Array.isArray(o.action_requests) ? o.action_requests : [];
2133
+ if (s.length > 0) {
2134
+ let e = s.map((e) => ({
2135
+ type: "tool_upsert",
2136
+ name: String(e?.name || "tool"),
2137
+ args: JSON.stringify(e?.args ?? {}),
2138
+ status: "paused",
2139
+ approvalRequestId: r,
2140
+ previousResponseId: i
2141
+ })), t = s[0] || {};
2142
+ return [...e, {
2143
+ type: "approval_request",
2144
+ approvalRequestId: r,
2145
+ previousResponseId: i,
2146
+ ...a ? { runId: a } : {},
2147
+ name: String(t?.name || "人工确认"),
2148
+ args: JSON.stringify(t?.args ?? {})
2149
+ }];
2150
+ }
2151
+ return [{
2152
+ type: "approval_request",
2153
+ approvalRequestId: r,
2154
+ previousResponseId: i,
2155
+ ...a ? { runId: a } : {}
2156
+ }];
2157
+ }
2158
+ if (r === "response.approval_resolved" || r === "response.ksadk.approval_resolved") {
2159
+ let e = String(t?.decision || t?.outcome || "").toLowerCase(), n = e === "reject" || e === "rejected" || e === "denied" ? "rejected" : e === "cancel" || e === "cancelled" || e === "canceled" ? "cancelled" : "approved";
2160
+ return [{
2161
+ type: "approval_resolved",
2162
+ approvalRequestId: String(t?.approval_request_id || t?.approvalRequestId || t?.approval_id || t?.approvalId || ""),
2163
+ decision: n,
2164
+ revision: Number(t?.revision || 2)
2165
+ }];
2166
+ }
2167
+ if (new Set([
2168
+ "response.ksadk.a2ui_surface_begin",
2169
+ "a2ui.surface.begin",
2170
+ "response.a2ui.createSurface",
2171
+ "a2ui.createSurface",
2172
+ "createSurface"
2173
+ ]).has(r)) {
2174
+ let e = t?.surface && typeof t.surface == "object" ? t.surface : {};
2175
+ return [{
2176
+ type: "a2ui_surface_begin",
2177
+ surfaceId: String(t?.surface_id || t?.surfaceId || e.surface_id || e.surfaceId || ""),
2178
+ surface: e
2179
+ }];
2180
+ }
2181
+ if (new Set([
2182
+ "response.ksadk.a2ui_surface_update",
2183
+ "a2ui.surface.update",
2184
+ "response.a2ui.updateComponents",
2185
+ "a2ui.updateComponents",
2186
+ "updateComponents"
2187
+ ]).has(r)) {
2188
+ let e = t?.surface && typeof t.surface == "object" ? t.surface : {};
2189
+ return [{
2190
+ type: "a2ui_surface_update",
2191
+ surfaceId: String(t?.surface_id || t?.surfaceId || e.surface_id || e.surfaceId || ""),
2192
+ surface: e
2193
+ }];
2194
+ }
2195
+ if (new Set([
2196
+ "response.ksadk.a2ui_surface_end",
2197
+ "a2ui.surface.end",
2198
+ "response.a2ui.deleteSurface",
2199
+ "a2ui.deleteSurface",
2200
+ "deleteSurface"
2201
+ ]).has(r)) return [{
2202
+ type: "a2ui_surface_end",
2203
+ surfaceId: String(t?.surface_id || t?.surfaceId || "")
2204
+ }];
2205
+ if (new Set(["response.ksadk.a2ui_interaction", "a2ui.interaction"]).has(r)) return [{
2206
+ type: "a2ui_interaction",
2207
+ surfaceId: String(t?.surface_id || t?.surfaceId || ""),
2208
+ interactionId: String(t?.interaction_id || t?.interactionId || ""),
2209
+ kind: String(t?.kind || "input"),
2210
+ inputSchema: t?.input_schema && typeof t.input_schema == "object" ? t.input_schema : {}
2211
+ }];
2212
+ let a = t?.content?.parts?.[0]?.text;
2213
+ return a && !t?.actions?.finishReason ? [{
2214
+ type: "text_delta",
2215
+ text: String(a)
2216
+ }] : [];
2217
+ }
2218
+ //#endregion
2219
+ //#region src/utils/session-events.js
2220
+ var bt = new Set([
2221
+ "completed",
2222
+ "failed",
2223
+ "error",
2224
+ "cancelled",
2225
+ "canceled",
2226
+ "aborted",
2227
+ "interrupted",
2228
+ "resume_failed"
2229
+ ]), xt = new Set([
2230
+ "tool_call",
2231
+ "tool_result",
2232
+ "stage_tool_call",
2233
+ "stage_tool_result"
2234
+ ]), St = {
2235
+ "run.started": "in_progress",
2236
+ "run.progress": "in_progress",
2237
+ "run.interrupted": "interrupted",
2238
+ "run.completed": "completed",
2239
+ "run.failed": "failed",
2240
+ "run.canceled": "cancelled"
2241
+ };
2242
+ function X(e) {
2243
+ let t = e?.Content?.payload;
2244
+ return t && typeof t == "object" ? t : {};
2245
+ }
2246
+ function Ct(e, t) {
2247
+ let n = String(e || ""), r = String(t || "");
2248
+ return n ? !r || n.endsWith(r) ? n : r.startsWith(n) ? r : `${n}${r}` : r;
2249
+ }
2250
+ function wt(e) {
2251
+ let t = String(e?.EventType || ""), n = X(e);
2252
+ return St[t] ? {
2253
+ ...e,
2254
+ EventType: "run_status",
2255
+ Content: {
2256
+ status: String(n.status || St[t]),
2257
+ ...n.detail ? { detail: String(n.detail) } : {}
2258
+ }
2259
+ } : t === "reasoning.delta" || t === "reasoning.completed" ? {
2260
+ ...e,
2261
+ EventType: "reasoning",
2262
+ Content: {
2263
+ role: "model",
2264
+ parts: [{ text: String(n.text || "") }]
2265
+ }
2266
+ } : t === "tool.call.begin" ? {
2267
+ ...e,
2268
+ EventType: "tool_call",
2269
+ Metadata: {
2270
+ ...e.Metadata || {},
2271
+ tool_name: n.name,
2272
+ tool_args: n.args,
2273
+ tool_call_id: n.call_id
2274
+ },
2275
+ Content: {
2276
+ role: "model",
2277
+ parts: []
2278
+ }
2279
+ } : t === "tool.call.end" ? {
2280
+ ...e,
2281
+ EventType: "tool_result",
2282
+ Metadata: {
2283
+ ...e.Metadata || {},
2284
+ tool_name: n.name,
2285
+ tool_output: n.error || n.result,
2286
+ tool_call_id: n.call_id
2287
+ },
2288
+ Content: {
2289
+ role: "model",
2290
+ parts: []
2291
+ }
2292
+ } : e;
2293
+ }
2294
+ function Tt(e) {
2295
+ let t = [], n = /* @__PURE__ */ new Map();
2296
+ for (let r of Array.isArray(e) ? e : []) {
2297
+ let e = String(r?.EventType || "");
2298
+ if (e === "text.delta" || e === "text.completed" || e === "message.delta") {
2299
+ let e = String(r?.InvocationId || "").trim();
2300
+ if (!e) continue;
2301
+ let t = n.get(e);
2302
+ n.set(e, {
2303
+ event: r,
2304
+ text: Ct(t?.text, X(r).text || r?.Content?.text)
2305
+ });
2306
+ continue;
2307
+ }
2308
+ t.push(wt(r));
2309
+ }
2310
+ for (let { event: e, text: r } of n.values()) t.push({
2311
+ ...e,
2312
+ EventType: "assistant_stream_snapshot",
2313
+ Content: {
2314
+ role: "model",
2315
+ parts: [{ text: r }]
2316
+ },
2317
+ Metadata: {
2318
+ ...e.Metadata || {},
2319
+ stream_snapshot: !0
2320
+ }
2321
+ });
2322
+ for (let n of Array.isArray(e) ? e : []) String(n?.EventType || "") === "message.completed" && t.push({
2323
+ ...n,
2324
+ EventType: "assistant_message",
2325
+ Content: {
2326
+ role: "model",
2327
+ parts: [{ text: String(X(n).text || n?.Content?.text || "") }]
2328
+ }
2329
+ });
2330
+ return t.sort((e, t) => Q(e) - Q(t));
2331
+ }
2332
+ function Et(e) {
2333
+ let t = String(e?.EventType || "").trim();
2334
+ if (t !== "run_status" && !t.startsWith("run.")) return "";
2335
+ let n = X(e);
2336
+ return String(e?.Content?.status || n.status || St[t] || "").trim().toLowerCase();
2337
+ }
2338
+ function Dt(e) {
2339
+ return typeof e == "string" ? e : Array.isArray(e) ? e.map((e) => typeof e == "string" ? e : e && typeof e == "object" && typeof e.text == "string" ? e.text : "").join("") : "";
2340
+ }
2341
+ function Ot(e) {
2342
+ let t = String(e || "").trim();
2343
+ return t ? `/agentengine/api/v1/AttachmentContent?FileUri=${encodeURIComponent(t)}` : "";
2344
+ }
2345
+ function kt(e) {
2346
+ return String(e || "").match(/^data:([^;,]+)/)?.[1] || "";
2347
+ }
2348
+ function At(e, t) {
2349
+ let n = String(e || "").trim(), r = String(t || "").trim();
2350
+ return !n || !r || n === r ? !0 : n.endsWith("/*") ? r.startsWith(n.slice(0, -1)) : r.endsWith("/*") ? n.startsWith(r.slice(0, -1)) : !1;
2351
+ }
2352
+ function Z(e) {
2353
+ let t = e?.Content?.parts || [], n = [], r = /* @__PURE__ */ new Map(), i = (e) => {
2354
+ let t = `${e.fileUri || e.url || e.name}|${e.type}`;
2355
+ r.has(t) || r.set(t, e);
2356
+ };
2357
+ for (let e of t) {
2358
+ if (e?.type === "input_text" || e?.text) {
2359
+ n.push(e.text || "");
2360
+ continue;
2361
+ }
2362
+ if (e?.type === "input_file" && e.inlineData) {
2363
+ i({
2364
+ name: e.inlineData.displayName || "attachment",
2365
+ url: `data:${e.inlineData.mimeType || "application/octet-stream"};base64,${e.inlineData.data}`,
2366
+ type: e.inlineData.mimeType || "application/octet-stream"
2367
+ });
2368
+ continue;
2369
+ }
2370
+ if (e?.type === "input_file" && typeof e.file_data == "string" && e.file_data) {
2371
+ i({
2372
+ name: e.filename || e.displayName || e.display_name || "attachment",
2373
+ url: `data:${e.mime_type || e.mimeType || "application/octet-stream"};base64,${e.file_data}`,
2374
+ type: e.mime_type || e.mimeType || "application/octet-stream"
2375
+ });
2376
+ continue;
2377
+ }
2378
+ if (e?.type === "input_file" && typeof e.file_url == "string" && e.file_url.trim()) {
2379
+ let t = e.file_url.trim();
2380
+ i({
2381
+ name: e.filename || e.displayName || e.display_name || "attachment",
2382
+ url: Ot(t),
2383
+ type: e.mime_type || e.mimeType || "application/octet-stream",
2384
+ fileUri: t
2385
+ });
2386
+ continue;
2387
+ }
2388
+ if (e?.type === "input_image" || e?.type === "image_url") {
2389
+ let t = typeof e.image_url == "string" ? e.image_url : e.image_url?.url;
2390
+ t && i({
2391
+ name: e.filename || e.displayName || e.display_name || "uploaded_image",
2392
+ url: t,
2393
+ type: e.mime_type || e.mimeType || kt(t) || "image/*"
2394
+ });
2395
+ continue;
2396
+ }
2397
+ if (e?.type === "input_file" && e.fileData) {
2398
+ let t = String(e.fileData.fileUri || "").trim();
2399
+ i({
2400
+ name: e.fileData.displayName || "attachment",
2401
+ url: Ot(t),
2402
+ type: e.fileData.mimeType || "application/octet-stream",
2403
+ fileUri: t
2404
+ });
2405
+ }
2406
+ }
2407
+ let a = Array.isArray(e?.Metadata?.attachments) ? e.Metadata.attachments : [];
2408
+ for (let e of a) {
2409
+ let t = String(e.file_uri || "").trim(), n = {
2410
+ name: e.display_name || "attachment",
2411
+ url: Ot(t),
2412
+ type: e.mime_type || "application/octet-stream",
2413
+ fileUri: t
2414
+ };
2415
+ !n.url && !n.fileUri && Array.from(r.values()).some((e) => e.name === n.name && At(e.type, n.type) && (e.url || e.fileUri)) || i(n);
2416
+ }
2417
+ let o = Array.from(r.values());
2418
+ return {
2419
+ text: n.join(""),
2420
+ attachments: o.length > 0 ? o : void 0
2421
+ };
2422
+ }
2423
+ function jt(e) {
2424
+ let t = e?.Metadata?.responses_output;
2425
+ if (!Array.isArray(t)) return {};
2426
+ let r = ut(), i = yt({
2427
+ eventName: "response.completed",
2428
+ data: { response: {
2429
+ id: String(e.Metadata?.response_id || ""),
2430
+ output: t
2431
+ } },
2432
+ state: r
2433
+ }), a = "", o = {};
2434
+ for (let e of i) {
2435
+ if (e.type === "reasoning_delta") {
2436
+ a += e.text;
2437
+ continue;
2438
+ }
2439
+ if (e.type === "tool_upsert") {
2440
+ o[e.name] = {
2441
+ ...o[e.name] || {
2442
+ name: e.name,
2443
+ args: ""
2444
+ },
2445
+ name: e.name,
2446
+ args: e.args,
2447
+ status: e.status,
2448
+ ...e.approvalRequestId ? { approvalRequestId: e.approvalRequestId } : {},
2449
+ ...e.previousResponseId ? { previousResponseId: e.previousResponseId } : {},
2450
+ ...e.serverLabel ? { serverLabel: e.serverLabel } : {},
2451
+ ...e.approvalRequestId ? { approvalStatus: "pending" } : {}
2452
+ };
2453
+ continue;
2454
+ }
2455
+ e.type === "tool_result" && (o[e.name] = {
2456
+ ...o[e.name] || {
2457
+ name: e.name,
2458
+ args: ""
2459
+ },
2460
+ name: e.name,
2461
+ output: e.output,
2462
+ status: n(e.output) ? "error" : "completed"
2463
+ });
2464
+ }
2465
+ return {
2466
+ ...a ? { reasoning: a } : {},
2467
+ ...Object.keys(o).length > 0 ? { tools: o } : {}
2468
+ };
2469
+ }
2470
+ function Mt(e, t, n) {
2471
+ return t === "running" ? e === "prompt_too_long" ? "检测到上下文过长,正在自动压缩历史后重试" : "正在自动压缩上下文" : n ? e === "prompt_too_long" ? "上下文过长,系统已自动压缩历史并重试" : "系统已自动压缩较早的对话上下文" : t === "failed" ? "自动压缩上下文未完成" : e === "prompt_too_long" ? "已完成上下文压缩,并继续当前回复" : "已完成上下文压缩";
2472
+ }
2473
+ function Nt(e, t) {
2474
+ return t?.itemId ? [
2475
+ "runtime-item",
2476
+ t.runId || e,
2477
+ t.scopeId || e,
2478
+ t.itemId
2479
+ ].join("\0") : [
2480
+ e,
2481
+ t.content || "",
2482
+ t.reasoning || ""
2483
+ ].join("\0");
2484
+ }
2485
+ function Pt(e) {
2486
+ let t = String(e?.InvocationId || "").trim(), n = e?.Metadata?.RuntimeItem;
2487
+ return n?.ItemId ? [
2488
+ "runtime-item",
2489
+ n.RunId || t,
2490
+ n.ScopeId || t,
2491
+ n.ItemId
2492
+ ].join("\0") : t;
2493
+ }
2494
+ function Ft(e) {
2495
+ let t = e?.Metadata?.RuntimeItem;
2496
+ return t?.ItemId ? {
2497
+ id: `runtime:${t.RunId || e.InvocationId || "run"}:${t.ScopeId || e.InvocationId || "scope"}:${t.ItemId}`,
2498
+ runId: String(t.RunId || e.InvocationId || ""),
2499
+ scopeId: String(t.ScopeId || e.InvocationId || ""),
2500
+ itemId: String(t.ItemId),
2501
+ partId: t.PartId ? String(t.PartId) : void 0
2502
+ } : null;
2503
+ }
2504
+ function It(e) {
2505
+ return e?.Metadata?.responses_mirror === !0 || String(e?.Metadata?.responses_mirror || "").trim().toLowerCase() === "true";
2506
+ }
2507
+ function Lt(e) {
2508
+ if (e?.EventType !== "user_message") return "";
2509
+ let t = Z(e), n = (t.attachments || []).map((e) => [
2510
+ e.name || "",
2511
+ e.fileUri || "",
2512
+ e.url || "",
2513
+ e.type || ""
2514
+ ].join("")).sort().join("");
2515
+ return [String(t.text || "").trim(), n].join("\0");
2516
+ }
2517
+ function Rt(e) {
2518
+ return e?.EventType === "assistant_stream_snapshot";
2519
+ }
2520
+ function Q(e) {
2521
+ let t = Number(e?.SeqId || 0);
2522
+ if (Number.isFinite(t) && t > 0) return t;
2523
+ let n = Number(e?.Timestamp || 0);
2524
+ return Number.isFinite(n) ? n : 0;
2525
+ }
2526
+ function zt(e) {
2527
+ if (typeof e == "string") return e;
2528
+ if (e == null) return "";
2529
+ try {
2530
+ return JSON.stringify(e, null, 2);
2531
+ } catch {
2532
+ return String(e);
2533
+ }
2534
+ }
2535
+ function Bt(e) {
2536
+ let t = String(e?.EventType || "");
2537
+ if (!xt.has(t)) return null;
2538
+ let r = String(e.Metadata?.tool_name || e.Metadata?.name || e.Metadata?.function_name || Dt(e.Content?.parts) || "tool").trim() || "tool", i = String(e.Metadata?.call_id || e.Metadata?.callId || e.Metadata?.RuntimeItem?.ToolCallId || "").trim(), a = {
2539
+ name: r,
2540
+ ...i ? { callId: i } : {},
2541
+ args: "",
2542
+ status: "running"
2543
+ }, o = t === "tool_result" || t === "stage_tool_result" ? (() => {
2544
+ let t = zt(e.Metadata?.tool_output ?? e.Metadata?.output ?? e.Metadata?.result ?? Dt(e.Content?.parts));
2545
+ return {
2546
+ ...a,
2547
+ output: t,
2548
+ status: n(t) ? "error" : "completed"
2549
+ };
2550
+ })() : {
2551
+ ...a,
2552
+ args: zt(e.Metadata?.tool_args ?? e.Metadata?.arguments ?? e.Metadata?.args ?? {})
2553
+ }, s = Ft(e);
2554
+ return {
2555
+ id: s?.id || e.EventId || String(Date.now() + Math.random()),
2556
+ role: "model",
2557
+ content: "",
2558
+ timestamp: e.Timestamp || Date.now(),
2559
+ eventType: t,
2560
+ ...s || {},
2561
+ tools: { [r]: o }
2562
+ };
2563
+ }
2564
+ function Vt(e = {}, t = {}) {
2565
+ let n = { ...e || {} };
2566
+ for (let [e, r] of Object.entries(t || {})) n[e] = {
2567
+ ...n[e] || {},
2568
+ ...r || {},
2569
+ name: e,
2570
+ args: r?.args || n[e]?.args || ""
2571
+ };
2572
+ return n;
2573
+ }
2574
+ function Ht(e = {}, t = "") {
2575
+ let n = String(t || "").trim().toLowerCase(), r = n === "completed" ? "completed" : bt.has(n) ? "error" : "";
2576
+ if (!r) return e;
2577
+ let i = !1, a = {};
2578
+ for (let [t, n] of Object.entries(e || {})) n?.status === "running" ? (i = !0, a[t] = {
2579
+ ...n,
2580
+ status: r
2581
+ }) : a[t] = n;
2582
+ return i ? a : e;
2583
+ }
2584
+ function Ut(e, t) {
2585
+ return {
2586
+ ...e,
2587
+ reasoning: Wt(e.reasoning, t.reasoning),
2588
+ tools: Vt(e.tools, t.tools),
2589
+ eventId: t.eventId || e.eventId,
2590
+ responseId: t.responseId || e.responseId,
2591
+ traceId: t.traceId || e.traceId,
2592
+ rootSpanId: t.rootSpanId || e.rootSpanId,
2593
+ timestamp: Math.max(Number(e.timestamp || 0), Number(t.timestamp || 0)) || t.timestamp || e.timestamp,
2594
+ id: t.responseId || t.reasoning || t.tools ? t.id : e.id
2595
+ };
2596
+ }
2597
+ function Wt(e = "", t = "") {
2598
+ let n = String(e || ""), r = String(t || "");
2599
+ return n ? !r || n.endsWith(r) ? n : r.startsWith(n) ? r : `${n}${r}` : r;
2600
+ }
2601
+ function Gt(e) {
2602
+ return {
2603
+ id: e.id,
2604
+ role: "system",
2605
+ eventType: "context_checkpoint",
2606
+ status: e.status,
2607
+ trigger: e.trigger,
2608
+ compactedUntilSeqId: e.compactedUntilSeqId,
2609
+ summary: e.summary,
2610
+ historical: e.historical,
2611
+ timestamp: e.timestamp,
2612
+ content: Mt(e.trigger, e.status, e.historical)
2613
+ };
2614
+ }
2615
+ function Kt(e) {
2616
+ let t = qt(e);
2617
+ return t && Number.isSafeInteger(e.SeqId) && e.SeqId > 0 && (t.historyOrder = { seq: e.SeqId }), t;
2618
+ }
2619
+ function qt(e) {
2620
+ let t = e?.EventType || "", n = Ft(e);
2621
+ if (t === "run_status") return e.Content?.status === "failed" ? {
2622
+ id: e.EventId || String(Date.now() + Math.random()),
2623
+ role: "system",
2624
+ content: e.Content?.detail || "本轮运行失败。",
2625
+ eventType: t,
2626
+ status: "failed",
2627
+ timestamp: e.Timestamp || Date.now()
2628
+ } : e.Content?.status === "cancelled" ? {
2629
+ id: e.EventId || String(Date.now() + Math.random()),
2630
+ role: "system",
2631
+ content: e.Content?.detail || "本轮输出已停止。",
2632
+ eventType: t,
2633
+ status: "cancelled",
2634
+ timestamp: e.Timestamp || Date.now()
2635
+ } : null;
2636
+ if (t === "context_checkpoint") {
2637
+ let t = Z(e);
2638
+ return Gt({
2639
+ id: e.EventId || String(Date.now() + Math.random()),
2640
+ timestamp: e.Timestamp || Date.now(),
2641
+ status: "completed",
2642
+ trigger: String(e.Metadata?.trigger || "auto"),
2643
+ compactedUntilSeqId: Number(e.Metadata?.compacted_until_seq_id || 0) || void 0,
2644
+ summary: t.text || void 0,
2645
+ historical: !0
2646
+ });
2647
+ }
2648
+ if (t === "reasoning") {
2649
+ let r = Z(e).text || Dt(e.Metadata?.reasoning);
2650
+ return r ? {
2651
+ id: n?.id || e.EventId || String(Date.now() + Math.random()),
2652
+ role: "model",
2653
+ content: "",
2654
+ reasoning: r,
2655
+ ...e.Metadata?.presentation_source === "public_summary" ? { reasoningSource: "public_summary" } : e.Metadata?.presentation_source === "provider_reasoning" ? { reasoningSource: "provider_reasoning" } : {},
2656
+ timestamp: e.Timestamp || Date.now(),
2657
+ eventType: t,
2658
+ ...n || {}
2659
+ } : null;
2660
+ }
2661
+ if (xt.has(t)) return Bt(e);
2662
+ if (t !== "user_message" && t !== "assistant_message" && t !== "assistant_stream_snapshot") return null;
2663
+ let r = Z(e), i = t === "assistant_message" ? jt(e) : {};
2664
+ if (!r.text && !r.attachments?.length && !i.reasoning && !i.tools) return null;
2665
+ let a = String(e.Metadata?.response_id || e.Metadata?.ResponseId || "").trim(), o = String(e.Metadata?.trace_id || e.Metadata?.TraceId || "").trim(), s = String(e.Metadata?.root_span_id || e.Metadata?.rootSpanId || e.Metadata?.RootSpanId || "").trim();
2666
+ return {
2667
+ id: n?.id || e.EventId || String(Date.now() + Math.random()),
2668
+ role: t === "user_message" ? "user" : "model",
2669
+ content: r.text,
2670
+ timestamp: e.Timestamp || Date.now(),
2671
+ eventType: t,
2672
+ eventId: e.EventId || void 0,
2673
+ responseId: a || void 0,
2674
+ traceId: o || void 0,
2675
+ rootSpanId: s || void 0,
2676
+ attachments: r.attachments,
2677
+ ...n || {},
2678
+ ...i
2679
+ };
2680
+ }
2681
+ function Jt(e = []) {
2682
+ let t = /* @__PURE__ */ new Map(), n = /* @__PURE__ */ new Map(), r = /* @__PURE__ */ new Set(), i = /* @__PURE__ */ new Set(), a = /* @__PURE__ */ new Set(), o = /* @__PURE__ */ new Set(), s = Tt(e), c = [];
2683
+ for (let e of s) {
2684
+ let t = String(e?.EventId || "").trim();
2685
+ if (t) {
2686
+ if (o.has(t)) continue;
2687
+ o.add(t);
2688
+ }
2689
+ c.push(e);
2690
+ }
2691
+ let l = /* @__PURE__ */ new Set(), u = /* @__PURE__ */ new Set(), d = /* @__PURE__ */ new Map();
2692
+ for (let e of c) {
2693
+ if (e?.EventType !== "user_message" || It(e)) {
2694
+ if (e?.EventType === "assistant_message") {
2695
+ let t = Pt(e);
2696
+ t && u.add(t);
2697
+ }
2698
+ if (Rt(e)) {
2699
+ let t = Pt(e);
2700
+ if (t) {
2701
+ let n = d.get(t);
2702
+ (!n || Q(e) >= Q(n)) && d.set(t, e);
2703
+ }
2704
+ }
2705
+ continue;
2706
+ }
2707
+ let t = Lt(e);
2708
+ t && l.add(t);
2709
+ }
2710
+ let f = c.filter((e) => {
2711
+ if (Rt(e)) {
2712
+ let t = Pt(e);
2713
+ return t ? u.has(t) ? !1 : d.get(t) === e : !0;
2714
+ }
2715
+ if (e?.EventType !== "user_message" || !It(e)) return !0;
2716
+ let t = Lt(e);
2717
+ return !t || !l.has(t);
2718
+ });
2719
+ for (let e of f) {
2720
+ let a = String(e.InvocationId || "").trim();
2721
+ a && e.EventType === "user_message" && i.add(a), a && [
2722
+ "assistant_message",
2723
+ "assistant_stream_snapshot",
2724
+ "reasoning",
2725
+ "tool_call",
2726
+ "tool_result",
2727
+ "stage_tool_call",
2728
+ "stage_tool_result"
2729
+ ].includes(String(e.EventType || "")) && r.add(a), e.EventType === "run_status" && a && (t.set(a, String(e.Content?.status || "").trim()), n.set(a, e));
2730
+ }
2731
+ let p = [], m = /* @__PURE__ */ new Map(), h = /* @__PURE__ */ new Map(), g = null, _ = /* @__PURE__ */ new Map(), v = (e) => {
2732
+ let t = e?.itemId ? `${e.runId || e.invocationId || ""}\u0000${e.scopeId || e.invocationId || ""}\u0000${e.itemId}` : "", n = t ? h.get(t) : void 0;
2733
+ if (n !== void 0) {
2734
+ p[n] = Ut(p[n], e);
2735
+ return;
2736
+ }
2737
+ let r = String(e?.responseId || "").trim(), i = r ? m.get(r) : void 0;
2738
+ if (i !== void 0) {
2739
+ p[i] = Ut(p[i], e);
2740
+ return;
2741
+ }
2742
+ r && m.set(r, p.length), t && h.set(t, p.length), p.push(e);
2743
+ }, y = () => {
2744
+ g &&= (v(g), null);
2745
+ }, b = (e, n = "") => {
2746
+ let r = String(e || "").trim();
2747
+ if (!r) return {};
2748
+ let i = _.get(r);
2749
+ return i ? (_.delete(r), { tools: Ht(i.tools, t.get(r) || n) }) : {};
2750
+ };
2751
+ for (let e of f) {
2752
+ if (e.EventType === "run_status") continue;
2753
+ let t = Kt(e);
2754
+ if (!t) continue;
2755
+ let n = String(e.InvocationId || "").trim();
2756
+ if (n && (t.invocationId = n), xt.has(t.eventType)) {
2757
+ let n = String(e.InvocationId || "").trim();
2758
+ if (t.itemId) {
2759
+ y(), v(t);
2760
+ continue;
2761
+ }
2762
+ if (!n) {
2763
+ y(), v(t);
2764
+ continue;
2765
+ }
2766
+ let r = _.get(n);
2767
+ _.set(n, {
2768
+ ...r || {
2769
+ id: t.id,
2770
+ role: "model",
2771
+ content: "",
2772
+ timestamp: t.timestamp,
2773
+ invocationId: n
2774
+ },
2775
+ timestamp: Math.max(Number(r?.timestamp || 0), Number(t.timestamp || 0)),
2776
+ tools: Vt(r?.tools, t.tools)
2777
+ });
2778
+ continue;
2779
+ }
2780
+ if (t.eventType === "reasoning") {
2781
+ let n = String(e.InvocationId || "").trim(), r = String(e.Metadata?.RuntimeItem?.Operation || "").toLowerCase(), i = !!(t.itemId && g?.itemId === t.itemId && g?.runId === t.runId && g?.scopeId === t.scopeId), a = !!(!t.itemId && !g?.itemId && n && g?.invocationId === n);
2782
+ if (g && (i || a)) {
2783
+ g.reasoning = r === "replace" || r === "completed" ? t.reasoning : `${g.reasoning || ""}${t.reasoning || ""}`, g.timestamp = t.timestamp;
2784
+ continue;
2785
+ }
2786
+ y(), g = {
2787
+ ...t,
2788
+ ...n ? { invocationId: n } : {}
2789
+ };
2790
+ continue;
2791
+ }
2792
+ let r = t.eventType === "assistant_message" || t.eventType === "assistant_stream_snapshot";
2793
+ if (r && g) {
2794
+ let n = String(e.InvocationId || "").trim(), r = Nt(n, t);
2795
+ if (t.eventType === "assistant_message" && n && a.has(r)) {
2796
+ g = null;
2797
+ continue;
2798
+ }
2799
+ t.eventType === "assistant_message" && n && a.add(r), v({
2800
+ ...t,
2801
+ ...b(n, t.eventType === "assistant_message" ? "completed" : ""),
2802
+ reasoning: Wt(g.reasoning, t.reasoning)
2803
+ }), g = null;
2804
+ continue;
2805
+ }
2806
+ if (y(), r) {
2807
+ let n = String(e.InvocationId || "").trim(), r = Nt(n, t);
2808
+ if (t.eventType === "assistant_message" && n && a.has(r)) continue;
2809
+ t.eventType === "assistant_message" && n && a.add(r), v({
2810
+ ...t,
2811
+ ...b(n, t.eventType === "assistant_message" ? "completed" : "")
2812
+ });
2813
+ continue;
2814
+ }
2815
+ v(t);
2816
+ }
2817
+ y();
2818
+ for (let [e, n] of _.entries()) v({
2819
+ ...n,
2820
+ tools: Ht(n.tools, t.get(e))
2821
+ });
2822
+ for (let [e, a] of t.entries()) {
2823
+ if (a !== "in_progress" || r.has(e) || !i.has(e)) continue;
2824
+ let t = n.get(e) || {};
2825
+ v({
2826
+ id: `run-placeholder-${e}`,
2827
+ role: "model",
2828
+ content: "",
2829
+ status: "running",
2830
+ eventType: "run_status",
2831
+ timestamp: t.Timestamp || Date.now()
2832
+ });
2833
+ }
2834
+ return p;
2835
+ }
2836
+ function Yt(e = [], t = []) {
2837
+ let n = [], r = /* @__PURE__ */ new Set();
2838
+ for (let i of [...Array.isArray(e) ? e : [], ...Array.isArray(t) ? t : []]) {
2839
+ let e = String(i?.EventId || "").trim();
2840
+ if (e) {
2841
+ if (r.has(e)) continue;
2842
+ r.add(e);
2843
+ }
2844
+ n.push(i);
2845
+ }
2846
+ return n.sort((e, t) => {
2847
+ let n = Number(e?.SeqId || 0), r = Number(t?.SeqId || 0);
2848
+ return Number.isFinite(n) && Number.isFinite(r) && n !== r ? n - r : Number(e?.Timestamp || 0) - Number(t?.Timestamp || 0);
2849
+ });
2850
+ }
2851
+ function Xt(e = []) {
2852
+ return (Array.isArray(e) ? e : []).reduce((e, t) => {
2853
+ let n = Number(t.SeqId || 0);
2854
+ return Number.isFinite(n) ? Math.max(e, n) : e;
2855
+ }, 0);
2856
+ }
2857
+ function Zt(e) {
2858
+ return bt.has(Et(e));
2859
+ }
2860
+ //#endregion
2861
+ //#region src/utils/persisted-session-history.ts
2862
+ function $(e) {
2863
+ return e && typeof e == "object" && !Array.isArray(e) ? e : null;
2864
+ }
2865
+ function Qt(e) {
2866
+ if (typeof e == "number" && Number.isFinite(e)) return e > 0 && e < 1e11 ? e * 1e3 : e;
2867
+ if (typeof e == "string" && e.trim()) {
2868
+ let t = Number(e.trim());
2869
+ if (Number.isFinite(t)) return t > 0 && t < 1e11 ? t * 1e3 : t;
2870
+ }
2871
+ let t = Date.parse(String(e || ""));
2872
+ return Number.isFinite(t) ? t : Date.now();
2873
+ }
2874
+ function $t(e) {
2875
+ let t = $(e?.Content?.runtime_event || e?.Content?.runtimeEvent);
2876
+ if (t && String(t.event_type || "")) return {
2877
+ ...t,
2878
+ family: "runtime",
2879
+ seq: Number(e.SeqId || t.seq || 0),
2880
+ timestamp: e.Timestamp || t.timestamp
2881
+ };
2882
+ let n = $(e?.Content?.session_event || e?.Content?.sessionEvent);
2883
+ if (!n || String(n.family || "") !== "runtime") return null;
2884
+ let r = $(n.payload);
2885
+ return !r || !String(r.event_type || n.event_type || "") ? null : {
2886
+ ...r,
2887
+ family: "runtime",
2888
+ event_type: r.event_type || n.event_type,
2889
+ run_id: r.run_id || n.run_id,
2890
+ seq: Number(e.SeqId || n.seq || r.seq || 0),
2891
+ timestamp: e.Timestamp || n.timestamp || r.timestamp
2892
+ };
2893
+ }
2894
+ function en(e, t, n) {
2895
+ let r = /* @__PURE__ */ new Map();
2896
+ for (let e of t) {
2897
+ if (e.EventType !== "run_status" || $t(e)) continue;
2898
+ let t = String(e.InvocationId || e.Metadata?.run_id || "").trim();
2899
+ t && !n.has(t) && r.set(t, e);
2900
+ }
2901
+ let i = e;
2902
+ for (let [e, t] of r) {
2903
+ let n = Kt({
2904
+ ...t,
2905
+ EventId: t.EventId || `legacy-terminal:${e}:${t.SeqId || 0}`,
2906
+ Timestamp: Qt(t.Timestamp)
2907
+ });
2908
+ if (!n) continue;
2909
+ let r = (t) => (t.invocationId || t.runId) === e;
2910
+ i = i.filter((e) => !(r(e) && e.role === "system" && e.eventType === "run_status")).map((e) => {
2911
+ if (!r(e) || e.role !== "model") return e;
2912
+ let t = (e) => e === "running" || e === "paused", i = "运行已结束,未记录此工具的最终结果。";
2913
+ return {
2914
+ ...e,
2915
+ tools: e.tools && Object.fromEntries(Object.entries(e.tools).map(([e, n]) => [e, t(n.status) ? {
2916
+ ...n,
2917
+ status: "unknown",
2918
+ summary: n.summary || i
2919
+ } : n])),
2920
+ blocks: e.blocks?.map((e) => e.type === "thinking" && e.status === "streaming" ? {
2921
+ ...e,
2922
+ status: "done"
2923
+ } : e.type !== "tool" || !t(e.status) ? e : {
2924
+ ...e,
2925
+ status: "unknown",
2926
+ summary: e.summary || i,
2927
+ extra: {
2928
+ ...e.extra,
2929
+ persistedToolStatus: e.status,
2930
+ terminalRunStatus: n.status
2931
+ }
2932
+ })
2933
+ };
2934
+ }), i.push({
2935
+ ...n,
2936
+ invocationId: e,
2937
+ eventId: t.EventId
2938
+ });
2939
+ }
2940
+ return i === e ? e : P(i);
2941
+ }
2942
+ function tn(e) {
2943
+ return e.role === "model" ? {
2944
+ ...e,
2945
+ blocks: e.blocks?.length ? e.blocks : Xe({
2946
+ content: e.content,
2947
+ reasoning: e.reasoning,
2948
+ reasoningSource: e.reasoningSource,
2949
+ tools: e.tools
2950
+ })
2951
+ } : e;
2952
+ }
2953
+ function nn(e, t) {
2954
+ let n = t.find((e) => e.role === "user"), r = e.some((e) => e.role === "user"), i = t.filter((e) => e.role === "model"), a = e.filter((e) => e.role === "model").at(-1), o = i.at(-1), s = e.map((e) => e.role === "user" && n ? {
2955
+ ...e,
2956
+ ...n,
2957
+ invocationId: e.invocationId || n.invocationId
2958
+ } : tn(e === a && o ? {
2959
+ ...e,
2960
+ responseId: o.responseId || e.responseId,
2961
+ traceId: o.traceId || e.traceId,
2962
+ rootSpanId: o.rootSpanId || e.rootSpanId
2963
+ } : e));
2964
+ return n && !r ? [n, ...s] : s;
2965
+ }
2966
+ function rn(e, t) {
2967
+ let n = t.filter((e) => e.role === "model"), r = n.flatMap((e) => Object.values(e.tools || {}).map((t) => ({
2968
+ tool: t,
2969
+ scope: e.scopeId
2970
+ }))), i = (e, t) => {
2971
+ if (!e) return;
2972
+ let n = r.filter((n) => n.tool.callId === e && (!t || n.scope === t));
2973
+ return n.length === 1 ? n[0].tool : void 0;
2974
+ }, a = r.filter((t) => !e.some((e) => Object.values(e.tools || {}).some((n) => n.callId && i(n.callId, e.scopeId) === t.tool))), o = n.map((e) => String(e.reasoning || "").trim()).filter(Boolean).join("\n\n"), s = [...e].reverse().find((e) => e.role === "model");
2975
+ return !s || r.length === 0 && !o ? e : e.map((e) => {
2976
+ let t = Object.fromEntries(Object.entries(e.tools || {}).map(([t, n]) => {
2977
+ let r = i(n.callId, e.scopeId);
2978
+ return [t, r ? {
2979
+ ...n,
2980
+ ...r,
2981
+ ...j(n, r),
2982
+ name: n.name
2983
+ } : n];
2984
+ })), n = e === s ? a : [];
2985
+ for (let { tool: e, scope: r } of n) {
2986
+ let n = t[e.name] ? JSON.stringify([r || "", e.callId || e.name]) : e.name;
2987
+ t[n] = e;
2988
+ }
2989
+ let r = e.blocks?.map((t) => {
2990
+ if (t.type !== "tool") return t;
2991
+ let n = t.extra?.callId, r = typeof n == "string" ? i(n, e.scopeId) : void 0;
2992
+ return r ? {
2993
+ ...t,
2994
+ ...j(t, r)
2995
+ } : t;
2996
+ }), c = Xe({ tools: Object.fromEntries(n.map(({ tool: e, scope: t }) => [JSON.stringify([t || "", e.callId || e.name]), e])) }), l = r?.findIndex((e) => e.type === "text") ?? -1;
2997
+ return tn({
2998
+ ...e,
2999
+ ...e === s ? { reasoning: e.reasoning || o || void 0 } : {},
3000
+ tools: Object.keys(t).length ? t : e.tools,
3001
+ blocks: r && c.length ? [
3002
+ ...r.slice(0, l < 0 ? r.length : l),
3003
+ ...c,
3004
+ ...r.slice(l < 0 ? r.length : l)
3005
+ ] : r
3006
+ });
3007
+ });
3008
+ }
3009
+ function an(e) {
3010
+ return new Set(e.filter((e) => e.role === "model" && e.eventType === "assistant_message" && !!String(e.content || "").trim() && !!e.invocationId).map((e) => String(e.invocationId)));
3011
+ }
3012
+ var on = new Set([
3013
+ "approval.resolved",
3014
+ "interaction.resolved",
3015
+ "interaction.cancelled",
3016
+ "interaction.canceled",
3017
+ "a2ui.action"
3018
+ ]);
3019
+ function sn(e) {
3020
+ let t = /* @__PURE__ */ new Set();
3021
+ for (let n of e) {
3022
+ let e = $(n.Content) || {}, r = $(e.runtime_event || e.runtimeEvent) || {}, i = $(e.payload) || $(r.payload) || {}, a = String(n.EventType || e.event_type || e.eventType || r.event_type || "").toLowerCase();
3023
+ if (!on.has(a)) continue;
3024
+ let o = String(e.outcome || e.action || e.name || e.status || i.outcome || i.action || i.name || i.status || "").toLowerCase();
3025
+ if (![
3026
+ "cancel",
3027
+ "cancelled",
3028
+ "canceled"
3029
+ ].includes(o)) continue;
3030
+ let s = String(n.InvocationId || e.runId || e.run_id || i.runId || i.run_id || r.run_id || "").trim();
3031
+ s && t.add(s);
3032
+ }
3033
+ return t;
3034
+ }
3035
+ function cn(e, t, n, r = "flat-v1") {
3036
+ e = M(e);
3037
+ let i = new ot(n), a = [], o = /* @__PURE__ */ new Set(), s = /* @__PURE__ */ new Set(), c = /* @__PURE__ */ new Set(), l = [...t || []].filter((e) => {
3038
+ let t = e.Content, r = $(t?.session_event || t?.sessionEvent), i = $(t?.runtime_event || t?.runtimeEvent);
3039
+ return [
3040
+ e.SessionId,
3041
+ r?.session_id,
3042
+ i?.session_id
3043
+ ].every((e) => !e || e === n);
3044
+ }).sort((e, t) => Number(e.SeqId || 0) - Number(t.SeqId || 0)), u = Number(l[0]?.SeqId || 0) <= 1;
3045
+ for (let e of l) {
3046
+ let t = $t(e);
3047
+ if (!t) continue;
3048
+ let n = String(t.run_id || "").trim();
3049
+ if (n) {
3050
+ o.add(n), !t.parent_scope_id && [
3051
+ "run.completed",
3052
+ "run.failed",
3053
+ "run.cancelled",
3054
+ "run.canceled"
3055
+ ].includes(String(t.event_type)) && s.add(n);
3056
+ let e = $($(t.source)?.metadata), r = String(t.event_type || "") === "run.started", i = e?.native_item_kind === "userMessage";
3057
+ (u || r || i) && c.add(n);
3058
+ }
3059
+ let r = i.translate(t);
3060
+ r && a.push({
3061
+ ...r,
3062
+ SeqId: Number(e.SeqId || r.SeqId || 0),
3063
+ Timestamp: Qt(e.Timestamp || r.Timestamp)
3064
+ });
3065
+ }
3066
+ let d = Jt(a).filter((e) => e.invocationId && o.has(e.invocationId)), f = /* @__PURE__ */ new Map();
3067
+ for (let t of e) {
3068
+ let e = String(t.invocationId || "");
3069
+ e && f.set(e, [...f.get(e) || [], t]);
3070
+ }
3071
+ let p = new Set([...an(d)].filter((e) => c.has(e))), m = /* @__PURE__ */ new Map();
3072
+ for (let e of l) {
3073
+ let t = $t(e);
3074
+ if (t?.schema_version !== 2 || !t.run_id) continue;
3075
+ let n = String(t.run_id), r = m.get(n) || [];
3076
+ r.push(t), m.set(n, r);
3077
+ }
3078
+ let h = (e) => [
3079
+ $(e.initial),
3080
+ $(e.snapshot),
3081
+ $(e.update)
3082
+ ].some((e) => $(e?.data)?.schema === "execution.scope/v1" || Array.isArray(e?.parts) && e.parts.some((e) => $($(e)?.data)?.schema === "execution.scope/v1")), g = new Set([...m].filter(([, e]) => e.some(h)).map(([e]) => e)), _ = /* @__PURE__ */ new Map(), v = /* @__PURE__ */ new Set();
3083
+ for (let [e, t] of m) {
3084
+ if (!g.has(e) && !p.has(e) && !t.some((e) => ["item.failed", "run.failed"].includes(String(e.event_type)))) continue;
3085
+ let i = t.some((e) => e.event_type === "run.started" && !e.parent_scope_id), a = /* @__PURE__ */ new Set(), o = t.every((e) => {
3086
+ let t = JSON.stringify([e.scope_id, e.item_id]);
3087
+ return e.event_type === "item.started" && a.add(t), !h(e) || a.has(t);
3088
+ });
3089
+ if (!(!i || !o)) try {
3090
+ let i = new S(n, void 0, r);
3091
+ t.forEach((e) => i.apply(e));
3092
+ let a = i.snapshot(), o = me(a, { profile: r }), s = Qt(t[0].timestamp);
3093
+ _.set(e, z({
3094
+ state: a,
3095
+ presentation: o,
3096
+ runId: e,
3097
+ cursor: Math.max(...t.map((e) => Number(e.seq || 0)))
3098
+ }).messages.map((n, r) => {
3099
+ let i = t.find((e) => e.item_id === n.itemId && (!n.scopeId || e.scope_id === n.scopeId)) || t[0];
3100
+ return {
3101
+ ...n,
3102
+ invocationId: e,
3103
+ timestamp: s + r,
3104
+ historyOrder: {
3105
+ seq: Number(i.seq),
3106
+ index: r
3107
+ }
3108
+ };
3109
+ }));
3110
+ } catch (t) {
3111
+ v.add(e), console.warn(`[SessionHistory] strict replay skipped for run ${e}:`, t instanceof Error ? t.message : "invalid runtime history");
3112
+ }
3113
+ }
3114
+ for (let e of new Set([...g, ...v])) _.has(e) || p.delete(e);
3115
+ let y = sn(l), b = [...p].flatMap((e) => nn(d.filter((t) => t.invocationId === e), f.get(e) || [])), x = /* @__PURE__ */ new Map(), C = [];
3116
+ for (let [e, t] of f) {
3117
+ if (p.has(e) || !o.has(e)) continue;
3118
+ let n = d.filter((t) => t.invocationId === e), r = t.some((e) => e.role === "model" && String(e.content || "").trim());
3119
+ x.set(e, r ? rn(t, n) : t), r || C.push(...n.map(tn));
3120
+ }
3121
+ for (let e of o) p.has(e) || f.has(e) || C.push(...d.filter((t) => t.invocationId === e).map(tn));
3122
+ let w = new Map([...x.values()].flat().map((e) => [e.id, e])), T = P([
3123
+ ...e.filter((e) => !(e.role === "model" && e.invocationId && y.has(e.invocationId)) && (e.role === "a2ui" || !e.invocationId || !p.has(e.invocationId))).map((e) => w.get(e.id) || e),
3124
+ ...b,
3125
+ ...C
3126
+ ]);
3127
+ for (let [e, t] of _) {
3128
+ if (!t.length) continue;
3129
+ let n = (t) => t.invocationId === e || t.runId === e, r = T.findIndex(n), i = T.filter(n), a = i.find((e) => e.role === "user"), o = g.has(e) ? a && !t.some((e) => e.role === "user") ? [a, ...t] : t : nn(t, i), s = T.filter((e) => !n(e)), c = r < 0 ? s.length : r;
3130
+ T = [
3131
+ ...s.slice(0, c),
3132
+ ...o,
3133
+ ...s.slice(c)
3134
+ ], p.add(e);
3135
+ }
3136
+ return {
3137
+ messages: M(P(en(T, l, s))),
3138
+ canonicalRunIds: [...p],
3139
+ translatedEvents: a
3140
+ };
3141
+ }
3142
+ //#endregion
3143
+ export { ee as A, ke as C, we as D, Ee as E, S as M, x as N, me as O, z as S, De as T, Ye as _, Zt as a, Je as b, Et as c, ot as d, st as f, Ke as g, Xe as h, Jt as i, w as j, te as k, ut as l, Ue as m, cn as n, Xt as o, We as p, Gt as r, Yt as s, $t as t, yt as u, Ge as v, M as w, Ve as x, qe as y };