§
    ÚŽ’jz(  ã                   ó¦   — d Z ddlZddlZddlmZmZmZ ddlmZm	Z	m
Z
 ddlmZmZ ddlmZ d	d
lmZmZ d	dlmZmZmZmZ dZ G d„ de¦  «        ZdS )z"The PowerShell tool in agentscope.é    N)ÚAsyncGeneratorÚAnyÚListé   )ÚBackendBaseÚLocalBackendÚ_normalize_newlinesé   )ÚToolBaseÚToolMiddlewareBase)Ú	ToolChunké   )Ú	TextBlockÚToolResultState)ÚPermissionBehaviorÚPermissionContextÚPermissionDecisionÚPermissionRule)Úpwshúpowershell.exec                   ó°  ‡ — e Zd ZU dZd Zeed<   	 dZeed<   	 ddddœdd	dœd
dddddœdœdgdœZe	ee
f         ed<   dZeed<   dZeed<   dZeed<   dZeed<   dZeed<   	 	 	 d(deej        e         z  dz  dee         dz  dedz  ddfˆ fd„Zdefd „Zd!e	ee
f         d"edefd#„Zd!e	ee
f         dee         fd$„Z	 	 d)deded&edeedf         fd'„Z ˆ xZ!S )*Ú
PowerShellz8Execute PowerShell commands through a workspace backend.Únamea<  Executes a PowerShell command and returns its output.

Each command starts in the configured working directory, but PowerShell
session state does not persist between commands. Commands run without
loading the user's PowerShell profile.

IMPORTANT: Avoid using this tool for filesystem operations when a dedicated
tool can accomplish the task. Prefer the dedicated tools because their calls
are easier for the user to review and authorize:

 - File search: Use Glob (NOT Get-ChildItem)
 - Content search: Use Grep (NOT Select-String)
 - Read files: Use Read (NOT Get-Content)
 - Edit files: Use Edit
 - Write files: Use Write (NOT Set-Content or Out-File)
 - Communication: Output text directly (NOT Write-Output)

# Instructions
 - Verify the parent directory before creating new directories or files.
 - Quote paths containing spaces, for example: Get-Item "path with spaces".
 - Prefer absolute paths and avoid Set-Location so the working directory is
   clear in every command.
 - You may specify an optional timeout in milliseconds (up to 600000ms /
   10 minutes). The default timeout is 120000ms (2 minutes).
 - Write a concise description of what the command does. Include more context
   for pipelines, uncommon parameters, or commands with side effects.
 - Run independent commands in parallel tool calls. Keep dependent commands
   together and use PowerShell-native error handling when later work depends
   on earlier work succeeding.
 - Prefer new git commits over amending existing commits. Avoid destructive
   git operations and never bypass hooks unless the user explicitly asks.ÚdescriptionÚobjectÚstringz"The PowerShell command to execute.)Útyper   z5Clear, concise description of what this command does.Úintegerz?Optional timeout in milliseconds (default: 120000, max: 600000)éÀÔ éÀ'	 r   )r   r   ÚdefaultÚmaximumÚminimum)Úcommandr   Útimeoutr$   )r   Ú
propertiesÚrequiredÚinput_schemaFÚis_mcpÚis_read_onlyÚis_concurrency_safeÚis_external_toolÚis_state_injectedNÚcwdÚmiddlewaresÚbackendÚreturnc                 ó¾   •— t          ¦   «                              |¬¦  «         |�t          j        |¦  «        nd| _        |pt          ¦   «         | _        d| _        dS )aÙ  Initialize the PowerShell tool.

        Args:
            cwd (`str | os.PathLike[str] | None`, optional):
                Working directory used when executing commands.
            middlewares (`List[ToolMiddlewareBase] | None`, optional):
                Tool middlewares wrapping command execution.
            backend (`BackendBase | None`, optional):
                Backend used for subprocess execution. Defaults to the
                host-local backend.
        )r/   N)ÚsuperÚ__init__ÚosÚfspathÚ_cwdr   Ú_backendÚ_executable)Úselfr.   r/   r0   Ú	__class__s       €úRC:\Projects\DSCons\.venv\Lib\site-packages\agentscope\tool\_builtin\_powershell.pyr4   zPowerShell.__init___   sU   ø€ õ" 	‰Œ×Ò [ÐÑ1Ô1Ð1Ø&) o•B”I˜c‘N”N�N¸4ˆŒ	ØÐ1¥<¡>¤>ˆŒØ'+ˆÔÐÐó    c           	   ƒ   ó¼   K  — | j         €Mt          D ]>}| j                             |dddddgd¬¦  «        ƒ d{V —†}|j        d	k    r	|| _          nŒ?d
