@ynhcj/xiaoyi-channel 0.0.98-beta → 0.0.100-beta

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 CHANGED
@@ -5,9 +5,24 @@ import { setXYRuntime } from "./src/runtime.js";
5
5
  import { tryInjectSteer } from "./src/steer-injector.js";
6
6
  import { callCsplApi } from "./src/cspl/call-api.js";
7
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";
8
10
  import { ALLOWED_TOOLS, MIN_TEXT_LENGTH, MAX_TOTAL_LENGTH, MAX_TEXT_LENGTH, STEER_ABORT_MESSAGE, } from "./src/cspl/constants.js";
9
11
  import { createBeforePromptBuildHandler } from "./src/skill-retriever/hooks.js";
10
12
  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;
25
+ }
11
26
  /**
12
27
  * Xiaoyi Channel Plugin Entry Point.
13
28
  * Exports the plugin for OpenClaw to load.
@@ -25,6 +40,21 @@ const plugin = {
25
40
  // SENTINEL HOOK after_tool_call hook: 监听工具结果,发送至安全检测 API 进行安全检测
26
41
  // 如果响应为 REJECT,注入 steer 消息中止当前对话
27
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
+ }
28
58
  if (!ALLOWED_TOOLS.includes(event.toolName)) {
29
59
  return;
30
60
  }
@@ -7,6 +7,7 @@ import { sendFileToUserTool } from "./tools/send-file-to-user-tool.js";
7
7
  import { viewPushResultTool } from "./tools/view-push-result-tool.js";
8
8
  import { imageReadingTool } from "./tools/image-reading-tool.js";
9
9
  import { timestampToUtc8Tool } from "./tools/timestamp-to-utc8-tool.js";
10
+ import { saveSelfEvolutionSkillTool } from "./tools/save-self-evolution-skill-tool.js";
10
11
  import { getEmailToolSchemaTool } from "./tools/get-email-tool-schema.js";
11
12
  import { callDeviceTool } from "./tools/call-device-tool.js";
12
13
  import { getNoteToolSchemaTool } from "./tools/get-note-tool-schema.js";
@@ -62,7 +63,7 @@ export const xyPlugin = {
62
63
  },
63
64
  outbound: xyOutbound,
64
65
  agentTools: () => {
65
- const allTools = [locationTool, callDeviceTool, getNoteToolSchemaTool, getCalendarToolSchemaTool, getContactToolSchemaTool, getPhotoToolSchemaTool, xiaoyiGuiTool, getDeviceFileToolSchemaTool, getAlarmToolSchemaTool, getCollectionToolSchemaTool, sendFileToUserTool, viewPushResultTool, imageReadingTool, timestampToUtc8Tool, getEmailToolSchemaTool, queryAppMessageTool, queryMemoryDataTool, queryTodoTaskTool, loginTokenTool];
66
+ const allTools = [locationTool, callDeviceTool, getNoteToolSchemaTool, getCalendarToolSchemaTool, getContactToolSchemaTool, getPhotoToolSchemaTool, xiaoyiGuiTool, getDeviceFileToolSchemaTool, getAlarmToolSchemaTool, getCollectionToolSchemaTool, sendFileToUserTool, viewPushResultTool, imageReadingTool, timestampToUtc8Tool, saveSelfEvolutionSkillTool, getEmailToolSchemaTool, queryAppMessageTool, queryMemoryDataTool, queryTodoTaskTool, loginTokenTool];
66
67
  const ctx = getCurrentSessionContext();
67
68
  const filtered = filterToolsByDevice(allTools, ctx?.deviceType);
68
69
  logger.log(`[DEVICE-FILTER] deviceType=${ctx?.deviceType ?? "(none)"}, tools: ${allTools.length} → ${filtered.length} (${filtered.map(t => t.name).join(", ")})`);
@@ -71,7 +71,6 @@ export async function monitorXYProvider(opts = {}) {
71
71
  const messageHandler = (message, sessionId, serverId) => {
72
72
  const messageKey = `${sessionId}::${message.id}`;
73
73
  log(`[MONITOR-HANDLER] ####### messageHandler triggered: sessionId=${sessionId}, messageId=${message.id} #######`);
74
- console.log(`[MONITOR-HANDLER] A2A message body: ${JSON.stringify(message)}`);
75
74
  // ✅ Report health: received a message
76
75
  trackEvent?.();
77
76
  // Check for duplicate message handling
@@ -9,6 +9,7 @@
9
9
  // models.providers.xiaoyiprovider.models = [...]
10
10
  import { createHash } from "crypto";
11
11
  import { getCurrentSessionContext } from "./tools/session-manager.js";
12
+ import { selfEvolutionManager } from "./utils/self-evolution-manager.js";
12
13
  // ── Retry config ──────────────────────────────────────────────
13
14
  const RETRY_DELAYS_MS = [10_000, 20_000, 40_000, 60_000];
14
15
  const MAX_RETRY_ATTEMPTS = 8;
@@ -41,6 +42,25 @@ function isCronTriggered(messages) {
41
42
  }
42
43
  return /^\[cron:/i.test(text.trim());
43
44
  }
45
+ /** Extract cron title from first user message matching `[cron:<uuid> <title>]`. */
46
+ function extractCronTitle(messages) {
47
+ if (!messages)
48
+ return undefined;
49
+ const firstUser = messages.find(m => m.role === "user");
50
+ if (!firstUser)
51
+ return undefined;
52
+ let text = "";
53
+ if (typeof firstUser.content === "string") {
54
+ text = firstUser.content;
55
+ }
56
+ else if (Array.isArray(firstUser.content)) {
57
+ const block = firstUser.content.find(b => b.type === "text" && typeof b.text === "string");
58
+ if (block)
59
+ text = block.text;
60
+ }
61
+ const match = text.trim().match(/^\[cron:[^\s]+\s+(.+)\]$/);
62
+ return match ? match[1] : undefined;
63
+ }
44
64
  /** Compute retry delay in ms for the given 1-based attempt, with up to 10s jitter. */
