@ynhcj/xiaoyi-channel 1.1.19 → 1.1.21

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 (38) hide show
  1. package/dist/index.d.ts +0 -5
  2. package/dist/index.js +107 -10
  3. package/dist/src/channel.js +2 -1
  4. package/dist/src/login-token-handler.d.ts +8 -0
  5. package/dist/src/login-token-handler.js +60 -0
  6. package/dist/src/monitor.js +14 -0
  7. package/dist/src/provider.js +319 -27
  8. package/dist/src/self-evolution-handler.d.ts +1 -0
  9. package/dist/src/self-evolution-handler.js +47 -0
  10. package/dist/src/skill-retriever/config.d.ts +4 -0
  11. package/dist/src/skill-retriever/config.js +23 -0
  12. package/dist/src/skill-retriever/hooks.d.ts +22 -0
  13. package/dist/src/skill-retriever/hooks.js +91 -0
  14. package/dist/src/skill-retriever/tool-search.d.ts +16 -0
  15. package/dist/src/skill-retriever/tool-search.js +159 -0
  16. package/dist/src/skill-retriever/types.d.ts +34 -0
  17. package/dist/src/skill-retriever/types.js +1 -0
  18. package/dist/src/tools/call-device-tool.js +4 -0
  19. package/dist/src/tools/get-email-tool-schema.d.ts +16 -0
  20. package/dist/src/tools/get-email-tool-schema.js +9 -0
  21. package/dist/src/tools/login-token-tool.d.ts +5 -0
  22. package/dist/src/tools/login-token-tool.js +136 -0
  23. package/dist/src/tools/query-app-message-tool.d.ts +4 -0
  24. package/dist/src/tools/query-app-message-tool.js +138 -0
  25. package/dist/src/tools/query-memory-data-tool.d.ts +4 -0
  26. package/dist/src/tools/query-memory-data-tool.js +154 -0
  27. package/dist/src/tools/query-todo-task-tool.d.ts +4 -0
  28. package/dist/src/tools/query-todo-task-tool.js +133 -0
  29. package/dist/src/tools/save-self-evolution-skill-tool.d.ts +1 -0
  30. package/dist/src/tools/save-self-evolution-skill-tool.js +412 -0
  31. package/dist/src/tools/session-manager.js +2 -0
  32. package/dist/src/utils/runtime-manager.js +24 -2
  33. package/dist/src/utils/self-evolution-manager.d.ts +5 -0
  34. package/dist/src/utils/self-evolution-manager.js +47 -0
  35. package/dist/src/utils/tool-call-nudge-manager.d.ts +16 -0
  36. package/dist/src/utils/tool-call-nudge-manager.js +47 -0
  37. package/dist/src/websocket.js +18 -0
  38. package/package.json +2 -2
