@kevisual/ai 0.0.26 → 0.0.27
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/agent.d.ts +32 -0
- package/dist/agent.js +64 -0
- package/dist/ai-provider-browser.d.ts +2 -140
- package/dist/ai-provider-browser.js +2 -19455
- package/dist/ai-provider.d.ts +2 -140
- package/dist/ai-provider.js +2 -1340
- package/package.json +18 -11
- package/src/agent/index.ts +65 -0
- package/src/provider/chat-adapter/cnb.ts +1 -1
- package/src/provider/index.ts +0 -2
- package/src/provider/utils/ai-config-type.ts +0 -52
- package/src/provider/utils/index.ts +0 -2
- package/src/provider/utils/parse-config.ts +0 -192
package/dist/agent.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import { QueryRouterServer, App, RouteInfo } from '@kevisual/router';
|
|
2
|
+
import { ModelMessage, LanguageModel, GenerateTextResult } from 'ai';
|
|
3
|
+
|
|
4
|
+
declare const createTool: (app: QueryRouterServer | App, message: {
|
|
5
|
+
path: string;
|
|
6
|
+
key: string;
|
|
7
|
+
token?: string;
|
|
8
|
+
}) => Promise<any>;
|
|
9
|
+
declare const createTools: (opts: {
|
|
10
|
+
app: QueryRouterServer | App;
|
|
11
|
+
token?: string;
|
|
12
|
+
}) => Promise<Record<string, any>>;
|
|
13
|
+
type Route = Partial<RouteInfo>;
|
|
14
|
+
type AgentResult = {
|
|
15
|
+
result: GenerateTextResult<Record<string, any>, any>;
|
|
16
|
+
messages: ModelMessage[];
|
|
17
|
+
};
|
|
18
|
+
declare const reCallAgent: (opts: {
|
|
19
|
+
messages?: ModelMessage[];
|
|
20
|
+
tools?: Record<string, any>;
|
|
21
|
+
languageModel: LanguageModel;
|
|
22
|
+
}) => Promise<AgentResult>;
|
|
23
|
+
declare const runAgent: (opts: {
|
|
24
|
+
app: QueryRouterServer | App;
|
|
25
|
+
messages?: ModelMessage[];
|
|
26
|
+
routes?: Route[];
|
|
27
|
+
query?: string;
|
|
28
|
+
languageModel: LanguageModel;
|
|
29
|
+
token: string;
|
|
30
|
+
}) => Promise<AgentResult>;
|
|
31
|
+
|
|
32
|
+
export { createTool, createTools, reCallAgent, runAgent };
|
package/dist/agent.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
// src/agent/index.ts
|
|
2
|
+
import { generateText, tool } from "ai";
|
|
3
|
+
import z from "zod";
|
|
4
|
+
import { filter } from "@kevisual/js-filter";
|
|
5
|
+
var createTool = async (app, message) => {
|
|
6
|
+
const route = app.findRoute({ path: message.path, key: message.key });
|
|
7
|
+
if (!route) {
|
|
8
|
+
console.error(`未找到路径 ${message.path} 和 key ${message.key} 的路由`);
|
|
9
|
+
return null;
|
|
10
|
+
}
|
|
11
|
+
const _tool = tool({
|
|
12
|
+
description: route?.metadata?.summary || route?.description || "无描述",
|
|
13
|
+
inputSchema: z.object({
|
|
14
|
+
...route.metadata?.args
|
|
15
|
+
}),
|
|
16
|
+
execute: async (args) => {
|
|
17
|
+
const res = await app.run({ path: message.path, key: message.key, payload: args, token: message.token });
|
|
18
|
+
return res;
|
|
19
|
+
}
|
|
20
|
+
});
|
|
21
|
+
return _tool;
|
|
22
|
+
};
|
|
23
|
+
var createTools = async (opts) => {
|
|
24
|
+
const { app, token } = opts;
|
|
25
|
+
const tools = {};
|
|
26
|
+
for (const route of app.routes) {
|
|
27
|
+
const id = route.id;
|
|
28
|
+
const _tool = await createTool(app, { path: route.path, key: route.key, token });
|
|
29
|
+
if (_tool && id) {
|
|
30
|
+
tools[id] = _tool;
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
return tools;
|
|
34
|
+
};
|
|
35
|
+
var reCallAgent = async (opts) => {
|
|
36
|
+
const { messages = [], tools = {}, languageModel } = opts;
|
|
37
|
+
const result = await generateText({
|
|
38
|
+
model: languageModel,
|
|
39
|
+
messages,
|
|
40
|
+
tools
|
|
41
|
+
});
|
|
42
|
+
const step = result.steps[0];
|
|
43
|
+
if (step.finishReason === "tool-calls") {
|
|
44
|
+
messages.push(...result.response.messages);
|
|
45
|
+
return reCallAgent({ messages, tools, languageModel });
|
|
46
|
+
}
|
|
47
|
+
return { result, messages };
|
|
48
|
+
};
|
|
49
|
+
var runAgent = async (opts) => {
|
|
50
|
+
const { app, languageModel } = opts;
|
|
51
|
+
let messages = opts.messages || [];
|
|
52
|
+
let routes = opts?.routes || app.routes;
|
|
53
|
+
if (opts.query) {
|
|
54
|
+
routes = filter(routes, opts.query);
|
|
55
|
+
}
|
|
56
|
+
const tools = await createTools({ app, token: opts.token });
|
|
57
|
+
return await reCallAgent({ messages, tools, languageModel });
|
|
58
|
+
};
|
|
59
|
+
export {
|
|
60
|
+
runAgent,
|
|
61
|
+
reCallAgent,
|
|
62
|
+
createTools,
|
|
63
|
+
createTool
|
|
64
|
+
};
|
|
@@ -1,5 +1,3 @@
|
|
|
1
|
-
import * as _kevisual_permission from '@kevisual/permission';
|
|
2
|
-
import { Permission } from '@kevisual/permission';
|
|
3
1
|
import { Result } from '@kevisual/query';
|
|
4
2
|
|
|
5
3
|
type ChatMessage = {
|
|
@@ -609,141 +607,5 @@ type RerankOptions = {
|
|
|
609
607
|
overlap_tokens?: number;
|
|
610
608
|
};
|
|
611
609
|
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
* 提供商
|
|
615
|
-
*/
|
|
616
|
-
provider: string;
|
|
617
|
-
/**
|
|
618
|
-
* 模型名称
|
|
619
|
-
*/
|
|
620
|
-
model: string;
|
|
621
|
-
/**
|
|
622
|
-
* 模型组
|
|
623
|
-
*/
|
|
624
|
-
group: string;
|
|
625
|
-
/**
|
|
626
|
-
* 每日请求频率限制
|
|
627
|
-
*/
|
|
628
|
-
dayLimit?: number;
|
|
629
|
-
/**
|
|
630
|
-
* 总的token限制
|
|
631
|
-
*/
|
|
632
|
-
tokenLimit?: number;
|
|
633
|
-
};
|
|
634
|
-
type SecretKey = {
|
|
635
|
-
/**
|
|
636
|
-
* 组
|
|
637
|
-
*/
|
|
638
|
-
group: string;
|
|
639
|
-
/**
|
|
640
|
-
* API密钥
|
|
641
|
-
*/
|
|
642
|
-
apiKey: string;
|
|
643
|
-
/**
|
|
644
|
-
* 解密密钥
|
|
645
|
-
*/
|
|
646
|
-
decryptKey?: string;
|
|
647
|
-
};
|
|
648
|
-
type AIConfig = {
|
|
649
|
-
title?: string;
|
|
650
|
-
description?: string;
|
|
651
|
-
models: AIModel[];
|
|
652
|
-
secretKeys: SecretKey[];
|
|
653
|
-
permission?: Permission;
|
|
654
|
-
filter?: {
|
|
655
|
-
objectKey: string;
|
|
656
|
-
type: 'array' | 'object';
|
|
657
|
-
operate: 'removeAttribute' | 'remove';
|
|
658
|
-
attribute: string[];
|
|
659
|
-
}[];
|
|
660
|
-
};
|
|
661
|
-
|
|
662
|
-
declare function encryptAES(plainText: string, secretKey: string): string;
|
|
663
|
-
declare function decryptAES(cipherText: string, secretKey: string): string;
|
|
664
|
-
type GetProviderOpts = {
|
|
665
|
-
model: string;
|
|
666
|
-
group: string;
|
|
667
|
-
decryptKey?: string;
|
|
668
|
-
};
|
|
669
|
-
type ProviderResult = {
|
|
670
|
-
provider: string;
|
|
671
|
-
model: string;
|
|
672
|
-
group: string;
|
|
673
|
-
apiKey: string;
|
|
674
|
-
dayLimit?: number;
|
|
675
|
-
tokenLimit?: number;
|
|
676
|
-
baseURL?: string;
|
|
677
|
-
/**
|
|
678
|
-
* 解密密钥
|
|
679
|
-
*/
|
|
680
|
-
decryptKey?: string;
|
|
681
|
-
};
|
|
682
|
-
declare class AIConfigParser {
|
|
683
|
-
private config;
|
|
684
|
-
result: ProviderResult;
|
|
685
|
-
constructor(config: AIConfig);
|
|
686
|
-
/**
|
|
687
|
-
* 获取模型配置
|
|
688
|
-
* @param opts
|
|
689
|
-
* @returns
|
|
690
|
-
*/
|
|
691
|
-
getProvider(opts: GetProviderOpts): ProviderResult;
|
|
692
|
-
/**
|
|
693
|
-
* 获取解密密钥
|
|
694
|
-
* @param opts
|
|
695
|
-
* @returns
|
|
696
|
-
*/
|
|
697
|
-
getSecretKey(opts?: {
|
|
698
|
-
getCache?: (key: string) => Promise<string>;
|
|
699
|
-
setCache?: (key: string, value: string) => Promise<void>;
|
|
700
|
-
providerResult?: ProviderResult;
|
|
701
|
-
}): Promise<string>;
|
|
702
|
-
/**
|
|
703
|
-
* 加密
|
|
704
|
-
* @param plainText
|
|
705
|
-
* @param secretKey
|
|
706
|
-
* @returns
|
|
707
|
-
*/
|
|
708
|
-
encrypt(plainText: string, secretKey: string): string;
|
|
709
|
-
/**
|
|
710
|
-
* 解密
|
|
711
|
-
* @param cipherText
|
|
712
|
-
* @param secretKey
|
|
713
|
-
* @returns
|
|
714
|
-
*/
|
|
715
|
-
decrypt(cipherText: string, secretKey: string): string;
|
|
716
|
-
/**
|
|
717
|
-
* 获取模型配置
|
|
718
|
-
* @returns
|
|
719
|
-
*/
|
|
720
|
-
getSelectOpts(): {
|
|
721
|
-
group: string;
|
|
722
|
-
apiKey: string;
|
|
723
|
-
decryptKey?: string;
|
|
724
|
-
provider: string;
|
|
725
|
-
model: string;
|
|
726
|
-
dayLimit?: number;
|
|
727
|
-
tokenLimit?: number;
|
|
728
|
-
}[];
|
|
729
|
-
getConfig(keepSecret?: boolean, config?: AIConfig): AIConfig | {
|
|
730
|
-
secretKeys: {
|
|
731
|
-
apiKey: any;
|
|
732
|
-
decryptKey: any;
|
|
733
|
-
group: string;
|
|
734
|
-
}[];
|
|
735
|
-
title?: string;
|
|
736
|
-
description?: string;
|
|
737
|
-
models?: AIModel[];
|
|
738
|
-
permission?: _kevisual_permission.Permission;
|
|
739
|
-
filter?: {
|
|
740
|
-
objectKey: string;
|
|
741
|
-
type: "array" | "object";
|
|
742
|
-
operate: "removeAttribute" | "remove";
|
|
743
|
-
attribute: string[];
|
|
744
|
-
}[];
|
|
745
|
-
};
|
|
746
|
-
}
|
|
747
|
-
|
|
748
|
-
export { AIConfigParser, AIUtils, BailianChat, BailianProvider, BaseChat, CNBChat, ChatProviderMap, Custom, CustomProvider, DeepSeek, DeepSeekProvider, Kevisual, KevisualProvider, Kimi, KimiProvider, KnowledgeBase, ModelScope, ModelScopeProvider, Ollama, OllamaProvider, ProviderManager, SiliconFlow, SiliconFlowKnowledge, SiliconFlowProvider, Volces, VolcesProvider, Zhipu, ZhipuProvider, decryptAES, encryptAES, readStream };
|
|
749
|
-
export type { AIConfig, AIModel, BaseChatInterface, BaseChatOptions, ChatMessage, ChatMessageComplete, ChatMessageOptions, ChatStream, EmbeddingMessage, EmbeddingMessageComplete, EmbeddingObject, GetProviderOpts, KnowledgeOptions, ProviderResult, RerankOptions, SecretKey, Usage };
|
|
610
|
+
export { AIUtils, BailianChat, BailianProvider, BaseChat, CNBChat, ChatProviderMap, Custom, CustomProvider, DeepSeek, DeepSeekProvider, Kevisual, KevisualProvider, Kimi, KimiProvider, KnowledgeBase, ModelScope, ModelScopeProvider, Ollama, OllamaProvider, ProviderManager, SiliconFlow, SiliconFlowKnowledge, SiliconFlowProvider, Volces, VolcesProvider, Zhipu, ZhipuProvider, readStream };
|
|
611
|
+
export type { BaseChatInterface, BaseChatOptions, ChatMessage, ChatMessageComplete, ChatMessageOptions, ChatStream, EmbeddingMessage, EmbeddingMessageComplete, EmbeddingObject, KnowledgeOptions, RerankOptions, Usage };
|