mirror of
https://github.com/aaronpo97/the-biergarten-app.git
synced 2026-10-15 10:04:44 +00:00
Formatted src/services
This commit is contained in:
@@ -23,80 +23,80 @@ PromptDirectory::PromptDirectory(const std::filesystem::path& prompt_dir)
|
|||||||
PromptDirectory::PromptDirectory(const std::filesystem::path& prompt_dir,
|
PromptDirectory::PromptDirectory(const std::filesystem::path& prompt_dir,
|
||||||
std::shared_ptr<ILogger> logger)
|
std::shared_ptr<ILogger> logger)
|
||||||
: prompt_dir_(prompt_dir), logger_(std::move(logger)) {
|
: prompt_dir_(prompt_dir), logger_(std::move(logger)) {
|
||||||
std::error_code ec;
|
std::error_code ec;
|
||||||
|
|
||||||
// Directory must exist.
|
// Directory must exist.
|
||||||
if (!std::filesystem::exists(prompt_dir_, ec) || ec) {
|
if (!std::filesystem::exists(prompt_dir_, ec) || ec) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"PromptDirectory: prompt directory does not exist: " +
|
"PromptDirectory: prompt directory does not exist: " +
|
||||||
prompt_dir_.string());
|
prompt_dir_.string());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Path must be a directory, not a file.
|
// Path must be a directory, not a file.
|
||||||
if (!std::filesystem::is_directory(prompt_dir_, ec) || ec) {
|
if (!std::filesystem::is_directory(prompt_dir_, ec) || ec) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
"PromptDirectory: prompt directory path is not a directory: " +
|
"PromptDirectory: prompt directory path is not a directory: " +
|
||||||
prompt_dir_.string());
|
prompt_dir_.string());
|
||||||
}
|
}
|
||||||
|
|
||||||
// Directory must be readable (probe with directory_iterator).
|
// Directory must be readable (probe with directory_iterator).
|
||||||
std::filesystem::directory_iterator probe(prompt_dir_, ec);
|
std::filesystem::directory_iterator probe(prompt_dir_, ec);
|
||||||
if (ec) {
|
if (ec) {
|
||||||
throw std::runtime_error(std::format(
|
throw std::runtime_error(std::format(
|
||||||
"PromptDirectory: prompt directory is not readable: {} ({})",
|
"PromptDirectory: prompt directory is not readable: {} ({})",
|
||||||
prompt_dir_.string(), ec.message()));
|
prompt_dir_.string(), ec.message()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logger_) {
|
if (logger_) {
|
||||||
logger_->Log(
|
logger_->Log(
|
||||||
{.level = LogLevel::Info,
|
{.level = LogLevel::Info,
|
||||||
.phase = PipelinePhase::Startup,
|
.phase = PipelinePhase::Startup,
|
||||||
.message =
|
.message =
|
||||||
std::string("[PromptDirectory] Resolved prompt directory: ") +
|
std::string("[PromptDirectory] Resolved prompt directory: ") +
|
||||||
prompt_dir_.string()});
|
prompt_dir_.string()});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string PromptDirectory::Load(std::string_view key) {
|
std::string PromptDirectory::Load(std::string_view key) {
|
||||||
const std::string key_str(key);
|
const std::string key_str(key);
|
||||||
|
|
||||||
// Return cached content if already loaded during this run.
|
// Return cached content if already loaded during this run.
|
||||||
const auto cache_it = cache_.find(key_str);
|
const auto cache_it = cache_.find(key_str);
|
||||||
if (cache_it != cache_.end()) {
|
if (cache_it != cache_.end()) {
|
||||||
return cache_it->second;
|
return cache_it->second;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Resolve <prompt_dir>/<key>.md and require it to exist.
|
// Resolve <prompt_dir>/<key>.md and require it to exist.
|
||||||
const std::filesystem::path file_path =
|
const std::filesystem::path file_path =
|
||||||
prompt_dir_ / std::filesystem::path(std::format("{}.md", key_str));
|
prompt_dir_ / std::filesystem::path(std::format("{}.md", key_str));
|
||||||
|
|
||||||
std::ifstream file(file_path);
|
std::ifstream file(file_path);
|
||||||
if (!file.is_open()) {
|
if (!file.is_open()) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
std::format("PromptDirectory: prompt file not found for key '{}': {}",
|
std::format("PromptDirectory: prompt file not found for key '{}': {}",
|
||||||
key_str, file_path.string()));
|
key_str, file_path.string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string content((std::istreambuf_iterator<char>(file)),
|
std::string content((std::istreambuf_iterator<char>(file)),
|
||||||
std::istreambuf_iterator<char>());
|
std::istreambuf_iterator<char>());
|
||||||
file.close();
|
file.close();
|
||||||
|
|
||||||
if (content.empty()) {
|
if (content.empty()) {
|
||||||
throw std::runtime_error(
|
throw std::runtime_error(
|
||||||
std::format("PromptDirectory: prompt file for key '{}' is empty: {}",
|
std::format("PromptDirectory: prompt file for key '{}' is empty: {}",
|
||||||
key_str, file_path.string()));
|
key_str, file_path.string()));
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logger_) {
|
if (logger_) {
|
||||||
logger_->Log(
|
logger_->Log(
|
||||||
{.level = LogLevel::Info,
|
{.level = LogLevel::Info,
|
||||||
.phase = PipelinePhase::Startup,
|
.phase = PipelinePhase::Startup,
|
||||||
.message = std::format(
|
.message = std::format(
|
||||||
"[PromptDirectory] Loaded prompt '{}' from '{}' ({} chars)",
|
"[PromptDirectory] Loaded prompt '{}' from '{}' ({} chars)",
|
||||||
key_str, file_path.string(), content.size())});
|
key_str, file_path.string(), content.size())});
|
||||||
}
|
}
|
||||||
|
|
||||||
// Cache the loaded content for future calls.
|
// Cache the loaded content for future calls.
|
||||||
cache_.emplace(key_str, content);
|
cache_.emplace(key_str, content);
|
||||||
return content;
|
return content;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user