|
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"; |
@@ -70,26 +71,55 @@ export function createCursorAdapter(provider: OcxProviderConfig, deps: CursorAda |
70 | 71 | const makeTransport = deps.createTransport ?? createLiveCursorTransport; |
71 | 72 | const kv = deps.kv ?? createCursorKvStore(); |
72 | 73 | _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 | | - ); |
| 74 | + let request = createCursorRequest(_parsed); |
| 75 | + // Keep remembered conversation id in sync when the request builder mints a fresh id |
| 76 | + // for external-model tool-result continuations (stateless replay). |
| 77 | + _parsed._cursorConversationId = request.conversationId; |
| 78 | + |
| 79 | + const runOnce = async (activeRequest: ReturnType<typeof createCursorRequest>) => { |
| 80 | + await runCursorTurnWithRetry( |
| 81 | + makeTransport, |
| 82 | + { |
| 83 | + provider, |
| 84 | + headers: incoming.headers, |
| 85 | + requestDeclaresFullAccess: cursorRequestDeclaresFullAccess(activeRequest), |
| 86 | + }, |
| 87 | + activeRequest, |
| 88 | + incoming.abortSignal, |
| 89 | + (message, activeTransport) => { |
| 90 | + if (incoming.abortSignal?.aborted) { |
| 91 | + emit({ type: "error", message: "Cursor turn was aborted." }); |
| 92 | + return; |
| 93 | + } |
| 94 | + const events = mapCursorServerMessage(message, { |
| 95 | + kv, |
| 96 | + writeClient: clientMessage => { |
| 97 | + void activeTransport.writeClient(clientMessage); |
| 98 | + }, |
| 99 | + }); |
| 100 | + for (const event of events) emit(event); |
| 101 | + }, |
| 102 | + ); |
| 103 | + }; |
| 104 | + |
| 105 | + try { |
| 106 | + await runOnce(request); |
| 107 | + } catch (err) { |
| 108 | + // One-shot fallback: external Cursor models can reject resume/continuation on a |
| 109 | + // stale server-side conversation with Connect invalid_argument after stepCompleted. |
| 110 | + // Replay once with a fresh conversation id and full history. |
| 111 | + if ( |
| 112 | + !isCursorInvalidArgumentError(err) |
| 113 | + || !isCursorExternalWireModel(request.modelId) |
| 114 | + || incoming.abortSignal?.aborted |
| 115 | + ) { |
| 116 | + throw err; |
| 117 | + } |
| 118 | + _parsed._cursorConversationId = undefined; |
| 119 | + request = createCursorRequest(_parsed, { forceFreshConversation: true }); |
| 120 | + _parsed._cursorConversationId = request.conversationId; |
| 121 | + await runOnce(request); |
| 122 | + } |
93 | 123 | } catch (err) { |
94 | 124 | if (isCursorBenignCancelError(err)) return; |
95 | 125 | const partialUsage = (err as { partialUsage?: import("../types").OcxUsage }).partialUsage; |
|
0 commit comments