@graphrefly/graphrefly 0.45.0 → 0.47.0
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 +1 -2
- package/dist/_internal-B23BagFd.d.cts +33 -0
- package/dist/_internal-B23BagFd.d.ts +33 -0
- package/dist/adaptive-rate-limiter-Dch_xYIi.d.cts +111 -0
- package/dist/adaptive-rate-limiter-Dch_xYIi.d.ts +111 -0
- package/dist/agents-C0Ji9ldU.d.cts +629 -0
- package/dist/agents-C9zexT7I.d.ts +629 -0
- package/dist/audit-BAXb3VOg.d.ts +246 -0
- package/dist/audit-C_bPfkqS.d.cts +246 -0
- package/dist/backoff-7KIK3WQW.js +24 -0
- package/dist/backoff-7KIK3WQW.js.map +1 -0
- package/dist/backoff-Bnb9OoPh.d.cts +6 -0
- package/dist/backoff-Bnb9OoPh.d.ts +6 -0
- package/dist/base/composition/index.cjs +865 -0
- package/dist/base/composition/index.cjs.map +1 -0
- package/dist/base/composition/index.d.cts +468 -0
- package/dist/base/composition/index.d.ts +468 -0
- package/dist/base/composition/index.js +40 -0
- package/dist/base/composition/index.js.map +1 -0
- package/dist/base/index.cjs +6390 -0
- package/dist/base/index.cjs.map +1 -0
- package/dist/base/index.d.cts +21 -0
- package/dist/base/index.d.ts +21 -0
- package/dist/base/index.js +259 -0
- package/dist/base/index.js.map +1 -0
- package/dist/base/io/index.cjs +3270 -0
- package/dist/base/io/index.cjs.map +1 -0
- package/dist/base/io/index.d.cts +2245 -0
- package/dist/base/io/index.d.ts +2245 -0
- package/dist/base/io/index.js +117 -0
- package/dist/base/io/index.js.map +1 -0
- package/dist/base/meta/index.cjs +43 -0
- package/dist/base/meta/index.cjs.map +1 -0
- package/dist/base/meta/index.d.cts +45 -0
- package/dist/base/meta/index.d.ts +45 -0
- package/dist/base/meta/index.js +13 -0
- package/dist/base/meta/index.js.map +1 -0
- package/dist/base/mutation/index.cjs +200 -0
- package/dist/base/mutation/index.cjs.map +1 -0
- package/dist/base/mutation/index.d.cts +177 -0
- package/dist/base/mutation/index.d.ts +177 -0
- package/dist/base/mutation/index.js +22 -0
- package/dist/base/mutation/index.js.map +1 -0
- package/dist/base/render/index.cjs +1120 -0
- package/dist/base/render/index.cjs.map +1 -0
- package/dist/base/render/index.d.cts +227 -0
- package/dist/base/render/index.d.ts +227 -0
- package/dist/base/render/index.js +24 -0
- package/dist/base/render/index.js.map +1 -0
- package/dist/base/sources/browser/index.cjs +172 -0
- package/dist/base/sources/browser/index.cjs.map +1 -0
- package/dist/base/sources/browser/index.d.cts +84 -0
- package/dist/base/sources/browser/index.d.ts +84 -0
- package/dist/base/sources/browser/index.js +151 -0
- package/dist/base/sources/browser/index.js.map +1 -0
- package/dist/base/sources/event/index.cjs +98 -0
- package/dist/base/sources/event/index.cjs.map +1 -0
- package/dist/base/sources/event/index.d.cts +91 -0
- package/dist/base/sources/event/index.d.ts +91 -0
- package/dist/base/sources/event/index.js +13 -0
- package/dist/base/sources/event/index.js.map +1 -0
- package/dist/base/sources/index.cjs +755 -0
- package/dist/base/sources/index.cjs.map +1 -0
- package/dist/base/sources/index.d.cts +357 -0
- package/dist/base/sources/index.d.ts +357 -0
- package/dist/base/sources/index.js +42 -0
- package/dist/base/sources/index.js.map +1 -0
- package/dist/base/sources/node/index.cjs +320 -0
- package/dist/base/sources/node/index.cjs.map +1 -0
- package/dist/base/sources/node/index.d.cts +185 -0
- package/dist/base/sources/node/index.d.ts +185 -0
- package/dist/base/sources/node/index.js +306 -0
- package/dist/base/sources/node/index.js.map +1 -0
- package/dist/base/utils/index.cjs +37 -0
- package/dist/base/utils/index.cjs.map +1 -0
- package/dist/base/utils/index.d.cts +37 -0
- package/dist/base/utils/index.d.ts +37 -0
- package/dist/base/utils/index.js +11 -0
- package/dist/base/utils/index.js.map +1 -0
- package/dist/base/worker/index.cjs +548 -0
- package/dist/base/worker/index.cjs.map +1 -0
- package/dist/base/worker/index.d.cts +207 -0
- package/dist/base/worker/index.d.ts +207 -0
- package/dist/base/worker/index.js +20 -0
- package/dist/base/worker/index.js.map +1 -0
- package/dist/breaker-C9skL3d8.d.ts +175 -0
- package/dist/breaker-ugSdq54q.d.cts +175 -0
- package/dist/cascading-CSSbKGrJ.d.ts +199 -0
- package/dist/cascading-baGkiihI.d.cts +199 -0
- package/dist/chunk-22SG74BD.js +207 -0
- package/dist/chunk-22SG74BD.js.map +1 -0
- package/dist/chunk-255UCBG4.js +58 -0
- package/dist/chunk-255UCBG4.js.map +1 -0
- package/dist/chunk-2LO3EL4W.js +1 -0
- package/dist/chunk-2LO3EL4W.js.map +1 -0
- package/dist/chunk-2OB3CEJS.js +1065 -0
- package/dist/chunk-2OB3CEJS.js.map +1 -0
- package/dist/chunk-36NMM65U.js +144 -0
- package/dist/chunk-36NMM65U.js.map +1 -0
- package/dist/chunk-3CEXCBN6.js +1 -0
- package/dist/chunk-3CEXCBN6.js.map +1 -0
- package/dist/chunk-3MUSLI6E.js +105 -0
- package/dist/chunk-3MUSLI6E.js.map +1 -0
- package/dist/chunk-3PSLNJDU.js +884 -0
- package/dist/chunk-3PSLNJDU.js.map +1 -0
- package/dist/chunk-42FQ27MQ.js +594 -0
- package/dist/chunk-42FQ27MQ.js.map +1 -0
- package/dist/chunk-4GYMCUDZ.js +1085 -0
- package/dist/chunk-4GYMCUDZ.js.map +1 -0
- package/dist/chunk-4S53H2KR.js +382 -0
- package/dist/chunk-4S53H2KR.js.map +1 -0
- package/dist/chunk-4XCHZRUJ.js +128 -0
- package/dist/chunk-4XCHZRUJ.js.map +1 -0
- package/dist/chunk-5IMMNARC.js +1153 -0
- package/dist/chunk-5IMMNARC.js.map +1 -0
- package/dist/chunk-6XZYT4SW.js +256 -0
- package/dist/chunk-6XZYT4SW.js.map +1 -0
- package/dist/chunk-7EGRP2VX.js +76 -0
- package/dist/chunk-7EGRP2VX.js.map +1 -0
- package/dist/chunk-A7KV5UK4.js +150 -0
- package/dist/chunk-A7KV5UK4.js.map +1 -0
- package/dist/chunk-APY2SS5X.js +156 -0
- package/dist/chunk-APY2SS5X.js.map +1 -0
- package/dist/chunk-AZDQPQ3V.js +66 -0
- package/dist/chunk-AZDQPQ3V.js.map +1 -0
- package/dist/chunk-BU3SEFA5.js +90 -0
- package/dist/chunk-BU3SEFA5.js.map +1 -0
- package/dist/chunk-BXGZFGZ4.js +189 -0
- package/dist/chunk-BXGZFGZ4.js.map +1 -0
- package/dist/chunk-CGHORL6G.js +579 -0
- package/dist/chunk-CGHORL6G.js.map +1 -0
- package/dist/chunk-CXANAIZU.js +530 -0
- package/dist/chunk-CXANAIZU.js.map +1 -0
- package/dist/chunk-CZQHCKKG.js +1 -0
- package/dist/chunk-CZQHCKKG.js.map +1 -0
- package/dist/chunk-DKNHAICT.js +133 -0
- package/dist/chunk-DKNHAICT.js.map +1 -0
- package/dist/chunk-DM4OMPWK.js +584 -0
- package/dist/chunk-DM4OMPWK.js.map +1 -0
- package/dist/chunk-DMSNO6ZB.js +452 -0
- package/dist/chunk-DMSNO6ZB.js.map +1 -0
- package/dist/chunk-E5OZPDIW.js +229 -0
- package/dist/chunk-E5OZPDIW.js.map +1 -0
- package/dist/chunk-EHRRQ4IC.js +211 -0
- package/dist/chunk-EHRRQ4IC.js.map +1 -0
- package/dist/chunk-EVYY4X5A.js +509 -0
- package/dist/chunk-EVYY4X5A.js.map +1 -0
- package/dist/chunk-FDFD67UO.js +1 -0
- package/dist/chunk-FDFD67UO.js.map +1 -0
- package/dist/chunk-FMPF42Q4.js +13 -0
- package/dist/chunk-FMPF42Q4.js.map +1 -0
- package/dist/chunk-FW23JYNQ.js +454 -0
- package/dist/chunk-FW23JYNQ.js.map +1 -0
- package/dist/chunk-GWRNLJNW.js +2508 -0
- package/dist/chunk-GWRNLJNW.js.map +1 -0
- package/dist/chunk-HL7HUJIX.js +1 -0
- package/dist/chunk-HL7HUJIX.js.map +1 -0
- package/dist/chunk-IHTWQEDR.js +169 -0
- package/dist/chunk-IHTWQEDR.js.map +1 -0
- package/dist/chunk-IJRR6YAI.js +128 -0
- package/dist/chunk-IJRR6YAI.js.map +1 -0
- package/dist/chunk-JGFRAFDL.js +221 -0
- package/dist/chunk-JGFRAFDL.js.map +1 -0
- package/dist/chunk-JKTC747G.js +725 -0
- package/dist/chunk-JKTC747G.js.map +1 -0
- package/dist/chunk-KN3H5CNT.js +11 -0
- package/dist/chunk-KN3H5CNT.js.map +1 -0
- package/dist/chunk-KPG3DGLA.js +1 -0
- package/dist/chunk-KPG3DGLA.js.map +1 -0
- package/dist/chunk-KRNQ6RGQ.js +1 -0
- package/dist/chunk-KRNQ6RGQ.js.map +1 -0
- package/dist/chunk-MLTPJMH6.js +417 -0
- package/dist/chunk-MLTPJMH6.js.map +1 -0
- package/dist/chunk-N3SZ7BMH.js +95 -0
- package/dist/chunk-N3SZ7BMH.js.map +1 -0
- package/dist/chunk-NDUD3IMO.js +540 -0
- package/dist/chunk-NDUD3IMO.js.map +1 -0
- package/dist/chunk-NY2PYHNC.js +873 -0
- package/dist/chunk-NY2PYHNC.js.map +1 -0
- package/dist/chunk-O3MT7DYI.js +225 -0
- package/dist/chunk-O3MT7DYI.js.map +1 -0
- package/dist/chunk-OCUDSN63.js +2386 -0
- package/dist/chunk-OCUDSN63.js.map +1 -0
- package/dist/chunk-OIWU3NYV.js +199 -0
- package/dist/chunk-OIWU3NYV.js.map +1 -0
- package/dist/chunk-OQUIJT7A.js +1 -0
- package/dist/chunk-OQUIJT7A.js.map +1 -0
- package/dist/chunk-P5LBT622.js +105 -0
- package/dist/chunk-P5LBT622.js.map +1 -0
- package/dist/chunk-PKGQG5QQ.js +519 -0
- package/dist/chunk-PKGQG5QQ.js.map +1 -0
- package/dist/chunk-PKPO3JTZ.js +561 -0
- package/dist/chunk-PKPO3JTZ.js.map +1 -0
- package/dist/chunk-PL5UDIQ5.js +118 -0
- package/dist/chunk-PL5UDIQ5.js.map +1 -0
- package/dist/chunk-PZWISPIQ.js +432 -0
- package/dist/chunk-PZWISPIQ.js.map +1 -0
- package/dist/chunk-Q3EYOCZB.js +510 -0
- package/dist/chunk-Q3EYOCZB.js.map +1 -0
- package/dist/chunk-QMBYUVRL.js +15 -0
- package/dist/chunk-QMBYUVRL.js.map +1 -0
- package/dist/chunk-RAGGHLCV.js +200 -0
- package/dist/chunk-RAGGHLCV.js.map +1 -0
- package/dist/chunk-RJOG4IJU.js +1039 -0
- package/dist/chunk-RJOG4IJU.js.map +1 -0
- package/dist/chunk-SOOKUYVM.js +403 -0
- package/dist/chunk-SOOKUYVM.js.map +1 -0
- package/dist/chunk-T5BN5KG7.js +1 -0
- package/dist/chunk-T5BN5KG7.js.map +1 -0
- package/dist/chunk-TNX5ZGDJ.js +574 -0
- package/dist/chunk-TNX5ZGDJ.js.map +1 -0
- package/dist/chunk-TP7244Y6.js +207 -0
- package/dist/chunk-TP7244Y6.js.map +1 -0
- package/dist/chunk-TSBFTJKM.js +57 -0
- package/dist/chunk-TSBFTJKM.js.map +1 -0
- package/dist/chunk-URQ2CBBF.js +143 -0
- package/dist/chunk-URQ2CBBF.js.map +1 -0
- package/dist/chunk-VLAGJZSL.js +1079 -0
- package/dist/chunk-VLAGJZSL.js.map +1 -0
- package/dist/chunk-W2BOPXTI.js +1 -0
- package/dist/chunk-W2BOPXTI.js.map +1 -0
- package/dist/chunk-Y52CS6YA.js +88 -0
- package/dist/chunk-Y52CS6YA.js.map +1 -0
- package/dist/chunk-YCBUWK77.js +92 -0
- package/dist/chunk-YCBUWK77.js.map +1 -0
- package/dist/chunk-YJ4U2D2C.js +314 -0
- package/dist/chunk-YJ4U2D2C.js.map +1 -0
- package/dist/chunk-Z4YXAUDN.js +239 -0
- package/dist/chunk-Z4YXAUDN.js.map +1 -0
- package/dist/chunk-Z65DVDEQ.js +146 -0
- package/dist/chunk-Z65DVDEQ.js.map +1 -0
- package/dist/chunk-Z6EGP5D7.js +92 -0
- package/dist/chunk-Z6EGP5D7.js.map +1 -0
- package/dist/chunk-ZT4WMQW4.js +1575 -0
- package/dist/chunk-ZT4WMQW4.js.map +1 -0
- package/dist/chunk-ZVXXDWIB.js +1282 -0
- package/dist/chunk-ZVXXDWIB.js.map +1 -0
- package/dist/compat/index.cjs +3150 -6
- package/dist/compat/index.cjs.map +1 -1
- package/dist/compat/index.d.cts +116 -1
- package/dist/compat/index.d.ts +116 -1
- package/dist/compat/index.js +175 -2
- package/dist/compat/index.js.map +1 -1
- package/dist/compat/jotai/index.cjs +130 -2
- package/dist/compat/jotai/index.cjs.map +1 -1
- package/dist/compat/jotai/index.d.cts +2 -1
- package/dist/compat/jotai/index.d.ts +2 -1
- package/dist/compat/jotai/index.js +7 -2
- package/dist/compat/jotai/index.js.map +1 -1
- package/dist/compat/nanostores/index.cjs +186 -2
- package/dist/compat/nanostores/index.cjs.map +1 -1
- package/dist/compat/nanostores/index.d.cts +2 -1
- package/dist/compat/nanostores/index.d.ts +2 -1
- package/dist/compat/nanostores/index.js +21 -2
- package/dist/compat/nanostores/index.js.map +1 -1
- package/dist/compat/nestjs/index.cjs +2291 -6
- package/dist/compat/nestjs/index.cjs.map +1 -1
- package/dist/compat/nestjs/index.d.cts +10 -1
- package/dist/compat/nestjs/index.d.ts +10 -1
- package/dist/compat/nestjs/index.js +76 -2
- package/dist/compat/nestjs/index.js.map +1 -1
- package/dist/compat/react/index.cjs +95 -2
- package/dist/compat/react/index.cjs.map +1 -1
- package/dist/compat/react/index.d.cts +2 -1
- package/dist/compat/react/index.d.ts +2 -1
- package/dist/compat/react/index.js +11 -2
- package/dist/compat/react/index.js.map +1 -1
- package/dist/compat/solid/index.cjs +82 -2
- package/dist/compat/solid/index.cjs.map +1 -1
- package/dist/compat/solid/index.d.cts +2 -1
- package/dist/compat/solid/index.d.ts +2 -1
- package/dist/compat/solid/index.js +11 -2
- package/dist/compat/solid/index.js.map +1 -1
- package/dist/compat/svelte/index.cjs +85 -2
- package/dist/compat/svelte/index.cjs.map +1 -1
- package/dist/compat/svelte/index.d.cts +2 -1
- package/dist/compat/svelte/index.d.ts +2 -1
- package/dist/compat/svelte/index.js +11 -2
- package/dist/compat/svelte/index.js.map +1 -1
- package/dist/compat/vue/index.cjs +100 -2
- package/dist/compat/vue/index.cjs.map +1 -1
- package/dist/compat/vue/index.d.cts +3 -1
- package/dist/compat/vue/index.d.ts +3 -1
- package/dist/compat/vue/index.js +11 -2
- package/dist/compat/vue/index.js.map +1 -1
- package/dist/compat/zustand/index.cjs +50 -2
- package/dist/compat/zustand/index.cjs.map +1 -1
- package/dist/compat/zustand/index.d.cts +2 -1
- package/dist/compat/zustand/index.d.ts +2 -1
- package/dist/compat/zustand/index.js +7 -2
- package/dist/compat/zustand/index.js.map +1 -1
- package/dist/distill-De6Rnn15.d.cts +48 -0
- package/dist/distill-De6Rnn15.d.ts +48 -0
- package/dist/external-register-CWyroXb_.d.cts +138 -0
- package/dist/external-register-CWyroXb_.d.ts +138 -0
- package/dist/fallback-Bx46zqky.d.cts +243 -0
- package/dist/fallback-pIWW8A2d.d.ts +243 -0
- package/dist/guarded-execution-BcdtxeBk.d.ts +207 -0
- package/dist/guarded-execution-C-3hnP6A.d.cts +207 -0
- package/dist/index-B6pxYJzO.d.cts +36 -0
- package/dist/index-B6pxYJzO.d.ts +36 -0
- package/dist/index-BFsng6v1.d.cts +44 -0
- package/dist/index-BFsng6v1.d.ts +44 -0
- package/dist/index-B_p8tnvf.d.cts +770 -0
- package/dist/index-Bg-LwEt-.d.cts +45 -0
- package/dist/index-Bg-LwEt-.d.ts +45 -0
- package/dist/index-Brp888t0.d.cts +127 -0
- package/dist/index-Brp888t0.d.ts +127 -0
- package/dist/index-CDfk6jHN.d.cts +37 -0
- package/dist/index-CDfk6jHN.d.ts +37 -0
- package/dist/index-DLAxYaN5.d.cts +169 -0
- package/dist/index-DLAxYaN5.d.ts +169 -0
- package/dist/index-DeWbQzMe.d.cts +34 -0
- package/dist/index-DeWbQzMe.d.ts +34 -0
- package/dist/index-_HDSmPyp.d.ts +770 -0
- package/dist/index-dX9IzPqj.d.cts +86 -0
- package/dist/index-dX9IzPqj.d.ts +86 -0
- package/dist/index.cjs +26009 -0
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +55 -42
- package/dist/index.d.ts +55 -42
- package/dist/index.js +849 -0
- package/dist/index.js.map +1 -1
- package/dist/layout-types-B5aiHYgk.d.cts +72 -0
- package/dist/layout-types-B5aiHYgk.d.ts +72 -0
- package/dist/memory-composers-BryDrRBX.d.cts +529 -0
- package/dist/memory-composers-CVQqPYEV.d.ts +529 -0
- package/dist/observable-B25XqCbZ.d.cts +59 -0
- package/dist/observable-B25XqCbZ.d.ts +59 -0
- package/dist/pipeline-graph-Ce47CB6Y.d.cts +145 -0
- package/dist/pipeline-graph-DXCwY9vG.d.ts +145 -0
- package/dist/presets/ai/index.cjs +4377 -0
- package/dist/presets/ai/index.cjs.map +1 -0
- package/dist/presets/ai/index.d.cts +98 -0
- package/dist/presets/ai/index.d.ts +98 -0
- package/dist/presets/ai/index.js +54 -0
- package/dist/presets/ai/index.js.map +1 -0
- package/dist/presets/harness/index.cjs +5929 -0
- package/dist/presets/harness/index.cjs.map +1 -0
- package/dist/presets/harness/index.d.cts +566 -0
- package/dist/presets/harness/index.d.ts +566 -0
- package/dist/presets/harness/index.js +71 -0
- package/dist/presets/harness/index.js.map +1 -0
- package/dist/presets/index.cjs +9782 -0
- package/dist/presets/index.cjs.map +1 -0
- package/dist/presets/index.d.cts +28 -0
- package/dist/presets/index.d.ts +28 -0
- package/dist/presets/index.js +129 -0
- package/dist/presets/index.js.map +1 -0
- package/dist/presets/inspect/index.cjs +1087 -0
- package/dist/presets/inspect/index.cjs.map +1 -0
- package/dist/presets/inspect/index.d.cts +172 -0
- package/dist/presets/inspect/index.d.ts +172 -0
- package/dist/presets/inspect/index.js +21 -0
- package/dist/presets/inspect/index.js.map +1 -0
- package/dist/presets/resilience/index.cjs +1593 -0
- package/dist/presets/resilience/index.cjs.map +1 -0
- package/dist/presets/resilience/index.d.cts +205 -0
- package/dist/presets/resilience/index.d.ts +205 -0
- package/dist/presets/resilience/index.js +18 -0
- package/dist/presets/resilience/index.js.map +1 -0
- package/dist/rate-limiter-CEALq4N1.d.ts +559 -0
- package/dist/rate-limiter-DpVbSYdH.d.cts +559 -0
- package/dist/reactive-layout-fswlBUvX.d.cts +195 -0
- package/dist/reactive-layout-fswlBUvX.d.ts +195 -0
- package/dist/retry-BDbRZ_gx.d.ts +125 -0
- package/dist/retry-DWuhjvsA.d.cts +125 -0
- package/dist/solutions/index.cjs +8200 -0
- package/dist/solutions/index.cjs.map +1 -0
- package/dist/solutions/index.d.cts +23 -0
- package/dist/solutions/index.d.ts +23 -0
- package/dist/solutions/index.js +55 -0
- package/dist/solutions/index.js.map +1 -0
- package/dist/spawnable-5mDY501F.d.cts +746 -0
- package/dist/spawnable-D3lR0oQu.d.ts +746 -0
- package/dist/status-U-rUI79b.d.cts +84 -0
- package/dist/status-U-rUI79b.d.ts +84 -0
- package/dist/timeout-U5O4ESK3.js +12 -0
- package/dist/timeout-U5O4ESK3.js.map +1 -0
- package/dist/types-BB5Lw-pB.d.cts +442 -0
- package/dist/types-BB5Lw-pB.d.ts +442 -0
- package/dist/types-CJWIMJiZ.d.ts +548 -0
- package/dist/types-vCq7ShIm.d.cts +548 -0
- package/dist/utils/ai/browser.cjs +2169 -0
- package/dist/utils/ai/browser.cjs.map +1 -0
- package/dist/utils/ai/browser.d.cts +129 -0
- package/dist/utils/ai/browser.d.ts +129 -0
- package/dist/utils/ai/browser.js +255 -0
- package/dist/utils/ai/browser.js.map +1 -0
- package/dist/utils/ai/index.cjs +8468 -0
- package/dist/utils/ai/index.cjs.map +1 -0
- package/dist/utils/ai/index.d.cts +1777 -0
- package/dist/utils/ai/index.d.ts +1777 -0
- package/dist/utils/ai/index.js +173 -0
- package/dist/utils/ai/index.js.map +1 -0
- package/dist/utils/ai/node.cjs +648 -0
- package/dist/utils/ai/node.cjs.map +1 -0
- package/dist/utils/ai/node.d.cts +57 -0
- package/dist/utils/ai/node.d.ts +57 -0
- package/dist/utils/ai/node.js +84 -0
- package/dist/utils/ai/node.js.map +1 -0
- package/dist/utils/cqrs/index.cjs +1036 -0
- package/dist/utils/cqrs/index.cjs.map +1 -0
- package/dist/utils/cqrs/index.d.cts +438 -0
- package/dist/utils/cqrs/index.d.ts +438 -0
- package/dist/utils/cqrs/index.js +18 -0
- package/dist/utils/cqrs/index.js.map +1 -0
- package/dist/utils/demo-shell/index.cjs +865 -0
- package/dist/utils/demo-shell/index.cjs.map +1 -0
- package/dist/utils/demo-shell/index.d.cts +90 -0
- package/dist/utils/demo-shell/index.d.ts +90 -0
- package/dist/utils/demo-shell/index.js +13 -0
- package/dist/utils/demo-shell/index.js.map +1 -0
- package/dist/utils/domain-templates/index.cjs +732 -0
- package/dist/utils/domain-templates/index.cjs.map +1 -0
- package/dist/utils/domain-templates/index.d.cts +214 -0
- package/dist/utils/domain-templates/index.d.ts +214 -0
- package/dist/utils/domain-templates/index.js +17 -0
- package/dist/utils/domain-templates/index.js.map +1 -0
- package/dist/utils/graphspec/index.cjs +1174 -0
- package/dist/utils/graphspec/index.cjs.map +1 -0
- package/dist/utils/graphspec/index.d.cts +449 -0
- package/dist/utils/graphspec/index.d.ts +449 -0
- package/dist/utils/graphspec/index.js +35 -0
- package/dist/utils/graphspec/index.js.map +1 -0
- package/dist/utils/harness/index.cjs +656 -0
- package/dist/utils/harness/index.cjs.map +1 -0
- package/dist/utils/harness/index.d.cts +542 -0
- package/dist/utils/harness/index.d.ts +542 -0
- package/dist/utils/harness/index.js +56 -0
- package/dist/utils/harness/index.js.map +1 -0
- package/dist/utils/index.cjs +17614 -0
- package/dist/utils/index.cjs.map +1 -0
- package/dist/utils/index.d.cts +96 -0
- package/dist/utils/index.d.ts +96 -0
- package/dist/utils/index.js +514 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/inspect/index.cjs +807 -0
- package/dist/utils/inspect/index.cjs.map +1 -0
- package/dist/utils/inspect/index.d.cts +123 -0
- package/dist/utils/inspect/index.d.ts +123 -0
- package/dist/utils/inspect/index.js +30 -0
- package/dist/utils/inspect/index.js.map +1 -0
- package/dist/utils/job-queue/index.cjs +717 -0
- package/dist/utils/job-queue/index.cjs.map +1 -0
- package/dist/utils/job-queue/index.d.cts +200 -0
- package/dist/utils/job-queue/index.d.ts +200 -0
- package/dist/utils/job-queue/index.js +18 -0
- package/dist/utils/job-queue/index.js.map +1 -0
- package/dist/utils/memory/index.cjs +1456 -0
- package/dist/utils/memory/index.cjs.map +1 -0
- package/dist/utils/memory/index.d.cts +660 -0
- package/dist/utils/memory/index.d.ts +660 -0
- package/dist/utils/memory/index.js +19 -0
- package/dist/utils/memory/index.js.map +1 -0
- package/dist/utils/messaging/index.cjs +666 -0
- package/dist/utils/messaging/index.cjs.map +1 -0
- package/dist/utils/messaging/index.d.cts +562 -0
- package/dist/utils/messaging/index.d.ts +562 -0
- package/dist/utils/messaging/index.js +50 -0
- package/dist/utils/messaging/index.js.map +1 -0
- package/dist/utils/orchestration/index.cjs +876 -0
- package/dist/utils/orchestration/index.cjs.map +1 -0
- package/dist/utils/orchestration/index.d.cts +233 -0
- package/dist/utils/orchestration/index.d.ts +233 -0
- package/dist/utils/orchestration/index.js +19 -0
- package/dist/utils/orchestration/index.js.map +1 -0
- package/dist/utils/process/index.cjs +743 -0
- package/dist/utils/process/index.cjs.map +1 -0
- package/dist/utils/process/index.d.cts +411 -0
- package/dist/utils/process/index.d.ts +411 -0
- package/dist/utils/process/index.js +14 -0
- package/dist/utils/process/index.js.map +1 -0
- package/dist/utils/reactive-layout/index.cjs +1607 -0
- package/dist/utils/reactive-layout/index.cjs.map +1 -0
- package/dist/utils/reactive-layout/index.d.cts +492 -0
- package/dist/utils/reactive-layout/index.d.ts +492 -0
- package/dist/utils/reactive-layout/index.js +52 -0
- package/dist/utils/reactive-layout/index.js.map +1 -0
- package/dist/utils/reduction/index.cjs +203 -0
- package/dist/utils/reduction/index.cjs.map +1 -0
- package/dist/utils/reduction/index.d.cts +102 -0
- package/dist/utils/reduction/index.d.ts +102 -0
- package/dist/utils/reduction/index.js +14 -0
- package/dist/utils/reduction/index.js.map +1 -0
- package/dist/utils/resilience/index.cjs +1617 -0
- package/dist/utils/resilience/index.cjs.map +1 -0
- package/dist/utils/resilience/index.d.cts +9 -0
- package/dist/utils/resilience/index.d.ts +9 -0
- package/dist/utils/resilience/index.js +44 -0
- package/dist/utils/resilience/index.js.map +1 -0
- package/dist/utils/surface/index.cjs +1070 -0
- package/dist/utils/surface/index.cjs.map +1 -0
- package/dist/utils/surface/index.d.cts +240 -0
- package/dist/utils/surface/index.d.ts +240 -0
- package/dist/utils/surface/index.js +30 -0
- package/dist/utils/surface/index.js.map +1 -0
- package/dist/utils/topology-view/index.cjs +620 -0
- package/dist/utils/topology-view/index.cjs.map +1 -0
- package/dist/utils/topology-view/index.d.cts +68 -0
- package/dist/utils/topology-view/index.d.ts +68 -0
- package/dist/utils/topology-view/index.js +11 -0
- package/dist/utils/topology-view/index.js.map +1 -0
- package/package.json +293 -241
- package/dist/core/index.cjs +0 -21
- package/dist/core/index.cjs.map +0 -1
- package/dist/core/index.d.cts +0 -1
- package/dist/core/index.d.ts +0 -1
- package/dist/core/index.js +0 -3
- package/dist/core/index.js.map +0 -1
- package/dist/extra/browser.cjs +0 -21
- package/dist/extra/browser.cjs.map +0 -1
- package/dist/extra/browser.d.cts +0 -1
- package/dist/extra/browser.d.ts +0 -1
- package/dist/extra/browser.js +0 -3
- package/dist/extra/browser.js.map +0 -1
- package/dist/extra/index.cjs +0 -21
- package/dist/extra/index.cjs.map +0 -1
- package/dist/extra/index.d.cts +0 -1
- package/dist/extra/index.d.ts +0 -1
- package/dist/extra/index.js +0 -3
- package/dist/extra/index.js.map +0 -1
- package/dist/extra/node.cjs +0 -21
- package/dist/extra/node.cjs.map +0 -1
- package/dist/extra/node.d.cts +0 -1
- package/dist/extra/node.d.ts +0 -1
- package/dist/extra/node.js +0 -3
- package/dist/extra/node.js.map +0 -1
- package/dist/extra/operators.cjs +0 -21
- package/dist/extra/operators.cjs.map +0 -1
- package/dist/extra/operators.d.cts +0 -1
- package/dist/extra/operators.d.ts +0 -1
- package/dist/extra/operators.js +0 -3
- package/dist/extra/operators.js.map +0 -1
- package/dist/extra/reactive.cjs +0 -21
- package/dist/extra/reactive.cjs.map +0 -1
- package/dist/extra/reactive.d.cts +0 -1
- package/dist/extra/reactive.d.ts +0 -1
- package/dist/extra/reactive.js +0 -3
- package/dist/extra/reactive.js.map +0 -1
- package/dist/extra/render/index.cjs +0 -21
- package/dist/extra/render/index.cjs.map +0 -1
- package/dist/extra/render/index.d.cts +0 -1
- package/dist/extra/render/index.d.ts +0 -1
- package/dist/extra/render/index.js +0 -3
- package/dist/extra/render/index.js.map +0 -1
- package/dist/extra/sources.cjs +0 -21
- package/dist/extra/sources.cjs.map +0 -1
- package/dist/extra/sources.d.cts +0 -1
- package/dist/extra/sources.d.ts +0 -1
- package/dist/extra/sources.js +0 -3
- package/dist/extra/sources.js.map +0 -1
- package/dist/extra/storage-browser.cjs +0 -21
- package/dist/extra/storage-browser.cjs.map +0 -1
- package/dist/extra/storage-browser.d.cts +0 -1
- package/dist/extra/storage-browser.d.ts +0 -1
- package/dist/extra/storage-browser.js +0 -3
- package/dist/extra/storage-browser.js.map +0 -1
- package/dist/extra/storage-core.cjs +0 -21
- package/dist/extra/storage-core.cjs.map +0 -1
- package/dist/extra/storage-core.d.cts +0 -1
- package/dist/extra/storage-core.d.ts +0 -1
- package/dist/extra/storage-core.js +0 -3
- package/dist/extra/storage-core.js.map +0 -1
- package/dist/extra/storage-node.cjs +0 -21
- package/dist/extra/storage-node.cjs.map +0 -1
- package/dist/extra/storage-node.d.cts +0 -1
- package/dist/extra/storage-node.d.ts +0 -1
- package/dist/extra/storage-node.js +0 -3
- package/dist/extra/storage-node.js.map +0 -1
- package/dist/extra/storage-tiers-browser.cjs +0 -21
- package/dist/extra/storage-tiers-browser.cjs.map +0 -1
- package/dist/extra/storage-tiers-browser.d.cts +0 -1
- package/dist/extra/storage-tiers-browser.d.ts +0 -1
- package/dist/extra/storage-tiers-browser.js +0 -3
- package/dist/extra/storage-tiers-browser.js.map +0 -1
- package/dist/extra/storage-tiers-node.cjs +0 -21
- package/dist/extra/storage-tiers-node.cjs.map +0 -1
- package/dist/extra/storage-tiers-node.d.cts +0 -1
- package/dist/extra/storage-tiers-node.d.ts +0 -1
- package/dist/extra/storage-tiers-node.js +0 -3
- package/dist/extra/storage-tiers-node.js.map +0 -1
- package/dist/extra/storage-tiers.cjs +0 -21
- package/dist/extra/storage-tiers.cjs.map +0 -1
- package/dist/extra/storage-tiers.d.cts +0 -1
- package/dist/extra/storage-tiers.d.ts +0 -1
- package/dist/extra/storage-tiers.js +0 -3
- package/dist/extra/storage-tiers.js.map +0 -1
- package/dist/extra/storage-wal.cjs +0 -21
- package/dist/extra/storage-wal.cjs.map +0 -1
- package/dist/extra/storage-wal.d.cts +0 -1
- package/dist/extra/storage-wal.d.ts +0 -1
- package/dist/extra/storage-wal.js +0 -3
- package/dist/extra/storage-wal.js.map +0 -1
- package/dist/graph/index.cjs +0 -21
- package/dist/graph/index.cjs.map +0 -1
- package/dist/graph/index.d.cts +0 -1
- package/dist/graph/index.d.ts +0 -1
- package/dist/graph/index.js +0 -3
- package/dist/graph/index.js.map +0 -1
- package/dist/patterns/ai/browser.cjs +0 -21
- package/dist/patterns/ai/browser.cjs.map +0 -1
- package/dist/patterns/ai/browser.d.cts +0 -1
- package/dist/patterns/ai/browser.d.ts +0 -1
- package/dist/patterns/ai/browser.js +0 -3
- package/dist/patterns/ai/browser.js.map +0 -1
- package/dist/patterns/ai/index.cjs +0 -21
- package/dist/patterns/ai/index.cjs.map +0 -1
- package/dist/patterns/ai/index.d.cts +0 -1
- package/dist/patterns/ai/index.d.ts +0 -1
- package/dist/patterns/ai/index.js +0 -3
- package/dist/patterns/ai/index.js.map +0 -1
- package/dist/patterns/ai/node.cjs +0 -21
- package/dist/patterns/ai/node.cjs.map +0 -1
- package/dist/patterns/ai/node.d.cts +0 -1
- package/dist/patterns/ai/node.d.ts +0 -1
- package/dist/patterns/ai/node.js +0 -3
- package/dist/patterns/ai/node.js.map +0 -1
- package/dist/patterns/cqrs/index.cjs +0 -21
- package/dist/patterns/cqrs/index.cjs.map +0 -1
- package/dist/patterns/cqrs/index.d.cts +0 -1
- package/dist/patterns/cqrs/index.d.ts +0 -1
- package/dist/patterns/cqrs/index.js +0 -3
- package/dist/patterns/cqrs/index.js.map +0 -1
- package/dist/patterns/demo-shell/index.cjs +0 -21
- package/dist/patterns/demo-shell/index.cjs.map +0 -1
- package/dist/patterns/demo-shell/index.d.cts +0 -1
- package/dist/patterns/demo-shell/index.d.ts +0 -1
- package/dist/patterns/demo-shell/index.js +0 -3
- package/dist/patterns/demo-shell/index.js.map +0 -1
- package/dist/patterns/domain-templates/index.cjs +0 -21
- package/dist/patterns/domain-templates/index.cjs.map +0 -1
- package/dist/patterns/domain-templates/index.d.cts +0 -1
- package/dist/patterns/domain-templates/index.d.ts +0 -1
- package/dist/patterns/domain-templates/index.js +0 -3
- package/dist/patterns/domain-templates/index.js.map +0 -1
- package/dist/patterns/graphspec/index.cjs +0 -21
- package/dist/patterns/graphspec/index.cjs.map +0 -1
- package/dist/patterns/graphspec/index.d.cts +0 -1
- package/dist/patterns/graphspec/index.d.ts +0 -1
- package/dist/patterns/graphspec/index.js +0 -3
- package/dist/patterns/graphspec/index.js.map +0 -1
- package/dist/patterns/harness/index.cjs +0 -21
- package/dist/patterns/harness/index.cjs.map +0 -1
- package/dist/patterns/harness/index.d.cts +0 -1
- package/dist/patterns/harness/index.d.ts +0 -1
- package/dist/patterns/harness/index.js +0 -3
- package/dist/patterns/harness/index.js.map +0 -1
- package/dist/patterns/inspect/index.cjs +0 -21
- package/dist/patterns/inspect/index.cjs.map +0 -1
- package/dist/patterns/inspect/index.d.cts +0 -1
- package/dist/patterns/inspect/index.d.ts +0 -1
- package/dist/patterns/inspect/index.js +0 -3
- package/dist/patterns/inspect/index.js.map +0 -1
- package/dist/patterns/job-queue/index.cjs +0 -21
- package/dist/patterns/job-queue/index.cjs.map +0 -1
- package/dist/patterns/job-queue/index.d.cts +0 -1
- package/dist/patterns/job-queue/index.d.ts +0 -1
- package/dist/patterns/job-queue/index.js +0 -3
- package/dist/patterns/job-queue/index.js.map +0 -1
- package/dist/patterns/memory/index.cjs +0 -21
- package/dist/patterns/memory/index.cjs.map +0 -1
- package/dist/patterns/memory/index.d.cts +0 -1
- package/dist/patterns/memory/index.d.ts +0 -1
- package/dist/patterns/memory/index.js +0 -3
- package/dist/patterns/memory/index.js.map +0 -1
- package/dist/patterns/messaging/index.cjs +0 -21
- package/dist/patterns/messaging/index.cjs.map +0 -1
- package/dist/patterns/messaging/index.d.cts +0 -1
- package/dist/patterns/messaging/index.d.ts +0 -1
- package/dist/patterns/messaging/index.js +0 -3
- package/dist/patterns/messaging/index.js.map +0 -1
- package/dist/patterns/orchestration/index.cjs +0 -21
- package/dist/patterns/orchestration/index.cjs.map +0 -1
- package/dist/patterns/orchestration/index.d.cts +0 -1
- package/dist/patterns/orchestration/index.d.ts +0 -1
- package/dist/patterns/orchestration/index.js +0 -3
- package/dist/patterns/orchestration/index.js.map +0 -1
- package/dist/patterns/process/index.cjs +0 -21
- package/dist/patterns/process/index.cjs.map +0 -1
- package/dist/patterns/process/index.d.cts +0 -1
- package/dist/patterns/process/index.d.ts +0 -1
- package/dist/patterns/process/index.js +0 -3
- package/dist/patterns/process/index.js.map +0 -1
- package/dist/patterns/reactive-layout/index.cjs +0 -21
- package/dist/patterns/reactive-layout/index.cjs.map +0 -1
- package/dist/patterns/reactive-layout/index.d.cts +0 -1
- package/dist/patterns/reactive-layout/index.d.ts +0 -1
- package/dist/patterns/reactive-layout/index.js +0 -3
- package/dist/patterns/reactive-layout/index.js.map +0 -1
- package/dist/patterns/reduction/index.cjs +0 -21
- package/dist/patterns/reduction/index.cjs.map +0 -1
- package/dist/patterns/reduction/index.d.cts +0 -1
- package/dist/patterns/reduction/index.d.ts +0 -1
- package/dist/patterns/reduction/index.js +0 -3
- package/dist/patterns/reduction/index.js.map +0 -1
- package/dist/patterns/surface/index.cjs +0 -21
- package/dist/patterns/surface/index.cjs.map +0 -1
- package/dist/patterns/surface/index.d.cts +0 -1
- package/dist/patterns/surface/index.d.ts +0 -1
- package/dist/patterns/surface/index.js +0 -3
- package/dist/patterns/surface/index.js.map +0 -1
- package/dist/patterns/topology-view/index.cjs +0 -21
- package/dist/patterns/topology-view/index.cjs.map +0 -1
- package/dist/patterns/topology-view/index.d.cts +0 -1
- package/dist/patterns/topology-view/index.d.ts +0 -1
- package/dist/patterns/topology-view/index.js +0 -3
- package/dist/patterns/topology-view/index.js.map +0 -1
- package/dist/testing/index.cjs +0 -21
- package/dist/testing/index.cjs.map +0 -1
- package/dist/testing/index.d.cts +0 -1
- package/dist/testing/index.d.ts +0 -1
- package/dist/testing/index.js +0 -3
- package/dist/testing/index.js.map +0 -1
|
@@ -0,0 +1,1085 @@
|
|
|
1
|
+
import {
|
|
2
|
+
circuitBreaker
|
|
3
|
+
} from "./chunk-RJOG4IJU.js";
|
|
4
|
+
import {
|
|
5
|
+
dryRunAdapter,
|
|
6
|
+
fallbackAdapter,
|
|
7
|
+
withLayer
|
|
8
|
+
} from "./chunk-CXANAIZU.js";
|
|
9
|
+
import {
|
|
10
|
+
firstValueFrom
|
|
11
|
+
} from "./chunk-O3MT7DYI.js";
|
|
12
|
+
import {
|
|
13
|
+
parseSSEStream
|
|
14
|
+
} from "./chunk-SOOKUYVM.js";
|
|
15
|
+
|
|
16
|
+
// src/utils/ai/adapters/providers/anthropic.ts
|
|
17
|
+
import { monotonicNs } from "@graphrefly/pure-ts/core";
|
|
18
|
+
|
|
19
|
+
// src/base/io/http-error.ts
|
|
20
|
+
async function makeHttpError(resp, provider) {
|
|
21
|
+
let body;
|
|
22
|
+
try {
|
|
23
|
+
body = await resp.text();
|
|
24
|
+
} catch {
|
|
25
|
+
body = "";
|
|
26
|
+
}
|
|
27
|
+
const prefix = provider ?? "HTTP";
|
|
28
|
+
const err = new Error(
|
|
29
|
+
`${prefix} API ${resp.status}: ${resp.statusText}${body ? ` \u2014 ${body}` : ""}`
|
|
30
|
+
);
|
|
31
|
+
err.status = resp.status;
|
|
32
|
+
err.headers = resp.headers;
|
|
33
|
+
return err;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
// src/utils/ai/adapters/providers/anthropic.ts
|
|
37
|
+
function anthropicAdapter(opts = {}) {
|
|
38
|
+
if (opts.sdk) return sdkBackedAnthropic(opts);
|
|
39
|
+
return fetchBackedAnthropic(opts);
|
|
40
|
+
}
|
|
41
|
+
function toAnthropicRequest(messages, invokeOpts, defaultModel, stream) {
|
|
42
|
+
const model = invokeOpts?.model ?? defaultModel;
|
|
43
|
+
if (!model)
|
|
44
|
+
throw new Error("anthropicAdapter: model must be set via options.model or invokeOpts.model");
|
|
45
|
+
const { system, chat } = partitionSystem(messages, invokeOpts?.systemPrompt);
|
|
46
|
+
const body = {
|
|
47
|
+
model,
|
|
48
|
+
messages: chat.map(toAnthropicMessage),
|
|
49
|
+
max_tokens: invokeOpts?.maxTokens ?? 4096
|
|
50
|
+
};
|
|
51
|
+
if (system) body.system = system;
|
|
52
|
+
if (invokeOpts?.temperature != null) body.temperature = invokeOpts.temperature;
|
|
53
|
+
if (invokeOpts?.tools && invokeOpts.tools.length > 0)
|
|
54
|
+
body.tools = invokeOpts.tools.map(toAnthropicTool);
|
|
55
|
+
if (invokeOpts?.maxReasoningTokens != null) {
|
|
56
|
+
body.thinking = { type: "enabled", budget_tokens: invokeOpts.maxReasoningTokens };
|
|
57
|
+
}
|
|
58
|
+
if (invokeOpts?.cacheHint) {
|
|
59
|
+
}
|
|
60
|
+
if (stream) body.stream = true;
|
|
61
|
+
if (invokeOpts?.providerExtras) Object.assign(body, invokeOpts.providerExtras);
|
|
62
|
+
return body;
|
|
63
|
+
}
|
|
64
|
+
function partitionSystem(messages, systemPrompt) {
|
|
65
|
+
const systemParts = [];
|
|
66
|
+
if (systemPrompt) systemParts.push(systemPrompt);
|
|
67
|
+
const chat = [];
|
|
68
|
+
for (const m of messages) {
|
|
69
|
+
if (m.role === "system") systemParts.push(m.content);
|
|
70
|
+
else chat.push(m);
|
|
71
|
+
}
|
|
72
|
+
return { system: systemParts.length > 0 ? systemParts.join("\n\n") : void 0, chat };
|
|
73
|
+
}
|
|
74
|
+
function toAnthropicMessage(m) {
|
|
75
|
+
if (m.role === "tool") {
|
|
76
|
+
return {
|
|
77
|
+
role: "user",
|
|
78
|
+
content: [
|
|
79
|
+
{
|
|
80
|
+
type: "tool_result",
|
|
81
|
+
tool_use_id: m.toolCallId,
|
|
82
|
+
content: m.content
|
|
83
|
+
}
|
|
84
|
+
]
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (m.role === "assistant" && m.toolCalls && m.toolCalls.length > 0) {
|
|
88
|
+
const blocks = [];
|
|
89
|
+
if (m.content) blocks.push({ type: "text", text: m.content });
|
|
90
|
+
for (const tc of m.toolCalls) {
|
|
91
|
+
blocks.push({ type: "tool_use", id: tc.id, name: tc.name, input: tc.arguments });
|
|
92
|
+
}
|
|
93
|
+
return { role: "assistant", content: blocks };
|
|
94
|
+
}
|
|
95
|
+
return { role: m.role, content: m.content };
|
|
96
|
+
}
|
|
97
|
+
function toAnthropicTool(t) {
|
|
98
|
+
return {
|
|
99
|
+
name: t.name,
|
|
100
|
+
description: t.description,
|
|
101
|
+
input_schema: t.parameters
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
function mergeAnthropicUsage(prev, next) {
|
|
105
|
+
if (!prev) return { ...next };
|
|
106
|
+
const merged = { ...prev };
|
|
107
|
+
if (next.input_tokens != null) merged.input_tokens = next.input_tokens;
|
|
108
|
+
if (next.output_tokens != null) merged.output_tokens = next.output_tokens;
|
|
109
|
+
if (next.cache_read_input_tokens != null)
|
|
110
|
+
merged.cache_read_input_tokens = next.cache_read_input_tokens;
|
|
111
|
+
if (next.cache_creation_input_tokens != null)
|
|
112
|
+
merged.cache_creation_input_tokens = next.cache_creation_input_tokens;
|
|
113
|
+
if (next.cache_creation) {
|
|
114
|
+
merged.cache_creation = {
|
|
115
|
+
...prev.cache_creation ?? {},
|
|
116
|
+
...next.cache_creation
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
if (next.server_tool_use) {
|
|
120
|
+
merged.server_tool_use = {
|
|
121
|
+
...prev.server_tool_use ?? {},
|
|
122
|
+
...next.server_tool_use
|
|
123
|
+
};
|
|
124
|
+
}
|
|
125
|
+
return merged;
|
|
126
|
+
}
|
|
127
|
+
function mapUsage(u) {
|
|
128
|
+
const usage = {
|
|
129
|
+
input: { regular: u?.input_tokens ?? 0 },
|
|
130
|
+
output: { regular: u?.output_tokens ?? 0 },
|
|
131
|
+
raw: u
|
|
132
|
+
};
|
|
133
|
+
if (u?.cache_read_input_tokens) usage.input.cacheRead = u.cache_read_input_tokens;
|
|
134
|
+
if (u?.cache_creation) {
|
|
135
|
+
if (u.cache_creation.ephemeral_5m_input_tokens)
|
|
136
|
+
usage.input.cacheWrite5m = u.cache_creation.ephemeral_5m_input_tokens;
|
|
137
|
+
if (u.cache_creation.ephemeral_1h_input_tokens)
|
|
138
|
+
usage.input.cacheWrite1h = u.cache_creation.ephemeral_1h_input_tokens;
|
|
139
|
+
} else if (u?.cache_creation_input_tokens) {
|
|
140
|
+
usage.input.cacheWrite5m = u.cache_creation_input_tokens;
|
|
141
|
+
}
|
|
142
|
+
if (u?.server_tool_use?.web_search_requests) {
|
|
143
|
+
usage.auxiliary = { webSearchRequests: u.server_tool_use.web_search_requests };
|
|
144
|
+
}
|
|
145
|
+
return usage;
|
|
146
|
+
}
|
|
147
|
+
function toLLMResponse(msg, latencyMs) {
|
|
148
|
+
const textParts = [];
|
|
149
|
+
const toolCalls = [];
|
|
150
|
+
for (const block of msg.content) {
|
|
151
|
+
if (block.type === "text" && typeof block.text === "string") {
|
|
152
|
+
textParts.push(block.text);
|
|
153
|
+
} else if (block.type === "tool_use") {
|
|
154
|
+
const tb = block;
|
|
155
|
+
toolCalls.push({ id: tb.id, name: tb.name, arguments: tb.input ?? {} });
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
return {
|
|
159
|
+
content: textParts.join(""),
|
|
160
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
161
|
+
usage: mapUsage(msg.usage),
|
|
162
|
+
finishReason: msg.stop_reason,
|
|
163
|
+
latencyMs,
|
|
164
|
+
model: msg.model,
|
|
165
|
+
provider: "anthropic"
|
|
166
|
+
};
|
|
167
|
+
}
|
|
168
|
+
function fetchBackedAnthropic(opts) {
|
|
169
|
+
const apiKey = opts.apiKey ?? globalThis.process?.env?.ANTHROPIC_API_KEY;
|
|
170
|
+
if (!apiKey) {
|
|
171
|
+
}
|
|
172
|
+
const baseURL = opts.baseURL ?? "https://api.anthropic.com";
|
|
173
|
+
const anthropicVersion = opts.anthropicVersion ?? "2023-06-01";
|
|
174
|
+
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
175
|
+
const commonHeaders = () => {
|
|
176
|
+
if (!apiKey)
|
|
177
|
+
throw new Error("anthropicAdapter: apiKey required for invoke/stream (or provide opts.sdk)");
|
|
178
|
+
return {
|
|
179
|
+
"x-api-key": apiKey,
|
|
180
|
+
"anthropic-version": anthropicVersion,
|
|
181
|
+
"content-type": "application/json",
|
|
182
|
+
...opts.headers ?? {}
|
|
183
|
+
};
|
|
184
|
+
};
|
|
185
|
+
return {
|
|
186
|
+
provider: "anthropic",
|
|
187
|
+
model: opts.model,
|
|
188
|
+
async invoke(messages, invokeOpts) {
|
|
189
|
+
const body = toAnthropicRequest(messages, invokeOpts, opts.model, false);
|
|
190
|
+
const start = monotonicNs();
|
|
191
|
+
const resp = await fetchImpl(`${baseURL}/v1/messages`, {
|
|
192
|
+
method: "POST",
|
|
193
|
+
headers: commonHeaders(),
|
|
194
|
+
body: JSON.stringify(body),
|
|
195
|
+
signal: invokeOpts?.signal
|
|
196
|
+
});
|
|
197
|
+
if (!resp.ok) throw await makeHttpError(resp, "Anthropic");
|
|
198
|
+
const json = await resp.json();
|
|
199
|
+
const latencyMs = Math.max(0, (monotonicNs() - start) / 1e6);
|
|
200
|
+
return toLLMResponse(json, latencyMs);
|
|
201
|
+
},
|
|
202
|
+
async *stream(messages, invokeOpts) {
|
|
203
|
+
const body = toAnthropicRequest(messages, invokeOpts, opts.model, true);
|
|
204
|
+
const resp = await fetchImpl(`${baseURL}/v1/messages`, {
|
|
205
|
+
method: "POST",
|
|
206
|
+
headers: { ...commonHeaders(), accept: "text/event-stream" },
|
|
207
|
+
body: JSON.stringify(body),
|
|
208
|
+
signal: invokeOpts?.signal
|
|
209
|
+
});
|
|
210
|
+
if (!resp.ok) throw await makeHttpError(resp, "Anthropic");
|
|
211
|
+
if (!resp.body) throw new Error("anthropicAdapter: streaming response has no body");
|
|
212
|
+
let finalUsage;
|
|
213
|
+
let finishReason;
|
|
214
|
+
const toolCallsByIndex = /* @__PURE__ */ new Map();
|
|
215
|
+
for await (const event of parseSSEStream(resp.body, { signal: invokeOpts?.signal })) {
|
|
216
|
+
const data = event.data;
|
|
217
|
+
if (!data) continue;
|
|
218
|
+
let parsed;
|
|
219
|
+
try {
|
|
220
|
+
parsed = JSON.parse(data);
|
|
221
|
+
} catch {
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
switch (parsed.type) {
|
|
225
|
+
case "message_start": {
|
|
226
|
+
const ms = parsed;
|
|
227
|
+
finalUsage = mergeAnthropicUsage(finalUsage, ms.message.usage);
|
|
228
|
+
break;
|
|
229
|
+
}
|
|
230
|
+
case "content_block_start": {
|
|
231
|
+
const cb = parsed;
|
|
232
|
+
const b = cb.content_block;
|
|
233
|
+
if (b.type === "tool_use") {
|
|
234
|
+
toolCallsByIndex.set(cb.index, {
|
|
235
|
+
id: String(b.id ?? ""),
|
|
236
|
+
name: String(b.name ?? ""),
|
|
237
|
+
argBuf: ""
|
|
238
|
+
});
|
|
239
|
+
yield {
|
|
240
|
+
type: "tool-call-delta",
|
|
241
|
+
delta: { id: String(b.id ?? ""), name: String(b.name ?? "") }
|
|
242
|
+
};
|
|
243
|
+
}
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
case "content_block_delta": {
|
|
247
|
+
const cbd = parsed;
|
|
248
|
+
const d = cbd.delta;
|
|
249
|
+
if (d.type === "text_delta" && typeof d.text === "string") {
|
|
250
|
+
yield { type: "token", delta: d.text };
|
|
251
|
+
} else if (d.type === "input_json_delta" && typeof d.partial_json === "string") {
|
|
252
|
+
const existing = toolCallsByIndex.get(cbd.index);
|
|
253
|
+
if (existing) existing.argBuf += d.partial_json;
|
|
254
|
+
yield {
|
|
255
|
+
type: "tool-call-delta",
|
|
256
|
+
delta: { argumentsDelta: d.partial_json }
|
|
257
|
+
};
|
|
258
|
+
} else if (d.type === "thinking_delta" && typeof d.thinking === "string") {
|
|
259
|
+
yield { type: "thinking", delta: d.thinking };
|
|
260
|
+
}
|
|
261
|
+
break;
|
|
262
|
+
}
|
|
263
|
+
case "message_delta": {
|
|
264
|
+
const md = parsed;
|
|
265
|
+
if (md.delta.stop_reason) finishReason = md.delta.stop_reason;
|
|
266
|
+
if (md.delta.usage) {
|
|
267
|
+
finalUsage = mergeAnthropicUsage(finalUsage, md.delta.usage);
|
|
268
|
+
}
|
|
269
|
+
break;
|
|
270
|
+
}
|
|
271
|
+
case "message_stop":
|
|
272
|
+
break;
|
|
273
|
+
}
|
|
274
|
+
}
|
|
275
|
+
if (finalUsage) yield { type: "usage", usage: mapUsage(finalUsage) };
|
|
276
|
+
yield { type: "finish", reason: finishReason ?? "stop" };
|
|
277
|
+
}
|
|
278
|
+
};
|
|
279
|
+
}
|
|
280
|
+
function sdkBackedAnthropic(opts) {
|
|
281
|
+
const sdk = opts.sdk;
|
|
282
|
+
if (!sdk) throw new Error("sdkBackedAnthropic: sdk instance required");
|
|
283
|
+
return {
|
|
284
|
+
provider: "anthropic",
|
|
285
|
+
model: opts.model,
|
|
286
|
+
async invoke(messages, invokeOpts) {
|
|
287
|
+
const body = toAnthropicRequest(messages, invokeOpts, opts.model, false);
|
|
288
|
+
const start = monotonicNs();
|
|
289
|
+
const resp = await sdk.messages.create(body, {
|
|
290
|
+
signal: invokeOpts?.signal
|
|
291
|
+
});
|
|
292
|
+
const latencyMs = Math.max(0, (monotonicNs() - start) / 1e6);
|
|
293
|
+
return toLLMResponse(resp, latencyMs);
|
|
294
|
+
},
|
|
295
|
+
async *stream(messages, invokeOpts) {
|
|
296
|
+
if (!sdk.messages.stream) {
|
|
297
|
+
throw new Error("sdkBackedAnthropic: SDK instance does not expose .messages.stream");
|
|
298
|
+
}
|
|
299
|
+
const body = toAnthropicRequest(messages, invokeOpts, opts.model, true);
|
|
300
|
+
let finalUsage;
|
|
301
|
+
let finishReason;
|
|
302
|
+
for await (const event of sdk.messages.stream(body, { signal: invokeOpts?.signal })) {
|
|
303
|
+
switch (event.type) {
|
|
304
|
+
case "message_start":
|
|
305
|
+
finalUsage = mergeAnthropicUsage(
|
|
306
|
+
finalUsage,
|
|
307
|
+
event.message.usage
|
|
308
|
+
);
|
|
309
|
+
break;
|
|
310
|
+
case "content_block_delta": {
|
|
311
|
+
const d = event.delta;
|
|
312
|
+
if (d?.type === "text_delta" && typeof d.text === "string") {
|
|
313
|
+
yield { type: "token", delta: d.text };
|
|
314
|
+
} else if (d?.type === "input_json_delta" && typeof d.partial_json === "string") {
|
|
315
|
+
yield { type: "tool-call-delta", delta: { argumentsDelta: d.partial_json } };
|
|
316
|
+
} else if (d?.type === "thinking_delta" && typeof d.thinking === "string") {
|
|
317
|
+
yield { type: "thinking", delta: d.thinking };
|
|
318
|
+
}
|
|
319
|
+
break;
|
|
320
|
+
}
|
|
321
|
+
case "message_delta": {
|
|
322
|
+
const md = event;
|
|
323
|
+
if (md.delta.stop_reason) finishReason = md.delta.stop_reason;
|
|
324
|
+
if (md.delta.usage) finalUsage = mergeAnthropicUsage(finalUsage, md.delta.usage);
|
|
325
|
+
break;
|
|
326
|
+
}
|
|
327
|
+
}
|
|
328
|
+
}
|
|
329
|
+
if (finalUsage) yield { type: "usage", usage: mapUsage(finalUsage) };
|
|
330
|
+
yield { type: "finish", reason: finishReason ?? "stop" };
|
|
331
|
+
}
|
|
332
|
+
};
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// src/utils/ai/adapters/providers/google.ts
|
|
336
|
+
import { monotonicNs as monotonicNs2 } from "@graphrefly/pure-ts/core";
|
|
337
|
+
function googleAdapter(opts = {}) {
|
|
338
|
+
if (opts.sdk) return sdkBackedGoogle(opts);
|
|
339
|
+
return fetchBackedGoogle(opts);
|
|
340
|
+
}
|
|
341
|
+
function toGeminiRequest(messages, invokeOpts) {
|
|
342
|
+
const systemParts = [];
|
|
343
|
+
const contents = [];
|
|
344
|
+
if (invokeOpts?.systemPrompt) systemParts.push(invokeOpts.systemPrompt);
|
|
345
|
+
for (const m of messages) {
|
|
346
|
+
if (m.role === "system") {
|
|
347
|
+
systemParts.push(m.content);
|
|
348
|
+
continue;
|
|
349
|
+
}
|
|
350
|
+
if (m.role === "tool") {
|
|
351
|
+
contents.push({
|
|
352
|
+
role: "user",
|
|
353
|
+
parts: [
|
|
354
|
+
{
|
|
355
|
+
functionResponse: {
|
|
356
|
+
name: m.name ?? m.toolCallId ?? "tool",
|
|
357
|
+
response: { result: m.content }
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
]
|
|
361
|
+
});
|
|
362
|
+
continue;
|
|
363
|
+
}
|
|
364
|
+
if (m.role === "assistant" && m.toolCalls && m.toolCalls.length > 0) {
|
|
365
|
+
const parts = [];
|
|
366
|
+
if (m.content) parts.push({ text: m.content });
|
|
367
|
+
for (const tc of m.toolCalls) {
|
|
368
|
+
parts.push({ functionCall: { name: tc.name, args: tc.arguments } });
|
|
369
|
+
}
|
|
370
|
+
contents.push({ role: "model", parts });
|
|
371
|
+
continue;
|
|
372
|
+
}
|
|
373
|
+
contents.push({
|
|
374
|
+
role: m.role === "assistant" ? "model" : "user",
|
|
375
|
+
parts: [{ text: m.content }]
|
|
376
|
+
});
|
|
377
|
+
}
|
|
378
|
+
const body = { contents };
|
|
379
|
+
if (systemParts.length > 0) {
|
|
380
|
+
body.systemInstruction = { role: "system", parts: [{ text: systemParts.join("\n\n") }] };
|
|
381
|
+
}
|
|
382
|
+
const genConfig = {};
|
|
383
|
+
if (invokeOpts?.maxTokens != null) genConfig.maxOutputTokens = invokeOpts.maxTokens;
|
|
384
|
+
if (invokeOpts?.temperature != null) genConfig.temperature = invokeOpts.temperature;
|
|
385
|
+
if (invokeOpts?.maxReasoningTokens != null) {
|
|
386
|
+
genConfig.thinkingConfig = { thinkingBudget: invokeOpts.maxReasoningTokens };
|
|
387
|
+
}
|
|
388
|
+
if (Object.keys(genConfig).length > 0) body.generationConfig = genConfig;
|
|
389
|
+
if (invokeOpts?.tools && invokeOpts.tools.length > 0) {
|
|
390
|
+
body.tools = [{ functionDeclarations: invokeOpts.tools.map(toGeminiTool) }];
|
|
391
|
+
}
|
|
392
|
+
if (invokeOpts?.providerExtras) Object.assign(body, invokeOpts.providerExtras);
|
|
393
|
+
return body;
|
|
394
|
+
}
|
|
395
|
+
function toGeminiTool(t) {
|
|
396
|
+
return {
|
|
397
|
+
name: t.name,
|
|
398
|
+
description: t.description,
|
|
399
|
+
parameters: t.parameters
|
|
400
|
+
};
|
|
401
|
+
}
|
|
402
|
+
function toSdkParams(body, model, signal) {
|
|
403
|
+
const config = {};
|
|
404
|
+
const gen = body.generationConfig;
|
|
405
|
+
if (gen) {
|
|
406
|
+
if (typeof gen.maxOutputTokens === "number") config.maxOutputTokens = gen.maxOutputTokens;
|
|
407
|
+
if (typeof gen.temperature === "number") config.temperature = gen.temperature;
|
|
408
|
+
if (gen.thinkingConfig) config.thinkingConfig = gen.thinkingConfig;
|
|
409
|
+
}
|
|
410
|
+
if (body.systemInstruction) config.systemInstruction = body.systemInstruction;
|
|
411
|
+
if (body.tools) config.tools = body.tools;
|
|
412
|
+
for (const [k, v] of Object.entries(body)) {
|
|
413
|
+
if (k === "contents" || k === "generationConfig" || k === "systemInstruction" || k === "tools" || // Skip `abortSignal` so a caller-supplied entry in `providerExtras`
|
|
414
|
+
// can't briefly land in `config.abortSignal` before being
|
|
415
|
+
// overwritten — the canonical signal channel is the second
|
|
416
|
+
// `signal` argument (set on `config.abortSignal` below).
|
|
417
|
+
k === "abortSignal" || // Skip `model` so a stray `body.model` (not produced by
|
|
418
|
+
// `toGeminiRequest` today, but defensively guarded) doesn't
|
|
419
|
+
// duplicate the top-level `model` field on the SDK request.
|
|
420
|
+
k === "model") {
|
|
421
|
+
continue;
|
|
422
|
+
}
|
|
423
|
+
config[k] = v;
|
|
424
|
+
}
|
|
425
|
+
if (signal) config.abortSignal = signal;
|
|
426
|
+
return {
|
|
427
|
+
model,
|
|
428
|
+
contents: body.contents,
|
|
429
|
+
config: Object.keys(config).length > 0 ? config : void 0
|
|
430
|
+
};
|
|
431
|
+
}
|
|
432
|
+
function mapUsage2(u) {
|
|
433
|
+
const usage = {
|
|
434
|
+
input: { regular: 0 },
|
|
435
|
+
output: { regular: 0 },
|
|
436
|
+
raw: u
|
|
437
|
+
};
|
|
438
|
+
if (!u) return usage;
|
|
439
|
+
const promptTotal = u.promptTokenCount ?? 0;
|
|
440
|
+
const cached = u.cachedContentTokenCount ?? 0;
|
|
441
|
+
usage.input.regular = Math.max(0, promptTotal - cached);
|
|
442
|
+
if (cached > 0) usage.input.cacheRead = cached;
|
|
443
|
+
if (u.toolUsePromptTokenCount) usage.input.toolUse = u.toolUsePromptTokenCount;
|
|
444
|
+
if (u.promptTokensDetails) {
|
|
445
|
+
for (const d of u.promptTokensDetails) {
|
|
446
|
+
const modality = d.modality?.toLowerCase();
|
|
447
|
+
if (modality === "image") usage.input.image = (usage.input.image ?? 0) + d.tokenCount;
|
|
448
|
+
else if (modality === "audio") usage.input.audio = (usage.input.audio ?? 0) + d.tokenCount;
|
|
449
|
+
else if (modality === "video") usage.input.video = (usage.input.video ?? 0) + d.tokenCount;
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
usage.output.regular = u.candidatesTokenCount ?? 0;
|
|
453
|
+
if (u.thoughtsTokenCount) usage.output.reasoning = u.thoughtsTokenCount;
|
|
454
|
+
return usage;
|
|
455
|
+
}
|
|
456
|
+
function toLLMResponse2(json, latencyMs) {
|
|
457
|
+
const cand = json.candidates?.[0];
|
|
458
|
+
const parts = cand?.content?.parts ?? [];
|
|
459
|
+
const textParts = [];
|
|
460
|
+
const toolCalls = [];
|
|
461
|
+
let i = 0;
|
|
462
|
+
for (const p of parts) {
|
|
463
|
+
if (typeof p.text === "string") textParts.push(p.text);
|
|
464
|
+
if (p.functionCall) {
|
|
465
|
+
toolCalls.push({
|
|
466
|
+
id: `${p.functionCall.name}-${i++}`,
|
|
467
|
+
name: p.functionCall.name,
|
|
468
|
+
arguments: p.functionCall.args ?? {}
|
|
469
|
+
});
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return {
|
|
473
|
+
content: textParts.join(""),
|
|
474
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
475
|
+
usage: mapUsage2(json.usageMetadata),
|
|
476
|
+
finishReason: cand?.finishReason,
|
|
477
|
+
latencyMs,
|
|
478
|
+
model: json.modelVersion,
|
|
479
|
+
provider: "google"
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
function resolveApiKey(opts) {
|
|
483
|
+
if (opts.apiKey) return opts.apiKey;
|
|
484
|
+
const env = globalThis.process?.env;
|
|
485
|
+
return env?.GOOGLE_API_KEY ?? env?.GEMINI_API_KEY;
|
|
486
|
+
}
|
|
487
|
+
function fetchBackedGoogle(opts) {
|
|
488
|
+
const baseURL = opts.baseURL ?? "https://generativelanguage.googleapis.com/v1beta";
|
|
489
|
+
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
490
|
+
const pickModel = (invokeOpts) => {
|
|
491
|
+
const model = invokeOpts?.model ?? opts.model;
|
|
492
|
+
if (!model)
|
|
493
|
+
throw new Error("googleAdapter: model must be set via options.model or invokeOpts.model");
|
|
494
|
+
return model;
|
|
495
|
+
};
|
|
496
|
+
const keyParam = () => {
|
|
497
|
+
const key = resolveApiKey(opts);
|
|
498
|
+
if (!key) throw new Error("googleAdapter: apiKey required for invoke/stream");
|
|
499
|
+
return `key=${encodeURIComponent(key)}`;
|
|
500
|
+
};
|
|
501
|
+
return {
|
|
502
|
+
provider: "google",
|
|
503
|
+
model: opts.model,
|
|
504
|
+
async invoke(messages, invokeOpts) {
|
|
505
|
+
const model = pickModel(invokeOpts);
|
|
506
|
+
const body = toGeminiRequest(messages, invokeOpts);
|
|
507
|
+
const start = monotonicNs2();
|
|
508
|
+
const url = `${baseURL}/models/${encodeURIComponent(model)}:generateContent?${keyParam()}`;
|
|
509
|
+
const resp = await fetchImpl(url, {
|
|
510
|
+
method: "POST",
|
|
511
|
+
headers: { "content-type": "application/json", ...opts.headers ?? {} },
|
|
512
|
+
body: JSON.stringify(body),
|
|
513
|
+
signal: invokeOpts?.signal
|
|
514
|
+
});
|
|
515
|
+
if (!resp.ok) throw await makeHttpError(resp, "Google");
|
|
516
|
+
const json = await resp.json();
|
|
517
|
+
const latencyMs = Math.max(0, (monotonicNs2() - start) / 1e6);
|
|
518
|
+
return toLLMResponse2(json, latencyMs);
|
|
519
|
+
},
|
|
520
|
+
async *stream(messages, invokeOpts) {
|
|
521
|
+
const model = pickModel(invokeOpts);
|
|
522
|
+
const body = toGeminiRequest(messages, invokeOpts);
|
|
523
|
+
const url = `${baseURL}/models/${encodeURIComponent(model)}:streamGenerateContent?alt=sse&${keyParam()}`;
|
|
524
|
+
const resp = await fetchImpl(url, {
|
|
525
|
+
method: "POST",
|
|
526
|
+
headers: {
|
|
527
|
+
"content-type": "application/json",
|
|
528
|
+
accept: "text/event-stream",
|
|
529
|
+
...opts.headers ?? {}
|
|
530
|
+
},
|
|
531
|
+
body: JSON.stringify(body),
|
|
532
|
+
signal: invokeOpts?.signal
|
|
533
|
+
});
|
|
534
|
+
if (!resp.ok) throw await makeHttpError(resp, "Google");
|
|
535
|
+
if (!resp.body) throw new Error("googleAdapter: streaming response has no body");
|
|
536
|
+
let finalUsage;
|
|
537
|
+
let finishReason;
|
|
538
|
+
for await (const event of parseSSEStream(resp.body, { signal: invokeOpts?.signal })) {
|
|
539
|
+
if (!event.data) continue;
|
|
540
|
+
let parsed;
|
|
541
|
+
try {
|
|
542
|
+
parsed = JSON.parse(event.data);
|
|
543
|
+
} catch {
|
|
544
|
+
continue;
|
|
545
|
+
}
|
|
546
|
+
const cand = parsed.candidates?.[0];
|
|
547
|
+
for (const p of cand?.content?.parts ?? []) {
|
|
548
|
+
if (typeof p.text === "string") {
|
|
549
|
+
if (p.thought) yield { type: "thinking", delta: p.text };
|
|
550
|
+
else yield { type: "token", delta: p.text };
|
|
551
|
+
}
|
|
552
|
+
if (p.functionCall) {
|
|
553
|
+
yield {
|
|
554
|
+
type: "tool-call-delta",
|
|
555
|
+
delta: {
|
|
556
|
+
name: p.functionCall.name,
|
|
557
|
+
argumentsDelta: JSON.stringify(p.functionCall.args ?? {})
|
|
558
|
+
}
|
|
559
|
+
};
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
if (cand?.finishReason) finishReason = cand.finishReason;
|
|
563
|
+
if (parsed.usageMetadata) finalUsage = parsed.usageMetadata;
|
|
564
|
+
}
|
|
565
|
+
if (finalUsage) yield { type: "usage", usage: mapUsage2(finalUsage) };
|
|
566
|
+
yield { type: "finish", reason: finishReason ?? "stop" };
|
|
567
|
+
}
|
|
568
|
+
};
|
|
569
|
+
}
|
|
570
|
+
function sdkBackedGoogle(opts) {
|
|
571
|
+
const sdk = opts.sdk;
|
|
572
|
+
if (!sdk) throw new Error("sdkBackedGoogle: sdk instance required");
|
|
573
|
+
return {
|
|
574
|
+
provider: "google",
|
|
575
|
+
model: opts.model,
|
|
576
|
+
async invoke(messages, invokeOpts) {
|
|
577
|
+
const body = toGeminiRequest(messages, invokeOpts);
|
|
578
|
+
const model = invokeOpts?.model ?? opts.model;
|
|
579
|
+
if (!model) throw new Error("googleAdapter: model required");
|
|
580
|
+
const params = toSdkParams(body, model, invokeOpts?.signal);
|
|
581
|
+
const start = monotonicNs2();
|
|
582
|
+
const resp = await sdk.models.generateContent(params);
|
|
583
|
+
const latencyMs = Math.max(0, (monotonicNs2() - start) / 1e6);
|
|
584
|
+
return toLLMResponse2(resp, latencyMs);
|
|
585
|
+
},
|
|
586
|
+
async *stream(messages, invokeOpts) {
|
|
587
|
+
if (!sdk.models.generateContentStream) {
|
|
588
|
+
throw new Error("sdkBackedGoogle: SDK instance does not expose generateContentStream");
|
|
589
|
+
}
|
|
590
|
+
const body = toGeminiRequest(messages, invokeOpts);
|
|
591
|
+
const model = invokeOpts?.model ?? opts.model;
|
|
592
|
+
if (!model) throw new Error("googleAdapter: model required");
|
|
593
|
+
const params = toSdkParams(body, model, invokeOpts?.signal);
|
|
594
|
+
let finalUsage;
|
|
595
|
+
let finishReason;
|
|
596
|
+
const stream = await sdk.models.generateContentStream(params);
|
|
597
|
+
for await (const chunk of stream) {
|
|
598
|
+
const cand = chunk.candidates?.[0];
|
|
599
|
+
for (const p of cand?.content?.parts ?? []) {
|
|
600
|
+
if (typeof p.text === "string") {
|
|
601
|
+
if (p.thought) yield { type: "thinking", delta: p.text };
|
|
602
|
+
else yield { type: "token", delta: p.text };
|
|
603
|
+
}
|
|
604
|
+
if (p.functionCall) {
|
|
605
|
+
yield {
|
|
606
|
+
type: "tool-call-delta",
|
|
607
|
+
delta: {
|
|
608
|
+
name: p.functionCall.name,
|
|
609
|
+
argumentsDelta: JSON.stringify(p.functionCall.args ?? {})
|
|
610
|
+
}
|
|
611
|
+
};
|
|
612
|
+
}
|
|
613
|
+
}
|
|
614
|
+
if (cand?.finishReason) finishReason = cand.finishReason;
|
|
615
|
+
if (chunk.usageMetadata) finalUsage = chunk.usageMetadata;
|
|
616
|
+
}
|
|
617
|
+
if (finalUsage) yield { type: "usage", usage: mapUsage2(finalUsage) };
|
|
618
|
+
yield { type: "finish", reason: finishReason ?? "stop" };
|
|
619
|
+
}
|
|
620
|
+
};
|
|
621
|
+
}
|
|
622
|
+
|
|
623
|
+
// src/utils/ai/adapters/providers/openai-compat.ts
|
|
624
|
+
import { monotonicNs as monotonicNs3 } from "@graphrefly/pure-ts/core";
|
|
625
|
+
var PRESETS = {
|
|
626
|
+
openai: { baseURL: "https://api.openai.com/v1", apiKeyEnv: "OPENAI_API_KEY", provider: "openai" },
|
|
627
|
+
openrouter: {
|
|
628
|
+
baseURL: "https://openrouter.ai/api/v1",
|
|
629
|
+
apiKeyEnv: "OPENROUTER_API_KEY",
|
|
630
|
+
provider: "openrouter"
|
|
631
|
+
},
|
|
632
|
+
groq: { baseURL: "https://api.groq.com/openai/v1", apiKeyEnv: "GROQ_API_KEY", provider: "groq" },
|
|
633
|
+
ollama: { baseURL: "http://localhost:11434/v1", provider: "ollama" },
|
|
634
|
+
deepseek: {
|
|
635
|
+
baseURL: "https://api.deepseek.com/v1",
|
|
636
|
+
apiKeyEnv: "DEEPSEEK_API_KEY",
|
|
637
|
+
provider: "deepseek"
|
|
638
|
+
},
|
|
639
|
+
xai: { baseURL: "https://api.x.ai/v1", apiKeyEnv: "XAI_API_KEY", provider: "xai" }
|
|
640
|
+
};
|
|
641
|
+
function openAICompatAdapter(opts = {}) {
|
|
642
|
+
if (opts.sdk) return sdkBackedOpenAI(opts);
|
|
643
|
+
return fetchBackedOpenAI(opts);
|
|
644
|
+
}
|
|
645
|
+
function toOpenAIRequest(messages, invokeOpts, defaultModel, stream, bodyExtras) {
|
|
646
|
+
const model = invokeOpts?.model ?? defaultModel;
|
|
647
|
+
if (!model)
|
|
648
|
+
throw new Error("openAICompatAdapter: model must be set via options.model or invokeOpts.model");
|
|
649
|
+
const mapped = messages.map(toOpenAIMessage);
|
|
650
|
+
if (invokeOpts?.systemPrompt && !messages.some((m) => m.role === "system")) {
|
|
651
|
+
mapped.unshift({ role: "system", content: invokeOpts.systemPrompt });
|
|
652
|
+
}
|
|
653
|
+
const body = { model, messages: mapped };
|
|
654
|
+
if (invokeOpts?.maxTokens != null) body.max_tokens = invokeOpts.maxTokens;
|
|
655
|
+
if (invokeOpts?.temperature != null) body.temperature = invokeOpts.temperature;
|
|
656
|
+
if (invokeOpts?.tools && invokeOpts.tools.length > 0) {
|
|
657
|
+
body.tools = invokeOpts.tools.map(toOpenAITool);
|
|
658
|
+
}
|
|
659
|
+
if (invokeOpts?.maxReasoningTokens != null) {
|
|
660
|
+
body.reasoning = { max_tokens: invokeOpts.maxReasoningTokens };
|
|
661
|
+
}
|
|
662
|
+
if (stream) {
|
|
663
|
+
body.stream = true;
|
|
664
|
+
body.stream_options = { include_usage: true };
|
|
665
|
+
}
|
|
666
|
+
if (bodyExtras) Object.assign(body, bodyExtras);
|
|
667
|
+
if (invokeOpts?.providerExtras) Object.assign(body, invokeOpts.providerExtras);
|
|
668
|
+
return body;
|
|
669
|
+
}
|
|
670
|
+
function toOpenAIMessage(m) {
|
|
671
|
+
if (m.role === "tool") {
|
|
672
|
+
return { role: "tool", tool_call_id: m.toolCallId, content: m.content };
|
|
673
|
+
}
|
|
674
|
+
if (m.role === "assistant" && m.toolCalls && m.toolCalls.length > 0) {
|
|
675
|
+
return {
|
|
676
|
+
role: "assistant",
|
|
677
|
+
content: m.content || null,
|
|
678
|
+
tool_calls: m.toolCalls.map((tc) => ({
|
|
679
|
+
id: tc.id,
|
|
680
|
+
type: "function",
|
|
681
|
+
function: { name: tc.name, arguments: JSON.stringify(tc.arguments) }
|
|
682
|
+
}))
|
|
683
|
+
};
|
|
684
|
+
}
|
|
685
|
+
return { role: m.role, content: m.content };
|
|
686
|
+
}
|
|
687
|
+
function toOpenAITool(t) {
|
|
688
|
+
return {
|
|
689
|
+
type: "function",
|
|
690
|
+
function: {
|
|
691
|
+
name: t.name,
|
|
692
|
+
description: t.description,
|
|
693
|
+
parameters: t.parameters
|
|
694
|
+
}
|
|
695
|
+
};
|
|
696
|
+
}
|
|
697
|
+
function mapUsage3(u) {
|
|
698
|
+
const usage = {
|
|
699
|
+
input: { regular: 0 },
|
|
700
|
+
output: { regular: 0 },
|
|
701
|
+
raw: u
|
|
702
|
+
};
|
|
703
|
+
if (!u) return usage;
|
|
704
|
+
const inputTotal = u.prompt_tokens ?? 0;
|
|
705
|
+
let cacheRead = u.prompt_tokens_details?.cached_tokens ?? 0;
|
|
706
|
+
if (u.prompt_cache_hit_tokens != null) {
|
|
707
|
+
cacheRead = u.prompt_cache_hit_tokens;
|
|
708
|
+
usage.input.regular = u.prompt_cache_miss_tokens ?? Math.max(0, inputTotal - cacheRead);
|
|
709
|
+
} else {
|
|
710
|
+
usage.input.regular = Math.max(0, inputTotal - cacheRead);
|
|
711
|
+
}
|
|
712
|
+
if (cacheRead > 0) usage.input.cacheRead = cacheRead;
|
|
713
|
+
if (u.prompt_tokens_details?.audio_tokens)
|
|
714
|
+
usage.input.audio = u.prompt_tokens_details.audio_tokens;
|
|
715
|
+
const outputTotal = u.completion_tokens ?? 0;
|
|
716
|
+
const reasoning = u.completion_tokens_details?.reasoning_tokens ?? 0;
|
|
717
|
+
usage.output.regular = Math.max(0, outputTotal - reasoning);
|
|
718
|
+
if (reasoning > 0) usage.output.reasoning = reasoning;
|
|
719
|
+
if (u.completion_tokens_details?.audio_tokens)
|
|
720
|
+
usage.output.audio = u.completion_tokens_details.audio_tokens;
|
|
721
|
+
if (u.completion_tokens_details?.accepted_prediction_tokens) {
|
|
722
|
+
usage.output.predictionAccepted = u.completion_tokens_details.accepted_prediction_tokens;
|
|
723
|
+
}
|
|
724
|
+
if (u.completion_tokens_details?.rejected_prediction_tokens) {
|
|
725
|
+
usage.output.predictionRejected = u.completion_tokens_details.rejected_prediction_tokens;
|
|
726
|
+
}
|
|
727
|
+
return usage;
|
|
728
|
+
}
|
|
729
|
+
function toLLMResponse3(json, latencyMs, provider) {
|
|
730
|
+
const choice = json.choices[0];
|
|
731
|
+
const msg = choice?.message;
|
|
732
|
+
const content = msg?.content ?? "";
|
|
733
|
+
const toolCalls = (msg?.tool_calls ?? []).map((tc) => ({
|
|
734
|
+
id: tc.id,
|
|
735
|
+
name: tc.function.name,
|
|
736
|
+
arguments: safeJsonParse(tc.function.arguments)
|
|
737
|
+
}));
|
|
738
|
+
return {
|
|
739
|
+
content,
|
|
740
|
+
toolCalls: toolCalls.length > 0 ? toolCalls : void 0,
|
|
741
|
+
usage: mapUsage3(json.usage),
|
|
742
|
+
finishReason: choice?.finish_reason,
|
|
743
|
+
latencyMs,
|
|
744
|
+
model: json.model,
|
|
745
|
+
provider
|
|
746
|
+
};
|
|
747
|
+
}
|
|
748
|
+
function safeJsonParse(s) {
|
|
749
|
+
try {
|
|
750
|
+
const parsed = JSON.parse(s);
|
|
751
|
+
return typeof parsed === "object" && parsed != null ? parsed : { _raw: s };
|
|
752
|
+
} catch {
|
|
753
|
+
return { _raw: s };
|
|
754
|
+
}
|
|
755
|
+
}
|
|
756
|
+
function resolveConfig(opts) {
|
|
757
|
+
const preset = opts.preset ?? "openai";
|
|
758
|
+
const base = PRESETS[preset];
|
|
759
|
+
const baseURL = opts.baseURL ?? base.baseURL;
|
|
760
|
+
const provider = opts.provider ?? base.provider;
|
|
761
|
+
const envKey = base.apiKeyEnv;
|
|
762
|
+
const apiKey = opts.apiKey ?? (envKey ? globalThis.process?.env?.[envKey] : void 0);
|
|
763
|
+
return { provider, baseURL, apiKey };
|
|
764
|
+
}
|
|
765
|
+
function fetchBackedOpenAI(opts) {
|
|
766
|
+
const { provider, baseURL, apiKey } = resolveConfig(opts);
|
|
767
|
+
const fetchImpl = opts.fetchImpl ?? fetch;
|
|
768
|
+
const needsKey = provider !== "ollama";
|
|
769
|
+
const commonHeaders = () => {
|
|
770
|
+
const h = {
|
|
771
|
+
"content-type": "application/json",
|
|
772
|
+
...opts.headers ?? {}
|
|
773
|
+
};
|
|
774
|
+
if (needsKey) {
|
|
775
|
+
if (!apiKey)
|
|
776
|
+
throw new Error(`openAICompatAdapter[${provider}]: apiKey required for invoke/stream`);
|
|
777
|
+
h.authorization = `Bearer ${apiKey}`;
|
|
778
|
+
}
|
|
779
|
+
return h;
|
|
780
|
+
};
|
|
781
|
+
return {
|
|
782
|
+
provider,
|
|
783
|
+
model: opts.model,
|
|
784
|
+
async invoke(messages, invokeOpts) {
|
|
785
|
+
const body = toOpenAIRequest(messages, invokeOpts, opts.model, false, opts.bodyExtras);
|
|
786
|
+
const start = monotonicNs3();
|
|
787
|
+
const resp = await fetchImpl(`${baseURL}/chat/completions`, {
|
|
788
|
+
method: "POST",
|
|
789
|
+
headers: commonHeaders(),
|
|
790
|
+
body: JSON.stringify(body),
|
|
791
|
+
signal: invokeOpts?.signal
|
|
792
|
+
});
|
|
793
|
+
if (!resp.ok) throw await makeHttpError(resp, provider);
|
|
794
|
+
const json = await resp.json();
|
|
795
|
+
const latencyMs = Math.max(0, (monotonicNs3() - start) / 1e6);
|
|
796
|
+
return toLLMResponse3(json, latencyMs, provider);
|
|
797
|
+
},
|
|
798
|
+
async *stream(messages, invokeOpts) {
|
|
799
|
+
const body = toOpenAIRequest(messages, invokeOpts, opts.model, true, opts.bodyExtras);
|
|
800
|
+
const resp = await fetchImpl(`${baseURL}/chat/completions`, {
|
|
801
|
+
method: "POST",
|
|
802
|
+
headers: { ...commonHeaders(), accept: "text/event-stream" },
|
|
803
|
+
body: JSON.stringify(body),
|
|
804
|
+
signal: invokeOpts?.signal
|
|
805
|
+
});
|
|
806
|
+
if (!resp.ok) throw await makeHttpError(resp, provider);
|
|
807
|
+
if (!resp.body)
|
|
808
|
+
throw new Error(`openAICompatAdapter[${provider}]: streaming response has no body`);
|
|
809
|
+
let finalUsage;
|
|
810
|
+
let finishReason;
|
|
811
|
+
for await (const event of parseSSEStream(resp.body, { signal: invokeOpts?.signal })) {
|
|
812
|
+
if (!event.data || event.data === "[DONE]") continue;
|
|
813
|
+
let parsed;
|
|
814
|
+
try {
|
|
815
|
+
parsed = JSON.parse(event.data);
|
|
816
|
+
} catch {
|
|
817
|
+
continue;
|
|
818
|
+
}
|
|
819
|
+
const choices = parsed.choices;
|
|
820
|
+
if (choices) {
|
|
821
|
+
const c = choices[0];
|
|
822
|
+
if (c?.delta?.content) yield { type: "token", delta: c.delta.content };
|
|
823
|
+
if (c?.delta?.reasoning_content) {
|
|
824
|
+
yield { type: "thinking", delta: c.delta.reasoning_content };
|
|
825
|
+
}
|
|
826
|
+
if (c?.delta?.tool_calls) {
|
|
827
|
+
for (const tc of c.delta.tool_calls) {
|
|
828
|
+
yield {
|
|
829
|
+
type: "tool-call-delta",
|
|
830
|
+
delta: {
|
|
831
|
+
id: tc.id,
|
|
832
|
+
name: tc.function?.name,
|
|
833
|
+
argumentsDelta: tc.function?.arguments
|
|
834
|
+
}
|
|
835
|
+
};
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
if (c?.finish_reason) finishReason = c.finish_reason;
|
|
839
|
+
}
|
|
840
|
+
if (parsed.usage) {
|
|
841
|
+
finalUsage = parsed.usage;
|
|
842
|
+
yield { type: "usage", usage: mapUsage3(finalUsage) };
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
yield { type: "finish", reason: finishReason ?? "stop" };
|
|
846
|
+
}
|
|
847
|
+
};
|
|
848
|
+
}
|
|
849
|
+
function sdkBackedOpenAI(opts) {
|
|
850
|
+
const sdk = opts.sdk;
|
|
851
|
+
if (!sdk) throw new Error("sdkBackedOpenAI: sdk instance required");
|
|
852
|
+
const { provider } = resolveConfig(opts);
|
|
853
|
+
return {
|
|
854
|
+
provider,
|
|
855
|
+
model: opts.model,
|
|
856
|
+
async invoke(messages, invokeOpts) {
|
|
857
|
+
const body = toOpenAIRequest(messages, invokeOpts, opts.model, false, opts.bodyExtras);
|
|
858
|
+
const start = monotonicNs3();
|
|
859
|
+
const resp = await sdk.chat.completions.create(body, { signal: invokeOpts?.signal });
|
|
860
|
+
const latencyMs = Math.max(0, (monotonicNs3() - start) / 1e6);
|
|
861
|
+
return toLLMResponse3(resp, latencyMs, provider);
|
|
862
|
+
},
|
|
863
|
+
async *stream(messages, invokeOpts) {
|
|
864
|
+
const body = toOpenAIRequest(messages, invokeOpts, opts.model, true, opts.bodyExtras);
|
|
865
|
+
const stream = await sdk.chat.completions.create(body, {
|
|
866
|
+
signal: invokeOpts?.signal
|
|
867
|
+
});
|
|
868
|
+
let finalUsage;
|
|
869
|
+
let finishReason;
|
|
870
|
+
for await (const chunk of stream) {
|
|
871
|
+
const choices = chunk.choices;
|
|
872
|
+
if (choices) {
|
|
873
|
+
const c = choices[0];
|
|
874
|
+
if (c?.delta?.content) yield { type: "token", delta: c.delta.content };
|
|
875
|
+
if (c?.delta?.reasoning_content)
|
|
876
|
+
yield { type: "thinking", delta: c.delta.reasoning_content };
|
|
877
|
+
if (c?.delta?.tool_calls) {
|
|
878
|
+
for (const tc of c.delta.tool_calls) {
|
|
879
|
+
yield {
|
|
880
|
+
type: "tool-call-delta",
|
|
881
|
+
delta: {
|
|
882
|
+
id: tc.id,
|
|
883
|
+
name: tc.function?.name,
|
|
884
|
+
argumentsDelta: tc.function?.arguments
|
|
885
|
+
}
|
|
886
|
+
};
|
|
887
|
+
}
|
|
888
|
+
}
|
|
889
|
+
if (c?.finish_reason) finishReason = c.finish_reason;
|
|
890
|
+
}
|
|
891
|
+
const u = chunk.usage;
|
|
892
|
+
if (u) {
|
|
893
|
+
finalUsage = u;
|
|
894
|
+
yield { type: "usage", usage: mapUsage3(u) };
|
|
895
|
+
}
|
|
896
|
+
}
|
|
897
|
+
if (!finalUsage) {
|
|
898
|
+
}
|
|
899
|
+
yield { type: "finish", reason: finishReason ?? "stop" };
|
|
900
|
+
}
|
|
901
|
+
};
|
|
902
|
+
}
|
|
903
|
+
|
|
904
|
+
// src/utils/ai/adapters/core/factory.ts
|
|
905
|
+
function createAdapter(opts) {
|
|
906
|
+
switch (opts.provider) {
|
|
907
|
+
case "anthropic": {
|
|
908
|
+
const a = {
|
|
909
|
+
apiKey: opts.apiKey,
|
|
910
|
+
model: opts.model,
|
|
911
|
+
baseURL: opts.baseURL,
|
|
912
|
+
headers: opts.headers,
|
|
913
|
+
sdk: opts.sdk,
|
|
914
|
+
fetchImpl: opts.fetchImpl,
|
|
915
|
+
...opts.extras
|
|
916
|
+
};
|
|
917
|
+
return anthropicAdapter(a);
|
|
918
|
+
}
|
|
919
|
+
case "google": {
|
|
920
|
+
const g = {
|
|
921
|
+
apiKey: opts.apiKey,
|
|
922
|
+
model: opts.model,
|
|
923
|
+
baseURL: opts.baseURL,
|
|
924
|
+
headers: opts.headers,
|
|
925
|
+
sdk: opts.sdk,
|
|
926
|
+
fetchImpl: opts.fetchImpl,
|
|
927
|
+
...opts.extras
|
|
928
|
+
};
|
|
929
|
+
return googleAdapter(g);
|
|
930
|
+
}
|
|
931
|
+
case "dry-run": {
|
|
932
|
+
const d = {
|
|
933
|
+
model: opts.model,
|
|
934
|
+
...opts.extras
|
|
935
|
+
};
|
|
936
|
+
return dryRunAdapter(d);
|
|
937
|
+
}
|
|
938
|
+
case "fallback": {
|
|
939
|
+
const f = {
|
|
940
|
+
provider: opts.provider,
|
|
941
|
+
model: opts.model,
|
|
942
|
+
...opts.extras
|
|
943
|
+
};
|
|
944
|
+
return fallbackAdapter(f);
|
|
945
|
+
}
|
|
946
|
+
// OpenAI-compat presets
|
|
947
|
+
case "openai":
|
|
948
|
+
case "openrouter":
|
|
949
|
+
case "groq":
|
|
950
|
+
case "ollama":
|
|
951
|
+
case "deepseek":
|
|
952
|
+
case "xai": {
|
|
953
|
+
const c = {
|
|
954
|
+
preset: opts.provider,
|
|
955
|
+
apiKey: opts.apiKey,
|
|
956
|
+
model: opts.model,
|
|
957
|
+
baseURL: opts.baseURL,
|
|
958
|
+
headers: opts.headers,
|
|
959
|
+
bodyExtras: opts.bodyExtras,
|
|
960
|
+
sdk: opts.sdk,
|
|
961
|
+
fetchImpl: opts.fetchImpl,
|
|
962
|
+
...opts.extras
|
|
963
|
+
};
|
|
964
|
+
return openAICompatAdapter(c);
|
|
965
|
+
}
|
|
966
|
+
default: {
|
|
967
|
+
const never = opts.provider;
|
|
968
|
+
throw new Error(`createAdapter: unknown provider: ${String(never)}`);
|
|
969
|
+
}
|
|
970
|
+
}
|
|
971
|
+
}
|
|
972
|
+
|
|
973
|
+
// src/utils/ai/adapters/routing/cascading.ts
|
|
974
|
+
import { fromAny } from "@graphrefly/pure-ts/extra";
|
|
975
|
+
var AllTiersExhaustedError = class extends Error {
|
|
976
|
+
name = "AllTiersExhaustedError";
|
|
977
|
+
skipped;
|
|
978
|
+
failed;
|
|
979
|
+
constructor(report) {
|
|
980
|
+
const parts = [];
|
|
981
|
+
if (report.failed.size > 0) parts.push(`failed=[${[...report.failed.keys()].join(",")}]`);
|
|
982
|
+
if (report.skipped.length > 0) {
|
|
983
|
+
parts.push(`skipped=[${report.skipped.map((s) => `${s.name}(${s.reason})`).join(",")}]`);
|
|
984
|
+
}
|
|
985
|
+
super(`All LLM adapter tiers exhausted: ${parts.join(" ")}`);
|
|
986
|
+
this.skipped = report.skipped;
|
|
987
|
+
this.failed = report.failed;
|
|
988
|
+
}
|
|
989
|
+
};
|
|
990
|
+
function cascadingLlmAdapter(tiers, opts = {}) {
|
|
991
|
+
if (tiers.length === 0) throw new RangeError("cascadingLlmAdapter: tiers must be non-empty");
|
|
992
|
+
const resolved = tiers.map((t) => ({
|
|
993
|
+
name: t.name,
|
|
994
|
+
adapter: t.adapter,
|
|
995
|
+
filter: t.filter,
|
|
996
|
+
breaker: t.breaker ? "canExecute" in t.breaker ? t.breaker : circuitBreaker(t.breaker) : void 0
|
|
997
|
+
}));
|
|
998
|
+
const streamRetryBeforeFirstChunk = opts.streamRetryBeforeFirstChunk ?? true;
|
|
999
|
+
const cascade = {
|
|
1000
|
+
provider: "cascading",
|
|
1001
|
+
model: void 0,
|
|
1002
|
+
async invoke(messages, invokeOpts) {
|
|
1003
|
+
const skipped = [];
|
|
1004
|
+
const failed = /* @__PURE__ */ new Map();
|
|
1005
|
+
for (let i = 0; i < resolved.length; i++) {
|
|
1006
|
+
const t = resolved[i];
|
|
1007
|
+
if (t.filter && !t.filter(messages, invokeOpts)) {
|
|
1008
|
+
skipped.push({ name: t.name, reason: "filter" });
|
|
1009
|
+
continue;
|
|
1010
|
+
}
|
|
1011
|
+
if (t.breaker && !t.breaker.canExecute()) {
|
|
1012
|
+
skipped.push({ name: t.name, reason: "breaker" });
|
|
1013
|
+
continue;
|
|
1014
|
+
}
|
|
1015
|
+
try {
|
|
1016
|
+
const resp = await firstValueFrom(fromAny(t.adapter.invoke(messages, invokeOpts)));
|
|
1017
|
+
t.breaker?.recordSuccess();
|
|
1018
|
+
return { ...resp, metadata: { ...resp.metadata ?? {}, tier: t.name } };
|
|
1019
|
+
} catch (err) {
|
|
1020
|
+
failed.set(t.name, err);
|
|
1021
|
+
t.breaker?.recordFailure(err);
|
|
1022
|
+
const next = resolved[i + 1];
|
|
1023
|
+
if (next) opts.onFallback?.(t.name, next.name, err);
|
|
1024
|
+
}
|
|
1025
|
+
}
|
|
1026
|
+
const report = { skipped, failed };
|
|
1027
|
+
opts.onExhausted?.(report);
|
|
1028
|
+
throw new AllTiersExhaustedError(report);
|
|
1029
|
+
},
|
|
1030
|
+
async *stream(messages, invokeOpts) {
|
|
1031
|
+
const skipped = [];
|
|
1032
|
+
const failed = /* @__PURE__ */ new Map();
|
|
1033
|
+
for (let i = 0; i < resolved.length; i++) {
|
|
1034
|
+
const t = resolved[i];
|
|
1035
|
+
if (t.filter && !t.filter(messages, invokeOpts)) {
|
|
1036
|
+
skipped.push({ name: t.name, reason: "filter" });
|
|
1037
|
+
continue;
|
|
1038
|
+
}
|
|
1039
|
+
if (t.breaker && !t.breaker.canExecute()) {
|
|
1040
|
+
skipped.push({ name: t.name, reason: "breaker" });
|
|
1041
|
+
continue;
|
|
1042
|
+
}
|
|
1043
|
+
let yieldedAny = false;
|
|
1044
|
+
try {
|
|
1045
|
+
for await (const delta of t.adapter.stream(messages, invokeOpts)) {
|
|
1046
|
+
yieldedAny = true;
|
|
1047
|
+
yield delta;
|
|
1048
|
+
}
|
|
1049
|
+
t.breaker?.recordSuccess();
|
|
1050
|
+
return;
|
|
1051
|
+
} catch (err) {
|
|
1052
|
+
failed.set(t.name, err);
|
|
1053
|
+
t.breaker?.recordFailure(err);
|
|
1054
|
+
if (yieldedAny || !streamRetryBeforeFirstChunk) {
|
|
1055
|
+
throw err;
|
|
1056
|
+
}
|
|
1057
|
+
const next = resolved[i + 1];
|
|
1058
|
+
if (next) opts.onFallback?.(t.name, next.name, err);
|
|
1059
|
+
}
|
|
1060
|
+
}
|
|
1061
|
+
const report = { skipped, failed };
|
|
1062
|
+
opts.onExhausted?.(report);
|
|
1063
|
+
throw new AllTiersExhaustedError(report);
|
|
1064
|
+
}
|
|
1065
|
+
};
|
|
1066
|
+
withLayer(cascade, `cascade[${resolved.map((t) => t.name).join(",")}]`);
|
|
1067
|
+
return cascade;
|
|
1068
|
+
}
|
|
1069
|
+
function tier(name, adapter, opts) {
|
|
1070
|
+
const out = { name, adapter };
|
|
1071
|
+
if (opts?.breaker) out.breaker = opts.breaker;
|
|
1072
|
+
if (opts?.filter) out.filter = opts.filter;
|
|
1073
|
+
return out;
|
|
1074
|
+
}
|
|
1075
|
+
|
|
1076
|
+
export {
|
|
1077
|
+
anthropicAdapter,
|
|
1078
|
+
googleAdapter,
|
|
1079
|
+
openAICompatAdapter,
|
|
1080
|
+
createAdapter,
|
|
1081
|
+
AllTiersExhaustedError,
|
|
1082
|
+
cascadingLlmAdapter,
|
|
1083
|
+
tier
|
|
1084
|
+
};
|
|
1085
|
+
//# sourceMappingURL=chunk-4GYMCUDZ.js.map
|