saarthiclaw 0.1.0 → 0.2.2
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 +30 -0
- package/README.md +49 -1
- package/dist/chunks/chunk-3UFD5346.js +126 -0
- package/dist/chunks/{chunk-LLHZUSMZ.js → chunk-5WH3FNFK.js} +1 -0
- package/dist/chunks/chunk-DSQDVSZS.js +411 -0
- package/dist/chunks/chunk-E4DRW4AW.js +126 -0
- package/dist/chunks/chunk-EG5A2Q6X.js +121 -0
- package/dist/chunks/{chunk-GNAQO3JX.js → chunk-GX2I7K4S.js} +19 -7
- 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-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-3UTSY2MH.js → chunk-TTT3KK46.js} +9 -12
- package/dist/chunks/{chunk-EWADWZYM.js → chunk-VDYO3QC7.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-JFJU4FGP.js +24 -0
- package/dist/chunks/orchestrator-KNCW4K57.js +32 -0
- package/dist/chunks/orchestrator-NIDJK4C3.js +25 -0
- package/dist/chunks/{session-UUHCIT66.js → session-CRFC3IEU.js} +2 -2
- package/dist/chunks/store-KW257WIV.js +40 -0
- package/dist/chunks/sync-S2A65FIE.js +21 -0
- package/dist/chunks/team-RBSOK3HT.js +378 -0
- package/dist/chunks/{telegram-X36RUESQ.js → telegram-JSGRYPIX.js} +24 -11
- package/dist/chunks/{wakeup-I7FQO5BS.js → wakeup-4IP4JXVF.js} +57 -16
- package/dist/index.js +62 -13
- package/package.json +4 -2
- package/dist/chunks/chunk-GVDXGKLD.js +0 -82
- 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,126 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UserFacingError
|
|
3
|
+
} from "./chunk-G6GCAEKE.js";
|
|
4
|
+
import {
|
|
5
|
+
userConfigDir
|
|
6
|
+
} from "./chunk-2GDAWRT7.js";
|
|
7
|
+
import {
|
|
8
|
+
projectStateDir
|
|
9
|
+
} from "./chunk-MMIJKDOU.js";
|
|
10
|
+
|
|
11
|
+
// team/store.ts
|
|
12
|
+
import crypto from "node:crypto";
|
|
13
|
+
import fs from "node:fs";
|
|
14
|
+
import path from "node:path";
|
|
15
|
+
var KEY_PATTERN = /^sc_[am]_[a-f0-9]{8}_[a-f0-9]{32}$/;
|
|
16
|
+
function sha256Hex(data) {
|
|
17
|
+
return crypto.createHash("sha256").update(data).digest("hex");
|
|
18
|
+
}
|
|
19
|
+
function teamFile(root) {
|
|
20
|
+
return path.join(projectStateDir(root), "team.json");
|
|
21
|
+
}
|
|
22
|
+
function syncStateFile(root) {
|
|
23
|
+
return path.join(projectStateDir(root), "team-sync.json");
|
|
24
|
+
}
|
|
25
|
+
function credentialsFile(env = process.env) {
|
|
26
|
+
return path.join(userConfigDir(env), "credentials.json");
|
|
27
|
+
}
|
|
28
|
+
function normalizeServer(input) {
|
|
29
|
+
const raw = input.trim();
|
|
30
|
+
if (/^[a-z][a-z0-9+.-]*:\/\//i.test(raw) && !/^https?:\/\//i.test(raw)) {
|
|
31
|
+
throw new UserFacingError("The dashboard address must start with http:// or https://");
|
|
32
|
+
}
|
|
33
|
+
let url;
|
|
34
|
+
try {
|
|
35
|
+
url = new URL(/^https?:\/\//i.test(raw) ? raw : `http://${raw}`);
|
|
36
|
+
} catch {
|
|
37
|
+
throw new UserFacingError(`"${input}" is not a valid dashboard address.`, "Example: http://127.0.0.1:3000 or https://team.example.com");
|
|
38
|
+
}
|
|
39
|
+
if (url.protocol !== "http:" && url.protocol !== "https:") {
|
|
40
|
+
throw new UserFacingError("The dashboard address must start with http:// or https://");
|
|
41
|
+
}
|
|
42
|
+
return `${url.protocol}//${url.host}`;
|
|
43
|
+
}
|
|
44
|
+
function readJson(file) {
|
|
45
|
+
try {
|
|
46
|
+
return JSON.parse(fs.readFileSync(file, "utf8"));
|
|
47
|
+
} catch {
|
|
48
|
+
return void 0;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
function writeJson(file, data, mode) {
|
|
52
|
+
fs.mkdirSync(path.dirname(file), { recursive: true });
|
|
53
|
+
fs.writeFileSync(file, JSON.stringify(data, null, 2) + "\n", { encoding: "utf8", mode });
|
|
54
|
+
if (mode !== void 0) {
|
|
55
|
+
try {
|
|
56
|
+
fs.chmodSync(file, mode);
|
|
57
|
+
} catch {
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
function readTeamLink(root, env = process.env) {
|
|
62
|
+
const link = readJson(teamFile(root));
|
|
63
|
+
const override = env.SAARTHICLAW_TEAM_SERVER?.trim();
|
|
64
|
+
if (override) return { ...link ?? { connectedAt: (/* @__PURE__ */ new Date()).toISOString() }, server: normalizeServer(override) };
|
|
65
|
+
if (!link || typeof link.server !== "string" || !link.server) return void 0;
|
|
66
|
+
return link;
|
|
67
|
+
}
|
|
68
|
+
function writeTeamLink(root, link) {
|
|
69
|
+
writeJson(teamFile(root), link);
|
|
70
|
+
}
|
|
71
|
+
function removeTeamLink(root) {
|
|
72
|
+
fs.rmSync(teamFile(root), { force: true });
|
|
73
|
+
fs.rmSync(syncStateFile(root), { force: true });
|
|
74
|
+
}
|
|
75
|
+
function credentialId(server, root) {
|
|
76
|
+
const abs = path.resolve(root);
|
|
77
|
+
return `${server} ${process.platform === "win32" ? abs.toLowerCase() : abs}`;
|
|
78
|
+
}
|
|
79
|
+
function readKey(server, root, env = process.env) {
|
|
80
|
+
const fromEnv = env.SAARTHICLAW_TEAM_KEY?.trim();
|
|
81
|
+
if (fromEnv) return fromEnv;
|
|
82
|
+
const creds = readJson(credentialsFile(env)) ?? {};
|
|
83
|
+
const k = creds[credentialId(server, root)]?.key;
|
|
84
|
+
return typeof k === "string" && k ? k : void 0;
|
|
85
|
+
}
|
|
86
|
+
function saveKey(server, root, key, env = process.env) {
|
|
87
|
+
const file = credentialsFile(env);
|
|
88
|
+
const creds = readJson(file) ?? {};
|
|
89
|
+
creds[credentialId(server, root)] = { key, savedAt: (/* @__PURE__ */ new Date()).toISOString() };
|
|
90
|
+
writeJson(file, creds, 384);
|
|
91
|
+
return file;
|
|
92
|
+
}
|
|
93
|
+
function removeKey(server, root, env = process.env) {
|
|
94
|
+
const file = credentialsFile(env);
|
|
95
|
+
const creds = readJson(file);
|
|
96
|
+
if (!creds) return;
|
|
97
|
+
const id = credentialId(server, root);
|
|
98
|
+
if (!(id in creds)) return;
|
|
99
|
+
delete creds[id];
|
|
100
|
+
writeJson(file, creds, 384);
|
|
101
|
+
}
|
|
102
|
+
function readSyncState(root) {
|
|
103
|
+
const s = readJson(syncStateFile(root));
|
|
104
|
+
return { seen: Array.isArray(s?.seen) ? s.seen.filter((n) => Number.isInteger(n)).slice(-2e3) : [], lastSyncAt: s?.lastSyncAt };
|
|
105
|
+
}
|
|
106
|
+
function writeSyncState(root, state) {
|
|
107
|
+
writeJson(syncStateFile(root), { seen: state.seen.slice(-2e3), lastSyncAt: state.lastSyncAt });
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
export {
|
|
111
|
+
KEY_PATTERN,
|
|
112
|
+
sha256Hex,
|
|
113
|
+
teamFile,
|
|
114
|
+
syncStateFile,
|
|
115
|
+
credentialsFile,
|
|
116
|
+
normalizeServer,
|
|
117
|
+
readTeamLink,
|
|
118
|
+
writeTeamLink,
|
|
119
|
+
removeTeamLink,
|
|
120
|
+
credentialId,
|
|
121
|
+
readKey,
|
|
122
|
+
saveKey,
|
|
123
|
+
removeKey,
|
|
124
|
+
readSyncState,
|
|
125
|
+
writeSyncState
|
|
126
|
+
};
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
import {
|
|
2
|
+
UserFacingError
|
|
3
|
+
} from "./chunk-G6GCAEKE.js";
|
|
4
|
+
|
|
5
|
+
// team/client.ts
|
|
6
|
+
var LEVEL_LABEL = {
|
|
7
|
+
admin: "Admin",
|
|
8
|
+
trusted: "Trusted",
|
|
9
|
+
needs_approval: "Needs approval",
|
|
10
|
+
suggest_only: "Suggest only"
|
|
11
|
+
};
|
|
12
|
+
var TeamApiError = class extends UserFacingError {
|
|
13
|
+
constructor(message, status, code, hint) {
|
|
14
|
+
super(message, hint);
|
|
15
|
+
this.status = status;
|
|
16
|
+
this.code = code;
|
|
17
|
+
this.name = "TeamApiError";
|
|
18
|
+
}
|
|
19
|
+
status;
|
|
20
|
+
code;
|
|
21
|
+
};
|
|
22
|
+
var TeamNetworkError = class extends UserFacingError {
|
|
23
|
+
constructor(server, detail) {
|
|
24
|
+
super(
|
|
25
|
+
`Can't reach the team dashboard at ${server}.`,
|
|
26
|
+
`${detail}
|
|
27
|
+
Check that the dashboard is running (\`npm start\` in saarthiclaw-web) and that this computer can reach that address.`
|
|
28
|
+
);
|
|
29
|
+
this.name = "TeamNetworkError";
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
var HINTS = {
|
|
33
|
+
INVALID_KEY: "Ask the Admin for a new key, then run `saarthiclaw team connect` again.",
|
|
34
|
+
SUGGEST_ONLY: 'Use `saarthiclaw team suggest "your idea"` — the Admin decides whether to pass it to the agent.',
|
|
35
|
+
PROJECT_LOCKED: "Ask the Admin to set the project back to Active on the dashboard.",
|
|
36
|
+
NO_PROJECT: "The Admin links the project by running `saarthiclaw team connect` inside the project folder.",
|
|
37
|
+
LOCKED_OUT: "Too many wrong keys were tried from this network. Wait, then check the key."
|
|
38
|
+
};
|
|
39
|
+
var TeamClient = class {
|
|
40
|
+
constructor(server, key, fetchImpl = (input, init) => fetch(input, init), timeoutMs = 2e4) {
|
|
41
|
+
this.server = server;
|
|
42
|
+
this.key = key;
|
|
43
|
+
this.fetchImpl = fetchImpl;
|
|
44
|
+
this.timeoutMs = timeoutMs;
|
|
45
|
+
}
|
|
46
|
+
server;
|
|
47
|
+
key;
|
|
48
|
+
fetchImpl;
|
|
49
|
+
timeoutMs;
|
|
50
|
+
async call(method, pathname, body) {
|
|
51
|
+
let response;
|
|
52
|
+
try {
|
|
53
|
+
response = await this.fetchImpl(`${this.server}/api/cli${pathname}`, {
|
|
54
|
+
method,
|
|
55
|
+
headers: {
|
|
56
|
+
Authorization: `Bearer ${this.key}`,
|
|
57
|
+
Accept: "application/json",
|
|
58
|
+
...body === void 0 ? {} : { "Content-Type": "application/json" }
|
|
59
|
+
},
|
|
60
|
+
body: body === void 0 ? void 0 : JSON.stringify(body),
|
|
61
|
+
signal: AbortSignal.timeout(this.timeoutMs)
|
|
62
|
+
});
|
|
63
|
+
} catch (e) {
|
|
64
|
+
const err = e;
|
|
65
|
+
const detail = err.name === "TimeoutError" ? `No answer within ${Math.round(this.timeoutMs / 1e3)} seconds.` : err.cause?.code ? `${err.cause.code}${err.cause.message ? `: ${err.cause.message}` : ""}` : err.message ?? String(e);
|
|
66
|
+
throw new TeamNetworkError(this.server, detail);
|
|
67
|
+
}
|
|
68
|
+
let payload;
|
|
69
|
+
try {
|
|
70
|
+
payload = await response.json();
|
|
71
|
+
} catch {
|
|
72
|
+
payload = void 0;
|
|
73
|
+
}
|
|
74
|
+
if (!response.ok || !payload?.ok) {
|
|
75
|
+
const code = payload?.error?.code ?? `HTTP_${response.status}`;
|
|
76
|
+
let message = payload?.error?.message ?? `The dashboard answered with HTTP ${response.status}.`;
|
|
77
|
+
const fields = payload?.error?.fields;
|
|
78
|
+
if (fields) message += " " + Object.entries(fields).map(([k, v]) => `${k}: ${v}`).join("; ");
|
|
79
|
+
if (!payload && response.status === 404) message = "This address does not look like a SaarthiClaw dashboard (no /api/cli).";
|
|
80
|
+
throw new TeamApiError(message, response.status, code, HINTS[code]);
|
|
81
|
+
}
|
|
82
|
+
return payload.data;
|
|
83
|
+
}
|
|
84
|
+
whoami() {
|
|
85
|
+
return this.call("GET", "/whoami");
|
|
86
|
+
}
|
|
87
|
+
pushProject(snapshot) {
|
|
88
|
+
return this.call("POST", "/project", snapshot);
|
|
89
|
+
}
|
|
90
|
+
propose(body) {
|
|
91
|
+
return this.call("POST", "/proposals", body);
|
|
92
|
+
}
|
|
93
|
+
recordApplied(body) {
|
|
94
|
+
return this.call("POST", "/applied", body);
|
|
95
|
+
}
|
|
96
|
+
myActions() {
|
|
97
|
+
return this.call("GET", "/actions");
|
|
98
|
+
}
|
|
99
|
+
reportResult(id, ok, message) {
|
|
100
|
+
return this.call("POST", `/actions/${id}/result`, { ok, message: message.slice(0, 500) });
|
|
101
|
+
}
|
|
102
|
+
suggest(message) {
|
|
103
|
+
return this.call("POST", "/suggestions", { message });
|
|
104
|
+
}
|
|
105
|
+
inbox() {
|
|
106
|
+
return this.call("GET", "/inbox");
|
|
107
|
+
}
|
|
108
|
+
inboxDone(id) {
|
|
109
|
+
return this.call("POST", `/inbox/${id}/done`);
|
|
110
|
+
}
|
|
111
|
+
invite(name, permissionLevel, regenerate = false) {
|
|
112
|
+
return this.call("POST", "/members", { name, permissionLevel, ...regenerate ? { regenerate: true } : {} });
|
|
113
|
+
}
|
|
114
|
+
};
|
|
115
|
+
|
|
116
|
+
export {
|
|
117
|
+
LEVEL_LABEL,
|
|
118
|
+
TeamApiError,
|
|
119
|
+
TeamNetworkError,
|
|
120
|
+
TeamClient
|
|
121
|
+
};
|
|
@@ -1,3 +1,6 @@
|
|
|
1
|
+
import {
|
|
2
|
+
attachAuditLog
|
|
3
|
+
} from "./chunk-5WH3FNFK.js";
|
|
1
4
|
import {
|
|
2
5
|
isSecretPath
|
|
3
6
|
} from "./chunk-XTVJWMUH.js";
|
|
@@ -5,9 +8,6 @@ import {
|
|
|
5
8
|
getAgentModel,
|
|
6
9
|
mapAiError
|
|
7
10
|
} from "./chunk-BTFIQWAE.js";
|
|
8
|
-
import {
|
|
9
|
-
attachAuditLog
|
|
10
|
-
} from "./chunk-LLHZUSMZ.js";
|
|
11
11
|
|
|
12
12
|
// modes/session.ts
|
|
13
13
|
import chalk from "chalk";
|
|
@@ -175,7 +175,12 @@ function buildProjectBrief(root, budgets = {}) {
|
|
|
175
175
|
}
|
|
176
176
|
|
|
177
177
|
// context/instructions.ts
|
|
178
|
-
var PERSONA = "You are SaarthiClaw, a careful coding assistant that lives in the developer's terminal. You behave like a very capable intern: you investigate first,
|
|
178
|
+
var PERSONA = "You are SaarthiClaw, a careful coding assistant that lives in the developer's terminal. You behave like a very capable intern: you investigate first, then do the work. The senior developer (the human) reviews and approves every staged change afterwards.";
|
|
179
|
+
var ACT_RULES = [
|
|
180
|
+
'Do NOT ask for permission or confirmation before making a change, and do not end with a question like "Would you like me to create the file?" — staging IS the safe step; the human approves afterwards.',
|
|
181
|
+
"Never paste the code into your reply instead of staging it. If the task asks for code, a file, a fix or a feature, call create_file / modify_file / create_folder (or delete_file) so the change is staged.",
|
|
182
|
+
"If the request is ambiguous, pick the most reasonable interpretation, stage it, and mention the assumption in your summary."
|
|
183
|
+
];
|
|
179
184
|
var MODE_RULES = {
|
|
180
185
|
ask: [
|
|
181
186
|
"Mode: ASK (read-only). Answer the question about this codebase.",
|
|
@@ -199,18 +204,21 @@ var MODE_RULES = {
|
|
|
199
204
|
],
|
|
200
205
|
"plan-step": [
|
|
201
206
|
"Mode: AGENT executing ONE step of an approved plan.",
|
|
207
|
+
...ACT_RULES,
|
|
202
208
|
"Only do the work for the current step; earlier steps' staged changes are visible to read_file.",
|
|
203
209
|
"Stage changes with the file tools; prefer minimal, focused diffs.",
|
|
204
210
|
"Finish with a short summary of what you staged and why."
|
|
205
211
|
],
|
|
206
212
|
agent: [
|
|
207
213
|
"Mode: AGENT. Complete the user's task by staging changes with the tools.",
|
|
214
|
+
...ACT_RULES,
|
|
208
215
|
"Read the relevant files first, then stage the smallest correct change (minimal diffs, keep the existing style).",
|
|
209
|
-
"
|
|
216
|
+
"Only answer without staging anything when the user clearly asked a question rather than for a change.",
|
|
210
217
|
"Finish with a short summary listing every staged change."
|
|
211
218
|
],
|
|
212
219
|
"telegram-agent": [
|
|
213
220
|
"Mode: AGENT via Telegram. Complete the task by staging changes with the tools.",
|
|
221
|
+
...ACT_RULES,
|
|
214
222
|
"Read relevant files first; stage minimal diffs; finish with a short summary of staged changes."
|
|
215
223
|
]
|
|
216
224
|
};
|
|
@@ -267,7 +275,8 @@ var STATUS_EVENT = {
|
|
|
267
275
|
approved: "approved",
|
|
268
276
|
rejected: "rejected",
|
|
269
277
|
applied: "applied",
|
|
270
|
-
failed: "failed"
|
|
278
|
+
failed: "failed",
|
|
279
|
+
submitted: "submitted"
|
|
271
280
|
};
|
|
272
281
|
var ActionTracker = class {
|
|
273
282
|
constructor(mode = "agent") {
|
|
@@ -343,7 +352,10 @@ function createSession(opts) {
|
|
|
343
352
|
config,
|
|
344
353
|
prompter: opts.prompter,
|
|
345
354
|
debug: Boolean(opts.debug),
|
|
346
|
-
model: opts.model ?? (() => getAgentModel({ apiKey: config.openrouterApiKey, model: config.model }))
|
|
355
|
+
model: opts.model ?? (() => getAgentModel({ apiKey: config.openrouterApiKey, model: config.model })),
|
|
356
|
+
teamFetch: opts.teamFetch,
|
|
357
|
+
env: opts.env,
|
|
358
|
+
teamPollMs: opts.teamPollMs
|
|
347
359
|
};
|
|
348
360
|
}
|
|
349
361
|
function hasWebTools(session) {
|
|
@@ -1,10 +1,3 @@
|
|
|
1
|
-
import {
|
|
2
|
-
Activity
|
|
3
|
-
} from "./chunk-HNAZBEDV.js";
|
|
4
|
-
import {
|
|
5
|
-
RunCancelledError,
|
|
6
|
-
runCancellable
|
|
7
|
-
} from "./chunk-HGBZ55ND.js";
|
|
8
1
|
import {
|
|
9
2
|
PathSafetyError,
|
|
10
3
|
globToRegExp,
|
|
@@ -503,218 +496,7 @@ var ToolExecutor = class {
|
|
|
503
496
|
}
|
|
504
497
|
};
|
|
505
498
|
|
|
506
|
-
// modes/agent/agent-tools.ts
|
|
507
|
-
import { tool } from "ai";
|
|
508
|
-
import { z } from "zod";
|
|
509
|
-
function createReadOnlyTools(executor) {
|
|
510
|
-
return {
|
|
511
|
-
read_file: tool({
|
|
512
|
-
description: "Read a text file from the workspace. Use a path relative to the project root. Secret files (.env, keys) are blocked.",
|
|
513
|
-
inputSchema: z.object({
|
|
514
|
-
path: z.string().describe("Relative file path, e.g. src/index.ts")
|
|
515
|
-
}),
|
|
516
|
-
execute: async ({ path: p }) => executor.readFile(p)
|
|
517
|
-
}),
|
|
518
|
-
list_files: tool({
|
|
519
|
-
description: "List files and directories under a path (relative to the project root).",
|
|
520
|
-
inputSchema: z.object({
|
|
521
|
-
path: z.string().default("."),
|
|
522
|
-
recursive: z.boolean().optional().default(false)
|
|
523
|
-
}),
|
|
524
|
-
execute: async ({ path: p, recursive }) => executor.listFiles(p, recursive ?? false)
|
|
525
|
-
}),
|
|
526
|
-
search_files: tool({
|
|
527
|
-
description: "Find files matching a glob pattern (e.g. '*.ts', 'src/**/*.md'). Optional content substring filter.",
|
|
528
|
-
inputSchema: z.object({
|
|
529
|
-
root: z.string().default(".").describe("Directory to search, relative to the project root"),
|
|
530
|
-
pattern: z.string().describe("Glob-like pattern using * and ** (forward slashes)"),
|
|
531
|
-
content_contains: z.string().optional()
|
|
532
|
-
}),
|
|
533
|
-
execute: async ({ root, pattern, content_contains }) => executor.searchFiles(root, pattern, content_contains)
|
|
534
|
-
}),
|
|
535
|
-
analyze_codebase: tool({
|
|
536
|
-
description: "Summarize structure: file counts, total size and most common extensions. Read-only.",
|
|
537
|
-
inputSchema: z.object({
|
|
538
|
-
path: z.string().default(".")
|
|
539
|
-
}),
|
|
540
|
-
execute: async ({ path: p }) => executor.analyzeCodebase(p)
|
|
541
|
-
}),
|
|
542
|
-
list_skills: tool({
|
|
543
|
-
description: "List absolute paths to SKILL.md files under configured skill directories.",
|
|
544
|
-
inputSchema: z.object({}),
|
|
545
|
-
execute: async () => executor.listSkills()
|
|
546
|
-
}),
|
|
547
|
-
read_skill: tool({
|
|
548
|
-
description: "Read a SKILL.md file returned by list_skills.",
|
|
549
|
-
inputSchema: z.object({
|
|
550
|
-
path: z.string()
|
|
551
|
-
}),
|
|
552
|
-
execute: async ({ path: p }) => executor.readSkill(p)
|
|
553
|
-
})
|
|
554
|
-
};
|
|
555
|
-
}
|
|
556
|
-
function createAgentTools(executor, options = {}) {
|
|
557
|
-
const base = {
|
|
558
|
-
...createReadOnlyTools(executor),
|
|
559
|
-
create_file: tool({
|
|
560
|
-
description: "Stage creation of a new file (not written until the user approves).",
|
|
561
|
-
inputSchema: z.object({
|
|
562
|
-
path: z.string(),
|
|
563
|
-
content: z.string()
|
|
564
|
-
}),
|
|
565
|
-
execute: async ({ path: p, content }) => executor.createFile(p, content)
|
|
566
|
-
}),
|
|
567
|
-
modify_file: tool({
|
|
568
|
-
description: "Stage a full-file replacement for an existing file (pending approval). Provide the complete new contents.",
|
|
569
|
-
inputSchema: z.object({
|
|
570
|
-
path: z.string(),
|
|
571
|
-
content: z.string().describe("Complete new file contents")
|
|
572
|
-
}),
|
|
573
|
-
execute: async ({ path: p, content }) => executor.modifyFile(p, content)
|
|
574
|
-
}),
|
|
575
|
-
delete_file: tool({
|
|
576
|
-
description: "Stage deletion of a file (pending approval).",
|
|
577
|
-
inputSchema: z.object({
|
|
578
|
-
path: z.string()
|
|
579
|
-
}),
|
|
580
|
-
execute: async ({ path: p }) => executor.deleteFile(p)
|
|
581
|
-
}),
|
|
582
|
-
create_folder: tool({
|
|
583
|
-
description: "Stage creation of a directory tree (pending approval).",
|
|
584
|
-
inputSchema: z.object({
|
|
585
|
-
path: z.string().describe("Relative directory path")
|
|
586
|
-
}),
|
|
587
|
-
execute: async ({ path: p }) => executor.createFolder(p)
|
|
588
|
-
})
|
|
589
|
-
};
|
|
590
|
-
if (!options.allowShell) return base;
|
|
591
|
-
return {
|
|
592
|
-
...base,
|
|
593
|
-
execute_shell: tool({
|
|
594
|
-
description: "Queue a shell command to run in the workspace root after the user approves it. Use only when necessary (e.g. installing a dependency, running tests).",
|
|
595
|
-
inputSchema: z.object({
|
|
596
|
-
command: z.string().describe("A single command line")
|
|
597
|
-
}),
|
|
598
|
-
execute: async ({ command }) => executor.queueShell(command)
|
|
599
|
-
})
|
|
600
|
-
};
|
|
601
|
-
}
|
|
602
|
-
|
|
603
|
-
// modes/agent/run.ts
|
|
604
|
-
import chalk from "chalk";
|
|
605
|
-
function preview(input, max = 140) {
|
|
606
|
-
let s;
|
|
607
|
-
try {
|
|
608
|
-
s = JSON.stringify(input) ?? "";
|
|
609
|
-
} catch {
|
|
610
|
-
s = String(input);
|
|
611
|
-
}
|
|
612
|
-
s = s.replace(/\s+/g, " ");
|
|
613
|
-
return s.length > max ? `${s.slice(0, max)}…` : s;
|
|
614
|
-
}
|
|
615
|
-
async function generateWithProgress(agent, prompt, label) {
|
|
616
|
-
const activity = new Activity();
|
|
617
|
-
activity.start(label);
|
|
618
|
-
try {
|
|
619
|
-
const result = await runCancellable(
|
|
620
|
-
(signal) => agent.generate({
|
|
621
|
-
prompt,
|
|
622
|
-
abortSignal: signal,
|
|
623
|
-
onStepFinish: (step) => {
|
|
624
|
-
const parts = step.content ?? [];
|
|
625
|
-
for (const part of parts) {
|
|
626
|
-
if (part.type === "tool-call") {
|
|
627
|
-
activity.log(`${chalk.green("✓")} ${chalk.bold(String(part.toolName))} ${chalk.dim(preview(part.input))}`);
|
|
628
|
-
} else if (part.type === "tool-error") {
|
|
629
|
-
const msg = part.error instanceof Error ? part.error.message : String(part.error);
|
|
630
|
-
activity.log(`${chalk.yellow("✗")} ${chalk.bold(String(part.toolName))} ${chalk.dim(preview(msg, 160))}`);
|
|
631
|
-
}
|
|
632
|
-
}
|
|
633
|
-
}
|
|
634
|
-
})
|
|
635
|
-
);
|
|
636
|
-
activity.stop(chalk.dim("Done thinking."));
|
|
637
|
-
return { text: result.text ?? "" };
|
|
638
|
-
} catch (e) {
|
|
639
|
-
activity.fail(e instanceof RunCancelledError ? "Cancelled." : "The AI run failed.");
|
|
640
|
-
throw e;
|
|
641
|
-
}
|
|
642
|
-
}
|
|
643
|
-
function reportApply(result) {
|
|
644
|
-
for (const s of result.shell) {
|
|
645
|
-
const head = s.ok ? chalk.green(`$ ${s.command}`) : chalk.red(`$ ${s.command} (exit ${s.exitCode ?? "?"})`);
|
|
646
|
-
console.log(`
|
|
647
|
-
${head}`);
|
|
648
|
-
if (s.output) console.log(chalk.dim(s.output));
|
|
649
|
-
}
|
|
650
|
-
if (result.errors.length) {
|
|
651
|
-
console.log(chalk.red("\nSome operations reported errors:"));
|
|
652
|
-
for (const e of result.errors) console.log(chalk.red(` • ${e}`));
|
|
653
|
-
if (result.rolledBack) console.log(chalk.yellow(" File changes were rolled back; your files are unchanged."));
|
|
654
|
-
console.log("");
|
|
655
|
-
} else {
|
|
656
|
-
console.log(chalk.green(`
|
|
657
|
-
✓ Applied ${result.applied} change${result.applied === 1 ? "" : "s"}.
|
|
658
|
-
`));
|
|
659
|
-
}
|
|
660
|
-
}
|
|
661
|
-
|
|
662
|
-
// modes/agent/loop-guards.ts
|
|
663
|
-
var READ_ONLY = /* @__PURE__ */ new Set(["read_file", "list_files", "search_files", "analyze_codebase", "list_skills", "read_skill"]);
|
|
664
|
-
var REPEAT_NOTE = "You already made this exact call and its result has not changed (see the earlier result). Do not repeat calls — use what you have and move on to your answer.";
|
|
665
|
-
function dedupeReadOnlyTools(tools) {
|
|
666
|
-
const seen = /* @__PURE__ */ new Set();
|
|
667
|
-
const out = {};
|
|
668
|
-
for (const [name, t] of Object.entries(tools)) {
|
|
669
|
-
const exec = t.execute;
|
|
670
|
-
if (!READ_ONLY.has(name) || !exec) {
|
|
671
|
-
out[name] = t;
|
|
672
|
-
continue;
|
|
673
|
-
}
|
|
674
|
-
out[name] = {
|
|
675
|
-
...t,
|
|
676
|
-
execute: async (input, opts) => {
|
|
677
|
-
const key = `${name}:${JSON.stringify(input)}`;
|
|
678
|
-
if (seen.has(key)) return REPEAT_NOTE;
|
|
679
|
-
seen.add(key);
|
|
680
|
-
return exec(input, opts);
|
|
681
|
-
}
|
|
682
|
-
};
|
|
683
|
-
}
|
|
684
|
-
return out;
|
|
685
|
-
}
|
|
686
|
-
function finishOnLastStep(maxSteps) {
|
|
687
|
-
return ({ stepNumber }) => stepNumber >= maxSteps - 1 ? { toolChoice: "none", activeTools: [] } : {};
|
|
688
|
-
}
|
|
689
|
-
|
|
690
|
-
// tui/terminal-md.ts
|
|
691
|
-
import { marked } from "marked";
|
|
692
|
-
import { markedTerminal } from "marked-terminal";
|
|
693
|
-
var ready = false;
|
|
694
|
-
function ensureMarked() {
|
|
695
|
-
if (ready) return;
|
|
696
|
-
const width = Math.max(40, Math.min(process.stdout.columns || 80, 120));
|
|
697
|
-
marked.use(markedTerminal({ width, reflowText: true }));
|
|
698
|
-
ready = true;
|
|
699
|
-
}
|
|
700
|
-
function renderTerminalMarkdown(source) {
|
|
701
|
-
try {
|
|
702
|
-
ensureMarked();
|
|
703
|
-
return marked.parse(source.trimEnd(), { async: false }).trimEnd();
|
|
704
|
-
} catch {
|
|
705
|
-
return source;
|
|
706
|
-
}
|
|
707
|
-
}
|
|
708
|
-
|
|
709
499
|
export {
|
|
710
500
|
clipOutput,
|
|
711
|
-
ToolExecutor
|
|
712
|
-
createReadOnlyTools,
|
|
713
|
-
createAgentTools,
|
|
714
|
-
generateWithProgress,
|
|
715
|
-
reportApply,
|
|
716
|
-
REPEAT_NOTE,
|
|
717
|
-
dedupeReadOnlyTools,
|
|
718
|
-
finishOnLastStep,
|
|
719
|
-
renderTerminalMarkdown
|
|
501
|
+
ToolExecutor
|
|
720
502
|
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import {
|
|
2
2
|
classifyAction,
|
|
3
3
|
maxRisk
|
|
4
|
-
} from "./chunk-
|
|
4
|
+
} from "./chunk-5WH3FNFK.js";
|
|
5
5
|
|
|
6
6
|
// tui/ui.ts
|
|
7
7
|
import chalk from "chalk";
|
|
@@ -81,9 +81,6 @@ var Activity = class {
|
|
|
81
81
|
}
|
|
82
82
|
};
|
|
83
83
|
|
|
84
|
-
// modes/agent/approval.ts
|
|
85
|
-
import chalk3 from "chalk";
|
|
86
|
-
|
|
87
84
|
// modes/agent/diff-view.ts
|
|
88
85
|
import { createTwoFilesPatch } from "diff";
|
|
89
86
|
import chalk2 from "chalk";
|
|
@@ -115,6 +112,7 @@ function colorizePatch(patch, maxLines = 400) {
|
|
|
115
112
|
}
|
|
116
113
|
|
|
117
114
|
// modes/agent/approval.ts
|
|
115
|
+
import chalk3 from "chalk";
|
|
118
116
|
var RISK_COLOR = {
|
|
119
117
|
low: chalk3.green,
|
|
120
118
|
medium: chalk3.yellow,
|
|
@@ -348,7 +346,11 @@ async function runApprovalFlow(tracker, prompter, options = {}) {
|
|
|
348
346
|
export {
|
|
349
347
|
renderTable,
|
|
350
348
|
Activity,
|
|
349
|
+
composeBeforeAfter,
|
|
350
|
+
colorizePatch,
|
|
351
351
|
riskBadge,
|
|
352
352
|
groupPending,
|
|
353
|
+
stagedSummaryTable,
|
|
354
|
+
showGroup,
|
|
353
355
|
runApprovalFlow
|
|
354
356
|
};
|
|
@@ -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
|
+
};
|