diff --git a/common/arg.cpp b/common/arg.cpp index 5dc7c8d3a74c..99feada640bb 100644 --- a/common/arg.cpp +++ b/common/arg.cpp @@ -3422,6 +3422,22 @@ common_params_context common_params_parser_init(common_params & params, llama_ex params.ui_mcp_proxy = value; } ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_UI_MCP_PROXY")); + add_opt(common_arg( + {"--ui-storage"}, + {"--no-ui-storage"}, + "experimental: keep UI conversations and settings on disk instead of only in the browser - anyone who can reach the server can read them (default: disabled)", + [](common_params & params, bool value) { + params.ui_storage = value; + } + ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_UI_STORAGE")); + add_opt(common_arg( + {"--ui-storage-dir"}, "PATH", + "directory for UI storage, implies --ui-storage (default: llama.cpp/ui in the user data directory)", + [](common_params & params, const std::string & value) { + params.ui_storage = true; + params.ui_storage_dir = value; + } + ).set_examples({LLAMA_EXAMPLE_SERVER}).set_env("LLAMA_ARG_UI_STORAGE_DIR")); add_opt(common_arg( {"--tools"}, "TOOL1,TOOL2,...", "experimental: whether to enable built-in tools for AI agents - do not enable in untrusted environments (default: no tools)\n" diff --git a/common/common.cpp b/common/common.cpp index 657dcdbf530c..35ff9fc0cccf 100644 --- a/common/common.cpp +++ b/common/common.cpp @@ -1014,6 +1014,24 @@ std::filesystem::path fs_get_config_directory() { return config_directory / "llama.cpp"; } +std::filesystem::path fs_get_data_directory() { + std::filesystem::path data_directory; +#if defined(_WIN32) + data_directory = common_get_path_from_env("APPDATA"); + if (data_directory.empty()) { + throw std::runtime_error("Failed to find %APPDATA% directory"); + } +#elif defined(__APPLE__) + data_directory = get_home_directory() / "Library/Application Support"; +#else + data_directory = common_get_path_from_env("XDG_DATA_HOME"); + if (data_directory.empty()) { + data_directory = get_home_directory() / ".local/share"; + } +#endif + return data_directory / "llama.cpp"; +} + std::filesystem::path fs_get_cache_file(const std::string & filename) { GGML_ASSERT(filename.find(DIRECTORY_SEPARATOR) == std::string::npos); const std::filesystem::path cache_directory = fs_get_cache_directory(); diff --git a/common/common.h b/common/common.h index 9b9e892d8631..9860d8e02532 100644 --- a/common/common.h +++ b/common/common.h @@ -703,6 +703,8 @@ struct common_params { bool ui = true; bool ui_mcp_proxy = false; std::string ui_config_json; + bool ui_storage = false; // keep UI conversations and settings on disk + std::string ui_storage_dir; // empty: fs_get_data_directory() / "ui" // "advanced" endpoints are disabled by default for better security bool endpoint_slots = true; @@ -960,6 +962,7 @@ inline bool common_create_directories(const std::filesystem::path & path, std::e std::filesystem::path fs_get_cache_directory(); std::filesystem::path fs_get_cache_file(const std::string & filename); std::filesystem::path fs_get_config_directory(); +std::filesystem::path fs_get_data_directory(); void fs_write_atomic(const std::filesystem::path & path, const std::string & data); diff --git a/tools/server/CMakeLists.txt b/tools/server/CMakeLists.txt index 9f9393f98384..7403ce5aa0cd 100644 --- a/tools/server/CMakeLists.txt +++ b/tools/server/CMakeLists.txt @@ -23,6 +23,8 @@ add_library(${TARGET} STATIC server-stream.h server-tools.cpp server-tools.h + server-storage.cpp + server-storage.h server-mcp.cpp server-mcp.h server-schema.cpp diff --git a/tools/server/README.md b/tools/server/README.md index 4295e74e8ac3..2d1c6e7928a8 100644 --- a/tools/server/README.md +++ b/tools/server/README.md @@ -203,6 +203,8 @@ For the full list of features, please refer to [server's changelog](https://gith | `--ui-config, --webui-config JSON` | JSON that provides default UI settings (overrides UI defaults)
(env: LLAMA_ARG_UI_CONFIG) | | `--ui-config-file, --webui-config-file PATH` | JSON file that provides default UI settings (overrides UI defaults)
(env: LLAMA_ARG_UI_CONFIG_FILE) | | `--ui-mcp-proxy, --webui-mcp-proxy, --no-ui-mcp-proxy, --no-webui-mcp-proxy` | experimental: whether to enable MCP CORS proxy, for Llama Chat to reach public MCP servers without CORS headers; local and private addresses are refused - do not enable in untrusted environments (default: disabled)
note: for security reasons, this will limit --cors-origins to localhost by default
(env: LLAMA_ARG_UI_MCP_PROXY) | +| `--ui-storage, --no-ui-storage` | experimental: keep UI conversations and settings on disk instead of only in the browser - anyone who can reach the server can read them (default: disabled)
(env: LLAMA_ARG_UI_STORAGE) | +| `--ui-storage-dir PATH` | directory for UI storage, implies --ui-storage (default: llama.cpp/ui in the user data directory)
(env: LLAMA_ARG_UI_STORAGE_DIR) | | `--tools TOOL1,TOOL2,...` | experimental: whether to enable built-in tools for AI agents - do not enable in untrusted environments (default: no tools)
specify "all" to enable all tools
available tools: read_file, file_glob_search, grep_search, exec_shell_command, write_file, edit_file, get_info
note: for security reasons, this will limit --cors-origins to localhost by default
(env: LLAMA_ARG_TOOLS) | | `--tools-runtime OPTION` | experimental: run tools in a separate runtime environment (default: none, use host environment)
available options:
'docker:', 'podman:': spin up a new container and reuse it for all invocations, clean up on server exit
'docker-container:', 'podman-container:': use an existing container by ID, won't stop on server exit
'ssh:': run tools on a remote POSIX host over SSH, key-based auth and a trusted host key are required

(env: LLAMA_ARG_TOOLS_RUNTIME) | | `--allow-unauthenticated-tools` | allow server tools, MCP servers or MCP proxy on a non-loopback address without an API key - only for trusted networks (default: disabled)
(env: LLAMA_ARG_ALLOW_UNAUTHENTICATED_TOOLS) | diff --git a/tools/server/server-context.cpp b/tools/server/server-context.cpp index 6a57aa8ca83b..59e9ae61076a 100644 --- a/tools/server/server-context.cpp +++ b/tools/server/server-context.cpp @@ -5630,6 +5630,7 @@ static json get_res_props(const server_context_meta & meta, const common_params { "build_info", meta.build_info }, { "is_sleeping", is_sleeping }, { "cors_proxy_enabled", params.ui_mcp_proxy }, + { "ui_storage", params.ui_storage }, }; if (!params.server_connect_code.empty()) { diff --git a/tools/server/server-models.cpp b/tools/server/server-models.cpp index 5d5992c08d3a..60bd889b5539 100644 --- a/tools/server/server-models.cpp +++ b/tools/server/server-models.cpp @@ -296,6 +296,9 @@ static void unset_reserved_args(common_preset & preset, bool unset_model_args) { preset.unset_option("LLAMA_ARG_CONNECT_CODE"); preset.unset_option("LLAMA_ARG_MCP_SERVERS_CONFIG"); preset.unset_option("LLAMA_ARG_MCP_SERVERS_JSON"); + // UI storage is served by the router only + preset.unset_option("LLAMA_ARG_UI_STORAGE"); + preset.unset_option("LLAMA_ARG_UI_STORAGE_DIR"); if (unset_model_args) { preset.unset_option("LLAMA_ARG_MODEL"); preset.unset_option("LLAMA_ARG_SHARD"); @@ -2073,6 +2076,7 @@ void server_models_routes::init_routes() { {"ui_settings", ui_settings}, {"build_info", std::string(llama_build_info())}, {"cors_proxy_enabled", params.ui_mcp_proxy}, + {"ui_storage", params.ui_storage}, }; if (!params.server_connect_code.empty()) { props["connect_code"] = params.server_connect_code; diff --git a/tools/server/server-storage.cpp b/tools/server/server-storage.cpp new file mode 100644 index 000000000000..2239e57c965a --- /dev/null +++ b/tools/server/server-storage.cpp @@ -0,0 +1,227 @@ +#include "server-storage.h" + +#include +#include + +namespace fs = std::filesystem; + +static json read_json_file(const fs::path & path) { + std::ifstream f(path, std::ios::binary); + if (!f) { + throw std::runtime_error("failed to open file: " + path.string()); + } + std::stringstream ss; + ss << f.rdbuf(); + return json::parse(ss.str()); +} + +// ids and record kinds come from the UI (uuids, table names), anything else could escape the storage directory +static void validate_id(const std::string & id, const char * what = "conversation id") { + if (id.empty() || id.size() > 64) { + throw std::invalid_argument(std::string("invalid ") + what); + } + for (char c : id) { + if (!std::isalnum((unsigned char) c) && c != '-' && c != '_') { + throw std::invalid_argument(std::string("invalid ") + what); + } + } +} + +static server_http_res_ptr res_json(int status, const json & data) { + auto res = std::make_unique(); + res->status = status; + res->data = safe_json_to_str(data); + return res; +} + +fs::path server_storage::conversation_path(const std::string & id) const { + return dir / "conversations" / fs::u8path(id + ".json"); +} + +fs::path server_storage::records_path(const std::string & kind) const { + return dir / "records" / fs::u8path(kind + ".json"); +} + +void server_storage::load_index() { + const fs::path index_path = dir / "index.json"; + + if (fs::exists(index_path)) { + try { + index = read_json_file(index_path); + if (index.is_object()) { + return; + } + } catch (const std::exception & e) { + SRV_WRN("failed to read %s, rebuilding it: %s\n", index_path.string().c_str(), e.what()); + } + } + + index = json::object(); + for (const auto & entry : fs::directory_iterator(dir / "conversations")) { + if (!entry.is_regular_file() || entry.path().extension() != ".json") { + continue; + } + try { + const json doc = read_json_file(entry.path()); + const std::string id = doc.at("conv").at("id").get(); + index[id] = { { "rev", doc.value("rev", 0) }, { "conv", doc.at("conv") } }; + } catch (const std::exception & e) { + SRV_WRN("skipping unreadable conversation %s: %s\n", entry.path().string().c_str(), e.what()); + } + } + save_index(); +} + +void server_storage::save_index() const { + fs_write_atomic(dir / "index.json", safe_json_to_str(index)); +} + +void server_storage::setup(const fs::path & storage_dir) { + dir = storage_dir; + + std::error_code ec; + common_create_directories(dir / "conversations", ec); + if (!ec) { + common_create_directories(dir / "records", ec); + } + if (ec) { + throw std::runtime_error("failed to create UI storage directory: " + dir.string()); + } + load_index(); + + SRV_INF("UI storage in %s (%zu conversations)\n", dir.string().c_str(), index.size()); + + handle_list = [this](const server_http_req &) { + std::lock_guard lock(mtx); + json list = json::array(); + for (const auto & [id, entry] : index.items()) { + list.push_back(entry); + } + return res_json(200, list); + }; + + handle_get = [this](const server_http_req & req) { + const std::string id = req.get_param("id"); + validate_id(id); + + std::lock_guard lock(mtx); + if (!index.contains(id)) { + return res_json(404, { { "error", format_error_response("conversation not found", ERROR_TYPE_NOT_FOUND) } }); + } + return res_json(200, read_json_file(conversation_path(id))); + }; + + // the body carries the revision it was based on, 0 for a new conversation + handle_put = [this](const server_http_req & req) { + const std::string id = req.get_param("id"); + validate_id(id); + + const json body = json::parse(req.body); + const int rev = body.at("rev").get(); + const json & conv = body.at("conv"); + const json & messages = body.at("messages"); + if (!conv.is_object() || conv.value("id", "") != id || !messages.is_array()) { + throw std::invalid_argument("expected { rev, conv, messages } with conv.id matching the path"); + } + + std::lock_guard lock(mtx); + const int current = index.contains(id) ? index[id].value("rev", 0) : 0; + if (rev != current) { + json error = { { "code", 409 }, { "message", "conversation was modified by another client" }, { "type", "conflict_error" } }; + return res_json(409, { { "error", error }, { "rev", current } }); + } + + const json doc = { { "rev", current + 1 }, { "conv", conv }, { "messages", messages } }; + fs_write_atomic(conversation_path(id), safe_json_to_str(doc)); + index[id] = { { "rev", current + 1 }, { "conv", conv } }; + save_index(); + + return res_json(200, { { "rev", current + 1 } }); + }; + + handle_delete = [this](const server_http_req & req) { + const std::string id = req.get_param("id"); + validate_id(id); + + std::lock_guard lock(mtx); + std::error_code ec; + fs::remove(conversation_path(id), ec); + if (index.contains(id)) { + index.erase(id); + save_index(); + } + return res_json(200, json::object()); + }; + + handle_get_settings = [this](const server_http_req &) { + std::lock_guard lock(mtx); + const fs::path path = dir / "settings.json"; + return res_json(200, fs::exists(path) ? read_json_file(path) : json::object()); + }; + + // merges per key, so two clients changing different settings do not overwrite each other + handle_put_settings = [this](const server_http_req & req) { + const json body = json::parse(req.body); + const json set = body.value("set", json::object()); + const json remove = body.value("remove", json::array()); + if (!set.is_object() || !remove.is_array()) { + throw std::invalid_argument("expected { set: { key: value }, remove: [key] }"); + } + + std::lock_guard lock(mtx); + const fs::path path = dir / "settings.json"; + json settings = fs::exists(path) ? read_json_file(path) : json::object(); + for (const auto & [key, value] : set.items()) { + if (!value.is_string()) { + throw std::invalid_argument("setting values must be strings"); + } + settings[key] = value; + } + for (const auto & key : remove) { + settings.erase(key.get()); + } + fs_write_atomic(path, safe_json_to_str(settings)); + + return res_json(200, settings); + }; + + handle_get_records = [this](const server_http_req & req) { + const std::string kind = req.get_param("kind"); + validate_id(kind, "record kind"); + + std::lock_guard lock(mtx); + const fs::path path = records_path(kind); + return res_json(200, fs::exists(path) ? read_json_file(path) : json::object()); + }; + + // records are small and written whole, merged per id like settings + handle_put_records = [this](const server_http_req & req) { + const std::string kind = req.get_param("kind"); + validate_id(kind, "record kind"); + + const json body = json::parse(req.body); + const json set = body.value("set", json::object()); + const json remove = body.value("remove", json::array()); + if (!set.is_object() || !remove.is_array()) { + throw std::invalid_argument("expected { set: { id: record }, remove: [id] }"); + } + for (const auto & [id, record] : set.items()) { + if (!record.is_object() || record.value("id", "") != id) { + throw std::invalid_argument("records must be objects whose id matches their key"); + } + } + + std::lock_guard lock(mtx); + const fs::path path = records_path(kind); + json records = fs::exists(path) ? read_json_file(path) : json::object(); + for (const auto & [id, record] : set.items()) { + records[id] = record; + } + for (const auto & id : remove) { + records.erase(id.get()); + } + fs_write_atomic(path, safe_json_to_str(records)); + + return res_json(200, json::object()); + }; +} diff --git a/tools/server/server-storage.h b/tools/server/server-storage.h new file mode 100644 index 000000000000..ee2c24bb1dd0 --- /dev/null +++ b/tools/server/server-storage.h @@ -0,0 +1,39 @@ +#pragma once + +#include "server-common.h" +#include "server-http.h" + +#include +#include + +// UI storage (--ui-storage): the Web UI's conversations and settings kept as JSON files on disk +// the server does not interpret the documents, it only tracks a revision per conversation so +// concurrent writers (tabs, browsers) get a 409 instead of silently overwriting each other +// +// layout: +// /conversations/.json { "rev": n, "conv": {...}, "messages": [...] } +// /index.json { "": { "rev": n, "conv": {...} } }, rebuilt when missing +// /settings.json { "": "" } +// /records/.json { "": {...} }, groups, projects, prompts, ... +struct server_storage { + void setup(const std::filesystem::path & dir); + + server_http_context::handler_t handle_list; // GET /storage/conversations + server_http_context::handler_t handle_get; // GET /storage/conversations/:id + server_http_context::handler_t handle_put; // POST /storage/conversations/:id + server_http_context::handler_t handle_delete; // DELETE /storage/conversations/:id + server_http_context::handler_t handle_get_settings; // GET /storage/settings + server_http_context::handler_t handle_put_settings; // POST /storage/settings + server_http_context::handler_t handle_get_records; // GET /storage/records/:kind + server_http_context::handler_t handle_put_records; // POST /storage/records/:kind + +private: + std::filesystem::path dir; + std::mutex mtx; + json index = json::object(); + + std::filesystem::path conversation_path(const std::string & id) const; + std::filesystem::path records_path(const std::string & kind) const; + void load_index(); + void save_index() const; +}; diff --git a/tools/server/server.cpp b/tools/server/server.cpp index d14978286224..1acdd56c7290 100644 --- a/tools/server/server.cpp +++ b/tools/server/server.cpp @@ -5,6 +5,7 @@ #include "server-cors-proxy.h" #include "server-stream.h" #include "server-tools.h" +#include "server-storage.h" #include "arg.h" #include "build-info.h" @@ -241,6 +242,7 @@ static bool server_setup_features( const common_params & params, server_mcp & mcp_mgr, server_tools & tools, + server_storage & storage, server_role_type role_type) { // return 403 for disabled features server_http_context::handler_t res_403 = [](const server_http_req &) { @@ -319,6 +321,33 @@ static bool server_setup_features( ctx_http.post("/tools", ex_wrapper(res_403)); } + if (params.ui_storage) { + try { + storage.setup(params.ui_storage_dir.empty() ? fs_get_data_directory() / "ui" : std::filesystem::u8path(params.ui_storage_dir)); + } catch (const std::exception & e) { + SRV_ERR("UI storage setup failed: %s\n", e.what()); + return false; + } + ctx_http.get ("/storage/conversations", ex_wrapper(storage.handle_list)); + ctx_http.get ("/storage/conversations/:id", ex_wrapper(storage.handle_get)); + ctx_http.post("/storage/conversations/:id", ex_wrapper(storage.handle_put)); + ctx_http.del ("/storage/conversations/:id", ex_wrapper(storage.handle_delete)); + ctx_http.get ("/storage/settings", ex_wrapper(storage.handle_get_settings)); + ctx_http.post("/storage/settings", ex_wrapper(storage.handle_put_settings)); + ctx_http.get ("/storage/records/:kind", ex_wrapper(storage.handle_get_records)); + ctx_http.post("/storage/records/:kind", ex_wrapper(storage.handle_put_records)); + warn_names.push_back("UI storage (experimental)"); + } else { + ctx_http.get ("/storage/conversations", ex_wrapper(res_403)); + ctx_http.get ("/storage/conversations/:id", ex_wrapper(res_403)); + ctx_http.post("/storage/conversations/:id", ex_wrapper(res_403)); + ctx_http.del ("/storage/conversations/:id", ex_wrapper(res_403)); + ctx_http.get ("/storage/settings", ex_wrapper(res_403)); + ctx_http.post("/storage/settings", ex_wrapper(res_403)); + ctx_http.get ("/storage/records/:kind", ex_wrapper(res_403)); + ctx_http.post("/storage/records/:kind", ex_wrapper(res_403)); + } + if (!warn_names.empty()) { std::string features; for (const auto & name : warn_names) { @@ -502,6 +531,7 @@ static int server_run_router(common_params & params, int argc, char ** argv, con server_context ctx_server; server_routes routes(params, ctx_server); server_tools tools; + server_storage storage; server_http_context ctx_http; if (!ctx_http.init(params)) { @@ -564,7 +594,7 @@ static int server_run_router(common_params & params, int argc, char ** argv, con models_routes->router_streams_lookup, models_routes->router_stream_delete); - if (!server_setup_features(ctx_http, params, mcp_mgr, tools, SERVER_ROLE_ROUTER)) { + if (!server_setup_features(ctx_http, params, mcp_mgr, tools, storage, SERVER_ROLE_ROUTER)) { return 1; } @@ -653,6 +683,7 @@ static int server_run_instance(common_params & params, server_role & role, commo server_context ctx_server; server_routes routes(params, ctx_server); server_tools tools; + server_storage storage; server_http_context ctx_http; if (!ctx_http.init(params)) { @@ -666,7 +697,7 @@ static int server_run_instance(common_params & params, server_role & role, commo server_stream_make_lookup_handler(), server_stream_make_delete_handler()); - if (!server_setup_features(ctx_http, params, mcp_mgr, tools, SERVER_ROLE_INSTANCE)) { + if (!server_setup_features(ctx_http, params, mcp_mgr, tools, storage, SERVER_ROLE_INSTANCE)) { return 1; } diff --git a/ui/apps/chat/docs/storage-adapters.md b/ui/apps/chat/docs/storage-adapters.md new file mode 100644 index 000000000000..37e2e27fc0ab --- /dev/null +++ b/ui/apps/chat/docs/storage-adapters.md @@ -0,0 +1,115 @@ +# Storage adapters + +By default the UI keeps conversations, groups, projects, prompts and settings in the browser (IndexedDB and localStorage). A storage adapter keeps them somewhere else instead. The built-in one is llama-server's own JSON document store (`llama-server --ui-storage`). You can add your own for Postgres (Supabase, Neon), PocketBase or any other database. + +```mermaid +flowchart LR + stores[Stores] --> db[DatabaseService
tree rules] + db --> backend{StorageBackend} + backend --> idb[IdbBackend
this browser] + backend --> dsb[DocumentStoreBackend
cache, batching, conflict replay] + dsb --> adapter{StorageAdapter} + adapter --> ls[llama-server
/storage routes] + adapter --> pg[your adapter
Postgres, PocketBase, ...] +``` + +## The contract + +An adapter stores one **document per conversation**: `{ rev, conv, messages }`. It doesn't need to understand the document. It only stores it as a whole and enforces one rule: + +- **Compare and swap on `rev`.** `putConversation(doc)` writes only if the store still holds revision `doc.rev`, where `0` means the document must not exist yet. It returns the new revision. Otherwise it throws `StorageConflictError` and writes nothing. This check and the write must be atomic. + +`DocumentStoreBackend` uses that rule to merge edits from two tabs or browsers: on a conflict it fetches the fresh document, replays its own edits and retries. + +Settings are plain string key/value pairs, merged per key: `updateSettings(set, remove)` must leave other keys untouched. The API key never reaches an adapter; it stays in the browser. + +Records are the small standalone objects next to conversations, one kind per table: `groups`, `projects` and `prompts`. Each has an `id`. `listRecords(kind)` returns all of a kind, and `updateRecords(kind, set, remove)` writes `set` whole and deletes `remove` by id, leaving the other records untouched. Last write wins per record. New kinds the UI adds later go through the same two methods, so a store keyed by kind and id needs no change for them. + +See `StorageAdapter` in `src/lib/types/storage.d.ts` for the full interface (eight methods). + +## Adding one + +1. Implement `StorageAdapter` in `src/lib/storage-adapters/.adapter.ts`, next to `llama-server.adapter.ts`. +2. Export a `StorageAdapterDefinition` (`id`, `name`, `description`, `isAvailable`, `create`) and add it to `STORAGE_ADAPTERS` in `src/lib/storage-adapters/index.ts`. +3. Build the UI. Settings → Import/Export → Storage then offers "Store on " wherever `isAvailable` returns true, and copies the browser's data over when the user picks it. + +Configuration such as a URL or a public key usually comes from build-time env vars (`import.meta.env.VITE_*`). Make `isAvailable` return false when they are missing. + +The in-memory adapter in `tests/unit/document-store-backend.test.ts` is the smallest working example. + +## Postgres: Supabase, Neon + +Store the document as JSON columns, and do the compare and swap in one SQL function: + +```sql +create table llama_ui_conversations ( + id text primary key, + rev integer not null, + conv jsonb not null, + messages jsonb not null +); + +create table llama_ui_settings ( + key text primary key, + value text not null +); + +create table llama_ui_records ( + kind text not null, + id text not null, + record jsonb not null, + primary key (kind, id) +); + +-- returns the new revision, or null when `doc.rev` is stale +create function llama_ui_put_conversation(doc jsonb) returns integer +language plpgsql as $$ +declare + base integer := (doc->>'rev')::integer; + new_rev integer; +begin + if base = 0 then + insert into llama_ui_conversations (id, rev, conv, messages) + values (doc->'conv'->>'id', 1, doc->'conv', doc->'messages') + on conflict (id) do nothing + returning rev into new_rev; + else + update llama_ui_conversations + set rev = rev + 1, conv = doc->'conv', messages = doc->'messages' + where id = doc->'conv'->>'id' and rev = base + returning rev into new_rev; + end if; + return new_rev; +end $$; +``` + +Supabase and Neon (Data API) both expose this over PostgREST, so the adapter is a thin wrapper around `fetch`: + +| Method | Request | +| -------------------- | --------------------------------------------------------------------------------------------------- | +| `listConversations` | `GET /llama_ui_conversations?select=rev,conv` | +| `getConversation` | `GET /llama_ui_conversations?id=eq.&select=rev,conv,messages`, first row or null | +| `putConversation` | `POST /rpc/llama_ui_put_conversation` with `{ "doc": ... }`; a null result → `StorageConflictError` | +| `deleteConversation` | `DELETE /llama_ui_conversations?id=eq.` | +| `getSettings` | `GET /llama_ui_settings`, rows into an object | +| `updateSettings` | upsert with `Prefer: resolution=merge-duplicates`, then `DELETE ?key=in.(...)` | +| `listRecords` | `GET /llama_ui_records?kind=eq.&select=record` | +| `updateRecords` | upsert `{ kind, id, record }` rows, then `DELETE ?kind=eq.&id=in.(...)` | + +Whatever key the adapter sends is in the built UI bundle, readable by anyone who loads the page. Protect the tables with row level security tied to a signed-in user (for example Supabase Auth), not with a key alone. + +Add a `user_id` column to every table if several people share the database. + +## PocketBase + +Create three collections: + +- `conversations`, with `conv_id` (text, unique), `rev` (number), `conv` (json) and `messages` (json). +- `settings`, with `key` (text, unique) and `value` (text). +- `records`, with `kind` (text), `record_id` (text) and `record` (json), unique on `kind` + `record_id`. + +PocketBase's record API has no conditional update, so do the compare and swap in a custom route in `pb_hooks`. Inside `$app.runInTransaction`, read the record, compare `rev`, then save it with `rev + 1`. Answer 409 on a mismatch, and have the adapter turn that 409 into `StorageConflictError`. + +## Going lower level + +If you'd rather store messages as rows, for example as normalized Postgres tables, implement `StorageBackend` (`src/lib/types/database.d.ts`) directly, the way `IdbBackend` does. Then pass it to `DatabaseService.useBackend`. Your backend then has to provide transactions across conversations, messages and records itself; the document model above exists to avoid that requirement. diff --git a/ui/apps/chat/src/lib/components/app/models/ModelsManager/utils.ts b/ui/apps/chat/src/lib/components/app/models/ModelsManager/utils.ts index 051f96a570ed..a1d78cb3a62f 100644 --- a/ui/apps/chat/src/lib/components/app/models/ModelsManager/utils.ts +++ b/ui/apps/chat/src/lib/components/app/models/ModelsManager/utils.ts @@ -13,6 +13,7 @@ import { ModelsTableServerKind } from '#lib/enums/index.ts'; import { HuggingFaceService, ModelsService } from '#lib/services/index.ts'; +import { PreferencesService } from '#lib/services/preferences.service.ts'; import { modelsStore, serversModelsStore, settingsStore } from '#lib/stores/index.ts'; import type { ModelDownloadEntry, @@ -155,7 +156,7 @@ export function isCustomized(override?: ModelOverride): boolean { export function loadOverrides(): ModelOverrideMap { try { - const raw = localStorage.getItem(MODEL_OVERRIDES_LOCALSTORAGE_KEY); + const raw = PreferencesService.getItem(MODEL_OVERRIDES_LOCALSTORAGE_KEY); if (!raw) return {}; @@ -169,7 +170,7 @@ export function loadOverrides(): ModelOverrideMap { export function saveOverrides(overrides: ModelOverrideMap): void { try { - localStorage.setItem(MODEL_OVERRIDES_LOCALSTORAGE_KEY, JSON.stringify(overrides)); + PreferencesService.setItem(MODEL_OVERRIDES_LOCALSTORAGE_KEY, JSON.stringify(overrides)); } catch { console.warn('[ModelsManager] Failed to persist model overrides'); } diff --git a/ui/apps/chat/src/lib/components/app/settings/SettingsChat/SettingsChat.svelte b/ui/apps/chat/src/lib/components/app/settings/SettingsChat/SettingsChat.svelte index 71d4e8a719e0..010a0b951dff 100644 --- a/ui/apps/chat/src/lib/components/app/settings/SettingsChat/SettingsChat.svelte +++ b/ui/apps/chat/src/lib/components/app/settings/SettingsChat/SettingsChat.svelte @@ -20,6 +20,7 @@ SETTINGS_SECTION_SLUGS } from '#lib/constants/index.ts'; import { ColorMode } from '#lib/enums/ui.enums.ts'; + import { StorageModeService } from '#lib/services/index.ts'; import { modelsStore, serverStore, settingsStore } from '#lib/stores/index.ts'; import type { SettingsSection, SettingsSectionTitle } from '#lib/types/index.ts'; import { applySurfaceTheme } from '#lib/utils/index.ts'; @@ -228,7 +229,11 @@
-

Settings are saved in browser's localStorage

+

+ {StorageModeService.activeAdapter + ? `Settings are saved on ${StorageModeService.activeAdapter.name}` + : "Settings are saved in browser's localStorage"} +

diff --git a/ui/apps/chat/src/lib/components/app/settings/SettingsChat/SettingsChatImportExportTab.svelte b/ui/apps/chat/src/lib/components/app/settings/SettingsChat/SettingsChatImportExportTab.svelte index 9f95d5e44cea..d6b6eca455f8 100644 --- a/ui/apps/chat/src/lib/components/app/settings/SettingsChat/SettingsChatImportExportTab.svelte +++ b/ui/apps/chat/src/lib/components/app/settings/SettingsChat/SettingsChatImportExportTab.svelte @@ -1,6 +1,6 @@
+ {#if activeStorage} + + openStorageDialog(null)} + title="Stored on {activeStorage.name}" + /> + + {:else if offeredStorage.length > 0} + + {#each offeredStorage as adapter (adapter.id)} + openStorageDialog(adapter)} + title="Stored in this browser" + /> + {/each} + + {/if} + + (showStorageDialog = false)} + onConfirm={handleStorageConfirm} + title={storageTarget ? `Store on ${storageTarget.name}` : 'Store in this browser'} +/> + = new Set([ + ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY, + CONFIG_LOCALSTORAGE_KEY, + DISABLED_TOOL_CATEGORIES_LOCALSTORAGE_KEY, + DISABLED_TOOL_KEYS_LOCALSTORAGE_KEY, + DISMISSED_RECOMMENDED_MCP_SERVERS_LOCALSTORAGE_KEY, + DISMISSED_RECOMMENDED_SERVERS_LOCALSTORAGE_KEY, + EXTENSIONS_LOCALSTORAGE_KEY, + FAVORITE_MODELS_LOCALSTORAGE_KEY, + HIDDEN_MODELS_LOCALSTORAGE_KEY, + MODEL_OVERRIDES_LOCALSTORAGE_KEY, + REASONING_EFFORT_DEFAULT_LOCALSTORAGE_KEY, + RECENT_MODELS_LOCALSTORAGE_KEY, + USER_OVERRIDES_LOCALSTORAGE_KEY +]); + // Deprecated old key names (kept for backward compat while users migrate) /** @deprecated Remote servers are settings-backed now; the demo build kept them here */ export const LEGACY_TUNNEL_SERVERS_LOCALSTORAGE_KEY = 'llama_servers'; @@ -77,6 +94,12 @@ export const DEPRECATED_FAVORITE_MODELS_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME_D /** @deprecated Use {@link USER_OVERRIDES_LOCALSTORAGE_KEY} instead */ export const DEPRECATED_USER_OVERRIDES_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME_DEPRECATED}.userOverrides`; +/** Storage adapter id this browser keeps conversations and user settings in, unset: this browser */ +export const STORAGE_ADAPTER_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.storageAdapter`; + +/** API key kept in this browser while the rest of the config lives on the server */ +export const API_KEY_LOCALSTORAGE_KEY = `${STORAGE_APP_NAME}.apiKey`; + /** Maps new keys to their deprecated fallback keys */ export const NEW_TO_DEPRECATED_MAP: Record = { [ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY]: DEPRECATED_ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY, diff --git a/ui/apps/chat/src/lib/enums/index.ts b/ui/apps/chat/src/lib/enums/index.ts index baba49379eb0..810bf6b250e0 100644 --- a/ui/apps/chat/src/lib/enums/index.ts +++ b/ui/apps/chat/src/lib/enums/index.ts @@ -132,3 +132,5 @@ export { SplashOrientation } from './splash.enums'; export { LibraryItemKind, LibraryTab, LibraryView } from './library.enums'; export { PromptDrift } from './prompt.enums'; export { VoiceInputMode, VoiceMode } from './voice.enums'; + +export { PreferenceScope } from './storage.enums'; diff --git a/ui/apps/chat/src/lib/enums/storage.enums.ts b/ui/apps/chat/src/lib/enums/storage.enums.ts new file mode 100644 index 000000000000..6766b153c178 --- /dev/null +++ b/ui/apps/chat/src/lib/enums/storage.enums.ts @@ -0,0 +1,7 @@ +/** Where a persisted preference belongs. */ +export enum PreferenceScope { + /** UI state of this browser only, never synced. */ + DEVICE = 'device', + /** The user's own data, follows them across browsers once synced. */ + USER = 'user' +} diff --git a/ui/apps/chat/src/lib/extensions/mcp/components/dialogs/DialogMcpServerAddNew.svelte b/ui/apps/chat/src/lib/extensions/mcp/components/dialogs/DialogMcpServerAddNew.svelte index e109adb474d9..c0c07a5bb5ad 100644 --- a/ui/apps/chat/src/lib/extensions/mcp/components/dialogs/DialogMcpServerAddNew.svelte +++ b/ui/apps/chat/src/lib/extensions/mcp/components/dialogs/DialogMcpServerAddNew.svelte @@ -11,6 +11,7 @@ RECOMMENDED_MCP_SERVERS } from '#lib/constants/index.ts'; import { BooleanString, HealthCheckStatus } from '#lib/enums/index.ts'; + import { PreferencesService } from '#lib/services/preferences.service.ts'; import { deviceStore } from '#lib/stores/index.ts'; import { canonicalizeServerUrl, parseHeadersToArray, uuid } from '#lib/utils/index.ts'; import { browser } from '$app/env'; @@ -93,7 +94,7 @@ function readRecommendationsDismissed(): boolean { if (!browser) return false; - const raw = localStorage.getItem(DISMISSED_RECOMMENDED_MCP_SERVERS_LOCALSTORAGE_KEY); + const raw = PreferencesService.getItem(DISMISSED_RECOMMENDED_MCP_SERVERS_LOCALSTORAGE_KEY); if (!raw) return false; @@ -114,7 +115,7 @@ recommendationsDismissed = dismissed; if (browser) { - localStorage.setItem( + PreferencesService.setItem( DISMISSED_RECOMMENDED_MCP_SERVERS_LOCALSTORAGE_KEY, dismissed ? BooleanString.TRUE : BooleanString.FALSE ); diff --git a/ui/apps/chat/src/lib/extensions/registry.svelte.ts b/ui/apps/chat/src/lib/extensions/registry.svelte.ts index a28939938f3b..fcc457d91a83 100644 --- a/ui/apps/chat/src/lib/extensions/registry.svelte.ts +++ b/ui/apps/chat/src/lib/extensions/registry.svelte.ts @@ -17,6 +17,7 @@ import type { ExtensionStatus } from './types'; import { EXTENSIONS_LOCALSTORAGE_KEY } from '#lib/constants/index.ts'; +import { PreferencesService } from '#lib/services/preferences.service.ts'; import { browser } from '$app/env'; import { SvelteMap } from 'svelte/reactivity'; @@ -199,7 +200,7 @@ class ExtensionRegistry { this.choicesLoaded = true; try { - const stored = localStorage.getItem(this.storageKey); + const stored = PreferencesService.getItem(this.storageKey); if (!stored) return; @@ -219,7 +220,10 @@ class ExtensionRegistry { if (!browser || !this.storageKey) return; try { - localStorage.setItem(this.storageKey, JSON.stringify(Object.fromEntries(this._choices))); + PreferencesService.setItem( + this.storageKey, + JSON.stringify(Object.fromEntries(this._choices)) + ); } catch { // ignore storage errors } diff --git a/ui/apps/chat/src/lib/services/chat.service.ts b/ui/apps/chat/src/lib/services/chat.service.ts index 98f3a789cfbb..0f0d59c6f491 100644 --- a/ui/apps/chat/src/lib/services/chat.service.ts +++ b/ui/apps/chat/src/lib/services/chat.service.ts @@ -8,6 +8,7 @@ import { getAudioInputFormat } from '../utils/audio-format'; import { capImageDataURLSize } from '../utils/cap-img-size'; +import { PreferencesService } from './preferences.service'; import { API_CHAT, API_SLOTS, @@ -135,7 +136,7 @@ export class ChatService { ChatService.streamStateSaveTrackers.delete(conversationId); try { - localStorage.removeItem(streamStorageKey(conversationId)); + PreferencesService.removeItem(streamStorageKey(conversationId)); } catch { // nothing to do } @@ -444,7 +445,7 @@ export class ChatService { if (!conversationId) return null; try { - const raw = localStorage.getItem(streamStorageKey(conversationId)); + const raw = PreferencesService.getItem(streamStorageKey(conversationId)); if (!raw) return null; @@ -1727,7 +1728,7 @@ export class ChatService { updatedAt: Date.now() }; - localStorage.setItem(streamStorageKey(conversationId), JSON.stringify(state)); + PreferencesService.setItem(streamStorageKey(conversationId), JSON.stringify(state)); } catch { // localStorage may be full or disabled, silently ignore } diff --git a/ui/apps/chat/src/lib/services/database.service.ts b/ui/apps/chat/src/lib/services/database.service.ts index 9bb972a636c5..026936826dff 100644 --- a/ui/apps/chat/src/lib/services/database.service.ts +++ b/ui/apps/chat/src/lib/services/database.service.ts @@ -1,58 +1,35 @@ /** - * DatabaseService - IndexedDB persistence for conversations, messages, groups - * and projects + * DatabaseService - persistence for conversations, messages, groups, projects + * and prompts * - * Thin Dexie layer over the conversations/messages/groups/projects tables: - * CRUD, tree navigation (descendants, reparenting) and cascading deletes. No - * reactive state; consumed by conversationsStore, groupsStore, projectsStore - * and the chat flows. + * Applies the tree rules (branching, forks, reparenting, cascading deletes) + * on top of a row-level StorageBackend, IndexedDB by default. No reactive + * state; consumed by conversationsStore, groupsStore, projectsStore, the + * prompt library and the chat flows. */ -import { - IDXDB_STORE_SCHEMAS, - IDXDB_STORES_V1, - IDXDB_TABLES, - IDXDB_VERSION_GROUPS, - IDXDB_VERSION_PROJECTS, - IDXDB_VERSION_PROMPTS, - STORAGE_APP_NAME -} from '#lib/constants/index.ts'; +import { IdbBackend } from './idb-backend'; +import { IDXDB_TABLES } from '#lib/constants/index.ts'; import { MessageRole } from '#lib/enums/index.ts'; import type { DatabaseMessageExtra, DatabasePrompt, ExportedConversation, - McpServerOverride + MessageTimeRange, + StorageBackend } from '#lib/types/database.js'; import type { Group } from '#lib/types/groups.js'; import type { Project } from '#lib/types/projects.js'; -import { filterByLeafNodeId, findDescendantMessages, uuid } from '#lib/utils/index.ts'; -import Dexie, { type EntityTable } from 'dexie'; - -class LlamaUiDatabase extends Dexie { - [IDXDB_TABLES.conversations]!: EntityTable; - [IDXDB_TABLES.groups]!: EntityTable; - [IDXDB_TABLES.messages]!: EntityTable; - [IDXDB_TABLES.projects]!: EntityTable; - [IDXDB_TABLES.prompts]!: EntityTable; - - constructor() { - super(STORAGE_APP_NAME); - - this.version(1).stores(IDXDB_STORES_V1); - this.version(IDXDB_VERSION_GROUPS).stores({ - [IDXDB_TABLES.groups]: IDXDB_STORE_SCHEMAS.groups - }); - this.version(IDXDB_VERSION_PROJECTS).stores({ - [IDXDB_TABLES.projects]: IDXDB_STORE_SCHEMAS.projects - }); - this.version(IDXDB_VERSION_PROMPTS).stores({ - [IDXDB_TABLES.prompts]: IDXDB_STORE_SCHEMAS.prompts - }); - } -} +import { + buildForkedConversation, + collectForkDescendantIds, + findDescendantMessages, + groupMessagesByConversation, + reparentDirectForks, + uuid +} from '#lib/utils/index.ts'; -const db = new LlamaUiDatabase(); +let backend: StorageBackend = new IdbBackend(); export class DatabaseService { /** @@ -70,45 +47,17 @@ export class DatabaseService { const idSet = new Set(cleanIds); - await db.transaction( - 'rw', - [db[IDXDB_TABLES.conversations], db[IDXDB_TABLES.messages]], - async () => { - // Pre-load each to-delete conversation so the per-id reparent - // walk-up doesn't ping-pong the same ancestry chain. - const prefetched = new Map(); + await backend.transaction(async () => { + const conversations = await backend.listConversations(); - let frontier = [...cleanIds]; - - const requested = new Set(frontier); - - while (frontier.length > 0) { - const fetched = await db[IDXDB_TABLES.conversations].bulkGet(frontier); - - frontier = []; - for (let i = 0; i < fetched.length; i++) { - const conv = fetched[i]; - - if (!conv || !conv.id) continue; - - prefetched.set(conv.id, conv); - const ancestor = conv.forkedFromConversationId; - - if (ancestor && !prefetched.has(ancestor) && !requested.has(ancestor)) { - frontier.push(ancestor); - requested.add(ancestor); - } - } - } + for (const id of cleanIds) { + const updates = reparentDirectForks(conversations, id, idSet); - for (const id of cleanIds) { - await this.reparentDirectChildren(id, idSet, prefetched); - } - - await db[IDXDB_TABLES.conversations].bulkDelete(cleanIds); - await db[IDXDB_TABLES.messages].where('convId').anyOf(cleanIds).delete(); + if (updates.length > 0) await backend.putConversations(updates); } - ); + + await backend.deleteConversations(cleanIds); + }); } /** @@ -127,8 +76,8 @@ export class DatabaseService { if (cleanIds.length === 0) return result; - await db.transaction('rw', db[IDXDB_TABLES.conversations], async () => { - const convs = await db[IDXDB_TABLES.conversations].bulkGet(cleanIds); + await backend.transaction(async () => { + const convs = await backend.getConversations(cleanIds); const updates: DatabaseConversation[] = []; for (let i = 0; i < cleanIds.length; i++) { @@ -144,7 +93,7 @@ export class DatabaseService { if (updates.length === 0) return; - await db[IDXDB_TABLES.conversations].bulkPut(updates); + await backend.putConversations(updates); }); return result; @@ -169,7 +118,7 @@ export class DatabaseService { ...fields }; - await db[IDXDB_TABLES.conversations].add(conversation); + await backend.addConversation(conversation); return conversation; } @@ -180,7 +129,7 @@ export class DatabaseService { * @returns The created group */ static async createGroup(group: Group): Promise { - await db[IDXDB_TABLES.groups].add(group); + await backend.addRecord(IDXDB_TABLES.groups, group); return group; } @@ -197,41 +146,37 @@ export class DatabaseService { message: Omit, parentId: string | null ): Promise { - return await db.transaction( - 'rw', - [db[IDXDB_TABLES.conversations], db[IDXDB_TABLES.messages]], - async () => { - // Handle null parent (root message case) - if (parentId !== null) { - const parentMessage = await db[IDXDB_TABLES.messages].get(parentId); - - if (!parentMessage) { - throw new Error(`Parent message ${parentId} not found`); - } + return await backend.transaction(async () => { + // Handle null parent (root message case) + if (parentId !== null) { + const parentMessage = await backend.getMessage(parentId); + + if (!parentMessage) { + throw new Error(`Parent message ${parentId} not found`); } + } - const newMessage: DatabaseMessage = { - ...message, - children: [], - id: uuid(), - parent: parentId, - toolCalls: message.toolCalls ?? '' - }; + const newMessage: DatabaseMessage = { + ...message, + children: [], + id: uuid(), + parent: parentId, + toolCalls: message.toolCalls ?? '' + }; - await db[IDXDB_TABLES.messages].add(newMessage); + await backend.addMessages([newMessage]); - // Update parent's children array if parent exists - if (parentId !== null) { - await this.addChildToParent(parentId, newMessage.id); - } + // Update parent's children array if parent exists + if (parentId !== null) { + await this.addChildToParent(parentId, newMessage.id); + } - await this.updateConversation(message.convId, { - currNode: newMessage.id - }); + await this.updateConversation(message.convId, { + currNode: newMessage.id + }); - return newMessage; - } - ); + return newMessage; + }); } /** @@ -240,7 +185,7 @@ export class DatabaseService { * @returns The created project */ static async createProject(project: Project): Promise { - await db[IDXDB_TABLES.projects].add(project); + await backend.addRecord(IDXDB_TABLES.projects, project); return project; } @@ -251,7 +196,7 @@ export class DatabaseService { * @returns The created prompt */ static async createPrompt(prompt: DatabasePrompt): Promise { - await db[IDXDB_TABLES.prompts].add(prompt); + await backend.addRecord(IDXDB_TABLES.prompts, prompt); return prompt; } @@ -276,7 +221,7 @@ export class DatabaseService { type: 'root' }; - await db[IDXDB_TABLES.messages].add(rootMessage); + await backend.addMessages([rootMessage]); return rootMessage.id; } @@ -302,8 +247,8 @@ export class DatabaseService { throw new Error('Cannot create system message with empty content'); } - return await db.transaction('rw', db[IDXDB_TABLES.messages], async () => { - const parentMessage = await db[IDXDB_TABLES.messages].get(parentId); + return await backend.transaction(async () => { + const parentMessage = await backend.getMessage(parentId); if (!parentMessage) { throw new Error(`Parent message ${parentId} not found`); @@ -321,7 +266,7 @@ export class DatabaseService { type: MessageRole.SYSTEM }; - await db[IDXDB_TABLES.messages].add(systemMessage); + await backend.addMessages([systemMessage]); await this.addChildToParent(parentId, systemMessage.id); return systemMessage; @@ -337,64 +282,40 @@ export class DatabaseService { id: string, options?: { deleteWithForks?: boolean } ): Promise { - await db.transaction( - 'rw', - [db[IDXDB_TABLES.conversations], db[IDXDB_TABLES.messages]], - async () => { - if (options?.deleteWithForks) { - // Recursively collect all descendant IDs - const idsToDelete: string[] = []; - const queue = [id]; - - while (queue.length > 0) { - const parentId = queue.pop()!; - const children = await db[IDXDB_TABLES.conversations] - .filter((c) => c.forkedFromConversationId === parentId) - .toArray(); - - for (const child of children) { - idsToDelete.push(child.id); - queue.push(child.id); - } - } - - for (const forkId of idsToDelete) { - await db[IDXDB_TABLES.conversations].delete(forkId); - await db[IDXDB_TABLES.messages].where('convId').equals(forkId).delete(); - } - } else { - await this.reparentDirectChildren(id); - } + await backend.transaction(async () => { + const conversations = await backend.listConversations(); + + if (options?.deleteWithForks) { + await backend.deleteConversations(collectForkDescendantIds(conversations, id)); + } else { + const updates = reparentDirectForks(conversations, id); - await db[IDXDB_TABLES.conversations].delete(id); - await db[IDXDB_TABLES.messages].where('convId').equals(id).delete(); + if (updates.length > 0) await backend.putConversations(updates); } - ); + + await backend.deleteConversations([id]); + }); } /** - * Deletes a group. Its conversations stay, out of any group. + * Deletes a group. Its conversations and projects stay, out of any group. * * @param id - Group ID */ static async deleteGroup(id: string): Promise { - await db.transaction( - 'rw', - [db[IDXDB_TABLES.groups], db[IDXDB_TABLES.conversations], db[IDXDB_TABLES.projects]], - async () => { - await db[IDXDB_TABLES.groups].delete(id); - await db[IDXDB_TABLES.conversations] - .filter((conversation) => conversation.groupId === id) - .modify((conversation) => { - delete conversation.groupId; - }); - await db[IDXDB_TABLES.projects] - .filter((project) => project.groupId === id) - .modify((project) => { - delete project.groupId; - }); + await backend.transaction(async () => { + await backend.deleteRecord(IDXDB_TABLES.groups, id); + + for (const conv of await backend.listConversations()) { + if (conv.groupId === id) await backend.updateConversation(conv.id, { groupId: undefined }); } - ); + + for (const project of await backend.listRecords(IDXDB_TABLES.projects)) { + if (project.groupId === id) { + await backend.updateRecord(IDXDB_TABLES.projects, project.id, { groupId: undefined }); + } + } + }); } /** @@ -403,14 +324,14 @@ export class DatabaseService { * @param messageId - ID of the message to delete */ static async deleteMessage(messageId: string): Promise { - await db.transaction('rw', db[IDXDB_TABLES.messages], async () => { - const message = await db[IDXDB_TABLES.messages].get(messageId); + await backend.transaction(async () => { + const message = await backend.getMessage(messageId); if (!message) return; await this.removeChildFromParent(messageId); - await db[IDXDB_TABLES.messages].delete(messageId); + await backend.deleteMessages([messageId]); }); } @@ -426,19 +347,16 @@ export class DatabaseService { conversationId: string, messageId: string ): Promise { - return await db.transaction('rw', db[IDXDB_TABLES.messages], async () => { + return await backend.transaction(async () => { // Get all messages in the conversation to find descendants - const allMessages = await db[IDXDB_TABLES.messages] - .where('convId') - .equals(conversationId) - .toArray(); + const allMessages = await backend.getMessages([conversationId]); const descendants = findDescendantMessages(allMessages, messageId); const allToDelete = [messageId, ...descendants]; await this.removeChildFromParent(messageId); // Delete all messages in the branch - await db[IDXDB_TABLES.messages].bulkDelete(allToDelete); + await backend.deleteMessages(allToDelete); return allToDelete; }); @@ -450,7 +368,7 @@ export class DatabaseService { * @param id - Project ID */ static async deleteProject(id: string): Promise { - await db[IDXDB_TABLES.projects].delete(id); + await backend.deleteRecord(IDXDB_TABLES.projects, id); } /** @@ -459,7 +377,7 @@ export class DatabaseService { * @param id - Prompt ID */ static async deletePrompt(id: string): Promise { - await db[IDXDB_TABLES.prompts].delete(id); + await backend.deleteRecord(IDXDB_TABLES.prompts, id); } /** @@ -476,80 +394,21 @@ export class DatabaseService { atMessageId: string, options: { name: string; includeAttachments: boolean } ): Promise { - return await db.transaction( - 'rw', - [db[IDXDB_TABLES.conversations], db[IDXDB_TABLES.messages]], - async () => { - const sourceConv = await db[IDXDB_TABLES.conversations].get(sourceConvId); - - if (!sourceConv) { - throw new Error(`Source conversation ${sourceConvId} not found`); - } + return await backend.transaction(async () => { + const [sourceConv] = await backend.getConversations([sourceConvId]); - const allMessages = await db[IDXDB_TABLES.messages] - .where('convId') - .equals(sourceConvId) - .toArray(); - const pathMessages = filterByLeafNodeId( - allMessages, - atMessageId, - true - ) as DatabaseMessage[]; - - if (pathMessages.length === 0) { - throw new Error(`Could not resolve message path to ${atMessageId}`); - } + if (!sourceConv) { + throw new Error(`Source conversation ${sourceConvId} not found`); + } - const idMap = new Map(); + const allMessages = await backend.getMessages([sourceConvId]); + const fork = buildForkedConversation(sourceConv, allMessages, atMessageId, options); - for (const msg of pathMessages) { - idMap.set(msg.id, uuid()); - } + await backend.addConversation(fork.conv); + await backend.addMessages(fork.messages); - const newConvId = uuid(); - const clonedMessages: DatabaseMessage[] = pathMessages.map((msg) => { - const newId = idMap.get(msg.id)!; - const newParent = msg.parent ? (idMap.get(msg.parent) ?? null) : null; - const newChildren = msg.children - .filter((childId: string) => idMap.has(childId)) - .map((childId: string) => idMap.get(childId)!); - - return { - ...msg, - children: newChildren, - convId: newConvId, - extra: options.includeAttachments ? msg.extra : undefined, - id: newId, - parent: newParent - }; - }); - const lastClonedMessage = clonedMessages[clonedMessages.length - 1]; - const newConv: DatabaseConversation = { - currNode: lastClonedMessage.id, - cwd: sourceConv.cwd, - disabledToolCategories: sourceConv.disabledToolCategories - ? [...sourceConv.disabledToolCategories] - : undefined, - disabledTools: sourceConv.disabledTools ? [...sourceConv.disabledTools] : undefined, - forkedFromConversationId: sourceConvId, - groupId: sourceConv.groupId, - id: newConvId, - lastModified: Date.now(), - mcpServerOverrides: sourceConv.mcpServerOverrides - ? sourceConv.mcpServerOverrides.map((o: McpServerOverride) => ({ - enabled: o.enabled, - serverId: o.serverId - })) - : undefined, - name: options.name - }; - - await db[IDXDB_TABLES.conversations].add(newConv); - await db[IDXDB_TABLES.messages].bulkAdd(clonedMessages); - - return newConv; - } - ); + return fork.conv; + }); } /** @@ -558,7 +417,7 @@ export class DatabaseService { * @returns Array of conversations */ static async getAllConversations(): Promise { - return await db[IDXDB_TABLES.conversations].orderBy('lastModified').reverse().toArray(); + return await backend.listConversations(); } /** @@ -568,7 +427,7 @@ export class DatabaseService { * @returns Array of groups */ static async getAllGroups(): Promise { - const groups = await db[IDXDB_TABLES.groups].toArray(); + const groups = await backend.listRecords(IDXDB_TABLES.groups); return groups.sort((a, b) => b.createdAt - a.createdAt); } @@ -579,7 +438,9 @@ export class DatabaseService { * @returns Array of projects */ static async getAllProjects(): Promise { - return await db[IDXDB_TABLES.projects].orderBy('updatedAt').reverse().toArray(); + const projects = await backend.listRecords(IDXDB_TABLES.projects); + + return projects.sort((a, b) => b.updatedAt - a.updatedAt); } /** @@ -588,7 +449,9 @@ export class DatabaseService { * @returns Array of prompts */ static async getAllPrompts(): Promise { - return await db[IDXDB_TABLES.prompts].orderBy('lastModified').reverse().toArray(); + const prompts = await backend.listRecords(IDXDB_TABLES.prompts); + + return prompts.sort((a, b) => b.lastModified - a.lastModified); } /** @@ -598,7 +461,9 @@ export class DatabaseService { * @returns The conversation if found, otherwise undefined */ static async getConversation(id: string): Promise { - return await db[IDXDB_TABLES.conversations].get(id); + const [conv] = await backend.getConversations([id]); + + return conv; } /** @@ -608,7 +473,16 @@ export class DatabaseService { * @returns Array of messages in the conversation */ static async getConversationMessages(convId: string): Promise { - return await db[IDXDB_TABLES.messages].where('convId').equals(convId).sortBy('timestamp'); + const messages = await backend.getMessages([convId]); + + return messages.sort((a, b) => a.timestamp - b.timestamp); + } + + /** Conversations by id, leaving out ids with none */ + static async getConversations(ids: string[]): Promise { + const found = await backend.getConversations(ids); + + return found.filter((conv): conv is DatabaseConversation => !!conv); } /** @@ -627,28 +501,15 @@ export class DatabaseService { if (cleanIds.length === 0) return result; const [convs, allMessages] = await Promise.all([ - db[IDXDB_TABLES.conversations].bulkGet(cleanIds), - db[IDXDB_TABLES.messages].where('convId').anyOf(cleanIds).toArray() + backend.getConversations(cleanIds), + backend.getMessages(cleanIds) ]); - const messagesByConv = new Map(); - - for (const msg of allMessages) { - const bucket = messagesByConv.get(msg.convId); - - if (bucket) bucket.push(msg); - else messagesByConv.set(msg.convId, [msg]); - } - - for (let i = 0; i < cleanIds.length; i++) { - const conv = convs[i]; + const messagesByConv = groupMessagesByConversation(allMessages); + for (const conv of convs) { if (!conv) continue; - const messages = (messagesByConv.get(conv.id) ?? []).sort( - (a, b) => a.timestamp - b.timestamp - ); - - result.set(conv.id, { conv, messages }); + result.set(conv.id, { conv, messages: messagesByConv.get(conv.id) ?? [] }); } return result; @@ -661,7 +522,17 @@ export class DatabaseService { * @returns Messages, oldest first */ static async getMessagesSince(timestamp: number): Promise { - return await db[IDXDB_TABLES.messages].where('timestamp').aboveOrEqual(timestamp).toArray(); + const messages: DatabaseMessage[] = []; + + await backend.scanMessages( + null, + (message) => { + if (message.timestamp >= timestamp) messages.push(message); + }, + { from: timestamp } + ); + + return messages.sort((a, b) => a.timestamp - b.timestamp); } /** @@ -677,46 +548,40 @@ export class DatabaseService { const imported: DatabaseConversation[] = []; const skipped: DatabaseConversation[] = []; - return await db.transaction( - 'rw', - [db[IDXDB_TABLES.conversations], db[IDXDB_TABLES.groups], db[IDXDB_TABLES.messages]], - async () => { - for (const item of data) { - const { messages } = item; - const existing = await db[IDXDB_TABLES.conversations].get(item.conv.id); + return await backend.transaction(async () => { + for (const item of data) { + const { messages } = item; + const [existing] = await backend.getConversations([item.conv.id]); - if (existing) { - skipped.push(item.conv); + if (existing) { + skipped.push(item.conv); - continue; - } - - const conv = { ...item.conv }; - const groupId = await DatabaseService.importGroup(item); + continue; + } - if (groupId) conv.groupId = groupId; - else delete conv.groupId; + const conv = { ...item.conv }; + const groupId = await DatabaseService.importGroup(item); - await db[IDXDB_TABLES.conversations].add(conv); - for (const msg of messages) { - await db[IDXDB_TABLES.messages].put(msg); - } + if (groupId) conv.groupId = groupId; + else delete conv.groupId; - imported.push(conv); - } + await backend.addConversation(conv); + await backend.putMessages(messages); - return { imported, skipped }; + imported.push(conv); } - ); + + return { imported, skipped }; + }); } - /** Run `fn` in a readonly transaction over all tables. `fn` must await only Dexie calls, else the transaction commits early */ - static async readOnly( - fn: (tables: Pick) => Promise - ): Promise { - return db.transaction('r', [db[IDXDB_TABLES.conversations], db[IDXDB_TABLES.messages]], () => - fn(db) - ); + /** See StorageBackend.scanMessages */ + static async scanMessages( + convIds: string[] | null, + visit: (message: DatabaseMessage) => void, + range?: MessageTimeRange + ): Promise { + await backend.scanMessages(convIds, visit, range); } /** @@ -727,18 +592,22 @@ export class DatabaseService { * @param groupId - Target group, null to take them out of their group */ static async setConversationsGroup(ids: string[], groupId: string | null): Promise { - await db[IDXDB_TABLES.conversations] - .where('id') - .anyOf(ids) - .modify((conversation) => { - if (groupId) { - conversation.groupId = groupId; - - if (conversation.pinned) conversation.pinned = false; - } else { - delete conversation.groupId; + await backend.transaction(async () => { + for (const conversation of await backend.getConversations(ids)) { + if (!conversation) continue; + + if (!groupId) { + await backend.updateConversation(conversation.id, { groupId: undefined }); + + continue; } - }); + + await backend.updateConversation( + conversation.id, + conversation.pinned ? { groupId, pinned: false } : { groupId } + ); + } + }); } /** @@ -748,7 +617,7 @@ export class DatabaseService { * @returns The new pinned status */ static async toggleConversationPin(id: string): Promise { - const conversation = await db[IDXDB_TABLES.conversations].get(id); + const conversation = await this.getConversation(id); if (!conversation) { throw new Error(`Conversation ${id} not found`); @@ -773,7 +642,7 @@ export class DatabaseService { id: string, updates: Partial> ): Promise { - await db[IDXDB_TABLES.conversations].update(id, updates); + await backend.updateConversation(id, updates); } /** @@ -797,7 +666,7 @@ export class DatabaseService { * @param updates - Partial group data to update */ static async updateGroup(id: string, updates: Partial>): Promise { - await db[IDXDB_TABLES.groups].update(id, updates); + await backend.updateRecord(IDXDB_TABLES.groups, id, updates); } /** @@ -811,7 +680,7 @@ export class DatabaseService { id: string, updates: Partial> ): Promise { - await db[IDXDB_TABLES.messages].update(id, updates); + await backend.updateMessage(id, updates); } /** @@ -822,7 +691,7 @@ export class DatabaseService { * @param updates - Partial updates to apply */ static async updateProject(id: string, updates: Partial>): Promise { - await db[IDXDB_TABLES.projects].update(id, updates); + await backend.updateRecord(IDXDB_TABLES.projects, id, updates); } /** @@ -835,18 +704,26 @@ export class DatabaseService { id: string, updates: Partial> ): Promise { - await db[IDXDB_TABLES.prompts].update(id, updates); + await backend.updateRecord(IDXDB_TABLES.prompts, id, updates); + } + + /** + * Switch where conversations are stored. Called once at startup, before any + * conversation is read. + */ + static useBackend(next: StorageBackend): void { + backend = next; } /** * Appends a child id to a parent message's children array. */ private static async addChildToParent(parentId: string, childId: string): Promise { - const parent = await db[IDXDB_TABLES.messages].get(parentId); + const parent = await backend.getMessage(parentId); if (!parent) return; - await db[IDXDB_TABLES.messages].update(parentId, { + await backend.updateMessage(parentId, { children: [...parent.children, childId] }); } @@ -860,25 +737,28 @@ export class DatabaseService { * @returns The group id, or undefined for none */ private static async importGroup(item: ExportedConversation): Promise { - const groups = db[IDXDB_TABLES.groups]; + const groups = await backend.listRecords(IDXDB_TABLES.groups); if (!item.group) { const { groupId } = item.conv; - return groupId && (await groups.get(groupId)) ? groupId : undefined; + return groupId && groups.some((group) => group.id === groupId) ? groupId : undefined; } const { id, name } = item.group; - if (await groups.get(id)) return id; + if (groups.some((group) => group.id === id)) return id; - const sameName = await groups.where('name').equals(name).first(); + // of several groups with the name, the first by id, as the name index gave them + const [sameName] = groups + .filter((group) => group.name === name) + .sort((a, b) => a.id.localeCompare(b.id)); if (sameName) return sameName.id; const now = Date.now(); - await groups.add({ createdAt: now, id, name, updatedAt: now }); + await backend.addRecord(IDXDB_TABLES.groups, { createdAt: now, id, name, updatedAt: now }); return id; } @@ -887,74 +767,15 @@ export class DatabaseService { * Removes a child id from its parent message's children array. */ private static async removeChildFromParent(messageId: string): Promise { - const message = await db[IDXDB_TABLES.messages].get(messageId); + const message = await backend.getMessage(messageId); if (!message?.parent) return; - const parent = await db[IDXDB_TABLES.messages].get(message.parent); + const parent = await backend.getMessage(message.parent); if (!parent) return; parent.children = parent.children.filter((childId: string) => childId !== messageId); - await db[IDXDB_TABLES.messages].put(parent); - } - - /** - * Reparents direct children of `parentId` to the nearest surviving - * ancestor (or promotes them to top-level when the immediate parent was - * top-level). Walking skips any ancestor listed in `excludeIds`, since - * those will be deleted in the same batch — leaving a grandchild pointing - * at an `excludeIds` entry would orphan it. Children whose own id is in - * `excludeIds` are dropped from the updates (the bulk-delete pass will - * remove them). `prefetched` may carry a pre-fetched ancestor map to - * avoid repeat reads inside a bulk transaction. - */ - private static async reparentDirectChildren( - parentId: string, - excludeIds: ReadonlySet = new Set(), - prefetched?: ReadonlyMap - ): Promise { - const conv = prefetched?.get(parentId) ?? (await db[IDXDB_TABLES.conversations].get(parentId)); - - if (!conv) return; - - let newParent = conv.forkedFromConversationId; - - const visited = new Set([parentId]); - - while (newParent && excludeIds.has(newParent)) { - if (visited.has(newParent)) { - newParent = undefined; - - break; - } - - visited.add(newParent); - const next = - prefetched?.get(newParent) ?? (await db[IDXDB_TABLES.conversations].get(newParent)); - - if (!next) { - newParent = undefined; - - break; - } - - newParent = next.forkedFromConversationId; - } - - const directChildren = await db[IDXDB_TABLES.conversations] - .filter((c) => c.forkedFromConversationId === parentId) - .toArray(); - const updates: DatabaseConversation[] = []; - - for (const child of directChildren) { - if (excludeIds.has(child.id)) continue; - - updates.push({ ...child, forkedFromConversationId: newParent }); - } - - if (updates.length === 0) return; - - await db[IDXDB_TABLES.conversations].bulkPut(updates); + await backend.putMessages([parent]); } } diff --git a/ui/apps/chat/src/lib/services/document-store-backend.ts b/ui/apps/chat/src/lib/services/document-store-backend.ts new file mode 100644 index 000000000000..c9e07f891716 --- /dev/null +++ b/ui/apps/chat/src/lib/services/document-store-backend.ts @@ -0,0 +1,502 @@ +/** + * DocumentStoreBackend - StorageBackend over a StorageAdapter + * + * The adapter stores one document per conversation. Row writes are applied + * to a cached copy of the document and recorded as ops; at the end of a + * transaction every touched document is written back. When another client + * wrote the document meanwhile (StorageConflictError), the fresh document is + * fetched and the ops are replayed on it, so concurrent edits to different + * rows both survive. Records (groups, projects, prompts) are small and written + * whole, merged per id by the adapter. + */ + +import { StorageConflictError } from '#lib/storage-adapters/storage-conflict-error.ts'; +import type { + DatabaseRecordKind, + DatabaseRecords, + MessageTimeRange, + StorageAdapter, + StorageBackend, + StoredConversation +} from '#lib/types/index.ts'; + +/** Turns a document (null: none) into its next state (null: deleted), without mutating it */ +type DocumentOp = (doc: StoredConversation | null) => StoredConversation | null; + +type AnyRecord = DatabaseRecords[DatabaseRecordKind]; + +interface Batch { + /** Cached state of each touched document before the transaction, undefined when not loaded */ + before: Map; + ops: Map; + /** Cached state of each touched record before the transaction, undefined when absent */ + records: Map>; +} + +const MAX_CONFLICT_RETRIES = 3; + +function groupByConversation(messages: DatabaseMessage[]): Map { + const groups = new Map(); + + for (const msg of messages) { + const group = groups.get(msg.convId); + + if (group) group.push(msg); + else groups.set(msg.convId, [msg]); + } + + return groups; +} + +function requireDocument(doc: StoredConversation | null, convId: string): StoredConversation { + if (!doc) throw new Error(`Conversation ${convId} not found`); + + return doc; +} + +export class DocumentStoreBackend implements StorageBackend { + private readonly adapter: StorageAdapter; + /** Loaded documents by conversation id, null when the store has none */ + private docs = new Map(); + /** Conversation metadata by id, null until the list is fetched */ + private index: Map | null = null; + /** Owning conversation of every message in a loaded document */ + private messageConversation = new Map(); + /** Writes recorded by the running transaction */ + private pending: Batch | null = null; + private queue: Promise = Promise.resolve(); + /** Loaded records by kind and id */ + private recordCache = new Map>(); + + async addConversation(conv: DatabaseConversation): Promise { + const index = await this.getIndex(); + + if (index.has(conv.id)) throw new Error(`Conversation ${conv.id} already exists`); + + const row = structuredClone(conv); + + await this.record(conv.id, (doc) => ({ conv: row, messages: doc?.messages ?? [], rev: 0 }), { + load: false + }); + } + + async addMessages(messages: DatabaseMessage[]): Promise { + for (const [convId, rows] of groupByConversation(structuredClone(messages))) { + await this.record(convId, (doc) => { + const current = requireDocument(doc, convId); + + return { ...current, messages: [...current.messages, ...rows] }; + }); + } + } + + async addRecord( + kind: K, + record: DatabaseRecords[K] + ): Promise { + const row = structuredClone(record); + + await this.writeRecord(kind, record.id, (current) => { + if (current) throw new Error(`${kind} record ${record.id} already exists`); + + return row; + }); + } + + constructor(adapter: StorageAdapter) { + this.adapter = adapter; + } + + async deleteConversations(ids: string[]): Promise { + for (const id of ids) { + await this.record(id, () => null, { load: false }); + } + } + + async deleteMessages(ids: string[]): Promise { + const byConversation = new Map>(); + + for (const id of ids) { + const convId = this.messageConversation.get(id); + + if (!convId) continue; + + const set = byConversation.get(convId) ?? new Set(); + + set.add(id); + byConversation.set(convId, set); + } + + for (const [convId, set] of byConversation) { + await this.record( + convId, + (doc) => doc && { ...doc, messages: doc.messages.filter((m) => !set.has(m.id)) } + ); + } + } + + async deleteRecord(kind: DatabaseRecordKind, id: string): Promise { + await this.writeRecord(kind, id, () => undefined); + } + + async getConversations(ids: string[]): Promise<(DatabaseConversation | undefined)[]> { + const index = await this.getIndex(); + + return ids.map((id) => { + const conv = index.get(id); + + return conv ? structuredClone(conv) : undefined; + }); + } + + async getMessage(id: string): Promise { + const convId = this.messageConversation.get(id); + const msg = convId ? this.docs.get(convId)?.messages.find((m) => m.id === id) : undefined; + + return msg ? structuredClone(msg) : undefined; + } + + async getMessages(convIds: string[]): Promise { + await Promise.all(convIds.map((id) => this.load(id))); + + return convIds.flatMap((id) => structuredClone(this.docs.get(id)?.messages ?? [])); + } + + async listConversations(): Promise { + // outside a transaction, pick up what other clients wrote since the last read + const index = this.pending ? await this.getIndex() : await this.refreshIndex(); + + return [...index.values()] + .sort((a, b) => b.lastModified - a.lastModified || (a.id < b.id ? 1 : -1)) + .map((conv) => structuredClone(conv)); + } + + async listRecords(kind: K): Promise { + // outside a transaction, pick up what other clients wrote since the last read + const records = this.pending ? await this.loadRecords(kind) : await this.refreshRecords(kind); + + return [...records.values()].map((record) => structuredClone(record)); + } + + async putConversations(convs: DatabaseConversation[]): Promise { + for (const conv of structuredClone(convs)) { + await this.record(conv.id, (doc) => + doc ? { ...doc, conv } : { conv, messages: [], rev: 0 } + ); + } + } + + async putMessages(messages: DatabaseMessage[]): Promise { + for (const [convId, rows] of groupByConversation(structuredClone(messages))) { + await this.record(convId, (doc) => { + const current = requireDocument(doc, convId); + const byId = new Map(rows.map((row) => [row.id, row])); + const kept = current.messages.map((m) => byId.get(m.id) ?? m); + const known = new Set(current.messages.map((m) => m.id)); + + return { ...current, messages: [...kept, ...rows.filter((row) => !known.has(row.id))] }; + }); + } + } + + async scanMessages( + convIds: string[] | null, + visit: (message: DatabaseMessage) => void, + range?: MessageTimeRange + ): Promise { + const ids = convIds ?? [...(await this.getIndex()).keys()]; + + await Promise.all(ids.map((id) => this.load(id))); + + const docs = ids.flatMap((id) => this.docs.get(id) ?? []); + const inRange = ({ timestamp }: DatabaseMessage) => + (range?.from === undefined || timestamp >= range.from) && + (range?.to === undefined || timestamp < range.to); + + for (const doc of docs) { + if (range && !doc.messages.some(inRange)) continue; + + // visit reads only, so the cached rows are handed over without a copy + for (const msg of doc.messages) visit(msg); + } + } + + async transaction(fn: () => Promise): Promise { + // a call made while a transaction runs joins it + if (this.pending) return await fn(); + + const run = async (): Promise => { + const batch: Batch = { before: new Map(), ops: new Map(), records: new Map() }; + + let result: T; + + this.pending = batch; + + try { + result = await fn(); + } catch (error) { + this.rollback(batch); + + throw error; + } finally { + // writes that arrive while flushing start a transaction of their own + this.pending = null; + } + + // a failed write keeps its local edits, the next write of that document carries them + await this.flush(batch); + + return result; + }; + const result = this.queue.then(run, run); + + this.queue = result.catch(() => {}); + + return await result; + } + + async updateConversation( + id: string, + updates: Partial> + ): Promise { + const index = await this.getIndex(); + + if (!index.has(id)) return; + + const changes = structuredClone(updates); + + await this.record(id, (doc) => doc && { ...doc, conv: { ...doc.conv, ...changes } }); + } + + async updateMessage(id: string, updates: Partial>): Promise { + const convId = this.messageConversation.get(id); + + if (!convId) return; + + const changes = structuredClone(updates); + + await this.record( + convId, + (doc) => + doc && { + ...doc, + messages: doc.messages.map((m) => (m.id === id ? { ...m, ...changes } : m)) + } + ); + } + + async updateRecord( + kind: K, + id: string, + updates: Partial> + ): Promise { + const changes = structuredClone(updates); + + await this.writeRecord(kind, id, (current) => { + if (!current) return undefined; + + const next = { ...current, ...changes }; + + for (const [key, value] of Object.entries(changes)) { + if (value === undefined) delete next[key as keyof typeof next]; + } + + return next; + }); + } + + /** Store a document's next state locally, keeping the revision it is based on. */ + private applyLocal(convId: string, doc: StoredConversation | null, baseRev: number): void { + const previous = this.docs.get(convId); + + for (const msg of previous?.messages ?? []) this.messageConversation.delete(msg.id); + + if (doc) { + this.docs.set(convId, { ...doc, rev: baseRev }); + this.index?.set(convId, doc.conv); + + for (const msg of doc.messages) this.messageConversation.set(msg.id, convId); + } else { + this.docs.set(convId, null); + this.index?.delete(convId); + } + } + + private async flush(batch: Batch): Promise { + for (const [kind, touched] of batch.records) { + const records = this.recordCache.get(kind); + const set: AnyRecord[] = []; + const remove: string[] = []; + + for (const id of touched.keys()) { + const record = records?.get(id); + + if (record) set.push(record); + else remove.push(id); + } + + await this.adapter.updateRecords(kind, set, remove); + } + + for (const [convId, ops] of batch.ops) { + for (let attempt = 0; ; attempt++) { + const doc = this.docs.get(convId) ?? null; + + try { + if (doc) { + doc.rev = await this.adapter.putConversation(doc); + } else { + await this.adapter.deleteConversation(convId); + } + + break; + } catch (error) { + if (!(error instanceof StorageConflictError) || attempt >= MAX_CONFLICT_RETRIES) + throw error; + + const fresh = await this.adapter.getConversation(convId); + + this.applyLocal( + convId, + ops.reduce((current, op) => op(current), fresh), + fresh?.rev ?? 0 + ); + } + } + } + } + + private async getIndex(): Promise> { + return this.index ?? (await this.refreshIndex()); + } + + private async load(convId: string): Promise { + if (this.docs.has(convId)) return; + + const doc = await this.adapter.getConversation(convId); + + // a concurrent load may have landed first, keep the copy that may carry local edits + if (this.docs.has(convId)) return; + + this.applyLocal(convId, doc, doc?.rev ?? 0); + } + + private async loadRecords( + kind: K + ): Promise> { + return (this.recordCache.get(kind) ?? (await this.refreshRecords(kind))) as Map< + string, + DatabaseRecords[K] + >; + } + + /** + * Record a write to one conversation document: apply it to the cached copy + * now and keep it for replay until the transaction is flushed. + */ + private async record( + convId: string, + op: DocumentOp, + options: { load: boolean } = { load: true } + ): Promise { + const batch = this.pending; + + if (!batch) return await this.transaction(() => this.record(convId, op, options)); + + if (options.load) await this.load(convId); + + // the transaction this write joined may have closed while the document loaded + if (this.pending !== batch) return await this.record(convId, op, options); + + const current = this.docs.get(convId) ?? null; + const ops = batch.ops.get(convId) ?? []; + + if (!batch.before.has(convId)) batch.before.set(convId, this.docs.get(convId)); + + this.applyLocal(convId, op(current), current?.rev ?? 0); + ops.push(op); + batch.ops.set(convId, ops); + } + + private async refreshIndex(): Promise> { + const entries = await this.adapter.listConversations(); + const index = new Map(); + + // cached documents stay as they are; a stale one is refreshed by the 409 on its next write + for (const { conv } of entries) { + index.set(conv.id, conv); + } + + this.index = index; + + return index; + } + + private async refreshRecords( + kind: K + ): Promise> { + const rows = await this.adapter.listRecords(kind); + const records = new Map(rows.map((row) => [row.id, row])); + + this.recordCache.set(kind, records); + + return records; + } + + /** Restore the documents a failed transaction touched, the index reloads on the next read. */ + private rollback(batch: Batch): void { + for (const [kind, touched] of batch.records) { + const records = this.recordCache.get(kind); + + for (const [id, before] of touched) { + if (before) records?.set(id, before); + else records?.delete(id); + } + } + + for (const [convId, before] of batch.before) { + if (before === undefined) { + for (const msg of this.docs.get(convId)?.messages ?? []) { + this.messageConversation.delete(msg.id); + } + + this.docs.delete(convId); + } else { + this.applyLocal(convId, before, before?.rev ?? 0); + } + } + + this.index = null; + } + + /** + * Record a write to one record: apply `next` to the cached copy now, it is + * sent whole when the transaction is flushed. + */ + private async writeRecord( + kind: K, + id: string, + next: (current: DatabaseRecords[K] | undefined) => DatabaseRecords[K] | undefined + ): Promise { + const batch = this.pending; + + if (!batch) return await this.transaction(() => this.writeRecord(kind, id, next)); + + const records = await this.loadRecords(kind); + + // the transaction this write joined may have closed while the records loaded + if (this.pending !== batch) return await this.writeRecord(kind, id, next); + + const touched = batch.records.get(kind) ?? new Map(); + const current = records.get(id); + + if (!touched.has(id)) touched.set(id, current); + + batch.records.set(kind, touched); + + const value = next(current); + + if (value) records.set(id, value); + else records.delete(id); + } +} diff --git a/ui/apps/chat/src/lib/services/idb-backend.ts b/ui/apps/chat/src/lib/services/idb-backend.ts new file mode 100644 index 000000000000..53ae7d17de5d --- /dev/null +++ b/ui/apps/chat/src/lib/services/idb-backend.ts @@ -0,0 +1,185 @@ +/** + * IdbBackend - IndexedDB (Dexie) implementation of StorageBackend + * + * Row-level reads and writes over the conversations/messages tables and the + * record tables (groups, projects, prompts). Calls made inside `transaction()` + * join the same Dexie transaction. + */ + +import { + IDXDB_STORE_SCHEMAS, + IDXDB_STORES_V1, + IDXDB_TABLES, + IDXDB_VERSION_GROUPS, + IDXDB_VERSION_PROJECTS, + IDXDB_VERSION_PROMPTS, + STORAGE_APP_NAME +} from '#lib/constants/index.ts'; +import type { + DatabasePrompt, + DatabaseRecordKind, + DatabaseRecords, + MessageTimeRange, + StorageBackend +} from '#lib/types/database.js'; +import type { Group } from '#lib/types/groups.js'; +import type { Project } from '#lib/types/projects.js'; +import Dexie, { type EntityTable, type Table, type UpdateSpec } from 'dexie'; + +class LlamaUiDatabase extends Dexie { + [IDXDB_TABLES.conversations]!: EntityTable; + [IDXDB_TABLES.groups]!: EntityTable; + [IDXDB_TABLES.messages]!: EntityTable; + [IDXDB_TABLES.projects]!: EntityTable; + [IDXDB_TABLES.prompts]!: EntityTable; + + constructor() { + super(STORAGE_APP_NAME); + + this.version(1).stores(IDXDB_STORES_V1); + this.version(IDXDB_VERSION_GROUPS).stores({ + [IDXDB_TABLES.groups]: IDXDB_STORE_SCHEMAS.groups + }); + this.version(IDXDB_VERSION_PROJECTS).stores({ + [IDXDB_TABLES.projects]: IDXDB_STORE_SCHEMAS.projects + }); + this.version(IDXDB_VERSION_PROMPTS).stores({ + [IDXDB_TABLES.prompts]: IDXDB_STORE_SCHEMAS.prompts + }); + } +} + +export class IdbBackend implements StorageBackend { + private readonly db = new LlamaUiDatabase(); + + private get conversations() { + return this.db[IDXDB_TABLES.conversations]; + } + + private get messages() { + return this.db[IDXDB_TABLES.messages]; + } + + async addConversation(conv: DatabaseConversation): Promise { + await this.conversations.add(conv); + } + + async addMessages(messages: DatabaseMessage[]): Promise { + await this.messages.bulkAdd(messages); + } + + async addRecord( + kind: K, + record: DatabaseRecords[K] + ): Promise { + await this.records(kind).add(record); + } + + async deleteConversations(ids: string[]): Promise { + await this.conversations.bulkDelete(ids); + await this.messages.where('convId').anyOf(ids).delete(); + } + + async deleteMessages(ids: string[]): Promise { + await this.messages.bulkDelete(ids); + } + + async deleteRecord(kind: DatabaseRecordKind, id: string): Promise { + await this.records(kind).delete(id); + } + + async getConversations(ids: string[]): Promise<(DatabaseConversation | undefined)[]> { + return await this.conversations.bulkGet(ids); + } + + async getMessage(id: string): Promise { + return await this.messages.get(id); + } + + async getMessages(convIds: string[]): Promise { + return await this.messages.where('convId').anyOf(convIds).toArray(); + } + + async listConversations(): Promise { + return await this.conversations.orderBy('lastModified').reverse().toArray(); + } + + async listRecords(kind: K): Promise { + return await this.records(kind).toArray(); + } + + async putConversations(convs: DatabaseConversation[]): Promise { + await this.conversations.bulkPut(convs); + } + + async putMessages(messages: DatabaseMessage[]): Promise { + await this.messages.bulkPut(messages); + } + + async scanMessages( + convIds: string[] | null, + visit: (message: DatabaseMessage) => void, + range?: MessageTimeRange + ): Promise { + // one transaction, so the range lookup and the read see the same data + await this.db.transaction('r', [this.messages], async () => { + let ids = convIds; + + if (range) { + const { from, to } = range; + const index = this.messages.where('timestamp'); + const inRange = + from !== undefined && to !== undefined + ? index.between(from, to, true, false) + : from !== undefined + ? index.aboveOrEqual(from) + : index.below(to); + const active = new Set(); + + await inRange.each((m) => active.add(m.convId)); + ids = ids ? ids.filter((id) => active.has(id)) : [...active]; + } + + await (ids ? this.messages.where('convId').anyOf(ids) : this.messages.toCollection()).each( + visit + ); + }); + } + + async transaction(fn: () => Promise): Promise { + return await this.db.transaction( + 'rw', + [ + this.conversations, + this.messages, + this.records(IDXDB_TABLES.groups), + this.records(IDXDB_TABLES.projects), + this.records(IDXDB_TABLES.prompts) + ], + fn + ); + } + + async updateConversation( + id: string, + updates: Partial> + ): Promise { + await this.conversations.update(id, updates); + } + + async updateMessage(id: string, updates: Partial>): Promise { + await this.messages.update(id, updates); + } + + async updateRecord( + kind: K, + id: string, + updates: Partial> + ): Promise { + await this.records(kind).update(id, updates as unknown as UpdateSpec); + } + + private records(kind: K): Table { + return this.db[kind] as unknown as Table; + } +} diff --git a/ui/apps/chat/src/lib/services/index.ts b/ui/apps/chat/src/lib/services/index.ts index c6d7a25c3433..a052ca3889d4 100644 --- a/ui/apps/chat/src/lib/services/index.ts +++ b/ui/apps/chat/src/lib/services/index.ts @@ -68,16 +68,17 @@ export { ServersService } from './servers.service'; export { ChatService } from './chat.service'; /** - * **DatabaseService** - IndexedDB persistence layer via Dexie ORM + * **DatabaseService** - persistence layer for conversations and messages * - * Provides stateless data access for conversations and messages using IndexedDB. - * Handles all low-level storage operations including branching tree structures, - * cascade deletions, and transaction safety for multi-table operations. + * Provides stateless data access for conversations and messages on top of a + * row-level `StorageBackend` (IndexedDB via Dexie by default, see idb-backend.ts). + * Handles branching tree structures, cascade deletions, and transaction safety + * for multi-table operations; the pure tree rules live in utils/conversation-tree. * * **Architecture & Relationships (bottom to top):** - * - **DatabaseService** (this class): Stateless IndexedDB operations - * - Lowest layer — direct Dexie/IndexedDB communication - * - Pure CRUD operations without business logic + * - **StorageBackend** (IdbBackend): row reads and writes, transactions + * + * - **DatabaseService** (this class): Stateless tree-aware operations * - Handles branching tree structure (parent-child relationships) * - Provides transaction safety for multi-table operations * @@ -327,3 +328,27 @@ export { MigrationService } from './migration.service'; * @see settingsStore in stores/settings/index.svelte.ts - reactive state + business logic */ export { SettingsService } from './settings.service'; + +/** + * **PreferencesService** - single access point for persisted key/value preferences + * + * Mirrors the localStorage API and classifies each key as the user's own data + * or device-local UI state, so a later storage backend can sync only the former. + * + * @see USER_PREFERENCE_KEYS in constants/storage.constants.ts - the user data keys + */ +export { PreferencesService } from './preferences.service'; + +/** + * **StorageModeService** - chooses where conversations and user settings live + * + * This browser (IndexedDB + localStorage) by default, or a storage adapter the + * user switched to: llama-server with --ui-storage, or one added in + * $lib/storage-adapters. Activated once at startup, switching copies this + * browser's data and reloads the app. + * + * - **StorageAdapter**: a remote document store, one document per conversation + * - **DocumentStoreBackend**: StorageBackend over an adapter, see document-store-backend.ts + */ +export { DocumentStoreBackend } from './document-store-backend'; +export { StorageModeService } from './storage-mode.service'; diff --git a/ui/apps/chat/src/lib/services/preferences.service.ts b/ui/apps/chat/src/lib/services/preferences.service.ts new file mode 100644 index 000000000000..235478b945a1 --- /dev/null +++ b/ui/apps/chat/src/lib/services/preferences.service.ts @@ -0,0 +1,98 @@ +import { USER_PREFERENCE_KEYS } from '#lib/constants/index.ts'; +import { PreferenceScope } from '#lib/enums/index.ts'; + +/** Sends a batch of changed user preferences to where they are stored remotely */ +export type PreferencesPush = (set: Record, remove: string[]) => Promise; + +/** + * PreferencesService - single access point for persisted key/value preferences + * + * Mirrors the localStorage API, errors included, so callers keep their own + * handling. Every key is either the user's own data or device-local UI state + * (see {@link PreferencesService.scopeOf}). Device-local keys always live in + * localStorage; user data does too unless {@link PreferencesService.useRemote} + * moved it to the server, in which case it is read from memory and written + * back in batches. + * + * Legacy key migrations in MigrationService read localStorage directly, since + * they are about this browser's past data. + */ +export class PreferencesService { + private static outbox = { remove: new Set(), set: new Map() }; + private static push: PreferencesPush | null = null; + private static pushing: Promise = Promise.resolve(); + private static pushScheduled = false; + private static remote: Map | null = null; + + /** Whether user data is currently read from and written to the server. */ + static get isRemote(): boolean { + return this.remote !== null; + } + + static getItem(key: string): string | null { + if (this.isRemoteKey(key)) return this.remote!.get(key) ?? null; + + return localStorage.getItem(key); + } + + static removeItem(key: string): void { + if (this.isRemoteKey(key)) { + this.remote!.delete(key); + this.outbox.set.delete(key); + this.outbox.remove.add(key); + this.schedulePush(); + + return; + } + + localStorage.removeItem(key); + } + + /** Whether a key holds the user's own data or this browser's UI state. */ + static scopeOf(key: string): PreferenceScope { + return USER_PREFERENCE_KEYS.has(key) ? PreferenceScope.USER : PreferenceScope.DEVICE; + } + + static setItem(key: string, value: string): void { + if (this.isRemoteKey(key)) { + this.remote!.set(key, value); + this.outbox.remove.delete(key); + this.outbox.set.set(key, value); + this.schedulePush(); + + return; + } + + localStorage.setItem(key, value); + } + + /** Serve user data from `values` (loaded from the server) and send changes through `push`. */ + static useRemote(values: Record, push: PreferencesPush): void { + this.remote = new Map(Object.entries(values)); + this.push = push; + } + + private static isRemoteKey(key: string): boolean { + return this.remote !== null && this.scopeOf(key) === PreferenceScope.USER; + } + + /** Coalesce writes made in the same task into one request, sent in order. */ + private static schedulePush(): void { + if (this.pushScheduled) return; + + this.pushScheduled = true; + queueMicrotask(() => { + this.pushScheduled = false; + + const set = Object.fromEntries(this.outbox.set); + const remove = [...this.outbox.remove]; + const push = this.push; + + this.outbox = { remove: new Set(), set: new Map() }; + + if (!push) return; + + this.pushing = this.pushing.then(() => push(set, remove)).catch(() => {}); + }); + } +} diff --git a/ui/apps/chat/src/lib/services/search-chats.service.ts b/ui/apps/chat/src/lib/services/search-chats.service.ts index 1d42addcd491..6aceb1625149 100644 --- a/ui/apps/chat/src/lib/services/search-chats.service.ts +++ b/ui/apps/chat/src/lib/services/search-chats.service.ts @@ -1,7 +1,7 @@ /** * SearchChatsService - executor for the search_chats browser tool * - * Reads conversations and messages in readonly transactions; matching and formatting live in utils/search-chats. + * Reads conversations and messages through DatabaseService; matching and formatting live in utils/search-chats. */ import { DatabaseService } from './database.service'; @@ -16,13 +16,7 @@ import { } from '#lib/utils/search-chats.ts'; function loadConversations(ids?: string[]): Promise { - return DatabaseService.readOnly(async ({ conversations }) => { - if (!ids) return conversations.toArray(); - - const found = await conversations.bulkGet(ids); - - return found.filter((c): c is DatabaseConversation => !!c); - }); + return ids ? DatabaseService.getConversations(ids) : DatabaseService.getAllConversations(); } /** Attachment payloads (base64 media, file text) are dropped as rows stream in, only their names are kept */ @@ -36,37 +30,15 @@ function withoutPayloads({ extra, ...message }: DatabaseMessage): DatabaseMessag }; } -/** One transaction, so the range lookup and the read see the same data */ -function loadMessages( +async function loadMessages( convIds: string[] | null, range?: SearchChatsTimeRange ): Promise { - return DatabaseService.readOnly(async ({ messages }) => { - let ids = convIds; - - if (range) { - const { from, to } = range; - const index = messages.where('timestamp'); - const inRange = - from !== undefined && to !== undefined - ? index.between(from, to, true, false) - : from !== undefined - ? index.aboveOrEqual(from) - : index.below(to); - const active = new Set(); - - await inRange.each((m) => active.add(m.convId)); - ids = ids ? ids.filter((id) => active.has(id)) : [...active]; - } - - const rows: DatabaseMessage[] = []; + const rows: DatabaseMessage[] = []; - await (ids ? messages.where('convId').anyOf(ids) : messages.toCollection()).each((m) => - rows.push(withoutPayloads(m)) - ); + await DatabaseService.scanMessages(convIds, (m) => rows.push(withoutPayloads(m)), range); - return rows; - }); + return rows; } const loader = { messages: loadMessages }; diff --git a/ui/apps/chat/src/lib/services/settings.service.ts b/ui/apps/chat/src/lib/services/settings.service.ts index f14b2950d9a3..620965a80ede 100644 --- a/ui/apps/chat/src/lib/services/settings.service.ts +++ b/ui/apps/chat/src/lib/services/settings.service.ts @@ -1,4 +1,10 @@ -import { CONFIG_LOCALSTORAGE_KEY, USER_OVERRIDES_LOCALSTORAGE_KEY } from '#lib/constants/index.ts'; +import { PreferencesService } from './preferences.service'; +import { + API_KEY_LOCALSTORAGE_KEY, + CONFIG_LOCALSTORAGE_KEY, + SETTINGS_KEYS, + USER_OVERRIDES_LOCALSTORAGE_KEY +} from '#lib/constants/index.ts'; import { browser } from '$app/env'; /** @@ -27,11 +33,19 @@ export class SettingsService { } try { - const storedConfigRaw = localStorage.getItem(CONFIG_LOCALSTORAGE_KEY); + const storedConfigRaw = PreferencesService.getItem(CONFIG_LOCALSTORAGE_KEY); const isFirstVisit = storedConfigRaw === null; const config = JSON.parse(storedConfigRaw || '{}') as Record; + + // with the config on the server, the API key stays in this browser + if (PreferencesService.isRemote) { + const apiKey = PreferencesService.getItem(API_KEY_LOCALSTORAGE_KEY); + + if (apiKey) config[SETTINGS_KEYS.API_KEY] = apiKey; + } + const userOverrides = JSON.parse( - localStorage.getItem(USER_OVERRIDES_LOCALSTORAGE_KEY) || '[]' + PreferencesService.getItem(USER_OVERRIDES_LOCALSTORAGE_KEY) || '[]' ) as string[]; return { config, isFirstVisit, userOverrides }; @@ -49,10 +63,10 @@ export class SettingsService { static migrateLegacyTheme(): string | null { if (!browser) return null; - const legacyTheme = localStorage.getItem('theme'); + const legacyTheme = PreferencesService.getItem('theme'); if (legacyTheme) { - localStorage.removeItem('theme'); + PreferencesService.removeItem('theme'); return legacyTheme; } @@ -67,8 +81,20 @@ export class SettingsService { if (!browser) return; try { - localStorage.setItem(CONFIG_LOCALSTORAGE_KEY, JSON.stringify(config)); - localStorage.setItem(USER_OVERRIDES_LOCALSTORAGE_KEY, JSON.stringify(userOverrides)); + if (PreferencesService.isRemote) { + const { [SETTINGS_KEYS.API_KEY]: apiKey, ...shared } = config; + + if (typeof apiKey === 'string' && apiKey) { + PreferencesService.setItem(API_KEY_LOCALSTORAGE_KEY, apiKey); + } else { + PreferencesService.removeItem(API_KEY_LOCALSTORAGE_KEY); + } + + config = shared; + } + + PreferencesService.setItem(CONFIG_LOCALSTORAGE_KEY, JSON.stringify(config)); + PreferencesService.setItem(USER_OVERRIDES_LOCALSTORAGE_KEY, JSON.stringify(userOverrides)); } catch (error) { console.error('Failed to save config to localStorage:', error); } diff --git a/ui/apps/chat/src/lib/services/storage-mode.service.ts b/ui/apps/chat/src/lib/services/storage-mode.service.ts new file mode 100644 index 000000000000..e942f5d7ea73 --- /dev/null +++ b/ui/apps/chat/src/lib/services/storage-mode.service.ts @@ -0,0 +1,154 @@ +import { DatabaseService } from './database.service'; +import { DocumentStoreBackend } from './document-store-backend'; +import { PreferencesService } from './preferences.service'; +import { + API_KEY_LOCALSTORAGE_KEY, + CONFIG_LOCALSTORAGE_KEY, + IDXDB_TABLES, + SETTINGS_KEYS, + STORAGE_ADAPTER_LOCALSTORAGE_KEY, + USER_PREFERENCE_KEYS +} from '#lib/constants/index.ts'; +import { findStorageAdapter, StorageConflictError } from '#lib/storage-adapters/index.ts'; +import type { StorageAdapterDefinition } from '#lib/types/index.ts'; + +/** + * StorageModeService - chooses where conversations and user settings live + * + * This browser (IndexedDB + localStorage) by default, or one of the storage + * adapters in $lib/storage-adapters. The choice is kept per browser. When the + * chosen adapter cannot be reached at startup the app falls back to this + * browser for that session and keeps the choice for the next one. + */ +export class StorageModeService { + private static active: StorageAdapterDefinition | null = null; + + /** Adapter this session runs with, null for this browser, which can differ from the chosen one after a fallback. */ + static get activeAdapter(): StorageAdapterDefinition | null { + return this.active; + } + + /** Adapter chosen in this browser, null for this browser. */ + static get chosenAdapter(): StorageAdapterDefinition | null { + try { + return ( + findStorageAdapter(PreferencesService.getItem(STORAGE_ADAPTER_LOCALSTORAGE_KEY)) ?? null + ); + } catch { + return null; + } + } + + /** + * Connect to the chosen storage adapter, if any. Runs once at startup, + * before any store reads conversations or settings. + * + * @param onPushError - Called when a settings change could not be saved + * @returns The adapter the session runs with, null for this browser + */ + static async activate( + onPushError: (error: unknown) => void + ): Promise { + const definition = this.chosenAdapter; + + if (!definition) return this.active; + + try { + const adapter = definition.create(); + const settings = await adapter.getSettings(); + + PreferencesService.useRemote(settings, async (set, remove) => { + try { + await adapter.updateSettings(set, remove); + } catch (error) { + onPushError(error); + } + }); + DatabaseService.useBackend(new DocumentStoreBackend(adapter)); + this.active = definition; + } catch (error) { + console.error(`${definition.name} storage is unavailable, using browser storage:`, error); + } + + return this.active; + } + + /** + * Copy this browser's conversations, groups, projects, prompts and settings + * through `definition`, then use it from the next load on. What the store + * already holds is kept as it is. + * + * @returns How many conversations were copied and how many the store already had + */ + static async switchTo( + definition: StorageAdapterDefinition + ): Promise<{ copied: number; skipped: number }> { + const adapter = definition.create(); + const conversations = await DatabaseService.getAllConversations(); + const documents = await DatabaseService.getConversationsWithMessages( + conversations.map((conv) => conv.id) + ); + + let copied = 0; + let skipped = 0; + + for (const { conv, messages } of documents.values()) { + try { + await adapter.putConversation({ conv, messages, rev: 0 }); + copied++; + } catch (error) { + if (!(error instanceof StorageConflictError)) throw error; + + skipped++; + } + } + + const localRecords = { + [IDXDB_TABLES.groups]: await DatabaseService.getAllGroups(), + [IDXDB_TABLES.projects]: await DatabaseService.getAllProjects(), + [IDXDB_TABLES.prompts]: await DatabaseService.getAllPrompts() + }; + + for (const kind of [IDXDB_TABLES.groups, IDXDB_TABLES.projects, IDXDB_TABLES.prompts]) { + const stored = new Set((await adapter.listRecords(kind)).map((record) => record.id)); + const missing = localRecords[kind].filter((record) => !stored.has(record.id)); + + if (missing.length > 0) await adapter.updateRecords(kind, missing, []); + } + + const remote = await adapter.getSettings(); + const localConfigRaw = PreferencesService.getItem(CONFIG_LOCALSTORAGE_KEY); + const localConfig = JSON.parse(localConfigRaw || '{}') as Record; + // the API key stays in this browser, see SettingsService + const { [SETTINGS_KEYS.API_KEY]: apiKey, ...sharedConfig } = localConfig; + const set: Record = {}; + + for (const key of USER_PREFERENCE_KEYS) { + const value = + key === CONFIG_LOCALSTORAGE_KEY + ? localConfigRaw && JSON.stringify(sharedConfig) + : PreferencesService.getItem(key); + + if (value === null || key in remote) continue; + + set[key] = value; + } + + await adapter.updateSettings(set, []); + + if (typeof apiKey === 'string' && apiKey) { + PreferencesService.setItem(API_KEY_LOCALSTORAGE_KEY, apiKey); + } + + PreferencesService.setItem(STORAGE_ADAPTER_LOCALSTORAGE_KEY, definition.id); + + return { copied, skipped }; + } + + /** + * Use this browser's storage from the next load on. Data in the adapter stays there. + */ + static switchToBrowser(): void { + PreferencesService.removeItem(STORAGE_ADAPTER_LOCALSTORAGE_KEY); + } +} diff --git a/ui/apps/chat/src/lib/storage-adapters/index.ts b/ui/apps/chat/src/lib/storage-adapters/index.ts new file mode 100644 index 000000000000..7789f0446e1a --- /dev/null +++ b/ui/apps/chat/src/lib/storage-adapters/index.ts @@ -0,0 +1,22 @@ +/** + * Storage adapters - where conversations and user settings can be kept besides this browser + * + * llama-server's own JSON document store is the built-in adapter. To keep data + * elsewhere (Postgres via Supabase or Neon, PocketBase, ...), implement + * StorageAdapter in a file next to this one and add its definition below. + * + * @see tools/ui/docs/storage-adapters.md - the contract and example schemas + */ + +import { llamaServerStorageAdapter } from './llama-server.adapter'; +import type { StorageAdapterDefinition } from '#lib/types/index.ts'; + +export { LlamaServerStorageAdapter } from './llama-server.adapter'; +export { StorageConflictError } from './storage-conflict-error'; + +/** Adapters offered in Settings, in this order. */ +export const STORAGE_ADAPTERS: StorageAdapterDefinition[] = [llamaServerStorageAdapter]; + +export function findStorageAdapter(id: string | null): StorageAdapterDefinition | undefined { + return STORAGE_ADAPTERS.find((adapter) => adapter.id === id); +} diff --git a/ui/apps/chat/src/lib/storage-adapters/llama-server.adapter.ts b/ui/apps/chat/src/lib/storage-adapters/llama-server.adapter.ts new file mode 100644 index 000000000000..5dd31429cd6b --- /dev/null +++ b/ui/apps/chat/src/lib/storage-adapters/llama-server.adapter.ts @@ -0,0 +1,127 @@ +import { StorageConflictError } from './storage-conflict-error'; +import { + API_KEY_LOCALSTORAGE_KEY, + API_STORAGE, + HEADERS, + LOCAL_BACKEND_ID +} from '#lib/constants/index.ts'; +import { PreferencesService } from '#lib/services/preferences.service.ts'; +import type { + DatabaseRecordKind, + DatabaseRecords, + StorageAdapter, + StorageAdapterDefinition, + StoredConversation +} from '#lib/types/index.ts'; +import { ApiError, apiFetch } from '#lib/utils/api-fetch.ts'; + +/** + * LlamaServerStorageAdapter - the server's /storage routes (--ui-storage) + * + * The server keeps one JSON file per conversation and answers a stale write + * with 409. Requests always go to the server serving this UI, whichever server + * chats target, and carry the API key kept in this browser, since the settings + * that normally carry it are themselves loaded from the server. + */ +export class LlamaServerStorageAdapter implements StorageAdapter { + async deleteConversation(id: string): Promise { + await this.request(`${API_STORAGE.CONVERSATIONS}/${encodeURIComponent(id)}`, { + method: 'DELETE' + }); + } + + async getConversation(id: string): Promise { + try { + return await this.request( + `${API_STORAGE.CONVERSATIONS}/${encodeURIComponent(id)}` + ); + } catch (error) { + if (error instanceof ApiError && error.status === 404) return null; + + throw error; + } + } + + async getSettings(): Promise> { + return await this.request(API_STORAGE.SETTINGS); + } + + async listConversations(): Promise[]> { + return await this.request(API_STORAGE.CONVERSATIONS); + } + + async listRecords(kind: K): Promise { + const records = await this.request>( + `${API_STORAGE.RECORDS}/${encodeURIComponent(kind)}` + ); + + return Object.values(records); + } + + async putConversation(doc: StoredConversation): Promise { + try { + const { rev } = await this.request<{ rev: number }>( + `${API_STORAGE.CONVERSATIONS}/${encodeURIComponent(doc.conv.id)}`, + { body: JSON.stringify(doc), method: 'POST' } + ); + + return rev; + } catch (error) { + if (error instanceof ApiError && error.status === 409) { + throw new StorageConflictError(doc.conv.id); + } + + throw error; + } + } + + async updateRecords( + kind: K, + set: DatabaseRecords[K][], + remove: string[] + ): Promise { + await this.request(`${API_STORAGE.RECORDS}/${encodeURIComponent(kind)}`, { + body: JSON.stringify({ remove, set: Object.fromEntries(set.map((r) => [r.id, r])) }), + method: 'POST' + }); + } + + async updateSettings(set: Record, remove: string[]): Promise { + await this.request(API_STORAGE.SETTINGS, { + body: JSON.stringify({ remove, set }), + method: 'POST' + }); + } + + private authHeaders(): Record { + let apiKey: string | null = null; + + try { + apiKey = PreferencesService.getItem(API_KEY_LOCALSTORAGE_KEY)?.trim() || null; + } catch { + // storage disabled, fall back to the settings store's key + } + + return apiKey ? { [HEADERS.AUTHORIZATION]: `${HEADERS.BEARER}${apiKey}` } : {}; + } + + private async request( + path: string, + init: { body?: string; method?: string } = {} + ): Promise { + return await apiFetch(path, { + ...init, + headers: this.authHeaders(), + serverId: LOCAL_BACKEND_ID + }); + } +} + +export const llamaServerStorageAdapter: StorageAdapterDefinition = { + create: () => new LlamaServerStorageAdapter(), + description: + "Keep conversations and settings on the server's disk, so they survive clearing browser data and are shared by every browser and port that opens this server. Anyone who can reach the server can read them.", + id: 'llama-server', + isAvailable: (props) => props?.ui_storage === true, + name: 'llama-server' +}; diff --git a/ui/apps/chat/src/lib/storage-adapters/storage-conflict-error.ts b/ui/apps/chat/src/lib/storage-adapters/storage-conflict-error.ts new file mode 100644 index 000000000000..6e7ee7a7cc91 --- /dev/null +++ b/ui/apps/chat/src/lib/storage-adapters/storage-conflict-error.ts @@ -0,0 +1,7 @@ +/** Thrown by a storage adapter when a write is based on a revision the store no longer holds. */ +export class StorageConflictError extends Error { + constructor(convId: string) { + super(`Conversation ${convId} was changed by another client`); + this.name = 'StorageConflictError'; + } +} diff --git a/ui/apps/chat/src/lib/stores/conversations/preferences.svelte.ts b/ui/apps/chat/src/lib/stores/conversations/preferences.svelte.ts index 70a3e420c9a0..745ed4b26428 100644 --- a/ui/apps/chat/src/lib/stores/conversations/preferences.svelte.ts +++ b/ui/apps/chat/src/lib/stores/conversations/preferences.svelte.ts @@ -14,6 +14,7 @@ import { REASONING_EFFORT_DEFAULT_LOCALSTORAGE_KEY } from '#lib/constants/index.ts'; import { ReasoningEffort, ToolSource } from '#lib/enums/index.ts'; import { DatabaseService } from '#lib/services/database.service.ts'; +import { PreferencesService } from '#lib/services/preferences.service.ts'; // direct imports between stores, not via the barrel, to avoid circular deps import { toolsStore } from '#lib/stores/tools.svelte.ts'; import type { DatabaseConversation, ToolEntry, ToolGroup } from '#lib/types/index.ts'; @@ -23,7 +24,7 @@ function loadReasoningEffortDefault(): ReasoningEffort { if (typeof globalThis.localStorage === 'undefined') return ReasoningEffort.DEFAULT; try { - const raw = localStorage.getItem(REASONING_EFFORT_DEFAULT_LOCALSTORAGE_KEY); + const raw = PreferencesService.getItem(REASONING_EFFORT_DEFAULT_LOCALSTORAGE_KEY); return (raw as ReasoningEffort) || ReasoningEffort.DEFAULT; } catch { @@ -35,7 +36,7 @@ function loadReasoningEffortDefault(): ReasoningEffort { function saveReasoningEffortDefault(effort: ReasoningEffort): void { if (typeof globalThis.localStorage === 'undefined') return; - localStorage.setItem(REASONING_EFFORT_DEFAULT_LOCALSTORAGE_KEY, effort); + PreferencesService.setItem(REASONING_EFFORT_DEFAULT_LOCALSTORAGE_KEY, effort); } /** diff --git a/ui/apps/chat/src/lib/stores/init.ts b/ui/apps/chat/src/lib/stores/init.ts index 0c743836d1e9..951a7ee4c790 100644 --- a/ui/apps/chat/src/lib/stores/init.ts +++ b/ui/apps/chat/src/lib/stores/init.ts @@ -16,11 +16,16 @@ import { versionStore } from './version.svelte'; import { BUILT_IN_EXTENSIONS } from '#lib/extensions/builtin.ts'; import { extensions } from '#lib/extensions/registry.svelte.ts'; import { MigrationService } from '#lib/services/migration.service.ts'; +import { StorageModeService } from '#lib/services/storage-mode.service.ts'; +import type { StorageAdapterDefinition } from '#lib/types/index.ts'; import { validateApiKey } from '#lib/utils/api-key-validation.ts'; import { isRemoteServer } from '#lib/utils/servers.ts'; import { browser } from '$app/env'; +import { toast } from 'svelte-sonner'; let activeTunnel: Promise | null = null; +/** Storage adapter chosen in this browser that could not be reached at startup */ +let storageFallback: StorageAdapterDefinition | null = null; let hydration: Promise | null = null; let extensionsStarted: Promise | null = null; let startup: Promise | null = null; @@ -78,6 +83,18 @@ export function hydrateStores(): Promise { hydration ??= (async () => { await MigrationService.runAllMigrations(); + // before anything reads conversations or settings, which may live in a storage adapter + const chosen = StorageModeService.chosenAdapter; + const active = await StorageModeService.activate(() => toast.error('Failed to save settings')); + + if (active) { + // these read user data while being constructed, before the mode was known + modelsStore.reloadPersisted(); + conversationsStore.preferences.resetPending(); + } else { + storageFallback = chosen; + } + settingsStore.initialize(); // the tunnel transport goes in before anything can ask for a model list: a @@ -125,6 +142,10 @@ export function initStores(): Promise { // per-server and never block startup void serversModelsStore.loadAll().then(() => modelsStore.warmHubDetails()); + if (storageFallback) { + toast.warning(`${storageFallback.name} storage is unavailable, using this browser's storage`); + } + permissionsStore.initialize(); toolsStore.initialize(); diff --git a/ui/apps/chat/src/lib/stores/models/index.svelte.ts b/ui/apps/chat/src/lib/stores/models/index.svelte.ts index 8d765ba61ef7..c02bb9608462 100644 --- a/ui/apps/chat/src/lib/stores/models/index.svelte.ts +++ b/ui/apps/chat/src/lib/stores/models/index.svelte.ts @@ -21,6 +21,7 @@ import { import { ReasoningEffort, ServerModelSource, ServerModelStatus } from '#lib/enums/index.ts'; import { HuggingFaceService } from '#lib/services/huggingface.service.ts'; import { ModelsService } from '#lib/services/models.service.ts'; +import { PreferencesService } from '#lib/services/preferences.service.ts'; import { conversationsStore } from '#lib/stores/conversations/index.svelte.ts'; import { serverStore } from '#lib/stores/llama-server.svelte.ts'; import { type ModelPropsHost, ModelPropsManager } from '#lib/stores/models/props.svelte.ts'; @@ -52,7 +53,7 @@ function loadStoredSelection(): { id: string; model: string | null } | null { if (!browser) return null; try { - const raw = localStorage.getItem(SELECTED_MODEL_LOCALSTORAGE_KEY); + const raw = PreferencesService.getItem(SELECTED_MODEL_LOCALSTORAGE_KEY); if (!raw) return null; @@ -73,7 +74,7 @@ function loadHiddenModels(): Set { if (!browser) return new SvelteSet(); try { - const raw = localStorage.getItem(HIDDEN_MODELS_LOCALSTORAGE_KEY); + const raw = PreferencesService.getItem(HIDDEN_MODELS_LOCALSTORAGE_KEY); const ids = raw ? (JSON.parse(raw) as string[]) : []; // ids saved before models were qualified by server all belonged to the local one @@ -90,7 +91,7 @@ function loadGroupOpenState(): SvelteMap { if (!browser) return new SvelteMap(); try { - const raw = localStorage.getItem(MODEL_GROUP_OPEN_LOCALSTORAGE_KEY); + const raw = PreferencesService.getItem(MODEL_GROUP_OPEN_LOCALSTORAGE_KEY); const parsed = raw ? (JSON.parse(raw) as unknown) : null; const entries = parsed && typeof parsed === 'object' ? Object.entries(parsed as Record) : []; @@ -108,7 +109,7 @@ function loadRecentModels(): string[] { if (!browser) return []; try { - const raw = localStorage.getItem(RECENT_MODELS_LOCALSTORAGE_KEY); + const raw = PreferencesService.getItem(RECENT_MODELS_LOCALSTORAGE_KEY); if (!raw) return []; @@ -556,6 +557,13 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { return this.isModelLoaded(modelId) && !this._status.isOperationInProgress(modelId); } + /** Re-read the persisted user lists, once startup has chosen where they are stored. */ + reloadPersisted(): void { + this.favoriteModelIds = this.loadFavoritesFromStorage(); + this.hiddenModelIds = loadHiddenModels(); + this.recentModelIds = loadRecentModels(); + } + /** * Why the router would refuse to delete a model from disk, or null when it can. * Only a model from the HF cache is removable; a server that does not report @@ -689,7 +697,7 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { this.favoriteModelIds = next; try { - localStorage.setItem(FAVORITE_MODELS_LOCALSTORAGE_KEY, JSON.stringify([...next])); + PreferencesService.setItem(FAVORITE_MODELS_LOCALSTORAGE_KEY, JSON.stringify([...next])); } catch { toast.error('Failed to save favorite models to local storage'); } @@ -718,7 +726,7 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { this.groupOpenState = next; try { - localStorage.setItem( + PreferencesService.setItem( MODEL_GROUP_OPEN_LOCALSTORAGE_KEY, JSON.stringify(Object.fromEntries(next)) ); @@ -798,7 +806,7 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { this.hiddenModelIds = next; try { - localStorage.setItem(HIDDEN_MODELS_LOCALSTORAGE_KEY, JSON.stringify([...next])); + PreferencesService.setItem(HIDDEN_MODELS_LOCALSTORAGE_KEY, JSON.stringify([...next])); } catch { toast.error('Failed to save hidden models to local storage'); } @@ -898,7 +906,7 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { private loadFavoritesFromStorage(): Set { try { - const raw = localStorage.getItem(FAVORITE_MODELS_LOCALSTORAGE_KEY); + const raw = PreferencesService.getItem(FAVORITE_MODELS_LOCALSTORAGE_KEY); return raw ? new Set(JSON.parse(raw) as string[]) : new Set(); } catch { @@ -913,9 +921,9 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { try { if (!this.selectedModelId) { - localStorage.removeItem(SELECTED_MODEL_LOCALSTORAGE_KEY); + PreferencesService.removeItem(SELECTED_MODEL_LOCALSTORAGE_KEY); } else { - localStorage.setItem( + PreferencesService.setItem( SELECTED_MODEL_LOCALSTORAGE_KEY, JSON.stringify({ id: this.selectedModelId, model: this.selectedModelName }) ); @@ -935,7 +943,10 @@ class ModelsStore implements ModelPropsHost, ModelStatusHost { if (!browser) return; try { - localStorage.setItem(RECENT_MODELS_LOCALSTORAGE_KEY, JSON.stringify(this.recentModelIds)); + PreferencesService.setItem( + RECENT_MODELS_LOCALSTORAGE_KEY, + JSON.stringify(this.recentModelIds) + ); } catch { console.warn('[ModelsStore] Failed to persist the recently used models'); } diff --git a/ui/apps/chat/src/lib/stores/models/status.svelte.ts b/ui/apps/chat/src/lib/stores/models/status.svelte.ts index 46ceaa6d3921..f66a232d274f 100644 --- a/ui/apps/chat/src/lib/stores/models/status.svelte.ts +++ b/ui/apps/chat/src/lib/stores/models/status.svelte.ts @@ -21,6 +21,7 @@ import { } from '#lib/enums/index.ts'; import { HuggingFaceService } from '#lib/services/huggingface.service.ts'; import { ModelsService } from '#lib/services/models.service.ts'; +import { PreferencesService } from '#lib/services/preferences.service.ts'; import { serverStore } from '#lib/stores/llama-server.svelte.ts'; import type { ModelPropsManager } from '#lib/stores/models/props.svelte.ts'; import { serversStore } from '#lib/stores/servers.svelte.ts'; @@ -302,7 +303,7 @@ export class ModelStatusManager { constructor(private host: ModelStatusHost) { // the paused-ids registry moved to the server's unfinished-downloads record; // drop the key this store used to persist - if (browser) localStorage.removeItem(`${STORAGE_APP_NAME}.pausedModelDownloads`); + if (browser) PreferencesService.removeItem(`${STORAGE_APP_NAME}.pausedModelDownloads`); } /** diff --git a/ui/apps/chat/src/lib/stores/permissions.svelte.ts b/ui/apps/chat/src/lib/stores/permissions.svelte.ts index c257b1ff4676..00759f2df49e 100644 --- a/ui/apps/chat/src/lib/stores/permissions.svelte.ts +++ b/ui/apps/chat/src/lib/stores/permissions.svelte.ts @@ -7,6 +7,7 @@ */ import { ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY } from '#lib/constants/index.ts'; +import { PreferencesService } from '#lib/services/preferences.service.ts'; import { browser } from '$app/env'; import { SvelteSet } from 'svelte/reactivity'; @@ -40,7 +41,7 @@ class PermissionsStore { if (!browser) return; try { - const stored = localStorage.getItem(ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY); + const stored = PreferencesService.getItem(ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY); if (stored) { for (const name of JSON.parse(stored) as string[]) { @@ -62,7 +63,10 @@ class PermissionsStore { private persist(): void { try { - localStorage.setItem(ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY, JSON.stringify([...this._tools])); + PreferencesService.setItem( + ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY, + JSON.stringify([...this._tools]) + ); } catch (err) { console.error( `Failed to persist to localStorage ("${ALWAYS_ALLOWED_TOOLS_LOCALSTORAGE_KEY}"):`, diff --git a/ui/apps/chat/src/lib/stores/servers.svelte.ts b/ui/apps/chat/src/lib/stores/servers.svelte.ts index 1edf55fc8e2e..7353f1f00524 100644 --- a/ui/apps/chat/src/lib/stores/servers.svelte.ts +++ b/ui/apps/chat/src/lib/stores/servers.svelte.ts @@ -14,6 +14,7 @@ import { LOCAL_BACKEND_ID, SETTINGS_KEYS } from '#lib/constants/index.ts'; +import { PreferencesService } from '#lib/services/preferences.service.ts'; import { serverStore } from '#lib/stores/llama-server.svelte.ts'; import { remoteAccessStore } from '#lib/stores/remote-access.svelte.ts'; import { settingsStore } from '#lib/stores/settings/index.svelte.ts'; @@ -32,7 +33,7 @@ function loadActiveServerId(): string { if (!browser) return LOCAL_BACKEND_ID; try { - return localStorage.getItem(ACTIVE_SERVER_LOCALSTORAGE_KEY) ?? LOCAL_BACKEND_ID; + return PreferencesService.getItem(ACTIVE_SERVER_LOCALSTORAGE_KEY) ?? LOCAL_BACKEND_ID; } catch { return LOCAL_BACKEND_ID; } @@ -42,7 +43,7 @@ function persistActiveServerId(serverId: string): void { if (!browser) return; try { - localStorage.setItem(ACTIVE_SERVER_LOCALSTORAGE_KEY, serverId); + PreferencesService.setItem(ACTIVE_SERVER_LOCALSTORAGE_KEY, serverId); } catch { /* ignore */ } diff --git a/ui/apps/chat/src/lib/stores/tabs.svelte.ts b/ui/apps/chat/src/lib/stores/tabs.svelte.ts index dabdc1cbf21f..91d3eaec1673 100644 --- a/ui/apps/chat/src/lib/stores/tabs.svelte.ts +++ b/ui/apps/chat/src/lib/stores/tabs.svelte.ts @@ -28,6 +28,7 @@ import { PROJECT_HOME_TAB_ID, ROUTES } from '#lib/constants/index.ts'; +import { PreferencesService } from '#lib/services/preferences.service.ts'; import { RouterService } from '#lib/services/router.service.ts'; import { projectOfNewChatTab, type TabView } from '#lib/utils/chat-tabs.ts'; import { browser } from '$app/env'; @@ -227,7 +228,7 @@ export class TabsStore { private load(): string[] { try { - const raw = localStorage.getItem(CONVERSATION_TABS_LOCALSTORAGE_KEY); + const raw = PreferencesService.getItem(CONVERSATION_TABS_LOCALSTORAGE_KEY); const parsed: unknown = raw ? JSON.parse(raw) : []; if (Array.isArray(parsed)) return parsed.filter((id) => typeof id === 'string'); @@ -250,7 +251,7 @@ export class TabsStore { // write would clobber them if (!browser || !this.initialized) return; - localStorage.setItem(CONVERSATION_TABS_LOCALSTORAGE_KEY, JSON.stringify(this.openTabs)); + PreferencesService.setItem(CONVERSATION_TABS_LOCALSTORAGE_KEY, JSON.stringify(this.openTabs)); } } diff --git a/ui/apps/chat/src/lib/stores/tools.svelte.ts b/ui/apps/chat/src/lib/stores/tools.svelte.ts index 3c0f6ebd065a..7e7154db5017 100644 --- a/ui/apps/chat/src/lib/stores/tools.svelte.ts +++ b/ui/apps/chat/src/lib/stores/tools.svelte.ts @@ -25,6 +25,7 @@ import { type ToolGroupIdentityProps, type ToolProvider } from '#lib/extensions/index.ts'; +import { PreferencesService } from '#lib/services/preferences.service.ts'; import { ToolsService } from '#lib/services/tools.service.ts'; import { serverStore } from '#lib/stores/llama-server.svelte.ts'; import { modelsStore } from '#lib/stores/models/index.svelte.ts'; @@ -434,7 +435,7 @@ class ToolsStore { if (!browser) return; try { - const stored = localStorage.getItem(DISABLED_TOOL_KEYS_LOCALSTORAGE_KEY); + const stored = PreferencesService.getItem(DISABLED_TOOL_KEYS_LOCALSTORAGE_KEY); if (stored) { const parsed = JSON.parse(stored); @@ -450,7 +451,7 @@ class ToolsStore { } try { - const stored = localStorage.getItem(DISABLED_TOOL_CATEGORIES_LOCALSTORAGE_KEY); + const stored = PreferencesService.getItem(DISABLED_TOOL_CATEGORIES_LOCALSTORAGE_KEY); if (stored) { const parsed = JSON.parse(stored); @@ -600,7 +601,7 @@ class ToolsStore { private persistDisabledToolCategories(): void { try { - localStorage.setItem( + PreferencesService.setItem( DISABLED_TOOL_CATEGORIES_LOCALSTORAGE_KEY, JSON.stringify([...this._disabledToolCategories]) ); @@ -611,7 +612,7 @@ class ToolsStore { private persistDisabledTools(): void { try { - localStorage.setItem( + PreferencesService.setItem( DISABLED_TOOL_KEYS_LOCALSTORAGE_KEY, JSON.stringify([...this._disabledTools]) ); diff --git a/ui/apps/chat/src/lib/stores/ui.svelte.ts b/ui/apps/chat/src/lib/stores/ui.svelte.ts index 435e703b9d1a..26f0d7924522 100644 --- a/ui/apps/chat/src/lib/stores/ui.svelte.ts +++ b/ui/apps/chat/src/lib/stores/ui.svelte.ts @@ -17,6 +17,7 @@ import { SIDEBAR_SECTION_ORDER_LOCALSTORAGE_KEY, SIDEBAR_WIDTH_LOCALSTORAGE_KEY } from '#lib/constants/index.ts'; +import { PreferencesService } from '#lib/services/preferences.service.ts'; import { deviceStore } from '#lib/stores/device.svelte.ts'; import { settingsStore } from '#lib/stores/settings/index.svelte.ts'; import type { Group } from '#lib/types/groups.js'; @@ -52,7 +53,7 @@ function loadCollapsedSidebarSections(): SvelteSet { if (!browser) return new SvelteSet(); try { - const raw = localStorage.getItem(SIDEBAR_COLLAPSED_SECTIONS_LOCALSTORAGE_KEY); + const raw = PreferencesService.getItem(SIDEBAR_COLLAPSED_SECTIONS_LOCALSTORAGE_KEY); const parsed = raw ? (JSON.parse(raw) as unknown) : null; return new SvelteSet( @@ -74,7 +75,9 @@ function loadSidebarSectionOrder(): string[] { if (!browser) return []; try { - const parsed = JSON.parse(localStorage.getItem(SIDEBAR_SECTION_ORDER_LOCALSTORAGE_KEY) ?? '[]'); + const parsed = JSON.parse( + PreferencesService.getItem(SIDEBAR_SECTION_ORDER_LOCALSTORAGE_KEY) ?? '[]' + ); return Array.isArray(parsed) ? parsed.filter((id): id is string => typeof id === 'string') : []; } catch { @@ -157,7 +160,7 @@ class UiStore { // a phone opens the sidebar as an overlay, which is no layout to restore if (!browser || deviceStore.isMobile) return; - localStorage.setItem(SIDEBAR_EXPANDED_LOCALSTORAGE_KEY, String(expanded)); + PreferencesService.setItem(SIDEBAR_EXPANDED_LOCALSTORAGE_KEY, String(expanded)); } /** Close the model details drawer. */ @@ -301,7 +304,7 @@ class UiStore { ]; try { - localStorage.setItem( + PreferencesService.setItem( SIDEBAR_SECTION_ORDER_LOCALSTORAGE_KEY, JSON.stringify(this.sidebarSectionOrder) ); @@ -317,9 +320,9 @@ class UiStore { if (!browser) return; if (width === null) { - localStorage.removeItem(SIDEBAR_WIDTH_LOCALSTORAGE_KEY); + PreferencesService.removeItem(SIDEBAR_WIDTH_LOCALSTORAGE_KEY); } else { - localStorage.setItem(SIDEBAR_WIDTH_LOCALSTORAGE_KEY, String(width)); + PreferencesService.setItem(SIDEBAR_WIDTH_LOCALSTORAGE_KEY, String(width)); } } @@ -332,7 +335,7 @@ class UiStore { } try { - localStorage.setItem( + PreferencesService.setItem( SIDEBAR_COLLAPSED_SECTIONS_LOCALSTORAGE_KEY, JSON.stringify([...this.collapsedSidebarSections]) ); @@ -353,13 +356,13 @@ class UiStore { private loadSidebarExpanded(): boolean { if (!browser || deviceStore.isMobile) return false; - return localStorage.getItem(SIDEBAR_EXPANDED_LOCALSTORAGE_KEY) === 'true'; + return PreferencesService.getItem(SIDEBAR_EXPANDED_LOCALSTORAGE_KEY) === 'true'; } private loadSidebarWidth(): number | null { if (!browser) return null; - const width = Number(localStorage.getItem(SIDEBAR_WIDTH_LOCALSTORAGE_KEY)); + const width = Number(PreferencesService.getItem(SIDEBAR_WIDTH_LOCALSTORAGE_KEY)); if (!Number.isFinite(width) || width <= 0) return null; diff --git a/ui/apps/chat/src/lib/types/api.d.ts b/ui/apps/chat/src/lib/types/api.d.ts index c47aed3bd79b..294a3415f425 100644 --- a/ui/apps/chat/src/lib/types/api.d.ts +++ b/ui/apps/chat/src/lib/types/api.d.ts @@ -312,6 +312,8 @@ export interface ApiLlamaCppServerProps { * pair with it. Absent on a server that does not run a host. */ connect_code?: string; + /** Server keeps UI conversations and settings on disk (--ui-storage) */ + ui_storage?: boolean; } export interface ApiChatCompletionRequest { diff --git a/ui/apps/chat/src/lib/types/database.d.ts b/ui/apps/chat/src/lib/types/database.d.ts index 8579f7b3129f..ea3a28f1706f 100644 --- a/ui/apps/chat/src/lib/types/database.d.ts +++ b/ui/apps/chat/src/lib/types/database.d.ts @@ -1,6 +1,7 @@ import { AttachmentType, ReasoningEffort, ToolSource } from '#lib/enums/index.ts'; import type { ChatMessageTimings, ChatMessageType, ChatRole } from '#lib/types/chat.js'; import type { Group } from '#lib/types/groups.js'; +import type { Project } from '#lib/types/projects.js'; /** * @deprecated Legacy per-conversation MCP server flags. MCP server enabled @@ -184,3 +185,75 @@ export type ExportedConversation = { }; export type ExportedConversations = ExportedConversation | ExportedConversation[]; + +/** Standalone records stored next to conversations, by table name. */ +export interface DatabaseRecords { + groups: Group; + projects: Project; + prompts: DatabasePrompt; +} + +export type DatabaseRecordKind = keyof DatabaseRecords; + +/** Message timestamps from `from` (inclusive) to `to` (exclusive), in ms; one end may be open. */ +export interface MessageTimeRange { + from?: number; + to?: number; +} + +/** + * Row-level storage for conversations and messages. Implementations only read + * and write rows; tree rules live in DatabaseService and utils/conversation-tree. + */ +export interface StorageBackend { + /** Runs `fn` atomically; backend calls made inside it join the same transaction. */ + transaction(fn: () => Promise): Promise; + + /** + * Calls `visit` with each message of `convIds` (every conversation when null), in one + * consistent read. With `range`, only conversations with a message timestamped in it + * are read. Lets search_chats drop attachment payloads as rows come in. + */ + scanMessages( + convIds: string[] | null, + visit: (message: DatabaseMessage) => void, + range?: MessageTimeRange + ): Promise; + + /** All conversations, most recently modified first. */ + listConversations(): Promise; + /** One entry per id, `undefined` where the conversation does not exist. */ + getConversations(ids: string[]): Promise<(DatabaseConversation | undefined)[]>; + /** Fails if a conversation with the same id exists. */ + addConversation(conv: DatabaseConversation): Promise; + putConversations(convs: DatabaseConversation[]): Promise; + /** Shallow-merges `updates` into the row; a no-op when it does not exist. */ + updateConversation(id: string, updates: Partial>): Promise; + /** Deletes the conversations together with all of their messages. */ + deleteConversations(ids: string[]): Promise; + + getMessage(id: string): Promise; + /** Messages of the given conversations, in no guaranteed order. */ + getMessages(convIds: string[]): Promise; + /** Fails if a message with the same id exists. */ + addMessages(messages: DatabaseMessage[]): Promise; + putMessages(messages: DatabaseMessage[]): Promise; + /** Shallow-merges `updates` into the row; a no-op when it does not exist. */ + updateMessage(id: string, updates: Partial>): Promise; + deleteMessages(ids: string[]): Promise; + + /** All records of a kind, in no guaranteed order. */ + listRecords(kind: K): Promise; + /** Fails if a record of that kind with the same id exists. */ + addRecord(kind: K, record: DatabaseRecords[K]): Promise; + /** + * Shallow-merges `updates` into the record, an `undefined` value removes the + * field; a no-op when it does not exist. + */ + updateRecord( + kind: K, + id: string, + updates: Partial> + ): Promise; + deleteRecord(kind: DatabaseRecordKind, id: string): Promise; +} diff --git a/ui/apps/chat/src/lib/types/index.ts b/ui/apps/chat/src/lib/types/index.ts index 7d8f4c64bab8..9619c4e21c0d 100644 --- a/ui/apps/chat/src/lib/types/index.ts +++ b/ui/apps/chat/src/lib/types/index.ts @@ -107,9 +107,16 @@ export type { DatabaseMessageExtra, DatabaseMessage, ExportedConversation, - ExportedConversations + ExportedConversations, + DatabaseRecordKind, + DatabaseRecords, + MessageTimeRange, + StorageBackend } from './database'; +// Storage adapter types +export type { StorageAdapter, StorageAdapterDefinition, StoredConversation } from './storage'; + // Model types export type { ModelCapabilities, diff --git a/ui/apps/chat/src/lib/types/storage.d.ts b/ui/apps/chat/src/lib/types/storage.d.ts new file mode 100644 index 000000000000..8cf780b0b5d5 --- /dev/null +++ b/ui/apps/chat/src/lib/types/storage.d.ts @@ -0,0 +1,69 @@ +import type { ApiLlamaCppServerProps } from './api'; +import type { + DatabaseConversation, + DatabaseMessage, + DatabaseRecordKind, + DatabaseRecords +} from './database'; + +/** One conversation as a storage adapter keeps it; `rev` 0 means it does not exist yet. */ +export interface StoredConversation { + rev: number; + conv: DatabaseConversation; + messages: DatabaseMessage[]; +} + +/** + * A remote document store for conversations and user settings. + * + * Each conversation is one opaque document with a revision number. The store + * does not need to understand the document, only to compare and bump `rev` on + * write, so it maps onto a key/value store, a JSON column in Postgres or a + * record in a NoSQL database. Settings and records (groups, projects, prompts) + * are small and merged per key or id, last write wins. Tree rules, caching, + * batching and conflict replay stay in the UI (DocumentStoreBackend). + * + * @see tools/ui/docs/storage-adapters.md - how to write and register one + */ +export interface StorageAdapter { + /** Metadata of every stored conversation, in any order. */ + listConversations(): Promise[]>; + /** A conversation document, or null when the store has none. */ + getConversation(id: string): Promise; + /** + * Write a conversation document if the store still holds revision `doc.rev` + * (0: it must not exist yet), atomically. + * @returns The new revision, greater than `doc.rev` + * @throws StorageConflictError when the store holds another revision + */ + putConversation(doc: StoredConversation): Promise; + /** Delete a conversation document, a no-op when it does not exist. */ + deleteConversation(id: string): Promise; + + /** All stored user settings. */ + getSettings(): Promise>; + /** Set and remove settings per key, leaving the other keys as they are. */ + updateSettings(set: Record, remove: string[]): Promise; + + /** Every stored record of a kind (groups, projects, prompts), in any order. */ + listRecords(kind: K): Promise; + /** Write `set` whole and delete `remove` by id, leaving the other records as they are. */ + updateRecords( + kind: K, + set: DatabaseRecords[K][], + remove: string[] + ): Promise; +} + +/** A storage adapter offered in Settings, see STORAGE_ADAPTERS in $lib/storage-adapters. */ +export interface StorageAdapterDefinition { + /** Stable id, kept in each browser that chose this adapter */ + id: string; + /** Short name shown in Settings, e.g. "Store on " */ + name: string; + /** What choosing it means for the user, shown in Settings */ + description: string; + /** Whether to offer it; `props` are those of the server serving this UI */ + isAvailable(props: ApiLlamaCppServerProps | null): boolean; + create(): StorageAdapter; +} diff --git a/ui/apps/chat/src/lib/utils/conversation-tree.ts b/ui/apps/chat/src/lib/utils/conversation-tree.ts new file mode 100644 index 000000000000..3d283cc28174 --- /dev/null +++ b/ui/apps/chat/src/lib/utils/conversation-tree.ts @@ -0,0 +1,174 @@ +/** + * Pure operations over the conversation fork tree and the message tree rows. + * + * Conversations form a fork tree through `forkedFromConversationId`; messages + * form a tree through `parent` / `children`. These helpers compute the rows to + * write, so any storage backend can persist them without re-implementing the + * tree rules. + */ + +import { filterByLeafNodeId } from './branching'; +import { uuid } from './uuid'; +import type { McpServerOverride } from '#lib/types/database.js'; + +/** + * Collects the ids of every fork descending from `rootId`, at any depth. + * `rootId` itself is not included. + */ +export function collectForkDescendantIds( + conversations: readonly DatabaseConversation[], + rootId: string +): string[] { + const ids: string[] = []; + const queue = [rootId]; + + while (queue.length > 0) { + const parentId = queue.pop()!; + + for (const conv of conversations) { + if (conv.forkedFromConversationId !== parentId) continue; + + ids.push(conv.id); + queue.push(conv.id); + } + } + + return ids; +} + +/** + * Computes the rows that reparent the direct forks of `parentId` to its + * nearest ancestor not listed in `excludeIds` (or promote them to top-level). + * Forks whose own id is in `excludeIds` are left out, since they are deleted + * in the same batch. + */ +export function reparentDirectForks( + conversations: readonly DatabaseConversation[], + parentId: string, + excludeIds: ReadonlySet = new Set() +): DatabaseConversation[] { + const byId = new Map(conversations.map((conv) => [conv.id, conv])); + const conv = byId.get(parentId); + + if (!conv) return []; + + let newParent = conv.forkedFromConversationId; + + const visited = new Set([parentId]); + + while (newParent && excludeIds.has(newParent)) { + if (visited.has(newParent)) { + newParent = undefined; + + break; + } + + visited.add(newParent); + const next = byId.get(newParent); + + if (!next) { + newParent = undefined; + + break; + } + + newParent = next.forkedFromConversationId; + } + + const updates: DatabaseConversation[] = []; + + for (const child of conversations) { + if (child.forkedFromConversationId !== parentId || excludeIds.has(child.id)) continue; + + updates.push({ ...child, forkedFromConversationId: newParent }); + } + + return updates; +} + +/** + * Builds a fork of `sourceConv` holding the path from the root to + * `atMessageId`, with fresh ids for the conversation and every message. + * + * @throws Error if no path to `atMessageId` can be resolved + */ +export function buildForkedConversation( + sourceConv: DatabaseConversation, + messages: readonly DatabaseMessage[], + atMessageId: string, + options: { name: string; includeAttachments: boolean } +): { conv: DatabaseConversation; messages: DatabaseMessage[] } { + const pathMessages = filterByLeafNodeId(messages, atMessageId, true) as DatabaseMessage[]; + + if (pathMessages.length === 0) { + throw new Error(`Could not resolve message path to ${atMessageId}`); + } + + const idMap = new Map(); + + for (const msg of pathMessages) { + idMap.set(msg.id, uuid()); + } + + const newConvId = uuid(); + const clonedMessages: DatabaseMessage[] = pathMessages.map((msg) => { + const newId = idMap.get(msg.id)!; + const newParent = msg.parent ? (idMap.get(msg.parent) ?? null) : null; + const newChildren = msg.children + .filter((childId: string) => idMap.has(childId)) + .map((childId: string) => idMap.get(childId)!); + + return { + ...msg, + children: newChildren, + convId: newConvId, + extra: options.includeAttachments ? msg.extra : undefined, + id: newId, + parent: newParent + }; + }); + const lastClonedMessage = clonedMessages[clonedMessages.length - 1]; + const conv: DatabaseConversation = { + currNode: lastClonedMessage.id, + cwd: sourceConv.cwd, + disabledToolCategories: sourceConv.disabledToolCategories + ? [...sourceConv.disabledToolCategories] + : undefined, + disabledTools: sourceConv.disabledTools ? [...sourceConv.disabledTools] : undefined, + forkedFromConversationId: sourceConv.id, + groupId: sourceConv.groupId, + id: newConvId, + lastModified: Date.now(), + mcpServerOverrides: sourceConv.mcpServerOverrides + ? sourceConv.mcpServerOverrides.map((o: McpServerOverride) => ({ + enabled: o.enabled, + serverId: o.serverId + })) + : undefined, + name: options.name + }; + + return { conv, messages: clonedMessages }; +} + +/** + * Groups messages by conversation, each group sorted by timestamp (oldest first). + */ +export function groupMessagesByConversation( + messages: readonly DatabaseMessage[] +): Map { + const groups = new Map(); + + for (const msg of messages) { + const bucket = groups.get(msg.convId); + + if (bucket) bucket.push(msg); + else groups.set(msg.convId, [msg]); + } + + for (const bucket of groups.values()) { + bucket.sort((a, b) => a.timestamp - b.timestamp); + } + + return groups; +} diff --git a/ui/apps/chat/src/lib/utils/index.ts b/ui/apps/chat/src/lib/utils/index.ts index 307337923c73..0e62623942ec 100644 --- a/ui/apps/chat/src/lib/utils/index.ts +++ b/ui/apps/chat/src/lib/utils/index.ts @@ -74,6 +74,14 @@ export { buildSiblingInfoMap } from './branching'; +// Conversation fork tree +export { + buildForkedConversation, + collectForkDescendantIds, + groupMessagesByConversation, + reparentDirectForks +} from './conversation-tree'; + // Code export { highlightCode, diff --git a/ui/apps/chat/tests/client/database-tree-db.svelte.test.ts b/ui/apps/chat/tests/client/database-tree-db.svelte.test.ts new file mode 100644 index 000000000000..1c8b4b418fa5 --- /dev/null +++ b/ui/apps/chat/tests/client/database-tree-db.svelte.test.ts @@ -0,0 +1,113 @@ +import { MessageRole, MessageType } from '#lib/enums/index.ts'; +import { DatabaseService } from '#lib/services/database.service.ts'; +import { afterEach, describe, expect, it } from 'vitest'; + +afterEach(async () => { + const conversations = await DatabaseService.getAllConversations(); + + await DatabaseService.bulkDeleteConversations(conversations.map((conv) => conv.id)); +}); + +async function createChat(name: string, forkedFromConversationId?: string) { + const conv = await DatabaseService.createConversation(name, { forkedFromConversationId }); + const rootId = await DatabaseService.createRootMessage(conv.id); + + return { conv, rootId }; +} + +function userMessage(convId: string, content: string, timestamp: number) { + return { + children: [], + content, + convId, + parent: null, + role: MessageRole.USER, + timestamp: Date.now() + timestamp, + type: MessageType.TEXT + }; +} + +/** + * Tree rules applied by DatabaseService on top of the IndexedDB backend. + */ +describe('DatabaseService tree operations', () => { + it('links a new branch to its parent and moves the current node', async () => { + const { conv, rootId } = await createChat('a'); + const first = await DatabaseService.createMessageBranch(userMessage(conv.id, 'one', 1), rootId); + const second = await DatabaseService.createMessageBranch( + userMessage(conv.id, 'two', 2), + rootId + ); + const messages = await DatabaseService.getConversationMessages(conv.id); + const root = messages.find((m) => m.id === rootId); + + expect(root?.children).toEqual([first.id, second.id]); + expect((await DatabaseService.getConversation(conv.id))?.currNode).toBe(second.id); + }); + + it('cascades a message delete and unlinks it from its parent', async () => { + const { conv, rootId } = await createChat('a'); + const user = await DatabaseService.createMessageBranch(userMessage(conv.id, 'u', 1), rootId); + const reply = await DatabaseService.createMessageBranch(userMessage(conv.id, 'r', 2), user.id); + const deleted = await DatabaseService.deleteMessageCascading(conv.id, user.id); + const messages = await DatabaseService.getConversationMessages(conv.id); + + expect(deleted.sort()).toEqual([user.id, reply.id].sort()); + expect(messages.map((m) => m.id)).toEqual([rootId]); + expect(messages[0].children).toEqual([]); + }); + + it('forks the path to a message into a new conversation', async () => { + const { conv, rootId } = await createChat('a'); + const user = await DatabaseService.createMessageBranch(userMessage(conv.id, 'u', 1), rootId); + + await DatabaseService.createMessageBranch(userMessage(conv.id, 'later', 2), user.id); + + const fork = await DatabaseService.forkConversation(conv.id, user.id, { + includeAttachments: true, + name: 'fork' + }); + const forkMessages = await DatabaseService.getConversationMessages(fork.id); + + expect(fork.forkedFromConversationId).toBe(conv.id); + expect(forkMessages.map((m) => m.content)).toEqual(['', 'u']); + expect(fork.currNode).toBe(forkMessages[1].id); + }); + + it('reparents forks of a deleted conversation to its parent', async () => { + const { conv: a } = await createChat('a'); + const { conv: b } = await createChat('b', a.id); + const { conv: c } = await createChat('c', b.id); + + await DatabaseService.deleteConversation(b.id); + + expect(await DatabaseService.getConversation(b.id)).toBeUndefined(); + expect(await DatabaseService.getConversationMessages(b.id)).toEqual([]); + expect((await DatabaseService.getConversation(c.id))?.forkedFromConversationId).toBe(a.id); + }); + + it('deletes forks along with the conversation when asked to', async () => { + const { conv: a } = await createChat('a'); + const { conv: b } = await createChat('b', a.id); + const { conv: c } = await createChat('c', b.id); + + await DatabaseService.deleteConversation(a.id, { deleteWithForks: true }); + + expect(await DatabaseService.getAllConversations()).toEqual([]); + expect(await DatabaseService.getConversationMessages(c.id)).toEqual([]); + }); + + it('reparents past every conversation deleted in the same batch', async () => { + const { conv: a } = await createChat('a'); + const { conv: b } = await createChat('b', a.id); + const { conv: c } = await createChat('c', b.id); + const { conv: d } = await createChat('d', c.id); + + await DatabaseService.bulkDeleteConversations([b.id, c.id]); + + const remaining = await DatabaseService.getAllConversations(); + + expect(remaining.map((conv) => conv.id).sort()).toEqual([a.id, d.id].sort()); + expect((await DatabaseService.getConversation(d.id))?.forkedFromConversationId).toBe(a.id); + }); +}); diff --git a/ui/apps/chat/tests/client/search-chats.service.svelte.test.ts b/ui/apps/chat/tests/client/search-chats.service.svelte.test.ts index 87eca5dbfe6e..f7a47c8821b0 100644 --- a/ui/apps/chat/tests/client/search-chats.service.svelte.test.ts +++ b/ui/apps/chat/tests/client/search-chats.service.svelte.test.ts @@ -104,10 +104,16 @@ describe('search_chats service', () => { expect(reply.content).toBe('Error: order_timestamp must be one of: asc, desc'); }); - it('reads through a transaction that refuses writes', async () => { - await expect( - DatabaseService.readOnly(({ messages }) => messages.delete('paris-m0')) - ).rejects.toThrow(); + it('scans the messages of the chats with one in a time range, leaving the store as it was', async () => { + const seen: string[] = []; + + await DatabaseService.scanMessages(['paris'], (m) => seen.push(m.id)); + expect(seen.sort()).toEqual(['paris-m0', 'paris-m1', 'paris-m2', 'paris-m3']); + + seen.length = 0; + // paris's messages are timestamped 1000 to 1003, so a window after them leaves it out + await DatabaseService.scanMessages(['paris'], (m) => seen.push(m.id), { from: 1004 }); + expect(seen).toEqual([]); expect(await DatabaseService.getConversationMessages('paris')).toHaveLength(4); }); }); diff --git a/ui/apps/chat/tests/unit/conversation-tree.test.ts b/ui/apps/chat/tests/unit/conversation-tree.test.ts new file mode 100644 index 000000000000..b5cd901a2407 --- /dev/null +++ b/ui/apps/chat/tests/unit/conversation-tree.test.ts @@ -0,0 +1,143 @@ +// Pure fork-tree and message-tree rules that every storage backend relies on. + +import { + CONFIG_LOCALSTORAGE_KEY, + SIDEBAR_WIDTH_LOCALSTORAGE_KEY, + STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX +} from '#lib/constants/index.ts'; +import { MessageRole, MessageType, PreferenceScope } from '#lib/enums/index.ts'; +import { PreferencesService } from '#lib/services/preferences.service.ts'; +import type { DatabaseConversation, DatabaseMessage } from '#lib/types/database.js'; +import { + buildForkedConversation, + collectForkDescendantIds, + groupMessagesByConversation, + reparentDirectForks +} from '#lib/utils/conversation-tree.ts'; +import { describe, expect, it } from 'vitest'; + +function conv(id: string, forkedFrom?: string): DatabaseConversation { + return { currNode: '', forkedFromConversationId: forkedFrom, id, lastModified: 0, name: id }; +} + +function msg( + id: string, + parent: string | null, + children: string[] = [], + timestamp = 0 +): DatabaseMessage { + return { + children, + content: id, + convId: 'c1', + extra: [], + id, + parent, + role: MessageRole.USER, + timestamp, + type: MessageType.TEXT + } as DatabaseMessage; +} + +describe('collectForkDescendantIds', () => { + it('collects forks at every depth, not the root itself', () => { + const convs = [conv('a'), conv('b', 'a'), conv('c', 'b'), conv('d', 'a'), conv('x')]; + + expect(collectForkDescendantIds(convs, 'a').sort()).toEqual(['b', 'c', 'd']); + }); +}); + +describe('reparentDirectForks', () => { + it('moves direct forks to the parent of the deleted conversation', () => { + const convs = [conv('a'), conv('b', 'a'), conv('c', 'b')]; + + expect(reparentDirectForks(convs, 'b')).toEqual([conv('c', 'a')]); + }); + + it('skips ancestors deleted in the same batch and drops forks being deleted', () => { + const convs = [conv('a'), conv('b', 'a'), conv('c', 'b'), conv('d', 'c'), conv('e', 'c')]; + + expect(reparentDirectForks(convs, 'c', new Set(['b', 'c', 'e']))).toEqual([conv('d', 'a')]); + }); + + it('promotes forks to top-level when the ancestry loops', () => { + const convs = [conv('a', 'b'), conv('b', 'a'), conv('c', 'a')]; + + expect(reparentDirectForks(convs, 'a', new Set(['a', 'b']))).toEqual([conv('c', undefined)]); + }); + + it('returns nothing for an unknown conversation', () => { + expect(reparentDirectForks([conv('a')], 'missing')).toEqual([]); + }); +}); + +describe('buildForkedConversation', () => { + // root -> u1 -> a1, with a sibling branch u1 -> a2 + const messages = [ + msg('root', null, ['u1'], 0), + msg('u1', 'root', ['a1', 'a2'], 1), + msg('a1', 'u1', [], 2), + msg('a2', 'u1', [], 3) + ]; + + it('clones the path to the target with fresh, consistently remapped ids', () => { + const { conv: fork, messages: cloned } = buildForkedConversation(conv('src'), messages, 'a1', { + includeAttachments: true, + name: 'Fork' + }); + + expect(fork.forkedFromConversationId).toBe('src'); + expect(fork.name).toBe('Fork'); + expect(cloned.map((m) => m.content)).toEqual(['root', 'u1', 'a1']); + expect(cloned.every((m) => m.convId === fork.id)).toBe(true); + expect(fork.currNode).toBe(cloned[2].id); + + const [root, u1, a1] = cloned; + + expect(root.parent).toBeNull(); + expect(root.children).toEqual([u1.id]); + expect(u1.parent).toBe(root.id); + expect(u1.children).toEqual([a1.id]); + expect(new Set(cloned.map((m) => m.id)).size).toBe(3); + expect(cloned.some((m) => messages.some((orig) => orig.id === m.id))).toBe(false); + expect(a1.extra).toEqual([]); + }); + + it('drops attachments when asked to', () => { + const { messages: cloned } = buildForkedConversation(conv('src'), messages, 'a1', { + includeAttachments: false, + name: 'Fork' + }); + + expect(cloned.every((m) => m.extra === undefined)).toBe(true); + }); + + it('throws when there are no messages to fork', () => { + expect(() => + buildForkedConversation(conv('src'), [], 'a1', { includeAttachments: true, name: 'Fork' }) + ).toThrow(); + }); +}); + +describe('groupMessagesByConversation', () => { + it('groups by conversation, oldest first', () => { + const groups = groupMessagesByConversation([ + { ...msg('b2', null, [], 2), convId: 'b' }, + { ...msg('a2', null, [], 2), convId: 'a' }, + { ...msg('b1', null, [], 1), convId: 'b' } + ]); + + expect(groups.get('a')?.map((m) => m.id)).toEqual(['a2']); + expect(groups.get('b')?.map((m) => m.id)).toEqual(['b1', 'b2']); + }); +}); + +describe('PreferencesService.scopeOf', () => { + it('classifies user data and device-local keys', () => { + expect(PreferencesService.scopeOf(CONFIG_LOCALSTORAGE_KEY)).toBe(PreferenceScope.USER); + expect(PreferencesService.scopeOf(SIDEBAR_WIDTH_LOCALSTORAGE_KEY)).toBe(PreferenceScope.DEVICE); + expect(PreferencesService.scopeOf(`${STREAM_RESUME_LOCALSTORAGE_KEY_PREFIX}conv-a`)).toBe( + PreferenceScope.DEVICE + ); + }); +}); diff --git a/ui/apps/chat/tests/unit/document-store-backend.test.ts b/ui/apps/chat/tests/unit/document-store-backend.test.ts new file mode 100644 index 000000000000..90cb075552ae --- /dev/null +++ b/ui/apps/chat/tests/unit/document-store-backend.test.ts @@ -0,0 +1,291 @@ +// DocumentStoreBackend against an in-memory adapter with the same revision +// rules as llama-server's store (tools/server/server-storage.cpp). + +import { MessageRole, MessageType } from '#lib/enums/index.ts'; +import { DatabaseService } from '#lib/services/database.service.ts'; +import { DocumentStoreBackend } from '#lib/services/document-store-backend.ts'; +import { PreferencesService } from '#lib/services/preferences.service.ts'; +import { LlamaServerStorageAdapter, StorageConflictError } from '#lib/storage-adapters/index.ts'; +import type { DatabaseMessage } from '#lib/types/database.js'; +import type { StorageAdapter, StoredConversation } from '#lib/types/index.ts'; +import { ApiError } from '#lib/utils/api-fetch.ts'; +import { beforeEach, describe, expect, it, vi } from 'vitest'; + +const apiFetch = vi.hoisted(() => vi.fn()); + +vi.mock('#lib/utils/api-fetch.ts', async (importOriginal) => ({ + ...(await importOriginal()), + apiFetch +})); + +const docs = new Map(); +const records = new Map>(); +const writes: string[] = []; +const memoryAdapter: StorageAdapter = { + async deleteConversation(id) { + writes.push(`delete ${id}`); + docs.delete(id); + }, + async getConversation(id) { + const doc = docs.get(id); + + return doc ? structuredClone(doc) : null; + }, + async getSettings() { + return {}; + }, + async listConversations() { + return [...docs.values()].map(({ conv, rev }) => structuredClone({ conv, rev })); + }, + async listRecords(kind) { + return structuredClone([...(records.get(kind)?.values() ?? [])]) as never; + }, + async putConversation(doc) { + const current = docs.get(doc.conv.id)?.rev ?? 0; + + if (doc.rev !== current) throw new StorageConflictError(doc.conv.id); + + writes.push(`put ${doc.conv.id}`); + docs.set(doc.conv.id, structuredClone({ ...doc, rev: current + 1 })); + + return current + 1; + }, + async updateRecords(kind, set, remove) { + const stored = records.get(kind) ?? new Map(); + + writes.push(`records ${kind}`); + for (const record of set) stored.set(record.id, structuredClone(record)); + for (const id of remove) stored.delete(id); + records.set(kind, stored); + }, + async updateSettings() {} +}; +const newBackend = () => new DocumentStoreBackend(memoryAdapter); + +function userMessage(convId: string, content: string): Omit { + return { + children: [], + content, + convId, + parent: null, + role: MessageRole.USER, + timestamp: Date.now(), + type: MessageType.TEXT + }; +} + +beforeEach(() => { + docs.clear(); + records.clear(); + writes.length = 0; +}); + +describe('DocumentStoreBackend', () => { + it('writes one document per conversation with the tree DatabaseService builds', async () => { + DatabaseService.useBackend(newBackend()); + + const conv = await DatabaseService.createConversation('a'); + const rootId = await DatabaseService.createRootMessage(conv.id); + const user = await DatabaseService.createMessageBranch(userMessage(conv.id, 'hi'), rootId); + const stored = docs.get(conv.id)!; + const messages = stored.messages as DatabaseMessage[]; + + expect(stored.rev).toBe(3); + expect(messages.find((m) => m.id === rootId)?.children).toEqual([user.id]); + expect((stored.conv as { currNode: string }).currNode).toBe(user.id); + }); + + it('replays its edits on a document another client changed meanwhile', async () => { + DatabaseService.useBackend(newBackend()); + + const conv = await DatabaseService.createConversation('a'); + const rootId = await DatabaseService.createRootMessage(conv.id); + const first = await DatabaseService.createMessageBranch(userMessage(conv.id, 'one'), rootId); + const second = await DatabaseService.createMessageBranch(userMessage(conv.id, 'two'), rootId); + // two tabs with the conversation open + const tabA = newBackend(); + const tabB = newBackend(); + + await tabA.getMessages([conv.id]); + await tabB.getMessages([conv.id]); + await tabA.updateMessage(first.id, { content: 'one, edited in A' }); + await tabB.updateMessage(second.id, { content: 'two, edited in B' }); + + const messages = docs.get(conv.id)!.messages as DatabaseMessage[]; + + expect(messages.find((m) => m.id === first.id)?.content).toBe('one, edited in A'); + expect(messages.find((m) => m.id === second.id)?.content).toBe('two, edited in B'); + }); + + it('restores the cached document when a transaction fails', async () => { + const backend = newBackend(); + + DatabaseService.useBackend(backend); + + const conv = await DatabaseService.createConversation('a'); + const rootId = await DatabaseService.createRootMessage(conv.id); + + writes.length = 0; + + await expect( + backend.transaction(async () => { + await backend.updateMessage(rootId, { content: 'changed' }); + + throw new Error('boom'); + }) + ).rejects.toThrow('boom'); + + expect((await backend.getMessage(rootId))?.content).toBe(''); + expect(writes).toEqual([]); + }); + + it('saves a write that joined a transaction which closed while it loaded', async () => { + DatabaseService.useBackend(newBackend()); + + const a = await DatabaseService.createConversation('a'); + const b = await DatabaseService.createConversation('b'); + const backend = newBackend(); + + let joined: Promise = Promise.resolve(); + + await backend.transaction(async () => { + await backend.updateConversation(a.id, { name: 'a2' }); + // not awaited: still loading b when the transaction ends + joined = backend.updateConversation(b.id, { name: 'b2' }); + }); + await joined; + + expect((docs.get(a.id)!.conv as { name: string }).name).toBe('a2'); + expect((docs.get(b.id)!.conv as { name: string }).name).toBe('b2'); + }); + + it('deletes a conversation document and drops it from the list', async () => { + DatabaseService.useBackend(newBackend()); + + const keep = await DatabaseService.createConversation('keep'); + const drop = await DatabaseService.createConversation('drop'); + + await DatabaseService.deleteConversation(drop.id); + + expect(docs.has(drop.id)).toBe(false); + expect((await DatabaseService.getAllConversations()).map((c) => c.id)).toEqual([keep.id]); + }); +}); + +describe('DocumentStoreBackend records', () => { + // groups list newest first by creation + const group = (id: string, createdAt: number) => ({ + createdAt, + id, + name: id, + updatedAt: createdAt + }); + + it('keeps groups per id, so two clients editing different groups both keep theirs', async () => { + DatabaseService.useBackend(newBackend()); + + await DatabaseService.createGroup(group('a', 1)); + await DatabaseService.createGroup(group('b', 2)); + + const tabA = newBackend(); + const tabB = newBackend(); + + await tabA.listRecords('groups'); + await tabB.listRecords('groups'); + await tabA.updateRecord('groups', 'a', { name: 'renamed in A' }); + await tabB.updateRecord('groups', 'b', { name: 'renamed in B' }); + + expect((await DatabaseService.getAllGroups()).map((g) => g.name)).toEqual([ + 'renamed in B', + 'renamed in A' + ]); + }); + + it('takes a deleted group off its conversations in one transaction', async () => { + DatabaseService.useBackend(newBackend()); + + await DatabaseService.createGroup(group('g', 1)); + + const conv = await DatabaseService.createConversation('a'); + + await DatabaseService.updateConversation(conv.id, { groupId: 'g' }); + writes.length = 0; + await DatabaseService.deleteGroup('g'); + + expect(records.get('groups')?.has('g')).toBe(false); + expect((docs.get(conv.id)!.conv as { groupId?: string }).groupId).toBeUndefined(); + expect(writes).toEqual(['records groups', `put ${conv.id}`]); + }); + + it('restores cached records when a transaction fails', async () => { + const backend = newBackend(); + + DatabaseService.useBackend(backend); + await DatabaseService.createGroup(group('g', 1)); + writes.length = 0; + + await expect( + backend.transaction(async () => { + await backend.updateRecord('groups', 'g', { name: 'changed' }); + + throw new Error('boom'); + }) + ).rejects.toThrow('boom'); + + await expect(backend.addRecord('groups', group('g', 2))).rejects.toThrow('already exists'); + expect(writes).toEqual([]); + expect(records.get('groups')?.get('g')).toMatchObject({ name: 'g' }); + }); +}); + +describe('PreferencesService remote mode', () => { + it('keeps user data on the remote and device state local, batching writes', async () => { + const store = new Map(); + const push = vi.fn(async () => {}); + + (globalThis as unknown as { localStorage: Partial }).localStorage = { + getItem: (key) => store.get(key) ?? null, + removeItem: (key) => void store.delete(key), + setItem: (key, value) => void store.set(key, value) + }; + + PreferencesService.useRemote({ 'LlamaUi.favoriteModels': '["a"]' }, push); + + expect(PreferencesService.getItem('LlamaUi.favoriteModels')).toBe('["a"]'); + + PreferencesService.setItem('LlamaUi.favoriteModels', '["b"]'); + PreferencesService.setItem('LlamaUi.hiddenModels', '["c"]'); + PreferencesService.removeItem('LlamaUi.recentModels'); + PreferencesService.setItem('LlamaUi.sidebarWidth', '300'); + + await Promise.resolve(); + await Promise.resolve(); + + expect(push).toHaveBeenCalledTimes(1); + expect(push).toHaveBeenCalledWith( + { 'LlamaUi.favoriteModels': '["b"]', 'LlamaUi.hiddenModels': '["c"]' }, + ['LlamaUi.recentModels'] + ); + expect(store.get('LlamaUi.sidebarWidth')).toBe('300'); + expect(store.has('LlamaUi.favoriteModels')).toBe(false); + }); +}); + +describe('LlamaServerStorageAdapter', () => { + it('maps the server answers onto the adapter contract', async () => { + const adapter = new LlamaServerStorageAdapter(); + const conv = { currNode: null, id: 'a', lastModified: 0, name: 'a' }; + + apiFetch.mockRejectedValueOnce(new ApiError('not found', 404)); + expect(await adapter.getConversation('a')).toBeNull(); + + apiFetch.mockRejectedValueOnce(new ApiError('conflict', 409)); + await expect(adapter.putConversation({ conv, messages: [], rev: 0 })).rejects.toBeInstanceOf( + StorageConflictError + ); + + apiFetch.mockResolvedValueOnce({ rev: 1 }); + expect(await adapter.putConversation({ conv, messages: [], rev: 0 })).toBe(1); + expect(apiFetch).toHaveBeenLastCalledWith('./storage/conversations/a', expect.anything()); + }); +}); diff --git a/ui/apps/chat/vite.config.ts b/ui/apps/chat/vite.config.ts index c393f7661e90..89fa4b47b258 100644 --- a/ui/apps/chat/vite.config.ts +++ b/ui/apps/chat/vite.config.ts @@ -113,6 +113,7 @@ export default defineConfig(({ mode }) => { '/models': toServer(), '/props': toServer(), '/slots': toServer(), + '/storage': toServer(), '/tools': toServer(), // ws: realtime transcription (/v1/realtime) is a WebSocket '/v1': toServer(true)