@@ -91,6 +91,10 @@ export interface CursorAgentSdkSendInput<Error> {
9191 readonly onDelta ?: ( update : InteractionUpdate ) => Effect . Effect < void , Error > ;
9292}
9393
94+ export interface CursorAgentSdkSummarizeInput < Error > {
95+ readonly onDelta ?: ( update : InteractionUpdate ) => Effect . Effect < void , Error > ;
96+ }
97+
9498export interface CursorAgentSdkRun {
9599 readonly runId : string ;
96100 readonly agentId : string ;
@@ -103,6 +107,9 @@ export interface CursorAgentSdkSession {
103107 readonly send : < Error > (
104108 input : CursorAgentSdkSendInput < Error > ,
105109 ) => Effect . Effect < CursorAgentSdkRun , CursorAgentSdkRunnerError > ;
110+ readonly summarize : < Error > (
111+ input ?: CursorAgentSdkSummarizeInput < Error > ,
112+ ) => Effect . Effect < CursorAgentSdkRun , CursorAgentSdkRunnerError > ;
106113 readonly listMessages : Effect . Effect < ReadonlyArray < AgentMessage > , CursorAgentSdkRunnerError > ;
107114 readonly close : Effect . Effect < void , CursorAgentSdkRunnerError > ;
108115}
@@ -176,6 +183,13 @@ export type CursorAgentSdkProtocolLogEvent =
176183 readonly options : CursorAgentSdkLoggedSendOptions ;
177184 } ;
178185 }
186+ | {
187+ readonly direction : "outgoing" ;
188+ readonly stage : "decoded" ;
189+ readonly payload : {
190+ readonly type : "run.summarize" ;
191+ } ;
192+ }
179193 | {
180194 readonly direction : "incoming" ;
181195 readonly stage : "decoded" ;
@@ -318,6 +332,25 @@ export function loggedCursorSendOptions(
318332 } ;
319333}
320334
335+ function summaryMessageToInteractionUpdate ( message : {
336+ readonly type : string ;
337+ readonly status ?: string ;
338+ readonly fallback ?: boolean ;
339+ } ) : InteractionUpdate | undefined {
340+ if ( message . type !== "summary" ) return undefined ;
341+ if ( message . status === "running" ) {
342+ return { type : "summary-started" } ;
343+ }
344+ if ( message . status === "completed" || message . status === "error" ) {
345+ return {
346+ type : "summary-completed" ,
347+ ...( message . status === "error" ? { failed : true } : { } ) ,
348+ ...( message . fallback === true ? { fallback : true } : { } ) ,
349+ } ;
350+ }
351+ return undefined ;
352+ }
353+
321354function makeCursorAgentSdkProtocolLogger ( input : {
322355 readonly nativeEventLogger : ProviderEventLoggers . EventNdjsonLogger | undefined ;
323356 readonly threadId : ThreadId ;
@@ -419,6 +452,106 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
419452 } ;
420453 let abandonedRunRecoveryAvailable = input . operation === "resume" ;
421454
455+ const wrapRun = Effect . fnUntraced ( function * < Error > ( input : {
456+ readonly run : Awaited < ReturnType < typeof agent . send > > ;
457+ readonly method : string ;
458+ readonly onDelta ?: ( update : InteractionUpdate ) => Effect . Effect < void , Error > ;
459+ readonly pumpStream ?: boolean ;
460+ } ) {
461+ const context = yield * Effect . context ( ) ;
462+ let callbackFailure : { readonly cause : unknown } | undefined ;
463+ let callbackChain = Promise . resolve ( ) ;
464+ const dispatchUpdate = ( update : InteractionUpdate ) : Promise < void > => {
465+ callbackChain = callbackChain
466+ . then ( ( ) => {
467+ if ( callbackFailure !== undefined ) {
468+ return ;
469+ }
470+ return Effect . runPromiseWith ( context ) (
471+ log ( {
472+ direction : "incoming" ,
473+ stage : "decoded" ,
474+ payload : {
475+ type : "interaction.update" ,
476+ runId : input . run . id ,
477+ update,
478+ } ,
479+ } ) . pipe ( Effect . andThen ( input . onDelta ?.( update ) ?? Effect . void ) ) ,
480+ ) ;
481+ } )
482+ . catch ( ( cause ) => {
483+ callbackFailure ??= { cause } ;
484+ } ) ;
485+ return callbackChain ;
486+ } ;
487+
488+ // Keep the stream pump off callbackChain — awaiting dispatchUpdate from inside a
489+ // callbackChain task deadlocks when the next link waits for that same task to finish.
490+ const pumpDone =
491+ input . pumpStream === true
492+ ? ( async ( ) => {
493+ for await ( const message of input . run . stream ( ) ) {
494+ const update = summaryMessageToInteractionUpdate ( message ) ;
495+ if ( update !== undefined ) {
496+ await dispatchUpdate ( update ) ;
497+ }
498+ }
499+ } ) ( ) . catch ( ( cause ) => {
500+ callbackFailure ??= { cause } ;
501+ } )
502+ : Promise . resolve ( ) ;
503+
504+ return {
505+ runId : input . run . id ,
506+ agentId : input . run . agentId ,
507+ wait : Effect . tryPromise ( {
508+ try : async ( ) => {
509+ const result = await input . run . wait ( ) ;
510+ await Promise . all ( [ callbackChain , pumpDone ] ) ;
511+ if ( callbackFailure !== undefined ) {
512+ throw callbackFailure . cause ;
513+ }
514+ return result ;
515+ } ,
516+ catch : ( cause ) => runnerError ( cause , `${ input . method } .wait` ) ,
517+ } ) . pipe (
518+ Effect . tap ( ( result ) =>
519+ log ( {
520+ direction : "incoming" ,
521+ stage : "decoded" ,
522+ payload : {
523+ type : "run.completed" ,
524+ result,
525+ } ,
526+ } ) ,
527+ ) ,
528+ ) ,
529+ cancel : log ( {
530+ direction : "outgoing" ,
531+ stage : "decoded" ,
532+ payload : {
533+ type : "run.cancel" ,
534+ runId : input . run . id ,
535+ } ,
536+ } ) . pipe (
537+ Effect . andThen (
538+ Effect . tryPromise ( {
539+ try : async ( ) => {
540+ try {
541+ await input . run . cancel ( ) ;
542+ } catch ( cause ) {
543+ if ( ! isCursorCancellationError ( cause ) ) {
544+ throw cause ;
545+ }
546+ }
547+ } ,
548+ catch : ( cause ) => runnerError ( cause , `${ input . method } .cancel` ) ,
549+ } ) ,
550+ ) ,
551+ ) ,
552+ } satisfies CursorAgentSdkRun ;
553+ } ) ;
554+
422555 return {
423556 agentId : agent . agentId ,
424557 send : Effect . fn ( "CursorAgentSdkSession.send" ) ( function * ( sendInput ) {
@@ -575,6 +708,32 @@ export const makeCursorAgentSdkRunner = Effect.fn("makeCursorAgentSdkRunner")(fu
575708 ) ,
576709 } satisfies CursorAgentSdkRun ;
577710 } ) ,
711+ summarize : Effect . fn ( "CursorAgentSdkSession.summarize" ) ( function * ( summarizeInput ) {
712+ yield * log ( {
713+ direction : "outgoing" ,
714+ stage : "decoded" ,
715+ payload : { type : "run.summarize" } ,
716+ } ) ;
717+ const run = yield * Effect . tryPromise ( {
718+ try : ( ) => agent . summarize ( ) ,
719+ catch : ( cause ) => runnerError ( cause , "run.summarize" ) ,
720+ } ) ;
721+ yield * log ( {
722+ direction : "incoming" ,
723+ stage : "decoded" ,
724+ payload : {
725+ type : "run.started" ,
726+ runId : run . id ,
727+ agentId : run . agentId ,
728+ } ,
729+ } ) ;
730+ return yield * wrapRun ( {
731+ run,
732+ method : "run.summarize" ,
733+ ...( summarizeInput ?. onDelta === undefined ? { } : { onDelta : summarizeInput . onDelta } ) ,
734+ pumpStream : true ,
735+ } ) ;
736+ } ) ,
578737 listMessages : log ( {
579738 direction : "outgoing" ,
580739 stage : "decoded" ,
0 commit comments