Skip to content

Commit

Permalink
Added AI config path preference and updated AIService to load config …
Browse files Browse the repository at this point in the history
…from file
  • Loading branch information
曾凯 committed Dec 16, 2024
1 parent a32b36c commit 4ed6a62
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 34 deletions.
7 changes: 7 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,13 @@
"type": "textfield",
"required": false,
"label": "Custom Words List"
},
{
"name": "aiConfigPath",
"title": "API Provider",
"description": "The API provider to use for AI services",
"type": "file",
"required": false
}
],
"dependencies": {
Expand Down
78 changes: 44 additions & 34 deletions src/services/AIService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ConfigurableProvider } from "./providers/configurable";
import type { Provider } from "./providers/base";
import * as fs from "fs";
import * as path from "path";
import { environment } from "@raycast/api";
import { environment, getPreferenceValues } from "@raycast/api";

interface ProviderConfig {
apiKey: string;
Expand All @@ -25,6 +25,10 @@ interface Config {
};
}

interface Preferences {
aiConfigPath?: string;
}

export class AIService {
private static instance: AIService;
private providers: Map<string, AIProvider>;
Expand All @@ -35,49 +39,55 @@ export class AIService {
this.providers = new Map();
this.config = this.loadConfig();

if (!this.config.providers || Object.keys(this.config.providers).length === 0) {
throw new Error('No providers found in config');
}

for (const [providerName, providerConfig] of Object.entries(this.config.providers)) {
try {
const provider = new ConfigurableProvider(
providerName,
providerConfig.apiEndpoint,
providerConfig.provider,
providerConfig.model,
[providerConfig.model],
providerConfig.apiKey
);
this.providers.set(providerName, provider);
console.log(`Provider ${providerName} initialized successfully`);
} catch (error) {
console.error(`Failed to initialize provider ${providerName}:`, error);
if (this.config.providers && Object.keys(this.config.providers).length > 0) {
for (const [providerName, providerConfig] of Object.entries(this.config.providers)) {
try {
const provider = new ConfigurableProvider(
providerName,
providerConfig.apiEndpoint,
providerConfig.provider,
providerConfig.model,
[providerConfig.model],
providerConfig.apiKey
);
this.providers.set(providerName, provider);
console.log(`Provider ${providerName} initialized successfully`);
} catch (error) {
console.error(`Failed to initialize provider ${providerName}:`, error);
}
}
}

if (this.providers.size === 0) {
throw new Error('No providers could be initialized');
}
if (this.providers.size > 0) {
const activeProvider = this.getProvider(this.config.activeProvider);
if (!activeProvider) {
console.warn(`Active provider ${this.config.activeProvider} not found, using first available provider`);
this.currentProvider = this.providers.values().next().value;
} else {
this.currentProvider = activeProvider;
}

const activeProvider = this.getProvider(this.config.activeProvider);
if (!activeProvider) {
console.warn(`Active provider ${this.config.activeProvider} not found, using first available provider`);
this.currentProvider = this.providers.values().next().value;
console.log('Available providers:', this.getProviderNames());
console.log('Current provider:', this.currentProvider.name);
} else {
this.currentProvider = activeProvider;
console.log('No providers configured');
}

console.log('Available providers:', this.getProviderNames());
console.log('Current provider:', this.currentProvider.name);
}

private loadConfig(): Config {
try {
const configPath = path.join(environment.assetsPath, "config.json");
if (fs.existsSync(configPath)) {
console.log('Loading config from:', configPath);
const configData = fs.readFileSync(configPath, "utf-8");
const preferences = getPreferenceValues<Preferences>();

if (!preferences.aiConfigPath) {
return {
activeProvider: "",
providers: {}
};
}

if (fs.existsSync(preferences.aiConfigPath)) {
console.log('Loading config from:', preferences.aiConfigPath);
const configData = fs.readFileSync(preferences.aiConfigPath, "utf-8");
const config = JSON.parse(configData) as Config;
if (this.validateConfig(config)) {
return config;
Expand Down

0 comments on commit 4ed6a62

Please sign in to comment.