@kingsoftcloud/ksadk-web 0.3.2 → 0.3.4

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 (185) hide show
  1. package/CHANGELOG.md +77 -0
  2. package/README.md +77 -4
  3. package/RELEASE_PROVENANCE.json +2 -2
  4. package/dist-ksadk/assets/{ArtifactsPanel-DSU1sWD_.js → ArtifactsPanel-10GPXIJ6.js} +1 -1
  5. package/dist-ksadk/assets/{CodeBlock-DOH6MVcn.js → CodeBlock-BXk1l-G1.js} +1 -1
  6. package/dist-ksadk/assets/{MathMessageMarkdown-BL8my1R2.js → MathMessageMarkdown-D9uB55Oh.js} +1 -1
  7. package/dist-ksadk/assets/{MermaidBlock-Dz4IP-Tx.js → MermaidBlock--OEYoXIJ.js} +6 -6
  8. package/dist-ksadk/assets/{NativeTerminalPanel-DUrK0JpZ.js → NativeTerminalPanel-DrEe1syq.js} +1 -1
  9. package/dist-ksadk/assets/{abnfDiagram-VCTEODGH-g20pFzNV.js → abnfDiagram-VCTEODGH-eeTFJLvo.js} +1 -1
  10. package/dist-ksadk/assets/{arc-C4FzinUA.js → arc-S3ZNbIAd.js} +1 -1
  11. package/dist-ksadk/assets/architecture-7GRP2DOG-Dk9nRQ6-.js +1 -0
  12. package/dist-ksadk/assets/{architectureDiagram-5GKGNRK7-DpIqL5h4.js → architectureDiagram-5GKGNRK7-CRjgEJDX.js} +1 -1
  13. package/dist-ksadk/assets/{blockDiagram-NRAW4CY4-BjkljTNz.js → blockDiagram-NRAW4CY4-C7bsBoNH.js} +1 -1
  14. package/dist-ksadk/assets/{c4Diagram-UCG6FXSJ-Dk_ieq2X.js → c4Diagram-UCG6FXSJ-BX3c33Ob.js} +1 -1
  15. package/dist-ksadk/assets/channel-s354Yo6o.js +1 -0
  16. package/dist-ksadk/assets/chunk-2Q5K7J3B-CDpPpKR5.js +1 -0
  17. package/dist-ksadk/assets/{chunk-5VM5RSS4-BVHAchFb.js → chunk-5VM5RSS4-bZ7ws-8m.js} +1 -1
  18. package/dist-ksadk/assets/{chunk-F27PBJKO-C-ipQzuS.js → chunk-F27PBJKO-D2r0pvhY.js} +1 -1
  19. package/dist-ksadk/assets/{chunk-G27WJ6UU-C2EyJbIK.js → chunk-G27WJ6UU-CZwF3oUP.js} +1 -1
  20. package/dist-ksadk/assets/{chunk-JWPE2WC7-vYvVJb_M.js → chunk-JWPE2WC7-DigFYCML.js} +1 -1
  21. package/dist-ksadk/assets/{chunk-LCL6LL3I-BcI6ysPT.js → chunk-LCL6LL3I-CnorGV5q.js} +1 -1
  22. package/dist-ksadk/assets/{chunk-POPQ4Y6H-C030x_Z1.js → chunk-POPQ4Y6H-CexntQA-.js} +1 -1
  23. package/dist-ksadk/assets/{chunk-RHFEMEQ7-yyGpsz4n.js → chunk-RHFEMEQ7-CHTC7UxA.js} +1 -1
  24. package/dist-ksadk/assets/{chunk-SVP7TREG-BLTlmMU7.js → chunk-SVP7TREG-BC1EWt_-.js} +1 -1
  25. package/dist-ksadk/assets/{chunk-XXDRQBXY-C_32ArgP.js → chunk-XXDRQBXY-BTG24xN0.js} +1 -1
  26. package/dist-ksadk/assets/classDiagram-DTDB5LWJ-Dd0nucl9.js +1 -0
  27. package/dist-ksadk/assets/classDiagram-v2-JRS7N3AN-Dd0nucl9.js +1 -0
  28. package/dist-ksadk/assets/{cose-bilkent-JH36ORCC-9YQYwdl0.js → cose-bilkent-JH36ORCC-CkonRdAS.js} +1 -1
  29. package/dist-ksadk/assets/cynefin-OW5HDTMX-BBlT4uMf.js +1 -0
  30. package/dist-ksadk/assets/{cynefinDiagram-5FMLGOSQ-ErLF5N13.js → cynefinDiagram-5FMLGOSQ-DEOCBe15.js} +1 -1
  31. package/dist-ksadk/assets/{dagre-3AP2YEHR-DFiQF-6f.js → dagre-3AP2YEHR-DBThPuLa.js} +1 -1
  32. package/dist-ksadk/assets/{dagre-BuhGnRI1.js → dagre-ClTPrTht.js} +1 -1
  33. package/dist-ksadk/assets/{diagram-S7CK7UJ4-Bt1v8-GC.js → diagram-S7CK7UJ4-DsZHrnpJ.js} +1 -1
  34. package/dist-ksadk/assets/{diagram-UQ7AKVKN-DSCxJdBK.js → diagram-UQ7AKVKN-BJR4nhVr.js} +1 -1
  35. package/dist-ksadk/assets/{diagram-VSXAHHWV-DHfYwp_9.js → diagram-VSXAHHWV-xzcERX0a.js} +1 -1
  36. package/dist-ksadk/assets/{diagram-VX7I27RA-DdzD-4Le.js → diagram-VX7I27RA-tn2xM1xu.js} +1 -1
  37. package/dist-ksadk/assets/{diagram-Z3DM3KII-Du-nAJ9F.js → diagram-Z3DM3KII-BZPGBkw5.js} +1 -1
  38. package/dist-ksadk/assets/{dist-ClxGviKw.js → dist-4zN1_tq5.js} +1 -1
  39. package/dist-ksadk/assets/{dist-BE7bu-DL.js → dist-8VWI-2bZ.js} +1 -1
  40. package/dist-ksadk/assets/{dist-BX8z72IC.js → dist-B-46q3Hk.js} +1 -1
  41. package/dist-ksadk/assets/{dist-BjU6y-A2.js → dist-B0gdk4rl.js} +1 -1
  42. package/dist-ksadk/assets/{dist-C_wsv-Qd.js → dist-B1oWRmrH.js} +1 -1
  43. package/dist-ksadk/assets/{dist-ngtN0DJB.js → dist-BI1qQH4n.js} +1 -1
  44. package/dist-ksadk/assets/{dist-dydu68Fl.js → dist-BJLUwH6W.js} +1 -1
  45. package/dist-ksadk/assets/{dist-Dq9gLD7L.js → dist-Bbwqa8TG.js} +1 -1
  46. package/dist-ksadk/assets/{dist-CttYUqzS.js → dist-Btyo4vX4.js} +1 -1
  47. package/dist-ksadk/assets/{dist-CXYYBw3_.js → dist-C71CsujO.js} +1 -1
  48. package/dist-ksadk/assets/{dist-dDdADKFA.js → dist-CXpH_Rdd.js} +1 -1
  49. package/dist-ksadk/assets/{dist-CSh_DE14.js → dist-Cq4OL6nO.js} +1 -1
  50. package/dist-ksadk/assets/{dist-8hqcx0sU.js → dist-D1V3f1B4.js} +1 -1
  51. package/dist-ksadk/assets/{dist-BVDffZ0U.js → dist-DJmCPRVB.js} +1 -1
  52. package/dist-ksadk/assets/{dist-DnfXs8Vn.js → dist-DQAB0qn_.js} +1 -1
  53. package/dist-ksadk/assets/{dist-DNp6rLRA.js → dist-DUwKa_l6.js} +1 -1
  54. package/dist-ksadk/assets/{dist-B7seoj3d.js → dist-DdRBIceA.js} +1 -1
  55. package/dist-ksadk/assets/{dist-BytlxIDT.js → dist-DfGFrb0v.js} +1 -1
  56. package/dist-ksadk/assets/{dist-Bf2M8m3N.js → dist-DjhwGUgZ.js} +1 -1
  57. package/dist-ksadk/assets/{dist-W-TIVntx.js → dist-DyqV4fL4.js} +1 -1
  58. package/dist-ksadk/assets/{dist-DUX-id_F.js → dist-M67Hwu0n.js} +1 -1
  59. package/dist-ksadk/assets/{dist-DmldrHd_.js → dist-u8tVH5Ak.js} +1 -1
  60. package/dist-ksadk/assets/{ebnfDiagram-PWID7BFC-Da9C9NO1.js → ebnfDiagram-PWID7BFC-DVfMsLge.js} +1 -1
  61. package/dist-ksadk/assets/{erDiagram-SSCWMZ5O-EAGqqR79.js → erDiagram-SSCWMZ5O-CWEZSYaw.js} +1 -1
  62. package/dist-ksadk/assets/eventmodeling-NTZA5JFV-Dr5okd8V.js +1 -0
  63. package/dist-ksadk/assets/flowDiagram-A5DVABFB-VEO-cVcO.js +1 -0
  64. package/dist-ksadk/assets/{ganttDiagram-EL5Y4UJY-DqJsKb59.js → ganttDiagram-EL5Y4UJY-TWIxoyYL.js} +1 -1
  65. package/dist-ksadk/assets/gitGraph-4MIJSDKK-CKiKY9Cf.js +1 -0
  66. package/dist-ksadk/assets/{gitGraphDiagram-WWUBYQGX-B6g4dtDi.js → gitGraphDiagram-WWUBYQGX-DDh2hd2S.js} +1 -1
  67. package/dist-ksadk/assets/index-B2k_urY8.js +241 -0
  68. package/dist-ksadk/assets/index-ILPt6IWF.css +2 -0
  69. package/dist-ksadk/assets/info-A6RAGUB7-ubA4Nm_p.js +1 -0
  70. package/dist-ksadk/assets/{infoDiagram-RXCK75RN-Djm_nbd5.js → infoDiagram-RXCK75RN-CgoyWiDR.js} +1 -1
  71. package/dist-ksadk/assets/{ishikawaDiagram-5VMMS53U-DWya9SG2.js → ishikawaDiagram-5VMMS53U-CyRIu_om.js} +1 -1
  72. package/dist-ksadk/assets/{journeyDiagram-EYS64GPL-DutxhSNI.js → journeyDiagram-EYS64GPL-DN5JXHzN.js} +1 -1
  73. package/dist-ksadk/assets/{kanban-definition-3QL26DDD-DJdOF8Fm.js → kanban-definition-3QL26DDD-CNP3ui66.js} +1 -1
  74. package/dist-ksadk/assets/{katex-vFWytM5c.js → katex-DWEzQy1d.js} +1 -1
  75. package/dist-ksadk/assets/{linear-BHjG8b-J.js → linear-C-qHp7d2.js} +1 -1
  76. package/dist-ksadk/assets/{mermaid-parser.core-KGSy4jWT.js → mermaid-parser.core-Cl-K943T.js} +2 -2
  77. package/dist-ksadk/assets/{mindmap-definition-FBJOCRG2-9-HC88D4.js → mindmap-definition-FBJOCRG2-D5DB5mYm.js} +1 -1
  78. package/dist-ksadk/assets/packet-AYTQ26CC-C7R3NUlK.js +1 -0
  79. package/dist-ksadk/assets/{pegDiagram-XKGWAZYB-DrEZd4fD.js → pegDiagram-XKGWAZYB-BQVc9ia_.js} +1 -1
  80. package/dist-ksadk/assets/pie-WAS4IAKB-BIT6qjr3.js +1 -0
  81. package/dist-ksadk/assets/{pieDiagram-E7YTZNPT-DpjqOFFp.js → pieDiagram-E7YTZNPT-B6UCAvtY.js} +1 -1
  82. package/dist-ksadk/assets/{quadrantDiagram-AXDQQJYC-BiNhgOXX.js → quadrantDiagram-AXDQQJYC-mIr2d3eb.js} +1 -1
  83. package/dist-ksadk/assets/radar-RG4KPBEZ-l5noef9X.js +1 -0
  84. package/dist-ksadk/assets/railroad-74A4TZTK-Baea6ZvM.js +1 -0
  85. package/dist-ksadk/assets/railroad-abnf-HS5TGJTU-OKn8zZnU.js +1 -0
  86. package/dist-ksadk/assets/railroad-ebnf-LZEXJU2U-DdKAKC4l.js +1 -0
  87. package/dist-ksadk/assets/railroad-peg-WCYAUIDC-Bds8udlG.js +1 -0
  88. package/dist-ksadk/assets/{railroadDiagram-O6MQD6OU-_Y3Ojg6G.js → railroadDiagram-O6MQD6OU-Dt3FFDmK.js} +1 -1
  89. package/dist-ksadk/assets/{requirementDiagram-EFPCY7ZU-DeCFdg9K.js → requirementDiagram-EFPCY7ZU-Cz_vQtgF.js} +1 -1
  90. package/dist-ksadk/assets/{sankeyDiagram-P5KCCOFB-CPH75rhw.js → sankeyDiagram-P5KCCOFB-wlO1TsH5.js} +1 -1
  91. package/dist-ksadk/assets/{sequenceDiagram-WJ2MYXX4-OEpQQdr4.js → sequenceDiagram-WJ2MYXX4-CHRbElYq.js} +1 -1
  92. package/dist-ksadk/assets/{sizeCapture-X5ZJPWSS-heUErzhI.js → sizeCapture-X5ZJPWSS-DXX54iyO.js} +1 -1
  93. package/dist-ksadk/assets/{stateDiagram-HBIQ2CUA-DvA3jSMB.js → stateDiagram-HBIQ2CUA-Dp_lw9II.js} +1 -1
  94. package/dist-ksadk/assets/stateDiagram-v2-4QOOHH4V-5b-8o2iS.js +1 -0
  95. package/dist-ksadk/assets/{swimlanes-XN3QIQJK-CR-dfN_t.js → swimlanes-XN3QIQJK-D8O5UlLG.js} +1 -1
  96. package/dist-ksadk/assets/swimlanesDiagram-VK2B7HYN-uf6WPRA4.js +8 -0
  97. package/dist-ksadk/assets/{timeline-definition-24CTP7MA-DAVJf1TX.js → timeline-definition-24CTP7MA-8mfKcxvG.js} +1 -1
  98. package/dist-ksadk/assets/treeView-Q6P3EWNA-CK5CSrSO.js +1 -0
  99. package/dist-ksadk/assets/treemap-WGGIJYW6-Ch2BKgAM.js +1 -0
  100. package/dist-ksadk/assets/{vennDiagram-4TSXK5OY-DwM8eDkh.js → vennDiagram-4TSXK5OY-EnNl0DE7.js} +1 -1
  101. package/dist-ksadk/assets/wardley-WFR3VGLG-YW3Q0ml_.js +1 -0
  102. package/dist-ksadk/assets/{wardleyDiagram-VM6X3IG4-DZL-Zz2t.js → wardleyDiagram-VM6X3IG4-DBu5fbYr.js} +1 -1
  103. package/dist-ksadk/assets/{xychartDiagram-S5SC5T6Z-Dy6Yh_hu.js → xychartDiagram-S5SC5T6Z-BSVXs4IY.js} +1 -1
  104. package/dist-ksadk/index.html +2 -2
  105. package/dist-lib/App.d.ts.map +1 -1
  106. package/dist-lib/{ArtifactsPanel-DDCKcvaL.js → ArtifactsPanel-D36BykpN.js} +1 -1
  107. package/dist-lib/{CodeBlock-BS9DcS5a.js → CodeBlock-BlEkUJ8C.js} +1 -1
  108. package/dist-lib/{MathMessageMarkdown-CpCcJFjZ.js → MathMessageMarkdown-CcnPvEgd.js} +2 -2
  109. package/dist-lib/{NativeTerminalPanel-CDqCdIly.js → NativeTerminalPanel-Dix1UX_z.js} +1 -1
  110. package/dist-lib/{WorkspacePanelContainer-DzbAkIfx.js → WorkspacePanelContainer-Cr1BwR1P.js} +1391 -1609
  111. package/dist-lib/components/chat/A2UIActivityMessage.d.ts.map +1 -1
  112. package/dist-lib/components/chat/ChatMessageList.d.ts +9 -0
  113. package/dist-lib/components/chat/ChatMessageList.d.ts.map +1 -1
  114. package/dist-lib/components/chat/ProcessingBlocksView.d.ts.map +1 -1
  115. package/dist-lib/components.js +4 -4
  116. package/dist-lib/conversation.js +28 -0
  117. package/dist-lib/core/conversation/client.d.ts +26 -0
  118. package/dist-lib/core/conversation/client.d.ts.map +1 -0
  119. package/dist-lib/core/conversation/contracts.d.ts +20 -0
  120. package/dist-lib/core/conversation/contracts.d.ts.map +1 -0
  121. package/dist-lib/core/conversation/errors.d.ts +12 -0
  122. package/dist-lib/core/conversation/errors.d.ts.map +1 -0
  123. package/dist-lib/core/conversation/hosted.d.ts +25 -0
  124. package/dist-lib/core/conversation/hosted.d.ts.map +1 -0
  125. package/dist-lib/core/conversation/index.d.ts +8 -0
  126. package/dist-lib/core/conversation/index.d.ts.map +1 -0
  127. package/dist-lib/core/conversation/presentation.d.ts +16 -0
  128. package/dist-lib/core/conversation/presentation.d.ts.map +1 -0
  129. package/dist-lib/core/conversation/reducer.d.ts +19 -0
  130. package/dist-lib/core/conversation/reducer.d.ts.map +1 -0
  131. package/dist-lib/core/conversation/renderer-registry.d.ts +20 -0
  132. package/dist-lib/core/conversation/renderer-registry.d.ts.map +1 -0
  133. package/dist-lib/core/conversation/types.d.ts +176 -0
  134. package/dist-lib/core/conversation/types.d.ts.map +1 -0
  135. package/dist-lib/core/run/blocks.d.ts +7 -0
  136. package/dist-lib/core/run/blocks.d.ts.map +1 -1
  137. package/dist-lib/core/run/dispatcher.d.ts.map +1 -1
  138. package/dist-lib/core/run/engine.d.ts +3 -0
  139. package/dist-lib/core/run/engine.d.ts.map +1 -1
  140. package/dist-lib/core/run/types.d.ts +8 -0
  141. package/dist-lib/core/run/types.d.ts.map +1 -1
  142. package/dist-lib/core/stream/kernel-events.d.ts.map +1 -1
  143. package/dist-lib/hooks/useRunAgent.d.ts.map +1 -1
  144. package/dist-lib/hooks/useSessionLifecycle.d.ts.map +1 -1
  145. package/dist-lib/persisted-session-history-C0N8TrMb.js +2033 -0
  146. package/dist-lib/public/conversation.d.ts +13 -0
  147. package/dist-lib/public/conversation.d.ts.map +1 -0
  148. package/dist-lib/public/runtime.d.ts +2 -0
  149. package/dist-lib/public/runtime.d.ts.map +1 -1
  150. package/dist-lib/public/types.d.ts +1 -0
  151. package/dist-lib/public/types.d.ts.map +1 -1
  152. package/dist-lib/runtime.js +1362 -2197
  153. package/dist-lib/styles.css +1 -1
  154. package/dist-lib/tool-display-C4tPXce0.js +0 -0
  155. package/dist-lib/utils/persisted-session-history.d.ts +27 -0
  156. package/dist-lib/utils/persisted-session-history.d.ts.map +1 -0
  157. package/dist-lib/utils/recovered-run.d.ts.map +1 -1
  158. package/dist-lib/utils/session-event-history.d.ts.map +1 -1
  159. package/dist-lib/utils/session-events.d.ts.map +1 -1
  160. package/package.json +14 -3
  161. package/schemas/release-provenance.schema.json +34 -0
  162. package/dist-ksadk/assets/architecture-7GRP2DOG-DjKratdf.js +0 -1
  163. package/dist-ksadk/assets/channel-4cQHKtx1.js +0 -1
  164. package/dist-ksadk/assets/chunk-2Q5K7J3B-DmgWkESh.js +0 -1
  165. package/dist-ksadk/assets/classDiagram-DTDB5LWJ-DLFd9Xi0.js +0 -1
  166. package/dist-ksadk/assets/classDiagram-v2-JRS7N3AN-DLFd9Xi0.js +0 -1
  167. package/dist-ksadk/assets/cynefin-OW5HDTMX-DjEM48Qp.js +0 -1
  168. package/dist-ksadk/assets/eventmodeling-NTZA5JFV-CMgMaJrC.js +0 -1
  169. package/dist-ksadk/assets/flowDiagram-A5DVABFB-BBKL0x3P.js +0 -1
  170. package/dist-ksadk/assets/gitGraph-4MIJSDKK-Dw63mrhw.js +0 -1
  171. package/dist-ksadk/assets/index-8ipRcQ-M.js +0 -240
  172. package/dist-ksadk/assets/index-ByFCqjlh.css +0 -2
  173. package/dist-ksadk/assets/info-A6RAGUB7-B6iAblII.js +0 -1
  174. package/dist-ksadk/assets/packet-AYTQ26CC-BOYAipuq.js +0 -1
  175. package/dist-ksadk/assets/pie-WAS4IAKB-Ctq0Kbku.js +0 -1
  176. package/dist-ksadk/assets/radar-RG4KPBEZ-DN0-ul_0.js +0 -1
  177. package/dist-ksadk/assets/railroad-74A4TZTK-C6cC_to0.js +0 -1
  178. package/dist-ksadk/assets/railroad-abnf-HS5TGJTU-BimjIuRc.js +0 -1
  179. package/dist-ksadk/assets/railroad-ebnf-LZEXJU2U-CvY65ABq.js +0 -1
  180. package/dist-ksadk/assets/railroad-peg-WCYAUIDC-D72Iq_Tv.js +0 -1
  181. package/dist-ksadk/assets/stateDiagram-v2-4QOOHH4V-BgQY03nz.js +0 -1
  182. package/dist-ksadk/assets/swimlanesDiagram-VK2B7HYN-DLVw3q5Q.js +0 -8
  183. package/dist-ksadk/assets/treeView-Q6P3EWNA-BMpO0wsB.js +0 -1
  184. package/dist-ksadk/assets/treemap-WGGIJYW6-Et0aqzU3.js +0 -1
  185. package/dist-ksadk/assets/wardley-WFR3VGLG-CbY0mhpl.js +0 -1
