@ferronhooi/aihub 0.1.0-next.2 → 0.1.0-next.4
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/commands/doctor.js +21 -0
- package/dist/commands/fabric.js +71 -0
- package/dist/commands/init.js +18 -0
- package/dist/commands/robot.js +53 -0
- package/dist/commands/skills.js +44 -0
- package/dist/lib/changes.js +14 -0
- package/dist/lib/clients.js +26 -0
- package/dist/lib/doctor.js +118 -0
- package/dist/lib/fabric.js +232 -0
- package/dist/lib/glob.js +28 -0
- package/dist/lib/init.js +157 -0
- package/dist/lib/install.js +141 -0
- package/dist/lib/pack.js +48 -0
- package/dist/lib/robot.js +88 -0
- package/dist/lib/schema.js +56 -0
- package/dist/router.js +12 -1
- package/dist/tools/lintSkills.js +1 -0
- package/pack/commands/copilot/cherry-pick.prompt.md +6 -0
- package/pack/commands/copilot/pr-update.prompt.md +6 -0
- package/pack/commands/copilot/pr.prompt.md +6 -0
- package/pack/commands/copilot/refine.prompt.md +6 -0
- package/pack/commands/copilot/review.prompt.md +6 -0
- package/pack/commands/copilot/story.prompt.md +6 -0
- package/pack/commands/copilot/test.prompt.md +6 -0
- package/pack/commands/copilot/watch.prompt.md +6 -0
- package/pack/commands/opencode/cherry-pick.md +5 -0
- package/pack/commands/opencode/pr-update.md +5 -0
- package/pack/commands/opencode/pr.md +5 -0
- package/pack/commands/opencode/refine.md +5 -0
- package/pack/commands/opencode/review.md +5 -0
- package/pack/commands/opencode/story.md +5 -0
- package/pack/commands/opencode/test.md +5 -0
- package/pack/commands/opencode/watch.md +5 -0
- package/pack/skills/_shared/assets/commit-template.nl.txt +5 -0
- package/pack/skills/_shared/assets/commit-template.txt +5 -0
- package/pack/skills/_shared/assets/pull_request_template.md +16 -0
- package/pack/skills/_shared/assets/pull_request_template.nl.md +16 -0
- package/pack/skills/_shared/assets/story-template.md +12 -0
- package/pack/skills/_shared/assets/story-template.nl.md +12 -0
- package/pack/skills/_shared/references/team.schema.yaml +56 -0
- package/pack/skills/branch-sync/SKILL.md +31 -0
- package/pack/skills/implement/SKILL.md +25 -0
- package/pack/skills/pipeline-fix/SKILL.md +29 -0
- package/pack/skills/pr-create/SKILL.md +29 -0
- package/pack/skills/pr-update/SKILL.md +28 -0
- package/pack/skills/pr-watch/SKILL.md +30 -0
- package/pack/skills/release/SKILL.md +35 -0
- package/pack/skills/review-process/SKILL.md +35 -0
- package/pack/skills/start-here/SKILL.md +36 -0
- package/pack/skills/story-create/SKILL.md +28 -0
- package/pack/skills/story-refine/SKILL.md +28 -0
- package/pack/skills/test-run/SKILL.md +35 -0
- package/pack/skills/test-run/references/fabric-robot.md +23 -0
- package/package.json +4 -3
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { AihubError } from "../lib/errors.js";
|
|
3
|
+
import { formatChecks, runChecks } from "../lib/doctor.js";
|
|
4
|
+
export async function run(argv, ctx) {
|
|
5
|
+
let json = false;
|
|
6
|
+
try {
|
|
7
|
+
json = !!parseArgs({ args: argv, options: { json: { type: "boolean" } } }).values.json;
|
|
8
|
+
}
|
|
9
|
+
catch (e) {
|
|
10
|
+
throw new AihubError(e.message);
|
|
11
|
+
}
|
|
12
|
+
const checks = runChecks(ctx);
|
|
13
|
+
if (json) {
|
|
14
|
+
ctx.out(JSON.stringify({ ok: checks.every((c) => c.status === "OK"), checks }));
|
|
15
|
+
return;
|
|
16
|
+
}
|
|
17
|
+
for (const l of formatChecks(checks))
|
|
18
|
+
ctx.out(l);
|
|
19
|
+
const bad = checks.filter((c) => c.status === "FIX").length;
|
|
20
|
+
ctx.out(bad ? `${bad} check(s) need a fix` : "all checks passed");
|
|
21
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
import { readFileSync } from "node:fs";
|
|
2
|
+
import { parseArgs } from "node:util";
|
|
3
|
+
import { changedFiles } from "../lib/changes.js";
|
|
4
|
+
import { AihubError } from "../lib/errors.js";
|
|
5
|
+
import { buildPlan, chooseWorkspace, executionDataFromParams, fabricWorkspaces, FabricClient, formatJob, formatPlan, guardEnv, makeFabricAuth, waitForJob, } from "../lib/fabric.js";
|
|
6
|
+
import { git, repoRoot } from "../lib/git.js";
|
|
7
|
+
import { loadTeam } from "../lib/team.js";
|
|
8
|
+
const USAGE = "usage: aihub fabric plan|run|status [flags]";
|
|
9
|
+
export async function run(argv, ctx) {
|
|
10
|
+
const [sub, ...rest] = argv;
|
|
11
|
+
let v;
|
|
12
|
+
try {
|
|
13
|
+
v = parseArgs({
|
|
14
|
+
args: rest,
|
|
15
|
+
options: {
|
|
16
|
+
branch: { type: "string" },
|
|
17
|
+
target: { type: "string" },
|
|
18
|
+
workspace: { type: "string" },
|
|
19
|
+
item: { type: "string" },
|
|
20
|
+
job: { type: "string" },
|
|
21
|
+
"params-file": { type: "string" },
|
|
22
|
+
"allow-env": { type: "string" },
|
|
23
|
+
wait: { type: "boolean" },
|
|
24
|
+
},
|
|
25
|
+
}).values;
|
|
26
|
+
}
|
|
27
|
+
catch (e) {
|
|
28
|
+
throw new AihubError(e.message);
|
|
29
|
+
}
|
|
30
|
+
if (sub !== "plan" && sub !== "run" && sub !== "status")
|
|
31
|
+
throw new AihubError(USAGE);
|
|
32
|
+
const root = repoRoot(ctx.cwd);
|
|
33
|
+
const team = loadTeam(root);
|
|
34
|
+
const list = fabricWorkspaces(team);
|
|
35
|
+
const given = v.branch;
|
|
36
|
+
if (sub === "plan") {
|
|
37
|
+
const c = changedFiles(root, team, { branch: given, target: v.target });
|
|
38
|
+
ctx.out(formatPlan(buildPlan(c.files, list, { branch: c.branch, workspace: v.workspace }), c.branch, c.target));
|
|
39
|
+
return;
|
|
40
|
+
}
|
|
41
|
+
const branch = given ?? git(root, ["rev-parse", "--abbrev-ref", "HEAD"], { allowFail: true }).trim();
|
|
42
|
+
const ws = chooseWorkspace(list, { workspace: v.workspace, branch });
|
|
43
|
+
if (!ws) {
|
|
44
|
+
if (v.workspace)
|
|
45
|
+
throw new AihubError(`workspace '${v.workspace}' is not in team.yaml fabric.workspaces; the CLI only runs workspaces the team listed`);
|
|
46
|
+
throw new AihubError("no dev/feature workspace in team.yaml fabric.workspaces; pass --workspace <name|id>");
|
|
47
|
+
}
|
|
48
|
+
guardEnv(ws, v["allow-env"]);
|
|
49
|
+
if (!v.item)
|
|
50
|
+
throw new AihubError(`aihub fabric ${sub} needs --item <name|id>`);
|
|
51
|
+
if (sub === "status" && !v.job)
|
|
52
|
+
throw new AihubError("aihub fabric status needs --job <id> and --item <name|id>");
|
|
53
|
+
const client = new FabricClient(makeFabricAuth({ env: ctx.env, exec: ctx.exec, cacheFile: ctx.tokenCache && `${ctx.tokenCache}.fabric` }), ctx.fetch);
|
|
54
|
+
const wsId = await client.workspaceId(ws);
|
|
55
|
+
const item = await client.resolveItem(wsId, v.item);
|
|
56
|
+
if (sub === "status") {
|
|
57
|
+
ctx.out(formatJob(await client.jobStatus(wsId, item.id, v.job), item.displayName));
|
|
58
|
+
return;
|
|
59
|
+
}
|
|
60
|
+
const exec = v["params-file"] ? executionDataFromParams(readFileSync(v["params-file"], "utf8"), item) : undefined;
|
|
61
|
+
const jobId = await client.startJob(wsId, item, exec);
|
|
62
|
+
ctx.out(`started ${item.type} ${item.displayName} in ${ws.name} (${ws.environment}): job ${jobId}`);
|
|
63
|
+
if (!v.wait) {
|
|
64
|
+
ctx.out(`follow with: aihub fabric status --item ${item.displayName} --job ${jobId}`);
|
|
65
|
+
return;
|
|
66
|
+
}
|
|
67
|
+
const j = await waitForJob(client, wsId, item.id, jobId, { pollMs: Number(ctx.env.AIHUB_POLL_MS ?? 5000) });
|
|
68
|
+
ctx.out(formatJob(j, item.displayName));
|
|
69
|
+
if (j.status !== "Completed")
|
|
70
|
+
throw new AihubError(`${item.displayName} ended as ${j.status}`);
|
|
71
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { AihubError } from "../lib/errors.js";
|
|
3
|
+
import { reportInit, runInit } from "../lib/init.js";
|
|
4
|
+
export async function run(argv, ctx) {
|
|
5
|
+
let v;
|
|
6
|
+
try {
|
|
7
|
+
v = parseArgs({
|
|
8
|
+
args: argv,
|
|
9
|
+
options: { repo: { type: "string" }, "no-pr": { type: "boolean" }, language: { type: "string" } },
|
|
10
|
+
}).values;
|
|
11
|
+
}
|
|
12
|
+
catch (e) {
|
|
13
|
+
throw new AihubError(e.message);
|
|
14
|
+
}
|
|
15
|
+
const r = await runInit(ctx, { repo: v.repo, noPr: !!v["no-pr"], language: v.language });
|
|
16
|
+
for (const l of reportInit(r))
|
|
17
|
+
ctx.out(l);
|
|
18
|
+
}
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { changedFiles } from "../lib/changes.js";
|
|
3
|
+
import { repoClient } from "../lib/context.js";
|
|
4
|
+
import { AihubError } from "../lib/errors.js";
|
|
5
|
+
import { followBuild, formatNeeded, queueRobot, robotConfig, robotNeeded, summarize } from "../lib/robot.js";
|
|
6
|
+
import { loadTeam } from "../lib/team.js";
|
|
7
|
+
export async function run(argv, ctx) {
|
|
8
|
+
const [sub, ...rest] = argv;
|
|
9
|
+
let v;
|
|
10
|
+
try {
|
|
11
|
+
v = parseArgs({
|
|
12
|
+
args: rest,
|
|
13
|
+
options: {
|
|
14
|
+
branch: { type: "string" },
|
|
15
|
+
target: { type: "string" },
|
|
16
|
+
suite: { type: "string" },
|
|
17
|
+
include: { type: "string" },
|
|
18
|
+
pipeline: { type: "string" },
|
|
19
|
+
"no-wait": { type: "boolean" },
|
|
20
|
+
},
|
|
21
|
+
}).values;
|
|
22
|
+
}
|
|
23
|
+
catch (e) {
|
|
24
|
+
throw new AihubError(e.message);
|
|
25
|
+
}
|
|
26
|
+
if (sub !== "needed" && sub !== "run")
|
|
27
|
+
throw new AihubError("usage: aihub robot needed|run [flags]");
|
|
28
|
+
const { root, client } = repoClient(ctx);
|
|
29
|
+
const team = loadTeam(root);
|
|
30
|
+
const cfg = robotConfig(team);
|
|
31
|
+
if (sub === "needed") {
|
|
32
|
+
const c = changedFiles(root, team, { branch: v.branch, target: v.target });
|
|
33
|
+
ctx.out(formatNeeded(robotNeeded(c.files, cfg), cfg, c.branch));
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
const pipeline = v.pipeline ?? cfg.pipeline;
|
|
37
|
+
if (!pipeline)
|
|
38
|
+
throw new AihubError("no Robot pipeline: set robot.pipeline in team.yaml or pass --pipeline <name>");
|
|
39
|
+
const branch = cfg.integrationBranch;
|
|
40
|
+
if (!branch)
|
|
41
|
+
throw new AihubError("robot.integrationBranch is not set in team.yaml; the Robot pipeline runs on that branch");
|
|
42
|
+
if (v.suite && cfg.suites.length && !cfg.suites.some((s) => s.name === v.suite)) {
|
|
43
|
+
throw new AihubError(`unknown suite '${v.suite}'; team.yaml suites: ${cfg.suites.map((s) => s.name).join(", ")}`);
|
|
44
|
+
}
|
|
45
|
+
const b = await queueRobot(client, { pipeline, branch, suite: v.suite, include: v.include });
|
|
46
|
+
ctx.out(`queued ${pipeline} build ${b.id} on ${branch}${v.suite ? ` (suite ${v.suite})` : ""}${v.include ? ` (include ${v.include})` : ""}`);
|
|
47
|
+
if (v["no-wait"])
|
|
48
|
+
return;
|
|
49
|
+
const done = await followBuild(client, b.id, Number(ctx.env.AIHUB_POLL_MS ?? 15000));
|
|
50
|
+
ctx.out(await summarize(client, done));
|
|
51
|
+
if (done.result !== "succeeded")
|
|
52
|
+
throw new AihubError(`Robot build ${b.id} ended as ${done.result ?? done.status}`);
|
|
53
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { parseArgs } from "node:util";
|
|
2
|
+
import { AihubError } from "../lib/errors.js";
|
|
3
|
+
import { repoRoot } from "../lib/git.js";
|
|
4
|
+
import { installClients, parseClients, uninstallClients } from "../lib/install.js";
|
|
5
|
+
function report(reports, out) {
|
|
6
|
+
for (const r of reports) {
|
|
7
|
+
const count = (a) => r.results.filter((x) => x.action === a).length;
|
|
8
|
+
out(`${r.client}: ${r.dir}`);
|
|
9
|
+
out(` created ${count("created")}, updated ${count("updated")}, unchanged ${count("unchanged")}, removed ${count("removed")}, skipped ${count("skipped")}`);
|
|
10
|
+
for (const s of r.results.filter((x) => x.action === "skipped"))
|
|
11
|
+
out(` skipped ${s.path} (${s.reason})`);
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
export async function run(argv, ctx) {
|
|
15
|
+
const [sub, ...rest] = argv;
|
|
16
|
+
if (sub !== "install" && sub !== "uninstall")
|
|
17
|
+
throw new AihubError("usage: aihub skills install|uninstall [--global] [--client opencode|github-copilot|codex|all]");
|
|
18
|
+
let v;
|
|
19
|
+
try {
|
|
20
|
+
v = parseArgs({ args: rest, options: { global: { type: "boolean" }, client: { type: "string" } } }).values;
|
|
21
|
+
}
|
|
22
|
+
catch (e) {
|
|
23
|
+
throw new AihubError(e.message);
|
|
24
|
+
}
|
|
25
|
+
let clients;
|
|
26
|
+
try {
|
|
27
|
+
clients = parseClients(v.client);
|
|
28
|
+
}
|
|
29
|
+
catch (e) {
|
|
30
|
+
throw new AihubError(e.message);
|
|
31
|
+
}
|
|
32
|
+
const global = !!v.global;
|
|
33
|
+
let root;
|
|
34
|
+
if (!global) {
|
|
35
|
+
try {
|
|
36
|
+
root = repoRoot(ctx.cwd);
|
|
37
|
+
}
|
|
38
|
+
catch {
|
|
39
|
+
throw new AihubError("not inside a git repository: run in a repository for a project install, or add --global");
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
const opts = { global, root };
|
|
43
|
+
report(sub === "install" ? installClients(clients, ctx.env, opts) : uninstallClients(clients, ctx.env, opts), ctx.out);
|
|
44
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { AihubError } from "./errors.js";
|
|
2
|
+
import { git, hasRef, originDefaultBranch } from "./git.js";
|
|
3
|
+
/** Files changed on `branch` (default HEAD) compared with the target branch (merge-base diff). */
|
|
4
|
+
export function changedFiles(cwd, team, opts = {}) {
|
|
5
|
+
const target = opts.target ?? team.repositories?.[0]?.defaultTarget ?? (originDefaultBranch(cwd) || "main");
|
|
6
|
+
const branch = opts.branch ?? "HEAD";
|
|
7
|
+
const tref = hasRef(cwd, `origin/${target}`) ? `origin/${target}` : target;
|
|
8
|
+
if (!hasRef(cwd, tref))
|
|
9
|
+
throw new AihubError(`target branch '${target}' not found; fetch it or pass --target`);
|
|
10
|
+
const bref = branch !== "HEAD" && !hasRef(cwd, branch) && hasRef(cwd, `origin/${branch}`) ? `origin/${branch}` : branch;
|
|
11
|
+
const out = git(cwd, ["diff", "--name-only", `${tref}...${bref}`]);
|
|
12
|
+
const label = branch === "HEAD" ? git(cwd, ["rev-parse", "--abbrev-ref", "HEAD"], { allowFail: true }).trim() || "HEAD" : branch;
|
|
13
|
+
return { files: out.split("\n").map((s) => s.trim()).filter(Boolean), target, branch: label };
|
|
14
|
+
}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
2
|
+
import { join } from "node:path";
|
|
3
|
+
export const CLIENTS = ["opencode", "github-copilot", "codex"];
|
|
4
|
+
const home = (env) => env.HOME || env.USERPROFILE || homedir();
|
|
5
|
+
/** Global (per user) or project (inside `root`) locations of a client. Environment overrides come from `env`. */
|
|
6
|
+
export function clientDirs(client, env, opts) {
|
|
7
|
+
if (opts.global) {
|
|
8
|
+
if (client === "opencode") {
|
|
9
|
+
const base = env.OPENCODE_CONFIG_DIR || join(env.XDG_CONFIG_HOME || join(home(env), ".config"), "opencode");
|
|
10
|
+
return { skills: join(base, "skills"), commands: { dir: join(base, "command"), source: "opencode" } };
|
|
11
|
+
}
|
|
12
|
+
if (client === "codex") {
|
|
13
|
+
const base = env.CODEX_HOME || join(home(env), ".codex");
|
|
14
|
+
return { skills: join(base, "skills"), commands: { dir: join(base, "prompts"), source: "opencode" } };
|
|
15
|
+
}
|
|
16
|
+
return { skills: join(env.COPILOT_HOME || join(home(env), ".copilot"), "skills") };
|
|
17
|
+
}
|
|
18
|
+
const root = opts.root ?? ".";
|
|
19
|
+
if (client === "opencode") {
|
|
20
|
+
return { skills: join(root, ".opencode", "skills"), commands: { dir: join(root, ".opencode", "command"), source: "opencode" } };
|
|
21
|
+
}
|
|
22
|
+
if (client === "github-copilot") {
|
|
23
|
+
return { skills: join(root, ".github", "skills"), commands: { dir: join(root, ".github", "prompts"), source: "copilot" } };
|
|
24
|
+
}
|
|
25
|
+
return { skills: join(root, ".agents", "skills") };
|
|
26
|
+
}
|
|
@@ -0,0 +1,118 @@
|
|
|
1
|
+
import { spawnSync } from "node:child_process";
|
|
2
|
+
import { existsSync, readFileSync } from "node:fs";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { parse } from "yaml";
|
|
5
|
+
import { parseRemote } from "./ado.js";
|
|
6
|
+
import { defaultExec, ADO_RESOURCE } from "./auth.js";
|
|
7
|
+
import { CLIENTS, clientDirs } from "./clients.js";
|
|
8
|
+
import { originDefaultBranch, remoteUrl, repoRoot, showFile } from "./git.js";
|
|
9
|
+
import { packRoot } from "./pack.js";
|
|
10
|
+
import { requiredPaths, validateTeam } from "./schema.js";
|
|
11
|
+
import { defaultTargetFor, loadTeam, readUtf8, TEAM_FILE } from "./team.js";
|
|
12
|
+
import { resolveTemplate } from "./templates.js";
|
|
13
|
+
const ok = (name, detail) => ({ name, status: "OK", detail });
|
|
14
|
+
const fix = (name, detail, how) => ({ name, status: "FIX", detail, fix: how });
|
|
15
|
+
/** Never prompts: a credential helper that wants a terminal fails instead of hanging. */
|
|
16
|
+
const quietExec = (cmd, args, input) => {
|
|
17
|
+
if (cmd !== "git")
|
|
18
|
+
return defaultExec(cmd, args, input);
|
|
19
|
+
const r = spawnSync(cmd, args, {
|
|
20
|
+
encoding: "utf8",
|
|
21
|
+
input,
|
|
22
|
+
timeout: 10000,
|
|
23
|
+
env: { ...process.env, GIT_TERMINAL_PROMPT: "0", GCM_INTERACTIVE: "never" },
|
|
24
|
+
});
|
|
25
|
+
if (r.error || r.status !== 0)
|
|
26
|
+
throw new Error(r.error?.message ?? (r.stderr || "").trim());
|
|
27
|
+
return r.stdout;
|
|
28
|
+
};
|
|
29
|
+
function checkAuth(ctx) {
|
|
30
|
+
const how = "run 'az login', set AIHUB_ADO_PAT, or sign in once with git (git credential manager)";
|
|
31
|
+
const pat = ctx.env.AIHUB_ADO_PAT ? "AIHUB_ADO_PAT" : ctx.env.AZURE_DEVOPS_EXT_PAT ? "AZURE_DEVOPS_EXT_PAT" : "";
|
|
32
|
+
if (pat)
|
|
33
|
+
return ok("auth", `PAT from environment (${pat})`);
|
|
34
|
+
const exec = ctx.exec ?? quietExec;
|
|
35
|
+
try {
|
|
36
|
+
const j = JSON.parse(exec("az", ["account", "get-access-token", "--resource", ADO_RESOURCE, "--output", "json"]));
|
|
37
|
+
if (j.accessToken)
|
|
38
|
+
return ok("auth", "Azure CLI token (az login)");
|
|
39
|
+
}
|
|
40
|
+
catch {
|
|
41
|
+
/* next method */
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
if (/^password=.+$/m.test(exec("git", ["credential", "fill"], "protocol=https\nhost=dev.azure.com\n\n"))) {
|
|
45
|
+
return ok("auth", "git credential");
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
catch {
|
|
49
|
+
/* none */
|
|
50
|
+
}
|
|
51
|
+
return fix("auth", "no way to sign in to Azure DevOps works", how);
|
|
52
|
+
}
|
|
53
|
+
export function runChecks(ctx) {
|
|
54
|
+
const checks = [];
|
|
55
|
+
const major = Number(process.versions.node.split(".")[0]);
|
|
56
|
+
checks.push(major >= 18
|
|
57
|
+
? ok("node", `v${process.versions.node}`)
|
|
58
|
+
: fix("node", `v${process.versions.node} is too old (needs 18 or newer)`, "install Node 18 or newer from nodejs.org"));
|
|
59
|
+
const g = spawnSync("git", ["--version"], { encoding: "utf8" });
|
|
60
|
+
checks.push(g.status === 0 ? ok("git", g.stdout.trim()) : fix("git", "git is not available", "install git and make sure it is on the PATH"));
|
|
61
|
+
let root;
|
|
62
|
+
try {
|
|
63
|
+
root = repoRoot(ctx.cwd);
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
checks.push(fix("repository", "not inside a git repository", "cd into a clone of your team repository"));
|
|
67
|
+
}
|
|
68
|
+
let repoName = "";
|
|
69
|
+
if (root) {
|
|
70
|
+
try {
|
|
71
|
+
const r = parseRemote(remoteUrl(root));
|
|
72
|
+
repoName = r.repo;
|
|
73
|
+
checks.push(ok("remote", `${r.org}/${r.project}/${r.repo}`));
|
|
74
|
+
}
|
|
75
|
+
catch (e) {
|
|
76
|
+
checks.push(fix("remote", e.message, "set origin to the Azure DevOps clone URL: git remote set-url origin <url>"));
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
checks.push(checkAuth(ctx));
|
|
80
|
+
if (root) {
|
|
81
|
+
const teamPath = join(root, TEAM_FILE);
|
|
82
|
+
if (!existsSync(teamPath)) {
|
|
83
|
+
checks.push(fix("team.yaml", `${TEAM_FILE} not found`, "run 'aihub init' (opens a pull request with the team pack)"));
|
|
84
|
+
}
|
|
85
|
+
else {
|
|
86
|
+
try {
|
|
87
|
+
const data = parse(readUtf8(teamPath));
|
|
88
|
+
const schemaFile = join(packRoot(), "skills", "_shared", "references", "team.schema.yaml");
|
|
89
|
+
const problems = validateTeam(data, requiredPaths(readFileSync(schemaFile, "utf8")));
|
|
90
|
+
checks.push(problems.length
|
|
91
|
+
? fix("team.yaml", problems.join("; "), `edit ${TEAM_FILE} (see docs/team-yaml.md) and open a pull request`)
|
|
92
|
+
: ok("team.yaml", `${TEAM_FILE} is valid`));
|
|
93
|
+
}
|
|
94
|
+
catch (e) {
|
|
95
|
+
checks.push(fix("team.yaml", `cannot be read: ${e.message.split("\n")[0]}`, `repair ${TEAM_FILE} (valid YAML) or recreate it with 'aihub init'`));
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
const team = loadTeam(root);
|
|
99
|
+
const target = defaultTargetFor(team, repoName) || originDefaultBranch(root) || "main";
|
|
100
|
+
const tpl = resolveTemplate(target, { readRef: (b, p) => showFile(root, b, p), localRoot: root });
|
|
101
|
+
checks.push(tpl
|
|
102
|
+
? ok("pr-template", `${tpl.path} (${tpl.from === "ref" ? `on origin/${target}` : "in the working tree only"})`)
|
|
103
|
+
: fix("pr-template", "no pull request template found", "run 'aihub init' or add .azuredevops/pull_request_template.md"));
|
|
104
|
+
}
|
|
105
|
+
for (const c of CLIENTS) {
|
|
106
|
+
const places = [
|
|
107
|
+
{ label: "global", dir: clientDirs(c, ctx.env, { global: true }).skills },
|
|
108
|
+
...(root ? [{ label: "project", dir: clientDirs(c, ctx.env, { global: false, root }).skills }] : []),
|
|
109
|
+
].filter((p) => existsSync(join(p.dir, "start-here", "SKILL.md")));
|
|
110
|
+
checks.push(places.length
|
|
111
|
+
? ok(`skills:${c}`, places.map((p) => `${p.label} (${p.dir})`).join(", "))
|
|
112
|
+
: fix(`skills:${c}`, "aihub skills not installed", `aihub skills install --client ${c} --global`));
|
|
113
|
+
}
|
|
114
|
+
return checks;
|
|
115
|
+
}
|
|
116
|
+
export function formatChecks(checks) {
|
|
117
|
+
return checks.map((c) => `${c.status.padEnd(4)} ${c.name}: ${c.detail}${c.fix ? ` -> ${c.fix}` : ""}`);
|
|
118
|
+
}
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { tmpdir } from "node:os";
|
|
3
|
+
import { join } from "node:path";
|
|
4
|
+
import { defaultExec } from "./auth.js";
|
|
5
|
+
import { AihubError } from "./errors.js";
|
|
6
|
+
import { anyGlob } from "./glob.js";
|
|
7
|
+
export const FABRIC_API = "https://api.fabric.microsoft.com/v1";
|
|
8
|
+
export const FABRIC_RESOURCE = "https://api.fabric.microsoft.com";
|
|
9
|
+
export const ENVS = ["dev", "feature", "test", "acc", "prd"];
|
|
10
|
+
/** Environments the CLI may run in without an explicit --allow-env. */
|
|
11
|
+
const SAFE_ENVS = ["dev", "feature"];
|
|
12
|
+
export function fabricWorkspaces(team) {
|
|
13
|
+
const f = team.fabric;
|
|
14
|
+
return (f?.workspaces ?? [])
|
|
15
|
+
.filter((w) => !!w?.name && ENVS.includes(String(w.environment).toLowerCase()))
|
|
16
|
+
.map((w) => ({ ...w, environment: String(w.environment).toLowerCase() }));
|
|
17
|
+
}
|
|
18
|
+
const ITEM_TYPES = ["Notebook", "DataPipeline", "Lakehouse", "Warehouse", "Environment", "VariableLibrary", "Dataflow", "SemanticModel", "Report"];
|
|
19
|
+
/** Dependencies first, then things that run. */
|
|
20
|
+
const ORDER = ["Environment", "VariableLibrary", "Lakehouse", "Warehouse", "Dataflow", "Notebook", "DataPipeline", "SemanticModel", "Report"];
|
|
21
|
+
export const jobTypeFor = (t) => (t === "Notebook" ? "RunNotebook" : t === "DataPipeline" ? "Pipeline" : undefined);
|
|
22
|
+
/** Map changed paths to Fabric items using the git-integration folder convention `<name>.<Type>/`. */
|
|
23
|
+
export function mapItems(files) {
|
|
24
|
+
const re = new RegExp(`(?:^|/)([^/]+)\\.(${ITEM_TYPES.join("|")})(?:/|$)`);
|
|
25
|
+
const found = new Map();
|
|
26
|
+
for (const f of files) {
|
|
27
|
+
const m = re.exec(f.replace(/\\/g, "/"));
|
|
28
|
+
if (!m)
|
|
29
|
+
continue;
|
|
30
|
+
const type = m[2];
|
|
31
|
+
const key = `${type}:${m[1]}`;
|
|
32
|
+
const cur = found.get(key) ?? { name: m[1], type, jobType: jobTypeFor(type), files: 0 };
|
|
33
|
+
cur.files++;
|
|
34
|
+
found.set(key, cur);
|
|
35
|
+
}
|
|
36
|
+
return [...found.values()].sort((a, b) => ORDER.indexOf(a.type) - ORDER.indexOf(b.type) || a.name.localeCompare(b.name));
|
|
37
|
+
}
|
|
38
|
+
const isGuid = (s) => /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(s);
|
|
39
|
+
/** Workspace for a request: explicit name/id, else the branch rules, else the first feature, else the first dev workspace. */
|
|
40
|
+
export function chooseWorkspace(list, opts) {
|
|
41
|
+
if (opts.workspace) {
|
|
42
|
+
const w = opts.workspace.toLowerCase();
|
|
43
|
+
return list.find((x) => x.name.toLowerCase() === w || x.id?.toLowerCase() === w);
|
|
44
|
+
}
|
|
45
|
+
const byBranch = opts.branch ? list.find((x) => x.branches && anyGlob(x.branches, opts.branch)) : undefined;
|
|
46
|
+
return byBranch ?? list.find((x) => x.environment === "feature") ?? list.find((x) => x.environment === "dev");
|
|
47
|
+
}
|
|
48
|
+
export function buildPlan(files, list, opts) {
|
|
49
|
+
return { workspace: chooseWorkspace(list, opts), items: mapItems(files) };
|
|
50
|
+
}
|
|
51
|
+
export function formatPlan(p, branch, target) {
|
|
52
|
+
const lines = [`Fabric plan for ${branch} (vs ${target}):`];
|
|
53
|
+
lines.push(p.workspace
|
|
54
|
+
? `workspace: ${p.workspace.name} (${p.workspace.environment})`
|
|
55
|
+
: "workspace: none found in team.yaml fabric.workspaces; pass --workspace <name|id>");
|
|
56
|
+
if (!p.items.length) {
|
|
57
|
+
lines.push("No Fabric items changed: nothing to run in Fabric.");
|
|
58
|
+
return lines.join("\n");
|
|
59
|
+
}
|
|
60
|
+
let n = 0;
|
|
61
|
+
for (const it of p.items) {
|
|
62
|
+
const how = it.jobType ? `run: aihub fabric run --item ${it.name} --wait` : "deploy only (nothing to run)";
|
|
63
|
+
lines.push(`${++n}. ${it.type} ${it.name} (${it.files} file${it.files === 1 ? "" : "s"}): ${how}`);
|
|
64
|
+
}
|
|
65
|
+
return lines.join("\n");
|
|
66
|
+
}
|
|
67
|
+
// ---- guard -----------------------------------------------------------------
|
|
68
|
+
export function guardEnv(w, allowEnv) {
|
|
69
|
+
if (SAFE_ENVS.includes(w.environment))
|
|
70
|
+
return;
|
|
71
|
+
if (allowEnv && allowEnv.toLowerCase() === w.environment)
|
|
72
|
+
return;
|
|
73
|
+
const E = w.environment.toUpperCase();
|
|
74
|
+
throw new AihubError(`workspace '${w.name}' is ${E}: the developer runs ${E}, the CLI only runs dev/feature (--allow-env ${w.environment} only when the developer asked for it)`);
|
|
75
|
+
}
|
|
76
|
+
export function makeFabricAuth(deps) {
|
|
77
|
+
const exec = deps.exec ?? defaultExec;
|
|
78
|
+
const now = deps.now ?? Date.now;
|
|
79
|
+
const cacheFile = deps.cacheFile ?? join(tmpdir(), "aihub-fabric-token.json");
|
|
80
|
+
let cached;
|
|
81
|
+
return async () => {
|
|
82
|
+
if (cached)
|
|
83
|
+
return cached;
|
|
84
|
+
if (deps.env.AIHUB_FABRIC_TOKEN)
|
|
85
|
+
return (cached = `Bearer ${deps.env.AIHUB_FABRIC_TOKEN}`);
|
|
86
|
+
try {
|
|
87
|
+
if (existsSync(cacheFile)) {
|
|
88
|
+
const c = JSON.parse(readFileSync(cacheFile, "utf8"));
|
|
89
|
+
if (c.token && c.expiresAt - 120_000 > now())
|
|
90
|
+
return (cached = `Bearer ${c.token}`);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
catch {
|
|
94
|
+
/* ignore a broken cache */
|
|
95
|
+
}
|
|
96
|
+
try {
|
|
97
|
+
const j = JSON.parse(exec("az", ["account", "get-access-token", "--resource", FABRIC_RESOURCE, "--output", "json"]));
|
|
98
|
+
const expiresAt = j.expires_on ? Number(j.expires_on) * 1000 : Date.parse(j.expiresOn ?? "") || now() + 30 * 60_000;
|
|
99
|
+
try {
|
|
100
|
+
writeFileSync(cacheFile, JSON.stringify({ token: j.accessToken, expiresAt }), { mode: 0o600 });
|
|
101
|
+
}
|
|
102
|
+
catch {
|
|
103
|
+
/* cache is optional */
|
|
104
|
+
}
|
|
105
|
+
return (cached = `Bearer ${j.accessToken}`);
|
|
106
|
+
}
|
|
107
|
+
catch {
|
|
108
|
+
throw new AihubError("not signed in to Fabric: run 'az login' or set AIHUB_FABRIC_TOKEN");
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
}
|
|
112
|
+
export class FabricClient {
|
|
113
|
+
auth;
|
|
114
|
+
fetchFn;
|
|
115
|
+
constructor(auth, fetchFn) {
|
|
116
|
+
this.auth = auth;
|
|
117
|
+
this.fetchFn = fetchFn;
|
|
118
|
+
}
|
|
119
|
+
async request(method, path, body) {
|
|
120
|
+
const res = await this.fetchFn(`${FABRIC_API}${path}`, {
|
|
121
|
+
method,
|
|
122
|
+
headers: {
|
|
123
|
+
Authorization: await this.auth(),
|
|
124
|
+
Accept: "application/json",
|
|
125
|
+
...(body !== undefined ? { "Content-Type": "application/json" } : {}),
|
|
126
|
+
},
|
|
127
|
+
body: body !== undefined ? JSON.stringify(body) : undefined,
|
|
128
|
+
});
|
|
129
|
+
const text = await res.text();
|
|
130
|
+
if (res.status === 401)
|
|
131
|
+
throw new AihubError("not signed in to Fabric: run 'az login' or set AIHUB_FABRIC_TOKEN");
|
|
132
|
+
if (!res.ok) {
|
|
133
|
+
let detail = "";
|
|
134
|
+
try {
|
|
135
|
+
const j = JSON.parse(text);
|
|
136
|
+
detail = j.message ?? j.errorCode ?? "";
|
|
137
|
+
}
|
|
138
|
+
catch {
|
|
139
|
+
detail = text.slice(0, 200);
|
|
140
|
+
}
|
|
141
|
+
if (res.status === 403)
|
|
142
|
+
throw new AihubError(`Fabric denied access (403): your account needs access to this workspace${detail ? ` (${detail})` : ""}`);
|
|
143
|
+
throw new AihubError(`Fabric ${method} ${path.split("?")[0]} failed (${res.status})${detail ? `: ${detail}` : ""}`);
|
|
144
|
+
}
|
|
145
|
+
let data = {};
|
|
146
|
+
try {
|
|
147
|
+
data = text ? JSON.parse(text) : {};
|
|
148
|
+
}
|
|
149
|
+
catch {
|
|
150
|
+
/* non-JSON body */
|
|
151
|
+
}
|
|
152
|
+
return { status: res.status, data, headers: res.headers };
|
|
153
|
+
}
|
|
154
|
+
async workspaceId(w) {
|
|
155
|
+
if (w.id)
|
|
156
|
+
return w.id;
|
|
157
|
+
if (isGuid(w.name))
|
|
158
|
+
return w.name;
|
|
159
|
+
const r = await this.request("GET", "/workspaces");
|
|
160
|
+
const hit = r.data.value?.find((x) => x.displayName.toLowerCase() === w.name.toLowerCase());
|
|
161
|
+
if (!hit)
|
|
162
|
+
throw new AihubError(`Fabric workspace '${w.name}' not found for your account`);
|
|
163
|
+
return hit.id;
|
|
164
|
+
}
|
|
165
|
+
async resolveItem(wsId, item) {
|
|
166
|
+
const r = await this.request("GET", `/workspaces/${wsId}/items`);
|
|
167
|
+
const items = (r.data.value ?? []);
|
|
168
|
+
const hits = items.filter((i) => i.id.toLowerCase() === item.toLowerCase() || i.displayName.toLowerCase() === item.toLowerCase());
|
|
169
|
+
if (!hits.length)
|
|
170
|
+
throw new AihubError(`item '${item}' not found in this workspace`);
|
|
171
|
+
if (hits.length > 1)
|
|
172
|
+
throw new AihubError(`item name '${item}' is ambiguous (${hits.map((h) => h.type).join(", ")}); pass the item id`);
|
|
173
|
+
return hits[0];
|
|
174
|
+
}
|
|
175
|
+
async startJob(wsId, item, executionData) {
|
|
176
|
+
const jobType = jobTypeFor(item.type);
|
|
177
|
+
if (!jobType)
|
|
178
|
+
throw new AihubError(`${item.type} '${item.displayName}' cannot run on demand (only Notebook and DataPipeline)`);
|
|
179
|
+
const r = await this.request("POST", `/workspaces/${wsId}/items/${item.id}/jobs/instances?jobType=${jobType}`, executionData ? { executionData } : undefined);
|
|
180
|
+
const jobId = /instances\/([^/?]+)/.exec(r.headers.get("location") ?? "")?.[1];
|
|
181
|
+
if (!jobId)
|
|
182
|
+
throw new AihubError("Fabric accepted the job but returned no job id");
|
|
183
|
+
return jobId;
|
|
184
|
+
}
|
|
185
|
+
async jobStatus(wsId, itemId, jobId) {
|
|
186
|
+
const r = await this.request("GET", `/workspaces/${wsId}/items/${itemId}/jobs/instances/${jobId}`);
|
|
187
|
+
return r.data;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
export const isDone = (s) => ["Completed", "Failed", "Cancelled", "Deduped"].includes(s);
|
|
191
|
+
export async function waitForJob(c, wsId, itemId, jobId, opts) {
|
|
192
|
+
const deadline = Date.now() + (opts.timeoutMs ?? 6 * 3600_000);
|
|
193
|
+
for (;;) {
|
|
194
|
+
const j = await c.jobStatus(wsId, itemId, jobId);
|
|
195
|
+
if (isDone(j.status))
|
|
196
|
+
return j;
|
|
197
|
+
if (Date.now() > deadline)
|
|
198
|
+
throw new AihubError(`job ${jobId} still ${j.status} after the timeout; check with: aihub fabric status --job ${jobId} --item ${itemId}`);
|
|
199
|
+
await new Promise((r) => setTimeout(r, opts.pollMs));
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
export function formatJob(j, itemName) {
|
|
203
|
+
const lines = [`job ${j.id}: ${j.status} (${itemName})`];
|
|
204
|
+
if (j.startTimeUtc)
|
|
205
|
+
lines.push(`started: ${j.startTimeUtc}${j.endTimeUtc ? `, ended: ${j.endTimeUtc}` : ""}`);
|
|
206
|
+
if (j.status === "Failed") {
|
|
207
|
+
const f = j.failureReason;
|
|
208
|
+
lines.push(`failure: ${f?.errorCode ?? "unknown"}: ${(f?.message ?? "no details from Fabric").replace(/\s+/g, " ").trim()}`);
|
|
209
|
+
}
|
|
210
|
+
return lines.join("\n");
|
|
211
|
+
}
|
|
212
|
+
/** Parameter file: JSON map (`{"limit": 10}`) or `{"parameters": {...}}`, turned into Fabric executionData. */
|
|
213
|
+
export function executionDataFromParams(text, item) {
|
|
214
|
+
let j;
|
|
215
|
+
try {
|
|
216
|
+
j = JSON.parse(text.replace(/^/, ""));
|
|
217
|
+
}
|
|
218
|
+
catch {
|
|
219
|
+
throw new AihubError('--params-file must contain JSON, e.g. {"limit": 10}');
|
|
220
|
+
}
|
|
221
|
+
const map = j && typeof j === "object" && j.parameters && typeof j.parameters === "object" ? j.parameters : j;
|
|
222
|
+
if (item.type === "DataPipeline")
|
|
223
|
+
return { parameters: map };
|
|
224
|
+
const typed = {};
|
|
225
|
+
for (const [k, v] of Object.entries(map)) {
|
|
226
|
+
if (v && typeof v === "object" && "value" in v && "type" in v)
|
|
227
|
+
typed[k] = v;
|
|
228
|
+
else
|
|
229
|
+
typed[k] = { value: v, type: typeof v === "boolean" ? "bool" : typeof v === "number" ? (Number.isInteger(v) ? "int" : "float") : "string" };
|
|
230
|
+
}
|
|
231
|
+
return { parameters: typed };
|
|
232
|
+
}
|
package/dist/lib/glob.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/** Minimal glob: `**` any depth, `*` within a segment, `?` one character. Case-insensitive, `/` separators. */
|
|
2
|
+
export function globToRegExp(glob) {
|
|
3
|
+
let re = "";
|
|
4
|
+
const g = glob.replace(/\\/g, "/");
|
|
5
|
+
for (let i = 0; i < g.length; i++) {
|
|
6
|
+
const c = g[i];
|
|
7
|
+
if (c === "*") {
|
|
8
|
+
if (g[i + 1] === "*") {
|
|
9
|
+
i++;
|
|
10
|
+
if (g[i + 1] === "/") {
|
|
11
|
+
i++;
|
|
12
|
+
re += "(?:.*/)?";
|
|
13
|
+
}
|
|
14
|
+
else
|
|
15
|
+
re += ".*";
|
|
16
|
+
}
|
|
17
|
+
else
|
|
18
|
+
re += "[^/]*";
|
|
19
|
+
}
|
|
20
|
+
else if (c === "?")
|
|
21
|
+
re += "[^/]";
|
|
22
|
+
else
|
|
23
|
+
re += c.replace(/[.+^${}()|[\]]/g, "\\$&");
|
|
24
|
+
}
|
|
25
|
+
return new RegExp(`^${re}$`, "i");
|
|
26
|
+
}
|
|
27
|
+
export const globMatch = (glob, path) => globToRegExp(glob).test(path.replace(/\\/g, "/"));
|
|
28
|
+
export const anyGlob = (globs, path) => globs.some((g) => globMatch(g, path));
|