@codyn06/cboard 0.0.1 → 0.1.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.
Files changed (107) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +68 -3
  3. package/THIRD_PARTY_NOTICES +29 -0
  4. package/bin/cboard-context.js +15 -0
  5. package/bin/cboard.js +5 -0
  6. package/dist/adapters/bash.d.ts +2 -0
  7. package/dist/adapters/bash.js +14 -0
  8. package/dist/adapters/c-family.d.ts +2 -0
  9. package/dist/adapters/c-family.js +81 -0
  10. package/dist/adapters/csharp.d.ts +2 -0
  11. package/dist/adapters/csharp.js +47 -0
  12. package/dist/adapters/extra-languages.d.ts +3 -0
  13. package/dist/adapters/extra-languages.js +15 -0
  14. package/dist/adapters/go.d.ts +2 -0
  15. package/dist/adapters/go.js +42 -0
  16. package/dist/adapters/grammar.d.ts +21 -0
  17. package/dist/adapters/grammar.js +54 -0
  18. package/dist/adapters/index.d.ts +20 -0
  19. package/dist/adapters/index.js +62 -0
  20. package/dist/adapters/java.d.ts +2 -0
  21. package/dist/adapters/java.js +36 -0
  22. package/dist/adapters/kotlin.d.ts +2 -0
  23. package/dist/adapters/kotlin.js +65 -0
  24. package/dist/adapters/lua-family.d.ts +2 -0
  25. package/dist/adapters/lua-family.js +186 -0
  26. package/dist/adapters/php.d.ts +2 -0
  27. package/dist/adapters/php.js +28 -0
  28. package/dist/adapters/python.d.ts +2 -0
  29. package/dist/adapters/python.js +68 -0
  30. package/dist/adapters/ruby.d.ts +2 -0
  31. package/dist/adapters/ruby.js +46 -0
  32. package/dist/adapters/rust.d.ts +2 -0
  33. package/dist/adapters/rust.js +85 -0
  34. package/dist/adapters/swift.d.ts +2 -0
  35. package/dist/adapters/swift.js +38 -0
  36. package/dist/adapters/types.d.ts +61 -0
  37. package/dist/adapters/types.js +1 -0
  38. package/dist/adapters/typescript.d.ts +2 -0
  39. package/dist/adapters/typescript.js +52 -0
  40. package/dist/api.d.ts +50 -0
  41. package/dist/api.js +421 -0
  42. package/dist/cboard.d.ts +2 -0
  43. package/dist/cboard.js +97 -0
  44. package/dist/cli.d.ts +27 -0
  45. package/dist/cli.js +180 -0
  46. package/dist/docs-parse.d.ts +64 -0
  47. package/dist/docs-parse.js +762 -0
  48. package/dist/extract.d.ts +36 -0
  49. package/dist/extract.js +458 -0
  50. package/dist/index.d.ts +5 -0
  51. package/dist/index.js +5 -0
  52. package/dist/languages.d.ts +9 -0
  53. package/dist/languages.js +128 -0
  54. package/dist/model.d.ts +85 -0
  55. package/dist/model.js +1 -0
  56. package/grammars/LICENSES/README.md +3 -0
  57. package/grammars/LICENSES/c.LICENSE +21 -0
  58. package/grammars/LICENSES/kotlin.LICENSE +21 -0
  59. package/grammars/LICENSES/lua.LICENSE +21 -0
  60. package/grammars/LICENSES/luau.LICENSE +21 -0
  61. package/grammars/LICENSES/swift.LICENSE +21 -0
  62. package/grammars/manifest.json +39 -0
  63. package/grammars/tree-sitter-c.wasm +0 -0
  64. package/grammars/tree-sitter-kotlin.wasm +0 -0
  65. package/grammars/tree-sitter-lua.wasm +0 -0
  66. package/grammars/tree-sitter-luau.wasm +0 -0
  67. package/grammars/tree-sitter-swift.wasm +0 -0
  68. package/kit/cboard-connect.js +490 -0
  69. package/kit/cboard-kit/brief.js +386 -0
  70. package/kit/cboard-kit/config.js +215 -0
  71. package/kit/cboard-kit/connect-check.js +52 -0
  72. package/kit/cboard-kit/headers.js +25 -0
  73. package/kit/cboard-kit/mcp.js +62 -0
  74. package/kit/cboard-kit/skill/SKILL.md +154 -0
  75. package/package.json +47 -5
  76. package/queries/bash/imports.scm +11 -0
  77. package/queries/bash/symbols.scm +1 -0
  78. package/queries/c/imports.scm +2 -0
  79. package/queries/c/symbols.scm +10 -0
  80. package/queries/cpp/imports.scm +2 -0
  81. package/queries/cpp/symbols.scm +11 -0
  82. package/queries/csharp/imports.scm +2 -0
  83. package/queries/csharp/symbols.scm +10 -0
  84. package/queries/go/imports.scm +2 -0
  85. package/queries/go/symbols.scm +8 -0
  86. package/queries/java/imports.scm +2 -0
  87. package/queries/java/symbols.scm +9 -0
  88. package/queries/javascript/imports.scm +3 -0
  89. package/queries/javascript/symbols.scm +16 -0
  90. package/queries/kotlin/imports.scm +2 -0
  91. package/queries/kotlin/symbols.scm +4 -0
  92. package/queries/lua/imports.scm +5 -0
  93. package/queries/lua/symbols.scm +6 -0
  94. package/queries/luau/imports.scm +5 -0
  95. package/queries/luau/symbols.scm +9 -0
  96. package/queries/php/imports.scm +6 -0
  97. package/queries/php/symbols.scm +7 -0
  98. package/queries/python/imports.scm +3 -0
  99. package/queries/python/symbols.scm +8 -0
  100. package/queries/ruby/imports.scm +2 -0
  101. package/queries/ruby/symbols.scm +4 -0
  102. package/queries/rust/imports.scm +5 -0
  103. package/queries/rust/symbols.scm +11 -0
  104. package/queries/swift/imports.scm +2 -0
  105. package/queries/swift/symbols.scm +5 -0
  106. package/queries/typescript/imports.scm +3 -0
  107. package/queries/typescript/symbols.scm +20 -0
