Skip to content

Commit

Permalink
Optimize ChatView functionality and improve provider name handling in…
Browse files Browse the repository at this point in the history
… AIService
  • Loading branch information
曾凯 committed Dec 16, 2024
1 parent 3c60e96 commit 0fd5070
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
27 changes: 15 additions & 12 deletions src/getPromptActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
<Action.Push
title={`Call ${providerName}`}
icon={Icon.AddPerson}
target={<ChatView getFormattedDescription={getFormattedDescription} providerName={providerName} />}
/>
),
}));
return aiService.getProviderNames().map(providerName => {
const displayName = `Call ${providerName}`;
return {
name: providerName.toLowerCase(),
displayName,
condition: true,
action: (
<Action.Push
title={displayName}
icon={Icon.AddPerson}
target={<ChatView getFormattedDescription={getFormattedDescription} providerName={providerName} />}
/>
),
};
});
})(),
{
name: "openURL",
Expand Down
22 changes: 17 additions & 5 deletions src/services/AIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -32,7 +31,7 @@ interface Preferences {
export class AIService {
private static instance: AIService;
private providers: Map<string, AIProvider>;
private currentProvider: AIProvider;
private currentProvider!: AIProvider;
private config: Config;

private constructor() {
Expand Down Expand Up @@ -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(', ')}`);
}
Expand Down

0 comments on commit 0fd5070

Please sign in to comment.