@codyn06/cboard 0.1.0 → 0.2.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 +5 -2
- package/dist/api.d.ts +8 -2
- package/dist/api.js +43 -25
- package/dist/cboard.js +57 -3
- package/dist/docs.d.ts +39 -0
- package/dist/docs.js +243 -0
- package/dist/extract.js +23 -1
- package/dist/issues.d.ts +44 -0
- package/dist/issues.js +138 -0
- package/dist/wake.d.ts +13 -0
- package/dist/wake.js +33 -0
- package/kit/cboard-connect.js +425 -46
- package/kit/cboard-kit/brief.js +144 -18
- package/kit/cboard-kit/config.js +38 -3
- package/kit/cboard-kit/connect-check.js +56 -13
- package/kit/cboard-kit/device.js +192 -0
- package/kit/cboard-kit/drift.js +55 -0
- package/kit/cboard-kit/headers.js +24 -3
- package/kit/cboard-kit/mcp.js +38 -3
- package/kit/cboard-kit/references.js +70 -0
- package/kit/cboard-kit/skill/SKILL.md +16 -0
- package/kit/cboard-kit/wait.js +45 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,6 +10,9 @@ Requires Node 20 or newer. No native modules and no install scripts.
|
|
|
10
10
|
npx @codyn06/cboard connect --project <project-uuid> # once per repository; rerun to update the kit
|
|
11
11
|
npx @codyn06/cboard sync # sync the committed code to the graph
|
|
12
12
|
npx @codyn06/cboard check # is the token accepted, is the project visible?
|
|
13
|
+
npx @codyn06/cboard connect --goal "<one line>" # record the project goal as a requirement node
|
|
14
|
+
npx @codyn06/cboard issues sync # mirror GitHub issues into the graph
|
|
15
|
+
npx @codyn06/cboard docs sync # import README, docs/ and ADRs into the graph
|
|
13
16
|
```
|
|
14
17
|
|
|
15
18
|
Or install it once and use the `cboard` command directly:
|
|
@@ -36,8 +39,8 @@ changed. `cboard connect` also keeps the generated files on LF with a marked `#
|
|
|
36
39
|
|
|
37
40
|
## The token
|
|
38
41
|
|
|
39
|
-
Create a token in the cboard web app (the "Agent tokens" button in the project header;
|
|
40
|
-
`graph:read`, `graph:contribute
|
|
42
|
+
Create a token in the cboard web app (the "Agent tokens" button in the project header; members pick
|
|
43
|
+
`graph:read`, `graph:contribute` and `graph:context`, viewers are read-only) and save it to `.cboard/token` in the repository
|
|
41
44
|
(`connect` adds it to `.gitignore`), or export it as `CBOARD_PAT`. The repository's token file takes
|
|
42
45
|
precedence over `CBOARD_PAT`. The token is only ever sent to the default cboard server or to a server you
|
|
43
46
|
list in `CBOARD_TRUSTED_SERVERS`.
|
package/dist/api.d.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { type WakeOptions } from "./wake.js";
|
|
1
2
|
import type { Contribution, Receipt, Result, Snapshot } from "./model.js";
|
|
2
3
|
export declare class GraphHttpError extends Error {
|
|
3
4
|
readonly status: number;
|
|
@@ -13,7 +14,10 @@ export declare class ContextApi {
|
|
|
13
14
|
private readonly token;
|
|
14
15
|
private readonly base;
|
|
15
16
|
private readonly mcp;
|
|
16
|
-
|
|
17
|
+
private readonly wake;
|
|
18
|
+
constructor(server: string, project: string, token: string, wake?: WakeOptions);
|
|
19
|
+
/** Retries a request while the hosted service wakes; announces that once per client. */
|
|
20
|
+
private waking;
|
|
17
21
|
private request;
|
|
18
22
|
/**
|
|
19
23
|
* Writes go through the MCP submit_contribution tool: the CDN firewall in front of the hosted
|
|
@@ -44,7 +48,9 @@ export declare function submitBatch<T>(api: ContextApi, group: T[], make: (items
|
|
|
44
48
|
receipt: Receipt;
|
|
45
49
|
}[]>;
|
|
46
50
|
/** Server state is authoritative; local state is never used to skip reconciliation. */
|
|
47
|
-
export declare function reconcile(api: ContextApi, snapshot: Snapshot, namespace: string, supersede?: boolean
|
|
51
|
+
export declare function reconcile(api: ContextApi, snapshot: Snapshot, namespace: string, supersede?: boolean,
|
|
52
|
+
/** Nodes outside the snapshot (other namespaces) that edges may point to, by client key. */
|
|
53
|
+
anchors?: Map<string, string>): Promise<{
|
|
48
54
|
contributions: number;
|
|
49
55
|
digest: string;
|
|
50
56
|
}>;
|
package/dist/api.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { createHash, randomUUID } from "node:crypto";
|
|
2
|
+
import { WAKE_BUDGET_MS, retryWake } from "./wake.js";
|
|
2
3
|
export class GraphHttpError extends Error {
|
|
3
4
|
status;
|
|
4
5
|
constructor(status) {
|
|
@@ -6,6 +7,7 @@ export class GraphHttpError extends Error {
|
|
|
6
7
|
this.status = status;
|
|
7
8
|
}
|
|
8
9
|
}
|
|
10
|
+
const LOCAL_HOSTS = ["127.0.0.1", "localhost", "[::1]"];
|
|
9
11
|
export class GraphToolError extends Error {
|
|
10
12
|
tool;
|
|
11
13
|
code;
|
|
@@ -20,7 +22,8 @@ export class ContextApi {
|
|
|
20
22
|
token;
|
|
21
23
|
base;
|
|
22
24
|
mcp;
|
|
23
|
-
|
|
25
|
+
wake;
|
|
26
|
+
constructor(server, project, token, wake = {}) {
|
|
24
27
|
this.project = project;
|
|
25
28
|
this.token = token;
|
|
26
29
|
const url = new URL(server);
|
|
@@ -37,38 +40,51 @@ export class ContextApi {
|
|
|
37
40
|
const root = server.replace(/\/$/, "");
|
|
38
41
|
this.base = `${root}/v1/graph/projects/${project}`;
|
|
39
42
|
this.mcp = `${root}/mcp`;
|
|
43
|
+
// A sleeping hosted service needs a minute or so to wake; a local graph is up or it is not.
|
|
44
|
+
this.wake = { budgetMs: LOCAL_HOSTS.includes(url.hostname) ? 0 : WAKE_BUDGET_MS, ...wake };
|
|
45
|
+
}
|
|
46
|
+
/** Retries a request while the hosted service wakes; announces that once per client. */
|
|
47
|
+
waking(attempt) {
|
|
48
|
+
return retryWake(attempt, this.wake);
|
|
40
49
|
}
|
|
41
50
|
async request(path) {
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
51
|
+
return this.waking(async (timeoutMs) => {
|
|
52
|
+
const response = await fetch(this.base + path, {
|
|
53
|
+
redirect: "error",
|
|
54
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
55
|
+
headers: { authorization: `Bearer ${this.token}` },
|
|
56
|
+
});
|
|
57
|
+
if (!response.ok)
|
|
58
|
+
throw new GraphHttpError(response.status);
|
|
59
|
+
return (await response.json());
|
|
45
60
|
});
|
|
46
|
-
if (!response.ok)
|
|
47
|
-
throw new Error(`Graph HTTP ${String(response.status)}`);
|
|
48
|
-
return (await response.json());
|
|
49
61
|
}
|
|
50
62
|
/**
|
|
51
63
|
* Writes go through the MCP submit_contribution tool: the CDN firewall in front of the hosted
|
|
52
64
|
* service blocks code-sync payloads on the REST contribution route (Cloudflare 403 "Blocked").
|
|
53
65
|
*/
|
|
54
66
|
async callTool(name, args) {
|
|
55
|
-
const response = await
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
67
|
+
const response = await this.waking(async (timeoutMs) => {
|
|
68
|
+
const res = await fetch(this.mcp, {
|
|
69
|
+
method: "POST",
|
|
70
|
+
redirect: "error",
|
|
71
|
+
signal: AbortSignal.timeout(timeoutMs),
|
|
72
|
+
headers: {
|
|
73
|
+
authorization: `Bearer ${this.token}`,
|
|
74
|
+
"content-type": "application/json",
|
|
75
|
+
accept: "application/json, text/event-stream",
|
|
76
|
+
},
|
|
77
|
+
body: JSON.stringify({
|
|
78
|
+
jsonrpc: "2.0",
|
|
79
|
+
id: 1,
|
|
80
|
+
method: "tools/call",
|
|
81
|
+
params: { name, arguments: args },
|
|
82
|
+
}),
|
|
83
|
+
});
|
|
84
|
+
if (!res.ok)
|
|
85
|
+
throw new GraphHttpError(res.status);
|
|
86
|
+
return res;
|
|
69
87
|
});
|
|
70
|
-
if (!response.ok)
|
|
71
|
-
throw new GraphHttpError(response.status);
|
|
72
88
|
const reply = (await response.json());
|
|
73
89
|
const text = reply.result?.content?.[0]?.text;
|
|
74
90
|
if (reply.error || typeof text !== "string")
|
|
@@ -254,7 +270,9 @@ const edgeKey = (e) => stable([
|
|
|
254
270
|
e.type === "amends" ? (e.properties.scope ?? null) : null,
|
|
255
271
|
]);
|
|
256
272
|
/** Server state is authoritative; local state is never used to skip reconciliation. */
|
|
257
|
-
export async function reconcile(api, snapshot, namespace, supersede = true
|
|
273
|
+
export async function reconcile(api, snapshot, namespace, supersede = true,
|
|
274
|
+
/** Nodes outside the snapshot (other namespaces) that edges may point to, by client key. */
|
|
275
|
+
anchors = new Map()) {
|
|
258
276
|
const existing = await api.all({ namespace, include_inactive: "true" });
|
|
259
277
|
const byKey = new Map(existing.nodes.map((n) => [n.clientKey, n]));
|
|
260
278
|
// Entities imported before namespace adoption carry the same client key outside the namespace.
|
|
@@ -273,7 +291,7 @@ export async function reconcile(api, snapshot, namespace, supersede = true) {
|
|
|
273
291
|
for (const [key, n] of found)
|
|
274
292
|
byKey.set(key, n);
|
|
275
293
|
}
|
|
276
|
-
const ids = new Map([...byKey].map(([key, n]) => [key, n.id]));
|
|
294
|
+
const ids = new Map([...anchors, ...[...byKey].map(([key, n]) => [key, n.id])]);
|
|
277
295
|
const desired = new Set(snapshot.nodes.map((n) => n.clientKey));
|
|
278
296
|
const revisions = [];
|
|
279
297
|
const fresh = snapshot.nodes.filter((n) => !byKey.has(n.clientKey));
|
package/dist/cboard.js
CHANGED
|
@@ -2,6 +2,8 @@ import { spawnSync } from "node:child_process";
|
|
|
2
2
|
import { existsSync, readFileSync } from "node:fs";
|
|
3
3
|
import { fileURLToPath } from "node:url";
|
|
4
4
|
import { sync } from "./cli.js";
|
|
5
|
+
import { docsSync } from "./docs.js";
|
|
6
|
+
import { issuesSync } from "./issues.js";
|
|
5
7
|
const HELP = `cboard — client for cboard project graphs
|
|
6
8
|
|
|
7
9
|
Usage: cboard <command> [flags]
|
|
@@ -9,6 +11,8 @@ Usage: cboard <command> [flags]
|
|
|
9
11
|
Commands:
|
|
10
12
|
connect Connect this repository to a cboard project (writes .cboard.json, .cboard/, .mcp.json, hooks)
|
|
11
13
|
sync Sync this repository's code layer to the cboard graph
|
|
14
|
+
issues Mirror this repository's GitHub issues into the graph ("cboard issues sync")
|
|
15
|
+
docs sync Import this repository's Markdown docs into the cboard graph
|
|
12
16
|
check Check that this repository can reach its cboard graph
|
|
13
17
|
|
|
14
18
|
Run "cboard <command> --help" for a command's flags.
|
|
@@ -16,20 +20,44 @@ Options:
|
|
|
16
20
|
--help, -h Show this help
|
|
17
21
|
--version, -v Show the version
|
|
18
22
|
`;
|
|
19
|
-
const CONNECT_HELP = `Usage: cboard connect --project <uuid> [--server <https url>] [--repo <path>]
|
|
20
|
-
[--no-hooks] [--no-agents] [--no-skill] [--strict] [--codex]
|
|
23
|
+
const CONNECT_HELP = `Usage: cboard connect [--project <uuid>] [--server <https url>] [--repo <path>]
|
|
24
|
+
[--no-hooks] [--no-agents] [--no-skill] [--no-login] [--strict] [--codex]
|
|
25
|
+
[--goal "<one line>"] [--reference <projectId> --name <name>]...
|
|
26
|
+
[--remove-reference <name>]...
|
|
27
|
+
|
|
28
|
+
Without a usable token (none saved, or the server rejects it) connect logs in through your browser: it
|
|
29
|
+
prints a URL and a code, you approve and pick the project there, and the token is saved to the repository's
|
|
30
|
+
token file (kept out of Git; never printed). --no-login skips that and needs --project and an existing token.
|
|
21
31
|
|
|
22
32
|
Writes .cboard.json, the committed kit in .cboard/ (hooks and the MCP header helper run it, never npx),
|
|
23
33
|
.mcp.json, the SessionStart hooks, an AGENTS.md block, the cboard-graph skill and a .gitattributes
|
|
24
34
|
block. Rerun to update the kit. --codex also prints the Codex MCP config snippet.
|
|
35
|
+
--goal creates (or, with a different text, revises) the project's one goal requirement node and remembers
|
|
36
|
+
its id in .cboard.json; it needs a token (graph:read, graph:contribute). Rerunning without --goal leaves it alone.
|
|
37
|
+
--reference <projectId> --name <name> (repeatable, in pairs) lets this repository read another project without
|
|
38
|
+
writing to it: it records the reference in .cboard.json, adds an MCP server "cboard-ref-<name>" and expects a
|
|
39
|
+
graph:read token for that project in .cboard/ref-<name>.token (kept out of Git). The project owner must have
|
|
40
|
+
added you as a member. --remove-reference <name> removes it (the token file is left in place).
|
|
25
41
|
`;
|
|
26
42
|
const SYNC_HELP = `Usage: cboard sync [--repo PATH] [--project UUID] [--server URL] [--token-env NAME] [--state PATH]
|
|
27
43
|
|
|
28
44
|
Project, server and token come from .cboard.json and the repo token file when the flags are omitted.
|
|
29
45
|
`;
|
|
46
|
+
const ISSUES_HELP = `Usage: cboard issues sync [--state all|open] [--limit N] [--repo PATH] [--project UUID] [--server URL] [--token-env NAME]
|
|
47
|
+
|
|
48
|
+
Mirrors the origin repository's GitHub issues (not pull requests) as open_issue nodes linked to the
|
|
49
|
+
repository node; run "cboard sync" first. Reads through "gh api", or GITHUB_TOKEN when gh is
|
|
50
|
+
unavailable. --state open or --limit mirrors a partial list and keeps issues it did not see.
|
|
51
|
+
`;
|
|
52
|
+
const DOCS_HELP = `Usage: cboard docs sync [--repo PATH] [--project UUID] [--server URL] [--token-env NAME]
|
|
53
|
+
|
|
54
|
+
Imports README.md, docs/**/*.md and ADR folders (docs/adr, adr, docs/decisions) as source,
|
|
55
|
+
excerpt, decision and plan nodes. Configure paths with .cboard.json "docs": {"include": [...], "exclude": [...]}.
|
|
56
|
+
`;
|
|
30
57
|
const CHECK_HELP = `Usage: cboard check
|
|
31
58
|
|
|
32
|
-
Reports whether the token in this repository is accepted and its configured project is visible
|
|
59
|
+
Reports whether the token in this repository is accepted and its configured project is visible, and tests each
|
|
60
|
+
reference project (token present, project visible, token read-only).
|
|
33
61
|
Exits 1 when it is not connected.
|
|
34
62
|
`;
|
|
35
63
|
const packageFile = new URL("../package.json", import.meta.url);
|
|
@@ -88,6 +116,32 @@ export async function main(argv) {
|
|
|
88
116
|
console.error(error instanceof Error ? error.message : "Context sync failed");
|
|
89
117
|
return 1;
|
|
90
118
|
}
|
|
119
|
+
case "issues":
|
|
120
|
+
if (wantsHelp || rest[0] !== "sync") {
|
|
121
|
+
console.log(ISSUES_HELP);
|
|
122
|
+
return wantsHelp ? 0 : 2;
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
await issuesSync(rest.slice(1));
|
|
126
|
+
return 0;
|
|
127
|
+
}
|
|
128
|
+
catch (error) {
|
|
129
|
+
console.error(error instanceof Error ? error.message : "Issues sync failed");
|
|
130
|
+
return 1;
|
|
131
|
+
}
|
|
132
|
+
case "docs":
|
|
133
|
+
if (wantsHelp || rest[0] !== "sync") {
|
|
134
|
+
console.log(DOCS_HELP);
|
|
135
|
+
return wantsHelp ? 0 : 2;
|
|
136
|
+
}
|
|
137
|
+
try {
|
|
138
|
+
await docsSync(rest.slice(1));
|
|
139
|
+
return 0;
|
|
140
|
+
}
|
|
141
|
+
catch (error) {
|
|
142
|
+
console.error(error instanceof Error ? error.message : "Docs sync failed");
|
|
143
|
+
return 1;
|
|
144
|
+
}
|
|
91
145
|
default:
|
|
92
146
|
if (command)
|
|
93
147
|
console.error(`cboard: unknown command "${command}"\n`);
|
package/dist/docs.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import type { Snapshot } from "./model.js";
|
|
2
|
+
/** `.cboard.json` `docs` block: globs relative to the repository root. */
|
|
3
|
+
export interface DocsConfig {
|
|
4
|
+
include?: string[];
|
|
5
|
+
exclude?: string[];
|
|
6
|
+
}
|
|
7
|
+
/** The optional `docs` block of `.cboard.json`; malformed values are ignored. */
|
|
8
|
+
export declare function docsConfig(root: string): DocsConfig;
|
|
9
|
+
export interface Section {
|
|
10
|
+
heading: string;
|
|
11
|
+
/** Heading line plus content, H3 and deeper folded in. */
|
|
12
|
+
text: string;
|
|
13
|
+
startLine: number;
|
|
14
|
+
endLine: number;
|
|
15
|
+
/** No content except headings. */
|
|
16
|
+
headingOnly: boolean;
|
|
17
|
+
}
|
|
18
|
+
/** Splits Markdown at H2 headings (fenced code is skipped). Merges heading-only fragments. */
|
|
19
|
+
export declare function parseMarkdown(text: string): {
|
|
20
|
+
title: string | undefined;
|
|
21
|
+
intro: string;
|
|
22
|
+
sections: Section[];
|
|
23
|
+
};
|
|
24
|
+
/** Maps an ADR status to the decision vocabulary; unknown wording gives no status. */
|
|
25
|
+
export declare function adrStatus(text: string): string | undefined;
|
|
26
|
+
export interface DocsSummary {
|
|
27
|
+
files: number;
|
|
28
|
+
sections: number;
|
|
29
|
+
truncated: number;
|
|
30
|
+
}
|
|
31
|
+
/** Reads the committed HEAD tree and builds the docs-layer snapshot. */
|
|
32
|
+
export declare function extractDocs(root: string, config?: DocsConfig): Snapshot & {
|
|
33
|
+
repo: string;
|
|
34
|
+
commit: string;
|
|
35
|
+
namespace: string;
|
|
36
|
+
summary: DocsSummary;
|
|
37
|
+
};
|
|
38
|
+
/** `cboard docs sync`: Markdown docs of this repository into the graph. */
|
|
39
|
+
export declare function docsSync(argv: string[]): Promise<void>;
|
package/dist/docs.js
ADDED
|
@@ -0,0 +1,243 @@
|
|
|
1
|
+
import { execFileSync } from "node:child_process";
|
|
2
|
+
import { readFileSync } from "node:fs";
|
|
3
|
+
import { posix, resolve } from "node:path";
|
|
4
|
+
import { ContextApi, reconcile } from "./api.js";
|
|
5
|
+
import { options, resolveSync } from "./cli.js";
|
|
6
|
+
import { repository } from "./extract.js";
|
|
7
|
+
import { globMatcher } from "./languages.js";
|
|
8
|
+
const DEFAULT_INCLUDE = ["README.md", "docs/**/*.md", "docs/adr/**", "adr/**", "docs/decisions/**"];
|
|
9
|
+
const ADR = ["docs/adr/**", "adr/**", "docs/decisions/**"].map(globMatcher);
|
|
10
|
+
const PLAN = globMatcher("docs/plans/**");
|
|
11
|
+
const MARKDOWN = /\.(md|markdown)$/i;
|
|
12
|
+
/** The server stores 8192 bytes; leave room like the code layer does. */
|
|
13
|
+
const BODY_BYTES = 7800;
|
|
14
|
+
/** The optional `docs` block of `.cboard.json`; malformed values are ignored. */
|
|
15
|
+
export function docsConfig(root) {
|
|
16
|
+
let parsed;
|
|
17
|
+
try {
|
|
18
|
+
parsed = JSON.parse(readFileSync(resolve(root, ".cboard.json"), "utf8"));
|
|
19
|
+
}
|
|
20
|
+
catch {
|
|
21
|
+
return {};
|
|
22
|
+
}
|
|
23
|
+
const found = parsed?.docs;
|
|
24
|
+
if (typeof found !== "object" || !found)
|
|
25
|
+
return {};
|
|
26
|
+
const docs = found;
|
|
27
|
+
const globs = (value) => Array.isArray(value) ? value.filter((v) => typeof v === "string") : undefined;
|
|
28
|
+
const config = {};
|
|
29
|
+
const include = globs(docs.include), exclude = globs(docs.exclude);
|
|
30
|
+
if (include)
|
|
31
|
+
config.include = include;
|
|
32
|
+
if (exclude)
|
|
33
|
+
config.exclude = exclude;
|
|
34
|
+
return config;
|
|
35
|
+
}
|
|
36
|
+
function capBody(text) {
|
|
37
|
+
if (Buffer.byteLength(text) <= BODY_BYTES)
|
|
38
|
+
return { body: text, truncated: false };
|
|
39
|
+
let bytes = 0, body = "";
|
|
40
|
+
for (const character of text) {
|
|
41
|
+
const size = Buffer.byteLength(character);
|
|
42
|
+
if (bytes + size > BODY_BYTES)
|
|
43
|
+
break;
|
|
44
|
+
bytes += size;
|
|
45
|
+
body += character;
|
|
46
|
+
}
|
|
47
|
+
return { body, truncated: true };
|
|
48
|
+
}
|
|
49
|
+
const plain = (s) => s
|
|
50
|
+
.replace(/\*\*|`/g, "")
|
|
51
|
+
.replace(/\[([^\]]+)\]\([^)]*\)/g, "$1")
|
|
52
|
+
.replace(/\s+/g, " ")
|
|
53
|
+
.trim();
|
|
54
|
+
const slugOf = (s) => plain(s)
|
|
55
|
+
.toLowerCase()
|
|
56
|
+
.replace(/[^a-z0-9]+/g, "-")
|
|
57
|
+
.replace(/^-+|-+$/g, "")
|
|
58
|
+
.slice(0, 60) || "section";
|
|
59
|
+
/** Splits Markdown at H2 headings (fenced code is skipped). Merges heading-only fragments. */
|
|
60
|
+
export function parseMarkdown(text) {
|
|
61
|
+
const lines = text.split(/\r?\n/);
|
|
62
|
+
let title;
|
|
63
|
+
const introLines = [];
|
|
64
|
+
const raw = [];
|
|
65
|
+
let fence = "";
|
|
66
|
+
lines.forEach((line, index) => {
|
|
67
|
+
const mark = /^\s*(```+|~~~+)/.exec(line)?.[1];
|
|
68
|
+
let heading = null;
|
|
69
|
+
if (mark) {
|
|
70
|
+
if (!fence)
|
|
71
|
+
fence = mark;
|
|
72
|
+
else if (mark === fence)
|
|
73
|
+
fence = "";
|
|
74
|
+
}
|
|
75
|
+
else if (!fence)
|
|
76
|
+
heading = /^(#{1,6})\s+(.+?)\s*#*\s*$/.exec(line);
|
|
77
|
+
const level = heading?.[1]?.length ?? 0;
|
|
78
|
+
const current = raw[raw.length - 1];
|
|
79
|
+
if (heading && level === 1 && !title && !current)
|
|
80
|
+
title = plain(heading[2] ?? "");
|
|
81
|
+
else if (heading && level <= 2) {
|
|
82
|
+
raw.push({
|
|
83
|
+
heading: plain(heading[2] ?? ""),
|
|
84
|
+
lines: [line],
|
|
85
|
+
start: index + 1,
|
|
86
|
+
content: false,
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
else if (current) {
|
|
90
|
+
current.lines.push(line);
|
|
91
|
+
if (!heading && line.trim())
|
|
92
|
+
current.content = true;
|
|
93
|
+
}
|
|
94
|
+
else
|
|
95
|
+
introLines.push(line);
|
|
96
|
+
});
|
|
97
|
+
const sections = raw.map((r) => ({
|
|
98
|
+
heading: r.heading,
|
|
99
|
+
text: r.lines.join("\n").trimEnd(),
|
|
100
|
+
startLine: r.start,
|
|
101
|
+
endLine: r.start + r.lines.length - 1,
|
|
102
|
+
headingOnly: !r.content,
|
|
103
|
+
}));
|
|
104
|
+
// A heading-only fragment joins the section before it (its parent), else the one after it.
|
|
105
|
+
const merged = [];
|
|
106
|
+
let pending = [];
|
|
107
|
+
for (const s of sections) {
|
|
108
|
+
if (s.headingOnly) {
|
|
109
|
+
const previous = merged[merged.length - 1];
|
|
110
|
+
if (previous) {
|
|
111
|
+
previous.text += `\n\n${s.text}`;
|
|
112
|
+
previous.endLine = s.endLine;
|
|
113
|
+
}
|
|
114
|
+
else
|
|
115
|
+
pending.push(s);
|
|
116
|
+
continue;
|
|
117
|
+
}
|
|
118
|
+
if (pending.length) {
|
|
119
|
+
s.text = `${pending.map((p) => p.text).join("\n\n")}\n\n${s.text}`;
|
|
120
|
+
s.startLine = pending[0]?.startLine ?? s.startLine;
|
|
121
|
+
pending = [];
|
|
122
|
+
}
|
|
123
|
+
merged.push(s);
|
|
124
|
+
}
|
|
125
|
+
return { title, intro: introLines.join("\n").trim(), sections: merged };
|
|
126
|
+
}
|
|
127
|
+
/** Maps an ADR status to the decision vocabulary; unknown wording gives no status. */
|
|
128
|
+
export function adrStatus(text) {
|
|
129
|
+
let value = /^\s*[*_]*status[*_]*\s*:[*_]*\s*(.+)$/im.exec(text)?.[1];
|
|
130
|
+
value ??= /^#{1,6}\s+status\s*\r?\n+\s*(\S.*)$/im.exec(text)?.[1];
|
|
131
|
+
const word = value ? plain(value).toLowerCase() : "";
|
|
132
|
+
if (/^(accepted|approved|adopted)/.test(word))
|
|
133
|
+
return "accepted";
|
|
134
|
+
if (/^decided/.test(word))
|
|
135
|
+
return "decided";
|
|
136
|
+
if (/^(proposed|draft|pending)/.test(word))
|
|
137
|
+
return "proposed";
|
|
138
|
+
if (/^rejected/.test(word))
|
|
139
|
+
return "rejected";
|
|
140
|
+
if (/^(superseded|deprecated|obsolete)/.test(word))
|
|
141
|
+
return "superseded";
|
|
142
|
+
return undefined;
|
|
143
|
+
}
|
|
144
|
+
/** Reads the committed HEAD tree and builds the docs-layer snapshot. */
|
|
145
|
+
export function extractDocs(root, config = {}) {
|
|
146
|
+
root = resolve(root);
|
|
147
|
+
const { repo, commit, namespace: codeNamespace } = repository(root);
|
|
148
|
+
const namespace = codeNamespace.replace(/^code:/, "docs:");
|
|
149
|
+
const nodes = [], edges = [];
|
|
150
|
+
const summary = { files: 0, sections: 0, truncated: 0 };
|
|
151
|
+
const location = (path, startLine = 1, endLine = startLine) => [
|
|
152
|
+
{ kind: "repo", repo, commit, path, startLine, endLine },
|
|
153
|
+
];
|
|
154
|
+
const add = (path, symbol, type, title, body, start, end, status, properties = {}) => {
|
|
155
|
+
const clientKey = `${namespace}:${path}${symbol ? `#${symbol}` : ""}`;
|
|
156
|
+
const capped = capBody(body);
|
|
157
|
+
title = title.length > 300 ? `${title.slice(0, 299)}…` : title;
|
|
158
|
+
if (clientKey.length > 1024)
|
|
159
|
+
throw new Error(`Document identity exceeds graph limits: ${path}`);
|
|
160
|
+
nodes.push({
|
|
161
|
+
clientKey,
|
|
162
|
+
namespace,
|
|
163
|
+
type,
|
|
164
|
+
title,
|
|
165
|
+
body: capped.body,
|
|
166
|
+
...(status ? { status } : {}),
|
|
167
|
+
properties: capped.truncated ? { ...properties, bodyTruncated: true } : properties,
|
|
168
|
+
provenance: location(path, start, end),
|
|
169
|
+
derivation: "extracted",
|
|
170
|
+
});
|
|
171
|
+
if (capped.truncated)
|
|
172
|
+
summary.truncated++;
|
|
173
|
+
return clientKey;
|
|
174
|
+
};
|
|
175
|
+
const link = (type, from, to, path) => edges.push({
|
|
176
|
+
type,
|
|
177
|
+
from,
|
|
178
|
+
to,
|
|
179
|
+
provenance: location(path),
|
|
180
|
+
derivation: "extracted",
|
|
181
|
+
properties: {},
|
|
182
|
+
});
|
|
183
|
+
const repoKey = add(".", "", "repository", `Repository ${repo} (docs)`, "", 1, 1);
|
|
184
|
+
const include = (config.include?.length ? config.include : DEFAULT_INCLUDE).map(globMatcher);
|
|
185
|
+
const exclude = (config.exclude ?? []).map(globMatcher);
|
|
186
|
+
const tracked = execFileSync("git", ["ls-tree", "-r", "-z", commit], {
|
|
187
|
+
cwd: root,
|
|
188
|
+
encoding: "utf8",
|
|
189
|
+
maxBuffer: Infinity,
|
|
190
|
+
})
|
|
191
|
+
.split("\0")
|
|
192
|
+
.filter(Boolean)
|
|
193
|
+
.flatMap((entry) => {
|
|
194
|
+
const tab = entry.indexOf("\t");
|
|
195
|
+
const [mode, type, sha] = entry.slice(0, tab).split(/\s+/);
|
|
196
|
+
const path = entry.slice(tab + 1);
|
|
197
|
+
return type === "blob" && mode !== "120000" && sha && MARKDOWN.test(path)
|
|
198
|
+
? [{ path, sha }]
|
|
199
|
+
: [];
|
|
200
|
+
})
|
|
201
|
+
.filter(({ path }) => include.some((m) => m(path)) && !exclude.some((m) => m(path)))
|
|
202
|
+
.sort((a, b) => (a.path < b.path ? -1 : 1));
|
|
203
|
+
for (const { path, sha } of tracked) {
|
|
204
|
+
const text = execFileSync("git", ["cat-file", "blob", sha], {
|
|
205
|
+
cwd: root,
|
|
206
|
+
encoding: "utf8",
|
|
207
|
+
maxBuffer: Infinity,
|
|
208
|
+
}).replace(new RegExp(`^${String.fromCharCode(0xfeff)}`), "");
|
|
209
|
+
const parsed = parseMarkdown(text);
|
|
210
|
+
const isAdr = ADR.some((m) => m(path));
|
|
211
|
+
const type = isAdr ? "decision" : PLAN(path) ? "plan" : "source";
|
|
212
|
+
const status = isAdr ? adrStatus(text) : type === "plan" ? "planned" : "recorded";
|
|
213
|
+
const lineCount = text.split(/\r?\n/).length;
|
|
214
|
+
const fileKey = add(path, "", type, parsed.title || posix.basename(path), isAdr ? text : parsed.intro, 1, lineCount, status, { path });
|
|
215
|
+
summary.files++;
|
|
216
|
+
link("derived_from", fileKey, repoKey, path);
|
|
217
|
+
const used = new Map();
|
|
218
|
+
for (const section of parsed.sections) {
|
|
219
|
+
const base = slugOf(section.heading);
|
|
220
|
+
const n = (used.get(base) ?? 0) + 1;
|
|
221
|
+
used.set(base, n);
|
|
222
|
+
const sectionKey = add(path, n > 1 ? `${base}-${String(n)}` : base, "excerpt", `${path}: ${section.heading}`, section.text, section.startLine, section.endLine, undefined, { path, heading: section.heading });
|
|
223
|
+
summary.sections++;
|
|
224
|
+
link("part_of", sectionKey, fileKey, path);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
227
|
+
return { nodes, edges, repo, commit, namespace, summary };
|
|
228
|
+
}
|
|
229
|
+
/** `cboard docs sync`: Markdown docs of this repository into the graph. */
|
|
230
|
+
export async function docsSync(argv) {
|
|
231
|
+
const args = options(argv);
|
|
232
|
+
const root = resolve(args.repo ?? process.cwd());
|
|
233
|
+
const { project, token, server } = resolveSync(args, root);
|
|
234
|
+
const snapshot = extractDocs(root, docsConfig(root));
|
|
235
|
+
const result = await reconcile(new ContextApi(server, project, token), snapshot, snapshot.namespace);
|
|
236
|
+
console.log(JSON.stringify({
|
|
237
|
+
commit: snapshot.commit,
|
|
238
|
+
nodes: snapshot.nodes.length,
|
|
239
|
+
edges: snapshot.edges.length,
|
|
240
|
+
contributions: result.contributions,
|
|
241
|
+
summary: snapshot.summary,
|
|
242
|
+
}));
|
|
243
|
+
}
|
package/dist/extract.js
CHANGED
|
@@ -5,6 +5,17 @@ import { posix, resolve } from "node:path";
|
|
|
5
5
|
import { adapterFor, openAdapter } from "./adapters/index.js";
|
|
6
6
|
import { globMatcher, languageOf } from "./languages.js";
|
|
7
7
|
const git = (root, args) => execFileSync("git", args, { cwd: root, encoding: "utf8" }).trim();
|
|
8
|
+
let warnedLocal = false;
|
|
9
|
+
/** Stable identity that `connect` stores in `.cboard.json` (the root commit hash). */
|
|
10
|
+
function configuredRepoId(root) {
|
|
11
|
+
try {
|
|
12
|
+
const id = JSON.parse(readFileSync(resolve(root, ".cboard.json"), "utf8")).repoId;
|
|
13
|
+
return typeof id === "string" ? id : "";
|
|
14
|
+
}
|
|
15
|
+
catch {
|
|
16
|
+
return "";
|
|
17
|
+
}
|
|
18
|
+
}
|
|
8
19
|
export function repository(root) {
|
|
9
20
|
root = resolve(root);
|
|
10
21
|
const commit = git(root, ["rev-parse", "HEAD"]);
|
|
@@ -26,7 +37,18 @@ export function repository(root) {
|
|
|
26
37
|
url.hash = "";
|
|
27
38
|
repo = url.toString();
|
|
28
39
|
}
|
|
29
|
-
|
|
40
|
+
if (!repo) {
|
|
41
|
+
const repoId = configuredRepoId(root);
|
|
42
|
+
if (repoId)
|
|
43
|
+
repo = `local:${repoId}`;
|
|
44
|
+
else {
|
|
45
|
+
repo = `local:${git(root, ["rev-parse", "--path-format=absolute", "--git-common-dir"]).replace(/\\/g, "/")}`;
|
|
46
|
+
if (!warnedLocal) {
|
|
47
|
+
warnedLocal = true;
|
|
48
|
+
console.error("cboard: no origin remote and no repoId in .cboard.json; this clone gets its own namespace. Run `cboard connect` to store a stable repoId.");
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
}
|
|
30
52
|
const namespace = `code:${createHash("sha256").update(repo).digest("hex").slice(0, 32)}`;
|
|
31
53
|
return { repo, commit, namespace };
|
|
32
54
|
}
|
package/dist/issues.d.ts
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { ContextApi } from "./api.js";
|
|
2
|
+
export interface GithubIssue {
|
|
3
|
+
number: number;
|
|
4
|
+
title: string;
|
|
5
|
+
body?: string | null;
|
|
6
|
+
state: string;
|
|
7
|
+
html_url: string;
|
|
8
|
+
labels?: (string | {
|
|
9
|
+
name?: string;
|
|
10
|
+
})[];
|
|
11
|
+
pull_request?: unknown;
|
|
12
|
+
}
|
|
13
|
+
export interface IssueRequest {
|
|
14
|
+
owner: string;
|
|
15
|
+
name: string;
|
|
16
|
+
state: "all" | "open";
|
|
17
|
+
limit?: number | undefined;
|
|
18
|
+
}
|
|
19
|
+
export type IssueSource = (request: IssueRequest) => Promise<GithubIssue[]>;
|
|
20
|
+
export interface IssuesOptions {
|
|
21
|
+
state: "all" | "open";
|
|
22
|
+
limit?: number | undefined;
|
|
23
|
+
}
|
|
24
|
+
/** owner/name from an https or ssh GitHub remote URL. */
|
|
25
|
+
export declare function parseGithubRemote(remote: string): {
|
|
26
|
+
owner: string;
|
|
27
|
+
name: string;
|
|
28
|
+
};
|
|
29
|
+
/** Flags: --repo, --project, --server, --token-env, --state all|open, --limit N. */
|
|
30
|
+
export declare function issuesSyncOptions(argv: string[]): Record<string, string> & IssuesOptions;
|
|
31
|
+
/** gh api first; GITHUB_TOKEN with fetch when gh is missing or fails. */
|
|
32
|
+
export declare const githubSource: IssueSource;
|
|
33
|
+
/**
|
|
34
|
+
* Mirrors issues into the graph with the code sync's reconcile, in its own namespace. When the
|
|
35
|
+
* list is partial (--state open or --limit) unseen issues are kept, not superseded.
|
|
36
|
+
*/
|
|
37
|
+
export declare function runIssuesSync(api: ContextApi, identity: {
|
|
38
|
+
repo: string;
|
|
39
|
+
namespace: string;
|
|
40
|
+
}, source: IssueSource, options: IssuesOptions): Promise<{
|
|
41
|
+
issues: number;
|
|
42
|
+
contributions: number;
|
|
43
|
+
}>;
|
|
44
|
+
export declare function issuesSync(argv: string[], source?: IssueSource): Promise<void>;
|