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