
    Ki                        d Z ddlmZ ddlmZ ddlmZ ddlmZm	Z	 ddl
mZmZ dd	lmZ 	 	 	 	 	 d	 	 	 	 	 	 	 	 	 	 	 ddZ	 	 	 	 d	 	 	 	 	 	 	 	 	 ddZy
)z#Tools 10, 11: list_flows, get_flow.    )annotations)Path)Any   )get_flow_by_id	get_flows)generate_hintsget_session   )
_get_storeNc                >   t        |       \  }}	 |s|n|dz  }t        |||      }|rJg }	|D ]>  }
|
j                  d      }||j                  |      }||k(  s.|	j	                  |
       @ |	d| }|dk(  r|D 
cg c]  }
|
d   |
d   |
d   d	 }}
d
dt        |       d|d}t        d|t                     |d<   ||j                          S c c}
w # t        $ r(}dt        |      dcY d}~|j                          S d}~ww xY w# |j                          w xY w)a'  List execution flows in the codebase, sorted by criticality.

    [EXPLORE] Retrieves stored execution flows from the knowledge graph.
    Each flow represents a call chain starting from an entry point
    (e.g. HTTP handler, CLI command, test function).

    Args:
        repo_root: Repository root path. Auto-detected if omitted.
        sort_by: Sort column: criticality, depth, node_count, file_count,
                 or name.
        limit: Maximum flows to return (default: 50).
        kind: Optional filter by entry point kind (e.g. "Test", "Function").
        detail_level: "standard" (default) returns full flow data;
                      "minimal" returns only name, criticality, and
                      node_count per flow.

    Returns:
        List of flows with criticality scores.
    
   sort_bylimitentry_point_idNminimalnamecriticality
node_count)r   r   r   okzFound z execution flow(s))statussummaryflows
list_flows_hintserrorr   r   )r   r   getget_node_kind_by_idappendlenr	   r
   close	Exceptionstr)	repo_rootr   r   kinddetail_levelstorerootfetch_limitr   filteredfep_id	node_kindresultexcs                  o/home/jay/workspace/scripts/.codegraph-venv/lib/python3.12/site-packages/code_review_graph/tools/flows_tools.pyr   r      s[   4 Y'KE4&E52: 	 %DH +./$ % 9 9% @I D( *+ Ve$E9$  	 fI#$]#3"#L/E  E
|+=>%

 *&+-
x  	+$  6!CH556 	sM   3C C !C <C.C C 	DD,D-D
 DD
 
Dc           
     T   t        |      \  }}	 d}| t        ||       }nK|It        |dd      }|D ]6  }|j                         |d   j                         v s't        ||d         } n |ddd	|j	                          S |rd
|v r|d
   D ]  }	|	j                  d      rt        |	d         nd}
|
|
j                         s||
z  }
|
}|s@|j                         sQ	 |j                  d      j                         t        d|	j                  d      xs ddz
        }t        t              |	j                  d      xs t                    }dj                  fdt        ||      D              |	d<    dd|d    d|d    d|d    d|d   d|d}t%        d|t'                     |d <   ||j	                          S # t         t"        f$ r	 d|	d<   Y Nw xY w# t(        $ r(}d!t+        |      d"cY d}~|j	                          S d}~ww xY w# |j	                          w xY w)#ah  Get details of a single execution flow.

    [EXPLORE] Retrieves full path details for a flow, including each step's
    function name, file, and line numbers.  Optionally includes source
    snippets for every step in the path.

    Args:
        flow_id: Database ID of the flow (from list_flows).
        flow_name: Name to search for (partial match). Ignored if flow_id
                   given.
        include_source: If True, include source code snippets for each step.
        repo_root: Repository root path. Auto-detected if omitted.

    Returns:
        Flow details with steps, or not_found status.
    Nr   i  r   r   id	not_foundz*No flow found matching the given criteria.)r   r   stepsfilereplace)errorsr   
line_startr   line_end
c              3  8   K   | ]  }|d z    d|      yw)r   z: N ).0iliness     r2   	<genexpr>zget_flow.<locals>.<genexpr>   s,      3 !  !1ugRaz23s   sourcez(could not read file)r   zFlow 'z': r   z nodes, depth depthz, criticality z.4f)r   r   flowget_flowr   r   r   )r   r   r   lowerr#   r   r   is_absoluteis_file	read_text
splitlinesmaxminr"   joinrangeOSErrorUnicodeDecodeErrorr	   r
   r$   r%   )flow_id	flow_nameinclude_sourcer&   r)   r*   rE   	all_flowsr-   stepfp	file_pathstartendr0   r1   rA   s                   @r2   rF   rF   Z   sr   , Y'KE4? !%1D"!}CI  ??$&	(99)%49D
 <%G^ 	S goW A+/88F+;T$v,'>"..*:B	!2!2!4A ) 3 3#, !4 !$*,  !$ 6 ;!q@! "J HHZ0>CJ *. 3%*5#%63 *X#A2 fc$|*<)= >g (#M2379 
 *
x  	' $%78 A)@XA   6!CH556 	sh   A	G! G! A
G! G! !BG;:G! GG! GG! !	H*H7H8H HH H')Nr   2   Nstandard)r&   
str | Noner   r%   r   intr'   r]   r(   r%   returndict[str, Any])NNFN)
rR   z
int | NonerS   r]   rT   boolr&   r]   r_   r`   )__doc__
__future__r   pathlibr   typingr   r   r   r   hintsr	   r
   _commonr   r   rF   r>       r2   <module>ri      s    ) "   - /  ! "AAA A 	A
 A AT    	VVV V 	V
 Vrh   