Repository navigation
Expand file tree
/
Copy pathrecipe.cpp
More file actions
144 lines (127 loc) · 4.76 KB
/
Copy pathrecipe.cpp
File metadata and controls
144 lines (127 loc) · 4.76 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
/**
* Recipe loader. See include/ragger/recipe.h.
*/
#include "recipe.h"
#include "Logger.h"
#include <filesystem>
#include <fstream>
#include <unordered_map>
namespace ragger {
namespace fs = std::filesystem;
namespace {
const std::unordered_map<std::string, LayerKind>& layer_kind_map() {
static const std::unordered_map<std::string, LayerKind> m = {
{"raw_turn", LayerKind::RawTurn},
{"turn_summary", LayerKind::TurnSummary},
{"episode", LayerKind::Episode},
// "session_summary" is an accepted alias of "episode" (PHASE5 D.3):
// user copies in ~/.ragger/recipes/ keep working unchanged.
{"session_summary", LayerKind::Episode},
// Parses but emits nothing at 0.17 (project rollups removed).
{"project_summary", LayerKind::ProjectSummary},
{"decisions", LayerKind::Decisions},
{"general_search", LayerKind::GeneralSearch},
};
return m;
}
void parse_layers_into(Recipe& recipe, const nlohmann::json& layers,
const std::string& source) {
const auto& kinds = layer_kind_map();
for (const auto& layer : layers) {
const std::string kind_str = layer.value("kind", "");
auto it = kinds.find(kind_str);
if (it == kinds.end()) {
Diskerror::Logger::warn(
"recipe '" + source + "': unknown layer kind '" + kind_str +
"', skipping");
continue;
}
int limit = layer.value("limit", 0);
// Accept legacy "count" field with a deprecation warning.
if (limit == 0 && layer.contains("count")) {
Diskerror::Logger::warn(
"recipe '" + source + "': layer field \"count\" is deprecated; use \"limit\" instead");
limit = layer.value("count", 0);
}
recipe.layers.push_back({it->second, limit});
}
}
} // namespace
Recipe parse_recipe(const nlohmann::json& j, const std::string& source) {
Recipe r;
r.name = j.value("name", source);
r.description = j.value("description", "");
r.max_tokens = j.value("max_tokens", 8000);
r.chars_per_token = j.value("chars_per_token", 4.0f);
if (j.contains("layers") && j["layers"].is_array()) {
parse_layers_into(r, j["layers"], r.name.empty() ? source : r.name);
}
return r;
}
std::vector<Recipe> load_recipes_from_dir(const std::string& dir) {
std::vector<Recipe> out;
std::error_code ec;
if (dir.empty() || !fs::exists(dir, ec) || !fs::is_directory(dir, ec)) {
return out;
}
for (const auto& entry : fs::directory_iterator(dir, ec)) {
if (!entry.is_regular_file()) continue;
const auto path = entry.path();
if (path.extension() != ".json") continue;
std::ifstream f(path);
if (!f) continue;
try {
nlohmann::json doc = nlohmann::json::parse(f);
const auto stem = path.stem().string();
if (doc.is_array()) {
for (const auto& item : doc) {
out.push_back(parse_recipe(item, stem));
}
} else {
out.push_back(parse_recipe(doc, stem));
}
} catch (const std::exception& e) {
Diskerror::Logger::warn(
std::string("recipe load failed for ") + path.string() +
": " + e.what());
}
}
return out;
}
std::vector<Recipe> builtin_recipes() {
std::vector<Recipe> out;
// natural_fading — the default. Mimics how memory decays:
// freshly-spoken turns stay verbatim, older ones fade to summaries,
// then to a session-level digest, then to project context, then to
// pertinent decisions.
Recipe nf;
nf.name = "natural_fading";
nf.description = "Recent raw → turn summaries → episodes → decisions";
nf.layers = {
{LayerKind::RawTurn, 2},
{LayerKind::TurnSummary, 5},
{LayerKind::Episode, 1},
{LayerKind::Decisions, 3},
{LayerKind::GeneralSearch, 5},
};
nf.max_tokens = 8000;
out.push_back(std::move(nf));
// raw_only — preserves today's pre-recipe behavior under an explicit
// name. Useful as a debugging baseline and for short sessions.
Recipe ro;
ro.name = "raw_only";
ro.description = "Raw turns of the active session, oldest first";
ro.layers = { {LayerKind::RawTurn, 0} };
ro.max_tokens = 16000;
out.push_back(std::move(ro));
return out;
}
const Recipe* find_recipe(const std::vector<Recipe>& recipes,
const std::string& name) {
if (name.empty()) return nullptr;
for (const auto& r : recipes) {
if (r.name == name) return &r;
}
return nullptr;
}
} // namespace ragger