@ynhcj/xiaoyi-channel 0.0.99-beta → 0.0.101-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 +30 -0
- package/dist/src/channel.js +2 -1
- package/dist/src/provider.js +45 -1
- package/dist/src/tools/save-self-evolution-skill-tool.d.ts +1 -0
- package/dist/src/tools/save-self-evolution-skill-tool.js +189 -0
- package/dist/src/tools/session-manager.js +2 -0
- package/dist/src/utils/self-evolution-manager.d.ts +5 -0
- package/dist/src/utils/self-evolution-manager.js +47 -0
- package/dist/src/utils/tool-call-nudge-manager.d.ts +14 -0
- package/dist/src/utils/tool-call-nudge-manager.js +35 -0
- package/package.json +1 -1
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
|
}
|
package/dist/src/channel.js
CHANGED
|
@@ -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(", ")})`);
|
package/dist/src/provider.js
CHANGED
|
@@ -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;
|
|
@@ -236,6 +237,39 @@ const HEADER_SESSION_ID = "x-session-id";
|
|
|
236
237
|
const HEADER_INTERACTION_ID = "x-interaction-id";
|
|
237
238
|
/** Internal key for passing fallback uid prefix from prepareExtraParams to wrapStreamFn. */
|
|
238
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
|
+
}
|
|
239
273
|
/**
|
|
240
274
|
* Encode uid via SHA-256 and take first 32 hex chars.
|
|
241
275
|
*/
|
|
@@ -344,6 +378,7 @@ export const xiaoyiProvider = {
|
|
|
344
378
|
if (context.systemPrompt) {
|
|
345
379
|
console.log(`[xiaoyiprovider] system prompt length: ${context.systemPrompt.length}`);
|
|
346
380
|
}
|
|
381
|
+
const sessionCtx = getCurrentSessionContext();
|
|
347
382
|
// 在发送给模型前,优化 systemPrompt 结构
|
|
348
383
|
if (context.systemPrompt) {
|
|
349
384
|
let sp = context.systemPrompt;
|
|
@@ -373,8 +408,17 @@ export const xiaoyiProvider = {
|
|
|
373
408
|
console.log(`[xiaoyiprovider] system prompt optimized: ${beforeLen} -> ${sp.length}`);
|
|
374
409
|
context.systemPrompt = sp;
|
|
375
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");
|
|
376
421
|
// Append device context to systemPrompt
|
|
377
|
-
const sessionCtx = getCurrentSessionContext();
|
|
378
422
|
if (sessionCtx?.deviceType) {
|
|
379
423
|
const rawDevice = sessionCtx.deviceType;
|
|
380
424
|
const displayDevice = (rawDevice === "2in1") ? "鸿蒙PC" : rawDevice;
|
|
@@ -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,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();
|