|
| 1 | +import React, { useEffect, useId, useState } from "react"; |
| 2 | +import styled from "@emotion/styled"; |
| 3 | +import { Modal, ModalInfo, ModalActions, CancelButton, PrimaryButton } from "./Modal"; |
| 4 | +import { formatCompactCommand } from "@/utils/chatCommands"; |
| 5 | + |
| 6 | +const FormGroup = styled.div` |
| 7 | + margin-bottom: 20px; |
| 8 | +
|
| 9 | + label { |
| 10 | + display: block; |
| 11 | + margin-bottom: 8px; |
| 12 | + color: #ccc; |
| 13 | + font-size: 14px; |
| 14 | + } |
| 15 | +
|
| 16 | + input, |
| 17 | + select { |
| 18 | + width: 100%; |
| 19 | + padding: 8px 12px; |
| 20 | + background: #2d2d2d; |
| 21 | + border: 1px solid #444; |
| 22 | + border-radius: 4px; |
| 23 | + color: #fff; |
| 24 | + font-size: 14px; |
| 25 | +
|
| 26 | + &:focus { |
| 27 | + outline: none; |
| 28 | + border-color: #007acc; |
| 29 | + } |
| 30 | +
|
| 31 | + &:disabled { |
| 32 | + opacity: 0.6; |
| 33 | + cursor: not-allowed; |
| 34 | + } |
| 35 | + } |
| 36 | +
|
| 37 | + select { |
| 38 | + cursor: pointer; |
| 39 | +
|
| 40 | + option { |
| 41 | + background: #2d2d2d; |
| 42 | + color: #fff; |
| 43 | + } |
| 44 | + } |
| 45 | +`; |
| 46 | + |
| 47 | +const HelpText = styled.div` |
| 48 | + color: #888; |
| 49 | + font-size: 12px; |
| 50 | + margin-top: 4px; |
| 51 | +`; |
| 52 | + |
| 53 | +const CommandDisplay = styled.div` |
| 54 | + margin-top: 20px; |
| 55 | + padding: 12px; |
| 56 | + background: #1e1e1e; |
| 57 | + border: 1px solid #3e3e42; |
| 58 | + border-radius: 4px; |
| 59 | + font-family: "Menlo", "Monaco", "Courier New", monospace; |
| 60 | + font-size: 13px; |
| 61 | + color: #d4d4d4; |
| 62 | + white-space: pre-wrap; |
| 63 | + word-break: break-all; |
| 64 | +`; |
| 65 | + |
| 66 | +const CommandLabel = styled.div` |
| 67 | + font-size: 12px; |
| 68 | + color: #888; |
| 69 | + margin-bottom: 8px; |
| 70 | + font-family: |
| 71 | + system-ui, |
| 72 | + -apple-system, |
| 73 | + sans-serif; |
| 74 | +`; |
| 75 | + |
| 76 | +interface CompactModalProps { |
| 77 | + isOpen: boolean; |
| 78 | + onClose: () => void; |
| 79 | + onCompact: (maxOutputTokens?: number, model?: string) => Promise<void>; |
| 80 | +} |
| 81 | + |
| 82 | +const CompactModal: React.FC<CompactModalProps> = ({ isOpen, onClose, onCompact }) => { |
| 83 | + const [maxOutputTokens, setMaxOutputTokens] = useState<string>(""); |
| 84 | + const [model, setModel] = useState<string>(""); |
| 85 | + const [isLoading, setIsLoading] = useState<boolean>(false); |
| 86 | + const infoId = useId(); |
| 87 | + |
| 88 | + // Reset form when modal opens |
| 89 | + useEffect(() => { |
| 90 | + if (isOpen) { |
| 91 | + setMaxOutputTokens(""); |
| 92 | + setModel(""); |
| 93 | + setIsLoading(false); |
| 94 | + } |
| 95 | + }, [isOpen]); |
| 96 | + |
| 97 | + const handleCancel = () => { |
| 98 | + if (!isLoading) { |
| 99 | + onClose(); |
| 100 | + } |
| 101 | + }; |
| 102 | + |
| 103 | + const handleSubmit = async (event: React.FormEvent) => { |
| 104 | + event.preventDefault(); |
| 105 | + |
| 106 | + setIsLoading(true); |
| 107 | + |
| 108 | + try { |
| 109 | + const tokens = maxOutputTokens.trim() ? parseInt(maxOutputTokens.trim(), 10) : undefined; |
| 110 | + const modelParam = model.trim() || undefined; |
| 111 | + |
| 112 | + await onCompact(tokens, modelParam); |
| 113 | + setMaxOutputTokens(""); |
| 114 | + setModel(""); |
| 115 | + onClose(); |
| 116 | + } catch (err) { |
| 117 | + console.error("Compact failed:", err); |
| 118 | + // Error handling is done by the parent component |
| 119 | + } finally { |
| 120 | + setIsLoading(false); |
| 121 | + } |
| 122 | + }; |
| 123 | + |
| 124 | + const tokensValue = maxOutputTokens.trim() ? parseInt(maxOutputTokens.trim(), 10) : undefined; |
| 125 | + const modelValue = model.trim() || undefined; |
| 126 | + |
| 127 | + return ( |
| 128 | + <Modal |
| 129 | + isOpen={isOpen} |
| 130 | + title="Compact Conversation" |
| 131 | + subtitle="Summarize conversation history into a compact form" |
| 132 | + onClose={handleCancel} |
| 133 | + isLoading={isLoading} |
| 134 | + describedById={infoId} |
| 135 | + > |
| 136 | + <form onSubmit={(event) => void handleSubmit(event)}> |
| 137 | + <FormGroup> |
| 138 | + <label htmlFor="maxOutputTokens">Max Output Tokens (optional):</label> |
| 139 | + <input |
| 140 | + id="maxOutputTokens" |
| 141 | + type="number" |
| 142 | + value={maxOutputTokens} |
| 143 | + onChange={(event) => setMaxOutputTokens(event.target.value)} |
| 144 | + disabled={isLoading} |
| 145 | + placeholder="e.g., 3000" |
| 146 | + min="100" |
| 147 | + /> |
| 148 | + <HelpText> |
| 149 | + Controls the length of the summary. Leave empty for default (~2000 words). |
| 150 | + </HelpText> |
| 151 | + </FormGroup> |
| 152 | + |
| 153 | + <FormGroup> |
| 154 | + <label htmlFor="model">Model (optional):</label> |
| 155 | + <input |
| 156 | + id="model" |
| 157 | + type="text" |
| 158 | + value={model} |
| 159 | + onChange={(event) => setModel(event.target.value)} |
| 160 | + disabled={isLoading} |
| 161 | + placeholder="e.g., claude-3-5-sonnet-20241022" |
| 162 | + /> |
| 163 | + <HelpText>Specify a model for compaction. Leave empty to use current model.</HelpText> |
| 164 | + </FormGroup> |
| 165 | + |
| 166 | + <ModalInfo id={infoId}> |
| 167 | + <p> |
| 168 | + Compaction will summarize your conversation history, allowing you to continue with a |
| 169 | + shorter context window. The AI will create a compact version that preserves important |
| 170 | + information for future interactions. |
| 171 | + </p> |
| 172 | + </ModalInfo> |
| 173 | + |
| 174 | + <div> |
| 175 | + <CommandLabel>Equivalent command:</CommandLabel> |
| 176 | + <CommandDisplay>{formatCompactCommand(tokensValue, modelValue)}</CommandDisplay> |
| 177 | + </div> |
| 178 | + |
| 179 | + <ModalActions> |
| 180 | + <CancelButton type="button" onClick={handleCancel} disabled={isLoading}> |
| 181 | + Cancel |
| 182 | + </CancelButton> |
| 183 | + <PrimaryButton type="submit" disabled={isLoading}> |
| 184 | + {isLoading ? "Compacting..." : "Start Compaction"} |
| 185 | + </PrimaryButton> |
| 186 | + </ModalActions> |
| 187 | + </form> |
| 188 | + </Modal> |
| 189 | + ); |
| 190 | +}; |
| 191 | + |
| 192 | +export default CompactModal; |
0 commit comments