mirror of
https://github.com/lrnselfreliance/wrolpi.git
synced 2026-11-25 17:36:18 -05:00
Research mode switches to nine kind-generic tools: search_files, get_file, read_content, search_zims, get_zim_entry, get_inventory, list_files, read_file, search_places. Tool definitions drop from 20 tools / 8,947 chars (~2,240 tokens) to 9 / 3,881 (~970) on every request, a sixth of the 8k context the small tier runs with instead of a quarter. The remaining descriptions are trimmed toward 300 chars. The replaced per-kind endpoints are removed (four searches, three getters, three readers, search_suggestions, the slug inventory route); their tests move onto the generic routes. The research prompt, its worked example, and the empty-search hint no longer tell the model to list collections first: channels resolve by name from the library map that is already in the prompt. list_collections, list_tags, and get_map_overview stay as endpoints for the MCP wrappers but leave the chat allowlist; the MCP search_suggestions tool goes since search_files returns the same matches inline. scripts/ai_eval.py expects search_files and treats "what channels do I have" as answerable from context. On the 1.7B model against the dev library both catalogs score 14/18, but the compact one answers with one tool call where the old one needed up to five, no longer invents the example's channel, and keeps kinds straight when asked for videos. Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
121 lines
5.5 KiB
JavaScript
121 lines
5.5 KiB
JavaScript
import React from 'react';
|
|
import {fireEvent, screen, waitFor} from '@testing-library/react';
|
|
|
|
import {render} from '../test-utils';
|
|
import {AiChatProvider} from '../contexts/AiChatContext';
|
|
import {parseSSEBlock} from '../sse';
|
|
import {AiChat, LinkifiedText} from './AiChat';
|
|
|
|
jest.mock('../api', () => ({
|
|
getChatModes: jest.fn().mockResolvedValue([
|
|
{name: 'help', suggestions: ['How do I download videos?']},
|
|
{name: 'research', suggestions: ['Find videos about canning']},
|
|
{name: 'system', suggestions: ['Why is my drive full?']},
|
|
]),
|
|
getAiCatalog: jest.fn().mockResolvedValue({
|
|
enabled: true, active_model: 'test.gguf', slow_hardware: false,
|
|
}),
|
|
}));
|
|
jest.mock('../sse', () => ({
|
|
...jest.requireActual('../sse'),
|
|
streamChat: jest.fn(),
|
|
}));
|
|
|
|
const {getAiCatalog, getChatModes} = require('../api');
|
|
const {streamChat} = require('../sse');
|
|
|
|
const renderChat = () => render(<AiChatProvider><AiChat/></AiChatProvider>);
|
|
|
|
describe('parseSSEBlock', () => {
|
|
it('parses an event and its JSON data', () => {
|
|
expect(parseSSEBlock('event: token\ndata: {"content": "hi"}'))
|
|
.toEqual({event: 'token', data: {content: 'hi'}});
|
|
expect(parseSSEBlock('')).toBeNull();
|
|
});
|
|
});
|
|
|
|
describe('LinkifiedText', () => {
|
|
it('makes relative WROLPi links clickable', () => {
|
|
render(<LinkifiedText content='See /videos/123 for details'/>);
|
|
const link = screen.getByRole('link', {name: '/videos/123'});
|
|
expect(link).toHaveAttribute('href', '/videos/123');
|
|
});
|
|
});
|
|
|
|
describe('AiChat', () => {
|
|
beforeEach(() => {
|
|
jest.clearAllMocks();
|
|
getChatModes.mockResolvedValue([
|
|
{name: 'help', suggestions: ['How do I download videos?']},
|
|
{name: 'research', suggestions: ['Find videos about canning']},
|
|
{name: 'system', suggestions: ['Why is my drive full?']},
|
|
]);
|
|
getAiCatalog.mockResolvedValue({enabled: true, active_model: 'test.gguf', slow_hardware: false});
|
|
});
|
|
|
|
it('starts with the mode picker; picking a mode shows its suggestion chips', async () => {
|
|
renderChat();
|
|
await waitFor(() => expect(screen.getByRole('button', {name: 'Research'})).toBeInTheDocument());
|
|
expect(screen.getByText(/Pick a mode/)).toBeInTheDocument();
|
|
|
|
fireEvent.click(screen.getByRole('button', {name: 'Research'}));
|
|
await waitFor(() => expect(screen.getByText('Find videos about canning')).toBeInTheDocument());
|
|
});
|
|
|
|
it('streams tokens and tool activity into the conversation', async () => {
|
|
streamChat.mockImplementation(async ({mode, messages}, onEvent) => {
|
|
expect(mode).toBe('research');
|
|
expect(messages[messages.length - 1]).toEqual({role: 'user', content: 'find canning'});
|
|
onEvent('tool_call', {tool: 'search_files', args: {search_str: 'canning', kind: 'video'}});
|
|
onEvent('tool_result', {tool: 'search_files', success: true});
|
|
onEvent('token', {content: 'One video: '});
|
|
onEvent('token', {content: '/videos/123'});
|
|
onEvent('done', {content: 'One video: /videos/123'});
|
|
});
|
|
|
|
renderChat();
|
|
await waitFor(() => expect(screen.getByRole('button', {name: 'Research'})).toBeInTheDocument());
|
|
fireEvent.click(screen.getByRole('button', {name: 'Research'}));
|
|
|
|
fireEvent.change(screen.getByLabelText('Chat message'), {target: {value: 'find canning'}});
|
|
fireEvent.click(screen.getByText('Send'));
|
|
|
|
await waitFor(() => expect(screen.getByText(/using search files/)).toBeInTheDocument());
|
|
await waitFor(() => expect(screen.getByRole('link', {name: '/videos/123'})).toBeInTheDocument());
|
|
expect(streamChat).toHaveBeenCalledTimes(1);
|
|
});
|
|
|
|
it('switching modes clears the conversation', async () => {
|
|
streamChat.mockImplementation(async (_, onEvent) => {
|
|
onEvent('done', {content: 'An answer.'});
|
|
});
|
|
renderChat();
|
|
await waitFor(() => expect(screen.getByRole('button', {name: 'Help'})).toBeInTheDocument());
|
|
fireEvent.click(screen.getByRole('button', {name: 'Help'}));
|
|
fireEvent.change(screen.getByLabelText('Chat message'), {target: {value: 'hello'}});
|
|
fireEvent.click(screen.getByText('Send'));
|
|
await waitFor(() => expect(screen.getByText('An answer.')).toBeInTheDocument());
|
|
|
|
fireEvent.click(screen.getByRole('button', {name: 'System'}));
|
|
await waitFor(() => expect(screen.queryByText('An answer.')).not.toBeInTheDocument());
|
|
});
|
|
|
|
it('shows an error event without crashing the conversation', async () => {
|
|
streamChat.mockImplementation(async (_, onEvent) => {
|
|
onEvent('error', {message: 'The AI failed to answer.'});
|
|
});
|
|
renderChat();
|
|
await waitFor(() => expect(screen.getByRole('button', {name: 'Help'})).toBeInTheDocument());
|
|
fireEvent.click(screen.getByRole('button', {name: 'Help'}));
|
|
fireEvent.change(screen.getByLabelText('Chat message'), {target: {value: 'hello'}});
|
|
fireEvent.click(screen.getByText('Send'));
|
|
await waitFor(() => expect(screen.getByText('The AI failed to answer.')).toBeInTheDocument());
|
|
});
|
|
|
|
it('points at the Manage tab when AI is not enabled', async () => {
|
|
getAiCatalog.mockResolvedValue({enabled: false, active_model: '', slow_hardware: false});
|
|
renderChat();
|
|
await waitFor(() => expect(screen.getByText(/not ready/)).toBeInTheDocument());
|
|
expect(screen.getByRole('link', {name: 'Manage'})).toHaveAttribute('href', '/ai/manage');
|
|
});
|
|
});
|