From 0fd50707d3dc195dd31416ba30de7c2f2e8d96d0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9B=BE=E5=87=AF?= Date: Mon, 16 Dec 2024 19:19:19 +0800 Subject: [PATCH] Optimize ChatView functionality and improve provider name handling in AIService --- src/getPromptActions.tsx | 27 +++++++++++++++------------ src/services/AIService.ts | 22 +++++++++++++++++----- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/getPromptActions.tsx b/src/getPromptActions.tsx index 1bfb405..f7fd86a 100644 --- a/src/getPromptActions.tsx +++ b/src/getPromptActions.tsx @@ -246,18 +246,21 @@ export function getPromptActions( // 动态生成AI服务的actions ...(() => { const aiService = AIService.getInstance(); - return aiService.getProviderNames().map(providerName => ({ - name: providerName, - displayName: `Call ${providerName}`, - condition: true, - action: ( - } - /> - ), - })); + return aiService.getProviderNames().map(providerName => { + const displayName = `Call ${providerName}`; + return { + name: providerName.toLowerCase(), + displayName, + condition: true, + action: ( + } + /> + ), + }; + }); })(), { name: "openURL", diff --git a/src/services/AIService.ts b/src/services/AIService.ts index e41294a..81779de 100644 --- a/src/services/AIService.ts +++ b/src/services/AIService.ts @@ -2,8 +2,7 @@ import type { AIProvider, ChatOptions, ChatResponse } from "./types"; import { ConfigurableProvider } from "./providers/configurable"; import type { Provider } from "./providers/base"; import * as fs from "fs"; -import * as path from "path"; -import { environment, getPreferenceValues } from "@raycast/api"; +import { getPreferenceValues } from "@raycast/api"; interface ProviderConfig { apiKey: string; @@ -32,7 +31,7 @@ interface Preferences { export class AIService { private static instance: AIService; private providers: Map; - private currentProvider: AIProvider; + private currentProvider!: AIProvider; private config: Config; private constructor() { @@ -124,13 +123,26 @@ export class AIService { getProvider(name: string): AIProvider | undefined { const normalizedName = name.toLowerCase(); console.log('Getting provider:', normalizedName, 'Available:', this.getProviderNames()); - return this.providers.get(normalizedName); + for (const [key, provider] of this.providers.entries()) { + if (key.toLowerCase() === normalizedName) { + return provider; + } + } + return undefined; } setCurrentProvider(name: string): void { const normalizedName = name.toLowerCase(); console.log('Setting provider:', normalizedName, 'Available:', this.getProviderNames()); - const provider = this.providers.get(normalizedName); + let provider: AIProvider | undefined; + + for (const [key, p] of this.providers.entries()) { + if (key.toLowerCase() === normalizedName) { + provider = p; + break; + } + } + if (!provider) { throw new Error(`Provider ${name} not found. Available providers: ${this.getProviderNames().join(', ')}`); }