-
Notifications
You must be signed in to change notification settings - Fork 3.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Custom Widget Editor integration with AI (#37257)
## Description Adds the AI widget builder integration inside the Custom Widget Editor. User can prompt the AI on the changes and the bot will update the code of the widget direclty. Fixes #37250 ## Automation /ok-to-test tags="@tag.Sanity" ### 🔍 Cypress test results <!-- This is an auto-generated comment: Cypress test results --> > [!IMPORTANT] > 🟣 🟣 🟣 Your tests are running. > Tests running at: <https://github.com/appsmithorg/appsmith/actions/runs/11772460894> > Commit: cffb595 > Workflow: `PR Automation test suite` > Tags: `@tag.Sanity` > Spec: `` > <hr>Mon, 11 Nov 2024 05:03:57 UTC <!-- end of auto-generated comment: Cypress test results --> ## Communication Should the DevRel and Marketing teams inform users about this change? - [x] Yes - [ ] No <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes - **New Features** - Introduced a feature flag for a custom AI widget builder. - Added a new `ChatBot` component for enhanced interactivity within the editor. - **Improvements** - Simplified layout management by removing the `LayoutControls` component. - Enhanced the `TabsLayout` to conditionally display an AI tab based on the feature flag. - Updated the `Editor` component to include the AI tab dynamically. - **Bug Fixes** - Streamlined state management for layout selection in the custom widget builder context. These updates improve user experience by integrating AI capabilities and simplifying the interface. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
- Loading branch information
Showing
13 changed files
with
196 additions
and
243 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
103 changes: 103 additions & 0 deletions
103
app/client/src/pages/Editor/CustomWidgetBuilder/Editor/ChatBot/ChatBot.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
import React, { | ||
useCallback, | ||
useContext, | ||
useEffect, | ||
useMemo, | ||
useRef, | ||
} from "react"; | ||
import type { ContentProps } from "../CodeEditors/types"; | ||
import { CustomWidgetBuilderContext } from "../.."; | ||
import { | ||
CUSTOM_WIDGET_AI_BOT_MESSAGE_RESPONSE_DEBOUNCE_TIMEOUT, | ||
CUSTOM_WIDGET_AI_BOT_URL, | ||
CUSTOM_WIDGET_AI_CHAT_TYPE, | ||
CUSTOM_WIDGET_AI_INITIALISED_MESSAGE, | ||
} from "../../constants"; | ||
import { isObject } from "lodash"; | ||
|
||
export const ChatBot = (props: ContentProps) => { | ||
const ref = useRef<HTMLIFrameElement>(null); | ||
const lastUpdateFromBot = useRef<number>(0); | ||
const { bulkUpdate, parentEntityId, uncompiledSrcDoc, widgetId } = useContext( | ||
CustomWidgetBuilderContext, | ||
); | ||
|
||
const handleSrcDocUpdates = useCallback(() => { | ||
// Don't send updates back to bot if the last update came from the bot within the last 100ms | ||
if ( | ||
Date.now() - lastUpdateFromBot.current < | ||
CUSTOM_WIDGET_AI_BOT_MESSAGE_RESPONSE_DEBOUNCE_TIMEOUT | ||
) { | ||
return; | ||
} | ||
|
||
// Send src doc to the chatbot iframe | ||
if (ref.current && ref.current.contentWindow && uncompiledSrcDoc) { | ||
ref.current.contentWindow.postMessage( | ||
{ | ||
html_code: uncompiledSrcDoc.html, | ||
css_code: uncompiledSrcDoc.css, | ||
js_code: uncompiledSrcDoc.js, | ||
chatType: CUSTOM_WIDGET_AI_CHAT_TYPE, | ||
}, | ||
"*", | ||
); | ||
} | ||
}, [uncompiledSrcDoc]); | ||
|
||
const updateContents = useCallback( | ||
( | ||
event: MessageEvent< | ||
string | { html_code?: string; css_code?: string; js_code?: string } | ||
>, | ||
) => { | ||
const iframeWindow = | ||
ref.current?.contentWindow || ref.current?.contentDocument?.defaultView; | ||
|
||
// Accept messages only from the current iframe | ||
if (event.source !== iframeWindow) return; | ||
|
||
if (event.data === CUSTOM_WIDGET_AI_INITIALISED_MESSAGE) { | ||
handleSrcDocUpdates(); | ||
|
||
return; | ||
} | ||
|
||
if (!bulkUpdate) return; | ||
|
||
if (isObject(event.data)) { | ||
lastUpdateFromBot.current = Date.now(); | ||
|
||
bulkUpdate({ | ||
html: event.data.html_code || "", | ||
css: event.data.css_code || "", | ||
js: event.data.js_code || "", | ||
}); | ||
} | ||
}, | ||
[bulkUpdate, handleSrcDocUpdates], | ||
); | ||
|
||
useEffect( | ||
function addEventListenerForBotUpdates() { | ||
// add a listener to update the contents | ||
window.addEventListener("message", updateContents, false); | ||
|
||
// clean up | ||
return () => window.removeEventListener("message", updateContents, false); | ||
}, | ||
[updateContents], | ||
); | ||
|
||
useEffect(handleSrcDocUpdates, [handleSrcDocUpdates]); | ||
|
||
const instanceId = `${widgetId}-${parentEntityId}`; | ||
|
||
const srcUrl = useMemo(() => { | ||
return CUSTOM_WIDGET_AI_BOT_URL(instanceId); | ||
}, [instanceId]); | ||
|
||
return ( | ||
<iframe height={`${props.height}px`} ref={ref} src={srcUrl} width="100%" /> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 0 additions & 49 deletions
49
app/client/src/pages/Editor/CustomWidgetBuilder/Editor/Header/layoutControls.tsx
This file was deleted.
Oops, something went wrong.
112 changes: 0 additions & 112 deletions
112
app/client/src/pages/Editor/CustomWidgetBuilder/Editor/Layouts/SplitLayout/index.tsx
This file was deleted.
Oops, something went wrong.
25 changes: 0 additions & 25 deletions
25
app/client/src/pages/Editor/CustomWidgetBuilder/Editor/Layouts/SplitLayout/styles.module.css
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.