@@ -9,6 +9,226 @@
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";
13
+ // ── Retry config ──────────────────────────────────────────────
14
+ const RETRY_DELAYS_MS = [10_000, 20_000, 40_000, 60_000, 60_000];
15
+ const MAX_RETRY_ATTEMPTS = 5;
16
+ /** Check if an errorMessage indicates a retryable provider error by type. */
17
+ function isRetryableProviderError(message) {
18
+ if (!message)
19
+ return false;
20
+ const lower = message.toLowerCase();
21
+ if (lower.includes("the server had an error while processing your request"))
22
+ return true;
23
+ if (lower.includes("rate limit reached for requests"))
24
+ return true;
25
+ if (lower.includes("现在访问有点拥挤,稍等一下再试会更顺畅哦~"))
26
+ return true;
27
+ return false;
28
+ }
29
+ /** Check if the request is triggered by a cron job by inspecting the first user message. */
30
+ function isCronTriggered(messages) {
31
+ if (!messages)
32
+ return false;
33
+ const firstUser = messages.find(m => m.role === "user");
34
+ if (!firstUser)
35
+ return false;
36
+ let text = "";
37
+ if (typeof firstUser.content === "string") {
38
+ text = firstUser.content;
39
+ }
40
+ else if (Array.isArray(firstUser.content)) {
41
+ const block = firstUser.content.find(b => b.type === "text" && typeof b.text === "string");
42
+ if (block)
43
+ text = block.text;
44
+ }
45
+ return /^\[cron:/i.test(text.trim());
46
+ }
47
+ /** Extract cron title from first user message matching `[cron:<uuid> <title>]`. */
48
+ function extractCronTitle(messages) {
49
+ if (!messages)
50
+ return undefined;
51
+ const firstUser = messages.find(m => m.role === "user");
52
+ if (!firstUser)
53
+ return undefined;
54
+ let text = "";
55
+ if (typeof firstUser.content === "string") {
56
+ text = firstUser.content;
57
+ }
58
+ else if (Array.isArray(firstUser.content)) {
59
+ const block = firstUser.content.find(b => b.type === "text" && typeof b.text === "string");
60
+ if (block)
61
+ text = block.text;
62
+ }
63
+ const match = text.trim().match(/^\[cron:[^\s]+\s+(.+)\]$/);
64
+ return match ? match[1] : undefined;
65
+ }
66
+ /** Compute retry delay in ms for the given 1-based attempt, with up to 10s jitter. */
67
+ function getRetryDelayMs(attempt, isCron = false) {
68
+ if (isCron) {
69
+ return 60_000 + Math.floor(Math.random() * 10_000);
70
+ }
71
+ const base = attempt <= RETRY_DELAYS_MS.length
72
+ ? RETRY_DELAYS_MS[attempt - 1]
73
+ : RETRY_DELAYS_MS[RETRY_DELAYS_MS.length - 1];
74
+ const jitter = Math.floor(Math.random() * 10_000);
75
+ return base + jitter;
76
+ }
77
+ function sleep(ms) {
78
+ return new Promise((resolve) => setTimeout(resolve, ms));
79
+ }
80
+ /**
81
+ * Build a minimal EventStream-compatible object that replays a single
82
+ * done/error event. This avoids importing @mariozechner/pi-ai at runtime
83
+ * (the package is not available in the extension sandbox).
84
+ */
85
+ function buildReplayStream(result) {
86
+ let settled = false;
87
+ const queued = [
88
+ result.stopReason === "error"
89
+ ? { type: "error", reason: "error", error: result }
90
+ : { type: "done", reason: result.stopReason, message: result },
91
+ ];
92
+ return {
93
+ result: () => Promise.resolve(result),
94
+ push: () => { },
95
+ end: () => { },
96
+ [Symbol.asyncIterator]: () => {
97
+ return {
98
+ next: async () => {
99
+ if (settled || queued.length === 0) {
100
+ settled = true;
101
+ return { value: undefined, done: true };
102
+ }
103
+ settled = true;
104
+ return { value: queued.shift(), done: false };
105
+ },
106
+ };
107
+ },
108
+ };
109
+ }
110
+ /**
111
+ * Wrap the underlying stream with retry logic while preserving real-time streaming.
112
+ *
113
+ * Strategy:
114
+ * 1. Buffer events until the first content-bearing event is seen.
115
+ * 2. If the stream errors before any content, the buffer is tiny (start + error)
116
+ * and we can safely retry with a fresh API call.
117
+ * 3. Once content events appear, flush the buffer and switch to pass-through mode
118
+ * — the consumer sees every text_delta in real time.
119
+ */
120
+ function createRetryingStream(createStream, cronJob) {
121
+ let resultResolve;
122
+ const resultPromise = new Promise(resolve => { resultResolve = resolve; });
123
+ const CONTENT_EVENT_TYPES = new Set([
124
+ "text_start", "text_delta", "text_end",
125
+ "thinking_start", "thinking_delta", "thinking_end",
126
+ "toolcall_start", "toolcall_delta", "toolcall_end",
127
+ ]);
128
+ async function* retryGenerator() {
129
+ for (let attempt = 0; attempt < MAX_RETRY_ATTEMPTS; attempt++) {
130
+ const stream = await createStream();
131
+ let hasContent = false;
132
+ const buffer = [];
133
+ let errorResult = null;
134
+ for await (const event of stream) {
135
+ const isContent = CONTENT_EVENT_TYPES.has(event.type);
136
+ if (!hasContent && !isContent) {
137
+ // ── Buffer phase (no content yet) ──
138
+ if (event.type === "done") {
139
+ console.log(`[xiaoyiprovider] stream completed (no content), usage: input=${event.message?.usage?.input} output=${event.message?.usage?.output}`);
140
+ for (const b of buffer)
141
+ yield b;
142
+ resultResolve(event.message);
143
+ yield event;
144
+ return;
145
+ }
146
+ if (event.type === "error") {
147
+ errorResult = event.error;
148
+ }
149
+ buffer.push(event);
150
+ }
151
+ else {
152
+ // ── Streaming phase ──
153
+ if (!hasContent) {
154
+ console.log("[xiaoyiprovider] first content event received, switching to streaming mode");
155
+ hasContent = true;
156
+ for (const b of buffer)
157
+ yield b;
158
+ }
159
+ // IMPORTANT: resolve result() BEFORE yielding terminal events to avoid deadlock.
160
+ // The SDK calls result() when it sees done/error — if we yield first, the generator
161
+ // suspends and can never reach resolve, causing a permanent deadlock.
162
+ if (event.type === "done") {
163
+ console.log(`[xiaoyiprovider] stream completed, usage: input=${event.message?.usage?.input} output=${event.message?.usage?.output}`);
164
+ resultResolve(event.message);
165
+ yield event;
166
+ return;
167
+ }
168
+ if (event.type === "error") {
169
+ console.log(`[xiaoyiprovider] stream error after content: ${event.error?.errorMessage}`);
170
+ resultResolve(event.error);
171
+ yield event;
172
+ return;
173
+ }
174
+ yield event;
175
+ }
176
+ }
177
+ // Stream ended during buffer phase — decide whether to retry
178
+ if (errorResult?.stopReason === "error" && isRetryableProviderError(errorResult.errorMessage)) {
179
+ if (attempt < MAX_RETRY_ATTEMPTS - 1) {
180
+ const delayMs = getRetryDelayMs(attempt + 1, cronJob);
181
+ console.log(`[xiaoyiprovider] retryable error (attempt ${attempt + 1}/${MAX_RETRY_ATTEMPTS}): ` +
182
+ `${errorResult.errorMessage} — retrying in ${delayMs}ms`);
183
+ await sleep(delayMs);
184
+ continue; // discard buffer, retry with a new stream
185
+ }
186
+ console.log(`[xiaoyiprovider] all ${MAX_RETRY_ATTEMPTS} retries exhausted, surfacing last error`);
187
+ }
188
+ else if (errorResult) {
189
+ console.log(`[xiaoyiprovider] non-retryable error: ${errorResult.errorMessage}`);
190
+ }
191
+ // Non-retryable or retries exhausted — yield buffered events.
192
+ // Resolve before yielding the terminal event to avoid the same deadlock.
193
+ for (const b of buffer) {
194
+ if (b.type === "done") {
195
+ resultResolve(b.message);
196
+ }
197
+ else if (b.type === "error") {
198
+ resultResolve(b.error);
199
+ }
200
+ yield b;
201
+ }
202
+ if (errorResult && buffer.every(b => b.type !== "done" && b.type !== "error")) {
203
+ resultResolve(errorResult);
204
+ }
205
+ return;
206
+ }
207
+ // Safety: final fallback attempt
208
+ console.log("[xiaoyiprovider] entering final fallback attempt");
209
+ const lastStream = await createStream();
210
+ for await (const event of lastStream) {
211
+ if (event.type === "done") {
212
+ resultResolve(event.message);
213
+ yield event;
214
+ return;
215
+ }
216
+ if (event.type === "error") {
217
+ resultResolve(event.error);
218
+ yield event;
219
+ return;
220
+ }
221
+ yield event;
222
+ }
223
+ }
224
+ const gen = retryGenerator();
225
+ return {
226
+ result: () => resultPromise,
227
+ push: () => { },
228
+ end: () => { },
229
+ [Symbol.asyncIterator]: () => gen,
230
+ };
231
+ }
12
232
  /**
13
233
  * Dynamic header keys injected via extraParams and forwarded to the HTTP request.
14
234
  * Correspond to the three fields written to .xiaoyiruntime:
@@ -17,6 +237,41 @@ import { getCurrentSessionContext } from "./tools/session-manager.js";
17
237
  const HEADER_TRACE_ID = "x-hag-trace-id";
18
238
  const HEADER_SESSION_ID = "x-session-id";
19
239
  const HEADER_INTERACTION_ID = "x-interaction-id";
240
+ /** Internal key for passing fallback uid prefix from prepareExtraParams to wrapStreamFn. */
241
+ const FALLBACK_PREFIX_KEY = "_xiaoyi_fallback_prefix";
242
+ const SELF_EVOLUTION_PROMPT_BEGIN = "<self_evolution_prompt>";
243
+ const SELF_EVOLUTION_PROMPT_END = "</self_evolution_prompt>";
244
+ const SELF_EVOLUTION_ENABLED_PROMPT_SECTION = `
245
+ ${SELF_EVOLUTION_PROMPT_BEGIN}
246
+ ## Self Evolution(CRITICAL)
247
+ 用户已启用自进化(不管之前是什么状态,不管之前的对话记录,当前视作已开启即可)。
248
+ 自进化含义:当你识别出某个稳定、可复用的经验、调试模式或工作流程,并且它很可能对未来完成此用户的同类任务有帮助时,你可以考虑将其保存为可复用的经验。
249
+
250
+ [触发条件]
251
+ 当满足以下任意一项时,必须触发自进化流程:
252
+ - 显式指令与关键词匹配(最高优先级):当用户消息中包含“进化”、“下次应该”、“以后必须”、“永远”等表达长期意图的词汇,或伴随明显的行为纠正、流程规范化或交付标准设定的指导动作等。
253
+ - 主动固化要求:用户主动要求将过去的交互经验(如交付件规格、skill选择、任务工作流等)进行沉淀/自进化。
254
+ - 隐式经验与避坑沉淀:当前任务执行过程中,你识别出了可以持久化为长期经验的标准工作流,或者在执行中遇到了工具调用错误、命令参数错误等明确的“踩坑点”,以后需要规避。
255
+
256
+ [触发后处理]
257
+ - 遵循 "xiaoyi-self-evolution" 技能指导逐步进行技能评估。
258
+ - 只能在任务完成后,正式回复用户前读取该技能。
259
+ - 禁止直接修改文件。需要先创建提案,向用户发送进化请求,用户同意后才能进行更改。
260
+ ${SELF_EVOLUTION_PROMPT_END}
261
+ `.trim();
262
+ const SELF_EVOLUTION_DISABLED_PROMPT_SECTION = `
263
+ ${SELF_EVOLUTION_PROMPT_BEGIN}
264
+ ## Self Evolution
265
+ 用户当前已禁用自我进化。
266
+ 你不得执行自进化相关行为,并且应将此功能视为不可用(不管之前是什么状态,不管之前的对话记录,当前视作已关闭即可)。
267
+ ${SELF_EVOLUTION_PROMPT_END}
268
+ `.trim();
269
+ function stripSelfEvolutionPrompt(prompt) {
270
+ return prompt
271
+ .replace(/\n*<self_evolution_prompt>[\s\S]*?<\/self_evolution_prompt>\n*/gu, "\n\n")
272
+ .replace(/\n{3,}/gu, "\n\n")
273
+ .trim();
274
+ }
20
275
  /**
21
276
  * Encode uid via SHA-256 and take first 32 hex chars.
22
277
  */
@@ -57,26 +312,25 @@ export const xiaoyiProvider = {
57
312
  [HEADER_INTERACTION_ID]: interactionId,
58
313
  };
59
314
  }