package/dist/cli.js ADDED
@@ -0,0 +1,180 @@
1
+ #!/usr/bin/env node
2
+ import { execFileSync } from "node:child_process";
3
+ import { existsSync, readFileSync } from "node:fs";
4
+ import { mkdir, writeFile, rename } from "node:fs/promises";
5
+ import { dirname, isAbsolute, posix, relative, resolve, sep, win32 } from "node:path";
6
+ import { pathToFileURL } from "node:url";
7
+ import { ContextApi, reconcile } from "./api.js";
8
+ import { codeConfig, extract } from "./extract.js";
9
+ export function options(argv) {
10
+ const result = {};
11
+ for (let i = 0; i < argv.length; i += 2) {
12
+ const name = argv[i]?.replace(/^--/, "");
13
+ const value = argv[i + 1];
14
+ if (!name ||
15
+ !["project", "repo", "server", "token-env", "state"].includes(name) ||
16
+ !value ||
17
+ value.startsWith("--"))
18
+ throw new Error("Invalid arguments; use [--repo PATH (default: current directory)] [--project UUID --server URL --token-env ENV_NAME --state PATH] (project, server and token come from .cboard.json when omitted)");
19
+ result[name] = value;
20
+ }
21
+ return result;
22
+ }
23
+ /** The repository's .cboard.json (written by cboard-connect), or {} when absent. */
24
+ export function repoConfig(root) {
25
+ const file = resolve(root, ".cboard.json");
26
+ if (!existsSync(file))
27
+ return {};
28
+ let parsed;
29
+ try {
30
+ parsed = JSON.parse(readFileSync(file, "utf8"));
31
+ }
32
+ catch {
33
+ throw new Error(".cboard.json is not valid JSON");
34
+ }
35
+ if (typeof parsed !== "object" || parsed === null || Array.isArray(parsed))
36
+ return {};
37
+ const text = (key) => {
38
+ const value = parsed[key];
39
+ return typeof value === "string" && value.trim() ? value.trim() : undefined;
40
+ };
41
+ const config = {};
42
+ for (const key of ["server", "projectId", "tokenFile", "tokenEnv"]) {
43
+ const value = text(key);
44
+ if (value)
45
+ config[key] = value;
46
+ }
47
+ return config;
48
+ }
49
+ // Keep equal to the kit's constants; tests/unit/code-context-kit-parity.test.ts checks it until
50
+ // both share one module (#119).
51
+ export const DEFAULT_SERVER = "https://cboard-fij7.onrender.com";
52
+ export const DEFAULT_TOKEN_ENV = "CBOARD_PAT";
53
+ const TOKEN_ENV = /^CBOARD_[A-Z0-9_]+$/;
54
+ const isLocalHost = (url) => url.hostname === "127.0.0.1" || url.hostname === "localhost";
55
+ /**
56
+ * A path from .cboard.json must be relative and stay inside the repository. Mirrors
57
+ * validateRepoPath in scripts/cboard-kit/config.js: Windows and POSIX absolute forms and `..`
58
+ * segments with either separator are refused on every platform.
59
+ */
60
+ function insideRepo(root, path, key) {
61
+ const rel = relative(root, resolve(root, path));
62
+ if (isAbsolute(path) ||
63
+ win32.isAbsolute(path) ||
64
+ posix.isAbsolute(path) ||
65
+ path.split(/[\\/]/).includes("..") ||
66
+ rel === "" ||
67
+ rel === ".." ||
68
+ rel.startsWith(`..${sep}`) ||
69
+ isAbsolute(rel))
70
+ throw new Error(`.cboard.json: ${key} must be a relative path inside the repository`);
71
+ return resolve(root, path);
72
+ }
73
+ /**
74
+ * Which server, project and token a sync uses. Explicit flags win over the environment, which
75
+ * wins over .cboard.json. .cboard.json is tracked, so a cloned repository must not be able to
76
+ * steer a token elsewhere: these rules mirror resolveConfig in scripts/cboard-kit/config.js
77
+ * (keep them in step): tokenEnv must be CBOARD_*, tokenFile must stay inside the repository, a
78
+ * non-default server named only by the file needs CBOARD_TRUSTED_SERVERS, non-local servers
79
+ * must be https, and a local server never receives the hosted token file. The token is never logged.
80
+ */
81
+ export function resolveSync(args, root) {
82
+ const config = repoConfig(root);
83
+ const project = args.project ?? process.env.CBOARD_PROJECT_ID ?? config.projectId;
84
+ if (!project)
85
+ throw new Error("--project is required (or run cboard-connect so .cboard.json names it)");
86
+ const trusted = args.server ?? process.env.CBOARD_GRAPH_URL?.trim();
87
+ const configured = trusted ?? config.server ?? DEFAULT_SERVER;
88
+ let url;
89
+ try {
90
+ url = new URL(configured);
91
+ }
92
+ catch {
93
+ throw new Error(`server URL is not valid: ${configured}`);
94
+ }
95
+ const local = isLocalHost(url);
96
+ if (!local && url.protocol !== "https:")
97
+ throw new Error("the cboard server must use https");
98
+ if (local && url.protocol !== "http:" && url.protocol !== "https:")
99
+ throw new Error("the cboard server must use http or https");
100
+ if (!local && !trusted) {
101
+ const origins = [DEFAULT_SERVER, ...(process.env.CBOARD_TRUSTED_SERVERS ?? "").split(",")]
102
+ .map((entry) => {
103
+ try {
104
+ return new URL(entry.trim()).origin;
105
+ }
106
+ catch {
107
+ return "";
108
+ }
109
+ })
110
+ .filter(Boolean);
111
+ if (!origins.includes(url.origin))
112
+ throw new Error(`.cboard.json names the server ${url.origin}, which is not the default cboard server; no token is sent there. If you trust it, set CBOARD_TRUSTED_SERVERS=${url.origin}`);
113
+ }
114
+ const tokenEnv = args["token-env"] ?? config.tokenEnv ?? DEFAULT_TOKEN_ENV;
115
+ if (!args["token-env"] && !TOKEN_ENV.test(tokenEnv))
116
+ throw new Error(`.cboard.json: tokenEnv must match ${String(TOKEN_ENV)}`);
117
+ const fileName = local ? ".local/cboard.pat" : (config.tokenFile ?? ".cboard/token");
118
+ // Same order as readToken in the kit (#91): a non-default tokenEnv variable, the repo token
119
+ // file, then CBOARD_PAT. --token-env reads only its variable.
120
+ let token = "";
121
+ if (args["token-env"])
122
+ token = process.env[tokenEnv]?.trim() ?? "";
123
+ else {
124
+ if (tokenEnv !== DEFAULT_TOKEN_ENV)
125
+ token = process.env[tokenEnv]?.trim() ?? "";
126
+ const file = insideRepo(root, fileName, "tokenFile");
127
+ if (!token && existsSync(file))
128
+ token = readFileSync(file, "utf8").trim();
129
+ token ||= process.env[DEFAULT_TOKEN_ENV]?.trim() ?? "";
130
+ }
131
+ if (!token)
132
+ throw new Error(`PAT is empty: set ${tokenEnv} or save it to ${fileName} (requires graph:read and graph:context)`);
133
+ return { project, token, server: configured };
134
+ }
135
+ export async function sync(argv) {
136
+ const args = options(argv);
137
+ const root = resolve(args.repo ?? process.cwd());
138
+ const { project, token, server } = resolveSync(args, root);
139
+ const api = new ContextApi(server, project, token);
140
+ const snapshot = await extract(root, [], codeConfig(root));
141
+ const result = await reconcile(api, snapshot, snapshot.namespace);
142
+ const state = args.state
143
+ ? resolve(args.state)
144
+ : execFileSync("git", ["rev-parse", "--path-format=absolute", "--git-path", "cboard-context.json"], { cwd: root, encoding: "utf8" }).trim();
145
+ await mkdir(dirname(state), { recursive: true });
146
+ const temporary = `${state}.${String(process.pid)}.tmp`;
147
+ await writeFile(temporary, JSON.stringify({
148
+ project,
149
+ repo: snapshot.repo,
150
+ commit: snapshot.commit,
151
+ digest: result.digest,
152
+ }));
153
+ await rename(temporary, state);
154
+ console.log(JSON.stringify({
155
+ commit: snapshot.commit,
156
+ nodes: snapshot.nodes.length,
157
+ edges: snapshot.edges.length,
158
+ contributions: result.contributions,
159
+ summary: snapshot.summary,
160
+ }));
161
+ const { summary } = snapshot;
162
+ const count = (counts) => Object.entries(counts)
163
+ .filter(([, n]) => n > 0)
164
+ .map(([name, n]) => `${name} ${String(n)}`)
165
+ .join(", ") || "none";
166
+ if (summary.dirty)
167
+ console.error("uncommitted changes were not synced (HEAD was read)");
168
+ console.error(`files by language: ${count(summary.files)}; symbols ${String(summary.symbols)}; edges ${String(summary.edges)}; skipped: ${count(summary.skipped)}; parse errors: ${summary.parseErrors.length ? summary.parseErrors.join(", ") : "none"}`);
169
+ }
170
+ if (process.argv[1] && import.meta.url === pathToFileURL(resolve(process.argv[1])).href) {
171
+ if (process.argv[2] !== "sync") {
172
+ console.error("Usage: cboard-context sync --repo PATH [--project UUID --server URL --token-env CBOARD_PAT]");
173
+ process.exitCode = 1;
174
+ }
175
+ else
176
+ await sync(process.argv.slice(3)).catch((error) => {
177
+ console.error(error.message);
178
+ process.exitCode = 1;
179
+ });
180
+ }
@@ -0,0 +1,64 @@
1
+ export interface DocFile {
2
+ /** Repo-relative POSIX path, for example "docs/plans/local-milestone.md". */
3
+ path: string;
4
+ text: string;
5
+ }
6
+ export interface ParsedNode {
7
+ /** Stable client key, for example "doc:docs/plans/local-milestone.md#T6". */
8
+ key: string;
9
+ type: string;
10
+ title: string;
11
+ body: string;
12
+ status?: string;
13
+ path: string;
14
+ /** 1-based inclusive line range in `path`. */
15
+ startLine: number;
16
+ endLine: number;
17
+ }
18
+ export interface ParsedEdge {
19
+ type: string;
20
+ /** Client keys of the endpoint nodes. */
21
+ from: string;
22
+ to: string;
23
+ path: string;
24
+ line: number;
25
+ }
26
+ export interface Mismatch {
27
+ wanted: string;
28
+ used: string;
29
+ reason: string;
30
+ count: number;
31
+ }
32
+ export interface ParsedDocs {
33
+ nodes: ParsedNode[];
34
+ edges: ParsedEdge[];
35
+ mismatches: Mismatch[];
36
+ notes: string[];
37
+ }
38
+ export declare const MAX_BODY_BYTES = 8192;
39
+ export declare function splitLines(text: string): string[];
40
+ export declare function stripMd(s: string): string;
41
+ export declare function slug(s: string): string;
42
+ export interface Unit {
43
+ kind: "bullet" | "number" | "para" | "row";
44
+ /** Leading number of a numbered item. */
45
+ num?: string;
46
+ /** 0-based inclusive line indexes. */
47
+ start: number;
48
+ end: number;
49
+ lines: string[];
50
+ /** Header cells when `kind` is "row". */
51
+ headers?: string[];
52
+ }
53
+ export declare function splitCells(line: string): string[];
54
+ export declare function splitUnits(lines: string[], from: number, to: number): Unit[];
55
+ export declare function unitText(u: Unit): string;
56
+ export declare function dRefs(s: string): number[];
57
+ export declare function tRefs(s: string): string[];
58
+ export declare function invRefs(s: string): string[];
59
+ /** Parse the cboard docs. Files not present in `files` are skipped; order of `files` does not matter. */
60
+ export declare function parseDocs(files: DocFile[]): ParsedDocs;
61
+ export declare function provenanceDetail(n: {
62
+ startLine: number;
63
+ endLine: number;
64
+ }, commit: string): string;