@librechat/agents 1.9.96 → 1.9.97

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.
@@ -74,7 +74,7 @@ class SplitStreamHandler {
74
74
  };
75
75
  this.handlers?.[_enum.GraphEvents.ON_REASONING_DELTA]?.({ event: _enum.GraphEvents.ON_REASONING_DELTA, data: reasoningDelta });
76
76
  };
77
- handleContent = (content, stepId, _type) => {
77
+ handleContent = (content, _type) => {
78
78
  let type = _type;
79
79
  if (this.inThinkBlock && type === _enum.ContentTypes.TEXT) {
80
80
  type = _enum.ContentTypes.THINK;
@@ -96,10 +96,7 @@ class SplitStreamHandler {
96
96
  },
97
97
  });
98
98
  }
99
- if (content.includes('```')) {
100
- this.inCodeBlock = !this.inCodeBlock;
101
- }
102
- this.currentLength += content.length;
99
+ const stepId = this.currentStepId ?? '';
103
100
  if (type === _enum.ContentTypes.THINK) {
104
101
  this.dispatchReasoningDelta(stepId, {
105
102
  content: [{
@@ -116,6 +113,7 @@ class SplitStreamHandler {
116
113
  }],
117
114
  });
118
115
  }
116
+ this.currentLength += content.length;
119
117
  if (this.inCodeBlock) {
120
118
  return;
121
119
  }
@@ -138,6 +136,9 @@ class SplitStreamHandler {
138
136
  if (!content.length && !reasoning_content.length) {
139
137
  return;
140
138
  }
139
+ if (content.includes('```')) {
140
+ this.inCodeBlock = !this.inCodeBlock;
141
+ }
141
142
  if (content === '<think>' && !this.inCodeBlock) {
142
143
  this.inThinkBlock = true;
143
144
  }
@@ -157,15 +158,11 @@ class SplitStreamHandler {
157
158
  },
158
159
  });
159
160
  }
160
- const stepId = this.currentStepId ?? '';
161
- if (!stepId) {
162
- return;
163
- }
164
161
  if (reasoning_content) {
165
- this.handleContent(reasoning_content, stepId, _enum.ContentTypes.THINK);
162
+ this.handleContent(reasoning_content, _enum.ContentTypes.THINK);
166
163
  }
167
164
  else {
168
- this.handleContent(content, stepId, _enum.ContentTypes.TEXT);
165
+ this.handleContent(content, _enum.ContentTypes.TEXT);
169
166
  }
170
167
  }
171
168
  }
