@@ -77,13 +77,10 @@ function buildRequestContext() {
7777 } ) ;
7878}
7979
80- function jsonBlob ( value : unknown ) : Uint8Array {
81- return encoder . encode ( JSON . stringify ( value ) ) ;
82- }
83-
8480type StoredRootBlob = {
8581 id : Uint8Array ;
8682 byteLength : number ;
83+ serialized : string ;
8784 role : "system" | "user" | "assistant" | "toolResult" ;
8885 messageIndex ?: number ;
8986 /** Original JSON text payload used when an active tool result must be truncated to fit. */
@@ -95,10 +92,12 @@ function storedRootBlob(
9592 role : StoredRootBlob [ "role" ] ,
9693 opts ?: { messageIndex ?: number ; text ?: string } ,
9794) : StoredRootBlob {
98- const data = jsonBlob ( value ) ;
95+ const serialized = JSON . stringify ( value ) ;
96+ const data = encoder . encode ( serialized ) ;
9997 return {
10098 id : storeCursorBlob ( data ) ,
10199 byteLength : data . byteLength ,
100+ serialized,
102101 role,
103102 ...( opts ?. messageIndex !== undefined ? { messageIndex : opts . messageIndex } : { } ) ,
104103 ...( opts ?. text !== undefined ? { text : opts . text } : { } ) ,
@@ -165,6 +164,7 @@ function rootPromptMessages(request: CursorRunRequest): {
165164 ids : Uint8Array [ ] ;
166165 byteLength : number ;
167166 historyMessageStart : number ;
167+ serialized : string [ ] ;
168168} {
169169 const entries = systemPromptBlobs ( request ) ;
170170 const systemEntryCount = entries . length ;
@@ -174,6 +174,7 @@ function rootPromptMessages(request: CursorRunRequest): {
174174 ids : entries . map ( entry => entry . id ) ,
175175 byteLength : entries . reduce ( ( sum , entry ) => sum + entry . byteLength , 0 ) ,
176176 historyMessageStart : 0 ,
177+ serialized : entries . map ( entry => entry . serialized ) ,
177178 } ;
178179 }
179180
@@ -290,6 +291,7 @@ function rootPromptMessages(request: CursorRunRequest): {
290291 ids : selected . map ( entry => entry . id ) ,
291292 byteLength : selected . reduce ( ( sum , entry ) => sum + entry . byteLength , 0 ) ,
292293 historyMessageStart,
294+ serialized : selected . map ( entry => entry . serialized ) ,
293295 } ;
294296}
295297
@@ -504,25 +506,37 @@ export function activePromptText(request: CursorRunRequest): string {
504506 return last ?. role === "tool" ? last . content : "" ;
505507}
506508
509+ function modelVisibleRequestPayload ( request : CursorRunRequest ) {
510+ const rawText = activePromptText ( request ) ;
511+ const lastRole = request . messages . at ( - 1 ) ?. role ;
512+ const text = lastRole === "user" || lastRole === "developer"
513+ ? appendCursorShellAliasHint ( request . tools , appendCursorGenericToolUseHint ( request . tools , rawText ) )
514+ : rawText ;
515+ return {
516+ text,
517+ roots : rootPromptMessages ( request ) ,
518+ mcpToolDefs : buildCursorToolDefinitions (
519+ cursorToolsForActivePrompt ( request . tools , rawText , request . toolChoice ) ,
520+ request . toolChoice ,
521+ ) ,
522+ } ;
523+ }
524+
507525/**
508526 * Conservative request-local fallback when Cursor emits neither a checkpoint nor a previously
509527 * observed context total. This is deliberately not persisted: a later authoritative checkpoint
510528 * remains the only source carried across turns.
511529 */
512530export function estimateCursorInputTokens ( request : CursorRunRequest ) : number {
513- return estimateTokens ( JSON . stringify ( {
514- system : request . system ,
515- messages : request . rawMessages ?? request . messages ,
516- tools : request . tools ,
517- } ) , request . modelId ) ;
531+ const payload = modelVisibleRequestPayload ( request ) ;
532+ const lastRawIsToolResult = request . rawMessages ?. at ( - 1 ) ?. role === "toolResult" ;
533+ const action = ! lastRawIsToolResult && payload . text . trim ( ) . length > 0 ? payload . text : "" ;
534+ return estimateTokens ( JSON . stringify ( { roots : payload . roots . serialized , action, tools : payload . mcpToolDefs } ) , request . modelId ) ;
518535}
519536
520537export function encodeCursorRunRequest ( request : CursorRunRequest ) : Uint8Array {
521- const rawText = activePromptText ( request ) ;
522- const lastRole = request . messages . at ( - 1 ) ?. role ;
523- const text = lastRole === "user" || lastRole === "developer"
524- ? appendCursorShellAliasHint ( request . tools , appendCursorGenericToolUseHint ( request . tools , rawText ) )
525- : rawText ;
538+ const payload = modelVisibleRequestPayload ( request ) ;
539+ const text = payload . text ;
526540 // Tool-result-only turns resume the remembered Cursor conversation with results in history.
527541 const lastRawIsToolResult = request . rawMessages ?. at ( - 1 ) ?. role === "toolResult" ;
528542 const actionCase = ! lastRawIsToolResult && text . trim ( ) . length > 0
@@ -547,7 +561,7 @@ export function encodeCursorRunRequest(request: CursorRunRequest): Uint8Array {
547561 } ) ,
548562 } ,
549563 } ) ;
550- const rootPromptMessagesState = rootPromptMessages ( request ) ;
564+ const rootPromptMessagesState = payload . roots ;
551565 const rootPromptMessageIds = rootPromptMessagesState . ids ;
552566 const turnIds = conversationTurns ( request , rootPromptMessagesState . historyMessageStart ) ;
553567 debugProviderDiagnostic ( "cursor" , "run-request" , {
@@ -612,11 +626,7 @@ export function encodeCursorRunRequest(request: CursorRunRequest): Uint8Array {
612626 // the event-state `clientToolNames` use (live-transport.ts). Advertising the raw `request.tools`
613627 // here would let mcp_tools expose a tool that the event state does not recognize for a generic
614628 // tool-count prompt, so a call to it would be rejected as an unknown Responses tool.
615- ...( ( ) => {
616- const visibleTools = cursorToolsForActivePrompt ( request . tools , activePromptText ( request ) , request . toolChoice ) ;
617- const mcpToolDefs = buildCursorToolDefinitions ( visibleTools , request . toolChoice ) ;
618- return mcpToolDefs . length > 0 ? { mcpTools : create ( McpToolsSchema , { mcpTools : mcpToolDefs } ) } : { } ;
619- } ) ( ) ,
629+ ...( payload . mcpToolDefs . length > 0 ? { mcpTools : create ( McpToolsSchema , { mcpTools : payload . mcpToolDefs } ) } : { } ) ,
620630 } ) ;
621631
622632 const message = create ( AgentClientMessageSchema , {
0 commit comments