mirror of
https://github.com/mauriceboe/TREK.git
synced 2026-12-06 10:44:23 +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,52 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
/**
|
||||
* The connections the trip assistant talks to a language model through: at most
|
||||
* one the admin sets up for everyone (`scope = 'instance'`, no user) and at most
|
||||
* one per user (`scope = 'user'`).
|
||||
*
|
||||
* `secrets` is one encrypted JSON blob (an API key, or ChatGPT's access and
|
||||
* refresh tokens), rotated as a whole by scripts/migrate-encryption.ts, and
|
||||
* `secrets_version` counts its writes so a token refresh can replace it only if
|
||||
* nobody else did in between. `account` holds what a sign-in leaves that is not
|
||||
* secret (ChatGPT's issued client id, the account's subject and scopes), so a
|
||||
* signed-out connection can sign in again as the same registration. `hint` is
|
||||
* what the settings show instead of the secret.
|
||||
*
|
||||
* Two kinds are bound to one side: a ChatGPT plan is personal and never the
|
||||
* instance connection, and a custom base URL (an Ollama in the LAN, say) is
|
||||
* only ever the admin's (#1772). The checks refuse the other combinations.
|
||||
*/
|
||||
export class Migration20200101046200_a_user_can_connect_an_ai_provider extends Migration {
|
||||
override name = 'Migration20200101046200_a_user_can_connect_an_ai_provider';
|
||||
|
||||
override up(): void {
|
||||
this.addSql(`
|
||||
CREATE TABLE IF NOT EXISTS ai_connections (
|
||||
id INTEGER PRIMARY KEY,
|
||||
scope TEXT NOT NULL CHECK(scope IN ('instance', 'user')),
|
||||
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
||||
kind TEXT NOT NULL CHECK(kind IN ('anthropic', 'openai', 'gemini', 'openrouter', 'chatgpt', 'custom')),
|
||||
model TEXT NOT NULL,
|
||||
base_url TEXT,
|
||||
secrets TEXT,
|
||||
secrets_version INTEGER NOT NULL DEFAULT 0,
|
||||
hint TEXT,
|
||||
account TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'ok' CHECK(status IN ('ok', 'needs_reauth', 'signed_out', 'error')),
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
CHECK((scope = 'user') = (user_id IS NOT NULL)),
|
||||
CHECK(kind <> 'chatgpt' OR scope = 'user'),
|
||||
CHECK(kind <> 'custom' OR (scope = 'instance' AND base_url IS NOT NULL)),
|
||||
CHECK(base_url IS NULL OR kind = 'custom')
|
||||
)
|
||||
`);
|
||||
this.addSql(
|
||||
'CREATE UNIQUE INDEX IF NOT EXISTS idx_ai_connections_user ON ai_connections(user_id) WHERE user_id IS NOT NULL',
|
||||
);
|
||||
this.addSql(
|
||||
'CREATE UNIQUE INDEX IF NOT EXISTS idx_ai_connections_instance ON ai_connections(scope) WHERE user_id IS NULL',
|
||||
);
|
||||
}
|
||||
}
|
||||
+86
@@ -0,0 +1,86 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
/**
|
||||
* The trip assistant's memory: one user's conversations about one trip.
|
||||
*
|
||||
* `assistant_threads` belong to a user and a trip and go with either.
|
||||
* `assistant_messages` keep each message's parts as the client renders them
|
||||
* (`parts`, JSON) and, for providers whose history has to be replayed as they
|
||||
* sent it, the provider's own items (`provider_state`, JSON).
|
||||
*
|
||||
* `assistant_tool_calls` are the changes the assistant proposed and the user
|
||||
* decides on. The key is our own id, not the provider's: a provider's call id
|
||||
* is only unique within its conversation (`UNIQUE(thread_id, provider_call_id)`).
|
||||
* The arguments are stored when the change is proposed and are what runs on
|
||||
* approval, never what a client sends back; `undo_until` is how long the
|
||||
* counterpart of an executed change may still be run.
|
||||
*
|
||||
* `assistant_usage` counts turns and tokens per user, UTC day and payer, which
|
||||
* is what the daily limit on the instance's connection is checked against.
|
||||
*/
|
||||
export class Migration20200101046210_the_trip_assistant_remembers_its_conversations extends Migration {
|
||||
override name = 'Migration20200101046210_the_trip_assistant_remembers_its_conversations';
|
||||
|
||||
override up(): void {
|
||||
this.addSql(`
|
||||
CREATE TABLE IF NOT EXISTS assistant_threads (
|
||||
id INTEGER PRIMARY KEY,
|
||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
trip_id INTEGER NOT NULL REFERENCES trips(id) ON DELETE CASCADE,
|
||||
title TEXT NOT NULL,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
last_message_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`);
|
||||
this.addSql(
|
||||
'CREATE INDEX IF NOT EXISTS idx_assistant_threads_user_trip ON assistant_threads(user_id, trip_id, last_message_at)',
|
||||
);
|
||||
this.addSql('CREATE INDEX IF NOT EXISTS idx_assistant_threads_trip ON assistant_threads(trip_id)');
|
||||
|
||||
this.addSql(`
|
||||
CREATE TABLE IF NOT EXISTS assistant_messages (
|
||||
id INTEGER PRIMARY KEY,
|
||||
thread_id INTEGER NOT NULL REFERENCES assistant_threads(id) ON DELETE CASCADE,
|
||||
role TEXT NOT NULL CHECK(role IN ('user', 'assistant')),
|
||||
parts TEXT NOT NULL,
|
||||
provider_state TEXT,
|
||||
status TEXT NOT NULL DEFAULT 'complete' CHECK(status IN ('complete', 'awaiting_approval', 'aborted', 'error')),
|
||||
model TEXT,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP
|
||||
)
|
||||
`);
|
||||
this.addSql('CREATE INDEX IF NOT EXISTS idx_assistant_messages_thread ON assistant_messages(thread_id, id)');
|
||||
|
||||
this.addSql(`
|
||||
CREATE TABLE IF NOT EXISTS assistant_tool_calls (
|
||||
id TEXT PRIMARY KEY NOT NULL,
|
||||
thread_id INTEGER NOT NULL REFERENCES assistant_threads(id) ON DELETE CASCADE,
|
||||
message_id INTEGER NOT NULL REFERENCES assistant_messages(id) ON DELETE CASCADE,
|
||||
provider_call_id TEXT NOT NULL,
|
||||
tool_name TEXT NOT NULL,
|
||||
args TEXT NOT NULL,
|
||||
preview TEXT NOT NULL,
|
||||
state TEXT NOT NULL DEFAULT 'approval_requested'
|
||||
CHECK(state IN ('approval_requested', 'approved', 'rejected', 'executed', 'failed', 'expired', 'undone')),
|
||||
result TEXT,
|
||||
undo_until DATETIME,
|
||||
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
decided_at DATETIME,
|
||||
UNIQUE(thread_id, provider_call_id)
|
||||
)
|
||||
`);
|
||||
this.addSql('CREATE INDEX IF NOT EXISTS idx_assistant_tool_calls_message ON assistant_tool_calls(message_id)');
|
||||
|
||||
this.addSql(`
|
||||
CREATE TABLE IF NOT EXISTS assistant_usage (
|
||||
user_id INTEGER NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||||
day TEXT NOT NULL,
|
||||
paid_by TEXT NOT NULL CHECK(paid_by IN ('instance', 'user')),
|
||||
turns INTEGER NOT NULL DEFAULT 0,
|
||||
input_tokens INTEGER NOT NULL DEFAULT 0,
|
||||
output_tokens INTEGER NOT NULL DEFAULT 0,
|
||||
PRIMARY KEY (user_id, day, paid_by)
|
||||
)
|
||||
`);
|
||||
}
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
import { Migration } from '@mikro-orm/migrations';
|
||||
|
||||
/**
|
||||
* A trip chat message can say where it came from when nobody typed it in the
|
||||
* chat: `'assistant'` for an answer of the trip assistant a member shared,
|
||||
* which the chat marks as such in each reader's language. NULL, as on every
|
||||
* message before, is one typed in the chat. Only the assistant's share sets
|
||||
* it; the chat's own routes and tools never do.
|
||||
*/
|
||||
export class Migration20200101046220_a_chat_message_can_say_it_came_from_the_assistant extends Migration {
|
||||
override name = 'Migration20200101046220_a_chat_message_can_say_it_came_from_the_assistant';
|
||||
|
||||
override up(): void {
|
||||
this.addSql(`ALTER TABLE collab_messages ADD COLUMN origin TEXT`);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user