(
cursorReq: CursorChatRequest,
initialResponse: string,
hasTools: boolean,
)
| 664 | } |
| 665 | |
| 666 | export async function autoContinueCursorToolResponseStream( |
| 667 | cursorReq: CursorChatRequest, |
| 668 | initialResponse: string, |
| 669 | hasTools: boolean, |
| 670 | ): Promise<string> { |
| 671 | let fullResponse = initialResponse; |
| 672 | // OpenAI-compatible clients expect complete tool calls in one logical response. |
| 673 | // Unlike Claude Code, they generally cannot recover a truncated json action block |
| 674 | // by issuing a native follow-up continuation themselves, so we force at least |
| 675 | // one internal continuation attempt here. |
| 676 | const MAX_AUTO_CONTINUE = Math.max(getConfig().maxAutoContinue, 1); |
| 677 | let continueCount = 0; |
| 678 | let consecutiveSmallAdds = 0; |
| 679 | |
| 680 | |
| 681 | while (MAX_AUTO_CONTINUE > 0 && shouldAutoContinueTruncatedToolResponse(fullResponse, hasTools) && continueCount < MAX_AUTO_CONTINUE) { |
| 682 | continueCount++; |
| 683 | |
| 684 | const anchorLength = Math.min(300, fullResponse.length); |
| 685 | const anchorText = fullResponse.slice(-anchorLength); |
| 686 | const continuationPrompt = `Your previous response was cut off mid-output. The last part of your output was: |
| 687 | |
| 688 | \`\`\` |
| 689 | ...${anchorText} |
| 690 | \`\`\` |
| 691 | |
| 692 | Continue EXACTLY from where you stopped. DO NOT repeat any content already generated. DO NOT restart the response. Output ONLY the remaining content, starting immediately from the cut-off point.`; |
| 693 | |
| 694 | const assistantContext = closeUnclosedThinking( |
| 695 | fullResponse.length > 2000 |
| 696 | ? '...\n' + fullResponse.slice(-2000) |
| 697 | : fullResponse, |
| 698 | ); |
| 699 | |
| 700 | const continuationReq: CursorChatRequest = { |
| 701 | ...cursorReq, |
| 702 | messages: [ |
| 703 | // ★ 续写优化:丢弃所有工具定义和历史消息,只保留续写上下文 |
| 704 | // 模型已经知道在写什么(从 assistantContext 可以推断),不需要工具 Schema |
| 705 | // 这样大幅减少输入体积,给输出留更多空间,续写更快 |
| 706 | { |
| 707 | parts: [{ type: 'text', text: assistantContext }], |
| 708 | id: uuidv4(), |
| 709 | role: 'assistant', |
| 710 | }, |
| 711 | { |
| 712 | parts: [{ type: 'text', text: continuationPrompt }], |
| 713 | id: uuidv4(), |
| 714 | role: 'user', |
| 715 | }, |
| 716 | ], |
| 717 | }; |
| 718 | |
| 719 | let continuationResponse = ''; |
| 720 | await sendCursorRequest(continuationReq, (event: CursorSSEEvent) => { |
| 721 | if (event.type === 'text-delta' && event.delta) { |
| 722 | continuationResponse += event.delta; |
| 723 | } |
no test coverage detected