agent-sh 0.5.0 → 0.7.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.
Files changed (54) hide show
  1. package/README.md +12 -43
  2. package/dist/agent/agent-loop.d.ts +1 -0
  3. package/dist/agent/agent-loop.js +119 -26
  4. package/dist/agent/subagent.js +3 -1
  5. package/dist/agent/system-prompt.d.ts +1 -1
  6. package/dist/agent/system-prompt.js +21 -16
  7. package/dist/agent/tools/bash.js +10 -1
  8. package/dist/agent/tools/display.d.ts +13 -0
  9. package/dist/agent/tools/display.js +70 -0
  10. package/dist/agent/tools/edit-file.js +60 -7
  11. package/dist/agent/tools/glob.js +39 -7
  12. package/dist/agent/tools/grep.js +111 -20
  13. package/dist/agent/tools/ls.js +31 -2
  14. package/dist/agent/tools/read-file.d.ts +9 -1
  15. package/dist/agent/tools/read-file.js +50 -4
  16. package/dist/agent/tools/user-shell.js +40 -13
  17. package/dist/agent/tools/write-file.js +9 -1
  18. package/dist/agent/types.d.ts +35 -1
  19. package/dist/context-manager.d.ts +3 -1
  20. package/dist/context-manager.js +11 -1
  21. package/dist/core.d.ts +1 -3
  22. package/dist/core.js +23 -12
  23. package/dist/event-bus.d.ts +41 -3
  24. package/dist/extension-loader.d.ts +1 -1
  25. package/dist/extension-loader.js +1 -3
  26. package/dist/extensions/overlay-agent.d.ts +11 -0
  27. package/dist/extensions/overlay-agent.js +43 -0
  28. package/dist/extensions/terminal-buffer.d.ts +14 -0
  29. package/dist/extensions/terminal-buffer.js +120 -0
  30. package/dist/extensions/tui-renderer.js +344 -83
  31. package/dist/index.js +45 -36
  32. package/dist/input-handler.js +10 -3
  33. package/dist/output-parser.js +8 -0
  34. package/dist/settings.js +1 -1
  35. package/dist/shell.d.ts +5 -0
  36. package/dist/shell.js +29 -4
  37. package/dist/types.d.ts +13 -0
  38. package/dist/utils/diff.js +10 -0
  39. package/dist/utils/floating-panel.d.ts +198 -0
  40. package/dist/utils/floating-panel.js +590 -0
  41. package/dist/utils/markdown.d.ts +1 -0
  42. package/dist/utils/markdown.js +23 -1
  43. package/dist/utils/output-writer.d.ts +14 -0
  44. package/dist/utils/output-writer.js +16 -0
  45. package/dist/utils/terminal-buffer.d.ts +65 -0
  46. package/dist/utils/terminal-buffer.js +166 -0
  47. package/dist/utils/tool-display.d.ts +4 -0
  48. package/dist/utils/tool-display.js +22 -5
  49. package/examples/extensions/claude-code-bridge/index.ts +8 -12
  50. package/examples/extensions/overlay-agent.ts +70 -0
  51. package/examples/extensions/pi-bridge/index.ts +10 -12
  52. package/examples/extensions/secret-guard.ts +100 -0
  53. package/examples/extensions/terminal-buffer.ts +184 -0
  54. package/package.json +5 -1