@@ -1 +1 @@
1
- {"version":3,"file":"splitStream.cjs","sources":["../../src/splitStream.ts"],"sourcesContent":["import { nanoid } from 'nanoid';\nimport type * as t from '@/types';\nimport { ContentTypes, GraphEvents, StepTypes } from '@/common';\n\nexport const SEPARATORS = ['.', '?', '!', '۔', '。', '‥', ';', '¡', '¿', '\\n', '```'];\n\nexport class SplitStreamHandler {\n private inCodeBlock = false;\n private inThinkBlock = false;\n private accumulate: boolean;\n tokens: string[] = [];\n lastToken = '';\n reasoningTokens: string[] = [];\n currentStepId?: string;\n currentMessageId?: string;\n currentType?: ContentTypes.TEXT | ContentTypes.THINK;\n currentLength = 0;\n reasoningKey: 'reasoning_content' | 'reasoning' = 'reasoning_content';\n currentIndex = -1;\n blockThreshold = 4500;\n /** The run ID AKA the Message ID associated with the complete generation */\n runId: string;\n handlers?: t.SplitStreamHandlers;\n constructor({\n runId,\n handlers,\n accumulate,\n reasoningKey,\n blockThreshold,\n }: {\n runId: string,\n accumulate?: boolean,\n handlers: t.SplitStreamHandlers\n blockThreshold?: number,\n reasoningKey?: 'reasoning_content' | 'reasoning',\n }) {\n this.runId = runId;\n this.handlers = handlers;\n if (reasoningKey) {\n this.reasoningKey = reasoningKey;\n }\n if (blockThreshold != null) {\n this.blockThreshold = blockThreshold;\n }\n this.accumulate = accumulate ?? false;\n }\n getMessageId = (): string | undefined => {\n const messageId = this.currentMessageId;\n if (messageId != null && messageId) {\n return messageId;\n }\n return undefined;\n };\n createMessageStep = (type?: ContentTypes.TEXT | ContentTypes.THINK): [string, string] => {\n if (type != null && this.currentType !== type) {\n this.currentType = type;\n }\n this.currentLength = 0;\n this.currentIndex += 1;\n this.currentStepId = `step_${nanoid()}`;\n this.currentMessageId = `msg_${nanoid()}`;\n return [this.currentStepId, this.currentMessageId];\n };\n dispatchRunStep = (stepId: string, stepDetails: t.StepDetails): void => {\n const runStep: t.RunStep = {\n id: stepId,\n runId: this.runId,\n type: stepDetails.type,\n index: this.currentIndex,\n stepDetails,\n // usage: null,\n };\n this.handlers?.[GraphEvents.ON_RUN_STEP]?.({ event: GraphEvents.ON_RUN_STEP, data: runStep });\n };\n dispatchMessageDelta = (stepId: string, delta: t.MessageDelta): void => {\n const messageDelta: t.MessageDeltaEvent = {\n id: stepId,\n delta,\n };\n this.handlers?.[GraphEvents.ON_MESSAGE_DELTA]?.({ event: GraphEvents.ON_MESSAGE_DELTA, data: messageDelta });\n };\n dispatchReasoningDelta = (stepId: string, delta: t.ReasoningDelta): void => {\n const reasoningDelta: t.ReasoningDeltaEvent = {\n id: stepId,\n delta,\n };\n this.handlers?.[GraphEvents.ON_REASONING_DELTA]?.({ event: GraphEvents.ON_REASONING_DELTA, data: reasoningDelta });\n };\n handleContent = (content: string, stepId: string, _type: ContentTypes.TEXT | ContentTypes.THINK): void => {\n let type = _type;\n if (this.inThinkBlock && type === ContentTypes.TEXT) {\n type = ContentTypes.THINK;\n }\n if (this.accumulate) {\n if (type === ContentTypes.THINK) {\n this.reasoningTokens.push(content);\n } else {\n this.tokens.push(content);\n }\n }\n\n if (this.currentType !== type) {\n const [newStepId, newMessageId] = this.createMessageStep(type);\n this.dispatchRunStep(newStepId, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: newMessageId,\n },\n });\n }\n\n if (content.includes('```')) {\n this.inCodeBlock = !this.inCodeBlock;\n }\n\n this.currentLength += content.length;\n\n if (type === ContentTypes.THINK) {\n this.dispatchReasoningDelta(stepId, {\n content: [{\n type: ContentTypes.THINK,\n think: content,\n }],\n });\n } else {\n this.dispatchMessageDelta(stepId, {\n content: [{\n type: ContentTypes.TEXT,\n text: content,\n }],\n });\n }\n\n if (this.inCodeBlock) {\n return;\n }\n\n if (this.currentLength > this.blockThreshold && SEPARATORS.some(sep => content.includes(sep))) {\n const [newStepId, newMessageId] = this.createMessageStep(type);\n this.dispatchRunStep(newStepId, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: newMessageId,\n },\n });\n }\n };\n handle(chunk?: t.CustomChunk): void {\n if (!chunk) {\n return;\n }\n\n const content = chunk.choices?.[0]?.delta.content ?? '';\n const reasoning_content = chunk.choices?.[0]?.delta[this.reasoningKey] ?? '';\n\n if (!content.length && !reasoning_content.length) {\n return;\n }\n\n if (content === '<think>' && !this.inCodeBlock) {\n this.inThinkBlock = true;\n } else if (this.lastToken === '</think>' && !this.inCodeBlock) {\n this.inThinkBlock = false;\n }\n\n this.lastToken = content;\n\n const message_id = this.getMessageId() ?? '';\n\n if (!message_id) {\n const initialContentType = this.inThinkBlock ? ContentTypes.THINK : ContentTypes.TEXT;\n const initialType = reasoning_content ? ContentTypes.THINK : initialContentType;\n const [stepId, message_id] = this.createMessageStep(initialType);\n this.dispatchRunStep(stepId, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n });\n }\n\n const stepId = this.currentStepId ?? '';\n if (!stepId) {\n return;\n }\n\n if (reasoning_content) {\n this.handleContent(reasoning_content, stepId, ContentTypes.THINK);\n } else {\n this.handleContent(content, stepId, ContentTypes.TEXT);\n }\n }\n}"],"names":["nanoid","GraphEvents","ContentTypes","StepTypes"],"mappings":";;;;;AAIa,MAAA,UAAU,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE;MAExE,kBAAkB,CAAA;IACrB,WAAW,GAAG,KAAK,CAAC;IACpB,YAAY,GAAG,KAAK,CAAC;AACrB,IAAA,UAAU,CAAU;IAC5B,MAAM,GAAa,EAAE,CAAC;IACtB,SAAS,GAAG,EAAE,CAAC;IACf,eAAe,GAAa,EAAE,CAAC;AAC/B,IAAA,aAAa,CAAU;AACvB,IAAA,gBAAgB,CAAU;AAC1B,IAAA,WAAW,CAA0C;IACrD,aAAa,GAAG,CAAC,CAAC;IAClB,YAAY,GAAs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,KAAK,CAAC;SAC3B;AAED,QAAA,IAAI,CAAC,SAAS,GAAG,OAAO,CAAC;QAEzB,MAAM,UAAU,GAAG,IAAI,CAAC,YAAY,EAAE,IAAI,EAAE,CAAC;QAE7C,IAAI,CAAC,UAAU,EAAE;AACf,YAAA,MAAM,kBAAkB,GAAG,IAAI,CAAC,YAAY,GAAGD,kBAAY,CAAC,KAAK,GAAGA,kBAAY,CAAC,IAAI,CAAC;AACtF,YAAA,MAAM,WAAW,GAAG,iBAAiB,GAAGA,kBAAY,CAAC,KAAK,GAAG,kBAAkB,CAAC;AAChF,YAAA,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,IAAI,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAC;AACjE,YAAA,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE;gBAC3B,IAAI,EAAEC,eAAS,CAAC,gBAAgB;AAChC,gBAAA,gBAAgB,EAAE;oBAChB,UAAU;AACX,iBAAA;AACF,aAAA,CAAC,CAAC;SACJ;AAED,QAAA,MAAM,MAAM,GAAG,IAAI,CAAC,aAAa,IAAI,EAAE,CAAC;QACxC,IAAI,CAAC,MAAM,EAAE;YACX,OAAO;SACR;QAED,IAAI,iBAAiB,EAAE;YACrB,IAAI,CAAC,aAAa,CAAC,iBAAiB,EAAE,MAAM,EAAED,kBAAY,CAAC,KAAK,CAAC,CAAC;SACnE;aAAM;YACL,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,MAAM,EAAEA,kBAAY,CAAC,IAAI,CAAC,CAAC;SACxD;KACF;AACF;;;;;"}
1
+ {"version":3,"file":"splitStream.cjs","sources":["../../src/splitStream.ts"],"sourcesContent":["import { nanoid } from 'nanoid';\nimport type * as t from '@/types';\nimport { ContentTypes, GraphEvents, StepTypes } from '@/common';\n\nexport const SEPARATORS = ['.', '?', '!', '۔', '。', '‥', ';', '¡', '¿', '\\n', '```'];\n\nexport class SplitStreamHandler {\n private inCodeBlock = false;\n private inThinkBlock = false;\n private accumulate: boolean;\n tokens: string[] = [];\n lastToken = '';\n reasoningTokens: string[] = [];\n currentStepId?: string;\n currentMessageId?: string;\n currentType?: ContentTypes.TEXT | ContentTypes.THINK;\n currentLength = 0;\n reasoningKey: 'reasoning_content' | 'reasoning' = 'reasoning_content';\n currentIndex = -1;\n blockThreshold = 4500;\n /** The run ID AKA the Message ID associated with the complete generation */\n runId: string;\n handlers?: t.SplitStreamHandlers;\n constructor({\n runId,\n handlers,\n accumulate,\n reasoningKey,\n blockThreshold,\n }: {\n runId: string,\n accumulate?: boolean,\n handlers: t.SplitStreamHandlers\n blockThreshold?: number,\n reasoningKey?: 'reasoning_content' | 'reasoning',\n }) {\n this.runId = runId;\n this.handlers = handlers;\n if (reasoningKey) {\n this.reasoningKey = reasoningKey;\n }\n if (blockThreshold != null) {\n this.blockThreshold = blockThreshold;\n }\n this.accumulate = accumulate ?? false;\n }\n getMessageId = (): string | undefined => {\n const messageId = this.currentMessageId;\n if (messageId != null && messageId) {\n return messageId;\n }\n return undefined;\n };\n createMessageStep = (type?: ContentTypes.TEXT | ContentTypes.THINK): [string, string] => {\n if (type != null && this.currentType !== type) {\n this.currentType = type;\n }\n this.currentLength = 0;\n this.currentIndex += 1;\n this.currentStepId = `step_${nanoid()}`;\n this.currentMessageId = `msg_${nanoid()}`;\n return [this.currentStepId, this.currentMessageId];\n };\n dispatchRunStep = (stepId: string, stepDetails: t.StepDetails): void => {\n const runStep: t.RunStep = {\n id: stepId,\n runId: this.runId,\n type: stepDetails.type,\n index: this.currentIndex,\n stepDetails,\n // usage: null,\n };\n this.handlers?.[GraphEvents.ON_RUN_STEP]?.({ event: GraphEvents.ON_RUN_STEP, data: runStep });\n };\n dispatchMessageDelta = (stepId: string, delta: t.MessageDelta): void => {\n const messageDelta: t.MessageDeltaEvent = {\n id: stepId,\n delta,\n };\n this.handlers?.[GraphEvents.ON_MESSAGE_DELTA]?.({ event: GraphEvents.ON_MESSAGE_DELTA, data: messageDelta });\n };\n dispatchReasoningDelta = (stepId: string, delta: t.ReasoningDelta): void => {\n const reasoningDelta: t.ReasoningDeltaEvent = {\n id: stepId,\n delta,\n };\n this.handlers?.[GraphEvents.ON_REASONING_DELTA]?.({ event: GraphEvents.ON_REASONING_DELTA, data: reasoningDelta });\n };\n handleContent = (content: string, _type: ContentTypes.TEXT | ContentTypes.THINK): void => {\n let type = _type;\n if (this.inThinkBlock && type === ContentTypes.TEXT) {\n type = ContentTypes.THINK;\n }\n if (this.accumulate) {\n if (type === ContentTypes.THINK) {\n this.reasoningTokens.push(content);\n } else {\n this.tokens.push(content);\n }\n }\n\n if (this.currentType !== type) {\n const [newStepId, newMessageId] = this.createMessageStep(type);\n this.dispatchRunStep(newStepId, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: newMessageId,\n },\n });\n }\n\n const stepId = this.currentStepId ?? '';\n if (type === ContentTypes.THINK) {\n this.dispatchReasoningDelta(stepId, {\n content: [{\n type: ContentTypes.THINK,\n think: content,\n }],\n });\n } else {\n this.dispatchMessageDelta(stepId, {\n content: [{\n type: ContentTypes.TEXT,\n text: content,\n }],\n });\n }\n\n this.currentLength += content.length;\n if (this.inCodeBlock) {\n return;\n }\n\n if (this.currentLength > this.blockThreshold && SEPARATORS.some(sep => content.includes(sep))) {\n const [newStepId, newMessageId] = this.createMessageStep(type);\n this.dispatchRunStep(newStepId, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: newMessageId,\n },\n });\n }\n };\n handle(chunk?: t.CustomChunk): void {\n if (!chunk) {\n return;\n }\n\n const content = chunk.choices?.[0]?.delta.content ?? '';\n const reasoning_content = chunk.choices?.[0]?.delta[this.reasoningKey] ?? '';\n\n if (!content.length && !reasoning_content.length) {\n return;\n }\n\n if (content.includes('```')) {\n this.inCodeBlock = !this.inCodeBlock;\n }\n\n if (content === '<think>' && !this.inCodeBlock) {\n this.inThinkBlock = true;\n } else if (this.lastToken === '</think>' && !this.inCodeBlock) {\n this.inThinkBlock = false;\n }\n\n this.lastToken = content;\n\n const message_id = this.getMessageId() ?? '';\n\n if (!message_id) {\n const initialContentType = this.inThinkBlock ? ContentTypes.THINK : ContentTypes.TEXT;\n const initialType = reasoning_content ? ContentTypes.THINK : initialContentType;\n const [stepId, message_id] = this.createMessageStep(initialType);\n this.dispatchRunStep(stepId, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n });\n }\n\n if (reasoning_content) {\n this.handleContent(reasoning_content, ContentTypes.THINK);\n } else {\n this.handleContent(content, ContentTypes.TEXT);\n }\n }\n}"],"names":["nanoid","GraphEvents","ContentTypes","StepTypes"],"mappings":";;;;;AAIa,MAAA,UAAU,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE;MAExE,kBAAkB,CAAA;IACrB,WAAW,GAAG,KAAK,CAAC;IACpB,YAAY,GAAG,KAAK,CAAC;AACrB,IAAA,UAAU,CAAU;IAC5B,MAAM,GAAa,EAAE,CAAC;IACtB,SAAS,GAAG,EAAE,CAAC;IACf,eAAe,GAAa,EAAE,CAAC;AAC/B,IAAA,aAAa,CAAU;AACvB,IAAA,gBAAgB,CAAU;AAC1B,IAAA,WAAW,CAA0C;IACrD,aAAa,GAAG,CAAC,CAAC;IAClB,YAAY,GAAsC,mBAAmB,CAAC;IACtE,YAAY,GAAG,CAAC,CAAC,CAAC;IAClB,cAAc,GAAG,IAAI,CAAC;;AAEtB,IAAA,KAAK,CAAS;AACd,IAAA,QAAQ,CAAyB;IACjC,WAAY,CAAA,EACV,KAAK,EACL,QAAQ,EACR,UAAU,EACV,YAAY,EACZ,cAAc,GAOb,EAAA;AACD,QAAA,IAAI,CAAC,KAAK,GAAG,KAAK,CAAC;AACnB,QAAA,IAAI,CAAC,QAAQ,GAAG,QAAQ,CAAC;QACzB,IAAI,YAAY,EAAE;AAChB,YAAA,I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W,GAAG,iBAAiB,GAAGA,kBAAY,CAAC,KAAK,GAAG,kBAAkB,CAAC;AAChF,YAAA,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,IAAI,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAC;AACjE,YAAA,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE;gBAC3B,IAAI,EAAEC,eAAS,CAAC,gBAAgB;AAChC,gBAAA,gBAAgB,EAAE;oBAChB,UAAU;AACX,iBAAA;AACF,aAAA,CAAC,CAAC;SACJ;QAED,IAAI,iBAAiB,EAAE;YACrB,IAAI,CAAC,aAAa,CAAC,iBAAiB,EAAED,kBAAY,CAAC,KAAK,CAAC,CAAC;SAC3D;aAAM;YACL,IAAI,CAAC,aAAa,CAAC,OAAO,EAAEA,kBAAY,CAAC,IAAI,CAAC,CAAC;SAChD;KACF;AACF;;;;;"}
@@ -72,7 +72,7 @@ class SplitStreamHandler {
72
72
  };
