@@ -347,16 +347,8 @@ public async Task<CopilotSession> CreateSessionAsync(SessionConfig? config = nul
347347 config ? . DisabledSkills ,
348348 config ? . InfiniteSessions ) ;
349349
350- CreateSessionResponse response ;
351- try
352- {
353- response = await connection . Rpc . InvokeWithCancellationAsync < CreateSessionResponse > (
354- "session.create" , [ request ] , cancellationToken ) ;
355- }
356- catch ( StreamJsonRpc . RemoteInvocationException ex )
357- {
358- throw new CopilotRpcException ( $ "Failed to create session: { ex . Message } ", ex ) ;
359- }
350+ var response = await InvokeRpcAsync < CreateSessionResponse > (
351+ connection . Rpc , "session.create" , [ request ] , cancellationToken ) ;
360352
361353 var session = new CopilotSession ( response . SessionId , connection . Rpc , response . WorkspacePath ) ;
362354 session . RegisterTools ( config ? . Tools ?? [ ] ) ;
@@ -412,16 +404,8 @@ public async Task<CopilotSession> ResumeSessionAsync(string sessionId, ResumeSes
412404 config ? . SkillDirectories ,
413405 config ? . DisabledSkills ) ;
414406
415- ResumeSessionResponse response ;
416- try
417- {
418- response = await connection . Rpc . InvokeWithCancellationAsync < ResumeSessionResponse > (
419- "session.resume" , [ request ] , cancellationToken ) ;
420- }
421- catch ( StreamJsonRpc . RemoteInvocationException ex )
422- {
423- throw new CopilotRpcException ( $ "Failed to resume session: { ex . Message } ", ex ) ;
424- }
407+ var response = await InvokeRpcAsync < ResumeSessionResponse > (
408+ connection . Rpc , "session.resume" , [ request ] , cancellationToken ) ;
425409
426410 var session = new CopilotSession ( response . SessionId , connection . Rpc , response . WorkspacePath ) ;
427411 session . RegisterTools ( config ? . Tools ?? [ ] ) ;
@@ -477,18 +461,8 @@ public async Task<PingResponse> PingAsync(string? message = null, CancellationTo
477461 {
478462 var connection = await EnsureConnectedAsync ( cancellationToken ) ;
479463
480- PingResponse response ;
481- try
482- {
483- response = await connection . Rpc . InvokeWithCancellationAsync < PingResponse > (
484- "ping" , [ new PingRequest { Message = message } ] , cancellationToken ) ;
485- }
486- catch ( StreamJsonRpc . RemoteInvocationException ex )
487- {
488- throw new CopilotRpcException ( $ "Failed to ping server: { ex . Message } ", ex ) ;
489- }
490-
491- return response ;
464+ return await InvokeRpcAsync < PingResponse > (
465+ connection . Rpc , "ping" , [ new PingRequest { Message = message } ] , cancellationToken ) ;
492466 }
493467
494468 /// <summary>
@@ -501,18 +475,8 @@ public async Task<GetStatusResponse> GetStatusAsync(CancellationToken cancellati
501475 {
502476 var connection = await EnsureConnectedAsync ( cancellationToken ) ;
503477
504- GetStatusResponse response ;
505- try
506- {
507- response = await connection . Rpc . InvokeWithCancellationAsync < GetStatusResponse > (
508- "status.get" , [ ] , cancellationToken ) ;
509- }
510- catch ( StreamJsonRpc . RemoteInvocationException ex )
511- {
512- throw new CopilotRpcException ( $ "Failed to get status: { ex . Message } ", ex ) ;
513- }
514-
515- return response ;
478+ return await InvokeRpcAsync < GetStatusResponse > (
479+ connection . Rpc , "status.get" , [ ] , cancellationToken ) ;
516480 }
517481
518482 /// <summary>
@@ -525,18 +489,8 @@ public async Task<GetAuthStatusResponse> GetAuthStatusAsync(CancellationToken ca
525489 {
526490 var connection = await EnsureConnectedAsync ( cancellationToken ) ;
527491
528- GetAuthStatusResponse response ;
529- try
530- {
531- response = await connection . Rpc . InvokeWithCancellationAsync < GetAuthStatusResponse > (
532- "auth.getStatus" , [ ] , cancellationToken ) ;
533- }
534- catch ( StreamJsonRpc . RemoteInvocationException ex )
535- {
536- throw new CopilotRpcException ( $ "Failed to get auth status: { ex . Message } ", ex ) ;
537- }
538-
539- return response ;
492+ return await InvokeRpcAsync < GetAuthStatusResponse > (
493+ connection . Rpc , "auth.getStatus" , [ ] , cancellationToken ) ;
540494 }
541495
542496 /// <summary>
@@ -549,16 +503,8 @@ public async Task<List<ModelInfo>> ListModelsAsync(CancellationToken cancellatio
549503 {
550504 var connection = await EnsureConnectedAsync ( cancellationToken ) ;
551505
552- GetModelsResponse response ;
553- try
554- {
555- response = await connection . Rpc . InvokeWithCancellationAsync < GetModelsResponse > (
556- "models.list" , [ ] , cancellationToken ) ;
557- }
558- catch ( StreamJsonRpc . RemoteInvocationException ex )
559- {
560- throw new CopilotRpcException ( $ "Failed to list models: { ex . Message } ", ex ) ;
561- }
506+ var response = await InvokeRpcAsync < GetModelsResponse > (
507+ connection . Rpc , "models.list" , [ ] , cancellationToken ) ;
562508
563509 return response . Models ;
564510 }
@@ -582,16 +528,8 @@ public async Task<List<ModelInfo>> ListModelsAsync(CancellationToken cancellatio
582528 {
583529 var connection = await EnsureConnectedAsync ( cancellationToken ) ;
584530
585- GetLastSessionIdResponse response ;
586- try
587- {
588- response = await connection . Rpc . InvokeWithCancellationAsync < GetLastSessionIdResponse > (
589- "session.getLastId" , [ ] , cancellationToken ) ;
590- }
591- catch ( StreamJsonRpc . RemoteInvocationException ex )
592- {
593- throw new CopilotRpcException ( $ "Failed to get last session ID: { ex . Message } ", ex ) ;
594- }
531+ var response = await InvokeRpcAsync < GetLastSessionIdResponse > (
532+ connection . Rpc , "session.getLastId" , [ ] , cancellationToken ) ;
595533
596534 return response . SessionId ;
597535 }
@@ -616,16 +554,8 @@ public async Task DeleteSessionAsync(string sessionId, CancellationToken cancell
616554 {
617555 var connection = await EnsureConnectedAsync ( cancellationToken ) ;
618556
619- DeleteSessionResponse response ;
620- try
621- {
622- response = await connection . Rpc . InvokeWithCancellationAsync < DeleteSessionResponse > (
623- "session.delete" , [ new DeleteSessionRequest ( sessionId ) ] , cancellationToken ) ;
624- }
625- catch ( StreamJsonRpc . RemoteInvocationException ex )
626- {
627- throw new CopilotRpcException ( $ "Failed to delete session: { ex . Message } ", ex ) ;
628- }
557+ var response = await InvokeRpcAsync < DeleteSessionResponse > (
558+ connection . Rpc , "session.delete" , [ new DeleteSessionRequest ( sessionId ) ] , cancellationToken ) ;
629559
630560 if ( ! response . Success )
631561 {
@@ -654,18 +584,22 @@ public async Task<List<SessionMetadata>> ListSessionsAsync(CancellationToken can
654584 {
655585 var connection = await EnsureConnectedAsync ( cancellationToken ) ;
656586
657- ListSessionsResponse response ;
587+ var response = await InvokeRpcAsync < ListSessionsResponse > (
588+ connection . Rpc , "session.list" , [ ] , cancellationToken ) ;
589+
590+ return response . Sessions ;
591+ }
592+
593+ private static async Task < T > InvokeRpcAsync < T > ( JsonRpc rpc , string method , object ? [ ] ? args , CancellationToken cancellationToken )
594+ {
658595 try
659596 {
660- response = await connection . Rpc . InvokeWithCancellationAsync < ListSessionsResponse > (
661- "session.list" , [ ] , cancellationToken ) ;
597+ return await rpc . InvokeWithCancellationAsync < T > ( method , args , cancellationToken ) ;
662598 }
663599 catch ( StreamJsonRpc . RemoteInvocationException ex )
664600 {
665- throw new CopilotRpcException ( $ "Failed to list sessions : { ex . Message } ", ex ) ;
601+ throw new IOException ( $ "Communication error with Copilot CLI : { ex . Message } ", ex ) ;
666602 }
667-
668- return response . Sessions ;
669603 }
670604
671605 private Task < Connection > EnsureConnectedAsync ( CancellationToken cancellationToken )
@@ -682,16 +616,8 @@ private Task<Connection> EnsureConnectedAsync(CancellationToken cancellationToke
682616 private async Task VerifyProtocolVersionAsync ( Connection connection , CancellationToken cancellationToken )
683617 {
684618 var expectedVersion = SdkProtocolVersion . GetVersion ( ) ;
685- PingResponse pingResponse ;
686- try
687- {
688- pingResponse = await connection . Rpc . InvokeWithCancellationAsync < PingResponse > (
689- "ping" , [ new PingRequest ( ) ] , cancellationToken ) ;
690- }
691- catch ( StreamJsonRpc . RemoteInvocationException ex )
692- {
693- throw new CopilotRpcException ( $ "Failed to verify protocol version: { ex . Message } ", ex ) ;
694- }
619+ var pingResponse = await InvokeRpcAsync < PingResponse > (
620+ connection . Rpc , "ping" , [ new PingRequest ( ) ] , cancellationToken ) ;
695621
696622 if ( ! pingResponse . ProtocolVersion . HasValue )
697623 {
0 commit comments