@@ -0,0 +1,184 @@
1
+ /**
2
+ * Terminal buffer extension.
3
+ *
4
+ * Maintains a headless xterm.js terminal fed from raw PTY data.
5
+ * Provides an accurate, clean-text snapshot of the terminal screen
6
+ * that the agent can use for context — handling ANSI codes, cursor
7
+ * movement, alternate screen (vim/htop), and line wrapping correctly.
8
+ *
9
+ * Registers two agent tools:
10
+ * - terminal_read: get the current screen contents + cursor position
11
+ * - terminal_keys: send raw keystrokes into the user's live PTY
12
+ *
13
+ * Together these let the agent operate inside interactive programs
14
+ * (vim, htop, less, etc.) by reading the screen and typing keys.
15
+ *
16
+ * Requires: npm install @xterm/headless@5.5.0 @xterm/addon-serialize@0.13.0
17
+ *
18
+ * Usage:
19
+ * agent-sh -e ./examples/extensions/terminal-buffer.ts
20
+ *
21
+ * # Or copy to ~/.agent-sh/extensions/ for permanent use:
22
+ * cp examples/extensions/terminal-buffer.ts ~/.agent-sh/extensions/
23
+ */
24
+ import type { ExtensionContext } from "agent-sh/types";
25
+
26
+ /** Wait for PTY output to settle after sending keystrokes. */
27
+ function settle(ms = 100): Promise<void> {
28
+ return new Promise((resolve) => setTimeout(resolve, ms));
29
+ }
30
+
31
+ /** Interpret C-style escape sequences in a string (e.g. \r → CR, \x1b → ESC). */
32
+ function interpretEscapes(str: string): string {
33
+ return str.replace(/\\(x[0-9a-fA-F]{2}|r|n|t|\\|0)/g, (_, seq: string) => {
34
+ if (seq === "r") return "\r";
35
+ if (seq === "n") return "\n";
36
+ if (seq === "t") return "\t";
37
+ if (seq === "\\") return "\\";
38
+ if (seq === "0") return "\0";
39
+ if (seq.startsWith("x")) return String.fromCharCode(parseInt(seq.slice(1), 16));
40
+ return seq;
41
+ });
42
+ }
43
+
44
+ export default function activate({ bus, terminalBuffer: tb, registerTool }: ExtensionContext): void {
45
+ if (!tb) {
46
+ console.warn("terminal-buffer: @xterm/headless not installed — extension disabled");
47
+ return;
48
+ }
49
+
50
+ // ── Agent tools ─────────────────────────────────────────────
51
+ // Context injection is intentionally NOT done here — the terminal
52
+ // buffer content would bloat every agent message. The agent can
53
+ // call terminal_read on demand, and the overlay extension injects
54
+ // context only when the overlay is active.
55
+
56
+ registerTool({
57
+ name: "terminal_read",
58
+ description:
59
+ "Read the current terminal screen contents. Returns clean text (ANSI stripped) " +
60
+ "with cursor position and whether an alternate-screen program (vim, htop, less) is active. " +
61
+ "Use this to see what the user sees before sending keystrokes with terminal_keys.",
62
+ input_schema: {
63
+ type: "object",
64
+ properties: {},
65
+ },
66
+ showOutput: true,
67
+
68
+ getDisplayInfo: () => ({
69
+ kind: "read" as const,
70
+ icon: "⊞",
71
+ locations: [],
72
+ }),
73
+
74
+ async execute() {
75
+ const { text, altScreen, cursorX, cursorY } = tb.readScreen();
76
+ const info = [
77
+ altScreen ? "mode: alternate screen" : "mode: normal",
78
+ `cursor: row=${cursorY} col=${cursorX}`,
79
+ ].join(", ");
80
+
81
+ return {
82
+ content: `[${info}]\n\n${text}`,
83
+ exitCode: 0,
84
+ isError: false,
85
+ };
86
+ },
87
+ });
88
+
89
+ registerTool({
90
+ name: "terminal_keys",
91
+ description:
92
+ "Send keystrokes to the user's live terminal. The keys are written directly to the PTY " +
93
+ "as if the user typed them. Use escape sequences for special keys:\n" +
94
+ " - Escape: \\x1b\n" +
95
+ " - Enter/Return: \\r\n" +
96
+ " - Tab: \\t\n" +
97
+ " - Ctrl+C: \\x03\n" +
98
+ " - Ctrl+D: \\x04\n" +
99
+ " - Ctrl+Z: \\x1a\n" +
100
+ " - Arrow keys: \\x1b[A (up), \\x1b[B (down), \\x1b[C (right), \\x1b[D (left)\n" +
101
+ " - Backspace: \\x7f\n\n" +
102
+ "Example: to quit vim without saving, send keys=\"\\x1b:q!\\r\" (Escape, :q!, Enter).\n" +
103
+ "Always call terminal_read after sending keys to verify the result.",
104
+ input_schema: {
105
+ type: "object",
106
+ properties: {
107
+ keys: {
108
+ type: "string",
109
+ description:
110
+ "The keystrokes to send. Use \\x1b for Escape, \\r for Enter, \\t for Tab, " +
111
+ "\\x03 for Ctrl+C, etc. Regular characters are sent as-is.",
112
+ },
113
+ settle_ms: {
114
+ type: "number",
115
+ description:
116
+ "Milliseconds to wait after sending keys for the terminal to settle before " +
117
+ "returning (default: 150). Increase for slow programs.",
118
+ },
119
+ },
120
+ required: ["keys"],
121
+ },
122
+ showOutput: false,
123
+
124
+ getDisplayInfo: (args) => ({
125
+ kind: "execute" as const,
126
+ icon: "⌨",
127
+ locations: [],
128
+ }),
129
+
130
+ formatCall: (args) => {
131
+ const keys = args.keys as string;
132
+ // Show a readable version of the keys — handle both literal
133
+ // escape strings (\\x1b) and actual bytes (\x1b)
134
+ return keys
135
+ .replace(/\\x1b|\x1b/g, "ESC")
136
+ .replace(/\\r|\r/g, "⏎")
137
+ .replace(/\\n|\n/g, "↵")
138
+ .replace(/\\t|\t/g, "TAB")
139
+ .replace(/\\x03|\x03/g, "^C")
140
+ .replace(/\\x04|\x04/g, "^D")
141
+ .replace(/\\x7f|\x7f/g, "BS");
142
+ },
143
+
144
+ async execute(args) {
145
+ const raw = args.keys as string;
146
+ const keys = interpretEscapes(raw);
147
+ const settleMs = (args.settle_ms as number) ?? 150;
148
+
149
+ // Force PTY output visible so the user sees the program's response.
150
+ // Stays visible for the rest of agent processing — Shell resets
151
+ // paused=false on processing-done anyway.
152
+ bus.emit("shell:stdout-show", {});
153
+ process.stdout.write("\n");
154
+ bus.emit("shell:pty-write", { data: keys });
155
+
156
+ // Wait for the terminal to process the keystrokes and render
157
+ await settle(settleMs);
158
+
159
+ // Return the screen state after the keystrokes
160
+ const { text, altScreen, cursorX, cursorY } = tb.readScreen();
161
+ const info = [
162
+ altScreen ? "mode: alternate screen" : "mode: normal",
163
+ `cursor: row=${cursorY} col=${cursorX}`,
164
+ ].join(", ");
165
+
166
+ return {
167
+ content: `Keys sent. Screen after:\n[${info}]\n\n${text}`,
168
+ exitCode: 0,
169
+ isError: false,
170
+ };
171
+ },
172
+ });
173
+
174
+ // ── Bus snapshot for other extensions ───────────────────────
175
+
176
+ bus.on("shell:buffer-request", () => {
177
+ const { text, altScreen, cursorX, cursorY } = tb.readScreen();
178
+ bus.emit("shell:buffer-snapshot", {
179
+ text,
180
+ altScreen,
181
+ cursor: { x: cursorX, y: cursorY },
182
+ });
183
+ });
184
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-sh",
3
- "version": "0.5.0",
3
+ "version": "0.7.0",
4
4
  "description": "A shell-first terminal where AI is one keystroke away",
5
5
  "type": "module",
6
6
  "main": "dist/core.js",
@@ -29,6 +29,10 @@
29
29
  "./utils/stream-transform": {
30
30
  "types": "./dist/utils/stream-transform.d.ts",
31
31
  "default": "./dist/utils/stream-transform.js"
32
+ },
33
+ "./utils/terminal-buffer": {
34
+ "types": "./dist/utils/terminal-buffer.d.ts",
35
+ "default": "./dist/utils/terminal-buffer.js"
32
36
  }
33
37
  },
34
38
  "files": [