Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ The names are shown as they appear in the knowledge files.

<!-- generated:providers:start -->
<!-- Generated by ./gradlew updateReadme; do not edit by hand -->
Presets for DeepSeek, 通义千问, Kimi, 智谱, 智谱 Coding Plan, 硅基流动, 火山方舟, MiniMax, 阶跃星辰, 腾讯混元, 百度千帆, OpenRouter, OpenDesign Go, OpenCode Go, Anthropic, Gemini, OpenAI, xAI, Mistral, Groq, Cloudflare Workers AI, Ollama, LM Studio and more; for any other provider pick Custom (自定义), which takes any OpenAI-compatible, Anthropic, Gemini or OpenAI Responses endpoint.
Presets for DeepSeek, 通义千问, Kimi, 智谱, 智谱 Coding Plan, 硅基流动, 火山方舟, MiniMax, 阶跃星辰, 腾讯混元, 百度千帆, OpenRouter, OpenDesign Go, OpenCode Go, Anthropic, Gemini, Vertex AI, OpenAI, xAI, Mistral, Groq, Cloudflare Workers AI, Ollama, LM Studio and more; for any other provider pick Custom (自定义), which takes any OpenAI-compatible, Anthropic, Gemini, Vertex or OpenAI Responses endpoint.
<!-- generated:providers:end -->

