@in-the-loop-labs/pair-review 5.1.0 → 5.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +14 -4
- package/package.json +3 -2
- package/plugin/.claude-plugin/plugin.json +1 -1
- package/plugin-code-critic/.claude-plugin/plugin.json +1 -1
- package/plugin-pair-loop/.claude-plugin/plugin.json +1 -1
- package/plugin-pair-loop/skills/loop/SKILL.md +21 -6
- package/public/css/pr.css +2 -2
- package/public/index.html +5 -2
- package/public/js/index.js +106 -9
- package/src/ai/analyzer.js +191 -27
- package/src/ai/index.js +1 -0
- package/src/ai/omp/review-config.yml +13 -0
- package/src/ai/omp-provider.js +279 -0
- package/src/ai/pi-format.js +535 -0
- package/src/ai/pi-provider.js +38 -963
- package/src/ai/pi-style-provider.js +526 -0
- package/src/ai/provider.js +2 -0
- package/src/chat/chat-providers.js +34 -11
- package/src/chat/omp-bridge.js +111 -0
- package/src/chat/pi-bridge.js +42 -34
- package/src/chat/session-manager.js +30 -4
- package/src/config.js +32 -3
- package/src/links/host-names.js +200 -0
- package/src/local-review.js +7 -3
- package/src/main.js +21 -1
- package/src/review-config.js +81 -5
- package/src/routes/analyses.js +6 -2
- package/src/routes/config.js +16 -0
- package/src/routes/local.js +7 -6
- package/src/routes/mcp.js +5 -2
- package/src/routes/pr.js +8 -5
- package/src/routes/setup.js +1 -1
- package/src/routes/shared.js +27 -0
- package/src/routes/stack-analysis.js +4 -1
- package/src/settings/registry.js +1 -1
- package/src/setup/local-setup.js +5 -3
- package/src/single-port.js +1 -1
- package/src/utils/json-extractor.js +162 -41
- package/src/utils/local-path-input.js +64 -8
|
@@ -0,0 +1,535 @@
|
|
|
1
|
+
// Copyright 2026 Tim Perkins (tjwp) | SPDX-License-Identifier: Apache-2.0
|
|
2
|
+
/**
|
|
3
|
+
* Pi JSONL Format Utilities
|
|
4
|
+
*
|
|
5
|
+
* Shared helpers for parsing the Pi coding agent's `--mode json` JSONL event
|
|
6
|
+
* stream. The stream consists of event envelopes (session, agent_start,
|
|
7
|
+
* turn_start, message_start/update/end, tool_execution_start/update/end,
|
|
8
|
+
* turn_end, agent_end) with assistant text carried in message content blocks.
|
|
9
|
+
*
|
|
10
|
+
* Both the Pi provider (pi-provider.js) and the OMP provider (omp-provider.js)
|
|
11
|
+
* consume this format — OMP (Oh My Pi) is a fork of Pi and emits an identical
|
|
12
|
+
* event stream. Helpers that log accept an optional `cliName` so messages can
|
|
13
|
+
* name the CLI that actually produced the output (defaults to 'Pi').
|
|
14
|
+
*/
|
|
15
|
+
|
|
16
|
+
const logger = require('../utils/logger');
|
|
17
|
+
const { extractJSON } = require('../utils/json-extractor');
|
|
18
|
+
|
|
19
|
+
// Keep raw stream capture bounded so large JSONL sessions cannot exhaust V8's
|
|
20
|
+
// maximum string size. Assistant text is still extracted incrementally from all
|
|
21
|
+
// complete JSONL lines and used as the primary parse/fallback input.
|
|
22
|
+
const MAX_PI_CAPTURED_STDOUT_CHARS = 5 * 1024 * 1024;
|
|
23
|
+
const MAX_PI_CAPTURED_STDERR_CHARS = 1 * 1024 * 1024;
|
|
24
|
+
const MAX_PI_LINE_CHARS = 2 * 1024 * 1024;
|
|
25
|
+
const PI_STDERR_HEAD_CHARS = 128 * 1024;
|
|
26
|
+
const PI_STDERR_TAIL_CHARS = MAX_PI_CAPTURED_STDERR_CHARS - PI_STDERR_HEAD_CHARS;
|
|
27
|
+
const PI_TRUNCATED_LINE_MARKER = '...[line truncated]...';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* Extract text from assistant content, handling both array-of-blocks and
|
|
31
|
+
* string content. Uses a Set for dedup to avoid incorrect substring matching.
|
|
32
|
+
*
|
|
33
|
+
* @param {Array|string} content - Content from an assistant message
|
|
34
|
+
* @param {Set<string>} seenTexts - Set tracking already-seen text blocks
|
|
35
|
+
* @returns {string} Extracted text (may be empty if all blocks were duplicates)
|
|
36
|
+
*/
|
|
37
|
+
function extractAssistantText(content, seenTexts) {
|
|
38
|
+
let text = '';
|
|
39
|
+
if (Array.isArray(content)) {
|
|
40
|
+
for (const block of content) {
|
|
41
|
+
if (block.type === 'text' && block.text) {
|
|
42
|
+
if (!seenTexts.has(block.text)) {
|
|
43
|
+
seenTexts.add(block.text);
|
|
44
|
+
text += block.text;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
} else if (typeof content === 'string') {
|
|
49
|
+
if (!seenTexts.has(content)) {
|
|
50
|
+
seenTexts.add(content);
|
|
51
|
+
text += content;
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return text;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Determine whether a parsed JSON object looks like a Pi JSONL event envelope
|
|
59
|
+
* rather than a final review result payload.
|
|
60
|
+
*
|
|
61
|
+
* @param {Object} value - Parsed JSON object
|
|
62
|
+
* @returns {boolean} True when the object appears to be a Pi event
|
|
63
|
+
*/
|
|
64
|
+
function isPiEventEnvelope(value) {
|
|
65
|
+
if (!value || typeof value !== 'object' || Array.isArray(value)) {
|
|
66
|
+
return false;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
if (typeof value.type !== 'string') {
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
if (
|
|
74
|
+
value.message ||
|
|
75
|
+
Array.isArray(value.messages) ||
|
|
76
|
+
value.assistantMessageEvent ||
|
|
77
|
+
value.toolName ||
|
|
78
|
+
value.toolCallId ||
|
|
79
|
+
Object.hasOwn(value, 'partialResult') ||
|
|
80
|
+
Object.hasOwn(value, 'result') ||
|
|
81
|
+
Object.hasOwn(value, 'version')
|
|
82
|
+
) {
|
|
83
|
+
return true;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
return /_(start|update|end)$/.test(value.type) || value.type === 'session';
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* Append a chunk to a captured stream buffer without exceeding the configured
|
|
91
|
+
* maximum. Returns the updated buffer and whether truncation occurred.
|
|
92
|
+
*
|
|
93
|
+
* @param {string} existing - Existing captured output
|
|
94
|
+
* @param {string} chunk - New output chunk
|
|
95
|
+
* @param {number} maxChars - Maximum number of chars to retain
|
|
96
|
+
* @returns {{value: string, truncated: boolean}}
|
|
97
|
+
*/
|
|
98
|
+
function appendWithLimit(existing, chunk, maxChars) {
|
|
99
|
+
if (!chunk || maxChars <= 0) {
|
|
100
|
+
return { value: existing, truncated: false };
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
const remaining = maxChars - existing.length;
|
|
104
|
+
if (remaining <= 0) {
|
|
105
|
+
return { value: existing, truncated: true };
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
if (chunk.length <= remaining) {
|
|
109
|
+
return { value: existing + chunk, truncated: false };
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
return {
|
|
113
|
+
value: existing + chunk.slice(0, remaining),
|
|
114
|
+
truncated: true
|
|
115
|
+
};
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* Append a chunk to a bounded head+tail buffer so error logs preserve both the
|
|
120
|
+
* start and end of noisy stderr output.
|
|
121
|
+
*
|
|
122
|
+
* @param {{head: string, tail: string, headFull: boolean, omittedChars: number}} buffer - Buffer state
|
|
123
|
+
* @param {string} chunk - New stderr chunk
|
|
124
|
+
* @param {number} maxHeadChars - Max chars to retain from the start
|
|
125
|
+
* @param {number} maxTailChars - Max chars to retain from the end
|
|
126
|
+
*/
|
|
127
|
+
function appendHeadTailBuffer(buffer, chunk, maxHeadChars, maxTailChars) {
|
|
128
|
+
if (!chunk) return;
|
|
129
|
+
|
|
130
|
+
if (!buffer.headFull) {
|
|
131
|
+
const remainingHead = maxHeadChars - buffer.head.length;
|
|
132
|
+
if (chunk.length <= remainingHead) {
|
|
133
|
+
buffer.head += chunk;
|
|
134
|
+
if (buffer.head.length >= maxHeadChars) {
|
|
135
|
+
buffer.headFull = true;
|
|
136
|
+
}
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const safeHead = Math.max(remainingHead, 0);
|
|
141
|
+
buffer.head += chunk.slice(0, safeHead);
|
|
142
|
+
buffer.headFull = true;
|
|
143
|
+
|
|
144
|
+
const overflow = chunk.slice(safeHead);
|
|
145
|
+
if (overflow.length > maxTailChars) {
|
|
146
|
+
buffer.omittedChars += overflow.length - maxTailChars;
|
|
147
|
+
buffer.tail = overflow.slice(-maxTailChars);
|
|
148
|
+
} else {
|
|
149
|
+
buffer.tail = overflow;
|
|
150
|
+
}
|
|
151
|
+
return;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const combinedTail = buffer.tail + chunk;
|
|
155
|
+
if (combinedTail.length > maxTailChars) {
|
|
156
|
+
buffer.omittedChars += combinedTail.length - maxTailChars;
|
|
157
|
+
buffer.tail = combinedTail.slice(-maxTailChars);
|
|
158
|
+
} else {
|
|
159
|
+
buffer.tail = combinedTail;
|
|
160
|
+
}
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
/**
|
|
164
|
+
* Render a bounded head+tail buffer as a string for logs and error messages.
|
|
165
|
+
*
|
|
166
|
+
* @param {{head: string, tail: string, headFull: boolean, omittedChars: number}} buffer - Buffer state
|
|
167
|
+
* @returns {string} Formatted stderr capture
|
|
168
|
+
*/
|
|
169
|
+
function formatHeadTailBuffer(buffer) {
|
|
170
|
+
if (buffer.omittedChars === 0) {
|
|
171
|
+
return `${buffer.head}${buffer.tail}`;
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
return `${buffer.head}\n...[${buffer.omittedChars} chars omitted]...\n${buffer.tail}`;
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/**
|
|
178
|
+
* Extract final assistant text from a Pi JSONL event.
|
|
179
|
+
*
|
|
180
|
+
* @param {Object} event - Parsed Pi event object
|
|
181
|
+
* @param {Set<string>} seenTexts - Set tracking already-seen text blocks
|
|
182
|
+
* @returns {string} Extracted text from the event
|
|
183
|
+
*/
|
|
184
|
+
function extractPiEventText(event, seenTexts) {
|
|
185
|
+
let text = '';
|
|
186
|
+
|
|
187
|
+
if (event.type === 'message_end' && event.message?.role === 'assistant') {
|
|
188
|
+
text += extractAssistantText(event.message.content, seenTexts);
|
|
189
|
+
}
|
|
190
|
+
|
|
191
|
+
if (event.type === 'turn_end' && event.message?.role === 'assistant') {
|
|
192
|
+
text += extractAssistantText(event.message.content, seenTexts);
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
if (event.type === 'agent_end' && Array.isArray(event.messages)) {
|
|
196
|
+
for (const msg of event.messages) {
|
|
197
|
+
if (msg.role === 'assistant') {
|
|
198
|
+
text += extractAssistantText(msg.content, seenTexts);
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
return text;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Accumulate only raw lines that could plausibly help the direct JSON fallback.
|
|
208
|
+
* Pi JSONL event envelopes are intentionally excluded because they are noisy
|
|
209
|
+
* transport records, not the final review result.
|
|
210
|
+
*
|
|
211
|
+
* @param {string} line - One stdout line
|
|
212
|
+
* @param {{rawOutput: string, rawOutputTruncated: boolean}} state - Parse state
|
|
213
|
+
* @param {string} levelPrefix - Prefix used in logs
|
|
214
|
+
* @param {{status: 'parsed', value: Object} | {status: 'failed'}} [parseResult] - Optional parse result reuse
|
|
215
|
+
* @param {string} [cliName='Pi'] - CLI name used in log messages
|
|
216
|
+
*/
|
|
217
|
+
function accumulatePiRawFallbackLine(line, state, levelPrefix, parseResult, cliName = 'Pi') {
|
|
218
|
+
if (!line?.trim()) return;
|
|
219
|
+
|
|
220
|
+
let parsed;
|
|
221
|
+
let parseFailed = false;
|
|
222
|
+
|
|
223
|
+
if (parseResult?.status === 'parsed') {
|
|
224
|
+
parsed = parseResult.value;
|
|
225
|
+
} else if (parseResult?.status === 'failed') {
|
|
226
|
+
parseFailed = true;
|
|
227
|
+
} else {
|
|
228
|
+
try {
|
|
229
|
+
parsed = JSON.parse(line);
|
|
230
|
+
} catch {
|
|
231
|
+
parseFailed = true;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
|
|
235
|
+
if (!parseFailed && isPiEventEnvelope(parsed)) {
|
|
236
|
+
return;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
const capture = appendWithLimit(state.rawOutput, `${line}\n`, MAX_PI_CAPTURED_STDOUT_CHARS);
|
|
240
|
+
state.rawOutput = capture.value;
|
|
241
|
+
|
|
242
|
+
if (capture.truncated && !state.rawOutputTruncated) {
|
|
243
|
+
state.rawOutputTruncated = true;
|
|
244
|
+
logger.warn(
|
|
245
|
+
`${levelPrefix} ${cliName} CLI raw-output fallback exceeded ${MAX_PI_CAPTURED_STDOUT_CHARS} chars; retaining only the first ${MAX_PI_CAPTURED_STDOUT_CHARS} chars`
|
|
246
|
+
);
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
/**
|
|
251
|
+
* Parse a single Pi JSONL line into accumulated assistant text.
|
|
252
|
+
*
|
|
253
|
+
* @param {string} line - One JSONL line
|
|
254
|
+
* @param {{textContent: string, seenTexts: Set<string>, rawOutput: string, rawOutputTruncated: boolean}} state - Parse state
|
|
255
|
+
* @param {string} levelPrefix - Prefix used in logs
|
|
256
|
+
* @param {string} [cliName='Pi'] - CLI name used in log messages
|
|
257
|
+
*/
|
|
258
|
+
function accumulatePiResponseLine(line, state, levelPrefix, cliName = 'Pi') {
|
|
259
|
+
if (!line?.trim()) return;
|
|
260
|
+
|
|
261
|
+
let parseResult;
|
|
262
|
+
try {
|
|
263
|
+
const event = JSON.parse(line);
|
|
264
|
+
state.textContent += extractPiEventText(event, state.seenTexts);
|
|
265
|
+
parseResult = { status: 'parsed', value: event };
|
|
266
|
+
} catch {
|
|
267
|
+
logger.debug(`${levelPrefix} Skipping malformed JSONL line: ${line.substring(0, 100)}`);
|
|
268
|
+
parseResult = { status: 'failed' };
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
accumulatePiRawFallbackLine(line, state, levelPrefix, parseResult, cliName);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
/**
|
|
275
|
+
* Append stdout data to the pending JSONL line buffer while capping any single
|
|
276
|
+
* unterminated line to avoid retaining multi-megabyte tool payloads in memory.
|
|
277
|
+
*
|
|
278
|
+
* @param {{buffer: string, lineTruncated: boolean, warningLogged: boolean}} state - Pending line state
|
|
279
|
+
* @param {string} chunk - New stdout chunk
|
|
280
|
+
* @param {string} levelPrefix - Prefix used in logs
|
|
281
|
+
* @param {string} [cliName='Pi'] - CLI name used in log messages
|
|
282
|
+
* @returns {string[]} Complete lines extracted from the chunk
|
|
283
|
+
*/
|
|
284
|
+
function appendPiChunkToLineBuffer(state, chunk, levelPrefix, cliName = 'Pi') {
|
|
285
|
+
if (!chunk) return [];
|
|
286
|
+
|
|
287
|
+
const lines = [];
|
|
288
|
+
let cursor = 0;
|
|
289
|
+
|
|
290
|
+
while (cursor < chunk.length) {
|
|
291
|
+
if (state.lineTruncated) {
|
|
292
|
+
const nextNewline = chunk.indexOf('\n', cursor);
|
|
293
|
+
if (nextNewline === -1) {
|
|
294
|
+
return lines;
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
lines.push(state.buffer);
|
|
298
|
+
state.buffer = '';
|
|
299
|
+
state.lineTruncated = false;
|
|
300
|
+
cursor = nextNewline + 1;
|
|
301
|
+
continue;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
const nextNewline = chunk.indexOf('\n', cursor);
|
|
305
|
+
const segmentEnd = nextNewline === -1 ? chunk.length : nextNewline;
|
|
306
|
+
const segment = chunk.slice(cursor, segmentEnd);
|
|
307
|
+
const remainingCapacity = MAX_PI_LINE_CHARS - state.buffer.length;
|
|
308
|
+
|
|
309
|
+
if (segment.length <= remainingCapacity) {
|
|
310
|
+
state.buffer += segment;
|
|
311
|
+
if (nextNewline !== -1) {
|
|
312
|
+
lines.push(state.buffer);
|
|
313
|
+
state.buffer = '';
|
|
314
|
+
}
|
|
315
|
+
cursor = segmentEnd + (nextNewline === -1 ? 0 : 1);
|
|
316
|
+
continue;
|
|
317
|
+
}
|
|
318
|
+
|
|
319
|
+
const safeCapacity = Math.max(remainingCapacity, 0);
|
|
320
|
+
state.buffer += segment.slice(0, safeCapacity) + PI_TRUNCATED_LINE_MARKER;
|
|
321
|
+
state.lineTruncated = true;
|
|
322
|
+
|
|
323
|
+
if (!state.warningLogged) {
|
|
324
|
+
state.warningLogged = true;
|
|
325
|
+
logger.warn(
|
|
326
|
+
`${levelPrefix} ${cliName} CLI emitted a JSONL event longer than ${MAX_PI_LINE_CHARS} chars; truncating the pending line buffer until the next newline`
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
if (nextNewline !== -1) {
|
|
331
|
+
lines.push(state.buffer);
|
|
332
|
+
state.buffer = '';
|
|
333
|
+
state.lineTruncated = false;
|
|
334
|
+
cursor = nextNewline + 1;
|
|
335
|
+
continue;
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
return lines;
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
return lines;
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
/**
|
|
345
|
+
* Finalize Pi response parsing from incrementally extracted assistant text and
|
|
346
|
+
* a bounded raw-output fallback buffer.
|
|
347
|
+
*
|
|
348
|
+
* @param {Object} input - Parse inputs
|
|
349
|
+
* @param {string} input.textContent - Assistant text extracted from JSONL events
|
|
350
|
+
* @param {string} input.rawOutput - Bounded raw stdout capture
|
|
351
|
+
* @param {boolean} [input.rawOutputTruncated=false] - Whether raw stdout was truncated
|
|
352
|
+
* @param {string|number} level - Analysis level for logging
|
|
353
|
+
* @param {string} levelPrefix - Prefix used in logs
|
|
354
|
+
* @param {string} [cliName='Pi'] - CLI name used in log and error messages
|
|
355
|
+
* @returns {{success: boolean, data?: Object, error?: string, textContent?: string}}
|
|
356
|
+
*/
|
|
357
|
+
function finalizePiResponseParsing({ textContent, rawOutput, rawOutputTruncated = false }, level, levelPrefix, cliName = 'Pi') {
|
|
358
|
+
if (textContent) {
|
|
359
|
+
const extracted = extractJSON(textContent, level, levelPrefix);
|
|
360
|
+
if (extracted.success) {
|
|
361
|
+
return extracted;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
logger.warn(`${levelPrefix} Text content is not JSON, treating as raw text`);
|
|
365
|
+
return { success: false, error: 'Text content is not valid JSON', textContent };
|
|
366
|
+
}
|
|
367
|
+
|
|
368
|
+
if (rawOutputTruncated) {
|
|
369
|
+
logger.warn(`${levelPrefix} ${cliName} CLI raw-output fallback was truncated before assistant text could be recovered`);
|
|
370
|
+
return {
|
|
371
|
+
success: false,
|
|
372
|
+
error: `${cliName} CLI raw-output fallback was truncated before assistant text could be recovered`
|
|
373
|
+
};
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
return extractJSON(rawOutput, level, levelPrefix);
|
|
377
|
+
}
|
|
378
|
+
|
|
379
|
+
/**
|
|
380
|
+
* Log a streaming JSONL line for debugging visibility.
|
|
381
|
+
* Extracts meaningful info from each event type without being too verbose.
|
|
382
|
+
*
|
|
383
|
+
* Uses logger.streamDebug() which only logs when --debug-stream flag is enabled.
|
|
384
|
+
*
|
|
385
|
+
* @param {string} line - A single JSONL line
|
|
386
|
+
* @param {number} lineNum - Line number for reference
|
|
387
|
+
* @param {string} levelPrefix - Level prefix for log messages
|
|
388
|
+
*/
|
|
389
|
+
function logPiStreamLine(line, lineNum, levelPrefix) {
|
|
390
|
+
// Early exit if stream debugging is disabled
|
|
391
|
+
if (!logger.isStreamDebugEnabled()) return;
|
|
392
|
+
|
|
393
|
+
try {
|
|
394
|
+
const event = JSON.parse(line);
|
|
395
|
+
const type = event.type || 'unknown';
|
|
396
|
+
|
|
397
|
+
// Log different event types with appropriate detail
|
|
398
|
+
switch (type) {
|
|
399
|
+
case 'session':
|
|
400
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] Session started: ${event.id || 'unknown'}`);
|
|
401
|
+
break;
|
|
402
|
+
|
|
403
|
+
case 'turn_start':
|
|
404
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] Turn started`);
|
|
405
|
+
break;
|
|
406
|
+
|
|
407
|
+
case 'turn_end': {
|
|
408
|
+
const msg = event.message;
|
|
409
|
+
if (msg?.role) {
|
|
410
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] Turn ended (${msg.role})`);
|
|
411
|
+
} else {
|
|
412
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] Turn ended`);
|
|
413
|
+
}
|
|
414
|
+
break;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
case 'message_start': {
|
|
418
|
+
const msg = event.message;
|
|
419
|
+
const role = msg?.role || 'unknown';
|
|
420
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] Message started (${role})`);
|
|
421
|
+
break;
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
case 'message_update': {
|
|
425
|
+
const assistantEvent = event.assistantMessageEvent;
|
|
426
|
+
if (assistantEvent?.type === 'text_delta' && assistantEvent?.delta) {
|
|
427
|
+
const preview = assistantEvent.delta.length > 60
|
|
428
|
+
? assistantEvent.delta.substring(0, 60) + '...'
|
|
429
|
+
: assistantEvent.delta;
|
|
430
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] text_delta: ${preview.replace(/\n/g, '\\n')}`);
|
|
431
|
+
} else if (assistantEvent?.type) {
|
|
432
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] message_update: ${assistantEvent.type}`);
|
|
433
|
+
} else {
|
|
434
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] message_update`);
|
|
435
|
+
}
|
|
436
|
+
break;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
case 'message_end': {
|
|
440
|
+
const msg = event.message;
|
|
441
|
+
const role = msg?.role || 'unknown';
|
|
442
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] Message ended (${role})`);
|
|
443
|
+
break;
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
case 'tool_execution_start': {
|
|
447
|
+
const toolName = event.toolName || 'unknown';
|
|
448
|
+
const toolId = event.toolCallId || '';
|
|
449
|
+
const idPart = toolId ? ` [${toolId.substring(0, 8)}]` : '';
|
|
450
|
+
|
|
451
|
+
let inputPreview = '';
|
|
452
|
+
const args = event.args;
|
|
453
|
+
if (args) {
|
|
454
|
+
if (typeof args === 'string') {
|
|
455
|
+
inputPreview = args.length > 60 ? args.substring(0, 60) + '...' : args;
|
|
456
|
+
} else if (typeof args === 'object') {
|
|
457
|
+
if (args.command) {
|
|
458
|
+
inputPreview = `cmd="${args.command.substring(0, 50)}${args.command.length > 50 ? '...' : ''}"`;
|
|
459
|
+
} else if (args.file_path || args.path) {
|
|
460
|
+
inputPreview = `path="${args.file_path || args.path}"`;
|
|
461
|
+
} else {
|
|
462
|
+
const keys = Object.keys(args);
|
|
463
|
+
inputPreview = `{${keys.slice(0, 3).join(', ')}${keys.length > 3 ? '...' : ''}}`;
|
|
464
|
+
}
|
|
465
|
+
}
|
|
466
|
+
}
|
|
467
|
+
|
|
468
|
+
const inputPart = inputPreview ? ` ${inputPreview}` : '';
|
|
469
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] tool_start: ${toolName}${idPart}${inputPart}`);
|
|
470
|
+
break;
|
|
471
|
+
}
|
|
472
|
+
|
|
473
|
+
case 'tool_execution_update': {
|
|
474
|
+
const partial = event.partialResult || '';
|
|
475
|
+
if (partial) {
|
|
476
|
+
const preview = typeof partial === 'string'
|
|
477
|
+
? (partial.length > 60 ? partial.substring(0, 60) + '...' : partial)
|
|
478
|
+
: JSON.stringify(partial).substring(0, 60);
|
|
479
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] tool_update: ${preview.replace(/\n/g, '\\n')}`);
|
|
480
|
+
} else {
|
|
481
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] tool_update`);
|
|
482
|
+
}
|
|
483
|
+
break;
|
|
484
|
+
}
|
|
485
|
+
|
|
486
|
+
case 'tool_execution_end': {
|
|
487
|
+
const isError = event.isError || false;
|
|
488
|
+
const statusPart = isError ? ' ERROR' : ' OK';
|
|
489
|
+
const result = event.result || '';
|
|
490
|
+
let resultPreview = '';
|
|
491
|
+
if (typeof result === 'string' && result.length > 0) {
|
|
492
|
+
resultPreview = result.length > 60 ? result.substring(0, 60) + '...' : result;
|
|
493
|
+
resultPreview = resultPreview.replace(/\n/g, '\\n');
|
|
494
|
+
}
|
|
495
|
+
const previewPart = resultPreview ? ` ${resultPreview}` : '';
|
|
496
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] tool_end${statusPart}${previewPart}`);
|
|
497
|
+
break;
|
|
498
|
+
}
|
|
499
|
+
|
|
500
|
+
case 'agent_start':
|
|
501
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] Agent started`);
|
|
502
|
+
break;
|
|
503
|
+
|
|
504
|
+
case 'agent_end':
|
|
505
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] Agent ended`);
|
|
506
|
+
break;
|
|
507
|
+
|
|
508
|
+
default:
|
|
509
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] ${type}`);
|
|
510
|
+
}
|
|
511
|
+
} catch {
|
|
512
|
+
// If we can't parse the line, log the full content for debugging
|
|
513
|
+
logger.streamDebug(`${levelPrefix} [#${lineNum}] (unparseable): ${line}`);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
module.exports = {
|
|
518
|
+
MAX_PI_CAPTURED_STDOUT_CHARS,
|
|
519
|
+
MAX_PI_CAPTURED_STDERR_CHARS,
|
|
520
|
+
MAX_PI_LINE_CHARS,
|
|
521
|
+
PI_STDERR_HEAD_CHARS,
|
|
522
|
+
PI_STDERR_TAIL_CHARS,
|
|
523
|
+
PI_TRUNCATED_LINE_MARKER,
|
|
524
|
+
extractAssistantText,
|
|
525
|
+
isPiEventEnvelope,
|
|
526
|
+
appendWithLimit,
|
|
527
|
+
appendHeadTailBuffer,
|
|
528
|
+
formatHeadTailBuffer,
|
|
529
|
+
extractPiEventText,
|
|
530
|
+
accumulatePiRawFallbackLine,
|
|
531
|
+
accumulatePiResponseLine,
|
|
532
|
+
appendPiChunkToLineBuffer,
|
|
533
|
+
finalizePiResponseParsing,
|
|
534
|
+
logPiStreamLine
|
|
535
|
+
};
|