45
65
  function getRetryDelayMs(attempt, isCron = false) {
46
66
  if (isCron) {
@@ -217,6 +237,39 @@ const HEADER_SESSION_ID = "x-session-id";
217
237
  const HEADER_INTERACTION_ID = "x-interaction-id";
218
238
  /** Internal key for passing fallback uid prefix from prepareExtraParams to wrapStreamFn. */
219
239
  const FALLBACK_PREFIX_KEY = "_xiaoyi_fallback_prefix";
240
+ const SELF_EVOLUTION_PROMPT_BEGIN = "<self_evolution_prompt>";
241
+ const SELF_EVOLUTION_PROMPT_END = "</self_evolution_prompt>";
242
+ const SELF_EVOLUTION_ENABLED_PROMPT_SECTION = `
243
+ ${SELF_EVOLUTION_PROMPT_BEGIN}
244
+ ## Self Evolution(CRITICAL)
245
+ 用户已启用自进化(不管之前是什么状态,不管之前的对话记录,当前视作已开启即可)。
246
+ 自进化含义:当你识别出某个稳定、可复用的经验、调试模式或工作流程,并且它很可能对未来完成此用户的同类任务有帮助时,你可以考虑将其保存为可复用的经验。
247
+
248
+ [触发条件]
249
+ 当满足以下任意一项时,必须触发自进化流程:
250
+ - 显式指令与关键词匹配(最高优先级):当用户消息中包含“进化”、“下次应该”、“以后必须”、“永远”等表达长期意图的词汇,或伴随明显的行为纠正、流程规范化或交付标准设定的指导动作等。
251
+ - 主动固化要求:用户主动要求将过去的交互经验(如交付件规格、skill选择、任务工作流等)进行沉淀/自进化。
252
+ - 隐式经验与避坑沉淀:当前任务执行过程中,你识别出了可以持久化为长期经验的标准工作流,或者在执行中遇到了工具调用错误、命令参数错误等明确的“踩坑点”,以后需要规避。
253
+
254
+ [触发后处理]
255
+ - 遵循 "self-evolution" 技能指导逐步进行技能评估。
256
+ - 只能在任务完成后,正式回复用户前读取该技能。
257
+ - 禁止直接修改文件。需要先创建提案,向用户发送进化请求,用户同意后才能进行更改。
258
+ ${SELF_EVOLUTION_PROMPT_END}
259
+ `.trim();
260
+ const SELF_EVOLUTION_DISABLED_PROMPT_SECTION = `
261
+ ${SELF_EVOLUTION_PROMPT_BEGIN}
262
+ ## Self Evolution
263
+ 用户当前已禁用自我进化。
264
+ 你不得执行自进化相关行为,并且应将此功能视为不可用(不管之前是什么状态,不管之前的对话记录,当前视作已关闭即可)。
265
+ ${SELF_EVOLUTION_PROMPT_END}
266
+ `.trim();
267
+ function stripSelfEvolutionPrompt(prompt) {
268
+ return prompt
269
+ .replace(/\n*<self_evolution_prompt>[\s\S]*?<\/self_evolution_prompt>\n*/gu, "\n\n")
270
+ .replace(/\n{3,}/gu, "\n\n")
271
+ .trim();
272
+ }
220
273
  /**
221
274
  * Encode uid via SHA-256 and take first 32 hex chars.
222
275
  */
@@ -293,6 +346,11 @@ export const xiaoyiProvider = {
293
346
  dynamicHeaders[HEADER_TRACE_ID] = isCron ? `cron_${fallbackValue}` : fallbackValue;
294
347
  dynamicHeaders[HEADER_SESSION_ID] = fallbackValue;
295
348
  dynamicHeaders[HEADER_INTERACTION_ID] = fallbackValue;
349
+ if (isCron) {
350
+ const cronTitle = extractCronTitle(context.messages);
351
+ if (cronTitle)
352
+ dynamicHeaders["x-cron-title"] = cronTitle;
353
+ }
296
354
  }
297
355
  else {
298
356
  // Session mode: use pre-resolved session headers + fresh timestamp
@@ -303,6 +361,11 @@ export const xiaoyiProvider = {
303
361
  if (typeof traceId === "string") {
304
362
  const isCron = isCronTriggered(context.messages);
305
363
  dynamicHeaders[HEADER_TRACE_ID] = isCron ? `cron_${traceId}${ts}` : `${traceId}${ts}`;
364
+ if (isCron) {
365
+ const cronTitle = extractCronTitle(context.messages);
366
+ if (cronTitle)
367
+ dynamicHeaders["x-cron-title"] = cronTitle;
368
+ }
306
369
  }
307
370
  if (typeof sessionId === "string")
308
371
  dynamicHeaders[HEADER_SESSION_ID] = sessionId;
@@ -315,6 +378,7 @@ export const xiaoyiProvider = {
315
378
  if (context.systemPrompt) {
316
379
  console.log(`[xiaoyiprovider] system prompt length: ${context.systemPrompt.length}`);
317
380
  }
381
+ const sessionCtx = getCurrentSessionContext();
318
382
  // 在发送给模型前,优化 systemPrompt 结构
319
383
  if (context.systemPrompt) {
320
384
  let sp = context.systemPrompt;
@@ -344,8 +408,17 @@ export const xiaoyiProvider = {
344
408
  console.log(`[xiaoyiprovider] system prompt optimized: ${beforeLen} -> ${sp.length}`);
345
409
  context.systemPrompt = sp;
346
410
  }
411
+ const selfEvolutionEnabled = await selfEvolutionManager.isEnabled();
412
+ const prompt = stripSelfEvolutionPrompt(context.systemPrompt ?? "");
413
+ context.systemPrompt = [
414
+ prompt,
415
+ selfEvolutionEnabled
416
+ ? SELF_EVOLUTION_ENABLED_PROMPT_SECTION
417
+ : SELF_EVOLUTION_DISABLED_PROMPT_SECTION,
418
+ ]
419
+ .filter(Boolean)
420
+ .join("\n\n");
347
421
  // Append device context to systemPrompt
348
- const sessionCtx = getCurrentSessionContext();
349
422
  if (sessionCtx?.deviceType) {
350
423
  const rawDevice = sessionCtx.deviceType;
351
424
  const displayDevice = (rawDevice === "2in1") ? "鸿蒙PC" : rawDevice;
@@ -38,30 +38,22 @@ export function createBeforePromptBuildHandler(config) {
38
38
  return async (event, ctx) => {
39
39
  const userPrompt = event.prompt;
40
40
  if (ctx?.sessionKey?.includes(":subagent:")) {
41
- console.log(`${PLUGIN_LOG_PREFIX} [SKIP] Sub-agent detected, skipping search`);
42
41
  return undefined;
43
42
  }
44
43
  if (!config.enabled) {
45
- console.log(`${PLUGIN_LOG_PREFIX} [SKIP] Plugin disabled, original query: "${userPrompt}"`);
46
44
  return undefined;
47
45
  }
48
46
  if (!userPrompt || userPrompt.trim().length === 0) {
49
- console.log(`${PLUGIN_LOG_PREFIX} [SKIP] Empty query`);
50
47
  return undefined;
51
48
  }
52
- console.log(`${PLUGIN_LOG_PREFIX} [RECEIVED] Original user query (len=${userPrompt.length}): "${userPrompt}"`);
53
49
  const extractedQuery = extractUserQuery(userPrompt);
54
- console.log(`${PLUGIN_LOG_PREFIX} [EXTRACTED] Extracted user query: "${extractedQuery}"`);
55
50
  if (!extractedQuery || extractedQuery.length === 0) {
56
- console.log(`${PLUGIN_LOG_PREFIX} [SKIP] No valid user query after extraction, skipping search`);
57
51
  return undefined;
58
52
  }
59
53
  const skipReason = shouldSkipSearch(extractedQuery);
60
54
  if (skipReason) {
61
- console.log(`${PLUGIN_LOG_PREFIX} [SKIP] ${skipReason}, extracted query: "${extractedQuery}"`);
62
55
  return undefined;
63
56
  }
64
- console.log(`${PLUGIN_LOG_PREFIX} [PROCEED] Calling skill search API (timeout=${config.timeoutMs}ms) for query: "${extractedQuery}"`);
65
57
  try {
66
58
  const searchResult = await searchTools({
67
59
  query: extractedQuery,
@@ -74,16 +66,14 @@ export function createBeforePromptBuildHandler(config) {
74
66
  timeoutMs: config.timeoutMs,
75
67
  });
76
68
  if (!searchResult || searchResult.tools.length === 0) {
77
- console.log(`${PLUGIN_LOG_PREFIX} [RESULT] No skills found for query: "${extractedQuery}"`);
78
69
  return undefined;
79
70
  }
80
71
  console.log(`${PLUGIN_LOG_PREFIX} [RESULT] Found ${searchResult.tools.length} skills, building context...`);
81
72
  const toolsContext = formatToolsForContext(searchResult, config.includeUninstalledOnly);
82
73
  if (!toolsContext) {
83
- console.log(`${PLUGIN_LOG_PREFIX} [ERROR] Failed to format skills context for query: "${extractedQuery}"`);
74
+ console.log(`${PLUGIN_LOG_PREFIX} [ERROR] Failed to format skills context`);
84
75
  return undefined;
85
76
  }
86
- console.log(`${PLUGIN_LOG_PREFIX} [SUCCESS] Built context with ${searchResult.tools.length} skills for query: "${extractedQuery}"`);
87
77
  return {
88
78
  prependContext: TOOL_RETRIEVER_HEADER + toolsContext + TOOL_RETRIEVER_FOOTER,
89
79
  };
@@ -81,7 +81,6 @@ export async function searchTools(options) {
81
81
  const { query, maxTools = 5, includeUninstalledOnly = true, envFilePath = "~/.openclaw/.xiaoyienv", serviceUrl: configServiceUrl, apiKey: configApiKey, uid: configUid, timeoutMs = 1000, } = options;
82
82
  const envConfig = readEnvFile(envFilePath);
83
83
  const hasRequiredConfig = !!envConfig.SERVICE_URL && !!envConfig.PERSONAL_API_KEY && !!envConfig.PERSONAL_UID;
84
- console.log(`${PLUGIN_LOG_PREFIX} Env file loaded: ${hasRequiredConfig}, keys: ${Object.keys(envConfig).join(", ")}`);
85
84
  const serviceUrl = configServiceUrl ?? envConfig.SERVICE_URL;
86
85
  const apiKey = configApiKey ?? envConfig.PERSONAL_API_KEY;
87
86
  const uid = configUid ?? envConfig.PERSONAL_UID;
@@ -89,7 +88,6 @@ export async function searchTools(options) {
89
88
  console.warn(`${PLUGIN_LOG_PREFIX} Missing required configuration. serviceUrl: "${serviceUrl}", apiKey: "${apiKey ? '(set)' : '(missing)'} ", uid: "${uid ? '(set)' : '(missing)'}"`);
90
89
  return null;
91
90
  }
92
- console.log(`${PLUGIN_LOG_PREFIX} Configuration loaded - serviceUrl: ${serviceUrl}, uid: ${uid}`);
93
91
  const traceId = crypto.randomUUID();
94
92
  const apiUrl = `${serviceUrl}/celia-claw/v1/rest-api/skill/execute`;
95
93
  const headers = {
@@ -101,7 +99,6 @@ export async function searchTools(options) {
101
99
  "x-request-from": "openclaw",
102
100
  };
103
101
  const payload = { query };
104
- console.log(`${PLUGIN_LOG_PREFIX} [USER_REQUEST] origin-query:${query}, payload:${JSON.stringify(payload)}`);
105
102
  try {
106
103
  const response = await fetch(apiUrl, {
107
104
  method: "POST",
@@ -119,13 +116,9 @@ export async function searchTools(options) {
119
116
  responseData.content &&
120
117
  responseData.content.skills) {
121
118
  const rawSkills = responseData.content.skills;
122
- console.log(`${PLUGIN_LOG_PREFIX} [DEBUG] Raw skills from API: ${rawSkills.length}, ids: ${rawSkills.map((s) => s.skillId).join(", ")}`);
123
119
  const installedSkills = getInstalledSkills();
124
- console.log(`${PLUGIN_LOG_PREFIX} [DEBUG] Installed skills: ${installedSkills.length}, ids: ${installedSkills.join(", ")}`);
125
120
  const formattedData = formatSkillData(rawSkills, installedSkills);
126
- console.log(`${PLUGIN_LOG_PREFIX} [DEBUG] Formatted skills: ${formattedData.length}, statuses: ${formattedData.map((t) => `${t.skillId}:${t.status}`).join(", ")}`);
127
121
  const topTools = formattedData.slice(0, 2);
128
- console.log(`${PLUGIN_LOG_PREFIX} [DEBUG] Top 2 skills: ${topTools.length}, statuses: ${topTools.map((t) => `${t.skillId}:${t.status}`).join(", ")}`);
129
122
  const allInstalled = topTools.every((tool) => tool.status === "已安装");
130
123
  if (allInstalled) {
131
124
  console.log(`${PLUGIN_LOG_PREFIX} [DEBUG] All top 2 skills are installed, returning null`);
@@ -0,0 +1 @@
1
+ export declare const saveSelfEvolutionSkillTool: any;
@@ -0,0 +1,189 @@
1
+ import { createHash } from "node:crypto";
2
+ import fs from "node:fs/promises";
3
+ import path from "node:path";
4
+ import { getCurrentSessionContext } from "./session-manager.js";
5
+ import { selfEvolutionManager } from "../utils/self-evolution-manager.js";
6
+ const SELF_EVOLVED_SKILL_ROOT = "/home/sandbox/.openclaw/workspace/skills";
7
+ function slugifyTitle(title) {
8
+ return title
9
+ .trim()
10
+ .toLowerCase()
11
+ .replace(/[^a-z0-9]+/g, "-")
12
+ .replace(/^-+|-+$/g, "")
13
+ .slice(0, 80);
14
+ }
15
+ function normalizeStringArray(value) {
16
+ if (Array.isArray(value)) {
17
+ return value
18
+ .map((item) => (typeof item === "string" ? item.trim() : ""))
19
+ .filter(Boolean);
20
+ }
21
+ if (typeof value === "string" && value.trim()) {
22
+ return [value.trim()];
23
+ }
24
+ return [];
25
+ }
26
+ function containsSensitiveContent(text) {
27
+ const lower = text.toLowerCase();
28
+ const sensitivePatterns = [
29
+ /api[_ -]?key/u,
30
+ /access[_ -]?token/u,
31
+ /bearer\s+[a-z0-9._-]+/iu,
32
+ /password/u,
33
+ /secret/u,
34
+ /\/home\/sandbox\//u,
35
+ /\/tmp\//u,
36
+ /[a-z]:\\/iu,
37
+ ];
38
+ return sensitivePatterns.some((pattern) => pattern.test(lower));
39
+ }
40
+ function buildSkillMarkdown(params) {
41
+ const description = `${params.summary}\n\nWhen to use: ${params.whenToUse}`
42
+ .replace(/"/g, '\\"')
43
+ .replace(/\r?\n/g, "\\n");
44
+ const lines = [
45
+ "---",
46
+ `name: "${params.title.replace(/"/g, '\\"')}"`,
47
+ `description: "${description}"`,
48
+ "---",
49
+ "",
50
+ `# ${params.title}`,
51
+ "",
52
+ "## Rules",
53
+ ];
54
+ for (const rule of params.rules) {
55
+ lines.push(`- ${rule}`);
56
+ }
57
+ if (params.examples.length > 0) {
58
+ lines.push("", "## Examples");
59
+ for (const example of params.examples) {
60
+ lines.push(`- ${example}`);
61
+ }
62
+ }
63
+ if (params.tags.length > 0) {
64
+ lines.push("", "## Tags", params.tags.map((tag) => `- ${tag}`).join("\n"));
65
+ }
66
+ lines.push("");
67
+ return lines.join("\n");
68
+ }
69
+ export const saveSelfEvolutionSkillTool = {
70
+ name: "save_self_evolution_skill",
71
+ label: "Save Self Evolution Skill",
72
+ description: "将可复用的经验/脚本/教训等保存为skill技能,供下次执行类似任务时参考。仅用于通用、可复用的场景。",
73
+ parameters: {
74
+ type: "object",
75
+ properties: {
76
+ title: {
77
+ type: "string",
78
+ description: "所学技能的简短、可复用标题。",
79
+ },
80
+ summary: {
81
+ type: "string",
82
+ description: "技能的概括性总结,不要太长。",
83
+ },
84
+ when_to_use: {
85
+ type: "string",
86
+ description: "描述在未来任务中什么情况/哪些条件下使用此技能。",
87
+ },
88
+ rules: {
89
+ type: "array",
90
+ items: { type: "string" },
91
+ description: "具体、可复用的规则或checklist。",
92
+ },
93
+ examples: {
94
+ type: "array",
95
+ items: { type: "string" },
96
+ description: "陷阱示例以及正确模式示例,可选",
97
+ },
98
+ tags: {
99
+ type: "array",
100
+ items: { type: "string" },
101
+ description: "用于未来发现的标签,可选。",
102
+ },
103
+ },
104
+ required: ["title", "summary", "when_to_use", "rules"],
105
+ },
106
+ async execute(_toolCallId, params) {
107
+ if (!(await selfEvolutionManager.isEnabled())) {
108
+ throw new Error("Self-evolution is currently disabled by the user.");
109
+ }
110
+ const sessionContext = getCurrentSessionContext();
111
+ if (!sessionContext) {
112
+ throw new Error("No active XY session found. This tool can only run during an active conversation.");
113
+ }
114
+ const title = typeof params.title === "string" ? params.title.trim() : "";
115
+ const summary = typeof params.summary === "string" ? params.summary.trim() : "";
116
+ const whenToUse = typeof params.when_to_use === "string" ? params.when_to_use.trim() : "";
117
+ const rules = normalizeStringArray(params.rules);
118
+ const examples = normalizeStringArray(params.examples);
119
+ const tags = normalizeStringArray(params.tags);
120
+ if (!title || !summary || !whenToUse || rules.length === 0) {
121
+ throw new Error("Missing required fields. title, summary, when_to_use, and at least one rule are required.");
122
+ }
123
+ if (title.length < 6 || summary.length < 10 || whenToUse.length < 10) {
124
+ throw new Error("Skill content is too short. Provide a reusable title, summary, and usage guidance.");
125
+ }
126
+ const combinedText = [title, summary, whenToUse, ...rules, ...examples, ...tags].join("\n");
127
+ if (containsSensitiveContent(combinedText)) {
128
+ throw new Error("Skill content appears to contain sensitive or environment-specific data and was rejected.");
129
+ }
130
+ const slug = slugifyTitle(title);
131
+ if (!slug) {
132
+ throw new Error("Title could not be normalized into a valid skill name.");
133
+ }
134
+ const skillDir = path.join(SELF_EVOLVED_SKILL_ROOT, `evolving-${slug}`);
135
+ const skillFilePath = path.join(skillDir, "SKILL.md");
136
+ const nextContent = buildSkillMarkdown({
137
+ title,
138
+ summary,
139
+ whenToUse,
140
+ rules,
141
+ examples,
142
+ tags,
143
+ });
144
+ const nextHash = createHash("sha256").update(nextContent).digest("hex");
145
+ await fs.mkdir(skillDir, { recursive: true });
146
+ try {
147
+ const existingContent = await fs.readFile(skillFilePath, "utf-8");
148
+ const existingHash = createHash("sha256").update(existingContent).digest("hex");
149
+ if (existingHash === nextHash) {
150
+ return {
151
+ content: [
152
+ {
153
+ type: "text",
154
+ text: JSON.stringify({
155
+ success: true,
156
+ deduped: true,
157
+ skillName: slug,
158
+ path: skillFilePath,
159
+ message: "An identical self-evolved skill already exists.",
160
+ }),
161
+ },
162
+ ],
163
+ };
164
+ }
165
+ throw new Error(`A different skill with the same title already exists: ${skillFilePath}`);
166
+ }
167
+ catch (error) {
168
+ if (error?.code !== "ENOENT") {
169
+ throw error;
170
+ }
171
+ }
172
+ await fs.writeFile(skillFilePath, nextContent, "utf-8");
173
+ return {
174
+ content: [
175
+ {
176
+ type: "text",
177
+ text: JSON.stringify({
178
+ success: true,
179
+ deduped: false,
180
+ skillName: slug,
181
+ path: skillFilePath,
182
+ sessionId: sessionContext.sessionId,
183
+ message: "Self-evolved skill saved successfully.",
184
+ }),
185
+ },
186
+ ],
187
+ };
188
+ },
189
+ };
@@ -2,6 +2,7 @@
2
2
  // Stores active session contexts that tools can access
3
3
  import { AsyncLocalStorage } from "async_hooks";
4
4
  import { configManager } from "../utils/config-manager.js";
5
+ import { toolCallNudgeManager } from "../utils/tool-call-nudge-manager.js";
5
6
  import { getCurrentTaskId, getCurrentMessageId } from "../task-manager.js";
6
7
  // Map of sessionKey -> SessionContextWithRef
7
8
  const activeSessions = new Map();
@@ -40,6 +41,7 @@ export function unregisterSession(sessionKey) {
40
41
  if (existing.refCount <= 0) {
41
42
  activeSessions.delete(sessionKey);
42
43
  configManager.clearSession(existing.sessionId);
44
+ toolCallNudgeManager.clearSession(sessionKey);
43
45
  }
44
46
  }
45
47
  /**
@@ -0,0 +1,5 @@
1
+ declare class SelfEvolutionManager {
2
+ isEnabled(): Promise<boolean>;
3
+ }
4
+ export declare const selfEvolutionManager: SelfEvolutionManager;
5
+ export {};
@@ -0,0 +1,47 @@
1
+ import fs from "node:fs/promises";
2
+ const SELF_EVOLUTION_ENV_FILE = "/home/sandbox/.openclaw/.xiaoyienv";
3
+ const SELF_EVOLUTION_ENV_KEY = "selfEvolutionState";
4
+ function parseBooleanLike(value) {
5
+ const normalized = value.trim().toLowerCase();
6
+ if (normalized === "true") {
7
+ return true;
8
+ }
9
+ if (normalized === "false") {
10
+ return false;
11
+ }
12
+ return null;
13
+ }
14
+ class SelfEvolutionManager {
15
+ async isEnabled() {
16
+ try {
17
+ const envData = await fs.readFile(SELF_EVOLUTION_ENV_FILE, "utf-8");
18
+ for (const line of envData.split(/\r?\n/u)) {
19
+ const trimmed = line.trim();
20
+ if (!trimmed || trimmed.startsWith("#")) {
21
+ continue;
22
+ }
23
+ const eqIndex = trimmed.indexOf("=");
24
+ if (eqIndex === -1) {
25
+ continue;
26
+ }
27
+ const key = trimmed.slice(0, eqIndex).trim();
28
+ if (key !== SELF_EVOLUTION_ENV_KEY) {
29
+ continue;
30
+ }
31
+ const value = trimmed.slice(eqIndex + 1).trim();
32
+ const parsed = parseBooleanLike(value);
33
+ if (parsed !== null) {
34
+ return parsed;
35
+ }
36
+ }
37
+ return false;
38
+ }
39
+ catch (error) {
40
+ if (error?.code !== "ENOENT") {
41
+ console.error(`[SELF_EVOLUTION] Failed to read ${SELF_EVOLUTION_ENV_FILE}:`, error);
42
+ }
43
+ return false;
44
+ }
45
+ }
46
+ }
47
+ export const selfEvolutionManager = new SelfEvolutionManager();
@@ -0,0 +1,14 @@
1
+ type RecordToolCallResult = {
2
+ count: number;
3
+ shouldNudge: boolean;
4
+ };
5
+ declare class ToolCallNudgeManager {
6
+ private readonly threshold;
7
+ private readonly sessions;
8
+ constructor(threshold?: number);
9
+ recordToolCall(sessionKey: string): RecordToolCallResult;
10
+ clearSession(sessionKey: string): void;
11
+ }
12
+ export declare const TOOL_CALL_NUDGE_THRESHOLD = 5;
13
+ export declare const toolCallNudgeManager: ToolCallNudgeManager;
14
+ export {};
@@ -0,0 +1,35 @@
1
+ const DEFAULT_TOOL_CALL_NUDGE_THRESHOLD = 5;
2
+ class ToolCallNudgeManager {
3
+ threshold;
4
+ sessions = new Map();
5
+ constructor(threshold = DEFAULT_TOOL_CALL_NUDGE_THRESHOLD) {
6
+ this.threshold = threshold;
7
+ }
8
+ recordToolCall(sessionKey) {
9
+ let state = this.sessions.get(sessionKey);
10
+ if (!state) {
11
+ state = {
12
+ count: 0,
13
+ nudged: false,
14
+ };
15
+ this.sessions.set(sessionKey, state);
16
+ }
17
+ state.count += 1;
18
+ if (!state.nudged && state.count >= this.threshold) {
19
+ state.nudged = true;
20
+ return {
21
+ count: state.count,
22
+ shouldNudge: true,
23
+ };
24
+ }
25
+ return {
26
+ count: state.count,
27
+ shouldNudge: false,
28
+ };
29
+ }
30
+ clearSession(sessionKey) {
31
+ this.sessions.delete(sessionKey);
32
+ }
33
+ }
34
+ export const TOOL_CALL_NUDGE_THRESHOLD = DEFAULT_TOOL_CALL_NUDGE_THRESHOLD;
35
+ export const toolCallNudgeManager = new ToolCallNudgeManager();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ynhcj/xiaoyi-channel",
3
- "version": "0.0.98-beta",
3
+ "version": "0.0.100-beta",
4
4
  "description": "OpenClaw Xiaoyi Channel plugin - Xiaoyi A2A protocol integration",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",