11use std:: collections:: BTreeMap ;
22
3+ use crate :: commands:: token_budget;
34use crate :: config:: Context ;
45use crate :: db;
56use crate :: graph:: code_graph;
@@ -10,6 +11,10 @@ use serde::Serialize;
1011
1112const GRAPH_BACKEND_HINT : & str =
1213 "Graph commands require a configured FalkorDB graph backend and synced graph projection." ;
14+ const USAGES_TOKEN_BUDGET_REFINE_HINT : & str =
15+ "`--limit`, `--offset`, or a more specific symbol query" ;
16+ const BLAST_RADIUS_TOKEN_BUDGET_REFINE_HINT : & str =
17+ "`--depth`, a more specific symbol query, or a symbol UUID" ;
1318
1419fn hint_for ( ctx : & Context ) -> Option < String > {
1520 if ctx. falkordb . is_none ( ) {
@@ -37,6 +42,12 @@ fn print_graph_hint_text(ctx: &Context, error: Option<&anyhow::Error>) {
3742 }
3843}
3944
45+ fn print_hint_text ( hint : Option < & str > ) {
46+ if let Some ( hint) = hint {
47+ eprintln ! ( "Hint: {hint}" ) ;
48+ }
49+ }
50+
4051fn graph_read_unavailable ( error : & anyhow:: Error ) -> bool {
4152 matches ! (
4253 error. downcast_ref:: <code_graph:: GraphReadError >( ) ,
@@ -429,6 +440,7 @@ pub fn usages(
429440 symbol_name : & str ,
430441 limit : usize ,
431442 offset : usize ,
443+ token_budget : Option < usize > ,
432444 format : Format ,
433445) -> anyhow:: Result < ( ) > {
434446 let Some ( ( symbol, total, results) ) = read_paged_symbol_graph_results (
@@ -443,6 +455,15 @@ pub fn usages(
443455 else {
444456 return Ok ( ( ) ) ;
445457 } ;
458+ let unbudgeted_result_count = results. len ( ) ;
459+ let budgeted = token_budget:: trim_results (
460+ results,
461+ token_budget,
462+ USAGES_TOKEN_BUDGET_REFINE_HINT ,
463+ |result| format_usage_result_line ( result, & symbol. display_name ) ,
464+ ) ;
465+ let results = budgeted. results ;
466+ let hint = token_budget:: combine_hints ( hint_for ( ctx) , budgeted. hint ) ;
446467
447468 match format {
448469 Format :: Json => output:: print_json ( & PagedResponse {
@@ -451,18 +472,21 @@ pub fn usages(
451472 offset,
452473 limit,
453474 results,
454- hint : hint_for ( ctx ) ,
475+ hint,
455476 } ) ,
456477 Format :: Text => {
457- if results . is_empty ( ) && offset == 0 {
478+ if unbudgeted_result_count == 0 && offset == 0 {
458479 output:: print_text ( & format ! ( "No usages found for '{}'" , symbol. display_name) ) ?;
459480 print_graph_hint_text ( ctx, None ) ;
460- } else if results . is_empty ( ) {
481+ } else if unbudgeted_result_count == 0 {
461482 eprintln ! ( "No usages at offset {offset} (total {total})" ) ;
483+ } else if results. is_empty ( ) {
484+ print_hint_text ( hint. as_deref ( ) ) ;
462485 } else {
463486 output:: print_text ( & format_grouped_graph_results ( & results, |r| {
464487 format_usage_result_line ( r, & symbol. display_name )
465488 } ) ) ?;
489+ print_hint_text ( hint. as_deref ( ) ) ;
466490 if total > offset + results. len ( ) {
467491 eprintln ! (
468492 "-- {} of {} results (use --offset {} for more)" ,
@@ -541,6 +565,7 @@ pub fn blast_radius(
541565 ctx : & Context ,
542566 target : & str ,
543567 depth : usize ,
568+ token_budget : Option < usize > ,
544569 format : Format ,
545570) -> anyhow:: Result < ( ) > {
546571 let Some ( ( ) ) =
@@ -560,26 +585,37 @@ pub fn blast_radius(
560585 return Ok ( ( ) ) ;
561586 } ;
562587 let total = results. len ( ) ;
588+ let budgeted = token_budget:: trim_results (
589+ results,
590+ token_budget,
591+ BLAST_RADIUS_TOKEN_BUDGET_REFINE_HINT ,
592+ format_blast_radius_result_line,
593+ ) ;
594+ let results = budgeted. results ;
595+ let hint = token_budget:: combine_hints ( hint_for ( ctx) , budgeted. hint ) ;
563596 match format {
564597 Format :: Json => output:: print_json ( & PagedResponse {
565598 project_id : ctx. project_id . clone ( ) ,
566599 total,
567600 offset : 0 ,
568601 limit : total,
569602 results,
570- hint : hint_for ( ctx ) ,
603+ hint,
571604 } ) ,
572605 Format :: Text => {
573- if results . is_empty ( ) {
606+ if total == 0 {
574607 output:: print_text ( & format ! (
575608 "No blast radius found for '{}'" ,
576609 symbol. display_name
577610 ) ) ?;
578611 print_graph_hint_text ( ctx, None ) ;
612+ } else if results. is_empty ( ) {
613+ print_hint_text ( hint. as_deref ( ) ) ;
579614 } else {
580615 output:: print_text ( & format_grouped_graph_results ( & results, |r| {
581616 format_blast_radius_result_line ( r)
582617 } ) ) ?;
618+ print_hint_text ( hint. as_deref ( ) ) ;
583619 }
584620 Ok ( ( ) )
585621 }
0 commit comments