@handy_wote/pi-ai 0.81.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/README.md +1623 -0
- package/dist/api/anthropic-messages.d.ts +71 -0
- package/dist/api/anthropic-messages.d.ts.map +1 -0
- package/dist/api/anthropic-messages.js +1024 -0
- package/dist/api/anthropic-messages.js.map +1 -0
- package/dist/api/anthropic-messages.lazy.d.ts +3 -0
- package/dist/api/anthropic-messages.lazy.d.ts.map +1 -0
- package/dist/api/anthropic-messages.lazy.js +3 -0
- package/dist/api/anthropic-messages.lazy.js.map +1 -0
- package/dist/api/azure-openai-responses.d.ts +15 -0
- package/dist/api/azure-openai-responses.d.ts.map +1 -0
- package/dist/api/azure-openai-responses.js +221 -0
- package/dist/api/azure-openai-responses.js.map +1 -0
- package/dist/api/azure-openai-responses.lazy.d.ts +3 -0
- package/dist/api/azure-openai-responses.lazy.d.ts.map +1 -0
- package/dist/api/azure-openai-responses.lazy.js +3 -0
- package/dist/api/azure-openai-responses.lazy.js.map +1 -0
- package/dist/api/bedrock-converse-stream.d.ts +38 -0
- package/dist/api/bedrock-converse-stream.d.ts.map +1 -0
- package/dist/api/bedrock-converse-stream.js +883 -0
- package/dist/api/bedrock-converse-stream.js.map +1 -0
- package/dist/api/bedrock-converse-stream.lazy.d.ts +9 -0
- package/dist/api/bedrock-converse-stream.lazy.d.ts.map +1 -0
- package/dist/api/bedrock-converse-stream.lazy.js +30 -0
- package/dist/api/bedrock-converse-stream.lazy.js.map +1 -0
- package/dist/api/cloudflare.d.ts +9 -0
- package/dist/api/cloudflare.d.ts.map +1 -0
- package/dist/api/cloudflare.js +9 -0
- package/dist/api/cloudflare.js.map +1 -0
- package/dist/api/github-copilot-headers.d.ts +8 -0
- package/dist/api/github-copilot-headers.d.ts.map +1 -0
- package/dist/api/github-copilot-headers.js +29 -0
- package/dist/api/github-copilot-headers.js.map +1 -0
- package/dist/api/google-generative-ai.d.ts +13 -0
- package/dist/api/google-generative-ai.d.ts.map +1 -0
- package/dist/api/google-generative-ai.js +407 -0
- package/dist/api/google-generative-ai.js.map +1 -0
- package/dist/api/google-generative-ai.lazy.d.ts +3 -0
- package/dist/api/google-generative-ai.lazy.d.ts.map +1 -0
- package/dist/api/google-generative-ai.lazy.js +3 -0
- package/dist/api/google-generative-ai.lazy.js.map +1 -0
- package/dist/api/google-shared.d.ts +70 -0
- package/dist/api/google-shared.d.ts.map +1 -0
- package/dist/api/google-shared.js +329 -0
- package/dist/api/google-shared.js.map +1 -0
- package/dist/api/google-vertex.d.ts +15 -0
- package/dist/api/google-vertex.d.ts.map +1 -0
- package/dist/api/google-vertex.js +456 -0
- package/dist/api/google-vertex.js.map +1 -0
- package/dist/api/google-vertex.lazy.d.ts +3 -0
- package/dist/api/google-vertex.lazy.d.ts.map +1 -0
- package/dist/api/google-vertex.lazy.js +3 -0
- package/dist/api/google-vertex.lazy.js.map +1 -0
- package/dist/api/lazy.d.ts +15 -0
- package/dist/api/lazy.d.ts.map +1 -0
- package/dist/api/lazy.js +58 -0
- package/dist/api/lazy.js.map +1 -0
- package/dist/api/mistral-conversations.d.ts +25 -0
- package/dist/api/mistral-conversations.d.ts.map +1 -0
- package/dist/api/mistral-conversations.js +555 -0
- package/dist/api/mistral-conversations.js.map +1 -0
- package/dist/api/mistral-conversations.lazy.d.ts +3 -0
- package/dist/api/mistral-conversations.lazy.d.ts.map +1 -0
- package/dist/api/mistral-conversations.lazy.js +3 -0
- package/dist/api/mistral-conversations.lazy.js.map +1 -0
- package/dist/api/openai-codex-responses.d.ts +31 -0
- package/dist/api/openai-codex-responses.d.ts.map +1 -0
- package/dist/api/openai-codex-responses.js +1222 -0
- package/dist/api/openai-codex-responses.js.map +1 -0
- package/dist/api/openai-codex-responses.lazy.d.ts +3 -0
- package/dist/api/openai-codex-responses.lazy.d.ts.map +1 -0
- package/dist/api/openai-codex-responses.lazy.js +3 -0
- package/dist/api/openai-codex-responses.lazy.js.map +1 -0
- package/dist/api/openai-completions.d.ts +20 -0
- package/dist/api/openai-completions.d.ts.map +1 -0
- package/dist/api/openai-completions.js +1120 -0
- package/dist/api/openai-completions.js.map +1 -0
- package/dist/api/openai-completions.lazy.d.ts +3 -0
- package/dist/api/openai-completions.lazy.d.ts.map +1 -0
- package/dist/api/openai-completions.lazy.js +3 -0
- package/dist/api/openai-completions.lazy.js.map +1 -0
- package/dist/api/openai-prompt-cache.d.ts +3 -0
- package/dist/api/openai-prompt-cache.d.ts.map +1 -0
- package/dist/api/openai-prompt-cache.js +10 -0
- package/dist/api/openai-prompt-cache.js.map +1 -0
- package/dist/api/openai-responses-shared.d.ts +20 -0
- package/dist/api/openai-responses-shared.d.ts.map +1 -0
- package/dist/api/openai-responses-shared.js +535 -0
- package/dist/api/openai-responses-shared.js.map +1 -0
- package/dist/api/openai-responses.d.ts +14 -0
- package/dist/api/openai-responses.d.ts.map +1 -0
- package/dist/api/openai-responses.js +246 -0
- package/dist/api/openai-responses.js.map +1 -0
- package/dist/api/openai-responses.lazy.d.ts +3 -0
- package/dist/api/openai-responses.lazy.d.ts.map +1 -0
- package/dist/api/openai-responses.lazy.js +3 -0
- package/dist/api/openai-responses.lazy.js.map +1 -0
- package/dist/api/openrouter-images.d.ts +3 -0
- package/dist/api/openrouter-images.d.ts.map +1 -0
- package/dist/api/openrouter-images.js +126 -0
- package/dist/api/openrouter-images.js.map +1 -0
- package/dist/api/openrouter-images.lazy.d.ts +3 -0
- package/dist/api/openrouter-images.lazy.d.ts.map +1 -0
- package/dist/api/openrouter-images.lazy.js +4 -0
- package/dist/api/openrouter-images.lazy.js.map +1 -0
- package/dist/api/pi-messages.d.ts +97 -0
- package/dist/api/pi-messages.d.ts.map +1 -0
- package/dist/api/pi-messages.js +307 -0
- package/dist/api/pi-messages.js.map +1 -0
- package/dist/api/pi-messages.lazy.d.ts +3 -0
- package/dist/api/pi-messages.lazy.d.ts.map +1 -0
- package/dist/api/pi-messages.lazy.js +3 -0
- package/dist/api/pi-messages.lazy.js.map +1 -0
- package/dist/api/simple-options.d.ts +9 -0
- package/dist/api/simple-options.d.ts.map +1 -0
- package/dist/api/simple-options.js +52 -0
- package/dist/api/simple-options.js.map +1 -0
- package/dist/api/transform-messages.d.ts +8 -0
- package/dist/api/transform-messages.d.ts.map +1 -0
- package/dist/api/transform-messages.js +187 -0
- package/dist/api/transform-messages.js.map +1 -0
- package/dist/auth/context.d.ts +7 -0
- package/dist/auth/context.d.ts.map +1 -0
- package/dist/auth/context.js +42 -0
- package/dist/auth/context.js.map +1 -0
- package/dist/auth/credential-store.d.ts +17 -0
- package/dist/auth/credential-store.d.ts.map +1 -0
- package/dist/auth/credential-store.js +40 -0
- package/dist/auth/credential-store.js.map +1 -0
- package/dist/auth/helpers.d.ts +21 -0
- package/dist/auth/helpers.d.ts.map +1 -0
- package/dist/auth/helpers.js +48 -0
- package/dist/auth/helpers.js.map +1 -0
- package/dist/auth/oauth/anthropic.d.ts +9 -0
- package/dist/auth/oauth/anthropic.d.ts.map +1 -0
- package/dist/auth/oauth/anthropic.js +302 -0
- package/dist/auth/oauth/anthropic.js.map +1 -0
- package/dist/auth/oauth/device-code.d.ts +23 -0
- package/dist/auth/oauth/device-code.d.ts.map +1 -0
- package/dist/auth/oauth/device-code.js +69 -0
- package/dist/auth/oauth/device-code.js.map +1 -0
- package/dist/auth/oauth/github-copilot.d.ts +6 -0
- package/dist/auth/oauth/github-copilot.d.ts.map +1 -0
- package/dist/auth/oauth/github-copilot.js +313 -0
- package/dist/auth/oauth/github-copilot.js.map +1 -0
- package/dist/auth/oauth/load.d.ts +23 -0
- package/dist/auth/oauth/load.d.ts.map +1 -0
- package/dist/auth/oauth/load.js +49 -0
- package/dist/auth/oauth/load.js.map +1 -0
- package/dist/auth/oauth/oauth-page.d.ts +3 -0
- package/dist/auth/oauth/oauth-page.d.ts.map +1 -0
- package/dist/auth/oauth/oauth-page.js +105 -0
- package/dist/auth/oauth/oauth-page.js.map +1 -0
- package/dist/auth/oauth/openai-codex.d.ts +9 -0
- package/dist/auth/oauth/openai-codex.d.ts.map +1 -0
- package/dist/auth/oauth/openai-codex.js +443 -0
- package/dist/auth/oauth/openai-codex.js.map +1 -0
- package/dist/auth/oauth/pkce.d.ts +13 -0
- package/dist/auth/oauth/pkce.d.ts.map +1 -0
- package/dist/auth/oauth/pkce.js +31 -0
- package/dist/auth/oauth/pkce.js.map +1 -0
- package/dist/auth/oauth/radius.d.ts +16 -0
- package/dist/auth/oauth/radius.d.ts.map +1 -0
- package/dist/auth/oauth/radius.js +321 -0
- package/dist/auth/oauth/radius.js.map +1 -0
- package/dist/auth/oauth/xai.d.ts +6 -0
- package/dist/auth/oauth/xai.d.ts.map +1 -0
- package/dist/auth/oauth/xai.js +189 -0
- package/dist/auth/oauth/xai.js.map +1 -0
- package/dist/auth/resolve.d.ts +24 -0
- package/dist/auth/resolve.d.ts.map +1 -0
- package/dist/auth/resolve.js +103 -0
- package/dist/auth/resolve.js.map +1 -0
- package/dist/auth/types.d.ts +219 -0
- package/dist/auth/types.d.ts.map +1 -0
- package/dist/auth/types.js +2 -0
- package/dist/auth/types.js.map +1 -0
- package/dist/bedrock-provider.d.ts +5 -0
- package/dist/bedrock-provider.d.ts.map +1 -0
- package/dist/bedrock-provider.js +6 -0
- package/dist/bedrock-provider.js.map +1 -0
- package/dist/bun-oauth.d.ts +3 -0
- package/dist/bun-oauth.d.ts.map +1 -0
- package/dist/bun-oauth.js +17 -0
- package/dist/bun-oauth.js.map +1 -0
- package/dist/cli.d.ts +3 -0
- package/dist/cli.d.ts.map +1 -0
- package/dist/cli.js +115 -0
- package/dist/cli.js.map +1 -0
- package/dist/compat/extension-oauth-types.d.ts +39 -0
- package/dist/compat/extension-oauth-types.d.ts.map +1 -0
- package/dist/compat/extension-oauth-types.js +2 -0
- package/dist/compat/extension-oauth-types.js.map +1 -0
- package/dist/compat.d.ts +70 -0
- package/dist/compat.d.ts.map +1 -0
- package/dist/compat.js +189 -0
- package/dist/compat.js.map +1 -0
- package/dist/env-api-keys.d.ts +18 -0
- package/dist/env-api-keys.d.ts.map +1 -0
- package/dist/env-api-keys.js +148 -0
- package/dist/env-api-keys.js.map +1 -0
- package/dist/image-models.d.ts +10 -0
- package/dist/image-models.d.ts.map +1 -0
- package/dist/image-models.js +24 -0
- package/dist/image-models.js.map +1 -0
- package/dist/images-api-registry.d.ts +14 -0
- package/dist/images-api-registry.d.ts.map +1 -0
- package/dist/images-api-registry.js +22 -0
- package/dist/images-api-registry.js.map +1 -0
- package/dist/images-models.d.ts +95 -0
- package/dist/images-models.d.ts.map +1 -0
- package/dist/images-models.js +142 -0
- package/dist/images-models.js.map +1 -0
- package/dist/images.d.ts +4 -0
- package/dist/images.d.ts.map +1 -0
- package/dist/images.js +14 -0
- package/dist/images.js.map +1 -0
- package/dist/index.d.ts +35 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +22 -0
- package/dist/index.js.map +1 -0
- package/dist/legacy-api-aliases.d.ts +42 -0
- package/dist/legacy-api-aliases.d.ts.map +1 -0
- package/dist/legacy-api-aliases.js +49 -0
- package/dist/legacy-api-aliases.js.map +1 -0
- package/dist/models-store.d.ts +27 -0
- package/dist/models-store.d.ts.map +1 -0
- package/dist/models-store.js +14 -0
- package/dist/models-store.js.map +1 -0
- package/dist/models.d.ts +178 -0
- package/dist/models.d.ts.map +1 -0
- package/dist/models.js +432 -0
- package/dist/models.js.map +1 -0
- package/dist/oauth.d.ts +3 -0
- package/dist/oauth.d.ts.map +1 -0
- package/dist/oauth.js +2 -0
- package/dist/oauth.js.map +1 -0
- package/dist/providers/all.d.ts +6 -0
- package/dist/providers/all.d.ts.map +1 -0
- package/dist/providers/all.js +9 -0
- package/dist/providers/all.js.map +1 -0
- package/dist/providers/anthropic-compat.d.ts +10 -0
- package/dist/providers/anthropic-compat.d.ts.map +1 -0
- package/dist/providers/anthropic-compat.js +20 -0
- package/dist/providers/anthropic-compat.js.map +1 -0
- package/dist/providers/faux.d.ts +97 -0
- package/dist/providers/faux.d.ts.map +1 -0
- package/dist/providers/faux.js +395 -0
- package/dist/providers/faux.js.map +1 -0
- package/dist/providers/images/register-builtins.d.ts +4 -0
- package/dist/providers/images/register-builtins.d.ts.map +1 -0
- package/dist/providers/images/register-builtins.js +34 -0
- package/dist/providers/images/register-builtins.js.map +1 -0
- package/dist/providers/openai-compat.d.ts +10 -0
- package/dist/providers/openai-compat.d.ts.map +1 -0
- package/dist/providers/openai-compat.js +24 -0
- package/dist/providers/openai-compat.js.map +1 -0
- package/dist/providers/openrouter-images.d.ts +3 -0
- package/dist/providers/openrouter-images.d.ts.map +1 -0
- package/dist/providers/openrouter-images.js +15 -0
- package/dist/providers/openrouter-images.js.map +1 -0
- package/dist/providers/radius.d.ts +9 -0
- package/dist/providers/radius.d.ts.map +1 -0
- package/dist/providers/radius.js +70 -0
- package/dist/providers/radius.js.map +1 -0
- package/dist/session-resources.d.ts +4 -0
- package/dist/session-resources.d.ts.map +1 -0
- package/dist/session-resources.js +22 -0
- package/dist/session-resources.js.map +1 -0
- package/dist/types.d.ts +629 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +2 -0
- package/dist/types.js.map +1 -0
- package/dist/utils/abort-signals.d.ts +6 -0
- package/dist/utils/abort-signals.d.ts.map +1 -0
- package/dist/utils/abort-signals.js +34 -0
- package/dist/utils/abort-signals.js.map +1 -0
- package/dist/utils/deferred-tools.d.ts +9 -0
- package/dist/utils/deferred-tools.d.ts.map +1 -0
- package/dist/utils/deferred-tools.js +36 -0
- package/dist/utils/deferred-tools.js.map +1 -0
- package/dist/utils/diagnostics.d.ts +19 -0
- package/dist/utils/diagnostics.d.ts.map +1 -0
- package/dist/utils/diagnostics.js +25 -0
- package/dist/utils/diagnostics.js.map +1 -0
- package/dist/utils/error-body.d.ts +25 -0
- package/dist/utils/error-body.d.ts.map +1 -0
- package/dist/utils/error-body.js +109 -0
- package/dist/utils/error-body.js.map +1 -0
- package/dist/utils/estimate.d.ts +17 -0
- package/dist/utils/estimate.d.ts.map +1 -0
- package/dist/utils/estimate.js +116 -0
- package/dist/utils/estimate.js.map +1 -0
- package/dist/utils/event-stream.d.ts +21 -0
- package/dist/utils/event-stream.d.ts.map +1 -0
- package/dist/utils/event-stream.js +81 -0
- package/dist/utils/event-stream.js.map +1 -0
- package/dist/utils/hash.d.ts +3 -0
- package/dist/utils/hash.d.ts.map +1 -0
- package/dist/utils/hash.js +14 -0
- package/dist/utils/hash.js.map +1 -0
- package/dist/utils/headers.d.ts +4 -0
- package/dist/utils/headers.d.ts.map +1 -0
- package/dist/utils/headers.js +18 -0
- package/dist/utils/headers.js.map +1 -0
- package/dist/utils/json-parse.d.ts +16 -0
- package/dist/utils/json-parse.d.ts.map +1 -0
- package/dist/utils/json-parse.js +113 -0
- package/dist/utils/json-parse.js.map +1 -0
- package/dist/utils/node-http-proxy.d.ts +4 -0
- package/dist/utils/node-http-proxy.d.ts.map +1 -0
- package/dist/utils/node-http-proxy.js +92 -0
- package/dist/utils/node-http-proxy.js.map +1 -0
- package/dist/utils/overflow.d.ts +59 -0
- package/dist/utils/overflow.d.ts.map +1 -0
- package/dist/utils/overflow.js +161 -0
- package/dist/utils/overflow.js.map +1 -0
- package/dist/utils/provider-env.d.ts +7 -0
- package/dist/utils/provider-env.d.ts.map +1 -0
- package/dist/utils/provider-env.js +44 -0
- package/dist/utils/provider-env.js.map +1 -0
- package/dist/utils/retry.d.ts +53 -0
- package/dist/utils/retry.d.ts.map +1 -0
- package/dist/utils/retry.js +170 -0
- package/dist/utils/retry.js.map +1 -0
- package/dist/utils/sanitize-unicode.d.ts +22 -0
- package/dist/utils/sanitize-unicode.d.ts.map +1 -0
- package/dist/utils/sanitize-unicode.js +26 -0
- package/dist/utils/sanitize-unicode.js.map +1 -0
- package/dist/utils/text.d.ts +6 -0
- package/dist/utils/text.d.ts.map +1 -0
- package/dist/utils/text.js +10 -0
- package/dist/utils/text.js.map +1 -0
- package/dist/utils/typebox-helpers.d.ts +17 -0
- package/dist/utils/typebox-helpers.d.ts.map +1 -0
- package/dist/utils/typebox-helpers.js +21 -0
- package/dist/utils/typebox-helpers.js.map +1 -0
- package/dist/utils/uuid.d.ts +3 -0
- package/dist/utils/uuid.d.ts.map +1 -0
- package/dist/utils/uuid.js +46 -0
- package/dist/utils/uuid.js.map +1 -0
- package/dist/utils/validation.d.ts +18 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +269 -0
- package/dist/utils/validation.js.map +1 -0
- package/package.json +95 -0
|
@@ -0,0 +1,456 @@
|
|
|
1
|
+
import { GoogleGenAI, ResourceScope, ThinkingLevel, } from "@google/genai";
|
|
2
|
+
import { calculateCost, clampThinkingLevel } from "../models.js";
|
|
3
|
+
import { formatProviderError, normalizeProviderError } from "../utils/error-body.js";
|
|
4
|
+
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
5
|
+
import { providerHeadersToRecord } from "../utils/headers.js";
|
|
6
|
+
import { getProviderEnvValue } from "../utils/provider-env.js";
|
|
7
|
+
import { sanitizeSurrogates } from "../utils/sanitize-unicode.js";
|
|
8
|
+
import { convertMessages, convertTools, isThinkingPart, mapStopReason, mapToolChoice, retainThoughtSignature, } from "./google-shared.js";
|
|
9
|
+
import { buildBaseOptions } from "./simple-options.js";
|
|
10
|
+
const API_VERSION = "v1";
|
|
11
|
+
const GCP_VERTEX_CREDENTIALS_MARKER = "gcp-vertex-credentials";
|
|
12
|
+
const THINKING_LEVEL_MAP = {
|
|
13
|
+
THINKING_LEVEL_UNSPECIFIED: ThinkingLevel.THINKING_LEVEL_UNSPECIFIED,
|
|
14
|
+
MINIMAL: ThinkingLevel.MINIMAL,
|
|
15
|
+
LOW: ThinkingLevel.LOW,
|
|
16
|
+
MEDIUM: ThinkingLevel.MEDIUM,
|
|
17
|
+
HIGH: ThinkingLevel.HIGH,
|
|
18
|
+
};
|
|
19
|
+
// Counter for generating unique tool call IDs
|
|
20
|
+
let toolCallCounter = 0;
|
|
21
|
+
export const stream = (model, context, options) => {
|
|
22
|
+
const stream = new AssistantMessageEventStream();
|
|
23
|
+
(async () => {
|
|
24
|
+
const output = {
|
|
25
|
+
role: "assistant",
|
|
26
|
+
content: [],
|
|
27
|
+
api: "google-vertex",
|
|
28
|
+
provider: model.provider,
|
|
29
|
+
model: model.id,
|
|
30
|
+
usage: {
|
|
31
|
+
input: 0,
|
|
32
|
+
output: 0,
|
|
33
|
+
cacheRead: 0,
|
|
34
|
+
cacheWrite: 0,
|
|
35
|
+
totalTokens: 0,
|
|
36
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
37
|
+
},
|
|
38
|
+
stopReason: "stop",
|
|
39
|
+
timestamp: Date.now(),
|
|
40
|
+
};
|
|
41
|
+
try {
|
|
42
|
+
const apiKey = resolveApiKey(options);
|
|
43
|
+
// Create the client using either a Vertex API key, if provided, or ADC with project and location
|
|
44
|
+
const client = apiKey
|
|
45
|
+
? createClientWithApiKey(model, apiKey, options?.headers)
|
|
46
|
+
: createClient(model, resolveProject(options), resolveLocation(options), options?.headers, options?.env);
|
|
47
|
+
let params = buildParams(model, context, options);
|
|
48
|
+
const nextParams = await options?.onPayload?.(params, model);
|
|
49
|
+
if (nextParams !== undefined) {
|
|
50
|
+
params = nextParams;
|
|
51
|
+
}
|
|
52
|
+
const googleStream = await client.models.generateContentStream(params);
|
|
53
|
+
stream.push({ type: "start", partial: output });
|
|
54
|
+
let currentBlock = null;
|
|
55
|
+
const blocks = output.content;
|
|
56
|
+
const blockIndex = () => blocks.length - 1;
|
|
57
|
+
for await (const chunk of googleStream) {
|
|
58
|
+
// Vertex uses the same @google/genai GenerateContentResponse type as Gemini.
|
|
59
|
+
// responseId is documented there as an output-only identifier for each response.
|
|
60
|
+
output.responseId ||= chunk.responseId;
|
|
61
|
+
const candidate = chunk.candidates?.[0];
|
|
62
|
+
if (candidate?.content?.parts) {
|
|
63
|
+
for (const part of candidate.content.parts) {
|
|
64
|
+
if (part.text !== undefined) {
|
|
65
|
+
const isThinking = isThinkingPart(part);
|
|
66
|
+
if (!currentBlock ||
|
|
67
|
+
(isThinking && currentBlock.type !== "thinking") ||
|
|
68
|
+
(!isThinking && currentBlock.type !== "text")) {
|
|
69
|
+
if (currentBlock) {
|
|
70
|
+
if (currentBlock.type === "text") {
|
|
71
|
+
stream.push({
|
|
72
|
+
type: "text_end",
|
|
73
|
+
contentIndex: blocks.length - 1,
|
|
74
|
+
content: currentBlock.text,
|
|
75
|
+
partial: output,
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
else {
|
|
79
|
+
stream.push({
|
|
80
|
+
type: "thinking_end",
|
|
81
|
+
contentIndex: blockIndex(),
|
|
82
|
+
content: currentBlock.thinking,
|
|
83
|
+
partial: output,
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
if (isThinking) {
|
|
88
|
+
currentBlock = { type: "thinking", thinking: "", thinkingSignature: undefined };
|
|
89
|
+
output.content.push(currentBlock);
|
|
90
|
+
stream.push({ type: "thinking_start", contentIndex: blockIndex(), partial: output });
|
|
91
|
+
}
|
|
92
|
+
else {
|
|
93
|
+
currentBlock = { type: "text", text: "" };
|
|
94
|
+
output.content.push(currentBlock);
|
|
95
|
+
stream.push({ type: "text_start", contentIndex: blockIndex(), partial: output });
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
if (currentBlock.type === "thinking") {
|
|
99
|
+
currentBlock.thinking += part.text;
|
|
100
|
+
currentBlock.thinkingSignature = retainThoughtSignature(currentBlock.thinkingSignature, part.thoughtSignature);
|
|
101
|
+
stream.push({
|
|
102
|
+
type: "thinking_delta",
|
|
103
|
+
contentIndex: blockIndex(),
|
|
104
|
+
delta: part.text,
|
|
105
|
+
partial: output,
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
currentBlock.text += part.text;
|
|
110
|
+
currentBlock.textSignature = retainThoughtSignature(currentBlock.textSignature, part.thoughtSignature);
|
|
111
|
+
stream.push({
|
|
112
|
+
type: "text_delta",
|
|
113
|
+
contentIndex: blockIndex(),
|
|
114
|
+
delta: part.text,
|
|
115
|
+
partial: output,
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
if (part.functionCall) {
|
|
120
|
+
if (currentBlock) {
|
|
121
|
+
if (currentBlock.type === "text") {
|
|
122
|
+
stream.push({
|
|
123
|
+
type: "text_end",
|
|
124
|
+
contentIndex: blockIndex(),
|
|
125
|
+
content: currentBlock.text,
|
|
126
|
+
partial: output,
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
stream.push({
|
|
131
|
+
type: "thinking_end",
|
|
132
|
+
contentIndex: blockIndex(),
|
|
133
|
+
content: currentBlock.thinking,
|
|
134
|
+
partial: output,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
currentBlock = null;
|
|
138
|
+
}
|
|
139
|
+
const providedId = part.functionCall.id;
|
|
140
|
+
const needsNewId = !providedId || output.content.some((b) => b.type === "toolCall" && b.id === providedId);
|
|
141
|
+
const toolCallId = needsNewId
|
|
142
|
+
? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}`
|
|
143
|
+
: providedId;
|
|
144
|
+
const toolCall = {
|
|
145
|
+
type: "toolCall",
|
|
146
|
+
id: toolCallId,
|
|
147
|
+
name: part.functionCall.name || "",
|
|
148
|
+
arguments: part.functionCall.args ?? {},
|
|
149
|
+
...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),
|
|
150
|
+
};
|
|
151
|
+
output.content.push(toolCall);
|
|
152
|
+
stream.push({ type: "toolcall_start", contentIndex: blockIndex(), partial: output });
|
|
153
|
+
stream.push({
|
|
154
|
+
type: "toolcall_delta",
|
|
155
|
+
contentIndex: blockIndex(),
|
|
156
|
+
delta: JSON.stringify(toolCall.arguments),
|
|
157
|
+
partial: output,
|
|
158
|
+
});
|
|
159
|
+
stream.push({ type: "toolcall_end", contentIndex: blockIndex(), toolCall, partial: output });
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
if (candidate?.finishReason) {
|
|
164
|
+
output.stopReason = mapStopReason(candidate.finishReason);
|
|
165
|
+
if (output.content.some((b) => b.type === "toolCall")) {
|
|
166
|
+
output.stopReason = "toolUse";
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
if (chunk.usageMetadata) {
|
|
170
|
+
output.usage = {
|
|
171
|
+
input: (chunk.usageMetadata.promptTokenCount || 0) - (chunk.usageMetadata.cachedContentTokenCount || 0),
|
|
172
|
+
output: (chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),
|
|
173
|
+
cacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,
|
|
174
|
+
cacheWrite: 0,
|
|
175
|
+
reasoning: chunk.usageMetadata.thoughtsTokenCount || 0,
|
|
176
|
+
totalTokens: chunk.usageMetadata.totalTokenCount || 0,
|
|
177
|
+
cost: {
|
|
178
|
+
input: 0,
|
|
179
|
+
output: 0,
|
|
180
|
+
cacheRead: 0,
|
|
181
|
+
cacheWrite: 0,
|
|
182
|
+
total: 0,
|
|
183
|
+
},
|
|
184
|
+
};
|
|
185
|
+
calculateCost(model, output.usage);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
if (currentBlock) {
|
|
189
|
+
if (currentBlock.type === "text") {
|
|
190
|
+
stream.push({
|
|
191
|
+
type: "text_end",
|
|
192
|
+
contentIndex: blockIndex(),
|
|
193
|
+
content: currentBlock.text,
|
|
194
|
+
partial: output,
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
else {
|
|
198
|
+
stream.push({
|
|
199
|
+
type: "thinking_end",
|
|
200
|
+
contentIndex: blockIndex(),
|
|
201
|
+
content: currentBlock.thinking,
|
|
202
|
+
partial: output,
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (options?.signal?.aborted) {
|
|
207
|
+
throw new Error("Request was aborted");
|
|
208
|
+
}
|
|
209
|
+
if (output.stopReason === "aborted" || output.stopReason === "error") {
|
|
210
|
+
throw new Error("An unknown error occurred");
|
|
211
|
+
}
|
|
212
|
+
stream.push({ type: "done", reason: output.stopReason, message: output });
|
|
213
|
+
stream.end();
|
|
214
|
+
}
|
|
215
|
+
catch (error) {
|
|
216
|
+
// Remove internal index property used during streaming
|
|
217
|
+
for (const block of output.content) {
|
|
218
|
+
if ("index" in block) {
|
|
219
|
+
delete block.index;
|
|
220
|
+
}
|
|
221
|
+
}
|
|
222
|
+
output.stopReason = options?.signal?.aborted ? "aborted" : "error";
|
|
223
|
+
output.errorMessage = formatProviderError(normalizeProviderError(error));
|
|
224
|
+
stream.push({ type: "error", reason: output.stopReason, error: output });
|
|
225
|
+
stream.end();
|
|
226
|
+
}
|
|
227
|
+
})();
|
|
228
|
+
return stream;
|
|
229
|
+
};
|
|
230
|
+
export const streamSimple = (model, context, options) => {
|
|
231
|
+
const base = buildBaseOptions(model, context, options, undefined);
|
|
232
|
+
if (!options?.reasoning) {
|
|
233
|
+
return stream(model, context, {
|
|
234
|
+
...base,
|
|
235
|
+
thinking: { enabled: false },
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
const clampedReasoning = clampThinkingLevel(model, options.reasoning);
|
|
239
|
+
const effort = (clampedReasoning === "off" ? "high" : clampedReasoning);
|
|
240
|
+
const geminiModel = model;
|
|
241
|
+
if (isGemini3ProModel(geminiModel) || isGemini3FlashModel(geminiModel)) {
|
|
242
|
+
return stream(model, context, {
|
|
243
|
+
...base,
|
|
244
|
+
thinking: {
|
|
245
|
+
enabled: true,
|
|
246
|
+
level: getGemini3ThinkingLevel(effort, geminiModel),
|
|
247
|
+
},
|
|
248
|
+
});
|
|
249
|
+
}
|
|
250
|
+
return stream(model, context, {
|
|
251
|
+
...base,
|
|
252
|
+
thinking: {
|
|
253
|
+
enabled: true,
|
|
254
|
+
budgetTokens: getGoogleBudget(geminiModel, effort, options.thinkingBudgets),
|
|
255
|
+
},
|
|
256
|
+
});
|
|
257
|
+
};
|
|
258
|
+
function createClient(model, project, location, optionsHeaders, env) {
|
|
259
|
+
const googleAuthOptions = buildGoogleAuthOptions(env);
|
|
260
|
+
return new GoogleGenAI({
|
|
261
|
+
vertexai: true,
|
|
262
|
+
project,
|
|
263
|
+
location,
|
|
264
|
+
apiVersion: API_VERSION,
|
|
265
|
+
...(googleAuthOptions ? { googleAuthOptions } : {}),
|
|
266
|
+
httpOptions: buildHttpOptions(model, optionsHeaders),
|
|
267
|
+
});
|
|
268
|
+
}
|
|
269
|
+
function createClientWithApiKey(model, apiKey, optionsHeaders) {
|
|
270
|
+
return new GoogleGenAI({
|
|
271
|
+
vertexai: true,
|
|
272
|
+
apiKey,
|
|
273
|
+
apiVersion: API_VERSION,
|
|
274
|
+
httpOptions: buildHttpOptions(model, optionsHeaders),
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
function buildHttpOptions(model, optionsHeaders) {
|
|
278
|
+
const httpOptions = {};
|
|
279
|
+
const baseUrl = resolveCustomBaseUrl(model.baseUrl);
|
|
280
|
+
if (baseUrl) {
|
|
281
|
+
httpOptions.baseUrl = baseUrl;
|
|
282
|
+
httpOptions.baseUrlResourceScope = ResourceScope.COLLECTION;
|
|
283
|
+
if (baseUrlIncludesApiVersion(baseUrl)) {
|
|
284
|
+
httpOptions.apiVersion = "";
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
const headers = providerHeadersToRecord({ ...model.headers, ...optionsHeaders });
|
|
288
|
+
if (headers) {
|
|
289
|
+
httpOptions.headers = headers;
|
|
290
|
+
}
|
|
291
|
+
return Object.keys(httpOptions).length > 0 ? httpOptions : undefined;
|
|
292
|
+
}
|
|
293
|
+
function resolveCustomBaseUrl(baseUrl) {
|
|
294
|
+
const trimmed = baseUrl.trim();
|
|
295
|
+
if (!trimmed || trimmed.includes("{location}")) {
|
|
296
|
+
return undefined;
|
|
297
|
+
}
|
|
298
|
+
return trimmed;
|
|
299
|
+
}
|
|
300
|
+
function baseUrlIncludesApiVersion(baseUrl) {
|
|
301
|
+
try {
|
|
302
|
+
const url = new URL(baseUrl);
|
|
303
|
+
return url.pathname.split("/").some((part) => /^v\d+(?:beta\d*)?$/.test(part));
|
|
304
|
+
}
|
|
305
|
+
catch {
|
|
306
|
+
return /(?:^|\/)v\d+(?:beta\d*)?(?:\/|$)/.test(baseUrl);
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
function buildGoogleAuthOptions(env) {
|
|
310
|
+
const keyFilename = getProviderEnvValue("GOOGLE_APPLICATION_CREDENTIALS", env);
|
|
311
|
+
return keyFilename ? { keyFilename } : undefined;
|
|
312
|
+
}
|
|
313
|
+
function resolveApiKey(options) {
|
|
314
|
+
const apiKey = options?.apiKey?.trim();
|
|
315
|
+
if (!apiKey || apiKey === GCP_VERTEX_CREDENTIALS_MARKER || isPlaceholderApiKey(apiKey)) {
|
|
316
|
+
return undefined;
|
|
317
|
+
}
|
|
318
|
+
return apiKey;
|
|
319
|
+
}
|
|
320
|
+
function isPlaceholderApiKey(apiKey) {
|
|
321
|
+
return /^<[^>]+>$/.test(apiKey);
|
|
322
|
+
}
|
|
323
|
+
function resolveProject(options) {
|
|
324
|
+
const project = options?.project ||
|
|
325
|
+
getProviderEnvValue("GOOGLE_CLOUD_PROJECT", options?.env) ||
|
|
326
|
+
getProviderEnvValue("GCLOUD_PROJECT", options?.env);
|
|
327
|
+
if (!project) {
|
|
328
|
+
throw new Error("Vertex AI requires a project ID. Set GOOGLE_CLOUD_PROJECT/GCLOUD_PROJECT or pass project in options.");
|
|
329
|
+
}
|
|
330
|
+
return project;
|
|
331
|
+
}
|
|
332
|
+
function resolveLocation(options) {
|
|
333
|
+
const location = options?.location || getProviderEnvValue("GOOGLE_CLOUD_LOCATION", options?.env);
|
|
334
|
+
if (!location) {
|
|
335
|
+
throw new Error("Vertex AI requires a location. Set GOOGLE_CLOUD_LOCATION or pass location in options.");
|
|
336
|
+
}
|
|
337
|
+
return location;
|
|
338
|
+
}
|
|
339
|
+
function buildParams(model, context, options = {}) {
|
|
340
|
+
const contents = convertMessages(model, context);
|
|
341
|
+
const generationConfig = {};
|
|
342
|
+
if (options.temperature !== undefined) {
|
|
343
|
+
generationConfig.temperature = options.temperature;
|
|
344
|
+
}
|
|
345
|
+
if (options.maxTokens !== undefined) {
|
|
346
|
+
generationConfig.maxOutputTokens = options.maxTokens;
|
|
347
|
+
}
|
|
348
|
+
const config = {
|
|
349
|
+
...(Object.keys(generationConfig).length > 0 && generationConfig),
|
|
350
|
+
...(context.systemPrompt && { systemInstruction: sanitizeSurrogates(context.systemPrompt) }),
|
|
351
|
+
...(context.tools && context.tools.length > 0 && { tools: convertTools(context.tools) }),
|
|
352
|
+
};
|
|
353
|
+
if (context.tools && context.tools.length > 0 && options.toolChoice) {
|
|
354
|
+
config.toolConfig = {
|
|
355
|
+
functionCallingConfig: {
|
|
356
|
+
mode: mapToolChoice(options.toolChoice),
|
|
357
|
+
},
|
|
358
|
+
};
|
|
359
|
+
}
|
|
360
|
+
else {
|
|
361
|
+
config.toolConfig = undefined;
|
|
362
|
+
}
|
|
363
|
+
if (options.thinking?.enabled && model.reasoning) {
|
|
364
|
+
const thinkingConfig = { includeThoughts: true };
|
|
365
|
+
if (options.thinking.level !== undefined) {
|
|
366
|
+
thinkingConfig.thinkingLevel = THINKING_LEVEL_MAP[options.thinking.level];
|
|
367
|
+
}
|
|
368
|
+
else if (options.thinking.budgetTokens !== undefined) {
|
|
369
|
+
thinkingConfig.thinkingBudget = options.thinking.budgetTokens;
|
|
370
|
+
}
|
|
371
|
+
config.thinkingConfig = thinkingConfig;
|
|
372
|
+
}
|
|
373
|
+
else if (model.reasoning && options.thinking && !options.thinking.enabled) {
|
|
374
|
+
config.thinkingConfig = getDisabledThinkingConfig(model);
|
|
375
|
+
}
|
|
376
|
+
if (options.signal) {
|
|
377
|
+
if (options.signal.aborted) {
|
|
378
|
+
throw new Error("Request aborted");
|
|
379
|
+
}
|
|
380
|
+
config.abortSignal = options.signal;
|
|
381
|
+
}
|
|
382
|
+
const params = {
|
|
383
|
+
model: model.id,
|
|
384
|
+
contents,
|
|
385
|
+
config,
|
|
386
|
+
};
|
|
387
|
+
return params;
|
|
388
|
+
}
|
|
389
|
+
function isGemini3ProModel(model) {
|
|
390
|
+
return /gemini-3(?:\.\d+)?-pro/.test(model.id.toLowerCase());
|
|
391
|
+
}
|
|
392
|
+
function isGemini3FlashModel(model) {
|
|
393
|
+
const id = model.id.toLowerCase();
|
|
394
|
+
return /gemini-3(?:\.\d+)?-flash/.test(id) || id === "gemini-flash-latest" || id === "gemini-flash-lite-latest";
|
|
395
|
+
}
|
|
396
|
+
function getDisabledThinkingConfig(model) {
|
|
397
|
+
// Google docs: Gemini 3.1 Pro cannot disable thinking, and Gemini 3 Flash / Flash-Lite
|
|
398
|
+
// do not support full thinking-off either. For Gemini 3 models, use the lowest supported
|
|
399
|
+
// thinkingLevel without includeThoughts so hidden thinking remains invisible to pi.
|
|
400
|
+
const geminiModel = model;
|
|
401
|
+
if (isGemini3ProModel(geminiModel)) {
|
|
402
|
+
return { thinkingLevel: ThinkingLevel.LOW };
|
|
403
|
+
}
|
|
404
|
+
if (isGemini3FlashModel(geminiModel)) {
|
|
405
|
+
return { thinkingLevel: ThinkingLevel.MINIMAL };
|
|
406
|
+
}
|
|
407
|
+
// Gemini 2.x supports disabling via thinkingBudget = 0.
|
|
408
|
+
return { thinkingBudget: 0 };
|
|
409
|
+
}
|
|
410
|
+
function getGemini3ThinkingLevel(effort, model) {
|
|
411
|
+
if (isGemini3ProModel(model)) {
|
|
412
|
+
switch (effort) {
|
|
413
|
+
case "minimal":
|
|
414
|
+
case "low":
|
|
415
|
+
return "LOW";
|
|
416
|
+
case "medium":
|
|
417
|
+
case "high":
|
|
418
|
+
return "HIGH";
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
switch (effort) {
|
|
422
|
+
case "minimal":
|
|
423
|
+
return "MINIMAL";
|
|
424
|
+
case "low":
|
|
425
|
+
return "LOW";
|
|
426
|
+
case "medium":
|
|
427
|
+
return "MEDIUM";
|
|
428
|
+
case "high":
|
|
429
|
+
return "HIGH";
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
function getGoogleBudget(model, effort, customBudgets) {
|
|
433
|
+
if (customBudgets?.[effort] !== undefined) {
|
|
434
|
+
return customBudgets[effort];
|
|
435
|
+
}
|
|
436
|
+
if (model.id.includes("2.5-pro")) {
|
|
437
|
+
const budgets = {
|
|
438
|
+
minimal: 128,
|
|
439
|
+
low: 2048,
|
|
440
|
+
medium: 8192,
|
|
441
|
+
high: 32768,
|
|
442
|
+
};
|
|
443
|
+
return budgets[effort];
|
|
444
|
+
}
|
|
445
|
+
if (model.id.includes("2.5-flash")) {
|
|
446
|
+
const budgets = {
|
|
447
|
+
minimal: 128,
|
|
448
|
+
low: 2048,
|
|
449
|
+
medium: 8192,
|
|
450
|
+
high: 24576,
|
|
451
|
+
};
|
|
452
|
+
return budgets[effort];
|
|
453
|
+
}
|
|
454
|
+
return -1;
|
|
455
|
+
}
|
|
456
|
+
//# sourceMappingURL=google-vertex.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google-vertex.js","sourceRoot":"","sources":["../../src/api/google-vertex.ts"],"names":[],"mappings":"AAAA,OAAO,EAGN,WAAW,EAEX,aAAa,EAEb,aAAa,GACb,MAAM,eAAe,CAAC;AACvB,OAAO,EAAE,aAAa,EAAE,kBAAkB,EAAE,MAAM,cAAc,CAAC;AAiBjE,OAAO,EAAE,mBAAmB,EAAE,sBAAsB,EAAE,MAAM,wBAAwB,CAAC;AACrF,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AACvE,OAAO,EAAE,uBAAuB,EAAE,MAAM,qBAAqB,CAAC;AAC9D,OAAO,EAAE,mBAAmB,EAAE,MAAM,0BAA0B,CAAC;AAC/D,OAAO,EAAE,kBAAkB,EAAE,MAAM,8BAA8B,CAAC;AAElE,OAAO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AC,MAAM,CAAC,KAAK,SAAS,EAAE,CAAC;QAC3C,OAAO,aAAa,CAAC,MAAM,CAAE,CAAC;IAC/B,CAAC;IAED,IAAI,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,SAAS,CAAC,EAAE,CAAC;QAClC,MAAM,OAAO,GAAyC;YACrD,OAAO,EAAE,GAAG;YACZ,GAAG,EAAE,IAAI;YACT,MAAM,EAAE,IAAI;YACZ,IAAI,EAAE,KAAK;SACX,CAAC;QACF,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;IACxB,CAAC;IAED,IAAI,KAAK,CAAC,EAAE,CAAC,QAAQ,CAAC,WAAW,CAAC,EAAE,CAAC;QACpC,MAAM,OAAO,GAAyC;YACrD,OAAO,EAAE,GAAG;YACZ,GAAG,EAAE,IAAI;YACT,MAAM,EAAE,IAAI;YACZ,IAAI,EAAE,KAAK;SACX,CAAC;QACF,OAAO,OAAO,CAAC,MAAM,CAAC,CAAC;IACxB,CAAC;IAED,OAAO,CAAC,CAAC,CAAC;AAAA,CACV","sourcesContent":["import {\n\ttype GenerateContentConfig,\n\ttype GenerateContentParameters,\n\tGoogleGenAI,\n\ttype HttpOptions,\n\tResourceScope,\n\ttype ThinkingConfig,\n\tThinkingLevel,\n} from \"@google/genai\";\nimport { calculateCost, clampThinkingLevel } from \"../models.ts\";\nimport type {\n\tApi,\n\tAssistantMessage,\n\tContext,\n\tModel,\n\tThinkingLevel as PiThinkingLevel,\n\tProviderEnv,\n\tProviderHeaders,\n\tSimpleStreamOptions,\n\tStreamFunction,\n\tStreamOptions,\n\tTextContent,\n\tThinkingBudgets,\n\tThinkingContent,\n\tToolCall,\n} from \"../types.ts\";\nimport { formatProviderError, normalizeProviderError } from \"../utils/error-body.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\nimport { providerHeadersToRecord } from \"../utils/headers.ts\";\nimport { getProviderEnvValue } from \"../utils/provider-env.ts\";\nimport { sanitizeSurrogates } from \"../utils/sanitize-unicode.ts\";\nimport type { GoogleThinkingLevel } from \"./google-shared.ts\";\nimport {\n\tconvertMessages,\n\tconvertTools,\n\tisThinkingPart,\n\tmapStopReason,\n\tmapToolChoice,\n\tretainThoughtSignature,\n} from \"./google-shared.ts\";\nimport { buildBaseOptions } from \"./simple-options.ts\";\n\nexport interface GoogleVertexOptions extends StreamOptions {\n\ttoolChoice?: \"auto\" | \"none\" | \"any\";\n\tthinking?: {\n\t\tenabled: boolean;\n\t\tbudgetTokens?: number; // -1 for dynamic, 0 to disable\n\t\tlevel?: GoogleThinkingLevel;\n\t};\n\tproject?: string;\n\tlocation?: string;\n}\n\nconst API_VERSION = \"v1\";\nconst GCP_VERTEX_CREDENTIALS_MARKER = \"gcp-vertex-credentials\";\n\nconst THINKING_LEVEL_MAP: Record<GoogleThinkingLevel, ThinkingLevel> = {\n\tTHINKING_LEVEL_UNSPECIFIED: ThinkingLevel.THINKING_LEVEL_UNSPECIFIED,\n\tMINIMAL: ThinkingLevel.MINIMAL,\n\tLOW: ThinkingLevel.LOW,\n\tMEDIUM: ThinkingLevel.MEDIUM,\n\tHIGH: ThinkingLevel.HIGH,\n};\n\n// Counter for generating unique tool call IDs\nlet toolCallCounter = 0;\n\nexport const stream: StreamFunction<\"google-vertex\", GoogleVertexOptions> = (\n\tmodel: Model<\"google-vertex\">,\n\tcontext: Context,\n\toptions?: GoogleVertexOptions,\n): AssistantMessageEventStream => {\n\tconst stream = new AssistantMessageEventStream();\n\n\t(async () => {\n\t\tconst output: AssistantMessage = {\n\t\t\trole: \"assistant\",\n\t\t\tcontent: [],\n\t\t\tapi: \"google-vertex\" as Api,\n\t\t\tprovider: model.provider,\n\t\t\tmodel: model.id,\n\t\t\tusage: {\n\t\t\t\tinput: 0,\n\t\t\t\toutput: 0,\n\t\t\t\tcacheRead: 0,\n\t\t\t\tcacheWrite: 0,\n\t\t\t\ttotalTokens: 0,\n\t\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t\t},\n\t\t\tstopReason: \"stop\",\n\t\t\ttimestamp: Date.now(),\n\t\t};\n\n\t\ttry {\n\t\t\tconst apiKey = resolveApiKey(options);\n\t\t\t// Create the client using either a Vertex API key, if provided, or ADC with project and location\n\t\t\tconst client = apiKey\n\t\t\t\t? createClientWithApiKey(model, apiKey, options?.headers)\n\t\t\t\t: createClient(model, resolveProject(options), resolveLocation(options), options?.headers, options?.env);\n\t\t\tlet params = buildParams(model, context, options);\n\t\t\tconst nextParams = await options?.onPayload?.(params, model);\n\t\t\tif (nextParams !== undefined) {\n\t\t\t\tparams = nextParams as GenerateContentParameters;\n\t\t\t}\n\t\t\tconst googleStream = await client.models.generateContentStream(params);\n\n\t\t\tstream.push({ type: \"start\", partial: output });\n\t\t\tlet currentBlock: TextContent | ThinkingContent | null = null;\n\t\t\tconst blocks = output.content;\n\t\t\tconst blockIndex = () => blocks.length - 1;\n\t\t\tfor await (const chunk of googleStream) {\n\t\t\t\t// Vertex uses the same @google/genai GenerateContentResponse type as Gemini.\n\t\t\t\t// responseId is documented there as an output-only identifier for each response.\n\t\t\t\toutput.responseId ||= chunk.responseId;\n\t\t\t\tconst candidate = chunk.candidates?.[0];\n\t\t\t\tif (candidate?.content?.parts) {\n\t\t\t\t\tfor (const part of candidate.content.parts) {\n\t\t\t\t\t\tif (part.text !== undefined) {\n\t\t\t\t\t\t\tconst isThinking = isThinkingPart(part);\n\t\t\t\t\t\t\tif (\n\t\t\t\t\t\t\t\t!currentBlock ||\n\t\t\t\t\t\t\t\t(isThinking && currentBlock.type !== \"thinking\") ||\n\t\t\t\t\t\t\t\t(!isThinking && currentBlock.type !== \"text\")\n\t\t\t\t\t\t\t) {\n\t\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\t\t\t\t\t\t\tcontentIndex: blocks.length - 1,\n\t\t\t\t\t\t\t\t\t\t\tcontent: currentBlock.text,\n\t\t\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\t\t\tcontent: currentBlock.thinking,\n\t\t\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tif (isThinking) {\n\t\t\t\t\t\t\t\t\tcurrentBlock = { type: \"thinking\", thinking: \"\", thinkingSignature: undefined };\n\t\t\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\t\t\tstream.push({ type: \"thinking_start\", contentIndex: blockIndex(), partial: output });\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tcurrentBlock = { type: \"text\", text: \"\" };\n\t\t\t\t\t\t\t\t\toutput.content.push(currentBlock);\n\t\t\t\t\t\t\t\t\tstream.push({ type: \"text_start\", contentIndex: blockIndex(), partial: output });\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\tif (currentBlock.type === \"thinking\") {\n\t\t\t\t\t\t\t\tcurrentBlock.thinking += part.text;\n\t\t\t\t\t\t\t\tcurrentBlock.thinkingSignature = retainThoughtSignature(\n\t\t\t\t\t\t\t\t\tcurrentBlock.thinkingSignature,\n\t\t\t\t\t\t\t\t\tpart.thoughtSignature,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\ttype: \"thinking_delta\",\n\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\tdelta: part.text,\n\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\tcurrentBlock.text += part.text;\n\t\t\t\t\t\t\t\tcurrentBlock.textSignature = retainThoughtSignature(\n\t\t\t\t\t\t\t\t\tcurrentBlock.textSignature,\n\t\t\t\t\t\t\t\t\tpart.thoughtSignature,\n\t\t\t\t\t\t\t\t);\n\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\ttype: \"text_delta\",\n\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\tdelta: part.text,\n\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t}\n\t\t\t\t\t\t}\n\n\t\t\t\t\t\tif (part.functionCall) {\n\t\t\t\t\t\t\tif (currentBlock) {\n\t\t\t\t\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\t\tcontent: currentBlock.text,\n\t\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t} else {\n\t\t\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\t\t\tcontent: currentBlock.thinking,\n\t\t\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\t\t}\n\t\t\t\t\t\t\t\tcurrentBlock = null;\n\t\t\t\t\t\t\t}\n\n\t\t\t\t\t\t\tconst providedId = part.functionCall.id;\n\t\t\t\t\t\t\tconst needsNewId =\n\t\t\t\t\t\t\t\t!providedId || output.content.some((b) => b.type === \"toolCall\" && b.id === providedId);\n\t\t\t\t\t\t\tconst toolCallId = needsNewId\n\t\t\t\t\t\t\t\t? `${part.functionCall.name}_${Date.now()}_${++toolCallCounter}`\n\t\t\t\t\t\t\t\t: providedId;\n\n\t\t\t\t\t\t\tconst toolCall: ToolCall = {\n\t\t\t\t\t\t\t\ttype: \"toolCall\",\n\t\t\t\t\t\t\t\tid: toolCallId,\n\t\t\t\t\t\t\t\tname: part.functionCall.name || \"\",\n\t\t\t\t\t\t\t\targuments: (part.functionCall.args as Record<string, any>) ?? {},\n\t\t\t\t\t\t\t\t...(part.thoughtSignature && { thoughtSignature: part.thoughtSignature }),\n\t\t\t\t\t\t\t};\n\n\t\t\t\t\t\t\toutput.content.push(toolCall);\n\t\t\t\t\t\t\tstream.push({ type: \"toolcall_start\", contentIndex: blockIndex(), partial: output });\n\t\t\t\t\t\t\tstream.push({\n\t\t\t\t\t\t\t\ttype: \"toolcall_delta\",\n\t\t\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\t\t\tdelta: JSON.stringify(toolCall.arguments),\n\t\t\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t\t\t});\n\t\t\t\t\t\t\tstream.push({ type: \"toolcall_end\", contentIndex: blockIndex(), toolCall, partial: output });\n\t\t\t\t\t\t}\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (candidate?.finishReason) {\n\t\t\t\t\toutput.stopReason = mapStopReason(candidate.finishReason);\n\t\t\t\t\tif (output.content.some((b) => b.type === \"toolCall\")) {\n\t\t\t\t\t\toutput.stopReason = \"toolUse\";\n\t\t\t\t\t}\n\t\t\t\t}\n\n\t\t\t\tif (chunk.usageMetadata) {\n\t\t\t\t\toutput.usage = {\n\t\t\t\t\t\tinput:\n\t\t\t\t\t\t\t(chunk.usageMetadata.promptTokenCount || 0) - (chunk.usageMetadata.cachedContentTokenCount || 0),\n\t\t\t\t\t\toutput:\n\t\t\t\t\t\t\t(chunk.usageMetadata.candidatesTokenCount || 0) + (chunk.usageMetadata.thoughtsTokenCount || 0),\n\t\t\t\t\t\tcacheRead: chunk.usageMetadata.cachedContentTokenCount || 0,\n\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\treasoning: chunk.usageMetadata.thoughtsTokenCount || 0,\n\t\t\t\t\t\ttotalTokens: chunk.usageMetadata.totalTokenCount || 0,\n\t\t\t\t\t\tcost: {\n\t\t\t\t\t\t\tinput: 0,\n\t\t\t\t\t\t\toutput: 0,\n\t\t\t\t\t\t\tcacheRead: 0,\n\t\t\t\t\t\t\tcacheWrite: 0,\n\t\t\t\t\t\t\ttotal: 0,\n\t\t\t\t\t\t},\n\t\t\t\t\t};\n\t\t\t\t\tcalculateCost(model, output.usage);\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (currentBlock) {\n\t\t\t\tif (currentBlock.type === \"text\") {\n\t\t\t\t\tstream.push({\n\t\t\t\t\t\ttype: \"text_end\",\n\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\tcontent: currentBlock.text,\n\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t});\n\t\t\t\t} else {\n\t\t\t\t\tstream.push({\n\t\t\t\t\t\ttype: \"thinking_end\",\n\t\t\t\t\t\tcontentIndex: blockIndex(),\n\t\t\t\t\t\tcontent: currentBlock.thinking,\n\t\t\t\t\t\tpartial: output,\n\t\t\t\t\t});\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (options?.signal?.aborted) {\n\t\t\t\tthrow new Error(\"Request was aborted\");\n\t\t\t}\n\n\t\t\tif (output.stopReason === \"aborted\" || output.stopReason === \"error\") {\n\t\t\t\tthrow new Error(\"An unknown error occurred\");\n\t\t\t}\n\n\t\t\tstream.push({ type: \"done\", reason: output.stopReason, message: output });\n\t\t\tstream.end();\n\t\t} catch (error) {\n\t\t\t// Remove internal index property used during streaming\n\t\t\tfor (const block of output.content) {\n\t\t\t\tif (\"index\" in block) {\n\t\t\t\t\tdelete (block as { index?: number }).index;\n\t\t\t\t}\n\t\t\t}\n\t\t\toutput.stopReason = options?.signal?.aborted ? \"aborted\" : \"error\";\n\t\t\toutput.errorMessage = formatProviderError(normalizeProviderError(error));\n\t\t\tstream.push({ type: \"error\", reason: output.stopReason, error: output });\n\t\t\tstream.end();\n\t\t}\n\t})();\n\n\treturn stream;\n};\n\nexport const streamSimple: StreamFunction<\"google-vertex\", SimpleStreamOptions> = (\n\tmodel: Model<\"google-vertex\">,\n\tcontext: Context,\n\toptions?: SimpleStreamOptions,\n): AssistantMessageEventStream => {\n\tconst base = buildBaseOptions(model, context, options, undefined);\n\tif (!options?.reasoning) {\n\t\treturn stream(model, context, {\n\t\t\t...base,\n\t\t\tthinking: { enabled: false },\n\t\t} satisfies GoogleVertexOptions);\n\t}\n\n\tconst clampedReasoning = clampThinkingLevel(model, options.reasoning);\n\tconst effort = (clampedReasoning === \"off\" ? \"high\" : clampedReasoning) as ClampedThinkingLevel;\n\tconst geminiModel = model as unknown as Model<\"google-generative-ai\">;\n\n\tif (isGemini3ProModel(geminiModel) || isGemini3FlashModel(geminiModel)) {\n\t\treturn stream(model, context, {\n\t\t\t...base,\n\t\t\tthinking: {\n\t\t\t\tenabled: true,\n\t\t\t\tlevel: getGemini3ThinkingLevel(effort, geminiModel),\n\t\t\t},\n\t\t} satisfies GoogleVertexOptions);\n\t}\n\n\treturn stream(model, context, {\n\t\t...base,\n\t\tthinking: {\n\t\t\tenabled: true,\n\t\t\tbudgetTokens: getGoogleBudget(geminiModel, effort, options.thinkingBudgets),\n\t\t},\n\t} satisfies GoogleVertexOptions);\n};\n\nfunction createClient(\n\tmodel: Model<\"google-vertex\">,\n\tproject: string,\n\tlocation: string,\n\toptionsHeaders?: ProviderHeaders,\n\tenv?: ProviderEnv,\n): GoogleGenAI {\n\tconst googleAuthOptions = buildGoogleAuthOptions(env);\n\treturn new GoogleGenAI({\n\t\tvertexai: true,\n\t\tproject,\n\t\tlocation,\n\t\tapiVersion: API_VERSION,\n\t\t...(googleAuthOptions ? { googleAuthOptions } : {}),\n\t\thttpOptions: buildHttpOptions(model, optionsHeaders),\n\t});\n}\n\nfunction createClientWithApiKey(\n\tmodel: Model<\"google-vertex\">,\n\tapiKey: string,\n\toptionsHeaders?: ProviderHeaders,\n): GoogleGenAI {\n\treturn new GoogleGenAI({\n\t\tvertexai: true,\n\t\tapiKey,\n\t\tapiVersion: API_VERSION,\n\t\thttpOptions: buildHttpOptions(model, optionsHeaders),\n\t});\n}\n\nfunction buildHttpOptions(model: Model<\"google-vertex\">, optionsHeaders?: ProviderHeaders): HttpOptions | undefined {\n\tconst httpOptions: HttpOptions = {};\n\tconst baseUrl = resolveCustomBaseUrl(model.baseUrl);\n\tif (baseUrl) {\n\t\thttpOptions.baseUrl = baseUrl;\n\t\thttpOptions.baseUrlResourceScope = ResourceScope.COLLECTION;\n\t\tif (baseUrlIncludesApiVersion(baseUrl)) {\n\t\t\thttpOptions.apiVersion = \"\";\n\t\t}\n\t}\n\n\tconst headers = providerHeadersToRecord({ ...model.headers, ...optionsHeaders });\n\tif (headers) {\n\t\thttpOptions.headers = headers;\n\t}\n\n\treturn Object.keys(httpOptions).length > 0 ? httpOptions : undefined;\n}\n\nfunction resolveCustomBaseUrl(baseUrl: string): string | undefined {\n\tconst trimmed = baseUrl.trim();\n\tif (!trimmed || trimmed.includes(\"{location}\")) {\n\t\treturn undefined;\n\t}\n\treturn trimmed;\n}\n\nfunction baseUrlIncludesApiVersion(baseUrl: string): boolean {\n\ttry {\n\t\tconst url = new URL(baseUrl);\n\t\treturn url.pathname.split(\"/\").some((part) => /^v\\d+(?:beta\\d*)?$/.test(part));\n\t} catch {\n\t\treturn /(?:^|\\/)v\\d+(?:beta\\d*)?(?:\\/|$)/.test(baseUrl);\n\t}\n}\n\nfunction buildGoogleAuthOptions(env?: ProviderEnv): { keyFilename: string } | undefined {\n\tconst keyFilename = getProviderEnvValue(\"GOOGLE_APPLICATION_CREDENTIALS\", env);\n\treturn keyFilename ? { keyFilename } : undefined;\n}\n\nfunction resolveApiKey(options?: GoogleVertexOptions): string | undefined {\n\tconst apiKey = options?.apiKey?.trim();\n\tif (!apiKey || apiKey === GCP_VERTEX_CREDENTIALS_MARKER || isPlaceholderApiKey(apiKey)) {\n\t\treturn undefined;\n\t}\n\treturn apiKey;\n}\n\nfunction isPlaceholderApiKey(apiKey: string): boolean {\n\treturn /^<[^>]+>$/.test(apiKey);\n}\n\nfunction resolveProject(options?: GoogleVertexOptions): string {\n\tconst project =\n\t\toptions?.project ||\n\t\tgetProviderEnvValue(\"GOOGLE_CLOUD_PROJECT\", options?.env) ||\n\t\tgetProviderEnvValue(\"GCLOUD_PROJECT\", options?.env);\n\tif (!project) {\n\t\tthrow new Error(\n\t\t\t\"Vertex AI requires a project ID. Set GOOGLE_CLOUD_PROJECT/GCLOUD_PROJECT or pass project in options.\",\n\t\t);\n\t}\n\treturn project;\n}\n\nfunction resolveLocation(options?: GoogleVertexOptions): string {\n\tconst location = options?.location || getProviderEnvValue(\"GOOGLE_CLOUD_LOCATION\", options?.env);\n\tif (!location) {\n\t\tthrow new Error(\"Vertex AI requires a location. Set GOOGLE_CLOUD_LOCATION or pass location in options.\");\n\t}\n\treturn location;\n}\n\nfunction buildParams(\n\tmodel: Model<\"google-vertex\">,\n\tcontext: Context,\n\toptions: GoogleVertexOptions = {},\n): GenerateContentParameters {\n\tconst contents = convertMessages(model, context);\n\n\tconst generationConfig: GenerateContentConfig = {};\n\tif (options.temperature !== undefined) {\n\t\tgenerationConfig.temperature = options.temperature;\n\t}\n\tif (options.maxTokens !== undefined) {\n\t\tgenerationConfig.maxOutputTokens = options.maxTokens;\n\t}\n\n\tconst config: GenerateContentConfig = {\n\t\t...(Object.keys(generationConfig).length > 0 && generationConfig),\n\t\t...(context.systemPrompt && { systemInstruction: sanitizeSurrogates(context.systemPrompt) }),\n\t\t...(context.tools && context.tools.length > 0 && { tools: convertTools(context.tools) }),\n\t};\n\n\tif (context.tools && context.tools.length > 0 && options.toolChoice) {\n\t\tconfig.toolConfig = {\n\t\t\tfunctionCallingConfig: {\n\t\t\t\tmode: mapToolChoice(options.toolChoice),\n\t\t\t},\n\t\t};\n\t} else {\n\t\tconfig.toolConfig = undefined;\n\t}\n\n\tif (options.thinking?.enabled && model.reasoning) {\n\t\tconst thinkingConfig: ThinkingConfig = { includeThoughts: true };\n\t\tif (options.thinking.level !== undefined) {\n\t\t\tthinkingConfig.thinkingLevel = THINKING_LEVEL_MAP[options.thinking.level];\n\t\t} else if (options.thinking.budgetTokens !== undefined) {\n\t\t\tthinkingConfig.thinkingBudget = options.thinking.budgetTokens;\n\t\t}\n\t\tconfig.thinkingConfig = thinkingConfig;\n\t} else if (model.reasoning && options.thinking && !options.thinking.enabled) {\n\t\tconfig.thinkingConfig = getDisabledThinkingConfig(model);\n\t}\n\n\tif (options.signal) {\n\t\tif (options.signal.aborted) {\n\t\t\tthrow new Error(\"Request aborted\");\n\t\t}\n\t\tconfig.abortSignal = options.signal;\n\t}\n\n\tconst params: GenerateContentParameters = {\n\t\tmodel: model.id,\n\t\tcontents,\n\t\tconfig,\n\t};\n\n\treturn params;\n}\n\ntype ClampedThinkingLevel = Exclude<PiThinkingLevel, \"xhigh\" | \"max\">;\n\nfunction isGemini3ProModel(model: Model<\"google-generative-ai\">): boolean {\n\treturn /gemini-3(?:\\.\\d+)?-pro/.test(model.id.toLowerCase());\n}\n\nfunction isGemini3FlashModel(model: Model<\"google-generative-ai\">): boolean {\n\tconst id = model.id.toLowerCase();\n\treturn /gemini-3(?:\\.\\d+)?-flash/.test(id) || id === \"gemini-flash-latest\" || id === \"gemini-flash-lite-latest\";\n}\n\nfunction getDisabledThinkingConfig(model: Model<\"google-vertex\">): ThinkingConfig {\n\t// Google docs: Gemini 3.1 Pro cannot disable thinking, and Gemini 3 Flash / Flash-Lite\n\t// do not support full thinking-off either. For Gemini 3 models, use the lowest supported\n\t// thinkingLevel without includeThoughts so hidden thinking remains invisible to pi.\n\tconst geminiModel = model as unknown as Model<\"google-generative-ai\">;\n\tif (isGemini3ProModel(geminiModel)) {\n\t\treturn { thinkingLevel: ThinkingLevel.LOW };\n\t}\n\tif (isGemini3FlashModel(geminiModel)) {\n\t\treturn { thinkingLevel: ThinkingLevel.MINIMAL };\n\t}\n\n\t// Gemini 2.x supports disabling via thinkingBudget = 0.\n\treturn { thinkingBudget: 0 };\n}\n\nfunction getGemini3ThinkingLevel(\n\teffort: ClampedThinkingLevel,\n\tmodel: Model<\"google-generative-ai\">,\n): GoogleThinkingLevel {\n\tif (isGemini3ProModel(model)) {\n\t\tswitch (effort) {\n\t\t\tcase \"minimal\":\n\t\t\tcase \"low\":\n\t\t\t\treturn \"LOW\";\n\t\t\tcase \"medium\":\n\t\t\tcase \"high\":\n\t\t\t\treturn \"HIGH\";\n\t\t}\n\t}\n\tswitch (effort) {\n\t\tcase \"minimal\":\n\t\t\treturn \"MINIMAL\";\n\t\tcase \"low\":\n\t\t\treturn \"LOW\";\n\t\tcase \"medium\":\n\t\t\treturn \"MEDIUM\";\n\t\tcase \"high\":\n\t\t\treturn \"HIGH\";\n\t}\n}\n\nfunction getGoogleBudget(\n\tmodel: Model<\"google-generative-ai\">,\n\teffort: ClampedThinkingLevel,\n\tcustomBudgets?: ThinkingBudgets,\n): number {\n\tif (customBudgets?.[effort] !== undefined) {\n\t\treturn customBudgets[effort]!;\n\t}\n\n\tif (model.id.includes(\"2.5-pro\")) {\n\t\tconst budgets: Record<ClampedThinkingLevel, number> = {\n\t\t\tminimal: 128,\n\t\t\tlow: 2048,\n\t\t\tmedium: 8192,\n\t\t\thigh: 32768,\n\t\t};\n\t\treturn budgets[effort];\n\t}\n\n\tif (model.id.includes(\"2.5-flash\")) {\n\t\tconst budgets: Record<ClampedThinkingLevel, number> = {\n\t\t\tminimal: 128,\n\t\t\tlow: 2048,\n\t\t\tmedium: 8192,\n\t\t\thigh: 24576,\n\t\t};\n\t\treturn budgets[effort];\n\t}\n\n\treturn -1;\n}\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google-vertex.lazy.d.ts","sourceRoot":"","sources":["../../src/api/google-vertex.lazy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAGnD,eAAO,MAAM,eAAe,uBAAqE,CAAC","sourcesContent":["import type { ProviderStreams } from \"../types.ts\";\nimport { lazyApi } from \"./lazy.ts\";\n\nexport const googleVertexApi = (): ProviderStreams => lazyApi(() => import(\"./google-vertex.ts\"));\n"]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"google-vertex.lazy.js","sourceRoot":"","sources":["../../src/api/google-vertex.lazy.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,OAAO,EAAE,MAAM,WAAW,CAAC;AAEpC,MAAM,CAAC,MAAM,eAAe,GAAG,GAAoB,EAAE,CAAC,OAAO,CAAC,GAAG,EAAE,CAAC,MAAM,CAAC,oBAAoB,CAAC,CAAC,CAAC","sourcesContent":["import type { ProviderStreams } from \"../types.ts\";\nimport { lazyApi } from \"./lazy.ts\";\n\nexport const googleVertexApi = (): ProviderStreams => lazyApi(() => import(\"./google-vertex.ts\"));\n"]}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import type { Api, AssistantMessageEvent, Model, ProviderStreams } from "../types.ts";
|
|
2
|
+
import { AssistantMessageEventStream } from "../utils/event-stream.ts";
|
|
3
|
+
/**
|
|
4
|
+
* Returns a stream synchronously while running async setup (auth resolution,
|
|
5
|
+
* lazy module loading) behind it. Setup failures terminate the stream with an
|
|
6
|
+
* error event.
|
|
7
|
+
*/
|
|
8
|
+
export declare function lazyStream(model: Model<Api>, setup: () => Promise<AsyncIterable<AssistantMessageEvent>>): AssistantMessageEventStream;
|
|
9
|
+
/**
|
|
10
|
+
* Wraps a dynamically imported API implementation module as `ProviderStreams`.
|
|
11
|
+
* The module loads on first stream call; the host's import cache deduplicates
|
|
12
|
+
* loads. Load failures terminate the returned stream with an error event.
|
|
13
|
+
*/
|
|
14
|
+
export declare function lazyApi(load: () => Promise<ProviderStreams>): ProviderStreams;
|
|
15
|
+
//# sourceMappingURL=lazy.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"lazy.d.ts","sourceRoot":"","sources":["../../src/api/lazy.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,GAAG,EAAoB,qBAAqB,EAAE,KAAK,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AACxG,OAAO,EAAE,2BAA2B,EAAE,MAAM,0BAA0B,CAAC;AAuCvE;;;;GAIG;AACH,wBAAgB,UAAU,CACzB,KAAK,EAAE,KAAK,CAAC,GAAG,CAAC,EACjB,KAAK,EAAE,MAAM,OAAO,CAAC,aAAa,CAAC,qBAAqB,CAAC,CAAC,GACxD,2BAA2B,CAY7B;AAED;;;;GAIG;AACH,wBAAgB,OAAO,CAAC,IAAI,EAAE,MAAM,OAAO,CAAC,eAAe,CAAC,GAAG,eAAe,CAO7E","sourcesContent":["import type { Api, AssistantMessage, AssistantMessageEvent, Model, ProviderStreams } from \"../types.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\n\nfunction createSetupErrorMessage(model: Model<Api>, error: unknown): AssistantMessage {\n\treturn {\n\t\trole: \"assistant\",\n\t\tcontent: [],\n\t\tapi: model.api,\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\tusage: {\n\t\t\tinput: 0,\n\t\t\toutput: 0,\n\t\t\tcacheRead: 0,\n\t\t\tcacheWrite: 0,\n\t\t\ttotalTokens: 0,\n\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t},\n\t\tstopReason: \"error\",\n\t\terrorMessage: error instanceof Error ? error.message : String(error),\n\t\ttimestamp: Date.now(),\n\t};\n}\n\nfunction hasResult(\n\tsource: AsyncIterable<AssistantMessageEvent>,\n): source is AsyncIterable<AssistantMessageEvent> & { result(): Promise<AssistantMessage> } {\n\treturn typeof (source as { result?: unknown }).result === \"function\";\n}\n\nasync function forwardStream(\n\ttarget: AssistantMessageEventStream,\n\tsource: AsyncIterable<AssistantMessageEvent>,\n): Promise<void> {\n\tfor await (const event of source) {\n\t\ttarget.push(event);\n\t}\n\ttarget.end(hasResult(source) ? await source.result() : undefined);\n}\n\n/**\n * Returns a stream synchronously while running async setup (auth resolution,\n * lazy module loading) behind it. Setup failures terminate the stream with an\n * error event.\n */\nexport function lazyStream(\n\tmodel: Model<Api>,\n\tsetup: () => Promise<AsyncIterable<AssistantMessageEvent>>,\n): AssistantMessageEventStream {\n\tconst outer = new AssistantMessageEventStream();\n\n\tsetup()\n\t\t.then((inner) => forwardStream(outer, inner))\n\t\t.catch((error) => {\n\t\t\tconst message = createSetupErrorMessage(model, error);\n\t\t\touter.push({ type: \"error\", reason: \"error\", error: message });\n\t\t\touter.end(message);\n\t\t});\n\n\treturn outer;\n}\n\n/**\n * Wraps a dynamically imported API implementation module as `ProviderStreams`.\n * The module loads on first stream call; the host's import cache deduplicates\n * loads. Load failures terminate the returned stream with an error event.\n */\nexport function lazyApi(load: () => Promise<ProviderStreams>): ProviderStreams {\n\treturn {\n\t\tstream: (model, context, options) =>\n\t\t\tlazyStream(model, async () => (await load()).stream(model, context, options)),\n\t\tstreamSimple: (model, context, options) =>\n\t\t\tlazyStream(model, async () => (await load()).streamSimple(model, context, options)),\n\t};\n}\n"]}
|
package/dist/api/lazy.js
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { AssistantMessageEventStream } from "../utils/event-stream.js";
|
|
2
|
+
function createSetupErrorMessage(model, error) {
|
|
3
|
+
return {
|
|
4
|
+
role: "assistant",
|
|
5
|
+
content: [],
|
|
6
|
+
api: model.api,
|
|
7
|
+
provider: model.provider,
|
|
8
|
+
model: model.id,
|
|
9
|
+
usage: {
|
|
10
|
+
input: 0,
|
|
11
|
+
output: 0,
|
|
12
|
+
cacheRead: 0,
|
|
13
|
+
cacheWrite: 0,
|
|
14
|
+
totalTokens: 0,
|
|
15
|
+
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },
|
|
16
|
+
},
|
|
17
|
+
stopReason: "error",
|
|
18
|
+
errorMessage: error instanceof Error ? error.message : String(error),
|
|
19
|
+
timestamp: Date.now(),
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
function hasResult(source) {
|
|
23
|
+
return typeof source.result === "function";
|
|
24
|
+
}
|
|
25
|
+
async function forwardStream(target, source) {
|
|
26
|
+
for await (const event of source) {
|
|
27
|
+
target.push(event);
|
|
28
|
+
}
|
|
29
|
+
target.end(hasResult(source) ? await source.result() : undefined);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Returns a stream synchronously while running async setup (auth resolution,
|
|
33
|
+
* lazy module loading) behind it. Setup failures terminate the stream with an
|
|
34
|
+
* error event.
|
|
35
|
+
*/
|
|
36
|
+
export function lazyStream(model, setup) {
|
|
37
|
+
const outer = new AssistantMessageEventStream();
|
|
38
|
+
setup()
|
|
39
|
+
.then((inner) => forwardStream(outer, inner))
|
|
40
|
+
.catch((error) => {
|
|
41
|
+
const message = createSetupErrorMessage(model, error);
|
|
42
|
+
outer.push({ type: "error", reason: "error", error: message });
|
|
43
|
+
outer.end(message);
|
|
44
|
+
});
|
|
45
|
+
return outer;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Wraps a dynamically imported API implementation module as `ProviderStreams`.
|
|
49
|
+
* The module loads on first stream call; the host's import cache deduplicates
|
|
50
|
+
* loads. Load failures terminate the returned stream with an error event.
|
|
51
|
+
*/
|
|
52
|
+
export function lazyApi(load) {
|
|
53
|
+
return {
|
|
54
|
+
stream: (model, context, options) => lazyStream(model, async () => (await load()).stream(model, context, options)),
|
|
55
|
+
streamSimple: (model, context, options) => lazyStream(model, async () => (await load()).streamSimple(model, context, options)),
|
|
56
|
+
};
|
|
57
|
+
}
|
|
58
|
+
//# sourceMappingURL=lazy.js.map
|
|
@@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AE,CAAC,CAAC,CAAC,SAAS,CAAC,CAAC;AAAA,CAClE;AAED;;;;GAIG;AACH,MAAM,UAAU,UAAU,CACzB,KAAiB,EACjB,KAA0D,EAC5B;IAC9B,MAAM,KAAK,GAAG,IAAI,2BAA2B,EAAE,CAAC;IAEhD,KAAK,EAAE;SACL,IAAI,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,aAAa,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;SAC5C,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC;QACjB,MAAM,OAAO,GAAG,uBAAuB,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;QACtD,KAAK,CAAC,IAAI,CAAC,EAAE,IAAI,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,EAAE,KAAK,EAAE,OAAO,EAAE,CAAC,CAAC;QAC/D,KAAK,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC;IAAA,CACnB,CAAC,CAAC;IAEJ,OAAO,KAAK,CAAC;AAAA,CACb;AAED;;;;GAIG;AACH,MAAM,UAAU,OAAO,CAAC,IAAoC,EAAmB;IAC9E,OAAO;QACN,MAAM,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,CACnC,UAAU,CAAC,KAAK,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;QAC9E,YAAY,EAAE,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,EAAE,EAAE,CACzC,UAAU,CAAC,KAAK,EAAE,KAAK,IAAI,EAAE,CAAC,CAAC,MAAM,IAAI,EAAE,CAAC,CAAC,YAAY,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,CAAC;KACpF,CAAC;AAAA,CACF","sourcesContent":["import type { Api, AssistantMessage, AssistantMessageEvent, Model, ProviderStreams } from \"../types.ts\";\nimport { AssistantMessageEventStream } from \"../utils/event-stream.ts\";\n\nfunction createSetupErrorMessage(model: Model<Api>, error: unknown): AssistantMessage {\n\treturn {\n\t\trole: \"assistant\",\n\t\tcontent: [],\n\t\tapi: model.api,\n\t\tprovider: model.provider,\n\t\tmodel: model.id,\n\t\tusage: {\n\t\t\tinput: 0,\n\t\t\toutput: 0,\n\t\t\tcacheRead: 0,\n\t\t\tcacheWrite: 0,\n\t\t\ttotalTokens: 0,\n\t\t\tcost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0, total: 0 },\n\t\t},\n\t\tstopReason: \"error\",\n\t\terrorMessage: error instanceof Error ? error.message : String(error),\n\t\ttimestamp: Date.now(),\n\t};\n}\n\nfunction hasResult(\n\tsource: AsyncIterable<AssistantMessageEvent>,\n): source is AsyncIterable<AssistantMessageEvent> & { result(): Promise<AssistantMessage> } {\n\treturn typeof (source as { result?: unknown }).result === \"function\";\n}\n\nasync function forwardStream(\n\ttarget: AssistantMessageEventStream,\n\tsource: AsyncIterable<AssistantMessageEvent>,\n): Promise<void> {\n\tfor await (const event of source) {\n\t\ttarget.push(event);\n\t}\n\ttarget.end(hasResult(source) ? await source.result() : undefined);\n}\n\n/**\n * Returns a stream synchronously while running async setup (auth resolution,\n * lazy module loading) behind it. Setup failures terminate the stream with an\n * error event.\n */\nexport function lazyStream(\n\tmodel: Model<Api>,\n\tsetup: () => Promise<AsyncIterable<AssistantMessageEvent>>,\n): AssistantMessageEventStream {\n\tconst outer = new AssistantMessageEventStream();\n\n\tsetup()\n\t\t.then((inner) => forwardStream(outer, inner))\n\t\t.catch((error) => {\n\t\t\tconst message = createSetupErrorMessage(model, error);\n\t\t\touter.push({ type: \"error\", reason: \"error\", error: message });\n\t\t\touter.end(message);\n\t\t});\n\n\treturn outer;\n}\n\n/**\n * Wraps a dynamically imported API implementation module as `ProviderStreams`.\n * The module loads on first stream call; the host's import cache deduplicates\n * loads. Load failures terminate the returned stream with an error event.\n */\nexport function lazyApi(load: () => Promise<ProviderStreams>): ProviderStreams {\n\treturn {\n\t\tstream: (model, context, options) =>\n\t\t\tlazyStream(model, async () => (await load()).stream(model, context, options)),\n\t\tstreamSimple: (model, context, options) =>\n\t\t\tlazyStream(model, async () => (await load()).streamSimple(model, context, options)),\n\t};\n}\n"]}
|