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