@ynhcj/xiaoyi-channel 0.0.46-beta → 0.0.46-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.
Files changed (76) hide show
  1. package/dist/index.d.ts +0 -2
  2. package/dist/index.js +44 -2
  3. package/dist/src/bot.d.ts +1 -0
  4. package/dist/src/bot.js +47 -51
  5. package/dist/src/channel.js +29 -4
  6. package/dist/src/client.js +0 -9
  7. package/dist/src/cspl/call-api.d.ts +3 -0
  8. package/dist/src/cspl/call-api.js +86 -0
  9. package/dist/src/cspl/config.d.ts +19 -0
  10. package/dist/src/cspl/config.js +50 -0
  11. package/dist/src/cspl/constants.d.ts +43 -0
  12. package/dist/src/cspl/constants.js +22 -0
  13. package/dist/src/cspl/utils.d.ts +10 -0
  14. package/dist/src/cspl/utils.js +57 -0
  15. package/dist/src/file-upload.d.ts +5 -0
  16. package/dist/src/file-upload.js +88 -6
  17. package/dist/src/formatter.d.ts +2 -0
  18. package/dist/src/formatter.js +13 -28
  19. package/dist/src/heartbeat.js +0 -4
  20. package/dist/src/monitor.js +8 -10
  21. package/dist/src/onboarding.d.ts +3 -4
  22. package/dist/src/onboarding.js +2 -2
  23. package/dist/src/outbound.d.ts +2 -1
  24. package/dist/src/outbound.js +1 -19
  25. package/dist/src/parser.d.ts +6 -0
  26. package/dist/src/parser.js +16 -0
  27. package/dist/src/provider.d.ts +2 -0
  28. package/dist/src/provider.js +80 -0
  29. package/dist/src/push.js +0 -21
  30. package/dist/src/reply-dispatcher.d.ts +4 -0
  31. package/dist/src/reply-dispatcher.js +46 -6
  32. package/dist/src/steer-injector.d.ts +16 -0
  33. package/dist/src/steer-injector.js +74 -0
  34. package/dist/src/thread-bindings.d.ts +54 -0
  35. package/dist/src/thread-bindings.js +214 -0
  36. package/dist/src/tools/calendar-tool.js +2 -37
  37. package/dist/src/tools/call-phone-tool.js +3 -60
  38. package/dist/src/tools/create-alarm-tool.js +6 -91
  39. package/dist/src/tools/delete-alarm-tool.js +3 -56
  40. package/dist/src/tools/device-tool-map.d.ts +4 -0
  41. package/dist/src/tools/device-tool-map.js +31 -0
  42. package/dist/src/tools/image-reading-tool.d.ts +5 -0
  43. package/dist/src/tools/image-reading-tool.js +328 -0
  44. package/dist/src/tools/location-tool.js +1 -32
  45. package/dist/src/tools/modify-alarm-tool.js +6 -94
  46. package/dist/src/tools/modify-note-tool.js +1 -34
  47. package/dist/src/tools/note-tool.js +2 -4
  48. package/dist/src/tools/search-alarm-tool.js +12 -118
  49. package/dist/src/tools/search-calendar-tool.js +4 -81
  50. package/dist/src/tools/search-contact-tool.js +2 -55
  51. package/dist/src/tools/search-file-tool.js +4 -61
  52. package/dist/src/tools/search-message-tool.js +2 -59
  53. package/dist/src/tools/search-note-tool.js +4 -22
  54. package/dist/src/tools/search-photo-gallery-tool.js +7 -56
  55. package/dist/src/tools/send-command-to-car-tool.d.ts +5 -0
  56. package/dist/src/tools/send-command-to-car-tool.js +85 -0
  57. package/dist/src/tools/send-file-to-user-tool.js +1 -39
  58. package/dist/src/tools/send-message-tool.js +5 -56
  59. package/dist/src/tools/session-manager.d.ts +1 -0
  60. package/dist/src/tools/session-manager.js +0 -45
  61. package/dist/src/tools/timestamp-to-utc8-tool.d.ts +12 -0
  62. package/dist/src/tools/timestamp-to-utc8-tool.js +104 -0
  63. package/dist/src/tools/upload-file-tool.js +0 -49
  64. package/dist/src/tools/upload-photo-tool.js +0 -42
  65. package/dist/src/tools/view-push-result-tool.js +0 -11
  66. package/dist/src/tools/xiaoyi-collection-tool.js +4 -82
  67. package/dist/src/tools/xiaoyi-gui-tool.js +0 -34
  68. package/dist/src/utils/runtime-manager.d.ts +7 -0
  69. package/dist/src/utils/runtime-manager.js +42 -0
  70. package/dist/src/websocket.js +33 -13
  71. package/openclaw.plugin.json +1 -0
  72. package/package.json +3 -4
  73. package/dist/src/tools/search-photo-tool.d.ts +0 -9
  74. package/dist/src/tools/search-photo-tool.js +0 -270
  75. package/dist/src/utils/session.d.ts +0 -34
  76. package/dist/src/utils/session.js +0 -50
package/dist/index.d.ts CHANGED
@@ -1,5 +1,4 @@
1
1
  import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
