import type { SerialTaskExecuteContext } from '../handler' import type { PrivateCustomEventName } from '../types' import TextStreamSlicer from '@minto-ai/text-stream-slicer' import { SerialHandler } from '../handler' import { createEventBus } from '../utils' const $bus = createEventBus() class TextSplit extends SerialHandler { private textStreamSlicer = new TextStreamSlicer() public execute( context: SerialTaskExecuteContext, ): void { if (context.isLastExecute) { this.textStreamSlicer.processText('', true).forEach((paragraph) => { this.forwardToHandler(paragraph) }) } else { this.textStreamSlicer.processText(context.taskItem.original!).forEach((paragraph) => { this.forwardToHandler(paragraph) }) } this.taskCompletedCallback() } protected onFinish(): void { $bus.emit('_textSplitFinish') } } export default TextSplit