agent-sh 0.8.0 → 0.9.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.
- package/README.md +25 -34
- package/dist/agent/agent-loop.d.ts +29 -6
- package/dist/agent/agent-loop.js +177 -59
- package/dist/agent/conversation-state.d.ts +3 -1
- package/dist/agent/conversation-state.js +6 -2
- package/dist/agent/nuclear-form.js +5 -4
- package/dist/agent/system-prompt.d.ts +4 -5
- package/dist/agent/system-prompt.js +12 -28
- package/dist/{token-budget.js → agent/token-budget.js} +1 -1
- package/dist/agent/tool-protocol.d.ts +83 -0
- package/dist/agent/tool-protocol.js +386 -0
- package/dist/agent/types.d.ts +21 -1
- package/dist/core.d.ts +7 -7
- package/dist/core.js +76 -194
- package/dist/event-bus.d.ts +26 -0
- package/dist/event-bus.js +20 -1
- package/dist/extension-loader.d.ts +5 -0
- package/dist/extension-loader.js +104 -17
- package/dist/extensions/agent-backend.d.ts +13 -0
- package/dist/extensions/agent-backend.js +167 -0
- package/dist/extensions/command-suggest.d.ts +3 -3
- package/dist/extensions/command-suggest.js +4 -3
- package/dist/extensions/index.d.ts +19 -0
- package/dist/extensions/index.js +25 -0
- package/dist/extensions/slash-commands.d.ts +1 -1
- package/dist/extensions/slash-commands.js +16 -1
- package/dist/extensions/terminal-buffer.d.ts +1 -1
- package/dist/extensions/terminal-buffer.js +13 -4
- package/dist/extensions/tui-renderer.js +63 -43
- package/dist/index.js +14 -20
- package/dist/settings.d.ts +6 -0
- package/dist/settings.js +4 -1
- package/dist/{input-handler.d.ts → shell/input-handler.d.ts} +1 -1
- package/dist/{input-handler.js → shell/input-handler.js} +60 -43
- package/dist/{output-parser.d.ts → shell/output-parser.d.ts} +1 -1
- package/dist/{output-parser.js → shell/output-parser.js} +1 -1
- package/dist/{shell.d.ts → shell/shell.d.ts} +8 -2
- package/dist/{shell.js → shell/shell.js} +20 -6
- package/dist/types.d.ts +49 -10
- package/dist/utils/compositor.d.ts +62 -0
- package/dist/utils/compositor.js +88 -0
- package/dist/utils/diff-renderer.js +92 -4
- package/dist/utils/floating-panel.d.ts +2 -0
- package/dist/utils/floating-panel.js +30 -14
- package/dist/utils/handler-registry.d.ts +26 -10
- package/dist/utils/handler-registry.js +52 -16
- package/dist/utils/line-editor.d.ts +23 -3
- package/dist/utils/line-editor.js +180 -42
- package/dist/utils/markdown.d.ts +1 -0
- package/dist/utils/markdown.js +1 -1
- package/dist/utils/message-utils.d.ts +35 -0
- package/dist/utils/message-utils.js +75 -0
- package/dist/utils/terminal-buffer.d.ts +5 -1
- package/dist/utils/terminal-buffer.js +18 -2
- package/dist/utils/tool-interactive.d.ts +12 -0
- package/dist/utils/tool-interactive.js +53 -0
- package/examples/extensions/ash-acp-bridge/README.md +39 -0
- package/examples/extensions/ash-acp-bridge/package.json +23 -0
- package/examples/extensions/ash-acp-bridge/src/index.ts +571 -0
- package/examples/extensions/ash-acp-bridge/tsconfig.json +14 -0
- package/examples/extensions/ash-mcp-bridge/README.md +72 -0
- package/examples/extensions/ash-mcp-bridge/index.ts +154 -0
- package/examples/extensions/ash-mcp-bridge/package.json +9 -0
- package/examples/extensions/interactive-prompts.ts +82 -110
- package/examples/extensions/overlay-agent.ts +84 -38
- package/examples/extensions/peer-mesh.ts +450 -0
- package/examples/extensions/questionnaire.ts +249 -0
- package/examples/extensions/tmux-pane.ts +307 -0
- package/examples/extensions/web-access.ts +327 -0
- package/package.json +9 -1
- package/dist/extensions/overlay-agent.d.ts +0 -14
- package/dist/extensions/overlay-agent.js +0 -147
- package/examples/extensions/terminal-buffer.ts +0 -184
- /package/dist/{token-budget.d.ts → agent/token-budget.d.ts} +0 -0
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { MarkdownRenderer } from "../utils/markdown.js";
|
|
2
|
-
import { palette as p } from "../utils/palette.js";
|
|
3
|
-
import { renderToolCall, formatElapsed, } from "../utils/tool-display.js";
|
|
4
|
-
export default function activate(ctx) {
|
|
5
|
-
const { bus, advise, call, createFloatingPanel } = ctx;
|
|
6
|
-
const panel = createFloatingPanel({
|
|
7
|
-
trigger: "\x1c", // Ctrl+\
|
|
8
|
-
dimBackground: true,
|
|
9
|
-
});
|
|
10
|
-
// Suppress TUI renderer when overlay owns agent output
|
|
11
|
-
advise("tui:should-render-agent", (next) => {
|
|
12
|
-
return panel.active ? false : next();
|
|
13
|
-
});
|
|
14
|
-
// Signal interactive overlay mode in dynamic context
|
|
15
|
-
advise("dynamic-context:build", (next) => {
|
|
16
|
-
const base = next();
|
|
17
|
-
if (!panel.active)
|
|
18
|
-
return base;
|
|
19
|
-
return base + "\ninteractive-session: true\n";
|
|
20
|
-
});
|
|
21
|
-
// ── Conversation state (persists across hide/show) ─────────
|
|
22
|
-
const messages = [];
|
|
23
|
-
let renderer = null;
|
|
24
|
-
let currentAssistantMsg = null;
|
|
25
|
-
// ── Tool state ─────────────────────────────────────────────
|
|
26
|
-
let toolStartTime = 0;
|
|
27
|
-
function getContentWidth() {
|
|
28
|
-
return panel.computeGeometry().contentW;
|
|
29
|
-
}
|
|
30
|
-
/** Rebuild panel content from full message history. */
|
|
31
|
-
function rebuildContent() {
|
|
32
|
-
panel.clearContent();
|
|
33
|
-
for (const msg of messages) {
|
|
34
|
-
for (const line of msg.lines) {
|
|
35
|
-
panel.appendLine(line);
|
|
36
|
-
}
|
|
37
|
-
panel.appendLine("");
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
/** Append a line to current assistant message and panel (if visible). */
|
|
41
|
-
function appendLine(line) {
|
|
42
|
-
currentAssistantMsg?.lines.push(line);
|
|
43
|
-
if (panel.visible)
|
|
44
|
-
panel.appendLine(line);
|
|
45
|
-
}
|
|
46
|
-
function drainRenderer() {
|
|
47
|
-
if (!renderer)
|
|
48
|
-
return;
|
|
49
|
-
for (const line of renderer.drainLines()) {
|
|
50
|
-
appendLine(line);
|
|
51
|
-
}
|
|
52
|
-
}
|
|
53
|
-
function flushRenderer() {
|
|
54
|
-
if (!renderer)
|
|
55
|
-
return;
|
|
56
|
-
renderer.flush();
|
|
57
|
-
drainRenderer();
|
|
58
|
-
}
|
|
59
|
-
function startAssistantMessage() {
|
|
60
|
-
flushRenderer();
|
|
61
|
-
currentAssistantMsg = { role: "assistant", lines: [] };
|
|
62
|
-
messages.push(currentAssistantMsg);
|
|
63
|
-
renderer = new MarkdownRenderer(getContentWidth());
|
|
64
|
-
}
|
|
65
|
-
function finalizeAssistantMessage() {
|
|
66
|
-
flushRenderer();
|
|
67
|
-
renderer = null;
|
|
68
|
-
currentAssistantMsg = null;
|
|
69
|
-
}
|
|
70
|
-
// ── Panel lifecycle ────────────────────────────────────────
|
|
71
|
-
panel.handlers.advise("panel:submit", (_next, query) => {
|
|
72
|
-
messages.push({
|
|
73
|
-
role: "user",
|
|
74
|
-
lines: [`${p.accent}${p.bold}❯${p.reset} ${query}`],
|
|
75
|
-
});
|
|
76
|
-
panel.setActive();
|
|
77
|
-
rebuildContent();
|
|
78
|
-
startAssistantMessage();
|
|
79
|
-
bus.emit("agent:submit", { query });
|
|
80
|
-
});
|
|
81
|
-
panel.handlers.advise("panel:show", (_next) => {
|
|
82
|
-
rebuildContent();
|
|
83
|
-
if (renderer)
|
|
84
|
-
drainRenderer();
|
|
85
|
-
});
|
|
86
|
-
// ── Stream agent response into panel ───────────────────────
|
|
87
|
-
bus.on("agent:response-chunk", (e) => {
|
|
88
|
-
if (!panel.active)
|
|
89
|
-
return;
|
|
90
|
-
if (!currentAssistantMsg)
|
|
91
|
-
startAssistantMessage();
|
|
92
|
-
for (const block of e.blocks) {
|
|
93
|
-
if (block.type === "text" && block.text) {
|
|
94
|
-
renderer.push(block.text);
|
|
95
|
-
drainRenderer();
|
|
96
|
-
}
|
|
97
|
-
else if (block.type === "code-block") {
|
|
98
|
-
flushRenderer();
|
|
99
|
-
// Reuse the shared code-block handler
|
|
100
|
-
call("render:code-block", block.language, block.code, getContentWidth());
|
|
101
|
-
}
|
|
102
|
-
}
|
|
103
|
-
});
|
|
104
|
-
// Capture lines emitted by render:code-block into the overlay
|
|
105
|
-
advise("render:code-block", (next, language, code, width) => {
|
|
106
|
-
if (!panel.active)
|
|
107
|
-
return next(language, code, width);
|
|
108
|
-
// Render code block as indented dim lines for the overlay
|
|
109
|
-
const label = language ? `${p.dim}${language}${p.reset}` : "";
|
|
110
|
-
if (label)
|
|
111
|
-
appendLine(label);
|
|
112
|
-
for (const codeLine of code.split("\n")) {
|
|
113
|
-
appendLine(` ${p.dim}${codeLine}${p.reset}`);
|
|
114
|
-
}
|
|
115
|
-
});
|
|
116
|
-
bus.on("agent:tool-started", (e) => {
|
|
117
|
-
if (!panel.active)
|
|
118
|
-
return;
|
|
119
|
-
if (!currentAssistantMsg)
|
|
120
|
-
startAssistantMessage();
|
|
121
|
-
flushRenderer();
|
|
122
|
-
toolStartTime = Date.now();
|
|
123
|
-
const lines = renderToolCall({
|
|
124
|
-
title: e.title,
|
|
125
|
-
kind: e.kind,
|
|
126
|
-
icon: e.icon,
|
|
127
|
-
locations: e.locations,
|
|
128
|
-
rawInput: e.rawInput,
|
|
129
|
-
displayDetail: e.displayDetail,
|
|
130
|
-
}, getContentWidth());
|
|
131
|
-
for (const line of lines)
|
|
132
|
-
appendLine(line);
|
|
133
|
-
});
|
|
134
|
-
bus.on("agent:tool-completed", (e) => {
|
|
135
|
-
if (!panel.active)
|
|
136
|
-
return;
|
|
137
|
-
const elapsed = toolStartTime ? formatElapsed(Date.now() - toolStartTime) : "";
|
|
138
|
-
const mark = call("tui:render-tool-complete", e.exitCode, elapsed, undefined);
|
|
139
|
-
appendLine(` ${mark}`);
|
|
140
|
-
});
|
|
141
|
-
bus.on("agent:processing-done", () => {
|
|
142
|
-
if (!panel.active)
|
|
143
|
-
return;
|
|
144
|
-
finalizeAssistantMessage();
|
|
145
|
-
panel.setDone();
|
|
146
|
-
});
|
|
147
|
-
}
|
|
@@ -1,184 +0,0 @@
|
|
|
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
|
-
}
|
|
File without changes
|