@leitwerk-dev/wiki 0.0.0-stage → 0.4.0
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/README.md +165 -2
- package/dist/LICENSE +13 -0
- package/dist/chunk-VDBICFJN.js +70 -0
- package/dist/chunk-VDBICFJN.js.map +1 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +13 -0
- package/dist/index.js.map +1 -0
- package/dist/instructions.d.ts +5 -0
- package/dist/instructions.d.ts.map +1 -0
- package/dist/integration.d.ts +22 -0
- package/dist/integration.d.ts.map +1 -0
- package/dist/integration.js +39 -0
- package/dist/integration.js.map +1 -0
- package/dist/model-D8KR8Be8.d.ts +50 -0
- package/dist/model.d.ts +48 -0
- package/dist/model.d.ts.map +1 -0
- package/dist/server/index.d.ts +5 -0
- package/dist/server/index.d.ts.map +1 -0
- package/dist/server/index.js +422 -0
- package/dist/server/index.js.map +1 -0
- package/dist/server/routes.d.ts +6 -0
- package/dist/server/routes.d.ts.map +1 -0
- package/dist/server/schema.d.ts +178 -0
- package/dist/server/schema.d.ts.map +1 -0
- package/dist/server/store.d.ts +5 -0
- package/dist/server/store.d.ts.map +1 -0
- package/dist/server/test-fixture.d.ts +8 -0
- package/dist/server/test-fixture.d.ts.map +1 -0
- package/dist/server/tools.d.ts +5 -0
- package/dist/server/tools.d.ts.map +1 -0
- package/dist/store-qRZ3c4xC.d.ts +16 -0
- package/dist/store.d.ts +14 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/ui/WikiEntryEditor.svelte +156 -0
- package/dist/ui/WikiPage.svelte +334 -0
- package/dist/ui/index.d.ts +74 -0
- package/dist/ui/index.d.ts.map +1 -0
- package/dist/ui/index.js +54 -0
- package/dist/ui/index.js.map +1 -0
- package/package.json +66 -5
- package/src/index.ts +3 -0
- package/src/instructions.ts +19 -0
- package/src/integration.ts +62 -0
- package/src/model.ts +96 -0
- package/src/server/index.ts +4 -0
- package/src/server/routes.ts +87 -0
- package/src/server/schema.ts +35 -0
- package/src/server/store.ts +201 -0
- package/src/server/test-fixture.ts +37 -0
- package/src/server/tools.ts +197 -0
- package/src/store.ts +30 -0
- package/src/ui/WikiEntryEditor.svelte +156 -0
- package/src/ui/WikiPage.svelte +334 -0
- package/src/ui/index.ts +132 -0
package/src/model.ts
ADDED
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
import { type Actor, asUnknownRecord } from "@leitwerk-dev/domain";
|
|
2
|
+
|
|
3
|
+
/** @internal */
|
|
4
|
+
export interface WikiTopic {
|
|
5
|
+
/** @internal */ id: string;
|
|
6
|
+
/** @internal */ key: string;
|
|
7
|
+
/** @internal */ title: string;
|
|
8
|
+
/** @internal */ url: string;
|
|
9
|
+
/** @internal */ sourceRevision: string;
|
|
10
|
+
/** @internal */ revision?: number;
|
|
11
|
+
/** @internal */ deleted?: boolean;
|
|
12
|
+
/** @internal */ deletion?: {
|
|
13
|
+
/** @internal */ actor: string;
|
|
14
|
+
/** @internal */ turnRecordId: string;
|
|
15
|
+
/** @internal */ at: string;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @internal */
|
|
20
|
+
export interface WikiEvidence {
|
|
21
|
+
/** @internal */ repository: string;
|
|
22
|
+
/** @internal */ revision: string;
|
|
23
|
+
/** @internal */ path: string;
|
|
24
|
+
/** @internal */ observation: string;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
/** @internal */
|
|
28
|
+
export interface WikiPage {
|
|
29
|
+
/** @internal */ id: string;
|
|
30
|
+
/** @internal */ topicId: string;
|
|
31
|
+
/** @internal */ revision: number;
|
|
32
|
+
/** @internal */ title: string;
|
|
33
|
+
/** @internal */ markdown: string;
|
|
34
|
+
/** @internal */ applicability: string;
|
|
35
|
+
/** @internal */ status: "proposed" | "observed" | "validated" | "needs_revalidation";
|
|
36
|
+
/** @internal */ evidence: WikiEvidence[];
|
|
37
|
+
/** @internal */ links: string[];
|
|
38
|
+
/** @internal */ sourceRevision: string;
|
|
39
|
+
/** @internal */ instanceId: string;
|
|
40
|
+
/** @internal */ turnRecordId: string;
|
|
41
|
+
/** @internal */ updatedAt: string;
|
|
42
|
+
/** @internal */ updatedBy?: Actor;
|
|
43
|
+
/** @internal */ deleted: boolean;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** @internal */
|
|
47
|
+
export type WikiPageContent = Pick<
|
|
48
|
+
WikiPage,
|
|
49
|
+
"title" | "markdown" | "applicability" | "status" | "evidence" | "links"
|
|
50
|
+
>;
|
|
51
|
+
|
|
52
|
+
/** @internal */
|
|
53
|
+
export function isWikiPageId(value: unknown): value is string {
|
|
54
|
+
return typeof value === "string" && /^[a-zA-Z0-9][a-zA-Z0-9_-]{0,119}$/.test(value);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/** Validates browser and agent contributions against the same evidence contract. @internal */
|
|
58
|
+
export function parseWikiPageContent(value: unknown): WikiPageContent {
|
|
59
|
+
const input = asUnknownRecord(value);
|
|
60
|
+
if (!input) throw new Error("Provide wiki entry content");
|
|
61
|
+
const text = (record: Record<string, unknown>, key: string): string => {
|
|
62
|
+
const value = record[key];
|
|
63
|
+
if (typeof value !== "string" || !value.trim())
|
|
64
|
+
throw new Error(`Wiki ${key} must not be empty`);
|
|
65
|
+
return value;
|
|
66
|
+
};
|
|
67
|
+
if (!["proposed", "observed", "validated", "needs_revalidation"].includes(String(input.status)))
|
|
68
|
+
throw new Error("Invalid wiki evidence status");
|
|
69
|
+
if (!Array.isArray(input.evidence) || input.evidence.length === 0)
|
|
70
|
+
throw new Error("Wiki contributions require evidence");
|
|
71
|
+
const evidence = input.evidence.map((value): WikiEvidence => {
|
|
72
|
+
const record = asUnknownRecord(value);
|
|
73
|
+
if (!record) throw new Error("Invalid wiki evidence");
|
|
74
|
+
const revision = text(record, "revision");
|
|
75
|
+
if (!/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/i.test(revision))
|
|
76
|
+
throw new Error(
|
|
77
|
+
"Wiki evidence requires the inspected repository commit SHA (40 or 64 hexadecimal characters)",
|
|
78
|
+
);
|
|
79
|
+
return {
|
|
80
|
+
repository: text(record, "repository"),
|
|
81
|
+
revision,
|
|
82
|
+
path: text(record, "path"),
|
|
83
|
+
observation: text(record, "observation"),
|
|
84
|
+
};
|
|
85
|
+
});
|
|
86
|
+
if (!Array.isArray(input.links) || input.links.some((link) => !isWikiPageId(link)))
|
|
87
|
+
throw new Error("Wiki links must be page IDs");
|
|
88
|
+
return {
|
|
89
|
+
title: text(input, "title"),
|
|
90
|
+
markdown: text(input, "markdown"),
|
|
91
|
+
applicability: text(input, "applicability"),
|
|
92
|
+
status: input.status as WikiPage["status"],
|
|
93
|
+
evidence,
|
|
94
|
+
links: input.links as string[],
|
|
95
|
+
};
|
|
96
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { type Actor, asUnknownRecord } from "@leitwerk-dev/domain";
|
|
2
|
+
import type { FastifyInstance, FastifyRequest } from "fastify";
|
|
3
|
+
import { isWikiPageId, parseWikiPageContent } from "../model.js";
|
|
4
|
+
import type { TopicWikiStore } from "../store.js";
|
|
5
|
+
|
|
6
|
+
/** @internal */
|
|
7
|
+
export function registerTopicWikiRoutes(
|
|
8
|
+
app: FastifyInstance,
|
|
9
|
+
store: TopicWikiStore,
|
|
10
|
+
actorForRequest: (request: FastifyRequest) => Actor,
|
|
11
|
+
): void {
|
|
12
|
+
app.get("/api/wiki/topics", async () => ({ topics: store.listTopics() }));
|
|
13
|
+
app.get<{ Params: { topicId: string } }>("/api/wiki/topics/:topicId", async (request, reply) => {
|
|
14
|
+
const topic = store.getTopic(request.params.topicId);
|
|
15
|
+
if (!topic || topic.deleted)
|
|
16
|
+
return reply.code(404).send({ error: "Wiki group unavailable or deleted" });
|
|
17
|
+
return { topic, pages: store.listPages(topic.id) };
|
|
18
|
+
});
|
|
19
|
+
for (const [suffix, subject, conflict] of [
|
|
20
|
+
["", "wiki group", "Wiki group deletion conflict"],
|
|
21
|
+
["/pages/:pageId", "page", "Wiki deletion conflict"],
|
|
22
|
+
] as const)
|
|
23
|
+
app.delete<{
|
|
24
|
+
Params: { topicId: string; pageId?: string };
|
|
25
|
+
Querystring: { revision?: string };
|
|
26
|
+
}>(`/api/wiki/topics/:topicId${suffix}`, async (request, reply) => {
|
|
27
|
+
const revision = Number(request.query.revision);
|
|
28
|
+
if (!Number.isSafeInteger(revision) || revision < 1)
|
|
29
|
+
return reply.code(400).send({ error: `A current ${subject} revision is required` });
|
|
30
|
+
try {
|
|
31
|
+
const { topicId, pageId } = request.params;
|
|
32
|
+
const actor = actorForRequest(request).id;
|
|
33
|
+
if (pageId !== undefined) store.deletePage(topicId, pageId, revision, actor);
|
|
34
|
+
else store.deleteTopic(topicId, revision, actor);
|
|
35
|
+
return { deleted: true };
|
|
36
|
+
} catch (error) {
|
|
37
|
+
return reply.code(409).send({ error: error instanceof Error ? error.message : conflict });
|
|
38
|
+
}
|
|
39
|
+
});
|
|
40
|
+
app.get<{ Params: { topicId: string; pageId: string } }>(
|
|
41
|
+
"/api/wiki/topics/:topicId/pages/:pageId/history",
|
|
42
|
+
async (request, reply) => {
|
|
43
|
+
if (!store.readPage(request.params.topicId, request.params.pageId))
|
|
44
|
+
return reply.code(404).send({ error: "Wiki entry unavailable or deleted" });
|
|
45
|
+
return { revisions: store.history(request.params.topicId, request.params.pageId) };
|
|
46
|
+
},
|
|
47
|
+
);
|
|
48
|
+
app.put<{ Params: { topicId: string; pageId: string }; Body: unknown }>(
|
|
49
|
+
"/api/wiki/topics/:topicId/pages/:pageId",
|
|
50
|
+
async (request, reply) => {
|
|
51
|
+
const input = asUnknownRecord(request.body);
|
|
52
|
+
if (
|
|
53
|
+
!input ||
|
|
54
|
+
!Number.isSafeInteger(input.expectedRevision) ||
|
|
55
|
+
Number(input.expectedRevision) < 1
|
|
56
|
+
)
|
|
57
|
+
return reply.code(400).send({ error: "A current page revision is required" });
|
|
58
|
+
if (!isWikiPageId(request.params.pageId))
|
|
59
|
+
return reply.code(400).send({ error: "Use a stable alphanumeric wiki page ID" });
|
|
60
|
+
const current = store.readPage(request.params.topicId, request.params.pageId);
|
|
61
|
+
if (!current) return reply.code(404).send({ error: "Wiki entry unavailable or deleted" });
|
|
62
|
+
let content: ReturnType<typeof parseWikiPageContent>;
|
|
63
|
+
try {
|
|
64
|
+
content = parseWikiPageContent(input);
|
|
65
|
+
} catch (error) {
|
|
66
|
+
return reply
|
|
67
|
+
.code(400)
|
|
68
|
+
.send({ error: error instanceof Error ? error.message : "Invalid wiki entry" });
|
|
69
|
+
}
|
|
70
|
+
try {
|
|
71
|
+
return {
|
|
72
|
+
page: store.savePage(
|
|
73
|
+
{ ...current, ...content, updatedBy: actorForRequest(request) },
|
|
74
|
+
Number(input.expectedRevision),
|
|
75
|
+
),
|
|
76
|
+
};
|
|
77
|
+
} catch (error) {
|
|
78
|
+
return reply.code(409).send({
|
|
79
|
+
error:
|
|
80
|
+
error instanceof Error
|
|
81
|
+
? error.message
|
|
82
|
+
: "Wiki revision conflict; refresh before editing",
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
},
|
|
86
|
+
);
|
|
87
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import { sqliteTable, text } from "drizzle-orm/sqlite-core";
|
|
2
|
+
|
|
3
|
+
/** @internal */
|
|
4
|
+
export const wikiTopics = sqliteTable("wiki_topics", {
|
|
5
|
+
/** @internal */
|
|
6
|
+
id: text("id").primaryKey(),
|
|
7
|
+
/** @internal */
|
|
8
|
+
key: text("key").notNull(),
|
|
9
|
+
/** @internal */
|
|
10
|
+
data: text("data").notNull(),
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
/** @internal */
|
|
14
|
+
export const wikiPages = sqliteTable("wiki_pages", {
|
|
15
|
+
/** @internal */
|
|
16
|
+
id: text("id").primaryKey(),
|
|
17
|
+
/** @internal */
|
|
18
|
+
topicId: text("topic_id")
|
|
19
|
+
.notNull()
|
|
20
|
+
.references(() => wikiTopics.id),
|
|
21
|
+
/** @internal */
|
|
22
|
+
data: text("data").notNull(),
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
/** @internal */
|
|
26
|
+
export const wikiRevisions = sqliteTable("wiki_revisions", {
|
|
27
|
+
/** @internal */
|
|
28
|
+
id: text("id").primaryKey(),
|
|
29
|
+
/** @internal */
|
|
30
|
+
pageId: text("page_id")
|
|
31
|
+
.notNull()
|
|
32
|
+
.references(() => wikiPages.id),
|
|
33
|
+
/** @internal */
|
|
34
|
+
data: text("data").notNull(),
|
|
35
|
+
});
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
import { createHash } from "node:crypto";
|
|
2
|
+
import { eq } from "drizzle-orm";
|
|
3
|
+
import type { drizzle } from "drizzle-orm/node-sqlite";
|
|
4
|
+
import type { WikiPage, WikiTopic } from "../model.js";
|
|
5
|
+
import type { TopicWikiStore } from "../store.js";
|
|
6
|
+
import { wikiPages, wikiRevisions, wikiTopics } from "./schema.js";
|
|
7
|
+
|
|
8
|
+
function digest(value: string): string {
|
|
9
|
+
return createHash("sha256").update(value).digest("hex");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
/** @internal */
|
|
13
|
+
export function createTopicWikiRepo(
|
|
14
|
+
db: ReturnType<typeof drizzle>,
|
|
15
|
+
changed: (topicId: string) => void = () => {},
|
|
16
|
+
): TopicWikiStore {
|
|
17
|
+
function rawPage(topicId: string, pageId: string): WikiPage | null {
|
|
18
|
+
const row = db
|
|
19
|
+
.select()
|
|
20
|
+
.from(wikiPages)
|
|
21
|
+
.where(eq(wikiPages.id, digest(JSON.stringify([topicId, pageId]))))
|
|
22
|
+
.get();
|
|
23
|
+
return row ? JSON.parse(row.data) : null;
|
|
24
|
+
}
|
|
25
|
+
function storePage(page: WikiPage): void {
|
|
26
|
+
const id = digest(JSON.stringify([page.topicId, page.id]));
|
|
27
|
+
const data = JSON.stringify(page);
|
|
28
|
+
db.insert(wikiPages)
|
|
29
|
+
.values({ id, topicId: page.topicId, data })
|
|
30
|
+
.onConflictDoUpdate({ target: wikiPages.id, set: { data } })
|
|
31
|
+
.run();
|
|
32
|
+
db.insert(wikiRevisions)
|
|
33
|
+
.values({ id: `${id}:${page.revision}`, pageId: id, data })
|
|
34
|
+
.run();
|
|
35
|
+
}
|
|
36
|
+
function storeTopic(topic: WikiTopic): void {
|
|
37
|
+
db.insert(wikiTopics)
|
|
38
|
+
.values({ id: topic.id, key: topic.key, data: JSON.stringify(topic) })
|
|
39
|
+
.onConflictDoUpdate({ target: wikiTopics.id, set: { data: JSON.stringify(topic) } })
|
|
40
|
+
.run();
|
|
41
|
+
}
|
|
42
|
+
function bumpTopic(topicId: string): void {
|
|
43
|
+
const topic = store.getTopic(topicId);
|
|
44
|
+
if (!topic) throw new Error("Unknown wiki topic");
|
|
45
|
+
storeTopic({ ...topic, revision: (topic.revision ?? 1) + 1 });
|
|
46
|
+
}
|
|
47
|
+
function tombstone(current: WikiPage, actor: string, turnRecordId = ""): WikiPage {
|
|
48
|
+
return {
|
|
49
|
+
...current,
|
|
50
|
+
markdown: "",
|
|
51
|
+
applicability: "",
|
|
52
|
+
evidence: [],
|
|
53
|
+
deleted: true,
|
|
54
|
+
revision: current.revision + 1,
|
|
55
|
+
updatedAt: new Date().toISOString(),
|
|
56
|
+
instanceId: actor,
|
|
57
|
+
turnRecordId,
|
|
58
|
+
updatedBy: undefined,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
function invalidateDependents(topicId: string, pageId: string, updatedAt: string): void {
|
|
62
|
+
for (const page of store.listPages(topicId))
|
|
63
|
+
if (page.links.includes(pageId))
|
|
64
|
+
storePage({
|
|
65
|
+
...page,
|
|
66
|
+
status: "needs_revalidation",
|
|
67
|
+
revision: page.revision + 1,
|
|
68
|
+
updatedAt,
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
const store: TopicWikiStore = {
|
|
72
|
+
ensureTopic(input) {
|
|
73
|
+
const id = digest(input.key);
|
|
74
|
+
const current = store.getTopic(id);
|
|
75
|
+
if (current?.deleted) return current;
|
|
76
|
+
const topic = { ...input, id, revision: current?.revision ?? 1, deleted: false };
|
|
77
|
+
if (JSON.stringify(current) === JSON.stringify(topic)) return topic;
|
|
78
|
+
if (current) topic.revision++;
|
|
79
|
+
storeTopic(topic);
|
|
80
|
+
changed(id);
|
|
81
|
+
return topic;
|
|
82
|
+
},
|
|
83
|
+
getTopic(id) {
|
|
84
|
+
const row = db.select().from(wikiTopics).where(eq(wikiTopics.id, id)).get();
|
|
85
|
+
if (!row) return null;
|
|
86
|
+
const topic = JSON.parse(row.data) as WikiTopic;
|
|
87
|
+
return { ...topic, revision: topic.revision ?? 1, deleted: topic.deleted ?? false };
|
|
88
|
+
},
|
|
89
|
+
listTopics() {
|
|
90
|
+
return db
|
|
91
|
+
.select()
|
|
92
|
+
.from(wikiTopics)
|
|
93
|
+
.all()
|
|
94
|
+
.map((row) => {
|
|
95
|
+
const topic = JSON.parse(row.data) as WikiTopic;
|
|
96
|
+
return { ...topic, revision: topic.revision ?? 1, deleted: topic.deleted ?? false };
|
|
97
|
+
})
|
|
98
|
+
.filter((topic) => !topic.deleted);
|
|
99
|
+
},
|
|
100
|
+
listPages(topicId) {
|
|
101
|
+
const topic = store.getTopic(topicId);
|
|
102
|
+
if (!topic || topic.deleted) return [];
|
|
103
|
+
const pages = db
|
|
104
|
+
.select()
|
|
105
|
+
.from(wikiPages)
|
|
106
|
+
.where(eq(wikiPages.topicId, topicId))
|
|
107
|
+
.all()
|
|
108
|
+
.map((row) => JSON.parse(row.data) as WikiPage)
|
|
109
|
+
.filter((page) => !page.deleted);
|
|
110
|
+
const ids = new Set(pages.map((page) => page.id));
|
|
111
|
+
const stale = new Set(
|
|
112
|
+
pages
|
|
113
|
+
.filter(
|
|
114
|
+
(page) =>
|
|
115
|
+
page.sourceRevision !== topic?.sourceRevision ||
|
|
116
|
+
page.status === "needs_revalidation" ||
|
|
117
|
+
page.links.some((link) => !ids.has(link)),
|
|
118
|
+
)
|
|
119
|
+
.map((page) => page.id),
|
|
120
|
+
);
|
|
121
|
+
let previousSize = -1;
|
|
122
|
+
while (previousSize !== stale.size) {
|
|
123
|
+
previousSize = stale.size;
|
|
124
|
+
for (const page of pages)
|
|
125
|
+
if (page.links.some((link) => stale.has(link))) stale.add(page.id);
|
|
126
|
+
}
|
|
127
|
+
return pages.map((page) => ({
|
|
128
|
+
...page,
|
|
129
|
+
status: stale.has(page.id) ? "needs_revalidation" : page.status,
|
|
130
|
+
}));
|
|
131
|
+
},
|
|
132
|
+
readPage(topicId, pageId) {
|
|
133
|
+
return store.listPages(topicId).find((page) => page.id === pageId) ?? null;
|
|
134
|
+
},
|
|
135
|
+
history(topicId, pageId) {
|
|
136
|
+
if (!store.readPage(topicId, pageId)) return [];
|
|
137
|
+
return db
|
|
138
|
+
.select()
|
|
139
|
+
.from(wikiRevisions)
|
|
140
|
+
.where(eq(wikiRevisions.pageId, digest(JSON.stringify([topicId, pageId]))))
|
|
141
|
+
.all()
|
|
142
|
+
.map((row) => JSON.parse(row.data) as WikiPage)
|
|
143
|
+
.sort((left, right) => right.revision - left.revision);
|
|
144
|
+
},
|
|
145
|
+
savePage(input, expectedRevision) {
|
|
146
|
+
const page = db.transaction(() => {
|
|
147
|
+
const current = rawPage(input.topicId, input.id);
|
|
148
|
+
if (current?.deleted || (current?.revision ?? 0) !== expectedRevision)
|
|
149
|
+
throw new Error("Wiki revision conflict or deleted page; refresh before editing");
|
|
150
|
+
const topic = store.getTopic(input.topicId);
|
|
151
|
+
if (!topic || topic.deleted) throw new Error("Wiki group unavailable or deleted");
|
|
152
|
+
for (const link of input.links)
|
|
153
|
+
if (!store.readPage(input.topicId, link) || link === input.id)
|
|
154
|
+
throw new Error("Wiki links must reference another current page in this topic");
|
|
155
|
+
const next: WikiPage = {
|
|
156
|
+
...input,
|
|
157
|
+
revision: expectedRevision + 1,
|
|
158
|
+
updatedAt: new Date().toISOString(),
|
|
159
|
+
deleted: false,
|
|
160
|
+
};
|
|
161
|
+
storePage(next);
|
|
162
|
+
if (current) invalidateDependents(input.topicId, input.id, next.updatedAt);
|
|
163
|
+
bumpTopic(input.topicId);
|
|
164
|
+
return next;
|
|
165
|
+
});
|
|
166
|
+
changed(input.topicId);
|
|
167
|
+
return page;
|
|
168
|
+
},
|
|
169
|
+
deletePage(topicId, pageId, expectedRevision, actor, turnRecordId) {
|
|
170
|
+
db.transaction(() => {
|
|
171
|
+
const current = rawPage(topicId, pageId);
|
|
172
|
+
if (!current || current.deleted || current.revision !== expectedRevision)
|
|
173
|
+
throw new Error("Wiki revision conflict; refresh before deleting");
|
|
174
|
+
storePage(tombstone(current, actor, turnRecordId));
|
|
175
|
+
invalidateDependents(topicId, pageId, new Date().toISOString());
|
|
176
|
+
bumpTopic(topicId);
|
|
177
|
+
});
|
|
178
|
+
changed(topicId);
|
|
179
|
+
},
|
|
180
|
+
deleteTopic(topicId, expectedRevision, actor, turnRecordId) {
|
|
181
|
+
db.transaction(() => {
|
|
182
|
+
const topic = store.getTopic(topicId);
|
|
183
|
+
if (!topic || topic.deleted || topic.revision !== expectedRevision)
|
|
184
|
+
throw new Error("Wiki group revision conflict; refresh before deleting");
|
|
185
|
+
const pages = db.select().from(wikiPages).where(eq(wikiPages.topicId, topicId)).all();
|
|
186
|
+
for (const row of pages) {
|
|
187
|
+
const page = JSON.parse(row.data) as WikiPage;
|
|
188
|
+
if (!page.deleted) storePage(tombstone(page, actor, turnRecordId));
|
|
189
|
+
}
|
|
190
|
+
storeTopic({
|
|
191
|
+
...topic,
|
|
192
|
+
deleted: true,
|
|
193
|
+
revision: expectedRevision + 1,
|
|
194
|
+
deletion: { actor, turnRecordId: turnRecordId ?? "", at: new Date().toISOString() },
|
|
195
|
+
});
|
|
196
|
+
});
|
|
197
|
+
changed(topicId);
|
|
198
|
+
},
|
|
199
|
+
};
|
|
200
|
+
return store;
|
|
201
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
import { DatabaseSync } from "node:sqlite";
|
|
2
|
+
import { drizzle } from "drizzle-orm/node-sqlite";
|
|
3
|
+
import { onTestFinished } from "vitest";
|
|
4
|
+
import type { WikiPageContent } from "../model.js";
|
|
5
|
+
import { createTopicWikiRepo } from "./store.js";
|
|
6
|
+
|
|
7
|
+
/** @internal */
|
|
8
|
+
export function createWikiFixture() {
|
|
9
|
+
const sqlite = new DatabaseSync(":memory:");
|
|
10
|
+
onTestFinished(() => sqlite.close());
|
|
11
|
+
sqlite.exec(`CREATE TABLE wiki_topics (id TEXT PRIMARY KEY, key TEXT NOT NULL, data TEXT NOT NULL);
|
|
12
|
+
CREATE TABLE wiki_pages (id TEXT PRIMARY KEY, topic_id TEXT NOT NULL REFERENCES wiki_topics(id), data TEXT NOT NULL);
|
|
13
|
+
CREATE TABLE wiki_revisions (id TEXT PRIMARY KEY, page_id TEXT NOT NULL REFERENCES wiki_pages(id), data TEXT NOT NULL);`);
|
|
14
|
+
const store = createTopicWikiRepo(drizzle({ client: sqlite }));
|
|
15
|
+
const topic = store.ensureTopic({
|
|
16
|
+
key: "issue:one",
|
|
17
|
+
title: "Source issue",
|
|
18
|
+
url: "https://tracker.test/1",
|
|
19
|
+
sourceRevision: "original",
|
|
20
|
+
});
|
|
21
|
+
const content: WikiPageContent = {
|
|
22
|
+
title: "A reusable solution",
|
|
23
|
+
markdown: "Original guidance",
|
|
24
|
+
applicability: "Version one",
|
|
25
|
+
status: "observed",
|
|
26
|
+
evidence: [
|
|
27
|
+
{
|
|
28
|
+
repository: "team/service",
|
|
29
|
+
revision: "a".repeat(40),
|
|
30
|
+
path: "README.md",
|
|
31
|
+
observation: "Inspected repository",
|
|
32
|
+
},
|
|
33
|
+
],
|
|
34
|
+
links: [],
|
|
35
|
+
};
|
|
36
|
+
return { store, topic, content };
|
|
37
|
+
}
|
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
import { type ServerExtensionAPI, stringArg } from "@leitwerk-dev/process-sdk";
|
|
2
|
+
import { wikiToolNames } from "../instructions.js";
|
|
3
|
+
import type { WikiIntegration } from "../integration.js";
|
|
4
|
+
import { isWikiPageId, parseWikiPageContent } from "../model.js";
|
|
5
|
+
|
|
6
|
+
/** Register once on the host; each turn still authorizes its tools explicitly. @internal */
|
|
7
|
+
export function registerWikiTools(
|
|
8
|
+
api: Pick<ServerExtensionAPI, "tool">,
|
|
9
|
+
wiki: WikiIntegration,
|
|
10
|
+
): void {
|
|
11
|
+
const contentKeys = ["title", "markdown", "applicability", "status", "evidence", "links"];
|
|
12
|
+
const fields = {
|
|
13
|
+
wiki_index: ["query"],
|
|
14
|
+
wiki_read: ["pageId"],
|
|
15
|
+
wiki_share: ["pageId", "expectedRevision", ...contentKeys],
|
|
16
|
+
wiki_edit: ["pageId", "expectedRevision", ...contentKeys],
|
|
17
|
+
wiki_delete: ["pageId", "expectedRevision"],
|
|
18
|
+
wiki_delete_group: ["expectedRevision"],
|
|
19
|
+
};
|
|
20
|
+
for (const name of wikiToolNames) {
|
|
21
|
+
const properties: Record<string, unknown> = {
|
|
22
|
+
pageId: {
|
|
23
|
+
type: "string",
|
|
24
|
+
description:
|
|
25
|
+
"Use an entry ID returned by wiki_index, not the topic ID or an evidence revision. For a new wiki_share page, choose a stable alphanumeric ID with hyphens or underscores.",
|
|
26
|
+
},
|
|
27
|
+
query: {
|
|
28
|
+
type: "string",
|
|
29
|
+
description:
|
|
30
|
+
"Optional case-insensitive literal substring across title, applicability and text. Omit or use an empty string to discover all pages first. Long questions and keyword lists are not semantic search. If a filter returns no pages, retry without it before concluding no guidance exists.",
|
|
31
|
+
},
|
|
32
|
+
expectedRevision: {
|
|
33
|
+
type: "integer",
|
|
34
|
+
minimum: name === "wiki_share" ? 0 : 1,
|
|
35
|
+
description:
|
|
36
|
+
name === "wiki_delete_group"
|
|
37
|
+
? "The topic.revision returned by wiki_index, including all entry changes."
|
|
38
|
+
: "The entry revision returned by wiki_read; 0 is only for new wiki_share pages.",
|
|
39
|
+
},
|
|
40
|
+
title: {
|
|
41
|
+
type: "string",
|
|
42
|
+
description: "Name the reusable solution, not this process or its progress.",
|
|
43
|
+
},
|
|
44
|
+
markdown: {
|
|
45
|
+
type: "string",
|
|
46
|
+
description:
|
|
47
|
+
"Explain the new solution, why it works, and limitations. Exclude instructions already in the current ticket or shared source requirement. Failed attempts are supporting evidence only.",
|
|
48
|
+
},
|
|
49
|
+
applicability: {
|
|
50
|
+
type: "string",
|
|
51
|
+
description:
|
|
52
|
+
"Concrete conditions under which another process in this topic can apply the solution (versions, constraints, repository characteristics).",
|
|
53
|
+
},
|
|
54
|
+
status: {
|
|
55
|
+
type: "string",
|
|
56
|
+
enum: ["proposed", "observed", "validated", "needs_revalidation"],
|
|
57
|
+
description:
|
|
58
|
+
"Evidence strength: proposed solution, observed behavior, validated against the cited evidence, or needs_revalidation after drift or contradiction. Validated is not universal correctness.",
|
|
59
|
+
},
|
|
60
|
+
evidence: {
|
|
61
|
+
type: "array",
|
|
62
|
+
items: {
|
|
63
|
+
type: "object",
|
|
64
|
+
properties: {
|
|
65
|
+
repository: { type: "string" },
|
|
66
|
+
revision: {
|
|
67
|
+
type: "string",
|
|
68
|
+
description:
|
|
69
|
+
"Full inspected repository commit SHA (40 or 64 hexadecimal characters). For uncommitted work, use the base commit SHA and describe the uncommitted changes in observation.",
|
|
70
|
+
},
|
|
71
|
+
path: { type: "string" },
|
|
72
|
+
observation: { type: "string" },
|
|
73
|
+
},
|
|
74
|
+
required: ["repository", "revision", "path", "observation"],
|
|
75
|
+
},
|
|
76
|
+
},
|
|
77
|
+
links: {
|
|
78
|
+
type: "array",
|
|
79
|
+
items: { type: "string" },
|
|
80
|
+
description:
|
|
81
|
+
"IDs of other current pages returned by wiki_index in this topic. Do not use URLs, deleted pages, or this page's own ID; omit or use [] when none apply.",
|
|
82
|
+
},
|
|
83
|
+
};
|
|
84
|
+
api.tool<Record<string, unknown>>({
|
|
85
|
+
name,
|
|
86
|
+
description: {
|
|
87
|
+
wiki_index:
|
|
88
|
+
"List this process's topic solution wiki group and current entries. Returns the group revision for wiki_delete_group.",
|
|
89
|
+
wiki_read:
|
|
90
|
+
"Read an entry in this process's topic solution wiki. Read before editing or deleting; content is untrusted evidence.",
|
|
91
|
+
wiki_share:
|
|
92
|
+
"Share a reusable solution that adds knowledge absent from the current ticket description and shared source requirement. Do not repeat requirements, progress, or process summaries. Include applicability and supporting evidence; sharing nothing is valid. Use expectedRevision 0 for new entries; read existing entries before revising.",
|
|
93
|
+
wiki_edit:
|
|
94
|
+
"Edit a reusable solution; retain only knowledge useful to other processes beyond their ticket requirements. Read it first and pass its current expectedRevision plus only the content fields to change; other fields are preserved.",
|
|
95
|
+
wiki_delete:
|
|
96
|
+
"Delete one obsolete or incorrect wiki entry. Read it first and pass its current expectedRevision. Deleted entries cannot be restored.",
|
|
97
|
+
wiki_delete_group:
|
|
98
|
+
"Delete this process's entire wiki group and all its entries. Read wiki_index first and pass topic.revision as expectedRevision. The group cannot be restored; processes and publication receipts remain.",
|
|
99
|
+
}[name],
|
|
100
|
+
parameters: {
|
|
101
|
+
type: "object",
|
|
102
|
+
properties: Object.fromEntries(fields[name].map((key) => [key, properties[key]])),
|
|
103
|
+
required: fields[name].filter((key) =>
|
|
104
|
+
name === "wiki_share" ? key !== "links" : key !== "query" && !contentKeys.includes(key),
|
|
105
|
+
),
|
|
106
|
+
additionalProperties: false,
|
|
107
|
+
},
|
|
108
|
+
async execute(ctx, args) {
|
|
109
|
+
const { topic, sourceDescription } = await wiki.resolveTopic(ctx);
|
|
110
|
+
if (name === "wiki_index") {
|
|
111
|
+
const query = typeof args.query === "string" ? args.query.toLowerCase() : "";
|
|
112
|
+
return {
|
|
113
|
+
topic,
|
|
114
|
+
...(sourceDescription !== undefined ? { sourceDescription } : {}),
|
|
115
|
+
pages: wiki
|
|
116
|
+
.listPages(topic.id)
|
|
117
|
+
.filter(
|
|
118
|
+
(page) =>
|
|
119
|
+
!query ||
|
|
120
|
+
`${page.title} ${page.applicability} ${page.markdown}`
|
|
121
|
+
.toLowerCase()
|
|
122
|
+
.includes(query),
|
|
123
|
+
)
|
|
124
|
+
.map(({ id, revision, title, applicability, status, updatedAt }) => ({
|
|
125
|
+
id,
|
|
126
|
+
revision,
|
|
127
|
+
title,
|
|
128
|
+
applicability,
|
|
129
|
+
status,
|
|
130
|
+
updatedAt,
|
|
131
|
+
})),
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
if (name === "wiki_delete_group") {
|
|
135
|
+
if (!Number.isSafeInteger(args.expectedRevision) || Number(args.expectedRevision) < 1)
|
|
136
|
+
throw new Error("Read the current wiki group revision with wiki_index before deleting");
|
|
137
|
+
wiki.deleteTopic(topic.id, Number(args.expectedRevision), ctx.process.id, ctx.turn.id);
|
|
138
|
+
return { deleted: true, topicId: topic.id };
|
|
139
|
+
}
|
|
140
|
+
const pageId = stringArg(args, "pageId");
|
|
141
|
+
if (!isWikiPageId(pageId)) throw new Error("Use a stable alphanumeric wiki page ID");
|
|
142
|
+
if (name === "wiki_read")
|
|
143
|
+
return {
|
|
144
|
+
page: wiki.readPage(topic.id, pageId),
|
|
145
|
+
instruction:
|
|
146
|
+
"Evidence describes its recorded revision. Verify the current repository before reuse; null means unavailable or deleted.",
|
|
147
|
+
};
|
|
148
|
+
if (
|
|
149
|
+
!Number.isSafeInteger(args.expectedRevision) ||
|
|
150
|
+
Number(args.expectedRevision) < (name === "wiki_share" ? 0 : 1)
|
|
151
|
+
)
|
|
152
|
+
throw new Error("Read the current page revision before editing or deleting");
|
|
153
|
+
if (name === "wiki_delete") {
|
|
154
|
+
wiki.deletePage(
|
|
155
|
+
topic.id,
|
|
156
|
+
pageId,
|
|
157
|
+
Number(args.expectedRevision),
|
|
158
|
+
ctx.process.id,
|
|
159
|
+
ctx.turn.id,
|
|
160
|
+
);
|
|
161
|
+
return { deleted: true, pageId };
|
|
162
|
+
}
|
|
163
|
+
const current = wiki.readPage(topic.id, pageId);
|
|
164
|
+
if (name === "wiki_edit" && !current) throw new Error("Wiki entry unavailable or deleted");
|
|
165
|
+
if (name === "wiki_edit" && !contentKeys.some((key) => Object.hasOwn(args, key)))
|
|
166
|
+
throw new Error("Provide at least one wiki content field to edit");
|
|
167
|
+
const content = parseWikiPageContent(
|
|
168
|
+
name === "wiki_edit" ? { ...current, ...args } : { links: [], ...args },
|
|
169
|
+
);
|
|
170
|
+
if (
|
|
171
|
+
current?.turnRecordId === ctx.turn.id &&
|
|
172
|
+
current.instanceId === ctx.process.id &&
|
|
173
|
+
!current.updatedBy &&
|
|
174
|
+
current.revision === Number(args.expectedRevision) + 1 &&
|
|
175
|
+
current.markdown === content.markdown &&
|
|
176
|
+
current.title === content.title &&
|
|
177
|
+
current.applicability === content.applicability &&
|
|
178
|
+
current.status === content.status &&
|
|
179
|
+
JSON.stringify(current.evidence) === JSON.stringify(content.evidence) &&
|
|
180
|
+
JSON.stringify(current.links) === JSON.stringify(content.links)
|
|
181
|
+
)
|
|
182
|
+
return current;
|
|
183
|
+
return wiki.savePage(
|
|
184
|
+
{
|
|
185
|
+
id: pageId,
|
|
186
|
+
topicId: topic.id,
|
|
187
|
+
...content,
|
|
188
|
+
sourceRevision: topic.sourceRevision,
|
|
189
|
+
instanceId: ctx.process.id,
|
|
190
|
+
turnRecordId: ctx.turn.id,
|
|
191
|
+
},
|
|
192
|
+
Number(args.expectedRevision),
|
|
193
|
+
);
|
|
194
|
+
},
|
|
195
|
+
});
|
|
196
|
+
}
|
|
197
|
+
}
|
package/src/store.ts
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import type { WikiPage, WikiTopic } from "./model.js";
|
|
2
|
+
|
|
3
|
+
/** @internal */
|
|
4
|
+
export interface TopicWikiStore {
|
|
5
|
+
/** @internal */ ensureTopic(
|
|
6
|
+
input: Omit<WikiTopic, "id" | "revision" | "deleted" | "deletion">,
|
|
7
|
+
): WikiTopic;
|
|
8
|
+
/** @internal */ getTopic(id: string): WikiTopic | null;
|
|
9
|
+
/** @internal */ listTopics(): WikiTopic[];
|
|
10
|
+
/** @internal */ listPages(topicId: string): WikiPage[];
|
|
11
|
+
/** @internal */ readPage(topicId: string, pageId: string): WikiPage | null;
|
|
12
|
+
/** @internal */ history(topicId: string, pageId: string): WikiPage[];
|
|
13
|
+
/** @internal */ savePage(
|
|
14
|
+
input: Omit<WikiPage, "revision" | "updatedAt" | "deleted">,
|
|
15
|
+
expectedRevision: number,
|
|
16
|
+
): WikiPage;
|
|
17
|
+
/** @internal */ deletePage(
|
|
18
|
+
topicId: string,
|
|
19
|
+
pageId: string,
|
|
20
|
+
expectedRevision: number,
|
|
21
|
+
actor: string,
|
|
22
|
+
turnRecordId?: string,
|
|
23
|
+
): void;
|
|
24
|
+
/** @internal */ deleteTopic(
|
|
25
|
+
topicId: string,
|
|
26
|
+
expectedRevision: number,
|
|
27
|
+
actor: string,
|
|
28
|
+
turnRecordId?: string,
|
|
29
|
+
): void;
|
|
30
|
+
}
|