@@ -54,7 +54,7 @@ public partial class CopilotSession : IAsyncDisposable
5454 private event SessionEventHandler ? _eventHandlers ;
5555 private readonly Dictionary < string , AIFunction > _toolHandlers = new ( ) ;
5656 private readonly JsonRpc _rpc ;
57- private readonly CopilotTelemetry . AgentTurnTracker ? _turnTracker ;
57+ private readonly CopilotTelemetry . AgentTurnTracker ? _telemetryTracker ;
5858 private volatile PermissionRequestHandler ? _permissionHandler ;
5959 private volatile UserInputHandler ? _userInputHandler ;
6060 private SessionHooks ? _hooks ;
@@ -82,12 +82,12 @@ public partial class CopilotSession : IAsyncDisposable
8282 /// </value>
8383 public string ? WorkspacePath { get ; internal set ; }
8484
85- internal string TelemetryProviderName => _turnTracker ? . ProviderName ?? OpenTelemetryConsts . DefaultProviderName ;
86- internal string ? TelemetryServerAddress => _turnTracker ? . ServerAddress ;
87- internal int ? TelemetryServerPort => _turnTracker ? . ServerPort ;
88- internal ActivityContext TelemetryActivityContext => _turnTracker ? . GetActivityContext ( ) ?? default ;
85+ internal string TelemetryProviderName => _telemetryTracker ? . ProviderName ?? OpenTelemetryConsts . DefaultProviderName ;
86+ internal string ? TelemetryServerAddress => _telemetryTracker ? . ServerAddress ;
87+ internal int ? TelemetryServerPort => _telemetryTracker ? . ServerPort ;
88+ internal ActivityContext TelemetryActivityContext => _telemetryTracker ? . GetActivityContext ( ) ?? default ;
8989 internal ActivityContext GetTelemetryToolCallParentContext ( string toolCallId ) =>
90- _turnTracker ? . GetToolCallParentContext ( toolCallId ) ?? default ;
90+ _telemetryTracker ? . GetToolCallParentContext ( toolCallId ) ?? default ;
9191
9292 /// <summary>
9393 /// Initializes a new instance of the <see cref="CopilotSession"/> class.
@@ -119,7 +119,7 @@ internal CopilotSession(
119119 {
120120 SessionId = sessionId ;
121121 _rpc = rpc ;
122- _turnTracker = telemetry is not null ? new CopilotTelemetry . AgentTurnTracker ( telemetry , sessionId , model , provider , systemMessage , tools , streaming , agentName , agentDescription ) : null ;
122+ _telemetryTracker = telemetry is not null ? new CopilotTelemetry . AgentTurnTracker ( telemetry , sessionId , model , provider , systemMessage , tools , streaming , agentName , agentDescription ) : null ;
123123 WorkspacePath = workspacePath ;
124124 }
125125
@@ -156,27 +156,18 @@ private Task<T> InvokeRpcAsync<T>(string method, object?[]? args, CancellationTo
156156 /// </example>
157157 public async Task < string > SendAsync ( MessageOptions options , CancellationToken cancellationToken = default )
158158 {
159- _turnTracker ? . BeginSend ( options . Prompt ) ;
160- try
159+ var request = new SendMessageRequest
161160 {
162- var request = new SendMessageRequest
163- {
164- SessionId = SessionId ,
165- Prompt = options . Prompt ,
166- Attachments = options . Attachments ,
167- Mode = options . Mode
168- } ;
161+ SessionId = SessionId ,
162+ Prompt = options . Prompt ,
163+ Attachments = options . Attachments ,
164+ Mode = options . Mode
165+ } ;
169166
170- var response = await InvokeRpcAsync < SendMessageResponse > (
171- "session.send" , [ request ] , cancellationToken ) ;
167+ var response = await InvokeRpcAsync < SendMessageResponse > (
168+ "session.send" , [ request ] , cancellationToken ) ;
172169
173- return response . MessageId ;
174- }
175- catch ( Exception ex ) when ( _turnTracker is { } tracker )
176- {
177- tracker . CompleteTurnWithError ( ex ) ;
178- throw ;
179- }
170+ return response . MessageId ;
180171 }
181172
182173 /// <summary>
@@ -238,28 +229,18 @@ void Handler(SessionEvent evt)
238229
239230 await SendAsync ( options , cancellationToken ) ;
240231
241- try
242- {
243- using var cts = CancellationTokenSource . CreateLinkedTokenSource ( cancellationToken ) ;
244- cts . CancelAfter ( effectiveTimeout ) ;
245-
246- using var registration = cts . Token . Register ( ( ) =>
247- {
248- if ( cancellationToken . IsCancellationRequested )
249- tcs . TrySetCanceled ( cancellationToken ) ;
250- else
251- tcs . TrySetException ( new TimeoutException ( $ "SendAndWaitAsync timed out after { effectiveTimeout } ") ) ;
252- } ) ;
232+ using var cts = CancellationTokenSource . CreateLinkedTokenSource ( cancellationToken ) ;
233+ cts . CancelAfter ( effectiveTimeout ) ;
253234
254- return await tcs . Task ;
255- }
256- catch ( Exception ex ) when ( _turnTracker is { } tracker )
235+ using var registration = cts . Token . Register ( ( ) =>
257236 {
258- // If timeout/cancellation occurs before DispatchEvent handles the turn-ending event,
259- // complete the telemetry span with the error (idempotent if already completed).
260- tracker . CompleteTurnWithError ( ex ) ;
261- throw ;
262- }
237+ if ( cancellationToken . IsCancellationRequested )
238+ tcs . TrySetCanceled ( cancellationToken ) ;
239+ else
240+ tcs . TrySetException ( new TimeoutException ( $ "SendAndWaitAsync timed out after { effectiveTimeout } ") ) ;
241+ } ) ;
242+
243+ return await tcs . Task ;
263244 }
264245
265246 /// <summary>
@@ -309,7 +290,7 @@ public IDisposable On(SessionEventHandler handler)
309290 /// </remarks>
310291 internal void DispatchEvent ( SessionEvent sessionEvent )
311292 {
312- _turnTracker ? . ProcessEvent ( sessionEvent ) ;
293+ _telemetryTracker ? . ProcessEvent ( sessionEvent ) ;
313294
314295 // Reading the field once gives us a snapshot; delegates are immutable.
315296 _eventHandlers ? . Invoke ( sessionEvent ) ;
@@ -620,7 +601,7 @@ await InvokeRpcAsync<object>(
620601
621602 _eventHandlers = null ;
622603 _toolHandlers . Clear ( ) ;
623- _turnTracker ? . CompleteOnDispose ( ) ;
604+ _telemetryTracker ? . CompleteOnDispose ( ) ;
624605
625606 _permissionHandler = null ;
626607 }
0 commit comments