Skip to content

Commit 66ad371

Browse files
authored
Merge pull request #1304 from singnet/http-lca
Add `LinkCreationAgent` HTTP API
2 parents 80625ed + 6499df7 commit 66ad371

20 files changed

Lines changed: 594 additions & 18 deletions

‎src/agents/command_router/BUILD‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ cc_library(
2222
deps = [
2323
"//agents:base_query_proxy",
2424
"//agents/evolution:query_evolution_proxy",
25+
"//agents/link_creation_agent:link_creation_proxy",
2526
"//agents/query_engine:pattern_matching_query_proxy",
2627
"//service_bus:service_bus_lib",
2728
],
@@ -49,6 +50,7 @@ cc_library(
4950
":bus_command_router_proxy",
5051
":evolution_metta_parser",
5152
"//agents/evolution:query_evolution_proxy",
53+
"//agents/link_creation_agent:link_creation_proxy",
5254
"//agents/query_engine:pattern_matching_query_proxy",
5355
"//agents/query_engine:query_answer",
5456
"//service_bus:bus_command_processor",

‎src/agents/command_router/BusCommandRouterProcessor.cc‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
#include "BaseQueryProxy.h"
88
#include "EvolutionMettaParser.h"
9+
#include "LinkCreationProxy.h"
910
#include "PatternMatchingQueryProxy.h"
1011
#include "PortPool.h"
1112
#include "QueryEvolutionProxy.h"
@@ -19,6 +20,7 @@
1920
using namespace command_router;
2021
using namespace query_engine;
2122
using namespace evolution;
23+
using namespace link_creation_agent;
2224
using namespace service_bus;
2325
using namespace commons;
2426

@@ -41,6 +43,22 @@ void apply_direct_evolution_parameters(Properties& evo_parameters, const Propert
4143
}
4244
}
4345

46+
// Keep link-creation defaults, then overlay query + link-creation keys. Context-agent
47+
// keys (use_cache, initial_rent_rate, ...) stay off the downstream proxy.
48+
void apply_direct_link_creation_parameters(Properties& lc_parameters,
49+
const Properties& router_parameters) {
50+
Properties allowed = SystemParametersSingleton::get_instance()->get_link_creation_agent_params() +
51+
SystemParametersSingleton::get_instance()->get_query_agent_params();
52+
allowed[LinkCreationProxy::LOG_NEW_LINKS] = true;
53+
allowed[LinkCreationProxy::LINK_CREATION_LOG_FILE_NAME] = string("");
54+
allowed[LinkCreationProxy::LINK_CREATOR_EXTRA_PARAMETERS] = string("");
55+
for (const auto& entry : router_parameters) {
56+
if (allowed.find(entry.first) != allowed.end()) {
57+
lc_parameters[entry.first] = entry.second;
58+
}
59+
}
60+
}
61+
4462
} // namespace
4563

