@@ -32,3 +32,90 @@ export function truncateList(
3232
3333 return truncated . join ( separator ) ;
3434}
35+
36+ function parseToolResult < TResult > (
37+ result : string | Partial < TResult > | undefined ,
38+ defaults : TResult ,
39+ stringParser ?: ( str : string ) => Partial < TResult > ,
40+ ) : TResult {
41+ if ( ! result ) {
42+ return defaults ;
43+ }
44+
45+ if ( typeof result === "string" ) {
46+ const stringResult =
47+ stringParser ?.( result ) ??
48+ ( { stdout : result } as unknown as Partial < TResult > ) ;
49+ return { ...defaults , ...stringResult } ;
50+ }
51+
52+ return { ...defaults , ...result } ;
53+ }
54+
55+ export interface ShellResult {
56+ stdout : string ;
57+ stderr : string ;
58+ exitCode ?: number ;
59+ }
60+
61+ export function parseShellResult (
62+ result : string | Partial < ShellResult > | undefined ,
63+ ) : ShellResult {
64+ return parseToolResult ( result , { stdout : "" , stderr : "" } ) ;
65+ }
66+
67+ export interface ShellStatusResult {
68+ stdout : string ;
69+ stderr : string ;
70+ status ?: string ;
71+ }
72+
73+ export function parseShellStatusResult (
74+ result : string | Partial < ShellStatusResult > | undefined ,
75+ ) : ShellStatusResult {
76+ return parseToolResult ( result , { stdout : "" , stderr : "" } ) ;
77+ }
78+
79+ export interface KillShellResult {
80+ success : boolean ;
81+ message : string ;
82+ }
83+
84+ export function parseKillShellResult (
85+ result : string | Partial < KillShellResult > | undefined ,
86+ ) : KillShellResult {
87+ return parseToolResult ( result , { success : false , message : "" } , ( str ) => ( {
88+ success : str . includes ( "killed" ) || str . includes ( "terminated" ) ,
89+ message : str ,
90+ } ) ) ;
91+ }
92+
93+ export interface GrepResultParsed {
94+ matches : string [ ] ;
95+ count ?: number ;
96+ }
97+
98+ export function parseGrepResult (
99+ result : string | Partial < GrepResultParsed > | undefined ,
100+ ) : GrepResultParsed {
101+ return parseToolResult ( result , { matches : [ ] } , ( str ) => ( {
102+ matches : parseStringListResult ( str ) ,
103+ } ) ) ;
104+ }
105+
106+ export function parseWebSearchResult < T > (
107+ result : string | { results ?: T [ ] } | undefined ,
108+ ) : T [ ] {
109+ if ( ! result ) return [ ] ;
110+
111+ if ( typeof result === "string" ) {
112+ try {
113+ const parsed = JSON . parse ( result ) ;
114+ return parsed . results || [ ] ;
115+ } catch {
116+ return [ ] ;
117+ }
118+ }
119+
120+ return result . results || [ ] ;
121+ }
0 commit comments