@kingsoftcloud/ksadk-web 0.3.4 → 0.3.5
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.
- package/CHANGELOG.md +25 -0
- package/README.md +31 -0
- package/RELEASE_PROVENANCE.json +2 -2
- package/dist-ksadk/assets/{ArtifactsPanel-10GPXIJ6.js → ArtifactsPanel-9RUQms_7.js} +1 -1
- package/dist-ksadk/assets/{CodeBlock-BXk1l-G1.js → CodeBlock-iq6mFygx.js} +1 -1
- package/dist-ksadk/assets/{MathMessageMarkdown-D9uB55Oh.js → MathMessageMarkdown-DSd_MMwp.js} +1 -1
- package/dist-ksadk/assets/{MermaidBlock--OEYoXIJ.js → MermaidBlock-B1tXyzN9.js} +6 -6
- package/dist-ksadk/assets/{NativeTerminalPanel-DrEe1syq.js → NativeTerminalPanel-BTTm24gh.js} +1 -1
- package/dist-ksadk/assets/{abnfDiagram-VCTEODGH-eeTFJLvo.js → abnfDiagram-VCTEODGH-CrPuGQ-J.js} +1 -1
- package/dist-ksadk/assets/{arc-S3ZNbIAd.js → arc-LSpozW7f.js} +1 -1
- package/dist-ksadk/assets/architecture-7GRP2DOG-DlLqIl7c.js +1 -0
- package/dist-ksadk/assets/{architectureDiagram-5GKGNRK7-CRjgEJDX.js → architectureDiagram-5GKGNRK7-BiBddfc6.js} +1 -1
- package/dist-ksadk/assets/{blockDiagram-NRAW4CY4-C7bsBoNH.js → blockDiagram-NRAW4CY4-Cfpt59CA.js} +1 -1
- package/dist-ksadk/assets/{c4Diagram-UCG6FXSJ-BX3c33Ob.js → c4Diagram-UCG6FXSJ-TtkMYecK.js} +1 -1
- package/dist-ksadk/assets/channel-cg7jYcXF.js +1 -0
- package/dist-ksadk/assets/chunk-2Q5K7J3B-C4iMKTWb.js +1 -0
- package/dist-ksadk/assets/{chunk-5VM5RSS4-bZ7ws-8m.js → chunk-5VM5RSS4-QNTrS1KC.js} +1 -1
- package/dist-ksadk/assets/{chunk-F27PBJKO-D2r0pvhY.js → chunk-F27PBJKO-CHsVsbjO.js} +1 -1
- package/dist-ksadk/assets/{chunk-G27WJ6UU-CZwF3oUP.js → chunk-G27WJ6UU-BiyNaPce.js} +1 -1
- package/dist-ksadk/assets/{chunk-JWPE2WC7-DigFYCML.js → chunk-JWPE2WC7-EjVrwIfj.js} +1 -1
- package/dist-ksadk/assets/{chunk-LCL6LL3I-CnorGV5q.js → chunk-LCL6LL3I-BCXNTVwQ.js} +1 -1
- package/dist-ksadk/assets/{chunk-POPQ4Y6H-CexntQA-.js → chunk-POPQ4Y6H-BNP6opFp.js} +1 -1
- package/dist-ksadk/assets/{chunk-RHFEMEQ7-CHTC7UxA.js → chunk-RHFEMEQ7-CroPk1q-.js} +1 -1
- package/dist-ksadk/assets/{chunk-SVP7TREG-BC1EWt_-.js → chunk-SVP7TREG-8eF2thNo.js} +1 -1
- package/dist-ksadk/assets/{chunk-XXDRQBXY-BTG24xN0.js → chunk-XXDRQBXY-CfKEfSWB.js} +1 -1
- package/dist-ksadk/assets/classDiagram-DTDB5LWJ-RtbaG_Dz.js +1 -0
- package/dist-ksadk/assets/classDiagram-v2-JRS7N3AN-RtbaG_Dz.js +1 -0
- package/dist-ksadk/assets/{cose-bilkent-JH36ORCC-CkonRdAS.js → cose-bilkent-JH36ORCC-NYLLr4y3.js} +1 -1
- package/dist-ksadk/assets/cynefin-OW5HDTMX-BRs0UJ32.js +1 -0
- package/dist-ksadk/assets/{cynefinDiagram-5FMLGOSQ-DEOCBe15.js → cynefinDiagram-5FMLGOSQ-1t2bjASn.js} +1 -1
- package/dist-ksadk/assets/{dagre-3AP2YEHR-DBThPuLa.js → dagre-3AP2YEHR-CyCcpGXQ.js} +1 -1
- package/dist-ksadk/assets/{dagre-ClTPrTht.js → dagre-DIBkWWe5.js} +1 -1
- package/dist-ksadk/assets/{diagram-S7CK7UJ4-DsZHrnpJ.js → diagram-S7CK7UJ4-Qnyx09fY.js} +1 -1
- package/dist-ksadk/assets/{diagram-UQ7AKVKN-BJR4nhVr.js → diagram-UQ7AKVKN-BBuBi2BK.js} +1 -1
- package/dist-ksadk/assets/{diagram-VSXAHHWV-xzcERX0a.js → diagram-VSXAHHWV-Cu2pi7DX.js} +1 -1
- package/dist-ksadk/assets/{diagram-VX7I27RA-tn2xM1xu.js → diagram-VX7I27RA-D1hy0wPv.js} +1 -1
- package/dist-ksadk/assets/{diagram-Z3DM3KII-BZPGBkw5.js → diagram-Z3DM3KII-DG_ukeOg.js} +1 -1
- package/dist-ksadk/assets/{dist-B1oWRmrH.js → dist-32Woo6w8.js} +1 -1
- package/dist-ksadk/assets/{dist-Btyo4vX4.js → dist-39frJg8c.js} +1 -1
- package/dist-ksadk/assets/{dist-DjhwGUgZ.js → dist-7mNpnBIq.js} +1 -1
- package/dist-ksadk/assets/{dist-C71CsujO.js → dist-BDYKkv8k.js} +1 -1
- package/dist-ksadk/assets/{dist-4zN1_tq5.js → dist-BRFE6wml.js} +1 -1
- package/dist-ksadk/assets/{dist-DdRBIceA.js → dist-Bami3Hbn.js} +1 -1
- package/dist-ksadk/assets/{dist-u8tVH5Ak.js → dist-BbqgJcRm.js} +1 -1
- package/dist-ksadk/assets/{dist-CXpH_Rdd.js → dist-Bo0uuYGj.js} +1 -1
- package/dist-ksadk/assets/{dist-BI1qQH4n.js → dist-BoDw8sHC.js} +1 -1
- package/dist-ksadk/assets/{dist-DJmCPRVB.js → dist-Bzd96FWg.js} +1 -1
- package/dist-ksadk/assets/{dist-BJLUwH6W.js → dist-C6YcRSBB.js} +1 -1
- package/dist-ksadk/assets/{dist-Cq4OL6nO.js → dist-C8E2u2Gy.js} +1 -1
- package/dist-ksadk/assets/{dist-DyqV4fL4.js → dist-CgpyS7Tw.js} +1 -1
- package/dist-ksadk/assets/{dist-D1V3f1B4.js → dist-CsdC51-G.js} +1 -1
- package/dist-ksadk/assets/{dist-8VWI-2bZ.js → dist-D0t-jg0i.js} +1 -1
- package/dist-ksadk/assets/{dist-DQAB0qn_.js → dist-D2l4f5Dh.js} +1 -1
- package/dist-ksadk/assets/{dist-DfGFrb0v.js → dist-Dbb1c9iJ.js} +1 -1
- package/dist-ksadk/assets/{dist-B-46q3Hk.js → dist-_RQDZ9v3.js} +1 -1
- package/dist-ksadk/assets/{dist-Bbwqa8TG.js → dist-dLCAHoLT.js} +1 -1
- package/dist-ksadk/assets/{dist-B0gdk4rl.js → dist-mo17RERT.js} +1 -1
- package/dist-ksadk/assets/{dist-M67Hwu0n.js → dist-snMJ4dAH.js} +1 -1
- package/dist-ksadk/assets/{dist-DUwKa_l6.js → dist-vM3yu1Iv.js} +1 -1
- package/dist-ksadk/assets/{ebnfDiagram-PWID7BFC-DVfMsLge.js → ebnfDiagram-PWID7BFC-DuBo2W05.js} +1 -1
- package/dist-ksadk/assets/{erDiagram-SSCWMZ5O-CWEZSYaw.js → erDiagram-SSCWMZ5O-ZJrJetMD.js} +1 -1
- package/dist-ksadk/assets/eventmodeling-NTZA5JFV-DCJzsswd.js +1 -0
- package/dist-ksadk/assets/flowDiagram-A5DVABFB-485rByL2.js +1 -0
- package/dist-ksadk/assets/{ganttDiagram-EL5Y4UJY-TWIxoyYL.js → ganttDiagram-EL5Y4UJY-Bwk2yIEM.js} +1 -1
- package/dist-ksadk/assets/gitGraph-4MIJSDKK-C5VB4yJL.js +1 -0
- package/dist-ksadk/assets/{gitGraphDiagram-WWUBYQGX-DDh2hd2S.js → gitGraphDiagram-WWUBYQGX-DWYLIagz.js} +1 -1
- package/dist-ksadk/assets/index-Cp5NEP4y.css +2 -0
- package/dist-ksadk/assets/index-j3aFdsSv.js +244 -0
- package/dist-ksadk/assets/info-A6RAGUB7-tqDdMmNT.js +1 -0
- package/dist-ksadk/assets/{infoDiagram-RXCK75RN-CgoyWiDR.js → infoDiagram-RXCK75RN-DMPen5_h.js} +1 -1
- package/dist-ksadk/assets/{ishikawaDiagram-5VMMS53U-CyRIu_om.js → ishikawaDiagram-5VMMS53U-DsPNKFIz.js} +1 -1
- package/dist-ksadk/assets/{journeyDiagram-EYS64GPL-DN5JXHzN.js → journeyDiagram-EYS64GPL-BNbTP-8a.js} +1 -1
- package/dist-ksadk/assets/{kanban-definition-3QL26DDD-CNP3ui66.js → kanban-definition-3QL26DDD-BKR6Itkv.js} +1 -1
- package/dist-ksadk/assets/{katex-DWEzQy1d.js → katex-B0oLa8CR.js} +1 -1
- package/dist-ksadk/assets/{linear-C-qHp7d2.js → linear-B3_la4oE.js} +1 -1
- package/dist-ksadk/assets/{mermaid-parser.core-Cl-K943T.js → mermaid-parser.core-B6PQSAV-.js} +2 -2
- package/dist-ksadk/assets/{mindmap-definition-FBJOCRG2-D5DB5mYm.js → mindmap-definition-FBJOCRG2-By8qFxGO.js} +1 -1
- package/dist-ksadk/assets/packet-AYTQ26CC-BxA-7Hkq.js +1 -0
- package/dist-ksadk/assets/{pegDiagram-XKGWAZYB-BQVc9ia_.js → pegDiagram-XKGWAZYB-K1kvFBKC.js} +1 -1
- package/dist-ksadk/assets/pie-WAS4IAKB-3Tmx9EyU.js +1 -0
- package/dist-ksadk/assets/{pieDiagram-E7YTZNPT-B6UCAvtY.js → pieDiagram-E7YTZNPT-5XFtDn56.js} +1 -1
- package/dist-ksadk/assets/{quadrantDiagram-AXDQQJYC-mIr2d3eb.js → quadrantDiagram-AXDQQJYC-DQwy74C1.js} +1 -1
- package/dist-ksadk/assets/radar-RG4KPBEZ-D_bZK5Bu.js +1 -0
- package/dist-ksadk/assets/railroad-74A4TZTK-COy6RdCd.js +1 -0
- package/dist-ksadk/assets/railroad-abnf-HS5TGJTU-CrdA8h6r.js +1 -0
- package/dist-ksadk/assets/railroad-ebnf-LZEXJU2U-BnDti7-r.js +1 -0
- package/dist-ksadk/assets/railroad-peg-WCYAUIDC-DmYdbc7f.js +1 -0
- package/dist-ksadk/assets/{railroadDiagram-O6MQD6OU-Dt3FFDmK.js → railroadDiagram-O6MQD6OU-cozhldnA.js} +1 -1
- package/dist-ksadk/assets/{requirementDiagram-EFPCY7ZU-Cz_vQtgF.js → requirementDiagram-EFPCY7ZU-CElPZjXR.js} +1 -1
- package/dist-ksadk/assets/{sankeyDiagram-P5KCCOFB-wlO1TsH5.js → sankeyDiagram-P5KCCOFB-y_USXv4y.js} +1 -1
- package/dist-ksadk/assets/{sequenceDiagram-WJ2MYXX4-CHRbElYq.js → sequenceDiagram-WJ2MYXX4-BJbQk7Za.js} +1 -1
- package/dist-ksadk/assets/{sizeCapture-X5ZJPWSS-DXX54iyO.js → sizeCapture-X5ZJPWSS-DAaf435S.js} +1 -1
- package/dist-ksadk/assets/{stateDiagram-HBIQ2CUA-Dp_lw9II.js → stateDiagram-HBIQ2CUA-BiLbLu1I.js} +1 -1
- package/dist-ksadk/assets/stateDiagram-v2-4QOOHH4V-5rxHhPTD.js +1 -0
- package/dist-ksadk/assets/{swimlanes-XN3QIQJK-D8O5UlLG.js → swimlanes-XN3QIQJK-DxhmU8aj.js} +1 -1
- package/dist-ksadk/assets/swimlanesDiagram-VK2B7HYN-B3Po2rui.js +8 -0
- package/dist-ksadk/assets/{timeline-definition-24CTP7MA-8mfKcxvG.js → timeline-definition-24CTP7MA-D18EWv7u.js} +1 -1
- package/dist-ksadk/assets/treeView-Q6P3EWNA-6G5wA0pE.js +1 -0
- package/dist-ksadk/assets/treemap-WGGIJYW6-BE9GnPbg.js +1 -0
- package/dist-ksadk/assets/{vennDiagram-4TSXK5OY-EnNl0DE7.js → vennDiagram-4TSXK5OY-rUBCuIGM.js} +1 -1
- package/dist-ksadk/assets/wardley-WFR3VGLG-BYh81kWC.js +1 -0
- package/dist-ksadk/assets/{wardleyDiagram-VM6X3IG4-DBu5fbYr.js → wardleyDiagram-VM6X3IG4-CDZQ5sZK.js} +1 -1
- package/dist-ksadk/assets/{xychartDiagram-S5SC5T6Z-BSVXs4IY.js → xychartDiagram-S5SC5T6Z-CH_HdHYk.js} +1 -1
- package/dist-ksadk/index.html +2 -2
- package/dist-lib/App.d.ts.map +1 -1
- package/dist-lib/{ArtifactsPanel-D36BykpN.js → ArtifactsPanel-BX-TQaOJ.js} +18 -17
- package/dist-lib/AttachmentPreview-D8BjnDTd.js +694 -0
- package/dist-lib/{CodeBlock-BlEkUJ8C.js → CodeBlock-D3-ZCOb_.js} +44 -41
- package/dist-lib/ConnectedComposer-BgTxuTfq.js +743 -0
- package/dist-lib/ConnectedMessageList-BwRiSswQ.js +1504 -0
- package/dist-lib/{MathMessageMarkdown-CcnPvEgd.js → MathMessageMarkdown-DwgeFqKs.js} +32 -31
- package/dist-lib/MenuChip-BXivncgX.js +43 -0
- package/dist-lib/{MermaidBlock-BFNE1NN8.js → MermaidBlock-DBFTztfQ.js} +15 -13
- package/dist-lib/{NativeTerminalPanel-Dix1UX_z.js → NativeTerminalPanel-DyPvD5ij.js} +166 -172
- package/dist-lib/WorkspacePanelContainer-C-na8Ns1.js +1617 -0
- package/dist-lib/a2ui-TSXmGrT-.js +486 -0
- package/dist-lib/api/client.d.ts +48 -14
- package/dist-lib/api/client.d.ts.map +1 -1
- package/dist-lib/{capabilities-njdklP3r.js → capabilities-CXle1R-K.js} +60 -30
- package/dist-lib/capabilities.js +1 -1
- package/dist-lib/chat-composer.js +4 -0
- package/dist-lib/chat-timeline.js +3 -0
- package/dist-lib/components/chat/A2UIActivityMessage.d.ts.map +1 -1
- package/dist-lib/components/chat/ChatComposer.d.ts +4 -3
- package/dist-lib/components/chat/ChatComposer.d.ts.map +1 -1
- package/dist-lib/components/chat/ChatMessageList.d.ts +7 -4
- package/dist-lib/components/chat/ChatMessageList.d.ts.map +1 -1
- package/dist-lib/components/chat/ConnectedComposer.d.ts +4 -3
- package/dist-lib/components/chat/ConnectedComposer.d.ts.map +1 -1
- package/dist-lib/components/chat/ConnectedMessageList.d.ts +6 -3
- package/dist-lib/components/chat/ConnectedMessageList.d.ts.map +1 -1
- package/dist-lib/components/chat/ContextUsageIndicator.d.ts +4 -8
- package/dist-lib/components/chat/ContextUsageIndicator.d.ts.map +1 -1
- package/dist-lib/components/chat/ExecutionModeMenu.d.ts.map +1 -1
- package/dist-lib/components/chat/InteractionHistoryAnchor.d.ts +1 -1
- package/dist-lib/components/chat/InteractionHistoryAnchor.d.ts.map +1 -1
- package/dist-lib/components/chat/InteractionSchemaForm.d.ts.map +1 -1
- package/dist-lib/components/chat/InteractionTray.d.ts +4 -4
- package/dist-lib/components/chat/InteractionTray.d.ts.map +1 -1
- package/dist-lib/components/chat/MenuChip.d.ts +19 -0
- package/dist-lib/components/chat/MenuChip.d.ts.map +1 -0
- package/dist-lib/components/chat/ModelSettingsMenu.d.ts.map +1 -1
- package/dist-lib/components/chat/PermissionMenu.d.ts.map +1 -1
- package/dist-lib/components/chat/ProcessingBlocksView.d.ts +3 -3
- package/dist-lib/components/chat/ProcessingBlocksView.d.ts.map +1 -1
- package/dist-lib/components/chat/schema-fields.d.ts +4 -1
- package/dist-lib/components/chat/schema-fields.d.ts.map +1 -1
- package/dist-lib/components/chat/types.d.ts +4 -1
- package/dist-lib/components/chat/types.d.ts.map +1 -1
- package/dist-lib/components.js +38 -30
- package/dist-lib/conversation.js +1 -1
- package/dist-lib/core/api/facade.d.ts +70 -13
- package/dist-lib/core/api/facade.d.ts.map +1 -1
- package/dist-lib/core/api/types.d.ts +4 -1
- package/dist-lib/core/api/types.d.ts.map +1 -1
- package/dist-lib/core/conversation/hosted.d.ts.map +1 -1
- package/dist-lib/core/conversation/index.d.ts +1 -1
- package/dist-lib/core/conversation/index.d.ts.map +1 -1
- package/dist-lib/core/interaction/adapters/normalize.d.ts +2 -2
- package/dist-lib/core/interaction/adapters/normalize.d.ts.map +1 -1
- package/dist-lib/core/interaction/adapters/responses.d.ts.map +1 -1
- package/dist-lib/core/interaction/adapters/session-events.d.ts.map +1 -1
- package/dist-lib/core/interaction/client.d.ts.map +1 -1
- package/dist-lib/core/interaction/index.d.ts +8 -0
- package/dist-lib/core/interaction/index.d.ts.map +1 -1
- package/dist-lib/core/interaction/store.d.ts +0 -0
- package/dist-lib/core/interaction/store.d.ts.map +1 -1
- package/dist-lib/core/run/a2ui.d.ts +2 -0
- package/dist-lib/core/run/a2ui.d.ts.map +1 -1
- package/dist-lib/core/run/blocks.d.ts +1 -0
- package/dist-lib/core/run/blocks.d.ts.map +1 -1
- package/dist-lib/core/run/dispatcher.d.ts.map +1 -1
- package/dist-lib/core/run/engine.d.ts.map +1 -1
- package/dist-lib/core/run/types.d.ts +3 -1
- package/dist-lib/core/run/types.d.ts.map +1 -1
- package/dist-lib/core/stream/kernel-events.d.ts +3 -0
- package/dist-lib/core/stream/kernel-events.d.ts.map +1 -1
- package/dist-lib/core/stream/types.d.ts +7 -0
- package/dist-lib/core/stream/types.d.ts.map +1 -1
- package/dist-lib/feedback-D256P415.js +239 -0
- package/dist-lib/hooks/useAgentChat.d.ts +95 -0
- package/dist-lib/hooks/useAgentChat.d.ts.map +1 -0
- package/dist-lib/hooks/useBootstrap.d.ts +3 -2
- package/dist-lib/hooks/useBootstrap.d.ts.map +1 -1
- package/dist-lib/hooks/useRunAgent.d.ts +7 -0
- package/dist-lib/hooks/useRunAgent.d.ts.map +1 -1
- package/dist-lib/hooks/useSessionLifecycle.d.ts +2 -0
- package/dist-lib/hooks/useSessionLifecycle.d.ts.map +1 -1
- package/dist-lib/hooks.js +167 -0
- package/dist-lib/permission-BWkTcIzC.js +24 -0
- package/dist-lib/{persisted-session-history-C0N8TrMb.js → persisted-session-history-Bb3ScKrL.js} +463 -266
- package/dist-lib/public/chat-composer.d.ts +15 -0
- package/dist-lib/public/chat-composer.d.ts.map +1 -0
- package/dist-lib/public/chat-timeline.d.ts +12 -0
- package/dist-lib/public/chat-timeline.d.ts.map +1 -0
- package/dist-lib/public/components.d.ts +23 -0
- package/dist-lib/public/components.d.ts.map +1 -1
- package/dist-lib/public/hooks.d.ts +3 -0
- package/dist-lib/public/hooks.d.ts.map +1 -0
- package/dist-lib/public/runtime.d.ts +2 -0
- package/dist-lib/public/runtime.d.ts.map +1 -1
- package/dist-lib/public/styles.d.ts +1 -1
- package/dist-lib/rolldown-runtime-Dy4uBu1J.js +11 -0
- package/dist-lib/{tool-display-C4tPXce0.js → runtime-items-q6tMjfGU.js} +0 -0
- package/dist-lib/runtime.js +277 -4987
- package/dist-lib/sandbox-BTRIKAO6.js +92 -0
- package/dist-lib/stores/model.d.ts +1 -0
- package/dist-lib/stores/model.d.ts.map +1 -1
- package/dist-lib/styles.css +1 -1
- package/dist-lib/tool-display-DTiRdghE.js +64 -0
- package/dist-lib/types/capabilities.d.ts +1 -0
- package/dist-lib/types/capabilities.d.ts.map +1 -1
- package/dist-lib/useSessionLifecycle-DuzkfGgL.js +4233 -0
- package/dist-lib/utils/capabilities.d.ts.map +1 -1
- package/dist-lib/utils/chat-message-grouping.d.ts +4 -0
- package/dist-lib/utils/chat-message-grouping.d.ts.map +1 -0
- package/dist-lib/utils/chat-scroll.d.ts +14 -0
- package/dist-lib/utils/chat-scroll.d.ts.map +1 -0
- package/dist-lib/utils/context.d.ts +12 -7
- package/dist-lib/utils/context.d.ts.map +1 -1
- package/dist-lib/utils/messages.d.ts.map +1 -1
- package/dist-lib/utils/persisted-session-history.d.ts +2 -0
- package/dist-lib/utils/persisted-session-history.d.ts.map +1 -1
- package/dist-lib/utils/responses-stream.d.ts +2 -0
- package/dist-lib/utils/responses-stream.d.ts.map +1 -1
- package/dist-lib/utils/session-events.d.ts.map +1 -1
- package/dist-lib/utils/session.d.ts +1 -0
- package/dist-lib/utils/session.d.ts.map +1 -1
- package/dist-lib/utils/workspace.d.ts +1 -1
- package/package.json +13 -1
- package/dist-ksadk/assets/architecture-7GRP2DOG-Dk9nRQ6-.js +0 -1
- package/dist-ksadk/assets/channel-s354Yo6o.js +0 -1
- package/dist-ksadk/assets/chunk-2Q5K7J3B-CDpPpKR5.js +0 -1
- package/dist-ksadk/assets/classDiagram-DTDB5LWJ-Dd0nucl9.js +0 -1
- package/dist-ksadk/assets/classDiagram-v2-JRS7N3AN-Dd0nucl9.js +0 -1
- package/dist-ksadk/assets/cynefin-OW5HDTMX-BBlT4uMf.js +0 -1
- package/dist-ksadk/assets/eventmodeling-NTZA5JFV-Dr5okd8V.js +0 -1
- package/dist-ksadk/assets/flowDiagram-A5DVABFB-VEO-cVcO.js +0 -1
- package/dist-ksadk/assets/gitGraph-4MIJSDKK-CKiKY9Cf.js +0 -1
- package/dist-ksadk/assets/index-B2k_urY8.js +0 -241
- package/dist-ksadk/assets/index-ILPt6IWF.css +0 -2
- package/dist-ksadk/assets/info-A6RAGUB7-ubA4Nm_p.js +0 -1
- package/dist-ksadk/assets/packet-AYTQ26CC-C7R3NUlK.js +0 -1
- package/dist-ksadk/assets/pie-WAS4IAKB-BIT6qjr3.js +0 -1
- package/dist-ksadk/assets/radar-RG4KPBEZ-l5noef9X.js +0 -1
- package/dist-ksadk/assets/railroad-74A4TZTK-Baea6ZvM.js +0 -1
- package/dist-ksadk/assets/railroad-abnf-HS5TGJTU-OKn8zZnU.js +0 -1
- package/dist-ksadk/assets/railroad-ebnf-LZEXJU2U-DdKAKC4l.js +0 -1
- package/dist-ksadk/assets/railroad-peg-WCYAUIDC-Bds8udlG.js +0 -1
- package/dist-ksadk/assets/stateDiagram-v2-4QOOHH4V-5b-8o2iS.js +0 -1
- package/dist-ksadk/assets/swimlanesDiagram-VK2B7HYN-uf6WPRA4.js +0 -8
- package/dist-ksadk/assets/treeView-Q6P3EWNA-CK5CSrSO.js +0 -1
- package/dist-ksadk/assets/treemap-WGGIJYW6-Ch2BKgAM.js +0 -1
- package/dist-ksadk/assets/wardley-WFR3VGLG-YW3Q0ml_.js +0 -1
- package/dist-lib/WorkspacePanelContainer-Cr1BwR1P.js +0 -4397
- package/dist-lib/api/bootstrap.d.ts +0 -2
- package/dist-lib/api/bootstrap.d.ts.map +0 -1
- package/dist-lib/api/checkpoints.d.ts +0 -38
- package/dist-lib/api/checkpoints.d.ts.map +0 -1
- package/dist-lib/api/events.d.ts +0 -20
- package/dist-lib/api/events.d.ts.map +0 -1
- package/dist-lib/api/messages.d.ts +0 -73
- package/dist-lib/api/messages.d.ts.map +0 -1
- package/dist-lib/api/model.d.ts +0 -2
- package/dist-lib/api/model.d.ts.map +0 -1
- package/dist-lib/api/run.d.ts +0 -36
- package/dist-lib/api/run.d.ts.map +0 -1
- package/dist-lib/api/session.d.ts +0 -61
- package/dist-lib/api/session.d.ts.map +0 -1
- package/dist-lib/api/upload.d.ts +0 -12
- package/dist-lib/api/upload.d.ts.map +0 -1
- package/dist-lib/api/workspace.d.ts +0 -8
- package/dist-lib/api/workspace.d.ts.map +0 -1
|
@@ -0,0 +1,4233 @@
|
|
|
1
|
+
import { a as e, i as t, o as n, t as r } from "./capabilities-CXle1R-K.js";
|
|
2
|
+
import { a as i, d as a, f as o, h as s, i as c, m as l, n as u, o as d, p as f, s as p, t as m, u as h } from "./a2ui-TSXmGrT-.js";
|
|
3
|
+
import { n as g } from "./runtime-items-q6tMjfGU.js";
|
|
4
|
+
import { a as _, c as v, i as y, l as b, n as x, o as S, r as C, s as w, t as T } from "./feedback-D256P415.js";
|
|
5
|
+
import { n as E } from "./tool-display-DTiRdghE.js";
|
|
6
|
+
import { A as D, T as O, _ as k, a as A, b as ee, c as j, d as M, f as te, g as ne, h as re, i as ie, j as N, k as ae, l as oe, m as se, n as ce, o as le, p as ue, r as de, s as fe, u as pe, v as me, y as he } from "./persisted-session-history-Bb3ScKrL.js";
|
|
7
|
+
import { useCallback as P, useEffect as F, useMemo as ge, useRef as I, useState as _e } from "react";
|
|
8
|
+
import { create as ve } from "zustand";
|
|
9
|
+
import { z as L } from "zod";
|
|
10
|
+
import { HttpAgent as ye, buildResumeArray as be } from "@ag-ui/client";
|
|
11
|
+
//#region src/api/client.ts
|
|
12
|
+
var R = class extends Error {
|
|
13
|
+
code;
|
|
14
|
+
detail;
|
|
15
|
+
constructor(e, t, n) {
|
|
16
|
+
super(t), this.name = "ApiError", this.code = e, this.detail = n;
|
|
17
|
+
}
|
|
18
|
+
}, z = class extends Error {
|
|
19
|
+
constructor(e = "请求已取消") {
|
|
20
|
+
super(e), this.name = "CancelledError";
|
|
21
|
+
}
|
|
22
|
+
}, xe = "/agentengine/api/v1";
|
|
23
|
+
async function Se(e) {
|
|
24
|
+
let t = await e.text(), n = {};
|
|
25
|
+
try {
|
|
26
|
+
n = t ? JSON.parse(t) : {};
|
|
27
|
+
} catch {
|
|
28
|
+
let n = e.headers.get("content-type")?.toLowerCase() || "";
|
|
29
|
+
if ((e.status === 401 || e.status === 403) && (n.includes("text/html") || t.trimStart().startsWith("<"))) throw new R(e.status, "访问会话已失效,请从 Dashboard 或 Studio 的云端会话重新打开 Agent。");
|
|
30
|
+
if (!e.ok) {
|
|
31
|
+
let n = t.trim();
|
|
32
|
+
throw new R(e.status, n && n.length <= 256 && !n.startsWith("<") ? n : e.statusText || `HTTP ${e.status}`, t);
|
|
33
|
+
}
|
|
34
|
+
throw new R(-2, "响应格式异常");
|
|
35
|
+
}
|
|
36
|
+
if (!e.ok) throw new R(e.status, n?.Message || `HTTP ${e.status}`, n);
|
|
37
|
+
if (n?.Code !== void 0 && n.Code !== 0) throw new R(n.Code, n.Message || "请求失败", n);
|
|
38
|
+
return n.Data;
|
|
39
|
+
}
|
|
40
|
+
function B(e) {
|
|
41
|
+
throw e instanceof DOMException && e.name === "AbortError" ? new z() : e;
|
|
42
|
+
}
|
|
43
|
+
var Ce = class {
|
|
44
|
+
fetcher;
|
|
45
|
+
baseUrl;
|
|
46
|
+
agentId;
|
|
47
|
+
constructor(e = {}) {
|
|
48
|
+
this.fetcher = e.fetch || ((e, t) => globalThis.fetch(e, t)), this.baseUrl = String(e.baseUrl || xe).replace(/\/+$/, ""), this.agentId = String(e.agentId || "").trim();
|
|
49
|
+
}
|
|
50
|
+
actionUrl(e) {
|
|
51
|
+
return `${this.baseUrl}/${e}`;
|
|
52
|
+
}
|
|
53
|
+
scopedBody(e) {
|
|
54
|
+
return this.agentId && !e.AgentId ? {
|
|
55
|
+
...e,
|
|
56
|
+
AgentId: this.agentId
|
|
57
|
+
} : e;
|
|
58
|
+
}
|
|
59
|
+
async postJsonAction(e, t, n) {
|
|
60
|
+
let r;
|
|
61
|
+
try {
|
|
62
|
+
r = await this.fetcher(this.actionUrl(e), {
|
|
63
|
+
method: "POST",
|
|
64
|
+
headers: { "Content-Type": "application/json" },
|
|
65
|
+
body: JSON.stringify(this.scopedBody(t)),
|
|
66
|
+
signal: n?.signal
|
|
67
|
+
});
|
|
68
|
+
} catch (e) {
|
|
69
|
+
throw B(e);
|
|
70
|
+
}
|
|
71
|
+
return Se(r);
|
|
72
|
+
}
|
|
73
|
+
async postFormAction(e, t, n) {
|
|
74
|
+
let r;
|
|
75
|
+
this.agentId && !t.has("AgentId") && t.append("AgentId", this.agentId);
|
|
76
|
+
try {
|
|
77
|
+
r = await this.fetcher(this.actionUrl(e), {
|
|
78
|
+
method: "POST",
|
|
79
|
+
body: t,
|
|
80
|
+
signal: n?.signal
|
|
81
|
+
});
|
|
82
|
+
} catch (e) {
|
|
83
|
+
throw B(e);
|
|
84
|
+
}
|
|
85
|
+
return Se(r);
|
|
86
|
+
}
|
|
87
|
+
async getResource(e, t, n) {
|
|
88
|
+
let r = this.agentId && !t.AgentId ? {
|
|
89
|
+
AgentId: this.agentId,
|
|
90
|
+
...t
|
|
91
|
+
} : t, i = new URLSearchParams(r).toString(), a = i ? `${this.actionUrl(e)}?${i}` : this.actionUrl(e), o;
|
|
92
|
+
try {
|
|
93
|
+
o = await this.fetcher(a, { signal: n?.signal });
|
|
94
|
+
} catch (e) {
|
|
95
|
+
throw B(e);
|
|
96
|
+
}
|
|
97
|
+
if (!o.ok) throw new R(o.status, `资源获取失败: ${o.statusText}`);
|
|
98
|
+
try {
|
|
99
|
+
return n?.asText ? await o.text() : await o.blob();
|
|
100
|
+
} catch {
|
|
101
|
+
throw new R(-3, "文件读取失败");
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
async streamAction(e, t, n) {
|
|
105
|
+
let r;
|
|
106
|
+
try {
|
|
107
|
+
r = await this.fetcher(this.actionUrl(e), {
|
|
108
|
+
method: "POST",
|
|
109
|
+
headers: {
|
|
110
|
+
"Content-Type": "application/json",
|
|
111
|
+
Accept: "text/event-stream"
|
|
112
|
+
},
|
|
113
|
+
body: JSON.stringify(this.scopedBody(t)),
|
|
114
|
+
signal: n?.signal
|
|
115
|
+
});
|
|
116
|
+
} catch (e) {
|
|
117
|
+
throw B(e);
|
|
118
|
+
}
|
|
119
|
+
if (!r.ok) throw await Se(r), new R(r.status, `流式请求失败: ${r.statusText}`);
|
|
120
|
+
if ((r.headers.get("content-type")?.toLowerCase() || "").includes("application/json") && await Se(r.clone()), !r.body) throw new R(-1, "无法获取响应流");
|
|
121
|
+
return r.body;
|
|
122
|
+
}
|
|
123
|
+
async streamGetAction(e, t, n) {
|
|
124
|
+
let r = this.agentId && !t.AgentId ? {
|
|
125
|
+
AgentId: this.agentId,
|
|
126
|
+
...t
|
|
127
|
+
} : t, i = new URLSearchParams(r).toString(), a = i ? `${this.actionUrl(e)}?${i}` : this.actionUrl(e), o;
|
|
128
|
+
try {
|
|
129
|
+
o = await this.fetcher(a, {
|
|
130
|
+
headers: { Accept: "text/event-stream" },
|
|
131
|
+
signal: n?.signal
|
|
132
|
+
});
|
|
133
|
+
} catch (e) {
|
|
134
|
+
throw B(e);
|
|
135
|
+
}
|
|
136
|
+
if (!o.ok) throw new R(o.status, `流式订阅失败: ${o.statusText}`);
|
|
137
|
+
if (!o.body) throw new R(-1, "无法获取响应流");
|
|
138
|
+
return o.body;
|
|
139
|
+
}
|
|
140
|
+
}, we = new Ce(), Te = (e, t, n) => we.streamAction(e, t, n), Ee = (e, t, n) => we.streamGetAction(e, t, n), V = class extends Error {
|
|
141
|
+
constructor(e) {
|
|
142
|
+
super(e), this.name = "ContractMismatchError";
|
|
143
|
+
}
|
|
144
|
+
}, De = new Set([
|
|
145
|
+
"schema_version",
|
|
146
|
+
"command_id",
|
|
147
|
+
"status",
|
|
148
|
+
"message_id",
|
|
149
|
+
"run_id",
|
|
150
|
+
"accepted_seq",
|
|
151
|
+
"error"
|
|
152
|
+
]), Oe = L.object({
|
|
153
|
+
code: L.string().min(1),
|
|
154
|
+
message: L.string(),
|
|
155
|
+
retryable: L.boolean(),
|
|
156
|
+
details: L.record(L.string(), L.unknown()).optional()
|
|
157
|
+
});
|
|
158
|
+
function H(e) {
|
|
159
|
+
let t = L.object({
|
|
160
|
+
schema_version: L.literal(1),
|
|
161
|
+
command_id: L.string().min(1),
|
|
162
|
+
status: L.enum([
|
|
163
|
+
"accepted",
|
|
164
|
+
"duplicate",
|
|
165
|
+
"rejected",
|
|
166
|
+
"unsupported",
|
|
167
|
+
"queue_full",
|
|
168
|
+
"persistence_uncertain"
|
|
169
|
+
]),
|
|
170
|
+
message_id: L.string().min(1).nullable().optional(),
|
|
171
|
+
run_id: L.string().min(1).nullable().optional(),
|
|
172
|
+
accepted_seq: L.number().int().min(0).nullable().optional(),
|
|
173
|
+
error: Oe.nullable().optional()
|
|
174
|
+
}).passthrough().safeParse(e);
|
|
175
|
+
if (!t.success) throw new V(`AgentControlReceipt/v1 mismatch: ${t.error.message}`);
|
|
176
|
+
let n = t.data;
|
|
177
|
+
if ((n.status === "rejected" || n.status === "unsupported" || n.status === "queue_full" || n.status === "persistence_uncertain") && !n.error) throw new V(`AgentControlReceipt/v1 mismatch: status ${n.status} requires error`);
|
|
178
|
+
let r = {};
|
|
179
|
+
for (let [e, t] of Object.entries(n)) De.has(e) || (r[e] = t);
|
|
180
|
+
let i = n.error ? {
|
|
181
|
+
...n.error,
|
|
182
|
+
extensions: {}
|
|
183
|
+
} : null;
|
|
184
|
+
return {
|
|
185
|
+
schema_version: 1,
|
|
186
|
+
command_id: n.command_id,
|
|
187
|
+
status: n.status,
|
|
188
|
+
message_id: n.message_id ?? null,
|
|
189
|
+
run_id: n.run_id ?? null,
|
|
190
|
+
accepted_seq: n.accepted_seq ?? null,
|
|
191
|
+
error: i,
|
|
192
|
+
extensions: r
|
|
193
|
+
};
|
|
194
|
+
}
|
|
195
|
+
function ke(e) {
|
|
196
|
+
try {
|
|
197
|
+
return n(e);
|
|
198
|
+
} catch (e) {
|
|
199
|
+
throw new V(`RuntimeCapabilityMatrix/v1 mismatch: ${e instanceof Error ? e.message : String(e)}`);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
var Ae = new Set([
|
|
203
|
+
"schema_version",
|
|
204
|
+
"event_id",
|
|
205
|
+
"session_id",
|
|
206
|
+
"seq",
|
|
207
|
+
"timestamp",
|
|
208
|
+
"family",
|
|
209
|
+
"family_version",
|
|
210
|
+
"event_type",
|
|
211
|
+
"payload",
|
|
212
|
+
"run_id",
|
|
213
|
+
"causation_id",
|
|
214
|
+
"correlation_id",
|
|
215
|
+
"actor_ref"
|
|
216
|
+
]), je = new Map([["control", 1], ["runtime", 2]]), Me = L.object({
|
|
217
|
+
schema_version: L.literal(1),
|
|
218
|
+
event_id: L.string().min(1),
|
|
219
|
+
session_id: L.string().min(1),
|
|
220
|
+
seq: L.number().int().min(0),
|
|
221
|
+
timestamp: L.string().min(1),
|
|
222
|
+
family: L.string().min(1),
|
|
223
|
+
family_version: L.number().int().min(1),
|
|
224
|
+
event_type: L.string().min(1),
|
|
225
|
+
payload: L.record(L.string(), L.unknown()),
|
|
226
|
+
run_id: L.string().min(1).nullable().optional(),
|
|
227
|
+
causation_id: L.string().min(1).nullable().optional(),
|
|
228
|
+
correlation_id: L.string().min(1).nullable().optional(),
|
|
229
|
+
actor_ref: L.string().min(1).nullable().optional()
|
|
230
|
+
}).passthrough();
|
|
231
|
+
function Ne(e) {
|
|
232
|
+
let t = Me.safeParse(e);
|
|
233
|
+
if (!t.success) return {
|
|
234
|
+
ok: !1,
|
|
235
|
+
error: new V(`SessionEventEnvelope/v1 mismatch: ${t.error.message}`)
|
|
236
|
+
};
|
|
237
|
+
let n = t.data, r = je.get(n.family);
|
|
238
|
+
if (r !== void 0 && n.family_version !== r) return {
|
|
239
|
+
ok: !1,
|
|
240
|
+
error: new V(`SessionEventEnvelope/v1 mismatch: family ${n.family} requires family_version ${r}`)
|
|
241
|
+
};
|
|
242
|
+
let i = {};
|
|
243
|
+
for (let [e, t] of Object.entries(n)) Ae.has(e) || (i[e] = t);
|
|
244
|
+
return {
|
|
245
|
+
ok: !0,
|
|
246
|
+
value: {
|
|
247
|
+
schema_version: 1,
|
|
248
|
+
event_id: n.event_id,
|
|
249
|
+
session_id: n.session_id,
|
|
250
|
+
seq: n.seq,
|
|
251
|
+
timestamp: n.timestamp,
|
|
252
|
+
family: n.family,
|
|
253
|
+
family_version: n.family_version,
|
|
254
|
+
event_type: n.event_type,
|
|
255
|
+
payload: n.payload,
|
|
256
|
+
run_id: n.run_id ?? null,
|
|
257
|
+
causation_id: n.causation_id ?? null,
|
|
258
|
+
correlation_id: n.correlation_id ?? null,
|
|
259
|
+
actor_ref: n.actor_ref ?? null,
|
|
260
|
+
extensions: i
|
|
261
|
+
}
|
|
262
|
+
};
|
|
263
|
+
}
|
|
264
|
+
function Pe(e) {
|
|
265
|
+
let t = L.object({
|
|
266
|
+
schema_version: L.literal(1),
|
|
267
|
+
agent_instance_id: L.string().min(1),
|
|
268
|
+
instance_state: L.enum([
|
|
269
|
+
"ready",
|
|
270
|
+
"degraded",
|
|
271
|
+
"unavailable"
|
|
272
|
+
]),
|
|
273
|
+
session_id: L.string().min(1).nullable().optional(),
|
|
274
|
+
active_run_id: L.string().min(1).nullable().optional(),
|
|
275
|
+
active_run_state: L.enum([
|
|
276
|
+
"pending",
|
|
277
|
+
"running",
|
|
278
|
+
"paused",
|
|
279
|
+
"waiting"
|
|
280
|
+
]).nullable().optional(),
|
|
281
|
+
inbox_depth: L.number().int().min(0),
|
|
282
|
+
activation_id: L.string().min(1).nullable().optional(),
|
|
283
|
+
lease_expires_at: L.string().min(1).nullable().optional(),
|
|
284
|
+
capability: L.unknown()
|
|
285
|
+
}).passthrough().safeParse(e);
|
|
286
|
+
if (!t.success) throw new V(`AgentStatusSnapshot/v1 mismatch: ${t.error.message}`);
|
|
287
|
+
let n = t.data, r = {};
|
|
288
|
+
for (let e of Object.keys(n)) [
|
|
289
|
+
"schema_version",
|
|
290
|
+
"agent_instance_id",
|
|
291
|
+
"instance_state",
|
|
292
|
+
"session_id",
|
|
293
|
+
"active_run_id",
|
|
294
|
+
"active_run_state",
|
|
295
|
+
"inbox_depth",
|
|
296
|
+
"activation_id",
|
|
297
|
+
"lease_expires_at",
|
|
298
|
+
"capability"
|
|
299
|
+
].includes(e) || (r[e] = n[e]);
|
|
300
|
+
return {
|
|
301
|
+
schema_version: 1,
|
|
302
|
+
agent_instance_id: n.agent_instance_id,
|
|
303
|
+
instance_state: n.instance_state,
|
|
304
|
+
session_id: n.session_id ?? null,
|
|
305
|
+
active_run_id: n.active_run_id ?? null,
|
|
306
|
+
active_run_state: n.active_run_state ?? null,
|
|
307
|
+
inbox_depth: n.inbox_depth,
|
|
308
|
+
activation_id: n.activation_id ?? null,
|
|
309
|
+
lease_expires_at: n.lease_expires_at ?? null,
|
|
310
|
+
capability: ke(n.capability),
|
|
311
|
+
extensions: r
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
//#endregion
|
|
315
|
+
//#region src/core/api/facade.ts
|
|
316
|
+
var Fe = class {
|
|
317
|
+
client;
|
|
318
|
+
constructor(e = {}) {
|
|
319
|
+
this.client = new Ce(e);
|
|
320
|
+
}
|
|
321
|
+
async compactSession(e, t) {
|
|
322
|
+
return this.client.postJsonAction("CompactSession", {
|
|
323
|
+
AgentId: e,
|
|
324
|
+
SessionId: t
|
|
325
|
+
});
|
|
326
|
+
}
|
|
327
|
+
async listSessions(e, t) {
|
|
328
|
+
let n = await this.client.postJsonAction("ListSessions", {
|
|
329
|
+
AgentId: e,
|
|
330
|
+
Page: t?.page,
|
|
331
|
+
PageSize: t?.pageSize
|
|
332
|
+
}, t), r = n.Sessions ?? [];
|
|
333
|
+
return {
|
|
334
|
+
Sessions: r,
|
|
335
|
+
Total: Number.isFinite(Number(n.Total)) ? Number(n.Total) : r.length,
|
|
336
|
+
Page: Number.isFinite(Number(n.Page)) ? Number(n.Page) : t?.page ?? 1,
|
|
337
|
+
PageSize: Number.isFinite(Number(n.PageSize)) ? Number(n.PageSize) : t?.pageSize ?? r.length
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
async createSession(e, t) {
|
|
341
|
+
let n = await this.client.postJsonAction("CreateSession", { AgentId: e }, t);
|
|
342
|
+
return { SessionId: String(n.Session?.SessionId || "") };
|
|
343
|
+
}
|
|
344
|
+
async deleteSession(e, t) {
|
|
345
|
+
return this.client.postJsonAction("DeleteSession", { SessionId: e }, t);
|
|
346
|
+
}
|
|
347
|
+
async getSession(e, t) {
|
|
348
|
+
return (await this.client.postJsonAction("GetSession", { SessionId: e }, t)).Session;
|
|
349
|
+
}
|
|
350
|
+
async listSessionEvents(e, t) {
|
|
351
|
+
let n = await this.client.postJsonAction("ListSessionEvents", {
|
|
352
|
+
SessionId: e,
|
|
353
|
+
Offset: t?.offset,
|
|
354
|
+
Limit: t?.limit
|
|
355
|
+
}, t), r = n.Events ?? [];
|
|
356
|
+
return {
|
|
357
|
+
Events: r,
|
|
358
|
+
Total: Number.isFinite(Number(n.Total)) ? Number(n.Total) : r.length,
|
|
359
|
+
Offset: Number.isFinite(Number(n.Offset)) ? Number(n.Offset) : t?.offset ?? 0,
|
|
360
|
+
Limit: Number.isFinite(Number(n.Limit)) ? Number(n.Limit) : t?.limit ?? r.length
|
|
361
|
+
};
|
|
362
|
+
}
|
|
363
|
+
async listSessionMessages(e, t) {
|
|
364
|
+
let n = await this.client.postJsonAction("ListSessionMessages", {
|
|
365
|
+
AgentId: t?.agentId,
|
|
366
|
+
SessionId: e,
|
|
367
|
+
AfterSeqId: t?.afterSeqId,
|
|
368
|
+
BeforeSeqId: t?.beforeSeqId,
|
|
369
|
+
Limit: t?.limit,
|
|
370
|
+
IncludeReasoning: t?.includeReasoning,
|
|
371
|
+
IncludeToolEvents: t?.includeToolEvents,
|
|
372
|
+
IncludeAttachments: t?.includeAttachments
|
|
373
|
+
}, t);
|
|
374
|
+
return {
|
|
375
|
+
Messages: n.Messages ?? [],
|
|
376
|
+
LatestSeqId: Number.isFinite(Number(n.LatestSeqId)) ? Number(n.LatestSeqId) : 0,
|
|
377
|
+
HasMore: !!n.HasMore,
|
|
378
|
+
NextCursor: n.NextCursor === null || n.NextCursor === void 0 ? null : Number.isFinite(Number(n.NextCursor)) ? Number(n.NextCursor) : null
|
|
379
|
+
};
|
|
380
|
+
}
|
|
381
|
+
async listSessionCheckpoints(e, t) {
|
|
382
|
+
return this.client.postJsonAction("ListSessionCheckpoints", {
|
|
383
|
+
AgentId: e.agentId,
|
|
384
|
+
SessionId: e.sessionId,
|
|
385
|
+
...e.runId ? { RunId: e.runId } : {}
|
|
386
|
+
}, t);
|
|
387
|
+
}
|
|
388
|
+
async previewCheckpointResume(e, t) {
|
|
389
|
+
return this.client.postJsonAction("GetCheckpointResumePreview", {
|
|
390
|
+
AgentId: e.agentId,
|
|
391
|
+
SessionId: e.sessionId,
|
|
392
|
+
RunId: e.runId,
|
|
393
|
+
CheckpointId: e.checkpointId
|
|
394
|
+
}, t);
|
|
395
|
+
}
|
|
396
|
+
async listToolReceipts(e, t) {
|
|
397
|
+
return this.client.postJsonAction("ListToolReceipts", {
|
|
398
|
+
AgentId: e.agentId,
|
|
399
|
+
SessionId: e.sessionId,
|
|
400
|
+
...e.runId ? { RunId: e.runId } : {},
|
|
401
|
+
...e.checkpointId ? { CheckpointId: e.checkpointId } : {}
|
|
402
|
+
}, t);
|
|
403
|
+
}
|
|
404
|
+
async runAgent(e, t) {
|
|
405
|
+
return this.client.streamAction("RunAgent", e, t);
|
|
406
|
+
}
|
|
407
|
+
async resumeRun(e, t) {
|
|
408
|
+
return this.client.streamAction("ResumeRun", {
|
|
409
|
+
AgentId: e.agentId,
|
|
410
|
+
SessionId: e.sessionId,
|
|
411
|
+
RunId: e.runId,
|
|
412
|
+
CheckpointId: e.checkpointId,
|
|
413
|
+
Stream: !0,
|
|
414
|
+
...e.resumeAttemptId ? { ResumeAttemptId: e.resumeAttemptId } : {},
|
|
415
|
+
...e.invocationId ? { InvocationId: e.invocationId } : {}
|
|
416
|
+
}, t);
|
|
417
|
+
}
|
|
418
|
+
async subscribeRunEvents(e, t) {
|
|
419
|
+
return this.client.streamGetAction("SubscribeRunEvents", {
|
|
420
|
+
SessionId: e.sessionId,
|
|
421
|
+
InvocationId: e.invocationId,
|
|
422
|
+
AfterSeqId: String(e.afterSeqId)
|
|
423
|
+
}, t);
|
|
424
|
+
}
|
|
425
|
+
async submitControl(e, t) {
|
|
426
|
+
return H(await this.client.postJsonAction("SubmitAgentControl", {
|
|
427
|
+
CommandType: e.command_type,
|
|
428
|
+
IdempotencyKey: e.idempotency_key,
|
|
429
|
+
Payload: e.payload
|
|
430
|
+
}, t));
|
|
431
|
+
}
|
|
432
|
+
async submitInteraction(e, t) {
|
|
433
|
+
return H(await this.client.postJsonAction("SubmitInteraction", { ...e }, t));
|
|
434
|
+
}
|
|
435
|
+
async getAgentStatus(e) {
|
|
436
|
+
return this.client.postJsonAction("GetAgentStatus", {}, e);
|
|
437
|
+
}
|
|
438
|
+
async subscribeSessionEvents(e, t, n) {
|
|
439
|
+
return this.client.streamGetAction("SubscribeSessionEvents", {
|
|
440
|
+
SessionId: e,
|
|
441
|
+
after_seq: String(t)
|
|
442
|
+
}, n);
|
|
443
|
+
}
|
|
444
|
+
async cancelRun(e, t, n, r) {
|
|
445
|
+
return this.client.postJsonAction("CancelRun", {
|
|
446
|
+
AgentId: e,
|
|
447
|
+
SessionId: t,
|
|
448
|
+
InvocationId: n
|
|
449
|
+
}, r);
|
|
450
|
+
}
|
|
451
|
+
async getResponseFeedback(e, t) {
|
|
452
|
+
return this.client.postJsonAction("GetResponseFeedback", e, t);
|
|
453
|
+
}
|
|
454
|
+
async upsertResponseFeedback(e, t) {
|
|
455
|
+
return this.client.postJsonAction("UpsertResponseFeedback", e, t);
|
|
456
|
+
}
|
|
457
|
+
async deleteResponseFeedback(e, t) {
|
|
458
|
+
await this.client.postJsonAction("DeleteResponseFeedback", e, t);
|
|
459
|
+
}
|
|
460
|
+
async listWorkspaceFiles(e, t, n, r) {
|
|
461
|
+
return this.client.postJsonAction("ListWorkspaceFiles", {
|
|
462
|
+
AgentId: e,
|
|
463
|
+
Path: t,
|
|
464
|
+
Recursive: n
|
|
465
|
+
}, r);
|
|
466
|
+
}
|
|
467
|
+
async addWorkspaceFile(e, t) {
|
|
468
|
+
return this.client.postFormAction("AddWorkspaceFile", e, t);
|
|
469
|
+
}
|
|
470
|
+
async deleteWorkspaceFile(e, t, n) {
|
|
471
|
+
await this.client.postJsonAction("DeleteWorkspaceFile", {
|
|
472
|
+
AgentId: e,
|
|
473
|
+
Path: t
|
|
474
|
+
}, n);
|
|
475
|
+
}
|
|
476
|
+
async getWorkspaceFileContent(e, t, n) {
|
|
477
|
+
return this.client.getResource("GetWorkspaceFileContent", {
|
|
478
|
+
AgentId: e,
|
|
479
|
+
Path: t
|
|
480
|
+
}, n);
|
|
481
|
+
}
|
|
482
|
+
async listAgentModels(e, t) {
|
|
483
|
+
return this.client.postJsonAction("ListAgentModels", { AgentId: e }, t);
|
|
484
|
+
}
|
|
485
|
+
async getAgentUiBootstrap(e, t) {
|
|
486
|
+
return this.client.postJsonAction("GetAgentUiBootstrap", e ? { AgentId: e } : {}, t);
|
|
487
|
+
}
|
|
488
|
+
async uploadFile(e, t) {
|
|
489
|
+
return this.client.postFormAction("UploadFile", e, t);
|
|
490
|
+
}
|
|
491
|
+
}, U = ve()((e) => ({
|
|
492
|
+
agentId: "default-agent",
|
|
493
|
+
agentName: "Agent",
|
|
494
|
+
agentFramework: "",
|
|
495
|
+
capabilities: t({}),
|
|
496
|
+
apiFormats: ["responses", "chat_completions"],
|
|
497
|
+
accessMode: "Owner",
|
|
498
|
+
workspaceFiles: null,
|
|
499
|
+
status: "loading",
|
|
500
|
+
errorMessage: "",
|
|
501
|
+
setAgentId: (t) => e({ agentId: t }),
|
|
502
|
+
setAgentName: (t) => e({ agentName: t }),
|
|
503
|
+
setAgentFramework: (t) => e({ agentFramework: t }),
|
|
504
|
+
setCapabilities: (t) => e({ capabilities: t }),
|
|
505
|
+
setApiFormats: (t) => e({ apiFormats: t }),
|
|
506
|
+
setAccessMode: (t) => e({ accessMode: t }),
|
|
507
|
+
setWorkspaceFiles: (t) => e({ workspaceFiles: t }),
|
|
508
|
+
setStatus: (t, n = "") => e({
|
|
509
|
+
status: t,
|
|
510
|
+
errorMessage: n
|
|
511
|
+
})
|
|
512
|
+
})), Ie = "ksadk:webui:selected-session:";
|
|
513
|
+
function Le(e) {
|
|
514
|
+
return String(e || "").trim() || "default-agent";
|
|
515
|
+
}
|
|
516
|
+
function Re(e) {
|
|
517
|
+
if (e && typeof e.getItem == "function") return e;
|
|
518
|
+
try {
|
|
519
|
+
if (typeof window < "u" && window.localStorage) return window.localStorage;
|
|
520
|
+
} catch {}
|
|
521
|
+
return null;
|
|
522
|
+
}
|
|
523
|
+
function ze(e) {
|
|
524
|
+
return `${Ie}${Le(e)}`;
|
|
525
|
+
}
|
|
526
|
+
function Be(e, t) {
|
|
527
|
+
let n = Re(t);
|
|
528
|
+
if (!n) return null;
|
|
529
|
+
try {
|
|
530
|
+
return String(n.getItem(ze(e)) || "").trim() || null;
|
|
531
|
+
} catch {
|
|
532
|
+
return null;
|
|
533
|
+
}
|
|
534
|
+
}
|
|
535
|
+
function Ve(e, t, n) {
|
|
536
|
+
let r = Re(n);
|
|
537
|
+
if (!r) return;
|
|
538
|
+
let i = ze(e), a = String(t || "").trim();
|
|
539
|
+
try {
|
|
540
|
+
if (a) {
|
|
541
|
+
r.setItem(i, a);
|
|
542
|
+
return;
|
|
543
|
+
}
|
|
544
|
+
r.removeItem(i);
|
|
545
|
+
} catch {}
|
|
546
|
+
}
|
|
547
|
+
function He(e, t) {
|
|
548
|
+
if (!Array.isArray(e) || e.length === 0) return null;
|
|
549
|
+
let n = String(t || "").trim();
|
|
550
|
+
if (n && e.some((e) => String(e?.SessionId || "").trim() === n)) return n;
|
|
551
|
+
for (let t of e) {
|
|
552
|
+
let e = String(t?.SessionId || "").trim();
|
|
553
|
+
if (e) return e;
|
|
554
|
+
}
|
|
555
|
+
return null;
|
|
556
|
+
}
|
|
557
|
+
function Ue(e, t, n) {
|
|
558
|
+
let r = Array.isArray(e) ? e : [], i = Array.isArray(t) ? t : [], a = n instanceof Set ? n : new Set(Array.isArray(n) ? n : []);
|
|
559
|
+
if (a.size === 0) return r;
|
|
560
|
+
let o = new Set(r.map((e) => String(e?.SessionId || "").trim()).filter(Boolean)), s = i.filter((e) => {
|
|
561
|
+
let t = String(e?.SessionId || "").trim();
|
|
562
|
+
return t && a.has(t) && !o.has(t);
|
|
563
|
+
});
|
|
564
|
+
return s.length > 0 ? [...r, ...s] : r;
|
|
565
|
+
}
|
|
566
|
+
//#endregion
|
|
567
|
+
//#region src/hooks/useBootstrap.ts
|
|
568
|
+
function We(e) {
|
|
569
|
+
return e instanceof R && (e.code === 401 || e.code === 403) ? "auth-required" : "error";
|
|
570
|
+
}
|
|
571
|
+
function Ge(e) {
|
|
572
|
+
if (!Array.isArray(e)) return ["responses", "chat_completions"];
|
|
573
|
+
let t = e.filter((e) => e === "responses" || e === "chat_completions");
|
|
574
|
+
return t.length > 0 ? t : ["responses", "chat_completions"];
|
|
575
|
+
}
|
|
576
|
+
var Ke = new Fe();
|
|
577
|
+
async function qe(e, t = Ke) {
|
|
578
|
+
try {
|
|
579
|
+
let n = await t.listAgentModels(e), r = n?.Models;
|
|
580
|
+
Array.isArray(r) && o.getState().upsertModels(r);
|
|
581
|
+
let i = n;
|
|
582
|
+
i?.Current && !o.getState().selectedModel && o.getState().setSelectedModel(String(i.Current)), i?.Source && o.getState().setModelSource(String(i.Source));
|
|
583
|
+
} catch (e) {
|
|
584
|
+
console.error("Failed to fetch models:", e);
|
|
585
|
+
} finally {
|
|
586
|
+
o.getState().setModelCatalogLoaded(!0);
|
|
587
|
+
}
|
|
588
|
+
}
|
|
589
|
+
function Je(e, n, i = Ke) {
|
|
590
|
+
F(() => {
|
|
591
|
+
let a = new AbortController();
|
|
592
|
+
return (async () => {
|
|
593
|
+
U.getState().setStatus("loading");
|
|
594
|
+
try {
|
|
595
|
+
let c = await i.getAgentUiBootstrap(n, { signal: a.signal });
|
|
596
|
+
if (a.signal.aborted) return;
|
|
597
|
+
let l = c, u = l?.Agent, d = String(u?.AgentId || "default-agent");
|
|
598
|
+
if (U.getState().setAgentId(d), u?.Name) {
|
|
599
|
+
let e = String(u.Name);
|
|
600
|
+
U.getState().setAgentName(e), document.title = e;
|
|
601
|
+
}
|
|
602
|
+
let f = String(u?.Framework || "").trim().toLowerCase();
|
|
603
|
+
U.getState().setAgentFramework(f);
|
|
604
|
+
let p = t(c);
|
|
605
|
+
U.getState().setCapabilities(p), U.getState().setApiFormats(Ge(p.HostedChat.ApiFormats)), U.getState().setAccessMode(String(l?.AccessMode || "Owner"));
|
|
606
|
+
let m = p.WorkspaceFiles && l?.WorkspaceFiles ? l.WorkspaceFiles : null;
|
|
607
|
+
U.getState().setWorkspaceFiles(m), m || s.getState().setWorkspacePanelOpen(!1), r(p) && e.fetchSessions(d, Be(d));
|
|
608
|
+
let h = l?.Model;
|
|
609
|
+
h?.id && (o.getState().setSelectedModel(h.id), o.getState().upsertModels([h]), o.getState().setModelSource(h.source || "")), qe(d, i), U.getState().setStatus("ready");
|
|
610
|
+
} catch (e) {
|
|
611
|
+
if (a.signal.aborted) return;
|
|
612
|
+
console.error("Failed to fetch bootstrap:", e);
|
|
613
|
+
let t = We(e);
|
|
614
|
+
U.getState().setStatus(t, t === "auth-required" ? "当前访问链接未授权或已过期。" : "无法加载 Agent 配置,请稍后重试。");
|
|
615
|
+
}
|
|
616
|
+
})(), () => a.abort();
|
|
617
|
+
}, [i, n]);
|
|
618
|
+
}
|
|
619
|
+
//#endregion
|
|
620
|
+
//#region src/hooks/useFeedback.ts
|
|
621
|
+
function Ye(e) {
|
|
622
|
+
let { agentId: t, currentSessionId: n, isStreaming: r, api: i, submitDraft: a } = e;
|
|
623
|
+
return {
|
|
624
|
+
submitResponseFeedback: P(async (e) => {
|
|
625
|
+
if (!n) return;
|
|
626
|
+
let r = null;
|
|
627
|
+
f.getState().patchMessages((t) => {
|
|
628
|
+
let n = T(t, {
|
|
629
|
+
messageId: e.message.id,
|
|
630
|
+
rating: e.rating,
|
|
631
|
+
comment: e.comment || ""
|
|
632
|
+
});
|
|
633
|
+
return r = n.previousFeedback, n.nextMessages;
|
|
634
|
+
});
|
|
635
|
+
try {
|
|
636
|
+
let r = S((await i.upsertResponseFeedback(C({
|
|
637
|
+
agentId: t,
|
|
638
|
+
sessionId: n,
|
|
639
|
+
message: e.message,
|
|
640
|
+
rating: e.rating,
|
|
641
|
+
comment: e.comment || ""
|
|
642
|
+
})))?.Feedback);
|
|
643
|
+
r && f.getState().patchMessages((t) => _(t, {
|
|
644
|
+
messageId: e.message.id,
|
|
645
|
+
feedback: r
|
|
646
|
+
}));
|
|
647
|
+
} catch (t) {
|
|
648
|
+
console.error("Failed to submit response feedback:", t), f.getState().patchMessages((t) => w(t, {
|
|
649
|
+
messageId: e.message.id,
|
|
650
|
+
previousFeedback: r
|
|
651
|
+
}));
|
|
652
|
+
}
|
|
653
|
+
}, [
|
|
654
|
+
t,
|
|
655
|
+
n,
|
|
656
|
+
i
|
|
657
|
+
]),
|
|
658
|
+
deleteResponseFeedback: P(async (e) => {
|
|
659
|
+
if (!n) return;
|
|
660
|
+
let r = e.feedback ? { ...e.feedback } : null;
|
|
661
|
+
f.getState().patchMessages((t) => y(t, { messageId: e.id }));
|
|
662
|
+
try {
|
|
663
|
+
await i.deleteResponseFeedback(x({
|
|
664
|
+
agentId: t,
|
|
665
|
+
sessionId: n,
|
|
666
|
+
message: e
|
|
667
|
+
}));
|
|
668
|
+
} catch (t) {
|
|
669
|
+
console.error("Failed to delete response feedback:", t), f.getState().patchMessages((t) => w(t, {
|
|
670
|
+
messageId: e.id,
|
|
671
|
+
previousFeedback: r
|
|
672
|
+
}));
|
|
673
|
+
}
|
|
674
|
+
}, [
|
|
675
|
+
t,
|
|
676
|
+
n,
|
|
677
|
+
i
|
|
678
|
+
]),
|
|
679
|
+
respondToApproval: P((e) => {
|
|
680
|
+
!e.approvalRequestId || r || (f.getState().patchMessages((t) => t.map((t) => {
|
|
681
|
+
let n = e.approve ? "approved" : "rejected", r = (t) => e.approve && t === "paused" ? "running" : e.approve ? t : "completed";
|
|
682
|
+
return {
|
|
683
|
+
...t,
|
|
684
|
+
tools: t.tools ? Object.fromEntries(Object.entries(t.tools).map(([t, i]) => [t, i.approvalRequestId === e.approvalRequestId ? {
|
|
685
|
+
...i,
|
|
686
|
+
status: r(i.status),
|
|
687
|
+
approvalStatus: n
|
|
688
|
+
} : i])) : t.tools,
|
|
689
|
+
blocks: t.blocks?.map((t) => t.type === "tool" && t.extra?.approvalRequestId === e.approvalRequestId ? {
|
|
690
|
+
...t,
|
|
691
|
+
status: r(t.status),
|
|
692
|
+
extra: {
|
|
693
|
+
...t.extra,
|
|
694
|
+
approvalStatus: n
|
|
695
|
+
}
|
|
696
|
+
} : t)
|
|
697
|
+
};
|
|
698
|
+
})), a("", [], [{
|
|
699
|
+
type: "mcp_approval_response",
|
|
700
|
+
approval_request_id: e.approvalRequestId,
|
|
701
|
+
approve: e.approve
|
|
702
|
+
}], e.previousResponseId));
|
|
703
|
+
}, [r, a])
|
|
704
|
+
};
|
|
705
|
+
}
|
|
706
|
+
//#endregion
|
|
707
|
+
//#region src/core/interaction/store.ts
|
|
708
|
+
var Xe = class {
|
|
709
|
+
records = /* @__PURE__ */ new Map();
|
|
710
|
+
terminalRuns = /* @__PURE__ */ new Set();
|
|
711
|
+
listeners = /* @__PURE__ */ new Set();
|
|
712
|
+
key(e, t) {
|
|
713
|
+
return `${e}\u0000${t}`;
|
|
714
|
+
}
|
|
715
|
+
runKey(e, t) {
|
|
716
|
+
return `${e}\u0000${t}`;
|
|
717
|
+
}
|
|
718
|
+
subscribe(e) {
|
|
719
|
+
return this.listeners.add(e), () => {
|
|
720
|
+
this.listeners.delete(e);
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
emit(e) {
|
|
724
|
+
for (let t of this.listeners) t(e);
|
|
725
|
+
}
|
|
726
|
+
get(e, t) {
|
|
727
|
+
return this.records.get(this.key(e, t)) || null;
|
|
728
|
+
}
|
|
729
|
+
listPending(e) {
|
|
730
|
+
return this.listAll(e).filter((e) => e.status === "pending");
|
|
731
|
+
}
|
|
732
|
+
listAll(e) {
|
|
733
|
+
return this.all().filter((t) => t.sessionId === e);
|
|
734
|
+
}
|
|
735
|
+
all() {
|
|
736
|
+
return [...this.records.values()].sort((e, t) => String(e.createdAt).localeCompare(String(t.createdAt)));
|
|
737
|
+
}
|
|
738
|
+
upsert(e) {
|
|
739
|
+
e.runId && !d(e.status) && this.terminalRuns.has(this.runKey(e.sessionId, e.runId)) && (e = {
|
|
740
|
+
...e,
|
|
741
|
+
status: "expired",
|
|
742
|
+
outcome: "expired",
|
|
743
|
+
resolvedAt: e.resolvedAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
744
|
+
responseSummary: e.responseSummary ?? "运行已结束"
|
|
745
|
+
});
|
|
746
|
+
let t = this.get(e.sessionId, e.interactionId), n, r;
|
|
747
|
+
return t ? d(t.status) && !d(e.status) || e.revision < t.revision || e.revision === t.revision && d(t.status) && !d(e.status) || t.status === "resolving" && !d(e.status) ? (n = t, r = {
|
|
748
|
+
type: "interaction_updated",
|
|
749
|
+
interaction: n
|
|
750
|
+
}) : d(e.status) ? (n = {
|
|
751
|
+
...t,
|
|
752
|
+
...e,
|
|
753
|
+
title: t.title || e.title,
|
|
754
|
+
kind: t.kind || e.kind,
|
|
755
|
+
message: t.message || e.message,
|
|
756
|
+
runId: e.runId ?? t.runId,
|
|
757
|
+
requestSchema: t.requestSchema ?? e.requestSchema,
|
|
758
|
+
presentation: t.presentation ?? e.presentation,
|
|
759
|
+
actor: e.actor ?? t.actor,
|
|
760
|
+
outcome: e.outcome ?? t.outcome,
|
|
761
|
+
resolvedAt: e.resolvedAt ?? t.resolvedAt,
|
|
762
|
+
responseSummary: e.responseSummary ?? t.responseSummary
|
|
763
|
+
}, r = {
|
|
764
|
+
type: "interaction_resolved",
|
|
765
|
+
interaction: n
|
|
766
|
+
}) : (n = {
|
|
767
|
+
...t,
|
|
768
|
+
...e
|
|
769
|
+
}, r = {
|
|
770
|
+
type: "interaction_updated",
|
|
771
|
+
interaction: n
|
|
772
|
+
}) : (n = e, r = {
|
|
773
|
+
type: "interaction_requested",
|
|
774
|
+
interaction: n
|
|
775
|
+
}), this.records.set(this.key(n.sessionId, n.interactionId), n), this.emit(r), r;
|
|
776
|
+
}
|
|
777
|
+
markResolving(e, t) {
|
|
778
|
+
let n = this.get(e, t);
|
|
779
|
+
!n || n.status !== "pending" || (this.records.set(this.key(e, t), {
|
|
780
|
+
...n,
|
|
781
|
+
status: "resolving"
|
|
782
|
+
}), this.emit({
|
|
783
|
+
type: "interaction_updated",
|
|
784
|
+
interaction: this.get(e, t)
|
|
785
|
+
}));
|
|
786
|
+
}
|
|
787
|
+
resolveLocally(e, t, n) {
|
|
788
|
+
let r = this.get(e, t);
|
|
789
|
+
if (!r) return;
|
|
790
|
+
let i = {
|
|
791
|
+
...r,
|
|
792
|
+
...n,
|
|
793
|
+
resolvedAt: n.resolvedAt ?? r.resolvedAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
794
|
+
revision: n.revision ?? r.revision
|
|
795
|
+
};
|
|
796
|
+
this.records.set(this.key(e, t), i), this.emit({
|
|
797
|
+
type: "interaction_resolved",
|
|
798
|
+
interaction: i
|
|
799
|
+
});
|
|
800
|
+
}
|
|
801
|
+
recordIdempotencyKey(e, t, n) {
|
|
802
|
+
let r = this.get(e, t);
|
|
803
|
+
if (!r) return;
|
|
804
|
+
let i = {
|
|
805
|
+
...r,
|
|
806
|
+
extensions: {
|
|
807
|
+
...r.extensions,
|
|
808
|
+
idempotency_key: n
|
|
809
|
+
}
|
|
810
|
+
};
|
|
811
|
+
this.records.set(this.key(e, t), i), this.emit({
|
|
812
|
+
type: "interaction_updated",
|
|
813
|
+
interaction: i
|
|
814
|
+
});
|
|
815
|
+
}
|
|
816
|
+
markFailed(e, t, n) {
|
|
817
|
+
let r = this.get(e, t);
|
|
818
|
+
if (!r || r.status !== "resolving") return;
|
|
819
|
+
let i = {
|
|
820
|
+
...r,
|
|
821
|
+
status: "failed",
|
|
822
|
+
extensions: {
|
|
823
|
+
...r.extensions,
|
|
824
|
+
submit_error: n
|
|
825
|
+
}
|
|
826
|
+
};
|
|
827
|
+
this.records.set(this.key(e, t), i), this.emit({
|
|
828
|
+
type: "interaction_updated",
|
|
829
|
+
interaction: i
|
|
830
|
+
});
|
|
831
|
+
}
|
|
832
|
+
revertToPending(e, t) {
|
|
833
|
+
let n = this.get(e, t);
|
|
834
|
+
if (!n || n.status !== "resolving") return;
|
|
835
|
+
let r = {
|
|
836
|
+
...n,
|
|
837
|
+
status: "pending"
|
|
838
|
+
};
|
|
839
|
+
this.records.set(this.key(e, t), r), this.emit({
|
|
840
|
+
type: "interaction_updated",
|
|
841
|
+
interaction: r
|
|
842
|
+
});
|
|
843
|
+
}
|
|
844
|
+
markRunTerminal(e, t) {
|
|
845
|
+
if (!(!e || !t)) {
|
|
846
|
+
this.terminalRuns.add(this.runKey(e, t));
|
|
847
|
+
for (let n of this.listAll(e)) {
|
|
848
|
+
if (n.runId !== t || d(n.status)) continue;
|
|
849
|
+
let r = {
|
|
850
|
+
...n,
|
|
851
|
+
status: "expired",
|
|
852
|
+
outcome: "expired",
|
|
853
|
+
resolvedAt: n.resolvedAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
854
|
+
responseSummary: n.responseSummary ?? "运行已结束"
|
|
855
|
+
};
|
|
856
|
+
this.records.set(this.key(e, n.interactionId), r), this.emit({
|
|
857
|
+
type: "interaction_resolved",
|
|
858
|
+
interaction: r
|
|
859
|
+
});
|
|
860
|
+
}
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
markSessionTerminal(e) {
|
|
864
|
+
if (!e) return;
|
|
865
|
+
let t = new Set(this.listAll(e).filter((e) => !d(e.status)).map((e) => e.runId).filter((e) => !!e));
|
|
866
|
+
for (let n of t) this.markRunTerminal(e, n);
|
|
867
|
+
for (let t of this.listAll(e)) {
|
|
868
|
+
if (t.runId || d(t.status)) continue;
|
|
869
|
+
let n = {
|
|
870
|
+
...t,
|
|
871
|
+
status: "expired",
|
|
872
|
+
outcome: "expired",
|
|
873
|
+
resolvedAt: t.resolvedAt ?? (/* @__PURE__ */ new Date()).toISOString(),
|
|
874
|
+
responseSummary: t.responseSummary ?? "运行已结束"
|
|
875
|
+
};
|
|
876
|
+
this.records.set(this.key(e, t.interactionId), n), this.emit({
|
|
877
|
+
type: "interaction_resolved",
|
|
878
|
+
interaction: n
|
|
879
|
+
});
|
|
880
|
+
}
|
|
881
|
+
}
|
|
882
|
+
remove(e, t) {
|
|
883
|
+
let n = this.key(e, t);
|
|
884
|
+
this.records.has(n) && (this.records.delete(n), this.emit({
|
|
885
|
+
type: "interaction_removed",
|
|
886
|
+
interactionId: t
|
|
887
|
+
}));
|
|
888
|
+
}
|
|
889
|
+
clearSession(e) {
|
|
890
|
+
for (let t of this.listAll(e)) this.remove(e, t.interactionId);
|
|
891
|
+
let t = `${e}\u0000`;
|
|
892
|
+
for (let e of this.terminalRuns) e.startsWith(t) && this.terminalRuns.delete(e);
|
|
893
|
+
}
|
|
894
|
+
}, Ze = {
|
|
895
|
+
approve: "approved",
|
|
896
|
+
reject: "rejected",
|
|
897
|
+
submit: "submitted",
|
|
898
|
+
cancel: "cancelled"
|
|
899
|
+
};
|
|
900
|
+
function W(e, t) {
|
|
901
|
+
return {
|
|
902
|
+
schema_version: 1,
|
|
903
|
+
command_id: `local-${t}`,
|
|
904
|
+
status: e,
|
|
905
|
+
message_id: null,
|
|
906
|
+
run_id: null,
|
|
907
|
+
accepted_seq: null,
|
|
908
|
+
error: null,
|
|
909
|
+
extensions: {}
|
|
910
|
+
};
|
|
911
|
+
}
|
|
912
|
+
var Qe = class {
|
|
913
|
+
store;
|
|
914
|
+
deps;
|
|
915
|
+
inFlight = /* @__PURE__ */ new Map();
|
|
916
|
+
constructor(e) {
|
|
917
|
+
this.deps = e, this.store = e.store || new Xe();
|
|
918
|
+
}
|
|
919
|
+
listPending(e) {
|
|
920
|
+
return this.store.listPending(e);
|
|
921
|
+
}
|
|
922
|
+
setInteractionV1Enabled(e) {
|
|
923
|
+
this.deps.interactionV1Enabled = e;
|
|
924
|
+
}
|
|
925
|
+
subscribe(e) {
|
|
926
|
+
return this.store.subscribe(e);
|
|
927
|
+
}
|
|
928
|
+
ingest(e) {
|
|
929
|
+
this.store.upsert(e);
|
|
930
|
+
}
|
|
931
|
+
ingestHistory(e) {
|
|
932
|
+
for (let t of e) this.store.upsert(t);
|
|
933
|
+
for (let [e] of this.inFlight) {
|
|
934
|
+
let t = this.findByInteractionId(e);
|
|
935
|
+
t && t.status !== "pending" && t.status !== "resolving" && this.inFlight.delete(e);
|
|
936
|
+
}
|
|
937
|
+
}
|
|
938
|
+
findByInteractionId(e) {
|
|
939
|
+
return this.store.all().find((t) => t.interactionId === e) || null;
|
|
940
|
+
}
|
|
941
|
+
async respond(e) {
|
|
942
|
+
let t = this.findByInteractionId(e.interactionId);
|
|
943
|
+
if (!t) return W("rejected", e.interactionId);
|
|
944
|
+
if (t.status === "resolved" || t.status === "cancelled" || t.status === "expired" || t.status === "resolving") return W("duplicate", e.interactionId);
|
|
945
|
+
let n = e.idempotencyKey || i(e.interactionId, e.expectedRevision), r = this.inFlight.get(e.interactionId);
|
|
946
|
+
if (r) return W(r === n ? "duplicate" : "rejected", e.interactionId);
|
|
947
|
+
this.inFlight.set(e.interactionId, n), this.store.markResolving(t.sessionId, e.interactionId);
|
|
948
|
+
let a = this.deps.interactionV1Enabled || t.source === "interaction_v1";
|
|
949
|
+
try {
|
|
950
|
+
if (!a && t.source === "responses" && this.deps.legacyResponsesApproval) return this.deps.legacyResponsesApproval(e.interactionId, e.action === "approve"), this.resolveLocal(t, e, n), W("accepted", e.interactionId);
|
|
951
|
+
if (!a && t.source === "agui" && this.deps.legacyAguiResume) return this.deps.legacyAguiResume(String(t.extensions.interrupt_id || e.interactionId), e.action === "cancel" ? "cancelled" : "resolved", e.response) ? (this.resolveLocal(t, e, n), W("accepted", e.interactionId)) : (this.store.revertToPending(t.sessionId, e.interactionId), W("rejected", e.interactionId));
|
|
952
|
+
let r = H(await this.deps.submitInteraction({
|
|
953
|
+
AgentId: this.deps.agentId,
|
|
954
|
+
SessionId: t.sessionId,
|
|
955
|
+
RunId: t.runId || "",
|
|
956
|
+
InteractionId: e.interactionId,
|
|
957
|
+
ExpectedRevision: e.expectedRevision,
|
|
958
|
+
Action: e.action,
|
|
959
|
+
Response: e.response,
|
|
960
|
+
IdempotencyKey: n
|
|
961
|
+
}));
|
|
962
|
+
return r.status === "accepted" || r.status === "duplicate" ? this.store.recordIdempotencyKey(t.sessionId, e.interactionId, n) : r.status === "queue_full" ? this.store.revertToPending(t.sessionId, e.interactionId) : this.store.markFailed(t.sessionId, e.interactionId, {
|
|
963
|
+
code: r.error?.code ?? String(r.status),
|
|
964
|
+
message: r.error?.message ?? `提交未通过(${r.status})`,
|
|
965
|
+
retryable: r.error?.retryable ?? !1
|
|
966
|
+
}), r;
|
|
967
|
+
} catch (n) {
|
|
968
|
+
let r = n instanceof Error ? n.message : String(n || "交互提交失败");
|
|
969
|
+
throw this.store.markFailed(t.sessionId, e.interactionId, {
|
|
970
|
+
code: "interaction_submit_failed",
|
|
971
|
+
message: r,
|
|
972
|
+
retryable: !0
|
|
973
|
+
}), n;
|
|
974
|
+
} finally {
|
|
975
|
+
this.inFlight.delete(e.interactionId);
|
|
976
|
+
}
|
|
977
|
+
}
|
|
978
|
+
resolveLocal(e, t, n) {
|
|
979
|
+
let r = t.action === "cancel" ? "cancelled" : "resolved";
|
|
980
|
+
this.store.resolveLocally(e.sessionId, t.interactionId, {
|
|
981
|
+
status: r,
|
|
982
|
+
outcome: Ze[t.action],
|
|
983
|
+
actor: "user",
|
|
984
|
+
responseSummary: p(t.action, t.response),
|
|
985
|
+
revision: t.expectedRevision
|
|
986
|
+
});
|
|
987
|
+
let i = this.store.get(e.sessionId, t.interactionId);
|
|
988
|
+
i && this.store.upsert({
|
|
989
|
+
...i,
|
|
990
|
+
extensions: {
|
|
991
|
+
...i.extensions,
|
|
992
|
+
idempotency_key: n
|
|
993
|
+
}
|
|
994
|
+
});
|
|
995
|
+
}
|
|
996
|
+
}, $e = new Set([
|
|
997
|
+
"approval",
|
|
998
|
+
"structured_input",
|
|
999
|
+
"plan_review",
|
|
1000
|
+
"custom"
|
|
1001
|
+
]), et = new Set([
|
|
1002
|
+
"pending",
|
|
1003
|
+
"resolving",
|
|
1004
|
+
"failed",
|
|
1005
|
+
"resolved",
|
|
1006
|
+
"cancelled",
|
|
1007
|
+
"expired"
|
|
1008
|
+
]), tt = new Set([
|
|
1009
|
+
"approved",
|
|
1010
|
+
"rejected",
|
|
1011
|
+
"submitted",
|
|
1012
|
+
"cancelled",
|
|
1013
|
+
"expired"
|
|
1014
|
+
]);
|
|
1015
|
+
function nt(e) {
|
|
1016
|
+
return typeof e == "object" && e && !Array.isArray(e) ? e : null;
|
|
1017
|
+
}
|
|
1018
|
+
function G(e) {
|
|
1019
|
+
return typeof e == "string" && e.length > 0 ? e : null;
|
|
1020
|
+
}
|
|
1021
|
+
function K(e) {
|
|
1022
|
+
let t = String(e.kind || "approval"), n = String(e.status || "pending"), r = String(e.outcome || ""), i = nt(e.presentation), a = i ? nt(i.a2ui) : null;
|
|
1023
|
+
return {
|
|
1024
|
+
interactionId: e.interactionId,
|
|
1025
|
+
sessionId: e.sessionId,
|
|
1026
|
+
runId: G(e.runId),
|
|
1027
|
+
kind: $e.has(t) ? t : "custom",
|
|
1028
|
+
title: typeof e.title == "string" && e.title ? e.title : "人工确认",
|
|
1029
|
+
message: typeof e.message == "string" && e.message ? e.message : "本次运行需要人工处理后才能继续。",
|
|
1030
|
+
requestSchema: nt(e.requestSchema),
|
|
1031
|
+
presentation: a ? { a2ui: {
|
|
1032
|
+
wireVersion: String(a.wire_version ?? a.wireVersion ?? ""),
|
|
1033
|
+
catalogDigest: String(a.catalog_digest ?? a.catalogDigest ?? ""),
|
|
1034
|
+
messages: Array.isArray(a.messages) ? a.messages.filter((e) => typeof e == "object") : []
|
|
1035
|
+
} } : null,
|
|
1036
|
+
status: et.has(n) ? n : "pending",
|
|
1037
|
+
revision: Number.isFinite(Number(e.revision)) && Number(e.revision) > 0 ? Number(e.revision) : 1,
|
|
1038
|
+
createdAt: G(e.createdAt) || (/* @__PURE__ */ new Date()).toISOString(),
|
|
1039
|
+
expiresAt: G(e.expiresAt),
|
|
1040
|
+
resolvedAt: G(e.resolvedAt),
|
|
1041
|
+
actor: G(e.actor),
|
|
1042
|
+
outcome: tt.has(r) ? r : null,
|
|
1043
|
+
responseSummary: G(e.responseSummary),
|
|
1044
|
+
source: e.source,
|
|
1045
|
+
extensions: e.extensions || {}
|
|
1046
|
+
};
|
|
1047
|
+
}
|
|
1048
|
+
//#endregion
|
|
1049
|
+
//#region src/core/interaction/adapters/session-events.ts
|
|
1050
|
+
var rt = new Set([
|
|
1051
|
+
"interaction.requested",
|
|
1052
|
+
"interaction_requested",
|
|
1053
|
+
"ksadk.interaction/v1.requested",
|
|
1054
|
+
"InteractionRequested",
|
|
1055
|
+
"approval.requested",
|
|
1056
|
+
"a2ui.interaction"
|
|
1057
|
+
]), it = new Set([
|
|
1058
|
+
"interaction.resolved",
|
|
1059
|
+
"interaction_resolved",
|
|
1060
|
+
"ksadk.interaction/v1.resolved",
|
|
1061
|
+
"InteractionResolved",
|
|
1062
|
+
"interaction.cancelled",
|
|
1063
|
+
"interaction.cancel",
|
|
1064
|
+
"interaction.expired",
|
|
1065
|
+
"approval.resolved",
|
|
1066
|
+
"a2ui.action"
|
|
1067
|
+
]);
|
|
1068
|
+
function q(e) {
|
|
1069
|
+
return typeof e == "object" && e && !Array.isArray(e) ? e : null;
|
|
1070
|
+
}
|
|
1071
|
+
function at(e, t) {
|
|
1072
|
+
let n = q(e);
|
|
1073
|
+
if (!n) return null;
|
|
1074
|
+
let r = String(n.event_type || n.EventType || "").trim();
|
|
1075
|
+
if (!r) return null;
|
|
1076
|
+
let i = q(n.payload ?? n.Content) || {}, a = q(i.interaction) || q(i.Interaction) || q(i.interaction_request) || i, o = String(a.interaction_id || a.interactionId || a.InteractionId || a.approval_id || a.approvalId || i.interaction_id || i.interactionId || i.InteractionId || i.approval_id || i.approvalId || "");
|
|
1077
|
+
if (!o) return null;
|
|
1078
|
+
let s = String(n.session_id || n.SessionId || a.session_id || a.sessionId || t || "");
|
|
1079
|
+
if (!s) return null;
|
|
1080
|
+
if (rt.has(r)) {
|
|
1081
|
+
let e = {
|
|
1082
|
+
...q(a.extensions) || {},
|
|
1083
|
+
...a.detail === void 0 ? {} : { detail: a.detail },
|
|
1084
|
+
...(a.call_id ?? a.callId) === void 0 ? {} : { call_id: a.call_id ?? a.callId }
|
|
1085
|
+
};
|
|
1086
|
+
return K({
|
|
1087
|
+
interactionId: o,
|
|
1088
|
+
sessionId: s,
|
|
1089
|
+
runId: a.run_id ?? a.runId ?? n.run_id ?? n.InvocationId,
|
|
1090
|
+
kind: r === "approval.requested" ? "approval" : a.kind ?? "approval",
|
|
1091
|
+
title: a.title,
|
|
1092
|
+
message: a.message ?? a.description ?? q(a.detail)?.command,
|
|
1093
|
+
requestSchema: a.request_schema ?? a.requestSchema ?? a.input_schema ?? a.inputSchema ?? a.RequestSchema,
|
|
1094
|
+
presentation: a.presentation,
|
|
1095
|
+
status: "pending",
|
|
1096
|
+
revision: a.revision ?? 1,
|
|
1097
|
+
createdAt: a.created_at ?? a.createdAt ?? n.timestamp ?? n.Timestamp,
|
|
1098
|
+
expiresAt: a.expires_at,
|
|
1099
|
+
source: "interaction_v1",
|
|
1100
|
+
extensions: e
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1103
|
+
if (it.has(r)) {
|
|
1104
|
+
let e = {
|
|
1105
|
+
...q(a.extensions) || {},
|
|
1106
|
+
...a.detail === void 0 ? {} : { detail: a.detail },
|
|
1107
|
+
...(a.call_id ?? a.callId) === void 0 ? {} : { call_id: a.call_id ?? a.callId }
|
|
1108
|
+
}, t = r === "interaction.cancelled" || r === "interaction.cancel" ? "cancelled" : r === "interaction.expired" ? "expired" : "resolved", c = String(i.outcome ?? a.outcome ?? a.status ?? "").toLowerCase(), l = String(i.action ?? a.action ?? i.name ?? a.name ?? "").toLowerCase(), u = c;
|
|
1109
|
+
u ||= l === "approve" ? "approved" : l === "reject" ? "rejected" : l === "submit" ? "submitted" : l === "cancel" || t === "cancelled" ? "cancelled" : t === "expired" ? "expired" : "submitted";
|
|
1110
|
+
let d = t === "resolved" ? "resolved" : t;
|
|
1111
|
+
return K({
|
|
1112
|
+
interactionId: o,
|
|
1113
|
+
sessionId: s,
|
|
1114
|
+
runId: a.run_id ?? a.runId ?? n.run_id ?? n.InvocationId,
|
|
1115
|
+
kind: a.kind,
|
|
1116
|
+
title: a.title,
|
|
1117
|
+
message: a.message,
|
|
1118
|
+
requestSchema: a.request_schema,
|
|
1119
|
+
presentation: a.presentation,
|
|
1120
|
+
status: d,
|
|
1121
|
+
revision: a.revision,
|
|
1122
|
+
createdAt: a.created_at ?? n.timestamp,
|
|
1123
|
+
expiresAt: a.expires_at,
|
|
1124
|
+
resolvedAt: i.resolved_at ?? i.resolvedAt ?? a.resolved_at ?? a.resolvedAt ?? n.timestamp ?? n.Timestamp,
|
|
1125
|
+
actor: i.actor ?? a.actor ?? n.actor_ref,
|
|
1126
|
+
outcome: u,
|
|
1127
|
+
responseSummary: i.response_summary ?? a.response_summary ?? (l ? `${l}` : void 0),
|
|
1128
|
+
source: "interaction_v1",
|
|
1129
|
+
extensions: e
|
|
1130
|
+
});
|
|
1131
|
+
}
|
|
1132
|
+
return null;
|
|
1133
|
+
}
|
|
1134
|
+
//#endregion
|
|
1135
|
+
//#region src/core/interaction/adapters/responses.ts
|
|
1136
|
+
function ot(e) {
|
|
1137
|
+
return !e.approvalRequestId || !e.sessionId ? null : K({
|
|
1138
|
+
interactionId: e.approvalRequestId,
|
|
1139
|
+
sessionId: e.sessionId,
|
|
1140
|
+
runId: e.runId ?? null,
|
|
1141
|
+
kind: "approval",
|
|
1142
|
+
title: e.name ? `审批:${e.name}` : "人工确认",
|
|
1143
|
+
message: e.message || "本次运行需要人工审批后才能继续。",
|
|
1144
|
+
requestSchema: e.requestSchema ?? null,
|
|
1145
|
+
status: "pending",
|
|
1146
|
+
revision: 1,
|
|
1147
|
+
source: "responses",
|
|
1148
|
+
extensions: {
|
|
1149
|
+
...e.approvalLevel ? { approval_level: e.approvalLevel } : {},
|
|
1150
|
+
...e.args ? { arguments: e.args } : {}
|
|
1151
|
+
}
|
|
1152
|
+
});
|
|
1153
|
+
}
|
|
1154
|
+
//#endregion
|
|
1155
|
+
//#region src/core/interaction/adapters/agui.ts
|
|
1156
|
+
function st(e) {
|
|
1157
|
+
return !e.interruptId || !e.sessionId ? null : K({
|
|
1158
|
+
interactionId: e.interruptId,
|
|
1159
|
+
sessionId: e.sessionId,
|
|
1160
|
+
runId: e.runId ?? null,
|
|
1161
|
+
kind: "approval",
|
|
1162
|
+
title: e.name ? `确认:${e.name}` : "人工确认",
|
|
1163
|
+
message: e.message || "本次运行需要人工确认后才能继续。",
|
|
1164
|
+
requestSchema: e.requestSchema ?? null,
|
|
1165
|
+
status: "pending",
|
|
1166
|
+
revision: 1,
|
|
1167
|
+
source: "agui",
|
|
1168
|
+
extensions: {
|
|
1169
|
+
interrupt_id: e.interruptId,
|
|
1170
|
+
...e.toolCallId ? { tool_call_id: e.toolCallId } : {},
|
|
1171
|
+
...e.reason ? { reason: e.reason } : {}
|
|
1172
|
+
}
|
|
1173
|
+
});
|
|
1174
|
+
}
|
|
1175
|
+
//#endregion
|
|
1176
|
+
//#region src/core/interaction/index.ts
|
|
1177
|
+
var ct = new Set([
|
|
1178
|
+
"completed",
|
|
1179
|
+
"failed",
|
|
1180
|
+
"error",
|
|
1181
|
+
"cancelled",
|
|
1182
|
+
"canceled",
|
|
1183
|
+
"aborted",
|
|
1184
|
+
"interrupted",
|
|
1185
|
+
"resume_failed"
|
|
1186
|
+
]), J = new Xe();
|
|
1187
|
+
function lt(e, t) {
|
|
1188
|
+
let n = at(e, t);
|
|
1189
|
+
n && J.upsert(n);
|
|
1190
|
+
let r = typeof e == "object" && e ? e : null, i = r ? j(r) : null;
|
|
1191
|
+
if (i !== null && ct.has(i)) {
|
|
1192
|
+
let e = typeof r?.Content == "object" && r.Content !== null ? r.Content : {}, n = typeof e.payload == "object" && e.payload !== null ? e.payload : {}, i = String(r?.SessionId || r?.session_id || t || ""), a = String(r?.InvocationId || r?.run_id || n.run_id || n.runId || "");
|
|
1193
|
+
J.markRunTerminal(i, a);
|
|
1194
|
+
}
|
|
1195
|
+
return n;
|
|
1196
|
+
}
|
|
1197
|
+
function ut(e) {
|
|
1198
|
+
let t = e.sessionId || "";
|
|
1199
|
+
if (!t || !e.approvalRequestId) return null;
|
|
1200
|
+
let n = e.protocol === "ag-ui" ? st({
|
|
1201
|
+
interruptId: e.approvalRequestId,
|
|
1202
|
+
sessionId: t,
|
|
1203
|
+
name: e.name,
|
|
1204
|
+
message: e.message,
|
|
1205
|
+
reason: e.approvalLevel
|
|
1206
|
+
}) : ot({
|
|
1207
|
+
approvalRequestId: e.approvalRequestId,
|
|
1208
|
+
sessionId: t,
|
|
1209
|
+
runId: e.runId,
|
|
1210
|
+
name: e.name,
|
|
1211
|
+
message: e.message,
|
|
1212
|
+
args: e.args,
|
|
1213
|
+
approvalLevel: e.approvalLevel
|
|
1214
|
+
});
|
|
1215
|
+
return n && J.upsert(n), n;
|
|
1216
|
+
}
|
|
1217
|
+
function dt(e) {
|
|
1218
|
+
let t = e.sessionId || "";
|
|
1219
|
+
return !t || !e.approvalRequestId ? null : lt({
|
|
1220
|
+
EventType: "approval.resolved",
|
|
1221
|
+
Content: {
|
|
1222
|
+
approvalId: e.approvalRequestId,
|
|
1223
|
+
revision: e.revision || 2,
|
|
1224
|
+
outcome: e.decision,
|
|
1225
|
+
actor: "user",
|
|
1226
|
+
resolvedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
1227
|
+
}
|
|
1228
|
+
}, t);
|
|
1229
|
+
}
|
|
1230
|
+
//#endregion
|
|
1231
|
+
//#region src/hooks/useInteractions.ts
|
|
1232
|
+
function ft(e) {
|
|
1233
|
+
let [t] = _e(() => new Qe({
|
|
1234
|
+
agentId: e.agentId,
|
|
1235
|
+
store: J,
|
|
1236
|
+
submitInteraction: (t) => e.api.submitInteraction({
|
|
1237
|
+
...t,
|
|
1238
|
+
AgentId: e.getAgentId?.() ?? t.AgentId
|
|
1239
|
+
}),
|
|
1240
|
+
interactionV1Enabled: e.interactionV1Enabled,
|
|
1241
|
+
legacyResponsesApproval: e.legacyResponsesApproval,
|
|
1242
|
+
legacyAguiResume: e.legacyAguiResume
|
|
1243
|
+
}));
|
|
1244
|
+
F(() => {
|
|
1245
|
+
t.setInteractionV1Enabled(e.interactionV1Enabled);
|
|
1246
|
+
}, [t, e.interactionV1Enabled]);
|
|
1247
|
+
let [n, r] = _e(0);
|
|
1248
|
+
return F(() => J.subscribe(() => r((e) => e + 1)), []), {
|
|
1249
|
+
client: t,
|
|
1250
|
+
pending: ge(() => e.currentSessionId ? J.listAll(e.currentSessionId).filter((e) => e.status === "pending" || e.status === "resolving" || e.status === "failed") : [], [n, e.currentSessionId]),
|
|
1251
|
+
records: ge(() => e.currentSessionId ? J.listAll(e.currentSessionId) : [], [n, e.currentSessionId]),
|
|
1252
|
+
respond: P((e) => t.respond(e), [t]),
|
|
1253
|
+
localCatalog: u
|
|
1254
|
+
};
|
|
1255
|
+
}
|
|
1256
|
+
var pt = .28;
|
|
1257
|
+
function mt(e, t = 768) {
|
|
1258
|
+
return Number(e) <= t;
|
|
1259
|
+
}
|
|
1260
|
+
function ht({ isMobile: e, desktopSidebarOpen: t, mobileSidebarOpen: n }) {
|
|
1261
|
+
return {
|
|
1262
|
+
desktopSidebarVisible: !e && !!t,
|
|
1263
|
+
mobileDrawerVisible: !!(e && n)
|
|
1264
|
+
};
|
|
1265
|
+
}
|
|
1266
|
+
function gt({ isMobile: e, viewportHeight: t, desktopMaxHeight: n = 160 }) {
|
|
1267
|
+
if (!e) return n;
|
|
1268
|
+
let r = Number(t);
|
|
1269
|
+
if (!Number.isFinite(r) || r <= 0) return n;
|
|
1270
|
+
let i = Math.floor(r * pt);
|
|
1271
|
+
return Math.max(120, Math.min(n, i));
|
|
1272
|
+
}
|
|
1273
|
+
//#endregion
|
|
1274
|
+
//#region src/hooks/useResponsiveViewport.ts
|
|
1275
|
+
function _t() {
|
|
1276
|
+
return typeof window > "u" ? 900 : Math.round(window.visualViewport?.height || window.innerHeight || 900);
|
|
1277
|
+
}
|
|
1278
|
+
function vt() {
|
|
1279
|
+
return typeof window > "u" ? {
|
|
1280
|
+
isMobile: !1,
|
|
1281
|
+
viewportHeight: 900
|
|
1282
|
+
} : {
|
|
1283
|
+
isMobile: mt(window.innerWidth, 768),
|
|
1284
|
+
viewportHeight: _t()
|
|
1285
|
+
};
|
|
1286
|
+
}
|
|
1287
|
+
function yt() {
|
|
1288
|
+
let [e, t] = _e(() => vt());
|
|
1289
|
+
return F(() => {
|
|
1290
|
+
if (typeof window > "u") return;
|
|
1291
|
+
let e = window.matchMedia("(max-width: 768px)"), n = () => {
|
|
1292
|
+
t(vt());
|
|
1293
|
+
};
|
|
1294
|
+
n();
|
|
1295
|
+
let r = typeof e.addEventListener == "function" ? (e.addEventListener("change", n), () => e.removeEventListener("change", n)) : (e.addListener(n), () => e.removeListener(n));
|
|
1296
|
+
return window.addEventListener("resize", n), window.visualViewport?.addEventListener("resize", n), () => {
|
|
1297
|
+
r(), window.removeEventListener("resize", n), window.visualViewport?.removeEventListener("resize", n);
|
|
1298
|
+
};
|
|
1299
|
+
}, []), e;
|
|
1300
|
+
}
|
|
1301
|
+
//#endregion
|
|
1302
|
+
//#region src/core/stream/responses-protocol.ts
|
|
1303
|
+
var bt = class {
|
|
1304
|
+
id = "responses";
|
|
1305
|
+
createState() {
|
|
1306
|
+
return oe();
|
|
1307
|
+
}
|
|
1308
|
+
parseItemOperations(e, t, n) {
|
|
1309
|
+
return g(e.eventName, e.data, t, n);
|
|
1310
|
+
}
|
|
1311
|
+
parse(e, t) {
|
|
1312
|
+
return pe({
|
|
1313
|
+
eventName: e.eventName,
|
|
1314
|
+
data: e.data,
|
|
1315
|
+
state: t
|
|
1316
|
+
}).map((e) => {
|
|
1317
|
+
if (e.type === "tool_upsert") {
|
|
1318
|
+
let { approvalRequestId: t, previousResponseId: n, serverLabel: r, ...i } = e;
|
|
1319
|
+
return {
|
|
1320
|
+
...i,
|
|
1321
|
+
extra: {
|
|
1322
|
+
...t ? { approvalRequestId: t } : {},
|
|
1323
|
+
...n ? { previousResponseId: n } : {},
|
|
1324
|
+
...r ? { serverLabel: r } : {},
|
|
1325
|
+
...t ? { approvalProtocol: "responses" } : {}
|
|
1326
|
+
}
|
|
1327
|
+
};
|
|
1328
|
+
}
|
|
1329
|
+
return e;
|
|
1330
|
+
});
|
|
1331
|
+
}
|
|
1332
|
+
};
|
|
1333
|
+
//#endregion
|
|
1334
|
+
//#region src/utils/stream-parsing.ts
|
|
1335
|
+
function xt(e) {
|
|
1336
|
+
return typeof e == "string" ? e : Array.isArray(e) ? e.map((e) => typeof e == "string" ? e : e && typeof e == "object" && "text" in e && typeof e.text == "string" ? e.text : "").join("") : "";
|
|
1337
|
+
}
|
|
1338
|
+
function St(e) {
|
|
1339
|
+
return e && typeof e == "object" ? e : {};
|
|
1340
|
+
}
|
|
1341
|
+
function Ct(e) {
|
|
1342
|
+
let t = St(e).choices, n = St(Array.isArray(t) ? t[0] : null), r = St(n.delta), i = St(n.message);
|
|
1343
|
+
return {
|
|
1344
|
+
content: xt(r.content),
|
|
1345
|
+
reasoning: xt(r.reasoning_content),
|
|
1346
|
+
finalText: xt(i.content)
|
|
1347
|
+
};
|
|
1348
|
+
}
|
|
1349
|
+
//#endregion
|
|
1350
|
+
//#region src/core/stream/chat-completions-protocol.ts
|
|
1351
|
+
var wt = class {
|
|
1352
|
+
id = "chat_completions";
|
|
1353
|
+
createState() {
|
|
1354
|
+
return {};
|
|
1355
|
+
}
|
|
1356
|
+
parse(e) {
|
|
1357
|
+
let t = e.data;
|
|
1358
|
+
if (typeof t != "object" || !t) return [];
|
|
1359
|
+
let n = Ct(t), r = [];
|
|
1360
|
+
return n.reasoning && r.push({
|
|
1361
|
+
type: "reasoning_delta",
|
|
1362
|
+
text: n.reasoning
|
|
1363
|
+
}), n.content && r.push({
|
|
1364
|
+
type: "text_delta",
|
|
1365
|
+
text: n.content
|
|
1366
|
+
}), n.finalText && r.push({
|
|
1367
|
+
type: "text_final",
|
|
1368
|
+
text: n.finalText
|
|
1369
|
+
}), r;
|
|
1370
|
+
}
|
|
1371
|
+
}, Tt = /* @__PURE__ */ new Map();
|
|
1372
|
+
Tt.set("responses", new bt()), Tt.set("chat_completions", new wt());
|
|
1373
|
+
function Et(e) {
|
|
1374
|
+
return Tt.get(e) || Tt.get("responses");
|
|
1375
|
+
}
|
|
1376
|
+
//#endregion
|
|
1377
|
+
//#region src/utils/stream-control.js
|
|
1378
|
+
function Dt(e = []) {
|
|
1379
|
+
return e.some((e) => {
|
|
1380
|
+
if (e?.type !== "terminal") return !1;
|
|
1381
|
+
let t = String(e.status || "").toLowerCase();
|
|
1382
|
+
return t === "failed" || t === "incomplete" || t === "cancelled";
|
|
1383
|
+
});
|
|
1384
|
+
}
|
|
1385
|
+
//#endregion
|
|
1386
|
+
//#region src/core/transport/sse-parser.ts
|
|
1387
|
+
function Y(e) {
|
|
1388
|
+
let t = [], n = e.trim();
|
|
1389
|
+
if (!n) return t;
|
|
1390
|
+
let r = n.split("\n");
|
|
1391
|
+
if (r.every((e) => e.startsWith(":"))) return t.push({
|
|
1392
|
+
eventName: "__ping__",
|
|
1393
|
+
data: null
|
|
1394
|
+
}), t;
|
|
1395
|
+
let i = "message", a = [];
|
|
1396
|
+
for (let e of r) e.startsWith(":") || (e.startsWith("event:") ? i = e.substring(6).trim() || "message" : e.startsWith("data:") && a.push(e.substring(5).trim()));
|
|
1397
|
+
let o = a.join("\n").trim();
|
|
1398
|
+
if (o === "[DONE]") return t.push({
|
|
1399
|
+
eventName: "__done__",
|
|
1400
|
+
data: null
|
|
1401
|
+
}), t;
|
|
1402
|
+
if (!o) return t;
|
|
1403
|
+
try {
|
|
1404
|
+
let e = JSON.parse(o);
|
|
1405
|
+
t.push({
|
|
1406
|
+
eventName: i,
|
|
1407
|
+
data: e
|
|
1408
|
+
});
|
|
1409
|
+
} catch {
|
|
1410
|
+
t.push({
|
|
1411
|
+
eventName: i,
|
|
1412
|
+
data: o
|
|
1413
|
+
});
|
|
1414
|
+
}
|
|
1415
|
+
return t;
|
|
1416
|
+
}
|
|
1417
|
+
function X(e) {
|
|
1418
|
+
let t = e.split("\n\n");
|
|
1419
|
+
return {
|
|
1420
|
+
chunks: t,
|
|
1421
|
+
remainder: t.pop() || ""
|
|
1422
|
+
};
|
|
1423
|
+
}
|
|
1424
|
+
//#endregion
|
|
1425
|
+
//#region src/utils/error.ts
|
|
1426
|
+
function Ot(e) {
|
|
1427
|
+
return e instanceof Error ? e.message : String(e);
|
|
1428
|
+
}
|
|
1429
|
+
//#endregion
|
|
1430
|
+
//#region src/utils/model-options.js
|
|
1431
|
+
var kt = new Set([
|
|
1432
|
+
"auto",
|
|
1433
|
+
"enabled",
|
|
1434
|
+
"disabled"
|
|
1435
|
+
]);
|
|
1436
|
+
function At(e) {
|
|
1437
|
+
let t = String(e || "").trim().toLowerCase();
|
|
1438
|
+
return kt.has(t) ? t : "auto";
|
|
1439
|
+
}
|
|
1440
|
+
function jt(e) {
|
|
1441
|
+
let t = At(e);
|
|
1442
|
+
if (t === "disabled") return { thinking: { type: "disabled" } };
|
|
1443
|
+
if (t === "enabled") return { thinking: { type: "enabled" } };
|
|
1444
|
+
}
|
|
1445
|
+
var Mt = 1280;
|
|
1446
|
+
function Nt(e) {
|
|
1447
|
+
let { apiFormats: t } = e;
|
|
1448
|
+
return t.includes("responses") ? "responses" : t.includes("chat_completions") ? "chat_completions" : "responses";
|
|
1449
|
+
}
|
|
1450
|
+
function Pt(e, t, n) {
|
|
1451
|
+
let r = Math.min(Mt, Math.max(420, t - n - 360));
|
|
1452
|
+
return Math.min(Math.max(e, 420), r);
|
|
1453
|
+
}
|
|
1454
|
+
//#endregion
|
|
1455
|
+
//#region src/core/run/agui.ts
|
|
1456
|
+
function Ft(e) {
|
|
1457
|
+
return String(e.type || "").toUpperCase();
|
|
1458
|
+
}
|
|
1459
|
+
function It(e, t) {
|
|
1460
|
+
return String(e.messageId || t);
|
|
1461
|
+
}
|
|
1462
|
+
function Lt(e, t) {
|
|
1463
|
+
return String(e.runId || t);
|
|
1464
|
+
}
|
|
1465
|
+
function Rt(e) {
|
|
1466
|
+
return e && typeof e == "object" && !Array.isArray(e) ? e : {};
|
|
1467
|
+
}
|
|
1468
|
+
function zt(e) {
|
|
1469
|
+
if (Array.isArray(e)) return c(e.filter((e) => !!(e && typeof e == "object")));
|
|
1470
|
+
if (e && typeof e == "object") {
|
|
1471
|
+
let t = e.a2ui_operations;
|
|
1472
|
+
return Array.isArray(t) ? c(t.filter((e) => !!(e && typeof e == "object"))) : [e];
|
|
1473
|
+
}
|
|
1474
|
+
return [];
|
|
1475
|
+
}
|
|
1476
|
+
function Bt(e) {
|
|
1477
|
+
return e == null || e === "" ? "" : typeof e == "string" ? e : JSON.stringify(e, null, 2);
|
|
1478
|
+
}
|
|
1479
|
+
var Vt = class {
|
|
1480
|
+
agent;
|
|
1481
|
+
onEvent;
|
|
1482
|
+
tools = /* @__PURE__ */ new Map();
|
|
1483
|
+
runMessageIds = /* @__PURE__ */ new Map();
|
|
1484
|
+
pendingByRun = /* @__PURE__ */ new Map();
|
|
1485
|
+
activeRunId = "";
|
|
1486
|
+
constructor(e) {
|
|
1487
|
+
this.agent = new ye({
|
|
1488
|
+
url: e.url,
|
|
1489
|
+
agentId: e.agentId,
|
|
1490
|
+
threadId: e.threadId,
|
|
1491
|
+
fetch: e.fetch
|
|
1492
|
+
}), this.onEvent = e.onEvent;
|
|
1493
|
+
}
|
|
1494
|
+
async run(e, t, n) {
|
|
1495
|
+
return this.activeRunId = t, this.agent.addMessage({
|
|
1496
|
+
id: `${t}:user`,
|
|
1497
|
+
role: "user",
|
|
1498
|
+
content: e
|
|
1499
|
+
}), this.execute({
|
|
1500
|
+
runId: t,
|
|
1501
|
+
abortController: this.controllerFor(n)
|
|
1502
|
+
});
|
|
1503
|
+
}
|
|
1504
|
+
async resume(e, t) {
|
|
1505
|
+
let n = this.agent.pendingInterrupts.length > 0 ? this.agent.pendingInterrupts : this.pendingByRun.get(this.activeRunId) || [], r = be(n.length > 0 ? n : [{
|
|
1506
|
+
id: t.interruptId,
|
|
1507
|
+
reason: "approval"
|
|
1508
|
+
}], { [t.interruptId]: t.status === "resolved" ? {
|
|
1509
|
+
status: "resolved",
|
|
1510
|
+
payload: t.payload
|
|
1511
|
+
} : { status: "cancelled" } });
|
|
1512
|
+
if (!r.some((e) => e.interruptId === t.interruptId)) throw Error(`AG-UI interrupt ${t.interruptId} is not pending`);
|
|
1513
|
+
return this.activeRunId = e, this.execute({
|
|
1514
|
+
runId: e,
|
|
1515
|
+
resume: r
|
|
1516
|
+
});
|
|
1517
|
+
}
|
|
1518
|
+
abort() {
|
|
1519
|
+
this.agent.abortRun();
|
|
1520
|
+
}
|
|
1521
|
+
controllerFor(e) {
|
|
1522
|
+
if (!e) return;
|
|
1523
|
+
let t = new AbortController();
|
|
1524
|
+
return e.aborted && t.abort(e.reason), e.addEventListener("abort", () => t.abort(e.reason), { once: !0 }), t;
|
|
1525
|
+
}
|
|
1526
|
+
async execute(e) {
|
|
1527
|
+
let t = "completed", n = await this.agent.runAgent({
|
|
1528
|
+
runId: e.runId,
|
|
1529
|
+
context: [m],
|
|
1530
|
+
forwardedProps: { injectA2UITool: !0 },
|
|
1531
|
+
resume: e.resume,
|
|
1532
|
+
abortController: e.abortController
|
|
1533
|
+
}, {
|
|
1534
|
+
onEvent: ({ event: n }) => {
|
|
1535
|
+
let r = this.project(n, e.runId);
|
|
1536
|
+
r.status && (t = r.status), r.events.forEach((e) => this.onEvent(e));
|
|
1537
|
+
},
|
|
1538
|
+
onRunFinishedEvent: ({ outcome: e }) => {
|
|
1539
|
+
t = e === "interrupt" ? "interrupted" : "completed";
|
|
1540
|
+
},
|
|
1541
|
+
onRunErrorEvent: () => {
|
|
1542
|
+
t = "failed";
|
|
1543
|
+
}
|
|
1544
|
+
}), r = this.agent.pendingInterrupts.slice();
|
|
1545
|
+
return r.length > 0 && (this.pendingByRun.set(e.runId, r), t = "interrupted"), {
|
|
1546
|
+
status: t,
|
|
1547
|
+
result: n
|
|
1548
|
+
};
|
|
1549
|
+
}
|
|
1550
|
+
project(e, t) {
|
|
1551
|
+
let n = Ft(e), r = Lt(e, t), i = It(e, `${r}:assistant`), a = e, o = [];
|
|
1552
|
+
if (n === "RUN_STARTED") o.push({
|
|
1553
|
+
type: "activity",
|
|
1554
|
+
phase: "AG-UI 运行已创建",
|
|
1555
|
+
status: "running",
|
|
1556
|
+
countEvent: !1
|
|
1557
|
+
});
|
|
1558
|
+
else if (n === "TEXT_MESSAGE_START") this.runMessageIds.set(r, i), o.push({
|
|
1559
|
+
type: "assistant_message_created",
|
|
1560
|
+
messageId: i
|
|
1561
|
+
});
|
|
1562
|
+
else if (n === "TEXT_MESSAGE_CONTENT") o.push({
|
|
1563
|
+
type: "text_delta",
|
|
1564
|
+
messageId: this.runMessageIds.get(r) || i,
|
|
1565
|
+
delta: String(a.delta || "")
|
|
1566
|
+
});
|
|
1567
|
+
else if (n === "TEXT_MESSAGE_END") o.push({
|
|
1568
|
+
type: "activity",
|
|
1569
|
+
phase: "生成回复内容",
|
|
1570
|
+
status: "running"
|
|
1571
|
+
});
|
|
1572
|
+
else if (n === "REASONING_MESSAGE_CONTENT") o.push({
|
|
1573
|
+
type: "reasoning_delta",
|
|
1574
|
+
messageId: this.runMessageIds.get(r) || i,
|
|
1575
|
+
delta: String(a.delta || "")
|
|
1576
|
+
});
|
|
1577
|
+
else if (n === "TOOL_CALL_START") {
|
|
1578
|
+
let e = String(a.toolCallId || "tool");
|
|
1579
|
+
this.tools.set(e, {
|
|
1580
|
+
name: String(a.toolCallName || "tool"),
|
|
1581
|
+
args: ""
|
|
1582
|
+
}), o.push({
|
|
1583
|
+
type: "tool_upsert",
|
|
1584
|
+
messageId: this.runMessageIds.get(r) || `${r}:assistant`,
|
|
1585
|
+
name: String(a.toolCallName || "tool"),
|
|
1586
|
+
args: "",
|
|
1587
|
+
status: "running"
|
|
1588
|
+
});
|
|
1589
|
+
} else if (n === "TOOL_CALL_ARGS") {
|
|
1590
|
+
let e = String(a.toolCallId || "tool"), t = this.tools.get(e) || {
|
|
1591
|
+
name: "tool",
|
|
1592
|
+
args: ""
|
|
1593
|
+
};
|
|
1594
|
+
t.args += String(a.delta || ""), this.tools.set(e, t), o.push({
|
|
1595
|
+
type: "tool_upsert",
|
|
1596
|
+
messageId: this.runMessageIds.get(r) || `${r}:assistant`,
|
|
1597
|
+
name: t.name,
|
|
1598
|
+
args: t.args,
|
|
1599
|
+
status: "running"
|
|
1600
|
+
});
|
|
1601
|
+
} else if (n === "TOOL_CALL_RESULT") {
|
|
1602
|
+
let e = String(a.toolCallId || "tool"), t = this.tools.get(e) || {
|
|
1603
|
+
name: "tool",
|
|
1604
|
+
args: ""
|
|
1605
|
+
};
|
|
1606
|
+
o.push({
|
|
1607
|
+
type: "tool_result",
|
|
1608
|
+
messageId: this.runMessageIds.get(r) || `${r}:assistant`,
|
|
1609
|
+
name: t.name,
|
|
1610
|
+
output: String(a.content || "")
|
|
1611
|
+
});
|
|
1612
|
+
} else if (n === "ACTIVITY_SNAPSHOT") {
|
|
1613
|
+
let e = a.content, t = Rt(e), n = String(t.surfaceId || t.surface_id || a.messageId || "default");
|
|
1614
|
+
o.push({
|
|
1615
|
+
type: "agui_activity",
|
|
1616
|
+
messageId: this.runMessageIds.get(r) || i,
|
|
1617
|
+
surfaceId: n,
|
|
1618
|
+
messages: zt(t.content ?? e)
|
|
1619
|
+
});
|
|
1620
|
+
} else if (n === "RUN_FINISHED") {
|
|
1621
|
+
let e = Rt(a.outcome);
|
|
1622
|
+
if (String(e.type || "").toLowerCase() === "interrupt") {
|
|
1623
|
+
let t = Array.isArray(e.interrupts) ? e.interrupts : [];
|
|
1624
|
+
this.pendingByRun.set(r, t);
|
|
1625
|
+
for (let e of t) {
|
|
1626
|
+
let t = Rt(e.metadata), n = e.toolCallId ? this.tools.get(e.toolCallId) : void 0, i = String(t.tool_name || t.toolName || n?.name || e.reason || "人工确认"), a = Bt(t.arguments ?? t.tool_args ?? t.args ?? n?.args);
|
|
1627
|
+
o.push({
|
|
1628
|
+
type: "approval_requested",
|
|
1629
|
+
messageId: this.runMessageIds.get(r) || `${r}:assistant`,
|
|
1630
|
+
approvalRequestId: e.id,
|
|
1631
|
+
protocol: "ag-ui",
|
|
1632
|
+
name: i,
|
|
1633
|
+
args: a,
|
|
1634
|
+
...e.message ? { message: e.message } : {},
|
|
1635
|
+
...t.approval_level ? { approvalLevel: String(t.approval_level) } : {}
|
|
1636
|
+
});
|
|
1637
|
+
}
|
|
1638
|
+
return o.push({
|
|
1639
|
+
type: "activity",
|
|
1640
|
+
phase: "等待人工确认",
|
|
1641
|
+
status: "waiting"
|
|
1642
|
+
}), {
|
|
1643
|
+
events: o,
|
|
1644
|
+
status: "interrupted"
|
|
1645
|
+
};
|
|
1646
|
+
}
|
|
1647
|
+
o.push({
|
|
1648
|
+
type: "terminal",
|
|
1649
|
+
status: "completed"
|
|
1650
|
+
});
|
|
1651
|
+
} else if (n === "RUN_ERROR") return o.push({
|
|
1652
|
+
type: "error",
|
|
1653
|
+
error: /* @__PURE__ */ Error("AG-UI 运行失败")
|
|
1654
|
+
}), {
|
|
1655
|
+
events: o,
|
|
1656
|
+
status: "failed"
|
|
1657
|
+
};
|
|
1658
|
+
return { events: o };
|
|
1659
|
+
}
|
|
1660
|
+
}, Ht = new Set([
|
|
1661
|
+
"completed",
|
|
1662
|
+
"failed",
|
|
1663
|
+
"error",
|
|
1664
|
+
"cancelled",
|
|
1665
|
+
"canceled",
|
|
1666
|
+
"aborted",
|
|
1667
|
+
"interrupted",
|
|
1668
|
+
"resume_failed"
|
|
1669
|
+
]);
|
|
1670
|
+
function Ut(e) {
|
|
1671
|
+
if (e.EventType !== "run_status") return null;
|
|
1672
|
+
let t = String(e.Content?.status || "").trim().toLowerCase();
|
|
1673
|
+
return Ht.has(t) ? t : null;
|
|
1674
|
+
}
|
|
1675
|
+
function Wt(e, t) {
|
|
1676
|
+
let n = String(t?.type || e || "").trim();
|
|
1677
|
+
if (n === "response.created") return {
|
|
1678
|
+
phase: "运行已创建",
|
|
1679
|
+
status: "running"
|
|
1680
|
+
};
|
|
1681
|
+
if (n === "response.in_progress") return {
|
|
1682
|
+
phase: "等待运行时输出",
|
|
1683
|
+
status: "waiting"
|
|
1684
|
+
};
|
|
1685
|
+
if (n === "response.output_item.added") {
|
|
1686
|
+
let e = t?.item;
|
|
1687
|
+
return e?.type === "function_call" ? {
|
|
1688
|
+
phase: `调用工具 ${e.name || "tool"}`,
|
|
1689
|
+
status: "running"
|
|
1690
|
+
} : String(e?.type || "").includes("reasoning") ? {
|
|
1691
|
+
phase: "生成思考过程",
|
|
1692
|
+
status: "running"
|
|
1693
|
+
} : {
|
|
1694
|
+
phase: "生成回复内容",
|
|
1695
|
+
status: "running"
|
|
1696
|
+
};
|
|
1697
|
+
}
|
|
1698
|
+
return n.includes("reasoning") ? {
|
|
1699
|
+
phase: "生成思考过程",
|
|
1700
|
+
status: "running"
|
|
1701
|
+
} : n.includes("function_call") || n === "response.tool_call" ? {
|
|
1702
|
+
phase: "调用工具",
|
|
1703
|
+
status: "running"
|
|
1704
|
+
} : n.includes("tool_result") ? {
|
|
1705
|
+
phase: "收到工具结果",
|
|
1706
|
+
status: "running"
|
|
1707
|
+
} : n.includes("output_text") || n.includes("content_part") ? {
|
|
1708
|
+
phase: "生成回复内容",
|
|
1709
|
+
status: "running"
|
|
1710
|
+
} : n === "response.completed" ? {
|
|
1711
|
+
phase: "运行完成",
|
|
1712
|
+
status: "completed"
|
|
1713
|
+
} : n === "response.failed" ? {
|
|
1714
|
+
phase: "运行失败",
|
|
1715
|
+
status: "failed"
|
|
1716
|
+
} : n === "response.incomplete" ? {
|
|
1717
|
+
phase: "运行中断",
|
|
1718
|
+
status: "failed"
|
|
1719
|
+
} : null;
|
|
1720
|
+
}
|
|
1721
|
+
function Gt() {
|
|
1722
|
+
let e = globalThis.crypto;
|
|
1723
|
+
return e && typeof e.randomUUID == "function" ? `run_${e.randomUUID()}` : `run_${Date.now()}_${Math.random().toString(16).slice(2)}`;
|
|
1724
|
+
}
|
|
1725
|
+
var Kt = {
|
|
1726
|
+
idle: [
|
|
1727
|
+
"creating-session",
|
|
1728
|
+
"uploading-files",
|
|
1729
|
+
"connecting",
|
|
1730
|
+
"error"
|
|
1731
|
+
],
|
|
1732
|
+
"creating-session": [
|
|
1733
|
+
"uploading-files",
|
|
1734
|
+
"connecting",
|
|
1735
|
+
"error",
|
|
1736
|
+
"idle"
|
|
1737
|
+
],
|
|
1738
|
+
"uploading-files": [
|
|
1739
|
+
"connecting",
|
|
1740
|
+
"error",
|
|
1741
|
+
"idle"
|
|
1742
|
+
],
|
|
1743
|
+
connecting: [
|
|
1744
|
+
"streaming",
|
|
1745
|
+
"error",
|
|
1746
|
+
"idle"
|
|
1747
|
+
],
|
|
1748
|
+
streaming: [
|
|
1749
|
+
"completing",
|
|
1750
|
+
"stopping",
|
|
1751
|
+
"recovering",
|
|
1752
|
+
"error"
|
|
1753
|
+
],
|
|
1754
|
+
stopping: ["cancelled", "idle"],
|
|
1755
|
+
completing: ["idle"],
|
|
1756
|
+
recovering: [
|
|
1757
|
+
"streaming",
|
|
1758
|
+
"error",
|
|
1759
|
+
"idle"
|
|
1760
|
+
],
|
|
1761
|
+
error: ["connecting", "idle"],
|
|
1762
|
+
cancelled: ["idle"]
|
|
1763
|
+
}, qt = class {
|
|
1764
|
+
_stage = "idle";
|
|
1765
|
+
listeners = /* @__PURE__ */ new Set();
|
|
1766
|
+
abortController = null;
|
|
1767
|
+
activeCompactionId = null;
|
|
1768
|
+
activeSessionId = null;
|
|
1769
|
+
aguiClient = null;
|
|
1770
|
+
aguiThreadSessionId = null;
|
|
1771
|
+
api;
|
|
1772
|
+
controlStateValue = { phase: "idle" };
|
|
1773
|
+
lastControlCommand = null;
|
|
1774
|
+
config = {
|
|
1775
|
+
agentId: "default-agent",
|
|
1776
|
+
apiFormats: ["responses"],
|
|
1777
|
+
agentFramework: "",
|
|
1778
|
+
selectedModel: "",
|
|
1779
|
+
thinkingMode: "auto"
|
|
1780
|
+
};
|
|
1781
|
+
constructor(e) {
|
|
1782
|
+
this.api = e;
|
|
1783
|
+
}
|
|
1784
|
+
get stage() {
|
|
1785
|
+
return this._stage;
|
|
1786
|
+
}
|
|
1787
|
+
get controlState() {
|
|
1788
|
+
return this.controlStateValue;
|
|
1789
|
+
}
|
|
1790
|
+
async submitControl(e) {
|
|
1791
|
+
this.lastControlCommand = e;
|
|
1792
|
+
let t;
|
|
1793
|
+
try {
|
|
1794
|
+
t = H(await this.api.submitControl(e));
|
|
1795
|
+
} catch (e) {
|
|
1796
|
+
if (e instanceof V) return this.controlStateValue = {
|
|
1797
|
+
phase: "contract_mismatch",
|
|
1798
|
+
error: null,
|
|
1799
|
+
receipt: null
|
|
1800
|
+
}, this.emit({
|
|
1801
|
+
type: "system_message",
|
|
1802
|
+
content: "【系统提示】控制指令响应不符合 agent-kernel/v1 合同,已停止变更本地状态。"
|
|
1803
|
+
}), this.controlStateValue;
|
|
1804
|
+
throw e;
|
|
1805
|
+
}
|
|
1806
|
+
return this.controlStateValue = this.controlStateFor(t, e), t.status === "accepted" || t.status === "duplicate" ? this.emit({
|
|
1807
|
+
type: "activity",
|
|
1808
|
+
phase: "指令已入队,等待运行时处理",
|
|
1809
|
+
status: "waiting",
|
|
1810
|
+
countEvent: !1
|
|
1811
|
+
}) : t.status === "queue_full" ? this.emit({
|
|
1812
|
+
type: "system_message",
|
|
1813
|
+
content: "【系统提示】指令队列已满,可点击重试;重试将复用同一幂等键。"
|
|
1814
|
+
}) : t.status === "unsupported" ? this.emit({
|
|
1815
|
+
type: "system_message",
|
|
1816
|
+
content: `【系统提示】运行时不支持该操作:${t.error?.message || ""}`
|
|
1817
|
+
}) : t.status === "persistence_uncertain" ? this.emit({
|
|
1818
|
+
type: "system_message",
|
|
1819
|
+
content: "【系统提示】指令提交结果待确认,正在查询状态…"
|
|
1820
|
+
}) : t.status === "rejected" && this.emit({
|
|
1821
|
+
type: "system_message",
|
|
1822
|
+
content: `【系统提示】指令被拒绝:${t.error?.message || ""}`
|
|
1823
|
+
}), this.controlStateValue;
|
|
1824
|
+
}
|
|
1825
|
+
controlStateFor(e, t) {
|
|
1826
|
+
switch (e.status) {
|
|
1827
|
+
case "accepted":
|
|
1828
|
+
case "duplicate": return {
|
|
1829
|
+
phase: "queued",
|
|
1830
|
+
receipt: e,
|
|
1831
|
+
error: null
|
|
1832
|
+
};
|
|
1833
|
+
case "queue_full": return {
|
|
1834
|
+
phase: "retryable",
|
|
1835
|
+
retryKey: t.idempotency_key,
|
|
1836
|
+
receipt: e,
|
|
1837
|
+
error: e.error
|
|
1838
|
+
};
|
|
1839
|
+
case "unsupported": return {
|
|
1840
|
+
phase: "unsupported",
|
|
1841
|
+
receipt: e,
|
|
1842
|
+
error: e.error
|
|
1843
|
+
};
|
|
1844
|
+
case "persistence_uncertain": return {
|
|
1845
|
+
phase: "confirming",
|
|
1846
|
+
retryKey: t.idempotency_key,
|
|
1847
|
+
receipt: e,
|
|
1848
|
+
error: e.error
|
|
1849
|
+
};
|
|
1850
|
+
default: return {
|
|
1851
|
+
phase: "rejected",
|
|
1852
|
+
receipt: e,
|
|
1853
|
+
error: e.error
|
|
1854
|
+
};
|
|
1855
|
+
}
|
|
1856
|
+
}
|
|
1857
|
+
async retryControl() {
|
|
1858
|
+
let e = this.lastControlCommand;
|
|
1859
|
+
if (!e || this.controlStateValue.phase !== "retryable" && this.controlStateValue.phase !== "confirming") return this.controlStateValue;
|
|
1860
|
+
if (this.controlStateValue.phase === "confirming") try {
|
|
1861
|
+
await this.api.getAgentStatus();
|
|
1862
|
+
} catch (e) {
|
|
1863
|
+
console.warn("[RunEngine] status query after persistence_uncertain failed:", e);
|
|
1864
|
+
}
|
|
1865
|
+
return this.submitControl(e);
|
|
1866
|
+
}
|
|
1867
|
+
updateConfig(e) {
|
|
1868
|
+
this.config = {
|
|
1869
|
+
...e,
|
|
1870
|
+
apiFormats: [...e.apiFormats]
|
|
1871
|
+
};
|
|
1872
|
+
}
|
|
1873
|
+
emit(e) {
|
|
1874
|
+
let t = "sessionId" in e ? e : {
|
|
1875
|
+
...e,
|
|
1876
|
+
sessionId: this.activeSessionId
|
|
1877
|
+
};
|
|
1878
|
+
for (let e of this.listeners) e(t);
|
|
1879
|
+
}
|
|
1880
|
+
getAguiClient(e) {
|
|
1881
|
+
let t = this.config.hostedChatTransport;
|
|
1882
|
+
return t?.Protocol !== "ag-ui" || !t.Endpoint ? null : ((!this.aguiClient || this.aguiThreadSessionId !== e) && (this.aguiClient = new Vt({
|
|
1883
|
+
url: t.Endpoint,
|
|
1884
|
+
agentId: this.config.agentId,
|
|
1885
|
+
threadId: e,
|
|
1886
|
+
onEvent: (e) => this.emit(e)
|
|
1887
|
+
}), this.aguiThreadSessionId = e), this.aguiClient);
|
|
1888
|
+
}
|
|
1889
|
+
setStage(e) {
|
|
1890
|
+
let t = Kt[this._stage];
|
|
1891
|
+
t && !t.includes(e) && console.warn(`[RunEngine] Invalid transition: ${this._stage} → ${e}`), this._stage = e, this.emit({
|
|
1892
|
+
type: "stage_changed",
|
|
1893
|
+
stage: e
|
|
1894
|
+
});
|
|
1895
|
+
}
|
|
1896
|
+
subscribe(e) {
|
|
1897
|
+
return this.listeners.add(e), () => {
|
|
1898
|
+
this.listeners.delete(e);
|
|
1899
|
+
};
|
|
1900
|
+
}
|
|
1901
|
+
start(e) {
|
|
1902
|
+
if (this._stage !== "idle") return !1;
|
|
1903
|
+
this.abortController = new AbortController();
|
|
1904
|
+
let t = e.responsesInput !== void 0;
|
|
1905
|
+
return (async () => {
|
|
1906
|
+
let n = e.sessionId || null;
|
|
1907
|
+
try {
|
|
1908
|
+
n ||= await this.createSession(e), n ||= `default-session-${Date.now()}`, this.activeSessionId = n;
|
|
1909
|
+
let r = Gt();
|
|
1910
|
+
l.getState().setCurrentRunId(r), l.getState().setActiveInvocationId(r), l.getState().setLastSeqId(0);
|
|
1911
|
+
let i = t ? null : await this.getConversationBootstrap(n);
|
|
1912
|
+
if (i && this.config.conversationClient) {
|
|
1913
|
+
let t = await this.buildCanonicalConversationInput(i, n, r, e);
|
|
1914
|
+
this.setStage("connecting"), this.emit({
|
|
1915
|
+
type: "activity",
|
|
1916
|
+
phase: "连接会话运行时",
|
|
1917
|
+
status: "connecting",
|
|
1918
|
+
countEvent: !1
|
|
1919
|
+
}), this.setStage("streaming"), this.emit({
|
|
1920
|
+
type: "activity",
|
|
1921
|
+
phase: "等待会话输出",
|
|
1922
|
+
status: "waiting",
|
|
1923
|
+
countEvent: !1
|
|
1924
|
+
});
|
|
1925
|
+
let a = await this.config.conversationClient.streamTurn({
|
|
1926
|
+
bootstrap: i,
|
|
1927
|
+
input: t,
|
|
1928
|
+
signal: this.abortController?.signal,
|
|
1929
|
+
onUpdate: (e) => {
|
|
1930
|
+
e.runId && (l.getState().setCurrentRunId(e.runId), l.getState().setActiveInvocationId(e.runId)), this.emit({
|
|
1931
|
+
type: "conversation_snapshot",
|
|
1932
|
+
result: e,
|
|
1933
|
+
sessionId: n
|
|
1934
|
+
});
|
|
1935
|
+
}
|
|
1936
|
+
});
|
|
1937
|
+
if (a.presentation.terminalStatus === "failed") {
|
|
1938
|
+
this.setStage("error"), this.emit({
|
|
1939
|
+
type: "activity",
|
|
1940
|
+
phase: "运行失败",
|
|
1941
|
+
status: "failed",
|
|
1942
|
+
countEvent: !1
|
|
1943
|
+
});
|
|
1944
|
+
return;
|
|
1945
|
+
}
|
|
1946
|
+
this.setStage("completing"), this.emit({
|
|
1947
|
+
type: "activity",
|
|
1948
|
+
phase: "运行完成",
|
|
1949
|
+
status: "completed",
|
|
1950
|
+
countEvent: !1
|
|
1951
|
+
}), this.emit({
|
|
1952
|
+
type: "stream_ended",
|
|
1953
|
+
sessionId: n,
|
|
1954
|
+
runId: a.runId
|
|
1955
|
+
});
|
|
1956
|
+
return;
|
|
1957
|
+
}
|
|
1958
|
+
let a = await this.uploadFiles(e, t);
|
|
1959
|
+
this.setStage("connecting"), this.emit({
|
|
1960
|
+
type: "activity",
|
|
1961
|
+
phase: "连接运行时",
|
|
1962
|
+
status: "connecting",
|
|
1963
|
+
countEvent: !1
|
|
1964
|
+
});
|
|
1965
|
+
let o = t ? "responses" : Nt({
|
|
1966
|
+
agentFramework: this.config.agentFramework,
|
|
1967
|
+
apiFormats: this.config.apiFormats
|
|
1968
|
+
}), s = Et(o), c = s.createState(), u = this.config.hostedChatTransport, d = !t && !e.executionMode && e.attachments.length === 0 && u?.Protocol === "ag-ui" ? this.getAguiClient(n) : null;
|
|
1969
|
+
if (d && u) {
|
|
1970
|
+
this.setStage("streaming"), this.emit({
|
|
1971
|
+
type: "activity",
|
|
1972
|
+
phase: "等待 AG-UI 输出",
|
|
1973
|
+
status: "waiting",
|
|
1974
|
+
countEvent: !1
|
|
1975
|
+
});
|
|
1976
|
+
let t = await d.run(e.text, r, this.abortController?.signal);
|
|
1977
|
+
if (t.status === "interrupted") {
|
|
1978
|
+
this.setStage("completing"), this.emit({ type: "stream_ended" });
|
|
1979
|
+
return;
|
|
1980
|
+
}
|
|
1981
|
+
if (t.status !== "completed") {
|
|
1982
|
+
this.setStage("error"), this.emit({
|
|
1983
|
+
type: "activity",
|
|
1984
|
+
phase: "AG-UI 运行失败",
|
|
1985
|
+
status: "failed",
|
|
1986
|
+
countEvent: !1
|
|
1987
|
+
});
|
|
1988
|
+
return;
|
|
1989
|
+
}
|
|
1990
|
+
this.setStage("completing"), this.emit({
|
|
1991
|
+
type: "activity",
|
|
1992
|
+
phase: "运行完成",
|
|
1993
|
+
status: "completed",
|
|
1994
|
+
countEvent: !1
|
|
1995
|
+
}), this.emit({ type: "stream_ended" });
|
|
1996
|
+
return;
|
|
1997
|
+
}
|
|
1998
|
+
let f = this.buildRequestBody(n, o, t, e, a, r), p = await this.api.runAgent(f, { signal: this.abortController?.signal });
|
|
1999
|
+
this.setStage("streaming"), this.emit({
|
|
2000
|
+
type: "activity",
|
|
2001
|
+
phase: "等待首个输出",
|
|
2002
|
+
status: "waiting",
|
|
2003
|
+
countEvent: !1
|
|
2004
|
+
});
|
|
2005
|
+
let m = `msg-${Date.now()}`, h = await te(p), g;
|
|
2006
|
+
if (g = h.receipt ? await this.consumeKernelRunEvents(n, r, h.receipt, m) : await this.consumeStream(h.stream, s, c, m, r), g.terminalStatus === "cancelled") {
|
|
2007
|
+
this.setStage("stopping"), l.getState().stopActivity("运行时已取消本次执行。"), this.emit({
|
|
2008
|
+
type: "activity",
|
|
2009
|
+
phase: "运行已取消",
|
|
2010
|
+
status: "stopped",
|
|
2011
|
+
countEvent: !1
|
|
2012
|
+
}), this.setStage("cancelled");
|
|
2013
|
+
return;
|
|
2014
|
+
}
|
|
2015
|
+
if (g.terminalStatus && g.terminalStatus !== "completed") {
|
|
2016
|
+
this.setStage("error"), this.emit({
|
|
2017
|
+
type: "activity",
|
|
2018
|
+
phase: g.terminalStatus === "incomplete" ? "运行中断" : "运行失败",
|
|
2019
|
+
status: "failed",
|
|
2020
|
+
countEvent: !1
|
|
2021
|
+
});
|
|
2022
|
+
return;
|
|
2023
|
+
}
|
|
2024
|
+
this.setStage("completing"), this.emit({
|
|
2025
|
+
type: "activity",
|
|
2026
|
+
phase: "运行完成",
|
|
2027
|
+
status: "completed",
|
|
2028
|
+
countEvent: !1
|
|
2029
|
+
}), this.emit({ type: "stream_ended" });
|
|
2030
|
+
} catch (e) {
|
|
2031
|
+
if (this.failCompaction(), !(e instanceof DOMException && e.name === "AbortError" || e instanceof N && e.code === "conversation_aborted")) if (console.error("[RunEngine] start() error:", e), e instanceof TypeError && e.message.includes("fetch")) {
|
|
2032
|
+
this.setStage("recovering"), this.emit({
|
|
2033
|
+
type: "activity",
|
|
2034
|
+
phase: "网络异常,尝试重连",
|
|
2035
|
+
status: "waiting",
|
|
2036
|
+
countEvent: !1
|
|
2037
|
+
});
|
|
2038
|
+
let e = l.getState().lastSeqId, t = l.getState().activeInvocationId;
|
|
2039
|
+
if (e > 0 && t && this.activeSessionId) try {
|
|
2040
|
+
this.resumeRun({
|
|
2041
|
+
sessionId: this.activeSessionId,
|
|
2042
|
+
invocationId: t,
|
|
2043
|
+
afterSeqId: e
|
|
2044
|
+
});
|
|
2045
|
+
return;
|
|
2046
|
+
} catch (e) {
|
|
2047
|
+
console.warn("[RunEngine] afterSeqId resume failed, fall back to error:", e);
|
|
2048
|
+
}
|
|
2049
|
+
} else e instanceof R && e.code === 429 ? (this.setStage("error"), this.emit({
|
|
2050
|
+
type: "rate_limited",
|
|
2051
|
+
message: e.message || "请求过于频繁,请稍后重试",
|
|
2052
|
+
sessionId: n
|
|
2053
|
+
})) : (this.setStage("error"), this.emit({
|
|
2054
|
+
type: "error",
|
|
2055
|
+
error: e instanceof Error ? e : Error(String(e))
|
|
2056
|
+
}));
|
|
2057
|
+
} finally {
|
|
2058
|
+
l.getState().setSessionStreaming(n, !1), this.setStage("idle"), this.activeCompactionId = null, this.activeSessionId = null, e.onSettled?.(n);
|
|
2059
|
+
}
|
|
2060
|
+
})(), !0;
|
|
2061
|
+
}
|
|
2062
|
+
stop() {
|
|
2063
|
+
if (this._stage === "idle") return;
|
|
2064
|
+
this.setStage("stopping");
|
|
2065
|
+
let e = l.getState().currentRunId;
|
|
2066
|
+
e && this.activeSessionId && this.api.cancelRun(this.config.agentId, this.activeSessionId, e).catch((e) => {
|
|
2067
|
+
console.warn("[RunEngine] cancelRun on stop failed:", e);
|
|
2068
|
+
}), this.abortController?.abort(), this.aguiClient?.abort(), l.getState().stopActivity(e ? "已向运行时发送取消请求;如果当前框架只支持协作式取消,后台可能会在下一个安全点停止。" : void 0), l.getState().setCurrentRunId(""), this.setStage("cancelled"), this.emit({
|
|
2069
|
+
type: "system_message",
|
|
2070
|
+
content: e ? "已请求取消本次运行。" : "已停止接收本次输出;如果运行时不支持取消,后台执行可能仍会继续。"
|
|
2071
|
+
});
|
|
2072
|
+
}
|
|
2073
|
+
disconnect() {
|
|
2074
|
+
this._stage !== "idle" && (this.abortController?.abort(), this.aguiClient?.abort(), l.getState().setSessionStreaming(this.activeSessionId, !1), l.getState().clearActivity(), this._stage = "idle", this.activeCompactionId = null, this.activeSessionId = null);
|
|
2075
|
+
}
|
|
2076
|
+
async cancelRemote(e) {
|
|
2077
|
+
try {
|
|
2078
|
+
this.activeSessionId && await this.api.cancelRun(this.config.agentId, this.activeSessionId, e);
|
|
2079
|
+
} catch (e) {
|
|
2080
|
+
console.warn("[RunEngine] cancelRemote failed:", e);
|
|
2081
|
+
}
|
|
2082
|
+
this.stop();
|
|
2083
|
+
}
|
|
2084
|
+
resumeRun(e) {
|
|
2085
|
+
this.abortController?.abort(), this.abortController = new AbortController(), this.activeSessionId = e.sessionId, l.getState().setCurrentRunId(e.invocationId), this.setStage("connecting"), this.emit({
|
|
2086
|
+
type: "activity",
|
|
2087
|
+
phase: "恢复运行事件订阅",
|
|
2088
|
+
status: "connecting",
|
|
2089
|
+
countEvent: !1
|
|
2090
|
+
}), (async () => {
|
|
2091
|
+
let t = null;
|
|
2092
|
+
try {
|
|
2093
|
+
let n = await this.api.subscribeRunEvents({
|
|
2094
|
+
sessionId: e.sessionId,
|
|
2095
|
+
invocationId: e.invocationId,
|
|
2096
|
+
afterSeqId: e.afterSeqId
|
|
2097
|
+
}, { signal: this.abortController?.signal });
|
|
2098
|
+
this.setStage("streaming"), this.emit({
|
|
2099
|
+
type: "activity",
|
|
2100
|
+
phase: "等待恢复事件",
|
|
2101
|
+
status: "waiting",
|
|
2102
|
+
countEvent: !1
|
|
2103
|
+
});
|
|
2104
|
+
let r = n.getReader(), i = new TextDecoder(), a = "";
|
|
2105
|
+
for (;;) {
|
|
2106
|
+
let { value: e, done: n } = await r.read();
|
|
2107
|
+
if (n) break;
|
|
2108
|
+
a += i.decode(e, { stream: !0 });
|
|
2109
|
+
let { chunks: o, remainder: s } = X(a);
|
|
2110
|
+
a = s;
|
|
2111
|
+
for (let e of o) {
|
|
2112
|
+
if (!e.trim()) continue;
|
|
2113
|
+
let n = Y(e);
|
|
2114
|
+
for (let e of n) e.eventName === "__done__" || e.eventName === "__ping__" || (this.emit({
|
|
2115
|
+
type: "activity",
|
|
2116
|
+
phase: "收到恢复事件",
|
|
2117
|
+
status: "running"
|
|
2118
|
+
}), this.emit({
|
|
2119
|
+
type: "stream_event",
|
|
2120
|
+
event: e.data
|
|
2121
|
+
}), t = Ut(e.data) || t);
|
|
2122
|
+
}
|
|
2123
|
+
}
|
|
2124
|
+
this.setStage("completing"), t === "cancelled" || t === "canceled" || t === "aborted" ? this.emit({
|
|
2125
|
+
type: "activity",
|
|
2126
|
+
phase: "后台长任务已取消",
|
|
2127
|
+
status: "stopped",
|
|
2128
|
+
countEvent: !1
|
|
2129
|
+
}) : t === "interrupted" ? this.emit({
|
|
2130
|
+
type: "activity",
|
|
2131
|
+
phase: "后台长任务已中断",
|
|
2132
|
+
status: "stopped",
|
|
2133
|
+
countEvent: !1
|
|
2134
|
+
}) : t === "failed" || t === "error" ? this.emit({
|
|
2135
|
+
type: "activity",
|
|
2136
|
+
phase: "后台长任务失败",
|
|
2137
|
+
status: "failed",
|
|
2138
|
+
countEvent: !1
|
|
2139
|
+
}) : t === "resume_failed" ? this.emit({
|
|
2140
|
+
type: "activity",
|
|
2141
|
+
phase: "后台长任务恢复失败",
|
|
2142
|
+
status: "failed",
|
|
2143
|
+
countEvent: !1
|
|
2144
|
+
}) : this.emit({
|
|
2145
|
+
type: "activity",
|
|
2146
|
+
phase: "后台长任务已完成",
|
|
2147
|
+
status: "completed",
|
|
2148
|
+
countEvent: !1
|
|
2149
|
+
}), this.emit({ type: "stream_ended" }), e.onSessionReloadNeeded?.();
|
|
2150
|
+
} catch (e) {
|
|
2151
|
+
e instanceof DOMException && e.name === "AbortError" || console.error("Failed to subscribe to run events:", e);
|
|
2152
|
+
} finally {
|
|
2153
|
+
l.getState().setSessionStreaming(e.sessionId, !1), l.getState().setCurrentRunId(""), this.setStage("idle"), this.activeSessionId = null;
|
|
2154
|
+
}
|
|
2155
|
+
})();
|
|
2156
|
+
}
|
|
2157
|
+
resumeCheckpoint(e) {
|
|
2158
|
+
if (this._stage !== "idle") return !1;
|
|
2159
|
+
this.abortController = new AbortController(), this.activeSessionId = e.sessionId;
|
|
2160
|
+
let t = Gt();
|
|
2161
|
+
return l.getState().setCurrentRunId(t), (async () => {
|
|
2162
|
+
try {
|
|
2163
|
+
if (this.setStage("connecting"), this.emit({
|
|
2164
|
+
type: "activity",
|
|
2165
|
+
source: "restore",
|
|
2166
|
+
phase: "从 checkpoint 恢复运行",
|
|
2167
|
+
status: "connecting",
|
|
2168
|
+
countEvent: !1
|
|
2169
|
+
}), this.config.checkpointResumePreviewEnabled) try {
|
|
2170
|
+
let t = (await this.api.previewCheckpointResume({
|
|
2171
|
+
agentId: this.config.agentId,
|
|
2172
|
+
sessionId: e.sessionId,
|
|
2173
|
+
runId: e.runId,
|
|
2174
|
+
checkpointId: e.checkpointId
|
|
2175
|
+
}, { signal: this.abortController?.signal })).Preview?.Risk?.Level || "unknown";
|
|
2176
|
+
this.emit({
|
|
2177
|
+
type: "activity",
|
|
2178
|
+
source: "restore",
|
|
2179
|
+
phase: `恢复预览完成:${String(t)} 风险`,
|
|
2180
|
+
status: "waiting",
|
|
2181
|
+
countEvent: !1
|
|
2182
|
+
});
|
|
2183
|
+
} catch (e) {
|
|
2184
|
+
console.warn("[RunEngine] checkpoint resume preview failed:", e);
|
|
2185
|
+
}
|
|
2186
|
+
let n = await this.api.resumeRun({
|
|
2187
|
+
agentId: this.config.agentId,
|
|
2188
|
+
sessionId: e.sessionId,
|
|
2189
|
+
runId: e.runId,
|
|
2190
|
+
checkpointId: e.checkpointId,
|
|
2191
|
+
resumeAttemptId: e.resumeAttemptId,
|
|
2192
|
+
invocationId: t
|
|
2193
|
+
}, { signal: this.abortController?.signal }), r = Et("responses"), i = r.createState();
|
|
2194
|
+
this.setStage("streaming"), this.emit({
|
|
2195
|
+
type: "activity",
|
|
2196
|
+
source: "restore",
|
|
2197
|
+
phase: "等待恢复输出",
|
|
2198
|
+
status: "waiting",
|
|
2199
|
+
countEvent: !1
|
|
2200
|
+
});
|
|
2201
|
+
let a = `msg-${Date.now()}`, o = await this.consumeStream(n, r, i, a, t);
|
|
2202
|
+
if (o.terminalStatus && o.terminalStatus !== "completed") {
|
|
2203
|
+
if (o.terminalStatus === "cancelled") {
|
|
2204
|
+
this.setStage("stopping"), l.getState().stopActivity("运行时已取消本次恢复。"), this.emit({
|
|
2205
|
+
type: "activity",
|
|
2206
|
+
source: "restore",
|
|
2207
|
+
phase: "恢复已取消",
|
|
2208
|
+
status: "stopped",
|
|
2209
|
+
countEvent: !1
|
|
2210
|
+
}), this.setStage("cancelled");
|
|
2211
|
+
return;
|
|
2212
|
+
}
|
|
2213
|
+
this.setStage("error"), this.emit({
|
|
2214
|
+
type: "activity",
|
|
2215
|
+
source: "restore",
|
|
2216
|
+
phase: o.terminalStatus === "incomplete" ? "恢复中断" : "恢复失败",
|
|
2217
|
+
status: "failed",
|
|
2218
|
+
countEvent: !1
|
|
2219
|
+
});
|
|
2220
|
+
return;
|
|
2221
|
+
}
|
|
2222
|
+
this.setStage("completing"), this.emit({
|
|
2223
|
+
type: "activity",
|
|
2224
|
+
source: "restore",
|
|
2225
|
+
phase: "恢复完成",
|
|
2226
|
+
status: "completed",
|
|
2227
|
+
countEvent: !1
|
|
2228
|
+
}), this.emit({ type: "stream_ended" });
|
|
2229
|
+
} catch (e) {
|
|
2230
|
+
e instanceof DOMException && e.name === "AbortError" || (console.error("[RunEngine] resumeCheckpoint() error:", e), this.setStage("error"), this.emit({
|
|
2231
|
+
type: "error",
|
|
2232
|
+
error: e instanceof Error ? e : Error(String(e))
|
|
2233
|
+
}));
|
|
2234
|
+
} finally {
|
|
2235
|
+
l.getState().setSessionStreaming(e.sessionId, !1), l.getState().setCurrentRunId(""), this.setStage("idle"), this.activeSessionId = null, e.onSettled?.(e.sessionId);
|
|
2236
|
+
}
|
|
2237
|
+
})(), !0;
|
|
2238
|
+
}
|
|
2239
|
+
resumeAguiInterrupt(e) {
|
|
2240
|
+
if (this._stage !== "idle") return !1;
|
|
2241
|
+
let t = e.sessionId || this.aguiThreadSessionId;
|
|
2242
|
+
if (!t) return !1;
|
|
2243
|
+
let n = this.getAguiClient(t);
|
|
2244
|
+
if (!n) return !1;
|
|
2245
|
+
this.activeSessionId = t;
|
|
2246
|
+
let r = Gt();
|
|
2247
|
+
return l.getState().setCurrentRunId(r), this.setStage("connecting"), this.emit({
|
|
2248
|
+
type: "activity",
|
|
2249
|
+
phase: "提交人工确认",
|
|
2250
|
+
status: "connecting",
|
|
2251
|
+
countEvent: !1
|
|
2252
|
+
}), (async () => {
|
|
2253
|
+
try {
|
|
2254
|
+
this.setStage("streaming");
|
|
2255
|
+
let t = await n.resume(r, {
|
|
2256
|
+
interruptId: e.interruptId,
|
|
2257
|
+
status: e.status,
|
|
2258
|
+
payload: e.payload
|
|
2259
|
+
}), i = e.payload && typeof e.payload == "object" ? e.payload : {}, a = String(i.decision || i.type || "").toLowerCase();
|
|
2260
|
+
if (this.emit({
|
|
2261
|
+
type: "approval_resolved",
|
|
2262
|
+
approvalRequestId: e.interruptId,
|
|
2263
|
+
decision: e.status === "cancelled" ? "cancelled" : a === "reject" || a === "rejected" ? "rejected" : "approved"
|
|
2264
|
+
}), t.status === "interrupted") {
|
|
2265
|
+
this.setStage("completing"), this.emit({ type: "stream_ended" });
|
|
2266
|
+
return;
|
|
2267
|
+
}
|
|
2268
|
+
if (t.status !== "completed") {
|
|
2269
|
+
this.setStage("error"), this.emit({
|
|
2270
|
+
type: "activity",
|
|
2271
|
+
phase: "人工确认恢复失败",
|
|
2272
|
+
status: "failed",
|
|
2273
|
+
countEvent: !1
|
|
2274
|
+
});
|
|
2275
|
+
return;
|
|
2276
|
+
}
|
|
2277
|
+
this.setStage("completing"), this.emit({
|
|
2278
|
+
type: "activity",
|
|
2279
|
+
phase: "运行完成",
|
|
2280
|
+
status: "completed",
|
|
2281
|
+
countEvent: !1
|
|
2282
|
+
}), this.emit({ type: "stream_ended" });
|
|
2283
|
+
} catch (e) {
|
|
2284
|
+
this.setStage("error"), this.emit({
|
|
2285
|
+
type: "error",
|
|
2286
|
+
error: e instanceof Error ? e : Error(String(e))
|
|
2287
|
+
});
|
|
2288
|
+
} finally {
|
|
2289
|
+
l.getState().setCurrentRunId(""), this.setStage("idle"), this.activeSessionId = null, e.onSettled?.(t);
|
|
2290
|
+
}
|
|
2291
|
+
})(), !0;
|
|
2292
|
+
}
|
|
2293
|
+
async createSession(e) {
|
|
2294
|
+
this.setStage("creating-session");
|
|
2295
|
+
try {
|
|
2296
|
+
let t = (await this.api.createSession(this.config.agentId, { signal: this.abortController?.signal })).SessionId || null;
|
|
2297
|
+
return t && (e.onSessionCreated?.(t), e.onSessionUpsert?.(t)), t;
|
|
2298
|
+
} catch (e) {
|
|
2299
|
+
return e instanceof DOMException && e.name === "AbortError" || console.error("Failed to create session:", e), null;
|
|
2300
|
+
}
|
|
2301
|
+
}
|
|
2302
|
+
async getConversationBootstrap(e) {
|
|
2303
|
+
let t = this.config.conversationClient;
|
|
2304
|
+
if (!t) return null;
|
|
2305
|
+
try {
|
|
2306
|
+
return await t.getSurface(this.config.agentId, e, { signal: this.abortController?.signal });
|
|
2307
|
+
} catch (e) {
|
|
2308
|
+
if (e instanceof N && e.code === "conversation_http_error" && e.status === 404) return null;
|
|
2309
|
+
throw e;
|
|
2310
|
+
}
|
|
2311
|
+
}
|
|
2312
|
+
async buildCanonicalConversationInput(e, t, n, r) {
|
|
2313
|
+
let i = [], a = r.text.trim();
|
|
2314
|
+
a && i.push({
|
|
2315
|
+
kind: "text",
|
|
2316
|
+
text: a
|
|
2317
|
+
});
|
|
2318
|
+
for (let t of r.attachments) {
|
|
2319
|
+
let n = t.type.startsWith("image/") ? "attachment.image" : "attachment.file";
|
|
2320
|
+
if (!D(e.surface, n)) throw new N("conversation_input_unsupported", "Conversation attachment is not declared by the active surface.", { capability: n });
|
|
2321
|
+
}
|
|
2322
|
+
r.attachments.length > 0 && i.push(...await this.uploadCanonicalAttachments(r.attachments));
|
|
2323
|
+
let o = At(this.config.thinkingMode), s = O({
|
|
2324
|
+
inputId: `input:${n}`,
|
|
2325
|
+
sessionId: t,
|
|
2326
|
+
idempotencyKey: `conversation:${n}`,
|
|
2327
|
+
parts: i,
|
|
2328
|
+
...this.config.selectedModel && D(e.surface, "model.select") ? { modelRef: this.config.selectedModel } : {},
|
|
2329
|
+
...o !== "auto" && D(e.surface, "reasoning.effort") ? { reasoning: o } : {},
|
|
2330
|
+
extensions: {
|
|
2331
|
+
...this.config.permissionMode && D(e.surface, "approval") ? { "ksadk.approval": this.config.permissionMode } : {},
|
|
2332
|
+
...r.executionMode === "plan" ? { "ksadk.collaboration": "plan" } : {},
|
|
2333
|
+
...r.executionMode === "goal" && a ? { "ksadk.goal": a } : {}
|
|
2334
|
+
}
|
|
2335
|
+
});
|
|
2336
|
+
return ae(e.surface, s);
|
|
2337
|
+
}
|
|
2338
|
+
async uploadCanonicalAttachments(e) {
|
|
2339
|
+
this.setStage("uploading-files");
|
|
2340
|
+
let t = [];
|
|
2341
|
+
for (let n of e) {
|
|
2342
|
+
if (n.size > 100 * 1024 * 1024) throw new N("conversation_input_unsupported", "Conversation attachment exceeds the Hosted UI upload limit.", { capability: n.type.startsWith("image/") ? "attachment.image" : "attachment.file" });
|
|
2343
|
+
let e = new FormData();
|
|
2344
|
+
e.append("file", n), e.append("AgentId", this.config.agentId);
|
|
2345
|
+
let r;
|
|
2346
|
+
try {
|
|
2347
|
+
r = await this.api.uploadFile(e, { signal: this.abortController?.signal });
|
|
2348
|
+
} catch (e) {
|
|
2349
|
+
throw new N("conversation_http_error", "Conversation attachment upload failed.", { cause: e });
|
|
2350
|
+
}
|
|
2351
|
+
let i = r?.FileData?.fileUri;
|
|
2352
|
+
if (!i) throw new N("conversation_contract_mismatch", "Conversation attachment upload did not return an attachment reference.");
|
|
2353
|
+
t.push({
|
|
2354
|
+
kind: "attachment",
|
|
2355
|
+
attachmentRef: i,
|
|
2356
|
+
mediaType: r.FileData.mimeType || n.type || "application/octet-stream",
|
|
2357
|
+
name: r.FileData.displayName || n.name
|
|
2358
|
+
});
|
|
2359
|
+
}
|
|
2360
|
+
return t;
|
|
2361
|
+
}
|
|
2362
|
+
async uploadFiles(e, t) {
|
|
2363
|
+
let n = [];
|
|
2364
|
+
if (t || e.attachments.length === 0) return n;
|
|
2365
|
+
this.setStage("uploading-files");
|
|
2366
|
+
for (let t of e.attachments) {
|
|
2367
|
+
if (t.size > 100 * 1024 * 1024) {
|
|
2368
|
+
this.emit({
|
|
2369
|
+
type: "system_message",
|
|
2370
|
+
content: `【系统提示】文件 ${t.name} 超过 100MB 限制,未发送。`
|
|
2371
|
+
});
|
|
2372
|
+
continue;
|
|
2373
|
+
}
|
|
2374
|
+
if (t.type.startsWith("image/")) {
|
|
2375
|
+
try {
|
|
2376
|
+
n.push({
|
|
2377
|
+
type: "input_image",
|
|
2378
|
+
image_url: await this.imageFileToDataUrl(t)
|
|
2379
|
+
});
|
|
2380
|
+
} catch (e) {
|
|
2381
|
+
this.emit({
|
|
2382
|
+
type: "system_message",
|
|
2383
|
+
content: `【系统提示】图片 ${t.name} 读取失败,原因: ${Ot(e)}`
|
|
2384
|
+
});
|
|
2385
|
+
}
|
|
2386
|
+
continue;
|
|
2387
|
+
}
|
|
2388
|
+
let e = new FormData();
|
|
2389
|
+
e.append("file", t), e.append("AgentId", this.config.agentId);
|
|
2390
|
+
try {
|
|
2391
|
+
let r = await this.api.uploadFile(e, { signal: this.abortController?.signal });
|
|
2392
|
+
r?.FileData?.fileUri && n.push({
|
|
2393
|
+
type: "input_file",
|
|
2394
|
+
filename: r.FileData.displayName || t.name,
|
|
2395
|
+
file_url: r.FileData.fileUri
|
|
2396
|
+
});
|
|
2397
|
+
} catch (e) {
|
|
2398
|
+
this.emit({
|
|
2399
|
+
type: "system_message",
|
|
2400
|
+
content: `【系统提示】文件 ${t.name} 上传失败,原因: ${Ot(e)}`
|
|
2401
|
+
});
|
|
2402
|
+
}
|
|
2403
|
+
}
|
|
2404
|
+
return n;
|
|
2405
|
+
}
|
|
2406
|
+
async imageFileToDataUrl(e) {
|
|
2407
|
+
let t = new Uint8Array(await e.arrayBuffer()), n = "", r = 32768;
|
|
2408
|
+
for (let e = 0; e < t.length; e += r) n += String.fromCharCode(...t.subarray(e, e + r));
|
|
2409
|
+
return `data:${e.type || "application/octet-stream"};base64,${btoa(n)}`;
|
|
2410
|
+
}
|
|
2411
|
+
buildRequestBody(e, t, n, r, i, a) {
|
|
2412
|
+
let o = this.buildExecutionMetadata(r), s = {
|
|
2413
|
+
AgentId: this.config.agentId,
|
|
2414
|
+
SessionId: e,
|
|
2415
|
+
InvocationId: a,
|
|
2416
|
+
Stream: !0,
|
|
2417
|
+
ApiFormat: t,
|
|
2418
|
+
Model: this.config.selectedModel || void 0,
|
|
2419
|
+
ModelMetadata: this.config.selectedModelMetadata || void 0,
|
|
2420
|
+
ModelOptions: jt(At(this.config.thinkingMode)),
|
|
2421
|
+
Metadata: { agentengine: {
|
|
2422
|
+
tool_approval_mode: this.config.permissionMode || "risk",
|
|
2423
|
+
...o
|
|
2424
|
+
} }
|
|
2425
|
+
};
|
|
2426
|
+
if (n) s.ResponsesInput = r.responsesInput, s.PreviousResponseId = r.previousResponseId;
|
|
2427
|
+
else {
|
|
2428
|
+
let e = [{
|
|
2429
|
+
type: "input_text",
|
|
2430
|
+
text: r.text.trim()
|
|
2431
|
+
}];
|
|
2432
|
+
e.push(...i), t === "responses" && (s.ResponsesInput = [{
|
|
2433
|
+
role: "user",
|
|
2434
|
+
content: e
|
|
2435
|
+
}]), s.Messages = [{
|
|
2436
|
+
role: "user",
|
|
2437
|
+
content: e
|
|
2438
|
+
}];
|
|
2439
|
+
}
|
|
2440
|
+
return s;
|
|
2441
|
+
}
|
|
2442
|
+
buildExecutionMetadata(e) {
|
|
2443
|
+
let t = e.executionMode;
|
|
2444
|
+
if (!t) return {};
|
|
2445
|
+
let n = this.config.runtimeCapabilityMatrix?.[t];
|
|
2446
|
+
if (!n?.supported || n.mode === "unavailable") return {};
|
|
2447
|
+
if (t === "plan") return { collaboration_mode: "plan" };
|
|
2448
|
+
if (t === "goal") {
|
|
2449
|
+
let t = e.text.trim();
|
|
2450
|
+
return t ? {
|
|
2451
|
+
collaboration_mode: "default",
|
|
2452
|
+
goal_objective: t
|
|
2453
|
+
} : {};
|
|
2454
|
+
}
|
|
2455
|
+
return {};
|
|
2456
|
+
}
|
|
2457
|
+
async consumeKernelRunEvents(e, t, n, r) {
|
|
2458
|
+
if (n.status === "rejected" || n.status === "unsupported") return this.setStage("error"), this.emit({
|
|
2459
|
+
type: "error",
|
|
2460
|
+
error: Error(n.error?.message || `运行提交被拒绝(${n.status})`)
|
|
2461
|
+
}), { terminalStatus: "failed" };
|
|
2462
|
+
if (n.status === "queue_full") return this.setStage("error"), this.emit({
|
|
2463
|
+
type: "rate_limited",
|
|
2464
|
+
message: "运行队列已满,请稍后重试"
|
|
2465
|
+
}), { terminalStatus: "failed" };
|
|
2466
|
+
n.status === "persistence_uncertain" && this.emit({
|
|
2467
|
+
type: "system_message",
|
|
2468
|
+
content: "提交结果未知,正在从事件流确认运行状态…"
|
|
2469
|
+
}), this.emit({
|
|
2470
|
+
type: "assistant_message_created",
|
|
2471
|
+
messageId: r,
|
|
2472
|
+
invocationId: t
|
|
2473
|
+
}), this.emit({
|
|
2474
|
+
type: "activity",
|
|
2475
|
+
phase: "已提交运行,订阅事件流",
|
|
2476
|
+
status: "running",
|
|
2477
|
+
countEvent: !1
|
|
2478
|
+
});
|
|
2479
|
+
let i = new M(e), a, o = 0;
|
|
2480
|
+
for (; a === void 0 && o < 6;) try {
|
|
2481
|
+
let t = l.getState().lastSeqId || 0, n = await this.api.subscribeSessionEvents(e, t, { signal: this.abortController?.signal });
|
|
2482
|
+
o = 0;
|
|
2483
|
+
let r = n.getReader(), s = new TextDecoder(), c = "";
|
|
2484
|
+
for (; a === void 0;) {
|
|
2485
|
+
let { value: t, done: n } = await r.read();
|
|
2486
|
+
if (n) break;
|
|
2487
|
+
c += s.decode(t, { stream: !0 });
|
|
2488
|
+
let o = X(c);
|
|
2489
|
+
c = o.remainder;
|
|
2490
|
+
for (let t of o.chunks) if (t.trim()) for (let n of Y(t)) {
|
|
2491
|
+
let t = n.data;
|
|
2492
|
+
if (!t || typeof t != "object") continue;
|
|
2493
|
+
let r = i.translate(t), o = Number(t.seq);
|
|
2494
|
+
if (Number.isFinite(o) && o > 0 && l.getState().setLastSeqId(o), r && (this.emit({
|
|
2495
|
+
type: "stream_event",
|
|
2496
|
+
event: r,
|
|
2497
|
+
sessionId: e
|
|
2498
|
+
}), r.EventType === "run_status")) {
|
|
2499
|
+
let e = String(r.Content?.status || "").toLowerCase();
|
|
2500
|
+
e !== "interrupted" && Ht.has(e) && (a = e);
|
|
2501
|
+
}
|
|
2502
|
+
}
|
|
2503
|
+
}
|
|
2504
|
+
a === void 0 && await new Promise((e) => setTimeout(e, 3e3));
|
|
2505
|
+
} catch (e) {
|
|
2506
|
+
if (e instanceof DOMException && e.name === "AbortError") return { terminalStatus: "cancelled" };
|
|
2507
|
+
if (o += 1, o >= 6) break;
|
|
2508
|
+
await new Promise((e) => setTimeout(e, 800 * o));
|
|
2509
|
+
}
|
|
2510
|
+
return a === void 0 && (a = "interrupted"), { terminalStatus: a };
|
|
2511
|
+
}
|
|
2512
|
+
async consumeStream(e, t, n, r, i) {
|
|
2513
|
+
let a = e.getReader(), o = new TextDecoder(), s = "", c = !1, l = 0;
|
|
2514
|
+
try {
|
|
2515
|
+
for (;;) {
|
|
2516
|
+
let { value: e, done: u } = await a.read();
|
|
2517
|
+
if (u) break;
|
|
2518
|
+
l += e.byteLength, c || (c = !0, this.emit({
|
|
2519
|
+
type: "assistant_message_created",
|
|
2520
|
+
messageId: r,
|
|
2521
|
+
invocationId: i
|
|
2522
|
+
})), s += o.decode(e, { stream: !0 });
|
|
2523
|
+
let { chunks: d, remainder: f } = X(s);
|
|
2524
|
+
s = f;
|
|
2525
|
+
for (let e of d) {
|
|
2526
|
+
if (!e.trim()) continue;
|
|
2527
|
+
if (this.isCompactionChunk(e)) {
|
|
2528
|
+
let t = Y(e);
|
|
2529
|
+
for (let e of t) e.eventName.startsWith("response.compaction") && this.upsertCompactionMessage({
|
|
2530
|
+
...e.data,
|
|
2531
|
+
phase: e.eventName.split(".").pop()
|
|
2532
|
+
});
|
|
2533
|
+
continue;
|
|
2534
|
+
}
|
|
2535
|
+
let i = Y(e), o = !1, s;
|
|
2536
|
+
for (let e of i) {
|
|
2537
|
+
if (e.eventName === "__done__") {
|
|
2538
|
+
o = !0;
|
|
2539
|
+
continue;
|
|
2540
|
+
}
|
|
2541
|
+
let i = Wt(e.eventName, e.data);
|
|
2542
|
+
i && this.emit({
|
|
2543
|
+
type: "activity",
|
|
2544
|
+
...i
|
|
2545
|
+
});
|
|
2546
|
+
let a = t.parse(e, n);
|
|
2547
|
+
for (let e of a) this.dispatchAction(e, r);
|
|
2548
|
+
if (Dt(a)) {
|
|
2549
|
+
o = !0;
|
|
2550
|
+
let e = a.find((e) => e.type === "terminal");
|
|
2551
|
+
s = e && "status" in e ? String(e.status || "") : void 0;
|
|
2552
|
+
}
|
|
2553
|
+
}
|
|
2554
|
+
if (o) return a.cancel().catch(() => {}), { terminalStatus: s };
|
|
2555
|
+
}
|
|
2556
|
+
}
|
|
2557
|
+
} catch (e) {
|
|
2558
|
+
if (!(e instanceof DOMException && e.name === "AbortError")) throw e;
|
|
2559
|
+
}
|
|
2560
|
+
if (l === 0) throw Error("运行时返回了空响应流,请稍后重试;若问题持续,请检查运行时状态。");
|
|
2561
|
+
return {};
|
|
2562
|
+
}
|
|
2563
|
+
isCompactionChunk(e) {
|
|
2564
|
+
return e.includes("response.compaction.start") || e.includes("response.compaction.done") || e.includes("response.compaction.failed");
|
|
2565
|
+
}
|
|
2566
|
+
dispatchAction(e, t) {
|
|
2567
|
+
switch (e.type) {
|
|
2568
|
+
case "text_delta":
|
|
2569
|
+
this.emit({
|
|
2570
|
+
type: "text_delta",
|
|
2571
|
+
messageId: t,
|
|
2572
|
+
delta: e.text
|
|
2573
|
+
});
|
|
2574
|
+
break;
|
|
2575
|
+
case "text_final":
|
|
2576
|
+
this.emit({
|
|
2577
|
+
type: "text_final",
|
|
2578
|
+
messageId: t,
|
|
2579
|
+
text: e.text
|
|
2580
|
+
});
|
|
2581
|
+
break;
|
|
2582
|
+
case "reasoning_delta":
|
|
2583
|
+
this.emit({
|
|
2584
|
+
type: "reasoning_delta",
|
|
2585
|
+
messageId: t,
|
|
2586
|
+
delta: e.text
|
|
2587
|
+
});
|
|
2588
|
+
break;
|
|
2589
|
+
case "tool_upsert":
|
|
2590
|
+
this.emit({
|
|
2591
|
+
type: "tool_upsert",
|
|
2592
|
+
messageId: t,
|
|
2593
|
+
name: e.name,
|
|
2594
|
+
args: e.args,
|
|
2595
|
+
status: e.status,
|
|
2596
|
+
extra: e.extra
|
|
2597
|
+
});
|
|
2598
|
+
break;
|
|
2599
|
+
case "tool_result":
|
|
2600
|
+
this.emit({
|
|
2601
|
+
type: "tool_result",
|
|
2602
|
+
messageId: t,
|
|
2603
|
+
name: e.name,
|
|
2604
|
+
output: e.output
|
|
2605
|
+
});
|
|
2606
|
+
break;
|
|
2607
|
+
case "approval_request":
|
|
2608
|
+
this.emit({
|
|
2609
|
+
type: "approval_requested",
|
|
2610
|
+
messageId: t,
|
|
2611
|
+
approvalRequestId: e.approvalRequestId,
|
|
2612
|
+
protocol: "responses",
|
|
2613
|
+
runId: e.runId,
|
|
2614
|
+
name: e.name || "人工确认",
|
|
2615
|
+
args: e.args || "",
|
|
2616
|
+
message: e.message || "本次运行需要人工审批后才能继续。"
|
|
2617
|
+
});
|
|
2618
|
+
break;
|
|
2619
|
+
case "approval_resolved":
|
|
2620
|
+
this.emit({
|
|
2621
|
+
type: "approval_resolved",
|
|
2622
|
+
approvalRequestId: e.approvalRequestId,
|
|
2623
|
+
decision: e.decision,
|
|
2624
|
+
revision: e.revision
|
|
2625
|
+
});
|
|
2626
|
+
break;
|
|
2627
|
+
case "incomplete": break;
|
|
2628
|
+
case "failed":
|
|
2629
|
+
this.emit({
|
|
2630
|
+
type: "text_final",
|
|
2631
|
+
messageId: t,
|
|
2632
|
+
text: `生成失败:${e.message}`
|
|
2633
|
+
});
|
|
2634
|
+
break;
|
|
2635
|
+
case "terminal":
|
|
2636
|
+
this.emit({
|
|
2637
|
+
type: "terminal",
|
|
2638
|
+
status: e.status
|
|
2639
|
+
});
|
|
2640
|
+
break;
|
|
2641
|
+
case "compaction":
|
|
2642
|
+
this.emit({
|
|
2643
|
+
type: "compaction",
|
|
2644
|
+
phase: e.phase,
|
|
2645
|
+
trigger: e.trigger,
|
|
2646
|
+
compactedUntilSeqId: e.compactedUntilSeqId
|
|
2647
|
+
});
|
|
2648
|
+
break;
|
|
2649
|
+
case "a2ui_surface_begin":
|
|
2650
|
+
this.emit({
|
|
2651
|
+
type: "a2ui_surface_begin",
|
|
2652
|
+
surfaceId: e.surfaceId,
|
|
2653
|
+
surface: e.surface
|
|
2654
|
+
});
|
|
2655
|
+
break;
|
|
2656
|
+
case "a2ui_surface_update":
|
|
2657
|
+
this.emit({
|
|
2658
|
+
type: "a2ui_surface_update",
|
|
2659
|
+
surfaceId: e.surfaceId,
|
|
2660
|
+
surface: e.surface
|
|
2661
|
+
});
|
|
2662
|
+
break;
|
|
2663
|
+
case "a2ui_surface_end":
|
|
2664
|
+
this.emit({
|
|
2665
|
+
type: "a2ui_surface_end",
|
|
2666
|
+
surfaceId: e.surfaceId
|
|
2667
|
+
});
|
|
2668
|
+
break;
|
|
2669
|
+
case "a2ui_interaction":
|
|
2670
|
+
this.emit({
|
|
2671
|
+
type: "a2ui_interaction",
|
|
2672
|
+
surfaceId: e.surfaceId,
|
|
2673
|
+
interactionId: e.interactionId,
|
|
2674
|
+
kind: e.kind,
|
|
2675
|
+
inputSchema: e.inputSchema
|
|
2676
|
+
});
|
|
2677
|
+
break;
|
|
2678
|
+
}
|
|
2679
|
+
}
|
|
2680
|
+
upsertCompactionMessage(e) {
|
|
2681
|
+
let t = typeof e.eventName == "string" ? e.eventName.split(".").pop() : void 0, n = String(e.phase || t || "start");
|
|
2682
|
+
this.emit({
|
|
2683
|
+
type: "compaction",
|
|
2684
|
+
phase: n,
|
|
2685
|
+
trigger: e.trigger ? String(e.trigger) : void 0,
|
|
2686
|
+
compactedUntilSeqId: e.compacted_until_seq_id ? Number(e.compacted_until_seq_id) : void 0
|
|
2687
|
+
}), n !== "start" && (this.activeCompactionId = null);
|
|
2688
|
+
}
|
|
2689
|
+
failCompaction() {
|
|
2690
|
+
this.activeCompactionId &&= (this.emit({
|
|
2691
|
+
type: "compaction",
|
|
2692
|
+
phase: "failed"
|
|
2693
|
+
}), null);
|
|
2694
|
+
}
|
|
2695
|
+
};
|
|
2696
|
+
//#endregion
|
|
2697
|
+
//#region src/utils/recovered-run.ts
|
|
2698
|
+
function Jt(e, t, n) {
|
|
2699
|
+
let r = e.filter((e) => e.role === "model" && e.invocationId === n), i = ie(t).filter((e) => e.role === "model"), a = i.map((e) => ({
|
|
2700
|
+
...e,
|
|
2701
|
+
id: !e.itemId && i.length === 1 ? r[0]?.id || `${n}:assistant` : e.id,
|
|
2702
|
+
invocationId: n,
|
|
2703
|
+
blocks: re({
|
|
2704
|
+
content: e.content,
|
|
2705
|
+
reasoning: e.reasoning,
|
|
2706
|
+
tools: e.tools
|
|
2707
|
+
})
|
|
2708
|
+
}));
|
|
2709
|
+
if (a.length === 0) return e;
|
|
2710
|
+
let o = e.findIndex((e) => e.role === "model" && e.invocationId === n), s = e.filter((e) => !(e.role === "model" && e.invocationId === n)), c = o < 0 ? s.length : Math.min(o, s.length);
|
|
2711
|
+
return [
|
|
2712
|
+
...s.slice(0, c),
|
|
2713
|
+
...a,
|
|
2714
|
+
...s.slice(c)
|
|
2715
|
+
];
|
|
2716
|
+
}
|
|
2717
|
+
//#endregion
|
|
2718
|
+
//#region src/core/conversation/hosted.ts
|
|
2719
|
+
var Yt = "conversation_item_v1";
|
|
2720
|
+
function Xt(e) {
|
|
2721
|
+
return typeof e == "object" && e && !Array.isArray(e) ? e : null;
|
|
2722
|
+
}
|
|
2723
|
+
function Z(e) {
|
|
2724
|
+
return typeof e == "string" && e.trim() ? e : null;
|
|
2725
|
+
}
|
|
2726
|
+
function Zt(e) {
|
|
2727
|
+
if (typeof e == "string") return e;
|
|
2728
|
+
if (e == null) return "";
|
|
2729
|
+
try {
|
|
2730
|
+
return JSON.stringify(e, null, 2);
|
|
2731
|
+
} catch {
|
|
2732
|
+
return String(e);
|
|
2733
|
+
}
|
|
2734
|
+
}
|
|
2735
|
+
function Q(e) {
|
|
2736
|
+
return {
|
|
2737
|
+
id: `conversation:${e.runId}:${e.itemId}`,
|
|
2738
|
+
timestamp: 0,
|
|
2739
|
+
eventType: Yt,
|
|
2740
|
+
eventId: e.sourceEventIds.at(-1),
|
|
2741
|
+
runId: e.runId,
|
|
2742
|
+
itemId: e.itemId,
|
|
2743
|
+
status: e.lifecycle === "failed" ? "failed" : e.lifecycle === "completed" ? "completed" : "running"
|
|
2744
|
+
};
|
|
2745
|
+
}
|
|
2746
|
+
function Qt(e) {
|
|
2747
|
+
return e.lifecycle === "failed" ? "error" : e.lifecycle === "completed" ? "done" : "streaming";
|
|
2748
|
+
}
|
|
2749
|
+
function $t(e) {
|
|
2750
|
+
let t = typeof e.payload.text == "string" ? e.payload.text : "";
|
|
2751
|
+
if (e.kind === "user_message") return {
|
|
2752
|
+
...Q(e),
|
|
2753
|
+
role: "user",
|
|
2754
|
+
content: t,
|
|
2755
|
+
eventType: "user_message"
|
|
2756
|
+
};
|
|
2757
|
+
let n = e.kind === "reasoning" ? {
|
|
2758
|
+
id: `conversation-block:${e.itemId}`,
|
|
2759
|
+
type: "thinking",
|
|
2760
|
+
content: t,
|
|
2761
|
+
status: Qt(e)
|
|
2762
|
+
} : {
|
|
2763
|
+
id: `conversation-block:${e.itemId}`,
|
|
2764
|
+
type: "text",
|
|
2765
|
+
content: t,
|
|
2766
|
+
status: Qt(e)
|
|
2767
|
+
};
|
|
2768
|
+
return {
|
|
2769
|
+
...Q(e),
|
|
2770
|
+
role: "model",
|
|
2771
|
+
content: e.kind === "assistant_text" ? t : "",
|
|
2772
|
+
reasoning: e.kind === "reasoning" ? t : void 0,
|
|
2773
|
+
blocks: [n]
|
|
2774
|
+
};
|
|
2775
|
+
}
|
|
2776
|
+
function en(e) {
|
|
2777
|
+
let t = Z(e.payload.tool) || "Tool", n = Z(e.payload.callId), r = Zt(e.payload.args), i = Object.prototype.hasOwnProperty.call(e.payload, "output") ? Zt(e.payload.output) : void 0, a = e.lifecycle === "failed" || e.payload.isError === !0 ? "error" : e.lifecycle === "completed" ? "completed" : "running";
|
|
2778
|
+
return {
|
|
2779
|
+
...Q(e),
|
|
2780
|
+
role: "model",
|
|
2781
|
+
content: "",
|
|
2782
|
+
blocks: [{
|
|
2783
|
+
id: `conversation-block:${e.itemId}`,
|
|
2784
|
+
type: "tool",
|
|
2785
|
+
toolName: t,
|
|
2786
|
+
args: r,
|
|
2787
|
+
output: i,
|
|
2788
|
+
status: a,
|
|
2789
|
+
...n ? { extra: { callId: n } } : {}
|
|
2790
|
+
}],
|
|
2791
|
+
tools: { [t]: {
|
|
2792
|
+
name: t,
|
|
2793
|
+
...n ? { callId: n } : {},
|
|
2794
|
+
args: r,
|
|
2795
|
+
output: i,
|
|
2796
|
+
status: a
|
|
2797
|
+
} }
|
|
2798
|
+
};
|
|
2799
|
+
}
|
|
2800
|
+
function tn(e) {
|
|
2801
|
+
return e === "structured_input" ? "structured_input" : e === "plan_review" ? "plan_review" : e === "custom" ? "custom" : "approval";
|
|
2802
|
+
}
|
|
2803
|
+
function nn(e) {
|
|
2804
|
+
let t = Z(e.payload.interactionId), n = e.payload.revision;
|
|
2805
|
+
if (!t || !Number.isInteger(n) || Number(n) < 1) return null;
|
|
2806
|
+
let r = Zt(e.payload.detail), i = Z(e.payload.prompt) || r, a = tn(e.payload.interactionKind), o = e.lifecycle === "completed";
|
|
2807
|
+
return {
|
|
2808
|
+
interactionId: t,
|
|
2809
|
+
sessionId: e.sessionId,
|
|
2810
|
+
runId: e.runId,
|
|
2811
|
+
kind: a,
|
|
2812
|
+
title: Z(e.payload.title) || (a === "approval" ? `审批:${Z(e.payload.kind) || "操作"}` : "需要补充信息"),
|
|
2813
|
+
message: i || "运行需要人工确认。",
|
|
2814
|
+
requestSchema: Xt(e.payload.inputSchema),
|
|
2815
|
+
presentation: null,
|
|
2816
|
+
status: o ? "resolved" : "pending",
|
|
2817
|
+
revision: Number(n),
|
|
2818
|
+
createdAt: Z(e.payload.createdAt) || "",
|
|
2819
|
+
expiresAt: Z(e.payload.expiresAt),
|
|
2820
|
+
resolvedAt: o ? Z(e.payload.resolvedAt) : null,
|
|
2821
|
+
actor: o ? Z(e.payload.actor) : null,
|
|
2822
|
+
outcome: o && [
|
|
2823
|
+
"approved",
|
|
2824
|
+
"rejected",
|
|
2825
|
+
"submitted",
|
|
2826
|
+
"cancelled",
|
|
2827
|
+
"expired"
|
|
2828
|
+
].includes(String(e.payload.outcome || "")) ? e.payload.outcome : null,
|
|
2829
|
+
responseSummary: o ? Z(e.payload.responseSummary) : null,
|
|
2830
|
+
source: "interaction_v1",
|
|
2831
|
+
extensions: {
|
|
2832
|
+
conversation_item_id: e.itemId,
|
|
2833
|
+
...Z(e.payload.callId) ? { call_id: Z(e.payload.callId) } : {}
|
|
2834
|
+
}
|
|
2835
|
+
};
|
|
2836
|
+
}
|
|
2837
|
+
function rn(e, t) {
|
|
2838
|
+
let n = Z(e.payload.kind) || "approval", r = Zt(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 = {
|
|
2839
|
+
approvalRequestId: t.interactionId,
|
|
2840
|
+
approvalStatus: i,
|
|
2841
|
+
approvalMessage: t.message
|
|
2842
|
+
};
|
|
2843
|
+
return {
|
|
2844
|
+
...Q(e),
|
|
2845
|
+
role: "model",
|
|
2846
|
+
content: "",
|
|
2847
|
+
blocks: [{
|
|
2848
|
+
id: `conversation-block:${e.itemId}`,
|
|
2849
|
+
type: "tool",
|
|
2850
|
+
toolName: n,
|
|
2851
|
+
args: r,
|
|
2852
|
+
status: a,
|
|
2853
|
+
extra: o
|
|
2854
|
+
}],
|
|
2855
|
+
tools: { [n]: {
|
|
2856
|
+
name: n,
|
|
2857
|
+
args: r,
|
|
2858
|
+
status: a,
|
|
2859
|
+
approvalRequestId: t.interactionId,
|
|
2860
|
+
approvalStatus: i,
|
|
2861
|
+
approvalMessage: t.message
|
|
2862
|
+
} }
|
|
2863
|
+
};
|
|
2864
|
+
}
|
|
2865
|
+
function an(e) {
|
|
2866
|
+
for (let t of [
|
|
2867
|
+
"createSurface",
|
|
2868
|
+
"updateComponents",
|
|
2869
|
+
"updateDataModel",
|
|
2870
|
+
"deleteSurface"
|
|
2871
|
+
]) {
|
|
2872
|
+
let n = Z(Xt(e[t])?.surfaceId);
|
|
2873
|
+
if (n) return n;
|
|
2874
|
+
}
|
|
2875
|
+
return null;
|
|
2876
|
+
}
|
|
2877
|
+
function on(e) {
|
|
2878
|
+
let t = Array.isArray(e.payload.data) ? e.payload.data : Array.isArray(e.payload.operations) ? e.payload.operations : null;
|
|
2879
|
+
if (!t) return null;
|
|
2880
|
+
let n = t.map(Xt);
|
|
2881
|
+
if (n.some((e) => e === null)) return null;
|
|
2882
|
+
let r = n, i = Z(e.payload.surfaceId) || r.map(an).find((e) => e !== null) || null;
|
|
2883
|
+
return i ? {
|
|
2884
|
+
...Q(e),
|
|
2885
|
+
role: "a2ui",
|
|
2886
|
+
content: "",
|
|
2887
|
+
aguiActivity: {
|
|
2888
|
+
surfaceId: i,
|
|
2889
|
+
messages: r
|
|
2890
|
+
}
|
|
2891
|
+
} : null;
|
|
2892
|
+
}
|
|
2893
|
+
function sn(e, t, n, r = !1) {
|
|
2894
|
+
return {
|
|
2895
|
+
...Q(e),
|
|
2896
|
+
role: "system",
|
|
2897
|
+
content: n ? `${t}: ${n}` : t,
|
|
2898
|
+
status: r ? "failed" : Q(e).status
|
|
2899
|
+
};
|
|
2900
|
+
}
|
|
2901
|
+
function cn(e) {
|
|
2902
|
+
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 = [];
|
|
2903
|
+
for (let { item: e } of t.timeline) {
|
|
2904
|
+
if (n.has(e.itemId)) {
|
|
2905
|
+
l.push($t(e));
|
|
2906
|
+
continue;
|
|
2907
|
+
}
|
|
2908
|
+
if (r.has(e.itemId)) {
|
|
2909
|
+
l.push(en(e));
|
|
2910
|
+
continue;
|
|
2911
|
+
}
|
|
2912
|
+
if (i.has(e.itemId) || a.has(e.itemId)) {
|
|
2913
|
+
let t = nn(e);
|
|
2914
|
+
t ? (u.push(t), l.push(rn(e, t))) : l.push(sn(e, "Interaction unavailable", "The server did not provide a durable interaction revision; this card is read-only."));
|
|
2915
|
+
continue;
|
|
2916
|
+
}
|
|
2917
|
+
if (o.has(e.itemId)) {
|
|
2918
|
+
let t = on(e);
|
|
2919
|
+
l.push(t || sn(e, "Unsupported content", "A2UI payload is not a valid passive operation list."));
|
|
2920
|
+
continue;
|
|
2921
|
+
}
|
|
2922
|
+
let t = s.get(e.itemId);
|
|
2923
|
+
if (t) {
|
|
2924
|
+
l.push(t.uri ? {
|
|
2925
|
+
...Q(e),
|
|
2926
|
+
role: "model",
|
|
2927
|
+
content: t.name,
|
|
2928
|
+
attachments: [{
|
|
2929
|
+
name: t.name,
|
|
2930
|
+
type: t.mimeType,
|
|
2931
|
+
url: t.uri
|
|
2932
|
+
}]
|
|
2933
|
+
} : sn(e, "Artifact unavailable", "The artifact URI is not a safe HTTP(S) link."));
|
|
2934
|
+
continue;
|
|
2935
|
+
}
|
|
2936
|
+
let d = c.get(e.itemId);
|
|
2937
|
+
if (d) {
|
|
2938
|
+
if (e.kind === "error" && ["cancelled", "canceled"].includes(String(e.payload.status || "").toLowerCase())) continue;
|
|
2939
|
+
l.push(sn(e, d.title, d.detail, d.failed));
|
|
2940
|
+
}
|
|
2941
|
+
}
|
|
2942
|
+
return {
|
|
2943
|
+
messages: l,
|
|
2944
|
+
interactions: u
|
|
2945
|
+
};
|
|
2946
|
+
}
|
|
2947
|
+
function ln(e, t) {
|
|
2948
|
+
let n = cn(t).messages;
|
|
2949
|
+
if (!n.length) return e;
|
|
2950
|
+
let r = e.filter((e) => e.eventType !== "optimistic_assistant_placeholder");
|
|
2951
|
+
{
|
|
2952
|
+
let e = -1;
|
|
2953
|
+
for (let t = r.length - 1; t >= 0; --t) {
|
|
2954
|
+
let n = r[t];
|
|
2955
|
+
if (n.role === "user" && n.eventType === "optimistic_user_message") {
|
|
2956
|
+
e = t;
|
|
2957
|
+
break;
|
|
2958
|
+
}
|
|
2959
|
+
}
|
|
2960
|
+
if (e >= 0) {
|
|
2961
|
+
let i = r[e], a = String(i.content || "").trim();
|
|
2962
|
+
if (r = [...r.slice(0, e), ...r.slice(e + 1)], !n.some((e) => e.role === "user") && a) {
|
|
2963
|
+
let e = r.filter((e) => !(e.eventType === Yt && e.runId === t.runId || e.invocationId === t.runId)), a = r.findIndex((e) => e.eventType === Yt && e.runId === t.runId || e.invocationId === t.runId), o = a < 0 ? e.length : a;
|
|
2964
|
+
return [
|
|
2965
|
+
...e.slice(0, o),
|
|
2966
|
+
{
|
|
2967
|
+
...i,
|
|
2968
|
+
timestamp: Date.now()
|
|
2969
|
+
},
|
|
2970
|
+
...n,
|
|
2971
|
+
...e.slice(o)
|
|
2972
|
+
];
|
|
2973
|
+
}
|
|
2974
|
+
}
|
|
2975
|
+
}
|
|
2976
|
+
let i = (e) => e.eventType === Yt && e.runId === t.runId || e.invocationId === t.runId, a = r.findIndex(i), o = r.filter((e) => !i(e)), s = a < 0 ? o.length : a;
|
|
2977
|
+
return [
|
|
2978
|
+
...o.slice(0, s),
|
|
2979
|
+
...n,
|
|
2980
|
+
...o.slice(s)
|
|
2981
|
+
];
|
|
2982
|
+
}
|
|
2983
|
+
//#endregion
|
|
2984
|
+
//#region src/core/run/dispatcher.ts
|
|
2985
|
+
var un = "连接断开或生成出错,请重试";
|
|
2986
|
+
function dn(e) {
|
|
2987
|
+
let t = String(e.message || "").trim();
|
|
2988
|
+
return /runtime agent kernel is not ready/i.test(t) ? "云端运行时尚未就绪,请检查部署状态后重试" : !t || /failed to fetch|networkerror/i.test(t) ? un : t.length <= 240 ? t : un;
|
|
2989
|
+
}
|
|
2990
|
+
var fn = new Set(["completed"]), pn = new Set([
|
|
2991
|
+
"failed",
|
|
2992
|
+
"error",
|
|
2993
|
+
"cancelled",
|
|
2994
|
+
"canceled",
|
|
2995
|
+
"aborted",
|
|
2996
|
+
"incomplete"
|
|
2997
|
+
]), mn = /* @__PURE__ */ new Map();
|
|
2998
|
+
function $(e, t) {
|
|
2999
|
+
f.getState().patchMessages((n) => {
|
|
3000
|
+
if (n.some((t) => t.id === e)) return t ? n.map((n) => n.id === e ? {
|
|
3001
|
+
...n,
|
|
3002
|
+
invocationId: t
|
|
3003
|
+
} : n) : n;
|
|
3004
|
+
let r = -1;
|
|
3005
|
+
for (let e = n.length - 1; e >= 0; --e) if (n[e]?.eventType === "optimistic_assistant_placeholder") {
|
|
3006
|
+
r = e;
|
|
3007
|
+
break;
|
|
3008
|
+
}
|
|
3009
|
+
return r >= 0 ? n.map((n, i) => i === r ? {
|
|
3010
|
+
...n,
|
|
3011
|
+
id: e,
|
|
3012
|
+
eventType: void 0,
|
|
3013
|
+
invocationId: t
|
|
3014
|
+
} : n) : [...n, {
|
|
3015
|
+
id: e,
|
|
3016
|
+
role: "model",
|
|
3017
|
+
content: "",
|
|
3018
|
+
timestamp: Date.now(),
|
|
3019
|
+
reasoning: "",
|
|
3020
|
+
invocationId: t
|
|
3021
|
+
}];
|
|
3022
|
+
});
|
|
3023
|
+
}
|
|
3024
|
+
function hn(e) {
|
|
3025
|
+
return e.filter((e) => e.eventType !== "optimistic_assistant_placeholder");
|
|
3026
|
+
}
|
|
3027
|
+
function gn(e) {
|
|
3028
|
+
let t = String(e || "").trim().toLowerCase(), n = fn.has(t) ? "completed" : pn.has(t) ? "error" : null;
|
|
3029
|
+
n && f.getState().patchMessages((e) => e.map((e) => {
|
|
3030
|
+
if (!e.tools) return e;
|
|
3031
|
+
let t = !1, r = Object.fromEntries(Object.entries(e.tools).map(([e, r]) => r.status === "running" ? (t = !0, [e, {
|
|
3032
|
+
...r,
|
|
3033
|
+
status: n
|
|
3034
|
+
}]) : [e, r]));
|
|
3035
|
+
return t ? {
|
|
3036
|
+
...e,
|
|
3037
|
+
tools: r
|
|
3038
|
+
} : e;
|
|
3039
|
+
}));
|
|
3040
|
+
}
|
|
3041
|
+
function _n(e) {
|
|
3042
|
+
let t = !!(e.sessionId && h.getState().currentSessionId !== e.sessionId);
|
|
3043
|
+
if (t && ![
|
|
3044
|
+
"activity",
|
|
3045
|
+
"stage_changed",
|
|
3046
|
+
"stream_ended",
|
|
3047
|
+
"error",
|
|
3048
|
+
"rate_limited",
|
|
3049
|
+
"terminal"
|
|
3050
|
+
].includes(e.type)) return;
|
|
3051
|
+
let n = f.getState(), r = (e) => {
|
|
3052
|
+
e && l.getState().updateActivity({ sessionId: e });
|
|
3053
|
+
};
|
|
3054
|
+
switch (e.type) {
|
|
3055
|
+
case "activity":
|
|
3056
|
+
l.getState().updateActivity({
|
|
3057
|
+
sessionId: e.sessionId,
|
|
3058
|
+
source: e.source,
|
|
3059
|
+
status: e.status,
|
|
3060
|
+
phase: e.phase,
|
|
3061
|
+
detail: e.detail,
|
|
3062
|
+
countEvent: e.countEvent
|
|
3063
|
+
});
|
|
3064
|
+
break;
|
|
3065
|
+
case "user_message_added":
|
|
3066
|
+
l.getState().setBanner(null), n.patchMessages((t) => [...t, {
|
|
3067
|
+
id: e.messageId,
|
|
3068
|
+
role: "user",
|
|
3069
|
+
content: "",
|
|
3070
|
+
timestamp: Date.now()
|
|
3071
|
+
}]);
|
|
3072
|
+
break;
|
|
3073
|
+
case "assistant_message_created":
|
|
3074
|
+
$(e.messageId, e.invocationId);
|
|
3075
|
+
break;
|
|
3076
|
+
case "text_delta":
|
|
3077
|
+
$(e.messageId), r(e.sessionId), n.patchMessages((t) => t.map((t) => t.id === e.messageId ? {
|
|
3078
|
+
...t,
|
|
3079
|
+
content: t.content + e.delta,
|
|
3080
|
+
blocks: ue(t.blocks, e.delta)
|
|
3081
|
+
} : t));
|
|
3082
|
+
break;
|
|
3083
|
+
case "text_final":
|
|
3084
|
+
$(e.messageId), r(e.sessionId), n.patchMessages((t) => t.map((t) => t.id === e.messageId ? (() => {
|
|
3085
|
+
let n = !!(e.text && !t.content && t.reasoning?.endsWith(e.text)), r = n ? t.reasoning.slice(0, -e.text.length) : t.reasoning, i = n ? me(t.blocks, e.text) : t.blocks;
|
|
3086
|
+
return {
|
|
3087
|
+
...t,
|
|
3088
|
+
reasoning: r,
|
|
3089
|
+
content: e.text,
|
|
3090
|
+
blocks: ne(i, e.text)
|
|
3091
|
+
};
|
|
3092
|
+
})() : t));
|
|
3093
|
+
break;
|
|
3094
|
+
case "reasoning_delta":
|
|
3095
|
+
$(e.messageId), r(e.sessionId), n.patchMessages((t) => t.map((t) => t.id === e.messageId ? {
|
|
3096
|
+
...t,
|
|
3097
|
+
reasoning: (t.reasoning || "") + e.delta,
|
|
3098
|
+
blocks: se(t.blocks, e.delta)
|
|
3099
|
+
} : t));
|
|
3100
|
+
break;
|
|
3101
|
+
case "tool_upsert":
|
|
3102
|
+
$(e.messageId), r(e.sessionId), n.patchMessages((t) => t.map((t) => {
|
|
3103
|
+
if (t.id !== e.messageId) return t;
|
|
3104
|
+
let n = t.tools?.[e.name], r = n?.approvalStatus === "approved" || n?.approvalStatus === "rejected" || n?.approvalStatus === "cancelled", i = e.status, a = n?.approvalStatus === "rejected" || n?.approvalStatus === "cancelled" ? "completed" : n?.status === "error" && i !== "error" ? "error" : i;
|
|
3105
|
+
return {
|
|
3106
|
+
...t,
|
|
3107
|
+
blocks: he(t.blocks, e.name, {
|
|
3108
|
+
args: e.args,
|
|
3109
|
+
status: a,
|
|
3110
|
+
extra: e.extra
|
|
3111
|
+
}),
|
|
3112
|
+
tools: {
|
|
3113
|
+
...t.tools || {},
|
|
3114
|
+
[e.name]: {
|
|
3115
|
+
...n || {
|
|
3116
|
+
name: e.name,
|
|
3117
|
+
args: ""
|
|
3118
|
+
},
|
|
3119
|
+
name: e.name,
|
|
3120
|
+
args: e.args,
|
|
3121
|
+
status: a,
|
|
3122
|
+
...e.extra || {},
|
|
3123
|
+
...e.extra?.approvalRequestId && !r ? { approvalStatus: "pending" } : {}
|
|
3124
|
+
}
|
|
3125
|
+
}
|
|
3126
|
+
};
|
|
3127
|
+
}));
|
|
3128
|
+
break;
|
|
3129
|
+
case "tool_result":
|
|
3130
|
+
$(e.messageId), r(e.sessionId), n.patchMessages((t) => t.map((t) => {
|
|
3131
|
+
if (t.id !== e.messageId) return t;
|
|
3132
|
+
let n = E(e.output) ? "error" : "completed";
|
|
3133
|
+
return {
|
|
3134
|
+
...t,
|
|
3135
|
+
blocks: he(t.blocks, e.name, {
|
|
3136
|
+
output: e.output,
|
|
3137
|
+
status: n
|
|
3138
|
+
}),
|
|
3139
|
+
tools: {
|
|
3140
|
+
...t.tools || {},
|
|
3141
|
+
[e.name]: {
|
|
3142
|
+
...t.tools?.[e.name] || {
|
|
3143
|
+
name: e.name,
|
|
3144
|
+
args: ""
|
|
3145
|
+
},
|
|
3146
|
+
output: e.output,
|
|
3147
|
+
status: n
|
|
3148
|
+
}
|
|
3149
|
+
}
|
|
3150
|
+
};
|
|
3151
|
+
}));
|
|
3152
|
+
break;
|
|
3153
|
+
case "approval_requested":
|
|
3154
|
+
$(e.messageId), ut({
|
|
3155
|
+
approvalRequestId: e.approvalRequestId,
|
|
3156
|
+
protocol: e.protocol,
|
|
3157
|
+
runId: e.runId,
|
|
3158
|
+
name: e.name,
|
|
3159
|
+
message: e.message,
|
|
3160
|
+
args: e.args,
|
|
3161
|
+
approvalLevel: e.approvalLevel,
|
|
3162
|
+
sessionId: e.sessionId
|
|
3163
|
+
}), n.patchMessages((t) => t.map((t) => {
|
|
3164
|
+
if (t.id !== e.messageId) return t;
|
|
3165
|
+
let n = t.tools?.[e.approvalRequestId], r = n?.approvalStatus === "approved" || n?.approvalStatus === "rejected" || n?.approvalStatus === "cancelled", i = {
|
|
3166
|
+
approvalRequestId: e.approvalRequestId,
|
|
3167
|
+
approvalProtocol: e.protocol,
|
|
3168
|
+
approvalStatus: r ? n.approvalStatus : "pending",
|
|
3169
|
+
...e.message ? { approvalMessage: e.message } : {},
|
|
3170
|
+
...e.approvalLevel ? { approvalLevel: e.approvalLevel } : {}
|
|
3171
|
+
}, a = r ? n.status : "paused";
|
|
3172
|
+
return {
|
|
3173
|
+
...t,
|
|
3174
|
+
blocks: he(t.blocks, e.name, {
|
|
3175
|
+
args: e.args,
|
|
3176
|
+
status: a,
|
|
3177
|
+
extra: i
|
|
3178
|
+
}),
|
|
3179
|
+
tools: {
|
|
3180
|
+
...t.tools || {},
|
|
3181
|
+
[e.approvalRequestId]: {
|
|
3182
|
+
...n || {},
|
|
3183
|
+
name: e.name,
|
|
3184
|
+
args: e.args,
|
|
3185
|
+
status: a,
|
|
3186
|
+
...i
|
|
3187
|
+
}
|
|
3188
|
+
}
|
|
3189
|
+
};
|
|
3190
|
+
}));
|
|
3191
|
+
break;
|
|
3192
|
+
case "approval_resolved":
|
|
3193
|
+
dt({
|
|
3194
|
+
approvalRequestId: e.approvalRequestId,
|
|
3195
|
+
decision: e.decision,
|
|
3196
|
+
revision: e.revision,
|
|
3197
|
+
sessionId: e.sessionId
|
|
3198
|
+
}), n.patchMessages((t) => t.map((t) => {
|
|
3199
|
+
if (!t.tools) return t;
|
|
3200
|
+
let n = !1, r = Object.fromEntries(Object.entries(t.tools).map(([t, r]) => {
|
|
3201
|
+
if (r.approvalRequestId !== e.approvalRequestId) return [t, r];
|
|
3202
|
+
n = !0;
|
|
3203
|
+
let i = e.decision === "rejected" || e.decision === "cancelled" ? "completed" : r.status === "paused" ? "running" : r.status;
|
|
3204
|
+
return [t, {
|
|
3205
|
+
...r,
|
|
3206
|
+
status: i,
|
|
3207
|
+
approvalStatus: e.decision
|
|
3208
|
+
}];
|
|
3209
|
+
}));
|
|
3210
|
+
return n ? {
|
|
3211
|
+
...t,
|
|
3212
|
+
tools: r,
|
|
3213
|
+
blocks: t.blocks?.map((t) => {
|
|
3214
|
+
if (t.type !== "tool" || t.extra?.approvalRequestId !== e.approvalRequestId) return t;
|
|
3215
|
+
let n = e.decision === "rejected" || e.decision === "cancelled" ? "completed" : t.status === "paused" ? "running" : t.status;
|
|
3216
|
+
return {
|
|
3217
|
+
...t,
|
|
3218
|
+
status: n,
|
|
3219
|
+
extra: {
|
|
3220
|
+
...t.extra,
|
|
3221
|
+
approvalStatus: e.decision
|
|
3222
|
+
}
|
|
3223
|
+
};
|
|
3224
|
+
})
|
|
3225
|
+
} : t;
|
|
3226
|
+
}));
|
|
3227
|
+
break;
|
|
3228
|
+
case "system_message":
|
|
3229
|
+
n.patchMessages((t) => [...t, {
|
|
3230
|
+
id: String(Date.now() + Math.random()),
|
|
3231
|
+
role: "system",
|
|
3232
|
+
content: e.content,
|
|
3233
|
+
timestamp: Date.now()
|
|
3234
|
+
}]);
|
|
3235
|
+
break;
|
|
3236
|
+
case "compaction": {
|
|
3237
|
+
let t = `compaction-${Date.now()}`, r = e.phase === "start" ? "running" : e.phase === "failed" ? "failed" : "completed", i = de({
|
|
3238
|
+
id: t,
|
|
3239
|
+
timestamp: Date.now(),
|
|
3240
|
+
status: r,
|
|
3241
|
+
trigger: e.trigger,
|
|
3242
|
+
compactedUntilSeqId: e.compactedUntilSeqId
|
|
3243
|
+
});
|
|
3244
|
+
n.patchMessages((e) => e.findIndex((e) => e.id === t) < 0 ? [...e, i] : e.map((e) => e.id === t ? {
|
|
3245
|
+
...e,
|
|
3246
|
+
...i
|
|
3247
|
+
} : e));
|
|
3248
|
+
break;
|
|
3249
|
+
}
|
|
3250
|
+
case "stage_changed":
|
|
3251
|
+
e.stage === "streaming" || e.stage === "connecting" ? l.getState().setSessionStreaming(e.sessionId, !0) : (e.stage === "completing" || e.stage === "error" || e.stage === "cancelled") && l.getState().setSessionStreaming(e.sessionId, !1);
|
|
3252
|
+
break;
|
|
3253
|
+
case "stream_ended":
|
|
3254
|
+
l.getState().setSessionStreaming(e.sessionId, !1), t || f.getState().patchMessages((t) => hn(t).map((t) => !(!e.runId || t.runId === e.runId || t.invocationId === e.runId) || t.role !== "model" || !t.blocks?.some((e) => e.status === "streaming") ? t : {
|
|
3255
|
+
...t,
|
|
3256
|
+
blocks: k(t.blocks.map((e) => e.type === "text" && e.status === "streaming" ? {
|
|
3257
|
+
...e,
|
|
3258
|
+
status: "done"
|
|
3259
|
+
} : e))
|
|
3260
|
+
})), globalThis.setTimeout(() => {
|
|
3261
|
+
let t = l.getState();
|
|
3262
|
+
t.getSessionActivity(e.sessionId)?.status === "completed" && t.clearSessionActivity(e.sessionId);
|
|
3263
|
+
}, 2400);
|
|
3264
|
+
break;
|
|
3265
|
+
case "error": {
|
|
3266
|
+
let r = dn(e.error);
|
|
3267
|
+
l.getState().setSessionStreaming(e.sessionId, !1), l.getState().updateActivity({
|
|
3268
|
+
sessionId: e.sessionId,
|
|
3269
|
+
status: "failed",
|
|
3270
|
+
phase: "运行失败",
|
|
3271
|
+
detail: r,
|
|
3272
|
+
countEvent: !1
|
|
3273
|
+
}), t || (l.getState().setBanner({
|
|
3274
|
+
kind: "error",
|
|
3275
|
+
message: r,
|
|
3276
|
+
sessionId: e.sessionId
|
|
3277
|
+
}), n.patchMessages((e) => [...hn(e), {
|
|
3278
|
+
id: String(Date.now()),
|
|
3279
|
+
role: "model",
|
|
3280
|
+
content: `运行失败:${r}`,
|
|
3281
|
+
timestamp: Date.now()
|
|
3282
|
+
}]));
|
|
3283
|
+
break;
|
|
3284
|
+
}
|
|
3285
|
+
case "rate_limited":
|
|
3286
|
+
l.getState().setSessionStreaming(e.sessionId, !1), l.getState().updateActivity({
|
|
3287
|
+
sessionId: e.sessionId,
|
|
3288
|
+
status: "failed",
|
|
3289
|
+
phase: "请求被限流",
|
|
3290
|
+
countEvent: !1
|
|
3291
|
+
}), t || (l.getState().setBanner({
|
|
3292
|
+
kind: "rate_limited",
|
|
3293
|
+
message: e.message || "请求过于频繁,请稍后重试",
|
|
3294
|
+
retryAfterSec: e.retryAfterSec,
|
|
3295
|
+
sessionId: e.sessionId
|
|
3296
|
+
}), n.patchMessages(hn));
|
|
3297
|
+
break;
|
|
3298
|
+
case "terminal":
|
|
3299
|
+
e.sessionId && String(e.status || "").trim().toLowerCase() !== "incomplete" && J.markSessionTerminal(e.sessionId), t || gn(e.status);
|
|
3300
|
+
break;
|
|
3301
|
+
case "stream_event": {
|
|
3302
|
+
let t = e.sessionId || e.event.SessionId;
|
|
3303
|
+
lt(e.event, t || void 0);
|
|
3304
|
+
let n = e.event.SeqId;
|
|
3305
|
+
typeof n == "number" && n > 0 && l.getState().setLastSeqId(n), t && l.getState().updateActivity({ sessionId: t });
|
|
3306
|
+
let r = String(e.event.InvocationId || "").trim();
|
|
3307
|
+
if (t && r) {
|
|
3308
|
+
let n = `${t}:${r}`, i = fe(mn.get(n) || [], [e.event]);
|
|
3309
|
+
mn.set(n, i), f.getState().patchMessages((e) => e.some((e) => e.eventType === "conversation_item_v1" && e.runId === r) ? e : Jt(e, i, r)), A(e.event) && mn.delete(n);
|
|
3310
|
+
}
|
|
3311
|
+
e.event.EventType === "run_checkpoint" && t && b.getState().upsertSessionCheckpoint(t, e.event);
|
|
3312
|
+
{
|
|
3313
|
+
let n = j(e.event);
|
|
3314
|
+
n === "completed" ? l.getState().updateActivity({
|
|
3315
|
+
sessionId: t,
|
|
3316
|
+
status: "completed",
|
|
3317
|
+
phase: "后台长任务已完成",
|
|
3318
|
+
countEvent: !1
|
|
3319
|
+
}) : n === "cancelled" || n === "canceled" || n === "aborted" ? l.getState().updateActivity({
|
|
3320
|
+
sessionId: t,
|
|
3321
|
+
status: "stopped",
|
|
3322
|
+
phase: "后台长任务已取消",
|
|
3323
|
+
countEvent: !1
|
|
3324
|
+
}) : (n === "failed" || n === "error") && l.getState().updateActivity({
|
|
3325
|
+
sessionId: t,
|
|
3326
|
+
status: "failed",
|
|
3327
|
+
phase: "后台长任务失败",
|
|
3328
|
+
countEvent: !1
|
|
3329
|
+
});
|
|
3330
|
+
}
|
|
3331
|
+
break;
|
|
3332
|
+
}
|
|
3333
|
+
case "conversation_snapshot": {
|
|
3334
|
+
let t = cn(e.result);
|
|
3335
|
+
for (let e of t.interactions) J.upsert(e);
|
|
3336
|
+
n.patchMessages((t) => ln(t, e.result)), r(e.sessionId);
|
|
3337
|
+
break;
|
|
3338
|
+
}
|
|
3339
|
+
case "a2ui_surface_begin": {
|
|
3340
|
+
let t = `a2ui-${e.surfaceId}`;
|
|
3341
|
+
f.getState().patchMessages((n) => [...n.filter((e) => e.id !== t), {
|
|
3342
|
+
id: t,
|
|
3343
|
+
role: "a2ui",
|
|
3344
|
+
content: "",
|
|
3345
|
+
timestamp: Date.now(),
|
|
3346
|
+
a2ui: {
|
|
3347
|
+
surfaceId: e.surfaceId,
|
|
3348
|
+
surface: e.surface
|
|
3349
|
+
}
|
|
3350
|
+
}]);
|
|
3351
|
+
break;
|
|
3352
|
+
}
|
|
3353
|
+
case "a2ui_surface_update": {
|
|
3354
|
+
let t = `a2ui-${e.surfaceId}`;
|
|
3355
|
+
f.getState().patchMessages((n) => n.map((n) => n.id === t && n.a2ui ? {
|
|
3356
|
+
...n,
|
|
3357
|
+
a2ui: {
|
|
3358
|
+
...n.a2ui,
|
|
3359
|
+
surface: e.surface
|
|
3360
|
+
}
|
|
3361
|
+
} : n));
|
|
3362
|
+
break;
|
|
3363
|
+
}
|
|
3364
|
+
case "a2ui_surface_end": {
|
|
3365
|
+
let t = `a2ui-${e.surfaceId}`;
|
|
3366
|
+
f.getState().patchMessages((e) => e.map((e) => e.id === t && e.a2ui ? {
|
|
3367
|
+
...e,
|
|
3368
|
+
a2ui: {
|
|
3369
|
+
...e.a2ui,
|
|
3370
|
+
ended: !0
|
|
3371
|
+
}
|
|
3372
|
+
} : e));
|
|
3373
|
+
break;
|
|
3374
|
+
}
|
|
3375
|
+
case "a2ui_interaction": {
|
|
3376
|
+
let t = `a2ui-${e.surfaceId}`;
|
|
3377
|
+
f.getState().patchMessages((n) => n.map((n) => n.id === t && n.a2ui ? {
|
|
3378
|
+
...n,
|
|
3379
|
+
a2ui: {
|
|
3380
|
+
...n.a2ui,
|
|
3381
|
+
pendingInteraction: {
|
|
3382
|
+
interactionId: e.interactionId,
|
|
3383
|
+
kind: e.kind,
|
|
3384
|
+
inputSchema: e.inputSchema
|
|
3385
|
+
}
|
|
3386
|
+
}
|
|
3387
|
+
} : n));
|
|
3388
|
+
break;
|
|
3389
|
+
}
|
|
3390
|
+
case "agui_activity": {
|
|
3391
|
+
let t = `agui-a2ui-${e.surfaceId}`;
|
|
3392
|
+
f.getState().patchMessages((n) => {
|
|
3393
|
+
let r = {
|
|
3394
|
+
surfaceId: e.surfaceId,
|
|
3395
|
+
messages: e.messages
|
|
3396
|
+
}, i = !1, a = n.map((t) => {
|
|
3397
|
+
if (t.id !== e.messageId) return t;
|
|
3398
|
+
i = !0;
|
|
3399
|
+
let n = [...(t.aguiActivities || []).filter((t) => t.surfaceId !== e.surfaceId), r];
|
|
3400
|
+
return {
|
|
3401
|
+
...t,
|
|
3402
|
+
aguiActivities: n
|
|
3403
|
+
};
|
|
3404
|
+
});
|
|
3405
|
+
if (i) return a;
|
|
3406
|
+
let o = n.find((e) => e.id === t), s = {
|
|
3407
|
+
id: t,
|
|
3408
|
+
role: "a2ui",
|
|
3409
|
+
content: "",
|
|
3410
|
+
timestamp: o?.timestamp || Date.now(),
|
|
3411
|
+
aguiActivity: r
|
|
3412
|
+
};
|
|
3413
|
+
return o ? n.map((e) => e.id === t ? s : e) : [...n, s];
|
|
3414
|
+
});
|
|
3415
|
+
break;
|
|
3416
|
+
}
|
|
3417
|
+
}
|
|
3418
|
+
}
|
|
3419
|
+
function vn() {
|
|
3420
|
+
mn.clear();
|
|
3421
|
+
}
|
|
3422
|
+
//#endregion
|
|
3423
|
+
//#region src/hooks/useRunAgent.ts
|
|
3424
|
+
function yn(t) {
|
|
3425
|
+
let n = I(/* @__PURE__ */ new Map()), r = I(() => {}), i = ge(() => new ee(), []), a = t.conversationClient === void 0 ? i : t.conversationClient, { agentId: o, apiFormats: c, agentFramework: u, selectedModel: d, selectedModelMetadata: p, thinkingMode: m, permissionMode: g, currentSessionIdRef: _, queuedDraftRef: v, onRunSettled: y, onSessionCreated: b, waitForPendingSessionCreation: x, uiCapabilities: S } = t, C = P((t) => {
|
|
3426
|
+
t.updateConfig({
|
|
3427
|
+
agentId: o,
|
|
3428
|
+
apiFormats: c,
|
|
3429
|
+
agentFramework: u,
|
|
3430
|
+
selectedModel: d,
|
|
3431
|
+
selectedModelMetadata: p,
|
|
3432
|
+
thinkingMode: S.Thinking ? m : "auto",
|
|
3433
|
+
permissionMode: g,
|
|
3434
|
+
runtimeCapabilityMatrix: S.RuntimeCapabilityMatrix,
|
|
3435
|
+
hostedChatTransport: e(S, { requireResumableRun: !!(S.RunLifecycle?.Enabled && S.RunLifecycle.Resume) }),
|
|
3436
|
+
checkpointResumePreviewEnabled: !!S.RunLifecycle?.CheckpointResumePreview,
|
|
3437
|
+
conversationClient: a || void 0
|
|
3438
|
+
});
|
|
3439
|
+
}, [
|
|
3440
|
+
o,
|
|
3441
|
+
c,
|
|
3442
|
+
u,
|
|
3443
|
+
d,
|
|
3444
|
+
p,
|
|
3445
|
+
m,
|
|
3446
|
+
g,
|
|
3447
|
+
S,
|
|
3448
|
+
a
|
|
3449
|
+
]), w = P((e) => {
|
|
3450
|
+
let r = String(e || "new-session"), i = n.current.get(r);
|
|
3451
|
+
return i ? C(i) : (i = new qt(t.api), C(i), i.subscribe(_n), n.current.set(r, i)), i;
|
|
3452
|
+
}, [t.api, C]);
|
|
3453
|
+
F(() => {
|
|
3454
|
+
for (let e of n.current.values()) C(e);
|
|
3455
|
+
}, [C]);
|
|
3456
|
+
let T = P((e) => {
|
|
3457
|
+
v.current.push(e), s.getState().setQueuedDrafts((t) => [...t, e]);
|
|
3458
|
+
}, [v]), E = P((e) => {
|
|
3459
|
+
let t = e.text.trim(), n = e.attachments.map((e) => ({
|
|
3460
|
+
name: e.name,
|
|
3461
|
+
url: URL.createObjectURL(e),
|
|
3462
|
+
type: e.type || "application/octet-stream"
|
|
3463
|
+
}));
|
|
3464
|
+
if (!t && n.length === 0) return;
|
|
3465
|
+
let r = `optimistic-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
3466
|
+
return f.getState().patchMessages((e) => [...e, {
|
|
3467
|
+
id: r,
|
|
3468
|
+
role: "user",
|
|
3469
|
+
content: t,
|
|
3470
|
+
timestamp: Date.now(),
|
|
3471
|
+
eventType: "optimistic_user_message",
|
|
3472
|
+
attachments: n.length ? n : void 0
|
|
3473
|
+
}]), r;
|
|
3474
|
+
}, []), D = P(() => {
|
|
3475
|
+
let e = `optimistic-assistant-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
3476
|
+
return f.getState().patchMessages((t) => [...t, {
|
|
3477
|
+
id: e,
|
|
3478
|
+
role: "model",
|
|
3479
|
+
content: "",
|
|
3480
|
+
reasoning: "",
|
|
3481
|
+
timestamp: Date.now(),
|
|
3482
|
+
eventType: "optimistic_assistant_placeholder"
|
|
3483
|
+
}]), e;
|
|
3484
|
+
}, []), O = P((e) => {
|
|
3485
|
+
let t = _.current, i = w(t);
|
|
3486
|
+
if (i.stage !== "idle") return !1;
|
|
3487
|
+
vn(), s.getState().setMobileActionsOpen(!1), l.getState().setSessionStreaming(t, !0), e.optimisticMessageId || E(e), e.optimisticAssistantMessageId || D();
|
|
3488
|
+
let a = i.start({
|
|
3489
|
+
text: e.text,
|
|
3490
|
+
attachments: e.attachments,
|
|
3491
|
+
responsesInput: e.responsesInput,
|
|
3492
|
+
previousResponseId: e.previousResponseId,
|
|
3493
|
+
executionMode: e.executionMode,
|
|
3494
|
+
sessionId: t,
|
|
3495
|
+
onSessionCreated: (e) => {
|
|
3496
|
+
b ? b(e) : (h.getState().upsertSessions([{
|
|
3497
|
+
SessionId: e,
|
|
3498
|
+
UpdatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
3499
|
+
}]), _.current = e, h.getState().setCurrentSessionId(e)), Ve(o, e), t !== e && n.current.set(e, i);
|
|
3500
|
+
},
|
|
3501
|
+
onSessionUpsert: () => {},
|
|
3502
|
+
onSettled: (e) => {
|
|
3503
|
+
y?.(e), r.current();
|
|
3504
|
+
}
|
|
3505
|
+
});
|
|
3506
|
+
return a || l.getState().setSessionStreaming(t, !1), a;
|
|
3507
|
+
}, [
|
|
3508
|
+
o,
|
|
3509
|
+
D,
|
|
3510
|
+
E,
|
|
3511
|
+
_,
|
|
3512
|
+
w,
|
|
3513
|
+
y,
|
|
3514
|
+
b
|
|
3515
|
+
]);
|
|
3516
|
+
F(() => {
|
|
3517
|
+
r.current = () => {
|
|
3518
|
+
let e = v.current.shift();
|
|
3519
|
+
if (!e) {
|
|
3520
|
+
s.getState().setQueuedDrafts([]);
|
|
3521
|
+
return;
|
|
3522
|
+
}
|
|
3523
|
+
s.getState().setQueuedDrafts((e) => e.slice(1)), queueMicrotask(() => {
|
|
3524
|
+
O(e) || (v.current.unshift(e), s.getState().setQueuedDrafts((t) => [e, ...t]));
|
|
3525
|
+
});
|
|
3526
|
+
};
|
|
3527
|
+
}, [v, O]);
|
|
3528
|
+
let k = P(async (e, t, n, r, i) => {
|
|
3529
|
+
let a = {
|
|
3530
|
+
text: e,
|
|
3531
|
+
attachments: t,
|
|
3532
|
+
responsesInput: n,
|
|
3533
|
+
previousResponseId: r,
|
|
3534
|
+
executionMode: i,
|
|
3535
|
+
optimisticMessageId: E({
|
|
3536
|
+
text: e,
|
|
3537
|
+
attachments: t
|
|
3538
|
+
}),
|
|
3539
|
+
optimisticAssistantMessageId: D()
|
|
3540
|
+
};
|
|
3541
|
+
if (await x?.(), w(_.current).stage !== "idle" && l.getState().isSessionStreaming(_.current)) {
|
|
3542
|
+
n === void 0 && T({
|
|
3543
|
+
text: e,
|
|
3544
|
+
attachments: t,
|
|
3545
|
+
executionMode: i,
|
|
3546
|
+
optimisticMessageId: a.optimisticMessageId,
|
|
3547
|
+
optimisticAssistantMessageId: a.optimisticAssistantMessageId
|
|
3548
|
+
});
|
|
3549
|
+
return;
|
|
3550
|
+
}
|
|
3551
|
+
!O(a) && n === void 0 && T({
|
|
3552
|
+
text: e,
|
|
3553
|
+
attachments: t,
|
|
3554
|
+
executionMode: i,
|
|
3555
|
+
optimisticMessageId: a.optimisticMessageId,
|
|
3556
|
+
optimisticAssistantMessageId: a.optimisticAssistantMessageId
|
|
3557
|
+
});
|
|
3558
|
+
}, [
|
|
3559
|
+
D,
|
|
3560
|
+
E,
|
|
3561
|
+
_,
|
|
3562
|
+
T,
|
|
3563
|
+
w,
|
|
3564
|
+
O,
|
|
3565
|
+
x
|
|
3566
|
+
]), A = P(() => {
|
|
3567
|
+
w(_.current).stop();
|
|
3568
|
+
}, [_, w]), j = P(() => {
|
|
3569
|
+
w(_.current).disconnect();
|
|
3570
|
+
}, [_, w]), M = P((e) => {
|
|
3571
|
+
let t = w(e.sessionId);
|
|
3572
|
+
if (t.stage !== "idle") return !1;
|
|
3573
|
+
vn(), s.getState().setMobileActionsOpen(!1), l.getState().setSessionStreaming(e.sessionId, !0);
|
|
3574
|
+
let n = t.resumeCheckpoint({
|
|
3575
|
+
...e,
|
|
3576
|
+
onSettled: (e) => {
|
|
3577
|
+
y?.(e), r.current();
|
|
3578
|
+
}
|
|
3579
|
+
});
|
|
3580
|
+
return n || l.getState().setSessionStreaming(e.sessionId, !1), n;
|
|
3581
|
+
}, [w, y]), te = P(() => {
|
|
3582
|
+
vn();
|
|
3583
|
+
}, []);
|
|
3584
|
+
return {
|
|
3585
|
+
submitDraft: k,
|
|
3586
|
+
stopGeneration: A,
|
|
3587
|
+
disconnectRun: j,
|
|
3588
|
+
resumeCheckpoint: M,
|
|
3589
|
+
submitAguiAction: P((e) => {
|
|
3590
|
+
let t = e.userAction;
|
|
3591
|
+
if (!t) return !1;
|
|
3592
|
+
let n = t.context || {}, r = String(n.interruptId || n.interrupt_id || "");
|
|
3593
|
+
if (!r) return !1;
|
|
3594
|
+
let i = _.current;
|
|
3595
|
+
if (!i) return !1;
|
|
3596
|
+
let a = w(i), o = n.status === "cancelled" ? "cancelled" : "resolved", s = Object.prototype.hasOwnProperty.call(n, "payload") ? n.payload : {
|
|
3597
|
+
action: t.name,
|
|
3598
|
+
sourceComponentId: t.sourceComponentId,
|
|
3599
|
+
context: n
|
|
3600
|
+
};
|
|
3601
|
+
return a.resumeAguiInterrupt({
|
|
3602
|
+
sessionId: i,
|
|
3603
|
+
interruptId: r,
|
|
3604
|
+
status: o,
|
|
3605
|
+
payload: s,
|
|
3606
|
+
onSettled: y
|
|
3607
|
+
});
|
|
3608
|
+
}, [
|
|
3609
|
+
_,
|
|
3610
|
+
w,
|
|
3611
|
+
y
|
|
3612
|
+
]),
|
|
3613
|
+
respondToAguiApproval: P((e) => {
|
|
3614
|
+
if (!e.interruptId) return !1;
|
|
3615
|
+
let t = _.current;
|
|
3616
|
+
if (!t) return !1;
|
|
3617
|
+
let n = w(t);
|
|
3618
|
+
if (n.stage !== "idle") return !1;
|
|
3619
|
+
l.getState().setSessionStreaming(t, !0);
|
|
3620
|
+
let r = n.resumeAguiInterrupt({
|
|
3621
|
+
sessionId: t,
|
|
3622
|
+
interruptId: e.interruptId,
|
|
3623
|
+
status: "resolved",
|
|
3624
|
+
payload: { decision: e.approve ? "approve" : "reject" },
|
|
3625
|
+
onSettled: y
|
|
3626
|
+
});
|
|
3627
|
+
return r || l.getState().setSessionStreaming(t, !1), r;
|
|
3628
|
+
}, [
|
|
3629
|
+
_,
|
|
3630
|
+
w,
|
|
3631
|
+
y
|
|
3632
|
+
]),
|
|
3633
|
+
resetCompaction: te
|
|
3634
|
+
};
|
|
3635
|
+
}
|
|
3636
|
+
//#endregion
|
|
3637
|
+
//#region src/utils/messages.js
|
|
3638
|
+
var bn = {
|
|
3639
|
+
running: "running",
|
|
3640
|
+
completed: "completed",
|
|
3641
|
+
failed: "error",
|
|
3642
|
+
paused: "paused",
|
|
3643
|
+
approved: "completed",
|
|
3644
|
+
denied: "completed"
|
|
3645
|
+
}, xn = {
|
|
3646
|
+
paused: "pending",
|
|
3647
|
+
approved: "approved",
|
|
3648
|
+
denied: "rejected"
|
|
3649
|
+
};
|
|
3650
|
+
function Sn(e) {
|
|
3651
|
+
if (typeof e == "number" && Number.isFinite(e)) return e > 0 && e < 1e11 ? e * 1e3 : e;
|
|
3652
|
+
if (typeof e == "string") {
|
|
3653
|
+
let t = Number(e.trim());
|
|
3654
|
+
if (e.trim() && Number.isFinite(t)) return t > 0 && t < 1e11 ? t * 1e3 : t;
|
|
3655
|
+
let n = Date.parse(e);
|
|
3656
|
+
if (Number.isFinite(n)) return n;
|
|
3657
|
+
}
|
|
3658
|
+
return Date.now();
|
|
3659
|
+
}
|
|
3660
|
+
function Cn(e) {
|
|
3661
|
+
if (typeof e == "string") return e;
|
|
3662
|
+
if (e && typeof e == "object") {
|
|
3663
|
+
if (typeof e.text == "string") return e.text;
|
|
3664
|
+
if (Array.isArray(e.parts)) return e.parts.map((e) => e && typeof e == "object" && typeof e.text == "string" ? e.text : "").join("");
|
|
3665
|
+
}
|
|
3666
|
+
return "";
|
|
3667
|
+
}
|
|
3668
|
+
function wn(e) {
|
|
3669
|
+
let t = {}, n = /* @__PURE__ */ new Map();
|
|
3670
|
+
for (let t of e ?? []) t?.Name && n.set(t.Name, (n.get(t.Name) || 0) + 1);
|
|
3671
|
+
for (let r of e ?? []) {
|
|
3672
|
+
if (!r?.Name) continue;
|
|
3673
|
+
let e = bn[String(r.Status ?? "completed").toLowerCase()] ?? "completed", i = {
|
|
3674
|
+
name: r.Name,
|
|
3675
|
+
args: r.Args === void 0 ? "" : typeof r.Args == "string" ? r.Args : JSON.stringify(r.Args),
|
|
3676
|
+
status: e
|
|
3677
|
+
};
|
|
3678
|
+
r.Result !== void 0 && (i.output = typeof r.Result == "string" ? r.Result : JSON.stringify(r.Result)), r.ApprovalRequestId && (i.approvalRequestId = r.ApprovalRequestId, i.approvalStatus = xn[String(r.Status ?? "paused").toLowerCase()] ?? "pending", i.approvalProtocol = String(r.Protocol ?? "").toLowerCase() === "ag-ui" ? "ag-ui" : "responses", r.ApprovalMessage && (i.approvalMessage = String(r.ApprovalMessage)), r.ApprovalLevel && (i.approvalLevel = String(r.ApprovalLevel))), r.ToolCallId && (i.previousResponseId = r.ToolCallId);
|
|
3679
|
+
let a = n.get(r.Name) > 1 && r.ToolCallId ? r.ToolCallId : r.Name;
|
|
3680
|
+
t[a] = i;
|
|
3681
|
+
}
|
|
3682
|
+
return t;
|
|
3683
|
+
}
|
|
3684
|
+
function Tn(e) {
|
|
3685
|
+
if (Array.isArray(e)) return e.map((e) => ({
|
|
3686
|
+
name: e.name || "",
|
|
3687
|
+
url: e.url || "",
|
|
3688
|
+
type: e.mime || "",
|
|
3689
|
+
fileUri: e.file_uri
|
|
3690
|
+
}));
|
|
3691
|
+
}
|
|
3692
|
+
function En(e) {
|
|
3693
|
+
return e == null ? "" : typeof e == "string" ? e : JSON.stringify(e);
|
|
3694
|
+
}
|
|
3695
|
+
function Dn(e) {
|
|
3696
|
+
if (!(!Array.isArray(e) || !e.length)) return e.flatMap((e, t) => {
|
|
3697
|
+
let n = String(e?.Type || "").toLowerCase(), r = e?.SeqId ?? t;
|
|
3698
|
+
if (n === "thinking" || n === "text") {
|
|
3699
|
+
let t = String(e?.Content || "");
|
|
3700
|
+
return t ? [{
|
|
3701
|
+
id: `history-${n}-${r}`,
|
|
3702
|
+
type: n,
|
|
3703
|
+
content: t,
|
|
3704
|
+
status: "done"
|
|
3705
|
+
}] : [];
|
|
3706
|
+
}
|
|
3707
|
+
if (n !== "tool") return [];
|
|
3708
|
+
let i = bn[String(e?.Status || "completed").toLowerCase()] ?? "completed";
|
|
3709
|
+
return [{
|
|
3710
|
+
id: `history-tool-${r}`,
|
|
3711
|
+
type: "tool",
|
|
3712
|
+
toolName: String(e?.Name || "tool"),
|
|
3713
|
+
args: En(e?.Args),
|
|
3714
|
+
...e?.Result === void 0 ? {} : { output: En(e.Result) },
|
|
3715
|
+
status: i,
|
|
3716
|
+
...e?.ToolCallId ? { extra: { previousResponseId: String(e.ToolCallId) } } : {}
|
|
3717
|
+
}];
|
|
3718
|
+
});
|
|
3719
|
+
}
|
|
3720
|
+
function On(e) {
|
|
3721
|
+
let t = e.Role === "assistant" ? "model" : e.Role || "user", n = e.ToolEvents?.length ? wn(e.ToolEvents) : void 0, r = Tn(e.Attachments), i = Dn(e.Blocks), a = e.Reasoning?.length ? e.Reasoning.map((e) => e.text).join("") : void 0, o = {
|
|
3722
|
+
id: e.MessageId || `msg-${e.SeqId ?? Math.random().toString(36).slice(2)}`,
|
|
3723
|
+
role: t,
|
|
3724
|
+
content: Cn(e.Content),
|
|
3725
|
+
timestamp: Sn(e.Timestamp),
|
|
3726
|
+
eventType: t === "user" ? "user_message" : "assistant_message",
|
|
3727
|
+
reasoning: a,
|
|
3728
|
+
tools: n,
|
|
3729
|
+
attachments: r
|
|
3730
|
+
};
|
|
3731
|
+
return t === "model" && (o.blocks = i ?? re({
|
|
3732
|
+
reasoning: a,
|
|
3733
|
+
tools: n,
|
|
3734
|
+
content: o.content
|
|
3735
|
+
})), e.MessageId && (o.eventId = e.MessageId), e.InvocationId && (o.invocationId = e.InvocationId), e.ResponseId && (o.responseId = e.ResponseId), e.TraceId && (o.traceId = e.TraceId), e.RootSpanId && (o.rootSpanId = e.RootSpanId), o;
|
|
3736
|
+
}
|
|
3737
|
+
function kn(e) {
|
|
3738
|
+
return Array.isArray(e.Activities) ? e.Activities.flatMap((t, n) => {
|
|
3739
|
+
let r = String(t?.SurfaceId || "");
|
|
3740
|
+
if (!r) return [];
|
|
3741
|
+
let i = t?.Content, a = Array.isArray(i?.a2ui_operations) ? i.a2ui_operations : Array.isArray(i) ? i : [];
|
|
3742
|
+
if (!a.length || r.startsWith("approval-") && a.some((e) => {
|
|
3743
|
+
let t = e?.updateComponents?.components || e?.update_components?.components;
|
|
3744
|
+
return Array.isArray(t) && t.some((e) => e?.component === "ApprovalBar");
|
|
3745
|
+
})) return [];
|
|
3746
|
+
let o = String(t.MessageId || "");
|
|
3747
|
+
return [{
|
|
3748
|
+
id: o && o !== String(e.MessageId || "") ? o : `${o || e.MessageId || e.SeqId || "message"}:a2ui:${r}:${n}`,
|
|
3749
|
+
role: "a2ui",
|
|
3750
|
+
content: "",
|
|
3751
|
+
timestamp: Sn(e.Timestamp),
|
|
3752
|
+
aguiActivity: {
|
|
3753
|
+
surfaceId: r,
|
|
3754
|
+
messages: c(a)
|
|
3755
|
+
}
|
|
3756
|
+
}];
|
|
3757
|
+
}) : [];
|
|
3758
|
+
}
|
|
3759
|
+
function An(e) {
|
|
3760
|
+
return e.flatMap((e) => [On(e), ...kn(e)]);
|
|
3761
|
+
}
|
|
3762
|
+
//#endregion
|
|
3763
|
+
//#region src/utils/session-event-history.ts
|
|
3764
|
+
var jn = class extends Error {
|
|
3765
|
+
seq;
|
|
3766
|
+
constructor(e) {
|
|
3767
|
+
super(`Session event seq ${e} was received twice with conflicting content`), this.name = "SessionEventConflictError", this.seq = e;
|
|
3768
|
+
}
|
|
3769
|
+
};
|
|
3770
|
+
function Mn(e) {
|
|
3771
|
+
return Array.isArray(e) ? `[${e.map(Mn).join(",")}]` : e && typeof e == "object" ? `{${Object.entries(e).filter(([, e]) => e !== void 0).sort(([e], [t]) => e < t ? -1 : +(e > t)).map(([e, t]) => `${JSON.stringify(e)}:${Mn(t)}`).join(",")}}` : JSON.stringify(e) ?? "null";
|
|
3772
|
+
}
|
|
3773
|
+
var Nn = new Set(["runtime"]), Pn = class {
|
|
3774
|
+
eventsBySeq = /* @__PURE__ */ new Map();
|
|
3775
|
+
lastSeqValue = 0;
|
|
3776
|
+
accept(e) {
|
|
3777
|
+
let t = Ne(e);
|
|
3778
|
+
if (!t.ok) throw t.error;
|
|
3779
|
+
let n = t.value, r = this.eventsBySeq.get(n.seq);
|
|
3780
|
+
if (r) {
|
|
3781
|
+
if (Mn(r) !== Mn(n)) throw new jn(n.seq);
|
|
3782
|
+
return;
|
|
3783
|
+
}
|
|
3784
|
+
this.eventsBySeq.set(n.seq, n), this.lastSeqValue = Math.max(this.lastSeqValue, n.seq);
|
|
3785
|
+
}
|
|
3786
|
+
get lastSeq() {
|
|
3787
|
+
return this.lastSeqValue;
|
|
3788
|
+
}
|
|
3789
|
+
reconnectAfterSeq() {
|
|
3790
|
+
return this.lastSeqValue;
|
|
3791
|
+
}
|
|
3792
|
+
displayableEvents() {
|
|
3793
|
+
return [...this.eventsBySeq.values()].filter((e) => Nn.has(e.family)).sort((e, t) => e.seq - t.seq);
|
|
3794
|
+
}
|
|
3795
|
+
};
|
|
3796
|
+
function Fn() {
|
|
3797
|
+
return new Pn();
|
|
3798
|
+
}
|
|
3799
|
+
//#endregion
|
|
3800
|
+
//#region src/hooks/useSessionLifecycle.ts
|
|
3801
|
+
var In = 500, Ln = 30, Rn = 50, zn = 200, Bn = 8, Vn = 1800 * 1e3;
|
|
3802
|
+
function Hn(e) {
|
|
3803
|
+
return new Promise((t) => {
|
|
3804
|
+
if (e.aborted) {
|
|
3805
|
+
t();
|
|
3806
|
+
return;
|
|
3807
|
+
}
|
|
3808
|
+
let n = globalThis.setTimeout(t, In);
|
|
3809
|
+
e.addEventListener("abort", () => {
|
|
3810
|
+
globalThis.clearTimeout(n), t();
|
|
3811
|
+
}, { once: !0 });
|
|
3812
|
+
});
|
|
3813
|
+
}
|
|
3814
|
+
var Un = new Set([
|
|
3815
|
+
"in_progress",
|
|
3816
|
+
"running",
|
|
3817
|
+
"resuming",
|
|
3818
|
+
"starting"
|
|
3819
|
+
]);
|
|
3820
|
+
function Wn(e) {
|
|
3821
|
+
let t = e.ActiveRunUpdatedAt || e.UpdatedAt, n = typeof t == "number" ? t > 1e11 ? t : t * 1e3 : Date.parse(String(t || ""));
|
|
3822
|
+
return Number.isFinite(n) && n <= Date.now() + Vn && Date.now() - n <= Vn;
|
|
3823
|
+
}
|
|
3824
|
+
function Gn(e) {
|
|
3825
|
+
let t = j(e);
|
|
3826
|
+
return t ? t === "completed" ? {
|
|
3827
|
+
status: "completed",
|
|
3828
|
+
phase: "后台长任务已完成"
|
|
3829
|
+
} : t === "cancelled" || t === "canceled" || t === "aborted" ? {
|
|
3830
|
+
status: "stopped",
|
|
3831
|
+
phase: "后台长任务已取消"
|
|
3832
|
+
} : t === "interrupted" ? {
|
|
3833
|
+
status: "stopped",
|
|
3834
|
+
phase: "后台长任务已中断"
|
|
3835
|
+
} : t === "failed" || t === "error" ? {
|
|
3836
|
+
status: "failed",
|
|
3837
|
+
phase: "后台长任务失败"
|
|
3838
|
+
} : t === "resume_failed" ? {
|
|
3839
|
+
status: "failed",
|
|
3840
|
+
phase: "后台长任务恢复失败"
|
|
3841
|
+
} : null : null;
|
|
3842
|
+
}
|
|
3843
|
+
function Kn(e) {
|
|
3844
|
+
let { agentId: t, api: n, isMobile: r, resetCompaction: i, uiCapabilities: o, disconnectRun: c } = e, u = I(e.currentSessionId), d = I(e.agentId), p = I(null), m = I(Fn()), g = I(0), _ = I(/* @__PURE__ */ new Map()), y = I(/* @__PURE__ */ new Map()), x = I(/* @__PURE__ */ new Map()), C = I(/* @__PURE__ */ new Map()), w = I(/* @__PURE__ */ new Map()), T = I(/* @__PURE__ */ new Map()), E = I(null), D = I(null), O = I(null), k = P(async (e, t, r) => {
|
|
3845
|
+
let i = r.filter((e) => v(e, !1, !1));
|
|
3846
|
+
if (!i.length) return;
|
|
3847
|
+
let a = await Promise.all(i.map(async (r) => {
|
|
3848
|
+
try {
|
|
3849
|
+
let i = await n.getResponseFeedback({
|
|
3850
|
+
AgentId: e,
|
|
3851
|
+
SessionId: t,
|
|
3852
|
+
ResponseId: r.responseId,
|
|
3853
|
+
EventId: r.eventId
|
|
3854
|
+
}), a = i?.Feedback ? S(i.Feedback) : null;
|
|
3855
|
+
return a ? {
|
|
3856
|
+
messageId: r.id,
|
|
3857
|
+
feedback: a
|
|
3858
|
+
} : null;
|
|
3859
|
+
} catch (e) {
|
|
3860
|
+
return console.error("Failed to load response feedback:", e), null;
|
|
3861
|
+
}
|
|
3862
|
+
}));
|
|
3863
|
+
if (u.current !== t) return;
|
|
3864
|
+
let o = new Map(a.filter((e) => !!e).map((e) => [e.messageId, e.feedback]));
|
|
3865
|
+
o.size && f.getState().patchMessages((e) => e.map((e) => o.has(e.id) ? {
|
|
3866
|
+
...e,
|
|
3867
|
+
feedback: o.get(e.id)
|
|
3868
|
+
} : e));
|
|
3869
|
+
}, [n]), ee = P(async (e) => {
|
|
3870
|
+
p.current?.abort();
|
|
3871
|
+
let t = new AbortController();
|
|
3872
|
+
p.current = t;
|
|
3873
|
+
let r = !1, i = !1, a = e.afterSeqId, o = () => p.current === t && u.current === e.sessionId;
|
|
3874
|
+
try {
|
|
3875
|
+
for (l.getState().setCurrentRunId(e.invocationId), l.getState().setActiveInvocationId(e.invocationId), l.getState().setSessionStreaming(e.sessionId, !0), l.getState().updateActivity({
|
|
3876
|
+
sessionId: e.sessionId,
|
|
3877
|
+
status: "running",
|
|
3878
|
+
phase: "后台长任务运行中",
|
|
3879
|
+
detail: e.invocationId,
|
|
3880
|
+
countEvent: !1
|
|
3881
|
+
}); !i && o();) {
|
|
3882
|
+
try {
|
|
3883
|
+
let s = await n.subscribeRunEvents({
|
|
3884
|
+
sessionId: e.sessionId,
|
|
3885
|
+
invocationId: e.invocationId,
|
|
3886
|
+
afterSeqId: a
|
|
3887
|
+
}, { signal: t.signal });
|
|
3888
|
+
if (!o()) {
|
|
3889
|
+
t.abort();
|
|
3890
|
+
return;
|
|
3891
|
+
}
|
|
3892
|
+
let c = s.getReader(), u = new TextDecoder(), d = "";
|
|
3893
|
+
for (; !i && o();) {
|
|
3894
|
+
let { value: t, done: n } = await c.read();
|
|
3895
|
+
if (n) break;
|
|
3896
|
+
d += u.decode(t, { stream: !0 });
|
|
3897
|
+
let s = X(d);
|
|
3898
|
+
d = s.remainder;
|
|
3899
|
+
for (let t of s.chunks) if (t.trim()) {
|
|
3900
|
+
for (let n of Y(t)) {
|
|
3901
|
+
if (n.eventName === "__ping__") {
|
|
3902
|
+
l.getState().updateActivity({
|
|
3903
|
+
sessionId: e.sessionId,
|
|
3904
|
+
status: "running",
|
|
3905
|
+
countEvent: !1
|
|
3906
|
+
});
|
|
3907
|
+
continue;
|
|
3908
|
+
}
|
|
3909
|
+
if (n.eventName === "__done__") {
|
|
3910
|
+
i = !0, r = !0;
|
|
3911
|
+
break;
|
|
3912
|
+
}
|
|
3913
|
+
if (!n.data || typeof n.data != "object") continue;
|
|
3914
|
+
let t = n.data;
|
|
3915
|
+
if (!o()) break;
|
|
3916
|
+
if (t.InvocationId && t.InvocationId !== e.invocationId) continue;
|
|
3917
|
+
if (typeof t.seq == "number") {
|
|
3918
|
+
try {
|
|
3919
|
+
m.current.accept(t);
|
|
3920
|
+
} catch (e) {
|
|
3921
|
+
console.error("[SessionLifecycle] session event cursor conflict:", e);
|
|
3922
|
+
}
|
|
3923
|
+
a = Math.max(a, m.current.reconnectAfterSeq()), _n({
|
|
3924
|
+
type: "stream_event",
|
|
3925
|
+
sessionId: e.sessionId,
|
|
3926
|
+
event: t
|
|
3927
|
+
}), i ||= A(t), r ||= i;
|
|
3928
|
+
let n = Gn(t);
|
|
3929
|
+
n && l.getState().updateActivity({
|
|
3930
|
+
sessionId: e.sessionId,
|
|
3931
|
+
status: n.status,
|
|
3932
|
+
phase: n.phase,
|
|
3933
|
+
detail: e.invocationId,
|
|
3934
|
+
countEvent: !1
|
|
3935
|
+
});
|
|
3936
|
+
continue;
|
|
3937
|
+
}
|
|
3938
|
+
let s = Number(t.SeqId || 0);
|
|
3939
|
+
Number.isFinite(s) && (a = Math.max(a, s)), _n({
|
|
3940
|
+
type: "stream_event",
|
|
3941
|
+
sessionId: e.sessionId,
|
|
3942
|
+
event: t
|
|
3943
|
+
}), i ||= A(t), r ||= i;
|
|
3944
|
+
let c = Gn(t);
|
|
3945
|
+
if (c && l.getState().updateActivity({
|
|
3946
|
+
sessionId: e.sessionId,
|
|
3947
|
+
status: c.status,
|
|
3948
|
+
phase: c.phase,
|
|
3949
|
+
detail: e.invocationId,
|
|
3950
|
+
countEvent: !1
|
|
3951
|
+
}), i) break;
|
|
3952
|
+
}
|
|
3953
|
+
if (i) break;
|
|
3954
|
+
}
|
|
3955
|
+
}
|
|
3956
|
+
i && c.cancel().catch(() => {});
|
|
3957
|
+
} catch (t) {
|
|
3958
|
+
if (t instanceof DOMException && t.name === "AbortError" || !o()) break;
|
|
3959
|
+
console.warn("Run event subscription disconnected; retrying:", t), l.getState().updateActivity({
|
|
3960
|
+
sessionId: e.sessionId,
|
|
3961
|
+
status: "waiting",
|
|
3962
|
+
phase: "恢复连接中",
|
|
3963
|
+
detail: e.invocationId,
|
|
3964
|
+
countEvent: !1
|
|
3965
|
+
});
|
|
3966
|
+
}
|
|
3967
|
+
!i && o() && await Hn(t.signal);
|
|
3968
|
+
}
|
|
3969
|
+
i && o() && _n({
|
|
3970
|
+
type: "stream_ended",
|
|
3971
|
+
sessionId: e.sessionId
|
|
3972
|
+
});
|
|
3973
|
+
} catch (e) {
|
|
3974
|
+
e instanceof DOMException && e.name === "AbortError" || console.error("Failed to subscribe run events:", e);
|
|
3975
|
+
} finally {
|
|
3976
|
+
let n = p.current === t;
|
|
3977
|
+
n && (p.current = null), n && u.current === e.sessionId && (l.getState().setCurrentRunId(""), l.getState().setActiveInvocationId(""), r && D.current?.(e.sessionId), O.current?.(d.current, e.sessionId));
|
|
3978
|
+
}
|
|
3979
|
+
}, [n]), j = P(async (e) => {
|
|
3980
|
+
let t = u.current, a = ++g.current, c = (e, t) => {
|
|
3981
|
+
let n = x.current;
|
|
3982
|
+
for (n.delete(e), n.set(e, t); n.size > Bn;) {
|
|
3983
|
+
let e = n.keys().next().value;
|
|
3984
|
+
if (!e) break;
|
|
3985
|
+
n.delete(e);
|
|
3986
|
+
}
|
|
3987
|
+
}, m = x.current.get(e);
|
|
3988
|
+
t && t !== e && (c(t, f.getState().messages), f.getState().setMessages(m || []), h.getState().clearSessionMessageHistory(e), b.getState().setSessionCheckpoints(e, []), b.getState().setSessionToolReceipts(e, []), l.getState().setCurrentRunId(""), l.getState().clearActivity()), u.current = e;
|
|
3989
|
+
let _ = () => u.current === e && g.current === a;
|
|
3990
|
+
h.getState().setCurrentSessionId(e), h.getState().setSessionInitialMessageHistoryLoading(e, !m), i(), p.current?.abort(), r && s.getState().setMobileSidebarOpen(!1);
|
|
3991
|
+
try {
|
|
3992
|
+
let t = {
|
|
3993
|
+
limit: Rn,
|
|
3994
|
+
includeReasoning: !0,
|
|
3995
|
+
includeToolEvents: !0,
|
|
3996
|
+
includeAttachments: !0
|
|
3997
|
+
}, r;
|
|
3998
|
+
try {
|
|
3999
|
+
r = await n.listSessionMessages(e, t);
|
|
4000
|
+
} catch (i) {
|
|
4001
|
+
if (i instanceof z) throw i;
|
|
4002
|
+
if (await new Promise((e) => globalThis.setTimeout(e, 180)), !_()) return;
|
|
4003
|
+
r = await n.listSessionMessages(e, t);
|
|
4004
|
+
}
|
|
4005
|
+
if (!_()) return;
|
|
4006
|
+
let i = An(r.Messages);
|
|
4007
|
+
C.current.set(e, i);
|
|
4008
|
+
let a = i, s = 0, l = [];
|
|
4009
|
+
m || (f.getState().setMessages(i), c(e, i)), h.getState().setSessionMessageHistory(e, {
|
|
4010
|
+
nextCursor: r.NextCursor,
|
|
4011
|
+
hasMore: r.HasMore
|
|
4012
|
+
}), h.getState().setSessionInitialMessageHistoryLoading(e, !1);
|
|
4013
|
+
try {
|
|
4014
|
+
let t = await n.listSessionEvents(e, {
|
|
4015
|
+
offset: 0,
|
|
4016
|
+
limit: zn
|
|
4017
|
+
});
|
|
4018
|
+
if (!_()) return;
|
|
4019
|
+
let o = {
|
|
4020
|
+
events: t.Events || [],
|
|
4021
|
+
loadedCount: t.Events?.length || 0,
|
|
4022
|
+
total: Math.max(0, Number(t.Total ?? t.Events?.length ?? 0) || 0)
|
|
4023
|
+
};
|
|
4024
|
+
if (w.current.set(e, o), h.getState().setSessionMessageHistory(e, {
|
|
4025
|
+
nextCursor: r.NextCursor,
|
|
4026
|
+
hasMore: !!(r.HasMore || o.loadedCount < o.total)
|
|
4027
|
+
}), o.events.length > 0) {
|
|
4028
|
+
s = le(o.events);
|
|
4029
|
+
let t = ce(i, o.events, e);
|
|
4030
|
+
l = t.canonicalRunIds, a = t.messages;
|
|
4031
|
+
for (let t of o.events) lt(t, e);
|
|
4032
|
+
}
|
|
4033
|
+
} catch (e) {
|
|
4034
|
+
console.warn("[SessionLifecycle] canonical history replay failed:", e);
|
|
4035
|
+
}
|
|
4036
|
+
if (!_()) return;
|
|
4037
|
+
y.current.set(e, new Set(l)), f.getState().setMessages(a), c(e, a), k(d.current, e, a);
|
|
4038
|
+
let u = Math.max(r.LatestSeqId || 0, s), p = U.getState().capabilities || o;
|
|
4039
|
+
if (p.RunLifecycle.Enabled && p.RunLifecycle.Checkpoints ? (n.listSessionCheckpoints({
|
|
4040
|
+
agentId: d.current,
|
|
4041
|
+
sessionId: e
|
|
4042
|
+
}).then((t) => {
|
|
4043
|
+
_() && b.getState().setSessionCheckpoints(e, t.Checkpoints || []);
|
|
4044
|
+
}).catch((t) => {
|
|
4045
|
+
_() && (console.warn("[SessionLifecycle] checkpoint load failed:", t), b.getState().setSessionCheckpoints(e, []));
|
|
4046
|
+
}), n.listToolReceipts({
|
|
4047
|
+
agentId: d.current,
|
|
4048
|
+
sessionId: e
|
|
4049
|
+
}).then((t) => {
|
|
4050
|
+
_() && b.getState().setSessionToolReceipts(e, t.ToolReceipts || []);
|
|
4051
|
+
}).catch((t) => {
|
|
4052
|
+
_() && (console.warn("[SessionLifecycle] tool receipt load failed:", t), b.getState().setSessionToolReceipts(e, []));
|
|
4053
|
+
})) : b.getState().clearSessionCheckpoints(e), p.RunLifecycle.Enabled && p.RunLifecycle.Resume) try {
|
|
4054
|
+
let t = await n.getSession(e);
|
|
4055
|
+
if (!_()) return;
|
|
4056
|
+
let r = String(t.ActiveRunStatus || "").toLowerCase();
|
|
4057
|
+
t.ActiveInvocationId && (Un.has(r) || r === "" && Wn(t)) && ee({
|
|
4058
|
+
sessionId: e,
|
|
4059
|
+
invocationId: t.ActiveInvocationId,
|
|
4060
|
+
afterSeqId: u
|
|
4061
|
+
});
|
|
4062
|
+
} catch (e) {
|
|
4063
|
+
console.warn("[SessionLifecycle] getSession for reconnect failed:", e);
|
|
4064
|
+
}
|
|
4065
|
+
} catch (e) {
|
|
4066
|
+
console.error("Failed to load session messages:", e);
|
|
4067
|
+
} finally {
|
|
4068
|
+
_() && h.getState().setSessionInitialMessageHistoryLoading(e, !1);
|
|
4069
|
+
}
|
|
4070
|
+
}, [
|
|
4071
|
+
n,
|
|
4072
|
+
r,
|
|
4073
|
+
k,
|
|
4074
|
+
i,
|
|
4075
|
+
ee,
|
|
4076
|
+
o
|
|
4077
|
+
]), M = P(async (e = "default-agent", t = null) => {
|
|
4078
|
+
try {
|
|
4079
|
+
let r = h.getState();
|
|
4080
|
+
r.sessionsAgentId && r.sessionsAgentId !== e && r.resetSessionPagination(e), h.getState().setLoadingSessions(!0);
|
|
4081
|
+
let i = await n.listSessions(e, {
|
|
4082
|
+
page: 1,
|
|
4083
|
+
pageSize: Ln
|
|
4084
|
+
}), a = i.Sessions || [];
|
|
4085
|
+
for (let e of a) T.current.delete(e.SessionId);
|
|
4086
|
+
let o = new Set(Array.from(T.current.entries()).filter(([, t]) => t === e).map(([e]) => e)), s = Ue(a, h.getState().sessions, o);
|
|
4087
|
+
if (h.getState().upsertSessions(s, {
|
|
4088
|
+
agentId: e,
|
|
4089
|
+
total: Number(i.Total ?? i.Sessions?.length ?? 0),
|
|
4090
|
+
page: Number(i.Page ?? 1),
|
|
4091
|
+
pageSize: Number(i.PageSize ?? Ln),
|
|
4092
|
+
replace: !0
|
|
4093
|
+
}), E.current) return;
|
|
4094
|
+
let d = h.getState().sessions, m = u.current, _ = He(d, m || t || Be(e));
|
|
4095
|
+
_ && _ !== m ? j(_) : !_ && m && (g.current += 1, p.current?.abort(), c?.(), u.current = null, h.getState().setCurrentSessionId(null), f.getState().setMessages([]), h.getState().clearSessionMessageHistory(), b.getState().clearSessionCheckpoints(), l.getState().setCurrentRunId(""), l.getState().clearActivity());
|
|
4096
|
+
} catch (e) {
|
|
4097
|
+
if (e instanceof z) return;
|
|
4098
|
+
console.error("Failed to fetch sessions:", e);
|
|
4099
|
+
} finally {
|
|
4100
|
+
h.getState().setLoadingSessions(!1);
|
|
4101
|
+
}
|
|
4102
|
+
}, [
|
|
4103
|
+
n,
|
|
4104
|
+
c,
|
|
4105
|
+
j
|
|
4106
|
+
]), te = P(async () => {
|
|
4107
|
+
let e = h.getState();
|
|
4108
|
+
if (e.isLoadingSessions || !e.hasMoreSessions) return;
|
|
4109
|
+
let t = a({
|
|
4110
|
+
total: e.sessionsTotal,
|
|
4111
|
+
pageSize: e.sessionsPageSize || Ln,
|
|
4112
|
+
loadedPages: e.loadedPages
|
|
4113
|
+
});
|
|
4114
|
+
if (!t) return;
|
|
4115
|
+
let r = e.sessionsPageSize || Ln, i = e.sessionsAgentId || d.current || "default-agent";
|
|
4116
|
+
try {
|
|
4117
|
+
h.getState().setLoadingSessions(!0);
|
|
4118
|
+
let a = await n.listSessions(i, {
|
|
4119
|
+
page: t,
|
|
4120
|
+
pageSize: r
|
|
4121
|
+
});
|
|
4122
|
+
h.getState().upsertSessions(a.Sessions || [], {
|
|
4123
|
+
agentId: i,
|
|
4124
|
+
total: Number(a.Total ?? e.sessionsTotal),
|
|
4125
|
+
page: Number(a.Page ?? t),
|
|
4126
|
+
pageSize: Number(a.PageSize ?? r)
|
|
4127
|
+
});
|
|
4128
|
+
} catch (e) {
|
|
4129
|
+
if (e instanceof z) return;
|
|
4130
|
+
console.error("Failed to load more sessions:", e);
|
|
4131
|
+
} finally {
|
|
4132
|
+
h.getState().setLoadingSessions(!1);
|
|
4133
|
+
}
|
|
4134
|
+
}, [n]);
|
|
4135
|
+
F(() => {
|
|
4136
|
+
D.current = j;
|
|
4137
|
+
}, [j]), F(() => {
|
|
4138
|
+
O.current = M;
|
|
4139
|
+
}, [M]);
|
|
4140
|
+
let ne = P((e, t = !1) => {
|
|
4141
|
+
g.current += 1, p.current?.abort(), T.current.set(e, d.current), h.getState().upsertSessions([{
|
|
4142
|
+
SessionId: e,
|
|
4143
|
+
UpdatedAt: (/* @__PURE__ */ new Date()).toISOString()
|
|
4144
|
+
}]), u.current = e, h.getState().setCurrentSessionId(e), t || (f.getState().setMessages([]), l.getState().setCurrentRunId(""), l.getState().clearActivity()), h.getState().clearSessionMessageHistory(e), b.getState().setSessionCheckpoints(e, []), b.getState().setSessionToolReceipts(e, []), r && (s.getState().setMobileSidebarOpen(!1), s.getState().setMobileActionsOpen(!1));
|
|
4145
|
+
}, [r]), re = P(async () => {
|
|
4146
|
+
g.current += 1, p.current?.abort(), u.current = null, h.getState().setCurrentSessionId(null), f.getState().setMessages([]);
|
|
4147
|
+
let e = n.createSession(t).then((e) => {
|
|
4148
|
+
let t = e.SessionId || null;
|
|
4149
|
+
return t && ne(t, f.getState().messages.some((e) => e.eventType === "optimistic_user_message" || e.eventType === "optimistic_assistant_placeholder")), t;
|
|
4150
|
+
}).catch((e) => (e instanceof z || console.error("Failed to create session:", e), null));
|
|
4151
|
+
E.current = e;
|
|
4152
|
+
try {
|
|
4153
|
+
await e;
|
|
4154
|
+
} finally {
|
|
4155
|
+
E.current === e && (E.current = null);
|
|
4156
|
+
}
|
|
4157
|
+
}, [
|
|
4158
|
+
ne,
|
|
4159
|
+
t,
|
|
4160
|
+
n
|
|
4161
|
+
]), ie = P(async () => (await E.current, u.current), []), N = P(async (e) => {
|
|
4162
|
+
try {
|
|
4163
|
+
if ((await n.deleteSession(e)).Deleted === !1) {
|
|
4164
|
+
s.getState().pushToast("会话暂未删除,云端运行时仍在同步,请稍后重试。", "error"), M(t, u.current ?? void 0);
|
|
4165
|
+
return;
|
|
4166
|
+
}
|
|
4167
|
+
h.getState().removeSession(e), h.getState().clearSessionMessageHistory(e), x.current.delete(e), C.current.delete(e), w.current.delete(e), y.current.delete(e), T.current.delete(e), u.current === e && (g.current += 1, p.current?.abort(), c?.(), u.current = null, f.getState().setMessages([]), b.getState().clearSessionCheckpoints(e), h.getState().setCurrentSessionId(null), l.getState().setCurrentRunId(""), l.getState().clearActivity(), M(t));
|
|
4168
|
+
} catch (e) {
|
|
4169
|
+
if (e instanceof z) return;
|
|
4170
|
+
console.error("Failed to delete session", e), s.getState().pushToast("删除会话失败,请稍后重试。", "error");
|
|
4171
|
+
}
|
|
4172
|
+
}, [
|
|
4173
|
+
t,
|
|
4174
|
+
n,
|
|
4175
|
+
c,
|
|
4176
|
+
M
|
|
4177
|
+
]);
|
|
4178
|
+
return {
|
|
4179
|
+
fetchSessions: M,
|
|
4180
|
+
loadMoreSessions: te,
|
|
4181
|
+
loadSession: j,
|
|
4182
|
+
loadOlderSessionMessages: P(async (e) => {
|
|
4183
|
+
let t = h.getState().messageHistory[e], r = w.current.get(e), i = !!(t?.hasMore && t.nextCursor !== null), a = !!(r && r.loadedCount < r.total);
|
|
4184
|
+
if (!t || t.isLoadingOlder || !i && !a) return;
|
|
4185
|
+
let o = g.current, s = Symbol(e);
|
|
4186
|
+
_.current.set(e, s);
|
|
4187
|
+
try {
|
|
4188
|
+
h.getState().setSessionMessageHistoryLoading(e, !0);
|
|
4189
|
+
let [c, l] = await Promise.all([i ? n.listSessionMessages(e, {
|
|
4190
|
+
beforeSeqId: t.nextCursor ?? void 0,
|
|
4191
|
+
limit: Rn,
|
|
4192
|
+
includeReasoning: !0,
|
|
4193
|
+
includeToolEvents: !0,
|
|
4194
|
+
includeAttachments: !0
|
|
4195
|
+
}) : Promise.resolve(null), a && r ? n.listSessionEvents(e, {
|
|
4196
|
+
offset: r.loadedCount,
|
|
4197
|
+
limit: zn
|
|
4198
|
+
}) : Promise.resolve(null)]);
|
|
4199
|
+
if (u.current !== e || g.current !== o || _.current.get(e) !== s) return;
|
|
4200
|
+
let p = c ? An(c.Messages) : [], m = new Set(p.map((e) => e.id)), v = [...p, ...(C.current.get(e) || []).filter((e) => !m.has(e.id))];
|
|
4201
|
+
C.current.set(e, v);
|
|
4202
|
+
let b = r?.events || [], S = r?.loadedCount || 0, T = r?.total || 0;
|
|
4203
|
+
if (l) {
|
|
4204
|
+
let t = l.Events || [];
|
|
4205
|
+
b = fe(t, b), T = Math.max(T, Number(l.Total ?? 0) || 0), S = t.length > 0 ? Math.min(T, S + t.length) : T, w.current.set(e, {
|
|
4206
|
+
events: b,
|
|
4207
|
+
loadedCount: S,
|
|
4208
|
+
total: T
|
|
4209
|
+
});
|
|
4210
|
+
for (let n of t) lt(n, e);
|
|
4211
|
+
}
|
|
4212
|
+
let E = ce(v, b, e), D = E.messages;
|
|
4213
|
+
y.current.set(e, new Set(E.canonicalRunIds)), f.getState().setMessages(D), x.current.delete(e), x.current.set(e, D), h.getState().setSessionMessageHistory(e, {
|
|
4214
|
+
nextCursor: c ? c.NextCursor : t.nextCursor,
|
|
4215
|
+
hasMore: !!(c && c.HasMore || S < T)
|
|
4216
|
+
}), k(d.current, e, p);
|
|
4217
|
+
} catch (e) {
|
|
4218
|
+
e instanceof z || console.error("Failed to load older session messages:", e);
|
|
4219
|
+
} finally {
|
|
4220
|
+
_.current.get(e) === s && (_.current.delete(e), h.getState().setSessionMessageHistoryLoading(e, !1));
|
|
4221
|
+
}
|
|
4222
|
+
}, [n, k]),
|
|
4223
|
+
createNewSession: re,
|
|
4224
|
+
adoptCreatedSession: ne,
|
|
4225
|
+
waitForPendingSessionCreation: ie,
|
|
4226
|
+
deleteSession: N,
|
|
4227
|
+
currentSessionIdRef: u,
|
|
4228
|
+
agentIdRef: d,
|
|
4229
|
+
runSubscriptionAbortRef: p
|
|
4230
|
+
};
|
|
4231
|
+
}
|
|
4232
|
+
//#endregion
|
|
4233
|
+
export { Ne as A, Ve as C, Pe as D, V as E, Te as M, Ee as N, ke as O, Je as S, Fe as T, ot as _, yn as a, Xe as b, At as c, Et as d, yt as f, st as g, ft as h, Fn as i, Ce as j, H as k, Y as l, ht as m, jn as n, qt as o, gt as p, Pn as r, Pt as s, Kn as t, X as u, at as v, U as w, Ye as x, Qe as y };
|