mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-11-30 08:30:05 +00:00
feat(assistant): trip assistant chat with your own AI connection
Adds an in-app assistant that reads and plans the open trip. Users connect their own provider (Sign in with ChatGPT, or an Anthropic, OpenAI, Gemini or OpenRouter key) or use the one the admin set up, including a custom OpenAI-compatible server. It is an addon and only shows while a connection is usable. The chat runs on the existing MCP tools, pinned to the trip. Every change is held as a change card the user applies or discards, and applied changes can be undone. On the desktop it is a floating window that can be moved and resized; on the phone it is fullscreen. Search results show as place cards that can go onto a day, weather lookups as a strip, proposed places as ghost markers on the map with a before/after switch, and an answer can be shared into the trip chat. Booking files can be dropped in and are read into change cards.
This commit is contained in:
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* FE-AI-ERR-001 to FE-AI-ERR-008
|
||||
*
|
||||
* The error line: one sentence, at most one thing to do, and that thing
|
||||
* does what it says.
|
||||
*/
|
||||
import type { AiStatusResponse, AssistantErrorCode } from '@trek/shared';
|
||||
import { Route, Routes } from 'react-router';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
import { fireEvent, render, screen } from '../../../tests/helpers/render';
|
||||
import { chatOf, useAssistantStore } from '../../store/assistantStore';
|
||||
import { setForcedOffline } from '../../sync/networkMode';
|
||||
import AssistantError from './AssistantError';
|
||||
|
||||
const TRIP = 4;
|
||||
const INITIAL = useAssistantStore.getState();
|
||||
|
||||
const STATUS: AiStatusResponse = {
|
||||
available: true,
|
||||
policy: 'instance_or_user',
|
||||
canConnectOwn: true,
|
||||
effective: { source: 'user', kind: 'anthropic', model: 'claude', host: 'api.anthropic.com', paidBy: 'user' },
|
||||
own: null,
|
||||
instance: null,
|
||||
quota: null,
|
||||
privacyAcked: true,
|
||||
retention: { mode: 'days', days: 90 },
|
||||
openrouterOAuth: true,
|
||||
};
|
||||
|
||||
const retry = vi.fn(async () => {});
|
||||
const continueHere = vi.fn(async () => {});
|
||||
const send = vi.fn(async () => {});
|
||||
|
||||
function withError(error: AssistantErrorCode | null, status: AiStatusResponse = STATUS) {
|
||||
useAssistantStore.setState({
|
||||
status,
|
||||
retry,
|
||||
continueHere,
|
||||
send,
|
||||
byTrip: { [TRIP]: { ...chatOf(useAssistantStore.getState(), TRIP), error } },
|
||||
});
|
||||
}
|
||||
|
||||
function renderLine() {
|
||||
return render(
|
||||
<Routes>
|
||||
<Route path="/trips/4" element={<AssistantError tripId={TRIP} />} />
|
||||
<Route path="/settings" element={<p>settings page</p>} />
|
||||
</Routes>,
|
||||
{ initialEntries: ['/trips/4'] }
|
||||
);
|
||||
}
|
||||
|
||||
beforeEach(() => setForcedOffline(false));
|
||||
afterEach(() => {
|
||||
useAssistantStore.setState(INITIAL, true);
|
||||
setForcedOffline(false);
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
describe('AssistantError', () => {
|
||||
it('FE-AI-ERR-001: shows nothing while all is well', () => {
|
||||
withError(null);
|
||||
const { container } = renderLine();
|
||||
expect(container).toBeEmptyDOMElement();
|
||||
});
|
||||
|
||||
it('FE-AI-ERR-002: names the provider and retries a provider that was down', () => {
|
||||
withError('provider_unavailable');
|
||||
renderLine();
|
||||
expect(screen.getByRole('status')).toHaveTextContent('Anthropic isn’t responding right now.');
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Try again' }));
|
||||
expect(retry).toHaveBeenCalledWith(TRIP);
|
||||
});
|
||||
|
||||
it('FE-AI-ERR-003: a busy chat continues here, and a long run goes on with "Continue"', () => {
|
||||
withError('busy');
|
||||
const { unmount } = renderLine();
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Continue here' }));
|
||||
expect(continueHere).toHaveBeenCalledWith(TRIP);
|
||||
unmount();
|
||||
|
||||
withError('max_steps');
|
||||
renderLine();
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Continue' }));
|
||||
expect(send).toHaveBeenCalledWith(TRIP, 'Continue');
|
||||
});
|
||||
|
||||
it('FE-AI-ERR-004: an expired sign-in leads straight to the settings card', () => {
|
||||
withError('needs_reauth');
|
||||
renderLine();
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Sign in again' }));
|
||||
expect(screen.getByText('settings page')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-AI-ERR-006: an own key that stopped working says so before anything is sent', () => {
|
||||
withError(null, {
|
||||
...STATUS,
|
||||
own: { scope: 'user', kind: 'anthropic', model: 'claude', hint: '…a1b2', status: 'error', host: 'api.anthropic.com' },
|
||||
});
|
||||
renderLine();
|
||||
fireEvent.click(screen.getByRole('button', { name: 'Replace key' }));
|
||||
expect(screen.getByText('settings page')).toBeInTheDocument();
|
||||
});
|
||||
|
||||
it('FE-AI-ERR-007: a provider limit names the provider, without guessing at its plan', () => {
|
||||
withError('rate_limited');
|
||||
renderLine();
|
||||
expect(screen.getByRole('status')).toHaveTextContent('Limit reached at Anthropic. Try again later.');
|
||||
expect(screen.queryByRole('button')).toBeNull();
|
||||
});
|
||||
|
||||
it('FE-AI-ERR-008: waiting changes ask for a decision before a place can be suggested', () => {
|
||||
withError('decide_first');
|
||||
renderLine();
|
||||
expect(screen.getByRole('status')).toHaveTextContent('Decide on the open suggestions first.');
|
||||
});
|
||||
|
||||
it('FE-AI-ERR-005: offline wins over any other error and offers nothing to press', () => {
|
||||
withError('provider_unavailable');
|
||||
setForcedOffline(true);
|
||||
renderLine();
|
||||
expect(screen.getByRole('status')).toHaveTextContent('Offline. The assistant needs internet.');
|
||||
expect(screen.queryByRole('button')).toBeNull();
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user