-
Notifications
You must be signed in to change notification settings - Fork 3.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Custom Widget Editor integration with AI #37257
Merged
Merged
Changes from all commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
a3a1a59
fix: API Body format context lost
hetunandu ff0a0a7
API and GraphQL
hetunandu 9b60d95
Merge branch 'release' into fix/paddings-and-scrolls
hetunandu 0e7e723
Merge branch 'release' into fix/paddings-and-scrolls
hetunandu efcc7ef
fix for other query fields
hetunandu 0abd134
Merge branch 'release' into fix/paddings-and-scrolls
hetunandu 9d04a85
API method and url
hetunandu 1398c9c
[Plugin Action Editor] Query Form evaluation
hetunandu 1e40efa
Merge branch 'release' into chore/plugin-action-editor-query-init
hetunandu 585c787
fix: Add changes
hetunandu eb956da
Merge branch 'release' into feat/custom-widget-tab-edit
hetunandu 46ecf58
Keep tabs mounted always
hetunandu 9444c94
Add flag for default AI tab
hetunandu 2d41e2a
Merge branch 'release' into feat/custom-widget-tab-edit
hetunandu 8e9107b
updates for iframe url
hetunandu 7d9cb19
use uncompled Src doc
hetunandu 31aeb2f
Merge branch 'release' into feat/custom-widget-tab-edit
hetunandu da40c16
add url origin on iframe
hetunandu 95bd4ad
sending context post CHAT_INTIALISED message
hetunandu dc633f4
Merge branch 'release' into feat/custom-widget-tab-edit
hetunandu 80d412c
clean up
hetunandu a824609
Fix bug with outdated code posting
hetunandu cffb595
Only show AI tab if feature flag exists
hetunandu f48efee
address review comments
hetunandu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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]); | ||
hetunandu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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%" /> | ||
); | ||
}; | ||
hetunandu marked this conversation as resolved.
Show resolved
Hide resolved
|
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Add type validation for message data.
Consider adding type validation to ensure message data is properly structured:
📝 Committable suggestion