73
73
  this.handlers?.[GraphEvents.ON_REASONING_DELTA]?.({ event: GraphEvents.ON_REASONING_DELTA, data: reasoningDelta });
74
74
  };
75
- handleContent = (content, stepId, _type) => {
75
+ handleContent = (content, _type) => {
76
76
  let type = _type;
77
77
  if (this.inThinkBlock && type === ContentTypes.TEXT) {
78
78
  type = ContentTypes.THINK;
@@ -94,10 +94,7 @@ class SplitStreamHandler {
94
94
  },
95
95
  });
96
96
  }
97
- if (content.includes('```')) {
98
- this.inCodeBlock = !this.inCodeBlock;
99
- }
100
- this.currentLength += content.length;
97
+ const stepId = this.currentStepId ?? '';
101
98
  if (type === ContentTypes.THINK) {
102
99
  this.dispatchReasoningDelta(stepId, {
103
100
  content: [{
@@ -114,6 +111,7 @@ class SplitStreamHandler {
114
111
  }],
115
112
  });
116
113
  }
114
+ this.currentLength += content.length;
117
115
  if (this.inCodeBlock) {
118
116
  return;
119
117
  }
@@ -136,6 +134,9 @@ class SplitStreamHandler {
136
134
  if (!content.length && !reasoning_content.length) {
137
135
  return;
138
136
  }
137
+ if (content.includes('```')) {
138
+ this.inCodeBlock = !this.inCodeBlock;
139
+ }
139
140
  if (content === '<think>' && !this.inCodeBlock) {
140
141
  this.inThinkBlock = true;
141
142
  }
@@ -155,15 +156,11 @@ class SplitStreamHandler {
155
156
  },
156
157
  });