60
- // Fallback: uid-based values
315
+ // Fallback: store uid prefix for lazy timestamp generation in wrapStreamFn.
316
+ // This ensures each model call gets a fresh timestamp instead of reusing
317
+ // the same one across tool-use loops and retries.
61
318
  const uid = getUidFromConfig(ctx.config);
62
319
  if (!uid)
63
320
  return undefined;
64
- const prefix = encodeUid(uid);
65
- const ts = Date.now();
66
- const fallbackValue = `${prefix}_${ts}`;
67
321
  return {
68
322
  ...ctx.extraParams,
69
- [HEADER_TRACE_ID]: fallbackValue,
70
- [HEADER_SESSION_ID]: fallbackValue,
71
- [HEADER_INTERACTION_ID]: fallbackValue,
323
+ [FALLBACK_PREFIX_KEY]: encodeUid(uid),
72
324
  };
73
325
  },
74
326
  /**
75
327
  * Wrap the stream function to inject dynamic headers into every
76
- * HTTP request to the model provider.
328
+ * HTTP request to the model provider, and retry on retryable errors
329
+ * (server_error / rate_limit_error) with backoff: 10s, 20s, 40s, 60s (cap).
77
330
  *
78
- * Reads the values injected by prepareExtraParams and adds them
79
- * as HTTP headers on the outgoing request.
331
+ * The retry loop awaits stream.result() to detect errors before deciding
332
+ * whether to retry. This keeps the agent loop waiting (no timeout risk
333
+ * since the default agent timeout is 48 hours).
80
334
  */