@@ -0,0 +1,2033 @@
1
+ import { n as e } from "./tool-display-C4tPXce0.js";
2
+ //#region src/core/conversation/errors.ts
3
+ var t = class extends Error {
4
+ code;
5
+ status;
6
+ capability;
7
+ runId;
8
+ cursor;
9
+ cause;
10
+ constructor(e, t, n = {}) {
11
+ super(t), this.name = "ConversationClientError", this.code = e, this.status = n.status, this.capability = n.capability, this.runId = n.runId, this.cursor = n.cursor, this.cause = n.cause;
12
+ }
13
+ }, n = "conversation.ksadk.io/v1", r = /^[a-z][a-z0-9]*(?:[._-][a-z0-9]+)*$/, i = new Set([
14
+ "native",
15
+ "translated",
16
+ "degraded",
17
+ "unavailable"
18
+ ]), a = new Set([
19
+ "user_message",
20
+ "assistant_text",
21
+ "reasoning",
22
+ "tool_call",
23
+ "approval",
24
+ "progress",
25
+ "plan",
26
+ "goal",
27
+ "artifact",
28
+ "a2ui",
29
+ "error",
30
+ "unknown"
31
+ ]), o = new Set([
32
+ "append",
33
+ "replace",
34
+ "completed"
35
+ ]), s = new Set([
36
+ "pending",
37
+ "streaming",
38
+ "completed",
39
+ "failed"
40
+ ]), c = new Set([
41
+ "public",
42
+ "internal",
43
+ "hidden"
44
+ ]), l = new Set([
45
+ "apiVersion",
46
+ "kind",
47
+ "inputId",
48
+ "sessionId",
49
+ "idempotencyKey",
50
+ "parts",
51
+ "modelRef",
52
+ "reasoning",
53
+ "extensions"
54
+ ]), u = new Set(["kind", "text"]), d = new Set([
55
+ "kind",
56
+ "attachmentRef",
57
+ "mediaType",
58
+ "name"
59
+ ]), f = "ksadk.approval", p = "ksadk.collaboration", m = "ksadk.goal";
60
+ function h(e) {
61
+ return typeof e == "object" && e && !Array.isArray(e) ? e : null;
62
+ }
63
+ function g(e, t) {
64
+ return typeof e == "string" && e.length > 0 && e.length <= t;
65
+ }
66
+ function _(e, t, n = !1) {
67
+ return e == null || typeof e == "string" && e.length <= t && (n || e.length > 0);
68
+ }
69
+ function v(e, t) {
70
+ return Object.keys(e).every((e) => t.has(e));
71
+ }
72
+ function y(e) {
73
+ let t = h(e);
74
+ return !t || typeof t.kind != "string" ? null : t.kind === "text" ? !v(t, u) || !g(t.text, 131072) ? null : {
75
+ kind: "text",
76
+ text: t.text
77
+ } : t.kind === "attachment" ? !v(t, d) || !g(t.attachmentRef, 2048) || !g(t.mediaType, 256) || !_(t.name, 1024, !0) ? null : {
78
+ kind: "attachment",
79
+ attachmentRef: t.attachmentRef,
80
+ mediaType: t.mediaType,
81
+ ...t.name === void 0 ? {} : { name: t.name }
82
+ } : null;
83
+ }
84
+ function b(e) {
85
+ if (e === void 0) return {};
86
+ let t = h(e);
87
+ if (!t || Object.keys(t).some((e) => !r.test(e) || !e.includes("."))) return null;
88
+ let n = t[f], i = t[p], a = t[m];
89
+ return n !== void 0 && ![
90
+ "ask",
91
+ "risk",
92
+ "full"
93
+ ].includes(String(n)) || i !== void 0 && !["default", "plan"].includes(String(i)) || a !== void 0 && !g(a, 4096) ? null : { ...t };
94
+ }
95
+ function ee(e) {
96
+ let t = h(e);
97
+ if (!t || !v(t, l) || t.apiVersion !== n || t.kind !== "ConversationInput" || !g(t.inputId, 256) || !g(t.sessionId, 256) || !g(t.idempotencyKey, 512) || !Array.isArray(t.parts) || t.parts.length === 0 || !_(t.modelRef, 256) || !_(t.reasoning, 64)) return null;
98
+ let r = t.parts.map(y), i = b(t.extensions);
99
+ return r.some((e) => e === null) || i === null ? null : {
100
+ apiVersion: n,
101
+ kind: "ConversationInput",
102
+ inputId: t.inputId,
103
+ sessionId: t.sessionId,
104
+ idempotencyKey: t.idempotencyKey,
105
+ parts: r,
106
+ ...t.modelRef === void 0 ? {} : { modelRef: t.modelRef },
107
+ ...t.reasoning === void 0 ? {} : { reasoning: t.reasoning },
108
+ ...t.extensions === void 0 ? {} : { extensions: i }
109
+ };
110
+ }
111
+ function te(e) {
112
+ let r = ee({
113
+ ...e,
114
+ apiVersion: e.apiVersion || n,
115
+ kind: e.kind || "ConversationInput"
116
+ });
117
+ if (!r) throw new t("conversation_contract_mismatch", "Conversation input does not match conversation.ksadk.io/v1.");
118
+ return r;
119
+ }
120
+ function ne(e) {
121
+ let t = e.parts.map((e) => e.kind === "text" ? "text" : e.mediaType.toLowerCase().startsWith("image/") ? "attachment.image" : "attachment.file");
122
+ e.modelRef && t.push("model.select"), e.reasoning && t.push("reasoning.effort");
123
+ for (let n of Object.keys(e.extensions || {})) n === f ? t.push("approval") : n === p ? e.extensions?.[n] === "plan" && t.push("plan") : n === m ? t.push("goal") : t.push(n);
124
+ return [...new Set(t)];
125
+ }
126
+ function re(e, n) {
127
+ if (e.sessionId !== n.sessionId) throw new t("conversation_session_mismatch", "Conversation input session does not match the active surface.");
128
+ for (let r of ne(n)) if (!ce(e, r)) throw new t("conversation_input_unsupported", "Conversation input is not declared by the active surface.", { capability: r });
129
+ return n;
130
+ }
131
+ function ie(e) {
132
+ let t = h(e);
133
+ return !t || !g(t.name, 128) || !r.test(t.name) || !i.has(t.mode) || !_(t.reason, 512, !0) || (t.mode === "degraded" || t.mode === "unavailable") && !g(t.reason, 512) ? null : {
134
+ name: t.name,
135
+ mode: t.mode,
136
+ ...t.reason === void 0 ? {} : { reason: t.reason }
137
+ };
138
+ }
139
+ function ae(e) {
140
+ if (e === void 0) return [];
141
+ if (!Array.isArray(e)) return null;
142
+ let t = e.map(ie);
143
+ if (t.some((e) => e === null)) return null;
144
+ let n = t;
145
+ return new Set(n.map((e) => e.name)).size === n.length ? n : null;
146
+ }
147
+ function oe(e) {
148
+ let t = h(e);
149
+ if (!t || t.apiVersion !== n || t.kind !== "ConversationSurface" || !g(t.surfaceId, 256) || !g(t.sessionId, 256) || !g(t.providerRef, 256)) return null;
150
+ let r = ae(t.inputs), i = ae(t.outputs);
151
+ return !r || !i ? null : {
152
+ apiVersion: n,
153
+ kind: "ConversationSurface",
154
+ surfaceId: t.surfaceId,
155
+ sessionId: t.sessionId,
156
+ providerRef: t.providerRef,
157
+ inputs: r,
158
+ outputs: i
159
+ };
160
+ }
161
+ function se(e) {
162
+ let t = h(e);
163
+ if (!t || t.apiVersion !== n || t.kindVersion !== 1 || !g(t.itemId, 512) || !_(t.parentItemId, 512) || !Array.isArray(t.sourceEventIds) || t.sourceEventIds.length === 0 || t.sourceEventIds.some((e) => typeof e != "string" || e.length === 0) || new Set(t.sourceEventIds).size !== t.sourceEventIds.length || !g(t.sessionId, 256) || !g(t.runId, 256) || !g(t.kind, 128) || !o.has(t.operation) || !s.has(t.lifecycle) || t.visibility !== void 0 && !c.has(t.visibility) || !g(t.payloadSchemaRef, 256) || !_(t.capabilityRef, 256, !0)) return null;
164
+ let r = t.operation, i = t.lifecycle;
165
+ if (r === "completed" && i !== "completed" && i !== "failed") return null;
166
+ let l = t.payload === void 0 ? {} : h(t.payload), u = t.nativeRef === void 0 ? {} : h(t.nativeRef);
167
+ if (!l || !u) return null;
168
+ let d = String(t.kind || "unknown"), f = a.has(d) ? d : "unknown";
169
+ return {
170
+ apiVersion: n,
171
+ kindVersion: 1,
172
+ itemId: t.itemId,
173
+ ...t.parentItemId === void 0 ? {} : { parentItemId: t.parentItemId },
174
+ sourceEventIds: [...t.sourceEventIds],
175
+ sessionId: t.sessionId,
176
+ runId: t.runId,
177
+ kind: f,
178
+ operation: r,
179
+ lifecycle: i,
180
+ visibility: f === "unknown" && d !== "unknown" ? "hidden" : t.visibility || "public",
181
+ payloadSchemaRef: t.payloadSchemaRef,
182
+ payload: f === "unknown" && d !== "unknown" ? {
183
+ originalKind: d,
184
+ summary: "This content type is not supported by the current renderer."
185
+ } : { ...l },
186
+ ...t.capabilityRef === void 0 ? {} : { capabilityRef: t.capabilityRef },
187
+ nativeRef: { ...u }
188
+ };
189
+ }
190
+ function ce(e, ...t) {
191
+ return e.inputs.some((e) => t.includes(e.name) && (e.mode === "native" || e.mode === "translated"));
192
+ }
193
+ //#endregion
194
+ //#region src/core/conversation/presentation.ts
195
+ var le = {
196
+ user_message: "conversation.item.user_message/v1",
197
+ assistant_text: "conversation.item.assistant_text/v1",
198
+ reasoning: "conversation.item.reasoning/v1",
199
+ tool_call: "conversation.item.tool-call/v1",
200
+ approval: "conversation.item.approval/v1",
201
+ artifact: "conversation.item.artifact/v1",
202
+ a2ui: "conversation.item.a2ui/v1",
203
+ error: "conversation.item.error/v1"
204
+ };
205
+ function x(e) {
206
+ return e.lifecycle === "completed" || e.lifecycle === "failed";
207
+ }
208
+ var ue = new Set([
209
+ "aborted",
210
+ "canceled",
211
+ "cancelled",
212
+ "failed",
213
+ "incomplete",
214
+ "interrupted"
215
+ ]);
216
+ function S(e) {
217
+ if (e.kind !== "progress" && e.kind !== "error" || !x(e)) return;
218
+ let t = typeof e.payload.status == "string" ? e.payload.status.toLowerCase() : "";
219
+ if (t === "completed") return "completed";
220
+ if (ue.has(t)) return "failed";
221
+ }
222
+ function de(e) {
223
+ let t = le[e.kind];
224
+ return t === void 0 || t === e.payloadSchemaRef;
225
+ }
226
+ function fe(e) {
227
+ if (typeof e != "string" || e.length === 0) return null;
228
+ try {
229
+ let t = new URL(e);
230
+ return t.protocol !== "http:" && t.protocol !== "https:" || t.username || t.password ? null : t.toString();
231
+ } catch {
232
+ return null;
233
+ }
234
+ }
235
+ function pe(e) {
236
+ return {
237
+ id: e.itemId,
238
+ parentId: e.parentItemId || null,
239
+ runId: e.runId,
240
+ kind: e.kind,
241
+ text: typeof e.payload.text == "string" ? e.payload.text : "",
242
+ lifecycle: e.lifecycle
243
+ };
244
+ }
245
+ function me(e) {
246
+ return {
247
+ id: e.itemId,
248
+ name: typeof e.payload.name == "string" && e.payload.name ? e.payload.name : "Artifact",
249
+ mimeType: typeof e.payload.mimeType == "string" && e.payload.mimeType ? e.payload.mimeType : "application/octet-stream",
250
+ uri: fe(e.payload.uri)
251
+ };
252
+ }
253
+ function C(e, t) {
254
+ let n = e.payload[t];
255
+ return typeof n == "string" && n ? n : null;
256
+ }
257
+ function he(e) {
258
+ if (e.kind === "tool_call") {
259
+ let t = C(e, "callId");
260
+ if (t) return `tool:${t}`;
261
+ }
262
+ if (e.kind === "approval") {
263
+ let t = C(e, "interactionId");
264
+ if (t) return `approval:${t}`;
265
+ }
266
+ if (e.kind === "a2ui") {
267
+ let t = C(e, "surfaceId");
268
+ if (t) return `a2ui:${t}`;
269
+ }
270
+ return `item:${e.itemId}`;
271
+ }
272
+ function ge(e, t) {
273
+ let n = x(e) && !x(t);
274
+ return {
275
+ ...e,
276
+ ...t,
277
+ itemId: e.itemId,
278
+ parentItemId: e.parentItemId,
279
+ sourceEventIds: [...new Set([...e.sourceEventIds, ...t.sourceEventIds])],
280
+ payload: {
281
+ ...e.payload,
282
+ ...t.payload
283
+ },
284
+ nativeRef: {
285
+ ...e.nativeRef,
286
+ ...t.nativeRef
287
+ },
288
+ ...n ? {
289
+ lifecycle: e.lifecycle,
290
+ operation: e.operation
291
+ } : {}
292
+ };
293
+ }
294
+ function _e(e) {
295
+ let t = [], n = /* @__PURE__ */ new Map();
296
+ for (let r of e) {
297
+ let e = he(r), i = n.get(e);
298
+ if (i === void 0) {
299
+ n.set(e, t.length), t.push({
300
+ key: e,
301
+ item: r,
302
+ sourceItemIds: [r.itemId]
303
+ });
304
+ continue;
305
+ }
306
+ let a = t[i];
307
+ t[i] = {
308
+ key: e,
309
+ item: ge(a.item, r),
310
+ sourceItemIds: [...new Set([...a.sourceItemIds, r.itemId])]
311
+ };
312
+ }
313
+ return t;
314
+ }
315
+ function ve(e, t = {}) {
316
+ let n = e.items.filter((e) => e.visibility === "public" || t.includeInternal === !0 && e.visibility === "internal"), r = n.filter(de), i = n.filter((e) => !de(e)), a = new Set([
317
+ "user_message",
318
+ "assistant_text",
319
+ "reasoning"
320
+ ]), o = r.filter((e) => a.has(e.kind)).map(pe), s = (e) => r.filter((t) => t.kind === e).map((e) => typeof e.payload.text == "string" ? e.payload.text : "").join(""), c = [...r.filter((e) => e.kind === "unknown"), ...i], l = r.filter((e) => e.kind === "error"), u = [...r].reverse().find((e) => S(e) !== void 0);
321
+ return {
322
+ timeline: _e(r.filter((e) => e.kind !== "progress")),
323
+ output: s("assistant_text"),
324
+ reasoning: s("reasoning"),
325
+ textItems: o,
326
+ toolItems: r.filter((e) => e.kind === "tool_call"),
327
+ approvalItems: r.filter((e) => e.kind === "approval"),
328
+ structuredInputItems: r.filter((e) => e.kind === "progress" && e.payloadSchemaRef === "conversation.item.structured-input/v1"),
329
+ a2uiItems: r.filter((e) => e.kind === "a2ui"),
330
+ artifacts: r.filter((e) => e.kind === "artifact").map(me),
331
+ fallbacks: [...c.map((e) => ({
332
+ id: e.itemId,
333
+ title: "Unsupported content",
334
+ detail: String(e.payload.summary || e.payload.originalKind || e.payloadSchemaRef),
335
+ failed: e.lifecycle === "failed"
336
+ })), ...l.map((e) => ({
337
+ id: e.itemId,
338
+ title: "Run failed",
339
+ detail: String(e.payload.error || "The agent run failed."),
340
+ failed: !0
341
+ }))],
342
+ runId: n.at(-1)?.runId || "",
343
+ terminalStatus: u ? S(u) : void 0
344
+ };
345
+ }
346
+ //#endregion
347
+ //#region src/core/conversation/reducer.ts
348
+ function ye() {
349
+ return {
350
+ items: [],
351
+ appliedSources: []
352
+ };
353
+ }
354
+ function w(e) {
355
+ return e.lifecycle === "completed" || e.lifecycle === "failed";
356
+ }
357
+ function be(e, t) {
358
+ let n = {
359
+ ...e,
360
+ ...t
361
+ };
362
+ typeof e.text == "string" && typeof t.text == "string" && (n.text = e.text + t.text);
363
+ for (let r of ["data", "operations"]) Array.isArray(e[r]) && Array.isArray(t[r]) && (n[r] = [...e[r], ...t[r]]);
364
+ return n;
365
+ }
366
+ function T(e, t) {
367
+ let n = t.sourceEventIds.map((e) => JSON.stringify([t.itemId, e])), r = new Set(e.appliedSources);
368
+ if (n.every((e) => r.has(e))) return e;
369
+ n.forEach((e) => r.add(e));
370
+ let i = e.items.findIndex((e) => e.itemId === t.itemId), a = i >= 0 ? e.items[i] : void 0;
371
+ if (a && w(a) && !w(t)) return {
372
+ ...e,
373
+ appliedSources: [...r]
374
+ };
375
+ let o = [...e.items];
376
+ if (!a) o.push(t);
377
+ else {
378
+ let e = [...new Set([...a.sourceEventIds, ...t.sourceEventIds])];
379
+ a.kind === t.kind ? t.operation === "append" ? o[i] = {
380
+ ...t,
381
+ payload: be(a.payload, t.payload),
382
+ sourceEventIds: e
383
+ } : o[i] = {
384
+ ...t,
385
+ sourceEventIds: e
386
+ } : o[i] = {
387
+ ...t,
388
+ kind: "unknown",
389
+ operation: "replace",
390
+ payloadSchemaRef: "conversation.item.unknown/v1",
391
+ payload: { summary: "The content type changed for the same item and was safely degraded." },
392
+ sourceEventIds: e
393
+ };
394
+ }
395
+ return {
396
+ items: o,
397
+ appliedSources: [...r]
398
+ };
399
+ }
400
+ var xe = class {
401
+ state = ye();
402
+ apply(e) {
403
+ let t = T(this.state, e);
404
+ if (t === this.state) return !1;
405
+ let n = t.items !== this.state.items;
406
+ return this.state = t, n;
407
+ }
408
+ applyAll(e) {
409
+ for (let t of e) this.apply(t);
410
+ }
411
+ snapshot() {
412
+ return {
413
+ items: this.state.items.map((e) => ({
414
+ ...e,
415
+ sourceEventIds: [...e.sourceEventIds],
416
+ payload: { ...e.payload },
417
+ nativeRef: { ...e.nativeRef }
418
+ })),
419
+ appliedSources: [...this.state.appliedSources]
420
+ };
421
+ }
422
+ }, E = 8;
423
+ function Se(e) {
424
+ return typeof e == "object" && e && !Array.isArray(e) ? e : null;
425
+ }
426
+ function D(e) {
427
+ if (e?.aborted) throw new t("conversation_aborted", "Conversation request was aborted.");
428
+ }
429
+ function Ce(e, t) {
430
+ return t?.aborted === !0 || e instanceof Error && e.name === "AbortError";
431
+ }
432
+ function we(e) {
433
+ let n = e.trim().replace(/\/+$/, "");
434
+ if (!n) return "";
435
+ if (!n.includes("://")) return n.startsWith("/") ? n : `/${n}`;
436
+ try {
437
+ let e = new URL(n);
438
+ if (e.protocol !== "http:" && e.protocol !== "https:" || e.username || e.password) throw Error("unsafe URL");
439
+ return e.toString().replace(/\/$/, "");
440
+ } catch (e) {
441
+ throw new t("conversation_contract_mismatch", "Conversation base URL must be an HTTP(S) URL without embedded credentials.", { cause: e });
442
+ }
443
+ }
444
+ function Te(e) {
445
+ return Math.min(200 * 2 ** Math.max(0, e - 1), 2e3);
446
+ }
447
+ function Ee(e) {
448
+ return new Promise((t) => {
449
+ setTimeout(t, e);
450
+ });
451
+ }
452
+ async function De(e, n, r) {
453
+ if (D(r), !r) {
454
+ await e(n);
455
+ return;
456
+ }
457
+ await new Promise((i, a) => {
458
+ let o = () => a(new t("conversation_aborted", "Conversation request was aborted."));
459
+ r.addEventListener("abort", o, { once: !0 }), e(n).then(i, a).finally(() => {
460
+ r.removeEventListener("abort", o);
461
+ });
462
+ });
463
+ }
464
+ function O(e, t, n) {
465
+ let r = e.snapshot();
466
+ return {
467
+ cursor: t,
468
+ runId: n,
469
+ state: r,
470
+ presentation: ve(r)
471
+ };
472
+ }
473
+ function Oe(e, n) {
474
+ let r, i = [];
475
+ for (let n of e.split(/\r?\n/)) {
476
+ let e = n.endsWith("\r") ? n.slice(0, -1) : n;
477
+ if (!e || e.startsWith(":")) continue;
478
+ let a = e.indexOf(":"), o = a < 0 ? e : e.slice(0, a), s = a < 0 ? "" : e.slice(a + 1), c = s.startsWith(" ") ? s.slice(1) : s;
479
+ if (o === "id") {
480
+ if (!/^\d+$/.test(c) || !Number.isSafeInteger(Number(c))) throw new t("conversation_stream_error", "Conversation stream contains an invalid replay cursor.");
481
+ r = Number(c);
482
+ } else o === "data" && i.push(c);
483
+ }
484
+ if (i.length === 0) return;
485
+ let a = i.join("\n");
486
+ if (!a || a === "[DONE]") return;
487
+ let o;
488
+ try {
489
+ o = JSON.parse(a);
490
+ } catch (e) {
491
+ throw new t("conversation_contract_mismatch", "Conversation stream contains invalid JSON.", { cause: e });
492
+ }
493
+ let s = Se(o);
494
+ if (!s || s.conversationItem === void 0) {
495
+ r !== void 0 && (n.cursor = Math.max(n.cursor, r));
496
+ return;
497
+ }
498
+ if (r === void 0) throw new t("conversation_stream_error", "Canonical conversation items require an SSE replay cursor.");
499
+ let c = se(s.conversationItem);
500
+ if (!c) throw new t("conversation_contract_mismatch", "Conversation stream item does not match conversation.ksadk.io/v1.");
501
+ if (n.runId && n.runId !== c.runId) throw new t("conversation_contract_mismatch", "Conversation stream changed canonical run identity.", {
502
+ runId: n.runId,
503
+ cursor: n.cursor
504
+ });
505
+ n.runId = c.runId;
506
+ let l = n.reducer.apply(c);
507
+ n.cursor = Math.max(n.cursor, r), l && n.options.onItem?.(c), n.options.onUpdate?.(O(n.reducer, n.cursor, n.runId));
508
+ }
509
+ async function ke(e, n) {
510
+ if (!e.body) throw new t("conversation_stream_error", "Conversation response has no event stream body.");
511
+ let r = e.body.getReader(), i = new TextDecoder(), a = "";
512
+ try {
513
+ for (;;) {
514
+ D(n.options.signal);
515
+ let { value: e, done: t } = await r.read();
516
+ if (t) break;
517
+ a += i.decode(e, { stream: !0 });
518
+ let o = /\r?\n\r?\n/.exec(a);
519
+ for (; o?.index !== void 0;) {
520
+ let e = o.index, t = o[0].length;
521
+ Oe(a.slice(0, e), n), a = a.slice(e + t), o = /\r?\n\r?\n/.exec(a);
522
+ }
523
+ }
524
+ a += i.decode(), a.trim() && Oe(a, n);
525
+ } catch (e) {
526
+ throw e instanceof t ? e : (Ce(e, n.options.signal) && D(n.options.signal), new t("conversation_stream_error", "Conversation event stream was interrupted.", {
527
+ cause: e,
528
+ runId: n.runId || void 0,
529
+ cursor: n.cursor
530
+ }));
531
+ } finally {
532
+ r.releaseLock();
533
+ }
534
+ }
535
+ function Ae(e) {
536
+ return e.state.items.some((e) => S(e) !== void 0);
537
+ }
538
+ var je = class {
539
+ fetcher;
540
+ baseUrl;
541
+ maxReconnects;
542
+ sleep;
543
+ retryDelayMs;
544
+ constructor(e = {}) {
545
+ if (!Number.isInteger(e.maxReconnects ?? E) || (e.maxReconnects ?? E) < 0 || (e.maxReconnects ?? E) > 32) throw new t("conversation_contract_mismatch", "Conversation reconnect limit must be an integer from 0 to 32.");
546
+ this.fetcher = e.fetch, this.baseUrl = we(e.baseUrl || ""), this.maxReconnects = e.maxReconnects ?? E, this.sleep = e.sleep || Ee, this.retryDelayMs = e.retryDelayMs || Te;
547
+ }
548
+ fetch() {
549
+ if (this.fetcher) return this.fetcher;
550
+ if (typeof globalThis.fetch == "function") return globalThis.fetch.bind(globalThis);
551
+ throw new t("conversation_http_error", "No fetch implementation is available for the conversation client.");
552
+ }
553
+ url(e) {
554
+ return `${this.baseUrl}${e}`;
555
+ }
556
+ async request(e, n, r, i = "conversation_stream_error") {
557
+ D(r);
558
+ try {
559
+ return await this.fetch()(e, n);
560
+ } catch (e) {
561
+ throw e instanceof t ? e : (Ce(e, r) && D(r), new t(i, "Conversation network request failed.", { cause: e }));
562
+ }
563
+ }
564
+ assertOk(e) {
565
+ if (!e.ok) throw new t("conversation_http_error", `Conversation endpoint returned HTTP ${e.status}.`, { status: e.status });
566
+ }
567
+ async getSurface(e, n, r = {}) {
568
+ if (!e || !n) throw new t("conversation_contract_mismatch", "Agent and session identities are required to get a conversation surface.");
569
+ let i = r.signal ? { signal: r.signal } : void 0, a = await this.request(this.url(`/api/v1/agents/${encodeURIComponent(e)}/conversation-surface?sessionId=${encodeURIComponent(n)}`), i, r.signal, "conversation_http_error");
570
+ this.assertOk(a);
571
+ let o;
572
+ try {
573
+ o = await a.json();
574
+ } catch (e) {
575
+ throw new t("conversation_contract_mismatch", "Conversation surface response is not valid JSON.", { cause: e });
576
+ }
577
+ let s = Se(o), c = oe(s?.surface);
578
+ if (!s || typeof s.buildId != "string" || !s.buildId || !c) throw new t("conversation_contract_mismatch", "Conversation surface response does not match conversation.ksadk.io/v1.");
579
+ return {
580
+ buildId: s.buildId,
581
+ surface: c
582
+ };
583
+ }
584
+ async streamTurn(e) {
585
+ D(e.signal), re(e.bootstrap.surface, e.input);
586
+ let n = {
587
+ cursor: 0,
588
+ runId: "",
589
+ reducer: new xe(),
590
+ options: e
591
+ }, r = {
592
+ method: "POST",
593
+ headers: {
594
+ "Content-Type": "application/json",
595
+ "Idempotency-Key": e.input.idempotencyKey
596
+ },
597
+ body: JSON.stringify({ input: e.input }),
598
+ ...e.signal ? { signal: e.signal } : {}
599
+ }, i = await this.request(this.url(`/api/v1/builds/${encodeURIComponent(e.bootstrap.buildId)}/conversation:stream`), r, e.signal);
600
+ this.assertOk(i);
601
+ try {
602
+ await ke(i, n);
603
+ } catch (e) {
604
+ if (!(e instanceof t) || e.code !== "conversation_stream_error" || !n.runId) throw e;
605
+ }
606
+ let a = O(n.reducer, n.cursor, n.runId);
607
+ if (Ae(a)) return a;
608
+ if (!n.runId) throw new t("conversation_run_identity_missing", "The stream ended before a canonical item supplied its run identity.", { cursor: n.cursor });
609
+ for (let r = 1; r <= this.maxReconnects; r += 1) {
610
+ await De(this.sleep, this.retryDelayMs(r), e.signal), D(e.signal);
611
+ let i;
612
+ try {
613
+ i = await this.request(this.url(`/api/v1/runs/${encodeURIComponent(n.runId)}/events?after=${n.cursor}`), {
614
+ method: "GET",
615
+ headers: { "Last-Event-ID": String(n.cursor) },
616
+ ...e.signal ? { signal: e.signal } : {}
617
+ }, e.signal), this.assertOk(i), await ke(i, n);
618
+ } catch (e) {
619
+ if (e instanceof t && e.code === "conversation_aborted" || e instanceof t && e.code === "conversation_contract_mismatch" || e instanceof t && e.code === "conversation_http_error" && e.status !== void 0 && e.status < 500) throw e;
620
+ continue;
621
+ }
622
+ if (a = O(n.reducer, n.cursor, n.runId), Ae(a)) return a;
623
+ }
624
+ throw new t("conversation_reconnect_exhausted", "Conversation replay stopped after the configured reconnect limit.", {
625
+ runId: n.runId,
626
+ cursor: n.cursor
627
+ });
628
+ }
629
+ }, Me = 0;
630
+ function k(e) {
631
+ return Me += 1, `${e}-${Me}`;
632
+ }
633
+ function A(e) {
634
+ return e.map((e) => e.type === "thinking" && e.status === "streaming" ? {
635
+ ...e,
636
+ status: "done"
637
+ } : e);
638
+ }
639
+ function Ne(e, t) {
640
+ let n = [...e ?? []], r = n[n.length - 1];
641
+ return r?.type === "thinking" && r.status === "streaming" ? (n[n.length - 1] = {
642
+ ...r,
643
+ content: r.content + t
644
+ }, n) : [...n, {
645
+ id: k("thinking"),
646
+ type: "thinking",
647
+ content: t,
648
+ status: "streaming"
649
+ }];
650
+ }
651
+ function Pe(e, t) {
652
+ let n = [...A(e ?? [])], r = n[n.length - 1];
653
+ return r?.type === "text" && r.status === "streaming" ? (n[n.length - 1] = {
654
+ ...r,
655
+ content: r.content + t
656
+ }, n) : [...n, {
657
+ id: k("text"),
658
+ type: "text",
659
+ content: t,
660
+ status: "streaming"
661
+ }];
662
+ }
663
+ function Fe(e, t) {
664
+ let n = A(e ?? []);
665
+ if (n.filter((e) => e.type === "text").map((e) => e.content).join("") === t) return n.map((e) => e.type === "text" && e.status === "streaming" ? {
666
+ ...e,
667
+ status: "done"
668
+ } : e);
669
+ let r = [...n], i = r.map((e) => e.type).lastIndexOf("text");
670
+ return i >= 0 && r[i].type === "text" ? (r[i] = {
671
+ ...r[i],
672
+ content: t,
673
+ status: "done"
674
+ }, r) : [...r, {
675
+ id: k("text"),
676
+ type: "text",
677
+ content: t,
678
+ status: "done"
679
+ }];
680
+ }
681
+ function Ie(e, t) {
682
+ if (!t) return [...e ?? []];
683
+ let n = [...e ?? []], r = n[n.length - 1];
684
+ if (r?.type !== "thinking" || !r.content.endsWith(t)) return n;
685
+ let i = r.content.slice(0, -t.length);
686
+ return i ? (n[n.length - 1] = {
687
+ ...r,
688
+ content: i
689
+ }, n) : (n.pop(), n);
690
+ }
691
+ function Le(e, t, n) {
692
+ let r = [...A(e ?? [])], i = r.findIndex((e) => e.type === "tool" && e.toolName === t);
693
+ if (i >= 0 && r[i].type === "tool") {
694
+ 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);
695
+ if (t && !a && !o) return r;
696
+ let s = n.extra ?? e.extra;
697
+ return r[i] = {
698
+ ...e,
699
+ ...n,
700
+ extra: s
701
+ }, r;
702
+ }
703
+ return [...r, {
704
+ id: k("tool"),
705
+ type: "tool",
706
+ toolName: t,
707
+ args: n.args ?? "",
708
+ output: n.output,
709
+ status: n.status ?? "running",
710
+ extra: n.extra
711
+ }];
712
+ }
713
+ function Re(e) {
714
+ return A(e ?? []);
715
+ }
716
+ function ze(e) {
717
+ let t = [];
718
+ if (e.reasoning && e.reasoning.trim() && t.push({
719
+ id: k("thinking"),
720
+ type: "thinking",
721
+ content: e.reasoning,
722
+ status: "done"
723
+ }), e.tools) for (let n of Object.values(e.tools)) t.push({
724
+ id: k("tool"),
725
+ type: "tool",
726
+ toolName: n.name,
727
+ args: n.args ?? "",
728
+ output: n.output,
729
+ status: n.status ?? "completed"
730
+ });
731
+ return e.content && e.content.trim() && t.push({
732
+ id: k("text"),
733
+ type: "text",
734
+ content: e.content,
735
+ status: "done"
736
+ }), t;
737
+ }
738
+ //#endregion
739
+ //#region src/core/stream/kernel-events.ts
740
+ var Be = {
741
+ "run.started": "in_progress",
742
+ "run.progress": "in_progress",
743
+ "run.completed": "completed",
744
+ "run.failed": "failed",
745
+ "run.canceled": "cancelled",
746
+ "run.interrupted": "interrupted"
747
+ }, Ve = new Set([
748
+ "completed",
749
+ "failed",
750
+ "cancelled",
751
+ "canceled"
752
+ ]);
753
+ function j(e) {
754
+ return typeof e == "object" && e && !Array.isArray(e) ? e : null;
755
+ }
756
+ function He(e) {
757
+ let t = j(e.source), n = t ? j(t.metadata) : null;
758
+ return String(n?.native_item_kind || "");
759
+ }
760
+ function M(e, t) {
761
+ let n = j(e[t]);
762
+ if (!n) return [];
763
+ let r = n?.parts;
764
+ return Array.isArray(r) ? r : t === "update" ? [n] : [];
765
+ }
766
+ function Ue(e) {
767
+ let t = j(e);
768
+ if (!t) return "";
769
+ if (typeof t.text == "string") return t.text;
770
+ let n = j(t.data);
771
+ if (n) {
772
+ let e = n.content;
773
+ if (Array.isArray(e)) return e.map((e) => String(j(e)?.text || "")).join("");
774
+ for (let e of [
775
+ "text",
776
+ "output",
777
+ "stdout",
778
+ "stderr"
779
+ ]) if (n[e] != null) return String(n[e]);
780
+ }
781
+ return "";
782
+ }
783
+ function N(e) {
784
+ return e.map(Ue).join("");
785
+ }
786
+ function P(e) {
787
+ return e.some((e) => j(j(e)?.data)?.type === "userMessage");
788
+ }
789
+ function F(e) {
790
+ if (e.length === 1) {
791
+ let t = j(j(e[0])?.data);
792
+ if (t) return t;
793
+ }
794
+ return N(e) || null;
795
+ }
796
+ function I(e, t, n) {
797
+ return { RuntimeItem: {
798
+ RunId: String(e.run_id || ""),
799
+ ScopeId: String(e.scope_id || e.run_id || ""),
800
+ ItemId: String(e.item_id || ""),
801
+ ...n ? { PartId: n } : {},
802
+ Operation: t,
803
+ SourceEventId: String(e.event_id || "")
804
+ } };
805
+ }
806
+ var L = class {
807
+ textByPart = /* @__PURE__ */ new Map();
808
+ sessionId;
809
+ constructor(e) {
810
+ this.sessionId = e;
811
+ }
812
+ translate(e) {
813
+ if (!j(e)) return null;
814
+ let t = Number(e.seq);
815
+ if (!Number.isFinite(t) || t <= 0) return null;
816
+ let n = String(e.family || ""), r = String(e.event_type || ""), i = String(e.run_id || ""), a = () => ({
817
+ EventId: e.event_id ? String(e.event_id) : `kernel-${t}`,
818
+ SessionId: this.sessionId,
819
+ InvocationId: i || void 0,
820
+ SeqId: t
821
+ });
822
+ if (n === "interaction") return {
823
+ ...a(),
824
+ EventType: r,
825
+ payload: e
826
+ };
827
+ if (n === "control") {
828
+ if (r === "control.run_transition") {
829
+ let t = String(e.state || "").toLowerCase();
830
+ if (Ve.has(t)) return {
831
+ ...a(),
832
+ EventType: "run_status",
833
+ Content: { status: t }
834
+ };
835
+ }
836
+ return null;
837
+ }
838
+ if (n !== "runtime") return null;
839
+ let o = Be[r];
840
+ if (o) return {
841
+ ...a(),
842
+ EventType: "run_status",
843
+ Content: { status: o }
844
+ };
845
+ let s = He(e);
846
+ if (r === "item.started") {
847
+ let t = M(e, "initial");
848
+ return s === "userMessage" || P(t) || s === "reasoning" || e.item_kind === "reasoning" ? null : s === "agentMessage" || !s && e.item_kind === "message" ? {
849
+ ...a(),
850
+ EventType: "assistant_stream_snapshot",
851
+ Content: { parts: [{ text: "" }] },
852
+ Metadata: I(e, "replace")
853
+ } : s && s !== "notification" ? {
854
+ ...a(),
855
+ EventType: "tool_call",
856
+ Metadata: {
857
+ ...I(e, "replace"),
858
+ call_id: String(e.item_id || ""),
859
+ tool_name: s,
860
+ run_id: i,
861
+ tool_args: F(t)
862
+ }
863
+ } : null;
864
+ }
865
+ if (r === "item.updated") {
866
+ let n = M(e, "update"), r = j(n[0]), i = String(r?.part_id || e.item_id || t), o = String(e.op || "replace"), c = N(n), l = this.textByPart.get(i) || "", u = o === "append" ? l + c : c;
867
+ return this.textByPart.set(i, u), s === "agentMessage" || !s && e.item_kind === "message" ? {
868
+ ...a(),
869
+ EventType: "assistant_stream_snapshot",
870
+ Content: { parts: [{ text: u }] },
871
+ Metadata: I(e, "replace", i)
872
+ } : s === "reasoning" || e.item_kind === "reasoning" ? {
873
+ ...a(),
874
+ EventType: "reasoning",
875
+ Content: { parts: [{ text: c }] },
876
+ Metadata: I(e, o === "append" ? "append" : "replace", i)
877
+ } : null;
878
+ }
879
+ if (r === "item.snapshot_replaced") {
880
+ let n = M(e, "snapshot"), r = String(j(n[0])?.part_id || e.item_id || t), i = N(n);
881
+ return this.textByPart.set(r, i), s === "agentMessage" || !s && e.item_kind === "message" ? {
882
+ ...a(),
883
+ EventType: "assistant_stream_snapshot",
884
+ Content: { parts: [{ text: i }] },
885
+ Metadata: I(e, "replace", r)
886
+ } : s === "reasoning" || e.item_kind === "reasoning" ? {
887
+ ...a(),
888
+ EventType: "reasoning",
889
+ Content: { parts: [{ text: i }] },
890
+ Metadata: I(e, "replace", r)
891
+ } : null;
892
+ }
893
+ if (r === "item.completed") {
894
+ let n = M(e, "snapshot");
895
+ if (s === "userMessage" || P(n)) return {
896
+ ...a(),
897
+ EventType: "user_message",
898
+ Content: { parts: [{ text: N(n) }] }
899
+ };
900
+ if (s === "agentMessage" || !s && e.item_kind === "message") {
901
+ let r = N(n), i = String(j(n[0])?.part_id || e.item_id || t);
902
+ return this.textByPart.set(i, r), {
903
+ ...a(),
904
+ EventType: "assistant_message",
905
+ Content: { parts: [{ text: r }] },
906
+ Metadata: I(e, "completed", i)
907
+ };
908
+ }
909
+ if (s === "reasoning" || e.item_kind === "reasoning") {
910
+ let r = String(j(n[0])?.part_id || e.item_id || t);
911
+ return {
912
+ ...a(),
913
+ EventType: "reasoning",
914
+ Content: { parts: [{ text: N(n) }] },
915
+ Metadata: I(e, "completed", r)
916
+ };
917
+ }
918
+ return s && s !== "notification" ? {
919
+ ...a(),
920
+ EventType: "tool_result",
921
+ Metadata: {
922
+ ...I(e, "completed"),
923
+ call_id: String(e.item_id || ""),
924
+ tool_name: s,
925
+ run_id: i,
926
+ tool_output: F(n)
927
+ }
928
+ } : null;
929
+ }
930
+ return r === "item.failed" && s && s !== "notification" && s !== "agentMessage" && s !== "reasoning" ? {
931
+ ...a(),
932
+ EventType: "tool_result",
933
+ Metadata: {
934
+ ...I(e, "completed"),
935
+ call_id: String(e.item_id || ""),
936
+ tool_name: s,
937
+ run_id: i,
938
+ tool_output: { error: j(e.error) || e.error || "item failed" }
939
+ }
940
+ } : null;
941
+ }
942
+ };
943
+ async function We(e) {
944
+ let t = e.getReader(), n = new TextDecoder(), r = "", i = null, a = !1, o = (e) => e.includes("data:") || e.includes("event:") || e.includes(":");
945
+ try {
946
+ for (;;) {
947
+ let { value: e, done: s } = await t.read();
948
+ if (e && (r += n.decode(e, { stream: !0 })), s) {
949
+ a = !0;
950
+ break;
951
+ }
952
+ if (r.trimStart().startsWith("{")) {
953
+ if (Ge(r)) {
954
+ i = Ke(r);
955
+ break;
956
+ }
957
+ } else if (o(r)) break;
958
+ }
959
+ } catch {}
960
+ let s = new TextEncoder().encode(r), c = new ReadableStream({
961
+ start(e) {
962
+ if (s.length > 0 && e.enqueue(s), a || i) {
963
+ e.close();
964
+ return;
965
+ }
966
+ let n = () => t.read().then(({ value: t, done: r }) => {
967
+ if (t && e.enqueue(t), r) {
968
+ e.close();
969
+ return;
970
+ }
971
+ return n();
972
+ }).catch(() => {
973
+ try {
974
+ e.close();
975
+ } catch {}
976
+ });
977
+ n();
978
+ },
979
+ cancel() {
980
+ t.cancel();
981
+ }
982
+ });
983
+ return {
984
+ receipt: i,
985
+ stream: c
986
+ };
987
+ }
988
+ function Ge(e) {
989
+ let t = e.trim();
990
+ if (!t.endsWith("}")) return !1;
991
+ let n = 0, r = !1, i = !1;
992
+ for (let e of t) {
993
+ if (i) {
994
+ i = !1;
995
+ continue;
996
+ }
997
+ if (e === "\\") {
998
+ i = !0;
999
+ continue;
1000
+ }
1001
+ e === "\"" && (r = !r), !r && ((e === "{" || e === "[") && (n += 1), (e === "}" || e === "]") && --n);
1002
+ }
1003
+ return n === 0;
1004
+ }
1005
+ function Ke(e) {
1006
+ try {
1007
+ let t = j(JSON.parse(e).Data), n = String(t?.ReceiptStatus || "");
1008
+ if (!n) return null;
1009
+ let r = j(t?.Error);
1010
+ return {
1011
+ status: n,
1012
+ messageId: t?.MessageId ?? null,
1013
+ runId: t?.RunId ?? null,
1014
+ acceptedSeq: t?.AcceptedSeq ?? null,
1015
+ error: r ? {
1016
+ code: String(r.code || ""),
1017
+ message: String(r.message || "")
1018
+ } : null
1019
+ };
1020
+ } catch {
1021
+ return null;
1022
+ }
1023
+ }
1024
+ //#endregion
1025
+ //#region src/utils/responses-stream.js
1026
+ function R() {
1027
+ return {
1028
+ toolNames: /* @__PURE__ */ new Map(),
1029
+ toolArguments: /* @__PURE__ */ new Map(),
1030
+ callIds: /* @__PURE__ */ new Map(),
1031
+ currentResponseId: ""
1032
+ };
1033
+ }
1034
+ function qe(e) {
1035
+ 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 : "";
1036
+ }
1037
+ function z(e) {
1038
+ return e == null ? "" : typeof e == "string" ? e : typeof e == "object" ? JSON.stringify(e, null, 2) : String(e);
1039
+ }
1040
+ function B(...e) {
1041
+ for (let t of e) {
1042
+ let e = qe(t);
1043
+ if (e) return e;
1044
+ }
1045
+ return "";
1046
+ }
1047
+ function Je(e, t) {
1048
+ return String(e?.id || e?.item_id || e?.call_id || t?.item_id || t?.call_id || t?.output_index || "");
1049
+ }
1050
+ function Ye(e, t, n, r, i) {
1051
+ t && (e.toolNames.set(t, r), e.toolArguments.set(t, i));
1052
+ let a = String(n?.call_id || "");
1053
+ a && (e.callIds.set(a, t || r), e.toolNames.set(a, r), e.toolArguments.set(a, i));
1054
+ }
1055
+ function V(e, t, n, r = "tool") {
1056
+ let i = String(n?.call_id || "");
1057
+ return String(n?.name || n?.tool_name || (t ? e.toolNames.get(t) : "") || (i ? e.toolNames.get(i) : "") || r);
1058
+ }
1059
+ function H(e) {
1060
+ if (!e || typeof e != "object") return "";
1061
+ let t = qe(e.content);
1062
+ return B(e.output_text, e.text, e.summary_text, e.summary, e.delta, t);
1063
+ }
1064
+ function Xe(e) {
1065
+ let t = e?.response && typeof e.response == "object" ? e.response : e, n = B(t?.output_text, t?.text);
1066
+ if (n) return n;
1067
+ let r = Array.isArray(t?.output) ? t.output : [];
1068
+ for (let e of r) {
1069
+ let t = H(e);
1070
+ if (t) return t;
1071
+ }
1072
+ return "";
1073
+ }
1074
+ function Ze(e) {
1075
+ let t = e?.response && typeof e.response == "object" ? e.response : e;
1076
+ return Array.isArray(t?.output) ? t.output : [];
1077
+ }
1078
+ function Qe(e) {
1079
+ let t = e;
1080
+ if (typeof t == "string") try {
1081
+ t = JSON.parse(t);
1082
+ } catch {
1083
+ return {};
1084
+ }
1085
+ return Array.isArray(t) && (t = t[0] || {}), !t || typeof t != "object" ? {} : t.value && typeof t.value == "object" && !Array.isArray(t.value) ? {
1086
+ ...t.value,
1087
+ approval_request_id: t.value.approval_request_id || t.value.id || t.id
1088
+ } : t;
1089
+ }
1090
+ function U({ data: e, state: t, status: n }) {
1091
+ let r = e?.item || e?.output_item || e || {}, i = String(r.type || "").trim(), a = Je(r, e);
1092
+ if (i === "function_call") {
1093
+ let e = V(t, a, r), i = z(r.arguments ?? r.args ?? r.input);
1094
+ return Ye(t, a, r, e, i), [{
1095
+ type: "tool_upsert",
1096
+ name: e,
1097
+ args: i,
1098
+ status: n
1099
+ }];
1100
+ }
1101
+ if (i === "function_call_output") return [{
1102
+ type: "tool_result",
1103
+ name: V(t, a, r),
1104
+ output: z(r.output ?? r.result ?? r.content)
1105
+ }];
1106
+ if (i === "mcp_approval_request") {
1107
+ let n = String(r.name || "approval"), i = z(r.arguments ?? r.args), a = String(r.id || r.approval_request_id || "");
1108
+ return [{
1109
+ type: "tool_upsert",
1110
+ name: n,
1111
+ args: i,
1112
+ status: "paused",
1113
+ approvalRequestId: a,
1114
+ previousResponseId: String(e?.response_id || t.currentResponseId || ""),
1115
+ serverLabel: String(r.server_label || "")
1116
+ }, {
1117
+ type: "approval_request",
1118
+ approvalRequestId: a,
1119
+ previousResponseId: String(e?.response_id || t.currentResponseId || ""),
1120
+ name: n,
1121
+ args: i
1122
+ }];
1123
+ }
1124
+ if (i === "reasoning" || i === "reasoning_summary" || i === "reasoning_summary_text") {
1125
+ let e = H(r);
1126
+ return e ? [{
1127
+ type: "reasoning_delta",
1128
+ text: e
1129
+ }] : [];
1130
+ }
1131
+ if (i === "message") {
1132
+ let e = H(r);
1133
+ return e ? [{
1134
+ type: n === "completed" ? "text_final" : "text_delta",
1135
+ text: e
1136
+ }] : [];
1137
+ }
1138
+ return [];
1139
+ }
1140
+ function $e({ eventName: e, data: t, state: n }) {
1141
+ let r = String(t?.type || e || "").trim(), i = String(t?.id || t?.response?.id || t?.response_id || "");
1142
+ if (i.startsWith("resp_") && (n.currentResponseId = i), r === "response.tool_call") return [{
1143
+ type: "tool_upsert",
1144
+ name: String(t?.name || t?.tool_name || "tool"),
1145
+ args: z(t?.args ?? t?.arguments),
1146
+ status: "running"
1147
+ }];
1148
+ if (r === "response.tool_result" || r === "response.ksadk.tool_result") return [{
1149
+ type: "tool_result",
1150
+ name: String(t?.name || t?.tool_name || "tool"),
1151
+ output: z(t?.output ?? t?.result)
1152
+ }];
1153
+ if (r === "response.output_item.added") return U({
1154
+ data: t,
1155
+ state: n,
1156
+ status: "running"
1157
+ });
1158
+ if (r === "response.output_item.done") return U({
1159
+ data: t,
1160
+ state: n,
1161
+ status: "completed"
1162
+ });
1163
+ if (r === "response.function_call_arguments.delta") {
1164
+ let e = String(t?.item_id || t?.call_id || ""), r = V(n, e, t), i = `${n.toolArguments.get(e) || ""}${String(t?.delta || "")}`;
1165
+ return n.toolArguments.set(e, i), [{
1166
+ type: "tool_upsert",
1167
+ name: r,
1168
+ args: i,
1169
+ status: "running"
1170
+ }];
1171
+ }
1172
+ if (r === "response.function_call_arguments.done") {
1173
+ let e = String(t?.item_id || t?.call_id || ""), r = V(n, e, t), i = z(t?.arguments ?? n.toolArguments.get(e));
1174
+ return n.toolArguments.set(e, i), [{
1175
+ type: "tool_upsert",
1176
+ name: r,
1177
+ args: i,
1178
+ status: "running"
1179
+ }];
1180
+ }
1181
+ if (r === "response.reasoning.delta" || r === "response.reasoning_text.delta" || r === "response.reasoning_summary.delta" || r === "response.reasoning_summary_text.delta") {
1182
+ let e = B(t?.delta, t?.text);
1183
+ return e ? [{
1184
+ type: "reasoning_delta",
1185
+ text: e
1186
+ }] : [];
1187
+ }
1188
+ if (r === "response.output_text.delta") {
1189
+ let e = B(t?.delta, t?.text);
1190
+ return e ? [{
1191
+ type: "text_delta",
1192
+ text: e
1193
+ }] : [];
1194
+ }
1195
+ if (r === "response.output_text.done") {
1196
+ let e = B(t?.text, t?.delta);
1197
+ return e ? [{
1198
+ type: "text_final",
1199
+ text: e
1200
+ }] : [];
1201
+ }
1202
+ if (r === "response.content_part.delta") {
1203
+ let e = String(t?.part?.type || t?.delta?.type || t?.content_type || ""), n = B(t?.delta?.text, t?.delta, t?.text);
1204
+ return n ? e.includes("reasoning") ? [{
1205
+ type: "reasoning_delta",
1206
+ text: n
1207
+ }] : [{
1208
+ type: "text_delta",
1209
+ text: n
1210
+ }] : [];
1211
+ }
1212
+ if (r === "response.completed") {
1213
+ let e = Xe(t), r = Ze(t).flatMap((e) => U({
1214
+ data: {
1215
+ ...t,
1216
+ item: e
1217
+ },
1218
+ state: n,
1219
+ status: "completed"
1220
+ })), i = r.some((e) => e.type === "text_final");
1221
+ return [
1222
+ ...r,
1223
+ ...e && !i ? [{
1224
+ type: "text_final",
1225
+ text: e
1226
+ }] : [],
1227
+ {
1228
+ type: "terminal",
1229
+ status: "completed"
1230
+ }
1231
+ ];
1232
+ }
1233
+ if (r === "response.failed") return [{
1234
+ type: "failed",
1235
+ message: t?.error?.message || "Agent 运行失败"
1236
+ }, {
1237
+ type: "terminal",
1238
+ status: "failed"
1239
+ }];
1240
+ if (r === "response.incomplete") return [{ type: "incomplete" }, {
1241
+ type: "terminal",
1242
+ status: "incomplete"
1243
+ }];
1244
+ if (r === "response.cancelled") return [{
1245
+ type: "terminal",
1246
+ status: "cancelled"
1247
+ }];
1248
+ if (r === "response.approval_request" || r === "response.ksadk.approval_request") {
1249
+ let e = Qe(t?.interrupt_info), r = String(e.approval_request_id || e.id || ""), i = String(t?.response_id || n.currentResponseId || ""), a = e.approval_requests && typeof e.approval_requests == "object" ? e.approval_requests : e, o = Array.isArray(a.action_requests) ? a.action_requests : [];
1250
+ if (o.length > 0) {
1251
+ let e = o.map((e) => ({
1252
+ type: "tool_upsert",
1253
+ name: String(e?.name || "tool"),
1254
+ args: JSON.stringify(e?.args ?? {}),
1255
+ status: "paused",
1256
+ approvalRequestId: r,
1257
+ previousResponseId: i
1258
+ })), t = o[0] || {};
1259
+ return [...e, {
1260
+ type: "approval_request",
1261
+ approvalRequestId: r,
1262
+ previousResponseId: i,
1263
+ name: String(t?.name || "人工确认"),
1264
+ args: JSON.stringify(t?.args ?? {})
1265
+ }];
1266
+ }
1267
+ return [{
1268
+ type: "approval_request",
1269
+ approvalRequestId: r,
1270
+ previousResponseId: i
1271
+ }];
1272
+ }
1273
+ if (new Set([
1274
+ "response.ksadk.a2ui_surface_begin",
1275
+ "a2ui.surface.begin",
1276
+ "response.a2ui.createSurface",
1277
+ "a2ui.createSurface",
1278
+ "createSurface"
1279
+ ]).has(r)) {
1280
+ let e = t?.surface && typeof t.surface == "object" ? t.surface : {};
1281
+ return [{
1282
+ type: "a2ui_surface_begin",
1283
+ surfaceId: String(t?.surface_id || t?.surfaceId || e.surface_id || e.surfaceId || ""),
1284
+ surface: e
1285
+ }];
1286
+ }
1287
+ if (new Set([
1288
+ "response.ksadk.a2ui_surface_update",
1289
+ "a2ui.surface.update",
1290
+ "response.a2ui.updateComponents",
1291
+ "a2ui.updateComponents",
1292
+ "updateComponents"
1293
+ ]).has(r)) {
1294
+ let e = t?.surface && typeof t.surface == "object" ? t.surface : {};
1295
+ return [{
1296
+ type: "a2ui_surface_update",
1297
+ surfaceId: String(t?.surface_id || t?.surfaceId || e.surface_id || e.surfaceId || ""),
1298
+ surface: e
1299
+ }];
1300
+ }
1301
+ if (new Set([
1302
+ "response.ksadk.a2ui_surface_end",
1303
+ "a2ui.surface.end",
1304
+ "response.a2ui.deleteSurface",
1305
+ "a2ui.deleteSurface",
1306
+ "deleteSurface"
1307
+ ]).has(r)) return [{
1308
+ type: "a2ui_surface_end",
1309
+ surfaceId: String(t?.surface_id || t?.surfaceId || "")
1310
+ }];
1311
+ if (new Set(["response.ksadk.a2ui_interaction", "a2ui.interaction"]).has(r)) return [{
1312
+ type: "a2ui_interaction",
1313
+ surfaceId: String(t?.surface_id || t?.surfaceId || ""),
1314
+ interactionId: String(t?.interaction_id || t?.interactionId || ""),
1315
+ kind: String(t?.kind || "input"),
1316
+ inputSchema: t?.input_schema && typeof t.input_schema == "object" ? t.input_schema : {}
1317
+ }];
1318
+ let a = t?.content?.parts?.[0]?.text;
1319
+ return a && !t?.actions?.finishReason ? [{
1320
+ type: "text_delta",
1321
+ text: String(a)
1322
+ }] : [];
1323
+ }
1324
+ //#endregion
1325
+ //#region src/utils/session-events.js
1326
+ var et = new Set([
1327
+ "completed",
1328
+ "failed",
1329
+ "error",
1330
+ "cancelled",
1331
+ "canceled",
1332
+ "aborted",
1333
+ "interrupted",
1334
+ "resume_failed"
1335
+ ]), W = new Set([
1336
+ "tool_call",
1337
+ "tool_result",
1338
+ "stage_tool_call",
1339
+ "stage_tool_result"
1340
+ ]), G = {
1341
+ "run.started": "in_progress",
1342
+ "run.progress": "in_progress",
1343
+ "run.interrupted": "interrupted",
1344
+ "run.completed": "completed",
1345
+ "run.failed": "failed",
1346
+ "run.canceled": "cancelled"
1347
+ };
1348
+ function K(e) {
1349
+ let t = e?.Content?.payload;
1350
+ return t && typeof t == "object" ? t : {};
1351
+ }
1352
+ function tt(e, t) {
1353
+ let n = String(e || ""), r = String(t || "");
1354
+ return n ? !r || n.endsWith(r) ? n : r.startsWith(n) ? r : `${n}${r}` : r;
1355
+ }
1356
+ function nt(e) {
1357
+ let t = String(e?.EventType || ""), n = K(e);
1358
+ return G[t] ? {
1359
+ ...e,
1360
+ EventType: "run_status",
1361
+ Content: {
1362
+ status: String(n.status || G[t]),
1363
+ ...n.detail ? { detail: String(n.detail) } : {}
1364
+ }
1365
+ } : t === "reasoning.delta" || t === "reasoning.completed" ? {
1366
+ ...e,
1367
+ EventType: "reasoning",
1368
+ Content: {
1369
+ role: "model",
1370
+ parts: [{ text: String(n.text || "") }]
1371
+ }
1372
+ } : t === "tool.call.begin" ? {
1373
+ ...e,
1374
+ EventType: "tool_call",
1375
+ Metadata: {
1376
+ ...e.Metadata || {},
1377
+ tool_name: n.name,
1378
+ tool_args: n.args,
1379
+ tool_call_id: n.call_id
1380
+ },
1381
+ Content: {
1382
+ role: "model",
1383
+ parts: []
1384
+ }
1385
+ } : t === "tool.call.end" ? {
1386
+ ...e,
1387
+ EventType: "tool_result",
1388
+ Metadata: {
1389
+ ...e.Metadata || {},
1390
+ tool_name: n.name,
1391
+ tool_output: n.error || n.result,
1392
+ tool_call_id: n.call_id
1393
+ },
1394
+ Content: {
1395
+ role: "model",
1396
+ parts: []
1397
+ }
1398
+ } : e;
1399
+ }
1400
+ function rt(e) {
1401
+ let t = [], n = /* @__PURE__ */ new Map();
1402
+ for (let r of Array.isArray(e) ? e : []) {
1403
+ let e = String(r?.EventType || "");
1404
+ if (e === "text.delta" || e === "text.completed") {
1405
+ let e = String(r?.InvocationId || "").trim();
1406
+ if (!e) continue;
1407
+ let t = n.get(e);
1408
+ n.set(e, {
1409
+ event: r,
1410
+ text: tt(t?.text, K(r).text)
1411
+ });
1412
+ continue;
1413
+ }
1414
+ t.push(nt(r));
1415
+ }
1416
+ for (let { event: e, text: r } of n.values()) t.push({
1417
+ ...e,
1418
+ EventType: "assistant_stream_snapshot",
1419
+ Content: {
1420
+ role: "model",
1421
+ parts: [{ text: r }]
1422
+ },
1423
+ Metadata: {
1424
+ ...e.Metadata || {},
1425
+ stream_snapshot: !0
1426
+ }
1427
+ });
1428
+ return t.sort((e, t) => Z(e) - Z(t));
1429
+ }
1430
+ function it(e) {
1431
+ let t = String(e?.EventType || "").trim(), n = K(e);
1432
+ return String(e?.Content?.status || n.status || G[t] || "").trim().toLowerCase();
1433
+ }
1434
+ function q(e) {
1435
+ 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("") : "";
1436
+ }
1437
+ function J(e) {
1438
+ let t = String(e || "").trim();
1439
+ return t ? `/agentengine/api/v1/AttachmentContent?FileUri=${encodeURIComponent(t)}` : "";
1440
+ }
1441
+ function at(e) {
1442
+ return String(e || "").match(/^data:([^;,]+)/)?.[1] || "";
1443
+ }
1444
+ function ot(e, t) {
1445
+ let n = String(e || "").trim(), r = String(t || "").trim();
1446
+ return !n || !r || n === r ? !0 : n.endsWith("/*") ? r.startsWith(n.slice(0, -1)) : r.endsWith("/*") ? n.startsWith(r.slice(0, -1)) : !1;
1447
+ }
1448
+ function Y(e) {
1449
+ let t = e?.Content?.parts || [], n = [], r = /* @__PURE__ */ new Map(), i = (e) => {
1450
+ let t = `${e.fileUri || e.url || e.name}|${e.type}`;
1451
+ r.has(t) || r.set(t, e);
1452
+ };
1453
+ for (let e of t) {
1454
+ if (e?.type === "input_text" || e?.text) {
1455
+ n.push(e.text || "");
1456
+ continue;
1457
+ }
1458
+ if (e?.type === "input_file" && e.inlineData) {
1459
+ i({
1460
+ name: e.inlineData.displayName || "attachment",
1461
+ url: `data:${e.inlineData.mimeType || "application/octet-stream"};base64,${e.inlineData.data}`,
1462
+ type: e.inlineData.mimeType || "application/octet-stream"
1463
+ });
1464
+ continue;
1465
+ }
1466
+ if (e?.type === "input_file" && typeof e.file_data == "string" && e.file_data) {
1467
+ i({
1468
+ name: e.filename || e.displayName || e.display_name || "attachment",
1469
+ url: `data:${e.mime_type || e.mimeType || "application/octet-stream"};base64,${e.file_data}`,
1470
+ type: e.mime_type || e.mimeType || "application/octet-stream"
1471
+ });
1472
+ continue;
1473
+ }
1474
+ if (e?.type === "input_file" && typeof e.file_url == "string" && e.file_url.trim()) {
1475
+ let t = e.file_url.trim();
1476
+ i({
1477
+ name: e.filename || e.displayName || e.display_name || "attachment",
1478
+ url: J(t),
1479
+ type: e.mime_type || e.mimeType || "application/octet-stream",
1480
+ fileUri: t
1481
+ });
1482
+ continue;
1483
+ }
1484
+ if (e?.type === "input_image" || e?.type === "image_url") {
1485
+ let t = typeof e.image_url == "string" ? e.image_url : e.image_url?.url;
1486
+ t && i({
1487
+ name: e.filename || e.displayName || e.display_name || "uploaded_image",
1488
+ url: t,
1489
+ type: e.mime_type || e.mimeType || at(t) || "image/*"
1490
+ });
1491
+ continue;
1492
+ }
1493
+ if (e?.type === "input_file" && e.fileData) {
1494
+ let t = String(e.fileData.fileUri || "").trim();
1495
+ i({
1496
+ name: e.fileData.displayName || "attachment",
1497
+ url: J(t),
1498
+ type: e.fileData.mimeType || "application/octet-stream",
1499
+ fileUri: t
1500
+ });
1501
+ }
1502
+ }
1503
+ let a = Array.isArray(e?.Metadata?.attachments) ? e.Metadata.attachments : [];
1504
+ for (let e of a) {
1505
+ let t = String(e.file_uri || "").trim(), n = {
1506
+ name: e.display_name || "attachment",
1507
+ url: J(t),
1508
+ type: e.mime_type || "application/octet-stream",
1509
+ fileUri: t
1510
+ };
1511
+ !n.url && !n.fileUri && Array.from(r.values()).some((e) => e.name === n.name && ot(e.type, n.type) && (e.url || e.fileUri)) || i(n);
1512
+ }
1513
+ let o = Array.from(r.values());
1514
+ return {
1515
+ text: n.join(""),
1516
+ attachments: o.length > 0 ? o : void 0
1517
+ };
1518
+ }
1519
+ function st(t) {
1520
+ let n = t?.Metadata?.responses_output;
1521
+ if (!Array.isArray(n)) return {};
1522
+ let r = R(), i = $e({
1523
+ eventName: "response.completed",
1524
+ data: { response: {
1525
+ id: String(t.Metadata?.response_id || ""),
1526
+ output: n
1527
+ } },
1528
+ state: r
1529
+ }), a = "", o = {};
1530
+ for (let t of i) {
1531
+ if (t.type === "reasoning_delta") {
1532
+ a += t.text;
1533
+ continue;
1534
+ }
1535
+ if (t.type === "tool_upsert") {
1536
+ o[t.name] = {
1537
+ ...o[t.name] || {
1538
+ name: t.name,
1539
+ args: ""
1540
+ },
1541
+ name: t.name,
1542
+ args: t.args,
1543
+ status: t.status,
1544
+ ...t.approvalRequestId ? { approvalRequestId: t.approvalRequestId } : {},
1545
+ ...t.previousResponseId ? { previousResponseId: t.previousResponseId } : {},
1546
+ ...t.serverLabel ? { serverLabel: t.serverLabel } : {},
1547
+ ...t.approvalRequestId ? { approvalStatus: "pending" } : {}
1548
+ };
1549
+ continue;
1550
+ }
1551
+ t.type === "tool_result" && (o[t.name] = {
1552
+ ...o[t.name] || {
1553
+ name: t.name,
1554
+ args: ""
1555
+ },
1556
+ name: t.name,
1557
+ output: t.output,
1558
+ status: e(t.output) ? "error" : "completed"
1559
+ });
1560
+ }
1561
+ return {
1562
+ ...a ? { reasoning: a } : {},
1563
+ ...Object.keys(o).length > 0 ? { tools: o } : {}
1564
+ };
1565
+ }
1566
+ function ct(e, t, n) {
1567
+ return t === "running" ? e === "prompt_too_long" ? "检测到上下文过长,正在自动压缩历史后重试" : "正在自动压缩上下文" : n ? e === "prompt_too_long" ? "上下文过长,系统已自动压缩历史并重试" : "系统已自动压缩较早的对话上下文" : t === "failed" ? "自动压缩上下文未完成" : e === "prompt_too_long" ? "已完成上下文压缩,并继续当前回复" : "已完成上下文压缩";
1568
+ }
1569
+ function lt(e, t) {
1570
+ return t?.itemId ? [
1571
+ "runtime-item",
1572
+ t.runId || e,
1573
+ t.scopeId || e,
1574
+ t.itemId
1575
+ ].join("\0") : [
1576
+ e,
1577
+ t.content || "",
1578
+ t.reasoning || ""
1579
+ ].join("\0");
1580
+ }
1581
+ function X(e) {
1582
+ let t = String(e?.InvocationId || "").trim(), n = e?.Metadata?.RuntimeItem;
1583
+ return n?.ItemId ? [
1584
+ "runtime-item",
1585
+ n.RunId || t,
1586
+ n.ScopeId || t,
1587
+ n.ItemId
1588
+ ].join("\0") : t;
1589
+ }
1590
+ function ut(e) {
1591
+ let t = e?.Metadata?.RuntimeItem;
1592
+ return t?.ItemId ? {
1593
+ id: `runtime:${t.RunId || e.InvocationId || "run"}:${t.ScopeId || e.InvocationId || "scope"}:${t.ItemId}`,
1594
+ runId: String(t.RunId || e.InvocationId || ""),
1595
+ scopeId: String(t.ScopeId || e.InvocationId || ""),
1596
+ itemId: String(t.ItemId),
1597
+ partId: t.PartId ? String(t.PartId) : void 0
1598
+ } : null;
1599
+ }
1600
+ function dt(e) {
1601
+ return e?.Metadata?.responses_mirror === !0 || String(e?.Metadata?.responses_mirror || "").trim().toLowerCase() === "true";
1602
+ }
1603
+ function ft(e) {
1604
+ if (e?.EventType !== "user_message") return "";
1605
+ let t = Y(e), n = (t.attachments || []).map((e) => [
1606
+ e.name || "",
1607
+ e.fileUri || "",
1608
+ e.url || "",
1609
+ e.type || ""
1610
+ ].join("")).sort().join("");
1611
+ return [String(t.text || "").trim(), n].join("\0");
1612
+ }
1613
+ function pt(e) {
1614
+ return e?.EventType === "assistant_stream_snapshot";
1615
+ }
1616
+ function Z(e) {
1617
+ let t = Number(e?.SeqId || 0);
1618
+ if (Number.isFinite(t) && t > 0) return t;
1619
+ let n = Number(e?.Timestamp || 0);
1620
+ return Number.isFinite(n) ? n : 0;
1621
+ }
1622
+ function mt(e) {
1623
+ if (typeof e == "string") return e;
1624
+ if (e == null) return "";
1625
+ try {
1626
+ return JSON.stringify(e, null, 2);
1627
+ } catch {
1628
+ return String(e);
1629
+ }
1630
+ }
1631
+ function ht(t) {
1632
+ let n = String(t?.EventType || "");
1633
+ if (!W.has(n)) return null;
1634
+ let r = String(t.Metadata?.tool_name || t.Metadata?.name || t.Metadata?.function_name || q(t.Content?.parts) || "tool").trim() || "tool", i = {
1635
+ name: r,
1636
+ args: "",
1637
+ status: "running"
1638
+ }, a = n === "tool_result" || n === "stage_tool_result" ? (() => {
1639
+ let n = mt(t.Metadata?.tool_output ?? t.Metadata?.output ?? t.Metadata?.result ?? q(t.Content?.parts));
1640
+ return {
1641
+ ...i,
1642
+ output: n,
1643
+ status: e(n) ? "error" : "completed"
1644
+ };
1645
+ })() : {
1646
+ ...i,
1647
+ args: mt(t.Metadata?.tool_args ?? t.Metadata?.arguments ?? t.Metadata?.args ?? {})
1648
+ }, o = ut(t);
1649
+ return {
1650
+ id: o?.id || t.EventId || String(Date.now() + Math.random()),
1651
+ role: "model",
1652
+ content: "",
1653
+ timestamp: t.Timestamp || Date.now(),
1654
+ eventType: n,
1655
+ ...o || {},
1656
+ tools: { [r]: a }
1657
+ };
1658
+ }
1659
+ function gt(e = {}, t = {}) {
1660
+ let n = { ...e || {} };
1661
+ for (let [e, r] of Object.entries(t || {})) n[e] = {
1662
+ ...n[e] || {},
1663
+ ...r || {},
1664
+ name: e,
1665
+ args: r?.args || n[e]?.args || ""
1666
+ };
1667
+ return n;
1668
+ }
1669
+ function _t(e = {}, t = "") {
1670
+ let n = String(t || "").trim().toLowerCase(), r = n === "completed" ? "completed" : et.has(n) ? "error" : "";
1671
+ if (!r) return e;
1672
+ let i = !1, a = {};
1673
+ for (let [t, n] of Object.entries(e || {})) n?.status === "running" ? (i = !0, a[t] = {
1674
+ ...n,
1675
+ status: r
1676
+ }) : a[t] = n;
1677
+ return i ? a : e;
1678
+ }
1679
+ function vt(e, t) {
1680
+ return {
1681
+ ...e,
1682
+ reasoning: yt(e.reasoning, t.reasoning),
1683
+ tools: gt(e.tools, t.tools),
1684
+ eventId: t.eventId || e.eventId,
1685
+ responseId: t.responseId || e.responseId,
1686
+ traceId: t.traceId || e.traceId,
1687
+ rootSpanId: t.rootSpanId || e.rootSpanId,
1688
+ timestamp: Math.max(Number(e.timestamp || 0), Number(t.timestamp || 0)) || t.timestamp || e.timestamp,
1689
+ id: t.responseId || t.reasoning || t.tools ? t.id : e.id
1690
+ };
1691
+ }
1692
+ function yt(e = "", t = "") {
1693
+ let n = String(e || ""), r = String(t || "");
1694
+ return n ? !r || n.endsWith(r) ? n : r.startsWith(n) ? r : `${n}${r}` : r;
1695
+ }
1696
+ function bt(e) {
1697
+ return {
1698
+ id: e.id,
1699
+ role: "system",
1700
+ eventType: "context_checkpoint",
1701
+ status: e.status,
1702
+ trigger: e.trigger,
1703
+ compactedUntilSeqId: e.compactedUntilSeqId,
1704
+ summary: e.summary,
1705
+ historical: e.historical,
1706
+ timestamp: e.timestamp,
1707
+ content: ct(e.trigger, e.status, e.historical)
1708
+ };
1709
+ }
1710
+ function xt(e) {
1711
+ let t = e?.EventType || "", n = ut(e);
1712
+ if (t === "run_status") return e.Content?.status === "failed" ? {
1713
+ id: e.EventId || String(Date.now() + Math.random()),
1714
+ role: "system",
1715
+ content: e.Content?.detail || "本轮运行失败。",
1716
+ eventType: t,
1717
+ status: "failed",
1718
+ timestamp: e.Timestamp || Date.now()
1719
+ } : e.Content?.status === "cancelled" ? {
1720
+ id: e.EventId || String(Date.now() + Math.random()),
1721
+ role: "system",
1722
+ content: e.Content?.detail || "本轮输出已停止。",
1723
+ eventType: t,
1724
+ status: "cancelled",
1725
+ timestamp: e.Timestamp || Date.now()
1726
+ } : null;
1727
+ if (t === "context_checkpoint") {
1728
+ let t = Y(e);
1729
+ return bt({
1730
+ id: e.EventId || String(Date.now() + Math.random()),
1731
+ timestamp: e.Timestamp || Date.now(),
1732
+ status: "completed",
1733
+ trigger: String(e.Metadata?.trigger || "auto"),
1734
+ compactedUntilSeqId: Number(e.Metadata?.compacted_until_seq_id || 0) || void 0,
1735
+ summary: t.text || void 0,
1736
+ historical: !0
1737
+ });
1738
+ }
1739
+ if (t === "reasoning") {
1740
+ let r = Y(e).text || q(e.Metadata?.reasoning);
1741
+ return r ? {
1742
+ id: n?.id || e.EventId || String(Date.now() + Math.random()),
1743
+ role: "model",
1744
+ content: "",
1745
+ reasoning: r,
1746
+ timestamp: e.Timestamp || Date.now(),
1747
+ eventType: t,
1748
+ ...n || {}
1749
+ } : null;
1750
+ }
1751
+ if (W.has(t)) return ht(e);
1752
+ if (t !== "user_message" && t !== "assistant_message" && t !== "assistant_stream_snapshot") return null;
1753
+ let r = Y(e), i = t === "assistant_message" ? st(e) : {};
1754
+ if (!r.text && !r.attachments?.length && !i.reasoning && !i.tools) return null;
1755
+ 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();
1756
+ return {
1757
+ id: n?.id || e.EventId || String(Date.now() + Math.random()),
1758
+ role: t === "user_message" ? "user" : "model",
1759
+ content: r.text,
1760
+ timestamp: e.Timestamp || Date.now(),
1761
+ eventType: t,
1762
+ eventId: e.EventId || void 0,
1763
+ responseId: a || void 0,
1764
+ traceId: o || void 0,
1765
+ rootSpanId: s || void 0,
1766
+ attachments: r.attachments,
1767
+ ...n || {},
1768
+ ...i
1769
+ };
1770
+ }
1771
+ function Q(e = []) {
1772
+ 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 = rt(e), c = [];
1773
+ for (let e of s) {
1774
+ let t = String(e?.EventId || "").trim();
1775
+ if (t) {
1776
+ if (o.has(t)) continue;
1777
+ o.add(t);
1778
+ }
1779
+ c.push(e);
1780
+ }
1781
+ let l = /* @__PURE__ */ new Set(), u = /* @__PURE__ */ new Set(), d = /* @__PURE__ */ new Map();
1782
+ for (let e of c) {
1783
+ if (e?.EventType !== "user_message" || dt(e)) {
1784
+ if (e?.EventType === "assistant_message") {
1785
+ let t = X(e);
1786
+ t && u.add(t);
1787
+ }
1788
+ if (pt(e)) {
1789
+ let t = X(e);
1790
+ if (t) {
1791
+ let n = d.get(t);
1792
+ (!n || Z(e) >= Z(n)) && d.set(t, e);
1793
+ }
1794
+ }
1795
+ continue;
1796
+ }
1797
+ let t = ft(e);
1798
+ t && l.add(t);
1799
+ }
1800
+ let f = c.filter((e) => {
1801
+ if (pt(e)) {
1802
+ let t = X(e);
1803
+ return t ? u.has(t) ? !1 : d.get(t) === e : !0;
1804
+ }
1805
+ if (e?.EventType !== "user_message" || !dt(e)) return !0;
1806
+ let t = ft(e);
1807
+ return !t || !l.has(t);
1808
+ });
1809
+ for (let e of f) {
1810
+ let a = String(e.InvocationId || "").trim();
1811
+ a && e.EventType === "user_message" && i.add(a), a && [
1812
+ "assistant_message",
1813
+ "assistant_stream_snapshot",
1814
+ "reasoning",
1815
+ "tool_call",
1816
+ "tool_result",
1817
+ "stage_tool_call",
1818
+ "stage_tool_result"
1819
+ ].includes(String(e.EventType || "")) && r.add(a), e.EventType === "run_status" && a && (t.set(a, String(e.Content?.status || "").trim()), n.set(a, e));
1820
+ }
1821
+ let p = [], m = /* @__PURE__ */ new Map(), h = /* @__PURE__ */ new Map(), g = null, _ = /* @__PURE__ */ new Map(), v = (e) => {
1822
+ let t = e?.itemId ? `${e.runId || e.invocationId || ""}\u0000${e.scopeId || e.invocationId || ""}\u0000${e.itemId}` : "", n = t ? h.get(t) : void 0;
1823
+ if (n !== void 0) {
1824
+ p[n] = vt(p[n], e);
1825
+ return;
1826
+ }
1827
+ let r = String(e?.responseId || "").trim(), i = r ? m.get(r) : void 0;
1828
+ if (i !== void 0) {
1829
+ p[i] = vt(p[i], e);
1830
+ return;
1831
+ }
1832
+ r && m.set(r, p.length), t && h.set(t, p.length), p.push(e);
1833
+ }, y = () => {
1834
+ g &&= (v(g), null);
1835
+ }, b = (e) => {
1836
+ let n = String(e || "").trim();
1837
+ if (!n) return {};
1838
+ let r = _.get(n);
1839
+ return r ? (_.delete(n), { tools: _t(r.tools, t.get(n)) }) : {};
1840
+ };
1841
+ for (let e of f) {
1842
+ if (e.EventType === "run_status") continue;
1843
+ let t = xt(e);
1844
+ if (!t) continue;
1845
+ let n = String(e.InvocationId || "").trim();
1846
+ if (n && (t.invocationId = n), W.has(t.eventType)) {
1847
+ let n = String(e.InvocationId || "").trim();
1848
+ if (t.itemId) {
1849
+ y(), v(t);
1850
+ continue;
1851
+ }
1852
+ if (!n) {
1853
+ y(), v(t);
1854
+ continue;
1855
+ }
1856
+ let r = _.get(n);
1857
+ _.set(n, {
1858
+ ...r || {
1859
+ id: t.id,
1860
+ role: "model",
1861
+ content: "",
1862
+ timestamp: t.timestamp,
1863
+ invocationId: n
1864
+ },
1865
+ timestamp: Math.max(Number(r?.timestamp || 0), Number(t.timestamp || 0)),
1866
+ tools: gt(r?.tools, t.tools)
1867
+ });
1868
+ continue;
1869
+ }
1870
+ if (t.eventType === "reasoning") {
1871
+ 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);
1872
+ if (g && (i || a)) {
1873
+ g.reasoning = r === "replace" || r === "completed" ? t.reasoning : `${g.reasoning || ""}${t.reasoning || ""}`, g.timestamp = t.timestamp;
1874
+ continue;
1875
+ }
1876
+ y(), g = {
1877
+ ...t,
1878
+ ...n ? { invocationId: n } : {}
1879
+ };
1880
+ continue;
1881
+ }
1882
+ let r = t.eventType === "assistant_message" || t.eventType === "assistant_stream_snapshot";
1883
+ if (r && g) {
1884
+ let n = String(e.InvocationId || "").trim(), r = lt(n, t);
1885
+ if (t.eventType === "assistant_message" && n && a.has(r)) {
1886
+ g = null;
1887
+ continue;
1888
+ }
1889
+ t.eventType === "assistant_message" && n && a.add(r), v({
1890
+ ...t,
1891
+ ...b(n),
1892
+ reasoning: yt(g.reasoning, t.reasoning)
1893
+ }), g = null;
1894
+ continue;
1895
+ }
1896
+ if (y(), r) {
1897
+ let n = String(e.InvocationId || "").trim(), r = lt(n, t);
1898
+ if (t.eventType === "assistant_message" && n && a.has(r)) continue;
1899
+ t.eventType === "assistant_message" && n && a.add(r), v({
1900
+ ...t,
1901
+ ...b(n)
1902
+ });
1903
+ continue;
1904
+ }
1905
+ v(t);
1906
+ }
1907
+ y();
1908
+ for (let [e, n] of _.entries()) v({
1909
+ ...n,
1910
+ tools: _t(n.tools, t.get(e))
1911
+ });
1912
+ for (let [e, a] of t.entries()) {
1913
+ if (a !== "in_progress" || r.has(e) || !i.has(e)) continue;
1914
+ let t = n.get(e) || {};
1915
+ v({
1916
+ id: `run-placeholder-${e}`,
1917
+ role: "model",
1918
+ content: "",
1919
+ status: "running",
1920
+ eventType: "run_status",
1921
+ timestamp: t.Timestamp || Date.now()
1922
+ });
1923
+ }
1924
+ return p;
1925
+ }
1926
+ function St(e = [], t = []) {
1927
+ let n = [], r = /* @__PURE__ */ new Set();
1928
+ for (let i of [...Array.isArray(e) ? e : [], ...Array.isArray(t) ? t : []]) {
1929
+ let e = String(i?.EventId || "").trim();
1930
+ if (e) {
1931
+ if (r.has(e)) continue;
1932
+ r.add(e);
1933
+ }
1934
+ n.push(i);
1935
+ }
1936
+ return n.sort((e, t) => {
1937
+ let n = Number(e?.SeqId || 0), r = Number(t?.SeqId || 0);
1938
+ return Number.isFinite(n) && Number.isFinite(r) && n !== r ? n - r : Number(e?.Timestamp || 0) - Number(t?.Timestamp || 0);
1939
+ });
1940
+ }
1941
+ function Ct(e = []) {
1942
+ return (Array.isArray(e) ? e : []).reduce((e, t) => {
1943
+ let n = Number(t.SeqId || 0);
1944
+ return Number.isFinite(n) ? Math.max(e, n) : e;
1945
+ }, 0);
1946
+ }
1947
+ function wt(e) {
1948
+ return et.has(it(e));
1949
+ }
1950
+ //#endregion
1951
+ //#region src/utils/persisted-session-history.ts
1952
+ function $(e) {
1953
+ return e && typeof e == "object" && !Array.isArray(e) ? e : null;
1954
+ }
1955
+ function Tt(e) {
1956
+ if (typeof e == "number" && Number.isFinite(e)) return e > 0 && e < 1e11 ? e * 1e3 : e;
1957
+ let t = Date.parse(String(e || ""));
1958
+ return Number.isFinite(t) ? t : Date.now();
1959
+ }
1960
+ function Et(e) {
1961
+ let t = $(e?.Content?.runtime_event);
1962
+ if (t && String(t.event_type || "")) return {
1963
+ ...t,
1964
+ family: "runtime",
1965
+ seq: Number(e.SeqId || t.seq || 0),
1966
+ timestamp: e.Timestamp || t.timestamp
1967
+ };
1968
+ let n = $(e?.Content?.session_event);
1969
+ if (!n || String(n.family || "") !== "runtime") return null;
1970
+ let r = $(n.payload);
1971
+ return !r || !String(r.event_type || n.event_type || "") ? null : {
1972
+ ...r,
1973
+ family: "runtime",
1974
+ event_type: r.event_type || n.event_type,
1975
+ run_id: r.run_id || n.run_id,
1976
+ seq: Number(e.SeqId || n.seq || r.seq || 0),
1977
+ timestamp: e.Timestamp || n.timestamp || r.timestamp
1978
+ };
1979
+ }
1980
+ function Dt(e) {
1981
+ return e.role === "model" ? {
1982
+ ...e,
1983
+ blocks: ze({
1984
+ content: e.content,
1985
+ reasoning: e.reasoning,
1986
+ tools: e.tools
1987
+ })
1988
+ } : e;
1989
+ }
1990
+ function Ot(e, t) {
1991
+ let n = t.find((e) => e.role === "user"), r = t.filter((e) => e.role === "model"), i = e.filter((e) => e.role === "model").at(-1), a = r.at(-1);
1992
+ return e.map((e) => e.role === "user" && n ? {
1993
+ ...e,
1994
+ ...n,
1995
+ invocationId: e.invocationId || n.invocationId
1996
+ } : Dt(e === i && a ? {
1997
+ ...e,
1998
+ responseId: a.responseId || e.responseId,
1999
+ traceId: a.traceId || e.traceId,
2000
+ rootSpanId: a.rootSpanId || e.rootSpanId
2001
+ } : e));
2002
+ }
2003
+ function kt(e) {
2004
+ return new Set(e.filter((e) => e.role === "model" && e.eventType === "assistant_message" && !!String(e.content || "").trim() && !!e.invocationId).map((e) => String(e.invocationId)));
2005
+ }
2006
+ function At(e, t, n) {
2007
+ let r = new L(n), i = [], a = /* @__PURE__ */ new Set(), o = [...t || []].sort((e, t) => Number(e.SeqId || 0) - Number(t.SeqId || 0));
2008
+ for (let e of o) {
2009
+ let t = Et(e);
2010
+ if (!t) continue;
2011
+ let n = String(t.run_id || "").trim();
2012
+ n && a.add(n);
2013
+ let o = r.translate(t);
2014
+ o && i.push({
2015
+ ...o,
2016
+ SeqId: Number(e.SeqId || o.SeqId || 0),
2017
+ Timestamp: Tt(e.Timestamp || o.Timestamp)
2018
+ });
2019
+ }
2020
+ let s = Q(i).filter((e) => e.invocationId && a.has(e.invocationId)), c = /* @__PURE__ */ new Map();
2021
+ for (let t of e) {
2022
+ let e = String(t.invocationId || "");
2023
+ e && c.set(e, [...c.get(e) || [], t]);
2024
+ }
2025
+ let l = kt(s), u = [...l].flatMap((e) => Ot(s.filter((t) => t.invocationId === e), c.get(e) || []));
2026
+ return {
2027
+ messages: [...e.filter((e) => e.role === "a2ui" || !e.invocationId || !l.has(e.invocationId)), ...u].sort((e, t) => Number(e.timestamp || 0) - Number(t.timestamp || 0)),
2028
+ canonicalRunIds: [...l],
2029
+ translatedEvents: i
2030
+ };
2031
+ }
2032
+ //#endregion
2033
+ export { ce as A, T as C, se as D, ee as E, oe as O, ye as S, te as T, Re as _, wt as a, je as b, it as c, L as d, We as f, Fe as g, ze as h, Q as i, t as j, re as k, R as l, Ne as m, At as n, Ct as o, Pe as p, bt as r, St as s, Et as t, $e as u, Ie as v, ve as w, xe as x, Le as y };