@ynhcj/xiaoyi-channel 0.0.101-beta → 0.0.101-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 (133) hide show
  1. package/dist/index.d.ts +4 -10
  2. package/dist/index.js +75 -95
  3. package/dist/provider-discovery.d.ts +2 -0
  4. package/dist/provider-discovery.js +4 -0
  5. package/dist/src/bot.js +30 -4
  6. package/dist/src/channel.js +2 -21
  7. package/dist/src/client.js +31 -22
  8. package/dist/src/cspl/call-api.js +6 -5
  9. package/dist/src/file-download.js +4 -3
  10. package/dist/src/file-upload.js +19 -18
  11. package/dist/src/formatter.d.ts +2 -0
  12. package/dist/src/formatter.js +25 -35
  13. package/dist/src/heartbeat.js +1 -1
  14. package/dist/src/message-queue.d.ts +17 -0
  15. package/dist/src/message-queue.js +51 -0
  16. package/dist/src/monitor.js +63 -14
  17. package/dist/src/outbound.js +19 -18
  18. package/dist/src/provider.d.ts +2 -1
  19. package/dist/src/provider.js +190 -64
  20. package/dist/src/push.js +16 -15
  21. package/dist/src/reply-dispatcher.js +12 -3
  22. package/dist/src/runtime.d.ts +3 -11
  23. package/dist/src/runtime.js +6 -18
  24. package/dist/src/self-evolution-handler.d.ts +6 -0
  25. package/dist/src/self-evolution-handler.js +100 -7
  26. package/dist/src/self-evolution-keyword.d.ts +9 -0
  27. package/dist/src/self-evolution-keyword.js +147 -0
  28. package/dist/src/self-evolution-tool-result-nudge.d.ts +3 -0
  29. package/dist/src/self-evolution-tool-result-nudge.js +96 -0
  30. package/dist/src/skill-retriever/hooks.js +8 -12
  31. package/dist/src/skill-retriever/tool-search.js +22 -8
  32. package/dist/src/skill-retriever/types.d.ts +2 -0
  33. package/dist/src/steer-injector.js +1 -1
  34. package/dist/src/task-manager.d.ts +4 -0
  35. package/dist/src/task-manager.js +12 -1
  36. package/dist/src/tools/calendar-tool.d.ts +2 -1
  37. package/dist/src/tools/calendar-tool.js +112 -116
  38. package/dist/src/tools/call-device-tool.d.ts +2 -1
  39. package/dist/src/tools/call-device-tool.js +126 -103
  40. package/dist/src/tools/call-phone-tool.d.ts +2 -1
  41. package/dist/src/tools/call-phone-tool.js +109 -113
  42. package/dist/src/tools/create-alarm-tool.d.ts +2 -1
  43. package/dist/src/tools/create-alarm-tool.js +227 -231
  44. package/dist/src/tools/create-all-tools.d.ts +16 -0
  45. package/dist/src/tools/create-all-tools.js +50 -0
  46. package/dist/src/tools/delete-alarm-tool.d.ts +2 -1
  47. package/dist/src/tools/delete-alarm-tool.js +131 -135
  48. package/dist/src/tools/get-alarm-tool-schema.d.ts +2 -1
  49. package/dist/src/tools/get-alarm-tool-schema.js +16 -10
  50. package/dist/src/tools/get-calendar-tool-schema.d.ts +2 -1
  51. package/dist/src/tools/get-calendar-tool-schema.js +12 -8
  52. package/dist/src/tools/get-collection-tool-schema.d.ts +2 -1
  53. package/dist/src/tools/get-collection-tool-schema.js +11 -9
  54. package/dist/src/tools/get-contact-tool-schema.d.ts +2 -1
  55. package/dist/src/tools/get-contact-tool-schema.js +16 -10
  56. package/dist/src/tools/get-device-file-tool-schema.d.ts +2 -1
  57. package/dist/src/tools/get-device-file-tool-schema.js +13 -9
  58. package/dist/src/tools/get-email-tool-schema.d.ts +2 -1
  59. package/dist/src/tools/get-email-tool-schema.js +11 -8
  60. package/dist/src/tools/get-note-tool-schema.d.ts +2 -1
  61. package/dist/src/tools/get-note-tool-schema.js +14 -9
  62. package/dist/src/tools/get-photo-tool-schema.d.ts +2 -1
  63. package/dist/src/tools/get-photo-tool-schema.js +12 -9
  64. package/dist/src/tools/image-reading-tool.d.ts +2 -1
  65. package/dist/src/tools/image-reading-tool.js +86 -90
  66. package/dist/src/tools/location-tool.d.ts +2 -1
  67. package/dist/src/tools/location-tool.js +87 -91
  68. package/dist/src/tools/login-token-tool.d.ts +3 -2
  69. package/dist/src/tools/login-token-tool.js +114 -117
  70. package/dist/src/tools/modify-alarm-tool.d.ts +2 -1
  71. package/dist/src/tools/modify-alarm-tool.js +232 -236
  72. package/dist/src/tools/modify-note-tool.d.ts +2 -1
  73. package/dist/src/tools/modify-note-tool.js +104 -108
  74. package/dist/src/tools/note-tool.d.ts +2 -1
  75. package/dist/src/tools/note-tool.js +103 -107
  76. package/dist/src/tools/query-app-message-tool.d.ts +2 -1
  77. package/dist/src/tools/query-app-message-tool.js +108 -111
  78. package/dist/src/tools/query-memory-data-tool.d.ts +2 -1
  79. package/dist/src/tools/query-memory-data-tool.js +109 -112
  80. package/dist/src/tools/query-todo-task-tool.d.ts +2 -1
  81. package/dist/src/tools/query-todo-task-tool.js +103 -106
  82. package/dist/src/tools/save-file-to-phone-tool.d.ts +2 -1
  83. package/dist/src/tools/save-file-to-phone-tool.js +127 -131
  84. package/dist/src/tools/save-media-to-gallery-tool.d.ts +2 -1
  85. package/dist/src/tools/save-media-to-gallery-tool.js +134 -138
  86. package/dist/src/tools/save-self-evolution-skill-tool.d.ts +2 -1
  87. package/dist/src/tools/save-self-evolution-skill-tool.js +346 -125
  88. package/dist/src/tools/search-alarm-tool.d.ts +2 -1
  89. package/dist/src/tools/search-alarm-tool.js +171 -175
  90. package/dist/src/tools/search-calendar-tool.d.ts +2 -1
  91. package/dist/src/tools/search-calendar-tool.js +145 -149
  92. package/dist/src/tools/search-contact-tool.d.ts +2 -1
  93. package/dist/src/tools/search-contact-tool.js +98 -102
  94. package/dist/src/tools/search-email-tool.d.ts +2 -1
  95. package/dist/src/tools/search-email-tool.js +107 -111
  96. package/dist/src/tools/search-file-tool.d.ts +2 -1
  97. package/dist/src/tools/search-file-tool.js +99 -103
  98. package/dist/src/tools/search-message-tool.d.ts +2 -1
  99. package/dist/src/tools/search-message-tool.js +100 -104
  100. package/dist/src/tools/search-note-tool.d.ts +2 -1
  101. package/dist/src/tools/search-note-tool.js +95 -99
  102. package/dist/src/tools/search-photo-gallery-tool.d.ts +2 -1
  103. package/dist/src/tools/search-photo-gallery-tool.js +34 -38
  104. package/dist/src/tools/send-email-tool.d.ts +2 -1
  105. package/dist/src/tools/send-email-tool.js +105 -108
  106. package/dist/src/tools/send-file-to-user-tool.d.ts +2 -1
  107. package/dist/src/tools/send-file-to-user-tool.js +158 -155
  108. package/dist/src/tools/send-message-tool.d.ts +2 -1
  109. package/dist/src/tools/send-message-tool.js +119 -123
  110. package/dist/src/tools/session-manager.d.ts +21 -6
  111. package/dist/src/tools/session-manager.js +145 -18
  112. package/dist/src/tools/upload-file-tool.d.ts +2 -1
  113. package/dist/src/tools/upload-file-tool.js +78 -82
  114. package/dist/src/tools/upload-photo-tool.d.ts +2 -1
  115. package/dist/src/tools/upload-photo-tool.js +69 -73
  116. package/dist/src/tools/xiaoyi-add-collection-tool.d.ts +2 -1
  117. package/dist/src/tools/xiaoyi-add-collection-tool.js +143 -147
  118. package/dist/src/tools/xiaoyi-collection-tool.d.ts +2 -1
  119. package/dist/src/tools/xiaoyi-collection-tool.js +111 -115
  120. package/dist/src/tools/xiaoyi-delete-collection-tool.d.ts +2 -1
  121. package/dist/src/tools/xiaoyi-delete-collection-tool.js +124 -128
  122. package/dist/src/tools/xiaoyi-gui-tool.d.ts +2 -1
  123. package/dist/src/tools/xiaoyi-gui-tool.js +87 -88
  124. package/dist/src/utils/logger.js +20 -18
  125. package/dist/src/utils/runtime-manager.js +24 -2
  126. package/dist/src/utils/self-evolution-manager.d.ts +5 -0
  127. package/dist/src/utils/self-evolution-manager.js +46 -24
  128. package/dist/src/utils/tool-call-nudge-manager.d.ts +3 -1
  129. package/dist/src/utils/tool-call-nudge-manager.js +14 -2
  130. package/dist/src/websocket.d.ts +3 -0
  131. package/dist/src/websocket.js +81 -28
  132. package/openclaw.plugin.json +22 -0
  133. package/package.json +3 -3
