@dj1029/plugin-smart-sl 0.0.15 → 0.0.17
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.
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"http-client.service.d.ts","sourceRoot":"","sources":["../../src/lib/http-client.service.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"http-client.service.d.ts","sourceRoot":"","sources":["../../src/lib/http-client.service.ts"],"names":[],"mappings":"AAIA,QAAA,MAAM,OAAO,+BAIX,CAAC;AA8HH,eAAe,OAAO,CAAA;AAGtB,qBACa,iBAAiB;IAC1B,OAAO,CAAC,aAAa,CAAC;;IAqBtB,GAAG,CAAC,GAAG,EAAE,MAAM,EAAC,IAAI,CAAC,EAAE,GAAG;IAI1B,IAAI,CAAC,GAAG,EAAE,MAAM,EAAE,IAAI,CAAC,EAAE,GAAG;CAK/B"}
|
|
@@ -1,6 +1,121 @@
|
|
|
1
1
|
import { __decorate, __metadata } from "tslib";
|
|
2
2
|
import { Injectable } from '@nestjs/common';
|
|
3
3
|
import axios from 'axios';
|
|
4
|
+
import errorCode from './errorCode.js';
|
|
5
|
+
const service = axios.create({
|
|
6
|
+
baseURL: 'https://www.antsfind.com:449', // 添加你的API基础URL
|
|
7
|
+
timeout: 10000, // 设置超时时间
|
|
8
|
+
headers: { 'Content-Type': 'application/json' }, // 设置默认头信息
|
|
9
|
+
});
|
|
10
|
+
// request拦截器
|
|
11
|
+
service.interceptors.request.use(config => {
|
|
12
|
+
// 是否需要设置 token
|
|
13
|
+
console.log("axios1 ---- config ---- ", config);
|
|
14
|
+
// get请求映射params参数
|
|
15
|
+
if (config.method === 'get' && config.params) {
|
|
16
|
+
let url = config.url + '?' + tansParams(config.params);
|
|
17
|
+
url = url.slice(0, -1);
|
|
18
|
+
config.params = {};
|
|
19
|
+
config.url = url;
|
|
20
|
+
}
|
|
21
|
+
return config;
|
|
22
|
+
}, error => {
|
|
23
|
+
Promise.reject(error);
|
|
24
|
+
});
|
|
25
|
+
// 在拦截器外部定义最大重试次数
|
|
26
|
+
const MAX_RETRY = 3;
|
|
27
|
+
service.interceptors.response.use((res) => {
|
|
28
|
+
const code = res.data.code || 200;
|
|
29
|
+
const msg = errorCode[code] || res.data.msg || errorCode['default'];
|
|
30
|
+
// 二进制数据则直接返回
|
|
31
|
+
if (res.request.responseType === 'blob' ||
|
|
32
|
+
res.request.responseType === 'arraybuffer') {
|
|
33
|
+
return res.data;
|
|
34
|
+
}
|
|
35
|
+
if (code === 401) {
|
|
36
|
+
return Promise.reject('无效的会话,或者会话已过期,请重新登录。');
|
|
37
|
+
}
|
|
38
|
+
else if (code === 500) {
|
|
39
|
+
// Message({ message: msg, type: 'error' });
|
|
40
|
+
// return Promise.reject(new Error(msg));
|
|
41
|
+
return null;
|
|
42
|
+
}
|
|
43
|
+
else if (code !== 200) {
|
|
44
|
+
return res.data;
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
return res.data;
|
|
48
|
+
}
|
|
49
|
+
}, async (error) => {
|
|
50
|
+
const config = error.config;
|
|
51
|
+
// 判断是否是 Axios 请求且配置存在
|
|
52
|
+
if (!config || !config.url) {
|
|
53
|
+
let { message } = error;
|
|
54
|
+
if (message === 'Network Error') {
|
|
55
|
+
message = '后端接口连接异常';
|
|
56
|
+
}
|
|
57
|
+
else if (message.includes('timeout')) {
|
|
58
|
+
message = '系统接口请求超时';
|
|
59
|
+
}
|
|
60
|
+
else if (message.includes('Request failed with status code')) {
|
|
61
|
+
message = '系统接口' + message.substr(message.length - 3) + '异常';
|
|
62
|
+
}
|
|
63
|
+
// Message.error(message);
|
|
64
|
+
return Promise.reject(error);
|
|
65
|
+
}
|
|
66
|
+
// 初始化 retryCount
|
|
67
|
+
config.__retryCount = config.__retryCount || 0;
|
|
68
|
+
// 判断是否允许重试(只针对 GET 请求)
|
|
69
|
+
if (config.method.toLowerCase() !== 'get' ||
|
|
70
|
+
config.__retryCount >= MAX_RETRY) {
|
|
71
|
+
let { message } = error;
|
|
72
|
+
if (message === 'Network Error') {
|
|
73
|
+
message = '后端接口连接异常';
|
|
74
|
+
}
|
|
75
|
+
else if (message.includes('timeout')) {
|
|
76
|
+
message = '系统接口请求超时';
|
|
77
|
+
}
|
|
78
|
+
else if (message.includes('Request failed with status code')) {
|
|
79
|
+
message = '系统接口' + message.substr(message.length - 3) + '异常';
|
|
80
|
+
}
|
|
81
|
+
// Message.error(message);
|
|
82
|
+
config.__retryCount = 0; // 清除计数器
|
|
83
|
+
return Promise.reject(error);
|
|
84
|
+
}
|
|
85
|
+
// 判断是否是网络错误或超时
|
|
86
|
+
if (error.message.includes('Network Error') ||
|
|
87
|
+
error.message.includes('timeout')) {
|
|
88
|
+
config.__retryCount++;
|
|
89
|
+
const delay = (retry) => new Promise((resolve) => setTimeout(resolve, 1000 * Math.pow(2, retry)));
|
|
90
|
+
console.warn(`请求失败,第 ${config.__retryCount} 次重试...`);
|
|
91
|
+
await delay(config.__retryCount); // 指数退避延迟
|
|
92
|
+
return service(config); // 使用原始配置重新发起请求
|
|
93
|
+
}
|
|
94
|
+
return Promise.reject(error);
|
|
95
|
+
});
|
|
96
|
+
function tansParams(params) {
|
|
97
|
+
let result = '';
|
|
98
|
+
for (const propName of Object.keys(params)) {
|
|
99
|
+
const value = params[propName];
|
|
100
|
+
var part = encodeURIComponent(propName) + "=";
|
|
101
|
+
if (value !== null && typeof (value) !== "undefined") {
|
|
102
|
+
if (typeof value === 'object') {
|
|
103
|
+
for (const key of Object.keys(value)) {
|
|
104
|
+
if (value[key] !== null && typeof (value[key]) !== 'undefined') {
|
|
105
|
+
let params = propName + '[' + key + ']';
|
|
106
|
+
var subPart = encodeURIComponent(params) + "=";
|
|
107
|
+
result += subPart + encodeURIComponent(value[key]) + "&";
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
result += part + encodeURIComponent(value) + "&";
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
return result;
|
|
117
|
+
}
|
|
118
|
+
export default service;
|
|
4
119
|
let HttpClientService = class HttpClientService {
|
|
5
120
|
constructor() {
|
|
6
121
|
this.axiosInstance = axios.create({
|
|
@@ -10,6 +125,7 @@ let HttpClientService = class HttpClientService {
|
|
|
10
125
|
});
|
|
11
126
|
// 添加拦截器(例如,请求和响应拦截)
|
|
12
127
|
this.axiosInstance.interceptors.request.use(config => {
|
|
128
|
+
console.log("axios2 ---- config ---- ", config);
|
|
13
129
|
// 在发送请求之前做些什么,例如设置认证头信息等
|
|
14
130
|
// config.headers['Authorization'] = `Bearer ${process.env.API_TOKEN}`; // 示例:从环境变量获取token
|
|
15
131
|
return config;
|
package/dist/lib/tool.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../src/lib/tool.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAIvB,wBAAgB,cAAc;;;;;;;;;;;;;;;
|
|
1
|
+
{"version":3,"file":"tool.d.ts","sourceRoot":"","sources":["../../src/lib/tool.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAIvB,wBAAgB,cAAc;;;;;;;;;;;;;;;QAgC7B"}
|
package/dist/lib/tool.js
CHANGED
|
@@ -1,15 +1,21 @@
|
|
|
1
1
|
import { tool } from '@langchain/core/tools';
|
|
2
2
|
import { getErrorMessage } from '@xpert-ai/plugin-sdk';
|
|
3
3
|
import { z } from 'zod';
|
|
4
|
-
import
|
|
4
|
+
import request from "./http-client.service.js";
|
|
5
5
|
export function buildSmartTool() {
|
|
6
6
|
return tool(async (input) => {
|
|
7
7
|
try {
|
|
8
|
-
console.log("input ---------- ",
|
|
9
|
-
const httpClient =
|
|
10
|
-
const res = await httpClient.post('/login', input);
|
|
11
|
-
console.log("res ---------- ", res.data.token)
|
|
12
|
-
return res.data.token;
|
|
8
|
+
// console.log("input ---------- ",input)
|
|
9
|
+
// const httpClient = new HttpClientService();
|
|
10
|
+
// const res = await httpClient.post('/login', input);
|
|
11
|
+
// console.log("res ---------- ", res.data.token)
|
|
12
|
+
// return res.data.token;
|
|
13
|
+
const req = await request({
|
|
14
|
+
url: '/login',
|
|
15
|
+
method: 'post',
|
|
16
|
+
data: input
|
|
17
|
+
});
|
|
18
|
+
return req.data.token;
|
|
13
19
|
}
|
|
14
20
|
catch (error) {
|
|
15
21
|
return "I don't know how to do that because: " + getErrorMessage(error);
|