
    h^0                         S SK r S SKrS SKrS SKrSqSqSqSrSrSr	Sq
S rS
S jrS rS rS rS r\S	:X  a  \" 5         gg)    NTc                      [         R                  R                  S5      (       a  [        SS5       n [        R
                  " U 5      nUR                  S5      qUR                  SS5      nU(       a%  UR                  S5      n[        U5      S:  a  US   q
[        S	[         35        [        (       a  [        S
[         35        SSS5        O [        (       d   [        R                  " / SQSSSS9nUR                  S:X  a]  [        R                   " UR"                  5      R                  S/ 5      nU(       a&  US   R                  S5      q[        S[         35        [        (       d   [        R                  " / SQSSSS9nUR                  S:X  aL  UR"                  R%                  5       (       a-  UR"                  R%                  5       q
[        S[         35        OSq
[        S[         35          [        R                  " / SQSSSS9nUR                  S:X  a  [        R                   " UR"                  5      R                  S/ 5      nU(       a  US   R                  S5      n[        R                  " SSS S!US"S#S$S%/	SSSS9nUR                  S:X  av  [        R                   " UR"                  5      R                  S&/ 5      n	U	(       a?  U	S   R                  S'5      qU	S   R                  S(S)5      n
[        S*U
 S+[&         S,35        [&        (       d5  S.[         R(                  R                  S/S05      -   q[        S1[&         35        [        (       d8  [        S25        [        S35        [        S45        [        S55        [        S65        g7g! , (       d  f       GN= f! [         a  n[        SU 35         SnAGNSnAff = f! [         a  n[        SU 35         SnAGNSnAff = f! [         a  nSq
[        S[         35         SnAGN!SnAff = f! [         a  n[        S-U 35         SnAGN,SnAff = f)8zDAuto-detect Q Business parameters from AWS CLI and local state filesz/tmp/qbusiness-app.jsonrapplicationIdapplicationArn :      u   ✓ Found Application ID: u&   ✓ Detected region from application: Nz1Warning: Could not read /tmp/qbusiness-app.json: )aws	qbusinesszlist-applications--max-results1T
   capture_outputtexttimeoutr   applicationsu'   ✓ Found Application ID from AWS CLI: z&Warning: Could not list applications: )r   	configuregetregion   u   ✓ Using AWS region: z	us-west-2u   ✓ Using default region: z.Warning: Could not get region, using default: )r   z	sso-adminzlist-instances--region
eu-north-1	InstancesIdentityStoreIdr   identitystorez
list-usersz--identity-store-idr   r   r   r   UsersUserIdUserNameunknownu    ✓ Found Identity Center user: z ()z-Warning: Could not get Identity Center user: z	cli-user-USERdefaultu   ✓ Using generated user ID: u5   
⚠ ERROR: Could not find Q Business Application ID!zPlease ensure:z'  1. Q Business application is deployedz'  2. /tmp/qbusiness-app.json exists, orz8  3. You have AWS CLI configured with proper credentialsF)ospathexistsopenjsonloadr   APPLICATION_IDsplitlenREGIONprint	Exception
subprocessrun
returncodeloadsstdoutstripUSER_IDenviron)fapp_dataapp_arn	arn_partseresultapps	instancesidentity_store_idusersusernames              N/root/racknerd_01/codex-conversation/amazon-q-terraform/q_business_chat_cli.pyauto_detect_parametersrE      s   
G77>>344/599Q<!)o!>",,'7< 'c 2I9~*!*12>2BCD6B6(KL 65 5" >	@^^O#$F   A%zz&--044^RH%)!W[[%ANCNCSTU
 6	M^^5#$F   A%&--*=*=*?*?,,..vh78$26(;<CLdB
 !

6==155k2FI$-aL$4$45F$G! $O\*,=E $(dB	 $$) JJv}}599'2FE"'(,,x"8#(8<<
I#F @
"WIUVWX
 7

vy AA-gY78 >FG7878HIs 65  GA!EFFG  	@:1#>??	@   	M FB6(KLL	M:  C=aSABBCs   0N* BNN* BO /A4O6 $O6 :DP" 
N'"N* 'N* *
O4OO
O3O..O36
P PP"
Q,Q  Qc                    U (       a  U q U(       a  UqU(       a  UqUb  UO[        nU(       a  [	        S5        S[         0n[
        (       a)  [        (       a  [
        US'   [        US'   [	        S5        O[	        S5        [        R                  " S0 UD6nUR                  S5      q
 UR                  S	5      nUR                  5       n	[	        S
U	S    35        [        $ U(       dA  [        R                  R                  [        R                  R!                  S5      5      (       aQ   [        R                  " U=(       d    S[         S9nUR                  S5      q
[	        SU=(       d    S 35        [        $ [        R                  " S[         S9q
[        $ ! [         a  n
[	        SU
 35         Sn
A
[        $ Sn
A
ff = f! [         a  n
[	        S5         Sn
A
NcSn
A
ff = f)z3Initialize the Q Business client with configurationNu*   ✓ Using IAM authentication (access keys)region_nameaws_access_key_idaws_secret_access_keyu+   ✓ Using explicitly configured access keysu<   ✓ Using credentials from environment or ~/.aws/credentialsr   stsu   ✓ Authenticated as: Arnu(   ⚠ Warning: Could not verify identity: z~/.aws/configqbusiness-sso)profile_namerG   u   ✓ Using AWS profile: zDNote: Could not use SSO profile, falling back to default credentials)rG    )r.   r+   r7   USE_IAM_AUTHr/   AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYboto3Sessionclientqbusiness_clientget_caller_identityr0   r%   r&   r'   
expanduser)r   application_iduser_idprofileuse_iam	auth_modesession_kwargssessionrJ   identityr=   s              rD   initialize_clientr`   u   s    ' #.LI:; (0!6!62CN./6KN23?@ PQ--1.1">>+6	B..'C..0H*8E?*;<=   bggnnRWW%7%7%HII_--W5O]cd#*>>+#> /0J?/KLM''
 !<<H#  	B<QC@AA	B  _\^^_s1   (2F )AG 
F?"F::F?
G!GG!c                    [         c
  [        5         [        U S.n[        (       a&  [        (       a  [        US'   [        S[         35        U(       a  XS'    [         R                  " S0 UD6nU$ ! [         ak  n[        SU 35        [        S[        R                  " UR                  5        VVs0 s H  u  pVUS:w  d  M  XV_M     Os  snnf snnS	S
9 35         SnAgSnAff = f)u6   
向 Amazon Q Business 发送消息并获取响应。
N)r   userMessageuserIdz[DEBUG] Using userId: conversationIdz,Error communicating with Amazon Q Business: z[DEBUG] Request params: rb      )indentrN   )rU   r`   r+   rO   r7   r/   	chat_syncr0   r)   dumpsitems)message_textcurrent_session_idrequest_paramsresponser=   kvs          rD   send_message_to_qrp      s      (#N |#*x &wi01+='(#--?? <QC@A(nFZFZF\4sF\da`aer`rTQTF\4s|})~(  A  	Bs*   A0 0
C%:7C 1CCC  C%c                 X   U (       d  [        S5        gU R                  S5      nU(       a  [        SU 35        O5[        S5        [        S5        [        [        R                  " U SSS	95        S
U ;   a  U S
   (       a  [        S5        [	        U S
   5       H~  u  p#UR                  SS5      nUR                  SS5      nUR                  SS5      n[        SUS-    SU 35        US:w  a  [        SU 35        U(       d  Ml  [        SUSS  S35        M     U R                  S5      $ )z"Print the response from Q Business0Could not get a response from Amazon Q Business.NsystemMessagez
Amazon Q: z)
Amazon Q: (No textual response received)z'Full Q Business response for debugging:re   F)rf   ensure_asciisourceAttributionsz	
Sources:titlezN/Aurlsnippetr   z     z. z
     URL: z     Snippet: d   z...rd   )r/   r   r)   rh   	enumerate)rm   system_messageisourcerv   rw   rx   s          rD   print_responser      s   @A \\/2N^,-.:;78djj!%@A x'H5I,Jl"8,@#ABIAJJw.E**UE*CjjB/GBqse2eW%&e|
3%()wwt}oS9: C <<())    c                  b     [         R                  " / SQSSSS9n U R                  S:H  $ !    g= f)zCheck if SSO is logged in)r   rJ   zget-caller-identityz	--profilerL   Tr   r   r   F)r1   r2   r3   )r>   s    rD   check_sso_loginr      s<    OdA
   A%%s   '* .c                     [        S5        [        S5        [        S5        [        S5        [        5       (       d  [        S5        g[        S5        [        S[         35        [        S[         35        [        S	[         35        [        S
[
        (       a  SOS 35        [        S5        [
        (       d  [        5       (       dx  [        S5        [        S5        [        S5        [        S5        [        S5        [        S5        [        S5      n U R                  5       S:w  a  [        S5        g[        S5        OB[        S5        [        S5        [        S5        [        S5        [        S5        [        S5        [        5         Sn[        S5        [        S5          [        S5      nUR                  5       S;   a  [        S 5        gUR                  5       (       d  MD  [        X!5      n U (       a  S!U ;   a  U S!   n[        U 5        O[        S"5         Mz  ! [         a    [        S#5         g[         a    [        S$5         g[         a  n[        S%U 35         SnANKSnAff = f)&zMain interactive chat loopz<============================================================zAmazon Q Business CLI Chatz-
Initializing and detecting configuration...
z!
Failed to initialize. Exiting...Nz
Configuration Summary:z  Application ID: z
  Region: z  User ID: z  Auth Mode: zIAM (Access Keys)zSSO (Identity Center)u#   
⚠ WARNING: SSO is not logged in!zHFor Identity Center enabled Q Business apps, you need to login with SSO.z
To login, run:z'  aws sso login --profile qbusiness-ssoz1
Or visit: https://d-c367615483.awsapps.com/startz=
============================================================z(
Do you want to continue anyway? (y/N): yz&Exiting... Please login and try again.z3
Continuing with default credentials (may fail)...
u6   
✓ Using IAM authentication - SSO login not requiredz  Ensure you have:zB  - AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY in environment, orz/  - Valid credentials in ~/.aws/credentials, orz(  - Access keys configured in the scriptu&   
✓ Ready! Type your questions below.z/Type 'exit' or 'quit' to end the conversation.
z
You: )exitquitz
Ending conversation. Goodbye!rd   rr   z

Interrupted by user. Goodbye!z
End of input. Goodbye!z
Unexpected error: )r/   rE   r+   r.   r7   rO   r   inputlowerr`   r6   rp   r   KeyboardInterruptEOFErrorr0   )rm   
session_id
user_inputr=   s       rD   mainr      s   	(O	
&'	(O	
;< "##23	$&	~.
/0	Jvh
 	Ky
!"	M-CZ[
\]	(O <  89\]$%;<FG/"HIH~~3&>?IJGH"#RS?@89hJ	
34	
<=
	.y)J!%5578##%%(@H#x/!)*:!;Jx(HI' * ! 	56 	,- 	.(,--	.s6   (*H! H! *(H! H! !I/8I/	I/I**I/__main__)NNNNN)rR   r)   r%   r1   r+   r.   r7   rO   rP   rQ   rU   rE   r`   rp   r   r   r   __name__rN   r   rD   <module>r      ss      	  	
    `F6 r<*B	L.^ zF r   