feat(ai): support auth methods
This commit is contained in:
1 parent
8599ab8200
commit
8df9484447
4 files changed
+105
-6
No files matched your search
@@ -24,6 +24,7 @@ import {
|
|||||||
DEFAULT_DESKTOP_SETTINGS,
|
DEFAULT_DESKTOP_SETTINGS,
|
||||||
type AiProvider,
|
type AiProvider,
|
||||||
type AiApiStyle,
|
type AiApiStyle,
|
||||||
|
type AiAuthMethod,
|
||||||
type EditorTheme,
|
type EditorTheme,
|
||||||
type DesktopIconTheme,
|
type DesktopIconTheme,
|
||||||
type DisconnectTabHandlingMode,
|
type DisconnectTabHandlingMode,
|
||||||
@@ -915,6 +916,7 @@ const selectedAiProviderPreset = computed(() => AI_PROVIDER_PRESETS[aiEditProvid
|
|||||||
|
|
||||||
const aiEditProvider = ref<AiProvider>(settingsStore.aiConfig.provider);
|
const aiEditProvider = ref<AiProvider>(settingsStore.aiConfig.provider);
|
||||||
const aiEditApiKey = ref(settingsStore.aiConfig.apiKey);
|
const aiEditApiKey = ref(settingsStore.aiConfig.apiKey);
|
||||||
|
const aiEditAuthMethod = ref<AiAuthMethod>(settingsStore.aiConfig.authMethod || AI_PROVIDER_PRESETS[settingsStore.aiConfig.provider].authMethod);
|
||||||
const aiEditEndpoint = ref(settingsStore.aiConfig.endpoint);
|
const aiEditEndpoint = ref(settingsStore.aiConfig.endpoint);
|
||||||
const aiEditModel = ref(settingsStore.aiConfig.model);
|
const aiEditModel = ref(settingsStore.aiConfig.model);
|
||||||
const aiEditApiStyle = ref<AiApiStyle>(settingsStore.aiConfig.apiStyle || "completions");
|
const aiEditApiStyle = ref<AiApiStyle>(settingsStore.aiConfig.apiStyle || "completions");
|
||||||
@@ -934,6 +936,13 @@ const aiTesting = ref(false);
|
|||||||
const aiTestResult = ref<"" | "success" | "error">("");
|
const aiTestResult = ref<"" | "success" | "error">("");
|
||||||
const aiTestError = ref("");
|
const aiTestError = ref("");
|
||||||
const aiRequiresApiKey = computed(() => AI_PROVIDER_PRESETS[aiEditProvider.value].requiresApiKey);
|
const aiRequiresApiKey = computed(() => AI_PROVIDER_PRESETS[aiEditProvider.value].requiresApiKey);
|
||||||
|
const aiSupportsAuthMethod = computed(() => aiEditProvider.value === "claude");
|
||||||
|
const aiCredentialLabel = computed(() => (aiSupportsAuthMethod.value && aiEditAuthMethod.value === "bearer" ? "Auth Token" : "API Key"));
|
||||||
|
const aiCredentialPlaceholder = computed(() => {
|
||||||
|
if (!aiRequiresApiKey.value) return "Optional";
|
||||||
|
if (aiSupportsAuthMethod.value && aiEditAuthMethod.value === "bearer") return "ANTHROPIC_AUTH_TOKEN";
|
||||||
|
return "";
|
||||||
|
});
|
||||||
const aiSupportsApiStyle = computed(() => aiEditProvider.value === "openai" || aiEditProvider.value === "openai-compatible" || aiEditProvider.value === "custom");
|
const aiSupportsApiStyle = computed(() => aiEditProvider.value === "openai" || aiEditProvider.value === "openai-compatible" || aiEditProvider.value === "custom");
|
||||||
const aiModelListSupported = computed(() => aiEditProvider.value !== "gemini");
|
const aiModelListSupported = computed(() => aiEditProvider.value !== "gemini");
|
||||||
const aiCanListModels = computed(() => aiModelListSupported.value && !!aiEditEndpoint.value.trim() && (!aiRequiresApiKey.value || !!aiEditApiKey.value.trim()));
|
const aiCanListModels = computed(() => aiModelListSupported.value && !!aiEditEndpoint.value.trim() && (!aiRequiresApiKey.value || !!aiEditApiKey.value.trim()));
|
||||||
@@ -957,6 +966,7 @@ function aiModelConfigSignature() {
|
|||||||
provider: aiEditProvider.value,
|
provider: aiEditProvider.value,
|
||||||
endpoint: aiEditEndpoint.value.trim(),
|
endpoint: aiEditEndpoint.value.trim(),
|
||||||
apiKey: aiEditApiKey.value.trim(),
|
apiKey: aiEditApiKey.value.trim(),
|
||||||
|
authMethod: aiEditAuthMethod.value,
|
||||||
proxyEnabled: aiEditProxyEnabled.value,
|
proxyEnabled: aiEditProxyEnabled.value,
|
||||||
proxyUrl: aiEditProxyUrl.value.trim(),
|
proxyUrl: aiEditProxyUrl.value.trim(),
|
||||||
});
|
});
|
||||||
@@ -966,6 +976,7 @@ function currentAiEditConfig() {
|
|||||||
return {
|
return {
|
||||||
provider: aiEditProvider.value,
|
provider: aiEditProvider.value,
|
||||||
apiKey: aiEditApiKey.value,
|
apiKey: aiEditApiKey.value,
|
||||||
|
authMethod: aiEditAuthMethod.value,
|
||||||
endpoint: aiEditEndpoint.value,
|
endpoint: aiEditEndpoint.value,
|
||||||
model: aiEditModel.value,
|
model: aiEditModel.value,
|
||||||
apiStyle: aiEditApiStyle.value,
|
apiStyle: aiEditApiStyle.value,
|
||||||
@@ -1038,6 +1049,7 @@ function aiSelectModel(modelId: string) {
|
|||||||
function syncAiEditState() {
|
function syncAiEditState() {
|
||||||
aiEditProvider.value = settingsStore.aiConfig.provider;
|
aiEditProvider.value = settingsStore.aiConfig.provider;
|
||||||
aiEditApiKey.value = settingsStore.aiConfig.apiKey;
|
aiEditApiKey.value = settingsStore.aiConfig.apiKey;
|
||||||
|
aiEditAuthMethod.value = settingsStore.aiConfig.authMethod || AI_PROVIDER_PRESETS[settingsStore.aiConfig.provider].authMethod;
|
||||||
aiEditEndpoint.value = settingsStore.aiConfig.endpoint;
|
aiEditEndpoint.value = settingsStore.aiConfig.endpoint;
|
||||||
aiEditModel.value = settingsStore.aiConfig.model;
|
aiEditModel.value = settingsStore.aiConfig.model;
|
||||||
aiEditApiStyle.value = settingsStore.aiConfig.apiStyle || "completions";
|
aiEditApiStyle.value = settingsStore.aiConfig.apiStyle || "completions";
|
||||||
@@ -1054,6 +1066,7 @@ function aiSelectProvider(provider: AiProvider) {
|
|||||||
aiEditEndpoint.value = AI_PROVIDER_PRESETS[provider].endpoint;
|
aiEditEndpoint.value = AI_PROVIDER_PRESETS[provider].endpoint;
|
||||||
aiEditModel.value = AI_PROVIDER_PRESETS[provider].model;
|
aiEditModel.value = AI_PROVIDER_PRESETS[provider].model;
|
||||||
aiEditApiStyle.value = AI_PROVIDER_PRESETS[provider].apiStyle;
|
aiEditApiStyle.value = AI_PROVIDER_PRESETS[provider].apiStyle;
|
||||||
|
aiEditAuthMethod.value = AI_PROVIDER_PRESETS[provider].authMethod;
|
||||||
if (!AI_PROVIDER_PRESETS[provider].requiresApiKey) aiEditApiKey.value = "";
|
if (!AI_PROVIDER_PRESETS[provider].requiresApiKey) aiEditApiKey.value = "";
|
||||||
clearAiModelOptions();
|
clearAiModelOptions();
|
||||||
}
|
}
|
||||||
@@ -1062,6 +1075,7 @@ function aiHasChanges(): boolean {
|
|||||||
return (
|
return (
|
||||||
aiEditProvider.value !== settingsStore.aiConfig.provider ||
|
aiEditProvider.value !== settingsStore.aiConfig.provider ||
|
||||||
aiEditApiKey.value !== settingsStore.aiConfig.apiKey ||
|
aiEditApiKey.value !== settingsStore.aiConfig.apiKey ||
|
||||||
|
aiEditAuthMethod.value !== (settingsStore.aiConfig.authMethod || AI_PROVIDER_PRESETS[settingsStore.aiConfig.provider].authMethod) ||
|
||||||
aiEditEndpoint.value !== settingsStore.aiConfig.endpoint ||
|
aiEditEndpoint.value !== settingsStore.aiConfig.endpoint ||
|
||||||
aiEditModel.value !== settingsStore.aiConfig.model ||
|
aiEditModel.value !== settingsStore.aiConfig.model ||
|
||||||
aiEditApiStyle.value !== (settingsStore.aiConfig.apiStyle || "completions") ||
|
aiEditApiStyle.value !== (settingsStore.aiConfig.apiStyle || "completions") ||
|
||||||
@@ -1929,9 +1943,22 @@ watch(
|
|||||||
</Select>
|
</Select>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div v-if="aiSupportsAuthMethod" class="grid grid-cols-3 items-center gap-3">
|
||||||
|
<Label class="text-right text-xs">Authentication</Label>
|
||||||
|
<Select v-model="aiEditAuthMethod">
|
||||||
|
<SelectTrigger class="col-span-2 h-8 text-xs">
|
||||||
|
<SelectValue />
|
||||||
|
</SelectTrigger>
|
||||||
|
<SelectContent>
|
||||||
|
<SelectItem value="api-key">API Key</SelectItem>
|
||||||
|
<SelectItem value="bearer">Auth Token</SelectItem>
|
||||||
|
</SelectContent>
|
||||||
|
</Select>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div class="grid grid-cols-3 items-center gap-3">
|
<div class="grid grid-cols-3 items-center gap-3">
|
||||||
<Label class="text-right text-xs">API Key</Label>
|
<Label class="text-right text-xs">{{ aiCredentialLabel }}</Label>
|
||||||
<Input v-model="aiEditApiKey" type="password" autocomplete="off" class="col-span-2 h-8 text-xs" :placeholder="aiRequiresApiKey ? '' : 'Optional'" />
|
<Input v-model="aiEditApiKey" type="password" autocomplete="off" class="col-span-2 h-8 text-xs" :placeholder="aiCredentialPlaceholder" />
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
<div class="grid grid-cols-3 items-center gap-3">
|
<div class="grid grid-cols-3 items-center gap-3">
|
||||||
|
|||||||
@@ -13,10 +13,12 @@ import { setDebugLoggingEnabled } from "@/lib/debugLog";
|
|||||||
|
|
||||||
export type AiProvider = "claude" | "openai" | "gemini" | "deepseek" | "qwen" | "ollama" | "openai-compatible" | "custom";
|
export type AiProvider = "claude" | "openai" | "gemini" | "deepseek" | "qwen" | "ollama" | "openai-compatible" | "custom";
|
||||||
export type AiApiStyle = "completions" | "responses";
|
export type AiApiStyle = "completions" | "responses";
|
||||||
|
export type AiAuthMethod = "api-key" | "bearer";
|
||||||
|
|
||||||
export interface AiConfig {
|
export interface AiConfig {
|
||||||
provider: AiProvider;
|
provider: AiProvider;
|
||||||
apiKey: string;
|
apiKey: string;
|
||||||
|
authMethod: AiAuthMethod;
|
||||||
endpoint: string;
|
endpoint: string;
|
||||||
model: string;
|
model: string;
|
||||||
apiStyle: AiApiStyle;
|
apiStyle: AiApiStyle;
|
||||||
@@ -65,6 +67,7 @@ export const AI_PROVIDER_PRESETS: Record<AiProvider, AiProviderPreset> = {
|
|||||||
endpoint: "https://api.anthropic.com/v1/messages",
|
endpoint: "https://api.anthropic.com/v1/messages",
|
||||||
model: "claude-sonnet-4-20250514",
|
model: "claude-sonnet-4-20250514",
|
||||||
apiStyle: "completions",
|
apiStyle: "completions",
|
||||||
|
authMethod: "api-key",
|
||||||
requiresApiKey: true,
|
requiresApiKey: true,
|
||||||
},
|
},
|
||||||
openai: {
|
openai: {
|
||||||
@@ -74,6 +77,7 @@ export const AI_PROVIDER_PRESETS: Record<AiProvider, AiProviderPreset> = {
|
|||||||
endpoint: "https://api.openai.com/v1/chat/completions",
|
endpoint: "https://api.openai.com/v1/chat/completions",
|
||||||
model: "gpt-4o-mini",
|
model: "gpt-4o-mini",
|
||||||
apiStyle: "completions",
|
apiStyle: "completions",
|
||||||
|
authMethod: "bearer",
|
||||||
requiresApiKey: true,
|
requiresApiKey: true,
|
||||||
},
|
},
|
||||||
gemini: {
|
gemini: {
|
||||||
@@ -83,6 +87,7 @@ export const AI_PROVIDER_PRESETS: Record<AiProvider, AiProviderPreset> = {
|
|||||||
endpoint: "https://generativelanguage.googleapis.com",
|
endpoint: "https://generativelanguage.googleapis.com",
|
||||||
model: "gemini-1.5-pro",
|
model: "gemini-1.5-pro",
|
||||||
apiStyle: "completions",
|
apiStyle: "completions",
|
||||||
|
authMethod: "api-key",
|
||||||
requiresApiKey: true,
|
requiresApiKey: true,
|
||||||
},
|
},
|
||||||
deepseek: {
|
deepseek: {
|
||||||
@@ -92,6 +97,7 @@ export const AI_PROVIDER_PRESETS: Record<AiProvider, AiProviderPreset> = {
|
|||||||
endpoint: "https://api.deepseek.com/v1",
|
endpoint: "https://api.deepseek.com/v1",
|
||||||
model: "deepseek-v4-flash",
|
model: "deepseek-v4-flash",
|
||||||
apiStyle: "completions",
|
apiStyle: "completions",
|
||||||
|
authMethod: "bearer",
|
||||||
requiresApiKey: true,
|
requiresApiKey: true,
|
||||||
},
|
},
|
||||||
qwen: {
|
qwen: {
|
||||||
@@ -101,6 +107,7 @@ export const AI_PROVIDER_PRESETS: Record<AiProvider, AiProviderPreset> = {
|
|||||||
endpoint: "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
endpoint: "https://dashscope.aliyuncs.com/compatible-mode/v1",
|
||||||
model: "qwen-plus",
|
model: "qwen-plus",
|
||||||
apiStyle: "completions",
|
apiStyle: "completions",
|
||||||
|
authMethod: "bearer",
|
||||||
requiresApiKey: true,
|
requiresApiKey: true,
|
||||||
},
|
},
|
||||||
ollama: {
|
ollama: {
|
||||||
@@ -110,6 +117,7 @@ export const AI_PROVIDER_PRESETS: Record<AiProvider, AiProviderPreset> = {
|
|||||||
endpoint: "http://localhost:11434/v1",
|
endpoint: "http://localhost:11434/v1",
|
||||||
model: "llama3.1",
|
model: "llama3.1",
|
||||||
apiStyle: "completions",
|
apiStyle: "completions",
|
||||||
|
authMethod: "bearer",
|
||||||
requiresApiKey: false,
|
requiresApiKey: false,
|
||||||
},
|
},
|
||||||
"openai-compatible": {
|
"openai-compatible": {
|
||||||
@@ -119,6 +127,7 @@ export const AI_PROVIDER_PRESETS: Record<AiProvider, AiProviderPreset> = {
|
|||||||
endpoint: "",
|
endpoint: "",
|
||||||
model: "",
|
model: "",
|
||||||
apiStyle: "completions",
|
apiStyle: "completions",
|
||||||
|
authMethod: "bearer",
|
||||||
requiresApiKey: true,
|
requiresApiKey: true,
|
||||||
},
|
},
|
||||||
custom: {
|
custom: {
|
||||||
@@ -127,6 +136,7 @@ export const AI_PROVIDER_PRESETS: Record<AiProvider, AiProviderPreset> = {
|
|||||||
endpoint: "",
|
endpoint: "",
|
||||||
model: "",
|
model: "",
|
||||||
apiStyle: "completions",
|
apiStyle: "completions",
|
||||||
|
authMethod: "bearer",
|
||||||
requiresApiKey: true,
|
requiresApiKey: true,
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
@@ -146,6 +156,7 @@ export function normalizeAiConfig(config: Partial<AiConfig> | null | undefined):
|
|||||||
...config,
|
...config,
|
||||||
provider,
|
provider,
|
||||||
apiStyle: config?.apiStyle ?? defaultConfigs[provider].apiStyle,
|
apiStyle: config?.apiStyle ?? defaultConfigs[provider].apiStyle,
|
||||||
|
authMethod: config?.authMethod ?? defaultConfigs[provider].authMethod,
|
||||||
proxyEnabled: !!config?.proxyEnabled,
|
proxyEnabled: !!config?.proxyEnabled,
|
||||||
proxyUrl: config?.proxyUrl ?? "",
|
proxyUrl: config?.proxyUrl ?? "",
|
||||||
enableThinking: config?.enableThinking ?? true,
|
enableThinking: config?.enableThinking ?? true,
|
||||||
|
|||||||
@@ -60,6 +60,14 @@ pub enum AiApiStyle {
|
|||||||
Responses,
|
Responses,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Default)]
|
||||||
|
#[serde(rename_all = "kebab-case")]
|
||||||
|
pub enum AiAuthMethod {
|
||||||
|
#[default]
|
||||||
|
ApiKey,
|
||||||
|
Bearer,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "camelCase")]
|
#[serde(rename_all = "camelCase")]
|
||||||
pub struct AiConfig {
|
pub struct AiConfig {
|
||||||
@@ -67,6 +75,8 @@ pub struct AiConfig {
|
|||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub api_key: String,
|
pub api_key: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
pub auth_method: AiAuthMethod,
|
||||||
|
#[serde(default)]
|
||||||
pub endpoint: String,
|
pub endpoint: String,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
pub model: String,
|
pub model: String,
|
||||||
@@ -393,7 +403,17 @@ fn maybe_bearer_headers(config: &AiConfig) -> Result<HeaderMap, String> {
|
|||||||
fn claude_headers(config: &AiConfig) -> Result<HeaderMap, String> {
|
fn claude_headers(config: &AiConfig) -> Result<HeaderMap, String> {
|
||||||
let mut headers = HeaderMap::new();
|
let mut headers = HeaderMap::new();
|
||||||
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
|
headers.insert(CONTENT_TYPE, HeaderValue::from_static("application/json"));
|
||||||
headers.insert("x-api-key", HeaderValue::from_str(&config.api_key).map_err(|e| e.to_string())?);
|
match config.auth_method {
|
||||||
|
AiAuthMethod::Bearer => {
|
||||||
|
headers.insert(
|
||||||
|
AUTHORIZATION,
|
||||||
|
HeaderValue::from_str(&format!("Bearer {}", config.api_key)).map_err(|e| e.to_string())?,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
AiAuthMethod::ApiKey => {
|
||||||
|
headers.insert("x-api-key", HeaderValue::from_str(&config.api_key).map_err(|e| e.to_string())?);
|
||||||
|
}
|
||||||
|
}
|
||||||
headers.insert("anthropic-version", HeaderValue::from_static("2023-06-01"));
|
headers.insert("anthropic-version", HeaderValue::from_static("2023-06-01"));
|
||||||
Ok(headers)
|
Ok(headers)
|
||||||
}
|
}
|
||||||
@@ -1130,9 +1150,10 @@ pub fn load_config(path: &Path) -> Result<Option<AiConfig>, String> {
|
|||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::{
|
use super::{
|
||||||
build_ai_http_client, gemini_text, openai_response_text, openai_stream_text, parse_model_list_response,
|
build_ai_http_client, claude_headers, gemini_text, openai_response_text, openai_stream_text,
|
||||||
resolve_endpoint, resolve_model_list_endpoint, responses_max_output_tokens, responses_text,
|
parse_model_list_response, resolve_endpoint, resolve_model_list_endpoint, responses_max_output_tokens,
|
||||||
supports_temperature, validate_config, AiApiStyle, AiConfig, AiModelInfo, AiProvider,
|
responses_text, supports_temperature, validate_config, AiApiStyle, AiAuthMethod, AiConfig, AiModelInfo,
|
||||||
|
AiProvider, AUTHORIZATION,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -1149,6 +1170,7 @@ mod tests {
|
|||||||
assert!(!config.proxy_enabled);
|
assert!(!config.proxy_enabled);
|
||||||
assert_eq!(config.proxy_url, "");
|
assert_eq!(config.proxy_url, "");
|
||||||
assert!(config.enable_thinking);
|
assert!(config.enable_thinking);
|
||||||
|
assert_eq!(config.auth_method, AiAuthMethod::ApiKey);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
@@ -1156,6 +1178,7 @@ mod tests {
|
|||||||
let config = AiConfig {
|
let config = AiConfig {
|
||||||
provider: AiProvider::Openai,
|
provider: AiProvider::Openai,
|
||||||
api_key: "key".to_string(),
|
api_key: "key".to_string(),
|
||||||
|
auth_method: AiAuthMethod::Bearer,
|
||||||
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
|
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
|
||||||
model: "gpt-4o".to_string(),
|
model: "gpt-4o".to_string(),
|
||||||
api_style: AiApiStyle::Completions,
|
api_style: AiApiStyle::Completions,
|
||||||
@@ -1174,6 +1197,7 @@ mod tests {
|
|||||||
let config = AiConfig {
|
let config = AiConfig {
|
||||||
provider: AiProvider::Openai,
|
provider: AiProvider::Openai,
|
||||||
api_key: "key".to_string(),
|
api_key: "key".to_string(),
|
||||||
|
auth_method: AiAuthMethod::Bearer,
|
||||||
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
|
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
|
||||||
model: "gpt-4o".to_string(),
|
model: "gpt-4o".to_string(),
|
||||||
api_style: AiApiStyle::Completions,
|
api_style: AiApiStyle::Completions,
|
||||||
@@ -1190,6 +1214,7 @@ mod tests {
|
|||||||
let config = AiConfig {
|
let config = AiConfig {
|
||||||
provider: AiProvider::OpenaiCompatible,
|
provider: AiProvider::OpenaiCompatible,
|
||||||
api_key: "key".to_string(),
|
api_key: "key".to_string(),
|
||||||
|
auth_method: AiAuthMethod::Bearer,
|
||||||
endpoint: "http://127.0.0.1:3456/v1".to_string(),
|
endpoint: "http://127.0.0.1:3456/v1".to_string(),
|
||||||
model: "gpt-4o".to_string(),
|
model: "gpt-4o".to_string(),
|
||||||
api_style: AiApiStyle::Completions,
|
api_style: AiApiStyle::Completions,
|
||||||
@@ -1206,6 +1231,7 @@ mod tests {
|
|||||||
let gemini = AiConfig {
|
let gemini = AiConfig {
|
||||||
provider: AiProvider::Gemini,
|
provider: AiProvider::Gemini,
|
||||||
api_key: "key".to_string(),
|
api_key: "key".to_string(),
|
||||||
|
auth_method: AiAuthMethod::ApiKey,
|
||||||
endpoint: "https://generativelanguage.googleapis.com".to_string(),
|
endpoint: "https://generativelanguage.googleapis.com".to_string(),
|
||||||
model: "gemini-1.5-pro".to_string(),
|
model: "gemini-1.5-pro".to_string(),
|
||||||
api_style: AiApiStyle::Completions,
|
api_style: AiApiStyle::Completions,
|
||||||
@@ -1222,6 +1248,7 @@ mod tests {
|
|||||||
let ollama = AiConfig {
|
let ollama = AiConfig {
|
||||||
provider: AiProvider::Ollama,
|
provider: AiProvider::Ollama,
|
||||||
api_key: String::new(),
|
api_key: String::new(),
|
||||||
|
auth_method: AiAuthMethod::Bearer,
|
||||||
endpoint: "http://localhost:11434/v1".to_string(),
|
endpoint: "http://localhost:11434/v1".to_string(),
|
||||||
model: "llama3.1".to_string(),
|
model: "llama3.1".to_string(),
|
||||||
api_style: AiApiStyle::Completions,
|
api_style: AiApiStyle::Completions,
|
||||||
@@ -1239,6 +1266,7 @@ mod tests {
|
|||||||
let openai = AiConfig {
|
let openai = AiConfig {
|
||||||
provider: AiProvider::Openai,
|
provider: AiProvider::Openai,
|
||||||
api_key: "key".to_string(),
|
api_key: "key".to_string(),
|
||||||
|
auth_method: AiAuthMethod::Bearer,
|
||||||
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
|
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
|
||||||
model: String::new(),
|
model: String::new(),
|
||||||
api_style: AiApiStyle::Completions,
|
api_style: AiApiStyle::Completions,
|
||||||
@@ -1251,6 +1279,7 @@ mod tests {
|
|||||||
let claude = AiConfig {
|
let claude = AiConfig {
|
||||||
provider: AiProvider::Claude,
|
provider: AiProvider::Claude,
|
||||||
api_key: "key".to_string(),
|
api_key: "key".to_string(),
|
||||||
|
auth_method: AiAuthMethod::ApiKey,
|
||||||
endpoint: "https://api.anthropic.com/v1/messages".to_string(),
|
endpoint: "https://api.anthropic.com/v1/messages".to_string(),
|
||||||
model: String::new(),
|
model: String::new(),
|
||||||
api_style: AiApiStyle::Completions,
|
api_style: AiApiStyle::Completions,
|
||||||
@@ -1261,6 +1290,30 @@ mod tests {
|
|||||||
assert_eq!(resolve_model_list_endpoint(&claude).unwrap(), "https://api.anthropic.com/v1/models");
|
assert_eq!(resolve_model_list_endpoint(&claude).unwrap(), "https://api.anthropic.com/v1/models");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn claude_headers_support_api_key_and_bearer_auth() {
|
||||||
|
let mut config = AiConfig {
|
||||||
|
provider: AiProvider::Claude,
|
||||||
|
api_key: "secret".to_string(),
|
||||||
|
auth_method: AiAuthMethod::ApiKey,
|
||||||
|
endpoint: "https://api.anthropic.com/v1/messages".to_string(),
|
||||||
|
model: "claude-sonnet-4-20250514".to_string(),
|
||||||
|
api_style: AiApiStyle::Completions,
|
||||||
|
proxy_enabled: false,
|
||||||
|
proxy_url: String::new(),
|
||||||
|
enable_thinking: true,
|
||||||
|
};
|
||||||
|
|
||||||
|
let api_key_headers = claude_headers(&config).unwrap();
|
||||||
|
assert_eq!(api_key_headers.get("x-api-key").unwrap(), "secret");
|
||||||
|
assert!(api_key_headers.get(AUTHORIZATION).is_none());
|
||||||
|
|
||||||
|
config.auth_method = AiAuthMethod::Bearer;
|
||||||
|
let bearer_headers = claude_headers(&config).unwrap();
|
||||||
|
assert_eq!(bearer_headers.get(AUTHORIZATION).unwrap(), "Bearer secret");
|
||||||
|
assert!(bearer_headers.get("x-api-key").is_none());
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn parses_openai_and_claude_model_list_items() {
|
fn parses_openai_and_claude_model_list_items() {
|
||||||
let data = serde_json::json!({
|
let data = serde_json::json!({
|
||||||
@@ -1297,6 +1350,7 @@ mod tests {
|
|||||||
let mut config = AiConfig {
|
let mut config = AiConfig {
|
||||||
provider: AiProvider::Openai,
|
provider: AiProvider::Openai,
|
||||||
api_key: "key".to_string(),
|
api_key: "key".to_string(),
|
||||||
|
auth_method: AiAuthMethod::Bearer,
|
||||||
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
|
endpoint: "https://api.openai.com/v1/chat/completions".to_string(),
|
||||||
model: "gpt-5.5".to_string(),
|
model: "gpt-5.5".to_string(),
|
||||||
api_style: AiApiStyle::Completions,
|
api_style: AiApiStyle::Completions,
|
||||||
|
|||||||
@@ -239,6 +239,8 @@ test("AI provider presets include common hosted and local providers", () => {
|
|||||||
assert.equal(AI_PROVIDER_PRESETS.qwen.endpoint, "https://dashscope.aliyuncs.com/compatible-mode/v1");
|
assert.equal(AI_PROVIDER_PRESETS.qwen.endpoint, "https://dashscope.aliyuncs.com/compatible-mode/v1");
|
||||||
assert.equal(AI_PROVIDER_PRESETS.ollama.endpoint, "http://localhost:11434/v1");
|
assert.equal(AI_PROVIDER_PRESETS.ollama.endpoint, "http://localhost:11434/v1");
|
||||||
assert.equal(AI_PROVIDER_PRESETS.ollama.requiresApiKey, false);
|
assert.equal(AI_PROVIDER_PRESETS.ollama.requiresApiKey, false);
|
||||||
|
assert.equal(AI_PROVIDER_PRESETS.claude.authMethod, "api-key");
|
||||||
|
assert.equal(AI_PROVIDER_PRESETS.openai.authMethod, "bearer");
|
||||||
assert.equal(AI_PROVIDER_PRESETS.openai.iconSlug, "openai");
|
assert.equal(AI_PROVIDER_PRESETS.openai.iconSlug, "openai");
|
||||||
assert.equal(AI_PROVIDER_PRESETS.deepseek.iconSlug, "deepseek");
|
assert.equal(AI_PROVIDER_PRESETS.deepseek.iconSlug, "deepseek");
|
||||||
});
|
});
|
||||||
@@ -254,11 +256,16 @@ test("normalizes legacy AI config and fills provider defaults", () => {
|
|||||||
assert.equal(legacy.apiStyle, "completions");
|
assert.equal(legacy.apiStyle, "completions");
|
||||||
assert.equal(legacy.provider, "openai");
|
assert.equal(legacy.provider, "openai");
|
||||||
assert.equal(legacy.apiKey, "key");
|
assert.equal(legacy.apiKey, "key");
|
||||||
|
assert.equal(legacy.authMethod, "bearer");
|
||||||
|
|
||||||
const ollama = normalizeAiConfig({ provider: "ollama" } as any);
|
const ollama = normalizeAiConfig({ provider: "ollama" } as any);
|
||||||
assert.equal(ollama.endpoint, "http://localhost:11434/v1");
|
assert.equal(ollama.endpoint, "http://localhost:11434/v1");
|
||||||
assert.equal(ollama.model, "llama3.1");
|
assert.equal(ollama.model, "llama3.1");
|
||||||
assert.equal(ollama.apiKey, "");
|
assert.equal(ollama.apiKey, "");
|
||||||
|
assert.equal(ollama.authMethod, "bearer");
|
||||||
|
|
||||||
|
const claudeToken = normalizeAiConfig({ provider: "claude", apiKey: "token", authMethod: "bearer" } as any);
|
||||||
|
assert.equal(claudeToken.authMethod, "bearer");
|
||||||
});
|
});
|
||||||
|
|
||||||
test("infers legacy AI provider from saved endpoint and model", () => {
|
test("infers legacy AI provider from saved endpoint and model", () => {
|
||||||
|
|||||||
Reference in new issue
Block a user