@librechat/agents 1.9.95 → 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.
- package/dist/cjs/main.cjs +3 -0
- package/dist/cjs/main.cjs.map +1 -1
- package/dist/cjs/splitStream.cjs +172 -0
- package/dist/cjs/splitStream.cjs.map +1 -0
- package/dist/esm/main.mjs +1 -0
- package/dist/esm/main.mjs.map +1 -1
- package/dist/esm/splitStream.mjs +169 -0
- package/dist/esm/splitStream.mjs.map +1 -0
- package/dist/types/index.d.ts +1 -0
- package/dist/types/splitStream.d.ts +1 -1
- package/dist/types/types/stream.d.ts +5 -1
- package/package.json +1 -1
- package/src/index.ts +1 -0
- package/src/splitStream.test.ts +91 -0
- package/src/splitStream.ts +9 -14
- package/src/types/stream.ts +6 -1
package/dist/cjs/main.cjs
CHANGED
|
@@ -2,6 +2,7 @@
|
|
|
2
2
|
|
|
3
3
|
var run = require('./run.cjs');
|
|
4
4
|
var stream = require('./stream.cjs');
|
|
5
|
+
var splitStream = require('./splitStream.cjs');
|
|
5
6
|
var events = require('./events.cjs');
|
|
6
7
|
var messages = require('./messages.cjs');
|
|
7
8
|
var Graph = require('./graphs/Graph.cjs');
|
|
@@ -17,6 +18,8 @@ exports.Run = run.Run;
|
|
|
17
18
|
exports.ChatModelStreamHandler = stream.ChatModelStreamHandler;
|
|
18
19
|
exports.createContentAggregator = stream.createContentAggregator;
|
|
19
20
|
exports.handleToolCalls = stream.handleToolCalls;
|
|
21
|
+
exports.SEPARATORS = splitStream.SEPARATORS;
|
|
22
|
+
exports.SplitStreamHandler = splitStream.SplitStreamHandler;
|
|
20
23
|
exports.HandlerRegistry = events.HandlerRegistry;
|
|
21
24
|
exports.LLMStreamHandler = events.LLMStreamHandler;
|
|
22
25
|
exports.ModelEndHandler = events.ModelEndHandler;
|
package/dist/cjs/main.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"main.cjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;"}
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var nanoid = require('nanoid');
|
|
4
|
+
var _enum = require('./common/enum.cjs');
|
|
5
|
+
|
|
6
|
+
const SEPARATORS = ['.', '?', '!', '۔', '。', '‥', ';', '¡', '¿', '\n', '```'];
|
|
7
|
+
class SplitStreamHandler {
|
|
8
|
+
inCodeBlock = false;
|
|
9
|
+
inThinkBlock = false;
|
|
10
|
+
accumulate;
|
|
11
|
+
tokens = [];
|
|
12
|
+
lastToken = '';
|
|
13
|
+
reasoningTokens = [];
|
|
14
|
+
currentStepId;
|
|
15
|
+
currentMessageId;
|
|
16
|
+
currentType;
|
|
17
|
+
currentLength = 0;
|
|
18
|
+
reasoningKey = 'reasoning_content';
|
|
19
|
+
currentIndex = -1;
|
|
20
|
+
blockThreshold = 4500;
|
|
21
|
+
/** The run ID AKA the Message ID associated with the complete generation */
|
|
22
|
+
runId;
|
|
23
|
+
handlers;
|
|
24
|
+
constructor({ runId, handlers, accumulate, reasoningKey, blockThreshold, }) {
|
|
25
|
+
this.runId = runId;
|
|
26
|
+
this.handlers = handlers;
|
|
27
|
+
if (reasoningKey) {
|
|
28
|
+
this.reasoningKey = reasoningKey;
|
|
29
|
+
}
|
|
30
|
+
if (blockThreshold != null) {
|
|
31
|
+
this.blockThreshold = blockThreshold;
|
|
32
|
+
}
|
|
33
|
+
this.accumulate = accumulate ?? false;
|
|
34
|
+
}
|
|
35
|
+
getMessageId = () => {
|
|
36
|
+
const messageId = this.currentMessageId;
|
|
37
|
+
if (messageId != null && messageId) {
|
|
38
|
+
return messageId;
|
|
39
|
+
}
|
|
40
|
+
return undefined;
|
|
41
|
+
};
|
|
42
|
+
createMessageStep = (type) => {
|
|
43
|
+
if (type != null && this.currentType !== type) {
|
|
44
|
+
this.currentType = type;
|
|
45
|
+
}
|
|
46
|
+
this.currentLength = 0;
|
|
47
|
+
this.currentIndex += 1;
|
|
48
|
+
this.currentStepId = `step_${nanoid.nanoid()}`;
|
|
49
|
+
this.currentMessageId = `msg_${nanoid.nanoid()}`;
|
|
50
|
+
return [this.currentStepId, this.currentMessageId];
|
|
51
|
+
};
|
|
52
|
+
dispatchRunStep = (stepId, stepDetails) => {
|
|
53
|
+
const runStep = {
|
|
54
|
+
id: stepId,
|
|
55
|
+
runId: this.runId,
|
|
56
|
+
type: stepDetails.type,
|
|
57
|
+
index: this.currentIndex,
|
|
58
|
+
stepDetails,
|
|
59
|
+
// usage: null,
|
|
60
|
+
};
|
|
61
|
+
this.handlers?.[_enum.GraphEvents.ON_RUN_STEP]?.({ event: _enum.GraphEvents.ON_RUN_STEP, data: runStep });
|
|
62
|
+
};
|
|
63
|
+
dispatchMessageDelta = (stepId, delta) => {
|
|
64
|
+
const messageDelta = {
|
|
65
|
+
id: stepId,
|
|
66
|
+
delta,
|
|
67
|
+
};
|
|
68
|
+
this.handlers?.[_enum.GraphEvents.ON_MESSAGE_DELTA]?.({ event: _enum.GraphEvents.ON_MESSAGE_DELTA, data: messageDelta });
|
|
69
|
+
};
|
|
70
|
+
dispatchReasoningDelta = (stepId, delta) => {
|
|
71
|
+
const reasoningDelta = {
|
|
72
|
+
id: stepId,
|
|
73
|
+
delta,
|
|
74
|
+
};
|
|
75
|
+
this.handlers?.[_enum.GraphEvents.ON_REASONING_DELTA]?.({ event: _enum.GraphEvents.ON_REASONING_DELTA, data: reasoningDelta });
|
|
76
|
+
};
|
|
77
|
+
handleContent = (content, _type) => {
|
|
78
|
+
let type = _type;
|
|
79
|
+
if (this.inThinkBlock && type === _enum.ContentTypes.TEXT) {
|
|
80
|
+
type = _enum.ContentTypes.THINK;
|
|
81
|
+
}
|
|
82
|
+
if (this.accumulate) {
|
|
83
|
+
if (type === _enum.ContentTypes.THINK) {
|
|
84
|
+
this.reasoningTokens.push(content);
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
this.tokens.push(content);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
if (this.currentType !== type) {
|
|
91
|
+
const [newStepId, newMessageId] = this.createMessageStep(type);
|
|
92
|
+
this.dispatchRunStep(newStepId, {
|
|
93
|
+
type: _enum.StepTypes.MESSAGE_CREATION,
|
|
94
|
+
message_creation: {
|
|
95
|
+
message_id: newMessageId,
|
|
96
|
+
},
|
|
97
|
+
});
|
|
98
|
+
}
|
|
99
|
+
const stepId = this.currentStepId ?? '';
|
|
100
|
+
if (type === _enum.ContentTypes.THINK) {
|
|
101
|
+
this.dispatchReasoningDelta(stepId, {
|
|
102
|
+
content: [{
|
|
103
|
+
type: _enum.ContentTypes.THINK,
|
|
104
|
+
think: content,
|
|
105
|
+
}],
|
|
106
|
+
});
|
|
107
|
+
}
|
|
108
|
+
else {
|
|
109
|
+
this.dispatchMessageDelta(stepId, {
|
|
110
|
+
content: [{
|
|
111
|
+
type: _enum.ContentTypes.TEXT,
|
|
112
|
+
text: content,
|
|
113
|
+
}],
|
|
114
|
+
});
|
|
115
|
+
}
|
|
116
|
+
this.currentLength += content.length;
|
|
117
|
+
if (this.inCodeBlock) {
|
|
118
|
+
return;
|
|
119
|
+
}
|
|
120
|
+
if (this.currentLength > this.blockThreshold && SEPARATORS.some(sep => content.includes(sep))) {
|
|
121
|
+
const [newStepId, newMessageId] = this.createMessageStep(type);
|
|
122
|
+
this.dispatchRunStep(newStepId, {
|
|
123
|
+
type: _enum.StepTypes.MESSAGE_CREATION,
|
|
124
|
+
message_creation: {
|
|
125
|
+
message_id: newMessageId,
|
|
126
|
+
},
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
};
|
|
130
|
+
handle(chunk) {
|
|
131
|
+
if (!chunk) {
|
|
132
|
+
return;
|
|
133
|
+
}
|
|
134
|
+
const content = chunk.choices?.[0]?.delta.content ?? '';
|
|
135
|
+
const reasoning_content = chunk.choices?.[0]?.delta[this.reasoningKey] ?? '';
|
|
136
|
+
if (!content.length && !reasoning_content.length) {
|
|
137
|
+
return;
|
|
138
|
+
}
|
|
139
|
+
if (content.includes('```')) {
|
|
140
|
+
this.inCodeBlock = !this.inCodeBlock;
|
|
141
|
+
}
|
|
142
|
+
if (content === '<think>' && !this.inCodeBlock) {
|
|
143
|
+
this.inThinkBlock = true;
|
|
144
|
+
}
|
|
145
|
+
else if (this.lastToken === '</think>' && !this.inCodeBlock) {
|
|
146
|
+
this.inThinkBlock = false;
|
|
147
|
+
}
|
|
148
|
+
this.lastToken = content;
|
|
149
|
+
const message_id = this.getMessageId() ?? '';
|
|
150
|
+
if (!message_id) {
|
|
151
|
+
const initialContentType = this.inThinkBlock ? _enum.ContentTypes.THINK : _enum.ContentTypes.TEXT;
|
|
152
|
+
const initialType = reasoning_content ? _enum.ContentTypes.THINK : initialContentType;
|
|
153
|
+
const [stepId, message_id] = this.createMessageStep(initialType);
|
|
154
|
+
this.dispatchRunStep(stepId, {
|
|
155
|
+
type: _enum.StepTypes.MESSAGE_CREATION,
|
|
156
|
+
message_creation: {
|
|
157
|
+
message_id,
|
|
158
|
+
},
|
|
159
|
+
});
|
|
160
|
+
}
|
|
161
|
+
if (reasoning_content) {
|
|
162
|
+
this.handleContent(reasoning_content, _enum.ContentTypes.THINK);
|
|
163
|
+
}
|
|
164
|
+
else {
|
|
165
|
+
this.handleContent(content, _enum.ContentTypes.TEXT);
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
exports.SEPARATORS = SEPARATORS;
|
|
171
|
+
exports.SplitStreamHandler = SplitStreamHandler;
|
|
172
|
+
//# sourceMappingURL=splitStream.cjs.map
|
|
@@ -0,0 +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, _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,WAA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U,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;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;;;;;"}
|
package/dist/esm/main.mjs
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
export { Run } from './run.mjs';
|
|
2
2
|
export { ChatModelStreamHandler, createContentAggregator, handleToolCalls } from './stream.mjs';
|
|
3
|
+
export { SEPARATORS, SplitStreamHandler } from './splitStream.mjs';
|
|
3
4
|
export { HandlerRegistry, LLMStreamHandler, ModelEndHandler, TestChatStreamHandler, TestLLMStreamHandler, ToolEndHandler, createMetadataAggregator } from './events.mjs';
|
|
4
5
|
export { convertMessagesToContent, findLastIndex, formatAnthropicArtifactContent, formatAnthropicMessage, formatOpenAIArtifactContent, getConverseOverrideMessage, modifyDeltaProperties } from './messages.mjs';
|
|
5
6
|
export { Graph, StandardGraph } from './graphs/Graph.mjs';
|
package/dist/esm/main.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"main.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"main.mjs","sources":[],"sourcesContent":[],"names":[],"mappings":";;;;;;;;;;"}
|
|
@@ -0,0 +1,169 @@
|
|
|
1
|
+
import { nanoid } from 'nanoid';
|
|
2
|
+
import { GraphEvents, ContentTypes, StepTypes } from './common/enum.mjs';
|
|
3
|
+
|
|
4
|
+
const SEPARATORS = ['.', '?', '!', '۔', '。', '‥', ';', '¡', '¿', '\n', '```'];
|
|
5
|
+
class SplitStreamHandler {
|
|
6
|
+
inCodeBlock = false;
|
|
7
|
+
inThinkBlock = false;
|
|
8
|
+
accumulate;
|
|
9
|
+
tokens = [];
|
|
10
|
+
lastToken = '';
|
|
11
|
+
reasoningTokens = [];
|
|
12
|
+
currentStepId;
|
|
13
|
+
currentMessageId;
|
|
14
|
+
currentType;
|
|
15
|
+
currentLength = 0;
|
|
16
|
+
reasoningKey = 'reasoning_content';
|
|
17
|
+
currentIndex = -1;
|
|
18
|
+
blockThreshold = 4500;
|
|
19
|
+
/** The run ID AKA the Message ID associated with the complete generation */
|
|
20
|
+
runId;
|
|
21
|
+
handlers;
|
|
22
|
+
constructor({ runId, handlers, accumulate, reasoningKey, blockThreshold, }) {
|
|
23
|
+
this.runId = runId;
|
|
24
|
+
this.handlers = handlers;
|
|
25
|
+
if (reasoningKey) {
|
|
26
|
+
this.reasoningKey = reasoningKey;
|
|
27
|
+
}
|
|
28
|
+
if (blockThreshold != null) {
|
|
29
|
+
this.blockThreshold = blockThreshold;
|
|
30
|
+
}
|
|
31
|
+
this.accumulate = accumulate ?? false;
|
|
32
|
+
}
|
|
33
|
+
getMessageId = () => {
|
|
34
|
+
const messageId = this.currentMessageId;
|
|
35
|
+
if (messageId != null && messageId) {
|
|
36
|
+
return messageId;
|
|
37
|
+
}
|
|
38
|
+
return undefined;
|
|
39
|
+
};
|
|
40
|
+
createMessageStep = (type) => {
|
|
41
|
+
if (type != null && this.currentType !== type) {
|
|
42
|
+
this.currentType = type;
|
|
43
|
+
}
|
|
44
|
+
this.currentLength = 0;
|
|
45
|
+
this.currentIndex += 1;
|
|
46
|
+
this.currentStepId = `step_${nanoid()}`;
|
|
47
|
+
this.currentMessageId = `msg_${nanoid()}`;
|
|
48
|
+
return [this.currentStepId, this.currentMessageId];
|
|
49
|
+
};
|
|
50
|
+
dispatchRunStep = (stepId, stepDetails) => {
|
|
51
|
+
const runStep = {
|
|
52
|
+
id: stepId,
|
|
53
|
+
runId: this.runId,
|
|
54
|
+
type: stepDetails.type,
|
|
55
|
+
index: this.currentIndex,
|
|
56
|
+
stepDetails,
|
|
57
|
+
// usage: null,
|
|
58
|
+
};
|
|
59
|
+
this.handlers?.[GraphEvents.ON_RUN_STEP]?.({ event: GraphEvents.ON_RUN_STEP, data: runStep });
|
|
60
|
+
};
|
|
61
|
+
dispatchMessageDelta = (stepId, delta) => {
|
|
62
|
+
const messageDelta = {
|
|
63
|
+
id: stepId,
|
|
64
|
+
delta,
|
|
65
|
+
};
|
|
66
|
+
this.handlers?.[GraphEvents.ON_MESSAGE_DELTA]?.({ event: GraphEvents.ON_MESSAGE_DELTA, data: messageDelta });
|
|
67
|
+
};
|
|
68
|
+
dispatchReasoningDelta = (stepId, delta) => {
|
|
69
|
+
const reasoningDelta = {
|
|
70
|
+
id: stepId,
|
|
71
|
+
delta,
|
|
72
|
+
};
|
|
73
|
+
this.handlers?.[GraphEvents.ON_REASONING_DELTA]?.({ event: GraphEvents.ON_REASONING_DELTA, data: reasoningDelta });
|
|
74
|
+
};
|
|
75
|
+
handleContent = (content, _type) => {
|
|
76
|
+
let type = _type;
|
|
77
|
+
if (this.inThinkBlock && type === ContentTypes.TEXT) {
|
|
78
|
+
type = ContentTypes.THINK;
|
|
79
|
+
}
|
|
80
|
+
if (this.accumulate) {
|
|
81
|
+
if (type === ContentTypes.THINK) {
|
|
82
|
+
this.reasoningTokens.push(content);
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
this.tokens.push(content);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (this.currentType !== type) {
|
|
89
|
+
const [newStepId, newMessageId] = this.createMessageStep(type);
|
|
90
|
+
this.dispatchRunStep(newStepId, {
|
|
91
|
+
type: StepTypes.MESSAGE_CREATION,
|
|
92
|
+
message_creation: {
|
|
93
|
+
message_id: newMessageId,
|
|
94
|
+
},
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
const stepId = this.currentStepId ?? '';
|
|
98
|
+
if (type === ContentTypes.THINK) {
|
|
99
|
+
this.dispatchReasoningDelta(stepId, {
|
|
100
|
+
content: [{
|
|
101
|
+
type: ContentTypes.THINK,
|
|
102
|
+
think: content,
|
|
103
|
+
}],
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
else {
|
|
107
|
+
this.dispatchMessageDelta(stepId, {
|
|
108
|
+
content: [{
|
|
109
|
+
type: ContentTypes.TEXT,
|
|
110
|
+
text: content,
|
|
111
|
+
}],
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
this.currentLength += content.length;
|
|
115
|
+
if (this.inCodeBlock) {
|
|
116
|
+
return;
|
|
117
|
+
}
|
|
118
|
+
if (this.currentLength > this.blockThreshold && SEPARATORS.some(sep => content.includes(sep))) {
|
|
119
|
+
const [newStepId, newMessageId] = this.createMessageStep(type);
|
|
120
|
+
this.dispatchRunStep(newStepId, {
|
|
121
|
+
type: StepTypes.MESSAGE_CREATION,
|
|
122
|
+
message_creation: {
|
|
123
|
+
message_id: newMessageId,
|
|
124
|
+
},
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
};
|
|
128
|
+
handle(chunk) {
|
|
129
|
+
if (!chunk) {
|
|
130
|
+
return;
|
|
131
|
+
}
|
|
132
|
+
const content = chunk.choices?.[0]?.delta.content ?? '';
|
|
133
|
+
const reasoning_content = chunk.choices?.[0]?.delta[this.reasoningKey] ?? '';
|
|
134
|
+
if (!content.length && !reasoning_content.length) {
|
|
135
|
+
return;
|
|
136
|
+
}
|
|
137
|
+
if (content.includes('```')) {
|
|
138
|
+
this.inCodeBlock = !this.inCodeBlock;
|
|
139
|
+
}
|
|
140
|
+
if (content === '<think>' && !this.inCodeBlock) {
|
|
141
|
+
this.inThinkBlock = true;
|
|
142
|
+
}
|
|
143
|
+
else if (this.lastToken === '</think>' && !this.inCodeBlock) {
|
|
144
|
+
this.inThinkBlock = false;
|
|
145
|
+
}
|
|
146
|
+
this.lastToken = content;
|
|
147
|
+
const message_id = this.getMessageId() ?? '';
|
|
148
|
+
if (!message_id) {
|
|
149
|
+
const initialContentType = this.inThinkBlock ? ContentTypes.THINK : ContentTypes.TEXT;
|
|
150
|
+
const initialType = reasoning_content ? ContentTypes.THINK : initialContentType;
|
|
151
|
+
const [stepId, message_id] = this.createMessageStep(initialType);
|
|
152
|
+
this.dispatchRunStep(stepId, {
|
|
153
|
+
type: StepTypes.MESSAGE_CREATION,
|
|
154
|
+
message_creation: {
|
|
155
|
+
message_id,
|
|
156
|
+
},
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
if (reasoning_content) {
|
|
160
|
+
this.handleContent(reasoning_content, ContentTypes.THINK);
|
|
161
|
+
}
|
|
162
|
+
else {
|
|
163
|
+
this.handleContent(content, ContentTypes.TEXT);
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export { SEPARATORS, SplitStreamHandler };
|
|
169
|
+
//# sourceMappingURL=splitStream.mjs.map
|
|
@@ -0,0 +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, _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,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,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AAC,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;;;;"}
|
package/dist/types/index.d.ts
CHANGED
|
@@ -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,
|
|
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
|
|
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
package/src/index.ts
CHANGED
package/src/splitStream.test.ts
CHANGED
|
@@ -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
|
});
|
package/src/splitStream.ts
CHANGED
|
@@ -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,
|
|
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
|
-
|
|
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,
|
|
183
|
+
this.handleContent(reasoning_content, ContentTypes.THINK);
|
|
189
184
|
} else {
|
|
190
|
-
this.handleContent(content,
|
|
185
|
+
this.handleContent(content, ContentTypes.TEXT);
|
|
191
186
|
}
|
|
192
187
|
}
|
|
193
188
|
}
|
package/src/types/stream.ts
CHANGED
|
@@ -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> & {
|