memory: remove exception parsing helpers

This commit is contained in:
Χγφτ Kompanion 2025-10-15 18:53:01 +02:00
parent 8479c23234
commit 122df11433
1 changed files with 42 additions and 21 deletions

View File

@ -1,10 +1,12 @@
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <charconv>
#include <chrono> #include <chrono>
#include <cctype> #include <cctype>
#include <cstdlib> #include <cstdlib>
#include <ctime> #include <ctime>
#include <exception>
#include <iomanip> #include <iomanip>
#include <optional> #include <optional>
#include <sstream> #include <sstream>
@ -190,11 +192,13 @@ inline std::optional<int> extract_int_field(const std::string& json, const std::
std::size_t start = pos; std::size_t start = pos;
while (pos < json.size() && (std::isdigit(static_cast<unsigned char>(json[pos])) || json[pos] == '-')) ++pos; while (pos < json.size() && (std::isdigit(static_cast<unsigned char>(json[pos])) || json[pos] == '-')) ++pos;
if (start == pos) return std::nullopt; if (start == pos) return std::nullopt;
try { const std::string_view token{json.data() + start, pos - start};
return std::stoi(json.substr(start, pos - start)); int value = 0;
} catch (...) { const auto parseResult = std::from_chars(token.data(), token.data() + token.size(), value);
if (parseResult.ec != std::errc() || parseResult.ptr != token.data() + token.size()) {
return std::nullopt; return std::nullopt;
} }
return value;
} }
inline std::string iso8601_from_tp(std::chrono::system_clock::time_point tp) { inline std::string iso8601_from_tp(std::chrono::system_clock::time_point tp) {
@ -299,10 +303,12 @@ inline std::vector<float> parse_float_array(const std::string& json, const std::
if (pos >= arr.size()) break; if (pos >= arr.size()) break;
std::size_t end = pos; std::size_t end = pos;
while (end < arr.size() && (std::isdigit(static_cast<unsigned char>(arr[end])) || arr[end] == '.' || arr[end] == 'e' || arr[end] == 'E' || arr[end] == '+' || arr[end] == '-')) ++end; while (end < arr.size() && (std::isdigit(static_cast<unsigned char>(arr[end])) || arr[end] == '.' || arr[end] == 'e' || arr[end] == 'E' || arr[end] == '+' || arr[end] == '-')) ++end;
try { float parsed = 0.0f;
values.push_back(std::stof(arr.substr(pos, end - pos))); const char* begin = arr.c_str() + pos;
} catch (...) { const char* finish = arr.c_str() + end;
// Skip unparsable token. const auto fc = std::from_chars(begin, finish, parsed);
if (fc.ec == std::errc() && fc.ptr == finish) {
values.push_back(parsed);
} }
pos = end; pos = end;
} }
@ -384,7 +390,9 @@ inline std::string upsert_memory(const std::string& reqJson) {
std::vector<std::string> ids; std::vector<std::string> ids;
ids.reserve(items.size()); ids.reserve(items.size());
#if defined(__cpp_exceptions)
try { try {
#endif
for (auto& parsed : items) { for (auto& parsed : items) {
kom::ItemRow row; kom::ItemRow row;
row.id = parsed.id; row.id = parsed.id;
@ -412,11 +420,14 @@ inline std::string upsert_memory(const std::string& reqJson) {
dal.upsertEmbeddings(std::vector<kom::EmbeddingRow>{emb}); dal.upsertEmbeddings(std::vector<kom::EmbeddingRow>{emb});
} }
} }
if (hasTx) dal.commit(); #if defined(__cpp_exceptions)
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
if (hasTx) dal.rollback(); if (hasTx) dal.rollback();
return detail::error_response("internal_error", ex.what()); return detail::error_response("internal_error", ex.what());
} }
#endif
if (hasTx) dal.commit();
std::ostringstream os; std::ostringstream os;
os << "{\"upserted\":" << ids.size(); os << "{\"upserted\":" << ids.size();
@ -530,23 +541,33 @@ inline std::string save_context(const std::string& reqJson) {
} }
} }
std::string insertedId;
#if defined(__cpp_exceptions)
try { try {
const std::string id = dal.upsertItem(row); #endif
auto stored = dal.getItemById(id); insertedId = dal.upsertItem(row);
const auto createdTp = stored ? stored->created_at : row.created_at; #if defined(__cpp_exceptions)
std::string createdIso = detail::iso8601_from_tp(createdTp);
if (createdIso.empty()) {
createdIso = detail::iso8601_from_tp(std::chrono::system_clock::now());
}
std::ostringstream os;
os << "{\"id\":\"" << detail::json_escape(id) << "\"";
os << ",\"created_at\":\"" << createdIso << "\"";
os << "}";
return os.str();
} catch (const std::exception& ex) { } catch (const std::exception& ex) {
return detail::error_response("internal_error", ex.what()); return detail::error_response("internal_error", ex.what());
} }
#endif
if (insertedId.empty()) {
return detail::error_response("internal_error", "failed to upsert item");
}
auto stored = dal.getItemById(insertedId);
const auto createdTp = stored ? stored->created_at : row.created_at;
std::string createdIso = detail::iso8601_from_tp(createdTp);
if (createdIso.empty()) {
createdIso = detail::iso8601_from_tp(std::chrono::system_clock::now());
}
std::ostringstream os;
os << "{\"id\":\"" << detail::json_escape(insertedId) << "\"";
os << ",\"created_at\":\"" << createdIso << "\"";
os << "}";
return os.str();
} }
inline std::string recall_context(const std::string& reqJson) { inline std::string recall_context(const std::string& reqJson) {