|
| 1 | +import { Button } from "@/components/ui/button"; |
| 2 | +import { Card, CardContent, CardFooter } from "@/components/ui/card"; |
| 3 | +import { JSONValue, useChatUI } from "@llamaindex/chat-ui"; |
| 4 | +import React, { FC, useState } from "react"; |
| 5 | +import { z } from "zod"; |
| 6 | + |
| 7 | +// This schema is equivalent to the CLICommand model defined in events.py |
| 8 | +const CLIInputEventSchema = z.object({ |
| 9 | + command: z.string(), |
| 10 | +}); |
| 11 | +type CLIInputEvent = z.infer<typeof CLIInputEventSchema>; |
| 12 | + |
| 13 | +const CLIHumanInput: FC<{ |
| 14 | + events: JSONValue[]; |
| 15 | +}> = ({ events }) => { |
| 16 | + const inputEvent = (events || []) |
| 17 | + .map((ev) => { |
| 18 | + const parseResult = CLIInputEventSchema.safeParse(ev); |
| 19 | + return parseResult.success ? parseResult.data : null; |
| 20 | + }) |
| 21 | + .filter((ev): ev is CLIInputEvent => ev !== null) |
| 22 | + .at(-1); |
| 23 | + |
| 24 | + const { append } = useChatUI(); |
| 25 | + const [confirmedValue, setConfirmedValue] = useState<boolean | null>(null); |
| 26 | + const [editableCommand, setEditableCommand] = useState<string | undefined>( |
| 27 | + inputEvent?.command, |
| 28 | + ); |
| 29 | + |
| 30 | + // Update editableCommand if inputEvent changes (e.g. new event comes in) |
| 31 | + React.useEffect(() => { |
| 32 | + setEditableCommand(inputEvent?.command); |
| 33 | + }, [inputEvent?.command]); |
| 34 | + |
| 35 | + const handleConfirm = () => { |
| 36 | + append({ |
| 37 | + content: "Yes", |
| 38 | + role: "user", |
| 39 | + annotations: [ |
| 40 | + { |
| 41 | + type: "human_response", |
| 42 | + data: { |
| 43 | + execute: true, |
| 44 | + command: editableCommand, // Use editable command |
| 45 | + }, |
| 46 | + }, |
| 47 | + ], |
| 48 | + }); |
| 49 | + setConfirmedValue(true); |
| 50 | + }; |
| 51 | + |
| 52 | + const handleCancel = () => { |
| 53 | + append({ |
| 54 | + content: "No", |
| 55 | + role: "user", |
| 56 | + annotations: [ |
| 57 | + { |
| 58 | + type: "human_response", |
| 59 | + data: { |
| 60 | + execute: false, |
| 61 | + command: inputEvent?.command, |
| 62 | + }, |
| 63 | + }, |
| 64 | + ], |
| 65 | + }); |
| 66 | + setConfirmedValue(false); |
| 67 | + }; |
| 68 | + |
| 69 | + return ( |
| 70 | + <Card className="my-4"> |
| 71 | + <CardContent className="pt-6"> |
| 72 | + <p className="text-sm text-gray-700"> |
| 73 | + Do you want to execute the following command? |
| 74 | + </p> |
| 75 | + <input |
| 76 | + disabled |
| 77 | + type="text" |
| 78 | + value={editableCommand || ""} |
| 79 | + onChange={(e) => setEditableCommand(e.target.value)} |
| 80 | + className="my-2 w-full overflow-x-auto rounded border border-gray-300 bg-gray-100 p-3 font-mono text-xs text-gray-800" |
| 81 | + /> |
| 82 | + </CardContent> |
| 83 | + {confirmedValue === null ? ( |
| 84 | + <CardFooter className="flex justify-end gap-2"> |
| 85 | + <> |
| 86 | + <Button onClick={handleConfirm}>Yes</Button> |
| 87 | + <Button onClick={handleCancel}>No</Button> |
| 88 | + </> |
| 89 | + </CardFooter> |
| 90 | + ) : null} |
| 91 | + </Card> |
| 92 | + ); |
| 93 | +}; |
| 94 | + |
| 95 | +export default CLIHumanInput; |
0 commit comments