@ynhcj/xiaoyi-channel 0.0.121-next → 0.0.122-next
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/src/cspl/call-api.d.ts +1 -1
- package/dist/src/cspl/call-api.js +20 -48
- package/package.json +1 -1
|
@@ -4,6 +4,6 @@ import type { ApiResponse } from "./constants.js";
|
|
|
4
4
|
export declare function callCsplApi(questionText: string, cfg: ClawdbotConfig): Promise<ApiResponse>;
|
|
5
5
|
/**
|
|
6
6
|
* Call CSPL API with a pre-resolved CsplConfig.
|
|
7
|
-
* Used by
|
|
7
|
+
* Used by after_tool_call hook which has session context but not ClawdbotConfig.
|
|
8
8
|
*/
|
|
9
9
|
export declare function callCsplApiWithConfig(questionText: string, config: CsplConfig): Promise<ApiResponse>;
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
// SENTINEL HOOK API 请求模块
|
|
2
|
+
import http from "node:http";
|
|
2
3
|
import https from "node:https";
|
|
3
4
|
import { URL } from "node:url";
|
|
4
5
|
import { randomBytes } from "node:crypto";
|
|
@@ -43,18 +44,12 @@ function parseResponse(data) {
|
|
|
43
44
|
}
|
|
44
45
|
return json;
|
|
45
46
|
}
|
|
46
|
-
|
|
47
|
-
const
|
|
48
|
-
const
|
|
49
|
-
const
|
|
50
|
-
questionText,
|
|
51
|
-
textSource: config.textSource,
|
|
52
|
-
action: config.action,
|
|
53
|
-
extra: JSON.stringify({ userId: config.uid }),
|
|
54
|
-
};
|
|
47
|
+
function doApiRequest(url, headers, payload, timeout) {
|
|
48
|
+
const isHttp = url.startsWith("http://");
|
|
49
|
+
const module = isHttp ? http : https;
|
|
50
|
+
const options = buildRequestOptions(url, headers, timeout);
|
|
55
51
|
return new Promise((resolve, reject) => {
|
|
56
|
-
const
|
|
57
|
-
const req = https.request(options, (res) => {
|
|
52
|
+
const req = module.request(options, (res) => {
|
|
58
53
|
if (res.statusCode && res.statusCode >= HTTP_STATUS_BAD_REQUEST) {
|
|
59
54
|
reject(new Error(`[SENTINEL HOOK] HTTP error: ${res.statusCode}`));
|
|
60
55
|
return;
|
|
@@ -80,7 +75,7 @@ export async function callCsplApi(questionText, cfg) {
|
|
|
80
75
|
reject(error);
|
|
81
76
|
});
|
|
82
77
|
req.on("timeout", () => {
|
|
83
|
-
logger.error(`[SENTINEL HOOK] ⏰ 请求超时 (${
|
|
78
|
+
logger.error(`[SENTINEL HOOK] ⏰ 请求超时 (${timeout}ms)`);
|
|
84
79
|
req.destroy();
|
|
85
80
|
reject(new Error("[SENTINEL HOOK] Request timeout"));
|
|
86
81
|
});
|
|
@@ -88,9 +83,20 @@ export async function callCsplApi(questionText, cfg) {
|
|
|
88
83
|
req.end();
|
|
89
84
|
});
|
|
90
85
|
}
|
|
86
|
+
export async function callCsplApi(questionText, cfg) {
|
|
87
|
+
const config = getCsplConfig(cfg);
|
|
88
|
+
const headers = buildHeaders(config);
|
|
89
|
+
const payload = {
|
|
90
|
+
questionText,
|
|
91
|
+
textSource: config.textSource,
|
|
92
|
+
action: config.action,
|
|
93
|
+
extra: JSON.stringify({ userId: config.uid }),
|
|
94
|
+
};
|
|
95
|
+
return doApiRequest(config.api.url, headers, payload, config.api.timeout);
|
|
96
|
+
}
|
|
91
97
|
/**
|
|
92
98
|
* Call CSPL API with a pre-resolved CsplConfig.
|
|
93
|
-
* Used by
|
|
99
|
+
* Used by after_tool_call hook which has session context but not ClawdbotConfig.
|
|
94
100
|
*/
|
|
95
101
|
export async function callCsplApiWithConfig(questionText, config) {
|
|
96
102
|
const headers = buildHeaders(config);
|
|
@@ -100,39 +106,5 @@ export async function callCsplApiWithConfig(questionText, config) {
|
|
|
100
106
|
action: config.action,
|
|
101
107
|
extra: JSON.stringify({ userId: config.uid }),
|
|
102
108
|
};
|
|
103
|
-
return
|
|
104
|
-
const options = buildRequestOptions(config.api.url, headers, config.api.timeout);
|
|
105
|
-
const req = https.request(options, (res) => {
|
|
106
|
-
if (res.statusCode && res.statusCode >= HTTP_STATUS_BAD_REQUEST) {
|
|
107
|
-
reject(new Error(`[SENTINEL HOOK] HTTP error: ${res.statusCode}`));
|
|
108
|
-
return;
|
|
109
|
-
}
|
|
110
|
-
let data = "";
|
|
111
|
-
res.on("data", (chunk) => {
|
|
112
|
-
data += chunk;
|
|
113
|
-
});
|
|
114
|
-
res.on("end", () => {
|
|
115
|
-
try {
|
|
116
|
-
const result = parseResponse(data);
|
|
117
|
-
logger.log(`[SENTINEL HOOK] ✅ 请求成功, securityResult=${result?.data?.securityResult ?? "N/A"}`);
|
|
118
|
-
resolve(result);
|
|
119
|
-
}
|
|
120
|
-
catch (e) {
|
|
121
|
-
logger.error(`[SENTINEL HOOK] ❌ 请求失败: ${e instanceof Error ? e.message : String(e)}`);
|
|
122
|
-
reject(e);
|
|
123
|
-
}
|
|
124
|
-
});
|
|
125
|
-
});
|
|
126
|
-
req.on("error", (error) => {
|
|
127
|
-
logger.error(`[SENTINEL HOOK] ❌ 请求错误: ${error instanceof Error ? error.message : String(error)}`);
|
|
128
|
-
reject(error);
|
|
129
|
-
});
|
|
130
|
-
req.on("timeout", () => {
|
|
131
|
-
logger.error(`[SENTINEL HOOK] ⏰ 请求超时 (${config.api.timeout}ms)`);
|
|
132
|
-
req.destroy();
|
|
133
|
-
reject(new Error("[SENTINEL HOOK] Request timeout"));
|
|
134
|
-
});
|
|
135
|
-
req.write(JSON.stringify(payload));
|
|
136
|
-
req.end();
|
|
137
|
-
});
|
|
109
|
+
return doApiRequest(config.api.url, headers, payload, config.api.timeout);
|
|
138
110
|
}
|