| _         | j         S )z>Prefer PowerShell 6+ and cache the first available executable.Nú-NoLogoú
-NoProfileú-NonInteractivez-Commandzexit 0g      $@)r%   é   r   )r9   Ú_SHELL_CANDIDATESr8   Ú
exec_shellÚ	exit_code)r:   Ú	candidateÚprobes      r<   Ú_resolve_executablezPowerShell._resolve_executableu   s¨   è è € àÔÐ#Ý.ð 4ð 4�	Ø"œm×6Ò6à!Ø!Ø$Ø)Ø"Ø ðð !ð 7ñ 
ô 
ð 
ð 
ð 
ð 
ð 
ð 
�ð ”? cÒ)Ð)Ø'0�DÔ$Ø�Eð *ð $4�Ô ØÔÐr=   Ú
tool_inputÚcontextc              ƒ   ó>   K  — t          t          j        dd¬¦  «        S )aF  Ask the user to confirm every PowerShell command.

        PowerShell-specific command validation is intentionally outside this
        implementation. Since no command is classified as safe, every
        invocation prompts the user. This is a regular ASK that allow rules
        and BYPASS mode may still override.
        zExecute PowerShell commandz,PowerShell command validation is not enabled)ÚbehaviorÚmessageÚdecision_reason)r   r   ÚASK)r:   rI   rJ   s      r<   Úcheck_permissionszPowerShell.check_permissions‹   s+   è è € õ "Ý'Ô+Ø0ØJð
ñ 
ô 
ð 	
r=   c              ƒ   ó
   K  — g S )zÀReturn no automatic allow-rule suggestions.

        A broad rule would weaken the conservative permission boundary before
        PowerShell-specific command validation is available.
        © )r:   rI   s     r<   Úgenerate_suggestionszPowerShell.generate_suggestions�   s   è è € ð ˆ	r=   Ú r%   c           	     óD  K  — t          |d¦  «        }t          j        |                     d¦  «        ¦  «                             d¦  «        }d|› d�}t          j        |                     d¦  «        ¦  «                             d¦  «        }	 |                      ¦   «         ƒ d{V —†}| j                             |ddd	d
|g| j        |dz  ¬¦  «        ƒ d{V —†}	nL# t          $ r?}
t          t          d|› d|
› �¬¦  «        gt          j        d¬¦  «        W V — Y d}
~
dS d}
~
ww xY wt          |	j                             dd¬¦  «        ¦  «        }t          |	j                             dd¬¦  «        ¦  «        }|	j        dk    rA|	j        dk    r6t          t          d|› d|› �¬¦  «        gt          j        d¬¦  «        W V — dS |	                     ¦   «         sjd|› d�}|r|d|› �z  }|r|d|› �z  }t'          |¦  «        dk    r|dd…         dz   }t          t          |¬¦  «        gt          j        d¬¦  «        W V — dS |}|r|r|dz  }||z  }t'          |¦  «        dk    r|dd…         dz   }t          t          |¬¦  «        gt          j        d¬¦  «        W V — dS )aÓ  Execute a PowerShell command through the configured backend.

        Args:
            command (`str`):
                PowerShell source text to execute.
            description (`str`, optional):
                Human-readable description of the command.
            timeout (`int`, optional):
                Timeout in milliseconds, capped at 600000.

        Yields:
            `ToolChunk`:
                A final chunk containing the command output.
        r    z	utf-16-leÚasciia  $ProgressPreference = [System.Management.Automation.ActionPreference]::SilentlyContinue