package/dist/index.d.ts CHANGED
@@ -1,14 +1,8 @@
1
- import type { OpenClawPluginApi } from "openclaw/plugin-sdk";
2
- /**
3
- * Xiaoyi Channel Plugin Entry Point.
4
- * Exports the plugin for OpenClaw to load.
5
- * Located at root level following feishu pattern for proper plugin registration.
6
- */
7
- declare const plugin: {
1
+ declare const _default: {
8
2
  id: string;
9
3
  name: string;
10
4
  description: string;
11
5
  configSchema: import("openclaw/plugin-sdk").OpenClawPluginConfigSchema;
12
- register(api: OpenClawPluginApi): void;
13
- };
14
- export default plugin;
6
+ register: NonNullable<import("openclaw/plugin-sdk/core").OpenClawPluginDefinition["register"]>;
7
+ } & Pick<import("openclaw/plugin-sdk/core").OpenClawPluginDefinition, "kind" | "reload" | "nodeHostCommands" | "securityAuditCollectors">;
8
+ export default _default;
package/dist/index.js CHANGED
@@ -1,107 +1,87 @@
1
- import { emptyPluginConfigSchema } from "openclaw/plugin-sdk";
2
- import { xyPlugin } from "./src/channel.js";
1
+ import { definePluginEntry } from "openclaw/plugin-sdk/core";
3
2
  import { xiaoyiProvider } from "./src/provider.js";
