@poncho-ai/cli 0.9.4 → 0.10.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/.turbo/turbo-build.log +5 -5
- package/CHANGELOG.md +25 -0
- package/dist/{chunk-HYTF3H3J.js → chunk-MGR2GJMB.js} +9 -1
- package/dist/cli.js +1 -1
- package/dist/index.js +1 -1
- package/dist/run-interactive-ink-X7VHWGLT.js +1964 -0
- package/package.json +3 -3
- package/src/index.ts +8 -0
- package/src/mascot.ts +1437 -0
- package/src/run-interactive-ink.ts +3 -10
- package/.turbo/turbo-test.log +0 -389
- package/dist/chunk-42U2R3FH.js +0 -5752
- package/dist/chunk-4UDNQZ3G.js +0 -5752
- package/dist/chunk-5NHWU4QU.js +0 -5752
- package/dist/chunk-6CDE6R7D.js +0 -5752
- package/dist/chunk-74HD63WM.js +0 -5819
- package/dist/chunk-7TRWWFGI.js +0 -5752
- package/dist/chunk-DFV5DGX4.js +0 -5794
- package/dist/chunk-G67AWHXV.js +0 -5752
- package/dist/chunk-GFGEMANG.js +0 -5820
- package/dist/chunk-IZJUA24R.js +0 -5750
- package/dist/chunk-J2MTY7EY.js +0 -5780
- package/dist/chunk-KKQSJC2U.js +0 -5750
- package/dist/chunk-L65TFTEI.js +0 -5752
- package/dist/chunk-O5NLOW2I.js +0 -5752
- package/dist/chunk-OGTT4YJG.js +0 -5752
- package/dist/chunk-OHKXVDVM.js +0 -5752
- package/dist/chunk-OTOMFL3L.js +0 -5773
- package/dist/chunk-PHVOJ2R5.js +0 -5781
- package/dist/chunk-Q3WHF2FP.js +0 -5752
- package/dist/chunk-RN7FDRZH.js +0 -5752
- package/dist/chunk-SWPCETEB.js +0 -5772
- package/dist/chunk-VP4ABFQK.js +0 -5795
- package/dist/chunk-ZCLLCLRR.js +0 -5752
- package/dist/chunk-ZHHKZDHY.js +0 -5795
- package/dist/run-interactive-ink-2CVZHZLL.js +0 -535
- package/dist/run-interactive-ink-3TNAVPQ7.js +0 -534
- package/dist/run-interactive-ink-54UJ6WGA.js +0 -535
- package/dist/run-interactive-ink-64XY2KJD.js +0 -535
- package/dist/run-interactive-ink-7EB3ZX6P.js +0 -535
- package/dist/run-interactive-ink-7OSESHKH.js +0 -535
- package/dist/run-interactive-ink-BU4ZKI3Z.js +0 -535
- package/dist/run-interactive-ink-DFIJTWMT.js +0 -535
- package/dist/run-interactive-ink-DORF57NC.js +0 -535
- package/dist/run-interactive-ink-EOW4MLEH.js +0 -535
- package/dist/run-interactive-ink-EU3DN4MJ.js +0 -535
- package/dist/run-interactive-ink-HMVUIZRO.js +0 -533
- package/dist/run-interactive-ink-MQTTMSSO.js +0 -535
- package/dist/run-interactive-ink-NT66KRS5.js +0 -535
- package/dist/run-interactive-ink-O5AV46ZE.js +0 -535
- package/dist/run-interactive-ink-OC57VVOY.js +0 -535
- package/dist/run-interactive-ink-PTUDJKT5.js +0 -535
- package/dist/run-interactive-ink-PYQFHCNW.js +0 -537
- package/dist/run-interactive-ink-QXIIUBIC.js +0 -534
- package/dist/run-interactive-ink-SOGKAQAE.js +0 -522
- package/dist/run-interactive-ink-UPMUH72Q.js +0 -520
- package/dist/run-interactive-ink-W4NEF7FL.js +0 -522
- package/dist/run-interactive-ink-WBPOSHHC.js +0 -535
- package/dist/run-interactive-ink-XEK5ZPSU.js +0 -535
- package/dist/run-interactive-ink-YWJ5OBNI.js +0 -535
|
@@ -1,537 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
consumeFirstRunIntro,
|
|
3
|
-
inferConversationTitle,
|
|
4
|
-
resolveHarnessEnvironment
|
|
5
|
-
} from "./chunk-42U2R3FH.js";
|
|
6
|
-
|
|
7
|
-
// src/run-interactive-ink.ts
|
|
8
|
-
import * as readline from "readline";
|
|
9
|
-
import { stdout } from "process";
|
|
10
|
-
import {
|
|
11
|
-
parseAgentFile
|
|
12
|
-
} from "@poncho-ai/harness";
|
|
13
|
-
var C = {
|
|
14
|
-
reset: "\x1B[0m",
|
|
15
|
-
bold: "\x1B[1m",
|
|
16
|
-
dim: "\x1B[2m",
|
|
17
|
-
cyan: "\x1B[36m",
|
|
18
|
-
green: "\x1B[32m",
|
|
19
|
-
yellow: "\x1B[33m",
|
|
20
|
-
red: "\x1B[31m",
|
|
21
|
-
gray: "\x1B[90m",
|
|
22
|
-
magenta: "\x1B[35m"
|
|
23
|
-
};
|
|
24
|
-
var green = (s) => `${C.green}${s}${C.reset}`;
|
|
25
|
-
var yellow = (s) => `${C.yellow}${s}${C.reset}`;
|
|
26
|
-
var red = (s) => `${C.red}${s}${C.reset}`;
|
|
27
|
-
var gray = (s) => `${C.gray}${s}${C.reset}`;
|
|
28
|
-
var magenta = (s) => `${C.magenta}${s}${C.reset}`;
|
|
29
|
-
var FAUX_TOOL_LOG_PATTERN = /Tool Used:|Tool Result:|\blist_skills\b|\bcreate_skill\b|\bedit_skill\b/i;
|
|
30
|
-
var formatDuration = (ms) => ms < 1e3 ? `${ms}ms` : `${(ms / 1e3).toFixed(1)}s`;
|
|
31
|
-
var stringifyValue = (v) => {
|
|
32
|
-
try {
|
|
33
|
-
return JSON.stringify(v);
|
|
34
|
-
} catch {
|
|
35
|
-
return String(v);
|
|
36
|
-
}
|
|
37
|
-
};
|
|
38
|
-
var truncate = (v, max) => v.length <= max ? v : `${v.slice(0, Math.max(0, max - 3))}...`;
|
|
39
|
-
var compactPreview = (v, max = 120) => truncate(stringifyValue(v).replace(/\s+/g, " "), max);
|
|
40
|
-
var loadMetadata = async (workingDir) => {
|
|
41
|
-
let agentName = "agent";
|
|
42
|
-
let model = "unknown";
|
|
43
|
-
let provider = "unknown";
|
|
44
|
-
try {
|
|
45
|
-
const parsed = await parseAgentFile(workingDir);
|
|
46
|
-
agentName = parsed.frontmatter.name ?? agentName;
|
|
47
|
-
model = parsed.frontmatter.model?.name ?? model;
|
|
48
|
-
provider = parsed.frontmatter.model?.provider ?? provider;
|
|
49
|
-
} catch {
|
|
50
|
-
}
|
|
51
|
-
return {
|
|
52
|
-
agentName,
|
|
53
|
-
model,
|
|
54
|
-
provider,
|
|
55
|
-
workingDir,
|
|
56
|
-
environment: resolveHarnessEnvironment()
|
|
57
|
-
};
|
|
58
|
-
};
|
|
59
|
-
var ask = (rl, prompt) => new Promise((res) => {
|
|
60
|
-
rl.question(prompt, (answer) => res(answer));
|
|
61
|
-
});
|
|
62
|
-
var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
63
|
-
var streamTextAsTokens = async (text) => {
|
|
64
|
-
const tokens = text.match(/\S+\s*|\n/g) ?? [text];
|
|
65
|
-
for (const token of tokens) {
|
|
66
|
-
stdout.write(token);
|
|
67
|
-
const trimmed = token.trim();
|
|
68
|
-
const delay = trimmed.length === 0 ? 0 : Math.max(4, Math.min(18, Math.floor(trimmed.length / 2)));
|
|
69
|
-
await sleep(delay);
|
|
70
|
-
}
|
|
71
|
-
};
|
|
72
|
-
var OWNER_ID = "local-owner";
|
|
73
|
-
var computeTurn = (messages) => Math.max(1, Math.floor(messages.length / 2) + 1);
|
|
74
|
-
var formatDate = (value) => {
|
|
75
|
-
try {
|
|
76
|
-
return new Date(value).toLocaleString();
|
|
77
|
-
} catch {
|
|
78
|
-
return String(value);
|
|
79
|
-
}
|
|
80
|
-
};
|
|
81
|
-
var handleSlash = async (command, state, conversationStore) => {
|
|
82
|
-
const [rawCommand, ...args] = command.trim().split(/\s+/);
|
|
83
|
-
const norm = rawCommand.toLowerCase();
|
|
84
|
-
if (norm === "/help") {
|
|
85
|
-
console.log(
|
|
86
|
-
gray(
|
|
87
|
-
"commands> /help /clear /exit /tools /list /open <id> /new [title] /delete [id] /continue /reset [all]"
|
|
88
|
-
)
|
|
89
|
-
);
|
|
90
|
-
return { shouldExit: false };
|
|
91
|
-
}
|
|
92
|
-
if (norm === "/clear") {
|
|
93
|
-
process.stdout.write("\x1B[2J\x1B[H");
|
|
94
|
-
return { shouldExit: false };
|
|
95
|
-
}
|
|
96
|
-
if (norm === "/exit") {
|
|
97
|
-
return { shouldExit: true };
|
|
98
|
-
}
|
|
99
|
-
if (norm === "/list") {
|
|
100
|
-
const conversations = await conversationStore.list(OWNER_ID);
|
|
101
|
-
if (conversations.length === 0) {
|
|
102
|
-
console.log(gray("conversations> none"));
|
|
103
|
-
return { shouldExit: false };
|
|
104
|
-
}
|
|
105
|
-
console.log(gray("conversations>"));
|
|
106
|
-
for (const conversation of conversations) {
|
|
107
|
-
const activeMarker = state.activeConversationId === conversation.conversationId ? "*" : " ";
|
|
108
|
-
const maxTitleLen = 40;
|
|
109
|
-
const title = conversation.title.length > maxTitleLen ? conversation.title.slice(0, maxTitleLen - 1) + "\u2026" : conversation.title;
|
|
110
|
-
console.log(
|
|
111
|
-
gray(
|
|
112
|
-
`${activeMarker} ${conversation.conversationId} | ${title} | ${formatDate(conversation.updatedAt)}`
|
|
113
|
-
)
|
|
114
|
-
);
|
|
115
|
-
}
|
|
116
|
-
return { shouldExit: false };
|
|
117
|
-
}
|
|
118
|
-
if (norm === "/open") {
|
|
119
|
-
const conversationId = args[0];
|
|
120
|
-
if (!conversationId) {
|
|
121
|
-
console.log(yellow("usage> /open <conversationId>"));
|
|
122
|
-
return { shouldExit: false };
|
|
123
|
-
}
|
|
124
|
-
const conversation = await conversationStore.get(conversationId);
|
|
125
|
-
if (!conversation) {
|
|
126
|
-
console.log(yellow(`conversations> not found: ${conversationId}`));
|
|
127
|
-
return { shouldExit: false };
|
|
128
|
-
}
|
|
129
|
-
state.activeConversationId = conversation.conversationId;
|
|
130
|
-
state.messages = [...conversation.messages];
|
|
131
|
-
state.turn = computeTurn(state.messages);
|
|
132
|
-
console.log(gray(`conversations> opened ${conversation.conversationId}`));
|
|
133
|
-
return { shouldExit: false };
|
|
134
|
-
}
|
|
135
|
-
if (norm === "/new") {
|
|
136
|
-
const title = args.join(" ").trim();
|
|
137
|
-
const conversation = await conversationStore.create(OWNER_ID, title || void 0);
|
|
138
|
-
state.activeConversationId = conversation.conversationId;
|
|
139
|
-
state.messages = [];
|
|
140
|
-
state.turn = 1;
|
|
141
|
-
console.log(gray(`conversations> new ${conversation.conversationId}`));
|
|
142
|
-
return { shouldExit: false };
|
|
143
|
-
}
|
|
144
|
-
if (norm === "/delete") {
|
|
145
|
-
const targetConversationId = args[0] ?? state.activeConversationId ?? "";
|
|
146
|
-
if (!targetConversationId) {
|
|
147
|
-
console.log(yellow("usage> /delete <conversationId>"));
|
|
148
|
-
return { shouldExit: false };
|
|
149
|
-
}
|
|
150
|
-
const removed = await conversationStore.delete(targetConversationId);
|
|
151
|
-
if (!removed) {
|
|
152
|
-
console.log(yellow(`conversations> not found: ${targetConversationId}`));
|
|
153
|
-
return { shouldExit: false };
|
|
154
|
-
}
|
|
155
|
-
if (state.activeConversationId === targetConversationId) {
|
|
156
|
-
state.activeConversationId = null;
|
|
157
|
-
state.messages = [];
|
|
158
|
-
state.turn = 1;
|
|
159
|
-
}
|
|
160
|
-
console.log(gray(`conversations> deleted ${targetConversationId}`));
|
|
161
|
-
return { shouldExit: false };
|
|
162
|
-
}
|
|
163
|
-
if (norm === "/continue") {
|
|
164
|
-
const conversations = await conversationStore.list(OWNER_ID);
|
|
165
|
-
const latest = conversations[0];
|
|
166
|
-
if (!latest) {
|
|
167
|
-
console.log(yellow("conversations> no conversations to continue"));
|
|
168
|
-
return { shouldExit: false };
|
|
169
|
-
}
|
|
170
|
-
state.activeConversationId = latest.conversationId;
|
|
171
|
-
state.messages = [...latest.messages];
|
|
172
|
-
state.turn = computeTurn(state.messages);
|
|
173
|
-
console.log(gray(`conversations> continued ${latest.conversationId}`));
|
|
174
|
-
return { shouldExit: false };
|
|
175
|
-
}
|
|
176
|
-
if (norm === "/reset") {
|
|
177
|
-
if (args[0]?.toLowerCase() === "all") {
|
|
178
|
-
const conversations = await conversationStore.list(OWNER_ID);
|
|
179
|
-
for (const conversation2 of conversations) {
|
|
180
|
-
await conversationStore.delete(conversation2.conversationId);
|
|
181
|
-
}
|
|
182
|
-
state.activeConversationId = null;
|
|
183
|
-
state.messages = [];
|
|
184
|
-
state.turn = 1;
|
|
185
|
-
console.log(gray("conversations> reset all"));
|
|
186
|
-
return { shouldExit: false };
|
|
187
|
-
}
|
|
188
|
-
if (!state.activeConversationId) {
|
|
189
|
-
state.messages = [];
|
|
190
|
-
state.turn = 1;
|
|
191
|
-
console.log(gray("conversations> current session reset"));
|
|
192
|
-
return { shouldExit: false };
|
|
193
|
-
}
|
|
194
|
-
const conversation = await conversationStore.get(state.activeConversationId);
|
|
195
|
-
if (!conversation) {
|
|
196
|
-
state.activeConversationId = null;
|
|
197
|
-
state.messages = [];
|
|
198
|
-
state.turn = 1;
|
|
199
|
-
console.log(yellow("conversations> active conversation no longer exists"));
|
|
200
|
-
return { shouldExit: false };
|
|
201
|
-
}
|
|
202
|
-
await conversationStore.update({
|
|
203
|
-
...conversation,
|
|
204
|
-
messages: []
|
|
205
|
-
});
|
|
206
|
-
state.messages = [];
|
|
207
|
-
state.turn = 1;
|
|
208
|
-
console.log(gray(`conversations> reset ${conversation.conversationId}`));
|
|
209
|
-
return { shouldExit: false };
|
|
210
|
-
}
|
|
211
|
-
console.log(yellow(`Unknown command: ${command}`));
|
|
212
|
-
return { shouldExit: false };
|
|
213
|
-
};
|
|
214
|
-
var runInteractiveInk = async ({
|
|
215
|
-
harness,
|
|
216
|
-
params,
|
|
217
|
-
workingDir,
|
|
218
|
-
config,
|
|
219
|
-
conversationStore,
|
|
220
|
-
onSetApprovalCallback
|
|
221
|
-
}) => {
|
|
222
|
-
const metadata = await loadMetadata(workingDir);
|
|
223
|
-
const rl = readline.createInterface({
|
|
224
|
-
input: process.stdin,
|
|
225
|
-
output: process.stdout,
|
|
226
|
-
terminal: true
|
|
227
|
-
});
|
|
228
|
-
if (onSetApprovalCallback) {
|
|
229
|
-
onSetApprovalCallback((req) => {
|
|
230
|
-
process.stdout.write("\n");
|
|
231
|
-
const preview = compactPreview(req.input, 100);
|
|
232
|
-
rl.question(
|
|
233
|
-
`${C.yellow}${C.bold}Tool "${req.tool}" requires approval${C.reset}
|
|
234
|
-
${C.gray}input: ${preview}${C.reset}
|
|
235
|
-
${C.yellow}approve? (y/n): ${C.reset}`,
|
|
236
|
-
(answer) => {
|
|
237
|
-
const approved = answer.trim().toLowerCase() === "y";
|
|
238
|
-
console.log(
|
|
239
|
-
approved ? green(` approved ${req.tool}`) : magenta(` denied ${req.tool}`)
|
|
240
|
-
);
|
|
241
|
-
req.resolve(approved);
|
|
242
|
-
}
|
|
243
|
-
);
|
|
244
|
-
});
|
|
245
|
-
}
|
|
246
|
-
const mascot = [
|
|
247
|
-
`${C.yellow} \u28E0\u28F6\u28F6\u28F6\u28F6\u28C4${C.reset}`,
|
|
248
|
-
`${C.yellow} \u2880\u28FE\u28FF\u28FF\u28FF\u28FF\u28FF\u28FF\u28F7\u2840${C.reset}`,
|
|
249
|
-
`${C.gray} \u2847 \u28B8${C.reset}`,
|
|
250
|
-
`${C.gray} \u2847${C.cyan}\u28FF\u28FF \u28FF\u28FF${C.gray}\u28B8${C.reset}`,
|
|
251
|
-
`${C.gray} \u2847${C.cyan} \u2812\u2812 ${C.gray}\u28B8${C.reset}`,
|
|
252
|
-
`${C.gray} \u28C7\u28C0\u28C0\u28C0\u28C0\u28C0\u28C0\u28F8${C.reset}`,
|
|
253
|
-
`${C.yellow} \u28FF\u28FF\u28FF\u28FF\u28FF\u28FF${C.reset}`,
|
|
254
|
-
`${C.gray} \u2803 \u2818${C.reset}`
|
|
255
|
-
];
|
|
256
|
-
console.log("");
|
|
257
|
-
for (const line of mascot) {
|
|
258
|
-
console.log(line);
|
|
259
|
-
}
|
|
260
|
-
console.log(`${C.bold}${C.cyan} poncho${C.reset}`);
|
|
261
|
-
console.log("");
|
|
262
|
-
console.log(
|
|
263
|
-
gray(
|
|
264
|
-
` ${metadata.agentName} \xB7 ${metadata.provider}/${metadata.model} \xB7 ${metadata.environment}`
|
|
265
|
-
)
|
|
266
|
-
);
|
|
267
|
-
console.log(gray(' Type "exit" to quit, "/help" for commands'));
|
|
268
|
-
console.log(gray(" Press Ctrl+C during a run to stop streaming output."));
|
|
269
|
-
console.log(
|
|
270
|
-
gray(" Conversation controls: /list /open <id> /new [title] /delete [id] /continue /reset [all]\n")
|
|
271
|
-
);
|
|
272
|
-
const intro = await consumeFirstRunIntro(workingDir, {
|
|
273
|
-
agentName: metadata.agentName,
|
|
274
|
-
provider: metadata.provider,
|
|
275
|
-
model: metadata.model,
|
|
276
|
-
config
|
|
277
|
-
});
|
|
278
|
-
if (intro) {
|
|
279
|
-
console.log(green("assistant>"));
|
|
280
|
-
await streamTextAsTokens(intro);
|
|
281
|
-
stdout.write("\n\n");
|
|
282
|
-
}
|
|
283
|
-
let messages = intro ? [{ role: "assistant", content: intro }] : [];
|
|
284
|
-
let turn = 1;
|
|
285
|
-
let activeConversationId = null;
|
|
286
|
-
let showToolPayloads = false;
|
|
287
|
-
let activeRunAbortController = null;
|
|
288
|
-
rl.on("SIGINT", () => {
|
|
289
|
-
if (activeRunAbortController && !activeRunAbortController.signal.aborted) {
|
|
290
|
-
activeRunAbortController.abort();
|
|
291
|
-
process.stdout.write("\n");
|
|
292
|
-
console.log(gray("stop> cancelling current run..."));
|
|
293
|
-
return;
|
|
294
|
-
}
|
|
295
|
-
rl.close();
|
|
296
|
-
});
|
|
297
|
-
const prompt = `${C.cyan}you> ${C.reset}`;
|
|
298
|
-
while (true) {
|
|
299
|
-
let task;
|
|
300
|
-
try {
|
|
301
|
-
task = await ask(rl, prompt);
|
|
302
|
-
} catch {
|
|
303
|
-
break;
|
|
304
|
-
}
|
|
305
|
-
const trimmed = task.trim();
|
|
306
|
-
if (!trimmed) continue;
|
|
307
|
-
if (trimmed.toLowerCase() === "exit") break;
|
|
308
|
-
if (trimmed.startsWith("/")) {
|
|
309
|
-
if (trimmed.toLowerCase() === "/exit") break;
|
|
310
|
-
if (trimmed.toLowerCase() === "/tools") {
|
|
311
|
-
showToolPayloads = !showToolPayloads;
|
|
312
|
-
console.log(gray(`tool payloads: ${showToolPayloads ? "on" : "off"}`));
|
|
313
|
-
continue;
|
|
314
|
-
}
|
|
315
|
-
const interactiveState = {
|
|
316
|
-
messages,
|
|
317
|
-
turn,
|
|
318
|
-
activeConversationId
|
|
319
|
-
};
|
|
320
|
-
const slashResult = await handleSlash(
|
|
321
|
-
trimmed,
|
|
322
|
-
interactiveState,
|
|
323
|
-
conversationStore
|
|
324
|
-
);
|
|
325
|
-
if (slashResult.shouldExit) {
|
|
326
|
-
break;
|
|
327
|
-
}
|
|
328
|
-
messages = interactiveState.messages;
|
|
329
|
-
turn = interactiveState.turn;
|
|
330
|
-
activeConversationId = interactiveState.activeConversationId;
|
|
331
|
-
continue;
|
|
332
|
-
}
|
|
333
|
-
console.log(gray(`
|
|
334
|
-
--- turn ${turn} ---`));
|
|
335
|
-
process.stdout.write(gray("thinking..."));
|
|
336
|
-
let thinkingCleared = false;
|
|
337
|
-
const clearThinking = () => {
|
|
338
|
-
if (thinkingCleared) return;
|
|
339
|
-
thinkingCleared = true;
|
|
340
|
-
readline.clearLine(process.stdout, 0);
|
|
341
|
-
readline.cursorTo(process.stdout, 0);
|
|
342
|
-
};
|
|
343
|
-
let responseText = "";
|
|
344
|
-
let streamedText = "";
|
|
345
|
-
let committedText = false;
|
|
346
|
-
let sawChunk = false;
|
|
347
|
-
let toolEvents = 0;
|
|
348
|
-
const toolTimeline = [];
|
|
349
|
-
const sections = [];
|
|
350
|
-
let currentText = "";
|
|
351
|
-
let currentTools = [];
|
|
352
|
-
let runFailed = false;
|
|
353
|
-
let runCancelled = false;
|
|
354
|
-
let usage;
|
|
355
|
-
let latestRunId = "";
|
|
356
|
-
const startedAt = Date.now();
|
|
357
|
-
activeRunAbortController = new AbortController();
|
|
358
|
-
try {
|
|
359
|
-
for await (const event of harness.run({
|
|
360
|
-
task: trimmed,
|
|
361
|
-
parameters: params,
|
|
362
|
-
messages,
|
|
363
|
-
abortSignal: activeRunAbortController.signal
|
|
364
|
-
})) {
|
|
365
|
-
if (event.type === "run:started") {
|
|
366
|
-
latestRunId = event.runId;
|
|
367
|
-
}
|
|
368
|
-
if (event.type === "model:chunk") {
|
|
369
|
-
sawChunk = true;
|
|
370
|
-
if (currentTools.length > 0) {
|
|
371
|
-
sections.push({ type: "tools", content: currentTools });
|
|
372
|
-
currentTools = [];
|
|
373
|
-
}
|
|
374
|
-
responseText += event.content;
|
|
375
|
-
streamedText += event.content;
|
|
376
|
-
currentText += event.content;
|
|
377
|
-
if (!thinkingCleared) {
|
|
378
|
-
clearThinking();
|
|
379
|
-
process.stdout.write(`${C.green}assistant> ${C.reset}`);
|
|
380
|
-
}
|
|
381
|
-
process.stdout.write(event.content);
|
|
382
|
-
} else if (event.type === "tool:started" || event.type === "tool:completed" || event.type === "tool:error" || event.type === "tool:approval:required" || event.type === "tool:approval:granted" || event.type === "tool:approval:denied") {
|
|
383
|
-
if (streamedText.length > 0) {
|
|
384
|
-
committedText = true;
|
|
385
|
-
streamedText = "";
|
|
386
|
-
process.stdout.write("\n");
|
|
387
|
-
}
|
|
388
|
-
clearThinking();
|
|
389
|
-
if (event.type === "tool:started") {
|
|
390
|
-
if (currentText.length > 0) {
|
|
391
|
-
sections.push({ type: "text", content: currentText });
|
|
392
|
-
currentText = "";
|
|
393
|
-
}
|
|
394
|
-
const preview = showToolPayloads ? compactPreview(event.input, 400) : compactPreview(event.input, 100);
|
|
395
|
-
console.log(yellow(`tools> start ${event.tool} input=${preview}`));
|
|
396
|
-
const toolText = `- start \`${event.tool}\``;
|
|
397
|
-
toolTimeline.push(toolText);
|
|
398
|
-
currentTools.push(toolText);
|
|
399
|
-
toolEvents += 1;
|
|
400
|
-
} else if (event.type === "tool:completed") {
|
|
401
|
-
const preview = showToolPayloads ? compactPreview(event.output, 400) : compactPreview(event.output, 100);
|
|
402
|
-
console.log(
|
|
403
|
-
yellow(
|
|
404
|
-
`tools> done ${event.tool} in ${formatDuration(event.duration)}`
|
|
405
|
-
)
|
|
406
|
-
);
|
|
407
|
-
if (showToolPayloads) {
|
|
408
|
-
console.log(yellow(`tools> output ${preview}`));
|
|
409
|
-
}
|
|
410
|
-
const toolText = `- done \`${event.tool}\` in ${formatDuration(event.duration)}`;
|
|
411
|
-
toolTimeline.push(toolText);
|
|
412
|
-
currentTools.push(toolText);
|
|
413
|
-
} else if (event.type === "tool:error") {
|
|
414
|
-
console.log(
|
|
415
|
-
red(`tools> error ${event.tool}: ${event.error}`)
|
|
416
|
-
);
|
|
417
|
-
const toolText = `- error \`${event.tool}\`: ${event.error}`;
|
|
418
|
-
toolTimeline.push(toolText);
|
|
419
|
-
currentTools.push(toolText);
|
|
420
|
-
} else if (event.type === "tool:approval:required") {
|
|
421
|
-
console.log(
|
|
422
|
-
magenta(`tools> approval required for ${event.tool}`)
|
|
423
|
-
);
|
|
424
|
-
const toolText = `- approval required \`${event.tool}\``;
|
|
425
|
-
toolTimeline.push(toolText);
|
|
426
|
-
currentTools.push(toolText);
|
|
427
|
-
} else if (event.type === "tool:approval:granted") {
|
|
428
|
-
console.log(
|
|
429
|
-
gray(`tools> approval granted (${event.approvalId})`)
|
|
430
|
-
);
|
|
431
|
-
const toolText = `- approval granted (${event.approvalId})`;
|
|
432
|
-
toolTimeline.push(toolText);
|
|
433
|
-
currentTools.push(toolText);
|
|
434
|
-
} else if (event.type === "tool:approval:denied") {
|
|
435
|
-
console.log(
|
|
436
|
-
magenta(`tools> approval denied (${event.approvalId})`)
|
|
437
|
-
);
|
|
438
|
-
const toolText = `- approval denied (${event.approvalId})`;
|
|
439
|
-
toolTimeline.push(toolText);
|
|
440
|
-
currentTools.push(toolText);
|
|
441
|
-
}
|
|
442
|
-
} else if (event.type === "run:error") {
|
|
443
|
-
clearThinking();
|
|
444
|
-
runFailed = true;
|
|
445
|
-
console.log(red(`error> ${event.error.message}`));
|
|
446
|
-
} else if (event.type === "run:cancelled") {
|
|
447
|
-
clearThinking();
|
|
448
|
-
runCancelled = true;
|
|
449
|
-
} else if (event.type === "model:response") {
|
|
450
|
-
usage = event.usage;
|
|
451
|
-
} else if (event.type === "run:completed" && !sawChunk) {
|
|
452
|
-
clearThinking();
|
|
453
|
-
responseText = event.result.response ?? "";
|
|
454
|
-
if (responseText.length > 0) {
|
|
455
|
-
process.stdout.write(
|
|
456
|
-
`${C.green}assistant> ${C.reset}${responseText}
|
|
457
|
-
`
|
|
458
|
-
);
|
|
459
|
-
}
|
|
460
|
-
}
|
|
461
|
-
}
|
|
462
|
-
} catch (error) {
|
|
463
|
-
clearThinking();
|
|
464
|
-
if (activeRunAbortController.signal.aborted) {
|
|
465
|
-
runCancelled = true;
|
|
466
|
-
} else {
|
|
467
|
-
runFailed = true;
|
|
468
|
-
console.log(
|
|
469
|
-
red(
|
|
470
|
-
`error> ${error instanceof Error ? error.message : "Unknown error"}`
|
|
471
|
-
)
|
|
472
|
-
);
|
|
473
|
-
}
|
|
474
|
-
} finally {
|
|
475
|
-
activeRunAbortController = null;
|
|
476
|
-
}
|
|
477
|
-
if (sawChunk && streamedText.length > 0) {
|
|
478
|
-
process.stdout.write("\n");
|
|
479
|
-
} else if (!sawChunk && !runFailed && !runCancelled && responseText.length === 0) {
|
|
480
|
-
clearThinking();
|
|
481
|
-
console.log(green("assistant> (no response)"));
|
|
482
|
-
}
|
|
483
|
-
const fullResponse = responseText || streamedText;
|
|
484
|
-
if (!runFailed && toolEvents === 0 && FAUX_TOOL_LOG_PATTERN.test(fullResponse)) {
|
|
485
|
-
console.log(
|
|
486
|
-
magenta(
|
|
487
|
-
"warning> assistant described tool execution but no real tool events occurred."
|
|
488
|
-
)
|
|
489
|
-
);
|
|
490
|
-
}
|
|
491
|
-
const durationMs = Date.now() - startedAt;
|
|
492
|
-
console.log(
|
|
493
|
-
gray(`meta> ${formatDuration(durationMs)} | tools: ${toolEvents}
|
|
494
|
-
`)
|
|
495
|
-
);
|
|
496
|
-
if (!activeConversationId) {
|
|
497
|
-
const created = await conversationStore.create(
|
|
498
|
-
OWNER_ID,
|
|
499
|
-
inferConversationTitle(trimmed)
|
|
500
|
-
);
|
|
501
|
-
activeConversationId = created.conversationId;
|
|
502
|
-
}
|
|
503
|
-
if (currentTools.length > 0) {
|
|
504
|
-
sections.push({ type: "tools", content: currentTools });
|
|
505
|
-
}
|
|
506
|
-
if (currentText.length > 0) {
|
|
507
|
-
sections.push({ type: "text", content: currentText });
|
|
508
|
-
}
|
|
509
|
-
messages.push({ role: "user", content: trimmed });
|
|
510
|
-
const hasAssistantContent = responseText.length > 0 || toolTimeline.length > 0 || sections.length > 0;
|
|
511
|
-
if (hasAssistantContent) {
|
|
512
|
-
messages.push({
|
|
513
|
-
role: "assistant",
|
|
514
|
-
content: responseText,
|
|
515
|
-
metadata: toolTimeline.length > 0 || sections.length > 0 ? {
|
|
516
|
-
toolActivity: toolTimeline,
|
|
517
|
-
sections: sections.length > 0 ? sections : void 0
|
|
518
|
-
} : void 0
|
|
519
|
-
});
|
|
520
|
-
}
|
|
521
|
-
turn = computeTurn(messages);
|
|
522
|
-
const conversation = await conversationStore.get(activeConversationId);
|
|
523
|
-
if (conversation) {
|
|
524
|
-
const maybeTitle = conversation.messages.length === 0 && (conversation.title === "New conversation" || conversation.title.trim().length === 0) ? inferConversationTitle(trimmed) : conversation.title;
|
|
525
|
-
await conversationStore.update({
|
|
526
|
-
...conversation,
|
|
527
|
-
title: maybeTitle,
|
|
528
|
-
messages: [...messages],
|
|
529
|
-
runtimeRunId: latestRunId || conversation.runtimeRunId
|
|
530
|
-
});
|
|
531
|
-
}
|
|
532
|
-
}
|
|
533
|
-
rl.close();
|
|
534
|
-
};
|
|
535
|
-
export {
|
|
536
|
-
runInteractiveInk
|
|
537
|
-
};
|