Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions src/components/App.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,17 @@ import { render } from 'ink-testing-library';

import { time } from '../utils';

const { mockExit } = vi.hoisted(() => ({
mockExit: vi.fn(),
}));

vi.mock('ink', async () => ({
...(await vi.importActual('ink')),
useApp: vi.fn(() => ({
exit: mockExit,
})),
}));

const resetSystemMessage = vi.hoisted(() => vi.fn());

vi.mock('../utils', async () => ({
Expand Down Expand Up @@ -90,6 +101,7 @@ describe('App', () => {
capturedCallbacks.onClose = null;
capturedCallbacks.onToggleMode = null;
resetSystemMessage.mockClear();
mockExit.mockReset();
});

it('renders title', () => {
Expand Down Expand Up @@ -137,6 +149,12 @@ describe('App', () => {
expect(lastFrame()).not.toContain('ModelPicker');
});

it('calls exit when /exit command is issued', () => {
render(<App />);
capturedCallbacks.onCommand?.('/exit');
expect(mockExit).toHaveBeenCalledOnce();
});

it('resets the chat session when /clear is issued', async () => {
const { lastFrame, rerender } = render(<App />);

Expand Down
34 changes: 21 additions & 13 deletions src/components/App.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Box } from 'ink';
import { Box, useApp } from 'ink';
import { useCallback, useState } from 'react';

import { MODE } from '../constants';
Expand All @@ -9,24 +9,32 @@ import { Header } from './Header';
import { ModelPicker } from './ModelPicker';

export function App() {
const { exit } = useApp();
const [model, setModel] = useState(() => config.loadConfig().model);
const [picking, setPicking] = useState(false);
const [mode, setMode] = useState<MODE.Name>(MODE.NAME.SAFE);
const [sessionId, setSessionId] = useState(0);

const handleCommand = useCallback((command: string) => {
switch (command) {
case '/model':
setPicking(true);
break;
const handleCommand = useCallback(
(command: string) => {
switch (command) {
case '/model':
setPicking(true);
break;

case '/clear':
agents.resetSystemMessage();
setPicking(false);
setSessionId((sessionId) => sessionId + 1);
break;
}
}, []);
case '/clear':
agents.resetSystemMessage();
setPicking(false);
setSessionId((sessionId) => sessionId + 1);
break;

case '/exit':
exit();
break;
}
},
[exit],
);

const handleSelect = useCallback((selected: string) => {
setModel(selected);
Expand Down
1 change: 1 addition & 0 deletions src/constants/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@ export interface CommandList {
export const LIST: CommandList[] = [
{ name: '/clear', description: 'clear the current session' },
{ name: '/model', description: 'switch the model' },
{ name: '/exit', description: 'exit the application' },
] as const;
Loading