@mastra/server 0.0.0-support-d1-client-20250701191943 → 0.0.0-suspendRuntimeContextTypeFix-20250930142630
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +3812 -0
- package/LICENSE.md +11 -42
- package/README.md +0 -5
- package/dist/{chunk-H5PTF3Y4.js → chunk-4QCXUEAT.js} +11 -2
- package/dist/chunk-4QCXUEAT.js.map +1 -0
- package/dist/chunk-4RRMWXQ2.js +3522 -0
- package/dist/chunk-4RRMWXQ2.js.map +1 -0
- package/dist/chunk-52UP7DFT.cjs +130 -0
- package/dist/chunk-52UP7DFT.cjs.map +1 -0
- package/dist/chunk-5DP5XZH6.cjs +928 -0
- package/dist/chunk-5DP5XZH6.cjs.map +1 -0
- package/dist/chunk-66YYHFGF.js +761 -0
- package/dist/chunk-66YYHFGF.js.map +1 -0
- package/dist/chunk-6GMFZ5LK.js +2774 -0
- package/dist/chunk-6GMFZ5LK.js.map +1 -0
- package/dist/{chunk-GHC4YV6R.js → chunk-6IJWRJZE.js} +109 -136
- package/dist/chunk-6IJWRJZE.js.map +1 -0
- package/dist/chunk-743UIDHI.cjs +2013 -0
- package/dist/chunk-743UIDHI.cjs.map +1 -0
- package/dist/chunk-7ESQBAXS.cjs +18023 -0
- package/dist/chunk-7ESQBAXS.cjs.map +1 -0
- package/dist/{chunk-OCWPVYNI.cjs → chunk-7NADHFD2.cjs} +3 -0
- package/dist/chunk-7NADHFD2.cjs.map +1 -0
- package/dist/{chunk-BOXE6WSV.js → chunk-A3AL7EWJ.js} +5 -3
- package/dist/chunk-A3AL7EWJ.js.map +1 -0
- package/dist/chunk-AK2FXLLB.cjs +849 -0
- package/dist/chunk-AK2FXLLB.cjs.map +1 -0
- package/dist/chunk-BKCWZLEL.cjs +197 -0
- package/dist/chunk-BKCWZLEL.cjs.map +1 -0
- package/dist/chunk-BOVFMBXY.js +607 -0
- package/dist/chunk-BOVFMBXY.js.map +1 -0
- package/dist/{chunk-QDOJJCS4.js → chunk-CY4TP3FK.js} +6 -2
- package/dist/chunk-CY4TP3FK.js.map +1 -0
- package/dist/chunk-EMMSS5I5.cjs +37 -0
- package/dist/chunk-EMMSS5I5.cjs.map +1 -0
- package/dist/chunk-EMNGA4R4.js +845 -0
- package/dist/chunk-EMNGA4R4.js.map +1 -0
- package/dist/chunk-FALVL2VV.cjs +3525 -0
- package/dist/chunk-FALVL2VV.cjs.map +1 -0
- package/dist/chunk-FQNT7PI4.js +937 -0
- package/dist/chunk-FQNT7PI4.js.map +1 -0
- package/dist/chunk-G3PMV62Z.js +33 -0
- package/dist/chunk-G3PMV62Z.js.map +1 -0
- package/dist/chunk-G4PUALCE.cjs +28 -0
- package/dist/chunk-G4PUALCE.cjs.map +1 -0
- package/dist/chunk-GU4EWMZB.cjs +769 -0
- package/dist/chunk-GU4EWMZB.cjs.map +1 -0
- package/dist/chunk-HJQKWRKQ.cjs +764 -0
- package/dist/chunk-HJQKWRKQ.cjs.map +1 -0
- package/dist/{chunk-LI436ITD.cjs → chunk-HVBBFCDH.cjs} +1041 -762
- package/dist/chunk-HVBBFCDH.cjs.map +1 -0
- package/dist/chunk-HZJRQ5L3.cjs +1411 -0
- package/dist/chunk-HZJRQ5L3.cjs.map +1 -0
- package/dist/chunk-IGFMAZZ5.cjs +1150 -0
- package/dist/chunk-IGFMAZZ5.cjs.map +1 -0
- package/dist/chunk-IOQGI4ML.js +931 -0
- package/dist/chunk-IOQGI4ML.js.map +1 -0
- package/dist/chunk-JQ74G5IO.js +17987 -0
- package/dist/chunk-JQ74G5IO.js.map +1 -0
- package/dist/{chunk-G37NVEDX.js → chunk-JRDEOHAJ.js} +23 -13
- package/dist/chunk-JRDEOHAJ.js.map +1 -0
- package/dist/chunk-JSMP6GVP.cjs +595 -0
- package/dist/chunk-JSMP6GVP.cjs.map +1 -0
- package/dist/{chunk-DFJUGWNU.js → chunk-KM7EJKXV.js} +50 -12
- package/dist/chunk-KM7EJKXV.js.map +1 -0
- package/dist/{chunk-TGZIR6AK.cjs → chunk-KNGXRN26.cjs} +48 -45
- package/dist/chunk-KNGXRN26.cjs.map +1 -0
- package/dist/{chunk-BP24Z4WG.js → chunk-KV6VHX4V.js} +24 -5
- package/dist/chunk-KV6VHX4V.js.map +1 -0
- package/dist/chunk-LF2ZLOFP.js +767 -0
- package/dist/chunk-LF2ZLOFP.js.map +1 -0
- package/dist/chunk-LJFAAEU7.cjs +167 -0
- package/dist/chunk-LJFAAEU7.cjs.map +1 -0
- package/dist/chunk-LYPU75T6.js +1147 -0
- package/dist/chunk-LYPU75T6.js.map +1 -0
- package/dist/{chunk-NYN7KFXL.js → chunk-MMROOK5J.js} +3 -0
- package/dist/chunk-MMROOK5J.js.map +1 -0
- package/dist/{chunk-MEGCYGBU.js → chunk-N7F33WAD.js} +1013 -762
- package/dist/chunk-N7F33WAD.js.map +1 -0
- package/dist/chunk-NG5IVLEZ.js +1012 -0
- package/dist/chunk-NG5IVLEZ.js.map +1 -0
- package/dist/chunk-NLWACBE7.cjs +128 -0
- package/dist/chunk-NLWACBE7.cjs.map +1 -0
- package/dist/chunk-OGW6HHVI.js +1408 -0
- package/dist/chunk-OGW6HHVI.js.map +1 -0
- package/dist/{chunk-SDPGVWQJ.cjs → chunk-OPI3FZYH.cjs} +110 -137
- package/dist/chunk-OPI3FZYH.cjs.map +1 -0
- package/dist/chunk-OZLRIVC4.cjs +588 -0
- package/dist/chunk-OZLRIVC4.cjs.map +1 -0
- package/dist/chunk-P7CIEIJ3.js +925 -0
- package/dist/chunk-P7CIEIJ3.js.map +1 -0
- package/dist/chunk-P7RBMCBE.cjs +934 -0
- package/dist/chunk-P7RBMCBE.cjs.map +1 -0
- package/dist/chunk-PPYGWINI.cjs +2777 -0
- package/dist/chunk-PPYGWINI.cjs.map +1 -0
- package/dist/{chunk-ULFULEW4.cjs → chunk-PWTXZZTR.cjs} +28 -8
- package/dist/chunk-PWTXZZTR.cjs.map +1 -0
- package/dist/chunk-RCHEPTZZ.js +2006 -0
- package/dist/chunk-RCHEPTZZ.js.map +1 -0
- package/dist/chunk-RE4RPXT2.cjs +18 -0
- package/dist/chunk-RE4RPXT2.cjs.map +1 -0
- package/dist/chunk-SIGXR3JT.cjs +1043 -0
- package/dist/chunk-SIGXR3JT.cjs.map +1 -0
- package/dist/{chunk-CNVDCLSY.cjs → chunk-SPLSYTYW.cjs} +11 -9
- package/dist/chunk-SPLSYTYW.cjs.map +1 -0
- package/dist/chunk-SQY4T6EJ.js +571 -0
- package/dist/chunk-SQY4T6EJ.js.map +1 -0
- package/dist/chunk-SXENNNR7.cjs +625 -0
- package/dist/chunk-SXENNNR7.cjs.map +1 -0
- package/dist/{chunk-TVBPFPTN.cjs → chunk-T3TIA3O6.cjs} +20 -18
- package/dist/chunk-T3TIA3O6.cjs.map +1 -0
- package/dist/{chunk-7GQJAMTA.js → chunk-TTHEEIZ3.js} +13 -10
- package/dist/chunk-TTHEEIZ3.js.map +1 -0
- package/dist/chunk-TVSIG4JE.cjs +940 -0
- package/dist/chunk-TVSIG4JE.cjs.map +1 -0
- package/dist/chunk-VY4ENABS.cjs +135 -0
- package/dist/chunk-VY4ENABS.cjs.map +1 -0
- package/dist/{chunk-FVMGZKFD.js → chunk-WHN4VX55.js} +5 -3
- package/dist/chunk-WHN4VX55.js.map +1 -0
- package/dist/chunk-WUJLVNLU.js +124 -0
- package/dist/chunk-WUJLVNLU.js.map +1 -0
- package/dist/chunk-XJOYGRTN.js +189 -0
- package/dist/chunk-XJOYGRTN.js.map +1 -0
- package/dist/chunk-YWOK4F5A.js +131 -0
- package/dist/chunk-YWOK4F5A.js.map +1 -0
- package/dist/{chunk-Z5TH5F35.js → chunk-ZAYTLC6C.js} +188 -80
- package/dist/chunk-ZAYTLC6C.js.map +1 -0
- package/dist/dist-26HWEQY6.js +3 -0
- package/dist/dist-26HWEQY6.js.map +1 -0
- package/dist/dist-3A5DXB37.cjs +20 -0
- package/dist/dist-3A5DXB37.cjs.map +1 -0
- package/dist/dist-3SJKQJGY.cjs +16 -0
- package/dist/dist-3SJKQJGY.cjs.map +1 -0
- package/dist/dist-4ZQSPE5K.js +3 -0
- package/dist/dist-4ZQSPE5K.js.map +1 -0
- package/dist/dist-5W5QNRTD.js +3 -0
- package/dist/dist-5W5QNRTD.js.map +1 -0
- package/dist/dist-653SRMPL.js +3 -0
- package/dist/dist-653SRMPL.js.map +1 -0
- package/dist/dist-6U6EFC5C.cjs +16 -0
- package/dist/dist-6U6EFC5C.cjs.map +1 -0
- package/dist/dist-7IHNNYMF.cjs +16 -0
- package/dist/dist-7IHNNYMF.cjs.map +1 -0
- package/dist/dist-B5IPRF6W.js +3 -0
- package/dist/dist-B5IPRF6W.js.map +1 -0
- package/dist/dist-EOMYFT4Y.cjs +16 -0
- package/dist/dist-EOMYFT4Y.cjs.map +1 -0
- package/dist/dist-EZZMMMNT.cjs +16 -0
- package/dist/dist-EZZMMMNT.cjs.map +1 -0
- package/dist/dist-F2ET4MNO.cjs +16 -0
- package/dist/dist-F2ET4MNO.cjs.map +1 -0
- package/dist/dist-H64VX6DE.js +3 -0
- package/dist/dist-H64VX6DE.js.map +1 -0
- package/dist/dist-HY7RMLJQ.cjs +16 -0
- package/dist/dist-HY7RMLJQ.cjs.map +1 -0
- package/dist/dist-M6S4P3FJ.js +3 -0
- package/dist/dist-M6S4P3FJ.js.map +1 -0
- package/dist/dist-NR7QSCQT.js +3 -0
- package/dist/dist-NR7QSCQT.js.map +1 -0
- package/dist/dist-QLFMCMCX.js +3 -0
- package/dist/dist-QLFMCMCX.js.map +1 -0
- package/dist/dist-UY46BFRP.js +3 -0
- package/dist/dist-UY46BFRP.js.map +1 -0
- package/dist/dist-WCQDRTIV.cjs +16 -0
- package/dist/dist-WCQDRTIV.cjs.map +1 -0
- package/dist/dist-WKYB3LTJ.cjs +16 -0
- package/dist/dist-WKYB3LTJ.cjs.map +1 -0
- package/dist/index.cjs +6 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +5 -0
- package/dist/index.js.map +1 -0
- package/dist/server/a2a/protocol.d.ts +8 -0
- package/dist/server/a2a/protocol.d.ts.map +1 -0
- package/dist/server/a2a/store.cjs +25 -0
- package/dist/server/a2a/store.cjs.map +1 -0
- package/dist/server/a2a/store.d.ts +14 -0
- package/dist/server/a2a/store.d.ts.map +1 -0
- package/dist/server/a2a/store.js +23 -0
- package/dist/server/a2a/store.js.map +1 -0
- package/dist/server/a2a/tasks.d.ts +20 -0
- package/dist/server/a2a/tasks.d.ts.map +1 -0
- package/dist/server/handlers/a2a.cjs +13 -11
- package/dist/server/handlers/a2a.cjs.map +1 -0
- package/dist/server/handlers/a2a.d.ts +68 -6
- package/dist/server/handlers/a2a.d.ts.map +1 -0
- package/dist/server/handlers/a2a.js +3 -1
- package/dist/server/handlers/a2a.js.map +1 -0
- package/dist/server/handlers/agent-builder.cjs +68 -0
- package/dist/server/handlers/agent-builder.cjs.map +1 -0
- package/dist/server/handlers/agent-builder.d.ts +92 -0
- package/dist/server/handlers/agent-builder.d.ts.map +1 -0
- package/dist/server/handlers/agent-builder.js +3 -0
- package/dist/server/handlers/agent-builder.js.map +1 -0
- package/dist/server/handlers/agents.cjs +49 -7
- package/dist/server/handlers/agents.cjs.map +1 -0
- package/dist/server/handlers/agents.d.ts +185 -6
- package/dist/server/handlers/agents.d.ts.map +1 -0
- package/dist/server/handlers/agents.js +3 -1
- package/dist/server/handlers/agents.js.map +1 -0
- package/dist/server/handlers/error.cjs +4 -2
- package/dist/server/handlers/error.cjs.map +1 -0
- package/dist/server/handlers/error.d.ts +2 -1
- package/dist/server/handlers/error.d.ts.map +1 -0
- package/dist/server/handlers/error.js +3 -1
- package/dist/server/handlers/error.js.map +1 -0
- package/dist/server/handlers/legacyWorkflows.cjs +13 -11
- package/dist/server/handlers/legacyWorkflows.cjs.map +1 -0
- package/dist/server/handlers/legacyWorkflows.d.ts +59 -10
- package/dist/server/handlers/legacyWorkflows.d.ts.map +1 -0
- package/dist/server/handlers/legacyWorkflows.js +3 -1
- package/dist/server/handlers/legacyWorkflows.js.map +1 -0
- package/dist/server/handlers/logs.cjs +6 -4
- package/dist/server/handlers/logs.cjs.map +1 -0
- package/dist/server/handlers/logs.d.ts +34 -3
- package/dist/server/handlers/logs.d.ts.map +1 -0
- package/dist/server/handlers/logs.js +3 -1
- package/dist/server/handlers/logs.js.map +1 -0
- package/dist/server/handlers/memory.cjs +39 -9
- package/dist/server/handlers/memory.cjs.map +1 -0
- package/dist/server/handlers/memory.d.ts +118 -8
- package/dist/server/handlers/memory.d.ts.map +1 -0
- package/dist/server/handlers/memory.js +3 -1
- package/dist/server/handlers/memory.js.map +1 -0
- package/dist/server/handlers/observability.cjs +24 -0
- package/dist/server/handlers/observability.cjs.map +1 -0
- package/dist/server/handlers/observability.d.ts +49 -0
- package/dist/server/handlers/observability.d.ts.map +1 -0
- package/dist/server/handlers/observability.js +3 -0
- package/dist/server/handlers/observability.js.map +1 -0
- package/dist/server/handlers/scores.cjs +32 -0
- package/dist/server/handlers/scores.cjs.map +1 -0
- package/dist/server/handlers/scores.d.ts +251 -0
- package/dist/server/handlers/scores.d.ts.map +1 -0
- package/dist/server/handlers/scores.js +3 -0
- package/dist/server/handlers/scores.js.map +1 -0
- package/dist/server/handlers/telemetry.cjs +9 -3
- package/dist/server/handlers/telemetry.cjs.map +1 -0
- package/dist/server/handlers/telemetry.d.ts +33 -2
- package/dist/server/handlers/telemetry.d.ts.map +1 -0
- package/dist/server/handlers/telemetry.js +3 -1
- package/dist/server/handlers/telemetry.js.map +1 -0
- package/dist/server/handlers/tools.cjs +11 -5
- package/dist/server/handlers/tools.cjs.map +1 -0
- package/dist/server/handlers/tools.d.ts +25 -4
- package/dist/server/handlers/tools.d.ts.map +1 -0
- package/dist/server/handlers/tools.js +3 -1
- package/dist/server/handlers/tools.js.map +1 -0
- package/dist/server/handlers/utils.cjs +8 -2
- package/dist/server/handlers/utils.cjs.map +1 -0
- package/dist/server/handlers/utils.d.ts +8 -1
- package/dist/server/handlers/utils.d.ts.map +1 -0
- package/dist/server/handlers/utils.js +3 -1
- package/dist/server/handlers/utils.js.map +1 -0
- package/dist/server/handlers/vNextNetwork.cjs +26 -24
- package/dist/server/handlers/vNextNetwork.cjs.map +1 -0
- package/dist/server/handlers/vNextNetwork.d.ts +246 -6
- package/dist/server/handlers/vNextNetwork.d.ts.map +1 -0
- package/dist/server/handlers/vNextNetwork.js +11 -9
- package/dist/server/handlers/vNextNetwork.js.map +1 -0
- package/dist/server/handlers/vector.cjs +9 -7
- package/dist/server/handlers/vector.cjs.map +1 -0
- package/dist/server/handlers/vector.d.ts +51 -6
- package/dist/server/handlers/vector.d.ts.map +1 -0
- package/dist/server/handlers/vector.js +3 -1
- package/dist/server/handlers/vector.js.map +1 -0
- package/dist/server/handlers/voice.cjs +7 -5
- package/dist/server/handlers/voice.cjs.map +1 -0
- package/dist/server/handlers/voice.d.ts +41 -4
- package/dist/server/handlers/voice.d.ts.map +1 -0
- package/dist/server/handlers/voice.js +3 -1
- package/dist/server/handlers/voice.js.map +1 -0
- package/dist/server/handlers/workflows.cjs +32 -14
- package/dist/server/handlers/workflows.cjs.map +1 -0
- package/dist/server/handlers/workflows.d.ts +87 -13
- package/dist/server/handlers/workflows.d.ts.map +1 -0
- package/dist/server/handlers/workflows.js +3 -1
- package/dist/server/handlers/workflows.js.map +1 -0
- package/dist/server/handlers.cjs +44 -32
- package/dist/server/handlers.cjs.map +1 -0
- package/dist/server/handlers.d.ts +14 -11
- package/dist/server/handlers.d.ts.map +1 -0
- package/dist/server/handlers.js +15 -11
- package/dist/server/handlers.js.map +1 -0
- package/dist/server/http-exception.d.ts +87 -0
- package/dist/server/http-exception.d.ts.map +1 -0
- package/dist/server/types.d.ts +10 -0
- package/dist/server/types.d.ts.map +1 -0
- package/dist/server/utils.d.ts +44 -0
- package/dist/server/utils.d.ts.map +1 -0
- package/package.json +46 -15
- package/dist/_tsup-dts-rollup.d.cts +0 -1068
- package/dist/_tsup-dts-rollup.d.ts +0 -1068
- package/dist/chunk-3DKDT264.cjs +0 -251
- package/dist/chunk-4QBIUKVY.js +0 -270
- package/dist/chunk-57CJTIPW.cjs +0 -18
- package/dist/chunk-7524QH7G.cjs +0 -483
- package/dist/chunk-75ZPJI57.cjs +0 -9
- package/dist/chunk-BWHGBV3K.cjs +0 -278
- package/dist/chunk-DPRB3R7Y.cjs +0 -148
- package/dist/chunk-FGGMQCPP.cjs +0 -128
- package/dist/chunk-L3N7ST2Z.cjs +0 -118
- package/dist/chunk-LTJNMM2Y.js +0 -241
- package/dist/chunk-MLKGABMK.js +0 -7
- package/dist/chunk-MTR2B27E.cjs +0 -14
- package/dist/chunk-VMXLEF54.js +0 -142
- package/dist/index.d.cts +0 -1
- package/dist/server/handlers/a2a.d.cts +0 -6
- package/dist/server/handlers/agents.d.cts +0 -6
- package/dist/server/handlers/error.d.cts +0 -1
- package/dist/server/handlers/legacyWorkflows.d.cts +0 -10
- package/dist/server/handlers/logs.d.cts +0 -3
- package/dist/server/handlers/memory.d.cts +0 -8
- package/dist/server/handlers/network.cjs +0 -22
- package/dist/server/handlers/network.d.cts +0 -4
- package/dist/server/handlers/network.d.ts +0 -4
- package/dist/server/handlers/network.js +0 -1
- package/dist/server/handlers/telemetry.d.cts +0 -2
- package/dist/server/handlers/tools.d.cts +0 -4
- package/dist/server/handlers/utils.d.cts +0 -1
- package/dist/server/handlers/vNextNetwork.d.cts +0 -6
- package/dist/server/handlers/vector.d.cts +0 -6
- package/dist/server/handlers/voice.d.cts +0 -4
- package/dist/server/handlers/workflows.d.cts +0 -13
- package/dist/server/handlers.d.cts +0 -11
|
@@ -0,0 +1,2006 @@
|
|
|
1
|
+
import { createJsonErrorResponseHandler, createProviderDefinedToolFactoryWithOutputSchema, createProviderDefinedToolFactory, withoutTrailingSlash, generateId, loadApiKey, parseProviderOptions, UnsupportedFunctionalityError, combineHeaders, resolve, postJsonToApi, createJsonResponseHandler, createEventSourceResponseHandler, convertToBase64, NoSuchModelError } from './chunk-N7F33WAD.js';
|
|
2
|
+
import z6, { z } from 'zod/v4';
|
|
3
|
+
|
|
4
|
+
var anthropicErrorDataSchema = z.object({
|
|
5
|
+
type: z.literal("error"),
|
|
6
|
+
error: z.object({
|
|
7
|
+
type: z.string(),
|
|
8
|
+
message: z.string()
|
|
9
|
+
})
|
|
10
|
+
});
|
|
11
|
+
var anthropicFailedResponseHandler = createJsonErrorResponseHandler({
|
|
12
|
+
errorSchema: anthropicErrorDataSchema,
|
|
13
|
+
errorToMessage: (data) => data.error.message
|
|
14
|
+
});
|
|
15
|
+
var anthropicFilePartProviderOptions = z.object({
|
|
16
|
+
/**
|
|
17
|
+
* Citation configuration for this document.
|
|
18
|
+
* When enabled, this document will generate citations in the response.
|
|
19
|
+
*/
|
|
20
|
+
citations: z.object({
|
|
21
|
+
/**
|
|
22
|
+
* Enable citations for this document
|
|
23
|
+
*/
|
|
24
|
+
enabled: z.boolean()
|
|
25
|
+
}).optional(),
|
|
26
|
+
/**
|
|
27
|
+
* Custom title for the document.
|
|
28
|
+
* If not provided, the filename will be used.
|
|
29
|
+
*/
|
|
30
|
+
title: z.string().optional(),
|
|
31
|
+
/**
|
|
32
|
+
* Context about the document that will be passed to the model
|
|
33
|
+
* but not used towards cited content.
|
|
34
|
+
* Useful for storing document metadata as text or stringified JSON.
|
|
35
|
+
*/
|
|
36
|
+
context: z.string().optional()
|
|
37
|
+
});
|
|
38
|
+
var anthropicProviderOptions = z.object({
|
|
39
|
+
sendReasoning: z.boolean().optional(),
|
|
40
|
+
thinking: z.object({
|
|
41
|
+
type: z.union([z.literal("enabled"), z.literal("disabled")]),
|
|
42
|
+
budgetTokens: z.number().optional()
|
|
43
|
+
}).optional(),
|
|
44
|
+
/**
|
|
45
|
+
* Whether to disable parallel function calling during tool use. Default is false.
|
|
46
|
+
* When set to true, Claude will use at most one tool per response.
|
|
47
|
+
*/
|
|
48
|
+
disableParallelToolUse: z.boolean().optional()
|
|
49
|
+
});
|
|
50
|
+
function getCacheControl(providerMetadata) {
|
|
51
|
+
var _a;
|
|
52
|
+
const anthropic2 = providerMetadata == null ? void 0 : providerMetadata.anthropic;
|
|
53
|
+
const cacheControlValue = (_a = anthropic2 == null ? void 0 : anthropic2.cacheControl) != null ? _a : anthropic2 == null ? void 0 : anthropic2.cache_control;
|
|
54
|
+
return cacheControlValue;
|
|
55
|
+
}
|
|
56
|
+
var webSearch_20250305ArgsSchema = z.object({
|
|
57
|
+
/**
|
|
58
|
+
* Maximum number of web searches Claude can perform during the conversation.
|
|
59
|
+
*/
|
|
60
|
+
maxUses: z.number().optional(),
|
|
61
|
+
/**
|
|
62
|
+
* Optional list of domains that Claude is allowed to search.
|
|
63
|
+
*/
|
|
64
|
+
allowedDomains: z.array(z.string()).optional(),
|
|
65
|
+
/**
|
|
66
|
+
* Optional list of domains that Claude should avoid when searching.
|
|
67
|
+
*/
|
|
68
|
+
blockedDomains: z.array(z.string()).optional(),
|
|
69
|
+
/**
|
|
70
|
+
* Optional user location information to provide geographically relevant search results.
|
|
71
|
+
*/
|
|
72
|
+
userLocation: z.object({
|
|
73
|
+
type: z.literal("approximate"),
|
|
74
|
+
city: z.string().optional(),
|
|
75
|
+
region: z.string().optional(),
|
|
76
|
+
country: z.string().optional(),
|
|
77
|
+
timezone: z.string().optional()
|
|
78
|
+
}).optional()
|
|
79
|
+
});
|
|
80
|
+
var webSearch_20250305OutputSchema = z.array(
|
|
81
|
+
z.object({
|
|
82
|
+
url: z.string(),
|
|
83
|
+
title: z.string(),
|
|
84
|
+
pageAge: z.string().nullable(),
|
|
85
|
+
encryptedContent: z.string(),
|
|
86
|
+
type: z.string()
|
|
87
|
+
})
|
|
88
|
+
);
|
|
89
|
+
var factory = createProviderDefinedToolFactoryWithOutputSchema({
|
|
90
|
+
id: "anthropic.web_search_20250305",
|
|
91
|
+
name: "web_search",
|
|
92
|
+
inputSchema: z.object({
|
|
93
|
+
query: z.string()
|
|
94
|
+
}),
|
|
95
|
+
outputSchema: webSearch_20250305OutputSchema
|
|
96
|
+
});
|
|
97
|
+
var webSearch_20250305 = (args = {}) => {
|
|
98
|
+
return factory(args);
|
|
99
|
+
};
|
|
100
|
+
function isWebSearchTool(tool) {
|
|
101
|
+
return typeof tool === "object" && tool !== null && "type" in tool && tool.type === "web_search_20250305";
|
|
102
|
+
}
|
|
103
|
+
function prepareTools({
|
|
104
|
+
tools,
|
|
105
|
+
toolChoice,
|
|
106
|
+
disableParallelToolUse
|
|
107
|
+
}) {
|
|
108
|
+
tools = (tools == null ? void 0 : tools.length) ? tools : void 0;
|
|
109
|
+
const toolWarnings = [];
|
|
110
|
+
const betas = /* @__PURE__ */ new Set();
|
|
111
|
+
if (tools == null) {
|
|
112
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
|
|
113
|
+
}
|
|
114
|
+
const anthropicTools2 = [];
|
|
115
|
+
for (const tool of tools) {
|
|
116
|
+
if (isWebSearchTool(tool)) {
|
|
117
|
+
anthropicTools2.push(tool);
|
|
118
|
+
continue;
|
|
119
|
+
}
|
|
120
|
+
switch (tool.type) {
|
|
121
|
+
case "function":
|
|
122
|
+
const cacheControl = getCacheControl(tool.providerOptions);
|
|
123
|
+
anthropicTools2.push({
|
|
124
|
+
name: tool.name,
|
|
125
|
+
description: tool.description,
|
|
126
|
+
input_schema: tool.inputSchema,
|
|
127
|
+
cache_control: cacheControl
|
|
128
|
+
});
|
|
129
|
+
break;
|
|
130
|
+
case "provider-defined":
|
|
131
|
+
switch (tool.id) {
|
|
132
|
+
case "anthropic.computer_20250124":
|
|
133
|
+
betas.add("computer-use-2025-01-24");
|
|
134
|
+
anthropicTools2.push({
|
|
135
|
+
name: "computer",
|
|
136
|
+
type: "computer_20250124",
|
|
137
|
+
display_width_px: tool.args.displayWidthPx,
|
|
138
|
+
display_height_px: tool.args.displayHeightPx,
|
|
139
|
+
display_number: tool.args.displayNumber
|
|
140
|
+
});
|
|
141
|
+
break;
|
|
142
|
+
case "anthropic.computer_20241022":
|
|
143
|
+
betas.add("computer-use-2024-10-22");
|
|
144
|
+
anthropicTools2.push({
|
|
145
|
+
name: "computer",
|
|
146
|
+
type: "computer_20241022",
|
|
147
|
+
display_width_px: tool.args.displayWidthPx,
|
|
148
|
+
display_height_px: tool.args.displayHeightPx,
|
|
149
|
+
display_number: tool.args.displayNumber
|
|
150
|
+
});
|
|
151
|
+
break;
|
|
152
|
+
case "anthropic.text_editor_20250124":
|
|
153
|
+
betas.add("computer-use-2025-01-24");
|
|
154
|
+
anthropicTools2.push({
|
|
155
|
+
name: "str_replace_editor",
|
|
156
|
+
type: "text_editor_20250124"
|
|
157
|
+
});
|
|
158
|
+
break;
|
|
159
|
+
case "anthropic.text_editor_20241022":
|
|
160
|
+
betas.add("computer-use-2024-10-22");
|
|
161
|
+
anthropicTools2.push({
|
|
162
|
+
name: "str_replace_editor",
|
|
163
|
+
type: "text_editor_20241022"
|
|
164
|
+
});
|
|
165
|
+
break;
|
|
166
|
+
case "anthropic.text_editor_20250429":
|
|
167
|
+
betas.add("computer-use-2025-01-24");
|
|
168
|
+
anthropicTools2.push({
|
|
169
|
+
name: "str_replace_based_edit_tool",
|
|
170
|
+
type: "text_editor_20250429"
|
|
171
|
+
});
|
|
172
|
+
break;
|
|
173
|
+
case "anthropic.bash_20250124":
|
|
174
|
+
betas.add("computer-use-2025-01-24");
|
|
175
|
+
anthropicTools2.push({
|
|
176
|
+
name: "bash",
|
|
177
|
+
type: "bash_20250124"
|
|
178
|
+
});
|
|
179
|
+
break;
|
|
180
|
+
case "anthropic.bash_20241022":
|
|
181
|
+
betas.add("computer-use-2024-10-22");
|
|
182
|
+
anthropicTools2.push({
|
|
183
|
+
name: "bash",
|
|
184
|
+
type: "bash_20241022"
|
|
185
|
+
});
|
|
186
|
+
break;
|
|
187
|
+
case "anthropic.web_search_20250305": {
|
|
188
|
+
const args = webSearch_20250305ArgsSchema.parse(tool.args);
|
|
189
|
+
anthropicTools2.push({
|
|
190
|
+
type: "web_search_20250305",
|
|
191
|
+
name: "web_search",
|
|
192
|
+
max_uses: args.maxUses,
|
|
193
|
+
allowed_domains: args.allowedDomains,
|
|
194
|
+
blocked_domains: args.blockedDomains,
|
|
195
|
+
user_location: args.userLocation
|
|
196
|
+
});
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
case "anthropic.code_execution_20250522": {
|
|
200
|
+
betas.add("code-execution-2025-05-22");
|
|
201
|
+
anthropicTools2.push({
|
|
202
|
+
type: "code_execution_20250522",
|
|
203
|
+
name: "code_execution"
|
|
204
|
+
});
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
default:
|
|
208
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
209
|
+
break;
|
|
210
|
+
}
|
|
211
|
+
break;
|
|
212
|
+
default:
|
|
213
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
214
|
+
break;
|
|
215
|
+
}
|
|
216
|
+
}
|
|
217
|
+
if (toolChoice == null) {
|
|
218
|
+
return {
|
|
219
|
+
tools: anthropicTools2,
|
|
220
|
+
toolChoice: disableParallelToolUse ? { type: "auto", disable_parallel_tool_use: disableParallelToolUse } : void 0,
|
|
221
|
+
toolWarnings,
|
|
222
|
+
betas
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
const type = toolChoice.type;
|
|
226
|
+
switch (type) {
|
|
227
|
+
case "auto":
|
|
228
|
+
return {
|
|
229
|
+
tools: anthropicTools2,
|
|
230
|
+
toolChoice: {
|
|
231
|
+
type: "auto",
|
|
232
|
+
disable_parallel_tool_use: disableParallelToolUse
|
|
233
|
+
},
|
|
234
|
+
toolWarnings,
|
|
235
|
+
betas
|
|
236
|
+
};
|
|
237
|
+
case "required":
|
|
238
|
+
return {
|
|
239
|
+
tools: anthropicTools2,
|
|
240
|
+
toolChoice: {
|
|
241
|
+
type: "any",
|
|
242
|
+
disable_parallel_tool_use: disableParallelToolUse
|
|
243
|
+
},
|
|
244
|
+
toolWarnings,
|
|
245
|
+
betas
|
|
246
|
+
};
|
|
247
|
+
case "none":
|
|
248
|
+
return { tools: void 0, toolChoice: void 0, toolWarnings, betas };
|
|
249
|
+
case "tool":
|
|
250
|
+
return {
|
|
251
|
+
tools: anthropicTools2,
|
|
252
|
+
toolChoice: {
|
|
253
|
+
type: "tool",
|
|
254
|
+
name: toolChoice.toolName,
|
|
255
|
+
disable_parallel_tool_use: disableParallelToolUse
|
|
256
|
+
},
|
|
257
|
+
toolWarnings,
|
|
258
|
+
betas
|
|
259
|
+
};
|
|
260
|
+
default: {
|
|
261
|
+
const _exhaustiveCheck = type;
|
|
262
|
+
throw new UnsupportedFunctionalityError({
|
|
263
|
+
functionality: `tool choice type: ${_exhaustiveCheck}`
|
|
264
|
+
});
|
|
265
|
+
}
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
var codeExecution_20250522OutputSchema = z.object({
|
|
269
|
+
type: z.literal("code_execution_result"),
|
|
270
|
+
stdout: z.string(),
|
|
271
|
+
stderr: z.string(),
|
|
272
|
+
return_code: z.number()
|
|
273
|
+
});
|
|
274
|
+
var factory2 = createProviderDefinedToolFactoryWithOutputSchema({
|
|
275
|
+
id: "anthropic.code_execution_20250522",
|
|
276
|
+
name: "code_execution",
|
|
277
|
+
inputSchema: z.object({
|
|
278
|
+
code: z.string()
|
|
279
|
+
}),
|
|
280
|
+
outputSchema: codeExecution_20250522OutputSchema
|
|
281
|
+
});
|
|
282
|
+
var codeExecution_20250522 = (args = {}) => {
|
|
283
|
+
return factory2(args);
|
|
284
|
+
};
|
|
285
|
+
function convertToString(data) {
|
|
286
|
+
if (typeof data === "string") {
|
|
287
|
+
return Buffer.from(data, "base64").toString("utf-8");
|
|
288
|
+
}
|
|
289
|
+
if (data instanceof Uint8Array) {
|
|
290
|
+
return new TextDecoder().decode(data);
|
|
291
|
+
}
|
|
292
|
+
if (data instanceof URL) {
|
|
293
|
+
throw new UnsupportedFunctionalityError({
|
|
294
|
+
functionality: "URL-based text documents are not supported for citations"
|
|
295
|
+
});
|
|
296
|
+
}
|
|
297
|
+
throw new UnsupportedFunctionalityError({
|
|
298
|
+
functionality: `unsupported data type for text documents: ${typeof data}`
|
|
299
|
+
});
|
|
300
|
+
}
|
|
301
|
+
async function convertToAnthropicMessagesPrompt({
|
|
302
|
+
prompt,
|
|
303
|
+
sendReasoning,
|
|
304
|
+
warnings
|
|
305
|
+
}) {
|
|
306
|
+
var _a, _b, _c, _d, _e;
|
|
307
|
+
const betas = /* @__PURE__ */ new Set();
|
|
308
|
+
const blocks = groupIntoBlocks(prompt);
|
|
309
|
+
let system = void 0;
|
|
310
|
+
const messages = [];
|
|
311
|
+
async function shouldEnableCitations(providerMetadata) {
|
|
312
|
+
var _a2, _b2;
|
|
313
|
+
const anthropicOptions = await parseProviderOptions({
|
|
314
|
+
provider: "anthropic",
|
|
315
|
+
providerOptions: providerMetadata,
|
|
316
|
+
schema: anthropicFilePartProviderOptions
|
|
317
|
+
});
|
|
318
|
+
return (_b2 = (_a2 = anthropicOptions == null ? void 0 : anthropicOptions.citations) == null ? void 0 : _a2.enabled) != null ? _b2 : false;
|
|
319
|
+
}
|
|
320
|
+
async function getDocumentMetadata(providerMetadata) {
|
|
321
|
+
const anthropicOptions = await parseProviderOptions({
|
|
322
|
+
provider: "anthropic",
|
|
323
|
+
providerOptions: providerMetadata,
|
|
324
|
+
schema: anthropicFilePartProviderOptions
|
|
325
|
+
});
|
|
326
|
+
return {
|
|
327
|
+
title: anthropicOptions == null ? void 0 : anthropicOptions.title,
|
|
328
|
+
context: anthropicOptions == null ? void 0 : anthropicOptions.context
|
|
329
|
+
};
|
|
330
|
+
}
|
|
331
|
+
for (let i = 0; i < blocks.length; i++) {
|
|
332
|
+
const block = blocks[i];
|
|
333
|
+
const isLastBlock = i === blocks.length - 1;
|
|
334
|
+
const type = block.type;
|
|
335
|
+
switch (type) {
|
|
336
|
+
case "system": {
|
|
337
|
+
if (system != null) {
|
|
338
|
+
throw new UnsupportedFunctionalityError({
|
|
339
|
+
functionality: "Multiple system messages that are separated by user/assistant messages"
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
system = block.messages.map(({ content, providerOptions }) => ({
|
|
343
|
+
type: "text",
|
|
344
|
+
text: content,
|
|
345
|
+
cache_control: getCacheControl(providerOptions)
|
|
346
|
+
}));
|
|
347
|
+
break;
|
|
348
|
+
}
|
|
349
|
+
case "user": {
|
|
350
|
+
const anthropicContent = [];
|
|
351
|
+
for (const message of block.messages) {
|
|
352
|
+
const { role, content } = message;
|
|
353
|
+
switch (role) {
|
|
354
|
+
case "user": {
|
|
355
|
+
for (let j = 0; j < content.length; j++) {
|
|
356
|
+
const part = content[j];
|
|
357
|
+
const isLastPart = j === content.length - 1;
|
|
358
|
+
const cacheControl = (_a = getCacheControl(part.providerOptions)) != null ? _a : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
359
|
+
switch (part.type) {
|
|
360
|
+
case "text": {
|
|
361
|
+
anthropicContent.push({
|
|
362
|
+
type: "text",
|
|
363
|
+
text: part.text,
|
|
364
|
+
cache_control: cacheControl
|
|
365
|
+
});
|
|
366
|
+
break;
|
|
367
|
+
}
|
|
368
|
+
case "file": {
|
|
369
|
+
if (part.mediaType.startsWith("image/")) {
|
|
370
|
+
anthropicContent.push({
|
|
371
|
+
type: "image",
|
|
372
|
+
source: part.data instanceof URL ? {
|
|
373
|
+
type: "url",
|
|
374
|
+
url: part.data.toString()
|
|
375
|
+
} : {
|
|
376
|
+
type: "base64",
|
|
377
|
+
media_type: part.mediaType === "image/*" ? "image/jpeg" : part.mediaType,
|
|
378
|
+
data: convertToBase64(part.data)
|
|
379
|
+
},
|
|
380
|
+
cache_control: cacheControl
|
|
381
|
+
});
|
|
382
|
+
} else if (part.mediaType === "application/pdf") {
|
|
383
|
+
betas.add("pdfs-2024-09-25");
|
|
384
|
+
const enableCitations = await shouldEnableCitations(
|
|
385
|
+
part.providerOptions
|
|
386
|
+
);
|
|
387
|
+
const metadata = await getDocumentMetadata(
|
|
388
|
+
part.providerOptions
|
|
389
|
+
);
|
|
390
|
+
anthropicContent.push({
|
|
391
|
+
type: "document",
|
|
392
|
+
source: part.data instanceof URL ? {
|
|
393
|
+
type: "url",
|
|
394
|
+
url: part.data.toString()
|
|
395
|
+
} : {
|
|
396
|
+
type: "base64",
|
|
397
|
+
media_type: "application/pdf",
|
|
398
|
+
data: convertToBase64(part.data)
|
|
399
|
+
},
|
|
400
|
+
title: (_b = metadata.title) != null ? _b : part.filename,
|
|
401
|
+
...metadata.context && { context: metadata.context },
|
|
402
|
+
...enableCitations && {
|
|
403
|
+
citations: { enabled: true }
|
|
404
|
+
},
|
|
405
|
+
cache_control: cacheControl
|
|
406
|
+
});
|
|
407
|
+
} else if (part.mediaType === "text/plain") {
|
|
408
|
+
const enableCitations = await shouldEnableCitations(
|
|
409
|
+
part.providerOptions
|
|
410
|
+
);
|
|
411
|
+
const metadata = await getDocumentMetadata(
|
|
412
|
+
part.providerOptions
|
|
413
|
+
);
|
|
414
|
+
anthropicContent.push({
|
|
415
|
+
type: "document",
|
|
416
|
+
source: part.data instanceof URL ? {
|
|
417
|
+
type: "url",
|
|
418
|
+
url: part.data.toString()
|
|
419
|
+
} : {
|
|
420
|
+
type: "text",
|
|
421
|
+
media_type: "text/plain",
|
|
422
|
+
data: convertToString(part.data)
|
|
423
|
+
},
|
|
424
|
+
title: (_c = metadata.title) != null ? _c : part.filename,
|
|
425
|
+
...metadata.context && { context: metadata.context },
|
|
426
|
+
...enableCitations && {
|
|
427
|
+
citations: { enabled: true }
|
|
428
|
+
},
|
|
429
|
+
cache_control: cacheControl
|
|
430
|
+
});
|
|
431
|
+
} else {
|
|
432
|
+
throw new UnsupportedFunctionalityError({
|
|
433
|
+
functionality: `media type: ${part.mediaType}`
|
|
434
|
+
});
|
|
435
|
+
}
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
break;
|
|
441
|
+
}
|
|
442
|
+
case "tool": {
|
|
443
|
+
for (let i2 = 0; i2 < content.length; i2++) {
|
|
444
|
+
const part = content[i2];
|
|
445
|
+
const isLastPart = i2 === content.length - 1;
|
|
446
|
+
const cacheControl = (_d = getCacheControl(part.providerOptions)) != null ? _d : isLastPart ? getCacheControl(message.providerOptions) : void 0;
|
|
447
|
+
const output = part.output;
|
|
448
|
+
let contentValue;
|
|
449
|
+
switch (output.type) {
|
|
450
|
+
case "content":
|
|
451
|
+
contentValue = output.value.map((contentPart) => {
|
|
452
|
+
switch (contentPart.type) {
|
|
453
|
+
case "text":
|
|
454
|
+
return {
|
|
455
|
+
type: "text",
|
|
456
|
+
text: contentPart.text,
|
|
457
|
+
cache_control: void 0
|
|
458
|
+
};
|
|
459
|
+
case "media": {
|
|
460
|
+
if (contentPart.mediaType.startsWith("image/")) {
|
|
461
|
+
return {
|
|
462
|
+
type: "image",
|
|
463
|
+
source: {
|
|
464
|
+
type: "base64",
|
|
465
|
+
media_type: contentPart.mediaType,
|
|
466
|
+
data: contentPart.data
|
|
467
|
+
},
|
|
468
|
+
cache_control: void 0
|
|
469
|
+
};
|
|
470
|
+
}
|
|
471
|
+
throw new UnsupportedFunctionalityError({
|
|
472
|
+
functionality: `media type: ${contentPart.mediaType}`
|
|
473
|
+
});
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
});
|
|
477
|
+
break;
|
|
478
|
+
case "text":
|
|
479
|
+
case "error-text":
|
|
480
|
+
contentValue = output.value;
|
|
481
|
+
break;
|
|
482
|
+
case "json":
|
|
483
|
+
case "error-json":
|
|
484
|
+
default:
|
|
485
|
+
contentValue = JSON.stringify(output.value);
|
|
486
|
+
break;
|
|
487
|
+
}
|
|
488
|
+
anthropicContent.push({
|
|
489
|
+
type: "tool_result",
|
|
490
|
+
tool_use_id: part.toolCallId,
|
|
491
|
+
content: contentValue,
|
|
492
|
+
is_error: output.type === "error-text" || output.type === "error-json" ? true : void 0,
|
|
493
|
+
cache_control: cacheControl
|
|
494
|
+
});
|
|
495
|
+
}
|
|
496
|
+
break;
|
|
497
|
+
}
|
|
498
|
+
default: {
|
|
499
|
+
const _exhaustiveCheck = role;
|
|
500
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
messages.push({ role: "user", content: anthropicContent });
|
|
505
|
+
break;
|
|
506
|
+
}
|
|
507
|
+
case "assistant": {
|
|
508
|
+
const anthropicContent = [];
|
|
509
|
+
for (let j = 0; j < block.messages.length; j++) {
|
|
510
|
+
const message = block.messages[j];
|
|
511
|
+
const isLastMessage = j === block.messages.length - 1;
|
|
512
|
+
const { content } = message;
|
|
513
|
+
for (let k = 0; k < content.length; k++) {
|
|
514
|
+
const part = content[k];
|
|
515
|
+
const isLastContentPart = k === content.length - 1;
|
|
516
|
+
const cacheControl = (_e = getCacheControl(part.providerOptions)) != null ? _e : isLastContentPart ? getCacheControl(message.providerOptions) : void 0;
|
|
517
|
+
switch (part.type) {
|
|
518
|
+
case "text": {
|
|
519
|
+
anthropicContent.push({
|
|
520
|
+
type: "text",
|
|
521
|
+
text: (
|
|
522
|
+
// trim the last text part if it's the last message in the block
|
|
523
|
+
// because Anthropic does not allow trailing whitespace
|
|
524
|
+
// in pre-filled assistant responses
|
|
525
|
+
isLastBlock && isLastMessage && isLastContentPart ? part.text.trim() : part.text
|
|
526
|
+
),
|
|
527
|
+
cache_control: cacheControl
|
|
528
|
+
});
|
|
529
|
+
break;
|
|
530
|
+
}
|
|
531
|
+
case "reasoning": {
|
|
532
|
+
if (sendReasoning) {
|
|
533
|
+
const reasoningMetadata = await parseProviderOptions({
|
|
534
|
+
provider: "anthropic",
|
|
535
|
+
providerOptions: part.providerOptions,
|
|
536
|
+
schema: anthropicReasoningMetadataSchema
|
|
537
|
+
});
|
|
538
|
+
if (reasoningMetadata != null) {
|
|
539
|
+
if (reasoningMetadata.signature != null) {
|
|
540
|
+
anthropicContent.push({
|
|
541
|
+
type: "thinking",
|
|
542
|
+
thinking: part.text,
|
|
543
|
+
signature: reasoningMetadata.signature,
|
|
544
|
+
cache_control: cacheControl
|
|
545
|
+
});
|
|
546
|
+
} else if (reasoningMetadata.redactedData != null) {
|
|
547
|
+
anthropicContent.push({
|
|
548
|
+
type: "redacted_thinking",
|
|
549
|
+
data: reasoningMetadata.redactedData,
|
|
550
|
+
cache_control: cacheControl
|
|
551
|
+
});
|
|
552
|
+
} else {
|
|
553
|
+
warnings.push({
|
|
554
|
+
type: "other",
|
|
555
|
+
message: "unsupported reasoning metadata"
|
|
556
|
+
});
|
|
557
|
+
}
|
|
558
|
+
} else {
|
|
559
|
+
warnings.push({
|
|
560
|
+
type: "other",
|
|
561
|
+
message: "unsupported reasoning metadata"
|
|
562
|
+
});
|
|
563
|
+
}
|
|
564
|
+
} else {
|
|
565
|
+
warnings.push({
|
|
566
|
+
type: "other",
|
|
567
|
+
message: "sending reasoning content is disabled for this model"
|
|
568
|
+
});
|
|
569
|
+
}
|
|
570
|
+
break;
|
|
571
|
+
}
|
|
572
|
+
case "tool-call": {
|
|
573
|
+
if (part.providerExecuted) {
|
|
574
|
+
if (part.toolName === "web_search") {
|
|
575
|
+
anthropicContent.push({
|
|
576
|
+
type: "server_tool_use",
|
|
577
|
+
id: part.toolCallId,
|
|
578
|
+
name: "web_search",
|
|
579
|
+
input: part.input,
|
|
580
|
+
cache_control: cacheControl
|
|
581
|
+
});
|
|
582
|
+
break;
|
|
583
|
+
}
|
|
584
|
+
if (part.toolName === "code_execution") {
|
|
585
|
+
anthropicContent.push({
|
|
586
|
+
type: "server_tool_use",
|
|
587
|
+
id: part.toolCallId,
|
|
588
|
+
name: "code_execution",
|
|
589
|
+
input: part.input,
|
|
590
|
+
cache_control: cacheControl
|
|
591
|
+
});
|
|
592
|
+
break;
|
|
593
|
+
}
|
|
594
|
+
warnings.push({
|
|
595
|
+
type: "other",
|
|
596
|
+
message: `provider executed tool call for tool ${part.toolName} is not supported`
|
|
597
|
+
});
|
|
598
|
+
break;
|
|
599
|
+
}
|
|
600
|
+
anthropicContent.push({
|
|
601
|
+
type: "tool_use",
|
|
602
|
+
id: part.toolCallId,
|
|
603
|
+
name: part.toolName,
|
|
604
|
+
input: part.input,
|
|
605
|
+
cache_control: cacheControl
|
|
606
|
+
});
|
|
607
|
+
break;
|
|
608
|
+
}
|
|
609
|
+
case "tool-result": {
|
|
610
|
+
if (part.toolName === "web_search") {
|
|
611
|
+
const output = part.output;
|
|
612
|
+
if (output.type !== "json") {
|
|
613
|
+
warnings.push({
|
|
614
|
+
type: "other",
|
|
615
|
+
message: `provider executed tool result output type ${output.type} for tool ${part.toolName} is not supported`
|
|
616
|
+
});
|
|
617
|
+
break;
|
|
618
|
+
}
|
|
619
|
+
const webSearchOutput = webSearch_20250305OutputSchema.parse(
|
|
620
|
+
output.value
|
|
621
|
+
);
|
|
622
|
+
anthropicContent.push({
|
|
623
|
+
type: "web_search_tool_result",
|
|
624
|
+
tool_use_id: part.toolCallId,
|
|
625
|
+
content: webSearchOutput.map((result) => ({
|
|
626
|
+
url: result.url,
|
|
627
|
+
title: result.title,
|
|
628
|
+
page_age: result.pageAge,
|
|
629
|
+
encrypted_content: result.encryptedContent,
|
|
630
|
+
type: result.type
|
|
631
|
+
})),
|
|
632
|
+
cache_control: cacheControl
|
|
633
|
+
});
|
|
634
|
+
break;
|
|
635
|
+
}
|
|
636
|
+
if (part.toolName === "code_execution") {
|
|
637
|
+
const output = part.output;
|
|
638
|
+
if (output.type !== "json") {
|
|
639
|
+
warnings.push({
|
|
640
|
+
type: "other",
|
|
641
|
+
message: `provider executed tool result output type ${output.type} for tool ${part.toolName} is not supported`
|
|
642
|
+
});
|
|
643
|
+
break;
|
|
644
|
+
}
|
|
645
|
+
const codeExecutionOutput = codeExecution_20250522OutputSchema.parse(output.value);
|
|
646
|
+
anthropicContent.push({
|
|
647
|
+
type: "code_execution_tool_result",
|
|
648
|
+
tool_use_id: part.toolCallId,
|
|
649
|
+
content: {
|
|
650
|
+
type: codeExecutionOutput.type,
|
|
651
|
+
stdout: codeExecutionOutput.stdout,
|
|
652
|
+
stderr: codeExecutionOutput.stderr,
|
|
653
|
+
return_code: codeExecutionOutput.return_code
|
|
654
|
+
},
|
|
655
|
+
cache_control: cacheControl
|
|
656
|
+
});
|
|
657
|
+
break;
|
|
658
|
+
}
|
|
659
|
+
warnings.push({
|
|
660
|
+
type: "other",
|
|
661
|
+
message: `provider executed tool result for tool ${part.toolName} is not supported`
|
|
662
|
+
});
|
|
663
|
+
break;
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
}
|
|
667
|
+
}
|
|
668
|
+
messages.push({ role: "assistant", content: anthropicContent });
|
|
669
|
+
break;
|
|
670
|
+
}
|
|
671
|
+
default: {
|
|
672
|
+
const _exhaustiveCheck = type;
|
|
673
|
+
throw new Error(`content type: ${_exhaustiveCheck}`);
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
}
|
|
677
|
+
return {
|
|
678
|
+
prompt: { system, messages },
|
|
679
|
+
betas
|
|
680
|
+
};
|
|
681
|
+
}
|
|
682
|
+
function groupIntoBlocks(prompt) {
|
|
683
|
+
const blocks = [];
|
|
684
|
+
let currentBlock = void 0;
|
|
685
|
+
for (const message of prompt) {
|
|
686
|
+
const { role } = message;
|
|
687
|
+
switch (role) {
|
|
688
|
+
case "system": {
|
|
689
|
+
if ((currentBlock == null ? void 0 : currentBlock.type) !== "system") {
|
|
690
|
+
currentBlock = { type: "system", messages: [] };
|
|
691
|
+
blocks.push(currentBlock);
|
|
692
|
+
}
|
|
693
|
+
currentBlock.messages.push(message);
|
|
694
|
+
break;
|
|
695
|
+
}
|
|
696
|
+
case "assistant": {
|
|
697
|
+
if ((currentBlock == null ? void 0 : currentBlock.type) !== "assistant") {
|
|
698
|
+
currentBlock = { type: "assistant", messages: [] };
|
|
699
|
+
blocks.push(currentBlock);
|
|
700
|
+
}
|
|
701
|
+
currentBlock.messages.push(message);
|
|
702
|
+
break;
|
|
703
|
+
}
|
|
704
|
+
case "user": {
|
|
705
|
+
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") {
|
|
706
|
+
currentBlock = { type: "user", messages: [] };
|
|
707
|
+
blocks.push(currentBlock);
|
|
708
|
+
}
|
|
709
|
+
currentBlock.messages.push(message);
|
|
710
|
+
break;
|
|
711
|
+
}
|
|
712
|
+
case "tool": {
|
|
713
|
+
if ((currentBlock == null ? void 0 : currentBlock.type) !== "user") {
|
|
714
|
+
currentBlock = { type: "user", messages: [] };
|
|
715
|
+
blocks.push(currentBlock);
|
|
716
|
+
}
|
|
717
|
+
currentBlock.messages.push(message);
|
|
718
|
+
break;
|
|
719
|
+
}
|
|
720
|
+
default: {
|
|
721
|
+
const _exhaustiveCheck = role;
|
|
722
|
+
throw new Error(`Unsupported role: ${_exhaustiveCheck}`);
|
|
723
|
+
}
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
return blocks;
|
|
727
|
+
}
|
|
728
|
+
function mapAnthropicStopReason({
|
|
729
|
+
finishReason,
|
|
730
|
+
isJsonResponseFromTool
|
|
731
|
+
}) {
|
|
732
|
+
switch (finishReason) {
|
|
733
|
+
case "end_turn":
|
|
734
|
+
case "stop_sequence":
|
|
735
|
+
return "stop";
|
|
736
|
+
case "tool_use":
|
|
737
|
+
return isJsonResponseFromTool ? "stop" : "tool-calls";
|
|
738
|
+
case "max_tokens":
|
|
739
|
+
return "length";
|
|
740
|
+
default:
|
|
741
|
+
return "unknown";
|
|
742
|
+
}
|
|
743
|
+
}
|
|
744
|
+
var citationSchemas = {
|
|
745
|
+
webSearchResult: z.object({
|
|
746
|
+
type: z.literal("web_search_result_location"),
|
|
747
|
+
cited_text: z.string(),
|
|
748
|
+
url: z.string(),
|
|
749
|
+
title: z.string(),
|
|
750
|
+
encrypted_index: z.string()
|
|
751
|
+
}),
|
|
752
|
+
pageLocation: z.object({
|
|
753
|
+
type: z.literal("page_location"),
|
|
754
|
+
cited_text: z.string(),
|
|
755
|
+
document_index: z.number(),
|
|
756
|
+
document_title: z.string().nullable(),
|
|
757
|
+
start_page_number: z.number(),
|
|
758
|
+
end_page_number: z.number()
|
|
759
|
+
}),
|
|
760
|
+
charLocation: z.object({
|
|
761
|
+
type: z.literal("char_location"),
|
|
762
|
+
cited_text: z.string(),
|
|
763
|
+
document_index: z.number(),
|
|
764
|
+
document_title: z.string().nullable(),
|
|
765
|
+
start_char_index: z.number(),
|
|
766
|
+
end_char_index: z.number()
|
|
767
|
+
})
|
|
768
|
+
};
|
|
769
|
+
var citationSchema = z.discriminatedUnion("type", [
|
|
770
|
+
citationSchemas.webSearchResult,
|
|
771
|
+
citationSchemas.pageLocation,
|
|
772
|
+
citationSchemas.charLocation
|
|
773
|
+
]);
|
|
774
|
+
z.discriminatedUnion("type", [
|
|
775
|
+
citationSchemas.pageLocation,
|
|
776
|
+
citationSchemas.charLocation
|
|
777
|
+
]);
|
|
778
|
+
function processCitation(citation, citationDocuments, generateId3, onSource) {
|
|
779
|
+
if (citation.type === "page_location" || citation.type === "char_location") {
|
|
780
|
+
const source = createCitationSource(
|
|
781
|
+
citation,
|
|
782
|
+
citationDocuments,
|
|
783
|
+
generateId3
|
|
784
|
+
);
|
|
785
|
+
if (source) {
|
|
786
|
+
onSource(source);
|
|
787
|
+
}
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
function createCitationSource(citation, citationDocuments, generateId3) {
|
|
791
|
+
var _a;
|
|
792
|
+
const documentInfo = citationDocuments[citation.document_index];
|
|
793
|
+
if (!documentInfo) {
|
|
794
|
+
return null;
|
|
795
|
+
}
|
|
796
|
+
const providerMetadata = citation.type === "page_location" ? {
|
|
797
|
+
citedText: citation.cited_text,
|
|
798
|
+
startPageNumber: citation.start_page_number,
|
|
799
|
+
endPageNumber: citation.end_page_number
|
|
800
|
+
} : {
|
|
801
|
+
citedText: citation.cited_text,
|
|
802
|
+
startCharIndex: citation.start_char_index,
|
|
803
|
+
endCharIndex: citation.end_char_index
|
|
804
|
+
};
|
|
805
|
+
return {
|
|
806
|
+
type: "source",
|
|
807
|
+
sourceType: "document",
|
|
808
|
+
id: generateId3(),
|
|
809
|
+
mediaType: documentInfo.mediaType,
|
|
810
|
+
title: (_a = citation.document_title) != null ? _a : documentInfo.title,
|
|
811
|
+
filename: documentInfo.filename,
|
|
812
|
+
providerMetadata: {
|
|
813
|
+
anthropic: providerMetadata
|
|
814
|
+
}
|
|
815
|
+
};
|
|
816
|
+
}
|
|
817
|
+
var AnthropicMessagesLanguageModel = class {
|
|
818
|
+
constructor(modelId, config) {
|
|
819
|
+
this.specificationVersion = "v2";
|
|
820
|
+
var _a;
|
|
821
|
+
this.modelId = modelId;
|
|
822
|
+
this.config = config;
|
|
823
|
+
this.generateId = (_a = config.generateId) != null ? _a : generateId;
|
|
824
|
+
}
|
|
825
|
+
supportsUrl(url) {
|
|
826
|
+
return url.protocol === "https:";
|
|
827
|
+
}
|
|
828
|
+
get provider() {
|
|
829
|
+
return this.config.provider;
|
|
830
|
+
}
|
|
831
|
+
get supportedUrls() {
|
|
832
|
+
var _a, _b, _c;
|
|
833
|
+
return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
|
|
834
|
+
}
|
|
835
|
+
async getArgs({
|
|
836
|
+
prompt,
|
|
837
|
+
maxOutputTokens = 4096,
|
|
838
|
+
// 4096: max model output tokens TODO update default in v5
|
|
839
|
+
temperature,
|
|
840
|
+
topP,
|
|
841
|
+
topK,
|
|
842
|
+
frequencyPenalty,
|
|
843
|
+
presencePenalty,
|
|
844
|
+
stopSequences,
|
|
845
|
+
responseFormat,
|
|
846
|
+
seed,
|
|
847
|
+
tools,
|
|
848
|
+
toolChoice,
|
|
849
|
+
providerOptions
|
|
850
|
+
}) {
|
|
851
|
+
var _a, _b, _c;
|
|
852
|
+
const warnings = [];
|
|
853
|
+
if (frequencyPenalty != null) {
|
|
854
|
+
warnings.push({
|
|
855
|
+
type: "unsupported-setting",
|
|
856
|
+
setting: "frequencyPenalty"
|
|
857
|
+
});
|
|
858
|
+
}
|
|
859
|
+
if (presencePenalty != null) {
|
|
860
|
+
warnings.push({
|
|
861
|
+
type: "unsupported-setting",
|
|
862
|
+
setting: "presencePenalty"
|
|
863
|
+
});
|
|
864
|
+
}
|
|
865
|
+
if (seed != null) {
|
|
866
|
+
warnings.push({
|
|
867
|
+
type: "unsupported-setting",
|
|
868
|
+
setting: "seed"
|
|
869
|
+
});
|
|
870
|
+
}
|
|
871
|
+
if ((responseFormat == null ? void 0 : responseFormat.type) === "json") {
|
|
872
|
+
if (responseFormat.schema == null) {
|
|
873
|
+
warnings.push({
|
|
874
|
+
type: "unsupported-setting",
|
|
875
|
+
setting: "responseFormat",
|
|
876
|
+
details: "JSON response format requires a schema. The response format is ignored."
|
|
877
|
+
});
|
|
878
|
+
} else if (tools != null) {
|
|
879
|
+
warnings.push({
|
|
880
|
+
type: "unsupported-setting",
|
|
881
|
+
setting: "tools",
|
|
882
|
+
details: "JSON response format does not support tools. The provided tools are ignored."
|
|
883
|
+
});
|
|
884
|
+
}
|
|
885
|
+
}
|
|
886
|
+
const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
|
|
887
|
+
type: "function",
|
|
888
|
+
name: "json",
|
|
889
|
+
description: "Respond with a JSON object.",
|
|
890
|
+
inputSchema: responseFormat.schema
|
|
891
|
+
} : void 0;
|
|
892
|
+
const anthropicOptions = await parseProviderOptions({
|
|
893
|
+
provider: "anthropic",
|
|
894
|
+
providerOptions,
|
|
895
|
+
schema: anthropicProviderOptions
|
|
896
|
+
});
|
|
897
|
+
const { prompt: messagesPrompt, betas: messagesBetas } = await convertToAnthropicMessagesPrompt({
|
|
898
|
+
prompt,
|
|
899
|
+
sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
|
|
900
|
+
warnings
|
|
901
|
+
});
|
|
902
|
+
const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
|
|
903
|
+
const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
|
|
904
|
+
const baseArgs = {
|
|
905
|
+
// model id:
|
|
906
|
+
model: this.modelId,
|
|
907
|
+
// standardized settings:
|
|
908
|
+
max_tokens: maxOutputTokens,
|
|
909
|
+
temperature,
|
|
910
|
+
top_k: topK,
|
|
911
|
+
top_p: topP,
|
|
912
|
+
stop_sequences: stopSequences,
|
|
913
|
+
// provider specific settings:
|
|
914
|
+
...isThinking && {
|
|
915
|
+
thinking: { type: "enabled", budget_tokens: thinkingBudget }
|
|
916
|
+
},
|
|
917
|
+
// prompt:
|
|
918
|
+
system: messagesPrompt.system,
|
|
919
|
+
messages: messagesPrompt.messages
|
|
920
|
+
};
|
|
921
|
+
if (isThinking) {
|
|
922
|
+
if (thinkingBudget == null) {
|
|
923
|
+
throw new UnsupportedFunctionalityError({
|
|
924
|
+
functionality: "thinking requires a budget"
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
if (baseArgs.temperature != null) {
|
|
928
|
+
baseArgs.temperature = void 0;
|
|
929
|
+
warnings.push({
|
|
930
|
+
type: "unsupported-setting",
|
|
931
|
+
setting: "temperature",
|
|
932
|
+
details: "temperature is not supported when thinking is enabled"
|
|
933
|
+
});
|
|
934
|
+
}
|
|
935
|
+
if (topK != null) {
|
|
936
|
+
baseArgs.top_k = void 0;
|
|
937
|
+
warnings.push({
|
|
938
|
+
type: "unsupported-setting",
|
|
939
|
+
setting: "topK",
|
|
940
|
+
details: "topK is not supported when thinking is enabled"
|
|
941
|
+
});
|
|
942
|
+
}
|
|
943
|
+
if (topP != null) {
|
|
944
|
+
baseArgs.top_p = void 0;
|
|
945
|
+
warnings.push({
|
|
946
|
+
type: "unsupported-setting",
|
|
947
|
+
setting: "topP",
|
|
948
|
+
details: "topP is not supported when thinking is enabled"
|
|
949
|
+
});
|
|
950
|
+
}
|
|
951
|
+
baseArgs.max_tokens = maxOutputTokens + thinkingBudget;
|
|
952
|
+
}
|
|
953
|
+
const {
|
|
954
|
+
tools: anthropicTools2,
|
|
955
|
+
toolChoice: anthropicToolChoice,
|
|
956
|
+
toolWarnings,
|
|
957
|
+
betas: toolsBetas
|
|
958
|
+
} = prepareTools(
|
|
959
|
+
jsonResponseTool != null ? {
|
|
960
|
+
tools: [jsonResponseTool],
|
|
961
|
+
toolChoice: { type: "tool", toolName: jsonResponseTool.name },
|
|
962
|
+
disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
|
|
963
|
+
} : {
|
|
964
|
+
tools: tools != null ? tools : [],
|
|
965
|
+
toolChoice,
|
|
966
|
+
disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
|
|
967
|
+
}
|
|
968
|
+
);
|
|
969
|
+
return {
|
|
970
|
+
args: {
|
|
971
|
+
...baseArgs,
|
|
972
|
+
tools: anthropicTools2,
|
|
973
|
+
tool_choice: anthropicToolChoice
|
|
974
|
+
},
|
|
975
|
+
warnings: [...warnings, ...toolWarnings],
|
|
976
|
+
betas: /* @__PURE__ */ new Set([...messagesBetas, ...toolsBetas]),
|
|
977
|
+
usesJsonResponseTool: jsonResponseTool != null
|
|
978
|
+
};
|
|
979
|
+
}
|
|
980
|
+
async getHeaders({
|
|
981
|
+
betas,
|
|
982
|
+
headers
|
|
983
|
+
}) {
|
|
984
|
+
return combineHeaders(
|
|
985
|
+
await resolve(this.config.headers),
|
|
986
|
+
betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
|
|
987
|
+
headers
|
|
988
|
+
);
|
|
989
|
+
}
|
|
990
|
+
buildRequestUrl(isStreaming) {
|
|
991
|
+
var _a, _b, _c;
|
|
992
|
+
return (_c = (_b = (_a = this.config).buildRequestUrl) == null ? void 0 : _b.call(_a, this.config.baseURL, isStreaming)) != null ? _c : `${this.config.baseURL}/messages`;
|
|
993
|
+
}
|
|
994
|
+
transformRequestBody(args) {
|
|
995
|
+
var _a, _b, _c;
|
|
996
|
+
return (_c = (_b = (_a = this.config).transformRequestBody) == null ? void 0 : _b.call(_a, args)) != null ? _c : args;
|
|
997
|
+
}
|
|
998
|
+
extractCitationDocuments(prompt) {
|
|
999
|
+
const isCitationPart = (part) => {
|
|
1000
|
+
var _a, _b;
|
|
1001
|
+
if (part.type !== "file") {
|
|
1002
|
+
return false;
|
|
1003
|
+
}
|
|
1004
|
+
if (part.mediaType !== "application/pdf" && part.mediaType !== "text/plain") {
|
|
1005
|
+
return false;
|
|
1006
|
+
}
|
|
1007
|
+
const anthropic2 = (_a = part.providerOptions) == null ? void 0 : _a.anthropic;
|
|
1008
|
+
const citationsConfig = anthropic2 == null ? void 0 : anthropic2.citations;
|
|
1009
|
+
return (_b = citationsConfig == null ? void 0 : citationsConfig.enabled) != null ? _b : false;
|
|
1010
|
+
};
|
|
1011
|
+
return prompt.filter((message) => message.role === "user").flatMap((message) => message.content).filter(isCitationPart).map((part) => {
|
|
1012
|
+
var _a;
|
|
1013
|
+
const filePart = part;
|
|
1014
|
+
return {
|
|
1015
|
+
title: (_a = filePart.filename) != null ? _a : "Untitled Document",
|
|
1016
|
+
filename: filePart.filename,
|
|
1017
|
+
mediaType: filePart.mediaType
|
|
1018
|
+
};
|
|
1019
|
+
});
|
|
1020
|
+
}
|
|
1021
|
+
async doGenerate(options) {
|
|
1022
|
+
var _a, _b, _c, _d, _e;
|
|
1023
|
+
const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
|
|
1024
|
+
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
1025
|
+
const {
|
|
1026
|
+
responseHeaders,
|
|
1027
|
+
value: response,
|
|
1028
|
+
rawValue: rawResponse
|
|
1029
|
+
} = await postJsonToApi({
|
|
1030
|
+
url: this.buildRequestUrl(false),
|
|
1031
|
+
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
1032
|
+
body: this.transformRequestBody(args),
|
|
1033
|
+
failedResponseHandler: anthropicFailedResponseHandler,
|
|
1034
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
1035
|
+
anthropicMessagesResponseSchema
|
|
1036
|
+
),
|
|
1037
|
+
abortSignal: options.abortSignal,
|
|
1038
|
+
fetch: this.config.fetch
|
|
1039
|
+
});
|
|
1040
|
+
const content = [];
|
|
1041
|
+
for (const part of response.content) {
|
|
1042
|
+
switch (part.type) {
|
|
1043
|
+
case "text": {
|
|
1044
|
+
if (!usesJsonResponseTool) {
|
|
1045
|
+
content.push({ type: "text", text: part.text });
|
|
1046
|
+
if (part.citations) {
|
|
1047
|
+
for (const citation of part.citations) {
|
|
1048
|
+
processCitation(
|
|
1049
|
+
citation,
|
|
1050
|
+
citationDocuments,
|
|
1051
|
+
this.generateId,
|
|
1052
|
+
(source) => content.push(source)
|
|
1053
|
+
);
|
|
1054
|
+
}
|
|
1055
|
+
}
|
|
1056
|
+
}
|
|
1057
|
+
break;
|
|
1058
|
+
}
|
|
1059
|
+
case "thinking": {
|
|
1060
|
+
content.push({
|
|
1061
|
+
type: "reasoning",
|
|
1062
|
+
text: part.thinking,
|
|
1063
|
+
providerMetadata: {
|
|
1064
|
+
anthropic: {
|
|
1065
|
+
signature: part.signature
|
|
1066
|
+
}
|
|
1067
|
+
}
|
|
1068
|
+
});
|
|
1069
|
+
break;
|
|
1070
|
+
}
|
|
1071
|
+
case "redacted_thinking": {
|
|
1072
|
+
content.push({
|
|
1073
|
+
type: "reasoning",
|
|
1074
|
+
text: "",
|
|
1075
|
+
providerMetadata: {
|
|
1076
|
+
anthropic: {
|
|
1077
|
+
redactedData: part.data
|
|
1078
|
+
}
|
|
1079
|
+
}
|
|
1080
|
+
});
|
|
1081
|
+
break;
|
|
1082
|
+
}
|
|
1083
|
+
case "tool_use": {
|
|
1084
|
+
content.push(
|
|
1085
|
+
// when a json response tool is used, the tool call becomes the text:
|
|
1086
|
+
usesJsonResponseTool ? {
|
|
1087
|
+
type: "text",
|
|
1088
|
+
text: JSON.stringify(part.input)
|
|
1089
|
+
} : {
|
|
1090
|
+
type: "tool-call",
|
|
1091
|
+
toolCallId: part.id,
|
|
1092
|
+
toolName: part.name,
|
|
1093
|
+
input: JSON.stringify(part.input)
|
|
1094
|
+
}
|
|
1095
|
+
);
|
|
1096
|
+
break;
|
|
1097
|
+
}
|
|
1098
|
+
case "server_tool_use": {
|
|
1099
|
+
if (part.name === "web_search" || part.name === "code_execution") {
|
|
1100
|
+
content.push({
|
|
1101
|
+
type: "tool-call",
|
|
1102
|
+
toolCallId: part.id,
|
|
1103
|
+
toolName: part.name,
|
|
1104
|
+
input: JSON.stringify(part.input),
|
|
1105
|
+
providerExecuted: true
|
|
1106
|
+
});
|
|
1107
|
+
}
|
|
1108
|
+
break;
|
|
1109
|
+
}
|
|
1110
|
+
case "web_search_tool_result": {
|
|
1111
|
+
if (Array.isArray(part.content)) {
|
|
1112
|
+
content.push({
|
|
1113
|
+
type: "tool-result",
|
|
1114
|
+
toolCallId: part.tool_use_id,
|
|
1115
|
+
toolName: "web_search",
|
|
1116
|
+
result: part.content.map((result) => {
|
|
1117
|
+
var _a2;
|
|
1118
|
+
return {
|
|
1119
|
+
url: result.url,
|
|
1120
|
+
title: result.title,
|
|
1121
|
+
pageAge: (_a2 = result.page_age) != null ? _a2 : null,
|
|
1122
|
+
encryptedContent: result.encrypted_content,
|
|
1123
|
+
type: result.type
|
|
1124
|
+
};
|
|
1125
|
+
}),
|
|
1126
|
+
providerExecuted: true
|
|
1127
|
+
});
|
|
1128
|
+
for (const result of part.content) {
|
|
1129
|
+
content.push({
|
|
1130
|
+
type: "source",
|
|
1131
|
+
sourceType: "url",
|
|
1132
|
+
id: this.generateId(),
|
|
1133
|
+
url: result.url,
|
|
1134
|
+
title: result.title,
|
|
1135
|
+
providerMetadata: {
|
|
1136
|
+
anthropic: {
|
|
1137
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
1138
|
+
}
|
|
1139
|
+
}
|
|
1140
|
+
});
|
|
1141
|
+
}
|
|
1142
|
+
} else {
|
|
1143
|
+
content.push({
|
|
1144
|
+
type: "tool-result",
|
|
1145
|
+
toolCallId: part.tool_use_id,
|
|
1146
|
+
toolName: "web_search",
|
|
1147
|
+
isError: true,
|
|
1148
|
+
result: {
|
|
1149
|
+
type: "web_search_tool_result_error",
|
|
1150
|
+
errorCode: part.content.error_code
|
|
1151
|
+
},
|
|
1152
|
+
providerExecuted: true
|
|
1153
|
+
});
|
|
1154
|
+
}
|
|
1155
|
+
break;
|
|
1156
|
+
}
|
|
1157
|
+
case "code_execution_tool_result": {
|
|
1158
|
+
if (part.content.type === "code_execution_result") {
|
|
1159
|
+
content.push({
|
|
1160
|
+
type: "tool-result",
|
|
1161
|
+
toolCallId: part.tool_use_id,
|
|
1162
|
+
toolName: "code_execution",
|
|
1163
|
+
result: {
|
|
1164
|
+
type: part.content.type,
|
|
1165
|
+
stdout: part.content.stdout,
|
|
1166
|
+
stderr: part.content.stderr,
|
|
1167
|
+
return_code: part.content.return_code
|
|
1168
|
+
},
|
|
1169
|
+
providerExecuted: true
|
|
1170
|
+
});
|
|
1171
|
+
} else if (part.content.type === "code_execution_tool_result_error") {
|
|
1172
|
+
content.push({
|
|
1173
|
+
type: "tool-result",
|
|
1174
|
+
toolCallId: part.tool_use_id,
|
|
1175
|
+
toolName: "code_execution",
|
|
1176
|
+
isError: true,
|
|
1177
|
+
result: {
|
|
1178
|
+
type: "code_execution_tool_result_error",
|
|
1179
|
+
errorCode: part.content.error_code
|
|
1180
|
+
},
|
|
1181
|
+
providerExecuted: true
|
|
1182
|
+
});
|
|
1183
|
+
}
|
|
1184
|
+
break;
|
|
1185
|
+
}
|
|
1186
|
+
}
|
|
1187
|
+
}
|
|
1188
|
+
return {
|
|
1189
|
+
content,
|
|
1190
|
+
finishReason: mapAnthropicStopReason({
|
|
1191
|
+
finishReason: response.stop_reason,
|
|
1192
|
+
isJsonResponseFromTool: usesJsonResponseTool
|
|
1193
|
+
}),
|
|
1194
|
+
usage: {
|
|
1195
|
+
inputTokens: response.usage.input_tokens,
|
|
1196
|
+
outputTokens: response.usage.output_tokens,
|
|
1197
|
+
totalTokens: response.usage.input_tokens + response.usage.output_tokens,
|
|
1198
|
+
cachedInputTokens: (_b = response.usage.cache_read_input_tokens) != null ? _b : void 0
|
|
1199
|
+
},
|
|
1200
|
+
request: { body: args },
|
|
1201
|
+
response: {
|
|
1202
|
+
id: (_c = response.id) != null ? _c : void 0,
|
|
1203
|
+
modelId: (_d = response.model) != null ? _d : void 0,
|
|
1204
|
+
headers: responseHeaders,
|
|
1205
|
+
body: rawResponse
|
|
1206
|
+
},
|
|
1207
|
+
warnings,
|
|
1208
|
+
providerMetadata: {
|
|
1209
|
+
anthropic: {
|
|
1210
|
+
usage: response.usage,
|
|
1211
|
+
cacheCreationInputTokens: (_e = response.usage.cache_creation_input_tokens) != null ? _e : null
|
|
1212
|
+
}
|
|
1213
|
+
}
|
|
1214
|
+
};
|
|
1215
|
+
}
|
|
1216
|
+
async doStream(options) {
|
|
1217
|
+
const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
|
|
1218
|
+
const citationDocuments = this.extractCitationDocuments(options.prompt);
|
|
1219
|
+
const body = { ...args, stream: true };
|
|
1220
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
1221
|
+
url: this.buildRequestUrl(true),
|
|
1222
|
+
headers: await this.getHeaders({ betas, headers: options.headers }),
|
|
1223
|
+
body: this.transformRequestBody(body),
|
|
1224
|
+
failedResponseHandler: anthropicFailedResponseHandler,
|
|
1225
|
+
successfulResponseHandler: createEventSourceResponseHandler(
|
|
1226
|
+
anthropicMessagesChunkSchema
|
|
1227
|
+
),
|
|
1228
|
+
abortSignal: options.abortSignal,
|
|
1229
|
+
fetch: this.config.fetch
|
|
1230
|
+
});
|
|
1231
|
+
let finishReason = "unknown";
|
|
1232
|
+
const usage = {
|
|
1233
|
+
inputTokens: void 0,
|
|
1234
|
+
outputTokens: void 0,
|
|
1235
|
+
totalTokens: void 0
|
|
1236
|
+
};
|
|
1237
|
+
const contentBlocks = {};
|
|
1238
|
+
let providerMetadata = void 0;
|
|
1239
|
+
let blockType = void 0;
|
|
1240
|
+
const generateId3 = this.generateId;
|
|
1241
|
+
return {
|
|
1242
|
+
stream: response.pipeThrough(
|
|
1243
|
+
new TransformStream({
|
|
1244
|
+
start(controller) {
|
|
1245
|
+
controller.enqueue({ type: "stream-start", warnings });
|
|
1246
|
+
},
|
|
1247
|
+
transform(chunk, controller) {
|
|
1248
|
+
var _a, _b, _c, _d, _e, _f, _g;
|
|
1249
|
+
if (options.includeRawChunks) {
|
|
1250
|
+
controller.enqueue({ type: "raw", rawValue: chunk.rawValue });
|
|
1251
|
+
}
|
|
1252
|
+
if (!chunk.success) {
|
|
1253
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
1254
|
+
return;
|
|
1255
|
+
}
|
|
1256
|
+
const value = chunk.value;
|
|
1257
|
+
switch (value.type) {
|
|
1258
|
+
case "ping": {
|
|
1259
|
+
return;
|
|
1260
|
+
}
|
|
1261
|
+
case "content_block_start": {
|
|
1262
|
+
const contentBlockType = value.content_block.type;
|
|
1263
|
+
blockType = contentBlockType;
|
|
1264
|
+
switch (contentBlockType) {
|
|
1265
|
+
case "text": {
|
|
1266
|
+
contentBlocks[value.index] = { type: "text" };
|
|
1267
|
+
controller.enqueue({
|
|
1268
|
+
type: "text-start",
|
|
1269
|
+
id: String(value.index)
|
|
1270
|
+
});
|
|
1271
|
+
return;
|
|
1272
|
+
}
|
|
1273
|
+
case "thinking": {
|
|
1274
|
+
contentBlocks[value.index] = { type: "reasoning" };
|
|
1275
|
+
controller.enqueue({
|
|
1276
|
+
type: "reasoning-start",
|
|
1277
|
+
id: String(value.index)
|
|
1278
|
+
});
|
|
1279
|
+
return;
|
|
1280
|
+
}
|
|
1281
|
+
case "redacted_thinking": {
|
|
1282
|
+
contentBlocks[value.index] = { type: "reasoning" };
|
|
1283
|
+
controller.enqueue({
|
|
1284
|
+
type: "reasoning-start",
|
|
1285
|
+
id: String(value.index),
|
|
1286
|
+
providerMetadata: {
|
|
1287
|
+
anthropic: {
|
|
1288
|
+
redactedData: value.content_block.data
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
});
|
|
1292
|
+
return;
|
|
1293
|
+
}
|
|
1294
|
+
case "tool_use": {
|
|
1295
|
+
contentBlocks[value.index] = usesJsonResponseTool ? { type: "text" } : {
|
|
1296
|
+
type: "tool-call",
|
|
1297
|
+
toolCallId: value.content_block.id,
|
|
1298
|
+
toolName: value.content_block.name,
|
|
1299
|
+
input: ""
|
|
1300
|
+
};
|
|
1301
|
+
controller.enqueue(
|
|
1302
|
+
usesJsonResponseTool ? { type: "text-start", id: String(value.index) } : {
|
|
1303
|
+
type: "tool-input-start",
|
|
1304
|
+
id: value.content_block.id,
|
|
1305
|
+
toolName: value.content_block.name
|
|
1306
|
+
}
|
|
1307
|
+
);
|
|
1308
|
+
return;
|
|
1309
|
+
}
|
|
1310
|
+
case "server_tool_use": {
|
|
1311
|
+
if (value.content_block.name === "web_search" || value.content_block.name === "code_execution") {
|
|
1312
|
+
contentBlocks[value.index] = {
|
|
1313
|
+
type: "tool-call",
|
|
1314
|
+
toolCallId: value.content_block.id,
|
|
1315
|
+
toolName: value.content_block.name,
|
|
1316
|
+
input: "",
|
|
1317
|
+
providerExecuted: true
|
|
1318
|
+
};
|
|
1319
|
+
controller.enqueue({
|
|
1320
|
+
type: "tool-input-start",
|
|
1321
|
+
id: value.content_block.id,
|
|
1322
|
+
toolName: value.content_block.name,
|
|
1323
|
+
providerExecuted: true
|
|
1324
|
+
});
|
|
1325
|
+
}
|
|
1326
|
+
return;
|
|
1327
|
+
}
|
|
1328
|
+
case "web_search_tool_result": {
|
|
1329
|
+
const part = value.content_block;
|
|
1330
|
+
if (Array.isArray(part.content)) {
|
|
1331
|
+
controller.enqueue({
|
|
1332
|
+
type: "tool-result",
|
|
1333
|
+
toolCallId: part.tool_use_id,
|
|
1334
|
+
toolName: "web_search",
|
|
1335
|
+
result: part.content.map((result) => {
|
|
1336
|
+
var _a2;
|
|
1337
|
+
return {
|
|
1338
|
+
url: result.url,
|
|
1339
|
+
title: result.title,
|
|
1340
|
+
pageAge: (_a2 = result.page_age) != null ? _a2 : null,
|
|
1341
|
+
encryptedContent: result.encrypted_content,
|
|
1342
|
+
type: result.type
|
|
1343
|
+
};
|
|
1344
|
+
}),
|
|
1345
|
+
providerExecuted: true
|
|
1346
|
+
});
|
|
1347
|
+
for (const result of part.content) {
|
|
1348
|
+
controller.enqueue({
|
|
1349
|
+
type: "source",
|
|
1350
|
+
sourceType: "url",
|
|
1351
|
+
id: generateId3(),
|
|
1352
|
+
url: result.url,
|
|
1353
|
+
title: result.title,
|
|
1354
|
+
providerMetadata: {
|
|
1355
|
+
anthropic: {
|
|
1356
|
+
pageAge: (_a = result.page_age) != null ? _a : null
|
|
1357
|
+
}
|
|
1358
|
+
}
|
|
1359
|
+
});
|
|
1360
|
+
}
|
|
1361
|
+
} else {
|
|
1362
|
+
controller.enqueue({
|
|
1363
|
+
type: "tool-result",
|
|
1364
|
+
toolCallId: part.tool_use_id,
|
|
1365
|
+
toolName: "web_search",
|
|
1366
|
+
isError: true,
|
|
1367
|
+
result: {
|
|
1368
|
+
type: "web_search_tool_result_error",
|
|
1369
|
+
errorCode: part.content.error_code
|
|
1370
|
+
},
|
|
1371
|
+
providerExecuted: true
|
|
1372
|
+
});
|
|
1373
|
+
}
|
|
1374
|
+
return;
|
|
1375
|
+
}
|
|
1376
|
+
case "code_execution_tool_result": {
|
|
1377
|
+
const part = value.content_block;
|
|
1378
|
+
if (part.content.type === "code_execution_result") {
|
|
1379
|
+
controller.enqueue({
|
|
1380
|
+
type: "tool-result",
|
|
1381
|
+
toolCallId: part.tool_use_id,
|
|
1382
|
+
toolName: "code_execution",
|
|
1383
|
+
result: {
|
|
1384
|
+
type: part.content.type,
|
|
1385
|
+
stdout: part.content.stdout,
|
|
1386
|
+
stderr: part.content.stderr,
|
|
1387
|
+
return_code: part.content.return_code
|
|
1388
|
+
},
|
|
1389
|
+
providerExecuted: true
|
|
1390
|
+
});
|
|
1391
|
+
} else if (part.content.type === "code_execution_tool_result_error") {
|
|
1392
|
+
controller.enqueue({
|
|
1393
|
+
type: "tool-result",
|
|
1394
|
+
toolCallId: part.tool_use_id,
|
|
1395
|
+
toolName: "code_execution",
|
|
1396
|
+
isError: true,
|
|
1397
|
+
result: {
|
|
1398
|
+
type: "code_execution_tool_result_error",
|
|
1399
|
+
errorCode: part.content.error_code
|
|
1400
|
+
},
|
|
1401
|
+
providerExecuted: true
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
return;
|
|
1405
|
+
}
|
|
1406
|
+
default: {
|
|
1407
|
+
const _exhaustiveCheck = contentBlockType;
|
|
1408
|
+
throw new Error(
|
|
1409
|
+
`Unsupported content block type: ${_exhaustiveCheck}`
|
|
1410
|
+
);
|
|
1411
|
+
}
|
|
1412
|
+
}
|
|
1413
|
+
}
|
|
1414
|
+
case "content_block_stop": {
|
|
1415
|
+
if (contentBlocks[value.index] != null) {
|
|
1416
|
+
const contentBlock = contentBlocks[value.index];
|
|
1417
|
+
switch (contentBlock.type) {
|
|
1418
|
+
case "text": {
|
|
1419
|
+
controller.enqueue({
|
|
1420
|
+
type: "text-end",
|
|
1421
|
+
id: String(value.index)
|
|
1422
|
+
});
|
|
1423
|
+
break;
|
|
1424
|
+
}
|
|
1425
|
+
case "reasoning": {
|
|
1426
|
+
controller.enqueue({
|
|
1427
|
+
type: "reasoning-end",
|
|
1428
|
+
id: String(value.index)
|
|
1429
|
+
});
|
|
1430
|
+
break;
|
|
1431
|
+
}
|
|
1432
|
+
case "tool-call":
|
|
1433
|
+
if (!usesJsonResponseTool) {
|
|
1434
|
+
controller.enqueue({
|
|
1435
|
+
type: "tool-input-end",
|
|
1436
|
+
id: contentBlock.toolCallId
|
|
1437
|
+
});
|
|
1438
|
+
controller.enqueue(contentBlock);
|
|
1439
|
+
}
|
|
1440
|
+
break;
|
|
1441
|
+
}
|
|
1442
|
+
delete contentBlocks[value.index];
|
|
1443
|
+
}
|
|
1444
|
+
blockType = void 0;
|
|
1445
|
+
return;
|
|
1446
|
+
}
|
|
1447
|
+
case "content_block_delta": {
|
|
1448
|
+
const deltaType = value.delta.type;
|
|
1449
|
+
switch (deltaType) {
|
|
1450
|
+
case "text_delta": {
|
|
1451
|
+
if (usesJsonResponseTool) {
|
|
1452
|
+
return;
|
|
1453
|
+
}
|
|
1454
|
+
controller.enqueue({
|
|
1455
|
+
type: "text-delta",
|
|
1456
|
+
id: String(value.index),
|
|
1457
|
+
delta: value.delta.text
|
|
1458
|
+
});
|
|
1459
|
+
return;
|
|
1460
|
+
}
|
|
1461
|
+
case "thinking_delta": {
|
|
1462
|
+
controller.enqueue({
|
|
1463
|
+
type: "reasoning-delta",
|
|
1464
|
+
id: String(value.index),
|
|
1465
|
+
delta: value.delta.thinking
|
|
1466
|
+
});
|
|
1467
|
+
return;
|
|
1468
|
+
}
|
|
1469
|
+
case "signature_delta": {
|
|
1470
|
+
if (blockType === "thinking") {
|
|
1471
|
+
controller.enqueue({
|
|
1472
|
+
type: "reasoning-delta",
|
|
1473
|
+
id: String(value.index),
|
|
1474
|
+
delta: "",
|
|
1475
|
+
providerMetadata: {
|
|
1476
|
+
anthropic: {
|
|
1477
|
+
signature: value.delta.signature
|
|
1478
|
+
}
|
|
1479
|
+
}
|
|
1480
|
+
});
|
|
1481
|
+
}
|
|
1482
|
+
return;
|
|
1483
|
+
}
|
|
1484
|
+
case "input_json_delta": {
|
|
1485
|
+
const contentBlock = contentBlocks[value.index];
|
|
1486
|
+
const delta = value.delta.partial_json;
|
|
1487
|
+
if (usesJsonResponseTool) {
|
|
1488
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) !== "text") {
|
|
1489
|
+
return;
|
|
1490
|
+
}
|
|
1491
|
+
controller.enqueue({
|
|
1492
|
+
type: "text-delta",
|
|
1493
|
+
id: String(value.index),
|
|
1494
|
+
delta
|
|
1495
|
+
});
|
|
1496
|
+
} else {
|
|
1497
|
+
if ((contentBlock == null ? void 0 : contentBlock.type) !== "tool-call") {
|
|
1498
|
+
return;
|
|
1499
|
+
}
|
|
1500
|
+
controller.enqueue({
|
|
1501
|
+
type: "tool-input-delta",
|
|
1502
|
+
id: contentBlock.toolCallId,
|
|
1503
|
+
delta
|
|
1504
|
+
});
|
|
1505
|
+
contentBlock.input += delta;
|
|
1506
|
+
}
|
|
1507
|
+
return;
|
|
1508
|
+
}
|
|
1509
|
+
case "citations_delta": {
|
|
1510
|
+
const citation = value.delta.citation;
|
|
1511
|
+
processCitation(
|
|
1512
|
+
citation,
|
|
1513
|
+
citationDocuments,
|
|
1514
|
+
generateId3,
|
|
1515
|
+
(source) => controller.enqueue(source)
|
|
1516
|
+
);
|
|
1517
|
+
return;
|
|
1518
|
+
}
|
|
1519
|
+
default: {
|
|
1520
|
+
const _exhaustiveCheck = deltaType;
|
|
1521
|
+
throw new Error(
|
|
1522
|
+
`Unsupported delta type: ${_exhaustiveCheck}`
|
|
1523
|
+
);
|
|
1524
|
+
}
|
|
1525
|
+
}
|
|
1526
|
+
}
|
|
1527
|
+
case "message_start": {
|
|
1528
|
+
usage.inputTokens = value.message.usage.input_tokens;
|
|
1529
|
+
usage.cachedInputTokens = (_b = value.message.usage.cache_read_input_tokens) != null ? _b : void 0;
|
|
1530
|
+
providerMetadata = {
|
|
1531
|
+
anthropic: {
|
|
1532
|
+
usage: value.message.usage,
|
|
1533
|
+
cacheCreationInputTokens: (_c = value.message.usage.cache_creation_input_tokens) != null ? _c : null
|
|
1534
|
+
}
|
|
1535
|
+
};
|
|
1536
|
+
controller.enqueue({
|
|
1537
|
+
type: "response-metadata",
|
|
1538
|
+
id: (_d = value.message.id) != null ? _d : void 0,
|
|
1539
|
+
modelId: (_e = value.message.model) != null ? _e : void 0
|
|
1540
|
+
});
|
|
1541
|
+
return;
|
|
1542
|
+
}
|
|
1543
|
+
case "message_delta": {
|
|
1544
|
+
usage.outputTokens = value.usage.output_tokens;
|
|
1545
|
+
usage.totalTokens = ((_f = usage.inputTokens) != null ? _f : 0) + ((_g = value.usage.output_tokens) != null ? _g : 0);
|
|
1546
|
+
finishReason = mapAnthropicStopReason({
|
|
1547
|
+
finishReason: value.delta.stop_reason,
|
|
1548
|
+
isJsonResponseFromTool: usesJsonResponseTool
|
|
1549
|
+
});
|
|
1550
|
+
return;
|
|
1551
|
+
}
|
|
1552
|
+
case "message_stop": {
|
|
1553
|
+
controller.enqueue({
|
|
1554
|
+
type: "finish",
|
|
1555
|
+
finishReason,
|
|
1556
|
+
usage,
|
|
1557
|
+
providerMetadata
|
|
1558
|
+
});
|
|
1559
|
+
return;
|
|
1560
|
+
}
|
|
1561
|
+
case "error": {
|
|
1562
|
+
controller.enqueue({ type: "error", error: value.error });
|
|
1563
|
+
return;
|
|
1564
|
+
}
|
|
1565
|
+
default: {
|
|
1566
|
+
const _exhaustiveCheck = value;
|
|
1567
|
+
throw new Error(`Unsupported chunk type: ${_exhaustiveCheck}`);
|
|
1568
|
+
}
|
|
1569
|
+
}
|
|
1570
|
+
}
|
|
1571
|
+
})
|
|
1572
|
+
),
|
|
1573
|
+
request: { body },
|
|
1574
|
+
response: { headers: responseHeaders }
|
|
1575
|
+
};
|
|
1576
|
+
}
|
|
1577
|
+
};
|
|
1578
|
+
var anthropicMessagesResponseSchema = z.object({
|
|
1579
|
+
type: z.literal("message"),
|
|
1580
|
+
id: z.string().nullish(),
|
|
1581
|
+
model: z.string().nullish(),
|
|
1582
|
+
content: z.array(
|
|
1583
|
+
z.discriminatedUnion("type", [
|
|
1584
|
+
z.object({
|
|
1585
|
+
type: z.literal("text"),
|
|
1586
|
+
text: z.string(),
|
|
1587
|
+
citations: z.array(citationSchema).optional()
|
|
1588
|
+
}),
|
|
1589
|
+
z.object({
|
|
1590
|
+
type: z.literal("thinking"),
|
|
1591
|
+
thinking: z.string(),
|
|
1592
|
+
signature: z.string()
|
|
1593
|
+
}),
|
|
1594
|
+
z.object({
|
|
1595
|
+
type: z.literal("redacted_thinking"),
|
|
1596
|
+
data: z.string()
|
|
1597
|
+
}),
|
|
1598
|
+
z.object({
|
|
1599
|
+
type: z.literal("tool_use"),
|
|
1600
|
+
id: z.string(),
|
|
1601
|
+
name: z.string(),
|
|
1602
|
+
input: z.unknown()
|
|
1603
|
+
}),
|
|
1604
|
+
z.object({
|
|
1605
|
+
type: z.literal("server_tool_use"),
|
|
1606
|
+
id: z.string(),
|
|
1607
|
+
name: z.string(),
|
|
1608
|
+
input: z.record(z.string(), z.unknown()).nullish()
|
|
1609
|
+
}),
|
|
1610
|
+
z.object({
|
|
1611
|
+
type: z.literal("web_search_tool_result"),
|
|
1612
|
+
tool_use_id: z.string(),
|
|
1613
|
+
content: z.union([
|
|
1614
|
+
z.array(
|
|
1615
|
+
z.object({
|
|
1616
|
+
type: z.literal("web_search_result"),
|
|
1617
|
+
url: z.string(),
|
|
1618
|
+
title: z.string(),
|
|
1619
|
+
encrypted_content: z.string(),
|
|
1620
|
+
page_age: z.string().nullish()
|
|
1621
|
+
})
|
|
1622
|
+
),
|
|
1623
|
+
z.object({
|
|
1624
|
+
type: z.literal("web_search_tool_result_error"),
|
|
1625
|
+
error_code: z.string()
|
|
1626
|
+
})
|
|
1627
|
+
])
|
|
1628
|
+
}),
|
|
1629
|
+
z.object({
|
|
1630
|
+
type: z.literal("code_execution_tool_result"),
|
|
1631
|
+
tool_use_id: z.string(),
|
|
1632
|
+
content: z.union([
|
|
1633
|
+
z.object({
|
|
1634
|
+
type: z.literal("code_execution_result"),
|
|
1635
|
+
stdout: z.string(),
|
|
1636
|
+
stderr: z.string(),
|
|
1637
|
+
return_code: z.number()
|
|
1638
|
+
}),
|
|
1639
|
+
z.object({
|
|
1640
|
+
type: z.literal("code_execution_tool_result_error"),
|
|
1641
|
+
error_code: z.string()
|
|
1642
|
+
})
|
|
1643
|
+
])
|
|
1644
|
+
})
|
|
1645
|
+
])
|
|
1646
|
+
),
|
|
1647
|
+
stop_reason: z.string().nullish(),
|
|
1648
|
+
usage: z.looseObject({
|
|
1649
|
+
input_tokens: z.number(),
|
|
1650
|
+
output_tokens: z.number(),
|
|
1651
|
+
cache_creation_input_tokens: z.number().nullish(),
|
|
1652
|
+
cache_read_input_tokens: z.number().nullish()
|
|
1653
|
+
})
|
|
1654
|
+
});
|
|
1655
|
+
var anthropicMessagesChunkSchema = z.discriminatedUnion("type", [
|
|
1656
|
+
z.object({
|
|
1657
|
+
type: z.literal("message_start"),
|
|
1658
|
+
message: z.object({
|
|
1659
|
+
id: z.string().nullish(),
|
|
1660
|
+
model: z.string().nullish(),
|
|
1661
|
+
usage: z.looseObject({
|
|
1662
|
+
input_tokens: z.number(),
|
|
1663
|
+
output_tokens: z.number(),
|
|
1664
|
+
cache_creation_input_tokens: z.number().nullish(),
|
|
1665
|
+
cache_read_input_tokens: z.number().nullish()
|
|
1666
|
+
})
|
|
1667
|
+
})
|
|
1668
|
+
}),
|
|
1669
|
+
z.object({
|
|
1670
|
+
type: z.literal("content_block_start"),
|
|
1671
|
+
index: z.number(),
|
|
1672
|
+
content_block: z.discriminatedUnion("type", [
|
|
1673
|
+
z.object({
|
|
1674
|
+
type: z.literal("text"),
|
|
1675
|
+
text: z.string()
|
|
1676
|
+
}),
|
|
1677
|
+
z.object({
|
|
1678
|
+
type: z.literal("thinking"),
|
|
1679
|
+
thinking: z.string()
|
|
1680
|
+
}),
|
|
1681
|
+
z.object({
|
|
1682
|
+
type: z.literal("tool_use"),
|
|
1683
|
+
id: z.string(),
|
|
1684
|
+
name: z.string()
|
|
1685
|
+
}),
|
|
1686
|
+
z.object({
|
|
1687
|
+
type: z.literal("redacted_thinking"),
|
|
1688
|
+
data: z.string()
|
|
1689
|
+
}),
|
|
1690
|
+
z.object({
|
|
1691
|
+
type: z.literal("server_tool_use"),
|
|
1692
|
+
id: z.string(),
|
|
1693
|
+
name: z.string(),
|
|
1694
|
+
input: z.record(z.string(), z.unknown()).nullish()
|
|
1695
|
+
}),
|
|
1696
|
+
z.object({
|
|
1697
|
+
type: z.literal("web_search_tool_result"),
|
|
1698
|
+
tool_use_id: z.string(),
|
|
1699
|
+
content: z.union([
|
|
1700
|
+
z.array(
|
|
1701
|
+
z.object({
|
|
1702
|
+
type: z.literal("web_search_result"),
|
|
1703
|
+
url: z.string(),
|
|
1704
|
+
title: z.string(),
|
|
1705
|
+
encrypted_content: z.string(),
|
|
1706
|
+
page_age: z.string().nullish()
|
|
1707
|
+
})
|
|
1708
|
+
),
|
|
1709
|
+
z.object({
|
|
1710
|
+
type: z.literal("web_search_tool_result_error"),
|
|
1711
|
+
error_code: z.string()
|
|
1712
|
+
})
|
|
1713
|
+
])
|
|
1714
|
+
}),
|
|
1715
|
+
z.object({
|
|
1716
|
+
type: z.literal("code_execution_tool_result"),
|
|
1717
|
+
tool_use_id: z.string(),
|
|
1718
|
+
content: z.union([
|
|
1719
|
+
z.object({
|
|
1720
|
+
type: z.literal("code_execution_result"),
|
|
1721
|
+
stdout: z.string(),
|
|
1722
|
+
stderr: z.string(),
|
|
1723
|
+
return_code: z.number()
|
|
1724
|
+
}),
|
|
1725
|
+
z.object({
|
|
1726
|
+
type: z.literal("code_execution_tool_result_error"),
|
|
1727
|
+
error_code: z.string()
|
|
1728
|
+
})
|
|
1729
|
+
])
|
|
1730
|
+
})
|
|
1731
|
+
])
|
|
1732
|
+
}),
|
|
1733
|
+
z.object({
|
|
1734
|
+
type: z.literal("content_block_delta"),
|
|
1735
|
+
index: z.number(),
|
|
1736
|
+
delta: z.discriminatedUnion("type", [
|
|
1737
|
+
z.object({
|
|
1738
|
+
type: z.literal("input_json_delta"),
|
|
1739
|
+
partial_json: z.string()
|
|
1740
|
+
}),
|
|
1741
|
+
z.object({
|
|
1742
|
+
type: z.literal("text_delta"),
|
|
1743
|
+
text: z.string()
|
|
1744
|
+
}),
|
|
1745
|
+
z.object({
|
|
1746
|
+
type: z.literal("thinking_delta"),
|
|
1747
|
+
thinking: z.string()
|
|
1748
|
+
}),
|
|
1749
|
+
z.object({
|
|
1750
|
+
type: z.literal("signature_delta"),
|
|
1751
|
+
signature: z.string()
|
|
1752
|
+
}),
|
|
1753
|
+
z.object({
|
|
1754
|
+
type: z.literal("citations_delta"),
|
|
1755
|
+
citation: citationSchema
|
|
1756
|
+
})
|
|
1757
|
+
])
|
|
1758
|
+
}),
|
|
1759
|
+
z.object({
|
|
1760
|
+
type: z.literal("content_block_stop"),
|
|
1761
|
+
index: z.number()
|
|
1762
|
+
}),
|
|
1763
|
+
z.object({
|
|
1764
|
+
type: z.literal("error"),
|
|
1765
|
+
error: z.object({
|
|
1766
|
+
type: z.string(),
|
|
1767
|
+
message: z.string()
|
|
1768
|
+
})
|
|
1769
|
+
}),
|
|
1770
|
+
z.object({
|
|
1771
|
+
type: z.literal("message_delta"),
|
|
1772
|
+
delta: z.object({ stop_reason: z.string().nullish() }),
|
|
1773
|
+
usage: z.object({ output_tokens: z.number() })
|
|
1774
|
+
}),
|
|
1775
|
+
z.object({
|
|
1776
|
+
type: z.literal("message_stop")
|
|
1777
|
+
}),
|
|
1778
|
+
z.object({
|
|
1779
|
+
type: z.literal("ping")
|
|
1780
|
+
})
|
|
1781
|
+
]);
|
|
1782
|
+
var anthropicReasoningMetadataSchema = z.object({
|
|
1783
|
+
signature: z.string().optional(),
|
|
1784
|
+
redactedData: z.string().optional()
|
|
1785
|
+
});
|
|
1786
|
+
var bash_20241022 = createProviderDefinedToolFactory({
|
|
1787
|
+
id: "anthropic.bash_20241022",
|
|
1788
|
+
name: "bash",
|
|
1789
|
+
inputSchema: z6.object({
|
|
1790
|
+
command: z6.string(),
|
|
1791
|
+
restart: z6.boolean().optional()
|
|
1792
|
+
})
|
|
1793
|
+
});
|
|
1794
|
+
var bash_20250124 = createProviderDefinedToolFactory({
|
|
1795
|
+
id: "anthropic.bash_20250124",
|
|
1796
|
+
name: "bash",
|
|
1797
|
+
inputSchema: z6.object({
|
|
1798
|
+
command: z6.string(),
|
|
1799
|
+
restart: z6.boolean().optional()
|
|
1800
|
+
})
|
|
1801
|
+
});
|
|
1802
|
+
var computer_20241022 = createProviderDefinedToolFactory({
|
|
1803
|
+
id: "anthropic.computer_20241022",
|
|
1804
|
+
name: "computer",
|
|
1805
|
+
inputSchema: z.object({
|
|
1806
|
+
action: z.enum([
|
|
1807
|
+
"key",
|
|
1808
|
+
"type",
|
|
1809
|
+
"mouse_move",
|
|
1810
|
+
"left_click",
|
|
1811
|
+
"left_click_drag",
|
|
1812
|
+
"right_click",
|
|
1813
|
+
"middle_click",
|
|
1814
|
+
"double_click",
|
|
1815
|
+
"screenshot",
|
|
1816
|
+
"cursor_position"
|
|
1817
|
+
]),
|
|
1818
|
+
coordinate: z.array(z.number().int()).optional(),
|
|
1819
|
+
text: z.string().optional()
|
|
1820
|
+
})
|
|
1821
|
+
});
|
|
1822
|
+
var computer_20250124 = createProviderDefinedToolFactory({
|
|
1823
|
+
id: "anthropic.computer_20250124",
|
|
1824
|
+
name: "computer",
|
|
1825
|
+
inputSchema: z.object({
|
|
1826
|
+
action: z.enum([
|
|
1827
|
+
"key",
|
|
1828
|
+
"hold_key",
|
|
1829
|
+
"type",
|
|
1830
|
+
"cursor_position",
|
|
1831
|
+
"mouse_move",
|
|
1832
|
+
"left_mouse_down",
|
|
1833
|
+
"left_mouse_up",
|
|
1834
|
+
"left_click",
|
|
1835
|
+
"left_click_drag",
|
|
1836
|
+
"right_click",
|
|
1837
|
+
"middle_click",
|
|
1838
|
+
"double_click",
|
|
1839
|
+
"triple_click",
|
|
1840
|
+
"scroll",
|
|
1841
|
+
"wait",
|
|
1842
|
+
"screenshot"
|
|
1843
|
+
]),
|
|
1844
|
+
coordinate: z.tuple([z.number().int(), z.number().int()]).optional(),
|
|
1845
|
+
duration: z.number().optional(),
|
|
1846
|
+
scroll_amount: z.number().optional(),
|
|
1847
|
+
scroll_direction: z.enum(["up", "down", "left", "right"]).optional(),
|
|
1848
|
+
start_coordinate: z.tuple([z.number().int(), z.number().int()]).optional(),
|
|
1849
|
+
text: z.string().optional()
|
|
1850
|
+
})
|
|
1851
|
+
});
|
|
1852
|
+
var textEditor_20241022 = createProviderDefinedToolFactory({
|
|
1853
|
+
id: "anthropic.text_editor_20241022",
|
|
1854
|
+
name: "str_replace_editor",
|
|
1855
|
+
inputSchema: z.object({
|
|
1856
|
+
command: z.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
|
|
1857
|
+
path: z.string(),
|
|
1858
|
+
file_text: z.string().optional(),
|
|
1859
|
+
insert_line: z.number().int().optional(),
|
|
1860
|
+
new_str: z.string().optional(),
|
|
1861
|
+
old_str: z.string().optional(),
|
|
1862
|
+
view_range: z.array(z.number().int()).optional()
|
|
1863
|
+
})
|
|
1864
|
+
});
|
|
1865
|
+
var textEditor_20250124 = createProviderDefinedToolFactory({
|
|
1866
|
+
id: "anthropic.text_editor_20250124",
|
|
1867
|
+
name: "str_replace_editor",
|
|
1868
|
+
inputSchema: z.object({
|
|
1869
|
+
command: z.enum(["view", "create", "str_replace", "insert", "undo_edit"]),
|
|
1870
|
+
path: z.string(),
|
|
1871
|
+
file_text: z.string().optional(),
|
|
1872
|
+
insert_line: z.number().int().optional(),
|
|
1873
|
+
new_str: z.string().optional(),
|
|
1874
|
+
old_str: z.string().optional(),
|
|
1875
|
+
view_range: z.array(z.number().int()).optional()
|
|
1876
|
+
})
|
|
1877
|
+
});
|
|
1878
|
+
var textEditor_20250429 = createProviderDefinedToolFactory({
|
|
1879
|
+
id: "anthropic.text_editor_20250429",
|
|
1880
|
+
name: "str_replace_based_edit_tool",
|
|
1881
|
+
inputSchema: z.object({
|
|
1882
|
+
command: z.enum(["view", "create", "str_replace", "insert"]),
|
|
1883
|
+
path: z.string(),
|
|
1884
|
+
file_text: z.string().optional(),
|
|
1885
|
+
insert_line: z.number().int().optional(),
|
|
1886
|
+
new_str: z.string().optional(),
|
|
1887
|
+
old_str: z.string().optional(),
|
|
1888
|
+
view_range: z.array(z.number().int()).optional()
|
|
1889
|
+
})
|
|
1890
|
+
});
|
|
1891
|
+
var anthropicTools = {
|
|
1892
|
+
/**
|
|
1893
|
+
* Creates a tool for running a bash command. Must have name "bash".
|
|
1894
|
+
*
|
|
1895
|
+
* Image results are supported.
|
|
1896
|
+
*
|
|
1897
|
+
* @param execute - The function to execute the tool. Optional.
|
|
1898
|
+
*/
|
|
1899
|
+
bash_20241022,
|
|
1900
|
+
/**
|
|
1901
|
+
* Creates a tool for running a bash command. Must have name "bash".
|
|
1902
|
+
*
|
|
1903
|
+
* Image results are supported.
|
|
1904
|
+
*
|
|
1905
|
+
* @param execute - The function to execute the tool. Optional.
|
|
1906
|
+
*/
|
|
1907
|
+
bash_20250124,
|
|
1908
|
+
/**
|
|
1909
|
+
* Creates a tool for editing text. Must have name "str_replace_editor".
|
|
1910
|
+
*/
|
|
1911
|
+
textEditor_20241022,
|
|
1912
|
+
/**
|
|
1913
|
+
* Creates a tool for editing text. Must have name "str_replace_editor".
|
|
1914
|
+
*/
|
|
1915
|
+
textEditor_20250124,
|
|
1916
|
+
/**
|
|
1917
|
+
* Creates a tool for editing text. Must have name "str_replace_based_edit_tool".
|
|
1918
|
+
* Note: This version does not support the "undo_edit" command.
|
|
1919
|
+
*/
|
|
1920
|
+
textEditor_20250429,
|
|
1921
|
+
/**
|
|
1922
|
+
* Creates a tool for executing actions on a computer. Must have name "computer".
|
|
1923
|
+
*
|
|
1924
|
+
* Image results are supported.
|
|
1925
|
+
*
|
|
1926
|
+
* @param displayWidthPx - The width of the display being controlled by the model in pixels.
|
|
1927
|
+
* @param displayHeightPx - The height of the display being controlled by the model in pixels.
|
|
1928
|
+
* @param displayNumber - The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
|
|
1929
|
+
*/
|
|
1930
|
+
computer_20241022,
|
|
1931
|
+
/**
|
|
1932
|
+
* Creates a tool for executing actions on a computer. Must have name "computer".
|
|
1933
|
+
*
|
|
1934
|
+
* Image results are supported.
|
|
1935
|
+
*
|
|
1936
|
+
* @param displayWidthPx - The width of the display being controlled by the model in pixels.
|
|
1937
|
+
* @param displayHeightPx - The height of the display being controlled by the model in pixels.
|
|
1938
|
+
* @param displayNumber - The display number to control (only relevant for X11 environments). If specified, the tool will be provided a display number in the tool definition.
|
|
1939
|
+
* @param execute - The function to execute the tool. Optional.
|
|
1940
|
+
*/
|
|
1941
|
+
computer_20250124,
|
|
1942
|
+
/**
|
|
1943
|
+
* Creates a web search tool that gives Claude direct access to real-time web content.
|
|
1944
|
+
* Must have name "web_search".
|
|
1945
|
+
*
|
|
1946
|
+
* @param maxUses - Maximum number of web searches Claude can perform during the conversation.
|
|
1947
|
+
* @param allowedDomains - Optional list of domains that Claude is allowed to search.
|
|
1948
|
+
* @param blockedDomains - Optional list of domains that Claude should avoid when searching.
|
|
1949
|
+
* @param userLocation - Optional user location information to provide geographically relevant search results.
|
|
1950
|
+
*/
|
|
1951
|
+
webSearch_20250305,
|
|
1952
|
+
/**
|
|
1953
|
+
* Creates a tool for executing Python code. Must have name "code_execution".
|
|
1954
|
+
*/
|
|
1955
|
+
codeExecution_20250522
|
|
1956
|
+
};
|
|
1957
|
+
function createAnthropic(options = {}) {
|
|
1958
|
+
var _a;
|
|
1959
|
+
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://api.anthropic.com/v1";
|
|
1960
|
+
const getHeaders = () => ({
|
|
1961
|
+
"anthropic-version": "2023-06-01",
|
|
1962
|
+
"x-api-key": loadApiKey({
|
|
1963
|
+
apiKey: options.apiKey,
|
|
1964
|
+
environmentVariableName: "ANTHROPIC_API_KEY",
|
|
1965
|
+
description: "Anthropic"
|
|
1966
|
+
}),
|
|
1967
|
+
...options.headers
|
|
1968
|
+
});
|
|
1969
|
+
const createChatModel = (modelId) => {
|
|
1970
|
+
var _a2;
|
|
1971
|
+
return new AnthropicMessagesLanguageModel(modelId, {
|
|
1972
|
+
provider: "anthropic.messages",
|
|
1973
|
+
baseURL,
|
|
1974
|
+
headers: getHeaders,
|
|
1975
|
+
fetch: options.fetch,
|
|
1976
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : generateId,
|
|
1977
|
+
supportedUrls: () => ({
|
|
1978
|
+
"image/*": [/^https?:\/\/.*$/]
|
|
1979
|
+
})
|
|
1980
|
+
});
|
|
1981
|
+
};
|
|
1982
|
+
const provider = function(modelId) {
|
|
1983
|
+
if (new.target) {
|
|
1984
|
+
throw new Error(
|
|
1985
|
+
"The Anthropic model function cannot be called with the new keyword."
|
|
1986
|
+
);
|
|
1987
|
+
}
|
|
1988
|
+
return createChatModel(modelId);
|
|
1989
|
+
};
|
|
1990
|
+
provider.languageModel = createChatModel;
|
|
1991
|
+
provider.chat = createChatModel;
|
|
1992
|
+
provider.messages = createChatModel;
|
|
1993
|
+
provider.textEmbeddingModel = (modelId) => {
|
|
1994
|
+
throw new NoSuchModelError({ modelId, modelType: "textEmbeddingModel" });
|
|
1995
|
+
};
|
|
1996
|
+
provider.imageModel = (modelId) => {
|
|
1997
|
+
throw new NoSuchModelError({ modelId, modelType: "imageModel" });
|
|
1998
|
+
};
|
|
1999
|
+
provider.tools = anthropicTools;
|
|
2000
|
+
return provider;
|
|
2001
|
+
}
|
|
2002
|
+
var anthropic = createAnthropic();
|
|
2003
|
+
|
|
2004
|
+
export { anthropic, createAnthropic };
|
|
2005
|
+
//# sourceMappingURL=chunk-RCHEPTZZ.js.map
|
|
2006
|
+
//# sourceMappingURL=chunk-RCHEPTZZ.js.map
|