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

Function executeAttempt

src/handler.ts:928–1043  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

926 });
927
928 const executeAttempt = async (): Promise<{
929 rawResponse: string;
930 visibleText: string;
931 thinkingContent: string;
932 streamer: ReturnType<typeof createIncrementalTextStreamer>;
933 }> => {
934 let rawResponse = '';
935 let visibleText = '';
936 let leadingBuffer = '';
937 let leadingResolved = false;
938 let thinkingContent = '';
939 const attemptStreamer = createIncrementalTextStreamer({
940 warmupChars: 300, // ★ 与工具模式对齐
941 transform: sanitizeResponse,
942 isBlockedPrefix: (text) => isRefusal(text.substring(0, 300)),
943 });
944
945 const flushVisible = (chunk: string): void => {
946 if (!chunk) return;
947 visibleText += chunk;
948 const delta = attemptStreamer.push(chunk);
949 if (!delta) return;
950
951 if (clientRequestedThinking && thinkingContent && !streamState.thinkingEmitted) {
952 emitAnthropicThinkingBlock(res, streamState, thinkingContent);
953 }
954 writeAnthropicTextDelta(res, streamState, delta);
955 };
956
957 const apiStart = Date.now();
958 let firstChunk = true;
959 log.startPhase('send', '发送到 Cursor');
960
961 await sendCursorRequest(activeCursorReq, (event: CursorSSEEvent) => {
962 if (event.type === 'finish') {
963 if (event.messageMetadata?.usage) cursorUsage = event.messageMetadata.usage;
964 return;
965 }
966 if (event.type !== 'text-delta' || !event.delta) return;
967
968 if (firstChunk) {
969 log.recordTTFT();
970 log.endPhase();
971 log.startPhase('response', '接收响应');
972 firstChunk = false;
973 }
974
975 rawResponse += event.delta;
976
977 // ★ 始终缓冲前导内容以检测并剥离 <thinking> 标签
978 // 无论 clientRequestedThinking 是否为 true,都需要分离 thinking
979 // 区别在于:true 时发送 thinking content block,false 时静默丢弃 thinking 标签
980 if (!leadingResolved) {
981 leadingBuffer += event.delta;
982 const split = splitLeadingThinkingBlocks(leadingBuffer);
983
984 if (split.startedWithThinking) {
985 if (!split.complete) return;

Callers 1

handleDirectTextStreamFunction · 0.70

Calls 9

isRefusalFunction · 0.90
sendCursorRequestFunction · 0.85
startPhaseMethod · 0.80
recordTTFTMethod · 0.80
endPhaseMethod · 0.80
recordCursorApiTimeMethod · 0.80
flushVisibleFunction · 0.70

Tested by

no test coverage detected