UNPKG

@xtsai/x-supports

Version:

The biz-support is an library for both frontend & backend

65 lines 2.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.XaiChunkParser = void 0; const RefRegex = /<ref>\[([\s\S]*?)\]<\/ref>/g; class XaiChunkParser { onExtracted; buffer = ''; fulltext = ''; constent = ''; pickMap = new Map(); constructor(onExtracted) { this.onExtracted = onExtracted; } /** * 您好!关于最新的优惠活动信息,建议您通过方APP或小程序进行查询,或拨打客服热线0991-6363111了解更多详情<ref>[角色]</ref>。此外,您也可以访问我们的官方网站以获取最新的促销信息<ref>[角色]</ref>。如有任何疑问或需要进一步的帮助,我们的客服团队将随时为您提供支持<ref>[角色]</ref>。 * @param chunk * @returns */ append(chunk) { this.fulltext += chunk; this.buffer += chunk; const regex = /<ref>\[([\s\S]*?)\]<\/ref>/g; const matches = []; let match; while ((match = regex.exec(this.buffer)) !== null) { matches.push(match[1]); } if (matches.length > 0) { this.buffer = this.buffer.slice(regex.lastIndex); this.setMap('ref', matches); this.onExtracted(this.cloneData()); } return this; } flush() { this.buffer = ''; this.fulltext = ''; this.pickMap = new Map(); } replaceAll() { return this.fulltext.replaceAll(RefRegex, ''); } getResult() { return this.buffer; } setMap(key, matches) { const innerMap = this.pickMap.has(key) ? this.pickMap.get(key) : new Map(); matches.forEach((s) => { innerMap?.set(s, s); }); this.pickMap.set(key, innerMap); } cloneData() { const data = { result: this.replaceAll(), buffer: this.buffer, picks: new Map(JSON.parse(JSON.stringify([...this.pickMap]))), }; return data; } } exports.XaiChunkParser = XaiChunkParser; //# sourceMappingURL=chunk.parser.js.map