pi-gemini-app 0.1.13 → 0.1.15
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/extensions/pi/auth.js +11 -7
- package/extensions/pi/provider.js +17 -5
- package/extensions/pi/stream.js +14 -5
- package/native-wire/robin-current.mjs +51 -14
- package/package.json +2 -1
- package/src/gateway/automatic-recovery.mjs +3 -1
- package/src/gateway/native-evidence.mjs +1 -0
- package/src/gateway/output-recovery.mjs +33 -0
- package/src/gateway/subscription-transport.mjs +78 -0
- package/src/gateway/subscription.mjs +36 -19
- package/src/native-provider.mjs +170 -41
- package/src/openai-contract.mjs +11 -1
- package/src/tool-protocol.mjs +306 -53
package/src/tool-protocol.mjs
CHANGED
|
@@ -205,6 +205,58 @@ export function flattenMessages(messages = [], { maxChars = 3_500_000 } = {}) {
|
|
|
205
205
|
|
|
206
206
|
const CALLER_RESULT_MARKER = '[Caller tool result]';
|
|
207
207
|
|
|
208
|
+
// Single source of truth for classifying an unquoted buffer against the internal
|
|
209
|
+
// `[Caller tool result]` + `{"tool_call_id":` wire format (supporting spaces, tabs, LF, and CRLF).
|
|
210
|
+
// Returns:
|
|
211
|
+
// - 'match' : confirmed synthetic wire block prefix
|
|
212
|
+
// - 'pending' : valid prefix that could still become a 'match' with more bytes
|
|
213
|
+
// - 'none' : definitely ordinary text (safe to stream immediately)
|
|
214
|
+
function classifyCallerResultWire(str, { jsonOnly = false } = {}) {
|
|
215
|
+
let i = 0;
|
|
216
|
+
const skipIndent = () => {
|
|
217
|
+
let spaces = 0;
|
|
218
|
+
while (i < str.length && spaces < 3 && str[i] === ' ') { i++; spaces++; }
|
|
219
|
+
};
|
|
220
|
+
const matchLiteral = literal => {
|
|
221
|
+
const rem = str.length - i;
|
|
222
|
+
if (rem <= 0) return 'pending';
|
|
223
|
+
if (rem < literal.length) return literal.startsWith(str.slice(i)) ? 'pending' : 'none';
|
|
224
|
+
if (!str.startsWith(literal, i)) return 'none';
|
|
225
|
+
i += literal.length;
|
|
226
|
+
return 'ok';
|
|
227
|
+
};
|
|
228
|
+
if (!jsonOnly) {
|
|
229
|
+
skipIndent();
|
|
230
|
+
if (i >= str.length) return 'pending';
|
|
231
|
+
const m = matchLiteral(CALLER_RESULT_MARKER);
|
|
232
|
+
if (m !== 'ok') return m;
|
|
233
|
+
let sawSep = false;
|
|
234
|
+
while (i < str.length && (str[i] === ' ' || str[i] === '\t')) { i++; sawSep = true; }
|
|
235
|
+
if (i < str.length && str[i] === '\r') {
|
|
236
|
+
i++;
|
|
237
|
+
if (i === str.length) return 'pending';
|
|
238
|
+
if (str[i] !== '\n') return 'none';
|
|
239
|
+
i++;
|
|
240
|
+
sawSep = true;
|
|
241
|
+
skipIndent();
|
|
242
|
+
} else if (i < str.length && str[i] === '\n') {
|
|
243
|
+
i++;
|
|
244
|
+
sawSep = true;
|
|
245
|
+
skipIndent();
|
|
246
|
+
}
|
|
247
|
+
if (i === str.length) return 'pending';
|
|
248
|
+
if (!sawSep) return 'none';
|
|
249
|
+
} else {
|
|
250
|
+
skipIndent();
|
|
251
|
+
if (i >= str.length) return 'pending';
|
|
252
|
+
}
|
|
253
|
+
const j = matchLiteral('{"tool_call_id"');
|
|
254
|
+
if (j !== 'ok') return j;
|
|
255
|
+
while (i < str.length && (str[i] === ' ' || str[i] === '\t')) i++;
|
|
256
|
+
if (i === str.length) return 'pending';
|
|
257
|
+
return str[i] === ':' ? 'match' : 'none';
|
|
258
|
+
}
|
|
259
|
+
|
|
208
260
|
// Returns the safe prefix length before a full or trailing partial CALLER_RESULT_MARKER.
|
|
209
261
|
// Because CALLER_RESULT_MARKER contains '[' only at index 0, any partial prefix at the end
|
|
210
262
|
// of `str` must start at the last '[' within the trailing slice.
|
|
@@ -222,16 +274,45 @@ function callerResultPreviewCut(str) {
|
|
|
222
274
|
* become call events; preview exposes ordinary pending text, never a tool body.
|
|
223
275
|
* Once at least one real tool_call has been emitted, any unquoted hallucinated
|
|
224
276
|
* `[Caller tool result]` continuation and everything after it is suppressed so
|
|
225
|
-
* the caller receives only the real tool calls.
|
|
277
|
+
* the caller receives only the real tool calls. Even before any tool_call, an
|
|
278
|
+
* unquoted wire-format `[Caller tool result]` + `{"tool_call_id":...}` block is
|
|
279
|
+
* rejected as recoverable MALFORMED_TOOL_CALL when leading (no prior visible
|
|
280
|
+
* text) or suppressed when trailing after real visible text. */
|
|
226
281
|
export function createToolCallSplitter({ newId = () => 'call_' + crypto.randomBytes(12).toString('hex'), policy = compileToolPolicy([]) } = {}) {
|
|
227
282
|
let pending = '', scanned = 0, seen = 0, closed = false, suppressed = false, bytes = 0, consumedChars = 0;
|
|
228
283
|
let mode = 'text', marker = '`', fenceLength = 3, toolBody = '', toolBytes = 0, headerBytes = 0, inlineTicks = 0;
|
|
284
|
+
let hasVisibleContent = false, leadingWhitespace = '', heldMarkerLine = null, heldMarkerOffset = 0;
|
|
229
285
|
const ids = new Set();
|
|
230
|
-
const
|
|
286
|
+
const pushContent = (events, chunk) => {
|
|
287
|
+
if (!chunk) return;
|
|
288
|
+
if (!hasVisibleContent && seen === 0) {
|
|
289
|
+
if (!chunk.trim().length) {
|
|
290
|
+
leadingWhitespace += chunk;
|
|
291
|
+
return;
|
|
292
|
+
}
|
|
293
|
+
hasVisibleContent = true;
|
|
294
|
+
events.push({ type: 'content', text: leadingWhitespace + chunk });
|
|
295
|
+
leadingWhitespace = '';
|
|
296
|
+
return;
|
|
297
|
+
}
|
|
298
|
+
if (chunk.trim().length > 0) hasVisibleContent = true;
|
|
299
|
+
events.push({ type: 'content', text: chunk });
|
|
300
|
+
};
|
|
301
|
+
const malformed = (reason, offset = undefined) => {
|
|
231
302
|
const error = protocolError('MALFORMED_TOOL_CALL', 502);
|
|
232
303
|
error.toolProtocolReason = reason;
|
|
304
|
+
if (offset !== undefined) error.postFenceOffset = offset;
|
|
233
305
|
throw error;
|
|
234
306
|
};
|
|
307
|
+
const handleZeroCallHallucination = offset => {
|
|
308
|
+
heldMarkerLine = null;
|
|
309
|
+
heldMarkerOffset = 0;
|
|
310
|
+
if (!hasVisibleContent) {
|
|
311
|
+
leadingWhitespace = '';
|
|
312
|
+
malformed('hallucinated_caller_result', offset);
|
|
313
|
+
}
|
|
314
|
+
suppressed = true;
|
|
315
|
+
};
|
|
235
316
|
const emitCall = events => {
|
|
236
317
|
const parsed = parseBoundedJson(toolBody.trim(), { status: 502 });
|
|
237
318
|
if (!plainObject(parsed) || Object.keys(parsed).some(k => !['name', 'arguments'].includes(k)) || typeof parsed.name !== 'string' || !/^[A-Za-z0-9_-]{1,64}$/.test(parsed.name) || !plainObject(parsed.arguments)) fail('INVALID_TOOL_CALL', 502);
|
|
@@ -240,30 +321,30 @@ export function createToolCallSplitter({ newId = () => 'call_' + crypto.randomBy
|
|
|
240
321
|
const id = newId();
|
|
241
322
|
if (typeof id !== 'string' || !/^call_[A-Za-z0-9_-]{1,200}$/.test(id) || ids.has(id)) fail('INVALID_TOOL_CALL_ID', 502);
|
|
242
323
|
ids.add(id);
|
|
324
|
+
if (leadingWhitespace) {
|
|
325
|
+
events.push({ type: 'content', text: leadingWhitespace });
|
|
326
|
+
leadingWhitespace = '';
|
|
327
|
+
}
|
|
243
328
|
events.push({ type: 'call', call: { id, type: 'function', function: { name: parsed.name, arguments: JSON.stringify(parsed.arguments) } } });
|
|
244
329
|
};
|
|
245
|
-
const
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
if (
|
|
251
|
-
if (
|
|
330
|
+
const scanInlineText = (text, raw, eol, events, { interrupted = false, allowToolFence = true, persistTicks = true } = {}) => {
|
|
331
|
+
let ticks = persistTicks ? inlineTicks : 0;
|
|
332
|
+
let lastBacktickRun = null;
|
|
333
|
+
for (let i = 0; i < text.length;) {
|
|
334
|
+
if (seen > 0 && !ticks) {
|
|
335
|
+
if (text.startsWith(CALLER_RESULT_MARKER, i)) {
|
|
336
|
+
if (i) pushContent(events, text.slice(0, i));
|
|
252
337
|
suppressed = true;
|
|
338
|
+
if (persistTicks) inlineTicks = 0;
|
|
339
|
+
return;
|
|
340
|
+
}
|
|
341
|
+
if (interrupted && text[i] === '[' && CALLER_RESULT_MARKER.startsWith(text.slice(i))) {
|
|
342
|
+
if (i) pushContent(events, text.slice(0, i));
|
|
343
|
+
if (persistTicks) inlineTicks = 0;
|
|
253
344
|
return;
|
|
254
345
|
}
|
|
255
346
|
}
|
|
256
|
-
|
|
257
|
-
return;
|
|
258
|
-
}
|
|
259
|
-
if (!text.trim()) inlineTicks = 0;
|
|
260
|
-
for (let i = 0; i < text.length;) {
|
|
261
|
-
if (seen > 0 && !inlineTicks && text.startsWith(CALLER_RESULT_MARKER, i)) {
|
|
262
|
-
if (i) events.push({ type: 'content', text: text.slice(0, i) });
|
|
263
|
-
suppressed = true;
|
|
264
|
-
return;
|
|
265
|
-
}
|
|
266
|
-
if (text[i] === '\\' && !inlineTicks) {
|
|
347
|
+
if (text[i] === '\\' && !ticks) {
|
|
267
348
|
i += 2;
|
|
268
349
|
while (text[i] === '`' && text[i - 1] === '`') i++;
|
|
269
350
|
continue;
|
|
@@ -273,21 +354,56 @@ export function createToolCallSplitter({ newId = () => 'call_' + crypto.randomBy
|
|
|
273
354
|
const length = end - i;
|
|
274
355
|
// Historical successful responses may append an actual block after prose.
|
|
275
356
|
// It still needs an exact label/newline and cannot be inside inline code.
|
|
276
|
-
if (!
|
|
277
|
-
if (i) events
|
|
357
|
+
if (allowToolFence && !ticks && length >= 3 && eol && /^[ \t]*tool_call[ \t]*$/.test(text.slice(end))) {
|
|
358
|
+
if (i) pushContent(events, text.slice(0, i));
|
|
278
359
|
mode = 'tool'; fenceLength = length; headerBytes = Buffer.byteLength(text.slice(i) + eol);
|
|
360
|
+
if (persistTicks) inlineTicks = 0;
|
|
279
361
|
return;
|
|
280
362
|
}
|
|
281
|
-
if (!
|
|
282
|
-
|
|
363
|
+
if (!ticks) {
|
|
364
|
+
if (!persistTicks) {
|
|
365
|
+
if (lastBacktickRun === null) {
|
|
366
|
+
lastBacktickRun = new Map();
|
|
367
|
+
for (let j = i; j < text.length;) {
|
|
368
|
+
if (text[j] !== '`') { j++; continue; }
|
|
369
|
+
let runEnd = j;
|
|
370
|
+
while (text[runEnd] === '`') runEnd++;
|
|
371
|
+
lastBacktickRun.set(runEnd - j, j);
|
|
372
|
+
j = runEnd;
|
|
373
|
+
}
|
|
374
|
+
}
|
|
375
|
+
if ((lastBacktickRun.get(length) ?? -1) >= end) ticks = length;
|
|
376
|
+
} else {
|
|
377
|
+
ticks = length;
|
|
378
|
+
}
|
|
379
|
+
} else if (ticks === length) ticks = 0;
|
|
283
380
|
i = end;
|
|
284
381
|
}
|
|
285
|
-
|
|
382
|
+
if (persistTicks) inlineTicks = ticks;
|
|
383
|
+
pushContent(events, raw);
|
|
384
|
+
};
|
|
385
|
+
const inlineText = (text, raw, eol, events, interrupted = false) => {
|
|
386
|
+
if (/^(?: {4}|\t| {0,3}(?:>|[-+*][ \t]|\d+[.)][ \t]))/.test(text)) {
|
|
387
|
+
inlineTicks = 0;
|
|
388
|
+
scanInlineText(text, raw, eol, events, { interrupted, allowToolFence: false, persistTicks: false });
|
|
389
|
+
return;
|
|
390
|
+
}
|
|
391
|
+
if (!text.trim()) inlineTicks = 0;
|
|
392
|
+
scanInlineText(text, raw, eol, events, { interrupted, allowToolFence: true, persistTicks: true });
|
|
286
393
|
};
|
|
287
|
-
const line = (raw, final, events, lineStartOffset = consumedChars) => {
|
|
394
|
+
const line = (raw, final, events, lineStartOffset = consumedChars, interrupted = false) => {
|
|
288
395
|
if (suppressed) return;
|
|
289
396
|
const eol = /\r?\n$/.exec(raw)?.[0] ?? '';
|
|
290
397
|
const text = eol ? raw.slice(0, -eol.length) : raw;
|
|
398
|
+
if (heldMarkerLine !== null) {
|
|
399
|
+
if (mode === 'text' && !inlineTicks && seen === 0 && classifyCallerResultWire(text, { jsonOnly: true }) === 'match') {
|
|
400
|
+
handleZeroCallHallucination(heldMarkerOffset);
|
|
401
|
+
return;
|
|
402
|
+
}
|
|
403
|
+
pushContent(events, heldMarkerLine);
|
|
404
|
+
heldMarkerLine = null;
|
|
405
|
+
heldMarkerOffset = 0;
|
|
406
|
+
}
|
|
291
407
|
// Prefix-only matching avoids backtracking across long untrusted lines.
|
|
292
408
|
const opening = /^ {0,3}(`{3,}|~{3,})/.exec(text);
|
|
293
409
|
const run = opening?.[1], info = opening ? text.slice(opening[0].length) : '';
|
|
@@ -303,14 +419,8 @@ export function createToolCallSplitter({ newId = () => 'call_' + crypto.randomBy
|
|
|
303
419
|
}
|
|
304
420
|
mode = 'text'; toolBody = ''; toolBytes = 0; inlineTicks = 0;
|
|
305
421
|
if (info.trim()) {
|
|
306
|
-
|
|
307
|
-
|
|
308
|
-
if (markerIdx > 0) events.push({ type: 'content', text: info.slice(0, markerIdx) });
|
|
309
|
-
suppressed = true;
|
|
310
|
-
return;
|
|
311
|
-
}
|
|
312
|
-
events.push({ type: 'content', text: info + eol });
|
|
313
|
-
} else if (eol) events.push({ type: 'content', text: eol });
|
|
422
|
+
scanInlineText(info, info + eol, eol, events, { interrupted, allowToolFence: false, persistTicks: false });
|
|
423
|
+
} else if (eol) pushContent(events, eol);
|
|
314
424
|
return;
|
|
315
425
|
}
|
|
316
426
|
if (run?.[0] === '`' && info.trim()) malformed('closing_fence');
|
|
@@ -319,12 +429,24 @@ export function createToolCallSplitter({ newId = () => 'call_' + crypto.randomBy
|
|
|
319
429
|
return;
|
|
320
430
|
}
|
|
321
431
|
if (mode === 'ordinary') {
|
|
322
|
-
events
|
|
432
|
+
pushContent(events, raw);
|
|
323
433
|
if (run?.[0] === marker && run.length >= fenceLength && /^[ \t]*$/.test(info)) mode = 'text';
|
|
324
434
|
return;
|
|
325
435
|
}
|
|
326
436
|
if (!opening || (run[0] === '`' && info.includes('`'))) {
|
|
327
|
-
|
|
437
|
+
if (seen === 0 && !inlineTicks) {
|
|
438
|
+
const wireState = classifyCallerResultWire(raw);
|
|
439
|
+
if (wireState === 'match') {
|
|
440
|
+
handleZeroCallHallucination(lineStartOffset);
|
|
441
|
+
return;
|
|
442
|
+
}
|
|
443
|
+
if (!final && wireState === 'pending' && eol) {
|
|
444
|
+
heldMarkerLine = raw;
|
|
445
|
+
heldMarkerOffset = lineStartOffset;
|
|
446
|
+
return;
|
|
447
|
+
}
|
|
448
|
+
}
|
|
449
|
+
inlineText(text, raw, eol, events, interrupted); return;
|
|
328
450
|
}
|
|
329
451
|
const label = info.trim();
|
|
330
452
|
if (label === 'tool_call' && run[0] === '`') {
|
|
@@ -335,12 +457,12 @@ export function createToolCallSplitter({ newId = () => 'call_' + crypto.randomBy
|
|
|
335
457
|
if (label.startsWith('tool_call')) malformed('opening_fence');
|
|
336
458
|
if (final && !eol && label && 'tool_call'.startsWith(label)) fail('TRUNCATED_TOOL_CALL', 502);
|
|
337
459
|
mode = 'ordinary'; inlineTicks = 0; marker = run[0]; fenceLength = run.length;
|
|
338
|
-
events
|
|
460
|
+
pushContent(events, raw);
|
|
339
461
|
};
|
|
340
|
-
const drain = final => {
|
|
462
|
+
const drain = (final, { interrupted = false } = {}) => {
|
|
341
463
|
const events = []; let consumed = 0, from = scanned;
|
|
342
|
-
const runLine = (raw, isFinal, lineStartOffset) => {
|
|
343
|
-
try { line(raw, isFinal, events, lineStartOffset); }
|
|
464
|
+
const runLine = (raw, isFinal, lineStartOffset, isInterrupted = false) => {
|
|
465
|
+
try { line(raw, isFinal, events, lineStartOffset, isInterrupted); }
|
|
344
466
|
catch (err) {
|
|
345
467
|
if (err && typeof err === 'object' && err.postFenceOffset === undefined) {
|
|
346
468
|
err.postFenceOffset = lineStartOffset + raw.length;
|
|
@@ -358,12 +480,64 @@ export function createToolCallSplitter({ newId = () => 'call_' + crypto.randomBy
|
|
|
358
480
|
}
|
|
359
481
|
if (consumed) { consumedChars += consumed; pending = pending.slice(consumed); }
|
|
360
482
|
scanned = pending.length;
|
|
483
|
+
if (!final && !suppressed && mode === 'text' && !inlineTicks && seen === 0 && pending) {
|
|
484
|
+
if (heldMarkerLine !== null) {
|
|
485
|
+
if (classifyCallerResultWire(pending, { jsonOnly: true }) === 'match') {
|
|
486
|
+
handleZeroCallHallucination(heldMarkerOffset);
|
|
487
|
+
if (suppressed) { consumedChars += pending.length; pending = ''; scanned = 0; return events; }
|
|
488
|
+
} else if (classifyCallerResultWire(pending) === 'match') {
|
|
489
|
+
pushContent(events, heldMarkerLine);
|
|
490
|
+
heldMarkerLine = null;
|
|
491
|
+
heldMarkerOffset = 0;
|
|
492
|
+
handleZeroCallHallucination(consumedChars);
|
|
493
|
+
if (suppressed) { consumedChars += pending.length; pending = ''; scanned = 0; return events; }
|
|
494
|
+
}
|
|
495
|
+
} else if (classifyCallerResultWire(pending) === 'match') {
|
|
496
|
+
handleZeroCallHallucination(consumedChars);
|
|
497
|
+
if (suppressed) { consumedChars += pending.length; pending = ''; scanned = 0; return events; }
|
|
498
|
+
}
|
|
499
|
+
}
|
|
361
500
|
if (final && pending) {
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
501
|
+
// On transport interruption (not a normal completed EOF), never release an unresolved
|
|
502
|
+
// `[Caller tool result]` wire prefix or held marker line as visible content.
|
|
503
|
+
// (For `seen > 0`, `inlineText()` handles per-character backtick-aware partial-marker trimming.)
|
|
504
|
+
if (interrupted && mode === 'text' && !inlineTicks && seen === 0) {
|
|
505
|
+
if (heldMarkerLine !== null) {
|
|
506
|
+
if (classifyCallerResultWire(pending, { jsonOnly: true }) !== 'none') {
|
|
507
|
+
heldMarkerLine = null;
|
|
508
|
+
heldMarkerOffset = 0;
|
|
509
|
+
pending = ''; scanned = 0;
|
|
510
|
+
return events;
|
|
511
|
+
}
|
|
512
|
+
// heldMarkerLine is confirmed prose; still guard pending if it starts a new wire prefix.
|
|
513
|
+
if (classifyCallerResultWire(pending) !== 'none') {
|
|
514
|
+
pushContent(events, heldMarkerLine);
|
|
515
|
+
heldMarkerLine = null;
|
|
516
|
+
heldMarkerOffset = 0;
|
|
517
|
+
pending = ''; scanned = 0;
|
|
518
|
+
return events;
|
|
519
|
+
}
|
|
520
|
+
} else if (classifyCallerResultWire(pending) !== 'none') {
|
|
521
|
+
pending = ''; scanned = 0;
|
|
522
|
+
return events;
|
|
523
|
+
}
|
|
524
|
+
}
|
|
525
|
+
if (pending) {
|
|
526
|
+
const lineStartOffset = consumedChars;
|
|
527
|
+
runLine(pending, true, lineStartOffset, interrupted);
|
|
528
|
+
consumedChars += pending.length;
|
|
529
|
+
pending = ''; scanned = 0;
|
|
530
|
+
if (suppressed) return events;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
if (final && heldMarkerLine !== null) {
|
|
534
|
+
if (!interrupted) pushContent(events, heldMarkerLine);
|
|
535
|
+
heldMarkerLine = null;
|
|
536
|
+
heldMarkerOffset = 0;
|
|
537
|
+
}
|
|
538
|
+
if (final && leadingWhitespace) {
|
|
539
|
+
if (!interrupted) events.push({ type: 'content', text: leadingWhitespace });
|
|
540
|
+
leadingWhitespace = '';
|
|
367
541
|
}
|
|
368
542
|
if (mode === 'tool') {
|
|
369
543
|
if (headerBytes + toolBytes + Buffer.byteLength(pending) > TOOL_LIMITS.bytes) fail('TOOL_SIZE_LIMIT', 502);
|
|
@@ -371,6 +545,78 @@ export function createToolCallSplitter({ newId = () => 'call_' + crypto.randomBy
|
|
|
371
545
|
}
|
|
372
546
|
return events;
|
|
373
547
|
};
|
|
548
|
+
// Incremental O(n) preview scanner matching scanInlineText()'s backtick and escape rules.
|
|
549
|
+
// Resets automatically whenever pending advances to a new line (consumedChars changes).
|
|
550
|
+
let pLineOffset = -1, pIdx = 0, pTicks = 0, pHold = -1, pRunStart = -1, pEscTicks = false;
|
|
551
|
+
const safePendingPreview = str => {
|
|
552
|
+
if (pLineOffset !== consumedChars) {
|
|
553
|
+
pLineOffset = consumedChars;
|
|
554
|
+
pIdx = 0;
|
|
555
|
+
pTicks = inlineTicks;
|
|
556
|
+
pHold = -1;
|
|
557
|
+
pRunStart = -1;
|
|
558
|
+
pEscTicks = false;
|
|
559
|
+
}
|
|
560
|
+
if (pHold >= 0) return str.slice(0, pHold);
|
|
561
|
+
if (pEscTicks) {
|
|
562
|
+
while (pIdx < str.length && str[pIdx] === '`') pIdx++;
|
|
563
|
+
if (pIdx === str.length) return str;
|
|
564
|
+
pEscTicks = false;
|
|
565
|
+
}
|
|
566
|
+
if (pRunStart >= 0) {
|
|
567
|
+
while (pIdx < str.length && str[pIdx] === '`') pIdx++;
|
|
568
|
+
const length = pIdx - pRunStart;
|
|
569
|
+
const atLineOpen = pRunStart <= 3 && /^ *$/.test(str.slice(0, pRunStart));
|
|
570
|
+
if (length >= 3 && (atLineOpen || !pTicks)) {
|
|
571
|
+
pHold = atLineOpen ? 0 : pRunStart;
|
|
572
|
+
pRunStart = -1;
|
|
573
|
+
return str.slice(0, pHold);
|
|
574
|
+
}
|
|
575
|
+
if (pIdx === str.length) {
|
|
576
|
+
return (atLineOpen || !pTicks) ? (atLineOpen ? '' : str.slice(0, pRunStart)) : str;
|
|
577
|
+
}
|
|
578
|
+
if (!pTicks) pTicks = length;
|
|
579
|
+
else if (pTicks === length) pTicks = 0;
|
|
580
|
+
pRunStart = -1;
|
|
581
|
+
}
|
|
582
|
+
let i = pIdx;
|
|
583
|
+
while (i < str.length) {
|
|
584
|
+
if (str[i] === '\\' && !pTicks) {
|
|
585
|
+
if (i + 1 >= str.length) { pIdx = i; return str; }
|
|
586
|
+
let next = i + 2;
|
|
587
|
+
if (str[i + 1] === '`') {
|
|
588
|
+
while (next < str.length && str[next] === '`') next++;
|
|
589
|
+
pIdx = next;
|
|
590
|
+
if (next === str.length) { pEscTicks = true; return str; }
|
|
591
|
+
i = next;
|
|
592
|
+
continue;
|
|
593
|
+
}
|
|
594
|
+
i = next;
|
|
595
|
+
pIdx = i;
|
|
596
|
+
continue;
|
|
597
|
+
}
|
|
598
|
+
if (str[i] !== '`') { i++; pIdx = i; continue; }
|
|
599
|
+
let end = i;
|
|
600
|
+
while (end < str.length && str[end] === '`') end++;
|
|
601
|
+
const length = end - i;
|
|
602
|
+
const atLineOpen = i <= 3 && /^ *$/.test(str.slice(0, i));
|
|
603
|
+
if (length >= 3 && (atLineOpen || !pTicks)) {
|
|
604
|
+
pHold = atLineOpen ? 0 : i;
|
|
605
|
+
pIdx = end;
|
|
606
|
+
return str.slice(0, pHold);
|
|
607
|
+
}
|
|
608
|
+
if (end === str.length) {
|
|
609
|
+
pRunStart = i;
|
|
610
|
+
pIdx = end;
|
|
611
|
+
return (atLineOpen || !pTicks) ? (atLineOpen ? '' : str.slice(0, i)) : str;
|
|
612
|
+
}
|
|
613
|
+
if (!pTicks) pTicks = length;
|
|
614
|
+
else if (pTicks === length) pTicks = 0;
|
|
615
|
+
i = end;
|
|
616
|
+
pIdx = i;
|
|
617
|
+
}
|
|
618
|
+
return str;
|
|
619
|
+
};
|
|
374
620
|
return {
|
|
375
621
|
push(text) {
|
|
376
622
|
if (closed) fail('SPLITTER_CLOSED', 502);
|
|
@@ -379,25 +625,32 @@ export function createToolCallSplitter({ newId = () => 'call_' + crypto.randomBy
|
|
|
379
625
|
pending += text;
|
|
380
626
|
return drain(false);
|
|
381
627
|
},
|
|
382
|
-
flush() {
|
|
628
|
+
flush({ interrupted = false } = {}) {
|
|
383
629
|
if (closed) fail('SPLITTER_CLOSED', 502);
|
|
384
630
|
closed = true;
|
|
385
631
|
if (suppressed) return [];
|
|
386
|
-
return drain(true);
|
|
632
|
+
return drain(true, { interrupted });
|
|
387
633
|
},
|
|
388
634
|
preview() {
|
|
389
635
|
if (suppressed || mode === 'tool') return '';
|
|
390
636
|
if (mode === 'ordinary') return pending;
|
|
391
|
-
if (
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
if (tick <= 3 && /^ *$/.test(safe.slice(0, tick))) return '';
|
|
396
|
-
safe = safe.slice(0, tick);
|
|
637
|
+
if (heldMarkerLine !== null) {
|
|
638
|
+
if (classifyCallerResultWire(pending, { jsonOnly: true }) !== 'none') return '';
|
|
639
|
+
const safe = (!inlineTicks && classifyCallerResultWire(pending) !== 'none') ? '' : safePendingPreview(pending);
|
|
640
|
+
return (!hasVisibleContent && seen === 0) ? (leadingWhitespace + heldMarkerLine + safe) : (heldMarkerLine + safe);
|
|
397
641
|
}
|
|
642
|
+
if (!pending.trim() && pending.length <= 3) return '';
|
|
643
|
+
if (!hasVisibleContent && seen === 0 && !pending.trim()) return '';
|
|
644
|
+
let safe = safePendingPreview(pending);
|
|
398
645
|
if (seen > 0) {
|
|
399
646
|
const cut = callerResultPreviewCut(safe);
|
|
400
647
|
if (cut >= 0) safe = safe.slice(0, cut);
|
|
648
|
+
} else if (!inlineTicks && classifyCallerResultWire(pending) !== 'none') {
|
|
649
|
+
return '';
|
|
650
|
+
}
|
|
651
|
+
if (!hasVisibleContent && seen === 0) {
|
|
652
|
+
if (!safe.trim()) return '';
|
|
653
|
+
return leadingWhitespace + safe;
|
|
401
654
|
}
|
|
402
655
|
return safe;
|
|
403
656
|
},
|