@kevisual/ai 0.0.12 → 0.0.13
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/ai-provider-browser.d.ts +93 -13
- package/dist/ai-provider-browser.js +1672 -1549
- package/dist/ai-provider.d.ts +93 -13
- package/dist/ai-provider.js +136 -13
- package/package.json +9 -9
- package/src/provider/chat-adapter/kimi.ts +10 -0
- package/src/provider/chat-adapter/zhipu.ts +10 -0
- package/src/provider/chat.ts +6 -0
- package/src/provider/core/chat.ts +4 -26
- package/src/provider/core/utils/index.ts +192 -0
- package/src/provider/utils/chunk.ts +0 -86
package/dist/ai-provider.d.ts
CHANGED
|
@@ -1,7 +1,8 @@
|
|
|
1
1
|
import * as openai_resources_index_mjs from 'openai/resources/index.mjs';
|
|
2
2
|
import OpenAI, { OpenAI as OpenAI$1 } from 'openai';
|
|
3
3
|
import * as openai_resources_embeddings_mjs from 'openai/resources/embeddings.mjs';
|
|
4
|
-
import
|
|
4
|
+
import * as _kevisual_permission from '@kevisual/permission';
|
|
5
|
+
import { Permission } from '@kevisual/permission';
|
|
5
6
|
|
|
6
7
|
type ChatMessage = OpenAI.Chat.Completions.ChatCompletionMessageParam;
|
|
7
8
|
type ChatMessageOptions = Partial<OpenAI.Chat.Completions.ChatCompletionCreateParams> & {
|
|
@@ -34,6 +35,77 @@ interface BaseChatUsageInterface {
|
|
|
34
35
|
}
|
|
35
36
|
type ChatStream = AsyncGenerator<ChatMessageComplete, void, unknown>;
|
|
36
37
|
|
|
38
|
+
declare class AIUtils {
|
|
39
|
+
/**
|
|
40
|
+
* 从 Markdown 代码块中提取 JSON
|
|
41
|
+
* @param str 包含 JSON 的字符串
|
|
42
|
+
* @returns 解析后的对象或 null
|
|
43
|
+
*/
|
|
44
|
+
extractJsonFromMarkdown(str: string): any | null;
|
|
45
|
+
/**
|
|
46
|
+
* 从 Markdown 代码块中提取代码
|
|
47
|
+
* @param str Markdown 字符串
|
|
48
|
+
* @param language 语言类型,不指定则返回所有代码块
|
|
49
|
+
* @returns 提取的代码字符串或数组
|
|
50
|
+
*/
|
|
51
|
+
extractCodeFromMarkdown(str: string, language?: string): string | string[] | null;
|
|
52
|
+
/**
|
|
53
|
+
* 清理 AI 响应中的多余空白和格式
|
|
54
|
+
* @param str 原始字符串
|
|
55
|
+
* @returns 清理后的字符串
|
|
56
|
+
*/
|
|
57
|
+
cleanResponse(str: string): string;
|
|
58
|
+
/**
|
|
59
|
+
* 从 AI 响应中提取标签
|
|
60
|
+
* @param str 响应字符串
|
|
61
|
+
* @returns 标签数组
|
|
62
|
+
*/
|
|
63
|
+
extractTags(str: string): string[];
|
|
64
|
+
/**
|
|
65
|
+
* 从文本中提取 URL
|
|
66
|
+
* @param str 文本字符串
|
|
67
|
+
* @returns URL 数组
|
|
68
|
+
*/
|
|
69
|
+
extractUrls(str: string): string[];
|
|
70
|
+
/**
|
|
71
|
+
* 分割长文本为指定 token 数量的块
|
|
72
|
+
* @param text 原始文本
|
|
73
|
+
* @param maxTokens 每块最大 token 数(粗略估算:1 token ≈ 4 字符)
|
|
74
|
+
* @returns 文本块数组
|
|
75
|
+
*/
|
|
76
|
+
chunkText(text: string, maxTokens?: number): string[];
|
|
77
|
+
/**
|
|
78
|
+
* 移除 AI 响应中的思考过程(thinking 标签)
|
|
79
|
+
* @param str 响应字符串
|
|
80
|
+
* @returns 清理后的字符串
|
|
81
|
+
*/
|
|
82
|
+
removeThinkingTags(str: string): string;
|
|
83
|
+
/**
|
|
84
|
+
* 转义特殊字符用于 AI 提示词
|
|
85
|
+
* @param str 原始字符串
|
|
86
|
+
* @returns 转义后的字符串
|
|
87
|
+
*/
|
|
88
|
+
escapeForPrompt(str: string): string;
|
|
89
|
+
/**
|
|
90
|
+
* 统计文本的大致 token 数量
|
|
91
|
+
* @param text 文本
|
|
92
|
+
* @returns 估算的 token 数量
|
|
93
|
+
*/
|
|
94
|
+
estimateTokens(text: string): number;
|
|
95
|
+
/**
|
|
96
|
+
* 从响应中提取结构化数据(key: value 格式)
|
|
97
|
+
* @param str 响应字符串
|
|
98
|
+
* @returns 键值对对象
|
|
99
|
+
*/
|
|
100
|
+
extractKeyValuePairs(str: string): Record<string, string>;
|
|
101
|
+
/**
|
|
102
|
+
* 验证 AI 响应是否完整(检查截断)
|
|
103
|
+
* @param str 响应字符串
|
|
104
|
+
* @returns 是否完整
|
|
105
|
+
*/
|
|
106
|
+
isResponseComplete(str: string): boolean;
|
|
107
|
+
}
|
|
108
|
+
|
|
37
109
|
type BaseChatOptions<T = Record<string, any>> = {
|
|
38
110
|
/**
|
|
39
111
|
* 默认baseURL
|
|
@@ -56,7 +128,6 @@ type BaseChatOptions<T = Record<string, any>> = {
|
|
|
56
128
|
*/
|
|
57
129
|
stream?: boolean;
|
|
58
130
|
} & T;
|
|
59
|
-
declare const getIsBrowser: () => boolean;
|
|
60
131
|
declare class BaseChat implements BaseChatInterface, BaseChatUsageInterface {
|
|
61
132
|
/**
|
|
62
133
|
* 默认baseURL
|
|
@@ -70,18 +141,11 @@ declare class BaseChat implements BaseChatInterface, BaseChatUsageInterface {
|
|
|
70
141
|
* 默认apiKey
|
|
71
142
|
*/
|
|
72
143
|
apiKey: string;
|
|
73
|
-
/**
|
|
74
|
-
* 是否在浏览器中使用
|
|
75
|
-
*/
|
|
76
|
-
isBrowser: boolean;
|
|
77
|
-
/**
|
|
78
|
-
* openai实例
|
|
79
|
-
*/
|
|
80
|
-
openai: OpenAI$1;
|
|
81
144
|
prompt_tokens: number;
|
|
82
145
|
total_tokens: number;
|
|
83
146
|
completion_tokens: number;
|
|
84
147
|
responseText: string;
|
|
148
|
+
utils: AIUtils;
|
|
85
149
|
constructor(options: BaseChatOptions);
|
|
86
150
|
post(url?: string, opts?: {
|
|
87
151
|
headers?: Record<string, string>;
|
|
@@ -217,6 +281,18 @@ declare class BailianChat extends BaseChat {
|
|
|
217
281
|
constructor(options: BailianOptions);
|
|
218
282
|
}
|
|
219
283
|
|
|
284
|
+
type ZhipuOptions = Partial<BaseChatOptions>;
|
|
285
|
+
declare class Zhipu extends BaseChat {
|
|
286
|
+
static BASE_URL: string;
|
|
287
|
+
constructor(options: ZhipuOptions);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
type KimiOptions = Partial<BaseChatOptions>;
|
|
291
|
+
declare class Kimi extends BaseChat {
|
|
292
|
+
static BASE_URL: string;
|
|
293
|
+
constructor(options: KimiOptions);
|
|
294
|
+
}
|
|
295
|
+
|
|
220
296
|
declare const OllamaProvider: typeof Ollama;
|
|
221
297
|
declare const SiliconFlowProvider: typeof SiliconFlow;
|
|
222
298
|
declare const CustomProvider: typeof Custom;
|
|
@@ -224,6 +300,8 @@ declare const VolcesProvider: typeof Volces;
|
|
|
224
300
|
declare const DeepSeekProvider: typeof DeepSeek;
|
|
225
301
|
declare const ModelScopeProvider: typeof ModelScope;
|
|
226
302
|
declare const BailianProvider: typeof BailianChat;
|
|
303
|
+
declare const ZhipuProvider: typeof Zhipu;
|
|
304
|
+
declare const KimiProvider: typeof Kimi;
|
|
227
305
|
declare const ChatProviderMap: {
|
|
228
306
|
Ollama: typeof Ollama;
|
|
229
307
|
SiliconFlow: typeof SiliconFlow;
|
|
@@ -233,6 +311,8 @@ declare const ChatProviderMap: {
|
|
|
233
311
|
ModelScope: typeof ModelScope;
|
|
234
312
|
BaseChat: typeof BaseChat;
|
|
235
313
|
Bailian: typeof BailianChat;
|
|
314
|
+
Zhipu: typeof Zhipu;
|
|
315
|
+
Kimi: typeof Kimi;
|
|
236
316
|
};
|
|
237
317
|
type ProviderManagerConfig = {
|
|
238
318
|
provider: string;
|
|
@@ -352,7 +432,7 @@ type AIConfig = {
|
|
|
352
432
|
description?: string;
|
|
353
433
|
models: AIModel[];
|
|
354
434
|
secretKeys: SecretKey[];
|
|
355
|
-
permission?: Permission
|
|
435
|
+
permission?: Permission;
|
|
356
436
|
filter?: {
|
|
357
437
|
objectKey: string;
|
|
358
438
|
type: 'array' | 'object';
|
|
@@ -437,7 +517,7 @@ declare class AIConfigParser {
|
|
|
437
517
|
title?: string;
|
|
438
518
|
description?: string;
|
|
439
519
|
models?: AIModel[];
|
|
440
|
-
permission?: Permission;
|
|
520
|
+
permission?: _kevisual_permission.Permission;
|
|
441
521
|
filter?: {
|
|
442
522
|
objectKey: string;
|
|
443
523
|
type: "array" | "object";
|
|
@@ -447,5 +527,5 @@ declare class AIConfigParser {
|
|
|
447
527
|
};
|
|
448
528
|
}
|
|
449
529
|
|
|
450
|
-
export { AIConfigParser, BailianProvider, BaseChat, ChatProviderMap, CustomProvider, DeepSeekProvider, KnowledgeBase, ModelScopeProvider, OllamaProvider, ProviderManager, SiliconFlowKnowledge, SiliconFlowProvider, VolcesProvider, decryptAES, encryptAES,
|
|
530
|
+
export { AIConfigParser, BailianProvider, BaseChat, ChatProviderMap, CustomProvider, DeepSeekProvider, KimiProvider, KnowledgeBase, ModelScopeProvider, OllamaProvider, ProviderManager, SiliconFlowKnowledge, SiliconFlowProvider, VolcesProvider, ZhipuProvider, decryptAES, encryptAES, readStream };
|
|
451
531
|
export type { AIConfig, AIModel, BaseChatInterface, BaseChatOptions, BaseChatUsageInterface, ChatMessage, ChatMessageComplete, ChatMessageOptions, ChatMessageStream, ChatStream, EmbeddingMessage, EmbeddingMessageComplete, GetProviderOpts, KnowledgeOptions, ProviderResult, RerankOptions, SecretKey };
|
package/dist/ai-provider.js
CHANGED
|
@@ -1235,31 +1235,131 @@ var require_enc_utf8 = __commonJS((exports, module) => {
|
|
|
1235
1235
|
});
|
|
1236
1236
|
});
|
|
1237
1237
|
|
|
1238
|
-
// src/provider/core/
|
|
1239
|
-
|
|
1240
|
-
|
|
1241
|
-
|
|
1242
|
-
|
|
1243
|
-
|
|
1238
|
+
// src/provider/core/utils/index.ts
|
|
1239
|
+
class AIUtils {
|
|
1240
|
+
extractJsonFromMarkdown(str) {
|
|
1241
|
+
const jsonRegex = /```json\s*([\s\S]*?)\s*```/;
|
|
1242
|
+
const match = str.match(jsonRegex);
|
|
1243
|
+
let jsonStr = match && match[1] ? match[1] : str;
|
|
1244
|
+
try {
|
|
1245
|
+
return JSON.parse(jsonStr);
|
|
1246
|
+
} catch {
|
|
1247
|
+
return null;
|
|
1248
|
+
}
|
|
1244
1249
|
}
|
|
1245
|
-
|
|
1250
|
+
extractCodeFromMarkdown(str, language) {
|
|
1251
|
+
if (language) {
|
|
1252
|
+
const regex2 = new RegExp(`\`\`\`${language}\\s*([\\s\\S]*?)\\s*\`\`\``, "g");
|
|
1253
|
+
const matches2 = str.match(regex2);
|
|
1254
|
+
if (!matches2)
|
|
1255
|
+
return null;
|
|
1256
|
+
return matches2.map((m) => m.replace(new RegExp(`\`\`\`${language}\\s*|\\s*\`\`\``, "g"), "").trim());
|
|
1257
|
+
}
|
|
1258
|
+
const regex = /```[\w]*\s*([\s\S]*?)\s*```/g;
|
|
1259
|
+
const matches = [...str.matchAll(regex)];
|
|
1260
|
+
if (matches.length === 0)
|
|
1261
|
+
return null;
|
|
1262
|
+
return matches.map((m) => m[1].trim());
|
|
1263
|
+
}
|
|
1264
|
+
cleanResponse(str) {
|
|
1265
|
+
return str.trim().replace(/\n{3,}/g, `
|
|
1266
|
+
|
|
1267
|
+
`).replace(/[ \t]+$/gm, "");
|
|
1268
|
+
}
|
|
1269
|
+
extractTags(str) {
|
|
1270
|
+
const tagPatterns = [
|
|
1271
|
+
/#(\w+)/g,
|
|
1272
|
+
/\[(\w+)\]/g,
|
|
1273
|
+
/tags?:\s*\[([^\]]+)\]/gi
|
|
1274
|
+
];
|
|
1275
|
+
const tags = new Set;
|
|
1276
|
+
for (const pattern of tagPatterns) {
|
|
1277
|
+
const matches = str.matchAll(pattern);
|
|
1278
|
+
for (const match of matches) {
|
|
1279
|
+
if (match[1]) {
|
|
1280
|
+
const extracted = match[1].split(/[,;]/).map((t) => t.trim()).filter(Boolean);
|
|
1281
|
+
extracted.forEach((tag) => tags.add(tag));
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
}
|
|
1285
|
+
return Array.from(tags);
|
|
1286
|
+
}
|
|
1287
|
+
extractUrls(str) {
|
|
1288
|
+
const urlRegex = /(https?:\/\/[^\s]+)/g;
|
|
1289
|
+
const matches = str.match(urlRegex);
|
|
1290
|
+
return matches || [];
|
|
1291
|
+
}
|
|
1292
|
+
chunkText(text, maxTokens = 1000) {
|
|
1293
|
+
const chunkSize = maxTokens * 4;
|
|
1294
|
+
const chunks = [];
|
|
1295
|
+
const paragraphs = text.split(/\n\n+/);
|
|
1296
|
+
let currentChunk = "";
|
|
1297
|
+
for (const paragraph of paragraphs) {
|
|
1298
|
+
if ((currentChunk + paragraph).length > chunkSize && currentChunk) {
|
|
1299
|
+
chunks.push(currentChunk.trim());
|
|
1300
|
+
currentChunk = paragraph;
|
|
1301
|
+
} else {
|
|
1302
|
+
currentChunk += (currentChunk ? `
|
|
1303
|
+
|
|
1304
|
+
` : "") + paragraph;
|
|
1305
|
+
}
|
|
1306
|
+
}
|
|
1307
|
+
if (currentChunk) {
|
|
1308
|
+
chunks.push(currentChunk.trim());
|
|
1309
|
+
}
|
|
1310
|
+
return chunks;
|
|
1311
|
+
}
|
|
1312
|
+
removeThinkingTags(str) {
|
|
1313
|
+
return str.replace(/<thinking>[\s\S]*?<\/thinking>/gi, "").replace(/\[thinking\][\s\S]*?\[\/thinking\]/gi, "").trim();
|
|
1314
|
+
}
|
|
1315
|
+
escapeForPrompt(str) {
|
|
1316
|
+
return str.replace(/\\/g, "\\\\").replace(/`/g, "\\`").replace(/\$/g, "\\$");
|
|
1317
|
+
}
|
|
1318
|
+
estimateTokens(text) {
|
|
1319
|
+
const chineseChars = (text.match(/[\u4e00-\u9fa5]/g) || []).length;
|
|
1320
|
+
const otherChars = text.length - chineseChars;
|
|
1321
|
+
return Math.ceil(chineseChars / 1.5 + otherChars / 4);
|
|
1322
|
+
}
|
|
1323
|
+
extractKeyValuePairs(str) {
|
|
1324
|
+
const result = {};
|
|
1325
|
+
const lines = str.split(`
|
|
1326
|
+
`);
|
|
1327
|
+
for (const line of lines) {
|
|
1328
|
+
const match = line.match(/^([^::]+)[::]\s*(.+)$/);
|
|
1329
|
+
if (match) {
|
|
1330
|
+
const key = match[1].trim();
|
|
1331
|
+
const value = match[2].trim();
|
|
1332
|
+
result[key] = value;
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
return result;
|
|
1336
|
+
}
|
|
1337
|
+
isResponseComplete(str) {
|
|
1338
|
+
const incompleteSigns = [
|
|
1339
|
+
/```[\w]*\s*[\s\S]*?(?<!```)$/,
|
|
1340
|
+
/\{[\s\S]*(?<!\})$/,
|
|
1341
|
+
/\[[\s\S]*(?<!\])$/,
|
|
1342
|
+
/\.{3,}$/
|
|
1343
|
+
];
|
|
1344
|
+
return !incompleteSigns.some((pattern) => pattern.test(str.trim()));
|
|
1345
|
+
}
|
|
1346
|
+
}
|
|
1246
1347
|
|
|
1348
|
+
// src/provider/core/chat.ts
|
|
1247
1349
|
class BaseChat {
|
|
1248
1350
|
baseURL;
|
|
1249
1351
|
model;
|
|
1250
1352
|
apiKey;
|
|
1251
|
-
isBrowser;
|
|
1252
|
-
openai;
|
|
1253
1353
|
prompt_tokens;
|
|
1254
1354
|
total_tokens;
|
|
1255
1355
|
completion_tokens;
|
|
1256
1356
|
responseText;
|
|
1357
|
+
utils;
|
|
1257
1358
|
constructor(options) {
|
|
1258
1359
|
this.baseURL = options.baseURL;
|
|
1259
1360
|
this.model = options.model;
|
|
1260
1361
|
this.apiKey = options.apiKey;
|
|
1261
|
-
|
|
1262
|
-
this.isBrowser = options.isBrowser ?? DEFAULT_IS_BROWSER;
|
|
1362
|
+
this.utils = new AIUtils;
|
|
1263
1363
|
}
|
|
1264
1364
|
post(url = "", opts = {}) {
|
|
1265
1365
|
let _url = url.startsWith("http") ? url : this.baseURL + url;
|
|
@@ -1478,6 +1578,24 @@ class BailianChat extends BaseChat {
|
|
|
1478
1578
|
}
|
|
1479
1579
|
}
|
|
1480
1580
|
|
|
1581
|
+
// src/provider/chat-adapter/zhipu.ts
|
|
1582
|
+
class Zhipu extends BaseChat {
|
|
1583
|
+
static BASE_URL = "https://open.bigmodel.cn/api/paas/v4/";
|
|
1584
|
+
constructor(options) {
|
|
1585
|
+
const baseURL = options.baseURL || Zhipu.BASE_URL;
|
|
1586
|
+
super({ ...options, baseURL });
|
|
1587
|
+
}
|
|
1588
|
+
}
|
|
1589
|
+
|
|
1590
|
+
// src/provider/chat-adapter/kimi.ts
|
|
1591
|
+
class Kimi extends BaseChat {
|
|
1592
|
+
static BASE_URL = "https://api.moonshot.cn/v1/";
|
|
1593
|
+
constructor(options) {
|
|
1594
|
+
const baseURL = options.baseURL || Kimi.BASE_URL;
|
|
1595
|
+
super({ ...options, baseURL });
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
|
|
1481
1599
|
// src/provider/chat.ts
|
|
1482
1600
|
var OllamaProvider = Ollama;
|
|
1483
1601
|
var SiliconFlowProvider = SiliconFlow;
|
|
@@ -1486,6 +1604,8 @@ var VolcesProvider = Volces;
|
|
|
1486
1604
|
var DeepSeekProvider = DeepSeek;
|
|
1487
1605
|
var ModelScopeProvider = ModelScope;
|
|
1488
1606
|
var BailianProvider = BailianChat;
|
|
1607
|
+
var ZhipuProvider = Zhipu;
|
|
1608
|
+
var KimiProvider = Kimi;
|
|
1489
1609
|
var ChatProviderMap = {
|
|
1490
1610
|
Ollama: OllamaProvider,
|
|
1491
1611
|
SiliconFlow: SiliconFlowProvider,
|
|
@@ -1494,7 +1614,9 @@ var ChatProviderMap = {
|
|
|
1494
1614
|
DeepSeek: DeepSeekProvider,
|
|
1495
1615
|
ModelScope: ModelScopeProvider,
|
|
1496
1616
|
BaseChat,
|
|
1497
|
-
Bailian: BailianProvider
|
|
1617
|
+
Bailian: BailianProvider,
|
|
1618
|
+
Zhipu: ZhipuProvider,
|
|
1619
|
+
Kimi: KimiProvider
|
|
1498
1620
|
};
|
|
1499
1621
|
|
|
1500
1622
|
class ProviderManager {
|
|
@@ -1703,9 +1825,9 @@ class AIConfigParser {
|
|
|
1703
1825
|
}
|
|
1704
1826
|
export {
|
|
1705
1827
|
readStream,
|
|
1706
|
-
getIsBrowser,
|
|
1707
1828
|
encryptAES,
|
|
1708
1829
|
decryptAES,
|
|
1830
|
+
ZhipuProvider,
|
|
1709
1831
|
VolcesProvider,
|
|
1710
1832
|
SiliconFlowProvider,
|
|
1711
1833
|
SiliconFlowKnowledge,
|
|
@@ -1713,6 +1835,7 @@ export {
|
|
|
1713
1835
|
OllamaProvider,
|
|
1714
1836
|
ModelScopeProvider,
|
|
1715
1837
|
KnowledgeBase,
|
|
1838
|
+
KimiProvider,
|
|
1716
1839
|
DeepSeekProvider,
|
|
1717
1840
|
CustomProvider,
|
|
1718
1841
|
ChatProviderMap,
|
package/package.json
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@kevisual/ai",
|
|
3
|
-
"version": "0.0.
|
|
3
|
+
"version": "0.0.13",
|
|
4
4
|
"description": "AI Center Services",
|
|
5
5
|
"main": "index.js",
|
|
6
6
|
"basename": "/root/ai-center-services",
|
|
7
7
|
"app": {
|
|
8
8
|
"entry": "dist/app.mjs",
|
|
9
|
-
"key": "ai-center-services",
|
|
10
9
|
"type": "system-app"
|
|
11
10
|
},
|
|
12
11
|
"files": [
|
|
@@ -28,7 +27,7 @@
|
|
|
28
27
|
],
|
|
29
28
|
"author": "abearxiong <xiongxiao@xiongxiao.me> (https://www.xiongxiao.me)",
|
|
30
29
|
"license": "MIT",
|
|
31
|
-
"packageManager": "pnpm@10.
|
|
30
|
+
"packageManager": "pnpm@10.24.0",
|
|
32
31
|
"type": "module",
|
|
33
32
|
"publishConfig": {
|
|
34
33
|
"registry": "https://registry.npmjs.org/",
|
|
@@ -58,14 +57,14 @@
|
|
|
58
57
|
"@kevisual/mark": "0.0.7",
|
|
59
58
|
"@kevisual/router": "0.0.33",
|
|
60
59
|
"@kevisual/types": "^0.0.10",
|
|
61
|
-
"@kevisual/use-config": "^1.0.
|
|
60
|
+
"@kevisual/use-config": "^1.0.21",
|
|
62
61
|
"@types/bun": "^1.3.3",
|
|
63
62
|
"@types/crypto-js": "^4.2.2",
|
|
64
63
|
"@types/formidable": "^3.4.6",
|
|
65
64
|
"@types/lodash-es": "^4.17.12",
|
|
66
65
|
"@types/node": "^24.10.1",
|
|
67
66
|
"@vitejs/plugin-basic-ssl": "^2.1.0",
|
|
68
|
-
"cookie": "^1.
|
|
67
|
+
"cookie": "^1.1.1",
|
|
69
68
|
"cross-env": "^10.1.0",
|
|
70
69
|
"crypto-js": "^4.2.0",
|
|
71
70
|
"dayjs": "^1.11.19",
|
|
@@ -75,17 +74,18 @@
|
|
|
75
74
|
"json5": "^2.2.3",
|
|
76
75
|
"lodash-es": "^4.17.21",
|
|
77
76
|
"openai": "6.9.1",
|
|
78
|
-
"pm2": "^6.0.
|
|
77
|
+
"pm2": "^6.0.14",
|
|
79
78
|
"rimraf": "^6.1.2",
|
|
80
79
|
"rollup": "^4.53.3",
|
|
81
|
-
"rollup-plugin-dts": "^6.
|
|
80
|
+
"rollup-plugin-dts": "^6.3.0",
|
|
82
81
|
"sequelize": "^6.37.7",
|
|
83
82
|
"tape": "^5.9.0",
|
|
84
83
|
"tiktoken": "^1.0.22",
|
|
85
84
|
"typescript": "^5.9.3",
|
|
86
|
-
"vite": "^7.2.
|
|
85
|
+
"vite": "^7.2.6"
|
|
87
86
|
},
|
|
88
87
|
"dependencies": {
|
|
89
|
-
"@kevisual/logger": "^0.0.4"
|
|
88
|
+
"@kevisual/logger": "^0.0.4",
|
|
89
|
+
"@kevisual/permission": "^0.0.3"
|
|
90
90
|
}
|
|
91
91
|
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BaseChat, BaseChatOptions } from '../core/chat.ts';
|
|
2
|
+
|
|
3
|
+
export type KimiOptions = Partial<BaseChatOptions>;
|
|
4
|
+
export class Kimi extends BaseChat {
|
|
5
|
+
static BASE_URL = 'https://api.moonshot.cn/v1/';
|
|
6
|
+
constructor(options: KimiOptions) {
|
|
7
|
+
const baseURL = options.baseURL || Kimi.BASE_URL;
|
|
8
|
+
super({ ...(options as BaseChatOptions), baseURL: baseURL });
|
|
9
|
+
}
|
|
10
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { BaseChat, BaseChatOptions } from '../core/chat.ts';
|
|
2
|
+
|
|
3
|
+
export type ZhipuOptions = Partial<BaseChatOptions>;
|
|
4
|
+
export class Zhipu extends BaseChat {
|
|
5
|
+
static BASE_URL = 'https://open.bigmodel.cn/api/paas/v4/';
|
|
6
|
+
constructor(options: ZhipuOptions) {
|
|
7
|
+
const baseURL = options.baseURL || Zhipu.BASE_URL;
|
|
8
|
+
super({ ...(options as BaseChatOptions), baseURL: baseURL });
|
|
9
|
+
}
|
|
10
|
+
}
|
package/src/provider/chat.ts
CHANGED
|
@@ -8,6 +8,8 @@ import { Volces } from './chat-adapter/volces.ts';
|
|
|
8
8
|
import { DeepSeek } from './chat-adapter/deepseek.ts';
|
|
9
9
|
import { ModelScope } from './chat-adapter/model-scope.ts';
|
|
10
10
|
import { BailianChat } from './chat-adapter/dashscope.ts';
|
|
11
|
+
import { Zhipu } from './chat-adapter/zhipu.ts';
|
|
12
|
+
import { Kimi } from './chat-adapter/kimi.ts';
|
|
11
13
|
|
|
12
14
|
import { ChatMessage } from './core/type.ts';
|
|
13
15
|
|
|
@@ -18,6 +20,8 @@ export const VolcesProvider = Volces;
|
|
|
18
20
|
export const DeepSeekProvider = DeepSeek;
|
|
19
21
|
export const ModelScopeProvider = ModelScope;
|
|
20
22
|
export const BailianProvider = BailianChat;
|
|
23
|
+
export const ZhipuProvider = Zhipu;
|
|
24
|
+
export const KimiProvider = Kimi;
|
|
21
25
|
|
|
22
26
|
export const ChatProviderMap = {
|
|
23
27
|
Ollama: OllamaProvider,
|
|
@@ -28,6 +32,8 @@ export const ChatProviderMap = {
|
|
|
28
32
|
ModelScope: ModelScopeProvider,
|
|
29
33
|
BaseChat: BaseChat,
|
|
30
34
|
Bailian: BailianProvider,
|
|
35
|
+
Zhipu: ZhipuProvider,
|
|
36
|
+
Kimi: KimiProvider,
|
|
31
37
|
};
|
|
32
38
|
|
|
33
39
|
type ProviderManagerConfig = {
|
|
@@ -9,6 +9,7 @@ import type {
|
|
|
9
9
|
EmbeddingMessage,
|
|
10
10
|
EmbeddingMessageComplete,
|
|
11
11
|
} from './type.ts';
|
|
12
|
+
import { AIUtils } from './utils/index.ts';
|
|
12
13
|
|
|
13
14
|
export type BaseChatOptions<T = Record<string, any>> = {
|
|
14
15
|
/**
|
|
@@ -32,14 +33,7 @@ export type BaseChatOptions<T = Record<string, any>> = {
|
|
|
32
33
|
*/
|
|
33
34
|
stream?: boolean;
|
|
34
35
|
} & T;
|
|
35
|
-
|
|
36
|
-
try {
|
|
37
|
-
// 检查是否存在window对象
|
|
38
|
-
return typeof window !== 'undefined' && typeof window.document !== 'undefined';
|
|
39
|
-
} catch (e) {
|
|
40
|
-
return false;
|
|
41
|
-
}
|
|
42
|
-
};
|
|
36
|
+
|
|
43
37
|
export class BaseChat implements BaseChatInterface, BaseChatUsageInterface {
|
|
44
38
|
/**
|
|
45
39
|
* 默认baseURL
|
|
@@ -53,32 +47,16 @@ export class BaseChat implements BaseChatInterface, BaseChatUsageInterface {
|
|
|
53
47
|
* 默认apiKey
|
|
54
48
|
*/
|
|
55
49
|
apiKey: string;
|
|
56
|
-
/**
|
|
57
|
-
* 是否在浏览器中使用
|
|
58
|
-
*/
|
|
59
|
-
isBrowser: boolean;
|
|
60
|
-
/**
|
|
61
|
-
* openai实例
|
|
62
|
-
*/
|
|
63
|
-
openai: OpenAI;
|
|
64
|
-
|
|
65
50
|
prompt_tokens: number;
|
|
66
51
|
total_tokens: number;
|
|
67
52
|
completion_tokens: number;
|
|
68
53
|
responseText: string;
|
|
69
|
-
|
|
54
|
+
utils: AIUtils;
|
|
70
55
|
constructor(options: BaseChatOptions) {
|
|
71
56
|
this.baseURL = options.baseURL;
|
|
72
57
|
this.model = options.model;
|
|
73
58
|
this.apiKey = options.apiKey;
|
|
74
|
-
|
|
75
|
-
const DEFAULT_IS_BROWSER = getIsBrowser();
|
|
76
|
-
this.isBrowser = options.isBrowser ?? DEFAULT_IS_BROWSER;
|
|
77
|
-
// this.openai = new OpenAI({
|
|
78
|
-
// apiKey: this.apiKey,
|
|
79
|
-
// baseURL: this.baseURL,
|
|
80
|
-
// dangerouslyAllowBrowser: options?.dangerouslyAllowBrowser ?? this.isBrowser,
|
|
81
|
-
// });
|
|
59
|
+
this.utils = new AIUtils();
|
|
82
60
|
}
|
|
83
61
|
post(url = '', opts: { headers?: Record<string, string>, data?: any } = {}) {
|
|
84
62
|
let _url = url.startsWith('http') ? url : this.baseURL + url;
|