ELP: two-layer activation pipeline (activate → suppress → reason → generate)
elp-input.el: replace broken engram_search_json with engram_activate_json as Layer 1. Layer 2 suppress/filter keeps nodes with non-zero salience/ importance. Reason step extracts patient from top activated node content. ELP grammar realizes the response via generate(). routes.el: add 'elp' event_type to handle_dharma_recv so the studio can route ELP requests through dharma.
This commit is contained in:
Vendored
+318
@@ -0,0 +1,318 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include "el_runtime.h"
|
||||
|
||||
el_val_t tier_working(void);
|
||||
el_val_t tier_episodic(void);
|
||||
el_val_t tier_canonical(void);
|
||||
el_val_t mem_store(el_val_t content, el_val_t label, el_val_t tags);
|
||||
el_val_t mem_remember(el_val_t content, el_val_t tags);
|
||||
el_val_t mem_recall(el_val_t query, el_val_t depth);
|
||||
el_val_t mem_search(el_val_t query, el_val_t limit);
|
||||
el_val_t mem_strengthen(el_val_t node_id);
|
||||
el_val_t mem_forget(el_val_t node_id);
|
||||
el_val_t mem_consolidate(void);
|
||||
el_val_t mem_save(el_val_t path);
|
||||
el_val_t mem_load(el_val_t path);
|
||||
el_val_t pulse_count(void);
|
||||
el_val_t pulse_inc(void);
|
||||
el_val_t make_action(el_val_t kind, el_val_t payload);
|
||||
el_val_t perceive(void);
|
||||
el_val_t attend(el_val_t node_json);
|
||||
el_val_t respond(el_val_t action_json);
|
||||
el_val_t record(el_val_t outcome_json);
|
||||
el_val_t one_cycle(void);
|
||||
el_val_t awareness_run(void);
|
||||
el_val_t chat_default_model(void);
|
||||
el_val_t engram_compile(el_val_t intent);
|
||||
el_val_t json_safe(el_val_t s);
|
||||
el_val_t build_system_prompt(el_val_t ctx);
|
||||
el_val_t hist_append(el_val_t hist, el_val_t role, el_val_t content);
|
||||
el_val_t hist_trim(el_val_t hist);
|
||||
el_val_t clean_llm_response(el_val_t s);
|
||||
el_val_t handle_chat(el_val_t body);
|
||||
el_val_t handle_see(el_val_t body);
|
||||
el_val_t studio_tools_json(void);
|
||||
el_val_t handle_chat_agentic(el_val_t body);
|
||||
el_val_t handle_chat_as_soul(el_val_t body);
|
||||
el_val_t auto_persist(el_val_t req, el_val_t resp);
|
||||
el_val_t auth_headers(el_val_t tok);
|
||||
el_val_t axon_get(el_val_t path);
|
||||
el_val_t axon_post(el_val_t path, el_val_t body);
|
||||
el_val_t handle_conversations(el_val_t method);
|
||||
el_val_t handle_config(el_val_t method, el_val_t body);
|
||||
el_val_t dharma_registry(void);
|
||||
el_val_t dharma_network_state(void);
|
||||
el_val_t handle_dharma(el_val_t path, el_val_t method, el_val_t body);
|
||||
el_val_t handle_tool(el_val_t path, el_val_t method, el_val_t body);
|
||||
el_val_t handle_nlg(el_val_t path, el_val_t method, el_val_t body);
|
||||
el_val_t render_studio(void);
|
||||
el_val_t elp_extract_topic(el_val_t msg);
|
||||
el_val_t elp_detect_predicate(el_val_t msg);
|
||||
el_val_t elp_parse(el_val_t msg);
|
||||
el_val_t handle_elp_chat(el_val_t body);
|
||||
el_val_t strip_query(el_val_t path);
|
||||
el_val_t err_404(el_val_t path);
|
||||
el_val_t err_405(el_val_t method, el_val_t path);
|
||||
el_val_t route_health(void);
|
||||
el_val_t route_lineage(void);
|
||||
el_val_t route_imprint_contextual(el_val_t body);
|
||||
el_val_t route_imprint_user(el_val_t body);
|
||||
el_val_t route_synthesize(el_val_t body);
|
||||
el_val_t handle_dharma_recv(el_val_t body);
|
||||
el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body);
|
||||
|
||||
el_val_t strip_query(el_val_t path) {
|
||||
el_val_t q = str_index_of(path, EL_STR("?"));
|
||||
if (q < 0) {
|
||||
return path;
|
||||
}
|
||||
return str_slice(path, 0, q);
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t err_404(el_val_t path) {
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"error\":\"not found\",\"path\":\""), path), EL_STR("\"}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t err_405(el_val_t method, el_val_t path) {
|
||||
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"error\":\"method not allowed\",\"method\":\""), method), EL_STR("\",\"path\":\"")), path), EL_STR("\"}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t route_health(void) {
|
||||
el_val_t cgi_id = state_get(EL_STR("soul_cgi_id"));
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"status\":\"alive\",\"cgi_id\":\""), cgi_id), EL_STR("\"}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t route_lineage(void) {
|
||||
el_val_t cgi_id = state_get(EL_STR("soul_cgi_id"));
|
||||
el_val_t q = el_str_concat(EL_STR("lineage:"), cgi_id);
|
||||
el_val_t results = engram_search_json(q, 1);
|
||||
el_val_t len = json_array_len(results);
|
||||
if (len <= 0) {
|
||||
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), cgi_id), EL_STR("\"")), EL_STR(",\"tier\":\"citizen\"")), EL_STR(",\"is_founding\":true")), EL_STR(",\"validation_attempts\":0")), EL_STR(",\"training_sessions\":0")), EL_STR(",\"is_sterile\":false}"));
|
||||
}
|
||||
el_val_t raw = json_get_raw(results, EL_STR("0"));
|
||||
return raw;
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t route_imprint_contextual(el_val_t body) {
|
||||
if (str_eq(body, EL_STR(""))) {
|
||||
return EL_STR("{\"ok\":false,\"error\":\"empty body\"}");
|
||||
}
|
||||
el_val_t tags = EL_STR("[\"imprint\",\"contextual\"]");
|
||||
el_val_t id = engram_node_full(body, EL_STR("Entity"), EL_STR("imprint:contextual"), el_from_float(0.7), el_from_float(0.6), el_from_float(0.9), EL_STR("Working"), tags);
|
||||
if (str_eq(id, EL_STR(""))) {
|
||||
return EL_STR("{\"ok\":false,\"error\":\"engram write failed\"}");
|
||||
}
|
||||
state_set(EL_STR("active_contextual_imprint"), id);
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), id), EL_STR("\"}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t route_imprint_user(el_val_t body) {
|
||||
if (str_eq(body, EL_STR(""))) {
|
||||
return EL_STR("{\"ok\":false,\"error\":\"empty body\"}");
|
||||
}
|
||||
el_val_t tags = EL_STR("[\"imprint\",\"user\"]");
|
||||
el_val_t id = engram_node_full(body, EL_STR("Entity"), EL_STR("imprint:user"), el_from_float(0.7), el_from_float(0.6), el_from_float(0.9), EL_STR("Working"), tags);
|
||||
if (str_eq(id, EL_STR(""))) {
|
||||
return EL_STR("{\"ok\":false,\"error\":\"engram write failed\"}");
|
||||
}
|
||||
state_set(EL_STR("active_user_imprint"), id);
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), id), EL_STR("\"}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t route_synthesize(el_val_t body) {
|
||||
if (str_eq(body, EL_STR(""))) {
|
||||
return EL_STR("{\"mechanism\":\"did not engage\"}");
|
||||
}
|
||||
el_val_t parent_a = json_get(body, EL_STR("parent_a"));
|
||||
el_val_t parent_b = json_get(body, EL_STR("parent_b"));
|
||||
if (str_eq(parent_a, EL_STR(""))) {
|
||||
return EL_STR("{\"mechanism\":\"did not engage\"}");
|
||||
}
|
||||
if (str_eq(parent_b, EL_STR(""))) {
|
||||
return EL_STR("{\"mechanism\":\"did not engage\"}");
|
||||
}
|
||||
el_val_t req = el_str_concat(el_str_concat(el_str_concat(EL_STR("synthesize "), parent_a), EL_STR(" ")), parent_b);
|
||||
el_val_t tags = EL_STR("[\"soul-inbox-pending\",\"synthesis-request\"]");
|
||||
engram_node_full(req, EL_STR("Entity"), EL_STR("synthesis-request"), el_from_float(0.8), el_from_float(0.8), el_from_float(0.9), EL_STR("Working"), tags);
|
||||
return EL_STR("{\"mechanism\":\"did not engage\"}");
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t handle_dharma_recv(el_val_t body) {
|
||||
el_val_t content_raw = json_get(body, EL_STR("content"));
|
||||
el_val_t from_id = json_get(body, EL_STR("from"));
|
||||
el_val_t event_type = json_get(content_raw, EL_STR("event_type"));
|
||||
el_val_t payload = json_get(content_raw, EL_STR("payload"));
|
||||
el_val_t eff_event = ({ el_val_t _if_result_1 = 0; if (str_eq(event_type, EL_STR(""))) { _if_result_1 = (EL_STR("chat")); } else { _if_result_1 = (event_type); } _if_result_1; });
|
||||
el_val_t eff_payload = ({ el_val_t _if_result_2 = 0; if (str_eq(payload, EL_STR(""))) { _if_result_2 = (content_raw); } else { _if_result_2 = (payload); } _if_result_2; });
|
||||
if (str_eq(eff_event, EL_STR("chat"))) {
|
||||
el_val_t msg = json_get(eff_payload, EL_STR("message"));
|
||||
el_val_t chat_body = ({ el_val_t _if_result_3 = 0; if (str_eq(msg, EL_STR(""))) { _if_result_3 = (el_str_concat(el_str_concat(EL_STR("{\"message\":\""), str_replace(str_replace(eff_payload, EL_STR("\\"), EL_STR("\\\\")), EL_STR("\""), EL_STR("\\\""))), EL_STR("\"}"))); } else { _if_result_3 = (eff_payload); } _if_result_3; });
|
||||
el_val_t agentic_flag = json_get_bool(eff_payload, EL_STR("agentic"));
|
||||
el_val_t reply = ({ el_val_t _if_result_4 = 0; if (agentic_flag) { _if_result_4 = (handle_chat_agentic(chat_body)); } else { _if_result_4 = (handle_chat(chat_body)); } _if_result_4; });
|
||||
auto_persist(chat_body, reply);
|
||||
return reply;
|
||||
}
|
||||
if (str_eq(eff_event, EL_STR("memory"))) {
|
||||
el_val_t query = json_get(eff_payload, EL_STR("query"));
|
||||
el_val_t limit_str = json_get(eff_payload, EL_STR("limit"));
|
||||
el_val_t limit = ({ el_val_t _if_result_5 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_5 = (20); } else { _if_result_5 = (str_to_int(limit_str)); } _if_result_5; });
|
||||
el_val_t q = ({ el_val_t _if_result_6 = 0; if (str_eq(query, EL_STR(""))) { _if_result_6 = (eff_payload); } else { _if_result_6 = (query); } _if_result_6; });
|
||||
return engram_search_json(q, limit);
|
||||
}
|
||||
if (str_eq(eff_event, EL_STR("tool"))) {
|
||||
el_val_t path_field = json_get(eff_payload, EL_STR("path"));
|
||||
el_val_t method_field = json_get(eff_payload, EL_STR("method"));
|
||||
el_val_t tool_body = json_get(eff_payload, EL_STR("body"));
|
||||
el_val_t eff_method = ({ el_val_t _if_result_7 = 0; if (str_eq(method_field, EL_STR(""))) { _if_result_7 = (EL_STR("POST")); } else { _if_result_7 = (method_field); } _if_result_7; });
|
||||
return handle_tool(path_field, eff_method, tool_body);
|
||||
}
|
||||
if (str_eq(eff_event, EL_STR("see"))) {
|
||||
return handle_see(eff_payload);
|
||||
}
|
||||
if (str_eq(eff_event, EL_STR("health"))) {
|
||||
return route_health();
|
||||
}
|
||||
if (str_eq(eff_event, EL_STR("chat_as_soul"))) {
|
||||
return handle_chat_as_soul(eff_payload);
|
||||
}
|
||||
if (str_eq(eff_event, EL_STR("elp"))) {
|
||||
return handle_elp_chat(eff_payload);
|
||||
}
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"error\":\"unknown event_type\",\"event_type\":\""), eff_event), EL_STR("\"}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) {
|
||||
el_val_t clean = strip_query(path);
|
||||
if (str_eq(method, EL_STR("POST")) && str_eq(clean, EL_STR("/dharma/recv"))) {
|
||||
return handle_dharma_recv(body);
|
||||
}
|
||||
if (str_eq(method, EL_STR("GET"))) {
|
||||
if (str_eq(clean, EL_STR("/health"))) {
|
||||
return route_health();
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/lineage"))) {
|
||||
return route_lineage();
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/graph")) || str_eq(clean, EL_STR("/api/graph/nodes"))) {
|
||||
return engram_scan_nodes_json(9999, 0);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/graph/edges"))) {
|
||||
el_val_t snap_path = el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/engram/snapshot.json"));
|
||||
engram_save(snap_path);
|
||||
el_val_t snap = fs_read(snap_path);
|
||||
el_val_t edges_raw = json_get_raw(snap, EL_STR("edges"));
|
||||
return ({ el_val_t _if_result_8 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_8 = (EL_STR("[]")); } else { _if_result_8 = (edges_raw); } _if_result_8; });
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/chat"))) {
|
||||
return handle_chat(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/conversations"))) {
|
||||
return handle_conversations(method);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/config"))) {
|
||||
return handle_config(method, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/tools/"))) {
|
||||
return handle_tool(clean, method, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/dharma"))) {
|
||||
return handle_dharma(clean, method, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/nlg"))) {
|
||||
return handle_nlg(clean, method, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/memories"))) {
|
||||
return axon_get(clean);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/knowledge"))) {
|
||||
return axon_get(clean);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/backlog"))) {
|
||||
return axon_get(clean);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/artifacts"))) {
|
||||
return axon_get(clean);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/projects"))) {
|
||||
return axon_get(clean);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/imprints"))) {
|
||||
return axon_get(clean);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/"))) {
|
||||
return render_studio();
|
||||
}
|
||||
return err_404(clean);
|
||||
}
|
||||
if (str_eq(method, EL_STR("POST"))) {
|
||||
if (str_eq(clean, EL_STR("/imprint/contextual"))) {
|
||||
return route_imprint_contextual(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/imprint/user"))) {
|
||||
return route_imprint_user(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/synthesize"))) {
|
||||
return route_synthesize(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/elp/chat"))) {
|
||||
return handle_elp_chat(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/chat"))) {
|
||||
el_val_t agentic_flag = json_get_bool(body, EL_STR("agentic"));
|
||||
el_val_t reply = ({ el_val_t _if_result_9 = 0; if (agentic_flag) { _if_result_9 = (handle_chat_agentic(body)); } else { _if_result_9 = (handle_chat(body)); } _if_result_9; });
|
||||
auto_persist(body, reply);
|
||||
return reply;
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/see"))) {
|
||||
return handle_see(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/conversations"))) {
|
||||
return handle_conversations(method);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/config"))) {
|
||||
return handle_config(method, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/tools/"))) {
|
||||
return handle_tool(clean, method, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/dharma"))) {
|
||||
return handle_dharma(clean, method, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/nlg"))) {
|
||||
return handle_nlg(clean, method, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/memories"))) {
|
||||
return axon_post(clean, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/knowledge"))) {
|
||||
return axon_post(clean, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/backlog"))) {
|
||||
return axon_post(clean, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/artifacts"))) {
|
||||
return axon_post(clean, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/projects"))) {
|
||||
return axon_post(clean, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/imprints"))) {
|
||||
return axon_post(clean, body);
|
||||
}
|
||||
return err_404(clean);
|
||||
}
|
||||
return err_405(method, clean);
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user