157
158
  }
158
- const stepId = this.currentStepId ?? '';
159
- if (!stepId) {
160
- return;
161
- }
162
159
  if (reasoning_content) {
163
- this.handleContent(reasoning_content, stepId, ContentTypes.THINK);
160
+ this.handleContent(reasoning_content, ContentTypes.THINK);
164
161
  }
165
162
  else {
166
- this.handleContent(content, stepId, ContentTypes.TEXT);
163
+ this.handleContent(content, ContentTypes.TEXT);
167
164
  }
168
165
  }
169
166
  }
@@ -1 +1 @@
1
- {"version":3,"file":"splitStream.mjs","sources":["../../src/splitStream.ts"],"sourcesContent":["import { nanoid } from 'nanoid';\nimport type * as t from '@/types';\nimport { ContentTypes, GraphEvents, StepTypes } from '@/common';\n\nexport const SEPARATORS = ['.', '?', '!', '۔', '。', '‥', ';', '¡', '¿', '\\n', '```'];\n\nexport class SplitStreamHandler {\n private inCodeBlock = false;\n private inThinkBlock = false;\n private accumulate: boolean;\n tokens: string[] = [];\n lastToken = '';\n reasoningTokens: string[] = [];\n currentStepId?: string;\n currentMessageId?: string;\n currentType?: ContentTypes.TEXT | ContentTypes.THINK;\n currentLength = 0;\n reasoningKey: 'reasoning_content' | 'reasoning' = 'reasoning_content';\n currentIndex = -1;\n blockThreshold = 4500;\n /** The run ID AKA the Message ID associated with the complete generation */\n runId: string;\n handlers?: t.SplitStreamHandlers;\n constructor({\n runId,\n handlers,\n accumulate,\n reasoningKey,\n blockThreshold,\n }: {\n runId: string,\n accumulate?: boolean,\n handlers: t.SplitStreamHandlers\n blockThreshold?: number,\n reasoningKey?: 'reasoning_content' | 'reasoning',\n }) {\n this.runId = runId;\n this.handlers = handlers;\n if (reasoningKey) {\n this.reasoningKey = reasoningKey;\n }\n if (blockThreshold != null) {\n this.blockThreshold = blockThreshold;\n }\n this.accumulate = accumulate ?? false;\n }\n getMessageId = (): string | undefined => {\n const messageId = this.currentMessageId;\n if (messageId != null && messageId) {\n return messageId;\n }\n return undefined;\n };\n createMessageStep = (type?: ContentTypes.TEXT | ContentTypes.THINK): [string, string] => {\n if (type != null && this.currentType !== type) {\n this.currentType = type;\n }\n this.currentLength = 0;\n this.currentIndex += 1;\n this.currentStepId = `step_${nanoid()}`;\n this.currentMessageId = `msg_${nanoid()}`;\n return [this.currentStepId, this.currentMessageId];\n };\n dispatchRunStep = (stepId: string, stepDetails: t.StepDetails): void => {\n const runStep: t.RunStep = {\n id: stepId,\n runId: this.runId,\n type: stepDetails.type,\n index: this.currentIndex,\n stepDetails,\n // usage: null,\n };\n this.handlers?.[GraphEvents.ON_RUN_STEP]?.({ event: GraphEvents.ON_RUN_STEP, data: runStep });\n };\n dispatchMessageDelta = (stepId: string, delta: t.MessageDelta): void => {\n const messageDelta: t.MessageDeltaEvent = {\n id: stepId,\n delta,\n };\n this.handlers?.[GraphEvents.ON_MESSAGE_DELTA]?.({ event: GraphEvents.ON_MESSAGE_DELTA, data: messageDelta });\n };\n dispatchReasoningDelta = (stepId: string, delta: t.ReasoningDelta): void => {\n const reasoningDelta: t.ReasoningDeltaEvent = {\n id: stepId,\n delta,\n };\n this.handlers?.[GraphEvents.ON_REASONING_DELTA]?.({ event: GraphEvents.ON_REASONING_DELTA, data: reasoningDelta });\n };\n handleContent = (content: string, stepId: string, _type: ContentTypes.TEXT | ContentTypes.THINK): void => {\n let type = _type;\n if (this.inThinkBlock && type === ContentTypes.TEXT) {\n type = ContentTypes.THINK;\n }\n if (this.accumulate) {\n if (type === ContentTypes.THINK) {\n this.reasoningTokens.push(content);\n } else {\n this.tokens.push(content);\n }\n }\n\n if (this.currentType !== type) {\n const [newStepId, newMessageId] = this.createMessageStep(type);\n this.dispatchRunStep(newStepId, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: newMessageId,\n },\n });\n }\n\n if (content.includes('```')) {\n this.inCodeBlock = !this.inCodeBlock;\n }\n\n this.currentLength += content.length;\n\n if (type === ContentTypes.THINK) {\n this.dispatchReasoningDelta(stepId, {\n content: [{\n type: ContentTypes.THINK,\n think: content,\n }],\n });\n } else {\n this.dispatchMessageDelta(stepId, {\n content: [{\n type: ContentTypes.TEXT,\n text: content,\n }],\n });\n }\n\n if (this.inCodeBlock) {\n return;\n }\n\n if (this.currentLength > this.blockThreshold && SEPARATORS.some(sep => content.includes(sep))) {\n const [newStepId, newMessageId] = this.createMessageStep(type);\n this.dispatchRunStep(newStepId, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: newMessageId,\n },\n });\n }\n };\n handle(chunk?: t.CustomChunk): void {\n if (!chunk) {\n return;\n }\n\n const content = chunk.choices?.[0]?.delta.content ?? '';\n const reasoning_content = chunk.choices?.[0]?.delta[this.reasoningKey] ?? '';\n\n if (!content.length && !reasoning_content.length) {\n return;\n }\n\n if (content === '<think>' && !this.inCodeBlock) {\n this.inThinkBlock = true;\n } else if (this.lastToken === '</think>' && !this.inCodeBlock) {\n this.inThinkBlock = false;\n }\n\n this.lastToken = content;\n\n const message_id = this.getMessageId() ?? '';\n\n if (!message_id) {\n const initialContentType = this.inThinkBlock ? ContentTypes.THINK : ContentTypes.TEXT;\n const initialType = reasoning_content ? ContentTypes.THINK : initialContentType;\n const [stepId, message_id] = this.createMessageStep(initialType);\n this.dispatchRunStep(stepId, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n });\n }\n\n const stepId = this.currentStepId ?? '';\n if (!stepId) {\n return;\n }\n\n if (reasoning_content) {\n this.handleContent(reasoning_content, stepId, ContentTypes.THINK);\n } else {\n this.handleContent(content, stepId, ContentTypes.TEXT);\n }\n }\n}"],"names":[],"mappings":";;;AAIa,MAAA,UAAU,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,E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EAAE,YAAY,CAAC,IAAI,CAAC,CAAC;SACxD;KACF;AACF;;;;"}
1
+ {"version":3,"file":"splitStream.mjs","sources":["../../src/splitStream.ts"],"sourcesContent":["import { nanoid } from 'nanoid';\nimport type * as t from '@/types';\nimport { ContentTypes, GraphEvents, StepTypes } from '@/common';\n\nexport const SEPARATORS = ['.', '?', '!', '۔', '。', '‥', ';', '¡', '¿', '\\n', '```'];\n\nexport class SplitStreamHandler {\n private inCodeBlock = false;\n private inThinkBlock = false;\n private accumulate: boolean;\n tokens: string[] = [];\n lastToken = '';\n reasoningTokens: string[] = [];\n currentStepId?: string;\n currentMessageId?: string;\n currentType?: ContentTypes.TEXT | ContentTypes.THINK;\n currentLength = 0;\n reasoningKey: 'reasoning_content' | 'reasoning' = 'reasoning_content';\n currentIndex = -1;\n blockThreshold = 4500;\n /** The run ID AKA the Message ID associated with the complete generation */\n runId: string;\n handlers?: t.SplitStreamHandlers;\n constructor({\n runId,\n handlers,\n accumulate,\n reasoningKey,\n blockThreshold,\n }: {\n runId: string,\n accumulate?: boolean,\n handlers: t.SplitStreamHandlers\n blockThreshold?: number,\n reasoningKey?: 'reasoning_content' | 'reasoning',\n }) {\n this.runId = runId;\n this.handlers = handlers;\n if (reasoningKey) {\n this.reasoningKey = reasoningKey;\n }\n if (blockThreshold != null) {\n this.blockThreshold = blockThreshold;\n }\n this.accumulate = accumulate ?? false;\n }\n getMessageId = (): string | undefined => {\n const messageId = this.currentMessageId;\n if (messageId != null && messageId) {\n return messageId;\n }\n return undefined;\n };\n createMessageStep = (type?: ContentTypes.TEXT | ContentTypes.THINK): [string, string] => {\n if (type != null && this.currentType !== type) {\n this.currentType = type;\n }\n this.currentLength = 0;\n this.currentIndex += 1;\n this.currentStepId = `step_${nanoid()}`;\n this.currentMessageId = `msg_${nanoid()}`;\n return [this.currentStepId, this.currentMessageId];\n };\n dispatchRunStep = (stepId: string, stepDetails: t.StepDetails): void => {\n const runStep: t.RunStep = {\n id: stepId,\n runId: this.runId,\n type: stepDetails.type,\n index: this.currentIndex,\n stepDetails,\n // usage: null,\n };\n this.handlers?.[GraphEvents.ON_RUN_STEP]?.({ event: GraphEvents.ON_RUN_STEP, data: runStep });\n };\n dispatchMessageDelta = (stepId: string, delta: t.MessageDelta): void => {\n const messageDelta: t.MessageDeltaEvent = {\n id: stepId,\n delta,\n };\n this.handlers?.[GraphEvents.ON_MESSAGE_DELTA]?.({ event: GraphEvents.ON_MESSAGE_DELTA, data: messageDelta });\n };\n dispatchReasoningDelta = (stepId: string, delta: t.ReasoningDelta): void => {\n const reasoningDelta: t.ReasoningDeltaEvent = {\n id: stepId,\n delta,\n };\n this.handlers?.[GraphEvents.ON_REASONING_DELTA]?.({ event: GraphEvents.ON_REASONING_DELTA, data: reasoningDelta });\n };\n handleContent = (content: string, _type: ContentTypes.TEXT | ContentTypes.THINK): void => {\n let type = _type;\n if (this.inThinkBlock && type === ContentTypes.TEXT) {\n type = ContentTypes.THINK;\n }\n if (this.accumulate) {\n if (type === ContentTypes.THINK) {\n this.reasoningTokens.push(content);\n } else {\n this.tokens.push(content);\n }\n }\n\n if (this.currentType !== type) {\n const [newStepId, newMessageId] = this.createMessageStep(type);\n this.dispatchRunStep(newStepId, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: newMessageId,\n },\n });\n }\n\n const stepId = this.currentStepId ?? '';\n if (type === ContentTypes.THINK) {\n this.dispatchReasoningDelta(stepId, {\n content: [{\n type: ContentTypes.THINK,\n think: content,\n }],\n });\n } else {\n this.dispatchMessageDelta(stepId, {\n content: [{\n type: ContentTypes.TEXT,\n text: content,\n }],\n });\n }\n\n this.currentLength += content.length;\n if (this.inCodeBlock) {\n return;\n }\n\n if (this.currentLength > this.blockThreshold && SEPARATORS.some(sep => content.includes(sep))) {\n const [newStepId, newMessageId] = this.createMessageStep(type);\n this.dispatchRunStep(newStepId, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id: newMessageId,\n },\n });\n }\n };\n handle(chunk?: t.CustomChunk): void {\n if (!chunk) {\n return;\n }\n\n const content = chunk.choices?.[0]?.delta.content ?? '';\n const reasoning_content = chunk.choices?.[0]?.delta[this.reasoningKey] ?? '';\n\n if (!content.length && !reasoning_content.length) {\n return;\n }\n\n if (content.includes('```')) {\n this.inCodeBlock = !this.inCodeBlock;\n }\n\n if (content === '<think>' && !this.inCodeBlock) {\n this.inThinkBlock = true;\n } else if (this.lastToken === '</think>' && !this.inCodeBlock) {\n this.inThinkBlock = false;\n }\n\n this.lastToken = content;\n\n const message_id = this.getMessageId() ?? '';\n\n if (!message_id) {\n const initialContentType = this.inThinkBlock ? ContentTypes.THINK : ContentTypes.TEXT;\n const initialType = reasoning_content ? ContentTypes.THINK : initialContentType;\n const [stepId, message_id] = this.createMessageStep(initialType);\n this.dispatchRunStep(stepId, {\n type: StepTypes.MESSAGE_CREATION,\n message_creation: {\n message_id,\n },\n });\n }\n\n if (reasoning_content) {\n this.handleContent(reasoning_content, ContentTypes.THINK);\n } else {\n this.handleContent(content, ContentTypes.TEXT);\n }\n }\n}"],"names":[],"mappings":";;;AAIa,MAAA,UAAU,GAAG,CAAC,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,GAAG,EAAE,IAAI,EAAE,KAAK,EAAE;MAExE,kBAAkB,CAAA;IACrB,WAAW,GAAG,KAAK,CAAC;IACpB,YAAY,GAAG,KAAK,CAAC;AACrB,IAAA,UAAU,CAAU;IAC5B,MAAM,GAAa,EAAE,CAAC;IACtB,SAAS,GAAG,EAAE,CAAC;IACf,eAAe,GAAa,EAAE,CAAC;AAC/B,IAAA,aAAa,CAAU;AACvB,IAAA,gBAAgB,CAAU;AAC1B,IAAA,WAAW,CAA0C;IACrD,aAAa,GAAG,CAAC,CAAC;IAClB,YAAY,GAAsC,mBAAmB,CAAC;IACtE,YAAY,GAAG,CAAC,CAAC,CAAC;IAClB,cAAc,GAAG,IA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AS,GAAG,OAAO,CAAC;QAEzB,MAAM,UAAU,GAAG,IAAI,CAAC,YAAY,EAAE,IAAI,EAAE,CAAC;QAE7C,IAAI,CAAC,UAAU,EAAE;AACf,YAAA,MAAM,kBAAkB,GAAG,IAAI,CAAC,YAAY,GAAG,YAAY,CAAC,KAAK,GAAG,YAAY,CAAC,IAAI,CAAC;AACtF,YAAA,MAAM,WAAW,GAAG,iBAAiB,GAAG,YAAY,CAAC,KAAK,GAAG,kBAAkB,CAAC;AAChF,YAAA,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,GAAG,IAAI,CAAC,iBAAiB,CAAC,WAAW,CAAC,CAAC;AACjE,YAAA,IAAI,CAAC,eAAe,CAAC,MAAM,EAAE;gBAC3B,IAAI,EAAE,SAAS,CAAC,gBAAgB;AAChC,gBAAA,gBAAgB,EAAE;oBAChB,UAAU;AACX,iBAAA;AACF,aAAA,CAAC,CAAC;SACJ;QAED,IAAI,iBAAiB,EAAE;YACrB,IAAI,CAAC,aAAa,CAAC,iBAAiB,EAAE,YAAY,CAAC,KAAK,CAAC,CAAC;SAC3D;aAAM;YACL,IAAI,CAAC,aAAa,CAAC,OAAO,EAAE,YAAY,CAAC,IAAI,CAAC,CAAC;SAChD;KACF;AACF;;;;"}
@@ -30,6 +30,6 @@ export declare class SplitStreamHandler {
30
30
  dispatchRunStep: (stepId: string, stepDetails: t.StepDetails) => void;
31
31
  dispatchMessageDelta: (stepId: string, delta: t.MessageDelta) => void;
32
32
  dispatchReasoningDelta: (stepId: string, delta: t.ReasoningDelta) => void;
33
- handleContent: (content: string, stepId: string, _type: ContentTypes.TEXT | ContentTypes.THINK) => void;
33
+ handleContent: (content: string, _type: ContentTypes.TEXT | ContentTypes.THINK) => void;
34
34
  handle(chunk?: t.CustomChunk): void;
35
35
  }
