kovence-cli 0.0.0-stage → 1.0.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.
@@ -0,0 +1,157 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Estimate token count with multi-byte Unicode & JSON structure adjustment (more accurate than chars/4).
5
+ */
6
+ function estimateTextTokens(text) {
7
+ const str = String(text || "");
8
+ if (!str) return 0;
9
+ let ascii = 0;
10
+ let nonAscii = 0;
11
+ for (let i = 0; i < str.length; i++) {
12
+ if (str.charCodeAt(i) <= 0x7f) ascii += 1;
13
+ else nonAscii += 1;
14
+ }
15
+ // Non-ASCII scripts (Bengali/CJK/emoji) typically consume ~1-1.5 tokens per UTF-16 char vs ~0.28 for ASCII
16
+ return Math.ceil(ascii * 0.28 + nonAscii * 1.15);
17
+ }
18
+
19
+ function estimateMessageTokens(msg) {
20
+ if (!msg) return 0;
21
+ let tokens = 6; // per-message framing overhead
22
+ if (msg.content)
23
+ tokens += estimateTextTokens(typeof msg.content === "string" ? msg.content : JSON.stringify(msg.content));
24
+ if (Array.isArray(msg.tool_calls)) {
25
+ for (const tc of msg.tool_calls) {
26
+ tokens += 8 + estimateTextTokens(tc.function?.name) + estimateTextTokens(tc.function?.arguments);
27
+ }
28
+ }
29
+ return tokens;
30
+ }
31
+
32
+ function estimateMessagesTokens(messages) {
33
+ return (Array.isArray(messages) ? messages : []).reduce((sum, m) => sum + estimateMessageTokens(m), 0);
34
+ }
35
+
36
+ /**
37
+ * Group linear chat history into atomic turns starting at each user message.
38
+ */
39
+ function splitHistoryIntoTurns(history) {
40
+ const turns = [];
41
+ let current = null;
42
+
43
+ for (const msg of Array.isArray(history) ? history : []) {
44
+ if (!msg || !msg.role) continue;
45
+ if (msg.role === "user") {
46
+ if (current && current.length) turns.push(current);
47
+ current = [msg];
48
+ continue;
49
+ }
50
+ if (!current) {
51
+ // Orphan leading assistant/tool before first user message — keep attached cleanly or discard if tool-only
52
+ if (msg.role === "tool") continue;
53
+ current = [msg];
54
+ } else {
55
+ current.push(msg);
56
+ }
57
+ }
58
+ if (current && current.length) turns.push(current);
59
+ return turns;
60
+ }
61
+
62
+ /**
63
+ * Shrink oversized earlier tool outputs within an oversized active turn without cutting user request or tool pairing.
64
+ */
65
+ function compressOversizedTurn(turnMessages, maxMessages) {
66
+ if (turnMessages.length <= maxMessages) return turnMessages;
67
+
68
+ const result = turnMessages.map((m) => ({ ...m }));
69
+ // Keep original user request (index 0) and most recent 4 messages intact; prune old tool outputs first
70
+ const keepTailFrom = Math.max(1, result.length - 4);
71
+ for (let i = 1; i < keepTailFrom; i++) {
72
+ if (result[i].role === "tool" && typeof result[i].content === "string" && result[i].content.length > 350) {
73
+ result[i].content =
74
+ `${result[i].content.slice(0, 220)}\n... [pruned earlier tool output: ${result[i].content.length} chars]`;
75
+ }
76
+ }
77
+ return result;
78
+ }
79
+
80
+ /**
81
+ * Turn-aware valid history slicer:
82
+ * - Never drops the active user request in long tool loops (B1)
83
+ * - Never splits assistant.tool_calls from its tool result messages
84
+ * - Respects both message count and token budget relative to contextWindow
85
+ */
86
+ function sliceValidHistory(history, maxMessages = 24, maxTokenBudget = 0) {
87
+ const clean = (Array.isArray(history) ? history : []).filter(Boolean);
88
+ if (clean.length === 0) return [];
89
+
90
+ const turns = splitHistoryIntoTurns(clean).filter((t) => t[0]?.role === "user");
91
+ if (turns.length === 0) return [];
92
+
93
+ const limit = Math.max(2, Number(maxMessages) || 24);
94
+ const chosenTurns = [];
95
+ let totalMsgs = 0;
96
+ let totalTokens = 0;
97
+
98
+ // Walk backward turn-by-turn from newest turn to oldest
99
+ for (let i = turns.length - 1; i >= 0; i--) {
100
+ let turn = turns[i];
101
+ const isNewestTurn = i === turns.length - 1;
102
+
103
+ if (isNewestTurn && turn.length > limit) {
104
+ turn = compressOversizedTurn(turn, limit);
105
+ }
106
+
107
+ const turnTokens = estimateMessagesTokens(turn);
108
+ const fitsMsgs = totalMsgs + turn.length <= limit;
109
+ const fitsTokens = !maxTokenBudget || totalTokens + turnTokens <= maxTokenBudget;
110
+
111
+ if (!isNewestTurn && (!fitsMsgs || !fitsTokens)) {
112
+ break;
113
+ }
114
+ chosenTurns.unshift(turn);
115
+ totalMsgs += turn.length;
116
+ totalTokens += turnTokens;
117
+ }
118
+
119
+ const flattened = chosenTurns.flat();
120
+
121
+ // Final safety guarantee: validate tool_call_id pairing so providers never reject malformed sequences
122
+ const seenToolCallIds = new Set();
123
+ const validated = [];
124
+ for (const m of flattened) {
125
+ if (m.role === "assistant" && Array.isArray(m.tool_calls)) {
126
+ for (const tc of m.tool_calls) {
127
+ if (tc?.id) seenToolCallIds.add(tc.id);
128
+ }
129
+ validated.push(m);
130
+ continue;
131
+ }
132
+ if (m.role === "tool") {
133
+ if (m.tool_call_id && !seenToolCallIds.has(m.tool_call_id)) continue;
134
+ }
135
+ validated.push(m);
136
+ }
137
+
138
+ // Remove trailing assistant tool_calls that have no matching tool response (e.g. crashed/errored mid-call)
139
+ while (validated.length > 0) {
140
+ const last = validated[validated.length - 1];
141
+ if (last.role === "assistant" && Array.isArray(last.tool_calls) && last.tool_calls.length > 0) {
142
+ validated.pop();
143
+ } else {
144
+ break;
145
+ }
146
+ }
147
+
148
+ return validated;
149
+ }
150
+
151
+ module.exports = {
152
+ estimateTextTokens,
153
+ estimateMessageTokens,
154
+ estimateMessagesTokens,
155
+ splitHistoryIntoTurns,
156
+ sliceValidHistory,
157
+ };
@@ -0,0 +1,136 @@
1
+ "use strict";
2
+
3
+ const fs = require("node:fs");
4
+ const os = require("node:os");
5
+ const path = require("node:path");
6
+
7
+ const MAX_INSTRUCTION_CHARS = 12000;
8
+
9
+ /**
10
+ * Read current git branch directly from .git/HEAD when inside a git repository.
11
+ */
12
+ function readGitBranch(workspaceRoot) {
13
+ try {
14
+ const headPath = path.join(workspaceRoot, ".git", "HEAD");
15
+ if (!fs.existsSync(headPath)) return "";
16
+ const raw = fs.readFileSync(headPath, "utf8").trim();
17
+ const m = raw.match(/^ref:\s*refs\/heads\/(.+)$/);
18
+ return m ? m[1] : raw.slice(0, 12);
19
+ } catch {
20
+ return "";
21
+ }
22
+ }
23
+
24
+ /**
25
+ * Collect concise runtime environment facts for the system prompt.
26
+ */
27
+ function gatherEnvironmentSummary(workspaceRoot = process.cwd()) {
28
+ const branch = readGitBranch(workspaceRoot);
29
+ const parts = [
30
+ `OS: ${os.type()} ${os.release()} (${process.arch})`,
31
+ `Node: ${process.version}`,
32
+ `Date: ${new Date().toISOString().slice(0, 10)}`,
33
+ ];
34
+ if (branch) parts.push(`Git Branch: ${branch}`);
35
+ return parts.join(" · ");
36
+ }
37
+
38
+ /**
39
+ * Auto-load project instruction files (KOVENCE.md, AGENTS.md, .kovence/KOVENCE.md).
40
+ */
41
+ function loadProjectInstructions(workspaceRoot = process.cwd()) {
42
+ const candidates = [
43
+ path.join(workspaceRoot, "KOVENCE.md"),
44
+ path.join(workspaceRoot, "AGENTS.md"),
45
+ path.join(workspaceRoot, ".kovence", "KOVENCE.md"),
46
+ ];
47
+ const sections = [];
48
+ let total = 0;
49
+
50
+ for (const file of candidates) {
51
+ if (!fs.existsSync(file)) continue;
52
+ try {
53
+ const stat = fs.statSync(file);
54
+ if (!stat.isFile()) continue;
55
+ const raw = fs.readFileSync(file, "utf8").trim();
56
+ if (!raw) continue;
57
+ const remain = MAX_INSTRUCTION_CHARS - total;
58
+ if (remain <= 0) break;
59
+ const slice = raw.slice(0, remain);
60
+ total += slice.length;
61
+ const rel = path.relative(workspaceRoot, file);
62
+ sections.push(`[Project Instructions: ${rel}]\n${slice}`);
63
+ } catch {
64
+ // Skip unreadable file cleanly.
65
+ }
66
+ }
67
+ return sections.join("\n\n");
68
+ }
69
+
70
+ /**
71
+ * Scan workspace metadata and generate a starter KOVENCE.md project guide (/init).
72
+ */
73
+ function generateProjectInitDoc(workspaceRoot = process.cwd()) {
74
+ const projectName = path.basename(workspaceRoot);
75
+ const tech = [];
76
+ const commands = [];
77
+
78
+ const pkgFile = path.join(workspaceRoot, "package.json");
79
+ if (fs.existsSync(pkgFile)) {
80
+ try {
81
+ const pkg = JSON.parse(fs.readFileSync(pkgFile, "utf8"));
82
+ tech.push(`Node.js (${pkg.name || projectName})`);
83
+ if (pkg.scripts && typeof pkg.scripts === "object") {
84
+ for (const [k, v] of Object.entries(pkg.scripts)) {
85
+ commands.push(`- \`npm run ${k}\`: \`${v}\``);
86
+ }
87
+ }
88
+ } catch {
89
+ tech.push("Node.js");
90
+ }
91
+ }
92
+ if (
93
+ fs.existsSync(path.join(workspaceRoot, "pyproject.toml")) ||
94
+ fs.existsSync(path.join(workspaceRoot, "requirements.txt"))
95
+ ) {
96
+ tech.push("Python");
97
+ }
98
+ if (fs.existsSync(path.join(workspaceRoot, "Cargo.toml"))) tech.push("Rust");
99
+ if (fs.existsSync(path.join(workspaceRoot, "go.mod"))) tech.push("Go");
100
+
101
+ let topEntries = [];
102
+ try {
103
+ topEntries = fs
104
+ .readdirSync(workspaceRoot, { withFileTypes: true })
105
+ .filter((e) => !e.name.startsWith(".") && e.name !== "node_modules")
106
+ .slice(0, 20)
107
+ .map((e) => `- \`${e.name}${e.isDirectory() ? "/" : ""}\``);
108
+ } catch {}
109
+
110
+ return [
111
+ `# KOVENCE.md — ${projectName}`,
112
+ "",
113
+ "Project instructions automatically loaded by Kovence CLI.",
114
+ "",
115
+ "## Stack",
116
+ tech.length ? tech.map((t) => `- ${t}`).join("\n") : "- General workspace",
117
+ "",
118
+ "## Structure",
119
+ topEntries.length ? topEntries.join("\n") : "- (empty workspace)",
120
+ "",
121
+ "## Commands",
122
+ commands.length ? commands.join("\n") : "- Add build/test/lint commands here",
123
+ "",
124
+ "## Conventions",
125
+ "- Keep changes minimal, tested, and aligned with existing code style.",
126
+ "- Run relevant checks before concluding a task.",
127
+ "",
128
+ ].join("\n");
129
+ }
130
+
131
+ module.exports = {
132
+ readGitBranch,
133
+ gatherEnvironmentSummary,
134
+ loadProjectInstructions,
135
+ generateProjectInitDoc,
136
+ };
@@ -0,0 +1,100 @@
1
+ "use strict";
2
+
3
+ /**
4
+ * Repair common malformed JSON emitted by smaller models in tool-call arguments.
5
+ */
6
+ function repairToolArguments(rawArgs) {
7
+ if (rawArgs && typeof rawArgs === "object") return rawArgs;
8
+ let src = String(rawArgs ?? "").trim();
9
+ if (!src) return {};
10
+
11
+ // Strip markdown ```json fences if wrapped
12
+ src = src
13
+ .replace(/^```(?:json)?\s*/i, "")
14
+ .replace(/\s*```$/i, "")
15
+ .trim();
16
+
17
+ try {
18
+ return JSON.parse(src);
19
+ } catch {}
20
+
21
+ // Remove trailing commas before } or ]
22
+ let fixed = src.replace(/,\s*([}\]])/g, "$1");
23
+ // Quote bare object keys: { path: "a" } -> { "path": "a" }
24
+ fixed = fixed.replace(/([{,]\s*)([A-Za-z_][A-Za-z0-9_]*)\s*:/g, '$1"$2":');
25
+ // Balance missing closing braces/brackets if truncated
26
+ let opensCurly = 0;
27
+ let opensSquare = 0;
28
+ let inStr = false;
29
+ let esc = false;
30
+ for (let i = 0; i < fixed.length; i++) {
31
+ const ch = fixed[i];
32
+ if (esc) {
33
+ esc = false;
34
+ continue;
35
+ }
36
+ if (ch === "\\") {
37
+ esc = true;
38
+ continue;
39
+ }
40
+ if (ch === '"') {
41
+ inStr = !inStr;
42
+ continue;
43
+ }
44
+ if (inStr) continue;
45
+ if (ch === "{") opensCurly += 1;
46
+ else if (ch === "}") opensCurly -= 1;
47
+ else if (ch === "[") opensSquare += 1;
48
+ else if (ch === "]") opensSquare -= 1;
49
+ }
50
+ if (inStr) fixed += '"';
51
+ while (opensSquare > 0) {
52
+ fixed += "]";
53
+ opensSquare -= 1;
54
+ }
55
+ while (opensCurly > 0) {
56
+ fixed += "}";
57
+ opensCurly -= 1;
58
+ }
59
+
60
+ try {
61
+ return JSON.parse(fixed);
62
+ } catch (err) {
63
+ throw new Error(`Invalid tool JSON arguments: ${err.message}`);
64
+ }
65
+ }
66
+
67
+ /**
68
+ * Detect repetitive identical tool calls across consecutive steps so agents don't spin forever.
69
+ */
70
+ class LoopDetector {
71
+ constructor(maxRepeats = 3) {
72
+ this.maxRepeats = maxRepeats;
73
+ this.history = [];
74
+ }
75
+
76
+ record(toolCalls = []) {
77
+ const sig = toolCalls
78
+ .map((tc) => `${tc.function?.name || ""}:${String(tc.function?.arguments || "").replace(/\s+/g, "")}`)
79
+ .sort()
80
+ .join("|");
81
+ if (!sig) return { repeated: false, count: 0 };
82
+
83
+ this.history.push(sig);
84
+ let consecutive = 0;
85
+ for (let i = this.history.length - 1; i >= 0; i--) {
86
+ if (this.history[i] === sig) consecutive += 1;
87
+ else break;
88
+ }
89
+ return {
90
+ repeated: consecutive >= this.maxRepeats,
91
+ count: consecutive,
92
+ signature: sig,
93
+ };
94
+ }
95
+ }
96
+
97
+ module.exports = {
98
+ repairToolArguments,
99
+ LoopDetector,
100
+ };