@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/dist/issues.js
ADDED
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { execFile } from "node:child_process";
|
|
2
|
+
import { createHash } from "node:crypto";
|
|
3
|
+
import { resolve } from "node:path";
|
|
4
|
+
import { promisify } from "node:util";
|
|
5
|
+
import { ContextApi, reconcile } from "./api.js";
|
|
6
|
+
import { resolveSync } from "./cli.js";
|
|
7
|
+
import { repository } from "./extract.js";
|
|
8
|
+
const run = promisify(execFile);
|
|
9
|
+
const BODY_MAX = 4000;
|
|
10
|
+
/** owner/name from an https or ssh GitHub remote URL. */
|
|
11
|
+
export function parseGithubRemote(remote) {
|
|
12
|
+
const match = /github\.com[/:]([^/\s]+)\/([^/\s]+?)(?:\.git)?\/?$/i.exec(remote);
|
|
13
|
+
if (!match?.[1] || !match[2])
|
|
14
|
+
throw new Error(`The origin remote is not a GitHub repository: ${remote || "(none)"}`);
|
|
15
|
+
return { owner: match[1], name: match[2] };
|
|
16
|
+
}
|
|
17
|
+
/** Flags: --repo, --project, --server, --token-env, --state all|open, --limit N. */
|
|
18
|
+
export function issuesSyncOptions(argv) {
|
|
19
|
+
const result = {};
|
|
20
|
+
for (let i = 0; i < argv.length; i += 2) {
|
|
21
|
+
const name = argv[i]?.replace(/^--/, "");
|
|
22
|
+
const value = argv[i + 1];
|
|
23
|
+
if (!name ||
|
|
24
|
+
!["repo", "project", "server", "token-env", "state", "limit"].includes(name) ||
|
|
25
|
+
!value ||
|
|
26
|
+
value.startsWith("--"))
|
|
27
|
+
throw new Error("Invalid arguments; use [--state all|open] [--limit N] [--repo PATH --project UUID --server URL --token-env ENV_NAME]");
|
|
28
|
+
result[name] = value;
|
|
29
|
+
}
|
|
30
|
+
const state = result.state ?? "all";
|
|
31
|
+
if (state !== "all" && state !== "open")
|
|
32
|
+
throw new Error("--state must be all|open");
|
|
33
|
+
const limit = result.limit === undefined ? undefined : Number(result.limit);
|
|
34
|
+
if (limit !== undefined && !(Number.isInteger(limit) && limit > 0))
|
|
35
|
+
throw new Error("--limit must be a positive integer");
|
|
36
|
+
return { ...result, state, ...(limit === undefined ? {} : { limit }) };
|
|
37
|
+
}
|
|
38
|
+
const NO_ACCESS = "Cannot read GitHub issues: install and authenticate the gh CLI (gh auth login) or set GITHUB_TOKEN";
|
|
39
|
+
/** gh api first; GITHUB_TOKEN with fetch when gh is missing or fails. */
|
|
40
|
+
export const githubSource = async ({ owner, name, state, limit }) => {
|
|
41
|
+
const path = `repos/${owner}/${name}/issues?state=${state}&per_page=100`;
|
|
42
|
+
try {
|
|
43
|
+
const { stdout } = await run("gh", ["api", "--paginate", path, "--jq", ".[]"], {
|
|
44
|
+
maxBuffer: 256 * 1024 * 1024,
|
|
45
|
+
});
|
|
46
|
+
return stdout
|
|
47
|
+
.split("\n")
|
|
48
|
+
.filter(Boolean)
|
|
49
|
+
.map((line) => JSON.parse(line));
|
|
50
|
+
}
|
|
51
|
+
catch (error) {
|
|
52
|
+
const token = process.env.GITHUB_TOKEN?.trim();
|
|
53
|
+
if (!token)
|
|
54
|
+
throw new Error(NO_ACCESS, { cause: error });
|
|
55
|
+
return fetchIssues(path, token, limit);
|
|
56
|
+
}
|
|
57
|
+
};
|
|
58
|
+
async function fetchIssues(path, token, limit) {
|
|
59
|
+
const issues = [];
|
|
60
|
+
for (let page = 1;; page++) {
|
|
61
|
+
const response = await fetch(`https://api.github.com/${path}&page=${String(page)}`, {
|
|
62
|
+
headers: {
|
|
63
|
+
authorization: `Bearer ${token}`,
|
|
64
|
+
accept: "application/vnd.github+json",
|
|
65
|
+
"user-agent": "cboard",
|
|
66
|
+
},
|
|
67
|
+
});
|
|
68
|
+
if (!response.ok)
|
|
69
|
+
throw new Error(`GitHub API ${String(response.status)} for ${path}`);
|
|
70
|
+
const batch = (await response.json());
|
|
71
|
+
issues.push(...batch);
|
|
72
|
+
// Pull requests are dropped later, so the limit is applied there, not here.
|
|
73
|
+
if (batch.length < 100 || (limit !== undefined && issues.length >= limit * 4))
|
|
74
|
+
return issues;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
function snapshotOf(issues, namespace, repoKey) {
|
|
78
|
+
const nodes = [];
|
|
79
|
+
const edges = [];
|
|
80
|
+
for (const i of issues) {
|
|
81
|
+
const clientKey = `${namespace}:#${String(i.number)}`;
|
|
82
|
+
// Issue nodes cite the issue URL (the graph's url provenance), not a repository commit.
|
|
83
|
+
const provenance = [{ kind: "url", ref: i.html_url }];
|
|
84
|
+
nodes.push({
|
|
85
|
+
clientKey,
|
|
86
|
+
namespace,
|
|
87
|
+
type: "open_issue",
|
|
88
|
+
title: `#${String(i.number)} ${i.title}`.slice(0, 300),
|
|
89
|
+
body: (i.body ?? "").trim().slice(0, BODY_MAX),
|
|
90
|
+
status: i.state === "open" ? "open" : "resolved",
|
|
91
|
+
properties: {
|
|
92
|
+
url: i.html_url,
|
|
93
|
+
labels: (i.labels ?? [])
|
|
94
|
+
.map((l) => (typeof l === "string" ? l : l.name))
|
|
95
|
+
.filter((l) => !!l),
|
|
96
|
+
},
|
|
97
|
+
provenance,
|
|
98
|
+
derivation: "extracted",
|
|
99
|
+
});
|
|
100
|
+
edges.push({
|
|
101
|
+
type: "part_of",
|
|
102
|
+
from: clientKey,
|
|
103
|
+
to: repoKey,
|
|
104
|
+
provenance,
|
|
105
|
+
derivation: "extracted",
|
|
106
|
+
properties: {},
|
|
107
|
+
});
|
|
108
|
+
}
|
|
109
|
+
return { nodes, edges };
|
|
110
|
+
}
|
|
111
|
+
/**
|
|
112
|
+
* Mirrors issues into the graph with the code sync's reconcile, in its own namespace. When the
|
|
113
|
+
* list is partial (--state open or --limit) unseen issues are kept, not superseded.
|
|
114
|
+
*/
|
|
115
|
+
export async function runIssuesSync(api, identity, source, options) {
|
|
116
|
+
const { owner, name } = parseGithubRemote(identity.repo);
|
|
117
|
+
const repoKey = `${identity.namespace}:.`;
|
|
118
|
+
const repoNode = (await api.all({ namespace: identity.namespace, types: "repository" })).nodes.find((n) => n.clientKey === repoKey && n.lifecycle === "active");
|
|
119
|
+
if (!repoNode)
|
|
120
|
+
throw new Error("The repository node is missing from the graph; run `cboard sync` first");
|
|
121
|
+
const namespace = `issues:${createHash("sha256").update(identity.repo).digest("hex").slice(0, 32)}`;
|
|
122
|
+
const fetched = await source({ owner, name, state: options.state, limit: options.limit });
|
|
123
|
+
const issues = fetched
|
|
124
|
+
.filter((i) => !i.pull_request)
|
|
125
|
+
.sort((a, b) => a.number - b.number)
|
|
126
|
+
.slice(0, options.limit);
|
|
127
|
+
const snapshot = snapshotOf(issues, namespace, repoKey);
|
|
128
|
+
const complete = options.state === "all" && options.limit === undefined;
|
|
129
|
+
const result = await reconcile(api, snapshot, namespace, complete, new Map([[repoKey, repoNode.id]]));
|
|
130
|
+
return { issues: issues.length, contributions: result.contributions };
|
|
131
|
+
}
|
|
132
|
+
export async function issuesSync(argv, source = githubSource) {
|
|
133
|
+
const args = issuesSyncOptions(argv);
|
|
134
|
+
const root = resolve(args.repo ?? process.cwd());
|
|
135
|
+
const { project, token, server } = resolveSync(args, root);
|
|
136
|
+
const result = await runIssuesSync(new ContextApi(server, project, token), repository(root), source, { state: args.state, limit: args.limit });
|
|
137
|
+
console.log(JSON.stringify(result));
|
|
138
|
+
}
|
package/dist/wake.d.ts
ADDED
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare const WAKE_MESSAGE = "hosted service is waking up (up to a minute)";
|
|
2
|
+
export declare const WAKE_BUDGET_MS = 90000;
|
|
3
|
+
export interface WakeOptions {
|
|
4
|
+
budgetMs?: number;
|
|
5
|
+
attemptMs?: number;
|
|
6
|
+
onWake?: (message: string) => void;
|
|
7
|
+
sleep?: (ms: number) => Promise<void>;
|
|
8
|
+
now?: () => number;
|
|
9
|
+
}
|
|
10
|
+
/** A failure that may be the service waking: a network error, a timeout or a 5xx. */
|
|
11
|
+
export declare function isTransient(error: unknown): boolean;
|
|
12
|
+
/** Runs `attempt` until it succeeds, fails for good, or the budget is spent (backoff 1 s to 10 s). */
|
|
13
|
+
export declare function retryWake<T>(attempt: (timeoutMs: number) => Promise<T>, { budgetMs, attemptMs, onWake, sleep, now, }?: WakeOptions): Promise<T>;
|
package/dist/wake.js
ADDED
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
// Cold-start wait for the code-sync client. Mirrors scripts/cboard-kit/wait.js (the kit is plain
|
|
2
|
+
// JS that ships separately and cannot be imported here); keep the two in step.
|
|
3
|
+
export const WAKE_MESSAGE = "hosted service is waking up (up to a minute)";
|
|
4
|
+
export const WAKE_BUDGET_MS = 90_000;
|
|
5
|
+
/** A failure that may be the service waking: a network error, a timeout or a 5xx. */
|
|
6
|
+
export function isTransient(error) {
|
|
7
|
+
const status = error?.status;
|
|
8
|
+
if (typeof status === "number")
|
|
9
|
+
return status >= 500;
|
|
10
|
+
return error instanceof TypeError || error?.name === "TimeoutError";
|
|
11
|
+
}
|
|
12
|
+
/** Runs `attempt` until it succeeds, fails for good, or the budget is spent (backoff 1 s to 10 s). */
|
|
13
|
+
export async function retryWake(attempt, { budgetMs = WAKE_BUDGET_MS, attemptMs = 30_000, onWake = (m) => void process.stderr.write(`cboard: ${m}\n`), sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)), now = Date.now, } = {}) {
|
|
14
|
+
const start = now();
|
|
15
|
+
let delay = 1000;
|
|
16
|
+
let woke = false;
|
|
17
|
+
for (;;) {
|
|
18
|
+
const left = budgetMs - (now() - start);
|
|
19
|
+
try {
|
|
20
|
+
return await attempt(Math.max(1000, Math.min(attemptMs, left)));
|
|
21
|
+
}
|
|
22
|
+
catch (error) {
|
|
23
|
+
if (!isTransient(error) || budgetMs - (now() - start) <= delay)
|
|
24
|
+
throw error;
|
|
25
|
+
if (!woke) {
|
|
26
|
+
woke = true;
|
|
27
|
+
onWake(WAKE_MESSAGE);
|
|
28
|
+
}
|
|
29
|
+
await sleep(delay);
|
|
30
|
+
delay = Math.min(delay * 2, 10_000);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|