@ai-sdk/google 3.0.51 → 3.0.52
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 +15 -0
- package/dist/index.js +123 -39
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +120 -36
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +122 -38
- package/dist/internal/index.js.map +1 -1
- package/dist/internal/index.mjs +119 -35
- package/dist/internal/index.mjs.map +1 -1
- package/package.json +3 -3
- package/src/convert-to-google-generative-ai-messages.ts +163 -32
- package/src/google-generative-ai-language-model.ts +6 -1
- package/src/google-generative-ai-prompt.ts +12 -2
|
@@ -2,22 +2,179 @@ import {
|
|
|
2
2
|
LanguageModelV3Prompt,
|
|
3
3
|
UnsupportedFunctionalityError,
|
|
4
4
|
} from '@ai-sdk/provider';
|
|
5
|
+
import { convertToBase64 } from '@ai-sdk/provider-utils';
|
|
5
6
|
import {
|
|
6
7
|
GoogleGenerativeAIContent,
|
|
7
8
|
GoogleGenerativeAIContentPart,
|
|
9
|
+
GoogleGenerativeAIFunctionResponsePart,
|
|
8
10
|
GoogleGenerativeAIPrompt,
|
|
9
11
|
} from './google-generative-ai-prompt';
|
|
10
|
-
|
|
12
|
+
|
|
13
|
+
const dataUrlRegex = /^data:([^;,]+);base64,(.+)$/s;
|
|
14
|
+
|
|
15
|
+
function parseBase64DataUrl(
|
|
16
|
+
value: string,
|
|
17
|
+
): { mediaType: string; data: string } | undefined {
|
|
18
|
+
const match = dataUrlRegex.exec(value);
|
|
19
|
+
if (match == null) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
return {
|
|
24
|
+
mediaType: match[1],
|
|
25
|
+
data: match[2],
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function convertUrlToolResultPart(
|
|
30
|
+
url: string,
|
|
31
|
+
): GoogleGenerativeAIFunctionResponsePart | undefined {
|
|
32
|
+
// Per https://ai.google.dev/api/caching#FunctionResponsePart, only inline data is supported.
|
|
33
|
+
// https://docs.cloud.google.com/vertex-ai/generative-ai/docs/model-reference/function-calling#functionresponsepart suggests that this
|
|
34
|
+
// may be different for Vertex, but this needs to be confirmed and further tested for both APIs.
|
|
35
|
+
const parsedDataUrl = parseBase64DataUrl(url);
|
|
36
|
+
if (parsedDataUrl == null) {
|
|
37
|
+
return undefined;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
return {
|
|
41
|
+
inlineData: {
|
|
42
|
+
mimeType: parsedDataUrl.mediaType,
|
|
43
|
+
data: parsedDataUrl.data,
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
/*
|
|
49
|
+
* Appends tool result content parts to the message using the functionResponse
|
|
50
|
+
* format with support for multimodal parts (e.g. inline images/files alongside
|
|
51
|
+
* text). This format is supported by Gemini 3+ models.
|
|
52
|
+
*/
|
|
53
|
+
function appendToolResultParts(
|
|
54
|
+
parts: GoogleGenerativeAIContentPart[],
|
|
55
|
+
toolName: string,
|
|
56
|
+
outputValue: Array<{
|
|
57
|
+
type: string;
|
|
58
|
+
[key: string]: unknown;
|
|
59
|
+
}>,
|
|
60
|
+
): void {
|
|
61
|
+
const functionResponseParts: GoogleGenerativeAIFunctionResponsePart[] = [];
|
|
62
|
+
const responseTextParts: string[] = [];
|
|
63
|
+
|
|
64
|
+
for (const contentPart of outputValue) {
|
|
65
|
+
switch (contentPart.type) {
|
|
66
|
+
case 'text': {
|
|
67
|
+
responseTextParts.push(contentPart.text as string);
|
|
68
|
+
break;
|
|
69
|
+
}
|
|
70
|
+
case 'image-data':
|
|
71
|
+
case 'file-data': {
|
|
72
|
+
functionResponseParts.push({
|
|
73
|
+
inlineData: {
|
|
74
|
+
mimeType: contentPart.mediaType as string,
|
|
75
|
+
data: contentPart.data as string,
|
|
76
|
+
},
|
|
77
|
+
});
|
|
78
|
+
break;
|
|
79
|
+
}
|
|
80
|
+
case 'image-url':
|
|
81
|
+
case 'file-url': {
|
|
82
|
+
const functionResponsePart = convertUrlToolResultPart(
|
|
83
|
+
contentPart.url as string,
|
|
84
|
+
);
|
|
85
|
+
|
|
86
|
+
if (functionResponsePart != null) {
|
|
87
|
+
functionResponseParts.push(functionResponsePart);
|
|
88
|
+
} else {
|
|
89
|
+
responseTextParts.push(JSON.stringify(contentPart));
|
|
90
|
+
}
|
|
91
|
+
break;
|
|
92
|
+
}
|
|
93
|
+
default: {
|
|
94
|
+
responseTextParts.push(JSON.stringify(contentPart));
|
|
95
|
+
break;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
parts.push({
|
|
101
|
+
functionResponse: {
|
|
102
|
+
name: toolName,
|
|
103
|
+
response: {
|
|
104
|
+
name: toolName,
|
|
105
|
+
content:
|
|
106
|
+
responseTextParts.length > 0
|
|
107
|
+
? responseTextParts.join('\n')
|
|
108
|
+
: 'Tool executed successfully.',
|
|
109
|
+
},
|
|
110
|
+
...(functionResponseParts.length > 0
|
|
111
|
+
? { parts: functionResponseParts }
|
|
112
|
+
: {}),
|
|
113
|
+
},
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
/*
|
|
118
|
+
* Appends tool result content parts using a legacy format for pre-Gemini 3
|
|
119
|
+
* models that do not support multimodal parts within functionResponse. Instead,
|
|
120
|
+
* non-text content like images is sent as separate top-level inlineData parts.
|
|
121
|
+
*/
|
|
122
|
+
function appendLegacyToolResultParts(
|
|
123
|
+
parts: GoogleGenerativeAIContentPart[],
|
|
124
|
+
toolName: string,
|
|
125
|
+
outputValue: Array<{
|
|
126
|
+
type: string;
|
|
127
|
+
[key: string]: unknown;
|
|
128
|
+
}>,
|
|
129
|
+
): void {
|
|
130
|
+
for (const contentPart of outputValue) {
|
|
131
|
+
switch (contentPart.type) {
|
|
132
|
+
case 'text':
|
|
133
|
+
parts.push({
|
|
134
|
+
functionResponse: {
|
|
135
|
+
name: toolName,
|
|
136
|
+
response: {
|
|
137
|
+
name: toolName,
|
|
138
|
+
content: contentPart.text,
|
|
139
|
+
},
|
|
140
|
+
},
|
|
141
|
+
});
|
|
142
|
+
break;
|
|
143
|
+
case 'image-data':
|
|
144
|
+
parts.push(
|
|
145
|
+
{
|
|
146
|
+
inlineData: {
|
|
147
|
+
mimeType: String(contentPart.mediaType),
|
|
148
|
+
data: String(contentPart.data),
|
|
149
|
+
},
|
|
150
|
+
},
|
|
151
|
+
{
|
|
152
|
+
text: 'Tool executed successfully and returned this image as a response',
|
|
153
|
+
},
|
|
154
|
+
);
|
|
155
|
+
break;
|
|
156
|
+
default:
|
|
157
|
+
parts.push({ text: JSON.stringify(contentPart) });
|
|
158
|
+
break;
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
}
|
|
11
162
|
|
|
12
163
|
export function convertToGoogleGenerativeAIMessages(
|
|
13
164
|
prompt: LanguageModelV3Prompt,
|
|
14
|
-
options?: {
|
|
165
|
+
options?: {
|
|
166
|
+
isGemmaModel?: boolean;
|
|
167
|
+
providerOptionsName?: string;
|
|
168
|
+
supportsFunctionResponseParts?: boolean;
|
|
169
|
+
},
|
|
15
170
|
): GoogleGenerativeAIPrompt {
|
|
16
171
|
const systemInstructionParts: Array<{ text: string }> = [];
|
|
17
172
|
const contents: Array<GoogleGenerativeAIContent> = [];
|
|
18
173
|
let systemMessagesAllowed = true;
|
|
19
174
|
const isGemmaModel = options?.isGemmaModel ?? false;
|
|
20
175
|
const providerOptionsName = options?.providerOptionsName ?? 'google';
|
|
176
|
+
const supportsFunctionResponseParts =
|
|
177
|
+
options?.supportsFunctionResponseParts ?? true;
|
|
21
178
|
|
|
22
179
|
for (const { role, content } of prompt) {
|
|
23
180
|
switch (role) {
|
|
@@ -160,36 +317,10 @@ export function convertToGoogleGenerativeAIMessages(
|
|
|
160
317
|
const output = part.output;
|
|
161
318
|
|
|
162
319
|
if (output.type === 'content') {
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
functionResponse: {
|
|
168
|
-
name: part.toolName,
|
|
169
|
-
response: {
|
|
170
|
-
name: part.toolName,
|
|
171
|
-
content: contentPart.text,
|
|
172
|
-
},
|
|
173
|
-
},
|
|
174
|
-
});
|
|
175
|
-
break;
|
|
176
|
-
case 'image-data':
|
|
177
|
-
parts.push(
|
|
178
|
-
{
|
|
179
|
-
inlineData: {
|
|
180
|
-
mimeType: contentPart.mediaType,
|
|
181
|
-
data: contentPart.data,
|
|
182
|
-
},
|
|
183
|
-
},
|
|
184
|
-
{
|
|
185
|
-
text: 'Tool executed successfully and returned this image as a response',
|
|
186
|
-
},
|
|
187
|
-
);
|
|
188
|
-
break;
|
|
189
|
-
default:
|
|
190
|
-
parts.push({ text: JSON.stringify(contentPart) });
|
|
191
|
-
break;
|
|
192
|
-
}
|
|
320
|
+
if (supportsFunctionResponseParts) {
|
|
321
|
+
appendToolResultParts(parts, part.toolName, output.value);
|
|
322
|
+
} else {
|
|
323
|
+
appendLegacyToolResultParts(parts, part.toolName, output.value);
|
|
193
324
|
}
|
|
194
325
|
} else {
|
|
195
326
|
parts.push({
|
|
@@ -135,10 +135,15 @@ export class GoogleGenerativeAILanguageModel implements LanguageModelV3 {
|
|
|
135
135
|
}
|
|
136
136
|
|
|
137
137
|
const isGemmaModel = this.modelId.toLowerCase().startsWith('gemma-');
|
|
138
|
+
const supportsFunctionResponseParts = this.modelId.startsWith('gemini-3');
|
|
138
139
|
|
|
139
140
|
const { contents, systemInstruction } = convertToGoogleGenerativeAIMessages(
|
|
140
141
|
prompt,
|
|
141
|
-
{
|
|
142
|
+
{
|
|
143
|
+
isGemmaModel,
|
|
144
|
+
providerOptionsName,
|
|
145
|
+
supportsFunctionResponseParts,
|
|
146
|
+
},
|
|
142
147
|
);
|
|
143
148
|
|
|
144
149
|
const {
|
|
@@ -2,9 +2,9 @@ import {
|
|
|
2
2
|
GroundingMetadataSchema,
|
|
3
3
|
PromptFeedbackSchema,
|
|
4
4
|
UrlContextMetadataSchema,
|
|
5
|
+
type SafetyRatingSchema,
|
|
5
6
|
UsageMetadataSchema,
|
|
6
7
|
} from './google-generative-ai-language-model';
|
|
7
|
-
import { type SafetyRatingSchema } from './google-generative-ai-language-model';
|
|
8
8
|
|
|
9
9
|
export type GoogleGenerativeAIPrompt = {
|
|
10
10
|
systemInstruction?: GoogleGenerativeAISystemInstruction;
|
|
@@ -24,9 +24,19 @@ export type GoogleGenerativeAIContentPart =
|
|
|
24
24
|
| { text: string; thought?: boolean; thoughtSignature?: string }
|
|
25
25
|
| { inlineData: { mimeType: string; data: string } }
|
|
26
26
|
| { functionCall: { name: string; args: unknown }; thoughtSignature?: string }
|
|
27
|
-
| {
|
|
27
|
+
| {
|
|
28
|
+
functionResponse: {
|
|
29
|
+
name: string;
|
|
30
|
+
response: unknown;
|
|
31
|
+
parts?: Array<GoogleGenerativeAIFunctionResponsePart>;
|
|
32
|
+
};
|
|
33
|
+
}
|
|
28
34
|
| { fileData: { mimeType: string; fileUri: string } };
|
|
29
35
|
|
|
36
|
+
export type GoogleGenerativeAIFunctionResponsePart = {
|
|
37
|
+
inlineData: { mimeType: string; data: string };
|
|
38
|
+
};
|
|
39
|
+
|
|
30
40
|
export type GoogleGenerativeAIGroundingMetadata = GroundingMetadataSchema;
|
|
31
41
|
|
|
32
42
|
export type GoogleGenerativeAIUrlContextMetadata = UrlContextMetadataSchema;
|