| 1 | import { Box, Text } from "ink"; |
| 2 | import React from "react"; |
| 3 | |
| 4 | import { getToolDisplayName } from "../../tools/index.js"; |
| 5 | import { ToolCallPreview } from "../../tools/types.js"; |
| 6 | import { ColoredDiff } from "../ColoredDiff.js"; |
| 7 | |
| 8 | import { ChecklistDisplay } from "./ChecklistDisplay.js"; |
| 9 | |
| 10 | interface ToolPreviewProps { |
| 11 | toolCallPreview?: ToolCallPreview[]; |
| 12 | toolName: string; |
| 13 | toolArgs: any; |
| 14 | } |
| 15 | |
| 16 | /** |
| 17 | * Generates a preview of what a tool will do based on its arguments |
| 18 | */ |
| 19 | export const ToolPreview: React.FC<ToolPreviewProps> = ({ |
| 20 | toolCallPreview, |
| 21 | toolName, |
| 22 | toolArgs, |
| 23 | }) => { |
| 24 | if (toolCallPreview) { |
| 25 | return ( |
| 26 | <Box display={"flex"} flexDirection="column" marginTop={1}> |
| 27 | {toolCallPreview.map((preview, index) => { |
| 28 | if (preview.type === "text") { |
| 29 | return ( |
| 30 | <Box key={index} paddingLeft={preview.paddingLeft ?? 0}> |
| 31 | {index === 0 && <Text color="dim">⎿ </Text>} |
| 32 | <Text color={preview.color ?? "dim"}>{preview.content}</Text> |
| 33 | </Box> |
| 34 | ); |
| 35 | } else if (preview.type === "checklist") { |
| 36 | return ( |
| 37 | <Box key={index}> |
| 38 | <ChecklistDisplay |
| 39 | content={`Task list status:\n${preview.content}`} |
| 40 | /> |
| 41 | </Box> |
| 42 | ); |
| 43 | } else { |
| 44 | return <ColoredDiff key={index} diffContent={preview.content} />; |
| 45 | } |
| 46 | })} |
| 47 | </Box> |
| 48 | ); |
| 49 | } |
| 50 | |
| 51 | let message = `Will call ${getToolDisplayName(toolName)}`; |
| 52 | const argCount = Object.keys(toolArgs || {}).length; |
| 53 | if (argCount > 0) { |
| 54 | message += ` with ${argCount} argument${argCount === 1 ? "" : "s"}`; |
| 55 | } |
| 56 | |
| 57 | return ( |
| 58 | <Box> |
| 59 | <Text color="dim">⎿ </Text> |
| 60 | <Text color="dim">{message}</Text> |
| 61 | </Box> |
| 62 | ); |
| 63 | }; |