-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathcontext.ts
More file actions
179 lines (159 loc) · 4.61 KB
/
context.ts
File metadata and controls
179 lines (159 loc) · 4.61 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import fs from "fs";
import path from "path";
import os from "os";
export interface ContextConfig {
enabled?: boolean;
maxHistoryCommands?: number;
}
export const DEFAULT_CONTEXT_CONFIG: ContextConfig = {
enabled: false,
maxHistoryCommands: 10,
};
// Size of each backwards-read chunk when scanning shell history files.
// Unit: bytes. 64 KiB balances I/O efficiency with memory usage.
const HISTORY_READ_CHUNK_SIZE_BYTES = 64 * 1024;
// Shell history parsing functions
function getHistoryFilePath(): string | null {
const shell = process.env.SHELL || "";
const home = os.homedir();
const isWindows = process.platform === "win32";
// Windows: Prefer PowerShell PSReadLine history if available
if (isWindows) {
const appData =
process.env.APPDATA || path.join(home, "AppData", "Roaming");
const psHistoryCandidates = [
// Windows PowerShell 5.x
path.join(
appData,
"Microsoft",
"Windows",
"PowerShell",
"PSReadLine",
"ConsoleHost_history.txt"
),
// PowerShell 7+
path.join(
appData,
"Microsoft",
"PowerShell",
"PSReadLine",
"ConsoleHost_history.txt"
),
];
for (const psPath of psHistoryCandidates) {
if (fs.existsSync(psPath)) return psPath;
}
// If nothing found on Windows, fall through to shared fallbacks that may exist under Git Bash, MSYS, or Cygwin
}
if (shell.includes("zsh")) {
return process.env.HISTFILE || path.join(home, ".zsh_history");
} else if (shell.includes("bash")) {
return process.env.HISTFILE || path.join(home, ".bash_history");
} else if (shell.includes("fish")) {
const macFish = path.join(
home,
"Library",
"Application Support",
"fish",
"fish_history"
);
const linuxFish = path.join(
home,
".local",
"share",
"fish",
"fish_history"
);
if (fs.existsSync(macFish)) return macFish;
return linuxFish;
}
// Try common paths as fallback
const commonPaths = [
path.join(home, ".zsh_history"),
path.join(home, ".bash_history"),
path.join(home, ".local", "share", "fish", "fish_history"),
path.join(home, "Library", "Application Support", "fish", "fish_history"),
];
for (const histPath of commonPaths) {
if (fs.existsSync(histPath)) {
return histPath;
}
}
return null;
}
/**
* Efficiently read the last N non-empty lines of a file without loading the whole file.
*
* Reads the file from the end in fixed-size chunks to avoid loading it entirely.
* Chunk size is defined by `HISTORY_READ_CHUNK_SIZE_BYTES` (bytes).
*/
function readLastLines({
filePath,
maxLines,
}: {
filePath: string;
maxLines: number;
}): string[] {
let fd: number | null = null;
try {
fd = fs.openSync(filePath, "r");
const { size } = fs.fstatSync(fd);
if (size === 0) return [];
let position = size;
let accumulator = "";
const buffer = Buffer.allocUnsafe(
Math.min(HISTORY_READ_CHUNK_SIZE_BYTES, size)
);
const hasEnoughLines = () =>
(accumulator.match(/\n/g)?.length || 0) >= maxLines + 1;
while (position > 0 && !hasEnoughLines()) {
const readLength = Math.min(buffer.length, position);
position -= readLength;
fs.readSync(fd, buffer, 0, readLength, position);
accumulator = buffer.toString("utf8", 0, readLength) + accumulator;
}
const allLines = accumulator.split("\n");
const nonEmpty = allLines.filter((l) => l.trim());
return nonEmpty.slice(-maxLines);
} catch {
return [];
} finally {
if (fd !== null) {
try {
fs.closeSync(fd);
} catch {}
}
}
}
function getRecentCommands(maxCommands: number): string[] {
const historyPath = getHistoryFilePath();
if (!historyPath) {
return [];
}
try {
// Include full raw lines from the history file for richer context
return readLastLines({ filePath: historyPath, maxLines: maxCommands });
} catch {
return [];
}
}
export function buildContextHistory(contextConfig: ContextConfig): string {
if (!contextConfig.enabled) {
return "";
}
let historyContext = "";
// Get recent commands
const recentCommands = getRecentCommands(
contextConfig.maxHistoryCommands ||
DEFAULT_CONTEXT_CONFIG.maxHistoryCommands!
);
if (recentCommands.length > 0) {
historyContext += "\n--- RECENT COMMANDS ---\n";
historyContext += "Recent shell commands (most recent last):\n";
recentCommands.forEach((cmd, idx) => {
historyContext += `${idx + 1}. ${cmd}\n`;
});
historyContext += "--- END COMMAND HISTORY ---\n";
}
return historyContext;
}