MCPcopy Index your code
hub / github.com/7836246/cursor2api / autoContinueCursorToolResponseStream

Function autoContinueCursorToolResponseStream

src/handler.ts:666–743  ·  view source on GitHub ↗
(
    cursorReq: CursorChatRequest,
    initialResponse: string,
    hasTools: boolean,
)

Source from the content-addressed store, hash-verified

664}
665
666export 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
692Continue 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 }

Callers 3

handleOpenAIStreamFunction · 0.85
handleResponsesStreamFunction · 0.85

Calls 5

getConfigFunction · 0.90
sendCursorRequestFunction · 0.85
deduplicateContinuationFunction · 0.85
closeUnclosedThinkingFunction · 0.70

Tested by

no test coverage detected