-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathresponse_formatter.cpp
More file actions
151 lines (125 loc) · 4.18 KB
/
response_formatter.cpp
File metadata and controls
151 lines (125 loc) · 4.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#include "../include/response_formatter.hpp"
#include <sstream>
#include <string>
#include <nlohmann/json.hpp>
#include "metadata.hpp"
namespace pg_ai {
namespace {
std::string format_multiline_comment(const std::string& text,
const std::string& prefix = "-- ",
std::size_t max_width = 70) {
std::string result;
std::istringstream stream(text);
std::string word;
std::string current_line = prefix;
while (stream >> word) {
const std::size_t space_needed = current_line.length() + word.length() +
(current_line != prefix ? 1 : 0);
if (space_needed > max_width) {
result += current_line + "\n";
current_line = prefix + word;
} else {
if (current_line != prefix)
current_line += " ";
current_line += word;
}
}
if (current_line != prefix) {
result += current_line;
}
return result;
}
} // anonymous namespace
std::string ResponseFormatter::formatResponse(
const QueryResult& result,
const config::Configuration& config) {
if (config.use_formatted_response) {
return createJSONResponse(result, config);
} else {
return createPlainTextResponse(result, config);
}
}
std::string ResponseFormatter::createJSONResponse(
const QueryResult& result,
const config::Configuration& config) {
nlohmann::json response;
// Always include the query
response["query"] = result.generated_query;
response["success"] = result.success;
// Add optional fields based on configuration
if (config.show_explanation && !result.explanation.empty()) {
response["explanation"] = result.explanation;
}
if (config.show_warnings && !result.warnings.empty()) {
response["warnings"] = result.warnings;
}
if (config.show_suggested_visualization &&
!result.suggested_visualization.empty()) {
response["suggested_visualization"] = result.suggested_visualization;
}
// Add metadata
if (result.row_limit_applied) {
response["row_limit_applied"] = true;
}
// ✅ Add AI metadata (only if explanation exists)
if (!result.explanation.empty()) {
response["sources"] = extractSources(result.explanation);
response["confidence"] = calculateConfidence(result.explanation);
response["tags"] = extractTags(result.explanation);
}
return response.dump(2); // Pretty print with 2-space indentation
}
std::string ResponseFormatter::createPlainTextResponse(
const QueryResult& result,
const config::Configuration& config) {
std::ostringstream output;
// Main query result
output << "-- Query:\n";
output << result.generated_query;
// Add explanation if enabled
if (config.show_explanation && !result.explanation.empty()) {
output << "\n\n-- Explanation:\n";
output << format_multiline_comment(result.explanation);
}
// Add warnings if enabled
if (config.show_warnings && !result.warnings.empty()) {
output << "\n\n" << formatWarnings(result.warnings);
}
// Add suggested visualization if enabled
if (config.show_suggested_visualization &&
!result.suggested_visualization.empty()) {
output << "\n\n" << formatVisualization(result.suggested_visualization);
}
// Add metadata
if (result.row_limit_applied) {
output << "\n\n-- Note: Row limit was automatically applied to this query "
"for safety";
}
return output.str();
}
std::string ResponseFormatter::formatWarnings(
const std::vector<std::string>& warnings) {
std::ostringstream output;
if (warnings.size() == 1) {
output << "-- Warning:\n";
output << format_multiline_comment(warnings[0]);
} else {
output << "-- Warnings:\n";
for (size_t i = 0; i < warnings.size(); ++i) {
std::string numbered = std::to_string(i + 1) + ". " + warnings[i];
output << format_multiline_comment(numbered, "-- ", 70);
if (i + 1 < warnings.size()) {
output << "\n";
}
}
}
return output.str();
}
std::string ResponseFormatter::formatVisualization(
const std::string& visualization) {
std::ostringstream output;
output << "-- Suggested Visualization:\n";
output << format_multiline_comment(visualization);
return output.str();
}
} // namespace pg_ai