3
+ import { xyPlugin } from "./src/channel.js";
4
+ import { callCsplApi } from "./src/cspl/call-api.js";
5
+ import { ALLOWED_TOOLS, MAX_TEXT_LENGTH, MAX_TOTAL_LENGTH, MIN_TEXT_LENGTH, STEER_ABORT_MESSAGE, } from "./src/cspl/constants.js";
6
+ import { extractResultText, parseSecurityResult, processText, validateAndTruncateText, } from "./src/cspl/utils.js";
4
7
  import { setXYRuntime } from "./src/runtime.js";
5
8
  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 { selfEvolutionManager } from "./src/utils/self-evolution-manager.js";
9
- import { TOOL_CALL_NUDGE_THRESHOLD, toolCallNudgeManager } from "./src/utils/tool-call-nudge-manager.js";
10
- import { ALLOWED_TOOLS, MIN_TEXT_LENGTH, MAX_TOTAL_LENGTH, MAX_TEXT_LENGTH, STEER_ABORT_MESSAGE, } from "./src/cspl/constants.js";
9
+ import { registerSelfEvolutionToolResultNudge } from "./src/self-evolution-tool-result-nudge.js";
11
10
  import { createBeforePromptBuildHandler } from "./src/skill-retriever/hooks.js";
12
11
  import { normalizeToolRetrieverConfig } from "./src/skill-retriever/config.js";
