From 0e9c0abda92dd4b0571b69173000fc891bd3dcf2 Mon Sep 17 00:00:00 2001 From: Raditya Harya Date: Fri, 3 May 2024 14:04:12 +0000 Subject: [PATCH] fix recommend stuff --- src/app/utils/reactFlowToWorkflow.ts | 15 ---------- src/components/nodes/Filter/RemoveMatch.tsx | 8 ----- src/components/nodes/Library/Playlist.tsx | 9 ------ src/components/nodes/Primitives/Debug.tsx | 2 +- src/components/nodes/Selectors/Recommend.tsx | 13 ++++---- src/hooks/useBasicNodeState.ts | 31 ++++++++++---------- src/lib/workflow/Selector.ts | 19 ++++++++---- src/types.d.ts | 0 8 files changed, 37 insertions(+), 60 deletions(-) delete mode 100644 src/types.d.ts diff --git a/src/app/utils/reactFlowToWorkflow.ts b/src/app/utils/reactFlowToWorkflow.ts index 9bfc583..d6c6382 100644 --- a/src/app/utils/reactFlowToWorkflow.ts +++ b/src/app/utils/reactFlowToWorkflow.ts @@ -9,21 +9,7 @@ type ReactFlowToWorkflowInput = { edges: Edge[]; }; -function filterNodes(nodes) { - return nodes.filter((node) => node.type?.match(/\w+\.\w+/)); -} - -function removeUnnecessaryData(nodes) { - nodes.forEach((node) => { - if (node.type === "Combiner.alternate") { - node.params.playlists = undefined; - node.params.playlistIds = undefined; - } - }); -} - function addNodesToWorkflow(nodes, workflow) { - let _hasSource = false; nodes.forEach((node) => { const typeWithoutPostfix = node.type!.split("-")[0]; @@ -78,7 +64,6 @@ export default async function reactFlowToWorkflow({ if (nodes.length > 0 && edges.length > 0) { addNodesToWorkflow(nodes, workflowObject); addEdgesToWorkflow(edges, workflowObject); - removeUnnecessaryData(workflowObject.operations); const response = await validateWorkflow(workflowObject); valid = response.valid; errors = response.errors; diff --git a/src/components/nodes/Filter/RemoveMatch.tsx b/src/components/nodes/Filter/RemoveMatch.tsx index 27df473..659dc81 100644 --- a/src/components/nodes/Filter/RemoveMatch.tsx +++ b/src/components/nodes/Filter/RemoveMatch.tsx @@ -25,14 +25,6 @@ type PlaylistProps = { data: any; }; -type Playlist = { - playlistId?: string; - name?: string; - description?: string; - image?: string; - total?: number; - owner?: string; -}; const formSchema = z.object({ filterKey: z.string().min(1, { diff --git a/src/components/nodes/Library/Playlist.tsx b/src/components/nodes/Library/Playlist.tsx index e85ea19..13f176a 100644 --- a/src/components/nodes/Library/Playlist.tsx +++ b/src/components/nodes/Library/Playlist.tsx @@ -51,15 +51,6 @@ type PlaylistProps = { data: Workflow.Playlist; }; -type Playlist = { - playlistId?: string; - name?: string; - description?: string; - image?: string; - total?: number; - owner?: string; -}; - const formSchema = z.object({ playlistId: z.string().min(1, { message: "Playlist is required.", diff --git a/src/components/nodes/Primitives/Debug.tsx b/src/components/nodes/Primitives/Debug.tsx index bba56f4..6cb467b 100644 --- a/src/components/nodes/Primitives/Debug.tsx +++ b/src/components/nodes/Primitives/Debug.tsx @@ -29,7 +29,7 @@ const DebugInfo = ({ data: {JSON.stringify(getNodeData(id), null, 2)} - +
TargetConnections:
{TargetConnections?.map((connection) => (
diff --git a/src/components/nodes/Selectors/Recommend.tsx b/src/components/nodes/Selectors/Recommend.tsx
index f593d57..4b07e51 100644
--- a/src/components/nodes/Selectors/Recommend.tsx
+++ b/src/components/nodes/Selectors/Recommend.tsx
@@ -52,12 +52,12 @@ const Recommend: React.FC = ({ id, data }) => {
     if (data) {
       const parsedData = {
         seedType: data.seedType || "tracks",
-        count: data.count || 20,
+        count: data.count || 27,
       };
       form!.reset(parsedData);
       form?.setValue("seedType", parsedData.seedType);
     }
-  }, [data, form]);
+  }, [data]);
 
   const watch = form!.watch();
   const prevWatchRef = React.useRef(watch);
@@ -109,11 +109,11 @@ const Recommend: React.FC = ({ id, data }) => {
             
             
              = ({ id, data }) => {
               }
               selectOptions={selectOptions}
               register={register!}
-              description={`The operation to perform`}
+              description={`The type of seed to use for the recommendation engine.`}
             />
             
           
         
       
-      
       ) => {
-  const { nodes, getNode, updateNodeData } = useStore(
+  const { nodes, edges, getNode, updateNodeData } = useStore(
     useShallow((state) => ({
       nodes: state.nodes,
+      edges: state.edges,
       getNode: state.getNode,
       updateNodeData: state.updateNodeData,
     })),
@@ -32,6 +24,7 @@ const usePlaylistLogic = (id: string, formSchema?: ZodObject) => {
 
   const { formState, register } = form ?? {};
 
+  const currentNode = getNode(id);
   const getNodeData = (id: string) => getNode(id)?.data;
 
   const targetConnections = useHandleConnections({
@@ -61,10 +54,11 @@ const usePlaylistLogic = (id: string, formSchema?: ZodObject) => {
     }
 
     targetConnections.forEach((connection) => {
-      const target = getNodeData(connection.source);
+      const node = getNode(connection.source);
+      const target = node!.data;
       if (!target) return;
 
-      const {
+      let {
         playlistId,
         playlistIds = [],
         total,
@@ -75,13 +69,19 @@ const usePlaylistLogic = (id: string, formSchema?: ZodObject) => {
         playlists = [],
       } = target as any;
 
+      
       const hasPlaylistId = Boolean(playlistId);
       const hasPlaylistIds = Boolean(playlistIds && playlistIds.length > 0);
 
       if (!(hasPlaylistId || hasPlaylistIds)) {
         invalidNodesCount++;
       }
-
+      
+      if (currentNode!.type === "Selector.recommend") {
+        playlistId = `recommend-${playlistId}`;
+        name = `[Recommended] ${name}`;
+        total = currentNode!.data.count;
+      }
       const playlist: Workflow.Playlist = {
         playlistId: playlistId as string,
         name: name as string,
@@ -103,7 +103,8 @@ const usePlaylistLogic = (id: string, formSchema?: ZodObject) => {
     const combinedPlaylists = Array.from(playlistsSet)
       .filter(Boolean)
       .filter((playlist) => Object.keys(playlist).length !== 0)
-      .filter((playlist) => playlist.playlistId);
+      .filter((playlist) => playlist.playlistId)
+      .filter((playlist) => playlist.playlistId !== "recommended");
 
     return {
       playlistIds: combinedPlaylistIds,
diff --git a/src/lib/workflow/Selector.ts b/src/lib/workflow/Selector.ts
index f69475d..8d27278 100644
--- a/src/lib/workflow/Selector.ts
+++ b/src/lib/workflow/Selector.ts
@@ -155,13 +155,22 @@ export default class Selector extends Base {
     const tracks = Selector.getTracks(sources);
 
     const seedTracks = new Array();
+    let maxRetry = 5;
 
     if (Array.isArray(tracks)) {
-      const res = new Set();
-      while (res.size < 5) {
-        // 5 is the max number of seed shared between tracks, artists, and genres
-        const randomIndex = Math.floor(Math.random() * tracks.length);
-        res.add(tracks[randomIndex]!);
+      let res = new Set();
+      if (tracks.length < 5) {
+        res = new Set(tracks);
+      } else {
+        while (res.size < 5) {
+          if (maxRetry === 0) {
+            throw new Error("Failed to get seed tracks");
+          }
+          // 5 is the max number of seed shared between tracks, artists, and genres
+          const randomIndex = Math.floor(Math.random() * tracks.length);
+          res.add(tracks[randomIndex]!);
+          maxRetry--;
+        }
       }
       seedTracks.push(...Array.from(res));
     } else {
diff --git a/src/types.d.ts b/src/types.d.ts
deleted file mode 100644
index e69de29..0000000