evolcore 0.0.41-test.0 → 0.0.41-test.1
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/CHANGELOG.md +49 -0
- package/dist/agents/baseagent-availability.js +1 -1
- package/dist/agents/baseagent.js +20 -43
- package/dist/agents/claude-runner.js +177 -92
- package/dist/agents/codex-runner.js +84 -28
- package/dist/agents/ecagent-runner.js +1 -1
- package/dist/agents/gemini-runner.js +1 -1
- package/dist/aun/group/fs-download.js +39 -0
- package/dist/aun/group/identity.js +36 -21
- package/dist/aun/group/operations.js +5 -0
- package/dist/aun/msg/managed-operation.js +68 -7
- package/dist/aun/msg/p2p.js +5 -0
- package/dist/aun/service-proxy.js +36 -12
- package/dist/channels/aun.js +334 -20
- package/dist/channels/daemon.js +15 -1
- package/dist/cli/aun-commands.js +4 -20
- package/dist/cli/daemon-commands.js +678 -697
- package/dist/cli/fs-command.js +64 -2
- package/dist/cli/index.js +48 -4
- package/dist/cli/managed-command-guard.js +4 -0
- package/dist/cli/model.js +40 -40
- package/dist/cli/proxy-command.js +92 -0
- package/dist/cli/role.js +85 -0
- package/dist/config/access-policy.js +7 -2
- package/dist/config/boot-log.js +38 -1
- package/dist/config/config-field-policy.js +5 -32
- package/dist/config/config-manager.js +46 -39
- package/dist/config/config-operation-service.js +5 -19
- package/dist/config/contact-book-store.js +47 -21
- package/dist/config/contact-book.js +18 -12
- package/dist/config/contact-operation-service.js +3 -4
- package/dist/config/contact-request-service.js +11 -0
- package/dist/config/daemon-services.js +11 -0
- package/dist/config/gateway-config.js +88 -594
- package/dist/config/peer-role-resolver.js +13 -11
- package/dist/config/providers-config.js +12 -7
- package/dist/config/providers-import.js +3 -21
- package/dist/config/resolved-config-op.js +3 -0
- package/dist/config/role-command-service.js +166 -0
- package/dist/config/role-request-service.js +193 -0
- package/dist/config/schema-migration.js +686 -25
- package/dist/config-store.js +79 -3
- package/dist/contracts/ecweb-instance.js +41 -0
- package/dist/contracts/ecweb-service-protocol.js +3 -1
- package/dist/core/agent-runtime.js +1 -1
- package/dist/core/auth/agent-delegation.js +2 -1
- package/dist/core/auth/auth-gateway.js +1 -1
- package/dist/core/auth/authorization-audit.js +17 -6
- package/dist/core/auth/delegation-authorization.js +3 -1
- package/dist/core/auth/operation-authorizer.js +15 -0
- package/dist/core/auth/operation-catalog.js +31 -6
- package/dist/core/command/agent-control.js +3 -7
- package/dist/core/command/agent-storage-menu.js +5 -3
- package/dist/core/command/cli-intent-parser.js +2 -1
- package/dist/core/command/command-handler.js +48 -99
- package/dist/core/command/connect-menu.js +11 -3
- package/dist/core/command/data-root-resolver.js +5 -4
- package/dist/core/command/memory-slash.js +16 -8
- package/dist/core/command/menu-catalog.js +5 -6
- package/dist/core/command/menu-handler.js +49 -62
- package/dist/core/command/role-menu.js +147 -30
- package/dist/core/command/slash-handler.js +123 -254
- package/dist/core/daemon-control.js +8 -14
- package/dist/core/evolagent-registry.js +4 -4
- package/dist/core/evolagent.js +2 -2
- package/dist/core/inference/text-inference.js +31 -40
- package/dist/core/ipc-registration.js +6 -2
- package/dist/core/memory/consolidation-context.js +51 -5
- package/dist/core/memory/consolidation-ops-records.js +1 -1
- package/dist/core/memory/consolidation-trigger.js +189 -189
- package/dist/core/memory/consolidation.js +316 -29
- package/dist/core/memory/forget-log.js +96 -0
- package/dist/core/memory/index-manager.js +27 -5
- package/dist/core/memory/memory-admin.js +45 -5
- package/dist/core/memory/memory-writer.js +163 -18
- package/dist/core/memory/relation-idle-monitor.js +119 -58
- package/dist/core/memory/relation-sweep.js +44 -39
- package/dist/core/message/aun-ipc-sender.js +8 -2
- package/dist/core/message/im-renderer.js +7 -4
- package/dist/core/message/message-admission.js +89 -4
- package/dist/core/message/message-bridge.js +31 -18
- package/dist/core/message/response-engine.js +77 -8
- package/dist/core/model/catalog-service.js +4 -2
- package/dist/core/model/config-scope.js +24 -64
- package/dist/core/model/field-scope.js +1 -1
- package/dist/core/model/model-catalog.js +30 -41
- package/dist/core/model/model-diagnostics.js +4 -3
- package/dist/core/model/model-fallback.js +10 -7
- package/dist/core/model/model-permission.js +3 -2
- package/dist/core/ownership/owner-resolver.js +23 -21
- package/dist/core/permission/ec-command-parser.js +30 -2
- package/dist/core/permission/sandbox-runtime.js +18 -1
- package/dist/core/permission/tool-policy.js +14 -3
- package/dist/core/relation/peer-identity.js +64 -1
- package/dist/core/role/runtime-policy.js +72 -13
- package/dist/core/session/session-capsule.js +1 -1
- package/dist/core/session/session-idle-monitor.js +1 -1
- package/dist/core/session/session-manager.js +20 -0
- package/dist/core/session/session-renew.js +3 -3
- package/dist/core/skill/index-generator.js +15 -5
- package/dist/core/system-session/memory.js +6 -1
- package/dist/core/thread/thread-service.js +19 -6
- package/dist/directory/owner-directory-admission.js +95 -0
- package/dist/directory/owner-directory-authorization.js +309 -0
- package/dist/directory/owner-directory-cache.js +635 -0
- package/dist/eck/kit-renderer.js +2 -2
- package/dist/ecweb/basic-settings-service.js +33 -40
- package/dist/ecweb/message-context-service.js +110 -0
- package/dist/index.js +76 -14
- package/dist/ipc.js +170 -12
- package/dist/paths.js +4 -0
- package/dist/proxy/command.js +122 -0
- package/dist/proxy/ipc-service.js +68 -0
- package/dist/proxy/manager.js +485 -0
- package/dist/proxy/service-supervisor.js +603 -0
- package/dist/response-system/engines/v1/proactive-flow.js +16 -6
- package/dist/utils/cross-platform.js +14 -0
- package/dist/utils/ecweb-supervisor.js +258 -83
- package/dist/utils/error-utils.js +2 -2
- package/dist/utils/logger.js +24 -5
- package/dist/utils/package-version-info.js +4 -2
- package/kits/docs/channels/aun.md +9 -0
- package/kits/docs/evolcore/agent.md +1 -1
- package/kits/docs/evolcore/config.md +17 -6
- package/kits/docs/evolcore/contact.md +3 -0
- package/kits/docs/evolcore/fs.md +11 -0
- package/kits/docs/evolcore/memory-architecture.md +2 -2
- package/kits/docs/evolcore/memory-consolidation.md +32 -8
- package/kits/docs/evolcore/memory.md +1 -1
- package/kits/docs/evolcore/menu-protocol.md +4 -0
- package/kits/docs/evolcore/model.md +23 -20
- package/kits/docs/evolcore/msg.md +4 -7
- package/kits/migrations/migrate-contact-book-v2.mjs +30 -11
- package/kits/schemas/_meta.json +37 -4
- package/kits/schemas/agent-config.schema.13.json +8 -3
- package/kits/schemas/agent-config.schema.16.json +891 -0
- package/kits/schemas/daemon.schema.9.json +366 -0
- package/kits/schemas/defaults.schema.5.json +406 -127
- package/kits/schemas/relation-config.schema.11.json +260 -0
- package/kits/schemas/role-config.schema.1.json +7 -7
- package/kits/skills/.switches.json +3 -7
- package/kits/skills/ec-service-proxy/SKILL.md +118 -0
- package/kits/skills/ec-trigger/SKILL.md +13 -5
- package/kits/skills/personal-assistant/SKILL.md +176 -0
- package/kits/skills/personal-assistant/references/01-workspace-spec.md +333 -0
- package/kits/skills/personal-assistant/references/02-bootstrap.md +205 -0
- package/kits/skills/personal-assistant/references/03-data-flow-and-gaps.md +177 -0
- package/kits/skills/personal-assistant/references/04-frontend-contract.md +439 -0
- package/kits/skills/personal-assistant/references/05-directory-reference.md +292 -0
- package/kits/skills/personal-assistant/references/06-render-config.md +350 -0
- package/kits/skills/personal-assistant/schema/workspace.schema.json +85 -0
- package/kits/skills/personal-assistant/schema/workspace.v1.json +66 -0
- package/kits/skills/personal-assistant/scripts/archive.mjs +154 -0
- package/kits/skills/personal-assistant/scripts/check.mjs +114 -0
- package/kits/skills/personal-assistant/scripts/index-rebuild.mjs +252 -0
- package/kits/skills/personal-assistant/scripts/ingest.mjs +344 -0
- package/kits/skills/personal-assistant/scripts/init.mjs +270 -0
- package/kits/skills/personal-assistant/scripts/lib/apply.mjs +92 -0
- package/kits/skills/personal-assistant/scripts/lib/schema.mjs +96 -0
- package/kits/skills/personal-assistant/scripts/lib/time.mjs +28 -0
- package/kits/skills/personal-assistant/scripts/migrate.mjs +172 -0
- package/kits/skills/personal-assistant/scripts/search.mjs +172 -0
- package/kits/skills/personal-assistant/scripts/verify.mjs +196 -0
- package/kits/skills/personal-assistant/templates/INDEX.md.tpl +24 -0
- package/kits/skills/personal-assistant/templates/README.md.tpl +108 -0
- package/kits/skills/personal-assistant/triggers/ingest-daily.json +23 -0
- package/kits/skills/personal-assistant/triggers/memory-consolidate.json +23 -0
- package/kits/skills/personal-assistant/triggers/report-daily.json +23 -0
- package/kits/skills/personal-assistant/triggers/report-weekly.json +23 -0
- package/kits/templates/memory-consolidation/forget.md +19 -6
- package/kits/templates/memory-consolidation/notes.md +1 -1
- package/kits/templates/memory-consolidation/profile.md +2 -2
- package/kits/templates/memory-consolidation/promote.md +20 -3
- package/kits/templates/roles/member.json +8 -1
- package/kits/templates/system-fragments/baseagent.md +2 -2
- package/kits/templates/system-fragments/memory.md +4 -4
- package/package.json +6 -2
|
@@ -0,0 +1,85 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json-schema.org/draft/2020-12/schema",
|
|
3
|
+
"$id": "https://evolcore.local/schemas/personal-assistant/workspace.schema.json",
|
|
4
|
+
"title": "个人助理工作区结构定义",
|
|
5
|
+
"description": "定义工作区在某个版本下应有的目录结构。check 脚本用它比对实际工作区;migrate 脚本用它推导差距。",
|
|
6
|
+
"type": "object",
|
|
7
|
+
"required": ["workspace_version", "roots", "directories", "files"],
|
|
8
|
+
"properties": {
|
|
9
|
+
"workspace_version": {
|
|
10
|
+
"type": "integer",
|
|
11
|
+
"minimum": 1,
|
|
12
|
+
"description": "本 schema 描述的工作区版本。与 .assistant/state.json 里的 workspace_version 比对,判断是否需要迁移。"
|
|
13
|
+
},
|
|
14
|
+
"roots": {
|
|
15
|
+
"type": "array",
|
|
16
|
+
"description": "工作区根目录下必须存在的条目(不含 .assistant,它由 directories 覆盖)",
|
|
17
|
+
"items": { "type": "string" }
|
|
18
|
+
},
|
|
19
|
+
"directories": {
|
|
20
|
+
"type": "array",
|
|
21
|
+
"description": "必须存在的目录,路径相对工作区根。目录不存在的按 MISSING 处理,可以补建。",
|
|
22
|
+
"items": {
|
|
23
|
+
"type": "object",
|
|
24
|
+
"required": ["path", "purpose"],
|
|
25
|
+
"properties": {
|
|
26
|
+
"path": { "type": "string" },
|
|
27
|
+
"purpose": { "type": "string", "description": "这个目录装什么。check 报告 MISSING 时会附上它。" },
|
|
28
|
+
"layer": {
|
|
29
|
+
"type": "string",
|
|
30
|
+
"enum": ["capture", "output", "runtime"],
|
|
31
|
+
"description": "所属层。capture 只增不改;output 可覆盖可重建;runtime 可删可重建。"
|
|
32
|
+
},
|
|
33
|
+
"archive_after_months": {
|
|
34
|
+
"type": ["integer", "null"],
|
|
35
|
+
"description": "超过多少个月归档。null 表示永不归档。"
|
|
36
|
+
},
|
|
37
|
+
"required": {
|
|
38
|
+
"type": "boolean",
|
|
39
|
+
"default": true,
|
|
40
|
+
"description": "false 表示该目录是可选的(如依赖不可用能力的分支),缺失只报告不补建。"
|
|
41
|
+
},
|
|
42
|
+
"gated_by": {
|
|
43
|
+
"type": "string",
|
|
44
|
+
"description": "可选目录的依赖能力名,对应 state.json 的 capabilities。<能力为 false 时不要求存在。"
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
},
|
|
49
|
+
"files": {
|
|
50
|
+
"type": "array",
|
|
51
|
+
"description": "必须存在的文件。缺失时从 templates 重建。",
|
|
52
|
+
"items": {
|
|
53
|
+
"type": "object",
|
|
54
|
+
"required": ["path"],
|
|
55
|
+
"properties": {
|
|
56
|
+
"path": { "type": "string" },
|
|
57
|
+
"template": { "type": "string", "description": "缺失时用于重建的模板,相对技能目录" },
|
|
58
|
+
"generated": {
|
|
59
|
+
"type": "boolean",
|
|
60
|
+
"default": false,
|
|
61
|
+
"description": "true 表示由脚本生成(如 INDEX.md),缺失可直接重建且内容可覆盖"
|
|
62
|
+
},
|
|
63
|
+
"never_delete": {
|
|
64
|
+
"type": "boolean",
|
|
65
|
+
"default": false,
|
|
66
|
+
"description": "true 表示这是数据而非缓存,迁移和清理都不得删除"
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
},
|
|
71
|
+
"naming": {
|
|
72
|
+
"type": "object",
|
|
73
|
+
"description": "命名规则。check 只做抽样校验,不做全量扫描(全量在校验脚本里)。",
|
|
74
|
+
"properties": {
|
|
75
|
+
"file_pattern": { "type": "string" },
|
|
76
|
+
"date_dir_pattern": { "type": "string" }
|
|
77
|
+
}
|
|
78
|
+
},
|
|
79
|
+
"frontmatter_required": {
|
|
80
|
+
"type": "array",
|
|
81
|
+
"description": "所有 md 必须带的 frontmatter 字段",
|
|
82
|
+
"items": { "type": "string" }
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
{
|
|
2
|
+
"workspace_version": 1,
|
|
3
|
+
"roots": ["README.md", "INDEX.md", "capture", "output", ".assistant"],
|
|
4
|
+
"directories": [
|
|
5
|
+
{ "path": "capture", "purpose": "原料层:外部给的与系统采集的,只增不改", "layer": "capture" },
|
|
6
|
+
|
|
7
|
+
{ "path": "capture/conversation", "purpose": "对话正文(md + jsonl 双份)", "layer": "capture", "archive_after_months": 3 },
|
|
8
|
+
{ "path": "capture/notes", "purpose": "用户口述的想法与随笔", "layer": "capture", "archive_after_months": 3 },
|
|
9
|
+
{ "path": "capture/links", "purpose": "转发的链接与文章(含摘要)", "layer": "capture", "archive_after_months": 3 },
|
|
10
|
+
{ "path": "capture/files", "purpose": "收到的文件原件", "layer": "capture", "archive_after_months": 3 },
|
|
11
|
+
{ "path": "capture/scene", "purpose": "看现场:照片引用 + 位置 + 描述", "layer": "capture", "archive_after_months": 3 },
|
|
12
|
+
{ "path": "capture/listening", "purpose": "非会议的持续聆听(转写 + 音频引用)", "layer": "capture", "archive_after_months": 3 },
|
|
13
|
+
{ "path": "capture/meeting", "purpose": "会议原料(转写、参与人、音频引用)", "layer": "capture", "archive_after_months": 3 },
|
|
14
|
+
{ "path": "capture/track", "purpose": "行为轨迹:位置、应用使用、设备、通讯", "layer": "capture", "archive_after_months": 3, "required": false, "gated_by": "observation_ingest" },
|
|
15
|
+
|
|
16
|
+
{ "path": "capture/_manifest", "purpose": "落地清单:文件数、字节数、sha256。这是数据不是缓存,永不删除", "layer": "capture", "archive_after_months": null },
|
|
17
|
+
{ "path": "capture/_inbox", "purpose": "兜底暂存,48 小时内必须归位", "layer": "capture", "archive_after_months": null },
|
|
18
|
+
{ "path": "capture/_archive", "purpose": "原料冷归档", "layer": "capture", "archive_after_months": null },
|
|
19
|
+
|
|
20
|
+
{ "path": "output", "purpose": "产出层:提炼的、生成的、可重建的", "layer": "output" },
|
|
21
|
+
|
|
22
|
+
{ "path": "output/reports", "purpose": "周期报告", "layer": "output" },
|
|
23
|
+
{ "path": "output/reports/daily", "purpose": "日报", "layer": "output", "archive_after_months": 3 },
|
|
24
|
+
{ "path": "output/reports/weekly", "purpose": "周报", "layer": "output", "archive_after_months": 3 },
|
|
25
|
+
{ "path": "output/reports/monthly", "purpose": "月报", "layer": "output", "archive_after_months": 3 },
|
|
26
|
+
{ "path": "output/reports/quarterly", "purpose": "季报", "layer": "output", "archive_after_months": 12 },
|
|
27
|
+
{ "path": "output/reports/yearly", "purpose": "年报", "layer": "output", "archive_after_months": null },
|
|
28
|
+
|
|
29
|
+
{ "path": "output/meetings", "purpose": "会议总结(对应 capture/meeting 的成品)", "layer": "output", "archive_after_months": 3 },
|
|
30
|
+
|
|
31
|
+
{ "path": "output/tasks", "purpose": "任务:唯一事实源", "layer": "output" },
|
|
32
|
+
{ "path": "output/tasks/done", "purpose": "已完成任务,按月归档", "layer": "output", "archive_after_months": 3 },
|
|
33
|
+
|
|
34
|
+
{ "path": "output/knowledge", "purpose": "长期沉淀,永不归档", "layer": "output", "archive_after_months": null },
|
|
35
|
+
{ "path": "output/knowledge/memory", "purpose": "记忆条目", "layer": "output", "archive_after_months": null },
|
|
36
|
+
{ "path": "output/knowledge/memory/rules", "purpose": "规则与偏好,最高优先级、永不过期", "layer": "output", "archive_after_months": null },
|
|
37
|
+
{ "path": "output/knowledge/memory/facts", "purpose": "事实条目,一条一文件", "layer": "output", "archive_after_months": null },
|
|
38
|
+
{ "path": "output/knowledge/memory/entities", "purpose": "实体记忆", "layer": "output", "archive_after_months": null },
|
|
39
|
+
{ "path": "output/knowledge/memory/entities/people", "purpose": "人物实体", "layer": "output", "archive_after_months": null },
|
|
40
|
+
{ "path": "output/knowledge/memory/entities/org", "purpose": "组织实体", "layer": "output", "archive_after_months": null },
|
|
41
|
+
{ "path": "output/knowledge/memory/episodes", "purpose": "事件记忆,按天", "layer": "output", "archive_after_months": null },
|
|
42
|
+
{ "path": "output/knowledge/profile", "purpose": "画像", "layer": "output", "archive_after_months": null },
|
|
43
|
+
{ "path": "output/knowledge/profile/people", "purpose": "他人档案", "layer": "output", "archive_after_months": null },
|
|
44
|
+
{ "path": "output/knowledge/profile/org", "purpose": "组织档案", "layer": "output", "archive_after_months": null },
|
|
45
|
+
{ "path": "output/knowledge/topics", "purpose": "主题梳理", "layer": "output", "archive_after_months": null },
|
|
46
|
+
|
|
47
|
+
{ "path": "output/_archive", "purpose": "产物冷归档", "layer": "output", "archive_after_months": null },
|
|
48
|
+
|
|
49
|
+
{ "path": ".assistant", "purpose": "运行时状态,可删可重建", "layer": "runtime" },
|
|
50
|
+
{ "path": ".assistant/index", "purpose": "分片索引", "layer": "runtime" },
|
|
51
|
+
{ "path": ".assistant/logs", "purpose": "运行日志,30 天自动清", "layer": "runtime" },
|
|
52
|
+
{ "path": ".assistant/tmp", "purpose": "临时文件,7 天自动清", "layer": "runtime" },
|
|
53
|
+
{ "path": ".assistant/locks", "purpose": "写操作互斥锁", "layer": "runtime" },
|
|
54
|
+
{ "path": ".assistant/cache", "purpose": "解析缓存", "layer": "runtime" }
|
|
55
|
+
],
|
|
56
|
+
"files": [
|
|
57
|
+
{ "path": "README.md", "template": "templates/README.md.tpl", "generated": true },
|
|
58
|
+
{ "path": "INDEX.md", "template": "templates/INDEX.md.tpl", "generated": true },
|
|
59
|
+
{ "path": ".assistant/state.json", "generated": true }
|
|
60
|
+
],
|
|
61
|
+
"naming": {
|
|
62
|
+
"file_pattern": "^\\d{4}-\\d{2}-\\d{2}-\\d{4}-[^/\\\\]{1,40}\\.md$",
|
|
63
|
+
"date_dir_pattern": "^\\d{4}/{1,2}(/\\d{1,2})?$"
|
|
64
|
+
},
|
|
65
|
+
"frontmatter_required": ["id", "date", "type", "tags", "source"]
|
|
66
|
+
}
|
|
@@ -0,0 +1,154 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* archive.mjs —— 冷热分层归档
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* node "<技能目录>/scripts/archive.mjs" --workspace "<路径>" [--apply] [--json]
|
|
7
|
+
*
|
|
8
|
+
* 默认 dry-run,只报告会移动什么;加 --apply 才真的移动。
|
|
9
|
+
*
|
|
10
|
+
* 规则(全部来自 schema 的 archive_after_months):
|
|
11
|
+
* capture/X → capture/_archive/X/... (保留原相对路径,便于追溯)
|
|
12
|
+
* output/X → output/_archive/X/...
|
|
13
|
+
* archive_after_months 为 null 的目录永不归档(knowledge/ 就是这类)
|
|
14
|
+
*
|
|
15
|
+
* 归档只移动位置,不改内容。移动后需要重跑 index-rebuild。
|
|
16
|
+
*/
|
|
17
|
+
import { existsSync, readdirSync, statSync, mkdirSync, renameSync, readFileSync } from 'node:fs';
|
|
18
|
+
import { join, resolve, relative, dirname, sep } from 'node:path';
|
|
19
|
+
import { latestVersion, loadSchema } from './lib/schema.mjs';
|
|
20
|
+
import { parseDate } from './lib/time.mjs';
|
|
21
|
+
|
|
22
|
+
function parseArgs(argv) {
|
|
23
|
+
const out = { json: false, apply: false };
|
|
24
|
+
for (let i = 2; i < argv.length; i++) {
|
|
25
|
+
const a = argv[i];
|
|
26
|
+
if (a === '--workspace' || a === '-w') out.workspace = argv[++i];
|
|
27
|
+
else if (a === '--apply') out.apply = true;
|
|
28
|
+
else if (a === '--json') out.json = true;
|
|
29
|
+
else if (a === '--help' || a === '-h') out.help = true;
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const args = parseArgs(process.argv);
|
|
35
|
+
if (args.help || !args.workspace) {
|
|
36
|
+
console.log(`用法:node archive.mjs --workspace <路径> [--apply] [--json]
|
|
37
|
+
|
|
38
|
+
默认只报告(dry-run)。加 --apply 才真正移动文件。`);
|
|
39
|
+
process.exit(args.help ? 0 : 30);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const workspace = resolve(args.workspace);
|
|
43
|
+
if (!existsSync(workspace)) {
|
|
44
|
+
console.error(`❌ 工作区不存在:${workspace}`);
|
|
45
|
+
process.exit(30);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
const schema = loadSchema(latestVersion());
|
|
49
|
+
|
|
50
|
+
// 只处理明确了归档阈值的目录
|
|
51
|
+
const rules = schema.directories.filter(d => d.archive_after_months != null);
|
|
52
|
+
|
|
53
|
+
function readDate(file) {
|
|
54
|
+
try {
|
|
55
|
+
const head = readFileSync(file, 'utf8').slice(0, 800);
|
|
56
|
+
const m = /^date:\s*(.+)$/m.exec(head);
|
|
57
|
+
if (m) {
|
|
58
|
+
const d = parseDate(m[1].trim().replace(/^["']|["']$/g, ''));
|
|
59
|
+
if (d) return d;
|
|
60
|
+
}
|
|
61
|
+
} catch { /* 二进制或不可读,退回 mtime */ }
|
|
62
|
+
return statSync(file).mtime;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function walk(dir, out = []) {
|
|
66
|
+
if (!existsSync(dir)) return out;
|
|
67
|
+
for (const e of readdirSync(dir, { withFileTypes: true })) {
|
|
68
|
+
if (e.name.startsWith('_')) continue; // 跳过 _archive/_manifest/_inbox
|
|
69
|
+
const abs = join(dir, e.name);
|
|
70
|
+
if (e.isDirectory()) walk(abs, out);
|
|
71
|
+
else out.push(abs);
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
const plan = [];
|
|
77
|
+
const now = Date.now();
|
|
78
|
+
|
|
79
|
+
for (const rule of rules) {
|
|
80
|
+
const abs = join(workspace, rule.path);
|
|
81
|
+
if (!existsSync(abs)) continue;
|
|
82
|
+
|
|
83
|
+
const cutoff = new Date(now);
|
|
84
|
+
cutoff.setMonth(cutoff.getMonth() - rule.archive_after_months);
|
|
85
|
+
|
|
86
|
+
const layerRoot = rule.layer === 'capture' ? 'capture' : 'output';
|
|
87
|
+
const archiveRoot = join(workspace, layerRoot, '_archive');
|
|
88
|
+
|
|
89
|
+
for (const file of walk(abs)) {
|
|
90
|
+
const d = readDate(file);
|
|
91
|
+
if (d >= cutoff) continue;
|
|
92
|
+
const rel = relative(workspace, file).split(sep).join('/');
|
|
93
|
+
const targetRel = join(layerRoot, '_archive', rel.slice(layerRoot.length + 1));
|
|
94
|
+
plan.push({ from: rel, to: targetRel.split(sep).join('/'), date: d.toISOString().slice(0, 10) });
|
|
95
|
+
}
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
// ── 执行 ────────────────────────────────────────────────────────
|
|
99
|
+
let moved = 0;
|
|
100
|
+
const errors = [];
|
|
101
|
+
if (args.apply) {
|
|
102
|
+
for (const p of plan) {
|
|
103
|
+
try {
|
|
104
|
+
const from = join(workspace, p.from);
|
|
105
|
+
const to = join(workspace, p.to);
|
|
106
|
+
mkdirSync(dirname(to), { recursive: true });
|
|
107
|
+
renameSync(from, to);
|
|
108
|
+
moved++;
|
|
109
|
+
} catch (e) {
|
|
110
|
+
errors.push({ path: p.from, reason: e.message });
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
if (args.json) {
|
|
116
|
+
console.log(JSON.stringify({
|
|
117
|
+
ok: !errors.length,
|
|
118
|
+
workspace,
|
|
119
|
+
mode: args.apply ? 'apply' : 'dry-run',
|
|
120
|
+
planned: plan.length,
|
|
121
|
+
moved,
|
|
122
|
+
files: plan,
|
|
123
|
+
errors,
|
|
124
|
+
}, null, 2));
|
|
125
|
+
process.exit(errors.length ? 20 : 0);
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
console.log(`工作区:${workspace}`);
|
|
129
|
+
console.log(`模式:${args.apply ? '执行' : 'dry-run(只报告,加 --apply 才移动)'}`);
|
|
130
|
+
console.log('');
|
|
131
|
+
|
|
132
|
+
if (!plan.length) {
|
|
133
|
+
console.log('✅ 没有需要归档的文件');
|
|
134
|
+
process.exit(0);
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
console.log(`待归档 ${plan.length} 项:`);
|
|
138
|
+
for (const p of plan.slice(0, 20)) {
|
|
139
|
+
console.log(` · ${p.from} → ${p.to} (${p.date})`);
|
|
140
|
+
}
|
|
141
|
+
if (plan.length > 20) console.log(` …… 还有 ${plan.length - 20} 项`);
|
|
142
|
+
|
|
143
|
+
if (errors.length) {
|
|
144
|
+
console.log('');
|
|
145
|
+
console.log(`⚠️ ${errors.length} 项失败:`);
|
|
146
|
+
for (const e of errors) console.log(` · ${e.path}:${e.reason}`);
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
console.log('');
|
|
150
|
+
if (args.apply) {
|
|
151
|
+
console.log(`✅ 已移动 ${moved} 项。建议接着重跑 index-rebuild 更新索引。`);
|
|
152
|
+
} else {
|
|
153
|
+
console.log('确认无误后加 --apply 执行。');
|
|
154
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* check.mjs —— 工作区体检(只读,永不写文件)
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* node "<技能目录>/scripts/check.mjs" --workspace "<工作区绝对路径>" [--json]
|
|
7
|
+
*
|
|
8
|
+
* 输出四类结论:
|
|
9
|
+
* OK 与当前 schema 一致
|
|
10
|
+
* MISSING schema 要求但工作区没有(可安全补建)
|
|
11
|
+
* EXTRA 工作区有但 schema 没定义(不动,只报告)
|
|
12
|
+
* DRIFT 版本落后,需要迁移
|
|
13
|
+
*
|
|
14
|
+
* 退出码:0 完全一致 / 10 有 MISSING(可修复)/ 20 需要 DRIFT 迁移 / 30 前置条件不满足
|
|
15
|
+
*/
|
|
16
|
+
import { existsSync } from 'node:fs';
|
|
17
|
+
import { resolve } from 'node:path';
|
|
18
|
+
import {
|
|
19
|
+
loadSchema, latestVersion, readWorkspaceVersion,
|
|
20
|
+
readCapabilities, diff,
|
|
21
|
+
} from './lib/schema.mjs';
|
|
22
|
+
|
|
23
|
+
function parseArgs(argv) {
|
|
24
|
+
const out = { json: false };
|
|
25
|
+
for (let i = 2; i < argv.length; i++) {
|
|
26
|
+
const a = argv[i];
|
|
27
|
+
if (a === '--workspace' || a === '-w') out.workspace = argv[++i];
|
|
28
|
+
else if (a === '--json') out.json = true;
|
|
29
|
+
else if (a === '--help' || a === '-h') out.help = true;
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const args = parseArgs(process.argv);
|
|
35
|
+
|
|
36
|
+
if (args.help || !args.workspace) {
|
|
37
|
+
console.log(`用法:node check.mjs --workspace <路径> [--json]
|
|
38
|
+
|
|
39
|
+
只读体检,判断工作区结构与当前 schema 的差距。永不写文件。`);
|
|
40
|
+
process.exit(args.help ? 0 : 30);
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
const workspace = resolve(args.workspace);
|
|
44
|
+
|
|
45
|
+
if (!existsSync(workspace)) {
|
|
46
|
+
const msg = `工作区不存在:${workspace}`;
|
|
47
|
+
if (args.json) console.log(JSON.stringify({ ok: false, error: msg }, null, 2));
|
|
48
|
+
else console.error(`❌ ${msg}`);
|
|
49
|
+
process.exit(30);
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
const targetVersion = latestVersion();
|
|
53
|
+
const currentVersion = readWorkspaceVersion(workspace);
|
|
54
|
+
const capabilities = readCapabilities(workspace);
|
|
55
|
+
|
|
56
|
+
// 工作区从未初始化(没有 state.json)
|
|
57
|
+
const isNew = currentVersion === 0;
|
|
58
|
+
const schema = loadSchema(isNew ? targetVersion : currentVersion);
|
|
59
|
+
const result = diff(workspace, schema, { capabilities });
|
|
60
|
+
|
|
61
|
+
const drifted = currentVersion < targetVersion;
|
|
62
|
+
const exitCode = drifted ? 20 : (result.missing.length ? 10 : 0);
|
|
63
|
+
|
|
64
|
+
if (args.json) {
|
|
65
|
+
console.log(JSON.stringify({
|
|
66
|
+
ok: exitCode === 0,
|
|
67
|
+
workspace,
|
|
68
|
+
version: { current: currentVersion, target: targetVersion, drifted },
|
|
69
|
+
capabilities,
|
|
70
|
+
summary: {
|
|
71
|
+
ok: result.ok.length,
|
|
72
|
+
missing: result.missing.length,
|
|
73
|
+
extra: result.extra.length,
|
|
74
|
+
},
|
|
75
|
+
missing: result.missing,
|
|
76
|
+
extra: result.extra,
|
|
77
|
+
}, null, 2));
|
|
78
|
+
process.exit(exitCode);
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// 人类可读输出
|
|
82
|
+
console.log(`工作区:${workspace}`);
|
|
83
|
+
console.log(`版本:当前 v${currentVersion || '未初始化'} / 目标 v${targetVersion}`);
|
|
84
|
+
console.log('');
|
|
85
|
+
|
|
86
|
+
if (drifted) {
|
|
87
|
+
console.log(`⚠️ DRIFT:工作区落后 ${targetVersion - currentVersion} 个版本,需要迁移`);
|
|
88
|
+
console.log(` 执行:node migrate.mjs --workspace "${workspace}"`);
|
|
89
|
+
console.log('');
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
if (result.missing.length) {
|
|
93
|
+
console.log(`MISSING(${result.missing.length} 项,可补建):`);
|
|
94
|
+
for (const m of result.missing) {
|
|
95
|
+
const tag = m.kind === 'dir' ? '目录' : '文件';
|
|
96
|
+
const why = m.purpose ? ` — ${m.purpose}` : (m.generated ? ' — 生成物,可重建' : '');
|
|
97
|
+
console.log(` · [${tag}] ${m.path}${why}`);
|
|
98
|
+
}
|
|
99
|
+
console.log('');
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
if (result.extra.length) {
|
|
103
|
+
console.log(`EXTRA(${result.extra.length} 项,schema 未定义,不会动它):`);
|
|
104
|
+
for (const e of result.extra) {
|
|
105
|
+
console.log(` · [${e.kind === 'dir' ? '目录' : '文件'}] ${e.path}`);
|
|
106
|
+
}
|
|
107
|
+
console.log('');
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
if (exitCode === 0) {
|
|
111
|
+
console.log(`✅ OK:${result.ok.length} 项全部符合 v${targetVersion}`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
process.exit(exitCode);
|
|
@@ -0,0 +1,252 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
/**
|
|
3
|
+
* index-rebuild.mjs —— 从 frontmatter 重建全部索引
|
|
4
|
+
*
|
|
5
|
+
* 用法:
|
|
6
|
+
* node "<技能目录>/scripts/index-rebuild.mjs" --workspace "<路径>" [--all] [--json]
|
|
7
|
+
*
|
|
8
|
+
* 生成:
|
|
9
|
+
* INDEX.md 最近 90 天的条目(人读入口)
|
|
10
|
+
* .assistant/index/tags.md 标签反查
|
|
11
|
+
* .assistant/index/by-source.md 来源反查
|
|
12
|
+
* .assistant/index/archive/YYYY-MM.md 按月归档分片
|
|
13
|
+
*
|
|
14
|
+
* --all 不按 90 天截断,INDEX.md 收录全部
|
|
15
|
+
*
|
|
16
|
+
* 索引是可重建的:任何时候删掉 INDEX.md 和 .assistant/index/,重跑即可恢复。
|
|
17
|
+
*/
|
|
18
|
+
import { existsSync, readdirSync, readFileSync, writeFileSync, mkdirSync, statSync } from 'node:fs';
|
|
19
|
+
import { join, resolve, relative, sep } from 'node:path';
|
|
20
|
+
import { nowLocal, parseDate } from './lib/time.mjs';
|
|
21
|
+
import { readState } from './lib/apply.mjs';
|
|
22
|
+
|
|
23
|
+
function parseArgs(argv) {
|
|
24
|
+
const out = { json: false, all: false, window: 90 };
|
|
25
|
+
for (let i = 2; i < argv.length; i++) {
|
|
26
|
+
const a = argv[i];
|
|
27
|
+
if (a === '--workspace' || a === '-w') out.workspace = argv[++i];
|
|
28
|
+
else if (a === '--all') out.all = true;
|
|
29
|
+
else if (a === '--window') out.window = Number(argv[++i]) || 90;
|
|
30
|
+
else if (a === '--json') out.json = true;
|
|
31
|
+
else if (a === '--help' || a === '-h') out.help = true;
|
|
32
|
+
}
|
|
33
|
+
return out;
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
const args = parseArgs(process.argv);
|
|
37
|
+
if (args.help || !args.workspace) {
|
|
38
|
+
console.log(`用法:node index-rebuild.mjs --workspace <路径> [--all] [--window 天数] [--json]`);
|
|
39
|
+
process.exit(args.help ? 0 : 30);
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
const workspace = resolve(args.workspace);
|
|
43
|
+
if (!existsSync(workspace)) {
|
|
44
|
+
console.error(`❌ 工作区不存在:${workspace}`);
|
|
45
|
+
process.exit(30);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
// ── frontmatter 解析(不引 yaml 依赖,只认键值对) ──────────────
|
|
49
|
+
function readFrontmatter(file) {
|
|
50
|
+
let text;
|
|
51
|
+
try {
|
|
52
|
+
text = readFileSync(file, 'utf8');
|
|
53
|
+
} catch {
|
|
54
|
+
return null;
|
|
55
|
+
}
|
|
56
|
+
if (!text.startsWith('---')) return null;
|
|
57
|
+
const end = text.indexOf('\n---', 3);
|
|
58
|
+
if (end < 0) return null;
|
|
59
|
+
const block = text.slice(3, end);
|
|
60
|
+
const fm = {};
|
|
61
|
+
for (const line of block.split(/\r?\n/)) {
|
|
62
|
+
const m = /^([A-Za-z_][\w-]*):\s*(.*)$/.exec(line.trim());
|
|
63
|
+
if (!m) continue;
|
|
64
|
+
let v = m[2].trim();
|
|
65
|
+
if (v.startsWith('[') && v.endsWith(']')) {
|
|
66
|
+
v = v.slice(1, -1).split(',').map(s => s.trim().replace(/^["']|["']$/g, '')).filter(Boolean);
|
|
67
|
+
} else {
|
|
68
|
+
v = v.replace(/^["']|["']$/g, '');
|
|
69
|
+
}
|
|
70
|
+
fm[m[1]] = v;
|
|
71
|
+
}
|
|
72
|
+
return fm;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
// ── 扫描 ────────────────────────────────────────────────────────
|
|
76
|
+
const SCAN_ROOTS = ['capture', 'output'];
|
|
77
|
+
const SKIP_DIRS = new Set(['_archive', 'node_modules']);
|
|
78
|
+
|
|
79
|
+
function walk(dir, out = []) {
|
|
80
|
+
if (!existsSync(dir)) return out;
|
|
81
|
+
for (const e of readdirSync(dir, { withFileTypes: true })) {
|
|
82
|
+
const abs = join(dir, e.name);
|
|
83
|
+
if (e.isDirectory()) {
|
|
84
|
+
if (SKIP_DIRS.has(e.name)) continue;
|
|
85
|
+
walk(abs, out);
|
|
86
|
+
} else if (e.name.endsWith('.md') && !e.name.startsWith('.')) {
|
|
87
|
+
out.push(abs);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
return out;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
const entries = [];
|
|
94
|
+
for (const root of SCAN_ROOTS) {
|
|
95
|
+
for (const file of walk(join(workspace, root))) {
|
|
96
|
+
const fm = readFrontmatter(file);
|
|
97
|
+
const st = statSync(file);
|
|
98
|
+
const rel = relative(workspace, file).split(sep).join('/');
|
|
99
|
+
const date = parseDate(fm?.date) || st.mtime;
|
|
100
|
+
entries.push({
|
|
101
|
+
path: rel,
|
|
102
|
+
id: fm?.id || '',
|
|
103
|
+
title: fm?.id || rel.split('/').pop().replace(/\.md$/, ''),
|
|
104
|
+
type: fm?.type || 'unknown',
|
|
105
|
+
tags: Array.isArray(fm?.tags) ? fm.tags : (fm?.tags ? [fm.tags] : []),
|
|
106
|
+
source: fm?.source || '',
|
|
107
|
+
date,
|
|
108
|
+
mtime: st.mtime,
|
|
109
|
+
hasFrontmatter: !!fm,
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
entries.sort((a, b) => b.date - a.date);
|
|
114
|
+
|
|
115
|
+
// ── 生成 ────────────────────────────────────────────────────────
|
|
116
|
+
const indexDir = join(workspace, '.assistant', 'index');
|
|
117
|
+
const archiveDir = join(indexDir, 'archive');
|
|
118
|
+
mkdirSync(archiveDir, { recursive: true });
|
|
119
|
+
|
|
120
|
+
const fmtDate = d => {
|
|
121
|
+
const pad = n => String(n).padStart(2, '0');
|
|
122
|
+
return `${d.getFullYear()}-${pad(d.getMonth() + 1)}-${pad(d.getDate())}`;
|
|
123
|
+
};
|
|
124
|
+
const md = s => String(s || '').replace(/\|/g, '\\|');
|
|
125
|
+
const short = (s, n) => (s.length > n ? s.slice(0, n) + '…' : s);
|
|
126
|
+
|
|
127
|
+
// INDEX.md
|
|
128
|
+
const cutoff = new Date(Date.now() - args.window * 86400_000);
|
|
129
|
+
const recent = args.all ? entries : entries.filter(e => e.date >= cutoff);
|
|
130
|
+
const older = args.all ? [] : entries.filter(e => e.date < cutoff);
|
|
131
|
+
|
|
132
|
+
const state = readState(workspace) || {};
|
|
133
|
+
const wsName = workspace.split(/[\\/]/).filter(Boolean).pop();
|
|
134
|
+
|
|
135
|
+
const lines = [
|
|
136
|
+
`# ${wsName} · 总索引`,
|
|
137
|
+
'',
|
|
138
|
+
'> 本文件由 `scripts/index-rebuild.mjs` 从各文件的 frontmatter 自动生成。',
|
|
139
|
+
'> **不要手工编辑**——下次重建会被覆盖。要加记录,去 `capture/` 或 `output/` 里写文件。',
|
|
140
|
+
'',
|
|
141
|
+
`- 工作区:\`${wsName}\``,
|
|
142
|
+
`- 结构版本:v${state.workspace_version || '?'}`,
|
|
143
|
+
`- 更新时间:${nowLocal()}`,
|
|
144
|
+
`- 收录窗口:${args.all ? '全部' : `最近 ${args.window} 天`}`,
|
|
145
|
+
'',
|
|
146
|
+
'---',
|
|
147
|
+
'',
|
|
148
|
+
'## 条目(最新在上)',
|
|
149
|
+
'',
|
|
150
|
+
'| 日期 | 类型 | 标题 | 标签 | 来源 | 文件 |',
|
|
151
|
+
'|------|------|------|------|------|------|',
|
|
152
|
+
...recent.map(e => `| ${fmtDate(e.date)} | ${e.type} | ${md(short(e.title, 40))} | ${md(e.tags.join(' '))} | ${md(short(e.source, 20))} | [${md(e.path.split('/').pop())}](${encodeURI(e.path)}) |`),
|
|
153
|
+
'',
|
|
154
|
+
];
|
|
155
|
+
|
|
156
|
+
if (older.length) {
|
|
157
|
+
lines.push('---', '', '## 归档', '');
|
|
158
|
+
const byMonth = new Map();
|
|
159
|
+
for (const e of older) {
|
|
160
|
+
const k = `${e.date.getFullYear()}-${String(e.date.getMonth() + 1).padStart(2, '0')}`;
|
|
161
|
+
if (!byMonth.has(k)) byMonth.set(k, []);
|
|
162
|
+
byMonth.get(k).push(e);
|
|
163
|
+
}
|
|
164
|
+
for (const k of [...byMonth.keys()].sort().reverse()) {
|
|
165
|
+
lines.push(`- [${k}](./.assistant/index/archive/${k}.md) —— ${byMonth.get(k).length} 条`);
|
|
166
|
+
}
|
|
167
|
+
lines.push('');
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
lines.push('---', '*由 `personal-assistant` 技能生成*', '');
|
|
171
|
+
writeFileSync(join(workspace, 'INDEX.md'), lines.join('\n'), 'utf8');
|
|
172
|
+
|
|
173
|
+
// 归档分片:按月
|
|
174
|
+
const allByMonth = new Map();
|
|
175
|
+
for (const e of entries) {
|
|
176
|
+
const k = `${e.date.getFullYear()}-${String(e.date.getMonth() + 1).padStart(2, '0')}`;
|
|
177
|
+
if (!allByMonth.has(k)) allByMonth.set(k, []);
|
|
178
|
+
allByMonth.get(k).push(e);
|
|
179
|
+
}
|
|
180
|
+
for (const [k, list] of allByMonth) {
|
|
181
|
+
const body = [
|
|
182
|
+
`# ${k} 归档索引`,
|
|
183
|
+
'',
|
|
184
|
+
'> 由 `scripts/index-rebuild.mjs` 生成。',
|
|
185
|
+
'',
|
|
186
|
+
'| 日期 | 类型 | 标题 | 标签 | 来源 | 文件 |',
|
|
187
|
+
'|------|------|------|------|------|------|',
|
|
188
|
+
...list.map(e => `| ${fmtDate(e.date)} | ${e.type} | ${md(short(e.title, 40))} | ${md(e.tags.join(' '))} | ${md(short(e.source, 20))} | \`${md(e.path)}\` |`),
|
|
189
|
+
'',
|
|
190
|
+
];
|
|
191
|
+
writeFileSync(join(archiveDir, `${k}.md`), body.join('\n'), 'utf8');
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
// tags.md
|
|
195
|
+
const byTag = new Map();
|
|
196
|
+
for (const e of entries) {
|
|
197
|
+
for (const t of e.tags) {
|
|
198
|
+
if (!byTag.has(t)) byTag.set(t, []);
|
|
199
|
+
byTag.get(t).push(e);
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
const tagLines = ['# 标签索引', '', '> 由 `scripts/index-rebuild.mjs` 生成。', ''];
|
|
203
|
+
for (const [t, list] of [...byTag.entries()].sort((a, b) => b[1].length - a[1].length)) {
|
|
204
|
+
tagLines.push(`## ${t}(${list.length})`, '');
|
|
205
|
+
for (const e of list) tagLines.push(`- ${fmtDate(e.date)} [${short(e.title, 40)}](../../${encodeURI(e.path)})`);
|
|
206
|
+
tagLines.push('');
|
|
207
|
+
}
|
|
208
|
+
writeFileSync(join(indexDir, 'tags.md'), tagLines.join('\n'), 'utf8');
|
|
209
|
+
|
|
210
|
+
// by-source.md
|
|
211
|
+
const bySource = new Map();
|
|
212
|
+
for (const e of entries) {
|
|
213
|
+
const s = e.source || '(未标注)';
|
|
214
|
+
if (!bySource.has(s)) bySource.set(s, []);
|
|
215
|
+
bySource.get(s).push(e);
|
|
216
|
+
}
|
|
217
|
+
const srcLines = ['# 来源索引', '', '> 由 `scripts/index-rebuild.mjs` 生成。', ''];
|
|
218
|
+
for (const [s, list] of [...bySource.entries()].sort((a, b) => b[1].length - a[1].length)) {
|
|
219
|
+
srcLines.push(`## ${s}(${list.length})`, '');
|
|
220
|
+
for (const e of list) srcLines.push(`- ${fmtDate(e.date)} [${short(e.title, 40)}](../../${encodeURI(e.path)})`);
|
|
221
|
+
srcLines.push('');
|
|
222
|
+
}
|
|
223
|
+
writeFileSync(join(indexDir, 'by-source.md'), srcLines.join('\n'), 'utf8');
|
|
224
|
+
|
|
225
|
+
const noFm = entries.filter(e => !e.hasFrontmatter).map(e => e.path);
|
|
226
|
+
|
|
227
|
+
if (args.json) {
|
|
228
|
+
console.log(JSON.stringify({
|
|
229
|
+
ok: true,
|
|
230
|
+
workspace,
|
|
231
|
+
scanned: entries.length,
|
|
232
|
+
inIndex: recent.length,
|
|
233
|
+
archived: older.length,
|
|
234
|
+
tags: byTag.size,
|
|
235
|
+
sources: bySource.size,
|
|
236
|
+
missingFrontmatter: noFm,
|
|
237
|
+
}, null, 2));
|
|
238
|
+
process.exit(0);
|
|
239
|
+
}
|
|
240
|
+
|
|
241
|
+
console.log(`扫描 ${entries.length} 个 md 文件`);
|
|
242
|
+
console.log(` INDEX.md 收录 ${recent.length} 条(${args.all ? '全部' : `最近 ${args.window} 天`})`);
|
|
243
|
+
if (older.length) console.log(` 归档 ${older.length} 条,分 ${allByMonth.size} 个月`);
|
|
244
|
+
console.log(` 标签 ${byTag.size} 个,来源 ${bySource.size} 个`);
|
|
245
|
+
if (noFm.length) {
|
|
246
|
+
console.log('');
|
|
247
|
+
console.log(`⚠️ ${noFm.length} 个文件没有 frontmatter(未收进索引):`);
|
|
248
|
+
for (const p of noFm.slice(0, 10)) console.log(` · ${p}`);
|
|
249
|
+
if (noFm.length > 10) console.log(` …… 还有 ${noFm.length - 10} 个`);
|
|
250
|
+
}
|
|
251
|
+
console.log('');
|
|
252
|
+
console.log('✅ 索引已重建');
|