From dac517add404fc42ce878fa59b8b4932ce596dda Mon Sep 17 00:00:00 2001 From: akhisud3195 Date: Thu, 13 Feb 2025 17:20:39 +0530 Subject: [PATCH] Run individual simulations in playground --- .../rowboat/app/actions/simulation_actions.ts | 15 ++- .../projects/[projectId]/playground/app.tsx | 118 ++++++++++-------- .../projects/[projectId]/simulation/app.tsx | 6 +- 3 files changed, 83 insertions(+), 56 deletions(-) diff --git a/apps/rowboat/app/actions/simulation_actions.ts b/apps/rowboat/app/actions/simulation_actions.ts index 964c4aff..91cd5db4 100644 --- a/apps/rowboat/app/actions/simulation_actions.ts +++ b/apps/rowboat/app/actions/simulation_actions.ts @@ -5,6 +5,7 @@ import { scenariosCollection } from "@/app/lib/mongodb"; import { z } from 'zod'; import { projectAuthCheck } from "./project_actions"; import { Scenario, type WithStringId } from "@/app/lib/types"; +import { SimulationScenarioData } from "@/app/lib/types"; export async function getScenarios(projectId: string): Promise>[]> { await projectAuthCheck(projectId); @@ -16,18 +17,22 @@ export async function getScenarios(projectId: string): Promise> | null> { +export async function getScenario(projectId: string, scenarioId: string): Promise>> { await projectAuthCheck(projectId); + // fetch scenario const scenario = await scenariosCollection.findOne({ _id: new ObjectId(scenarioId), projectId, }); - - if (!scenario) return null; + if (!scenario) { + throw new Error('Scenario not found'); + } + const { _id, description, ...rest } = scenario; return { - ...scenario, - _id: scenario._id.toString(), + ...rest, + _id: _id.toString(), + scenario: description, }; } diff --git a/apps/rowboat/app/projects/[projectId]/playground/app.tsx b/apps/rowboat/app/projects/[projectId]/playground/app.tsx index c1d3a389..a59f1b3e 100644 --- a/apps/rowboat/app/projects/[projectId]/playground/app.tsx +++ b/apps/rowboat/app/projects/[projectId]/playground/app.tsx @@ -1,14 +1,15 @@ 'use client'; import { Dropdown, DropdownItem, DropdownMenu, DropdownTrigger, Spinner } from "@nextui-org/react"; -import { useEffect, useState, useMemo } from "react"; +import { useEffect, useState, useMemo, useCallback } from "react"; import { z } from "zod"; -import { PlaygroundChat, SimulationData, Workflow } from "@/app/lib/types"; +import { PlaygroundChat, SimulationData, SimulationScenarioData, Workflow } from "@/app/lib/types"; import { SimulateScenarioOption, SimulateURLOption } from "./simulation-options"; import { Chat } from "./chat"; import { useSearchParams } from "next/navigation"; import { ActionButton, Pane } from "../workflow/pane"; import { apiV1 } from "rowboat-shared"; import { EllipsisVerticalIcon, MessageSquarePlusIcon, PlayIcon } from "lucide-react"; +import { getScenario } from "@/app/actions/simulation_actions"; function SimulateLabel() { return Simulatebeta; @@ -41,9 +42,42 @@ export function App({ systemMessage: defaultSystemMessage, }); + const beginSimulation = useCallback((data: z.infer) => { + setExistingChatId(null); + setViewSimulationMenu(false); + setCounter(counter + 1); + setChat({ + projectId, + createdAt: new Date().toISOString(), + messages: [], + simulated: true, + simulationData: data, + }); + }, [counter, projectId]); + + useEffect(() => { + const scenarioId = localStorage.getItem('pendingScenarioId'); + if (scenarioId && projectId) { + console.log('Scenario Effect triggered:', { scenarioId, projectId }); + getScenario(projectId, scenarioId).then((scenario) => { + console.log('Scenario data received:', scenario); + beginSimulation(scenario as z.infer); + localStorage.removeItem('pendingScenarioId'); + }).catch(error => { + console.error('Error fetching scenario:', error); + localStorage.removeItem('pendingScenarioId'); + }); + } + }, [projectId, beginSimulation]); + + if (hidden) { + return <>; + } + function handleSimulateButtonClick() { setViewSimulationMenu(true); } + function handleNewChatButtonClick() { setExistingChatId(null); setViewSimulationMenu(false); @@ -56,52 +90,38 @@ export function App({ systemMessage: defaultSystemMessage, }); } - function beginSimulation(data: z.infer) { - setExistingChatId(null); - setViewSimulationMenu(false); - setCounter(counter + 1); - setChat({ - projectId, - createdAt: new Date().toISOString(), - messages: [], - simulated: true, - simulationData: data, - }); - } - if (hidden) { - return <>; - } - - return : "Chat"} actions={[ - } - onClick={handleNewChatButtonClick} - > - New chat - , - !viewSimulationMenu && } - onClick={handleSimulateButtonClick} - > - Simulate - , - ]}> -
- {!viewSimulationMenu && loadingChat &&
- -
} - {!viewSimulationMenu && !loadingChat && } - {viewSimulationMenu && } -
-
; + return ( + : "Chat"} actions={[ + } + onClick={handleNewChatButtonClick} + > + New chat + , + !viewSimulationMenu && } + onClick={handleSimulateButtonClick} + > + Simulate + , + ]}> +
+ {!viewSimulationMenu && loadingChat &&
+ +
} + {!viewSimulationMenu && !loadingChat && } + {viewSimulationMenu && } +
+
+ ); } diff --git a/apps/rowboat/app/projects/[projectId]/simulation/app.tsx b/apps/rowboat/app/projects/[projectId]/simulation/app.tsx index 6ca0f34e..6c7ad62b 100644 --- a/apps/rowboat/app/projects/[projectId]/simulation/app.tsx +++ b/apps/rowboat/app/projects/[projectId]/simulation/app.tsx @@ -156,8 +156,10 @@ export default function SimulationApp() { }; const runSingleScenario = (scenario: ScenarioType) => { - // Navigate to the workflow playground with the scenario - router.push(`/projects/${projectId}/workflow/playground?scenarioId=${scenario._id}`); + // Store scenario ID in localStorage instead of URL parameter + localStorage.setItem('pendingScenarioId', scenario._id); + // Navigate to the playground without query parameter + router.push(`/projects/${projectId}/workflow`); setMenuOpenScenarioId(null); };