| 265 | }; |
| 266 | |
| 267 | function createAssistantStreamer(projectId: string, requestId: string | undefined, options: AssistantStreamerOptions = {}) { |
| 268 | let buffer = ''; |
| 269 | let assistantMessageId: string | null = options.initialMessageId ?? null; |
| 270 | let lastStreamedPayload: string | null = null; |
| 271 | let lastChunk: string | null = null; |
| 272 | |
| 273 | const normalizeChunk = (value: string) => value.replace(/\r/g, ''); |
| 274 | |
| 275 | const isNoiseChunk = (value: string) => { |
| 276 | const normalized = normalizeChunk(value); |
| 277 | const trimmed = normalized.trim(); |
| 278 | if (!trimmed) { |
| 279 | return true; |
| 280 | } |
| 281 | |
| 282 | // Ignore spinner characters like / - \ | |
| 283 | if (trimmed.length <= 3 && /^[\-/\\|]+$/.test(trimmed)) { |
| 284 | return true; |
| 285 | } |
| 286 | |
| 287 | // Ignore progress dots or ellipsis |
| 288 | if (/^[.··…]+$/.test(trimmed)) { |
| 289 | return true; |
| 290 | } |
| 291 | |
| 292 | return false; |
| 293 | }; |
| 294 | |
| 295 | const buildPayload = () => buffer.trim(); |
| 296 | |
| 297 | const streamDraft = () => { |
| 298 | const content = buildPayload(); |
| 299 | if (!content || content === lastStreamedPayload) { |
| 300 | return; |
| 301 | } |
| 302 | const id = assistantMessageId ?? (assistantMessageId = randomUUID()); |
| 303 | const realtime = createRealtimeMessage({ |
| 304 | id, |
| 305 | projectId, |
| 306 | role: 'assistant', |
| 307 | messageType: 'chat', |
| 308 | content, |
| 309 | metadata: { cli_type: 'cursor' }, |
| 310 | requestId, |
| 311 | isStreaming: true, |
| 312 | isFinal: false, |
| 313 | }); |
| 314 | streamManager.publish(projectId, { type: 'message', data: realtime }); |
| 315 | lastStreamedPayload = content; |
| 316 | }; |
| 317 | |
| 318 | const append = (text: string) => { |
| 319 | if (!text) return; |
| 320 | if (isNoiseChunk(text)) { |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | const normalized = normalizeChunk(text); |