How to handle user inputs in the Generative UI application.
Depending on the use case or query, the API might present the user with a form to fill out rather than asking a set of questions and expecting the user to type in the answers. For example, if the user wants to plan a trip, the API would present a form to fill out asking their preferences like the destination, the dates, the type of accommodation, etc. rather than asking users to type in the answers.
C1 supports a variety of input types out of the box and we are constantly adding more. The following input fields are currently supported:
The generation, validation and submission of the form is automatically handled by Thesys SDK and no additional code is required to handle this. To guide the generation of the form, you can use the system prompt to instruct the API to generate forms with certain fields or specific kind of input options.
Sometimes, you might want to generate a form with a specific set of fields or input options. For example, if you want to generate a form for a trip planning application, you can use the following system prompt:
In most cases, you don’t need to manually specify a prompt for the form. A better approach is to pass in the tool call for the action you want the agent to perform and let C1 automtically render the form based on the JSON schema of the tool call input parameters.
For example if you want to create a AI Jira copilot, rather than manually specifying the form fields, you can pass in the tool call specification for creating a Jira issue and let C1 automatically render the form based on the JSON schema of the tool call input parameters.
In this case, C1 will automatically render a form with the fields required to create a Jira issue and even automatically prefill the title since that was already provided in the user query.
Form submissions can be handled in the same way as general interactivity is handled.
When the user fills out the form and clicks on the submit button, the onAction
callback will be called with the form data.
In this case, Thesys SDK will automatically fill the llmFriendlyMessage
with the form data and the action
such that
the appropriate tool call is made to the backend.
Note: If you are building a chatbot, this is already handled for you in the <C1Chat>
component.
Sometime, you might want to persist the form data in the Generative UI application so that when the user refreshes the page, the form data is not lost. Field data is automatically kept in sync with the user’s input within the context of the same ‘response’ object.
Whenever the user updates the form, the updateMessage
callback will be called with the updated form data merged with
the rest of the response object. Simply pass in a function to the updateMessage
prop that can be used to persist the
entire reponse and form persistence should be handled automatically.
If you implement persistence for the generated response and update it via this callback, when the user refreshes the page the entire application state will be restored including the form data.