llmconfig 0.0.2 → 0.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +7 -16
- package/dist/index.mjs +18 -58
- package/package.json +1 -1
package/dist/index.d.mts
CHANGED
|
@@ -11,19 +11,14 @@ declare const LLMBilities: readonly ["text", "image", "image_modify", "video", "
|
|
|
11
11
|
//#endregion
|
|
12
12
|
//#region src/types/llmconfig.d.ts
|
|
13
13
|
type LLMBility = typeof LLMBilities[number];
|
|
14
|
-
interface LLMProvider {
|
|
15
|
-
id: string;
|
|
16
|
-
name: string;
|
|
17
|
-
apiKey: string;
|
|
18
|
-
createdAt: number;
|
|
19
|
-
baseUrl?: string;
|
|
20
|
-
}
|
|
21
14
|
/**
|
|
22
15
|
* LLM配置接口
|
|
23
16
|
*/
|
|
24
17
|
interface LLMConfig {
|
|
25
18
|
id: string;
|
|
26
|
-
|
|
19
|
+
provider: string;
|
|
20
|
+
apiKey: string;
|
|
21
|
+
baseUrl?: string;
|
|
27
22
|
model: string;
|
|
28
23
|
bility: LLMBility;
|
|
29
24
|
createdAt: number;
|
|
@@ -114,12 +109,11 @@ type WrapperPredicate = (wrapper: ILLMWrapper) => boolean;
|
|
|
114
109
|
declare class Manager {
|
|
115
110
|
#private;
|
|
116
111
|
get configs(): LLMConfig[];
|
|
117
|
-
get providers(): LLMProvider[];
|
|
118
112
|
get wrappers(): ILLMWrapper[];
|
|
119
113
|
getWrapper(id: string): ILLMWrapper | undefined;
|
|
120
114
|
filterWrapper(pred: WrapperPredicate): ILLMWrapper[];
|
|
121
115
|
findWrapper(pred: WrapperPredicate): ILLMWrapper | undefined;
|
|
122
|
-
reLoad(models: LLMConfig[]
|
|
116
|
+
reLoad(models: LLMConfig[]): void;
|
|
123
117
|
upsertWrapper(cfg: LLMConfig): boolean;
|
|
124
118
|
delWrapper(id: string): void;
|
|
125
119
|
private getMatchingWrapper;
|
|
@@ -132,16 +126,13 @@ declare class Manager {
|
|
|
132
126
|
declare class Library extends Manager {
|
|
133
127
|
#private;
|
|
134
128
|
get modelTable(): Table<LLMConfig>;
|
|
135
|
-
|
|
136
|
-
init(models: Table<LLMConfig>, providers: Table<LLMProvider>): Promise<void>;
|
|
129
|
+
init(models: Table<LLMConfig>): Promise<void>;
|
|
137
130
|
upsertModel(cfg: LLMConfig): Promise<boolean>;
|
|
138
131
|
delModel(id: string): Promise<void>;
|
|
139
|
-
upsertProvider(provider: LLMProvider): Promise<boolean>;
|
|
140
|
-
delProvider(pid: string): Promise<void>;
|
|
141
132
|
}
|
|
142
133
|
declare const llmcfgInst: Library;
|
|
143
134
|
//#endregion
|
|
144
135
|
//#region src/wrapper/providers.d.ts
|
|
145
|
-
declare function listModels(
|
|
136
|
+
declare function listModels(config: LLMConfig): Promise<string[]>;
|
|
146
137
|
//#endregion
|
|
147
|
-
export { type CallResult, type GenerateObjectOptions, type GenerateObjectResult, type ILLMWrapper, type ITextLLMWrapper, type JSONCallResult, LLMBilities, type LLMBility, type LLMConfig,
|
|
138
|
+
export { type CallResult, type GenerateObjectOptions, type GenerateObjectResult, type ILLMWrapper, type ITextLLMWrapper, type JSONCallResult, LLMBilities, type LLMBility, type LLMConfig, TableSchema, llmcfgInst as default, llmcfgInst, listModels };
|
package/dist/index.mjs
CHANGED
|
@@ -131,12 +131,12 @@ const PROVIDER_CONFIG = {
|
|
|
131
131
|
}
|
|
132
132
|
};
|
|
133
133
|
const ProviderNames = Object.keys(PROVIDER_CONFIG);
|
|
134
|
-
async function listModels(
|
|
135
|
-
const providerConfig = PROVIDER_CONFIG[provider
|
|
136
|
-
if (!providerConfig) throw new Error(`不支持的提供商: ${provider
|
|
134
|
+
async function listModels(config) {
|
|
135
|
+
const providerConfig = PROVIDER_CONFIG[config.provider];
|
|
136
|
+
if (!providerConfig) throw new Error(`不支持的提供商: ${config.provider}`);
|
|
137
137
|
const baseURL = providerConfig.baseURL.trim();
|
|
138
138
|
const response = await fetch(`${baseURL}/models`, { headers: {
|
|
139
|
-
"Authorization": `Bearer ${
|
|
139
|
+
"Authorization": `Bearer ${config.apiKey}`,
|
|
140
140
|
"Content-Type": "application/json"
|
|
141
141
|
} });
|
|
142
142
|
if (!response.ok) throw new Error(`获取模型列表失败: ${response.statusText}`);
|
|
@@ -152,18 +152,18 @@ var ModelFactory = class {
|
|
|
152
152
|
/**
|
|
153
153
|
* 创建 AI SDK 模型实例
|
|
154
154
|
*/
|
|
155
|
-
static createModel(config
|
|
156
|
-
const constProvider = PROVIDER_CONFIG[provider
|
|
157
|
-
if (!constProvider) throw new Error(`不支持的提供商: ${config.
|
|
155
|
+
static createModel(config) {
|
|
156
|
+
const constProvider = PROVIDER_CONFIG[config.provider];
|
|
157
|
+
if (!constProvider) throw new Error(`不支持的提供商: ${config.provider}`);
|
|
158
158
|
const modelName = config.model.trim();
|
|
159
|
-
const apiKey =
|
|
159
|
+
const apiKey = config.apiKey;
|
|
160
160
|
const baseURL = constProvider.baseURL?.trim();
|
|
161
161
|
const protocol = constProvider.protocal || "openai";
|
|
162
162
|
try {
|
|
163
163
|
return this.createModelByProtocol(protocol, modelName, apiKey, baseURL);
|
|
164
164
|
} catch (error) {
|
|
165
|
-
console.error(`创建模型失败 (${config.
|
|
166
|
-
throw new Error(`无法创建模型: ${config.
|
|
165
|
+
console.error(`创建模型失败 (${config.provider}):`, error);
|
|
166
|
+
throw new Error(`无法创建模型: ${config.provider} - ${modelName}`);
|
|
167
167
|
}
|
|
168
168
|
}
|
|
169
169
|
/**
|
|
@@ -264,10 +264,9 @@ var TextWrapper = class {
|
|
|
264
264
|
get callCount() {
|
|
265
265
|
return this.#callCount;
|
|
266
266
|
}
|
|
267
|
-
constructor(config
|
|
267
|
+
constructor(config) {
|
|
268
268
|
this.config = config;
|
|
269
|
-
this.
|
|
270
|
-
this.model = ModelFactory.createModel(config, provider);
|
|
269
|
+
this.model = ModelFactory.createModel(config);
|
|
271
270
|
}
|
|
272
271
|
/**
|
|
273
272
|
* 将输入转换为消息格式
|
|
@@ -438,10 +437,10 @@ var WrapperFactory = class {
|
|
|
438
437
|
/**
|
|
439
438
|
* 根据 tag 创建相应的包装器
|
|
440
439
|
*/
|
|
441
|
-
static createWrapper(config
|
|
440
|
+
static createWrapper(config) {
|
|
442
441
|
const bility = this.getModelTypeFromTag(config.bility);
|
|
443
442
|
switch (bility) {
|
|
444
|
-
case "text": return new TextWrapper(config
|
|
443
|
+
case "text": return new TextWrapper(config);
|
|
445
444
|
case "image": break;
|
|
446
445
|
}
|
|
447
446
|
throw new Error(`不支持的模型标签: ${bility}`);
|
|
@@ -465,14 +464,10 @@ const CallError = {
|
|
|
465
464
|
error: /* @__PURE__ */ new Error("Not Found or All Error")
|
|
466
465
|
};
|
|
467
466
|
var Manager = class {
|
|
468
|
-
#providers = [];
|
|
469
467
|
#wrappers = /* @__PURE__ */ new Map();
|
|
470
468
|
get configs() {
|
|
471
469
|
return Array.from(this.#wrappers.values()).map((wrapper) => wrapper.getConfig());
|
|
472
470
|
}
|
|
473
|
-
get providers() {
|
|
474
|
-
return this.#providers;
|
|
475
|
-
}
|
|
476
471
|
get wrappers() {
|
|
477
472
|
return Array.from(this.#wrappers.values());
|
|
478
473
|
}
|
|
@@ -485,8 +480,7 @@ var Manager = class {
|
|
|
485
480
|
findWrapper(pred) {
|
|
486
481
|
return Array.from(this.#wrappers.values()).find(pred);
|
|
487
482
|
}
|
|
488
|
-
reLoad(models
|
|
489
|
-
this.#providers = [...provides];
|
|
483
|
+
reLoad(models) {
|
|
490
484
|
this.#wrappers.clear();
|
|
491
485
|
models.forEach((model) => {
|
|
492
486
|
this.upsertWrapper(model);
|
|
@@ -495,9 +489,7 @@ var Manager = class {
|
|
|
495
489
|
upsertWrapper(cfg) {
|
|
496
490
|
if (this.#wrappers.has(cfg.id)) this.#wrappers.delete(cfg.id);
|
|
497
491
|
if (!cfg.enabled) return true;
|
|
498
|
-
const
|
|
499
|
-
if (!provider) return false;
|
|
500
|
-
const wrapper = WrapperFactory.createWrapper(cfg, provider);
|
|
492
|
+
const wrapper = WrapperFactory.createWrapper(cfg);
|
|
501
493
|
this.#wrappers.set(cfg.id, wrapper);
|
|
502
494
|
return true;
|
|
503
495
|
}
|
|
@@ -540,19 +532,13 @@ var Manager = class {
|
|
|
540
532
|
//#region src/library.ts
|
|
541
533
|
var Library = class extends Manager {
|
|
542
534
|
#modelTable;
|
|
543
|
-
#providerTable;
|
|
544
535
|
get modelTable() {
|
|
545
536
|
return this.#modelTable;
|
|
546
537
|
}
|
|
547
|
-
|
|
548
|
-
return this.#providerTable;
|
|
549
|
-
}
|
|
550
|
-
async init(models, providers) {
|
|
538
|
+
async init(models) {
|
|
551
539
|
this.#modelTable = models;
|
|
552
|
-
this.#providerTable = providers;
|
|
553
540
|
const allModels = await models.toArray();
|
|
554
|
-
|
|
555
|
-
super.reLoad(allModels, allProviders);
|
|
541
|
+
super.reLoad(allModels);
|
|
556
542
|
}
|
|
557
543
|
async upsertModel(cfg) {
|
|
558
544
|
if (!super.upsertWrapper(cfg)) return false;
|
|
@@ -562,32 +548,6 @@ var Library = class extends Manager {
|
|
|
562
548
|
super.delWrapper(id);
|
|
563
549
|
await this.#modelTable.delete(id);
|
|
564
550
|
}
|
|
565
|
-
async upsertProvider(provider) {
|
|
566
|
-
const idx = super.providers.findIndex((p) => p.id === provider.id);
|
|
567
|
-
await this.#providerTable.put(provider);
|
|
568
|
-
if (idx >= 0) {
|
|
569
|
-
super.providers[idx] = { ...provider };
|
|
570
|
-
super.configs.filter((cfg) => cfg.pid === provider.id).forEach((model) => {
|
|
571
|
-
super.upsertWrapper(model);
|
|
572
|
-
});
|
|
573
|
-
} else super.providers.push({ ...provider });
|
|
574
|
-
return true;
|
|
575
|
-
}
|
|
576
|
-
async delProvider(pid) {
|
|
577
|
-
const idx = super.providers.findIndex((p) => p.id === pid);
|
|
578
|
-
if (idx !== -1) {
|
|
579
|
-
await this.#providerTable.delete(pid);
|
|
580
|
-
super.providers.splice(idx, 1);
|
|
581
|
-
const affectModels = super.configs.filter((cfg) => cfg.pid === pid);
|
|
582
|
-
if (affectModels.length > 0) {
|
|
583
|
-
const ids = affectModels.map((model) => {
|
|
584
|
-
super.delWrapper(model.id);
|
|
585
|
-
return model.id;
|
|
586
|
-
});
|
|
587
|
-
await this.#modelTable.bulkDelete(ids);
|
|
588
|
-
}
|
|
589
|
-
}
|
|
590
|
-
}
|
|
591
551
|
};
|
|
592
552
|
const llmcfgInst = new Library();
|
|
593
553
|
|