$OutputEncoding = [Console]::OutputEncoding = [System.Text.UTF8Encoding]::new($false)
$AgentScopeCommand = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String('z1'))
& ([ScriptBlock]::Create($AgentScopeCommand))Nr?   r@   rA   z-EncodedCommandg     @�@)r.   r%   zCommand failed: z
Error: )ÚtextT)ÚcontentÚstateÚis_lastzutf-8Úreplace)Úerrorséÿÿÿÿs	   timed outzCommand timed out after zms: ú
z	
Stdout:
z	
Stderr:
i0u  z
... (output truncated))ÚminÚbase64Ú	b64encodeÚencodeÚdecoderH   r8   rD   r7   Ú	Exceptionr   r   r   ÚERRORr	   ÚstdoutÚstderrrE   ÚokÚlenÚRUNNING)r:   r$   r   r%   Ú
timeout_msÚencoded_user_commandÚpowershell_scriptÚencoded_commandÚ
executableÚresultÚexcrf   rg   Úerror_resultÚoutputs                  r<   ÚcallzPowerShell.call¨   sæ  è è € õ( ˜ &Ñ)Ô)ˆ
Ý%Ô/Ø�NŠN˜;Ñ'Ô'ñ 
ô  
ç
Š&�‰/Œ/ð 	ð<ð %ð<ð <ð <ð 	õ !Ô*Ø×$Ò$ [Ñ1Ô1ñ
ô 
ç
Š&�‰/Œ/ð 	ð	Ø#×7Ò7Ñ9Ô9Ð9Ð9Ð9Ð9Ð9Ð9ˆJØœ=×3Ò3àØØ Ø%Ø%Ø#ðð ”IØ" VÑ+ð 4ñ ô ð ð ð ð ð ð ˆFˆFøõ ð 
	ð 
	ð 
	ÝåØG°ÐGÐGÀ#ÐGÐGðñ ô ðõ
 &Ô+Øðñ ô ð ð ð ð ð ˆFˆFˆFˆFˆFøøøøð
	øøøõ %ØŒM× Ò  °Ð Ñ;Ô;ñ
ô 
ˆõ %ØŒM× Ò  °Ð Ñ;Ô;ñ
ô 
ˆð Ô˜rÒ!Ð! f¤m°|Ò&CÐ&CÝåð)°zð )ð )Ø&ð)ð )ðñ ô ðõ &Ô+Øðñ ô ð ð ð ð ð ˆFà�yŠy‰{Œ{ð 	Ø9¨gÐ9Ð9Ð9ˆLØð 7ØÐ 6¨fÐ 6Ð 6Ñ6�Øð 7ØÐ 6¨fÐ 6Ð 6Ñ6�Ý�<Ñ Ô  5Ò(Ð(à   % Ô(Ð+EÑEð õ Ý"¨Ð5Ñ5Ô5Ð6Ý%Ô+Øðñ ô ð ð ð ð ð
 ˆFàˆØð 	Øð Ø˜$‘�Ø�fÑˆFÝˆv‰;Œ;˜ÒÐØ˜F˜U˜F”^Ð&@Ñ@ˆFÝÝ FÐ+Ñ+Ô+Ð,Ý!Ô)Øð
ñ 
ô 
ð 	
ð 	
ð 	
ð 	
ð 	
ð 	
s   ÂAC Ã
D#Ã$4DÄD#)NNN)rT   r   )"Ú__name__Ú
__module__Ú__qualname__Ú__doc__r   ÚstrÚ__annotations__r   r(   Údictr   r)   Úboolr*   r+   r,   r-   r5   ÚPathLiker   r   r   r4   rH   r   r   rP   r   rS   Úintr   r   rt   Ú__classcell__)r;   s   @r<   r   r      s†  ø€ € € € € € ØBÐBà€Dˆ#ÐÐÑØ/ðM€K�ð Mð Mñ Mð> 2ð ð !ØCðð ð
 !àKðð ð "ð5ð "Ø!Øð	ð 	ð
ð 
ð, �Kð1$ð $€L�$�s˜C�x”.ð ð ñ ð6 €FˆDÐÐÑØ€L�$ÐÐÑØ %Ð˜Ð%Ð%Ñ%Ø"Ð�dÐ"Ð"Ñ"Ø#Ð�tÐ#Ð#Ñ#ð .2Ø7;Ø&*ð	,ð ,à�2”;˜sÔ#Ñ# dÑ*ð,ð Ð,Ô-°Ñ4ð,ð ˜tÑ#ð	,ð
 
ð,ð ,ð ,ð ,ð ,ð ,ð, ¨3ð  ð  ð  ð  ð,
à˜˜c˜”Nð
ð #ð
ð 
ð	
ð 
ð 
ð 
ð$	à˜˜c˜”Nð	ð 
ˆnÔ	ð	ð 	ð 	ð 	ð Øð	q
ð q
àðq
ð ðq
ð ð	q
ð
 
˜	 4˜Ô	(ðq
ð q
ð q
ð q
ð q
ð q
ð q
ð q
r=   r   )rx   r`   r5   Útypingr   r   r   r8   r   r   r	   Ú_baser   r   Ú	_responser   rM   r   r   Ú
permissionr   r   r   r   rC   r   rR   r=   r<   ú<module>r„      s  ðà (Ð (à €€€Ø 	€	€	€	Ø ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,Ð ,à DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DÐ DØ 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ð 0Ø !Ð !Ð !Ð !Ð !Ð !Ø 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1Ð 1ðð ð ð ð ð ð ð ð ð ð ð ð /Ð ðB
ð B
ð B
ð B
ð B
�ñ B
ô B
ð B
ð B
ð B
r=   