@anvia/core 1.1.2 → 1.2.1
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/README.md +226 -0
- package/dist/agent/agent-stream.js +7 -0
- package/dist/agent/agent-stream.js.map +1 -0
- package/dist/agent/agent-tool.js +24 -0
- package/dist/agent/agent-tool.js.map +1 -0
- package/dist/agent/agent.js +89 -0
- package/dist/agent/agent.js.map +1 -0
- package/dist/agent/errors.js +17 -0
- package/dist/agent/errors.js.map +1 -0
- package/dist/agent/ids.js +7 -0
- package/dist/agent/ids.js.map +1 -0
- package/dist/agent/index.d.ts +127 -9
- package/dist/agent/index.js +103 -21
- package/dist/agent/interactions/index.js +2 -2
- package/dist/agent/interactions.js +22 -0
- package/dist/agent/interactions.js.map +1 -0
- package/dist/agent/lifecycle.js +9 -0
- package/dist/agent/lifecycle.js.map +1 -0
- package/dist/agent/output-schema.js +10 -0
- package/dist/agent/output-schema.js.map +1 -0
- package/dist/agent/resolve-options.js +46 -0
- package/dist/agent/resolve-options.js.map +1 -0
- package/dist/agent/resolved-agent.js +92 -0
- package/dist/agent/resolved-agent.js.map +1 -0
- package/dist/agent/run-types.js +1 -0
- package/dist/agent/run-types.js.map +1 -0
- package/dist/agent/snapshot.js +17 -0
- package/dist/agent/snapshot.js.map +1 -0
- package/dist/agent/team/agent-team.js +98 -0
- package/dist/agent/team/agent-team.js.map +1 -0
- package/dist/agent/team/errors.js +9 -0
- package/dist/agent/team/errors.js.map +1 -0
- package/dist/agent/team/index.js +104 -0
- package/dist/agent/team/index.js.map +1 -0
- package/dist/agent/team/types.js +1 -0
- package/dist/agent/team/types.js.map +1 -0
- package/dist/agent/tool-catalog.js +37 -0
- package/dist/agent/tool-catalog.js.map +1 -0
- package/dist/agent/tool-state.js +11 -0
- package/dist/agent/tool-state.js.map +1 -0
- package/dist/agent/types.js +1 -0
- package/dist/agent/types.js.map +1 -0
- package/dist/agent/vector-context.js +10 -0
- package/dist/agent/vector-context.js.map +1 -0
- package/dist/{agent-DTmzs1Qr.d.ts → agent-C9VhENdj.d.ts} +2 -3
- package/dist/{chunk-I3Q7XSBB.js → chunk-23LRXKQX.js} +6 -4
- package/dist/{chunk-I3Q7XSBB.js.map → chunk-23LRXKQX.js.map} +1 -1
- package/dist/{chunk-YK4WAAS4.js → chunk-2NLINXRF.js} +1 -1
- package/dist/{chunk-SPD4XVR7.js → chunk-2XSMGVBG.js} +1 -1
- package/dist/chunk-3J2CSVPR.js +1 -0
- package/dist/chunk-3J2CSVPR.js.map +1 -0
- package/dist/chunk-3P3YVJ7V.js +1 -0
- package/dist/chunk-3P3YVJ7V.js.map +1 -0
- package/dist/chunk-3TAQXJBC.js +107 -0
- package/dist/chunk-3TAQXJBC.js.map +1 -0
- package/dist/chunk-47NTHIVB.js +76 -0
- package/dist/chunk-47NTHIVB.js.map +1 -0
- package/dist/chunk-4AW2633B.js +9 -0
- package/dist/chunk-4AW2633B.js.map +1 -0
- package/dist/{chunk-3XQGVDU5.js → chunk-4C7AXYOM.js} +1 -1
- package/dist/{chunk-IZNOP6JG.js → chunk-4FIVU53H.js} +1 -1
- package/dist/chunk-4ITAV7YA.js +1 -0
- package/dist/chunk-4ITAV7YA.js.map +1 -0
- package/dist/{chunk-7JLAIN6E.js → chunk-4ONUNWGC.js} +2 -2
- package/dist/chunk-4S4OECPW.js +158 -0
- package/dist/chunk-4S4OECPW.js.map +1 -0
- package/dist/chunk-4UUIGAIX.js +1 -0
- package/dist/chunk-4UUIGAIX.js.map +1 -0
- package/dist/chunk-53TPAXYR.js +1 -0
- package/dist/chunk-53TPAXYR.js.map +1 -0
- package/dist/chunk-54ZZJRP2.js +57 -0
- package/dist/chunk-54ZZJRP2.js.map +1 -0
- package/dist/chunk-5FCZ64HM.js +112 -0
- package/dist/chunk-5FCZ64HM.js.map +1 -0
- package/dist/chunk-5I2NVRHC.js +146 -0
- package/dist/chunk-5I2NVRHC.js.map +1 -0
- package/dist/chunk-5IURMWFN.js +53 -0
- package/dist/chunk-5IURMWFN.js.map +1 -0
- package/dist/chunk-5M6GTA3T.js +1 -0
- package/dist/chunk-5M6GTA3T.js.map +1 -0
- package/dist/chunk-5N5W7CX7.js +18 -0
- package/dist/chunk-5N5W7CX7.js.map +1 -0
- package/dist/chunk-5S7T2URX.js +1 -0
- package/dist/chunk-5S7T2URX.js.map +1 -0
- package/dist/chunk-5V533R7F.js +19 -0
- package/dist/chunk-5V533R7F.js.map +1 -0
- package/dist/chunk-5WLIHNNJ.js +56 -0
- package/dist/chunk-5WLIHNNJ.js.map +1 -0
- package/dist/chunk-6BTJC3ES.js +37 -0
- package/dist/chunk-6BTJC3ES.js.map +1 -0
- package/dist/chunk-6N4ENZO4.js +24 -0
- package/dist/chunk-6N4ENZO4.js.map +1 -0
- package/dist/{chunk-4UQALF5Y.js → chunk-6RQYWZI3.js} +2 -36
- package/dist/chunk-6RQYWZI3.js.map +1 -0
- package/dist/chunk-6TAWRKFK.js +86 -0
- package/dist/chunk-6TAWRKFK.js.map +1 -0
- package/dist/chunk-72LBHKYP.js +236 -0
- package/dist/chunk-72LBHKYP.js.map +1 -0
- package/dist/chunk-7F53DOML.js +53 -0
- package/dist/chunk-7F53DOML.js.map +1 -0
- package/dist/chunk-7LBPRXZP.js +1 -0
- package/dist/chunk-7LBPRXZP.js.map +1 -0
- package/dist/chunk-A7BXVZXZ.js +25 -0
- package/dist/chunk-A7BXVZXZ.js.map +1 -0
- package/dist/chunk-AOG6KSV4.js +107 -0
- package/dist/chunk-AOG6KSV4.js.map +1 -0
- package/dist/chunk-B47EXUHL.js +67 -0
- package/dist/chunk-B47EXUHL.js.map +1 -0
- package/dist/chunk-B5JQDR33.js +14 -0
- package/dist/chunk-B5JQDR33.js.map +1 -0
- package/dist/chunk-BDDC72GF.js +189 -0
- package/dist/chunk-BDDC72GF.js.map +1 -0
- package/dist/chunk-BDKFFFVX.js +342 -0
- package/dist/chunk-BDKFFFVX.js.map +1 -0
- package/dist/{chunk-QGX73TSQ.js → chunk-BONQTXRQ.js} +3 -12
- package/dist/chunk-BONQTXRQ.js.map +1 -0
- package/dist/chunk-BUPC72Y2.js +1007 -0
- package/dist/chunk-BUPC72Y2.js.map +1 -0
- package/dist/chunk-CKKNO4XB.js +70 -0
- package/dist/chunk-CKKNO4XB.js.map +1 -0
- package/dist/chunk-CX7ZVTO5.js +22 -0
- package/dist/chunk-CX7ZVTO5.js.map +1 -0
- package/dist/chunk-DU4WA6LQ.js +1 -0
- package/dist/chunk-DU4WA6LQ.js.map +1 -0
- package/dist/chunk-EJR6VVB3.js +63 -0
- package/dist/chunk-EJR6VVB3.js.map +1 -0
- package/dist/chunk-EKZRAWBP.js +110 -0
- package/dist/chunk-EKZRAWBP.js.map +1 -0
- package/dist/chunk-ENZ2XX27.js +26 -0
- package/dist/chunk-ENZ2XX27.js.map +1 -0
- package/dist/chunk-EOAJBBII.js +1 -0
- package/dist/chunk-EOAJBBII.js.map +1 -0
- package/dist/chunk-ERN7P6Q4.js +1 -0
- package/dist/chunk-ERN7P6Q4.js.map +1 -0
- package/dist/chunk-ESOFEAYK.js +57 -0
- package/dist/chunk-ESOFEAYK.js.map +1 -0
- package/dist/chunk-ETYB5JO6.js +175 -0
- package/dist/chunk-ETYB5JO6.js.map +1 -0
- package/dist/chunk-EZR5AV3J.js +74 -0
- package/dist/chunk-EZR5AV3J.js.map +1 -0
- package/dist/chunk-F2OKLHGB.js +9 -0
- package/dist/chunk-F2OKLHGB.js.map +1 -0
- package/dist/chunk-F3E2RLC7.js +342 -0
- package/dist/chunk-F3E2RLC7.js.map +1 -0
- package/dist/chunk-FAPIZJS2.js +54 -0
- package/dist/chunk-FAPIZJS2.js.map +1 -0
- package/dist/{chunk-2QZJTG2E.js → chunk-G336L2IE.js} +11 -71
- package/dist/chunk-G336L2IE.js.map +1 -0
- package/dist/chunk-GDJNOZ47.js +1 -0
- package/dist/chunk-GDJNOZ47.js.map +1 -0
- package/dist/chunk-GDY2Z2SZ.js +83 -0
- package/dist/chunk-GDY2Z2SZ.js.map +1 -0
- package/dist/chunk-GLMYZ6OA.js +20 -0
- package/dist/chunk-GLMYZ6OA.js.map +1 -0
- package/dist/{chunk-XEVOC433.js → chunk-GPVXCY5I.js} +2 -73
- package/dist/chunk-GPVXCY5I.js.map +1 -0
- package/dist/{chunk-KSKST3KP.js → chunk-H3H74RWJ.js} +1 -1
- package/dist/chunk-HAAGSIZH.js +89 -0
- package/dist/chunk-HAAGSIZH.js.map +1 -0
- package/dist/{chunk-K5L7R7XM.js → chunk-HHMLE3TZ.js} +6 -54
- package/dist/chunk-HHMLE3TZ.js.map +1 -0
- package/dist/{chunk-JLJMVRRE.js → chunk-HP34GRQX.js} +10 -83
- package/dist/chunk-HP34GRQX.js.map +1 -0
- package/dist/chunk-HTI343NP.js +1 -0
- package/dist/chunk-HTI343NP.js.map +1 -0
- package/dist/{chunk-T2C3CGDQ.js → chunk-HTI7DZJM.js} +6 -4
- package/dist/{chunk-T2C3CGDQ.js.map → chunk-HTI7DZJM.js.map} +1 -1
- package/dist/chunk-IDNL6IH4.js +294 -0
- package/dist/chunk-IDNL6IH4.js.map +1 -0
- package/dist/chunk-IV6YQTY2.js +47 -0
- package/dist/chunk-IV6YQTY2.js.map +1 -0
- package/dist/chunk-J4SINPUA.js +939 -0
- package/dist/chunk-J4SINPUA.js.map +1 -0
- package/dist/chunk-J6S6MT7O.js +32 -0
- package/dist/chunk-J6S6MT7O.js.map +1 -0
- package/dist/chunk-JBQNCUXA.js +34 -0
- package/dist/chunk-JBQNCUXA.js.map +1 -0
- package/dist/chunk-JMJFK3A4.js +1 -0
- package/dist/chunk-JMJFK3A4.js.map +1 -0
- package/dist/chunk-JMMZICU7.js +74 -0
- package/dist/chunk-JMMZICU7.js.map +1 -0
- package/dist/chunk-K4IBS7VB.js +55 -0
- package/dist/chunk-K4IBS7VB.js.map +1 -0
- package/dist/chunk-KBBQSP4M.js +16 -0
- package/dist/chunk-KBBQSP4M.js.map +1 -0
- package/dist/chunk-KFAVEL5F.js +68 -0
- package/dist/chunk-KFAVEL5F.js.map +1 -0
- package/dist/chunk-KITX3HDN.js +17 -0
- package/dist/chunk-KITX3HDN.js.map +1 -0
- package/dist/chunk-KO5NMTAM.js +1 -0
- package/dist/chunk-KO5NMTAM.js.map +1 -0
- package/dist/chunk-KY4VXIN2.js +118 -0
- package/dist/chunk-KY4VXIN2.js.map +1 -0
- package/dist/chunk-KZGSXJKU.js +29 -0
- package/dist/chunk-KZGSXJKU.js.map +1 -0
- package/dist/chunk-LAIATEUB.js +24 -0
- package/dist/chunk-LAIATEUB.js.map +1 -0
- package/dist/{chunk-OJBFDBLG.js → chunk-LBYJPMHC.js} +2 -2
- package/dist/chunk-LKIABZNR.js +49 -0
- package/dist/chunk-LKIABZNR.js.map +1 -0
- package/dist/chunk-LQJ4KHMY.js +23 -0
- package/dist/chunk-LQJ4KHMY.js.map +1 -0
- package/dist/chunk-MEN4AFDZ.js +1 -0
- package/dist/chunk-MEN4AFDZ.js.map +1 -0
- package/dist/chunk-MHOJJYDK.js +93 -0
- package/dist/chunk-MHOJJYDK.js.map +1 -0
- package/dist/chunk-MJCMKZFP.js +30 -0
- package/dist/chunk-MJCMKZFP.js.map +1 -0
- package/dist/chunk-MNYMLWWA.js +168 -0
- package/dist/chunk-MNYMLWWA.js.map +1 -0
- package/dist/chunk-MT3BLVYN.js +31 -0
- package/dist/chunk-MT3BLVYN.js.map +1 -0
- package/dist/chunk-NF6WBPFG.js +37 -0
- package/dist/chunk-NF6WBPFG.js.map +1 -0
- package/dist/chunk-NNEKBZSM.js +66 -0
- package/dist/chunk-NNEKBZSM.js.map +1 -0
- package/dist/chunk-NPJ2T3L5.js +19 -0
- package/dist/chunk-NPJ2T3L5.js.map +1 -0
- package/dist/chunk-NVALGVQG.js +150 -0
- package/dist/chunk-NVALGVQG.js.map +1 -0
- package/dist/chunk-O6DTOCBM.js +355 -0
- package/dist/chunk-O6DTOCBM.js.map +1 -0
- package/dist/chunk-OJF67RNM.js +1 -0
- package/dist/chunk-OJF67RNM.js.map +1 -0
- package/dist/chunk-PHLGGRHC.js +89 -0
- package/dist/chunk-PHLGGRHC.js.map +1 -0
- package/dist/chunk-PIMD4DYG.js +104 -0
- package/dist/chunk-PIMD4DYG.js.map +1 -0
- package/dist/chunk-PJNE75WC.js +1 -0
- package/dist/chunk-PJNE75WC.js.map +1 -0
- package/dist/{chunk-J6LVLV6P.js → chunk-PLDJCCVL.js} +1 -1
- package/dist/{chunk-3RWESPUG.js → chunk-POXHJF3H.js} +6 -51
- package/dist/chunk-POXHJF3H.js.map +1 -0
- package/dist/chunk-PPJ7SYQL.js +81 -0
- package/dist/chunk-PPJ7SYQL.js.map +1 -0
- package/dist/chunk-QHGYCYV6.js +91 -0
- package/dist/chunk-QHGYCYV6.js.map +1 -0
- package/dist/chunk-QUS6S2NB.js +663 -0
- package/dist/chunk-QUS6S2NB.js.map +1 -0
- package/dist/chunk-R6722CIU.js +28 -0
- package/dist/chunk-R6722CIU.js.map +1 -0
- package/dist/chunk-RA4YVN43.js +1 -0
- package/dist/chunk-RA4YVN43.js.map +1 -0
- package/dist/chunk-RT5LUEO3.js +2339 -0
- package/dist/chunk-RT5LUEO3.js.map +1 -0
- package/dist/chunk-SC4SUIEY.js +1 -0
- package/dist/chunk-SC4SUIEY.js.map +1 -0
- package/dist/chunk-SIYZDPVH.js +613 -0
- package/dist/chunk-SIYZDPVH.js.map +1 -0
- package/dist/chunk-SQAAVYJG.js +12 -0
- package/dist/chunk-SQAAVYJG.js.map +1 -0
- package/dist/chunk-SXE4J43E.js +59 -0
- package/dist/chunk-SXE4J43E.js.map +1 -0
- package/dist/{chunk-EFLT7XZD.js → chunk-SYULMZGL.js} +6 -4
- package/dist/{chunk-EFLT7XZD.js.map → chunk-SYULMZGL.js.map} +1 -1
- package/dist/chunk-T4IAJJUU.js +48 -0
- package/dist/chunk-T4IAJJUU.js.map +1 -0
- package/dist/chunk-T4KS577P.js +58 -0
- package/dist/chunk-T4KS577P.js.map +1 -0
- package/dist/chunk-TFIOU6UR.js +78 -0
- package/dist/chunk-TFIOU6UR.js.map +1 -0
- package/dist/chunk-TIZGADU4.js +375 -0
- package/dist/chunk-TIZGADU4.js.map +1 -0
- package/dist/chunk-TP3MCUXS.js +1 -0
- package/dist/chunk-TP3MCUXS.js.map +1 -0
- package/dist/{chunk-JTJU56ZV.js → chunk-UOGM62JL.js} +11 -7
- package/dist/{chunk-JTJU56ZV.js.map → chunk-UOGM62JL.js.map} +1 -1
- package/dist/chunk-USRKPEQN.js +55 -0
- package/dist/chunk-USRKPEQN.js.map +1 -0
- package/dist/chunk-UTAB3XQI.js +667 -0
- package/dist/chunk-UTAB3XQI.js.map +1 -0
- package/dist/chunk-V3FFZWF3.js +182 -0
- package/dist/chunk-V3FFZWF3.js.map +1 -0
- package/dist/chunk-V4OZ7ISA.js +9 -0
- package/dist/chunk-V4OZ7ISA.js.map +1 -0
- package/dist/chunk-VZ5ZZBNX.js +139 -0
- package/dist/chunk-VZ5ZZBNX.js.map +1 -0
- package/dist/{chunk-AHLKV6KP.js → chunk-WN6AVBO4.js} +1 -1
- package/dist/chunk-WXR5CWSC.js +1 -0
- package/dist/chunk-WXR5CWSC.js.map +1 -0
- package/dist/chunk-X6WS3XWL.js +36 -0
- package/dist/chunk-X6WS3XWL.js.map +1 -0
- package/dist/{chunk-Q5BCNYED.js → chunk-XJRNBTIV.js} +7 -4
- package/dist/chunk-XQV3XNVT.js +225 -0
- package/dist/chunk-XQV3XNVT.js.map +1 -0
- package/dist/chunk-XV3DH5GC.js +58 -0
- package/dist/chunk-XV3DH5GC.js.map +1 -0
- package/dist/chunk-Y7JTHNCU.js +318 -0
- package/dist/chunk-Y7JTHNCU.js.map +1 -0
- package/dist/chunk-YWRXOPHC.js +43 -0
- package/dist/chunk-YWRXOPHC.js.map +1 -0
- package/dist/chunk-YZVFIW5D.js +27 -0
- package/dist/chunk-YZVFIW5D.js.map +1 -0
- package/dist/chunk-ZHC5CD6J.js +33 -0
- package/dist/chunk-ZHC5CD6J.js.map +1 -0
- package/dist/chunk-ZPJFXRQZ.js +23 -0
- package/dist/chunk-ZPJFXRQZ.js.map +1 -0
- package/dist/chunk-ZPXOBQDS.js +57 -0
- package/dist/chunk-ZPXOBQDS.js.map +1 -0
- package/dist/chunk-ZUFS6R7L.js +155 -0
- package/dist/chunk-ZUFS6R7L.js.map +1 -0
- package/dist/chunk-ZXLCTQUX.js +62 -0
- package/dist/chunk-ZXLCTQUX.js.map +1 -0
- package/dist/chunk-ZYXMWEL4.js +59 -0
- package/dist/chunk-ZYXMWEL4.js.map +1 -0
- package/dist/completion/controls.js +11 -0
- package/dist/completion/controls.js.map +1 -0
- package/dist/completion/documents.js +9 -0
- package/dist/completion/documents.js.map +1 -0
- package/dist/completion/generate-completion.js +23 -0
- package/dist/completion/generate-completion.js.map +1 -0
- package/dist/completion/index.js +23 -12
- package/dist/completion/json.js +7 -0
- package/dist/completion/json.js.map +1 -0
- package/dist/completion/message-schema.js +18 -0
- package/dist/completion/message-schema.js.map +1 -0
- package/dist/completion/provider-output-error.js +12 -0
- package/dist/completion/provider-output-error.js.map +1 -0
- package/dist/completion/stream-accumulator.js +10 -0
- package/dist/completion/stream-accumulator.js.map +1 -0
- package/dist/completion/types.js +28 -0
- package/dist/completion/types.js.map +1 -0
- package/dist/documents/chunk-text.js +7 -0
- package/dist/documents/chunk-text.js.map +1 -0
- package/dist/documents/index.js +5 -2
- package/dist/documents/text-document.js +8 -0
- package/dist/documents/text-document.js.map +1 -0
- package/dist/embeddings/distance.js +17 -0
- package/dist/embeddings/distance.js.map +1 -0
- package/dist/embeddings/embed.js +18 -0
- package/dist/embeddings/embed.js.map +1 -0
- package/dist/embeddings/index.js +11 -6
- package/dist/embeddings/types.js +2 -0
- package/dist/embeddings/types.js.map +1 -0
- package/dist/evals/advanced-metrics.js +46 -0
- package/dist/evals/advanced-metrics.js.map +1 -0
- package/dist/evals/agent-target.js +10 -0
- package/dist/evals/agent-target.js.map +1 -0
- package/dist/evals/cli.js +40 -0
- package/dist/evals/cli.js.map +1 -0
- package/dist/evals/execution.js +15 -0
- package/dist/evals/execution.js.map +1 -0
- package/dist/evals/format.js +13 -0
- package/dist/evals/format.js.map +1 -0
- package/dist/evals/index.d.ts +4 -4
- package/dist/evals/index.js +87 -2660
- package/dist/evals/index.js.map +1 -1
- package/dist/evals/judge.js +27 -0
- package/dist/evals/judge.js.map +1 -0
- package/dist/evals/metric.js +7 -0
- package/dist/evals/metric.js.map +1 -0
- package/dist/evals/metrics.js +53 -0
- package/dist/evals/metrics.js.map +1 -0
- package/dist/evals/outcome.js +7 -0
- package/dist/evals/outcome.js.map +1 -0
- package/dist/evals/reporting.js +11 -0
- package/dist/evals/reporting.js.map +1 -0
- package/dist/evals/runner.js +29 -0
- package/dist/evals/runner.js.map +1 -0
- package/dist/evals/selectors.js +18 -0
- package/dist/evals/selectors.js.map +1 -0
- package/dist/evals/suite.js +11 -0
- package/dist/evals/suite.js.map +1 -0
- package/dist/evals/types.js +1 -0
- package/dist/evals/types.js.map +1 -0
- package/dist/extractor/extractor.js +23 -0
- package/dist/extractor/extractor.js.map +1 -0
- package/dist/extractor/index.js +16 -7
- package/dist/guardrails/actions.js +13 -0
- package/dist/guardrails/actions.js.map +1 -0
- package/dist/guardrails/index.d.ts +3 -4
- package/dist/guardrails/index.js +15 -7
- package/dist/guardrails/message.js +9 -0
- package/dist/guardrails/message.js.map +1 -0
- package/dist/guardrails/policy.js +17 -0
- package/dist/guardrails/policy.js.map +1 -0
- package/dist/guardrails/runtime.js +11 -0
- package/dist/guardrails/runtime.js.map +1 -0
- package/dist/guardrails/text.js +9 -0
- package/dist/guardrails/text.js.map +1 -0
- package/dist/guardrails/types.js +1 -0
- package/dist/guardrails/types.js.map +1 -0
- package/dist/hooks/control.js +17 -0
- package/dist/hooks/control.js.map +1 -0
- package/dist/hooks/index.js +19 -0
- package/dist/hooks/index.js.map +1 -0
- package/dist/hooks/types.js +2 -0
- package/dist/hooks/types.js.map +1 -0
- package/dist/image-generation/generate-image.js +11 -0
- package/dist/image-generation/generate-image.js.map +1 -0
- package/dist/image-generation/index.js +7 -4
- package/dist/image-generation/types.js +2 -0
- package/dist/image-generation/types.js.map +1 -0
- package/dist/index.d.ts +5 -7
- package/dist/index.js +163 -57
- package/dist/internal/abort.js +13 -0
- package/dist/internal/abort.js.map +1 -0
- package/dist/internal/agent-runtime/agent-run.js +81 -0
- package/dist/internal/agent-runtime/agent-run.js.map +1 -0
- package/dist/internal/agent-runtime/approval-request.js +1 -0
- package/dist/internal/agent-runtime/approval-request.js.map +1 -0
- package/dist/internal/agent-runtime/approval-requirement.js +9 -0
- package/dist/internal/agent-runtime/approval-requirement.js.map +1 -0
- package/dist/internal/agent-runtime/continuation-state.js +26 -0
- package/dist/internal/agent-runtime/continuation-state.js.map +1 -0
- package/dist/internal/agent-runtime/interaction-suspension.js +25 -0
- package/dist/internal/agent-runtime/interaction-suspension.js.map +1 -0
- package/dist/internal/agent-runtime/memory-scope.js +8 -0
- package/dist/internal/agent-runtime/memory-scope.js.map +1 -0
- package/dist/internal/agent-runtime/memory.js +24 -0
- package/dist/internal/agent-runtime/memory.js.map +1 -0
- package/dist/internal/agent-runtime/prepared-tool-call.js +19 -0
- package/dist/internal/agent-runtime/prepared-tool-call.js.map +1 -0
- package/dist/internal/agent-runtime/retrieval.js +35 -0
- package/dist/internal/agent-runtime/retrieval.js.map +1 -0
- package/dist/internal/agent-runtime/run-options.js +9 -0
- package/dist/internal/agent-runtime/run-options.js.map +1 -0
- package/dist/internal/agent-runtime/run-validation.js +9 -0
- package/dist/internal/agent-runtime/run-validation.js.map +1 -0
- package/dist/internal/agent-runtime/stream-events.js +11 -0
- package/dist/internal/agent-runtime/stream-events.js.map +1 -0
- package/dist/internal/agent-runtime/structured-output.js +13 -0
- package/dist/internal/agent-runtime/structured-output.js.map +1 -0
- package/dist/internal/agent-runtime/tool-execution.js +55 -0
- package/dist/internal/agent-runtime/tool-execution.js.map +1 -0
- package/dist/internal/agent.d.ts +5 -5
- package/dist/internal/agent.js +93 -57
- package/dist/internal/agent.js.map +1 -1
- package/dist/internal/async-queue.js +7 -0
- package/dist/internal/async-queue.js.map +1 -0
- package/dist/internal/completion-request.js +11 -0
- package/dist/internal/completion-request.js.map +1 -0
- package/dist/internal/concurrency.js +7 -0
- package/dist/internal/concurrency.js.map +1 -0
- package/dist/internal/json-object.js +8 -0
- package/dist/internal/json-object.js.map +1 -0
- package/dist/internal/rag-text.js +7 -0
- package/dist/internal/rag-text.js.map +1 -0
- package/dist/internal/record.js +7 -0
- package/dist/internal/record.js.map +1 -0
- package/dist/internal/team-runtime/coordination.js +12 -0
- package/dist/internal/team-runtime/coordination.js.map +1 -0
- package/dist/internal/team-runtime/member.js +26 -0
- package/dist/internal/team-runtime/member.js.map +1 -0
- package/dist/internal/team-runtime/policy.js +7 -0
- package/dist/internal/team-runtime/policy.js.map +1 -0
- package/dist/internal/team-runtime/run.js +95 -0
- package/dist/internal/team-runtime/run.js.map +1 -0
- package/dist/internal/team-runtime/stream.js +9 -0
- package/dist/internal/team-runtime/stream.js.map +1 -0
- package/dist/internal/team-runtime/tools.js +17 -0
- package/dist/internal/team-runtime/tools.js.map +1 -0
- package/dist/internal/type-utils.js +1 -0
- package/dist/internal/type-utils.js.map +1 -0
- package/dist/internal/vector-search-options.js +9 -0
- package/dist/internal/vector-search-options.js.map +1 -0
- package/dist/mcp/index.js +3 -1
- package/dist/mcp/tool.js +7 -0
- package/dist/mcp/tool.js.map +1 -0
- package/dist/mcp/types.js +2 -0
- package/dist/mcp/types.js.map +1 -0
- package/dist/memory/assert.js +11 -0
- package/dist/memory/assert.js.map +1 -0
- package/dist/memory/compaction.js +26 -0
- package/dist/memory/compaction.js.map +1 -0
- package/dist/memory/errors.js +9 -0
- package/dist/memory/errors.js.map +1 -0
- package/dist/memory/index.js +23 -10
- package/dist/memory/options.js +10 -0
- package/dist/memory/options.js.map +1 -0
- package/dist/memory/scope-key.js +7 -0
- package/dist/memory/scope-key.js.map +1 -0
- package/dist/memory/types.js +2 -0
- package/dist/memory/types.js.map +1 -0
- package/dist/{types-CRda8x7p.d.ts → middleware-CWjnMbiH.d.ts} +64 -5
- package/dist/model-call-options.js +1 -0
- package/dist/model-call-options.js.map +1 -0
- package/dist/model-listing/errors.js +7 -0
- package/dist/model-listing/errors.js.map +1 -0
- package/dist/model-listing/index.js +4 -13
- package/dist/model-listing/index.js.map +1 -1
- package/dist/model-listing/types.js +2 -0
- package/dist/model-listing/types.js.map +1 -0
- package/dist/observability/group.js +30 -0
- package/dist/observability/group.js.map +1 -0
- package/dist/observability/index.d.ts +4 -6
- package/dist/observability/index.js +16 -8
- package/dist/observability/snapshot.js +7 -0
- package/dist/observability/snapshot.js.map +1 -0
- package/dist/observability/types.js +1 -0
- package/dist/observability/types.js.map +1 -0
- package/dist/pipeline/errors.js +9 -0
- package/dist/pipeline/errors.js.map +1 -0
- package/dist/pipeline/graph.js +21 -0
- package/dist/pipeline/graph.js.map +1 -0
- package/dist/pipeline/index.d.ts +4 -6
- package/dist/pipeline/index.js +27 -805
- package/dist/pipeline/index.js.map +1 -1
- package/dist/pipeline/observability.js +15 -0
- package/dist/pipeline/observability.js.map +1 -0
- package/dist/pipeline/pipeline.js +30 -0
- package/dist/pipeline/pipeline.js.map +1 -0
- package/dist/pipeline/runtime.js +17 -0
- package/dist/pipeline/runtime.js.map +1 -0
- package/dist/pipeline/types.js +1 -0
- package/dist/pipeline/types.js.map +1 -0
- package/dist/retry.js +20 -0
- package/dist/retry.js.map +1 -0
- package/dist/schema/index.js +1 -0
- package/dist/schema/index.js.map +1 -0
- package/dist/schema/zod-schema.js +7 -0
- package/dist/schema/zod-schema.js.map +1 -0
- package/dist/skills/index.js +41 -12
- package/dist/skills/instructions.js +7 -0
- package/dist/skills/instructions.js.map +1 -0
- package/dist/skills/load.js +39 -0
- package/dist/skills/load.js.map +1 -0
- package/dist/skills/local.js +9 -0
- package/dist/skills/local.js.map +1 -0
- package/dist/skills/tools.js +37 -0
- package/dist/skills/tools.js.map +1 -0
- package/dist/skills/types.js +7 -0
- package/dist/skills/types.js.map +1 -0
- package/dist/speech-generation/generate-speech.js +11 -0
- package/dist/speech-generation/generate-speech.js.map +1 -0
- package/dist/speech-generation/index.js +7 -4
- package/dist/speech-generation/types.js +2 -0
- package/dist/speech-generation/types.js.map +1 -0
- package/dist/streaming/index.js +4 -37
- package/dist/streaming/index.js.map +1 -1
- package/dist/streaming/readable-stream.js +8 -0
- package/dist/streaming/readable-stream.js.map +1 -0
- package/dist/{text-CmjXxakS.d.ts → text-GbW4WYMg.d.ts} +1 -1
- package/dist/tool/create-tool.js +14 -0
- package/dist/tool/create-tool.js.map +1 -0
- package/dist/tool/dynamic-tools.js +35 -0
- package/dist/tool/dynamic-tools.js.map +1 -0
- package/dist/tool/errors.js +11 -0
- package/dist/tool/errors.js.map +1 -0
- package/dist/tool/index.d.ts +3 -2
- package/dist/tool/index.js +41 -15
- package/dist/tool/middleware.js +7 -0
- package/dist/tool/middleware.js.map +1 -0
- package/dist/tool/question-tool.js +17 -0
- package/dist/tool/question-tool.js.map +1 -0
- package/dist/tool/skill-tool-marker.js +9 -0
- package/dist/tool/skill-tool-marker.js.map +1 -0
- package/dist/tool/think-tool.js +15 -0
- package/dist/tool/think-tool.js.map +1 -0
- package/dist/tool/tool.js +17 -0
- package/dist/tool/tool.js.map +1 -0
- package/dist/transcription/index.js +7 -4
- package/dist/transcription/transcribe.js +11 -0
- package/dist/transcription/transcribe.js.map +1 -0
- package/dist/transcription/types.js +2 -0
- package/dist/transcription/types.js.map +1 -0
- package/dist/{types-h0EL6Xn0.d.ts → types-Bqw0AuFl.d.ts} +6 -2
- package/dist/vector-store/filter.js +9 -0
- package/dist/vector-store/filter.js.map +1 -0
- package/dist/vector-store/index.js +31 -11
- package/dist/vector-store/ingest.js +19 -0
- package/dist/vector-store/ingest.js.map +1 -0
- package/dist/vector-store/lsh.js +7 -0
- package/dist/vector-store/lsh.js.map +1 -0
- package/dist/vector-store/retrieve.js +15 -0
- package/dist/vector-store/retrieve.js.map +1 -0
- package/dist/vector-store/types.js +1 -0
- package/dist/vector-store/types.js.map +1 -0
- package/package.json +2 -2
- package/dist/chunk-2QZJTG2E.js.map +0 -1
- package/dist/chunk-3RWESPUG.js.map +0 -1
- package/dist/chunk-4UQALF5Y.js.map +0 -1
- package/dist/chunk-AZB6N7P4.js +0 -320
- package/dist/chunk-AZB6N7P4.js.map +0 -1
- package/dist/chunk-JLJMVRRE.js.map +0 -1
- package/dist/chunk-K5L7R7XM.js.map +0 -1
- package/dist/chunk-OI3LSMJG.js +0 -1545
- package/dist/chunk-OI3LSMJG.js.map +0 -1
- package/dist/chunk-QGX73TSQ.js.map +0 -1
- package/dist/chunk-XC3LVC4K.js +0 -753
- package/dist/chunk-XC3LVC4K.js.map +0 -1
- package/dist/chunk-XEVOC433.js.map +0 -1
- package/dist/chunk-XSV5C4R5.js +0 -400
- package/dist/chunk-XSV5C4R5.js.map +0 -1
- package/dist/chunk-XU54PLSD.js +0 -4786
- package/dist/chunk-XU54PLSD.js.map +0 -1
- package/dist/middleware-mY_lpjDp.d.ts +0 -59
- package/dist/type-utils-CtHVDRn_.d.ts +0 -6
- /package/dist/{chunk-YK4WAAS4.js.map → chunk-2NLINXRF.js.map} +0 -0
- /package/dist/{chunk-SPD4XVR7.js.map → chunk-2XSMGVBG.js.map} +0 -0
- /package/dist/{chunk-3XQGVDU5.js.map → chunk-4C7AXYOM.js.map} +0 -0
- /package/dist/{chunk-IZNOP6JG.js.map → chunk-4FIVU53H.js.map} +0 -0
- /package/dist/{chunk-7JLAIN6E.js.map → chunk-4ONUNWGC.js.map} +0 -0
- /package/dist/{chunk-KSKST3KP.js.map → chunk-H3H74RWJ.js.map} +0 -0
- /package/dist/{chunk-OJBFDBLG.js.map → chunk-LBYJPMHC.js.map} +0 -0
- /package/dist/{chunk-J6LVLV6P.js.map → chunk-PLDJCCVL.js.map} +0 -0
- /package/dist/{chunk-AHLKV6KP.js.map → chunk-WN6AVBO4.js.map} +0 -0
- /package/dist/{chunk-Q5BCNYED.js.map → chunk-XJRNBTIV.js.map} +0 -0
|
@@ -0,0 +1,2339 @@
|
|
|
1
|
+
import {
|
|
2
|
+
fetchContextDocuments,
|
|
3
|
+
fetchToolDefinitions
|
|
4
|
+
} from "./chunk-GDY2Z2SZ.js";
|
|
5
|
+
import {
|
|
6
|
+
getInternalAgentRunOptions
|
|
7
|
+
} from "./chunk-KITX3HDN.js";
|
|
8
|
+
import {
|
|
9
|
+
assertNonnegativeSafeInteger,
|
|
10
|
+
assertPositiveSafeInteger
|
|
11
|
+
} from "./chunk-NPJ2T3L5.js";
|
|
12
|
+
import {
|
|
13
|
+
addTurn,
|
|
14
|
+
addTurnToToolCallDelta,
|
|
15
|
+
isGenerationDeltaEvent
|
|
16
|
+
} from "./chunk-YWRXOPHC.js";
|
|
17
|
+
import {
|
|
18
|
+
STRUCTURED_OUTPUT_RETRY_PROMPT,
|
|
19
|
+
STRUCTURED_OUTPUT_TRUNCATED_RETRY_PROMPT,
|
|
20
|
+
normalizeStructuredOutput,
|
|
21
|
+
structuredOutputRepairPreview
|
|
22
|
+
} from "./chunk-SXE4J43E.js";
|
|
23
|
+
import {
|
|
24
|
+
ToolCallExecutor
|
|
25
|
+
} from "./chunk-J4SINPUA.js";
|
|
26
|
+
import {
|
|
27
|
+
parseContinuationState,
|
|
28
|
+
questionResult,
|
|
29
|
+
serializeContinuationState
|
|
30
|
+
} from "./chunk-5I2NVRHC.js";
|
|
31
|
+
import {
|
|
32
|
+
AgentInteractionSignal,
|
|
33
|
+
ToolExecutionSuspension,
|
|
34
|
+
approvalInteraction
|
|
35
|
+
} from "./chunk-5IURMWFN.js";
|
|
36
|
+
import {
|
|
37
|
+
normalizeMemoryScope
|
|
38
|
+
} from "./chunk-LAIATEUB.js";
|
|
39
|
+
import {
|
|
40
|
+
AgentRunMemory
|
|
41
|
+
} from "./chunk-F3E2RLC7.js";
|
|
42
|
+
import {
|
|
43
|
+
isQuestionTool
|
|
44
|
+
} from "./chunk-IV6YQTY2.js";
|
|
45
|
+
import {
|
|
46
|
+
startAgentRunObservers
|
|
47
|
+
} from "./chunk-XJRNBTIV.js";
|
|
48
|
+
import {
|
|
49
|
+
MemoryCompactionError
|
|
50
|
+
} from "./chunk-LQJ4KHMY.js";
|
|
51
|
+
import {
|
|
52
|
+
extractRagText
|
|
53
|
+
} from "./chunk-MJCMKZFP.js";
|
|
54
|
+
import {
|
|
55
|
+
createAsyncQueue
|
|
56
|
+
} from "./chunk-EZR5AV3J.js";
|
|
57
|
+
import {
|
|
58
|
+
runControl
|
|
59
|
+
} from "./chunk-FAPIZJS2.js";
|
|
60
|
+
import {
|
|
61
|
+
getAgentToolState
|
|
62
|
+
} from "./chunk-A7BXVZXZ.js";
|
|
63
|
+
import {
|
|
64
|
+
mergeCompletionControlValues
|
|
65
|
+
} from "./chunk-6RQYWZI3.js";
|
|
66
|
+
import {
|
|
67
|
+
assertAgentInteractionResponse,
|
|
68
|
+
parseAgentContinuation,
|
|
69
|
+
parseAgentInteractionResponse
|
|
70
|
+
} from "./chunk-LBYJPMHC.js";
|
|
71
|
+
import {
|
|
72
|
+
composeAgentLifecycle,
|
|
73
|
+
lifecycleSnapshot
|
|
74
|
+
} from "./chunk-KFAVEL5F.js";
|
|
75
|
+
import {
|
|
76
|
+
getAgentProviderOutputSchema
|
|
77
|
+
} from "./chunk-GLMYZ6OA.js";
|
|
78
|
+
import {
|
|
79
|
+
runInputGuardrails,
|
|
80
|
+
runOutputGuardrails
|
|
81
|
+
} from "./chunk-VZ5ZZBNX.js";
|
|
82
|
+
import {
|
|
83
|
+
appendGuardrailPolicies,
|
|
84
|
+
hasEnforcedOutputGuardrails
|
|
85
|
+
} from "./chunk-6BTJC3ES.js";
|
|
86
|
+
import {
|
|
87
|
+
isStreamingCompletionModel
|
|
88
|
+
} from "./chunk-BDKFFFVX.js";
|
|
89
|
+
import {
|
|
90
|
+
createCompletionRequest
|
|
91
|
+
} from "./chunk-CKKNO4XB.js";
|
|
92
|
+
import {
|
|
93
|
+
assertJsonObject
|
|
94
|
+
} from "./chunk-4ONUNWGC.js";
|
|
95
|
+
import {
|
|
96
|
+
CompletionStreamAccumulator
|
|
97
|
+
} from "./chunk-UTAB3XQI.js";
|
|
98
|
+
import {
|
|
99
|
+
assertCompletionResponseIntegrity
|
|
100
|
+
} from "./chunk-XQV3XNVT.js";
|
|
101
|
+
import {
|
|
102
|
+
toolMayRequireApproval
|
|
103
|
+
} from "./chunk-YZVFIW5D.js";
|
|
104
|
+
import {
|
|
105
|
+
Usage,
|
|
106
|
+
assertCompletionRequestSupported,
|
|
107
|
+
getAssistantGenerationMetadata,
|
|
108
|
+
textFromAssistantContent
|
|
109
|
+
} from "./chunk-IDNL6IH4.js";
|
|
110
|
+
import {
|
|
111
|
+
parseMessage,
|
|
112
|
+
parseMessages
|
|
113
|
+
} from "./chunk-BONQTXRQ.js";
|
|
114
|
+
import {
|
|
115
|
+
isJsonValue
|
|
116
|
+
} from "./chunk-4C7AXYOM.js";
|
|
117
|
+
import {
|
|
118
|
+
completionProviderOutputErrorUsage,
|
|
119
|
+
resolveRetryOptions,
|
|
120
|
+
retryDelayMs,
|
|
121
|
+
retryErrorAttributes,
|
|
122
|
+
retryOptionsForFailure,
|
|
123
|
+
waitForRetry
|
|
124
|
+
} from "./chunk-POXHJF3H.js";
|
|
125
|
+
import {
|
|
126
|
+
abortError,
|
|
127
|
+
throwIfAborted
|
|
128
|
+
} from "./chunk-USRKPEQN.js";
|
|
129
|
+
import {
|
|
130
|
+
AgentRunCancelledError,
|
|
131
|
+
AgentStreamClosedError,
|
|
132
|
+
AgentStructuredOutputError,
|
|
133
|
+
MaxTurnsError
|
|
134
|
+
} from "./chunk-WN6AVBO4.js";
|
|
135
|
+
|
|
136
|
+
// src/internal/agent-runtime/agent-run.ts
|
|
137
|
+
async function settleFailureCleanup(operations) {
|
|
138
|
+
for (const operation of operations) {
|
|
139
|
+
try {
|
|
140
|
+
await operation();
|
|
141
|
+
} catch {
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
var AgentRun = class _AgentRun {
|
|
146
|
+
constructor(agent, promptMessage, initialHistory = [], options = {}) {
|
|
147
|
+
this.agent = agent;
|
|
148
|
+
this.promptMessage = promptMessage;
|
|
149
|
+
this.chatHistory = initialHistory;
|
|
150
|
+
this.maxTurnCount = assertNonnegativeSafeInteger(
|
|
151
|
+
options.maxTurns ?? agent.defaultMaxTurns ?? 0,
|
|
152
|
+
"maxTurns"
|
|
153
|
+
);
|
|
154
|
+
const internalOptions = getInternalAgentRunOptions(options);
|
|
155
|
+
this.activeHook = internalOptions?.hook;
|
|
156
|
+
this.onInternalFailure = internalOptions?.onFailure;
|
|
157
|
+
this.beforeFinish = internalOptions?.beforeFinish;
|
|
158
|
+
this.onSteeringApplied = internalOptions?.onSteeringApplied;
|
|
159
|
+
this.onInternalMemoryCompaction = internalOptions?.onMemoryCompaction;
|
|
160
|
+
this.requestedRunId = normalizeRequestedRunId(internalOptions?.runId);
|
|
161
|
+
this.activeLifecycle = composeAgentLifecycle(agent.lifecycle, options.lifecycle);
|
|
162
|
+
this.guardrailPolicies = options.guardrails === void 0 ? [...agent.guardrails] : appendGuardrailPolicies([...agent.guardrails], options.guardrails);
|
|
163
|
+
const configuredConcurrency = assertPositiveSafeInteger(
|
|
164
|
+
options.toolConcurrency ?? 1,
|
|
165
|
+
"toolConcurrency"
|
|
166
|
+
);
|
|
167
|
+
this.concurrency = this.activeHook !== void 0 || agent.tools.some(
|
|
168
|
+
(tool) => toolMayRequireApproval(tool.requiresApproval) || isQuestionTool(tool)
|
|
169
|
+
) ? 1 : configuredConcurrency;
|
|
170
|
+
this.traceOptions = options.trace;
|
|
171
|
+
const retrySetting = options.retries === void 0 ? agent.retries : options.retries;
|
|
172
|
+
this.completionRetryOptions = retrySetting === void 0 || retrySetting === false ? void 0 : resolveRetryOptions(retrySetting);
|
|
173
|
+
this.requestMiddlewares = [...options.middlewares ?? []];
|
|
174
|
+
this.controls = mergeCompletionControlValues(agent.controls, options.controls);
|
|
175
|
+
this.memoryScope = options.memoryScope;
|
|
176
|
+
this.memoryRecorder = new AgentRunMemory(agent, options.memoryScope, initialHistory);
|
|
177
|
+
this.continuationState = options.continuationState;
|
|
178
|
+
this.interactionResponse = options.interactionResponse;
|
|
179
|
+
if (options.continuationState !== void 0) {
|
|
180
|
+
this.steeringMessages.push(
|
|
181
|
+
...options.continuationState.steering.map((entry) => ({
|
|
182
|
+
id: entry.id,
|
|
183
|
+
messages: [...entry.messages]
|
|
184
|
+
}))
|
|
185
|
+
);
|
|
186
|
+
}
|
|
187
|
+
this.resumedFrom = options.sourceRunId === void 0 || options.interactionId === void 0 ? void 0 : { runId: options.sourceRunId, interactionId: options.interactionId };
|
|
188
|
+
this.linkExternalAbortSignal(options.abortSignal);
|
|
189
|
+
}
|
|
190
|
+
agent;
|
|
191
|
+
promptMessage;
|
|
192
|
+
chatHistory;
|
|
193
|
+
maxTurnCount;
|
|
194
|
+
activeHook;
|
|
195
|
+
activeLifecycle;
|
|
196
|
+
guardrailPolicies;
|
|
197
|
+
guardrailDecisions = [];
|
|
198
|
+
concurrency;
|
|
199
|
+
traceOptions;
|
|
200
|
+
generationPromptRef;
|
|
201
|
+
completionRetryOptions;
|
|
202
|
+
requestMiddlewares;
|
|
203
|
+
controls;
|
|
204
|
+
steeringMessages = [];
|
|
205
|
+
runState = "idle";
|
|
206
|
+
memoryRecorder;
|
|
207
|
+
memoryScope;
|
|
208
|
+
onInternalFailure;
|
|
209
|
+
beforeFinish;
|
|
210
|
+
onSteeringApplied;
|
|
211
|
+
onInternalMemoryCompaction;
|
|
212
|
+
memoryCompaction;
|
|
213
|
+
requestedRunId;
|
|
214
|
+
continuationState;
|
|
215
|
+
interactionResponse;
|
|
216
|
+
resumedFrom;
|
|
217
|
+
currentMessages = [];
|
|
218
|
+
cancellationError;
|
|
219
|
+
activeGeneration;
|
|
220
|
+
validatedStructuredOutput;
|
|
221
|
+
failedCompletionUsage = Usage.empty();
|
|
222
|
+
abortController = new AbortController();
|
|
223
|
+
removeExternalAbortListener;
|
|
224
|
+
static fromAgent(agent, options) {
|
|
225
|
+
const normalized = normalizeAgentInput(agent.id, options);
|
|
226
|
+
if (normalized.scope !== void 0 && agent.memory === void 0) {
|
|
227
|
+
throw new TypeError(`Agent "${agent.id}" cannot use a session without a memory store.`);
|
|
228
|
+
}
|
|
229
|
+
return new _AgentRun(agent, normalized.prompt, normalized.history, {
|
|
230
|
+
...options,
|
|
231
|
+
memoryScope: normalized.scope,
|
|
232
|
+
continuationState: normalized.continuationState,
|
|
233
|
+
interactionResponse: normalized.interactionResponse,
|
|
234
|
+
sourceRunId: normalized.sourceRunId,
|
|
235
|
+
interactionId: normalized.interactionId
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
steer(input) {
|
|
239
|
+
if (this.isTerminal() || this.cancellationError !== void 0) {
|
|
240
|
+
throw new AgentStreamClosedError();
|
|
241
|
+
}
|
|
242
|
+
const receipt = Object.freeze({
|
|
243
|
+
id: globalThis.crypto.randomUUID(),
|
|
244
|
+
status: "queued"
|
|
245
|
+
});
|
|
246
|
+
this.steeringMessages.push({ id: receipt.id, messages: normalizeSteeringInput(input) });
|
|
247
|
+
return receipt;
|
|
248
|
+
}
|
|
249
|
+
cancel(reason) {
|
|
250
|
+
if (this.isTerminal() || this.cancellationError !== void 0) {
|
|
251
|
+
return this.cancellationError;
|
|
252
|
+
}
|
|
253
|
+
const messages = this.currentMessages.length === 0 ? [this.promptMessage] : [...this.currentMessages];
|
|
254
|
+
const error = new AgentRunCancelledError([...this.chatHistory, ...messages], reason);
|
|
255
|
+
this.setCancellationError(error);
|
|
256
|
+
return error;
|
|
257
|
+
}
|
|
258
|
+
setCancellationError(error) {
|
|
259
|
+
this.cancellationError = error;
|
|
260
|
+
if (!this.abortController.signal.aborted) this.abortController.abort(error);
|
|
261
|
+
}
|
|
262
|
+
async generate() {
|
|
263
|
+
this.startRun();
|
|
264
|
+
const runId = this.requestedRunId ?? globalThis.crypto.randomUUID();
|
|
265
|
+
let usage = Usage.empty();
|
|
266
|
+
let currentTurns = 0;
|
|
267
|
+
let lastPrompt = this.promptMessage;
|
|
268
|
+
let newMessages = [this.promptMessage];
|
|
269
|
+
let runObservers;
|
|
270
|
+
let pendingTurnMessages = [];
|
|
271
|
+
try {
|
|
272
|
+
this.throwIfCancelled();
|
|
273
|
+
const memoryPreparation = this.continuationState === void 0 ? await this.memoryRecorder.prepareHistory(
|
|
274
|
+
runId,
|
|
275
|
+
newMessages,
|
|
276
|
+
this.abortController.signal
|
|
277
|
+
) : void 0;
|
|
278
|
+
if (memoryPreparation !== void 0) {
|
|
279
|
+
this.chatHistory = memoryPreparation.history;
|
|
280
|
+
usage = Usage.add(usage, memoryPreparation.usage);
|
|
281
|
+
this.memoryCompaction = memoryPreparation.compaction;
|
|
282
|
+
await this.notifyInternalMemoryCompaction(memoryPreparation.compaction);
|
|
283
|
+
}
|
|
284
|
+
runObservers = await this.startRunObservers(runId);
|
|
285
|
+
if (memoryPreparation !== void 0) {
|
|
286
|
+
await this.recordMemoryCompaction(memoryPreparation, runObservers);
|
|
287
|
+
}
|
|
288
|
+
this.throwIfCancelled();
|
|
289
|
+
await this.activeLifecycle?.onStart?.({
|
|
290
|
+
runId,
|
|
291
|
+
input: lifecycleSnapshot(this.promptMessage),
|
|
292
|
+
history: lifecycleSnapshot(this.chatHistory),
|
|
293
|
+
maxTurns: this.maxTurnCount
|
|
294
|
+
});
|
|
295
|
+
if (this.continuationState === void 0) {
|
|
296
|
+
const inputResult = await runInputGuardrails(this.guardrailPolicies, {
|
|
297
|
+
prompt: this.promptMessage,
|
|
298
|
+
history: this.chatHistory,
|
|
299
|
+
inputText: textFromMessage(this.promptMessage),
|
|
300
|
+
run: this.guardrailRunContext(runId)
|
|
301
|
+
});
|
|
302
|
+
for (const decision of inputResult.decisions) {
|
|
303
|
+
await this.recordGuardrailDecision(decision, runObservers);
|
|
304
|
+
}
|
|
305
|
+
this.promptMessage = inputResult.prompt;
|
|
306
|
+
if (inputResult.blocked) {
|
|
307
|
+
const text = inputResult.message ?? "The request was blocked by a guardrail.";
|
|
308
|
+
const result = {
|
|
309
|
+
type: "blocked",
|
|
310
|
+
stage: "input",
|
|
311
|
+
...blockedOutcomeDetails(
|
|
312
|
+
this.guardrailDecisions,
|
|
313
|
+
"The request was blocked by a guardrail."
|
|
314
|
+
),
|
|
315
|
+
runId,
|
|
316
|
+
text,
|
|
317
|
+
usage,
|
|
318
|
+
messages: [this.promptMessage, { role: "assistant", content: text }],
|
|
319
|
+
trace: runObservers.trace,
|
|
320
|
+
guardrails: [...this.guardrailDecisions],
|
|
321
|
+
...this.memoryCompactionResult()
|
|
322
|
+
};
|
|
323
|
+
if (this.resumedFrom !== void 0) result.resumedFrom = this.resumedFrom;
|
|
324
|
+
this.runState = "closing";
|
|
325
|
+
await this.runLifecycleFinish(result);
|
|
326
|
+
await runObservers.end(observerRunEnd(result));
|
|
327
|
+
this.runState = "completed";
|
|
328
|
+
this.disposeAbortLink();
|
|
329
|
+
return result;
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
newMessages = [this.promptMessage];
|
|
333
|
+
await this.memoryRecorder.commitAcceptedInput(runId, newMessages);
|
|
334
|
+
pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
|
|
335
|
+
await this.runRunStartHook(newMessages);
|
|
336
|
+
if (this.continuationState !== void 0) {
|
|
337
|
+
const interactionAttributes = {};
|
|
338
|
+
if (this.resumedFrom?.runId !== void 0) {
|
|
339
|
+
interactionAttributes.sourceRunId = this.resumedFrom.runId;
|
|
340
|
+
}
|
|
341
|
+
if (this.resumedFrom?.interactionId !== void 0) {
|
|
342
|
+
interactionAttributes.interactionId = this.resumedFrom.interactionId;
|
|
343
|
+
}
|
|
344
|
+
if (this.interactionResponse?.type !== void 0) {
|
|
345
|
+
interactionAttributes.interactionType = this.interactionResponse.type;
|
|
346
|
+
}
|
|
347
|
+
await runObservers.event({
|
|
348
|
+
name: "agent.interaction_response",
|
|
349
|
+
attributes: interactionAttributes
|
|
350
|
+
});
|
|
351
|
+
try {
|
|
352
|
+
const toolResults = await this.resolveContinuationTools(
|
|
353
|
+
runId,
|
|
354
|
+
newMessages,
|
|
355
|
+
void 0,
|
|
356
|
+
void 0,
|
|
357
|
+
{
|
|
358
|
+
turn: 1,
|
|
359
|
+
runObservers
|
|
360
|
+
}
|
|
361
|
+
);
|
|
362
|
+
const toolMessage = { role: "tool", content: toolResults };
|
|
363
|
+
newMessages.push(toolMessage);
|
|
364
|
+
await this.memoryRecorder.commitMessages(runId, 1, [toolMessage], pendingTurnMessages);
|
|
365
|
+
await this.drainSteeringMessages(runId, 1, newMessages, pendingTurnMessages);
|
|
366
|
+
await this.memoryRecorder.commitCompletedTurn(runId, 1, pendingTurnMessages);
|
|
367
|
+
} catch (error) {
|
|
368
|
+
if (error instanceof ToolExecutionSuspension) {
|
|
369
|
+
return this.finishSuspension({
|
|
370
|
+
runId,
|
|
371
|
+
turn: 1,
|
|
372
|
+
usage,
|
|
373
|
+
newMessages,
|
|
374
|
+
pendingTurnMessages,
|
|
375
|
+
runObservers,
|
|
376
|
+
suspension: error,
|
|
377
|
+
uncommittedMessages: []
|
|
378
|
+
});
|
|
379
|
+
}
|
|
380
|
+
throw error;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
while (currentTurns <= this.maxTurnCount) {
|
|
384
|
+
const prompt = newMessages.at(-1);
|
|
385
|
+
if (prompt === void 0) {
|
|
386
|
+
throw new Error("AgentRun requires at least one message");
|
|
387
|
+
}
|
|
388
|
+
lastPrompt = prompt;
|
|
389
|
+
currentTurns += 1;
|
|
390
|
+
const historyForRequest = [...this.chatHistory, ...newMessages.slice(0, -1)];
|
|
391
|
+
await this.runTurnStartHook(currentTurns, prompt, historyForRequest, newMessages);
|
|
392
|
+
await this.runCompletionCallHook(prompt, historyForRequest, newMessages);
|
|
393
|
+
const request = await this.createTurnRequest(prompt, historyForRequest, currentTurns);
|
|
394
|
+
let response;
|
|
395
|
+
try {
|
|
396
|
+
response = await this.runCompletion(request, currentTurns, runObservers);
|
|
397
|
+
} catch (error) {
|
|
398
|
+
await settleFailureCleanup([
|
|
399
|
+
() => this.runCompletionErrorHook(prompt, error, newMessages)
|
|
400
|
+
]);
|
|
401
|
+
throw error;
|
|
402
|
+
}
|
|
403
|
+
response = await this.runCompletionResponseMiddlewares(request, response, currentTurns);
|
|
404
|
+
try {
|
|
405
|
+
assertCompletionResponseIntegrity({ response });
|
|
406
|
+
} catch (error) {
|
|
407
|
+
const providerOutputUsage = completionProviderOutputErrorUsage(error);
|
|
408
|
+
if (providerOutputUsage !== void 0) usage = Usage.add(usage, providerOutputUsage);
|
|
409
|
+
throw error;
|
|
410
|
+
}
|
|
411
|
+
usage = Usage.add(usage, response.usage);
|
|
412
|
+
this.updateRunProgress(newMessages);
|
|
413
|
+
await this.runCompletionResponseHook(prompt, response, newMessages);
|
|
414
|
+
await this.runTurnEndHook(currentTurns, response, newMessages);
|
|
415
|
+
await this.activeLifecycle?.onStepFinish?.({
|
|
416
|
+
runId,
|
|
417
|
+
step: currentTurns,
|
|
418
|
+
response: lifecycleSnapshot(response),
|
|
419
|
+
usage: lifecycleSnapshot(usage)
|
|
420
|
+
});
|
|
421
|
+
const toolCalls = response.choice.filter(
|
|
422
|
+
(item) => item.type === "tool-call"
|
|
423
|
+
);
|
|
424
|
+
const assistantMessage = this.generatedAssistantMessage(response, request);
|
|
425
|
+
newMessages.push(assistantMessage);
|
|
426
|
+
if (toolCalls.length === 0) {
|
|
427
|
+
await this.beforeFinish?.();
|
|
428
|
+
if (this.steeringMessages.length > 0) {
|
|
429
|
+
await this.memoryRecorder.commitMessages(
|
|
430
|
+
runId,
|
|
431
|
+
currentTurns,
|
|
432
|
+
[assistantMessage],
|
|
433
|
+
pendingTurnMessages
|
|
434
|
+
);
|
|
435
|
+
}
|
|
436
|
+
const appliedSteering = await this.drainSteeringMessages(
|
|
437
|
+
runId,
|
|
438
|
+
currentTurns,
|
|
439
|
+
newMessages,
|
|
440
|
+
pendingTurnMessages,
|
|
441
|
+
{ closeWhenEmpty: true }
|
|
442
|
+
);
|
|
443
|
+
if (appliedSteering.length > 0) {
|
|
444
|
+
for (const receipt of appliedSteering) {
|
|
445
|
+
await runObservers.event({
|
|
446
|
+
name: "agent.steering_applied",
|
|
447
|
+
attributes: { id: receipt.id, turn: currentTurns }
|
|
448
|
+
});
|
|
449
|
+
}
|
|
450
|
+
await this.memoryRecorder.commitCompletedTurn(runId, currentTurns, pendingTurnMessages);
|
|
451
|
+
continue;
|
|
452
|
+
}
|
|
453
|
+
const guardedOutput = await this.runOutputGuardrailsForResponse(
|
|
454
|
+
runId,
|
|
455
|
+
usage,
|
|
456
|
+
response,
|
|
457
|
+
newMessages,
|
|
458
|
+
runObservers
|
|
459
|
+
);
|
|
460
|
+
response = guardedOutput.response;
|
|
461
|
+
const finalAssistantMessage = this.generatedAssistantMessage(response, request);
|
|
462
|
+
newMessages[newMessages.length - 1] = finalAssistantMessage;
|
|
463
|
+
await this.memoryRecorder.commitMessages(
|
|
464
|
+
runId,
|
|
465
|
+
currentTurns,
|
|
466
|
+
[finalAssistantMessage],
|
|
467
|
+
pendingTurnMessages
|
|
468
|
+
);
|
|
469
|
+
const result = this.createTerminalResult(
|
|
470
|
+
runId,
|
|
471
|
+
guardedOutput.text,
|
|
472
|
+
guardedOutput.blocked,
|
|
473
|
+
usage,
|
|
474
|
+
newMessages,
|
|
475
|
+
runObservers
|
|
476
|
+
);
|
|
477
|
+
if (result.type === "response") {
|
|
478
|
+
await this.runRunEndHook(result, newMessages);
|
|
479
|
+
}
|
|
480
|
+
await this.memoryRecorder.commitCompletedRun(
|
|
481
|
+
runId,
|
|
482
|
+
currentTurns,
|
|
483
|
+
newMessages,
|
|
484
|
+
pendingTurnMessages
|
|
485
|
+
);
|
|
486
|
+
await this.runLifecycleFinish(result);
|
|
487
|
+
await runObservers.end(observerRunEnd(result));
|
|
488
|
+
this.runState = "completed";
|
|
489
|
+
this.disposeAbortLink();
|
|
490
|
+
return result;
|
|
491
|
+
}
|
|
492
|
+
this.updateRunProgress(newMessages);
|
|
493
|
+
let toolResults;
|
|
494
|
+
try {
|
|
495
|
+
toolResults = await this.executeToolCalls(
|
|
496
|
+
runId,
|
|
497
|
+
toolCalls,
|
|
498
|
+
newMessages,
|
|
499
|
+
void 0,
|
|
500
|
+
void 0,
|
|
501
|
+
{
|
|
502
|
+
turn: currentTurns,
|
|
503
|
+
runObservers,
|
|
504
|
+
toolDefinitions: request.tools
|
|
505
|
+
}
|
|
506
|
+
);
|
|
507
|
+
} catch (error) {
|
|
508
|
+
if (error instanceof ToolExecutionSuspension) {
|
|
509
|
+
return this.finishSuspension({
|
|
510
|
+
runId,
|
|
511
|
+
turn: currentTurns,
|
|
512
|
+
usage,
|
|
513
|
+
newMessages,
|
|
514
|
+
pendingTurnMessages,
|
|
515
|
+
runObservers,
|
|
516
|
+
suspension: error,
|
|
517
|
+
uncommittedMessages: [assistantMessage]
|
|
518
|
+
});
|
|
519
|
+
}
|
|
520
|
+
throw error;
|
|
521
|
+
}
|
|
522
|
+
const toolMessage = { role: "tool", content: toolResults };
|
|
523
|
+
newMessages.push(toolMessage);
|
|
524
|
+
await this.memoryRecorder.commitMessages(
|
|
525
|
+
runId,
|
|
526
|
+
currentTurns,
|
|
527
|
+
[assistantMessage, toolMessage],
|
|
528
|
+
pendingTurnMessages
|
|
529
|
+
);
|
|
530
|
+
await this.drainSteeringMessages(runId, currentTurns, newMessages, pendingTurnMessages);
|
|
531
|
+
await this.memoryRecorder.commitCompletedTurn(runId, currentTurns, pendingTurnMessages);
|
|
532
|
+
}
|
|
533
|
+
throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
|
|
534
|
+
} catch (error) {
|
|
535
|
+
if (error instanceof MemoryCompactionError && error.usage !== void 0) {
|
|
536
|
+
usage = Usage.add(usage, error.usage);
|
|
537
|
+
}
|
|
538
|
+
const failedCompletionUsage = this.takeFailedCompletionUsage();
|
|
539
|
+
if (error instanceof AgentStructuredOutputError) {
|
|
540
|
+
usage = Usage.add(usage, error.usage);
|
|
541
|
+
} else {
|
|
542
|
+
usage = Usage.add(usage, failedCompletionUsage);
|
|
543
|
+
}
|
|
544
|
+
this.runState = "closing";
|
|
545
|
+
const runError = this.normalizeRunError(error);
|
|
546
|
+
const reportedError = await this.reportRunFailure(
|
|
547
|
+
runError,
|
|
548
|
+
runId,
|
|
549
|
+
usage,
|
|
550
|
+
newMessages,
|
|
551
|
+
runObservers
|
|
552
|
+
);
|
|
553
|
+
this.runState = reportedError instanceof AgentRunCancelledError ? "cancelled" : "errored";
|
|
554
|
+
this.disposeAbortLink();
|
|
555
|
+
throw reportedError;
|
|
556
|
+
}
|
|
557
|
+
}
|
|
558
|
+
async *events() {
|
|
559
|
+
this.startRun();
|
|
560
|
+
const runId = this.requestedRunId ?? globalThis.crypto.randomUUID();
|
|
561
|
+
let usage = Usage.empty();
|
|
562
|
+
let currentTurns = 0;
|
|
563
|
+
let lastPrompt = this.promptMessage;
|
|
564
|
+
let newMessages = [this.promptMessage];
|
|
565
|
+
const bufferOutputDeltas = hasEnforcedOutputGuardrails(this.guardrailPolicies);
|
|
566
|
+
let runObservers;
|
|
567
|
+
let pendingTurnMessages = [];
|
|
568
|
+
try {
|
|
569
|
+
this.throwIfCancelled();
|
|
570
|
+
const memoryPreparation = this.continuationState === void 0 ? await this.memoryRecorder.prepareHistory(
|
|
571
|
+
runId,
|
|
572
|
+
newMessages,
|
|
573
|
+
this.abortController.signal
|
|
574
|
+
) : void 0;
|
|
575
|
+
if (memoryPreparation !== void 0) {
|
|
576
|
+
this.chatHistory = memoryPreparation.history;
|
|
577
|
+
usage = Usage.add(usage, memoryPreparation.usage);
|
|
578
|
+
this.memoryCompaction = memoryPreparation.compaction;
|
|
579
|
+
await this.notifyInternalMemoryCompaction(memoryPreparation.compaction);
|
|
580
|
+
}
|
|
581
|
+
runObservers = await this.startRunObservers(runId);
|
|
582
|
+
if (memoryPreparation !== void 0) {
|
|
583
|
+
await this.recordMemoryCompaction(memoryPreparation, runObservers);
|
|
584
|
+
}
|
|
585
|
+
if (memoryPreparation?.compaction !== void 0) {
|
|
586
|
+
yield { type: "memory_compaction", ...memoryPreparation.compaction };
|
|
587
|
+
}
|
|
588
|
+
this.throwIfCancelled();
|
|
589
|
+
await this.activeLifecycle?.onStart?.({
|
|
590
|
+
runId,
|
|
591
|
+
input: lifecycleSnapshot(this.promptMessage),
|
|
592
|
+
history: lifecycleSnapshot(this.chatHistory),
|
|
593
|
+
maxTurns: this.maxTurnCount
|
|
594
|
+
});
|
|
595
|
+
if (this.continuationState === void 0) {
|
|
596
|
+
const inputResult = await runInputGuardrails(this.guardrailPolicies, {
|
|
597
|
+
prompt: this.promptMessage,
|
|
598
|
+
history: this.chatHistory,
|
|
599
|
+
inputText: textFromMessage(this.promptMessage),
|
|
600
|
+
run: this.guardrailRunContext(runId)
|
|
601
|
+
});
|
|
602
|
+
for (const decision of inputResult.decisions) {
|
|
603
|
+
await this.recordGuardrailDecision(decision, runObservers);
|
|
604
|
+
yield { type: "guardrail_decision", decision };
|
|
605
|
+
}
|
|
606
|
+
this.promptMessage = inputResult.prompt;
|
|
607
|
+
if (inputResult.blocked) {
|
|
608
|
+
const text = inputResult.message ?? "The request was blocked by a guardrail.";
|
|
609
|
+
const result = {
|
|
610
|
+
type: "blocked",
|
|
611
|
+
stage: "input",
|
|
612
|
+
...blockedOutcomeDetails(
|
|
613
|
+
this.guardrailDecisions,
|
|
614
|
+
"The request was blocked by a guardrail."
|
|
615
|
+
),
|
|
616
|
+
runId,
|
|
617
|
+
text,
|
|
618
|
+
usage,
|
|
619
|
+
messages: [this.promptMessage, { role: "assistant", content: text }],
|
|
620
|
+
trace: runObservers.trace,
|
|
621
|
+
guardrails: [...this.guardrailDecisions],
|
|
622
|
+
...this.memoryCompactionResult()
|
|
623
|
+
};
|
|
624
|
+
if (this.resumedFrom !== void 0) result.resumedFrom = this.resumedFrom;
|
|
625
|
+
this.runState = "closing";
|
|
626
|
+
await this.runLifecycleFinish(result);
|
|
627
|
+
await runObservers.end(observerRunEnd(result));
|
|
628
|
+
this.runState = "completed";
|
|
629
|
+
this.disposeAbortLink();
|
|
630
|
+
yield result;
|
|
631
|
+
return;
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
newMessages = [this.promptMessage];
|
|
635
|
+
await this.memoryRecorder.commitAcceptedInput(runId, newMessages);
|
|
636
|
+
pendingTurnMessages = this.memoryRecorder.pendingTurnMessages(newMessages);
|
|
637
|
+
await this.runRunStartHook(newMessages);
|
|
638
|
+
if (this.continuationState !== void 0) {
|
|
639
|
+
const responsePart = this.promptMessage.role === "tool" ? this.promptMessage.content[0] : void 0;
|
|
640
|
+
if (responsePart === void 0 || responsePart.type === "tool-result") {
|
|
641
|
+
throw new TypeError("Agent continuation response message is invalid.");
|
|
642
|
+
}
|
|
643
|
+
yield {
|
|
644
|
+
type: "interaction_response",
|
|
645
|
+
response: responsePart,
|
|
646
|
+
sourceRunId: this.resumedFrom?.runId ?? this.continuationState.kind
|
|
647
|
+
};
|
|
648
|
+
try {
|
|
649
|
+
const execution = this.executeContinuationToolStream(runId, newMessages, {
|
|
650
|
+
turn: 1,
|
|
651
|
+
runObservers
|
|
652
|
+
});
|
|
653
|
+
for await (const event of execution.events) {
|
|
654
|
+
yield { turn: 1, ...event };
|
|
655
|
+
}
|
|
656
|
+
const toolResults = await execution.results;
|
|
657
|
+
const toolMessage = { role: "tool", content: toolResults };
|
|
658
|
+
newMessages.push(toolMessage);
|
|
659
|
+
await this.memoryRecorder.commitMessages(runId, 1, [toolMessage], pendingTurnMessages);
|
|
660
|
+
for (const receipt of await this.drainSteeringMessages(
|
|
661
|
+
runId,
|
|
662
|
+
1,
|
|
663
|
+
newMessages,
|
|
664
|
+
pendingTurnMessages
|
|
665
|
+
)) {
|
|
666
|
+
yield { type: "steering_applied", id: receipt.id, turn: 1 };
|
|
667
|
+
}
|
|
668
|
+
await this.memoryRecorder.commitCompletedTurn(runId, 1, pendingTurnMessages);
|
|
669
|
+
} catch (error) {
|
|
670
|
+
if (error instanceof ToolExecutionSuspension) {
|
|
671
|
+
const result = await this.finishSuspension({
|
|
672
|
+
runId,
|
|
673
|
+
turn: 1,
|
|
674
|
+
usage,
|
|
675
|
+
newMessages,
|
|
676
|
+
pendingTurnMessages,
|
|
677
|
+
runObservers,
|
|
678
|
+
suspension: error,
|
|
679
|
+
uncommittedMessages: []
|
|
680
|
+
});
|
|
681
|
+
yield result;
|
|
682
|
+
return;
|
|
683
|
+
}
|
|
684
|
+
throw error;
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
while (currentTurns <= this.maxTurnCount) {
|
|
688
|
+
const prompt = newMessages.at(-1);
|
|
689
|
+
if (prompt === void 0) {
|
|
690
|
+
throw new Error("AgentRun requires at least one message");
|
|
691
|
+
}
|
|
692
|
+
lastPrompt = prompt;
|
|
693
|
+
currentTurns += 1;
|
|
694
|
+
const historyForRequest = [...this.chatHistory, ...newMessages.slice(0, -1)];
|
|
695
|
+
yield {
|
|
696
|
+
type: "turn_start",
|
|
697
|
+
turn: currentTurns,
|
|
698
|
+
prompt,
|
|
699
|
+
history: historyForRequest
|
|
700
|
+
};
|
|
701
|
+
await this.runTurnStartHook(currentTurns, prompt, historyForRequest, newMessages);
|
|
702
|
+
await this.runCompletionCallHook(prompt, historyForRequest, newMessages);
|
|
703
|
+
const request = await this.createTurnRequest(prompt, historyForRequest, currentTurns);
|
|
704
|
+
assertCompletionRequestSupported(this.agent.model, request, { streaming: true });
|
|
705
|
+
const providerRequest = this.providerTraceRequest(request, { stream: true });
|
|
706
|
+
const generationStartArgs = this.generationStartArgs(
|
|
707
|
+
currentTurns,
|
|
708
|
+
request,
|
|
709
|
+
providerRequest
|
|
710
|
+
);
|
|
711
|
+
const generationObservers = await runObservers.startGeneration(generationStartArgs);
|
|
712
|
+
this.activeGeneration = { turn: currentTurns, observers: generationObservers };
|
|
713
|
+
const generationStartedAt = Date.now();
|
|
714
|
+
yield {
|
|
715
|
+
type: "generation_start",
|
|
716
|
+
turn: currentTurns,
|
|
717
|
+
request,
|
|
718
|
+
modelInfo: generationStartArgs.modelInfo,
|
|
719
|
+
promptRef: generationStartArgs.promptRef
|
|
720
|
+
};
|
|
721
|
+
const bufferResponseEvents = this.shouldBufferStreamResponseEvents() || this.agent.outputSchema !== void 0;
|
|
722
|
+
const completionState = {
|
|
723
|
+
response: void 0,
|
|
724
|
+
firstDeltaMs: void 0,
|
|
725
|
+
emittedToolCallIds: /* @__PURE__ */ new Set(),
|
|
726
|
+
providerErrorUsage: Usage.empty()
|
|
727
|
+
};
|
|
728
|
+
let response;
|
|
729
|
+
try {
|
|
730
|
+
try {
|
|
731
|
+
for await (const event of this.streamCompletion({
|
|
732
|
+
request,
|
|
733
|
+
turn: currentTurns,
|
|
734
|
+
bufferResponseEvents,
|
|
735
|
+
bufferOutputDeltas,
|
|
736
|
+
generationStartedAt,
|
|
737
|
+
generationObservers,
|
|
738
|
+
runObservers,
|
|
739
|
+
state: completionState
|
|
740
|
+
})) {
|
|
741
|
+
yield event;
|
|
742
|
+
}
|
|
743
|
+
} finally {
|
|
744
|
+
usage = Usage.add(usage, completionState.providerErrorUsage);
|
|
745
|
+
}
|
|
746
|
+
if (completionState.response === void 0) {
|
|
747
|
+
throw new Error("Streaming completion ended without a response.");
|
|
748
|
+
}
|
|
749
|
+
response = completionState.response;
|
|
750
|
+
} catch (error) {
|
|
751
|
+
await settleFailureCleanup([
|
|
752
|
+
() => this.closeActiveGeneration(error),
|
|
753
|
+
() => this.runCompletionErrorHook(prompt, error, newMessages)
|
|
754
|
+
]);
|
|
755
|
+
throw error;
|
|
756
|
+
}
|
|
757
|
+
const { firstDeltaMs, emittedToolCallIds } = completionState;
|
|
758
|
+
let generationEndArgs = {
|
|
759
|
+
turn: currentTurns,
|
|
760
|
+
response
|
|
761
|
+
};
|
|
762
|
+
if (firstDeltaMs !== void 0) {
|
|
763
|
+
generationEndArgs = { ...generationEndArgs, firstDeltaMs };
|
|
764
|
+
}
|
|
765
|
+
this.activeGeneration = void 0;
|
|
766
|
+
await generationObservers.end(generationEndArgs);
|
|
767
|
+
response = await this.runCompletionResponseMiddlewares(request, response, currentTurns);
|
|
768
|
+
try {
|
|
769
|
+
assertCompletionResponseIntegrity({ response });
|
|
770
|
+
} catch (error) {
|
|
771
|
+
const providerOutputUsage = completionProviderOutputErrorUsage(error);
|
|
772
|
+
if (providerOutputUsage !== void 0) usage = Usage.add(usage, providerOutputUsage);
|
|
773
|
+
throw error;
|
|
774
|
+
}
|
|
775
|
+
usage = Usage.add(usage, response.usage);
|
|
776
|
+
this.updateRunProgress(newMessages);
|
|
777
|
+
await this.runCompletionResponseHook(prompt, response, newMessages);
|
|
778
|
+
await this.runTurnEndHook(currentTurns, response, newMessages);
|
|
779
|
+
await this.activeLifecycle?.onStepFinish?.({
|
|
780
|
+
runId,
|
|
781
|
+
step: currentTurns,
|
|
782
|
+
response: lifecycleSnapshot(response),
|
|
783
|
+
usage: lifecycleSnapshot(usage)
|
|
784
|
+
});
|
|
785
|
+
const toolCalls = response.choice.filter(
|
|
786
|
+
(item) => item.type === "tool-call"
|
|
787
|
+
);
|
|
788
|
+
const assistantMessage = this.generatedAssistantMessage(response, request);
|
|
789
|
+
newMessages.push(assistantMessage);
|
|
790
|
+
if (toolCalls.length === 0) {
|
|
791
|
+
await this.beforeFinish?.();
|
|
792
|
+
let emittedTurnEnd = false;
|
|
793
|
+
if (!bufferOutputDeltas) {
|
|
794
|
+
if (bufferResponseEvents) {
|
|
795
|
+
for (const event of responseStreamEvents(currentTurns, response)) {
|
|
796
|
+
yield event;
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
yield {
|
|
800
|
+
type: "turn_end",
|
|
801
|
+
turn: currentTurns,
|
|
802
|
+
response,
|
|
803
|
+
firstDeltaMs
|
|
804
|
+
};
|
|
805
|
+
emittedTurnEnd = true;
|
|
806
|
+
}
|
|
807
|
+
if (this.steeringMessages.length > 0) {
|
|
808
|
+
await this.memoryRecorder.commitMessages(
|
|
809
|
+
runId,
|
|
810
|
+
currentTurns,
|
|
811
|
+
[assistantMessage],
|
|
812
|
+
pendingTurnMessages
|
|
813
|
+
);
|
|
814
|
+
}
|
|
815
|
+
const appliedSteering = await this.drainSteeringMessages(
|
|
816
|
+
runId,
|
|
817
|
+
currentTurns,
|
|
818
|
+
newMessages,
|
|
819
|
+
pendingTurnMessages,
|
|
820
|
+
{ closeWhenEmpty: true }
|
|
821
|
+
);
|
|
822
|
+
for (const receipt of appliedSteering) {
|
|
823
|
+
yield { type: "steering_applied", id: receipt.id, turn: currentTurns };
|
|
824
|
+
}
|
|
825
|
+
if (appliedSteering.length > 0) {
|
|
826
|
+
await this.memoryRecorder.commitCompletedTurn(runId, currentTurns, pendingTurnMessages);
|
|
827
|
+
continue;
|
|
828
|
+
}
|
|
829
|
+
for await (const event of this.completeStreamingRun({
|
|
830
|
+
runId,
|
|
831
|
+
turn: currentTurns,
|
|
832
|
+
request,
|
|
833
|
+
response,
|
|
834
|
+
firstDeltaMs,
|
|
835
|
+
usage,
|
|
836
|
+
newMessages,
|
|
837
|
+
pendingTurnMessages,
|
|
838
|
+
runObservers,
|
|
839
|
+
bufferResponseEvents,
|
|
840
|
+
bufferOutputDeltas,
|
|
841
|
+
emittedTurnEnd
|
|
842
|
+
})) {
|
|
843
|
+
yield event;
|
|
844
|
+
}
|
|
845
|
+
return;
|
|
846
|
+
}
|
|
847
|
+
if (bufferResponseEvents) {
|
|
848
|
+
for (const event of responseStreamEvents(currentTurns, response)) {
|
|
849
|
+
yield event;
|
|
850
|
+
}
|
|
851
|
+
} else {
|
|
852
|
+
for (const toolCall of toolCalls) {
|
|
853
|
+
if (!emittedToolCallIds.has(toolCall.toolCallId)) {
|
|
854
|
+
yield { type: "tool_call", turn: currentTurns, toolCall };
|
|
855
|
+
}
|
|
856
|
+
}
|
|
857
|
+
}
|
|
858
|
+
this.updateRunProgress(newMessages);
|
|
859
|
+
yield {
|
|
860
|
+
type: "turn_end",
|
|
861
|
+
turn: currentTurns,
|
|
862
|
+
response,
|
|
863
|
+
firstDeltaMs
|
|
864
|
+
};
|
|
865
|
+
let toolResults;
|
|
866
|
+
try {
|
|
867
|
+
const toolExecution = this.executeStreamingToolCalls(runId, toolCalls, newMessages, {
|
|
868
|
+
turn: currentTurns,
|
|
869
|
+
runObservers,
|
|
870
|
+
toolDefinitions: request.tools
|
|
871
|
+
});
|
|
872
|
+
for await (const result of toolExecution.events) {
|
|
873
|
+
yield { turn: currentTurns, ...result };
|
|
874
|
+
}
|
|
875
|
+
toolResults = await toolExecution.results;
|
|
876
|
+
} catch (error) {
|
|
877
|
+
if (error instanceof ToolExecutionSuspension) {
|
|
878
|
+
const result = await this.finishSuspension({
|
|
879
|
+
runId,
|
|
880
|
+
turn: currentTurns,
|
|
881
|
+
usage,
|
|
882
|
+
newMessages,
|
|
883
|
+
pendingTurnMessages,
|
|
884
|
+
runObservers,
|
|
885
|
+
suspension: error,
|
|
886
|
+
uncommittedMessages: [assistantMessage]
|
|
887
|
+
});
|
|
888
|
+
yield result;
|
|
889
|
+
return;
|
|
890
|
+
}
|
|
891
|
+
throw error;
|
|
892
|
+
}
|
|
893
|
+
const toolMessage = { role: "tool", content: toolResults };
|
|
894
|
+
newMessages.push(toolMessage);
|
|
895
|
+
await this.memoryRecorder.commitMessages(
|
|
896
|
+
runId,
|
|
897
|
+
currentTurns,
|
|
898
|
+
[assistantMessage, toolMessage],
|
|
899
|
+
pendingTurnMessages
|
|
900
|
+
);
|
|
901
|
+
for (const receipt of await this.drainSteeringMessages(
|
|
902
|
+
runId,
|
|
903
|
+
currentTurns,
|
|
904
|
+
newMessages,
|
|
905
|
+
pendingTurnMessages
|
|
906
|
+
)) {
|
|
907
|
+
yield { type: "steering_applied", id: receipt.id, turn: currentTurns };
|
|
908
|
+
}
|
|
909
|
+
await this.memoryRecorder.commitCompletedTurn(runId, currentTurns, pendingTurnMessages);
|
|
910
|
+
}
|
|
911
|
+
throw new MaxTurnsError(this.maxTurnCount, [...this.chatHistory, ...newMessages], lastPrompt);
|
|
912
|
+
} catch (error) {
|
|
913
|
+
if (error instanceof MemoryCompactionError && error.usage !== void 0) {
|
|
914
|
+
usage = Usage.add(usage, error.usage);
|
|
915
|
+
}
|
|
916
|
+
if (error instanceof AgentStructuredOutputError) {
|
|
917
|
+
usage = Usage.add(usage, error.usage);
|
|
918
|
+
}
|
|
919
|
+
this.runState = "closing";
|
|
920
|
+
const runError = this.normalizeRunError(error);
|
|
921
|
+
const reportedError = await this.reportRunFailure(
|
|
922
|
+
runError,
|
|
923
|
+
runId,
|
|
924
|
+
usage,
|
|
925
|
+
newMessages,
|
|
926
|
+
runObservers
|
|
927
|
+
);
|
|
928
|
+
this.runState = reportedError instanceof AgentRunCancelledError ? "cancelled" : "errored";
|
|
929
|
+
const finalUsage = usage;
|
|
930
|
+
yield { type: "error", error: reportedError, usage: finalUsage };
|
|
931
|
+
this.disposeAbortLink();
|
|
932
|
+
return;
|
|
933
|
+
} finally {
|
|
934
|
+
if (this.runState === "running" || this.runState === "closing") {
|
|
935
|
+
const cancellation = this.cancellationError ?? new AgentRunCancelledError([...this.chatHistory, ...newMessages], "Agent stream closed.");
|
|
936
|
+
this.cancellationError = cancellation;
|
|
937
|
+
await settleFailureCleanup([() => this.closeActiveGeneration(cancellation)]);
|
|
938
|
+
await this.reportRunFailure(cancellation, runId, usage, newMessages, runObservers);
|
|
939
|
+
this.runState = "cancelled";
|
|
940
|
+
this.disposeAbortLink();
|
|
941
|
+
}
|
|
942
|
+
}
|
|
943
|
+
}
|
|
944
|
+
async runCompletion(request, turn, runObservers) {
|
|
945
|
+
assertCompletionRequestSupported(this.agent.model, request);
|
|
946
|
+
this.validatedStructuredOutput = void 0;
|
|
947
|
+
this.failedCompletionUsage = Usage.empty();
|
|
948
|
+
const providerRequest = this.providerTraceRequest(request);
|
|
949
|
+
const generationObservers = await runObservers.startGeneration(
|
|
950
|
+
this.generationStartArgs(turn, request, providerRequest)
|
|
951
|
+
);
|
|
952
|
+
let currentRequest = request;
|
|
953
|
+
let failedAttemptUsage = Usage.empty();
|
|
954
|
+
try {
|
|
955
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
956
|
+
let response;
|
|
957
|
+
try {
|
|
958
|
+
this.throwIfCancelled();
|
|
959
|
+
response = await this.agent.model.completion(currentRequest, this.modelCallOptions());
|
|
960
|
+
assertCompletionResponseIntegrity({ response });
|
|
961
|
+
} catch (error) {
|
|
962
|
+
const attemptUsage = completionProviderOutputErrorUsage(error);
|
|
963
|
+
if (attemptUsage !== void 0) {
|
|
964
|
+
failedAttemptUsage = Usage.add(failedAttemptUsage, attemptUsage);
|
|
965
|
+
this.failedCompletionUsage = failedAttemptUsage;
|
|
966
|
+
}
|
|
967
|
+
const retryOptions = this.retryOptionsForFailure(error, attempt, turn, false);
|
|
968
|
+
if (retryOptions === void 0) {
|
|
969
|
+
throw error;
|
|
970
|
+
}
|
|
971
|
+
await this.scheduleCompletionRetry(
|
|
972
|
+
error,
|
|
973
|
+
attempt,
|
|
974
|
+
turn,
|
|
975
|
+
false,
|
|
976
|
+
retryOptions,
|
|
977
|
+
runObservers,
|
|
978
|
+
providerOutputRetryEventAttributes(error, failedAttemptUsage)
|
|
979
|
+
);
|
|
980
|
+
continue;
|
|
981
|
+
}
|
|
982
|
+
const cumulativeUsage = Usage.add(failedAttemptUsage, response.usage);
|
|
983
|
+
try {
|
|
984
|
+
this.validateStructuredResponse(response, attempt, cumulativeUsage);
|
|
985
|
+
} catch (error) {
|
|
986
|
+
const retryOptions = this.retryOptionsForFailure(error, attempt, turn, false);
|
|
987
|
+
if (retryOptions === void 0) {
|
|
988
|
+
throw error;
|
|
989
|
+
}
|
|
990
|
+
failedAttemptUsage = cumulativeUsage;
|
|
991
|
+
this.failedCompletionUsage = failedAttemptUsage;
|
|
992
|
+
const retryRequest = structuredOutputRetryRequest(request, response, error);
|
|
993
|
+
currentRequest = retryRequest.request;
|
|
994
|
+
await this.scheduleCompletionRetry(
|
|
995
|
+
error,
|
|
996
|
+
attempt,
|
|
997
|
+
turn,
|
|
998
|
+
false,
|
|
999
|
+
retryOptions,
|
|
1000
|
+
runObservers,
|
|
1001
|
+
structuredOutputRetryEventAttributes(error, retryRequest)
|
|
1002
|
+
);
|
|
1003
|
+
continue;
|
|
1004
|
+
}
|
|
1005
|
+
const finalResponse = Usage.isEmpty(failedAttemptUsage) ? response : { ...response, usage: cumulativeUsage };
|
|
1006
|
+
await generationObservers.end({ turn, response: finalResponse });
|
|
1007
|
+
this.failedCompletionUsage = Usage.empty();
|
|
1008
|
+
return finalResponse;
|
|
1009
|
+
}
|
|
1010
|
+
} catch (error) {
|
|
1011
|
+
await settleFailureCleanup([() => generationObservers.error({ turn, error })]);
|
|
1012
|
+
throw error;
|
|
1013
|
+
}
|
|
1014
|
+
}
|
|
1015
|
+
async createTurnRequest(prompt, history, turn) {
|
|
1016
|
+
const ragText = extractRagText(prompt);
|
|
1017
|
+
const abortSignal = this.abortController.signal;
|
|
1018
|
+
const documents = await fetchContextDocuments(this.agent, ragText, abortSignal);
|
|
1019
|
+
const toolDefinitions = await fetchToolDefinitions(this.agent, ragText, abortSignal);
|
|
1020
|
+
const request = createCompletionRequest(providerMessages([...history, prompt]), {
|
|
1021
|
+
instructions: this.agent.instructions,
|
|
1022
|
+
documents,
|
|
1023
|
+
tools: [...toolDefinitions, ...getAgentToolState(this.agent).providerTools],
|
|
1024
|
+
temperature: this.agent.temperature,
|
|
1025
|
+
maxTokens: this.agent.maxTokens,
|
|
1026
|
+
providerOptions: this.agent.providerOptions,
|
|
1027
|
+
toolChoice: this.agent.toolChoice,
|
|
1028
|
+
controls: this.controls,
|
|
1029
|
+
outputSchema: getAgentProviderOutputSchema(this.agent)
|
|
1030
|
+
});
|
|
1031
|
+
return this.runCompletionRequestMiddlewares(request, turn);
|
|
1032
|
+
}
|
|
1033
|
+
async *streamCompletion(args) {
|
|
1034
|
+
const model = this.agent.model;
|
|
1035
|
+
if (!isStreamingCompletionModel(model)) {
|
|
1036
|
+
throw new TypeError("Streaming completion requires a streaming-capable model.");
|
|
1037
|
+
}
|
|
1038
|
+
this.validatedStructuredOutput = void 0;
|
|
1039
|
+
let currentRequest = args.request;
|
|
1040
|
+
for (let attempt = 1; ; attempt += 1) {
|
|
1041
|
+
const accumulator = new CompletionStreamAccumulator();
|
|
1042
|
+
let hasProviderProgress = false;
|
|
1043
|
+
let recordedErrorUsage = false;
|
|
1044
|
+
let attemptErrorUsage;
|
|
1045
|
+
try {
|
|
1046
|
+
this.throwIfCancelled();
|
|
1047
|
+
for await (const event of model.streamCompletion(currentRequest, this.modelCallOptions())) {
|
|
1048
|
+
if (event.type === "error") {
|
|
1049
|
+
const eventUsage = event.usage ?? completionProviderOutputErrorUsage(event.error);
|
|
1050
|
+
if (eventUsage !== void 0) {
|
|
1051
|
+
args.state.providerErrorUsage = Usage.add(args.state.providerErrorUsage, eventUsage);
|
|
1052
|
+
recordedErrorUsage = true;
|
|
1053
|
+
attemptErrorUsage = eventUsage;
|
|
1054
|
+
}
|
|
1055
|
+
throw event.error;
|
|
1056
|
+
}
|
|
1057
|
+
const mapped = accumulator.accept(event);
|
|
1058
|
+
if (event.type === "final") break;
|
|
1059
|
+
if (event.type === "tool_call_delta" || mapped !== void 0) {
|
|
1060
|
+
hasProviderProgress = true;
|
|
1061
|
+
}
|
|
1062
|
+
if (args.state.firstDeltaMs === void 0 && isGenerationDeltaEvent(event.type)) {
|
|
1063
|
+
args.state.firstDeltaMs = Date.now() - args.generationStartedAt;
|
|
1064
|
+
}
|
|
1065
|
+
if (event.type === "tool_call_delta") {
|
|
1066
|
+
yield addTurnToToolCallDelta(args.turn, event);
|
|
1067
|
+
}
|
|
1068
|
+
if (mapped !== void 0) {
|
|
1069
|
+
await args.generationObservers.update?.({ turn: args.turn, delta: mapped });
|
|
1070
|
+
if (mapped.type === "tool_call") {
|
|
1071
|
+
args.state.emittedToolCallIds.add(mapped.toolCall.toolCallId);
|
|
1072
|
+
}
|
|
1073
|
+
const shouldBuffer = args.bufferResponseEvents || args.bufferOutputDeltas && (mapped.type === "text_delta" || mapped.type === "reasoning_delta");
|
|
1074
|
+
if (!shouldBuffer) {
|
|
1075
|
+
yield addTurn(args.turn, mapped);
|
|
1076
|
+
}
|
|
1077
|
+
}
|
|
1078
|
+
}
|
|
1079
|
+
const response = accumulator.response();
|
|
1080
|
+
assertCompletionResponseIntegrity({ response });
|
|
1081
|
+
const cumulativeStructuredUsage = Usage.add(args.state.providerErrorUsage, response.usage);
|
|
1082
|
+
try {
|
|
1083
|
+
this.validateStructuredResponse(response, attempt, cumulativeStructuredUsage);
|
|
1084
|
+
} catch (error) {
|
|
1085
|
+
const retryOptions = this.retryOptionsForFailure(error, attempt, args.turn, true);
|
|
1086
|
+
if (retryOptions === void 0) {
|
|
1087
|
+
if (error instanceof AgentStructuredOutputError) {
|
|
1088
|
+
args.state.providerErrorUsage = Usage.empty();
|
|
1089
|
+
}
|
|
1090
|
+
throw error;
|
|
1091
|
+
}
|
|
1092
|
+
args.state.providerErrorUsage = Usage.add(args.state.providerErrorUsage, response.usage);
|
|
1093
|
+
args.state.firstDeltaMs = void 0;
|
|
1094
|
+
args.state.emittedToolCallIds.clear();
|
|
1095
|
+
const retryRequest = structuredOutputRetryRequest(args.request, response, error);
|
|
1096
|
+
currentRequest = retryRequest.request;
|
|
1097
|
+
await this.scheduleCompletionRetry(
|
|
1098
|
+
error,
|
|
1099
|
+
attempt,
|
|
1100
|
+
args.turn,
|
|
1101
|
+
true,
|
|
1102
|
+
retryOptions,
|
|
1103
|
+
args.runObservers,
|
|
1104
|
+
structuredOutputRetryEventAttributes(error, retryRequest)
|
|
1105
|
+
);
|
|
1106
|
+
continue;
|
|
1107
|
+
}
|
|
1108
|
+
args.state.response = Usage.isEmpty(args.state.providerErrorUsage) ? response : { ...response, usage: cumulativeStructuredUsage };
|
|
1109
|
+
args.state.providerErrorUsage = Usage.empty();
|
|
1110
|
+
return;
|
|
1111
|
+
} catch (error) {
|
|
1112
|
+
if (!recordedErrorUsage) {
|
|
1113
|
+
const attemptUsage = completionProviderOutputErrorUsage(error);
|
|
1114
|
+
if (attemptUsage !== void 0) {
|
|
1115
|
+
args.state.providerErrorUsage = Usage.add(args.state.providerErrorUsage, attemptUsage);
|
|
1116
|
+
attemptErrorUsage = attemptUsage;
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
const retryOptions = hasProviderProgress ? void 0 : this.retryOptionsForFailure(error, attempt, args.turn, true);
|
|
1120
|
+
if (retryOptions === void 0) {
|
|
1121
|
+
throw error;
|
|
1122
|
+
}
|
|
1123
|
+
await this.scheduleCompletionRetry(
|
|
1124
|
+
error,
|
|
1125
|
+
attempt,
|
|
1126
|
+
args.turn,
|
|
1127
|
+
true,
|
|
1128
|
+
retryOptions,
|
|
1129
|
+
args.runObservers,
|
|
1130
|
+
providerOutputRetryEventAttributes(
|
|
1131
|
+
error,
|
|
1132
|
+
args.state.providerErrorUsage,
|
|
1133
|
+
attemptErrorUsage
|
|
1134
|
+
)
|
|
1135
|
+
);
|
|
1136
|
+
}
|
|
1137
|
+
}
|
|
1138
|
+
}
|
|
1139
|
+
async *completeStreamingRun(args) {
|
|
1140
|
+
const guardedOutput = await this.runOutputGuardrailsForResponse(
|
|
1141
|
+
args.runId,
|
|
1142
|
+
args.usage,
|
|
1143
|
+
args.response,
|
|
1144
|
+
args.newMessages,
|
|
1145
|
+
args.runObservers
|
|
1146
|
+
);
|
|
1147
|
+
for (const decision of guardedOutput.decisions) {
|
|
1148
|
+
yield { type: "guardrail_decision", decision };
|
|
1149
|
+
}
|
|
1150
|
+
const response = guardedOutput.response;
|
|
1151
|
+
const assistantMessage = this.generatedAssistantMessage(response, args.request);
|
|
1152
|
+
args.newMessages[args.newMessages.length - 1] = assistantMessage;
|
|
1153
|
+
await this.memoryRecorder.commitMessages(
|
|
1154
|
+
args.runId,
|
|
1155
|
+
args.turn,
|
|
1156
|
+
[assistantMessage],
|
|
1157
|
+
args.pendingTurnMessages
|
|
1158
|
+
);
|
|
1159
|
+
if (!args.emittedTurnEnd && (args.bufferResponseEvents || args.bufferOutputDeltas)) {
|
|
1160
|
+
for (const event of responseStreamEvents(args.turn, response, args.bufferResponseEvents)) {
|
|
1161
|
+
yield event;
|
|
1162
|
+
}
|
|
1163
|
+
}
|
|
1164
|
+
if (!args.emittedTurnEnd) {
|
|
1165
|
+
yield {
|
|
1166
|
+
type: "turn_end",
|
|
1167
|
+
turn: args.turn,
|
|
1168
|
+
response,
|
|
1169
|
+
firstDeltaMs: args.firstDeltaMs
|
|
1170
|
+
};
|
|
1171
|
+
}
|
|
1172
|
+
const result = this.createTerminalResult(
|
|
1173
|
+
args.runId,
|
|
1174
|
+
guardedOutput.text,
|
|
1175
|
+
guardedOutput.blocked,
|
|
1176
|
+
args.usage,
|
|
1177
|
+
args.newMessages,
|
|
1178
|
+
args.runObservers
|
|
1179
|
+
);
|
|
1180
|
+
if (result.type === "response") {
|
|
1181
|
+
await this.runRunEndHook(result, args.newMessages);
|
|
1182
|
+
}
|
|
1183
|
+
await this.memoryRecorder.commitCompletedRun(
|
|
1184
|
+
args.runId,
|
|
1185
|
+
args.turn,
|
|
1186
|
+
args.newMessages,
|
|
1187
|
+
args.pendingTurnMessages
|
|
1188
|
+
);
|
|
1189
|
+
await this.runLifecycleFinish(result);
|
|
1190
|
+
await args.runObservers.end(observerRunEnd(result));
|
|
1191
|
+
this.runState = "completed";
|
|
1192
|
+
this.disposeAbortLink();
|
|
1193
|
+
yield result;
|
|
1194
|
+
}
|
|
1195
|
+
async finishSuspension(args) {
|
|
1196
|
+
const partialToolMessage = args.suspension.completedResults.length === 0 ? void 0 : { role: "tool", content: [...args.suspension.completedResults] };
|
|
1197
|
+
if (partialToolMessage !== void 0) {
|
|
1198
|
+
args.newMessages.push(partialToolMessage);
|
|
1199
|
+
args.uncommittedMessages.push(partialToolMessage);
|
|
1200
|
+
}
|
|
1201
|
+
await this.memoryRecorder.commitMessages(
|
|
1202
|
+
args.runId,
|
|
1203
|
+
args.turn,
|
|
1204
|
+
args.uncommittedMessages,
|
|
1205
|
+
args.pendingTurnMessages
|
|
1206
|
+
);
|
|
1207
|
+
await this.memoryRecorder.commitCompletedRun(
|
|
1208
|
+
args.runId,
|
|
1209
|
+
args.turn,
|
|
1210
|
+
args.newMessages,
|
|
1211
|
+
args.pendingTurnMessages
|
|
1212
|
+
);
|
|
1213
|
+
this.runState = "closing";
|
|
1214
|
+
const continuationState = {
|
|
1215
|
+
kind: "anvia.agent-continuation",
|
|
1216
|
+
history: [...this.chatHistory],
|
|
1217
|
+
messages: [...args.newMessages],
|
|
1218
|
+
pending: args.suspension.pending,
|
|
1219
|
+
remainingToolCalls: [...args.suspension.remainingToolCalls],
|
|
1220
|
+
steering: this.steeringMessages.map((entry) => ({
|
|
1221
|
+
id: entry.id,
|
|
1222
|
+
messages: [...entry.messages]
|
|
1223
|
+
}))
|
|
1224
|
+
};
|
|
1225
|
+
if (this.memoryScope !== void 0) continuationState.memoryScope = this.memoryScope;
|
|
1226
|
+
const continuation = parseAgentContinuation({
|
|
1227
|
+
version: 1,
|
|
1228
|
+
agentId: this.agent.id,
|
|
1229
|
+
sourceRunId: args.runId,
|
|
1230
|
+
interaction: args.suspension.interaction,
|
|
1231
|
+
state: serializeContinuationState(continuationState)
|
|
1232
|
+
});
|
|
1233
|
+
const result = {
|
|
1234
|
+
type: "interaction",
|
|
1235
|
+
runId: args.runId,
|
|
1236
|
+
text: latestAssistantText([...this.chatHistory, ...args.newMessages]),
|
|
1237
|
+
usage: args.usage,
|
|
1238
|
+
messages: [...args.newMessages],
|
|
1239
|
+
trace: args.runObservers.trace,
|
|
1240
|
+
guardrails: [...this.guardrailDecisions],
|
|
1241
|
+
interaction: continuation.interaction,
|
|
1242
|
+
continuation,
|
|
1243
|
+
...generationArtifacts([...this.chatHistory, ...args.newMessages]),
|
|
1244
|
+
...this.memoryCompactionResult()
|
|
1245
|
+
};
|
|
1246
|
+
if (this.resumedFrom !== void 0) result.resumedFrom = this.resumedFrom;
|
|
1247
|
+
await this.runLifecycleFinish(result);
|
|
1248
|
+
await args.runObservers.end(observerRunEnd(result));
|
|
1249
|
+
this.runState = "completed";
|
|
1250
|
+
this.currentMessages = [...args.newMessages];
|
|
1251
|
+
this.disposeAbortLink();
|
|
1252
|
+
return result;
|
|
1253
|
+
}
|
|
1254
|
+
generatedAssistantMessage(response, _request) {
|
|
1255
|
+
const generation = {
|
|
1256
|
+
provider: this.agent.model.provider,
|
|
1257
|
+
modelId: this.agent.model.modelId,
|
|
1258
|
+
usage: { ...response.usage }
|
|
1259
|
+
};
|
|
1260
|
+
if (response.finishReason !== void 0) generation.finishReason = response.finishReason;
|
|
1261
|
+
if (response.providerFinishReason !== void 0) {
|
|
1262
|
+
generation.providerFinishReason = response.providerFinishReason;
|
|
1263
|
+
}
|
|
1264
|
+
if (response.contextUsage !== void 0) generation.contextUsage = response.contextUsage;
|
|
1265
|
+
if (response.sources !== void 0) generation.sources = response.sources;
|
|
1266
|
+
if (response.providerToolCalls !== void 0) {
|
|
1267
|
+
generation.providerToolCalls = response.providerToolCalls;
|
|
1268
|
+
}
|
|
1269
|
+
const metadata = { anvia: { generation } };
|
|
1270
|
+
let message = {
|
|
1271
|
+
role: "assistant",
|
|
1272
|
+
content: response.choice,
|
|
1273
|
+
metadata
|
|
1274
|
+
};
|
|
1275
|
+
if (response.messageId !== void 0) message = { ...message, id: response.messageId };
|
|
1276
|
+
return message;
|
|
1277
|
+
}
|
|
1278
|
+
providerTraceRequest(request, options = {}) {
|
|
1279
|
+
try {
|
|
1280
|
+
return this.agent.model.traceRequest?.(request, options);
|
|
1281
|
+
} catch (error) {
|
|
1282
|
+
return {
|
|
1283
|
+
error: error instanceof Error ? error.message : String(error)
|
|
1284
|
+
};
|
|
1285
|
+
}
|
|
1286
|
+
}
|
|
1287
|
+
generationStartArgs(turn, request, providerRequest) {
|
|
1288
|
+
let args = {
|
|
1289
|
+
turn,
|
|
1290
|
+
request,
|
|
1291
|
+
promptRef: this.generationPromptRef,
|
|
1292
|
+
modelInfo: {
|
|
1293
|
+
provider: this.agent.model.provider,
|
|
1294
|
+
modelId: this.agent.model.modelId,
|
|
1295
|
+
capabilities: this.agent.model.capabilities
|
|
1296
|
+
}
|
|
1297
|
+
};
|
|
1298
|
+
if (providerRequest !== void 0) args = { ...args, providerRequest };
|
|
1299
|
+
return args;
|
|
1300
|
+
}
|
|
1301
|
+
retryOptionsForFailure(error, attempt, turn, streaming) {
|
|
1302
|
+
if (this.abortController.signal.aborted) return void 0;
|
|
1303
|
+
return retryOptionsForFailure(this.completionRetryOptions, {
|
|
1304
|
+
error,
|
|
1305
|
+
attempt,
|
|
1306
|
+
turn,
|
|
1307
|
+
streaming
|
|
1308
|
+
});
|
|
1309
|
+
}
|
|
1310
|
+
async scheduleCompletionRetry(error, attempt, turn, streaming, options, runObservers, additionalAttributes) {
|
|
1311
|
+
const delayMs = retryDelayMs(options, attempt);
|
|
1312
|
+
const attributes = {
|
|
1313
|
+
turn,
|
|
1314
|
+
attempt,
|
|
1315
|
+
nextAttempt: attempt + 1,
|
|
1316
|
+
maxAttempts: options.maxAttempts,
|
|
1317
|
+
delayMs,
|
|
1318
|
+
streaming,
|
|
1319
|
+
...retryErrorAttributes(error)
|
|
1320
|
+
};
|
|
1321
|
+
if (additionalAttributes !== void 0) {
|
|
1322
|
+
Object.assign(attributes, additionalAttributes);
|
|
1323
|
+
}
|
|
1324
|
+
await runObservers.event({
|
|
1325
|
+
name: "completion.retry",
|
|
1326
|
+
level: "WARNING",
|
|
1327
|
+
attributes
|
|
1328
|
+
});
|
|
1329
|
+
await waitForRetry(delayMs, this.abortController.signal);
|
|
1330
|
+
}
|
|
1331
|
+
async executeToolCalls(runId, toolCalls, newMessages, onResult, onStreamEvent, observation) {
|
|
1332
|
+
const executor = this.createToolExecutor(runId, newMessages);
|
|
1333
|
+
return executor.execute(toolCalls, onResult, onStreamEvent, observation);
|
|
1334
|
+
}
|
|
1335
|
+
createToolExecutor(runId, newMessages) {
|
|
1336
|
+
return new ToolCallExecutor(
|
|
1337
|
+
this.agent,
|
|
1338
|
+
this.activeHook,
|
|
1339
|
+
async (request) => {
|
|
1340
|
+
throw new AgentInteractionSignal(approvalInteraction(request), request.rejectMessage);
|
|
1341
|
+
},
|
|
1342
|
+
this.activeLifecycle,
|
|
1343
|
+
{
|
|
1344
|
+
runId,
|
|
1345
|
+
sessionId: this.memoryScope?.sessionId,
|
|
1346
|
+
metadata: this.memoryScope?.metadata
|
|
1347
|
+
},
|
|
1348
|
+
this.concurrency,
|
|
1349
|
+
this.requestMiddlewares,
|
|
1350
|
+
this.abortController.signal,
|
|
1351
|
+
(reason) => this.cancelled(newMessages, reason)
|
|
1352
|
+
);
|
|
1353
|
+
}
|
|
1354
|
+
async resolveContinuationTools(runId, newMessages, onResult, onStreamEvent, observation) {
|
|
1355
|
+
const state = this.continuationState;
|
|
1356
|
+
const response = this.interactionResponse;
|
|
1357
|
+
if (state === void 0 || response === void 0) {
|
|
1358
|
+
return [];
|
|
1359
|
+
}
|
|
1360
|
+
const pending = state.pending;
|
|
1361
|
+
const interaction = this.resumedFrom;
|
|
1362
|
+
if (interaction === void 0) {
|
|
1363
|
+
throw new TypeError("Agent continuation is missing its source run linkage.");
|
|
1364
|
+
}
|
|
1365
|
+
const executor = this.createToolExecutor(runId, newMessages);
|
|
1366
|
+
const results = [];
|
|
1367
|
+
if (response.type === "tool-approval") {
|
|
1368
|
+
const attributes = {
|
|
1369
|
+
approvalId: interaction.interactionId,
|
|
1370
|
+
toolName: pending.toolCall.toolName,
|
|
1371
|
+
toolCallId: pending.toolCall.toolCallId,
|
|
1372
|
+
internalCallId: pending.internalCallId,
|
|
1373
|
+
approved: response.approved
|
|
1374
|
+
};
|
|
1375
|
+
if (response.reason !== void 0) attributes.decisionReason = response.reason;
|
|
1376
|
+
await observation.runObservers.event({
|
|
1377
|
+
name: "tool.approval_resolved",
|
|
1378
|
+
attributes
|
|
1379
|
+
});
|
|
1380
|
+
if (response.approved) {
|
|
1381
|
+
results.push(await executor.executeResumed(pending, onResult, onStreamEvent, observation));
|
|
1382
|
+
} else {
|
|
1383
|
+
const output = {
|
|
1384
|
+
type: "execution-denied",
|
|
1385
|
+
reason: response.reason ?? pending.rejectMessage ?? "Tool approval was rejected."
|
|
1386
|
+
};
|
|
1387
|
+
results.push(await executor.resolveResumed(pending, output, onResult, observation));
|
|
1388
|
+
}
|
|
1389
|
+
} else {
|
|
1390
|
+
const currentTool = this.agent.getTool(pending.toolCall.toolName);
|
|
1391
|
+
if (currentTool === void 0 || !isQuestionTool(currentTool)) {
|
|
1392
|
+
throw new TypeError(
|
|
1393
|
+
`Cannot resume question interaction because tool "${pending.toolCall.toolName}" is no longer registered as a question tool.`
|
|
1394
|
+
);
|
|
1395
|
+
}
|
|
1396
|
+
const output = questionResult(response.answers);
|
|
1397
|
+
results.push(await executor.resolveResumed(pending, output, onResult, observation));
|
|
1398
|
+
}
|
|
1399
|
+
try {
|
|
1400
|
+
results.push(
|
|
1401
|
+
...await executor.execute(state.remainingToolCalls, onResult, onStreamEvent, observation)
|
|
1402
|
+
);
|
|
1403
|
+
} catch (error) {
|
|
1404
|
+
if (error instanceof ToolExecutionSuspension) {
|
|
1405
|
+
error.completedResults = [...results, ...error.completedResults];
|
|
1406
|
+
}
|
|
1407
|
+
throw error;
|
|
1408
|
+
}
|
|
1409
|
+
return results;
|
|
1410
|
+
}
|
|
1411
|
+
executeStreamingToolCalls(runId, toolCalls, newMessages, observation) {
|
|
1412
|
+
const events = createAsyncQueue();
|
|
1413
|
+
const results = this.executeToolCalls(
|
|
1414
|
+
runId,
|
|
1415
|
+
toolCalls,
|
|
1416
|
+
newMessages,
|
|
1417
|
+
(result) => events.enqueue(result),
|
|
1418
|
+
(event) => events.enqueue(event),
|
|
1419
|
+
observation
|
|
1420
|
+
);
|
|
1421
|
+
results.then(
|
|
1422
|
+
() => events.close(),
|
|
1423
|
+
(error) => events.throw(error)
|
|
1424
|
+
);
|
|
1425
|
+
return { events, results };
|
|
1426
|
+
}
|
|
1427
|
+
executeContinuationToolStream(runId, newMessages, observation) {
|
|
1428
|
+
const events = createAsyncQueue();
|
|
1429
|
+
const results = this.resolveContinuationTools(
|
|
1430
|
+
runId,
|
|
1431
|
+
newMessages,
|
|
1432
|
+
(result) => events.enqueue(result),
|
|
1433
|
+
(event) => events.enqueue(event),
|
|
1434
|
+
observation
|
|
1435
|
+
);
|
|
1436
|
+
results.then(
|
|
1437
|
+
() => events.close(),
|
|
1438
|
+
(error) => events.throw(error)
|
|
1439
|
+
);
|
|
1440
|
+
return { events, results };
|
|
1441
|
+
}
|
|
1442
|
+
async closeActiveGeneration(error) {
|
|
1443
|
+
const active = this.activeGeneration;
|
|
1444
|
+
if (active === void 0) {
|
|
1445
|
+
return;
|
|
1446
|
+
}
|
|
1447
|
+
this.activeGeneration = void 0;
|
|
1448
|
+
await active.observers.error({ turn: active.turn, error });
|
|
1449
|
+
}
|
|
1450
|
+
updateRunProgress(messages) {
|
|
1451
|
+
this.currentMessages = [...messages];
|
|
1452
|
+
}
|
|
1453
|
+
async runLifecycleFinish(result) {
|
|
1454
|
+
const common = {
|
|
1455
|
+
runId: result.runId,
|
|
1456
|
+
text: result.text,
|
|
1457
|
+
usage: lifecycleSnapshot(result.usage),
|
|
1458
|
+
messages: lifecycleSnapshot(result.messages)
|
|
1459
|
+
};
|
|
1460
|
+
if (result.memoryCompaction !== void 0) {
|
|
1461
|
+
Object.assign(common, { memoryCompaction: lifecycleSnapshot(result.memoryCompaction) });
|
|
1462
|
+
}
|
|
1463
|
+
const event = result.type === "response" ? {
|
|
1464
|
+
...common,
|
|
1465
|
+
status: "completed",
|
|
1466
|
+
output: lifecycleSnapshot(result.output)
|
|
1467
|
+
} : result.type === "blocked" ? {
|
|
1468
|
+
...common,
|
|
1469
|
+
status: "blocked",
|
|
1470
|
+
stage: result.stage
|
|
1471
|
+
} : {
|
|
1472
|
+
...common,
|
|
1473
|
+
status: "suspended",
|
|
1474
|
+
interaction: lifecycleSnapshot(result.interaction)
|
|
1475
|
+
};
|
|
1476
|
+
await this.activeLifecycle?.onFinish?.(event);
|
|
1477
|
+
}
|
|
1478
|
+
async runLifecycleError(error, runId, usage, messages) {
|
|
1479
|
+
try {
|
|
1480
|
+
await this.activeLifecycle?.onError?.({
|
|
1481
|
+
runId,
|
|
1482
|
+
error: lifecycleSnapshot(error),
|
|
1483
|
+
usage: lifecycleSnapshot(usage),
|
|
1484
|
+
messages: lifecycleSnapshot([...this.chatHistory, ...messages])
|
|
1485
|
+
});
|
|
1486
|
+
return void 0;
|
|
1487
|
+
} catch (lifecycleError) {
|
|
1488
|
+
return lifecycleError;
|
|
1489
|
+
}
|
|
1490
|
+
}
|
|
1491
|
+
async reportRunFailure(error, runId, usage, messages, runObservers) {
|
|
1492
|
+
const reportedError = await this.resolveReportedRunError(error, runId, usage, messages);
|
|
1493
|
+
await settleFailureCleanup([
|
|
1494
|
+
() => this.onInternalFailure?.({
|
|
1495
|
+
error: reportedError,
|
|
1496
|
+
messages: lifecycleSnapshot(messages)
|
|
1497
|
+
}),
|
|
1498
|
+
() => runObservers?.error({
|
|
1499
|
+
status: reportedError instanceof AgentRunCancelledError ? "cancelled" : "failed",
|
|
1500
|
+
error: reportedError,
|
|
1501
|
+
usage,
|
|
1502
|
+
messages: [...messages]
|
|
1503
|
+
}),
|
|
1504
|
+
() => this.memoryRecorder.recordError(runId, reportedError, messages)
|
|
1505
|
+
]);
|
|
1506
|
+
return reportedError;
|
|
1507
|
+
}
|
|
1508
|
+
async resolveReportedRunError(error, runId, usage, messages) {
|
|
1509
|
+
try {
|
|
1510
|
+
await this.runRunErrorHook(error, usage, messages);
|
|
1511
|
+
} catch {
|
|
1512
|
+
}
|
|
1513
|
+
const reportedError = this.cancellationError ?? error;
|
|
1514
|
+
await this.runLifecycleError(reportedError, runId, usage, messages);
|
|
1515
|
+
return reportedError;
|
|
1516
|
+
}
|
|
1517
|
+
async runOutputGuardrailsForResponse(runId, usage, response, messages, runObservers) {
|
|
1518
|
+
const originalOutput = textFromAssistantContent(response.choice);
|
|
1519
|
+
const result = await runOutputGuardrails(this.guardrailPolicies, {
|
|
1520
|
+
outputText: originalOutput,
|
|
1521
|
+
messages: [...this.chatHistory, ...messages],
|
|
1522
|
+
usage,
|
|
1523
|
+
run: this.guardrailRunContext(runId)
|
|
1524
|
+
});
|
|
1525
|
+
for (const decision of result.decisions) {
|
|
1526
|
+
await this.recordGuardrailDecision(decision, runObservers);
|
|
1527
|
+
}
|
|
1528
|
+
const text = result.blocked ? result.message ?? "The response was blocked by a guardrail." : result.outputText;
|
|
1529
|
+
if (text === originalOutput) {
|
|
1530
|
+
return { text, blocked: result.blocked, response, decisions: result.decisions };
|
|
1531
|
+
}
|
|
1532
|
+
return {
|
|
1533
|
+
text,
|
|
1534
|
+
blocked: result.blocked,
|
|
1535
|
+
response: {
|
|
1536
|
+
...response,
|
|
1537
|
+
choice: [{ type: "text", text }]
|
|
1538
|
+
},
|
|
1539
|
+
decisions: result.decisions
|
|
1540
|
+
};
|
|
1541
|
+
}
|
|
1542
|
+
createTerminalResult(runId, text, blocked, usage, messages, runObservers) {
|
|
1543
|
+
const common = {
|
|
1544
|
+
runId,
|
|
1545
|
+
text,
|
|
1546
|
+
usage,
|
|
1547
|
+
messages: [...messages],
|
|
1548
|
+
trace: runObservers.trace,
|
|
1549
|
+
guardrails: [...this.guardrailDecisions],
|
|
1550
|
+
...generationArtifacts(messages),
|
|
1551
|
+
...this.memoryCompactionResult()
|
|
1552
|
+
};
|
|
1553
|
+
if (this.resumedFrom !== void 0) Object.assign(common, { resumedFrom: this.resumedFrom });
|
|
1554
|
+
if (blocked) {
|
|
1555
|
+
return {
|
|
1556
|
+
...common,
|
|
1557
|
+
type: "blocked",
|
|
1558
|
+
stage: "output",
|
|
1559
|
+
...blockedOutcomeDetails(
|
|
1560
|
+
this.guardrailDecisions,
|
|
1561
|
+
"The response was blocked by a guardrail."
|
|
1562
|
+
)
|
|
1563
|
+
};
|
|
1564
|
+
}
|
|
1565
|
+
return {
|
|
1566
|
+
...common,
|
|
1567
|
+
type: "response",
|
|
1568
|
+
output: this.parseOutput(text)
|
|
1569
|
+
};
|
|
1570
|
+
}
|
|
1571
|
+
validateStructuredResponse(response, attempt, usage) {
|
|
1572
|
+
if (this.agent.outputSchema === void 0) return;
|
|
1573
|
+
if (response.choice.some((item) => item.type === "tool-call")) return;
|
|
1574
|
+
const text = textFromAssistantContent(response.choice);
|
|
1575
|
+
const normalized = normalizeStructuredOutput(text);
|
|
1576
|
+
if (response.finishReason === "content-filter") {
|
|
1577
|
+
throw new AgentStructuredOutputError({
|
|
1578
|
+
phase: "content-filter",
|
|
1579
|
+
attempt,
|
|
1580
|
+
maxAttempts: this.completionRetryOptions?.maxAttempts ?? 1,
|
|
1581
|
+
outputLength: text.length,
|
|
1582
|
+
normalizedLength: normalized.text.length,
|
|
1583
|
+
outputFormat: normalized.format,
|
|
1584
|
+
attemptUsage: response.usage,
|
|
1585
|
+
usage,
|
|
1586
|
+
finishReason: response.finishReason,
|
|
1587
|
+
providerFinishReason: response.providerFinishReason
|
|
1588
|
+
});
|
|
1589
|
+
}
|
|
1590
|
+
if (response.finishReason === "length") {
|
|
1591
|
+
throw new AgentStructuredOutputError({
|
|
1592
|
+
phase: "truncated",
|
|
1593
|
+
attempt,
|
|
1594
|
+
maxAttempts: this.completionRetryOptions?.maxAttempts ?? 1,
|
|
1595
|
+
outputLength: text.length,
|
|
1596
|
+
normalizedLength: normalized.text.length,
|
|
1597
|
+
outputFormat: normalized.format,
|
|
1598
|
+
attemptUsage: response.usage,
|
|
1599
|
+
usage,
|
|
1600
|
+
finishReason: response.finishReason,
|
|
1601
|
+
providerFinishReason: response.providerFinishReason
|
|
1602
|
+
});
|
|
1603
|
+
}
|
|
1604
|
+
const output = this.parseOutput(text, attempt, usage, {
|
|
1605
|
+
useValidatedOutput: false,
|
|
1606
|
+
attemptUsage: response.usage,
|
|
1607
|
+
finishReason: response.finishReason,
|
|
1608
|
+
providerFinishReason: response.providerFinishReason
|
|
1609
|
+
});
|
|
1610
|
+
this.validatedStructuredOutput = { text, output };
|
|
1611
|
+
}
|
|
1612
|
+
takeFailedCompletionUsage() {
|
|
1613
|
+
const usage = this.failedCompletionUsage;
|
|
1614
|
+
this.failedCompletionUsage = Usage.empty();
|
|
1615
|
+
return usage;
|
|
1616
|
+
}
|
|
1617
|
+
parseOutput(text, attempt = 1, usage = Usage.empty(), options = {}) {
|
|
1618
|
+
const schema = this.agent.outputSchema;
|
|
1619
|
+
if (schema === void 0) return text;
|
|
1620
|
+
if (options.useValidatedOutput !== false && this.validatedStructuredOutput?.text === text) {
|
|
1621
|
+
return this.validatedStructuredOutput.output;
|
|
1622
|
+
}
|
|
1623
|
+
const normalized = normalizeStructuredOutput(text);
|
|
1624
|
+
const maxAttempts = this.completionRetryOptions?.maxAttempts ?? 1;
|
|
1625
|
+
let json;
|
|
1626
|
+
try {
|
|
1627
|
+
json = JSON.parse(normalized.text);
|
|
1628
|
+
if (!isJsonValue(json)) {
|
|
1629
|
+
throw new TypeError("Agent structured output is not a JSON value.");
|
|
1630
|
+
}
|
|
1631
|
+
} catch (error) {
|
|
1632
|
+
throw new AgentStructuredOutputError({
|
|
1633
|
+
phase: "parse",
|
|
1634
|
+
attempt,
|
|
1635
|
+
maxAttempts,
|
|
1636
|
+
outputLength: text.length,
|
|
1637
|
+
normalizedLength: normalized.text.length,
|
|
1638
|
+
outputFormat: normalized.format,
|
|
1639
|
+
attemptUsage: options.attemptUsage ?? usage,
|
|
1640
|
+
usage,
|
|
1641
|
+
finishReason: options.finishReason,
|
|
1642
|
+
providerFinishReason: options.providerFinishReason,
|
|
1643
|
+
cause: error
|
|
1644
|
+
});
|
|
1645
|
+
}
|
|
1646
|
+
try {
|
|
1647
|
+
return schema.parse(json);
|
|
1648
|
+
} catch (error) {
|
|
1649
|
+
throw new AgentStructuredOutputError({
|
|
1650
|
+
phase: "schema",
|
|
1651
|
+
attempt,
|
|
1652
|
+
maxAttempts,
|
|
1653
|
+
outputLength: text.length,
|
|
1654
|
+
normalizedLength: normalized.text.length,
|
|
1655
|
+
outputFormat: normalized.format,
|
|
1656
|
+
attemptUsage: options.attemptUsage ?? usage,
|
|
1657
|
+
usage,
|
|
1658
|
+
finishReason: options.finishReason,
|
|
1659
|
+
providerFinishReason: options.providerFinishReason,
|
|
1660
|
+
cause: error
|
|
1661
|
+
});
|
|
1662
|
+
}
|
|
1663
|
+
}
|
|
1664
|
+
memoryCompactionResult() {
|
|
1665
|
+
return this.memoryCompaction === void 0 ? {} : { memoryCompaction: lifecycleSnapshot(this.memoryCompaction) };
|
|
1666
|
+
}
|
|
1667
|
+
async recordGuardrailDecision(decision, runObservers) {
|
|
1668
|
+
this.guardrailDecisions.push(decision);
|
|
1669
|
+
await runObservers.event({
|
|
1670
|
+
name: "guardrail.decision",
|
|
1671
|
+
level: decision.action === "block" ? "WARNING" : "DEFAULT",
|
|
1672
|
+
attributes: guardrailDecisionAttributes(decision)
|
|
1673
|
+
});
|
|
1674
|
+
}
|
|
1675
|
+
async recordMemoryCompaction(preparation, runObservers) {
|
|
1676
|
+
const compaction = preparation.compaction;
|
|
1677
|
+
if (compaction === void 0) {
|
|
1678
|
+
return;
|
|
1679
|
+
}
|
|
1680
|
+
await runObservers.event({
|
|
1681
|
+
name: "memory.compaction",
|
|
1682
|
+
attributes: {
|
|
1683
|
+
originalMessageCount: compaction.originalMessageCount,
|
|
1684
|
+
compactedMessageCount: compaction.compactedMessageCount,
|
|
1685
|
+
retainedMessageCount: compaction.retainedMessageCount,
|
|
1686
|
+
originalTokenCount: compaction.originalTokenCount,
|
|
1687
|
+
compactedTokenCount: compaction.compactedTokenCount,
|
|
1688
|
+
retainedTokenCount: compaction.retainedTokenCount,
|
|
1689
|
+
resultTokenCount: compaction.resultTokenCount,
|
|
1690
|
+
attempts: compaction.attempts,
|
|
1691
|
+
inputTokens: compaction.usage.inputTokens,
|
|
1692
|
+
outputTokens: compaction.usage.outputTokens,
|
|
1693
|
+
totalTokens: compaction.usage.totalTokens
|
|
1694
|
+
}
|
|
1695
|
+
});
|
|
1696
|
+
}
|
|
1697
|
+
async notifyInternalMemoryCompaction(compaction) {
|
|
1698
|
+
if (compaction === void 0) {
|
|
1699
|
+
return;
|
|
1700
|
+
}
|
|
1701
|
+
await this.onInternalMemoryCompaction?.(lifecycleSnapshot(compaction));
|
|
1702
|
+
}
|
|
1703
|
+
guardrailRunContext(runId) {
|
|
1704
|
+
const context = {
|
|
1705
|
+
agentId: this.agent.id,
|
|
1706
|
+
runId
|
|
1707
|
+
};
|
|
1708
|
+
if (this.memoryScope !== void 0) {
|
|
1709
|
+
context.sessionId = this.memoryScope.sessionId;
|
|
1710
|
+
if (this.memoryScope.metadata !== void 0) {
|
|
1711
|
+
context.metadata = this.memoryScope.metadata;
|
|
1712
|
+
}
|
|
1713
|
+
}
|
|
1714
|
+
return context;
|
|
1715
|
+
}
|
|
1716
|
+
async startRunObservers(runId) {
|
|
1717
|
+
const observability = this.agent.observability;
|
|
1718
|
+
return startAgentRunObservers(
|
|
1719
|
+
observability?.observers ?? {},
|
|
1720
|
+
{
|
|
1721
|
+
runId,
|
|
1722
|
+
agentName: this.agent.name,
|
|
1723
|
+
agentDescription: this.agent.description,
|
|
1724
|
+
instructions: this.agent.instructions,
|
|
1725
|
+
trace: this.traceOptions,
|
|
1726
|
+
promptRef: this.traceOptions?.promptRef,
|
|
1727
|
+
prompt: this.promptMessage,
|
|
1728
|
+
history: this.chatHistory,
|
|
1729
|
+
maxTurns: this.maxTurnCount
|
|
1730
|
+
},
|
|
1731
|
+
{
|
|
1732
|
+
primaryTrace: observability?.primaryTrace,
|
|
1733
|
+
errorPolicy: observability?.errorPolicy ?? "ignore"
|
|
1734
|
+
}
|
|
1735
|
+
);
|
|
1736
|
+
}
|
|
1737
|
+
async runCompletionCallHook(prompt, history, newMessages) {
|
|
1738
|
+
const action = await this.activeHook?.onCompletionCall?.({
|
|
1739
|
+
prompt,
|
|
1740
|
+
history,
|
|
1741
|
+
run: runControl
|
|
1742
|
+
});
|
|
1743
|
+
if (action?.type === "terminate") {
|
|
1744
|
+
throw this.cancelled(newMessages, action.reason);
|
|
1745
|
+
}
|
|
1746
|
+
}
|
|
1747
|
+
async runRunStartHook(newMessages) {
|
|
1748
|
+
const action = await this.activeHook?.onRunStart?.({
|
|
1749
|
+
prompt: this.promptMessage,
|
|
1750
|
+
history: this.chatHistory,
|
|
1751
|
+
maxTurns: this.maxTurnCount,
|
|
1752
|
+
run: runControl
|
|
1753
|
+
});
|
|
1754
|
+
if (action?.type === "terminate") {
|
|
1755
|
+
throw this.cancelled(newMessages, action.reason);
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
async runRunEndHook(result, newMessages) {
|
|
1759
|
+
const action = await this.activeHook?.onRunEnd?.({
|
|
1760
|
+
status: "completed",
|
|
1761
|
+
output: result.output,
|
|
1762
|
+
text: result.text,
|
|
1763
|
+
usage: result.usage,
|
|
1764
|
+
messages: result.messages,
|
|
1765
|
+
run: runControl
|
|
1766
|
+
});
|
|
1767
|
+
if (action?.type === "terminate") {
|
|
1768
|
+
throw this.cancelled(newMessages, action.reason);
|
|
1769
|
+
}
|
|
1770
|
+
}
|
|
1771
|
+
async runRunErrorHook(error, usage, newMessages) {
|
|
1772
|
+
const action = await this.activeHook?.onRunError?.({
|
|
1773
|
+
error,
|
|
1774
|
+
usage,
|
|
1775
|
+
messages: [...this.chatHistory, ...newMessages],
|
|
1776
|
+
run: runControl
|
|
1777
|
+
});
|
|
1778
|
+
if (action?.type === "terminate") {
|
|
1779
|
+
this.cancelled(newMessages, action.reason);
|
|
1780
|
+
}
|
|
1781
|
+
}
|
|
1782
|
+
async runTurnStartHook(turn, prompt, history, newMessages) {
|
|
1783
|
+
const action = await this.activeHook?.onTurnStart?.({
|
|
1784
|
+
turn,
|
|
1785
|
+
prompt,
|
|
1786
|
+
history,
|
|
1787
|
+
run: runControl
|
|
1788
|
+
});
|
|
1789
|
+
if (action?.type === "terminate") {
|
|
1790
|
+
throw this.cancelled(newMessages, action.reason);
|
|
1791
|
+
}
|
|
1792
|
+
}
|
|
1793
|
+
async runTurnEndHook(turn, response, newMessages) {
|
|
1794
|
+
const action = await this.activeHook?.onTurnEnd?.({
|
|
1795
|
+
turn,
|
|
1796
|
+
response,
|
|
1797
|
+
run: runControl
|
|
1798
|
+
});
|
|
1799
|
+
if (action?.type === "terminate") {
|
|
1800
|
+
throw this.cancelled(newMessages, action.reason);
|
|
1801
|
+
}
|
|
1802
|
+
}
|
|
1803
|
+
async runCompletionRequestMiddlewares(request, turn) {
|
|
1804
|
+
let current = request;
|
|
1805
|
+
this.generationPromptRef = this.traceOptions?.promptRef;
|
|
1806
|
+
for (const middleware of this.activeMiddlewares()) {
|
|
1807
|
+
const replacement = await middleware.onCompletionRequest?.({
|
|
1808
|
+
turn,
|
|
1809
|
+
request: current,
|
|
1810
|
+
originalRequest: request,
|
|
1811
|
+
promptRef: this.generationPromptRef
|
|
1812
|
+
});
|
|
1813
|
+
if (replacement?.promptRef !== void 0) {
|
|
1814
|
+
this.generationPromptRef = replacement.promptRef ?? void 0;
|
|
1815
|
+
}
|
|
1816
|
+
if (replacement?.request !== void 0) {
|
|
1817
|
+
current = replacement.request;
|
|
1818
|
+
if (current.providerOptions !== void 0) {
|
|
1819
|
+
assertJsonObject(current.providerOptions, "providerOptions");
|
|
1820
|
+
}
|
|
1821
|
+
}
|
|
1822
|
+
}
|
|
1823
|
+
return current;
|
|
1824
|
+
}
|
|
1825
|
+
async runCompletionResponseMiddlewares(request, response, turn) {
|
|
1826
|
+
let current = response;
|
|
1827
|
+
for (const middleware of this.activeMiddlewares()) {
|
|
1828
|
+
const replacement = await middleware.onCompletionResponse?.({
|
|
1829
|
+
turn,
|
|
1830
|
+
request,
|
|
1831
|
+
response: current,
|
|
1832
|
+
originalResponse: response
|
|
1833
|
+
});
|
|
1834
|
+
if (replacement?.response !== void 0) {
|
|
1835
|
+
current = replacement.response;
|
|
1836
|
+
}
|
|
1837
|
+
}
|
|
1838
|
+
return current;
|
|
1839
|
+
}
|
|
1840
|
+
async runCompletionResponseHook(prompt, response, newMessages) {
|
|
1841
|
+
const action = await this.activeHook?.onCompletionResponse?.({
|
|
1842
|
+
prompt,
|
|
1843
|
+
response,
|
|
1844
|
+
run: runControl
|
|
1845
|
+
});
|
|
1846
|
+
if (action?.type === "terminate") {
|
|
1847
|
+
throw this.cancelled(newMessages, action.reason);
|
|
1848
|
+
}
|
|
1849
|
+
}
|
|
1850
|
+
async runCompletionErrorHook(prompt, error, newMessages) {
|
|
1851
|
+
const action = await this.activeHook?.onCompletionError?.({
|
|
1852
|
+
prompt,
|
|
1853
|
+
error,
|
|
1854
|
+
run: runControl
|
|
1855
|
+
});
|
|
1856
|
+
if (action?.type === "terminate") {
|
|
1857
|
+
throw this.cancelled(newMessages, action.reason);
|
|
1858
|
+
}
|
|
1859
|
+
}
|
|
1860
|
+
activeMiddlewares() {
|
|
1861
|
+
return [...this.agent.middlewares, ...this.requestMiddlewares];
|
|
1862
|
+
}
|
|
1863
|
+
shouldBufferStreamResponseEvents() {
|
|
1864
|
+
return this.activeHook?.onCompletionResponse !== void 0 || this.activeMiddlewares().some((middleware) => middleware.onCompletionResponse !== void 0);
|
|
1865
|
+
}
|
|
1866
|
+
async drainSteeringMessages(runId, turn, newMessages, pendingTurnMessages, options = {}) {
|
|
1867
|
+
const receipts = this.steeringMessages.splice(0);
|
|
1868
|
+
if (receipts.length === 0) {
|
|
1869
|
+
if (options.closeWhenEmpty === true) {
|
|
1870
|
+
this.runState = "closing";
|
|
1871
|
+
}
|
|
1872
|
+
return [];
|
|
1873
|
+
}
|
|
1874
|
+
const messages = receipts.flatMap((receipt) => receipt.messages);
|
|
1875
|
+
newMessages.push(...messages);
|
|
1876
|
+
await this.memoryRecorder.commitMessages(runId, turn, messages, pendingTurnMessages);
|
|
1877
|
+
for (const receipt of receipts) this.onSteeringApplied?.(receipt.id);
|
|
1878
|
+
return receipts;
|
|
1879
|
+
}
|
|
1880
|
+
startRun() {
|
|
1881
|
+
if (this.runState === "idle") {
|
|
1882
|
+
this.runState = "running";
|
|
1883
|
+
return;
|
|
1884
|
+
}
|
|
1885
|
+
if (this.runState === "running") {
|
|
1886
|
+
throw new Error("Agent stream is already running.");
|
|
1887
|
+
}
|
|
1888
|
+
throw new Error("Agent stream has already been consumed.");
|
|
1889
|
+
}
|
|
1890
|
+
isTerminal() {
|
|
1891
|
+
return this.runState !== "idle" && this.runState !== "running";
|
|
1892
|
+
}
|
|
1893
|
+
cancelled(newMessages, reason) {
|
|
1894
|
+
if (this.cancellationError !== void 0) return this.cancellationError;
|
|
1895
|
+
const error = new AgentRunCancelledError([...this.chatHistory, ...newMessages], reason);
|
|
1896
|
+
this.setCancellationError(error);
|
|
1897
|
+
return error;
|
|
1898
|
+
}
|
|
1899
|
+
linkExternalAbortSignal(signal) {
|
|
1900
|
+
if (signal === void 0) return;
|
|
1901
|
+
const cancelFromSignal = () => {
|
|
1902
|
+
if (this.cancellationError !== void 0 || this.isTerminal()) return;
|
|
1903
|
+
const cause = abortError(signal.reason);
|
|
1904
|
+
const reason = abortReason(signal.reason);
|
|
1905
|
+
const messages = this.currentMessages.length === 0 ? [this.promptMessage] : [...this.currentMessages];
|
|
1906
|
+
this.setCancellationError(
|
|
1907
|
+
new AgentRunCancelledError([...this.chatHistory, ...messages], reason, { cause })
|
|
1908
|
+
);
|
|
1909
|
+
};
|
|
1910
|
+
if (signal.aborted) {
|
|
1911
|
+
cancelFromSignal();
|
|
1912
|
+
return;
|
|
1913
|
+
}
|
|
1914
|
+
signal.addEventListener("abort", cancelFromSignal, { once: true });
|
|
1915
|
+
this.removeExternalAbortListener = () => signal.removeEventListener("abort", cancelFromSignal);
|
|
1916
|
+
}
|
|
1917
|
+
throwIfCancelled() {
|
|
1918
|
+
if (this.cancellationError !== void 0) throw this.cancellationError;
|
|
1919
|
+
throwIfAborted(this.abortController.signal);
|
|
1920
|
+
}
|
|
1921
|
+
normalizeRunError(error) {
|
|
1922
|
+
return this.abortController.signal.aborted && this.cancellationError !== void 0 ? this.cancellationError : error;
|
|
1923
|
+
}
|
|
1924
|
+
modelCallOptions() {
|
|
1925
|
+
return { abortSignal: this.abortController.signal };
|
|
1926
|
+
}
|
|
1927
|
+
disposeAbortLink() {
|
|
1928
|
+
this.removeExternalAbortListener?.();
|
|
1929
|
+
this.removeExternalAbortListener = void 0;
|
|
1930
|
+
}
|
|
1931
|
+
};
|
|
1932
|
+
function abortReason(reason) {
|
|
1933
|
+
if (typeof reason === "string" && reason.trim().length > 0) return reason;
|
|
1934
|
+
if (reason instanceof Error && reason.message.trim().length > 0) return reason.message;
|
|
1935
|
+
return "External abort signal.";
|
|
1936
|
+
}
|
|
1937
|
+
function normalizeAgentInput(agentId, input) {
|
|
1938
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) {
|
|
1939
|
+
throw new TypeError("Agent runs require one options object with prompt or messages.");
|
|
1940
|
+
}
|
|
1941
|
+
const hasContinuation = "continuation" in input && input.continuation !== void 0;
|
|
1942
|
+
const hasPrompt = "prompt" in input && input.prompt !== void 0;
|
|
1943
|
+
const hasMessages = "messages" in input && input.messages !== void 0;
|
|
1944
|
+
if (Number(hasContinuation) + Number(hasPrompt) + Number(hasMessages) !== 1) {
|
|
1945
|
+
throw new TypeError("Agent runs require exactly one of prompt, messages, or continuation.");
|
|
1946
|
+
}
|
|
1947
|
+
if (hasContinuation) {
|
|
1948
|
+
if (!("response" in input) || input.response === void 0) {
|
|
1949
|
+
throw new TypeError("Agent continuation runs require an interaction response.");
|
|
1950
|
+
}
|
|
1951
|
+
if ("session" in input && input.session !== void 0) {
|
|
1952
|
+
throw new TypeError("Agent continuations carry their memory scope and cannot use session.");
|
|
1953
|
+
}
|
|
1954
|
+
const continuation = parseAgentContinuation(input.continuation);
|
|
1955
|
+
if (continuation.agentId !== agentId) {
|
|
1956
|
+
throw new TypeError(
|
|
1957
|
+
`Agent continuation belongs to "${continuation.agentId}", not "${agentId}".`
|
|
1958
|
+
);
|
|
1959
|
+
}
|
|
1960
|
+
const response = parseAgentInteractionResponse(input.response);
|
|
1961
|
+
assertAgentInteractionResponse(continuation.interaction, response);
|
|
1962
|
+
const state = parseContinuationState(continuation.state, continuation.interaction);
|
|
1963
|
+
let responsePart;
|
|
1964
|
+
if (response.type === "tool-approval") {
|
|
1965
|
+
responsePart = {
|
|
1966
|
+
type: "tool-approval-response",
|
|
1967
|
+
interactionId: continuation.interaction.id,
|
|
1968
|
+
toolCallId: continuation.interaction.toolCallId,
|
|
1969
|
+
toolName: continuation.interaction.toolName,
|
|
1970
|
+
approved: response.approved
|
|
1971
|
+
};
|
|
1972
|
+
if (response.reason !== void 0)
|
|
1973
|
+
responsePart = { ...responsePart, reason: response.reason };
|
|
1974
|
+
} else {
|
|
1975
|
+
responsePart = {
|
|
1976
|
+
type: "tool-question-response",
|
|
1977
|
+
interactionId: continuation.interaction.id,
|
|
1978
|
+
toolCallId: continuation.interaction.toolCallId,
|
|
1979
|
+
toolName: continuation.interaction.toolName,
|
|
1980
|
+
answers: response.answers
|
|
1981
|
+
};
|
|
1982
|
+
}
|
|
1983
|
+
if (continuation.interaction.callId !== void 0) {
|
|
1984
|
+
responsePart = { ...responsePart, callId: continuation.interaction.callId };
|
|
1985
|
+
}
|
|
1986
|
+
const normalized = {
|
|
1987
|
+
prompt: parseMessage({ role: "tool", content: [responsePart] }),
|
|
1988
|
+
history: [...state.history, ...state.messages],
|
|
1989
|
+
continuationState: state,
|
|
1990
|
+
interactionResponse: response,
|
|
1991
|
+
sourceRunId: continuation.sourceRunId,
|
|
1992
|
+
interactionId: continuation.interaction.id
|
|
1993
|
+
};
|
|
1994
|
+
if (state.memoryScope !== void 0) {
|
|
1995
|
+
Object.assign(normalized, { scope: normalizeMemoryScope(state.memoryScope) });
|
|
1996
|
+
}
|
|
1997
|
+
return normalized;
|
|
1998
|
+
}
|
|
1999
|
+
if (hasPrompt) {
|
|
2000
|
+
const prompt = input.prompt;
|
|
2001
|
+
if (typeof prompt !== "string" && (typeof prompt !== "object" || prompt === null || prompt.role !== "user")) {
|
|
2002
|
+
throw new TypeError("Agent prompt must be text or a user message.");
|
|
2003
|
+
}
|
|
2004
|
+
const parsedPrompt = parseMessage(
|
|
2005
|
+
typeof prompt === "string" ? { role: "user", content: prompt } : prompt
|
|
2006
|
+
);
|
|
2007
|
+
if (parsedPrompt.role !== "user") {
|
|
2008
|
+
throw new TypeError("Agent prompt must be text or a user message.");
|
|
2009
|
+
}
|
|
2010
|
+
const normalized = {
|
|
2011
|
+
prompt: parsedPrompt,
|
|
2012
|
+
history: []
|
|
2013
|
+
};
|
|
2014
|
+
if (input.session !== void 0) {
|
|
2015
|
+
Object.assign(normalized, { scope: normalizeMemoryScope(input.session) });
|
|
2016
|
+
}
|
|
2017
|
+
return normalized;
|
|
2018
|
+
}
|
|
2019
|
+
if (input.session !== void 0) {
|
|
2020
|
+
throw new TypeError("Agent messages cannot be combined with a persisted session.");
|
|
2021
|
+
}
|
|
2022
|
+
const messages = input.messages;
|
|
2023
|
+
if (!Array.isArray(messages) || messages.length === 0) {
|
|
2024
|
+
throw new TypeError("Agent input transcript must contain at least one message.");
|
|
2025
|
+
}
|
|
2026
|
+
const parsedMessages = parseMessages(messages);
|
|
2027
|
+
const activePrompt = parsedMessages.at(-1);
|
|
2028
|
+
if (activePrompt === void 0) {
|
|
2029
|
+
throw new TypeError("Agent input transcript must contain at least one message.");
|
|
2030
|
+
}
|
|
2031
|
+
if (activePrompt.role !== "user") {
|
|
2032
|
+
throw new TypeError("Agent input transcript must end with a user message.");
|
|
2033
|
+
}
|
|
2034
|
+
return {
|
|
2035
|
+
prompt: activePrompt,
|
|
2036
|
+
history: parsedMessages.slice(0, -1)
|
|
2037
|
+
};
|
|
2038
|
+
}
|
|
2039
|
+
function normalizeSteeringInput(input) {
|
|
2040
|
+
if (typeof input !== "object" || input === null || Array.isArray(input)) {
|
|
2041
|
+
throw new TypeError("Agent steering requires one options object with prompt or messages.");
|
|
2042
|
+
}
|
|
2043
|
+
const hasPrompt = "prompt" in input && input.prompt !== void 0;
|
|
2044
|
+
const hasMessages = "messages" in input && input.messages !== void 0;
|
|
2045
|
+
if (hasPrompt === hasMessages) {
|
|
2046
|
+
throw new TypeError("Agent steering requires exactly one of prompt or messages.");
|
|
2047
|
+
}
|
|
2048
|
+
if (hasPrompt) {
|
|
2049
|
+
const prompt = input.prompt;
|
|
2050
|
+
if (typeof prompt !== "string" && (typeof prompt !== "object" || prompt === null || prompt.role !== "user")) {
|
|
2051
|
+
throw new TypeError("Agent steering prompt must be text or a user message.");
|
|
2052
|
+
}
|
|
2053
|
+
const parsedPrompt = parseMessage(
|
|
2054
|
+
typeof prompt === "string" ? { role: "user", content: prompt } : prompt
|
|
2055
|
+
);
|
|
2056
|
+
if (parsedPrompt.role !== "user") {
|
|
2057
|
+
throw new TypeError("Agent steering prompt must be text or a user message.");
|
|
2058
|
+
}
|
|
2059
|
+
return [parsedPrompt];
|
|
2060
|
+
}
|
|
2061
|
+
const messages = input.messages;
|
|
2062
|
+
if (!Array.isArray(messages) || messages.length === 0) {
|
|
2063
|
+
throw new TypeError("Agent steering messages must contain at least one user message.");
|
|
2064
|
+
}
|
|
2065
|
+
const parsedMessages = parseMessages(messages);
|
|
2066
|
+
if (parsedMessages.some((message) => message.role !== "user")) {
|
|
2067
|
+
throw new TypeError("Agent steering messages must all be user messages.");
|
|
2068
|
+
}
|
|
2069
|
+
return parsedMessages;
|
|
2070
|
+
}
|
|
2071
|
+
function normalizeRequestedRunId(runId) {
|
|
2072
|
+
if (runId === void 0) {
|
|
2073
|
+
return void 0;
|
|
2074
|
+
}
|
|
2075
|
+
if (typeof runId !== "string" || runId.trim().length === 0) {
|
|
2076
|
+
throw new TypeError("runId must be a non-empty string.");
|
|
2077
|
+
}
|
|
2078
|
+
return runId;
|
|
2079
|
+
}
|
|
2080
|
+
function responseStreamEvents(turn, response, includeProviderArtifacts = true) {
|
|
2081
|
+
const events = [];
|
|
2082
|
+
for (const item of response.choice) {
|
|
2083
|
+
if (item.type === "text") {
|
|
2084
|
+
if (item.text.length > 0) {
|
|
2085
|
+
events.push({ type: "text_delta", turn, delta: item.text });
|
|
2086
|
+
}
|
|
2087
|
+
continue;
|
|
2088
|
+
}
|
|
2089
|
+
if (item.type === "reasoning") {
|
|
2090
|
+
if (item.details === void 0) {
|
|
2091
|
+
if (item.text.length > 0) {
|
|
2092
|
+
events.push(reasoningDeltaEvent(turn, item.text, { id: item.id }));
|
|
2093
|
+
}
|
|
2094
|
+
continue;
|
|
2095
|
+
}
|
|
2096
|
+
for (const content of item.details) {
|
|
2097
|
+
const delta = content.type === "encrypted" || content.type === "redacted" ? content.data : content.text;
|
|
2098
|
+
events.push(
|
|
2099
|
+
reasoningDeltaEvent(turn, delta, {
|
|
2100
|
+
id: item.id,
|
|
2101
|
+
contentType: content.type,
|
|
2102
|
+
signature: content.type === "text" ? content.signature : void 0
|
|
2103
|
+
})
|
|
2104
|
+
);
|
|
2105
|
+
}
|
|
2106
|
+
continue;
|
|
2107
|
+
}
|
|
2108
|
+
if (item.type === "tool-call") {
|
|
2109
|
+
events.push({ type: "tool_call", turn, toolCall: item });
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
if (includeProviderArtifacts) {
|
|
2113
|
+
for (const source of response.sources ?? []) {
|
|
2114
|
+
events.push({ type: "source", turn, source });
|
|
2115
|
+
}
|
|
2116
|
+
for (const toolCall of response.providerToolCalls ?? []) {
|
|
2117
|
+
events.push({ type: "provider_tool_call", turn, toolCall });
|
|
2118
|
+
}
|
|
2119
|
+
}
|
|
2120
|
+
return events;
|
|
2121
|
+
}
|
|
2122
|
+
function generationArtifacts(messages) {
|
|
2123
|
+
const sources = /* @__PURE__ */ new Map();
|
|
2124
|
+
const providerToolCalls = /* @__PURE__ */ new Map();
|
|
2125
|
+
let finishReason;
|
|
2126
|
+
let providerFinishReason;
|
|
2127
|
+
let contextUsage;
|
|
2128
|
+
for (const message of messages) {
|
|
2129
|
+
const metadata = getAssistantGenerationMetadata(message);
|
|
2130
|
+
if (metadata !== void 0) {
|
|
2131
|
+
finishReason = metadata.finishReason;
|
|
2132
|
+
providerFinishReason = metadata.providerFinishReason;
|
|
2133
|
+
contextUsage = metadata.contextUsage;
|
|
2134
|
+
}
|
|
2135
|
+
for (const source of metadata?.sources ?? []) {
|
|
2136
|
+
const key = `${source.url}\0${source.startIndex ?? ""}\0${source.endIndex ?? ""}`;
|
|
2137
|
+
sources.set(key, source);
|
|
2138
|
+
}
|
|
2139
|
+
for (const toolCall of metadata?.providerToolCalls ?? []) {
|
|
2140
|
+
providerToolCalls.set(toolCall.id, toolCall);
|
|
2141
|
+
}
|
|
2142
|
+
}
|
|
2143
|
+
const artifacts = {};
|
|
2144
|
+
if (sources.size > 0) artifacts.sources = [...sources.values()];
|
|
2145
|
+
if (providerToolCalls.size > 0) {
|
|
2146
|
+
artifacts.providerToolCalls = [...providerToolCalls.values()];
|
|
2147
|
+
}
|
|
2148
|
+
if (finishReason !== void 0) artifacts.finishReason = finishReason;
|
|
2149
|
+
if (providerFinishReason !== void 0) {
|
|
2150
|
+
artifacts.providerFinishReason = providerFinishReason;
|
|
2151
|
+
}
|
|
2152
|
+
if (contextUsage !== void 0) artifacts.contextUsage = contextUsage;
|
|
2153
|
+
return artifacts;
|
|
2154
|
+
}
|
|
2155
|
+
function reasoningDeltaEvent(turn, delta, details = {}) {
|
|
2156
|
+
const event = {
|
|
2157
|
+
type: "reasoning_delta",
|
|
2158
|
+
turn,
|
|
2159
|
+
delta
|
|
2160
|
+
};
|
|
2161
|
+
if (details.id !== void 0) {
|
|
2162
|
+
event.id = details.id;
|
|
2163
|
+
}
|
|
2164
|
+
if (details.contentType !== void 0) {
|
|
2165
|
+
event.contentType = details.contentType;
|
|
2166
|
+
}
|
|
2167
|
+
if (details.signature !== void 0) {
|
|
2168
|
+
event.signature = details.signature;
|
|
2169
|
+
}
|
|
2170
|
+
return event;
|
|
2171
|
+
}
|
|
2172
|
+
function textFromMessage(message) {
|
|
2173
|
+
if (message.role === "system") {
|
|
2174
|
+
return message.content;
|
|
2175
|
+
}
|
|
2176
|
+
if (typeof message.content === "string") {
|
|
2177
|
+
return message.content;
|
|
2178
|
+
}
|
|
2179
|
+
return message.content.flatMap((content) => {
|
|
2180
|
+
if (content.type === "text") {
|
|
2181
|
+
return [content.text];
|
|
2182
|
+
}
|
|
2183
|
+
if (content.type === "file" && content.data.type === "text") {
|
|
2184
|
+
return [content.data.text];
|
|
2185
|
+
}
|
|
2186
|
+
return [];
|
|
2187
|
+
}).join("\n");
|
|
2188
|
+
}
|
|
2189
|
+
function latestAssistantText(messages) {
|
|
2190
|
+
for (let index = messages.length - 1; index >= 0; index -= 1) {
|
|
2191
|
+
const message = messages[index];
|
|
2192
|
+
if (message?.role === "assistant") {
|
|
2193
|
+
return typeof message.content === "string" ? message.content : textFromAssistantContent(message.content);
|
|
2194
|
+
}
|
|
2195
|
+
}
|
|
2196
|
+
return "";
|
|
2197
|
+
}
|
|
2198
|
+
function structuredOutputRetryRequest(request, response, error) {
|
|
2199
|
+
const truncated = error instanceof AgentStructuredOutputError && error.phase === "truncated";
|
|
2200
|
+
const correction = {
|
|
2201
|
+
role: "user",
|
|
2202
|
+
content: truncated ? STRUCTURED_OUTPUT_TRUNCATED_RETRY_PROMPT : STRUCTURED_OUTPUT_RETRY_PROMPT
|
|
2203
|
+
};
|
|
2204
|
+
if (truncated) {
|
|
2205
|
+
return {
|
|
2206
|
+
request: {
|
|
2207
|
+
...request,
|
|
2208
|
+
chatHistory: [...request.chatHistory, correction]
|
|
2209
|
+
},
|
|
2210
|
+
previousResponse: "omitted",
|
|
2211
|
+
includedOutputLength: 0
|
|
2212
|
+
};
|
|
2213
|
+
}
|
|
2214
|
+
const preview = structuredOutputRepairPreview(textFromAssistantContent(response.choice));
|
|
2215
|
+
if (preview.includedOutputLength === 0) {
|
|
2216
|
+
return {
|
|
2217
|
+
request: {
|
|
2218
|
+
...request,
|
|
2219
|
+
chatHistory: [...request.chatHistory, correction]
|
|
2220
|
+
},
|
|
2221
|
+
previousResponse: "omitted",
|
|
2222
|
+
includedOutputLength: 0
|
|
2223
|
+
};
|
|
2224
|
+
}
|
|
2225
|
+
const invalidResponse = {
|
|
2226
|
+
role: "assistant",
|
|
2227
|
+
content: [{ type: "text", text: preview.text }]
|
|
2228
|
+
};
|
|
2229
|
+
return {
|
|
2230
|
+
request: {
|
|
2231
|
+
...request,
|
|
2232
|
+
chatHistory: [...request.chatHistory, invalidResponse, correction]
|
|
2233
|
+
},
|
|
2234
|
+
previousResponse: "preview",
|
|
2235
|
+
includedOutputLength: preview.includedOutputLength
|
|
2236
|
+
};
|
|
2237
|
+
}
|
|
2238
|
+
function structuredOutputRetryEventAttributes(error, retryRequest) {
|
|
2239
|
+
const attributes = {
|
|
2240
|
+
previousResponse: retryRequest.previousResponse,
|
|
2241
|
+
includedOutputLength: retryRequest.includedOutputLength
|
|
2242
|
+
};
|
|
2243
|
+
if (!(error instanceof AgentStructuredOutputError)) return attributes;
|
|
2244
|
+
Object.assign(attributes, {
|
|
2245
|
+
failurePhase: error.phase,
|
|
2246
|
+
outputLength: error.outputLength,
|
|
2247
|
+
normalizedLength: error.normalizedLength,
|
|
2248
|
+
attemptUsage: usageEventValue(error.attemptUsage),
|
|
2249
|
+
cumulativeUsage: usageEventValue(error.usage)
|
|
2250
|
+
});
|
|
2251
|
+
if (error.finishReason !== void 0) attributes.finishReason = error.finishReason;
|
|
2252
|
+
if (error.providerFinishReason !== void 0) {
|
|
2253
|
+
attributes.providerFinishReason = error.providerFinishReason;
|
|
2254
|
+
}
|
|
2255
|
+
return attributes;
|
|
2256
|
+
}
|
|
2257
|
+
function providerOutputRetryEventAttributes(error, cumulativeUsage, attemptUsage) {
|
|
2258
|
+
const resolvedAttemptUsage = attemptUsage ?? completionProviderOutputErrorUsage(error);
|
|
2259
|
+
if (resolvedAttemptUsage === void 0) return void 0;
|
|
2260
|
+
return {
|
|
2261
|
+
attemptUsage: usageEventValue(resolvedAttemptUsage),
|
|
2262
|
+
cumulativeUsage: usageEventValue(cumulativeUsage)
|
|
2263
|
+
};
|
|
2264
|
+
}
|
|
2265
|
+
function usageEventValue(usage) {
|
|
2266
|
+
const value = {
|
|
2267
|
+
inputTokens: usage.inputTokens,
|
|
2268
|
+
outputTokens: usage.outputTokens,
|
|
2269
|
+
totalTokens: usage.totalTokens,
|
|
2270
|
+
cachedInputTokens: usage.cachedInputTokens,
|
|
2271
|
+
cacheCreationInputTokens: usage.cacheCreationInputTokens
|
|
2272
|
+
};
|
|
2273
|
+
if (usage.details !== void 0) value.details = { ...usage.details };
|
|
2274
|
+
return value;
|
|
2275
|
+
}
|
|
2276
|
+
function providerMessages(messages) {
|
|
2277
|
+
const providerMessages2 = [];
|
|
2278
|
+
for (const message of messages) {
|
|
2279
|
+
if (message.role !== "tool") {
|
|
2280
|
+
providerMessages2.push(message);
|
|
2281
|
+
continue;
|
|
2282
|
+
}
|
|
2283
|
+
const results = message.content.filter((part) => part.type === "tool-result");
|
|
2284
|
+
if (results.length > 0) {
|
|
2285
|
+
providerMessages2.push({ ...message, content: results });
|
|
2286
|
+
}
|
|
2287
|
+
}
|
|
2288
|
+
return providerMessages2;
|
|
2289
|
+
}
|
|
2290
|
+
function guardrailDecisionAttributes(decision) {
|
|
2291
|
+
const attributes = {
|
|
2292
|
+
policyId: decision.policyId,
|
|
2293
|
+
guardrailId: decision.guardrailId,
|
|
2294
|
+
boundary: decision.boundary,
|
|
2295
|
+
mode: decision.mode,
|
|
2296
|
+
action: decision.action,
|
|
2297
|
+
applied: decision.applied,
|
|
2298
|
+
latencyMs: decision.latencyMs
|
|
2299
|
+
};
|
|
2300
|
+
if (decision.reason !== void 0) {
|
|
2301
|
+
attributes.reason = decision.reason;
|
|
2302
|
+
}
|
|
2303
|
+
if (decision.message !== void 0) {
|
|
2304
|
+
attributes.message = decision.message;
|
|
2305
|
+
}
|
|
2306
|
+
return attributes;
|
|
2307
|
+
}
|
|
2308
|
+
function blockedOutcomeDetails(decisions, fallbackReason) {
|
|
2309
|
+
for (let index = decisions.length - 1; index >= 0; index -= 1) {
|
|
2310
|
+
const decision = decisions[index];
|
|
2311
|
+
if (decision?.applied !== true || decision.action !== "block") continue;
|
|
2312
|
+
return decision.message === void 0 ? { reason: decision.reason ?? fallbackReason } : { reason: decision.reason ?? fallbackReason, message: decision.message };
|
|
2313
|
+
}
|
|
2314
|
+
return { reason: fallbackReason };
|
|
2315
|
+
}
|
|
2316
|
+
function observerRunEnd(outcome) {
|
|
2317
|
+
const common = {
|
|
2318
|
+
runId: outcome.runId,
|
|
2319
|
+
text: outcome.text,
|
|
2320
|
+
usage: outcome.usage,
|
|
2321
|
+
messages: outcome.messages,
|
|
2322
|
+
sources: outcome.sources,
|
|
2323
|
+
providerToolCalls: outcome.providerToolCalls,
|
|
2324
|
+
resumedFrom: outcome.resumedFrom
|
|
2325
|
+
};
|
|
2326
|
+
switch (outcome.type) {
|
|
2327
|
+
case "response":
|
|
2328
|
+
return { ...common, status: "completed", output: outcome.output };
|
|
2329
|
+
case "blocked":
|
|
2330
|
+
return { ...common, status: "blocked", stage: outcome.stage };
|
|
2331
|
+
case "interaction":
|
|
2332
|
+
return { ...common, status: "suspended", interaction: outcome.interaction };
|
|
2333
|
+
}
|
|
2334
|
+
}
|
|
2335
|
+
|
|
2336
|
+
export {
|
|
2337
|
+
AgentRun
|
|
2338
|
+
};
|
|
2339
|
+
//# sourceMappingURL=chunk-RT5LUEO3.js.map
|