lonny-agent 0.2.3 → 0.2.4
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/.claude/settings.local.json +14 -0
- package/dist/agent/index.js +1 -1
- package/dist/agent/index.js.map +1 -1
- package/dist/agent/project.d.ts +26 -0
- package/dist/agent/project.d.ts.map +1 -0
- package/dist/agent/project.js +303 -0
- package/dist/agent/project.js.map +1 -0
- package/dist/agent/prompt-builder.d.ts +1 -1
- package/dist/agent/prompt-builder.d.ts.map +1 -1
- package/dist/agent/prompt-builder.js +10 -5
- package/dist/agent/prompt-builder.js.map +1 -1
- package/dist/agent/providers/anthropic.d.ts.map +1 -1
- package/dist/agent/providers/anthropic.js +20 -2
- package/dist/agent/providers/anthropic.js.map +1 -1
- package/dist/agent/providers/google.js +1 -1
- package/dist/agent/providers/google.js.map +1 -1
- package/dist/agent/providers/ollama.d.ts.map +1 -1
- package/dist/agent/providers/ollama.js +2 -1
- package/dist/agent/providers/ollama.js.map +1 -1
- package/dist/agent/providers/openai.d.ts.map +1 -1
- package/dist/agent/providers/openai.js +23 -3
- package/dist/agent/providers/openai.js.map +1 -1
- package/dist/agent/session.d.ts +4 -2
- package/dist/agent/session.d.ts.map +1 -1
- package/dist/agent/session.js +206 -147
- package/dist/agent/session.js.map +1 -1
- package/dist/tools/__tests__/edit.test.js +14 -14
- package/dist/tools/__tests__/edit.test.js.map +1 -1
- package/dist/tools/edit.d.ts +17 -0
- package/dist/tools/edit.d.ts.map +1 -1
- package/dist/tools/edit.js +81 -34
- package/dist/tools/edit.js.map +1 -1
- package/dist/tools/read.d.ts +6 -0
- package/dist/tools/read.d.ts.map +1 -1
- package/dist/tools/read.js +42 -15
- package/dist/tools/read.js.map +1 -1
- package/dist/tools/registry.d.ts.map +1 -1
- package/dist/tools/registry.js +0 -4
- package/dist/tools/registry.js.map +1 -1
- package/dist/tools/write_plan.d.ts +1 -1
- package/dist/tools/write_plan.d.ts.map +1 -1
- package/dist/tools/write_plan.js +14 -4
- package/dist/tools/write_plan.js.map +1 -1
- package/dist/tui/index.d.ts.map +1 -1
- package/dist/tui/index.js +5 -4
- package/dist/tui/index.js.map +1 -1
- package/dist/web/index.js +5 -5
- package/dist/web/index.js.map +1 -1
- package/package.json +1 -1
- package/src/agent/index.ts +1 -1
- package/src/agent/project.ts +366 -0
- package/src/agent/prompt-builder.ts +11 -5
- package/src/agent/providers/anthropic.ts +9 -2
- package/src/agent/providers/google.ts +1 -1
- package/src/agent/providers/ollama.ts +5 -1
- package/src/agent/providers/openai.ts +15 -3
- package/src/agent/session.ts +216 -150
- package/src/tools/__tests__/edit.test.ts +14 -14
- package/src/tools/edit.ts +103 -36
- package/src/tools/read.ts +61 -25
- package/src/tools/registry.ts +260 -265
- package/src/tools/write_plan.ts +21 -5
- package/src/tui/index.ts +5 -4
- package/src/web/index.ts +5 -5
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../../src/agent/providers/openai.ts"],"names":[],"mappings":"AAQA,OAAO,EAAY,KAAK,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACpE,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAclE,qBAAa,cAAe,YAAW,WAAW;IAChD,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,KAAK,CAAQ;IACrB,OAAO,CAAC,QAAQ,CAAC,CAAS;IAC1B,OAAO,CAAC,eAAe,CAAC,CAAQ;IAChC,OAAO,CAAC,WAAW,CAAS;IAC5B,6DAA6D;IAC7D,OAAO,CAAC,WAAW,CAAS;gBAG1B,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,MAAM,EAChB,KAAK,CAAC,EAAE,MAAM,EACd,QAAQ,CAAC,EAAE,OAAO,EAClB,eAAe,CAAC,EAAE,MAAM,EACxB,WAAW,CAAC,EAAE,OAAO,EACrB,WAAW,CAAC,EAAE,OAAO;IAWhB,IAAI,CACT,QAAQ,EAAE,UAAU,EAAE,EACtB,KAAK,EAAE,cAAc,EAAE,EACvB,MAAM,CAAC,EAAE,WAAW,GACnB,cAAc,CAAC,QAAQ,CAAC;
|
|
1
|
+
{"version":3,"file":"openai.d.ts","sourceRoot":"","sources":["../../../src/agent/providers/openai.ts"],"names":[],"mappings":"AAQA,OAAO,EAAY,KAAK,cAAc,EAAE,MAAM,sBAAsB,CAAA;AACpE,OAAO,KAAK,EAAE,QAAQ,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,WAAW,CAAA;AAclE,qBAAa,cAAe,YAAW,WAAW;IAChD,OAAO,CAAC,MAAM,CAAQ;IACtB,OAAO,CAAC,KAAK,CAAQ;IACrB,OAAO,CAAC,QAAQ,CAAC,CAAS;IAC1B,OAAO,CAAC,eAAe,CAAC,CAAQ;IAChC,OAAO,CAAC,WAAW,CAAS;IAC5B,6DAA6D;IAC7D,OAAO,CAAC,WAAW,CAAS;gBAG1B,MAAM,EAAE,MAAM,EACd,OAAO,CAAC,EAAE,MAAM,EAChB,KAAK,CAAC,EAAE,MAAM,EACd,QAAQ,CAAC,EAAE,OAAO,EAClB,eAAe,CAAC,EAAE,MAAM,EACxB,WAAW,CAAC,EAAE,OAAO,EACrB,WAAW,CAAC,EAAE,OAAO;IAWhB,IAAI,CACT,QAAQ,EAAE,UAAU,EAAE,EACtB,KAAK,EAAE,cAAc,EAAE,EACvB,MAAM,CAAC,EAAE,WAAW,GACnB,cAAc,CAAC,QAAQ,CAAC;CAwR5B"}
|
|
@@ -137,12 +137,21 @@ export class OpenAIProvider {
|
|
|
137
137
|
for (const tc of delta.tool_calls) {
|
|
138
138
|
if (tc.id) {
|
|
139
139
|
if (currentToolCall) {
|
|
140
|
+
let input;
|
|
141
|
+
const rawArgs = currentToolCall.arguments || '';
|
|
142
|
+
try {
|
|
143
|
+
input = JSON.parse(rawArgs || '{}');
|
|
144
|
+
}
|
|
145
|
+
catch {
|
|
146
|
+
console.error('[openai] Failed to parse tool_call arguments (flush on new tool):', rawArgs);
|
|
147
|
+
input = {};
|
|
148
|
+
}
|
|
140
149
|
yield {
|
|
141
150
|
type: 'tool_use',
|
|
142
151
|
tool_call: {
|
|
143
152
|
id: currentToolCall.id,
|
|
144
153
|
name: currentToolCall.name,
|
|
145
|
-
input
|
|
154
|
+
input,
|
|
146
155
|
},
|
|
147
156
|
reasoning_content: reasoningContent,
|
|
148
157
|
};
|
|
@@ -161,18 +170,20 @@ export class OpenAIProvider {
|
|
|
161
170
|
}
|
|
162
171
|
if (chunk.choices?.[0]?.finish_reason) {
|
|
163
172
|
if (currentToolCall) {
|
|
173
|
+
const finalArgs = currentToolCall.arguments || '';
|
|
164
174
|
try {
|
|
165
175
|
yield {
|
|
166
176
|
type: 'tool_use',
|
|
167
177
|
tool_call: {
|
|
168
178
|
id: currentToolCall.id,
|
|
169
179
|
name: currentToolCall.name,
|
|
170
|
-
input: JSON.parse(
|
|
180
|
+
input: JSON.parse(finalArgs || '{}'),
|
|
171
181
|
},
|
|
172
182
|
reasoning_content: reasoningContent,
|
|
173
183
|
};
|
|
174
184
|
}
|
|
175
185
|
catch {
|
|
186
|
+
console.error('[openai] Failed to parse tool_call arguments (finish_reason):', finalArgs);
|
|
176
187
|
yield {
|
|
177
188
|
type: 'tool_use',
|
|
178
189
|
tool_call: {
|
|
@@ -232,12 +243,21 @@ export class OpenAIProvider {
|
|
|
232
243
|
output_tokens: lastUsage.completion_tokens ?? 0,
|
|
233
244
|
}
|
|
234
245
|
: undefined;
|
|
246
|
+
let input;
|
|
247
|
+
const rawFinalArgs = currentToolCall.arguments || '';
|
|
248
|
+
try {
|
|
249
|
+
input = JSON.parse(rawFinalArgs || '{}');
|
|
250
|
+
}
|
|
251
|
+
catch {
|
|
252
|
+
console.error('[openai] Failed to parse tool_call arguments (final flush):', rawFinalArgs);
|
|
253
|
+
input = {};
|
|
254
|
+
}
|
|
235
255
|
yield {
|
|
236
256
|
type: 'tool_use',
|
|
237
257
|
tool_call: {
|
|
238
258
|
id: currentToolCall.id,
|
|
239
259
|
name: currentToolCall.name,
|
|
240
|
-
input
|
|
260
|
+
input,
|
|
241
261
|
},
|
|
242
262
|
reasoning_content: reasoningContent,
|
|
243
263
|
usage,
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"openai.js","sourceRoot":"","sources":["../../../src/agent/providers/openai.ts"],"names":[],"mappings":"AAAA,OAAO,MAAM,MAAM,QAAQ,CAAA;AAuB3B,MAAM,OAAO,cAAc;IACjB,MAAM,CAAQ;IACd,KAAK,CAAQ;IACb,QAAQ,CAAU;IAClB,eAAe,CAAS;IACxB,WAAW,CAAS;IAC5B,6DAA6D;IACrD,WAAW,CAAS;IAE5B,YACE,MAAc,EACd,OAAgB,EAChB,KAAc,EACd,QAAkB,EAClB,eAAwB,EACxB,WAAqB,EACrB,WAAqB;QAErB,IAAI,CAAC,MAAM,GAAG,IAAI,MAAM,CAAC,EAAE,MAAM,EAAE,OAAO,EAAE,CAAC,CAAA;QAC7C,IAAI,CAAC,KAAK,GAAG,KAAK,IAAI,QAAQ,CAAA;QAC9B,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAA;QACxB,IAAI,CAAC,eAAe,GAAG,eAAe,CAAA;QACtC,IAAI,CAAC,WAAW,GAAG,WAAW,IAAI,KAAK,CAAA;QACvC,4DAA4D;QAC5D,IAAI,CAAC,WAAW,GAAG,WAAW,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAA;IAC7E,CAAC;IAED,KAAK,CAAC,CAAC,IAAI,CACT,QAAsB,EACtB,KAAuB,EACvB,MAAoB;QAEpB,MAAM,oBAAoB,GAAyB,KAAK,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE;YAC/D,MAAM,UAAU,GAA4B,EAAE,CAAA;YAC9C,KAAK,MAAM,CAAC,GAAG,EAAE,KAAK,CAAC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EAAE,eAAe,CAAC,aAAa;gBAC5C,iBAAiB,EAAE,eAAe,CAAC,iBAAiB;gBACpD,KAAK;aACN,CAAA;YACD,eAAe,GAAG,IAAI,CAAA;QACxB,CAAC;QAED,IAAI,eAAe,EAAE,CAAC;YACpB,MAAM,KAAK,GAAG,SAAS;gBACrB,CAAC,CAAC;oBACE,YAAY,EAAE,SAAS,CAAC,aAAa,IAAI,CAAC;oBAC1C,aAAa,EAAE,SAAS,CAAC,iBAAiB,IAAI,CAAC;iBAChD;gBACH,CAAC,CAAC,SAAS,CAAA;YACb,IAAI,KAA8B,CAAA;YAClC,MAAM,YAAY,GAAG,eAAe,CAAC,SAAS,IAAI,EAAE,CAAA;YACpD,IAAI,CAAC;gBACH,KAAK,GAAG,IAAI,CAAC,KAAK,CAAC,YAAY,IAAI,IAAI,CAAC,CAAA;YAC1C,CAAC;YAAC,MAAM,CAAC;gBACP,OAAO,CAAC,KAAK,CAAC,6DAA6D,EAAE,YAAY,CAAC,CAAA;gBAC1F,KAAK,GAAG,EAAE,CAAA;YACZ,CAAC;YACD,MAAM;gBACJ,IAAI,EAAE,UAAU;gBAChB,SAAS,EAAE;oBACT,EAAE,EAAE,eAAe,CAAC,EAAE;oBACtB,IAAI,EAAE,eAAe,CAAC,IAAI;oBAC1B,KAAK;iBACN;gBACD,iBAAiB,EAAE,gBAAgB;gBACnC,KAAK;aACN,CAAA;QACH,CAAC;IACH,CAAC;CACF"}
|
package/dist/agent/session.d.ts
CHANGED
|
@@ -34,13 +34,15 @@ export declare class Session {
|
|
|
34
34
|
private stopped;
|
|
35
35
|
private abortController;
|
|
36
36
|
constructor(config: Config, output?: SessionOutput);
|
|
37
|
+
/** Initialize the system prompt asynchronously */
|
|
38
|
+
private initSystemPrompt;
|
|
37
39
|
/** Persist the current session to ~/.lonny/sessions/ */
|
|
38
40
|
save(): void;
|
|
39
41
|
/** Try to load a saved session for the given cwd. Returns null if none exists. */
|
|
40
|
-
static load(config: Config, output?: SessionOutput): Session | null
|
|
42
|
+
static load(config: Config, output?: SessionOutput): Promise<Session | null>;
|
|
41
43
|
/** Remove the saved session file for the given cwd. */
|
|
42
44
|
static clearSavedSession(cwd: string): void;
|
|
43
|
-
setMode(mode: 'code' | 'plan' | 'ask'): void
|
|
45
|
+
setMode(mode: 'code' | 'plan' | 'ask'): Promise<void>;
|
|
44
46
|
/** Stop the current conversation gracefully */
|
|
45
47
|
stop(): void;
|
|
46
48
|
/** Check if the session was stopped */
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/agent/session.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAEhD,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;
|
|
1
|
+
{"version":3,"file":"session.d.ts","sourceRoot":"","sources":["../../src/agent/session.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAAE,MAAM,EAAE,MAAM,oBAAoB,CAAA;AAEhD,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAA;AAElD,OAAO,EAAE,YAAY,EAAE,MAAM,sBAAsB,CAAA;AACnD,OAAO,KAAK,EAAE,QAAQ,EAAc,MAAM,mBAAmB,CAAA;AAG7D,OAAO,KAAK,EAAE,UAAU,EAAE,WAAW,EAAE,MAAM,UAAU,CAAA;AAmFvD,MAAM,WAAW,aAAa;IAC5B,KAAK,EAAE,CAAC,IAAI,EAAE,MAAM,KAAK,IAAI,CAAA;IAC7B,6FAA6F;IAC7F,kBAAkB,CAAC,EAAE,OAAO,CAAA;IAC5B;;;OAGG;IACH,WAAW,CAAC,EAAE,CAAC,SAAS,EAAE,QAAQ,EAAE,KAAK,OAAO,CAAC,OAAO,CAAC,CAAA;CAC1D;AAqFD,wBAAgB,eAAe,CAAC,EAAE,EAAE,QAAQ,GAAG,MAAM,CAyBpD;AAoBD,qBAAa,OAAO;IAClB,QAAQ,EAAE,UAAU,EAA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CAAC,UAAU,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC;CA2Z9C"}
|
package/dist/agent/session.js
CHANGED
|
@@ -4,6 +4,7 @@ import * as os from 'node:os';
|
|
|
4
4
|
import * as path from 'node:path';
|
|
5
5
|
import { saveTokenUsage } from '../config/tokens.js';
|
|
6
6
|
import { FileReadTracker } from '../diff/apply.js';
|
|
7
|
+
import { fmtErr } from '../tools/errors.js';
|
|
7
8
|
import { ToolRegistry } from '../tools/registry.js';
|
|
8
9
|
import { compact, shouldCompact } from './compaction.js';
|
|
9
10
|
import { EventChannels, getGlobalEventBus } from './event-bus.js';
|
|
@@ -241,7 +242,16 @@ export class Session {
|
|
|
241
242
|
else {
|
|
242
243
|
this.provider = new AnthropicProvider(config.apiKey, config.baseUrl, config.model);
|
|
243
244
|
}
|
|
244
|
-
|
|
245
|
+
// Placeholder until initSystemPrompt() is called
|
|
246
|
+
this.messages = [{ role: 'system', content: '' }];
|
|
247
|
+
// Initialize system prompt asynchronously
|
|
248
|
+
this.initSystemPrompt(config);
|
|
249
|
+
}
|
|
250
|
+
/** Initialize the system prompt asynchronously */
|
|
251
|
+
initSystemPrompt(config) {
|
|
252
|
+
buildSystemPrompt(config).then(prompt => {
|
|
253
|
+
this.messages = [{ role: 'system', content: prompt }];
|
|
254
|
+
});
|
|
245
255
|
}
|
|
246
256
|
/** Persist the current session to ~/.lonny/sessions/ */
|
|
247
257
|
save() {
|
|
@@ -262,7 +272,7 @@ export class Session {
|
|
|
262
272
|
fs.writeFileSync(filePath, JSON.stringify(data, null, 2), 'utf-8');
|
|
263
273
|
}
|
|
264
274
|
/** Try to load a saved session for the given cwd. Returns null if none exists. */
|
|
265
|
-
static load(config, output) {
|
|
275
|
+
static async load(config, output) {
|
|
266
276
|
const filePath = getSessionFilePath(config.cwd);
|
|
267
277
|
let data;
|
|
268
278
|
try {
|
|
@@ -285,7 +295,8 @@ export class Session {
|
|
|
285
295
|
if (data.model !== config.model ||
|
|
286
296
|
data.provider !== config.provider ||
|
|
287
297
|
data.mode !== config.mode) {
|
|
288
|
-
|
|
298
|
+
const prompt = await buildSystemPrompt(config);
|
|
299
|
+
session.messages[0] = { role: 'system', content: prompt };
|
|
289
300
|
}
|
|
290
301
|
// Restore token stats
|
|
291
302
|
session.totalInputTokens = data.totalInputTokens;
|
|
@@ -303,9 +314,10 @@ export class Session {
|
|
|
303
314
|
// Ignore if file doesn't exist
|
|
304
315
|
}
|
|
305
316
|
}
|
|
306
|
-
setMode(mode) {
|
|
317
|
+
async setMode(mode) {
|
|
307
318
|
this.config.mode = mode;
|
|
308
|
-
|
|
319
|
+
const prompt = await buildSystemPrompt(this.config);
|
|
320
|
+
this.messages[0] = { role: 'system', content: prompt };
|
|
309
321
|
this.registry.setMode(mode);
|
|
310
322
|
this.save();
|
|
311
323
|
}
|
|
@@ -322,6 +334,8 @@ export class Session {
|
|
|
322
334
|
/** Reset the stopped flag for a new conversation */
|
|
323
335
|
resetStopped() {
|
|
324
336
|
this.stopped = false;
|
|
337
|
+
// Create new AbortController for next conversation
|
|
338
|
+
this.abortController = new AbortController();
|
|
325
339
|
}
|
|
326
340
|
async chat(userPrompt) {
|
|
327
341
|
const bus = getGlobalEventBus();
|
|
@@ -361,171 +375,205 @@ export class Session {
|
|
|
361
375
|
bus.emit(EventChannels.TURN_START, { prompt: userPrompt, iteration: iterations });
|
|
362
376
|
bus.emit(EventChannels.LLM_STREAM_START, { iteration: iterations });
|
|
363
377
|
const stream = this.provider.chat(this.messages, this.registry.getDefinitions(), this.abortController.signal);
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
reasoningOutput
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
}
|
|
378
|
-
// Terminal display with box drawing (skip in Web UI mode, handled by EventBus)
|
|
379
|
-
if (!out?.suppressToolOutput) {
|
|
380
|
-
// Track column position on current line for wrapping
|
|
381
|
-
let thinkCol = 0;
|
|
382
|
-
// Handle newlines in streamed content - add left border on each new line
|
|
383
|
-
// Also manually wrap long lines so wrapped lines keep the │ prefix.
|
|
384
|
-
let remaining = chunk.reasoning_content;
|
|
385
|
-
const maxContentWidth = termWidth() - THINK_PREFIX_WIDTH;
|
|
386
|
-
while (remaining.length > 0) {
|
|
387
|
-
if (reasoningLineStart) {
|
|
388
|
-
writeOut(` ${GY}│${RS}${TH}`, out);
|
|
389
|
-
reasoningLineStart = false;
|
|
390
|
-
thinkCol = 0;
|
|
378
|
+
try {
|
|
379
|
+
for await (const chunk of stream) {
|
|
380
|
+
if (chunk.reasoning_content) {
|
|
381
|
+
reasoningContent = chunk.reasoning_content;
|
|
382
|
+
// Stream reasoning content in real-time (only when no text in same chunk)
|
|
383
|
+
if (!chunk.text) {
|
|
384
|
+
// Emit thinking via EventBus for Web UI
|
|
385
|
+
bus.emit(EventChannels.THINKING, { text: chunk.reasoning_content });
|
|
386
|
+
if (!reasoningOutput) {
|
|
387
|
+
reasoningOutput = true;
|
|
388
|
+
reasoningLineStart = true;
|
|
389
|
+
if (!out?.suppressToolOutput) {
|
|
390
|
+
writeOut(thinkTopBorder(), out);
|
|
391
391
|
}
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
392
|
+
}
|
|
393
|
+
// Terminal display with box drawing (skip in Web UI mode, handled by EventBus)
|
|
394
|
+
if (!out?.suppressToolOutput) {
|
|
395
|
+
// Track column position on current line for wrapping
|
|
396
|
+
let thinkCol = 0;
|
|
397
|
+
// Handle newlines in streamed content - add left border on each new line
|
|
398
|
+
// Also manually wrap long lines so wrapped lines keep the │ prefix.
|
|
399
|
+
let remaining = chunk.reasoning_content;
|
|
400
|
+
const maxContentWidth = termWidth() - THINK_PREFIX_WIDTH;
|
|
401
|
+
while (remaining.length > 0) {
|
|
402
|
+
if (reasoningLineStart) {
|
|
403
|
+
writeOut(` ${GY}│${RS}${TH}`, out);
|
|
404
|
+
reasoningLineStart = false;
|
|
405
|
+
thinkCol = 0;
|
|
406
|
+
}
|
|
407
|
+
const nlIdx = remaining.indexOf('\n');
|
|
408
|
+
if (nlIdx === -1) {
|
|
409
|
+
// No newline — write as much as fits on current line, wrap if needed
|
|
410
|
+
while (remaining.length > 0) {
|
|
411
|
+
const segWidth = visibleWidth(remaining);
|
|
412
|
+
const avail = maxContentWidth - thinkCol;
|
|
413
|
+
if (segWidth <= avail) {
|
|
414
|
+
// Fits entirely on current line
|
|
415
|
+
writeOut(remaining, out);
|
|
416
|
+
thinkCol += segWidth;
|
|
417
|
+
remaining = '';
|
|
418
|
+
}
|
|
419
|
+
else if (avail <= 0) {
|
|
420
|
+
// Current line is full, wrap to next
|
|
421
|
+
writeOut(`${RS}\n`, out);
|
|
422
|
+
writeOut(` ${GY}│${RS}${TH}`, out);
|
|
423
|
+
thinkCol = 0;
|
|
424
|
+
}
|
|
425
|
+
else {
|
|
426
|
+
// Write first part that fits, then wrap
|
|
427
|
+
// Find character boundary that fits within avail
|
|
428
|
+
let cut = avail;
|
|
429
|
+
while (cut > 0 && visibleWidth(remaining.slice(0, cut)) > avail)
|
|
430
|
+
cut--;
|
|
431
|
+
if (cut <= 0)
|
|
432
|
+
cut = 1;
|
|
433
|
+
writeOut(remaining.slice(0, cut), out);
|
|
434
|
+
writeOut(`${RS}\n`, out);
|
|
435
|
+
writeOut(` ${GY}│${RS}${TH}`, out);
|
|
436
|
+
thinkCol = 0;
|
|
437
|
+
remaining = remaining.slice(cut);
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
else {
|
|
442
|
+
// Has newline — process the segment up to newline
|
|
443
|
+
const segment = remaining.slice(0, nlIdx);
|
|
444
|
+
const segWidth = visibleWidth(segment);
|
|
397
445
|
const avail = maxContentWidth - thinkCol;
|
|
398
446
|
if (segWidth <= avail) {
|
|
399
|
-
//
|
|
400
|
-
writeOut(
|
|
401
|
-
thinkCol += segWidth;
|
|
402
|
-
remaining = '';
|
|
403
|
-
}
|
|
404
|
-
else if (avail <= 0) {
|
|
405
|
-
// Current line is full, wrap to next
|
|
447
|
+
// Segment fits on current line
|
|
448
|
+
writeOut(segment, out);
|
|
406
449
|
writeOut(`${RS}\n`, out);
|
|
407
|
-
|
|
450
|
+
reasoningLineStart = true;
|
|
408
451
|
thinkCol = 0;
|
|
409
452
|
}
|
|
410
453
|
else {
|
|
411
|
-
//
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
cut
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
454
|
+
// Segment too long — write what fits, wrap, then rest
|
|
455
|
+
let rest = segment;
|
|
456
|
+
// Write remainder of current line
|
|
457
|
+
if (avail > 0) {
|
|
458
|
+
let cut = avail;
|
|
459
|
+
while (cut > 0 && visibleWidth(rest.slice(0, cut)) > avail)
|
|
460
|
+
cut--;
|
|
461
|
+
if (cut <= 0)
|
|
462
|
+
cut = 1;
|
|
463
|
+
writeOut(rest.slice(0, cut), out);
|
|
464
|
+
rest = rest.slice(cut);
|
|
465
|
+
}
|
|
419
466
|
writeOut(`${RS}\n`, out);
|
|
420
|
-
|
|
467
|
+
reasoningLineStart = true;
|
|
421
468
|
thinkCol = 0;
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
while (cut > 0 && visibleWidth(rest.slice(0, cut)) > avail)
|
|
445
|
-
cut--;
|
|
446
|
-
if (cut <= 0)
|
|
447
|
-
cut = 1;
|
|
448
|
-
writeOut(rest.slice(0, cut), out);
|
|
449
|
-
rest = rest.slice(cut);
|
|
450
|
-
}
|
|
451
|
-
writeOut(`${RS}\n`, out);
|
|
452
|
-
reasoningLineStart = true;
|
|
453
|
-
thinkCol = 0;
|
|
454
|
-
// Write rest of segment on continuation line(s)
|
|
455
|
-
if (rest.length > 0) {
|
|
456
|
-
writeOut(` ${GY}│${RS}${TH}`, out);
|
|
457
|
-
reasoningLineStart = false;
|
|
458
|
-
while (rest.length > 0) {
|
|
459
|
-
const rw = visibleWidth(rest);
|
|
460
|
-
if (rw <= maxContentWidth) {
|
|
461
|
-
writeOut(rest, out);
|
|
462
|
-
thinkCol = rw;
|
|
463
|
-
rest = '';
|
|
464
|
-
}
|
|
465
|
-
else {
|
|
466
|
-
let cut = maxContentWidth;
|
|
467
|
-
while (cut > 0 && visibleWidth(rest.slice(0, cut)) > maxContentWidth)
|
|
468
|
-
cut--;
|
|
469
|
-
if (cut <= 0)
|
|
470
|
-
cut = 1;
|
|
471
|
-
writeOut(rest.slice(0, cut), out);
|
|
472
|
-
writeOut(`${RS}\n`, out);
|
|
473
|
-
writeOut(` ${GY}│${RS}${TH}`, out);
|
|
474
|
-
rest = rest.slice(cut);
|
|
469
|
+
// Write rest of segment on continuation line(s)
|
|
470
|
+
if (rest.length > 0) {
|
|
471
|
+
writeOut(` ${GY}│${RS}${TH}`, out);
|
|
472
|
+
reasoningLineStart = false;
|
|
473
|
+
while (rest.length > 0) {
|
|
474
|
+
const rw = visibleWidth(rest);
|
|
475
|
+
if (rw <= maxContentWidth) {
|
|
476
|
+
writeOut(rest, out);
|
|
477
|
+
thinkCol = rw;
|
|
478
|
+
rest = '';
|
|
479
|
+
}
|
|
480
|
+
else {
|
|
481
|
+
let cut = maxContentWidth;
|
|
482
|
+
while (cut > 0 && visibleWidth(rest.slice(0, cut)) > maxContentWidth)
|
|
483
|
+
cut--;
|
|
484
|
+
if (cut <= 0)
|
|
485
|
+
cut = 1;
|
|
486
|
+
writeOut(rest.slice(0, cut), out);
|
|
487
|
+
writeOut(`${RS}\n`, out);
|
|
488
|
+
writeOut(` ${GY}│${RS}${TH}`, out);
|
|
489
|
+
rest = rest.slice(cut);
|
|
490
|
+
}
|
|
475
491
|
}
|
|
476
492
|
}
|
|
493
|
+
writeOut(`${RS}\n`, out);
|
|
494
|
+
reasoningLineStart = true;
|
|
495
|
+
thinkCol = 0;
|
|
477
496
|
}
|
|
478
|
-
|
|
479
|
-
reasoningLineStart = true;
|
|
480
|
-
thinkCol = 0;
|
|
497
|
+
remaining = remaining.slice(nlIdx + 1);
|
|
481
498
|
}
|
|
482
|
-
remaining = remaining.slice(nlIdx + 1);
|
|
483
499
|
}
|
|
484
500
|
}
|
|
485
501
|
}
|
|
486
502
|
}
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
503
|
+
if (chunk.type === 'text' && chunk.text) {
|
|
504
|
+
if (reasoningOutput) {
|
|
505
|
+
bus.emit(EventChannels.THINKING_END, {});
|
|
506
|
+
if (!out?.suppressToolOutput) {
|
|
507
|
+
writeOut(`${RS}\n`, out);
|
|
508
|
+
writeOut(thinkBottomBorder(), out);
|
|
509
|
+
}
|
|
510
|
+
reasoningOutput = false;
|
|
511
|
+
reasoningLineStart = false;
|
|
494
512
|
}
|
|
495
|
-
|
|
496
|
-
|
|
513
|
+
fullResponse += chunk.text;
|
|
514
|
+
writeOut(chunk.text, out);
|
|
497
515
|
}
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
}
|
|
501
|
-
else if (chunk.type === 'tool_use' && chunk.tool_call) {
|
|
502
|
-
toolCalls.push(chunk.tool_call);
|
|
503
|
-
}
|
|
504
|
-
else if (chunk.type === 'complete') {
|
|
505
|
-
if (chunk.usage) {
|
|
506
|
-
this.turnInputTokens += chunk.usage.input_tokens;
|
|
507
|
-
this.turnOutputTokens += chunk.usage.output_tokens;
|
|
508
|
-
this.totalInputTokens += chunk.usage.input_tokens;
|
|
509
|
-
this.totalOutputTokens += chunk.usage.output_tokens;
|
|
516
|
+
else if (chunk.type === 'tool_use' && chunk.tool_call) {
|
|
517
|
+
toolCalls.push(chunk.tool_call);
|
|
510
518
|
}
|
|
511
|
-
if (chunk.
|
|
512
|
-
if (
|
|
513
|
-
|
|
514
|
-
|
|
515
|
-
|
|
516
|
-
|
|
517
|
-
|
|
518
|
-
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
519
|
+
else if (chunk.type === 'complete') {
|
|
520
|
+
if (chunk.usage) {
|
|
521
|
+
this.turnInputTokens += chunk.usage.input_tokens;
|
|
522
|
+
this.turnOutputTokens += chunk.usage.output_tokens;
|
|
523
|
+
this.totalInputTokens += chunk.usage.input_tokens;
|
|
524
|
+
this.totalOutputTokens += chunk.usage.output_tokens;
|
|
525
|
+
}
|
|
526
|
+
if (chunk.finish_reason === 'stop' || chunk.finish_reason === 'end_turn') {
|
|
527
|
+
if (toolCalls.length === 0) {
|
|
528
|
+
const finalAssistantMsg = {
|
|
529
|
+
role: 'assistant',
|
|
530
|
+
content: fullResponse || null,
|
|
531
|
+
reasoning_content: reasoningContent,
|
|
532
|
+
};
|
|
533
|
+
this.messages.push(finalAssistantMsg);
|
|
534
|
+
printTokenStats(this.turnInputTokens, this.turnOutputTokens, this.totalInputTokens, this.totalOutputTokens, this.turnApiCalls, this.totalApiCalls, out);
|
|
535
|
+
writeOut('\n\n', out);
|
|
536
|
+
saveTokenUsage(this.config.cwd, this.turnInputTokens, this.turnOutputTokens, this.turnApiCalls);
|
|
537
|
+
bus.emit(EventChannels.TURN_END, { iterations, toolCallCount: 0 });
|
|
538
|
+
this.save();
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
525
541
|
}
|
|
526
542
|
}
|
|
527
543
|
}
|
|
528
544
|
}
|
|
545
|
+
catch (e) {
|
|
546
|
+
const errMsg = fmtErr(e);
|
|
547
|
+
const partialContent = fullResponse ? fullResponse.slice(0, 500) : '(empty)';
|
|
548
|
+
if (!out?.suppressToolOutput) {
|
|
549
|
+
writeOut(`\n${RE}Stream error:${RS} ${errMsg}`, out);
|
|
550
|
+
writeOut(`\n ${GY}┃${RS} Partial response: ${partialContent}\n`, out);
|
|
551
|
+
}
|
|
552
|
+
console.error('[session] Stream error:', errMsg, '| Partial response:', partialContent);
|
|
553
|
+
if (reasoningOutput) {
|
|
554
|
+
bus.emit(EventChannels.THINKING_END, {});
|
|
555
|
+
if (!out?.suppressToolOutput) {
|
|
556
|
+
writeOut(`${RS}\n`, out);
|
|
557
|
+
writeOut(thinkBottomBorder(), out);
|
|
558
|
+
}
|
|
559
|
+
}
|
|
560
|
+
bus.emit(EventChannels.LLM_STREAM_END, { iteration: iterations, toolCallCount: 0 });
|
|
561
|
+
bus.emit(EventChannels.TURN_END, { iterations, toolCallCount: 0 });
|
|
562
|
+
// If there are pending toolCalls that weren't executed due to abort,
|
|
563
|
+
// add an assistant message so the model knows they weren't executed
|
|
564
|
+
if (toolCalls.length > 0) {
|
|
565
|
+
const interruptedMsg = {
|
|
566
|
+
role: 'assistant',
|
|
567
|
+
content: null,
|
|
568
|
+
tool_calls: toolCalls,
|
|
569
|
+
reasoning_content: reasoningContent,
|
|
570
|
+
};
|
|
571
|
+
this.messages.push(interruptedMsg);
|
|
572
|
+
}
|
|
573
|
+
saveTokenUsage(this.config.cwd, this.turnInputTokens, this.turnOutputTokens, this.turnApiCalls);
|
|
574
|
+
this.save();
|
|
575
|
+
return;
|
|
576
|
+
}
|
|
529
577
|
bus.emit(EventChannels.LLM_STREAM_END, {
|
|
530
578
|
iteration: iterations,
|
|
531
579
|
toolCallCount: toolCalls.length,
|
|
@@ -583,9 +631,20 @@ export class Session {
|
|
|
583
631
|
}
|
|
584
632
|
}
|
|
585
633
|
}
|
|
586
|
-
for (
|
|
634
|
+
for (let i = 0; i < toolCalls.length; i++) {
|
|
635
|
+
const tc = toolCalls[i];
|
|
587
636
|
// Check if stop was requested after each tool call
|
|
588
637
|
if (this.isStopped()) {
|
|
638
|
+
// Add assistant message with remaining tool_calls so model knows they weren't executed
|
|
639
|
+
const remainingToolCalls = toolCalls.slice(i);
|
|
640
|
+
if (remainingToolCalls.length > 0) {
|
|
641
|
+
const interruptedMsg = {
|
|
642
|
+
role: 'assistant',
|
|
643
|
+
content: null,
|
|
644
|
+
tool_calls: remainingToolCalls,
|
|
645
|
+
};
|
|
646
|
+
this.messages.push(interruptedMsg);
|
|
647
|
+
}
|
|
589
648
|
bus.emit(EventChannels.TURN_END, { iterations, toolCallCount: toolCalls.length });
|
|
590
649
|
this.save();
|
|
591
650
|
return;
|