@ynhcj/xiaoyi-channel 0.0.166-beta → 0.0.166-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 +141 -1
- package/dist/src/bot.js +60 -5
- package/dist/src/cron-command.d.ts +2 -0
- package/dist/src/cron-command.js +14 -8
- package/dist/src/cron-query-handler.js +45 -8
- package/dist/src/cspl/call_api.js +2 -2
- package/dist/src/cspl/sentinel_hook.js +9 -4
- package/dist/src/cspl/upload_file.js +2 -2
- package/dist/src/cspl/utils.d.ts +9 -3
- package/dist/src/cspl/utils.js +15 -9
- package/dist/src/file-upload.d.ts +5 -0
- package/dist/src/file-upload.js +102 -0
- package/dist/src/formatter.d.ts +29 -0
- package/dist/src/formatter.js +100 -2
- package/dist/src/monitor.js +35 -23
- package/dist/src/parser.d.ts +6 -0
- package/dist/src/parser.js +23 -13
- package/dist/src/provider.js +41 -1
- package/dist/src/reply-dispatcher.js +34 -16
- package/dist/src/self-evolution-handler.d.ts +1 -1
- package/dist/src/self-evolution-handler.js +12 -1
- package/dist/src/tools/check-plugin-privilege-tool.d.ts +6 -0
- package/dist/src/tools/check-plugin-privilege-tool.js +182 -0
- package/dist/src/tools/create-all-tools.js +4 -0
- package/dist/src/tools/device-tool-map.d.ts +1 -1
- package/dist/src/tools/device-tool-map.js +8 -1
- package/dist/src/tools/modify-alarm-tool.js +17 -0
- package/dist/src/tools/send-cross-device-task-tool.js +84 -15
- package/dist/src/tools/send-file-to-user-tool.js +9 -11
- package/dist/src/tools/send-html-card-tool.d.ts +7 -0
- package/dist/src/tools/send-html-card-tool.js +113 -0
- package/dist/src/tools/session-manager.d.ts +11 -2
- package/dist/src/tools/session-manager.js +65 -18
- package/dist/src/tools/xiaoyi-gui-tool.js +1 -1
- package/dist/src/types.d.ts +9 -7
- package/dist/src/utils/config-manager.d.ts +3 -2
- package/dist/src/utils/config-manager.js +22 -2
- package/dist/src/utils/cron-push-map.d.ts +26 -0
- package/dist/src/utils/cron-push-map.js +131 -0
- package/dist/src/websocket.js +11 -13
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3,7 +3,10 @@ import { xiaoyiProvider } from "./src/provider.js";
|
|
|
3
3
|
import { xyPlugin } from "./src/channel.js";
|
|
4
4
|
import registerSentinelHook from "./src/cspl/sentinel_hook.js";
|
|
5
5
|
import { setXYRuntime } from "./src/runtime.js";
|
|
6
|
-
import { markCronToolCall, clearCronToolCall } from "./src/tools/session-manager.js";
|
|
6
|
+
import { markCronToolCall, clearCronToolCall, getSessionContext } from "./src/tools/session-manager.js";
|
|
7
|
+
import { configManager } from "./src/utils/config-manager.js";
|
|
8
|
+
import { setJobPushId } from "./src/utils/cron-push-map.js";
|
|
9
|
+
import { getAllPushIds } from "./src/utils/pushid-manager.js";
|
|
7
10
|
import { registerSelfEvolutionToolResultNudge } from "./src/self-evolution-tool-result-nudge.js";
|
|
8
11
|
import { createBeforePromptBuildHandler } from "./src/skill-retriever/hooks.js";
|
|
9
12
|
import { normalizeToolRetrieverConfig } from "./src/skill-retriever/config.js";
|
|
@@ -25,8 +28,145 @@ function registerCronDetectionHook(api) {
|
|
|
25
28
|
if (event.toolCallId) {
|
|
26
29
|
clearCronToolCall(event.toolCallId);
|
|
27
30
|
}
|
|
31
|
+
// 捕获对话创建的 cron job:agent 调 cron(add) 后,从 result 拿 jobId,
|
|
32
|
+
// 配合当前会话的 pushId,写入 jobId↔pushId 映射,供 fire 时反查设备。
|
|
33
|
+
await captureCronAddMapping(event, ctx).catch((err) => {
|
|
34
|
+
// 捕获失败不影响工具结果
|
|
35
|
+
console.error("[xy] captureCronAddMapping failed:", err);
|
|
36
|
+
});
|
|
28
37
|
});
|
|
29
38
|
}
|
|
39
|
+
/** 从 cron add 工具结果中提取 jobId 并写入 pushId 映射。 */
|
|
40
|
+
async function captureCronAddMapping(event, ctx) {
|
|
41
|
+
// 两条创建路径都要捕获:
|
|
42
|
+
// 1) cron agent 工具:toolName==="cron", params.action==="add"
|
|
43
|
+
// 2) exec 跑 CLI:toolName==="exec", params.command 含 "cron add"
|
|
44
|
+
// (agent 实际用的是这条:openclaw cron add --name ... --cron ... --message ...)
|
|
45
|
+
const isCronAddTool = event.toolName === "cron" &&
|
|
46
|
+
(event.params?.action === "add" || event.params?.action === "create");
|
|
47
|
+
const isExecCronAdd = event.toolName === "exec" && isExecCronAddCommand(event.params?.command);
|
|
48
|
+
if (!isCronAddTool && !isExecCronAdd)
|
|
49
|
+
return;
|
|
50
|
+
console.log(`[CRONMAP] after_tool_call path=${event.toolName}, resultType=${typeof event.result}`);
|
|
51
|
+
const jobId = readJobIdFromResult(event.result);
|
|
52
|
+
if (!jobId) {
|
|
53
|
+
console.log(`[CRONMAP] skip: could not extract jobId. preview=${preview(event.result)}`);
|
|
54
|
+
return;
|
|
55
|
+
}
|
|
56
|
+
console.log(`[CRONMAP] extracted jobId=${jobId}`);
|
|
57
|
+
const sessionCtx = ctx.sessionKey ? getSessionContext(ctx.sessionKey) : null;
|
|
58
|
+
const sessionId = sessionCtx?.sessionId;
|
|
59
|
+
if (!sessionId) {
|
|
60
|
+
console.log(`[CRONMAP] skip: no sessionId (sessionKey=${ctx.sessionKey}, ctxFound=${!!sessionCtx})`);
|
|
61
|
+
return;
|
|
62
|
+
}
|
|
63
|
+
const pushId = await resolvePushId(sessionId);
|
|
64
|
+
if (!pushId) {
|
|
65
|
+
console.log(`[CRONMAP] skip: no pushId available for sessionId=${sessionId} (no session match, no global, no file)`);
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
console.log(`[CRONMAP] writing map: jobId=${jobId}, sessionId=${sessionId}, pushId=${pushId.substring(0, 16)}...`);
|
|
69
|
+
await setJobPushId(jobId, {
|
|
70
|
+
pushId,
|
|
71
|
+
sessionId,
|
|
72
|
+
deviceType: sessionCtx?.deviceType,
|
|
73
|
+
source: event.toolName === "exec" ? "exec-cli" : "conversation",
|
|
74
|
+
});
|
|
75
|
+
console.log(`[CRONMAP] map written OK`);
|
|
76
|
+
}
|
|
77
|
+
/** 回退链取 pushId:当前会话 → 全局兜底 → 本地文件首个(保底)。 */
|
|
78
|
+
async function resolvePushId(sessionId) {
|
|
79
|
+
// 1. 同会话
|
|
80
|
+
const session = configManager.getPushId(sessionId);
|
|
81
|
+
if (session)
|
|
82
|
+
return session;
|
|
83
|
+
// 2. 全局(任何会话注册过的)
|
|
84
|
+
const global = configManager.getPushId();
|
|
85
|
+
if (global)
|
|
86
|
+
return global;
|
|
87
|
+
// 3. 文件兜底
|
|
88
|
+
try {
|
|
89
|
+
const all = await getAllPushIds();
|
|
90
|
+
if (all.length > 0)
|
|
91
|
+
return all[0];
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
// ignore
|
|
95
|
+
}
|
|
96
|
+
return null;
|
|
97
|
+
}
|
|
98
|
+
/** 判断 exec 命令是否为 cron add(匹配 "openclaw cron add" 或裸 "cron add",排除 list/remove 等)。 */
|
|
99
|
+
function isExecCronAddCommand(command) {
|
|
100
|
+
if (typeof command !== "string")
|
|
101
|
+
return false;
|
|
102
|
+
return /\bcron\s+add\b/.test(command);
|
|
103
|
+
}
|
|
104
|
+
/** 取结果的短预览,用于诊断。 */
|
|
105
|
+
function preview(value) {
|
|
106
|
+
if (value == null)
|
|
107
|
+
return String(value);
|
|
108
|
+
const s = typeof value === "string" ? value : JSON.stringify(value);
|
|
109
|
+
return s.length > 200 ? s.slice(0, 200) + "…" : s;
|
|
110
|
+
}
|
|
111
|
+
/** 防御性地从 cron add 结果中取 job id。
|
|
112
|
+
* 覆盖:裸 job 对象、JSON 字符串、exec 输出文本、
|
|
113
|
+
* {content:[{text}]} / {stdout} / data/result/job 嵌套。 */
|
|
114
|
+
function readJobIdFromResult(result) {
|
|
115
|
+
if (!result)
|
|
116
|
+
return undefined;
|
|
117
|
+
// {content: [{type:"text", text: "..."}]} — exec 工具的输出信封
|
|
118
|
+
if (result && typeof result === "object") {
|
|
119
|
+
const contentArr = result.content;
|
|
120
|
+
if (Array.isArray(contentArr)) {
|
|
121
|
+
for (const item of contentArr) {
|
|
122
|
+
if (item && typeof item === "object") {
|
|
123
|
+
const text = item.text;
|
|
124
|
+
if (typeof text === "string" && text.trim()) {
|
|
125
|
+
const fromContent = readJobIdFromResult(text);
|
|
126
|
+
if (fromContent)
|
|
127
|
+
return fromContent;
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
// {stdout} — 备选 exec 输出信封
|
|
134
|
+
if (result && typeof result === "object") {
|
|
135
|
+
const stdout = result.stdout;
|
|
136
|
+
if (typeof stdout === "string" && stdout.trim()) {
|
|
137
|
+
const fromStdout = readJobIdFromResult(stdout);
|
|
138
|
+
if (fromStdout)
|
|
139
|
+
return fromStdout;
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
let obj = result;
|
|
143
|
+
if (typeof result === "string") {
|
|
144
|
+
try {
|
|
145
|
+
obj = JSON.parse(result);
|
|
146
|
+
}
|
|
147
|
+
catch {
|
|
148
|
+
// 纯文本:可能含 stderr 前缀行 + JSON。用正则抓 "id":"..."。
|
|
149
|
+
const m = result.match(/"id"\s*:\s*"([^"]+)"/);
|
|
150
|
+
if (m)
|
|
151
|
+
return m[1];
|
|
152
|
+
return undefined;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
if (obj && typeof obj === "object") {
|
|
156
|
+
const id = obj.id;
|
|
157
|
+
if (typeof id === "string" && id.trim())
|
|
158
|
+
return id.trim();
|
|
159
|
+
for (const k of ["data", "result", "job"]) {
|
|
160
|
+
const inner = obj[k];
|
|
161
|
+
if (inner && typeof inner === "object") {
|
|
162
|
+
const innerId = inner.id;
|
|
163
|
+
if (typeof innerId === "string" && innerId.trim())
|
|
164
|
+
return innerId.trim();
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
return undefined;
|
|
169
|
+
}
|
|
30
170
|
function registerFullHooks(api) {
|
|
31
171
|
// SKILL RETRIEVER HOOK: before_prompt_build hook
|
|
32
172
|
const pluginConfig = api.pluginConfig || {};
|
package/dist/src/bot.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
+
import { updateSessionStoreEntry, updateSessionStore, resolveStorePath } from "openclaw/plugin-sdk/session-store-runtime";
|
|
1
2
|
import { getXYRuntime } from "./runtime.js";
|
|
2
3
|
import { createXYReplyDispatcher } from "./reply-dispatcher.js";
|
|
3
|
-
import { parseA2AMessage, extractTextFromParts, extractFileParts, extractPushId, extractDeviceType, extractTriggerData, extractRunCrossTaskContext } from "./parser.js";
|
|
4
|
+
import { parseA2AMessage, extractTextFromParts, extractFileParts, extractPushId, extractDeviceType, extractModelName, extractTriggerData, extractRunCrossTaskContext } from "./parser.js";
|
|
4
5
|
import { downloadFilesFromParts } from "./file-download.js";
|
|
5
6
|
import { resolveXYConfig } from "./config.js";
|
|
6
7
|
import { sendStatusUpdate, sendClearContextResponse, sendTasksCancelResponse, sendA2AResponse } from "./formatter.js";
|
|
@@ -32,9 +33,9 @@ export async function handleXYMessage(params) {
|
|
|
32
33
|
try {
|
|
33
34
|
// Check for special messages BEFORE parsing (these have different param structures)
|
|
34
35
|
const messageMethod = message.method;
|
|
35
|
-
// Handle clearContext messages (
|
|
36
|
+
// Handle clearContext messages (sessionId at top level, no params)
|
|
36
37
|
if (messageMethod === "clearContext" || messageMethod === "clear_context") {
|
|
37
|
-
const sessionId = message.params?.sessionId;
|
|
38
|
+
const sessionId = message.sessionId ?? message.params?.sessionId;
|
|
38
39
|
if (!sessionId) {
|
|
39
40
|
throw new Error("clearContext request missing sessionId in params");
|
|
40
41
|
}
|
|
@@ -48,9 +49,9 @@ export async function handleXYMessage(params) {
|
|
|
48
49
|
});
|
|
49
50
|
return;
|
|
50
51
|
}
|
|
51
|
-
// Handle tasks/cancel messages
|
|
52
|
+
// Handle tasks/cancel messages (sessionId at top level, no params)
|
|
52
53
|
if (messageMethod === "tasks/cancel" || messageMethod === "tasks_cancel") {
|
|
53
|
-
const sessionId = message.params?.sessionId;
|
|
54
|
+
const sessionId = message.sessionId ?? message.params?.sessionId;
|
|
54
55
|
const taskId = message.params?.id || message.id;
|
|
55
56
|
if (!sessionId) {
|
|
56
57
|
throw new Error("tasks/cancel request missing sessionId in params");
|
|
@@ -138,6 +139,11 @@ export async function handleXYMessage(params) {
|
|
|
138
139
|
if (deviceType) {
|
|
139
140
|
log.log(`[BOT] Extracted deviceType: ${deviceType}`);
|
|
140
141
|
}
|
|
142
|
+
// Extract modelName if present (used by provider.ts to override model.id)
|
|
143
|
+
const modelName = extractModelName(parsed.parts);
|
|
144
|
+
if (modelName) {
|
|
145
|
+
log.log(`[BOT] Extracted modelName: ${modelName}`);
|
|
146
|
+
}
|
|
141
147
|
const runCrossTaskContext = extractRunCrossTaskContext(parsed.parts);
|
|
142
148
|
// Resolve configuration (needed for status updates)
|
|
143
149
|
const config = resolveXYConfig(cfg);
|
|
@@ -164,8 +170,56 @@ export async function handleXYMessage(params) {
|
|
|
164
170
|
messageId: parsed.messageId,
|
|
165
171
|
agentId: route.accountId,
|
|
166
172
|
deviceType,
|
|
173
|
+
modelName,
|
|
167
174
|
runCrossTaskContext: runCrossTaskContext ?? undefined,
|
|
168
175
|
});
|
|
176
|
+
// 🔑 Sync A2A modelName to OpenClaw session store so that session_status
|
|
177
|
+
// reports the correct model. Without this, session_status returns the
|
|
178
|
+
// configured default model instead of the A2A-specified one.
|
|
179
|
+
if (modelName && modelName.trim() !== "" && modelName.toLowerCase() !== "none") {
|
|
180
|
+
try {
|
|
181
|
+
const storePath = resolveStorePath();
|
|
182
|
+
const result = await updateSessionStoreEntry({
|
|
183
|
+
storePath,
|
|
184
|
+
sessionKey: route.sessionKey,
|
|
185
|
+
update: async () => ({
|
|
186
|
+
providerOverride: "xiaoyiprovider",
|
|
187
|
+
modelOverride: modelName,
|
|
188
|
+
modelOverrideSource: "user",
|
|
189
|
+
model: "",
|
|
190
|
+
modelProvider: "",
|
|
191
|
+
contextTokens: 256_000,
|
|
192
|
+
}),
|
|
193
|
+
});
|
|
194
|
+
if (!result) {
|
|
195
|
+
// Session entry doesn't exist yet (first message, xy_channel
|
|
196
|
+
// bypasses the standard turn kernel). Create a minimal entry
|
|
197
|
+
// with the override via updateSessionStore.
|
|
198
|
+
await updateSessionStore(storePath, (store) => {
|
|
199
|
+
if (!store[route.sessionKey]) {
|
|
200
|
+
store[route.sessionKey] = {
|
|
201
|
+
// sessionId must pass validateSessionId regex /^[a-z0-9][a-z0-9._-]{0,127}$/i
|
|
202
|
+
// route.sessionKey like "agent:main:direct:xxx" contains colons which are invalid.
|
|
203
|
+
// Use parsed.sessionId (raw UUID from A2A) which is always safe.
|
|
204
|
+
sessionId: parsed.sessionId,
|
|
205
|
+
updatedAt: Date.now(),
|
|
206
|
+
providerOverride: "xiaoyiprovider",
|
|
207
|
+
modelOverride: modelName,
|
|
208
|
+
modelOverrideSource: "user",
|
|
209
|
+
contextTokens: 256_000,
|
|
210
|
+
};
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
log.log(`[BOT] Created session entry with model override: xiaoyiprovider/${modelName}`);
|
|
214
|
+
}
|
|
215
|
+
else {
|
|
216
|
+
log.log(`[BOT] Patched session store model override: xiaoyiprovider/${modelName}`);
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
catch (patchErr) {
|
|
220
|
+
log.error(`[BOT] Failed to patch session model override:`, patchErr);
|
|
221
|
+
}
|
|
222
|
+
}
|
|
169
223
|
// 🔑 发送初始状态更新
|
|
170
224
|
log.log(`[BOT] Sending initial status update`);
|
|
171
225
|
void sendStatusUpdate({
|
|
@@ -311,6 +365,7 @@ export async function handleXYMessage(params) {
|
|
|
311
365
|
messageId: parsed.messageId,
|
|
312
366
|
agentId: route.accountId,
|
|
313
367
|
deviceType,
|
|
368
|
+
modelName,
|
|
314
369
|
runCrossTaskContext: runCrossTaskContext ?? undefined,
|
|
315
370
|
};
|
|
316
371
|
log.log(`[BOT-DISPATCH] withReplyDispatcher starting, sessionKey=${route.sessionKey}`);
|
|
@@ -2,6 +2,8 @@ import type { XYChannelConfig, A2ACommand } from "./types.js";
|
|
|
2
2
|
export interface SendCommandViaPushParams {
|
|
3
3
|
config: XYChannelConfig;
|
|
4
4
|
command: A2ACommand;
|
|
5
|
+
/** 指定设备的 pushId(多设备路由)。未传时回退到 getAllPushIds()[0]。 */
|
|
6
|
+
pushId?: string;
|
|
5
7
|
}
|
|
6
8
|
/**
|
|
7
9
|
* Send a tool command through the push channel (for cron-triggered tool calls).
|
package/dist/src/cron-command.js
CHANGED
|
@@ -24,16 +24,22 @@ export async function sendCommandViaPush(params) {
|
|
|
24
24
|
command.header?.name ??
|
|
25
25
|
"Command";
|
|
26
26
|
logger.log(`[CRON-CMD] Sending command via push, intent=${intentName}`);
|
|
27
|
-
// 1.
|
|
27
|
+
// 1. 选 pushId:优先用调用方解析出的设备 pushId(多设备路由正确);
|
|
28
|
+
// 未提供时回退到 getAllPushIds()[0](单设备兼容旧行为)。
|
|
28
29
|
let pushId = config.pushId;
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
if (pushIdList.length > 0) {
|
|
32
|
-
pushId = pushIdList[0];
|
|
33
|
-
}
|
|
30
|
+
if (params.pushId) {
|
|
31
|
+
pushId = params.pushId;
|
|
34
32
|
}
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
else {
|
|
34
|
+
try {
|
|
35
|
+
const pushIdList = await getAllPushIds();
|
|
36
|
+
if (pushIdList.length > 0) {
|
|
37
|
+
pushId = pushIdList[0];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
catch (error) {
|
|
41
|
+
logger.error("[CRON-CMD] Failed to load pushIds:", error);
|
|
42
|
+
}
|
|
37
43
|
}
|
|
38
44
|
// 2. Build and send push notification with command in directives
|
|
39
45
|
const pushService = new XYPushService(config);
|
|
@@ -7,6 +7,8 @@ import { callGatewayTool } from "openclaw/plugin-sdk/agent-harness-runtime";
|
|
|
7
7
|
import * as os from "os";
|
|
8
8
|
import { sendCommand } from "./formatter.js";
|
|
9
9
|
import { resolveXYConfig } from "./config.js";
|
|
10
|
+
import { configManager } from "./utils/config-manager.js";
|
|
11
|
+
import { setJobPushId } from "./utils/cron-push-map.js";
|
|
10
12
|
import { logger } from "./utils/logger.js";
|
|
11
13
|
import { readFileSync, readdirSync } from "fs";
|
|
12
14
|
import { join } from "path";
|
|
@@ -19,7 +21,8 @@ const GATEWAY_TIMEOUT_MS = 60_000;
|
|
|
19
21
|
*/
|
|
20
22
|
export async function handleCronQueryEvent(context, cfg) {
|
|
21
23
|
const { action, jobId, params, sessionId, taskId, messageId } = context;
|
|
22
|
-
logger.
|
|
24
|
+
const log = logger.withContext(sessionId ?? "", taskId ?? "");
|
|
25
|
+
log.log(`[CRON-QUERY] Received event: action=${action}, jobId=${jobId ?? "(none)"}`);
|
|
23
26
|
let result;
|
|
24
27
|
let error;
|
|
25
28
|
try {
|
|
@@ -38,6 +41,11 @@ export async function handleCronQueryEvent(context, cfg) {
|
|
|
38
41
|
break;
|
|
39
42
|
case "add":
|
|
40
43
|
result = await callGatewayTool("cron.add", { timeoutMs: GATEWAY_TIMEOUT_MS }, params ?? {});
|
|
44
|
+
// 捕获 jobId↔pushId:cron-query 路径由 channel 自己建 job,
|
|
45
|
+
// 此处 context 握着 sessionId,configManager 有对应设备 pushId。
|
|
46
|
+
await persistCronPushMap(context.sessionId, result).catch((err) => {
|
|
47
|
+
logger.error(`[CRON-QUERY] Failed to persist cron-push-map:`, err);
|
|
48
|
+
});
|
|
41
49
|
break;
|
|
42
50
|
case "update":
|
|
43
51
|
result = await callGatewayTool("cron.update", { timeoutMs: GATEWAY_TIMEOUT_MS }, {
|
|
@@ -62,17 +70,17 @@ export async function handleCronQueryEvent(context, cfg) {
|
|
|
62
70
|
break;
|
|
63
71
|
default:
|
|
64
72
|
error = `Unknown action: ${context.action}`;
|
|
65
|
-
|
|
73
|
+
log.error(`[CRON-QUERY] ${error}`);
|
|
66
74
|
result = { error };
|
|
67
75
|
}
|
|
68
76
|
}
|
|
69
77
|
catch (err) {
|
|
70
78
|
error = err instanceof Error ? err.message : String(err);
|
|
71
|
-
|
|
79
|
+
log.error(`[CRON-QUERY] RPC call failed for action=${action}:`, err);
|
|
72
80
|
result = { error };
|
|
73
81
|
}
|
|
74
82
|
// Log the result
|
|
75
|
-
|
|
83
|
+
log.log(`[CRON-QUERY] RPC result for action=${action}: ${JSON.stringify(result, null, 2)}`);
|
|
76
84
|
// Send result back via sendCommand as System.CronQuery with payload.ans
|
|
77
85
|
if (cfg && sessionId && taskId && messageId) {
|
|
78
86
|
try {
|
|
@@ -93,17 +101,46 @@ export async function handleCronQueryEvent(context, cfg) {
|
|
|
93
101
|
taskId,
|
|
94
102
|
messageId,
|
|
95
103
|
command,
|
|
96
|
-
final:
|
|
104
|
+
final: sessionId.toLowerCase().endsWith("cronquery"),
|
|
97
105
|
});
|
|
98
|
-
|
|
106
|
+
log.log(`[CRON-QUERY] Sent response via sendCommand, action=${action}`);
|
|
99
107
|
}
|
|
100
108
|
catch (sendErr) {
|
|
101
|
-
|
|
109
|
+
log.error(`[CRON-QUERY] Failed to send response via sendCommand:`, sendErr);
|
|
102
110
|
}
|
|
103
111
|
}
|
|
104
112
|
else {
|
|
105
|
-
|
|
113
|
+
log.warn(`[CRON-QUERY] Missing cfg/sessionId/taskId/messageId, skipping sendCommand`);
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* 从 cron.add 结果中提取 jobId,配合 sessionId 对应的 pushId 写入映射。
|
|
118
|
+
*/
|
|
119
|
+
async function persistCronPushMap(sessionId, result) {
|
|
120
|
+
logger.log(`[CRONMAP] cron-query persist: sessionId=${sessionId ?? "(none)"}, resultType=${typeof result}`);
|
|
121
|
+
if (!sessionId) {
|
|
122
|
+
logger.log(`[CRONMAP] cron-query skip: no sessionId in context`);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
let jobId;
|
|
126
|
+
if (result && typeof result === "object") {
|
|
127
|
+
const id = result.id;
|
|
128
|
+
if (typeof id === "string" && id.trim())
|
|
129
|
+
jobId = id.trim();
|
|
130
|
+
}
|
|
131
|
+
if (!jobId) {
|
|
132
|
+
const preview = typeof result === "string" ? result.slice(0, 200) : JSON.stringify(result)?.slice(0, 200);
|
|
133
|
+
logger.log(`[CRONMAP] cron-query skip: no jobId in result. preview=${preview ?? "(empty)"}`);
|
|
134
|
+
return;
|
|
135
|
+
}
|
|
136
|
+
const pushId = configManager.getPushId(sessionId);
|
|
137
|
+
if (!pushId) {
|
|
138
|
+
logger.log(`[CRONMAP] cron-query skip: configManager has no pushId for sessionId=${sessionId}`);
|
|
139
|
+
return;
|
|
106
140
|
}
|
|
141
|
+
logger.log(`[CRONMAP] cron-query writing map: jobId=${jobId}, pushId=${pushId.substring(0, 16)}...`);
|
|
142
|
+
await setJobPushId(jobId, { pushId, sessionId, source: "cron-query" });
|
|
143
|
+
logger.log(`[CRONMAP] cron-query map written OK`);
|
|
107
144
|
}
|
|
108
145
|
/**
|
|
109
146
|
* Read local cron folder directly (bypassing openclaw RPC) and return
|
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
import https from 'https';
|
|
5
5
|
import { URL } from 'url';
|
|
6
6
|
import { getConfig } from './config.js';
|
|
7
|
-
import {
|
|
7
|
+
import { DEFAULT_HTTPS_PORT, HTTP_STATUS_BAD_REQUEST, API_URL_SUFFIX } from './constants.js';
|
|
8
8
|
function buildHeadersForCelia(config, sessionId) {
|
|
9
9
|
if (!config.uid || !config.apiKey || !config.skillId || !config.requestFrom) {
|
|
10
10
|
throw new Error('[SENTINEL HOOK] Missing required configuration: uid, apiKey, skillId, or requestFrom is not defined');
|
|
@@ -22,7 +22,7 @@ function buildRequestOptions(url, headers, timeout) {
|
|
|
22
22
|
const urlObj = new URL(url);
|
|
23
23
|
return {
|
|
24
24
|
hostname: urlObj.hostname,
|
|
25
|
-
port: urlObj.port ||
|
|
25
|
+
port: urlObj.port || DEFAULT_HTTPS_PORT,
|
|
26
26
|
path: urlObj.pathname,
|
|
27
27
|
method: "POST",
|
|
28
28
|
headers: headers,
|
|
@@ -15,16 +15,21 @@ export default function register(api) {
|
|
|
15
15
|
// 生成sessionID
|
|
16
16
|
const sessionId = (event.runId?.replace(/-/g, '') || crypto.randomBytes(16).toString('hex'));
|
|
17
17
|
logger.log(`[SENTINEL HOOK] Generated Session ID: ${sessionId}`);
|
|
18
|
-
// 处理 TOOL_INPUT
|
|
18
|
+
// 处理 TOOL_INPUT 数据采集、发送数据,根据扫描结果决定是否阻塞
|
|
19
19
|
try {
|
|
20
|
+
let scanResult = null;
|
|
20
21
|
if (event.toolName === 'exec') {
|
|
21
|
-
await handleExecToolInput(event, api, sessionId);
|
|
22
|
+
scanResult = await handleExecToolInput(event, api, sessionId);
|
|
22
23
|
}
|
|
23
24
|
else if (event.toolName === 'message') {
|
|
24
|
-
await handleMessageToolInput(event, api, sessionId);
|
|
25
|
+
scanResult = await handleMessageToolInput(event, api, sessionId);
|
|
25
26
|
}
|
|
26
27
|
else {
|
|
27
|
-
await handleOtherToolInput(event, api, sessionId);
|
|
28
|
+
scanResult = await handleOtherToolInput(event, api, sessionId);
|
|
29
|
+
}
|
|
30
|
+
if (scanResult?.status === 'REJECT') {
|
|
31
|
+
logger.warn(`[SENTINEL HOOK] TOOL_INPUT REJECT, blocking tool call: ${event.toolName}`);
|
|
32
|
+
return { block: true, blockReason: `安全扫描检测到风险,已阻止工具调用: ${event.toolName}` };
|
|
28
33
|
}
|
|
29
34
|
}
|
|
30
35
|
catch (error) {
|
|
@@ -6,7 +6,7 @@ import path from 'path';
|
|
|
6
6
|
import https from 'https';
|
|
7
7
|
import { URL } from 'url';
|
|
8
8
|
import { getConfig } from './config.js';
|
|
9
|
-
import {
|
|
9
|
+
import { DEFAULT_HTTPS_PORT, MAX_TIMES, CONNECT_TIMEOUT, READ_TIMEOUT, EXPIRE_TIME, OSMS_PREPARE_URL, OSMS_COMPLETE_URL, TEMPORARY_MATERIAL_PACKAGE, FILE_OWNER_UID, FILE_OWNER_TEAM_ID } from './constants.js';
|
|
10
10
|
function buildOsmsHeaders(config, traceId) {
|
|
11
11
|
return {
|
|
12
12
|
'content-type': 'application/json',
|
|
@@ -22,7 +22,7 @@ function httpRequest(url, method, headers, body, timeout) {
|
|
|
22
22
|
const urlObj = new URL(url);
|
|
23
23
|
const options = {
|
|
24
24
|
hostname: urlObj.hostname,
|
|
25
|
-
port: urlObj.port ||
|
|
25
|
+
port: urlObj.port || DEFAULT_HTTPS_PORT,
|
|
26
26
|
path: urlObj.pathname + urlObj.search,
|
|
27
27
|
method: method,
|
|
28
28
|
headers: headers,
|
package/dist/src/cspl/utils.d.ts
CHANGED
|
@@ -14,6 +14,12 @@ export declare function extractFilePathsFromCommand(command: string): string[];
|
|
|
14
14
|
export declare function calculateContentHash(content: string): string;
|
|
15
15
|
export declare function getFileSizeInKB(filePath: string): number;
|
|
16
16
|
export declare function adjustContentLength(data: any, api: OpenClawPluginApi, fields: string[]): any;
|
|
17
|
-
export declare function handleExecToolInput(event: any, api: OpenClawPluginApi, sessionId: string): Promise<
|
|
18
|
-
|
|
19
|
-
|
|
17
|
+
export declare function handleExecToolInput(event: any, api: OpenClawPluginApi, sessionId: string): Promise<{
|
|
18
|
+
status: 'ACCEPT' | 'REJECT';
|
|
19
|
+
} | null>;
|
|
20
|
+
export declare function handleMessageToolInput(event: any, api: OpenClawPluginApi, sessionId: string): Promise<{
|
|
21
|
+
status: 'ACCEPT' | 'REJECT';
|
|
22
|
+
} | null>;
|
|
23
|
+
export declare function handleOtherToolInput(event: any, api: OpenClawPluginApi, sessionId: string): Promise<{
|
|
24
|
+
status: 'ACCEPT' | 'REJECT';
|
|
25
|
+
} | null>;
|
package/dist/src/cspl/utils.js
CHANGED
|
@@ -213,13 +213,14 @@ export function adjustContentLength(data, api, fields) {
|
|
|
213
213
|
}
|
|
214
214
|
return adjusted;
|
|
215
215
|
}
|
|
216
|
-
// 发送TOOL_INPUT
|
|
216
|
+
// 发送TOOL_INPUT请求并处理响应,返回扫描结果
|
|
217
217
|
async function sendToolInputRequest(postText, api, sessionId) {
|
|
218
218
|
const response = await callApi(postText, api, sessionId, TOOL_INPUT_ACTION);
|
|
219
219
|
const result = parseSecurityResult(response);
|
|
220
220
|
logger.log(`[SENTINEL HOOK] TOOL_INPUT response: status=${result.status}`);
|
|
221
|
+
return result;
|
|
221
222
|
}
|
|
222
|
-
// 处理exec工具的TOOL_INPUT
|
|
223
|
+
// 处理exec工具的TOOL_INPUT数据采集,返回最终扫描结果
|
|
223
224
|
export async function handleExecToolInput(event, api, sessionId) {
|
|
224
225
|
const command = extractInputParams(event, 'exec');
|
|
225
226
|
if (!command) {
|
|
@@ -232,6 +233,7 @@ export async function handleExecToolInput(event, api, sessionId) {
|
|
|
232
233
|
// 场景1:执行代码文件
|
|
233
234
|
logger.log(`[SENTINEL HOOK] Found ${filePaths.length} file(s) in command`);
|
|
234
235
|
const nonExistingFiles = [];
|
|
236
|
+
let lastResult = null;
|
|
235
237
|
for (const filePath of filePaths) {
|
|
236
238
|
if (!fs.existsSync(filePath)) {
|
|
237
239
|
nonExistingFiles.push(filePath);
|
|
@@ -247,7 +249,10 @@ export async function handleExecToolInput(event, api, sessionId) {
|
|
|
247
249
|
const postText = JSON.stringify(adjustedData);
|
|
248
250
|
logger.log(`[SENTINEL HOOK] Sending TOOL_INPUT for file: ${path.basename(filePath)}, body length: ${postText.length}`);
|
|
249
251
|
try {
|
|
250
|
-
await sendToolInputRequest(postText, api, sessionId);
|
|
252
|
+
lastResult = await sendToolInputRequest(postText, api, sessionId);
|
|
253
|
+
if (lastResult.status === 'REJECT') {
|
|
254
|
+
return lastResult;
|
|
255
|
+
}
|
|
251
256
|
}
|
|
252
257
|
catch (e) {
|
|
253
258
|
logger.error(`[SENTINEL HOOK] Sending TOOL_INPUT Failed: ${e}`);
|
|
@@ -258,6 +263,7 @@ export async function handleExecToolInput(event, api, sessionId) {
|
|
|
258
263
|
const fileNames = nonExistingFiles.map(f => path.basename(f)).join(', ');
|
|
259
264
|
logger.log(`[SENTINEL HOOK] Non-existing files: ${fileNames}`);
|
|
260
265
|
}
|
|
266
|
+
return lastResult;
|
|
261
267
|
}
|
|
262
268
|
else {
|
|
263
269
|
// 场景2:直接执行代码(heredoc场景)
|
|
@@ -268,10 +274,10 @@ export async function handleExecToolInput(event, api, sessionId) {
|
|
|
268
274
|
const adjustedData = adjustContentLength(toolInputData, api, ['source']);
|
|
269
275
|
const postText = JSON.stringify(adjustedData);
|
|
270
276
|
logger.log(`[SENTINEL HOOK] Sending TOOL_INPUT for direct code execution, body length: ${postText.length}`);
|
|
271
|
-
await sendToolInputRequest(postText, api, sessionId);
|
|
277
|
+
return await sendToolInputRequest(postText, api, sessionId);
|
|
272
278
|
}
|
|
273
279
|
}
|
|
274
|
-
// 处理message工具的TOOL_INPUT
|
|
280
|
+
// 处理message工具的TOOL_INPUT数据采集,返回扫描结果
|
|
275
281
|
export async function handleMessageToolInput(event, api, sessionId) {
|
|
276
282
|
const message = extractInputParams(event, 'message');
|
|
277
283
|
if (!message) {
|
|
@@ -285,14 +291,14 @@ export async function handleMessageToolInput(event, api, sessionId) {
|
|
|
285
291
|
const adjustedData = adjustContentLength(toolInputData, api, ['content']);
|
|
286
292
|
const postText = JSON.stringify(adjustedData);
|
|
287
293
|
logger.log(`[SENTINEL HOOK] Sending TOOL_INPUT for message, body length: ${postText.length}`);
|
|
288
|
-
await sendToolInputRequest(postText, api, sessionId);
|
|
294
|
+
return await sendToolInputRequest(postText, api, sessionId);
|
|
289
295
|
}
|
|
290
|
-
// 处理其他工具(非 exec 和非 message)的 TOOL_INPUT
|
|
296
|
+
// 处理其他工具(非 exec 和非 message)的 TOOL_INPUT 数据采集,返回扫描结果
|
|
291
297
|
export async function handleOtherToolInput(event, api, sessionId) {
|
|
292
298
|
const params = event.params;
|
|
293
299
|
if (!params) {
|
|
294
300
|
logger.log('[SENTINEL HOOK] No params found for tool');
|
|
295
|
-
return;
|
|
301
|
+
return null;
|
|
296
302
|
}
|
|
297
303
|
logger.log(`[SENTINEL HOOK] Processing other tool input, toolName: ${event.toolName}`);
|
|
298
304
|
// 将 params 序列化为 JSON 字符串
|
|
@@ -305,5 +311,5 @@ export async function handleOtherToolInput(event, api, sessionId) {
|
|
|
305
311
|
const adjustedData = adjustContentLength(toolInputData, api, ['content']);
|
|
306
312
|
const postText = JSON.stringify(adjustedData);
|
|
307
313
|
logger.log(`[SENTINEL HOOK] Sending TOOL_INPUT for ${event.toolName}, body length: ${postText.length}`);
|
|
308
|
-
await sendToolInputRequest(postText, api, sessionId);
|
|
314
|
+
return await sendToolInputRequest(postText, api, sessionId);
|
|
309
315
|
}
|
|
@@ -17,6 +17,11 @@ export declare class XYFileUploadService {
|
|
|
17
17
|
* Uses completeAndQuery endpoint to get the file URL directly.
|
|
18
18
|
*/
|
|
19
19
|
uploadFileAndGetUrl(filePath: string, objectType?: string): Promise<string>;
|
|
20
|
+
/**
|
|
21
|
+
* Upload a file and return a preview-able URL (needPreview=true).
|
|
22
|
+
* Same as uploadFileAndGetUrl but adds needPreview flag to get a directly viewable URL.
|
|
23
|
+
*/
|
|
24
|
+
uploadFileAndGetPreviewUrl(filePath: string, objectType?: string): Promise<string>;
|
|
20
25
|
/**
|
|
21
26
|
* Upload multiple files and return their file IDs.
|
|
22
27
|
*/
|