diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 9621e31..e292dae 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -26,6 +26,10 @@ jobs: # - run: npm ci - run: npm install --no-package-lock + - name: Check linting + working-directory: ./client + run: npm run lint + - name: Run client tests working-directory: ./client run: npm test diff --git a/client/src/components/DynamicJsonForm.tsx b/client/src/components/DynamicJsonForm.tsx index fa04467..6a5993c 100644 --- a/client/src/components/DynamicJsonForm.tsx +++ b/client/src/components/DynamicJsonForm.tsx @@ -250,7 +250,12 @@ const DynamicJsonForm = ({
{isJsonMode && ( - )} diff --git a/client/src/components/SamplingRequest.tsx b/client/src/components/SamplingRequest.tsx new file mode 100644 index 0000000..2fc22d9 --- /dev/null +++ b/client/src/components/SamplingRequest.tsx @@ -0,0 +1,167 @@ +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 "@/hooks/use-toast"; +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; diff --git a/client/src/components/SamplingTab.tsx b/client/src/components/SamplingTab.tsx index d7d0212..c8ed9dc 100644 --- a/client/src/components/SamplingTab.tsx +++ b/client/src/components/SamplingTab.tsx @@ -1,11 +1,10 @@ import { Alert, AlertDescription } from "@/components/ui/alert"; -import { Button } from "@/components/ui/button"; import { TabsContent } from "@/components/ui/tabs"; import { CreateMessageRequest, CreateMessageResult, } from "@modelcontextprotocol/sdk/types.js"; -import JsonView from "./JsonView"; +import SamplingRequest from "./SamplingRequest"; export type PendingRequest = { id: number; @@ -19,19 +18,6 @@ export type Props = { }; const SamplingTab = ({ pendingRequests, onApprove, onReject }: Props) => { - const handleApprove = (id: number) => { - // For now, just return a stub response - onApprove(id, { - model: "stub-model", - stopReason: "endTurn", - role: "assistant", - content: { - type: "text", - text: "This is a stub response.", - }, - }); - }; - return (
@@ -44,21 +30,12 @@ const SamplingTab = ({ pendingRequests, onApprove, onReject }: Props) => {

Recent Requests

{pendingRequests.map((request) => ( -
- - -
- - -
-
+ ))} {pendingRequests.length === 0 && (

No pending requests

diff --git a/client/src/components/__tests__/samplingRequest.test.tsx b/client/src/components/__tests__/samplingRequest.test.tsx new file mode 100644 index 0000000..80d87d9 --- /dev/null +++ b/client/src/components/__tests__/samplingRequest.test.tsx @@ -0,0 +1,73 @@ +import { render, screen, fireEvent } from "@testing-library/react"; +import SamplingRequest from "../SamplingRequest"; +import { PendingRequest } from "../SamplingTab"; + +const mockRequest: PendingRequest = { + id: 1, + request: { + method: "sampling/createMessage", + params: { + messages: [ + { + role: "user", + content: { + type: "text", + text: "What files are in the current directory?", + }, + }, + ], + systemPrompt: "You are a helpful file system assistant.", + includeContext: "thisServer", + maxTokens: 100, + }, + }, +}; + +describe("Form to handle sampling response", () => { + const mockOnApprove = jest.fn(); + const mockOnReject = jest.fn(); + + afterEach(() => { + jest.clearAllMocks(); + }); + + it("should call onApprove with correct text content when Approve button is clicked", () => { + render( + , + ); + + // Click the Approve button + fireEvent.click(screen.getByRole("button", { name: /approve/i })); + + // Assert that onApprove is called with the correct arguments + expect(mockOnApprove).toHaveBeenCalledWith(mockRequest.id, { + model: "stub-model", + stopReason: "endTurn", + role: "assistant", + content: { + type: "text", + text: "", + }, + }); + }); + + it("should call onReject with correct request id when Reject button is clicked", () => { + render( + , + ); + + // Click the Approve button + fireEvent.click(screen.getByRole("button", { name: /Reject/i })); + + // Assert that onApprove is called with the correct arguments + expect(mockOnReject).toHaveBeenCalledWith(mockRequest.id); + }); +}); diff --git a/client/src/components/__tests__/samplingTab.test.tsx b/client/src/components/__tests__/samplingTab.test.tsx new file mode 100644 index 0000000..3e72121 --- /dev/null +++ b/client/src/components/__tests__/samplingTab.test.tsx @@ -0,0 +1,55 @@ +import { render, screen } from "@testing-library/react"; +import { Tabs } from "@/components/ui/tabs"; +import SamplingTab, { PendingRequest } from "../SamplingTab"; + +describe("Sampling tab", () => { + const mockOnApprove = jest.fn(); + const mockOnReject = jest.fn(); + + const renderSamplingTab = (pendingRequests: PendingRequest[]) => + render( + + + , + ); + + it("should render 'No pending requests' when there are no pending requests", () => { + renderSamplingTab([]); + expect( + screen.getByText( + "When the server requests LLM sampling, requests will appear here for approval.", + ), + ).toBeTruthy(); + expect(screen.findByText("No pending requests")).toBeTruthy(); + }); + + it("should render the correct number of requests", () => { + renderSamplingTab( + Array.from({ length: 5 }, (_, i) => ({ + id: i, + request: { + method: "sampling/createMessage", + params: { + messages: [ + { + role: "user", + content: { + type: "text", + text: "What files are in the current directory?", + }, + }, + ], + systemPrompt: "You are a helpful file system assistant.", + includeContext: "thisServer", + maxTokens: 100, + }, + }, + })), + ); + expect(screen.getAllByTestId("sampling-request").length).toBe(5); + }); +});