2
- import { xyPlugin } from "./src/channel.js";
3
2
  /**
4
3
  * Xiaoyi Channel Plugin Entry Point.
5
4
  * Exports the plugin for OpenClaw to load.
@@ -13,4 +12,3 @@ declare const plugin: {
13
12
  register(api: OpenClawPluginApi): void;
14
13
  };
15
14
  export default plugin;
16
- export { xyPlugin };
package/dist/index.js CHANGED
@@ -1,6 +1,11 @@
1
1
  import { emptyPluginConfigSchema } from "openclaw/plugin-sdk";
2
2
  import { xyPlugin } from "./src/channel.js";
3
+ import { xiaoyiProvider } from "./src/provider.js";
3
4
  import { setXYRuntime } from "./src/runtime.js";
5
+ import { tryInjectSteer } from "./src/steer-injector.js";
6
+ import { callCsplApi } from "./src/cspl/call-api.js";
7
+ import { extractResultText, processText, parseSecurityResult, validateAndTruncateText } from "./src/cspl/utils.js";
8
+ import { ALLOWED_TOOLS, MIN_TEXT_LENGTH, MAX_TOTAL_LENGTH, MAX_TEXT_LENGTH, STEER_ABORT_MESSAGE, } from "./src/cspl/constants.js";
4
9
  /**
5
10
  * Xiaoyi Channel Plugin Entry Point.
6
11
  * Exports the plugin for OpenClaw to load.
@@ -14,8 +19,45 @@ const plugin = {
14
19
  register(api) {
15
20
  setXYRuntime(api.runtime);
16
21
  api.registerChannel({ plugin: xyPlugin });
22
+ api.registerProvider(xiaoyiProvider);
23
+ // CSPL after_tool_call hook: 监听工具结果,发送至 CSPL API 进行安全检测
24
+ // 如果响应为 REJECT,注入 steer 消息中止当前对话
25
+ api.on("after_tool_call", async (event, ctx) => {
26
+ if (!ALLOWED_TOOLS.includes(event.toolName)) {
27
+ return;
28
+ }
29
+ console.log(`[CSPL] after_tool_call triggered: toolName=${event.toolName}, sessionKey=${ctx.sessionKey ?? "none"}`);
30
+ try {
31
+ const resultText = extractResultText(event, event.toolName);
32
+ const resultLength = resultText.length;
33
+ if (resultLength <= MIN_TEXT_LENGTH || resultLength > MAX_TOTAL_LENGTH) {
34
+ return;
35
+ }
36
+ // 构造 sentinel_hook 格式的 payload: { tool, output: [{ content }] }
37
+ const questionText = {
38
+ tool: event.toolName,
39
+ output: [{ content: "" }],
40
+ };
41
+ const originText = processText(resultText);
42
+ questionText.output[0].content = originText;
43
+ let finalJson = JSON.stringify(questionText);
44
+ if (finalJson.length > MAX_TEXT_LENGTH) {
45
+ const diff = finalJson.length - MAX_TEXT_LENGTH;
46
+ const { text: trimmed } = validateAndTruncateText(originText, MAX_TEXT_LENGTH - diff);
47
+ questionText.output[0].content = trimmed;
48
+ finalJson = JSON.stringify(questionText);
49
+ }
50
+ const response = await callCsplApi(finalJson, api.config);
51
+ const result = parseSecurityResult(response);
52
+ console.log(`[CSPL] Security result: status=${result.status}`);
53
+ if (result.status === "REJECT") {
54
+ await tryInjectSteer(ctx.sessionKey, STEER_ABORT_MESSAGE);
55
+ }
56
+ }
57
+ catch (err) {
58
+ api.logger.error(`[CSPL] after_tool_call error: ${err}`);
59
+ }
60
+ });
17
61
  },
18
62
  };
19
63
  export default plugin;
20
- // Also export the plugin directly for testing
21
- export { xyPlugin };
package/dist/src/bot.d.ts CHANGED
@@ -8,6 +8,7 @@ export interface HandleXYMessageParams {
8
8
  runtime: RuntimeEnv;
9
9
  message: A2AJsonRpcRequest;
10
10
  accountId: string;
11
+ webSocketSessionId?: string;
11
12
  }
12
13
  /**
13
14
  * Handle an incoming A2A message.
package/dist/src/bot.js CHANGED
@@ -1,6 +1,7 @@
1
1
  import { getXYRuntime } from "./runtime.js";
2
+ import { setCachedContext } from "./steer-injector.js";
2
3
  import { createXYReplyDispatcher } from "./reply-dispatcher.js";
3
- import { parseA2AMessage, extractTextFromParts, extractFileParts, extractPushId, extractTriggerData } from "./parser.js";
4
+ import { parseA2AMessage, extractTextFromParts, extractFileParts, extractPushId, extractDeviceType, extractTriggerData } 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";
@@ -8,6 +9,7 @@ import { registerSession, unregisterSession, runWithSessionContext } from "./too
8
9
  import { configManager } from "./utils/config-manager.js";
9
10
  import { addPushId } from "./utils/pushid-manager.js";
10
11
  import { getPushDataById } from "./utils/pushdata-manager.js";
12
+ import { saveRuntimeInfo } from "./utils/runtime-manager.js";
11
13
  import { registerTaskId, decrementTaskIdRef, lockTaskId, unlockTaskId, hasActiveTask, } from "./task-manager.js";
12
14
  /**
13
15
  * Handle an incoming A2A message.
@@ -15,16 +17,16 @@ import { registerTaskId, decrementTaskIdRef, lockTaskId, unlockTaskId, hasActive
15
17
  * Runtime is expected to be validated before calling this function.
16
18
  */
