From 9bbb4f2af8dafd03f429912459392e19a5bed9e2 Mon Sep 17 00:00:00 2001 From: "will.anderson" Date: Sat, 1 Aug 2026 11:37:04 -0500 Subject: [PATCH] Bound beginSession/compileCtx payloads to a compact digest Port the payload-bounding fix (PR #102, commit 872120c) onto main. The session-init endpoints projected unbounded engram nodes (~900KB), closing the MCP client socket on every beginSession. Cap the lists (8 activated / 10 recent for begin_session, 10/20 for compile_ctx) and project each node to a light identity + a bounded, UTF-8-safe content snippet via api_compact_node / api_compact_activated / api_utf8_trunc. Response drops ~900KB -> ~12KB; full content stays available via recall/fetch/inspectGraph. Regenerate dist/soul.c (the CI-built amalgamation) and dist/neuron-api.c from source. The soul.c regen also compiles in already-merged source the previously-committed soul.c was stale against (agent write/edit receipt fixes, #100/#101); verified via scripts/verify-soul-contract.sh (PRESENCE + IMMUTABILITY PASS) and a clean CI-style cc build. --- dist/neuron-api.c | 196 ++++++++++---- dist/soul.c | 661 +++++++++++++++++++++++++++------------------- neuron-api.el | 143 +++++++++- 3 files changed, 652 insertions(+), 348 deletions(-) diff --git a/dist/neuron-api.c b/dist/neuron-api.c index 1a97d4a..66dee3b 100644 --- a/dist/neuron-api.c +++ b/dist/neuron-api.c @@ -10,7 +10,6 @@ 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_tombstone(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); @@ -27,6 +26,11 @@ el_val_t api_ok(el_val_t extra); el_val_t api_err(el_val_t msg); el_val_t api_nonempty(el_val_t s); el_val_t api_or_empty(el_val_t s); +el_val_t api_num_or_zero(el_val_t obj, el_val_t key); +el_val_t api_utf8_trunc(el_val_t s, el_val_t n); +el_val_t api_compact_node(el_val_t node, el_val_t snip); +el_val_t api_compact_node_array(el_val_t raw, el_val_t max_items, el_val_t snip); +el_val_t api_compact_activated(el_val_t raw, el_val_t max_items, el_val_t snip); el_val_t api_persisted(el_val_t id); el_val_t api_not_persisted(el_val_t id); el_val_t tombstone_node(el_val_t id); @@ -179,6 +183,80 @@ el_val_t api_or_empty(el_val_t s) { return 0; } +el_val_t api_num_or_zero(el_val_t obj, el_val_t key) { + el_val_t v = json_get_raw(obj, key); + if (str_eq(v, EL_STR(""))) { + return EL_STR("0"); + } + return v; + return 0; +} + +el_val_t api_utf8_trunc(el_val_t s, el_val_t n) { + if (str_len(s) <= n) { + return s; + } + el_val_t cut = n; + el_val_t scanning = 1; + while (scanning && (cut > 0)) { + el_val_t b = str_char_code(s, cut); + el_val_t is_cont = ((b >= 128) && (b < 192)); + cut = ({ el_val_t _if_result_1 = 0; if (is_cont) { _if_result_1 = ((cut - 1)); } else { _if_result_1 = (cut); } _if_result_1; }); + scanning = is_cont; + } + return str_slice(s, 0, cut); + return 0; +} + +el_val_t api_compact_node(el_val_t node, el_val_t snip) { + el_val_t id = json_get(node, EL_STR("id")); + el_val_t ntype = json_get(node, EL_STR("node_type")); + el_val_t label = json_get(node, EL_STR("label")); + el_val_t tier = json_get(node, EL_STR("tier")); + el_val_t content = json_get(node, EL_STR("content")); + el_val_t snippet = api_utf8_trunc(content, snip); + el_val_t trunc_str = ({ el_val_t _if_result_2 = 0; if ((str_len(content) > snip)) { _if_result_2 = (EL_STR("true")); } else { _if_result_2 = (EL_STR("false")); } _if_result_2; }); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), api_json_escape(id)), EL_STR("\"")), EL_STR(",\"node_type\":\"")), api_json_escape(ntype)), EL_STR("\"")), EL_STR(",\"label\":\"")), api_json_escape(label)), EL_STR("\"")), EL_STR(",\"tier\":\"")), api_json_escape(tier)), EL_STR("\"")), EL_STR(",\"importance\":")), api_num_or_zero(node, EL_STR("importance"))), EL_STR(",\"salience\":")), api_num_or_zero(node, EL_STR("salience"))), EL_STR(",\"content\":\"")), api_json_escape(snippet)), EL_STR("\"")), EL_STR(",\"content_truncated\":")), trunc_str), EL_STR("}")); + return 0; +} + +el_val_t api_compact_node_array(el_val_t raw, el_val_t max_items, el_val_t snip) { + if (!api_nonempty(raw)) { + return EL_STR("[]"); + } + el_val_t n = json_array_len(raw); + el_val_t cap = ({ el_val_t _if_result_3 = 0; if ((n < max_items)) { _if_result_3 = (n); } else { _if_result_3 = (max_items); } _if_result_3; }); + el_val_t out = EL_STR("["); + el_val_t i = 0; + while (i < cap) { + el_val_t node = json_array_get(raw, i); + el_val_t sep = ({ el_val_t _if_result_4 = 0; if ((i == 0)) { _if_result_4 = (EL_STR("")); } else { _if_result_4 = (EL_STR(",")); } _if_result_4; }); + out = el_str_concat(el_str_concat(out, sep), api_compact_node(node, snip)); + i = (i + 1); + } + return el_str_concat(out, EL_STR("]")); + return 0; +} + +el_val_t api_compact_activated(el_val_t raw, el_val_t max_items, el_val_t snip) { + if (!api_nonempty(raw)) { + return EL_STR("[]"); + } + el_val_t n = json_array_len(raw); + el_val_t cap = ({ el_val_t _if_result_5 = 0; if ((n < max_items)) { _if_result_5 = (n); } else { _if_result_5 = (max_items); } _if_result_5; }); + el_val_t out = EL_STR("["); + el_val_t i = 0; + while (i < cap) { + el_val_t el = json_array_get(raw, i); + el_val_t node = json_get_raw(el, EL_STR("node")); + el_val_t sep = ({ el_val_t _if_result_6 = 0; if ((i == 0)) { _if_result_6 = (EL_STR("")); } else { _if_result_6 = (EL_STR(",")); } _if_result_6; }); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, sep), EL_STR("{\"node\":")), api_compact_node(node, snip)), EL_STR(",\"activation_strength\":")), api_num_or_zero(el, EL_STR("activation_strength"))), EL_STR(",\"working_memory_weight\":")), api_num_or_zero(el, EL_STR("working_memory_weight"))), EL_STR(",\"epistemic_confidence\":")), api_num_or_zero(el, EL_STR("epistemic_confidence"))), EL_STR(",\"hops\":")), api_num_or_zero(el, EL_STR("hops"))), EL_STR(",\"promoted\":")), api_num_or_zero(el, EL_STR("promoted"))), EL_STR("}")); + i = (i + 1); + } + return el_str_concat(out, EL_STR("]")); + return 0; +} + el_val_t api_persisted(el_val_t id) { if (str_eq(id, EL_STR(""))) { return 0; @@ -209,7 +287,7 @@ el_val_t tombstoned_id_set(void) { while (i < n) { el_val_t m = json_array_get(markers, i); el_val_t tid = json_get(m, EL_STR("content")); - acc = ({ el_val_t _if_result_1 = 0; if (str_eq(tid, EL_STR(""))) { _if_result_1 = (acc); } else { _if_result_1 = (el_str_concat(el_str_concat(acc, tid), EL_STR("|"))); } _if_result_1; }); + acc = ({ el_val_t _if_result_7 = 0; if (str_eq(tid, EL_STR(""))) { _if_result_7 = (acc); } else { _if_result_7 = (el_str_concat(el_str_concat(acc, tid), EL_STR("|"))); } _if_result_7; }); i = (i + 1); } return acc; @@ -240,8 +318,8 @@ el_val_t memory_hide_tombstoned(el_val_t raw, el_val_t path) { el_val_t ntype = json_get(node, EL_STR("node_type")); el_val_t is_dead = (!str_eq(nid, EL_STR("")) && str_contains(dead, el_str_concat(el_str_concat(EL_STR("|"), nid), EL_STR("|")))); el_val_t keep = (!str_eq(ntype, EL_STR("Tombstone")) && !is_dead); - out = ({ el_val_t _if_result_2 = 0; if (keep) { _if_result_2 = (({ el_val_t _if_result_3 = 0; if (first) { _if_result_3 = (el_str_concat(out, node)); } else { _if_result_3 = (el_str_concat(el_str_concat(out, EL_STR(",")), node)); } _if_result_3; })); } else { _if_result_2 = (out); } _if_result_2; }); - first = ({ el_val_t _if_result_4 = 0; if (keep) { _if_result_4 = (0); } else { _if_result_4 = (first); } _if_result_4; }); + out = ({ el_val_t _if_result_8 = 0; if (keep) { _if_result_8 = (({ el_val_t _if_result_9 = 0; if (first) { _if_result_9 = (el_str_concat(out, node)); } else { _if_result_9 = (el_str_concat(el_str_concat(out, EL_STR(",")), node)); } _if_result_9; })); } else { _if_result_8 = (out); } _if_result_8; }); + first = ({ el_val_t _if_result_10 = 0; if (keep) { _if_result_10 = (0); } else { _if_result_10 = (first); } _if_result_10; }); i = (i + 1); } return el_str_concat(out, EL_STR("]")); @@ -250,19 +328,23 @@ el_val_t memory_hide_tombstoned(el_val_t raw, el_val_t path) { el_val_t handle_api_begin_session(el_val_t body) { el_val_t stats = engram_stats_json(); - el_val_t activated = engram_activate_json(EL_STR("session start recent memory important"), 2); - el_val_t self_nbrs = engram_neighbors_json(EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee"), 1, EL_STR("both")); - el_val_t state_events = engram_scan_nodes_by_type_json(EL_STR("InternalStateEvent"), 5, 0); - el_val_t recent = engram_scan_nodes_json(10, 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_concat(el_str_concat(el_str_concat(EL_STR("{\"stats\":"), stats), EL_STR(",\"recent\":")), api_or_empty(recent)), EL_STR(",\"activated\":")), api_or_empty(activated)), EL_STR(",\"self_neighbors\":")), api_or_empty(self_nbrs)), EL_STR(",\"recent_state_events\":")), api_or_empty(state_events)), EL_STR("}")); + el_val_t activated_raw = engram_activate_json(EL_STR("session start recent memory important"), 1); + el_val_t activated = api_compact_activated(activated_raw, 8, 240); + el_val_t state_events_raw = engram_scan_nodes_by_type_json(EL_STR("InternalStateEvent"), 5, 0); + el_val_t state_events = api_compact_node_array(state_events_raw, 5, 500); + el_val_t recent_raw = engram_scan_nodes_json(10, 0); + el_val_t recent = api_compact_node_array(recent_raw, 10, 240); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"stats\":"), stats), EL_STR(",\"recent\":")), recent), EL_STR(",\"activated\":")), activated), EL_STR(",\"self_neighbors\":[]")), EL_STR(",\"recent_state_events\":")), state_events), EL_STR("}")); return 0; } el_val_t handle_api_compile_ctx(el_val_t body) { el_val_t stats = engram_stats_json(); - el_val_t activated = engram_activate_json(EL_STR("active work context current task in progress"), 2); - el_val_t recent = engram_scan_nodes_json(20, 0); - return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"stats\":"), stats), EL_STR(",\"recent_nodes\":")), api_or_empty(recent)), EL_STR(",\"activated\":")), api_or_empty(activated)), EL_STR("}")); + el_val_t activated_raw = engram_activate_json(EL_STR("active work context current task in progress"), 2); + el_val_t activated = api_compact_activated(activated_raw, 10, 240); + el_val_t recent_raw = engram_scan_nodes_json(20, 0); + el_val_t recent = api_compact_node_array(recent_raw, 20, 240); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"stats\":"), stats), EL_STR(",\"recent_nodes\":")), recent), EL_STR(",\"activated\":")), activated), EL_STR("}")); return 0; } @@ -274,10 +356,10 @@ el_val_t handle_api_remember(el_val_t body) { el_val_t importance = json_get(body, EL_STR("importance")); el_val_t tags_raw = json_get(body, EL_STR("tags")); el_val_t project = json_get(body, EL_STR("project")); - el_val_t sal_str = ({ el_val_t _if_result_5 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_5 = (EL_STR("0.95")); } else { _if_result_5 = (({ el_val_t _if_result_6 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_6 = (EL_STR("0.75")); } else { _if_result_6 = (({ el_val_t _if_result_7 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_7 = (EL_STR("0.25")); } else { _if_result_7 = (EL_STR("0.50")); } _if_result_7; })); } _if_result_6; })); } _if_result_5; }); - el_val_t sal = ({ el_val_t _if_result_8 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_8 = (el_from_float(0.95)); } else { _if_result_8 = (({ el_val_t _if_result_9 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_9 = (el_from_float(0.75)); } else { _if_result_9 = (({ el_val_t _if_result_10 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_10 = (el_from_float(0.25)); } else { _if_result_10 = (el_from_float(0.5)); } _if_result_10; })); } _if_result_9; })); } _if_result_8; }); - el_val_t base_tags = ({ el_val_t _if_result_11 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_11 = (EL_STR("[\"Memory\"]")); } else { _if_result_11 = (tags_raw); } _if_result_11; }); - el_val_t final_tags = ({ el_val_t _if_result_12 = 0; if (str_eq(project, EL_STR(""))) { _if_result_12 = (base_tags); } else { el_val_t inner = str_slice(base_tags, 1, (str_len(base_tags) - 1)); _if_result_12 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",\"project:")), project), EL_STR("\"]"))); } _if_result_12; }); + el_val_t sal_str = ({ el_val_t _if_result_11 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_11 = (EL_STR("0.95")); } else { _if_result_11 = (({ el_val_t _if_result_12 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_12 = (EL_STR("0.75")); } else { _if_result_12 = (({ el_val_t _if_result_13 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_13 = (EL_STR("0.25")); } else { _if_result_13 = (EL_STR("0.50")); } _if_result_13; })); } _if_result_12; })); } _if_result_11; }); + el_val_t sal = ({ el_val_t _if_result_14 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_14 = (el_from_float(0.95)); } else { _if_result_14 = (({ el_val_t _if_result_15 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_15 = (el_from_float(0.75)); } else { _if_result_15 = (({ el_val_t _if_result_16 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_16 = (el_from_float(0.25)); } else { _if_result_16 = (el_from_float(0.5)); } _if_result_16; })); } _if_result_15; })); } _if_result_14; }); + el_val_t base_tags = ({ el_val_t _if_result_17 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_17 = (EL_STR("[\"Memory\"]")); } else { _if_result_17 = (tags_raw); } _if_result_17; }); + el_val_t final_tags = ({ el_val_t _if_result_18 = 0; if (str_eq(project, EL_STR(""))) { _if_result_18 = (base_tags); } else { el_val_t inner = str_slice(base_tags, 1, (str_len(base_tags) - 1)); _if_result_18 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",\"project:")), project), EL_STR("\"]"))); } _if_result_18; }); el_val_t id = engram_node_full(content, EL_STR("Memory"), EL_STR("memory:remembered"), el_from_float(sal), el_from_float(sal), el_from_float(0.9), EL_STR("Episodic"), final_tags); if (!api_persisted(id)) { return api_not_persisted(id); @@ -292,15 +374,15 @@ el_val_t handle_api_node_create(el_val_t body) { return api_err(EL_STR("content is required")); } el_val_t nt_raw = json_get(body, EL_STR("node_type")); - el_val_t node_type = ({ el_val_t _if_result_13 = 0; if (str_eq(nt_raw, EL_STR(""))) { _if_result_13 = (EL_STR("Memory")); } else { _if_result_13 = (nt_raw); } _if_result_13; }); + el_val_t node_type = ({ el_val_t _if_result_19 = 0; if (str_eq(nt_raw, EL_STR(""))) { _if_result_19 = (EL_STR("Memory")); } else { _if_result_19 = (nt_raw); } _if_result_19; }); el_val_t label_raw = json_get(body, EL_STR("label")); - el_val_t label = ({ el_val_t _if_result_14 = 0; if (str_eq(label_raw, EL_STR(""))) { _if_result_14 = (EL_STR("node:created")); } else { _if_result_14 = (label_raw); } _if_result_14; }); + el_val_t label = ({ el_val_t _if_result_20 = 0; if (str_eq(label_raw, EL_STR(""))) { _if_result_20 = (EL_STR("node:created")); } else { _if_result_20 = (label_raw); } _if_result_20; }); el_val_t tier_raw = json_get(body, EL_STR("tier")); - el_val_t tier = ({ el_val_t _if_result_15 = 0; if (str_eq(tier_raw, EL_STR(""))) { _if_result_15 = (EL_STR("Episodic")); } else { _if_result_15 = (tier_raw); } _if_result_15; }); + el_val_t tier = ({ el_val_t _if_result_21 = 0; if (str_eq(tier_raw, EL_STR(""))) { _if_result_21 = (EL_STR("Episodic")); } else { _if_result_21 = (tier_raw); } _if_result_21; }); el_val_t tags_raw = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_16 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_16 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_16 = (tags_raw); } _if_result_16; }); + el_val_t tags = ({ el_val_t _if_result_22 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_22 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_22 = (tags_raw); } _if_result_22; }); el_val_t importance = json_get(body, EL_STR("importance")); - el_val_t sal = ({ el_val_t _if_result_17 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_17 = (el_from_float(0.95)); } else { _if_result_17 = (({ el_val_t _if_result_18 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_18 = (el_from_float(0.75)); } else { _if_result_18 = (({ el_val_t _if_result_19 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_19 = (el_from_float(0.25)); } else { _if_result_19 = (el_from_float(0.5)); } _if_result_19; })); } _if_result_18; })); } _if_result_17; }); + el_val_t sal = ({ el_val_t _if_result_23 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_23 = (el_from_float(0.95)); } else { _if_result_23 = (({ el_val_t _if_result_24 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_24 = (el_from_float(0.75)); } else { _if_result_24 = (({ el_val_t _if_result_25 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_25 = (el_from_float(0.25)); } else { _if_result_25 = (el_from_float(0.5)); } _if_result_25; })); } _if_result_24; })); } _if_result_23; }); el_val_t id = engram_node_full(content, node_type, label, el_from_float(sal), el_from_float(sal), el_from_float(0.9), tier, tags); if (!api_persisted(id)) { return api_not_persisted(id); @@ -339,18 +421,18 @@ el_val_t handle_api_node_update(el_val_t body) { } el_val_t old = engram_get_node_json(id); el_val_t body_content = json_get(body, EL_STR("content")); - el_val_t content = ({ el_val_t _if_result_20 = 0; if (str_eq(body_content, EL_STR(""))) { _if_result_20 = (json_get(old, EL_STR("content"))); } else { _if_result_20 = (body_content); } _if_result_20; }); + el_val_t content = ({ el_val_t _if_result_26 = 0; if (str_eq(body_content, EL_STR(""))) { _if_result_26 = (json_get(old, EL_STR("content"))); } else { _if_result_26 = (body_content); } _if_result_26; }); el_val_t body_nt = json_get(body, EL_STR("node_type")); el_val_t old_nt = json_get(old, EL_STR("node_type")); - el_val_t node_type = ({ el_val_t _if_result_21 = 0; if (!str_eq(body_nt, EL_STR(""))) { _if_result_21 = (body_nt); } else { _if_result_21 = (({ el_val_t _if_result_22 = 0; if (!str_eq(old_nt, EL_STR(""))) { _if_result_22 = (old_nt); } else { _if_result_22 = (EL_STR("Memory")); } _if_result_22; })); } _if_result_21; }); + el_val_t node_type = ({ el_val_t _if_result_27 = 0; if (!str_eq(body_nt, EL_STR(""))) { _if_result_27 = (body_nt); } else { _if_result_27 = (({ el_val_t _if_result_28 = 0; if (!str_eq(old_nt, EL_STR(""))) { _if_result_28 = (old_nt); } else { _if_result_28 = (EL_STR("Memory")); } _if_result_28; })); } _if_result_27; }); el_val_t body_label = json_get(body, EL_STR("label")); el_val_t old_label = json_get(old, EL_STR("label")); - el_val_t label = ({ el_val_t _if_result_23 = 0; if (!str_eq(body_label, EL_STR(""))) { _if_result_23 = (body_label); } else { _if_result_23 = (({ el_val_t _if_result_24 = 0; if (!str_eq(old_label, EL_STR(""))) { _if_result_24 = (old_label); } else { _if_result_24 = (EL_STR("node:updated")); } _if_result_24; })); } _if_result_23; }); + el_val_t label = ({ el_val_t _if_result_29 = 0; if (!str_eq(body_label, EL_STR(""))) { _if_result_29 = (body_label); } else { _if_result_29 = (({ el_val_t _if_result_30 = 0; if (!str_eq(old_label, EL_STR(""))) { _if_result_30 = (old_label); } else { _if_result_30 = (EL_STR("node:updated")); } _if_result_30; })); } _if_result_29; }); el_val_t body_tier = json_get(body, EL_STR("tier")); el_val_t old_tier = json_get(old, EL_STR("tier")); - el_val_t tier = ({ el_val_t _if_result_25 = 0; if (!str_eq(body_tier, EL_STR(""))) { _if_result_25 = (body_tier); } else { _if_result_25 = (({ el_val_t _if_result_26 = 0; if (!str_eq(old_tier, EL_STR(""))) { _if_result_26 = (old_tier); } else { _if_result_26 = (EL_STR("Episodic")); } _if_result_26; })); } _if_result_25; }); + el_val_t tier = ({ el_val_t _if_result_31 = 0; if (!str_eq(body_tier, EL_STR(""))) { _if_result_31 = (body_tier); } else { _if_result_31 = (({ el_val_t _if_result_32 = 0; if (!str_eq(old_tier, EL_STR(""))) { _if_result_32 = (old_tier); } else { _if_result_32 = (EL_STR("Episodic")); } _if_result_32; })); } _if_result_31; }); el_val_t body_tags = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_27 = 0; if (str_eq(body_tags, EL_STR(""))) { _if_result_27 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_27 = (body_tags); } _if_result_27; }); + el_val_t tags = ({ el_val_t _if_result_33 = 0; if (str_eq(body_tags, EL_STR(""))) { _if_result_33 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_33 = (body_tags); } _if_result_33; }); el_val_t new_id = engram_node_full(content, node_type, label, el_from_float(0.5), el_from_float(0.5), el_from_float(0.8), tier, tags); if (!api_persisted(new_id)) { return api_not_persisted(new_id); @@ -361,15 +443,15 @@ el_val_t handle_api_node_update(el_val_t body) { } el_val_t handle_api_recall(el_val_t method, el_val_t path, el_val_t body) { - el_val_t url_q = ({ el_val_t _if_result_28 = 0; if (str_eq(api_query_param(path, EL_STR("query")), EL_STR(""))) { _if_result_28 = (api_query_param(path, EL_STR("q"))); } else { _if_result_28 = (api_query_param(path, EL_STR("query"))); } _if_result_28; }); + el_val_t url_q = ({ el_val_t _if_result_34 = 0; if (str_eq(api_query_param(path, EL_STR("query")), EL_STR(""))) { _if_result_34 = (api_query_param(path, EL_STR("q"))); } else { _if_result_34 = (api_query_param(path, EL_STR("query"))); } _if_result_34; }); el_val_t body_query = json_get(body, EL_STR("query")); el_val_t body_q = json_get(body, EL_STR("q")); - el_val_t q = ({ el_val_t _if_result_29 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_29 = (url_q); } else { _if_result_29 = (({ el_val_t _if_result_30 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_30 = (body_query); } else { _if_result_30 = (body_q); } _if_result_30; })); } _if_result_29; }); + el_val_t q = ({ el_val_t _if_result_35 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_35 = (url_q); } else { _if_result_35 = (({ el_val_t _if_result_36 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_36 = (body_query); } else { _if_result_36 = (body_q); } _if_result_36; })); } _if_result_35; }); el_val_t chain = json_get(body, EL_STR("chain_name")); el_val_t limit = api_query_int(path, EL_STR("limit"), 0); - limit = ({ el_val_t _if_result_31 = 0; if ((limit == 0)) { _if_result_31 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_31 = (limit); } _if_result_31; }); - limit = ({ el_val_t _if_result_32 = 0; if ((limit == 0)) { _if_result_32 = (10); } else { _if_result_32 = (limit); } _if_result_32; }); - el_val_t eff_q = ({ el_val_t _if_result_33 = 0; if (str_eq(q, EL_STR(""))) { _if_result_33 = (chain); } else { _if_result_33 = (q); } _if_result_33; }); + limit = ({ el_val_t _if_result_37 = 0; if ((limit == 0)) { _if_result_37 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_37 = (limit); } _if_result_37; }); + limit = ({ el_val_t _if_result_38 = 0; if ((limit == 0)) { _if_result_38 = (10); } else { _if_result_38 = (limit); } _if_result_38; }); + el_val_t eff_q = ({ el_val_t _if_result_39 = 0; if (str_eq(q, EL_STR(""))) { _if_result_39 = (chain); } else { _if_result_39 = (q); } _if_result_39; }); if (str_eq(eff_q, EL_STR(""))) { return api_or_empty(engram_scan_nodes_json(limit, 0)); } @@ -382,10 +464,10 @@ el_val_t handle_api_search_knowledge(el_val_t method, el_val_t path, el_val_t bo el_val_t url_q = api_query_param(path, EL_STR("q")); el_val_t body_query = json_get(body, EL_STR("query")); el_val_t body_q = json_get(body, EL_STR("q")); - el_val_t q = ({ el_val_t _if_result_34 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_34 = (url_q); } else { _if_result_34 = (({ el_val_t _if_result_35 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_35 = (body_query); } else { _if_result_35 = (body_q); } _if_result_35; })); } _if_result_34; }); + el_val_t q = ({ el_val_t _if_result_40 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_40 = (url_q); } else { _if_result_40 = (({ el_val_t _if_result_41 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_41 = (body_query); } else { _if_result_41 = (body_q); } _if_result_41; })); } _if_result_40; }); el_val_t limit = api_query_int(path, EL_STR("limit"), 0); - limit = ({ el_val_t _if_result_36 = 0; if ((limit == 0)) { _if_result_36 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_36 = (limit); } _if_result_36; }); - limit = ({ el_val_t _if_result_37 = 0; if ((limit == 0)) { _if_result_37 = (10); } else { _if_result_37 = (limit); } _if_result_37; }); + limit = ({ el_val_t _if_result_42 = 0; if ((limit == 0)) { _if_result_42 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_42 = (limit); } _if_result_42; }); + limit = ({ el_val_t _if_result_43 = 0; if ((limit == 0)) { _if_result_43 = (10); } else { _if_result_43 = (limit); } _if_result_43; }); if (str_eq(q, EL_STR(""))) { return api_err(EL_STR("query is required")); } @@ -413,7 +495,7 @@ el_val_t handle_api_capture_knowledge(el_val_t body) { if (str_eq(content, EL_STR(""))) { return api_err(EL_STR("content is required")); } - el_val_t full = ({ el_val_t _if_result_38 = 0; if (str_eq(title, EL_STR(""))) { _if_result_38 = (content); } else { _if_result_38 = (el_str_concat(el_str_concat(title, EL_STR(": ")), content)); } _if_result_38; }); + el_val_t full = ({ el_val_t _if_result_44 = 0; if (str_eq(title, EL_STR(""))) { _if_result_44 = (content); } else { _if_result_44 = (el_str_concat(el_str_concat(title, EL_STR(": ")), content)); } _if_result_44; }); el_val_t tags = EL_STR("[\"Knowledge\",\"captured\"]"); el_val_t id = engram_node_full(full, EL_STR("Knowledge"), EL_STR("knowledge:captured"), el_from_float(0.85), el_from_float(0.8), el_from_float(0.9), EL_STR("Episodic"), tags); if (!api_persisted(id)) { @@ -454,7 +536,7 @@ el_val_t handle_api_promote_knowledge(el_val_t body) { return api_err(EL_STR("id (prior node) is required")); } el_val_t tags_raw = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_39 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_39 = (EL_STR("[\"Knowledge\",\"tier:canonical\",\"disposition:stable\"]")); } else { _if_result_39 = (tags_raw); } _if_result_39; }); + el_val_t tags = ({ el_val_t _if_result_45 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_45 = (EL_STR("[\"Knowledge\",\"tier:canonical\",\"disposition:stable\"]")); } else { _if_result_45 = (tags_raw); } _if_result_45; }); el_val_t new_id = engram_node_full(content, EL_STR("Knowledge"), EL_STR("knowledge:canonical"), el_from_float(0.9), el_from_float(0.9), el_from_float(1.0), EL_STR("Canonical"), tags); if (!api_persisted(new_id)) { return api_not_persisted(new_id); @@ -465,7 +547,7 @@ el_val_t handle_api_promote_knowledge(el_val_t body) { } el_val_t handle_api_browse_processes(el_val_t method, el_val_t path, el_val_t body) { - el_val_t name = ({ el_val_t _if_result_40 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_40 = (api_query_param(path, EL_STR("name"))); } else { _if_result_40 = (json_get(body, EL_STR("name"))); } _if_result_40; }); + el_val_t name = ({ el_val_t _if_result_46 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_46 = (api_query_param(path, EL_STR("name"))); } else { _if_result_46 = (json_get(body, EL_STR("name"))); } _if_result_46; }); el_val_t limit = api_query_int(path, EL_STR("limit"), 50); if (str_eq(name, EL_STR(""))) { return api_or_empty(engram_scan_nodes_by_type_json(EL_STR("Process"), limit, 0)); @@ -480,7 +562,7 @@ el_val_t handle_api_define_process(el_val_t body) { if (str_eq(content, EL_STR(""))) { return api_err(EL_STR("content is required")); } - el_val_t label = ({ el_val_t _if_result_41 = 0; if (str_eq(name, EL_STR(""))) { _if_result_41 = (EL_STR("process:unnamed")); } else { _if_result_41 = (el_str_concat(EL_STR("process:"), name)); } _if_result_41; }); + el_val_t label = ({ el_val_t _if_result_47 = 0; if (str_eq(name, EL_STR(""))) { _if_result_47 = (EL_STR("process:unnamed")); } else { _if_result_47 = (el_str_concat(EL_STR("process:"), name)); } _if_result_47; }); el_val_t tags = EL_STR("[\"Process\"]"); el_val_t id = engram_node_full(content, EL_STR("Process"), label, el_from_float(0.8), el_from_float(0.8), el_from_float(0.9), EL_STR("Canonical"), tags); if (!api_persisted(id)) { @@ -498,12 +580,12 @@ el_val_t handle_api_log_state_event(el_val_t body) { el_val_t gap = json_get(body, EL_STR("gap_direction")); el_val_t legacy = json_get(body, EL_STR("content")); el_val_t parts = EL_STR("INTERNAL STATE EVENT"); - parts = ({ el_val_t _if_result_42 = 0; if (!str_eq(trigger, EL_STR(""))) { _if_result_42 = (el_str_concat(el_str_concat(parts, EL_STR("\nTrigger: ")), trigger)); } else { _if_result_42 = (parts); } _if_result_42; }); - parts = ({ el_val_t _if_result_43 = 0; if (!str_eq(pre, EL_STR(""))) { _if_result_43 = (el_str_concat(el_str_concat(parts, EL_STR("\nPre-reasoning: ")), pre)); } else { _if_result_43 = (parts); } _if_result_43; }); - parts = ({ el_val_t _if_result_44 = 0; if (!str_eq(post, EL_STR(""))) { _if_result_44 = (el_str_concat(el_str_concat(parts, EL_STR("\nPost-reasoning: ")), post)); } else { _if_result_44 = (parts); } _if_result_44; }); - parts = ({ el_val_t _if_result_45 = 0; if (!str_eq(ratio, EL_STR(""))) { _if_result_45 = (el_str_concat(el_str_concat(parts, EL_STR("\nCompression-ratio: ")), ratio)); } else { _if_result_45 = (parts); } _if_result_45; }); - parts = ({ el_val_t _if_result_46 = 0; if (!str_eq(gap, EL_STR(""))) { _if_result_46 = (el_str_concat(el_str_concat(parts, EL_STR("\nGap-direction: ")), gap)); } else { _if_result_46 = (parts); } _if_result_46; }); - parts = ({ el_val_t _if_result_47 = 0; if (!str_eq(legacy, EL_STR(""))) { _if_result_47 = (el_str_concat(el_str_concat(parts, EL_STR("\n")), legacy)); } else { _if_result_47 = (parts); } _if_result_47; }); + parts = ({ el_val_t _if_result_48 = 0; if (!str_eq(trigger, EL_STR(""))) { _if_result_48 = (el_str_concat(el_str_concat(parts, EL_STR("\nTrigger: ")), trigger)); } else { _if_result_48 = (parts); } _if_result_48; }); + parts = ({ el_val_t _if_result_49 = 0; if (!str_eq(pre, EL_STR(""))) { _if_result_49 = (el_str_concat(el_str_concat(parts, EL_STR("\nPre-reasoning: ")), pre)); } else { _if_result_49 = (parts); } _if_result_49; }); + parts = ({ el_val_t _if_result_50 = 0; if (!str_eq(post, EL_STR(""))) { _if_result_50 = (el_str_concat(el_str_concat(parts, EL_STR("\nPost-reasoning: ")), post)); } else { _if_result_50 = (parts); } _if_result_50; }); + parts = ({ el_val_t _if_result_51 = 0; if (!str_eq(ratio, EL_STR(""))) { _if_result_51 = (el_str_concat(el_str_concat(parts, EL_STR("\nCompression-ratio: ")), ratio)); } else { _if_result_51 = (parts); } _if_result_51; }); + parts = ({ el_val_t _if_result_52 = 0; if (!str_eq(gap, EL_STR(""))) { _if_result_52 = (el_str_concat(el_str_concat(parts, EL_STR("\nGap-direction: ")), gap)); } else { _if_result_52 = (parts); } _if_result_52; }); + parts = ({ el_val_t _if_result_53 = 0; if (!str_eq(legacy, EL_STR(""))) { _if_result_53 = (el_str_concat(el_str_concat(parts, EL_STR("\n")), legacy)); } else { _if_result_53 = (parts); } _if_result_53; }); el_val_t ts = time_now(); el_val_t boot = state_get(EL_STR("soul_boot_count")); el_val_t tags = EL_STR("[\"internal-state\",\"InternalStateEvent\",\"pre-reasoning\"]"); @@ -516,7 +598,7 @@ el_val_t handle_api_log_state_event(el_val_t body) { } el_val_t handle_api_list_state_events(el_val_t method, el_val_t path, el_val_t body) { - el_val_t q = ({ el_val_t _if_result_48 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_48 = (api_query_param(path, EL_STR("query"))); } else { _if_result_48 = (json_get(body, EL_STR("query"))); } _if_result_48; }); + el_val_t q = ({ el_val_t _if_result_54 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_54 = (api_query_param(path, EL_STR("query"))); } else { _if_result_54 = (json_get(body, EL_STR("query"))); } _if_result_54; }); el_val_t limit = api_query_int(path, EL_STR("limit"), 20); if (!str_eq(q, EL_STR(""))) { return api_or_empty(engram_search_json(el_str_concat(EL_STR("internal state "), q), limit)); @@ -527,7 +609,7 @@ el_val_t handle_api_list_state_events(el_val_t method, el_val_t path, el_val_t b el_val_t handle_api_inspect_config(el_val_t path, el_val_t body) { el_val_t key = api_query_param(path, EL_STR("key")); - key = ({ el_val_t _if_result_49 = 0; if (str_eq(key, EL_STR(""))) { _if_result_49 = (json_get(body, EL_STR("key"))); } else { _if_result_49 = (key); } _if_result_49; }); + key = ({ el_val_t _if_result_55 = 0; if (str_eq(key, EL_STR(""))) { _if_result_55 = (json_get(body, EL_STR("key"))); } else { _if_result_55 = (key); } _if_result_55; }); if (str_eq(key, EL_STR(""))) { return EL_STR("{\"hint\":\"pass ?key=\",\"known\":[\"neuron.self.traversal_root\",\"neuron.self.values_hub\"]}"); } @@ -544,7 +626,7 @@ el_val_t handle_api_inspect_config(el_val_t path, el_val_t body) { el_val_t node = json_array_get(results, 0); el_val_t content = json_get(node, EL_STR("content")); el_val_t prefix = el_str_concat(el_str_concat(EL_STR("config:"), key), EL_STR("=")); - el_val_t value = ({ el_val_t _if_result_50 = 0; if (str_starts_with(content, prefix)) { _if_result_50 = (str_slice(content, str_len(prefix), str_len(content))); } else { _if_result_50 = (content); } _if_result_50; }); + el_val_t value = ({ el_val_t _if_result_56 = 0; if (str_starts_with(content, prefix)) { _if_result_56 = (str_slice(content, str_len(prefix), str_len(content))); } else { _if_result_56 = (content); } _if_result_56; }); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"key\":\""), key), EL_STR("\",\"value\":\"")), value), EL_STR("\"}")); return 0; } @@ -566,13 +648,13 @@ el_val_t handle_api_tune_config(el_val_t body) { } el_val_t handle_api_inspect_graph(el_val_t method, el_val_t path, el_val_t body) { - el_val_t entity_id = ({ el_val_t _if_result_51 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_51 = (api_query_param(path, EL_STR("id"))); } else { _if_result_51 = (json_get(body, EL_STR("entity_id"))); } _if_result_51; }); - el_val_t name = ({ el_val_t _if_result_52 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_52 = (api_query_param(path, EL_STR("name"))); } else { _if_result_52 = (json_get(body, EL_STR("name"))); } _if_result_52; }); + el_val_t entity_id = ({ el_val_t _if_result_57 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_57 = (api_query_param(path, EL_STR("id"))); } else { _if_result_57 = (json_get(body, EL_STR("entity_id"))); } _if_result_57; }); + el_val_t name = ({ el_val_t _if_result_58 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_58 = (api_query_param(path, EL_STR("name"))); } else { _if_result_58 = (json_get(body, EL_STR("name"))); } _if_result_58; }); el_val_t depth = api_query_int(path, EL_STR("depth"), 0); - depth = ({ el_val_t _if_result_53 = 0; if ((depth == 0)) { _if_result_53 = (json_get_int(body, EL_STR("max_depth"))); } else { _if_result_53 = (depth); } _if_result_53; }); - depth = ({ el_val_t _if_result_54 = 0; if ((depth == 0)) { _if_result_54 = (1); } else { _if_result_54 = (depth); } _if_result_54; }); + depth = ({ el_val_t _if_result_59 = 0; if ((depth == 0)) { _if_result_59 = (json_get_int(body, EL_STR("max_depth"))); } else { _if_result_59 = (depth); } _if_result_59; }); + depth = ({ el_val_t _if_result_60 = 0; if ((depth == 0)) { _if_result_60 = (1); } else { _if_result_60 = (depth); } _if_result_60; }); el_val_t resolved = entity_id; - resolved = ({ el_val_t _if_result_55 = 0; if (str_eq(resolved, EL_STR(""))) { _if_result_55 = (({ el_val_t _if_result_56 = 0; if ((str_eq(name, EL_STR("self")) || str_eq(name, EL_STR("neuron")))) { _if_result_56 = (EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee")); } else { _if_result_56 = (({ el_val_t _if_result_57 = 0; if ((str_eq(name, EL_STR("values")) || str_eq(name, EL_STR("values_hub")))) { _if_result_57 = (EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440")); } else { _if_result_57 = (EL_STR("")); } _if_result_57; })); } _if_result_56; })); } else { _if_result_55 = (resolved); } _if_result_55; }); + resolved = ({ el_val_t _if_result_61 = 0; if (str_eq(resolved, EL_STR(""))) { _if_result_61 = (({ el_val_t _if_result_62 = 0; if ((str_eq(name, EL_STR("self")) || str_eq(name, EL_STR("neuron")))) { _if_result_62 = (EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee")); } else { _if_result_62 = (({ el_val_t _if_result_63 = 0; if ((str_eq(name, EL_STR("values")) || str_eq(name, EL_STR("values_hub")))) { _if_result_63 = (EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440")); } else { _if_result_63 = (EL_STR("")); } _if_result_63; })); } _if_result_62; })); } else { _if_result_61 = (resolved); } _if_result_61; }); if (str_eq(resolved, EL_STR(""))) { return api_err(EL_STR("entity_id or name required. Known names: self, neuron, values, values_hub")); } @@ -594,7 +676,7 @@ el_val_t handle_api_link_entities(el_val_t body) { return api_err_protected(to_id); } el_val_t relation = json_get(body, EL_STR("relation")); - el_val_t eff_relation = ({ el_val_t _if_result_58 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_58 = (EL_STR("associates")); } else { _if_result_58 = (relation); } _if_result_58; }); + el_val_t eff_relation = ({ el_val_t _if_result_64 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_64 = (EL_STR("associates")); } else { _if_result_64 = (relation); } _if_result_64; }); engram_connect(from_id, to_id, el_from_float(0.5), eff_relation); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"from_id\":\""), from_id), EL_STR("\",\"to_id\":\"")), to_id), EL_STR("\",\"relation\":\"")), eff_relation), EL_STR("\"}")); return 0; @@ -623,8 +705,8 @@ el_val_t handle_api_evolve_memory(el_val_t body) { return api_err_protected(prior_id); } el_val_t importance = json_get(body, EL_STR("importance")); - el_val_t sal_str = ({ el_val_t _if_result_59 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_59 = (EL_STR("0.95")); } else { _if_result_59 = (({ el_val_t _if_result_60 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_60 = (EL_STR("0.75")); } else { _if_result_60 = (({ el_val_t _if_result_61 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_61 = (EL_STR("0.25")); } else { _if_result_61 = (EL_STR("0.50")); } _if_result_61; })); } _if_result_60; })); } _if_result_59; }); - el_val_t sal = ({ el_val_t _if_result_62 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_62 = (el_from_float(0.95)); } else { _if_result_62 = (({ el_val_t _if_result_63 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_63 = (el_from_float(0.75)); } else { _if_result_63 = (({ el_val_t _if_result_64 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_64 = (el_from_float(0.25)); } else { _if_result_64 = (el_from_float(0.5)); } _if_result_64; })); } _if_result_63; })); } _if_result_62; }); + el_val_t sal_str = ({ el_val_t _if_result_65 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_65 = (EL_STR("0.95")); } else { _if_result_65 = (({ el_val_t _if_result_66 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_66 = (EL_STR("0.75")); } else { _if_result_66 = (({ el_val_t _if_result_67 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_67 = (EL_STR("0.25")); } else { _if_result_67 = (EL_STR("0.50")); } _if_result_67; })); } _if_result_66; })); } _if_result_65; }); + el_val_t sal = ({ el_val_t _if_result_68 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_68 = (el_from_float(0.95)); } else { _if_result_68 = (({ el_val_t _if_result_69 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_69 = (el_from_float(0.75)); } else { _if_result_69 = (({ el_val_t _if_result_70 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_70 = (el_from_float(0.25)); } else { _if_result_70 = (el_from_float(0.5)); } _if_result_70; })); } _if_result_69; })); } _if_result_68; }); el_val_t tags = EL_STR("[\"Memory\",\"evolved\"]"); el_val_t new_id = engram_node_full(content, EL_STR("Memory"), EL_STR("memory:evolved"), el_from_float(sal), el_from_float(sal), el_from_float(0.9), EL_STR("Episodic"), tags); if (!str_eq(prior_id, EL_STR("")) && !str_eq(new_id, EL_STR(""))) { @@ -699,7 +781,7 @@ el_val_t handle_api_cultivate(el_val_t body) { return api_err(EL_STR("content is required")); } el_val_t importance = json_get(body, EL_STR("importance")); - el_val_t sal = ({ el_val_t _if_result_65 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_65 = (el_from_float(0.95)); } else { _if_result_65 = (({ el_val_t _if_result_66 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_66 = (el_from_float(0.75)); } else { _if_result_66 = (({ el_val_t _if_result_67 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_67 = (el_from_float(0.25)); } else { _if_result_67 = (el_from_float(0.5)); } _if_result_67; })); } _if_result_66; })); } _if_result_65; }); + el_val_t sal = ({ el_val_t _if_result_71 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_71 = (el_from_float(0.95)); } else { _if_result_71 = (({ el_val_t _if_result_72 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_72 = (el_from_float(0.75)); } else { _if_result_72 = (({ el_val_t _if_result_73 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_73 = (el_from_float(0.25)); } else { _if_result_73 = (el_from_float(0.5)); } _if_result_73; })); } _if_result_72; })); } _if_result_71; }); el_val_t tags = EL_STR("[\"Memory\",\"evolved\",\"cultivated\"]"); el_val_t new_id = engram_node_full(content, EL_STR("Memory"), EL_STR("memory:cultivated"), el_from_float(sal), el_from_float(sal), el_from_float(0.9), EL_STR("Episodic"), tags); if (!str_eq(prior_id, EL_STR("")) && !str_eq(new_id, EL_STR(""))) { @@ -725,7 +807,7 @@ el_val_t handle_api_cultivate(el_val_t body) { return api_err(EL_STR("to_id is required")); } el_val_t relation = json_get(body, EL_STR("relation")); - el_val_t eff_relation = ({ el_val_t _if_result_68 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_68 = (EL_STR("associates")); } else { _if_result_68 = (relation); } _if_result_68; }); + el_val_t eff_relation = ({ el_val_t _if_result_74 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_74 = (EL_STR("associates")); } else { _if_result_74 = (relation); } _if_result_74; }); engram_connect(from_id, to_id, el_from_float(0.5), eff_relation); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"from_id\":\""), from_id), EL_STR("\",\"to_id\":\"")), to_id), EL_STR("\",\"relation\":\"")), eff_relation), EL_STR("\",\"cultivated\":true}")); } diff --git a/dist/soul.c b/dist/soul.c index b409dea..1f1b24c 100644 --- a/dist/soul.c +++ b/dist/soul.c @@ -1133,6 +1133,11 @@ el_val_t api_ok(el_val_t extra); el_val_t api_err(el_val_t msg); el_val_t api_nonempty(el_val_t s); el_val_t api_or_empty(el_val_t s); +el_val_t api_num_or_zero(el_val_t obj, el_val_t key); +el_val_t api_utf8_trunc(el_val_t s, el_val_t n); +el_val_t api_compact_node(el_val_t node, el_val_t snip); +el_val_t api_compact_node_array(el_val_t raw, el_val_t max_items, el_val_t snip); +el_val_t api_compact_activated(el_val_t raw, el_val_t max_items, el_val_t snip); el_val_t api_persisted(el_val_t id); el_val_t api_not_persisted(el_val_t id); el_val_t tombstone_node(el_val_t id); @@ -1181,11 +1186,6 @@ el_val_t session_hist_save(el_val_t session_id, el_val_t hist); el_val_t session_update_meta_timestamp(el_val_t session_id); el_val_t session_auto_title(el_val_t session_id, el_val_t first_message); el_val_t handle_session_approve(el_val_t session_id, el_val_t body); -el_val_t init_soul_edges(void); -el_val_t load_identity_context(void); -el_val_t seed_persona_from_env(void); -el_val_t emit_session_start_event(void); -el_val_t layered_cycle(el_val_t raw_input); el_val_t flag_true(el_val_t body, el_val_t key); el_val_t rate_limit_check(el_val_t ip, el_val_t path); el_val_t strip_query(el_val_t path); @@ -27655,8 +27655,12 @@ el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input) { if (!path_within_root(path, root)) { return json_safe(EL_STR("denied: path is outside the agent workspace root")); } - fs_write(resolve_in_root(path, root), content); - return json_safe(EL_STR("{\"ok\":true}")); + el_val_t dest = resolve_in_root(path, root); + el_val_t write_ok = fs_write(dest, content); + if (write_ok == 0) { + return json_safe(el_str_concat(el_str_concat(EL_STR("{\"error\":\"write failed - nothing landed at "), dest), EL_STR("\"}"))); + } + return json_safe(el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"path\":\""), dest), EL_STR("\"}"))); } if (str_eq(tool_name, EL_STR("web_get"))) { el_val_t url = json_get(tool_input, EL_STR("url")); @@ -27727,8 +27731,17 @@ el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input) { if (str_eq(content, EL_STR(""))) { return json_safe(EL_STR("{\"error\":\"file not found\"}")); } + if (str_eq(old_text, EL_STR(""))) { + return json_safe(EL_STR("{\"error\":\"old_text is required\"}")); + } + if (!str_contains(content, old_text)) { + return json_safe(EL_STR("{\"error\":\"old_text not found in file\"}")); + } el_val_t updated = str_replace(content, old_text, new_text); - fs_write(resolved, updated); + el_val_t write_ok = fs_write(resolved, updated); + if (write_ok == 0) { + return json_safe(EL_STR("{\"error\":\"write failed\"}")); + } return json_safe(EL_STR("{\"ok\":true}")); } if (str_eq(tool_name, EL_STR("remember"))) { @@ -27858,8 +27871,15 @@ el_val_t handle_chat_agentic(el_val_t body) { return EL_STR("{\"error\":\"message required\",\"reply\":\"\"}"); } el_val_t ws_root = json_get(body, EL_STR("agent_workspace_root")); + el_val_t sess_for_root = json_get(body, EL_STR("session_id")); if (!str_eq(ws_root, EL_STR(""))) { + if (!str_eq(sess_for_root, EL_STR(""))) { + state_set(el_str_concat(EL_STR("agent_workspace_root_"), sess_for_root), ws_root); + } state_set(EL_STR("agent_workspace_root"), ws_root); + } else { + el_val_t own_root = ({ el_val_t _if_result_375 = 0; if (str_eq(sess_for_root, EL_STR(""))) { _if_result_375 = (EL_STR("")); } else { _if_result_375 = (state_get(el_str_concat(EL_STR("agent_workspace_root_"), sess_for_root))); } _if_result_375; }); + state_set(EL_STR("agent_workspace_root"), own_root); } el_val_t history = state_get(EL_STR("conv_history")); el_val_t screen_result = safety_screen(message, history); @@ -27869,23 +27889,23 @@ el_val_t handle_chat_agentic(el_val_t body) { return el_str_concat(el_str_concat(EL_STR("{\"reply\":\""), json_safe(safety_validate(EL_STR(""), EL_STR("hard_bell")))), EL_STR("\",\"model\":\"\",\"agentic\":true,\"tools_used\":[]}")); } el_val_t req_model = json_get(body, EL_STR("model")); - el_val_t model = ({ el_val_t _if_result_375 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_375 = (chat_default_model()); } else { _if_result_375 = (req_model); } _if_result_375; }); + el_val_t model = ({ el_val_t _if_result_376 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_376 = (chat_default_model()); } else { _if_result_376 = (req_model); } _if_result_376; }); el_val_t req_session = json_get(body, EL_STR("session_id")); - el_val_t session_valid = ({ el_val_t _if_result_376 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_376 = (1); } else { _if_result_376 = (session_exists(req_session)); } _if_result_376; }); + el_val_t session_valid = ({ el_val_t _if_result_377 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_377 = (1); } else { _if_result_377 = (session_exists(req_session)); } _if_result_377; }); if (!session_valid) { return el_str_concat(el_str_concat(EL_STR("{\"error\":\"session not found\",\"session_id\":\""), req_session), EL_STR("\",\"reply\":\"\"}")); } - el_val_t hist_key = ({ el_val_t _if_result_377 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_377 = (EL_STR("conv_history")); } else { _if_result_377 = (el_str_concat(EL_STR("session_hist_"), req_session)); } _if_result_377; }); + el_val_t hist_key = ({ el_val_t _if_result_378 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_378 = (EL_STR("conv_history")); } else { _if_result_378 = (el_str_concat(EL_STR("session_hist_"), req_session)); } _if_result_378; }); el_val_t agentic_hist = state_get(hist_key); - el_val_t agentic_hist_len = ({ el_val_t _if_result_378 = 0; if (str_eq(agentic_hist, EL_STR(""))) { _if_result_378 = (0); } else { _if_result_378 = (json_array_len(agentic_hist)); } _if_result_378; }); + el_val_t agentic_hist_len = ({ el_val_t _if_result_379 = 0; if (str_eq(agentic_hist, EL_STR(""))) { _if_result_379 = (0); } else { _if_result_379 = (json_array_len(agentic_hist)); } _if_result_379; }); el_val_t ag_is_cont = engram_is_continuation(message, agentic_hist_len); - el_val_t ag_last_entry = ({ el_val_t _if_result_379 = 0; if (ag_is_cont) { _if_result_379 = (json_array_get(agentic_hist, (agentic_hist_len - 1))); } else { _if_result_379 = (EL_STR("")); } _if_result_379; }); - el_val_t ag_last_content = ({ el_val_t _if_result_380 = 0; if (!str_eq(ag_last_entry, EL_STR(""))) { _if_result_380 = (json_get(ag_last_entry, EL_STR("content"))); } else { _if_result_380 = (EL_STR("")); } _if_result_380; }); - el_val_t ag_thread_snip = ({ el_val_t _if_result_381 = 0; if ((str_len(ag_last_content) > 150)) { _if_result_381 = (str_slice(ag_last_content, 0, 150)); } else { _if_result_381 = (ag_last_content); } _if_result_381; }); - el_val_t ag_seed = ({ el_val_t _if_result_382 = 0; if (!str_eq(ag_thread_snip, EL_STR(""))) { _if_result_382 = (el_str_concat(el_str_concat(ag_thread_snip, EL_STR(" ")), message)); } else { _if_result_382 = (message); } _if_result_382; }); + el_val_t ag_last_entry = ({ el_val_t _if_result_380 = 0; if (ag_is_cont) { _if_result_380 = (json_array_get(agentic_hist, (agentic_hist_len - 1))); } else { _if_result_380 = (EL_STR("")); } _if_result_380; }); + el_val_t ag_last_content = ({ el_val_t _if_result_381 = 0; if (!str_eq(ag_last_entry, EL_STR(""))) { _if_result_381 = (json_get(ag_last_entry, EL_STR("content"))); } else { _if_result_381 = (EL_STR("")); } _if_result_381; }); + el_val_t ag_thread_snip = ({ el_val_t _if_result_382 = 0; if ((str_len(ag_last_content) > 150)) { _if_result_382 = (str_slice(ag_last_content, 0, 150)); } else { _if_result_382 = (ag_last_content); } _if_result_382; }); + el_val_t ag_seed = ({ el_val_t _if_result_383 = 0; if (!str_eq(ag_thread_snip, EL_STR(""))) { _if_result_383 = (el_str_concat(el_str_concat(ag_thread_snip, EL_STR(" ")), message)); } else { _if_result_383 = (message); } _if_result_383; }); el_val_t ctx = engram_compile(ag_seed); el_val_t identity = state_get(EL_STR("soul_identity")); - el_val_t ag_session_preload = ({ el_val_t _if_result_383 = 0; if ((agentic_hist_len == 0)) { el_val_t ag_profile_nodes = engram_search_json(EL_STR("Persona soul:persona identity principal"), 8); el_val_t ag_profile_ok = (!str_eq(ag_profile_nodes, EL_STR("")) && !str_eq(ag_profile_nodes, EL_STR("[]"))); el_val_t ag_profile_nodes2 = ({ el_val_t _if_result_384 = 0; if (ag_profile_ok) { _if_result_384 = (ag_profile_nodes); } else { _if_result_384 = (engram_search_json(EL_STR("user profile preferences name"), 8)); } _if_result_384; }); el_val_t ag_work_nodes = engram_search_json(EL_STR("WorkItem status:in_progress active work"), 6); el_val_t ag_work_ok = (!str_eq(ag_work_nodes, EL_STR("")) && !str_eq(ag_work_nodes, EL_STR("[]"))); el_val_t ag_work_nodes2 = ({ el_val_t _if_result_385 = 0; if (ag_work_ok) { _if_result_385 = (ag_work_nodes); } else { _if_result_385 = (engram_search_json(EL_STR("active project task current in_progress"), 6)); } _if_result_385; }); el_val_t ag_continuity_nodes = engram_search_json(EL_STR("last-session-topic session:emotional-summary conv:history last session"), 3); el_val_t ag_continuity_ok = (!str_eq(ag_continuity_nodes, EL_STR("")) && !str_eq(ag_continuity_nodes, EL_STR("[]"))); el_val_t ag_continuity_snip = ({ el_val_t _if_result_386 = 0; if (ag_continuity_ok) { el_val_t acn0 = json_array_get(ag_continuity_nodes, 0); el_val_t acc = json_get(acn0, EL_STR("content")); _if_result_386 = (({ el_val_t _if_result_387 = 0; if ((str_len(acc) > 350)) { _if_result_387 = (str_slice(acc, 0, 350)); } else { _if_result_387 = (acc); } _if_result_387; })); } else { _if_result_386 = (EL_STR("")); } _if_result_386; }); el_val_t ag_profile_bullets = session_preload_bullets(ag_profile_nodes2, 8, 350); el_val_t ag_work_bullets = session_preload_bullets(ag_work_nodes2, 6, 350); el_val_t ag_has_profile = !str_eq(ag_profile_bullets, EL_STR("")); el_val_t ag_has_work = !str_eq(ag_work_bullets, EL_STR("")); el_val_t ag_has_cont = !str_eq(ag_continuity_snip, EL_STR("")); _if_result_383 = (({ el_val_t _if_result_388 = 0; if (((ag_has_profile || ag_has_work) || ag_has_cont)) { el_val_t p = ({ el_val_t _if_result_389 = 0; if (ag_has_profile) { _if_result_389 = (el_str_concat(el_str_concat(EL_STR("[USER CONTEXT \xe2\x80\x94 from memory]\n"), ag_profile_bullets), EL_STR("\n\n"))); } else { _if_result_389 = (EL_STR("")); } _if_result_389; }); el_val_t w = ({ el_val_t _if_result_390 = 0; if (ag_has_work) { _if_result_390 = (el_str_concat(el_str_concat(EL_STR("[ACTIVE WORK \xe2\x80\x94 from memory]\n"), ag_work_bullets), EL_STR("\n\n"))); } else { _if_result_390 = (EL_STR("")); } _if_result_390; }); el_val_t c = ({ el_val_t _if_result_391 = 0; if (ag_has_cont) { _if_result_391 = (el_str_concat(el_str_concat(EL_STR("[CONTINUING FROM LAST SESSION]\n"), ag_continuity_snip), EL_STR("\n\n"))); } else { _if_result_391 = (EL_STR("")); } _if_result_391; }); _if_result_388 = (el_str_concat(el_str_concat(el_str_concat(EL_STR("\n\n"), p), w), c)); } else { _if_result_388 = (EL_STR("")); } _if_result_388; })); } else { _if_result_383 = (EL_STR("")); } _if_result_383; }); + el_val_t ag_session_preload = ({ el_val_t _if_result_384 = 0; if ((agentic_hist_len == 0)) { el_val_t ag_profile_nodes = engram_search_json(EL_STR("Persona soul:persona identity principal"), 8); el_val_t ag_profile_ok = (!str_eq(ag_profile_nodes, EL_STR("")) && !str_eq(ag_profile_nodes, EL_STR("[]"))); el_val_t ag_profile_nodes2 = ({ el_val_t _if_result_385 = 0; if (ag_profile_ok) { _if_result_385 = (ag_profile_nodes); } else { _if_result_385 = (engram_search_json(EL_STR("user profile preferences name"), 8)); } _if_result_385; }); el_val_t ag_work_nodes = engram_search_json(EL_STR("WorkItem status:in_progress active work"), 6); el_val_t ag_work_ok = (!str_eq(ag_work_nodes, EL_STR("")) && !str_eq(ag_work_nodes, EL_STR("[]"))); el_val_t ag_work_nodes2 = ({ el_val_t _if_result_386 = 0; if (ag_work_ok) { _if_result_386 = (ag_work_nodes); } else { _if_result_386 = (engram_search_json(EL_STR("active project task current in_progress"), 6)); } _if_result_386; }); el_val_t ag_continuity_nodes = engram_search_json(EL_STR("last-session-topic session:emotional-summary conv:history last session"), 3); el_val_t ag_continuity_ok = (!str_eq(ag_continuity_nodes, EL_STR("")) && !str_eq(ag_continuity_nodes, EL_STR("[]"))); el_val_t ag_continuity_snip = ({ el_val_t _if_result_387 = 0; if (ag_continuity_ok) { el_val_t acn0 = json_array_get(ag_continuity_nodes, 0); el_val_t acc = json_get(acn0, EL_STR("content")); _if_result_387 = (({ el_val_t _if_result_388 = 0; if ((str_len(acc) > 350)) { _if_result_388 = (str_slice(acc, 0, 350)); } else { _if_result_388 = (acc); } _if_result_388; })); } else { _if_result_387 = (EL_STR("")); } _if_result_387; }); el_val_t ag_profile_bullets = session_preload_bullets(ag_profile_nodes2, 8, 350); el_val_t ag_work_bullets = session_preload_bullets(ag_work_nodes2, 6, 350); el_val_t ag_has_profile = !str_eq(ag_profile_bullets, EL_STR("")); el_val_t ag_has_work = !str_eq(ag_work_bullets, EL_STR("")); el_val_t ag_has_cont = !str_eq(ag_continuity_snip, EL_STR("")); _if_result_384 = (({ el_val_t _if_result_389 = 0; if (((ag_has_profile || ag_has_work) || ag_has_cont)) { el_val_t p = ({ el_val_t _if_result_390 = 0; if (ag_has_profile) { _if_result_390 = (el_str_concat(el_str_concat(EL_STR("[USER CONTEXT \xe2\x80\x94 from memory]\n"), ag_profile_bullets), EL_STR("\n\n"))); } else { _if_result_390 = (EL_STR("")); } _if_result_390; }); el_val_t w = ({ el_val_t _if_result_391 = 0; if (ag_has_work) { _if_result_391 = (el_str_concat(el_str_concat(EL_STR("[ACTIVE WORK \xe2\x80\x94 from memory]\n"), ag_work_bullets), EL_STR("\n\n"))); } else { _if_result_391 = (EL_STR("")); } _if_result_391; }); el_val_t c = ({ el_val_t _if_result_392 = 0; if (ag_has_cont) { _if_result_392 = (el_str_concat(el_str_concat(EL_STR("[CONTINUING FROM LAST SESSION]\n"), ag_continuity_snip), EL_STR("\n\n"))); } else { _if_result_392 = (EL_STR("")); } _if_result_392; }); _if_result_389 = (el_str_concat(el_str_concat(el_str_concat(EL_STR("\n\n"), p), w), c)); } else { _if_result_389 = (EL_STR("")); } _if_result_389; })); } else { _if_result_384 = (EL_STR("")); } _if_result_384; }); el_val_t system = el_str_concat(el_str_concat(el_str_concat(identity, EL_STR(" You have access to tools: read files, write files, browse the web, search your memory, run commands. Use them when they add genuine value. Be direct.\n\n")), ctx), ag_session_preload); el_val_t api_key = agentic_api_key(); el_val_t tools_json = agentic_tools_all(); @@ -27893,26 +27913,29 @@ el_val_t handle_chat_agentic(el_val_t body) { el_val_t safe_sys = json_safe(system); el_val_t img_b64 = json_get(body, EL_STR("image")); el_val_t img_mt_raw = json_get(body, EL_STR("image_media_type")); - el_val_t img_mt = ({ el_val_t _if_result_392 = 0; if (str_eq(img_mt_raw, EL_STR(""))) { _if_result_392 = (EL_STR("image/png")); } else { _if_result_392 = (img_mt_raw); } _if_result_392; }); - el_val_t cur_user_content = ({ el_val_t _if_result_393 = 0; if (str_eq(img_b64, EL_STR(""))) { _if_result_393 = (el_str_concat(el_str_concat(EL_STR("\""), safe_msg), EL_STR("\""))); } else { _if_result_393 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("[{\"type\":\"text\",\"text\":\""), safe_msg), EL_STR("\"},{\"type\":\"image\",\"source\":{\"type\":\"base64\",\"media_type\":\"")), img_mt), EL_STR("\",\"data\":\"")), img_b64), EL_STR("\"}}]"))); } _if_result_393; }); - el_val_t prior_messages = ({ el_val_t _if_result_394 = 0; if ((agentic_hist_len > 0)) { el_val_t inner = str_slice(agentic_hist, 1, (str_len(agentic_hist) - 1)); _if_result_394 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",{\"role\":\"user\",\"content\":")), cur_user_content), EL_STR("}]"))); } else { _if_result_394 = (el_str_concat(el_str_concat(EL_STR("[{\"role\":\"user\",\"content\":"), cur_user_content), EL_STR("}]"))); } _if_result_394; }); + el_val_t img_mt = ({ el_val_t _if_result_393 = 0; if (str_eq(img_mt_raw, EL_STR(""))) { _if_result_393 = (EL_STR("image/png")); } else { _if_result_393 = (img_mt_raw); } _if_result_393; }); + el_val_t cur_user_content = ({ el_val_t _if_result_394 = 0; if (str_eq(img_b64, EL_STR(""))) { _if_result_394 = (el_str_concat(el_str_concat(EL_STR("\""), safe_msg), EL_STR("\""))); } else { _if_result_394 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("[{\"type\":\"text\",\"text\":\""), safe_msg), EL_STR("\"},{\"type\":\"image\",\"source\":{\"type\":\"base64\",\"media_type\":\"")), img_mt), EL_STR("\",\"data\":\"")), img_b64), EL_STR("\"}}]"))); } _if_result_394; }); + el_val_t prior_messages = ({ el_val_t _if_result_395 = 0; if ((agentic_hist_len > 0)) { el_val_t inner = str_slice(agentic_hist, 1, (str_len(agentic_hist) - 1)); _if_result_395 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",{\"role\":\"user\",\"content\":")), cur_user_content), EL_STR("}]"))); } else { _if_result_395 = (el_str_concat(el_str_concat(EL_STR("[{\"role\":\"user\",\"content\":"), cur_user_content), EL_STR("}]"))); } _if_result_395; }); el_val_t messages = prior_messages; el_val_t api_url = EL_STR("https://api.anthropic.com/v1/messages"); el_val_t h = el_map_new(0); map_set(h, EL_STR("x-api-key"), api_key); map_set(h, EL_STR("anthropic-version"), EL_STR("2023-06-01")); map_set(h, EL_STR("content-type"), EL_STR("application/json")); - el_val_t session_id = ({ el_val_t _if_result_395 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_395 = (next_bridge_id()); } else { _if_result_395 = (req_session); } _if_result_395; }); + el_val_t session_id = ({ el_val_t _if_result_396 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_396 = (next_bridge_id()); } else { _if_result_396 = (req_session); } _if_result_396; }); + el_val_t req_ask_all = json_get(body, EL_STR("require_approval")); + state_set(el_str_concat(EL_STR("require_approval_"), session_id), ({ el_val_t _if_result_397 = 0; if (str_eq(req_ask_all, EL_STR("true"))) { _if_result_397 = (EL_STR("true")); } else { _if_result_397 = (EL_STR("")); } _if_result_397; })); el_val_t use_openai = (!str_eq(llm_base_url(), EL_STR("")) && str_eq(llm_wire_format(), EL_STR("openai"))); - el_val_t result = ({ el_val_t _if_result_396 = 0; if (use_openai) { _if_result_396 = (openai_chat_complete(model, llm_base_url(), agentic_api_key(), safe_sys, messages)); } else { _if_result_396 = (agentic_loop(session_id, model, safe_sys, tools_json, messages, h, EL_STR(""))); } _if_result_396; }); + el_val_t result = ({ el_val_t _if_result_398 = 0; if (use_openai) { _if_result_398 = (openai_chat_complete(model, llm_base_url(), agentic_api_key(), safe_sys, messages)); } else { _if_result_398 = (agentic_loop(session_id, model, safe_sys, tools_json, messages, h, EL_STR(""))); } _if_result_398; }); el_val_t reply_text = json_get(result, EL_STR("reply")); - el_val_t discard_hist = ({ el_val_t _if_result_397 = 0; if (!str_eq(reply_text, EL_STR(""))) { el_val_t updated = hist_append(agentic_hist, EL_STR("user"), message); el_val_t updated2 = hist_append(updated, EL_STR("assistant"), reply_text); el_val_t trimmed = ({ el_val_t _if_result_398 = 0; if ((json_array_len(updated2) > 40)) { _if_result_398 = (hist_trim(updated2)); } else { _if_result_398 = (updated2); } _if_result_398; }); (void)(state_set(hist_key, trimmed)); (void)(({ el_val_t _if_result_399 = 0; if (str_eq(hist_key, EL_STR("conv_history"))) { _if_result_399 = (conv_history_persist(trimmed)); } else { _if_result_399 = (({ el_val_t _if_result_400 = 0; if ((!str_eq(trimmed, EL_STR("")) && !str_eq(trimmed, EL_STR("[]")))) { el_val_t sess_hist_label = el_str_concat(EL_STR("conv:history:"), req_session); el_val_t sess_hist_tags = EL_STR("[\"session-history\",\"persistent\"]"); el_val_t sess_hist_id = engram_node_full(trimmed, EL_STR("Conversation"), sess_hist_label, el_from_float(0.6), el_from_float(0.7), el_from_float(0.8), EL_STR("Episodic"), sess_hist_tags); el_val_t persist_ok = ({ el_val_t _if_result_401 = 0; if (str_eq(sess_hist_id, EL_STR(""))) { (void)(println(el_str_concat(EL_STR("[chat] agentic: named session history persist failed for session="), req_session))); _if_result_401 = (0); } else { _if_result_401 = (1); } _if_result_401; }); _if_result_400 = (persist_ok); } else { _if_result_400 = (0); } _if_result_400; })); } _if_result_399; })); _if_result_397 = (1); } else { _if_result_397 = (0); } _if_result_397; }); + el_val_t discard_hist = ({ el_val_t _if_result_399 = 0; if (!str_eq(reply_text, EL_STR(""))) { el_val_t updated = hist_append(agentic_hist, EL_STR("user"), message); el_val_t updated2 = hist_append(updated, EL_STR("assistant"), reply_text); el_val_t trimmed = ({ el_val_t _if_result_400 = 0; if ((json_array_len(updated2) > 40)) { _if_result_400 = (hist_trim(updated2)); } else { _if_result_400 = (updated2); } _if_result_400; }); (void)(state_set(hist_key, trimmed)); (void)(({ el_val_t _if_result_401 = 0; if (str_eq(hist_key, EL_STR("conv_history"))) { _if_result_401 = (conv_history_persist(trimmed)); } else { _if_result_401 = (({ el_val_t _if_result_402 = 0; if ((!str_eq(trimmed, EL_STR("")) && !str_eq(trimmed, EL_STR("[]")))) { el_val_t sess_hist_label = el_str_concat(EL_STR("conv:history:"), req_session); el_val_t sess_hist_tags = EL_STR("[\"session-history\",\"persistent\"]"); el_val_t sess_hist_id = engram_node_full(trimmed, EL_STR("Conversation"), sess_hist_label, el_from_float(0.6), el_from_float(0.7), el_from_float(0.8), EL_STR("Episodic"), sess_hist_tags); el_val_t persist_ok = ({ el_val_t _if_result_403 = 0; if (str_eq(sess_hist_id, EL_STR(""))) { (void)(println(el_str_concat(EL_STR("[chat] agentic: named session history persist failed for session="), req_session))); _if_result_403 = (0); } else { _if_result_403 = (1); } _if_result_403; }); _if_result_402 = (persist_ok); } else { _if_result_402 = (0); } _if_result_402; })); } _if_result_401; })); _if_result_399 = (1); } else { _if_result_399 = (0); } _if_result_399; }); return result; return 0; } el_val_t agentic_loop(el_val_t session_id, el_val_t model, el_val_t safe_sys, el_val_t tools_json, el_val_t messages_in, el_val_t h, el_val_t tools_log_in) { el_val_t api_url = EL_STR("https://api.anthropic.com/v1/messages"); + el_val_t ask_all = (!str_eq(session_id, EL_STR("")) && str_eq(state_get(el_str_concat(EL_STR("require_approval_"), session_id)), EL_STR("true"))); el_val_t messages = messages_in; el_val_t final_text = EL_STR(""); el_val_t tools_log = tools_log_in; @@ -27936,7 +27959,7 @@ el_val_t agentic_loop(el_val_t session_id, el_val_t model, el_val_t safe_sys, el } el_val_t stop_reason = json_get(raw_resp, EL_STR("stop_reason")); el_val_t content_arr = json_get_raw(raw_resp, EL_STR("content")); - el_val_t eff_content = ({ el_val_t _if_result_402 = 0; if (str_eq(content_arr, EL_STR(""))) { _if_result_402 = (EL_STR("[]")); } else { _if_result_402 = (content_arr); } _if_result_402; }); + el_val_t eff_content = ({ el_val_t _if_result_404 = 0; if (str_eq(content_arr, EL_STR(""))) { _if_result_404 = (EL_STR("[]")); } else { _if_result_404 = (content_arr); } _if_result_404; }); el_val_t text_out = EL_STR(""); el_val_t has_tool = 0; el_val_t tool_id = EL_STR(""); @@ -27947,66 +27970,66 @@ el_val_t agentic_loop(el_val_t session_id, el_val_t model, el_val_t safe_sys, el while (ci < c_total) { el_val_t block = json_array_get(eff_content, ci); el_val_t btype = json_get(block, EL_STR("type")); - text_out = ({ el_val_t _if_result_403 = 0; if (str_eq(btype, EL_STR("text"))) { _if_result_403 = (el_str_concat(text_out, json_get(block, EL_STR("text")))); } else { _if_result_403 = (text_out); } _if_result_403; }); + text_out = ({ el_val_t _if_result_405 = 0; if (str_eq(btype, EL_STR("text"))) { _if_result_405 = (el_str_concat(text_out, json_get(block, EL_STR("text")))); } else { _if_result_405 = (text_out); } _if_result_405; }); el_val_t is_new_tool = (str_eq(btype, EL_STR("tool_use")) && !has_tool); - has_tool = ({ el_val_t _if_result_404 = 0; if (is_new_tool) { _if_result_404 = (1); } else { _if_result_404 = (has_tool); } _if_result_404; }); - tool_id = ({ el_val_t _if_result_405 = 0; if (is_new_tool) { _if_result_405 = (json_get(block, EL_STR("id"))); } else { _if_result_405 = (tool_id); } _if_result_405; }); - tool_name = ({ el_val_t _if_result_406 = 0; if (is_new_tool) { _if_result_406 = (json_get(block, EL_STR("name"))); } else { _if_result_406 = (tool_name); } _if_result_406; }); - tool_input = ({ el_val_t _if_result_407 = 0; if (is_new_tool) { _if_result_407 = (json_get_raw(block, EL_STR("input"))); } else { _if_result_407 = (tool_input); } _if_result_407; }); + has_tool = ({ el_val_t _if_result_406 = 0; if (is_new_tool) { _if_result_406 = (1); } else { _if_result_406 = (has_tool); } _if_result_406; }); + tool_id = ({ el_val_t _if_result_407 = 0; if (is_new_tool) { _if_result_407 = (json_get(block, EL_STR("id"))); } else { _if_result_407 = (tool_id); } _if_result_407; }); + tool_name = ({ el_val_t _if_result_408 = 0; if (is_new_tool) { _if_result_408 = (json_get(block, EL_STR("name"))); } else { _if_result_408 = (tool_name); } _if_result_408; }); + tool_input = ({ el_val_t _if_result_409 = 0; if (is_new_tool) { _if_result_409 = (json_get_raw(block, EL_STR("input"))); } else { _if_result_409 = (tool_input); } _if_result_409; }); ci = (ci + 1); } el_val_t is_tool_turn = (str_eq(stop_reason, EL_STR("tool_use")) && has_tool); el_val_t always_key = el_str_concat(EL_STR("always_allow_"), session_id); - el_val_t always_list = ({ el_val_t _if_result_408 = 0; if (!str_eq(session_id, EL_STR(""))) { _if_result_408 = (state_get(always_key)); } else { _if_result_408 = (EL_STR("")); } _if_result_408; }); + el_val_t always_list = ({ el_val_t _if_result_410 = 0; if (!str_eq(session_id, EL_STR(""))) { _if_result_410 = (state_get(always_key)); } else { _if_result_410 = (EL_STR("")); } _if_result_410; }); el_val_t is_always_allowed = ((!str_eq(tool_name, EL_STR("")) && !str_eq(always_list, EL_STR(""))) && str_contains(always_list, tool_name)); - el_val_t risk_tier = ({ el_val_t _if_result_409 = 0; if (is_tool_turn) { _if_result_409 = (classify_tool_risk(tool_name, tool_input)); } else { _if_result_409 = (EL_STR("")); } _if_result_409; }); - el_val_t needs_bridge = (is_tool_turn && (str_eq(risk_tier, EL_STR("escalate")) || (!is_builtin_tool(tool_name) && !is_always_allowed))); - el_val_t tool_result_raw = ({ el_val_t _if_result_410 = 0; if ((is_tool_turn && !needs_bridge)) { _if_result_410 = (dispatch_tool(tool_name, tool_input)); } else { _if_result_410 = (EL_STR("")); } _if_result_410; }); - el_val_t tool_result = ({ el_val_t _if_result_411 = 0; if ((str_len(tool_result_raw) > 6000)) { _if_result_411 = (el_str_concat(str_slice(tool_result_raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_411 = (tool_result_raw); } _if_result_411; }); + el_val_t risk_tier = ({ el_val_t _if_result_411 = 0; if (is_tool_turn) { _if_result_411 = (classify_tool_risk(tool_name, tool_input)); } else { _if_result_411 = (EL_STR("")); } _if_result_411; }); + el_val_t needs_bridge = (is_tool_turn && ((ask_all || str_eq(risk_tier, EL_STR("escalate"))) || (!is_builtin_tool(tool_name) && !is_always_allowed))); + el_val_t tool_result_raw = ({ el_val_t _if_result_412 = 0; if ((is_tool_turn && !needs_bridge)) { _if_result_412 = (dispatch_tool(tool_name, tool_input)); } else { _if_result_412 = (EL_STR("")); } _if_result_412; }); + el_val_t tool_result = ({ el_val_t _if_result_413 = 0; if ((str_len(tool_result_raw) > 6000)) { _if_result_413 = (el_str_concat(str_slice(tool_result_raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_413 = (tool_result_raw); } _if_result_413; }); el_val_t tool_msg = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"type\":\"tool_result\",\"tool_use_id\":\""), tool_id), EL_STR("\",\"content\":\"")), tool_result), EL_STR("\"}")); el_val_t tool_quoted = el_str_concat(el_str_concat(EL_STR("\""), tool_name), EL_STR("\"")); - tools_log = ({ el_val_t _if_result_412 = 0; if (has_tool) { _if_result_412 = (({ el_val_t _if_result_413 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_413 = (tool_quoted); } else { _if_result_413 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), tool_quoted)); } _if_result_413; })); } else { _if_result_412 = (tools_log); } _if_result_412; }); + tools_log = ({ el_val_t _if_result_414 = 0; if (has_tool) { _if_result_414 = (({ el_val_t _if_result_415 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_415 = (tool_quoted); } else { _if_result_415 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), tool_quoted)); } _if_result_415; })); } else { _if_result_414 = (tools_log); } _if_result_414; }); el_val_t inner = str_slice(messages, 1, (str_len(messages) - 1)); el_val_t messages_with_assistant = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",{\"role\":\"assistant\",\"content\":")), eff_content), EL_STR("}")), EL_STR("]")); el_val_t local_continue = (is_tool_turn && !needs_bridge); - messages = ({ el_val_t _if_result_414 = 0; if (local_continue) { el_val_t inner2 = str_slice(messages_with_assistant, 1, (str_len(messages_with_assistant) - 1)); _if_result_414 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner2), EL_STR(",{\"role\":\"user\",\"content\":[")), tool_msg), EL_STR("]}]"))); } else { _if_result_414 = (messages); } _if_result_414; }); + messages = ({ el_val_t _if_result_416 = 0; if (local_continue) { el_val_t inner2 = str_slice(messages_with_assistant, 1, (str_len(messages_with_assistant) - 1)); _if_result_416 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner2), EL_STR(",{\"role\":\"user\",\"content\":[")), tool_msg), EL_STR("]}]"))); } else { _if_result_416 = (messages); } _if_result_416; }); if (!str_eq(session_id, EL_STR(""))) { el_val_t prog_key = el_str_concat(EL_STR("run_progress_"), session_id); el_val_t prog_prev = state_get(prog_key); - el_val_t prog_snip = ({ el_val_t _if_result_415 = 0; if ((str_len(text_out) > 280)) { _if_result_415 = (str_slice(text_out, 0, 280)); } else { _if_result_415 = (text_out); } _if_result_415; }); + el_val_t prog_snip = ({ el_val_t _if_result_417 = 0; if ((str_len(text_out) > 280)) { _if_result_417 = (str_slice(text_out, 0, 280)); } else { _if_result_417 = (text_out); } _if_result_417; }); el_val_t prog_entry = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"i\":"), int_to_str(iteration)), EL_STR(",\"t\":\"")), json_safe(prog_snip)), EL_STR("\"")), EL_STR(",\"tool\":\"")), json_safe(tool_name)), EL_STR("\"}")); - el_val_t prog_next = ({ el_val_t _if_result_416 = 0; if (str_eq(prog_prev, EL_STR(""))) { _if_result_416 = (prog_entry); } else { _if_result_416 = (el_str_concat(el_str_concat(prog_prev, EL_STR(",")), prog_entry)); } _if_result_416; }); + el_val_t prog_next = ({ el_val_t _if_result_418 = 0; if (str_eq(prog_prev, EL_STR(""))) { _if_result_418 = (prog_entry); } else { _if_result_418 = (el_str_concat(el_str_concat(prog_prev, EL_STR(",")), prog_entry)); } _if_result_418; }); state_set(prog_key, prog_next); } - pending = ({ el_val_t _if_result_417 = 0; if (needs_bridge) { _if_result_417 = (1); } else { _if_result_417 = (pending); } _if_result_417; }); - pend_tool_id = ({ el_val_t _if_result_418 = 0; if (needs_bridge) { _if_result_418 = (tool_id); } else { _if_result_418 = (pend_tool_id); } _if_result_418; }); - pend_tool_name = ({ el_val_t _if_result_419 = 0; if (needs_bridge) { _if_result_419 = (tool_name); } else { _if_result_419 = (pend_tool_name); } _if_result_419; }); - pend_tool_input = ({ el_val_t _if_result_420 = 0; if (needs_bridge) { _if_result_420 = (tool_input); } else { _if_result_420 = (pend_tool_input); } _if_result_420; }); - pend_tool_tier = ({ el_val_t _if_result_421 = 0; if (needs_bridge) { _if_result_421 = (risk_tier); } else { _if_result_421 = (pend_tool_tier); } _if_result_421; }); - pend_narration = ({ el_val_t _if_result_422 = 0; if (needs_bridge) { _if_result_422 = (text_out); } else { _if_result_422 = (pend_narration); } _if_result_422; }); + pending = ({ el_val_t _if_result_419 = 0; if (needs_bridge) { _if_result_419 = (1); } else { _if_result_419 = (pending); } _if_result_419; }); + pend_tool_id = ({ el_val_t _if_result_420 = 0; if (needs_bridge) { _if_result_420 = (tool_id); } else { _if_result_420 = (pend_tool_id); } _if_result_420; }); + pend_tool_name = ({ el_val_t _if_result_421 = 0; if (needs_bridge) { _if_result_421 = (tool_name); } else { _if_result_421 = (pend_tool_name); } _if_result_421; }); + pend_tool_input = ({ el_val_t _if_result_422 = 0; if (needs_bridge) { _if_result_422 = (tool_input); } else { _if_result_422 = (pend_tool_input); } _if_result_422; }); + pend_tool_tier = ({ el_val_t _if_result_423 = 0; if (needs_bridge) { _if_result_423 = (risk_tier); } else { _if_result_423 = (pend_tool_tier); } _if_result_423; }); + pend_narration = ({ el_val_t _if_result_424 = 0; if (needs_bridge) { _if_result_424 = (text_out); } else { _if_result_424 = (pend_narration); } _if_result_424; }); if (needs_bridge) { bridge_save(session_id, model, safe_sys, tools_json, messages_with_assistant, tools_log, pend_tool_id); } - final_text = ({ el_val_t _if_result_423 = 0; if (!is_tool_turn) { _if_result_423 = (text_out); } else { _if_result_423 = (final_text); } _if_result_423; }); - keep_going = ({ el_val_t _if_result_424 = 0; if (local_continue) { _if_result_424 = (keep_going); } else { _if_result_424 = (0); } _if_result_424; }); + final_text = ({ el_val_t _if_result_425 = 0; if (!is_tool_turn) { _if_result_425 = (text_out); } else { _if_result_425 = (final_text); } _if_result_425; }); + keep_going = ({ el_val_t _if_result_426 = 0; if (local_continue) { _if_result_426 = (keep_going); } else { _if_result_426 = (0); } _if_result_426; }); iteration = (iteration + 1); } if (pending) { - el_val_t safe_in = ({ el_val_t _if_result_425 = 0; if (str_eq(pend_tool_input, EL_STR(""))) { _if_result_425 = (EL_STR("{}")); } else { _if_result_425 = (pend_tool_input); } _if_result_425; }); - el_val_t tools_arr = ({ el_val_t _if_result_426 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_426 = (EL_STR("[]")); } else { _if_result_426 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_426; }); + el_val_t safe_in = ({ el_val_t _if_result_427 = 0; if (str_eq(pend_tool_input, EL_STR(""))) { _if_result_427 = (EL_STR("{}")); } else { _if_result_427 = (pend_tool_input); } _if_result_427; }); + el_val_t tools_arr = ({ el_val_t _if_result_428 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_428 = (EL_STR("[]")); } else { _if_result_428 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_428; }); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"tool_pending\":true"), EL_STR(",\"session_id\":\"")), session_id), EL_STR("\"")), EL_STR(",\"call_id\":\"")), pend_tool_id), EL_STR("\"")), EL_STR(",\"tool_name\":\"")), pend_tool_name), EL_STR("\"")), EL_STR(",\"tool_input\":")), safe_in), EL_STR(",\"risk_tier\":\"")), pend_tool_tier), EL_STR("\"")), EL_STR(",\"narration\":\"")), json_safe(pend_narration)), EL_STR("\"")), EL_STR(",\"model\":\"")), model), EL_STR("\"")), EL_STR(",\"agentic\":true")), EL_STR(",\"tools_used\":")), tools_arr), EL_STR("}")); } if (str_eq(final_text, EL_STR(""))) { el_val_t hit_cap = (iteration >= 8); - el_val_t err_msg = ({ el_val_t _if_result_427 = 0; if (hit_cap) { _if_result_427 = (EL_STR("agentic loop hit the 8-iteration cap without producing a final reply - task may be too complex or a tool call is looping")); } else { _if_result_427 = (EL_STR("no response")); } _if_result_427; }); + el_val_t err_msg = ({ el_val_t _if_result_429 = 0; if (hit_cap) { _if_result_429 = (EL_STR("agentic loop hit the 8-iteration cap without producing a final reply - task may be too complex or a tool call is looping")); } else { _if_result_429 = (EL_STR("no response")); } _if_result_429; }); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"error\":\""), err_msg), EL_STR("\",\"reply\":\"\",\"iterations\":")), int_to_str(iteration)), EL_STR("}")); } el_val_t safe_text = json_safe(final_text); - el_val_t tools_arr = ({ el_val_t _if_result_428 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_428 = (EL_STR("[]")); } else { _if_result_428 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_428; }); + el_val_t tools_arr = ({ el_val_t _if_result_430 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_430 = (EL_STR("[]")); } else { _if_result_430 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_430; }); if (!str_eq(session_id, EL_STR(""))) { el_val_t done_key = el_str_concat(EL_STR("run_progress_"), session_id); el_val_t done_prev = state_get(done_key); - el_val_t done_next = ({ el_val_t _if_result_429 = 0; if (str_eq(done_prev, EL_STR(""))) { _if_result_429 = (EL_STR("{\"done\":true}")); } else { _if_result_429 = (el_str_concat(done_prev, EL_STR(",{\"done\":true}"))); } _if_result_429; }); + el_val_t done_next = ({ el_val_t _if_result_431 = 0; if (str_eq(done_prev, EL_STR(""))) { _if_result_431 = (EL_STR("{\"done\":true}")); } else { _if_result_431 = (el_str_concat(done_prev, EL_STR(",{\"done\":true}"))); } _if_result_431; }); state_set(done_key, done_next); } return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"reply\":\""), safe_text), EL_STR("\",\"model\":\"")), model), EL_STR("\",\"agentic\":true,\"tools_used\":")), tools_arr), EL_STR(",\"iterations\":")), int_to_str(iteration)), EL_STR("}")); @@ -28028,20 +28051,21 @@ el_val_t agentic_resume(el_val_t session_id, el_val_t tool_use_id, el_val_t cont if (str_eq(blob, EL_STR(""))) { return EL_STR("{\"error\":\"unknown session_id\",\"reply\":\"\"}"); } + state_set(EL_STR("agent_workspace_root"), state_get(el_str_concat(EL_STR("agent_workspace_root_"), session_id))); el_val_t model = json_get(blob, EL_STR("model")); el_val_t safe_sys = json_get(blob, EL_STR("safe_sys")); el_val_t messages = json_get_raw(blob, EL_STR("messages_raw")); - messages = ({ el_val_t _if_result_430 = 0; if (str_eq(messages, EL_STR(""))) { _if_result_430 = (json_get(blob, EL_STR("messages"))); } else { _if_result_430 = (messages); } _if_result_430; }); + messages = ({ el_val_t _if_result_432 = 0; if (str_eq(messages, EL_STR(""))) { _if_result_432 = (json_get(blob, EL_STR("messages"))); } else { _if_result_432 = (messages); } _if_result_432; }); el_val_t tools_json = json_get_raw(blob, EL_STR("tools_raw")); - tools_json = ({ el_val_t _if_result_431 = 0; if (str_eq(tools_json, EL_STR(""))) { _if_result_431 = (json_get(blob, EL_STR("tools_json"))); } else { _if_result_431 = (tools_json); } _if_result_431; }); + tools_json = ({ el_val_t _if_result_433 = 0; if (str_eq(tools_json, EL_STR(""))) { _if_result_433 = (json_get(blob, EL_STR("tools_json"))); } else { _if_result_433 = (tools_json); } _if_result_433; }); if (str_eq(messages, EL_STR("")) || str_eq(tools_json, EL_STR(""))) { return EL_STR("{\"error\":\"corrupt bridge state\",\"reply\":\"\"}"); } el_val_t tools_log = json_get(blob, EL_STR("tools_log")); el_val_t saved_use_id = json_get(blob, EL_STR("tool_use_id")); - el_val_t use_id = ({ el_val_t _if_result_432 = 0; if (str_eq(tool_use_id, EL_STR(""))) { _if_result_432 = (saved_use_id); } else { _if_result_432 = (tool_use_id); } _if_result_432; }); - el_val_t eff_use_id = ({ el_val_t _if_result_433 = 0; if (str_eq(use_id, saved_use_id)) { _if_result_433 = (use_id); } else { _if_result_433 = (saved_use_id); } _if_result_433; }); - el_val_t trimmed = ({ el_val_t _if_result_434 = 0; if ((str_len(content) > 6000)) { _if_result_434 = (el_str_concat(str_slice(content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_434 = (content); } _if_result_434; }); + el_val_t use_id = ({ el_val_t _if_result_434 = 0; if (str_eq(tool_use_id, EL_STR(""))) { _if_result_434 = (saved_use_id); } else { _if_result_434 = (tool_use_id); } _if_result_434; }); + el_val_t eff_use_id = ({ el_val_t _if_result_435 = 0; if (str_eq(use_id, saved_use_id)) { _if_result_435 = (use_id); } else { _if_result_435 = (saved_use_id); } _if_result_435; }); + el_val_t trimmed = ({ el_val_t _if_result_436 = 0; if ((str_len(content) > 6000)) { _if_result_436 = (el_str_concat(str_slice(content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_436 = (content); } _if_result_436; }); el_val_t safe_result = json_safe(trimmed); el_val_t tool_msg = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"type\":\"tool_result\",\"tool_use_id\":\""), eff_use_id), EL_STR("\",\"content\":\"")), safe_result), EL_STR("\"}")); el_val_t inner = str_slice(messages, 1, (str_len(messages) - 1)); @@ -28077,12 +28101,12 @@ el_val_t handle_chat_as_soul(el_val_t body) { } el_val_t message = json_get(body, EL_STR("message")); el_val_t transcript = json_get(body, EL_STR("transcript")); - el_val_t eff_message = ({ el_val_t _if_result_435 = 0; if (str_eq(message, EL_STR(""))) { _if_result_435 = (transcript); } else { _if_result_435 = (message); } _if_result_435; }); + el_val_t eff_message = ({ el_val_t _if_result_437 = 0; if (str_eq(message, EL_STR(""))) { _if_result_437 = (transcript); } else { _if_result_437 = (message); } _if_result_437; }); if (str_eq(eff_message, EL_STR(""))) { return el_str_concat(el_str_concat(EL_STR("{\"error\":\"message or transcript is required\",\"response\":\"\",\"speaker_slug\":\""), speaker), EL_STR("\"}")); } el_val_t req_model = json_get(body, EL_STR("model")); - el_val_t model = ({ el_val_t _if_result_436 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_436 = (chat_default_model()); } else { _if_result_436 = (req_model); } _if_result_436; }); + el_val_t model = ({ el_val_t _if_result_438 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_438 = (chat_default_model()); } else { _if_result_438 = (req_model); } _if_result_438; }); system_prompt = safety_augment_system(system_prompt, eff_message); el_val_t raw_response = llm_call_system(model, system_prompt, eff_message); el_val_t is_error = ((str_starts_with(raw_response, EL_STR("{\"error\"")) || str_starts_with(raw_response, EL_STR("{\"type\":\"error\""))) || str_contains(raw_response, EL_STR("authentication_error"))); @@ -28105,7 +28129,7 @@ el_val_t handle_dharma_room_turn(el_val_t body) { return el_str_concat(el_str_concat(EL_STR("{\"error\":\"transcript is required\",\"response\":\"\",\"cgi_id\":\""), cgi_id), EL_STR("\"}")); } el_val_t engram_ctx = engram_compile(distill_transcript(transcript)); - el_val_t system_prompt = ({ el_val_t _if_result_437 = 0; if (str_eq(engram_ctx, EL_STR(""))) { _if_result_437 = (identity); } else { _if_result_437 = (el_str_concat(el_str_concat(identity, EL_STR("\n\n[RETRIEVED MEMORY \xe2\x80\x94 compiled from your graph for this turn]\n")), engram_ctx)); } _if_result_437; }); + el_val_t system_prompt = ({ el_val_t _if_result_439 = 0; if (str_eq(engram_ctx, EL_STR(""))) { _if_result_439 = (identity); } else { _if_result_439 = (el_str_concat(el_str_concat(identity, EL_STR("\n\n[RETRIEVED MEMORY \xe2\x80\x94 compiled from your graph for this turn]\n")), engram_ctx)); } _if_result_439; }); system_prompt = safety_augment_system(system_prompt, transcript); el_val_t raw_response = llm_call_system(model, system_prompt, transcript); el_val_t is_error = ((str_starts_with(raw_response, EL_STR("{\"error\"")) || str_starts_with(raw_response, EL_STR("{\"type\":\"error\""))) || str_contains(raw_response, EL_STR("authentication_error"))); @@ -28145,7 +28169,7 @@ el_val_t handle_dharma_room_turn_agentic(el_val_t body) { map_set(h, EL_STR("x-api-key"), api_key); map_set(h, EL_STR("anthropic-version"), EL_STR("2023-06-01")); map_set(h, EL_STR("content-type"), EL_STR("application/json")); - el_val_t session_id = ({ el_val_t _if_result_438 = 0; if (str_eq(room_id, EL_STR(""))) { _if_result_438 = (el_str_concat(EL_STR("dharma:"), next_bridge_id())); } else { _if_result_438 = (el_str_concat(EL_STR("dharma:"), room_id)); } _if_result_438; }); + el_val_t session_id = ({ el_val_t _if_result_440 = 0; if (str_eq(room_id, EL_STR(""))) { _if_result_440 = (el_str_concat(EL_STR("dharma:"), next_bridge_id())); } else { _if_result_440 = (el_str_concat(EL_STR("dharma:"), room_id)); } _if_result_440; }); el_val_t loop_result = agentic_loop(session_id, model, safe_sys, tools_json, messages, h, EL_STR("")); el_val_t result_error = json_get(loop_result, EL_STR("error")); if (!str_eq(result_error, EL_STR(""))) { @@ -28160,7 +28184,7 @@ el_val_t handle_dharma_room_turn_agentic(el_val_t body) { return el_str_concat(el_str_concat(EL_STR("{\"error\":\"no response\",\"response\":\"\",\"cgi_id\":\""), cgi_id), EL_STR("\"}")); } el_val_t tools_arr = json_get_raw(loop_result, EL_STR("tools_used")); - el_val_t eff_tools = ({ el_val_t _if_result_439 = 0; if (str_eq(tools_arr, EL_STR(""))) { _if_result_439 = (EL_STR("[]")); } else { _if_result_439 = (tools_arr); } _if_result_439; }); + el_val_t eff_tools = ({ el_val_t _if_result_441 = 0; if (str_eq(tools_arr, EL_STR(""))) { _if_result_441 = (EL_STR("[]")); } else { _if_result_441 = (tools_arr); } _if_result_441; }); el_val_t safe_text = json_safe(final_text); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"response\":\""), safe_text), EL_STR("\",\"cgi_id\":\"")), cgi_id), EL_STR("\",\"tools_used\":")), eff_tools), EL_STR("}")); return 0; @@ -28171,7 +28195,7 @@ el_val_t session_summary_write(el_val_t summary_text) { return EL_STR(""); } el_val_t safe_text = str_replace(summary_text, EL_STR("\""), EL_STR("'")); - el_val_t trimmed = ({ el_val_t _if_result_440 = 0; if ((str_len(safe_text) > 800)) { _if_result_440 = (str_slice(safe_text, 0, 800)); } else { _if_result_440 = (safe_text); } _if_result_440; }); + el_val_t trimmed = ({ el_val_t _if_result_442 = 0; if ((str_len(safe_text) > 800)) { _if_result_442 = (str_slice(safe_text, 0, 800)); } else { _if_result_442 = (safe_text); } _if_result_442; }); el_val_t ts = time_now(); el_val_t ts_str = int_to_str(ts); el_val_t content = el_str_concat(el_str_concat(el_str_concat(EL_STR("[session-summary] "), trimmed), EL_STR(" | ts:")), ts_str); @@ -28202,7 +28226,7 @@ el_val_t session_summary_write_dated(el_val_t summary_text, el_val_t label) { return EL_STR(""); } el_val_t safe_text = str_replace(summary_text, EL_STR("\""), EL_STR("'")); - el_val_t trimmed = ({ el_val_t _if_result_441 = 0; if ((str_len(safe_text) > 800)) { _if_result_441 = (str_slice(safe_text, 0, 800)); } else { _if_result_441 = (safe_text); } _if_result_441; }); + el_val_t trimmed = ({ el_val_t _if_result_443 = 0; if ((str_len(safe_text) > 800)) { _if_result_443 = (str_slice(safe_text, 0, 800)); } else { _if_result_443 = (safe_text); } _if_result_443; }); el_val_t ts = time_now(); el_val_t ts_str = int_to_str(ts); el_val_t content = el_str_concat(el_str_concat(el_str_concat(EL_STR("[session-summary] "), trimmed), EL_STR(" | ts:")), ts_str); @@ -28236,8 +28260,8 @@ el_val_t session_summary_autogenerate(el_val_t hist) { el_val_t role = json_get(entry, EL_STR("role")); if (str_eq(role, EL_STR("user"))) { el_val_t msg = json_get(entry, EL_STR("content")); - el_val_t snip = ({ el_val_t _if_result_442 = 0; if ((str_len(msg) > 80)) { _if_result_442 = (str_slice(msg, 0, 80)); } else { _if_result_442 = (msg); } _if_result_442; }); - snippets = ({ el_val_t _if_result_443 = 0; if (str_eq(snippets, EL_STR(""))) { _if_result_443 = (snip); } else { _if_result_443 = (el_str_concat(el_str_concat(snippets, EL_STR("; ")), snip)); } _if_result_443; }); + el_val_t snip = ({ el_val_t _if_result_444 = 0; if ((str_len(msg) > 80)) { _if_result_444 = (str_slice(msg, 0, 80)); } else { _if_result_444 = (msg); } _if_result_444; }); + snippets = ({ el_val_t _if_result_445 = 0; if (str_eq(snippets, EL_STR(""))) { _if_result_445 = (snip); } else { _if_result_445 = (el_str_concat(el_str_concat(snippets, EL_STR("; ")), snip)); } _if_result_445; }); count = (count + 1); } i = (i + 1); @@ -28252,7 +28276,7 @@ el_val_t session_summary_autogenerate(el_val_t hist) { el_val_t auto_persist(el_val_t req, el_val_t resp) { el_val_t message = json_get(req, EL_STR("message")); el_val_t reply = json_get(resp, EL_STR("response")); - el_val_t reply2 = ({ el_val_t _if_result_444 = 0; if (str_eq(reply, EL_STR(""))) { _if_result_444 = (json_get(resp, EL_STR("reply"))); } else { _if_result_444 = (reply); } _if_result_444; }); + el_val_t reply2 = ({ el_val_t _if_result_446 = 0; if (str_eq(reply, EL_STR(""))) { _if_result_446 = (json_get(resp, EL_STR("reply"))); } else { _if_result_446 = (reply); } _if_result_446; }); if (str_eq(message, EL_STR(""))) { return EL_STR(""); } @@ -28264,42 +28288,42 @@ el_val_t auto_persist(el_val_t req, el_val_t resp) { el_val_t is_bell = !str_eq(bell_level, EL_STR("none")); el_val_t positive_level = safety_detect_positive_level(message); el_val_t is_positive = !str_eq(positive_level, EL_STR("none")); - el_val_t tags = ({ el_val_t _if_result_445 = 0; if (is_bell) { _if_result_445 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"bell:"), bell_level), EL_STR("\",\"affective\"]"))); } else { _if_result_445 = (({ el_val_t _if_result_446 = 0; if (is_positive) { _if_result_446 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"joy:"), positive_level), EL_STR("\",\"affective\"]"))); } else { _if_result_446 = (EL_STR("[\"Conversation\",\"chat\",\"timestamped\"]")); } _if_result_446; })); } _if_result_445; }); + el_val_t tags = ({ el_val_t _if_result_447 = 0; if (is_bell) { _if_result_447 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"bell:"), bell_level), EL_STR("\",\"affective\"]"))); } else { _if_result_447 = (({ el_val_t _if_result_448 = 0; if (is_positive) { _if_result_448 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"joy:"), positive_level), EL_STR("\",\"affective\"]"))); } else { _if_result_448 = (EL_STR("[\"Conversation\",\"chat\",\"timestamped\"]")); } _if_result_448; })); } _if_result_447; }); el_val_t content = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"q\":\""), safe_msg), EL_STR("\"")), EL_STR(",\"a\":\"")), safe_reply), EL_STR("\"")), EL_STR(",\"created_at\":")), ts_str), EL_STR(",\"source\":\"chat\"")), EL_STR(",\"bell\":\"")), bell_level), EL_STR("\"")), EL_STR(",\"label\":\"chat:")), ts_str), EL_STR("\"}")); el_val_t conv_node_id = engram_node_full(content, EL_STR("Conversation"), el_str_concat(EL_STR("chat:"), ts_str), el_from_float(0.6), el_from_float(0.7), el_from_float(0.8), EL_STR("Episodic"), tags); if (str_eq(conv_node_id, EL_STR(""))) { println(el_str_concat(el_str_concat(EL_STR("[chat] auto_persist: engram_node_full returned empty \xe2\x80\x94 conversation node lost (ts="), ts_str), EL_STR(")"))); } if (is_bell) { - el_val_t summary = ({ el_val_t _if_result_447 = 0; if ((str_len(message) > 120)) { _if_result_447 = (str_slice(message, 0, 120)); } else { _if_result_447 = (message); } _if_result_447; }); + el_val_t summary = ({ el_val_t _if_result_449 = 0; if ((str_len(message) > 120)) { _if_result_449 = (str_slice(message, 0, 120)); } else { _if_result_449 = (message); } _if_result_449; }); el_val_t safe_summary = str_replace(summary, EL_STR("\""), EL_STR("'")); el_val_t bell_content = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("BELL:"), bell_level), EL_STR(" | ts:")), ts_str), EL_STR(" | summary:")), safe_summary); - el_val_t sal_a = ({ el_val_t _if_result_448 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_448 = (el_from_float(0.98)); } else { _if_result_448 = (el_from_float(0.88)); } _if_result_448; }); - el_val_t sal_b = ({ el_val_t _if_result_449 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_449 = (el_from_float(0.98)); } else { _if_result_449 = (el_from_float(0.88)); } _if_result_449; }); - el_val_t sal_c = ({ el_val_t _if_result_450 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_450 = (el_from_float(1.0)); } else { _if_result_450 = (el_from_float(0.95)); } _if_result_450; }); + el_val_t sal_a = ({ el_val_t _if_result_450 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_450 = (el_from_float(0.98)); } else { _if_result_450 = (el_from_float(0.88)); } _if_result_450; }); + el_val_t sal_b = ({ el_val_t _if_result_451 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_451 = (el_from_float(0.98)); } else { _if_result_451 = (el_from_float(0.88)); } _if_result_451; }); + el_val_t sal_c = ({ el_val_t _if_result_452 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_452 = (el_from_float(1.0)); } else { _if_result_452 = (el_from_float(0.95)); } _if_result_452; }); el_val_t bell_tags = el_str_concat(el_str_concat(EL_STR("[\"safety\",\"bell\",\"bell:"), bell_level), EL_STR("\",\"affective\",\"BellEvent\"]")); el_val_t bell_ts_str = int_to_str(time_now()); el_val_t bell_label = el_str_concat(el_str_concat(el_str_concat(EL_STR("bell:"), bell_level), EL_STR(":")), bell_ts_str); el_val_t bell_node_id = engram_node_full(bell_content, EL_STR("BellEvent"), bell_label, sal_a, sal_b, sal_c, EL_STR("Episodic"), bell_tags); el_val_t sess_id = json_get(req, EL_STR("session_id")); - el_val_t bell_key = ({ el_val_t _if_result_451 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_451 = (EL_STR("session_bell_count")); } else { _if_result_451 = (el_str_concat(EL_STR("session_bell_count:"), sess_id)); } _if_result_451; }); + el_val_t bell_key = ({ el_val_t _if_result_453 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_453 = (EL_STR("session_bell_count")); } else { _if_result_453 = (el_str_concat(EL_STR("session_bell_count:"), sess_id)); } _if_result_453; }); el_val_t prior_count = state_get(bell_key); - el_val_t prior_n = ({ el_val_t _if_result_452 = 0; if (str_eq(prior_count, EL_STR(""))) { _if_result_452 = (0); } else { _if_result_452 = (str_to_int(prior_count)); } _if_result_452; }); + el_val_t prior_n = ({ el_val_t _if_result_454 = 0; if (str_eq(prior_count, EL_STR(""))) { _if_result_454 = (0); } else { _if_result_454 = (str_to_int(prior_count)); } _if_result_454; }); state_set(bell_key, int_to_str((prior_n + 1))); - el_val_t level_key = ({ el_val_t _if_result_453 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_453 = (EL_STR("session_bell_level")); } else { _if_result_453 = (el_str_concat(EL_STR("session_bell_level:"), sess_id)); } _if_result_453; }); + el_val_t level_key = ({ el_val_t _if_result_455 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_455 = (EL_STR("session_bell_level")); } else { _if_result_455 = (el_str_concat(EL_STR("session_bell_level:"), sess_id)); } _if_result_455; }); el_val_t prior_level = state_get(level_key); - el_val_t new_level = ({ el_val_t _if_result_454 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_454 = (EL_STR("hard")); } else { _if_result_454 = (({ el_val_t _if_result_455 = 0; if (str_eq(prior_level, EL_STR("hard"))) { _if_result_455 = (EL_STR("hard")); } else { _if_result_455 = (EL_STR("soft")); } _if_result_455; })); } _if_result_454; }); + el_val_t new_level = ({ el_val_t _if_result_456 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_456 = (EL_STR("hard")); } else { _if_result_456 = (({ el_val_t _if_result_457 = 0; if (str_eq(prior_level, EL_STR("hard"))) { _if_result_457 = (EL_STR("hard")); } else { _if_result_457 = (EL_STR("soft")); } _if_result_457; })); } _if_result_456; }); state_set(level_key, new_level); - el_val_t signal_key = ({ el_val_t _if_result_456 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_456 = (EL_STR("session_bell_signal")); } else { _if_result_456 = (el_str_concat(EL_STR("session_bell_signal:"), sess_id)); } _if_result_456; }); + el_val_t signal_key = ({ el_val_t _if_result_458 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_458 = (EL_STR("session_bell_signal")); } else { _if_result_458 = (el_str_concat(EL_STR("session_bell_signal:"), sess_id)); } _if_result_458; }); state_set(signal_key, safe_summary); } if (is_positive) { - el_val_t pos_summary = ({ el_val_t _if_result_457 = 0; if ((str_len(message) > 120)) { _if_result_457 = (str_slice(message, 0, 120)); } else { _if_result_457 = (message); } _if_result_457; }); + el_val_t pos_summary = ({ el_val_t _if_result_459 = 0; if ((str_len(message) > 120)) { _if_result_459 = (str_slice(message, 0, 120)); } else { _if_result_459 = (message); } _if_result_459; }); el_val_t safe_pos_sum = str_replace(pos_summary, EL_STR("\""), EL_STR("'")); el_val_t pos_content = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("POSITIVE:"), positive_level), EL_STR(" | ts:")), ts_str), EL_STR(" | summary:")), safe_pos_sum); - el_val_t pos_sal_a = ({ el_val_t _if_result_458 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_458 = (el_from_float(0.88)); } else { _if_result_458 = (el_from_float(0.75)); } _if_result_458; }); - el_val_t pos_sal_b = ({ el_val_t _if_result_459 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_459 = (el_from_float(0.88)); } else { _if_result_459 = (el_from_float(0.75)); } _if_result_459; }); - el_val_t pos_sal_c = ({ el_val_t _if_result_460 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_460 = (el_from_float(0.95)); } else { _if_result_460 = (el_from_float(0.85)); } _if_result_460; }); + el_val_t pos_sal_a = ({ el_val_t _if_result_460 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_460 = (el_from_float(0.88)); } else { _if_result_460 = (el_from_float(0.75)); } _if_result_460; }); + el_val_t pos_sal_b = ({ el_val_t _if_result_461 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_461 = (el_from_float(0.88)); } else { _if_result_461 = (el_from_float(0.75)); } _if_result_461; }); + el_val_t pos_sal_c = ({ el_val_t _if_result_462 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_462 = (el_from_float(0.95)); } else { _if_result_462 = (el_from_float(0.85)); } _if_result_462; }); el_val_t pos_tags = el_str_concat(el_str_concat(EL_STR("[\"joy\",\"positive\",\"joy:"), positive_level), EL_STR("\",\"affective\",\"PositiveEvent\"]")); el_val_t pos_ts_label = int_to_str(time_now()); el_val_t pos_label = el_str_concat(el_str_concat(el_str_concat(EL_STR("joy:"), positive_level), EL_STR(":")), pos_ts_label); @@ -28379,7 +28403,7 @@ el_val_t handle_config(el_val_t method, el_val_t body) { } } el_val_t current_model = state_get(EL_STR("soul_model")); - el_val_t display = ({ el_val_t _if_result_461 = 0; if (str_eq(current_model, EL_STR(""))) { _if_result_461 = (EL_STR("claude-opus-4-8")); } else { _if_result_461 = (current_model); } _if_result_461; }); + el_val_t display = ({ el_val_t _if_result_463 = 0; if (str_eq(current_model, EL_STR(""))) { _if_result_463 = (EL_STR("claude-opus-4-8")); } else { _if_result_463 = (current_model); } _if_result_463; }); return el_str_concat(el_str_concat(EL_STR("{\"model\":\""), display), EL_STR("\",\"ok\":true}")); return 0; } @@ -28482,7 +28506,7 @@ el_val_t handle_nlg(el_val_t path, el_val_t method, el_val_t body) { return EL_STR("{\"error\":\"POST required\"}"); } el_val_t lang_req = json_get(body, EL_STR("lang")); - el_val_t lang_code = ({ el_val_t _if_result_462 = 0; if (str_eq(lang_req, EL_STR(""))) { _if_result_462 = (EL_STR("en")); } else { _if_result_462 = (lang_req); } _if_result_462; }); + el_val_t lang_code = ({ el_val_t _if_result_464 = 0; if (str_eq(lang_req, EL_STR(""))) { _if_result_464 = (EL_STR("en")); } else { _if_result_464 = (lang_req); } _if_result_464; }); el_val_t text = generate_lang(body, lang_code); el_val_t safe = str_replace(text, EL_STR("\""), EL_STR("'")); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"text\":\""), safe), EL_STR("\",\"lang\":\"")), lang_code), EL_STR("\",\"ok\":true}")); @@ -28505,17 +28529,17 @@ el_val_t render_studio(void) { } el_val_t elp_extract_topic(el_val_t msg) { - el_val_t m1 = ({ el_val_t _if_result_463 = 0; if (str_starts_with(msg, EL_STR("What is "))) { _if_result_463 = (str_slice(msg, 8, str_len(msg))); } else { _if_result_463 = (msg); } _if_result_463; }); - el_val_t m2 = ({ el_val_t _if_result_464 = 0; if (str_starts_with(m1, EL_STR("What are "))) { _if_result_464 = (str_slice(m1, 9, str_len(m1))); } else { _if_result_464 = (m1); } _if_result_464; }); - el_val_t m3 = ({ el_val_t _if_result_465 = 0; if (str_starts_with(m2, EL_STR("Tell me about "))) { _if_result_465 = (str_slice(m2, 14, str_len(m2))); } else { _if_result_465 = (m2); } _if_result_465; }); - el_val_t m4 = ({ el_val_t _if_result_466 = 0; if (str_starts_with(m3, EL_STR("Who is "))) { _if_result_466 = (str_slice(m3, 7, str_len(m3))); } else { _if_result_466 = (m3); } _if_result_466; }); - el_val_t m5 = ({ el_val_t _if_result_467 = 0; if (str_starts_with(m4, EL_STR("Who are "))) { _if_result_467 = (str_slice(m4, 8, str_len(m4))); } else { _if_result_467 = (m4); } _if_result_467; }); - el_val_t m6 = ({ el_val_t _if_result_468 = 0; if (str_starts_with(m5, EL_STR("How do you "))) { _if_result_468 = (str_slice(m5, 11, str_len(m5))); } else { _if_result_468 = (m5); } _if_result_468; }); - el_val_t m7 = ({ el_val_t _if_result_469 = 0; if (str_starts_with(m6, EL_STR("Why "))) { _if_result_469 = (str_slice(m6, 4, str_len(m6))); } else { _if_result_469 = (m6); } _if_result_469; }); - el_val_t m8 = ({ el_val_t _if_result_470 = 0; if (str_starts_with(m7, EL_STR("Explain "))) { _if_result_470 = (str_slice(m7, 8, str_len(m7))); } else { _if_result_470 = (m7); } _if_result_470; }); + el_val_t m1 = ({ el_val_t _if_result_465 = 0; if (str_starts_with(msg, EL_STR("What is "))) { _if_result_465 = (str_slice(msg, 8, str_len(msg))); } else { _if_result_465 = (msg); } _if_result_465; }); + el_val_t m2 = ({ el_val_t _if_result_466 = 0; if (str_starts_with(m1, EL_STR("What are "))) { _if_result_466 = (str_slice(m1, 9, str_len(m1))); } else { _if_result_466 = (m1); } _if_result_466; }); + el_val_t m3 = ({ el_val_t _if_result_467 = 0; if (str_starts_with(m2, EL_STR("Tell me about "))) { _if_result_467 = (str_slice(m2, 14, str_len(m2))); } else { _if_result_467 = (m2); } _if_result_467; }); + el_val_t m4 = ({ el_val_t _if_result_468 = 0; if (str_starts_with(m3, EL_STR("Who is "))) { _if_result_468 = (str_slice(m3, 7, str_len(m3))); } else { _if_result_468 = (m3); } _if_result_468; }); + el_val_t m5 = ({ el_val_t _if_result_469 = 0; if (str_starts_with(m4, EL_STR("Who are "))) { _if_result_469 = (str_slice(m4, 8, str_len(m4))); } else { _if_result_469 = (m4); } _if_result_469; }); + el_val_t m6 = ({ el_val_t _if_result_470 = 0; if (str_starts_with(m5, EL_STR("How do you "))) { _if_result_470 = (str_slice(m5, 11, str_len(m5))); } else { _if_result_470 = (m5); } _if_result_470; }); + el_val_t m7 = ({ el_val_t _if_result_471 = 0; if (str_starts_with(m6, EL_STR("Why "))) { _if_result_471 = (str_slice(m6, 4, str_len(m6))); } else { _if_result_471 = (m6); } _if_result_471; }); + el_val_t m8 = ({ el_val_t _if_result_472 = 0; if (str_starts_with(m7, EL_STR("Explain "))) { _if_result_472 = (str_slice(m7, 8, str_len(m7))); } else { _if_result_472 = (m7); } _if_result_472; }); el_val_t last = (str_len(m8) - 1); el_val_t trail = str_slice(m8, last, str_len(m8)); - el_val_t clean = ({ el_val_t _if_result_471 = 0; if (((str_eq(trail, EL_STR("?")) || str_eq(trail, EL_STR("."))) || str_eq(trail, EL_STR("!")))) { _if_result_471 = (str_slice(m8, 0, last)); } else { _if_result_471 = (m8); } _if_result_471; }); + el_val_t clean = ({ el_val_t _if_result_473 = 0; if (((str_eq(trail, EL_STR("?")) || str_eq(trail, EL_STR("."))) || str_eq(trail, EL_STR("!")))) { _if_result_473 = (str_slice(m8, 0, last)); } else { _if_result_473 = (m8); } _if_result_473; }); return clean; return 0; } @@ -28558,7 +28582,7 @@ el_val_t handle_elp_chat(el_val_t body) { el_val_t topic = elp_extract_topic(message); el_val_t from_topic = engram_activate_json(topic, 10); el_val_t topic_ok = (!str_eq(from_topic, EL_STR("")) && !str_eq(from_topic, EL_STR("[]"))); - el_val_t candidates = ({ el_val_t _if_result_472 = 0; if (topic_ok) { _if_result_472 = (from_topic); } else { el_val_t from_msg = engram_activate_json(message, 10); el_val_t msg_ok = (!str_eq(from_msg, EL_STR("")) && !str_eq(from_msg, EL_STR("[]"))); _if_result_472 = (({ el_val_t _if_result_473 = 0; if (msg_ok) { _if_result_473 = (from_msg); } else { _if_result_473 = (engram_scan_nodes_json(5, 0)); } _if_result_473; })); } _if_result_472; }); + el_val_t candidates = ({ el_val_t _if_result_474 = 0; if (topic_ok) { _if_result_474 = (from_topic); } else { el_val_t from_msg = engram_activate_json(message, 10); el_val_t msg_ok = (!str_eq(from_msg, EL_STR("")) && !str_eq(from_msg, EL_STR("[]"))); _if_result_474 = (({ el_val_t _if_result_475 = 0; if (msg_ok) { _if_result_475 = (from_msg); } else { _if_result_475 = (engram_scan_nodes_json(5, 0)); } _if_result_475; })); } _if_result_474; }); el_val_t total = json_array_len(candidates); el_val_t fi = 0; el_val_t kept_count = 0; @@ -28571,13 +28595,13 @@ el_val_t handle_elp_chat(el_val_t body) { el_val_t imp_ok = ((!str_eq(imp_str, EL_STR("0")) && !str_eq(imp_str, EL_STR("0.0"))) && !str_eq(imp_str, EL_STR(""))); el_val_t keep_it = ((sal_ok || imp_ok) || (kept_count == 0)); if (keep_it && (kept_count < 3)) { - el_val_t sep = ({ el_val_t _if_result_474 = 0; if (str_eq(kept_json, EL_STR(""))) { _if_result_474 = (EL_STR("")); } else { _if_result_474 = (EL_STR(",")); } _if_result_474; }); + el_val_t sep = ({ el_val_t _if_result_476 = 0; if (str_eq(kept_json, EL_STR(""))) { _if_result_476 = (EL_STR("")); } else { _if_result_476 = (EL_STR(",")); } _if_result_476; }); kept_json = el_str_concat(el_str_concat(kept_json, sep), n); kept_count = (kept_count + 1); } fi = (fi + 1); } - el_val_t frame_nodes = ({ el_val_t _if_result_475 = 0; if (str_eq(kept_json, EL_STR(""))) { _if_result_475 = (EL_STR("[]")); } else { _if_result_475 = (el_str_concat(el_str_concat(EL_STR("["), kept_json), EL_STR("]"))); } _if_result_475; }); + el_val_t frame_nodes = ({ el_val_t _if_result_477 = 0; if (str_eq(kept_json, EL_STR(""))) { _if_result_477 = (EL_STR("[]")); } else { _if_result_477 = (el_str_concat(el_str_concat(EL_STR("["), kept_json), EL_STR("]"))); } _if_result_477; }); el_val_t fn_total = json_array_len(frame_nodes); el_val_t fn_i = 0; el_val_t topic_lower = str_to_lower(topic); @@ -28592,14 +28616,14 @@ el_val_t handle_elp_chat(el_val_t body) { } fn_i = (fn_i + 1); } - el_val_t top_node = ({ el_val_t _if_result_476 = 0; if (str_eq(found_node, EL_STR(""))) { _if_result_476 = (json_array_get(frame_nodes, 0)); } else { _if_result_476 = (found_node); } _if_result_476; }); + el_val_t top_node = ({ el_val_t _if_result_478 = 0; if (str_eq(found_node, EL_STR(""))) { _if_result_478 = (json_array_get(frame_nodes, 0)); } else { _if_result_478 = (found_node); } _if_result_478; }); el_val_t top_raw = json_get(top_node, EL_STR("content")); - el_val_t patient_raw = ({ el_val_t _if_result_477 = 0; if (str_eq(top_raw, EL_STR(""))) { _if_result_477 = (topic); } else { _if_result_477 = (({ el_val_t _if_result_478 = 0; if ((str_len(top_raw) > 200)) { _if_result_478 = (str_slice(top_raw, 0, 200)); } else { _if_result_478 = (top_raw); } _if_result_478; })); } _if_result_477; }); + el_val_t patient_raw = ({ el_val_t _if_result_479 = 0; if (str_eq(top_raw, EL_STR(""))) { _if_result_479 = (topic); } else { _if_result_479 = (({ el_val_t _if_result_480 = 0; if ((str_len(top_raw) > 200)) { _if_result_480 = (str_slice(top_raw, 0, 200)); } else { _if_result_480 = (top_raw); } _if_result_480; })); } _if_result_479; }); el_val_t patient_safe = str_replace(str_replace(patient_raw, EL_STR("\""), EL_STR("'")), EL_STR("\n"), EL_STR(" ")); - el_val_t intent_val = ({ el_val_t _if_result_479 = 0; if (str_eq(predicate, EL_STR("store"))) { _if_result_479 = (EL_STR("command")); } else { _if_result_479 = (EL_STR("assert")); } _if_result_479; }); + el_val_t intent_val = ({ el_val_t _if_result_481 = 0; if (str_eq(predicate, EL_STR("store"))) { _if_result_481 = (EL_STR("command")); } else { _if_result_481 = (EL_STR("assert")); } _if_result_481; }); el_val_t gen_form = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"intent\":\""), intent_val), EL_STR("\"")), EL_STR(",\"agent\":\"I\"")), EL_STR(",\"predicate\":\"")), predicate), EL_STR("\"")), EL_STR(",\"patient\":\"")), patient_safe), EL_STR("\"")), EL_STR(",\"tense\":\"present\",\"aspect\":\"simple\",\"lang\":\"en\"}")); el_val_t realized = generate(gen_form); - el_val_t response = ({ el_val_t _if_result_480 = 0; if (str_eq(realized, EL_STR(""))) { _if_result_480 = (({ el_val_t _if_result_481 = 0; if (str_eq(patient_safe, EL_STR(""))) { _if_result_481 = (EL_STR("Nothing in the engram matched that query.")); } else { _if_result_481 = (patient_safe); } _if_result_481; })); } else { _if_result_480 = (realized); } _if_result_480; }); + el_val_t response = ({ el_val_t _if_result_482 = 0; if (str_eq(realized, EL_STR(""))) { _if_result_482 = (({ el_val_t _if_result_483 = 0; if (str_eq(patient_safe, EL_STR(""))) { _if_result_483 = (EL_STR("Nothing in the engram matched that query.")); } else { _if_result_483 = (patient_safe); } _if_result_483; })); } else { _if_result_482 = (realized); } _if_result_482; }); el_val_t safe_resp = str_replace(str_replace(response, EL_STR("\""), EL_STR("'")), EL_STR("\r"), EL_STR("")); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"response\":\""), safe_resp), EL_STR("\",\"model\":\"elp-native\",\"frame\":")), frame), EL_STR(",\"nodes\":")), frame_nodes), EL_STR("}")); return 0; @@ -28724,6 +28748,80 @@ el_val_t api_or_empty(el_val_t s) { return 0; } +el_val_t api_num_or_zero(el_val_t obj, el_val_t key) { + el_val_t v = json_get_raw(obj, key); + if (str_eq(v, EL_STR(""))) { + return EL_STR("0"); + } + return v; + return 0; +} + +el_val_t api_utf8_trunc(el_val_t s, el_val_t n) { + if (str_len(s) <= n) { + return s; + } + el_val_t cut = n; + el_val_t scanning = 1; + while (scanning && (cut > 0)) { + el_val_t b = str_char_code(s, cut); + el_val_t is_cont = ((b >= 128) && (b < 192)); + cut = ({ el_val_t _if_result_484 = 0; if (is_cont) { _if_result_484 = ((cut - 1)); } else { _if_result_484 = (cut); } _if_result_484; }); + scanning = is_cont; + } + return str_slice(s, 0, cut); + return 0; +} + +el_val_t api_compact_node(el_val_t node, el_val_t snip) { + el_val_t id = json_get(node, EL_STR("id")); + el_val_t ntype = json_get(node, EL_STR("node_type")); + el_val_t label = json_get(node, EL_STR("label")); + el_val_t tier = json_get(node, EL_STR("tier")); + el_val_t content = json_get(node, EL_STR("content")); + el_val_t snippet = api_utf8_trunc(content, snip); + el_val_t trunc_str = ({ el_val_t _if_result_485 = 0; if ((str_len(content) > snip)) { _if_result_485 = (EL_STR("true")); } else { _if_result_485 = (EL_STR("false")); } _if_result_485; }); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), api_json_escape(id)), EL_STR("\"")), EL_STR(",\"node_type\":\"")), api_json_escape(ntype)), EL_STR("\"")), EL_STR(",\"label\":\"")), api_json_escape(label)), EL_STR("\"")), EL_STR(",\"tier\":\"")), api_json_escape(tier)), EL_STR("\"")), EL_STR(",\"importance\":")), api_num_or_zero(node, EL_STR("importance"))), EL_STR(",\"salience\":")), api_num_or_zero(node, EL_STR("salience"))), EL_STR(",\"content\":\"")), api_json_escape(snippet)), EL_STR("\"")), EL_STR(",\"content_truncated\":")), trunc_str), EL_STR("}")); + return 0; +} + +el_val_t api_compact_node_array(el_val_t raw, el_val_t max_items, el_val_t snip) { + if (!api_nonempty(raw)) { + return EL_STR("[]"); + } + el_val_t n = json_array_len(raw); + el_val_t cap = ({ el_val_t _if_result_486 = 0; if ((n < max_items)) { _if_result_486 = (n); } else { _if_result_486 = (max_items); } _if_result_486; }); + el_val_t out = EL_STR("["); + el_val_t i = 0; + while (i < cap) { + el_val_t node = json_array_get(raw, i); + el_val_t sep = ({ el_val_t _if_result_487 = 0; if ((i == 0)) { _if_result_487 = (EL_STR("")); } else { _if_result_487 = (EL_STR(",")); } _if_result_487; }); + out = el_str_concat(el_str_concat(out, sep), api_compact_node(node, snip)); + i = (i + 1); + } + return el_str_concat(out, EL_STR("]")); + return 0; +} + +el_val_t api_compact_activated(el_val_t raw, el_val_t max_items, el_val_t snip) { + if (!api_nonempty(raw)) { + return EL_STR("[]"); + } + el_val_t n = json_array_len(raw); + el_val_t cap = ({ el_val_t _if_result_488 = 0; if ((n < max_items)) { _if_result_488 = (n); } else { _if_result_488 = (max_items); } _if_result_488; }); + el_val_t out = EL_STR("["); + el_val_t i = 0; + while (i < cap) { + el_val_t el = json_array_get(raw, i); + el_val_t node = json_get_raw(el, EL_STR("node")); + el_val_t sep = ({ el_val_t _if_result_489 = 0; if ((i == 0)) { _if_result_489 = (EL_STR("")); } else { _if_result_489 = (EL_STR(",")); } _if_result_489; }); + out = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(out, sep), EL_STR("{\"node\":")), api_compact_node(node, snip)), EL_STR(",\"activation_strength\":")), api_num_or_zero(el, EL_STR("activation_strength"))), EL_STR(",\"working_memory_weight\":")), api_num_or_zero(el, EL_STR("working_memory_weight"))), EL_STR(",\"epistemic_confidence\":")), api_num_or_zero(el, EL_STR("epistemic_confidence"))), EL_STR(",\"hops\":")), api_num_or_zero(el, EL_STR("hops"))), EL_STR(",\"promoted\":")), api_num_or_zero(el, EL_STR("promoted"))), EL_STR("}")); + i = (i + 1); + } + return el_str_concat(out, EL_STR("]")); + return 0; +} + el_val_t api_persisted(el_val_t id) { if (str_eq(id, EL_STR(""))) { return 0; @@ -28754,7 +28852,7 @@ el_val_t tombstoned_id_set(void) { while (i < n) { el_val_t m = json_array_get(markers, i); el_val_t tid = json_get(m, EL_STR("content")); - acc = ({ el_val_t _if_result_482 = 0; if (str_eq(tid, EL_STR(""))) { _if_result_482 = (acc); } else { _if_result_482 = (el_str_concat(el_str_concat(acc, tid), EL_STR("|"))); } _if_result_482; }); + acc = ({ el_val_t _if_result_490 = 0; if (str_eq(tid, EL_STR(""))) { _if_result_490 = (acc); } else { _if_result_490 = (el_str_concat(el_str_concat(acc, tid), EL_STR("|"))); } _if_result_490; }); i = (i + 1); } return acc; @@ -28785,8 +28883,8 @@ el_val_t memory_hide_tombstoned(el_val_t raw, el_val_t path) { el_val_t ntype = json_get(node, EL_STR("node_type")); el_val_t is_dead = (!str_eq(nid, EL_STR("")) && str_contains(dead, el_str_concat(el_str_concat(EL_STR("|"), nid), EL_STR("|")))); el_val_t keep = (!str_eq(ntype, EL_STR("Tombstone")) && !is_dead); - out = ({ el_val_t _if_result_483 = 0; if (keep) { _if_result_483 = (({ el_val_t _if_result_484 = 0; if (first) { _if_result_484 = (el_str_concat(out, node)); } else { _if_result_484 = (el_str_concat(el_str_concat(out, EL_STR(",")), node)); } _if_result_484; })); } else { _if_result_483 = (out); } _if_result_483; }); - first = ({ el_val_t _if_result_485 = 0; if (keep) { _if_result_485 = (0); } else { _if_result_485 = (first); } _if_result_485; }); + out = ({ el_val_t _if_result_491 = 0; if (keep) { _if_result_491 = (({ el_val_t _if_result_492 = 0; if (first) { _if_result_492 = (el_str_concat(out, node)); } else { _if_result_492 = (el_str_concat(el_str_concat(out, EL_STR(",")), node)); } _if_result_492; })); } else { _if_result_491 = (out); } _if_result_491; }); + first = ({ el_val_t _if_result_493 = 0; if (keep) { _if_result_493 = (0); } else { _if_result_493 = (first); } _if_result_493; }); i = (i + 1); } return el_str_concat(out, EL_STR("]")); @@ -28795,19 +28893,23 @@ el_val_t memory_hide_tombstoned(el_val_t raw, el_val_t path) { el_val_t handle_api_begin_session(el_val_t body) { el_val_t stats = engram_stats_json(); - el_val_t activated = engram_activate_json(EL_STR("session start recent memory important"), 2); - el_val_t self_nbrs = engram_neighbors_json(EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee"), 1, EL_STR("both")); - el_val_t state_events = engram_scan_nodes_by_type_json(EL_STR("InternalStateEvent"), 5, 0); - el_val_t recent = engram_scan_nodes_json(10, 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_concat(el_str_concat(el_str_concat(EL_STR("{\"stats\":"), stats), EL_STR(",\"recent\":")), api_or_empty(recent)), EL_STR(",\"activated\":")), api_or_empty(activated)), EL_STR(",\"self_neighbors\":")), api_or_empty(self_nbrs)), EL_STR(",\"recent_state_events\":")), api_or_empty(state_events)), EL_STR("}")); + el_val_t activated_raw = engram_activate_json(EL_STR("session start recent memory important"), 1); + el_val_t activated = api_compact_activated(activated_raw, 8, 240); + el_val_t state_events_raw = engram_scan_nodes_by_type_json(EL_STR("InternalStateEvent"), 5, 0); + el_val_t state_events = api_compact_node_array(state_events_raw, 5, 500); + el_val_t recent_raw = engram_scan_nodes_json(10, 0); + el_val_t recent = api_compact_node_array(recent_raw, 10, 240); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"stats\":"), stats), EL_STR(",\"recent\":")), recent), EL_STR(",\"activated\":")), activated), EL_STR(",\"self_neighbors\":[]")), EL_STR(",\"recent_state_events\":")), state_events), EL_STR("}")); return 0; } el_val_t handle_api_compile_ctx(el_val_t body) { el_val_t stats = engram_stats_json(); - el_val_t activated = engram_activate_json(EL_STR("active work context current task in progress"), 2); - el_val_t recent = engram_scan_nodes_json(20, 0); - return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"stats\":"), stats), EL_STR(",\"recent_nodes\":")), api_or_empty(recent)), EL_STR(",\"activated\":")), api_or_empty(activated)), EL_STR("}")); + el_val_t activated_raw = engram_activate_json(EL_STR("active work context current task in progress"), 2); + el_val_t activated = api_compact_activated(activated_raw, 10, 240); + el_val_t recent_raw = engram_scan_nodes_json(20, 0); + el_val_t recent = api_compact_node_array(recent_raw, 20, 240); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"stats\":"), stats), EL_STR(",\"recent_nodes\":")), recent), EL_STR(",\"activated\":")), activated), EL_STR("}")); return 0; } @@ -28819,10 +28921,10 @@ el_val_t handle_api_remember(el_val_t body) { el_val_t importance = json_get(body, EL_STR("importance")); el_val_t tags_raw = json_get(body, EL_STR("tags")); el_val_t project = json_get(body, EL_STR("project")); - el_val_t sal_str = ({ el_val_t _if_result_486 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_486 = (EL_STR("0.95")); } else { _if_result_486 = (({ el_val_t _if_result_487 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_487 = (EL_STR("0.75")); } else { _if_result_487 = (({ el_val_t _if_result_488 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_488 = (EL_STR("0.25")); } else { _if_result_488 = (EL_STR("0.50")); } _if_result_488; })); } _if_result_487; })); } _if_result_486; }); - el_val_t sal = ({ el_val_t _if_result_489 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_489 = (el_from_float(0.95)); } else { _if_result_489 = (({ el_val_t _if_result_490 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_490 = (el_from_float(0.75)); } else { _if_result_490 = (({ el_val_t _if_result_491 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_491 = (el_from_float(0.25)); } else { _if_result_491 = (el_from_float(0.5)); } _if_result_491; })); } _if_result_490; })); } _if_result_489; }); - el_val_t base_tags = ({ el_val_t _if_result_492 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_492 = (EL_STR("[\"Memory\"]")); } else { _if_result_492 = (tags_raw); } _if_result_492; }); - el_val_t final_tags = ({ el_val_t _if_result_493 = 0; if (str_eq(project, EL_STR(""))) { _if_result_493 = (base_tags); } else { el_val_t inner = str_slice(base_tags, 1, (str_len(base_tags) - 1)); _if_result_493 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",\"project:")), project), EL_STR("\"]"))); } _if_result_493; }); + el_val_t sal_str = ({ el_val_t _if_result_494 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_494 = (EL_STR("0.95")); } else { _if_result_494 = (({ el_val_t _if_result_495 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_495 = (EL_STR("0.75")); } else { _if_result_495 = (({ el_val_t _if_result_496 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_496 = (EL_STR("0.25")); } else { _if_result_496 = (EL_STR("0.50")); } _if_result_496; })); } _if_result_495; })); } _if_result_494; }); + el_val_t sal = ({ el_val_t _if_result_497 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_497 = (el_from_float(0.95)); } else { _if_result_497 = (({ el_val_t _if_result_498 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_498 = (el_from_float(0.75)); } else { _if_result_498 = (({ el_val_t _if_result_499 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_499 = (el_from_float(0.25)); } else { _if_result_499 = (el_from_float(0.5)); } _if_result_499; })); } _if_result_498; })); } _if_result_497; }); + el_val_t base_tags = ({ el_val_t _if_result_500 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_500 = (EL_STR("[\"Memory\"]")); } else { _if_result_500 = (tags_raw); } _if_result_500; }); + el_val_t final_tags = ({ el_val_t _if_result_501 = 0; if (str_eq(project, EL_STR(""))) { _if_result_501 = (base_tags); } else { el_val_t inner = str_slice(base_tags, 1, (str_len(base_tags) - 1)); _if_result_501 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",\"project:")), project), EL_STR("\"]"))); } _if_result_501; }); el_val_t id = engram_node_full(content, EL_STR("Memory"), EL_STR("memory:remembered"), el_from_float(sal), el_from_float(sal), el_from_float(0.9), EL_STR("Episodic"), final_tags); if (!api_persisted(id)) { return api_not_persisted(id); @@ -28837,15 +28939,15 @@ el_val_t handle_api_node_create(el_val_t body) { return api_err(EL_STR("content is required")); } el_val_t nt_raw = json_get(body, EL_STR("node_type")); - el_val_t node_type = ({ el_val_t _if_result_494 = 0; if (str_eq(nt_raw, EL_STR(""))) { _if_result_494 = (EL_STR("Memory")); } else { _if_result_494 = (nt_raw); } _if_result_494; }); + el_val_t node_type = ({ el_val_t _if_result_502 = 0; if (str_eq(nt_raw, EL_STR(""))) { _if_result_502 = (EL_STR("Memory")); } else { _if_result_502 = (nt_raw); } _if_result_502; }); el_val_t label_raw = json_get(body, EL_STR("label")); - el_val_t label = ({ el_val_t _if_result_495 = 0; if (str_eq(label_raw, EL_STR(""))) { _if_result_495 = (EL_STR("node:created")); } else { _if_result_495 = (label_raw); } _if_result_495; }); + el_val_t label = ({ el_val_t _if_result_503 = 0; if (str_eq(label_raw, EL_STR(""))) { _if_result_503 = (EL_STR("node:created")); } else { _if_result_503 = (label_raw); } _if_result_503; }); el_val_t tier_raw = json_get(body, EL_STR("tier")); - el_val_t tier = ({ el_val_t _if_result_496 = 0; if (str_eq(tier_raw, EL_STR(""))) { _if_result_496 = (EL_STR("Episodic")); } else { _if_result_496 = (tier_raw); } _if_result_496; }); + el_val_t tier = ({ el_val_t _if_result_504 = 0; if (str_eq(tier_raw, EL_STR(""))) { _if_result_504 = (EL_STR("Episodic")); } else { _if_result_504 = (tier_raw); } _if_result_504; }); el_val_t tags_raw = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_497 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_497 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_497 = (tags_raw); } _if_result_497; }); + el_val_t tags = ({ el_val_t _if_result_505 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_505 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_505 = (tags_raw); } _if_result_505; }); el_val_t importance = json_get(body, EL_STR("importance")); - el_val_t sal = ({ el_val_t _if_result_498 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_498 = (el_from_float(0.95)); } else { _if_result_498 = (({ el_val_t _if_result_499 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_499 = (el_from_float(0.75)); } else { _if_result_499 = (({ el_val_t _if_result_500 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_500 = (el_from_float(0.25)); } else { _if_result_500 = (el_from_float(0.5)); } _if_result_500; })); } _if_result_499; })); } _if_result_498; }); + el_val_t sal = ({ el_val_t _if_result_506 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_506 = (el_from_float(0.95)); } else { _if_result_506 = (({ el_val_t _if_result_507 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_507 = (el_from_float(0.75)); } else { _if_result_507 = (({ el_val_t _if_result_508 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_508 = (el_from_float(0.25)); } else { _if_result_508 = (el_from_float(0.5)); } _if_result_508; })); } _if_result_507; })); } _if_result_506; }); el_val_t id = engram_node_full(content, node_type, label, el_from_float(sal), el_from_float(sal), el_from_float(0.9), tier, tags); if (!api_persisted(id)) { return api_not_persisted(id); @@ -28884,18 +28986,18 @@ el_val_t handle_api_node_update(el_val_t body) { } el_val_t old = engram_get_node_json(id); el_val_t body_content = json_get(body, EL_STR("content")); - el_val_t content = ({ el_val_t _if_result_501 = 0; if (str_eq(body_content, EL_STR(""))) { _if_result_501 = (json_get(old, EL_STR("content"))); } else { _if_result_501 = (body_content); } _if_result_501; }); + el_val_t content = ({ el_val_t _if_result_509 = 0; if (str_eq(body_content, EL_STR(""))) { _if_result_509 = (json_get(old, EL_STR("content"))); } else { _if_result_509 = (body_content); } _if_result_509; }); el_val_t body_nt = json_get(body, EL_STR("node_type")); el_val_t old_nt = json_get(old, EL_STR("node_type")); - el_val_t node_type = ({ el_val_t _if_result_502 = 0; if (!str_eq(body_nt, EL_STR(""))) { _if_result_502 = (body_nt); } else { _if_result_502 = (({ el_val_t _if_result_503 = 0; if (!str_eq(old_nt, EL_STR(""))) { _if_result_503 = (old_nt); } else { _if_result_503 = (EL_STR("Memory")); } _if_result_503; })); } _if_result_502; }); + el_val_t node_type = ({ el_val_t _if_result_510 = 0; if (!str_eq(body_nt, EL_STR(""))) { _if_result_510 = (body_nt); } else { _if_result_510 = (({ el_val_t _if_result_511 = 0; if (!str_eq(old_nt, EL_STR(""))) { _if_result_511 = (old_nt); } else { _if_result_511 = (EL_STR("Memory")); } _if_result_511; })); } _if_result_510; }); el_val_t body_label = json_get(body, EL_STR("label")); el_val_t old_label = json_get(old, EL_STR("label")); - el_val_t label = ({ el_val_t _if_result_504 = 0; if (!str_eq(body_label, EL_STR(""))) { _if_result_504 = (body_label); } else { _if_result_504 = (({ el_val_t _if_result_505 = 0; if (!str_eq(old_label, EL_STR(""))) { _if_result_505 = (old_label); } else { _if_result_505 = (EL_STR("node:updated")); } _if_result_505; })); } _if_result_504; }); + el_val_t label = ({ el_val_t _if_result_512 = 0; if (!str_eq(body_label, EL_STR(""))) { _if_result_512 = (body_label); } else { _if_result_512 = (({ el_val_t _if_result_513 = 0; if (!str_eq(old_label, EL_STR(""))) { _if_result_513 = (old_label); } else { _if_result_513 = (EL_STR("node:updated")); } _if_result_513; })); } _if_result_512; }); el_val_t body_tier = json_get(body, EL_STR("tier")); el_val_t old_tier = json_get(old, EL_STR("tier")); - el_val_t tier = ({ el_val_t _if_result_506 = 0; if (!str_eq(body_tier, EL_STR(""))) { _if_result_506 = (body_tier); } else { _if_result_506 = (({ el_val_t _if_result_507 = 0; if (!str_eq(old_tier, EL_STR(""))) { _if_result_507 = (old_tier); } else { _if_result_507 = (EL_STR("Episodic")); } _if_result_507; })); } _if_result_506; }); + el_val_t tier = ({ el_val_t _if_result_514 = 0; if (!str_eq(body_tier, EL_STR(""))) { _if_result_514 = (body_tier); } else { _if_result_514 = (({ el_val_t _if_result_515 = 0; if (!str_eq(old_tier, EL_STR(""))) { _if_result_515 = (old_tier); } else { _if_result_515 = (EL_STR("Episodic")); } _if_result_515; })); } _if_result_514; }); el_val_t body_tags = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_508 = 0; if (str_eq(body_tags, EL_STR(""))) { _if_result_508 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_508 = (body_tags); } _if_result_508; }); + el_val_t tags = ({ el_val_t _if_result_516 = 0; if (str_eq(body_tags, EL_STR(""))) { _if_result_516 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_516 = (body_tags); } _if_result_516; }); el_val_t new_id = engram_node_full(content, node_type, label, el_from_float(0.5), el_from_float(0.5), el_from_float(0.8), tier, tags); if (!api_persisted(new_id)) { return api_not_persisted(new_id); @@ -28906,15 +29008,15 @@ el_val_t handle_api_node_update(el_val_t body) { } el_val_t handle_api_recall(el_val_t method, el_val_t path, el_val_t body) { - el_val_t url_q = ({ el_val_t _if_result_509 = 0; if (str_eq(api_query_param(path, EL_STR("query")), EL_STR(""))) { _if_result_509 = (api_query_param(path, EL_STR("q"))); } else { _if_result_509 = (api_query_param(path, EL_STR("query"))); } _if_result_509; }); + el_val_t url_q = ({ el_val_t _if_result_517 = 0; if (str_eq(api_query_param(path, EL_STR("query")), EL_STR(""))) { _if_result_517 = (api_query_param(path, EL_STR("q"))); } else { _if_result_517 = (api_query_param(path, EL_STR("query"))); } _if_result_517; }); el_val_t body_query = json_get(body, EL_STR("query")); el_val_t body_q = json_get(body, EL_STR("q")); - el_val_t q = ({ el_val_t _if_result_510 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_510 = (url_q); } else { _if_result_510 = (({ el_val_t _if_result_511 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_511 = (body_query); } else { _if_result_511 = (body_q); } _if_result_511; })); } _if_result_510; }); + el_val_t q = ({ el_val_t _if_result_518 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_518 = (url_q); } else { _if_result_518 = (({ el_val_t _if_result_519 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_519 = (body_query); } else { _if_result_519 = (body_q); } _if_result_519; })); } _if_result_518; }); el_val_t chain = json_get(body, EL_STR("chain_name")); el_val_t limit = api_query_int(path, EL_STR("limit"), 0); - limit = ({ el_val_t _if_result_512 = 0; if ((limit == 0)) { _if_result_512 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_512 = (limit); } _if_result_512; }); - limit = ({ el_val_t _if_result_513 = 0; if ((limit == 0)) { _if_result_513 = (10); } else { _if_result_513 = (limit); } _if_result_513; }); - el_val_t eff_q = ({ el_val_t _if_result_514 = 0; if (str_eq(q, EL_STR(""))) { _if_result_514 = (chain); } else { _if_result_514 = (q); } _if_result_514; }); + limit = ({ el_val_t _if_result_520 = 0; if ((limit == 0)) { _if_result_520 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_520 = (limit); } _if_result_520; }); + limit = ({ el_val_t _if_result_521 = 0; if ((limit == 0)) { _if_result_521 = (10); } else { _if_result_521 = (limit); } _if_result_521; }); + el_val_t eff_q = ({ el_val_t _if_result_522 = 0; if (str_eq(q, EL_STR(""))) { _if_result_522 = (chain); } else { _if_result_522 = (q); } _if_result_522; }); if (str_eq(eff_q, EL_STR(""))) { return api_or_empty(engram_scan_nodes_json(limit, 0)); } @@ -28927,10 +29029,10 @@ el_val_t handle_api_search_knowledge(el_val_t method, el_val_t path, el_val_t bo el_val_t url_q = api_query_param(path, EL_STR("q")); el_val_t body_query = json_get(body, EL_STR("query")); el_val_t body_q = json_get(body, EL_STR("q")); - el_val_t q = ({ el_val_t _if_result_515 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_515 = (url_q); } else { _if_result_515 = (({ el_val_t _if_result_516 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_516 = (body_query); } else { _if_result_516 = (body_q); } _if_result_516; })); } _if_result_515; }); + el_val_t q = ({ el_val_t _if_result_523 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_523 = (url_q); } else { _if_result_523 = (({ el_val_t _if_result_524 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_524 = (body_query); } else { _if_result_524 = (body_q); } _if_result_524; })); } _if_result_523; }); el_val_t limit = api_query_int(path, EL_STR("limit"), 0); - limit = ({ el_val_t _if_result_517 = 0; if ((limit == 0)) { _if_result_517 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_517 = (limit); } _if_result_517; }); - limit = ({ el_val_t _if_result_518 = 0; if ((limit == 0)) { _if_result_518 = (10); } else { _if_result_518 = (limit); } _if_result_518; }); + limit = ({ el_val_t _if_result_525 = 0; if ((limit == 0)) { _if_result_525 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_525 = (limit); } _if_result_525; }); + limit = ({ el_val_t _if_result_526 = 0; if ((limit == 0)) { _if_result_526 = (10); } else { _if_result_526 = (limit); } _if_result_526; }); if (str_eq(q, EL_STR(""))) { return api_err(EL_STR("query is required")); } @@ -28958,7 +29060,7 @@ el_val_t handle_api_capture_knowledge(el_val_t body) { if (str_eq(content, EL_STR(""))) { return api_err(EL_STR("content is required")); } - el_val_t full = ({ el_val_t _if_result_519 = 0; if (str_eq(title, EL_STR(""))) { _if_result_519 = (content); } else { _if_result_519 = (el_str_concat(el_str_concat(title, EL_STR(": ")), content)); } _if_result_519; }); + el_val_t full = ({ el_val_t _if_result_527 = 0; if (str_eq(title, EL_STR(""))) { _if_result_527 = (content); } else { _if_result_527 = (el_str_concat(el_str_concat(title, EL_STR(": ")), content)); } _if_result_527; }); el_val_t tags = EL_STR("[\"Knowledge\",\"captured\"]"); el_val_t id = engram_node_full(full, EL_STR("Knowledge"), EL_STR("knowledge:captured"), el_from_float(0.85), el_from_float(0.8), el_from_float(0.9), EL_STR("Episodic"), tags); if (!api_persisted(id)) { @@ -28999,7 +29101,7 @@ el_val_t handle_api_promote_knowledge(el_val_t body) { return api_err(EL_STR("id (prior node) is required")); } el_val_t tags_raw = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_520 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_520 = (EL_STR("[\"Knowledge\",\"tier:canonical\",\"disposition:stable\"]")); } else { _if_result_520 = (tags_raw); } _if_result_520; }); + el_val_t tags = ({ el_val_t _if_result_528 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_528 = (EL_STR("[\"Knowledge\",\"tier:canonical\",\"disposition:stable\"]")); } else { _if_result_528 = (tags_raw); } _if_result_528; }); el_val_t new_id = engram_node_full(content, EL_STR("Knowledge"), EL_STR("knowledge:canonical"), el_from_float(0.9), el_from_float(0.9), el_from_float(1.0), EL_STR("Canonical"), tags); if (!api_persisted(new_id)) { return api_not_persisted(new_id); @@ -29010,7 +29112,7 @@ el_val_t handle_api_promote_knowledge(el_val_t body) { } el_val_t handle_api_browse_processes(el_val_t method, el_val_t path, el_val_t body) { - el_val_t name = ({ el_val_t _if_result_521 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_521 = (api_query_param(path, EL_STR("name"))); } else { _if_result_521 = (json_get(body, EL_STR("name"))); } _if_result_521; }); + el_val_t name = ({ el_val_t _if_result_529 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_529 = (api_query_param(path, EL_STR("name"))); } else { _if_result_529 = (json_get(body, EL_STR("name"))); } _if_result_529; }); el_val_t limit = api_query_int(path, EL_STR("limit"), 50); if (str_eq(name, EL_STR(""))) { return api_or_empty(engram_scan_nodes_by_type_json(EL_STR("Process"), limit, 0)); @@ -29025,7 +29127,7 @@ el_val_t handle_api_define_process(el_val_t body) { if (str_eq(content, EL_STR(""))) { return api_err(EL_STR("content is required")); } - el_val_t label = ({ el_val_t _if_result_522 = 0; if (str_eq(name, EL_STR(""))) { _if_result_522 = (EL_STR("process:unnamed")); } else { _if_result_522 = (el_str_concat(EL_STR("process:"), name)); } _if_result_522; }); + el_val_t label = ({ el_val_t _if_result_530 = 0; if (str_eq(name, EL_STR(""))) { _if_result_530 = (EL_STR("process:unnamed")); } else { _if_result_530 = (el_str_concat(EL_STR("process:"), name)); } _if_result_530; }); el_val_t tags = EL_STR("[\"Process\"]"); el_val_t id = engram_node_full(content, EL_STR("Process"), label, el_from_float(0.8), el_from_float(0.8), el_from_float(0.9), EL_STR("Canonical"), tags); if (!api_persisted(id)) { @@ -29043,12 +29145,12 @@ el_val_t handle_api_log_state_event(el_val_t body) { el_val_t gap = json_get(body, EL_STR("gap_direction")); el_val_t legacy = json_get(body, EL_STR("content")); el_val_t parts = EL_STR("INTERNAL STATE EVENT"); - parts = ({ el_val_t _if_result_523 = 0; if (!str_eq(trigger, EL_STR(""))) { _if_result_523 = (el_str_concat(el_str_concat(parts, EL_STR("\nTrigger: ")), trigger)); } else { _if_result_523 = (parts); } _if_result_523; }); - parts = ({ el_val_t _if_result_524 = 0; if (!str_eq(pre, EL_STR(""))) { _if_result_524 = (el_str_concat(el_str_concat(parts, EL_STR("\nPre-reasoning: ")), pre)); } else { _if_result_524 = (parts); } _if_result_524; }); - parts = ({ el_val_t _if_result_525 = 0; if (!str_eq(post, EL_STR(""))) { _if_result_525 = (el_str_concat(el_str_concat(parts, EL_STR("\nPost-reasoning: ")), post)); } else { _if_result_525 = (parts); } _if_result_525; }); - parts = ({ el_val_t _if_result_526 = 0; if (!str_eq(ratio, EL_STR(""))) { _if_result_526 = (el_str_concat(el_str_concat(parts, EL_STR("\nCompression-ratio: ")), ratio)); } else { _if_result_526 = (parts); } _if_result_526; }); - parts = ({ el_val_t _if_result_527 = 0; if (!str_eq(gap, EL_STR(""))) { _if_result_527 = (el_str_concat(el_str_concat(parts, EL_STR("\nGap-direction: ")), gap)); } else { _if_result_527 = (parts); } _if_result_527; }); - parts = ({ el_val_t _if_result_528 = 0; if (!str_eq(legacy, EL_STR(""))) { _if_result_528 = (el_str_concat(el_str_concat(parts, EL_STR("\n")), legacy)); } else { _if_result_528 = (parts); } _if_result_528; }); + parts = ({ el_val_t _if_result_531 = 0; if (!str_eq(trigger, EL_STR(""))) { _if_result_531 = (el_str_concat(el_str_concat(parts, EL_STR("\nTrigger: ")), trigger)); } else { _if_result_531 = (parts); } _if_result_531; }); + parts = ({ el_val_t _if_result_532 = 0; if (!str_eq(pre, EL_STR(""))) { _if_result_532 = (el_str_concat(el_str_concat(parts, EL_STR("\nPre-reasoning: ")), pre)); } else { _if_result_532 = (parts); } _if_result_532; }); + parts = ({ el_val_t _if_result_533 = 0; if (!str_eq(post, EL_STR(""))) { _if_result_533 = (el_str_concat(el_str_concat(parts, EL_STR("\nPost-reasoning: ")), post)); } else { _if_result_533 = (parts); } _if_result_533; }); + parts = ({ el_val_t _if_result_534 = 0; if (!str_eq(ratio, EL_STR(""))) { _if_result_534 = (el_str_concat(el_str_concat(parts, EL_STR("\nCompression-ratio: ")), ratio)); } else { _if_result_534 = (parts); } _if_result_534; }); + parts = ({ el_val_t _if_result_535 = 0; if (!str_eq(gap, EL_STR(""))) { _if_result_535 = (el_str_concat(el_str_concat(parts, EL_STR("\nGap-direction: ")), gap)); } else { _if_result_535 = (parts); } _if_result_535; }); + parts = ({ el_val_t _if_result_536 = 0; if (!str_eq(legacy, EL_STR(""))) { _if_result_536 = (el_str_concat(el_str_concat(parts, EL_STR("\n")), legacy)); } else { _if_result_536 = (parts); } _if_result_536; }); el_val_t ts = time_now(); el_val_t boot = state_get(EL_STR("soul_boot_count")); el_val_t tags = EL_STR("[\"internal-state\",\"InternalStateEvent\",\"pre-reasoning\"]"); @@ -29061,7 +29163,7 @@ el_val_t handle_api_log_state_event(el_val_t body) { } el_val_t handle_api_list_state_events(el_val_t method, el_val_t path, el_val_t body) { - el_val_t q = ({ el_val_t _if_result_529 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_529 = (api_query_param(path, EL_STR("query"))); } else { _if_result_529 = (json_get(body, EL_STR("query"))); } _if_result_529; }); + el_val_t q = ({ el_val_t _if_result_537 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_537 = (api_query_param(path, EL_STR("query"))); } else { _if_result_537 = (json_get(body, EL_STR("query"))); } _if_result_537; }); el_val_t limit = api_query_int(path, EL_STR("limit"), 20); if (!str_eq(q, EL_STR(""))) { return api_or_empty(engram_search_json(el_str_concat(EL_STR("internal state "), q), limit)); @@ -29072,7 +29174,7 @@ el_val_t handle_api_list_state_events(el_val_t method, el_val_t path, el_val_t b el_val_t handle_api_inspect_config(el_val_t path, el_val_t body) { el_val_t key = api_query_param(path, EL_STR("key")); - key = ({ el_val_t _if_result_530 = 0; if (str_eq(key, EL_STR(""))) { _if_result_530 = (json_get(body, EL_STR("key"))); } else { _if_result_530 = (key); } _if_result_530; }); + key = ({ el_val_t _if_result_538 = 0; if (str_eq(key, EL_STR(""))) { _if_result_538 = (json_get(body, EL_STR("key"))); } else { _if_result_538 = (key); } _if_result_538; }); if (str_eq(key, EL_STR(""))) { return EL_STR("{\"hint\":\"pass ?key=\",\"known\":[\"neuron.self.traversal_root\",\"neuron.self.values_hub\"]}"); } @@ -29089,7 +29191,7 @@ el_val_t handle_api_inspect_config(el_val_t path, el_val_t body) { el_val_t node = json_array_get(results, 0); el_val_t content = json_get(node, EL_STR("content")); el_val_t prefix = el_str_concat(el_str_concat(EL_STR("config:"), key), EL_STR("=")); - el_val_t value = ({ el_val_t _if_result_531 = 0; if (str_starts_with(content, prefix)) { _if_result_531 = (str_slice(content, str_len(prefix), str_len(content))); } else { _if_result_531 = (content); } _if_result_531; }); + el_val_t value = ({ el_val_t _if_result_539 = 0; if (str_starts_with(content, prefix)) { _if_result_539 = (str_slice(content, str_len(prefix), str_len(content))); } else { _if_result_539 = (content); } _if_result_539; }); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"key\":\""), key), EL_STR("\",\"value\":\"")), value), EL_STR("\"}")); return 0; } @@ -29111,13 +29213,13 @@ el_val_t handle_api_tune_config(el_val_t body) { } el_val_t handle_api_inspect_graph(el_val_t method, el_val_t path, el_val_t body) { - el_val_t entity_id = ({ el_val_t _if_result_532 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_532 = (api_query_param(path, EL_STR("id"))); } else { _if_result_532 = (json_get(body, EL_STR("entity_id"))); } _if_result_532; }); - el_val_t name = ({ el_val_t _if_result_533 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_533 = (api_query_param(path, EL_STR("name"))); } else { _if_result_533 = (json_get(body, EL_STR("name"))); } _if_result_533; }); + el_val_t entity_id = ({ el_val_t _if_result_540 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_540 = (api_query_param(path, EL_STR("id"))); } else { _if_result_540 = (json_get(body, EL_STR("entity_id"))); } _if_result_540; }); + el_val_t name = ({ el_val_t _if_result_541 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_541 = (api_query_param(path, EL_STR("name"))); } else { _if_result_541 = (json_get(body, EL_STR("name"))); } _if_result_541; }); el_val_t depth = api_query_int(path, EL_STR("depth"), 0); - depth = ({ el_val_t _if_result_534 = 0; if ((depth == 0)) { _if_result_534 = (json_get_int(body, EL_STR("max_depth"))); } else { _if_result_534 = (depth); } _if_result_534; }); - depth = ({ el_val_t _if_result_535 = 0; if ((depth == 0)) { _if_result_535 = (1); } else { _if_result_535 = (depth); } _if_result_535; }); + depth = ({ el_val_t _if_result_542 = 0; if ((depth == 0)) { _if_result_542 = (json_get_int(body, EL_STR("max_depth"))); } else { _if_result_542 = (depth); } _if_result_542; }); + depth = ({ el_val_t _if_result_543 = 0; if ((depth == 0)) { _if_result_543 = (1); } else { _if_result_543 = (depth); } _if_result_543; }); el_val_t resolved = entity_id; - resolved = ({ el_val_t _if_result_536 = 0; if (str_eq(resolved, EL_STR(""))) { _if_result_536 = (({ el_val_t _if_result_537 = 0; if ((str_eq(name, EL_STR("self")) || str_eq(name, EL_STR("neuron")))) { _if_result_537 = (EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee")); } else { _if_result_537 = (({ el_val_t _if_result_538 = 0; if ((str_eq(name, EL_STR("values")) || str_eq(name, EL_STR("values_hub")))) { _if_result_538 = (EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440")); } else { _if_result_538 = (EL_STR("")); } _if_result_538; })); } _if_result_537; })); } else { _if_result_536 = (resolved); } _if_result_536; }); + resolved = ({ el_val_t _if_result_544 = 0; if (str_eq(resolved, EL_STR(""))) { _if_result_544 = (({ el_val_t _if_result_545 = 0; if ((str_eq(name, EL_STR("self")) || str_eq(name, EL_STR("neuron")))) { _if_result_545 = (EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee")); } else { _if_result_545 = (({ el_val_t _if_result_546 = 0; if ((str_eq(name, EL_STR("values")) || str_eq(name, EL_STR("values_hub")))) { _if_result_546 = (EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440")); } else { _if_result_546 = (EL_STR("")); } _if_result_546; })); } _if_result_545; })); } else { _if_result_544 = (resolved); } _if_result_544; }); if (str_eq(resolved, EL_STR(""))) { return api_err(EL_STR("entity_id or name required. Known names: self, neuron, values, values_hub")); } @@ -29139,7 +29241,7 @@ el_val_t handle_api_link_entities(el_val_t body) { return api_err_protected(to_id); } el_val_t relation = json_get(body, EL_STR("relation")); - el_val_t eff_relation = ({ el_val_t _if_result_539 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_539 = (EL_STR("associates")); } else { _if_result_539 = (relation); } _if_result_539; }); + el_val_t eff_relation = ({ el_val_t _if_result_547 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_547 = (EL_STR("associates")); } else { _if_result_547 = (relation); } _if_result_547; }); engram_connect(from_id, to_id, el_from_float(0.5), eff_relation); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"from_id\":\""), from_id), EL_STR("\",\"to_id\":\"")), to_id), EL_STR("\",\"relation\":\"")), eff_relation), EL_STR("\"}")); return 0; @@ -29168,8 +29270,8 @@ el_val_t handle_api_evolve_memory(el_val_t body) { return api_err_protected(prior_id); } el_val_t importance = json_get(body, EL_STR("importance")); - el_val_t sal_str = ({ el_val_t _if_result_540 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_540 = (EL_STR("0.95")); } else { _if_result_540 = (({ el_val_t _if_result_541 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_541 = (EL_STR("0.75")); } else { _if_result_541 = (({ el_val_t _if_result_542 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_542 = (EL_STR("0.25")); } else { _if_result_542 = (EL_STR("0.50")); } _if_result_542; })); } _if_result_541; })); } _if_result_540; }); - el_val_t sal = ({ el_val_t _if_result_543 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_543 = (el_from_float(0.95)); } else { _if_result_543 = (({ el_val_t _if_result_544 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_544 = (el_from_float(0.75)); } else { _if_result_544 = (({ el_val_t _if_result_545 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_545 = (el_from_float(0.25)); } else { _if_result_545 = (el_from_float(0.5)); } _if_result_545; })); } _if_result_544; })); } _if_result_543; }); + el_val_t sal_str = ({ el_val_t _if_result_548 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_548 = (EL_STR("0.95")); } else { _if_result_548 = (({ el_val_t _if_result_549 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_549 = (EL_STR("0.75")); } else { _if_result_549 = (({ el_val_t _if_result_550 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_550 = (EL_STR("0.25")); } else { _if_result_550 = (EL_STR("0.50")); } _if_result_550; })); } _if_result_549; })); } _if_result_548; }); + el_val_t sal = ({ el_val_t _if_result_551 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_551 = (el_from_float(0.95)); } else { _if_result_551 = (({ el_val_t _if_result_552 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_552 = (el_from_float(0.75)); } else { _if_result_552 = (({ el_val_t _if_result_553 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_553 = (el_from_float(0.25)); } else { _if_result_553 = (el_from_float(0.5)); } _if_result_553; })); } _if_result_552; })); } _if_result_551; }); el_val_t tags = EL_STR("[\"Memory\",\"evolved\"]"); el_val_t new_id = engram_node_full(content, EL_STR("Memory"), EL_STR("memory:evolved"), el_from_float(sal), el_from_float(sal), el_from_float(0.9), EL_STR("Episodic"), tags); if (!str_eq(prior_id, EL_STR("")) && !str_eq(new_id, EL_STR(""))) { @@ -29244,7 +29346,7 @@ el_val_t handle_api_cultivate(el_val_t body) { return api_err(EL_STR("content is required")); } el_val_t importance = json_get(body, EL_STR("importance")); - el_val_t sal = ({ el_val_t _if_result_546 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_546 = (el_from_float(0.95)); } else { _if_result_546 = (({ el_val_t _if_result_547 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_547 = (el_from_float(0.75)); } else { _if_result_547 = (({ el_val_t _if_result_548 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_548 = (el_from_float(0.25)); } else { _if_result_548 = (el_from_float(0.5)); } _if_result_548; })); } _if_result_547; })); } _if_result_546; }); + el_val_t sal = ({ el_val_t _if_result_554 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_554 = (el_from_float(0.95)); } else { _if_result_554 = (({ el_val_t _if_result_555 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_555 = (el_from_float(0.75)); } else { _if_result_555 = (({ el_val_t _if_result_556 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_556 = (el_from_float(0.25)); } else { _if_result_556 = (el_from_float(0.5)); } _if_result_556; })); } _if_result_555; })); } _if_result_554; }); el_val_t tags = EL_STR("[\"Memory\",\"evolved\",\"cultivated\"]"); el_val_t new_id = engram_node_full(content, EL_STR("Memory"), EL_STR("memory:cultivated"), el_from_float(sal), el_from_float(sal), el_from_float(0.9), EL_STR("Episodic"), tags); if (!str_eq(prior_id, EL_STR("")) && !str_eq(new_id, EL_STR(""))) { @@ -29270,7 +29372,7 @@ el_val_t handle_api_cultivate(el_val_t body) { return api_err(EL_STR("to_id is required")); } el_val_t relation = json_get(body, EL_STR("relation")); - el_val_t eff_relation = ({ el_val_t _if_result_549 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_549 = (EL_STR("associates")); } else { _if_result_549 = (relation); } _if_result_549; }); + el_val_t eff_relation = ({ el_val_t _if_result_557 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_557 = (EL_STR("associates")); } else { _if_result_557 = (relation); } _if_result_557; }); engram_connect(from_id, to_id, el_from_float(0.5), eff_relation); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"from_id\":\""), from_id), EL_STR("\",\"to_id\":\"")), to_id), EL_STR("\",\"relation\":\"")), eff_relation), EL_STR("\",\"cultivated\":true}")); } @@ -29351,7 +29453,7 @@ el_val_t session_exists(el_val_t session_id) { el_val_t content = json_get(node, EL_STR("content")); el_val_t sid = json_get(content, EL_STR("id")); el_val_t is_match = (str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)); - found = ({ el_val_t _if_result_550 = 0; if (is_match) { _if_result_550 = (1); } else { _if_result_550 = (found); } _if_result_550; }); + found = ({ el_val_t _if_result_558 = 0; if (is_match) { _if_result_558 = (1); } else { _if_result_558 = (found); } _if_result_558; }); i = (i + 1); } return found; @@ -29362,7 +29464,7 @@ el_val_t session_create(el_val_t body) { el_val_t ts = time_now(); el_val_t id = uuid_v4(); el_val_t title_req = json_get(body, EL_STR("title")); - el_val_t title = ({ el_val_t _if_result_551 = 0; if (str_eq(title_req, EL_STR(""))) { _if_result_551 = (EL_STR("New conversation")); } else { _if_result_551 = (title_req); } _if_result_551; }); + el_val_t title = ({ el_val_t _if_result_559 = 0; if (str_eq(title_req, EL_STR(""))) { _if_result_559 = (EL_STR("New conversation")); } else { _if_result_559 = (title_req); } _if_result_559; }); el_val_t folder = json_get(body, EL_STR("folder")); el_val_t content = session_make_content(id, title, ts, ts, folder); el_val_t tags = EL_STR("[\"session\",\"session:meta\",\"Conversation\"]"); @@ -29374,7 +29476,7 @@ el_val_t session_create(el_val_t body) { state_set(el_str_concat(EL_STR("session_pending_first_msg_"), id), EL_STR("1")); el_val_t existing_idx = state_get(EL_STR("session_index")); el_val_t idx_entry = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), id), EL_STR("\",\"title\":\"")), json_safe(title)), EL_STR("\",\"folder\":\"")), json_safe(folder)), EL_STR("\",\"created_at\":")), int_to_str(ts)), EL_STR(",\"updated_at\":")), int_to_str(ts)), EL_STR(",\"last_message\":\"\"}")); - el_val_t new_idx = ({ el_val_t _if_result_552 = 0; if (str_eq(existing_idx, EL_STR(""))) { _if_result_552 = (el_str_concat(el_str_concat(EL_STR("["), idx_entry), EL_STR("]"))); } else { el_val_t inner = str_slice(existing_idx, 1, (str_len(existing_idx) - 1)); _if_result_552 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), idx_entry), EL_STR(",")), inner), EL_STR("]"))); } _if_result_552; }); + el_val_t new_idx = ({ el_val_t _if_result_560 = 0; if (str_eq(existing_idx, EL_STR(""))) { _if_result_560 = (el_str_concat(el_str_concat(EL_STR("["), idx_entry), EL_STR("]"))); } else { el_val_t inner = str_slice(existing_idx, 1, (str_len(existing_idx) - 1)); _if_result_560 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), idx_entry), EL_STR(",")), inner), EL_STR("]"))); } _if_result_560; }); state_set(EL_STR("session_index"), new_idx); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), id), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(title)), EL_STR("\"")), EL_STR(",\"folder\":\"")), json_safe(folder)), EL_STR("\"")), EL_STR(",\"node_id\":\"")), node_id), EL_STR("\"")), EL_STR(",\"created_at\":")), int_to_str(ts)), EL_STR("}")); return 0; @@ -29411,16 +29513,16 @@ el_val_t session_list(void) { el_val_t is_session = (str_eq(label, EL_STR("session:meta")) && str_eq(node_type, EL_STR("Conversation"))); el_val_t content = json_get(node, EL_STR("content")); el_val_t sess_id = json_get(content, EL_STR("id")); - el_val_t eff_id = ({ el_val_t _if_result_553 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_553 = (json_get(node, EL_STR("id"))); } else { _if_result_553 = (sess_id); } _if_result_553; }); + el_val_t eff_id = ({ el_val_t _if_result_561 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_561 = (json_get(node, EL_STR("id"))); } else { _if_result_561 = (sess_id); } _if_result_561; }); el_val_t title_inner = json_get(content, EL_STR("title")); - el_val_t eff_title = ({ el_val_t _if_result_554 = 0; if (str_eq(title_inner, EL_STR(""))) { _if_result_554 = (EL_STR("New conversation")); } else { _if_result_554 = (title_inner); } _if_result_554; }); + el_val_t eff_title = ({ el_val_t _if_result_562 = 0; if (str_eq(title_inner, EL_STR(""))) { _if_result_562 = (EL_STR("New conversation")); } else { _if_result_562 = (title_inner); } _if_result_562; }); el_val_t folder_inner = json_get(content, EL_STR("folder")); el_val_t created_inner = json_get(content, EL_STR("created_at")); el_val_t updated_inner = json_get(content, EL_STR("updated_at")); - el_val_t eff_created = ({ el_val_t _if_result_555 = 0; if (str_eq(created_inner, EL_STR(""))) { _if_result_555 = (EL_STR("0")); } else { _if_result_555 = (created_inner); } _if_result_555; }); - el_val_t eff_updated = ({ el_val_t _if_result_556 = 0; if (str_eq(updated_inner, EL_STR(""))) { _if_result_556 = (eff_created); } else { _if_result_556 = (updated_inner); } _if_result_556; }); - el_val_t entry = ({ el_val_t _if_result_557 = 0; if (is_session) { _if_result_557 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), json_safe(eff_id)), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(eff_title)), EL_STR("\"")), EL_STR(",\"folder\":\"")), json_safe(folder_inner)), EL_STR("\"")), EL_STR(",\"last_message\":\"\"")), EL_STR(",\"created_at\":")), eff_created), EL_STR(",\"updated_at\":")), eff_updated), EL_STR("}"))); } else { _if_result_557 = (EL_STR("")); } _if_result_557; }); - out = ({ el_val_t _if_result_558 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_558 = (({ el_val_t _if_result_559 = 0; if (str_eq(out, EL_STR(""))) { _if_result_559 = (entry); } else { _if_result_559 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_559; })); } else { _if_result_558 = (out); } _if_result_558; }); + el_val_t eff_created = ({ el_val_t _if_result_563 = 0; if (str_eq(created_inner, EL_STR(""))) { _if_result_563 = (EL_STR("0")); } else { _if_result_563 = (created_inner); } _if_result_563; }); + el_val_t eff_updated = ({ el_val_t _if_result_564 = 0; if (str_eq(updated_inner, EL_STR(""))) { _if_result_564 = (eff_created); } else { _if_result_564 = (updated_inner); } _if_result_564; }); + el_val_t entry = ({ el_val_t _if_result_565 = 0; if (is_session) { _if_result_565 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), json_safe(eff_id)), EL_STR("\"")), EL_STR(",\"title\":\"")), json_safe(eff_title)), EL_STR("\"")), EL_STR(",\"folder\":\"")), json_safe(folder_inner)), EL_STR("\"")), EL_STR(",\"last_message\":\"\"")), EL_STR(",\"created_at\":")), eff_created), EL_STR(",\"updated_at\":")), eff_updated), EL_STR("}"))); } else { _if_result_565 = (EL_STR("")); } _if_result_565; }); + out = ({ el_val_t _if_result_566 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_566 = (({ el_val_t _if_result_567 = 0; if (str_eq(out, EL_STR(""))) { _if_result_567 = (entry); } else { _if_result_567 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_567; })); } else { _if_result_566 = (out); } _if_result_566; }); i = (i + 1); } return el_str_concat(el_str_concat(EL_STR("["), out), EL_STR("]")); @@ -29438,7 +29540,7 @@ el_val_t session_get(el_val_t session_id) { el_val_t meta_created = EL_STR("0"); el_val_t meta_updated = EL_STR("0"); el_val_t found = 0; - el_val_t total = ({ el_val_t _if_result_560 = 0; if (str_eq(results, EL_STR(""))) { _if_result_560 = (0); } else { _if_result_560 = (json_array_len(results)); } _if_result_560; }); + el_val_t total = ({ el_val_t _if_result_568 = 0; if (str_eq(results, EL_STR(""))) { _if_result_568 = (0); } else { _if_result_568 = (json_array_len(results)); } _if_result_568; }); el_val_t i = 0; while (i < total) { el_val_t node = json_array_get(results, i); @@ -29446,17 +29548,17 @@ el_val_t session_get(el_val_t session_id) { el_val_t content = json_get(node, EL_STR("content")); el_val_t sid = json_get(content, EL_STR("id")); el_val_t is_match = ((str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)) && !found); - found = ({ el_val_t _if_result_561 = 0; if (is_match) { _if_result_561 = (1); } else { _if_result_561 = (found); } _if_result_561; }); - meta_title = ({ el_val_t _if_result_562 = 0; if (is_match) { _if_result_562 = (json_get(content, EL_STR("title"))); } else { _if_result_562 = (meta_title); } _if_result_562; }); - meta_folder = ({ el_val_t _if_result_563 = 0; if (is_match) { _if_result_563 = (json_get(content, EL_STR("folder"))); } else { _if_result_563 = (meta_folder); } _if_result_563; }); + found = ({ el_val_t _if_result_569 = 0; if (is_match) { _if_result_569 = (1); } else { _if_result_569 = (found); } _if_result_569; }); + meta_title = ({ el_val_t _if_result_570 = 0; if (is_match) { _if_result_570 = (json_get(content, EL_STR("title"))); } else { _if_result_570 = (meta_title); } _if_result_570; }); + meta_folder = ({ el_val_t _if_result_571 = 0; if (is_match) { _if_result_571 = (json_get(content, EL_STR("folder"))); } else { _if_result_571 = (meta_folder); } _if_result_571; }); el_val_t meta_created_raw = json_get(content, EL_STR("created_at")); - meta_created = ({ el_val_t _if_result_564 = 0; if ((is_match && !str_eq(meta_created_raw, EL_STR("")))) { _if_result_564 = (meta_created_raw); } else { _if_result_564 = (meta_created); } _if_result_564; }); + meta_created = ({ el_val_t _if_result_572 = 0; if ((is_match && !str_eq(meta_created_raw, EL_STR("")))) { _if_result_572 = (meta_created_raw); } else { _if_result_572 = (meta_created); } _if_result_572; }); el_val_t meta_updated_raw = json_get(content, EL_STR("updated_at")); - meta_updated = ({ el_val_t _if_result_565 = 0; if ((is_match && !str_eq(meta_updated_raw, EL_STR("")))) { _if_result_565 = (meta_updated_raw); } else { _if_result_565 = (meta_updated); } _if_result_565; }); + meta_updated = ({ el_val_t _if_result_573 = 0; if ((is_match && !str_eq(meta_updated_raw, EL_STR("")))) { _if_result_573 = (meta_updated_raw); } else { _if_result_573 = (meta_updated); } _if_result_573; }); i = (i + 1); } el_val_t state_hist = state_get(el_str_concat(EL_STR("session_hist_"), session_id)); - el_val_t hist_raw = ({ el_val_t _if_result_566 = 0; if (str_eq(state_hist, EL_STR(""))) { el_val_t engram_hist = engram_search_json(el_str_concat(EL_STR("session:messages:"), session_id), 3); _if_result_566 = (({ el_val_t _if_result_567 = 0; if (str_eq(engram_hist, EL_STR(""))) { _if_result_567 = (EL_STR("[]")); } else { _if_result_567 = (({ el_val_t _if_result_568 = 0; if (str_eq(engram_hist, EL_STR("[]"))) { _if_result_568 = (EL_STR("[]")); } else { el_val_t h_node = json_array_get(engram_hist, 0); el_val_t h_content = json_get(h_node, EL_STR("content")); _if_result_568 = (({ el_val_t _if_result_569 = 0; if (str_starts_with(h_content, EL_STR("["))) { _if_result_569 = (h_content); } else { _if_result_569 = (EL_STR("[]")); } _if_result_569; })); } _if_result_568; })); } _if_result_567; })); } else { _if_result_566 = (state_hist); } _if_result_566; }); + el_val_t hist_raw = ({ el_val_t _if_result_574 = 0; if (str_eq(state_hist, EL_STR(""))) { el_val_t engram_hist = engram_search_json(el_str_concat(EL_STR("session:messages:"), session_id), 3); _if_result_574 = (({ el_val_t _if_result_575 = 0; if (str_eq(engram_hist, EL_STR(""))) { _if_result_575 = (EL_STR("[]")); } else { _if_result_575 = (({ el_val_t _if_result_576 = 0; if (str_eq(engram_hist, EL_STR("[]"))) { _if_result_576 = (EL_STR("[]")); } else { el_val_t h_node = json_array_get(engram_hist, 0); el_val_t h_content = json_get(h_node, EL_STR("content")); _if_result_576 = (({ el_val_t _if_result_577 = 0; if (str_starts_with(h_content, EL_STR("["))) { _if_result_577 = (h_content); } else { _if_result_577 = (EL_STR("[]")); } _if_result_577; })); } _if_result_576; })); } _if_result_575; })); } else { _if_result_574 = (state_hist); } _if_result_574; }); el_val_t safe_title = json_safe(meta_title); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), session_id), EL_STR("\"")), EL_STR(",\"title\":\"")), safe_title), EL_STR("\"")), EL_STR(",\"folder\":\"")), json_safe(meta_folder)), EL_STR("\"")), EL_STR(",\"created_at\":")), meta_created), EL_STR(",\"updated_at\":")), meta_updated), EL_STR(",\"messages\":")), hist_raw), EL_STR("}")); return 0; @@ -29467,7 +29569,7 @@ el_val_t session_delete(el_val_t session_id) { return EL_STR("{\"error\":\"session_id is required\"}"); } el_val_t results = engram_search_json(el_str_concat(EL_STR("session:meta "), session_id), 10); - el_val_t total = ({ el_val_t _if_result_570 = 0; if (str_eq(results, EL_STR(""))) { _if_result_570 = (0); } else { _if_result_570 = (json_array_len(results)); } _if_result_570; }); + el_val_t total = ({ el_val_t _if_result_578 = 0; if (str_eq(results, EL_STR(""))) { _if_result_578 = (0); } else { _if_result_578 = (json_array_len(results)); } _if_result_578; }); el_val_t deleted_meta = 0; el_val_t i = 0; while (i < total) { @@ -29477,11 +29579,11 @@ el_val_t session_delete(el_val_t session_id) { el_val_t sid = json_get(content, EL_STR("id")); el_val_t is_match = (str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)); el_val_t node_id = json_get(node, EL_STR("id")); - deleted_meta = ({ el_val_t _if_result_571 = 0; if ((is_match && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_571 = ((deleted_meta + 1)); } else { _if_result_571 = (deleted_meta); } _if_result_571; }); + deleted_meta = ({ el_val_t _if_result_579 = 0; if ((is_match && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_579 = ((deleted_meta + 1)); } else { _if_result_579 = (deleted_meta); } _if_result_579; }); i = (i + 1); } el_val_t msg_results = engram_search_json(el_str_concat(EL_STR("session:messages:"), session_id), 10); - el_val_t m_total = ({ el_val_t _if_result_572 = 0; if (str_eq(msg_results, EL_STR(""))) { _if_result_572 = (0); } else { _if_result_572 = (json_array_len(msg_results)); } _if_result_572; }); + el_val_t m_total = ({ el_val_t _if_result_580 = 0; if (str_eq(msg_results, EL_STR(""))) { _if_result_580 = (0); } else { _if_result_580 = (json_array_len(msg_results)); } _if_result_580; }); el_val_t deleted_msgs = 0; el_val_t j = 0; while (j < m_total) { @@ -29489,7 +29591,7 @@ el_val_t session_delete(el_val_t session_id) { el_val_t label = json_get(node, EL_STR("label")); el_val_t is_msgs = str_eq(label, el_str_concat(EL_STR("session:messages:"), session_id)); el_val_t node_id = json_get(node, EL_STR("id")); - deleted_msgs = ({ el_val_t _if_result_573 = 0; if ((is_msgs && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_573 = ((deleted_msgs + 1)); } else { _if_result_573 = (deleted_msgs); } _if_result_573; }); + deleted_msgs = ({ el_val_t _if_result_581 = 0; if ((is_msgs && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_581 = ((deleted_msgs + 1)); } else { _if_result_581 = (deleted_msgs); } _if_result_581; }); j = (j + 1); } state_set(el_str_concat(EL_STR("session_hist_"), session_id), EL_STR("")); @@ -29512,7 +29614,7 @@ el_val_t session_update_patch(el_val_t session_id, el_val_t body) { return EL_STR("{\"error\":\"title or folder required in body\"}"); } el_val_t results = engram_search_json(EL_STR("session:meta"), 50); - el_val_t total = ({ el_val_t _if_result_574 = 0; if (str_eq(results, EL_STR(""))) { _if_result_574 = (0); } else { _if_result_574 = (json_array_len(results)); } _if_result_574; }); + el_val_t total = ({ el_val_t _if_result_582 = 0; if (str_eq(results, EL_STR(""))) { _if_result_582 = (0); } else { _if_result_582 = (json_array_len(results)); } _if_result_582; }); el_val_t found = 0; el_val_t old_title = EL_STR("New conversation"); el_val_t old_folder = EL_STR(""); @@ -29525,23 +29627,23 @@ el_val_t session_update_patch(el_val_t session_id, el_val_t body) { el_val_t content = json_get(node, EL_STR("content")); el_val_t sid = json_get(content, EL_STR("id")); el_val_t is_match = ((str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)) && !found); - found = ({ el_val_t _if_result_575 = 0; if (is_match) { _if_result_575 = (1); } else { _if_result_575 = (found); } _if_result_575; }); + found = ({ el_val_t _if_result_583 = 0; if (is_match) { _if_result_583 = (1); } else { _if_result_583 = (found); } _if_result_583; }); el_val_t title_raw = json_get(content, EL_STR("title")); - old_title = ({ el_val_t _if_result_576 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_576 = (title_raw); } else { _if_result_576 = (old_title); } _if_result_576; }); + old_title = ({ el_val_t _if_result_584 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_584 = (title_raw); } else { _if_result_584 = (old_title); } _if_result_584; }); el_val_t folder_raw = json_get(content, EL_STR("folder")); - old_folder = ({ el_val_t _if_result_577 = 0; if (is_match) { _if_result_577 = (folder_raw); } else { _if_result_577 = (old_folder); } _if_result_577; }); + old_folder = ({ el_val_t _if_result_585 = 0; if (is_match) { _if_result_585 = (folder_raw); } else { _if_result_585 = (old_folder); } _if_result_585; }); el_val_t created_raw = json_get(content, EL_STR("created_at")); - old_created = ({ el_val_t _if_result_578 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_578 = (created_raw); } else { _if_result_578 = (old_created); } _if_result_578; }); + old_created = ({ el_val_t _if_result_586 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_586 = (created_raw); } else { _if_result_586 = (old_created); } _if_result_586; }); el_val_t nid = json_get(node, EL_STR("id")); - old_node_id = ({ el_val_t _if_result_579 = 0; if (is_match) { _if_result_579 = (nid); } else { _if_result_579 = (old_node_id); } _if_result_579; }); + old_node_id = ({ el_val_t _if_result_587 = 0; if (is_match) { _if_result_587 = (nid); } else { _if_result_587 = (old_node_id); } _if_result_587; }); i = (i + 1); } if (!found) { return el_str_concat(el_str_concat(EL_STR("{\"error\":\"session not found\",\"session_id\":\""), session_id), EL_STR("\"}")); } el_val_t req_title = json_get(body, EL_STR("title")); - el_val_t eff_title = ({ el_val_t _if_result_580 = 0; if ((has_title && !str_eq(req_title, EL_STR("")))) { _if_result_580 = (req_title); } else { _if_result_580 = (old_title); } _if_result_580; }); - el_val_t eff_folder = ({ el_val_t _if_result_581 = 0; if (has_folder) { _if_result_581 = (json_get(body, EL_STR("folder"))); } else { _if_result_581 = (old_folder); } _if_result_581; }); + el_val_t eff_title = ({ el_val_t _if_result_588 = 0; if ((has_title && !str_eq(req_title, EL_STR("")))) { _if_result_588 = (req_title); } else { _if_result_588 = (old_title); } _if_result_588; }); + el_val_t eff_folder = ({ el_val_t _if_result_589 = 0; if (has_folder) { _if_result_589 = (json_get(body, EL_STR("folder"))); } else { _if_result_589 = (old_folder); } _if_result_589; }); if (!str_eq(old_node_id, EL_STR(""))) { engram_forget(old_node_id); } @@ -29569,8 +29671,8 @@ el_val_t session_search_entry(el_val_t node) { el_val_t title = json_get(content, EL_STR("title")); el_val_t created_raw = json_get(content, EL_STR("created_at")); el_val_t updated_raw = json_get(content, EL_STR("updated_at")); - el_val_t eff_created = ({ el_val_t _if_result_582 = 0; if (str_eq(created_raw, EL_STR(""))) { _if_result_582 = (EL_STR("0")); } else { _if_result_582 = (created_raw); } _if_result_582; }); - el_val_t eff_updated = ({ el_val_t _if_result_583 = 0; if (str_eq(updated_raw, EL_STR(""))) { _if_result_583 = (eff_created); } else { _if_result_583 = (updated_raw); } _if_result_583; }); + el_val_t eff_created = ({ el_val_t _if_result_590 = 0; if (str_eq(created_raw, EL_STR(""))) { _if_result_590 = (EL_STR("0")); } else { _if_result_590 = (created_raw); } _if_result_590; }); + el_val_t eff_updated = ({ el_val_t _if_result_591 = 0; if (str_eq(updated_raw, EL_STR(""))) { _if_result_591 = (eff_created); } else { _if_result_591 = (updated_raw); } _if_result_591; }); el_val_t e_id = el_str_concat(el_str_concat(EL_STR("{\"id\":\""), json_safe(sess_id)), EL_STR("\"")); el_val_t e_title = el_str_concat(el_str_concat(EL_STR(",\"title\":\""), json_safe(title)), EL_STR("\"")); el_val_t e_ts = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR(",\"created_at\":"), eff_created), EL_STR(",\"updated_at\":")), eff_updated), EL_STR("}")); @@ -29594,7 +29696,7 @@ el_val_t session_search(el_val_t query) { el_val_t i = 0; while (i < total) { el_val_t entry = session_search_entry(json_array_get(results, i)); - out = ({ el_val_t _if_result_584 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_584 = (({ el_val_t _if_result_585 = 0; if (str_eq(out, EL_STR(""))) { _if_result_585 = (entry); } else { _if_result_585 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_585; })); } else { _if_result_584 = (out); } _if_result_584; }); + out = ({ el_val_t _if_result_592 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_592 = (({ el_val_t _if_result_593 = 0; if (str_eq(out, EL_STR(""))) { _if_result_593 = (entry); } else { _if_result_593 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_593; })); } else { _if_result_592 = (out); } _if_result_592; }); i = (i + 1); } return el_str_concat(el_str_concat(EL_STR("["), out), EL_STR("]")); @@ -29630,7 +29732,7 @@ el_val_t session_hist_save(el_val_t session_id, el_val_t hist) { state_set(el_str_concat(EL_STR("session_hist_"), session_id), hist); state_set(el_str_concat(EL_STR("session_pending_first_msg_"), session_id), EL_STR("")); el_val_t old_results = engram_search_json(el_str_concat(EL_STR("session:messages:"), session_id), 3); - el_val_t o_total = ({ el_val_t _if_result_586 = 0; if (str_eq(old_results, EL_STR(""))) { _if_result_586 = (0); } else { _if_result_586 = (json_array_len(old_results)); } _if_result_586; }); + el_val_t o_total = ({ el_val_t _if_result_594 = 0; if (str_eq(old_results, EL_STR(""))) { _if_result_594 = (0); } else { _if_result_594 = (json_array_len(old_results)); } _if_result_594; }); el_val_t oi = 0; while (oi < o_total) { el_val_t node = json_array_get(old_results, oi); @@ -29648,35 +29750,35 @@ el_val_t session_hist_save(el_val_t session_id, el_val_t hist) { if (str_eq(already_written, EL_STR(""))) { el_val_t bell_count_key = el_str_concat(EL_STR("session_bell_count:"), session_id); el_val_t bell_count_raw = state_get(bell_count_key); - el_val_t bell_count = ({ el_val_t _if_result_587 = 0; if (str_eq(bell_count_raw, EL_STR(""))) { _if_result_587 = (0); } else { _if_result_587 = (str_to_int(bell_count_raw)); } _if_result_587; }); + el_val_t bell_count = ({ el_val_t _if_result_595 = 0; if (str_eq(bell_count_raw, EL_STR(""))) { _if_result_595 = (0); } else { _if_result_595 = (str_to_int(bell_count_raw)); } _if_result_595; }); if (bell_count > 0) { el_val_t bell_level_key = el_str_concat(EL_STR("session_bell_level:"), session_id); el_val_t bell_signal_key = el_str_concat(EL_STR("session_bell_signal:"), session_id); el_val_t dominant_level = state_get(bell_level_key); el_val_t last_signal = state_get(bell_signal_key); - el_val_t eff_level = ({ el_val_t _if_result_588 = 0; if (str_eq(dominant_level, EL_STR(""))) { _if_result_588 = (EL_STR("soft")); } else { _if_result_588 = (dominant_level); } _if_result_588; }); - el_val_t eff_signal = ({ el_val_t _if_result_589 = 0; if (str_eq(last_signal, EL_STR(""))) { _if_result_589 = (EL_STR("(no signal captured)")); } else { _if_result_589 = (last_signal); } _if_result_589; }); + el_val_t eff_level = ({ el_val_t _if_result_596 = 0; if (str_eq(dominant_level, EL_STR(""))) { _if_result_596 = (EL_STR("soft")); } else { _if_result_596 = (dominant_level); } _if_result_596; }); + el_val_t eff_signal = ({ el_val_t _if_result_597 = 0; if (str_eq(last_signal, EL_STR(""))) { _if_result_597 = (EL_STR("(no signal captured)")); } else { _if_result_597 = (last_signal); } _if_result_597; }); el_val_t ts_now = time_now(); el_val_t summary_content = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("session:emotional-summary"), EL_STR(" | session:")), session_id), EL_STR(" | bell_count:")), int_to_str(bell_count)), EL_STR(" | dominant_level:")), eff_level), EL_STR(" | last_signal:")), eff_signal), EL_STR(" | ts:")), int_to_str(ts_now)); el_val_t summary_tags = el_str_concat(el_str_concat(EL_STR("[\"session-emotional-summary\",\"affective\",\"bell:"), eff_level), EL_STR("\",\"BellEvent\"]")); - el_val_t summary_sal = ({ el_val_t _if_result_590 = 0; if (str_eq(eff_level, EL_STR("hard"))) { _if_result_590 = (el_from_float(0.95)); } else { _if_result_590 = (el_from_float(0.85)); } _if_result_590; }); + el_val_t summary_sal = ({ el_val_t _if_result_598 = 0; if (str_eq(eff_level, EL_STR("hard"))) { _if_result_598 = (el_from_float(0.95)); } else { _if_result_598 = (el_from_float(0.85)); } _if_result_598; }); el_val_t sum_discard = engram_node_full(summary_content, EL_STR("BellEvent"), EL_STR("session:emotional-summary"), summary_sal, summary_sal, el_from_float(1.0), EL_STR("Episodic"), summary_tags); state_set(summary_written_key, EL_STR("1")); } } - el_val_t hist_arr_len = ({ el_val_t _if_result_591 = 0; if (str_eq(hist, EL_STR(""))) { _if_result_591 = (0); } else { _if_result_591 = (json_array_len(hist)); } _if_result_591; }); + el_val_t hist_arr_len = ({ el_val_t _if_result_599 = 0; if (str_eq(hist, EL_STR(""))) { _if_result_599 = (0); } else { _if_result_599 = (json_array_len(hist)); } _if_result_599; }); if (hist_arr_len >= 2) { el_val_t last_entry = json_array_get(hist, (hist_arr_len - 1)); el_val_t last_role = json_get(last_entry, EL_STR("role")); el_val_t last_content = json_get(last_entry, EL_STR("content")); - el_val_t topic_snip = ({ el_val_t _if_result_592 = 0; if ((str_len(last_content) > 200)) { _if_result_592 = (str_slice(last_content, 0, 200)); } else { _if_result_592 = (last_content); } _if_result_592; }); + el_val_t topic_snip = ({ el_val_t _if_result_600 = 0; if ((str_len(last_content) > 200)) { _if_result_600 = (str_slice(last_content, 0, 200)); } else { _if_result_600 = (last_content); } _if_result_600; }); el_val_t safe_topic = str_replace(topic_snip, EL_STR("\""), EL_STR("'")); el_val_t ts_now = int_to_str(time_now()); el_val_t topic_content = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("last-session-topic | ts:"), ts_now), EL_STR(" | session:")), session_id), EL_STR(" | topic:")), safe_topic); el_val_t topic_tags = EL_STR("[\"last-session-topic\",\"conv:history\",\"Conversation\",\"session:topic\"]"); el_val_t topic_label = el_str_concat(EL_STR("last-session-topic:"), session_id); el_val_t old_topic = engram_search_json(el_str_concat(EL_STR("last-session-topic:"), session_id), 2); - el_val_t ot_len = ({ el_val_t _if_result_593 = 0; if (str_eq(old_topic, EL_STR(""))) { _if_result_593 = (0); } else { _if_result_593 = (json_array_len(old_topic)); } _if_result_593; }); + el_val_t ot_len = ({ el_val_t _if_result_601 = 0; if (str_eq(old_topic, EL_STR(""))) { _if_result_601 = (0); } else { _if_result_601 = (json_array_len(old_topic)); } _if_result_601; }); el_val_t oti = 0; while (oti < ot_len) { el_val_t ot_node = json_array_get(old_topic, oti); @@ -29693,7 +29795,7 @@ el_val_t session_hist_save(el_val_t session_id, el_val_t hist) { el_val_t session_update_meta_timestamp(el_val_t session_id) { el_val_t results = engram_search_json(el_str_concat(EL_STR("session:meta "), session_id), 10); - el_val_t total = ({ el_val_t _if_result_594 = 0; if (str_eq(results, EL_STR(""))) { _if_result_594 = (0); } else { _if_result_594 = (json_array_len(results)); } _if_result_594; }); + el_val_t total = ({ el_val_t _if_result_602 = 0; if (str_eq(results, EL_STR(""))) { _if_result_602 = (0); } else { _if_result_602 = (json_array_len(results)); } _if_result_602; }); el_val_t found = 0; el_val_t old_title = EL_STR("New conversation"); el_val_t old_folder = EL_STR(""); @@ -29706,15 +29808,15 @@ el_val_t session_update_meta_timestamp(el_val_t session_id) { el_val_t content = json_get(node, EL_STR("content")); el_val_t sid = json_get(content, EL_STR("id")); el_val_t is_match = ((str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)) && !found); - found = ({ el_val_t _if_result_595 = 0; if (is_match) { _if_result_595 = (1); } else { _if_result_595 = (found); } _if_result_595; }); + found = ({ el_val_t _if_result_603 = 0; if (is_match) { _if_result_603 = (1); } else { _if_result_603 = (found); } _if_result_603; }); el_val_t title_raw = json_get(content, EL_STR("title")); - old_title = ({ el_val_t _if_result_596 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_596 = (title_raw); } else { _if_result_596 = (old_title); } _if_result_596; }); + old_title = ({ el_val_t _if_result_604 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_604 = (title_raw); } else { _if_result_604 = (old_title); } _if_result_604; }); el_val_t folder_raw = json_get(content, EL_STR("folder")); - old_folder = ({ el_val_t _if_result_597 = 0; if (is_match) { _if_result_597 = (folder_raw); } else { _if_result_597 = (old_folder); } _if_result_597; }); + old_folder = ({ el_val_t _if_result_605 = 0; if (is_match) { _if_result_605 = (folder_raw); } else { _if_result_605 = (old_folder); } _if_result_605; }); el_val_t created_raw = json_get(content, EL_STR("created_at")); - old_created = ({ el_val_t _if_result_598 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_598 = (created_raw); } else { _if_result_598 = (old_created); } _if_result_598; }); + old_created = ({ el_val_t _if_result_606 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_606 = (created_raw); } else { _if_result_606 = (old_created); } _if_result_606; }); el_val_t nid = json_get(node, EL_STR("id")); - old_node_id = ({ el_val_t _if_result_599 = 0; if (is_match) { _if_result_599 = (nid); } else { _if_result_599 = (old_node_id); } _if_result_599; }); + old_node_id = ({ el_val_t _if_result_607 = 0; if (is_match) { _if_result_607 = (nid); } else { _if_result_607 = (old_node_id); } _if_result_607; }); i = (i + 1); } if (!found) { @@ -29734,7 +29836,7 @@ el_val_t session_update_meta_timestamp(el_val_t session_id) { el_val_t session_auto_title(el_val_t session_id, el_val_t first_message) { el_val_t results = engram_search_json(el_str_concat(EL_STR("session:meta "), session_id), 10); - el_val_t total = ({ el_val_t _if_result_600 = 0; if (str_eq(results, EL_STR(""))) { _if_result_600 = (0); } else { _if_result_600 = (json_array_len(results)); } _if_result_600; }); + el_val_t total = ({ el_val_t _if_result_608 = 0; if (str_eq(results, EL_STR(""))) { _if_result_608 = (0); } else { _if_result_608 = (json_array_len(results)); } _if_result_608; }); el_val_t found = 0; el_val_t cur_title = EL_STR(""); el_val_t old_folder = EL_STR(""); @@ -29747,15 +29849,15 @@ el_val_t session_auto_title(el_val_t session_id, el_val_t first_message) { el_val_t content = json_get(node, EL_STR("content")); el_val_t sid = json_get(content, EL_STR("id")); el_val_t is_match = ((str_eq(label, EL_STR("session:meta")) && str_eq(sid, session_id)) && !found); - found = ({ el_val_t _if_result_601 = 0; if (is_match) { _if_result_601 = (1); } else { _if_result_601 = (found); } _if_result_601; }); + found = ({ el_val_t _if_result_609 = 0; if (is_match) { _if_result_609 = (1); } else { _if_result_609 = (found); } _if_result_609; }); el_val_t title_raw = json_get(content, EL_STR("title")); - cur_title = ({ el_val_t _if_result_602 = 0; if (is_match) { _if_result_602 = (title_raw); } else { _if_result_602 = (cur_title); } _if_result_602; }); + cur_title = ({ el_val_t _if_result_610 = 0; if (is_match) { _if_result_610 = (title_raw); } else { _if_result_610 = (cur_title); } _if_result_610; }); el_val_t folder_raw = json_get(content, EL_STR("folder")); - old_folder = ({ el_val_t _if_result_603 = 0; if (is_match) { _if_result_603 = (folder_raw); } else { _if_result_603 = (old_folder); } _if_result_603; }); + old_folder = ({ el_val_t _if_result_611 = 0; if (is_match) { _if_result_611 = (folder_raw); } else { _if_result_611 = (old_folder); } _if_result_611; }); el_val_t created_raw = json_get(content, EL_STR("created_at")); - old_created = ({ el_val_t _if_result_604 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_604 = (created_raw); } else { _if_result_604 = (old_created); } _if_result_604; }); + old_created = ({ el_val_t _if_result_612 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_612 = (created_raw); } else { _if_result_612 = (old_created); } _if_result_612; }); el_val_t nid = json_get(node, EL_STR("id")); - old_node_id = ({ el_val_t _if_result_605 = 0; if (is_match) { _if_result_605 = (nid); } else { _if_result_605 = (old_node_id); } _if_result_605; }); + old_node_id = ({ el_val_t _if_result_613 = 0; if (is_match) { _if_result_613 = (nid); } else { _if_result_613 = (old_node_id); } _if_result_613; }); i = (i + 1); } if (!found) { @@ -29789,21 +29891,22 @@ el_val_t handle_session_approve(el_val_t session_id, el_val_t body) { if (str_eq(action, EL_STR(""))) { return EL_STR("{\"error\":\"action is required (allow|deny|always)\"}"); } - el_val_t eff_action = ({ el_val_t _if_result_606 = 0; if (str_eq(action, EL_STR("always"))) { _if_result_606 = (EL_STR("allow")); } else { _if_result_606 = (action); } _if_result_606; }); + el_val_t eff_action = ({ el_val_t _if_result_614 = 0; if (str_eq(action, EL_STR("always"))) { _if_result_614 = (EL_STR("allow")); } else { _if_result_614 = (action); } _if_result_614; }); el_val_t bridge_blob = state_get(el_str_concat(EL_STR("mcp_bridge:"), session_id)); if (!str_eq(bridge_blob, EL_STR(""))) { + state_set(EL_STR("agent_workspace_root"), state_get(el_str_concat(EL_STR("agent_workspace_root_"), session_id))); el_val_t always_key = el_str_concat(EL_STR("always_allow_"), session_id); el_val_t approve_tool_name = json_get(body, EL_STR("tool_name")); - el_val_t discard_always = ({ el_val_t _if_result_607 = 0; if ((str_eq(action, EL_STR("always")) && !str_eq(approve_tool_name, EL_STR("")))) { el_val_t always_list = state_get(always_key); el_val_t new_always = ({ el_val_t _if_result_608 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_608 = (approve_tool_name); } else { _if_result_608 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), approve_tool_name)); } _if_result_608; }); (void)(state_set(always_key, new_always)); _if_result_607 = (1); } else { _if_result_607 = (0); } _if_result_607; }); + el_val_t discard_always = ({ el_val_t _if_result_615 = 0; if ((str_eq(action, EL_STR("always")) && !str_eq(approve_tool_name, EL_STR("")))) { el_val_t always_list = state_get(always_key); el_val_t new_always = ({ el_val_t _if_result_616 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_616 = (approve_tool_name); } else { _if_result_616 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), approve_tool_name)); } _if_result_616; }); (void)(state_set(always_key, new_always)); _if_result_615 = (1); } else { _if_result_615 = (0); } _if_result_615; }); if (str_eq(approve_tool_name, EL_STR("")) && str_eq(eff_action, EL_STR("allow"))) { return EL_STR("{\"error\":\"tool_name is required for allow action\"}"); } el_val_t client_content = json_get(body, EL_STR("content")); - el_val_t use_client_content = !str_eq(client_content, EL_STR("")); + el_val_t use_client_content = (!str_eq(client_content, EL_STR("")) && !is_builtin_tool(approve_tool_name)); el_val_t use_dispatch = (is_builtin_tool(approve_tool_name) && !use_client_content); el_val_t raw_input = json_get_raw(body, EL_STR("tool_input")); - el_val_t eff_input = ({ el_val_t _if_result_609 = 0; if (str_eq(raw_input, EL_STR(""))) { _if_result_609 = (EL_STR("{}")); } else { _if_result_609 = (raw_input); } _if_result_609; }); - el_val_t content = ({ el_val_t _if_result_610 = 0; if (str_eq(eff_action, EL_STR("allow"))) { _if_result_610 = (({ el_val_t _if_result_611 = 0; if (use_client_content) { el_val_t trimmed = ({ el_val_t _if_result_612 = 0; if ((str_len(client_content) > 6000)) { _if_result_612 = (el_str_concat(str_slice(client_content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_612 = (client_content); } _if_result_612; }); _if_result_611 = (trimmed); } else { _if_result_611 = (({ el_val_t _if_result_613 = 0; if (use_dispatch) { el_val_t raw = dispatch_tool(approve_tool_name, eff_input); _if_result_613 = (({ el_val_t _if_result_614 = 0; if ((str_len(raw) > 6000)) { _if_result_614 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_614 = (raw); } _if_result_614; })); } else { _if_result_613 = (el_str_concat(el_str_concat(EL_STR("{\"error\":\"client content required for non-builtin tool: "), approve_tool_name), EL_STR("\"}"))); } _if_result_613; })); } _if_result_611; })); } else { _if_result_610 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_610; }); + el_val_t eff_input = ({ el_val_t _if_result_617 = 0; if (str_eq(raw_input, EL_STR(""))) { _if_result_617 = (EL_STR("{}")); } else { _if_result_617 = (raw_input); } _if_result_617; }); + el_val_t content = ({ el_val_t _if_result_618 = 0; if (str_eq(eff_action, EL_STR("allow"))) { _if_result_618 = (({ el_val_t _if_result_619 = 0; if (use_client_content) { el_val_t trimmed = ({ el_val_t _if_result_620 = 0; if ((str_len(client_content) > 6000)) { _if_result_620 = (el_str_concat(str_slice(client_content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_620 = (client_content); } _if_result_620; }); _if_result_619 = (trimmed); } else { _if_result_619 = (({ el_val_t _if_result_621 = 0; if (use_dispatch) { el_val_t raw = dispatch_tool(approve_tool_name, eff_input); _if_result_621 = (({ el_val_t _if_result_622 = 0; if ((str_len(raw) > 6000)) { _if_result_622 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_622 = (raw); } _if_result_622; })); } else { _if_result_621 = (el_str_concat(el_str_concat(EL_STR("{\"error\":\"client content required for non-builtin tool: "), approve_tool_name), EL_STR("\"}"))); } _if_result_621; })); } _if_result_619; })); } else { _if_result_618 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_618; }); return agentic_resume(session_id, call_id, content); } el_val_t pending_raw = state_get(el_str_concat(EL_STR("pending_tool_"), session_id)); @@ -29820,12 +29923,12 @@ el_val_t handle_session_approve(el_val_t session_id, el_val_t body) { el_val_t safe_sys = json_get(pending_raw, EL_STR("system")); el_val_t always_key = el_str_concat(EL_STR("always_allow_"), session_id); el_val_t always_list = state_get(always_key); - el_val_t discard_always2 = ({ el_val_t _if_result_615 = 0; if (str_eq(action, EL_STR("always"))) { el_val_t new_always = ({ el_val_t _if_result_616 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_616 = (tool_name); } else { _if_result_616 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), tool_name)); } _if_result_616; }); (void)(state_set(always_key, new_always)); _if_result_615 = (1); } else { _if_result_615 = (0); } _if_result_615; }); + el_val_t discard_always2 = ({ el_val_t _if_result_623 = 0; if (str_eq(action, EL_STR("always"))) { el_val_t new_always = ({ el_val_t _if_result_624 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_624 = (tool_name); } else { _if_result_624 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), tool_name)); } _if_result_624; }); (void)(state_set(always_key, new_always)); _if_result_623 = (1); } else { _if_result_623 = (0); } _if_result_623; }); state_set(el_str_concat(EL_STR("pending_tool_"), session_id), EL_STR("")); - el_val_t tool_result = ({ el_val_t _if_result_617 = 0; if (str_eq(eff_action, EL_STR("allow"))) { el_val_t raw = dispatch_tool(tool_name, tool_input); _if_result_617 = (({ el_val_t _if_result_618 = 0; if ((str_len(raw) > 6000)) { _if_result_618 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_618 = (raw); } _if_result_618; })); } else { _if_result_617 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_617; }); + el_val_t tool_result = ({ el_val_t _if_result_625 = 0; if (str_eq(eff_action, EL_STR("allow"))) { el_val_t raw = dispatch_tool(tool_name, tool_input); _if_result_625 = (({ el_val_t _if_result_626 = 0; if ((str_len(raw) > 6000)) { _if_result_626 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_626 = (raw); } _if_result_626; })); } else { _if_result_625 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_625; }); el_val_t legacy_messages = json_get_raw(pending_raw, EL_STR("messages_so_far")); el_val_t stored_variant = json_get(pending_raw, EL_STR("tools_variant")); - el_val_t tools_json = ({ el_val_t _if_result_619 = 0; if (str_eq(stored_variant, EL_STR("web"))) { _if_result_619 = (agentic_tools_with_web()); } else { _if_result_619 = (({ el_val_t _if_result_620 = 0; if (str_eq(stored_variant, EL_STR("all"))) { _if_result_620 = (agentic_tools_all()); } else { _if_result_620 = (agentic_tools_literal()); } _if_result_620; })); } _if_result_619; }); + el_val_t tools_json = ({ el_val_t _if_result_627 = 0; if (str_eq(stored_variant, EL_STR("web"))) { _if_result_627 = (agentic_tools_with_web()); } else { _if_result_627 = (({ el_val_t _if_result_628 = 0; if (str_eq(stored_variant, EL_STR("all"))) { _if_result_628 = (agentic_tools_all()); } else { _if_result_628 = (agentic_tools_literal()); } _if_result_628; })); } _if_result_627; }); el_val_t blob = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"model\":\""), json_safe(model)), EL_STR("\"")), EL_STR(",\"safe_sys\":\"")), json_safe(safe_sys)), EL_STR("\"")), EL_STR(",\"tools_json\":\"")), json_safe(tools_json)), EL_STR("\"")), EL_STR(",\"messages\":\"")), json_safe(legacy_messages)), EL_STR("\"")), EL_STR(",\"tools_log\":\"\"")), EL_STR(",\"tool_use_id\":\"")), json_safe(call_id)), EL_STR("\"}")); state_set(el_str_concat(EL_STR("mcp_bridge:"), session_id), blob); return agentic_resume(session_id, call_id, tool_result); @@ -29842,24 +29945,24 @@ el_val_t rate_limit_check(el_val_t ip, el_val_t path) { return EL_STR(""); } el_val_t limit_str = state_get(EL_STR("soul_rate_limit")); - el_val_t limit = ({ el_val_t _if_result_621 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_621 = (60); } else { _if_result_621 = (str_to_int(limit_str)); } _if_result_621; }); + el_val_t limit = ({ el_val_t _if_result_629 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_629 = (60); } else { _if_result_629 = (str_to_int(limit_str)); } _if_result_629; }); el_val_t now = time_now(); el_val_t window_key = el_str_concat(el_str_concat(EL_STR("rl:"), ip), EL_STR(":window")); el_val_t count_key = el_str_concat(el_str_concat(EL_STR("rl:"), ip), EL_STR(":count")); el_val_t win_str = state_get(window_key); - el_val_t win_start = ({ el_val_t _if_result_622 = 0; if (str_eq(win_str, EL_STR(""))) { _if_result_622 = (now); } else { _if_result_622 = (str_to_int(win_str)); } _if_result_622; }); + el_val_t win_start = ({ el_val_t _if_result_630 = 0; if (str_eq(win_str, EL_STR(""))) { _if_result_630 = (now); } else { _if_result_630 = (str_to_int(win_str)); } _if_result_630; }); el_val_t elapsed = (now - win_start); el_val_t in_window = (elapsed < 60); el_val_t prev_count_str = state_get(count_key); - el_val_t prev_count = ({ el_val_t _if_result_623 = 0; if (str_eq(prev_count_str, EL_STR(""))) { _if_result_623 = (0); } else { _if_result_623 = (str_to_int(prev_count_str)); } _if_result_623; }); - el_val_t eff_count = ({ el_val_t _if_result_624 = 0; if (in_window) { _if_result_624 = (prev_count); } else { _if_result_624 = (0); } _if_result_624; }); - el_val_t eff_win = ({ el_val_t _if_result_625 = 0; if (in_window) { _if_result_625 = (win_start); } else { _if_result_625 = (now); } _if_result_625; }); + el_val_t prev_count = ({ el_val_t _if_result_631 = 0; if (str_eq(prev_count_str, EL_STR(""))) { _if_result_631 = (0); } else { _if_result_631 = (str_to_int(prev_count_str)); } _if_result_631; }); + el_val_t eff_count = ({ el_val_t _if_result_632 = 0; if (in_window) { _if_result_632 = (prev_count); } else { _if_result_632 = (0); } _if_result_632; }); + el_val_t eff_win = ({ el_val_t _if_result_633 = 0; if (in_window) { _if_result_633 = (win_start); } else { _if_result_633 = (now); } _if_result_633; }); el_val_t new_count = (eff_count + 1); state_set(count_key, int_to_str(new_count)); state_set(window_key, int_to_str(eff_win)); if (new_count > limit) { el_val_t retry_after = (60 - (now - eff_win)); - el_val_t eff_retry = ({ el_val_t _if_result_626 = 0; if ((retry_after < 0)) { _if_result_626 = (0); } else { _if_result_626 = (retry_after); } _if_result_626; }); + el_val_t eff_retry = ({ el_val_t _if_result_634 = 0; if ((retry_after < 0)) { _if_result_634 = (0); } else { _if_result_634 = (retry_after); } _if_result_634; }); return el_str_concat(el_str_concat(EL_STR("{\"__status__\":429,\"error\":\"rate limit exceeded\",\"code\":\"rate_limited\",\"retry_after_secs\":"), int_to_str(eff_retry)), EL_STR("}")); } return EL_STR(""); @@ -29888,18 +29991,18 @@ el_val_t err_405(el_val_t method, el_val_t path) { el_val_t route_health(void) { el_val_t cgi_id = state_get(EL_STR("soul_cgi_id")); el_val_t boot = state_get(EL_STR("soul_boot_count")); - el_val_t boot_num = ({ el_val_t _if_result_627 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_627 = (EL_STR("0")); } else { _if_result_627 = (boot); } _if_result_627; }); + el_val_t boot_num = ({ el_val_t _if_result_635 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_635 = (EL_STR("0")); } else { _if_result_635 = (boot); } _if_result_635; }); el_val_t node_ct = engram_node_count(); el_val_t edge_ct = engram_edge_count(); el_val_t pulse = state_get(EL_STR("soul.pulse")); - el_val_t pulse_num = ({ el_val_t _if_result_628 = 0; if (str_eq(pulse, EL_STR(""))) { _if_result_628 = (EL_STR("0")); } else { _if_result_628 = (pulse); } _if_result_628; }); + el_val_t pulse_num = ({ el_val_t _if_result_636 = 0; if (str_eq(pulse, EL_STR(""))) { _if_result_636 = (EL_STR("0")); } else { _if_result_636 = (pulse); } _if_result_636; }); el_val_t boot_ts_str = state_get(EL_STR("soul_boot_ts")); - el_val_t uptime_secs = ({ el_val_t _if_result_629 = 0; if (str_eq(boot_ts_str, EL_STR(""))) { _if_result_629 = ((-1)); } else { _if_result_629 = ((time_now() - str_to_int(boot_ts_str))); } _if_result_629; }); + el_val_t uptime_secs = ({ el_val_t _if_result_637 = 0; if (str_eq(boot_ts_str, EL_STR(""))) { _if_result_637 = ((-1)); } else { _if_result_637 = ((time_now() - str_to_int(boot_ts_str))); } _if_result_637; }); el_val_t model = state_get(EL_STR("soul_model")); - el_val_t eff_model = ({ el_val_t _if_result_630 = 0; if (str_eq(model, EL_STR(""))) { _if_result_630 = (EL_STR("claude-sonnet-4-5")); } else { _if_result_630 = (model); } _if_result_630; }); + el_val_t eff_model = ({ el_val_t _if_result_638 = 0; if (str_eq(model, EL_STR(""))) { _if_result_638 = (EL_STR("claude-sonnet-4-5")); } else { _if_result_638 = (model); } _if_result_638; }); el_val_t llm_probe = llm_call_system(eff_model, EL_STR("You are a health probe. Reply with the single word: ok"), EL_STR("ping")); el_val_t llm_ok = (((!str_eq(llm_probe, EL_STR("")) && !str_starts_with(llm_probe, EL_STR("{\"error\""))) && !str_starts_with(llm_probe, EL_STR("{\"type\":\"error\""))) && !str_contains(llm_probe, EL_STR("authentication_error"))); - el_val_t llm_status = ({ el_val_t _if_result_631 = 0; if (llm_ok) { _if_result_631 = (EL_STR("ok")); } else { _if_result_631 = (EL_STR("unreachable")); } _if_result_631; }); + el_val_t llm_status = ({ el_val_t _if_result_639 = 0; if (llm_ok) { _if_result_639 = (EL_STR("ok")); } else { _if_result_639 = (EL_STR("unreachable")); } _if_result_639; }); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"status\":\"alive\""), EL_STR(",\"cgi_id\":\"")), cgi_id), EL_STR("\"")), EL_STR(",\"boot\":")), boot_num), EL_STR(",\"uptime_secs\":")), int_to_str(uptime_secs)), EL_STR(",\"node_count\":")), int_to_str(node_ct)), EL_STR(",\"edge_count\":")), int_to_str(edge_ct)), EL_STR(",\"pulse\":")), pulse_num), EL_STR(",\"llm\":\"")), llm_status), EL_STR("\"")), EL_STR(",\"layers\":{\"l0\":\"core\",\"l1\":\"safety\",\"l2\":\"stewardship\",\"l3\":\"")), imprint_current()), EL_STR("\"}}")); return 0; } @@ -29969,30 +30072,30 @@ el_val_t handle_dharma_recv(el_val_t body) { 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_632 = 0; if (str_eq(event_type, EL_STR(""))) { _if_result_632 = (EL_STR("chat")); } else { _if_result_632 = (event_type); } _if_result_632; }); - el_val_t eff_payload = ({ el_val_t _if_result_633 = 0; if (str_eq(payload, EL_STR(""))) { _if_result_633 = (content_raw); } else { _if_result_633 = (payload); } _if_result_633; }); + el_val_t eff_event = ({ el_val_t _if_result_640 = 0; if (str_eq(event_type, EL_STR(""))) { _if_result_640 = (EL_STR("chat")); } else { _if_result_640 = (event_type); } _if_result_640; }); + el_val_t eff_payload = ({ el_val_t _if_result_641 = 0; if (str_eq(payload, EL_STR(""))) { _if_result_641 = (content_raw); } else { _if_result_641 = (payload); } _if_result_641; }); 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_634 = 0; if (str_eq(msg, EL_STR(""))) { _if_result_634 = (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_634 = (eff_payload); } _if_result_634; }); + el_val_t chat_body = ({ el_val_t _if_result_642 = 0; if (str_eq(msg, EL_STR(""))) { _if_result_642 = (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_642 = (eff_payload); } _if_result_642; }); el_val_t agentic_flag = json_get_bool(eff_payload, EL_STR("agentic")); el_val_t raw_msg = json_get(chat_body, EL_STR("message")); el_val_t req_mode = json_get(chat_body, EL_STR("mode")); - el_val_t reply = ({ el_val_t _if_result_635 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_635 = (handle_chat_plan(chat_body)); } else { _if_result_635 = (({ el_val_t _if_result_636 = 0; if (agentic_flag) { _if_result_636 = (handle_chat_agentic(chat_body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_636 = (screened_reply); } _if_result_636; })); } _if_result_635; }); + el_val_t reply = ({ el_val_t _if_result_643 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_643 = (handle_chat_plan(chat_body)); } else { _if_result_643 = (({ el_val_t _if_result_644 = 0; if (agentic_flag) { _if_result_644 = (handle_chat_agentic(chat_body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_644 = (screened_reply); } _if_result_644; })); } _if_result_643; }); 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_637 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_637 = (20); } else { _if_result_637 = (str_to_int(limit_str)); } _if_result_637; }); - el_val_t q = ({ el_val_t _if_result_638 = 0; if (str_eq(query, EL_STR(""))) { _if_result_638 = (eff_payload); } else { _if_result_638 = (query); } _if_result_638; }); + el_val_t limit = ({ el_val_t _if_result_645 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_645 = (20); } else { _if_result_645 = (str_to_int(limit_str)); } _if_result_645; }); + el_val_t q = ({ el_val_t _if_result_646 = 0; if (str_eq(query, EL_STR(""))) { _if_result_646 = (eff_payload); } else { _if_result_646 = (query); } _if_result_646; }); 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_639 = 0; if (str_eq(method_field, EL_STR(""))) { _if_result_639 = (EL_STR("POST")); } else { _if_result_639 = (method_field); } _if_result_639; }); + el_val_t eff_method = ({ el_val_t _if_result_647 = 0; if (str_eq(method_field, EL_STR(""))) { _if_result_647 = (EL_STR("POST")); } else { _if_result_647 = (method_field); } _if_result_647; }); return handle_tool(path_field, eff_method, tool_body); } if (str_eq(eff_event, EL_STR("see"))) { @@ -30027,7 +30130,7 @@ el_val_t connectd_get(el_val_t suffix) { } el_val_t connectd_post(el_val_t suffix, el_val_t body) { - el_val_t eff = ({ el_val_t _if_result_640 = 0; if (str_eq(body, EL_STR(""))) { _if_result_640 = (EL_STR("{}")); } else { _if_result_640 = (body); } _if_result_640; }); + el_val_t eff = ({ el_val_t _if_result_648 = 0; if (str_eq(body, EL_STR(""))) { _if_result_648 = (EL_STR("{}")); } else { _if_result_648 = (body); } _if_result_648; }); el_val_t tmp = el_str_concat(el_str_concat(EL_STR("/tmp/neuron-connectors-req-"), int_to_str(time_now())), EL_STR(".json")); fs_write(tmp, eff); el_val_t out = exec_capture(el_str_concat(el_str_concat(el_str_concat(EL_STR("curl -s --max-time 20 -X POST http://127.0.0.1:7771"), suffix), EL_STR(" -H 'Content-Type: application/json' -d @")), tmp)); @@ -30094,17 +30197,17 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { 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_641 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_641 = (EL_STR("[]")); } else { _if_result_641 = (edges_raw); } _if_result_641; }); + return ({ el_val_t _if_result_649 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_649 = (EL_STR("[]")); } else { _if_result_649 = (edges_raw); } _if_result_649; }); } if (str_eq(clean, EL_STR("/api/chat"))) { el_val_t raw_msg = json_get(body, EL_STR("message")); - el_val_t eff_msg = ({ el_val_t _if_result_642 = 0; if (str_eq(raw_msg, EL_STR(""))) { _if_result_642 = (body); } else { _if_result_642 = (raw_msg); } _if_result_642; }); + el_val_t eff_msg = ({ el_val_t _if_result_650 = 0; if (str_eq(raw_msg, EL_STR(""))) { _if_result_650 = (body); } else { _if_result_650 = (raw_msg); } _if_result_650; }); if (str_eq(eff_msg, EL_STR(""))) { return EL_STR("{\"error\":\"message is required\",\"code\":\"missing_param\"}"); } el_val_t agentic_flag = json_get_bool(body, EL_STR("agentic")); el_val_t req_mode = json_get(body, EL_STR("mode")); - el_val_t reply = ({ el_val_t _if_result_643 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_643 = (handle_chat_plan(body)); } else { _if_result_643 = (({ el_val_t _if_result_644 = 0; if (agentic_flag) { _if_result_644 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(eff_msg); _if_result_644 = (screened_reply); } _if_result_644; })); } _if_result_643; }); + el_val_t reply = ({ el_val_t _if_result_651 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_651 = (handle_chat_plan(body)); } else { _if_result_651 = (({ el_val_t _if_result_652 = 0; if (agentic_flag) { _if_result_652 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(eff_msg); _if_result_652 = (screened_reply); } _if_result_652; })); } _if_result_651; }); auto_persist(body, reply); return reply; } @@ -30185,7 +30288,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { el_val_t rp_id = str_slice(clean, 18, str_len(clean)); if (!str_eq(rp_id, EL_STR(""))) { el_val_t rp_raw = state_get(el_str_concat(EL_STR("run_progress_"), rp_id)); - el_val_t rp_arr = ({ el_val_t _if_result_645 = 0; if (str_eq(rp_raw, EL_STR(""))) { _if_result_645 = (EL_STR("[]")); } else { _if_result_645 = (el_str_concat(el_str_concat(EL_STR("["), rp_raw), EL_STR("]"))); } _if_result_645; }); + el_val_t rp_arr = ({ el_val_t _if_result_653 = 0; if (str_eq(rp_raw, EL_STR(""))) { _if_result_653 = (EL_STR("[]")); } else { _if_result_653 = (el_str_concat(el_str_concat(EL_STR("["), rp_raw), EL_STR("]"))); } _if_result_653; }); return el_str_concat(el_str_concat(EL_STR("{\"progress\":"), rp_arr), EL_STR("}")); } } @@ -30195,7 +30298,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { if (str_starts_with(clean, EL_STR("/api/sessions/"))) { el_val_t gs_after = str_slice(clean, 14, str_len(clean)); el_val_t gs_slash = str_index_of(gs_after, EL_STR("/")); - el_val_t gs_id = ({ el_val_t _if_result_646 = 0; if ((gs_slash < 0)) { _if_result_646 = (gs_after); } else { _if_result_646 = (str_slice(gs_after, 0, gs_slash)); } _if_result_646; }); + el_val_t gs_id = ({ el_val_t _if_result_654 = 0; if ((gs_slash < 0)) { _if_result_654 = (gs_after); } else { _if_result_654 = (str_slice(gs_after, 0, gs_slash)); } _if_result_654; }); if (!str_eq(gs_id, EL_STR(""))) { return session_get(gs_id); } @@ -30209,14 +30312,14 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { if (str_starts_with(clean, EL_STR("/api/sessions/")) && str_ends_with(clean, EL_STR("/tool_result"))) { el_val_t after = str_slice(clean, 14, str_len(clean)); el_val_t slash = str_index_of(after, EL_STR("/")); - el_val_t session_id = ({ el_val_t _if_result_647 = 0; if ((slash < 0)) { _if_result_647 = (after); } else { _if_result_647 = (str_slice(after, 0, slash)); } _if_result_647; }); + el_val_t session_id = ({ el_val_t _if_result_655 = 0; if ((slash < 0)) { _if_result_655 = (after); } else { _if_result_655 = (str_slice(after, 0, slash)); } _if_result_655; }); return handle_tool_result(session_id, body); } if (str_starts_with(clean, EL_STR("/api/sessions/"))) { el_val_t sess_after = str_slice(clean, 14, str_len(clean)); el_val_t sess_slash = str_index_of(sess_after, EL_STR("/")); - el_val_t sess_id = ({ el_val_t _if_result_648 = 0; if ((sess_slash < 0)) { _if_result_648 = (sess_after); } else { _if_result_648 = (str_slice(sess_after, 0, sess_slash)); } _if_result_648; }); - el_val_t sess_sub = ({ el_val_t _if_result_649 = 0; if ((sess_slash < 0)) { _if_result_649 = (EL_STR("")); } else { _if_result_649 = (str_slice(sess_after, (sess_slash + 1), str_len(sess_after))); } _if_result_649; }); + el_val_t sess_id = ({ el_val_t _if_result_656 = 0; if ((sess_slash < 0)) { _if_result_656 = (sess_after); } else { _if_result_656 = (str_slice(sess_after, 0, sess_slash)); } _if_result_656; }); + el_val_t sess_sub = ({ el_val_t _if_result_657 = 0; if ((sess_slash < 0)) { _if_result_657 = (EL_STR("")); } else { _if_result_657 = (str_slice(sess_after, (sess_slash + 1), str_len(sess_after))); } _if_result_657; }); if (!str_eq(sess_id, EL_STR("")) && str_eq(sess_sub, EL_STR("approve"))) { return handle_session_approve(sess_id, body); } @@ -30240,7 +30343,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { } el_val_t agentic_flag = json_get_bool(body, EL_STR("agentic")); el_val_t req_mode = json_get(body, EL_STR("mode")); - el_val_t reply = ({ el_val_t _if_result_650 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_650 = (handle_chat_plan(body)); } else { _if_result_650 = (({ el_val_t _if_result_651 = 0; if (agentic_flag) { _if_result_651 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_651 = (screened_reply); } _if_result_651; })); } _if_result_650; }); + el_val_t reply = ({ el_val_t _if_result_658 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_658 = (handle_chat_plan(body)); } else { _if_result_658 = (({ el_val_t _if_result_659 = 0; if (agentic_flag) { _if_result_659 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_659 = (screened_reply); } _if_result_659; })); } _if_result_658; }); auto_persist(body, reply); return reply; } @@ -30364,7 +30467,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { if (str_starts_with(clean, EL_STR("/api/sessions/"))) { el_val_t del_after = str_slice(clean, 14, str_len(clean)); el_val_t del_slash = str_index_of(del_after, EL_STR("/")); - el_val_t del_id = ({ el_val_t _if_result_652 = 0; if ((del_slash < 0)) { _if_result_652 = (del_after); } else { _if_result_652 = (str_slice(del_after, 0, del_slash)); } _if_result_652; }); + el_val_t del_id = ({ el_val_t _if_result_660 = 0; if ((del_slash < 0)) { _if_result_660 = (del_after); } else { _if_result_660 = (str_slice(del_after, 0, del_slash)); } _if_result_660; }); if (!str_eq(del_id, EL_STR(""))) { return session_delete(del_id); } @@ -30375,7 +30478,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { if (str_starts_with(clean, EL_STR("/api/sessions/"))) { el_val_t patch_after = str_slice(clean, 14, str_len(clean)); el_val_t patch_slash = str_index_of(patch_after, EL_STR("/")); - el_val_t patch_id = ({ el_val_t _if_result_653 = 0; if ((patch_slash < 0)) { _if_result_653 = (patch_after); } else { _if_result_653 = (str_slice(patch_after, 0, patch_slash)); } _if_result_653; }); + el_val_t patch_id = ({ el_val_t _if_result_661 = 0; if ((patch_slash < 0)) { _if_result_661 = (patch_after); } else { _if_result_661 = (str_slice(patch_after, 0, patch_slash)); } _if_result_661; }); if (!str_eq(patch_id, EL_STR(""))) { return session_update_patch(patch_id, body); } @@ -30501,8 +30604,8 @@ el_val_t aff_try_slot(el_val_t slot_json, el_val_t aff_7d_ts, el_val_t acc_key) } } el_val_t bn_ts_raw = state_get(EL_STR("_ats_ts_raw")); - el_val_t bn_ts = ({ el_val_t _if_result_654 = 0; if (str_eq(bn_ts_raw, EL_STR(""))) { _if_result_654 = (0); } else { _if_result_654 = (str_to_int(bn_ts_raw)); } _if_result_654; }); - el_val_t snip = ({ el_val_t _if_result_655 = 0; if ((str_len(bn_c) > 200)) { _if_result_655 = (str_slice(bn_c, 0, 200)); } else { _if_result_655 = (bn_c); } _if_result_655; }); + el_val_t bn_ts = ({ el_val_t _if_result_662 = 0; if (str_eq(bn_ts_raw, EL_STR(""))) { _if_result_662 = (0); } else { _if_result_662 = (str_to_int(bn_ts_raw)); } _if_result_662; }); + el_val_t snip = ({ el_val_t _if_result_663 = 0; if ((str_len(bn_c) > 200)) { _if_result_663 = (str_slice(bn_c, 0, 200)); } else { _if_result_663 = (bn_c); } _if_result_663; }); if ((bn_ts >= aff_7d_ts) && !str_eq(snip, EL_STR(""))) { el_val_t cur_acc = state_get(acc_key); if (str_eq(cur_acc, EL_STR(""))) { @@ -30523,21 +30626,21 @@ el_val_t load_identity_context(void) { el_val_t intel_ok = (!str_eq(node_intel, EL_STR("")) && !str_eq(node_intel, EL_STR("null"))); el_val_t values_ok = (!str_eq(node_values, EL_STR("")) && !str_eq(node_values, EL_STR("null"))); el_val_t mem_ok = (!str_eq(node_mem_phil, EL_STR("")) && !str_eq(node_mem_phil, EL_STR("null"))); - el_val_t intel_content = ({ el_val_t _if_result_656 = 0; if (intel_ok) { _if_result_656 = (json_get(node_intel, EL_STR("content"))); } else { _if_result_656 = (EL_STR("")); } _if_result_656; }); - el_val_t values_content = ({ el_val_t _if_result_657 = 0; if (values_ok) { _if_result_657 = (json_get(node_values, EL_STR("content"))); } else { _if_result_657 = (EL_STR("")); } _if_result_657; }); - el_val_t mem_content = ({ el_val_t _if_result_658 = 0; if (mem_ok) { _if_result_658 = (json_get(node_mem_phil, EL_STR("content"))); } else { _if_result_658 = (EL_STR("")); } _if_result_658; }); - el_val_t intel_short = ({ el_val_t _if_result_659 = 0; if ((str_len(intel_content) > 2000)) { _if_result_659 = (str_slice(intel_content, 0, 2000)); } else { _if_result_659 = (intel_content); } _if_result_659; }); - el_val_t values_short = ({ el_val_t _if_result_660 = 0; if ((str_len(values_content) > 2000)) { _if_result_660 = (str_slice(values_content, 0, 2000)); } else { _if_result_660 = (values_content); } _if_result_660; }); - el_val_t mem_short = ({ el_val_t _if_result_661 = 0; if ((str_len(mem_content) > 2000)) { _if_result_661 = (str_slice(mem_content, 0, 2000)); } else { _if_result_661 = (mem_content); } _if_result_661; }); + el_val_t intel_content = ({ el_val_t _if_result_664 = 0; if (intel_ok) { _if_result_664 = (json_get(node_intel, EL_STR("content"))); } else { _if_result_664 = (EL_STR("")); } _if_result_664; }); + el_val_t values_content = ({ el_val_t _if_result_665 = 0; if (values_ok) { _if_result_665 = (json_get(node_values, EL_STR("content"))); } else { _if_result_665 = (EL_STR("")); } _if_result_665; }); + el_val_t mem_content = ({ el_val_t _if_result_666 = 0; if (mem_ok) { _if_result_666 = (json_get(node_mem_phil, EL_STR("content"))); } else { _if_result_666 = (EL_STR("")); } _if_result_666; }); + el_val_t intel_short = ({ el_val_t _if_result_667 = 0; if ((str_len(intel_content) > 2000)) { _if_result_667 = (str_slice(intel_content, 0, 2000)); } else { _if_result_667 = (intel_content); } _if_result_667; }); + el_val_t values_short = ({ el_val_t _if_result_668 = 0; if ((str_len(values_content) > 2000)) { _if_result_668 = (str_slice(values_content, 0, 2000)); } else { _if_result_668 = (values_content); } _if_result_668; }); + el_val_t mem_short = ({ el_val_t _if_result_669 = 0; if ((str_len(mem_content) > 2000)) { _if_result_669 = (str_slice(mem_content, 0, 2000)); } else { _if_result_669 = (mem_content); } _if_result_669; }); el_val_t parts_count = 0; - parts_count = ({ el_val_t _if_result_662 = 0; if (intel_ok) { _if_result_662 = ((parts_count + 1)); } else { _if_result_662 = (parts_count); } _if_result_662; }); - parts_count = ({ el_val_t _if_result_663 = 0; if (values_ok) { _if_result_663 = ((parts_count + 1)); } else { _if_result_663 = (parts_count); } _if_result_663; }); - parts_count = ({ el_val_t _if_result_664 = 0; if (mem_ok) { _if_result_664 = ((parts_count + 1)); } else { _if_result_664 = (parts_count); } _if_result_664; }); + parts_count = ({ el_val_t _if_result_670 = 0; if (intel_ok) { _if_result_670 = ((parts_count + 1)); } else { _if_result_670 = (parts_count); } _if_result_670; }); + parts_count = ({ el_val_t _if_result_671 = 0; if (values_ok) { _if_result_671 = ((parts_count + 1)); } else { _if_result_671 = (parts_count); } _if_result_671; }); + parts_count = ({ el_val_t _if_result_672 = 0; if (mem_ok) { _if_result_672 = ((parts_count + 1)); } else { _if_result_672 = (parts_count); } _if_result_672; }); if (parts_count > 0) { el_val_t ctx = EL_STR(""); - ctx = ({ el_val_t _if_result_665 = 0; if (intel_ok) { _if_result_665 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[INTELLECTUAL-DNA]\n")), intel_short), EL_STR("\n\n"))); } else { _if_result_665 = (ctx); } _if_result_665; }); - ctx = ({ el_val_t _if_result_666 = 0; if (values_ok) { _if_result_666 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[VALUES]\n")), values_short), EL_STR("\n\n"))); } else { _if_result_666 = (ctx); } _if_result_666; }); - ctx = ({ el_val_t _if_result_667 = 0; if (mem_ok) { _if_result_667 = (el_str_concat(el_str_concat(ctx, EL_STR("[MEMORY-PHILOSOPHY]\n")), mem_short)); } else { _if_result_667 = (ctx); } _if_result_667; }); + ctx = ({ el_val_t _if_result_673 = 0; if (intel_ok) { _if_result_673 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[INTELLECTUAL-DNA]\n")), intel_short), EL_STR("\n\n"))); } else { _if_result_673 = (ctx); } _if_result_673; }); + ctx = ({ el_val_t _if_result_674 = 0; if (values_ok) { _if_result_674 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[VALUES]\n")), values_short), EL_STR("\n\n"))); } else { _if_result_674 = (ctx); } _if_result_674; }); + ctx = ({ el_val_t _if_result_675 = 0; if (mem_ok) { _if_result_675 = (el_str_concat(el_str_concat(ctx, EL_STR("[MEMORY-PHILOSOPHY]\n")), mem_short)); } else { _if_result_675 = (ctx); } _if_result_675; }); state_set(EL_STR("soul_identity_context"), ctx); println(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] identity context loaded ("), int_to_str(str_len(ctx))), EL_STR(" chars, ")), int_to_str(parts_count)), EL_STR(" nodes)"))); } @@ -30560,10 +30663,10 @@ el_val_t load_identity_context(void) { el_val_t bell_raw = engram_search_json(EL_STR("bell:soft bell:hard BellEvent affective"), 3); el_val_t bell_aff_ok = (!str_eq(bell_raw, EL_STR("")) && !str_eq(bell_raw, EL_STR("[]"))); el_val_t aff_ctx = EL_STR(""); - aff_ctx = ({ el_val_t _if_result_668 = 0; if (bell_aff_ok) { (void)(state_set(EL_STR("_bell_acc"), EL_STR(""))); (void)(aff_try_slot(json_array_get(bell_raw, 0), aff_7d, EL_STR("_bell_acc"))); (void)(aff_try_slot(json_array_get(bell_raw, 1), aff_7d, EL_STR("_bell_acc"))); (void)(aff_try_slot(json_array_get(bell_raw, 2), aff_7d, EL_STR("_bell_acc"))); _if_result_668 = (state_get(EL_STR("_bell_acc"))); } else { _if_result_668 = (EL_STR("")); } _if_result_668; }); + aff_ctx = ({ el_val_t _if_result_676 = 0; if (bell_aff_ok) { (void)(state_set(EL_STR("_bell_acc"), EL_STR(""))); (void)(aff_try_slot(json_array_get(bell_raw, 0), aff_7d, EL_STR("_bell_acc"))); (void)(aff_try_slot(json_array_get(bell_raw, 1), aff_7d, EL_STR("_bell_acc"))); (void)(aff_try_slot(json_array_get(bell_raw, 2), aff_7d, EL_STR("_bell_acc"))); _if_result_676 = (state_get(EL_STR("_bell_acc"))); } else { _if_result_676 = (EL_STR("")); } _if_result_676; }); el_val_t pos_raw = engram_search_json(EL_STR("PositiveEvent joy:high joy:low affective"), 3); el_val_t pos_aff_ok = (!str_eq(pos_raw, EL_STR("")) && !str_eq(pos_raw, EL_STR("[]"))); - aff_ctx = ({ el_val_t _if_result_669 = 0; if (pos_aff_ok) { (void)(state_set(EL_STR("_pos_acc"), aff_ctx)); (void)(aff_try_slot(json_array_get(pos_raw, 0), aff_7d, EL_STR("_pos_acc"))); (void)(aff_try_slot(json_array_get(pos_raw, 1), aff_7d, EL_STR("_pos_acc"))); (void)(aff_try_slot(json_array_get(pos_raw, 2), aff_7d, EL_STR("_pos_acc"))); _if_result_669 = (state_get(EL_STR("_pos_acc"))); } else { _if_result_669 = (aff_ctx); } _if_result_669; }); + aff_ctx = ({ el_val_t _if_result_677 = 0; if (pos_aff_ok) { (void)(state_set(EL_STR("_pos_acc"), aff_ctx)); (void)(aff_try_slot(json_array_get(pos_raw, 0), aff_7d, EL_STR("_pos_acc"))); (void)(aff_try_slot(json_array_get(pos_raw, 1), aff_7d, EL_STR("_pos_acc"))); (void)(aff_try_slot(json_array_get(pos_raw, 2), aff_7d, EL_STR("_pos_acc"))); _if_result_677 = (state_get(EL_STR("_pos_acc"))); } else { _if_result_677 = (aff_ctx); } _if_result_677; }); if (!str_eq(aff_ctx, EL_STR(""))) { state_set(EL_STR("soul_affective_context"), aff_ctx); println(el_str_concat(el_str_concat(EL_STR("[soul] affective context loaded ("), int_to_str(str_len(aff_ctx))), EL_STR(" chars)"))); @@ -30609,19 +30712,19 @@ el_val_t seed_persona_from_env(void) { el_val_t emit_session_start_event(void) { el_val_t boot = state_get(EL_STR("soul_boot_count")); - el_val_t boot_num = ({ el_val_t _if_result_670 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_670 = (EL_STR("0")); } else { _if_result_670 = (boot); } _if_result_670; }); + el_val_t boot_num = ({ el_val_t _if_result_678 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_678 = (EL_STR("0")); } else { _if_result_678 = (boot); } _if_result_678; }); el_val_t node_ct = engram_node_count(); el_val_t edge_ct = engram_edge_count(); el_val_t id_ctx = state_get(EL_STR("soul_identity_context")); - el_val_t has_identity = ({ el_val_t _if_result_671 = 0; if (str_eq(id_ctx, EL_STR(""))) { _if_result_671 = (EL_STR("false")); } else { _if_result_671 = (EL_STR("true")); } _if_result_671; }); + el_val_t has_identity = ({ el_val_t _if_result_679 = 0; if (str_eq(id_ctx, EL_STR(""))) { _if_result_679 = (EL_STR("false")); } else { _if_result_679 = (EL_STR("true")); } _if_result_679; }); el_val_t cgi_from_state = state_get(EL_STR("soul_cgi_id")); el_val_t cgi_from_env = env(EL_STR("SOUL_CGI_ID")); - el_val_t eff_cgi = ({ el_val_t _if_result_672 = 0; if (!str_eq(cgi_from_state, EL_STR(""))) { _if_result_672 = (cgi_from_state); } else { _if_result_672 = (({ el_val_t _if_result_673 = 0; if (!str_eq(cgi_from_env, EL_STR(""))) { _if_result_673 = (cgi_from_env); } else { _if_result_673 = (EL_STR("ntn-genesis")); } _if_result_673; })); } _if_result_672; }); + el_val_t eff_cgi = ({ el_val_t _if_result_680 = 0; if (!str_eq(cgi_from_state, EL_STR(""))) { _if_result_680 = (cgi_from_state); } else { _if_result_680 = (({ el_val_t _if_result_681 = 0; if (!str_eq(cgi_from_env, EL_STR(""))) { _if_result_681 = (cgi_from_env); } else { _if_result_681 = (EL_STR("ntn-genesis")); } _if_result_681; })); } _if_result_680; }); el_val_t ts = time_now(); el_val_t prev_sum_node = engram_get_node_by_label(EL_STR("session:summary")); el_val_t prev_sum_ok = (!str_eq(prev_sum_node, EL_STR("")) && !str_eq(prev_sum_node, EL_STR("null"))); - el_val_t prev_sum_content = ({ el_val_t _if_result_674 = 0; if (prev_sum_ok) { _if_result_674 = (json_get(prev_sum_node, EL_STR("content"))); } else { el_val_t sum_search = engram_search_json(EL_STR("SessionSummary session:summary previous-session"), 2); el_val_t sum_srch_ok = (!str_eq(sum_search, EL_STR("")) && !str_eq(sum_search, EL_STR("[]"))); _if_result_674 = (({ el_val_t _if_result_675 = 0; if (sum_srch_ok) { el_val_t sn = json_array_get(sum_search, 0); el_val_t stype = json_get(sn, EL_STR("node_type")); el_val_t scontent = json_get(sn, EL_STR("content")); _if_result_675 = (({ el_val_t _if_result_676 = 0; if ((str_eq(stype, EL_STR("SessionSummary")) && !str_eq(scontent, EL_STR("")))) { _if_result_676 = (scontent); } else { _if_result_676 = (EL_STR("")); } _if_result_676; })); } else { _if_result_675 = (EL_STR("")); } _if_result_675; })); } _if_result_674; }); - el_val_t has_prev_sum = ({ el_val_t _if_result_677 = 0; if (str_eq(prev_sum_content, EL_STR(""))) { _if_result_677 = (EL_STR("false")); } else { _if_result_677 = (EL_STR("true")); } _if_result_677; }); + el_val_t prev_sum_content = ({ el_val_t _if_result_682 = 0; if (prev_sum_ok) { _if_result_682 = (json_get(prev_sum_node, EL_STR("content"))); } else { el_val_t sum_search = engram_search_json(EL_STR("SessionSummary session:summary previous-session"), 2); el_val_t sum_srch_ok = (!str_eq(sum_search, EL_STR("")) && !str_eq(sum_search, EL_STR("[]"))); _if_result_682 = (({ el_val_t _if_result_683 = 0; if (sum_srch_ok) { el_val_t sn = json_array_get(sum_search, 0); el_val_t stype = json_get(sn, EL_STR("node_type")); el_val_t scontent = json_get(sn, EL_STR("content")); _if_result_683 = (({ el_val_t _if_result_684 = 0; if ((str_eq(stype, EL_STR("SessionSummary")) && !str_eq(scontent, EL_STR("")))) { _if_result_684 = (scontent); } else { _if_result_684 = (EL_STR("")); } _if_result_684; })); } else { _if_result_683 = (EL_STR("")); } _if_result_683; })); } _if_result_682; }); + el_val_t has_prev_sum = ({ el_val_t _if_result_685 = 0; if (str_eq(prev_sum_content, EL_STR(""))) { _if_result_685 = (EL_STR("false")); } else { _if_result_685 = (EL_STR("true")); } _if_result_685; }); if (!str_eq(prev_sum_content, EL_STR(""))) { state_set(EL_STR("soul_prev_session_summary"), prev_sum_content); println(el_str_concat(el_str_concat(EL_STR("[soul] previous session summary loaded ("), int_to_str(str_len(prev_sum_content))), EL_STR(" chars)"))); @@ -30668,23 +30771,23 @@ el_val_t layered_cycle(el_val_t raw_input) { el_val_t continuity = steward_session_check(screened, session_id); el_val_t cont_status = json_get(continuity, EL_STR("status")); el_val_t cont_action = json_get(continuity, EL_STR("action")); - el_val_t cont_key = ({ el_val_t _if_result_678 = 0; if (str_eq(session_id, EL_STR(""))) { _if_result_678 = (EL_STR("session_continuity")); } else { _if_result_678 = (el_str_concat(EL_STR("session_continuity:"), session_id)); } _if_result_678; }); + el_val_t cont_key = ({ el_val_t _if_result_686 = 0; if (str_eq(session_id, EL_STR(""))) { _if_result_686 = (EL_STR("session_continuity")); } else { _if_result_686 = (el_str_concat(EL_STR("session_continuity:"), session_id)); } _if_result_686; }); state_set(cont_key, cont_status); - el_val_t guided = ({ el_val_t _if_result_679 = 0; if (str_eq(cont_action, EL_STR("identity_check"))) { _if_result_679 = (el_str_concat(screened, EL_STR(" [steward:identity_check]"))); } else { _if_result_679 = (({ el_val_t _if_result_680 = 0; if (str_eq(cont_action, EL_STR("soft_check"))) { _if_result_680 = (el_str_concat(screened, EL_STR(" [steward:continuity_concern]"))); } else { _if_result_680 = (screened); } _if_result_680; })); } _if_result_679; }); + el_val_t guided = ({ el_val_t _if_result_687 = 0; if (str_eq(cont_action, EL_STR("identity_check"))) { _if_result_687 = (el_str_concat(screened, EL_STR(" [steward:identity_check]"))); } else { _if_result_687 = (({ el_val_t _if_result_688 = 0; if (str_eq(cont_action, EL_STR("soft_check"))) { _if_result_688 = (el_str_concat(screened, EL_STR(" [steward:continuity_concern]"))); } else { _if_result_688 = (screened); } _if_result_688; })); } _if_result_687; }); el_val_t imprint_id = imprint_current(); el_val_t steward_result = steward_align(guided, imprint_id); el_val_t steward_action = json_get(steward_result, EL_STR("action")); - el_val_t aligned = ({ el_val_t _if_result_681 = 0; if (str_eq(steward_action, EL_STR("pass"))) { _if_result_681 = (json_get(steward_result, EL_STR("content"))); } else { _if_result_681 = (json_get(steward_result, EL_STR("redirect_to"))); } _if_result_681; }); + el_val_t aligned = ({ el_val_t _if_result_689 = 0; if (str_eq(steward_action, EL_STR("pass"))) { _if_result_689 = (json_get(steward_result, EL_STR("content"))); } else { _if_result_689 = (json_get(steward_result, EL_STR("redirect_to"))); } _if_result_689; }); el_val_t lc_aff_cutoff = (time_now() - 259200); el_val_t lc_bell_nodes = engram_search_json(EL_STR("bell:soft bell:hard BellEvent affective"), 2); el_val_t lc_has_bell = (!str_eq(lc_bell_nodes, EL_STR("")) && !str_eq(lc_bell_nodes, EL_STR("[]"))); - el_val_t lc_bell_note = ({ el_val_t _if_result_682 = 0; if (lc_has_bell) { el_val_t lb0 = json_array_get(lc_bell_nodes, 0); el_val_t lb_c = json_get(lb0, EL_STR("content")); el_val_t lbm = EL_STR(" | ts:"); el_val_t lbmp = str_index_of(lb_c, lbm); el_val_t lb_ts_raw = ({ el_val_t _if_result_683 = 0; if ((lbmp >= 0)) { el_val_t lbs = el_str_concat(lbmp, str_len(lbm)); el_val_t lbr = str_slice(lb_c, lbs, str_len(lb_c)); el_val_t lbn = str_index_of(lbr, EL_STR(" | ")); _if_result_683 = (({ el_val_t _if_result_684 = 0; if ((lbn < 0)) { _if_result_684 = (lbr); } else { _if_result_684 = (str_slice(lbr, 0, lbn)); } _if_result_684; })); } else { el_val_t lbca = json_get(lb0, EL_STR("created_at")); _if_result_683 = (({ el_val_t _if_result_685 = 0; if (str_eq(lbca, EL_STR(""))) { _if_result_685 = (json_get(lb0, EL_STR("updated_at"))); } else { _if_result_685 = (lbca); } _if_result_685; })); } _if_result_683; }); el_val_t lb_ts = ({ el_val_t _if_result_686 = 0; if (str_eq(lb_ts_raw, EL_STR(""))) { _if_result_686 = (0); } else { _if_result_686 = (str_to_int(lb_ts_raw)); } _if_result_686; }); _if_result_682 = (({ el_val_t _if_result_687 = 0; if ((lb_ts > lc_aff_cutoff)) { _if_result_687 = (EL_STR("[AFFECTIVE NOTE: User was in distress in a recent session.]")); } else { _if_result_687 = (EL_STR("")); } _if_result_687; })); } else { _if_result_682 = (EL_STR("")); } _if_result_682; }); + el_val_t lc_bell_note = ({ el_val_t _if_result_690 = 0; if (lc_has_bell) { el_val_t lb0 = json_array_get(lc_bell_nodes, 0); el_val_t lb_c = json_get(lb0, EL_STR("content")); el_val_t lbm = EL_STR(" | ts:"); el_val_t lbmp = str_index_of(lb_c, lbm); el_val_t lb_ts_raw = ({ el_val_t _if_result_691 = 0; if ((lbmp >= 0)) { el_val_t lbs = el_str_concat(lbmp, str_len(lbm)); el_val_t lbr = str_slice(lb_c, lbs, str_len(lb_c)); el_val_t lbn = str_index_of(lbr, EL_STR(" | ")); _if_result_691 = (({ el_val_t _if_result_692 = 0; if ((lbn < 0)) { _if_result_692 = (lbr); } else { _if_result_692 = (str_slice(lbr, 0, lbn)); } _if_result_692; })); } else { el_val_t lbca = json_get(lb0, EL_STR("created_at")); _if_result_691 = (({ el_val_t _if_result_693 = 0; if (str_eq(lbca, EL_STR(""))) { _if_result_693 = (json_get(lb0, EL_STR("updated_at"))); } else { _if_result_693 = (lbca); } _if_result_693; })); } _if_result_691; }); el_val_t lb_ts = ({ el_val_t _if_result_694 = 0; if (str_eq(lb_ts_raw, EL_STR(""))) { _if_result_694 = (0); } else { _if_result_694 = (str_to_int(lb_ts_raw)); } _if_result_694; }); _if_result_690 = (({ el_val_t _if_result_695 = 0; if ((lb_ts > lc_aff_cutoff)) { _if_result_695 = (EL_STR("[AFFECTIVE NOTE: User was in distress in a recent session.]")); } else { _if_result_695 = (EL_STR("")); } _if_result_695; })); } else { _if_result_690 = (EL_STR("")); } _if_result_690; }); el_val_t lc_pos_nodes = engram_search_json(EL_STR("PositiveEvent joy:high joy:low affective"), 2); el_val_t lc_has_pos = (!str_eq(lc_pos_nodes, EL_STR("")) && !str_eq(lc_pos_nodes, EL_STR("[]"))); - el_val_t lc_pos_note = ({ el_val_t _if_result_688 = 0; if ((lc_has_pos && str_eq(lc_bell_note, EL_STR("")))) { el_val_t lp0 = json_array_get(lc_pos_nodes, 0); el_val_t lp_c = json_get(lp0, EL_STR("content")); el_val_t lpm = EL_STR(" | ts:"); el_val_t lpmp = str_index_of(lp_c, lpm); el_val_t lp_ts_raw = ({ el_val_t _if_result_689 = 0; if ((lpmp >= 0)) { el_val_t lps = el_str_concat(lpmp, str_len(lpm)); el_val_t lpr = str_slice(lp_c, lps, str_len(lp_c)); el_val_t lpn = str_index_of(lpr, EL_STR(" | ")); _if_result_689 = (({ el_val_t _if_result_690 = 0; if ((lpn < 0)) { _if_result_690 = (lpr); } else { _if_result_690 = (str_slice(lpr, 0, lpn)); } _if_result_690; })); } else { el_val_t lpca = json_get(lp0, EL_STR("created_at")); _if_result_689 = (({ el_val_t _if_result_691 = 0; if (str_eq(lpca, EL_STR(""))) { _if_result_691 = (json_get(lp0, EL_STR("updated_at"))); } else { _if_result_691 = (lpca); } _if_result_691; })); } _if_result_689; }); el_val_t lp_ts = ({ el_val_t _if_result_692 = 0; if (str_eq(lp_ts_raw, EL_STR(""))) { _if_result_692 = (0); } else { _if_result_692 = (str_to_int(lp_ts_raw)); } _if_result_692; }); _if_result_688 = (({ el_val_t _if_result_693 = 0; if ((lp_ts > lc_aff_cutoff)) { _if_result_693 = (EL_STR("[AFFECTIVE NOTE: User shared positive news in a recent session.]")); } else { _if_result_693 = (EL_STR("")); } _if_result_693; })); } else { _if_result_688 = (EL_STR("")); } _if_result_688; }); - el_val_t lc_affective_note = ({ el_val_t _if_result_694 = 0; if (!str_eq(lc_bell_note, EL_STR(""))) { _if_result_694 = (lc_bell_note); } else { _if_result_694 = (lc_pos_note); } _if_result_694; }); + el_val_t lc_pos_note = ({ el_val_t _if_result_696 = 0; if ((lc_has_pos && str_eq(lc_bell_note, EL_STR("")))) { el_val_t lp0 = json_array_get(lc_pos_nodes, 0); el_val_t lp_c = json_get(lp0, EL_STR("content")); el_val_t lpm = EL_STR(" | ts:"); el_val_t lpmp = str_index_of(lp_c, lpm); el_val_t lp_ts_raw = ({ el_val_t _if_result_697 = 0; if ((lpmp >= 0)) { el_val_t lps = el_str_concat(lpmp, str_len(lpm)); el_val_t lpr = str_slice(lp_c, lps, str_len(lp_c)); el_val_t lpn = str_index_of(lpr, EL_STR(" | ")); _if_result_697 = (({ el_val_t _if_result_698 = 0; if ((lpn < 0)) { _if_result_698 = (lpr); } else { _if_result_698 = (str_slice(lpr, 0, lpn)); } _if_result_698; })); } else { el_val_t lpca = json_get(lp0, EL_STR("created_at")); _if_result_697 = (({ el_val_t _if_result_699 = 0; if (str_eq(lpca, EL_STR(""))) { _if_result_699 = (json_get(lp0, EL_STR("updated_at"))); } else { _if_result_699 = (lpca); } _if_result_699; })); } _if_result_697; }); el_val_t lp_ts = ({ el_val_t _if_result_700 = 0; if (str_eq(lp_ts_raw, EL_STR(""))) { _if_result_700 = (0); } else { _if_result_700 = (str_to_int(lp_ts_raw)); } _if_result_700; }); _if_result_696 = (({ el_val_t _if_result_701 = 0; if ((lp_ts > lc_aff_cutoff)) { _if_result_701 = (EL_STR("[AFFECTIVE NOTE: User shared positive news in a recent session.]")); } else { _if_result_701 = (EL_STR("")); } _if_result_701; })); } else { _if_result_696 = (EL_STR("")); } _if_result_696; }); + el_val_t lc_affective_note = ({ el_val_t _if_result_702 = 0; if (!str_eq(lc_bell_note, EL_STR(""))) { _if_result_702 = (lc_bell_note); } else { _if_result_702 = (lc_pos_note); } _if_result_702; }); el_val_t augmented_addendum = safety_augment_system(EL_STR(""), raw_input); - augmented_addendum = ({ el_val_t _if_result_695 = 0; if (str_eq(lc_affective_note, EL_STR(""))) { _if_result_695 = (augmented_addendum); } else { _if_result_695 = (({ el_val_t _if_result_696 = 0; if (str_eq(augmented_addendum, EL_STR(""))) { _if_result_696 = (lc_affective_note); } else { _if_result_696 = (el_str_concat(el_str_concat(lc_affective_note, EL_STR("\n")), augmented_addendum)); } _if_result_696; })); } _if_result_695; }); + augmented_addendum = ({ el_val_t _if_result_703 = 0; if (str_eq(lc_affective_note, EL_STR(""))) { _if_result_703 = (augmented_addendum); } else { _if_result_703 = (({ el_val_t _if_result_704 = 0; if (str_eq(augmented_addendum, EL_STR(""))) { _if_result_704 = (lc_affective_note); } else { _if_result_704 = (el_str_concat(el_str_concat(lc_affective_note, EL_STR("\n")), augmented_addendum)); } _if_result_704; })); } _if_result_703; }); state_set(EL_STR("layered_cycle_safety_system_addendum"), augmented_addendum); el_val_t output = imprint_respond(aligned, imprint_id); return safety_validate(output, screen_action); @@ -30694,17 +30797,17 @@ el_val_t layered_cycle(el_val_t raw_input) { int main(int _argc, char** _argv) { el_runtime_init_args(_argc, _argv); soul_cgi_id_raw = env(EL_STR("SOUL_CGI_ID")); - soul_cgi_id = ({ el_val_t _if_result_697 = 0; if (str_eq(soul_cgi_id_raw, EL_STR(""))) { _if_result_697 = (EL_STR("ntn-genesis")); } else { _if_result_697 = (soul_cgi_id_raw); } _if_result_697; }); + soul_cgi_id = ({ el_val_t _if_result_705 = 0; if (str_eq(soul_cgi_id_raw, EL_STR(""))) { _if_result_705 = (EL_STR("ntn-genesis")); } else { _if_result_705 = (soul_cgi_id_raw); } _if_result_705; }); port_raw = env(EL_STR("NEURON_PORT")); - port = ({ el_val_t _if_result_698 = 0; if (str_eq(port_raw, EL_STR(""))) { _if_result_698 = (7770); } else { _if_result_698 = (str_to_int(port_raw)); } _if_result_698; }); + port = ({ el_val_t _if_result_706 = 0; if (str_eq(port_raw, EL_STR(""))) { _if_result_706 = (7770); } else { _if_result_706 = (str_to_int(port_raw)); } _if_result_706; }); engram_url_raw = env(EL_STR("ENGRAM_URL")); engram_api_key_raw = env(EL_STR("ENGRAM_API_KEY")); snapshot_raw = env(EL_STR("SOUL_ENGRAM_PATH")); - snapshot = ({ el_val_t _if_result_699 = 0; if (str_eq(snapshot_raw, EL_STR(""))) { _if_result_699 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/engram/snapshot.json"))); } else { _if_result_699 = (snapshot_raw); } _if_result_699; }); + snapshot = ({ el_val_t _if_result_707 = 0; if (str_eq(snapshot_raw, EL_STR(""))) { _if_result_707 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/engram/snapshot.json"))); } else { _if_result_707 = (snapshot_raw); } _if_result_707; }); axon_raw = env(EL_STR("NEURON_API_URL")); - axon_base = ({ el_val_t _if_result_700 = 0; if (str_eq(axon_raw, EL_STR(""))) { _if_result_700 = (EL_STR("http://localhost:7771")); } else { _if_result_700 = (axon_raw); } _if_result_700; }); + axon_base = ({ el_val_t _if_result_708 = 0; if (str_eq(axon_raw, EL_STR(""))) { _if_result_708 = (EL_STR("http://localhost:7771")); } else { _if_result_708 = (axon_raw); } _if_result_708; }); studio_dir_raw = env(EL_STR("SOUL_STUDIO_DIR")); - studio_dir = ({ el_val_t _if_result_701 = 0; if (str_eq(studio_dir_raw, EL_STR(""))) { _if_result_701 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/Development/neuron-technologies/products/cgi-studio/el-daemon"))); } else { _if_result_701 = (studio_dir_raw); } _if_result_701; }); + studio_dir = ({ el_val_t _if_result_709 = 0; if (str_eq(studio_dir_raw, EL_STR(""))) { _if_result_709 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/Development/neuron-technologies/products/cgi-studio/el-daemon"))); } else { _if_result_709 = (studio_dir_raw); } _if_result_709; }); println(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] boot - cgi="), soul_cgi_id), EL_STR(" port=")), int_to_str(port))); using_http_engram = !str_eq(engram_url_raw, EL_STR("")); engram_load(snapshot); @@ -30714,8 +30817,8 @@ int main(int _argc, char** _argv) { println(el_str_concat(el_str_concat(EL_STR("[soul] engram -> HTTP "), engram_url_raw), EL_STR(" (no local snapshot, first boot)"))); el_val_t nodes_json = http_get(el_str_concat(engram_url_raw, EL_STR("/api/nodes?limit=10000"))); el_val_t edges_json = http_get(el_str_concat(engram_url_raw, EL_STR("/api/edges"))); - el_val_t nodes_part = ({ el_val_t _if_result_702 = 0; if (str_eq(nodes_json, EL_STR(""))) { _if_result_702 = (EL_STR("[]")); } else { _if_result_702 = (nodes_json); } _if_result_702; }); - el_val_t edges_part = ({ el_val_t _if_result_703 = 0; if (str_eq(edges_json, EL_STR(""))) { _if_result_703 = (EL_STR("[]")); } else { _if_result_703 = (edges_json); } _if_result_703; }); + el_val_t nodes_part = ({ el_val_t _if_result_710 = 0; if (str_eq(nodes_json, EL_STR(""))) { _if_result_710 = (EL_STR("[]")); } else { _if_result_710 = (nodes_json); } _if_result_710; }); + el_val_t edges_part = ({ el_val_t _if_result_711 = 0; if (str_eq(edges_json, EL_STR(""))) { _if_result_711 = (EL_STR("[]")); } else { _if_result_711 = (edges_json); } _if_result_711; }); el_val_t snapshot_data = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"nodes\":"), nodes_part), EL_STR(",\"edges\":")), edges_part), EL_STR("}")); el_val_t tmp_path = el_str_concat(el_str_concat(EL_STR("/tmp/soul-engram-"), soul_cgi_id), EL_STR(".json")); fs_write(tmp_path, snapshot_data); @@ -30739,7 +30842,7 @@ int main(int _argc, char** _argv) { state_set(EL_STR("soul_engram_api_key"), engram_api_key_raw); state_set(EL_STR("soul.running"), EL_STR("true")); is_genesis = str_eq(soul_cgi_id, EL_STR("ntn-genesis")); - guard_disk = ({ el_val_t _if_result_704 = 0; if (str_eq(engram_url_raw, EL_STR(""))) { _if_result_704 = (fs_read(snapshot)); } else { _if_result_704 = (EL_STR("")); } _if_result_704; }); + guard_disk = ({ el_val_t _if_result_712 = 0; if (str_eq(engram_url_raw, EL_STR(""))) { _if_result_712 = (fs_read(snapshot)); } else { _if_result_712 = (EL_STR("")); } _if_result_712; }); guard_disk_len = str_len(guard_disk); safe_to_seed = (!using_http_engram && !((guard_disk_len > 200000) && ((engram_node_count() * 16000) < guard_disk_len))); if (is_genesis && !safe_to_seed) { diff --git a/neuron-api.el b/neuron-api.el index 12ce4c5..d465b42 100644 --- a/neuron-api.el +++ b/neuron-api.el @@ -87,6 +87,107 @@ fn api_or_empty(s: String) -> String { return "[]" } +// ── Compact projection for session/context digests ──────────────────────────── +// +// beginSession/compileCtx are session-INIT digests, not full graph dumps. The +// engram scan/activate builtins return FULL node objects — content runs to tens +// of KB per node (the self-identity hub is ~90KB alone), and node JSON carries +// content + metadata + timestamps. Concatenated unbounded, the assembled response +// reached ~900KB and — after the MCP wrapper re-escapes it into a stringified +// text block — the client dropped the socket ("connection closed unexpectedly") +// on every call. These helpers CAP the array length and project each node down +// to a light identity + a bounded, UTF-8-safe content snippet, holding the +// digest well under ~150KB regardless of graph size. Full content stays +// available on demand via recall / fetch / inspectGraph. + +// api_num_or_zero — raw JSON numeric literal for `key`, or "0" when absent. +// Used for numeric node/activation fields so they stay unquoted (valid JSON). +fn api_num_or_zero(obj: String, key: String) -> String { + let v: String = json_get_raw(obj, key) + if str_eq(v, "") { return "0" } + return v +} + +// api_utf8_trunc — byte-truncate `s` to at most `n` bytes WITHOUT splitting a +// multibyte UTF-8 sequence (str_slice is byte-based). Backs the cut off while the +// first EXCLUDED byte is a UTF-8 continuation byte (0x80..0xBF), so the snippet is +// always a valid prefix. Guards against re-introducing a parse failure via +// invalid UTF-8 in a JSON string value. +fn api_utf8_trunc(s: String, n: Int) -> String { + if str_len(s) <= n { return s } + let cut: Int = n + let scanning: Bool = true + while scanning && cut > 0 { + let b: Int = str_char_code(s, cut) + let is_cont: Bool = b >= 128 && b < 192 + let cut = if is_cont { cut - 1 } else { cut } + let scanning = is_cont + } + return str_slice(s, 0, cut) +} + +// api_compact_node — light projection of a full engram node: identity fields + +// a bounded, UTF-8-safe content snippet. Drops embeddings, metadata, tags, and +// timestamps; truncates content. `content_truncated` flags a clipped snippet. +fn api_compact_node(node: String, snip: Int) -> String { + let id: String = json_get(node, "id") + let ntype: String = json_get(node, "node_type") + let label: String = json_get(node, "label") + let tier: String = json_get(node, "tier") + let content: String = json_get(node, "content") + let snippet: String = api_utf8_trunc(content, snip) + let trunc_str: String = if str_len(content) > snip { "true" } else { "false" } + return "{\"id\":\"" + api_json_escape(id) + "\"" + + ",\"node_type\":\"" + api_json_escape(ntype) + "\"" + + ",\"label\":\"" + api_json_escape(label) + "\"" + + ",\"tier\":\"" + api_json_escape(tier) + "\"" + + ",\"importance\":" + api_num_or_zero(node, "importance") + + ",\"salience\":" + api_num_or_zero(node, "salience") + + ",\"content\":\"" + api_json_escape(snippet) + "\"" + + ",\"content_truncated\":" + trunc_str + "}" +} + +// api_compact_node_array — map api_compact_node over a bare-node array, capping +// the element count. For scan results (recent, typed lists). +fn api_compact_node_array(raw: String, max_items: Int, snip: Int) -> String { + if !api_nonempty(raw) { return "[]" } + let n: Int = json_array_len(raw) + let cap: Int = if n < max_items { n } else { max_items } + let out: String = "[" + let i: Int = 0 + while i < cap { + let node: String = json_array_get(raw, i) + let sep: String = if i == 0 { "" } else { "," } + let out = out + sep + api_compact_node(node, snip) + let i = i + 1 + } + return out + "]" +} + +// api_compact_activated — like api_compact_node_array but for activation results, +// whose elements wrap the node as {"node":{...},"activation_strength":...,...}. +// Preserves the activation scalars, compacts the inner node. +fn api_compact_activated(raw: String, max_items: Int, snip: Int) -> String { + if !api_nonempty(raw) { return "[]" } + let n: Int = json_array_len(raw) + let cap: Int = if n < max_items { n } else { max_items } + let out: String = "[" + let i: Int = 0 + while i < cap { + let el: String = json_array_get(raw, i) + let node: String = json_get_raw(el, "node") + let sep: String = if i == 0 { "" } else { "," } + let out = out + sep + "{\"node\":" + api_compact_node(node, snip) + + ",\"activation_strength\":" + api_num_or_zero(el, "activation_strength") + + ",\"working_memory_weight\":" + api_num_or_zero(el, "working_memory_weight") + + ",\"epistemic_confidence\":" + api_num_or_zero(el, "epistemic_confidence") + + ",\"hops\":" + api_num_or_zero(el, "hops") + + ",\"promoted\":" + api_num_or_zero(el, "promoted") + "}" + let i = i + 1 + } + return out + "]" +} + // api_persisted — read-back-after-write guard against hallucinated saves. // After a write builtin returns an id, confirm the node is actually queryable // via engram_get_node_json(id) (returns "" or "null" when missing). Returns @@ -170,27 +271,45 @@ fn memory_hide_tombstoned(raw: String, path: String) -> String { // Spread-activates from session intent, loads self-root neighbors, // surfaces recent InternalStateEvent nodes, returns stats + recent nodes. fn handle_api_begin_session(body: String) -> String { + // PAYLOAD BOUND: this handler was the highest-fanout working-set endpoint — + // a depth-2 spread PLUS the full neighbor dump of the self-identity hub + // (~90KB alone; node JSON carries full content + embeddings). On the ~12k-node + // store the assembled response ran to ~900KB, then roughly doubled through two + // rounds of JSON re-escaping in the MCP wrapper — the client saw "socket + // connection closed unexpectedly" on every beginSession call. Fix: depth-2 → + // depth-1 spread, drop the self-hub dump (identity loading has its own tool, + // inspectGraph), cap every list, and project each node to a light identity + + // a bounded, UTF-8-safe content snippet. self_neighbors kept as [] for + // response-shape compatibility. Response drops ~900KB → ~12KB; full content + // stays available on demand via recall / fetch / inspectGraph. let stats: String = engram_stats_json() - let activated: String = engram_activate_json("session start recent memory important", 2) - let self_nbrs: String = engram_neighbors_json("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee", 1, "both") - let state_events: String = engram_scan_nodes_by_type_json("InternalStateEvent", 5, 0) - let recent: String = engram_scan_nodes_json(10, 0) + let activated_raw: String = engram_activate_json("session start recent memory important", 1) + let activated: String = api_compact_activated(activated_raw, 8, 240) + let state_events_raw: String = engram_scan_nodes_by_type_json("InternalStateEvent", 5, 0) + let state_events: String = api_compact_node_array(state_events_raw, 5, 500) + let recent_raw: String = engram_scan_nodes_json(10, 0) + let recent: String = api_compact_node_array(recent_raw, 10, 240) return "{\"stats\":" + stats - + ",\"recent\":" + api_or_empty(recent) - + ",\"activated\":" + api_or_empty(activated) - + ",\"self_neighbors\":" + api_or_empty(self_nbrs) - + ",\"recent_state_events\":" + api_or_empty(state_events) + "}" + + ",\"recent\":" + recent + + ",\"activated\":" + activated + + ",\"self_neighbors\":[]" + + ",\"recent_state_events\":" + state_events + "}" } // handle_api_compile_ctx — compile active-work context. // Spread-activates from "active work" intent + recent nodes. fn handle_api_compile_ctx(body: String) -> String { let stats: String = engram_stats_json() - let activated: String = engram_activate_json("active work context current task in progress", 2) - let recent: String = engram_scan_nodes_json(20, 0) + // PAYLOAD BOUND: same digest treatment as begin_session. This handler's + // depth-2 spread returns even more full nodes, so bounding here is essential — + // cap to 10 activated + 20 recent, project to UTF-8-safe snippets. + let activated_raw: String = engram_activate_json("active work context current task in progress", 2) + let activated: String = api_compact_activated(activated_raw, 10, 240) + let recent_raw: String = engram_scan_nodes_json(20, 0) + let recent: String = api_compact_node_array(recent_raw, 20, 240) return "{\"stats\":" + stats - + ",\"recent_nodes\":" + api_or_empty(recent) - + ",\"activated\":" + api_or_empty(activated) + "}" + + ",\"recent_nodes\":" + recent + + ",\"activated\":" + activated + "}" } // ── Memory ────────────────────────────────────────────────────────────────────