@koush/chatsh 1.0.12 → 1.0.15
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/LICENSE +21 -0
- package/README.md +16 -35
- package/dist/main.js +68 -5
- package/package.json +3 -3
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Koushik Dutta
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -2,34 +2,23 @@
|
|
|
2
2
|
|
|
3
3
|
A terminal-based shell assistant that connects your shell session to an LLM for context-aware help.
|
|
4
4
|
|
|
5
|
-
##
|
|
5
|
+
## Quick Start
|
|
6
6
|
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
```
|
|
12
|
-
|
|
13
|
-
The LLM receives your entire shell transcript and can provide context-aware assistance.
|
|
14
|
-
|
|
15
|
-
## Features
|
|
7
|
+
```bash
|
|
8
|
+
$ npx @koush/chatsh
|
|
9
|
+
$ ls /nonexistent
|
|
10
|
+
ls: /nonexistent: No such file or directory
|
|
16
11
|
|
|
17
|
-
|
|
18
|
-
- **LLM integration** - Multiple provider support (OpenAI, Anthropic, Google, OpenAI-compatible)
|
|
19
|
-
- **Transcript tracking** - Maintains full terminal history
|
|
20
|
-
- **Smart clear detection** - Resets transcript on terminal clear
|
|
12
|
+
$ help why did this command fail
|
|
21
13
|
|
|
22
|
-
|
|
14
|
+
The command failed because the directory /nonexistent does not exist.
|
|
15
|
+
The ls command lists directory contents, but it cannot find a path
|
|
16
|
+
that doesn't exist on your filesystem. Try ls without arguments to
|
|
17
|
+
see your current directory, or use ls / to list the root directory.
|
|
23
18
|
|
|
24
|
-
```bash
|
|
25
|
-
npm install -g @koush/chatsh
|
|
26
19
|
```
|
|
27
20
|
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
```bash
|
|
31
|
-
npx @koush/chatsh
|
|
32
|
-
```
|
|
21
|
+
The LLM sees your entire terminal transcript and provides context-aware assistance.
|
|
33
22
|
|
|
34
23
|
## Configuration
|
|
35
24
|
|
|
@@ -75,20 +64,12 @@ Create `~/.chatsh/chatsh.jsonc`:
|
|
|
75
64
|
}
|
|
76
65
|
```
|
|
77
66
|
|
|
78
|
-
##
|
|
79
|
-
|
|
80
|
-
Start chatsh:
|
|
81
|
-
|
|
82
|
-
```bash
|
|
83
|
-
chatsh
|
|
84
|
-
```
|
|
85
|
-
|
|
86
|
-
Use your shell normally. When you need help:
|
|
67
|
+
## Features
|
|
87
68
|
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
69
|
+
- Wraps zsh/bash/fish in a PTY session
|
|
70
|
+
- Multiple LLM providers (OpenAI, Anthropic, Google, OpenAI-compatible)
|
|
71
|
+
- Maintains full terminal history
|
|
72
|
+
- Resets transcript on terminal clear
|
|
92
73
|
|
|
93
74
|
## How it works
|
|
94
75
|
|
package/dist/main.js
CHANGED
|
@@ -4,12 +4,48 @@ import { once } from 'node:events';
|
|
|
4
4
|
import { readFileSync, existsSync } from 'node:fs';
|
|
5
5
|
import { homedir } from 'node:os';
|
|
6
6
|
import { join } from 'node:path';
|
|
7
|
+
import { fileURLToPath } from 'node:url';
|
|
7
8
|
import * as jsonc from 'jsonc-parser';
|
|
8
9
|
import { createOpenAI } from '@ai-sdk/openai';
|
|
9
10
|
import { createAnthropic } from '@ai-sdk/anthropic';
|
|
10
11
|
import { createGoogleGenerativeAI } from '@ai-sdk/google';
|
|
11
12
|
import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
|
|
12
13
|
import { streamText } from 'ai';
|
|
14
|
+
const __filename = fileURLToPath(import.meta.url);
|
|
15
|
+
function createEscapeConverter() {
|
|
16
|
+
let buffer = '';
|
|
17
|
+
const patterns = ['\\033[', '\\x1b[', '\\e[', '\\u001b['];
|
|
18
|
+
return {
|
|
19
|
+
convert(chunk) {
|
|
20
|
+
buffer += chunk;
|
|
21
|
+
let result = '';
|
|
22
|
+
for (const pattern of patterns) {
|
|
23
|
+
while (buffer.includes(pattern)) {
|
|
24
|
+
const idx = buffer.indexOf(pattern);
|
|
25
|
+
result += buffer.substring(0, idx);
|
|
26
|
+
result += '\x1b[';
|
|
27
|
+
buffer = buffer.substring(idx + pattern.length);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
// Keep potential partial matches in buffer
|
|
31
|
+
let safeIdx = buffer.length;
|
|
32
|
+
for (const pattern of patterns) {
|
|
33
|
+
const partialMatch = pattern.substring(0, pattern.length - 1);
|
|
34
|
+
if (buffer.endsWith(partialMatch)) {
|
|
35
|
+
safeIdx = Math.min(safeIdx, buffer.length - partialMatch.length);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
result += buffer.substring(0, safeIdx);
|
|
39
|
+
buffer = buffer.substring(safeIdx);
|
|
40
|
+
return result;
|
|
41
|
+
},
|
|
42
|
+
flush() {
|
|
43
|
+
const remaining = buffer;
|
|
44
|
+
buffer = '';
|
|
45
|
+
return remaining;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
}
|
|
13
49
|
const CONFIG_PATH = join(homedir(), '.chatsh', 'chatsh.jsonc');
|
|
14
50
|
function loadConfig() {
|
|
15
51
|
if (!existsSync(CONFIG_PATH)) {
|
|
@@ -148,14 +184,16 @@ async function main() {
|
|
|
148
184
|
try {
|
|
149
185
|
const result = streamText({
|
|
150
186
|
model,
|
|
151
|
-
system: 'You are a helpful assistant running in a terminal session. You receive a shell transcript from the user and an associated user query, if any.\n\nCRITICAL: Do NOT use any markdown formatting whatsoever. This means:\n- No bullet points (- or *)\n- No backticks (`) for code\n- No headers (#)\n- No bold/italic markers (** or _)\n- No markdown links\n\nUse plain text only.
|
|
187
|
+
system: 'You are a helpful assistant running in a terminal session. You receive a shell transcript from the user and an associated user query, if any.\n\nCRITICAL: Do NOT use any markdown formatting whatsoever. This means:\n- No bullet points (- or *)\n- No backticks (`) for code\n- No headers (#)\n- No bold/italic markers (** or _)\n- No markdown links\n\nUse plain text only. When you need to emphasize something or format output, use ANSI escape codes. They WILL be converted and rendered properly in the terminal:\n\nWrite escape codes using \\033 notation:\n- \\033[1m for bold\n- \\033[32m for green\n- \\033[31m for red\n- \\033[33m for yellow\n- \\033[34m for blue\n- \\033[36m for cyan\n- \\033[35m for magenta\n- \\033[0m to reset formatting\n\nExample: To print "Hello" in green, write: \\033[32mHello\\033[0m\n\nThe escape codes will be automatically converted and rendered as colors in the terminal.\n\nHelp the user based on the transcript context.',
|
|
152
188
|
prompt: `${transcript}\n\n${body}`
|
|
153
189
|
});
|
|
154
190
|
res.writeHead(200, { 'Content-Type': 'text/plain' });
|
|
155
191
|
res.write('\n\n');
|
|
192
|
+
const converter = createEscapeConverter();
|
|
156
193
|
for await (const chunk of result.textStream) {
|
|
157
|
-
res.write(chunk);
|
|
194
|
+
res.write(converter.convert(chunk));
|
|
158
195
|
}
|
|
196
|
+
res.write(converter.flush());
|
|
159
197
|
res.write('\n\n');
|
|
160
198
|
res.end();
|
|
161
199
|
}
|
|
@@ -180,7 +218,13 @@ async function main() {
|
|
|
180
218
|
cols: process.stdout.columns || 80,
|
|
181
219
|
rows: process.stdout.rows || 24,
|
|
182
220
|
cwd: process.cwd(),
|
|
183
|
-
env: {
|
|
221
|
+
env: {
|
|
222
|
+
...process.env,
|
|
223
|
+
CHATSH_PORT: String(port),
|
|
224
|
+
CHATSH_NODE: process.execPath,
|
|
225
|
+
CHATSH_SCRIPT: __filename,
|
|
226
|
+
CHATSH_TS: __filename.endsWith('.ts') ? '--experimental-strip-types' : ''
|
|
227
|
+
}
|
|
184
228
|
});
|
|
185
229
|
process.stdin.setRawMode(true);
|
|
186
230
|
process.stdin.resume();
|
|
@@ -200,7 +244,7 @@ async function main() {
|
|
|
200
244
|
}
|
|
201
245
|
process.stdout.write(data);
|
|
202
246
|
});
|
|
203
|
-
ptyProcess.write('help() {
|
|
247
|
+
ptyProcess.write('help() { "$CHATSH_NODE" --no-warnings $CHATSH_TS "$CHATSH_SCRIPT" --help "$*"; }\n');
|
|
204
248
|
if (shell.includes('zsh')) {
|
|
205
249
|
ptyProcess.write('bindkey "^R" history-incremental-search-backward\n');
|
|
206
250
|
}
|
|
@@ -214,4 +258,23 @@ async function main() {
|
|
|
214
258
|
ptyProcess.resize(process.stdout.columns || 80, process.stdout.rows || 24);
|
|
215
259
|
});
|
|
216
260
|
}
|
|
217
|
-
|
|
261
|
+
// Handle --help flag for shell help command
|
|
262
|
+
if (process.argv[2] === '--help') {
|
|
263
|
+
const query = process.argv.slice(3).join(' ');
|
|
264
|
+
const port = process.env.CHATSH_PORT;
|
|
265
|
+
if (!port) {
|
|
266
|
+
console.error('CHATSH_PORT not set');
|
|
267
|
+
process.exit(1);
|
|
268
|
+
}
|
|
269
|
+
const response = await fetch(`http://localhost:${port}`, {
|
|
270
|
+
method: 'POST',
|
|
271
|
+
body: query
|
|
272
|
+
});
|
|
273
|
+
for await (const chunk of response.body) {
|
|
274
|
+
process.stdout.write(chunk);
|
|
275
|
+
}
|
|
276
|
+
process.exit(0);
|
|
277
|
+
}
|
|
278
|
+
else {
|
|
279
|
+
main();
|
|
280
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@koush/chatsh",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.15",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"main": "dist/main.js",
|
|
6
6
|
"bin": {
|
|
@@ -14,8 +14,8 @@
|
|
|
14
14
|
"start": "node --experimental-strip-types src/main.ts",
|
|
15
15
|
"prepublishOnly": "npm version patch && npm run build"
|
|
16
16
|
},
|
|
17
|
-
"author": "",
|
|
18
|
-
"license": "
|
|
17
|
+
"author": "Koushik Dutta",
|
|
18
|
+
"license": "MIT",
|
|
19
19
|
"description": "",
|
|
20
20
|
"devDependencies": {
|
|
21
21
|
"@types/node": "^20.10.0",
|