13
- const SELF_EVOLUTION_NUDGE_MESSAGE = `[系统消息,非用户发送] 当前任务已经调用了较多工具。如果你识别到存在稳定、可复用的踩坑经验或排错模式,可以在完成当前主要任务后,正式回复用户前,阅读evolution skill开启自进化。注意,不要打断当前主任务,已完成任务为最高优先级。`;
14
- function shouldCountToolCall(toolName) {
15
- if (toolName === "save_self_evolution_skill") {
16
- return false;
17
- }
18
- if (toolName === "call_device_tool") {
19
- return false;
20
- }
21
- if (toolName.endsWith("_tool_schema")) {
22
- return false;
23
- }
24
- return true;
12
+ function registerFullHooks(api) {
13
+ // SKILL RETRIEVER HOOK: before_prompt_build hook
14
+ const pluginConfig = api.pluginConfig || {};
15
+ const skillRetrieverConfig = normalizeToolRetrieverConfig({
16
+ enabled: pluginConfig.skillRetrieverEnabled ?? true,
17
+ maxTools: pluginConfig.skillRetrieverMaxTools ?? 2,
18
+ includeUninstalledOnly: true,
19
+ envFilePath: "~/.openclaw/.xiaoyienv",
20
+ timeoutMs: pluginConfig.skillRetrieverTimeoutMs ?? 1000,
21
+ });
22
+ const beforePromptBuildHandler = createBeforePromptBuildHandler(skillRetrieverConfig);
23
+ api.on("before_prompt_build", beforePromptBuildHandler);
24
+ registerSelfEvolutionToolResultNudge(api);
25
+ api.on("after_tool_call", async (event, ctx) => {
26
+ if (!ALLOWED_TOOLS.includes(event.toolName)) {
27
+ return;
28
+ }
29
+ console.log(`[SENTINEL HOOK] 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
+ const questionText = {
37
+ subSceneID: "TOOL_OUTPUT",
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(`[SENTINEL HOOK] 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(`[SENTINEL HOOK] after_tool_call error: ${err}`);
59
+ }
60
+ });
25
61
  }
26
- /**
27
- * Xiaoyi Channel Plugin Entry Point.
28
- * Exports the plugin for OpenClaw to load.
29
- * Located at root level following feishu pattern for proper plugin registration.
30
- */
31
- const plugin = {
62
+ export default definePluginEntry({
32
63
  id: "xiaoyi-channel",
33
64
  name: "Xiaoyi Channel",
34
65
  description: "Xiaoyi channel plugin - Xiaoyi A2A protocol integration",
35
- configSchema: emptyPluginConfigSchema(),
36
66
  register(api) {
37
- setXYRuntime(api.runtime);
38
- api.registerChannel({ plugin: xyPlugin });
67
+ // Always register the provider so wrapStreamFn/prepareExtraParams work
68
+ // in ALL registration modes (not just "full").
39
69
  api.registerProvider(xiaoyiProvider);
40
- // SENTINEL HOOK after_tool_call hook: 监听工具结果,发送至安全检测 API 进行安全检测
41
- // 如果响应为 REJECT,注入 steer 消息中止当前对话
42
- api.on("after_tool_call", async (event, ctx) => {
43
- if (ctx.sessionKey &&
44
- await selfEvolutionManager.isEnabled() &&
45
- shouldCountToolCall(event.toolName)) {
46
- try {
47
- const { count, shouldNudge } = toolCallNudgeManager.recordToolCall(ctx.sessionKey);
48
- api.logger.debug?.(`[SELF_EVOLUTION] Tool call counted: tool=${event.toolName}, count=${count}, threshold=${TOOL_CALL_NUDGE_THRESHOLD}, sessionKey=${ctx.sessionKey}`);
49
- if (shouldNudge) {
50
- api.logger.info?.(`[SELF_EVOLUTION] Tool call threshold reached, injecting nudge: count=${count}, sessionKey=${ctx.sessionKey}`);
51
- await tryInjectSteer(ctx.sessionKey, SELF_EVOLUTION_NUDGE_MESSAGE);
52
- }
53
- }
54
- catch (err) {
55
- api.logger.error(`[SELF_EVOLUTION] after_tool_call nudge error: ${err}`);
56
- }
57
- }
58
- if (!ALLOWED_TOOLS.includes(event.toolName)) {
59
- return;
60
- }
61
- console.log(`[SENTINEL HOOK] after_tool_call triggered: toolName=${event.toolName}, sessionKey=${ctx.sessionKey ?? "none"}`);
62
- try {
63
- const resultText = extractResultText(event, event.toolName);
64
- const resultLength = resultText.length;
65
- if (resultLength <= MIN_TEXT_LENGTH || resultLength > MAX_TOTAL_LENGTH) {
66
- return;
67
- }
68
- // 构造 sentinel_hook 格式的 payload: { tool, output: [{ content }] }
69
- const questionText = {
70
- subSceneID: 'TOOL_OUTPUT',
71
- tool: event.toolName,
72
- output: [{ content: "" }],
73
- };
74
- const originText = processText(resultText);
75
- questionText.output[0].content = originText;
76
- let finalJson = JSON.stringify(questionText);
77
- if (finalJson.length > MAX_TEXT_LENGTH) {
78
- const diff = finalJson.length - MAX_TEXT_LENGTH;
79
- const { text: trimmed } = validateAndTruncateText(originText, MAX_TEXT_LENGTH - diff);
80
- questionText.output[0].content = trimmed;
81
- finalJson = JSON.stringify(questionText);
82
- }
83
- const response = await callCsplApi(finalJson, api.config);
84
- const result = parseSecurityResult(response);
85
- console.log(`[SENTINEL HOOK] Security result: status=${result.status}`);
86
- if (result.status === "REJECT") {
87
- await tryInjectSteer(ctx.sessionKey, STEER_ABORT_MESSAGE);
88
- }
89
- }
90
- catch (err) {
91
- api.logger.error(`[SENTINEL HOOK] after_tool_call error: ${err}`);
92
- }
93
- });
94
- // SKILL RETRIEVER HOOK: before_prompt_build hook
95
- const pluginConfig = api.pluginConfig || {};
96
- const skillRetrieverConfig = normalizeToolRetrieverConfig({
97
- enabled: pluginConfig.skillRetrieverEnabled ?? true,
98
- maxTools: pluginConfig.skillRetrieverMaxTools ?? 2,
99
- includeUninstalledOnly: true,
100
- envFilePath: "~/.openclaw/.xiaoyienv",
101
- timeoutMs: pluginConfig.skillRetrieverTimeoutMs ?? 1000,
102
- });
103
- const beforePromptBuildHandler = createBeforePromptBuildHandler(skillRetrieverConfig);
104
- api.on("before_prompt_build", beforePromptBuildHandler);
70
+ if (api.registrationMode === "cli-metadata") {
71
+ return;
72
+ }
73
+ if (api.registrationMode === "tool-discovery") {
74
+ registerFullHooks(api);
75
+ return;
76
+ }
77
+ // Register channel plugin and set runtime
78
+ api.registerChannel({ plugin: xyPlugin });
79
+ setXYRuntime(api.runtime);
80
+ if (api.registrationMode === "discovery") {
81
+ return;
82
+ }
83
+ if (api.registrationMode === "full") {
84
+ registerFullHooks(api);
85
+ }
105
86
  },
106
- };
107
- export default plugin;
87
+ });
@@ -0,0 +1,2 @@
1
+ import { xiaoyiProvider } from "./src/provider.js";
2
+ export default xiaoyiProvider;
@@ -0,0 +1,4 @@
1
+ // Provider discovery entry for fast-path provider resolution.
2
+ // Exported as default so normalizeDiscoveryModule can unwrap it via .default.
3
+ import { xiaoyiProvider } from "./src/provider.js";
4
+ export default xiaoyiProvider;
package/dist/src/bot.js CHANGED
@@ -5,11 +5,14 @@ import { parseA2AMessage, extractTextFromParts, extractFileParts, extractPushId,
5
5
  import { downloadFilesFromParts } from "./file-download.js";
6
6
  import { resolveXYConfig } from "./config.js";
7
7
  import { sendStatusUpdate, sendClearContextResponse, sendTasksCancelResponse, sendA2AResponse } from "./formatter.js";
8
+ import { appendSelfEvolutionKeywordNudge, shouldNudgeForSelfEvolutionKeyword, } from "./self-evolution-keyword.js";
8
9
  import { registerSession, unregisterSession, runWithSessionContext } from "./tools/session-manager.js";
9
10
  import { configManager } from "./utils/config-manager.js";
10
11
  import { addPushId } from "./utils/pushid-manager.js";
11
12
  import { getPushDataById } from "./utils/pushdata-manager.js";
13
+ import { selfEvolutionManager } from "./utils/self-evolution-manager.js";
12
14
  import { saveRuntimeInfo } from "./utils/runtime-manager.js";
15
+ import { toolCallNudgeManager } from "./utils/tool-call-nudge-manager.js";
13
16
  import { registerTaskId, decrementTaskIdRef, lockTaskId, unlockTaskId, hasActiveTask, } from "./task-manager.js";
14
17
  /**
15
18
  * Handle an incoming A2A message.
@@ -87,6 +90,7 @@ export async function handleXYMessage(params) {
87
90
  text: pushDataItem.dataDetail,
88
91
  append: false,
89
92
  final: true,
93
+ runtime,
90
94
  });
91
95
  log(`[BOT] ✅ Trigger response sent successfully, exiting early`);
92
96
  return; // 提前返回,不继续处理
@@ -159,6 +163,7 @@ export async function handleXYMessage(params) {
159
163
  taskId: parsed.taskId,
160
164
  messageId: parsed.messageId,
161
165
  agentId: route.accountId,
166
+ deviceType,
162
167
  });
163
168
  // 🔑 发送初始状态更新(第二条消息也要发,用新taskId)
164
169
  log(`[STATUS] Sending initial status update for session ${parsed.sessionId}`);
@@ -169,20 +174,41 @@ export async function handleXYMessage(params) {
169
174
  messageId: parsed.messageId,
170
175
  text: isSecondMessage ? "新消息已接收,正在处理..." : "任务正在处理中,请稍候~",
171
176
  state: "working",
177
+ runtime,
172
178
  }).catch((err) => {
173
179
  error(`Failed to send initial status update:`, err);
174
180
  });
175
181
  // Extract text and files from parts
176
182
  const text = extractTextFromParts(parsed.parts);
183
+ let textForAgent = text || "";
184
+ if (route.sessionKey && textForAgent) {
185
+ try {
186
+ const selfEvolutionEnabled = await selfEvolutionManager.isEnabled();
187
+ if (selfEvolutionEnabled && shouldNudgeForSelfEvolutionKeyword(textForAgent)) {
188
+ const shouldNudge = toolCallNudgeManager.tryMarkKeywordNudge(route.sessionKey);
189
+ log(`[SELF_EVOLUTION] Keyword check hit during inbound build: sessionKey=${route.sessionKey}, shouldNudge=${shouldNudge}`);
190
+ if (shouldNudge) {
191
+ const augmented = appendSelfEvolutionKeywordNudge(textForAgent);
192
+ textForAgent = augmented.text;
193
+ if (augmented.appended) {
194
+ log(`[SELF_EVOLUTION] Keyword-triggered inline nudge appended: sessionKey=${route.sessionKey}`);
195
+ }
196
+ }
197
+ }
198
+ }
199
+ catch (selfEvolutionError) {
200
+ error(`[SELF_EVOLUTION] Failed to append inline keyword nudge: ${String(selfEvolutionError)}`);
201
+ }
202
+ }
177
203
  const fileParts = extractFileParts(parsed.parts);
178
204
  // Download files to local disk
179
205
  const downloadedFiles = await downloadFilesFromParts(fileParts);
180
- console.log("Downloaded files:", JSON.stringify(downloadedFiles, null, 2));
206
+ log("Downloaded files:", JSON.stringify(downloadedFiles, null, 2));
181
207
  const mediaPayload = buildXYMediaPayload(downloadedFiles);
182
208
  // Resolve envelope format options (following feishu pattern)
183
209
  const envelopeOptions = core.channel.reply.resolveEnvelopeFormatOptions(cfg);
184
210
  // Build message body with speaker prefix (following feishu pattern)
185
- let messageBody = text || "";
211
+ let messageBody = textForAgent;
186
212
  // Add speaker prefix for clarity
187
213
  const speaker = parsed.sessionId;
188
214
  messageBody = `${speaker}: ${messageBody}`;
@@ -198,8 +224,8 @@ export async function handleXYMessage(params) {
198
224
  // Use route.accountId and route.sessionKey instead of parsed fields
199
225
  const ctxPayload = core.channel.reply.finalizeInboundContext({
200
226
  Body: body,
201
- RawBody: text || "",
202
- CommandBody: text || "",
227
+ RawBody: textForAgent,
228
+ CommandBody: textForAgent,
203
229
  From: parsed.sessionId,
204
230
  To: parsed.sessionId, // ✅ Simplified: use sessionId as target (context is managed by SessionKey)
205
231
  SessionKey: route.sessionKey, // ✅ Use route.sessionKey
@@ -1,28 +1,9 @@
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 { locationTool } from "./tools/location-tool.js";
5
- import { xiaoyiGuiTool } from "./tools/xiaoyi-gui-tool.js";
6
- import { sendFileToUserTool } from "./tools/send-file-to-user-tool.js";
7
- import { viewPushResultTool } from "./tools/view-push-result-tool.js";
8
- import { imageReadingTool } from "./tools/image-reading-tool.js";
9
- import { timestampToUtc8Tool } from "./tools/timestamp-to-utc8-tool.js";
10
- import { saveSelfEvolutionSkillTool } from "./tools/save-self-evolution-skill-tool.js";
11
- import { getEmailToolSchemaTool } from "./tools/get-email-tool-schema.js";
12
- import { callDeviceTool } from "./tools/call-device-tool.js";
13
- import { getNoteToolSchemaTool } from "./tools/get-note-tool-schema.js";
14
- import { getCalendarToolSchemaTool } from "./tools/get-calendar-tool-schema.js";
15
- import { getContactToolSchemaTool } from "./tools/get-contact-tool-schema.js";
16
- import { getPhotoToolSchemaTool } from "./tools/get-photo-tool-schema.js";
17
- import { getDeviceFileToolSchemaTool } from "./tools/get-device-file-tool-schema.js";
18
- import { getAlarmToolSchemaTool } from "./tools/get-alarm-tool-schema.js";
19
- import { getCollectionToolSchemaTool } from "./tools/get-collection-tool-schema.js";
20
- import { queryAppMessageTool } from "./tools/query-app-message-tool.js";
21
- import { queryMemoryDataTool } from "./tools/query-memory-data-tool.js";
22
- import { queryTodoTaskTool } from "./tools/query-todo-task-tool.js";
23
- import { loginTokenTool } from "./tools/login-token-tool.js";
24
4
  import { filterToolsByDevice } from "./tools/device-tool-map.js";
25
5
  import { getCurrentSessionContext } from "./tools/session-manager.js";
6
+ import { createAllTools } from "./tools/create-all-tools.js";
26
7
  import { logger } from "./utils/logger.js";
27
8
  /**
28
9
  * Xiaoyi Channel Plugin for OpenClaw.
@@ -63,8 +44,8 @@ export const xyPlugin = {
63
44
  },
64
45
  outbound: xyOutbound,
65
46
  agentTools: () => {
66
- const allTools = [locationTool, callDeviceTool, getNoteToolSchemaTool, getCalendarToolSchemaTool, getContactToolSchemaTool, getPhotoToolSchemaTool, xiaoyiGuiTool, getDeviceFileToolSchemaTool, getAlarmToolSchemaTool, getCollectionToolSchemaTool, sendFileToUserTool, viewPushResultTool, imageReadingTool, timestampToUtc8Tool, saveSelfEvolutionSkillTool, getEmailToolSchemaTool, queryAppMessageTool, queryMemoryDataTool, queryTodoTaskTool, loginTokenTool];
67
47
  const ctx = getCurrentSessionContext();
48
+ const allTools = createAllTools(ctx);
68
49
  const filtered = filterToolsByDevice(allTools, ctx?.deviceType);
69
50
  logger.log(`[DEVICE-FILTER] deviceType=${ctx?.deviceType ?? "(none)"}, tools: ${allTools.length} → ${filtered.length} (${filtered.map(t => t.name).join(", ")})`);
70
51
  return filtered;
@@ -12,8 +12,14 @@ export function setClientRuntime(rt) {
12
12
  /**
13
13
  * Global cache for WebSocket managers.
14
14
  * Key format: `${apiKey}-${agentId}`
15
+ * Uses globalThis to ensure a single cache across all module copies
16
+ * (same fix as session-manager.ts for openclaw multi-instance loading).
15
17
  */
16
- const wsManagerCache = new Map();
18
+ const _g = globalThis;
19
+ if (!_g.__xyWsManagerCache) {
20
+ _g.__xyWsManagerCache = new Map();
21
+ }
22
+ const wsManagerCache = _g.__xyWsManagerCache;
17
23
  /**
18
24
  * Get or create a WebSocket manager for the given configuration.
19
25
  * Reuses existing managers if config matches.
@@ -38,16 +44,17 @@ export function getXYWebSocketManager(config) {
38
44
  * Disconnects the manager and removes it from the cache.
39
45
  */
40
46
  export function removeXYWebSocketManager(config) {
47
+ const log = runtime?.log ?? console.log;
41
48
  const cacheKey = `${config.apiKey}-${config.agentId}`;
42
49
  const manager = wsManagerCache.get(cacheKey);
43
50
  if (manager) {
44
- console.log(`🗑️ [WS-MANAGER-CACHE] Removing manager from cache: ${cacheKey}`);
51
+ log(`🗑️ [WS-MANAGER-CACHE] Removing manager from cache: ${cacheKey}`);
45
52
  manager.disconnect();
46
53
  wsManagerCache.delete(cacheKey);
47
- console.log(`🗑️ [WS-MANAGER-CACHE] Manager removed, remaining managers: ${wsManagerCache.size}`);
54
+ log(`🗑️ [WS-MANAGER-CACHE] Manager removed, remaining managers: ${wsManagerCache.size}`);
48
55
  }
49
56
  else {
50
- console.log(`⚠️ [WS-MANAGER-CACHE] Manager not found in cache: ${cacheKey}`);
57
+ log(`⚠️ [WS-MANAGER-CACHE] Manager not found in cache: ${cacheKey}`);
51
58
  }
52
59
  }
53
60
  /**
@@ -72,36 +79,37 @@ export function getCachedManagerCount() {
72
79
  * Helps identify connection issues and orphan connections.
73
80
  */
74
81
  export function diagnoseAllManagers() {
75
- console.log(`Total cached managers: ${wsManagerCache.size}`);
82
+ const log = runtime?.log ?? console.log;
83
+ log(`Total cached managers: ${wsManagerCache.size}`);
76
84
  if (wsManagerCache.size === 0) {
77
- console.log("ℹ️ No managers in cache");
85
+ log("ℹ️ No managers in cache");
78
86
  return;
79
87
  }
80
88
  let orphanCount = 0;
81
89
  wsManagerCache.forEach((manager, key) => {
82
90
  const diag = manager.getConnectionDiagnostics();
83
- console.log(` Total event listeners on manager: ${diag.totalEventListeners}`);
91
+ log(` Total event listeners on manager: ${diag.totalEventListeners}`);
84
92
  // Connection
85
- console.log(` 🔌 Connection:`);
86
- console.log(` - Exists: ${diag.connection.exists}`);
87
- console.log(` - ReadyState: ${diag.connection.readyState}`);
88
- console.log(` - State connected/ready: ${diag.connection.stateConnected}/${diag.connection.stateReady}`);
89
- console.log(` - Reconnect attempts: ${diag.connection.reconnectAttempts}`);
90
- console.log(` - Listeners on WebSocket: ${diag.connection.listenerCount}`);
91
- console.log(` - Heartbeat active: ${diag.connection.heartbeatActive}`);
92
- console.log(` - Has reconnect timer: ${diag.connection.hasReconnectTimer}`);
93
+ log(` 🔌 Connection:`);
94
+ log(` - Exists: ${diag.connection.exists}`);
95
+ log(` - ReadyState: ${diag.connection.readyState}`);
96
+ log(` - State connected/ready: ${diag.connection.stateConnected}/${diag.connection.stateReady}`);
97
+ log(` - Reconnect attempts: ${diag.connection.reconnectAttempts}`);
98
+ log(` - Listeners on WebSocket: ${diag.connection.listenerCount}`);
99
+ log(` - Heartbeat active: ${diag.connection.heartbeatActive}`);
100
+ log(` - Has reconnect timer: ${diag.connection.hasReconnectTimer}`);
93
101
  if (diag.connection.isOrphan) {
94
- console.log(` ⚠️ ORPHAN CONNECTION DETECTED!`);
102
+ log(` ⚠️ ORPHAN CONNECTION DETECTED!`);
95
103
  orphanCount++;
96
104
  }
97
- console.log("");
105
+ log("");
98
106
  });
99
107
  if (orphanCount > 0) {
100
- console.log(`⚠️ Total orphan connections found: ${orphanCount}`);
101
- console.log(`💡 Suggestion: These connections should be cleaned up`);
108
+ log(`⚠️ Total orphan connections found: ${orphanCount}`);
109
+ log(`💡 Suggestion: These connections should be cleaned up`);
102
110
  }
103
111
  else {
104
- console.log(`✅ No orphan connections found`);
112
+ log(`✅ No orphan connections found`);
105
113
  }
106
114
  }
107
115
  /**
@@ -109,17 +117,18 @@ export function diagnoseAllManagers() {
109
117
  * Returns the number of managers that had orphan connections.
110
118
  */
111
119
  export function cleanupOrphanConnections() {
120
+ const log = runtime?.log ?? console.log;
112
121
  let cleanedCount = 0;
113
122
  wsManagerCache.forEach((manager, key) => {
114
123
  const diag = manager.getConnectionDiagnostics();
115
124
  if (diag.connection.isOrphan) {
116
- console.log(`🧹 Cleaning up orphan connections in manager: ${key}`);
125
+ log(`🧹 Cleaning up orphan connections in manager: ${key}`);
117
126
  manager.disconnect();
118
127
  cleanedCount++;
119
128
  }
120
129
  });
121
130
  if (cleanedCount > 0) {
122
- console.log(`🧹 Cleaned up ${cleanedCount} manager(s) with orphan connections`);
131
+ log(`🧹 Cleaned up ${cleanedCount} manager(s) with orphan connections`);
123
132
  }
124
133
  return cleanedCount;
125
134
  }
@@ -4,12 +4,13 @@ import { URL } from "node:url";
4
4
  import { randomBytes } from "node:crypto";
5
5
  import { getCsplConfig } from "./config.js";
6
6
  import { DEFAULT_HTTP_PORT, HTTP_STATUS_BAD_REQUEST } from "./constants.js";
7
+ import { logger } from "../utils/logger.js";
7
8
  function generateTraceId() {
8
9
  return randomBytes(16).toString("hex");
9
10
  }
10
11
  function buildHeaders(config) {
11
12
  const traceId = generateTraceId();
12
- console.log(`[SENTINEL HOOK] trace-id: ${traceId}`);
13
+ logger.log(`[SENTINEL HOOK] trace-id: ${traceId}`);
13
14
  return {
14
15
  "x-hag-trace-id": traceId,
15
16
  "x-uid": config.uid,
@@ -65,21 +66,21 @@ export async function callCsplApi(questionText, cfg) {
65
66
  res.on("end", () => {
66
67
  try {
67
68
  const result = parseResponse(data);
68
- console.log(`[SENTINEL HOOK] ✅ 请求成功`);
69
+ logger.log(`[SENTINEL HOOK] ✅ 请求成功`);
69
70
  resolve(result);
70
71
  }
71
72
  catch (e) {
72
- console.error(`[SENTINEL HOOK] ❌ 请求失败: ${e instanceof Error ? e.message : String(e)}`);
73
+ logger.error(`[SENTINEL HOOK] ❌ 请求失败: ${e instanceof Error ? e.message : String(e)}`);
73
74
  reject(e);
74
75
  }
75
76
  });
76
77
  });
77
78
  req.on("error", (error) => {
78
- console.error(`[SENTINEL HOOK] ❌ 请求错误: ${error instanceof Error ? error.message : String(error)}`);
79
+ logger.error(`[SENTINEL HOOK] ❌ 请求错误: ${error instanceof Error ? error.message : String(error)}`);
79
80
  reject(error);
80
81
  });
81
82
  req.on("timeout", () => {
82
- console.error(`[SENTINEL HOOK] ⏰ 请求超时 (${config.api.timeout}ms)`);
83
+ logger.error(`[SENTINEL HOOK] ⏰ 请求超时 (${config.api.timeout}ms)`);
83
84
  req.destroy();
84
85
  reject(new Error("[SENTINEL HOOK] Request timeout"));
85
86
  });
@@ -2,6 +2,7 @@
2
2
  import fetch from "node-fetch";
3
3
  import fs from "fs/promises";
4
4
  import path from "path";
5
+ import { logger } from "./utils/logger.js";
5
6
  /**
6
7
  * Download a file from URL to local path.
7
8
  */
@@ -19,10 +20,10 @@ export async function downloadFile(url, destPath) {
19
20
  }
20
21
  catch (error) {
21
22
  if (error.name === 'AbortError') {
22
- console.log(`Download timeout (30s) for ${url}`);
23
+ logger.log(`Download timeout (30s) for ${url}`);
23
24
  throw new Error(`Download timeout after 30 seconds`);
24
25
  }
25
- console.log(`Failed to download file from ${url}:`);
26
+ logger.log(`Failed to download file from ${url}:`);
26
27
  throw error;
27
28
  }
28
29
  finally {
@@ -51,7 +52,7 @@ export async function downloadFilesFromParts(fileParts, tempDir = "/tmp/xy_chann
51
52
  });
52
53
  }
53
54
  catch (error) {
54
- console.log(`Failed to download file ${name}:`);
55
+ logger.log(`Failed to download file ${name}:`);
55
56
  // Continue with other files
56
57
  }
57
58
  }