-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent-session.mjs
More file actions
651 lines (590 loc) · 24.2 KB
/
Copy pathagent-session.mjs
File metadata and controls
651 lines (590 loc) · 24.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
import { extractTextFromResponse, extractUsage } from './response.mjs';
import { dedupeToolCalls, dedupeToolOutputs, requiresToolConfirmation, runToolCall, toolCallIdentity, toolOutputForCall } from './tool-dispatch.mjs';
import { applyFirstUserMessage, buildInputMessage } from './prompt-builder.mjs';
import { clearSession, persistCheckpoint, persistResponseState, readLatestCheckpoint, readSessionState } from './session-state.mjs';
import { formatTurnUsageReport, formatUsageReport } from './usage.mjs';
import { createUsageTotals } from './response.mjs';
import { formatCommandMessage, formatInfoMessage, formatMcpMessage, formatSystemMessage } from './shell-display.mjs';
const SHELL_OUTPUT_PREVIEW = 120;
const STATUS_UPDATE_INTERVAL_MS = 250;
const GREEN = '\u001b[32m';
const PINK = '\u001b[95m';
const LIGHT_ORANGE = '\u001b[38;5;214m';
const RESET = '\u001b[0m';
function formatElapsedStatus(elapsedMs) {
const totalSeconds = Math.max(0, Math.round(Number(elapsedMs ?? 0) / 1000));
if (totalSeconds >= 60) {
const minutes = Math.floor(totalSeconds / 60);
const seconds = totalSeconds % 60;
return `${minutes}m ${seconds}s`;
}
return `${totalSeconds}s`;
}
function stripStatusValue(value) {
if (value && typeof value === 'object' && 'value' in value) return String(value.value ?? '');
if (typeof value !== 'string') return String(value ?? '');
return value.replace(/^([a-z]+):\s+/, '').replace(/\[[0-9;]*m/g, '');
}
// Produce a compact JSON message describing the transaction completion.
// Only include fields that have meaningful values; undefined or empty
// strings are omitted to avoid clutter in logs.
function formatTransactionCompletionMessage(summary) {
const obj = {};
if (summary?.time !== undefined && summary.time !== '') {
obj.time = String(summary.time);
}
const reasoning = stripStatusValue(summary?.reasoning);
if (reasoning) {
obj.reasoning = reasoning;
}
const writing = stripStatusValue(summary?.writing);
if (writing) {
obj.writing = writing;
}
const executing = stripStatusValue(summary?.executing);
if (executing) {
obj.executing = executing;
}
return JSON.stringify(obj);
}
function formatSpinnerFrame() {
return '';
}
function createStatusLineController(sessionStartedAt = Date.now(), { quiet = false, transitionOnly = false } = {}) {
let timer = null;
let lastRendered = '';
let state = null;
let stateStartedAt = 0;
let paused = false;
let suppressStatusAfterOutput = false;
const phases = {
reasoning: { lastMs: 0, totalMs: 0 },
executing: { lastMs: 0, totalMs: 0 },
writing: { lastMs: 0, totalMs: 0 },
};
function clearRenderedLine() {
// lastRendered is only set while the cursor is on our temporary status
// line. Clear that line, then leave the cursor at its beginning so the
// next status frame or streamed output owns the terminal position.
if (quiet || transitionOnly || !lastRendered) return;
process.stdout.write('\r\x1b[2K\r');
lastRendered = '';
}
function stopTimer() {
if (timer) clearInterval(timer);
timer = null;
}
function startTimer() {
if (quiet || transitionOnly || timer || paused) return;
timer = setInterval(render, STATUS_UPDATE_INTERVAL_MS);
}
function finalizeActive(now = Date.now()) {
if (!state) return;
const elapsed = Math.max(0, now - stateStartedAt);
const phase = phases[state];
phase.lastMs = elapsed;
phase.totalMs += elapsed;
}
function phaseSnapshot(name, now) {
const phase = phases[name];
const active = state === name;
const elapsed = active ? Math.max(0, now - stateStartedAt) : phase.lastMs;
const total = active ? phase.totalMs + elapsed : phase.totalMs;
return {
active,
value: `${formatElapsedStatus(elapsed)}/${formatElapsedStatus(total)}`,
};
}
function formatStatusField(name, snapshotValue) {
const field = `"${name}":"${snapshotValue.value}"`;
return snapshotValue.active ? `${GREEN}${field}${RESET}` : field;
}
function snapshot(now = Date.now()) {
return {
time: formatElapsedStatus(now - sessionStartedAt),
reasoning: phaseSnapshot('reasoning', now),
writing: phaseSnapshot('writing', now),
executing: phaseSnapshot('executing', now),
};
}
function writeLine(text) {
if (text === lastRendered) return;
if (transitionOnly) {
process.stdout.write(`${text}\n`);
lastRendered = text;
return;
}
clearRenderedLine();
process.stdout.write(text);
lastRendered = text;
}
function render() {
if (quiet || paused || !state || state === 'writing') return;
const stats = snapshot();
writeLine(`{"time":"${stats.time}",${formatStatusField('reasoning', stats.reasoning)},${formatStatusField('writing', stats.writing)},${formatStatusField('executing', stats.executing)}}`);
}
function prepareOutput() {
// Streamed command/text output can arrive while the controller is already
// in the writing phase (for example after a tool event). In that case
// transition() is a no-op, so explicitly remove the temporary status line
// before writing output.
clearRenderedLine();
}
function transition(nextState, { renderNow = true, allowStatusAfterOutput = false } = {}) {
const now = Date.now();
if (state === nextState) {
if (!paused && renderNow && (transitionOnly || state !== 'writing')) render();
return;
}
finalizeActive(now);
state = nextState;
stateStartedAt = now;
if (nextState === 'writing') {
stopTimer();
clearRenderedLine();
suppressStatusAfterOutput = true;
return;
}
if (suppressStatusAfterOutput && !allowStatusAfterOutput) {
stopTimer();
clearRenderedLine();
return;
}
suppressStatusAfterOutput = false;
if (paused) return;
startTimer();
if (renderNow) render();
}
return {
showReasoning(options) {
transition('reasoning', options);
},
showExecuting(done, total, options) {
transition('executing', options);
},
updateExecuting(_done, _total) {
if (state !== 'executing' || paused) return;
render();
},
beginWriting(options) {
prepareOutput();
transition('writing', options);
},
prepareOutput,
pause() {
paused = true;
stopTimer();
clearRenderedLine();
},
resume({ renderNow = true } = {}) {
if (!paused) return;
paused = false;
if (state && state !== 'writing') {
startTimer();
if (renderNow) render();
}
},
snapshot() {
return snapshot();
},
refresh() {
render();
},
clear() {
finalizeActive();
stopTimer();
// Clear the rendered status before dropping the phase state. If state is
// reset first, clearRenderedLine() can no longer tell that writing has
// started and may erase the final streamed response line.
clearRenderedLine();
state = null;
stateStartedAt = 0;
paused = false;
suppressStatusAfterOutput = false;
},
stop() {
// Failure path: unlike clear(), do not preserve a completion snapshot;
// only stop future renders and remove the temporary terminal line.
stopTimer();
clearRenderedLine();
state = null;
stateStartedAt = 0;
paused = true;
suppressStatusAfterOutput = false;
},
};
}
function textFromContent(content) {
if (content == null) return undefined;
const parts = [];
for (const part of content) {
if (part?.text) parts.push(String(part.text));
else if (part?.type === 'input_text' || part?.type === 'output_text') parts.push(String(part.text ?? ''));
else if (part?.type === 'refusal' && part.refusal) parts.push(`[refusal] ${part.refusal}`);
}
return parts.filter(Boolean).join('\n');
}
function compactJson(value) {
return JSON.stringify(value, (key, nested) => {
if (key === 'encrypted_content') return '[encrypted reasoning omitted]';
if (key === 'result' && typeof nested === 'string' && nested.length > 500) return `[large result omitted: ${nested.length} chars]`;
if (key === 'output' && Array.isArray(nested)) {
return nested.map((chunk) => {
if (!chunk || typeof chunk !== 'object') return chunk;
return {
stdout: String(chunk.stdout ?? '').slice(0, SHELL_OUTPUT_PREVIEW),
stderr: String(chunk.stderr ?? '').slice(0, SHELL_OUTPUT_PREVIEW),
outcome: chunk.outcome,
};
});
}
return nested;
});
}
function shellOutputPreview(item) {
const chunks = Array.isArray(item?.output) ? item.output : [];
return chunks.map((chunk) => ({
stdout: String(chunk?.stdout ?? '').slice(0, SHELL_OUTPUT_PREVIEW),
stderr: String(chunk?.stderr ?? '').slice(0, SHELL_OUTPUT_PREVIEW),
outcome: chunk?.outcome ?? null,
}));
}
function isShellToolCall(item) {
return item?.type === 'shell_call';
}
function isMcpToolCall(item) {
return item?.type === 'mcp_call';
}
export function responseItemToTranscript(item) {
if (!item || item.role === 'developer' || item.role === 'system') return '';
if (item.type === 'message') {
const role = item.role || 'message';
const text = textFromContent(item.content);
return text ? `${role}: ${text}` : '';
}
if (item.type === 'function_call') {
return `assistant tool call: ${item.name || 'function'}(${item.arguments || item.input || ''})`;
}
if (item.type === 'shell_call') {
return `assistant shell call: ${compactJson({ call_id: item.call_id, action: item.action, status: item.status })}`;
}
if (item.type === 'function_call_output') {
return `tool output: ${item.output ?? ''}`;
}
if (item.type === 'shell_call_output') {
return `tool output shell_call_output: ${compactJson({ call_id: item.call_id, max_output_length: item.max_output_length, status: item.status, output: shellOutputPreview(item) })}`;
}
if (item.type === 'reasoning') {
const summary = textFromContent(item.summary);
return summary ?? '';
}
if (item.type?.endsWith?.('_call')) {
return `assistant ${item.type}: ${compactJson(item)}`;
}
if (item.type?.endsWith?.('_call_output')) {
return `tool output ${item.type}: ${compactJson(item)}`;
}
return `${item.role || item.type || 'item'}: ${compactJson(item)}`;
}
function isResponseCompletedEvent(event, raw) {
if (event?.type === 'response.completed') return true;
return typeof raw === 'string' && raw.includes('"type":"response.completed"');
}
function isFunctionCallArgumentsDeltaEvent(event) {
return event?.type === 'response.function_call_arguments.delta';
}
function isShellCallCommandDeltaEvent(event) {
return event?.type === 'response.shell_call_command.delta';
}
function isWebSearchEvent(event) {
return typeof event?.type === 'string' && event.type.startsWith('response.web_search_call.');
}
function isMcpEvent(event) {
return typeof event?.type === 'string' && event.type.startsWith('response.mcp_');
}
function isReasoningSummaryEvent(event) {
return typeof event?.type === 'string' && event.type.startsWith('response.reasoning_summary_');
}
function colorizeReasoningSummary(text) {
return `${LIGHT_ORANGE}${text}${RESET}`;
}
function formatMcpProgress(event) {
const progress = event?.progress ?? event?.progress_update ?? event?.message ?? event?.data ?? event?.payload ?? event?.status ?? event?.delta;
if (progress === undefined || progress === null || progress === '') return '';
return JSON.stringify({ mcp: String(progress) });
}
function colorizePink(text) {
return `${PINK}${text}${RESET}`;
}
function webSearchStatusLine(stage) {
return colorizePink(JSON.stringify({ web_search: stage }));
}
function webSearchCompletionLine(item) {
const queries = Array.isArray(item?.action?.queries) ? item.action.queries.filter(Boolean).map(String) : [];
const sources = Array.isArray(item?.action?.sources)
? item.action.sources.map((source) => String(source?.url ?? source)).filter(Boolean)
: [];
if (queries.length === 0 && sources.length === 0) return '';
return colorizePink(JSON.stringify({
web_search: 'complete',
queries,
sources,
}, null, 2));
}
function createLiveResponseHandlers({ liveStreaming, statusController, debug = false }) {
let sawOutput = false;
let streamedText = '';
let streamedReasoningSummary = false;
const markOutput = () => {
if (sawOutput) return;
sawOutput = true;
statusController?.beginWriting();
};
const startWebSearch = (stage) => {
if (!statusController) return;
statusController.showExecuting(0, 0, { renderNow: false });
statusController.pause();
process.stdout.write(`${webSearchStatusLine(stage)}\n`);
};
const finishWebSearch = (item) => {
if (!statusController) return;
const completionLine = webSearchCompletionLine(item);
if (!completionLine) return;
statusController.showReasoning({ renderNow: false });
process.stdout.write(`${completionLine}\n`);
statusController.resume();
};
const showReasoningSummaryDelta = (delta) => {
if (delta === undefined || delta === null || delta === '') return;
streamedReasoningSummary = true;
statusController?.pause();
process.stdout.write(colorizeReasoningSummary(String(delta)));
};
const reasoningSummaryDelta = (event) => event?.delta ?? event?.text ?? event?.summary_text;
const finishReasoningSummary = () => {
if (!statusController) return;
process.stdout.write('\n');
// Do not immediately render a fresh status line after the summary's
// newline. The next shell-call delta will transition to writing and own
// the cursor without leaving a temporary line behind.
statusController.resume({ renderNow: false });
};
const handleMcpEvent = (event) => {
const type = String(event.type);
if (type.endsWith('.in_progress')) {
statusController?.showExecuting(0, 0);
return;
}
if (type.endsWith('.completed') || type.endsWith('.failed')) {
statusController?.showReasoning({ renderNow: false });
return;
}
if (type.includes('progress') || type.includes('update')) {
statusController?.showExecuting(0, 0, { renderNow: false });
const line = formatMcpProgress(event);
if (line) process.stdout.write(`${formatInfoMessage(line)}\n`);
}
};
return {
sawOutput: () => sawOutput,
streamedText: () => streamedText,
handlers: liveStreaming ? {
/* c8 ignore next 12 */
onEvent(event, message) {
if (isResponseCompletedEvent(event, message?.raw)) {
// Do not erase the line after the final text has been streamed. The
// cursor may already be at the start of the next line, which makes
// the terminal control sequence look like it removed the last line.
return;
}
if (isReasoningSummaryEvent(event)) {
if (debug) return;
if (event.type.endsWith('.delta')) showReasoningSummaryDelta(reasoningSummaryDelta(event));
else if (event.type.endsWith('.done')) finishReasoningSummary();
return;
}
if (isMcpEvent(event)) {
if (debug && (event.type === 'response.mcp_call_arguments.delta' || event.type === 'response.reasoning_summary_text.delta')) return;
if (event.type === 'response.mcp_call_arguments.delta') {
markOutput();
const delta = String(event?.delta ?? '');
if (delta) process.stdout.write(formatMcpMessage(delta));
return;
}
handleMcpEvent(event);
return;
}
if (isWebSearchEvent(event)) {
if (event.type.endsWith('.in_progress')) {
startWebSearch('in_progress');
return;
}
if (event.type.endsWith('.searching')) {
process.stdout.write(`${webSearchStatusLine('searching')}\n`);
return;
}
if (event.type.endsWith('.completed')) {
return;
}
}
if (isFunctionCallArgumentsDeltaEvent(event) || isShellCallCommandDeltaEvent(event)) {
markOutput();
const delta = String(event?.delta ?? '');
if (delta) {
streamedText += delta;
process.stdout.write(formatCommandMessage(delta));
}
}
},
onItemAdded(item) {
if (!isMcpToolCall(item)) return;
markOutput();
statusController?.pause();
statusController?.beginWriting();
const label = item.name || item.server_label || 'mcp_call';
process.stdout.write(formatMcpMessage(`${label}(`));
},
onTextDelta(delta) {
markOutput();
const text = String(delta ?? '');
streamedText += text;
process.stdout.write(text);
},
onItemDone(item) {
if (item?.type === 'web_search_call') {
finishWebSearch(item);
return;
}
if (isShellToolCall(item) || isMcpToolCall(item)) {
markOutput();
if (isMcpToolCall(item)) process.stdout.write(formatMcpMessage(')'));
streamedText += '\n';
process.stdout.write('\n');
if (isMcpToolCall(item)) {
// Keep the status line paused while the next response takes over
// the terminal. Resume then immediately pause only to preserve
// the controller transition for callers that observe it; do not
// allow a render between tool output and final answer.
statusController?.resume({ renderNow: false });
statusController?.pause();
}
}
if (item?.type === 'reasoning') {
if (debug) return;
const transcript = responseItemToTranscript(item);
if (transcript && !streamedReasoningSummary) process.stdout.write(`${formatSystemMessage(transcript)}\n`);
}
},
} : null,
};
}
async function createStreamedResponse(openai, request, { liveStreaming = false, statusController = null } = {}) {
if (liveStreaming) statusController?.showReasoning();
const live = createLiveResponseHandlers({ liveStreaming, statusController, ...(process.argv.includes('--debug') ? { debug: true } : {}) });
const response = await openai.responses.create(request, live.handlers || undefined);
if (liveStreaming && live.sawOutput()) {
// Clear any in-progress status line before adding the response terminator.
// Clearing afterwards can target the blank line following a response that
// already ended with a newline, making the last response line appear lost.
statusController?.clear();
if (!live.streamedText().endsWith('\n')) process.stdout.write('\n');
} else {
statusController?.clear();
}
return response;
}
export async function handleToolCalls(openai, response, baseRequest, cwd, onResponseUsage, runToolCallFn = runToolCall, streamOptions = {}) {
let current = response;
const liveStreaming = Boolean(streamOptions?.liveStreaming);
const sessionStartedAt = streamOptions?.sessionStartedAt ?? Date.now();
const statusController = streamOptions?.statusController || (liveStreaming ? createStatusLineController(sessionStartedAt, { quiet: Boolean(streamOptions?.suppressStatusOutput), transitionOnly: Boolean(streamOptions?.transitionOnlyStatus) }) : null);
const onResponseState = streamOptions?.onResponseState;
const skipInitialUsageAccounting = Boolean(streamOptions?.skipInitialUsageAccounting);
const onToolExecutionState = streamOptions?.onToolExecutionState;
const confirmToolCall = streamOptions?.confirmToolCall;
const yolo = Boolean(streamOptions?.yolo);
let isFirstResponse = true;
const executeToolCall = streamOptions?.runToolCall || runToolCallFn;
for (; ;) {
const shouldReportUsage = !(skipInitialUsageAccounting && isFirstResponse);
const usage = shouldReportUsage ? extractUsage(current) : createUsageTotals();
const calls = dedupeToolCalls((current?.output ?? []).filter((item) => isShellToolCall(item) || (item?.type === 'function_call' && ['spawn_agent', 'agent_status', 'cancel_agent'].includes(item?.name))), cwd);
const cumulativeUsage = shouldReportUsage && onResponseUsage ? onResponseUsage(usage, { skipIncrement: false }) : null;
if (onResponseState) {
await onResponseState({ response: current, pendingToolCalls: calls, isInitialResponse: isFirstResponse, cumulativeUsage });
}
if (shouldReportUsage) {
process.stdout.write(`${formatSystemMessage(formatTurnUsageReport({ ...usage, model: baseRequest?.model }))}\n`);
if (cumulativeUsage) {
process.stdout.write(`${formatSystemMessage(formatUsageReport({ ...cumulativeUsage, model: baseRequest?.model }))}\n`);
}
}
if (calls.length === 0) {
statusController?.clear();
process.stdout.write(`${formatInfoMessage(formatTransactionCompletionMessage(statusController?.snapshot?.() ?? { time: formatElapsedStatus(Date.now() - sessionStartedAt), reasoning: '0s/0s', writing: '0s/0s', executing: '0s/0s' }))}\n`);
return current;
}
isFirstResponse = false;
statusController?.showExecuting(0, calls.length);
const outputs = [];
let completed = 0;
try {
for (const [callIndex, call] of calls.entries()) {
let approved = true;
if (!yolo && requiresToolConfirmation(call) && confirmToolCall) {
statusController?.pause();
try { approved = await confirmToolCall(call, cwd); } finally { statusController?.resume({ renderNow: false }); }
}
if (!approved) {
outputs.push(toolOutputForCall(call, { type: 'shell_call_output', call_id: call.call_id || call.id || '', status: 'incomplete', output: [{ stdout: '', stderr: 'Tool execution declined by user.', outcome: { type: 'exit', exit_code: 1 } }] }));
continue;
}
await onToolExecutionState?.({ call, response: current, status: 'started', identity: toolCallIdentity(call, cwd), callIndex, callCount: calls.length });
const output = await executeToolCall(call, cwd, { isFirstResponse, currentResponse: current, callIndex, callCount: calls.length, statusController });
await onToolExecutionState?.({ call, response: current, status: 'completed', identity: toolCallIdentity(call, cwd), callIndex, callCount: calls.length });
outputs.push(toolOutputForCall(call, output));
completed += 1;
statusController?.updateExecuting(completed, calls.length);
}
} finally {
statusController?.clear();
}
const request = {
...baseRequest,
input: dedupeToolOutputs(outputs),
previous_response_id: current.id,
store: true,
};
try {
current = await createStreamedResponse(openai, request, liveStreaming ? { liveStreaming, statusController } : { statusController });
} catch (error) {
await streamOptions?.onRetryState?.({ request, response: current });
throw error;
}
}
}
export async function sendMessage(openai, template, previousResponseId, userMessage, agentsText, cwd, onResponseUsage, requestOverride = null, streamOptions = {}) {
const baseRequest = JSON.parse(JSON.stringify(template));
const sessionStartedAt = streamOptions?.sessionStartedAt ?? Date.now();
const statusController = streamOptions?.statusController || createStatusLineController(sessionStartedAt, { quiet: Boolean(streamOptions?.suppressStatusOutput), transitionOnly: Boolean(streamOptions?.transitionOnlyStatus) });
const request = requestOverride ? { ...baseRequest, ...requestOverride } : (previousResponseId
? {
...baseRequest,
input: [buildInputMessage(userMessage)],
store: true,
previous_response_id: previousResponseId,
}
: {
...applyFirstUserMessage(baseRequest, userMessage, agentsText, cwd),
store: true,
});
try {
const response = await createStreamedResponse(openai, request, streamOptions?.liveStreaming ? { liveStreaming: true, statusController } : { statusController });
return await handleToolCalls(openai, response, baseRequest, cwd, onResponseUsage, runToolCall, { ...streamOptions, statusController });
} catch (error) {
statusController?.stop?.();
throw error;
}
}
export { persistResponseState, persistCheckpoint, readLatestCheckpoint, clearSession, readSessionState, extractTextFromResponse, extractUsage, formatTurnUsageReport, formatElapsedStatus, formatSpinnerFrame, formatTransactionCompletionMessage, createStatusLineController, createStreamedResponse };
export { formatUsageSummary } from './response.mjs';