@koush/chatsh 1.0.13 → 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.
Files changed (3) hide show
  1. package/LICENSE +21 -0
  2. package/dist/main.js +38 -2
  3. 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/dist/main.js CHANGED
@@ -12,6 +12,40 @@ import { createGoogleGenerativeAI } from '@ai-sdk/google';
12
12
  import { createOpenAICompatible } from '@ai-sdk/openai-compatible';
13
13
  import { streamText } from 'ai';
14
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
+ }
15
49
  const CONFIG_PATH = join(homedir(), '.chatsh', 'chatsh.jsonc');
16
50
  function loadConfig() {
17
51
  if (!existsSync(CONFIG_PATH)) {
@@ -150,14 +184,16 @@ async function main() {
150
184
  try {
151
185
  const result = streamText({
152
186
  model,
153
- 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. If you need to emphasize something or format code examples, use ANSI escape codes instead (e.g., \\x1b[1m for bold, \\x1b[32m for green, \\x1b[0m to reset).\n\nHelp the user based on the transcript context.',
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.',
154
188
  prompt: `${transcript}\n\n${body}`
155
189
  });
156
190
  res.writeHead(200, { 'Content-Type': 'text/plain' });
157
191
  res.write('\n\n');
192
+ const converter = createEscapeConverter();
158
193
  for await (const chunk of result.textStream) {
159
- res.write(chunk);
194
+ res.write(converter.convert(chunk));
160
195
  }
196
+ res.write(converter.flush());
161
197
  res.write('\n\n');
162
198
  res.end();
163
199
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@koush/chatsh",
3
- "version": "1.0.13",
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": "ISC",
17
+ "author": "Koushik Dutta",
18
+ "license": "MIT",
19
19
  "description": "",
20
20
  "devDependencies": {
21
21
  "@types/node": "^20.10.0",