agent-sh 0.11.0 → 0.12.1
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 +13 -6
- package/dist/agent/agent-loop.js +87 -17
- package/dist/agent/conversation-state.d.ts +15 -1
- package/dist/agent/conversation-state.js +65 -14
- package/dist/agent/subagent.d.ts +8 -4
- package/dist/agent/subagent.js +45 -5
- package/dist/agent/tool-protocol.d.ts +5 -5
- package/dist/agent/tool-protocol.js +8 -8
- package/dist/event-bus.d.ts +9 -0
- package/dist/event-bus.js +1 -1
- package/dist/extensions/agent-backend.js +36 -27
- package/dist/extensions/openrouter.js +1 -6
- package/dist/extensions/tui-renderer.js +65 -19
- package/dist/index.js +10 -0
- package/dist/shell/input-handler.js +30 -0
- package/examples/extensions/wire-log.ts +35 -0
- package/package.json +9 -3
- package/examples/extensions/ash-acp-bridge/src/index.ts +0 -574
package/README.md
CHANGED
|
@@ -1,16 +1,15 @@
|
|
|
1
1
|
# agent-sh
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
A real shell with an AI agent one keystroke away.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/agent-sh)
|
|
6
6
|
[](https://github.com/guanyilun/agent-sh/blob/main/LICENSE)
|
|
7
|
-
[](https://agent-sh.dev)
|
|
8
7
|
|
|
9
8
|

|
|
10
9
|
|
|
11
|
-
|
|
10
|
+
I live in my terminal. A lot of the time I'm not coding — I'm deploying something, poking at a failing `rsync`, figuring out why `docker build` won't start, fixing a one-liner. And very often I need an AI agent to help. Spinning up a full coding agent for this stuff is overkill, and I got tired of copy-pasting errors into a chat window every time.
|
|
12
11
|
|
|
13
|
-
agent-sh
|
|
12
|
+
So I built agent-sh. Under the hood it's a normal shell on top of node-pty — your rc config, your aliases, vim and tmux all just work. But at the start of any line, type `>` and you're talking to a small agent that already sees your cwd, your last command, and its output. Nothing to set up, no project to explain.
|
|
14
13
|
|
|
15
14
|
```
|
|
16
15
|
~ $ ls -la # real shell command
|
|
@@ -20,9 +19,17 @@ agent-sh flips this. It's your shell first — full PTY, your rc config, your al
|
|
|
20
19
|
~ $ > draft a commit message # agent reads your diff and shell history
|
|
21
20
|
```
|
|
22
21
|
|
|
22
|
+
I still use Claude Code and pi for serious coding work — this doesn't replace them. But for the quick stuff in the terminal, I reach for agent-sh almost every day now. The built-in agent is lightweight and good enough for most of what I throw at it, and when it isn't, bridge extensions let you plug [Claude Code](examples/extensions/claude-code-bridge/) or [pi](examples/extensions/pi-bridge/) in as the backend.
|
|
23
|
+
|
|
23
24
|
## Quick Start
|
|
24
25
|
|
|
25
|
-
Install
|
|
26
|
+
Install the latest from GitHub (recommended — development moves faster than npm releases):
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
npm install -g github:guanyilun/agent-sh
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
Or the last published npm release:
|
|
26
33
|
|
|
27
34
|
```bash
|
|
28
35
|
npm install -g agent-sh
|
|
@@ -62,7 +69,7 @@ Tip — add a shell alias:
|
|
|
62
69
|
alias ash="agent-sh"
|
|
63
70
|
```
|
|
64
71
|
|
|
65
|
-
Requires Node.js 18+.
|
|
72
|
+
Requires Node.js 18+. Currently supports **bash** and **zsh**; other shells (fish, nushell, etc.) are not yet wired up.
|
|
66
73
|
|
|
67
74
|
## Key Features
|
|
68
75
|
|
package/dist/agent/agent-loop.js
CHANGED
|
@@ -141,10 +141,33 @@ export class AgentLoop {
|
|
|
141
141
|
// here in the ctor so late-registered modes aren't dropped.
|
|
142
142
|
onCtor("config:add-modes", ({ modes: extra }) => {
|
|
143
143
|
const providers = new Set(extra.map((m) => m.provider).filter(Boolean));
|
|
144
|
+
const prev = this.modes[this.currentModeIndex];
|
|
145
|
+
// Keep the active mode even if the re-registration drops it (persisted
|
|
146
|
+
// model missing from a refreshed catalog) — otherwise currentModeIndex
|
|
147
|
+
// slips to modes[0] and the next stream() call uses a different model
|
|
148
|
+
// mid-turn.
|
|
149
|
+
const activePreserved = prev &&
|
|
150
|
+
prev.provider &&
|
|
151
|
+
providers.has(prev.provider) &&
|
|
152
|
+
!extra.some((m) => m.model === prev.model && m.provider === prev.provider);
|
|
144
153
|
this.modes = [
|
|
145
|
-
...this.modes.filter((m) =>
|
|
154
|
+
...this.modes.filter((m) => {
|
|
155
|
+
if (activePreserved && m === prev)
|
|
156
|
+
return true;
|
|
157
|
+
return !m.provider || !providers.has(m.provider);
|
|
158
|
+
}),
|
|
146
159
|
...extra,
|
|
147
160
|
];
|
|
161
|
+
if (prev) {
|
|
162
|
+
const newIdx = this.modes.findIndex((m) => m.model === prev.model && m.provider === prev.provider);
|
|
163
|
+
if (newIdx !== -1)
|
|
164
|
+
this.currentModeIndex = newIdx;
|
|
165
|
+
}
|
|
166
|
+
if (activePreserved && prev) {
|
|
167
|
+
this.bus.emit("ui:info", {
|
|
168
|
+
message: `${prev.provider}:${prev.model} is not in the refreshed catalog — keeping it active until you /model to another.`,
|
|
169
|
+
});
|
|
170
|
+
}
|
|
148
171
|
this.bus.emit("config:changed", {});
|
|
149
172
|
});
|
|
150
173
|
// Fires before wire() too — agent-backend emits this from
|
|
@@ -516,8 +539,9 @@ export class AgentLoop {
|
|
|
516
539
|
const target = baseURL ?? provider ?? "provider";
|
|
517
540
|
return `Could not connect to ${target} (${raw}). Check that the API endpoint is reachable.`;
|
|
518
541
|
}
|
|
519
|
-
//
|
|
520
|
-
if (status === 401 ||
|
|
542
|
+
// Explicit signals only — bare "auth" hit "author" in echoed API params.
|
|
543
|
+
if (status === 401 || status === 403 ||
|
|
544
|
+
/\b(unauthorized|authentication|api[-_ ]?key|invalid[-_ ]?token)\b/i.test(raw)) {
|
|
521
545
|
return `Authentication failed for ${provider ?? "provider"} (model: ${model}). Check your API key.`;
|
|
522
546
|
}
|
|
523
547
|
// Model not found
|
|
@@ -626,6 +650,9 @@ export class AgentLoop {
|
|
|
626
650
|
*/
|
|
627
651
|
registerHandlers() {
|
|
628
652
|
const h = this.handlers;
|
|
653
|
+
// Advisable so extensions can inject fallback parsers without
|
|
654
|
+
// subclassing the protocol.
|
|
655
|
+
h.define("tool-protocol:extract-calls", (args) => this.toolProtocol.extractToolCalls(args.text, args.streamedCalls));
|
|
629
656
|
// System prompt: static identity + behavioral instructions.
|
|
630
657
|
// Extensions can use registerInstruction() for a managed section,
|
|
631
658
|
// or advise this handler directly for full control.
|
|
@@ -894,7 +921,16 @@ export class AgentLoop {
|
|
|
894
921
|
const toolCtx = this.compositor
|
|
895
922
|
? { ui: createToolUI(this.bus, this.compositor.surface("agent")) }
|
|
896
923
|
: undefined;
|
|
897
|
-
|
|
924
|
+
// Surface thrown errors as tool results so the agent can self-correct
|
|
925
|
+
// instead of the throw killing the whole turn.
|
|
926
|
+
let result;
|
|
927
|
+
try {
|
|
928
|
+
result = await tool.execute(args, onChunk, toolCtx);
|
|
929
|
+
}
|
|
930
|
+
catch (err) {
|
|
931
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
932
|
+
result = { content: message, exitCode: 1, isError: true };
|
|
933
|
+
}
|
|
898
934
|
// Invalidate read cache when a file is modified
|
|
899
935
|
if (tool.modifiesFiles && typeof args.path === "string" && !result.isError) {
|
|
900
936
|
const absPath = path.resolve(process.cwd(), args.path);
|
|
@@ -1013,13 +1049,14 @@ export class AgentLoop {
|
|
|
1013
1049
|
// tool_call → tool_result chain some providers require.
|
|
1014
1050
|
// Stream LLM response with retry
|
|
1015
1051
|
const result = await this.streamWithRetry(systemPrompt, dynamicContext, signal);
|
|
1016
|
-
const { text, toolCalls: streamedToolCalls } = result;
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1052
|
+
const { text, toolCalls: streamedToolCalls, extras } = result;
|
|
1053
|
+
const toolCalls = this.handlers.call("tool-protocol:extract-calls", {
|
|
1054
|
+
text,
|
|
1055
|
+
streamedCalls: streamedToolCalls,
|
|
1056
|
+
});
|
|
1020
1057
|
fullResponseText += text;
|
|
1021
1058
|
// Record the assistant message via protocol
|
|
1022
|
-
this.toolProtocol.recordAssistant(this.conversation, text, toolCalls);
|
|
1059
|
+
this.toolProtocol.recordAssistant(this.conversation, text, toolCalls, extras);
|
|
1023
1060
|
this.bus.emit("conversation:message-appended", {
|
|
1024
1061
|
role: "assistant",
|
|
1025
1062
|
content: text,
|
|
@@ -1408,6 +1445,11 @@ export class AgentLoop {
|
|
|
1408
1445
|
*/
|
|
1409
1446
|
async streamResponse(systemPrompt, dynamicContext, signal) {
|
|
1410
1447
|
let text = "";
|
|
1448
|
+
// reasoning_details streams as per-chunk fragments keyed by index;
|
|
1449
|
+
// merge .text per index or the provider rejects the fragmented shape.
|
|
1450
|
+
let reasoningField = null;
|
|
1451
|
+
let reasoning = "";
|
|
1452
|
+
const reasoningDetailsByIndex = new Map();
|
|
1411
1453
|
const pendingToolCalls = [];
|
|
1412
1454
|
const rawMessages = [
|
|
1413
1455
|
{ role: "system", content: systemPrompt },
|
|
@@ -1429,16 +1471,18 @@ export class AgentLoop {
|
|
|
1429
1471
|
}
|
|
1430
1472
|
// Stream filter strips tool tags from display (inline mode only)
|
|
1431
1473
|
const streamFilter = this.toolProtocol.createStreamFilter(this.toolRegistry.all().map((t) => t.name));
|
|
1432
|
-
const
|
|
1474
|
+
const requestParams = {
|
|
1433
1475
|
messages,
|
|
1434
1476
|
tools: apiTools,
|
|
1435
1477
|
model: this.currentModel,
|
|
1436
1478
|
reasoning_effort: this.shouldSendReasoningEffort() ? this.thinkingLevel : undefined,
|
|
1437
|
-
|
|
1438
|
-
|
|
1479
|
+
};
|
|
1480
|
+
this.bus.emit("llm:request", requestParams);
|
|
1481
|
+
const stream = await this.llmClient.stream({ ...requestParams, signal });
|
|
1439
1482
|
for await (const chunk of stream) {
|
|
1440
1483
|
if (signal.aborted)
|
|
1441
1484
|
break;
|
|
1485
|
+
this.bus.emit("llm:chunk", { chunk });
|
|
1442
1486
|
// Token usage (may arrive in a chunk with empty choices)
|
|
1443
1487
|
if (chunk.usage) {
|
|
1444
1488
|
const u = chunk.usage;
|
|
@@ -1470,11 +1514,29 @@ export class AgentLoop {
|
|
|
1470
1514
|
});
|
|
1471
1515
|
}
|
|
1472
1516
|
}
|
|
1473
|
-
|
|
1474
|
-
|
|
1475
|
-
|
|
1476
|
-
|
|
1477
|
-
|
|
1517
|
+
const d = delta;
|
|
1518
|
+
for (const name of ["reasoning", "reasoning_content"]) {
|
|
1519
|
+
if (typeof d?.[name] === "string" && d[name].length > 0) {
|
|
1520
|
+
reasoning += d[name];
|
|
1521
|
+
reasoningField ??= name;
|
|
1522
|
+
this.bus.emit("agent:thinking-chunk", { text: d[name] });
|
|
1523
|
+
}
|
|
1524
|
+
}
|
|
1525
|
+
if (Array.isArray(d?.reasoning_details)) {
|
|
1526
|
+
for (const x of d.reasoning_details) {
|
|
1527
|
+
const idx = typeof x?.index === "number" ? x.index : reasoningDetailsByIndex.size;
|
|
1528
|
+
const prev = reasoningDetailsByIndex.get(idx);
|
|
1529
|
+
if (!prev) {
|
|
1530
|
+
reasoningDetailsByIndex.set(idx, { ...x });
|
|
1531
|
+
}
|
|
1532
|
+
else {
|
|
1533
|
+
if (typeof x.text === "string")
|
|
1534
|
+
prev.text = (prev.text ?? "") + x.text;
|
|
1535
|
+
for (const [k, v] of Object.entries(x))
|
|
1536
|
+
if (k !== "text" && prev[k] === undefined)
|
|
1537
|
+
prev[k] = v;
|
|
1538
|
+
}
|
|
1539
|
+
}
|
|
1478
1540
|
}
|
|
1479
1541
|
// Tool calls (streamed incrementally)
|
|
1480
1542
|
if (delta?.tool_calls) {
|
|
@@ -1522,9 +1584,17 @@ export class AgentLoop {
|
|
|
1522
1584
|
tc.argumentsJson = "{}";
|
|
1523
1585
|
}
|
|
1524
1586
|
}
|
|
1587
|
+
const extras = {};
|
|
1588
|
+
if (reasoning && reasoningField)
|
|
1589
|
+
extras[reasoningField] = reasoning;
|
|
1590
|
+
if (reasoningDetailsByIndex.size > 0) {
|
|
1591
|
+
extras.reasoning_details = [...reasoningDetailsByIndex.entries()]
|
|
1592
|
+
.sort((a, b) => a[0] - b[0]).map(([, v]) => v);
|
|
1593
|
+
}
|
|
1525
1594
|
return {
|
|
1526
1595
|
text,
|
|
1527
1596
|
toolCalls: pendingToolCalls,
|
|
1597
|
+
extras: Object.keys(extras).length > 0 ? extras : undefined,
|
|
1528
1598
|
};
|
|
1529
1599
|
}
|
|
1530
1600
|
}
|
|
@@ -49,12 +49,26 @@ export declare class ConversationState {
|
|
|
49
49
|
name: string;
|
|
50
50
|
arguments: string;
|
|
51
51
|
};
|
|
52
|
-
}[]): void;
|
|
52
|
+
}[], extras?: Record<string, unknown>): void;
|
|
53
53
|
addToolResult(toolCallId: string, content: string, isError?: boolean): void;
|
|
54
54
|
/** Add tool results as a user message (for inline tool protocol). */
|
|
55
55
|
addToolResultInline(content: string): void;
|
|
56
56
|
addSystemNote(text: string): void;
|
|
57
57
|
getMessages(): ChatCompletionMessageParam[];
|
|
58
|
+
/**
|
|
59
|
+
* If a stream was interrupted mid-tool-execution, an assistant message
|
|
60
|
+
* with tool_calls can land in history without matching tool results.
|
|
61
|
+
* Strict providers (DeepSeek) 400 on this. Stub each missing result
|
|
62
|
+
* with a [cancelled] marker so the protocol stays valid.
|
|
63
|
+
*/
|
|
64
|
+
private stubDanglingToolCalls;
|
|
65
|
+
/**
|
|
66
|
+
* DeepSeek 400s if any assistant in a thinking-mode conversation is
|
|
67
|
+
* missing reasoning_content. Cross-alias here (OpenRouter streams as
|
|
68
|
+
* `reasoning`, DeepSeek input expects `reasoning_content`) and stub
|
|
69
|
+
* gaps (text-only turns, pre-fix messages) with empty string.
|
|
70
|
+
*/
|
|
71
|
+
private normalizeReasoningConsistency;
|
|
58
72
|
/**
|
|
59
73
|
* Replace the messages array wholesale — the write side for custom
|
|
60
74
|
* compaction strategies. Invalidates API token baseline since the
|
|
@@ -78,21 +78,21 @@ export class ConversationState {
|
|
|
78
78
|
this.invalidateMessagesCache();
|
|
79
79
|
this.eagerNucleateUser(text);
|
|
80
80
|
}
|
|
81
|
-
addAssistantMessage(content, toolCalls) {
|
|
81
|
+
addAssistantMessage(content, toolCalls, extras) {
|
|
82
|
+
// extras is opaque provider payload to echo back (reasoning_content,
|
|
83
|
+
// reasoning_details, etc.). Spread verbatim; shape is the stream
|
|
84
|
+
// parser's concern.
|
|
85
|
+
const base = { role: "assistant", content: content ?? (toolCalls?.length ? null : "") };
|
|
82
86
|
if (toolCalls?.length) {
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
type: "function",
|
|
89
|
-
function: tc.function,
|
|
90
|
-
})),
|
|
91
|
-
});
|
|
92
|
-
}
|
|
93
|
-
else {
|
|
94
|
-
this.messages.push({ role: "assistant", content: content ?? "" });
|
|
87
|
+
base.tool_calls = toolCalls.map((tc) => ({
|
|
88
|
+
id: tc.id,
|
|
89
|
+
type: "function",
|
|
90
|
+
function: tc.function,
|
|
91
|
+
}));
|
|
95
92
|
}
|
|
93
|
+
if (extras)
|
|
94
|
+
Object.assign(base, extras);
|
|
95
|
+
this.messages.push(base);
|
|
96
96
|
this.invalidateMessagesCache();
|
|
97
97
|
}
|
|
98
98
|
addToolResult(toolCallId, content, isError = false) {
|
|
@@ -111,7 +111,58 @@ export class ConversationState {
|
|
|
111
111
|
this.invalidateMessagesCache();
|
|
112
112
|
}
|
|
113
113
|
getMessages() {
|
|
114
|
-
return this.messages;
|
|
114
|
+
return this.normalizeReasoningConsistency(this.stubDanglingToolCalls(this.messages));
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* If a stream was interrupted mid-tool-execution, an assistant message
|
|
118
|
+
* with tool_calls can land in history without matching tool results.
|
|
119
|
+
* Strict providers (DeepSeek) 400 on this. Stub each missing result
|
|
120
|
+
* with a [cancelled] marker so the protocol stays valid.
|
|
121
|
+
*/
|
|
122
|
+
stubDanglingToolCalls(messages) {
|
|
123
|
+
const result = [];
|
|
124
|
+
let i = 0;
|
|
125
|
+
while (i < messages.length) {
|
|
126
|
+
const msg = messages[i];
|
|
127
|
+
result.push(msg);
|
|
128
|
+
i++;
|
|
129
|
+
if (msg.role !== "assistant" || !("tool_calls" in msg) || !msg.tool_calls)
|
|
130
|
+
continue;
|
|
131
|
+
const seen = new Set();
|
|
132
|
+
while (i < messages.length && messages[i].role === "tool") {
|
|
133
|
+
const t = messages[i];
|
|
134
|
+
seen.add(t.tool_call_id);
|
|
135
|
+
result.push(t);
|
|
136
|
+
i++;
|
|
137
|
+
}
|
|
138
|
+
for (const tc of msg.tool_calls) {
|
|
139
|
+
if (!seen.has(tc.id)) {
|
|
140
|
+
result.push({ role: "tool", tool_call_id: tc.id, content: "[cancelled]" });
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
/**
|
|
147
|
+
* DeepSeek 400s if any assistant in a thinking-mode conversation is
|
|
148
|
+
* missing reasoning_content. Cross-alias here (OpenRouter streams as
|
|
149
|
+
* `reasoning`, DeepSeek input expects `reasoning_content`) and stub
|
|
150
|
+
* gaps (text-only turns, pre-fix messages) with empty string.
|
|
151
|
+
*/
|
|
152
|
+
normalizeReasoningConsistency(messages) {
|
|
153
|
+
const needsNormalize = messages.some((m) => m.role === "assistant" && (m.reasoning !== undefined ||
|
|
154
|
+
m.reasoning_content !== undefined ||
|
|
155
|
+
m.reasoning_details !== undefined));
|
|
156
|
+
if (!needsNormalize)
|
|
157
|
+
return messages;
|
|
158
|
+
return messages.map((m) => {
|
|
159
|
+
if (m.role !== "assistant")
|
|
160
|
+
return m;
|
|
161
|
+
const a = m;
|
|
162
|
+
if (a.reasoning_content !== undefined)
|
|
163
|
+
return m;
|
|
164
|
+
return { ...m, reasoning_content: a.reasoning ?? "" };
|
|
165
|
+
});
|
|
115
166
|
}
|
|
116
167
|
/**
|
|
117
168
|
* Replace the messages array wholesale — the write side for custom
|
package/dist/agent/subagent.d.ts
CHANGED
|
@@ -36,10 +36,14 @@ export interface SubagentOptions {
|
|
|
36
36
|
*/
|
|
37
37
|
dynamicContext?: string;
|
|
38
38
|
/**
|
|
39
|
-
* Per-subagent token budget. When
|
|
40
|
-
*
|
|
41
|
-
*
|
|
42
|
-
*
|
|
39
|
+
* Per-subagent completion-token budget. When the cumulative
|
|
40
|
+
* completion_tokens across iterations exceeds this, the subagent
|
|
41
|
+
* terminates gracefully on the next iteration. We deliberately don't
|
|
42
|
+
* count prompt tokens: the full history is resent each iteration, so
|
|
43
|
+
* prompt-inclusive counting double-charges context and makes a budget
|
|
44
|
+
* of N exhaust after O(log N) tool calls. Completion tokens measure
|
|
45
|
+
* the work the subagent actually produces. The parent's daily budget
|
|
46
|
+
* still sees real prompt+completion via onUsage.
|
|
43
47
|
*/
|
|
44
48
|
budgetTokens?: number;
|
|
45
49
|
/**
|
package/dist/agent/subagent.js
CHANGED
|
@@ -28,13 +28,13 @@ export async function runSubagent(opts) {
|
|
|
28
28
|
break;
|
|
29
29
|
}
|
|
30
30
|
// Stream LLM response
|
|
31
|
-
const { text, toolCalls, assistantContent, assistantToolCalls, usage } = await streamOnce(llmClient, systemPrompt, conversation, apiTools, model, signal, dynamicContext);
|
|
31
|
+
const { text, toolCalls, assistantContent, assistantToolCalls, extras, usage } = await streamOnce(llmClient, systemPrompt, conversation, apiTools, model, signal, dynamicContext);
|
|
32
32
|
if (usage) {
|
|
33
|
-
tokensConsumed += usage.
|
|
33
|
+
tokensConsumed += usage.completion_tokens || 0;
|
|
34
34
|
onUsage?.(usage);
|
|
35
35
|
}
|
|
36
36
|
fullResponseText += text;
|
|
37
|
-
conversation.addAssistantMessage(assistantContent, assistantToolCalls);
|
|
37
|
+
conversation.addAssistantMessage(assistantContent, assistantToolCalls, extras);
|
|
38
38
|
// No tool calls → done
|
|
39
39
|
if (toolCalls.length === 0)
|
|
40
40
|
break;
|
|
@@ -86,7 +86,7 @@ export async function runSubagent(opts) {
|
|
|
86
86
|
}
|
|
87
87
|
}
|
|
88
88
|
if (budgetExhausted) {
|
|
89
|
-
const note = `\n\n[Subagent terminated: token budget (${budgetTokens}) exhausted after ${tokensConsumed} tokens. Returning partial progress.]`;
|
|
89
|
+
const note = `\n\n[Subagent terminated: completion-token budget (${budgetTokens}) exhausted after ${tokensConsumed} completion tokens. Returning partial progress.]`;
|
|
90
90
|
return fullResponseText + note;
|
|
91
91
|
}
|
|
92
92
|
return fullResponseText;
|
|
@@ -94,6 +94,9 @@ export async function runSubagent(opts) {
|
|
|
94
94
|
/** Stream a single LLM response. */
|
|
95
95
|
async function streamOnce(llmClient, systemPrompt, conversation, apiTools, model, signal, dynamicContext) {
|
|
96
96
|
let text = "";
|
|
97
|
+
let reasoning = "";
|
|
98
|
+
let reasoningField = null;
|
|
99
|
+
const reasoningDetailsByIndex = new Map();
|
|
97
100
|
const pendingToolCalls = [];
|
|
98
101
|
let usage = null;
|
|
99
102
|
const messages = [
|
|
@@ -127,6 +130,29 @@ async function streamOnce(llmClient, systemPrompt, conversation, apiTools, model
|
|
|
127
130
|
if (delta?.content) {
|
|
128
131
|
text += delta.content;
|
|
129
132
|
}
|
|
133
|
+
const d = delta;
|
|
134
|
+
for (const name of ["reasoning", "reasoning_content"]) {
|
|
135
|
+
if (typeof d?.[name] === "string" && d[name].length > 0) {
|
|
136
|
+
reasoning += d[name];
|
|
137
|
+
reasoningField ??= name;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
if (Array.isArray(d?.reasoning_details)) {
|
|
141
|
+
for (const x of d.reasoning_details) {
|
|
142
|
+
const idx = typeof x?.index === "number" ? x.index : reasoningDetailsByIndex.size;
|
|
143
|
+
const prev = reasoningDetailsByIndex.get(idx);
|
|
144
|
+
if (!prev) {
|
|
145
|
+
reasoningDetailsByIndex.set(idx, { ...x });
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
if (typeof x.text === "string")
|
|
149
|
+
prev.text = (prev.text ?? "") + x.text;
|
|
150
|
+
for (const [k, v] of Object.entries(x))
|
|
151
|
+
if (k !== "text" && prev[k] === undefined)
|
|
152
|
+
prev[k] = v;
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
}
|
|
130
156
|
if (delta?.tool_calls) {
|
|
131
157
|
for (const tc of delta.tool_calls) {
|
|
132
158
|
const idx = tc.index;
|
|
@@ -157,5 +183,19 @@ async function streamOnce(llmClient, systemPrompt, conversation, apiTools, model
|
|
|
157
183
|
const assistantToolCalls = pendingToolCalls.length
|
|
158
184
|
? pendingToolCalls.map(tc => ({ id: tc.id, function: { name: tc.name, arguments: tc.argumentsJson } }))
|
|
159
185
|
: undefined;
|
|
160
|
-
|
|
186
|
+
const extras = {};
|
|
187
|
+
if (reasoning && reasoningField)
|
|
188
|
+
extras[reasoningField] = reasoning;
|
|
189
|
+
if (reasoningDetailsByIndex.size > 0) {
|
|
190
|
+
extras.reasoning_details = [...reasoningDetailsByIndex.entries()]
|
|
191
|
+
.sort((a, b) => a[0] - b[0]).map(([, v]) => v);
|
|
192
|
+
}
|
|
193
|
+
return {
|
|
194
|
+
text,
|
|
195
|
+
toolCalls: pendingToolCalls,
|
|
196
|
+
assistantContent: text || null,
|
|
197
|
+
assistantToolCalls,
|
|
198
|
+
extras: Object.keys(extras).length > 0 ? extras : undefined,
|
|
199
|
+
usage,
|
|
200
|
+
};
|
|
161
201
|
}
|
|
@@ -39,7 +39,7 @@ export interface ToolProtocol {
|
|
|
39
39
|
/** Rewrite a tool call before execution (e.g., unwrap meta-tool). */
|
|
40
40
|
rewriteToolCall(tc: PendingToolCall): PendingToolCall;
|
|
41
41
|
/** Record the assistant turn in conversation state. */
|
|
42
|
-
recordAssistant(conv: ConversationState, text: string, toolCalls: PendingToolCall[]): void;
|
|
42
|
+
recordAssistant(conv: ConversationState, text: string, toolCalls: PendingToolCall[], extras?: Record<string, unknown>): void;
|
|
43
43
|
/** Record all tool results for a batch as conversation messages. */
|
|
44
44
|
recordResults(conv: ConversationState, results: ToolResult[]): void;
|
|
45
45
|
/** Create a stream filter for stripping tool calls from display. null = pass-through. */
|
|
@@ -57,7 +57,7 @@ export declare class ApiToolProtocol implements ToolProtocol {
|
|
|
57
57
|
getToolPrompt(): string;
|
|
58
58
|
extractToolCalls(_text: string, streamedCalls: PendingToolCall[]): PendingToolCall[];
|
|
59
59
|
rewriteToolCall(tc: PendingToolCall): PendingToolCall;
|
|
60
|
-
recordAssistant(conv: ConversationState, text: string, toolCalls: PendingToolCall[]): void;
|
|
60
|
+
recordAssistant(conv: ConversationState, text: string, toolCalls: PendingToolCall[], extras?: Record<string, unknown>): void;
|
|
61
61
|
recordResults(conv: ConversationState, results: ToolResult[]): void;
|
|
62
62
|
createStreamFilter(): null;
|
|
63
63
|
}
|
|
@@ -68,7 +68,7 @@ export declare class InlineToolProtocol implements ToolProtocol {
|
|
|
68
68
|
getToolPrompt(tools: ToolDefinition[]): string;
|
|
69
69
|
rewriteToolCall(tc: PendingToolCall): PendingToolCall;
|
|
70
70
|
extractToolCalls(text: string, _streamedCalls: PendingToolCall[]): PendingToolCall[];
|
|
71
|
-
recordAssistant(conv: ConversationState, text: string, _toolCalls: PendingToolCall[]): void;
|
|
71
|
+
recordAssistant(conv: ConversationState, text: string, _toolCalls: PendingToolCall[], extras?: Record<string, unknown>): void;
|
|
72
72
|
recordResults(conv: ConversationState, results: ToolResult[]): void;
|
|
73
73
|
createStreamFilter(_toolNames: string[]): StreamFilter;
|
|
74
74
|
}
|
|
@@ -82,7 +82,7 @@ export declare class DeferredToolProtocol implements ToolProtocol {
|
|
|
82
82
|
getToolPrompt(): string;
|
|
83
83
|
extractToolCalls(_text: string, streamedCalls: PendingToolCall[]): PendingToolCall[];
|
|
84
84
|
rewriteToolCall(tc: PendingToolCall): PendingToolCall;
|
|
85
|
-
recordAssistant(conv: ConversationState, text: string, toolCalls: PendingToolCall[]): void;
|
|
85
|
+
recordAssistant(conv: ConversationState, text: string, toolCalls: PendingToolCall[], extras?: Record<string, unknown>): void;
|
|
86
86
|
recordResults(conv: ConversationState, results: ToolResult[]): void;
|
|
87
87
|
createStreamFilter(): null;
|
|
88
88
|
}
|
|
@@ -97,7 +97,7 @@ export declare class DeferredLookupProtocol implements ToolProtocol {
|
|
|
97
97
|
getToolPrompt(): string;
|
|
98
98
|
extractToolCalls(_text: string, streamedCalls: PendingToolCall[]): PendingToolCall[];
|
|
99
99
|
rewriteToolCall(tc: PendingToolCall): PendingToolCall;
|
|
100
|
-
recordAssistant(conv: ConversationState, text: string, toolCalls: PendingToolCall[]): void;
|
|
100
|
+
recordAssistant(conv: ConversationState, text: string, toolCalls: PendingToolCall[], extras?: Record<string, unknown>): void;
|
|
101
101
|
recordResults(conv: ConversationState, results: ToolResult[]): void;
|
|
102
102
|
createStreamFilter(): null;
|
|
103
103
|
getProtocolTools(): ToolDefinition[];
|
|
@@ -22,14 +22,14 @@ export class ApiToolProtocol {
|
|
|
22
22
|
rewriteToolCall(tc) {
|
|
23
23
|
return tc;
|
|
24
24
|
}
|
|
25
|
-
recordAssistant(conv, text, toolCalls) {
|
|
25
|
+
recordAssistant(conv, text, toolCalls, extras) {
|
|
26
26
|
const calls = toolCalls.length
|
|
27
27
|
? toolCalls.map((tc) => ({
|
|
28
28
|
id: tc.id,
|
|
29
29
|
function: { name: tc.name, arguments: tc.argumentsJson },
|
|
30
30
|
}))
|
|
31
31
|
: undefined;
|
|
32
|
-
conv.addAssistantMessage(text || null, calls);
|
|
32
|
+
conv.addAssistantMessage(text || null, calls, extras);
|
|
33
33
|
}
|
|
34
34
|
recordResults(conv, results) {
|
|
35
35
|
for (const r of results) {
|
|
@@ -97,8 +97,8 @@ export class InlineToolProtocol {
|
|
|
97
97
|
}
|
|
98
98
|
return calls;
|
|
99
99
|
}
|
|
100
|
-
recordAssistant(conv, text, _toolCalls) {
|
|
101
|
-
conv.addAssistantMessage(text || null);
|
|
100
|
+
recordAssistant(conv, text, _toolCalls, extras) {
|
|
101
|
+
conv.addAssistantMessage(text || null, undefined, extras);
|
|
102
102
|
}
|
|
103
103
|
recordResults(conv, results) {
|
|
104
104
|
if (results.length === 0)
|
|
@@ -351,14 +351,14 @@ export class DeferredToolProtocol {
|
|
|
351
351
|
return tc; // Let it fail naturally downstream
|
|
352
352
|
}
|
|
353
353
|
}
|
|
354
|
-
recordAssistant(conv, text, toolCalls) {
|
|
354
|
+
recordAssistant(conv, text, toolCalls, extras) {
|
|
355
355
|
const calls = toolCalls.length
|
|
356
356
|
? toolCalls.map((tc) => ({
|
|
357
357
|
id: tc.id,
|
|
358
358
|
function: { name: tc.name, arguments: tc.argumentsJson },
|
|
359
359
|
}))
|
|
360
360
|
: undefined;
|
|
361
|
-
conv.addAssistantMessage(text || null, calls);
|
|
361
|
+
conv.addAssistantMessage(text || null, calls, extras);
|
|
362
362
|
}
|
|
363
363
|
recordResults(conv, results) {
|
|
364
364
|
for (const r of results) {
|
|
@@ -444,14 +444,14 @@ export class DeferredLookupProtocol {
|
|
|
444
444
|
rewriteToolCall(tc) {
|
|
445
445
|
return tc; // no dispatching needed — load_tool is a real registered tool
|
|
446
446
|
}
|
|
447
|
-
recordAssistant(conv, text, toolCalls) {
|
|
447
|
+
recordAssistant(conv, text, toolCalls, extras) {
|
|
448
448
|
const calls = toolCalls.length
|
|
449
449
|
? toolCalls.map((tc) => ({
|
|
450
450
|
id: tc.id,
|
|
451
451
|
function: { name: tc.name, arguments: tc.argumentsJson },
|
|
452
452
|
}))
|
|
453
453
|
: undefined;
|
|
454
|
-
conv.addAssistantMessage(text || null, calls);
|
|
454
|
+
conv.addAssistantMessage(text || null, calls, extras);
|
|
455
455
|
}
|
|
456
456
|
recordResults(conv, results) {
|
|
457
457
|
for (const r of results) {
|
package/dist/event-bus.d.ts
CHANGED
|
@@ -65,6 +65,15 @@ export interface ShellEvents {
|
|
|
65
65
|
completion_tokens: number;
|
|
66
66
|
total_tokens: number;
|
|
67
67
|
};
|
|
68
|
+
"llm:request": {
|
|
69
|
+
messages: unknown[];
|
|
70
|
+
tools?: unknown;
|
|
71
|
+
model?: string;
|
|
72
|
+
reasoning_effort?: string;
|
|
73
|
+
};
|
|
74
|
+
"llm:chunk": {
|
|
75
|
+
chunk: unknown;
|
|
76
|
+
};
|
|
68
77
|
"agent:processing-start": Record<string, never>;
|
|
69
78
|
"agent:processing-done": Record<string, never>;
|
|
70
79
|
"agent:cancelled": Record<string, never>;
|
package/dist/event-bus.js
CHANGED
|
@@ -6,7 +6,7 @@ import { EventEmitter } from "node:events";
|
|
|
6
6
|
* can modify the payload before passing to the next
|
|
7
7
|
*/
|
|
8
8
|
export class EventBus {
|
|
9
|
-
emitter = new EventEmitter();
|
|
9
|
+
emitter = new EventEmitter().setMaxListeners(0);
|
|
10
10
|
pipeListeners = new Map();
|
|
11
11
|
asyncPipeListeners = new Map();
|
|
12
12
|
/** Subscribe to a fire-and-forget event. */
|