import "./index.css"; import { v4 as uuidv4 } from "uuid"; import { Snapshot } from "../../../types"; import { Button } from "@/components/ui/button"; import { useEffect, useState } from "react"; import { Input } from "@/components/ui/input"; import { UIMessage, useStreamContext } from "@langchain/langgraph-sdk/react-ui"; import { Message } from "@langchain/langgraph-sdk"; import { getToolResponse } from "agent/uis/utils/get-tool-response"; import { DO_NOT_RENDER_ID_PREFIX } from "@/lib/ensure-tool-responses"; function Purchased({ ticker, quantity, price, }: { ticker: string; quantity: number; price: number; }) { return (

Purchase Executed - {ticker}

Number of Shares

Market Price

Total Cost

{quantity}

${price}

${(quantity * price).toFixed(2)}

); } export default function BuyStock(props: { toolCallId: string; snapshot: Snapshot; quantity: number; }) { const { snapshot, toolCallId } = props; const [quantity, setQuantity] = useState(props.quantity); const [finalPurchase, setFinalPurchase] = useState<{ ticker: string; quantity: number; price: number; }>(); const thread = useStreamContext< { messages: Message[]; ui: UIMessage[] }, { MetaType: { ui: UIMessage | undefined } } >(); useEffect(() => { if (typeof window === "undefined" || finalPurchase) return; const toolResponse = getToolResponse(toolCallId, thread); if (toolResponse) { try { const parsedContent: { purchaseDetails: { ticker: string; quantity: number; price: number; }; } = JSON.parse(toolResponse.content as string); setFinalPurchase(parsedContent.purchaseDetails); } catch { console.error("Failed to parse tool response content."); } } }, []); function handleBuyStock() { const orderDetails = { message: "Successfully purchased stock", purchaseDetails: { ticker: snapshot.ticker, quantity: quantity, price: snapshot.price, }, }; thread.submit({ messages: [ { type: "tool", tool_call_id: toolCallId, id: `${DO_NOT_RENDER_ID_PREFIX}${uuidv4()}`, name: "buy-stock", content: JSON.stringify(orderDetails), }, { type: "human", content: `Purchased ${quantity} shares of ${snapshot.ticker} at ${snapshot.price} per share`, }, ], }); setFinalPurchase(orderDetails.purchaseDetails); } if (finalPurchase) { return ; } return (

Buy {snapshot.ticker}

Number of Shares

Market Price

Total Cost

setQuantity(Number(e.target.value))} min={1} />

${snapshot.price}

${(quantity * snapshot.price).toFixed(2)}

); }