Commit 188aa128 authored by nanahira's avatar nanahira

refa provider

parent f227084d
...@@ -64,7 +64,7 @@ export class AccountPoolService ...@@ -64,7 +64,7 @@ export class AccountPoolService
this.ChatGPTAPIBrowserConstructor = ( this.ChatGPTAPIBrowserConstructor = (
await eval("import('chatgpt3')") await eval("import('chatgpt3')")
).ChatGPTAPIBrowser; ).ChatGPTAPIBrowser;
this.accountInfos = await this.accountProvider.getAccounts(); this.accountInfos = await this.accountProvider.get();
this.accountInfos.forEach((a) => this.initAccount(a)); this.accountInfos.forEach((a) => this.initAccount(a));
} }
......
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { OpenAIAccount } from '../utility/config'; import { OpenAIAccount } from '../utility/config';
import { fetchDatasource } from '../datasource/datasource'; import { BaseProvider } from '../datasource/base-provider';
import { ConfigService } from '@nestjs/config';
@Injectable() @Injectable()
export class AccountProviderService { export class AccountProviderService extends BaseProvider<OpenAIAccount[]> {
constructor(private config: ConfigService) {} constructor(config: ConfigService) {
async getAccounts() { super(config, 'accounts');
return (
(await fetchDatasource<OpenAIAccount[]>(this.config.get('accounts'))) ||
[]
);
} }
async writeBack( async writeBack(
accounts: OpenAIAccount[], accounts: OpenAIAccount[],
type: 'add' | 'remove', type: 'add' | 'remove',
......
import { ConfigService } from '@nestjs/config';
import { DatasourceConfig, Datasources } from './datasource';
export class BaseProvider<T> {
constructor(protected config: ConfigService, protected key: string) {}
async get(): Promise<T> {
const config = this.config.get<DatasourceConfig<T>>(this.key);
if (!config) {
return;
}
if (Array.isArray(config)) {
// legacy config
return config as T;
}
const Datasource = Datasources.get(config.type);
if (!Datasource) {
throw new Error(`Unknown datasource type: ${config.type}`);
}
return new Datasource(config.config).getData();
}
}
...@@ -6,7 +6,10 @@ export interface DatasourceConfigMap<T> { ...@@ -6,7 +6,10 @@ export interface DatasourceConfigMap<T> {
static: T; static: T;
api: { endpoint: string; headers: Record<string, string | number> }; api: { endpoint: string; headers: Record<string, string | number> };
} }
export interface DatasourceConfig<T, K extends keyof DatasourceConfigMap<T>> { export interface DatasourceConfig<
T,
K extends keyof DatasourceConfigMap<T> = keyof DatasourceConfigMap<T>,
> {
type: K; type: K;
config: DatasourceConfigMap<T>[K]; config: DatasourceConfigMap<T>[K];
} }
...@@ -20,20 +23,3 @@ export const Datasources = new Map< ...@@ -20,20 +23,3 @@ export const Datasources = new Map<
Datasources.set('static', StaticDatasource as any); Datasources.set('static', StaticDatasource as any);
Datasources.set('api', ApiDatasource as any); Datasources.set('api', ApiDatasource as any);
export async function fetchDatasource<T>(
config: DatasourceConfig<T, keyof DatasourceConfigMap<any>>,
): Promise<T> {
if (!config) {
return;
}
if (Array.isArray(config)) {
// legacy config
return config as any;
}
const Datasource = Datasources.get(config.type);
if (!Datasource) {
throw new Error(`Unknown datasource type: ${config.type}`);
}
return new Datasource(config.config).getData();
}
...@@ -19,7 +19,7 @@ export class DavinciService extends ConsoleLogger implements OnModuleInit { ...@@ -19,7 +19,7 @@ export class DavinciService extends ConsoleLogger implements OnModuleInit {
private pointer = 0; private pointer = 0;
private async getKey(exclude: string[] = []) { private async getKey(exclude: string[] = []) {
const excludeSet = new Set(exclude); const excludeSet = new Set(exclude);
const keys = (await this.keyProvider.getKeys()).filter( const keys = (await this.keyProvider.get()).filter(
(key) => !excludeSet.has(key), (key) => !excludeSet.has(key),
); );
if (!keys.length) { if (!keys.length) {
...@@ -28,7 +28,10 @@ export class DavinciService extends ConsoleLogger implements OnModuleInit { ...@@ -28,7 +28,10 @@ export class DavinciService extends ConsoleLogger implements OnModuleInit {
'No available accounts.', 'No available accounts.',
).toException(); ).toException();
} }
const index = this.pointer++ % keys.length; if (this.pointer >= keys.length) {
this.pointer = 0;
}
const index = this.pointer++;
return keys[index]; return keys[index];
} }
......
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { BaseProvider } from '../datasource/base-provider';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { fetchDatasource } from '../datasource/datasource';
@Injectable() @Injectable()
export class KeyProviderService { export class KeyProviderService extends BaseProvider<string[]> {
constructor(private config: ConfigService) {} constructor(config: ConfigService) {
async getKeys() { super(config, 'apiKeys');
return (await fetchDatasource<string[]>(this.config.get('apiKeys'))) || [];
} }
} }
...@@ -7,7 +7,7 @@ export class ProxyPoolService { ...@@ -7,7 +7,7 @@ export class ProxyPoolService {
constructor(private proxyProvider: ProxyProviderService) {} constructor(private proxyProvider: ProxyProviderService) {}
async getProxy() { async getProxy() {
const proxies = await this.proxyProvider.getProxies(); const proxies = await this.proxyProvider.get();
if (!proxies?.length) { if (!proxies?.length) {
return; return;
} }
......
import { Injectable } from '@nestjs/common'; import { Injectable } from '@nestjs/common';
import { BaseProvider } from '../datasource/base-provider';
import { ConfigService } from '@nestjs/config'; import { ConfigService } from '@nestjs/config';
import { fetchDatasource } from '../datasource/datasource';
@Injectable() @Injectable()
export class ProxyProviderService { export class ProxyProviderService extends BaseProvider<string[]> {
constructor(private config: ConfigService) {} constructor(config: ConfigService) {
async getProxies() { super(config, 'proxies');
return (await fetchDatasource<string[]>(this.config.get('proxies'))) || [];
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment