import { Button } from "@/components/ui/button"; import JsonView from "./JsonView"; import { useMemo, useState } from "react"; import { CreateMessageResult, CreateMessageResultSchema, } from "@modelcontextprotocol/sdk/types.js"; import { PendingRequest } from "./SamplingTab"; import DynamicJsonForm from "./DynamicJsonForm"; import { useToast } from "@/lib/hooks/useToast"; import { JsonSchemaType, JsonValue } from "@/utils/jsonUtils"; export type SamplingRequestProps = { request: PendingRequest; onApprove: (id: number, result: CreateMessageResult) => void; onReject: (id: number) => void; }; const SamplingRequest = ({ onApprove, request, onReject, }: SamplingRequestProps) => { const { toast } = useToast(); const [messageResult, setMessageResult] = useState({ model: "stub-model", stopReason: "endTurn", role: "assistant", content: { type: "text", text: "", }, }); const contentType = ( (messageResult as { [key: string]: JsonValue })?.content as { [key: string]: JsonValue; } )?.type; const schema = useMemo(() => { const s: JsonSchemaType = { type: "object", description: "Message result", properties: { model: { type: "string", default: "stub-model", description: "model name", }, stopReason: { type: "string", default: "endTurn", description: "Stop reason", }, role: { type: "string", default: "endTurn", description: "Role of the model", }, content: { type: "object", properties: { type: { type: "string", default: "text", description: "Type of content", }, }, }, }, }; if (contentType === "text" && s.properties) { s.properties.content.properties = { ...s.properties.content.properties, text: { type: "string", default: "", description: "text content", }, }; setMessageResult((prev) => ({ ...(prev as { [key: string]: JsonValue }), content: { type: contentType, text: "", }, })); } else if (contentType === "image" && s.properties) { s.properties.content.properties = { ...s.properties.content.properties, data: { type: "string", default: "", description: "Base64 encoded image data", }, mimeType: { type: "string", default: "", description: "Mime type of the image", }, }; setMessageResult((prev) => ({ ...(prev as { [key: string]: JsonValue }), content: { type: contentType, data: "", mimeType: "", }, })); } return s; }, [contentType]); const handleApprove = (id: number) => { const validationResult = CreateMessageResultSchema.safeParse(messageResult); if (!validationResult.success) { toast({ title: "Error", description: `There was an error validating the message result: ${validationResult.error.message}`, variant: "destructive", }); return; } onApprove(id, validationResult.data); }; return (
{ setMessageResult(newValue); }} />
); }; export default SamplingRequest;