@@ -3,6 +3,11 @@ import { Notice } from 'obsidian';
33import { hasErrorContentBlock } from '../../../core/chat/error-blocks' ;
44import { detectBuiltInCommand } from '../../../core/commands/built-in-commands' ;
55import type { BrowserSelectionContext , CanvasSelectionContext } from '../../../core/context/types' ;
6+ import {
7+ logElapsed ,
8+ measure ,
9+ measureAsync ,
10+ } from '../../../core/diagnostics/performance' ;
611import type { EditorSelectionContext } from '../../../core/editor/editor-context' ;
712import type { ChatRuntime } from '../../../core/runtime/chat-runtime' ;
813import type { ApprovalCallbackOptions , ChatTurnRequest } from '../../../core/runtime/types' ;
@@ -277,13 +282,13 @@ export class InputController {
277282 displayContent : content ,
278283 turnRequest : cloneChatTurnRequest ( options . turnRequestOverride ) ,
279284 }
280- : await this . buildTurnSubmission ( {
285+ : await measureAsync ( 'turn.buildSubmission' , ( ) => this . buildTurnSubmission ( {
281286 content,
282287 images : imagesForMessage ,
283288 editorContextOverride : options ?. editorContextOverride ,
284289 browserContextOverride : options ?. browserContextOverride ,
285290 canvasContextOverride : options ?. canvasContextOverride ,
286- } ) ;
291+ } ) ) ;
287292 const { displayContent, turnRequest } = turnSubmission ;
288293
289294 fileContextManager ?. markCurrentNoteSent ( ) ;
@@ -300,7 +305,7 @@ export class InputController {
300305 state . hasPendingConversationSave = true ;
301306 renderer . addMessage ( userMsg ) ;
302307
303- await this . triggerTitleGeneration ( ) ;
308+ await measureAsync ( 'turn.titleGeneration' , ( ) => this . triggerTitleGeneration ( ) ) ;
304309
305310 const assistantMsg : ChatMessage = {
306311 id : this . deps . generateId ( ) ,
@@ -325,6 +330,9 @@ export class InputController {
325330 isCompact ? 'qoderian-thinking--compact' : undefined ,
326331 ) ;
327332 state . responseStartTime = performance . now ( ) ;
333+ // Turn-level timing origin for the [qoderian perf] turn.* diagnostics.
334+ const turnStartedAt = state . responseStartTime ;
335+ let sawFirstRuntimeChunk = false ;
328336
329337 let wasInterrupted = false ;
330338 let wasInvalidated = false ;
@@ -333,7 +341,7 @@ export class InputController {
333341
334342 // Lazy initialization: ensure service is ready before first query
335343 if ( this . deps . ensureServiceInitialized ) {
336- const ready = await this . deps . ensureServiceInitialized ( ) ;
344+ const ready = await measureAsync ( 'turn.serviceInit' , ( ) => this . deps . ensureServiceInitialized ! ( ) ) ;
337345 if ( ! ready ) {
338346 new Notice ( 'Failed to initialize agent service. Please try again.' ) ;
339347 streamController . hideThinkingIndicator ( ) ;
@@ -376,7 +384,7 @@ export class InputController {
376384 }
377385
378386 try {
379- const preparedTurn = agentService . prepareTurn ( turnRequest ) ;
387+ const preparedTurn = measure ( 'turn.prepareTurn' , ( ) => agentService . prepareTurn ( turnRequest ) ) ;
380388 userMsg . content = preparedTurn . persistedContent ;
381389 userMsg . currentNote = preparedTurn . isCompact
382390 ? undefined
@@ -386,6 +394,10 @@ export class InputController {
386394 // This prevents duplication when rebuilding context for new sessions
387395 const previousMessages = state . messages . slice ( 0 , - 2 ) ;
388396 for await ( const chunk of agentService . query ( preparedTurn , previousMessages ) ) {
397+ if ( ! sawFirstRuntimeChunk ) {
398+ sawFirstRuntimeChunk = true ;
399+ logElapsed ( 'turn.firstChunk' , turnStartedAt ) ;
400+ }
389401 if ( state . streamGeneration !== streamGeneration ) {
390402 wasInvalidated = true ;
391403 break ;
@@ -416,6 +428,7 @@ export class InputController {
416428 this . activeStreamingAssistantMessage ?? assistantMsg ,
417429 ) ;
418430 } finally {
431+ logElapsed ( 'turn.total' , turnStartedAt ) ;
419432 const finalAssistantMsg = this . activeStreamingAssistantMessage ?? assistantMsg ;
420433 const turnMetadata = agentService . consumeTurnMetadata ( ) ;
421434 userMsg . userMessageId = turnMetadata . userMessageId ?? userMsg . userMessageId ;
0 commit comments