rowboat/apps/rowboat/app/projects/[projectId]/playground/app.tsx

133 lines
4.8 KiB
TypeScript
Raw Normal View History

2025-01-13 15:31:31 +05:30
'use client';
2025-01-23 11:39:27 +05:30
import { Dropdown, DropdownItem, DropdownMenu, DropdownTrigger, Spinner } from "@nextui-org/react";
import { useEffect, useState, useMemo, useCallback } from "react";
2025-01-13 15:31:31 +05:30
import { z } from "zod";
2025-02-14 13:18:02 +05:30
import { PlaygroundChat, SimulationData, SimulationScenarioData, Workflow } from "../../../lib/types";
2025-01-13 15:31:31 +05:30
import { SimulateScenarioOption, SimulateURLOption } from "./simulation-options";
import { Chat } from "./chat";
import { useSearchParams, useRouter } from "next/navigation";
2025-01-13 15:31:31 +05:30
import { ActionButton, Pane } from "../workflow/pane";
import { apiV1 } from "rowboat-shared";
2025-01-23 11:39:27 +05:30
import { EllipsisVerticalIcon, MessageSquarePlusIcon, PlayIcon } from "lucide-react";
2025-02-14 13:18:02 +05:30
import { getScenario } from "../../../actions/simulation_actions";
2025-01-13 15:31:31 +05:30
function SimulateLabel() {
return <span>Simulate<sup className="pl-1">beta</sup></span>;
}
const defaultSystemMessage = '';
export function App({
hidden = false,
projectId,
workflow,
messageSubscriber,
}: {
hidden?: boolean;
projectId: string;
workflow: z.infer<typeof Workflow>;
messageSubscriber?: (messages: z.infer<typeof apiV1.ChatMessage>[]) => void;
}) {
const searchParams = useSearchParams();
const router = useRouter();
2025-01-13 15:31:31 +05:30
const initialChatId = useMemo(() => searchParams.get('chatId'), [searchParams]);
const [existingChatId, setExistingChatId] = useState<string | null>(initialChatId);
const [loadingChat, setLoadingChat] = useState<boolean>(false);
const [viewSimulationMenu, setViewSimulationMenu] = useState<boolean>(false);
const [counter, setCounter] = useState<number>(0);
const [chat, setChat] = useState<z.infer<typeof PlaygroundChat>>({
projectId,
createdAt: new Date().toISOString(),
messages: [],
simulated: false,
systemMessage: defaultSystemMessage,
});
const beginSimulation = useCallback((data: z.infer<typeof SimulationData>) => {
2025-01-13 15:31:31 +05:30
setExistingChatId(null);
setViewSimulationMenu(false);
setCounter(counter + 1);
setChat({
projectId,
createdAt: new Date().toISOString(),
messages: [],
simulated: true,
simulationData: data,
2025-02-14 12:18:50 +05:30
systemMessage: 'context' in data ? data.context : '',
2025-01-13 15:31:31 +05:30
});
}, [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);
2025-02-14 12:18:50 +05:30
beginSimulation({
...scenario,
systemMessage: scenario.context || '',
} as z.infer<typeof SimulationScenarioData>);
localStorage.removeItem('pendingScenarioId');
}).catch(error => {
console.error('Error fetching scenario:', error);
localStorage.removeItem('pendingScenarioId');
});
}
}, [projectId, beginSimulation]);
if (hidden) {
return <></>;
}
function handleSimulateButtonClick() {
router.push(`/projects/${projectId}/simulation`);
2025-01-13 15:31:31 +05:30
}
function handleNewChatButtonClick() {
2025-01-13 15:31:31 +05:30
setExistingChatId(null);
setViewSimulationMenu(false);
setCounter(counter + 1);
setChat({
projectId,
createdAt: new Date().toISOString(),
messages: [],
simulated: false,
systemMessage: defaultSystemMessage,
2025-01-13 15:31:31 +05:30
});
}
return (
<Pane title={viewSimulationMenu ? <SimulateLabel /> : "Chat"} actions={[
<ActionButton
key="new-chat"
icon={<MessageSquarePlusIcon size={16} />}
onClick={handleNewChatButtonClick}
>
New chat
</ActionButton>,
<ActionButton
key="simulate"
icon={<PlayIcon size={16} />}
onClick={handleSimulateButtonClick}
>
Simulate
</ActionButton>,
]}>
<div className="h-full overflow-auto">
{!viewSimulationMenu && loadingChat && <div className="flex justify-center items-center h-full">
<Spinner />
</div>}
{!viewSimulationMenu && !loadingChat && <Chat
key={existingChatId || 'chat-' + counter}
chat={chat}
initialChatId={existingChatId || null}
projectId={projectId}
workflow={workflow}
messageSubscriber={messageSubscriber}
/>}
{viewSimulationMenu && <SimulateScenarioOption beginSimulation={beginSimulation} projectId={projectId} />}
</div>
</Pane>
);
2025-01-13 15:31:31 +05:30
}