@gajae-code/ai 0.1.1
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 +2644 -0
- package/README.md +1181 -0
- package/dist/types/api-registry.d.ts +30 -0
- package/dist/types/auth-broker/client.d.ts +66 -0
- package/dist/types/auth-broker/index.d.ts +5 -0
- package/dist/types/auth-broker/refresher.d.ts +25 -0
- package/dist/types/auth-broker/remote-store.d.ts +96 -0
- package/dist/types/auth-broker/server.d.ts +32 -0
- package/dist/types/auth-broker/types.d.ts +105 -0
- package/dist/types/auth-broker/wire-schemas.d.ts +412 -0
- package/dist/types/auth-gateway/http.d.ts +39 -0
- package/dist/types/auth-gateway/index.d.ts +3 -0
- package/dist/types/auth-gateway/server.d.ts +17 -0
- package/dist/types/auth-gateway/types.d.ts +115 -0
- package/dist/types/auth-storage.d.ts +641 -0
- package/dist/types/cli.d.ts +2 -0
- package/dist/types/index.d.ts +49 -0
- package/dist/types/model-cache.d.ts +17 -0
- package/dist/types/model-manager.d.ts +62 -0
- package/dist/types/model-thinking.d.ts +71 -0
- package/dist/types/models.d.ts +12 -0
- package/dist/types/provider-details.d.ts +24 -0
- package/dist/types/provider-models/bundled-references.d.ts +4 -0
- package/dist/types/provider-models/descriptors.d.ts +48 -0
- package/dist/types/provider-models/google.d.ts +20 -0
- package/dist/types/provider-models/index.d.ts +5 -0
- package/dist/types/provider-models/ollama.d.ts +7 -0
- package/dist/types/provider-models/openai-compat.d.ts +237 -0
- package/dist/types/provider-models/special.d.ts +16 -0
- package/dist/types/providers/amazon-bedrock.d.ts +36 -0
- package/dist/types/providers/anthropic-messages-server-schema.d.ts +450 -0
- package/dist/types/providers/anthropic-messages-server.d.ts +17 -0
- package/dist/types/providers/anthropic.d.ts +188 -0
- package/dist/types/providers/aws-credentials.d.ts +43 -0
- package/dist/types/providers/aws-eventstream.d.ts +38 -0
- package/dist/types/providers/aws-sigv4.d.ts +55 -0
- package/dist/types/providers/azure-openai-responses.d.ts +15 -0
- package/dist/types/providers/cursor/gen/agent_pb.d.ts +13022 -0
- package/dist/types/providers/cursor.d.ts +42 -0
- package/dist/types/providers/error-message.d.ts +27 -0
- package/dist/types/providers/github-copilot-headers.d.ts +40 -0
- package/dist/types/providers/gitlab-duo.d.ts +27 -0
- package/dist/types/providers/google-auth.d.ts +24 -0
- package/dist/types/providers/google-gemini-cli.d.ts +72 -0
- package/dist/types/providers/google-gemini-headers.d.ts +18 -0
- package/dist/types/providers/google-shared.d.ts +163 -0
- package/dist/types/providers/google-types.d.ts +138 -0
- package/dist/types/providers/google-vertex.d.ts +7 -0
- package/dist/types/providers/google.d.ts +4 -0
- package/dist/types/providers/grammar.d.ts +1 -0
- package/dist/types/providers/kimi.d.ts +27 -0
- package/dist/types/providers/mock.d.ts +175 -0
- package/dist/types/providers/ollama.d.ts +6 -0
- package/dist/types/providers/openai-anthropic-shim.d.ts +31 -0
- package/dist/types/providers/openai-chat-server-schema.d.ts +814 -0
- package/dist/types/providers/openai-chat-server.d.ts +16 -0
- package/dist/types/providers/openai-codex/constants.d.ts +26 -0
- package/dist/types/providers/openai-codex/request-transformer.d.ts +49 -0
- package/dist/types/providers/openai-codex/response-handler.d.ts +17 -0
- package/dist/types/providers/openai-codex-responses.d.ts +67 -0
- package/dist/types/providers/openai-completions-compat.d.ts +25 -0
- package/dist/types/providers/openai-completions.d.ts +33 -0
- package/dist/types/providers/openai-responses-server-schema.d.ts +392 -0
- package/dist/types/providers/openai-responses-server.d.ts +17 -0
- package/dist/types/providers/openai-responses-shared.d.ts +89 -0
- package/dist/types/providers/openai-responses.d.ts +32 -0
- package/dist/types/providers/pi-native-client.d.ts +13 -0
- package/dist/types/providers/pi-native-server.d.ts +68 -0
- package/dist/types/providers/register-builtins.d.ts +31 -0
- package/dist/types/providers/synthetic.d.ts +26 -0
- package/dist/types/providers/transform-messages.d.ts +12 -0
- package/dist/types/providers/vision-guard.d.ts +8 -0
- package/dist/types/rate-limit-utils.d.ts +19 -0
- package/dist/types/stream.d.ts +24 -0
- package/dist/types/types.d.ts +746 -0
- package/dist/types/usage/claude.d.ts +3 -0
- package/dist/types/usage/gemini.d.ts +2 -0
- package/dist/types/usage/github-copilot.d.ts +7 -0
- package/dist/types/usage/google-antigravity.d.ts +2 -0
- package/dist/types/usage/kimi.d.ts +2 -0
- package/dist/types/usage/minimax-code.d.ts +2 -0
- package/dist/types/usage/openai-codex.d.ts +3 -0
- package/dist/types/usage/shared.d.ts +1 -0
- package/dist/types/usage/zai.d.ts +2 -0
- package/dist/types/usage.d.ts +258 -0
- package/dist/types/utils/abort.d.ts +19 -0
- package/dist/types/utils/anthropic-auth.d.ts +31 -0
- package/dist/types/utils/discovery/antigravity.d.ts +61 -0
- package/dist/types/utils/discovery/codex.d.ts +38 -0
- package/dist/types/utils/discovery/cursor.d.ts +23 -0
- package/dist/types/utils/discovery/gemini.d.ts +25 -0
- package/dist/types/utils/discovery/index.d.ts +4 -0
- package/dist/types/utils/discovery/openai-compatible.d.ts +72 -0
- package/dist/types/utils/event-stream.d.ts +28 -0
- package/dist/types/utils/fireworks-model-id.d.ts +10 -0
- package/dist/types/utils/foundry.d.ts +1 -0
- package/dist/types/utils/h2-fetch.d.ts +22 -0
- package/dist/types/utils/http-inspector.d.ts +31 -0
- package/dist/types/utils/idle-iterator.d.ts +67 -0
- package/dist/types/utils/json-parse.d.ts +10 -0
- package/dist/types/utils/oauth/alibaba-coding-plan.d.ts +18 -0
- package/dist/types/utils/oauth/anthropic.d.ts +22 -0
- package/dist/types/utils/oauth/api-key-login.d.ts +35 -0
- package/dist/types/utils/oauth/api-key-validation.d.ts +27 -0
- package/dist/types/utils/oauth/callback-server.d.ts +57 -0
- package/dist/types/utils/oauth/cerebras.d.ts +1 -0
- package/dist/types/utils/oauth/cloudflare-ai-gateway.d.ts +18 -0
- package/dist/types/utils/oauth/cursor.d.ts +15 -0
- package/dist/types/utils/oauth/deepseek.d.ts +10 -0
- package/dist/types/utils/oauth/firepass.d.ts +1 -0
- package/dist/types/utils/oauth/fireworks.d.ts +1 -0
- package/dist/types/utils/oauth/github-copilot.d.ts +38 -0
- package/dist/types/utils/oauth/gitlab-duo.d.ts +3 -0
- package/dist/types/utils/oauth/google-antigravity.d.ts +11 -0
- package/dist/types/utils/oauth/google-gemini-cli.d.ts +10 -0
- package/dist/types/utils/oauth/google-oauth-shared.d.ts +28 -0
- package/dist/types/utils/oauth/huggingface.d.ts +19 -0
- package/dist/types/utils/oauth/index.d.ts +38 -0
- package/dist/types/utils/oauth/kagi.d.ts +17 -0
- package/dist/types/utils/oauth/kilo.d.ts +5 -0
- package/dist/types/utils/oauth/kimi.d.ts +21 -0
- package/dist/types/utils/oauth/litellm.d.ts +18 -0
- package/dist/types/utils/oauth/lm-studio.d.ts +17 -0
- package/dist/types/utils/oauth/minimax-code.d.ts +28 -0
- package/dist/types/utils/oauth/moonshot.d.ts +1 -0
- package/dist/types/utils/oauth/nanogpt.d.ts +1 -0
- package/dist/types/utils/oauth/nvidia.d.ts +18 -0
- package/dist/types/utils/oauth/ollama-cloud.d.ts +2 -0
- package/dist/types/utils/oauth/ollama.d.ts +18 -0
- package/dist/types/utils/oauth/openai-codex.d.ts +21 -0
- package/dist/types/utils/oauth/opencode.d.ts +18 -0
- package/dist/types/utils/oauth/parallel.d.ts +17 -0
- package/dist/types/utils/oauth/perplexity.d.ts +9 -0
- package/dist/types/utils/oauth/pkce.d.ts +8 -0
- package/dist/types/utils/oauth/qianfan.d.ts +17 -0
- package/dist/types/utils/oauth/qwen-portal.d.ts +19 -0
- package/dist/types/utils/oauth/synthetic.d.ts +1 -0
- package/dist/types/utils/oauth/tavily.d.ts +17 -0
- package/dist/types/utils/oauth/together.d.ts +1 -0
- package/dist/types/utils/oauth/types.d.ts +44 -0
- package/dist/types/utils/oauth/venice.d.ts +18 -0
- package/dist/types/utils/oauth/vercel-ai-gateway.d.ts +18 -0
- package/dist/types/utils/oauth/vllm.d.ts +16 -0
- package/dist/types/utils/oauth/xiaomi.d.ts +19 -0
- package/dist/types/utils/oauth/zai.d.ts +18 -0
- package/dist/types/utils/oauth/zenmux.d.ts +1 -0
- package/dist/types/utils/overflow.d.ts +54 -0
- package/dist/types/utils/parse-bind.d.ts +23 -0
- package/dist/types/utils/provider-response.d.ts +3 -0
- package/dist/types/utils/retry-after.d.ts +3 -0
- package/dist/types/utils/retry.d.ts +26 -0
- package/dist/types/utils/schema/adapt.d.ts +24 -0
- package/dist/types/utils/schema/compatibility.d.ts +30 -0
- package/dist/types/utils/schema/dereference.d.ts +11 -0
- package/dist/types/utils/schema/draft.d.ts +10 -0
- package/dist/types/utils/schema/equality.d.ts +4 -0
- package/dist/types/utils/schema/fields.d.ts +49 -0
- package/dist/types/utils/schema/index.d.ts +13 -0
- package/dist/types/utils/schema/json-schema-validator.d.ts +12 -0
- package/dist/types/utils/schema/meta-validator.d.ts +2 -0
- package/dist/types/utils/schema/normalize.d.ts +93 -0
- package/dist/types/utils/schema/spill.d.ts +8 -0
- package/dist/types/utils/schema/stamps.d.ts +25 -0
- package/dist/types/utils/schema/types.d.ts +4 -0
- package/dist/types/utils/schema/wire.d.ts +54 -0
- package/dist/types/utils/schema/zod-decontaminate.d.ts +31 -0
- package/dist/types/utils/sse-debug.d.ts +10 -0
- package/dist/types/utils/tool-call-healing.d.ts +71 -0
- package/dist/types/utils/tool-choice.d.ts +50 -0
- package/dist/types/utils/validation.d.ts +17 -0
- package/dist/types/utils.d.ts +28 -0
- package/package.json +146 -0
- package/src/api-registry.ts +96 -0
- package/src/auth-broker/client.ts +358 -0
- package/src/auth-broker/index.ts +5 -0
- package/src/auth-broker/refresher.ts +127 -0
- package/src/auth-broker/remote-store.ts +623 -0
- package/src/auth-broker/server.ts +644 -0
- package/src/auth-broker/types.ts +127 -0
- package/src/auth-broker/wire-schemas.ts +200 -0
- package/src/auth-gateway/http.ts +194 -0
- package/src/auth-gateway/index.ts +3 -0
- package/src/auth-gateway/server.ts +717 -0
- package/src/auth-gateway/types.ts +134 -0
- package/src/auth-storage.ts +4104 -0
- package/src/cli.ts +262 -0
- package/src/index.ts +54 -0
- package/src/model-cache.ts +129 -0
- package/src/model-manager.ts +450 -0
- package/src/model-thinking.ts +691 -0
- package/src/models.json +73853 -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 +90 -0
- package/src/provider-models/bundled-references.ts +38 -0
- package/src/provider-models/descriptors.ts +308 -0
- package/src/provider-models/google.ts +91 -0
- package/src/provider-models/index.ts +5 -0
- package/src/provider-models/ollama.ts +153 -0
- package/src/provider-models/openai-compat.ts +2275 -0
- package/src/provider-models/special.ts +67 -0
- package/src/providers/amazon-bedrock.ts +849 -0
- package/src/providers/anthropic-messages-server-schema.ts +229 -0
- package/src/providers/anthropic-messages-server.ts +677 -0
- package/src/providers/anthropic.ts +2696 -0
- package/src/providers/aws-credentials.ts +501 -0
- package/src/providers/aws-eventstream.ts +185 -0
- package/src/providers/aws-sigv4.ts +218 -0
- package/src/providers/azure-openai-responses.ts +337 -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 +2561 -0
- package/src/providers/error-message.ts +21 -0
- package/src/providers/github-copilot-headers.ts +140 -0
- package/src/providers/gitlab-duo.ts +372 -0
- package/src/providers/google-auth.ts +252 -0
- package/src/providers/google-gemini-cli.ts +795 -0
- package/src/providers/google-gemini-headers.ts +41 -0
- package/src/providers/google-shared.ts +902 -0
- package/src/providers/google-types.ts +167 -0
- package/src/providers/google-vertex.ts +88 -0
- package/src/providers/google.ts +41 -0
- package/src/providers/grammar.ts +70 -0
- package/src/providers/kimi.ts +52 -0
- package/src/providers/mock.ts +500 -0
- package/src/providers/ollama.ts +544 -0
- package/src/providers/openai-anthropic-shim.ts +138 -0
- package/src/providers/openai-chat-server-schema.ts +243 -0
- package/src/providers/openai-chat-server.ts +628 -0
- package/src/providers/openai-codex/constants.ts +43 -0
- package/src/providers/openai-codex/request-transformer.ts +161 -0
- package/src/providers/openai-codex/response-handler.ts +81 -0
- package/src/providers/openai-codex-responses.ts +2598 -0
- package/src/providers/openai-completions-compat.ts +279 -0
- package/src/providers/openai-completions.ts +1853 -0
- package/src/providers/openai-responses-server-schema.ts +290 -0
- package/src/providers/openai-responses-server.ts +1183 -0
- package/src/providers/openai-responses-shared.ts +800 -0
- package/src/providers/openai-responses.ts +621 -0
- package/src/providers/pi-native-client.ts +228 -0
- package/src/providers/pi-native-server.ts +210 -0
- package/src/providers/register-builtins.ts +412 -0
- package/src/providers/synthetic.ts +50 -0
- package/src/providers/transform-messages.ts +309 -0
- package/src/providers/vision-guard.ts +31 -0
- package/src/rate-limit-utils.ts +84 -0
- package/src/stream.ts +895 -0
- package/src/types.ts +884 -0
- package/src/usage/claude.ts +431 -0
- package/src/usage/gemini.ts +250 -0
- package/src/usage/github-copilot.ts +421 -0
- package/src/usage/google-antigravity.ts +201 -0
- package/src/usage/kimi.ts +271 -0
- package/src/usage/minimax-code.ts +31 -0
- package/src/usage/openai-codex.ts +503 -0
- package/src/usage/shared.ts +10 -0
- package/src/usage/zai.ts +247 -0
- package/src/usage.ts +183 -0
- package/src/utils/abort.ts +51 -0
- package/src/utils/anthropic-auth.ts +87 -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 +4 -0
- package/src/utils/discovery/openai-compatible.ts +224 -0
- package/src/utils/event-stream.ts +142 -0
- package/src/utils/fireworks-model-id.ts +30 -0
- package/src/utils/foundry.ts +8 -0
- package/src/utils/h2-fetch.ts +60 -0
- package/src/utils/http-inspector.ts +176 -0
- package/src/utils/idle-iterator.ts +250 -0
- package/src/utils/json-parse.ts +148 -0
- package/src/utils/oauth/alibaba-coding-plan.ts +59 -0
- package/src/utils/oauth/anthropic.ts +200 -0
- package/src/utils/oauth/api-key-login.ts +87 -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 +16 -0
- package/src/utils/oauth/cloudflare-ai-gateway.ts +48 -0
- package/src/utils/oauth/cursor.ts +157 -0
- package/src/utils/oauth/deepseek.ts +53 -0
- package/src/utils/oauth/firepass.ts +24 -0
- package/src/utils/oauth/fireworks.ts +15 -0
- package/src/utils/oauth/github-copilot.ts +362 -0
- package/src/utils/oauth/gitlab-duo.ts +123 -0
- package/src/utils/oauth/google-antigravity.ts +200 -0
- package/src/utils/oauth/google-gemini-cli.ts +256 -0
- package/src/utils/oauth/google-oauth-shared.ts +110 -0
- package/src/utils/oauth/huggingface.ts +62 -0
- package/src/utils/oauth/index.ts +444 -0
- package/src/utils/oauth/kagi.ts +47 -0
- package/src/utils/oauth/kilo.ts +87 -0
- package/src/utils/oauth/kimi.ts +254 -0
- package/src/utils/oauth/litellm.ts +47 -0
- package/src/utils/oauth/lm-studio.ts +38 -0
- package/src/utils/oauth/minimax-code.ts +78 -0
- package/src/utils/oauth/moonshot.ts +16 -0
- package/src/utils/oauth/nanogpt.ts +15 -0
- package/src/utils/oauth/nvidia.ts +70 -0
- package/src/utils/oauth/oauth.html +199 -0
- package/src/utils/oauth/ollama-cloud.ts +28 -0
- package/src/utils/oauth/ollama.ts +47 -0
- package/src/utils/oauth/openai-codex.ts +299 -0
- package/src/utils/oauth/opencode.ts +49 -0
- package/src/utils/oauth/parallel.ts +46 -0
- package/src/utils/oauth/perplexity.ts +206 -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 +16 -0
- package/src/utils/oauth/tavily.ts +46 -0
- package/src/utils/oauth/together.ts +16 -0
- package/src/utils/oauth/types.ts +94 -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 +137 -0
- package/src/utils/oauth/zai.ts +60 -0
- package/src/utils/oauth/zenmux.ts +15 -0
- package/src/utils/overflow.ts +137 -0
- package/src/utils/parse-bind.ts +54 -0
- package/src/utils/provider-response.ts +30 -0
- package/src/utils/retry-after.ts +110 -0
- package/src/utils/retry.ts +54 -0
- package/src/utils/schema/CONSTRAINTS.md +164 -0
- package/src/utils/schema/adapt.ts +36 -0
- package/src/utils/schema/compatibility.ts +435 -0
- package/src/utils/schema/dereference.ts +98 -0
- package/src/utils/schema/draft.ts +341 -0
- package/src/utils/schema/equality.ts +97 -0
- package/src/utils/schema/fields.ts +190 -0
- package/src/utils/schema/index.ts +13 -0
- package/src/utils/schema/json-schema-validator.ts +577 -0
- package/src/utils/schema/meta-validator.ts +167 -0
- package/src/utils/schema/normalize.ts +1588 -0
- package/src/utils/schema/spill.ts +43 -0
- package/src/utils/schema/stamps.ts +97 -0
- package/src/utils/schema/types.ts +11 -0
- package/src/utils/schema/wire.ts +213 -0
- package/src/utils/schema/zod-decontaminate.ts +331 -0
- package/src/utils/sse-debug.ts +289 -0
- package/src/utils/tool-call-healing.ts +271 -0
- package/src/utils/tool-choice.ts +99 -0
- package/src/utils/validation.ts +1019 -0
- package/src/utils.ts +166 -0
|
@@ -0,0 +1,902 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Shared utilities for Google Generative AI and Google Cloud Code Assist providers.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
import { extractHttpStatusFromError, readSseJson } from "@gajae-code/utils";
|
|
6
|
+
import { calculateCost } from "../models";
|
|
7
|
+
import type {
|
|
8
|
+
Api,
|
|
9
|
+
AssistantMessage,
|
|
10
|
+
Context,
|
|
11
|
+
FetchImpl,
|
|
12
|
+
ImageContent,
|
|
13
|
+
Model,
|
|
14
|
+
StopReason,
|
|
15
|
+
StreamOptions,
|
|
16
|
+
TextContent,
|
|
17
|
+
ThinkingContent,
|
|
18
|
+
Tool,
|
|
19
|
+
ToolCall,
|
|
20
|
+
} from "../types";
|
|
21
|
+
import { normalizeSystemPrompts } from "../utils";
|
|
22
|
+
import { AssistantMessageEventStream } from "../utils/event-stream";
|
|
23
|
+
import { finalizeErrorMessage, type RawHttpRequestDump, withHttpStatus } from "../utils/http-inspector";
|
|
24
|
+
import { normalizeSchemaForCCA, normalizeSchemaForGoogle, toolWireSchema } from "../utils/schema";
|
|
25
|
+
import type {
|
|
26
|
+
Content,
|
|
27
|
+
FinishReason,
|
|
28
|
+
FunctionCallingConfigMode,
|
|
29
|
+
GenerateContentConfig,
|
|
30
|
+
GenerateContentParameters,
|
|
31
|
+
GenerateContentResponse,
|
|
32
|
+
Part,
|
|
33
|
+
ThinkingConfig,
|
|
34
|
+
ThinkingLevel,
|
|
35
|
+
} from "./google-types";
|
|
36
|
+
import { transformMessages } from "./transform-messages";
|
|
37
|
+
import { NON_VISION_IMAGE_PLACEHOLDER } from "./vision-guard";
|
|
38
|
+
|
|
39
|
+
export type {
|
|
40
|
+
Content,
|
|
41
|
+
FunctionCallingConfigMode,
|
|
42
|
+
GenerateContentParameters,
|
|
43
|
+
GenerateContentResponse,
|
|
44
|
+
ThinkingConfig,
|
|
45
|
+
} from "./google-types";
|
|
46
|
+
export { normalizeSchemaForGoogle };
|
|
47
|
+
|
|
48
|
+
type GoogleApiType = "google-generative-ai" | "google-gemini-cli" | "google-vertex";
|
|
49
|
+
|
|
50
|
+
/**
|
|
51
|
+
* Thinking level for Gemini 3 models. Mirrors Google's `ThinkingLevel` enum values.
|
|
52
|
+
* Defined here (not in any specific provider) so all Google providers can reference it
|
|
53
|
+
* without inducing a circular dependency.
|
|
54
|
+
*/
|
|
55
|
+
export type GoogleThinkingLevel = "THINKING_LEVEL_UNSPECIFIED" | "MINIMAL" | "LOW" | "MEDIUM" | "HIGH";
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Sampling/thinking options shared by `streamGoogle` and `streamGoogleVertex`.
|
|
59
|
+
* `google-gemini-cli` uses a different transport and request shape — do not extend this for it.
|
|
60
|
+
*/
|
|
61
|
+
export interface GoogleSharedStreamOptions extends StreamOptions {
|
|
62
|
+
toolChoice?: "auto" | "none" | "any";
|
|
63
|
+
thinking?: {
|
|
64
|
+
enabled: boolean;
|
|
65
|
+
budgetTokens?: number;
|
|
66
|
+
level?: GoogleThinkingLevel;
|
|
67
|
+
};
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* Determines whether a streamed Gemini `Part` should be treated as "thinking".
|
|
72
|
+
*
|
|
73
|
+
* Protocol note (Gemini / Vertex AI thought signatures):
|
|
74
|
+
* - `thought: true` is the definitive marker for thinking content (thought summaries).
|
|
75
|
+
* - `thoughtSignature` is an encrypted representation of the model's internal thought process
|
|
76
|
+
* used to preserve reasoning context across multi-turn interactions.
|
|
77
|
+
* - `thoughtSignature` can appear on ANY part type (text, functionCall, etc.) - it does NOT
|
|
78
|
+
* indicate the part itself is thinking content.
|
|
79
|
+
* - For non-functionCall responses, the signature appears on the last part for context replay.
|
|
80
|
+
* - When persisting/replaying model outputs, signature-bearing parts must be preserved as-is;
|
|
81
|
+
* do not merge/move signatures across parts.
|
|
82
|
+
*
|
|
83
|
+
* See: https://ai.google.dev/gemini-api/docs/thought-signatures
|
|
84
|
+
*/
|
|
85
|
+
export function isThinkingPart(part: Pick<Part, "thought" | "thoughtSignature">): boolean {
|
|
86
|
+
return part.thought === true;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Retain thought signatures during streaming.
|
|
91
|
+
*
|
|
92
|
+
* Some backends only send `thoughtSignature` on the first delta for a given part/block; later deltas may omit it.
|
|
93
|
+
* This helper preserves the last non-empty signature for the current block.
|
|
94
|
+
*
|
|
95
|
+
* Note: this does NOT merge or move signatures across distinct response parts. It only prevents
|
|
96
|
+
* a signature from being overwritten with `undefined` within the same streamed block.
|
|
97
|
+
*/
|
|
98
|
+
export function retainThoughtSignature(existing: string | undefined, incoming: string | undefined): string | undefined {
|
|
99
|
+
if (typeof incoming === "string" && incoming.length > 0) return incoming;
|
|
100
|
+
return existing;
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// Thought signatures must be base64 for Google APIs (TYPE_BYTES).
|
|
104
|
+
const base64SignaturePattern = /^[A-Za-z0-9+/]+={0,2}$/;
|
|
105
|
+
|
|
106
|
+
const SKIP_THOUGHT_SIGNATURE = "skip_thought_signature_validator";
|
|
107
|
+
|
|
108
|
+
function isValidThoughtSignature(signature: string | undefined): boolean {
|
|
109
|
+
if (!signature) return false;
|
|
110
|
+
if (signature.length % 4 !== 0) return false;
|
|
111
|
+
return base64SignaturePattern.test(signature);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Only keep signatures from the same provider/model and with valid base64.
|
|
116
|
+
*/
|
|
117
|
+
function resolveThoughtSignature(isSameProviderAndModel: boolean, signature: string | undefined): string | undefined {
|
|
118
|
+
return isSameProviderAndModel && isValidThoughtSignature(signature) ? signature : undefined;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
/**
|
|
122
|
+
* Anthropic model models via Google APIs require explicit tool call IDs in function calls/responses.
|
|
123
|
+
*/
|
|
124
|
+
export function requiresToolCallId(modelId: string): boolean {
|
|
125
|
+
return modelId.startsWith("claude-");
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
function getGeminiMajorVersion(modelId: string): number | undefined {
|
|
129
|
+
const match = modelId.toLowerCase().match(/^gemini(?:-live)?-(\d+)/);
|
|
130
|
+
if (!match) return undefined;
|
|
131
|
+
return Number.parseInt(match[1], 10);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
function supportsMultimodalFunctionResponse(modelId: string): boolean {
|
|
135
|
+
const geminiMajorVersion = getGeminiMajorVersion(modelId);
|
|
136
|
+
if (geminiMajorVersion !== undefined) {
|
|
137
|
+
return geminiMajorVersion >= 3;
|
|
138
|
+
}
|
|
139
|
+
return true;
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
function isGemini3Model(modelId: string): boolean {
|
|
143
|
+
return modelId.includes("gemini-3");
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
/**
|
|
147
|
+
* Convert internal messages to Gemini Content[] format.
|
|
148
|
+
*/
|
|
149
|
+
export function convertMessages<T extends GoogleApiType>(model: Model<T>, context: Context): Content[] {
|
|
150
|
+
const contents: Content[] = [];
|
|
151
|
+
const normalizeToolCallId = (id: string): string => {
|
|
152
|
+
if (!requiresToolCallId(model.id)) return id;
|
|
153
|
+
return id.replace(/[^a-zA-Z0-9_-]/g, "_").slice(0, 64);
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
const transformedMessages = transformMessages(context.messages, model, normalizeToolCallId);
|
|
157
|
+
|
|
158
|
+
for (const msg of transformedMessages) {
|
|
159
|
+
if (msg.role === "user" || msg.role === "developer") {
|
|
160
|
+
if (typeof msg.content === "string") {
|
|
161
|
+
// Skip empty user messages
|
|
162
|
+
if (!msg.content || msg.content.trim() === "") continue;
|
|
163
|
+
contents.push({
|
|
164
|
+
role: "user",
|
|
165
|
+
parts: [{ text: msg.content.toWellFormed() }],
|
|
166
|
+
});
|
|
167
|
+
} else {
|
|
168
|
+
const supportsImages = model.input.includes("image");
|
|
169
|
+
const parts: Part[] = [];
|
|
170
|
+
let omittedImages = false;
|
|
171
|
+
for (const item of msg.content) {
|
|
172
|
+
if (item.type === "text") {
|
|
173
|
+
const text = item.text.toWellFormed();
|
|
174
|
+
if (text.trim().length === 0) continue;
|
|
175
|
+
parts.push({ text });
|
|
176
|
+
} else if (supportsImages) {
|
|
177
|
+
parts.push({
|
|
178
|
+
inlineData: {
|
|
179
|
+
mimeType: item.mimeType,
|
|
180
|
+
data: item.data,
|
|
181
|
+
},
|
|
182
|
+
});
|
|
183
|
+
} else {
|
|
184
|
+
omittedImages = true;
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (omittedImages) {
|
|
188
|
+
parts.push({ text: NON_VISION_IMAGE_PLACEHOLDER });
|
|
189
|
+
}
|
|
190
|
+
if (parts.length === 0) continue;
|
|
191
|
+
contents.push({
|
|
192
|
+
role: "user",
|
|
193
|
+
parts,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
} else if (msg.role === "assistant") {
|
|
197
|
+
const parts: Part[] = [];
|
|
198
|
+
// Check if message is from same provider and model - only then keep thinking blocks
|
|
199
|
+
const isSameProviderAndModel = msg.provider === model.provider && msg.model === model.id;
|
|
200
|
+
|
|
201
|
+
for (const block of msg.content) {
|
|
202
|
+
if (block.type === "text") {
|
|
203
|
+
// Skip empty text blocks - they can cause issues with some models (e.g. Anthropic model via Antigravity)
|
|
204
|
+
if (!block.text || block.text.trim() === "") continue;
|
|
205
|
+
const thoughtSignature = resolveThoughtSignature(isSameProviderAndModel, block.textSignature);
|
|
206
|
+
parts.push({
|
|
207
|
+
text: block.text.toWellFormed(),
|
|
208
|
+
...(thoughtSignature && { thoughtSignature }),
|
|
209
|
+
});
|
|
210
|
+
} else if (block.type === "thinking") {
|
|
211
|
+
// Skip empty thinking blocks
|
|
212
|
+
if (!block.thinking || block.thinking.trim() === "") continue;
|
|
213
|
+
// Only keep as thinking block if same provider AND same model
|
|
214
|
+
// Otherwise convert to plain text (no tags to avoid model mimicking them)
|
|
215
|
+
if (isSameProviderAndModel) {
|
|
216
|
+
const thoughtSignature = resolveThoughtSignature(isSameProviderAndModel, block.thinkingSignature);
|
|
217
|
+
parts.push({
|
|
218
|
+
thought: true,
|
|
219
|
+
text: block.thinking.toWellFormed(),
|
|
220
|
+
...(thoughtSignature && { thoughtSignature }),
|
|
221
|
+
});
|
|
222
|
+
} else {
|
|
223
|
+
parts.push({
|
|
224
|
+
text: block.thinking.toWellFormed(),
|
|
225
|
+
});
|
|
226
|
+
}
|
|
227
|
+
} else if (block.type === "toolCall") {
|
|
228
|
+
const thoughtSignature = resolveThoughtSignature(isSameProviderAndModel, block.thoughtSignature);
|
|
229
|
+
const effectiveSignature =
|
|
230
|
+
thoughtSignature || (isGemini3Model(model.id) ? SKIP_THOUGHT_SIGNATURE : undefined);
|
|
231
|
+
|
|
232
|
+
const part: Part = {
|
|
233
|
+
functionCall: {
|
|
234
|
+
name: block.name,
|
|
235
|
+
args: block.arguments ?? {},
|
|
236
|
+
...(requiresToolCallId(model.id) ? { id: block.id } : {}),
|
|
237
|
+
},
|
|
238
|
+
};
|
|
239
|
+
if (model.provider === "google-vertex" && part?.functionCall?.id) {
|
|
240
|
+
delete part.functionCall.id; // Vertex AI does not support 'id' in functionCall
|
|
241
|
+
}
|
|
242
|
+
if (effectiveSignature) {
|
|
243
|
+
part.thoughtSignature = effectiveSignature;
|
|
244
|
+
}
|
|
245
|
+
parts.push(part);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
if (parts.length === 0) continue;
|
|
250
|
+
contents.push({
|
|
251
|
+
role: "model",
|
|
252
|
+
parts,
|
|
253
|
+
});
|
|
254
|
+
} else if (msg.role === "toolResult") {
|
|
255
|
+
// Extract text and image content
|
|
256
|
+
const supportsImages = model.input.includes("image");
|
|
257
|
+
const textContent = msg.content.filter((c): c is TextContent => c.type === "text");
|
|
258
|
+
const textResult = textContent.map(c => c.text).join("\n");
|
|
259
|
+
const imageContent = supportsImages ? msg.content.filter((c): c is ImageContent => c.type === "image") : [];
|
|
260
|
+
const omittedImages = !supportsImages && msg.content.some((c): c is ImageContent => c.type === "image");
|
|
261
|
+
|
|
262
|
+
const hasText = textResult.length > 0;
|
|
263
|
+
const hasImages = imageContent.length > 0;
|
|
264
|
+
|
|
265
|
+
// Gemini 3+ models support multimodal function responses with images nested inside
|
|
266
|
+
// functionResponse.parts. Anthropic model and other non-Gemini models behind Cloud Code Assist /
|
|
267
|
+
// Antigravity also accept this shape. Gemini < 3 still needs a separate user image turn.
|
|
268
|
+
const modelSupportsMultimodalFunctionResponse = supportsMultimodalFunctionResponse(model.id);
|
|
269
|
+
|
|
270
|
+
// Use "output" key for success, "error" key for errors as per SDK documentation
|
|
271
|
+
const responseValue = omittedImages
|
|
272
|
+
? [hasText ? textResult.toWellFormed() : "", NON_VISION_IMAGE_PLACEHOLDER].filter(Boolean).join("\n")
|
|
273
|
+
: hasText
|
|
274
|
+
? textResult.toWellFormed()
|
|
275
|
+
: hasImages
|
|
276
|
+
? "(see attached image)"
|
|
277
|
+
: "";
|
|
278
|
+
|
|
279
|
+
const imageParts: Part[] = imageContent.map(imageBlock => ({
|
|
280
|
+
inlineData: {
|
|
281
|
+
mimeType: imageBlock.mimeType,
|
|
282
|
+
data: imageBlock.data,
|
|
283
|
+
},
|
|
284
|
+
}));
|
|
285
|
+
|
|
286
|
+
const includeId = requiresToolCallId(model.id);
|
|
287
|
+
const functionResponsePart: Part = {
|
|
288
|
+
functionResponse: {
|
|
289
|
+
name: msg.toolName,
|
|
290
|
+
response: msg.isError ? { error: responseValue } : { output: responseValue },
|
|
291
|
+
...(hasImages && modelSupportsMultimodalFunctionResponse && { parts: imageParts }),
|
|
292
|
+
...(includeId ? { id: msg.toolCallId } : {}),
|
|
293
|
+
},
|
|
294
|
+
};
|
|
295
|
+
|
|
296
|
+
if (model.provider === "google-vertex" && functionResponsePart.functionResponse?.id) {
|
|
297
|
+
delete functionResponsePart.functionResponse.id; // Vertex AI does not support 'id' in functionResponse
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
// Cloud Code Assist API requires all function responses to be in a single user turn.
|
|
301
|
+
// Check if the last content is already a user turn with function responses and merge.
|
|
302
|
+
const lastContent = contents[contents.length - 1];
|
|
303
|
+
if (lastContent?.role === "user" && lastContent.parts?.some(p => p.functionResponse)) {
|
|
304
|
+
lastContent.parts.push(functionResponsePart);
|
|
305
|
+
} else {
|
|
306
|
+
contents.push({
|
|
307
|
+
role: "user",
|
|
308
|
+
parts: [functionResponsePart],
|
|
309
|
+
});
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
// For Gemini < 3, add images in a separate user message
|
|
313
|
+
if (hasImages && !modelSupportsMultimodalFunctionResponse) {
|
|
314
|
+
contents.push({
|
|
315
|
+
role: "user",
|
|
316
|
+
parts: [{ text: "Tool result image:" }, ...imageParts],
|
|
317
|
+
});
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
return contents;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
/**
|
|
326
|
+
* Convert tools to Gemini function declarations format.
|
|
327
|
+
*
|
|
328
|
+
* We prefer `parametersJsonSchema` (full JSON Schema: anyOf/oneOf/const/etc.).
|
|
329
|
+
*
|
|
330
|
+
* Anthropic model models via Cloud Code Assist require the legacy `parameters` field; the API
|
|
331
|
+
* translates it into Anthropic's `input_schema`. When using that path, we sanitize the
|
|
332
|
+
* schema to remove Google-unsupported JSON Schema keywords.
|
|
333
|
+
*/
|
|
334
|
+
export function convertTools(
|
|
335
|
+
tools: Tool[],
|
|
336
|
+
model: Model<"google-generative-ai" | "google-gemini-cli" | "google-vertex">,
|
|
337
|
+
): { functionDeclarations: Record<string, unknown>[] }[] | undefined {
|
|
338
|
+
if (tools.length === 0) return undefined;
|
|
339
|
+
|
|
340
|
+
/**
|
|
341
|
+
* Anthropic model models on Cloud Code Assist need the legacy `parameters` field;
|
|
342
|
+
* the API translates it into Anthropic's `input_schema`.
|
|
343
|
+
*/
|
|
344
|
+
const useParameters = model.id.startsWith("claude-");
|
|
345
|
+
|
|
346
|
+
return [
|
|
347
|
+
{
|
|
348
|
+
functionDeclarations: tools.map(tool => ({
|
|
349
|
+
name: tool.name,
|
|
350
|
+
description: tool.description || "",
|
|
351
|
+
...(useParameters
|
|
352
|
+
? { parameters: normalizeSchemaForCCA(toolWireSchema(tool)) }
|
|
353
|
+
: { parametersJsonSchema: toolWireSchema(tool) }),
|
|
354
|
+
})),
|
|
355
|
+
},
|
|
356
|
+
];
|
|
357
|
+
}
|
|
358
|
+
|
|
359
|
+
/**
|
|
360
|
+
* Map tool choice string to Gemini FunctionCallingConfigMode.
|
|
361
|
+
*/
|
|
362
|
+
export function mapToolChoice(choice: string): FunctionCallingConfigMode {
|
|
363
|
+
switch (choice) {
|
|
364
|
+
case "auto":
|
|
365
|
+
return "AUTO";
|
|
366
|
+
case "none":
|
|
367
|
+
return "NONE";
|
|
368
|
+
case "any":
|
|
369
|
+
return "ANY";
|
|
370
|
+
default:
|
|
371
|
+
return "AUTO";
|
|
372
|
+
}
|
|
373
|
+
}
|
|
374
|
+
|
|
375
|
+
/**
|
|
376
|
+
* Map Gemini FinishReason to our StopReason.
|
|
377
|
+
*/
|
|
378
|
+
export function mapStopReason(reason: FinishReason): StopReason {
|
|
379
|
+
switch (reason) {
|
|
380
|
+
case "STOP":
|
|
381
|
+
return "stop";
|
|
382
|
+
case "MAX_TOKENS":
|
|
383
|
+
return "length";
|
|
384
|
+
case "BLOCKLIST":
|
|
385
|
+
case "PROHIBITED_CONTENT":
|
|
386
|
+
case "SPII":
|
|
387
|
+
case "SAFETY":
|
|
388
|
+
case "IMAGE_SAFETY":
|
|
389
|
+
case "IMAGE_PROHIBITED_CONTENT":
|
|
390
|
+
case "IMAGE_RECITATION":
|
|
391
|
+
case "IMAGE_OTHER":
|
|
392
|
+
case "RECITATION":
|
|
393
|
+
case "FINISH_REASON_UNSPECIFIED":
|
|
394
|
+
case "OTHER":
|
|
395
|
+
case "LANGUAGE":
|
|
396
|
+
case "MALFORMED_FUNCTION_CALL":
|
|
397
|
+
case "UNEXPECTED_TOOL_CALL":
|
|
398
|
+
case "NO_IMAGE":
|
|
399
|
+
return "error";
|
|
400
|
+
default: {
|
|
401
|
+
throw new Error(`Unhandled stop reason: ${reason satisfies never}`);
|
|
402
|
+
}
|
|
403
|
+
}
|
|
404
|
+
}
|
|
405
|
+
|
|
406
|
+
/**
|
|
407
|
+
* Map string finish reason to our StopReason (for raw API responses).
|
|
408
|
+
*/
|
|
409
|
+
export function mapStopReasonString(reason: string): StopReason {
|
|
410
|
+
switch (reason) {
|
|
411
|
+
case "STOP":
|
|
412
|
+
return "stop";
|
|
413
|
+
case "MAX_TOKENS":
|
|
414
|
+
return "length";
|
|
415
|
+
default:
|
|
416
|
+
return "error";
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
/**
|
|
421
|
+
* Module-local counter for generating unique tool call IDs across Google providers.
|
|
422
|
+
* Shared so that a single monotonically-increasing sequence is used regardless of which
|
|
423
|
+
* Google API surface produced the stream — purely for uniqueness, not ordering semantics.
|
|
424
|
+
*/
|
|
425
|
+
let toolCallCounter = 0;
|
|
426
|
+
|
|
427
|
+
export function nextToolCallId(name: string): string {
|
|
428
|
+
return `${name}_${Date.now()}_${++toolCallCounter}`;
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
/**
|
|
432
|
+
* Push the appropriate `text_end` / `thinking_end` event for the given block.
|
|
433
|
+
* Shared between the SDK-backed stream consumer and the gemini-cli SSE consumer so
|
|
434
|
+
* the end-of-block event shape stays in lockstep.
|
|
435
|
+
*/
|
|
436
|
+
export function pushBlockEndEvent(
|
|
437
|
+
block: TextContent | ThinkingContent,
|
|
438
|
+
contentIndex: number,
|
|
439
|
+
output: AssistantMessage,
|
|
440
|
+
stream: AssistantMessageEventStream,
|
|
441
|
+
): void {
|
|
442
|
+
if (block.type === "text") {
|
|
443
|
+
stream.push({ type: "text_end", contentIndex, content: block.text, partial: output });
|
|
444
|
+
} else {
|
|
445
|
+
stream.push({ type: "thinking_end", contentIndex, content: block.thinking, partial: output });
|
|
446
|
+
}
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
/**
|
|
450
|
+
* Push the three lifecycle events (`toolcall_start` / `toolcall_delta` / `toolcall_end`) for a
|
|
451
|
+
* fully-assembled `ToolCall`. Caller is responsible for appending the toolCall to `output.content`
|
|
452
|
+
* before invoking — this helper does not mutate `output.content`.
|
|
453
|
+
*/
|
|
454
|
+
export function pushToolCallEvents(
|
|
455
|
+
toolCall: ToolCall,
|
|
456
|
+
contentIndex: number,
|
|
457
|
+
output: AssistantMessage,
|
|
458
|
+
stream: AssistantMessageEventStream,
|
|
459
|
+
): void {
|
|
460
|
+
stream.push({ type: "toolcall_start", contentIndex, partial: output });
|
|
461
|
+
stream.push({
|
|
462
|
+
type: "toolcall_delta",
|
|
463
|
+
contentIndex,
|
|
464
|
+
delta: JSON.stringify(toolCall.arguments),
|
|
465
|
+
partial: output,
|
|
466
|
+
});
|
|
467
|
+
stream.push({ type: "toolcall_end", contentIndex, toolCall, partial: output });
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
/**
|
|
471
|
+
* Append a new text- or thinking-block to `output.content` and push the matching
|
|
472
|
+
* `text_start` / `thinking_start` event. `onBeforeStartEvent` lets the SSE consumer
|
|
473
|
+
* inject its `ensureStarted()` first-token side effect into the canonical event order.
|
|
474
|
+
*/
|
|
475
|
+
export function startTextOrThinkingBlock(
|
|
476
|
+
isThinking: boolean,
|
|
477
|
+
output: AssistantMessage,
|
|
478
|
+
stream: AssistantMessageEventStream,
|
|
479
|
+
onBeforeStartEvent?: () => void,
|
|
480
|
+
): TextContent | ThinkingContent {
|
|
481
|
+
const block: TextContent | ThinkingContent = isThinking
|
|
482
|
+
? { type: "thinking", thinking: "", thinkingSignature: undefined }
|
|
483
|
+
: { type: "text", text: "" };
|
|
484
|
+
output.content.push(block);
|
|
485
|
+
onBeforeStartEvent?.();
|
|
486
|
+
const contentIndex = output.content.length - 1;
|
|
487
|
+
if (isThinking) {
|
|
488
|
+
stream.push({ type: "thinking_start", contentIndex, partial: output });
|
|
489
|
+
} else {
|
|
490
|
+
stream.push({ type: "text_start", contentIndex, partial: output });
|
|
491
|
+
}
|
|
492
|
+
return block;
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
/**
|
|
496
|
+
* Drives the chunked `generateContentStream` iterator into an `AssistantMessage` and
|
|
497
|
+
* the corresponding `AssistantMessageEventStream`. Shared between `streamGoogle` and
|
|
498
|
+
* `streamGoogleVertex` — every observable event order and stop-reason rule is preserved.
|
|
499
|
+
*
|
|
500
|
+
* The caller still owns: `output` construction, timing fields (`duration`/`ttft`),
|
|
501
|
+
* `rawRequestDump`, the `client.models.generateContentStream(params)` call itself,
|
|
502
|
+
* pushing `start`/`done`/`error` events, and the surrounding try/catch that translates
|
|
503
|
+
* thrown errors into `output.stopReason`/`errorMessage`.
|
|
504
|
+
*
|
|
505
|
+
* This helper handles: the chunk loop, currentBlock flush transitions, usage metadata
|
|
506
|
+
* decoding (`calculateCost` included), tool-call id collision avoidance, finish-reason
|
|
507
|
+
* mapping, and the abort/stop-reason post-checks that re-throw to bubble into the
|
|
508
|
+
* caller's catch.
|
|
509
|
+
*/
|
|
510
|
+
export async function consumeGoogleStream<T extends GoogleApiType>(args: {
|
|
511
|
+
googleStream: AsyncIterable<GenerateContentResponse>;
|
|
512
|
+
output: AssistantMessage;
|
|
513
|
+
stream: AssistantMessageEventStream;
|
|
514
|
+
model: Model<T>;
|
|
515
|
+
options: { signal?: AbortSignal } | undefined;
|
|
516
|
+
/** Vertex preserves `textSignature` on streamed text deltas; google-generative-ai does not. */
|
|
517
|
+
retainTextSignature?: boolean;
|
|
518
|
+
onFirstToken?: () => void;
|
|
519
|
+
}): Promise<void> {
|
|
520
|
+
const { googleStream, output, stream, model, options, retainTextSignature, onFirstToken } = args;
|
|
521
|
+
const blocks = output.content;
|
|
522
|
+
const blockIndex = () => blocks.length - 1;
|
|
523
|
+
let currentBlock: TextContent | ThinkingContent | null = null;
|
|
524
|
+
let firstTokenSeen = false;
|
|
525
|
+
|
|
526
|
+
const flushCurrent = () => {
|
|
527
|
+
if (!currentBlock) return;
|
|
528
|
+
pushBlockEndEvent(currentBlock, blockIndex(), output, stream);
|
|
529
|
+
};
|
|
530
|
+
|
|
531
|
+
for await (const chunk of googleStream) {
|
|
532
|
+
const candidate = chunk.candidates?.[0];
|
|
533
|
+
if (candidate?.content?.parts) {
|
|
534
|
+
for (const part of candidate.content.parts) {
|
|
535
|
+
if (part.text !== undefined) {
|
|
536
|
+
if (!firstTokenSeen) {
|
|
537
|
+
firstTokenSeen = true;
|
|
538
|
+
onFirstToken?.();
|
|
539
|
+
}
|
|
540
|
+
const isThinking = isThinkingPart(part);
|
|
541
|
+
if (
|
|
542
|
+
!currentBlock ||
|
|
543
|
+
(isThinking && currentBlock.type !== "thinking") ||
|
|
544
|
+
(!isThinking && currentBlock.type !== "text")
|
|
545
|
+
) {
|
|
546
|
+
flushCurrent();
|
|
547
|
+
currentBlock = startTextOrThinkingBlock(isThinking, output, stream);
|
|
548
|
+
}
|
|
549
|
+
if (currentBlock.type === "thinking") {
|
|
550
|
+
currentBlock.thinking += part.text;
|
|
551
|
+
currentBlock.thinkingSignature = retainThoughtSignature(
|
|
552
|
+
currentBlock.thinkingSignature,
|
|
553
|
+
part.thoughtSignature,
|
|
554
|
+
);
|
|
555
|
+
stream.push({
|
|
556
|
+
type: "thinking_delta",
|
|
557
|
+
contentIndex: blockIndex(),
|
|
558
|
+
delta: part.text,
|
|
559
|
+
partial: output,
|
|
560
|
+
});
|
|
561
|
+
} else {
|
|
562
|
+
currentBlock.text += part.text;
|
|
563
|
+
if (retainTextSignature) {
|
|
564
|
+
currentBlock.textSignature = retainThoughtSignature(
|
|
565
|
+
currentBlock.textSignature,
|
|
566
|
+
part.thoughtSignature,
|
|
567
|
+
);
|
|
568
|
+
}
|
|
569
|
+
stream.push({
|
|
570
|
+
type: "text_delta",
|
|
571
|
+
contentIndex: blockIndex(),
|
|
572
|
+
delta: part.text,
|
|
573
|
+
partial: output,
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
|
|
578
|
+
if (part.functionCall) {
|
|
579
|
+
if (currentBlock) {
|
|
580
|
+
flushCurrent();
|
|
581
|
+
currentBlock = null;
|
|
582
|
+
}
|
|
583
|
+
|
|
584
|
+
// Generate unique ID if not provided or if it's a duplicate
|
|
585
|
+
const providedId = part.functionCall.id;
|
|
586
|
+
const needsNewId = !providedId || output.content.some(b => b.type === "toolCall" && b.id === providedId);
|
|
587
|
+
const toolCallId = needsNewId ? nextToolCallId(part.functionCall.name || "tool") : providedId;
|
|
588
|
+
|
|
589
|
+
const toolCall: ToolCall = {
|
|
590
|
+
type: "toolCall",
|
|
591
|
+
id: toolCallId,
|
|
592
|
+
name: part.functionCall.name || "",
|
|
593
|
+
arguments: (part.functionCall.args ?? {}) as Record<string, any>,
|
|
594
|
+
...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),
|
|
595
|
+
};
|
|
596
|
+
|
|
597
|
+
output.content.push(toolCall);
|
|
598
|
+
pushToolCallEvents(toolCall, blockIndex(), output, stream);
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
|
|
603
|
+
if (candidate?.finishReason) {
|
|
604
|
+
output.stopReason = mapStopReason(candidate.finishReason);
|
|
605
|
+
if (output.content.some(b => b.type === "toolCall")) {
|
|
606
|
+
output.stopReason = "toolUse";
|
|
607
|
+
}
|
|
608
|
+
}
|
|
609
|
+
|
|
610
|
+
if (chunk.usageMetadata) {
|
|
611
|
+
// promptTokenCount includes cachedContentTokenCount when cached content is used.
|
|
612
|
+
// Subtract to get non-cached input, matching the OpenAI convention where
|
|
613
|
+
// input = uncached prompt tokens and cacheRead = cached tokens so that
|
|
614
|
+
// input + cacheRead = total prompt tokens (no double-counting).
|
|
615
|
+
// Ref: https://ai.google.dev/api/generate-content#v1beta.GenerateContentResponse.UsageMetadata
|
|
616
|
+
const cachedTokens = chunk.usageMetadata.cachedContentTokenCount || 0;
|
|
617
|
+
const thinkingTokens = chunk.usageMetadata.thoughtsTokenCount || 0;
|
|
618
|
+
output.usage = {
|
|
619
|
+
input: (chunk.usageMetadata.promptTokenCount || 0) - cachedTokens,
|
|
620
|
+
output: (chunk.usageMetadata.candidatesTokenCount || 0) + thinkingTokens,
|
|
621
|
+
cacheRead: cachedTokens,
|
|
622
|
+
cacheWrite: 0,
|
|
623
|
+
totalTokens: chunk.usageMetadata.totalTokenCount || 0,
|
|
624
|
+
...(thinkingTokens > 0 ? { reasoningTokens: thinkingTokens } : {}),
|
|
625
|
+
cost: {
|
|
626
|
+
input: 0,
|
|
627
|
+
output: 0,
|
|
628
|
+
cacheRead: 0,
|
|
629
|
+
cacheWrite: 0,
|
|
630
|
+
total: 0,
|
|
631
|
+
},
|
|
632
|
+
};
|
|
633
|
+
calculateCost(model, output.usage);
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
|
|
637
|
+
flushCurrent();
|
|
638
|
+
|
|
639
|
+
if (options?.signal?.aborted) {
|
|
640
|
+
throw new Error("Request was aborted");
|
|
641
|
+
}
|
|
642
|
+
|
|
643
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") {
|
|
644
|
+
throw new Error(output.errorMessage ?? "An unknown error occurred");
|
|
645
|
+
}
|
|
646
|
+
}
|
|
647
|
+
|
|
648
|
+
/**
|
|
649
|
+
* Generation/sampling fields that map directly onto Gemini's `GenerateContentConfig`.
|
|
650
|
+
* Excludes any provider-specific extensions (`topP`/`topK`/etc are all forwarded as-is).
|
|
651
|
+
*/
|
|
652
|
+
interface GoogleGenerationConfig extends GenerateContentConfig {
|
|
653
|
+
topP?: number;
|
|
654
|
+
topK?: number;
|
|
655
|
+
minP?: number;
|
|
656
|
+
presencePenalty?: number;
|
|
657
|
+
repetitionPenalty?: number;
|
|
658
|
+
}
|
|
659
|
+
|
|
660
|
+
/**
|
|
661
|
+
* Build the `GenerateContentParameters` payload for the public Gemini API and Vertex AI.
|
|
662
|
+
* Both surfaces accept the same `GenerateContentConfig` shape — every numeric/string knob,
|
|
663
|
+
* tool-config, thinking-config, and system-instruction conversion is identical.
|
|
664
|
+
*
|
|
665
|
+
* `google-gemini-cli` is NOT routed through here: its `CloudCodeAssistRequest` body has a
|
|
666
|
+
* distinct top-level shape (project/request/requestType) and a different thinking-config
|
|
667
|
+
* placement on `generationConfig`.
|
|
668
|
+
*/
|
|
669
|
+
export function buildGoogleGenerateContentParams<T extends "google-generative-ai" | "google-vertex">(
|
|
670
|
+
model: Model<T>,
|
|
671
|
+
context: Context,
|
|
672
|
+
options: GoogleSharedStreamOptions,
|
|
673
|
+
): GenerateContentParameters {
|
|
674
|
+
const systemPrompts = normalizeSystemPrompts(context.systemPrompt);
|
|
675
|
+
const contents = convertMessages(model, context);
|
|
676
|
+
|
|
677
|
+
const generationConfig: GoogleGenerationConfig = {};
|
|
678
|
+
if (options.temperature !== undefined) generationConfig.temperature = options.temperature;
|
|
679
|
+
if (options.maxTokens !== undefined) generationConfig.maxOutputTokens = options.maxTokens;
|
|
680
|
+
if (options.topP !== undefined) generationConfig.topP = options.topP;
|
|
681
|
+
if (options.topK !== undefined) generationConfig.topK = options.topK;
|
|
682
|
+
if (options.minP !== undefined) generationConfig.minP = options.minP;
|
|
683
|
+
if (options.presencePenalty !== undefined) generationConfig.presencePenalty = options.presencePenalty;
|
|
684
|
+
if (options.repetitionPenalty !== undefined) generationConfig.repetitionPenalty = options.repetitionPenalty;
|
|
685
|
+
|
|
686
|
+
const config: GenerateContentConfig = {
|
|
687
|
+
...(Object.keys(generationConfig).length > 0 && generationConfig),
|
|
688
|
+
...(systemPrompts.length > 0 && { systemInstruction: { parts: systemPrompts.map(text => ({ text })) } }),
|
|
689
|
+
...(context.tools && context.tools.length > 0 && { tools: convertTools(context.tools, model) }),
|
|
690
|
+
};
|
|
691
|
+
|
|
692
|
+
if (context.tools && context.tools.length > 0 && options.toolChoice) {
|
|
693
|
+
config.toolConfig = {
|
|
694
|
+
functionCallingConfig: {
|
|
695
|
+
mode: mapToolChoice(options.toolChoice),
|
|
696
|
+
},
|
|
697
|
+
};
|
|
698
|
+
} else {
|
|
699
|
+
config.toolConfig = undefined;
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
if (options.thinking?.enabled && model.reasoning) {
|
|
703
|
+
const cfg: ThinkingConfig = { includeThoughts: true };
|
|
704
|
+
if (options.thinking.level !== undefined) {
|
|
705
|
+
// GoogleThinkingLevel mirrors the SDK's `ThinkingLevel` string enum values 1:1.
|
|
706
|
+
cfg.thinkingLevel = options.thinking.level as ThinkingLevel;
|
|
707
|
+
} else if (options.thinking.budgetTokens !== undefined) {
|
|
708
|
+
cfg.thinkingBudget = options.thinking.budgetTokens;
|
|
709
|
+
}
|
|
710
|
+
config.thinkingConfig = cfg;
|
|
711
|
+
}
|
|
712
|
+
|
|
713
|
+
if (options.signal) {
|
|
714
|
+
if (options.signal.aborted) {
|
|
715
|
+
throw new Error("Request aborted");
|
|
716
|
+
}
|
|
717
|
+
config.abortSignal = options.signal;
|
|
718
|
+
}
|
|
719
|
+
|
|
720
|
+
return {
|
|
721
|
+
model: model.id,
|
|
722
|
+
contents,
|
|
723
|
+
config,
|
|
724
|
+
};
|
|
725
|
+
}
|
|
726
|
+
|
|
727
|
+
/**
|
|
728
|
+
* Drive the `streamGoogle` / `streamGoogleVertex` event flow: build the assistant message,
|
|
729
|
+
* push start/done/error events, run `consumeGoogleStream`, and translate thrown errors into
|
|
730
|
+
* the canonical `error` event shape.
|
|
731
|
+
*
|
|
732
|
+
* Caller-supplied `prepare()` runs inside the try-block so any failure (missing project,
|
|
733
|
+
* bad auth, etc.) is funneled through the same error path as a streaming failure.
|
|
734
|
+
*/
|
|
735
|
+
export interface GoogleGenAIRequestPlan {
|
|
736
|
+
params: GenerateContentParameters;
|
|
737
|
+
url: string;
|
|
738
|
+
headers: Record<string, string>;
|
|
739
|
+
fetch?: FetchImpl;
|
|
740
|
+
}
|
|
741
|
+
|
|
742
|
+
export function streamGoogleGenAI<T extends "google-generative-ai" | "google-vertex">(args: {
|
|
743
|
+
model: Model<T>;
|
|
744
|
+
options: GoogleSharedStreamOptions | undefined;
|
|
745
|
+
api: T;
|
|
746
|
+
retainTextSignature?: boolean;
|
|
747
|
+
prepare: () => GoogleGenAIRequestPlan | Promise<GoogleGenAIRequestPlan>;
|
|
748
|
+
}): AssistantMessageEventStream {
|
|
749
|
+
const { model, options, api, retainTextSignature, prepare } = args;
|
|
750
|
+
const stream = new AssistantMessageEventStream();
|
|
751
|
+
|
|
752
|
+
(async () => {
|
|
753
|
+
const startTime = Date.now();
|
|
754
|
+
let firstTokenTime: number | undefined;
|
|
755
|
+
|
|
756
|
+
const output: AssistantMessage = {
|
|
757
|
+
role: "assistant",
|
|
758
|
+
content: [],
|
|
759
|
+
api: api as Api,
|
|
760
|
+
provider: model.provider,
|
|
761
|
+
model: model.id,
|
|
762
|
+
usage: {
|
|
763
|
+
input: 0,
|
|
764
|
+
output: 0,
|
|
765
|
+
cacheRead: 0,
|
|
766
|
+
cacheWrite: 0,
|
|
767
|
+
totalTokens: 0,
|
|
768
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
769
|
+
},
|
|
770
|
+
stopReason: "stop",
|
|
771
|
+
timestamp: Date.now(),
|
|
772
|
+
};
|
|
773
|
+
let rawRequestDump: RawHttpRequestDump | undefined;
|
|
774
|
+
|
|
775
|
+
try {
|
|
776
|
+
const plan = await prepare();
|
|
777
|
+
let params = plan.params;
|
|
778
|
+
const replacement = await options?.onPayload?.(params, model);
|
|
779
|
+
if (replacement !== undefined) {
|
|
780
|
+
params = replacement as GenerateContentParameters;
|
|
781
|
+
}
|
|
782
|
+
rawRequestDump = {
|
|
783
|
+
provider: model.provider,
|
|
784
|
+
api: output.api,
|
|
785
|
+
model: model.id,
|
|
786
|
+
method: "POST",
|
|
787
|
+
url: plan.url,
|
|
788
|
+
body: params,
|
|
789
|
+
headers: plan.headers,
|
|
790
|
+
};
|
|
791
|
+
|
|
792
|
+
const wireBody = paramsToWireBody(params);
|
|
793
|
+
const fetchImpl = plan.fetch ?? options?.fetch ?? (globalThis.fetch.bind(globalThis) as FetchImpl);
|
|
794
|
+
const response = await fetchImpl(plan.url, {
|
|
795
|
+
method: "POST",
|
|
796
|
+
headers: { ...plan.headers, "Content-Type": "application/json", Accept: "text/event-stream" },
|
|
797
|
+
body: JSON.stringify(wireBody),
|
|
798
|
+
signal: options?.signal,
|
|
799
|
+
});
|
|
800
|
+
if (!response.ok) {
|
|
801
|
+
const errorText = await response.text().catch(() => "");
|
|
802
|
+
throw withHttpStatus(
|
|
803
|
+
new Error(`Google API error (${response.status}): ${extractGoogleErrorMessage(errorText)}`),
|
|
804
|
+
response.status,
|
|
805
|
+
);
|
|
806
|
+
}
|
|
807
|
+
if (!response.body) {
|
|
808
|
+
throw new Error("Google API returned an empty response body");
|
|
809
|
+
}
|
|
810
|
+
|
|
811
|
+
const googleStream = readSseJson<GenerateContentResponse>(response.body, options?.signal, event =>
|
|
812
|
+
options?.onSseEvent?.({ event: event.event, data: event.data, raw: [...event.raw] }, model),
|
|
813
|
+
);
|
|
814
|
+
|
|
815
|
+
stream.push({ type: "start", partial: output });
|
|
816
|
+
await consumeGoogleStream({
|
|
817
|
+
googleStream,
|
|
818
|
+
output,
|
|
819
|
+
stream,
|
|
820
|
+
model,
|
|
821
|
+
options,
|
|
822
|
+
retainTextSignature,
|
|
823
|
+
onFirstToken: () => {
|
|
824
|
+
firstTokenTime = Date.now();
|
|
825
|
+
},
|
|
826
|
+
});
|
|
827
|
+
|
|
828
|
+
output.duration = Date.now() - startTime;
|
|
829
|
+
if (firstTokenTime) output.ttft = firstTokenTime - startTime;
|
|
830
|
+
stream.push({ type: "done", reason: output.stopReason as "length" | "stop" | "toolUse", message: output });
|
|
831
|
+
stream.end();
|
|
832
|
+
} catch (error) {
|
|
833
|
+
for (const block of output.content) {
|
|
834
|
+
if ("index" in block) {
|
|
835
|
+
delete (block as { index?: number }).index;
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
839
|
+
output.errorStatus = extractHttpStatusFromError(error);
|
|
840
|
+
output.errorMessage = await finalizeErrorMessage(error, rawRequestDump);
|
|
841
|
+
output.duration = Date.now() - startTime;
|
|
842
|
+
if (firstTokenTime) output.ttft = firstTokenTime - startTime;
|
|
843
|
+
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
844
|
+
stream.end();
|
|
845
|
+
}
|
|
846
|
+
})();
|
|
847
|
+
|
|
848
|
+
return stream;
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
/**
|
|
852
|
+
* Lift the SDK's `params.config` fields out of `config` and place them where the
|
|
853
|
+
* Gemini / Vertex AI REST API expects them on the request body. Mirrors the
|
|
854
|
+
* generateContentParametersTo{Mldev,Vertex} transformation in @google/genai
|
|
855
|
+
* for the subset of fields this codebase actually sets.
|
|
856
|
+
*
|
|
857
|
+
* `abortSignal` is intentionally dropped — the SDK propagates it via `fetch.signal`,
|
|
858
|
+
* which our caller already wires up through `options.signal`.
|
|
859
|
+
*/
|
|
860
|
+
function paramsToWireBody(params: GenerateContentParameters): Record<string, unknown> {
|
|
861
|
+
const body: Record<string, unknown> = { contents: params.contents };
|
|
862
|
+
const config = params.config;
|
|
863
|
+
if (!config) return body;
|
|
864
|
+
|
|
865
|
+
if (config.systemInstruction !== undefined) body.systemInstruction = config.systemInstruction;
|
|
866
|
+
if (config.tools !== undefined) body.tools = config.tools;
|
|
867
|
+
if (config.toolConfig !== undefined) body.toolConfig = config.toolConfig;
|
|
868
|
+
if (config.safetySettings !== undefined) body.safetySettings = config.safetySettings;
|
|
869
|
+
if (config.cachedContent !== undefined) body.cachedContent = config.cachedContent;
|
|
870
|
+
|
|
871
|
+
const gen: Record<string, unknown> = {};
|
|
872
|
+
if (config.temperature !== undefined) gen.temperature = config.temperature;
|
|
873
|
+
if (config.maxOutputTokens !== undefined) gen.maxOutputTokens = config.maxOutputTokens;
|
|
874
|
+
if (config.topP !== undefined) gen.topP = config.topP;
|
|
875
|
+
if (config.topK !== undefined) gen.topK = config.topK;
|
|
876
|
+
if (config.candidateCount !== undefined) gen.candidateCount = config.candidateCount;
|
|
877
|
+
if (config.stopSequences !== undefined) gen.stopSequences = config.stopSequences;
|
|
878
|
+
if (config.presencePenalty !== undefined) gen.presencePenalty = config.presencePenalty;
|
|
879
|
+
if (config.frequencyPenalty !== undefined) gen.frequencyPenalty = config.frequencyPenalty;
|
|
880
|
+
if (config.seed !== undefined) gen.seed = config.seed;
|
|
881
|
+
if (config.responseMimeType !== undefined) gen.responseMimeType = config.responseMimeType;
|
|
882
|
+
if (config.responseSchema !== undefined) gen.responseSchema = config.responseSchema;
|
|
883
|
+
if (config.responseJsonSchema !== undefined) gen.responseJsonSchema = config.responseJsonSchema;
|
|
884
|
+
if (config.responseModalities !== undefined) gen.responseModalities = config.responseModalities;
|
|
885
|
+
if (config.thinkingConfig !== undefined) gen.thinkingConfig = config.thinkingConfig;
|
|
886
|
+
const generationConfig = config as unknown as { minP?: number; repetitionPenalty?: number };
|
|
887
|
+
if (generationConfig.minP !== undefined) gen.minP = generationConfig.minP;
|
|
888
|
+
if (generationConfig.repetitionPenalty !== undefined) gen.repetitionPenalty = generationConfig.repetitionPenalty;
|
|
889
|
+
if (Object.keys(gen).length > 0) body.generationConfig = gen;
|
|
890
|
+
return body;
|
|
891
|
+
}
|
|
892
|
+
|
|
893
|
+
function extractGoogleErrorMessage(errorText: string): string {
|
|
894
|
+
if (!errorText) return "Unknown error";
|
|
895
|
+
try {
|
|
896
|
+
const parsed = JSON.parse(errorText) as { error?: { message?: string } };
|
|
897
|
+
if (parsed.error?.message) return parsed.error.message;
|
|
898
|
+
} catch {
|
|
899
|
+
// fall through to raw text
|
|
900
|
+
}
|
|
901
|
+
return errorText;
|
|
902
|
+
}
|