@@ -8,19 +8,57 @@ namespace AIShell.Integration.Commands;
88[ Cmdlet ( VerbsLifecycle . Invoke , "AIShell" , DefaultParameterSetName = "Default" ) ]
99public class InvokeAIShellCommand : PSCmdlet
1010{
11- [ Parameter ( Mandatory = true , ValueFromRemainingArguments = true ) ]
11+ private const string DefaultSet = "Default" ;
12+ private const string ClipboardSet = "Clipboard" ;
13+ private const string PostCodeSet = "PostCode" ;
14+ private const string CopyCodeSet = "CopyCode" ;
15+ private const string ExitSet = "Exit" ;
16+
17+ /// <summary>
18+ /// Sets and gets the query to be sent to AIShell
19+ /// </summary>
20+ [ Parameter ( Mandatory = true , ValueFromRemainingArguments = true , ParameterSetName = DefaultSet ) ]
21+ [ Parameter ( Mandatory = true , ValueFromRemainingArguments = true , ParameterSetName = ClipboardSet ) ]
1222 public string [ ] Query { get ; set ; }
1323
14- [ Parameter ]
24+ /// <summary>
25+ /// Sets and gets the agent to use for the query.
26+ /// </summary>
27+ [ Parameter ( ParameterSetName = DefaultSet ) ]
28+ [ Parameter ( ParameterSetName = ClipboardSet ) ]
1529 [ ValidateNotNullOrEmpty ]
1630 public string Agent { get ; set ; }
1731
18- [ Parameter ( ParameterSetName = "Default" , Mandatory = false , ValueFromPipeline = true ) ]
32+ /// <summary>
33+ /// Sets and gets the context information for the query.
34+ /// </summary>
35+ [ Parameter ( ValueFromPipeline = true , ParameterSetName = DefaultSet ) ]
1936 public PSObject Context { get ; set ; }
2037
21- [ Parameter ( ParameterSetName = "Clipboard" , Mandatory = true ) ]
38+ /// <summary>
39+ /// Indicates getting context information from clipboard.
40+ /// </summary>
41+ [ Parameter ( Mandatory = true , ParameterSetName = ClipboardSet ) ]
2242 public SwitchParameter ContextFromClipboard { get ; set ; }
2343
44+ /// <summary>
45+ /// Indicates running '/code post' from the AIShell.
46+ /// </summary>
47+ [ Parameter ( ParameterSetName = PostCodeSet ) ]
48+ public SwitchParameter PostCode { get ; set ; }
49+
50+ /// <summary>
51+ /// Indicates running '/code copy' from the AIShell.
52+ /// </summary>
53+ [ Parameter ( ParameterSetName = CopyCodeSet ) ]
54+ public SwitchParameter CopyCode { get ; set ; }
55+
56+ /// <summary>
57+ /// Indicates running '/exit' from the AIShell.
58+ /// </summary>
59+ [ Parameter ( ParameterSetName = ExitSet ) ]
60+ public SwitchParameter Exit { get ; set ; }
61+
2462 private List < PSObject > _contextObjects ;
2563
2664 protected override void ProcessRecord ( )
@@ -36,25 +74,43 @@ protected override void ProcessRecord()
3674
3775 protected override void EndProcessing ( )
3876 {
39- Collection < string > results = null ;
40- if ( _contextObjects is not null )
41- {
42- using PowerShell pwsh = PowerShell . Create ( RunspaceMode . CurrentRunspace ) ;
43- results = pwsh
44- . AddCommand ( "Out-String" )
45- . AddParameter ( "InputObject" , _contextObjects )
46- . Invoke < string > ( ) ;
47- }
48- else if ( ContextFromClipboard )
77+ string message , context = null ;
78+
79+ switch ( ParameterSetName )
4980 {
50- using PowerShell pwsh = PowerShell . Create ( RunspaceMode . CurrentRunspace ) ;
51- results = pwsh
52- . AddCommand ( "Get-Clipboard" )
53- . AddParameter ( "Raw" )
54- . Invoke < string > ( ) ;
81+ case PostCodeSet :
82+ message = "/code post" ;
83+ break ;
84+ case CopyCodeSet :
85+ message = "/code copy" ;
86+ break ;
87+ case ExitSet :
88+ message = "/exit" ;
89+ break ;
90+ default :
91+ Collection < string > results = null ;
92+ if ( _contextObjects is not null )
93+ {
94+ using PowerShell pwsh = PowerShell . Create ( RunspaceMode . CurrentRunspace ) ;
95+ results = pwsh
96+ . AddCommand ( "Out-String" )
97+ . AddParameter ( "InputObject" , _contextObjects )
98+ . Invoke < string > ( ) ;
99+ }
100+ else if ( ContextFromClipboard )
101+ {
102+ using PowerShell pwsh = PowerShell . Create ( RunspaceMode . CurrentRunspace ) ;
103+ results = pwsh
104+ . AddCommand ( "Get-Clipboard" )
105+ . AddParameter ( "Raw" )
106+ . Invoke < string > ( ) ;
107+ }
108+
109+ context = results ? . Count > 0 ? results [ 0 ] : null ;
110+ message = string . Join ( ' ' , Query ) ;
111+ break ;
55112 }
56113
57- string context = results ? . Count > 0 ? results [ 0 ] : null ;
58- Channel . Singleton . PostQuery ( new PostQueryMessage ( string . Join ( ' ' , Query ) , context , Agent ) ) ;
114+ Channel . Singleton . PostQuery ( new PostQueryMessage ( message , context , Agent ) ) ;
59115 }
60116}
0 commit comments