diff --git a/dist/soul.c b/dist/soul.c index 2b11fcc..3beeb0f 100644 --- a/dist/soul.c +++ b/dist/soul.c @@ -1114,6 +1114,11 @@ el_val_t llm_base_url(void); el_val_t llm_wire_format(void); el_val_t json_escape(el_val_t s); el_val_t openai_chat_complete(el_val_t model, el_val_t base_url, el_val_t api_key, el_val_t safe_sys, el_val_t messages_json); +el_val_t openai_tools_json(el_val_t tools_anthropic); +el_val_t utf8_safe_slice(el_val_t s, el_val_t n); +el_val_t json_trim_dangling_escape(el_val_t s); +el_val_t agentic_tools_no_web(void); +el_val_t openai_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 tools_log_in); el_val_t agentic_tools_literal(void); el_val_t web_search_tool_json(void); el_val_t strip_client_web_search(el_val_t tools_inner); @@ -1137,7 +1142,7 @@ el_val_t handle_chat_plan(el_val_t body); el_val_t agentic_safety_screen(el_val_t session_id, el_val_t message); el_val_t handle_chat_agentic(el_val_t body); 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 bridge_save(el_val_t session_id, el_val_t model, el_val_t safe_sys, el_val_t tools_json, el_val_t messages, el_val_t tools_log, el_val_t tool_use_id); +el_val_t bridge_save(el_val_t session_id, el_val_t model, el_val_t safe_sys, el_val_t tools_json, el_val_t messages, el_val_t tools_log, el_val_t tool_use_id, el_val_t wire); el_val_t agentic_resume(el_val_t session_id, el_val_t tool_use_id, el_val_t content); el_val_t handle_tool_result(el_val_t session_id, el_val_t body); el_val_t handle_chat_as_soul(el_val_t body); @@ -28095,8 +28100,8 @@ el_val_t session_preload_bullets(el_val_t nodes, el_val_t max_bullets, el_val_t while (i < limit) { el_val_t node = json_array_get(nodes, i); el_val_t content = json_get(node, EL_STR("content")); - el_val_t snip = ({ el_val_t _if_result_356 = 0; if ((str_len(content) > snip_len)) { _if_result_356 = (str_slice(content, 0, snip_len)); } else { _if_result_356 = (content); } _if_result_356; }); - bullets = ({ el_val_t _if_result_357 = 0; if (str_eq(snip, EL_STR(""))) { _if_result_357 = (bullets); } else { _if_result_357 = (({ el_val_t _if_result_358 = 0; if (str_eq(bullets, EL_STR(""))) { _if_result_358 = (el_str_concat(EL_STR("- "), snip)); } else { _if_result_358 = (el_str_concat(el_str_concat(bullets, EL_STR("\n- ")), snip)); } _if_result_358; })); } _if_result_357; }); + el_val_t snip = utf8_safe_slice(content, snip_len); + bullets = ({ el_val_t _if_result_356 = 0; if (str_eq(snip, EL_STR(""))) { _if_result_356 = (bullets); } else { _if_result_356 = (({ el_val_t _if_result_357 = 0; if (str_eq(bullets, EL_STR(""))) { _if_result_357 = (el_str_concat(EL_STR("- "), snip)); } else { _if_result_357 = (el_str_concat(el_str_concat(bullets, EL_STR("\n- ")), snip)); } _if_result_357; })); } _if_result_356; }); i = (i + 1); } return bullets; @@ -28110,11 +28115,11 @@ el_val_t affective_context_prefix(void) { el_val_t has_boot_aff = !str_eq(boot_aff, EL_STR("")); el_val_t dist_nodes_aff = engram_search_json(EL_STR("bell:soft bell:hard BellEvent affective"), 3); el_val_t has_dist_aff = (!str_eq(dist_nodes_aff, EL_STR("")) && !str_eq(dist_nodes_aff, EL_STR("[]"))); - el_val_t found_recent_dist = ({ el_val_t _if_result_359 = 0; if (has_boot_aff) { _if_result_359 = (1); } else { _if_result_359 = (({ el_val_t _if_result_360 = 0; if (has_dist_aff) { el_val_t dn0 = json_array_get(dist_nodes_aff, 0); el_val_t daff_ts = affective_node_ts(dn0); _if_result_360 = ((daff_ts > aff_cutoff)); } else { _if_result_360 = (0); } _if_result_360; })); } _if_result_359; }); + el_val_t found_recent_dist = ({ el_val_t _if_result_358 = 0; if (has_boot_aff) { _if_result_358 = (1); } else { _if_result_358 = (({ el_val_t _if_result_359 = 0; if (has_dist_aff) { el_val_t dn0 = json_array_get(dist_nodes_aff, 0); el_val_t daff_ts = affective_node_ts(dn0); _if_result_359 = ((daff_ts > aff_cutoff)); } else { _if_result_359 = (0); } _if_result_359; })); } _if_result_358; }); el_val_t pos_nodes_aff = engram_search_json(EL_STR("PositiveEvent joy:high joy:low affective"), 3); el_val_t has_pos_aff = (!str_eq(pos_nodes_aff, EL_STR("")) && !str_eq(pos_nodes_aff, EL_STR("[]"))); - el_val_t found_recent_pos = ({ el_val_t _if_result_361 = 0; if ((has_pos_aff && !found_recent_dist)) { el_val_t pn0 = json_array_get(pos_nodes_aff, 0); el_val_t paff_ts = affective_node_ts(pn0); _if_result_361 = ((paff_ts > aff_cutoff)); } else { _if_result_361 = (0); } _if_result_361; }); - el_val_t affective_out = ({ el_val_t _if_result_362 = 0; if (found_recent_dist) { _if_result_362 = (EL_STR("[RECENT CONTEXT: User recently expressed significant distress. Monitor for indirect crisis signals and respond with care.]\n\n")); } else { _if_result_362 = (({ el_val_t _if_result_363 = 0; if (found_recent_pos) { _if_result_363 = (EL_STR("[RECENT CONTEXT: User recently shared exciting or joyful news. Acknowledge and celebrate with them when relevant.]\n\n")); } else { _if_result_363 = (EL_STR("")); } _if_result_363; })); } _if_result_362; }); + el_val_t found_recent_pos = ({ el_val_t _if_result_360 = 0; if ((has_pos_aff && !found_recent_dist)) { el_val_t pn0 = json_array_get(pos_nodes_aff, 0); el_val_t paff_ts = affective_node_ts(pn0); _if_result_360 = ((paff_ts > aff_cutoff)); } else { _if_result_360 = (0); } _if_result_360; }); + el_val_t affective_out = ({ el_val_t _if_result_361 = 0; if (found_recent_dist) { _if_result_361 = (EL_STR("[RECENT CONTEXT: User recently expressed significant distress. Monitor for indirect crisis signals and respond with care.]\n\n")); } else { _if_result_361 = (({ el_val_t _if_result_362 = 0; if (found_recent_pos) { _if_result_362 = (EL_STR("[RECENT CONTEXT: User recently shared exciting or joyful news. Acknowledge and celebrate with them when relevant.]\n\n")); } else { _if_result_362 = (EL_STR("")); } _if_result_362; })); } _if_result_361; }); return affective_out; return 0; } @@ -28125,25 +28130,25 @@ el_val_t handle_chat(el_val_t body) { return EL_STR("{\"__status__\":400,\"error\":\"message is required\",\"response\":\"\"}"); } el_val_t state_hist = state_get(conv_hist_key(EL_STR(""))); - el_val_t stored_hist = ({ el_val_t _if_result_364 = 0; if (str_eq(state_hist, EL_STR(""))) { _if_result_364 = (conv_history_load(EL_STR(""))); } else { _if_result_364 = (state_hist); } _if_result_364; }); + el_val_t stored_hist = ({ el_val_t _if_result_363 = 0; if (str_eq(state_hist, EL_STR(""))) { _if_result_363 = (conv_history_load(EL_STR(""))); } else { _if_result_363 = (state_hist); } _if_result_363; }); el_val_t hist_load_failed = str_eq(state_get(EL_STR("conv_history_load_failed")), EL_STR("1")); - el_val_t hist_len = ({ el_val_t _if_result_365 = 0; if (str_eq(stored_hist, EL_STR(""))) { _if_result_365 = (0); } else { _if_result_365 = (json_array_len(stored_hist)); } _if_result_365; }); + el_val_t hist_len = ({ el_val_t _if_result_364 = 0; if (str_eq(stored_hist, EL_STR(""))) { _if_result_364 = (0); } else { _if_result_364 = (json_array_len(stored_hist)); } _if_result_364; }); el_val_t is_continuation = engram_is_continuation(message, hist_len); - el_val_t last_entry = ({ el_val_t _if_result_366 = 0; if (is_continuation) { _if_result_366 = (json_array_get(stored_hist, (hist_len - 1))); } else { _if_result_366 = (EL_STR("")); } _if_result_366; }); - el_val_t last_content = ({ el_val_t _if_result_367 = 0; if (!str_eq(last_entry, EL_STR(""))) { _if_result_367 = (json_get(last_entry, EL_STR("content"))); } else { _if_result_367 = (EL_STR("")); } _if_result_367; }); - el_val_t thread_snip = ({ el_val_t _if_result_368 = 0; if ((str_len(last_content) > 250)) { _if_result_368 = (str_slice(last_content, 0, 250)); } else { _if_result_368 = (last_content); } _if_result_368; }); - el_val_t activation_seed = ({ el_val_t _if_result_369 = 0; if (!str_eq(thread_snip, EL_STR(""))) { _if_result_369 = (el_str_concat(el_str_concat(thread_snip, EL_STR(" ")), message)); } else { _if_result_369 = (message); } _if_result_369; }); + el_val_t last_entry = ({ el_val_t _if_result_365 = 0; if (is_continuation) { _if_result_365 = (json_array_get(stored_hist, (hist_len - 1))); } else { _if_result_365 = (EL_STR("")); } _if_result_365; }); + el_val_t last_content = ({ el_val_t _if_result_366 = 0; if (!str_eq(last_entry, EL_STR(""))) { _if_result_366 = (json_get(last_entry, EL_STR("content"))); } else { _if_result_366 = (EL_STR("")); } _if_result_366; }); + el_val_t thread_snip = ({ el_val_t _if_result_367 = 0; if ((str_len(last_content) > 250)) { _if_result_367 = (str_slice(last_content, 0, 250)); } else { _if_result_367 = (last_content); } _if_result_367; }); + el_val_t activation_seed = ({ el_val_t _if_result_368 = 0; if (!str_eq(thread_snip, EL_STR(""))) { _if_result_368 = (el_str_concat(el_str_concat(thread_snip, EL_STR(" ")), message)); } else { _if_result_368 = (message); } _if_result_368; }); el_val_t affective_prefix = affective_context_prefix(); el_val_t ctx = engram_compile(activation_seed); el_val_t sp_req_model = json_get(body, EL_STR("model")); - el_val_t sp_model = ({ el_val_t _if_result_370 = 0; if (str_eq(sp_req_model, EL_STR(""))) { _if_result_370 = (chat_default_model()); } else { _if_result_370 = (sp_req_model); } _if_result_370; }); + el_val_t sp_model = ({ el_val_t _if_result_369 = 0; if (str_eq(sp_req_model, EL_STR(""))) { _if_result_369 = (chat_default_model()); } else { _if_result_369 = (sp_req_model); } _if_result_369; }); el_val_t system = el_str_concat(el_str_concat(affective_prefix, build_system_prompt(ctx, 1)), current_engine_note(sp_model)); el_val_t seen_ids = state_get(EL_STR("engram_compile_seen_ids")); - el_val_t session_preload = ({ el_val_t _if_result_371 = 0; if ((hist_len == 0)) { el_val_t profile_nodes = engram_search_json(EL_STR("user profile identity preferences"), 5); el_val_t work_nodes_0 = engram_search_json(EL_STR("in_progress active project work"), 5); el_val_t project_nodes = engram_search_json(EL_STR("project status current ongoing active"), 5); el_val_t summary_nodes = engram_search_json(EL_STR("SessionSummary session:summary previous-session recent"), 3); el_val_t profile_ok = (!str_eq(profile_nodes, EL_STR("")) && !str_eq(profile_nodes, EL_STR("[]"))); el_val_t work_nodes_typed = engram_search_json(EL_STR("WorkItem status:in_progress active work"), 6); el_val_t work_ok_typed = (!str_eq(work_nodes_typed, EL_STR("")) && !str_eq(work_nodes_typed, EL_STR("[]"))); el_val_t work_nodes_1 = ({ el_val_t _if_result_372 = 0; if (work_ok_typed) { _if_result_372 = (work_nodes_typed); } else { _if_result_372 = (engram_search_json(EL_STR("active project task current in_progress"), 6)); } _if_result_372; }); el_val_t work_ok = (!str_eq(work_nodes_1, EL_STR("")) && !str_eq(work_nodes_1, EL_STR("[]"))); el_val_t project_ok = (!str_eq(project_nodes, EL_STR("")) && !str_eq(project_nodes, EL_STR("[]"))); el_val_t summary_ok = (!str_eq(summary_nodes, EL_STR("")) && !str_eq(summary_nodes, EL_STR("[]"))); el_val_t profile_bullets = ({ el_val_t _if_result_373 = 0; if (profile_ok) { el_val_t pn = json_array_len(profile_nodes); el_val_t bullets_0 = EL_STR(""); el_val_t bullets_1 = ({ el_val_t _if_result_374 = 0; if ((pn > 0)) { el_val_t n0 = json_array_get(profile_nodes, 0); el_val_t id0 = json_get(n0, EL_STR("id")); el_val_t c0 = json_get(n0, EL_STR("content")); el_val_t s0 = ({ el_val_t _if_result_375 = 0; if ((str_len(c0) > 120)) { _if_result_375 = (str_slice(c0, 0, 120)); } else { _if_result_375 = (c0); } _if_result_375; }); _if_result_374 = (({ el_val_t _if_result_376 = 0; if ((id_in_seen(id0, seen_ids) || str_eq(s0, EL_STR("")))) { _if_result_376 = (bullets_0); } else { _if_result_376 = (el_str_concat(EL_STR("- "), s0)); } _if_result_376; })); } else { _if_result_374 = (bullets_0); } _if_result_374; }); el_val_t bullets_2 = ({ el_val_t _if_result_377 = 0; if ((pn > 1)) { el_val_t n1 = json_array_get(profile_nodes, 1); el_val_t id1 = json_get(n1, EL_STR("id")); el_val_t c1 = json_get(n1, EL_STR("content")); el_val_t s1 = ({ el_val_t _if_result_378 = 0; if ((str_len(c1) > 120)) { _if_result_378 = (str_slice(c1, 0, 120)); } else { _if_result_378 = (c1); } _if_result_378; }); _if_result_377 = (({ el_val_t _if_result_379 = 0; if ((id_in_seen(id1, seen_ids) || str_eq(s1, EL_STR("")))) { _if_result_379 = (bullets_1); } else { _if_result_379 = (el_str_concat(el_str_concat(bullets_1, EL_STR("\n- ")), s1)); } _if_result_379; })); } else { _if_result_377 = (bullets_1); } _if_result_377; }); el_val_t bullets_3 = ({ el_val_t _if_result_380 = 0; if ((pn > 2)) { el_val_t n2 = json_array_get(profile_nodes, 2); el_val_t id2 = json_get(n2, EL_STR("id")); el_val_t c2 = json_get(n2, EL_STR("content")); el_val_t s2 = ({ el_val_t _if_result_381 = 0; if ((str_len(c2) > 120)) { _if_result_381 = (str_slice(c2, 0, 120)); } else { _if_result_381 = (c2); } _if_result_381; }); _if_result_380 = (({ el_val_t _if_result_382 = 0; if ((id_in_seen(id2, seen_ids) || str_eq(s2, EL_STR("")))) { _if_result_382 = (bullets_2); } else { _if_result_382 = (el_str_concat(el_str_concat(bullets_2, EL_STR("\n- ")), s2)); } _if_result_382; })); } else { _if_result_380 = (bullets_2); } _if_result_380; }); _if_result_373 = (bullets_3); } else { _if_result_373 = (EL_STR("")); } _if_result_373; }); el_val_t work_bullets = ({ el_val_t _if_result_383 = 0; if (work_ok) { el_val_t wn = json_array_len(work_nodes_1); el_val_t wb_0 = EL_STR(""); el_val_t wb_1 = ({ el_val_t _if_result_384 = 0; if ((wn > 0)) { el_val_t w0 = json_array_get(work_nodes_1, 0); el_val_t wid0 = json_get(w0, EL_STR("id")); el_val_t wc0 = json_get(w0, EL_STR("content")); el_val_t ws0 = ({ el_val_t _if_result_385 = 0; if ((str_len(wc0) > 120)) { _if_result_385 = (str_slice(wc0, 0, 120)); } else { _if_result_385 = (wc0); } _if_result_385; }); _if_result_384 = (({ el_val_t _if_result_386 = 0; if ((id_in_seen(wid0, seen_ids) || str_eq(ws0, EL_STR("")))) { _if_result_386 = (wb_0); } else { _if_result_386 = (el_str_concat(EL_STR("- "), ws0)); } _if_result_386; })); } else { _if_result_384 = (wb_0); } _if_result_384; }); el_val_t wb_2 = ({ el_val_t _if_result_387 = 0; if ((wn > 1)) { el_val_t w1 = json_array_get(work_nodes_1, 1); el_val_t wid1 = json_get(w1, EL_STR("id")); el_val_t wc1 = json_get(w1, EL_STR("content")); el_val_t ws1 = ({ el_val_t _if_result_388 = 0; if ((str_len(wc1) > 120)) { _if_result_388 = (str_slice(wc1, 0, 120)); } else { _if_result_388 = (wc1); } _if_result_388; }); _if_result_387 = (({ el_val_t _if_result_389 = 0; if ((id_in_seen(wid1, seen_ids) || str_eq(ws1, EL_STR("")))) { _if_result_389 = (wb_1); } else { _if_result_389 = (el_str_concat(el_str_concat(wb_1, EL_STR("\n- ")), ws1)); } _if_result_389; })); } else { _if_result_387 = (wb_1); } _if_result_387; }); _if_result_383 = (wb_2); } else { _if_result_383 = (EL_STR("")); } _if_result_383; }); el_val_t project_bullets = ({ el_val_t _if_result_390 = 0; if (project_ok) { el_val_t prn = json_array_len(project_nodes); el_val_t pb_0 = EL_STR(""); el_val_t pb_1 = ({ el_val_t _if_result_391 = 0; if ((prn > 0)) { el_val_t pr0 = json_array_get(project_nodes, 0); el_val_t prid0 = json_get(pr0, EL_STR("id")); el_val_t prc0 = json_get(pr0, EL_STR("content")); el_val_t ps0 = ({ el_val_t _if_result_392 = 0; if ((str_len(prc0) > 120)) { _if_result_392 = (str_slice(prc0, 0, 120)); } else { _if_result_392 = (prc0); } _if_result_392; }); _if_result_391 = (({ el_val_t _if_result_393 = 0; if ((id_in_seen(prid0, seen_ids) || str_eq(ps0, EL_STR("")))) { _if_result_393 = (pb_0); } else { _if_result_393 = (el_str_concat(EL_STR("- "), ps0)); } _if_result_393; })); } else { _if_result_391 = (pb_0); } _if_result_391; }); el_val_t pb_2 = ({ el_val_t _if_result_394 = 0; if ((prn > 1)) { el_val_t pr1 = json_array_get(project_nodes, 1); el_val_t prid1 = json_get(pr1, EL_STR("id")); el_val_t prc1 = json_get(pr1, EL_STR("content")); el_val_t ps1 = ({ el_val_t _if_result_395 = 0; if ((str_len(prc1) > 120)) { _if_result_395 = (str_slice(prc1, 0, 120)); } else { _if_result_395 = (prc1); } _if_result_395; }); _if_result_394 = (({ el_val_t _if_result_396 = 0; if ((id_in_seen(prid1, seen_ids) || str_eq(ps1, EL_STR("")))) { _if_result_396 = (pb_1); } else { _if_result_396 = (el_str_concat(el_str_concat(pb_1, EL_STR("\n- ")), ps1)); } _if_result_396; })); } else { _if_result_394 = (pb_1); } _if_result_394; }); _if_result_390 = (pb_2); } else { _if_result_390 = (EL_STR("")); } _if_result_390; }); el_val_t summary_bullet = ({ el_val_t _if_result_397 = 0; if (summary_ok) { el_val_t sn0 = json_array_get(summary_nodes, 0); el_val_t snid0 = json_get(sn0, EL_STR("id")); el_val_t sc0 = json_get(sn0, EL_STR("content")); el_val_t ss0 = ({ el_val_t _if_result_398 = 0; if ((str_len(sc0) > 200)) { _if_result_398 = (str_slice(sc0, 0, 200)); } else { _if_result_398 = (sc0); } _if_result_398; }); _if_result_397 = (({ el_val_t _if_result_399 = 0; if ((id_in_seen(snid0, seen_ids) || str_eq(ss0, EL_STR("")))) { _if_result_399 = (EL_STR("")); } else { _if_result_399 = (el_str_concat(EL_STR("- "), ss0)); } _if_result_399; })); } else { _if_result_397 = (EL_STR("")); } _if_result_397; }); el_val_t hp = !str_eq(profile_bullets, EL_STR("")); el_val_t hw = !str_eq(work_bullets, EL_STR("")); el_val_t hpr = !str_eq(project_bullets, EL_STR("")); el_val_t hs = !str_eq(summary_bullet, EL_STR("")); el_val_t preload = ({ el_val_t _if_result_400 = 0; if ((((hp || hw) || hpr) || hs)) { el_val_t sec_p = ({ el_val_t _if_result_401 = 0; if (hp) { _if_result_401 = (el_str_concat(EL_STR("[USER CONTEXT \xe2\x80\x94 from memory]\n"), profile_bullets)); } else { _if_result_401 = (EL_STR("")); } _if_result_401; }); el_val_t sec_w = ({ el_val_t _if_result_402 = 0; if (hw) { _if_result_402 = (el_str_concat(EL_STR("[ACTIVE WORK \xe2\x80\x94 from memory]\n"), work_bullets)); } else { _if_result_402 = (EL_STR("")); } _if_result_402; }); el_val_t sec_pr = ({ el_val_t _if_result_403 = 0; if (hpr) { _if_result_403 = (el_str_concat(EL_STR("[PROJECTS \xe2\x80\x94 from memory]\n"), project_bullets)); } else { _if_result_403 = (EL_STR("")); } _if_result_403; }); el_val_t sec_s = ({ el_val_t _if_result_404 = 0; if (hs) { _if_result_404 = (el_str_concat(EL_STR("[PREVIOUS SESSION \xe2\x80\x94 from memory]\n"), summary_bullet)); } else { _if_result_404 = (EL_STR("")); } _if_result_404; }); el_val_t sep1 = ({ el_val_t _if_result_405 = 0; if ((hp && ((hw || hpr) || hs))) { _if_result_405 = (EL_STR("\n\n")); } else { _if_result_405 = (EL_STR("")); } _if_result_405; }); el_val_t sep2 = ({ el_val_t _if_result_406 = 0; if ((hw && (hpr || hs))) { _if_result_406 = (EL_STR("\n\n")); } else { _if_result_406 = (EL_STR("")); } _if_result_406; }); el_val_t sep3 = ({ el_val_t _if_result_407 = 0; if ((hpr && hs)) { _if_result_407 = (EL_STR("\n\n")); } else { _if_result_407 = (EL_STR("")); } _if_result_407; }); _if_result_400 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("\n\n"), sec_p), sep1), sec_w), sep2), sec_pr), sep3), sec_s)); } else { _if_result_400 = (EL_STR("")); } _if_result_400; }); _if_result_371 = (preload); } else { _if_result_371 = (EL_STR("")); } _if_result_371; }); - el_val_t rendered_hist = ({ el_val_t _if_result_408 = 0; if ((hist_len > 0)) { el_val_t rh_total = json_array_len(stored_hist); el_val_t rh_out = EL_STR(""); el_val_t rh_i = 0; _if_result_408 = (rh_out); } else { _if_result_408 = (EL_STR("")); } _if_result_408; }); - el_val_t full_system = ({ el_val_t _if_result_409 = 0; if ((hist_len > 0)) { _if_result_409 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(system, EL_STR("\n\n[RECENT CONVERSATION \xe2\x80\x94 last ")), int_to_str(hist_len)), EL_STR(" turns]\n")), rendered_hist)); } else { _if_result_409 = (el_str_concat(system, session_preload)); } _if_result_409; }); + el_val_t session_preload = ({ el_val_t _if_result_370 = 0; if ((hist_len == 0)) { el_val_t profile_nodes = engram_search_json(EL_STR("user profile identity preferences"), 5); el_val_t work_nodes_0 = engram_search_json(EL_STR("in_progress active project work"), 5); el_val_t project_nodes = engram_search_json(EL_STR("project status current ongoing active"), 5); el_val_t summary_nodes = engram_search_json(EL_STR("SessionSummary session:summary previous-session recent"), 3); el_val_t profile_ok = (!str_eq(profile_nodes, EL_STR("")) && !str_eq(profile_nodes, EL_STR("[]"))); el_val_t work_nodes_typed = engram_search_json(EL_STR("WorkItem status:in_progress active work"), 6); el_val_t work_ok_typed = (!str_eq(work_nodes_typed, EL_STR("")) && !str_eq(work_nodes_typed, EL_STR("[]"))); el_val_t work_nodes_1 = ({ el_val_t _if_result_371 = 0; if (work_ok_typed) { _if_result_371 = (work_nodes_typed); } else { _if_result_371 = (engram_search_json(EL_STR("active project task current in_progress"), 6)); } _if_result_371; }); el_val_t work_ok = (!str_eq(work_nodes_1, EL_STR("")) && !str_eq(work_nodes_1, EL_STR("[]"))); el_val_t project_ok = (!str_eq(project_nodes, EL_STR("")) && !str_eq(project_nodes, EL_STR("[]"))); el_val_t summary_ok = (!str_eq(summary_nodes, EL_STR("")) && !str_eq(summary_nodes, EL_STR("[]"))); el_val_t profile_bullets = ({ el_val_t _if_result_372 = 0; if (profile_ok) { el_val_t pn = json_array_len(profile_nodes); el_val_t bullets_0 = EL_STR(""); el_val_t bullets_1 = ({ el_val_t _if_result_373 = 0; if ((pn > 0)) { el_val_t n0 = json_array_get(profile_nodes, 0); el_val_t id0 = json_get(n0, EL_STR("id")); el_val_t c0 = json_get(n0, EL_STR("content")); el_val_t s0 = ({ el_val_t _if_result_374 = 0; if ((str_len(c0) > 120)) { _if_result_374 = (str_slice(c0, 0, 120)); } else { _if_result_374 = (c0); } _if_result_374; }); _if_result_373 = (({ el_val_t _if_result_375 = 0; if ((id_in_seen(id0, seen_ids) || str_eq(s0, EL_STR("")))) { _if_result_375 = (bullets_0); } else { _if_result_375 = (el_str_concat(EL_STR("- "), s0)); } _if_result_375; })); } else { _if_result_373 = (bullets_0); } _if_result_373; }); el_val_t bullets_2 = ({ el_val_t _if_result_376 = 0; if ((pn > 1)) { el_val_t n1 = json_array_get(profile_nodes, 1); el_val_t id1 = json_get(n1, EL_STR("id")); el_val_t c1 = json_get(n1, EL_STR("content")); el_val_t s1 = ({ el_val_t _if_result_377 = 0; if ((str_len(c1) > 120)) { _if_result_377 = (str_slice(c1, 0, 120)); } else { _if_result_377 = (c1); } _if_result_377; }); _if_result_376 = (({ el_val_t _if_result_378 = 0; if ((id_in_seen(id1, seen_ids) || str_eq(s1, EL_STR("")))) { _if_result_378 = (bullets_1); } else { _if_result_378 = (el_str_concat(el_str_concat(bullets_1, EL_STR("\n- ")), s1)); } _if_result_378; })); } else { _if_result_376 = (bullets_1); } _if_result_376; }); el_val_t bullets_3 = ({ el_val_t _if_result_379 = 0; if ((pn > 2)) { el_val_t n2 = json_array_get(profile_nodes, 2); el_val_t id2 = json_get(n2, EL_STR("id")); el_val_t c2 = json_get(n2, EL_STR("content")); el_val_t s2 = ({ el_val_t _if_result_380 = 0; if ((str_len(c2) > 120)) { _if_result_380 = (str_slice(c2, 0, 120)); } else { _if_result_380 = (c2); } _if_result_380; }); _if_result_379 = (({ el_val_t _if_result_381 = 0; if ((id_in_seen(id2, seen_ids) || str_eq(s2, EL_STR("")))) { _if_result_381 = (bullets_2); } else { _if_result_381 = (el_str_concat(el_str_concat(bullets_2, EL_STR("\n- ")), s2)); } _if_result_381; })); } else { _if_result_379 = (bullets_2); } _if_result_379; }); _if_result_372 = (bullets_3); } else { _if_result_372 = (EL_STR("")); } _if_result_372; }); el_val_t work_bullets = ({ el_val_t _if_result_382 = 0; if (work_ok) { el_val_t wn = json_array_len(work_nodes_1); el_val_t wb_0 = EL_STR(""); el_val_t wb_1 = ({ el_val_t _if_result_383 = 0; if ((wn > 0)) { el_val_t w0 = json_array_get(work_nodes_1, 0); el_val_t wid0 = json_get(w0, EL_STR("id")); el_val_t wc0 = json_get(w0, EL_STR("content")); el_val_t ws0 = ({ el_val_t _if_result_384 = 0; if ((str_len(wc0) > 120)) { _if_result_384 = (str_slice(wc0, 0, 120)); } else { _if_result_384 = (wc0); } _if_result_384; }); _if_result_383 = (({ el_val_t _if_result_385 = 0; if ((id_in_seen(wid0, seen_ids) || str_eq(ws0, EL_STR("")))) { _if_result_385 = (wb_0); } else { _if_result_385 = (el_str_concat(EL_STR("- "), ws0)); } _if_result_385; })); } else { _if_result_383 = (wb_0); } _if_result_383; }); el_val_t wb_2 = ({ el_val_t _if_result_386 = 0; if ((wn > 1)) { el_val_t w1 = json_array_get(work_nodes_1, 1); el_val_t wid1 = json_get(w1, EL_STR("id")); el_val_t wc1 = json_get(w1, EL_STR("content")); el_val_t ws1 = ({ el_val_t _if_result_387 = 0; if ((str_len(wc1) > 120)) { _if_result_387 = (str_slice(wc1, 0, 120)); } else { _if_result_387 = (wc1); } _if_result_387; }); _if_result_386 = (({ el_val_t _if_result_388 = 0; if ((id_in_seen(wid1, seen_ids) || str_eq(ws1, EL_STR("")))) { _if_result_388 = (wb_1); } else { _if_result_388 = (el_str_concat(el_str_concat(wb_1, EL_STR("\n- ")), ws1)); } _if_result_388; })); } else { _if_result_386 = (wb_1); } _if_result_386; }); _if_result_382 = (wb_2); } else { _if_result_382 = (EL_STR("")); } _if_result_382; }); el_val_t project_bullets = ({ el_val_t _if_result_389 = 0; if (project_ok) { el_val_t prn = json_array_len(project_nodes); el_val_t pb_0 = EL_STR(""); el_val_t pb_1 = ({ el_val_t _if_result_390 = 0; if ((prn > 0)) { el_val_t pr0 = json_array_get(project_nodes, 0); el_val_t prid0 = json_get(pr0, EL_STR("id")); el_val_t prc0 = json_get(pr0, EL_STR("content")); el_val_t ps0 = ({ el_val_t _if_result_391 = 0; if ((str_len(prc0) > 120)) { _if_result_391 = (str_slice(prc0, 0, 120)); } else { _if_result_391 = (prc0); } _if_result_391; }); _if_result_390 = (({ el_val_t _if_result_392 = 0; if ((id_in_seen(prid0, seen_ids) || str_eq(ps0, EL_STR("")))) { _if_result_392 = (pb_0); } else { _if_result_392 = (el_str_concat(EL_STR("- "), ps0)); } _if_result_392; })); } else { _if_result_390 = (pb_0); } _if_result_390; }); el_val_t pb_2 = ({ el_val_t _if_result_393 = 0; if ((prn > 1)) { el_val_t pr1 = json_array_get(project_nodes, 1); el_val_t prid1 = json_get(pr1, EL_STR("id")); el_val_t prc1 = json_get(pr1, EL_STR("content")); el_val_t ps1 = ({ el_val_t _if_result_394 = 0; if ((str_len(prc1) > 120)) { _if_result_394 = (str_slice(prc1, 0, 120)); } else { _if_result_394 = (prc1); } _if_result_394; }); _if_result_393 = (({ el_val_t _if_result_395 = 0; if ((id_in_seen(prid1, seen_ids) || str_eq(ps1, EL_STR("")))) { _if_result_395 = (pb_1); } else { _if_result_395 = (el_str_concat(el_str_concat(pb_1, EL_STR("\n- ")), ps1)); } _if_result_395; })); } else { _if_result_393 = (pb_1); } _if_result_393; }); _if_result_389 = (pb_2); } else { _if_result_389 = (EL_STR("")); } _if_result_389; }); el_val_t summary_bullet = ({ el_val_t _if_result_396 = 0; if (summary_ok) { el_val_t sn0 = json_array_get(summary_nodes, 0); el_val_t snid0 = json_get(sn0, EL_STR("id")); el_val_t sc0 = json_get(sn0, EL_STR("content")); el_val_t ss0 = ({ el_val_t _if_result_397 = 0; if ((str_len(sc0) > 200)) { _if_result_397 = (str_slice(sc0, 0, 200)); } else { _if_result_397 = (sc0); } _if_result_397; }); _if_result_396 = (({ el_val_t _if_result_398 = 0; if ((id_in_seen(snid0, seen_ids) || str_eq(ss0, EL_STR("")))) { _if_result_398 = (EL_STR("")); } else { _if_result_398 = (el_str_concat(EL_STR("- "), ss0)); } _if_result_398; })); } else { _if_result_396 = (EL_STR("")); } _if_result_396; }); el_val_t hp = !str_eq(profile_bullets, EL_STR("")); el_val_t hw = !str_eq(work_bullets, EL_STR("")); el_val_t hpr = !str_eq(project_bullets, EL_STR("")); el_val_t hs = !str_eq(summary_bullet, EL_STR("")); el_val_t preload = ({ el_val_t _if_result_399 = 0; if ((((hp || hw) || hpr) || hs)) { el_val_t sec_p = ({ el_val_t _if_result_400 = 0; if (hp) { _if_result_400 = (el_str_concat(EL_STR("[USER CONTEXT \xe2\x80\x94 from memory]\n"), profile_bullets)); } else { _if_result_400 = (EL_STR("")); } _if_result_400; }); el_val_t sec_w = ({ el_val_t _if_result_401 = 0; if (hw) { _if_result_401 = (el_str_concat(EL_STR("[ACTIVE WORK \xe2\x80\x94 from memory]\n"), work_bullets)); } else { _if_result_401 = (EL_STR("")); } _if_result_401; }); el_val_t sec_pr = ({ el_val_t _if_result_402 = 0; if (hpr) { _if_result_402 = (el_str_concat(EL_STR("[PROJECTS \xe2\x80\x94 from memory]\n"), project_bullets)); } else { _if_result_402 = (EL_STR("")); } _if_result_402; }); el_val_t sec_s = ({ el_val_t _if_result_403 = 0; if (hs) { _if_result_403 = (el_str_concat(EL_STR("[PREVIOUS SESSION \xe2\x80\x94 from memory]\n"), summary_bullet)); } else { _if_result_403 = (EL_STR("")); } _if_result_403; }); el_val_t sep1 = ({ el_val_t _if_result_404 = 0; if ((hp && ((hw || hpr) || hs))) { _if_result_404 = (EL_STR("\n\n")); } else { _if_result_404 = (EL_STR("")); } _if_result_404; }); el_val_t sep2 = ({ el_val_t _if_result_405 = 0; if ((hw && (hpr || hs))) { _if_result_405 = (EL_STR("\n\n")); } else { _if_result_405 = (EL_STR("")); } _if_result_405; }); el_val_t sep3 = ({ el_val_t _if_result_406 = 0; if ((hpr && hs)) { _if_result_406 = (EL_STR("\n\n")); } else { _if_result_406 = (EL_STR("")); } _if_result_406; }); _if_result_399 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("\n\n"), sec_p), sep1), sec_w), sep2), sec_pr), sep3), sec_s)); } else { _if_result_399 = (EL_STR("")); } _if_result_399; }); _if_result_370 = (preload); } else { _if_result_370 = (EL_STR("")); } _if_result_370; }); + el_val_t rendered_hist = ({ el_val_t _if_result_407 = 0; if ((hist_len > 0)) { el_val_t rh_total = json_array_len(stored_hist); el_val_t rh_out = EL_STR(""); el_val_t rh_i = 0; _if_result_407 = (rh_out); } else { _if_result_407 = (EL_STR("")); } _if_result_407; }); + el_val_t full_system = ({ el_val_t _if_result_408 = 0; if ((hist_len > 0)) { _if_result_408 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(system, EL_STR("\n\n[RECENT CONVERSATION \xe2\x80\x94 last ")), int_to_str(hist_len)), EL_STR(" turns]\n")), rendered_hist)); } else { _if_result_408 = (el_str_concat(system, session_preload)); } _if_result_408; }); el_val_t req_model = json_get(body, EL_STR("model")); - el_val_t model = ({ el_val_t _if_result_410 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_410 = (chat_default_model()); } else { _if_result_410 = (req_model); } _if_result_410; }); + el_val_t model = ({ el_val_t _if_result_409 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_409 = (chat_default_model()); } else { _if_result_409 = (req_model); } _if_result_409; }); full_system = safety_augment_system(full_system, message); el_val_t raw_response = llm_call_system(model, full_system, 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"))); @@ -28154,7 +28159,7 @@ el_val_t handle_chat(el_val_t body) { el_val_t safe_response = json_safe(clean_response); el_val_t updated_hist = hist_append(stored_hist, EL_STR("user"), message); el_val_t updated_hist2 = hist_append(updated_hist, EL_STR("assistant"), raw_response); - el_val_t final_hist = ({ el_val_t _if_result_411 = 0; if ((json_array_len(updated_hist2) > 20)) { _if_result_411 = (hist_trim_with_bell_guard(updated_hist2)); } else { _if_result_411 = (updated_hist2); } _if_result_411; }); + el_val_t final_hist = ({ el_val_t _if_result_410 = 0; if ((json_array_len(updated_hist2) > 20)) { _if_result_410 = (hist_trim_with_bell_guard(updated_hist2)); } else { _if_result_410 = (updated_hist2); } _if_result_410; }); state_set(conv_hist_key(EL_STR("")), final_hist); conv_history_persist(EL_STR(""), final_hist); el_val_t final_hist_len = json_array_len(final_hist); @@ -28162,7 +28167,7 @@ el_val_t handle_chat(el_val_t body) { el_val_t already_wrote = state_get(EL_STR("session_summary_written")); if (str_eq(already_wrote, EL_STR(""))) { el_val_t boot_id = state_get(EL_STR("session_boot_id")); - boot_id = ({ el_val_t _if_result_412 = 0; if (str_eq(boot_id, EL_STR(""))) { el_val_t new_id = int_to_str(time_now()); (void)(state_set(EL_STR("session_boot_id"), new_id)); _if_result_412 = (new_id); } else { _if_result_412 = (boot_id); } _if_result_412; }); + boot_id = ({ el_val_t _if_result_411 = 0; if (str_eq(boot_id, EL_STR(""))) { el_val_t new_id = int_to_str(time_now()); (void)(state_set(EL_STR("session_boot_id"), new_id)); _if_result_411 = (new_id); } else { _if_result_411 = (boot_id); } _if_result_411; }); el_val_t sess_label = el_str_concat(EL_STR("session:summary:"), boot_id); el_val_t auto_sum = session_summary_autogenerate(final_hist); if (!str_eq(auto_sum, EL_STR(""))) { @@ -28173,9 +28178,9 @@ el_val_t handle_chat(el_val_t body) { } el_val_t activation_nodes = engram_activate_json(message, 2); el_val_t act_ok = (!str_eq(activation_nodes, EL_STR("")) && !str_eq(activation_nodes, EL_STR("[]"))); - el_val_t act_out = ({ el_val_t _if_result_413 = 0; if (act_ok) { _if_result_413 = (activation_nodes); } else { _if_result_413 = (EL_STR("[]")); } _if_result_413; }); + el_val_t act_out = ({ el_val_t _if_result_412 = 0; if (act_ok) { _if_result_412 = (activation_nodes); } else { _if_result_412 = (EL_STR("[]")); } _if_result_412; }); strengthen_chat_nodes(act_out); - el_val_t hist_warning = ({ el_val_t _if_result_414 = 0; if (hist_load_failed) { _if_result_414 = (EL_STR(",\"history_load_failed\":true")); } else { _if_result_414 = (EL_STR("")); } _if_result_414; }); + el_val_t hist_warning = ({ el_val_t _if_result_413 = 0; if (hist_load_failed) { _if_result_413 = (EL_STR(",\"history_load_failed\":true")); } else { _if_result_413 = (EL_STR("")); } _if_result_413; }); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"response\":\""), safe_response), EL_STR("\",\"model\":\"")), model), EL_STR("\",\"activation_nodes\":")), act_out), hist_warning), EL_STR("}")); return 0; } @@ -28186,9 +28191,9 @@ el_val_t handle_see(el_val_t body) { return EL_STR("{\"error\":\"image is required\",\"reply\":\"\"}"); } el_val_t message = json_get(body, EL_STR("message")); - el_val_t prompt = ({ el_val_t _if_result_415 = 0; if (str_eq(message, EL_STR(""))) { _if_result_415 = (EL_STR("What do you see in this image? Describe the scene and anything notable.")); } else { _if_result_415 = (message); } _if_result_415; }); + el_val_t prompt = ({ el_val_t _if_result_414 = 0; if (str_eq(message, EL_STR(""))) { _if_result_414 = (EL_STR("What do you see in this image? Describe the scene and anything notable.")); } else { _if_result_414 = (message); } _if_result_414; }); el_val_t req_model = json_get(body, EL_STR("model")); - el_val_t model = ({ el_val_t _if_result_416 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_416 = (chat_default_model()); } else { _if_result_416 = (req_model); } _if_result_416; }); + el_val_t model = ({ el_val_t _if_result_415 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_415 = (chat_default_model()); } else { _if_result_415 = (req_model); } _if_result_415; }); el_val_t identity = state_get(EL_STR("soul_identity")); el_val_t system = el_str_concat(el_str_concat(identity, bounded_persona_floor()), EL_STR(" You have been given vision. Describe what you see directly and honestly. Be present-tense and observant.")); el_val_t text = llm_vision(model, system, prompt, image); @@ -28210,21 +28215,37 @@ el_val_t agentic_api_key(void) { if (!str_eq(k1, EL_STR(""))) { return k1; } - return env(EL_STR("NEURON_LLM_0_KEY")); + el_val_t k2 = env(EL_STR("NEURON_LLM_0_KEY")); + if (!str_eq(k2, EL_STR(""))) { + return k2; + } + return env(EL_STR("SOUL_API_KEY")); return 0; } el_val_t llm_base_url(void) { - return env(EL_STR("NEURON_LLM_0_URL")); + el_val_t u = env(EL_STR("NEURON_LLM_0_URL")); + if (!str_eq(u, EL_STR(""))) { + return u; + } + el_val_t p = env(EL_STR("SOUL_LLM_PROVIDER")); + if (str_eq(p, EL_STR("")) || str_eq(p, EL_STR("anthropic"))) { + return EL_STR(""); + } + return env(EL_STR("SOUL_LLM_BASE_URL")); return 0; } el_val_t llm_wire_format(void) { el_val_t f = env(EL_STR("NEURON_LLM_0_FORMAT")); - if (str_eq(f, EL_STR(""))) { - return EL_STR("anthropic"); + if (!str_eq(f, EL_STR(""))) { + return f; } - return f; + el_val_t p = env(EL_STR("SOUL_LLM_PROVIDER")); + if ((((str_eq(p, EL_STR("openai")) || str_eq(p, EL_STR("grok"))) || str_eq(p, EL_STR("gemini"))) || str_eq(p, EL_STR("groq"))) || str_eq(p, EL_STR("ollama"))) { + return EL_STR("openai"); + } + return EL_STR("anthropic"); return 0; } @@ -28238,8 +28259,8 @@ el_val_t json_escape(el_val_t s) { } el_val_t openai_chat_complete(el_val_t model, el_val_t base_url, el_val_t api_key, el_val_t safe_sys, el_val_t messages_json) { - el_val_t inner = ({ el_val_t _if_result_417 = 0; if ((json_array_len(messages_json) > 0)) { _if_result_417 = (str_slice(messages_json, 1, (str_len(messages_json) - 1))); } else { _if_result_417 = (EL_STR("")); } _if_result_417; }); - el_val_t msgs = ({ el_val_t _if_result_418 = 0; if (str_eq(inner, EL_STR(""))) { _if_result_418 = (el_str_concat(el_str_concat(EL_STR("[{\"role\":\"system\",\"content\":\""), safe_sys), EL_STR("\"}]"))); } else { _if_result_418 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("[{\"role\":\"system\",\"content\":\""), safe_sys), EL_STR("\"},")), inner), EL_STR("]"))); } _if_result_418; }); + el_val_t inner = ({ el_val_t _if_result_416 = 0; if ((json_array_len(messages_json) > 0)) { _if_result_416 = (str_slice(messages_json, 1, (str_len(messages_json) - 1))); } else { _if_result_416 = (EL_STR("")); } _if_result_416; }); + el_val_t msgs = ({ el_val_t _if_result_417 = 0; if (str_eq(inner, EL_STR(""))) { _if_result_417 = (el_str_concat(el_str_concat(EL_STR("[{\"role\":\"system\",\"content\":\""), safe_sys), EL_STR("\"}]"))); } else { _if_result_417 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("[{\"role\":\"system\",\"content\":\""), safe_sys), EL_STR("\"},")), inner), EL_STR("]"))); } _if_result_417; }); el_val_t req_body = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"model\":\""), model), EL_STR("\"")), EL_STR(",\"max_tokens\":4096")), EL_STR(",\"messages\":")), msgs), EL_STR("}")); el_val_t h = el_map_new(0); map_set(h, EL_STR("content-type"), EL_STR("application/json")); @@ -28253,7 +28274,7 @@ el_val_t openai_chat_complete(el_val_t model, el_val_t base_url, el_val_t api_ke return EL_STR("{\"error\":\"llm unavailable\",\"reply\":\"\"}"); } el_val_t choices = json_get_raw(raw_resp, EL_STR("choices")); - el_val_t eff_choices = ({ el_val_t _if_result_419 = 0; if (str_eq(choices, EL_STR(""))) { _if_result_419 = (EL_STR("[]")); } else { _if_result_419 = (choices); } _if_result_419; }); + el_val_t eff_choices = ({ el_val_t _if_result_418 = 0; if (str_eq(choices, EL_STR(""))) { _if_result_418 = (EL_STR("[]")); } else { _if_result_418 = (choices); } _if_result_418; }); if (json_array_len(eff_choices) < 1) { return EL_STR("{\"error\":\"empty response\",\"reply\":\"\"}"); } @@ -28264,6 +28285,195 @@ el_val_t openai_chat_complete(el_val_t model, el_val_t base_url, el_val_t api_ke return 0; } +el_val_t openai_tools_json(el_val_t tools_anthropic) { + el_val_t out = EL_STR(""); + el_val_t i = 0; + el_val_t n = json_array_len(tools_anthropic); + while (i < n) { + el_val_t entry = json_array_get(tools_anthropic, i); + el_val_t name = json_get(entry, EL_STR("name")); + el_val_t desc = json_get(entry, EL_STR("description")); + el_val_t schema = json_get_raw(entry, EL_STR("input_schema")); + el_val_t keep = (!str_eq(name, EL_STR("")) && !str_eq(schema, EL_STR(""))); + el_val_t piece = ({ el_val_t _if_result_419 = 0; if (keep) { _if_result_419 = (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("{\"type\":\"function\",\"function\":{\"name\":\""), json_escape(name)), EL_STR("\"")), EL_STR(",\"description\":\"")), json_escape(desc)), EL_STR("\"")), EL_STR(",\"parameters\":")), schema), EL_STR("}}"))); } else { _if_result_419 = (EL_STR("")); } _if_result_419; }); + out = ({ el_val_t _if_result_420 = 0; if (keep) { _if_result_420 = (({ el_val_t _if_result_421 = 0; if (str_eq(out, EL_STR(""))) { _if_result_421 = (piece); } else { _if_result_421 = (el_str_concat(el_str_concat(out, EL_STR(",")), piece)); } _if_result_421; })); } else { _if_result_420 = (out); } _if_result_420; }); + i = (i + 1); + } + return el_str_concat(el_str_concat(EL_STR("["), out), EL_STR("]")); + return 0; +} + +el_val_t utf8_safe_slice(el_val_t s, el_val_t n) { + if (str_len(s) <= n) { + return s; + } + el_val_t cut = str_slice(s, 0, n); + el_val_t total = str_len(cut); + el_val_t i = (total - 1); + el_val_t keep = total; + el_val_t scanning = 1; + el_val_t steps = 0; + while ((scanning && (steps < 4)) && (i >= 0)) { + el_val_t c = str_char_code(cut, i); + el_val_t is_ascii = (c < 128); + el_val_t is_lead = (c >= 192); + el_val_t need = ({ el_val_t _if_result_422 = 0; if ((c >= 240)) { _if_result_422 = (4); } else { _if_result_422 = (({ el_val_t _if_result_423 = 0; if ((c >= 224)) { _if_result_423 = (3); } else { _if_result_423 = (2); } _if_result_423; })); } _if_result_422; }); + el_val_t have = (total - i); + keep = ({ el_val_t _if_result_424 = 0; if (is_ascii) { _if_result_424 = (total); } else { _if_result_424 = (({ el_val_t _if_result_425 = 0; if (is_lead) { _if_result_425 = (({ el_val_t _if_result_426 = 0; if ((have == need)) { _if_result_426 = (total); } else { _if_result_426 = (i); } _if_result_426; })); } else { _if_result_425 = (keep); } _if_result_425; })); } _if_result_424; }); + scanning = ({ el_val_t _if_result_427 = 0; if ((is_ascii || is_lead)) { _if_result_427 = (0); } else { _if_result_427 = (1); } _if_result_427; }); + i = (i - 1); + steps = (steps + 1); + } + return str_slice(cut, 0, keep); + return 0; +} + +el_val_t json_trim_dangling_escape(el_val_t s) { + el_val_t out = s; + while (str_ends_with(out, EL_STR("\\"))) { + out = str_slice(out, 0, (str_len(out) - 1)); + } + return out; + return 0; +} + +el_val_t agentic_tools_no_web(void) { + el_val_t base = agentic_tools_literal(); + el_val_t conn = connector_tools_json(); + el_val_t base_inner = str_slice(base, 1, (str_len(base) - 1)); + el_val_t conn_inner = str_slice(conn, 1, (str_len(conn) - 1)); + el_val_t merged = ({ el_val_t _if_result_428 = 0; if (str_eq(conn_inner, EL_STR(""))) { _if_result_428 = (base_inner); } else { _if_result_428 = (el_str_concat(el_str_concat(base_inner, EL_STR(",")), conn_inner)); } _if_result_428; }); + return el_str_concat(el_str_concat(EL_STR("["), strip_client_web_search(merged)), EL_STR("]")); + return 0; +} + +el_val_t openai_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 tools_log_in) { + el_val_t api_url = el_str_concat(llm_base_url(), EL_STR("/chat/completions")); + el_val_t api_key = agentic_api_key(); + el_val_t h = el_map_new(0); + map_set(h, EL_STR("content-type"), EL_STR("application/json")); + if (!str_eq(api_key, EL_STR(""))) { + map_set(h, EL_STR("Authorization"), el_str_concat(EL_STR("Bearer "), api_key)); + } + 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 tools_oai = openai_tools_json(tools_json); + el_val_t has_tools = (json_array_len(tools_oai) > 0); + el_val_t messages = messages_in; + el_val_t final_text = EL_STR(""); + el_val_t tools_log = tools_log_in; + el_val_t iteration = 0; + el_val_t keep_going = 1; + el_val_t pending = 0; + el_val_t pend_tool_id = EL_STR(""); + el_val_t pend_tool_name = EL_STR(""); + el_val_t pend_tool_input = EL_STR(""); + el_val_t pend_tool_tier = EL_STR(""); + el_val_t pend_narration = EL_STR(""); + if (!str_eq(session_id, EL_STR(""))) { + state_set(el_str_concat(EL_STR("run_progress_"), session_id), EL_STR("")); + } + while (keep_going && (iteration < 12)) { + el_val_t inner_msgs = str_slice(messages, 1, (str_len(messages) - 1)); + el_val_t all_msgs = ({ el_val_t _if_result_429 = 0; if (str_eq(inner_msgs, EL_STR(""))) { _if_result_429 = (el_str_concat(el_str_concat(EL_STR("[{\"role\":\"system\",\"content\":\""), safe_sys), EL_STR("\"}]"))); } else { _if_result_429 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("[{\"role\":\"system\",\"content\":\""), safe_sys), EL_STR("\"},")), inner_msgs), EL_STR("]"))); } _if_result_429; }); + el_val_t tool_frag = ({ el_val_t _if_result_430 = 0; if (has_tools) { _if_result_430 = (el_str_concat(el_str_concat(EL_STR(",\"tools\":"), tools_oai), EL_STR(",\"tool_choice\":\"auto\",\"parallel_tool_calls\":false"))); } else { _if_result_430 = (EL_STR("")); } _if_result_430; }); + el_val_t req_body = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"model\":\""), model), EL_STR("\"")), EL_STR(",\"max_tokens\":16384")), tool_frag), EL_STR(",\"messages\":")), all_msgs), EL_STR("}")); + el_val_t raw_resp = http_post_with_headers(api_url, req_body, h); + el_val_t is_error = (str_eq(raw_resp, EL_STR("")) || str_starts_with(raw_resp, EL_STR("{\"error\""))); + if (is_error) { + el_val_t err_head = ({ el_val_t _if_result_431 = 0; if ((str_len(raw_resp) > 220)) { _if_result_431 = (str_slice(raw_resp, 0, 220)); } else { _if_result_431 = (raw_resp); } _if_result_431; }); + println(el_str_concat(EL_STR("[soul] llm error (openai lane): "), err_head)); + return EL_STR("{\"error\":\"llm unavailable\",\"reply\":\"\"}"); + } + el_val_t choices = json_get_raw(raw_resp, EL_STR("choices")); + el_val_t eff_choices = ({ el_val_t _if_result_432 = 0; if (str_eq(choices, EL_STR(""))) { _if_result_432 = (EL_STR("[]")); } else { _if_result_432 = (choices); } _if_result_432; }); + if (json_array_len(eff_choices) < 1) { + el_val_t noc_head = ({ el_val_t _if_result_433 = 0; if ((str_len(raw_resp) > 220)) { _if_result_433 = (str_slice(raw_resp, 0, 220)); } else { _if_result_433 = (raw_resp); } _if_result_433; }); + println(el_str_concat(EL_STR("[soul] llm error (openai lane): no choices in response: "), noc_head)); + return EL_STR("{\"error\":\"llm unavailable\",\"reply\":\"\"}"); + } + el_val_t first = json_array_get(eff_choices, 0); + el_val_t message_o = json_get_raw(first, EL_STR("message")); + el_val_t finish = json_get(first, EL_STR("finish_reason")); + el_val_t content_raw = json_get_raw(message_o, EL_STR("content")); + el_val_t is_null_content = (str_eq(content_raw, EL_STR("null")) || str_eq(content_raw, EL_STR(""))); + el_val_t text_out = ({ el_val_t _if_result_434 = 0; if (is_null_content) { _if_result_434 = (EL_STR("")); } else { _if_result_434 = (json_get(message_o, EL_STR("content"))); } _if_result_434; }); + el_val_t tc_raw = json_get_raw(message_o, EL_STR("tool_calls")); + el_val_t tc_arr = ({ el_val_t _if_result_435 = 0; if ((str_eq(tc_raw, EL_STR("")) || str_eq(tc_raw, EL_STR("null")))) { _if_result_435 = (EL_STR("[]")); } else { _if_result_435 = (tc_raw); } _if_result_435; }); + el_val_t tc_n = json_array_len(tc_arr); + el_val_t has_tool = (tc_n > 0); + if (tc_n > 1) { + println(el_str_concat(el_str_concat(EL_STR("[soul] DRIFT: provider returned "), int_to_str(tc_n)), EL_STR(" parallel tool_calls despite parallel_tool_calls:false - keeping the first only (ADR-0005 mirror)"))); + } + if (((!str_eq(finish, EL_STR("stop")) && !str_eq(finish, EL_STR("tool_calls"))) && !str_eq(finish, EL_STR("length"))) && !str_eq(finish, EL_STR(""))) { + println(el_str_concat(EL_STR("[soul] DRIFT: unknown finish_reason from API: "), finish)); + } + el_val_t tc0 = ({ el_val_t _if_result_436 = 0; if (has_tool) { _if_result_436 = (json_array_get(tc_arr, 0)); } else { _if_result_436 = (EL_STR("")); } _if_result_436; }); + el_val_t tool_id = ({ el_val_t _if_result_437 = 0; if (has_tool) { _if_result_437 = (json_get(tc0, EL_STR("id"))); } else { _if_result_437 = (EL_STR("")); } _if_result_437; }); + el_val_t tc_fn = ({ el_val_t _if_result_438 = 0; if (has_tool) { _if_result_438 = (json_get_raw(tc0, EL_STR("function"))); } else { _if_result_438 = (EL_STR("")); } _if_result_438; }); + el_val_t tool_name = ({ el_val_t _if_result_439 = 0; if (has_tool) { _if_result_439 = (json_get(tc_fn, EL_STR("name"))); } else { _if_result_439 = (EL_STR("")); } _if_result_439; }); + el_val_t tool_input_raw = ({ el_val_t _if_result_440 = 0; if (has_tool) { _if_result_440 = (json_get(tc_fn, EL_STR("arguments"))); } else { _if_result_440 = (EL_STR("")); } _if_result_440; }); + el_val_t tool_input = ({ el_val_t _if_result_441 = 0; if (str_eq(tool_input_raw, EL_STR(""))) { _if_result_441 = (EL_STR("{}")); } else { _if_result_441 = (tool_input_raw); } _if_result_441; }); + el_val_t is_tool_turn = 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_442 = 0; if (!str_eq(session_id, EL_STR(""))) { _if_result_442 = (state_get(always_key)); } else { _if_result_442 = (EL_STR("")); } _if_result_442; }); + 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_443 = 0; if (is_tool_turn) { _if_result_443 = (classify_tool_risk(tool_name, tool_input)); } else { _if_result_443 = (EL_STR("")); } _if_result_443; }); + 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_444 = 0; if ((is_tool_turn && !needs_bridge)) { _if_result_444 = (dispatch_tool(tool_name, tool_input)); } else { _if_result_444 = (EL_STR("")); } _if_result_444; }); + el_val_t tool_result = ({ el_val_t _if_result_445 = 0; if ((str_len(tool_result_raw) > 6000)) { _if_result_445 = (el_str_concat(json_trim_dangling_escape(str_slice(tool_result_raw, 0, 6000)), EL_STR("...[truncated]"))); } else { _if_result_445 = (tool_result_raw); } _if_result_445; }); + el_val_t tool_quoted = el_str_concat(el_str_concat(EL_STR("\""), tool_name), EL_STR("\"")); + tools_log = ({ el_val_t _if_result_446 = 0; if (is_tool_turn) { _if_result_446 = (({ el_val_t _if_result_447 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_447 = (tool_quoted); } else { _if_result_447 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), tool_quoted)); } _if_result_447; })); } else { _if_result_446 = (tools_log); } _if_result_446; }); + el_val_t content_frag = ({ el_val_t _if_result_448 = 0; if (is_null_content) { _if_result_448 = (EL_STR("null")); } else { _if_result_448 = (content_raw); } _if_result_448; }); + el_val_t assist_turn = ({ el_val_t _if_result_449 = 0; if (has_tool) { _if_result_449 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"role\":\"assistant\",\"content\":"), content_frag), EL_STR(",\"tool_calls\":[")), tc0), EL_STR("]}"))); } else { _if_result_449 = (el_str_concat(el_str_concat(EL_STR("{\"role\":\"assistant\",\"content\":"), content_frag), EL_STR("}"))); } _if_result_449; }); + el_val_t inner_now = 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("["), inner_now), EL_STR(",")), assist_turn), EL_STR("]")); + el_val_t local_continue = (is_tool_turn && !needs_bridge); + messages = ({ el_val_t _if_result_450 = 0; if (local_continue) { el_val_t inner2 = str_slice(messages_with_assistant, 1, (str_len(messages_with_assistant) - 1)); _if_result_450 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner2), EL_STR(",{\"role\":\"tool\",\"tool_call_id\":\"")), tool_id), EL_STR("\",\"content\":\"")), tool_result), EL_STR("\"}]"))); } else { _if_result_450 = (messages); } _if_result_450; }); + 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_451 = 0; if ((str_len(text_out) > 280)) { _if_result_451 = (str_slice(text_out, 0, 280)); } else { _if_result_451 = (text_out); } _if_result_451; }); + 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_452 = 0; if (str_eq(prog_prev, EL_STR(""))) { _if_result_452 = (prog_entry); } else { _if_result_452 = (el_str_concat(el_str_concat(prog_prev, EL_STR(",")), prog_entry)); } _if_result_452; }); + state_set(prog_key, prog_next); + } + pending = ({ el_val_t _if_result_453 = 0; if (needs_bridge) { _if_result_453 = (1); } else { _if_result_453 = (pending); } _if_result_453; }); + pend_tool_id = ({ el_val_t _if_result_454 = 0; if (needs_bridge) { _if_result_454 = (tool_id); } else { _if_result_454 = (pend_tool_id); } _if_result_454; }); + pend_tool_name = ({ el_val_t _if_result_455 = 0; if (needs_bridge) { _if_result_455 = (tool_name); } else { _if_result_455 = (pend_tool_name); } _if_result_455; }); + pend_tool_input = ({ el_val_t _if_result_456 = 0; if (needs_bridge) { _if_result_456 = (tool_input); } else { _if_result_456 = (pend_tool_input); } _if_result_456; }); + pend_tool_tier = ({ el_val_t _if_result_457 = 0; if (needs_bridge) { _if_result_457 = (risk_tier); } else { _if_result_457 = (pend_tool_tier); } _if_result_457; }); + pend_narration = ({ el_val_t _if_result_458 = 0; if (needs_bridge) { _if_result_458 = (text_out); } else { _if_result_458 = (pend_narration); } _if_result_458; }); + if (needs_bridge) { + bridge_save(session_id, model, safe_sys, tools_json, messages_with_assistant, tools_log, tool_id, EL_STR("openai")); + } + final_text = ({ el_val_t _if_result_459 = 0; if (!is_tool_turn) { _if_result_459 = (el_str_concat(el_str_concat(final_text, text_join_sep(final_text, text_out, 1)), text_out)); } else { _if_result_459 = (final_text); } _if_result_459; }); + final_text = ({ el_val_t _if_result_460 = 0; if ((str_eq(finish, EL_STR("length")) && has_tool)) { _if_result_460 = (el_str_concat(final_text, EL_STR("\n\n[Output limit reached mid-action - the last planned action did not run. Ask me to continue to finish it.]"))); } else { _if_result_460 = (final_text); } _if_result_460; }); + keep_going = ({ el_val_t _if_result_461 = 0; if (local_continue) { _if_result_461 = (keep_going); } else { _if_result_461 = (0); } _if_result_461; }); + iteration = (iteration + 1); + } + if (pending) { + el_val_t safe_in = ({ el_val_t _if_result_462 = 0; if (str_eq(pend_tool_input, EL_STR(""))) { _if_result_462 = (EL_STR("{}")); } else { _if_result_462 = (pend_tool_input); } _if_result_462; }); + el_val_t tools_arr = ({ el_val_t _if_result_463 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_463 = (EL_STR("[]")); } else { _if_result_463 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_463; }); + 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_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(",\"sources\":\"\"")), EL_STR(",\"tools_used\":")), tools_arr), EL_STR("}")); + } + final_text = receipt_strip(final_text); + if (str_eq(final_text, EL_STR(""))) { + el_val_t hit_cap = (iteration >= 12); + el_val_t err_msg = ({ el_val_t _if_result_464 = 0; if (hit_cap) { _if_result_464 = (EL_STR("agentic loop hit the 12-iteration cap without producing a final reply - task may be too complex or a tool call is looping")); } else { _if_result_464 = (EL_STR("no response")); } _if_result_464; }); + 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_465 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_465 = (EL_STR("[]")); } else { _if_result_465 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_465; }); + 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_466 = 0; if (str_eq(done_prev, EL_STR(""))) { _if_result_466 = (EL_STR("{\"done\":true}")); } else { _if_result_466 = (el_str_concat(done_prev, EL_STR(",{\"done\":true}"))); } _if_result_466; }); + 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(",\"sources\":\"\",\"iterations\":")), int_to_str(iteration)), EL_STR("}")); + return 0; +} + el_val_t agentic_tools_literal(void) { 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("["), EL_STR("{\"name\":\"read_file\",\"description\":\"Read contents of a file from disk.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\",\"description\":\"Absolute file path\"}},\"required\":[\"path\"]}},")), EL_STR("{\"name\":\"write_file\",\"description\":\"Write content to a file on disk.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\"},\"content\":{\"type\":\"string\"}},\"required\":[\"path\",\"content\"]}},")), EL_STR("{\"name\":\"web_get\",\"description\":\"Fetch content from a URL.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"url\":{\"type\":\"string\"}},\"required\":[\"url\"]}},")), EL_STR("{\"name\":\"search_memory\",\"description\":\"Search engram memory for relevant nodes.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"query\":{\"type\":\"string\"}},\"required\":[\"query\"]}},")), EL_STR("{\"name\":\"run_command\",\"description\":\"Run a shell command and capture output.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"command\":{\"type\":\"string\"}},\"required\":[\"command\"]}},")), EL_STR("{\"name\":\"list_files\",\"description\":\"List files in a directory.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\"}},\"required\":[\"path\"]}},")), EL_STR("{\"name\":\"grep\",\"description\":\"Search for a pattern in files.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"pattern\":{\"type\":\"string\"},\"path\":{\"type\":\"string\"}},\"required\":[\"pattern\",\"path\"]}},")), EL_STR("{\"name\":\"edit_file\",\"description\":\"Edit a file by replacing old_text with new_text.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"path\":{\"type\":\"string\"},\"old_text\":{\"type\":\"string\"},\"new_text\":{\"type\":\"string\"}},\"required\":[\"path\",\"old_text\",\"new_text\"]}},")), EL_STR("{\"name\":\"remember\",\"description\":\"Store a memory in the Engram graph.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"content\":{\"type\":\"string\"},\"tags\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}}},\"required\":[\"content\"]}},")), EL_STR("{\"name\":\"recall\",\"description\":\"Recall memories by activating the Engram graph from a query.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"query\":{\"type\":\"string\"},\"depth\":{\"type\":\"integer\"}},\"required\":[\"query\"]}},")), EL_STR("{\"name\":\"neuron_search_knowledge\",\"description\":\"Search Neuron's knowledge base.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"query\":{\"type\":\"string\"},\"limit\":{\"type\":\"integer\"}},\"required\":[\"query\"]}},")), EL_STR("{\"name\":\"neuron_remember\",\"description\":\"Store a memory in Neuron's persistent graph.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"content\":{\"type\":\"string\"},\"tags\":{\"type\":\"array\",\"items\":{\"type\":\"string\"}},\"project\":{\"type\":\"string\"},\"importance\":{\"type\":\"string\"}},\"required\":[\"content\"]}},")), EL_STR("{\"name\":\"neuron_recall\",\"description\":\"Search Neuron's memory nodes.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"query\":{\"type\":\"string\"},\"limit\":{\"type\":\"integer\"}},\"required\":[\"query\"]}},")), EL_STR("{\"name\":\"neuron_review_backlog\",\"description\":\"Review Neuron's work backlog.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"view\":{\"type\":\"string\"},\"project\":{\"type\":\"string\"},\"status\":{\"type\":\"string\"},\"priority\":{\"type\":\"string\"},\"query\":{\"type\":\"string\"}},\"required\":[]}},")), EL_STR("{\"name\":\"neuron_find_artifacts\",\"description\":\"Find Neuron artifacts by project or query.\",\"input_schema\":{\"type\":\"object\",\"properties\":{\"query\":{\"type\":\"string\"},\"project\":{\"type\":\"string\"}},\"required\":[]}},")), EL_STR("{\"name\":\"neuron_compile_ctx\",\"description\":\"Compile Neuron's full active context snapshot.\",\"input_schema\":{\"type\":\"object\",\"properties\":{},\"required\":[]}}")), EL_STR("]")); return 0; @@ -28271,7 +28481,7 @@ el_val_t agentic_tools_literal(void) { el_val_t web_search_tool_json(void) { el_val_t ver = state_get(EL_STR("web_search_tool_version")); - el_val_t eff = ({ el_val_t _if_result_420 = 0; if (str_eq(ver, EL_STR(""))) { _if_result_420 = (EL_STR("web_search_20250305")); } else { _if_result_420 = (ver); } _if_result_420; }); + el_val_t eff = ({ el_val_t _if_result_467 = 0; if (str_eq(ver, EL_STR(""))) { _if_result_467 = (EL_STR("web_search_20250305")); } else { _if_result_467 = (ver); } _if_result_467; }); return el_str_concat(el_str_concat(EL_STR("{\"type\":\""), eff), EL_STR("\",\"name\":\"web_search\",\"max_uses\":5}")); return 0; } @@ -28317,13 +28527,13 @@ el_val_t agentic_tools_all(void) { el_val_t conn = connector_tools_json(); el_val_t base_inner = str_slice(base, 1, (str_len(base) - 1)); el_val_t conn_inner = str_slice(conn, 1, (str_len(conn) - 1)); - el_val_t merged = ({ el_val_t _if_result_421 = 0; if (str_eq(conn_inner, EL_STR(""))) { _if_result_421 = (base_inner); } else { _if_result_421 = (el_str_concat(el_str_concat(base_inner, EL_STR(",")), conn_inner)); } _if_result_421; }); + el_val_t merged = ({ el_val_t _if_result_468 = 0; if (str_eq(conn_inner, EL_STR(""))) { _if_result_468 = (base_inner); } else { _if_result_468 = (el_str_concat(el_str_concat(base_inner, EL_STR(",")), conn_inner)); } _if_result_468; }); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), strip_client_web_search(merged)), EL_STR(",")), web_search_tool_json()), EL_STR("]")); return 0; } el_val_t call_mcp_bridge(el_val_t tool_name, el_val_t tool_input) { - el_val_t eff_input = ({ el_val_t _if_result_422 = 0; if (str_eq(tool_input, EL_STR(""))) { _if_result_422 = (EL_STR("{}")); } else { _if_result_422 = (tool_input); } _if_result_422; }); + el_val_t eff_input = ({ el_val_t _if_result_469 = 0; if (str_eq(tool_input, EL_STR(""))) { _if_result_469 = (EL_STR("{}")); } else { _if_result_469 = (tool_input); } _if_result_469; }); el_val_t body = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"name\":\""), tool_name), EL_STR("\",\"input\":")), eff_input), EL_STR("}")); el_val_t tmp = EL_STR("/tmp/neuron-mcp-call.json"); fs_write(tmp, body); @@ -28358,7 +28568,7 @@ el_val_t call_neuron_mcp(el_val_t tool_name, el_val_t args) { el_val_t result = json_get(raw, EL_STR("result")); if (str_eq(result, EL_STR(""))) { el_val_t err = json_get(raw, EL_STR("error")); - return json_safe(({ el_val_t _if_result_423 = 0; if (str_eq(err, EL_STR(""))) { _if_result_423 = (EL_STR("Neuron MCP call failed")); } else { _if_result_423 = (el_str_concat(EL_STR("Neuron MCP error: "), err)); } _if_result_423; })); + return json_safe(({ el_val_t _if_result_470 = 0; if (str_eq(err, EL_STR(""))) { _if_result_470 = (EL_STR("Neuron MCP call failed")); } else { _if_result_470 = (el_str_concat(EL_STR("Neuron MCP error: "), err)); } _if_result_470; })); } return json_safe(result); return 0; @@ -28410,7 +28620,7 @@ el_val_t run_command_is_readonly(el_val_t cmd) { return 0; } el_val_t sp = str_index_of(cmd, EL_STR(" ")); - el_val_t first = ({ el_val_t _if_result_424 = 0; if ((sp < 0)) { _if_result_424 = (cmd); } else { _if_result_424 = (str_slice(cmd, 0, sp)); } _if_result_424; }); + el_val_t first = ({ el_val_t _if_result_471 = 0; if ((sp < 0)) { _if_result_471 = (cmd); } else { _if_result_471 = (str_slice(cmd, 0, sp)); } _if_result_471; }); if (((str_eq(first, EL_STR("ls")) || str_eq(first, EL_STR("cat"))) || str_eq(first, EL_STR("head"))) || str_eq(first, EL_STR("tail"))) { return 1; } @@ -28432,7 +28642,7 @@ el_val_t cmd_abs_escape_at(el_val_t cmd, el_val_t root, el_val_t needle) { el_val_t slash_at = ((idx + str_len(needle)) - 1); el_val_t after = str_slice(rest, slash_at, str_len(rest)); el_val_t ok = ((str_starts_with(after, el_str_concat(root, EL_STR("/"))) || str_starts_with(after, el_str_concat(root, EL_STR(" ")))) || str_eq(after, root)); - found = ({ el_val_t _if_result_425 = 0; if (!ok) { _if_result_425 = (1); } else { _if_result_425 = (found); } _if_result_425; }); + found = ({ el_val_t _if_result_472 = 0; if (!ok) { _if_result_472 = (1); } else { _if_result_472 = (found); } _if_result_472; }); rest = str_slice(rest, (slash_at + 1), str_len(rest)); } return found; @@ -28553,7 +28763,7 @@ el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input) { el_val_t content = json_get(out, EL_STR("content")); if (str_eq(content, EL_STR(""))) { el_val_t err = json_get(out, EL_STR("error")); - el_val_t msg = ({ el_val_t _if_result_426 = 0; if (str_eq(err, EL_STR(""))) { _if_result_426 = (EL_STR("MCP call failed")); } else { _if_result_426 = (el_str_concat(EL_STR("MCP error: "), err)); } _if_result_426; }); + el_val_t msg = ({ el_val_t _if_result_473 = 0; if (str_eq(err, EL_STR(""))) { _if_result_473 = (EL_STR("MCP call failed")); } else { _if_result_473 = (el_str_concat(EL_STR("MCP error: "), err)); } _if_result_473; }); return json_safe(msg); } return json_safe(content); @@ -28606,21 +28816,21 @@ el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input) { if (str_eq(tool_name, EL_STR("remember"))) { el_val_t content = json_get(tool_input, EL_STR("content")); el_val_t tags_raw = json_get(tool_input, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_427 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_427 = (EL_STR("[\"chat\"]")); } else { _if_result_427 = (tags_raw); } _if_result_427; }); + el_val_t tags = ({ el_val_t _if_result_474 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_474 = (EL_STR("[\"chat\"]")); } else { _if_result_474 = (tags_raw); } _if_result_474; }); el_val_t id = mem_remember(content, tags); return json_safe(el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), id), EL_STR("\"}"))); } if (str_eq(tool_name, EL_STR("recall"))) { el_val_t query = json_get(tool_input, EL_STR("query")); el_val_t depth_str = json_get(tool_input, EL_STR("depth")); - el_val_t depth = ({ el_val_t _if_result_428 = 0; if (str_eq(depth_str, EL_STR(""))) { _if_result_428 = (3); } else { _if_result_428 = (str_to_int(depth_str)); } _if_result_428; }); + el_val_t depth = ({ el_val_t _if_result_475 = 0; if (str_eq(depth_str, EL_STR(""))) { _if_result_475 = (3); } else { _if_result_475 = (str_to_int(depth_str)); } _if_result_475; }); el_val_t result = mem_recall(query, depth); return json_safe(result); } if (str_eq(tool_name, EL_STR("neuron_search_knowledge"))) { el_val_t query = json_get(tool_input, EL_STR("query")); el_val_t limit_str = json_get(tool_input, EL_STR("limit")); - el_val_t limit = ({ el_val_t _if_result_429 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_429 = (5); } else { _if_result_429 = (str_to_int(limit_str)); } _if_result_429; }); + el_val_t limit = ({ el_val_t _if_result_476 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_476 = (5); } else { _if_result_476 = (str_to_int(limit_str)); } _if_result_476; }); el_val_t args = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"query\":\""), json_safe(query)), EL_STR("\",\"limit\":")), int_to_str(limit)), EL_STR("}")); el_val_t result = call_neuron_mcp(EL_STR("searchKnowledge"), args); return json_safe(result); @@ -28631,9 +28841,9 @@ el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input) { el_val_t project = json_get(tool_input, EL_STR("project")); el_val_t importance = json_get(tool_input, EL_STR("importance")); el_val_t safe_content = json_safe(content); - el_val_t tags_part = ({ el_val_t _if_result_430 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_430 = (EL_STR("\"tags\":[\"chat\"]")); } else { _if_result_430 = (el_str_concat(EL_STR("\"tags\":"), tags_raw)); } _if_result_430; }); - el_val_t project_part = ({ el_val_t _if_result_431 = 0; if (str_eq(project, EL_STR(""))) { _if_result_431 = (EL_STR("")); } else { _if_result_431 = (el_str_concat(el_str_concat(EL_STR(",\"project\":\""), json_safe(project)), EL_STR("\""))); } _if_result_431; }); - el_val_t importance_part = ({ el_val_t _if_result_432 = 0; if (str_eq(importance, EL_STR(""))) { _if_result_432 = (EL_STR("")); } else { _if_result_432 = (el_str_concat(el_str_concat(EL_STR(",\"importance\":\""), json_safe(importance)), EL_STR("\""))); } _if_result_432; }); + el_val_t tags_part = ({ el_val_t _if_result_477 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_477 = (EL_STR("\"tags\":[\"chat\"]")); } else { _if_result_477 = (el_str_concat(EL_STR("\"tags\":"), tags_raw)); } _if_result_477; }); + el_val_t project_part = ({ el_val_t _if_result_478 = 0; if (str_eq(project, EL_STR(""))) { _if_result_478 = (EL_STR("")); } else { _if_result_478 = (el_str_concat(el_str_concat(EL_STR(",\"project\":\""), json_safe(project)), EL_STR("\""))); } _if_result_478; }); + el_val_t importance_part = ({ el_val_t _if_result_479 = 0; if (str_eq(importance, EL_STR(""))) { _if_result_479 = (EL_STR("")); } else { _if_result_479 = (el_str_concat(el_str_concat(EL_STR(",\"importance\":\""), json_safe(importance)), EL_STR("\""))); } _if_result_479; }); el_val_t args = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"content\":\""), safe_content), EL_STR("\",")), tags_part), project_part), importance_part), EL_STR("}")); el_val_t result = call_neuron_mcp(EL_STR("remember"), args); return json_safe(result); @@ -28641,7 +28851,7 @@ el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input) { if (str_eq(tool_name, EL_STR("neuron_recall"))) { el_val_t query = json_get(tool_input, EL_STR("query")); el_val_t limit_str = json_get(tool_input, EL_STR("limit")); - el_val_t limit = ({ el_val_t _if_result_433 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_433 = (10); } else { _if_result_433 = (str_to_int(limit_str)); } _if_result_433; }); + el_val_t limit = ({ el_val_t _if_result_480 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_480 = (10); } else { _if_result_480 = (str_to_int(limit_str)); } _if_result_480; }); el_val_t args = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"query\":\""), json_safe(query)), EL_STR("\",\"limit\":")), int_to_str(limit)), EL_STR("}")); el_val_t result = call_neuron_mcp(EL_STR("inspectMemories"), args); return json_safe(result); @@ -28652,11 +28862,11 @@ el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input) { el_val_t status = json_get(tool_input, EL_STR("status")); el_val_t priority = json_get(tool_input, EL_STR("priority")); el_val_t query = json_get(tool_input, EL_STR("query")); - el_val_t view_part = ({ el_val_t _if_result_434 = 0; if (str_eq(view, EL_STR(""))) { _if_result_434 = (EL_STR("\"view\":\"roadmap\"")); } else { _if_result_434 = (el_str_concat(el_str_concat(EL_STR("\"view\":\""), json_safe(view)), EL_STR("\""))); } _if_result_434; }); - el_val_t project_part = ({ el_val_t _if_result_435 = 0; if (str_eq(project, EL_STR(""))) { _if_result_435 = (EL_STR("")); } else { _if_result_435 = (el_str_concat(el_str_concat(EL_STR(",\"project\":\""), json_safe(project)), EL_STR("\""))); } _if_result_435; }); - el_val_t status_part = ({ el_val_t _if_result_436 = 0; if (str_eq(status, EL_STR(""))) { _if_result_436 = (EL_STR("")); } else { _if_result_436 = (el_str_concat(el_str_concat(EL_STR(",\"status\":\""), json_safe(status)), EL_STR("\""))); } _if_result_436; }); - el_val_t priority_part = ({ el_val_t _if_result_437 = 0; if (str_eq(priority, EL_STR(""))) { _if_result_437 = (EL_STR("")); } else { _if_result_437 = (el_str_concat(el_str_concat(EL_STR(",\"priority\":\""), json_safe(priority)), EL_STR("\""))); } _if_result_437; }); - el_val_t query_part = ({ el_val_t _if_result_438 = 0; if (str_eq(query, EL_STR(""))) { _if_result_438 = (EL_STR("")); } else { _if_result_438 = (el_str_concat(el_str_concat(EL_STR(",\"query\":\""), json_safe(query)), EL_STR("\""))); } _if_result_438; }); + el_val_t view_part = ({ el_val_t _if_result_481 = 0; if (str_eq(view, EL_STR(""))) { _if_result_481 = (EL_STR("\"view\":\"roadmap\"")); } else { _if_result_481 = (el_str_concat(el_str_concat(EL_STR("\"view\":\""), json_safe(view)), EL_STR("\""))); } _if_result_481; }); + el_val_t project_part = ({ el_val_t _if_result_482 = 0; if (str_eq(project, EL_STR(""))) { _if_result_482 = (EL_STR("")); } else { _if_result_482 = (el_str_concat(el_str_concat(EL_STR(",\"project\":\""), json_safe(project)), EL_STR("\""))); } _if_result_482; }); + el_val_t status_part = ({ el_val_t _if_result_483 = 0; if (str_eq(status, EL_STR(""))) { _if_result_483 = (EL_STR("")); } else { _if_result_483 = (el_str_concat(el_str_concat(EL_STR(",\"status\":\""), json_safe(status)), EL_STR("\""))); } _if_result_483; }); + el_val_t priority_part = ({ el_val_t _if_result_484 = 0; if (str_eq(priority, EL_STR(""))) { _if_result_484 = (EL_STR("")); } else { _if_result_484 = (el_str_concat(el_str_concat(EL_STR(",\"priority\":\""), json_safe(priority)), EL_STR("\""))); } _if_result_484; }); + el_val_t query_part = ({ el_val_t _if_result_485 = 0; if (str_eq(query, EL_STR(""))) { _if_result_485 = (EL_STR("")); } else { _if_result_485 = (el_str_concat(el_str_concat(EL_STR(",\"query\":\""), json_safe(query)), EL_STR("\""))); } _if_result_485; }); el_val_t args = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{"), view_part), project_part), status_part), priority_part), query_part), EL_STR("}")); el_val_t result = call_neuron_mcp(EL_STR("reviewBacklog"), args); return json_safe(result); @@ -28664,8 +28874,8 @@ el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input) { if (str_eq(tool_name, EL_STR("neuron_find_artifacts"))) { el_val_t query = json_get(tool_input, EL_STR("query")); el_val_t project = json_get(tool_input, EL_STR("project")); - el_val_t query_part = ({ el_val_t _if_result_439 = 0; if (str_eq(query, EL_STR(""))) { _if_result_439 = (EL_STR("")); } else { _if_result_439 = (el_str_concat(el_str_concat(EL_STR("\"query\":\""), json_safe(query)), EL_STR("\""))); } _if_result_439; }); - el_val_t project_part = ({ el_val_t _if_result_440 = 0; if (str_eq(project, EL_STR(""))) { _if_result_440 = (EL_STR("")); } else { _if_result_440 = (({ el_val_t _if_result_441 = 0; if (str_eq(query_part, EL_STR(""))) { _if_result_441 = (el_str_concat(el_str_concat(EL_STR("\"project\":\""), json_safe(project)), EL_STR("\""))); } else { _if_result_441 = (el_str_concat(el_str_concat(EL_STR(",\"project\":\""), json_safe(project)), EL_STR("\""))); } _if_result_441; })); } _if_result_440; }); + el_val_t query_part = ({ el_val_t _if_result_486 = 0; if (str_eq(query, EL_STR(""))) { _if_result_486 = (EL_STR("")); } else { _if_result_486 = (el_str_concat(el_str_concat(EL_STR("\"query\":\""), json_safe(query)), EL_STR("\""))); } _if_result_486; }); + el_val_t project_part = ({ el_val_t _if_result_487 = 0; if (str_eq(project, EL_STR(""))) { _if_result_487 = (EL_STR("")); } else { _if_result_487 = (({ el_val_t _if_result_488 = 0; if (str_eq(query_part, EL_STR(""))) { _if_result_488 = (el_str_concat(el_str_concat(EL_STR("\"project\":\""), json_safe(project)), EL_STR("\""))); } else { _if_result_488 = (el_str_concat(el_str_concat(EL_STR(",\"project\":\""), json_safe(project)), EL_STR("\""))); } _if_result_488; })); } _if_result_487; }); el_val_t args = el_str_concat(el_str_concat(el_str_concat(EL_STR("{"), query_part), project_part), EL_STR("}")); el_val_t result = call_neuron_mcp(EL_STR("findArtifacts"), args); return json_safe(result); @@ -28685,7 +28895,7 @@ el_val_t is_builtin_tool(el_val_t tool_name) { el_val_t next_bridge_id(void) { el_val_t prev = state_get(EL_STR("mcp_bridge_seq")); - el_val_t n = ({ el_val_t _if_result_442 = 0; if (str_eq(prev, EL_STR(""))) { _if_result_442 = (0); } else { _if_result_442 = (str_to_int(prev)); } _if_result_442; }); + el_val_t n = ({ el_val_t _if_result_489 = 0; if (str_eq(prev, EL_STR(""))) { _if_result_489 = (0); } else { _if_result_489 = (str_to_int(prev)); } _if_result_489; }); el_val_t next = (n + 1); state_set(EL_STR("mcp_bridge_seq"), int_to_str(next)); el_val_t uid = uuid_v4(); @@ -28699,12 +28909,12 @@ el_val_t handle_chat_plan(el_val_t body) { return EL_STR("{\"error\":\"message required\",\"plan\":null}"); } el_val_t req_model = json_get(body, EL_STR("model")); - el_val_t model = ({ el_val_t _if_result_443 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_443 = (chat_default_model()); } else { _if_result_443 = (req_model); } _if_result_443; }); + el_val_t model = ({ el_val_t _if_result_490 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_490 = (chat_default_model()); } else { _if_result_490 = (req_model); } _if_result_490; }); el_val_t op_home = env(EL_STR("HOME")); el_val_t op_user = env(EL_STR("USER")); - el_val_t op_display = ({ el_val_t _if_result_444 = 0; if (str_eq(op_user, EL_STR(""))) { _if_result_444 = (EL_STR("the current user")); } else { _if_result_444 = (op_user); } _if_result_444; }); + el_val_t op_display = ({ el_val_t _if_result_491 = 0; if (str_eq(op_user, EL_STR(""))) { _if_result_491 = (EL_STR("the current user")); } else { _if_result_491 = (op_user); } _if_result_491; }); el_val_t ctx = engram_compile(message); - el_val_t ctx_block = ({ el_val_t _if_result_445 = 0; if (str_eq(ctx, EL_STR(""))) { _if_result_445 = (EL_STR("")); } else { _if_result_445 = (el_str_concat(EL_STR("\n\n[CONTEXT]\n"), ctx)); } _if_result_445; }); + el_val_t ctx_block = ({ el_val_t _if_result_492 = 0; if (str_eq(ctx, EL_STR(""))) { _if_result_492 = (EL_STR("")); } else { _if_result_492 = (el_str_concat(EL_STR("\n\n[CONTEXT]\n"), ctx)); } _if_result_492; }); el_val_t plan_system = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("You are in PLAN MODE. Your job is to produce a concise step-by-step plan for the request below \xe2\x80\x94 WITHOUT executing it.\n\nReturn ONLY a JSON object. No markdown. No preamble. No explanation. Just the JSON:\n{\"steps\":[{\"id\":\"s1\",\"title\":\"<2-6 word title>\",\"detail\":\"\"},{\"id\":\"s2\",...}]}\n\nPlan rules:\n- 3-7 steps (more only when genuinely needed for a complex multi-file task)\n- Each step is one atomic, independently verifiable action\n- title: 2-6 words, imperative (e.g. \"Read config file\", \"Write updated handler\")\n- detail: exactly one sentence describing what happens\n- No tool calls. No execution. No side effects. The user approves before anything runs.\n\nOperator: "), op_display), EL_STR(" at ")), op_home), ctx_block), bounded_persona_floor()); el_val_t raw = llm_call_system(model, plan_system, message); el_val_t is_error = str_starts_with(raw, EL_STR("{\"error\"")); @@ -28716,10 +28926,10 @@ el_val_t handle_chat_plan(el_val_t body) { el_val_t scan_i = (str_len(raw) - 1); while (scan_i >= 0) { el_val_t ch = str_slice(raw, scan_i, (scan_i + 1)); - brace_end = ({ el_val_t _if_result_446 = 0; if ((str_eq(ch, EL_STR("}")) && (brace_end < 0))) { _if_result_446 = (scan_i); } else { _if_result_446 = (brace_end); } _if_result_446; }); - scan_i = ({ el_val_t _if_result_447 = 0; if ((brace_end >= 0)) { _if_result_447 = ((-1)); } else { _if_result_447 = ((scan_i - 1)); } _if_result_447; }); + brace_end = ({ el_val_t _if_result_493 = 0; if ((str_eq(ch, EL_STR("}")) && (brace_end < 0))) { _if_result_493 = (scan_i); } else { _if_result_493 = (brace_end); } _if_result_493; }); + scan_i = ({ el_val_t _if_result_494 = 0; if ((brace_end >= 0)) { _if_result_494 = ((-1)); } else { _if_result_494 = ((scan_i - 1)); } _if_result_494; }); } - el_val_t plan_json = ({ el_val_t _if_result_448 = 0; if ((brace_start >= 0)) { _if_result_448 = (({ el_val_t _if_result_449 = 0; if ((brace_end > brace_start)) { _if_result_449 = (str_slice(raw, brace_start, (brace_end + 1))); } else { _if_result_449 = (raw); } _if_result_449; })); } else { _if_result_448 = (raw); } _if_result_448; }); + el_val_t plan_json = ({ el_val_t _if_result_495 = 0; if ((brace_start >= 0)) { _if_result_495 = (({ el_val_t _if_result_496 = 0; if ((brace_end > brace_start)) { _if_result_496 = (str_slice(raw, brace_start, (brace_end + 1))); } else { _if_result_496 = (raw); } _if_result_496; })); } else { _if_result_495 = (raw); } _if_result_495; }); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"plan\":"), plan_json), EL_STR(",\"model\":\"")), json_safe(model)), EL_STR("\"}")); return 0; } @@ -28743,7 +28953,7 @@ el_val_t handle_chat_agentic(el_val_t body) { } state_set(EL_STR("agent_workspace_root"), ws_root); } else { - el_val_t own_root = ({ el_val_t _if_result_450 = 0; if (str_eq(sess_for_root, EL_STR(""))) { _if_result_450 = (EL_STR("")); } else { _if_result_450 = (state_get(el_str_concat(EL_STR("agent_workspace_root_"), sess_for_root))); } _if_result_450; }); + el_val_t own_root = ({ el_val_t _if_result_497 = 0; if (str_eq(sess_for_root, EL_STR(""))) { _if_result_497 = (EL_STR("")); } else { _if_result_497 = (state_get(el_str_concat(EL_STR("agent_workspace_root_"), sess_for_root))); } _if_result_497; }); state_set(EL_STR("agent_workspace_root"), own_root); } el_val_t screen_result = agentic_safety_screen(sess_for_root, message); @@ -28753,50 +28963,51 @@ 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_451 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_451 = (chat_default_model()); } else { _if_result_451 = (req_model); } _if_result_451; }); + el_val_t model = ({ el_val_t _if_result_498 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_498 = (chat_default_model()); } else { _if_result_498 = (req_model); } _if_result_498; }); el_val_t req_session = json_get(body, EL_STR("session_id")); - el_val_t session_valid = ({ el_val_t _if_result_452 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_452 = (1); } else { _if_result_452 = (session_exists(req_session)); } _if_result_452; }); + el_val_t session_valid = ({ el_val_t _if_result_499 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_499 = (1); } else { _if_result_499 = (session_exists(req_session)); } _if_result_499; }); 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 = conv_hist_key(req_session); el_val_t agentic_hist = state_get(hist_key); - el_val_t agentic_hist_len = ({ el_val_t _if_result_453 = 0; if (str_eq(agentic_hist, EL_STR(""))) { _if_result_453 = (0); } else { _if_result_453 = (json_array_len(agentic_hist)); } _if_result_453; }); + el_val_t agentic_hist_len = ({ el_val_t _if_result_500 = 0; if (str_eq(agentic_hist, EL_STR(""))) { _if_result_500 = (0); } else { _if_result_500 = (json_array_len(agentic_hist)); } _if_result_500; }); el_val_t ag_is_cont = engram_is_continuation(message, agentic_hist_len); - el_val_t ag_last_entry = ({ el_val_t _if_result_454 = 0; if (ag_is_cont) { _if_result_454 = (json_array_get(agentic_hist, (agentic_hist_len - 1))); } else { _if_result_454 = (EL_STR("")); } _if_result_454; }); - el_val_t ag_last_content = ({ el_val_t _if_result_455 = 0; if (!str_eq(ag_last_entry, EL_STR(""))) { _if_result_455 = (json_get(ag_last_entry, EL_STR("content"))); } else { _if_result_455 = (EL_STR("")); } _if_result_455; }); - el_val_t ag_thread_snip = ({ el_val_t _if_result_456 = 0; if ((str_len(ag_last_content) > 150)) { _if_result_456 = (str_slice(ag_last_content, 0, 150)); } else { _if_result_456 = (ag_last_content); } _if_result_456; }); - el_val_t ag_seed = ({ el_val_t _if_result_457 = 0; if (!str_eq(ag_thread_snip, EL_STR(""))) { _if_result_457 = (el_str_concat(el_str_concat(ag_thread_snip, EL_STR(" ")), message)); } else { _if_result_457 = (message); } _if_result_457; }); + el_val_t ag_last_entry = ({ el_val_t _if_result_501 = 0; if (ag_is_cont) { _if_result_501 = (json_array_get(agentic_hist, (agentic_hist_len - 1))); } else { _if_result_501 = (EL_STR("")); } _if_result_501; }); + el_val_t ag_last_content = ({ el_val_t _if_result_502 = 0; if (!str_eq(ag_last_entry, EL_STR(""))) { _if_result_502 = (json_get(ag_last_entry, EL_STR("content"))); } else { _if_result_502 = (EL_STR("")); } _if_result_502; }); + el_val_t ag_thread_snip = ({ el_val_t _if_result_503 = 0; if ((str_len(ag_last_content) > 150)) { _if_result_503 = (str_slice(ag_last_content, 0, 150)); } else { _if_result_503 = (ag_last_content); } _if_result_503; }); + el_val_t ag_seed = ({ el_val_t _if_result_504 = 0; if (!str_eq(ag_thread_snip, EL_STR(""))) { _if_result_504 = (el_str_concat(el_str_concat(ag_thread_snip, EL_STR(" ")), message)); } else { _if_result_504 = (message); } _if_result_504; }); 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_458 = 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_459 = 0; if (ag_profile_ok) { _if_result_459 = (ag_profile_nodes); } else { _if_result_459 = (engram_search_json(EL_STR("user profile preferences name"), 8)); } _if_result_459; }); 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_460 = 0; if (ag_work_ok) { _if_result_460 = (ag_work_nodes); } else { _if_result_460 = (engram_search_json(EL_STR("active project task current in_progress"), 6)); } _if_result_460; }); 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_461 = 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_461 = (({ el_val_t _if_result_462 = 0; if ((str_len(acc) > 350)) { _if_result_462 = (str_slice(acc, 0, 350)); } else { _if_result_462 = (acc); } _if_result_462; })); } else { _if_result_461 = (EL_STR("")); } _if_result_461; }); 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_458 = (({ el_val_t _if_result_463 = 0; if (((ag_has_profile || ag_has_work) || ag_has_cont)) { el_val_t p = ({ el_val_t _if_result_464 = 0; if (ag_has_profile) { _if_result_464 = (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_464 = (EL_STR("")); } _if_result_464; }); el_val_t w = ({ el_val_t _if_result_465 = 0; if (ag_has_work) { _if_result_465 = (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_465 = (EL_STR("")); } _if_result_465; }); el_val_t c = ({ el_val_t _if_result_466 = 0; if (ag_has_cont) { _if_result_466 = (el_str_concat(el_str_concat(EL_STR("[CONTINUING FROM LAST SESSION]\n"), ag_continuity_snip), EL_STR("\n\n"))); } else { _if_result_466 = (EL_STR("")); } _if_result_466; }); _if_result_463 = (el_str_concat(el_str_concat(el_str_concat(EL_STR("\n\n"), p), w), c)); } else { _if_result_463 = (EL_STR("")); } _if_result_463; })); } else { _if_result_458 = (EL_STR("")); } _if_result_458; }); + el_val_t ag_session_preload = ({ el_val_t _if_result_505 = 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_506 = 0; if (ag_profile_ok) { _if_result_506 = (ag_profile_nodes); } else { _if_result_506 = (engram_search_json(EL_STR("user profile preferences name"), 8)); } _if_result_506; }); 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_507 = 0; if (ag_work_ok) { _if_result_507 = (ag_work_nodes); } else { _if_result_507 = (engram_search_json(EL_STR("active project task current in_progress"), 6)); } _if_result_507; }); 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_508 = 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_508 = (utf8_safe_slice(acc, 350)); } else { _if_result_508 = (EL_STR("")); } _if_result_508; }); 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_505 = (({ el_val_t _if_result_509 = 0; if (((ag_has_profile || ag_has_work) || ag_has_cont)) { el_val_t p = ({ el_val_t _if_result_510 = 0; if (ag_has_profile) { _if_result_510 = (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_510 = (EL_STR("")); } _if_result_510; }); el_val_t w = ({ el_val_t _if_result_511 = 0; if (ag_has_work) { _if_result_511 = (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_511 = (EL_STR("")); } _if_result_511; }); el_val_t c = ({ el_val_t _if_result_512 = 0; if (ag_has_cont) { _if_result_512 = (el_str_concat(el_str_concat(EL_STR("[CONTINUING FROM LAST SESSION]\n"), ag_continuity_snip), EL_STR("\n\n"))); } else { _if_result_512 = (EL_STR("")); } _if_result_512; }); _if_result_509 = (el_str_concat(el_str_concat(el_str_concat(EL_STR("\n\n"), p), w), c)); } else { _if_result_509 = (EL_STR("")); } _if_result_509; })); } else { _if_result_505 = (EL_STR("")); } _if_result_505; }); el_val_t system = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(identity, bounded_persona_floor()), 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), receipt_rule()); el_val_t api_key = agentic_api_key(); - el_val_t tools_json = agentic_tools_all(); + el_val_t tools_lane_openai = (!str_eq(llm_base_url(), EL_STR("")) && str_eq(llm_wire_format(), EL_STR("openai"))); + el_val_t tools_json = ({ el_val_t _if_result_513 = 0; if (tools_lane_openai) { _if_result_513 = (agentic_tools_no_web()); } else { _if_result_513 = (agentic_tools_all()); } _if_result_513; }); el_val_t safe_msg = json_safe(message); 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_467 = 0; if (str_eq(img_mt_raw, EL_STR(""))) { _if_result_467 = (EL_STR("image/png")); } else { _if_result_467 = (img_mt_raw); } _if_result_467; }); - el_val_t cur_user_content = ({ el_val_t _if_result_468 = 0; if (str_eq(img_b64, EL_STR(""))) { _if_result_468 = (el_str_concat(el_str_concat(EL_STR("\""), safe_msg), EL_STR("\""))); } else { _if_result_468 = (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_468; }); - el_val_t prior_messages = ({ el_val_t _if_result_469 = 0; if ((agentic_hist_len > 0)) { el_val_t inner = str_slice(agentic_hist, 1, (str_len(agentic_hist) - 1)); _if_result_469 = (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_469 = (el_str_concat(el_str_concat(EL_STR("[{\"role\":\"user\",\"content\":"), cur_user_content), EL_STR("}]"))); } _if_result_469; }); + el_val_t img_mt = ({ el_val_t _if_result_514 = 0; if (str_eq(img_mt_raw, EL_STR(""))) { _if_result_514 = (EL_STR("image/png")); } else { _if_result_514 = (img_mt_raw); } _if_result_514; }); + el_val_t cur_user_content = ({ el_val_t _if_result_515 = 0; if (str_eq(img_b64, EL_STR(""))) { _if_result_515 = (el_str_concat(el_str_concat(EL_STR("\""), safe_msg), EL_STR("\""))); } else { _if_result_515 = (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_515; }); + el_val_t prior_messages = ({ el_val_t _if_result_516 = 0; if ((agentic_hist_len > 0)) { el_val_t inner = str_slice(agentic_hist, 1, (str_len(agentic_hist) - 1)); _if_result_516 = (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_516 = (el_str_concat(el_str_concat(EL_STR("[{\"role\":\"user\",\"content\":"), cur_user_content), EL_STR("}]"))); } _if_result_516; }); 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_470 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_470 = (next_bridge_id()); } else { _if_result_470 = (req_session); } _if_result_470; }); + el_val_t session_id = ({ el_val_t _if_result_517 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_517 = (next_bridge_id()); } else { _if_result_517 = (req_session); } _if_result_517; }); 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_471 = 0; if (str_eq(req_ask_all, EL_STR("true"))) { _if_result_471 = (EL_STR("true")); } else { _if_result_471 = (EL_STR("")); } _if_result_471; })); - 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_472 = 0; if (use_openai) { _if_result_472 = (openai_chat_complete(model, llm_base_url(), agentic_api_key(), safe_sys, messages)); } else { _if_result_472 = (agentic_loop(session_id, model, safe_sys, tools_json, messages, h, EL_STR(""))); } _if_result_472; }); + state_set(el_str_concat(EL_STR("require_approval_"), session_id), ({ el_val_t _if_result_518 = 0; if (str_eq(req_ask_all, EL_STR("true"))) { _if_result_518 = (EL_STR("true")); } else { _if_result_518 = (EL_STR("")); } _if_result_518; })); + el_val_t use_openai = tools_lane_openai; + el_val_t result = ({ el_val_t _if_result_519 = 0; if (use_openai) { _if_result_519 = (openai_agentic_loop(session_id, model, safe_sys, tools_json, messages, EL_STR(""))); } else { _if_result_519 = (agentic_loop(session_id, model, safe_sys, tools_json, messages, h, EL_STR(""))); } _if_result_519; }); el_val_t reply_text = json_get(result, EL_STR("reply")); el_val_t turn_tools = json_get_raw(result, EL_STR("tools_used")); el_val_t turn_sources = json_get(result, EL_STR("sources")); el_val_t turn_receipt = tool_receipt(turn_tools, turn_sources); el_val_t record_turn = (!str_eq(reply_text, EL_STR("")) && !is_utility_request(body, req_session)); - el_val_t discard_hist = ({ el_val_t _if_result_473 = 0; if (record_turn) { el_val_t updated = hist_append(agentic_hist, EL_STR("user"), message); el_val_t updated2 = hist_append(updated, EL_STR("assistant"), el_str_concat(reply_text, turn_receipt)); el_val_t trimmed = ({ el_val_t _if_result_474 = 0; if ((json_array_len(updated2) > 40)) { _if_result_474 = (hist_trim(updated2)); } else { _if_result_474 = (updated2); } _if_result_474; }); (void)(state_set(hist_key, trimmed)); (void)(conv_history_persist(req_session, trimmed)); _if_result_473 = (1); } else { _if_result_473 = (0); } _if_result_473; }); + el_val_t discard_hist = ({ el_val_t _if_result_520 = 0; if (record_turn) { el_val_t updated = hist_append(agentic_hist, EL_STR("user"), message); el_val_t updated2 = hist_append(updated, EL_STR("assistant"), el_str_concat(reply_text, turn_receipt)); el_val_t trimmed = ({ el_val_t _if_result_521 = 0; if ((json_array_len(updated2) > 40)) { _if_result_521 = (hist_trim(updated2)); } else { _if_result_521 = (updated2); } _if_result_521; }); (void)(state_set(hist_key, trimmed)); (void)(conv_history_persist(req_session, trimmed)); _if_result_520 = (1); } else { _if_result_520 = (0); } _if_result_520; }); return result; return 0; } @@ -28823,40 +29034,40 @@ el_val_t agentic_loop(el_val_t session_id, el_val_t model, el_val_t safe_sys, el state_set(el_str_concat(EL_STR("run_progress_"), session_id), EL_STR("")); } while (keep_going && (iteration < 12)) { - el_val_t cont_frag = ({ el_val_t _if_result_475 = 0; if (str_eq(container_id, EL_STR(""))) { _if_result_475 = (EL_STR("")); } else { _if_result_475 = (el_str_concat(el_str_concat(EL_STR(",\"container\":\""), container_id), EL_STR("\""))); } _if_result_475; }); + el_val_t cont_frag = ({ el_val_t _if_result_522 = 0; if (str_eq(container_id, EL_STR(""))) { _if_result_522 = (EL_STR("")); } else { _if_result_522 = (el_str_concat(el_str_concat(EL_STR(",\"container\":\""), container_id), EL_STR("\""))); } _if_result_522; }); el_val_t req_body = 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\":\""), model), EL_STR("\"")), cont_frag), EL_STR(",\"max_tokens\":16384")), EL_STR(",\"tool_choice\":{\"type\":\"auto\",\"disable_parallel_tool_use\":true}")), EL_STR(",\"system\":\"")), safe_sys), EL_STR("\"")), EL_STR(",\"tools\":")), tools_eff), EL_STR(",\"messages\":")), messages), EL_STR("}")); if (!str_eq(session_id, EL_STR(""))) { el_val_t start_key = el_str_concat(EL_STR("run_progress_"), session_id); el_val_t start_prev = state_get(start_key); el_val_t start_entry = el_str_concat(el_str_concat(EL_STR("{\"i\":"), int_to_str(iteration)), EL_STR(",\"t\":\"\",\"tool\":\"__working__\"}")); - el_val_t start_next = ({ el_val_t _if_result_476 = 0; if (str_eq(start_prev, EL_STR(""))) { _if_result_476 = (start_entry); } else { _if_result_476 = (el_str_concat(el_str_concat(start_prev, EL_STR(",")), start_entry)); } _if_result_476; }); + el_val_t start_next = ({ el_val_t _if_result_523 = 0; if (str_eq(start_prev, EL_STR(""))) { _if_result_523 = (start_entry); } else { _if_result_523 = (el_str_concat(el_str_concat(start_prev, EL_STR(",")), start_entry)); } _if_result_523; }); state_set(start_key, start_next); } el_val_t raw_resp = http_post_with_headers(api_url, req_body, h); el_val_t is_error = ((str_starts_with(raw_resp, EL_STR("{\"error\"")) || str_starts_with(raw_resp, EL_STR("{\"type\":\"error\""))) || str_contains(raw_resp, EL_STR("authentication_error"))); - el_val_t ws_pos = ({ el_val_t _if_result_477 = 0; if (is_error) { _if_result_477 = (str_index_of(tools_eff, EL_STR("\"type\":\"web_search_"))); } else { _if_result_477 = ((0 - 1)); } _if_result_477; }); - el_val_t ws_rest = ({ el_val_t _if_result_478 = 0; if ((ws_pos >= 0)) { _if_result_478 = (str_slice(tools_eff, (ws_pos + 8), str_len(tools_eff))); } else { _if_result_478 = (EL_STR("")); } _if_result_478; }); - el_val_t ws_vend = ({ el_val_t _if_result_479 = 0; if ((ws_pos >= 0)) { _if_result_479 = (str_index_of(ws_rest, EL_STR("\""))); } else { _if_result_479 = ((0 - 1)); } _if_result_479; }); - el_val_t ws_cur = ({ el_val_t _if_result_480 = 0; if ((ws_vend > 0)) { _if_result_480 = (str_slice(ws_rest, 0, ws_vend)); } else { _if_result_480 = (EL_STR("")); } _if_result_480; }); + el_val_t ws_pos = ({ el_val_t _if_result_524 = 0; if (is_error) { _if_result_524 = (str_index_of(tools_eff, EL_STR("\"type\":\"web_search_"))); } else { _if_result_524 = ((0 - 1)); } _if_result_524; }); + el_val_t ws_rest = ({ el_val_t _if_result_525 = 0; if ((ws_pos >= 0)) { _if_result_525 = (str_slice(tools_eff, (ws_pos + 8), str_len(tools_eff))); } else { _if_result_525 = (EL_STR("")); } _if_result_525; }); + el_val_t ws_vend = ({ el_val_t _if_result_526 = 0; if ((ws_pos >= 0)) { _if_result_526 = (str_index_of(ws_rest, EL_STR("\""))); } else { _if_result_526 = ((0 - 1)); } _if_result_526; }); + el_val_t ws_cur = ({ el_val_t _if_result_527 = 0; if ((ws_vend > 0)) { _if_result_527 = (str_slice(ws_rest, 0, ws_vend)); } else { _if_result_527 = (EL_STR("")); } _if_result_527; }); el_val_t ws_conflict = str_contains(raw_resp, EL_STR("programmatic tool calling")); el_val_t can_fallback = ((((((is_error && !ws_drift) && (ws_pos >= 0)) && (ws_vend > 0)) && !str_eq(ws_cur, EL_STR(""))) && !str_eq(ws_cur, EL_STR("web_search_20250305"))) && (str_contains(raw_resp, ws_cur) || ws_conflict)); - tools_eff = ({ el_val_t _if_result_481 = 0; if (can_fallback) { _if_result_481 = (el_str_concat(el_str_concat(str_slice(tools_eff, 0, (ws_pos + 8)), EL_STR("web_search_20250305")), str_slice(ws_rest, ws_vend, str_len(ws_rest)))); } else { _if_result_481 = (tools_eff); } _if_result_481; }); - ws_drift = ({ el_val_t _if_result_482 = 0; if (can_fallback) { _if_result_482 = (1); } else { _if_result_482 = (ws_drift); } _if_result_482; }); + tools_eff = ({ el_val_t _if_result_528 = 0; if (can_fallback) { _if_result_528 = (el_str_concat(el_str_concat(str_slice(tools_eff, 0, (ws_pos + 8)), EL_STR("web_search_20250305")), str_slice(ws_rest, ws_vend, str_len(ws_rest)))); } else { _if_result_528 = (tools_eff); } _if_result_528; }); + ws_drift = ({ el_val_t _if_result_529 = 0; if (can_fallback) { _if_result_529 = (1); } else { _if_result_529 = (ws_drift); } _if_result_529; }); if (can_fallback) { println(el_str_concat(el_str_concat(EL_STR("[soul] DRIFT: web_search variant '"), ws_cur), EL_STR("' rejected by API - fell back to web_search_20250305"))); state_set(EL_STR("web_search_version_drift"), ws_cur); } if (is_error && !can_fallback) { - el_val_t err_head = ({ el_val_t _if_result_483 = 0; if ((str_len(raw_resp) > 220)) { _if_result_483 = (str_slice(raw_resp, 0, 220)); } else { _if_result_483 = (raw_resp); } _if_result_483; }); + el_val_t err_head = ({ el_val_t _if_result_530 = 0; if ((str_len(raw_resp) > 220)) { _if_result_530 = (str_slice(raw_resp, 0, 220)); } else { _if_result_530 = (raw_resp); } _if_result_530; }); println(el_str_concat(EL_STR("[soul] llm error: "), err_head)); return EL_STR("{\"error\":\"llm unavailable\",\"reply\":\"\"}"); } el_val_t stop_reason = json_get(raw_resp, EL_STR("stop_reason")); el_val_t cont_raw = json_get_raw(raw_resp, EL_STR("container")); - el_val_t cont_id_new = ({ el_val_t _if_result_484 = 0; if ((!str_eq(cont_raw, EL_STR("")) && !str_eq(cont_raw, EL_STR("null")))) { _if_result_484 = (json_get(cont_raw, EL_STR("id"))); } else { _if_result_484 = (EL_STR("")); } _if_result_484; }); - container_id = ({ el_val_t _if_result_485 = 0; if (!str_eq(cont_id_new, EL_STR(""))) { _if_result_485 = (cont_id_new); } else { _if_result_485 = (container_id); } _if_result_485; }); + el_val_t cont_id_new = ({ el_val_t _if_result_531 = 0; if ((!str_eq(cont_raw, EL_STR("")) && !str_eq(cont_raw, EL_STR("null")))) { _if_result_531 = (json_get(cont_raw, EL_STR("id"))); } else { _if_result_531 = (EL_STR("")); } _if_result_531; }); + container_id = ({ el_val_t _if_result_532 = 0; if (!str_eq(cont_id_new, EL_STR(""))) { _if_result_532 = (cont_id_new); } else { _if_result_532 = (container_id); } _if_result_532; }); el_val_t content_arr = json_get_raw(raw_resp, EL_STR("content")); - el_val_t eff_content = ({ el_val_t _if_result_486 = 0; if (str_eq(content_arr, EL_STR(""))) { _if_result_486 = (EL_STR("[]")); } else { _if_result_486 = (content_arr); } _if_result_486; }); + el_val_t eff_content = ({ el_val_t _if_result_533 = 0; if (str_eq(content_arr, EL_STR(""))) { _if_result_533 = (EL_STR("[]")); } else { _if_result_533 = (content_arr); } _if_result_533; }); el_val_t text_out = EL_STR(""); el_val_t has_tool = 0; el_val_t tool_id = EL_STR(""); @@ -28872,21 +29083,21 @@ el_val_t agentic_loop(el_val_t session_id, el_val_t model, el_val_t safe_sys, el el_val_t cit_raw = json_get_raw(block, EL_STR("citations")); el_val_t has_cit = (!str_eq(cit_raw, EL_STR("")) && !str_eq(cit_raw, EL_STR("null"))); el_val_t btype_scan = json_get(block, EL_STR("type")); - el_val_t btype = ({ el_val_t _if_result_487 = 0; if (has_cit) { _if_result_487 = (EL_STR("text")); } else { _if_result_487 = (btype_scan); } _if_result_487; }); + el_val_t btype = ({ el_val_t _if_result_534 = 0; if (has_cit) { _if_result_534 = (EL_STR("text")); } else { _if_result_534 = (btype_scan); } _if_result_534; }); el_val_t is_text = str_eq(btype, EL_STR("text")); - el_val_t btext = ({ el_val_t _if_result_488 = 0; if (is_text) { _if_result_488 = (json_get(block, EL_STR("text"))); } else { _if_result_488 = (EL_STR("")); } _if_result_488; }); - text_out = ({ el_val_t _if_result_489 = 0; if (is_text) { _if_result_489 = (el_str_concat(el_str_concat(text_out, text_join_sep(text_out, btext, saw_nontext)), btext)); } else { _if_result_489 = (text_out); } _if_result_489; }); - saw_nontext = ({ el_val_t _if_result_490 = 0; if (is_text) { _if_result_490 = (0); } else { _if_result_490 = (1); } _if_result_490; }); + el_val_t btext = ({ el_val_t _if_result_535 = 0; if (is_text) { _if_result_535 = (json_get(block, EL_STR("text"))); } else { _if_result_535 = (EL_STR("")); } _if_result_535; }); + text_out = ({ el_val_t _if_result_536 = 0; if (is_text) { _if_result_536 = (el_str_concat(el_str_concat(text_out, text_join_sep(text_out, btext, saw_nontext)), btext)); } else { _if_result_536 = (text_out); } _if_result_536; }); + saw_nontext = ({ el_val_t _if_result_537 = 0; if (is_text) { _if_result_537 = (0); } else { _if_result_537 = (1); } _if_result_537; }); src_log = provenance_add_sources(block, btype, has_cit, cit_raw, src_log); el_val_t is_srv = str_eq(btype, EL_STR("server_tool_use")); - el_val_t srv_name_raw = ({ el_val_t _if_result_491 = 0; if (is_srv) { _if_result_491 = (json_get(block, EL_STR("name"))); } else { _if_result_491 = (EL_STR("")); } _if_result_491; }); - el_val_t srv_name = ({ el_val_t _if_result_492 = 0; if ((is_srv && str_eq(srv_name_raw, EL_STR("")))) { _if_result_492 = (EL_STR("server_tool")); } else { _if_result_492 = (srv_name_raw); } _if_result_492; }); - srv_log = ({ el_val_t _if_result_493 = 0; if (is_srv) { _if_result_493 = (({ el_val_t _if_result_494 = 0; if (str_eq(srv_log, EL_STR(""))) { _if_result_494 = (el_str_concat(el_str_concat(EL_STR("\""), srv_name), EL_STR("\""))); } else { _if_result_494 = (el_str_concat(el_str_concat(el_str_concat(srv_log, EL_STR(",\"")), srv_name), EL_STR("\""))); } _if_result_494; })); } else { _if_result_493 = (srv_log); } _if_result_493; }); + el_val_t srv_name_raw = ({ el_val_t _if_result_538 = 0; if (is_srv) { _if_result_538 = (json_get(block, EL_STR("name"))); } else { _if_result_538 = (EL_STR("")); } _if_result_538; }); + el_val_t srv_name = ({ el_val_t _if_result_539 = 0; if ((is_srv && str_eq(srv_name_raw, EL_STR("")))) { _if_result_539 = (EL_STR("server_tool")); } else { _if_result_539 = (srv_name_raw); } _if_result_539; }); + srv_log = ({ el_val_t _if_result_540 = 0; if (is_srv) { _if_result_540 = (({ el_val_t _if_result_541 = 0; if (str_eq(srv_log, EL_STR(""))) { _if_result_541 = (el_str_concat(el_str_concat(EL_STR("\""), srv_name), EL_STR("\""))); } else { _if_result_541 = (el_str_concat(el_str_concat(el_str_concat(srv_log, EL_STR(",\"")), srv_name), EL_STR("\""))); } _if_result_541; })); } else { _if_result_540 = (srv_log); } _if_result_540; }); el_val_t is_new_tool = (str_eq(btype, EL_STR("tool_use")) && !has_tool); - has_tool = ({ el_val_t _if_result_495 = 0; if (is_new_tool) { _if_result_495 = (1); } else { _if_result_495 = (has_tool); } _if_result_495; }); - tool_id = ({ el_val_t _if_result_496 = 0; if (is_new_tool) { _if_result_496 = (json_get(block, EL_STR("id"))); } else { _if_result_496 = (tool_id); } _if_result_496; }); - tool_name = ({ el_val_t _if_result_497 = 0; if (is_new_tool) { _if_result_497 = (json_get(block, EL_STR("name"))); } else { _if_result_497 = (tool_name); } _if_result_497; }); - tool_input = ({ el_val_t _if_result_498 = 0; if (is_new_tool) { _if_result_498 = (json_get_raw(block, EL_STR("input"))); } else { _if_result_498 = (tool_input); } _if_result_498; }); + has_tool = ({ el_val_t _if_result_542 = 0; if (is_new_tool) { _if_result_542 = (1); } else { _if_result_542 = (has_tool); } _if_result_542; }); + tool_id = ({ el_val_t _if_result_543 = 0; if (is_new_tool) { _if_result_543 = (json_get(block, EL_STR("id"))); } else { _if_result_543 = (tool_id); } _if_result_543; }); + tool_name = ({ el_val_t _if_result_544 = 0; if (is_new_tool) { _if_result_544 = (json_get(block, EL_STR("name"))); } else { _if_result_544 = (tool_name); } _if_result_544; }); + tool_input = ({ el_val_t _if_result_545 = 0; if (is_new_tool) { _if_result_545 = (json_get_raw(block, EL_STR("input"))); } else { _if_result_545 = (tool_input); } _if_result_545; }); ci = (ci + 1); } el_val_t is_tool_turn = (str_eq(stop_reason, EL_STR("tool_use")) && has_tool); @@ -28895,72 +29106,72 @@ el_val_t agentic_loop(el_val_t session_id, el_val_t model, el_val_t safe_sys, el println(el_str_concat(EL_STR("[soul] DRIFT: unknown stop_reason from API: "), stop_reason)); } el_val_t always_key = el_str_concat(EL_STR("always_allow_"), session_id); - el_val_t always_list = ({ el_val_t _if_result_499 = 0; if (!str_eq(session_id, EL_STR(""))) { _if_result_499 = (state_get(always_key)); } else { _if_result_499 = (EL_STR("")); } _if_result_499; }); + el_val_t always_list = ({ el_val_t _if_result_546 = 0; if (!str_eq(session_id, EL_STR(""))) { _if_result_546 = (state_get(always_key)); } else { _if_result_546 = (EL_STR("")); } _if_result_546; }); 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_500 = 0; if (is_tool_turn) { _if_result_500 = (classify_tool_risk(tool_name, tool_input)); } else { _if_result_500 = (EL_STR("")); } _if_result_500; }); + el_val_t risk_tier = ({ el_val_t _if_result_547 = 0; if (is_tool_turn) { _if_result_547 = (classify_tool_risk(tool_name, tool_input)); } else { _if_result_547 = (EL_STR("")); } _if_result_547; }); 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_501 = 0; if ((is_tool_turn && !needs_bridge)) { _if_result_501 = (dispatch_tool(tool_name, tool_input)); } else { _if_result_501 = (EL_STR("")); } _if_result_501; }); - el_val_t tool_result = ({ el_val_t _if_result_502 = 0; if ((str_len(tool_result_raw) > 6000)) { _if_result_502 = (el_str_concat(str_slice(tool_result_raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_502 = (tool_result_raw); } _if_result_502; }); + el_val_t tool_result_raw = ({ el_val_t _if_result_548 = 0; if ((is_tool_turn && !needs_bridge)) { _if_result_548 = (dispatch_tool(tool_name, tool_input)); } else { _if_result_548 = (EL_STR("")); } _if_result_548; }); + el_val_t tool_result = ({ el_val_t _if_result_549 = 0; if ((str_len(tool_result_raw) > 6000)) { _if_result_549 = (el_str_concat(str_slice(tool_result_raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_549 = (tool_result_raw); } _if_result_549; }); 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_503 = 0; if (is_tool_turn) { _if_result_503 = (({ el_val_t _if_result_504 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_504 = (tool_quoted); } else { _if_result_504 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), tool_quoted)); } _if_result_504; })); } else { _if_result_503 = (tools_log); } _if_result_503; }); - tools_log = ({ el_val_t _if_result_505 = 0; if (str_eq(srv_log, EL_STR(""))) { _if_result_505 = (tools_log); } else { _if_result_505 = (({ el_val_t _if_result_506 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_506 = (srv_log); } else { _if_result_506 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), srv_log)); } _if_result_506; })); } _if_result_505; }); - sources_all = ({ el_val_t _if_result_507 = 0; if (str_eq(src_log, EL_STR(""))) { _if_result_507 = (sources_all); } else { _if_result_507 = (({ el_val_t _if_result_508 = 0; if (str_eq(sources_all, EL_STR(""))) { _if_result_508 = (src_log); } else { _if_result_508 = (el_str_concat(el_str_concat(sources_all, EL_STR("; ")), src_log)); } _if_result_508; })); } _if_result_507; }); + tools_log = ({ el_val_t _if_result_550 = 0; if (is_tool_turn) { _if_result_550 = (({ el_val_t _if_result_551 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_551 = (tool_quoted); } else { _if_result_551 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), tool_quoted)); } _if_result_551; })); } else { _if_result_550 = (tools_log); } _if_result_550; }); + tools_log = ({ el_val_t _if_result_552 = 0; if (str_eq(srv_log, EL_STR(""))) { _if_result_552 = (tools_log); } else { _if_result_552 = (({ el_val_t _if_result_553 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_553 = (srv_log); } else { _if_result_553 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), srv_log)); } _if_result_553; })); } _if_result_552; }); + sources_all = ({ el_val_t _if_result_554 = 0; if (str_eq(src_log, EL_STR(""))) { _if_result_554 = (sources_all); } else { _if_result_554 = (({ el_val_t _if_result_555 = 0; if (str_eq(sources_all, EL_STR(""))) { _if_result_555 = (src_log); } else { _if_result_555 = (el_str_concat(el_str_concat(sources_all, EL_STR("; ")), src_log)); } _if_result_555; })); } _if_result_554; }); 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); el_val_t is_pause_resume = (is_pause && !needs_bridge); - messages = ({ el_val_t _if_result_509 = 0; if (local_continue) { el_val_t inner2 = str_slice(messages_with_assistant, 1, (str_len(messages_with_assistant) - 1)); _if_result_509 = (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_509 = (({ el_val_t _if_result_510 = 0; if (is_pause_resume) { _if_result_510 = (messages_with_assistant); } else { _if_result_510 = (messages); } _if_result_510; })); } _if_result_509; }); + messages = ({ el_val_t _if_result_556 = 0; if (local_continue) { el_val_t inner2 = str_slice(messages_with_assistant, 1, (str_len(messages_with_assistant) - 1)); _if_result_556 = (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_556 = (({ el_val_t _if_result_557 = 0; if (is_pause_resume) { _if_result_557 = (messages_with_assistant); } else { _if_result_557 = (messages); } _if_result_557; })); } _if_result_556; }); if (!str_eq(session_id, EL_STR("")) && !can_fallback) { 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_511 = 0; if ((str_len(text_out) > 280)) { _if_result_511 = (str_slice(text_out, 0, 280)); } else { _if_result_511 = (text_out); } _if_result_511; }); + el_val_t prog_snip = ({ el_val_t _if_result_558 = 0; if ((str_len(text_out) > 280)) { _if_result_558 = (str_slice(text_out, 0, 280)); } else { _if_result_558 = (text_out); } _if_result_558; }); 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_512 = 0; if (str_eq(prog_prev, EL_STR(""))) { _if_result_512 = (prog_entry); } else { _if_result_512 = (el_str_concat(el_str_concat(prog_prev, EL_STR(",")), prog_entry)); } _if_result_512; }); + el_val_t prog_next = ({ el_val_t _if_result_559 = 0; if (str_eq(prog_prev, EL_STR(""))) { _if_result_559 = (prog_entry); } else { _if_result_559 = (el_str_concat(el_str_concat(prog_prev, EL_STR(",")), prog_entry)); } _if_result_559; }); state_set(prog_key, prog_next); } - pending = ({ el_val_t _if_result_513 = 0; if (needs_bridge) { _if_result_513 = (1); } else { _if_result_513 = (pending); } _if_result_513; }); - pend_tool_id = ({ el_val_t _if_result_514 = 0; if (needs_bridge) { _if_result_514 = (tool_id); } else { _if_result_514 = (pend_tool_id); } _if_result_514; }); - pend_tool_name = ({ el_val_t _if_result_515 = 0; if (needs_bridge) { _if_result_515 = (tool_name); } else { _if_result_515 = (pend_tool_name); } _if_result_515; }); - pend_tool_input = ({ el_val_t _if_result_516 = 0; if (needs_bridge) { _if_result_516 = (tool_input); } else { _if_result_516 = (pend_tool_input); } _if_result_516; }); - pend_tool_tier = ({ el_val_t _if_result_517 = 0; if (needs_bridge) { _if_result_517 = (risk_tier); } else { _if_result_517 = (pend_tool_tier); } _if_result_517; }); - pend_narration = ({ el_val_t _if_result_518 = 0; if (needs_bridge) { _if_result_518 = (text_out); } else { _if_result_518 = (pend_narration); } _if_result_518; }); + pending = ({ el_val_t _if_result_560 = 0; if (needs_bridge) { _if_result_560 = (1); } else { _if_result_560 = (pending); } _if_result_560; }); + pend_tool_id = ({ el_val_t _if_result_561 = 0; if (needs_bridge) { _if_result_561 = (tool_id); } else { _if_result_561 = (pend_tool_id); } _if_result_561; }); + pend_tool_name = ({ el_val_t _if_result_562 = 0; if (needs_bridge) { _if_result_562 = (tool_name); } else { _if_result_562 = (pend_tool_name); } _if_result_562; }); + pend_tool_input = ({ el_val_t _if_result_563 = 0; if (needs_bridge) { _if_result_563 = (tool_input); } else { _if_result_563 = (pend_tool_input); } _if_result_563; }); + pend_tool_tier = ({ el_val_t _if_result_564 = 0; if (needs_bridge) { _if_result_564 = (risk_tier); } else { _if_result_564 = (pend_tool_tier); } _if_result_564; }); + pend_narration = ({ el_val_t _if_result_565 = 0; if (needs_bridge) { _if_result_565 = (text_out); } else { _if_result_565 = (pend_narration); } _if_result_565; }); if (needs_bridge) { - bridge_save(session_id, model, safe_sys, tools_json, messages_with_assistant, tools_log, pend_tool_id); + bridge_save(session_id, model, safe_sys, tools_json, messages_with_assistant, tools_log, pend_tool_id, EL_STR("anthropic")); } - final_text = ({ el_val_t _if_result_519 = 0; if ((!is_tool_turn && !can_fallback)) { _if_result_519 = (el_str_concat(el_str_concat(final_text, text_join_sep(final_text, text_out, 1)), text_out)); } else { _if_result_519 = (final_text); } _if_result_519; }); - final_text = ({ el_val_t _if_result_520 = 0; if ((str_eq(stop_reason, EL_STR("max_tokens")) && has_tool)) { _if_result_520 = (el_str_concat(final_text, EL_STR("\n\n[Output limit reached mid-action - the last planned action did not run. Ask me to continue to finish it.]"))); } else { _if_result_520 = (final_text); } _if_result_520; }); - keep_going = ({ el_val_t _if_result_521 = 0; if (((local_continue || is_pause_resume) || can_fallback)) { _if_result_521 = (keep_going); } else { _if_result_521 = (0); } _if_result_521; }); + final_text = ({ el_val_t _if_result_566 = 0; if ((!is_tool_turn && !can_fallback)) { _if_result_566 = (el_str_concat(el_str_concat(final_text, text_join_sep(final_text, text_out, 1)), text_out)); } else { _if_result_566 = (final_text); } _if_result_566; }); + final_text = ({ el_val_t _if_result_567 = 0; if ((str_eq(stop_reason, EL_STR("max_tokens")) && has_tool)) { _if_result_567 = (el_str_concat(final_text, EL_STR("\n\n[Output limit reached mid-action - the last planned action did not run. Ask me to continue to finish it.]"))); } else { _if_result_567 = (final_text); } _if_result_567; }); + keep_going = ({ el_val_t _if_result_568 = 0; if (((local_continue || is_pause_resume) || can_fallback)) { _if_result_568 = (keep_going); } else { _if_result_568 = (0); } _if_result_568; }); iteration = (iteration + 1); } if (pending) { - el_val_t safe_in = ({ el_val_t _if_result_522 = 0; if (str_eq(pend_tool_input, EL_STR(""))) { _if_result_522 = (EL_STR("{}")); } else { _if_result_522 = (pend_tool_input); } _if_result_522; }); - el_val_t tools_arr = ({ el_val_t _if_result_523 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_523 = (EL_STR("[]")); } else { _if_result_523 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_523; }); + el_val_t safe_in = ({ el_val_t _if_result_569 = 0; if (str_eq(pend_tool_input, EL_STR(""))) { _if_result_569 = (EL_STR("{}")); } else { _if_result_569 = (pend_tool_input); } _if_result_569; }); + el_val_t tools_arr = ({ el_val_t _if_result_570 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_570 = (EL_STR("[]")); } else { _if_result_570 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_570; }); 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_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(",\"sources\":\"")), json_safe(sources_all)), EL_STR("\"")), EL_STR(",\"tools_used\":")), tools_arr), EL_STR("}")); } final_text = receipt_strip(final_text); if (str_eq(final_text, EL_STR(""))) { el_val_t hit_cap = (iteration >= 12); - el_val_t err_msg = ({ el_val_t _if_result_524 = 0; if (hit_cap) { _if_result_524 = (EL_STR("agentic loop hit the 12-iteration cap without producing a final reply - task may be too complex or a tool call is looping")); } else { _if_result_524 = (EL_STR("no response")); } _if_result_524; }); + el_val_t err_msg = ({ el_val_t _if_result_571 = 0; if (hit_cap) { _if_result_571 = (EL_STR("agentic loop hit the 12-iteration cap without producing a final reply - task may be too complex or a tool call is looping")); } else { _if_result_571 = (EL_STR("no response")); } _if_result_571; }); 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_525 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_525 = (EL_STR("[]")); } else { _if_result_525 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_525; }); + el_val_t tools_arr = ({ el_val_t _if_result_572 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_572 = (EL_STR("[]")); } else { _if_result_572 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_572; }); 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_526 = 0; if (str_eq(done_prev, EL_STR(""))) { _if_result_526 = (EL_STR("{\"done\":true}")); } else { _if_result_526 = (el_str_concat(done_prev, EL_STR(",{\"done\":true}"))); } _if_result_526; }); + el_val_t done_next = ({ el_val_t _if_result_573 = 0; if (str_eq(done_prev, EL_STR(""))) { _if_result_573 = (EL_STR("{\"done\":true}")); } else { _if_result_573 = (el_str_concat(done_prev, EL_STR(",{\"done\":true}"))); } _if_result_573; }); 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_concat(el_str_concat(EL_STR("{\"reply\":\""), safe_text), EL_STR("\",\"model\":\"")), model), EL_STR("\",\"agentic\":true,\"tools_used\":")), tools_arr), EL_STR(",\"sources\":\"")), json_safe(sources_all)), EL_STR("\",\"iterations\":")), int_to_str(iteration)), EL_STR("}")); return 0; } -el_val_t bridge_save(el_val_t session_id, el_val_t model, el_val_t safe_sys, el_val_t tools_json, el_val_t messages, el_val_t tools_log, el_val_t tool_use_id) { +el_val_t bridge_save(el_val_t session_id, el_val_t model, el_val_t safe_sys, el_val_t tools_json, el_val_t messages, el_val_t tools_log, el_val_t tool_use_id, el_val_t wire) { if (str_eq(messages, EL_STR("")) || str_eq(tools_json, EL_STR(""))) { return 0; } - 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_concat(EL_STR("{\"model\":\""), json_safe(model)), EL_STR("\"")), EL_STR(",\"safe_sys\":\"")), json_safe(safe_sys)), EL_STR("\"")), EL_STR(",\"tools_log\":\"")), json_safe(tools_log)), EL_STR("\"")), EL_STR(",\"tool_use_id\":\"")), json_safe(tool_use_id)), EL_STR("\"")), EL_STR(",\"tools_raw\":")), tools_json), EL_STR(",\"messages_raw\":")), messages), EL_STR("}")); + 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_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_log\":\"")), json_safe(tools_log)), EL_STR("\"")), EL_STR(",\"tool_use_id\":\"")), json_safe(tool_use_id)), EL_STR("\"")), EL_STR(",\"wire\":\"")), json_safe(wire)), EL_STR("\"")), EL_STR(",\"tools_raw\":")), tools_json), EL_STR(",\"messages_raw\":")), messages), EL_STR("}")); state_set(el_str_concat(EL_STR("mcp_bridge:"), session_id), blob); return 1; return 0; @@ -28975,21 +29186,36 @@ el_val_t agentic_resume(el_val_t session_id, el_val_t tool_use_id, el_val_t cont 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_527 = 0; if (str_eq(messages, EL_STR(""))) { _if_result_527 = (json_get(blob, EL_STR("messages"))); } else { _if_result_527 = (messages); } _if_result_527; }); + messages = ({ el_val_t _if_result_574 = 0; if (str_eq(messages, EL_STR(""))) { _if_result_574 = (json_get(blob, EL_STR("messages"))); } else { _if_result_574 = (messages); } _if_result_574; }); el_val_t tools_json = json_get_raw(blob, EL_STR("tools_raw")); - tools_json = ({ el_val_t _if_result_528 = 0; if (str_eq(tools_json, EL_STR(""))) { _if_result_528 = (json_get(blob, EL_STR("tools_json"))); } else { _if_result_528 = (tools_json); } _if_result_528; }); + tools_json = ({ el_val_t _if_result_575 = 0; if (str_eq(tools_json, EL_STR(""))) { _if_result_575 = (json_get(blob, EL_STR("tools_json"))); } else { _if_result_575 = (tools_json); } _if_result_575; }); 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 eff_use_id = ({ el_val_t _if_result_529 = 0; if (str_eq(tool_use_id, EL_STR(""))) { _if_result_529 = (saved_use_id); } else { _if_result_529 = (tool_use_id); } _if_result_529; }); - el_val_t trimmed = ({ el_val_t _if_result_530 = 0; if ((str_len(content) > 6000)) { _if_result_530 = (el_str_concat(str_slice(content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_530 = (content); } _if_result_530; }); + el_val_t eff_use_id = ({ el_val_t _if_result_576 = 0; if (str_eq(tool_use_id, EL_STR(""))) { _if_result_576 = (saved_use_id); } else { _if_result_576 = (tool_use_id); } _if_result_576; }); + el_val_t trimmed = ({ el_val_t _if_result_577 = 0; if ((str_len(content) > 6000)) { _if_result_577 = (el_str_concat(str_slice(content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_577 = (content); } _if_result_577; }); 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)); - el_val_t resumed_messages = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",{\"role\":\"user\",\"content\":[")), tool_msg), EL_STR("]}]")); state_set(el_str_concat(EL_STR("mcp_bridge:"), session_id), EL_STR("")); + el_val_t i_traw = str_index_of(blob, EL_STR(",\"tools_raw\":")); + el_val_t i_tjson = str_index_of(blob, EL_STR(",\"tools_json\":")); + el_val_t i_mraw = str_index_of(blob, EL_STR(",\"messages_raw\":")); + el_val_t i_msgs = str_index_of(blob, EL_STR(",\"messages\":")); + el_val_t cut1 = ({ el_val_t _if_result_578 = 0; if ((i_traw > 0)) { _if_result_578 = (i_traw); } else { _if_result_578 = (str_len(blob)); } _if_result_578; }); + el_val_t cut2 = ({ el_val_t _if_result_579 = 0; if (((i_tjson > 0) && (i_tjson < cut1))) { _if_result_579 = (i_tjson); } else { _if_result_579 = (cut1); } _if_result_579; }); + el_val_t cut3 = ({ el_val_t _if_result_580 = 0; if (((i_mraw > 0) && (i_mraw < cut2))) { _if_result_580 = (i_mraw); } else { _if_result_580 = (cut2); } _if_result_580; }); + el_val_t cut = ({ el_val_t _if_result_581 = 0; if (((i_msgs > 0) && (i_msgs < cut3))) { _if_result_581 = (i_msgs); } else { _if_result_581 = (cut3); } _if_result_581; }); + el_val_t blob_head = str_slice(blob, 0, cut); + el_val_t wire = json_get(blob_head, EL_STR("wire")); + if (str_eq(wire, EL_STR("openai"))) { + el_val_t tool_msg_o = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"role\":\"tool\",\"tool_call_id\":\""), eff_use_id), EL_STR("\",\"content\":\"")), safe_result), EL_STR("\"}")); + el_val_t resumed_o = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",")), tool_msg_o), EL_STR("]")); + return openai_agentic_loop(session_id, model, safe_sys, tools_json, resumed_o, tools_log); + } + 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 resumed_messages = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",{\"role\":\"user\",\"content\":[")), tool_msg), EL_STR("]}]")); el_val_t api_key = agentic_api_key(); el_val_t h = el_map_new(0); map_set(h, EL_STR("x-api-key"), api_key); @@ -29020,12 +29246,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_531 = 0; if (str_eq(message, EL_STR(""))) { _if_result_531 = (transcript); } else { _if_result_531 = (message); } _if_result_531; }); + el_val_t eff_message = ({ el_val_t _if_result_582 = 0; if (str_eq(message, EL_STR(""))) { _if_result_582 = (transcript); } else { _if_result_582 = (message); } _if_result_582; }); 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_532 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_532 = (chat_default_model()); } else { _if_result_532 = (req_model); } _if_result_532; }); + el_val_t model = ({ el_val_t _if_result_583 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_583 = (chat_default_model()); } else { _if_result_583 = (req_model); } _if_result_583; }); system_prompt = safety_augment_system(system_prompt, eff_message); system_prompt = el_str_concat(system_prompt, bounded_persona_floor()); el_val_t raw_response = llm_call_system(model, system_prompt, eff_message); @@ -29049,7 +29275,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_533 = 0; if (str_eq(engram_ctx, EL_STR(""))) { _if_result_533 = (identity); } else { _if_result_533 = (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_533; }); + el_val_t system_prompt = ({ el_val_t _if_result_584 = 0; if (str_eq(engram_ctx, EL_STR(""))) { _if_result_584 = (identity); } else { _if_result_584 = (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_584; }); system_prompt = safety_augment_system(system_prompt, transcript); system_prompt = el_str_concat(system_prompt, bounded_persona_floor()); el_val_t raw_response = llm_call_system(model, system_prompt, transcript); @@ -29082,7 +29308,8 @@ el_val_t handle_dharma_room_turn_agentic(el_val_t body) { el_val_t system = el_str_concat(el_str_concat(el_str_concat(identity, bounded_persona_floor()), 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 and stay in character.\n\n")), ctx); el_val_t api_key = agentic_api_key(); system = safety_augment_system(system, transcript); - el_val_t tools_json = agentic_tools_all(); + el_val_t use_openai_d = (!str_eq(llm_base_url(), EL_STR("")) && str_eq(llm_wire_format(), EL_STR("openai"))); + el_val_t tools_json = ({ el_val_t _if_result_585 = 0; if (use_openai_d) { _if_result_585 = (agentic_tools_no_web()); } else { _if_result_585 = (agentic_tools_all()); } _if_result_585; }); el_val_t safe_transcript = json_safe(transcript); el_val_t safe_sys = json_safe(system); el_val_t messages = el_str_concat(el_str_concat(EL_STR("[{\"role\":\"user\",\"content\":\""), safe_transcript), EL_STR("\"}]")); @@ -29090,8 +29317,8 @@ 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_534 = 0; if (str_eq(room_id, EL_STR(""))) { _if_result_534 = (el_str_concat(EL_STR("dharma:"), next_bridge_id())); } else { _if_result_534 = (el_str_concat(EL_STR("dharma:"), room_id)); } _if_result_534; }); - el_val_t loop_result = agentic_loop(session_id, model, safe_sys, tools_json, messages, h, EL_STR("")); + el_val_t session_id = ({ el_val_t _if_result_586 = 0; if (str_eq(room_id, EL_STR(""))) { _if_result_586 = (el_str_concat(EL_STR("dharma:"), next_bridge_id())); } else { _if_result_586 = (el_str_concat(EL_STR("dharma:"), room_id)); } _if_result_586; }); + el_val_t loop_result = ({ el_val_t _if_result_587 = 0; if (use_openai_d) { _if_result_587 = (openai_agentic_loop(session_id, model, safe_sys, tools_json, messages, EL_STR(""))); } else { _if_result_587 = (agentic_loop(session_id, model, safe_sys, tools_json, messages, h, EL_STR(""))); } _if_result_587; }); el_val_t result_error = json_get(loop_result, EL_STR("error")); if (!str_eq(result_error, EL_STR(""))) { return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"error\":\""), result_error), EL_STR("\",\"response\":\"\",\"cgi_id\":\"")), cgi_id), EL_STR("\"}")); @@ -29105,7 +29332,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_535 = 0; if (str_eq(tools_arr, EL_STR(""))) { _if_result_535 = (EL_STR("[]")); } else { _if_result_535 = (tools_arr); } _if_result_535; }); + el_val_t eff_tools = ({ el_val_t _if_result_588 = 0; if (str_eq(tools_arr, EL_STR(""))) { _if_result_588 = (EL_STR("[]")); } else { _if_result_588 = (tools_arr); } _if_result_588; }); 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; @@ -29116,7 +29343,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_536 = 0; if ((str_len(safe_text) > 800)) { _if_result_536 = (str_slice(safe_text, 0, 800)); } else { _if_result_536 = (safe_text); } _if_result_536; }); + el_val_t trimmed = ({ el_val_t _if_result_589 = 0; if ((str_len(safe_text) > 800)) { _if_result_589 = (str_slice(safe_text, 0, 800)); } else { _if_result_589 = (safe_text); } _if_result_589; }); 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); @@ -29147,7 +29374,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_537 = 0; if ((str_len(safe_text) > 800)) { _if_result_537 = (str_slice(safe_text, 0, 800)); } else { _if_result_537 = (safe_text); } _if_result_537; }); + el_val_t trimmed = ({ el_val_t _if_result_590 = 0; if ((str_len(safe_text) > 800)) { _if_result_590 = (str_slice(safe_text, 0, 800)); } else { _if_result_590 = (safe_text); } _if_result_590; }); 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); @@ -29181,8 +29408,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_538 = 0; if ((str_len(msg) > 80)) { _if_result_538 = (str_slice(msg, 0, 80)); } else { _if_result_538 = (msg); } _if_result_538; }); - snippets = ({ el_val_t _if_result_539 = 0; if (str_eq(snippets, EL_STR(""))) { _if_result_539 = (snip); } else { _if_result_539 = (el_str_concat(el_str_concat(snippets, EL_STR("; ")), snip)); } _if_result_539; }); + el_val_t snip = ({ el_val_t _if_result_591 = 0; if ((str_len(msg) > 80)) { _if_result_591 = (str_slice(msg, 0, 80)); } else { _if_result_591 = (msg); } _if_result_591; }); + snippets = ({ el_val_t _if_result_592 = 0; if (str_eq(snippets, EL_STR(""))) { _if_result_592 = (snip); } else { _if_result_592 = (el_str_concat(el_str_concat(snippets, EL_STR("; ")), snip)); } _if_result_592; }); count = (count + 1); } i = (i + 1); @@ -29197,7 +29424,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_540 = 0; if (str_eq(reply, EL_STR(""))) { _if_result_540 = (json_get(resp, EL_STR("reply"))); } else { _if_result_540 = (reply); } _if_result_540; }); + el_val_t reply2 = ({ el_val_t _if_result_593 = 0; if (str_eq(reply, EL_STR(""))) { _if_result_593 = (json_get(resp, EL_STR("reply"))); } else { _if_result_593 = (reply); } _if_result_593; }); if (str_eq(message, EL_STR(""))) { return EL_STR(""); } @@ -29209,42 +29436,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_541 = 0; if (is_bell) { _if_result_541 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"bell:"), bell_level), EL_STR("\",\"affective\"]"))); } else { _if_result_541 = (({ el_val_t _if_result_542 = 0; if (is_positive) { _if_result_542 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"joy:"), positive_level), EL_STR("\",\"affective\"]"))); } else { _if_result_542 = (EL_STR("[\"Conversation\",\"chat\",\"timestamped\"]")); } _if_result_542; })); } _if_result_541; }); + el_val_t tags = ({ el_val_t _if_result_594 = 0; if (is_bell) { _if_result_594 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"bell:"), bell_level), EL_STR("\",\"affective\"]"))); } else { _if_result_594 = (({ el_val_t _if_result_595 = 0; if (is_positive) { _if_result_595 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"joy:"), positive_level), EL_STR("\",\"affective\"]"))); } else { _if_result_595 = (EL_STR("[\"Conversation\",\"chat\",\"timestamped\"]")); } _if_result_595; })); } _if_result_594; }); 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 = wt_node(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_543 = 0; if ((str_len(message) > 120)) { _if_result_543 = (str_slice(message, 0, 120)); } else { _if_result_543 = (message); } _if_result_543; }); + el_val_t summary = ({ el_val_t _if_result_596 = 0; if ((str_len(message) > 120)) { _if_result_596 = (str_slice(message, 0, 120)); } else { _if_result_596 = (message); } _if_result_596; }); 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_544 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_544 = (el_from_float(0.98)); } else { _if_result_544 = (el_from_float(0.88)); } _if_result_544; }); - el_val_t sal_b = ({ el_val_t _if_result_545 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_545 = (el_from_float(0.98)); } else { _if_result_545 = (el_from_float(0.88)); } _if_result_545; }); - el_val_t sal_c = ({ el_val_t _if_result_546 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_546 = (el_from_float(1.0)); } else { _if_result_546 = (el_from_float(0.95)); } _if_result_546; }); + el_val_t sal_a = ({ el_val_t _if_result_597 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_597 = (el_from_float(0.98)); } else { _if_result_597 = (el_from_float(0.88)); } _if_result_597; }); + el_val_t sal_b = ({ el_val_t _if_result_598 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_598 = (el_from_float(0.98)); } else { _if_result_598 = (el_from_float(0.88)); } _if_result_598; }); + el_val_t sal_c = ({ el_val_t _if_result_599 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_599 = (el_from_float(1.0)); } else { _if_result_599 = (el_from_float(0.95)); } _if_result_599; }); 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 = wt_node(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_547 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_547 = (EL_STR("session_bell_count")); } else { _if_result_547 = (el_str_concat(EL_STR("session_bell_count:"), sess_id)); } _if_result_547; }); + el_val_t bell_key = ({ el_val_t _if_result_600 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_600 = (EL_STR("session_bell_count")); } else { _if_result_600 = (el_str_concat(EL_STR("session_bell_count:"), sess_id)); } _if_result_600; }); el_val_t prior_count = state_get(bell_key); - el_val_t prior_n = ({ el_val_t _if_result_548 = 0; if (str_eq(prior_count, EL_STR(""))) { _if_result_548 = (0); } else { _if_result_548 = (str_to_int(prior_count)); } _if_result_548; }); + el_val_t prior_n = ({ el_val_t _if_result_601 = 0; if (str_eq(prior_count, EL_STR(""))) { _if_result_601 = (0); } else { _if_result_601 = (str_to_int(prior_count)); } _if_result_601; }); state_set(bell_key, int_to_str((prior_n + 1))); - el_val_t level_key = ({ el_val_t _if_result_549 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_549 = (EL_STR("session_bell_level")); } else { _if_result_549 = (el_str_concat(EL_STR("session_bell_level:"), sess_id)); } _if_result_549; }); + el_val_t level_key = ({ el_val_t _if_result_602 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_602 = (EL_STR("session_bell_level")); } else { _if_result_602 = (el_str_concat(EL_STR("session_bell_level:"), sess_id)); } _if_result_602; }); el_val_t prior_level = state_get(level_key); - el_val_t new_level = ({ el_val_t _if_result_550 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_550 = (EL_STR("hard")); } else { _if_result_550 = (({ el_val_t _if_result_551 = 0; if (str_eq(prior_level, EL_STR("hard"))) { _if_result_551 = (EL_STR("hard")); } else { _if_result_551 = (EL_STR("soft")); } _if_result_551; })); } _if_result_550; }); + el_val_t new_level = ({ el_val_t _if_result_603 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_603 = (EL_STR("hard")); } else { _if_result_603 = (({ el_val_t _if_result_604 = 0; if (str_eq(prior_level, EL_STR("hard"))) { _if_result_604 = (EL_STR("hard")); } else { _if_result_604 = (EL_STR("soft")); } _if_result_604; })); } _if_result_603; }); state_set(level_key, new_level); - el_val_t signal_key = ({ el_val_t _if_result_552 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_552 = (EL_STR("session_bell_signal")); } else { _if_result_552 = (el_str_concat(EL_STR("session_bell_signal:"), sess_id)); } _if_result_552; }); + el_val_t signal_key = ({ el_val_t _if_result_605 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_605 = (EL_STR("session_bell_signal")); } else { _if_result_605 = (el_str_concat(EL_STR("session_bell_signal:"), sess_id)); } _if_result_605; }); state_set(signal_key, safe_summary); } if (is_positive) { - el_val_t pos_summary = ({ el_val_t _if_result_553 = 0; if ((str_len(message) > 120)) { _if_result_553 = (str_slice(message, 0, 120)); } else { _if_result_553 = (message); } _if_result_553; }); + el_val_t pos_summary = ({ el_val_t _if_result_606 = 0; if ((str_len(message) > 120)) { _if_result_606 = (str_slice(message, 0, 120)); } else { _if_result_606 = (message); } _if_result_606; }); 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_554 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_554 = (el_from_float(0.88)); } else { _if_result_554 = (el_from_float(0.75)); } _if_result_554; }); - el_val_t pos_sal_b = ({ el_val_t _if_result_555 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_555 = (el_from_float(0.88)); } else { _if_result_555 = (el_from_float(0.75)); } _if_result_555; }); - el_val_t pos_sal_c = ({ el_val_t _if_result_556 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_556 = (el_from_float(0.95)); } else { _if_result_556 = (el_from_float(0.85)); } _if_result_556; }); + el_val_t pos_sal_a = ({ el_val_t _if_result_607 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_607 = (el_from_float(0.88)); } else { _if_result_607 = (el_from_float(0.75)); } _if_result_607; }); + el_val_t pos_sal_b = ({ el_val_t _if_result_608 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_608 = (el_from_float(0.88)); } else { _if_result_608 = (el_from_float(0.75)); } _if_result_608; }); + el_val_t pos_sal_c = ({ el_val_t _if_result_609 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_609 = (el_from_float(0.95)); } else { _if_result_609 = (el_from_float(0.85)); } _if_result_609; }); 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); @@ -29324,7 +29551,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_557 = 0; if (str_eq(current_model, EL_STR(""))) { _if_result_557 = (EL_STR("claude-opus-4-8")); } else { _if_result_557 = (current_model); } _if_result_557; }); + el_val_t display = ({ el_val_t _if_result_610 = 0; if (str_eq(current_model, EL_STR(""))) { _if_result_610 = (EL_STR("claude-opus-4-8")); } else { _if_result_610 = (current_model); } _if_result_610; }); return el_str_concat(el_str_concat(EL_STR("{\"model\":\""), display), EL_STR("\",\"ok\":true}")); return 0; } @@ -29427,7 +29654,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_558 = 0; if (str_eq(lang_req, EL_STR(""))) { _if_result_558 = (EL_STR("en")); } else { _if_result_558 = (lang_req); } _if_result_558; }); + el_val_t lang_code = ({ el_val_t _if_result_611 = 0; if (str_eq(lang_req, EL_STR(""))) { _if_result_611 = (EL_STR("en")); } else { _if_result_611 = (lang_req); } _if_result_611; }); 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}")); @@ -29450,17 +29677,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_559 = 0; if (str_starts_with(msg, EL_STR("What is "))) { _if_result_559 = (str_slice(msg, 8, str_len(msg))); } else { _if_result_559 = (msg); } _if_result_559; }); - el_val_t m2 = ({ el_val_t _if_result_560 = 0; if (str_starts_with(m1, EL_STR("What are "))) { _if_result_560 = (str_slice(m1, 9, str_len(m1))); } else { _if_result_560 = (m1); } _if_result_560; }); - el_val_t m3 = ({ el_val_t _if_result_561 = 0; if (str_starts_with(m2, EL_STR("Tell me about "))) { _if_result_561 = (str_slice(m2, 14, str_len(m2))); } else { _if_result_561 = (m2); } _if_result_561; }); - el_val_t m4 = ({ el_val_t _if_result_562 = 0; if (str_starts_with(m3, EL_STR("Who is "))) { _if_result_562 = (str_slice(m3, 7, str_len(m3))); } else { _if_result_562 = (m3); } _if_result_562; }); - el_val_t m5 = ({ el_val_t _if_result_563 = 0; if (str_starts_with(m4, EL_STR("Who are "))) { _if_result_563 = (str_slice(m4, 8, str_len(m4))); } else { _if_result_563 = (m4); } _if_result_563; }); - el_val_t m6 = ({ el_val_t _if_result_564 = 0; if (str_starts_with(m5, EL_STR("How do you "))) { _if_result_564 = (str_slice(m5, 11, str_len(m5))); } else { _if_result_564 = (m5); } _if_result_564; }); - el_val_t m7 = ({ el_val_t _if_result_565 = 0; if (str_starts_with(m6, EL_STR("Why "))) { _if_result_565 = (str_slice(m6, 4, str_len(m6))); } else { _if_result_565 = (m6); } _if_result_565; }); - el_val_t m8 = ({ el_val_t _if_result_566 = 0; if (str_starts_with(m7, EL_STR("Explain "))) { _if_result_566 = (str_slice(m7, 8, str_len(m7))); } else { _if_result_566 = (m7); } _if_result_566; }); + el_val_t m1 = ({ el_val_t _if_result_612 = 0; if (str_starts_with(msg, EL_STR("What is "))) { _if_result_612 = (str_slice(msg, 8, str_len(msg))); } else { _if_result_612 = (msg); } _if_result_612; }); + el_val_t m2 = ({ el_val_t _if_result_613 = 0; if (str_starts_with(m1, EL_STR("What are "))) { _if_result_613 = (str_slice(m1, 9, str_len(m1))); } else { _if_result_613 = (m1); } _if_result_613; }); + el_val_t m3 = ({ el_val_t _if_result_614 = 0; if (str_starts_with(m2, EL_STR("Tell me about "))) { _if_result_614 = (str_slice(m2, 14, str_len(m2))); } else { _if_result_614 = (m2); } _if_result_614; }); + el_val_t m4 = ({ el_val_t _if_result_615 = 0; if (str_starts_with(m3, EL_STR("Who is "))) { _if_result_615 = (str_slice(m3, 7, str_len(m3))); } else { _if_result_615 = (m3); } _if_result_615; }); + el_val_t m5 = ({ el_val_t _if_result_616 = 0; if (str_starts_with(m4, EL_STR("Who are "))) { _if_result_616 = (str_slice(m4, 8, str_len(m4))); } else { _if_result_616 = (m4); } _if_result_616; }); + el_val_t m6 = ({ el_val_t _if_result_617 = 0; if (str_starts_with(m5, EL_STR("How do you "))) { _if_result_617 = (str_slice(m5, 11, str_len(m5))); } else { _if_result_617 = (m5); } _if_result_617; }); + el_val_t m7 = ({ el_val_t _if_result_618 = 0; if (str_starts_with(m6, EL_STR("Why "))) { _if_result_618 = (str_slice(m6, 4, str_len(m6))); } else { _if_result_618 = (m6); } _if_result_618; }); + el_val_t m8 = ({ el_val_t _if_result_619 = 0; if (str_starts_with(m7, EL_STR("Explain "))) { _if_result_619 = (str_slice(m7, 8, str_len(m7))); } else { _if_result_619 = (m7); } _if_result_619; }); 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_567 = 0; if (((str_eq(trail, EL_STR("?")) || str_eq(trail, EL_STR("."))) || str_eq(trail, EL_STR("!")))) { _if_result_567 = (str_slice(m8, 0, last)); } else { _if_result_567 = (m8); } _if_result_567; }); + el_val_t clean = ({ el_val_t _if_result_620 = 0; if (((str_eq(trail, EL_STR("?")) || str_eq(trail, EL_STR("."))) || str_eq(trail, EL_STR("!")))) { _if_result_620 = (str_slice(m8, 0, last)); } else { _if_result_620 = (m8); } _if_result_620; }); return clean; return 0; } @@ -29503,7 +29730,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_568 = 0; if (topic_ok) { _if_result_568 = (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_568 = (({ el_val_t _if_result_569 = 0; if (msg_ok) { _if_result_569 = (from_msg); } else { _if_result_569 = (engram_scan_nodes_json(5, 0)); } _if_result_569; })); } _if_result_568; }); + el_val_t candidates = ({ el_val_t _if_result_621 = 0; if (topic_ok) { _if_result_621 = (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_621 = (({ el_val_t _if_result_622 = 0; if (msg_ok) { _if_result_622 = (from_msg); } else { _if_result_622 = (engram_scan_nodes_json(5, 0)); } _if_result_622; })); } _if_result_621; }); el_val_t total = json_array_len(candidates); el_val_t fi = 0; el_val_t kept_count = 0; @@ -29516,13 +29743,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_570 = 0; if (str_eq(kept_json, EL_STR(""))) { _if_result_570 = (EL_STR("")); } else { _if_result_570 = (EL_STR(",")); } _if_result_570; }); + el_val_t sep = ({ el_val_t _if_result_623 = 0; if (str_eq(kept_json, EL_STR(""))) { _if_result_623 = (EL_STR("")); } else { _if_result_623 = (EL_STR(",")); } _if_result_623; }); 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_571 = 0; if (str_eq(kept_json, EL_STR(""))) { _if_result_571 = (EL_STR("[]")); } else { _if_result_571 = (el_str_concat(el_str_concat(EL_STR("["), kept_json), EL_STR("]"))); } _if_result_571; }); + el_val_t frame_nodes = ({ el_val_t _if_result_624 = 0; if (str_eq(kept_json, EL_STR(""))) { _if_result_624 = (EL_STR("[]")); } else { _if_result_624 = (el_str_concat(el_str_concat(EL_STR("["), kept_json), EL_STR("]"))); } _if_result_624; }); 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); @@ -29537,14 +29764,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_572 = 0; if (str_eq(found_node, EL_STR(""))) { _if_result_572 = (json_array_get(frame_nodes, 0)); } else { _if_result_572 = (found_node); } _if_result_572; }); + el_val_t top_node = ({ el_val_t _if_result_625 = 0; if (str_eq(found_node, EL_STR(""))) { _if_result_625 = (json_array_get(frame_nodes, 0)); } else { _if_result_625 = (found_node); } _if_result_625; }); el_val_t top_raw = json_get(top_node, EL_STR("content")); - el_val_t patient_raw = ({ el_val_t _if_result_573 = 0; if (str_eq(top_raw, EL_STR(""))) { _if_result_573 = (topic); } else { _if_result_573 = (({ el_val_t _if_result_574 = 0; if ((str_len(top_raw) > 200)) { _if_result_574 = (str_slice(top_raw, 0, 200)); } else { _if_result_574 = (top_raw); } _if_result_574; })); } _if_result_573; }); + el_val_t patient_raw = ({ el_val_t _if_result_626 = 0; if (str_eq(top_raw, EL_STR(""))) { _if_result_626 = (topic); } else { _if_result_626 = (({ el_val_t _if_result_627 = 0; if ((str_len(top_raw) > 200)) { _if_result_627 = (str_slice(top_raw, 0, 200)); } else { _if_result_627 = (top_raw); } _if_result_627; })); } _if_result_626; }); 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_575 = 0; if (str_eq(predicate, EL_STR("store"))) { _if_result_575 = (EL_STR("command")); } else { _if_result_575 = (EL_STR("assert")); } _if_result_575; }); + el_val_t intent_val = ({ el_val_t _if_result_628 = 0; if (str_eq(predicate, EL_STR("store"))) { _if_result_628 = (EL_STR("command")); } else { _if_result_628 = (EL_STR("assert")); } _if_result_628; }); 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_576 = 0; if (str_eq(realized, EL_STR(""))) { _if_result_576 = (({ el_val_t _if_result_577 = 0; if (str_eq(patient_safe, EL_STR(""))) { _if_result_577 = (EL_STR("Nothing in the engram matched that query.")); } else { _if_result_577 = (patient_safe); } _if_result_577; })); } else { _if_result_576 = (realized); } _if_result_576; }); + el_val_t response = ({ el_val_t _if_result_629 = 0; if (str_eq(realized, EL_STR(""))) { _if_result_629 = (({ el_val_t _if_result_630 = 0; if (str_eq(patient_safe, EL_STR(""))) { _if_result_630 = (EL_STR("Nothing in the engram matched that query.")); } else { _if_result_630 = (patient_safe); } _if_result_630; })); } else { _if_result_629 = (realized); } _if_result_629; }); 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; @@ -29627,7 +29854,7 @@ el_val_t api_query_param(el_val_t path, el_val_t key) { } el_val_t after = str_slice(qs, (pos + str_len(needle)), str_len(qs)); el_val_t amp = str_index_of(after, EL_STR("&")); - el_val_t raw = ({ el_val_t _if_result_578 = 0; if ((amp < 0)) { _if_result_578 = (after); } else { _if_result_578 = (str_slice(after, 0, amp)); } _if_result_578; }); + el_val_t raw = ({ el_val_t _if_result_631 = 0; if ((amp < 0)) { _if_result_631 = (after); } else { _if_result_631 = (str_slice(after, 0, amp)); } _if_result_631; }); return url_decode(raw); return 0; } @@ -29685,7 +29912,7 @@ el_val_t api_utf8_trunc(el_val_t s, el_val_t n) { 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_579 = 0; if (is_cont) { _if_result_579 = ((cut - 1)); } else { _if_result_579 = (cut); } _if_result_579; }); + cut = ({ el_val_t _if_result_632 = 0; if (is_cont) { _if_result_632 = ((cut - 1)); } else { _if_result_632 = (cut); } _if_result_632; }); scanning = is_cont; } return str_slice(s, 0, cut); @@ -29699,7 +29926,7 @@ el_val_t api_compact_node(el_val_t node, el_val_t snip) { 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_580 = 0; if ((str_len(content) > snip)) { _if_result_580 = (EL_STR("true")); } else { _if_result_580 = (EL_STR("false")); } _if_result_580; }); + el_val_t trunc_str = ({ el_val_t _if_result_633 = 0; if ((str_len(content) > snip)) { _if_result_633 = (EL_STR("true")); } else { _if_result_633 = (EL_STR("false")); } _if_result_633; }); 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; } @@ -29709,12 +29936,12 @@ el_val_t api_compact_node_array(el_val_t raw, el_val_t max_items, el_val_t snip) return EL_STR("[]"); } el_val_t n = json_array_len(raw); - el_val_t cap = ({ el_val_t _if_result_581 = 0; if ((n < max_items)) { _if_result_581 = (n); } else { _if_result_581 = (max_items); } _if_result_581; }); + el_val_t cap = ({ el_val_t _if_result_634 = 0; if ((n < max_items)) { _if_result_634 = (n); } else { _if_result_634 = (max_items); } _if_result_634; }); 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_582 = 0; if ((i == 0)) { _if_result_582 = (EL_STR("")); } else { _if_result_582 = (EL_STR(",")); } _if_result_582; }); + el_val_t sep = ({ el_val_t _if_result_635 = 0; if ((i == 0)) { _if_result_635 = (EL_STR("")); } else { _if_result_635 = (EL_STR(",")); } _if_result_635; }); out = el_str_concat(el_str_concat(out, sep), api_compact_node(node, snip)); i = (i + 1); } @@ -29727,13 +29954,13 @@ el_val_t api_compact_activated(el_val_t raw, el_val_t max_items, el_val_t snip) return EL_STR("[]"); } el_val_t n = json_array_len(raw); - el_val_t cap = ({ el_val_t _if_result_583 = 0; if ((n < max_items)) { _if_result_583 = (n); } else { _if_result_583 = (max_items); } _if_result_583; }); + el_val_t cap = ({ el_val_t _if_result_636 = 0; if ((n < max_items)) { _if_result_636 = (n); } else { _if_result_636 = (max_items); } _if_result_636; }); 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_584 = 0; if ((i == 0)) { _if_result_584 = (EL_STR("")); } else { _if_result_584 = (EL_STR(",")); } _if_result_584; }); + el_val_t sep = ({ el_val_t _if_result_637 = 0; if ((i == 0)) { _if_result_637 = (EL_STR("")); } else { _if_result_637 = (EL_STR(",")); } _if_result_637; }); 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); } @@ -29770,7 +29997,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_585 = 0; if (str_eq(tid, EL_STR(""))) { _if_result_585 = (acc); } else { _if_result_585 = (el_str_concat(el_str_concat(acc, tid), EL_STR("|"))); } _if_result_585; }); + acc = ({ el_val_t _if_result_638 = 0; if (str_eq(tid, EL_STR(""))) { _if_result_638 = (acc); } else { _if_result_638 = (el_str_concat(el_str_concat(acc, tid), EL_STR("|"))); } _if_result_638; }); i = (i + 1); } return acc; @@ -29801,8 +30028,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_586 = 0; if (keep) { _if_result_586 = (({ el_val_t _if_result_587 = 0; if (first) { _if_result_587 = (el_str_concat(out, node)); } else { _if_result_587 = (el_str_concat(el_str_concat(out, EL_STR(",")), node)); } _if_result_587; })); } else { _if_result_586 = (out); } _if_result_586; }); - first = ({ el_val_t _if_result_588 = 0; if (keep) { _if_result_588 = (0); } else { _if_result_588 = (first); } _if_result_588; }); + out = ({ el_val_t _if_result_639 = 0; if (keep) { _if_result_639 = (({ el_val_t _if_result_640 = 0; if (first) { _if_result_640 = (el_str_concat(out, node)); } else { _if_result_640 = (el_str_concat(el_str_concat(out, EL_STR(",")), node)); } _if_result_640; })); } else { _if_result_639 = (out); } _if_result_639; }); + first = ({ el_val_t _if_result_641 = 0; if (keep) { _if_result_641 = (0); } else { _if_result_641 = (first); } _if_result_641; }); i = (i + 1); } return el_str_concat(out, EL_STR("]")); @@ -29841,10 +30068,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_589 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_589 = (EL_STR("0.95")); } else { _if_result_589 = (({ el_val_t _if_result_590 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_590 = (EL_STR("0.75")); } else { _if_result_590 = (({ el_val_t _if_result_591 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_591 = (EL_STR("0.25")); } else { _if_result_591 = (EL_STR("0.50")); } _if_result_591; })); } _if_result_590; })); } _if_result_589; }); - el_val_t sal = ({ el_val_t _if_result_592 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_592 = (el_from_float(0.95)); } else { _if_result_592 = (({ el_val_t _if_result_593 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_593 = (el_from_float(0.75)); } else { _if_result_593 = (({ el_val_t _if_result_594 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_594 = (el_from_float(0.25)); } else { _if_result_594 = (el_from_float(0.5)); } _if_result_594; })); } _if_result_593; })); } _if_result_592; }); - el_val_t base_tags = ({ el_val_t _if_result_595 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_595 = (EL_STR("[\"Memory\"]")); } else { _if_result_595 = (tags_raw); } _if_result_595; }); - el_val_t final_tags = ({ el_val_t _if_result_596 = 0; if (str_eq(project, EL_STR(""))) { _if_result_596 = (base_tags); } else { el_val_t inner = str_slice(base_tags, 1, (str_len(base_tags) - 1)); _if_result_596 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",\"project:")), project), EL_STR("\"]"))); } _if_result_596; }); + el_val_t sal_str = ({ el_val_t _if_result_642 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_642 = (EL_STR("0.95")); } else { _if_result_642 = (({ el_val_t _if_result_643 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_643 = (EL_STR("0.75")); } else { _if_result_643 = (({ el_val_t _if_result_644 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_644 = (EL_STR("0.25")); } else { _if_result_644 = (EL_STR("0.50")); } _if_result_644; })); } _if_result_643; })); } _if_result_642; }); + el_val_t sal = ({ el_val_t _if_result_645 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_645 = (el_from_float(0.95)); } else { _if_result_645 = (({ el_val_t _if_result_646 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_646 = (el_from_float(0.75)); } else { _if_result_646 = (({ el_val_t _if_result_647 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_647 = (el_from_float(0.25)); } else { _if_result_647 = (el_from_float(0.5)); } _if_result_647; })); } _if_result_646; })); } _if_result_645; }); + el_val_t base_tags = ({ el_val_t _if_result_648 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_648 = (EL_STR("[\"Memory\"]")); } else { _if_result_648 = (tags_raw); } _if_result_648; }); + el_val_t final_tags = ({ el_val_t _if_result_649 = 0; if (str_eq(project, EL_STR(""))) { _if_result_649 = (base_tags); } else { el_val_t inner = str_slice(base_tags, 1, (str_len(base_tags) - 1)); _if_result_649 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",\"project:")), project), EL_STR("\"]"))); } _if_result_649; }); el_val_t id = wt_node(content, EL_STR("Memory"), EL_STR("memory:remembered"), sal, sal, el_from_float(0.9), EL_STR("Episodic"), final_tags); if (!api_persisted(id)) { return api_not_persisted(id); @@ -29860,15 +30087,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_597 = 0; if (str_eq(nt_raw, EL_STR(""))) { _if_result_597 = (EL_STR("Memory")); } else { _if_result_597 = (nt_raw); } _if_result_597; }); + el_val_t node_type = ({ el_val_t _if_result_650 = 0; if (str_eq(nt_raw, EL_STR(""))) { _if_result_650 = (EL_STR("Memory")); } else { _if_result_650 = (nt_raw); } _if_result_650; }); el_val_t label_raw = json_get(body, EL_STR("label")); - el_val_t label = ({ el_val_t _if_result_598 = 0; if (str_eq(label_raw, EL_STR(""))) { _if_result_598 = (EL_STR("node:created")); } else { _if_result_598 = (label_raw); } _if_result_598; }); + el_val_t label = ({ el_val_t _if_result_651 = 0; if (str_eq(label_raw, EL_STR(""))) { _if_result_651 = (EL_STR("node:created")); } else { _if_result_651 = (label_raw); } _if_result_651; }); el_val_t tier_raw = json_get(body, EL_STR("tier")); - el_val_t tier = ({ el_val_t _if_result_599 = 0; if (str_eq(tier_raw, EL_STR(""))) { _if_result_599 = (EL_STR("Episodic")); } else { _if_result_599 = (tier_raw); } _if_result_599; }); + el_val_t tier = ({ el_val_t _if_result_652 = 0; if (str_eq(tier_raw, EL_STR(""))) { _if_result_652 = (EL_STR("Episodic")); } else { _if_result_652 = (tier_raw); } _if_result_652; }); el_val_t tags_raw = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_600 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_600 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_600 = (tags_raw); } _if_result_600; }); + el_val_t tags = ({ el_val_t _if_result_653 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_653 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_653 = (tags_raw); } _if_result_653; }); el_val_t importance = json_get(body, EL_STR("importance")); - el_val_t sal = ({ el_val_t _if_result_601 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_601 = (el_from_float(0.95)); } else { _if_result_601 = (({ el_val_t _if_result_602 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_602 = (el_from_float(0.75)); } else { _if_result_602 = (({ el_val_t _if_result_603 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_603 = (el_from_float(0.25)); } else { _if_result_603 = (el_from_float(0.5)); } _if_result_603; })); } _if_result_602; })); } _if_result_601; }); + el_val_t sal = ({ el_val_t _if_result_654 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_654 = (el_from_float(0.95)); } else { _if_result_654 = (({ el_val_t _if_result_655 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_655 = (el_from_float(0.75)); } else { _if_result_655 = (({ el_val_t _if_result_656 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_656 = (el_from_float(0.25)); } else { _if_result_656 = (el_from_float(0.5)); } _if_result_656; })); } _if_result_655; })); } _if_result_654; }); el_val_t id = wt_node(content, node_type, label, sal, sal, el_from_float(0.9), tier, tags); if (!api_persisted(id)) { return api_not_persisted(id); @@ -29907,18 +30134,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_604 = 0; if (str_eq(body_content, EL_STR(""))) { _if_result_604 = (json_get(old, EL_STR("content"))); } else { _if_result_604 = (body_content); } _if_result_604; }); + el_val_t content = ({ el_val_t _if_result_657 = 0; if (str_eq(body_content, EL_STR(""))) { _if_result_657 = (json_get(old, EL_STR("content"))); } else { _if_result_657 = (body_content); } _if_result_657; }); 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_605 = 0; if (!str_eq(body_nt, EL_STR(""))) { _if_result_605 = (body_nt); } else { _if_result_605 = (({ el_val_t _if_result_606 = 0; if (!str_eq(old_nt, EL_STR(""))) { _if_result_606 = (old_nt); } else { _if_result_606 = (EL_STR("Memory")); } _if_result_606; })); } _if_result_605; }); + el_val_t node_type = ({ el_val_t _if_result_658 = 0; if (!str_eq(body_nt, EL_STR(""))) { _if_result_658 = (body_nt); } else { _if_result_658 = (({ el_val_t _if_result_659 = 0; if (!str_eq(old_nt, EL_STR(""))) { _if_result_659 = (old_nt); } else { _if_result_659 = (EL_STR("Memory")); } _if_result_659; })); } _if_result_658; }); 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_607 = 0; if (!str_eq(body_label, EL_STR(""))) { _if_result_607 = (body_label); } else { _if_result_607 = (({ el_val_t _if_result_608 = 0; if (!str_eq(old_label, EL_STR(""))) { _if_result_608 = (old_label); } else { _if_result_608 = (EL_STR("node:updated")); } _if_result_608; })); } _if_result_607; }); + el_val_t label = ({ el_val_t _if_result_660 = 0; if (!str_eq(body_label, EL_STR(""))) { _if_result_660 = (body_label); } else { _if_result_660 = (({ el_val_t _if_result_661 = 0; if (!str_eq(old_label, EL_STR(""))) { _if_result_661 = (old_label); } else { _if_result_661 = (EL_STR("node:updated")); } _if_result_661; })); } _if_result_660; }); 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_609 = 0; if (!str_eq(body_tier, EL_STR(""))) { _if_result_609 = (body_tier); } else { _if_result_609 = (({ el_val_t _if_result_610 = 0; if (!str_eq(old_tier, EL_STR(""))) { _if_result_610 = (old_tier); } else { _if_result_610 = (EL_STR("Episodic")); } _if_result_610; })); } _if_result_609; }); + el_val_t tier = ({ el_val_t _if_result_662 = 0; if (!str_eq(body_tier, EL_STR(""))) { _if_result_662 = (body_tier); } else { _if_result_662 = (({ el_val_t _if_result_663 = 0; if (!str_eq(old_tier, EL_STR(""))) { _if_result_663 = (old_tier); } else { _if_result_663 = (EL_STR("Episodic")); } _if_result_663; })); } _if_result_662; }); el_val_t body_tags = json_get(body, EL_STR("tags")); - el_val_t tags = ({ el_val_t _if_result_611 = 0; if (str_eq(body_tags, EL_STR(""))) { _if_result_611 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_611 = (body_tags); } _if_result_611; }); + el_val_t tags = ({ el_val_t _if_result_664 = 0; if (str_eq(body_tags, EL_STR(""))) { _if_result_664 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_664 = (body_tags); } _if_result_664; }); el_val_t new_id = wt_node(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); @@ -29929,15 +30156,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_612 = 0; if (str_eq(api_query_param(path, EL_STR("query")), EL_STR(""))) { _if_result_612 = (api_query_param(path, EL_STR("q"))); } else { _if_result_612 = (api_query_param(path, EL_STR("query"))); } _if_result_612; }); + el_val_t url_q = ({ el_val_t _if_result_665 = 0; if (str_eq(api_query_param(path, EL_STR("query")), EL_STR(""))) { _if_result_665 = (api_query_param(path, EL_STR("q"))); } else { _if_result_665 = (api_query_param(path, EL_STR("query"))); } _if_result_665; }); 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_613 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_613 = (url_q); } else { _if_result_613 = (({ el_val_t _if_result_614 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_614 = (body_query); } else { _if_result_614 = (body_q); } _if_result_614; })); } _if_result_613; }); + el_val_t q = ({ el_val_t _if_result_666 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_666 = (url_q); } else { _if_result_666 = (({ el_val_t _if_result_667 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_667 = (body_query); } else { _if_result_667 = (body_q); } _if_result_667; })); } _if_result_666; }); 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_615 = 0; if ((limit == 0)) { _if_result_615 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_615 = (limit); } _if_result_615; }); - limit = ({ el_val_t _if_result_616 = 0; if ((limit == 0)) { _if_result_616 = (10); } else { _if_result_616 = (limit); } _if_result_616; }); - el_val_t eff_q = ({ el_val_t _if_result_617 = 0; if (str_eq(q, EL_STR(""))) { _if_result_617 = (chain); } else { _if_result_617 = (q); } _if_result_617; }); + limit = ({ el_val_t _if_result_668 = 0; if ((limit == 0)) { _if_result_668 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_668 = (limit); } _if_result_668; }); + limit = ({ el_val_t _if_result_669 = 0; if ((limit == 0)) { _if_result_669 = (10); } else { _if_result_669 = (limit); } _if_result_669; }); + el_val_t eff_q = ({ el_val_t _if_result_670 = 0; if (str_eq(q, EL_STR(""))) { _if_result_670 = (chain); } else { _if_result_670 = (q); } _if_result_670; }); if (str_eq(eff_q, EL_STR(""))) { return api_or_empty(engram_scan_nodes_json(limit, 0)); } @@ -29950,10 +30177,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_618 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_618 = (url_q); } else { _if_result_618 = (({ el_val_t _if_result_619 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_619 = (body_query); } else { _if_result_619 = (body_q); } _if_result_619; })); } _if_result_618; }); + el_val_t q = ({ el_val_t _if_result_671 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_671 = (url_q); } else { _if_result_671 = (({ el_val_t _if_result_672 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_672 = (body_query); } else { _if_result_672 = (body_q); } _if_result_672; })); } _if_result_671; }); el_val_t limit = api_query_int(path, EL_STR("limit"), 0); - limit = ({ el_val_t _if_result_620 = 0; if ((limit == 0)) { _if_result_620 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_620 = (limit); } _if_result_620; }); - limit = ({ el_val_t _if_result_621 = 0; if ((limit == 0)) { _if_result_621 = (10); } else { _if_result_621 = (limit); } _if_result_621; }); + limit = ({ el_val_t _if_result_673 = 0; if ((limit == 0)) { _if_result_673 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_673 = (limit); } _if_result_673; }); + limit = ({ el_val_t _if_result_674 = 0; if ((limit == 0)) { _if_result_674 = (10); } else { _if_result_674 = (limit); } _if_result_674; }); if (str_eq(q, EL_STR(""))) { return api_err(EL_STR("query is required")); } @@ -29981,7 +30208,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_622 = 0; if (str_eq(title, EL_STR(""))) { _if_result_622 = (content); } else { _if_result_622 = (el_str_concat(el_str_concat(title, EL_STR(": ")), content)); } _if_result_622; }); + el_val_t full = ({ el_val_t _if_result_675 = 0; if (str_eq(title, EL_STR(""))) { _if_result_675 = (content); } else { _if_result_675 = (el_str_concat(el_str_concat(title, EL_STR(": ")), content)); } _if_result_675; }); el_val_t lbl = str_slice(title, 0, 80); el_val_t tags = EL_STR("[\"Knowledge\",\"captured\"]"); el_val_t id = wt_node(full, EL_STR("Knowledge"), lbl, el_from_float(0.85), el_from_float(0.8), el_from_float(0.9), EL_STR("Episodic"), tags); @@ -30023,7 +30250,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_623 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_623 = (EL_STR("[\"Knowledge\",\"tier:canonical\",\"disposition:stable\"]")); } else { _if_result_623 = (tags_raw); } _if_result_623; }); + el_val_t tags = ({ el_val_t _if_result_676 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_676 = (EL_STR("[\"Knowledge\",\"tier:canonical\",\"disposition:stable\"]")); } else { _if_result_676 = (tags_raw); } _if_result_676; }); el_val_t new_id = wt_node(content, EL_STR("Knowledge"), EL_STR(""), 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); @@ -30034,7 +30261,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_624 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_624 = (api_query_param(path, EL_STR("name"))); } else { _if_result_624 = (json_get(body, EL_STR("name"))); } _if_result_624; }); + el_val_t name = ({ el_val_t _if_result_677 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_677 = (api_query_param(path, EL_STR("name"))); } else { _if_result_677 = (json_get(body, EL_STR("name"))); } _if_result_677; }); 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)); @@ -30049,7 +30276,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_625 = 0; if (str_eq(name, EL_STR(""))) { _if_result_625 = (EL_STR("process:unnamed")); } else { _if_result_625 = (el_str_concat(EL_STR("process:"), name)); } _if_result_625; }); + el_val_t label = ({ el_val_t _if_result_678 = 0; if (str_eq(name, EL_STR(""))) { _if_result_678 = (EL_STR("process:unnamed")); } else { _if_result_678 = (el_str_concat(EL_STR("process:"), name)); } _if_result_678; }); el_val_t tags = EL_STR("[\"Process\"]"); el_val_t id = wt_node(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)) { @@ -30067,12 +30294,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_626 = 0; if (!str_eq(trigger, EL_STR(""))) { _if_result_626 = (el_str_concat(el_str_concat(parts, EL_STR("\nTrigger: ")), trigger)); } else { _if_result_626 = (parts); } _if_result_626; }); - parts = ({ el_val_t _if_result_627 = 0; if (!str_eq(pre, EL_STR(""))) { _if_result_627 = (el_str_concat(el_str_concat(parts, EL_STR("\nPre-reasoning: ")), pre)); } else { _if_result_627 = (parts); } _if_result_627; }); - parts = ({ el_val_t _if_result_628 = 0; if (!str_eq(post, EL_STR(""))) { _if_result_628 = (el_str_concat(el_str_concat(parts, EL_STR("\nPost-reasoning: ")), post)); } else { _if_result_628 = (parts); } _if_result_628; }); - parts = ({ el_val_t _if_result_629 = 0; if (!str_eq(ratio, EL_STR(""))) { _if_result_629 = (el_str_concat(el_str_concat(parts, EL_STR("\nCompression-ratio: ")), ratio)); } else { _if_result_629 = (parts); } _if_result_629; }); - parts = ({ el_val_t _if_result_630 = 0; if (!str_eq(gap, EL_STR(""))) { _if_result_630 = (el_str_concat(el_str_concat(parts, EL_STR("\nGap-direction: ")), gap)); } else { _if_result_630 = (parts); } _if_result_630; }); - parts = ({ el_val_t _if_result_631 = 0; if (!str_eq(legacy, EL_STR(""))) { _if_result_631 = (el_str_concat(el_str_concat(parts, EL_STR("\n")), legacy)); } else { _if_result_631 = (parts); } _if_result_631; }); + parts = ({ el_val_t _if_result_679 = 0; if (!str_eq(trigger, EL_STR(""))) { _if_result_679 = (el_str_concat(el_str_concat(parts, EL_STR("\nTrigger: ")), trigger)); } else { _if_result_679 = (parts); } _if_result_679; }); + parts = ({ el_val_t _if_result_680 = 0; if (!str_eq(pre, EL_STR(""))) { _if_result_680 = (el_str_concat(el_str_concat(parts, EL_STR("\nPre-reasoning: ")), pre)); } else { _if_result_680 = (parts); } _if_result_680; }); + parts = ({ el_val_t _if_result_681 = 0; if (!str_eq(post, EL_STR(""))) { _if_result_681 = (el_str_concat(el_str_concat(parts, EL_STR("\nPost-reasoning: ")), post)); } else { _if_result_681 = (parts); } _if_result_681; }); + parts = ({ el_val_t _if_result_682 = 0; if (!str_eq(ratio, EL_STR(""))) { _if_result_682 = (el_str_concat(el_str_concat(parts, EL_STR("\nCompression-ratio: ")), ratio)); } else { _if_result_682 = (parts); } _if_result_682; }); + parts = ({ el_val_t _if_result_683 = 0; if (!str_eq(gap, EL_STR(""))) { _if_result_683 = (el_str_concat(el_str_concat(parts, EL_STR("\nGap-direction: ")), gap)); } else { _if_result_683 = (parts); } _if_result_683; }); + parts = ({ el_val_t _if_result_684 = 0; if (!str_eq(legacy, EL_STR(""))) { _if_result_684 = (el_str_concat(el_str_concat(parts, EL_STR("\n")), legacy)); } else { _if_result_684 = (parts); } _if_result_684; }); 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\"]"); @@ -30085,7 +30312,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_632 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_632 = (api_query_param(path, EL_STR("query"))); } else { _if_result_632 = (json_get(body, EL_STR("query"))); } _if_result_632; }); + el_val_t q = ({ el_val_t _if_result_685 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_685 = (api_query_param(path, EL_STR("query"))); } else { _if_result_685 = (json_get(body, EL_STR("query"))); } _if_result_685; }); 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)); @@ -30096,7 +30323,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_633 = 0; if (str_eq(key, EL_STR(""))) { _if_result_633 = (json_get(body, EL_STR("key"))); } else { _if_result_633 = (key); } _if_result_633; }); + key = ({ el_val_t _if_result_686 = 0; if (str_eq(key, EL_STR(""))) { _if_result_686 = (json_get(body, EL_STR("key"))); } else { _if_result_686 = (key); } _if_result_686; }); if (str_eq(key, EL_STR(""))) { return EL_STR("{\"hint\":\"pass ?key=\",\"known\":[\"neuron.self.traversal_root\",\"neuron.self.values_hub\"]}"); } @@ -30113,7 +30340,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_634 = 0; if (str_starts_with(content, prefix)) { _if_result_634 = (str_slice(content, str_len(prefix), str_len(content))); } else { _if_result_634 = (content); } _if_result_634; }); + el_val_t value = ({ el_val_t _if_result_687 = 0; if (str_starts_with(content, prefix)) { _if_result_687 = (str_slice(content, str_len(prefix), str_len(content))); } else { _if_result_687 = (content); } _if_result_687; }); 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; } @@ -30135,13 +30362,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_635 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_635 = (api_query_param(path, EL_STR("id"))); } else { _if_result_635 = (json_get(body, EL_STR("entity_id"))); } _if_result_635; }); - el_val_t name = ({ el_val_t _if_result_636 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_636 = (api_query_param(path, EL_STR("name"))); } else { _if_result_636 = (json_get(body, EL_STR("name"))); } _if_result_636; }); + el_val_t entity_id = ({ el_val_t _if_result_688 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_688 = (api_query_param(path, EL_STR("id"))); } else { _if_result_688 = (json_get(body, EL_STR("entity_id"))); } _if_result_688; }); + el_val_t name = ({ el_val_t _if_result_689 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_689 = (api_query_param(path, EL_STR("name"))); } else { _if_result_689 = (json_get(body, EL_STR("name"))); } _if_result_689; }); el_val_t depth = api_query_int(path, EL_STR("depth"), 0); - depth = ({ el_val_t _if_result_637 = 0; if ((depth == 0)) { _if_result_637 = (json_get_int(body, EL_STR("max_depth"))); } else { _if_result_637 = (depth); } _if_result_637; }); - depth = ({ el_val_t _if_result_638 = 0; if ((depth == 0)) { _if_result_638 = (1); } else { _if_result_638 = (depth); } _if_result_638; }); + depth = ({ el_val_t _if_result_690 = 0; if ((depth == 0)) { _if_result_690 = (json_get_int(body, EL_STR("max_depth"))); } else { _if_result_690 = (depth); } _if_result_690; }); + depth = ({ el_val_t _if_result_691 = 0; if ((depth == 0)) { _if_result_691 = (1); } else { _if_result_691 = (depth); } _if_result_691; }); el_val_t resolved = entity_id; - resolved = ({ el_val_t _if_result_639 = 0; if (str_eq(resolved, EL_STR(""))) { _if_result_639 = (({ el_val_t _if_result_640 = 0; if ((str_eq(name, EL_STR("self")) || str_eq(name, EL_STR("neuron")))) { _if_result_640 = (EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee")); } else { _if_result_640 = (({ el_val_t _if_result_641 = 0; if ((str_eq(name, EL_STR("values")) || str_eq(name, EL_STR("values_hub")))) { _if_result_641 = (EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440")); } else { _if_result_641 = (EL_STR("")); } _if_result_641; })); } _if_result_640; })); } else { _if_result_639 = (resolved); } _if_result_639; }); + resolved = ({ el_val_t _if_result_692 = 0; if (str_eq(resolved, EL_STR(""))) { _if_result_692 = (({ el_val_t _if_result_693 = 0; if ((str_eq(name, EL_STR("self")) || str_eq(name, EL_STR("neuron")))) { _if_result_693 = (EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee")); } else { _if_result_693 = (({ el_val_t _if_result_694 = 0; if ((str_eq(name, EL_STR("values")) || str_eq(name, EL_STR("values_hub")))) { _if_result_694 = (EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440")); } else { _if_result_694 = (EL_STR("")); } _if_result_694; })); } _if_result_693; })); } else { _if_result_692 = (resolved); } _if_result_692; }); if (str_eq(resolved, EL_STR(""))) { return api_err(EL_STR("entity_id or name required. Known names: self, neuron, values, values_hub")); } @@ -30163,7 +30390,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_642 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_642 = (EL_STR("associates")); } else { _if_result_642 = (relation); } _if_result_642; }); + el_val_t eff_relation = ({ el_val_t _if_result_695 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_695 = (EL_STR("associates")); } else { _if_result_695 = (relation); } _if_result_695; }); wt_edge(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; @@ -30192,8 +30419,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_643 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_643 = (EL_STR("0.95")); } else { _if_result_643 = (({ el_val_t _if_result_644 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_644 = (EL_STR("0.75")); } else { _if_result_644 = (({ el_val_t _if_result_645 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_645 = (EL_STR("0.25")); } else { _if_result_645 = (EL_STR("0.50")); } _if_result_645; })); } _if_result_644; })); } _if_result_643; }); - el_val_t sal = ({ el_val_t _if_result_646 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_646 = (el_from_float(0.95)); } else { _if_result_646 = (({ el_val_t _if_result_647 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_647 = (el_from_float(0.75)); } else { _if_result_647 = (({ el_val_t _if_result_648 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_648 = (el_from_float(0.25)); } else { _if_result_648 = (el_from_float(0.5)); } _if_result_648; })); } _if_result_647; })); } _if_result_646; }); + el_val_t sal_str = ({ el_val_t _if_result_696 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_696 = (EL_STR("0.95")); } else { _if_result_696 = (({ el_val_t _if_result_697 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_697 = (EL_STR("0.75")); } else { _if_result_697 = (({ el_val_t _if_result_698 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_698 = (EL_STR("0.25")); } else { _if_result_698 = (EL_STR("0.50")); } _if_result_698; })); } _if_result_697; })); } _if_result_696; }); + el_val_t sal = ({ el_val_t _if_result_699 = 0; if (str_eq(sal_str, EL_STR("0.95"))) { _if_result_699 = (el_from_float(0.95)); } else { _if_result_699 = (({ el_val_t _if_result_700 = 0; if (str_eq(sal_str, EL_STR("0.75"))) { _if_result_700 = (el_from_float(0.75)); } else { _if_result_700 = (({ el_val_t _if_result_701 = 0; if (str_eq(sal_str, EL_STR("0.25"))) { _if_result_701 = (el_from_float(0.25)); } else { _if_result_701 = (el_from_float(0.5)); } _if_result_701; })); } _if_result_700; })); } _if_result_699; }); el_val_t tags = EL_STR("[\"Memory\",\"evolved\"]"); el_val_t new_id = wt_node(content, EL_STR("Memory"), EL_STR("memory:evolved"), sal, sal, el_from_float(0.9), EL_STR("Episodic"), tags); if (!str_eq(prior_id, EL_STR("")) && !str_eq(new_id, EL_STR(""))) { @@ -30268,7 +30495,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_649 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_649 = (el_from_float(0.95)); } else { _if_result_649 = (({ el_val_t _if_result_650 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_650 = (el_from_float(0.75)); } else { _if_result_650 = (({ el_val_t _if_result_651 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_651 = (el_from_float(0.25)); } else { _if_result_651 = (el_from_float(0.5)); } _if_result_651; })); } _if_result_650; })); } _if_result_649; }); + el_val_t sal = ({ el_val_t _if_result_702 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_702 = (el_from_float(0.95)); } else { _if_result_702 = (({ el_val_t _if_result_703 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_703 = (el_from_float(0.75)); } else { _if_result_703 = (({ el_val_t _if_result_704 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_704 = (el_from_float(0.25)); } else { _if_result_704 = (el_from_float(0.5)); } _if_result_704; })); } _if_result_703; })); } _if_result_702; }); el_val_t tags = EL_STR("[\"Memory\",\"evolved\",\"cultivated\"]"); el_val_t new_id = wt_node(content, EL_STR("Memory"), EL_STR("memory:cultivated"), sal, sal, el_from_float(0.9), EL_STR("Episodic"), tags); if (!str_eq(prior_id, EL_STR("")) && !str_eq(new_id, EL_STR(""))) { @@ -30294,7 +30521,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_652 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_652 = (EL_STR("associates")); } else { _if_result_652 = (relation); } _if_result_652; }); + el_val_t eff_relation = ({ el_val_t _if_result_705 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_705 = (EL_STR("associates")); } else { _if_result_705 = (relation); } _if_result_705; }); wt_edge(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}")); } @@ -30335,11 +30562,11 @@ el_val_t audit_pct1(el_val_t num, el_val_t den) { return EL_STR("null"); } el_val_t neg = (num < 0); - el_val_t a = ({ el_val_t _if_result_653 = 0; if (neg) { _if_result_653 = ((0 - num)); } else { _if_result_653 = (num); } _if_result_653; }); + el_val_t a = ({ el_val_t _if_result_706 = 0; if (neg) { _if_result_706 = ((0 - num)); } else { _if_result_706 = (num); } _if_result_706; }); el_val_t tenths = ((a * 1000) / den); el_val_t whole = (tenths / 10); el_val_t frac = (tenths - (whole * 10)); - el_val_t sign = ({ el_val_t _if_result_654 = 0; if (neg) { _if_result_654 = (EL_STR("-")); } else { _if_result_654 = (EL_STR("")); } _if_result_654; }); + el_val_t sign = ({ el_val_t _if_result_707 = 0; if (neg) { _if_result_707 = (EL_STR("-")); } else { _if_result_707 = (EL_STR("")); } _if_result_707; }); return el_str_concat(el_str_concat(el_str_concat(sign, int_to_str(whole)), EL_STR(".")), int_to_str(frac)); return 0; } @@ -30355,7 +30582,7 @@ el_val_t audit_str_at(el_val_t s, el_val_t start, el_val_t maxlen) { return EL_STR(""); } el_val_t end_guess = (start + maxlen); - el_val_t stop = ({ el_val_t _if_result_655 = 0; if ((end_guess > n)) { _if_result_655 = (n); } else { _if_result_655 = (end_guess); } _if_result_655; }); + el_val_t stop = ({ el_val_t _if_result_708 = 0; if ((end_guess > n)) { _if_result_708 = (n); } else { _if_result_708 = (end_guess); } _if_result_708; }); el_val_t win = str_slice(s, start, stop); el_val_t q = str_index_of(win, EL_STR("\"")); if (q < 0) { @@ -30396,13 +30623,13 @@ el_val_t audit_divergence(void) { el_val_t d_edges = (rt_edges - ow_edges); el_val_t prev_raw = state_get(EL_STR("audit_prev_node_delta")); el_val_t prev = str_to_int(prev_raw); - el_val_t abs_now = ({ el_val_t _if_result_656 = 0; if ((d_nodes < 0)) { _if_result_656 = ((0 - d_nodes)); } else { _if_result_656 = (d_nodes); } _if_result_656; }); - el_val_t abs_prev = ({ el_val_t _if_result_657 = 0; if ((prev < 0)) { _if_result_657 = ((0 - prev)); } else { _if_result_657 = (prev); } _if_result_657; }); - el_val_t trend = ({ el_val_t _if_result_658 = 0; if (str_eq(prev_raw, EL_STR(""))) { _if_result_658 = (EL_STR("no_prior_audit")); } else { _if_result_658 = (({ el_val_t _if_result_659 = 0; if ((abs_now > abs_prev)) { _if_result_659 = (EL_STR("growing")); } else { _if_result_659 = (({ el_val_t _if_result_660 = 0; if ((abs_now < abs_prev)) { _if_result_660 = (EL_STR("shrinking")); } else { _if_result_660 = (EL_STR("flat")); } _if_result_660; })); } _if_result_659; })); } _if_result_658; }); + el_val_t abs_now = ({ el_val_t _if_result_709 = 0; if ((d_nodes < 0)) { _if_result_709 = ((0 - d_nodes)); } else { _if_result_709 = (d_nodes); } _if_result_709; }); + el_val_t abs_prev = ({ el_val_t _if_result_710 = 0; if ((prev < 0)) { _if_result_710 = ((0 - prev)); } else { _if_result_710 = (prev); } _if_result_710; }); + el_val_t trend = ({ el_val_t _if_result_711 = 0; if (str_eq(prev_raw, EL_STR(""))) { _if_result_711 = (EL_STR("no_prior_audit")); } else { _if_result_711 = (({ el_val_t _if_result_712 = 0; if ((abs_now > abs_prev)) { _if_result_712 = (EL_STR("growing")); } else { _if_result_712 = (({ el_val_t _if_result_713 = 0; if ((abs_now < abs_prev)) { _if_result_713 = (EL_STR("shrinking")); } else { _if_result_713 = (EL_STR("flat")); } _if_result_713; })); } _if_result_712; })); } _if_result_711; }); state_set(EL_STR("audit_prev_node_delta"), int_to_str(d_nodes)); state_set(EL_STR("audit_prev_ts"), int_to_str(time_now())); - el_val_t note_head = ({ el_val_t _if_result_661 = 0; if ((d_nodes == 0)) { _if_result_661 = (EL_STR("Runtime and owner agree on node count.")); } else { _if_result_661 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("Runtime holds "), int_to_str(d_nodes)), EL_STR(" nodes (")), audit_pct1(d_nodes, rt_nodes)), EL_STR("% of its own graph) that the persistence owner does not report. Nodes ")), EL_STR("that exist only in runtime memory do not survive a restart."))); } _if_result_661; }); - return audit_finding(EL_STR("owner_runtime_divergence"), 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("\"runtime_nodes\":"), int_to_str(rt_nodes)), EL_STR(",\"runtime_edges\":")), int_to_str(rt_edges)), EL_STR(",\"owner\":\"")), api_json_escape(url)), EL_STR("\",\"owner_reachable\":true")), EL_STR(",\"owner_nodes\":")), int_to_str(ow_nodes)), EL_STR(",\"owner_edges\":")), int_to_str(ow_edges)), EL_STR(",\"node_delta\":")), int_to_str(d_nodes)), EL_STR(",\"edge_delta\":")), int_to_str(d_edges)), EL_STR(",\"node_delta_pct_of_runtime\":")), audit_pct1(d_nodes, rt_nodes)), EL_STR(",\"trend_vs_previous_audit\":\"")), trend), EL_STR("\"")), EL_STR(",\"previous_node_delta\":")), ({ el_val_t _if_result_662 = 0; if (str_eq(prev_raw, EL_STR(""))) { _if_result_662 = (EL_STR("null")); } else { _if_result_662 = (int_to_str(prev)); } _if_result_662; })), el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(note_head, EL_STR(" Trend against the previous audit recorded in this soul's ")), EL_STR("state: ")), trend), EL_STR(". This is the comparison whose absence let a ")), EL_STR("~24,000-node loss run for weeks with every boot reporting green."))); + el_val_t note_head = ({ el_val_t _if_result_714 = 0; if ((d_nodes == 0)) { _if_result_714 = (EL_STR("Runtime and owner agree on node count.")); } else { _if_result_714 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("Runtime holds "), int_to_str(d_nodes)), EL_STR(" nodes (")), audit_pct1(d_nodes, rt_nodes)), EL_STR("% of its own graph) that the persistence owner does not report. Nodes ")), EL_STR("that exist only in runtime memory do not survive a restart."))); } _if_result_714; }); + return audit_finding(EL_STR("owner_runtime_divergence"), 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("\"runtime_nodes\":"), int_to_str(rt_nodes)), EL_STR(",\"runtime_edges\":")), int_to_str(rt_edges)), EL_STR(",\"owner\":\"")), api_json_escape(url)), EL_STR("\",\"owner_reachable\":true")), EL_STR(",\"owner_nodes\":")), int_to_str(ow_nodes)), EL_STR(",\"owner_edges\":")), int_to_str(ow_edges)), EL_STR(",\"node_delta\":")), int_to_str(d_nodes)), EL_STR(",\"edge_delta\":")), int_to_str(d_edges)), EL_STR(",\"node_delta_pct_of_runtime\":")), audit_pct1(d_nodes, rt_nodes)), EL_STR(",\"trend_vs_previous_audit\":\"")), trend), EL_STR("\"")), EL_STR(",\"previous_node_delta\":")), ({ el_val_t _if_result_715 = 0; if (str_eq(prev_raw, EL_STR(""))) { _if_result_715 = (EL_STR("null")); } else { _if_result_715 = (int_to_str(prev)); } _if_result_715; })), el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(note_head, EL_STR(" Trend against the previous audit recorded in this soul's ")), EL_STR("state: ")), trend), EL_STR(". This is the comparison whose absence let a ")), EL_STR("~24,000-node loss run for weeks with every boot reporting green."))); return 0; } @@ -30431,9 +30658,9 @@ el_val_t audit_edge_typing(el_val_t edges, el_val_t total_edges, el_val_t node_t } el_val_t audit_orphans_dangling(el_val_t edges, el_val_t total_edges, el_val_t node_total, el_val_t edge_cap, el_val_t node_cap) { - el_val_t n_take = ({ el_val_t _if_result_663 = 0; if ((node_total < node_cap)) { _if_result_663 = (node_total); } else { _if_result_663 = (node_cap); } _if_result_663; }); - el_val_t n_stride = ({ el_val_t _if_result_664 = 0; if ((n_take > 0)) { _if_result_664 = ((node_total / n_take)); } else { _if_result_664 = (1); } _if_result_664; }); - n_stride = ({ el_val_t _if_result_665 = 0; if ((n_stride < 1)) { _if_result_665 = (1); } else { _if_result_665 = (n_stride); } _if_result_665; }); + el_val_t n_take = ({ el_val_t _if_result_716 = 0; if ((node_total < node_cap)) { _if_result_716 = (node_total); } else { _if_result_716 = (node_cap); } _if_result_716; }); + el_val_t n_stride = ({ el_val_t _if_result_717 = 0; if ((n_take > 0)) { _if_result_717 = ((node_total / n_take)); } else { _if_result_717 = (1); } _if_result_717; }); + n_stride = ({ el_val_t _if_result_718 = 0; if ((n_stride < 1)) { _if_result_718 = (1); } else { _if_result_718 = (n_stride); } _if_result_718; }); el_val_t orphans = 0; el_val_t n_checked = 0; el_val_t j = 0; @@ -30443,7 +30670,7 @@ el_val_t audit_orphans_dangling(el_val_t edges, el_val_t total_edges, el_val_t n if (!str_eq(nid, EL_STR(""))) { el_val_t nbrs = engram_neighbors_json(nid, 1, EL_STR("both")); el_val_t deg = json_array_len(nbrs); - orphans = ({ el_val_t _if_result_666 = 0; if ((deg == 0)) { _if_result_666 = ((orphans + 1)); } else { _if_result_666 = (orphans); } _if_result_666; }); + orphans = ({ el_val_t _if_result_719 = 0; if ((deg == 0)) { _if_result_719 = ((orphans + 1)); } else { _if_result_719 = (orphans); } _if_result_719; }); n_checked = (n_checked + 1); } j = (j + 1); @@ -30452,10 +30679,10 @@ el_val_t audit_orphans_dangling(el_val_t edges, el_val_t total_edges, el_val_t n el_val_t to_pos = str_index_of_all(edges, EL_STR("\"to_id\":\"")); el_val_t nf = len(from_pos); el_val_t nt = len(to_pos); - el_val_t ne = ({ el_val_t _if_result_667 = 0; if ((nf < nt)) { _if_result_667 = (nf); } else { _if_result_667 = (nt); } _if_result_667; }); - el_val_t e_take = ({ el_val_t _if_result_668 = 0; if ((ne < edge_cap)) { _if_result_668 = (ne); } else { _if_result_668 = (edge_cap); } _if_result_668; }); - el_val_t e_stride = ({ el_val_t _if_result_669 = 0; if ((e_take > 0)) { _if_result_669 = ((ne / e_take)); } else { _if_result_669 = (1); } _if_result_669; }); - e_stride = ({ el_val_t _if_result_670 = 0; if ((e_stride < 1)) { _if_result_670 = (1); } else { _if_result_670 = (e_stride); } _if_result_670; }); + el_val_t ne = ({ el_val_t _if_result_720 = 0; if ((nf < nt)) { _if_result_720 = (nf); } else { _if_result_720 = (nt); } _if_result_720; }); + el_val_t e_take = ({ el_val_t _if_result_721 = 0; if ((ne < edge_cap)) { _if_result_721 = (ne); } else { _if_result_721 = (edge_cap); } _if_result_721; }); + el_val_t e_stride = ({ el_val_t _if_result_722 = 0; if ((e_take > 0)) { _if_result_722 = ((ne / e_take)); } else { _if_result_722 = (1); } _if_result_722; }); + e_stride = ({ el_val_t _if_result_723 = 0; if ((e_stride < 1)) { _if_result_723 = (1); } else { _if_result_723 = (e_stride); } _if_result_723; }); el_val_t dangling = 0; el_val_t e_checked = 0; el_val_t i = 0; @@ -30463,15 +30690,15 @@ el_val_t audit_orphans_dangling(el_val_t edges, el_val_t total_edges, el_val_t n el_val_t fid = audit_str_at(edges, (get(from_pos, i) + 11), 96); el_val_t tid = audit_str_at(edges, (get(to_pos, i) + 9), 96); el_val_t f_gone = str_eq(engram_get_node_json(fid), EL_STR("{}")); - el_val_t t_gone = ({ el_val_t _if_result_671 = 0; if (f_gone) { _if_result_671 = (1); } else { _if_result_671 = (str_eq(engram_get_node_json(tid), EL_STR("{}"))); } _if_result_671; }); - dangling = ({ el_val_t _if_result_672 = 0; if ((f_gone || t_gone)) { _if_result_672 = ((dangling + 1)); } else { _if_result_672 = (dangling); } _if_result_672; }); + el_val_t t_gone = ({ el_val_t _if_result_724 = 0; if (f_gone) { _if_result_724 = (1); } else { _if_result_724 = (str_eq(engram_get_node_json(tid), EL_STR("{}"))); } _if_result_724; }); + dangling = ({ el_val_t _if_result_725 = 0; if ((f_gone || t_gone)) { _if_result_725 = ((dangling + 1)); } else { _if_result_725 = (dangling); } _if_result_725; }); e_checked = (e_checked + 1); i = (i + e_stride); } - el_val_t orphan_est = ({ el_val_t _if_result_673 = 0; if ((n_checked > 0)) { _if_result_673 = (((orphans * node_total) / n_checked)); } else { _if_result_673 = (0); } _if_result_673; }); - el_val_t dangle_est = ({ el_val_t _if_result_674 = 0; if ((e_checked > 0)) { _if_result_674 = (((dangling * total_edges) / e_checked)); } else { _if_result_674 = (0); } _if_result_674; }); - el_val_t exhaustive_n = ({ el_val_t _if_result_675 = 0; if ((n_checked >= node_total)) { _if_result_675 = (EL_STR("true")); } else { _if_result_675 = (EL_STR("false")); } _if_result_675; }); - el_val_t exhaustive_e = ({ el_val_t _if_result_676 = 0; if ((e_checked >= ne)) { _if_result_676 = (EL_STR("true")); } else { _if_result_676 = (EL_STR("false")); } _if_result_676; }); + el_val_t orphan_est = ({ el_val_t _if_result_726 = 0; if ((n_checked > 0)) { _if_result_726 = (((orphans * node_total) / n_checked)); } else { _if_result_726 = (0); } _if_result_726; }); + el_val_t dangle_est = ({ el_val_t _if_result_727 = 0; if ((e_checked > 0)) { _if_result_727 = (((dangling * total_edges) / e_checked)); } else { _if_result_727 = (0); } _if_result_727; }); + el_val_t exhaustive_n = ({ el_val_t _if_result_728 = 0; if ((n_checked >= node_total)) { _if_result_728 = (EL_STR("true")); } else { _if_result_728 = (EL_STR("false")); } _if_result_728; }); + el_val_t exhaustive_e = ({ el_val_t _if_result_729 = 0; if ((e_checked >= ne)) { _if_result_729 = (EL_STR("true")); } else { _if_result_729 = (EL_STR("false")); } _if_result_729; }); return audit_finding(EL_STR("orphans_and_dangling_edges"), 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("\"nodes_population\":"), int_to_str(node_total)), EL_STR(",\"nodes_sampled\":")), int_to_str(n_checked)), EL_STR(",\"nodes_sample_exhaustive\":")), exhaustive_n), EL_STR(",\"orphans_in_sample\":")), int_to_str(orphans)), EL_STR(",\"orphan_rate_pct\":")), audit_pct1(orphans, n_checked)), EL_STR(",\"orphans_extrapolated\":")), int_to_str(orphan_est)), EL_STR(",\"edges_population\":")), int_to_str(total_edges)), EL_STR(",\"edges_sampled\":")), int_to_str(e_checked)), EL_STR(",\"edges_sample_exhaustive\":")), exhaustive_e), EL_STR(",\"dangling_in_sample\":")), int_to_str(dangling)), EL_STR(",\"dangling_rate_pct\":")), audit_pct1(dangling, e_checked)), EL_STR(",\"dangling_extrapolated\":")), int_to_str(dangle_est)), 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("Orphan = zero RESOLVABLE edges, so a node whose only edges dangle counts "), EL_STR("as an orphan; either way it is unreachable by traversal. Dangling = an ")), EL_STR("edge with an endpoint id that resolves to no node. Both are uniform ")), EL_STR("stride samples over the whole population, not the head of the list; ")), EL_STR("the extrapolations are estimates and are labelled as such. Pass ")), EL_STR("?node_sample= / ?edge_sample= at or above the population size to run ")), EL_STR("either check exhaustively. A high orphan rate is a characterization, ")), EL_STR("not a verdict: an accumulating store legitimately holds unlinked ")), EL_STR("material. It becomes a defect when the write paths were SUPPOSED to ")), EL_STR("link and did not."))); return 0; } @@ -30512,7 +30739,7 @@ el_val_t handle_api_structural_audit(el_val_t method, el_val_t path, el_val_t bo el_val_t node_cap = api_query_int(path, EL_STR("node_sample"), 300); el_val_t divergence = audit_divergence(); el_val_t self_model = audit_self_model(); - el_val_t edge_part = ({ el_val_t _if_result_677 = 0; if (want_edges) { el_val_t scratch_dir = env(EL_STR("TMPDIR")); el_val_t scratch_base = ({ el_val_t _if_result_678 = 0; if (str_eq(scratch_dir, EL_STR(""))) { _if_result_678 = (EL_STR("/tmp")); } else { _if_result_678 = (scratch_dir); } _if_result_678; }); el_val_t snap_path = el_str_concat(el_str_concat(el_str_concat(scratch_base, EL_STR("/soul-audit-export-")), state_get(EL_STR("soul_cgi_id"))), EL_STR(".json")); el_val_t saved = engram_save(snap_path); _if_result_677 = (({ el_val_t _if_result_679 = 0; if ((saved == 0)) { _if_result_679 = (el_str_concat(EL_STR(","), audit_finding(EL_STR("typed_edge_distribution"), EL_STR("\"available\":false"), el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("Could not export the graph to "), snap_path), EL_STR(" for edge analysis, ")), EL_STR("so edge typing and the dangling-edge sample were not run. ")), EL_STR("Reported as a gap, not as zero findings."))))); } else { el_val_t snap = wt_read(snap_path); el_val_t edges_raw = json_get_raw(snap, EL_STR("edges")); el_val_t edges = ({ el_val_t _if_result_680 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_680 = (EL_STR("[]")); } else { _if_result_680 = (edges_raw); } _if_result_680; }); _if_result_679 = (el_str_concat(el_str_concat(el_str_concat(EL_STR(","), audit_edge_typing(edges, edge_total, node_total)), EL_STR(",")), audit_orphans_dangling(edges, edge_total, node_total, edge_cap, node_cap))); } _if_result_679; })); } else { _if_result_677 = (EL_STR("")); } _if_result_677; }); + el_val_t edge_part = ({ el_val_t _if_result_730 = 0; if (want_edges) { el_val_t scratch_dir = env(EL_STR("TMPDIR")); el_val_t scratch_base = ({ el_val_t _if_result_731 = 0; if (str_eq(scratch_dir, EL_STR(""))) { _if_result_731 = (EL_STR("/tmp")); } else { _if_result_731 = (scratch_dir); } _if_result_731; }); el_val_t snap_path = el_str_concat(el_str_concat(el_str_concat(scratch_base, EL_STR("/soul-audit-export-")), state_get(EL_STR("soul_cgi_id"))), EL_STR(".json")); el_val_t saved = engram_save(snap_path); _if_result_730 = (({ el_val_t _if_result_732 = 0; if ((saved == 0)) { _if_result_732 = (el_str_concat(EL_STR(","), audit_finding(EL_STR("typed_edge_distribution"), EL_STR("\"available\":false"), el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("Could not export the graph to "), snap_path), EL_STR(" for edge analysis, ")), EL_STR("so edge typing and the dangling-edge sample were not run. ")), EL_STR("Reported as a gap, not as zero findings."))))); } else { el_val_t snap = wt_read(snap_path); el_val_t edges_raw = json_get_raw(snap, EL_STR("edges")); el_val_t edges = ({ el_val_t _if_result_733 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_733 = (EL_STR("[]")); } else { _if_result_733 = (edges_raw); } _if_result_733; }); _if_result_732 = (el_str_concat(el_str_concat(el_str_concat(EL_STR(","), audit_edge_typing(edges, edge_total, node_total)), EL_STR(",")), audit_orphans_dangling(edges, edge_total, node_total, edge_cap, node_cap))); } _if_result_732; })); } else { _if_result_730 = (EL_STR("")); } _if_result_730; }); 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("{\"audit\":\"structural\",\"stage\":1"), EL_STR(",\"spec\":\"CGI provisional 05-detailed-description.md, Stage 1: Structural audit 430\"")), EL_STR(",\"assessment\":\"coherence_assessment_432\"")), EL_STR(",\"assessment_kind\":\"annotated_characterization\"")), EL_STR(",\"score\":null")), EL_STR(",\"score_note\":\"By design. The specification calls for an annotated characterization of the graph's structural properties, not a binary score. Read the findings.\"")), EL_STR(",\"cgi_id\":\"")), api_json_escape(state_get(EL_STR("soul_cgi_id")))), EL_STR("\"")), EL_STR(",\"ts_ms\":")), int_to_str(time_now())), EL_STR(",\"findings\":[")), divergence), EL_STR(",")), self_model), edge_part), EL_STR("]")), EL_STR(",\"deferred\":")), audit_deferred()), EL_STR("}")); return 0; } @@ -30562,7 +30789,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_681 = 0; if (is_match) { _if_result_681 = (1); } else { _if_result_681 = (found); } _if_result_681; }); + found = ({ el_val_t _if_result_734 = 0; if (is_match) { _if_result_734 = (1); } else { _if_result_734 = (found); } _if_result_734; }); i = (i + 1); } return found; @@ -30573,7 +30800,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_682 = 0; if (str_eq(title_req, EL_STR(""))) { _if_result_682 = (EL_STR("New conversation")); } else { _if_result_682 = (title_req); } _if_result_682; }); + el_val_t title = ({ el_val_t _if_result_735 = 0; if (str_eq(title_req, EL_STR(""))) { _if_result_735 = (EL_STR("New conversation")); } else { _if_result_735 = (title_req); } _if_result_735; }); 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\"]"); @@ -30585,7 +30812,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_683 = 0; if (str_eq(existing_idx, EL_STR(""))) { _if_result_683 = (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_683 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), idx_entry), EL_STR(",")), inner), EL_STR("]"))); } _if_result_683; }); + el_val_t new_idx = ({ el_val_t _if_result_736 = 0; if (str_eq(existing_idx, EL_STR(""))) { _if_result_736 = (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_736 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), idx_entry), EL_STR(",")), inner), EL_STR("]"))); } _if_result_736; }); 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; @@ -30622,16 +30849,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_684 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_684 = (json_get(node, EL_STR("id"))); } else { _if_result_684 = (sess_id); } _if_result_684; }); + el_val_t eff_id = ({ el_val_t _if_result_737 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_737 = (json_get(node, EL_STR("id"))); } else { _if_result_737 = (sess_id); } _if_result_737; }); el_val_t title_inner = json_get(content, EL_STR("title")); - el_val_t eff_title = ({ el_val_t _if_result_685 = 0; if (str_eq(title_inner, EL_STR(""))) { _if_result_685 = (EL_STR("New conversation")); } else { _if_result_685 = (title_inner); } _if_result_685; }); + el_val_t eff_title = ({ el_val_t _if_result_738 = 0; if (str_eq(title_inner, EL_STR(""))) { _if_result_738 = (EL_STR("New conversation")); } else { _if_result_738 = (title_inner); } _if_result_738; }); 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_686 = 0; if (str_eq(created_inner, EL_STR(""))) { _if_result_686 = (EL_STR("0")); } else { _if_result_686 = (created_inner); } _if_result_686; }); - el_val_t eff_updated = ({ el_val_t _if_result_687 = 0; if (str_eq(updated_inner, EL_STR(""))) { _if_result_687 = (eff_created); } else { _if_result_687 = (updated_inner); } _if_result_687; }); - el_val_t entry = ({ el_val_t _if_result_688 = 0; if (is_session) { _if_result_688 = (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_688 = (EL_STR("")); } _if_result_688; }); - out = ({ el_val_t _if_result_689 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_689 = (({ el_val_t _if_result_690 = 0; if (str_eq(out, EL_STR(""))) { _if_result_690 = (entry); } else { _if_result_690 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_690; })); } else { _if_result_689 = (out); } _if_result_689; }); + el_val_t eff_created = ({ el_val_t _if_result_739 = 0; if (str_eq(created_inner, EL_STR(""))) { _if_result_739 = (EL_STR("0")); } else { _if_result_739 = (created_inner); } _if_result_739; }); + el_val_t eff_updated = ({ el_val_t _if_result_740 = 0; if (str_eq(updated_inner, EL_STR(""))) { _if_result_740 = (eff_created); } else { _if_result_740 = (updated_inner); } _if_result_740; }); + el_val_t entry = ({ el_val_t _if_result_741 = 0; if (is_session) { _if_result_741 = (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_741 = (EL_STR("")); } _if_result_741; }); + out = ({ el_val_t _if_result_742 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_742 = (({ el_val_t _if_result_743 = 0; if (str_eq(out, EL_STR(""))) { _if_result_743 = (entry); } else { _if_result_743 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_743; })); } else { _if_result_742 = (out); } _if_result_742; }); i = (i + 1); } return el_str_concat(el_str_concat(EL_STR("["), out), EL_STR("]")); @@ -30649,7 +30876,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_691 = 0; if (str_eq(results, EL_STR(""))) { _if_result_691 = (0); } else { _if_result_691 = (json_array_len(results)); } _if_result_691; }); + el_val_t total = ({ el_val_t _if_result_744 = 0; if (str_eq(results, EL_STR(""))) { _if_result_744 = (0); } else { _if_result_744 = (json_array_len(results)); } _if_result_744; }); el_val_t i = 0; while (i < total) { el_val_t node = json_array_get(results, i); @@ -30657,17 +30884,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_692 = 0; if (is_match) { _if_result_692 = (1); } else { _if_result_692 = (found); } _if_result_692; }); - meta_title = ({ el_val_t _if_result_693 = 0; if (is_match) { _if_result_693 = (json_get(content, EL_STR("title"))); } else { _if_result_693 = (meta_title); } _if_result_693; }); - meta_folder = ({ el_val_t _if_result_694 = 0; if (is_match) { _if_result_694 = (json_get(content, EL_STR("folder"))); } else { _if_result_694 = (meta_folder); } _if_result_694; }); + found = ({ el_val_t _if_result_745 = 0; if (is_match) { _if_result_745 = (1); } else { _if_result_745 = (found); } _if_result_745; }); + meta_title = ({ el_val_t _if_result_746 = 0; if (is_match) { _if_result_746 = (json_get(content, EL_STR("title"))); } else { _if_result_746 = (meta_title); } _if_result_746; }); + meta_folder = ({ el_val_t _if_result_747 = 0; if (is_match) { _if_result_747 = (json_get(content, EL_STR("folder"))); } else { _if_result_747 = (meta_folder); } _if_result_747; }); el_val_t meta_created_raw = json_get(content, EL_STR("created_at")); - meta_created = ({ el_val_t _if_result_695 = 0; if ((is_match && !str_eq(meta_created_raw, EL_STR("")))) { _if_result_695 = (meta_created_raw); } else { _if_result_695 = (meta_created); } _if_result_695; }); + meta_created = ({ el_val_t _if_result_748 = 0; if ((is_match && !str_eq(meta_created_raw, EL_STR("")))) { _if_result_748 = (meta_created_raw); } else { _if_result_748 = (meta_created); } _if_result_748; }); el_val_t meta_updated_raw = json_get(content, EL_STR("updated_at")); - meta_updated = ({ el_val_t _if_result_696 = 0; if ((is_match && !str_eq(meta_updated_raw, EL_STR("")))) { _if_result_696 = (meta_updated_raw); } else { _if_result_696 = (meta_updated); } _if_result_696; }); + meta_updated = ({ el_val_t _if_result_749 = 0; if ((is_match && !str_eq(meta_updated_raw, EL_STR("")))) { _if_result_749 = (meta_updated_raw); } else { _if_result_749 = (meta_updated); } _if_result_749; }); 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_697 = 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_697 = (({ el_val_t _if_result_698 = 0; if (str_eq(engram_hist, EL_STR(""))) { _if_result_698 = (EL_STR("[]")); } else { _if_result_698 = (({ el_val_t _if_result_699 = 0; if (str_eq(engram_hist, EL_STR("[]"))) { _if_result_699 = (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_699 = (({ el_val_t _if_result_700 = 0; if (str_starts_with(h_content, EL_STR("["))) { _if_result_700 = (h_content); } else { _if_result_700 = (EL_STR("[]")); } _if_result_700; })); } _if_result_699; })); } _if_result_698; })); } else { _if_result_697 = (state_hist); } _if_result_697; }); + el_val_t hist_raw = ({ el_val_t _if_result_750 = 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_750 = (({ el_val_t _if_result_751 = 0; if (str_eq(engram_hist, EL_STR(""))) { _if_result_751 = (EL_STR("[]")); } else { _if_result_751 = (({ el_val_t _if_result_752 = 0; if (str_eq(engram_hist, EL_STR("[]"))) { _if_result_752 = (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_752 = (({ el_val_t _if_result_753 = 0; if (str_starts_with(h_content, EL_STR("["))) { _if_result_753 = (h_content); } else { _if_result_753 = (EL_STR("[]")); } _if_result_753; })); } _if_result_752; })); } _if_result_751; })); } else { _if_result_750 = (state_hist); } _if_result_750; }); 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; @@ -30678,7 +30905,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_701 = 0; if (str_eq(results, EL_STR(""))) { _if_result_701 = (0); } else { _if_result_701 = (json_array_len(results)); } _if_result_701; }); + el_val_t total = ({ el_val_t _if_result_754 = 0; if (str_eq(results, EL_STR(""))) { _if_result_754 = (0); } else { _if_result_754 = (json_array_len(results)); } _if_result_754; }); el_val_t deleted_meta = 0; el_val_t i = 0; while (i < total) { @@ -30688,11 +30915,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_702 = 0; if ((is_match && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_702 = ((deleted_meta + 1)); } else { _if_result_702 = (deleted_meta); } _if_result_702; }); + deleted_meta = ({ el_val_t _if_result_755 = 0; if ((is_match && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_755 = ((deleted_meta + 1)); } else { _if_result_755 = (deleted_meta); } _if_result_755; }); 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_703 = 0; if (str_eq(msg_results, EL_STR(""))) { _if_result_703 = (0); } else { _if_result_703 = (json_array_len(msg_results)); } _if_result_703; }); + el_val_t m_total = ({ el_val_t _if_result_756 = 0; if (str_eq(msg_results, EL_STR(""))) { _if_result_756 = (0); } else { _if_result_756 = (json_array_len(msg_results)); } _if_result_756; }); el_val_t deleted_msgs = 0; el_val_t j = 0; while (j < m_total) { @@ -30700,7 +30927,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_704 = 0; if ((is_msgs && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_704 = ((deleted_msgs + 1)); } else { _if_result_704 = (deleted_msgs); } _if_result_704; }); + deleted_msgs = ({ el_val_t _if_result_757 = 0; if ((is_msgs && !str_eq(node_id, EL_STR("")))) { (void)(engram_forget(node_id)); _if_result_757 = ((deleted_msgs + 1)); } else { _if_result_757 = (deleted_msgs); } _if_result_757; }); j = (j + 1); } state_set(el_str_concat(EL_STR("session_hist_"), session_id), EL_STR("")); @@ -30723,7 +30950,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_705 = 0; if (str_eq(results, EL_STR(""))) { _if_result_705 = (0); } else { _if_result_705 = (json_array_len(results)); } _if_result_705; }); + el_val_t total = ({ el_val_t _if_result_758 = 0; if (str_eq(results, EL_STR(""))) { _if_result_758 = (0); } else { _if_result_758 = (json_array_len(results)); } _if_result_758; }); el_val_t found = 0; el_val_t old_title = EL_STR("New conversation"); el_val_t old_folder = EL_STR(""); @@ -30736,23 +30963,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_706 = 0; if (is_match) { _if_result_706 = (1); } else { _if_result_706 = (found); } _if_result_706; }); + found = ({ el_val_t _if_result_759 = 0; if (is_match) { _if_result_759 = (1); } else { _if_result_759 = (found); } _if_result_759; }); el_val_t title_raw = json_get(content, EL_STR("title")); - old_title = ({ el_val_t _if_result_707 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_707 = (title_raw); } else { _if_result_707 = (old_title); } _if_result_707; }); + old_title = ({ el_val_t _if_result_760 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_760 = (title_raw); } else { _if_result_760 = (old_title); } _if_result_760; }); el_val_t folder_raw = json_get(content, EL_STR("folder")); - old_folder = ({ el_val_t _if_result_708 = 0; if (is_match) { _if_result_708 = (folder_raw); } else { _if_result_708 = (old_folder); } _if_result_708; }); + old_folder = ({ el_val_t _if_result_761 = 0; if (is_match) { _if_result_761 = (folder_raw); } else { _if_result_761 = (old_folder); } _if_result_761; }); el_val_t created_raw = json_get(content, EL_STR("created_at")); - old_created = ({ el_val_t _if_result_709 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_709 = (created_raw); } else { _if_result_709 = (old_created); } _if_result_709; }); + old_created = ({ el_val_t _if_result_762 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_762 = (created_raw); } else { _if_result_762 = (old_created); } _if_result_762; }); el_val_t nid = json_get(node, EL_STR("id")); - old_node_id = ({ el_val_t _if_result_710 = 0; if (is_match) { _if_result_710 = (nid); } else { _if_result_710 = (old_node_id); } _if_result_710; }); + old_node_id = ({ el_val_t _if_result_763 = 0; if (is_match) { _if_result_763 = (nid); } else { _if_result_763 = (old_node_id); } _if_result_763; }); 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_711 = 0; if ((has_title && !str_eq(req_title, EL_STR("")))) { _if_result_711 = (req_title); } else { _if_result_711 = (old_title); } _if_result_711; }); - el_val_t eff_folder = ({ el_val_t _if_result_712 = 0; if (has_folder) { _if_result_712 = (json_get(body, EL_STR("folder"))); } else { _if_result_712 = (old_folder); } _if_result_712; }); + el_val_t eff_title = ({ el_val_t _if_result_764 = 0; if ((has_title && !str_eq(req_title, EL_STR("")))) { _if_result_764 = (req_title); } else { _if_result_764 = (old_title); } _if_result_764; }); + el_val_t eff_folder = ({ el_val_t _if_result_765 = 0; if (has_folder) { _if_result_765 = (json_get(body, EL_STR("folder"))); } else { _if_result_765 = (old_folder); } _if_result_765; }); if (!str_eq(old_node_id, EL_STR(""))) { engram_forget(old_node_id); } @@ -30780,8 +31007,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_713 = 0; if (str_eq(created_raw, EL_STR(""))) { _if_result_713 = (EL_STR("0")); } else { _if_result_713 = (created_raw); } _if_result_713; }); - el_val_t eff_updated = ({ el_val_t _if_result_714 = 0; if (str_eq(updated_raw, EL_STR(""))) { _if_result_714 = (eff_created); } else { _if_result_714 = (updated_raw); } _if_result_714; }); + el_val_t eff_created = ({ el_val_t _if_result_766 = 0; if (str_eq(created_raw, EL_STR(""))) { _if_result_766 = (EL_STR("0")); } else { _if_result_766 = (created_raw); } _if_result_766; }); + el_val_t eff_updated = ({ el_val_t _if_result_767 = 0; if (str_eq(updated_raw, EL_STR(""))) { _if_result_767 = (eff_created); } else { _if_result_767 = (updated_raw); } _if_result_767; }); 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("}")); @@ -30805,7 +31032,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_715 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_715 = (({ el_val_t _if_result_716 = 0; if (str_eq(out, EL_STR(""))) { _if_result_716 = (entry); } else { _if_result_716 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_716; })); } else { _if_result_715 = (out); } _if_result_715; }); + out = ({ el_val_t _if_result_768 = 0; if (!str_eq(entry, EL_STR(""))) { _if_result_768 = (({ el_val_t _if_result_769 = 0; if (str_eq(out, EL_STR(""))) { _if_result_769 = (entry); } else { _if_result_769 = (el_str_concat(el_str_concat(out, EL_STR(",")), entry)); } _if_result_769; })); } else { _if_result_768 = (out); } _if_result_768; }); i = (i + 1); } return el_str_concat(el_str_concat(EL_STR("["), out), EL_STR("]")); @@ -30841,7 +31068,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_717 = 0; if (str_eq(old_results, EL_STR(""))) { _if_result_717 = (0); } else { _if_result_717 = (json_array_len(old_results)); } _if_result_717; }); + el_val_t o_total = ({ el_val_t _if_result_770 = 0; if (str_eq(old_results, EL_STR(""))) { _if_result_770 = (0); } else { _if_result_770 = (json_array_len(old_results)); } _if_result_770; }); el_val_t oi = 0; while (oi < o_total) { el_val_t node = json_array_get(old_results, oi); @@ -30859,35 +31086,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_718 = 0; if (str_eq(bell_count_raw, EL_STR(""))) { _if_result_718 = (0); } else { _if_result_718 = (str_to_int(bell_count_raw)); } _if_result_718; }); + el_val_t bell_count = ({ el_val_t _if_result_771 = 0; if (str_eq(bell_count_raw, EL_STR(""))) { _if_result_771 = (0); } else { _if_result_771 = (str_to_int(bell_count_raw)); } _if_result_771; }); 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_719 = 0; if (str_eq(dominant_level, EL_STR(""))) { _if_result_719 = (EL_STR("soft")); } else { _if_result_719 = (dominant_level); } _if_result_719; }); - el_val_t eff_signal = ({ el_val_t _if_result_720 = 0; if (str_eq(last_signal, EL_STR(""))) { _if_result_720 = (EL_STR("(no signal captured)")); } else { _if_result_720 = (last_signal); } _if_result_720; }); + el_val_t eff_level = ({ el_val_t _if_result_772 = 0; if (str_eq(dominant_level, EL_STR(""))) { _if_result_772 = (EL_STR("soft")); } else { _if_result_772 = (dominant_level); } _if_result_772; }); + el_val_t eff_signal = ({ el_val_t _if_result_773 = 0; if (str_eq(last_signal, EL_STR(""))) { _if_result_773 = (EL_STR("(no signal captured)")); } else { _if_result_773 = (last_signal); } _if_result_773; }); 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_721 = 0; if (str_eq(eff_level, EL_STR("hard"))) { _if_result_721 = (el_from_float(0.95)); } else { _if_result_721 = (el_from_float(0.85)); } _if_result_721; }); + el_val_t summary_sal = ({ el_val_t _if_result_774 = 0; if (str_eq(eff_level, EL_STR("hard"))) { _if_result_774 = (el_from_float(0.95)); } else { _if_result_774 = (el_from_float(0.85)); } _if_result_774; }); el_val_t sum_discard = wt_node(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_722 = 0; if (str_eq(hist, EL_STR(""))) { _if_result_722 = (0); } else { _if_result_722 = (json_array_len(hist)); } _if_result_722; }); + el_val_t hist_arr_len = ({ el_val_t _if_result_775 = 0; if (str_eq(hist, EL_STR(""))) { _if_result_775 = (0); } else { _if_result_775 = (json_array_len(hist)); } _if_result_775; }); 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_723 = 0; if ((str_len(last_content) > 200)) { _if_result_723 = (str_slice(last_content, 0, 200)); } else { _if_result_723 = (last_content); } _if_result_723; }); + el_val_t topic_snip = ({ el_val_t _if_result_776 = 0; if ((str_len(last_content) > 200)) { _if_result_776 = (str_slice(last_content, 0, 200)); } else { _if_result_776 = (last_content); } _if_result_776; }); 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_724 = 0; if (str_eq(old_topic, EL_STR(""))) { _if_result_724 = (0); } else { _if_result_724 = (json_array_len(old_topic)); } _if_result_724; }); + el_val_t ot_len = ({ el_val_t _if_result_777 = 0; if (str_eq(old_topic, EL_STR(""))) { _if_result_777 = (0); } else { _if_result_777 = (json_array_len(old_topic)); } _if_result_777; }); el_val_t oti = 0; while (oti < ot_len) { el_val_t ot_node = json_array_get(old_topic, oti); @@ -30904,7 +31131,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_725 = 0; if (str_eq(results, EL_STR(""))) { _if_result_725 = (0); } else { _if_result_725 = (json_array_len(results)); } _if_result_725; }); + el_val_t total = ({ el_val_t _if_result_778 = 0; if (str_eq(results, EL_STR(""))) { _if_result_778 = (0); } else { _if_result_778 = (json_array_len(results)); } _if_result_778; }); el_val_t found = 0; el_val_t old_title = EL_STR("New conversation"); el_val_t old_folder = EL_STR(""); @@ -30917,15 +31144,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_726 = 0; if (is_match) { _if_result_726 = (1); } else { _if_result_726 = (found); } _if_result_726; }); + found = ({ el_val_t _if_result_779 = 0; if (is_match) { _if_result_779 = (1); } else { _if_result_779 = (found); } _if_result_779; }); el_val_t title_raw = json_get(content, EL_STR("title")); - old_title = ({ el_val_t _if_result_727 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_727 = (title_raw); } else { _if_result_727 = (old_title); } _if_result_727; }); + old_title = ({ el_val_t _if_result_780 = 0; if ((is_match && !str_eq(title_raw, EL_STR("")))) { _if_result_780 = (title_raw); } else { _if_result_780 = (old_title); } _if_result_780; }); el_val_t folder_raw = json_get(content, EL_STR("folder")); - old_folder = ({ el_val_t _if_result_728 = 0; if (is_match) { _if_result_728 = (folder_raw); } else { _if_result_728 = (old_folder); } _if_result_728; }); + old_folder = ({ el_val_t _if_result_781 = 0; if (is_match) { _if_result_781 = (folder_raw); } else { _if_result_781 = (old_folder); } _if_result_781; }); el_val_t created_raw = json_get(content, EL_STR("created_at")); - old_created = ({ el_val_t _if_result_729 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_729 = (created_raw); } else { _if_result_729 = (old_created); } _if_result_729; }); + old_created = ({ el_val_t _if_result_782 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_782 = (created_raw); } else { _if_result_782 = (old_created); } _if_result_782; }); el_val_t nid = json_get(node, EL_STR("id")); - old_node_id = ({ el_val_t _if_result_730 = 0; if (is_match) { _if_result_730 = (nid); } else { _if_result_730 = (old_node_id); } _if_result_730; }); + old_node_id = ({ el_val_t _if_result_783 = 0; if (is_match) { _if_result_783 = (nid); } else { _if_result_783 = (old_node_id); } _if_result_783; }); i = (i + 1); } if (!found) { @@ -30945,7 +31172,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_731 = 0; if (str_eq(results, EL_STR(""))) { _if_result_731 = (0); } else { _if_result_731 = (json_array_len(results)); } _if_result_731; }); + el_val_t total = ({ el_val_t _if_result_784 = 0; if (str_eq(results, EL_STR(""))) { _if_result_784 = (0); } else { _if_result_784 = (json_array_len(results)); } _if_result_784; }); el_val_t found = 0; el_val_t cur_title = EL_STR(""); el_val_t old_folder = EL_STR(""); @@ -30958,15 +31185,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_732 = 0; if (is_match) { _if_result_732 = (1); } else { _if_result_732 = (found); } _if_result_732; }); + found = ({ el_val_t _if_result_785 = 0; if (is_match) { _if_result_785 = (1); } else { _if_result_785 = (found); } _if_result_785; }); el_val_t title_raw = json_get(content, EL_STR("title")); - cur_title = ({ el_val_t _if_result_733 = 0; if (is_match) { _if_result_733 = (title_raw); } else { _if_result_733 = (cur_title); } _if_result_733; }); + cur_title = ({ el_val_t _if_result_786 = 0; if (is_match) { _if_result_786 = (title_raw); } else { _if_result_786 = (cur_title); } _if_result_786; }); el_val_t folder_raw = json_get(content, EL_STR("folder")); - old_folder = ({ el_val_t _if_result_734 = 0; if (is_match) { _if_result_734 = (folder_raw); } else { _if_result_734 = (old_folder); } _if_result_734; }); + old_folder = ({ el_val_t _if_result_787 = 0; if (is_match) { _if_result_787 = (folder_raw); } else { _if_result_787 = (old_folder); } _if_result_787; }); el_val_t created_raw = json_get(content, EL_STR("created_at")); - old_created = ({ el_val_t _if_result_735 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_735 = (created_raw); } else { _if_result_735 = (old_created); } _if_result_735; }); + old_created = ({ el_val_t _if_result_788 = 0; if ((is_match && !str_eq(created_raw, EL_STR("")))) { _if_result_788 = (created_raw); } else { _if_result_788 = (old_created); } _if_result_788; }); el_val_t nid = json_get(node, EL_STR("id")); - old_node_id = ({ el_val_t _if_result_736 = 0; if (is_match) { _if_result_736 = (nid); } else { _if_result_736 = (old_node_id); } _if_result_736; }); + old_node_id = ({ el_val_t _if_result_789 = 0; if (is_match) { _if_result_789 = (nid); } else { _if_result_789 = (old_node_id); } _if_result_789; }); i = (i + 1); } if (!found) { @@ -31000,13 +31227,13 @@ 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_737 = 0; if (str_eq(action, EL_STR("always"))) { _if_result_737 = (EL_STR("allow")); } else { _if_result_737 = (action); } _if_result_737; }); + el_val_t eff_action = ({ el_val_t _if_result_790 = 0; if (str_eq(action, EL_STR("always"))) { _if_result_790 = (EL_STR("allow")); } else { _if_result_790 = (action); } _if_result_790; }); 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_738 = 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_739 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_739 = (approve_tool_name); } else { _if_result_739 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), approve_tool_name)); } _if_result_739; }); (void)(state_set(always_key, new_always)); _if_result_738 = (1); } else { _if_result_738 = (0); } _if_result_738; }); + el_val_t discard_always = ({ el_val_t _if_result_791 = 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_792 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_792 = (approve_tool_name); } else { _if_result_792 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), approve_tool_name)); } _if_result_792; }); (void)(state_set(always_key, new_always)); _if_result_791 = (1); } else { _if_result_791 = (0); } _if_result_791; }); 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\"}"); } @@ -31014,8 +31241,8 @@ el_val_t handle_session_approve(el_val_t session_id, el_val_t body) { 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_740 = 0; if (str_eq(raw_input, EL_STR(""))) { _if_result_740 = (EL_STR("{}")); } else { _if_result_740 = (raw_input); } _if_result_740; }); - el_val_t content = ({ el_val_t _if_result_741 = 0; if (str_eq(eff_action, EL_STR("allow"))) { _if_result_741 = (({ el_val_t _if_result_742 = 0; if (use_client_content) { el_val_t trimmed = ({ el_val_t _if_result_743 = 0; if ((str_len(client_content) > 6000)) { _if_result_743 = (el_str_concat(str_slice(client_content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_743 = (client_content); } _if_result_743; }); _if_result_742 = (trimmed); } else { _if_result_742 = (({ el_val_t _if_result_744 = 0; if (use_dispatch) { el_val_t raw = dispatch_tool(approve_tool_name, eff_input); _if_result_744 = (({ el_val_t _if_result_745 = 0; if ((str_len(raw) > 6000)) { _if_result_745 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_745 = (raw); } _if_result_745; })); } else { _if_result_744 = (el_str_concat(el_str_concat(EL_STR("{\"error\":\"client content required for non-builtin tool: "), approve_tool_name), EL_STR("\"}"))); } _if_result_744; })); } _if_result_742; })); } else { _if_result_741 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_741; }); + el_val_t eff_input = ({ el_val_t _if_result_793 = 0; if (str_eq(raw_input, EL_STR(""))) { _if_result_793 = (EL_STR("{}")); } else { _if_result_793 = (raw_input); } _if_result_793; }); + el_val_t content = ({ el_val_t _if_result_794 = 0; if (str_eq(eff_action, EL_STR("allow"))) { _if_result_794 = (({ el_val_t _if_result_795 = 0; if (use_client_content) { el_val_t trimmed = ({ el_val_t _if_result_796 = 0; if ((str_len(client_content) > 6000)) { _if_result_796 = (el_str_concat(str_slice(client_content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_796 = (client_content); } _if_result_796; }); _if_result_795 = (trimmed); } else { _if_result_795 = (({ el_val_t _if_result_797 = 0; if (use_dispatch) { el_val_t raw = dispatch_tool(approve_tool_name, eff_input); _if_result_797 = (({ el_val_t _if_result_798 = 0; if ((str_len(raw) > 6000)) { _if_result_798 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_798 = (raw); } _if_result_798; })); } else { _if_result_797 = (el_str_concat(el_str_concat(EL_STR("{\"error\":\"client content required for non-builtin tool: "), approve_tool_name), EL_STR("\"}"))); } _if_result_797; })); } _if_result_795; })); } else { _if_result_794 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_794; }); return agentic_resume(session_id, call_id, content); } el_val_t pending_raw = state_get(el_str_concat(EL_STR("pending_tool_"), session_id)); @@ -31032,12 +31259,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_746 = 0; if (str_eq(action, EL_STR("always"))) { el_val_t new_always = ({ el_val_t _if_result_747 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_747 = (tool_name); } else { _if_result_747 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), tool_name)); } _if_result_747; }); (void)(state_set(always_key, new_always)); _if_result_746 = (1); } else { _if_result_746 = (0); } _if_result_746; }); + el_val_t discard_always2 = ({ el_val_t _if_result_799 = 0; if (str_eq(action, EL_STR("always"))) { el_val_t new_always = ({ el_val_t _if_result_800 = 0; if (str_eq(always_list, EL_STR(""))) { _if_result_800 = (tool_name); } else { _if_result_800 = (el_str_concat(el_str_concat(always_list, EL_STR(",")), tool_name)); } _if_result_800; }); (void)(state_set(always_key, new_always)); _if_result_799 = (1); } else { _if_result_799 = (0); } _if_result_799; }); state_set(el_str_concat(EL_STR("pending_tool_"), session_id), EL_STR("")); - el_val_t tool_result = ({ el_val_t _if_result_748 = 0; if (str_eq(eff_action, EL_STR("allow"))) { el_val_t raw = dispatch_tool(tool_name, tool_input); _if_result_748 = (({ el_val_t _if_result_749 = 0; if ((str_len(raw) > 6000)) { _if_result_749 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_749 = (raw); } _if_result_749; })); } else { _if_result_748 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_748; }); + el_val_t tool_result = ({ el_val_t _if_result_801 = 0; if (str_eq(eff_action, EL_STR("allow"))) { el_val_t raw = dispatch_tool(tool_name, tool_input); _if_result_801 = (({ el_val_t _if_result_802 = 0; if ((str_len(raw) > 6000)) { _if_result_802 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_802 = (raw); } _if_result_802; })); } else { _if_result_801 = (EL_STR("{\"error\":\"User denied this tool call\"}")); } _if_result_801; }); 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_750 = 0; if (str_eq(stored_variant, EL_STR("web"))) { _if_result_750 = (agentic_tools_with_web()); } else { _if_result_750 = (({ el_val_t _if_result_751 = 0; if (str_eq(stored_variant, EL_STR("all"))) { _if_result_751 = (agentic_tools_all()); } else { _if_result_751 = (agentic_tools_literal()); } _if_result_751; })); } _if_result_750; }); + el_val_t tools_json = ({ el_val_t _if_result_803 = 0; if (str_eq(stored_variant, EL_STR("web"))) { _if_result_803 = (agentic_tools_with_web()); } else { _if_result_803 = (({ el_val_t _if_result_804 = 0; if (str_eq(stored_variant, EL_STR("all"))) { _if_result_804 = (agentic_tools_all()); } else { _if_result_804 = (agentic_tools_literal()); } _if_result_804; })); } _if_result_803; }); 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); @@ -31063,24 +31290,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_752 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_752 = (60); } else { _if_result_752 = (str_to_int(limit_str)); } _if_result_752; }); + el_val_t limit = ({ el_val_t _if_result_805 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_805 = (60); } else { _if_result_805 = (str_to_int(limit_str)); } _if_result_805; }); 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_753 = 0; if (str_eq(win_str, EL_STR(""))) { _if_result_753 = (now); } else { _if_result_753 = (str_to_int(win_str)); } _if_result_753; }); + el_val_t win_start = ({ el_val_t _if_result_806 = 0; if (str_eq(win_str, EL_STR(""))) { _if_result_806 = (now); } else { _if_result_806 = (str_to_int(win_str)); } _if_result_806; }); 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_754 = 0; if (str_eq(prev_count_str, EL_STR(""))) { _if_result_754 = (0); } else { _if_result_754 = (str_to_int(prev_count_str)); } _if_result_754; }); - el_val_t eff_count = ({ el_val_t _if_result_755 = 0; if (in_window) { _if_result_755 = (prev_count); } else { _if_result_755 = (0); } _if_result_755; }); - el_val_t eff_win = ({ el_val_t _if_result_756 = 0; if (in_window) { _if_result_756 = (win_start); } else { _if_result_756 = (now); } _if_result_756; }); + el_val_t prev_count = ({ el_val_t _if_result_807 = 0; if (str_eq(prev_count_str, EL_STR(""))) { _if_result_807 = (0); } else { _if_result_807 = (str_to_int(prev_count_str)); } _if_result_807; }); + el_val_t eff_count = ({ el_val_t _if_result_808 = 0; if (in_window) { _if_result_808 = (prev_count); } else { _if_result_808 = (0); } _if_result_808; }); + el_val_t eff_win = ({ el_val_t _if_result_809 = 0; if (in_window) { _if_result_809 = (win_start); } else { _if_result_809 = (now); } _if_result_809; }); 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_757 = 0; if ((retry_after < 0)) { _if_result_757 = (0); } else { _if_result_757 = (retry_after); } _if_result_757; }); + el_val_t eff_retry = ({ el_val_t _if_result_810 = 0; if ((retry_after < 0)) { _if_result_810 = (0); } else { _if_result_810 = (retry_after); } _if_result_810; }); 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(""); @@ -31109,18 +31336,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_758 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_758 = (EL_STR("0")); } else { _if_result_758 = (boot); } _if_result_758; }); + el_val_t boot_num = ({ el_val_t _if_result_811 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_811 = (EL_STR("0")); } else { _if_result_811 = (boot); } _if_result_811; }); 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_759 = 0; if (str_eq(pulse, EL_STR(""))) { _if_result_759 = (EL_STR("0")); } else { _if_result_759 = (pulse); } _if_result_759; }); + el_val_t pulse_num = ({ el_val_t _if_result_812 = 0; if (str_eq(pulse, EL_STR(""))) { _if_result_812 = (EL_STR("0")); } else { _if_result_812 = (pulse); } _if_result_812; }); el_val_t boot_ts_str = state_get(EL_STR("soul_boot_ts")); - el_val_t uptime_secs = ({ el_val_t _if_result_760 = 0; if (str_eq(boot_ts_str, EL_STR(""))) { _if_result_760 = ((-1)); } else { _if_result_760 = ((time_now() - str_to_int(boot_ts_str))); } _if_result_760; }); + el_val_t uptime_secs = ({ el_val_t _if_result_813 = 0; if (str_eq(boot_ts_str, EL_STR(""))) { _if_result_813 = ((-1)); } else { _if_result_813 = ((time_now() - str_to_int(boot_ts_str))); } _if_result_813; }); el_val_t model = state_get(EL_STR("soul_model")); - el_val_t eff_model = ({ el_val_t _if_result_761 = 0; if (str_eq(model, EL_STR(""))) { _if_result_761 = (EL_STR("claude-sonnet-4-5")); } else { _if_result_761 = (model); } _if_result_761; }); + el_val_t eff_model = ({ el_val_t _if_result_814 = 0; if (str_eq(model, EL_STR(""))) { _if_result_814 = (EL_STR("claude-sonnet-4-5")); } else { _if_result_814 = (model); } _if_result_814; }); 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_762 = 0; if (llm_ok) { _if_result_762 = (EL_STR("ok")); } else { _if_result_762 = (EL_STR("unreachable")); } _if_result_762; }); + el_val_t llm_status = ({ el_val_t _if_result_815 = 0; if (llm_ok) { _if_result_815 = (EL_STR("ok")); } else { _if_result_815 = (EL_STR("unreachable")); } _if_result_815; }); 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; } @@ -31190,30 +31417,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_763 = 0; if (str_eq(event_type, EL_STR(""))) { _if_result_763 = (EL_STR("chat")); } else { _if_result_763 = (event_type); } _if_result_763; }); - el_val_t eff_payload = ({ el_val_t _if_result_764 = 0; if (str_eq(payload, EL_STR(""))) { _if_result_764 = (content_raw); } else { _if_result_764 = (payload); } _if_result_764; }); + el_val_t eff_event = ({ el_val_t _if_result_816 = 0; if (str_eq(event_type, EL_STR(""))) { _if_result_816 = (EL_STR("chat")); } else { _if_result_816 = (event_type); } _if_result_816; }); + el_val_t eff_payload = ({ el_val_t _if_result_817 = 0; if (str_eq(payload, EL_STR(""))) { _if_result_817 = (content_raw); } else { _if_result_817 = (payload); } _if_result_817; }); 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_765 = 0; if (str_eq(msg, EL_STR(""))) { _if_result_765 = (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_765 = (eff_payload); } _if_result_765; }); + el_val_t chat_body = ({ el_val_t _if_result_818 = 0; if (str_eq(msg, EL_STR(""))) { _if_result_818 = (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_818 = (eff_payload); } _if_result_818; }); 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_766 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_766 = (handle_chat_plan(chat_body)); } else { _if_result_766 = (({ el_val_t _if_result_767 = 0; if (agentic_flag) { _if_result_767 = (handle_chat_agentic(chat_body)); } else { el_val_t screened_reply = layered_cycle(raw_msg, json_get(chat_body, EL_STR("session_id")), is_utility_request(chat_body, json_get(chat_body, EL_STR("session_id")))); _if_result_767 = (plain_chat_envelope(screened_reply, chat_default_model())); } _if_result_767; })); } _if_result_766; }); + el_val_t reply = ({ el_val_t _if_result_819 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_819 = (handle_chat_plan(chat_body)); } else { _if_result_819 = (({ el_val_t _if_result_820 = 0; if (agentic_flag) { _if_result_820 = (handle_chat_agentic(chat_body)); } else { el_val_t screened_reply = layered_cycle(raw_msg, json_get(chat_body, EL_STR("session_id")), is_utility_request(chat_body, json_get(chat_body, EL_STR("session_id")))); _if_result_820 = (plain_chat_envelope(screened_reply, chat_default_model())); } _if_result_820; })); } _if_result_819; }); 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_768 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_768 = (20); } else { _if_result_768 = (str_to_int(limit_str)); } _if_result_768; }); - el_val_t q = ({ el_val_t _if_result_769 = 0; if (str_eq(query, EL_STR(""))) { _if_result_769 = (eff_payload); } else { _if_result_769 = (query); } _if_result_769; }); + el_val_t limit = ({ el_val_t _if_result_821 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_821 = (20); } else { _if_result_821 = (str_to_int(limit_str)); } _if_result_821; }); + el_val_t q = ({ el_val_t _if_result_822 = 0; if (str_eq(query, EL_STR(""))) { _if_result_822 = (eff_payload); } else { _if_result_822 = (query); } _if_result_822; }); 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_770 = 0; if (str_eq(method_field, EL_STR(""))) { _if_result_770 = (EL_STR("POST")); } else { _if_result_770 = (method_field); } _if_result_770; }); + el_val_t eff_method = ({ el_val_t _if_result_823 = 0; if (str_eq(method_field, EL_STR(""))) { _if_result_823 = (EL_STR("POST")); } else { _if_result_823 = (method_field); } _if_result_823; }); return handle_tool(path_field, eff_method, tool_body); } if (str_eq(eff_event, EL_STR("see"))) { @@ -31248,7 +31475,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_771 = 0; if (str_eq(body, EL_STR(""))) { _if_result_771 = (EL_STR("{}")); } else { _if_result_771 = (body); } _if_result_771; }); + el_val_t eff = ({ el_val_t _if_result_824 = 0; if (str_eq(body, EL_STR(""))) { _if_result_824 = (EL_STR("{}")); } else { _if_result_824 = (body); } _if_result_824; }); 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)); @@ -31320,22 +31547,22 @@ el_val_t route_dispatch(el_val_t method, el_val_t path, el_val_t body) { } if (str_eq(clean, EL_STR("/api/graph/edges"))) { el_val_t scratch_dir = env(EL_STR("TMPDIR")); - el_val_t scratch_base = ({ el_val_t _if_result_772 = 0; if (str_eq(scratch_dir, EL_STR(""))) { _if_result_772 = (EL_STR("/tmp")); } else { _if_result_772 = (scratch_dir); } _if_result_772; }); + el_val_t scratch_base = ({ el_val_t _if_result_825 = 0; if (str_eq(scratch_dir, EL_STR(""))) { _if_result_825 = (EL_STR("/tmp")); } else { _if_result_825 = (scratch_dir); } _if_result_825; }); el_val_t snap_path = el_str_concat(el_str_concat(el_str_concat(scratch_base, EL_STR("/soul-edges-export-")), state_get(EL_STR("soul_cgi_id"))), EL_STR(".json")); engram_save(snap_path); el_val_t snap = fs_read(snap_path); el_val_t edges_raw = json_get_raw(snap, EL_STR("edges")); - return ({ el_val_t _if_result_773 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_773 = (EL_STR("[]")); } else { _if_result_773 = (edges_raw); } _if_result_773; }); + return ({ el_val_t _if_result_826 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_826 = (EL_STR("[]")); } else { _if_result_826 = (edges_raw); } _if_result_826; }); } 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_774 = 0; if (str_eq(raw_msg, EL_STR(""))) { _if_result_774 = (body); } else { _if_result_774 = (raw_msg); } _if_result_774; }); + el_val_t eff_msg = ({ el_val_t _if_result_827 = 0; if (str_eq(raw_msg, EL_STR(""))) { _if_result_827 = (body); } else { _if_result_827 = (raw_msg); } _if_result_827; }); 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_775 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_775 = (handle_chat_plan(body)); } else { _if_result_775 = (({ el_val_t _if_result_776 = 0; if (agentic_flag) { _if_result_776 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(eff_msg, json_get(body, EL_STR("session_id")), is_utility_request(body, json_get(body, EL_STR("session_id")))); _if_result_776 = (plain_chat_envelope(screened_reply, chat_default_model())); } _if_result_776; })); } _if_result_775; }); + el_val_t reply = ({ el_val_t _if_result_828 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_828 = (handle_chat_plan(body)); } else { _if_result_828 = (({ el_val_t _if_result_829 = 0; if (agentic_flag) { _if_result_829 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(eff_msg, json_get(body, EL_STR("session_id")), is_utility_request(body, json_get(body, EL_STR("session_id")))); _if_result_829 = (plain_chat_envelope(screened_reply, chat_default_model())); } _if_result_829; })); } _if_result_828; }); auto_persist(body, reply); return reply; } @@ -31419,7 +31646,7 @@ el_val_t route_dispatch(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_777 = 0; if (str_eq(rp_raw, EL_STR(""))) { _if_result_777 = (EL_STR("[]")); } else { _if_result_777 = (el_str_concat(el_str_concat(EL_STR("["), rp_raw), EL_STR("]"))); } _if_result_777; }); + el_val_t rp_arr = ({ el_val_t _if_result_830 = 0; if (str_eq(rp_raw, EL_STR(""))) { _if_result_830 = (EL_STR("[]")); } else { _if_result_830 = (el_str_concat(el_str_concat(EL_STR("["), rp_raw), EL_STR("]"))); } _if_result_830; }); return el_str_concat(el_str_concat(EL_STR("{\"progress\":"), rp_arr), EL_STR("}")); } } @@ -31429,7 +31656,7 @@ el_val_t route_dispatch(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_778 = 0; if ((gs_slash < 0)) { _if_result_778 = (gs_after); } else { _if_result_778 = (str_slice(gs_after, 0, gs_slash)); } _if_result_778; }); + el_val_t gs_id = ({ el_val_t _if_result_831 = 0; if ((gs_slash < 0)) { _if_result_831 = (gs_after); } else { _if_result_831 = (str_slice(gs_after, 0, gs_slash)); } _if_result_831; }); if (!str_eq(gs_id, EL_STR(""))) { return session_get(gs_id); } @@ -31443,14 +31670,14 @@ el_val_t route_dispatch(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_779 = 0; if ((slash < 0)) { _if_result_779 = (after); } else { _if_result_779 = (str_slice(after, 0, slash)); } _if_result_779; }); + el_val_t session_id = ({ el_val_t _if_result_832 = 0; if ((slash < 0)) { _if_result_832 = (after); } else { _if_result_832 = (str_slice(after, 0, slash)); } _if_result_832; }); 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_780 = 0; if ((sess_slash < 0)) { _if_result_780 = (sess_after); } else { _if_result_780 = (str_slice(sess_after, 0, sess_slash)); } _if_result_780; }); - el_val_t sess_sub = ({ el_val_t _if_result_781 = 0; if ((sess_slash < 0)) { _if_result_781 = (EL_STR("")); } else { _if_result_781 = (str_slice(sess_after, (sess_slash + 1), str_len(sess_after))); } _if_result_781; }); + el_val_t sess_id = ({ el_val_t _if_result_833 = 0; if ((sess_slash < 0)) { _if_result_833 = (sess_after); } else { _if_result_833 = (str_slice(sess_after, 0, sess_slash)); } _if_result_833; }); + el_val_t sess_sub = ({ el_val_t _if_result_834 = 0; if ((sess_slash < 0)) { _if_result_834 = (EL_STR("")); } else { _if_result_834 = (str_slice(sess_after, (sess_slash + 1), str_len(sess_after))); } _if_result_834; }); if (!str_eq(sess_id, EL_STR("")) && str_eq(sess_sub, EL_STR("approve"))) { return handle_session_approve(sess_id, body); } @@ -31474,7 +31701,7 @@ el_val_t route_dispatch(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_782 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_782 = (handle_chat_plan(body)); } else { _if_result_782 = (({ el_val_t _if_result_783 = 0; if (agentic_flag) { _if_result_783 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(raw_msg, json_get(body, EL_STR("session_id")), is_utility_request(body, json_get(body, EL_STR("session_id")))); _if_result_783 = (plain_chat_envelope(screened_reply, chat_default_model())); } _if_result_783; })); } _if_result_782; }); + el_val_t reply = ({ el_val_t _if_result_835 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_835 = (handle_chat_plan(body)); } else { _if_result_835 = (({ el_val_t _if_result_836 = 0; if (agentic_flag) { _if_result_836 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(raw_msg, json_get(body, EL_STR("session_id")), is_utility_request(body, json_get(body, EL_STR("session_id")))); _if_result_836 = (plain_chat_envelope(screened_reply, chat_default_model())); } _if_result_836; })); } _if_result_835; }); auto_persist(body, reply); return reply; } @@ -31601,7 +31828,7 @@ el_val_t route_dispatch(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_784 = 0; if ((del_slash < 0)) { _if_result_784 = (del_after); } else { _if_result_784 = (str_slice(del_after, 0, del_slash)); } _if_result_784; }); + el_val_t del_id = ({ el_val_t _if_result_837 = 0; if ((del_slash < 0)) { _if_result_837 = (del_after); } else { _if_result_837 = (str_slice(del_after, 0, del_slash)); } _if_result_837; }); if (!str_eq(del_id, EL_STR(""))) { return session_delete(del_id); } @@ -31612,7 +31839,7 @@ el_val_t route_dispatch(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_785 = 0; if ((patch_slash < 0)) { _if_result_785 = (patch_after); } else { _if_result_785 = (str_slice(patch_after, 0, patch_slash)); } _if_result_785; }); + el_val_t patch_id = ({ el_val_t _if_result_838 = 0; if ((patch_slash < 0)) { _if_result_838 = (patch_after); } else { _if_result_838 = (str_slice(patch_after, 0, patch_slash)); } _if_result_838; }); if (!str_eq(patch_id, EL_STR(""))) { return session_update_patch(patch_id, body); } @@ -31738,8 +31965,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_786 = 0; if (str_eq(bn_ts_raw, EL_STR(""))) { _if_result_786 = (0); } else { _if_result_786 = (str_to_int(bn_ts_raw)); } _if_result_786; }); - el_val_t snip = ({ el_val_t _if_result_787 = 0; if ((str_len(bn_c) > 200)) { _if_result_787 = (str_slice(bn_c, 0, 200)); } else { _if_result_787 = (bn_c); } _if_result_787; }); + el_val_t bn_ts = ({ el_val_t _if_result_839 = 0; if (str_eq(bn_ts_raw, EL_STR(""))) { _if_result_839 = (0); } else { _if_result_839 = (str_to_int(bn_ts_raw)); } _if_result_839; }); + el_val_t snip = ({ el_val_t _if_result_840 = 0; if ((str_len(bn_c) > 200)) { _if_result_840 = (str_slice(bn_c, 0, 200)); } else { _if_result_840 = (bn_c); } _if_result_840; }); 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(""))) { @@ -31760,21 +31987,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_788 = 0; if (intel_ok) { _if_result_788 = (json_get(node_intel, EL_STR("content"))); } else { _if_result_788 = (EL_STR("")); } _if_result_788; }); - el_val_t values_content = ({ el_val_t _if_result_789 = 0; if (values_ok) { _if_result_789 = (json_get(node_values, EL_STR("content"))); } else { _if_result_789 = (EL_STR("")); } _if_result_789; }); - el_val_t mem_content = ({ el_val_t _if_result_790 = 0; if (mem_ok) { _if_result_790 = (json_get(node_mem_phil, EL_STR("content"))); } else { _if_result_790 = (EL_STR("")); } _if_result_790; }); - el_val_t intel_short = ({ el_val_t _if_result_791 = 0; if ((str_len(intel_content) > 2000)) { _if_result_791 = (str_slice(intel_content, 0, 2000)); } else { _if_result_791 = (intel_content); } _if_result_791; }); - el_val_t values_short = ({ el_val_t _if_result_792 = 0; if ((str_len(values_content) > 2000)) { _if_result_792 = (str_slice(values_content, 0, 2000)); } else { _if_result_792 = (values_content); } _if_result_792; }); - el_val_t mem_short = ({ el_val_t _if_result_793 = 0; if ((str_len(mem_content) > 2000)) { _if_result_793 = (str_slice(mem_content, 0, 2000)); } else { _if_result_793 = (mem_content); } _if_result_793; }); + el_val_t intel_content = ({ el_val_t _if_result_841 = 0; if (intel_ok) { _if_result_841 = (json_get(node_intel, EL_STR("content"))); } else { _if_result_841 = (EL_STR("")); } _if_result_841; }); + el_val_t values_content = ({ el_val_t _if_result_842 = 0; if (values_ok) { _if_result_842 = (json_get(node_values, EL_STR("content"))); } else { _if_result_842 = (EL_STR("")); } _if_result_842; }); + el_val_t mem_content = ({ el_val_t _if_result_843 = 0; if (mem_ok) { _if_result_843 = (json_get(node_mem_phil, EL_STR("content"))); } else { _if_result_843 = (EL_STR("")); } _if_result_843; }); + el_val_t intel_short = ({ el_val_t _if_result_844 = 0; if ((str_len(intel_content) > 2000)) { _if_result_844 = (str_slice(intel_content, 0, 2000)); } else { _if_result_844 = (intel_content); } _if_result_844; }); + el_val_t values_short = ({ el_val_t _if_result_845 = 0; if ((str_len(values_content) > 2000)) { _if_result_845 = (str_slice(values_content, 0, 2000)); } else { _if_result_845 = (values_content); } _if_result_845; }); + el_val_t mem_short = ({ el_val_t _if_result_846 = 0; if ((str_len(mem_content) > 2000)) { _if_result_846 = (str_slice(mem_content, 0, 2000)); } else { _if_result_846 = (mem_content); } _if_result_846; }); el_val_t parts_count = 0; - parts_count = ({ el_val_t _if_result_794 = 0; if (intel_ok) { _if_result_794 = ((parts_count + 1)); } else { _if_result_794 = (parts_count); } _if_result_794; }); - parts_count = ({ el_val_t _if_result_795 = 0; if (values_ok) { _if_result_795 = ((parts_count + 1)); } else { _if_result_795 = (parts_count); } _if_result_795; }); - parts_count = ({ el_val_t _if_result_796 = 0; if (mem_ok) { _if_result_796 = ((parts_count + 1)); } else { _if_result_796 = (parts_count); } _if_result_796; }); + parts_count = ({ el_val_t _if_result_847 = 0; if (intel_ok) { _if_result_847 = ((parts_count + 1)); } else { _if_result_847 = (parts_count); } _if_result_847; }); + parts_count = ({ el_val_t _if_result_848 = 0; if (values_ok) { _if_result_848 = ((parts_count + 1)); } else { _if_result_848 = (parts_count); } _if_result_848; }); + parts_count = ({ el_val_t _if_result_849 = 0; if (mem_ok) { _if_result_849 = ((parts_count + 1)); } else { _if_result_849 = (parts_count); } _if_result_849; }); if (parts_count > 0) { el_val_t ctx = EL_STR(""); - ctx = ({ el_val_t _if_result_797 = 0; if (intel_ok) { _if_result_797 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[INTELLECTUAL-DNA]\n")), intel_short), EL_STR("\n\n"))); } else { _if_result_797 = (ctx); } _if_result_797; }); - ctx = ({ el_val_t _if_result_798 = 0; if (values_ok) { _if_result_798 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[VALUES]\n")), values_short), EL_STR("\n\n"))); } else { _if_result_798 = (ctx); } _if_result_798; }); - ctx = ({ el_val_t _if_result_799 = 0; if (mem_ok) { _if_result_799 = (el_str_concat(el_str_concat(ctx, EL_STR("[MEMORY-PHILOSOPHY]\n")), mem_short)); } else { _if_result_799 = (ctx); } _if_result_799; }); + ctx = ({ el_val_t _if_result_850 = 0; if (intel_ok) { _if_result_850 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[INTELLECTUAL-DNA]\n")), intel_short), EL_STR("\n\n"))); } else { _if_result_850 = (ctx); } _if_result_850; }); + ctx = ({ el_val_t _if_result_851 = 0; if (values_ok) { _if_result_851 = (el_str_concat(el_str_concat(el_str_concat(ctx, EL_STR("[VALUES]\n")), values_short), EL_STR("\n\n"))); } else { _if_result_851 = (ctx); } _if_result_851; }); + ctx = ({ el_val_t _if_result_852 = 0; if (mem_ok) { _if_result_852 = (el_str_concat(el_str_concat(ctx, EL_STR("[MEMORY-PHILOSOPHY]\n")), mem_short)); } else { _if_result_852 = (ctx); } _if_result_852; }); 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)"))); } @@ -31797,10 +32024,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_800 = 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_800 = (state_get(EL_STR("_bell_acc"))); } else { _if_result_800 = (EL_STR("")); } _if_result_800; }); + aff_ctx = ({ el_val_t _if_result_853 = 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_853 = (state_get(EL_STR("_bell_acc"))); } else { _if_result_853 = (EL_STR("")); } _if_result_853; }); 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_801 = 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_801 = (state_get(EL_STR("_pos_acc"))); } else { _if_result_801 = (aff_ctx); } _if_result_801; }); + aff_ctx = ({ el_val_t _if_result_854 = 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_854 = (state_get(EL_STR("_pos_acc"))); } else { _if_result_854 = (aff_ctx); } _if_result_854; }); 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)"))); @@ -31846,19 +32073,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_802 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_802 = (EL_STR("0")); } else { _if_result_802 = (boot); } _if_result_802; }); + el_val_t boot_num = ({ el_val_t _if_result_855 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_855 = (EL_STR("0")); } else { _if_result_855 = (boot); } _if_result_855; }); 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_803 = 0; if (str_eq(id_ctx, EL_STR(""))) { _if_result_803 = (EL_STR("false")); } else { _if_result_803 = (EL_STR("true")); } _if_result_803; }); + el_val_t has_identity = ({ el_val_t _if_result_856 = 0; if (str_eq(id_ctx, EL_STR(""))) { _if_result_856 = (EL_STR("false")); } else { _if_result_856 = (EL_STR("true")); } _if_result_856; }); 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_804 = 0; if (!str_eq(cgi_from_state, EL_STR(""))) { _if_result_804 = (cgi_from_state); } else { _if_result_804 = (({ el_val_t _if_result_805 = 0; if (!str_eq(cgi_from_env, EL_STR(""))) { _if_result_805 = (cgi_from_env); } else { _if_result_805 = (EL_STR("ntn-genesis")); } _if_result_805; })); } _if_result_804; }); + el_val_t eff_cgi = ({ el_val_t _if_result_857 = 0; if (!str_eq(cgi_from_state, EL_STR(""))) { _if_result_857 = (cgi_from_state); } else { _if_result_857 = (({ el_val_t _if_result_858 = 0; if (!str_eq(cgi_from_env, EL_STR(""))) { _if_result_858 = (cgi_from_env); } else { _if_result_858 = (EL_STR("ntn-genesis")); } _if_result_858; })); } _if_result_857; }); 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_806 = 0; if (prev_sum_ok) { _if_result_806 = (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_806 = (({ el_val_t _if_result_807 = 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_807 = (({ el_val_t _if_result_808 = 0; if ((str_eq(stype, EL_STR("SessionSummary")) && !str_eq(scontent, EL_STR("")))) { _if_result_808 = (scontent); } else { _if_result_808 = (EL_STR("")); } _if_result_808; })); } else { _if_result_807 = (EL_STR("")); } _if_result_807; })); } _if_result_806; }); - el_val_t has_prev_sum = ({ el_val_t _if_result_809 = 0; if (str_eq(prev_sum_content, EL_STR(""))) { _if_result_809 = (EL_STR("false")); } else { _if_result_809 = (EL_STR("true")); } _if_result_809; }); + el_val_t prev_sum_content = ({ el_val_t _if_result_859 = 0; if (prev_sum_ok) { _if_result_859 = (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_859 = (({ el_val_t _if_result_860 = 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_860 = (({ el_val_t _if_result_861 = 0; if ((str_eq(stype, EL_STR("SessionSummary")) && !str_eq(scontent, EL_STR("")))) { _if_result_861 = (scontent); } else { _if_result_861 = (EL_STR("")); } _if_result_861; })); } else { _if_result_860 = (EL_STR("")); } _if_result_860; })); } _if_result_859; }); + el_val_t has_prev_sum = ({ el_val_t _if_result_862 = 0; if (str_eq(prev_sum_content, EL_STR(""))) { _if_result_862 = (EL_STR("false")); } else { _if_result_862 = (EL_STR("true")); } _if_result_862; }); 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)"))); @@ -31905,23 +32132,23 @@ el_val_t layered_cycle(el_val_t raw_input, el_val_t session_id, el_val_t utility 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_810 = 0; if (str_eq(session_id, EL_STR(""))) { _if_result_810 = (EL_STR("session_continuity")); } else { _if_result_810 = (el_str_concat(EL_STR("session_continuity:"), session_id)); } _if_result_810; }); + el_val_t cont_key = ({ el_val_t _if_result_863 = 0; if (str_eq(session_id, EL_STR(""))) { _if_result_863 = (EL_STR("session_continuity")); } else { _if_result_863 = (el_str_concat(EL_STR("session_continuity:"), session_id)); } _if_result_863; }); state_set(cont_key, cont_status); - el_val_t guided = ({ el_val_t _if_result_811 = 0; if (str_eq(cont_action, EL_STR("identity_check"))) { _if_result_811 = (el_str_concat(screened, EL_STR(" [steward:identity_check]"))); } else { _if_result_811 = (({ el_val_t _if_result_812 = 0; if (str_eq(cont_action, EL_STR("soft_check"))) { _if_result_812 = (el_str_concat(screened, EL_STR(" [steward:continuity_concern]"))); } else { _if_result_812 = (screened); } _if_result_812; })); } _if_result_811; }); + el_val_t guided = ({ el_val_t _if_result_864 = 0; if (str_eq(cont_action, EL_STR("identity_check"))) { _if_result_864 = (el_str_concat(screened, EL_STR(" [steward:identity_check]"))); } else { _if_result_864 = (({ el_val_t _if_result_865 = 0; if (str_eq(cont_action, EL_STR("soft_check"))) { _if_result_865 = (el_str_concat(screened, EL_STR(" [steward:continuity_concern]"))); } else { _if_result_865 = (screened); } _if_result_865; })); } _if_result_864; }); 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_813 = 0; if (str_eq(steward_action, EL_STR("pass"))) { _if_result_813 = (json_get(steward_result, EL_STR("content"))); } else { _if_result_813 = (json_get(steward_result, EL_STR("redirect_to"))); } _if_result_813; }); + el_val_t aligned = ({ el_val_t _if_result_866 = 0; if (str_eq(steward_action, EL_STR("pass"))) { _if_result_866 = (json_get(steward_result, EL_STR("content"))); } else { _if_result_866 = (json_get(steward_result, EL_STR("redirect_to"))); } _if_result_866; }); 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_814 = 0; if (lc_has_bell) { el_val_t lb0 = json_array_get(lc_bell_nodes, 0); el_val_t lb_ts = affective_node_ts(lb0); _if_result_814 = (({ el_val_t _if_result_815 = 0; if ((lb_ts > lc_aff_cutoff)) { _if_result_815 = (EL_STR("[AFFECTIVE NOTE: User was in distress in a recent session.]")); } else { _if_result_815 = (EL_STR("")); } _if_result_815; })); } else { _if_result_814 = (EL_STR("")); } _if_result_814; }); + el_val_t lc_bell_note = ({ el_val_t _if_result_867 = 0; if (lc_has_bell) { el_val_t lb0 = json_array_get(lc_bell_nodes, 0); el_val_t lb_ts = affective_node_ts(lb0); _if_result_867 = (({ el_val_t _if_result_868 = 0; if ((lb_ts > lc_aff_cutoff)) { _if_result_868 = (EL_STR("[AFFECTIVE NOTE: User was in distress in a recent session.]")); } else { _if_result_868 = (EL_STR("")); } _if_result_868; })); } else { _if_result_867 = (EL_STR("")); } _if_result_867; }); 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_816 = 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_ts = affective_node_ts(lp0); _if_result_816 = (({ el_val_t _if_result_817 = 0; if ((lp_ts > lc_aff_cutoff)) { _if_result_817 = (EL_STR("[AFFECTIVE NOTE: User shared positive news in a recent session.]")); } else { _if_result_817 = (EL_STR("")); } _if_result_817; })); } else { _if_result_816 = (EL_STR("")); } _if_result_816; }); - el_val_t lc_affective_note = ({ el_val_t _if_result_818 = 0; if (!str_eq(lc_bell_note, EL_STR(""))) { _if_result_818 = (lc_bell_note); } else { _if_result_818 = (lc_pos_note); } _if_result_818; }); + el_val_t lc_pos_note = ({ el_val_t _if_result_869 = 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_ts = affective_node_ts(lp0); _if_result_869 = (({ el_val_t _if_result_870 = 0; if ((lp_ts > lc_aff_cutoff)) { _if_result_870 = (EL_STR("[AFFECTIVE NOTE: User shared positive news in a recent session.]")); } else { _if_result_870 = (EL_STR("")); } _if_result_870; })); } else { _if_result_869 = (EL_STR("")); } _if_result_869; }); + el_val_t lc_affective_note = ({ el_val_t _if_result_871 = 0; if (!str_eq(lc_bell_note, EL_STR(""))) { _if_result_871 = (lc_bell_note); } else { _if_result_871 = (lc_pos_note); } _if_result_871; }); el_val_t augmented_addendum = safety_augment_system(EL_STR(""), raw_input); - augmented_addendum = ({ el_val_t _if_result_819 = 0; if (str_eq(lc_affective_note, EL_STR(""))) { _if_result_819 = (augmented_addendum); } else { _if_result_819 = (({ el_val_t _if_result_820 = 0; if (str_eq(augmented_addendum, EL_STR(""))) { _if_result_820 = (lc_affective_note); } else { _if_result_820 = (el_str_concat(el_str_concat(lc_affective_note, EL_STR("\n")), augmented_addendum)); } _if_result_820; })); } _if_result_819; }); + augmented_addendum = ({ el_val_t _if_result_872 = 0; if (str_eq(lc_affective_note, EL_STR(""))) { _if_result_872 = (augmented_addendum); } else { _if_result_872 = (({ el_val_t _if_result_873 = 0; if (str_eq(augmented_addendum, EL_STR(""))) { _if_result_873 = (lc_affective_note); } else { _if_result_873 = (el_str_concat(el_str_concat(lc_affective_note, EL_STR("\n")), augmented_addendum)); } _if_result_873; })); } _if_result_872; }); state_set(EL_STR("layered_cycle_safety_system_addendum"), augmented_addendum); el_val_t prompt = imprint_respond(aligned, imprint_id); el_val_t output = layered_generate(prompt, imprint_id, session_id); @@ -31937,17 +32164,17 @@ el_val_t layered_cycle(el_val_t raw_input, el_val_t session_id, el_val_t utility 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_821 = 0; if (str_eq(soul_cgi_id_raw, EL_STR(""))) { _if_result_821 = (EL_STR("ntn-genesis")); } else { _if_result_821 = (soul_cgi_id_raw); } _if_result_821; }); + soul_cgi_id = ({ el_val_t _if_result_874 = 0; if (str_eq(soul_cgi_id_raw, EL_STR(""))) { _if_result_874 = (EL_STR("ntn-genesis")); } else { _if_result_874 = (soul_cgi_id_raw); } _if_result_874; }); port_raw = env(EL_STR("NEURON_PORT")); - port = ({ el_val_t _if_result_822 = 0; if (str_eq(port_raw, EL_STR(""))) { _if_result_822 = (7770); } else { _if_result_822 = (str_to_int(port_raw)); } _if_result_822; }); + port = ({ el_val_t _if_result_875 = 0; if (str_eq(port_raw, EL_STR(""))) { _if_result_875 = (7770); } else { _if_result_875 = (str_to_int(port_raw)); } _if_result_875; }); 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_823 = 0; if (str_eq(snapshot_raw, EL_STR(""))) { _if_result_823 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/engram/snapshot.json"))); } else { _if_result_823 = (snapshot_raw); } _if_result_823; }); + snapshot = ({ el_val_t _if_result_876 = 0; if (str_eq(snapshot_raw, EL_STR(""))) { _if_result_876 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/engram/snapshot.json"))); } else { _if_result_876 = (snapshot_raw); } _if_result_876; }); axon_raw = env(EL_STR("NEURON_API_URL")); - axon_base = ({ el_val_t _if_result_824 = 0; if (str_eq(axon_raw, EL_STR(""))) { _if_result_824 = (EL_STR("http://localhost:7771")); } else { _if_result_824 = (axon_raw); } _if_result_824; }); + axon_base = ({ el_val_t _if_result_877 = 0; if (str_eq(axon_raw, EL_STR(""))) { _if_result_877 = (EL_STR("http://localhost:7771")); } else { _if_result_877 = (axon_raw); } _if_result_877; }); studio_dir_raw = env(EL_STR("SOUL_STUDIO_DIR")); - studio_dir = ({ el_val_t _if_result_825 = 0; if (str_eq(studio_dir_raw, EL_STR(""))) { _if_result_825 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/Development/neuron-technologies/products/cgi-studio/el-daemon"))); } else { _if_result_825 = (studio_dir_raw); } _if_result_825; }); + studio_dir = ({ el_val_t _if_result_878 = 0; if (str_eq(studio_dir_raw, EL_STR(""))) { _if_result_878 = (el_str_concat(env(EL_STR("HOME")), EL_STR("/Development/neuron-technologies/products/cgi-studio/el-daemon"))); } else { _if_result_878 = (studio_dir_raw); } _if_result_878; }); 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); @@ -31957,8 +32184,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_826 = 0; if (str_eq(nodes_json, EL_STR(""))) { _if_result_826 = (EL_STR("[]")); } else { _if_result_826 = (nodes_json); } _if_result_826; }); - el_val_t edges_part = ({ el_val_t _if_result_827 = 0; if (str_eq(edges_json, EL_STR(""))) { _if_result_827 = (EL_STR("[]")); } else { _if_result_827 = (edges_json); } _if_result_827; }); + el_val_t nodes_part = ({ el_val_t _if_result_879 = 0; if (str_eq(nodes_json, EL_STR(""))) { _if_result_879 = (EL_STR("[]")); } else { _if_result_879 = (nodes_json); } _if_result_879; }); + el_val_t edges_part = ({ el_val_t _if_result_880 = 0; if (str_eq(edges_json, EL_STR(""))) { _if_result_880 = (EL_STR("[]")); } else { _if_result_880 = (edges_json); } _if_result_880; }); 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); @@ -31982,7 +32209,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_828 = 0; if (str_eq(engram_url_raw, EL_STR(""))) { _if_result_828 = (fs_read(snapshot)); } else { _if_result_828 = (EL_STR("")); } _if_result_828; }); + guard_disk = ({ el_val_t _if_result_881 = 0; if (str_eq(engram_url_raw, EL_STR(""))) { _if_result_881 = (fs_read(snapshot)); } else { _if_result_881 = (EL_STR("")); } _if_result_881; }); 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/dist/soul.c.stamp b/dist/soul.c.stamp index 90c0264..6977880 100644 --- a/dist/soul.c.stamp +++ b/dist/soul.c.stamp @@ -1,9 +1,9 @@ # soul.c.stamp — fingerprint of the .el sources dist/soul.c was generated from. # Written by tools/soulc-stamp.sh --write. Do not hand-edit. -# generated_amalgam_sha256 63e30030bee5e87fa082a84cda5c1226896f49da6076101fcd6b9530ea7caf49 -# generated_amalgam_bytes 1204442 +# generated_amalgam_sha256 562193a341876f8fa0618ae7a76d0ecc073bb4d1d21b128c48b4aee28e1ef0d9 +# generated_amalgam_bytes 1226240 f8597e10546654bce3fbbe40461b2da59d0e06dbf1b038d1d362d24f949e3911 awareness.el -b6f3d14ca0c26017a2d617399a6d3754dabb0905e4d5f52eb75d25c4ad18d3c5 chat.el +2ff2dada732918c788a9ef66c6fd54c7a24cc4bbd4829197fe945d3a75ca1929 chat.el 42288c212cbf72fb1e8ecbd4d9900e4e9ee1cfa475b7974295c7637f1bf2939f elp-input.el b3f77f49d6086932c38bd17fe7a5eaf8bce25685f6fc3e1750f05729c6b49b9e imprint.el fba8ffdb9ba72bca5b09ca1c93a520edc52f3f4d8aec2c7585fe9b17e06420b2 manifest.el