mirror of
https://github.com/DayuanJiang/next-ai-draw-io.git
synced 2026-09-08 12:20:20 +08:00
Express Mode with API key
This commit is contained in:
+37
-33
@@ -31,8 +31,7 @@ export interface ClientOverrides {
|
|||||||
awsRegion?: string | null
|
awsRegion?: string | null
|
||||||
awsSessionToken?: string | null
|
awsSessionToken?: string | null
|
||||||
// Vertex AI config
|
// Vertex AI config
|
||||||
vertexProject?: string | null
|
vertexApiKey?: string | null // Express Mode API key
|
||||||
vertexLocation?: string | null
|
|
||||||
// Custom headers (e.g., for EdgeOne cookie auth)
|
// Custom headers (e.g., for EdgeOne cookie auth)
|
||||||
headers?: Record<string, string>
|
headers?: Record<string, string>
|
||||||
}
|
}
|
||||||
@@ -294,30 +293,32 @@ function buildProviderOptions(
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
case "vertexai": {
|
case "vertexai": {
|
||||||
// Google Vertex supports the same thinking config as standard Google provider
|
// Google Vertex supports thinking config for thinking-enabled models
|
||||||
const thinkingBudget = parseIntSafe(
|
// Only models with "thinking" in their name support this feature
|
||||||
process.env.GOOGLE_VERTEX_THINKING_BUDGET,
|
const isThinkingModel = modelId?.toLowerCase().includes("thinking")
|
||||||
"GOOGLE_VERTEX_THINKING_BUDGET",
|
|
||||||
1024,
|
if (isThinkingModel) {
|
||||||
100000,
|
const thinkingBudget = parseIntSafe(
|
||||||
)
|
process.env.GOOGLE_VERTEX_THINKING_BUDGET,
|
||||||
const thinkingLevel = process.env.GOOGLE_VERTEX_THINKING_LEVEL
|
"GOOGLE_VERTEX_THINKING_BUDGET",
|
||||||
|
1024,
|
||||||
|
100000,
|
||||||
|
)
|
||||||
|
const thinkingLevel = process.env.GOOGLE_VERTEX_THINKING_LEVEL
|
||||||
|
|
||||||
if (
|
|
||||||
modelId &&
|
|
||||||
(modelId.includes("gemini-2") ||
|
|
||||||
modelId.includes("gemini-3") ||
|
|
||||||
modelId.includes("gemini2") ||
|
|
||||||
modelId.includes("gemini3"))
|
|
||||||
) {
|
|
||||||
const thinkingConfig: Record<string, any> = {
|
const thinkingConfig: Record<string, any> = {
|
||||||
includeThoughts: true,
|
includeThoughts: true,
|
||||||
}
|
}
|
||||||
|
|
||||||
const isGemini3 =
|
const isGemini3 =
|
||||||
modelId.includes("gemini-3") || modelId.includes("gemini3")
|
modelId?.includes("gemini-3") ||
|
||||||
|
modelId?.includes("gemini3")
|
||||||
if (isGemini3 && thinkingLevel) {
|
if (isGemini3 && thinkingLevel) {
|
||||||
|
// Gemini 3: Use thinkingLevel (minimal/low/medium/high)
|
||||||
thinkingConfig.thinkingLevel = thinkingLevel as
|
thinkingConfig.thinkingLevel = thinkingLevel as
|
||||||
|
| "minimal"
|
||||||
| "low"
|
| "low"
|
||||||
|
| "medium"
|
||||||
| "high"
|
| "high"
|
||||||
} else if (!isGemini3 && thinkingBudget) {
|
} else if (!isGemini3 && thinkingBudget) {
|
||||||
thinkingConfig.thinkingBudget = thinkingBudget
|
thinkingConfig.thinkingBudget = thinkingBudget
|
||||||
@@ -427,7 +428,7 @@ const PROVIDER_ENV_VARS: Record<ProviderName, string | null> = {
|
|||||||
openai: "OPENAI_API_KEY",
|
openai: "OPENAI_API_KEY",
|
||||||
anthropic: "ANTHROPIC_API_KEY",
|
anthropic: "ANTHROPIC_API_KEY",
|
||||||
google: "GOOGLE_GENERATIVE_AI_API_KEY",
|
google: "GOOGLE_GENERATIVE_AI_API_KEY",
|
||||||
vertexai: null, // Uses GOOGLE_APPLICATION_CREDENTIALS or IAM role
|
vertexai: "GOOGLE_VERTEX_API_KEY",
|
||||||
azure: "AZURE_API_KEY",
|
azure: "AZURE_API_KEY",
|
||||||
ollama: null, // No credentials needed for local Ollama
|
ollama: null, // No credentials needed for local Ollama
|
||||||
openrouter: "OPENROUTER_API_KEY",
|
openrouter: "OPENROUTER_API_KEY",
|
||||||
@@ -540,7 +541,11 @@ export function getAIModel(overrides?: ClientOverrides): ModelConfig {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Check if client is providing their own provider override
|
// Check if client is providing their own provider override
|
||||||
const isClientOverride = !!(overrides?.provider && overrides?.apiKey)
|
const isClientOverride = !!(
|
||||||
|
overrides?.provider &&
|
||||||
|
(overrides?.apiKey ||
|
||||||
|
(overrides?.provider === "vertexai" && overrides?.vertexApiKey))
|
||||||
|
)
|
||||||
|
|
||||||
// Use client override if provided, otherwise fall back to env vars
|
// Use client override if provided, otherwise fall back to env vars
|
||||||
const modelId = overrides?.modelId || process.env.AI_MODEL
|
const modelId = overrides?.modelId || process.env.AI_MODEL
|
||||||
@@ -721,25 +726,24 @@ export function getAIModel(overrides?: ClientOverrides): ModelConfig {
|
|||||||
break
|
break
|
||||||
}
|
}
|
||||||
case "vertexai": {
|
case "vertexai": {
|
||||||
// Google Vertex AI uses GCP service account authentication, not API keys
|
// Express Mode: Use API key for authentication
|
||||||
// Auth is handled via GOOGLE_APPLICATION_CREDENTIALS env var or GCP default credentials
|
const vertexApiKey =
|
||||||
// Client-provided Project ID and Location, falling back to server environment variables
|
overrides?.vertexApiKey || process.env.GOOGLE_VERTEX_API_KEY
|
||||||
const vertexProject =
|
|
||||||
overrides?.vertexProject || process.env.GOOGLE_VERTEX_PROJECT
|
|
||||||
const vertexLocation =
|
|
||||||
overrides?.vertexLocation ||
|
|
||||||
process.env.GOOGLE_VERTEX_LOCATION ||
|
|
||||||
"us-central1"
|
|
||||||
|
|
||||||
if (!vertexProject) {
|
if (!vertexApiKey) {
|
||||||
throw new Error(
|
throw new Error(
|
||||||
"Project ID is required for Vertex AI. Please configure it in Settings or set GOOGLE_VERTEX_PROJECT environment variable.",
|
"Vertex AI requires an API key for Express Mode. " +
|
||||||
|
"Get one from Google Cloud Console or set GOOGLE_VERTEX_API_KEY environment variable.",
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Support custom base URL from env or client override
|
||||||
|
const baseURL =
|
||||||
|
overrides?.baseUrl || process.env.GOOGLE_VERTEX_BASE_URL
|
||||||
|
|
||||||
const vertexProvider = createVertex({
|
const vertexProvider = createVertex({
|
||||||
project: vertexProject,
|
apiKey: vertexApiKey,
|
||||||
location: vertexLocation,
|
...(baseURL && { baseURL }),
|
||||||
})
|
})
|
||||||
model = vertexProvider(modelId)
|
model = vertexProvider(modelId)
|
||||||
break
|
break
|
||||||
|
|||||||
@@ -38,8 +38,7 @@ export interface ProviderConfig {
|
|||||||
awsRegion?: string
|
awsRegion?: string
|
||||||
awsSessionToken?: string // Optional, for temporary credentials
|
awsSessionToken?: string // Optional, for temporary credentials
|
||||||
// Vertex AI specific fields
|
// Vertex AI specific fields
|
||||||
vertexProject?: string
|
vertexApiKey?: string // Express Mode API key
|
||||||
vertexLocation?: string
|
|
||||||
|
|
||||||
models: ModelConfig[]
|
models: ModelConfig[]
|
||||||
validated?: boolean // Has API key been validated
|
validated?: boolean // Has API key been validated
|
||||||
@@ -67,8 +66,7 @@ export interface FlattenedModel {
|
|||||||
awsRegion?: string
|
awsRegion?: string
|
||||||
awsSessionToken?: string
|
awsSessionToken?: string
|
||||||
// Vertex AI specific fields
|
// Vertex AI specific fields
|
||||||
vertexProject?: string
|
vertexApiKey?: string // Express Mode API key
|
||||||
vertexLocation?: string
|
|
||||||
|
|
||||||
validated?: boolean // Has this model been validated
|
validated?: boolean // Has this model been validated
|
||||||
}
|
}
|
||||||
@@ -332,8 +330,7 @@ export function flattenModels(config: MultiModelConfig): FlattenedModel[] {
|
|||||||
awsRegion: provider.awsRegion,
|
awsRegion: provider.awsRegion,
|
||||||
awsSessionToken: provider.awsSessionToken,
|
awsSessionToken: provider.awsSessionToken,
|
||||||
// Vertex AI fields
|
// Vertex AI fields
|
||||||
vertexProject: provider.vertexProject,
|
vertexApiKey: provider.vertexApiKey,
|
||||||
vertexLocation: provider.vertexLocation,
|
|
||||||
|
|
||||||
validated: model.validated,
|
validated: model.validated,
|
||||||
})
|
})
|
||||||
|
|||||||
Reference in New Issue
Block a user