@@ -8,6 +8,10 @@ const services = require('./services')
88const Sampler = require ( '../../dd-trace/src/sampler' )
99const { MEASURED } = require ( '../../../ext/tags' )
1010
11+ // String#replaceAll unavailable on Node.js@v 14 (dd-trace@<=v3)
12+ const RE_NEWLINE = / \n / g
13+ const RE_TAB = / \t / g
14+
1115// TODO: In the future we should refactor config.js to make it requirable
1216let MAX_TEXT_LEN = 128
1317
@@ -85,11 +89,11 @@ class OpenApiPlugin extends TracingPlugin {
8589 store . prompt = prompt
8690 if ( typeof prompt === 'string' || ( Array . isArray ( prompt ) && typeof prompt [ 0 ] === 'number' ) ) {
8791 // This is a single prompt, either String or [Number]
88- tags [ `openai.request.prompt` ] = normalizeStringOrTokenArray ( prompt )
92+ tags [ `openai.request.prompt` ] = normalizeStringOrTokenArray ( prompt , true )
8993 } else if ( Array . isArray ( prompt ) ) {
9094 // This is multiple prompts, either [String] or [[Number]]
9195 for ( let i = 0 ; i < prompt . length ; i ++ ) {
92- tags [ `openai.request.prompt.${ i } ` ] = normalizeStringOrTokenArray ( prompt [ i ] )
96+ tags [ `openai.request.prompt.${ i } ` ] = normalizeStringOrTokenArray ( prompt [ i ] , true )
9397 }
9498 }
9599 }
@@ -559,8 +563,8 @@ function truncateText (text) {
559563 if ( ! text ) return
560564
561565 text = text
562- . replaceAll ( '\n' , '\\n' )
563- . replaceAll ( '\t' , '\\t' )
566+ . replace ( RE_NEWLINE , '\\n' )
567+ . replace ( RE_TAB , '\\t' )
564568
565569 if ( text . length > MAX_TEXT_LEN ) {
566570 return text . substring ( 0 , MAX_TEXT_LEN ) + '...'
@@ -690,7 +694,7 @@ function normalizeRequestPayload (methodName, args) {
690694 * "foo" -> "foo"
691695 * [1,2,3] -> "[1, 2, 3]"
692696 */
693- function normalizeStringOrTokenArray ( input , truncate = true ) {
697+ function normalizeStringOrTokenArray ( input , truncate ) {
694698 const normalized = Array . isArray ( input )
695699 ? `[${ input . join ( ', ' ) } ]` // "[1, 2, 999]"
696700 : input // "foo"
0 commit comments