MCPcopy
hub / github.com/7836246/cursor2api / createIncrementalTextStreamer

Function createIncrementalTextStreamer

src/streaming-text.ts:134–218  ·  view source on GitHub ↗
(
    options: IncrementalTextStreamerOptions = {},
)

Source from the content-addressed store, hash-verified

132 * - 最终 finish() 时再把剩余文本一次性补齐
133 */
134export function createIncrementalTextStreamer(
135 options: IncrementalTextStreamerOptions = {},
136): IncrementalTextStreamer {
137 const warmupChars = options.warmupChars ?? DEFAULT_WARMUP_CHARS;
138 const guardChars = options.guardChars ?? DEFAULT_GUARD_CHARS;
139 const transform = options.transform ?? ((text: string) => text);
140 const isBlockedPrefix = options.isBlockedPrefix ?? (() => false);
141
142 let rawText = '';
143 let sentText = '';
144 let unlocked = false;
145 let sentAny = false;
146
147 const tryUnlock = (): boolean => {
148 if (unlocked) return true;
149
150 const preview = transform(rawText);
151 if (!preview.trim()) return false;
152
153 const hasBoundary = STREAM_START_BOUNDARY_RE.test(preview);
154 const enoughChars = preview.length >= warmupChars;
155 if (!hasBoundary && !enoughChars) {
156 return false;
157 }
158
159 if (isBlockedPrefix(preview.trim())) {
160 return false;
161 }
162
163 // ★ HTML 内容有效性检查:防止 <br>、</s>、&nbsp; 等纯 HTML token 连续重复时提前 unlock
164 // 超过 guardChars(256)后强制放行,此时 cursor-client 的 htmlRepeatAborted 早已触发重试
165 if (preview.length < guardChars) {
166 const noSpace = preview.replace(/\s/g, '');
167 const stripped = noSpace.replace(HTML_TOKEN_STRIP_RE, '');
168 const ratio = noSpace.length === 0 ? 0 : stripped.length / noSpace.length;
169 if (ratio < HTML_VALID_RATIO_MIN) {
170 return false;
171 }
172 }
173
174 unlocked = true;
175 return true;
176 };
177
178 const emitFromRawLength = (rawLength: number): string => {
179 const transformed = transform(rawText.slice(0, rawLength));
180 if (transformed.length <= sentText.length) return '';
181
182 const delta = transformed.slice(sentText.length);
183 sentText = transformed;
184 if (delta) sentAny = true;
185 return delta;
186 };
187
188 return {
189 push(chunk: string): string {
190 if (!chunk) return '';
191

Callers 6

executeAttemptFunction · 0.85
handleOpenAIStreamFunction · 0.85
handleDirectTextStreamFunction · 0.85
executeAttemptFunction · 0.85
handleStreamFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected