archidocs 1.3.0 → 1.3.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/dist/cli/commands.d.ts +8 -8
- package/dist/cli/commands.js +7 -7
- package/dist/cli/dev.d.ts +5 -6
- package/dist/cli/dev.js +12 -14
- package/dist/cli/diff-format.d.ts +1 -5
- package/dist/cli/main.js +2 -324
- package/dist/cli/run.d.ts +18 -0
- package/dist/cli/run.js +283 -0
- package/dist/commands/build.d.ts +51 -0
- package/dist/commands/build.js +39 -0
- package/dist/commands/check.d.ts +8 -0
- package/dist/commands/check.js +5 -0
- package/dist/commands/diff.d.ts +29 -0
- package/dist/commands/diff.js +25 -0
- package/dist/commands/export.d.ts +41 -0
- package/dist/commands/export.js +47 -0
- package/dist/commands/history.d.ts +18 -0
- package/dist/commands/history.js +9 -0
- package/dist/commands/index.d.ts +11 -0
- package/dist/commands/index.js +11 -0
- package/dist/core/index.d.ts +1 -1
- package/dist/core/index.js +1 -1
- package/dist/core/load.d.ts +0 -2
- package/dist/core/load.js +5 -6
- package/dist/core/workspace-files.d.ts +13 -0
- package/dist/core/workspace-files.js +20 -0
- package/dist/git.d.ts +2 -0
- package/dist/git.js +14 -4
- package/dist/history/git.d.ts +0 -10
- package/dist/history/git.js +1 -30
- package/dist/history/index.d.ts +15 -31
- package/dist/history/index.js +47 -43
- package/dist/history/points.d.ts +23 -0
- package/dist/history/points.js +28 -0
- package/dist/history/site.d.ts +30 -0
- package/dist/history/site.js +37 -0
- package/dist/mcp/history.d.ts +4 -4
- package/dist/mcp/history.js +56 -116
- package/dist/mcp/server.js +18 -17
- package/dist/mcp/site.d.ts +12 -0
- package/dist/mcp/site.js +46 -0
- package/dist/mcp/source.d.ts +14 -7
- package/dist/mcp/source.js +25 -99
- package/dist/revisions/find.d.ts +64 -0
- package/dist/revisions/find.js +85 -0
- package/dist/revisions/index.d.ts +57 -0
- package/dist/revisions/index.js +24 -0
- package/dist/revisions/site.d.ts +82 -0
- package/dist/revisions/site.js +157 -0
- package/dist/revisions/text.d.ts +8 -0
- package/dist/revisions/text.js +17 -0
- package/dist/revisions/touching.d.ts +7 -0
- package/dist/revisions/touching.js +7 -0
- package/dist/viewer/assets/index-C0lIIm8V.js +138 -0
- package/dist/viewer/assets/{png-CAaXWR-k.js → png-DMwnpRCt.js} +1 -1
- package/dist/viewer/assets/{svg-BPVHfD36.js → svg-DN6lMeZo.js} +1 -1
- package/dist/viewer/index.html +1 -1
- package/dist/workspace/index.d.ts +37 -0
- package/dist/workspace/index.js +52 -0
- package/package.json +1 -1
- package/dist/viewer/assets/index-BXa5R96a.js +0 -138
- /package/dist/{history → revisions}/migrate.d.ts +0 -0
- /package/dist/{history → revisions}/migrate.js +0 -0
package/dist/mcp/history.js
CHANGED
|
@@ -1,7 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import { gitTopLevel } from "../git.js";
|
|
4
|
-
import { bundleAt, buildHistory, history, isDate, migrateBundle, readIndex, resolvePoint } from "../history/index.js";
|
|
1
|
+
import { bundleAt, buildHistory, readIndex, resolvePoint } from "../history/index.js";
|
|
2
|
+
import { find, INDEX_PATH, span, timeSpan } from "../revisions/index.js";
|
|
5
3
|
import { commitInfo } from "../history/git.js";
|
|
6
4
|
export function pastOf(source) {
|
|
7
5
|
return source.kind === "checkout" ? checkoutPast(source) : bundlePast(source);
|
|
@@ -14,28 +12,26 @@ const noCommit = (point, suggestions) => ({
|
|
|
14
12
|
...(suggestions?.length ? { suggestions } : {}),
|
|
15
13
|
});
|
|
16
14
|
const reason = (error) => (error instanceof Error ? error.message : String(error));
|
|
17
|
-
/**
|
|
18
|
-
function
|
|
19
|
-
if (
|
|
20
|
-
return
|
|
21
|
-
|
|
15
|
+
/** `find`'s failure in MCP's words: no Revision (naming the first one for a date before it), or a prefix of several. */
|
|
16
|
+
function findFailure(point, failure) {
|
|
17
|
+
if (failure.kind === "ambiguous") {
|
|
18
|
+
return { ok: false, code: "not_found", message: `"${point}" is the start of ${failure.count} Revisions' shas; give more of it.`, suggestions: failure.candidates.map((r) => r.sha) };
|
|
19
|
+
}
|
|
20
|
+
const missing = noCommit(point, failure.tags);
|
|
21
|
+
return failure.first ? { ...missing, message: `${missing.message} The first Revision is ${failure.first.sha.slice(0, 7)}, ${failure.first.date}.` } : missing;
|
|
22
22
|
}
|
|
23
23
|
function checkoutPast(source) {
|
|
24
|
-
|
|
25
|
-
const repo = () => (found ??= (async () => {
|
|
26
|
-
const root = await realpath(source.root);
|
|
27
|
-
const repoRoot = await gitTopLevel(root);
|
|
28
|
-
return repoRoot ? history(repoRoot, root) : undefined;
|
|
29
|
-
})());
|
|
24
|
+
const repo = async () => (await source.checkout.repo())?.history;
|
|
30
25
|
const noGit = unavailable(`The Workspace at ${source.root} is not in a git repository, so it has no history.`);
|
|
31
26
|
return {
|
|
32
27
|
async at(point) {
|
|
33
28
|
const h = await repo();
|
|
34
29
|
if (!h)
|
|
35
30
|
return noGit;
|
|
36
|
-
const
|
|
37
|
-
if (!
|
|
38
|
-
return
|
|
31
|
+
const resolved = await resolvePoint(h, point);
|
|
32
|
+
if (!resolved.ok)
|
|
33
|
+
return findFailure(point, resolved);
|
|
34
|
+
const { sha } = resolved;
|
|
39
35
|
try {
|
|
40
36
|
return { ok: true, commit: sha, snapshot: { bundle: (await bundleAt(h, sha)).bundle, issues: [] } };
|
|
41
37
|
}
|
|
@@ -56,120 +52,64 @@ function checkoutPast(source) {
|
|
|
56
52
|
return unavailable(`Could not read the history of ${h.repoRoot}: ${reason(error)}`);
|
|
57
53
|
}
|
|
58
54
|
},
|
|
59
|
-
async
|
|
60
|
-
|
|
61
|
-
|
|
55
|
+
async span(point) {
|
|
56
|
+
// A ref spans its commit's instant, a tag that looks like a date included;
|
|
57
|
+
// otherwise a day or a time spans itself, as on a site.
|
|
58
|
+
const own = timeSpan(point);
|
|
62
59
|
const h = await repo();
|
|
63
60
|
if (!h)
|
|
64
|
-
return noGit;
|
|
65
|
-
const
|
|
66
|
-
if (!
|
|
67
|
-
return
|
|
68
|
-
|
|
61
|
+
return own ? { ok: true, span: own } : noGit;
|
|
62
|
+
const resolved = await resolvePoint(h, point);
|
|
63
|
+
if (own && !(resolved.ok && resolved.ref))
|
|
64
|
+
return { ok: true, span: own };
|
|
65
|
+
if (!resolved.ok)
|
|
66
|
+
return findFailure(point, resolved);
|
|
67
|
+
const at = Date.parse((await commitInfo(h.repoRoot, resolved.sha)).date);
|
|
68
|
+
return { ok: true, span: { start: at, end: at + 1 } };
|
|
69
69
|
},
|
|
70
70
|
};
|
|
71
71
|
}
|
|
72
|
-
/** How long a fetched index is trusted before the site is asked again, as for the Bundle. */
|
|
73
|
-
const RECHECK_MS = 60_000;
|
|
74
72
|
function bundlePast(source) {
|
|
75
|
-
|
|
76
|
-
const bundles = new Map();
|
|
77
|
-
let cached;
|
|
78
|
-
const sibling = (path) => {
|
|
79
|
-
const location = source.location;
|
|
80
|
-
return /^https?:\/\//i.test(location) ? new URL(path, location).href : join(dirname(location), path);
|
|
81
|
-
};
|
|
82
|
-
const read = async (path) => {
|
|
83
|
-
const where = sibling(path);
|
|
84
|
-
if (!/^https?:\/\//i.test(where))
|
|
85
|
-
return readFile(where, "utf8");
|
|
86
|
-
const response = await fetch(where, { headers: { accept: "application/json" }, cache: "no-store" });
|
|
87
|
-
if (!response.ok)
|
|
88
|
-
throw new Error(`${where} answered ${response.status} ${response.statusText}`.trim());
|
|
89
|
-
return response.text();
|
|
90
|
-
};
|
|
73
|
+
const { site } = source;
|
|
91
74
|
const index = async () => {
|
|
92
|
-
const
|
|
93
|
-
if (
|
|
94
|
-
return
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
}
|
|
99
|
-
catch (error) {
|
|
100
|
-
return unavailable(`The site has no readable Revision index (bundles/index.json): ${reason(error)}`);
|
|
101
|
-
}
|
|
102
|
-
const revisions = raw?.revisions;
|
|
103
|
-
if (!Array.isArray(revisions))
|
|
104
|
-
return unavailable("The site's bundles/index.json holds no Revisions.");
|
|
105
|
-
cached = { revisions: revisions, checkedAt: now };
|
|
106
|
-
return { ok: true, revisions: cached.revisions };
|
|
107
|
-
};
|
|
108
|
-
/** A point as a Revision of the index: the last at or before a date, a tag, or a sha (or its prefix). */
|
|
109
|
-
const entryAt = async (point) => {
|
|
110
|
-
const listed = await index();
|
|
111
|
-
if (!listed.ok)
|
|
112
|
-
return listed;
|
|
113
|
-
const { revisions } = listed;
|
|
114
|
-
if (isDate(point)) {
|
|
115
|
-
const time = dateTime(point, "end");
|
|
116
|
-
return revisions.find((r) => Date.parse(r.date) <= time) ?? noCommit(point);
|
|
117
|
-
}
|
|
118
|
-
const tagged = revisions.find((r) => r.tags.includes(point));
|
|
119
|
-
if (tagged)
|
|
120
|
-
return tagged;
|
|
121
|
-
if (/^[0-9a-f]{4,40}$/i.test(point)) {
|
|
122
|
-
const matches = revisions.filter((r) => r.sha.startsWith(point.toLowerCase()));
|
|
123
|
-
if (matches.length === 1)
|
|
124
|
-
return matches[0];
|
|
125
|
-
if (matches.length > 1)
|
|
126
|
-
return { ok: false, code: "not_found", message: `"${point}" is the start of ${matches.length} Revisions' shas; give more of it.`, suggestions: matches.map((m) => m.sha).slice(0, 5) };
|
|
127
|
-
}
|
|
128
|
-
return noCommit(point, revisions.flatMap((r) => r.tags).slice(0, 5));
|
|
129
|
-
};
|
|
130
|
-
const fetchBundle = (sha) => {
|
|
131
|
-
let pending = bundles.get(sha);
|
|
132
|
-
if (!pending) {
|
|
133
|
-
pending = (async () => {
|
|
134
|
-
let raw;
|
|
135
|
-
try {
|
|
136
|
-
raw = JSON.parse(await read(`bundles/${sha}.json`));
|
|
137
|
-
}
|
|
138
|
-
catch (error) {
|
|
139
|
-
return unavailable(`The Bundle at ${sha.slice(0, 7)} can't be read from the site: ${reason(error)}`);
|
|
140
|
-
}
|
|
141
|
-
return migrateBundle(raw) ?? unavailable(`The Bundle at ${sha.slice(0, 7)} has a schema this Archidocs can't read or migrate.`);
|
|
142
|
-
})();
|
|
143
|
-
bundles.set(sha, pending);
|
|
144
|
-
// A failure may be a host's hiccup; ask again next time.
|
|
145
|
-
void pending.then((b) => "ok" in b && bundles.delete(sha));
|
|
146
|
-
}
|
|
147
|
-
return pending;
|
|
75
|
+
const read = await site.index();
|
|
76
|
+
if (!read.ok)
|
|
77
|
+
return unavailable(read.message);
|
|
78
|
+
if (!read.index)
|
|
79
|
+
return unavailable(`The site has no Revision index (${INDEX_PATH}), so it has no history.`);
|
|
80
|
+
return { ok: true, revisions: read.index.revisions };
|
|
148
81
|
};
|
|
149
82
|
return {
|
|
150
83
|
index,
|
|
151
84
|
async at(point, current) {
|
|
152
85
|
const commit = current.bundle.commit;
|
|
153
|
-
|
|
86
|
+
// The current commit answers without the index; a prefix of it does unless the index says it starts other shas too.
|
|
87
|
+
if (commit && point === commit)
|
|
154
88
|
return { ok: true, commit, snapshot: current };
|
|
155
|
-
}
|
|
156
|
-
const
|
|
157
|
-
if (
|
|
158
|
-
return
|
|
89
|
+
const startsCommit = !!commit && /^[0-9a-f]{4,64}$/i.test(point) && commit.startsWith(point.toLowerCase());
|
|
90
|
+
const listed = await index();
|
|
91
|
+
if (!listed.ok)
|
|
92
|
+
return startsCommit ? { ok: true, commit: commit, snapshot: current } : listed;
|
|
93
|
+
const found = find(listed.revisions, point);
|
|
94
|
+
if (!found.ok)
|
|
95
|
+
return found.kind === "not_found" && startsCommit ? { ok: true, commit: commit, snapshot: current } : findFailure(point, found);
|
|
96
|
+
const entry = found.revision;
|
|
159
97
|
if (entry.sha === commit)
|
|
160
98
|
return { ok: true, commit, snapshot: current };
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
return bundle;
|
|
166
|
-
return { ok: true, commit: entry.sha, snapshot: { bundle, issues: [] } };
|
|
99
|
+
const past = await site.bundle(entry);
|
|
100
|
+
if (!past.ok)
|
|
101
|
+
return unavailable(past.message);
|
|
102
|
+
return { ok: true, commit: entry.sha, snapshot: { bundle: past.bundle, issues: [] } };
|
|
167
103
|
},
|
|
168
|
-
async
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
104
|
+
async span(point) {
|
|
105
|
+
// Tags come first, so read the index; a day or a time still spans itself without one.
|
|
106
|
+
const listed = await index();
|
|
107
|
+
if (!listed.ok) {
|
|
108
|
+
const own = timeSpan(point);
|
|
109
|
+
return own ? { ok: true, span: own } : listed;
|
|
110
|
+
}
|
|
111
|
+
const spanned = span(listed.revisions, point);
|
|
112
|
+
return spanned.ok ? spanned : findFailure(point, spanned);
|
|
173
113
|
},
|
|
174
114
|
};
|
|
175
115
|
}
|
package/dist/mcp/server.js
CHANGED
|
@@ -9,6 +9,7 @@ import { describeLens, listViews, resolveView } from "../core/views/index.js";
|
|
|
9
9
|
import { listAdrs } from "../core/adrs.js";
|
|
10
10
|
import { AdrStatus } from "../schema/adr.js";
|
|
11
11
|
import { BundleAdr, BundleDeployment, BundleElement, BundleLens, BundleRelationship } from "../schema/bundle.js";
|
|
12
|
+
import { POINT_GRAMMAR, touching } from "../revisions/index.js";
|
|
12
13
|
import { pastOf } from "./history.js";
|
|
13
14
|
import { registerWriteTools } from "./writes.js";
|
|
14
15
|
import { authoringGuide, topics } from "../guide/index.js";
|
|
@@ -506,10 +507,10 @@ export function createServer(options) {
|
|
|
506
507
|
});
|
|
507
508
|
tool("get_changes", {
|
|
508
509
|
title: "Get Changes",
|
|
509
|
-
description:
|
|
510
|
+
description: `What changed in the Model between two points in time, each ${POINT_GRAMMAR} (on a checkout, any git ref too): a flat list of Changes (added, removed or changed), each with the fields that differ and the whole item before and after. \`to\` defaults to \`at\`, else the Workspace the server is reading. \`scope\` narrows to an Element (with its descendants and the Relationships, Deployments and ADRs touching them), a Team (what it owns at either side) or a Project; pass \`scope_kind\` when a Team and a Project share the id. \`kinds\` keeps only some item kinds.`,
|
|
510
511
|
input: z.object({
|
|
511
|
-
from: Point.describe(
|
|
512
|
-
to: Point.optional().describe(
|
|
512
|
+
from: Point.describe(`The point to compare from: ${POINT_GRAMMAR}`),
|
|
513
|
+
to: Point.optional().describe(`The point to compare to: ${POINT_GRAMMAR}. Defaults to \`at\`, else the current Workspace`),
|
|
513
514
|
scope: z.string().min(1).optional().describe("An Element (project:id), a Team id or a Project id"),
|
|
514
515
|
scope_kind: ScopeKind.optional(),
|
|
515
516
|
kinds: z.array(ChangeKindSchema).min(1).optional().describe(`Only these item kinds: ${changeKinds.join(", ")}`),
|
|
@@ -562,12 +563,12 @@ export function createServer(options) {
|
|
|
562
563
|
});
|
|
563
564
|
tool("list_history", {
|
|
564
565
|
title: "List history",
|
|
565
|
-
description: "The Revisions (default-branch commits that changed the Workspace), newest first, each with sha, date, subject, author, tags, change counts against the Revision before, and whether its Bundle can be read with `at`. `scope` keeps the Revisions that touched an Element or its descendants, a Team or what it owns, or a Project (as they are at `at`, else now). `since` and `until` are
|
|
566
|
+
description: "The Revisions (default-branch commits that changed the Workspace), newest first, each with sha, date, subject, author, tags, change counts against the Revision before, and whether its Bundle can be read with `at`. `scope` keeps the Revisions that touched an Element or its descendants, a Team or what it owns, or a Project (as they are at `at`, else now). `since` and `until` are points, inclusive: a day covers the whole UTC day. With `at`, Revisions after it are left out. Read from the Revision index, so it is cheap.",
|
|
566
567
|
input: z.object({
|
|
567
568
|
scope: z.string().min(1).optional().describe("An Element (project:id), a Team id or a Project id"),
|
|
568
569
|
scope_kind: ScopeKind.optional(),
|
|
569
|
-
since: Point.optional().describe(
|
|
570
|
-
until: Point.optional().describe(
|
|
570
|
+
since: Point.optional().describe(`The earliest point: ${POINT_GRAMMAR}`),
|
|
571
|
+
until: Point.optional().describe(`The latest point: ${POINT_GRAMMAR}`),
|
|
571
572
|
limit: Limit,
|
|
572
573
|
}),
|
|
573
574
|
output: z.object({
|
|
@@ -581,17 +582,18 @@ export function createServer(options) {
|
|
|
581
582
|
if (!listed.ok)
|
|
582
583
|
return listed;
|
|
583
584
|
let revisions = listed.revisions;
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
[
|
|
587
|
-
[
|
|
585
|
+
// A Revision is kept from the start of `since`'s span up to, not including, the end of `until`'s or `at`'s.
|
|
586
|
+
for (const [point, keep] of [
|
|
587
|
+
[since, (s, d) => d >= s.start],
|
|
588
|
+
[until, (s, d) => d < s.end],
|
|
589
|
+
[at, (s, d) => d < s.end],
|
|
588
590
|
]) {
|
|
589
591
|
if (point === undefined)
|
|
590
592
|
continue;
|
|
591
|
-
const
|
|
592
|
-
if (!
|
|
593
|
-
return
|
|
594
|
-
revisions = revisions.filter((r) => keep(
|
|
593
|
+
const spanned = await past.span(point);
|
|
594
|
+
if (!spanned.ok)
|
|
595
|
+
return spanned;
|
|
596
|
+
revisions = revisions.filter((r) => keep(spanned.span, Date.parse(r.date)));
|
|
595
597
|
}
|
|
596
598
|
let resolved;
|
|
597
599
|
if (scope !== undefined) {
|
|
@@ -599,8 +601,7 @@ export function createServer(options) {
|
|
|
599
601
|
if (!found.ok)
|
|
600
602
|
return scopeFailure(scope, found);
|
|
601
603
|
resolved = found.scope;
|
|
602
|
-
|
|
603
|
-
revisions = revisions.filter((r) => touched(r.changedIds));
|
|
604
|
+
revisions = touching(revisions, scopeMatcher(resolved, [bundle]));
|
|
604
605
|
}
|
|
605
606
|
const { items, total, truncated } = page(revisions.map(({ sha, date, subject, author, tags, counts, bundle: kept }) => ({ sha, date, subject, author, tags, counts, bundle: kept })), limit);
|
|
606
607
|
const tally = ({ added, removed, changed }) => [added && `+${added}`, removed && `-${removed}`, changed && `~${changed}`].filter(Boolean).join(" ") || "no Changes";
|
|
@@ -684,7 +685,7 @@ function scopeFailure(scope, found) {
|
|
|
684
685
|
return fail("ambiguous", `"${scope}" is both a ${found.candidates.map((c) => c.kind).join(" and a ")}; pass scope_kind to pick one.`, found.candidates.map((c) => c.kind));
|
|
685
686
|
return notFound("Element, Team or Project", scope, found.known);
|
|
686
687
|
}
|
|
687
|
-
const At = z.string().min(1).describe(
|
|
688
|
+
const At = z.string().min(1).describe(`A point in time to answer at: ${POINT_GRAMMAR}. On a checkout, any git ref works too. Leave out for the current Workspace.`);
|
|
688
689
|
/** Whether a call's raw arguments carry `at`, which write tools never take. */
|
|
689
690
|
export const takesAt = (raw) => raw !== null && typeof raw === "object" && "at" in raw;
|
|
690
691
|
export const atRefused = "Write tools never take `at`: they edit the Workspace as it is now.";
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { type SiteReader } from "../revisions/index.ts";
|
|
2
|
+
/**
|
|
3
|
+
* A {@link SiteReader} for `archidocs mcp --bundle <location>`: a URL or a
|
|
4
|
+
* path, naming the site (its root) or a `.json` Bundle file in it. A
|
|
5
|
+
* Bundle file's folder is the root, and its name is the latest Bundle's
|
|
6
|
+
* path. Over HTTP it reads with `fetch`; on disk, a file's mtime and size
|
|
7
|
+
* stand in for an ETag, so an unchanged file is not read again.
|
|
8
|
+
*/
|
|
9
|
+
export declare function siteAt(location: string): {
|
|
10
|
+
reader: SiteReader;
|
|
11
|
+
bundle: string;
|
|
12
|
+
};
|
package/dist/mcp/site.js
ADDED
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { readFile, stat } from "node:fs/promises";
|
|
2
|
+
import { basename, dirname, join, resolve } from "node:path";
|
|
3
|
+
import { fetchReader, LATEST_BUNDLE_PATH } from "../revisions/index.js";
|
|
4
|
+
/**
|
|
5
|
+
* A {@link SiteReader} for `archidocs mcp --bundle <location>`: a URL or a
|
|
6
|
+
* path, naming the site (its root) or a `.json` Bundle file in it. A
|
|
7
|
+
* Bundle file's folder is the root, and its name is the latest Bundle's
|
|
8
|
+
* path. Over HTTP it reads with `fetch`; on disk, a file's mtime and size
|
|
9
|
+
* stand in for an ETag, so an unchanged file is not read again.
|
|
10
|
+
*/
|
|
11
|
+
export function siteAt(location) {
|
|
12
|
+
if (/^https?:\/\//i.test(location)) {
|
|
13
|
+
const url = new URL(location);
|
|
14
|
+
if (url.pathname.endsWith(".json")) {
|
|
15
|
+
const name = url.pathname.slice(url.pathname.lastIndexOf("/") + 1);
|
|
16
|
+
return { reader: fetchReader(new URL("./", url).href), bundle: name };
|
|
17
|
+
}
|
|
18
|
+
if (!url.pathname.endsWith("/"))
|
|
19
|
+
url.pathname += "/";
|
|
20
|
+
return { reader: fetchReader(url.href), bundle: LATEST_BUNDLE_PATH };
|
|
21
|
+
}
|
|
22
|
+
const path = resolve(location);
|
|
23
|
+
return path.endsWith(".json") ? { reader: fileReader(dirname(path)), bundle: basename(path) } : { reader: fileReader(path), bundle: LATEST_BUNDLE_PATH };
|
|
24
|
+
}
|
|
25
|
+
function fileReader(root) {
|
|
26
|
+
const location = (path) => join(root, ...path.split("/"));
|
|
27
|
+
return {
|
|
28
|
+
location,
|
|
29
|
+
async read(path, validators) {
|
|
30
|
+
const where = location(path);
|
|
31
|
+
try {
|
|
32
|
+
const s = await stat(where);
|
|
33
|
+
const etag = `${s.mtimeMs}-${s.size}`;
|
|
34
|
+
if (validators?.etag === etag)
|
|
35
|
+
return { kind: "not_modified" };
|
|
36
|
+
return { kind: "text", text: await readFile(where, "utf8"), validators: { etag } };
|
|
37
|
+
}
|
|
38
|
+
catch (error) {
|
|
39
|
+
// A missing file, or a folder missing on the way to it.
|
|
40
|
+
if (error.code === "ENOENT" || error.code === "ENOTDIR")
|
|
41
|
+
return { kind: "missing" };
|
|
42
|
+
throw error;
|
|
43
|
+
}
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
}
|
package/dist/mcp/source.d.ts
CHANGED
|
@@ -1,5 +1,7 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
1
|
+
import type { Issue } from "../core/index.ts";
|
|
2
|
+
import { type Checkout } from "../workspace/index.ts";
|
|
3
|
+
import type { Bundle } from "../schema/bundle.ts";
|
|
4
|
+
import { type Site } from "../revisions/index.ts";
|
|
3
5
|
/** The Bundle the server answers from, with the issues Load found building it. */
|
|
4
6
|
export interface Snapshot {
|
|
5
7
|
bundle: Bundle;
|
|
@@ -20,11 +22,15 @@ export interface CheckoutSource extends SourceBase {
|
|
|
20
22
|
readonly kind: "checkout";
|
|
21
23
|
/** The Workspace root: the folder holding `workspace.yaml`. */
|
|
22
24
|
readonly root: string;
|
|
25
|
+
/** The Checkout at `root` the Bundle is built from. */
|
|
26
|
+
readonly checkout: Checkout;
|
|
23
27
|
}
|
|
24
28
|
export interface BundleSource extends SourceBase {
|
|
25
29
|
readonly kind: "bundle";
|
|
26
30
|
/** The Bundle file's URL or absolute path. */
|
|
27
31
|
readonly location: string;
|
|
32
|
+
/** The site the Bundle is read from, which also holds its Revision index and past Bundles. */
|
|
33
|
+
readonly site: Site;
|
|
28
34
|
}
|
|
29
35
|
/**
|
|
30
36
|
* A local checkout: the Bundle is built in memory and rebuilt when a file
|
|
@@ -38,11 +44,12 @@ export declare function checkoutSource({ root }: {
|
|
|
38
44
|
/**
|
|
39
45
|
* A published Workspace Bundle, read-only. `location` is a URL or a path,
|
|
40
46
|
* naming either the Bundle file or the site (folder) that holds
|
|
41
|
-
* `workspace.bundle.json`. The
|
|
42
|
-
* HTTP the re-check is a conditional
|
|
43
|
-
*
|
|
44
|
-
*
|
|
45
|
-
*
|
|
47
|
+
* `workspace.bundle.json`. The Bundle and the site's Revision index are
|
|
48
|
+
* re-checked at most every 60 s; over HTTP the re-check is a conditional
|
|
49
|
+
* request when the host gave an ETag or Last-Modified, so an unchanged file
|
|
50
|
+
* costs a 304. A Bundle from an older Archidocs is migrated. A source that
|
|
51
|
+
* can't be read, or doesn't hold a Bundle this version can read, rejects and
|
|
52
|
+
* the tool returns `source_unavailable`; the next call tries again.
|
|
46
53
|
*/
|
|
47
54
|
export declare function bundleSource({ location }: {
|
|
48
55
|
location: string;
|
package/dist/mcp/source.js
CHANGED
|
@@ -1,8 +1,6 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import { gitHead } from "../git.js";
|
|
5
|
-
import { Bundle } from "../schema/bundle.js";
|
|
1
|
+
import { checkoutAt } from "../workspace/index.js";
|
|
2
|
+
import { openSite } from "../revisions/index.js";
|
|
3
|
+
import { siteAt } from "./site.js";
|
|
6
4
|
/**
|
|
7
5
|
* A local checkout: the Bundle is built in memory and rebuilt when a file
|
|
8
6
|
* under the Workspace root changes or HEAD moves. Change is detected per
|
|
@@ -10,123 +8,51 @@ import { Bundle } from "../schema/bundle.js";
|
|
|
10
8
|
* next call with no watcher to start or stop.
|
|
11
9
|
*/
|
|
12
10
|
export function checkoutSource({ root }) {
|
|
11
|
+
const checkout = checkoutAt(root);
|
|
13
12
|
let cached;
|
|
14
13
|
return {
|
|
15
14
|
kind: "checkout",
|
|
16
15
|
root,
|
|
16
|
+
checkout,
|
|
17
17
|
writesAllowed: true,
|
|
18
18
|
async snapshot() {
|
|
19
|
-
const [files, head] = await Promise.all([fingerprint(
|
|
19
|
+
const [files, head] = await Promise.all([checkout.fingerprint(), checkout.head()]);
|
|
20
20
|
const key = `${head.commit}\n${head.builtAt}\n${files}`;
|
|
21
|
-
if (cached?.key !== key)
|
|
22
|
-
cached = { key, snapshot: await load(
|
|
23
|
-
}
|
|
21
|
+
if (cached?.key !== key)
|
|
22
|
+
cached = { key, snapshot: await checkout.load() };
|
|
24
23
|
return cached.snapshot;
|
|
25
24
|
},
|
|
26
25
|
};
|
|
27
26
|
}
|
|
28
|
-
/** How long
|
|
27
|
+
/** How long the site's latest Bundle and index are trusted before the site is asked again. */
|
|
29
28
|
const RECHECK_MS = 60_000;
|
|
30
|
-
const BUNDLE_FILE = "workspace.bundle.json";
|
|
31
29
|
/**
|
|
32
30
|
* A published Workspace Bundle, read-only. `location` is a URL or a path,
|
|
33
31
|
* naming either the Bundle file or the site (folder) that holds
|
|
34
|
-
* `workspace.bundle.json`. The
|
|
35
|
-
* HTTP the re-check is a conditional
|
|
36
|
-
*
|
|
37
|
-
*
|
|
38
|
-
*
|
|
32
|
+
* `workspace.bundle.json`. The Bundle and the site's Revision index are
|
|
33
|
+
* re-checked at most every 60 s; over HTTP the re-check is a conditional
|
|
34
|
+
* request when the host gave an ETag or Last-Modified, so an unchanged file
|
|
35
|
+
* costs a 304. A Bundle from an older Archidocs is migrated. A source that
|
|
36
|
+
* can't be read, or doesn't hold a Bundle this version can read, rejects and
|
|
37
|
+
* the tool returns `source_unavailable`; the next call tries again.
|
|
39
38
|
*/
|
|
40
39
|
export function bundleSource({ location }) {
|
|
41
|
-
const
|
|
42
|
-
|
|
43
|
-
let
|
|
44
|
-
const fetchUrl = async (href) => {
|
|
45
|
-
const headers = { accept: "application/json" };
|
|
46
|
-
if (cached?.etag)
|
|
47
|
-
headers["if-none-match"] = cached.etag;
|
|
48
|
-
if (cached?.lastModified)
|
|
49
|
-
headers["if-modified-since"] = cached.lastModified;
|
|
50
|
-
const response = await fetch(href, { headers, cache: "no-store" });
|
|
51
|
-
if (response.status === 304 && cached)
|
|
52
|
-
return cached;
|
|
53
|
-
if (!response.ok)
|
|
54
|
-
throw new Error(`${href} answered ${response.status} ${response.statusText}`.trim());
|
|
55
|
-
return {
|
|
56
|
-
snapshot: parse(await response.text(), href),
|
|
57
|
-
checkedAt: 0,
|
|
58
|
-
etag: response.headers.get("etag") ?? undefined,
|
|
59
|
-
lastModified: response.headers.get("last-modified") ?? undefined,
|
|
60
|
-
};
|
|
61
|
-
};
|
|
62
|
-
const readPath = async () => {
|
|
63
|
-
path ??= await bundlePath(location);
|
|
64
|
-
const s = await stat(path);
|
|
65
|
-
const fileKey = `${s.mtimeMs}\0${s.size}`;
|
|
66
|
-
if (cached?.fileKey === fileKey)
|
|
67
|
-
return cached;
|
|
68
|
-
return { snapshot: parse(await readFile(path, "utf8"), path), checkedAt: 0, fileKey };
|
|
69
|
-
};
|
|
40
|
+
const { reader, bundle } = siteAt(location);
|
|
41
|
+
const site = openSite(reader, { recheckMs: RECHECK_MS, bundle });
|
|
42
|
+
let held;
|
|
70
43
|
return {
|
|
71
44
|
kind: "bundle",
|
|
45
|
+
site,
|
|
72
46
|
get location() {
|
|
73
|
-
return
|
|
47
|
+
return site.location;
|
|
74
48
|
},
|
|
75
49
|
writesAllowed: false,
|
|
76
50
|
async snapshot() {
|
|
77
|
-
const
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
return cached.snapshot;
|
|
51
|
+
const latest = await site.latest();
|
|
52
|
+
// A Bundle is only ever published from a Workspace with no errors; check's warnings aren't kept in it.
|
|
53
|
+
if (held?.bundle !== latest)
|
|
54
|
+
held = { bundle: latest, issues: [] };
|
|
55
|
+
return held;
|
|
83
56
|
},
|
|
84
57
|
};
|
|
85
58
|
}
|
|
86
|
-
/** A site root URL becomes its `workspace.bundle.json`; a URL to a `.json` file is used as given. */
|
|
87
|
-
function bundleUrl(location) {
|
|
88
|
-
const url = new URL(location);
|
|
89
|
-
if (url.pathname.endsWith(".json"))
|
|
90
|
-
return url.href;
|
|
91
|
-
if (!url.pathname.endsWith("/"))
|
|
92
|
-
url.pathname += "/";
|
|
93
|
-
return new URL(BUNDLE_FILE, url).href;
|
|
94
|
-
}
|
|
95
|
-
/** A folder becomes its `workspace.bundle.json`; a file is used as given. */
|
|
96
|
-
async function bundlePath(location) {
|
|
97
|
-
const path = resolve(location);
|
|
98
|
-
return (await stat(path)).isDirectory() ? join(path, BUNDLE_FILE) : path;
|
|
99
|
-
}
|
|
100
|
-
function parse(text, from) {
|
|
101
|
-
let json;
|
|
102
|
-
try {
|
|
103
|
-
json = JSON.parse(text);
|
|
104
|
-
}
|
|
105
|
-
catch {
|
|
106
|
-
throw new Error(`${from} is not JSON.`);
|
|
107
|
-
}
|
|
108
|
-
const bundle = Bundle.safeParse(json);
|
|
109
|
-
if (!bundle.success)
|
|
110
|
-
throw new Error(`${from} is not a Workspace Bundle this version can read.`);
|
|
111
|
-
// A Bundle is only ever published from a Workspace with no errors; check's warnings aren't kept in it.
|
|
112
|
-
return { bundle: bundle.data, issues: [] };
|
|
113
|
-
}
|
|
114
|
-
/** Path, mtime and size of every `.yaml` and `.md` file (ADRs) under the root, in a stable order. */
|
|
115
|
-
async function fingerprint(root) {
|
|
116
|
-
const lines = [];
|
|
117
|
-
const dirs = [root];
|
|
118
|
-
for (let i = 0; i < dirs.length; i++) {
|
|
119
|
-
for (const entry of await readdir(dirs[i], { withFileTypes: true })) {
|
|
120
|
-
if (entry.name.startsWith(".") || entry.name === "node_modules")
|
|
121
|
-
continue;
|
|
122
|
-
const path = join(dirs[i], entry.name);
|
|
123
|
-
if (entry.isDirectory())
|
|
124
|
-
dirs.push(path);
|
|
125
|
-
else if (entry.name.endsWith(".yaml") || entry.name.endsWith(".md")) {
|
|
126
|
-
const s = await stat(path);
|
|
127
|
-
lines.push(`${path}\0${s.mtimeMs}\0${s.size}`);
|
|
128
|
-
}
|
|
129
|
-
}
|
|
130
|
-
}
|
|
131
|
-
return lines.sort().join("\n");
|
|
132
|
-
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The point grammar: how a tag, a sha (or its prefix), a day or a time names
|
|
3
|
+
* a Revision. One reading for the viewer, MCP on a site and, through ticket
|
|
4
|
+
* 05, a checkout, so a point names the same Revision everywhere.
|
|
5
|
+
*/
|
|
6
|
+
/** What `find` needs of a Revision; the index's `RevisionEntry` is one. */
|
|
7
|
+
export interface Dated {
|
|
8
|
+
sha: string;
|
|
9
|
+
/** ISO 8601, as git's `%cI` writes it. */
|
|
10
|
+
date: string;
|
|
11
|
+
tags: readonly string[];
|
|
12
|
+
}
|
|
13
|
+
/** The point grammar in one sentence, for every reference that takes a point. */
|
|
14
|
+
export declare const POINT_GRAMMAR = "a tag, a commit sha or its first 4 or more characters, or a date (`YYYY-MM-DD`) or time (ISO 8601, UTC unless it gives a zone)";
|
|
15
|
+
/** The Revision a point names, or why it names none. */
|
|
16
|
+
export type Found<R extends Dated> = {
|
|
17
|
+
ok: true;
|
|
18
|
+
revision: R;
|
|
19
|
+
}
|
|
20
|
+
/** `tags` to suggest; `first` is the first Revision when the point is a date or time before it. */
|
|
21
|
+
| {
|
|
22
|
+
ok: false;
|
|
23
|
+
kind: "not_found";
|
|
24
|
+
tags: string[];
|
|
25
|
+
first?: R;
|
|
26
|
+
}
|
|
27
|
+
/** `count` Revisions' shas start with the point; `candidates` are the newest 5 of them. */
|
|
28
|
+
| {
|
|
29
|
+
ok: false;
|
|
30
|
+
kind: "ambiguous";
|
|
31
|
+
count: number;
|
|
32
|
+
candidates: R[];
|
|
33
|
+
};
|
|
34
|
+
/** A stretch of time in ms since the epoch: from `start` up to, but not including, `end`. */
|
|
35
|
+
export interface Span {
|
|
36
|
+
start: number;
|
|
37
|
+
end: number;
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* The span a day or a time stands for, or undefined when the point is
|
|
41
|
+
* neither. A day (`YYYY-MM-DD`) is the UTC day `[00:00Z, next 00:00Z)`; a
|
|
42
|
+
* time is its one instant, read as UTC when it gives no zone.
|
|
43
|
+
*/
|
|
44
|
+
export declare function timeSpan(point: string): Span | undefined;
|
|
45
|
+
/**
|
|
46
|
+
* The Revision a point names, over Revisions listed newest first. In order:
|
|
47
|
+
* 1. an exact tag: the newest Revision carrying it;
|
|
48
|
+
* 2. a day or a time: the last Revision dated before the span's end;
|
|
49
|
+
* 3. hex of 4 to 64 characters, any case: the one Revision whose sha starts with it.
|
|
50
|
+
* Anything else, or nothing found, is `not_found`; hex that starts several shas is `ambiguous`.
|
|
51
|
+
*/
|
|
52
|
+
export declare function find<R extends Dated>(revisions: readonly R[], point: string): Found<R>;
|
|
53
|
+
/**
|
|
54
|
+
* The time a point spans, for `since`/`until`/`at` filters: a tag or a sha
|
|
55
|
+
* spans its Revision's one instant, a day its whole UTC day, a time its one
|
|
56
|
+
* instant. A day or time spans itself even with no Revision in or before it.
|
|
57
|
+
* Keep a Revision dated `>=` the `since` span's start and `<` the `until` span's end.
|
|
58
|
+
*/
|
|
59
|
+
export declare function span<R extends Dated>(revisions: readonly R[], point: string): {
|
|
60
|
+
ok: true;
|
|
61
|
+
span: Span;
|
|
62
|
+
} | Exclude<Found<R>, {
|
|
63
|
+
ok: true;
|
|
64
|
+
}>;
|