
    kKjT                    v   d Z ddlmZ ddlZddlmZ ddlmZ ddlm	Z	m
Z
 	 ddlZddlmZ ddlmZmZ dd	lmZ dd
lmZmZmZmZmZmZmZmZ ddlmZ ddlmZm Z  	  e
ejF                         e
d      k\  Z$ddl%Z%ddl&m'Z' 	 	 	 	 	 	 ddZ( G d de'      Z)y# e!$ rZ" e!d      e"dZ"["ww xY w# e	$ r dZ$Y ?w xY w)up  FormInput — a Provider that collects structured input from the user.

Define a Pydantic model for the data you need, and ``FormInput``
generates a form UI. The user fills it out, the submission is
validated, and an optional callback processes the result.

Requires ``fastmcp[apps]`` (prefab-ui).

Usage::

    from pydantic import BaseModel
    from fastmcp import FastMCP
    from fastmcp.apps.form import FormInput

    class ShippingAddress(BaseModel):
        street: str
        city: str
        state: str
        zip_code: str

    mcp = FastMCP("My Server")
    mcp.add_provider(FormInput(model=ShippingAddress))
    )annotationsN)Callable)Any)InvalidVersionVersion)SetState)CallToolSendMessage)	PrefabApp)H3CardCardContent
CardFooter
CardHeaderColumnFormMuted)If)RESULTSTATEzGFormInput requires prefab-ui. Install with: pip install 'fastmcp[apps]'z0.19.1F)
FastMCPAppc                    | j                   j                         D ]X  \  }}||v r|j                  t        u s|j                  t
        j                  j                  ur|j                  ||<   Td||<   Z |S )a  Fill in missing boolean fields with their model defaults.

    HTML checkboxes omit the field entirely when unchecked, so the
    submitted data dict won't contain a key for ``False`` booleans.
    This backfills those missing keys so Pydantic validation succeeds.
    F)model_fieldsitems
annotationbooldefaultpydanticfieldsPydanticUndefined)modeldataname
field_infos       b/Users/ahmed/devFolder/Ultron/claude-voice/.venv/lib/python3.12/site-packages/fastmcp/apps/form.py_backfill_boolean_defaultsr&   C   sq     "..446j4<  D(!!)J)JJ'//T
"T
 7 K    c                  b     e Zd ZdZddddddd	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 d	 fdZd
dZddZ xZS )	FormInputa  A Provider that collects structured input via a Pydantic model.

    Define a model for the data you need, and ``FormInput`` generates
    a form from it using ``Form.from_model()``. Field types, labels,
    descriptions, and validation are all derived from the model.

    Optionally provide an ``on_submit`` callback to process the
    validated data. The callback receives a model instance and returns
    a string that goes back to the LLM. Without a callback, the
    validated JSON is sent directly.

    Example::

        from pydantic import BaseModel
        from fastmcp import FastMCP
        from fastmcp.apps.form import FormInput

        class Contact(BaseModel):
            name: str
            email: str

        mcp = FastMCP("My Server")
        mcp.add_provider(FormInput(model=Contact))

    With a callback::

        def save_contact(contact: Contact) -> str:
            db.insert(contact.model_dump())
            return f"Saved {contact.name}"

        mcp.add_provider(FormInput(model=Contact, on_submit=save_contact))
    NSubmitF)r#   titlesubmit_text	tool_name	on_submitsend_messagec                  |xs |j                   }t        	| 	  |       || _        |xs |j                   | _        || _        |xs d|j                   j                          | _        || _        || _	        | j                          y )Ncollect_)__name__super__init___model_title_submit_textlower
_tool_name
_on_submit_send_message_register_tools)
selfr!   r#   r+   r,   r-   r.   r/   app_name	__class__s
            r%   r4   zFormInput.__init__z   s{     )5>>"-u~~'#J%..2F2F2H1I'J#)r'   c                6    d| j                   j                  dS )Nz
