Repository navigation
Expand file tree
/
Copy pathknowledge-network-guide-persist.ts
More file actions
104 lines (104 loc) · 3.01 KB
/
Copy pathknowledge-network-guide-persist.ts
File metadata and controls
104 lines (104 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
/** Persist reviewed demo artifacts through the same native-document publication path. */
import { readFile, writeFile } from "node:fs/promises";
import { openDatabase } from "../packages/db/src/index.js";
import { config } from "../apps/server/src/bootstrap/config.js";
import {
saveHumanKnowledge,
reviewKnowledgeEntry,
} from "../packages/core/src/modules/knowledge/system.js";
import {
knowledgeOverviewContext,
saveKnowledgeOverview,
} from "../packages/core/src/modules/knowledge/overview.js";
const directory = "artifacts/network-guide",
state = JSON.parse(await readFile(`${directory}/state.json`, "utf8")),
db = await openDatabase(config().database);
try {
const actor = await db
.selectFrom("users")
.select(["id", "display_name", "admin"])
.where("login", "=", "admin")
.executeTakeFirstOrThrow();
let updated = 0;
for (const [key, chapter] of Object.entries(state.chapters) as any) {
const markdown = await readFile(`${directory}/${key}.md`, "utf8"),
entry = await db
.selectFrom("knowledge_entries")
.selectAll()
.where("id", "=", chapter.id)
.executeTakeFirstOrThrow();
if (entry.markdown === markdown) continue;
const draft = await saveHumanKnowledge(
db,
actor,
state.libraryId,
{
id: entry.id,
expectedRevision: entry.revision,
title: chapter.title,
path: chapter.path,
markdown,
},
"ai",
);
await reviewKnowledgeEntry(
db,
actor,
state.libraryId,
draft.id,
draft.revision,
"publish",
);
chapter.characters = markdown.length;
updated++;
}
const directories = await db
.selectFrom("knowledge_directories")
.selectAll()
.where("library_id", "=", state.libraryId)
.execute();
for (const row of directories) {
const title = row.path.split("\u001f").at(-1)!;
const context = await knowledgeOverviewContext(
db,
actor,
state.libraryId,
row.resource_id,
);
if (context.title === title) continue;
const file = `${directory}/overviews/${row.resource_id}.md`;
const body = (await readFile(file, "utf8"))
.replace(/^\s*# [^\n]*\n?/, "")
.trim();
await db
.updateTable("resources")
.set({ title })
.where("id", "=", row.resource_id)
.execute();
const latest = await knowledgeOverviewContext(
db,
actor,
state.libraryId,
row.resource_id,
);
await saveKnowledgeOverview(db, actor, state.libraryId, {
...latest,
markdown: `# ${title}\n\n${body}`,
});
await writeFile(file, `# ${title}\n\n${body}`);
}
await writeFile(`${directory}/state.json`, JSON.stringify(state, null, 2));
console.log(
JSON.stringify({
updated,
chapters: Object.keys(state.chapters).length,
overviews: directories.length,
characters: Object.values(state.chapters).reduce(
(n: number, c: any) => n + c.characters,
0,
),
}),
);
} finally {
await db.destroy();
}