Widget
Overview
Widgets in UPTIQ AI Workbench are modular UI components that enhance the functionality of AI agents by embedding custom interactive elements. They allow developers to create custom interfaces that integrate seamlessly within agent workflows, enabling enhanced interactivity, data visualization, and workflow automation.
What is a Widget?
A widget in UPTIQ AI Workbench is a reusable web component that extends the capabilities of AI agents. It can be a simple UI element like a button or a complex component that interacts with backend workflows, listens to events, and processes user input.
How Widgets Work in UPTIQ AI Workbench
- Creation: Developers create widgets using React, Tailwind CSS, and Shadcn UI. 
- Exporting: Widgets are converted into web components using - @r2wc/react-to-web-component.
- Hosting: Built widget bundles are hosted and linked in the agent configuration. 
- Integration: The widget is embedded within the agent’s UI and interacts with workflows. 
- Event Listening & Execution: Widgets can listen to custom events and trigger workflows accordingly. 
How to add a custom widget?
Important Note
It is mandatory to use only the following libraries/packages to develop a component:
- React - https://react.dev 
- Shadcn UI - https://ui.shadcn.com/docs 
- Tailwind CSS - https://tailwindcss.com/ 
- @r2wc/react-to-web-component - for converting React components to web components - https://www.npmjs.com/package/@r2wc/react-to-web-component 
Prerequisites
- Node.js and Yarn installed 
- Basic knowledge of React and web components 
- Access to the UPTIQ AI Workbench 
Step 1: Set Up the Custom Widget Project
- Download the starter project from - custom-widget-starterhttps://drive.google.com/drive/folders/14JVnBlhEijOan1AxGA6IhjicsBMjSi8d
- Install dependencies 
yarn- The entry point for widgets is - src/index.ts. This file exports all widgets as web components.
Step 2: Create a New Widget
- Create a new file: - src/widgets/secondWidget/SecondWidget.tsx.
- Add a React component: 
export const SecondWidget = () => {
  return (
    <div>
      // Your component code goes here.
    </div>
  );
};- Create an - index.tsinside- src/widgets/secondWidget/for better organization:
export { SecondWidget } from "./SecondWidget";- Update - src/index.tsto register the new widget:
import { SecondWidget } from "./widgets/secondWidget";
const widgets = [
  { tag: "first-widget", component: FirstWidget },
  { tag: "second-widget", component: SecondWidget },
];
widgets.forEach(registerWidgetAsWebComponent);Step 3: Build and Deploy the Widget
- Build the project: - yarn build
- Host the - dist/folder bundle in the cloud.
- Use - dist/index.jsin the AI Workbench as a script:
<script src="<hosted-base-url>/index.js" type="module"></script>
<second-widget></second-widget>Step 4: Configure Custom Widget in UPTIQ AI Workbench
- Navigate to the Widgets tab in the AI Agent config page. 
- Click on Add Custom Widget. 
- Fill in the required details: - Name: Widget name 
- Description: Short description 
- Events: Custom events for workflow interactions 
- HTML Content: Include the script to load the widget 
 
- Click Save. 
- Approve the widget and toggle it on for use. 
Step 5: Handling Events in Custom Widgets
- Use - useSamuelEventListenerto listen for custom events:
const handleEvent = useCallback((eventData: any) => {
  console.log(eventData);
});
useSamuelEventListenr("test-event", handleEvent);Step 6: Running Workflows from a Widget
Call the workflow execution API:
const handleRunWorkflow = async (taskInputs: any) => {
  const executionId = uuid();
  const { uid } = getSamuelUser();
  const { appId, serverUrl, widgetKey } = getSamuelConfig();
  
  const workflowId = taskInputs?.workflowId;
  if (!workflowId) throw new Error("workflowId is required");
  
  const response = await axios.post(
    `${serverUrl}/workflow-defs/run-sync`,
    { executionId, uid, integrationId: workflowId, appId, taskInputs },
    { headers: { widgetKey, appid: appId } }
  );
  console.log(response.data);
};Step 7: Testing Widgets Locally
- Update - src/development/constants.tswith test values.
- Modify - index.htmlto include the widget:
<script type="module" src="/src/index.ts"></script>
<div style="width: 400px">
  <second-widget></second-widget>
</div>- Start the local development server: 
yarn devConclusion
Following these steps, developers can create, configure, and integrate custom widgets into the UPTIQ AI Workbench to enhance AI agent capabilities.
Last updated

