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
|
@@ -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
|
};
|
|
@@ -0,0 +1,411 @@
|
|
|
1
|
+
import {
|
|
2
|
+
Activity,
|
|
3
|
+
composeBeforeAfter,
|
|
4
|
+
groupPending,
|
|
5
|
+
runApprovalFlow,
|
|
6
|
+
showGroup,
|
|
7
|
+
stagedSummaryTable
|
|
8
|
+
} from "./chunk-QRAFFI5V.js";
|
|
9
|
+
import {
|
|
10
|
+
getTeamContext
|
|
11
|
+
} from "./chunk-SKYTL7AH.js";
|
|
12
|
+
import {
|
|
13
|
+
sha256Hex
|
|
14
|
+
} from "./chunk-E4DRW4AW.js";
|
|
15
|
+
import {
|
|
16
|
+
RunCancelledError,
|
|
17
|
+
runCancellable
|
|
18
|
+
} from "./chunk-HGBZ55ND.js";
|
|
19
|
+
import {
|
|
20
|
+
resolveInsideRoot
|
|
21
|
+
} from "./chunk-XTVJWMUH.js";
|
|
22
|
+
|
|
23
|
+
// modes/agent/agent-tools.ts
|
|
24
|
+
import { tool } from "ai";
|
|
25
|
+
import { z } from "zod";
|
|
26
|
+
function createReadOnlyTools(executor) {
|
|
27
|
+
return {
|
|
28
|
+
read_file: tool({
|
|
29
|
+
description: "Read a text file from the workspace. Use a path relative to the project root. Secret files (.env, keys) are blocked.",
|
|
30
|
+
inputSchema: z.object({
|
|
31
|
+
path: z.string().describe("Relative file path, e.g. src/index.ts")
|
|
32
|
+
}),
|
|
33
|
+
execute: async ({ path: p }) => executor.readFile(p)
|
|
34
|
+
}),
|
|
35
|
+
list_files: tool({
|
|
36
|
+
description: "List files and directories under a path (relative to the project root).",
|
|
37
|
+
inputSchema: z.object({
|
|
38
|
+
path: z.string().default("."),
|
|
39
|
+
recursive: z.boolean().optional().default(false)
|
|
40
|
+
}),
|
|
41
|
+
execute: async ({ path: p, recursive }) => executor.listFiles(p, recursive ?? false)
|
|
42
|
+
}),
|
|
43
|
+
search_files: tool({
|
|
44
|
+
description: "Find files matching a glob pattern (e.g. '*.ts', 'src/**/*.md'). Optional content substring filter.",
|
|
45
|
+
inputSchema: z.object({
|
|
46
|
+
root: z.string().default(".").describe("Directory to search, relative to the project root"),
|
|
47
|
+
pattern: z.string().describe("Glob-like pattern using * and ** (forward slashes)"),
|
|
48
|
+
content_contains: z.string().optional()
|
|
49
|
+
}),
|
|
50
|
+
execute: async ({ root, pattern, content_contains }) => executor.searchFiles(root, pattern, content_contains)
|
|
51
|
+
}),
|
|
52
|
+
analyze_codebase: tool({
|
|
53
|
+
description: "Summarize structure: file counts, total size and most common extensions. Read-only.",
|
|
54
|
+
inputSchema: z.object({
|
|
55
|
+
path: z.string().default(".")
|
|
56
|
+
}),
|
|
57
|
+
execute: async ({ path: p }) => executor.analyzeCodebase(p)
|
|
58
|
+
}),
|
|
59
|
+
list_skills: tool({
|
|
60
|
+
description: "List absolute paths to SKILL.md files under configured skill directories.",
|
|
61
|
+
inputSchema: z.object({}),
|
|
62
|
+
execute: async () => executor.listSkills()
|
|
63
|
+
}),
|
|
64
|
+
read_skill: tool({
|
|
65
|
+
description: "Read a SKILL.md file returned by list_skills.",
|
|
66
|
+
inputSchema: z.object({
|
|
67
|
+
path: z.string()
|
|
68
|
+
}),
|
|
69
|
+
execute: async ({ path: p }) => executor.readSkill(p)
|
|
70
|
+
})
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
function createAgentTools(executor, options = {}) {
|
|
74
|
+
const base = {
|
|
75
|
+
...createReadOnlyTools(executor),
|
|
76
|
+
create_file: tool({
|
|
77
|
+
description: "Stage creation of a new file (not written until the user approves).",
|
|
78
|
+
inputSchema: z.object({
|
|
79
|
+
path: z.string(),
|
|
80
|
+
content: z.string()
|
|
81
|
+
}),
|
|
82
|
+
execute: async ({ path: p, content }) => executor.createFile(p, content)
|
|
83
|
+
}),
|
|
84
|
+
modify_file: tool({
|
|
85
|
+
description: "Stage a full-file replacement for an existing file (pending approval). Provide the complete new contents.",
|
|
86
|
+
inputSchema: z.object({
|
|
87
|
+
path: z.string(),
|
|
88
|
+
content: z.string().describe("Complete new file contents")
|
|
89
|
+
}),
|
|
90
|
+
execute: async ({ path: p, content }) => executor.modifyFile(p, content)
|
|
91
|
+
}),
|
|
92
|
+
delete_file: tool({
|
|
93
|
+
description: "Stage deletion of a file (pending approval).",
|
|
94
|
+
inputSchema: z.object({
|
|
95
|
+
path: z.string()
|
|
96
|
+
}),
|
|
97
|
+
execute: async ({ path: p }) => executor.deleteFile(p)
|
|
98
|
+
}),
|
|
99
|
+
create_folder: tool({
|
|
100
|
+
description: "Stage creation of a directory tree (pending approval).",
|
|
101
|
+
inputSchema: z.object({
|
|
102
|
+
path: z.string().describe("Relative directory path")
|
|
103
|
+
}),
|
|
104
|
+
execute: async ({ path: p }) => executor.createFolder(p)
|
|
105
|
+
})
|
|
106
|
+
};
|
|
107
|
+
if (!options.allowShell) return base;
|
|
108
|
+
return {
|
|
109
|
+
...base,
|
|
110
|
+
execute_shell: tool({
|
|
111
|
+
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).",
|
|
112
|
+
inputSchema: z.object({
|
|
113
|
+
command: z.string().describe("A single command line")
|
|
114
|
+
}),
|
|
115
|
+
execute: async ({ command }) => executor.queueShell(command)
|
|
116
|
+
})
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
// modes/agent/run.ts
|
|
121
|
+
import chalk from "chalk";
|
|
122
|
+
function preview(input, max = 140) {
|
|
123
|
+
let s;
|
|
124
|
+
try {
|
|
125
|
+
s = JSON.stringify(input) ?? "";
|
|
126
|
+
} catch {
|
|
127
|
+
s = String(input);
|
|
128
|
+
}
|
|
129
|
+
s = s.replace(/\s+/g, " ");
|
|
130
|
+
return s.length > max ? `${s.slice(0, max)}…` : s;
|
|
131
|
+
}
|
|
132
|
+
async function generateWithProgress(agent, prompt, label) {
|
|
133
|
+
const activity = new Activity();
|
|
134
|
+
activity.start(label);
|
|
135
|
+
try {
|
|
136
|
+
const result = await runCancellable(
|
|
137
|
+
(signal) => agent.generate({
|
|
138
|
+
prompt,
|
|
139
|
+
abortSignal: signal,
|
|
140
|
+
onStepFinish: (step) => {
|
|
141
|
+
const parts = step.content ?? [];
|
|
142
|
+
for (const part of parts) {
|
|
143
|
+
if (part.type === "tool-call") {
|
|
144
|
+
activity.log(`${chalk.green("✓")} ${chalk.bold(String(part.toolName))} ${chalk.dim(preview(part.input))}`);
|
|
145
|
+
} else if (part.type === "tool-error") {
|
|
146
|
+
const msg = part.error instanceof Error ? part.error.message : String(part.error);
|
|
147
|
+
activity.log(`${chalk.yellow("✗")} ${chalk.bold(String(part.toolName))} ${chalk.dim(preview(msg, 160))}`);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
})
|
|
152
|
+
);
|
|
153
|
+
activity.stop(chalk.dim("Done thinking."));
|
|
154
|
+
return { text: result.text ?? "" };
|
|
155
|
+
} catch (e) {
|
|
156
|
+
activity.fail(e instanceof RunCancelledError ? "Cancelled." : "The AI run failed.");
|
|
157
|
+
throw e;
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
function reportApply(result) {
|
|
161
|
+
for (const s of result.shell) {
|
|
162
|
+
const head = s.ok ? chalk.green(`$ ${s.command}`) : chalk.red(`$ ${s.command} (exit ${s.exitCode ?? "?"})`);
|
|
163
|
+
console.log(`
|
|
164
|
+
${head}`);
|
|
165
|
+
if (s.output) console.log(chalk.dim(s.output));
|
|
166
|
+
}
|
|
167
|
+
if (result.errors.length) {
|
|
168
|
+
console.log(chalk.red("\nSome operations reported errors:"));
|
|
169
|
+
for (const e of result.errors) console.log(chalk.red(` • ${e}`));
|
|
170
|
+
if (result.rolledBack) console.log(chalk.yellow(" File changes were rolled back; your files are unchanged."));
|
|
171
|
+
console.log("");
|
|
172
|
+
} else {
|
|
173
|
+
console.log(chalk.green(`
|
|
174
|
+
✓ Applied ${result.applied} change${result.applied === 1 ? "" : "s"}.
|
|
175
|
+
`));
|
|
176
|
+
}
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
// modes/agent/loop-guards.ts
|
|
180
|
+
var READ_ONLY = /* @__PURE__ */ new Set(["read_file", "list_files", "search_files", "analyze_codebase", "list_skills", "read_skill"]);
|
|
181
|
+
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.";
|
|
182
|
+
function dedupeReadOnlyTools(tools) {
|
|
183
|
+
const seen = /* @__PURE__ */ new Set();
|
|
184
|
+
const out = {};
|
|
185
|
+
for (const [name, t] of Object.entries(tools)) {
|
|
186
|
+
const exec = t.execute;
|
|
187
|
+
if (!READ_ONLY.has(name) || !exec) {
|
|
188
|
+
out[name] = t;
|
|
189
|
+
continue;
|
|
190
|
+
}
|
|
191
|
+
out[name] = {
|
|
192
|
+
...t,
|
|
193
|
+
execute: async (input, opts) => {
|
|
194
|
+
const key = `${name}:${JSON.stringify(input)}`;
|
|
195
|
+
if (seen.has(key)) return REPEAT_NOTE;
|
|
196
|
+
seen.add(key);
|
|
197
|
+
return exec(input, opts);
|
|
198
|
+
}
|
|
199
|
+
};
|
|
200
|
+
}
|
|
201
|
+
return out;
|
|
202
|
+
}
|
|
203
|
+
function finishOnLastStep(maxSteps) {
|
|
204
|
+
return ({ stepNumber }) => stepNumber >= maxSteps - 1 ? { toolChoice: "none", activeTools: [] } : {};
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
// tui/terminal-md.ts
|
|
208
|
+
import { marked } from "marked";
|
|
209
|
+
import { markedTerminal } from "marked-terminal";
|
|
210
|
+
var ready = false;
|
|
211
|
+
function ensureMarked() {
|
|
212
|
+
if (ready) return;
|
|
213
|
+
const width = Math.max(40, Math.min(process.stdout.columns || 80, 120));
|
|
214
|
+
marked.use(markedTerminal({ width, reflowText: true }));
|
|
215
|
+
ready = true;
|
|
216
|
+
}
|
|
217
|
+
function renderTerminalMarkdown(source) {
|
|
218
|
+
try {
|
|
219
|
+
ensureMarked();
|
|
220
|
+
return marked.parse(source.trimEnd(), { async: false }).trimEnd();
|
|
221
|
+
} catch {
|
|
222
|
+
return source;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// team/finalize.ts
|
|
227
|
+
import chalk2 from "chalk";
|
|
228
|
+
|
|
229
|
+
// team/payload.ts
|
|
230
|
+
import crypto from "node:crypto";
|
|
231
|
+
import fs from "node:fs";
|
|
232
|
+
function newBatchId() {
|
|
233
|
+
return `b_${crypto.randomBytes(8).toString("hex")}`;
|
|
234
|
+
}
|
|
235
|
+
function diskHash(root, rel) {
|
|
236
|
+
const abs = resolveInsideRoot(root, rel);
|
|
237
|
+
try {
|
|
238
|
+
const st = fs.statSync(abs);
|
|
239
|
+
if (!st.isFile()) return null;
|
|
240
|
+
return sha256Hex(fs.readFileSync(abs));
|
|
241
|
+
} catch {
|
|
242
|
+
return null;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
function buildStagedItems(pending, root) {
|
|
246
|
+
const out = [];
|
|
247
|
+
for (const group of groupPending(pending, root)) {
|
|
248
|
+
const acts = pending.filter((a) => group.actionIds.includes(a.id)).sort((x, y) => x.timestamp.getTime() - y.timestamp.getTime());
|
|
249
|
+
if (group.kind === "shell") {
|
|
250
|
+
out.push({ group, actionIds: group.actionIds, item: { type: "shell_command", command: group.command ?? "", baseHash: null, risk: group.risk } });
|
|
251
|
+
continue;
|
|
252
|
+
}
|
|
253
|
+
if (group.kind === "folder") {
|
|
254
|
+
out.push({ group, actionIds: group.actionIds, item: { type: "folder_create", path: group.path, baseHash: null, risk: group.risk } });
|
|
255
|
+
continue;
|
|
256
|
+
}
|
|
257
|
+
const fileActs = acts.filter((a) => a.type !== "folder_create");
|
|
258
|
+
const baseHash = diskHash(root, group.path);
|
|
259
|
+
if (group.isDelete) {
|
|
260
|
+
if (baseHash === null) continue;
|
|
261
|
+
out.push({ group, actionIds: group.actionIds, item: { type: "file_delete", path: group.path, baseHash, diff: group.patch, risk: group.risk } });
|
|
262
|
+
continue;
|
|
263
|
+
}
|
|
264
|
+
const { after } = composeBeforeAfter(fileActs);
|
|
265
|
+
out.push({
|
|
266
|
+
group,
|
|
267
|
+
actionIds: group.actionIds,
|
|
268
|
+
item: { type: baseHash === null ? "file_create" : "file_modify", path: group.path, after, baseHash, diff: group.patch, risk: group.risk }
|
|
269
|
+
});
|
|
270
|
+
}
|
|
271
|
+
return out;
|
|
272
|
+
}
|
|
273
|
+
function describeItems(items) {
|
|
274
|
+
return items.map((s) => s.item.type === "shell_command" ? `run \`${s.item.command}\`` : `${s.item.type.replace("file_", "").replace("_create", " create")} ${s.item.path}`).join(", ");
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
// team/finalize.ts
|
|
278
|
+
function clip(s, n) {
|
|
279
|
+
const t = (s ?? "").trim();
|
|
280
|
+
return t.length > n ? `${t.slice(0, n - 1)}…` : t;
|
|
281
|
+
}
|
|
282
|
+
function wire(s, extra = {}) {
|
|
283
|
+
return { ...s.item, ...extra };
|
|
284
|
+
}
|
|
285
|
+
async function applyLocally(session, tracker, executor, info, team) {
|
|
286
|
+
const staged = team.client ? buildStagedItems(tracker.PendingMutation(), session.root) : [];
|
|
287
|
+
const ok = await runApprovalFlow(tracker, session.prompter, { root: session.root });
|
|
288
|
+
if (!ok) return { approved: false };
|
|
289
|
+
const result = executor.applyApprovedFromTracker();
|
|
290
|
+
reportApply(result);
|
|
291
|
+
if (team.client && !team.offline && staged.length) {
|
|
292
|
+
const byId = new Map(tracker.getActions().map((a) => [a.id, a]));
|
|
293
|
+
const decided = staged.map((s) => ({ s, statuses: s.actionIds.map((id) => byId.get(id)?.status) })).filter(({ statuses }) => statuses.some((st) => st === "applied" || st === "failed"));
|
|
294
|
+
if (decided.length) {
|
|
295
|
+
const items = decided.map(({ s, statuses }) => {
|
|
296
|
+
const good = statuses.every((st) => st === "applied");
|
|
297
|
+
const why = s.actionIds.map((id) => byId.get(id)?.details.error).find(Boolean);
|
|
298
|
+
return wire(s, { after: void 0, ok: good, message: good ? "Applied" : clip(why ?? "Failed", 300) });
|
|
299
|
+
});
|
|
300
|
+
try {
|
|
301
|
+
await team.client.recordApplied({ task: clip(info.task, 1e3), summary: clip(info.summary, 1500), batchId: newBatchId(), items });
|
|
302
|
+
console.log(chalk2.dim(`📋 Logged ${items.length} change${items.length === 1 ? "" : "s"} on the team dashboard.
|
|
303
|
+
`));
|
|
304
|
+
} catch (e) {
|
|
305
|
+
console.log(chalk2.yellow(`⚠ Applied locally, but could not log on the team dashboard: ${e.message}
|
|
306
|
+
`));
|
|
307
|
+
}
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
return { approved: true, result };
|
|
311
|
+
}
|
|
312
|
+
async function sendForApproval(session, tracker, info, team) {
|
|
313
|
+
const pending = tracker.PendingMutation();
|
|
314
|
+
const staged = buildStagedItems(pending, session.root);
|
|
315
|
+
if (staged.length === 0) {
|
|
316
|
+
tracker.discardPending("nothing to send");
|
|
317
|
+
console.log(chalk2.dim("\nNothing to send.\n"));
|
|
318
|
+
return { approved: false };
|
|
319
|
+
}
|
|
320
|
+
console.log(chalk2.bold(`
|
|
321
|
+
📝 Staged changes (${staged.length}) — nothing has been written`));
|
|
322
|
+
console.log(chalk2.dim(` Your level is Needs approval: these go to the Admin, who approves or rejects each one.
|
|
323
|
+
`));
|
|
324
|
+
console.log(stagedSummaryTable(pending, session.root));
|
|
325
|
+
for (const s of staged) showGroup(s.group);
|
|
326
|
+
console.log("");
|
|
327
|
+
const how = info.autoSend ? "all" : await session.prompter.select({
|
|
328
|
+
message: `Send ${staged.length === 1 ? "this change" : `these ${staged.length} changes`} to the Admin for approval?`,
|
|
329
|
+
options: [
|
|
330
|
+
{ value: "all", label: "Send all for approval" },
|
|
331
|
+
...staged.length > 1 ? [{ value: "pick", label: "Choose which to send" }] : [],
|
|
332
|
+
{ value: "none", label: "Discard (send nothing)" }
|
|
333
|
+
],
|
|
334
|
+
initialValue: "all"
|
|
335
|
+
});
|
|
336
|
+
let chosen = staged;
|
|
337
|
+
if (how === "none") chosen = [];
|
|
338
|
+
if (how === "pick") {
|
|
339
|
+
const ids = await session.prompter.multiselect({
|
|
340
|
+
message: "Which changes should go to the Admin?",
|
|
341
|
+
options: staged.map((s, i) => ({ value: String(i), label: s.group.label })),
|
|
342
|
+
initialValues: staged.map((_, i) => String(i)),
|
|
343
|
+
required: false
|
|
344
|
+
});
|
|
345
|
+
chosen = staged.filter((_, i) => ids.includes(String(i)));
|
|
346
|
+
}
|
|
347
|
+
if (chosen.length === 0) {
|
|
348
|
+
tracker.discardPending("not sent");
|
|
349
|
+
console.log(chalk2.yellow("\nNothing was sent and nothing was written.\n"));
|
|
350
|
+
return { approved: false, sent: 0 };
|
|
351
|
+
}
|
|
352
|
+
const res = await team.client.propose({
|
|
353
|
+
task: clip(info.task, 1e3),
|
|
354
|
+
summary: clip(info.summary, 1500),
|
|
355
|
+
batchId: newBatchId(),
|
|
356
|
+
items: chosen.map((s) => wire(s))
|
|
357
|
+
});
|
|
358
|
+
for (const s of chosen) for (const id of s.actionIds) tracker.updateStatus(id, "submitted", false);
|
|
359
|
+
tracker.discardPending("not sent");
|
|
360
|
+
console.log(chalk2.green(`
|
|
361
|
+
📨 Sent ${res.ids.length} change${res.ids.length === 1 ? "" : "s"} to the Admin for approval.`) + chalk2.dim(" Nothing was written yet.\n"));
|
|
362
|
+
const wait = session.prompter.interactive ? await session.prompter.confirm({ message: "Wait here for the Admin's decision? (approved changes are applied here right after)", initialValue: true }) : false;
|
|
363
|
+
if (wait) {
|
|
364
|
+
const { runTeamSync } = await import("./sync-S2A65FIE.js");
|
|
365
|
+
const summary = await runTeamSync(session, { wait: true });
|
|
366
|
+
return { approved: summary.applied > 0, sent: res.ids.length };
|
|
367
|
+
}
|
|
368
|
+
console.log(chalk2.dim(" When the Admin approves on the dashboard, run ") + chalk2.bold("saarthiclaw team sync") + chalk2.dim(" to apply them here"));
|
|
369
|
+
console.log(chalk2.dim(" (or ") + chalk2.bold("saarthiclaw team sync --wait") + chalk2.dim(" to wait for the decision).\n"));
|
|
370
|
+
return { approved: false, sent: res.ids.length };
|
|
371
|
+
}
|
|
372
|
+
async function offerSuggestion(session, tracker, info, team) {
|
|
373
|
+
const staged = buildStagedItems(tracker.PendingMutation(), session.root);
|
|
374
|
+
tracker.discardPending("suggest-only level");
|
|
375
|
+
console.log(chalk2.yellow("\nYour level is Suggest only, so code changes can't be applied or sent for approval. Nothing was written."));
|
|
376
|
+
const send = await session.prompter.confirm({ message: "Send this as a suggestion to the Admin instead?", initialValue: true });
|
|
377
|
+
if (!send) return { approved: false };
|
|
378
|
+
const touched = staged.length ? ` (the agent wanted to ${clip(describeItems(staged), 200)})` : "";
|
|
379
|
+
const message = clip(`${info.task}${touched}`, 500).padEnd(10, ".");
|
|
380
|
+
await team.client.suggest(message);
|
|
381
|
+
console.log(chalk2.green("\n💡 Suggestion sent. The Admin sees it on the dashboard under Suggestions.\n"));
|
|
382
|
+
return { approved: false };
|
|
383
|
+
}
|
|
384
|
+
async function finalizeStaged(session, tracker, executor, info) {
|
|
385
|
+
if (tracker.PendingMutation().length === 0) {
|
|
386
|
+
console.log(chalk2.dim("\nNothing to approve — no file changes were staged.\n"));
|
|
387
|
+
return { approved: false };
|
|
388
|
+
}
|
|
389
|
+
const team = await getTeamContext(session);
|
|
390
|
+
switch (team.mode) {
|
|
391
|
+
case "solo":
|
|
392
|
+
case "admin":
|
|
393
|
+
case "trusted":
|
|
394
|
+
return applyLocally(session, tracker, executor, info, team);
|
|
395
|
+
case "needs_approval":
|
|
396
|
+
return sendForApproval(session, tracker, info, team);
|
|
397
|
+
case "suggest_only":
|
|
398
|
+
return offerSuggestion(session, tracker, info, team);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
export {
|
|
403
|
+
createReadOnlyTools,
|
|
404
|
+
createAgentTools,
|
|
405
|
+
generateWithProgress,
|
|
406
|
+
REPEAT_NOTE,
|
|
407
|
+
dedupeReadOnlyTools,
|
|
408
|
+
finishOnLastStep,
|
|
409
|
+
renderTerminalMarkdown,
|
|
410
|
+
finalizeStaged
|
|
411
|
+
};
|
|
@@ -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
|
};
|