@@ -8,14 +8,14 @@ import {
8
8
type Update ,
9
9
wrapThunk ,
10
10
} from "../tea/tea.ts" ;
11
- import { d , type View } from "../tea/view.ts" ;
11
+ import { d , withBindings , type View } from "../tea/view.ts" ;
12
12
import * as ToolManager from "../tools/toolManager.ts" ;
13
13
import { type Result } from "../utils/result.ts" ;
14
14
import { Counter } from "../utils/uniqueId.ts" ;
15
15
import type { Nvim } from "nvim-node" ;
16
16
import type { Lsp } from "../lsp.ts" ;
17
17
import {
18
- getClient ,
18
+ getClient as getProvider ,
19
19
type ProviderMessage ,
20
20
type ProviderMessageContent ,
21
21
type ProviderName ,
@@ -24,6 +24,7 @@ import {
24
24
} from "../providers/provider.ts" ;
25
25
import { assertUnreachable } from "../utils/assertUnreachable.ts" ;
26
26
import { DEFAULT_OPTIONS , type MagentaOptions } from "../options.ts" ;
27
+ import { getOption } from "../nvim/nvim.ts" ;
27
28
28
29
export type Role = "user" | "assistant" ;
29
30
@@ -103,6 +104,9 @@ export type Msg =
103
104
| {
104
105
type : "set-opts" ;
105
106
options : MagentaOptions ;
107
+ }
108
+ | {
109
+ type : "show-message-debug-info" ;
106
110
} ;
107
111
108
112
export function init ( { nvim, lsp } : { nvim : Nvim ; lsp : Lsp } ) {
@@ -421,7 +425,7 @@ ${msg.error.stack}`,
421
425
model ,
422
426
// eslint-disable-next-line @typescript-eslint/require-await
423
427
async ( ) => {
424
- getClient ( nvim , model . activeProvider , model . options ) . abort ( ) ;
428
+ getProvider ( nvim , model . activeProvider , model . options ) . abort ( ) ;
425
429
} ,
426
430
] ;
427
431
}
@@ -430,6 +434,10 @@ ${msg.error.stack}`,
430
434
return [ { ...model , options : msg . options } ] ;
431
435
}
432
436
437
+ case "show-message-debug-info" : {
438
+ return [ model , ( ) => showDebugInfo ( model ) ] ;
439
+ }
440
+
433
441
default :
434
442
assertUnreachable ( msg ) ;
435
443
}
@@ -490,7 +498,7 @@ ${msg.error.stack}`,
490
498
} ) ;
491
499
let res ;
492
500
try {
493
- res = await getClient (
501
+ res = await getProvider (
494
502
nvim ,
495
503
model . activeProvider ,
496
504
model . options ,
@@ -562,12 +570,17 @@ ${msg.error.stack}`,
562
570
) % MESSAGE_ANIMATION . length
563
571
]
564
572
} `
565
- : d `Stopped (${ model . conversation . stopReason } ) [input: ${ model . conversation . usage . inputTokens . toString ( ) } , output: ${ model . conversation . usage . outputTokens . toString ( ) } ${
566
- model . conversation . usage . cacheHits !== undefined &&
567
- model . conversation . usage . cacheMisses !== undefined
568
- ? d `, cache hits: ${ model . conversation . usage . cacheHits . toString ( ) } , cache misses: ${ model . conversation . usage . cacheMisses . toString ( ) } `
569
- : ""
570
- } ]`
573
+ : withBindings (
574
+ d `Stopped (${ model . conversation . stopReason } ) [input: ${ model . conversation . usage . inputTokens . toString ( ) } , output: ${ model . conversation . usage . outputTokens . toString ( ) } ${
575
+ model . conversation . usage . cacheHits !== undefined &&
576
+ model . conversation . usage . cacheMisses !== undefined
577
+ ? d `, cache hits: ${ model . conversation . usage . cacheHits . toString ( ) } , cache misses: ${ model . conversation . usage . cacheMisses . toString ( ) } `
578
+ : ""
579
+ } ]`,
580
+ {
581
+ "<CR>" : ( ) => dispatch ( { type : "show-message-debug-info" } ) ,
582
+ } ,
583
+ )
571
584
: ""
572
585
} ${
573
586
model . conversation . state == "stopped" &&
@@ -642,6 +655,44 @@ ${msg.error.stack}`,
642
655
return messages . map ( ( m ) => m . message ) ;
643
656
}
644
657
658
+ async function showDebugInfo ( model : Model ) {
659
+ const messages = await getMessages ( model ) ;
660
+ const provider = getProvider ( nvim , model . activeProvider , model . options ) ;
661
+ const params = provider . createStreamParameters ( messages ) ;
662
+ const nTokens = await provider . countTokens ( messages ) ;
663
+
664
+ // Create a floating window
665
+ const bufnr = await nvim . call ( "nvim_create_buf" , [ false , true ] ) ;
666
+ await nvim . call ( "nvim_buf_set_option" , [ bufnr , "bufhidden" , "wipe" ] ) ;
667
+ const [ editorWidth , editorHeight ] = ( await Promise . all ( [
668
+ getOption ( "columns" , nvim ) ,
669
+ await getOption ( "lines" , nvim ) ,
670
+ ] ) ) as [ number , number ] ;
671
+ const width = 80 ;
672
+ const height = editorHeight - 20 ;
673
+ await nvim . call ( "nvim_open_win" , [
674
+ bufnr ,
675
+ true ,
676
+ {
677
+ relative : "editor" ,
678
+ width,
679
+ height,
680
+ col : Math . floor ( ( editorWidth - width ) / 2 ) ,
681
+ row : Math . floor ( ( editorHeight - height ) / 2 ) ,
682
+ style : "minimal" ,
683
+ border : "single" ,
684
+ } ,
685
+ ] ) ;
686
+
687
+ const lines = JSON . stringify ( params , null , 2 ) . split ( "\n" ) ;
688
+ lines . push ( `nTokens: ${ nTokens } ` ) ;
689
+ await nvim . call ( "nvim_buf_set_lines" , [ bufnr , 0 , - 1 , false , lines ] ) ;
690
+
691
+ // Set buffer options
692
+ await nvim . call ( "nvim_buf_set_option" , [ bufnr , "modifiable" , false ] ) ;
693
+ await nvim . call ( "nvim_buf_set_option" , [ bufnr , "filetype" , "json" ] ) ;
694
+ }
695
+
645
696
return {
646
697
initModel,
647
698
update,
0 commit comments