You can also skip the key and sign in with a ChatGPT (Plus / Pro), xAI / Grok or OpenRouter account. Steps for adding a model, and what to know about each sign-in, are in [models and sign-in](docs/models.en.md).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ internal data class AllowanceRoute(
"OpenCode Go" -> AllowanceRoute(dashboard = "https://opencode.ai/console")
"Anthropic" -> AllowanceRoute(dashboard = "https://platform.claude.com/cost")
"Gemini" -> AllowanceRoute(dashboard = "https://aistudio.google.com/usage")
"Vertex AI" -> AllowanceRoute(dashboard = "https://console.cloud.google.com/billing")
"OpenAI" -> AllowanceRoute(dashboard = "https://platform.openai.com/settings/organization/billing/overview")
"xAI" -> AllowanceRoute(dashboard = "https://console.x.ai/")
"Mistral" -> AllowanceRoute(dashboard = "https://admin.mistral.ai/organization/usage")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ object ProviderKeySites {
"openrouter.ai" to "https://openrouter.ai/settings/keys",
"api.anthropic.com" to "https://console.anthropic.com/settings/keys",
"generativelanguage.googleapis.com" to "https://aistudio.google.com/app/apikey",
"aiplatform.googleapis.com" to "https://console.cloud.google.com/iam-admin/serviceaccounts",
"api.openai.com" to "https://platform.openai.com/api-keys",
"api.x.ai" to "https://console.x.ai/",
"api.mistral.ai" to "https://console.mistral.ai/api-keys",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ object ProviderPresets {
ProviderPreset("OpenCode Go", ProviderConfig.Kind.OpenAiCompat, "https://opencode.ai/zen/go/v1", caps(), sessionHeader = "x-opencode-session"),
ProviderPreset("Anthropic", ProviderConfig.Kind.Anthropic, "https://api.anthropic.com", caps(vision = true, context = 200_000), familyTokens = listOf("claude")),
ProviderPreset("Gemini", ProviderConfig.Kind.Gemini, "https://generativelanguage.googleapis.com", caps(vision = true, context = 128_000), familyTokens = listOf("gemini")),
ProviderPreset("Vertex AI", ProviderConfig.Kind.Vertex, "https://aiplatform.googleapis.com", caps(vision = true, context = 1_000_000, caching = PromptCaching.Implicit), familyTokens = listOf("gemini")),
// 官方 API 走 Responses:新的主力模型调用工具必须用 Responses,Chat Completions 带工具会被拒。列模型同样是 GET /models
ProviderPreset("OpenAI", ProviderConfig.Kind.OpenAiResponses, "https://api.openai.com/v1", caps(vision = true, context = 400_000), familyTokens = listOf("gpt")),
ProviderPreset("xAI", ProviderConfig.Kind.OpenAiCompat, "https://api.x.ai/v1", caps(vision = true, context = 256_000), familyTokens = listOf("grok")),
Expand Down
73 changes: 70 additions & 3 deletions app/src/main/java/com/wangzan101/mika/settings/ProviderStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ import com.wangzan101.mika.core.model.Author
import com.wangzan101.mika.core.model.ChatModel
import com.wangzan101.mika.core.model.ClientHeaders
import com.wangzan101.mika.core.model.GeminiModel
import com.wangzan101.mika.core.model.VertexModel
import com.wangzan101.mika.core.oauth.VertexEndpoints
import com.wangzan101.mika.core.oauth.VertexServiceAccount
import com.wangzan101.mika.core.oauth.VertexTokenProvider
import com.wangzan101.mika.core.model.GeminiTtsModel
import com.wangzan101.mika.core.model.OpenAiSpeechModel
import com.wangzan101.mika.core.model.TtsCapabilities
Expand Down Expand Up @@ -122,7 +126,7 @@ data class ProviderConfig(
* [Xai] 同样是账号登录(设备码),凭据是登录不是 API Key,调用 xAI 自己的 Responses 接口。
*/
enum class Kind {
OpenAiCompat, Anthropic, Gemini, OpenAiResponses, ChatGpt, Xai;
OpenAiCompat, Anthropic, Gemini, Vertex, OpenAiResponses, ChatGpt, Xai;

/**
* 这个接口类型发请求走哪种协议。用它筛掉接口写明了不支持的模型,见 [com.wangzan101.mika.ui.ModelSetupViewModel]
Expand All @@ -133,7 +137,7 @@ data class ProviderConfig(
get() = when (this) {
OpenAiCompat -> ModelProtocol.ChatCompletions
Anthropic -> ModelProtocol.Messages
Gemini -> ModelProtocol.GenerateContent
Gemini, Vertex -> ModelProtocol.GenerateContent
OpenAiResponses, ChatGpt, Xai -> ModelProtocol.Responses
}

Expand Down Expand Up @@ -189,6 +193,10 @@ data class Provider(
val serviceTiers: Map<String, OpenAiServiceTier> = emptyMap(),
/** 用户标为 TTS 的模型名,独立于对话用途和服务商模型列表。 */
val ttsModels: Set<String> = emptySet(),
/** Vertex AI GCP project ID (optional when embedded in Service Account JSON). */
val projectId: String = "",
/** Vertex AI location (blank defaults to `global`). */
val location: String = "",
) {
val key: String get() = ProviderConnection.keyOf(kind, baseUrl, name)

Expand Down Expand Up @@ -1586,6 +1594,35 @@ class ProviderStore(
ProviderConfig.Kind.Xai ->
ResponsesModel(baseUrl, accountTokensFor(cfg.providerId), cfg.model, cfg.capabilities, http, headers = setup.headers,
reasoningEffort = setup.reasoningEffort, accountLogin = XAI_ACCOUNT)
ProviderConfig.Kind.Vertex -> {
val p = provider(cfg.providerId)
val raw = apiKey.orEmpty().trim()
val token: BearerToken = if (raw.startsWith("{")) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Preserve service-account JSON before selecting the token provider

When a user pastes a service-account JSON, probe() and apiKey() first pass it through KeyPoolManager.parseKeys(), which splits on commas and newlines. raw here is therefore a field fragment rather than the JSON object, so this branch selects BearerToken.fixed and sends that fragment as a bearer token instead of using VertexTokenProvider; initial testing and subsequent chats with service-account credentials fail. Preserve or detect the raw JSON before key-pool parsing.

Useful? React with 👍 / 👎.

VertexTokenProvider(http, loadServiceAccountJson = { providerKey(cfg.providerId)?.trim()?.takeIf { it.isNotEmpty() } ?: raw })
} else {
BearerToken.fixed(raw)
}
val parsedProject = if (raw.startsWith("{")) {
runCatching { VertexServiceAccount.parse(raw).projectId }.getOrDefault("")
} else ""
val project = p?.projectId?.trim().orEmpty().ifBlank { parsedProject }

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Collect a project ID for pasted OAuth bearer tokens

When a user supplies an OAuth/ADC bearer token instead of service-account JSON, parsedProject is empty and a newly connected provider keeps the blank default projectId; the setup UI has no field that populates it. This passes an empty ID to VertexModel, whose constructor rejects it before making a request, so bearer-token users cannot test or use models. Collect a project ID or provide another reliable source for it.

Useful? React with 👍 / 👎.

val location = VertexEndpoints.normalizeLocation(p?.location.orEmpty())
val customBase = baseUrl.trim().trimEnd('/').takeIf {
it.isNotEmpty() && !it.equals("https://aiplatform.googleapis.com", ignoreCase = true)
}
VertexModel(
projectId = project,
location = location,
token = token,
model = cfg.model,
capabilities = cfg.capabilities,
http = http,
headers = setup.headers,
maxOutputTokens = setup.maxOutputTokens,
reasoningEffort = setup.reasoningEffort,
baseUrl = customBase,
)
}
}
}

Expand Down Expand Up @@ -1758,6 +1795,8 @@ class ProviderStore(
}.toSet(),
// 旧版本存下的没有这一项:那时每一家都走过「测试并保存」,按测过算
savedUntested = o["untested"]?.jsonPrimitive?.booleanOrNull ?: false,
projectId = o["projectId"]?.jsonPrimitive?.content.orEmpty(),
location = o["location"]?.jsonPrimitive?.content.orEmpty(),
models = o["models"]?.jsonArray?.map { e ->
val m = e.jsonObject
ListedModel(
Expand Down Expand Up @@ -1883,6 +1922,8 @@ class ProviderStore(
for ((model, tier) in p.serviceTiers) put(model, tier.requestValue)
}
if (p.savedUntested) put("untested", true)
if (p.projectId.isNotEmpty()) put("projectId", p.projectId)
if (p.location.isNotEmpty()) put("location", p.location)
if (p.verifiedModels.isNotEmpty()) putJsonArray("verifiedModels") { p.verifiedModels.forEach { add(it) } }
if (p.contextSettings.isNotEmpty()) putJsonObject("contextSettings") {
for ((model, settings) in p.contextSettings) putJsonObject(model) {
Expand Down Expand Up @@ -2066,14 +2107,40 @@ class ProviderStore(
catalog.openAiCompat(baseUrl, apiKey, headers)
ProviderConfig.Kind.Anthropic -> catalog.anthropic(baseUrl, apiKey.orEmpty(), headers)
ProviderConfig.Kind.Gemini -> catalog.gemini(baseUrl, apiKey.orEmpty(), headers)
ProviderConfig.Kind.Vertex -> {
val raw = apiKey.orEmpty().trim()
val parsedSa = if (raw.startsWith("{")) runCatching { VertexServiceAccount.parse(raw) }.getOrNull() else null
val token: BearerToken = if (parsedSa != null) {
VertexTokenProvider(shortHttp, parsedSa)
} else {
BearerToken.fixed(raw)
}
val customBase = baseUrl.trim().trimEnd('/').takeIf {
it.isNotEmpty() && !it.equals("https://aiplatform.googleapis.com", ignoreCase = true)
}
val res = catalog.vertex(
token = token,
projectId = parsedSa?.projectId?.takeIf { it.isNotBlank() },
headers = headers,
baseUrl = customBase,
)
when (res) {
is ModelListResult.Ok -> {
val seen = res.models.map { it.id }.toSet()
val merged = res.models + ModelCatalog.CURATED_VERTEX_MODELS.filter { it.id !in seen }
ModelListResult.Ok(merged)
}
is ModelListResult.Failed -> res
}
}
}

/**
* 调用这个模型用哪种适配器、交给它什么 Base URL。建模型([buildModel])和给思考档位([ReasoningOptions])都按它。
* 订阅登录(xAI)固定走这一家的接口,不跟模型列表里写的别的协议走,免得把订阅转去另一个接口。
*/
internal fun adapterFor(cfg: ProviderConfig, endpoint: ModelEndpoint?): Pair<ProviderConfig.Kind, String> =
if (endpoint == null || cfg.kind == ProviderConfig.Kind.Xai) cfg.kind to cfg.baseUrl
if (endpoint == null || cfg.kind == ProviderConfig.Kind.Xai || cfg.kind == ProviderConfig.Kind.Vertex) cfg.kind to cfg.baseUrl
else ProviderConfig.Kind.of(endpoint.protocol) to endpoint.adapterBaseUrl

/** 产品里的那份(App 建):配置在 files/settings 下,Key 用 Android 密钥库加密,用量记到 [usage]。 */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ data class ReasoningOptions(val levels: List<String>, val fromProvider: Boolean)
ProviderPresets.forBaseUrl(config.baseUrl)?.reasoningLevels
?.takeIf { it.isNotEmpty() }?.let { ReasoningOptions(it, fromProvider = false) }
?: ReasoningOptions(manualLevels, fromProvider = false)
ProviderConfig.Kind.Anthropic, ProviderConfig.Kind.Gemini -> null
ProviderConfig.Kind.Anthropic, ProviderConfig.Kind.Gemini, ProviderConfig.Kind.Vertex -> null
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1376,7 +1376,7 @@ object ModelSetupCopy {
/** 手填模型名时举的例子,按接口类型(各家模型名的样子不同)。 */
fun modelExample(kind: ProviderConfig.Kind): String = tr(
when (kind) {
ProviderConfig.Kind.Gemini -> R.string.model_example_gemini
ProviderConfig.Kind.Gemini, ProviderConfig.Kind.Vertex -> R.string.model_example_gemini
ProviderConfig.Kind.Anthropic -> R.string.model_example_anthropic
ProviderConfig.Kind.OpenAiResponses, ProviderConfig.Kind.ChatGpt -> R.string.model_example_openai
ProviderConfig.Kind.Xai -> R.string.model_example_grok
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/com/wangzan101/mika/ui/ModelsScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,7 @@ object ModelsCopy {
ProviderConfig.Kind.OpenAiCompat -> tr(R.string.models_openai_compat)
ProviderConfig.Kind.Anthropic -> "Anthropic"
ProviderConfig.Kind.Gemini -> "Gemini"
ProviderConfig.Kind.Vertex -> "Vertex AI"
ProviderConfig.Kind.OpenAiResponses -> "OpenAI Responses"
ProviderConfig.Kind.ChatGpt -> "ChatGPT"
ProviderConfig.Kind.Xai -> "xAI Responses"
Expand Down
Loading
Loading