@@ -173,7 +173,11 @@ export type ReasoningDeltaUpdate = {
173
173
  think: string;
174
174
  };
175
175
  export type ContentType = 'text' | 'image_url' | 'tool_call' | 'think' | string;
176
- export type MessageContentComplex = (MessageContentText | MessageContentImageUrl | (Record<string, any> & {
176
+ export type ReasoningContentText = {
177
+ type: ContentTypes.THINK;
178
+ think: string;
179
+ };
180
+ export type MessageContentComplex = (ReasoningContentText | MessageContentText | MessageContentImageUrl | (Record<string, any> & {
177
181
  type?: 'text' | 'image_url' | 'think' | string;
178
182
  }) | (Record<string, any> & {
179
183
  type?: never;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@librechat/agents",
3
- "version": "1.9.96",
3
+ "version": "1.9.97",
4
4
  "main": "./dist/cjs/main.cjs",
5
5
  "module": "./dist/esm/main.mjs",
6
6
  "types": "./dist/types/index.d.ts",
@@ -536,4 +536,95 @@ describe('SplitStreamHandler', () => {
536
536
  // Verify no reasoning events were generated
537
537
  expect(reasoningDeltaEvents.length).toBe(0);
538
538
  });
539
+
540
+ it('should properly split content with think tags while maintaining context', async () => {
541
+ const runId = nanoid();
542
+ const messageDeltaEvents: t.MessageDeltaEvent[] = [];
543
+ const reasoningDeltaEvents: t.ReasoningDeltaEvent[] = [];
544
+ const runStepEvents: t.RunStep[] = [];
545
+ const { contentParts, aggregateContent } = createContentAggregator();
546
+
547
+ const streamHandler = new SplitStreamHandler({
548
+ runId,
549
+ blockThreshold: 20, // Small threshold to force splits
550
+ handlers: {
551
+ [GraphEvents.ON_MESSAGE_DELTA]: (event): void => {
552
+ messageDeltaEvents.push(event.data);
553
+ aggregateContent(event);
554
+ },
555
+ [GraphEvents.ON_REASONING_DELTA]: (event): void => {
556
+ reasoningDeltaEvents.push(event.data);
557
+ aggregateContent(event);
558
+ },
559
+ [GraphEvents.ON_RUN_STEP]: (event): void => {
560
+ runStepEvents.push(event.data);
561
+ aggregateContent(event);
562
+ },
563
+ },
564
+ });
565
+
566
+ const content = 'Here\'s some regular text. <think>Now I\'m thinking deeply about something important. This is a long thought that should be split into multiple parts. We want to ensure the splitting works correctly.</think> Back to regular text after thinking.';
567
+
568
+ const stream = createMockStream({
569
+ text: content,
570
+ streamRate: 5,
571
+ })();
572
+
573
+ for await (const chunk of stream) {
574
+ streamHandler.handle(chunk);
575
+ }
576
+
577
+ // Verify that multiple message blocks were created
578
+ expect(runStepEvents.length).toBeGreaterThan(2);
579
+
580
+ // Check that content before <think> was handled as regular text
581
+ expect(messageDeltaEvents.some(event =>
582
+ (event.delta.content?.[0] as t.MessageDeltaUpdate | undefined)?.text.includes('regular')
583
+ )).toBe(true);
584
+
585
+ // Verify that reasoning content was split into multiple parts
586
+ const reasoningParts = reasoningDeltaEvents
587
+ .map(event => (event.delta.content?.[0] as t.ReasoningDeltaUpdate | undefined)?.think)
588
+ .filter(Boolean);
589
+ expect(reasoningParts.length).toBeGreaterThan(1);
590
+
591
+ // Verify that the complete reasoning content is preserved when joined
592
+ const fullReasoningContent = reasoningParts.join('');
593
+ expect(fullReasoningContent).toContain('thinking');
594
+ expect(fullReasoningContent).toContain('should');
595
+ expect(fullReasoningContent).toContain('be');
596
+ expect(fullReasoningContent).toContain('split');
597
+
598
+ // Check that each reasoning part maintains proper think context
599
+ let seenThinkOpen = false;
600
+ let seenThinkClose = false;
601
+ reasoningParts.forEach(part => {
602
+ if (part == null) return;
603
+ if (part.includes('<think>')) {
604
+ seenThinkOpen = true;
605
+ }
606
+ if (part.includes('</think>')) {
607
+ seenThinkClose = true;
608
+ }
609
+ // Middle parts should be handled as reasoning even without explicit think tags
610
+ if (!part.includes('<think>') && !part.includes('</think>')) {
611
+ expect(reasoningDeltaEvents.some(event =>
612
+ (event.delta.content?.[0] as t.ReasoningDeltaUpdate | undefined)?.think === part
613
+ )).toBe(true);
614
+ }
615
+ });
616
+ expect(seenThinkOpen).toBe(true);
617
+ expect(seenThinkClose).toBe(true);
618
+
619
+ // Check that content after </think> was handled as regular text
620
+ expect(messageDeltaEvents.some(event =>
621
+ (event.delta.content?.[0] as t.MessageDeltaUpdate | undefined)?.text.includes('Back')
622
+ )).toBe(true);
623
+
624
+ const thinkingBlocks = contentParts.filter(part =>
625
+ part?.type === ContentTypes.THINK
626
+ );
627
+ expect(thinkingBlocks.length).toEqual(4);
628
+ expect((thinkingBlocks[0] as t.ReasoningContentText).think.startsWith('<think>')).toBeTruthy();
629
+ });
539
630
  });
@@ -86,7 +86,7 @@ export class SplitStreamHandler {
86
86
  };
87
87
  this.handlers?.[GraphEvents.ON_REASONING_DELTA]?.({ event: GraphEvents.ON_REASONING_DELTA, data: reasoningDelta });
88
88
  };
89
- handleContent = (content: string, stepId: string, _type: ContentTypes.TEXT | ContentTypes.THINK): void => {
89
+ handleContent = (content: string, _type: ContentTypes.TEXT | ContentTypes.THINK): void => {
90
90
  let type = _type;
91
91
  if (this.inThinkBlock && type === ContentTypes.TEXT) {
92
92
  type = ContentTypes.THINK;
@@ -109,12 +109,7 @@ export class SplitStreamHandler {
109
109
  });
110
110
  }
111
111
 
112
- if (content.includes('```')) {
113
- this.inCodeBlock = !this.inCodeBlock;
114
- }
115
-
116
- this.currentLength += content.length;
117
-
112
+ const stepId = this.currentStepId ?? '';
118
113
  if (type === ContentTypes.THINK) {
119
114
  this.dispatchReasoningDelta(stepId, {
120
115
  content: [{
@@ -131,6 +126,7 @@ export class SplitStreamHandler {
131
126
  });
132
127
  }
133
128
 
129
+ this.currentLength += content.length;
134
130
  if (this.inCodeBlock) {
135
131
  return;
136
132
  }
@@ -157,6 +153,10 @@ export class SplitStreamHandler {
157
153
  return;
158
154
  }
159
155
 
156
+ if (content.includes('```')) {
157
+ this.inCodeBlock = !this.inCodeBlock;
158
+ }
159
+
160
160
  if (content === '<think>' && !this.inCodeBlock) {
161
161
  this.inThinkBlock = true;
162
162
  } else if (this.lastToken === '</think>' && !this.inCodeBlock) {
@@ -179,15 +179,10 @@ export class SplitStreamHandler {
179
179
  });
180
180
  }
181
181
 
182
- const stepId = this.currentStepId ?? '';
183
- if (!stepId) {
184
- return;
185
- }
186
-
187
182
  if (reasoning_content) {
188
- this.handleContent(reasoning_content, stepId, ContentTypes.THINK);
183
+ this.handleContent(reasoning_content, ContentTypes.THINK);
189
184
  } else {
190
- this.handleContent(content, stepId, ContentTypes.TEXT);
185
+ this.handleContent(content, ContentTypes.TEXT);
191
186
  }
192
187
  }
193
188
  }
@@ -210,8 +210,13 @@ export type ReasoningDeltaUpdate = { type: ContentTypes.THINK; think: string; };
210
210
 
211
211
  export type ContentType = 'text' | 'image_url' | 'tool_call' | 'think' | string;
212
212
 
213
+ export type ReasoningContentText = {
214
+ type: ContentTypes.THINK;
215
+ think: string;
216
+ };
217
+
213
218
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
214
- export type MessageContentComplex = (MessageContentText | MessageContentImageUrl | (Record<string, any> & {
219
+ export type MessageContentComplex = (ReasoningContentText | MessageContentText | MessageContentImageUrl | (Record<string, any> & {
215
220
  type?: 'text' | 'image_url' | 'think' | string;
216
221
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
217
222
  }) | (Record<string, any> & {