4664
BusCommandRouterProcessor::BusCommandRouterProcessor(shared_ptr<ServiceBus> service_bus)
@@ -134,6 +152,8 @@ void BusCommandRouterProcessor::run_command_internal(shared_ptr<BusCommandRouter
134152
handle_query(router_proxy, arg);
135153
} else if (command == "evolution") {
136154
handle_evolution(router_proxy, arg);
155+
} else if (command == "link_creation") {
156+
handle_link_creation(router_proxy, arg);
137157
} else {
138158
RAISE_ERROR("Unknown router command: " + command);
139159
}
@@ -312,3 +332,27 @@ void BusCommandRouterProcessor::handle_evolution(shared_ptr<BusCommandRouterProx
312332
apply_direct_evolution_parameters(evo_proxy->parameters, proxy->parameters);
313333
this->forward_to_service(proxy, evo_proxy);
314334
}
335+
336+
void BusCommandRouterProcessor::handle_link_creation(shared_ptr<BusCommandRouterProxy> proxy,
337+
const string& arg) {
338+
string context = proxy->parameters.get<string>(CONTEXT_KEY);
339+
340+
LinkCreationMettaArgs metta_args;
341+
if (!try_parse_link_creation_metta_arg(arg, metta_args)) {
342+
RAISE_ERROR(
343+
"Link creation ARG must be a labeled MeTTa form ((query ...) (lc <link-creator-tag>))");
344+
}
345+
346+
const bool use_metta = proxy->parameters.get<bool>(BaseQueryProxy::USE_METTA_AS_QUERY_TOKENS);
347+
vector<string> query_tokens;
348+
if (use_metta) {
349+
query_tokens = {normalize_metta_percent_variables(metta_args.query)};
350+
} else {
351+
query_tokens = Utils::split(metta_args.query, ' ');
352+
}
353+
354+
auto lc_proxy = make_shared<LinkCreationProxy>(
355+
query_tokens, context, metta_args.link_creator_tag, BaseProxy::NONE);
356+
apply_direct_link_creation_parameters(lc_proxy->parameters, proxy->parameters);
357+
this->forward_to_service(proxy, lc_proxy);
358+
}

