#include #include #include "el_runtime.h" el_val_t tier_working(void); el_val_t tier_episodic(void); el_val_t tier_canonical(void); el_val_t mem_store(el_val_t content, el_val_t label, el_val_t tags); el_val_t mem_remember(el_val_t content, el_val_t tags); el_val_t mem_recall(el_val_t query, el_val_t depth); el_val_t mem_search(el_val_t query, el_val_t limit); el_val_t mem_strengthen(el_val_t node_id); el_val_t mem_forget(el_val_t node_id); el_val_t mem_consolidate(void); el_val_t mem_save(el_val_t path); el_val_t mem_load(el_val_t path); el_val_t mem_boot_count_get(void); el_val_t mem_boot_count_inc(void); el_val_t mem_emit_state_event(el_val_t trigger, el_val_t kind, el_val_t content); el_val_t idle_count(void); el_val_t idle_inc(void); el_val_t idle_reset(void); el_val_t ise_post(el_val_t content); el_val_t elapsed_ms(void); el_val_t elapsed_human(void); el_val_t embed_ok(void); el_val_t emit_heartbeat(void); el_val_t proactive_curiosity(void); el_val_t pulse_count(void); el_val_t pulse_inc(void); el_val_t make_action(el_val_t kind, el_val_t payload); el_val_t perceive(void); el_val_t attend(el_val_t node_json); el_val_t respond(el_val_t action_json); el_val_t record(el_val_t outcome_json); el_val_t one_cycle(void); el_val_t awareness_run(void); el_val_t security_research_authorized(void); el_val_t threat_score_command(el_val_t cmd); el_val_t threat_score_path(el_val_t path); el_val_t threat_score_history(el_val_t history); el_val_t threat_trajectory_check(el_val_t tool_name, el_val_t tool_input); el_val_t threat_history_append(el_val_t text); el_val_t chat_default_model(void); el_val_t engram_numeric_valid(el_val_t s); el_val_t parse_float_x100(el_val_t s); el_val_t engram_score_node(el_val_t node_json); el_val_t engram_render_node(el_val_t node_json); el_val_t engram_render_nodes(el_val_t nodes_json); el_val_t engram_dedup_nodes(el_val_t nodes_json); el_val_t engram_compile_ranked(el_val_t nodes_json, el_val_t max_nodes); el_val_t engram_split_topics(el_val_t message); el_val_t engram_extract_entities(el_val_t message); el_val_t engram_detect_recall_intent(el_val_t message); el_val_t engram_is_continuation(el_val_t message, el_val_t hist_len); el_val_t engram_compile_multi(el_val_t topic); el_val_t engram_nodes_merge(el_val_t a, el_val_t b); el_val_t id_in_seen(el_val_t node_id, el_val_t seen); el_val_t add_to_seen(el_val_t seen, el_val_t node_id); el_val_t engram_extract_ids(el_val_t nodes_json); el_val_t engram_compile(el_val_t intent); el_val_t json_safe(el_val_t s); el_val_t build_system_prompt(el_val_t ctx, el_val_t chat_mode); el_val_t hist_append(el_val_t hist, el_val_t role, el_val_t content); el_val_t hist_trim(el_val_t hist); el_val_t hist_trim_with_bell_guard(el_val_t hist); el_val_t clean_llm_response(el_val_t s); el_val_t conv_history_persist(el_val_t hist); el_val_t conv_history_load(void); el_val_t session_preload_bullets(el_val_t nodes, el_val_t max_bullets, el_val_t snip_len); el_val_t handle_chat(el_val_t body); el_val_t handle_see(el_val_t body); el_val_t studio_tools_json(void); el_val_t agentic_api_key(void); el_val_t agentic_tools_literal(void); el_val_t agentic_tools_with_web(void); el_val_t connector_tools_json(void); el_val_t agentic_tools_all(void); el_val_t call_mcp_bridge(el_val_t tool_name, el_val_t tool_input); el_val_t tool_auto_approved(el_val_t tool_name); el_val_t call_neuron_mcp(el_val_t tool_name, el_val_t args); el_val_t agent_workspace_root(void); el_val_t path_within_root(el_val_t path, el_val_t root); el_val_t resolve_in_root(el_val_t path, el_val_t root); el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input); el_val_t is_builtin_tool(el_val_t tool_name); el_val_t next_bridge_id(void); 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 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); el_val_t handle_dharma_room_turn(el_val_t body); el_val_t handle_dharma_room_turn_agentic(el_val_t body); el_val_t session_summary_write(el_val_t summary_text); el_val_t session_summary_write_dated(el_val_t summary_text, el_val_t label); 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 strengthen_chat_nodes(el_val_t activation_nodes); el_val_t auth_headers(el_val_t tok); el_val_t axon_get(el_val_t path); el_val_t axon_post(el_val_t path, el_val_t body); el_val_t handle_conversations(el_val_t method); el_val_t handle_config(el_val_t method, el_val_t body); el_val_t dharma_registry(void); el_val_t dharma_network_state(void); el_val_t handle_dharma(el_val_t path, el_val_t method, el_val_t body); el_val_t handle_tool(el_val_t path, el_val_t method, el_val_t body); el_val_t handle_nlg(el_val_t path, el_val_t method, el_val_t body); el_val_t render_studio(void); el_val_t elp_extract_topic(el_val_t msg); el_val_t elp_detect_predicate(el_val_t msg); el_val_t elp_parse(el_val_t msg); el_val_t handle_elp_chat(el_val_t body); el_val_t is_protected_node(el_val_t id); el_val_t api_err_protected(el_val_t id); el_val_t api_json_escape(el_val_t s); el_val_t api_query_param(el_val_t path, el_val_t key); el_val_t api_query_int(el_val_t path, el_val_t key, el_val_t default_val); el_val_t api_ok(el_val_t extra); el_val_t api_err(el_val_t msg); el_val_t api_nonempty(el_val_t s); el_val_t api_or_empty(el_val_t s); el_val_t api_persisted(el_val_t id); el_val_t api_not_persisted(el_val_t id); el_val_t handle_api_begin_session(el_val_t body); el_val_t handle_api_compile_ctx(el_val_t body); el_val_t handle_api_remember(el_val_t body); el_val_t handle_api_node_create(el_val_t body); el_val_t handle_api_node_delete(el_val_t body); 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 handle_api_search_knowledge(el_val_t method, el_val_t path, el_val_t body); el_val_t handle_api_browse_knowledge(el_val_t path, el_val_t body); el_val_t handle_api_capture_knowledge(el_val_t body); el_val_t handle_api_evolve_knowledge(el_val_t body); 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 handle_api_define_process(el_val_t body); 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 handle_api_inspect_config(el_val_t path, el_val_t body); 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 handle_api_link_entities(el_val_t body); el_val_t handle_api_forget(el_val_t body); el_val_t handle_api_evolve_memory(el_val_t body); el_val_t handle_api_memory_delete(el_val_t body); el_val_t handle_api_memory_update(el_val_t body); el_val_t handle_api_cultivate(el_val_t body); el_val_t handle_api_list_typed(el_val_t node_type, el_val_t path, el_val_t body); el_val_t handle_api_consolidate(el_val_t body); el_val_t session_title_from_message(el_val_t message); el_val_t session_make_content(el_val_t id, el_val_t title, el_val_t created_at, el_val_t updated_at, el_val_t folder); el_val_t session_exists(el_val_t session_id); el_val_t session_create(el_val_t body); el_val_t session_create_cleanup(el_val_t session_id); el_val_t session_list(void); el_val_t session_get(el_val_t session_id); el_val_t session_delete(el_val_t session_id); el_val_t session_update_patch(el_val_t session_id, el_val_t body); el_val_t session_search(el_val_t query); el_val_t session_hist_load(el_val_t session_id); el_val_t session_hist_save(el_val_t session_id, el_val_t hist); el_val_t init_soul_edges(void); el_val_t load_identity_context(void); el_val_t seed_persona_from_env(void); el_val_t emit_session_start_event(void); el_val_t layered_cycle(el_val_t raw_input); el_val_t rate_limit_check(el_val_t ip, el_val_t path); el_val_t strip_query(el_val_t path); el_val_t err_404(el_val_t path); el_val_t err_405(el_val_t method, el_val_t path); el_val_t route_health(void); el_val_t route_lineage(void); el_val_t route_imprint_contextual(el_val_t body); el_val_t route_imprint_user(el_val_t body); el_val_t route_synthesize(el_val_t body); el_val_t handle_dharma_recv(el_val_t body); el_val_t connectd_get(el_val_t suffix); el_val_t connectd_post(el_val_t suffix, el_val_t body); el_val_t handle_connectors(el_val_t method, el_val_t clean, el_val_t body); el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body); el_val_t rate_limit_check(el_val_t ip, el_val_t path) { if (str_eq(path, EL_STR("/health"))) { return EL_STR(""); } el_val_t limit_str = state_get(EL_STR("soul_rate_limit")); el_val_t limit = ({ el_val_t _if_result_1 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_1 = (60); } else { _if_result_1 = (str_to_int(limit_str)); } _if_result_1; }); 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_2 = 0; if (str_eq(win_str, EL_STR(""))) { _if_result_2 = (now); } else { _if_result_2 = (str_to_int(win_str)); } _if_result_2; }); 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_3 = 0; if (str_eq(prev_count_str, EL_STR(""))) { _if_result_3 = (0); } else { _if_result_3 = (str_to_int(prev_count_str)); } _if_result_3; }); el_val_t eff_count = ({ el_val_t _if_result_4 = 0; if (in_window) { _if_result_4 = (prev_count); } else { _if_result_4 = (0); } _if_result_4; }); el_val_t eff_win = ({ el_val_t _if_result_5 = 0; if (in_window) { _if_result_5 = (win_start); } else { _if_result_5 = (now); } _if_result_5; }); 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_6 = 0; if ((retry_after < 0)) { _if_result_6 = (0); } else { _if_result_6 = (retry_after); } _if_result_6; }); 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(""); return 0; } el_val_t strip_query(el_val_t path) { el_val_t q = str_index_of(path, EL_STR("?")); if (q < 0) { return path; } return str_slice(path, 0, q); return 0; } el_val_t err_404(el_val_t path) { return el_str_concat(el_str_concat(EL_STR("{\"error\":\"not found\",\"code\":\"not_found\",\"path\":\""), path), EL_STR("\"}")); return 0; } el_val_t err_405(el_val_t method, el_val_t path) { return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"error\":\"method not allowed\",\"code\":\"method_not_allowed\",\"method\":\""), method), EL_STR("\",\"path\":\"")), path), EL_STR("\"}")); return 0; } el_val_t route_health(void) { el_val_t cgi_id = state_get(EL_STR("soul_cgi_id")); el_val_t boot = state_get(EL_STR("soul_boot_count")); el_val_t boot_num = ({ el_val_t _if_result_7 = 0; if (str_eq(boot, EL_STR(""))) { _if_result_7 = (EL_STR("0")); } else { _if_result_7 = (boot); } _if_result_7; }); 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_8 = 0; if (str_eq(pulse, EL_STR(""))) { _if_result_8 = (EL_STR("0")); } else { _if_result_8 = (pulse); } _if_result_8; }); el_val_t boot_ts_str = state_get(EL_STR("soul_boot_ts")); el_val_t uptime_secs = ({ el_val_t _if_result_9 = 0; if (str_eq(boot_ts_str, EL_STR(""))) { _if_result_9 = ((-1)); } else { _if_result_9 = ((time_now() - str_to_int(boot_ts_str))); } _if_result_9; }); el_val_t model = state_get(EL_STR("soul_model")); el_val_t eff_model = ({ el_val_t _if_result_10 = 0; if (str_eq(model, EL_STR(""))) { _if_result_10 = (EL_STR("claude-sonnet-4-5")); } else { _if_result_10 = (model); } _if_result_10; }); 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_11 = 0; if (llm_ok) { _if_result_11 = (EL_STR("ok")); } else { _if_result_11 = (EL_STR("unreachable")); } _if_result_11; }); 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; } el_val_t route_lineage(void) { el_val_t cgi_id = state_get(EL_STR("soul_cgi_id")); el_val_t q = el_str_concat(EL_STR("lineage:"), cgi_id); el_val_t results = engram_search_json(q, 1); el_val_t len = json_array_len(results); if (len <= 0) { return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), cgi_id), EL_STR("\"")), EL_STR(",\"tier\":\"citizen\"")), EL_STR(",\"is_founding\":true")), EL_STR(",\"validation_attempts\":0")), EL_STR(",\"training_sessions\":0")), EL_STR(",\"is_sterile\":false}")); } el_val_t raw = json_get_raw(results, EL_STR("0")); return raw; return 0; } el_val_t route_imprint_contextual(el_val_t body) { if (str_eq(body, EL_STR(""))) { return EL_STR("{\"ok\":false,\"error\":\"empty body\"}"); } el_val_t tags = EL_STR("[\"imprint\",\"contextual\"]"); el_val_t id = engram_node_full(body, EL_STR("Entity"), EL_STR("imprint:contextual"), el_from_float(0.7), el_from_float(0.6), el_from_float(0.9), EL_STR("Working"), tags); if (str_eq(id, EL_STR(""))) { return EL_STR("{\"ok\":false,\"error\":\"engram write failed\"}"); } state_set(EL_STR("active_contextual_imprint"), id); return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), id), EL_STR("\"}")); return 0; } el_val_t route_imprint_user(el_val_t body) { if (str_eq(body, EL_STR(""))) { return EL_STR("{\"ok\":false,\"error\":\"empty body\"}"); } el_val_t tags = EL_STR("[\"imprint\",\"user\"]"); el_val_t id = engram_node_full(body, EL_STR("Entity"), EL_STR("imprint:user"), el_from_float(0.7), el_from_float(0.6), el_from_float(0.9), EL_STR("Working"), tags); if (str_eq(id, EL_STR(""))) { return EL_STR("{\"ok\":false,\"error\":\"engram write failed\"}"); } state_set(EL_STR("active_user_imprint"), id); return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), id), EL_STR("\"}")); return 0; } el_val_t route_synthesize(el_val_t body) { if (str_eq(body, EL_STR(""))) { return EL_STR("{\"error\":\"body is required\",\"code\":\"missing_param\"}"); } el_val_t parent_a = json_get(body, EL_STR("parent_a")); el_val_t parent_b = json_get(body, EL_STR("parent_b")); if (str_eq(parent_a, EL_STR(""))) { return EL_STR("{\"error\":\"parent_a is required\",\"code\":\"missing_param\"}"); } if (str_eq(parent_b, EL_STR(""))) { return EL_STR("{\"error\":\"parent_b is required\",\"code\":\"missing_param\"}"); } el_val_t req = el_str_concat(el_str_concat(el_str_concat(EL_STR("synthesize "), parent_a), EL_STR(" ")), parent_b); el_val_t tags = EL_STR("[\"soul-inbox-pending\",\"synthesis-request\"]"); engram_node_full(req, EL_STR("Entity"), EL_STR("synthesis-request"), el_from_float(0.8), el_from_float(0.8), el_from_float(0.9), EL_STR("Working"), tags); return EL_STR("{\"mechanism\":\"did not engage\"}"); return 0; } el_val_t handle_dharma_recv(el_val_t body) { el_val_t content_raw = json_get(body, EL_STR("content")); el_val_t from_id = json_get(body, EL_STR("from")); el_val_t event_type = json_get(content_raw, EL_STR("event_type")); el_val_t payload = json_get(content_raw, EL_STR("payload")); el_val_t eff_event = ({ el_val_t _if_result_12 = 0; if (str_eq(event_type, EL_STR(""))) { _if_result_12 = (EL_STR("chat")); } else { _if_result_12 = (event_type); } _if_result_12; }); el_val_t eff_payload = ({ el_val_t _if_result_13 = 0; if (str_eq(payload, EL_STR(""))) { _if_result_13 = (content_raw); } else { _if_result_13 = (payload); } _if_result_13; }); 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_14 = 0; if (str_eq(msg, EL_STR(""))) { _if_result_14 = (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_14 = (eff_payload); } _if_result_14; }); 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 reply = ({ el_val_t _if_result_15 = 0; if (agentic_flag) { _if_result_15 = (handle_chat_agentic(chat_body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_15 = (screened_reply); } _if_result_15; }); 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_16 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_16 = (20); } else { _if_result_16 = (str_to_int(limit_str)); } _if_result_16; }); el_val_t q = ({ el_val_t _if_result_17 = 0; if (str_eq(query, EL_STR(""))) { _if_result_17 = (eff_payload); } else { _if_result_17 = (query); } _if_result_17; }); 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_18 = 0; if (str_eq(method_field, EL_STR(""))) { _if_result_18 = (EL_STR("POST")); } else { _if_result_18 = (method_field); } _if_result_18; }); return handle_tool(path_field, eff_method, tool_body); } if (str_eq(eff_event, EL_STR("see"))) { return handle_see(eff_payload); } if (str_eq(eff_event, EL_STR("health"))) { return route_health(); } if (str_eq(eff_event, EL_STR("dharma_room_turn_agentic"))) { return handle_dharma_room_turn_agentic(eff_payload); } if (str_eq(eff_event, EL_STR("dharma_room_turn"))) { return handle_dharma_room_turn(eff_payload); } if (str_eq(eff_event, EL_STR("chat_as_soul"))) { return handle_chat_as_soul(eff_payload); } if (str_eq(eff_event, EL_STR("elp"))) { return handle_elp_chat(eff_payload); } return el_str_concat(el_str_concat(EL_STR("{\"error\":\"unknown event_type\",\"event_type\":\""), eff_event), EL_STR("\"}")); return 0; } el_val_t connectd_get(el_val_t suffix) { el_val_t out = exec_capture(el_str_concat(EL_STR("curl -s --max-time 5 http://127.0.0.1:7771"), suffix)); if (str_eq(out, EL_STR(""))) { return EL_STR("{\"ok\":false,\"error\":\"connector bridge unreachable (neuron-connectd on :7771)\"}"); } return out; return 0; } el_val_t connectd_post(el_val_t suffix, el_val_t body) { el_val_t eff = ({ el_val_t _if_result_19 = 0; if (str_eq(body, EL_STR(""))) { _if_result_19 = (EL_STR("{}")); } else { _if_result_19 = (body); } _if_result_19; }); 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)); if (str_eq(out, EL_STR(""))) { return EL_STR("{\"ok\":false,\"error\":\"connector bridge unreachable (neuron-connectd on :7771)\"}"); } return out; return 0; } el_val_t handle_connectors(el_val_t method, el_val_t clean, el_val_t body) { if (str_eq(method, EL_STR("GET"))) { return connectd_get(EL_STR("/mcp/servers")); } if (str_eq(clean, EL_STR("/api/connectors/add"))) { return connectd_post(EL_STR("/mcp/servers/add"), body); } if (str_eq(clean, EL_STR("/api/connectors/toggle"))) { return connectd_post(EL_STR("/mcp/servers/toggle"), body); } if (str_eq(clean, EL_STR("/api/connectors/auto-approve"))) { return connectd_post(EL_STR("/mcp/servers/auto-approve"), body); } if (str_eq(clean, EL_STR("/api/connectors/remove"))) { return connectd_post(EL_STR("/mcp/servers/remove"), body); } if (str_eq(clean, EL_STR("/api/connectors/secret"))) { return connectd_post(EL_STR("/mcp/servers/secret"), body); } if (str_eq(clean, EL_STR("/api/connectors/oauth/start"))) { return connectd_post(EL_STR("/mcp/oauth/start"), body); } return EL_STR("{\"ok\":false,\"error\":\"unknown connectors route\"}"); return 0; } el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { el_val_t clean = strip_query(path); el_val_t ip = env(EL_STR("REMOTE_ADDR")); if (!str_eq(ip, EL_STR(""))) { el_val_t rl_result = rate_limit_check(ip, clean); if (!str_eq(rl_result, EL_STR(""))) { return rl_result; } } if (str_eq(method, EL_STR("POST")) && str_eq(clean, EL_STR("/dharma/recv"))) { return handle_dharma_recv(body); } if (str_eq(method, EL_STR("GET"))) { if (str_eq(clean, EL_STR("/health"))) { return route_health(); } if (str_eq(clean, EL_STR("/lineage"))) { return route_lineage(); } if (str_eq(clean, EL_STR("/api/graph")) || str_eq(clean, EL_STR("/api/graph/nodes"))) { return engram_scan_nodes_json(9999, 0); } if (str_eq(clean, EL_STR("/api/graph/edges"))) { el_val_t snap_path = el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/engram/snapshot.json")); engram_save(snap_path); el_val_t snap = fs_read(snap_path); el_val_t edges_raw = json_get_raw(snap, EL_STR("edges")); return ({ el_val_t _if_result_20 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_20 = (EL_STR("[]")); } else { _if_result_20 = (edges_raw); } _if_result_20; }); } 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_21 = 0; if (str_eq(raw_msg, EL_STR(""))) { _if_result_21 = (body); } else { _if_result_21 = (raw_msg); } _if_result_21; }); 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 reply = ({ el_val_t _if_result_22 = 0; if (agentic_flag) { _if_result_22 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(eff_msg); _if_result_22 = (screened_reply); } _if_result_22; }); auto_persist(body, reply); return reply; } if (str_eq(clean, EL_STR("/api/conversations"))) { return handle_conversations(method); } if (str_eq(clean, EL_STR("/api/config"))) { return handle_config(method, body); } if (str_starts_with(clean, EL_STR("/api/tools/"))) { return handle_tool(clean, method, body); } if (str_starts_with(clean, EL_STR("/api/dharma"))) { return handle_dharma(clean, method, body); } if (str_starts_with(clean, EL_STR("/api/nlg"))) { return handle_nlg(clean, method, body); } if (str_starts_with(clean, EL_STR("/api/memories"))) { return axon_get(clean); } if (str_starts_with(clean, EL_STR("/api/knowledge"))) { return axon_get(clean); } if (str_starts_with(clean, EL_STR("/api/backlog"))) { return axon_get(clean); } if (str_starts_with(clean, EL_STR("/api/artifacts"))) { return axon_get(clean); } if (str_starts_with(clean, EL_STR("/api/projects"))) { return axon_get(clean); } if (str_starts_with(clean, EL_STR("/api/imprints"))) { return axon_get(clean); } if (str_eq(clean, EL_STR("/"))) { return render_studio(); } if (str_eq(clean, EL_STR("/api/neuron/session/begin"))) { return handle_api_begin_session(EL_STR("")); } if (str_eq(clean, EL_STR("/api/neuron/ctx"))) { return handle_api_compile_ctx(EL_STR("")); } if (str_eq(clean, EL_STR("/api/safety-contact"))) { return handle_safety_contact_get(); } if (str_starts_with(clean, EL_STR("/api/neuron/knowledge/search"))) { return handle_api_search_knowledge(method, path, body); } if (str_eq(clean, EL_STR("/api/neuron/knowledge"))) { return handle_api_browse_knowledge(path, body); } if (str_starts_with(clean, EL_STR("/api/neuron/processes"))) { return handle_api_browse_processes(method, path, body); } if (str_starts_with(clean, EL_STR("/api/neuron/state-events"))) { return handle_api_list_state_events(method, path, body); } if (str_starts_with(clean, EL_STR("/api/neuron/config"))) { return handle_api_inspect_config(path, body); } if (str_starts_with(clean, EL_STR("/api/neuron/graph"))) { return handle_api_inspect_graph(method, path, body); } if (str_starts_with(clean, EL_STR("/api/neuron/list/"))) { el_val_t node_type = str_slice(clean, 16, str_len(clean)); return handle_api_list_typed(node_type, path, body); } if (str_starts_with(clean, EL_STR("/api/neuron/recall"))) { return handle_api_recall(method, path, body); } if (str_starts_with(clean, EL_STR("/api/connectors"))) { return handle_connectors(method, clean, body); } if (str_eq(clean, EL_STR("/api/sessions"))) { return session_list(); } 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_23 = 0; if ((gs_slash < 0)) { _if_result_23 = (gs_after); } else { _if_result_23 = (str_slice(gs_after, 0, gs_slash)); } _if_result_23; }); if (!str_eq(gs_id, EL_STR(""))) { return session_get(gs_id); } } return err_404(clean); } if (str_eq(method, EL_STR("POST"))) { if (str_eq(clean, EL_STR("/api/sessions"))) { return session_create(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_24 = 0; if ((slash < 0)) { _if_result_24 = (after); } else { _if_result_24 = (str_slice(after, 0, slash)); } _if_result_24; }); 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_25 = 0; if ((sess_slash < 0)) { _if_result_25 = (sess_after); } else { _if_result_25 = (str_slice(sess_after, 0, sess_slash)); } _if_result_25; }); el_val_t sess_sub = ({ el_val_t _if_result_26 = 0; if ((sess_slash < 0)) { _if_result_26 = (EL_STR("")); } else { _if_result_26 = (str_slice(sess_after, (sess_slash + 1), str_len(sess_after))); } _if_result_26; }); if (!str_eq(sess_id, EL_STR("")) && str_eq(sess_sub, EL_STR("approve"))) { return handle_session_approve(sess_id, body); } } if (str_eq(clean, EL_STR("/imprint/contextual"))) { return route_imprint_contextual(body); } if (str_eq(clean, EL_STR("/imprint/user"))) { return route_imprint_user(body); } if (str_eq(clean, EL_STR("/synthesize"))) { return route_synthesize(body); } if (str_eq(clean, EL_STR("/api/elp/chat"))) { return handle_elp_chat(body); } if (str_eq(clean, EL_STR("/api/chat"))) { el_val_t raw_msg = json_get(body, EL_STR("message")); if (str_eq(raw_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 reply = ({ el_val_t _if_result_27 = 0; if (agentic_flag) { _if_result_27 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_27 = (screened_reply); } _if_result_27; }); auto_persist(body, reply); return reply; } if (str_eq(clean, EL_STR("/api/see"))) { return handle_see(body); } if (str_eq(clean, EL_STR("/api/conversations"))) { return handle_conversations(method); } if (str_eq(clean, EL_STR("/api/config"))) { return handle_config(method, body); } if (str_starts_with(clean, EL_STR("/api/tools/"))) { return handle_tool(clean, method, body); } if (str_starts_with(clean, EL_STR("/api/dharma"))) { return handle_dharma(clean, method, body); } if (str_starts_with(clean, EL_STR("/api/nlg"))) { return handle_nlg(clean, method, body); } if (str_starts_with(clean, EL_STR("/api/memories"))) { return axon_post(clean, body); } if (str_starts_with(clean, EL_STR("/api/knowledge"))) { return axon_post(clean, body); } if (str_starts_with(clean, EL_STR("/api/backlog"))) { return axon_post(clean, body); } if (str_starts_with(clean, EL_STR("/api/artifacts"))) { return axon_post(clean, body); } if (str_starts_with(clean, EL_STR("/api/projects"))) { return axon_post(clean, body); } if (str_starts_with(clean, EL_STR("/api/imprints"))) { return axon_post(clean, body); } if (str_eq(clean, EL_STR("/api/neuron/session/begin"))) { return handle_api_begin_session(body); } if (str_eq(clean, EL_STR("/api/neuron/ctx"))) { return handle_api_compile_ctx(body); } if (str_eq(clean, EL_STR("/api/neuron/knowledge/search"))) { return handle_api_search_knowledge(method, path, body); } if (str_eq(clean, EL_STR("/api/neuron/knowledge/capture"))) { return handle_api_capture_knowledge(body); } if (str_eq(clean, EL_STR("/api/neuron/knowledge/evolve"))) { return handle_api_evolve_knowledge(body); } if (str_eq(clean, EL_STR("/api/neuron/knowledge/promote"))) { return handle_api_promote_knowledge(body); } if (str_eq(clean, EL_STR("/api/neuron/processes"))) { return handle_api_browse_processes(method, path, body); } if (str_eq(clean, EL_STR("/api/neuron/processes/define"))) { return handle_api_define_process(body); } if (str_eq(clean, EL_STR("/api/neuron/state-events"))) { return handle_api_log_state_event(body); } if (str_eq(clean, EL_STR("/api/neuron/config"))) { return handle_api_inspect_config(path, body); } if (str_eq(clean, EL_STR("/api/neuron/config/tune"))) { return handle_api_tune_config(body); } if (str_eq(clean, EL_STR("/api/neuron/graph"))) { return handle_api_inspect_graph(method, path, body); } if (str_eq(clean, EL_STR("/api/neuron/graph/link"))) { return handle_api_link_entities(body); } if (str_eq(clean, EL_STR("/api/neuron/memory"))) { return handle_api_remember(body); } if (str_eq(clean, EL_STR("/api/safety-contact"))) { return handle_safety_contact_post(body); } if (str_eq(clean, EL_STR("/api/neuron/node/create"))) { return handle_api_node_create(body); } if (str_eq(clean, EL_STR("/api/neuron/node/update"))) { return handle_api_node_update(body); } if (str_eq(clean, EL_STR("/api/neuron/node/delete"))) { return handle_api_node_delete(body); } if (str_eq(clean, EL_STR("/api/neuron/memory/evolve"))) { return handle_api_evolve_memory(body); } if (str_eq(clean, EL_STR("/api/neuron/memory/forget"))) { return handle_api_forget(body); } if (str_eq(clean, EL_STR("/api/neuron/memory/delete"))) { return handle_api_memory_delete(body); } if (str_eq(clean, EL_STR("/api/neuron/memory/update"))) { return handle_api_memory_update(body); } if (str_eq(clean, EL_STR("/api/neuron/recall"))) { return handle_api_recall(method, path, body); } if (str_eq(clean, EL_STR("/api/neuron/consolidate"))) { return handle_api_consolidate(body); } if (str_eq(clean, EL_STR("/api/neuron/cultivate"))) { return handle_api_cultivate(body); } if (str_starts_with(clean, EL_STR("/api/connectors"))) { return handle_connectors(method, clean, body); } return err_404(clean); } if (str_eq(method, EL_STR("DELETE"))) { 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_28 = 0; if ((del_slash < 0)) { _if_result_28 = (del_after); } else { _if_result_28 = (str_slice(del_after, 0, del_slash)); } _if_result_28; }); if (!str_eq(del_id, EL_STR(""))) { return session_delete(del_id); } } return err_404(clean); } if (str_eq(method, EL_STR("PATCH"))) { 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_29 = 0; if ((patch_slash < 0)) { _if_result_29 = (patch_after); } else { _if_result_29 = (str_slice(patch_after, 0, patch_slash)); } _if_result_29; }); if (!str_eq(patch_id, EL_STR(""))) { return session_update_patch(patch_id, body); } } return err_404(clean); } return err_405(method, clean); return 0; }