@mastra/server 0.0.0-revert-schema-20250416221206 → 0.0.0-safe-stringify-telemetry-20251205024938
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 +4368 -0
- package/LICENSE.md +11 -42
- package/README.md +45 -137
- package/dist/chunk-2PLXW4ZX.cjs +2777 -0
- package/dist/chunk-2PLXW4ZX.cjs.map +1 -0
- package/dist/chunk-2S4IMB6E.cjs +130 -0
- package/dist/chunk-2S4IMB6E.cjs.map +1 -0
- package/dist/chunk-3SNGNFUJ.cjs +128 -0
- package/dist/chunk-3SNGNFUJ.cjs.map +1 -0
- package/dist/{chunk-L7XE5QTW.js → chunk-4QCXUEAT.js} +11 -2
- package/dist/chunk-4QCXUEAT.js.map +1 -0
- package/dist/{chunk-WTHDCRMY.js → chunk-67GYDFSB.js} +33 -8
- package/dist/chunk-67GYDFSB.js.map +1 -0
- package/dist/chunk-6N3HC6O4.js +705 -0
- package/dist/chunk-6N3HC6O4.js.map +1 -0
- package/dist/{chunk-7IWQE76Z.cjs → chunk-6ZR275MD.cjs} +37 -11
- package/dist/chunk-6ZR275MD.cjs.map +1 -0
- package/dist/{chunk-FV45V6WC.cjs → chunk-7NADHFD2.cjs} +3 -7
- package/dist/chunk-7NADHFD2.cjs.map +1 -0
- package/dist/chunk-7RGKOGE4.cjs +485 -0
- package/dist/chunk-7RGKOGE4.cjs.map +1 -0
- package/dist/chunk-ABRFV4XP.js +83 -0
- package/dist/chunk-ABRFV4XP.js.map +1 -0
- package/dist/{chunk-UV4WTEH4.js → chunk-AHB4JCIQ.js} +55 -15
- package/dist/chunk-AHB4JCIQ.js.map +1 -0
- package/dist/chunk-AW5BU3RQ.js +192 -0
- package/dist/chunk-AW5BU3RQ.js.map +1 -0
- package/dist/chunk-BATEJLED.js +124 -0
- package/dist/chunk-BATEJLED.js.map +1 -0
- package/dist/chunk-CY4TP3FK.js +16 -0
- package/dist/chunk-CY4TP3FK.js.map +1 -0
- package/dist/chunk-CY6R3HLM.cjs +727 -0
- package/dist/chunk-CY6R3HLM.cjs.map +1 -0
- package/dist/{chunk-M56ECCHK.cjs → chunk-EP3JQDPD.cjs} +22 -19
- package/dist/chunk-EP3JQDPD.cjs.map +1 -0
- package/dist/chunk-G4PUALCE.cjs +28 -0
- package/dist/chunk-G4PUALCE.cjs.map +1 -0
- package/dist/chunk-G5O5SC7R.cjs +19565 -0
- package/dist/chunk-G5O5SC7R.cjs.map +1 -0
- package/dist/chunk-GU4EWMZB.cjs +769 -0
- package/dist/chunk-GU4EWMZB.cjs.map +1 -0
- package/dist/chunk-I3C4ODGV.cjs +335 -0
- package/dist/chunk-I3C4ODGV.cjs.map +1 -0
- package/dist/chunk-IAE7SFBS.js +477 -0
- package/dist/chunk-IAE7SFBS.js.map +1 -0
- package/dist/chunk-LF2ZLOFP.js +767 -0
- package/dist/chunk-LF2ZLOFP.js.map +1 -0
- package/dist/{chunk-OPP7H5TW.js → chunk-LLUOPR3J.js} +92 -74
- package/dist/chunk-LLUOPR3J.js.map +1 -0
- package/dist/chunk-LPM6BBAX.cjs +1043 -0
- package/dist/chunk-LPM6BBAX.cjs.map +1 -0
- package/dist/{chunk-TRDNDNGQ.js → chunk-MMROOK5J.js} +4 -7
- package/dist/chunk-MMROOK5J.js.map +1 -0
- package/dist/{chunk-4JINXASC.js → chunk-MYR4PVGN.js} +7 -4
- package/dist/chunk-MYR4PVGN.js.map +1 -0
- package/dist/chunk-NT5XW5PI.cjs +596 -0
- package/dist/chunk-NT5XW5PI.cjs.map +1 -0
- package/dist/chunk-O7I5CWRX.cjs +44 -0
- package/dist/chunk-O7I5CWRX.cjs.map +1 -0
- package/dist/chunk-PBXWFGEH.js +579 -0
- package/dist/chunk-PBXWFGEH.js.map +1 -0
- package/dist/chunk-PR4QN5HX.js +39 -0
- package/dist/chunk-PR4QN5HX.js.map +1 -0
- package/dist/chunk-QGNHNNJR.js +909 -0
- package/dist/chunk-QGNHNNJR.js.map +1 -0
- package/dist/{chunk-3RVHWGWO.js → chunk-QQXMIP6C.js} +49 -22
- package/dist/chunk-QQXMIP6C.js.map +1 -0
- package/dist/chunk-QU6N55W6.cjs +256 -0
- package/dist/chunk-QU6N55W6.cjs.map +1 -0
- package/dist/chunk-RE4RPXT2.cjs +18 -0
- package/dist/chunk-RE4RPXT2.cjs.map +1 -0
- package/dist/chunk-RQK4FQUD.js +1012 -0
- package/dist/chunk-RQK4FQUD.js.map +1 -0
- package/dist/chunk-S6YZQY4V.cjs +938 -0
- package/dist/chunk-S6YZQY4V.cjs.map +1 -0
- package/dist/chunk-SHWNNZBL.js +254 -0
- package/dist/chunk-SHWNNZBL.js.map +1 -0
- package/dist/chunk-SPRRK3P7.cjs +200 -0
- package/dist/chunk-SPRRK3P7.cjs.map +1 -0
- package/dist/chunk-TOCYBDP2.js +46 -0
- package/dist/chunk-TOCYBDP2.js.map +1 -0
- package/dist/chunk-TRGAMKHX.cjs +167 -0
- package/dist/chunk-TRGAMKHX.cjs.map +1 -0
- package/dist/chunk-TRHCWKZ2.js +19529 -0
- package/dist/chunk-TRHCWKZ2.js.map +1 -0
- package/dist/chunk-VY4ENABS.cjs +135 -0
- package/dist/chunk-VY4ENABS.cjs.map +1 -0
- package/dist/chunk-X6C7BUWN.cjs +48 -0
- package/dist/chunk-X6C7BUWN.cjs.map +1 -0
- package/dist/chunk-XN74I6VW.cjs +88 -0
- package/dist/chunk-XN74I6VW.cjs.map +1 -0
- package/dist/chunk-YWOK4F5A.js +131 -0
- package/dist/chunk-YWOK4F5A.js.map +1 -0
- package/dist/chunk-ZULZ2752.js +2774 -0
- package/dist/chunk-ZULZ2752.js.map +1 -0
- package/dist/dist-4MVGNSRL.cjs +1150 -0
- package/dist/dist-4MVGNSRL.cjs.map +1 -0
- package/dist/dist-FZYCV3VB.cjs +940 -0
- package/dist/dist-FZYCV3VB.cjs.map +1 -0
- package/dist/dist-G2BYZJOC.cjs +928 -0
- package/dist/dist-G2BYZJOC.cjs.map +1 -0
- package/dist/dist-P4MXBQ3U.cjs +16 -0
- package/dist/dist-P4MXBQ3U.cjs.map +1 -0
- package/dist/dist-PQZUVLPC.js +937 -0
- package/dist/dist-PQZUVLPC.js.map +1 -0
- package/dist/dist-R7WYX6LC.js +925 -0
- package/dist/dist-R7WYX6LC.js.map +1 -0
- package/dist/dist-RFMYFILX.cjs +764 -0
- package/dist/dist-RFMYFILX.cjs.map +1 -0
- package/dist/dist-X7XR3M3Z.js +1147 -0
- package/dist/dist-X7XR3M3Z.js.map +1 -0
- package/dist/dist-XVBSOGFK.js +761 -0
- package/dist/dist-XVBSOGFK.js.map +1 -0
- package/dist/dist-YREX2TJT.js +3 -0
- package/dist/dist-YREX2TJT.js.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 +32 -0
- package/dist/server/handlers/a2a.cjs.map +1 -0
- package/dist/server/handlers/a2a.d.ts +68 -0
- package/dist/server/handlers/a2a.d.ts.map +1 -0
- package/dist/server/handlers/a2a.js +3 -0
- 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 +94 -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 +65 -7
- package/dist/server/handlers/agents.cjs.map +1 -0
- package/dist/server/handlers/agents.d.ts +210 -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 +48 -0
- package/dist/server/handlers/legacyWorkflows.cjs.map +1 -0
- package/dist/server/handlers/legacyWorkflows.d.ts +59 -0
- package/dist/server/handlers/legacyWorkflows.d.ts.map +1 -0
- package/dist/server/handlers/legacyWorkflows.js +3 -0
- 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 +152 -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/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 +10 -4
- package/dist/server/handlers/voice.cjs.map +1 -0
- package/dist/server/handlers/voice.d.ts +41 -3
- 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 +83 -13
- package/dist/server/handlers/workflows.cjs.map +1 -0
- package/dist/server/handlers/workflows.d.ts +151 -10
- 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 +49 -27
- package/dist/server/handlers.cjs.map +1 -0
- package/dist/server/handlers.d.ts +14 -9
- package/dist/server/handlers.d.ts.map +1 -0
- package/dist/server/handlers.js +15 -9
- 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/dist/token-375W3LEI.cjs +64 -0
- package/dist/token-375W3LEI.cjs.map +1 -0
- package/dist/token-C3IMNCC4.js +62 -0
- package/dist/token-C3IMNCC4.js.map +1 -0
- package/dist/token-util-CV3RRG6K.cjs +9 -0
- package/dist/token-util-CV3RRG6K.cjs.map +1 -0
- package/dist/token-util-E5QO2RCL.js +7 -0
- package/dist/token-util-E5QO2RCL.js.map +1 -0
- package/package.json +50 -19
- package/dist/_tsup-dts-rollup.d.cts +0 -573
- package/dist/_tsup-dts-rollup.d.ts +0 -573
- package/dist/chunk-2FJURXCL.cjs +0 -213
- package/dist/chunk-3AHQ5RGN.js +0 -11
- package/dist/chunk-3EJZQ6TQ.js +0 -49
- package/dist/chunk-A7DF4ETD.cjs +0 -100
- package/dist/chunk-B64YROKU.cjs +0 -317
- package/dist/chunk-I6MBOQTH.js +0 -156
- package/dist/chunk-JLDXUWK7.cjs +0 -126
- package/dist/chunk-PDC4ZBQW.cjs +0 -126
- package/dist/chunk-QH6XWSXP.cjs +0 -5579
- package/dist/chunk-QN4KF3BH.cjs +0 -18
- package/dist/chunk-RBQASTUP.js +0 -203
- package/dist/chunk-SKBVVI24.cjs +0 -54
- package/dist/chunk-UE27Q3XD.cjs +0 -164
- package/dist/chunk-VK6FX47H.js +0 -5576
- package/dist/chunk-YANVFOYA.js +0 -120
- package/dist/chunk-ZLBRQFDD.cjs +0 -13
- package/dist/index.d.cts +0 -1
- package/dist/server/handlers/agents.d.cts +0 -6
- package/dist/server/handlers/error.d.cts +0 -1
- 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/vector.d.cts +0 -6
- package/dist/server/handlers/voice.d.cts +0 -3
- package/dist/server/handlers/workflows.d.cts +0 -10
- package/dist/server/handlers.d.cts +0 -9
|
@@ -0,0 +1,937 @@
|
|
|
1
|
+
import { createJsonErrorResponseHandler, withoutTrailingSlash, generateId, loadApiKey, parseProviderOptions, combineHeaders, resolve, postJsonToApi, createJsonResponseHandler, createEventSourceResponseHandler, UnsupportedFunctionalityError, convertUint8ArrayToBase64, TooManyEmbeddingValuesForCallError } from './chunk-RQK4FQUD.js';
|
|
2
|
+
import { z } from 'zod';
|
|
3
|
+
|
|
4
|
+
function convertJSONSchemaToOpenAPISchema(jsonSchema) {
|
|
5
|
+
if (isEmptyObjectSchema(jsonSchema)) {
|
|
6
|
+
return void 0;
|
|
7
|
+
}
|
|
8
|
+
if (typeof jsonSchema === "boolean") {
|
|
9
|
+
return { type: "boolean", properties: {} };
|
|
10
|
+
}
|
|
11
|
+
const {
|
|
12
|
+
type,
|
|
13
|
+
description,
|
|
14
|
+
required,
|
|
15
|
+
properties,
|
|
16
|
+
items,
|
|
17
|
+
allOf,
|
|
18
|
+
anyOf,
|
|
19
|
+
oneOf,
|
|
20
|
+
format,
|
|
21
|
+
const: constValue,
|
|
22
|
+
minLength,
|
|
23
|
+
enum: enumValues
|
|
24
|
+
} = jsonSchema;
|
|
25
|
+
const result = {};
|
|
26
|
+
if (description)
|
|
27
|
+
result.description = description;
|
|
28
|
+
if (required)
|
|
29
|
+
result.required = required;
|
|
30
|
+
if (format)
|
|
31
|
+
result.format = format;
|
|
32
|
+
if (constValue !== void 0) {
|
|
33
|
+
result.enum = [constValue];
|
|
34
|
+
}
|
|
35
|
+
if (type) {
|
|
36
|
+
if (Array.isArray(type)) {
|
|
37
|
+
if (type.includes("null")) {
|
|
38
|
+
result.type = type.filter((t) => t !== "null")[0];
|
|
39
|
+
result.nullable = true;
|
|
40
|
+
} else {
|
|
41
|
+
result.type = type;
|
|
42
|
+
}
|
|
43
|
+
} else if (type === "null") {
|
|
44
|
+
result.type = "null";
|
|
45
|
+
} else {
|
|
46
|
+
result.type = type;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
if (enumValues !== void 0) {
|
|
50
|
+
result.enum = enumValues;
|
|
51
|
+
}
|
|
52
|
+
if (properties != null) {
|
|
53
|
+
result.properties = Object.entries(properties).reduce(
|
|
54
|
+
(acc, [key, value]) => {
|
|
55
|
+
acc[key] = convertJSONSchemaToOpenAPISchema(value);
|
|
56
|
+
return acc;
|
|
57
|
+
},
|
|
58
|
+
{}
|
|
59
|
+
);
|
|
60
|
+
}
|
|
61
|
+
if (items) {
|
|
62
|
+
result.items = Array.isArray(items) ? items.map(convertJSONSchemaToOpenAPISchema) : convertJSONSchemaToOpenAPISchema(items);
|
|
63
|
+
}
|
|
64
|
+
if (allOf) {
|
|
65
|
+
result.allOf = allOf.map(convertJSONSchemaToOpenAPISchema);
|
|
66
|
+
}
|
|
67
|
+
if (anyOf) {
|
|
68
|
+
if (anyOf.some(
|
|
69
|
+
(schema) => typeof schema === "object" && (schema == null ? void 0 : schema.type) === "null"
|
|
70
|
+
)) {
|
|
71
|
+
const nonNullSchemas = anyOf.filter(
|
|
72
|
+
(schema) => !(typeof schema === "object" && (schema == null ? void 0 : schema.type) === "null")
|
|
73
|
+
);
|
|
74
|
+
if (nonNullSchemas.length === 1) {
|
|
75
|
+
const converted = convertJSONSchemaToOpenAPISchema(nonNullSchemas[0]);
|
|
76
|
+
if (typeof converted === "object") {
|
|
77
|
+
result.nullable = true;
|
|
78
|
+
Object.assign(result, converted);
|
|
79
|
+
}
|
|
80
|
+
} else {
|
|
81
|
+
result.anyOf = nonNullSchemas.map(convertJSONSchemaToOpenAPISchema);
|
|
82
|
+
result.nullable = true;
|
|
83
|
+
}
|
|
84
|
+
} else {
|
|
85
|
+
result.anyOf = anyOf.map(convertJSONSchemaToOpenAPISchema);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (oneOf) {
|
|
89
|
+
result.oneOf = oneOf.map(convertJSONSchemaToOpenAPISchema);
|
|
90
|
+
}
|
|
91
|
+
if (minLength !== void 0) {
|
|
92
|
+
result.minLength = minLength;
|
|
93
|
+
}
|
|
94
|
+
return result;
|
|
95
|
+
}
|
|
96
|
+
function isEmptyObjectSchema(jsonSchema) {
|
|
97
|
+
return jsonSchema != null && typeof jsonSchema === "object" && jsonSchema.type === "object" && (jsonSchema.properties == null || Object.keys(jsonSchema.properties).length === 0) && !jsonSchema.additionalProperties;
|
|
98
|
+
}
|
|
99
|
+
function convertToGoogleGenerativeAIMessages(prompt) {
|
|
100
|
+
var _a, _b;
|
|
101
|
+
const systemInstructionParts = [];
|
|
102
|
+
const contents = [];
|
|
103
|
+
let systemMessagesAllowed = true;
|
|
104
|
+
for (const { role, content } of prompt) {
|
|
105
|
+
switch (role) {
|
|
106
|
+
case "system": {
|
|
107
|
+
if (!systemMessagesAllowed) {
|
|
108
|
+
throw new UnsupportedFunctionalityError({
|
|
109
|
+
functionality: "system messages are only supported at the beginning of the conversation"
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
systemInstructionParts.push({ text: content });
|
|
113
|
+
break;
|
|
114
|
+
}
|
|
115
|
+
case "user": {
|
|
116
|
+
systemMessagesAllowed = false;
|
|
117
|
+
const parts = [];
|
|
118
|
+
for (const part of content) {
|
|
119
|
+
switch (part.type) {
|
|
120
|
+
case "text": {
|
|
121
|
+
parts.push({ text: part.text });
|
|
122
|
+
break;
|
|
123
|
+
}
|
|
124
|
+
case "image": {
|
|
125
|
+
parts.push(
|
|
126
|
+
part.image instanceof URL ? {
|
|
127
|
+
fileData: {
|
|
128
|
+
mimeType: (_a = part.mimeType) != null ? _a : "image/jpeg",
|
|
129
|
+
fileUri: part.image.toString()
|
|
130
|
+
}
|
|
131
|
+
} : {
|
|
132
|
+
inlineData: {
|
|
133
|
+
mimeType: (_b = part.mimeType) != null ? _b : "image/jpeg",
|
|
134
|
+
data: convertUint8ArrayToBase64(part.image)
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
);
|
|
138
|
+
break;
|
|
139
|
+
}
|
|
140
|
+
case "file": {
|
|
141
|
+
parts.push(
|
|
142
|
+
part.data instanceof URL ? {
|
|
143
|
+
fileData: {
|
|
144
|
+
mimeType: part.mimeType,
|
|
145
|
+
fileUri: part.data.toString()
|
|
146
|
+
}
|
|
147
|
+
} : {
|
|
148
|
+
inlineData: {
|
|
149
|
+
mimeType: part.mimeType,
|
|
150
|
+
data: part.data
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
);
|
|
154
|
+
break;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
}
|
|
158
|
+
contents.push({ role: "user", parts });
|
|
159
|
+
break;
|
|
160
|
+
}
|
|
161
|
+
case "assistant": {
|
|
162
|
+
systemMessagesAllowed = false;
|
|
163
|
+
contents.push({
|
|
164
|
+
role: "model",
|
|
165
|
+
parts: content.map((part) => {
|
|
166
|
+
switch (part.type) {
|
|
167
|
+
case "text": {
|
|
168
|
+
return part.text.length === 0 ? void 0 : { text: part.text };
|
|
169
|
+
}
|
|
170
|
+
case "file": {
|
|
171
|
+
if (part.mimeType !== "image/png") {
|
|
172
|
+
throw new UnsupportedFunctionalityError({
|
|
173
|
+
functionality: "Only PNG images are supported in assistant messages"
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
if (part.data instanceof URL) {
|
|
177
|
+
throw new UnsupportedFunctionalityError({
|
|
178
|
+
functionality: "File data URLs in assistant messages are not supported"
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
return {
|
|
182
|
+
inlineData: {
|
|
183
|
+
mimeType: part.mimeType,
|
|
184
|
+
data: part.data
|
|
185
|
+
}
|
|
186
|
+
};
|
|
187
|
+
}
|
|
188
|
+
case "tool-call": {
|
|
189
|
+
return {
|
|
190
|
+
functionCall: {
|
|
191
|
+
name: part.toolName,
|
|
192
|
+
args: part.args
|
|
193
|
+
}
|
|
194
|
+
};
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
}).filter((part) => part !== void 0)
|
|
198
|
+
});
|
|
199
|
+
break;
|
|
200
|
+
}
|
|
201
|
+
case "tool": {
|
|
202
|
+
systemMessagesAllowed = false;
|
|
203
|
+
contents.push({
|
|
204
|
+
role: "user",
|
|
205
|
+
parts: content.map((part) => ({
|
|
206
|
+
functionResponse: {
|
|
207
|
+
name: part.toolName,
|
|
208
|
+
response: {
|
|
209
|
+
name: part.toolName,
|
|
210
|
+
content: part.result
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
}))
|
|
214
|
+
});
|
|
215
|
+
break;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
return {
|
|
220
|
+
systemInstruction: systemInstructionParts.length > 0 ? { parts: systemInstructionParts } : void 0,
|
|
221
|
+
contents
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
function getModelPath(modelId) {
|
|
225
|
+
return modelId.includes("/") ? modelId : `models/${modelId}`;
|
|
226
|
+
}
|
|
227
|
+
var googleErrorDataSchema = z.object({
|
|
228
|
+
error: z.object({
|
|
229
|
+
code: z.number().nullable(),
|
|
230
|
+
message: z.string(),
|
|
231
|
+
status: z.string()
|
|
232
|
+
})
|
|
233
|
+
});
|
|
234
|
+
var googleFailedResponseHandler = createJsonErrorResponseHandler({
|
|
235
|
+
errorSchema: googleErrorDataSchema,
|
|
236
|
+
errorToMessage: (data) => data.error.message
|
|
237
|
+
});
|
|
238
|
+
function prepareTools(mode, useSearchGrounding, dynamicRetrievalConfig, modelId) {
|
|
239
|
+
var _a, _b;
|
|
240
|
+
const tools = ((_a = mode.tools) == null ? void 0 : _a.length) ? mode.tools : void 0;
|
|
241
|
+
const toolWarnings = [];
|
|
242
|
+
const isGemini2 = modelId.includes("gemini-2");
|
|
243
|
+
const supportsDynamicRetrieval = modelId.includes("gemini-1.5-flash") && !modelId.includes("-8b");
|
|
244
|
+
if (useSearchGrounding) {
|
|
245
|
+
return {
|
|
246
|
+
tools: isGemini2 ? { googleSearch: {} } : {
|
|
247
|
+
googleSearchRetrieval: !supportsDynamicRetrieval || !dynamicRetrievalConfig ? {} : { dynamicRetrievalConfig }
|
|
248
|
+
},
|
|
249
|
+
toolConfig: void 0,
|
|
250
|
+
toolWarnings
|
|
251
|
+
};
|
|
252
|
+
}
|
|
253
|
+
if (tools == null) {
|
|
254
|
+
return { tools: void 0, toolConfig: void 0, toolWarnings };
|
|
255
|
+
}
|
|
256
|
+
const functionDeclarations = [];
|
|
257
|
+
for (const tool of tools) {
|
|
258
|
+
if (tool.type === "provider-defined") {
|
|
259
|
+
toolWarnings.push({ type: "unsupported-tool", tool });
|
|
260
|
+
} else {
|
|
261
|
+
functionDeclarations.push({
|
|
262
|
+
name: tool.name,
|
|
263
|
+
description: (_b = tool.description) != null ? _b : "",
|
|
264
|
+
parameters: convertJSONSchemaToOpenAPISchema(tool.parameters)
|
|
265
|
+
});
|
|
266
|
+
}
|
|
267
|
+
}
|
|
268
|
+
const toolChoice = mode.toolChoice;
|
|
269
|
+
if (toolChoice == null) {
|
|
270
|
+
return {
|
|
271
|
+
tools: { functionDeclarations },
|
|
272
|
+
toolConfig: void 0,
|
|
273
|
+
toolWarnings
|
|
274
|
+
};
|
|
275
|
+
}
|
|
276
|
+
const type = toolChoice.type;
|
|
277
|
+
switch (type) {
|
|
278
|
+
case "auto":
|
|
279
|
+
return {
|
|
280
|
+
tools: { functionDeclarations },
|
|
281
|
+
toolConfig: { functionCallingConfig: { mode: "AUTO" } },
|
|
282
|
+
toolWarnings
|
|
283
|
+
};
|
|
284
|
+
case "none":
|
|
285
|
+
return {
|
|
286
|
+
tools: { functionDeclarations },
|
|
287
|
+
toolConfig: { functionCallingConfig: { mode: "NONE" } },
|
|
288
|
+
toolWarnings
|
|
289
|
+
};
|
|
290
|
+
case "required":
|
|
291
|
+
return {
|
|
292
|
+
tools: { functionDeclarations },
|
|
293
|
+
toolConfig: { functionCallingConfig: { mode: "ANY" } },
|
|
294
|
+
toolWarnings
|
|
295
|
+
};
|
|
296
|
+
case "tool":
|
|
297
|
+
return {
|
|
298
|
+
tools: { functionDeclarations },
|
|
299
|
+
toolConfig: {
|
|
300
|
+
functionCallingConfig: {
|
|
301
|
+
mode: "ANY",
|
|
302
|
+
allowedFunctionNames: [toolChoice.toolName]
|
|
303
|
+
}
|
|
304
|
+
},
|
|
305
|
+
toolWarnings
|
|
306
|
+
};
|
|
307
|
+
default: {
|
|
308
|
+
const _exhaustiveCheck = type;
|
|
309
|
+
throw new UnsupportedFunctionalityError({
|
|
310
|
+
functionality: `Unsupported tool choice type: ${_exhaustiveCheck}`
|
|
311
|
+
});
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
function mapGoogleGenerativeAIFinishReason({
|
|
316
|
+
finishReason,
|
|
317
|
+
hasToolCalls
|
|
318
|
+
}) {
|
|
319
|
+
switch (finishReason) {
|
|
320
|
+
case "STOP":
|
|
321
|
+
return hasToolCalls ? "tool-calls" : "stop";
|
|
322
|
+
case "MAX_TOKENS":
|
|
323
|
+
return "length";
|
|
324
|
+
case "IMAGE_SAFETY":
|
|
325
|
+
case "RECITATION":
|
|
326
|
+
case "SAFETY":
|
|
327
|
+
case "BLOCKLIST":
|
|
328
|
+
case "PROHIBITED_CONTENT":
|
|
329
|
+
case "SPII":
|
|
330
|
+
return "content-filter";
|
|
331
|
+
case "FINISH_REASON_UNSPECIFIED":
|
|
332
|
+
case "OTHER":
|
|
333
|
+
return "other";
|
|
334
|
+
case "MALFORMED_FUNCTION_CALL":
|
|
335
|
+
return "error";
|
|
336
|
+
default:
|
|
337
|
+
return "unknown";
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
var GoogleGenerativeAILanguageModel = class {
|
|
341
|
+
constructor(modelId, settings, config) {
|
|
342
|
+
this.specificationVersion = "v1";
|
|
343
|
+
this.defaultObjectGenerationMode = "json";
|
|
344
|
+
this.supportsImageUrls = false;
|
|
345
|
+
this.modelId = modelId;
|
|
346
|
+
this.settings = settings;
|
|
347
|
+
this.config = config;
|
|
348
|
+
}
|
|
349
|
+
get supportsStructuredOutputs() {
|
|
350
|
+
var _a;
|
|
351
|
+
return (_a = this.settings.structuredOutputs) != null ? _a : true;
|
|
352
|
+
}
|
|
353
|
+
get provider() {
|
|
354
|
+
return this.config.provider;
|
|
355
|
+
}
|
|
356
|
+
async getArgs({
|
|
357
|
+
mode,
|
|
358
|
+
prompt,
|
|
359
|
+
maxTokens,
|
|
360
|
+
temperature,
|
|
361
|
+
topP,
|
|
362
|
+
topK,
|
|
363
|
+
frequencyPenalty,
|
|
364
|
+
presencePenalty,
|
|
365
|
+
stopSequences,
|
|
366
|
+
responseFormat,
|
|
367
|
+
seed,
|
|
368
|
+
providerMetadata
|
|
369
|
+
}) {
|
|
370
|
+
var _a, _b, _c;
|
|
371
|
+
const type = mode.type;
|
|
372
|
+
const warnings = [];
|
|
373
|
+
const googleOptions = parseProviderOptions({
|
|
374
|
+
provider: "google",
|
|
375
|
+
providerOptions: providerMetadata,
|
|
376
|
+
schema: googleGenerativeAIProviderOptionsSchema
|
|
377
|
+
});
|
|
378
|
+
if (((_a = googleOptions == null ? void 0 : googleOptions.thinkingConfig) == null ? void 0 : _a.includeThoughts) === true && !this.config.provider.startsWith("google.vertex.")) {
|
|
379
|
+
warnings.push({
|
|
380
|
+
type: "other",
|
|
381
|
+
message: `The 'includeThoughts' option is only supported with the Google Vertex provider and might not be supported or could behave unexpectedly with the current Google provider (${this.config.provider}).`
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
const generationConfig = {
|
|
385
|
+
// standardized settings:
|
|
386
|
+
maxOutputTokens: maxTokens,
|
|
387
|
+
temperature,
|
|
388
|
+
topK,
|
|
389
|
+
topP,
|
|
390
|
+
frequencyPenalty,
|
|
391
|
+
presencePenalty,
|
|
392
|
+
stopSequences,
|
|
393
|
+
seed,
|
|
394
|
+
// response format:
|
|
395
|
+
responseMimeType: (responseFormat == null ? void 0 : responseFormat.type) === "json" ? "application/json" : void 0,
|
|
396
|
+
responseSchema: (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
|
397
|
+
// so this is needed as an escape hatch:
|
|
398
|
+
this.supportsStructuredOutputs ? convertJSONSchemaToOpenAPISchema(responseFormat.schema) : void 0,
|
|
399
|
+
...this.settings.audioTimestamp && {
|
|
400
|
+
audioTimestamp: this.settings.audioTimestamp
|
|
401
|
+
},
|
|
402
|
+
// provider options:
|
|
403
|
+
responseModalities: googleOptions == null ? void 0 : googleOptions.responseModalities,
|
|
404
|
+
thinkingConfig: googleOptions == null ? void 0 : googleOptions.thinkingConfig
|
|
405
|
+
};
|
|
406
|
+
const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(prompt);
|
|
407
|
+
switch (type) {
|
|
408
|
+
case "regular": {
|
|
409
|
+
const { tools, toolConfig, toolWarnings } = prepareTools(
|
|
410
|
+
mode,
|
|
411
|
+
(_b = this.settings.useSearchGrounding) != null ? _b : false,
|
|
412
|
+
this.settings.dynamicRetrievalConfig,
|
|
413
|
+
this.modelId
|
|
414
|
+
);
|
|
415
|
+
return {
|
|
416
|
+
args: {
|
|
417
|
+
generationConfig,
|
|
418
|
+
contents,
|
|
419
|
+
systemInstruction,
|
|
420
|
+
safetySettings: this.settings.safetySettings,
|
|
421
|
+
tools,
|
|
422
|
+
toolConfig,
|
|
423
|
+
cachedContent: this.settings.cachedContent
|
|
424
|
+
},
|
|
425
|
+
warnings: [...warnings, ...toolWarnings]
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
case "object-json": {
|
|
429
|
+
return {
|
|
430
|
+
args: {
|
|
431
|
+
generationConfig: {
|
|
432
|
+
...generationConfig,
|
|
433
|
+
responseMimeType: "application/json",
|
|
434
|
+
responseSchema: mode.schema != null && // Google GenAI does not support all OpenAPI Schema features,
|
|
435
|
+
// so this is needed as an escape hatch:
|
|
436
|
+
this.supportsStructuredOutputs ? convertJSONSchemaToOpenAPISchema(mode.schema) : void 0
|
|
437
|
+
},
|
|
438
|
+
contents,
|
|
439
|
+
systemInstruction,
|
|
440
|
+
safetySettings: this.settings.safetySettings,
|
|
441
|
+
cachedContent: this.settings.cachedContent
|
|
442
|
+
},
|
|
443
|
+
warnings
|
|
444
|
+
};
|
|
445
|
+
}
|
|
446
|
+
case "object-tool": {
|
|
447
|
+
return {
|
|
448
|
+
args: {
|
|
449
|
+
generationConfig,
|
|
450
|
+
contents,
|
|
451
|
+
systemInstruction,
|
|
452
|
+
tools: {
|
|
453
|
+
functionDeclarations: [
|
|
454
|
+
{
|
|
455
|
+
name: mode.tool.name,
|
|
456
|
+
description: (_c = mode.tool.description) != null ? _c : "",
|
|
457
|
+
parameters: convertJSONSchemaToOpenAPISchema(
|
|
458
|
+
mode.tool.parameters
|
|
459
|
+
)
|
|
460
|
+
}
|
|
461
|
+
]
|
|
462
|
+
},
|
|
463
|
+
toolConfig: { functionCallingConfig: { mode: "ANY" } },
|
|
464
|
+
safetySettings: this.settings.safetySettings,
|
|
465
|
+
cachedContent: this.settings.cachedContent
|
|
466
|
+
},
|
|
467
|
+
warnings
|
|
468
|
+
};
|
|
469
|
+
}
|
|
470
|
+
default: {
|
|
471
|
+
const _exhaustiveCheck = type;
|
|
472
|
+
throw new Error(`Unsupported type: ${_exhaustiveCheck}`);
|
|
473
|
+
}
|
|
474
|
+
}
|
|
475
|
+
}
|
|
476
|
+
supportsUrl(url) {
|
|
477
|
+
return this.config.isSupportedUrl(url);
|
|
478
|
+
}
|
|
479
|
+
async doGenerate(options) {
|
|
480
|
+
var _a, _b, _c, _d, _e;
|
|
481
|
+
const { args, warnings } = await this.getArgs(options);
|
|
482
|
+
const body = JSON.stringify(args);
|
|
483
|
+
const mergedHeaders = combineHeaders(
|
|
484
|
+
await resolve(this.config.headers),
|
|
485
|
+
options.headers
|
|
486
|
+
);
|
|
487
|
+
const {
|
|
488
|
+
responseHeaders,
|
|
489
|
+
value: response,
|
|
490
|
+
rawValue: rawResponse
|
|
491
|
+
} = await postJsonToApi({
|
|
492
|
+
url: `${this.config.baseURL}/${getModelPath(
|
|
493
|
+
this.modelId
|
|
494
|
+
)}:generateContent`,
|
|
495
|
+
headers: mergedHeaders,
|
|
496
|
+
body: args,
|
|
497
|
+
failedResponseHandler: googleFailedResponseHandler,
|
|
498
|
+
successfulResponseHandler: createJsonResponseHandler(responseSchema),
|
|
499
|
+
abortSignal: options.abortSignal,
|
|
500
|
+
fetch: this.config.fetch
|
|
501
|
+
});
|
|
502
|
+
const { contents: rawPrompt, ...rawSettings } = args;
|
|
503
|
+
const candidate = response.candidates[0];
|
|
504
|
+
const parts = candidate.content == null || typeof candidate.content !== "object" || !("parts" in candidate.content) ? [] : candidate.content.parts;
|
|
505
|
+
const toolCalls = getToolCallsFromParts({
|
|
506
|
+
parts,
|
|
507
|
+
// Use candidateParts
|
|
508
|
+
generateId: this.config.generateId
|
|
509
|
+
});
|
|
510
|
+
const usageMetadata = response.usageMetadata;
|
|
511
|
+
return {
|
|
512
|
+
text: getTextFromParts(parts),
|
|
513
|
+
reasoning: getReasoningDetailsFromParts(parts),
|
|
514
|
+
files: (_a = getInlineDataParts(parts)) == null ? void 0 : _a.map((part) => ({
|
|
515
|
+
data: part.inlineData.data,
|
|
516
|
+
mimeType: part.inlineData.mimeType
|
|
517
|
+
})),
|
|
518
|
+
toolCalls,
|
|
519
|
+
finishReason: mapGoogleGenerativeAIFinishReason({
|
|
520
|
+
finishReason: candidate.finishReason,
|
|
521
|
+
hasToolCalls: toolCalls != null && toolCalls.length > 0
|
|
522
|
+
}),
|
|
523
|
+
usage: {
|
|
524
|
+
promptTokens: (_b = usageMetadata == null ? void 0 : usageMetadata.promptTokenCount) != null ? _b : NaN,
|
|
525
|
+
completionTokens: (_c = usageMetadata == null ? void 0 : usageMetadata.candidatesTokenCount) != null ? _c : NaN
|
|
526
|
+
},
|
|
527
|
+
rawCall: { rawPrompt, rawSettings },
|
|
528
|
+
rawResponse: { headers: responseHeaders, body: rawResponse },
|
|
529
|
+
warnings,
|
|
530
|
+
providerMetadata: {
|
|
531
|
+
google: {
|
|
532
|
+
groundingMetadata: (_d = candidate.groundingMetadata) != null ? _d : null,
|
|
533
|
+
safetyRatings: (_e = candidate.safetyRatings) != null ? _e : null
|
|
534
|
+
}
|
|
535
|
+
},
|
|
536
|
+
sources: extractSources({
|
|
537
|
+
groundingMetadata: candidate.groundingMetadata,
|
|
538
|
+
generateId: this.config.generateId
|
|
539
|
+
}),
|
|
540
|
+
request: { body }
|
|
541
|
+
};
|
|
542
|
+
}
|
|
543
|
+
async doStream(options) {
|
|
544
|
+
const { args, warnings } = await this.getArgs(options);
|
|
545
|
+
const body = JSON.stringify(args);
|
|
546
|
+
const headers = combineHeaders(
|
|
547
|
+
await resolve(this.config.headers),
|
|
548
|
+
options.headers
|
|
549
|
+
);
|
|
550
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
551
|
+
url: `${this.config.baseURL}/${getModelPath(
|
|
552
|
+
this.modelId
|
|
553
|
+
)}:streamGenerateContent?alt=sse`,
|
|
554
|
+
headers,
|
|
555
|
+
body: args,
|
|
556
|
+
failedResponseHandler: googleFailedResponseHandler,
|
|
557
|
+
successfulResponseHandler: createEventSourceResponseHandler(chunkSchema),
|
|
558
|
+
abortSignal: options.abortSignal,
|
|
559
|
+
fetch: this.config.fetch
|
|
560
|
+
});
|
|
561
|
+
const { contents: rawPrompt, ...rawSettings } = args;
|
|
562
|
+
let finishReason = "unknown";
|
|
563
|
+
let usage = {
|
|
564
|
+
promptTokens: Number.NaN,
|
|
565
|
+
completionTokens: Number.NaN
|
|
566
|
+
};
|
|
567
|
+
let providerMetadata = void 0;
|
|
568
|
+
const generateId2 = this.config.generateId;
|
|
569
|
+
let hasToolCalls = false;
|
|
570
|
+
return {
|
|
571
|
+
stream: response.pipeThrough(
|
|
572
|
+
new TransformStream({
|
|
573
|
+
transform(chunk, controller) {
|
|
574
|
+
var _a, _b, _c, _d, _e, _f;
|
|
575
|
+
if (!chunk.success) {
|
|
576
|
+
controller.enqueue({ type: "error", error: chunk.error });
|
|
577
|
+
return;
|
|
578
|
+
}
|
|
579
|
+
const value = chunk.value;
|
|
580
|
+
const usageMetadata = value.usageMetadata;
|
|
581
|
+
if (usageMetadata != null) {
|
|
582
|
+
usage = {
|
|
583
|
+
promptTokens: (_a = usageMetadata.promptTokenCount) != null ? _a : NaN,
|
|
584
|
+
completionTokens: (_b = usageMetadata.candidatesTokenCount) != null ? _b : NaN
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
const candidate = (_c = value.candidates) == null ? void 0 : _c[0];
|
|
588
|
+
if (candidate == null) {
|
|
589
|
+
return;
|
|
590
|
+
}
|
|
591
|
+
const content = candidate.content;
|
|
592
|
+
if (content != null) {
|
|
593
|
+
const deltaText = getTextFromParts(content.parts);
|
|
594
|
+
if (deltaText != null) {
|
|
595
|
+
controller.enqueue({
|
|
596
|
+
type: "text-delta",
|
|
597
|
+
textDelta: deltaText
|
|
598
|
+
});
|
|
599
|
+
}
|
|
600
|
+
const reasoningDeltaText = getReasoningDetailsFromParts(
|
|
601
|
+
content.parts
|
|
602
|
+
);
|
|
603
|
+
if (reasoningDeltaText != null) {
|
|
604
|
+
for (const part of reasoningDeltaText) {
|
|
605
|
+
controller.enqueue({
|
|
606
|
+
type: "reasoning",
|
|
607
|
+
textDelta: part.text
|
|
608
|
+
});
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
const inlineDataParts = getInlineDataParts(content.parts);
|
|
612
|
+
if (inlineDataParts != null) {
|
|
613
|
+
for (const part of inlineDataParts) {
|
|
614
|
+
controller.enqueue({
|
|
615
|
+
type: "file",
|
|
616
|
+
mimeType: part.inlineData.mimeType,
|
|
617
|
+
data: part.inlineData.data
|
|
618
|
+
});
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
const toolCallDeltas = getToolCallsFromParts({
|
|
622
|
+
parts: content.parts,
|
|
623
|
+
generateId: generateId2
|
|
624
|
+
});
|
|
625
|
+
if (toolCallDeltas != null) {
|
|
626
|
+
for (const toolCall of toolCallDeltas) {
|
|
627
|
+
controller.enqueue({
|
|
628
|
+
type: "tool-call-delta",
|
|
629
|
+
toolCallType: "function",
|
|
630
|
+
toolCallId: toolCall.toolCallId,
|
|
631
|
+
toolName: toolCall.toolName,
|
|
632
|
+
argsTextDelta: toolCall.args
|
|
633
|
+
});
|
|
634
|
+
controller.enqueue({
|
|
635
|
+
type: "tool-call",
|
|
636
|
+
toolCallType: "function",
|
|
637
|
+
toolCallId: toolCall.toolCallId,
|
|
638
|
+
toolName: toolCall.toolName,
|
|
639
|
+
args: toolCall.args
|
|
640
|
+
});
|
|
641
|
+
hasToolCalls = true;
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
if (candidate.finishReason != null) {
|
|
646
|
+
finishReason = mapGoogleGenerativeAIFinishReason({
|
|
647
|
+
finishReason: candidate.finishReason,
|
|
648
|
+
hasToolCalls
|
|
649
|
+
});
|
|
650
|
+
const sources = (_d = extractSources({
|
|
651
|
+
groundingMetadata: candidate.groundingMetadata,
|
|
652
|
+
generateId: generateId2
|
|
653
|
+
})) != null ? _d : [];
|
|
654
|
+
for (const source of sources) {
|
|
655
|
+
controller.enqueue({ type: "source", source });
|
|
656
|
+
}
|
|
657
|
+
providerMetadata = {
|
|
658
|
+
google: {
|
|
659
|
+
groundingMetadata: (_e = candidate.groundingMetadata) != null ? _e : null,
|
|
660
|
+
safetyRatings: (_f = candidate.safetyRatings) != null ? _f : null
|
|
661
|
+
}
|
|
662
|
+
};
|
|
663
|
+
}
|
|
664
|
+
},
|
|
665
|
+
flush(controller) {
|
|
666
|
+
controller.enqueue({
|
|
667
|
+
type: "finish",
|
|
668
|
+
finishReason,
|
|
669
|
+
usage,
|
|
670
|
+
providerMetadata
|
|
671
|
+
});
|
|
672
|
+
}
|
|
673
|
+
})
|
|
674
|
+
),
|
|
675
|
+
rawCall: { rawPrompt, rawSettings },
|
|
676
|
+
rawResponse: { headers: responseHeaders },
|
|
677
|
+
warnings,
|
|
678
|
+
request: { body }
|
|
679
|
+
};
|
|
680
|
+
}
|
|
681
|
+
};
|
|
682
|
+
function getToolCallsFromParts({
|
|
683
|
+
parts,
|
|
684
|
+
generateId: generateId2
|
|
685
|
+
}) {
|
|
686
|
+
const functionCallParts = parts == null ? void 0 : parts.filter(
|
|
687
|
+
(part) => "functionCall" in part
|
|
688
|
+
);
|
|
689
|
+
return functionCallParts == null || functionCallParts.length === 0 ? void 0 : functionCallParts.map((part) => ({
|
|
690
|
+
toolCallType: "function",
|
|
691
|
+
toolCallId: generateId2(),
|
|
692
|
+
toolName: part.functionCall.name,
|
|
693
|
+
args: JSON.stringify(part.functionCall.args)
|
|
694
|
+
}));
|
|
695
|
+
}
|
|
696
|
+
function getTextFromParts(parts) {
|
|
697
|
+
const textParts = parts == null ? void 0 : parts.filter(
|
|
698
|
+
(part) => "text" in part && part.thought !== true
|
|
699
|
+
);
|
|
700
|
+
return textParts == null || textParts.length === 0 ? void 0 : textParts.map((part) => part.text).join("");
|
|
701
|
+
}
|
|
702
|
+
function getReasoningDetailsFromParts(parts) {
|
|
703
|
+
const reasoningParts = parts == null ? void 0 : parts.filter(
|
|
704
|
+
(part) => "text" in part && part.thought === true && part.text != null
|
|
705
|
+
);
|
|
706
|
+
return reasoningParts == null || reasoningParts.length === 0 ? void 0 : reasoningParts.map((part) => ({ type: "text", text: part.text }));
|
|
707
|
+
}
|
|
708
|
+
function getInlineDataParts(parts) {
|
|
709
|
+
return parts == null ? void 0 : parts.filter(
|
|
710
|
+
(part) => "inlineData" in part
|
|
711
|
+
);
|
|
712
|
+
}
|
|
713
|
+
function extractSources({
|
|
714
|
+
groundingMetadata,
|
|
715
|
+
generateId: generateId2
|
|
716
|
+
}) {
|
|
717
|
+
var _a;
|
|
718
|
+
return (_a = groundingMetadata == null ? void 0 : groundingMetadata.groundingChunks) == null ? void 0 : _a.filter(
|
|
719
|
+
(chunk) => chunk.web != null
|
|
720
|
+
).map((chunk) => ({
|
|
721
|
+
sourceType: "url",
|
|
722
|
+
id: generateId2(),
|
|
723
|
+
url: chunk.web.uri,
|
|
724
|
+
title: chunk.web.title
|
|
725
|
+
}));
|
|
726
|
+
}
|
|
727
|
+
var contentSchema = z.object({
|
|
728
|
+
parts: z.array(
|
|
729
|
+
z.union([
|
|
730
|
+
// note: order matters since text can be fully empty
|
|
731
|
+
z.object({
|
|
732
|
+
functionCall: z.object({
|
|
733
|
+
name: z.string(),
|
|
734
|
+
args: z.unknown()
|
|
735
|
+
})
|
|
736
|
+
}),
|
|
737
|
+
z.object({
|
|
738
|
+
inlineData: z.object({
|
|
739
|
+
mimeType: z.string(),
|
|
740
|
+
data: z.string()
|
|
741
|
+
})
|
|
742
|
+
}),
|
|
743
|
+
z.object({
|
|
744
|
+
text: z.string().nullish(),
|
|
745
|
+
thought: z.boolean().nullish()
|
|
746
|
+
})
|
|
747
|
+
])
|
|
748
|
+
).nullish()
|
|
749
|
+
});
|
|
750
|
+
var groundingChunkSchema = z.object({
|
|
751
|
+
web: z.object({ uri: z.string(), title: z.string() }).nullish(),
|
|
752
|
+
retrievedContext: z.object({ uri: z.string(), title: z.string() }).nullish()
|
|
753
|
+
});
|
|
754
|
+
var groundingMetadataSchema = z.object({
|
|
755
|
+
webSearchQueries: z.array(z.string()).nullish(),
|
|
756
|
+
retrievalQueries: z.array(z.string()).nullish(),
|
|
757
|
+
searchEntryPoint: z.object({ renderedContent: z.string() }).nullish(),
|
|
758
|
+
groundingChunks: z.array(groundingChunkSchema).nullish(),
|
|
759
|
+
groundingSupports: z.array(
|
|
760
|
+
z.object({
|
|
761
|
+
segment: z.object({
|
|
762
|
+
startIndex: z.number().nullish(),
|
|
763
|
+
endIndex: z.number().nullish(),
|
|
764
|
+
text: z.string().nullish()
|
|
765
|
+
}),
|
|
766
|
+
segment_text: z.string().nullish(),
|
|
767
|
+
groundingChunkIndices: z.array(z.number()).nullish(),
|
|
768
|
+
supportChunkIndices: z.array(z.number()).nullish(),
|
|
769
|
+
confidenceScores: z.array(z.number()).nullish(),
|
|
770
|
+
confidenceScore: z.array(z.number()).nullish()
|
|
771
|
+
})
|
|
772
|
+
).nullish(),
|
|
773
|
+
retrievalMetadata: z.union([
|
|
774
|
+
z.object({
|
|
775
|
+
webDynamicRetrievalScore: z.number()
|
|
776
|
+
}),
|
|
777
|
+
z.object({})
|
|
778
|
+
]).nullish()
|
|
779
|
+
});
|
|
780
|
+
var safetyRatingSchema = z.object({
|
|
781
|
+
category: z.string().nullish(),
|
|
782
|
+
probability: z.string().nullish(),
|
|
783
|
+
probabilityScore: z.number().nullish(),
|
|
784
|
+
severity: z.string().nullish(),
|
|
785
|
+
severityScore: z.number().nullish(),
|
|
786
|
+
blocked: z.boolean().nullish()
|
|
787
|
+
});
|
|
788
|
+
var responseSchema = z.object({
|
|
789
|
+
candidates: z.array(
|
|
790
|
+
z.object({
|
|
791
|
+
content: contentSchema.nullish().or(z.object({}).strict()),
|
|
792
|
+
finishReason: z.string().nullish(),
|
|
793
|
+
safetyRatings: z.array(safetyRatingSchema).nullish(),
|
|
794
|
+
groundingMetadata: groundingMetadataSchema.nullish()
|
|
795
|
+
})
|
|
796
|
+
),
|
|
797
|
+
usageMetadata: z.object({
|
|
798
|
+
promptTokenCount: z.number().nullish(),
|
|
799
|
+
candidatesTokenCount: z.number().nullish(),
|
|
800
|
+
totalTokenCount: z.number().nullish()
|
|
801
|
+
}).nullish()
|
|
802
|
+
});
|
|
803
|
+
var chunkSchema = z.object({
|
|
804
|
+
candidates: z.array(
|
|
805
|
+
z.object({
|
|
806
|
+
content: contentSchema.nullish(),
|
|
807
|
+
finishReason: z.string().nullish(),
|
|
808
|
+
safetyRatings: z.array(safetyRatingSchema).nullish(),
|
|
809
|
+
groundingMetadata: groundingMetadataSchema.nullish()
|
|
810
|
+
})
|
|
811
|
+
).nullish(),
|
|
812
|
+
usageMetadata: z.object({
|
|
813
|
+
promptTokenCount: z.number().nullish(),
|
|
814
|
+
candidatesTokenCount: z.number().nullish(),
|
|
815
|
+
totalTokenCount: z.number().nullish()
|
|
816
|
+
}).nullish()
|
|
817
|
+
});
|
|
818
|
+
var googleGenerativeAIProviderOptionsSchema = z.object({
|
|
819
|
+
responseModalities: z.array(z.enum(["TEXT", "IMAGE"])).nullish(),
|
|
820
|
+
thinkingConfig: z.object({
|
|
821
|
+
thinkingBudget: z.number().nullish(),
|
|
822
|
+
includeThoughts: z.boolean().nullish()
|
|
823
|
+
}).nullish()
|
|
824
|
+
});
|
|
825
|
+
var GoogleGenerativeAIEmbeddingModel = class {
|
|
826
|
+
constructor(modelId, settings, config) {
|
|
827
|
+
this.specificationVersion = "v1";
|
|
828
|
+
this.modelId = modelId;
|
|
829
|
+
this.settings = settings;
|
|
830
|
+
this.config = config;
|
|
831
|
+
}
|
|
832
|
+
get provider() {
|
|
833
|
+
return this.config.provider;
|
|
834
|
+
}
|
|
835
|
+
get maxEmbeddingsPerCall() {
|
|
836
|
+
return 2048;
|
|
837
|
+
}
|
|
838
|
+
get supportsParallelCalls() {
|
|
839
|
+
return true;
|
|
840
|
+
}
|
|
841
|
+
async doEmbed({
|
|
842
|
+
values,
|
|
843
|
+
headers,
|
|
844
|
+
abortSignal
|
|
845
|
+
}) {
|
|
846
|
+
if (values.length > this.maxEmbeddingsPerCall) {
|
|
847
|
+
throw new TooManyEmbeddingValuesForCallError({
|
|
848
|
+
provider: this.provider,
|
|
849
|
+
modelId: this.modelId,
|
|
850
|
+
maxEmbeddingsPerCall: this.maxEmbeddingsPerCall,
|
|
851
|
+
values
|
|
852
|
+
});
|
|
853
|
+
}
|
|
854
|
+
const mergedHeaders = combineHeaders(
|
|
855
|
+
await resolve(this.config.headers),
|
|
856
|
+
headers
|
|
857
|
+
);
|
|
858
|
+
const { responseHeaders, value: response } = await postJsonToApi({
|
|
859
|
+
url: `${this.config.baseURL}/models/${this.modelId}:batchEmbedContents`,
|
|
860
|
+
headers: mergedHeaders,
|
|
861
|
+
body: {
|
|
862
|
+
requests: values.map((value) => ({
|
|
863
|
+
model: `models/${this.modelId}`,
|
|
864
|
+
content: { role: "user", parts: [{ text: value }] },
|
|
865
|
+
outputDimensionality: this.settings.outputDimensionality,
|
|
866
|
+
taskType: this.settings.taskType
|
|
867
|
+
}))
|
|
868
|
+
},
|
|
869
|
+
failedResponseHandler: googleFailedResponseHandler,
|
|
870
|
+
successfulResponseHandler: createJsonResponseHandler(
|
|
871
|
+
googleGenerativeAITextEmbeddingResponseSchema
|
|
872
|
+
),
|
|
873
|
+
abortSignal,
|
|
874
|
+
fetch: this.config.fetch
|
|
875
|
+
});
|
|
876
|
+
return {
|
|
877
|
+
embeddings: response.embeddings.map((item) => item.values),
|
|
878
|
+
usage: void 0,
|
|
879
|
+
rawResponse: { headers: responseHeaders }
|
|
880
|
+
};
|
|
881
|
+
}
|
|
882
|
+
};
|
|
883
|
+
var googleGenerativeAITextEmbeddingResponseSchema = z.object({
|
|
884
|
+
embeddings: z.array(z.object({ values: z.array(z.number()) }))
|
|
885
|
+
});
|
|
886
|
+
function isSupportedFileUrl(url) {
|
|
887
|
+
return url.toString().startsWith("https://generativelanguage.googleapis.com/v1beta/files/");
|
|
888
|
+
}
|
|
889
|
+
function createGoogleGenerativeAI(options = {}) {
|
|
890
|
+
var _a;
|
|
891
|
+
const baseURL = (_a = withoutTrailingSlash(options.baseURL)) != null ? _a : "https://generativelanguage.googleapis.com/v1beta";
|
|
892
|
+
const getHeaders = () => ({
|
|
893
|
+
"x-goog-api-key": loadApiKey({
|
|
894
|
+
apiKey: options.apiKey,
|
|
895
|
+
environmentVariableName: "GOOGLE_GENERATIVE_AI_API_KEY",
|
|
896
|
+
description: "Google Generative AI"
|
|
897
|
+
}),
|
|
898
|
+
...options.headers
|
|
899
|
+
});
|
|
900
|
+
const createChatModel = (modelId, settings = {}) => {
|
|
901
|
+
var _a2;
|
|
902
|
+
return new GoogleGenerativeAILanguageModel(modelId, settings, {
|
|
903
|
+
provider: "google.generative-ai",
|
|
904
|
+
baseURL,
|
|
905
|
+
headers: getHeaders,
|
|
906
|
+
generateId: (_a2 = options.generateId) != null ? _a2 : generateId,
|
|
907
|
+
isSupportedUrl: isSupportedFileUrl,
|
|
908
|
+
fetch: options.fetch
|
|
909
|
+
});
|
|
910
|
+
};
|
|
911
|
+
const createEmbeddingModel = (modelId, settings = {}) => new GoogleGenerativeAIEmbeddingModel(modelId, settings, {
|
|
912
|
+
provider: "google.generative-ai",
|
|
913
|
+
baseURL,
|
|
914
|
+
headers: getHeaders,
|
|
915
|
+
fetch: options.fetch
|
|
916
|
+
});
|
|
917
|
+
const provider = function(modelId, settings) {
|
|
918
|
+
if (new.target) {
|
|
919
|
+
throw new Error(
|
|
920
|
+
"The Google Generative AI model function cannot be called with the new keyword."
|
|
921
|
+
);
|
|
922
|
+
}
|
|
923
|
+
return createChatModel(modelId, settings);
|
|
924
|
+
};
|
|
925
|
+
provider.languageModel = createChatModel;
|
|
926
|
+
provider.chat = createChatModel;
|
|
927
|
+
provider.generativeAI = createChatModel;
|
|
928
|
+
provider.embedding = createEmbeddingModel;
|
|
929
|
+
provider.textEmbedding = createEmbeddingModel;
|
|
930
|
+
provider.textEmbeddingModel = createEmbeddingModel;
|
|
931
|
+
return provider;
|
|
932
|
+
}
|
|
933
|
+
var google = createGoogleGenerativeAI();
|
|
934
|
+
|
|
935
|
+
export { createGoogleGenerativeAI, google };
|
|
936
|
+
//# sourceMappingURL=dist-PQZUVLPC.js.map
|
|
937
|
+
//# sourceMappingURL=dist-PQZUVLPC.js.map
|