‎src/agents/command_router/BusCommandRouterProcessor.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ class BusCommandRouterProcessor : public BusCommandProcessor {
4242
void handle_set(shared_ptr<BusCommandRouterProxy> proxy, const string& arg);
4343
void handle_query(shared_ptr<BusCommandRouterProxy> proxy, const string& arg);
4444
void handle_evolution(shared_ptr<BusCommandRouterProxy> proxy, const string& arg);
45+
void handle_link_creation(shared_ptr<BusCommandRouterProxy> proxy, const string& arg);
4546

4647
void forward_to_service(shared_ptr<BusCommandRouterProxy> router_proxy,
4748
shared_ptr<BaseQueryProxy> service_proxy);

‎src/agents/command_router/BusCommandRouterProxy.cc‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "BusCommandRouterProxy.h"
22

33
#include "BaseQueryProxy.h"
4+
#include "LinkCreationProxy.h"
45
#include "PatternMatchingQueryProxy.h"
56
#include "QueryEvolutionProxy.h"
67
#include "ServiceBus.h"
@@ -11,6 +12,7 @@
1112

1213
using namespace command_router;
1314
using namespace evolution;
15+
using namespace link_creation_agent;
1416

1517
string BusCommandRouterProxy::PARAMS_RESPONSE = "params_response";
1618
string BusCommandRouterProxy::SET_PARAM_ACK = "set_param_ack";
@@ -34,6 +36,16 @@ BusCommandRouterProxy::~BusCommandRouterProxy() {}
3436

3537
void BusCommandRouterProxy::apply_default_parameters(Properties& parameters) {
3638
parameters += SystemParametersSingleton::get_instance()->get_command_router_params();
39+
// Runtime link-creation keys that are not part of agents.link_creation.params.
40+
if (parameters.find(LinkCreationProxy::LOG_NEW_LINKS) == parameters.end()) {
41+
parameters[LinkCreationProxy::LOG_NEW_LINKS] = true;
42+
}
43+
if (parameters.find(LinkCreationProxy::LINK_CREATION_LOG_FILE_NAME) == parameters.end()) {
44+
parameters[LinkCreationProxy::LINK_CREATION_LOG_FILE_NAME] = string("");
45+
}
46+
if (parameters.find(LinkCreationProxy::LINK_CREATOR_EXTRA_PARAMETERS) == parameters.end()) {
47+
parameters[LinkCreationProxy::LINK_CREATOR_EXTRA_PARAMETERS] = string("");
48+
}
3749
}
3850

3951
void BusCommandRouterProxy::pack_command_line_args() {

‎src/agents/command_router/BusCommandRouterProxy.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@ namespace command_router {
1515
* Proxy for the BUS_COMMAND_ROUTER service.
1616
*
1717
* Wire format: proxy->args = {COMMAND, ARG} (two strings).
18-
* Extends BaseQueryProxy so query/evolution answers from downstream services are received
19-
* directly on the client's proxy (no router relay).
18+
* Extends BaseQueryProxy so query, evolution, and link-creation answers from downstream
19+
* services are received directly on the client's proxy (no router relay).
2020
*
2121
* For HTTP evolution with remote fitness, EVAL_FITNESS from the evolution agent is buffered
2222
* here so the stream poller can forward it over WebSocket and reply with EVAL_FITNESS_RESPONSE.

‎src/agents/command_router/EvolutionMettaParser.cc‎

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -482,3 +482,75 @@ bool command_router::try_parse_evolution_metta_arg(const string& arg, EvolutionM
482482
}
483483
return found_labeled_slot;
484484
}
485+
486+
namespace {
487+
488+
const string PARAM_LINK_CREATOR = "link-creator-tag";
489+
const string ALIAS_LINK_CREATOR = "lc";
490+
491+
string canonical_link_creation_slot(const string& name) {
492+
if (name == PARAM_QUERY || name == ALIAS_QUERY) {
493+
return PARAM_QUERY;
494+
}
495+
if (name == PARAM_LINK_CREATOR || name == ALIAS_LINK_CREATOR) {
496+
return PARAM_LINK_CREATOR;
497+
}
498+
return "";
499+
}
500+
501+
} // namespace
502+
503+
bool command_router::try_parse_link_creation_metta_arg(const string& arg, LinkCreationMettaArgs& out) {
504+
out = LinkCreationMettaArgs{};
505+
string trimmed = Utils::trim(arg);
506+
if (trimmed.empty() || trimmed[0] != '(') {
507+
return false;
508+
}
509+
510+
auto actions = parse_metta(trimmed);
511+
if (actions->root_handle.empty()) {
512+
return false;
513+
}
514+
auto root_link = as_link(actions->handle_to_atom.at(actions->root_handle));
515+
if (!root_link) {
516+
return false;
517+
}
518+
519+
bool found_query = false;
520+
bool found_tag = false;
521+
vector<shared_ptr<Atom>> clauses = link_targets(root_link, *actions);
522+
for (const auto& clause_atom : clauses) {
523+
auto clause_link = as_link(clause_atom);
524+
if (!clause_link) {
525+
continue;
526+
}
527+
vector<shared_ptr<Atom>> children = link_targets(clause_link, *actions);
528+
if (children.empty()) {
529+
continue;
530+
}
531+
string canonical = canonical_link_creation_slot(atom_name(children[0]));
532+
if (canonical.empty()) {
533+
continue;
534+
}
535+
// Each recognized slot is (label body). Extra bodies would be dropped, so a
536+
// caller who sent two MeTTa expressions would run only the first.
537+
if (children.size() != 2) {
538+
return false;
539+
}
540+
const auto& body = children[1];
541+
if (canonical == PARAM_QUERY) {
542+
if (found_query) {
543+
return false;
544+
}
545+
out.query = query_expression_from_atom(body, *actions);
546+
found_query = !out.query.empty();
547+
} else if (canonical == PARAM_LINK_CREATOR) {
548+
if (found_tag) {
549+
return false;
550+
}
551+
out.link_creator_tag = query_expression_from_atom(body, *actions);
552+
found_tag = !out.link_creator_tag.empty();
553+
}
554+
}
555+
return found_query && found_tag;
556+
}

‎src/agents/command_router/EvolutionMettaParser.h‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,26 @@ string canonical_evolution_param_key(const string& key_or_alias);
5555
*/
5656
bool try_parse_evolution_metta_arg(const string& arg, EvolutionMettaArgs& out);
5757

58+
/** Fields parsed from a labeled MeTTa link-creation ARG (context comes from router params). */
59+
struct LinkCreationMettaArgs {
60+
string query;
61+
string link_creator_tag;
62+
};
63+
64+
/**
65+
* Parse link-creation ARG as a MeTTa list of labeled clauses, e.g.:
66+
* (
67+
* (query (and (Concept $C1) (Concept $C2)))
68+
* (lc and_two_predicates)
69+
* )
70+
*
71+
* Slot labels: q | query; lc | link-creator-tag.
72+
* A link_template query is one quoted token stream: (query "LINK_TEMPLATE ...").
73+
*
74+
* @return true when both the query and link-creator tag are present.
75+
*/
76+
bool try_parse_link_creation_metta_arg(const string& arg, LinkCreationMettaArgs& out);
77+
5878
/** Rewrite unquoted `%name` variable tokens to `$name`. Quoted percents are left unchanged. */
5979
string normalize_metta_percent_variables(const string& expression);
6080

‎src/agents/command_router/http_api/BusCommandRouterProxyStreamPoller.cc‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,7 @@ PollStreamResult BusCommandRouterProxyStreamPoller::poll_stream(
171171
return poll_succeeded();
172172
}
173173

174-
if (command_type == "query" || command_type == "evolution") {
174+
if (command_type == "query" || command_type == "evolution" || command_type == "link_creation") {
175175
while (!router_proxy->routed_flag && !finished_or_error()) {
176176
if (handle_abort()) {
177177
return {};

‎src/agents/command_router/http_api/BusCommandRouterProxyStreamPoller.h‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,17 @@ class BusCommandRouterProxyStreamPoller {
3838
/**
3939
* Poll router_proxy until the command finishes, is aborted, or fails.
4040
*
41-
* For command_type "query" and "evolution", answers are popped from the proxy
42-
* and forwarded in batches of at most items_per_chunk JSON values. For "get" and
43-
* "set", a single chunk is emitted once the proxy response is ready.
41+
* For command_type "query", "evolution", and "link_creation", answers are popped
42+
* from the proxy and forwarded in batches of at most items_per_chunk JSON values.
43+
* For "get" and "set", a single chunk is emitted once the proxy response is ready.
4444
*
4545
* When command_type is "evolution" and on_eval_fitness is set, pending EVAL_FITNESS
4646
* requests on the proxy are converted to JSON and delegated to the handler; the
4747
* returned floats are sent back to the evolution agent.
4848
*
4949
* @param router_proxy Proxy already issued on the service bus.
50-
* @param command_type Router command: "get", "set", "query", or "evolution".
51-
* @param items_per_chunk Maximum answers per on_chunk call for query/evolution.
50+
* @param command_type Router command: "get", "set", "query", "evolution", or "link_creation".
51+
* @param items_per_chunk Maximum answers per on_chunk call for query/evolution/link_creation.
5252
* Must be at least 1.
5353
* @param should_abort Optional callback; when it returns true, the proxy is aborted
5454
* and on_aborted is invoked.

‎src/agents/command_router/http_api/CommandRouterHttpAPI.cc‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,10 @@ using namespace agents;
2121

2222
using json = nlohmann::json;
2323

24-
const unordered_set<string> CommandRouterHttpAPI::VALID_COMMAND = {HttpCommandProxyFactory::QUERY,
25-
HttpCommandProxyFactory::EVOLUTION};
24+
const unordered_set<string> CommandRouterHttpAPI::VALID_COMMAND = {
25+
HttpCommandProxyFactory::QUERY,
26+
HttpCommandProxyFactory::EVOLUTION,
27+
HttpCommandProxyFactory::LINK_CREATION};
2628

2729
namespace {
2830

@@ -198,7 +200,9 @@ void CommandRouterHttpAPI::setup_routes() {
198200

199201
if (!this->is_valid_command(command)) {
200202
this->set_json_response(
201-
response, 400, {{"error", "Invalid command. Allowed values: query, evolution"}});
203+
response,
204+
400,
205+
{{"error", "Invalid command. Allowed values: query, evolution, link_creation"}});
202206
return;
203207
}
204208

0 commit comments

Comments
 (0)