@kenz1117/dsh-engram 0.9.2 → 0.9.4

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/lib/index.js CHANGED
@@ -4,7 +4,7 @@ import { dirname, isAbsolute, join, relative, resolve } from "node:path";
4
4
  import { BlockAssembler, createUserMessage } from "@deepseek-ai/dsh-llm";
5
5
  import { homedir } from "node:os";
6
6
  import z from "@deepseek-ai/schemastery";
7
- import { copyFile, mkdir, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
7
+ import { copyFile, mkdir, readFile, readdir, rename, rm, stat, writeFile } from "node:fs/promises";
8
8
  import { createGzip, gunzip } from "node:zlib";
9
9
  import { promisify } from "node:util";
10
10
  import { AsyncLocalStorage } from "node:async_hooks";
@@ -516,6 +516,7 @@ const zh$2 = {
516
516
  "misc.exportHeading": "# dsh-engram 导出({scope})",
517
517
  "misc.exportRecordLine": "- [{status}/{kind}] {content}(id={id},importance {importance})",
518
518
  "misc.exportEdgesHeading": "## 关系边",
519
+ "misc.importInvalidPayload": "导入文件结构校验失败:{error}",
519
520
  "misc.backfillCannotStart": "无法启动回填",
520
521
  "misc.archivePathOutsideDbDir": "archivePath 必须在 dbDir 内",
521
522
  "misc.restoreFailed": "恢复失败:{message}",
@@ -553,6 +554,7 @@ const en$2 = {
553
554
  "misc.exportHeading": "# dsh-engram export ({scope})",
554
555
  "misc.exportRecordLine": "- [{status}/{kind}] {content} (id={id}, importance {importance})",
555
556
  "misc.exportEdgesHeading": "## Relation edges",
557
+ "misc.importInvalidPayload": "import file structure validation failed: {error}",
556
558
  "misc.backfillCannotStart": "cannot start backfill",
557
559
  "misc.archivePathOutsideDbDir": "archivePath must be inside dbDir",
558
560
  "misc.restoreFailed": "restore failed: {message}",
@@ -590,6 +592,7 @@ const ru$2 = {
590
592
  "misc.exportHeading": "# Экспорт dsh-engram ({scope})",
591
593
  "misc.exportRecordLine": "- [{status}/{kind}] {content} (id={id}, importance {importance})",
592
594
  "misc.exportEdgesHeading": "## Связи",
595
+ "misc.importInvalidPayload": "проверка структуры файла импорта не пройдена: {error}",
593
596
  "misc.backfillCannotStart": "не удалось запустить заполнение",
594
597
  "misc.archivePathOutsideDbDir": "archivePath должен находиться внутри dbDir",
595
598
  "misc.restoreFailed": "восстановление не удалось: {message}",
@@ -993,6 +996,20 @@ const zh = {
993
996
  "tool.exportWritten": "{path}({records} 条记忆,{edges} 条边{redacted})",
994
997
  "tool.exportDone": "已导出{redacted}:\n{paths}",
995
998
  "tool.exportRedactedView": "(脱敏视图)",
999
+ "tool.importDescription": "从 dsh-engram 导出文件导入记忆:JSON(engram_export format=json 的产物,无损)或导出 Markdown(有损回解析,缺省按扩展名自动识别)。先 dryRun=true 预览分类(新增/重复/冲突),确认后正式导入;导入条目自动就地补嵌入。",
1000
+ "tool.importPathDescription": "导出文件的绝对路径(engram_export 的 JSON 或 Markdown 产物)",
1001
+ "tool.importScopeDescription": "导入目标分库(user/project/shared;默认 user)",
1002
+ "tool.importFormatDescription": "文件格式:json(默认,无损)或 markdown(导出 MD 回解析;缺省按扩展名 .md/.markdown 自动识别)",
1003
+ "tool.importDryRunDescription": "预览模式:只分类不写库(默认 false 正式导入)",
1004
+ "tool.importKeepDuplicatesDescription": "内容全等的重复也强制导入(仅限 id 未被占用的;默认 false 跳过)",
1005
+ "tool.importSelectedIdsDescription": "只导入这些 id(缺省 = 全部按分类规则放行的条目)",
1006
+ "tool.importPathRequired": "engram_import: path 必填(engram_export 产出的 JSON 文件路径)",
1007
+ "tool.importReadFailed": "engram_import: 无法读取 {path}:{error}",
1008
+ "tool.importInvalidPayload": "engram_import: 文件结构校验失败:{error}",
1009
+ "tool.importEmpty": "导入文件没有可导入的记忆(records 为空)。",
1010
+ "tool.importPreviewDone": "预览(未写库):共 {total} 条——新增 {fresh},重复跳过 {dup},冲突(id 被不同内容占用){conflict};边 {edges} 条(悬挂将丢弃 {dangling});实体新建 {entitiesNew} / 合并 {entitiesMerged}。确认后以 dryRun=false 执行。",
1011
+ "tool.importNothing": "没有写入任何条目:跳过重复 {dup} 条,冲突 {conflict} 条。",
1012
+ "tool.importDone": "导入完成:写入 {imported} 条(就地嵌入 {embedded} 条,待嵌入 {pending} 条),跳过重复 {dup},冲突 {conflict};连边 {edges},丢弃悬挂边 {dangling};实体新建 {entitiesNew} / 合并 {entitiesMerged}。",
996
1013
  "tool.distillDescription": "蒸馏整理:把同主题的记忆簇合并提炼为更高层的规律(旧条目归档、supersedes 链保留)。建议记忆较多时周期性执行。",
997
1014
  "tool.distillNoLlm": "engram_distill: 辅助 LLM 不可用(宿主未提供 llm 服务),无法蒸馏",
998
1015
  "tool.distillNoRoute": "engram_distill: 无法确定模型路由(会话尚无模型请求),请在 cordis.yml 配置 provider/model",
@@ -1311,6 +1328,20 @@ const en = {
1311
1328
  "tool.exportWritten": "{path} ({records} memories, {edges} edges{redacted})",
1312
1329
  "tool.exportDone": "Exported{redacted}:\n{paths}",
1313
1330
  "tool.exportRedactedView": " (redacted view)",
1331
+ "tool.importDescription": "Import memories from a dsh-engram export file: JSON (product of engram_export format=json, lossless) or exported Markdown (lossy re-parsing; auto-detected by extension by default). Preview first with dryRun=true (new/duplicate/conflict classification), then commit; imported entries are embedded in place automatically.",
1332
+ "tool.importPathDescription": "Absolute path to the export file (JSON or Markdown product of engram_export)",
1333
+ "tool.importScopeDescription": "Target store to import into (user/project/shared; default user)",
1334
+ "tool.importFormatDescription": "File format: json (default, lossless) or markdown (re-parse exported MD; auto-detected from .md/.markdown extension by default)",
1335
+ "tool.importDryRunDescription": "Preview mode: classify only, no writes (default false = commit)",
1336
+ "tool.importKeepDuplicatesDescription": "Force-import content-identical duplicates (only when their id is unoccupied; default false = skip)",
1337
+ "tool.importSelectedIdsDescription": "Import only these ids (default: everything admitted by the classification rules)",
1338
+ "tool.importPathRequired": "engram_import: path is required (path to a JSON file produced by engram_export)",
1339
+ "tool.importReadFailed": "engram_import: cannot read {path}: {error}",
1340
+ "tool.importInvalidPayload": "engram_import: file structure validation failed: {error}",
1341
+ "tool.importEmpty": "The import file contains no memories (records is empty).",
1342
+ "tool.importPreviewDone": "Preview (nothing written): {total} entries — {fresh} new, {dup} duplicates (skipped), {conflict} conflicts (id occupied by different content); {edges} edges ({dangling} dangling will be dropped); entities: {entitiesNew} new / {entitiesMerged} merged. Commit with dryRun=false.",
1343
+ "tool.importNothing": "Nothing imported: {dup} duplicates skipped, {conflict} conflicts.",
1344
+ "tool.importDone": "Import complete: {imported} entries written ({embedded} embedded in place, {pending} pending embedding), {dup} duplicates skipped, {conflict} conflicts; {edges} edges linked, {dangling} dangling edges dropped; entities: {entitiesNew} new / {entitiesMerged} merged.",
1314
1345
  "tool.distillDescription": "Distillation: merge clusters of memories on the same topic into higher-level rules (old entries are archived, the supersedes chain is kept). Best run periodically once there are many memories.",
1315
1346
  "tool.distillNoLlm": "engram_distill: the auxiliary LLM is unavailable (the host provides no llm service), cannot distill",
1316
1347
  "tool.distillNoRoute": "engram_distill: cannot determine the model route (the session has no model request yet); configure provider/model in cordis.yml",
@@ -1629,6 +1660,20 @@ const ru = {
1629
1660
  "tool.exportWritten": "{path} ({records} воспоминаний, {edges} связей{redacted})",
1630
1661
  "tool.exportDone": "Экспортировано{redacted}:\n{paths}",
1631
1662
  "tool.exportRedactedView": " (обезличенный вид)",
1663
+ "tool.importDescription": "Импортировать воспоминания из файла экспорта dsh-engram: JSON (продукт engram_export format=json, без потерь) или экспортированный Markdown (разбор с потерями; по умолчанию формат определяется по расширению). Сначала предпросмотр с dryRun=true (классификация: новое / дубликат / конфликт), затем импорт; импортированные записи автоматически снабжаются векторами на месте.",
1664
+ "tool.importPathDescription": "Абсолютный путь к файлу экспорта (JSON или Markdown продукт engram_export)",
1665
+ "tool.importScopeDescription": "Целевое хранилище импорта (user/project/shared; по умолчанию user)",
1666
+ "tool.importFormatDescription": "Формат файла: json (по умолчанию, без потерь) или markdown (разбор экспортированного MD; по умолчанию определяется по расширению .md/.markdown)",
1667
+ "tool.importDryRunDescription": "Режим предпросмотра: только классификация, без записи (по умолчанию false = импорт)",
1668
+ "tool.importKeepDuplicatesDescription": "Принудительно импортировать дубликаты с идентичным содержимым (только когда id свободен; по умолчанию false = пропускать)",
1669
+ "tool.importSelectedIdsDescription": "Импортировать только эти id (по умолчанию: всё, допущенное правилами классификации)",
1670
+ "tool.importPathRequired": "engram_import: требуется path (путь к JSON-файлу, созданному engram_export)",
1671
+ "tool.importReadFailed": "engram_import: не удаётся прочитать {path}: {error}",
1672
+ "tool.importInvalidPayload": "engram_import: проверка структуры файла не пройдена: {error}",
1673
+ "tool.importEmpty": "Файл импорта не содержит воспоминаний (records пуст).",
1674
+ "tool.importPreviewDone": "Предпросмотр (ничего не записано): всего {total} — новых {fresh}, дубликатов (пропущено) {dup}, конфликтов (id занят другим содержимым) {conflict}; связей {edges} (висячих будет отброшено {dangling}); сущностей: новых {entitiesNew} / объединено {entitiesMerged}. Подтвердите с dryRun=false.",
1675
+ "tool.importNothing": "Ничего не импортировано: пропущено дубликатов {dup}, конфликтов {conflict}.",
1676
+ "tool.importDone": "Импорт завершён: записано {imported} ({embedded} с векторами на месте, {pending} ожидают векторизации), пропущено дубликатов {dup}, конфликтов {conflict}; связей {edges}, отброшено висячих {dangling}; сущностей: новых {entitiesNew} / объединено {entitiesMerged}.",
1632
1677
  "tool.distillDescription": "Дистилляция: объединить кластеры воспоминаний на одну тему в закономерности более высокого уровня (старые записи архивируются, цепочка supersedes сохраняется). Лучше выполнять периодически, когда воспоминаний много.",
1633
1678
  "tool.distillNoLlm": "engram_distill: вспомогательная LLM недоступна (хост не предоставляет службу llm), дистилляция невозможна",
1634
1679
  "tool.distillNoRoute": "engram_distill: не удалось определить маршрут модели (в сессии ещё не было запроса к модели); настройте provider/model в cordis.yml",
@@ -3849,6 +3894,270 @@ function requireCandidateIds(raw, candidateIds) {
3849
3894
  return ids;
3850
3895
  }
3851
3896
  //#endregion
3897
+ //#region src/store/import.ts
3898
+ const SCOPES = [
3899
+ "user",
3900
+ "project",
3901
+ "shared"
3902
+ ];
3903
+ const KINDS$1 = [
3904
+ "fact",
3905
+ "preference",
3906
+ "decision",
3907
+ "episode",
3908
+ "skill"
3909
+ ];
3910
+ const STATUSES = [
3911
+ "active",
3912
+ "archived",
3913
+ "forgotten"
3914
+ ];
3915
+ const EDGE_TYPES = [
3916
+ "supports",
3917
+ "contradicts",
3918
+ "refines",
3919
+ "related",
3920
+ "supersedes"
3921
+ ];
3922
+ const isObj = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
3923
+ const isStr = (v) => typeof v === "string";
3924
+ const isNum = (v) => typeof v === "number" && Number.isFinite(v);
3925
+ const oneOf = (v, allowed) => isStr(v) && allowed.includes(v);
3926
+ const parseRecord = (raw, index) => {
3927
+ if (!isObj(raw)) return `records[${index}] 不是对象`;
3928
+ const id = raw.id;
3929
+ if (!isStr(id) || id.trim() === "") return `records[${index}].id 缺失或为空`;
3930
+ const content = raw.content;
3931
+ if (!isStr(content)) return `records[${index}].content 不是字符串`;
3932
+ if (!oneOf(raw.scope, SCOPES)) return `records[${index}].scope 非法:${JSON.stringify(raw.scope)}`;
3933
+ if (!oneOf(raw.kind, KINDS$1)) return `records[${index}].kind 非法:${JSON.stringify(raw.kind)}`;
3934
+ if (!oneOf(raw.status, STATUSES)) return `records[${index}].status 非法:${JSON.stringify(raw.status)}`;
3935
+ const importance = raw.importance;
3936
+ const confidence = raw.confidence;
3937
+ const createdAt = raw.createdAt;
3938
+ const lastAccessedAt = raw.lastAccessedAt;
3939
+ const accessCount = raw.accessCount;
3940
+ if (!isNum(importance)) return `records[${index}].importance 不是有限数字`;
3941
+ if (!isNum(confidence)) return `records[${index}].confidence 不是有限数字`;
3942
+ if (!isNum(createdAt)) return `records[${index}].createdAt 不是有限数字`;
3943
+ if (!isNum(lastAccessedAt)) return `records[${index}].lastAccessedAt 不是有限数字`;
3944
+ if (!isNum(accessCount)) return `records[${index}].accessCount 不是有限数字`;
3945
+ if (raw.outcome !== void 0 && raw.outcome !== null && !oneOf(raw.outcome, ["success", "failure"])) return `records[${index}].outcome 非法:${JSON.stringify(raw.outcome)}`;
3946
+ const slot = raw.slot;
3947
+ if (slot !== void 0 && slot !== null && !(isObj(slot) && isStr(slot.room) && slot.room !== "" && isNum(slot.index))) return `records[${index}].slot 非法`;
3948
+ const review = raw.review;
3949
+ if (review !== void 0 && review !== null && !(isObj(review) && (review.nextReviewAt === null || isNum(review.nextReviewAt)) && isNum(review.easeFactor) && isNum(review.intervalDays) && isNum(review.reps))) return `records[${index}].review 非法`;
3950
+ const imagery = raw.imagery;
3951
+ if (imagery !== void 0 && imagery !== null && !(isObj(imagery) && isStr(imagery.caption) && Array.isArray(imagery.sensoryTags) && isNum(imagery.emotionalValence) && typeof imagery.provisional === "boolean")) return `records[${index}].imagery 非法`;
3952
+ return {
3953
+ id: asMemoryId(id),
3954
+ scope: raw.scope,
3955
+ kind: raw.kind,
3956
+ content,
3957
+ importance,
3958
+ confidence,
3959
+ status: raw.status,
3960
+ ...raw.outcome === "success" || raw.outcome === "failure" ? { outcome: raw.outcome } : {},
3961
+ createdAt,
3962
+ lastAccessedAt,
3963
+ accessCount,
3964
+ sourceSessionId: raw.sourceSessionId === null ? null : isStr(raw.sourceSessionId) ? raw.sourceSessionId : null,
3965
+ sourceRound: isNum(raw.sourceRound) ? raw.sourceRound : null,
3966
+ sourceSeq: isNum(raw.sourceSeq) ? raw.sourceSeq : null,
3967
+ ...isObj(raw.imagery) && isStr(raw.imagery.caption) && Array.isArray(raw.imagery.sensoryTags) && isNum(raw.imagery.emotionalValence) && typeof raw.imagery.provisional === "boolean" ? { imagery: {
3968
+ caption: raw.imagery.caption,
3969
+ sensoryTags: raw.imagery.sensoryTags.filter(isStr),
3970
+ emotionalValence: raw.imagery.emotionalValence,
3971
+ provisional: raw.imagery.provisional
3972
+ } } : {},
3973
+ ...isObj(raw.slot) && isStr(raw.slot.room) && isNum(raw.slot.index) ? { slot: {
3974
+ room: raw.slot.room,
3975
+ index: raw.slot.index
3976
+ } } : {},
3977
+ ...isNum(raw.imageryScore) ? { imageryScore: raw.imageryScore } : {},
3978
+ ...isObj(raw.review) && isNum(raw.review.easeFactor) && isNum(raw.review.intervalDays) && isNum(raw.review.reps) ? { review: {
3979
+ nextReviewAt: isNum(raw.review.nextReviewAt) ? raw.review.nextReviewAt : null,
3980
+ easeFactor: raw.review.easeFactor,
3981
+ intervalDays: raw.review.intervalDays,
3982
+ reps: raw.review.reps
3983
+ } } : {}
3984
+ };
3985
+ };
3986
+ const parseEdge = (raw, index) => {
3987
+ if (!isObj(raw)) return `edges[${index}] 不是对象`;
3988
+ if (!isStr(raw.from) || !isStr(raw.to)) return `edges[${index}].from/to 缺失`;
3989
+ if (!oneOf(raw.type, EDGE_TYPES)) return `edges[${index}].type 非法:${JSON.stringify(raw.type)}`;
3990
+ if (!isNum(raw.createdAt)) return `edges[${index}].createdAt 不是有限数字`;
3991
+ return {
3992
+ from: asMemoryId(raw.from),
3993
+ to: asMemoryId(raw.to),
3994
+ type: raw.type,
3995
+ createdAt: raw.createdAt
3996
+ };
3997
+ };
3998
+ const parseEntity = (raw, index) => {
3999
+ if (!isObj(raw)) return `entities[${index}] 不是对象`;
4000
+ if (!isStr(raw.id) || raw.id.trim() === "") return `entities[${index}].id 缺失或为空`;
4001
+ if (!isStr(raw.name) || raw.name.trim() === "") return `entities[${index}].name 缺失或为空`;
4002
+ if (!oneOf(raw.kind, [
4003
+ "person",
4004
+ "project",
4005
+ "tool",
4006
+ "concept",
4007
+ "other"
4008
+ ])) return `entities[${index}].kind 非法:${JSON.stringify(raw.kind)}`;
4009
+ if (!Array.isArray(raw.aliases) || !raw.aliases.every(isStr)) return `entities[${index}].aliases 不是字符串数组`;
4010
+ const entityCreatedAt = raw.createdAt;
4011
+ const entityUpdatedAt = raw.updatedAt;
4012
+ if (!isNum(entityCreatedAt)) return `entities[${index}].createdAt 不是有限数字`;
4013
+ if (!isNum(entityUpdatedAt)) return `entities[${index}].updatedAt 不是有限数字`;
4014
+ return {
4015
+ id: asEntityId(raw.id),
4016
+ name: raw.name,
4017
+ kind: raw.kind,
4018
+ aliases: raw.aliases,
4019
+ createdAt: entityCreatedAt,
4020
+ updatedAt: entityUpdatedAt
4021
+ };
4022
+ };
4023
+ /**
4024
+ * 校验并裁剪导入 payload。返回 string = 首个校验错误(调用方原样呈现给用户)。
4025
+ * 空文件(records 为空数组)合法——预览为空、提交零写入。
4026
+ */
4027
+ function parseImportData(raw) {
4028
+ if (!isObj(raw)) return "导入文件不是 JSON 对象";
4029
+ if (!Array.isArray(raw.records)) return "records 不是数组(不是 dsh-engram 导出文件?)";
4030
+ if (raw.edges !== void 0 && !Array.isArray(raw.edges)) return "edges 不是数组";
4031
+ if (raw.entities !== void 0 && !Array.isArray(raw.entities)) return "entities 不是数组";
4032
+ const records = [];
4033
+ for (let i = 0; i < raw.records.length; i++) {
4034
+ const parsed = parseRecord(raw.records[i], i);
4035
+ if (typeof parsed === "string") return parsed;
4036
+ records.push(parsed);
4037
+ }
4038
+ const edgeList = Array.isArray(raw.edges) ? raw.edges : [];
4039
+ const entityList = Array.isArray(raw.entities) ? raw.entities : [];
4040
+ const edges = [];
4041
+ for (let i = 0; i < edgeList.length; i++) {
4042
+ const parsed = parseEdge(edgeList[i], i);
4043
+ if (typeof parsed === "string") return parsed;
4044
+ edges.push(parsed);
4045
+ }
4046
+ const entities = [];
4047
+ for (let i = 0; i < entityList.length; i++) {
4048
+ const parsed = parseEntity(entityList[i], i);
4049
+ if (typeof parsed === "string") return parsed;
4050
+ entities.push(parsed);
4051
+ }
4052
+ return {
4053
+ exportedAt: isNum(raw.exportedAt) ? raw.exportedAt : Date.now(),
4054
+ records,
4055
+ edges,
4056
+ ...entities.length > 0 ? { entities } : {}
4057
+ };
4058
+ }
4059
+ /** 就地补嵌入的单批大小:导出格式不携带向量,导入后分批补齐,单批失败不拖垮整批。 */
4060
+ const EMBED_BATCH_SIZE = 32;
4061
+ /**
4062
+ * 导出 Markdown 的回解析(engram_export format=markdown 的产物 → ExportData)。
4063
+ * 有损恢复:MD 行只携带 status/kind/content/id/importance(记录)与 from/type/to(边),
4064
+ * 其余字段取缺省(confidence 0.5、时间取当前、accessCount 0)。不匹配导出行格式的行静默跳过。
4065
+ * 头部带脱敏标记(zh/en/ru 三语)时返回 redacted=true——调用方应提示用户内容是截断预览。
4066
+ */
4067
+ function parseMarkdownExport(raw, scope) {
4068
+ const at = Date.now();
4069
+ const recordRe = /^- \[([a-z]+)\/([a-z]+)\] (.*[^\s])?\s*[((]id=([^,()\s]+)[,,]\s*importance ([0-9.]+)[))]\s*$/;
4070
+ const edgeRe = /^- (\S+) --([a-z]+)--> (\S+)\s*$/;
4071
+ const headerRe = /^#\s*(?:dsh-engram\s*导出|dsh-engram\s*export|Экспорт\s*dsh-engram)\s*[((](.*)[))]\s*$/;
4072
+ const edgesHeadingRe = /^##\s*(?:关系边|Relation edges|Связи)\s*$/;
4073
+ let redacted = false;
4074
+ let inEdges = false;
4075
+ const records = [];
4076
+ const edges = [];
4077
+ for (const line of raw.split(/\r?\n/)) {
4078
+ const header = headerRe.exec(line);
4079
+ if (header !== null) {
4080
+ const label = header[1] ?? "";
4081
+ redacted = label.includes("脱敏视图") || label.includes("redacted view") || label.includes("обезличенный");
4082
+ continue;
4083
+ }
4084
+ if (edgesHeadingRe.test(line)) {
4085
+ inEdges = true;
4086
+ continue;
4087
+ }
4088
+ if (inEdges) {
4089
+ const edge = edgeRe.exec(line);
4090
+ if (edge === null) continue;
4091
+ const from = edge[1];
4092
+ const type = edge[2];
4093
+ const to = edge[3];
4094
+ if (from === void 0 || to === void 0 || !oneOf(type, EDGE_TYPES)) continue;
4095
+ edges.push({
4096
+ from: asMemoryId(from),
4097
+ to: asMemoryId(to),
4098
+ type,
4099
+ createdAt: at
4100
+ });
4101
+ continue;
4102
+ }
4103
+ const record = recordRe.exec(line);
4104
+ if (record === null) continue;
4105
+ const status = record[1];
4106
+ const kind = record[2];
4107
+ const content = record[3];
4108
+ const id = record[4];
4109
+ const importanceRaw = record[5];
4110
+ if (status === void 0 || kind === void 0 || content === void 0 || content === "" || id === void 0 || id === "" || importanceRaw === void 0) continue;
4111
+ if (!oneOf(status, STATUSES) || !oneOf(kind, KINDS$1)) continue;
4112
+ const importance = Number(importanceRaw);
4113
+ if (!Number.isFinite(importance)) continue;
4114
+ records.push({
4115
+ id: asMemoryId(id),
4116
+ scope,
4117
+ kind,
4118
+ content,
4119
+ importance,
4120
+ confidence: .5,
4121
+ status,
4122
+ createdAt: at,
4123
+ lastAccessedAt: at,
4124
+ accessCount: 0,
4125
+ sourceSessionId: null,
4126
+ sourceRound: null,
4127
+ sourceSeq: null
4128
+ });
4129
+ }
4130
+ return {
4131
+ data: {
4132
+ exportedAt: at,
4133
+ records,
4134
+ edges
4135
+ },
4136
+ redacted
4137
+ };
4138
+ }
4139
+ /**
4140
+ * 导入后就地补嵌入(engram_import 工具与面板导入路由共用):按批嵌入导入条目并经
4141
+ * store.attachEmbeddings 回填。嵌入器不可用返回 0;单批失败只跳过该批不中断。
4142
+ */
4143
+ async function embedImportedRecords(store, embedder, importedIds) {
4144
+ if (embedder === void 0 || importedIds.length === 0) return 0;
4145
+ const records = await store.getMany(importedIds);
4146
+ let embedded = 0;
4147
+ for (let i = 0; i < records.length; i += EMBED_BATCH_SIZE) {
4148
+ const batch = records.slice(i, i + EMBED_BATCH_SIZE);
4149
+ try {
4150
+ const vectors = await embedder.embed(batch.map((record) => record.content));
4151
+ const attached = batch.map((record, position) => ({
4152
+ id: record.id,
4153
+ embedding: vectors[position]
4154
+ })).filter((entry) => entry.embedding !== void 0);
4155
+ embedded += await store.attachEmbeddings(attached);
4156
+ } catch {}
4157
+ }
4158
+ return embedded;
4159
+ }
4160
+ //#endregion
3852
4161
  //#region src/jev/client.ts
3853
4162
  /**
3854
4163
  * 调用 Jev /v1/systemone 端点并行回答 Noul 问题。
@@ -4874,7 +5183,11 @@ function registerEngramRoutes(ctx, deps) {
4874
5183
  return;
4875
5184
  }
4876
5185
  if (req.method === "GET" && route === "stats") {
4877
- json(res, 200, { parts: await Promise.all(["user", "project"].map(async (scope) => ({
5186
+ json(res, 200, { parts: await Promise.all([
5187
+ "user",
5188
+ "project",
5189
+ "shared"
5190
+ ].map(async (scope) => ({
4878
5191
  scope,
4879
5192
  stats: await (await storeFor(scope)).stats()
4880
5193
  }))) });
@@ -5243,16 +5556,67 @@ function registerEngramRoutes(ctx, deps) {
5243
5556
  res.end(body);
5244
5557
  return;
5245
5558
  }
5559
+ if (req.method === "POST" && route === "import") {
5560
+ if (!guardWrite(req, res)) return;
5561
+ const body = await readJsonBody(req);
5562
+ const bodyObj = typeof body === "object" && body !== null && !Array.isArray(body) ? body : {};
5563
+ const scope = scopeOf$1(typeof bodyObj.scope === "string" ? bodyObj.scope : null, "user");
5564
+ let data;
5565
+ let redacted = false;
5566
+ if (bodyObj.format === "markdown") {
5567
+ if (typeof bodyObj.data !== "string") {
5568
+ json(res, 400, { error: messageIn(deps.language, "misc.importInvalidPayload", { error: "format=markdown 需要 data 为导出 Markdown 文本" }) });
5569
+ return;
5570
+ }
5571
+ const parsedMarkdown = parseMarkdownExport(bodyObj.data, scope);
5572
+ if (typeof parsedMarkdown === "string") {
5573
+ json(res, 400, { error: messageIn(deps.language, "misc.importInvalidPayload", { error: parsedMarkdown }) });
5574
+ return;
5575
+ }
5576
+ data = parsedMarkdown.data;
5577
+ redacted = parsedMarkdown.redacted;
5578
+ } else {
5579
+ const parsed = parseImportData(bodyObj.data);
5580
+ if (typeof parsed === "string") {
5581
+ json(res, 400, { error: messageIn(deps.language, "misc.importInvalidPayload", { error: parsed }) });
5582
+ return;
5583
+ }
5584
+ data = parsed;
5585
+ }
5586
+ const store = await storeFor(scope);
5587
+ if (bodyObj.dryRun === true) {
5588
+ json(res, 200, {
5589
+ preview: await store.importPreview(data),
5590
+ ...redacted ? { redacted: true } : {}
5591
+ });
5592
+ return;
5593
+ }
5594
+ const result = await store.importData(data, {
5595
+ ...Array.isArray(bodyObj.selectedIds) ? { selectedIds: bodyObj.selectedIds.filter((v) => typeof v === "string").map((id) => asMemoryId(id)) } : {},
5596
+ ...bodyObj.keepDuplicates === true ? { keepDuplicates: true } : {}
5597
+ });
5598
+ const embedded = await embedImportedRecords(store, deps.embedder === void 0 ? void 0 : await deps.embedder, result.importedIds);
5599
+ json(res, 200, {
5600
+ ...result,
5601
+ embedded
5602
+ });
5603
+ return;
5604
+ }
5246
5605
  if (req.method === "GET" && route === "mirror") {
5247
5606
  const scope = scopeOf$1(url.searchParams.get("scope"), "user");
5248
5607
  const data = await (await storeFor(scope)).exportAll();
5249
5608
  const stamp = (/* @__PURE__ */ new Date()).toISOString().replaceAll(":", "-").slice(0, 19);
5250
- json(res, 200, {
5251
- ...await writeMirror(join(deps.mirrorDir, scope, stamp), data, deps.language),
5609
+ const report = await writeMirror(join(deps.mirrorDir, scope, stamp), data, deps.language);
5610
+ res.writeHead(200, {
5611
+ "content-type": "application/json; charset=utf-8",
5612
+ "content-disposition": `attachment; filename="mirror-${scope}-${stamp}.json"`
5613
+ });
5614
+ res.end(JSON.stringify({
5615
+ ...report,
5252
5616
  scope,
5253
5617
  memoryCount: data.records.length,
5254
5618
  edgeCount: data.edges.length
5255
- });
5619
+ }));
5256
5620
  return;
5257
5621
  }
5258
5622
  if (req.method === "GET" && route === "health") {
@@ -6450,6 +6814,14 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
6450
6814
  const sqlCountRedacted = db.prepare("SELECT COUNT(*) AS n FROM nodes WHERE content LIKE '%[REDACTED:%'");
6451
6815
  const sqlAllNodes = db.prepare("SELECT * FROM nodes ORDER BY created_at");
6452
6816
  const sqlAllEdges = db.prepare("SELECT * FROM edges");
6817
+ const sqlImportInsert = db.prepare(`INSERT INTO nodes
6818
+ (id, scope, kind, content, importance, confidence, status, created_at, last_accessed_at, access_count,
6819
+ source_session_id, source_round, source_seq, embedding, outcome, imagery_json,
6820
+ slot_room, slot_index, imagery_score, next_review_at, ease_factor, interval_days, review_reps)
6821
+ VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)`);
6822
+ const sqlImportContentMatch = db.prepare("SELECT id FROM nodes WHERE content = ? LIMIT 1");
6823
+ const sqlImportAttachEmbedding = db.prepare("UPDATE nodes SET embedding = ? WHERE id = ?");
6824
+ const sqlImportEntityAliases = db.prepare("UPDATE entities SET aliases_json = ?, updated_at = ? WHERE id = ?");
6453
6825
  const sqlDecay = db.prepare(`UPDATE nodes SET status = 'archived'
6454
6826
  WHERE status = 'active' AND importance < ? AND last_accessed_at < ? AND next_review_at IS NULL`);
6455
6827
  const sqlScheduleReview = db.prepare(`UPDATE nodes
@@ -6607,6 +6979,37 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
6607
6979
  ...initialReviewAt === void 0 ? {} : { initialReviewAt }
6608
6980
  };
6609
6981
  };
6982
+ /** 导入分类(importPreview 与 importData 共用):id 占用时比内容全等,否则查内容是否与库内他条全等。 */
6983
+ const classifyForImport = (records) => records.map((record) => {
6984
+ const content = String(record.content ?? "").trim();
6985
+ const occupied = getRow(record.id);
6986
+ if (occupied !== void 0) {
6987
+ if (occupied.content === content) return {
6988
+ record,
6989
+ content,
6990
+ classification: "duplicate",
6991
+ duplicateOf: asMemoryId(occupied.id)
6992
+ };
6993
+ return {
6994
+ record,
6995
+ content,
6996
+ classification: "conflict",
6997
+ duplicateOf: asMemoryId(occupied.id)
6998
+ };
6999
+ }
7000
+ const hit = sqlImportContentMatch.get(content);
7001
+ if (hit !== void 0) return {
7002
+ record,
7003
+ content,
7004
+ classification: "duplicate",
7005
+ duplicateOf: asMemoryId(hit.id)
7006
+ };
7007
+ return {
7008
+ record,
7009
+ content,
7010
+ classification: "new"
7011
+ };
7012
+ });
6610
7013
  return {
6611
7014
  async write(input) {
6612
7015
  const content = input.content.trim();
@@ -7148,6 +7551,113 @@ async function openEngramStore(path, rankBoost = NO_BOOST, automation = DEFAULT_
7148
7551
  entities
7149
7552
  };
7150
7553
  },
7554
+ async importPreview(data) {
7555
+ const classified = classifyForImport(data.records ?? []);
7556
+ const storeIds = new Set(sqlAllNodes.all().map((row) => row.id));
7557
+ const importable = new Set(classified.filter((c) => c.classification === "new").map((c) => c.record.id));
7558
+ let edgesDangling = 0;
7559
+ for (const edge of data.edges ?? []) {
7560
+ if (!(storeIds.has(edge.from) || importable.has(edge.from))) {
7561
+ edgesDangling++;
7562
+ continue;
7563
+ }
7564
+ if (!(storeIds.has(edge.to) || importable.has(edge.to))) edgesDangling++;
7565
+ }
7566
+ let entitiesNew = 0;
7567
+ let entitiesMerged = 0;
7568
+ for (const entity of data.entities ?? []) if (sqlEntityGet.get(entity.id) === void 0) entitiesNew++;
7569
+ else entitiesMerged++;
7570
+ return {
7571
+ entries: classified.map((c) => ({
7572
+ id: c.record.id,
7573
+ kind: c.record.kind,
7574
+ status: c.record.status,
7575
+ content: c.content,
7576
+ classification: c.classification,
7577
+ ...c.duplicateOf === void 0 ? {} : { duplicateOf: c.duplicateOf }
7578
+ })),
7579
+ edgesTotal: (data.edges ?? []).length,
7580
+ edgesDangling,
7581
+ entitiesNew,
7582
+ entitiesMerged
7583
+ };
7584
+ },
7585
+ async importData(data, options = {}) {
7586
+ const classified = classifyForImport(data.records ?? []);
7587
+ const selected = options.selectedIds === void 0 ? void 0 : new Set(options.selectedIds);
7588
+ const eligible = classified.filter((c) => {
7589
+ if (selected !== void 0 && !selected.has(c.record.id)) return false;
7590
+ if (c.classification === "new") return true;
7591
+ if (c.classification !== "duplicate") return false;
7592
+ return options.keepDuplicates === true && getRow(c.record.id) === void 0;
7593
+ });
7594
+ const importedIds = new Set(eligible.map((c) => c.record.id));
7595
+ const storeIds = new Set(sqlAllNodes.all().map((row) => row.id));
7596
+ const entityRows = data.entities ?? [];
7597
+ let edgesLinked = 0;
7598
+ let edgesDangling = 0;
7599
+ let entitiesNew = 0;
7600
+ let entitiesMerged = 0;
7601
+ const at = Date.now();
7602
+ withTransaction(() => {
7603
+ for (const c of eligible) {
7604
+ const r = c.record;
7605
+ const review = r.review;
7606
+ sqlImportInsert.run(r.id, r.scope, r.kind, c.content, r.importance, r.confidence, r.status, r.createdAt, r.lastAccessedAt, r.accessCount, r.sourceSessionId ?? null, r.sourceRound ?? null, r.sourceSeq ?? null, null, r.outcome ?? null, imageryToJson(r.imagery), r.slot?.room ?? null, r.slot?.index ?? null, r.imageryScore ?? null, review?.nextReviewAt ?? null, review?.easeFactor ?? null, review?.intervalDays ?? null, review?.reps ?? null);
7607
+ sqlFtsInsert.run(r.id, tokenizeForFts(c.content));
7608
+ }
7609
+ for (const edge of data.edges ?? []) {
7610
+ if (!(storeIds.has(edge.from) || importedIds.has(edge.from))) {
7611
+ edgesDangling++;
7612
+ continue;
7613
+ }
7614
+ if (!(storeIds.has(edge.to) || importedIds.has(edge.to))) {
7615
+ edgesDangling++;
7616
+ continue;
7617
+ }
7618
+ sqlEdgeUpsert.run(edge.from, edge.to, edge.type, edge.createdAt);
7619
+ edgesLinked++;
7620
+ }
7621
+ for (const entity of entityRows) {
7622
+ const row = sqlEntityGet.get(entity.id);
7623
+ if (row === void 0) {
7624
+ const aliases = [...new Set(entity.aliases)].map((a) => a.trim()).filter((a) => a !== "");
7625
+ sqlEntityInsert.run(entity.id, entity.name, entity.kind, JSON.stringify(aliases), entity.createdAt, entity.updatedAt);
7626
+ entitiesNew++;
7627
+ continue;
7628
+ }
7629
+ const merged = [.../* @__PURE__ */ new Set([...entityRowToRecord(row).aliases, ...entity.aliases])].map((a) => a.trim()).filter((a) => a !== "");
7630
+ sqlImportEntityAliases.run(JSON.stringify(merged), at, entity.id);
7631
+ entitiesMerged++;
7632
+ }
7633
+ if (eligible.length > 0 || (data.edges ?? []).length > 0) sqlLog.run(at, "import", "BATCH", JSON.stringify({
7634
+ imported: eligible.length,
7635
+ edges: edgesLinked,
7636
+ dangling: edgesDangling,
7637
+ exportedAt: data.exportedAt
7638
+ }));
7639
+ });
7640
+ return {
7641
+ imported: eligible.length,
7642
+ importedIds: eligible.map((c) => c.record.id),
7643
+ skippedDuplicates: classified.filter((c) => c.classification === "duplicate").length - eligible.filter((c) => c.classification === "duplicate").length,
7644
+ conflicts: classified.filter((c) => c.classification === "conflict").length,
7645
+ edgesLinked,
7646
+ edgesDangling,
7647
+ entitiesNew,
7648
+ entitiesMerged
7649
+ };
7650
+ },
7651
+ async attachEmbeddings(entries) {
7652
+ let attached = 0;
7653
+ withTransaction(() => {
7654
+ for (const entry of entries) {
7655
+ const result = sqlImportAttachEmbedding.run(vecToBlob(entry.embedding), entry.id);
7656
+ attached += Number(result.changes);
7657
+ }
7658
+ });
7659
+ return attached;
7660
+ },
7151
7661
  async decay(options) {
7152
7662
  const cutoff = Date.now() - options.olderThanDays * 864e5;
7153
7663
  const result = sqlDecay.run(options.importanceBelow, cutoff);
@@ -8049,8 +8559,8 @@ async function rewriteQueries(deps, exec, query) {
8049
8559
  }
8050
8560
  }
8051
8561
  /**
8052
- * 构造 20 个工具定义(engram_save/search/facts/assess/timeline/episode_timeline/update/forget/report/review/review_queue/
8053
- * stats/export/distill/examine/neighbors/audit_forgotten/tour/ingest_history/profile_edit)。
8562
+ * 构造 21 个工具定义(engram_save/search/facts/assess/timeline/episode_timeline/update/forget/report/review/review_queue/
8563
+ * stats/export/import/distill/examine/neighbors/audit_forgotten/tour/ingest_history/profile_edit)。
8054
8564
  * @param baseDeps - 分库打开器、嵌入器、辅助 LLM、导出目录。
8055
8565
  * @returns 可直接 register 的工具定义数组(execute 已绑定会话 cwd 的项目宫殿路由)。
8056
8566
  */
@@ -9946,6 +10456,126 @@ function createEngramTools(baseDeps) {
9946
10456
  }) };
9947
10457
  }
9948
10458
  }),
10459
+ defineTool({
10460
+ name: "engram_import",
10461
+ description: message("tool.importDescription"),
10462
+ parameters: {
10463
+ path: {
10464
+ type: "string",
10465
+ description: message("tool.importPathDescription")
10466
+ },
10467
+ scope: {
10468
+ type: "string",
10469
+ enum: [
10470
+ "user",
10471
+ "project",
10472
+ "shared"
10473
+ ],
10474
+ description: message("tool.importScopeDescription")
10475
+ },
10476
+ format: {
10477
+ type: "string",
10478
+ enum: ["json", "markdown"],
10479
+ description: message("tool.importFormatDescription")
10480
+ },
10481
+ dryRun: {
10482
+ type: "boolean",
10483
+ description: message("tool.importDryRunDescription")
10484
+ },
10485
+ keepDuplicates: {
10486
+ type: "boolean",
10487
+ description: message("tool.importKeepDuplicatesDescription")
10488
+ },
10489
+ selectedIds: {
10490
+ type: "array",
10491
+ items: { type: "string" },
10492
+ description: message("tool.importSelectedIdsDescription")
10493
+ }
10494
+ },
10495
+ output: {
10496
+ schema: {
10497
+ type: "object",
10498
+ additionalProperties: false,
10499
+ properties: { text: {
10500
+ type: "string",
10501
+ required: true
10502
+ } }
10503
+ },
10504
+ render: (_args, value) => [{
10505
+ type: "text",
10506
+ text: value.text
10507
+ }]
10508
+ },
10509
+ async execute(args) {
10510
+ const input = args;
10511
+ if (typeof input.path !== "string" || input.path.trim() === "") throw new Error(message("tool.importPathRequired"));
10512
+ const scope = scopeOf(input.scope, "user");
10513
+ const markdown = input.format === "markdown" || input.format === void 0 && /\.(?:md|markdown)$/i.test(input.path);
10514
+ let text;
10515
+ try {
10516
+ text = await readFile(input.path, "utf8");
10517
+ } catch (error) {
10518
+ throw new Error(message("tool.importReadFailed", {
10519
+ path: input.path,
10520
+ error: error instanceof Error ? error.message : String(error)
10521
+ }));
10522
+ }
10523
+ let data;
10524
+ if (markdown) {
10525
+ const parsedMarkdown = parseMarkdownExport(text, scope);
10526
+ if (typeof parsedMarkdown === "string") throw new Error(message("tool.importInvalidPayload", { error: parsedMarkdown }));
10527
+ data = parsedMarkdown.data;
10528
+ } else {
10529
+ let raw;
10530
+ try {
10531
+ raw = JSON.parse(text);
10532
+ } catch (error) {
10533
+ throw new Error(message("tool.importReadFailed", {
10534
+ path: input.path,
10535
+ error: error instanceof Error ? error.message : String(error)
10536
+ }));
10537
+ }
10538
+ const parsed = parseImportData(raw);
10539
+ if (typeof parsed === "string") throw new Error(message("tool.importInvalidPayload", { error: parsed }));
10540
+ data = parsed;
10541
+ }
10542
+ const store = await deps.openStore(scope);
10543
+ if (input.dryRun === true) {
10544
+ const preview = await store.importPreview(data);
10545
+ const fresh = preview.entries.filter((e) => e.classification === "new").length;
10546
+ const dup = preview.entries.filter((e) => e.classification === "duplicate").length;
10547
+ return { text: preview.entries.length === 0 ? message("tool.importEmpty") : message("tool.importPreviewDone", {
10548
+ total: preview.entries.length,
10549
+ fresh,
10550
+ dup,
10551
+ conflict: preview.entries.length - fresh - dup,
10552
+ edges: preview.edgesTotal,
10553
+ dangling: preview.edgesDangling,
10554
+ entitiesNew: preview.entitiesNew,
10555
+ entitiesMerged: preview.entitiesMerged
10556
+ }) };
10557
+ }
10558
+ const result = await store.importData(data, {
10559
+ ...Array.isArray(input.selectedIds) ? { selectedIds: input.selectedIds.filter((v) => typeof v === "string").map((id) => asMemoryId(id)) } : {},
10560
+ ...input.keepDuplicates === true ? { keepDuplicates: true } : {}
10561
+ });
10562
+ const embedded = await embedImportedRecords(store, await deps.embedder, result.importedIds);
10563
+ return { text: result.imported === 0 ? message("tool.importNothing", {
10564
+ dup: result.skippedDuplicates,
10565
+ conflict: result.conflicts
10566
+ }) : message("tool.importDone", {
10567
+ imported: result.imported,
10568
+ embedded,
10569
+ pending: result.imported - embedded,
10570
+ dup: result.skippedDuplicates,
10571
+ conflict: result.conflicts,
10572
+ edges: result.edgesLinked,
10573
+ dangling: result.edgesDangling,
10574
+ entitiesNew: result.entitiesNew,
10575
+ entitiesMerged: result.entitiesMerged
10576
+ }) };
10577
+ }
10578
+ }),
9949
10579
  defineTool({
9950
10580
  name: "engram_distill",
9951
10581
  description: message("tool.distillDescription"),
@@ -10599,7 +11229,7 @@ function estimateTokens(text) {
10599
11229
  }
10600
11230
  //#endregion
10601
11231
  //#region package.json
10602
- var version = "0.9.0";
11232
+ var version = "0.9.2";
10603
11233
  //#endregion
10604
11234
  //#region src/index.ts
10605
11235
  /**
@@ -10838,7 +11468,10 @@ async function preStep(ctx, openStore, openStoreForProjectCwd, resolved, embedde
10838
11468
  };
10839
11469
  }
10840
11470
  }
10841
- if (step !== 1) return decision;
11471
+ const compactionCount = agent.session.snapshotEvents().filter((event) => event.type === "compaction/end").length;
11472
+ const compacted = state.lastProfileCompactions !== null && compactionCount > state.lastProfileCompactions;
11473
+ if (state.lastProfileCompactions === null) state.lastProfileCompactions = compactionCount;
11474
+ if (step !== 1 && !compacted) return decision;
10842
11475
  const store = await openStore("user");
10843
11476
  const curated = await store.getProfileBlock("user");
10844
11477
  const top = await store.topActive("user", resolved.profileTopN);
@@ -10884,9 +11517,10 @@ async function preStep(ctx, openStore, openStoreForProjectCwd, resolved, embedde
10884
11517
  });
10885
11518
  const textWithRationale = wrapWithRationale(text, top) + dueLine;
10886
11519
  const hash = createHash("sha256").update(textWithRationale).digest("hex");
10887
- if (state.lastProfileAgent === String(agent.id) && hash === state.lastProfileHash) return decision;
11520
+ if (!compacted && state.lastProfileAgent === String(agent.id) && hash === state.lastProfileHash) return decision;
10888
11521
  state.lastProfileAgent = String(agent.id);
10889
11522
  state.lastProfileHash = hash;
11523
+ state.lastProfileCompactions = compactionCount;
10890
11524
  const packet = renderMemoryPacket(text, "turn_start", currentUserRequestText(decision.messages));
10891
11525
  return {
10892
11526
  ...decision,
@@ -11054,6 +11688,7 @@ function apply(ctx, config = {}) {
11054
11688
  pendingReplayed: false,
11055
11689
  lastProfileAgent: null,
11056
11690
  lastProfileHash: null,
11691
+ lastProfileCompactions: null,
11057
11692
  lastAssessReminder: null,
11058
11693
  route: void 0
11059
11694
  };