完善 skill_manager 的技能维护能力
This commit is contained in:
@@ -38,7 +38,13 @@ const reviewResultSchema = z.object({
|
||||
skills: z
|
||||
.array(
|
||||
z.object({
|
||||
action: z.enum(["append_pattern", "remove_pattern", "write_reference"]),
|
||||
action: z.enum([
|
||||
"append_pattern",
|
||||
"remove_pattern",
|
||||
"write_reference",
|
||||
"write_skill",
|
||||
"remove_skill",
|
||||
]),
|
||||
confidence: z.number().min(0).max(1),
|
||||
content: z.string().optional(),
|
||||
evidence: z.string().default(""),
|
||||
@@ -409,11 +415,15 @@ export class LearningOrchestrator {
|
||||
proposal.skill_path,
|
||||
proposal.target_id ?? "",
|
||||
)
|
||||
: await this.skillStore.writeReference(
|
||||
proposal.skill_path,
|
||||
proposal.file_path ?? "",
|
||||
proposal.content ?? "",
|
||||
);
|
||||
: proposal.action === "write_reference"
|
||||
? await this.skillStore.writeReference(
|
||||
proposal.skill_path,
|
||||
proposal.file_path ?? "",
|
||||
proposal.content ?? "",
|
||||
)
|
||||
: proposal.action === "write_skill"
|
||||
? await this.skillStore.writeSkill(proposal.skill_path, proposal.content ?? "")
|
||||
: await this.skillStore.removeSkill(proposal.skill_path);
|
||||
await writeLearningAuditLog({
|
||||
action: `skill-${proposal.action}`,
|
||||
detail: sanitizeAuditDetail(result.detail),
|
||||
@@ -501,10 +511,11 @@ const buildReviewPrompt = ({
|
||||
"- Save only reusable workflows, methods, or pitfalls that will help in future similar tasks.",
|
||||
"- Prefer append_pattern for concise reusable lessons.",
|
||||
"- Use write_reference only for compact durable supporting notes under references/*.md.",
|
||||
"- Do not edit frontmatter or arbitrary sections.",
|
||||
"- Use write_skill only when the conversation establishes a complete reusable SKILL.md with frontmatter name and description; it creates or overwrites the main SKILL.md.",
|
||||
"- Use remove_skill only when the conversation clearly establishes the whole skill is obsolete or invalid.",
|
||||
"",
|
||||
"Output JSON schema:",
|
||||
`{"summary":"string","memories":[{"action":"add|replace|remove","scope":"user|workspace","content":"string?","target_id":"string?","confidence":0.0,"evidence":"string"}],"skills":[{"action":"append_pattern|remove_pattern|write_reference","skill_path":"string","pattern":"string?","target_id":"string?","file_path":"references/example.md?","content":"string?","confidence":0.0,"evidence":"string"}]}`,
|
||||
`{"summary":"string","memories":[{"action":"add|replace|remove","scope":"user|workspace","content":"string?","target_id":"string?","confidence":0.0,"evidence":"string"}],"skills":[{"action":"append_pattern|remove_pattern|write_reference|write_skill|remove_skill","skill_path":"string","pattern":"string?","target_id":"string?","file_path":"references/example.md?","content":"string?","confidence":0.0,"evidence":"string"}]}`,
|
||||
"",
|
||||
"If nothing should be saved, return empty arrays.",
|
||||
"",
|
||||
|
||||
+87
-5
@@ -18,6 +18,7 @@ import {
|
||||
} from "../utils/persistencePolicy.js";
|
||||
|
||||
const LEARNED_PATTERNS_MARKER = "## Learned Patterns";
|
||||
const ROOT_SKILL_ALIAS = "__root__";
|
||||
const PROJECT_ROOT_DIR = resolve(dirname(fileURLToPath(import.meta.url)), "../..");
|
||||
const resolveProjectPath = (path: string) =>
|
||||
isAbsolute(path) ? path : resolve(PROJECT_ROOT_DIR, path);
|
||||
@@ -45,7 +46,8 @@ export class SkillStore {
|
||||
return null;
|
||||
}
|
||||
const target = this.skillFilePath(normalizedSkillPath);
|
||||
const current = (await readTextFile(target)) ?? defaultLearnedSkill(normalizedSkillPath);
|
||||
const current =
|
||||
(await readTextFile(target)) ?? defaultSkillDocument(normalizedSkillPath);
|
||||
return {
|
||||
references: await this.listReferenceFiles(normalizedSkillPath),
|
||||
scripts: await this.listScriptFiles(normalizedSkillPath),
|
||||
@@ -66,7 +68,8 @@ export class SkillStore {
|
||||
}
|
||||
return this.serializeWrite(async () => {
|
||||
const target = this.skillFilePath(normalizedSkillPath);
|
||||
const current = (await readTextFile(target)) ?? defaultLearnedSkill(normalizedSkillPath);
|
||||
const current =
|
||||
(await readTextFile(target)) ?? defaultSkillDocument(normalizedSkillPath);
|
||||
const existingPatterns = extractLearnedPatterns(current);
|
||||
if (existingPatterns.some((entry) => entry.content === sanitizedPattern)) {
|
||||
return { changed: false, detail: "pattern already existed", target };
|
||||
@@ -90,6 +93,49 @@ export class SkillStore {
|
||||
});
|
||||
}
|
||||
|
||||
async writeSkill(skillPath: string, content: string) {
|
||||
const normalizedSkillPath = normalizeSkillPath(skillPath);
|
||||
if (!normalizedSkillPath) {
|
||||
return { changed: false, detail: "invalid skill_path", target: "" };
|
||||
}
|
||||
const sanitizedContent = sanitizePersistentDocument(content, 12000);
|
||||
if (!sanitizedContent) {
|
||||
return { changed: false, detail: "skill content rejected by persistence policy", target: "" };
|
||||
}
|
||||
if (!hasValidSkillFrontmatter(sanitizedContent)) {
|
||||
return {
|
||||
changed: false,
|
||||
detail: "skill content rejected: expected SKILL.md frontmatter with name and description",
|
||||
target: "",
|
||||
};
|
||||
}
|
||||
return this.serializeWrite(async () => {
|
||||
const target = this.skillFilePath(normalizedSkillPath);
|
||||
await ensureDirectory(this.skillDirPath(normalizedSkillPath));
|
||||
await atomicWriteFileWithHistory(target, `${sanitizedContent}\n`, {
|
||||
backupDir: this.backupDir,
|
||||
rootDir: this.rootDir,
|
||||
});
|
||||
return { changed: true, detail: "skill written", target };
|
||||
});
|
||||
}
|
||||
|
||||
async removeSkill(skillPath: string) {
|
||||
const normalizedSkillPath = normalizeSkillPath(skillPath);
|
||||
if (!normalizedSkillPath) {
|
||||
return { changed: false, detail: "invalid skill_path", target: "" };
|
||||
}
|
||||
return this.serializeWrite(async () => {
|
||||
const target = this.skillFilePath(normalizedSkillPath);
|
||||
const previous = await readTextFile(target);
|
||||
if (previous === null) {
|
||||
return { changed: false, detail: "skill file not found", target };
|
||||
}
|
||||
await removeFileIfExists(target);
|
||||
return { changed: true, detail: "skill removed", target };
|
||||
});
|
||||
}
|
||||
|
||||
async removePattern(skillPath: string, targetId: string) {
|
||||
const normalizedSkillPath = normalizeSkillPath(skillPath);
|
||||
if (!normalizedSkillPath) {
|
||||
@@ -205,18 +251,28 @@ export class SkillStore {
|
||||
|
||||
private async listReferenceFiles(skillPath: string) {
|
||||
const referenceDir = join(this.rootDir, skillPath, "references");
|
||||
if (skillPath === ROOT_SKILL_ALIAS) {
|
||||
return [];
|
||||
}
|
||||
const files = await listFiles(referenceDir);
|
||||
return files.map((file) => file.slice(referenceDir.length + 1));
|
||||
}
|
||||
|
||||
private async listScriptFiles(skillPath: string) {
|
||||
if (skillPath === ROOT_SKILL_ALIAS) {
|
||||
return [];
|
||||
}
|
||||
const scriptDir = join(this.rootDir, skillPath, "scripts");
|
||||
const files = await listFiles(scriptDir);
|
||||
return files.map((file) => file.slice(scriptDir.length + 1));
|
||||
}
|
||||
|
||||
private skillFilePath(skillPath: string) {
|
||||
return join(this.rootDir, skillPath, "SKILL.md");
|
||||
return join(this.skillDirPath(skillPath), "SKILL.md");
|
||||
}
|
||||
|
||||
private skillDirPath(skillPath: string) {
|
||||
return skillPath === ROOT_SKILL_ALIAS ? this.rootDir : join(this.rootDir, skillPath);
|
||||
}
|
||||
|
||||
private async serializeWrite<T>(task: () => Promise<T>) {
|
||||
@@ -231,6 +287,9 @@ export class SkillStore {
|
||||
|
||||
export const normalizeSkillPath = (rawSkillPath: string) => {
|
||||
const normalized = posix.normalize(rawSkillPath.trim().replace(/^\/+|\/+$/g, ""));
|
||||
if (normalized === ROOT_SKILL_ALIAS) {
|
||||
return ROOT_SKILL_ALIAS;
|
||||
}
|
||||
if (!normalized || normalized === "." || normalized.startsWith("..")) {
|
||||
return null;
|
||||
}
|
||||
@@ -341,6 +400,26 @@ const extractLearnedPatternsSection = (content: string) => {
|
||||
return tail.slice(0, nextHeadingMatch?.index ?? tail.length);
|
||||
};
|
||||
|
||||
const hasValidSkillFrontmatter = (content: string) => {
|
||||
const match = content.match(/^---\n([\s\S]*?)\n---(?:\n|$)/);
|
||||
if (!match) {
|
||||
return false;
|
||||
}
|
||||
const lines = match[1].split("\n").map((line) => line.trim());
|
||||
return (
|
||||
lines.some((line) => /^name:\s*\S+/i.test(line)) &&
|
||||
lines.some((line) => /^description:\s*\S+/i.test(line))
|
||||
);
|
||||
};
|
||||
|
||||
const defaultRootSkill = () => `---
|
||||
name: skills
|
||||
description: TJWater Skills root index.
|
||||
---
|
||||
|
||||
# TJWater Skills
|
||||
`;
|
||||
|
||||
const defaultLearnedSkill = (skillPath: string) => `---
|
||||
name: tjwater-action-${skillPath
|
||||
.split("/")
|
||||
@@ -349,7 +428,7 @@ name: tjwater-action-${skillPath
|
||||
.replace(/[^a-z0-9._-]+/gi, "-")
|
||||
.replace(/^-+|-+$/g, "")
|
||||
.slice(0, 120) || "generated-skill"}
|
||||
description: 由 skill_manager 在线追加的高置信度可复用 workflow。
|
||||
description: 由 skill_manager 在线维护的高置信度可复用 workflow。
|
||||
version: 1.0.0
|
||||
---
|
||||
|
||||
@@ -357,7 +436,10 @@ version: 1.0.0
|
||||
|
||||
## 简介
|
||||
|
||||
记录由 \`skill_manager\` 在线追加的高置信度 workflow 模式。
|
||||
记录由 \`skill_manager\` 在线维护的高置信度 workflow 模式。
|
||||
|
||||
## Learned Patterns
|
||||
`;
|
||||
|
||||
const defaultSkillDocument = (skillPath: string) =>
|
||||
skillPath === ROOT_SKILL_ALIAS ? defaultRootSkill() : defaultLearnedSkill(skillPath);
|
||||
|
||||
Reference in New Issue
Block a user