@@ -189,6 +189,28 @@ export function getErrorMessage(exception: unknown): string | undefined {
189189 * such as " at least one item is required" from matching.
190190 */
191191const JS_STACK_FRAME = / ^ \s + a t \s .* (?: : \d + : \d + \) ? | \( n a t i v e \) | < a n o n y m o u s > \) ? ) \s * $ / ;
192+ /**
193+ * The offset of the first stack frame line (see {@link JS_STACK_FRAME}), or -1. Walks the lines
194+ * with indexOf, so no array of lines is built and no line is looked at twice.
195+ */
196+ function indexOfFirstFrameLine ( text : string ) : number {
197+ let lineStart = 0 ;
198+ while ( lineStart <= text . length ) {
199+ let lineEnd = text . indexOf ( "\n" , lineStart ) ;
200+ if ( lineEnd === - 1 ) {
201+ lineEnd = text . length ;
202+ }
203+ // A frame line starts with whitespace; the cheap check skips the regex for all other lines
204+ if ( lineEnd > lineStart && isWhitespaceCode ( text . charCodeAt ( lineStart ) ) ) {
205+ if ( JS_STACK_FRAME . test ( text . slice ( lineStart , lineEnd ) ) ) {
206+ return lineStart ;
207+ }
208+ }
209+ lineStart = lineEnd + 1 ;
210+ }
211+ return - 1 ;
212+ }
213+
192214/** The header of a Python traceback relayed inside an error message */
193215const PYTHON_TRACEBACK = "Traceback (most recent call last):" ;
194216/** The lines Python prints between chained tracebacks */
@@ -198,8 +220,20 @@ const PYTHON_CHAINING_LINES: ReadonlySet<string> = new Set([
198220] ) ;
199221/** An error name on its own, followed by ":" ("Error:"), left in front of a relayed traceback */
200222const BARE_ERROR_NAME = / ^ [ A - Z a - z _ $ ] [ \w $ . ] * : $ / ;
201- /** "<Name>: <rest>": the header line that opens a JavaScript stack */
202- const NAMED_HEADER = / ^ ( [ A - Z a - z _ $ ] [ \w $ . ] * ) : ( [ \s \S ] * ) $ / ;
223+
224+ /** " + Exception Group" at the end of the text removed, then trimmed (linear, no regex) */
225+ function dropExceptionGroupSuffix ( text : string ) : string {
226+ const trimmed = text . trimEnd ( ) ;
227+ const suffix = "Exception Group" ;
228+ if ( ! trimmed . endsWith ( suffix ) ) {
229+ return text . trim ( ) ;
230+ }
231+ let end = trimmed . length - suffix . length ;
232+ while ( end > 0 && / [ \s + | ] / . test ( trimmed [ end - 1 ] ) ) {
233+ end -- ;
234+ }
235+ return trimmed . slice ( 0 , end ) . trim ( ) ;
236+ }
203237
204238/**
205239 * Removes relayed Python tracebacks: the "Traceback (most recent call last):" header and the
@@ -215,10 +249,7 @@ function stripPythonTracebacks(lines: string[]): { lines: string[]; stripped: bo
215249 if ( header !== - 1 ) {
216250 // e.g. "Error: Traceback (most recent call last):" when the traceback is an Error's message,
217251 // or " + Exception Group Traceback (most recent call last):"
218- const before = line
219- . slice ( 0 , header )
220- . replace ( / [ \s + | ] * E x c e p t i o n G r o u p \s * $ / , "" )
221- . trim ( ) ;
252+ const before = dropExceptionGroupSuffix ( line . slice ( 0 , header ) ) ;
222253 if ( before . length > 0 && ! BARE_ERROR_NAME . test ( before ) ) {
223254 kept . push ( before ) ;
224255 }
@@ -241,19 +272,94 @@ function stripPythonTracebacks(lines: string[]): { lines: string[]; stripped: bo
241272 return { lines : kept , stripped } ;
242273}
243274
275+ /** Same characters as the regex `\s` and `String.prototype.trim` */
276+ function isWhitespaceCode ( code : number ) : boolean {
277+ return (
278+ ( code >= 9 && code <= 13 ) ||
279+ code === 32 ||
280+ code === 160 ||
281+ code === 0x1680 ||
282+ ( code >= 0x2000 && code <= 0x200a ) ||
283+ code === 0x2028 ||
284+ code === 0x2029 ||
285+ code === 0x202f ||
286+ code === 0x205f ||
287+ code === 0x3000 ||
288+ code === 0xfeff
289+ ) ;
290+ }
291+
292+ function isNameStart ( code : number ) : boolean {
293+ return (
294+ ( code >= 65 && code <= 90 ) || ( code >= 97 && code <= 122 ) || code === 95 || code === 36
295+ ) ;
296+ }
297+
298+ function isNamePart ( code : number ) : boolean {
299+ return isNameStart ( code ) || ( code >= 48 && code <= 57 ) || code === 46 ;
300+ }
301+
244302/**
245303 * LangChain's `run.error` is `message + "\n\n" + stack`, and a stack opens with
246304 * `"<Name>: <message>"`. When the text is the message followed by that header, returns the
247305 * message, so it is not repeated.
306+ *
307+ * Linear in the text size: every index only moves forward, no tail is sliced or matched by a
308+ * regex per separator, and the message is compared only when its length can fit the tail.
248309 */
249310function dropRepeatedStackHeader ( text : string ) : string {
311+ const length = text . length ;
250312 let separator = text . indexOf ( "\n\n" ) ;
313+ if ( separator === - 1 ) {
314+ return text ;
315+ }
316+ // The text trimmed: [start, end)
317+ let start = 0 ;
318+ while ( start < length && isWhitespaceCode ( text . charCodeAt ( start ) ) ) {
319+ start ++ ;
320+ }
321+ let end = length ;
322+ while ( end > start && isWhitespaceCode ( text . charCodeAt ( end - 1 ) ) ) {
323+ end -- ;
324+ }
325+ let scanned = start ; // text[start, scanned) was scanned for the last non-whitespace
326+ let messageEnd = start ; // end of the trimmed text before the separator
327+ let tail = 0 ; // the first non-whitespace at or after the last separator + 2
251328 while ( separator !== - 1 ) {
252- const message = text . slice ( 0 , separator ) . trim ( ) ;
253- const rest = text . slice ( separator + 2 ) . trim ( ) ;
254- const header = NAMED_HEADER . exec ( rest ) ;
255- if ( message . length > 0 && ( rest === message || header ?. [ 2 ] . trim ( ) === message ) ) {
256- return message ;
329+ for ( ; scanned < separator ; scanned ++ ) {
330+ if ( ! isWhitespaceCode ( text . charCodeAt ( scanned ) ) ) {
331+ messageEnd = scanned + 1 ;
332+ }
333+ }
334+ const messageLength = messageEnd - start ;
335+ if ( messageLength > 0 ) {
336+ tail = Math . max ( tail , separator + 2 ) ;
337+ while ( tail < end && isWhitespaceCode ( text . charCodeAt ( tail ) ) ) {
338+ tail ++ ;
339+ }
340+ // rest === message
341+ if ( tail + messageLength === end && text . startsWith ( text . slice ( start , messageEnd ) , tail ) ) {
342+ return text . slice ( start , messageEnd ) ;
343+ }
344+ // rest is "<Name>: <message>"
345+ if ( tail < end && isNameStart ( text . charCodeAt ( tail ) ) ) {
346+ let cursor = tail + 1 ;
347+ while ( cursor < end && isNamePart ( text . charCodeAt ( cursor ) ) ) {
348+ cursor ++ ;
349+ }
350+ if ( text . charCodeAt ( cursor ) === 58 && text . charCodeAt ( cursor + 1 ) === 32 ) {
351+ cursor += 2 ;
352+ while ( cursor < end && isWhitespaceCode ( text . charCodeAt ( cursor ) ) ) {
353+ cursor ++ ;
354+ }
355+ if (
356+ cursor + messageLength === end &&
357+ text . startsWith ( text . slice ( start , messageEnd ) , cursor )
358+ ) {
359+ return text . slice ( start , messageEnd ) ;
360+ }
361+ }
362+ }
257363 }
258364 separator = text . indexOf ( "\n\n" , separator + 2 ) ;
259365 }
@@ -268,20 +374,21 @@ function dropRepeatedStackHeader(text: string): string {
268374 * final "Type: message" line is kept. Text without a stack is returned unchanged.
269375 */
270376export function stripStackTrace ( description : string ) : string {
271- let lines = description . split ( "\n" ) ;
272- // A JS stack: everything from the first frame on is the stack.
273- const firstFrame = lines . findIndex ( ( line ) => JS_STACK_FRAME . test ( line ) ) ;
274- let stripped = firstFrame !== - 1 ;
275- if ( stripped ) {
276- lines = lines . slice ( 0 , firstFrame ) ;
277- }
278- const python = stripPythonTracebacks ( lines ) ;
279- stripped = stripped || python . stripped ;
377+ // A JS stack: everything from the first frame line on is the stack.
378+ const frame = indexOfFirstFrameLine ( description ) ;
379+ let stripped = frame !== - 1 ;
380+ let text = frame === - 1 ? description : description . slice ( 0 , Math . max ( frame - 1 , 0 ) ) ;
381+ if ( text . includes ( PYTHON_TRACEBACK ) ) {
382+ // Runs of blank lines end up as one blank line anyway; collapsing them first keeps a message
383+ // of a million newlines from becoming a million-element array.
384+ const python = stripPythonTracebacks ( text . replace ( / \n { 3 , } / g, "\n\n" ) . split ( "\n" ) ) ;
385+ stripped = stripped || python . stripped ;
386+ text = python . lines . join ( "\n" ) ;
387+ }
280388 if ( ! stripped ) {
281389 return description ;
282390 }
283- const text = python . lines . join ( "\n" ) . replace ( / \n { 3 , } / g, "\n\n" ) . trim ( ) ;
284- return dropRepeatedStackHeader ( text ) ;
391+ return dropRepeatedStackHeader ( text . replace ( / \n { 3 , } / g, "\n\n" ) . trim ( ) ) ;
285392}
286393
287394/**
0 commit comments