-
Notifications
You must be signed in to change notification settings - Fork 115
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Working iframe Saving progress, dirty code, File transfer working. Working. Need UI work + code clean up. Build working * Fix file upload on front edge. to be reverted. * Fix UX * Tab manually changed. * Fix UI and rendering. * Better naming * Clean up * Adding front-viz * Adding comments. * Clean up console.log * Remove unused image. * Draft * Create .nvmrc * Add foundations for viz * ✨ * 🙈 * 🙈 * Fix * Fix useFile hook * ✂️ * ✂️ * Remove Sparkle from viz * ✨ * 🔙 * Use NEXT_PUBLIC_VIZ_URL at build time * 🙈 * ✂️ * 🙈 * ✂️ * 😑 * 🐵 * 🔨 * 💥 * Only listen to current action * ✂️ * ✂️ * ✨ * ✂️ --------- Co-authored-by: Aric Lasry <lasry.aric@gmail.com>
- Loading branch information
Showing
12 changed files
with
457 additions
and
82 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
171 changes: 171 additions & 0 deletions
171
front/components/assistant/conversation/actions/VisualizationActionIframe.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,171 @@ | ||
import { BracesIcon, PlayIcon, Tab } from "@dust-tt/sparkle"; | ||
import type { | ||
VisualizationActionType, | ||
VisualizationRPCRequest, | ||
WorkspaceType, | ||
} from "@dust-tt/types"; | ||
import { | ||
isGetCodeToExecuteRequest, | ||
isGetFileRequest, | ||
isVisualizationRPCRequest, | ||
visualizationExtractCodeNonStreaming, | ||
visualizationExtractCodeStreaming, | ||
} from "@dust-tt/types"; | ||
import { useCallback, useEffect, useState } from "react"; | ||
|
||
import { RenderMessageMarkdown } from "@app/components/assistant/RenderMessageMarkdown"; | ||
|
||
const sendResponseToIframe = ( | ||
request: VisualizationRPCRequest, | ||
response: unknown, | ||
target: MessageEventSource | ||
) => { | ||
target.postMessage( | ||
{ | ||
command: "answer", | ||
messageUniqueId: request.messageUniqueId, | ||
actionId: request.actionId, | ||
result: response, | ||
}, | ||
// TODO(2024-07-24 flav) Restrict origin. | ||
{ targetOrigin: "*" } | ||
); | ||
}; | ||
|
||
// Custom hook to encapsulate the logic for handling visualization messages. | ||
function useVisualizationDataHandler( | ||
action: VisualizationActionType, | ||
workspaceId: string, | ||
onRetry: () => void | ||
) { | ||
const getFile = useCallback( | ||
async (fileId: string) => { | ||
const response = await fetch( | ||
`/api/w/${workspaceId}/files/${fileId}?action=view` | ||
); | ||
if (!response.ok) { | ||
// TODO(2024-07-24 flav) Propagate the error to the iframe. | ||
throw new Error(`Failed to fetch file ${fileId}`); | ||
} | ||
|
||
const resBuffer = await response.arrayBuffer(); | ||
return new File([resBuffer], fileId, { | ||
type: response.headers.get("Content-Type") || undefined, | ||
}); | ||
}, | ||
[workspaceId] | ||
); | ||
|
||
useEffect(() => { | ||
const listener = async (event: MessageEvent) => { | ||
const { data } = event; | ||
|
||
// TODO(2024-07-24 flav) Check origin. | ||
if ( | ||
!isVisualizationRPCRequest(data) || | ||
!event.source || | ||
data.actionId !== action.id | ||
) { | ||
return; | ||
} | ||
|
||
if (isGetFileRequest(data)) { | ||
const file = await getFile(data.params.fileId); | ||
|
||
sendResponseToIframe(data, { file }, event.source); | ||
} else if (isGetCodeToExecuteRequest(data)) { | ||
const code = action.generation; | ||
|
||
sendResponseToIframe(data, { code }, event.source); | ||
} else { | ||
// TODO(2024-07-24 flav) Pass the error message to the host window. | ||
onRetry(); | ||
} | ||
|
||
// TODO: Types above are not accurate, as it can pass the first check but won't enter any if block. | ||
}; | ||
|
||
window.addEventListener("message", listener); | ||
return () => window.removeEventListener("message", listener); | ||
}, [action.generation, action.id, onRetry, getFile]); | ||
|
||
return { getFile }; | ||
} | ||
|
||
export function VisualizationActionIframe({ | ||
owner, | ||
action, | ||
isStreaming, | ||
streamedCode, | ||
onRetry, | ||
}: { | ||
conversationId: string; | ||
owner: WorkspaceType; | ||
action: VisualizationActionType; | ||
streamedCode: string | null; | ||
isStreaming: boolean; | ||
onRetry: () => void; | ||
}) { | ||
const [activeTab, setActiveTab] = useState<"code" | "runtime">("code"); | ||
const [tabManuallyChanged, setTabManuallyChanged] = useState(false); | ||
|
||
const workspaceId = owner.sId; | ||
|
||
useVisualizationDataHandler(action, workspaceId, onRetry); | ||
|
||
useEffect(() => { | ||
if (activeTab === "code" && action.generation && !tabManuallyChanged) { | ||
setActiveTab("runtime"); | ||
setTabManuallyChanged(true); | ||
} | ||
}, [action.generation, activeTab, tabManuallyChanged]); | ||
|
||
let extractedCode: string | null = null; | ||
|
||
if (action.generation) { | ||
extractedCode = visualizationExtractCodeNonStreaming(action.generation); | ||
} else { | ||
extractedCode = visualizationExtractCodeStreaming(streamedCode || ""); | ||
} | ||
|
||
return ( | ||
<> | ||
<Tab | ||
tabs={[ | ||
{ | ||
label: "Code", | ||
id: "code", | ||
current: activeTab === "code", | ||
icon: BracesIcon, | ||
sizing: "expand", | ||
}, | ||
{ | ||
label: "Run", | ||
id: "runtime", | ||
current: activeTab === "runtime", | ||
icon: PlayIcon, | ||
sizing: "expand", | ||
hasSeparator: true, | ||
}, | ||
]} | ||
setCurrentTab={(tabId, event) => { | ||
event.preventDefault(); | ||
setActiveTab(tabId as "code" | "runtime"); | ||
}} | ||
/> | ||
{activeTab === "code" && extractedCode && extractedCode.length > 0 && ( | ||
<RenderMessageMarkdown | ||
content={"```javascript\n" + extractedCode + "\n```"} | ||
isStreaming={isStreaming} | ||
/> | ||
)} | ||
{activeTab === "runtime" && ( | ||
<iframe | ||
style={{ width: "100%", height: "600px" }} | ||
src={`${process.env.NEXT_PUBLIC_VIZ_URL}/content?aId=${action.id}`} | ||
sandbox="allow-scripts" | ||
/> | ||
)} | ||
</> | ||
); | ||
} |
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.