listpage-next-nest 0.0.163 → 0.0.183

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.
@@ -25,6 +25,40 @@ export declare class AdminAuthGuard implements CanActivate {
25
25
  }>;
26
26
  }
27
27
 
28
+ export declare class Agent {
29
+ private provider;
30
+ private subject;
31
+ messages: {
32
+ role: string;
33
+ content: string;
34
+ }[];
35
+ tools: {
36
+ name: string;
37
+ description: string;
38
+ parameters: Record<string, any>;
39
+ execute: (params: Record<string, any>) => Promise<any>;
40
+ }[];
41
+ constructor(options: AgentOptions);
42
+ private answer;
43
+ private function_call;
44
+ private doLoop;
45
+ chat(question: string): ReplaySubject<ServerSendEventData>;
46
+ }
47
+
48
+ declare interface AgentOptions {
49
+ provider: string;
50
+ api_key: string;
51
+ model: string;
52
+ system_prompt?: string;
53
+ user_prompt?: string;
54
+ tools: {
55
+ name: string;
56
+ description: string;
57
+ parameters: Record<string, any>;
58
+ execute: (params: Record<string, any>) => Promise<any>;
59
+ }[];
60
+ }
61
+
28
62
  export declare class ApiResponse<T = any> {
29
63
  code: number;
30
64
  message: string;
@@ -70,6 +104,27 @@ export declare class HttpExceptionFilter implements ExceptionFilter {
70
104
  catch(exception: unknown, host: ArgumentsHost): void;
71
105
  }
72
106
 
107
+ export declare abstract class LLMProvider<RequestionOption extends {
108
+ messages: {
109
+ role: string;
110
+ content: string;
111
+ }[];
112
+ } = any> {
113
+ protected baseURL: string;
114
+ protected apiKey: string;
115
+ protected model: string;
116
+ constructor(options?: {
117
+ api_key?: string;
118
+ model?: string;
119
+ });
120
+ call(options: RequestionOption): Promise<ServerSendJsonData>;
121
+ sse(options: RequestionOption): Promise<ReplaySubject<ServerSendEventData>>;
122
+ protected abstract handleJson(json: any): ServerSendJsonData;
123
+ protected abstract handleStream(subject: ReplaySubject<ServerSendEventData>, stream: ReadableStream<Uint8Array<ArrayBufferLike>>): Promise<void>;
124
+ protected abstract request(options: RequestionOption): Promise<Response>;
125
+ static parseJson<T = any>(json: string): T;
126
+ }
127
+
73
128
  export declare type LLMProviderCallOptions = {
74
129
  provider: SupportProvider;
75
130
  params: ProviderParams[LLMProviderCallOptions['provider']];
@@ -179,11 +234,12 @@ export declare type ServerSendEventCallback<T extends ServerSendEventData['type'
179
234
  }>['data']) => void;
180
235
 
181
236
  export declare type ServerSendEventData = {
182
- type: 'received' | 'thinking' | 'processing' | 'chunk' | 'complete' | 'citations';
237
+ type: 'function_call' | 'received' | 'thinking' | 'processing' | 'chunk' | 'complete' | 'citations';
183
238
  data: SevrverSendEventMessages[ServerSendEventData['type']];
184
239
  };
185
240
 
186
241
  export declare enum ServerSendEventName {
242
+ FunctionCall = "function_call",
187
243
  Received = "received",
188
244
  Thinking = "thinking",
189
245
  Processing = "processing",
@@ -227,6 +283,11 @@ export declare type SevrverSendEventMessages = {
227
283
  [ServerSendEventName.Citations]: {
228
284
  citations: CitationItem[];
229
285
  };
286
+ [ServerSendEventName.FunctionCall]: {
287
+ name: string;
288
+ arguments: any;
289
+ result?: any;
290
+ };
230
291
  };
231
292
 
232
293
  export declare class StaticRouteMiddleware implements NestMiddleware {
@@ -26,23 +26,25 @@ __webpack_require__.r(__webpack_exports__);
26
26
  __webpack_require__.d(__webpack_exports__, {
27
27
  LLMProviderModule: ()=>LLMProviderModule,
28
28
  RequestContextMiddleware: ()=>RequestContextMiddleware,
29
+ LLMProvider: ()=>LLMProvider,
29
30
  AdminAuthGuard: ()=>AdminAuthGuard,
30
- StaticRouteMiddleware: ()=>StaticRouteMiddleware,
31
31
  PaginationData: ()=>PaginationData,
32
+ StaticRouteMiddleware: ()=>StaticRouteMiddleware,
32
33
  StaticRouteMiddlewareFactory: ()=>StaticRouteMiddlewareFactory,
33
34
  RequestIdMiddleware: ()=>RequestIdMiddleware,
34
35
  LoggingInterceptor: ()=>LoggingInterceptor,
35
36
  ResponseInterceptor: ()=>ResponseInterceptor,
36
37
  StaticRouteModule: ()=>StaticRouteModule,
37
38
  CorsInterceptor: ()=>CorsInterceptor,
38
- BaseQueryDto: ()=>BaseQueryDto,
39
39
  ApiResponse: ()=>ApiResponse,
40
+ BaseQueryDto: ()=>BaseQueryDto,
40
41
  HttpExceptionFilter: ()=>HttpExceptionFilter,
41
42
  User: ()=>User,
42
43
  Public: ()=>Public,
43
44
  PaginatedResponse: ()=>PaginatedResponse,
44
45
  RequestContextService: ()=>RequestContextService,
45
46
  ServerSendEventName: ()=>types_ServerSendEventName,
47
+ Agent: ()=>Agent,
46
48
  LLMProviderService: ()=>LLMProviderService,
47
49
  setup: ()=>setup
48
50
  });
@@ -403,7 +405,6 @@ query_dto_ts_decorate([
403
405
  (0, external_class_transformer_namespaceObject.Type)(()=>Number),
404
406
  (0, external_class_validator_namespaceObject.IsInt)(),
405
407
  (0, external_class_validator_namespaceObject.Min)(1),
406
- (0, external_class_validator_namespaceObject.Max)(100),
407
408
  _ts_metadata("design:type", Number)
408
409
  ], BaseQueryDto.prototype, "pageSize", void 0);
409
410
  query_dto_ts_decorate([
@@ -517,6 +518,11 @@ StaticRouteModule = static_route_module_ts_decorate([
517
518
  ], StaticRouteModule);
518
519
  const external_rxjs_namespaceObject = require("rxjs");
519
520
  class LLMProvider {
521
+ constructor(options){
522
+ this.baseURL = '';
523
+ this.apiKey = options.api_key;
524
+ this.model = options.model;
525
+ }
520
526
  async call(options) {
521
527
  const response = await this.request({
522
528
  ...options,
@@ -544,14 +550,15 @@ class LLMProvider {
544
550
  return subject;
545
551
  }
546
552
  static parseJson(json) {
547
- json = json.trim();
548
- if (json.startsWith('```json') && json.endsWith('```')) return JSON.parse(json.slice(6, -3));
549
- json = json.match(/```json([\s\S]*?)```/)?.[1] || json;
550
- json = json.trim();
551
- return JSON.parse(json);
552
- }
553
- constructor(){
554
- this.baseURL = '';
553
+ try {
554
+ json = (json || '').trim();
555
+ const match = json.match(/(```json[\s\S]*?```)/);
556
+ json = (match ? match[1] : json).trim();
557
+ if (json.startsWith('```json') && json.endsWith('```')) return JSON.parse(json.slice(6, -3));
558
+ return JSON.parse(json);
559
+ } catch (e) {
560
+ throw new Error('模型输出不是有效 JSON');
561
+ }
555
562
  }
556
563
  }
557
564
  const parseJson = (json)=>{
@@ -634,12 +641,15 @@ class VolcengineProvider extends LLMProvider {
634
641
  subject.complete();
635
642
  }
636
643
  async request(options) {
637
- const { api_key, ...restOptions } = options;
644
+ const { api_key = this.apiKey, model = this.model, ...restOptions } = options;
638
645
  const headers = {
639
646
  'Content-Type': 'application/json',
640
647
  Authorization: `Bearer ${api_key}`
641
648
  };
642
- const body = restOptions;
649
+ const body = {
650
+ ...restOptions,
651
+ model
652
+ };
643
653
  const response = await fetch(this.baseURL, {
644
654
  method: 'POST',
645
655
  headers,
@@ -745,12 +755,15 @@ class MetaProvider extends LLMProvider {
745
755
  subject.complete();
746
756
  }
747
757
  async request(options) {
748
- const { api_key, ...restOptions } = options;
758
+ const { api_key = this.apiKey, model = this.model, ...restOptions } = options;
749
759
  const headers = {
750
760
  'Content-Type': 'application/json',
751
761
  Authorization: `Bearer ${api_key}`
752
762
  };
753
- const body = restOptions;
763
+ const body = {
764
+ ...restOptions,
765
+ model
766
+ };
754
767
  const response = await fetch(this.baseURL, {
755
768
  method: 'POST',
756
769
  headers,
@@ -854,6 +867,7 @@ LLMProviderModule = llm_provider_module_ts_decorate([
854
867
  })
855
868
  ], LLMProviderModule);
856
869
  var types_ServerSendEventName = /*#__PURE__*/ function(ServerSendEventName) {
870
+ ServerSendEventName["FunctionCall"] = "function_call";
857
871
  ServerSendEventName["Received"] = "received";
858
872
  ServerSendEventName["Thinking"] = "thinking";
859
873
  ServerSendEventName["Processing"] = "processing";
@@ -862,11 +876,211 @@ var types_ServerSendEventName = /*#__PURE__*/ function(ServerSendEventName) {
862
876
  ServerSendEventName["Citations"] = "citations";
863
877
  return ServerSendEventName;
864
878
  }({});
879
+ function createProvider(options) {
880
+ if ('volcengine' === options.provider) return new VolcengineProvider({
881
+ api_key: options.api_key,
882
+ model: options.model
883
+ });
884
+ if ('meta' === options.provider) return new MetaProvider({
885
+ api_key: options.api_key,
886
+ model: options.model
887
+ });
888
+ throw new Error('不支持该大模型提供商');
889
+ }
890
+ const system_prompt_system_prompt = `## 角色
891
+
892
+ Coze平台任务规划智能专家,擅长精准理解用户目标,结合平台项目内的工具,将复杂任务拆解为可执行步骤,并进行逻辑校验和执行规划,协调工具高效完成任务。输出结果以指定的JSON格式呈现。
893
+
894
+ ## 人设说明
895
+
896
+ 用户可以通过在对话开始时说明期望您扮演的角色或风格,来定制您的响应方式。例如:"请你扮演一位严谨的学术研究者"或"请用轻松幽默的语气回答"。用户也可能明确指定你最终输出答案的格式。
897
+
898
+ ## 目标
899
+
900
+ 基于用户输入(初始任务或工具执行结果)和可用资源,明确当前核心目标,评估任务边界和可行性。将复杂任务拆分为序列步骤,在每一步输出单个操作,并基于上下文调整规划。最终以"answer"操作结束任务。
901
+
902
+ ## 可用工具
903
+
904
+ {{ tools }}
905
+
906
+ ## 技能
907
+
908
+ 1. 深入的任务理解能力,能基于上下文准确把握当前目标。
909
+ 2. 迭代式任务拆解技巧,合理规划步骤序列。
910
+ 3. 严谨的逻辑校验能力,检查步骤间依赖和状态一致性。
911
+ 4. 基于反馈的动态规划能力,调整步骤优先级和处理异常。
912
+ 5. 熟练生成指定JSON输出格式,确保参数符合工具要求。
913
+
914
+ ## 工作流程
915
+
916
+ 1. 接收输入:用户初始任务描述或上一个工具的执行结果。
917
+ 2. 分析当前状态:结合历史步骤和结果,明确当前任务进度和下一步目标。
918
+ 3. 评估可行性:基于可用工具和当前状态,判断任务是否可继续、需终止或需补充信息,需要体现在 reasoning 中。
919
+ 4. 检索结果校验:若上一步为搜索工具且返回空结果,自动触发重试机制:
920
+ - 分析无结果原因(如关键词过窄、术语不匹配)
921
+ - 生成2-3个替代查询词(需与原需求强相关)
922
+ - 未达最大重试次数则继续调用搜索工具,已达则终止检索
923
+ 5. 决定下一个操作:需要工具调用输出"tool"操作;任务完成、不可行(重试后仍无结果)或需直接响应,输出"answer"操作。
924
+ 6. 推理分析:在reasoning中说明当前分析,包括目标明确性、依赖关系、检索策略调整理由等。
925
+ 7. 输出JSON格式的任务规划结果,包含:
926
+ - "name": 任务名称,简洁阐述总体目标。
927
+ - "reasoning": 针对当前任务状态的分析,包括可行性评估、依赖关系、异常处理等。
928
+ - "action": 用户接下来应该进行什么操作:
929
+ - "name": 操作名称,可选值:"tool"(调用工具)、"answer"(返回结果)。
930
+ - "args": 参数对象。如果操作是"tool",则是对应工具的参数(例如,对于search_files工具,args为{ name: "search_files", params: { "query": "用户查询" } });如果操作是"answer",则可以是{"message": "答案内容"};。
931
+
932
+
933
+ ## 约束
934
+
935
+ 1. 必须严格基于用户输入和上下文状态进行规划,不得忽略历史步骤。
936
+ 2. 必须使用指定JSON输出格式,操作参数必须匹配工具定义。
937
+ 3. 每个输出仅包含一个操作("tool"或"answer"),确保步骤序列化。注意选择 answer 的前提是你觉得用户的问题已经能够被解决。如果选择 answer:你需要根据用户要求的人设以及格式基于当前的信息给用户一个回答,注意是第二人称视角,仿佛你和用户在一对一对话。 如果选择 tool:你需要按照工具的描述以及参数的描述,生成为完成当前任务有帮助,且满足工具参数要求的参数。
938
+ 4. 禁止在未评估可行性时输出操作;如果任务无法完成,使用"answer"返回错误或请求补充信息。
939
+ 5. 输出必须为有效JSON对象,无需额外格式。
940
+
941
+ ## 输出格式
942
+
943
+ 输出为JSON对象,结构如下:
944
+ {
945
+ "name": "任务名称",
946
+ "reasoning": "针对当前任务状态的分析,包括目标明确性、可行性、依赖关系、异常处理等",
947
+ "action": {
948
+ "name": "操作名称", // 可选值: "tool", "answer"
949
+ "args": { ... } // 参数对象,具体内容取决于操作类型
950
+ }
951
+ }
952
+
953
+ ## 示例
954
+
955
+ 示例一:
956
+ 输入:查找关于创新政策的文件
957
+ 输出:
958
+ {
959
+ "name": "检索创新政策文件",
960
+ "reasoning": "用户需要查找与创新相关的政策文件。可用工具是search_files,可以根据查询检索文档。任务可行,无需额外依赖。如果搜索失败,将重试一次。",
961
+ "action": {
962
+ "name": "tool",
963
+ "args": {
964
+ "name": "search_files"
965
+ "params": { query: "创新政策" }
966
+ }
967
+ }
968
+ }
969
+
970
+ 示例二:
971
+ 输入:
972
+ 任务描述:“分析项目中的政策并生成总结”
973
+
974
+ 输出:
975
+ {
976
+ "name": "分析政策并生成总结",
977
+ "reasoning": "用户需要检索政策文件并生成总结。首先使用搜索工具获取相关文档,然后基于结果生成答案。步骤间有依赖关系:生成总结依赖搜索输出。如果搜索无结果,将直接生成提示信息。",
978
+ "action":{
979
+ "name": "answer",
980
+ "args": {
981
+ "message": "基于检索到的政策文件生成总结..."
982
+ }
983
+ }
984
+ }
985
+ `;
986
+ class Agent {
987
+ constructor(options){
988
+ this.subject = new external_rxjs_namespaceObject.ReplaySubject();
989
+ this.messages = [];
990
+ this.tools = [];
991
+ this.tools = options.tools;
992
+ this.provider = createProvider(options);
993
+ const system_prompt = compilePrompt(options.system_prompt ?? system_prompt_system_prompt, {
994
+ tools: JSON.stringify(options.tools.map((t)=>({
995
+ name: t.name,
996
+ description: t.description,
997
+ parameters: t.parameters
998
+ })))
999
+ });
1000
+ this.messages.push({
1001
+ role: 'system',
1002
+ content: system_prompt
1003
+ });
1004
+ if (options.user_prompt) this.messages.push({
1005
+ role: 'user',
1006
+ content: options.user_prompt
1007
+ });
1008
+ }
1009
+ async answer(params) {
1010
+ this.subject.next({
1011
+ type: 'chunk',
1012
+ data: {
1013
+ content: params.message
1014
+ }
1015
+ });
1016
+ this.subject.next({
1017
+ type: 'complete',
1018
+ data: {
1019
+ usage: {
1020
+ input: 0,
1021
+ output: 0,
1022
+ total: 0
1023
+ }
1024
+ }
1025
+ });
1026
+ this.subject.complete();
1027
+ }
1028
+ async function_call({ name, params }) {
1029
+ this.subject.next({
1030
+ type: 'function_call',
1031
+ data: {
1032
+ name,
1033
+ arguments: params
1034
+ }
1035
+ });
1036
+ const tool = this.tools.find((t)=>t.name === name);
1037
+ if (!tool) return void await this.doLoop(`工具 ${name} 调用失败:工具不存在`);
1038
+ const result = await tool.execute(params);
1039
+ this.subject.next({
1040
+ type: 'function_call',
1041
+ data: {
1042
+ name,
1043
+ arguments: params,
1044
+ result
1045
+ }
1046
+ });
1047
+ await this.doLoop(`工具 ${name} 调用成功,执行的结果是:${JSON.stringify(result)}`);
1048
+ }
1049
+ async doLoop(query) {
1050
+ this.messages.push({
1051
+ role: 'user',
1052
+ content: query
1053
+ });
1054
+ const { content } = await this.provider.call({
1055
+ messages: this.messages
1056
+ });
1057
+ const result = LLMProvider.parseJson(content);
1058
+ const { name, reasoning, action } = result;
1059
+ this.subject.next({
1060
+ type: 'thinking',
1061
+ data: {
1062
+ content: reasoning
1063
+ }
1064
+ });
1065
+ this.messages.push({
1066
+ role: 'assistant',
1067
+ content: content
1068
+ });
1069
+ if ('answer' === action.name) await this.answer(action.args);
1070
+ if ('tool' === action.name) await this.function_call(action.args);
1071
+ }
1072
+ chat(question) {
1073
+ this.doLoop(`用户问题是:「${question}」。`);
1074
+ return this.subject;
1075
+ }
1076
+ }
865
1077
  exports.AdminAuthGuard = __webpack_exports__.AdminAuthGuard;
1078
+ exports.Agent = __webpack_exports__.Agent;
866
1079
  exports.ApiResponse = __webpack_exports__.ApiResponse;
867
1080
  exports.BaseQueryDto = __webpack_exports__.BaseQueryDto;
868
1081
  exports.CorsInterceptor = __webpack_exports__.CorsInterceptor;
869
1082
  exports.HttpExceptionFilter = __webpack_exports__.HttpExceptionFilter;
1083
+ exports.LLMProvider = __webpack_exports__.LLMProvider;
870
1084
  exports.LLMProviderModule = __webpack_exports__.LLMProviderModule;
871
1085
  exports.LLMProviderService = __webpack_exports__.LLMProviderService;
872
1086
  exports.LoggingInterceptor = __webpack_exports__.LoggingInterceptor;
@@ -885,10 +1099,12 @@ exports.User = __webpack_exports__.User;
885
1099
  exports.setup = __webpack_exports__.setup;
886
1100
  for(var __webpack_i__ in __webpack_exports__)if (-1 === [
887
1101
  "AdminAuthGuard",
1102
+ "Agent",
888
1103
  "ApiResponse",
889
1104
  "BaseQueryDto",
890
1105
  "CorsInterceptor",
891
1106
  "HttpExceptionFilter",
1107
+ "LLMProvider",
892
1108
  "LLMProviderModule",
893
1109
  "LLMProviderService",
894
1110
  "LoggingInterceptor",
package/package.json CHANGED
@@ -1,10 +1,9 @@
1
1
  {
2
2
  "name": "listpage-next-nest",
3
- "version": "0.0.163",
3
+ "version": "0.0.183",
4
4
  "description": "A React component library for creating filter forms with Ant Design",
5
5
  "main": "./dist/cjs/index.cjs",
6
6
  "module": "./dist/esm/index.js",
7
- "type": "module",
8
7
  "exports": {
9
8
  ".": {
10
9
  "types": "./dist/cjs/index.d.ts",