|
1 | 1 | import type { AdapterEvent, OcxProviderConfig } from "../types"; |
2 | 2 | import type { ProviderAdapter } from "./base"; |
3 | 3 | import { cursorExecDeniedMessage, cursorRequestDeclaresFullAccess } from "./cursor/exec-policy"; |
4 | | -import { isCursorBenignCancelError, safeCursorErrorMessage } from "./cursor/cursor-errors"; |
| 4 | +import { isCursorBenignCancelError, isCursorInvalidArgumentError, safeCursorErrorMessage } from "./cursor/cursor-errors"; |
| 5 | +import { isCursorExternalWireModel } from "./cursor/discovery"; |
5 | 6 | import { createCursorKvStore, type CursorKvStore } from "./cursor/kv-store"; |
6 | 7 | import { mapCursorServerMessage } from "./cursor/message-mapper"; |
7 | 8 | import { createCursorRequest, generatedCursorConversationId } from "./cursor/request-builder"; |
8 | | -import { createLiveCursorTransport, CursorMissingCredentialError } from "./cursor/live-transport"; |
| 9 | +import { |
| 10 | + createLiveCursorTransport, |
| 11 | + CursorMissingCredentialError, |
| 12 | + rekeyCursorContextUsage, |
| 13 | +} from "./cursor/live-transport"; |
9 | 14 | import { runCursorTurnWithRetry } from "./cursor/transport-retry"; |
10 | 15 | import { |
11 | 16 | createDisabledCursorTransport, |
@@ -70,26 +75,68 @@ export function createCursorAdapter(provider: OcxProviderConfig, deps: CursorAda |
70 | 75 | const makeTransport = deps.createTransport ?? createLiveCursorTransport; |
71 | 76 | const kv = deps.kv ?? createCursorKvStore(); |
72 | 77 | _parsed._cursorConversationId ??= generatedCursorConversationId(); |
73 | | - const request = createCursorRequest(_parsed); |
74 | | - await runCursorTurnWithRetry( |
75 | | - makeTransport, |
76 | | - { provider, headers: incoming.headers, requestDeclaresFullAccess: cursorRequestDeclaresFullAccess(request) }, |
77 | | - request, |
78 | | - incoming.abortSignal, |
79 | | - (message, activeTransport) => { |
80 | | - if (incoming.abortSignal?.aborted) { |
81 | | - emit({ type: "error", message: "Cursor turn was aborted." }); |
82 | | - return; |
83 | | - } |
84 | | - const events = mapCursorServerMessage(message, { |
85 | | - kv, |
86 | | - writeClient: clientMessage => { |
87 | | - void activeTransport.writeClient(clientMessage); |
88 | | - }, |
89 | | - }); |
90 | | - for (const event of events) emit(event); |
91 | | - }, |
92 | | - ); |
| 78 | + const previousConversationId = _parsed._cursorConversationId; |
| 79 | + let request = createCursorRequest(_parsed); |
| 80 | + // Keep remembered conversation id in sync when the request builder mints a fresh id |
| 81 | + // for external-model tool-result continuations (stateless replay). |
| 82 | + if (request.conversationId !== previousConversationId) { |
| 83 | + rekeyCursorContextUsage(previousConversationId, request.conversationId); |
| 84 | + } |
| 85 | + _parsed._cursorConversationId = request.conversationId; |
| 86 | + let emittedOutput = false; |
| 87 | + const lastRawIsToolResult = _parsed.context.messages.at(-1)?.role === "toolResult"; |
| 88 | + |
| 89 | + const runOnce = async (activeRequest: ReturnType<typeof createCursorRequest>) => { |
| 90 | + await runCursorTurnWithRetry( |
| 91 | + makeTransport, |
| 92 | + { |
| 93 | + provider, |
| 94 | + headers: incoming.headers, |
| 95 | + requestDeclaresFullAccess: cursorRequestDeclaresFullAccess(activeRequest), |
| 96 | + }, |
| 97 | + activeRequest, |
| 98 | + incoming.abortSignal, |
| 99 | + (message, activeTransport) => { |
| 100 | + if (incoming.abortSignal?.aborted) { |
| 101 | + emit({ type: "error", message: "Cursor turn was aborted." }); |
| 102 | + return; |
| 103 | + } |
| 104 | + const events = mapCursorServerMessage(message, { |
| 105 | + kv, |
| 106 | + writeClient: clientMessage => { |
| 107 | + void activeTransport.writeClient(clientMessage); |
| 108 | + }, |
| 109 | + }); |
| 110 | + for (const event of events) { |
| 111 | + if (event.type !== "heartbeat") emittedOutput = true; |
| 112 | + emit(event); |
| 113 | + } |
| 114 | + }, |
| 115 | + ); |
| 116 | + }; |
| 117 | + |
| 118 | + try { |
| 119 | + await runOnce(request); |
| 120 | + } catch (err) { |
| 121 | + // One-shot fallback: only for external-model tool-result continuations that fail |
| 122 | + // with Connect invalid_argument before any non-heartbeat output was forwarded. |
| 123 | + // Replaying after text/tool events would duplicate output. |
| 124 | + if ( |
| 125 | + !isCursorInvalidArgumentError(err) |
| 126 | + || !isCursorExternalWireModel(request.modelId) |
| 127 | + || !lastRawIsToolResult |
| 128 | + || emittedOutput |
| 129 | + || incoming.abortSignal?.aborted |
| 130 | + ) { |
| 131 | + throw err; |
| 132 | + } |
| 133 | + const failedConversationId = request.conversationId; |
| 134 | + _parsed._cursorConversationId = undefined; |
| 135 | + request = createCursorRequest(_parsed, { forceFreshConversation: true }); |
| 136 | + rekeyCursorContextUsage(failedConversationId, request.conversationId); |
| 137 | + _parsed._cursorConversationId = request.conversationId; |
| 138 | + await runOnce(request); |
| 139 | + } |
93 | 140 | } catch (err) { |
94 | 141 | if (isCursorBenignCancelError(err)) return; |
95 | 142 | const partialUsage = (err as { partialUsage?: import("../types").OcxUsage }).partialUsage; |
|
0 commit comments