@f5-sales-demo/pi-ai 19.51.2
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 +1997 -0
- package/README.md +1160 -0
- package/package.json +135 -0
- package/src/api-registry.ts +95 -0
- package/src/auth-storage.ts +2694 -0
- package/src/cli.ts +493 -0
- package/src/index.ts +42 -0
- package/src/model-cache.ts +97 -0
- package/src/model-manager.ts +349 -0
- package/src/model-thinking.ts +561 -0
- package/src/models.json +49439 -0
- package/src/models.json.d.ts +9 -0
- package/src/models.ts +56 -0
- package/src/prompts/turn-aborted-guidance.md +4 -0
- package/src/provider-details.ts +81 -0
- package/src/provider-models/descriptors.ts +285 -0
- package/src/provider-models/google.ts +90 -0
- package/src/provider-models/index.ts +4 -0
- package/src/provider-models/openai-compat.ts +2074 -0
- package/src/provider-models/special.ts +106 -0
- package/src/providers/amazon-bedrock.ts +706 -0
- package/src/providers/anthropic.ts +1682 -0
- package/src/providers/azure-openai-responses.ts +391 -0
- package/src/providers/cursor/gen/agent_pb.ts +15274 -0
- package/src/providers/cursor/proto/agent.proto +3526 -0
- package/src/providers/cursor/proto/buf.gen.yaml +6 -0
- package/src/providers/cursor/proto/buf.yaml +17 -0
- package/src/providers/cursor.ts +2218 -0
- package/src/providers/github-copilot-headers.ts +140 -0
- package/src/providers/gitlab-duo.ts +381 -0
- package/src/providers/google-gemini-cli.ts +1133 -0
- package/src/providers/google-shared.ts +354 -0
- package/src/providers/google-vertex.ts +436 -0
- package/src/providers/google.ts +381 -0
- package/src/providers/kimi.ts +151 -0
- package/src/providers/openai-codex/constants.ts +43 -0
- package/src/providers/openai-codex/request-transformer.ts +158 -0
- package/src/providers/openai-codex/response-handler.ts +81 -0
- package/src/providers/openai-codex-responses.ts +2345 -0
- package/src/providers/openai-completions-compat.ts +159 -0
- package/src/providers/openai-completions.ts +1290 -0
- package/src/providers/openai-responses-shared.ts +452 -0
- package/src/providers/openai-responses.ts +519 -0
- package/src/providers/register-builtins.ts +329 -0
- package/src/providers/synthetic.ts +154 -0
- package/src/providers/transform-messages.ts +234 -0
- package/src/rate-limit-utils.ts +84 -0
- package/src/stream.ts +728 -0
- package/src/types.ts +546 -0
- package/src/usage/claude.ts +337 -0
- package/src/usage/gemini.ts +248 -0
- package/src/usage/github-copilot.ts +421 -0
- package/src/usage/google-antigravity.ts +200 -0
- package/src/usage/kimi.ts +286 -0
- package/src/usage/minimax-code.ts +31 -0
- package/src/usage/openai-codex.ts +387 -0
- package/src/usage/zai.ts +247 -0
- package/src/usage.ts +130 -0
- package/src/utils/abort.ts +36 -0
- package/src/utils/anthropic-auth.ts +293 -0
- package/src/utils/discovery/antigravity.ts +261 -0
- package/src/utils/discovery/codex.ts +371 -0
- package/src/utils/discovery/cursor.ts +306 -0
- package/src/utils/discovery/gemini.ts +248 -0
- package/src/utils/discovery/index.ts +5 -0
- package/src/utils/discovery/openai-compatible.ts +224 -0
- package/src/utils/event-stream.ts +209 -0
- package/src/utils/http-inspector.ts +165 -0
- package/src/utils/idle-iterator.ts +176 -0
- package/src/utils/json-parse.ts +28 -0
- package/src/utils/oauth/alibaba-coding-plan.ts +59 -0
- package/src/utils/oauth/anthropic.ts +134 -0
- package/src/utils/oauth/api-key-validation.ts +92 -0
- package/src/utils/oauth/callback-server.ts +276 -0
- package/src/utils/oauth/cerebras.ts +59 -0
- package/src/utils/oauth/cloudflare-ai-gateway.ts +48 -0
- package/src/utils/oauth/cursor.ts +157 -0
- package/src/utils/oauth/github-copilot.ts +358 -0
- package/src/utils/oauth/gitlab-duo.ts +123 -0
- package/src/utils/oauth/google-antigravity.ts +275 -0
- package/src/utils/oauth/google-gemini-cli.ts +334 -0
- package/src/utils/oauth/huggingface.ts +62 -0
- package/src/utils/oauth/index.ts +512 -0
- package/src/utils/oauth/kagi.ts +47 -0
- package/src/utils/oauth/kilo.ts +87 -0
- package/src/utils/oauth/kimi.ts +251 -0
- package/src/utils/oauth/litellm.ts +81 -0
- package/src/utils/oauth/lm-studio.ts +40 -0
- package/src/utils/oauth/minimax-code.ts +78 -0
- package/src/utils/oauth/moonshot.ts +59 -0
- package/src/utils/oauth/nanogpt.ts +51 -0
- package/src/utils/oauth/nvidia.ts +70 -0
- package/src/utils/oauth/oauth.html +199 -0
- package/src/utils/oauth/ollama.ts +47 -0
- package/src/utils/oauth/openai-codex.ts +190 -0
- package/src/utils/oauth/opencode.ts +49 -0
- package/src/utils/oauth/parallel.ts +46 -0
- package/src/utils/oauth/perplexity.ts +200 -0
- package/src/utils/oauth/pkce.ts +18 -0
- package/src/utils/oauth/qianfan.ts +58 -0
- package/src/utils/oauth/qwen-portal.ts +60 -0
- package/src/utils/oauth/synthetic.ts +60 -0
- package/src/utils/oauth/tavily.ts +46 -0
- package/src/utils/oauth/together.ts +59 -0
- package/src/utils/oauth/types.ts +89 -0
- package/src/utils/oauth/venice.ts +59 -0
- package/src/utils/oauth/vercel-ai-gateway.ts +47 -0
- package/src/utils/oauth/vllm.ts +40 -0
- package/src/utils/oauth/xiaomi.ts +88 -0
- package/src/utils/oauth/zai.ts +60 -0
- package/src/utils/oauth/zenmux.ts +51 -0
- package/src/utils/overflow.ts +134 -0
- package/src/utils/retry-after.ts +110 -0
- package/src/utils/retry.ts +93 -0
- package/src/utils/schema/CONSTRAINTS.md +160 -0
- package/src/utils/schema/adapt.ts +20 -0
- package/src/utils/schema/compatibility.ts +397 -0
- package/src/utils/schema/dereference.ts +93 -0
- package/src/utils/schema/equality.ts +93 -0
- package/src/utils/schema/fields.ts +147 -0
- package/src/utils/schema/index.ts +9 -0
- package/src/utils/schema/normalize-cca.ts +479 -0
- package/src/utils/schema/sanitize-google.ts +212 -0
- package/src/utils/schema/strict-mode.ts +385 -0
- package/src/utils/schema/types.ts +5 -0
- package/src/utils/tool-choice.ts +81 -0
- package/src/utils/validation.ts +664 -0
- package/src/utils.ts +147 -0
|
@@ -0,0 +1,354 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared utilities for Google Generative AI and Google Cloud Code Assist providers.
|
|
3
|
+
*/
|
|
4
|
+
import { type Content, FinishReason, FunctionCallingConfigMode, type Part } from "@google/genai";
|
|
5
|
+
import type { Context, ImageContent, Model, StopReason, TextContent, Tool } from "../types";
|
|
6
|
+
import { prepareSchemaForCCA, sanitizeSchemaForGoogle } from "../utils/schema";
|
|
7
|
+
import { transformMessages } from "./transform-messages";
|
|
8
|
+
|
|
9
|
+
export { sanitizeSchemaForGoogle };
|
|
10
|
+
|
|
11
|
+
type GoogleApiType = "google-generative-ai" | "google-gemini-cli" | "google-vertex";
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Determines whether a streamed Gemini `Part` should be treated as "thinking".
|
|
15
|
+
*
|
|
16
|
+
* Protocol note (Gemini / Vertex AI thought signatures):
|
|
17
|
+
* - `thought: true` is the definitive marker for thinking content (thought summaries).
|
|
18
|
+
* - `thoughtSignature` is an encrypted representation of the model's internal thought process
|
|
19
|
+
* used to preserve reasoning context across multi-turn interactions.
|
|
20
|
+
* - `thoughtSignature` can appear on ANY part type (text, functionCall, etc.) - it does NOT
|
|
21
|
+
* indicate the part itself is thinking content.
|
|
22
|
+
* - For non-functionCall responses, the signature appears on the last part for context replay.
|
|
23
|
+
* - When persisting/replaying model outputs, signature-bearing parts must be preserved as-is;
|
|
24
|
+
* do not merge/move signatures across parts.
|
|
25
|
+
*
|
|
26
|
+
* See: https://ai.google.dev/gemini-api/docs/thought-signatures
|
|
27
|
+
*/
|
|
28
|
+
export function isThinkingPart(part: Pick<Part, "thought" | "thoughtSignature">): boolean {
|
|
29
|
+
return part.thought === true;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Retain thought signatures during streaming.
|
|
34
|
+
*
|
|
35
|
+
* Some backends only send `thoughtSignature` on the first delta for a given part/block; later deltas may omit it.
|
|
36
|
+
* This helper preserves the last non-empty signature for the current block.
|
|
37
|
+
*
|
|
38
|
+
* Note: this does NOT merge or move signatures across distinct response parts. It only prevents
|
|
39
|
+
* a signature from being overwritten with `undefined` within the same streamed block.
|
|
40
|
+
*/
|
|
41
|
+
export function retainThoughtSignature(existing: string | undefined, incoming: string | undefined): string | undefined {
|
|
42
|
+
if (typeof incoming === "string" && incoming.length > 0) return incoming;
|
|
43
|
+
return existing;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// Thought signatures must be base64 for Google APIs (TYPE_BYTES).
|
|
47
|
+
const base64SignaturePattern = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
48
|
+
|
|
49
|
+
const SKIP_THOUGHT_SIGNATURE = "skip_thought_signature_validator";
|
|
50
|
+
|
|
51
|
+
function isValidThoughtSignature(signature: string | undefined): boolean {
|
|
52
|
+
if (!signature) return false;
|
|
53
|
+
if (signature.length % 4 !== 0) return false;
|
|
54
|
+
return base64SignaturePattern.test(signature);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Only keep signatures from the same provider/model and with valid base64.
|
|
59
|
+
*/
|
|
60
|
+
function resolveThoughtSignature(isSameProviderAndModel: boolean, signature: string | undefined): string | undefined {
|
|
61
|
+
return isSameProviderAndModel && isValidThoughtSignature(signature) ? signature : undefined;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/**
|
|
65
|
+
* Claude models via Google APIs require explicit tool call IDs in function calls/responses.
|
|
66
|
+
*/
|
|
67
|
+
export function requiresToolCallId(modelId: string): boolean {
|
|
68
|
+
return modelId.startsWith("claude-");
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
function getGeminiMajorVersion(modelId: string): number | undefined {
|
|
72
|
+
const match = modelId.toLowerCase().match(/^gemini(?:-live)?-(\d+)/);
|
|
73
|
+
if (!match) return undefined;
|
|
74
|
+
return Number.parseInt(match[1], 10);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
function supportsMultimodalFunctionResponse(modelId: string): boolean {
|
|
78
|
+
const geminiMajorVersion = getGeminiMajorVersion(modelId);
|
|
79
|
+
if (geminiMajorVersion !== undefined) {
|
|
80
|
+
return geminiMajorVersion >= 3;
|
|
81
|
+
}
|
|
82
|
+
return true;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
function isGemini3Model(modelId: string): boolean {
|
|
86
|
+
return modelId.includes("gemini-3");
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Convert internal messages to Gemini Content[] format.
|
|
91
|
+
*/
|
|
92
|
+
export function convertMessages<T extends GoogleApiType>(model: Model<T>, context: Context): Content[] {
|
|
93
|
+
const contents: Content[] = [];
|
|
94
|
+
const normalizeToolCallId = (id: string): string => {
|
|
95
|
+
if (!requiresToolCallId(model.id)) return id;
|
|
96
|
+
return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
|
|
97
|
+
};
|
|
98
|
+
|
|
99
|
+
const transformedMessages = transformMessages(context.messages, model, normalizeToolCallId);
|
|
100
|
+
|
|
101
|
+
for (const msg of transformedMessages) {
|
|
102
|
+
if (msg.role === "user" || msg.role === "developer") {
|
|
103
|
+
if (typeof msg.content === "string") {
|
|
104
|
+
// Skip empty user messages
|
|
105
|
+
if (!msg.content || msg.content.trim() === "") continue;
|
|
106
|
+
contents.push({
|
|
107
|
+
role: "user",
|
|
108
|
+
parts: [{ text: msg.content.toWellFormed() }],
|
|
109
|
+
});
|
|
110
|
+
} else {
|
|
111
|
+
const parts: Part[] = msg.content.map(item => {
|
|
112
|
+
if (item.type === "text") {
|
|
113
|
+
return { text: item.text.toWellFormed() };
|
|
114
|
+
} else {
|
|
115
|
+
return {
|
|
116
|
+
inlineData: {
|
|
117
|
+
mimeType: item.mimeType,
|
|
118
|
+
data: item.data,
|
|
119
|
+
},
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
// Filter out images if model doesn't support them, and empty text blocks
|
|
124
|
+
let filteredParts = !model.input.includes("image") ? parts.filter(p => p.text !== undefined) : parts;
|
|
125
|
+
filteredParts = filteredParts.filter(p => {
|
|
126
|
+
if (p.text !== undefined) {
|
|
127
|
+
return p.text.trim().length > 0;
|
|
128
|
+
}
|
|
129
|
+
return true; // Keep non-text parts (images)
|
|
130
|
+
});
|
|
131
|
+
if (filteredParts.length === 0) continue;
|
|
132
|
+
contents.push({
|
|
133
|
+
role: "user",
|
|
134
|
+
parts: filteredParts,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
} else if (msg.role === "assistant") {
|
|
138
|
+
const parts: Part[] = [];
|
|
139
|
+
// Check if message is from same provider and model - only then keep thinking blocks
|
|
140
|
+
const isSameProviderAndModel = msg.provider === model.provider && msg.model === model.id;
|
|
141
|
+
|
|
142
|
+
for (const block of msg.content) {
|
|
143
|
+
if (block.type === "text") {
|
|
144
|
+
// Skip empty text blocks - they can cause issues with some models (e.g. Claude via Antigravity)
|
|
145
|
+
if (!block.text || block.text.trim() === "") continue;
|
|
146
|
+
const thoughtSignature = resolveThoughtSignature(isSameProviderAndModel, block.textSignature);
|
|
147
|
+
parts.push({
|
|
148
|
+
text: block.text.toWellFormed(),
|
|
149
|
+
...(thoughtSignature && { thoughtSignature }),
|
|
150
|
+
});
|
|
151
|
+
} else if (block.type === "thinking") {
|
|
152
|
+
// Skip empty thinking blocks
|
|
153
|
+
if (!block.thinking || block.thinking.trim() === "") continue;
|
|
154
|
+
// Only keep as thinking block if same provider AND same model
|
|
155
|
+
// Otherwise convert to plain text (no tags to avoid model mimicking them)
|
|
156
|
+
if (isSameProviderAndModel) {
|
|
157
|
+
const thoughtSignature = resolveThoughtSignature(isSameProviderAndModel, block.thinkingSignature);
|
|
158
|
+
parts.push({
|
|
159
|
+
thought: true,
|
|
160
|
+
text: block.thinking.toWellFormed(),
|
|
161
|
+
...(thoughtSignature && { thoughtSignature }),
|
|
162
|
+
});
|
|
163
|
+
} else {
|
|
164
|
+
parts.push({
|
|
165
|
+
text: block.thinking.toWellFormed(),
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
} else if (block.type === "toolCall") {
|
|
169
|
+
const thoughtSignature = resolveThoughtSignature(isSameProviderAndModel, block.thoughtSignature);
|
|
170
|
+
const effectiveSignature =
|
|
171
|
+
thoughtSignature || (isGemini3Model(model.id) ? SKIP_THOUGHT_SIGNATURE : undefined);
|
|
172
|
+
|
|
173
|
+
const part: Part = {
|
|
174
|
+
functionCall: {
|
|
175
|
+
name: block.name,
|
|
176
|
+
args: block.arguments ?? {},
|
|
177
|
+
...(requiresToolCallId(model.id) ? { id: block.id } : {}),
|
|
178
|
+
},
|
|
179
|
+
};
|
|
180
|
+
if (model.provider === "google-vertex" && part?.functionCall?.id) {
|
|
181
|
+
delete part.functionCall.id; // Vertex AI does not support 'id' in functionCall
|
|
182
|
+
}
|
|
183
|
+
if (effectiveSignature) {
|
|
184
|
+
part.thoughtSignature = effectiveSignature;
|
|
185
|
+
}
|
|
186
|
+
parts.push(part);
|
|
187
|
+
}
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
if (parts.length === 0) continue;
|
|
191
|
+
contents.push({
|
|
192
|
+
role: "model",
|
|
193
|
+
parts,
|
|
194
|
+
});
|
|
195
|
+
} else if (msg.role === "toolResult") {
|
|
196
|
+
// Extract text and image content
|
|
197
|
+
const textContent = msg.content.filter((c): c is TextContent => c.type === "text");
|
|
198
|
+
const textResult = textContent.map(c => c.text).join("\n");
|
|
199
|
+
const imageContent = model.input.includes("image")
|
|
200
|
+
? msg.content.filter((c): c is ImageContent => c.type === "image")
|
|
201
|
+
: [];
|
|
202
|
+
|
|
203
|
+
const hasText = textResult.length > 0;
|
|
204
|
+
const hasImages = imageContent.length > 0;
|
|
205
|
+
|
|
206
|
+
// Gemini 3+ models support multimodal function responses with images nested inside
|
|
207
|
+
// functionResponse.parts. Claude and other non-Gemini models behind Cloud Code Assist /
|
|
208
|
+
// Antigravity also accept this shape. Gemini < 3 still needs a separate user image turn.
|
|
209
|
+
const modelSupportsMultimodalFunctionResponse = supportsMultimodalFunctionResponse(model.id);
|
|
210
|
+
|
|
211
|
+
// Use "output" key for success, "error" key for errors as per SDK documentation
|
|
212
|
+
const responseValue = hasText ? textResult.toWellFormed() : hasImages ? "(see attached image)" : "";
|
|
213
|
+
|
|
214
|
+
const imageParts: Part[] = imageContent.map(imageBlock => ({
|
|
215
|
+
inlineData: {
|
|
216
|
+
mimeType: imageBlock.mimeType,
|
|
217
|
+
data: imageBlock.data,
|
|
218
|
+
},
|
|
219
|
+
}));
|
|
220
|
+
|
|
221
|
+
const includeId = requiresToolCallId(model.id);
|
|
222
|
+
const functionResponsePart: Part = {
|
|
223
|
+
functionResponse: {
|
|
224
|
+
name: msg.toolName,
|
|
225
|
+
response: msg.isError ? { error: responseValue } : { output: responseValue },
|
|
226
|
+
...(hasImages && modelSupportsMultimodalFunctionResponse && { parts: imageParts }),
|
|
227
|
+
...(includeId ? { id: msg.toolCallId } : {}),
|
|
228
|
+
},
|
|
229
|
+
};
|
|
230
|
+
|
|
231
|
+
if (model.provider === "google-vertex" && functionResponsePart.functionResponse?.id) {
|
|
232
|
+
delete functionResponsePart.functionResponse.id; // Vertex AI does not support 'id' in functionResponse
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
// Cloud Code Assist API requires all function responses to be in a single user turn.
|
|
236
|
+
// Check if the last content is already a user turn with function responses and merge.
|
|
237
|
+
const lastContent = contents[contents.length - 1];
|
|
238
|
+
if (lastContent?.role === "user" && lastContent.parts?.some(p => p.functionResponse)) {
|
|
239
|
+
lastContent.parts.push(functionResponsePart);
|
|
240
|
+
} else {
|
|
241
|
+
contents.push({
|
|
242
|
+
role: "user",
|
|
243
|
+
parts: [functionResponsePart],
|
|
244
|
+
});
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
// For Gemini < 3, add images in a separate user message
|
|
248
|
+
if (hasImages && !modelSupportsMultimodalFunctionResponse) {
|
|
249
|
+
contents.push({
|
|
250
|
+
role: "user",
|
|
251
|
+
parts: [{ text: "Tool result image:" }, ...imageParts],
|
|
252
|
+
});
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
}
|
|
256
|
+
|
|
257
|
+
return contents;
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Convert tools to Gemini function declarations format.
|
|
262
|
+
*
|
|
263
|
+
* We prefer `parametersJsonSchema` (full JSON Schema: anyOf/oneOf/const/etc.).
|
|
264
|
+
*
|
|
265
|
+
* Claude models via Cloud Code Assist require the legacy `parameters` field; the API
|
|
266
|
+
* translates it into Anthropic's `input_schema`. When using that path, we sanitize the
|
|
267
|
+
* schema to remove Google-unsupported JSON Schema keywords.
|
|
268
|
+
*/
|
|
269
|
+
export function convertTools(
|
|
270
|
+
tools: Tool[],
|
|
271
|
+
model: Model<"google-generative-ai" | "google-gemini-cli" | "google-vertex">,
|
|
272
|
+
): { functionDeclarations: Record<string, unknown>[] }[] | undefined {
|
|
273
|
+
if (tools.length === 0) return undefined;
|
|
274
|
+
|
|
275
|
+
/**
|
|
276
|
+
* Claude models on Cloud Code Assist need the legacy `parameters` field;
|
|
277
|
+
* the API translates it into Anthropic's `input_schema`.
|
|
278
|
+
*/
|
|
279
|
+
const useParameters = model.id.startsWith("claude-");
|
|
280
|
+
|
|
281
|
+
return [
|
|
282
|
+
{
|
|
283
|
+
functionDeclarations: tools.map(tool => ({
|
|
284
|
+
name: tool.name,
|
|
285
|
+
description: tool.description || "",
|
|
286
|
+
...(useParameters
|
|
287
|
+
? { parameters: prepareSchemaForCCA(tool.parameters) }
|
|
288
|
+
: { parametersJsonSchema: tool.parameters }),
|
|
289
|
+
})),
|
|
290
|
+
},
|
|
291
|
+
];
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
/**
|
|
295
|
+
* Map tool choice string to Gemini FunctionCallingConfigMode.
|
|
296
|
+
*/
|
|
297
|
+
export function mapToolChoice(choice: string): FunctionCallingConfigMode {
|
|
298
|
+
switch (choice) {
|
|
299
|
+
case "auto":
|
|
300
|
+
return FunctionCallingConfigMode.AUTO;
|
|
301
|
+
case "none":
|
|
302
|
+
return FunctionCallingConfigMode.NONE;
|
|
303
|
+
case "any":
|
|
304
|
+
return FunctionCallingConfigMode.ANY;
|
|
305
|
+
default:
|
|
306
|
+
return FunctionCallingConfigMode.AUTO;
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
|
|
310
|
+
/**
|
|
311
|
+
* Map Gemini FinishReason to our StopReason.
|
|
312
|
+
*/
|
|
313
|
+
export function mapStopReason(reason: FinishReason): StopReason {
|
|
314
|
+
switch (reason) {
|
|
315
|
+
case FinishReason.STOP:
|
|
316
|
+
return "stop";
|
|
317
|
+
case FinishReason.MAX_TOKENS:
|
|
318
|
+
return "length";
|
|
319
|
+
case FinishReason.BLOCKLIST:
|
|
320
|
+
case FinishReason.PROHIBITED_CONTENT:
|
|
321
|
+
case FinishReason.SPII:
|
|
322
|
+
case FinishReason.SAFETY:
|
|
323
|
+
case FinishReason.IMAGE_SAFETY:
|
|
324
|
+
case FinishReason.IMAGE_PROHIBITED_CONTENT:
|
|
325
|
+
case FinishReason.IMAGE_RECITATION:
|
|
326
|
+
case FinishReason.IMAGE_OTHER:
|
|
327
|
+
case FinishReason.RECITATION:
|
|
328
|
+
case FinishReason.FINISH_REASON_UNSPECIFIED:
|
|
329
|
+
case FinishReason.OTHER:
|
|
330
|
+
case FinishReason.LANGUAGE:
|
|
331
|
+
case FinishReason.MALFORMED_FUNCTION_CALL:
|
|
332
|
+
case FinishReason.UNEXPECTED_TOOL_CALL:
|
|
333
|
+
case FinishReason.NO_IMAGE:
|
|
334
|
+
return "error";
|
|
335
|
+
default: {
|
|
336
|
+
const _exhaustive: never = reason;
|
|
337
|
+
throw new Error(`Unhandled stop reason: ${_exhaustive}`);
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/**
|
|
343
|
+
* Map string finish reason to our StopReason (for raw API responses).
|
|
344
|
+
*/
|
|
345
|
+
export function mapStopReasonString(reason: string): StopReason {
|
|
346
|
+
switch (reason) {
|
|
347
|
+
case "STOP":
|
|
348
|
+
return "stop";
|
|
349
|
+
case "MAX_TOKENS":
|
|
350
|
+
return "length";
|
|
351
|
+
default:
|
|
352
|
+
return "error";
|
|
353
|
+
}
|
|
354
|
+
}
|