#pragma once #include "apikey.hpp" #include #include #include namespace svapi { struct Settings; struct CollectionMeta; /// Per-project settings: embedding overrides, and the policy a newly /// registered function inherits. An empty field means "inherit the global /// value" - there is no separate presence flag, because an empty base URL, API /// key or model is meaningless on its own. /// /// Stored as one document per project in the (encrypted) global /// `vectorapi_settings` collection under projectSettingsDocId(project). The DB /// client strips a top-level `id` from stored bodies, so the project name also /// travels in the body as `project` (same approach as KeyStore's `key_id`). struct ProjectSettings { std::string project; std::string openaiApiBase; std::string openaiApiKey; std::string defaultEmbeddingModel; /// The policy a function gets on FIRST registration in this project. /// Unset means fall back to builtinDefaultFunctionPolicy(), which grants /// nothing. Only ever consulted for a first registration - an existing /// function keeps the policy an admin gave it, so changing this never /// retroactively widens or narrows anything already deployed. std::optional defaultFunctionPolicy; /// True when at least one EMBEDDING field overrides the global value. bool hasEmbeddingOverrides() const { return !openaiApiBase.empty() || !openaiApiKey.empty() || !defaultEmbeddingModel.empty(); } /// True when the project has anything stored at all. The settings document /// is deleted when this goes false, so it has to account for every field: /// keying the delete on the embedding fields alone would silently drop a /// function policy the moment someone cleared an embedding override. bool hasAnyOverrides() const { return hasEmbeddingOverrides() || defaultFunctionPolicy.has_value(); } /// A project base URL must carry its own API key, otherwise the global /// credential would be sent to a foreign endpoint. A project key without a /// base URL is fine (own key, default endpoint). bool baseHasItsOwnKey() const { return openaiApiBase.empty() || !openaiApiKey.empty(); } static ProjectSettings fromJson(const nlohmann::json& j); nlohmann::json toJson() const; }; /// Document id holding `project`'s overrides. Prefixed so it can never collide /// with the global settings document ("current"). std::string projectSettingsDocId(const std::string& project); /// The endpoint actually used for one embedding call. struct ResolvedEmbedding { std::string apiBase; std::string apiKey; std::string model; }; /// Resolve the effective embedding endpoint for a collection. /// apiBase / apiKey : project override, else global. /// model : collection pin, else project override, else global default. /// `proj` may be null when the project has no overrides stored. ResolvedEmbedding resolveEmbedding(const Settings& global, const ProjectSettings* proj, const CollectionMeta& coll); /// The policy a first-time function registration in this project inherits: /// the project's configured default, else the built-in floor. `proj` may be /// null when the project has nothing stored. KeyScope defaultFunctionPolicyFor(const ProjectSettings* proj); } // namespace svapi