saarthiclaw 0.1.0 → 0.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +25 -0
- package/README.md +49 -1
- package/dist/chunks/{chunk-GVDXGKLD.js → chunk-4CNBITFA.js} +39 -15
- package/dist/chunks/{chunk-GNAQO3JX.js → chunk-4YYY2YOT.js} +9 -5
- package/dist/chunks/{chunk-LLHZUSMZ.js → chunk-5WH3FNFK.js} +1 -0
- package/dist/chunks/chunk-E4DRW4AW.js +126 -0
- package/dist/chunks/chunk-EG5A2Q6X.js +121 -0
- package/dist/chunks/{chunk-3UTSY2MH.js → chunk-GSBUJPPF.js} +9 -12
- package/dist/chunks/{chunk-DORYMXNC.js → chunk-OICPXSO2.js} +1 -1
- package/dist/chunks/{chunk-4MPWZPGX.js → chunk-OS5GVLKA.js} +1 -219
- package/dist/chunks/chunk-QFVMJJQY.js +411 -0
- package/dist/chunks/{chunk-HNAZBEDV.js → chunk-QRAFFI5V.js} +6 -4
- package/dist/chunks/chunk-R7ID2QVW.js +39 -0
- package/dist/chunks/chunk-SKYTL7AH.js +83 -0
- package/dist/chunks/{chunk-EWADWZYM.js → chunk-VAP6FVF5.js} +21 -17
- package/dist/chunks/chunk-ZXVP7JFV.js +232 -0
- package/dist/chunks/client-5WRI2PBK.js +18 -0
- package/dist/chunks/context-AP3U34DQ.js +20 -0
- package/dist/chunks/{doctor-333IEMCL.js → doctor-O3R2YTRO.js} +21 -0
- package/dist/chunks/{history-IWDIOWLB.js → history-FANJN33X.js} +4 -3
- package/dist/chunks/{init-Z4YC3G7V.js → init-GCJNYE3Y.js} +17 -50
- package/dist/chunks/orchestrator-4SMA442I.js +24 -0
- package/dist/chunks/orchestrator-CYIGQH74.js +32 -0
- package/dist/chunks/orchestrator-L4TOOHXY.js +23 -0
- package/dist/chunks/{session-UUHCIT66.js → session-FPFJMS7Y.js} +2 -2
- package/dist/chunks/store-KW257WIV.js +40 -0
- package/dist/chunks/sync-S2A65FIE.js +21 -0
- package/dist/chunks/team-OFNBRXVP.js +378 -0
- package/dist/chunks/{telegram-X36RUESQ.js → telegram-LPSIQBQX.js} +24 -11
- package/dist/chunks/{wakeup-I7FQO5BS.js → wakeup-M5EIP54K.js} +57 -16
- package/dist/index.js +62 -13
- package/package.json +4 -2
- package/dist/chunks/orchestrator-QCIY6FLH.js +0 -17
- package/dist/chunks/orchestrator-SDAA5TOH.js +0 -18
- package/dist/chunks/orchestrator-WXGJJEJW.js +0 -26
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
|
|
2
|
+
// config/gitignore.ts
|
|
3
|
+
import fs from "node:fs";
|
|
4
|
+
import path from "node:path";
|
|
5
|
+
function canonical(line) {
|
|
6
|
+
return line.trim().replace(/^\//, "").replace(/\/$/, "");
|
|
7
|
+
}
|
|
8
|
+
function ensureGitignoreEntries(source, entries) {
|
|
9
|
+
const existing = new Set(
|
|
10
|
+
source.replace(/\r\n?/g, "\n").split("\n").map(canonical).filter((l) => l && !l.startsWith("#"))
|
|
11
|
+
);
|
|
12
|
+
const missing = [];
|
|
13
|
+
for (const entry of entries) {
|
|
14
|
+
const c = canonical(entry);
|
|
15
|
+
if (!existing.has(c)) {
|
|
16
|
+
existing.add(c);
|
|
17
|
+
missing.push(entry);
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
if (missing.length === 0) return source;
|
|
21
|
+
const eol = source.includes("\r\n") ? "\r\n" : "\n";
|
|
22
|
+
let out = source;
|
|
23
|
+
if (out.length && !out.endsWith("\n")) out += eol;
|
|
24
|
+
if (out.length) out += eol;
|
|
25
|
+
out += ["# SaarthiClaw (secrets + local state)", ...missing].join(eol) + eol;
|
|
26
|
+
return out;
|
|
27
|
+
}
|
|
28
|
+
function ensureGitignore(root, entries) {
|
|
29
|
+
const file = path.join(root, ".gitignore");
|
|
30
|
+
const current = fs.existsSync(file) ? fs.readFileSync(file, "utf8") : "";
|
|
31
|
+
const next = ensureGitignoreEntries(current, entries);
|
|
32
|
+
if (next === current) return false;
|
|
33
|
+
fs.writeFileSync(file, next, "utf8");
|
|
34
|
+
return true;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
export {
|
|
38
|
+
ensureGitignore
|
|
39
|
+
};
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
import {
|
|
2
|
+
readKey,
|
|
3
|
+
readTeamLink
|
|
4
|
+
} from "./chunk-E4DRW4AW.js";
|
|
5
|
+
import {
|
|
6
|
+
LEVEL_LABEL,
|
|
7
|
+
TeamClient,
|
|
8
|
+
TeamNetworkError
|
|
9
|
+
} from "./chunk-EG5A2Q6X.js";
|
|
10
|
+
import {
|
|
11
|
+
UserFacingError
|
|
12
|
+
} from "./chunk-G6GCAEKE.js";
|
|
13
|
+
|
|
14
|
+
// team/context.ts
|
|
15
|
+
import chalk from "chalk";
|
|
16
|
+
var cache = /* @__PURE__ */ new WeakMap();
|
|
17
|
+
function envOf(session) {
|
|
18
|
+
return session.env ?? process.env;
|
|
19
|
+
}
|
|
20
|
+
function teamClientFor(session) {
|
|
21
|
+
const env = envOf(session);
|
|
22
|
+
const link = readTeamLink(session.root, env);
|
|
23
|
+
if (!link) return void 0;
|
|
24
|
+
const key = readKey(link.server, session.root, env);
|
|
25
|
+
if (!key) {
|
|
26
|
+
throw new UserFacingError(
|
|
27
|
+
`This folder is linked to the team dashboard at ${link.server}, but no access key is saved for this folder yet.`,
|
|
28
|
+
`Run \`npx saarthiclaw team connect ${link.server}\` here and paste YOUR key (a copied project folder needs its own connect), or \`npx saarthiclaw team disconnect\` to work solo.`
|
|
29
|
+
);
|
|
30
|
+
}
|
|
31
|
+
return { client: new TeamClient(link.server, key, session.teamFetch), link };
|
|
32
|
+
}
|
|
33
|
+
async function resolve(session) {
|
|
34
|
+
const found = teamClientFor(session);
|
|
35
|
+
if (!found) return { mode: "solo" };
|
|
36
|
+
const { client, link } = found;
|
|
37
|
+
try {
|
|
38
|
+
const me = await client.whoami();
|
|
39
|
+
return { mode: me.permissionLevel, client, me, link };
|
|
40
|
+
} catch (e) {
|
|
41
|
+
if (e instanceof TeamNetworkError && link.role === "admin") {
|
|
42
|
+
console.log(chalk.yellow(`⚠ Team dashboard unreachable (${link.server}). Working as Admin; changes won't be logged there this time.`));
|
|
43
|
+
return { mode: "admin", client, link, offline: true };
|
|
44
|
+
}
|
|
45
|
+
throw e;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
function getTeamContext(session, opts = {}) {
|
|
49
|
+
if (opts.refresh || !cache.has(session)) {
|
|
50
|
+
const p = resolve(session);
|
|
51
|
+
cache.set(session, p);
|
|
52
|
+
p.catch(() => cache.delete(session));
|
|
53
|
+
}
|
|
54
|
+
return cache.get(session);
|
|
55
|
+
}
|
|
56
|
+
function describeTeamMode(ctx) {
|
|
57
|
+
if (ctx.mode === "solo") return void 0;
|
|
58
|
+
const who = ctx.me ? `${ctx.me.name} · ${LEVEL_LABEL[ctx.mode]}` : LEVEL_LABEL[ctx.mode];
|
|
59
|
+
const project = ctx.me?.project?.name ? ` on ${ctx.me.project.name}` : "";
|
|
60
|
+
const how = {
|
|
61
|
+
admin: "you approve changes here; they are logged on the dashboard.",
|
|
62
|
+
trusted: "you approve your own changes here; they are logged on the dashboard.",
|
|
63
|
+
needs_approval: "your changes are sent to the Admin; nothing is written until they approve.",
|
|
64
|
+
suggest_only: "you can ask and plan, and send suggestions; code changes are not allowed."
|
|
65
|
+
};
|
|
66
|
+
return `👥 Team${project}: ${who} — ${how[ctx.mode]}`;
|
|
67
|
+
}
|
|
68
|
+
function assertProjectOpen(ctx) {
|
|
69
|
+
const status = ctx.me?.project?.status;
|
|
70
|
+
if (ctx.mode !== "solo" && ctx.mode !== "admin" && status && status !== "active") {
|
|
71
|
+
throw new UserFacingError(
|
|
72
|
+
`The Admin has marked this project as ${status}; changes are not accepted right now.`,
|
|
73
|
+
"Ask the Admin to set it back to Active on the dashboard."
|
|
74
|
+
);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
export {
|
|
79
|
+
teamClientFor,
|
|
80
|
+
getTeamContext,
|
|
81
|
+
describeTeamMode,
|
|
82
|
+
assertProjectOpen
|
|
83
|
+
};
|
|
@@ -1,22 +1,27 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createWebTools
|
|
3
|
-
} from "./chunk-
|
|
3
|
+
} from "./chunk-OICPXSO2.js";
|
|
4
4
|
import {
|
|
5
5
|
REPEAT_NOTE,
|
|
6
|
-
ToolExecutor,
|
|
7
|
-
clipOutput,
|
|
8
6
|
createAgentTools,
|
|
9
7
|
createReadOnlyTools,
|
|
10
8
|
dedupeReadOnlyTools,
|
|
9
|
+
finalizeStaged,
|
|
11
10
|
finishOnLastStep,
|
|
12
11
|
generateWithProgress,
|
|
13
|
-
renderTerminalMarkdown
|
|
14
|
-
|
|
15
|
-
|
|
12
|
+
renderTerminalMarkdown
|
|
13
|
+
} from "./chunk-QFVMJJQY.js";
|
|
14
|
+
import {
|
|
15
|
+
ToolExecutor,
|
|
16
|
+
clipOutput
|
|
17
|
+
} from "./chunk-OS5GVLKA.js";
|
|
16
18
|
import {
|
|
17
|
-
Activity
|
|
18
|
-
|
|
19
|
-
|
|
19
|
+
Activity
|
|
20
|
+
} from "./chunk-QRAFFI5V.js";
|
|
21
|
+
import {
|
|
22
|
+
describeTeamMode,
|
|
23
|
+
getTeamContext
|
|
24
|
+
} from "./chunk-SKYTL7AH.js";
|
|
20
25
|
import {
|
|
21
26
|
RunCancelledError,
|
|
22
27
|
runCancellable
|
|
@@ -26,7 +31,7 @@ import {
|
|
|
26
31
|
createTracker,
|
|
27
32
|
instructionsFor,
|
|
28
33
|
readOnlyAgentConfig
|
|
29
|
-
} from "./chunk-
|
|
34
|
+
} from "./chunk-4YYY2YOT.js";
|
|
30
35
|
import {
|
|
31
36
|
AI_MAX_RETRIES
|
|
32
37
|
} from "./chunk-BTFIQWAE.js";
|
|
@@ -233,6 +238,8 @@ function stepPrompt(plan, step, index, total) {
|
|
|
233
238
|
}
|
|
234
239
|
async function runPlanMode(session, presetGoal) {
|
|
235
240
|
console.log(chalk3.bold("\n✍️ Plan Mode\n"));
|
|
241
|
+
const banner = describeTeamMode(await getTeamContext(session));
|
|
242
|
+
if (banner) console.log(chalk3.cyan(banner) + "\n");
|
|
236
243
|
const goal = presetGoal?.trim() || (await session.prompter.text({
|
|
237
244
|
message: "What is your goal?",
|
|
238
245
|
placeholder: "e.g. add input validation to the signup route",
|
|
@@ -290,13 +297,10 @@ async function runPlanMode(session, presetGoal) {
|
|
|
290
297
|
if (text.trim()) console.log(renderTerminalMarkdown(text));
|
|
291
298
|
}
|
|
292
299
|
}
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
}
|
|
297
|
-
const ok = await runApprovalFlow(tracker, session.prompter, { root: session.root });
|
|
298
|
-
if (!ok) return;
|
|
299
|
-
reportApply(executor.applyApprovedFromTracker());
|
|
300
|
+
await finalizeStaged(session, tracker, executor, {
|
|
301
|
+
task: `Plan: ${goal}`,
|
|
302
|
+
summary: plan.steps.map((s, i) => `${i + 1}. ${s.title}`).join("\n")
|
|
303
|
+
});
|
|
300
304
|
} catch (e) {
|
|
301
305
|
const discarded = tracker.discardPending(e instanceof RunCancelledError ? "cancelled" : "aborted");
|
|
302
306
|
if (discarded > 0) {
|
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
import {
|
|
2
|
+
clipOutput
|
|
3
|
+
} from "./chunk-OS5GVLKA.js";
|
|
4
|
+
import {
|
|
5
|
+
colorizePatch,
|
|
6
|
+
riskBadge
|
|
7
|
+
} from "./chunk-QRAFFI5V.js";
|
|
8
|
+
import {
|
|
9
|
+
getTeamContext
|
|
10
|
+
} from "./chunk-SKYTL7AH.js";
|
|
11
|
+
import {
|
|
12
|
+
readSyncState,
|
|
13
|
+
sha256Hex,
|
|
14
|
+
writeSyncState
|
|
15
|
+
} from "./chunk-E4DRW4AW.js";
|
|
16
|
+
import {
|
|
17
|
+
RunCancelledError,
|
|
18
|
+
runCancellable
|
|
19
|
+
} from "./chunk-HGBZ55ND.js";
|
|
20
|
+
import {
|
|
21
|
+
createAuditLogger
|
|
22
|
+
} from "./chunk-5WH3FNFK.js";
|
|
23
|
+
import {
|
|
24
|
+
UserFacingError
|
|
25
|
+
} from "./chunk-G6GCAEKE.js";
|
|
26
|
+
import {
|
|
27
|
+
NonInteractiveError
|
|
28
|
+
} from "./chunk-IT46XDPQ.js";
|
|
29
|
+
import {
|
|
30
|
+
isSecretPath,
|
|
31
|
+
relativePosix,
|
|
32
|
+
resolveInsideRoot
|
|
33
|
+
} from "./chunk-XTVJWMUH.js";
|
|
34
|
+
|
|
35
|
+
// team/sync.ts
|
|
36
|
+
import { spawnSync } from "node:child_process";
|
|
37
|
+
import fs from "node:fs";
|
|
38
|
+
import path from "node:path";
|
|
39
|
+
import { setTimeout as sleep } from "node:timers/promises";
|
|
40
|
+
import chalk from "chalk";
|
|
41
|
+
var SHELL_TIMEOUT_MS = 5 * 60 * 1e3;
|
|
42
|
+
function label(a) {
|
|
43
|
+
if (a.type === "shell_command") return `Shell: ${a.command ?? ""}`;
|
|
44
|
+
const verb = { file_create: "create", file_modify: "modify", file_delete: "delete", folder_create: "folder" };
|
|
45
|
+
return `${a.path} (${verb[a.type] ?? a.type})`;
|
|
46
|
+
}
|
|
47
|
+
function currentHash(abs) {
|
|
48
|
+
try {
|
|
49
|
+
const st = fs.statSync(abs);
|
|
50
|
+
if (st.isDirectory()) return "directory";
|
|
51
|
+
return sha256Hex(fs.readFileSync(abs));
|
|
52
|
+
} catch {
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
var CONFLICT = "changed on this computer after the change was proposed, so it was NOT applied. Ask the agent again with the latest code.";
|
|
57
|
+
function applyRemoteAction(root, a, opts) {
|
|
58
|
+
if (a.type === "shell_command") {
|
|
59
|
+
const cmd = a.command ?? "";
|
|
60
|
+
if (!cmd) return { ok: false, message: "No command received." };
|
|
61
|
+
if (!opts.runShell) return { ok: false, skipped: true, message: "Not run yet." };
|
|
62
|
+
const r = spawnSync(cmd, { shell: true, cwd: root, encoding: "utf8", maxBuffer: 16 * 1024 * 1024, timeout: SHELL_TIMEOUT_MS, windowsHide: true });
|
|
63
|
+
const out = clipOutput(`${r.stdout ?? ""}${r.stderr ?? ""}`.trim(), 400);
|
|
64
|
+
if (!r.error && r.status === 0) return { ok: true, message: out ? `Ran OK: ${out}` : "Ran OK" };
|
|
65
|
+
const why = r.error ? r.error.message : r.signal ? `killed by ${r.signal}` : `exit code ${r.status}`;
|
|
66
|
+
return { ok: false, message: `Command failed (${why})${out ? `: ${out}` : ""}` };
|
|
67
|
+
}
|
|
68
|
+
let abs;
|
|
69
|
+
try {
|
|
70
|
+
abs = resolveInsideRoot(root, a.path ?? "");
|
|
71
|
+
} catch (e) {
|
|
72
|
+
return { ok: false, message: e.message };
|
|
73
|
+
}
|
|
74
|
+
const rel = relativePosix(root, abs);
|
|
75
|
+
if (rel === "." || isSecretPath(rel)) return { ok: false, message: `Refused: ${rel} is protected.` };
|
|
76
|
+
if (a.type === "folder_create") {
|
|
77
|
+
if (currentHash(abs) !== null && currentHash(abs) !== "directory") return { ok: false, message: `${rel} exists and is a file.` };
|
|
78
|
+
fs.mkdirSync(abs, { recursive: true });
|
|
79
|
+
return { ok: true, message: "Folder created" };
|
|
80
|
+
}
|
|
81
|
+
const now = currentHash(abs);
|
|
82
|
+
if (now === "directory") return { ok: false, message: `${rel} is a folder, not a file.` };
|
|
83
|
+
if (a.type === "file_delete") {
|
|
84
|
+
if (now === null) return { ok: true, message: "Already deleted" };
|
|
85
|
+
if (now !== a.baseHash) return { ok: false, message: `${rel} ${CONFLICT}` };
|
|
86
|
+
fs.rmSync(abs, { force: true });
|
|
87
|
+
return { ok: true, message: "Deleted" };
|
|
88
|
+
}
|
|
89
|
+
if (typeof a.after !== "string") return { ok: false, message: "The approved content was not received." };
|
|
90
|
+
if (now !== null && now === sha256Hex(a.after)) return { ok: true, message: "Already up to date" };
|
|
91
|
+
if (now !== (a.baseHash ?? null)) return { ok: false, message: `${rel} ${CONFLICT}` };
|
|
92
|
+
fs.mkdirSync(path.dirname(abs), { recursive: true });
|
|
93
|
+
fs.writeFileSync(abs, a.after, "utf8");
|
|
94
|
+
return { ok: true, message: a.type === "file_create" ? "Created" : "Updated" };
|
|
95
|
+
}
|
|
96
|
+
function auditEvent(root, a, outcome) {
|
|
97
|
+
const log = createAuditLogger(root);
|
|
98
|
+
const action = {
|
|
99
|
+
id: `team_${a.id}`,
|
|
100
|
+
timestamp: /* @__PURE__ */ new Date(),
|
|
101
|
+
type: a.type,
|
|
102
|
+
path: a.path ?? "shell",
|
|
103
|
+
details: { command: a.command ?? void 0 },
|
|
104
|
+
status: outcome.ok ? "applied" : "failed"
|
|
105
|
+
};
|
|
106
|
+
log({ kind: outcome.ok ? "applied" : "failed", action, mode: "team-sync", reason: outcome.ok ? void 0 : outcome.message });
|
|
107
|
+
}
|
|
108
|
+
var ORDER = { folder_create: 0, file_create: 1, file_modify: 1, file_delete: 2, shell_command: 3 };
|
|
109
|
+
async function applyApproved(session, client, approved, opts, summary) {
|
|
110
|
+
console.log(chalk.bold(`
|
|
111
|
+
✅ Approved by the Admin (${approved.length})
|
|
112
|
+
`));
|
|
113
|
+
for (const a of approved) {
|
|
114
|
+
console.log(`${chalk.bold(label(a))} ${riskBadge(a.risk)} ${chalk.dim(a.decidedBy ? `approved by ${a.decidedBy}` : "")}`);
|
|
115
|
+
if (a.type === "shell_command") console.log(" " + chalk.inverse(` ${a.command ?? ""} `));
|
|
116
|
+
else if (a.diff) console.log(colorizePatch(a.diff, 80));
|
|
117
|
+
console.log("");
|
|
118
|
+
}
|
|
119
|
+
if (!opts.yes) {
|
|
120
|
+
if (!session.prompter.interactive) throw new NonInteractiveError("`saarthiclaw team sync` without --yes");
|
|
121
|
+
const go = await session.prompter.confirm({
|
|
122
|
+
message: `Apply ${approved.length === 1 ? "this approved change" : `these ${approved.length} approved changes`} to your files now?`,
|
|
123
|
+
initialValue: true
|
|
124
|
+
});
|
|
125
|
+
if (!go) {
|
|
126
|
+
summary.skipped += approved.length;
|
|
127
|
+
console.log(chalk.yellow("Not applied. They stay approved — run `saarthiclaw team sync` again when you are ready.\n"));
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const sorted = [...approved].sort((x, y) => (ORDER[x.type] ?? 9) - (ORDER[y.type] ?? 9) || x.id - y.id);
|
|
132
|
+
for (const a of sorted) {
|
|
133
|
+
let runShell = Boolean(opts.allowShell || session.config.allowShellExecution);
|
|
134
|
+
if (a.type === "shell_command" && !runShell && session.prompter.interactive && !opts.yes) {
|
|
135
|
+
runShell = await session.prompter.confirm({ message: `Run the approved command now? ${a.command}`, initialValue: false });
|
|
136
|
+
}
|
|
137
|
+
const outcome = applyRemoteAction(session.root, a, { runShell });
|
|
138
|
+
if (outcome.skipped) {
|
|
139
|
+
summary.skipped++;
|
|
140
|
+
console.log(` ${chalk.dim("… later ")} ${label(a)} ${chalk.dim("— still approved; run `team sync --allow-shell` to run it")}`);
|
|
141
|
+
continue;
|
|
142
|
+
}
|
|
143
|
+
auditEvent(session.root, a, outcome);
|
|
144
|
+
try {
|
|
145
|
+
await client.reportResult(a.id, outcome.ok, outcome.message);
|
|
146
|
+
} catch (e) {
|
|
147
|
+
console.log(chalk.yellow(` ⚠ Could not report the result of #${a.id} to the dashboard: ${e.message}`));
|
|
148
|
+
}
|
|
149
|
+
if (outcome.ok) {
|
|
150
|
+
summary.applied++;
|
|
151
|
+
console.log(` ${chalk.green("✓")} ${label(a)} ${chalk.dim(`— ${outcome.message}`)}`);
|
|
152
|
+
} else {
|
|
153
|
+
summary.failed++;
|
|
154
|
+
console.log(` ${chalk.red("✗")} ${label(a)} ${chalk.red(`— ${outcome.message}`)}`);
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
console.log("");
|
|
158
|
+
}
|
|
159
|
+
async function runTeamSync(session, opts = {}) {
|
|
160
|
+
const summary = { applied: 0, failed: 0, rejected: 0, pending: 0, skipped: 0 };
|
|
161
|
+
const team = await getTeamContext(session, { refresh: true });
|
|
162
|
+
if (team.mode === "solo" || !team.client) {
|
|
163
|
+
throw new UserFacingError("This project is not connected to a team dashboard.", "Run `saarthiclaw team connect <dashboard-url>` first.");
|
|
164
|
+
}
|
|
165
|
+
if (team.mode === "admin") {
|
|
166
|
+
const me = team.me;
|
|
167
|
+
console.log(chalk.bold("\n🔄 Team sync\n"));
|
|
168
|
+
console.log("As the Admin you approve your own changes in the terminal, so there is nothing to apply here.");
|
|
169
|
+
if (me?.pendingApprovals) console.log(chalk.yellow(`• ${me.pendingApprovals} change${me.pendingApprovals === 1 ? "" : "s"} from your team are waiting for you on the dashboard → Approvals.`));
|
|
170
|
+
if (me?.inbox) console.log(chalk.cyan(`• ${me.inbox} forwarded suggestion${me.inbox === 1 ? "" : "s"} ready for the agent → run \`saarthiclaw team inbox\`.`));
|
|
171
|
+
console.log("");
|
|
172
|
+
return summary;
|
|
173
|
+
}
|
|
174
|
+
const client = team.client;
|
|
175
|
+
const state = readSyncState(session.root);
|
|
176
|
+
const seen = new Set(state.seen);
|
|
177
|
+
const pollMs = opts.pollMs ?? session.teamPollMs ?? 3e3;
|
|
178
|
+
const deadline = Date.now() + (opts.maxWaitMs ?? 30 * 60 * 1e3);
|
|
179
|
+
let waitingShown = false;
|
|
180
|
+
console.log(chalk.bold("\n🔄 Team sync") + chalk.dim(` ${team.me?.name ?? ""} · ${team.link?.server ?? ""}
|
|
181
|
+
`));
|
|
182
|
+
try {
|
|
183
|
+
while (true) {
|
|
184
|
+
const actions = await client.myActions();
|
|
185
|
+
for (const a of actions) {
|
|
186
|
+
if (seen.has(a.id)) continue;
|
|
187
|
+
if (a.status === "rejected") {
|
|
188
|
+
seen.add(a.id);
|
|
189
|
+
summary.rejected++;
|
|
190
|
+
console.log(`${chalk.yellow("✗ Rejected")} ${label(a)}${a.decidedBy ? chalk.dim(` by ${a.decidedBy}`) : ""}${a.rejectReason ? ` — ${chalk.italic(a.rejectReason)}` : ""}`);
|
|
191
|
+
} else if (a.status === "executed" || a.status === "failed") {
|
|
192
|
+
seen.add(a.id);
|
|
193
|
+
}
|
|
194
|
+
}
|
|
195
|
+
const approved = actions.filter((a) => a.status === "approved");
|
|
196
|
+
if (approved.length) await applyApproved(session, client, approved, opts, summary);
|
|
197
|
+
const pending = actions.filter((a) => a.status === "pending");
|
|
198
|
+
summary.pending = pending.length;
|
|
199
|
+
state.seen = [...seen];
|
|
200
|
+
state.lastSyncAt = (/* @__PURE__ */ new Date()).toISOString();
|
|
201
|
+
writeSyncState(session.root, state);
|
|
202
|
+
if (!opts.wait || pending.length === 0 || summary.skipped > 0) break;
|
|
203
|
+
if (Date.now() > deadline) {
|
|
204
|
+
console.log(chalk.yellow("Stopped waiting after 30 minutes."));
|
|
205
|
+
break;
|
|
206
|
+
}
|
|
207
|
+
if (!waitingShown) {
|
|
208
|
+
waitingShown = true;
|
|
209
|
+
console.log(chalk.cyan(`⏳ Waiting for the Admin to review ${pending.length} change${pending.length === 1 ? "" : "s"}… `) + chalk.dim("(Ctrl+C stops waiting; nothing is lost)"));
|
|
210
|
+
}
|
|
211
|
+
await runCancellable((signal) => sleep(pollMs, void 0, { signal }));
|
|
212
|
+
}
|
|
213
|
+
} catch (e) {
|
|
214
|
+
if (!(e instanceof RunCancelledError)) throw e;
|
|
215
|
+
console.log(chalk.yellow("\nStopped waiting. Your changes are still with the Admin — run `saarthiclaw team sync` later."));
|
|
216
|
+
}
|
|
217
|
+
const parts = [];
|
|
218
|
+
if (summary.applied) parts.push(chalk.green(`${summary.applied} applied`));
|
|
219
|
+
if (summary.failed) parts.push(chalk.red(`${summary.failed} failed`));
|
|
220
|
+
if (summary.rejected) parts.push(chalk.yellow(`${summary.rejected} rejected`));
|
|
221
|
+
if (summary.skipped) parts.push(chalk.dim(`${summary.skipped} left for later`));
|
|
222
|
+
if (summary.pending) parts.push(chalk.cyan(`${summary.pending} still waiting for the Admin`));
|
|
223
|
+
console.log(parts.length ? `
|
|
224
|
+
${parts.join(" · ")}
|
|
225
|
+
` : chalk.dim("\nAll caught up — nothing new from the Admin.\n"));
|
|
226
|
+
return summary;
|
|
227
|
+
}
|
|
228
|
+
|
|
229
|
+
export {
|
|
230
|
+
applyRemoteAction,
|
|
231
|
+
runTeamSync
|
|
232
|
+
};
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import {
|
|
2
|
+
LEVEL_LABEL,
|
|
3
|
+
TeamApiError,
|
|
4
|
+
TeamClient,
|
|
5
|
+
TeamNetworkError
|
|
6
|
+
} from "./chunk-EG5A2Q6X.js";
|
|
7
|
+
import "./chunk-G6GCAEKE.js";
|
|
8
|
+
import "./chunk-IT46XDPQ.js";
|
|
9
|
+
import "./chunk-XTVJWMUH.js";
|
|
10
|
+
import "./chunk-BTFIQWAE.js";
|
|
11
|
+
import "./chunk-2GDAWRT7.js";
|
|
12
|
+
import "./chunk-MMIJKDOU.js";
|
|
13
|
+
export {
|
|
14
|
+
LEVEL_LABEL,
|
|
15
|
+
TeamApiError,
|
|
16
|
+
TeamClient,
|
|
17
|
+
TeamNetworkError
|
|
18
|
+
};
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import {
|
|
2
|
+
assertProjectOpen,
|
|
3
|
+
describeTeamMode,
|
|
4
|
+
getTeamContext,
|
|
5
|
+
teamClientFor
|
|
6
|
+
} from "./chunk-SKYTL7AH.js";
|
|
7
|
+
import "./chunk-E4DRW4AW.js";
|
|
8
|
+
import "./chunk-EG5A2Q6X.js";
|
|
9
|
+
import "./chunk-G6GCAEKE.js";
|
|
10
|
+
import "./chunk-IT46XDPQ.js";
|
|
11
|
+
import "./chunk-XTVJWMUH.js";
|
|
12
|
+
import "./chunk-BTFIQWAE.js";
|
|
13
|
+
import "./chunk-2GDAWRT7.js";
|
|
14
|
+
import "./chunk-MMIJKDOU.js";
|
|
15
|
+
export {
|
|
16
|
+
assertProjectOpen,
|
|
17
|
+
describeTeamMode,
|
|
18
|
+
getTeamContext,
|
|
19
|
+
teamClientFor
|
|
20
|
+
};
|
|
@@ -117,10 +117,30 @@ function printChecks(checks) {
|
|
|
117
117
|
const width = Math.max(...checks.map((c) => c.name.length));
|
|
118
118
|
for (const c of checks) console.log(` ${ICON[c.status]} ${c.name.padEnd(width)} ${c.detail}`);
|
|
119
119
|
}
|
|
120
|
+
async function checkTeam(session, live) {
|
|
121
|
+
const { readTeamLink, readKey } = await import("./store-KW257WIV.js");
|
|
122
|
+
const env = session.env ?? process.env;
|
|
123
|
+
const link = readTeamLink(session.root, env);
|
|
124
|
+
if (!link) return { name: "Team dashboard", status: "info", detail: "solo mode (not connected)", required: false };
|
|
125
|
+
if (!readKey(link.server, session.root, env)) return { name: "Team dashboard", status: "warn", detail: `no key saved for ${link.server} — run \`saarthiclaw team connect\``, required: false };
|
|
126
|
+
if (!live) return { name: "Team dashboard", status: "info", detail: `${link.server} (not contacted: --offline)`, required: false };
|
|
127
|
+
try {
|
|
128
|
+
const { TeamClient } = await import("./client-5WRI2PBK.js");
|
|
129
|
+
const me = await new TeamClient(link.server, readKey(link.server, session.root, env), session.teamFetch, 8e3).whoami();
|
|
130
|
+
return { name: "Team dashboard", status: "pass", detail: `${me.name} (${me.permissionLevel.replace("_", " ")}) at ${link.server}`, required: false };
|
|
131
|
+
} catch (e) {
|
|
132
|
+
return { name: "Team dashboard", status: "warn", detail: e.message, required: false };
|
|
133
|
+
}
|
|
134
|
+
}
|
|
120
135
|
async function runDoctor(session, opts = {}) {
|
|
121
136
|
console.log(chalk.bold("\n🩺 SaarthiClaw doctor\n"));
|
|
122
137
|
const checks = staticChecks(session.config);
|
|
123
138
|
printChecks(checks);
|
|
139
|
+
const teamCheck = await checkTeam(session, opts.live !== false);
|
|
140
|
+
if (teamCheck) {
|
|
141
|
+
printChecks([teamCheck]);
|
|
142
|
+
checks.push(teamCheck);
|
|
143
|
+
}
|
|
124
144
|
if (opts.live !== false) {
|
|
125
145
|
process.stdout.write(chalk.dim("\n Testing a live request (1 short call)…\n"));
|
|
126
146
|
const live = await liveCheck(session);
|
|
@@ -141,6 +161,7 @@ async function runDoctor(session, opts = {}) {
|
|
|
141
161
|
return 0;
|
|
142
162
|
}
|
|
143
163
|
export {
|
|
164
|
+
checkTeam,
|
|
144
165
|
checkWritableStateDir,
|
|
145
166
|
liveCheck,
|
|
146
167
|
printChecks,
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import {
|
|
2
2
|
renderTable,
|
|
3
3
|
riskBadge
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-QRAFFI5V.js";
|
|
5
5
|
import {
|
|
6
6
|
auditLogPath,
|
|
7
7
|
readAuditLog
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-5WH3FNFK.js";
|
|
9
9
|
import "./chunk-MMIJKDOU.js";
|
|
10
10
|
|
|
11
11
|
// cli/history.ts
|
|
@@ -15,7 +15,8 @@ var EVENT_COLOR = {
|
|
|
15
15
|
approved: chalk.green,
|
|
16
16
|
rejected: chalk.yellow,
|
|
17
17
|
applied: chalk.green.bold,
|
|
18
|
-
failed: chalk.red
|
|
18
|
+
failed: chalk.red,
|
|
19
|
+
submitted: chalk.magenta
|
|
19
20
|
};
|
|
20
21
|
function fmtTime(iso) {
|
|
21
22
|
const d = new Date(iso);
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
maskSecret
|
|
3
3
|
} from "./chunk-2OOG5CBM.js";
|
|
4
|
+
import {
|
|
5
|
+
ensureGitignore
|
|
6
|
+
} from "./chunk-R7ID2QVW.js";
|
|
4
7
|
import {
|
|
5
8
|
DEFAULT_MODEL,
|
|
6
9
|
MODEL_PRESETS,
|
|
@@ -12,47 +15,10 @@ import {
|
|
|
12
15
|
import "./chunk-MMIJKDOU.js";
|
|
13
16
|
|
|
14
17
|
// cli/init.ts
|
|
15
|
-
import fs2 from "node:fs";
|
|
16
|
-
import path2 from "node:path";
|
|
17
|
-
import chalk from "chalk";
|
|
18
|
-
import * as clack from "@clack/prompts";
|
|
19
|
-
|
|
20
|
-
// config/gitignore.ts
|
|
21
18
|
import fs from "node:fs";
|
|
22
19
|
import path from "node:path";
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
}
|
|
26
|
-
function ensureGitignoreEntries(source, entries) {
|
|
27
|
-
const existing = new Set(
|
|
28
|
-
source.replace(/\r\n?/g, "\n").split("\n").map(canonical).filter((l) => l && !l.startsWith("#"))
|
|
29
|
-
);
|
|
30
|
-
const missing = [];
|
|
31
|
-
for (const entry of entries) {
|
|
32
|
-
const c = canonical(entry);
|
|
33
|
-
if (!existing.has(c)) {
|
|
34
|
-
existing.add(c);
|
|
35
|
-
missing.push(entry);
|
|
36
|
-
}
|
|
37
|
-
}
|
|
38
|
-
if (missing.length === 0) return source;
|
|
39
|
-
const eol = source.includes("\r\n") ? "\r\n" : "\n";
|
|
40
|
-
let out = source;
|
|
41
|
-
if (out.length && !out.endsWith("\n")) out += eol;
|
|
42
|
-
if (out.length) out += eol;
|
|
43
|
-
out += ["# SaarthiClaw (secrets + local state)", ...missing].join(eol) + eol;
|
|
44
|
-
return out;
|
|
45
|
-
}
|
|
46
|
-
function ensureGitignore(root, entries) {
|
|
47
|
-
const file = path.join(root, ".gitignore");
|
|
48
|
-
const current = fs.existsSync(file) ? fs.readFileSync(file, "utf8") : "";
|
|
49
|
-
const next = ensureGitignoreEntries(current, entries);
|
|
50
|
-
if (next === current) return false;
|
|
51
|
-
fs.writeFileSync(file, next, "utf8");
|
|
52
|
-
return true;
|
|
53
|
-
}
|
|
54
|
-
|
|
55
|
-
// cli/init.ts
|
|
20
|
+
import chalk from "chalk";
|
|
21
|
+
import * as clack from "@clack/prompts";
|
|
56
22
|
var GITIGNORE_ENTRIES = [".env", ".saarthiclaw/"];
|
|
57
23
|
var SCRIPT_NAME = "saarthiclaw";
|
|
58
24
|
var SCRIPT_VALUE = "saarthiclaw wakeup";
|
|
@@ -82,22 +48,22 @@ function detectIndent(json) {
|
|
|
82
48
|
return m ? m[0] : 2;
|
|
83
49
|
}
|
|
84
50
|
function addPackageScript(root) {
|
|
85
|
-
const file =
|
|
86
|
-
if (!
|
|
87
|
-
const raw =
|
|
51
|
+
const file = path.join(root, "package.json");
|
|
52
|
+
if (!fs.existsSync(file)) return false;
|
|
53
|
+
const raw = fs.readFileSync(file, "utf8");
|
|
88
54
|
const pkg = JSON.parse(raw);
|
|
89
55
|
if (pkg.scripts?.[SCRIPT_NAME]) return false;
|
|
90
56
|
pkg.scripts = { ...pkg.scripts ?? {}, [SCRIPT_NAME]: SCRIPT_VALUE };
|
|
91
57
|
const eol = raw.includes("\r\n") ? "\r\n" : "\n";
|
|
92
58
|
const out = JSON.stringify(pkg, null, detectIndent(raw)).replace(/\n/g, eol) + (raw.endsWith("\n") ? eol : "");
|
|
93
|
-
|
|
59
|
+
fs.writeFileSync(file, out, "utf8");
|
|
94
60
|
return true;
|
|
95
61
|
}
|
|
96
62
|
function hasPackageScript(root) {
|
|
97
|
-
const file =
|
|
98
|
-
if (!
|
|
63
|
+
const file = path.join(root, "package.json");
|
|
64
|
+
if (!fs.existsSync(file)) return void 0;
|
|
99
65
|
try {
|
|
100
|
-
const pkg = JSON.parse(
|
|
66
|
+
const pkg = JSON.parse(fs.readFileSync(file, "utf8"));
|
|
101
67
|
return Boolean(pkg.scripts?.[SCRIPT_NAME]);
|
|
102
68
|
} catch {
|
|
103
69
|
return void 0;
|
|
@@ -111,7 +77,7 @@ async function runInit(opts) {
|
|
|
111
77
|
const { root, prompter } = opts;
|
|
112
78
|
const decorate = opts.decorate ?? false;
|
|
113
79
|
const current = loadConfig({ root, env: opts.env ?? process.env, userDir: opts.userDir });
|
|
114
|
-
const envFile =
|
|
80
|
+
const envFile = path.join(root, ".env");
|
|
115
81
|
const cfgFile = projectConfigPath(root);
|
|
116
82
|
if (decorate) clack.intro(chalk.bold.cyan(" SaarthiClaw setup "));
|
|
117
83
|
console.log(
|
|
@@ -119,8 +85,8 @@ async function runInit(opts) {
|
|
|
119
85
|
`Project: ${chalk.bold(root)}`,
|
|
120
86
|
"",
|
|
121
87
|
"This will create or update:",
|
|
122
|
-
` • ${chalk.cyan(".env")} secrets (API keys, tokens) ${
|
|
123
|
-
` • ${chalk.cyan(".saarthiclaw/config.json")} non-secret settings ${
|
|
88
|
+
` • ${chalk.cyan(".env")} secrets (API keys, tokens) ${fs.existsSync(envFile) ? chalk.dim("(exists)") : ""}`,
|
|
89
|
+
` • ${chalk.cyan(".saarthiclaw/config.json")} non-secret settings ${fs.existsSync(cfgFile) ? chalk.dim("(exists)") : ""}`,
|
|
124
90
|
` • ${chalk.cyan(".gitignore")} adds .env and .saarthiclaw/ if missing`,
|
|
125
91
|
"",
|
|
126
92
|
chalk.dim("Re-running is safe: current values are shown (secrets masked) and you only change what you want."),
|
|
@@ -214,13 +180,14 @@ async function runInit(opts) {
|
|
|
214
180
|
if (decorate) clack.outro(chalk.yellow("Nothing was written."));
|
|
215
181
|
return { wroteEnvKeys: [], configFile: cfgFile, gitignoreChanged: false, scriptAdded: false, cancelled: true };
|
|
216
182
|
}
|
|
217
|
-
if (changedKeys.length || !
|
|
183
|
+
if (changedKeys.length || !fs.existsSync(envFile)) writeDotenvFile(envFile, envUpdates);
|
|
218
184
|
const configFile = writeProjectConfig(root, { model, allowShellExecution: allowShell });
|
|
219
185
|
const gitignoreChanged = ensureGitignore(root, GITIGNORE_ENTRIES);
|
|
220
186
|
if (wantsScript) scriptAdded = addPackageScript(root);
|
|
221
187
|
const runHint = scriptAdded || scriptState === true ? "npm run saarthiclaw" : "npx saarthiclaw wakeup";
|
|
222
188
|
if (decorate) clack.outro(`${chalk.green("All set!")} Start with ${chalk.bold(runHint)} — or check things with ${chalk.bold("npx saarthiclaw doctor")}.`);
|
|
223
189
|
else console.log(chalk.green(`All set! Start with ${runHint}`));
|
|
190
|
+
console.log(chalk.dim(`Working with a team? Link this project to your dashboard: ${chalk.bold("npx saarthiclaw team connect <dashboard-url>")}`));
|
|
224
191
|
return { wroteEnvKeys: changedKeys, configFile, gitignoreChanged, scriptAdded, cancelled: false };
|
|
225
192
|
}
|
|
226
193
|
export {
|