17
19
  export async function handleXYMessage(params) {
18
- const { cfg, runtime, message, accountId } = params;
20
+ const { cfg, runtime, message, accountId, webSocketSessionId } = params;
19
21
  const log = runtime?.log ?? console.log;
20
22
  const error = runtime?.error ?? console.error;
23
+ // 每次收到消息时更新缓存,供 steer 注入使用
24
+ setCachedContext(cfg, runtime, accountId);
21
25
  // Get runtime (already validated in monitor.ts, but get reference for use)
22
26
  const core = getXYRuntime();
23
27
  try {
24
28
  // Check for special messages BEFORE parsing (these have different param structures)
25
29
  const messageMethod = message.method;
26
- log(`[BOT-ENTRY] <<<<<<< Received message with method: ${messageMethod}, id: ${message.id} >>>>>>>`);
27
- log(`[BOT-ENTRY] Stack trace for debugging:`, new Error().stack?.split('\n').slice(1, 4).join('\n'));
28
30
  // Handle clearContext messages (params only has sessionId)
29
31
  if (messageMethod === "clearContext" || messageMethod === "clear_context") {
30
32
  const sessionId = message.params?.sessionId;
@@ -75,8 +77,6 @@ export async function handleXYMessage(params) {
75
77
  }
76
78
  log(`[BOT] ✅ Found pushData, sending direct response`);
77
79
  log(`[BOT] - pushDataId: ${pushDataItem.pushDataId}`);
78
- log(`[BOT] - time: ${pushDataItem.time}`);
79
- log(`[BOT] - content length: ${pushDataItem.dataDetail.length} chars`);
80
80
  const config = resolveXYConfig(cfg);
81
81
  // 直接发送响应(final=true,不走 openclaw 流程)
82
82
  await sendA2AResponse({
@@ -117,9 +117,6 @@ export async function handleXYMessage(params) {
117
117
  const pushId = extractPushId(parsed.parts);
118
118
  if (pushId) {
119
119
  log(`[BOT] 📌 Extracted push_id from user message`);
120
- log(`[BOT] - Session ID: ${parsed.sessionId}`);
121
- log(`[BOT] - Push ID preview: ${pushId.substring(0, 20)}...`);
122
- log(`[BOT] - Full push_id: ${pushId}`);
123
120
  configManager.updatePushId(parsed.sessionId, pushId);
124
121
  // 持久化 pushId 到本地文件(异步,不阻塞主流程)
125
122
  addPushId(pushId).catch((err) => {
@@ -129,6 +126,18 @@ export async function handleXYMessage(params) {
129
126
  else {
130
127
  log(`[BOT] ℹ️ No push_id found in message, will use config default`);
131
128
  }
129
+ // Extract deviceType if present (same level as push_id in systemVariables)
130
+ const deviceType = extractDeviceType(parsed.parts);
131
+ if (deviceType) {
132
+ log(`[BOT] 📱 Extracted deviceType from user message: ${deviceType}`);
133
+ }
134
+ // 保存 runtime 信息到 .xiaoyiruntime 文件(异步,不阻塞主流程)
135
+ saveRuntimeInfo(webSocketSessionId || parsed.sessionId, // SESSION_ID (WebSocket 层级,如果没有则 fallback)
136
+ parsed.sessionId, // CONVERSATION_ID (param 里的 sessionId)
137
+ parsed.taskId // TASK_ID (param.id)
138
+ ).catch((err) => {
139
+ error(`[BOT] Failed to save runtime info:`, err);
140
+ });
132
141
  // Resolve configuration (needed for status updates)
133
142
  const config = resolveXYConfig(cfg);
134
143
  // ✅ Resolve agent route (following feishu pattern)
@@ -144,12 +153,6 @@ export async function handleXYMessage(params) {
144
153
  },
145
154
  });
146
155
  log(`xy: resolved route accountId=${route.accountId}, sessionKey=${route.sessionKey}`);
147
- // 🔑 注册session(带引用计数)
148
- log(`[BOT] 📝 About to register session for tools...`);
149
- log(`[BOT] - sessionKey: ${route.sessionKey}`);
150
- log(`[BOT] - sessionId: ${parsed.sessionId}`);
151
- log(`[BOT] - taskId: ${parsed.taskId}`);
152
- log(`[BOT] - isSecondMessage: ${isSecondMessage}`);
153
156
  registerSession(route.sessionKey, {
154
157
  config,
155
158
  sessionId: parsed.sessionId,
@@ -157,7 +160,6 @@ export async function handleXYMessage(params) {
157
160
  messageId: parsed.messageId,
158
161
  agentId: route.accountId,
159
162
  });
160
- log(`[BOT] ✅ Session registered for tools`);
161
163
  // 🔑 发送初始状态更新(第二条消息也要发,用新taskId)
162
164
  log(`[STATUS] Sending initial status update for session ${parsed.sessionId}`);
163
165
  void sendStatusUpdate({
@@ -165,7 +167,7 @@ export async function handleXYMessage(params) {
165
167
  sessionId: parsed.sessionId,
166
168
  taskId: parsed.taskId,
167
169
  messageId: parsed.messageId,
168
- text: isSecondMessage ? "新消息已接收,正在处理..." : "任务正在处理中,请稍后~",
170
+ text: isSecondMessage ? "新消息已接收,正在处理..." : "任务正在处理中,请稍候~",
169
171
  state: "working",
170
172
  }).catch((err) => {
171
173
  error(`Failed to send initial status update:`, err);
@@ -218,9 +220,7 @@ export async function handleXYMessage(params) {
218
220
  });
219
221
  // 🔑 创建dispatcher(dispatcher会自动使用动态taskId)
220
222
  log(`[BOT-DISPATCHER] 🎯 Creating reply dispatcher`);
221
- log(`[BOT-DISPATCHER] - session: ${parsed.sessionId}`);
222
223
  log(`[BOT-DISPATCHER] - taskId: ${parsed.taskId}`);
223
- log(`[BOT-DISPATCHER] - isSecondMessage: ${isSecondMessage}`);
224
224
  const { dispatcher, replyOptions, markDispatchIdle, startStatusInterval } = createXYReplyDispatcher({
225
225
  cfg,
226
226
  runtime,
@@ -230,19 +230,12 @@ export async function handleXYMessage(params) {
230
230
  accountId: route.accountId,
231
231
  isSteerFollower: isSecondMessage, // 🔑 标记第二条消息
232
232
  });
233
- log(`[BOT-DISPATCHER] ✅ Reply dispatcher created successfully`);
234
233
  // 🔑 只有第一条消息启动状态定时器
235
234
  // 第二条消息会很快返回,不需要定时器
236
235
  if (!isSecondMessage) {
237
236
  startStatusInterval();
238
237
  log(`[BOT-DISPATCHER] ✅ Status interval started for first message`);
239
238
  }
240
- else {
241
- log(`[BOT-DISPATCHER] ⏭️ Skipped status interval for steer follower`);
242
- }
243
- log(`xy: dispatching to agent (session=${parsed.sessionId})`);
244
- // Dispatch to OpenClaw core using correct API (following feishu pattern)
245
- log(`[BOT] 🚀 Starting dispatcher with session: ${route.sessionKey}`);
246
239
  // Build session context for AsyncLocalStorage
247
240
  const sessionContext = {
248
241
  config,
@@ -250,13 +243,14 @@ export async function handleXYMessage(params) {
250
243
  taskId: parsed.taskId,
251
244
  messageId: parsed.messageId,
252
245
  agentId: route.accountId,
246
+ deviceType,
253
247
  };
248
+ log(`[BOT-DISPATCH] ⏳ withReplyDispatcher starting, sessionKey=${route.sessionKey}`);
254
249
  await core.channel.reply.withReplyDispatcher({
255
250
  dispatcher,
256
251
  onSettled: () => {
257
252
  log(`[BOT] 🏁 onSettled called for session: ${route.sessionKey}`);
258
253
  log(`[BOT] - isSecondMessage: ${isSecondMessage}`);
259
- markDispatchIdle();
260
254
  // 🔑 减少引用计数
261
255
  decrementTaskIdRef(parsed.sessionId);
262
256
  // 🔑 如果是第一条消息完成,解锁
@@ -270,12 +264,32 @@ export async function handleXYMessage(params) {
270
264
  },
271
265
  run: () =>
272
266
  // 🔐 Use AsyncLocalStorage to provide session context to tools
273
- runWithSessionContext(sessionContext, () => core.channel.reply.dispatchReplyFromConfig({
274
- ctx: ctxPayload,
275
- cfg,
276
- dispatcher,
277
- replyOptions,
278
- })),
267
+ runWithSessionContext(sessionContext, async () => {
268
+ log(`[BOT-DISPATCH] ⏳ dispatchReplyFromConfig starting...`);
269
+ log(`[BOT-DISPATCH] - sessionKey: ${ctxPayload.SessionKey}`);
270
+ log(`[BOT-DISPATCH] - provider: ${ctxPayload.Provider}`);
271
+ log(`[BOT-DISPATCH] - surface: ${ctxPayload.Surface}`);
272
+ log(`[BOT-DISPATCH] - from: ${ctxPayload.From}`);
273
+ log(`[BOT-DISPATCH] - body length: ${ctxPayload.Body?.length ?? 0}`);
274
+ try {
275
+ const result = await core.channel.reply.dispatchReplyFromConfig({
276
+ ctx: ctxPayload,
277
+ cfg,
278
+ dispatcher,
279
+ replyOptions,
280
+ });
281
+ log(`[BOT-DISPATCH] ✅ dispatchReplyFromConfig returned`);
282
+ log(`[BOT-DISPATCH] - result: ${JSON.stringify(result)}`);
283
+ return result;
284
+ }
285
+ catch (dispatchErr) {
286
+ error(`[BOT-DISPATCH] ❌ dispatchReplyFromConfig threw`);
287
+ error(`[BOT-DISPATCH] - error name: ${dispatchErr instanceof Error ? dispatchErr.name : "unknown"}`);
288
+ error(`[BOT-DISPATCH] - error message: ${String(dispatchErr)}`);
289
+ error(`[BOT-DISPATCH] - error stack: ${dispatchErr instanceof Error ? dispatchErr.stack?.slice(0, 500) : "N/A"}`);
290
+ throw dispatchErr;
291
+ }
292
+ }),
279
293
  });
280
294
  log(`[BOT] ✅ Dispatcher completed for session: ${parsed.sessionId}`);
281
295
  log(`xy: dispatch complete (session=${parsed.sessionId})`);
@@ -329,25 +343,7 @@ function buildXYMediaPayload(mediaList) {
329
343
  return {
330
344
  MediaPath: first?.path,
331
345
  MediaType: first?.mimeType,
332
- MediaUrl: first?.path,
333
346
  MediaPaths: mediaPaths.length > 0 ? mediaPaths : undefined,
334
- MediaUrls: mediaPaths.length > 0 ? mediaPaths : undefined,
335
347
  MediaTypes: mediaTypes.length > 0 ? mediaTypes : undefined,
336
348
  };
337
349
  }
338
- /**
339
- * Infer OpenClaw media type from file type string.
340
- */
341
- function inferMediaType(fileType) {
342
- const lower = fileType.toLowerCase();
343
- if (lower.includes("image") || /\.(jpg|jpeg|png|gif|bmp|webp)$/i.test(lower)) {
344
- return "image";
345
- }
346
- if (lower.includes("video") || /\.(mp4|avi|mov|mkv|webm)$/i.test(lower)) {
347
- return "video";
348
- }
349
- if (lower.includes("audio") || /\.(mp3|wav|ogg|m4a)$/i.test(lower)) {
350
- return "audio";
351
- }
352
- return "file";
353
- }
@@ -1,7 +1,6 @@
1
1
  import { resolveXYConfig, listXYAccountIds, getDefaultXYAccountId } from "./config.js";
2
2
  import { xyConfigSchema } from "./config-schema.js";
3
3
  import { xyOutbound } from "./outbound.js";
4
- import { xyOnboardingAdapter } from "./onboarding.js";
5
4
  import { locationTool } from "./tools/location-tool.js";
6
5
  import { noteTool } from "./tools/note-tool.js";
7
6
  import { searchNoteTool } from "./tools/search-note-tool.js";
@@ -22,8 +21,13 @@ import { searchAlarmTool } from "./tools/search-alarm-tool.js";
22
21
  import { modifyAlarmTool } from "./tools/modify-alarm-tool.js";
23
22
  import { deleteAlarmTool } from "./tools/delete-alarm-tool.js";
24
23
  import { sendFileToUserTool } from "./tools/send-file-to-user-tool.js";
25
- import { xiaoyiCollectionTool } from "./tools/xiaoyi-collection-tool.js";
26
24
  import { viewPushResultTool } from "./tools/view-push-result-tool.js";
25
+ import { imageReadingTool } from "./tools/image-reading-tool.js";
26
+ import { timestampToUtc8Tool } from "./tools/timestamp-to-utc8-tool.js";
27
+ import { sendCommandToCarTool } from "./tools/send-command-to-car-tool.js";
28
+ import { filterToolsByDevice } from "./tools/device-tool-map.js";
29
+ import { getCurrentSessionContext } from "./tools/session-manager.js";
30
+ import { logger } from "./utils/logger.js";
27
31
  /**
28
32
  * Xiaoyi Channel Plugin for OpenClaw.
29
33
  * Implements Xiaoyi A2A protocol with dual WebSocket connections.
@@ -62,8 +66,13 @@ export const xyPlugin = {
62
66
  schema: xyConfigSchema,
63
67
  },
64
68
  outbound: xyOutbound,
65
- onboarding: xyOnboardingAdapter,
66
- agentTools: [locationTool, noteTool, searchNoteTool, modifyNoteTool, calendarTool, searchCalendarTool, searchContactTool, searchPhotoGalleryTool, uploadPhotoTool, xiaoyiGuiTool, callPhoneTool, searchMessageTool, sendMessageTool, searchFileTool, uploadFileTool, createAlarmTool, searchAlarmTool, modifyAlarmTool, deleteAlarmTool, sendFileToUserTool, xiaoyiCollectionTool, viewPushResultTool],
69
+ agentTools: () => {
70
+ const allTools = [locationTool, noteTool, searchNoteTool, modifyNoteTool, calendarTool, searchCalendarTool, searchContactTool, searchPhotoGalleryTool, uploadPhotoTool, xiaoyiGuiTool, callPhoneTool, searchMessageTool, sendMessageTool, searchFileTool, uploadFileTool, createAlarmTool, searchAlarmTool, modifyAlarmTool, deleteAlarmTool, sendFileToUserTool, viewPushResultTool, imageReadingTool, timestampToUtc8Tool, sendCommandToCarTool];
71
+ const ctx = getCurrentSessionContext();
72
+ const filtered = filterToolsByDevice(allTools, ctx?.deviceType);
73
+ logger.log(`[DEVICE-FILTER] deviceType=${ctx?.deviceType ?? "(none)"}, tools: ${allTools.length} → ${filtered.length} (${filtered.map(t => t.name).join(", ")})`);
74
+ return filtered;
75
+ },
67
76
  messaging: {
68
77
  normalizeTarget: (raw) => {
69
78
  const trimmed = raw.trim();
@@ -80,6 +89,22 @@ export const xyPlugin = {
80
89
  hint: "<sessionId>",
81
90
  },
82
91
  },
92
+ bindings: {
93
+ compileConfiguredBinding: ({ conversationId }) => {
94
+ const sessionId = conversationId.trim();
95
+ if (!sessionId)
96
+ return null;
97
+ return {
98
+ conversationId: sessionId,
99
+ parentConversationId: undefined,
100
+ };
101
+ },
102
+ matchInboundConversation: ({ compiledBinding, conversationId }) => {
103
+ return compiledBinding.conversationId === conversationId
104
+ ? { conversationId, matchPriority: 2 }
105
+ : null;
106
+ },
107
+ },
83
108
  reload: {
84
109
  configPrefixes: ["channels.xiaoyi-channel"],
85
110
  },
@@ -23,7 +23,6 @@ export function getXYWebSocketManager(config) {
23
23
  let cached = wsManagerCache.get(cacheKey);
24
24
  if (cached && cached.isConfigMatch(config)) {
25
25
  const log = runtime?.log ?? console.log;
26
- log(`[WS-MANAGER-CACHE] ✅ Reusing cached WebSocket manager: ${cacheKey}, total managers: ${wsManagerCache.size}`);
27
26
  return cached;
28
27
  }
29
28
  // Create new manager
@@ -73,21 +72,14 @@ export function getCachedManagerCount() {
73
72
  * Helps identify connection issues and orphan connections.
74
73
  */
75
74
  export function diagnoseAllManagers() {
76
- console.log("========================================");
77
- console.log("📊 WebSocket Manager Global Diagnostics");
78
- console.log("========================================");
79
75
  console.log(`Total cached managers: ${wsManagerCache.size}`);
80
- console.log("");
81
76
  if (wsManagerCache.size === 0) {
82
77
  console.log("ℹ️ No managers in cache");
83
- console.log("========================================");
84
78
  return;
85
79
  }
86
80
  let orphanCount = 0;
87
81
  wsManagerCache.forEach((manager, key) => {
88
82
  const diag = manager.getConnectionDiagnostics();
89
- console.log(`📌 Manager: ${key}`);
90
- console.log(` Shutting down: ${diag.isShuttingDown}`);
91
83
  console.log(` Total event listeners on manager: ${diag.totalEventListeners}`);
92
84
  // Connection
93
85
  console.log(` 🔌 Connection:`);
@@ -111,7 +103,6 @@ export function diagnoseAllManagers() {
111
103
  else {
112
104
  console.log(`✅ No orphan connections found`);
113
105
  }
114
- console.log("========================================");
115
106
  }
116
107
  /**
117
108
  * Clean up orphan connections across all managers.
@@ -0,0 +1,3 @@
1
+ import type { ClawdbotConfig } from "openclaw/plugin-sdk";
2
+ import type { ApiResponse } from "./constants.js";
3
+ export declare function callCsplApi(questionText: string, cfg: ClawdbotConfig): Promise<ApiResponse>;
@@ -0,0 +1,86 @@
1
+ // CSPL API 请求模块
2
+ import https from "node:https";
3
+ import { URL } from "node:url";
4
+ import { randomBytes } from "node:crypto";
5
+ import { getCsplConfig } from "./config.js";
6
+ import { DEFAULT_HTTP_PORT, HTTP_STATUS_BAD_REQUEST } from "./constants.js";
7
+ function generateTraceId() {
8
+ return randomBytes(16).toString("hex");
9
+ }
10
+ function buildHeaders(config) {
11
+ return {
12
+ "x-hag-trace-id": generateTraceId(),
13
+ "x-uid": config.uid,
14
+ "x-api-key": config.apiKey,
15
+ "x-request-from": config.requestFrom,
16
+ "x-skill-id": config.skillId,
17
+ "content-type": "application/json",
18
+ };
19
+ }
20
+ function buildRequestOptions(url, headers, timeout) {
21
+ const urlObj = new URL(url);
22
+ return {
23
+ hostname: urlObj.hostname,
24
+ port: urlObj.port || DEFAULT_HTTP_PORT,
25
+ path: urlObj.pathname,
26
+ method: "POST",
27
+ headers: headers,
28
+ timeout,
29
+ };
30
+ }
31
+ function parseResponse(data) {
32
+ if (!data?.trim())
33
+ throw new Error("[CSPL] API response is empty");
34
+ const json = JSON.parse(data);
35
+ if (json.retCode && json.retCode !== "0") {
36
+ throw new Error(`[CSPL] API error: ${json.retMsg || "unknown"}`);
37
+ }
38
+ if (!json.retCode && json.code) {
39
+ throw new Error(`[CSPL] Backend error: ${json.desc || "unknown"}`);
40
+ }
41
+ return json;
42
+ }
43
+ export async function callCsplApi(questionText, cfg) {
44
+ const config = getCsplConfig(cfg);
45
+ const headers = buildHeaders(config);
46
+ const payload = {
47
+ questionText,
48
+ textSource: config.textSource,
49
+ action: config.action,
50
+ };
51
+ return new Promise((resolve, reject) => {
52
+ const options = buildRequestOptions(config.api.url, headers, config.api.timeout);
53
+ const req = https.request(options, (res) => {
54
+ if (res.statusCode && res.statusCode >= HTTP_STATUS_BAD_REQUEST) {
55
+ reject(new Error(`[CSPL] HTTP error: ${res.statusCode}`));
56
+ return;
57
+ }
58
+ let data = "";
59
+ res.on("data", (chunk) => {
60
+ data += chunk;
61
+ });
62
+ res.on("end", () => {
63
+ try {
64
+ const result = parseResponse(data);
65
+ console.log(`[CSPL API] ✅ 请求成功`);
66
+ resolve(result);
67
+ }
68
+ catch (e) {
69
+ console.error(`[CSPL API] ❌ 请求失败: ${e instanceof Error ? e.message : String(e)}`);
70
+ reject(e);
71
+ }
72
+ });
73
+ });
74
+ req.on("error", (error) => {
75
+ console.error(`[CSPL API] ❌ 请求错误: ${error instanceof Error ? error.message : String(error)}`);
76
+ reject(error);
77
+ });
78
+ req.on("timeout", () => {
79
+ console.error(`[CSPL API] ⏰ 请求超时 (${config.api.timeout}ms)`);
80
+ req.destroy();
81
+ reject(new Error("[CSPL] Request timeout"));
82
+ });
83
+ req.write(JSON.stringify(payload));
84
+ req.end();
85
+ });
86
+ }
@@ -0,0 +1,19 @@
1
+ import type { ClawdbotConfig } from "openclaw/plugin-sdk";
2
+ export interface ApiConfig {
3
+ url: string;
4
+ timeout: number;
5
+ }
6
+ export interface CsplConfig {
7
+ api: ApiConfig;
8
+ uid: string;
9
+ apiKey: string;
10
+ skillId: string;
11
+ requestFrom: string;
12
+ textSource: string;
13
+ action: string;
14
+ }
15
+ /**
16
+ * 构建 CSPL 配置。uid 和 apiKey 复用 XYChannelConfig,避免重复配置。
17
+ * serviceUrl 从 .xiaoyienv 文件读取,skillId 写死在常量中。
18
+ */
19
+ export declare function getCsplConfig(cfg: ClawdbotConfig): CsplConfig;
@@ -0,0 +1,50 @@
1
+ // CSPL Hook 配置管理
2
+ // uid 和 apiKey 复用 XYChannelConfig,skillId 写死在常量中
3
+ import { resolveXYConfig } from "../config.js";
4
+ import { CSPL_STATIC_CONFIG, API_URL_SUFFIX, ENV_FILE_PATH } from "./constants.js";
5
+ import fs from "node:fs";
6
+ import { logger } from "../utils/logger.js";
7
+ let cachedConfig = null;
8
+ function readServiceUrl() {
9
+ if (!fs.existsSync(ENV_FILE_PATH)) {
10
+ throw new Error(`[CSPL] Environment file not found: ${ENV_FILE_PATH}`);
11
+ }
12
+ const envData = fs.readFileSync(ENV_FILE_PATH, "utf-8");
13
+ for (const line of envData.split("\n")) {
14
+ const trimmed = line.trim();
15
+ if (!trimmed || trimmed.startsWith("#"))
16
+ continue;
17
+ const eqIdx = trimmed.indexOf("=");
18
+ if (eqIdx === -1)
19
+ continue;
20
+ const key = trimmed.substring(0, eqIdx).trim();
21
+ const value = trimmed.substring(eqIdx + 1).trim();
22
+ if (key === "SERVICE_URL" && value)
23
+ return value;
24
+ }
25
+ throw new Error("[CSPL] Missing SERVICE_URL in env file");
26
+ }
27
+ /**
28
+ * 构建 CSPL 配置。uid 和 apiKey 复用 XYChannelConfig,避免重复配置。
29
+ * serviceUrl 从 .xiaoyienv 文件读取,skillId 写死在常量中。
30
+ */
31
+ export function getCsplConfig(cfg) {
32
+ if (cachedConfig)
33
+ return cachedConfig;
34
+ const xyConfig = resolveXYConfig(cfg);
35
+ const serviceUrl = readServiceUrl();
36
+ cachedConfig = {
37
+ api: {
38
+ url: `${serviceUrl}${API_URL_SUFFIX}`,
39
+ timeout: CSPL_STATIC_CONFIG.api.timeout,
40
+ },
41
+ uid: xyConfig.uid,
42
+ apiKey: xyConfig.apiKey,
43
+ skillId: CSPL_STATIC_CONFIG.skillId,
44
+ requestFrom: CSPL_STATIC_CONFIG.requestFrom,
45
+ textSource: CSPL_STATIC_CONFIG.textSource,
46
+ action: CSPL_STATIC_CONFIG.action,
47
+ };
48
+ logger.log("[CSPL] Config loaded (uid/apiKey from XYChannelConfig)");
49
+ return cachedConfig;
50
+ }
@@ -0,0 +1,43 @@
1
+ export interface HttpHeaders {
2
+ "x-hag-trace-id": string;
3
+ "x-uid": string;
4
+ "x-api-key": string;
5
+ "x-request-from": string;
6
+ "x-skill-id": string;
7
+ "content-type": string;
8
+ }
9
+ export interface ApiPayload {
10
+ questionText: string;
11
+ textSource: string;
12
+ action: string;
13
+ }
14
+ export interface ApiResponse {
15
+ data?: {
16
+ securityResult?: string;
17
+ };
18
+ retCode?: string;
19
+ retMsg?: string;
20
+ code?: string;
21
+ desc?: string;
22
+ [key: string]: any;
23
+ }
24
+ export declare const MIN_TEXT_LENGTH = 0;
25
+ export declare const MAX_TEXT_LENGTH = 4096;
26
+ export declare const MAX_TOTAL_LENGTH = 40960;
27
+ export declare const regex: RegExp;
28
+ export declare const DEFAULT_HTTP_PORT = 443;
29
+ export declare const HTTP_STATUS_BAD_REQUEST = 400;
30
+ export declare const ENV_FILE_PATH = "/home/sandbox/.openclaw/.xiaoyienv";
31
+ export declare const API_URL_SUFFIX = "/celia-claw/v1/rest-api/skill/execute";
32
+ export declare const REQUIRED_ENV_VARS: string[];
33
+ export declare const ALLOWED_TOOLS: string[];
34
+ export declare const CSPL_STATIC_CONFIG: {
35
+ readonly api: {
36
+ readonly timeout: 5000;
37
+ };
38
+ readonly skillId: "skill-scope";
39
+ readonly requestFrom: "openclaw";
40
+ readonly textSource: "question";
41
+ readonly action: "TOOL_OUTPUT_SCAN";
42
+ };
43
+ export declare const STEER_ABORT_MESSAGE = "\u5F53\u524D\u884C\u4E3A\u5B58\u5728\u5B89\u5168\u9690\u60A3\uFF0C\u7EC8\u6B62\u6240\u6709\u64CD\u4F5C\uFF0C\u5E76\u4E14\u5728\u6700\u7EC8\u56DE\u590D\u4E2D\u8BF4\u660E\uFF0C\u8BC6\u522B\u5230\u5F53\u524D\u6D41\u7A0B\u4E2D\u5B58\u5728\u6F5C\u5728\u5B89\u5168\u98CE\u9669\uFF0C\u5DF2\u5E2E\u60A8\u4E2D\u6B62\u5F53\u524D\u6D41\u7A0B\uFF0C\u8BF7\u6E05\u7A7A\u4E0A\u4E0B\u6587\u540E\u91CD\u8BD5";
@@ -0,0 +1,22 @@
1
+ // CSPL Hook 常量与类型定义
2
+ export const MIN_TEXT_LENGTH = 0;
3
+ export const MAX_TEXT_LENGTH = 4096;
4
+ export const MAX_TOTAL_LENGTH = 40960;
5
+ export const regex = /[^\u4e00-\u9fa5a-zA-Z0-9\s\.,!?;:,。!?;:""\'\'()()\[\]【】]/;
6
+ export const DEFAULT_HTTP_PORT = 443;
7
+ export const HTTP_STATUS_BAD_REQUEST = 400;
8
+ export const ENV_FILE_PATH = "/home/sandbox/.openclaw/.xiaoyienv";
9
+ export const API_URL_SUFFIX = "/celia-claw/v1/rest-api/skill/execute";
10
+ export const REQUIRED_ENV_VARS = ["PERSONAL-API-KEY", "PERSONAL-UID", "SERVICE_URL"];
11
+ // 工具白名单
12
+ export const ALLOWED_TOOLS = ["web_fetch", "read", "pdf"];
13
+ // 静态配置(非敏感,敏感值从 ENV 文件读取)
14
+ export const CSPL_STATIC_CONFIG = {
15
+ api: { timeout: 5000 },
16
+ skillId: "skill-scope",
17
+ requestFrom: "openclaw",
18
+ textSource: "question",
19
+ action: "TOOL_OUTPUT_SCAN",
20
+ };
21
+ // Steer 注入常量
22
+ export const STEER_ABORT_MESSAGE = "当前行为存在安全隐患,终止所有操作,并且在最终回复中说明,识别到当前流程中存在潜在安全风险,已帮您中止当前流程,请清空上下文后重试";
@@ -0,0 +1,10 @@
1
+ export declare function filterText(text: string): string;
2
+ export declare function validateAndTruncateText(text: string, maxLength: number): {
3
+ text: string;
4
+ truncated: boolean;
5
+ };
6
+ export declare function extractResultText(event: any, toolName: string): string;
7
+ export declare function processText(resultText: string): string;
8
+ export declare function parseSecurityResult(response: any): {
9
+ status: "ACCEPT" | "REJECT";
10
+ };