Autocompact threshold UI cleanup (#5232)
This commit is contained in:
@@ -28,7 +28,6 @@ import { DroppedFile, useFileDrop } from '../hooks/useFileDrop';
|
|||||||
import { Recipe } from '../recipe';
|
import { Recipe } from '../recipe';
|
||||||
import MessageQueue from './MessageQueue';
|
import MessageQueue from './MessageQueue';
|
||||||
import { detectInterruption } from '../utils/interruptionDetector';
|
import { detectInterruption } from '../utils/interruptionDetector';
|
||||||
import { getApiUrl } from '../config';
|
|
||||||
|
|
||||||
interface QueuedMessage {
|
interface QueuedMessage {
|
||||||
id: string;
|
id: string;
|
||||||
@@ -502,52 +501,19 @@ export default function ChatInput({
|
|||||||
// Load auto-compact threshold
|
// Load auto-compact threshold
|
||||||
const loadAutoCompactThreshold = useCallback(async () => {
|
const loadAutoCompactThreshold = useCallback(async () => {
|
||||||
try {
|
try {
|
||||||
const secretKey = await window.electron.getSecretKey();
|
const threshold = await read('GOOSE_AUTO_COMPACT_THRESHOLD', false);
|
||||||
const response = await fetch(getApiUrl('/config/read'), {
|
if (threshold !== undefined && threshold !== null) {
|
||||||
method: 'POST',
|
setAutoCompactThreshold(threshold as number);
|
||||||
headers: {
|
|
||||||
'X-Secret-Key': secretKey,
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
key: 'GOOSE_AUTO_COMPACT_THRESHOLD',
|
|
||||||
is_secret: false,
|
|
||||||
}),
|
|
||||||
});
|
|
||||||
if (response.ok) {
|
|
||||||
const data = await response.json();
|
|
||||||
console.log('Loaded auto-compact threshold from config:', data);
|
|
||||||
if (data !== undefined && data !== null) {
|
|
||||||
setAutoCompactThreshold(data);
|
|
||||||
console.log('Set auto-compact threshold to:', data);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
console.error('Failed to fetch auto-compact threshold, status:', response.status);
|
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error('Error fetching auto-compact threshold:', err);
|
console.error('Error fetching auto-compact threshold:', err);
|
||||||
}
|
}
|
||||||
}, []);
|
}, [read]);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
loadAutoCompactThreshold();
|
loadAutoCompactThreshold();
|
||||||
}, [loadAutoCompactThreshold]);
|
}, [loadAutoCompactThreshold]);
|
||||||
|
|
||||||
// Listen for threshold change events from AlertBox
|
|
||||||
useEffect(() => {
|
|
||||||
const handleThresholdChange = (event: CustomEvent<{ threshold: number }>) => {
|
|
||||||
setAutoCompactThreshold(event.detail.threshold);
|
|
||||||
};
|
|
||||||
|
|
||||||
// Type assertion to handle the mismatch between CustomEvent and EventListener
|
|
||||||
const eventListener = handleThresholdChange as (event: globalThis.Event) => void;
|
|
||||||
window.addEventListener('autoCompactThresholdChanged', eventListener);
|
|
||||||
|
|
||||||
return () => {
|
|
||||||
window.removeEventListener('autoCompactThresholdChanged', eventListener);
|
|
||||||
};
|
|
||||||
}, []);
|
|
||||||
|
|
||||||
// Handle tool count alerts and token usage
|
// Handle tool count alerts and token usage
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
clearAlerts();
|
clearAlerts();
|
||||||
@@ -574,6 +540,9 @@ export default function ChatInput({
|
|||||||
},
|
},
|
||||||
compactIcon: <ScrollText size={12} />,
|
compactIcon: <ScrollText size={12} />,
|
||||||
autoCompactThreshold: autoCompactThreshold,
|
autoCompactThreshold: autoCompactThreshold,
|
||||||
|
onThresholdChange: (newThreshold: number) => {
|
||||||
|
setAutoCompactThreshold(newThreshold);
|
||||||
|
},
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -3,7 +3,7 @@ import { IoIosCloseCircle, IoIosWarning, IoIosInformationCircle } from 'react-ic
|
|||||||
import { FaPencilAlt, FaSave } from 'react-icons/fa';
|
import { FaPencilAlt, FaSave } from 'react-icons/fa';
|
||||||
import { cn } from '../../utils';
|
import { cn } from '../../utils';
|
||||||
import { Alert, AlertType } from './types';
|
import { Alert, AlertType } from './types';
|
||||||
import { getApiUrl } from '../../config';
|
import { upsertConfig } from '../../api';
|
||||||
|
|
||||||
const alertIcons: Record<AlertType, React.ReactNode> = {
|
const alertIcons: Record<AlertType, React.ReactNode> = {
|
||||||
[AlertType.Error]: <IoIosCloseCircle className="h-5 w-5" />,
|
[AlertType.Error]: <IoIosCloseCircle className="h-5 w-5" />,
|
||||||
@@ -42,41 +42,21 @@ export const AlertBox = ({ alert, className }: AlertBoxProps) => {
|
|||||||
setIsSaving(true);
|
setIsSaving(true);
|
||||||
try {
|
try {
|
||||||
const newThreshold = validThreshold / 100; // Convert percentage to decimal
|
const newThreshold = validThreshold / 100; // Convert percentage to decimal
|
||||||
console.log('Saving auto-compact threshold:', newThreshold);
|
|
||||||
|
|
||||||
// Update the configuration via the upsert API
|
await upsertConfig({
|
||||||
const response = await fetch(getApiUrl('/config/upsert'), {
|
body: {
|
||||||
method: 'POST',
|
|
||||||
headers: {
|
|
||||||
'Content-Type': 'application/json',
|
|
||||||
'X-Secret-Key': await window.electron.getSecretKey(),
|
|
||||||
},
|
|
||||||
body: JSON.stringify({
|
|
||||||
key: 'GOOSE_AUTO_COMPACT_THRESHOLD',
|
key: 'GOOSE_AUTO_COMPACT_THRESHOLD',
|
||||||
value: newThreshold,
|
value: newThreshold,
|
||||||
is_secret: false,
|
is_secret: false,
|
||||||
}),
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!response.ok) {
|
|
||||||
const errorText = await response.text();
|
|
||||||
throw new Error(`Failed to update threshold: ${errorText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
const responseText = await response.text();
|
|
||||||
console.log('Threshold save response:', responseText);
|
|
||||||
|
|
||||||
setIsEditingThreshold(false);
|
setIsEditingThreshold(false);
|
||||||
|
|
||||||
// Dispatch a custom event to notify other components that the threshold has changed
|
// Notify parent component of the threshold change
|
||||||
// This allows ChatInput to reload the threshold without a page reload
|
if (alert.onThresholdChange) {
|
||||||
window.dispatchEvent(
|
alert.onThresholdChange(newThreshold);
|
||||||
new CustomEvent('autoCompactThresholdChanged', {
|
}
|
||||||
detail: { threshold: newThreshold },
|
|
||||||
})
|
|
||||||
);
|
|
||||||
|
|
||||||
console.log('Dispatched autoCompactThresholdChanged event with threshold:', newThreshold);
|
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Error saving threshold:', error);
|
console.error('Error saving threshold:', error);
|
||||||
window.alert(
|
window.alert(
|
||||||
|
|||||||
@@ -21,4 +21,5 @@ export interface Alert {
|
|||||||
onCompact?: () => void;
|
onCompact?: () => void;
|
||||||
compactIcon?: React.ReactNode;
|
compactIcon?: React.ReactNode;
|
||||||
autoCompactThreshold?: number;
|
autoCompactThreshold?: number;
|
||||||
|
onThresholdChange?: (threshold: number) => void;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user