automagik-cli
Version:
Automagik CLI - A powerful command-line interface for interacting with Automagik Hive multi-agent AI systems
99 lines (98 loc) • 5.41 kB
JavaScript
import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
import { useState, useEffect } from 'react';
import { Box, Text, useInput } from 'ink';
import { RadioButtonSelect } from './RadioButtonSelect.js';
import { Colors } from '../colors.js';
import { useSession } from '../contexts/SessionContext.js';
export function SessionSelectionDialog({ selectedTarget, onSelect, onBack, }) {
const { listSessions, listBackendSessions } = useSession();
const [existingSessions, setExistingSessions] = useState([]);
const [backendSessions, setBackendSessions] = useState([]);
const [showSessionList, setShowSessionList] = useState(false);
const [loading, setLoading] = useState(false);
useEffect(() => {
const loadSessions = async () => {
setLoading(true);
try {
const [localSessions, remoteSessions] = await Promise.all([
listSessions(selectedTarget),
listBackendSessions(selectedTarget)
]);
setExistingSessions(localSessions);
setBackendSessions(remoteSessions);
}
catch (error) {
console.error('Failed to load sessions:', error);
}
finally {
setLoading(false);
}
};
loadSessions();
}, [selectedTarget, listSessions, listBackendSessions]);
const hasExistingSessions = existingSessions.length > 0 || backendSessions.length > 0;
const items = [
{
label: 'Start new conversation',
value: 'new',
},
{
label: hasExistingSessions ? 'Continue existing session' : 'Continue existing session (no sessions found)',
value: 'existing',
disabled: !hasExistingSessions,
},
];
const handleSelect = (sessionAction) => {
if (sessionAction === 'existing' && hasExistingSessions) {
setShowSessionList(true);
}
else {
onSelect(sessionAction);
}
};
const handleSessionSelect = (sessionId) => {
onSelect('existing', sessionId);
};
useInput((input, key) => {
if (key.escape) {
if (showSessionList) {
setShowSessionList(false);
}
else {
onBack();
}
}
});
if (showSessionList) {
const formatSessionTime = (timestamp) => {
const date = new Date(timestamp);
const now = new Date();
const diffMs = now.getTime() - date.getTime();
const diffDays = Math.floor(diffMs / (1000 * 60 * 60 * 24));
if (diffDays === 0) {
return date.toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' });
}
else if (diffDays === 1) {
return 'Yesterday';
}
else if (diffDays < 7) {
return `${diffDays} days ago`;
}
else {
return date.toLocaleDateString();
}
};
const sessionItems = [
...existingSessions.map(session => ({
label: `${formatSessionTime(session.updatedAt)} (${session.metadata?.totalMessages || 0} messages)`,
value: session.id,
})),
...backendSessions.map(session => ({
label: `${session.name || session.id} (Backend)`,
value: session.id,
})),
];
return (_jsxs(Box, { borderStyle: "round", borderColor: Colors.AccentPurple, flexDirection: "column", padding: 1, width: "100%", children: [_jsx(Text, { bold: true, color: Colors.AccentPurple, children: "Select Session" }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.Foreground, children: "Choose a session to continue:" }) }), _jsx(Box, { marginTop: 1, children: _jsx(RadioButtonSelect, { items: sessionItems, initialIndex: 0, onSelect: handleSessionSelect, isFocused: true }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.Gray, children: "(Use \u2191/\u2193 arrows and Enter to select, Esc to go back)" }) })] }));
}
return (_jsxs(Box, { borderStyle: "round", borderColor: Colors.AccentPurple, flexDirection: "column", padding: 1, width: "100%", children: [_jsx(Text, { bold: true, color: Colors.AccentPurple, children: "Session Options" }), _jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: Colors.Foreground, children: ["Ready to chat with: ", _jsx(Text, { color: Colors.AccentCyan, children: selectedTarget.name })] }) }), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.Foreground, children: "How would you like to proceed?" }) }), loading && (_jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.Gray, children: "Loading sessions..." }) })), !loading && (_jsx(Box, { marginTop: 1, children: _jsx(RadioButtonSelect, { items: items, initialIndex: 0, onSelect: handleSelect, isFocused: true }) })), _jsx(Box, { marginTop: 1, children: _jsx(Text, { color: Colors.Gray, children: "(Use \u2191/\u2193 arrows and Enter to select, Esc to go back)" }) }), hasExistingSessions && (_jsx(Box, { marginTop: 1, children: _jsxs(Text, { color: Colors.AccentGreen, children: ["Found ", existingSessions.length, " local sessions and ", backendSessions.length, " backend sessions"] }) }))] }));
}