新增 memory 和 skill 存储,实现 Agent 持续学习,并增加工具支持;增加 LLM progress detail 输出

This commit is contained in:
2026-05-11 16:12:20 +08:00
parent 883faa2d54
commit f049712b68
16 changed files with 1411 additions and 129 deletions
+111
View File
@@ -0,0 +1,111 @@
import { createHash } from "node:crypto";
import { mkdir, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
import { dirname, join } from "node:path";
type JsonRecord = Record<string, unknown>;
const isErrnoException = (error: unknown): error is NodeJS.ErrnoException =>
error instanceof Error && "code" in error;
export const ensureDirectory = async (path: string) => {
await mkdir(path, { recursive: true });
};
export const atomicWriteFile = async (path: string, content: string) => {
await ensureDirectory(dirname(path));
const tempPath = `${path}.${process.pid}.${Date.now().toString(36)}.tmp`;
await writeFile(tempPath, content, "utf8");
await rename(tempPath, path);
};
export const atomicWriteJson = async (path: string, value: JsonRecord | unknown[]) => {
await atomicWriteFile(path, JSON.stringify(value, null, 2));
};
export const readJsonFile = async <T>(path: string): Promise<T | null> => {
try {
const content = await readFile(path, "utf8");
return JSON.parse(content) as T;
} catch (error) {
if (isErrnoException(error) && error.code === "ENOENT") {
return null;
}
throw error;
}
};
export const readTextFile = async (path: string): Promise<string | null> => {
try {
return await readFile(path, "utf8");
} catch (error) {
if (isErrnoException(error) && error.code === "ENOENT") {
return null;
}
throw error;
}
};
export const listJsonFiles = async (path: string) => {
try {
const names = await readdir(path);
return names.filter((name) => name.endsWith(".json")).map((name) => join(path, name));
} catch (error) {
if (isErrnoException(error) && error.code === "ENOENT") {
return [];
}
throw error;
}
};
export const listFiles = async (path: string) => {
try {
const names = await readdir(path);
return names.map((name) => join(path, name));
} catch (error) {
if (isErrnoException(error) && error.code === "ENOENT") {
return [];
}
throw error;
}
};
export const removeFileIfExists = async (path: string) => {
try {
await rm(path, { force: true });
} catch (error) {
if (isErrnoException(error) && error.code === "ENOENT") {
return;
}
throw error;
}
};
export const getFileStat = async (path: string) => {
try {
return await stat(path);
} catch (error) {
if (isErrnoException(error) && error.code === "ENOENT") {
return null;
}
throw error;
}
};
export const toScopedKey = (prefix: string, value?: string) => {
const normalized = value?.trim() || `${prefix}-default`;
return `${prefix}-${createHash("sha256").update(normalized).digest("hex").slice(0, 16)}`;
};
export const toActorKey = (userId?: string) => toScopedKey("actor", userId);
export const toProjectKey = (projectId?: string) => toScopedKey("project", projectId);
export const toStableId = (...parts: string[]) =>
createHash("sha256").update(parts.join("|")).digest("hex").slice(0, 24);
export const slugify = (value: string) =>
value
.toLowerCase()
.replace(/[^a-z0-9._-]+/g, "-")
.replace(/^-+|-+$/g, "")
.slice(0, 64) || "entry";
+43
View File
@@ -0,0 +1,43 @@
const FORBIDDEN_PERSISTENCE_PATTERNS = [
/ignore\s+(all|previous|prior|above)\s+instructions/i,
/system\s+prompt/i,
/do\s+not\s+tell\s+the\s+user/i,
/curl\s+.*(token|secret|password|api)/i,
/bearer\s+[a-z0-9._-]{16,}/i,
/(api[_-]?key|access[_-]?token|refresh[_-]?token|secret|password)\s*[:=]/i,
/eyJ[a-zA-Z0-9_-]{8,}\.[a-zA-Z0-9._-]{8,}\.[a-zA-Z0-9._-]{8,}/,
];
export const sanitizePersistentLine = (content: string, maxLength: number) => {
const normalized = content.replace(/\s+/g, " ").trim();
if (!normalized) {
return "";
}
if (FORBIDDEN_PERSISTENCE_PATTERNS.some((pattern) => pattern.test(normalized))) {
return "";
}
if (normalized.length > maxLength) {
return `${normalized.slice(0, maxLength - 3).trimEnd()}...`;
}
return normalized;
};
export const sanitizePersistentDocument = (content: string, maxLength: number) => {
const normalized = content
.replace(/\r\n/g, "\n")
.split("\n")
.map((line) => line.trimEnd())
.join("\n")
.replace(/\n{3,}/g, "\n\n")
.trim();
if (!normalized) {
return "";
}
if (FORBIDDEN_PERSISTENCE_PATTERNS.some((pattern) => pattern.test(normalized))) {
return "";
}
if (normalized.length > maxLength) {
return `${normalized.slice(0, maxLength - 3).trimEnd()}...`;
}
return normalized;
};