agent-sh 0.2.0 → 0.3.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 +21 -0
- package/dist/acp-client.d.ts +24 -0
- package/dist/acp-client.js +144 -38
- package/dist/context-manager.d.ts +5 -3
- package/dist/context-manager.js +62 -31
- package/dist/event-bus.d.ts +4 -0
- package/dist/extension-loader.js +3 -14
- package/dist/extensions/shell-exec.js +27 -22
- package/dist/extensions/tui-renderer.d.ts +1 -1
- package/dist/extensions/tui-renderer.js +125 -26
- package/dist/index.js +184 -37
- package/dist/input-handler.d.ts +10 -0
- package/dist/input-handler.js +169 -10
- package/dist/mcp-server.js +37 -8
- package/dist/settings.d.ts +33 -0
- package/dist/settings.js +43 -0
- package/dist/shell.d.ts +1 -0
- package/dist/shell.js +44 -4
- package/dist/types.d.ts +2 -0
- package/dist/utils/ansi.d.ts +4 -1
- package/dist/utils/ansi.js +60 -2
- package/dist/utils/line-editor.d.ts +21 -1
- package/dist/utils/line-editor.js +193 -99
- package/dist/utils/markdown.js +4 -4
- package/dist/utils/tool-display.d.ts +2 -0
- package/dist/utils/tool-display.js +48 -20
- package/examples/pi-agent-sh.ts +166 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -77,6 +77,7 @@ See the [Usage Guide](docs/usage.md) for all options, model configuration, and e
|
|
|
77
77
|
| `Ctrl-C` | Standard signal to shell, or cancels active agent response |
|
|
78
78
|
| `Ctrl-O` | Expand/collapse truncated diff preview |
|
|
79
79
|
| `Ctrl-T` | Toggle thinking/reasoning text display |
|
|
80
|
+
| `Shift-Tab` | Cycle thinking level (off → minimal → low → medium → high → xhigh) |
|
|
80
81
|
| `Escape` | Exit agent input mode (when typing after `>`) |
|
|
81
82
|
|
|
82
83
|
### Agent Input Keybindings
|
|
@@ -85,6 +86,10 @@ When typing after `>`, full readline-style keybindings are available:
|
|
|
85
86
|
|
|
86
87
|
| Key | Action |
|
|
87
88
|
|---|---|
|
|
89
|
+
| `↑` / `↓` | Browse query history (persisted across sessions) |
|
|
90
|
+
| `Shift-Enter` | Insert newline (multiline input) |
|
|
91
|
+
| `Shift-Tab` | Cycle thinking level |
|
|
92
|
+
| `Ctrl-D` | Exit agent input mode (on empty line) |
|
|
88
93
|
| `Ctrl-A` / `Home` | Move to start of line |
|
|
89
94
|
| `Ctrl-E` / `End` | Move to end of line |
|
|
90
95
|
| `Ctrl-B` / `←` | Move back one character |
|
|
@@ -96,6 +101,16 @@ When typing after `>`, full readline-style keybindings are available:
|
|
|
96
101
|
| `Ctrl-W` / `Option-Backspace` | Delete word backward |
|
|
97
102
|
| `Option-D` | Delete word forward |
|
|
98
103
|
|
|
104
|
+
### Thinking Level
|
|
105
|
+
|
|
106
|
+
The agent prompt shows the current thinking level next to the model name:
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
pi (claude-3.5-sonnet) [medium] ● ❯
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
Press **Shift-Tab** in agent input mode to cycle through levels. The levels are advertised by the agent via ACP session modes — different agents may offer different options. The spinner label reflects the mode: "Thinking" when thinking is enabled, "Working" when it's off.
|
|
113
|
+
|
|
99
114
|
### Slash Commands
|
|
100
115
|
|
|
101
116
|
| Command | Description |
|
|
@@ -106,6 +121,12 @@ When typing after `>`, full readline-style keybindings are available:
|
|
|
106
121
|
| `/compact` | Ask agent to summarize the conversation |
|
|
107
122
|
| `/quit` | Exit agent-sh |
|
|
108
123
|
|
|
124
|
+
## Configuration
|
|
125
|
+
|
|
126
|
+
agent-sh stores settings and history in `~/.agent-sh/`. Behavior is configurable via `~/.agent-sh/settings.json` — context window size, truncation thresholds, display limits, and more. All fields are optional with sensible defaults.
|
|
127
|
+
|
|
128
|
+
See the [Usage Guide](docs/usage.md#configuration) for the full settings reference.
|
|
129
|
+
|
|
109
130
|
## Development
|
|
110
131
|
|
|
111
132
|
```bash
|
package/dist/acp-client.d.ts
CHANGED
|
@@ -16,8 +16,11 @@ export declare class AcpClient {
|
|
|
16
16
|
private terminalCounter;
|
|
17
17
|
private fileWatcher;
|
|
18
18
|
private pendingToolCalls;
|
|
19
|
+
private autoCancelled;
|
|
19
20
|
private pendingToolCounter;
|
|
20
21
|
private agentInfo;
|
|
22
|
+
private modes;
|
|
23
|
+
private currentModeId;
|
|
21
24
|
constructor(opts: {
|
|
22
25
|
bus: EventBus;
|
|
23
26
|
contextManager: ContextManager;
|
|
@@ -28,6 +31,12 @@ export declare class AcpClient {
|
|
|
28
31
|
* Send a user query to the agent.
|
|
29
32
|
*/
|
|
30
33
|
sendPrompt(query: string): Promise<void>;
|
|
34
|
+
/**
|
|
35
|
+
* Silently cancel the prompt after a shell tool completes.
|
|
36
|
+
* Unlike user-initiated cancel(), this doesn't show "(cancelled)" —
|
|
37
|
+
* the tool already ran, we just skip the unnecessary LLM follow-up.
|
|
38
|
+
*/
|
|
39
|
+
private autoCancel;
|
|
31
40
|
/**
|
|
32
41
|
* Cancel the current prompt and force-recover shell mode.
|
|
33
42
|
*/
|
|
@@ -48,10 +57,25 @@ export declare class AcpClient {
|
|
|
48
57
|
version: string;
|
|
49
58
|
} | null;
|
|
50
59
|
getModel(): string | undefined;
|
|
60
|
+
/**
|
|
61
|
+
* Get the current mode (e.g. thinking level).
|
|
62
|
+
*/
|
|
63
|
+
getCurrentMode(): {
|
|
64
|
+
id: string;
|
|
65
|
+
name: string;
|
|
66
|
+
} | null;
|
|
51
67
|
/**
|
|
52
68
|
* Check if agent is connected.
|
|
53
69
|
*/
|
|
54
70
|
isConnected(): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Parse modes from a session response and notify listeners.
|
|
73
|
+
*/
|
|
74
|
+
private updateModes;
|
|
75
|
+
/**
|
|
76
|
+
* Cycle to the next session mode.
|
|
77
|
+
*/
|
|
78
|
+
private cycleMode;
|
|
55
79
|
private log;
|
|
56
80
|
/**
|
|
57
81
|
* Create the Client handler that responds to agent requests.
|
package/dist/acp-client.js
CHANGED
|
@@ -21,9 +21,12 @@ export class AcpClient {
|
|
|
21
21
|
terminalDonePromises = new Map();
|
|
22
22
|
terminalCounter = 0;
|
|
23
23
|
fileWatcher;
|
|
24
|
-
pendingToolCalls = new Map();
|
|
24
|
+
pendingToolCalls = new Map(); // toolCallId → title
|
|
25
|
+
autoCancelled = false;
|
|
25
26
|
pendingToolCounter = 0;
|
|
26
27
|
agentInfo = null;
|
|
28
|
+
modes = [];
|
|
29
|
+
currentModeId = null;
|
|
27
30
|
constructor(opts) {
|
|
28
31
|
this.bus = opts.bus;
|
|
29
32
|
this.contextManager = opts.contextManager;
|
|
@@ -33,8 +36,16 @@ export class AcpClient {
|
|
|
33
36
|
async start() {
|
|
34
37
|
this.log(`Starting agent: ${this.config.agentCommand} ${this.config.agentArgs.join(" ")}`);
|
|
35
38
|
// Spawn the agent subprocess with the user's full shell environment
|
|
36
|
-
// (includes vars from .zshrc/.bashrc that process.env may not have)
|
|
37
|
-
|
|
39
|
+
// (includes vars from .zshrc/.bashrc that process.env may not have).
|
|
40
|
+
// Merge in any runtime env vars set by extensions (e.g. AGENT_SH_SOCKET)
|
|
41
|
+
// that weren't present when shellEnv was captured at startup.
|
|
42
|
+
const baseEnv = this.config.shellEnv ?? process.env;
|
|
43
|
+
const agentEnv = { ...baseEnv };
|
|
44
|
+
for (const [k, v] of Object.entries(process.env)) {
|
|
45
|
+
if (v !== undefined && !(k in agentEnv)) {
|
|
46
|
+
agentEnv[k] = v;
|
|
47
|
+
}
|
|
48
|
+
}
|
|
38
49
|
this.agentProcess = spawn(this.config.agentCommand, this.config.agentArgs, {
|
|
39
50
|
stdio: ["pipe", "pipe", process.env.DEBUG ? "inherit" : "ignore"],
|
|
40
51
|
env: agentEnv,
|
|
@@ -67,19 +78,23 @@ export class AcpClient {
|
|
|
67
78
|
this.log("Creating ACP connection");
|
|
68
79
|
// Create the client-side connection, providing our Client handler
|
|
69
80
|
this.connection = new acp.ClientSideConnection((_agent) => this.createClientHandler(), stream);
|
|
70
|
-
// Initialize the connection
|
|
81
|
+
// Initialize the connection with timeout
|
|
71
82
|
this.log("Sending initialize request");
|
|
72
|
-
const
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
83
|
+
const initTimeoutMs = 30000; // 30 seconds
|
|
84
|
+
const initResponse = await Promise.race([
|
|
85
|
+
this.connection.initialize({
|
|
86
|
+
protocolVersion: acp.PROTOCOL_VERSION,
|
|
87
|
+
clientInfo: { name: "agent-sh", version: "0.1.0" },
|
|
88
|
+
clientCapabilities: {
|
|
89
|
+
terminal: true,
|
|
90
|
+
fs: {
|
|
91
|
+
readTextFile: true,
|
|
92
|
+
writeTextFile: true,
|
|
93
|
+
},
|
|
80
94
|
},
|
|
81
|
-
},
|
|
82
|
-
|
|
95
|
+
}),
|
|
96
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error(`Initialize timeout after ${initTimeoutMs}ms`)), initTimeoutMs)),
|
|
97
|
+
]);
|
|
83
98
|
this.log("Initialize successful");
|
|
84
99
|
// Store agent info for display
|
|
85
100
|
if (initResponse.agentInfo) {
|
|
@@ -96,12 +111,20 @@ export class AcpClient {
|
|
|
96
111
|
cwd,
|
|
97
112
|
mcpServers: [],
|
|
98
113
|
});
|
|
99
|
-
const
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
114
|
+
const sessionTimeoutMs = 30000; // 30 seconds
|
|
115
|
+
const sessionResponse = await Promise.race([
|
|
116
|
+
this.connection.newSession({
|
|
117
|
+
cwd: sessionConfig.cwd,
|
|
118
|
+
mcpServers: sessionConfig.mcpServers,
|
|
119
|
+
}),
|
|
120
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error(`newSession timeout after ${sessionTimeoutMs}ms`)), sessionTimeoutMs)),
|
|
121
|
+
]);
|
|
103
122
|
this.sessionId = sessionResponse.sessionId;
|
|
104
123
|
this.log(`Session created: ${this.sessionId}`);
|
|
124
|
+
// Parse session modes (thinking level, etc.)
|
|
125
|
+
this.updateModes(sessionResponse);
|
|
126
|
+
// Listen for mode cycle requests from input handler
|
|
127
|
+
this.bus.on("config:cycle", () => this.cycleMode());
|
|
105
128
|
}
|
|
106
129
|
/**
|
|
107
130
|
* Send a user query to the agent.
|
|
@@ -115,6 +138,7 @@ export class AcpClient {
|
|
|
115
138
|
this.bus.emit("agent:processing-start", {});
|
|
116
139
|
await this.fileWatcher.snapshot();
|
|
117
140
|
this.currentResponseText = "";
|
|
141
|
+
this.autoCancelled = false;
|
|
118
142
|
let cancelled = false;
|
|
119
143
|
// Emit agent query event (TUI renders echo+spinner, ContextManager records it)
|
|
120
144
|
this.bus.emit("agent:query", { query });
|
|
@@ -122,19 +146,25 @@ export class AcpClient {
|
|
|
122
146
|
const contextBlock = this.contextManager.getContext();
|
|
123
147
|
try {
|
|
124
148
|
this.log("sending prompt...");
|
|
125
|
-
const
|
|
126
|
-
|
|
127
|
-
prompt
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
149
|
+
const promptTimeoutMs = 300000; // 5 minutes timeout for LLM response
|
|
150
|
+
const response = await Promise.race([
|
|
151
|
+
this.connection.prompt({
|
|
152
|
+
sessionId: this.sessionId,
|
|
153
|
+
prompt: [
|
|
154
|
+
{
|
|
155
|
+
type: "text",
|
|
156
|
+
text: contextBlock + "\n" + query,
|
|
157
|
+
},
|
|
158
|
+
],
|
|
159
|
+
}),
|
|
160
|
+
new Promise((_, reject) => setTimeout(() => reject(new Error(`Prompt timeout after ${promptTimeoutMs}ms`)), promptTimeoutMs)),
|
|
161
|
+
]);
|
|
134
162
|
this.log(`prompt resolved: stopReason=${response.stopReason}`);
|
|
135
163
|
if (response.stopReason === "cancelled") {
|
|
136
164
|
cancelled = true;
|
|
137
|
-
this.
|
|
165
|
+
if (!this.autoCancelled) {
|
|
166
|
+
this.bus.emit("agent:cancelled", {});
|
|
167
|
+
}
|
|
138
168
|
}
|
|
139
169
|
}
|
|
140
170
|
catch (err) {
|
|
@@ -160,6 +190,18 @@ export class AcpClient {
|
|
|
160
190
|
this.promptInProgress = false;
|
|
161
191
|
}
|
|
162
192
|
}
|
|
193
|
+
/**
|
|
194
|
+
* Silently cancel the prompt after a shell tool completes.
|
|
195
|
+
* Unlike user-initiated cancel(), this doesn't show "(cancelled)" —
|
|
196
|
+
* the tool already ran, we just skip the unnecessary LLM follow-up.
|
|
197
|
+
*/
|
|
198
|
+
autoCancel() {
|
|
199
|
+
if (!this.connection || !this.sessionId || !this.promptInProgress)
|
|
200
|
+
return;
|
|
201
|
+
this.log("auto-cancel: shell tool completed, skipping LLM follow-up");
|
|
202
|
+
this.autoCancelled = true;
|
|
203
|
+
this.connection.cancel({ sessionId: this.sessionId }).catch(() => { });
|
|
204
|
+
}
|
|
163
205
|
/**
|
|
164
206
|
* Cancel the current prompt and force-recover shell mode.
|
|
165
207
|
*/
|
|
@@ -202,6 +244,7 @@ export class AcpClient {
|
|
|
202
244
|
this.sessionId = sessionResponse.sessionId;
|
|
203
245
|
this.lastResponseText = "";
|
|
204
246
|
this.currentResponseText = "";
|
|
247
|
+
this.updateModes(sessionResponse);
|
|
205
248
|
}
|
|
206
249
|
/**
|
|
207
250
|
* Get the text of the last agent response (for /copy).
|
|
@@ -218,6 +261,14 @@ export class AcpClient {
|
|
|
218
261
|
getModel() {
|
|
219
262
|
return this.config.model;
|
|
220
263
|
}
|
|
264
|
+
/**
|
|
265
|
+
* Get the current mode (e.g. thinking level).
|
|
266
|
+
*/
|
|
267
|
+
getCurrentMode() {
|
|
268
|
+
if (!this.currentModeId)
|
|
269
|
+
return null;
|
|
270
|
+
return this.modes.find((m) => m.id === this.currentModeId) ?? null;
|
|
271
|
+
}
|
|
221
272
|
/**
|
|
222
273
|
* Check if agent is connected.
|
|
223
274
|
*/
|
|
@@ -226,6 +277,45 @@ export class AcpClient {
|
|
|
226
277
|
// Session ID may not be set yet if we're still initializing
|
|
227
278
|
return this.connection !== null && this.agentInfo !== null;
|
|
228
279
|
}
|
|
280
|
+
/**
|
|
281
|
+
* Parse modes from a session response and notify listeners.
|
|
282
|
+
*/
|
|
283
|
+
updateModes(response) {
|
|
284
|
+
const modes = response.modes;
|
|
285
|
+
if (!modes)
|
|
286
|
+
return;
|
|
287
|
+
if (modes.availableModes) {
|
|
288
|
+
this.modes = modes.availableModes.map((m) => ({
|
|
289
|
+
id: m.id,
|
|
290
|
+
name: m.name || m.id,
|
|
291
|
+
}));
|
|
292
|
+
}
|
|
293
|
+
if (modes.currentModeId) {
|
|
294
|
+
this.currentModeId = modes.currentModeId;
|
|
295
|
+
}
|
|
296
|
+
this.bus.emit("config:changed", {});
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
* Cycle to the next session mode.
|
|
300
|
+
*/
|
|
301
|
+
async cycleMode() {
|
|
302
|
+
if (!this.connection || !this.sessionId || this.modes.length === 0)
|
|
303
|
+
return;
|
|
304
|
+
const currentIdx = this.modes.findIndex((m) => m.id === this.currentModeId);
|
|
305
|
+
const nextIdx = (currentIdx + 1) % this.modes.length;
|
|
306
|
+
const nextMode = this.modes[nextIdx];
|
|
307
|
+
try {
|
|
308
|
+
await this.connection.setSessionMode({
|
|
309
|
+
sessionId: this.sessionId,
|
|
310
|
+
modeId: nextMode.id,
|
|
311
|
+
});
|
|
312
|
+
this.currentModeId = nextMode.id;
|
|
313
|
+
this.bus.emit("config:changed", {});
|
|
314
|
+
}
|
|
315
|
+
catch (err) {
|
|
316
|
+
this.log(`Failed to set mode: ${err}`);
|
|
317
|
+
}
|
|
318
|
+
}
|
|
229
319
|
log(msg) {
|
|
230
320
|
if (process.env.DEBUG) {
|
|
231
321
|
process.stderr.write(`[agent-sh] ${msg}\n`);
|
|
@@ -293,7 +383,7 @@ export class AcpClient {
|
|
|
293
383
|
}
|
|
294
384
|
case "tool_call": {
|
|
295
385
|
const toolId = update.toolCallId || `tool-${this.pendingToolCounter++}`;
|
|
296
|
-
this.pendingToolCalls.set(toolId,
|
|
386
|
+
this.pendingToolCalls.set(toolId, update.title ?? "");
|
|
297
387
|
this.bus.emit("agent:tool-started", {
|
|
298
388
|
title: update.title,
|
|
299
389
|
toolCallId: toolId,
|
|
@@ -304,16 +394,19 @@ export class AcpClient {
|
|
|
304
394
|
break;
|
|
305
395
|
}
|
|
306
396
|
case "tool_call_update": {
|
|
307
|
-
|
|
308
|
-
|
|
309
|
-
|
|
310
|
-
|
|
311
|
-
|
|
397
|
+
const toolId = update.toolCallId;
|
|
398
|
+
const toolTitle = toolId ? this.pendingToolCalls.get(toolId) : undefined;
|
|
399
|
+
if (update.status === "completed" || update.status === "failed") {
|
|
400
|
+
// Show content only on final status. Skip tools whose output the
|
|
401
|
+
// user already sees (user_shell → PTY) or is agent-only (shell_recall).
|
|
402
|
+
const skipOutput = toolTitle === "user_shell" || toolTitle === "shell_recall";
|
|
403
|
+
if (!skipOutput && update.content && Array.isArray(update.content)) {
|
|
404
|
+
for (const block of update.content) {
|
|
405
|
+
if (block.type === "content" && block.content?.type === "text" && block.content.text) {
|
|
406
|
+
this.bus.emit("agent:tool-output-chunk", { chunk: block.content.text });
|
|
407
|
+
}
|
|
312
408
|
}
|
|
313
409
|
}
|
|
314
|
-
}
|
|
315
|
-
if (update.status === "completed" || update.status === "failed") {
|
|
316
|
-
const toolId = update.toolCallId;
|
|
317
410
|
const exitCode = update.status === "completed" ? 0 : 1;
|
|
318
411
|
if (toolId && this.pendingToolCalls.has(toolId)) {
|
|
319
412
|
this.pendingToolCalls.delete(toolId);
|
|
@@ -326,11 +419,24 @@ export class AcpClient {
|
|
|
326
419
|
else if (!toolId) {
|
|
327
420
|
this.bus.emit("agent:tool-completed", { exitCode, rawOutput: update.rawOutput });
|
|
328
421
|
}
|
|
422
|
+
// Auto-cancel after shell tools complete — the command already
|
|
423
|
+
// ran in the user's PTY, no need for a second LLM round trip.
|
|
424
|
+
// The result is captured in shell context / shell_recall.
|
|
425
|
+
if (toolTitle === "user_shell" && update.status === "completed") {
|
|
426
|
+
this.autoCancel();
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
break;
|
|
430
|
+
}
|
|
431
|
+
case "current_mode_update": {
|
|
432
|
+
const modeId = update.currentModeId;
|
|
433
|
+
if (modeId) {
|
|
434
|
+
this.currentModeId = modeId;
|
|
435
|
+
this.bus.emit("config:changed", {});
|
|
329
436
|
}
|
|
330
437
|
break;
|
|
331
438
|
}
|
|
332
439
|
default:
|
|
333
|
-
// Ignore other update types for now
|
|
334
440
|
break;
|
|
335
441
|
}
|
|
336
442
|
}
|
|
@@ -5,6 +5,8 @@ export declare class ContextManager {
|
|
|
5
5
|
private currentCwd;
|
|
6
6
|
private sessionStart;
|
|
7
7
|
private pendingToolCalls;
|
|
8
|
+
private firstPrompt;
|
|
9
|
+
private agentShellActive;
|
|
8
10
|
constructor(bus: EventBus);
|
|
9
11
|
getCwd(): string;
|
|
10
12
|
/**
|
|
@@ -17,9 +19,10 @@ export declare class ContextManager {
|
|
|
17
19
|
*/
|
|
18
20
|
search(query: string): string;
|
|
19
21
|
/**
|
|
20
|
-
* Return
|
|
22
|
+
* Return content for specific exchange IDs.
|
|
23
|
+
* Optional start/end restrict to a line range (1-indexed).
|
|
21
24
|
*/
|
|
22
|
-
expand(ids: number[]): string;
|
|
25
|
+
expand(ids: number[], start?: number, end?: number): string;
|
|
23
26
|
/**
|
|
24
27
|
* One-line summaries of last N exchanges.
|
|
25
28
|
*/
|
|
@@ -37,7 +40,6 @@ export declare class ContextManager {
|
|
|
37
40
|
private formatContext;
|
|
38
41
|
private addExchange;
|
|
39
42
|
private formatExchangeTruncated;
|
|
40
|
-
private truncateForRecall;
|
|
41
43
|
private formatExchangeFull;
|
|
42
44
|
private exchangeOneLiner;
|
|
43
45
|
private exchangeSearchText;
|
package/dist/context-manager.js
CHANGED
|
@@ -1,21 +1,18 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
// Truncation thresholds (in lines)
|
|
4
|
-
const SHELL_TRUNCATE_THRESHOLD = 30;
|
|
5
|
-
const SHELL_HEAD_LINES = 10;
|
|
6
|
-
const SHELL_TAIL_LINES = 10;
|
|
1
|
+
import { getSettings } from "./settings.js";
|
|
2
|
+
// Non-configurable thresholds (agent response and tool output follow shell settings)
|
|
7
3
|
const AGENT_RESPONSE_TRUNCATE_THRESHOLD = 20;
|
|
8
4
|
const AGENT_RESPONSE_HEAD_LINES = 15;
|
|
9
5
|
const TOOL_TRUNCATE_THRESHOLD = 20;
|
|
10
6
|
const TOOL_HEAD_LINES = 5;
|
|
11
7
|
const TOOL_TAIL_LINES = 5;
|
|
12
|
-
const RECALL_EXPAND_MAX_LINES = 500;
|
|
13
8
|
export class ContextManager {
|
|
14
9
|
exchanges = [];
|
|
15
10
|
nextId = 1;
|
|
16
11
|
currentCwd;
|
|
17
12
|
sessionStart;
|
|
18
13
|
pendingToolCalls = [];
|
|
14
|
+
firstPrompt = true;
|
|
15
|
+
agentShellActive = false; // true while user_shell command is executing
|
|
19
16
|
constructor(bus) {
|
|
20
17
|
this.currentCwd = process.cwd();
|
|
21
18
|
this.sessionStart = Date.now();
|
|
@@ -30,11 +27,15 @@ export class ContextManager {
|
|
|
30
27
|
exitCode: e.exitCode,
|
|
31
28
|
outputLines: lines.length,
|
|
32
29
|
outputBytes: e.output.length,
|
|
30
|
+
source: this.agentShellActive ? "agent" : "user",
|
|
33
31
|
});
|
|
34
32
|
});
|
|
35
33
|
bus.on("shell:cwd-change", (e) => {
|
|
36
34
|
this.currentCwd = e.cwd;
|
|
37
35
|
});
|
|
36
|
+
// Track agent-initiated shell commands (user_shell tool)
|
|
37
|
+
bus.on("shell:agent-exec-start", () => { this.agentShellActive = true; });
|
|
38
|
+
bus.on("shell:agent-exec-done", () => { this.agentShellActive = false; });
|
|
38
39
|
// ── Subscribe to agent events ──
|
|
39
40
|
bus.on("agent:query", (e) => {
|
|
40
41
|
this.pendingToolCalls = [];
|
|
@@ -85,7 +86,8 @@ export class ContextManager {
|
|
|
85
86
|
* Build the <shell_context> block for the agent prompt.
|
|
86
87
|
* Pipeline: window → truncate → format
|
|
87
88
|
*/
|
|
88
|
-
getContext(budget
|
|
89
|
+
getContext(budget) {
|
|
90
|
+
budget ??= getSettings().contextBudget;
|
|
89
91
|
let exchanges = this.applyWindow(this.exchanges);
|
|
90
92
|
exchanges = this.applyTruncation(exchanges, budget);
|
|
91
93
|
return this.formatContext(exchanges);
|
|
@@ -141,9 +143,10 @@ export class ContextManager {
|
|
|
141
143
|
return parts.join("\n");
|
|
142
144
|
}
|
|
143
145
|
/**
|
|
144
|
-
* Return
|
|
146
|
+
* Return content for specific exchange IDs.
|
|
147
|
+
* Optional start/end restrict to a line range (1-indexed).
|
|
145
148
|
*/
|
|
146
|
-
expand(ids) {
|
|
149
|
+
expand(ids, start, end) {
|
|
147
150
|
const results = [];
|
|
148
151
|
for (const id of ids) {
|
|
149
152
|
const ex = this.exchanges.find((e) => e.id === id);
|
|
@@ -151,7 +154,25 @@ export class ContextManager {
|
|
|
151
154
|
results.push(`#${id}: not found`);
|
|
152
155
|
continue;
|
|
153
156
|
}
|
|
154
|
-
|
|
157
|
+
const text = this.formatExchangeFull(ex);
|
|
158
|
+
const lines = text.split("\n");
|
|
159
|
+
const total = lines.length;
|
|
160
|
+
if (start != null || end != null) {
|
|
161
|
+
// Line range requested
|
|
162
|
+
const s = Math.max(0, (start ?? 1) - 1);
|
|
163
|
+
const e = end ?? total;
|
|
164
|
+
results.push(lines.slice(s, e).join("\n") +
|
|
165
|
+
`\n[showing lines ${s + 1}-${Math.min(e, total)} of ${total}]`);
|
|
166
|
+
}
|
|
167
|
+
else if (total > getSettings().recallExpandMaxLines) {
|
|
168
|
+
// Too large — tell the agent to narrow down
|
|
169
|
+
results.push(`#${ex.id}: output is ${total} lines, too large to expand fully. ` +
|
|
170
|
+
`Use start/end params to select a line range (e.g. start=1, end=50), ` +
|
|
171
|
+
`or use search with a regex to find specific content.`);
|
|
172
|
+
}
|
|
173
|
+
else {
|
|
174
|
+
results.push(text);
|
|
175
|
+
}
|
|
155
176
|
}
|
|
156
177
|
return results.join("\n\n");
|
|
157
178
|
}
|
|
@@ -205,10 +226,12 @@ export class ContextManager {
|
|
|
205
226
|
clear() {
|
|
206
227
|
this.exchanges = [];
|
|
207
228
|
this.pendingToolCalls = [];
|
|
229
|
+
this.firstPrompt = true;
|
|
208
230
|
// Don't reset nextId — IDs should be globally unique within a session
|
|
209
231
|
}
|
|
210
232
|
// ── Pipeline stages ───────────────────────────────────────────
|
|
211
|
-
applyWindow(exchanges, windowSize
|
|
233
|
+
applyWindow(exchanges, windowSize) {
|
|
234
|
+
windowSize ??= getSettings().contextWindowSize;
|
|
212
235
|
return exchanges.slice(-windowSize);
|
|
213
236
|
}
|
|
214
237
|
applyTruncation(exchanges, budget) {
|
|
@@ -217,7 +240,8 @@ export class ContextManager {
|
|
|
217
240
|
// Pass 1: per-type truncation
|
|
218
241
|
for (const ex of result) {
|
|
219
242
|
if (ex.type === "shell_command") {
|
|
220
|
-
|
|
243
|
+
const s = getSettings();
|
|
244
|
+
ex.output = truncateOutput(ex.output, s.shellTruncateThreshold, s.shellHeadLines, s.shellTailLines, ex.id);
|
|
221
245
|
}
|
|
222
246
|
else if (ex.type === "agent_response") {
|
|
223
247
|
ex.response = truncateHead(ex.response, AGENT_RESPONSE_TRUNCATE_THRESHOLD, AGENT_RESPONSE_HEAD_LINES, ex.id);
|
|
@@ -248,9 +272,22 @@ export class ContextManager {
|
|
|
248
272
|
const elapsed = Math.round((Date.now() - this.sessionStart) / 60000);
|
|
249
273
|
const totalCount = this.exchanges.length;
|
|
250
274
|
let out = "<shell_context>\n";
|
|
275
|
+
if (this.firstPrompt) {
|
|
276
|
+
out += `You are an AI assistant living inside agent-sh, a shell-first terminal.\n`;
|
|
277
|
+
out += `The user interacts with a real shell (PTY) and sends you queries inline. You are there to help them with their tasks.\n`;
|
|
278
|
+
out += `\n`;
|
|
279
|
+
out += `IMPORTANT tool usage rules:\n`;
|
|
280
|
+
out += `- user_shell runs commands in the user's live shell (PTY). The user sees output directly — no summary needed.\n`;
|
|
281
|
+
out += `- Your internal tools (bash, read, write, ls, etc.) run in an isolated subprocess. The user CANNOT see their output.\n`;
|
|
282
|
+
out += `- When the user asks to see, list, view, or display anything, ALWAYS use user_shell. NEVER use internal tools like ls/read/bash for display — the user won't see it.\n`;
|
|
283
|
+
out += `- Only use internal tools when YOU need to reason about content silently (e.g. reading a file to answer a question about it).\n`;
|
|
284
|
+
out += `- After a user_shell command, the user already saw the output. Do NOT repeat or summarize it.\n`;
|
|
285
|
+
out += `- You can browse or search session history with shell_recall.\n`;
|
|
286
|
+
out += `\n`;
|
|
287
|
+
this.firstPrompt = false;
|
|
288
|
+
}
|
|
251
289
|
out += `cwd: ${this.currentCwd}\n`;
|
|
252
290
|
out += `session: ${totalCount} exchanges, ${elapsed}m elapsed\n`;
|
|
253
|
-
out += `[hint: use the shell_recall tool to retrieve truncated content — search(query) or expand(ids)]\n`;
|
|
254
291
|
for (const ex of exchanges) {
|
|
255
292
|
out += "\n" + this.formatExchangeTruncated(ex);
|
|
256
293
|
}
|
|
@@ -269,7 +306,8 @@ export class ContextManager {
|
|
|
269
306
|
formatExchangeTruncated(ex) {
|
|
270
307
|
switch (ex.type) {
|
|
271
308
|
case "shell_command": {
|
|
272
|
-
|
|
309
|
+
const label = ex.source === "agent" ? "agent → shell" : "shell";
|
|
310
|
+
let s = `#${ex.id} [${label} cwd:${ex.cwd}] $ ${ex.command}\n`;
|
|
273
311
|
if (ex.output)
|
|
274
312
|
s += indent(ex.output, " ") + "\n";
|
|
275
313
|
if (ex.exitCode !== null)
|
|
@@ -299,20 +337,12 @@ export class ContextManager {
|
|
|
299
337
|
}
|
|
300
338
|
}
|
|
301
339
|
}
|
|
302
|
-
truncateForRecall(text) {
|
|
303
|
-
const lines = text.split("\n");
|
|
304
|
-
if (lines.length <= RECALL_EXPAND_MAX_LINES)
|
|
305
|
-
return text;
|
|
306
|
-
const half = RECALL_EXPAND_MAX_LINES / 2;
|
|
307
|
-
return (lines.slice(0, half).join("\n") +
|
|
308
|
-
`\n[... ${lines.length - RECALL_EXPAND_MAX_LINES} more lines ...]\n` +
|
|
309
|
-
lines.slice(-half).join("\n"));
|
|
310
|
-
}
|
|
311
340
|
formatExchangeFull(ex) {
|
|
312
341
|
switch (ex.type) {
|
|
313
342
|
case "shell_command": {
|
|
314
|
-
const
|
|
315
|
-
|
|
343
|
+
const label = ex.source === "agent" ? "agent → shell" : "shell";
|
|
344
|
+
const output = ex.output;
|
|
345
|
+
let s = `#${ex.id} [${label}] $ ${ex.command} (${ex.outputLines} lines, ${ex.outputBytes} bytes)\n`;
|
|
316
346
|
if (output)
|
|
317
347
|
s += output + "\n";
|
|
318
348
|
if (ex.exitCode !== null)
|
|
@@ -324,10 +354,9 @@ export class ContextManager {
|
|
|
324
354
|
case "agent_response":
|
|
325
355
|
return `#${ex.id} [agent]\n${ex.response}`;
|
|
326
356
|
case "tool_execution": {
|
|
327
|
-
const output = this.truncateForRecall(ex.output);
|
|
328
357
|
let s = `#${ex.id} [tool] ${ex.tool} (${ex.outputLines} lines, ${ex.outputBytes} bytes)\n`;
|
|
329
|
-
if (output)
|
|
330
|
-
s += output + "\n";
|
|
358
|
+
if (ex.output)
|
|
359
|
+
s += ex.output + "\n";
|
|
331
360
|
if (ex.exitCode !== null)
|
|
332
361
|
s += `exit ${ex.exitCode}\n`;
|
|
333
362
|
return s;
|
|
@@ -336,8 +365,10 @@ export class ContextManager {
|
|
|
336
365
|
}
|
|
337
366
|
exchangeOneLiner(ex) {
|
|
338
367
|
switch (ex.type) {
|
|
339
|
-
case "shell_command":
|
|
340
|
-
|
|
368
|
+
case "shell_command": {
|
|
369
|
+
const label = ex.source === "agent" ? "agent → shell" : "shell";
|
|
370
|
+
return `#${ex.id} ${label} [cwd:${ex.cwd}]: ${ex.command} (${ex.outputLines} total lines, exit ${ex.exitCode ?? "?"})`;
|
|
371
|
+
}
|
|
341
372
|
case "agent_query":
|
|
342
373
|
return `#${ex.id} query: ${ex.query}`;
|
|
343
374
|
case "agent_response": {
|
package/dist/event-bus.d.ts
CHANGED
|
@@ -18,6 +18,8 @@ export interface ShellEvents {
|
|
|
18
18
|
"shell:foreground-busy": {
|
|
19
19
|
busy: boolean;
|
|
20
20
|
};
|
|
21
|
+
"shell:agent-exec-start": Record<string, never>;
|
|
22
|
+
"shell:agent-exec-done": Record<string, never>;
|
|
21
23
|
"agent:submit": {
|
|
22
24
|
query: string;
|
|
23
25
|
};
|
|
@@ -114,6 +116,8 @@ export interface ShellEvents {
|
|
|
114
116
|
}[];
|
|
115
117
|
}[];
|
|
116
118
|
};
|
|
119
|
+
"config:changed": Record<string, never>;
|
|
120
|
+
"config:cycle": Record<string, never>;
|
|
117
121
|
"autocomplete:request": {
|
|
118
122
|
buffer: string;
|
|
119
123
|
items: {
|
package/dist/extension-loader.js
CHANGED
|
@@ -1,9 +1,7 @@
|
|
|
1
1
|
import * as fs from "node:fs/promises";
|
|
2
2
|
import * as path from "node:path";
|
|
3
|
-
import
|
|
4
|
-
const CONFIG_DIR = path.join(os.homedir(), ".agent-sh");
|
|
3
|
+
import { CONFIG_DIR, getSettings } from "./settings.js";
|
|
5
4
|
const EXT_DIR = path.join(CONFIG_DIR, "extensions");
|
|
6
|
-
const SETTINGS_PATH = path.join(CONFIG_DIR, "settings.json");
|
|
7
5
|
const TS_EXTS = [".ts", ".tsx", ".mts"];
|
|
8
6
|
const SCRIPT_EXTS = [".js", ".mjs", ".ts", ".tsx", ".mts"];
|
|
9
7
|
let tsRegistered = false;
|
|
@@ -19,15 +17,6 @@ async function ensureTsSupport() {
|
|
|
19
17
|
// tsx not available — TS extensions will fail with a clear error
|
|
20
18
|
}
|
|
21
19
|
}
|
|
22
|
-
async function loadSettings() {
|
|
23
|
-
try {
|
|
24
|
-
const raw = await fs.readFile(SETTINGS_PATH, "utf-8");
|
|
25
|
-
return JSON.parse(raw);
|
|
26
|
-
}
|
|
27
|
-
catch {
|
|
28
|
-
return {};
|
|
29
|
-
}
|
|
30
|
-
}
|
|
31
20
|
/**
|
|
32
21
|
* Load extensions from three sources (merged, deduplicated):
|
|
33
22
|
*
|
|
@@ -49,8 +38,8 @@ export async function loadExtensions(ctx, cliExtensions) {
|
|
|
49
38
|
specifiers.push(...cliExtensions);
|
|
50
39
|
}
|
|
51
40
|
// 2. settings.json
|
|
52
|
-
const settings =
|
|
53
|
-
if (settings.extensions) {
|
|
41
|
+
const settings = getSettings();
|
|
42
|
+
if (settings.extensions.length > 0) {
|
|
54
43
|
specifiers.push(...settings.extensions);
|
|
55
44
|
}
|
|
56
45
|
// 3. ~/.agent-sh/extensions/ directory
|