@botconnector/bccli 0.4.0-beta.9 → 0.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.en.md +242 -0
- package/README.md +80 -4
- package/dist/acp-ATIL4JOZ.js +182 -0
- package/dist/args-J5ONVGIF.js +13 -0
- package/dist/chunk-45SDLMTV.js +341 -0
- package/dist/chunk-6M5X2KZE.js +8 -0
- package/dist/chunk-7F3IWBPQ.js +191 -0
- package/dist/{chunk-CLN4Z5E3.js → chunk-BLOYFIAD.js} +6 -3
- package/dist/chunk-BNA74HPN.js +145 -0
- package/dist/{chunk-DX7CZTBB.js → chunk-BQ2IGPBR.js} +14 -9
- package/dist/chunk-JL5ZCSP2.js +76 -0
- package/dist/chunk-KEMYIGCP.js +235 -0
- package/dist/{chunk-BFZXRKW5.js → chunk-L7QI6JN2.js} +24 -2
- package/dist/chunk-LACUWBWS.js +361 -0
- package/dist/chunk-M7CALSP3.js +733 -0
- package/dist/{chunk-ELML4KVQ.js → chunk-O7M3TGIW.js} +34 -2
- package/dist/chunk-PSO7R5AD.js +10 -0
- package/dist/chunk-PUN4HMAG.js +940 -0
- package/dist/{chunk-SJH3YLW4.js → chunk-QDQU6VY2.js} +60 -6
- package/dist/chunk-QZKLZELZ.js +64 -0
- package/dist/{chunk-7S6JXQCH.js → chunk-SPRNLS7K.js} +2 -1
- package/dist/chunk-TLPFQIS2.js +231 -0
- package/dist/chunk-TVALKHFB.js +2143 -0
- package/dist/chunk-UZWJQF67.js +500 -0
- package/dist/chunk-XBO5GACQ.js +56 -0
- package/dist/cli.js +151 -1382
- package/dist/completion-HPPFDAPB.js +125 -0
- package/dist/{config-YUNKPCNB.js → config-COPNMMV6.js} +5 -2
- package/dist/config-RZ5JONMX.js +30 -0
- package/dist/ghBotCli-IBCZIWT5.js +677 -0
- package/dist/integrationCli-P75R2VSC.js +685 -0
- package/dist/login-SYIQP46J.js +16 -0
- package/dist/{mcpCli-DZWQPTOS.js → mcpCli-QUV5SD64.js} +42 -9
- package/dist/modelsCli-TRRB5LX4.js +36 -0
- package/dist/npmRunner-LU43GM3W.js +46 -0
- package/dist/oauthLogin-6FWQABJ7.js +210 -0
- package/dist/pluginsCli-GNW4TPVV.js +284 -0
- package/dist/prCli-5IMV3VNQ.js +78 -0
- package/dist/providerCli-FAOZHOKW.js +15 -0
- package/dist/sdk.js +5490 -0
- package/dist/serve-6QLBJUYW.js +178 -0
- package/dist/serve-JG7RLIUD.js +515 -0
- package/dist/session-KBAQ4BZ5.js +12 -0
- package/dist/setup-FJAWDWV4.js +24 -0
- package/dist/statsCli-CF3R7ATL.js +353 -0
- package/dist/transferCli-TK2GSVRK.js +71 -0
- package/dist/ui-B3CJ6TGE.js +2365 -0
- package/dist/update-K5IQ2MYR.js +145 -0
- package/package.json +25 -4
- package/dist/args-JPVSGCGY.js +0 -12
- package/dist/chunk-2MGINW4O.js +0 -114
- package/dist/chunk-ISGBGFSU.js +0 -52
- package/dist/chunk-KV27KTOX.js +0 -254
- package/dist/chunk-U7GK73CK.js +0 -341
- package/dist/chunk-ZYCYTEKD.js +0 -96
- package/dist/integrationCli-BGT5UZU4.js +0 -223
- package/dist/login-6K32PIXV.js +0 -9
- package/dist/providerCli-Q7LRM2NP.js +0 -12
- package/dist/session-7UB77CEG.js +0 -9
- package/dist/ui-6GTQUJOE.js +0 -1415
|
@@ -1,4 +1,7 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
contentText
|
|
4
|
+
} from "./chunk-PUN4HMAG.js";
|
|
2
5
|
|
|
3
6
|
// src/session.ts
|
|
4
7
|
import { randomBytes } from "crypto";
|
|
@@ -36,7 +39,7 @@ var Session = class _Session {
|
|
|
36
39
|
return readdirSync(dir).filter((f) => f.endsWith(".jsonl")).map((f) => {
|
|
37
40
|
const session = new _Session(join(dir, f));
|
|
38
41
|
const first = session.load().find((m) => m.role === "user");
|
|
39
|
-
return { session, mtime: statSync(session.file).mtime, preview: first ?
|
|
42
|
+
return { session, mtime: statSync(session.file).mtime, preview: first ? contentText(first.content).slice(0, 80) : "" };
|
|
40
43
|
}).sort((a, b) => b.mtime.getTime() - a.mtime.getTime() || b.session.file.localeCompare(a.session.file));
|
|
41
44
|
}
|
|
42
45
|
static latest(home, cwd) {
|
|
@@ -49,26 +52,77 @@ var Session = class _Session {
|
|
|
49
52
|
append(message) {
|
|
50
53
|
this.write({ t: "msg", m: message });
|
|
51
54
|
}
|
|
55
|
+
appendUsage(usage) {
|
|
56
|
+
this.write({ t: "usage", ...usage });
|
|
57
|
+
}
|
|
52
58
|
reset() {
|
|
53
59
|
this.write({ t: "reset" });
|
|
54
60
|
}
|
|
55
|
-
|
|
61
|
+
records() {
|
|
56
62
|
if (!existsSync(this.file)) return [];
|
|
57
|
-
|
|
63
|
+
const out = [];
|
|
58
64
|
for (const line of readFileSync(this.file, "utf8").split("\n")) {
|
|
59
65
|
if (!line.trim()) continue;
|
|
60
|
-
let record;
|
|
61
66
|
try {
|
|
62
|
-
|
|
67
|
+
out.push(JSON.parse(line));
|
|
63
68
|
} catch {
|
|
64
|
-
continue;
|
|
65
69
|
}
|
|
70
|
+
}
|
|
71
|
+
return out;
|
|
72
|
+
}
|
|
73
|
+
/** What the model continues from: everything after the last reset (after a compaction that is the summary and the recent turns). */
|
|
74
|
+
load() {
|
|
75
|
+
let messages = [];
|
|
76
|
+
for (const record of this.records()) {
|
|
66
77
|
if (record.t === "reset") messages = [];
|
|
67
78
|
else if (record.t === "msg") messages.push(record.m);
|
|
68
79
|
}
|
|
69
80
|
return repairTail(messages);
|
|
70
81
|
}
|
|
82
|
+
/**
|
|
83
|
+
* What the user sees on resume: the whole conversation. Compaction only shrinks what the model is sent, so
|
|
84
|
+
* its reset does not hide the older turns here. A real reset (/clear, rewind) still starts over, the
|
|
85
|
+
* synthetic summary pair is never shown, and the recent turns a compaction re-writes are not shown twice.
|
|
86
|
+
*/
|
|
87
|
+
loadHistory() {
|
|
88
|
+
const segments = [[]];
|
|
89
|
+
for (const record of this.records()) {
|
|
90
|
+
if (record.t === "reset") segments.push([]);
|
|
91
|
+
else if (record.t === "msg") segments[segments.length - 1].push(record.m);
|
|
92
|
+
}
|
|
93
|
+
let shown = segments[0];
|
|
94
|
+
for (const segment of segments.slice(1)) {
|
|
95
|
+
if (!isCompactionSummary(segment[0])) {
|
|
96
|
+
shown = segment;
|
|
97
|
+
continue;
|
|
98
|
+
}
|
|
99
|
+
const kept = segment.slice(2);
|
|
100
|
+
let at = 0;
|
|
101
|
+
while (at <= shown.length && !endsWithPrefixOf(shown, at, kept)) at++;
|
|
102
|
+
shown = [...shown, ...kept.slice(shown.length - at)];
|
|
103
|
+
}
|
|
104
|
+
return repairTail(shown.filter((m) => !isCompactionSummary(m) && !isCompactionAck(m)));
|
|
105
|
+
}
|
|
71
106
|
};
|
|
107
|
+
var SUMMARY_PREFIX = "Summary of the previous conversation:";
|
|
108
|
+
var SUMMARY_ACK = "OK, I will continue from this summary.";
|
|
109
|
+
function isCompactionSummary(m) {
|
|
110
|
+
return m?.role === "user" && typeof m.content === "string" && m.content.startsWith(SUMMARY_PREFIX);
|
|
111
|
+
}
|
|
112
|
+
function isCompactionAck(m) {
|
|
113
|
+
return m.role === "assistant" && m.content === SUMMARY_ACK;
|
|
114
|
+
}
|
|
115
|
+
function sameMessage(a, b) {
|
|
116
|
+
if (a.role !== b.role) return false;
|
|
117
|
+
if (a.role === "tool" && b.role === "tool") return a.tool_call_id === b.tool_call_id;
|
|
118
|
+
return JSON.stringify(a.content) === JSON.stringify(b.content) && JSON.stringify("tool_calls" in a && a.tool_calls) === JSON.stringify("tool_calls" in b && b.tool_calls);
|
|
119
|
+
}
|
|
120
|
+
function endsWithPrefixOf(shown, at, kept) {
|
|
121
|
+
const n = shown.length - at;
|
|
122
|
+
if (n > kept.length) return false;
|
|
123
|
+
for (let j = 0; j < n; j++) if (!sameMessage(shown[at + j], kept[j])) return false;
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
72
126
|
function pruneSessions(home, now = Date.now(), maxAgeDays = 30) {
|
|
73
127
|
const root = join(home, "sessions");
|
|
74
128
|
if (!existsSync(root)) return 0;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
|
|
3
|
+
// src/tokenBudget.ts
|
|
4
|
+
var estimateTokens = (text) => Math.ceil(text.length / 4);
|
|
5
|
+
var estimateMessages = (messages) => estimateTokens(JSON.stringify(messages));
|
|
6
|
+
function trimToFit(messages, maxTokens) {
|
|
7
|
+
let out = messages;
|
|
8
|
+
while (estimateMessages(out) > maxTokens) {
|
|
9
|
+
const boundary = out.findIndex((m, i) => i > 0 && m.role === "user");
|
|
10
|
+
if (boundary === -1) return out;
|
|
11
|
+
out = out.slice(boundary);
|
|
12
|
+
}
|
|
13
|
+
return out;
|
|
14
|
+
}
|
|
15
|
+
function splitKeepRecent(messages, keepTokens) {
|
|
16
|
+
if (messages.length === 0) return [[], []];
|
|
17
|
+
let start = messages.length;
|
|
18
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
19
|
+
if (i > 0 && messages[i].role !== "user") continue;
|
|
20
|
+
if (estimateMessages(messages.slice(i)) <= keepTokens) start = i;
|
|
21
|
+
else break;
|
|
22
|
+
}
|
|
23
|
+
if (start === messages.length) {
|
|
24
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
25
|
+
if (messages[i].role === "user") {
|
|
26
|
+
start = i;
|
|
27
|
+
break;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
return [messages.slice(0, start), messages.slice(start)];
|
|
32
|
+
}
|
|
33
|
+
var TOOL_STUB = "[Old tool result content cleared]";
|
|
34
|
+
var TOOL_PRUNE_KEEP_TOKENS = 4e4;
|
|
35
|
+
var TOOL_PRUNE_MIN_TOKENS = 2e4;
|
|
36
|
+
var isToolStub = (content) => content.startsWith(TOOL_STUB);
|
|
37
|
+
function pruneOldToolOutputs(messages) {
|
|
38
|
+
let kept = 0;
|
|
39
|
+
let oldTokens = 0;
|
|
40
|
+
const old = [];
|
|
41
|
+
for (let i = messages.length - 1; i >= 0; i--) {
|
|
42
|
+
const m = messages[i];
|
|
43
|
+
if (m.role !== "tool" || isToolStub(m.content)) continue;
|
|
44
|
+
kept += estimateTokens(m.content);
|
|
45
|
+
if (kept > TOOL_PRUNE_KEEP_TOKENS) {
|
|
46
|
+
old.push(i);
|
|
47
|
+
oldTokens += estimateTokens(m.content);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
if (oldTokens <= TOOL_PRUNE_MIN_TOKENS) return false;
|
|
51
|
+
for (const i of old) {
|
|
52
|
+
const m = messages[i];
|
|
53
|
+
messages[i] = { ...m, content: TOOL_STUB };
|
|
54
|
+
}
|
|
55
|
+
return true;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
export {
|
|
59
|
+
estimateTokens,
|
|
60
|
+
estimateMessages,
|
|
61
|
+
trimToFit,
|
|
62
|
+
splitKeepRecent,
|
|
63
|
+
pruneOldToolOutputs
|
|
64
|
+
};
|
|
@@ -5,7 +5,7 @@ import {
|
|
|
5
5
|
readCredentials,
|
|
6
6
|
readJsonConfig,
|
|
7
7
|
removeCredential
|
|
8
|
-
} from "./chunk-
|
|
8
|
+
} from "./chunk-LACUWBWS.js";
|
|
9
9
|
|
|
10
10
|
// src/providers.ts
|
|
11
11
|
import { mkdirSync, writeFileSync } from "fs";
|
|
@@ -45,6 +45,7 @@ function removeGlobalProvider(id, env = process.env) {
|
|
|
45
45
|
export {
|
|
46
46
|
providerName,
|
|
47
47
|
hasKey,
|
|
48
|
+
readGlobalConfigFile,
|
|
48
49
|
writeGlobalConfig,
|
|
49
50
|
removeGlobalProvider
|
|
50
51
|
};
|
|
@@ -0,0 +1,231 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
import {
|
|
3
|
+
runCommand,
|
|
4
|
+
runHooks
|
|
5
|
+
} from "./chunk-M7CALSP3.js";
|
|
6
|
+
import {
|
|
7
|
+
ConfigError
|
|
8
|
+
} from "./chunk-LACUWBWS.js";
|
|
9
|
+
import {
|
|
10
|
+
t
|
|
11
|
+
} from "./chunk-UZWJQF67.js";
|
|
12
|
+
|
|
13
|
+
// src/verify.ts
|
|
14
|
+
var MAX_VERIFY_ROUNDS = 2;
|
|
15
|
+
async function runVerify(commands, opts) {
|
|
16
|
+
const run = opts.run ?? runCommand;
|
|
17
|
+
const failures = [];
|
|
18
|
+
for (const cmd of commands) {
|
|
19
|
+
const r = await run(cmd, { cwd: opts.cwd, timeoutMs: opts.timeoutMs ?? 3e5, signal: opts.signal, env: opts.env });
|
|
20
|
+
const exitCode = r.exitCode ?? -1;
|
|
21
|
+
if (exitCode !== 0) failures.push({ cmd, exitCode, output: r.output });
|
|
22
|
+
}
|
|
23
|
+
return failures;
|
|
24
|
+
}
|
|
25
|
+
function verifyFollowup(failures, maxBytes = 8e3) {
|
|
26
|
+
const blocks = failures.map((f) => {
|
|
27
|
+
const output = f.output.length > maxBytes ? `${f.output.slice(0, maxBytes)}
|
|
28
|
+
[truncated]` : f.output;
|
|
29
|
+
return `$ ${f.cmd}
|
|
30
|
+
[exit code ${f.exitCode}]
|
|
31
|
+
${output}`;
|
|
32
|
+
});
|
|
33
|
+
return t("Automatic verification after your edits failed. Fix the problems so these commands pass, then stop.\n\n{blocks}", {
|
|
34
|
+
blocks: blocks.join("\n\n")
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// src/print.ts
|
|
39
|
+
function toolLabel(tool) {
|
|
40
|
+
if (tool === "web_search") return "Search";
|
|
41
|
+
return tool.charAt(0).toUpperCase() + tool.slice(1);
|
|
42
|
+
}
|
|
43
|
+
async function runPrint(rt, prompt, io = { out: process.stdout, err: process.stderr }, opts = {}) {
|
|
44
|
+
const format = opts.outputFormat ?? "text";
|
|
45
|
+
const emit = (line) => io.out.write(`${JSON.stringify(line)}
|
|
46
|
+
`);
|
|
47
|
+
let result = "";
|
|
48
|
+
const toolNames = /* @__PURE__ */ new Map();
|
|
49
|
+
rt.interaction.approvePlan = async () => {
|
|
50
|
+
if (opts.allowAll) return "allowAll";
|
|
51
|
+
io.err.write(`${t("Plan needs approval; run with --allow-all.")}
|
|
52
|
+
`);
|
|
53
|
+
return "no";
|
|
54
|
+
};
|
|
55
|
+
let failed = false;
|
|
56
|
+
let wroteText = false;
|
|
57
|
+
let edited = false;
|
|
58
|
+
rt.agent.onEvent = (event) => {
|
|
59
|
+
switch (event.type) {
|
|
60
|
+
case "text":
|
|
61
|
+
if (format === "text") {
|
|
62
|
+
io.out.write(event.delta);
|
|
63
|
+
wroteText = true;
|
|
64
|
+
} else {
|
|
65
|
+
result += event.delta;
|
|
66
|
+
}
|
|
67
|
+
break;
|
|
68
|
+
case "toolStart":
|
|
69
|
+
toolNames.set(event.id, event.tool);
|
|
70
|
+
io.err.write(`\u23BF ${toolLabel(event.tool)} ${event.target}
|
|
71
|
+
`);
|
|
72
|
+
if (format === "stream-json") emit({ type: "tool_use", tool: event.tool, target: event.target });
|
|
73
|
+
break;
|
|
74
|
+
case "toolEnd":
|
|
75
|
+
if (event.isError) io.err.write(` \u2717 ${event.output.split("\n")[0]}
|
|
76
|
+
`);
|
|
77
|
+
if ((event.tool === "edit" || event.tool === "write") && !event.isError) edited = true;
|
|
78
|
+
if (format === "stream-json") emit({ type: "tool_result", tool: toolNames.get(event.id), isError: !!event.isError, output: event.output });
|
|
79
|
+
break;
|
|
80
|
+
case "stepLimit":
|
|
81
|
+
io.err.write(`${t("Step limit reached.")}
|
|
82
|
+
`);
|
|
83
|
+
failed = true;
|
|
84
|
+
break;
|
|
85
|
+
case "budgetExceeded":
|
|
86
|
+
io.err.write(`${t("Budget exceeded ({kind}): {used} of {limit}. Further model calls are blocked; raise usageCap in ~/.bccli/config.json.", { kind: event.kind, used: Math.round(event.used * 100) / 100, limit: event.limit })}
|
|
87
|
+
`);
|
|
88
|
+
if (format === "stream-json") emit({ type: "error", message: "budgetExceeded" });
|
|
89
|
+
failed = true;
|
|
90
|
+
break;
|
|
91
|
+
case "aborted":
|
|
92
|
+
io.err.write(`${t("Cancelled.")}
|
|
93
|
+
`);
|
|
94
|
+
failed = true;
|
|
95
|
+
break;
|
|
96
|
+
case "error":
|
|
97
|
+
io.err.write(`Error: ${event.message}
|
|
98
|
+
`);
|
|
99
|
+
if (format === "stream-json") emit({ type: "error", message: event.message });
|
|
100
|
+
failed = true;
|
|
101
|
+
break;
|
|
102
|
+
}
|
|
103
|
+
};
|
|
104
|
+
rt.agent.askPermission = async (req) => {
|
|
105
|
+
io.err.write(` ${t("Needs permission for {tool}. Run with --allow-all or --allowed-tools {kind}.", { tool: req.tool, kind: req.kind })}
|
|
106
|
+
`);
|
|
107
|
+
return "no";
|
|
108
|
+
};
|
|
109
|
+
const controller = new AbortController();
|
|
110
|
+
const onSigint = () => controller.abort();
|
|
111
|
+
process.once("SIGINT", onSigint);
|
|
112
|
+
const start = await runHooks(rt.config.hooks, "SessionStart", {}, { env: rt.env, cwd: rt.cwd });
|
|
113
|
+
for (const warning of start.warnings) io.err.write(`${warning}
|
|
114
|
+
`);
|
|
115
|
+
await rt.emitPlugins("SessionStart");
|
|
116
|
+
try {
|
|
117
|
+
await rt.agent.run(prompt, controller.signal);
|
|
118
|
+
const commands = rt.config.verifyCommands;
|
|
119
|
+
if (edited && commands.length && !controller.signal.aborted) {
|
|
120
|
+
for (let round = 0; round < MAX_VERIFY_ROUNDS; round++) {
|
|
121
|
+
io.err.write(`${t("Verifying edits: {cmds}", { cmds: commands.join(", ") })}
|
|
122
|
+
`);
|
|
123
|
+
const failures = await runVerify(commands, { cwd: rt.cwd, env: rt.env, signal: controller.signal });
|
|
124
|
+
if (!failures.length) {
|
|
125
|
+
io.err.write(`${t("Verification passed.")}
|
|
126
|
+
`);
|
|
127
|
+
break;
|
|
128
|
+
}
|
|
129
|
+
if (round === MAX_VERIFY_ROUNDS - 1 || controller.signal.aborted) {
|
|
130
|
+
io.err.write(`${t("Verification still failing after {n} fix round(s). Run the commands manually to see why.", { n: MAX_VERIFY_ROUNDS })}
|
|
131
|
+
`);
|
|
132
|
+
failed = true;
|
|
133
|
+
break;
|
|
134
|
+
}
|
|
135
|
+
await rt.agent.run(verifyFollowup(failures), controller.signal);
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
} finally {
|
|
139
|
+
process.off("SIGINT", onSigint);
|
|
140
|
+
}
|
|
141
|
+
const stop = await runHooks(rt.config.hooks, "Stop", {}, { env: rt.env, cwd: rt.cwd });
|
|
142
|
+
for (const warning of stop.warnings) io.err.write(`${warning}
|
|
143
|
+
`);
|
|
144
|
+
await rt.emitPlugins("Stop");
|
|
145
|
+
if (format === "json") {
|
|
146
|
+
io.out.write(`${JSON.stringify({ status: failed ? "failed" : "ok", model: rt.modelRef, result })}
|
|
147
|
+
`);
|
|
148
|
+
} else if (format === "stream-json") {
|
|
149
|
+
if (result) emit({ type: "message", text: result });
|
|
150
|
+
emit({ type: "result", status: failed ? "failed" : "ok", model: rt.modelRef });
|
|
151
|
+
} else if (wroteText) {
|
|
152
|
+
io.out.write("\n");
|
|
153
|
+
}
|
|
154
|
+
return failed ? 1 : 0;
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// src/worktree.ts
|
|
158
|
+
import { execFile } from "child_process";
|
|
159
|
+
import { existsSync } from "fs";
|
|
160
|
+
import { basename, dirname, join } from "path";
|
|
161
|
+
function git(cwd, args, bin = "git") {
|
|
162
|
+
return new Promise((resolve) => {
|
|
163
|
+
execFile(
|
|
164
|
+
bin,
|
|
165
|
+
args,
|
|
166
|
+
{ cwd, maxBuffer: 20 * 1024 * 1024 },
|
|
167
|
+
(error, stdout, stderr) => resolve({ ok: !error, out: error ? stderr : stdout, missing: error?.code === "ENOENT" })
|
|
168
|
+
);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
var NAME_RE = /^[A-Za-z0-9._-]+$/;
|
|
172
|
+
function samePath(a, b) {
|
|
173
|
+
const norm = (p) => process.platform === "win32" ? p.replace(/\//g, "\\").toLowerCase() : p.replace(/\\/g, "/");
|
|
174
|
+
return norm(a) === norm(b);
|
|
175
|
+
}
|
|
176
|
+
function worktreeEntries(porcelain) {
|
|
177
|
+
return porcelain.split(/\r?\n\r?\n/).map((block) => {
|
|
178
|
+
const path = /^worktree (.+)$/m.exec(block)?.[1]?.trim();
|
|
179
|
+
if (!path) return void 0;
|
|
180
|
+
const branch = /^branch refs\/heads\/(.+)$/m.exec(block)?.[1];
|
|
181
|
+
return branch ? { path, branch } : { path };
|
|
182
|
+
}).filter((entry) => !!entry);
|
|
183
|
+
}
|
|
184
|
+
async function resolveWorktree(cwd, name, bin = "git") {
|
|
185
|
+
if (!NAME_RE.test(name)) {
|
|
186
|
+
throw new ConfigError(t('Invalid worktree name "{name}": use letters, digits, dot, dash, or underscore (no slashes).', { name }));
|
|
187
|
+
}
|
|
188
|
+
const inside = await git(cwd, ["rev-parse", "--is-inside-work-tree"], bin);
|
|
189
|
+
if (inside.missing) throw new ConfigError(t("git is not installed or not on PATH, so -w cannot be used."));
|
|
190
|
+
if (!inside.ok || inside.out.trim() !== "true") throw new ConfigError(t("This folder is not a git repository, so -w cannot create a worktree."));
|
|
191
|
+
const list = await git(cwd, ["worktree", "list", "--porcelain"], bin);
|
|
192
|
+
if (!list.ok) throw new ConfigError(t("This folder is not a git repository, so -w cannot create a worktree."));
|
|
193
|
+
const entries = worktreeEntries(list.out);
|
|
194
|
+
const main = entries[0]?.path;
|
|
195
|
+
if (!main) throw new ConfigError(t("This folder is not a git repository, so -w cannot create a worktree."));
|
|
196
|
+
const path = join(dirname(main), `${basename(main)}.worktrees`, name);
|
|
197
|
+
if (entries.some((entry) => samePath(entry.path, path))) return { path, created: false };
|
|
198
|
+
if (existsSync(path)) {
|
|
199
|
+
throw new ConfigError(t("Worktree path {path} already exists but is not a registered worktree; remove or rename it first.", { path }));
|
|
200
|
+
}
|
|
201
|
+
const branch = `bccli/${name}`;
|
|
202
|
+
const hasBranch = (await git(main, ["rev-parse", "--verify", "-q", `refs/heads/${branch}`], bin)).ok;
|
|
203
|
+
const add = hasBranch ? await git(main, ["worktree", "add", path, branch], bin) : await git(main, ["worktree", "add", "-b", branch, path], bin);
|
|
204
|
+
if (!add.ok) throw new ConfigError(add.out.trim() || t("This folder is not a git repository, so -w cannot create a worktree."));
|
|
205
|
+
return { path, created: true };
|
|
206
|
+
}
|
|
207
|
+
async function worktreeListText(cwd, bin = "git") {
|
|
208
|
+
const inside = await git(cwd, ["rev-parse", "--is-inside-work-tree"], bin);
|
|
209
|
+
if (inside.missing) return t("git is not installed or not on PATH, so /worktree cannot be used.");
|
|
210
|
+
if (!inside.ok || inside.out.trim() !== "true") return t("This folder is not a git repository, so there is no diff.");
|
|
211
|
+
const list = await git(cwd, ["worktree", "list", "--porcelain"], bin);
|
|
212
|
+
if (!list.ok) return t("This folder is not a git repository, so there is no diff.");
|
|
213
|
+
const rows = worktreeEntries(list.out).map((entry) => ` ${entry.path}${entry.branch ? ` (${entry.branch})` : ""}`);
|
|
214
|
+
return [
|
|
215
|
+
t("Git worktrees:"),
|
|
216
|
+
...rows,
|
|
217
|
+
"",
|
|
218
|
+
t("Create one with: bccli -w <name>"),
|
|
219
|
+
t("Remove with: git worktree remove <path>")
|
|
220
|
+
].join("\n");
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
export {
|
|
224
|
+
MAX_VERIFY_ROUNDS,
|
|
225
|
+
runVerify,
|
|
226
|
+
verifyFollowup,
|
|
227
|
+
toolLabel,
|
|
228
|
+
runPrint,
|
|
229
|
+
resolveWorktree,
|
|
230
|
+
worktreeListText
|
|
231
|
+
};
|