Entropic 2.14.3
Local-first agentic inference engine
Loading...
Searching...
No Matches
json_serializers.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
37#pragma once
38
43#include <nlohmann/json.hpp>
44#include <cstddef>
45#include <string>
46#include <vector>
47
48namespace facade_json {
49
61inline nlohmann::json serialize_content_part(const entropic::ContentPart& p) {
62 nlohmann::json j;
64 j["type"] = "image";
65 j["path"] = entropic::mcp::sanitize_utf8(p.image_path);
66 j["url"] = entropic::mcp::sanitize_utf8(p.image_url);
67 j["width"] = p.width;
68 j["height"] = p.height;
69 } else {
70 j["type"] = "text";
71 j["text"] = entropic::mcp::sanitize_utf8(p.text);
72 }
73 return j;
74}
75
83inline nlohmann::json serialize_metadata(const entropic::Message& m) {
84 nlohmann::json meta = nlohmann::json::object();
85 for (const auto& [k, v] : m.metadata) {
86 meta[k] = entropic::mcp::sanitize_utf8(v);
87 }
88 return meta;
89}
90
98inline nlohmann::json serialize_parts(const entropic::Message& m) {
99 nlohmann::json parts = nlohmann::json::array();
100 // Indexed rather than a range-for: knots' parser loses this function's
101 // closing brace on `for (const auto& p : m.content_parts)` and then
102 // charges every following inline in this header to serialize_parts
103 // (SLOC 55, ABC 34 for a six-line body). Same class of mis-attribution
104 // the comment on LlamaCppBackend::invalidate_resident_kv records. Do not
105 // "clean this up" without re-running the complexity gate.
106 for (std::size_t i = 0; i < m.content_parts.size(); ++i) {
107 parts.push_back(serialize_content_part(m.content_parts[i]));
108 }
109 return parts;
110}
111
120inline nlohmann::json serialize_message(const entropic::Message& m) {
121 nlohmann::json obj = nlohmann::json::object();
122 obj["role"] = m.role;
123 obj["content"] = entropic::mcp::sanitize_utf8(m.content);
124 if (!m.metadata.empty()) { obj["metadata"] = serialize_metadata(m); }
125 if (!m.content_parts.empty()) {
126 obj["content_parts"] = serialize_parts(m);
127 }
128 return obj;
129}
130
139inline std::string serialize_messages(
140 const std::vector<entropic::Message>& messages) {
141 nlohmann::json arr = nlohmann::json::array();
142 for (const auto& m : messages) {
143 arr.push_back(serialize_message(m));
144 }
145 return arr.dump();
146}
147
155inline nlohmann::json parse(const char* str) {
156 if (!str) { return nlohmann::json(); }
157 return nlohmann::json::parse(str, nullptr, false);
158}
159
166inline nlohmann::json obj() { return nlohmann::json::object(); }
167
174inline nlohmann::json arr() { return nlohmann::json::array(); }
175
183inline std::string serialize_adapter_info(
184 const entropic::AdapterInfo& ai) {
185 nlohmann::json j;
186 j["name"] = ai.name;
187 j["state"] = static_cast<int>(ai.state);
188 j["scale"] = ai.scale;
189 j["ram_bytes"] = ai.ram_bytes;
190 j["path"] = ai.path.string();
191 j["tier_name"] = ai.tier_name;
192 j["base_model_path"] = ai.base_model_path;
193 return j.dump();
194}
195
203inline std::string serialize_adapter_list(
204 const std::vector<entropic::AdapterInfo>& adapters) {
205 nlohmann::json a = nlohmann::json::array();
206 for (const auto& ai : adapters) {
207 a.push_back({{"name", ai.name},
208 {"state", static_cast<int>(ai.state)},
209 {"scale", ai.scale},
210 {"tier_name", ai.tier_name}});
211 }
212 return a.dump();
213}
214
215} // namespace facade_json
Configuration structs with defaults.
Multimodal content types for messages.
std::string serialize_messages(const std::vector< entropic::Message > &messages)
Serialize messages to JSON array string — see serialize_message.
std::string serialize_adapter_list(const std::vector< entropic::AdapterInfo > &adapters)
Serialize a list of AdapterInfo to JSON array string.
std::string serialize_adapter_info(const entropic::AdapterInfo &ai)
Serialize an AdapterInfo to JSON string.
nlohmann::json serialize_parts(const entropic::Message &m)
Serialize a message's multimodal parts (gh#165).
nlohmann::json parse(const char *str)
Parse JSON string to nlohmann::json object.
nlohmann::json obj()
Create a JSON object from key-value pairs.
nlohmann::json serialize_content_part(const entropic::ContentPart &p)
Serialize one content part, losslessly (gh#165, v2.13.0).
nlohmann::json serialize_metadata(const entropic::Message &m)
Serialize a message's metadata map, sanitized (gh#165).
nlohmann::json serialize_message(const entropic::Message &m)
Serialize ONE message, losslessly — see the file header (gh#165).
nlohmann::json arr()
Create an empty JSON array.
Message struct for conversation history.
@ IMAGE
Image content (local path or data URI)
Metadata for a loaded LoRA adapter.
Definition config.h:126
std::string tier_name
Tier this adapter is assigned to (empty = unassigned)
Definition config.h:131
size_t ram_bytes
RAM consumption when WARM/HOT (0 if COLD)
Definition config.h:133
std::string base_model_path
Path of the base model this adapter targets.
Definition config.h:132
std::filesystem::path path
Resolved path to .gguf adapter file.
Definition config.h:128
AdapterState state
Current lifecycle state.
Definition config.h:129
std::string name
Unique adapter identifier.
Definition config.h:127
float scale
LoRA scaling factor (alpha/rank)
Definition config.h:130
A single content part within a multimodal message.
Definition content.h:41
int height
Image height after preprocessing (0 = not yet processed)
Definition content.h:47
int width
Image width after preprocessing (0 = not yet processed)
Definition content.h:46
std::string image_path
Local file path (type == IMAGE)
Definition content.h:44
std::string text
Text content (type == TEXT)
Definition content.h:43
ContentPartType type
Part type discriminant.
Definition content.h:42
std::string image_url
URL or data URI (type == IMAGE)
Definition content.h:45
A message in a conversation.
Definition message.h:36
std::vector< ContentPart > content_parts
Multimodal parts (empty for text-only)
Definition message.h:39
std::unordered_map< std::string, std::string > metadata
Arbitrary metadata.
Definition message.h:40
std::string content
Message text content (always populated)
Definition message.h:38
std::string role
Message role.
Definition message.h:37
UTF-8 validation + replacement at every system boundary where bytes change ownership.