dsh-vertex-argon 1.6.3 → 2.0.0
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 +35 -1
- package/README.md +134 -139
- package/adapter.js +142 -96
- package/client.js +26 -65
- package/diagnostic-store.js +2 -2
- package/google-connection.js +2 -5
- package/google-signatures.js +48 -19
- package/index.js +3 -7
- package/inspector-tool.js +1 -1
- package/interaction-adapter.js +8 -8
- package/interaction-state.js +2 -2
- package/interactions-input.js +85 -18
- package/locale/en.json +2 -2
- package/locale/zh.json +2 -2
- package/package.json +11 -10
package/adapter.js
CHANGED
|
@@ -23,29 +23,16 @@ export const PROVIDER = 'google-vertex-argon'
|
|
|
23
23
|
export const NATIVE_PROVIDER = 'google-vertex'
|
|
24
24
|
export const MODEL = 'gemini-4-argon'
|
|
25
25
|
export const GOOGLE_API_MODEL = 'gemini-4-argon-google'
|
|
26
|
-
export const
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
export
|
|
30
|
-
|
|
31
|
-
export const PROXY_MODEL_NAMES = Object.freeze({
|
|
32
|
-
[PROXY_MODEL]: 'Gemini 4 Argon (Antigravity)',
|
|
33
|
-
[PROXY_FLASH_MODEL]: 'Gemini 3.8 Flash (Antigravity)',
|
|
34
|
-
})
|
|
35
|
-
/** 反代模型判定:Argon 与 Flash 都走同一条 Antigravity 通道。 */
|
|
36
|
-
export function isProxyModel(modelId) {
|
|
37
|
-
return typeof modelId === 'string' && PROXY_MODELS.includes(modelId)
|
|
26
|
+
export const CARBON_MODEL = 'gemini-4-carbon'
|
|
27
|
+
export const GOOGLE_API_CARBON_MODEL = 'gemini-4-carbon-google'
|
|
28
|
+
/** Gemini API 官方直连模型判定(Argon 与 Carbon)。 */
|
|
29
|
+
export function isGoogleApiModel(modelId) {
|
|
30
|
+
return modelId === GOOGLE_API_MODEL || modelId === GOOGLE_API_CARBON_MODEL
|
|
38
31
|
}
|
|
39
|
-
/**
|
|
40
|
-
export function
|
|
41
|
-
return modelId ===
|
|
32
|
+
/** 官方直连(Vertex AI / Gemini API)对应的上游模型 ID。 */
|
|
33
|
+
export function officialUpstreamModel(modelId) {
|
|
34
|
+
return modelId === CARBON_MODEL || modelId === GOOGLE_API_CARBON_MODEL ? CARBON_MODEL : MODEL
|
|
42
35
|
}
|
|
43
|
-
/** 反代上游模型 ID:`<基础 ID>-<low|medium|high>`,与反代暴露的档位模型一一对应。 */
|
|
44
|
-
export function resolveProxyUpstreamModel(modelId, level) {
|
|
45
|
-
return `${proxyUpstreamBaseModel(modelId)}-${level}`
|
|
46
|
-
}
|
|
47
|
-
export const PROXY_API_KEY_ENV = 'ANTIGRAVITY_API_KEY'
|
|
48
|
-
export const DEFAULT_PROXY_BASE_URL = 'http://127.0.0.1:8045/v1beta'
|
|
49
36
|
export const LEVELS = ['low', 'medium', 'high']
|
|
50
37
|
export const MAX_SINGLE_CALL_TOKENS = 65536
|
|
51
38
|
export const MAX_LONG_DECODE_TOKENS = 1048576
|
|
@@ -53,18 +40,18 @@ const COMPACTION_HEADROOM_FLOOR = 131072
|
|
|
53
40
|
const ENV_KEYS = ['GOOGLE_APPLICATION_CREDENTIALS', 'GOOGLE_CLOUD_PROJECT', 'GCLOUD_PROJECT', 'GOOGLE_CLOUD_LOCATION', 'GOOGLE_CLOUD_API_KEY']
|
|
54
41
|
const DEFAULT_THINKING_MAP = { off: null, minimal: null, low: 'low', medium: 'medium', high: 'high', xhigh: null, max: null }
|
|
55
42
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
if (
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
url.pathname = (!url.pathname || url.pathname === '/') ? '/v1beta' : url.pathname.replace(/[/]+$/, '')
|
|
65
|
-
return url.href
|
|
43
|
+
// The host's agent/request hook already narrows 'max' to 'high'. Direct adapter
|
|
44
|
+
// callers (advisor tools calling adapter.stream / prepareCall().stream) bypass
|
|
45
|
+
// that hook, so the same narrowing has to happen at this boundary too.
|
|
46
|
+
export function narrowMaxReasoning(options) {
|
|
47
|
+
if (!options || typeof options !== 'object') return options
|
|
48
|
+
const requested = [options.reasoning, options.reasoningEffort]
|
|
49
|
+
if (!requested.some(value => typeof value === 'string' && value.toLowerCase() === 'max')) return options
|
|
50
|
+
return { ...options, reasoning: 'high', reasoningEffort: 'high' }
|
|
66
51
|
}
|
|
67
52
|
|
|
53
|
+
const readVal = v => (v && typeof v === 'object' && typeof v.get === 'function') ? v.get() : v
|
|
54
|
+
|
|
68
55
|
export function isGemini4Model(model) {
|
|
69
56
|
return Boolean(
|
|
70
57
|
model &&
|
|
@@ -76,16 +63,26 @@ export function isGemini4Model(model) {
|
|
|
76
63
|
|
|
77
64
|
function googleKeyEnvironment(value) {
|
|
78
65
|
const name = typeof value === 'string' ? value.trim() : ''
|
|
79
|
-
|
|
80
|
-
return !name || name.toUpperCase() === PROXY_API_KEY_ENV ? 'GOOGLE_API_KEY' : name
|
|
66
|
+
return name || 'GOOGLE_API_KEY'
|
|
81
67
|
}
|
|
82
68
|
|
|
83
69
|
export function detectPiAiArgonRoutes(ctx, explicitRoutes) {
|
|
84
70
|
if (explicitRoutes && typeof explicitRoutes === 'object') {
|
|
71
|
+
const hasVertexCarbon = Boolean(explicitRoutes.hasVertexCarbon)
|
|
72
|
+
const hasGoogleCarbon = Boolean(explicitRoutes.hasGoogleCarbon)
|
|
73
|
+
const hasVertexArgon = explicitRoutes.hasVertexArgon !== undefined
|
|
74
|
+
? Boolean(explicitRoutes.hasVertexArgon)
|
|
75
|
+
: explicitRoutes.hasVertex !== false
|
|
76
|
+
const hasGoogleArgon = explicitRoutes.hasGoogleArgon !== undefined
|
|
77
|
+
? Boolean(explicitRoutes.hasGoogleArgon)
|
|
78
|
+
: Boolean(explicitRoutes.hasGoogle)
|
|
85
79
|
return {
|
|
86
|
-
hasVertex:
|
|
87
|
-
hasGoogle:
|
|
88
|
-
|
|
80
|
+
hasVertex: hasVertexArgon || hasVertexCarbon,
|
|
81
|
+
hasGoogle: hasGoogleArgon || hasGoogleCarbon,
|
|
82
|
+
hasVertexArgon,
|
|
83
|
+
hasGoogleArgon,
|
|
84
|
+
hasVertexCarbon,
|
|
85
|
+
hasGoogleCarbon,
|
|
89
86
|
googleApiKeyEnv: googleKeyEnvironment(explicitRoutes.googleApiKeyEnv),
|
|
90
87
|
googleBaseUrl: explicitRoutes.googleBaseUrl,
|
|
91
88
|
vertexBaseUrl: explicitRoutes.vertexBaseUrl,
|
|
@@ -98,22 +95,68 @@ export function detectPiAiArgonRoutes(ctx, explicitRoutes) {
|
|
|
98
95
|
const providers = readVal(entry?.fiber?.config?.providers) ?? readVal(entry?.options?.config?.providers) ?? {}
|
|
99
96
|
const googleCfg = providers?.google
|
|
100
97
|
const vertexCfg = providers?.['google-vertex']
|
|
101
|
-
const hasModel = list => Array.isArray(list) && list.some(m =>
|
|
102
|
-
const
|
|
103
|
-
const
|
|
98
|
+
const hasModel = (list, re) => Array.isArray(list) && list.some(m => re.test(String(readVal(m?.id) ?? '')))
|
|
99
|
+
const googleModels = readVal(googleCfg?.models)
|
|
100
|
+
const vertexModels = readVal(vertexCfg?.models)
|
|
101
|
+
const hasGoogleArgon = hasModel(googleModels, /^(?:models\/)?gemini-4-argon\b/i)
|
|
102
|
+
const hasVertexArgon = hasModel(vertexModels, /^(?:models\/)?gemini-4-argon\b/i)
|
|
103
|
+
const hasGoogleCarbon = hasModel(googleModels, /^(?:models\/)?gemini-4-carbon\b/i)
|
|
104
|
+
const hasVertexCarbon = hasModel(vertexModels, /^(?:models\/)?gemini-4-carbon\b/i)
|
|
105
|
+
const hasGoogle = hasGoogleArgon || hasGoogleCarbon
|
|
106
|
+
const hasVertex = hasVertexArgon || hasVertexCarbon || !hasGoogle
|
|
104
107
|
return {
|
|
105
|
-
hasVertex
|
|
108
|
+
hasVertex,
|
|
106
109
|
hasGoogle,
|
|
110
|
+
hasVertexArgon: hasVertexArgon || (!hasGoogle && !hasVertexCarbon),
|
|
111
|
+
hasGoogleArgon,
|
|
112
|
+
hasVertexCarbon,
|
|
113
|
+
hasGoogleCarbon,
|
|
107
114
|
googleApiKeyEnv: googleKeyEnvironment(readVal(googleCfg?.apiKeyEnv)),
|
|
108
115
|
googleBaseUrl: readVal(googleCfg?.baseURL),
|
|
109
116
|
vertexBaseUrl: readVal(vertexCfg?.baseURL),
|
|
110
117
|
}
|
|
111
118
|
}
|
|
112
119
|
} catch {}
|
|
113
|
-
return {
|
|
120
|
+
return {
|
|
121
|
+
hasVertex: true,
|
|
122
|
+
hasGoogle: false,
|
|
123
|
+
hasVertexArgon: true,
|
|
124
|
+
hasGoogleArgon: false,
|
|
125
|
+
hasVertexCarbon: false,
|
|
126
|
+
hasGoogleCarbon: false,
|
|
127
|
+
googleApiKeyEnv: 'GOOGLE_API_KEY',
|
|
128
|
+
}
|
|
114
129
|
}
|
|
115
130
|
|
|
116
131
|
const TRANSIENT_GEMINI_STOP_RE = /(?:Unhandled stop reason:\s*(?:MALFORMED_RESPONSE|MALFORMED_FUNCTION_CALL|UNEXPECTED_TOOL_CALL|TOO_MANY_TOOL_CALLS)|Provider stopped with:\s*(?:MALFORMED_RESPONSE|MALFORMED_FUNCTION_CALL|UNEXPECTED_TOOL_CALL|OTHER|FINISH_REASON_UNSPECIFIED))/i
|
|
132
|
+
const AUTO_TOOL_MODE_TTL_MS = 180_000
|
|
133
|
+
const autoToolModeFallbacks = new Map()
|
|
134
|
+
|
|
135
|
+
function retryKeyOf(requestModel, context, options) {
|
|
136
|
+
const tail = context?.messages?.at?.(-1)
|
|
137
|
+
const tailHint = typeof tail?.content === 'string' ? tail.content.slice(0, 160) : JSON.stringify(tail?.content ?? '').slice(0, 160)
|
|
138
|
+
return `${options?.sessionId ?? 'anon'}:${requestModel?.id ?? ''}:${context?.messages?.length ?? 0}:${tailHint}`
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
function markAutoToolModeFallback(key) {
|
|
142
|
+
if (!key) return
|
|
143
|
+
if (autoToolModeFallbacks.size >= 256) autoToolModeFallbacks.delete(autoToolModeFallbacks.keys().next().value)
|
|
144
|
+
autoToolModeFallbacks.set(key, Date.now())
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
function shouldFallbackToAutoToolMode(key) {
|
|
148
|
+
const ts = key ? autoToolModeFallbacks.get(key) : undefined
|
|
149
|
+
if (ts === undefined) return false
|
|
150
|
+
if (Date.now() - ts > AUTO_TOOL_MODE_TTL_MS) {
|
|
151
|
+
autoToolModeFallbacks.delete(key)
|
|
152
|
+
return false
|
|
153
|
+
}
|
|
154
|
+
return true
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
function clearAutoToolModeFallback(key) {
|
|
158
|
+
if (key) autoToolModeFallbacks.delete(key)
|
|
159
|
+
}
|
|
117
160
|
|
|
118
161
|
let apiClientProtoRef = null
|
|
119
162
|
let apiClientPatch = null
|
|
@@ -154,13 +197,14 @@ export function ensureGemini4ApiClientPatch() {
|
|
|
154
197
|
return patch.dispose
|
|
155
198
|
}
|
|
156
199
|
|
|
157
|
-
function wrapGemini4Stream(rawStream, tracker, signal) {
|
|
200
|
+
function wrapGemini4Stream(rawStream, tracker, signal, retryKey) {
|
|
158
201
|
if (!rawStream || typeof rawStream[Symbol.asyncIterator] !== 'function') return rawStream
|
|
159
202
|
return {
|
|
160
203
|
[Symbol.asyncIterator]: async function* () {
|
|
161
204
|
try {
|
|
162
205
|
for await (const event of rawStream) {
|
|
163
206
|
if (event?.type === 'done') {
|
|
207
|
+
clearAutoToolModeFallback(retryKey)
|
|
164
208
|
const stop = event.message?.stopReason
|
|
165
209
|
const outcome = stop === 'toolUse' ? 'tool-calls' : stop === 'length' ? 'max-tokens' : 'done'
|
|
166
210
|
tracker?.finishStream({ outcome })
|
|
@@ -168,6 +212,9 @@ function wrapGemini4Stream(rawStream, tracker, signal) {
|
|
|
168
212
|
const isTransient =
|
|
169
213
|
typeof event?.error?.errorMessage === 'string' &&
|
|
170
214
|
TRANSIENT_GEMINI_STOP_RE.test(event.error.errorMessage)
|
|
215
|
+
if (isTransient || event?.error?.code === 'STREAM_PHASE_TIMEOUT' || /stream phase timed out/i.test(String(event?.error?.errorMessage ?? ''))) {
|
|
216
|
+
markAutoToolModeFallback(retryKey)
|
|
217
|
+
}
|
|
171
218
|
tracker?.finishStream({
|
|
172
219
|
outcome: signal?.aborted ? 'aborted' : 'error',
|
|
173
220
|
error: event.error,
|
|
@@ -187,6 +234,9 @@ function wrapGemini4Stream(rawStream, tracker, signal) {
|
|
|
187
234
|
yield event
|
|
188
235
|
}
|
|
189
236
|
} catch (error) {
|
|
237
|
+
if (error?.code === 'STREAM_PHASE_TIMEOUT' || /stream phase timed out/i.test(String(error?.message ?? ''))) {
|
|
238
|
+
markAutoToolModeFallback(retryKey)
|
|
239
|
+
}
|
|
190
240
|
tracker?.finishStream({
|
|
191
241
|
outcome: signal?.aborted ? 'aborted' : 'error',
|
|
192
242
|
error,
|
|
@@ -216,11 +266,11 @@ export function gemini4StreamSimple(
|
|
|
216
266
|
assertPreparedStructuredOutput(structuredOutput)
|
|
217
267
|
ensureGemini4ApiClientPatch()
|
|
218
268
|
const fallback = defaultLevel ?? (/cyber-permissive/i.test(requestModel?.id ?? '') ? 'high' : 'medium')
|
|
219
|
-
const
|
|
269
|
+
const requestedLevel = options.reasoning ?? fallback
|
|
270
|
+
const rawLevel = typeof requestedLevel === 'string' && requestedLevel.toLowerCase() === 'max' ? 'high' : requestedLevel
|
|
220
271
|
const mapped = requestModel?.thinkingLevelMap?.[rawLevel] ?? rawLevel
|
|
221
272
|
const level = typeof mapped === 'string' ? mapped.toLowerCase() : fallback
|
|
222
|
-
if (!LEVELS.includes(level)) throw new LlmError('Gemini 4 Argon requires low, medium or high.', 'UNSUPPORTED_REASONING_EFFORT')
|
|
223
|
-
const resolvedUpstreamModelId = isProxyModel(requestModel?.id) ? resolveProxyUpstreamModel(requestModel.id, level) : upstreamModelId
|
|
273
|
+
if (!LEVELS.includes(level)) throw new LlmError('Gemini 4 (Argon / Carbon) requires low, medium or high.', 'UNSUPPORTED_REASONING_EFFORT')
|
|
224
274
|
const { reasoning, thinking, thinkingBudgets, temperature, onPayload: prior, onProviderStreamEvent: priorEvent, ...rest } = options
|
|
225
275
|
const requestedMaxTokens = options.maxTokens ?? requestModel.maxTokens
|
|
226
276
|
const effectiveMaxTokens =
|
|
@@ -242,6 +292,7 @@ export function gemini4StreamSimple(
|
|
|
242
292
|
captureOptions,
|
|
243
293
|
)
|
|
244
294
|
const signatures = createGoogleSignatureBridge(context, requestModel)
|
|
295
|
+
const retryKey = retryKeyOf(requestModel, context, options)
|
|
245
296
|
return wrapGemini4Stream(
|
|
246
297
|
signatures.stream(nativeStream(requestModel, signatures.context, {
|
|
247
298
|
...rest,
|
|
@@ -251,10 +302,10 @@ export function gemini4StreamSimple(
|
|
|
251
302
|
const replacement = await prior?.(payload, payloadModel)
|
|
252
303
|
let body = replacement === undefined ? payload : replacement
|
|
253
304
|
if (!body || typeof body !== 'object' || Array.isArray(body) || !Array.isArray(body.contents) || !body.config || typeof body.config !== 'object' || Array.isArray(body.config)) {
|
|
254
|
-
throw new LlmError('Gemini 4
|
|
305
|
+
throw new LlmError('Gemini 4 received an incompatible native payload.', 'INVALID_REQUEST')
|
|
255
306
|
}
|
|
256
|
-
if (body.model !== requestModel.id && body.model !== upstreamModelId
|
|
257
|
-
throw new LlmError('Gemini 4
|
|
307
|
+
if (body.model !== requestModel.id && body.model !== upstreamModelId) {
|
|
308
|
+
throw new LlmError('Gemini 4 received an incompatible model payload.', 'INVALID_REQUEST')
|
|
258
309
|
}
|
|
259
310
|
body = signatures.restorePayload(body)
|
|
260
311
|
if (structuredOutput) body = structuredOutput.generateContent(body)
|
|
@@ -263,15 +314,16 @@ export function gemini4StreamSimple(
|
|
|
263
314
|
cleanConfig.httpOptions && typeof cleanConfig.httpOptions === 'object' && !Array.isArray(cleanConfig.httpOptions)
|
|
264
315
|
? cleanConfig.httpOptions
|
|
265
316
|
: {}
|
|
317
|
+
const toolMode = shouldFallbackToAutoToolMode(retryKey) ? 'AUTO' : 'VALIDATED'
|
|
266
318
|
return {
|
|
267
319
|
...body,
|
|
268
|
-
model:
|
|
320
|
+
model: upstreamModelId,
|
|
269
321
|
config: {
|
|
270
322
|
...cleanConfig,
|
|
271
323
|
...(cleanConfig.tools?.some(tool => tool.functionDeclarations?.length) ? {
|
|
272
324
|
toolConfig: {
|
|
273
325
|
...cleanConfig.toolConfig,
|
|
274
|
-
functionCallingConfig: { ...cleanConfig.toolConfig?.functionCallingConfig, mode:
|
|
326
|
+
functionCallingConfig: { ...cleanConfig.toolConfig?.functionCallingConfig, mode: toolMode },
|
|
275
327
|
},
|
|
276
328
|
} : {}),
|
|
277
329
|
automaticContinuation: false,
|
|
@@ -291,6 +343,7 @@ export function gemini4StreamSimple(
|
|
|
291
343
|
})),
|
|
292
344
|
tracker,
|
|
293
345
|
options.signal,
|
|
346
|
+
retryKey,
|
|
294
347
|
)
|
|
295
348
|
}
|
|
296
349
|
|
|
@@ -366,10 +419,6 @@ export function resolveConfigValues(raw = {}) {
|
|
|
366
419
|
const googleServiceTier = readVal(source?.googleServiceTier) ?? 'standard'
|
|
367
420
|
const vertexServiceTier = readVal(source?.vertexServiceTier) ?? 'standard'
|
|
368
421
|
const vertexPaygoOnly = readVal(source?.vertexPaygoOnly) ?? false
|
|
369
|
-
const proxyEnabled = readVal(source?.proxyEnabled) ?? false
|
|
370
|
-
const proxyBaseUrl = normalizeProxyBaseUrl(readVal(source?.proxyBaseUrl))
|
|
371
|
-
const proxyApiKey = readVal(source?.proxyApiKey) ?? ''
|
|
372
|
-
if (typeof proxyEnabled !== 'boolean' || typeof proxyApiKey !== 'string') throw new LlmError('Invalid Antigravity proxy configuration.', 'INVALID_CONFIG')
|
|
373
422
|
const retainRawResponses = readVal(source?.retainRawResponses) ?? false
|
|
374
423
|
if (typeof retainRawResponses !== 'boolean') throw new LlmError('Raw response retention consent must be an explicit boolean.', 'INVALID_CONFIG')
|
|
375
424
|
if (![googleServiceTier, vertexServiceTier].every(tier => SERVICE_TIERS.includes(tier))) throw new LlmError('Invalid Argon service tier.', 'INVALID_CONFIG')
|
|
@@ -399,9 +448,6 @@ export function resolveConfigValues(raw = {}) {
|
|
|
399
448
|
googleServiceTier,
|
|
400
449
|
vertexServiceTier,
|
|
401
450
|
vertexPaygoOnly,
|
|
402
|
-
proxyEnabled,
|
|
403
|
-
proxyBaseUrl,
|
|
404
|
-
proxyApiKey: proxyApiKey.trim(),
|
|
405
451
|
retainRawResponses,
|
|
406
452
|
interactionsBackground,
|
|
407
453
|
allowInteractionStorage,
|
|
@@ -409,44 +455,43 @@ export function resolveConfigValues(raw = {}) {
|
|
|
409
455
|
}
|
|
410
456
|
}
|
|
411
457
|
|
|
412
|
-
/** Serve Vertex AI
|
|
458
|
+
/** Serve the official Vertex AI and Gemini API models in one plugin group. */
|
|
413
459
|
export function argonProvider(nativeVertex, models, defaultLevel = 'medium', nativeGoogle = googleProvider(), longDecode = true, recordDiagnostic, serviceConfig = {}, responseHub, structuredOutput) {
|
|
414
460
|
const modelMap = new Map((Array.isArray(models) ? models : [models]).map(m => [m.id, m]))
|
|
415
461
|
return {
|
|
416
462
|
id: PROVIDER,
|
|
417
|
-
name: '
|
|
463
|
+
name: 'Gemini 4 Adapter',
|
|
418
464
|
auth: nativeVertex.auth,
|
|
419
465
|
getModels: () => [...modelMap.values()],
|
|
420
466
|
streamSimple(requestModel, context, options = {}) {
|
|
421
467
|
const configured = modelMap.get(requestModel?.id)
|
|
422
468
|
if (!configured || requestModel.provider !== PROVIDER) {
|
|
423
|
-
throw new LlmError('
|
|
469
|
+
throw new LlmError('Gemini 4 Adapter only serves configured Gemini 4 routes.', 'UNSUPPORTED_OPTION')
|
|
424
470
|
}
|
|
425
471
|
const { env: rawEnv, apiKey: vertexApiKey, ...restOptions } = options
|
|
426
472
|
const cleanEnv = rawEnv ? { ...rawEnv } : undefined
|
|
427
473
|
if (cleanEnv) {
|
|
428
474
|
delete cleanEnv.__GOOGLE_API_KEY__
|
|
429
|
-
delete cleanEnv.__ANTIGRAVITY_API_KEY__
|
|
430
475
|
}
|
|
431
476
|
if (configured.api === 'google-generative-ai') {
|
|
432
|
-
const
|
|
433
|
-
|
|
434
|
-
|
|
477
|
+
const apiKey = rawEnv?.__GOOGLE_API_KEY__
|
|
478
|
+
if (!apiKey) throw new LlmError('Gemini API credentials are not configured.', 'MISSING_CREDENTIAL')
|
|
479
|
+
const policy = resolveServicePolicy(serviceConfig, 'google-generative-ai', 'generate-content')
|
|
435
480
|
return gemini4StreamSimple(
|
|
436
481
|
(m, c, o) => nativeGoogle.stream(m, c, o),
|
|
437
482
|
requestModel,
|
|
438
483
|
context,
|
|
439
484
|
{ ...restOptions, ...(cleanEnv ? { env: cleanEnv } : {}), ...(apiKey ? { apiKey } : {}), maxTokens: options.maxTokens ?? configured.maxTokens },
|
|
440
485
|
defaultLevel,
|
|
441
|
-
|
|
486
|
+
officialUpstreamModel(configured.id),
|
|
442
487
|
longDecode,
|
|
443
488
|
recordDiagnostic,
|
|
444
|
-
|
|
489
|
+
policy,
|
|
445
490
|
{ responseHub, retainRawResponses: serviceConfig.retainRawResponses }, structuredOutput,
|
|
446
491
|
)
|
|
447
492
|
}
|
|
448
493
|
if (requestModel.api !== 'google-vertex') {
|
|
449
|
-
throw new LlmError('
|
|
494
|
+
throw new LlmError('Gemini 4 Adapter only serves google-vertex or google-generative-ai.', 'UNSUPPORTED_OPTION')
|
|
450
495
|
}
|
|
451
496
|
return gemini4StreamSimple(
|
|
452
497
|
(m, c, o) => nativeVertex.stream(m, c, o),
|
|
@@ -454,7 +499,7 @@ export function argonProvider(nativeVertex, models, defaultLevel = 'medium', nat
|
|
|
454
499
|
context,
|
|
455
500
|
{ ...restOptions, ...(cleanEnv ? { env: cleanEnv } : {}), ...(vertexApiKey ? { apiKey: vertexApiKey } : {}), maxTokens: options.maxTokens ?? configured.maxTokens },
|
|
456
501
|
defaultLevel,
|
|
457
|
-
|
|
502
|
+
officialUpstreamModel(configured.id),
|
|
458
503
|
longDecode,
|
|
459
504
|
recordDiagnostic,
|
|
460
505
|
resolveServicePolicy(serviceConfig, 'google-vertex', 'generate-content'),
|
|
@@ -477,7 +522,7 @@ export class ArgonAdapter extends LlmAdapter {
|
|
|
477
522
|
imageRequestPricing(provider, model) { return this.inner.imageRequestPricing?.(provider, model) }
|
|
478
523
|
imageRequestTarget(provider, model, image) {
|
|
479
524
|
const cfg = this.configSnapshot?.()
|
|
480
|
-
const protocol = (model
|
|
525
|
+
const protocol = isGoogleApiModel(model) ? cfg?.googleProtocol : cfg?.vertexProtocol
|
|
481
526
|
if (provider === PROVIDER && protocol === 'interactions' && this.interactionHandler) return interactionImageRequestTarget(image)
|
|
482
527
|
return this.inner.imageRequestTarget(provider, model, image)
|
|
483
528
|
}
|
|
@@ -488,13 +533,14 @@ export class ArgonAdapter extends LlmAdapter {
|
|
|
488
533
|
resolveModel(provider, model, signal) { return this.inner.resolveModel(provider, model, signal) }
|
|
489
534
|
async prepareCall(provider, model, signal) {
|
|
490
535
|
const cfg = this.configSnapshot?.()
|
|
491
|
-
|
|
536
|
+
const googleApi = isGoogleApiModel(model)
|
|
537
|
+
const protocol = googleApi ? cfg?.googleProtocol : cfg?.vertexProtocol
|
|
538
|
+
if (provider === PROVIDER && cfg) resolveServicePolicy(cfg, googleApi ? 'google-generative-ai' : 'google-vertex', protocol)
|
|
492
539
|
const prepared = await this.inner.prepareCall(provider, model, signal)
|
|
493
|
-
const protocol = (model === GOOGLE_API_MODEL || isProxyModel(model)) ? cfg?.googleProtocol : cfg?.vertexProtocol
|
|
494
540
|
if (provider === PROVIDER && protocol === 'interactions' && this.interactionHandler) {
|
|
495
|
-
return { model: prepared.model, stream: options => this.interactionHandler(options, cfg) }
|
|
541
|
+
return { model: prepared.model, stream: options => this.interactionHandler(narrowMaxReasoning(options), cfg) }
|
|
496
542
|
}
|
|
497
|
-
return { ...prepared, stream: options => prepared.stream(generateContentHistory(options, model)) }
|
|
543
|
+
return { ...prepared, stream: options => prepared.stream(generateContentHistory(narrowMaxReasoning(options), model)) }
|
|
498
544
|
}
|
|
499
545
|
async *stream(options) {
|
|
500
546
|
const prepared = await this.prepareCall(options.provider, options.model, options.signal)
|
|
@@ -554,12 +600,12 @@ export function createArgonAdapter(ctx, configInput, nativeVertex = googleVertex
|
|
|
554
600
|
vt: cfg.vertexServiceTier,
|
|
555
601
|
paygo: cfg.vertexPaygoOnly,
|
|
556
602
|
raw: cfg.retainRawResponses,
|
|
557
|
-
pe: cfg.proxyEnabled,
|
|
558
|
-
pb: cfg.proxyBaseUrl,
|
|
559
|
-
pk: cfg.proxyApiKey,
|
|
560
|
-
p: detected.hasProxy,
|
|
561
603
|
v: detected.hasVertex,
|
|
562
604
|
g: detected.hasGoogle,
|
|
605
|
+
va: detected.hasVertexArgon,
|
|
606
|
+
ga: detected.hasGoogleArgon,
|
|
607
|
+
vc: detected.hasVertexCarbon,
|
|
608
|
+
gc: detected.hasGoogleCarbon,
|
|
563
609
|
ge: detected.googleApiKeyEnv,
|
|
564
610
|
gb: detected.googleBaseUrl,
|
|
565
611
|
vb: detected.vertexBaseUrl,
|
|
@@ -579,21 +625,19 @@ export function createArgonAdapter(ctx, configInput, nativeVertex = googleVertex
|
|
|
579
625
|
(await input.ctx.env(detected.googleApiKeyEnv || 'GOOGLE_API_KEY')) ??
|
|
580
626
|
(await input.ctx.env('GOOGLE_API_KEY')) ??
|
|
581
627
|
(await input.ctx.env('GEMINI_API_KEY'))
|
|
582
|
-
const proxyKey = cfg.proxyApiKey || await input.ctx.env(PROXY_API_KEY_ENV)
|
|
583
628
|
if (googleKey) env.__GOOGLE_API_KEY__ = googleKey
|
|
584
|
-
if (proxyKey) env.__ANTIGRAVITY_API_KEY__ = proxyKey
|
|
585
629
|
if (vertexRes) {
|
|
586
630
|
return { ...vertexRes, env: { ...env, ...vertexRes.env } }
|
|
587
631
|
}
|
|
588
|
-
if (googleKey
|
|
589
|
-
return { auth: {}, env, source:
|
|
632
|
+
if (googleKey) {
|
|
633
|
+
return { auth: {}, env, source: detected.googleApiKeyEnv || 'GOOGLE_API_KEY' }
|
|
590
634
|
}
|
|
591
635
|
return undefined
|
|
592
636
|
},
|
|
593
637
|
},
|
|
594
638
|
}
|
|
595
639
|
const models = []
|
|
596
|
-
if (detected.hasVertex) {
|
|
640
|
+
if (detected.hasVertexArgon ?? detected.hasVertex) {
|
|
597
641
|
models.push({
|
|
598
642
|
id: MODEL,
|
|
599
643
|
name: detected.hasGoogle ? 'Gemini 4 Argon (Vertex AI)' : 'Gemini 4 Argon',
|
|
@@ -608,13 +652,13 @@ export function createArgonAdapter(ctx, configInput, nativeVertex = googleVertex
|
|
|
608
652
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
609
653
|
})
|
|
610
654
|
}
|
|
611
|
-
if (detected.
|
|
655
|
+
if (detected.hasVertexCarbon) {
|
|
612
656
|
models.push({
|
|
613
|
-
id:
|
|
614
|
-
name: 'Gemini 4
|
|
657
|
+
id: CARBON_MODEL,
|
|
658
|
+
name: detected.hasGoogle ? 'Gemini 4 Carbon (Vertex AI)' : 'Gemini 4 Carbon',
|
|
615
659
|
provider: PROVIDER,
|
|
616
|
-
api: 'google-
|
|
617
|
-
baseUrl: detected.
|
|
660
|
+
api: 'google-vertex',
|
|
661
|
+
baseUrl: detected.vertexBaseUrl || 'https://{location}-aiplatform.googleapis.com',
|
|
618
662
|
reasoning: true,
|
|
619
663
|
input: ['text', 'image'],
|
|
620
664
|
contextWindow: cfg.contextWindow,
|
|
@@ -623,13 +667,13 @@ export function createArgonAdapter(ctx, configInput, nativeVertex = googleVertex
|
|
|
623
667
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
624
668
|
})
|
|
625
669
|
}
|
|
626
|
-
if (
|
|
670
|
+
if (detected.hasGoogleArgon ?? detected.hasGoogle) {
|
|
627
671
|
models.push({
|
|
628
|
-
id:
|
|
629
|
-
name:
|
|
672
|
+
id: GOOGLE_API_MODEL,
|
|
673
|
+
name: 'Gemini 4 Argon (Gemini API)',
|
|
630
674
|
provider: PROVIDER,
|
|
631
675
|
api: 'google-generative-ai',
|
|
632
|
-
baseUrl:
|
|
676
|
+
baseUrl: detected.googleBaseUrl || 'https://generativelanguage.googleapis.com/v1beta',
|
|
633
677
|
reasoning: true,
|
|
634
678
|
input: ['text', 'image'],
|
|
635
679
|
contextWindow: cfg.contextWindow,
|
|
@@ -637,12 +681,14 @@ export function createArgonAdapter(ctx, configInput, nativeVertex = googleVertex
|
|
|
637
681
|
thinkingLevelMap: { ...DEFAULT_THINKING_MAP },
|
|
638
682
|
cost: { input: 0, output: 0, cacheRead: 0, cacheWrite: 0 },
|
|
639
683
|
})
|
|
684
|
+
}
|
|
685
|
+
if (detected.hasGoogleCarbon) {
|
|
640
686
|
models.push({
|
|
641
|
-
id:
|
|
642
|
-
name:
|
|
687
|
+
id: GOOGLE_API_CARBON_MODEL,
|
|
688
|
+
name: 'Gemini 4 Carbon (Gemini API)',
|
|
643
689
|
provider: PROVIDER,
|
|
644
690
|
api: 'google-generative-ai',
|
|
645
|
-
baseUrl:
|
|
691
|
+
baseUrl: detected.googleBaseUrl || 'https://generativelanguage.googleapis.com/v1beta',
|
|
646
692
|
reasoning: true,
|
|
647
693
|
input: ['text', 'image'],
|
|
648
694
|
contextWindow: cfg.contextWindow,
|
|
@@ -656,10 +702,10 @@ export function createArgonAdapter(ctx, configInput, nativeVertex = googleVertex
|
|
|
656
702
|
cachedKey = key
|
|
657
703
|
cachedProfiles = new Map([[PROVIDER, {
|
|
658
704
|
provider: PROVIDER,
|
|
659
|
-
displayName: '
|
|
705
|
+
displayName: 'Gemini 4 Adapter',
|
|
660
706
|
piProvider: provider,
|
|
661
707
|
reasoning: cfg.reasoning,
|
|
662
|
-
streamIdleTimeoutMs: cfg.longDecode ? STREAM_READ_TIMEOUT_MS + 60000 :
|
|
708
|
+
streamIdleTimeoutMs: cfg.longDecode ? STREAM_READ_TIMEOUT_MS + 60000 : STREAM_READ_TIMEOUT_MS,
|
|
663
709
|
maxRequestImageBytes: 20 * 1024 * 1024,
|
|
664
710
|
requestImagePixelBudget: 2048 * 2048,
|
|
665
711
|
requestImageMaxBytes: 1024 * 1024,
|