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:
Maurice
2026-10-21 18:52:44 +02:00
parent 31859651d7
commit b64da26651
505 changed files with 51841 additions and 227 deletions
@@ -0,0 +1,164 @@
import { ADDON_IDS } from '../../addons';
import type { User } from '../../types';
import { AddonGuard } from '../addons/addon.guard';
import { RequireAddon } from '../addons/require-addon.decorator';
import { aiRefusal } from '../ai-connections/ai-connections.constants';
import { CurrentUser } from '../auth-core/current-user.decorator';
import { JwtAuthGuard } from '../auth-core/jwt-auth.guard';
import { ManagedForbidden } from '../common/managed';
import { ResponseContract } from '../common/response-contract';
import { onClientGone, openSseStream } from '../common/sse-stream';
import { RequirePermission, TripAccessGuard } from '../permissions/trip-access.guard';
import { AssistantFoundPlacesService } from './assistant-found-places.service';
import { AssistantShareService } from './assistant-share.service';
import {
AssistantAddFoundPlaceDto,
AssistantChatDto,
AssistantDecisionsDto,
AssistantShareDto,
AssistantStopDto,
AssistantUndoDto,
} from './assistant.dto';
import { AssistantService, type TurnChannel } from './assistant.service';
import { Body, Controller, Delete, Get, Headers, HttpCode, Param, Post, Res, UseGuards } from '@nestjs/common';
import {
assistantAddFoundPlaceResponseSchema,
assistantThreadListResponseSchema,
assistantThreadResponseSchema,
assistantUndoResponseSchema,
collabMessageResponseSchema,
successResponseSchema,
} from '@trek/shared';
import type { Response } from 'express';
/** A route id: a positive integer, or the 404 an unknown one gets. */
function idParam(value: string): number {
const id = Number(value);
if (!Number.isInteger(id) || id <= 0) throw aiRefusal('not_found', 'Conversation not found');
return id;
}
/** The turn's stream on this response, and the signal that its client went away. */
export function channelOf(res: Response): TurnChannel {
const gone = new AbortController();
onClientGone(res, () => gone.abort());
return { open: () => openSseStream(res), signal: gone.signal };
}
/**
* `/api/trips/:tripId/assistant`: the user's conversations with the trip
* assistant on one trip. A conversation belongs to the user who had it and
* is only reached on its own trip. Reading the trip is enough to talk about
* it; what a proposed change may do is decided by the tool that runs it, with
* the user's own rights.
*
* `chat` and `decisions` answer with a stream of UI message chunks. A refusal
* before the stream opens (rate limit, connection, daily limit, a turn
* already running) is a JSON `{ error, code }` with its status; one after it
* is an `error` chunk.
*/
@Controller('api/trips/:tripId/assistant')
@UseGuards(AddonGuard, JwtAuthGuard, TripAccessGuard)
@RequireAddon(ADDON_IDS.AI_ASSISTANT, 'AI assistant')
@ManagedForbidden('the operator decides which model a managed install talks to, and pays for it')
export class AssistantController {
constructor(
private readonly assistant: AssistantService,
private readonly foundPlaces: AssistantFoundPlacesService,
private readonly shares: AssistantShareService,
) {}
@Get('threads')
@ResponseContract(assistantThreadListResponseSchema)
threads(@CurrentUser() user: User, @Param('tripId') tripId: string) {
return this.assistant.listThreads(user.id, Number(tripId));
}
@Get('threads/:threadId')
@ResponseContract(assistantThreadResponseSchema)
thread(@CurrentUser() user: User, @Param('tripId') tripId: string, @Param('threadId') threadId: string) {
return this.assistant.getThread(user.id, Number(tripId), idParam(threadId));
}
@Delete('threads/:threadId')
@HttpCode(200)
@ResponseContract(successResponseSchema)
async deleteThread(@CurrentUser() user: User, @Param('tripId') tripId: string, @Param('threadId') threadId: string) {
await this.assistant.deleteThread(user.id, Number(tripId), idParam(threadId));
return { success: true as const };
}
// response-contract-exempt: streams the turn through @Res() as server-sent events, there is no JSON body to check.
@Post('chat')
async chat(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@Body() body: AssistantChatDto,
@Res() res: Response,
): Promise<void> {
await this.assistant.chat(user.id, Number(tripId), body, channelOf(res));
}
// response-contract-exempt: streams the turn through @Res() as server-sent events, there is no JSON body to check.
@Post('threads/:threadId/decisions')
async decide(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@Param('threadId') threadId: string,
@Body() body: AssistantDecisionsDto,
@Res() res: Response,
): Promise<void> {
await this.assistant.decide(user.id, Number(tripId), idParam(threadId), body, channelOf(res));
}
/** "+ Day 3" on a place a search found: a change card for the day, held like any other, no model turn. */
@Post('threads/:threadId/found-places')
@HttpCode(200)
@ResponseContract(assistantAddFoundPlaceResponseSchema)
addFoundPlace(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@Param('threadId') threadId: string,
@Body() body: AssistantAddFoundPlaceDto,
) {
return this.foundPlaces.add(user.id, Number(tripId), idParam(threadId), body);
}
/** An answer shared in the trip's chat, marked as from the assistant; the chat's own right decides. */
@Post('threads/:threadId/messages/:messageId/share')
@HttpCode(201)
@RequirePermission('collab_edit')
@ResponseContract(collabMessageResponseSchema)
share(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@Param('threadId') threadId: string,
@Param('messageId') messageId: string,
@Body() body: AssistantShareDto,
@Headers('x-socket-id') socketId?: string,
) {
return this.shares.share(user, Number(tripId), idParam(threadId), messageId, body.text, socketId);
}
@Post('tool-calls/:toolCallId/undo')
@HttpCode(200)
@ResponseContract(assistantUndoResponseSchema)
async undo(
@CurrentUser() user: User,
@Param('tripId') tripId: string,
@Param('toolCallId') toolCallId: string,
@Body() _body: AssistantUndoDto,
) {
await this.assistant.undo(user.id, Number(tripId), toolCallId);
return { undone: true as const };
}
@Post('stop')
@HttpCode(200)
@ResponseContract(successResponseSchema)
async stop(@CurrentUser() user: User, @Body() _body: AssistantStopDto) {
await this.assistant.stop(user.id);
return { success: true as const };
}
}