project_settings.hpp 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #pragma once
  2. #include "apikey.hpp"
  3. #include <nlohmann/json.hpp>
  4. #include <optional>
  5. #include <string>
  6. namespace svapi {
  7. struct Settings;
  8. struct CollectionMeta;
  9. /// Per-project settings: embedding overrides, and the policy a newly
  10. /// registered function inherits. An empty field means "inherit the global
  11. /// value" - there is no separate presence flag, because an empty base URL, API
  12. /// key or model is meaningless on its own.
  13. ///
  14. /// Stored as one document per project in the (encrypted) global
  15. /// `vectorapi_settings` collection under projectSettingsDocId(project). The DB
  16. /// client strips a top-level `id` from stored bodies, so the project name also
  17. /// travels in the body as `project` (same approach as KeyStore's `key_id`).
  18. struct ProjectSettings {
  19. std::string project;
  20. std::string openaiApiBase;
  21. std::string openaiApiKey;
  22. std::string defaultEmbeddingModel;
  23. /// The policy a function gets on FIRST registration in this project.
  24. /// Unset means fall back to builtinDefaultFunctionPolicy(), which grants
  25. /// nothing. Only ever consulted for a first registration - an existing
  26. /// function keeps the policy an admin gave it, so changing this never
  27. /// retroactively widens or narrows anything already deployed.
  28. std::optional<KeyScope> defaultFunctionPolicy;
  29. /// True when at least one EMBEDDING field overrides the global value.
  30. bool hasEmbeddingOverrides() const {
  31. return !openaiApiBase.empty() || !openaiApiKey.empty() || !defaultEmbeddingModel.empty();
  32. }
  33. /// True when the project has anything stored at all. The settings document
  34. /// is deleted when this goes false, so it has to account for every field:
  35. /// keying the delete on the embedding fields alone would silently drop a
  36. /// function policy the moment someone cleared an embedding override.
  37. bool hasAnyOverrides() const {
  38. return hasEmbeddingOverrides() || defaultFunctionPolicy.has_value();
  39. }
  40. /// A project base URL must carry its own API key, otherwise the global
  41. /// credential would be sent to a foreign endpoint. A project key without a
  42. /// base URL is fine (own key, default endpoint).
  43. bool baseHasItsOwnKey() const { return openaiApiBase.empty() || !openaiApiKey.empty(); }
  44. static ProjectSettings fromJson(const nlohmann::json& j);
  45. nlohmann::json toJson() const;
  46. };
  47. /// Document id holding `project`'s overrides. Prefixed so it can never collide
  48. /// with the global settings document ("current").
  49. std::string projectSettingsDocId(const std::string& project);
  50. /// The endpoint actually used for one embedding call.
  51. struct ResolvedEmbedding {
  52. std::string apiBase;
  53. std::string apiKey;
  54. std::string model;
  55. };
  56. /// Resolve the effective embedding endpoint for a collection.
  57. /// apiBase / apiKey : project override, else global.
  58. /// model : collection pin, else project override, else global default.
  59. /// `proj` may be null when the project has no overrides stored.
  60. ResolvedEmbedding resolveEmbedding(const Settings& global, const ProjectSettings* proj,
  61. const CollectionMeta& coll);
  62. /// The policy a first-time function registration in this project inherits:
  63. /// the project's configured default, else the built-in floor. `proj` may be
  64. /// null when the project has nothing stored.
  65. KeyScope defaultFunctionPolicyFor(const ProjectSettings* proj);
  66. } // namespace svapi