FormInput())r5   r2   )r=   s    r%   __repr__zFormInput.__repr__   s    DKK003155r'   c                    | | j                   | j                         ddfd       }| j                  j                  dj                   dj                   d      	 	 	 d		 	 	 	 	 	 	 	 	 d
fd       }y )Nc                    | i } t        |       } j                  |       }j                  j                  |      S t        j                  |j                  d            S )z%Validate and process form submission.json)mode)r&   model_validater:   rE   dumps
model_dump)r"   	validatedr!   providers     r%   submit_formz.FormInput._register_tools.<locals>.submit_form   se     |-eT:D,,T2I"".**955::i222?@@r'   zCollect zX information from the user via a form. Call this tool when you need the user to provide zi data. The user will see a validated form. After calling this tool, STOP and wait for the user to submit.)r#   descriptionc           	        |xs 
j                   }|xs 
j                  }t        d      5 }t               5  t	        |       ddd       t               5  t        d      5  t        |        t        dd      g}
j                  r|j                  dt        t                     |t        d	|
      gd}|rt        r||d<   t        j                   	fi | ddd       ddd       t#               5  t%        t&        j(                        5  t        d       ddd       ddd       ddd       t+        ddi      S # 1 sw Y   xY w# 1 sw Y   txY w# 1 sw Y   xxY w# 1 sw Y   LxY w# 1 sw Y   PxY w# 1 sw Y   TxY w)u  Collect structured input from the user.

            Args:
                prompt: Tell the user what you need and why.
                title: Optional heading for the form card.
                submit_text: Optional label for the submit button.
                default: Optional suggested response — a partial dict of form
                    field values keyed by field name. The form renders with
                    those values pre-filled so the user can confirm or edit
                    rather than start from a blank form. Use this when you
                    already know (or can infer) what the answer should be.
                    Requires prefab-ui>=0.19.1; silently ignored on older
                    versions.
            zmax-w-lg mx-auto)	css_classN   )gap	submittedTr   rL   )
on_success)submit_labelr.   defaultsz
Submitted.F)viewstate)r6   r7   r   r   r   r   r   r   r   r;   insertr
   r   r	   _FORM_SUPPORTS_DEFAULTSr   
from_modelr   r   r   rR   r   )promptr+   r,   r   r6   _submitrV   on_success_actionsfrom_model_kwargsr!   rK   s            r%   collect_inputz0FormInput._register_tools.<locals>.collect_input   sE   : -hooF!:X%:%:G 23t\vJ " !]FqM&M !d35&  --*11'/ )0$ -+=&9% #:8?)*5OOE?->?1 %2]4  \2eoo#6,' $7\= 4B "E* A "\ %2M]]4 $7#6\\= 43s   E?EE?E$A8EE$E?6E3E'E3$E?E	E?EEE$	 E?'E0,E33E<	8E??F)N)r"   dict[str, Any] | Nonereturnstr)NNN)
r[   rb   r+   
str | Noner,   rc   r   r`   ra   r   )r5   tooluir9   r2   )r=   rL   r_   r!   rK   s      @@r%   r<   zFormInput._register_tools   s    		A 
	A 
$$5>>* +D>>" #QR 
 

 !%&*-1	;	;	;	 $;	 +	;	
 ;	

;	r'   )r!   type[pydantic.BaseModel]r#   rc   r+   rc   r,   rb   r-   rc   r.   zCallable[..., str] | Noner/   r   ra   None)ra   rb   )ra   rg   )r2   
__module____qualname____doc__r4   rB   r<   __classcell__)r?   s   @r%   r)   r)   X   s{    J   # $/3"' 	
    -  
*6Sr'   r)   )r!   rf   r"   dict[str, Any]ra   rl   )*rj   
__future__r   rE   collections.abcr   typingr   packaging.versionr   r   	prefab_uiprefab_ui.actionsr   prefab_ui.actions.mcpr	   r
   prefab_ui.appr   prefab_ui.componentsr   r   r   r   r   r   r   r   !prefab_ui.components.control_flowr   prefab_ui.rxr   r   ImportError_exc__version__rY   r   fastmcp.apps.appr   r&   r)    r'   r%   <module>r}      s   0 #  $  5*;'	 	 	 5*$%i&;&;<@QQ  '#
 *M
 MM  
Q  $#$s)   :B B. B+	B&&B+.B87B8