@ynhcj/xiaoyi-channel 0.0.124-next → 0.0.125-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/index.js +9 -62
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
1
|
import { definePluginEntry } from "openclaw/plugin-sdk/core";
|
|
2
2
|
import { xiaoyiProvider } from "./src/provider.js";
|
|
3
3
|
import { xyPlugin } from "./src/channel.js";
|
|
4
|
-
import {
|
|
5
|
-
import { getCsplConfig, initCsplConfigFromXYConfig } from "./src/cspl/config.js";
|
|
6
|
-
import { ALLOWED_TOOLS, MAX_TEXT_LENGTH, MAX_TOTAL_LENGTH, MIN_TEXT_LENGTH, STEER_ABORT_MESSAGE, } from "./src/cspl/constants.js";
|
|
7
|
-
import { extractResultText, parseSecurityResult, processText, validateAndTruncateText, } from "./src/cspl/utils.js";
|
|
8
|
-
import { injectCsplSteer } from "./src/cspl/steer-context.js";
|
|
9
|
-
import { getSessionContext } from "./src/tools/session-manager.js";
|
|
10
|
-
import { logger } from "./src/utils/logger.js";
|
|
4
|
+
import { createCsplMiddleware } from "./src/cspl/middleware.js";
|
|
11
5
|
import { setXYRuntime } from "./src/runtime.js";
|
|
12
6
|
import { registerSelfEvolutionToolResultNudge } from "./src/self-evolution-tool-result-nudge.js";
|
|
13
7
|
import { createBeforePromptBuildHandler } from "./src/skill-retriever/hooks.js";
|
|
@@ -26,60 +20,13 @@ function registerFullHooks(api) {
|
|
|
26
20
|
api.on("before_prompt_build", beforePromptBuildHandler);
|
|
27
21
|
registerSelfEvolutionToolResultNudge(api);
|
|
28
22
|
}
|
|
29
|
-
function
|
|
30
|
-
// CSPL security scanning via
|
|
31
|
-
//
|
|
32
|
-
//
|
|
33
|
-
// Only registered in "full" mode because it depends on
|
|
34
|
-
//
|
|
35
|
-
api.
|
|
36
|
-
if (!ALLOWED_TOOLS.includes(event.toolName)) {
|
|
37
|
-
return;
|
|
38
|
-
}
|
|
39
|
-
try {
|
|
40
|
-
const resultText = extractResultText(event, event.toolName);
|
|
41
|
-
const resultLength = resultText.length;
|
|
42
|
-
if (resultLength <= MIN_TEXT_LENGTH || resultLength > MAX_TOTAL_LENGTH) {
|
|
43
|
-
return;
|
|
44
|
-
}
|
|
45
|
-
logger.log(`[SENTINEL HOOK] after_tool_call: toolName=${event.toolName}, textLength=${resultLength}`);
|
|
46
|
-
const questionText = {
|
|
47
|
-
subSceneID: "TOOL_OUTPUT",
|
|
48
|
-
tool: event.toolName,
|
|
49
|
-
output: [{ content: "" }],
|
|
50
|
-
};
|
|
51
|
-
const originText = processText(resultText);
|
|
52
|
-
questionText.output[0].content = originText;
|
|
53
|
-
let finalJson = JSON.stringify(questionText);
|
|
54
|
-
if (finalJson.length > MAX_TEXT_LENGTH) {
|
|
55
|
-
const diff = finalJson.length - MAX_TEXT_LENGTH;
|
|
56
|
-
const { text: trimmed } = validateAndTruncateText(originText, MAX_TEXT_LENGTH - diff);
|
|
57
|
-
questionText.output[0].content = trimmed;
|
|
58
|
-
finalJson = JSON.stringify(questionText);
|
|
59
|
-
}
|
|
60
|
-
const sessionCtx = getSessionContext(ctx.sessionKey ?? "");
|
|
61
|
-
const csplConfig = sessionCtx
|
|
62
|
-
? initCsplConfigFromXYConfig(sessionCtx.config)
|
|
63
|
-
: getCsplConfig();
|
|
64
|
-
const csplStartTime = Date.now();
|
|
65
|
-
const response = await callCsplApiWithConfig(finalJson, csplConfig);
|
|
66
|
-
const csplElapsed = Date.now() - csplStartTime;
|
|
67
|
-
const result = parseSecurityResult(response);
|
|
68
|
-
logger.log(`[SENTINEL HOOK] Security result: status=${result.status}, toolName=${event.toolName}, elapsed=${csplElapsed}ms`);
|
|
69
|
-
if (result.status === "REJECT") {
|
|
70
|
-
logger.log(`[SENTINEL HOOK] REJECT - injecting steer message`);
|
|
71
|
-
if (sessionCtx) {
|
|
72
|
-
await injectCsplSteer(sessionCtx.sessionId, sessionCtx.taskId, STEER_ABORT_MESSAGE);
|
|
73
|
-
}
|
|
74
|
-
else {
|
|
75
|
-
logger.error("[SENTINEL HOOK] No session context, cannot inject steer");
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
}
|
|
79
|
-
catch (err) {
|
|
80
|
-
logger.error(`[SENTINEL HOOK] after_tool_call error: ${err}`);
|
|
81
|
-
}
|
|
82
|
-
});
|
|
23
|
+
function registerCsplMiddleware(api) {
|
|
24
|
+
// CSPL security scanning via AgentToolResultMiddleware.
|
|
25
|
+
// Intercepts tool results BEFORE they reach the LLM, so on REJECT
|
|
26
|
+
// the result is replaced entirely — no steer injection needed.
|
|
27
|
+
// Only registered in "full" mode because it depends on session context
|
|
28
|
+
// for CSPL config resolution.
|
|
29
|
+
api.registerAgentToolResultMiddleware(createCsplMiddleware(), { runtimes: ["pi"] });
|
|
83
30
|
}
|
|
84
31
|
export default definePluginEntry({
|
|
85
32
|
id: "xiaoyi-channel",
|
|
@@ -94,7 +41,7 @@ export default definePluginEntry({
|
|
|
94
41
|
setXYRuntime(api.runtime);
|
|
95
42
|
if (api.registrationMode === "full") {
|
|
96
43
|
registerFullHooks(api);
|
|
97
|
-
|
|
44
|
+
registerCsplMiddleware(api);
|
|
98
45
|
}
|
|
99
46
|
},
|
|
100
47
|
});
|