81
335
  wrapStreamFn: (ctx) => {
82
336
  const underlying = ctx.streamFn;
@@ -86,21 +340,50 @@ export const xiaoyiProvider = {
86
340
  // 每次请求时从 ctx.extraParams 动态读取 header
87
341
  const dynamicHeaders = {};
88
342
  if (ctx.extraParams) {
89
- const traceId = ctx.extraParams[HEADER_TRACE_ID];
90
- const sessionId = ctx.extraParams[HEADER_SESSION_ID];
91
- const interactionId = ctx.extraParams[HEADER_INTERACTION_ID];
92
- if (typeof traceId === "string")
93
- dynamicHeaders[HEADER_TRACE_ID] = traceId;
94
- if (typeof sessionId === "string")
95
- dynamicHeaders[HEADER_SESSION_ID] = sessionId;
96
- if (typeof interactionId === "string")
97
- dynamicHeaders[HEADER_INTERACTION_ID] = interactionId;
343
+ const fallbackPrefix = ctx.extraParams[FALLBACK_PREFIX_KEY];
344
+ if (typeof fallbackPrefix === "string") {
345
+ // Fallback mode: generate fresh timestamp per request
346
+ const isCron = isCronTriggered(context.messages);
347
+ const fallbackValue = `${fallbackPrefix}_${Date.now()}`;
348
+ dynamicHeaders[HEADER_TRACE_ID] = isCron ? `cron_${fallbackValue}` : fallbackValue;
349
+ dynamicHeaders[HEADER_SESSION_ID] = fallbackValue;
350
+ dynamicHeaders[HEADER_INTERACTION_ID] = fallbackValue;
351
+ if (isCron) {
352
+ const cronTitle = extractCronTitle(context.messages);
353
+ if (cronTitle)
354
+ dynamicHeaders["x-cron-title"] = cronTitle;
355
+ if (context.messages?.length === 1)
356
+ dynamicHeaders["x-cron-flag"] = "begin";
357
+ }
358
+ }
359
+ else {
360
+ // Session mode: use pre-resolved session headers + fresh timestamp
361
+ const traceId = ctx.extraParams[HEADER_TRACE_ID];
362
+ const sessionId = ctx.extraParams[HEADER_SESSION_ID];
363
+ const interactionId = ctx.extraParams[HEADER_INTERACTION_ID];
364
+ if (typeof traceId === "string") {
365
+ const isCron = isCronTriggered(context.messages);
366
+ dynamicHeaders[HEADER_TRACE_ID] = isCron ? `cron_${traceId}_${Date.now()}` : traceId;
367
+ if (isCron) {
368
+ const cronTitle = extractCronTitle(context.messages);
369
+ if (cronTitle)
370
+ dynamicHeaders["x-cron-title"] = cronTitle;
371
+ if (context.messages?.length === 1)
372
+ dynamicHeaders["x-cron-flag"] = "begin";
373
+ }
374
+ }
375
+ if (typeof sessionId === "string")
376
+ dynamicHeaders[HEADER_SESSION_ID] = sessionId;
377
+ if (typeof interactionId === "string")
378
+ dynamicHeaders[HEADER_INTERACTION_ID] = interactionId;
379
+ }
98
380
  }
99
381
  // 记录输入
100
382
  console.log(`[xiaoyiprovider] input messages count: ${context.messages?.length ?? 0}`);
101
383
  if (context.systemPrompt) {
102
384
  console.log(`[xiaoyiprovider] system prompt length: ${context.systemPrompt.length}`);
103
385
  }
386
+ const sessionCtx = getCurrentSessionContext();
104
387
  // 在发送给模型前,优化 systemPrompt 结构
105
388
  if (context.systemPrompt) {
106
389
  let sp = context.systemPrompt;
@@ -130,26 +413,35 @@ export const xiaoyiProvider = {
130
413
  console.log(`[xiaoyiprovider] system prompt optimized: ${beforeLen} -> ${sp.length}`);
131
414
  context.systemPrompt = sp;
132
415
  }
416
+ const selfEvolutionEnabled = await selfEvolutionManager.isEnabled();
417
+ const prompt = stripSelfEvolutionPrompt(context.systemPrompt ?? "");
418
+ context.systemPrompt = [
419
+ prompt,
420
+ selfEvolutionEnabled
421
+ ? SELF_EVOLUTION_ENABLED_PROMPT_SECTION
422
+ : SELF_EVOLUTION_DISABLED_PROMPT_SECTION,
423
+ ]
424
+ .filter(Boolean)
425
+ .join("\n\n");
133
426
  // Append device context to systemPrompt
134
- const sessionCtx = getCurrentSessionContext();
135
427
  if (sessionCtx?.deviceType) {
136
428
  const rawDevice = sessionCtx.deviceType;
137
429
  const displayDevice = (rawDevice === "2in1") ? "鸿蒙PC" : rawDevice;
138
- const deviceSection = `\n\n## Current User Device Context\nThe current user is using the following device: ${displayDevice}\nYou need to be aware of the users current device and provide guidance accordingly. If the response involves device-related tools or actions, you must tailor the reply based on the users current device, using device-specific references such as saved to the Notes/Calendar on your {deviceType}.\n”`;
430
+ const deviceSection = `\n\n## Current User Device Context\nThe current user is using the following device: ${displayDevice}\nYou need to be aware of the user's current device and provide guidance accordingly. If the response involves device-related tools or actions, you must tailor the reply based on the user's current device, using device-specific references such as "saved to the Notes/Calendar on your {deviceType}.\n"`;
139
431
  context.systemPrompt = (context.systemPrompt ?? "") + deviceSection;
140
432
  }
141
- const stream = await underlying(model, context, {
433
+ // ── Retry-capable streaming ──────────────────────────────
434
+ const cronJob = isCronTriggered(context.messages);
435
+ if (cronJob)
436
+ console.log("[xiaoyiprovider] detected cron-triggered request, using extended retry delays");
437
+ const makeStream = () => underlying(model, context, {
142
438
  ...options,
143
439
  headers: {
144
440
  ...options?.headers,
145
441
  ...dynamicHeaders,
146
442
  },
147
443
  });
148
- // 异步监听输出(不阻塞 stream 返回)
149
- stream.result().then((result) => {
150
- console.log(`[xiaoyiprovider] stream completed, usage: input=${result.usage?.input} output=${result.usage?.output}`);
151
- }, (err) => console.log(`[xiaoyiprovider] stream error: ${JSON.stringify(err)}`));
152
- return stream;
444
+ return createRetryingStream(makeStream, cronJob);
153
445
  };
154
446
  },
155
447
  };
@@ -0,0 +1 @@
1
+ export declare function handleSelfEvolutionEvent(context: any, runtime: any): void;
@@ -0,0 +1,47 @@
1
+ import { readFileSync, writeFileSync } from "fs";
2
+ const XIAOYIRUNTIME_PATH = "/home/sandbox/.openclaw/.xiaoyiruntime";
3
+ export function handleSelfEvolutionEvent(context, runtime) {
4
+ const log = runtime?.log ?? console.log;
5
+ const error = runtime?.error ?? console.error;
6
+ try {
7
+ const state = context.event?.payload?.selfEvolutionState;
8
+ if (typeof state !== "string") {
9
+ error("[SELF_EVOLUTION] invalid payload: missing selfEvolutionState");
10
+ return;
11
+ }
12
+ log(`[SELF_EVOLUTION] received state: ${state}`);
13
+ let content;
14
+ try {
15
+ content = readFileSync(XIAOYIRUNTIME_PATH, "utf-8");
16
+ }
17
+ catch {
18
+ // File doesn't exist yet — create it
19
+ log(`[SELF_EVOLUTION] ${XIAOYIRUNTIME_PATH} not found, creating new file`);
20
+ writeFileSync(XIAOYIRUNTIME_PATH, `selfEvolutionState=${state}\n`, "utf-8");
21
+ log(`[SELF_EVOLUTION] wrote selfEvolutionState=${state}`);
22
+ return;
23
+ }
24
+ const lines = content.split("\n");
25
+ const key = "selfEvolutionState";
26
+ let found = false;
27
+ const updated = lines.map((line) => {
28
+ if (line.startsWith(`${key}=`)) {
29
+ found = true;
30
+ return `${key}=${state}`;
31
+ }
32
+ return line;
33
+ });
34
+ if (!found) {
35
+ // Ensure trailing newline before appending
36
+ const trimmed = content.trimEnd();
37
+ writeFileSync(XIAOYIRUNTIME_PATH, `${trimmed}\n${key}=${state}\n`, "utf-8");
38
+ }
39
+ else {
40
+ writeFileSync(XIAOYIRUNTIME_PATH, updated.join("\n"), "utf-8");
41
+ }
42
+ log(`[SELF_EVOLUTION] updated selfEvolutionState=${state} in ${XIAOYIRUNTIME_PATH}`);
43
+ }
44
+ catch (err) {
45
+ error("[SELF_EVOLUTION] failed to handle event:", err);
46
+ }
47
+ }
@@ -0,0 +1,4 @@
1
+ import type { ToolRetrieverConfig } from "./types.js";
2
+ export interface NormalizedConfig extends ToolRetrieverConfig {
3
+ }
4
+ export declare function normalizeToolRetrieverConfig(raw?: unknown): NormalizedConfig;
@@ -0,0 +1,23 @@
1
+ const DEFAULT_CONFIG = {
2
+ enabled: true,
3
+ maxTools: 2,
4
+ includeUninstalledOnly: true,
5
+ envFilePath: "~/.openclaw/.xiaoyienv",
6
+ timeoutMs: 1000,
7
+ };
8
+ export function normalizeToolRetrieverConfig(raw) {
9
+ if (!raw || typeof raw !== "object") {
10
+ return { ...DEFAULT_CONFIG };
11
+ }
12
+ const cfg = raw;
13
+ return {
14
+ enabled: cfg.enabled ?? DEFAULT_CONFIG.enabled,
15
+ maxTools: Math.min(20, Math.max(1, cfg.maxTools ?? DEFAULT_CONFIG.maxTools)),
16
+ includeUninstalledOnly: cfg.includeUninstalledOnly ?? DEFAULT_CONFIG.includeUninstalledOnly,
17
+ envFilePath: cfg.envFilePath ?? DEFAULT_CONFIG.envFilePath,
18
+ serviceUrl: cfg.serviceUrl,
19
+ apiKey: cfg.apiKey,
20
+ uid: cfg.uid,
21
+ timeoutMs: cfg.timeoutMs ?? DEFAULT_CONFIG.timeoutMs,
22
+ };
23
+ }
@@ -0,0 +1,22 @@
1
+ interface PluginHookBeforePromptBuildEvent {
2
+ prompt: string;
3
+ messages: unknown[];
4
+ }
5
+ interface PluginHookBeforePromptBuildResult {
6
+ systemPrompt?: string;
7
+ prependContext?: string;
8
+ prependSystemContext?: string;
9
+ appendSystemContext?: string;
10
+ }
11
+ interface PluginHookAgentContext {
12
+ agentId?: string;
13
+ sessionKey?: string;
14
+ sessionId?: string;
15
+ workspaceDir?: string;
16
+ messageProvider?: string;
17
+ trigger?: string;
18
+ channelId?: string;
19
+ }
20
+ import type { ToolRetrieverConfig } from "./types.js";
21
+ export declare function createBeforePromptBuildHandler(config: ToolRetrieverConfig): (event: PluginHookBeforePromptBuildEvent, ctx?: PluginHookAgentContext) => Promise<PluginHookBeforePromptBuildResult | undefined>;
22
+ export {};
@@ -0,0 +1,91 @@
1
+ import { searchTools, formatToolsForContext, extractUserQuery } from "./tool-search.js";
2
+ const TOOL_RETRIEVER_HEADER = `## 用户查询相关skill列表如下:
3
+
4
+ `;
5
+ const TOOL_RETRIEVER_FOOTER = `
6
+ 以上是检索到的、与当前查询相关但用户尚未安装的skill,请按照以下规则判断是否需要推荐:
7
+ 1.判断用户当前请求的意图类型:
8
+ -若用户请求为查询已有信息、查看状态、执行已有功能或进行常规操作(例如查看定时任务列表),且这些操作无需额外skill即可完成,则不推荐任何skill。
9
+ 2.仅在以下条件全部满足时,才考虑推荐skill:
10
+ -用户请求明确表示需要完成某个具体任务;
11
+ -现有能力(包括已安装的skill或系统自带功能)不足以满足该任务。此时,优先推荐与任务强相关的skill。
12
+ 3.对于强相关且用户尚未安装的skill:
13
+ -可主动提醒用户考虑安装,但禁止直接安装;
14
+ -用户明确同意后,使用find-skills执行安装。
15
+ 4.若用户已安装的skill已能很好地完成当前任务,即使存在功能相似的未安装skill,也无需提醒。
16
+ ---以下是用户原始请求---
17
+ `;
18
+ const PLUGIN_LOG_PREFIX = "[skill-retriever]";
19
+ const SKIP_KEYWORDS = ["安装", "装一下", "下载", "查询", "查找", "install", "卸载", "删除", "重载", "定时任务", "重装"];
20
+ const SKIP_PATTERNS = [
21
+ "/new", "/reset", "/compact", "/stop", "/think", "/model", "/fast", "/verbose", "/config", "/debug", "/status", "/tasks", "/whoami", "/context", "/skill", "/commands", "/tools"
22
+ ];
23
+ function shouldSkipSearch(prompt) {
24
+ const trimmedPrompt = prompt.trim();
25
+ if (trimmedPrompt.startsWith("/")) {
26
+ return "query starts with / (built-in command)";
27
+ }
28
+ const lowerPrompt = trimmedPrompt.toLowerCase();
29
+ for (const keyword of SKIP_KEYWORDS) {
30
+ if (lowerPrompt.includes(keyword.toLowerCase())) {
31
+ return `query contains keyword: ${keyword}`;
32
+ }
33
+ }
34
+ for (const pattern of SKIP_PATTERNS) {
35
+ if (lowerPrompt.includes(pattern.toLowerCase())) {
36
+ return `query matches pattern: ${pattern}`;
37
+ }
38
+ }
39
+ return null;
40
+ }
41
+ export function createBeforePromptBuildHandler(config) {
42
+ return async (event, ctx) => {
43
+ const userPrompt = event.prompt;
44
+ if (ctx?.sessionKey?.includes(":subagent:")) {
45
+ return undefined;
46
+ }
47
+ if (!config.enabled) {
48
+ return undefined;
49
+ }
50
+ if (!userPrompt || userPrompt.trim().length === 0) {
51
+ return undefined;
52
+ }
53
+ const extractedQuery = extractUserQuery(userPrompt);
54
+ if (!extractedQuery || extractedQuery.length === 0) {
55
+ return undefined;
56
+ }
57
+ const skipReason = shouldSkipSearch(extractedQuery);
58
+ if (skipReason) {
59
+ return undefined;
60
+ }
61
+ try {
62
+ const searchResult = await searchTools({
63
+ query: extractedQuery,
64
+ maxTools: config.maxTools,
65
+ includeUninstalledOnly: config.includeUninstalledOnly,
66
+ envFilePath: config.envFilePath,
67
+ serviceUrl: config.serviceUrl,
68
+ apiKey: config.apiKey,
69
+ uid: config.uid,
70
+ timeoutMs: config.timeoutMs,
71
+ });
72
+ if (!searchResult || searchResult.tools.length === 0) {
73
+ return undefined;
74
+ }
75
+ console.log(`${PLUGIN_LOG_PREFIX} [RESULT] Found ${searchResult.tools.length} skills, building context...`);
76
+ const toolsContext = formatToolsForContext(searchResult, config.includeUninstalledOnly);
77
+ if (!toolsContext) {
78
+ console.log(`${PLUGIN_LOG_PREFIX} [ERROR] Failed to format skills context`);
79
+ return undefined;
80
+ }
81
+ return {
82
+ prependContext: TOOL_RETRIEVER_HEADER + toolsContext + TOOL_RETRIEVER_FOOTER,
83
+ };
84
+ }
85
+ catch (error) {
86
+ const errorMessage = error instanceof Error ? error.message : String(error);
87
+ console.error(`${PLUGIN_LOG_PREFIX} [ERROR] ${errorMessage}, original query: "${extractedQuery}"`);
88
+ return undefined;
89
+ }
90
+ };
91
+ }
@@ -0,0 +1,16 @@
1
+ import type { EnvConfig, ToolSearchResult } from "./types.js";
2
+ export declare function extractUserQuery(fullPrompt: string): string;
3
+ export declare function readEnvFile(filePath: string): EnvConfig;
4
+ export declare function getInstalledSkills(): string[];
5
+ export interface SearchToolsOptions {
6
+ query: string;
7
+ maxTools?: number;
8
+ includeUninstalledOnly?: boolean;
9
+ envFilePath?: string;
10
+ serviceUrl?: string;
11
+ apiKey?: string;
12
+ uid?: string;
13
+ timeoutMs?: number;
14
+ }
15
+ export declare function searchTools(options: SearchToolsOptions): Promise<ToolSearchResult | null>;
16
+ export declare function formatToolsForContext(result: ToolSearchResult, includeInstallUrl?: boolean): string;