|
| 1 | +import { Button } from "@/components/ui/button"; |
| 2 | +import JsonView from "./JsonView"; |
| 3 | +import { useMemo, useState } from "react"; |
| 4 | +import { |
| 5 | + CreateMessageResult, |
| 6 | + CreateMessageResultSchema, |
| 7 | +} from "@modelcontextprotocol/sdk/types.js"; |
| 8 | +import { PendingRequest } from "./SamplingTab"; |
| 9 | +import DynamicJsonForm from "./DynamicJsonForm"; |
| 10 | +import { useToast } from "@/hooks/use-toast"; |
| 11 | +import { JsonSchemaType, JsonValue } from "@/utils/jsonUtils"; |
| 12 | + |
| 13 | +export type SamplingRequestProps = { |
| 14 | + request: PendingRequest; |
| 15 | + onApprove: (id: number, result: CreateMessageResult) => void; |
| 16 | + onReject: (id: number) => void; |
| 17 | +}; |
| 18 | + |
| 19 | +const SamplingRequest = ({ |
| 20 | + onApprove, |
| 21 | + request, |
| 22 | + onReject, |
| 23 | +}: SamplingRequestProps) => { |
| 24 | + const { toast } = useToast(); |
| 25 | + |
| 26 | + const [messageResult, setMessageResult] = useState<JsonValue>({ |
| 27 | + model: "stub-model", |
| 28 | + stopReason: "endTurn", |
| 29 | + role: "assistant", |
| 30 | + content: { |
| 31 | + type: "text", |
| 32 | + text: "", |
| 33 | + }, |
| 34 | + }); |
| 35 | + |
| 36 | + const contentType = ( |
| 37 | + (messageResult as { [key: string]: JsonValue })?.content as { |
| 38 | + [key: string]: JsonValue; |
| 39 | + } |
| 40 | + )?.type; |
| 41 | + |
| 42 | + const schema = useMemo(() => { |
| 43 | + const s: JsonSchemaType = { |
| 44 | + type: "object", |
| 45 | + description: "Message result", |
| 46 | + properties: { |
| 47 | + model: { |
| 48 | + type: "string", |
| 49 | + default: "stub-model", |
| 50 | + description: "model name", |
| 51 | + }, |
| 52 | + stopReason: { |
| 53 | + type: "string", |
| 54 | + default: "endTurn", |
| 55 | + description: "Stop reason", |
| 56 | + }, |
| 57 | + role: { |
| 58 | + type: "string", |
| 59 | + default: "endTurn", |
| 60 | + description: "Role of the model", |
| 61 | + }, |
| 62 | + content: { |
| 63 | + type: "object", |
| 64 | + properties: { |
| 65 | + type: { |
| 66 | + type: "string", |
| 67 | + default: "text", |
| 68 | + description: "Type of content", |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + }; |
| 74 | + |
| 75 | + if (contentType === "text" && s.properties) { |
| 76 | + s.properties.content.properties = { |
| 77 | + ...s.properties.content.properties, |
| 78 | + text: { |
| 79 | + type: "string", |
| 80 | + default: "", |
| 81 | + description: "text content", |
| 82 | + }, |
| 83 | + }; |
| 84 | + setMessageResult((prev) => ({ |
| 85 | + ...(prev as { [key: string]: JsonValue }), |
| 86 | + content: { |
| 87 | + type: contentType, |
| 88 | + text: "", |
| 89 | + }, |
| 90 | + })); |
| 91 | + } else if (contentType === "image" && s.properties) { |
| 92 | + s.properties.content.properties = { |
| 93 | + ...s.properties.content.properties, |
| 94 | + data: { |
| 95 | + type: "string", |
| 96 | + default: "", |
| 97 | + description: "Base64 encoded image data", |
| 98 | + }, |
| 99 | + mimeType: { |
| 100 | + type: "string", |
| 101 | + default: "", |
| 102 | + description: "Mime type of the image", |
| 103 | + }, |
| 104 | + }; |
| 105 | + setMessageResult((prev) => ({ |
| 106 | + ...(prev as { [key: string]: JsonValue }), |
| 107 | + content: { |
| 108 | + type: contentType, |
| 109 | + data: "", |
| 110 | + mimeType: "", |
| 111 | + }, |
| 112 | + })); |
| 113 | + } |
| 114 | + |
| 115 | + return s; |
| 116 | + }, [contentType]); |
| 117 | + |
| 118 | + const handleApprove = (id: number) => { |
| 119 | + const validationResult = CreateMessageResultSchema.safeParse(messageResult); |
| 120 | + if (!validationResult.success) { |
| 121 | + toast({ |
| 122 | + title: "Error", |
| 123 | + description: `There was an error validating the message result: ${validationResult.error.message}`, |
| 124 | + variant: "destructive", |
| 125 | + }); |
| 126 | + return; |
| 127 | + } |
| 128 | + |
| 129 | + onApprove(id, validationResult.data); |
| 130 | + }; |
| 131 | + |
| 132 | + return ( |
| 133 | + <div |
| 134 | + data-testid="sampling-request" |
| 135 | + className="flex gap-4 p-4 border rounded-lg space-y-4" |
| 136 | + > |
| 137 | + <div className="flex-1 bg-gray-50 dark:bg-gray-800 dark:text-gray-100 p-2 rounded"> |
| 138 | + <JsonView data={JSON.stringify(request.request)} /> |
| 139 | + </div> |
| 140 | + <form className="flex-1 space-y-4"> |
| 141 | + <div className="space-y-2"> |
| 142 | + <DynamicJsonForm |
| 143 | + schema={schema} |
| 144 | + value={messageResult} |
| 145 | + onChange={(newValue: JsonValue) => { |
| 146 | + setMessageResult(newValue); |
| 147 | + }} |
| 148 | + /> |
| 149 | + </div> |
| 150 | + <div className="flex space-x-2 mt-1"> |
| 151 | + <Button type="button" onClick={() => handleApprove(request.id)}> |
| 152 | + Approve |
| 153 | + </Button> |
| 154 | + <Button |
| 155 | + type="button" |
| 156 | + variant="outline" |
| 157 | + onClick={() => onReject(request.id)} |
| 158 | + > |
| 159 | + Reject |
| 160 | + </Button> |
| 161 | + </div> |
| 162 | + </form> |
| 163 | + </div> |
| 164 | + ); |
| 165 | +}; |
| 166 | + |
| 167 | +export default SamplingRequest; |
0 commit comments