diff --git a/dist/chat.c b/dist/chat.c index 6efcb26..51592b1 100644 --- a/dist/chat.c +++ b/dist/chat.c @@ -679,7 +679,8 @@ el_val_t handle_chat(el_val_t body) { el_val_t thread_snip = ({ el_val_t _if_result_112 = 0; if ((str_len(last_content) > 250)) { _if_result_112 = (str_slice(last_content, 0, 250)); } else { _if_result_112 = (last_content); } _if_result_112; }); el_val_t activation_seed = ({ el_val_t _if_result_113 = 0; if (!str_eq(thread_snip, EL_STR(""))) { _if_result_113 = (el_str_concat(el_str_concat(thread_snip, EL_STR(" ")), message)); } else { _if_result_113 = (message); } _if_result_113; }); el_val_t affective_prefix = EL_NULL; - el_val_t aff_now_ts = time_now(); + EL_NULL; + Int = time_now(); el_val_t aff_cutoff = (aff_now_ts - 259200); el_val_t boot_aff = state_get(EL_STR("soul_affective_context")); el_val_t has_boot_aff = !str_eq(boot_aff, EL_STR("")); @@ -1067,17 +1068,21 @@ el_val_t handle_chat_agentic(el_val_t body) { el_val_t tools_json = agentic_tools_all(); el_val_t safe_msg = json_safe(message); el_val_t safe_sys = json_safe(system); - el_val_t prior_messages = ({ el_val_t _if_result_164 = 0; if ((agentic_hist_len > 0)) { el_val_t inner = str_slice(agentic_hist, 1, (str_len(agentic_hist) - 1)); _if_result_164 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",{\"role\":\"user\",\"content\":\"")), safe_msg), EL_STR("\"}]"))); } else { _if_result_164 = (el_str_concat(el_str_concat(EL_STR("[{\"role\":\"user\",\"content\":\""), safe_msg), EL_STR("\"}]"))); } _if_result_164; }); + 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_164 = 0; if (str_eq(img_mt_raw, EL_STR(""))) { _if_result_164 = (EL_STR("image/png")); } else { _if_result_164 = (img_mt_raw); } _if_result_164; }); + el_val_t cur_user_content = ({ el_val_t _if_result_165 = 0; if (str_eq(img_b64, EL_STR(""))) { _if_result_165 = (el_str_concat(el_str_concat(EL_STR("\""), safe_msg), EL_STR("\""))); } else { _if_result_165 = (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_165; }); + el_val_t prior_messages = ({ el_val_t _if_result_166 = 0; if ((agentic_hist_len > 0)) { el_val_t inner = str_slice(agentic_hist, 1, (str_len(agentic_hist) - 1)); _if_result_166 = (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_166 = (el_str_concat(el_str_concat(EL_STR("[{\"role\":\"user\",\"content\":"), cur_user_content), EL_STR("}]"))); } _if_result_166; }); 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_165 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_165 = (next_bridge_id()); } else { _if_result_165 = (req_session); } _if_result_165; }); + el_val_t session_id = ({ el_val_t _if_result_167 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_167 = (next_bridge_id()); } else { _if_result_167 = (req_session); } _if_result_167; }); el_val_t result = agentic_loop(session_id, model, safe_sys, tools_json, messages, h, EL_STR("")); el_val_t reply_text = json_get(result, EL_STR("reply")); - el_val_t discard_hist = ({ el_val_t _if_result_166 = 0; if (!str_eq(reply_text, EL_STR(""))) { el_val_t updated = hist_append(agentic_hist, EL_STR("user"), message); el_val_t updated2 = hist_append(updated, EL_STR("assistant"), reply_text); el_val_t trimmed = ({ el_val_t _if_result_167 = 0; if ((json_array_len(updated2) > 40)) { _if_result_167 = (hist_trim(updated2)); } else { _if_result_167 = (updated2); } _if_result_167; }); (void)(state_set(hist_key, trimmed)); (void)(({ el_val_t _if_result_168 = 0; if (str_eq(hist_key, EL_STR("conv_history"))) { _if_result_168 = (conv_history_persist(trimmed)); } else { _if_result_168 = (({ el_val_t _if_result_169 = 0; if ((!str_eq(trimmed, EL_STR("")) && !str_eq(trimmed, EL_STR("[]")))) { el_val_t sess_hist_label = el_str_concat(EL_STR("conv:history:"), req_session); el_val_t sess_hist_tags = EL_STR("[\"session-history\",\"persistent\"]"); el_val_t sess_hist_id = engram_node_full(trimmed, EL_STR("Conversation"), sess_hist_label, el_from_float(0.6), el_from_float(0.7), el_from_float(0.8), EL_STR("Episodic"), sess_hist_tags); _if_result_169 = (({ el_val_t _if_result_170 = 0; if (str_eq(sess_hist_id, EL_STR(""))) { _if_result_170 = (println(el_str_concat(EL_STR("[chat] agentic: named session history persist failed for session="), req_session))); } else { } _if_result_170; })); } else { } _if_result_169; })); } _if_result_168; })); _if_result_166 = (1); } else { _if_result_166 = (0); } _if_result_166; }); + el_val_t discard_hist = ({ el_val_t _if_result_168 = 0; if (!str_eq(reply_text, EL_STR(""))) { el_val_t updated = hist_append(agentic_hist, EL_STR("user"), message); el_val_t updated2 = hist_append(updated, EL_STR("assistant"), reply_text); el_val_t trimmed = ({ el_val_t _if_result_169 = 0; if ((json_array_len(updated2) > 40)) { _if_result_169 = (hist_trim(updated2)); } else { _if_result_169 = (updated2); } _if_result_169; }); (void)(state_set(hist_key, trimmed)); (void)(({ el_val_t _if_result_170 = 0; if (str_eq(hist_key, EL_STR("conv_history"))) { _if_result_170 = (conv_history_persist(trimmed)); } else { _if_result_170 = (({ el_val_t _if_result_171 = 0; if ((!str_eq(trimmed, EL_STR("")) && !str_eq(trimmed, EL_STR("[]")))) { el_val_t sess_hist_label = el_str_concat(EL_STR("conv:history:"), req_session); el_val_t sess_hist_tags = EL_STR("[\"session-history\",\"persistent\"]"); el_val_t sess_hist_id = engram_node_full(trimmed, EL_STR("Conversation"), sess_hist_label, el_from_float(0.6), el_from_float(0.7), el_from_float(0.8), EL_STR("Episodic"), sess_hist_tags); _if_result_171 = (({ el_val_t _if_result_172 = 0; if (str_eq(sess_hist_id, EL_STR(""))) { _if_result_172 = (println(el_str_concat(EL_STR("[chat] agentic: named session history persist failed for session="), req_session))); } else { } _if_result_172; })); } else { } _if_result_171; })); } _if_result_170; })); _if_result_168 = (1); } else { _if_result_168 = (0); } _if_result_168; }); return result; return 0; } @@ -1102,7 +1107,7 @@ el_val_t agentic_loop(el_val_t session_id, el_val_t model, el_val_t safe_sys, el } el_val_t stop_reason = json_get(raw_resp, EL_STR("stop_reason")); el_val_t content_arr = json_get_raw(raw_resp, EL_STR("content")); - el_val_t eff_content = ({ el_val_t _if_result_171 = 0; if (str_eq(content_arr, EL_STR(""))) { _if_result_171 = (EL_STR("[]")); } else { _if_result_171 = (content_arr); } _if_result_171; }); + el_val_t eff_content = ({ el_val_t _if_result_173 = 0; if (str_eq(content_arr, EL_STR(""))) { _if_result_173 = (EL_STR("[]")); } else { _if_result_173 = (content_arr); } _if_result_173; }); el_val_t text_out = EL_STR(""); el_val_t has_tool = 0; el_val_t tool_id = EL_STR(""); @@ -1113,51 +1118,51 @@ el_val_t agentic_loop(el_val_t session_id, el_val_t model, el_val_t safe_sys, el while (ci < c_total) { el_val_t block = json_array_get(eff_content, ci); el_val_t btype = json_get(block, EL_STR("type")); - text_out = ({ el_val_t _if_result_172 = 0; if (str_eq(btype, EL_STR("text"))) { _if_result_172 = (el_str_concat(text_out, json_get(block, EL_STR("text")))); } else { _if_result_172 = (text_out); } _if_result_172; }); + text_out = ({ el_val_t _if_result_174 = 0; if (str_eq(btype, EL_STR("text"))) { _if_result_174 = (el_str_concat(text_out, json_get(block, EL_STR("text")))); } else { _if_result_174 = (text_out); } _if_result_174; }); el_val_t is_new_tool = (str_eq(btype, EL_STR("tool_use")) && !has_tool); - has_tool = ({ el_val_t _if_result_173 = 0; if (is_new_tool) { _if_result_173 = (1); } else { _if_result_173 = (has_tool); } _if_result_173; }); - tool_id = ({ el_val_t _if_result_174 = 0; if (is_new_tool) { _if_result_174 = (json_get(block, EL_STR("id"))); } else { _if_result_174 = (tool_id); } _if_result_174; }); - tool_name = ({ el_val_t _if_result_175 = 0; if (is_new_tool) { _if_result_175 = (json_get(block, EL_STR("name"))); } else { _if_result_175 = (tool_name); } _if_result_175; }); - tool_input = ({ el_val_t _if_result_176 = 0; if (is_new_tool) { _if_result_176 = (json_get_raw(block, EL_STR("input"))); } else { _if_result_176 = (tool_input); } _if_result_176; }); + has_tool = ({ el_val_t _if_result_175 = 0; if (is_new_tool) { _if_result_175 = (1); } else { _if_result_175 = (has_tool); } _if_result_175; }); + tool_id = ({ el_val_t _if_result_176 = 0; if (is_new_tool) { _if_result_176 = (json_get(block, EL_STR("id"))); } else { _if_result_176 = (tool_id); } _if_result_176; }); + tool_name = ({ el_val_t _if_result_177 = 0; if (is_new_tool) { _if_result_177 = (json_get(block, EL_STR("name"))); } else { _if_result_177 = (tool_name); } _if_result_177; }); + tool_input = ({ el_val_t _if_result_178 = 0; if (is_new_tool) { _if_result_178 = (json_get_raw(block, EL_STR("input"))); } else { _if_result_178 = (tool_input); } _if_result_178; }); ci = (ci + 1); } el_val_t is_tool_turn = (str_eq(stop_reason, EL_STR("tool_use")) && has_tool); el_val_t always_key = el_str_concat(EL_STR("always_allow_"), session_id); - el_val_t always_list = ({ el_val_t _if_result_177 = 0; if (!str_eq(session_id, EL_STR(""))) { _if_result_177 = (state_get(always_key)); } else { _if_result_177 = (EL_STR("")); } _if_result_177; }); + el_val_t always_list = ({ el_val_t _if_result_179 = 0; if (!str_eq(session_id, EL_STR(""))) { _if_result_179 = (state_get(always_key)); } else { _if_result_179 = (EL_STR("")); } _if_result_179; }); 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 needs_bridge = ((is_tool_turn && !is_builtin_tool(tool_name)) && !is_always_allowed); - el_val_t tool_result_raw = ({ el_val_t _if_result_178 = 0; if ((is_tool_turn && !needs_bridge)) { _if_result_178 = (dispatch_tool(tool_name, tool_input)); } else { _if_result_178 = (EL_STR("")); } _if_result_178; }); - el_val_t tool_result = ({ el_val_t _if_result_179 = 0; if ((str_len(tool_result_raw) > 6000)) { _if_result_179 = (el_str_concat(str_slice(tool_result_raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_179 = (tool_result_raw); } _if_result_179; }); + el_val_t tool_result_raw = ({ el_val_t _if_result_180 = 0; if ((is_tool_turn && !needs_bridge)) { _if_result_180 = (dispatch_tool(tool_name, tool_input)); } else { _if_result_180 = (EL_STR("")); } _if_result_180; }); + el_val_t tool_result = ({ el_val_t _if_result_181 = 0; if ((str_len(tool_result_raw) > 6000)) { _if_result_181 = (el_str_concat(str_slice(tool_result_raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_181 = (tool_result_raw); } _if_result_181; }); 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_180 = 0; if (has_tool) { _if_result_180 = (({ el_val_t _if_result_181 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_181 = (tool_quoted); } else { _if_result_181 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), tool_quoted)); } _if_result_181; })); } else { _if_result_180 = (tools_log); } _if_result_180; }); + tools_log = ({ el_val_t _if_result_182 = 0; if (has_tool) { _if_result_182 = (({ el_val_t _if_result_183 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_183 = (tool_quoted); } else { _if_result_183 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), tool_quoted)); } _if_result_183; })); } else { _if_result_182 = (tools_log); } _if_result_182; }); el_val_t inner = str_slice(messages, 1, (str_len(messages) - 1)); el_val_t messages_with_assistant = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",{\"role\":\"assistant\",\"content\":")), eff_content), EL_STR("}")), EL_STR("]")); el_val_t local_continue = (is_tool_turn && !needs_bridge); - messages = ({ el_val_t _if_result_182 = 0; if (local_continue) { el_val_t inner2 = str_slice(messages_with_assistant, 1, (str_len(messages_with_assistant) - 1)); _if_result_182 = (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_182 = (messages); } _if_result_182; }); - pending = ({ el_val_t _if_result_183 = 0; if (needs_bridge) { _if_result_183 = (1); } else { _if_result_183 = (pending); } _if_result_183; }); - pend_tool_id = ({ el_val_t _if_result_184 = 0; if (needs_bridge) { _if_result_184 = (tool_id); } else { _if_result_184 = (pend_tool_id); } _if_result_184; }); - pend_tool_name = ({ el_val_t _if_result_185 = 0; if (needs_bridge) { _if_result_185 = (tool_name); } else { _if_result_185 = (pend_tool_name); } _if_result_185; }); - pend_tool_input = ({ el_val_t _if_result_186 = 0; if (needs_bridge) { _if_result_186 = (tool_input); } else { _if_result_186 = (pend_tool_input); } _if_result_186; }); + messages = ({ el_val_t _if_result_184 = 0; if (local_continue) { el_val_t inner2 = str_slice(messages_with_assistant, 1, (str_len(messages_with_assistant) - 1)); _if_result_184 = (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_184 = (messages); } _if_result_184; }); + pending = ({ el_val_t _if_result_185 = 0; if (needs_bridge) { _if_result_185 = (1); } else { _if_result_185 = (pending); } _if_result_185; }); + pend_tool_id = ({ el_val_t _if_result_186 = 0; if (needs_bridge) { _if_result_186 = (tool_id); } else { _if_result_186 = (pend_tool_id); } _if_result_186; }); + pend_tool_name = ({ el_val_t _if_result_187 = 0; if (needs_bridge) { _if_result_187 = (tool_name); } else { _if_result_187 = (pend_tool_name); } _if_result_187; }); + pend_tool_input = ({ el_val_t _if_result_188 = 0; if (needs_bridge) { _if_result_188 = (tool_input); } else { _if_result_188 = (pend_tool_input); } _if_result_188; }); if (needs_bridge) { bridge_save(session_id, model, safe_sys, tools_json, messages_with_assistant, tools_log, pend_tool_id); } - final_text = ({ el_val_t _if_result_187 = 0; if (!is_tool_turn) { _if_result_187 = (text_out); } else { _if_result_187 = (final_text); } _if_result_187; }); - keep_going = ({ el_val_t _if_result_188 = 0; if (local_continue) { _if_result_188 = (keep_going); } else { _if_result_188 = (0); } _if_result_188; }); + final_text = ({ el_val_t _if_result_189 = 0; if (!is_tool_turn) { _if_result_189 = (text_out); } else { _if_result_189 = (final_text); } _if_result_189; }); + keep_going = ({ el_val_t _if_result_190 = 0; if (local_continue) { _if_result_190 = (keep_going); } else { _if_result_190 = (0); } _if_result_190; }); iteration = (iteration + 1); } if (pending) { - el_val_t safe_in = ({ el_val_t _if_result_189 = 0; if (str_eq(pend_tool_input, EL_STR(""))) { _if_result_189 = (EL_STR("{}")); } else { _if_result_189 = (pend_tool_input); } _if_result_189; }); - el_val_t tools_arr = ({ el_val_t _if_result_190 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_190 = (EL_STR("[]")); } else { _if_result_190 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_190; }); + el_val_t safe_in = ({ el_val_t _if_result_191 = 0; if (str_eq(pend_tool_input, EL_STR(""))) { _if_result_191 = (EL_STR("{}")); } else { _if_result_191 = (pend_tool_input); } _if_result_191; }); + el_val_t tools_arr = ({ el_val_t _if_result_192 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_192 = (EL_STR("[]")); } else { _if_result_192 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_192; }); 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("{\"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(",\"model\":\"")), model), EL_STR("\"")), EL_STR(",\"agentic\":true")), EL_STR(",\"tools_used\":")), tools_arr), EL_STR("}")); } if (str_eq(final_text, EL_STR(""))) { el_val_t hit_cap = (iteration >= 8); - el_val_t err_msg = ({ el_val_t _if_result_191 = 0; if (hit_cap) { _if_result_191 = (EL_STR("agentic loop hit the 8-iteration cap without producing a final reply - task may be too complex or a tool call is looping")); } else { _if_result_191 = (EL_STR("no response")); } _if_result_191; }); + el_val_t err_msg = ({ el_val_t _if_result_193 = 0; if (hit_cap) { _if_result_193 = (EL_STR("agentic loop hit the 8-iteration cap without producing a final reply - task may be too complex or a tool call is looping")); } else { _if_result_193 = (EL_STR("no response")); } _if_result_193; }); 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_192 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_192 = (EL_STR("[]")); } else { _if_result_192 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_192; }); + el_val_t tools_arr = ({ el_val_t _if_result_194 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_194 = (EL_STR("[]")); } else { _if_result_194 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_194; }); return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"reply\":\""), safe_text), EL_STR("\",\"model\":\"")), model), EL_STR("\",\"agentic\":true,\"tools_used\":")), tools_arr), EL_STR(",\"iterations\":")), int_to_str(iteration)), EL_STR("}")); return 0; } @@ -1180,17 +1185,17 @@ 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_193 = 0; if (str_eq(messages, EL_STR(""))) { _if_result_193 = (json_get(blob, EL_STR("messages"))); } else { _if_result_193 = (messages); } _if_result_193; }); + messages = ({ el_val_t _if_result_195 = 0; if (str_eq(messages, EL_STR(""))) { _if_result_195 = (json_get(blob, EL_STR("messages"))); } else { _if_result_195 = (messages); } _if_result_195; }); el_val_t tools_json = json_get_raw(blob, EL_STR("tools_raw")); - tools_json = ({ el_val_t _if_result_194 = 0; if (str_eq(tools_json, EL_STR(""))) { _if_result_194 = (json_get(blob, EL_STR("tools_json"))); } else { _if_result_194 = (tools_json); } _if_result_194; }); + tools_json = ({ el_val_t _if_result_196 = 0; if (str_eq(tools_json, EL_STR(""))) { _if_result_196 = (json_get(blob, EL_STR("tools_json"))); } else { _if_result_196 = (tools_json); } _if_result_196; }); if (str_eq(messages, EL_STR("")) || str_eq(tools_json, EL_STR(""))) { return EL_STR("{\"error\":\"corrupt bridge state\",\"reply\":\"\"}"); } el_val_t tools_log = json_get(blob, EL_STR("tools_log")); el_val_t saved_use_id = json_get(blob, EL_STR("tool_use_id")); - el_val_t use_id = ({ el_val_t _if_result_195 = 0; if (str_eq(tool_use_id, EL_STR(""))) { _if_result_195 = (saved_use_id); } else { _if_result_195 = (tool_use_id); } _if_result_195; }); - el_val_t eff_use_id = ({ el_val_t _if_result_196 = 0; if (str_eq(use_id, saved_use_id)) { _if_result_196 = (use_id); } else { _if_result_196 = (saved_use_id); } _if_result_196; }); - el_val_t trimmed = ({ el_val_t _if_result_197 = 0; if ((str_len(content) > 6000)) { _if_result_197 = (el_str_concat(str_slice(content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_197 = (content); } _if_result_197; }); + el_val_t use_id = ({ el_val_t _if_result_197 = 0; if (str_eq(tool_use_id, EL_STR(""))) { _if_result_197 = (saved_use_id); } else { _if_result_197 = (tool_use_id); } _if_result_197; }); + el_val_t eff_use_id = ({ el_val_t _if_result_198 = 0; if (str_eq(use_id, saved_use_id)) { _if_result_198 = (use_id); } else { _if_result_198 = (saved_use_id); } _if_result_198; }); + el_val_t trimmed = ({ el_val_t _if_result_199 = 0; if ((str_len(content) > 6000)) { _if_result_199 = (el_str_concat(str_slice(content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_199 = (content); } _if_result_199; }); 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)); @@ -1226,12 +1231,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_198 = 0; if (str_eq(message, EL_STR(""))) { _if_result_198 = (transcript); } else { _if_result_198 = (message); } _if_result_198; }); + el_val_t eff_message = ({ el_val_t _if_result_200 = 0; if (str_eq(message, EL_STR(""))) { _if_result_200 = (transcript); } else { _if_result_200 = (message); } _if_result_200; }); 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_199 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_199 = (chat_default_model()); } else { _if_result_199 = (req_model); } _if_result_199; }); + el_val_t model = ({ el_val_t _if_result_201 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_201 = (chat_default_model()); } else { _if_result_201 = (req_model); } _if_result_201; }); system_prompt = safety_augment_system(system_prompt, eff_message); el_val_t raw_response = llm_call_system(model, system_prompt, eff_message); el_val_t is_error = ((str_starts_with(raw_response, EL_STR("{\"error\"")) || str_starts_with(raw_response, EL_STR("{\"type\":\"error\""))) || str_contains(raw_response, EL_STR("authentication_error"))); @@ -1254,7 +1259,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_200 = 0; if (str_eq(engram_ctx, EL_STR(""))) { _if_result_200 = (identity); } else { _if_result_200 = (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_200; }); + el_val_t system_prompt = ({ el_val_t _if_result_202 = 0; if (str_eq(engram_ctx, EL_STR(""))) { _if_result_202 = (identity); } else { _if_result_202 = (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_202; }); system_prompt = safety_augment_system(system_prompt, transcript); el_val_t raw_response = llm_call_system(model, system_prompt, transcript); el_val_t is_error = ((str_starts_with(raw_response, EL_STR("{\"error\"")) || str_starts_with(raw_response, EL_STR("{\"type\":\"error\""))) || str_contains(raw_response, EL_STR("authentication_error"))); @@ -1294,7 +1299,7 @@ el_val_t handle_dharma_room_turn_agentic(el_val_t body) { map_set(h, EL_STR("x-api-key"), api_key); map_set(h, EL_STR("anthropic-version"), EL_STR("2023-06-01")); map_set(h, EL_STR("content-type"), EL_STR("application/json")); - el_val_t session_id = ({ el_val_t _if_result_201 = 0; if (str_eq(room_id, EL_STR(""))) { _if_result_201 = (el_str_concat(EL_STR("dharma:"), next_bridge_id())); } else { _if_result_201 = (el_str_concat(EL_STR("dharma:"), room_id)); } _if_result_201; }); + el_val_t session_id = ({ el_val_t _if_result_203 = 0; if (str_eq(room_id, EL_STR(""))) { _if_result_203 = (el_str_concat(EL_STR("dharma:"), next_bridge_id())); } else { _if_result_203 = (el_str_concat(EL_STR("dharma:"), room_id)); } _if_result_203; }); el_val_t loop_result = agentic_loop(session_id, model, safe_sys, tools_json, messages, h, EL_STR("")); el_val_t result_error = json_get(loop_result, EL_STR("error")); if (!str_eq(result_error, EL_STR(""))) { @@ -1309,7 +1314,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_202 = 0; if (str_eq(tools_arr, EL_STR(""))) { _if_result_202 = (EL_STR("[]")); } else { _if_result_202 = (tools_arr); } _if_result_202; }); + el_val_t eff_tools = ({ el_val_t _if_result_204 = 0; if (str_eq(tools_arr, EL_STR(""))) { _if_result_204 = (EL_STR("[]")); } else { _if_result_204 = (tools_arr); } _if_result_204; }); 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; @@ -1320,7 +1325,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_203 = 0; if ((str_len(safe_text) > 800)) { _if_result_203 = (str_slice(safe_text, 0, 800)); } else { _if_result_203 = (safe_text); } _if_result_203; }); + el_val_t trimmed = ({ el_val_t _if_result_205 = 0; if ((str_len(safe_text) > 800)) { _if_result_205 = (str_slice(safe_text, 0, 800)); } else { _if_result_205 = (safe_text); } _if_result_205; }); 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); @@ -1351,7 +1356,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_204 = 0; if ((str_len(safe_text) > 800)) { _if_result_204 = (str_slice(safe_text, 0, 800)); } else { _if_result_204 = (safe_text); } _if_result_204; }); + el_val_t trimmed = ({ el_val_t _if_result_206 = 0; if ((str_len(safe_text) > 800)) { _if_result_206 = (str_slice(safe_text, 0, 800)); } else { _if_result_206 = (safe_text); } _if_result_206; }); 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); @@ -1385,8 +1390,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_205 = 0; if ((str_len(msg) > 80)) { _if_result_205 = (str_slice(msg, 0, 80)); } else { _if_result_205 = (msg); } _if_result_205; }); - snippets = ({ el_val_t _if_result_206 = 0; if (str_eq(snippets, EL_STR(""))) { _if_result_206 = (snip); } else { _if_result_206 = (el_str_concat(el_str_concat(snippets, EL_STR("; ")), snip)); } _if_result_206; }); + el_val_t snip = ({ el_val_t _if_result_207 = 0; if ((str_len(msg) > 80)) { _if_result_207 = (str_slice(msg, 0, 80)); } else { _if_result_207 = (msg); } _if_result_207; }); + snippets = ({ el_val_t _if_result_208 = 0; if (str_eq(snippets, EL_STR(""))) { _if_result_208 = (snip); } else { _if_result_208 = (el_str_concat(el_str_concat(snippets, EL_STR("; ")), snip)); } _if_result_208; }); count = (count + 1); } i = (i + 1); @@ -1401,7 +1406,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_207 = 0; if (str_eq(reply, EL_STR(""))) { _if_result_207 = (json_get(resp, EL_STR("reply"))); } else { _if_result_207 = (reply); } _if_result_207; }); + el_val_t reply2 = ({ el_val_t _if_result_209 = 0; if (str_eq(reply, EL_STR(""))) { _if_result_209 = (json_get(resp, EL_STR("reply"))); } else { _if_result_209 = (reply); } _if_result_209; }); if (str_eq(message, EL_STR(""))) { return EL_STR(""); } @@ -1413,42 +1418,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_208 = 0; if (is_bell) { _if_result_208 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"bell:"), bell_level), EL_STR("\",\"affective\"]"))); } else { _if_result_208 = (({ el_val_t _if_result_209 = 0; if (is_positive) { _if_result_209 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"joy:"), positive_level), EL_STR("\",\"affective\"]"))); } else { _if_result_209 = (EL_STR("[\"Conversation\",\"chat\",\"timestamped\"]")); } _if_result_209; })); } _if_result_208; }); + el_val_t tags = ({ el_val_t _if_result_210 = 0; if (is_bell) { _if_result_210 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"bell:"), bell_level), EL_STR("\",\"affective\"]"))); } else { _if_result_210 = (({ el_val_t _if_result_211 = 0; if (is_positive) { _if_result_211 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"joy:"), positive_level), EL_STR("\",\"affective\"]"))); } else { _if_result_211 = (EL_STR("[\"Conversation\",\"chat\",\"timestamped\"]")); } _if_result_211; })); } _if_result_210; }); el_val_t content = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"q\":\""), safe_msg), EL_STR("\"")), EL_STR(",\"a\":\"")), safe_reply), EL_STR("\"")), EL_STR(",\"created_at\":")), ts_str), EL_STR(",\"source\":\"chat\"")), EL_STR(",\"bell\":\"")), bell_level), EL_STR("\"")), EL_STR(",\"label\":\"chat:")), ts_str), EL_STR("\"}")); el_val_t conv_node_id = engram_node_full(content, EL_STR("Conversation"), el_str_concat(EL_STR("chat:"), ts_str), el_from_float(0.6), el_from_float(0.7), el_from_float(0.8), EL_STR("Episodic"), tags); if (str_eq(conv_node_id, EL_STR(""))) { println(el_str_concat(el_str_concat(EL_STR("[chat] auto_persist: engram_node_full returned empty \xe2\x80\x94 conversation node lost (ts="), ts_str), EL_STR(")"))); } if (is_bell) { - el_val_t summary = ({ el_val_t _if_result_210 = 0; if ((str_len(message) > 120)) { _if_result_210 = (str_slice(message, 0, 120)); } else { _if_result_210 = (message); } _if_result_210; }); + el_val_t summary = ({ el_val_t _if_result_212 = 0; if ((str_len(message) > 120)) { _if_result_212 = (str_slice(message, 0, 120)); } else { _if_result_212 = (message); } _if_result_212; }); 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_211 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_211 = (el_from_float(0.98)); } else { _if_result_211 = (el_from_float(0.88)); } _if_result_211; }); - el_val_t sal_b = ({ el_val_t _if_result_212 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_212 = (el_from_float(0.98)); } else { _if_result_212 = (el_from_float(0.88)); } _if_result_212; }); - el_val_t sal_c = ({ el_val_t _if_result_213 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_213 = (el_from_float(1.0)); } else { _if_result_213 = (el_from_float(0.95)); } _if_result_213; }); + el_val_t sal_a = ({ el_val_t _if_result_213 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_213 = (el_from_float(0.98)); } else { _if_result_213 = (el_from_float(0.88)); } _if_result_213; }); + el_val_t sal_b = ({ el_val_t _if_result_214 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_214 = (el_from_float(0.98)); } else { _if_result_214 = (el_from_float(0.88)); } _if_result_214; }); + el_val_t sal_c = ({ el_val_t _if_result_215 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_215 = (el_from_float(1.0)); } else { _if_result_215 = (el_from_float(0.95)); } _if_result_215; }); el_val_t bell_tags = el_str_concat(el_str_concat(EL_STR("[\"safety\",\"bell\",\"bell:"), bell_level), EL_STR("\",\"affective\",\"BellEvent\"]")); el_val_t bell_ts_str = int_to_str(time_now()); el_val_t bell_label = el_str_concat(el_str_concat(el_str_concat(EL_STR("bell:"), bell_level), EL_STR(":")), bell_ts_str); el_val_t bell_node_id = engram_node_full(bell_content, EL_STR("BellEvent"), bell_label, sal_a, sal_b, sal_c, EL_STR("Episodic"), bell_tags); el_val_t sess_id = json_get(req, EL_STR("session_id")); - el_val_t bell_key = ({ el_val_t _if_result_214 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_214 = (EL_STR("session_bell_count")); } else { _if_result_214 = (el_str_concat(EL_STR("session_bell_count:"), sess_id)); } _if_result_214; }); + el_val_t bell_key = ({ el_val_t _if_result_216 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_216 = (EL_STR("session_bell_count")); } else { _if_result_216 = (el_str_concat(EL_STR("session_bell_count:"), sess_id)); } _if_result_216; }); el_val_t prior_count = state_get(bell_key); - el_val_t prior_n = ({ el_val_t _if_result_215 = 0; if (str_eq(prior_count, EL_STR(""))) { _if_result_215 = (0); } else { _if_result_215 = (str_to_int(prior_count)); } _if_result_215; }); + el_val_t prior_n = ({ el_val_t _if_result_217 = 0; if (str_eq(prior_count, EL_STR(""))) { _if_result_217 = (0); } else { _if_result_217 = (str_to_int(prior_count)); } _if_result_217; }); state_set(bell_key, int_to_str((prior_n + 1))); - el_val_t level_key = ({ el_val_t _if_result_216 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_216 = (EL_STR("session_bell_level")); } else { _if_result_216 = (el_str_concat(EL_STR("session_bell_level:"), sess_id)); } _if_result_216; }); + el_val_t level_key = ({ el_val_t _if_result_218 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_218 = (EL_STR("session_bell_level")); } else { _if_result_218 = (el_str_concat(EL_STR("session_bell_level:"), sess_id)); } _if_result_218; }); el_val_t prior_level = state_get(level_key); - el_val_t new_level = ({ el_val_t _if_result_217 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_217 = (EL_STR("hard")); } else { _if_result_217 = (({ el_val_t _if_result_218 = 0; if (str_eq(prior_level, EL_STR("hard"))) { _if_result_218 = (EL_STR("hard")); } else { _if_result_218 = (EL_STR("soft")); } _if_result_218; })); } _if_result_217; }); + el_val_t new_level = ({ el_val_t _if_result_219 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_219 = (EL_STR("hard")); } else { _if_result_219 = (({ el_val_t _if_result_220 = 0; if (str_eq(prior_level, EL_STR("hard"))) { _if_result_220 = (EL_STR("hard")); } else { _if_result_220 = (EL_STR("soft")); } _if_result_220; })); } _if_result_219; }); state_set(level_key, new_level); - el_val_t signal_key = ({ el_val_t _if_result_219 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_219 = (EL_STR("session_bell_signal")); } else { _if_result_219 = (el_str_concat(EL_STR("session_bell_signal:"), sess_id)); } _if_result_219; }); + el_val_t signal_key = ({ el_val_t _if_result_221 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_221 = (EL_STR("session_bell_signal")); } else { _if_result_221 = (el_str_concat(EL_STR("session_bell_signal:"), sess_id)); } _if_result_221; }); state_set(signal_key, safe_summary); } if (is_positive) { - el_val_t pos_summary = ({ el_val_t _if_result_220 = 0; if ((str_len(message) > 120)) { _if_result_220 = (str_slice(message, 0, 120)); } else { _if_result_220 = (message); } _if_result_220; }); + el_val_t pos_summary = ({ el_val_t _if_result_222 = 0; if ((str_len(message) > 120)) { _if_result_222 = (str_slice(message, 0, 120)); } else { _if_result_222 = (message); } _if_result_222; }); 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_221 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_221 = (el_from_float(0.88)); } else { _if_result_221 = (el_from_float(0.75)); } _if_result_221; }); - el_val_t pos_sal_b = ({ el_val_t _if_result_222 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_222 = (el_from_float(0.88)); } else { _if_result_222 = (el_from_float(0.75)); } _if_result_222; }); - el_val_t pos_sal_c = ({ el_val_t _if_result_223 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_223 = (el_from_float(0.95)); } else { _if_result_223 = (el_from_float(0.85)); } _if_result_223; }); + el_val_t pos_sal_a = ({ el_val_t _if_result_223 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_223 = (el_from_float(0.88)); } else { _if_result_223 = (el_from_float(0.75)); } _if_result_223; }); + el_val_t pos_sal_b = ({ el_val_t _if_result_224 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_224 = (el_from_float(0.88)); } else { _if_result_224 = (el_from_float(0.75)); } _if_result_224; }); + el_val_t pos_sal_c = ({ el_val_t _if_result_225 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_225 = (el_from_float(0.95)); } else { _if_result_225 = (el_from_float(0.85)); } _if_result_225; }); 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); @@ -1485,11 +1490,11 @@ int main(int _argc, char** _argv) { el_val_t ctx = engram_compile(activation_seed); el_val_t system = el_str_concat(affective_prefix, build_system_prompt(ctx, 1)); el_val_t seen_ids = state_get(EL_STR("engram_compile_seen_ids")); - el_val_t session_preload = ({ el_val_t _if_result_224 = 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 = 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 = ({ el_val_t _if_result_225 = 0; if (work_ok_typed) { _if_result_225 = (work_nodes_typed); } else { _if_result_225 = (engram_search_json(EL_STR("active project task current in_progress"), 6)); } _if_result_225; }); el_val_t work_ok = (!str_eq(work_nodes, EL_STR("")) && !str_eq(work_nodes, 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_226 = 0; if (profile_ok) { el_val_t pn = json_array_len(profile_nodes); el_val_t bullets = EL_STR(""); el_val_t bullets = ({ el_val_t _if_result_227 = 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_228 = 0; if ((str_len(c0) > 120)) { _if_result_228 = (str_slice(c0, 0, 120)); } else { _if_result_228 = (c0); } _if_result_228; }); _if_result_227 = (({ el_val_t _if_result_229 = 0; if ((id_in_seen(id0, seen_ids) || str_eq(s0, EL_STR("")))) { _if_result_229 = (bullets); } else { _if_result_229 = (el_str_concat(EL_STR("- "), s0)); } _if_result_229; })); } else { _if_result_227 = (bullets); } _if_result_227; }); el_val_t bullets = ({ el_val_t _if_result_230 = 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_231 = 0; if ((str_len(c1) > 120)) { _if_result_231 = (str_slice(c1, 0, 120)); } else { _if_result_231 = (c1); } _if_result_231; }); _if_result_230 = (({ el_val_t _if_result_232 = 0; if ((id_in_seen(id1, seen_ids) || str_eq(s1, EL_STR("")))) { _if_result_232 = (bullets); } else { _if_result_232 = (el_str_concat(el_str_concat(bullets, EL_STR("\n- ")), s1)); } _if_result_232; })); } else { _if_result_230 = (bullets); } _if_result_230; }); el_val_t bullets = ({ el_val_t _if_result_233 = 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_234 = 0; if ((str_len(c2) > 120)) { _if_result_234 = (str_slice(c2, 0, 120)); } else { _if_result_234 = (c2); } _if_result_234; }); _if_result_233 = (({ el_val_t _if_result_235 = 0; if ((id_in_seen(id2, seen_ids) || str_eq(s2, EL_STR("")))) { _if_result_235 = (bullets); } else { _if_result_235 = (el_str_concat(el_str_concat(bullets, EL_STR("\n- ")), s2)); } _if_result_235; })); } else { _if_result_233 = (bullets); } _if_result_233; }); _if_result_226 = (bullets); } else { _if_result_226 = (EL_STR("")); } _if_result_226; }); el_val_t work_bullets = ({ el_val_t _if_result_236 = 0; if (work_ok) { el_val_t wn = json_array_len(work_nodes); el_val_t wb = EL_STR(""); el_val_t wb = ({ el_val_t _if_result_237 = 0; if ((wn > 0)) { el_val_t w0 = json_array_get(work_nodes, 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_238 = 0; if ((str_len(wc0) > 120)) { _if_result_238 = (str_slice(wc0, 0, 120)); } else { _if_result_238 = (wc0); } _if_result_238; }); _if_result_237 = (({ el_val_t _if_result_239 = 0; if ((id_in_seen(wid0, seen_ids) || str_eq(ws0, EL_STR("")))) { _if_result_239 = (wb); } else { _if_result_239 = (el_str_concat(EL_STR("- "), ws0)); } _if_result_239; })); } else { _if_result_237 = (wb); } _if_result_237; }); el_val_t wb = ({ el_val_t _if_result_240 = 0; if ((wn > 1)) { el_val_t w1 = json_array_get(work_nodes, 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_241 = 0; if ((str_len(wc1) > 120)) { _if_result_241 = (str_slice(wc1, 0, 120)); } else { _if_result_241 = (wc1); } _if_result_241; }); _if_result_240 = (({ el_val_t _if_result_242 = 0; if ((id_in_seen(wid1, seen_ids) || str_eq(ws1, EL_STR("")))) { _if_result_242 = (wb); } else { _if_result_242 = (el_str_concat(el_str_concat(wb, EL_STR("\n- ")), ws1)); } _if_result_242; })); } else { _if_result_240 = (wb); } _if_result_240; }); _if_result_236 = (wb); } else { _if_result_236 = (EL_STR("")); } _if_result_236; }); el_val_t project_bullets = ({ el_val_t _if_result_243 = 0; if (project_ok) { el_val_t prn = json_array_len(project_nodes); el_val_t pb = EL_STR(""); el_val_t pb = ({ el_val_t _if_result_244 = 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_245 = 0; if ((str_len(prc0) > 120)) { _if_result_245 = (str_slice(prc0, 0, 120)); } else { _if_result_245 = (prc0); } _if_result_245; }); _if_result_244 = (({ el_val_t _if_result_246 = 0; if ((id_in_seen(prid0, seen_ids) || str_eq(ps0, EL_STR("")))) { _if_result_246 = (pb); } else { _if_result_246 = (el_str_concat(EL_STR("- "), ps0)); } _if_result_246; })); } else { _if_result_244 = (pb); } _if_result_244; }); el_val_t pb = ({ el_val_t _if_result_247 = 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_248 = 0; if ((str_len(prc1) > 120)) { _if_result_248 = (str_slice(prc1, 0, 120)); } else { _if_result_248 = (prc1); } _if_result_248; }); _if_result_247 = (({ el_val_t _if_result_249 = 0; if ((id_in_seen(prid1, seen_ids) || str_eq(ps1, EL_STR("")))) { _if_result_249 = (pb); } else { _if_result_249 = (el_str_concat(el_str_concat(pb, EL_STR("\n- ")), ps1)); } _if_result_249; })); } else { _if_result_247 = (pb); } _if_result_247; }); _if_result_243 = (pb); } else { _if_result_243 = (EL_STR("")); } _if_result_243; }); el_val_t summary_bullet = ({ el_val_t _if_result_250 = 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_251 = 0; if ((str_len(sc0) > 200)) { _if_result_251 = (str_slice(sc0, 0, 200)); } else { _if_result_251 = (sc0); } _if_result_251; }); _if_result_250 = (({ el_val_t _if_result_252 = 0; if ((id_in_seen(snid0, seen_ids) || str_eq(ss0, EL_STR("")))) { _if_result_252 = (EL_STR("")); } else { _if_result_252 = (el_str_concat(EL_STR("- "), ss0)); } _if_result_252; })); } else { _if_result_250 = (EL_STR("")); } _if_result_250; }); 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_253 = 0; if ((((hp || hw) || hpr) || hs)) { el_val_t sec_p = ({ el_val_t _if_result_254 = 0; if (hp) { _if_result_254 = (el_str_concat(EL_STR("[USER CONTEXT \xe2\x80\x94 from memory]\n"), profile_bullets)); } else { _if_result_254 = (EL_STR("")); } _if_result_254; }); el_val_t sec_w = ({ el_val_t _if_result_255 = 0; if (hw) { _if_result_255 = (el_str_concat(EL_STR("[ACTIVE WORK \xe2\x80\x94 from memory]\n"), work_bullets)); } else { _if_result_255 = (EL_STR("")); } _if_result_255; }); el_val_t sec_pr = ({ el_val_t _if_result_256 = 0; if (hpr) { _if_result_256 = (el_str_concat(EL_STR("[PROJECTS \xe2\x80\x94 from memory]\n"), project_bullets)); } else { _if_result_256 = (EL_STR("")); } _if_result_256; }); el_val_t sec_s = ({ el_val_t _if_result_257 = 0; if (hs) { _if_result_257 = (el_str_concat(EL_STR("[PREVIOUS SESSION \xe2\x80\x94 from memory]\n"), summary_bullet)); } else { _if_result_257 = (EL_STR("")); } _if_result_257; }); el_val_t sep1 = ({ el_val_t _if_result_258 = 0; if ((hp && ((hw || hpr) || hs))) { _if_result_258 = (EL_STR("\n\n")); } else { _if_result_258 = (EL_STR("")); } _if_result_258; }); el_val_t sep2 = ({ el_val_t _if_result_259 = 0; if ((hw && (hpr || hs))) { _if_result_259 = (EL_STR("\n\n")); } else { _if_result_259 = (EL_STR("")); } _if_result_259; }); el_val_t sep3 = ({ el_val_t _if_result_260 = 0; if ((hpr && hs)) { _if_result_260 = (EL_STR("\n\n")); } else { _if_result_260 = (EL_STR("")); } _if_result_260; }); _if_result_253 = (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_253 = (EL_STR("")); } _if_result_253; }); _if_result_224 = (preload); } else { _if_result_224 = (EL_STR("")); } _if_result_224; }); - el_val_t rendered_hist = ({ el_val_t _if_result_261 = 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_261 = (rh_out); } else { _if_result_261 = (EL_STR("")); } _if_result_261; }); - el_val_t full_system = ({ el_val_t _if_result_262 = 0; if ((hist_len > 0)) { _if_result_262 = (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_262 = (el_str_concat(system, session_preload)); } _if_result_262; }); + el_val_t session_preload = ({ el_val_t _if_result_226 = 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 = 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 = ({ el_val_t _if_result_227 = 0; if (work_ok_typed) { _if_result_227 = (work_nodes_typed); } else { _if_result_227 = (engram_search_json(EL_STR("active project task current in_progress"), 6)); } _if_result_227; }); el_val_t work_ok = (!str_eq(work_nodes, EL_STR("")) && !str_eq(work_nodes, 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_228 = 0; if (profile_ok) { el_val_t pn = json_array_len(profile_nodes); el_val_t bullets = EL_STR(""); el_val_t bullets = ({ el_val_t _if_result_229 = 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_230 = 0; if ((str_len(c0) > 120)) { _if_result_230 = (str_slice(c0, 0, 120)); } else { _if_result_230 = (c0); } _if_result_230; }); _if_result_229 = (({ el_val_t _if_result_231 = 0; if ((id_in_seen(id0, seen_ids) || str_eq(s0, EL_STR("")))) { _if_result_231 = (bullets); } else { _if_result_231 = (el_str_concat(EL_STR("- "), s0)); } _if_result_231; })); } else { _if_result_229 = (bullets); } _if_result_229; }); el_val_t bullets = ({ el_val_t _if_result_232 = 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_233 = 0; if ((str_len(c1) > 120)) { _if_result_233 = (str_slice(c1, 0, 120)); } else { _if_result_233 = (c1); } _if_result_233; }); _if_result_232 = (({ el_val_t _if_result_234 = 0; if ((id_in_seen(id1, seen_ids) || str_eq(s1, EL_STR("")))) { _if_result_234 = (bullets); } else { _if_result_234 = (el_str_concat(el_str_concat(bullets, EL_STR("\n- ")), s1)); } _if_result_234; })); } else { _if_result_232 = (bullets); } _if_result_232; }); el_val_t bullets = ({ el_val_t _if_result_235 = 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_236 = 0; if ((str_len(c2) > 120)) { _if_result_236 = (str_slice(c2, 0, 120)); } else { _if_result_236 = (c2); } _if_result_236; }); _if_result_235 = (({ el_val_t _if_result_237 = 0; if ((id_in_seen(id2, seen_ids) || str_eq(s2, EL_STR("")))) { _if_result_237 = (bullets); } else { _if_result_237 = (el_str_concat(el_str_concat(bullets, EL_STR("\n- ")), s2)); } _if_result_237; })); } else { _if_result_235 = (bullets); } _if_result_235; }); _if_result_228 = (bullets); } else { _if_result_228 = (EL_STR("")); } _if_result_228; }); el_val_t work_bullets = ({ el_val_t _if_result_238 = 0; if (work_ok) { el_val_t wn = json_array_len(work_nodes); el_val_t wb = EL_STR(""); el_val_t wb = ({ el_val_t _if_result_239 = 0; if ((wn > 0)) { el_val_t w0 = json_array_get(work_nodes, 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_240 = 0; if ((str_len(wc0) > 120)) { _if_result_240 = (str_slice(wc0, 0, 120)); } else { _if_result_240 = (wc0); } _if_result_240; }); _if_result_239 = (({ el_val_t _if_result_241 = 0; if ((id_in_seen(wid0, seen_ids) || str_eq(ws0, EL_STR("")))) { _if_result_241 = (wb); } else { _if_result_241 = (el_str_concat(EL_STR("- "), ws0)); } _if_result_241; })); } else { _if_result_239 = (wb); } _if_result_239; }); el_val_t wb = ({ el_val_t _if_result_242 = 0; if ((wn > 1)) { el_val_t w1 = json_array_get(work_nodes, 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_243 = 0; if ((str_len(wc1) > 120)) { _if_result_243 = (str_slice(wc1, 0, 120)); } else { _if_result_243 = (wc1); } _if_result_243; }); _if_result_242 = (({ el_val_t _if_result_244 = 0; if ((id_in_seen(wid1, seen_ids) || str_eq(ws1, EL_STR("")))) { _if_result_244 = (wb); } else { _if_result_244 = (el_str_concat(el_str_concat(wb, EL_STR("\n- ")), ws1)); } _if_result_244; })); } else { _if_result_242 = (wb); } _if_result_242; }); _if_result_238 = (wb); } else { _if_result_238 = (EL_STR("")); } _if_result_238; }); el_val_t project_bullets = ({ el_val_t _if_result_245 = 0; if (project_ok) { el_val_t prn = json_array_len(project_nodes); el_val_t pb = EL_STR(""); el_val_t pb = ({ el_val_t _if_result_246 = 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_247 = 0; if ((str_len(prc0) > 120)) { _if_result_247 = (str_slice(prc0, 0, 120)); } else { _if_result_247 = (prc0); } _if_result_247; }); _if_result_246 = (({ el_val_t _if_result_248 = 0; if ((id_in_seen(prid0, seen_ids) || str_eq(ps0, EL_STR("")))) { _if_result_248 = (pb); } else { _if_result_248 = (el_str_concat(EL_STR("- "), ps0)); } _if_result_248; })); } else { _if_result_246 = (pb); } _if_result_246; }); el_val_t pb = ({ el_val_t _if_result_249 = 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_250 = 0; if ((str_len(prc1) > 120)) { _if_result_250 = (str_slice(prc1, 0, 120)); } else { _if_result_250 = (prc1); } _if_result_250; }); _if_result_249 = (({ el_val_t _if_result_251 = 0; if ((id_in_seen(prid1, seen_ids) || str_eq(ps1, EL_STR("")))) { _if_result_251 = (pb); } else { _if_result_251 = (el_str_concat(el_str_concat(pb, EL_STR("\n- ")), ps1)); } _if_result_251; })); } else { _if_result_249 = (pb); } _if_result_249; }); _if_result_245 = (pb); } else { _if_result_245 = (EL_STR("")); } _if_result_245; }); el_val_t summary_bullet = ({ el_val_t _if_result_252 = 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_253 = 0; if ((str_len(sc0) > 200)) { _if_result_253 = (str_slice(sc0, 0, 200)); } else { _if_result_253 = (sc0); } _if_result_253; }); _if_result_252 = (({ el_val_t _if_result_254 = 0; if ((id_in_seen(snid0, seen_ids) || str_eq(ss0, EL_STR("")))) { _if_result_254 = (EL_STR("")); } else { _if_result_254 = (el_str_concat(EL_STR("- "), ss0)); } _if_result_254; })); } else { _if_result_252 = (EL_STR("")); } _if_result_252; }); 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_255 = 0; if ((((hp || hw) || hpr) || hs)) { el_val_t sec_p = ({ el_val_t _if_result_256 = 0; if (hp) { _if_result_256 = (el_str_concat(EL_STR("[USER CONTEXT \xe2\x80\x94 from memory]\n"), profile_bullets)); } else { _if_result_256 = (EL_STR("")); } _if_result_256; }); el_val_t sec_w = ({ el_val_t _if_result_257 = 0; if (hw) { _if_result_257 = (el_str_concat(EL_STR("[ACTIVE WORK \xe2\x80\x94 from memory]\n"), work_bullets)); } else { _if_result_257 = (EL_STR("")); } _if_result_257; }); el_val_t sec_pr = ({ el_val_t _if_result_258 = 0; if (hpr) { _if_result_258 = (el_str_concat(EL_STR("[PROJECTS \xe2\x80\x94 from memory]\n"), project_bullets)); } else { _if_result_258 = (EL_STR("")); } _if_result_258; }); el_val_t sec_s = ({ el_val_t _if_result_259 = 0; if (hs) { _if_result_259 = (el_str_concat(EL_STR("[PREVIOUS SESSION \xe2\x80\x94 from memory]\n"), summary_bullet)); } else { _if_result_259 = (EL_STR("")); } _if_result_259; }); el_val_t sep1 = ({ el_val_t _if_result_260 = 0; if ((hp && ((hw || hpr) || hs))) { _if_result_260 = (EL_STR("\n\n")); } else { _if_result_260 = (EL_STR("")); } _if_result_260; }); el_val_t sep2 = ({ el_val_t _if_result_261 = 0; if ((hw && (hpr || hs))) { _if_result_261 = (EL_STR("\n\n")); } else { _if_result_261 = (EL_STR("")); } _if_result_261; }); el_val_t sep3 = ({ el_val_t _if_result_262 = 0; if ((hpr && hs)) { _if_result_262 = (EL_STR("\n\n")); } else { _if_result_262 = (EL_STR("")); } _if_result_262; }); _if_result_255 = (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_255 = (EL_STR("")); } _if_result_255; }); _if_result_226 = (preload); } else { _if_result_226 = (EL_STR("")); } _if_result_226; }); + el_val_t rendered_hist = ({ el_val_t _if_result_263 = 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_263 = (rh_out); } else { _if_result_263 = (EL_STR("")); } _if_result_263; }); + el_val_t full_system = ({ el_val_t _if_result_264 = 0; if ((hist_len > 0)) { _if_result_264 = (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_264 = (el_str_concat(system, session_preload)); } _if_result_264; }); el_val_t req_model = json_get(body, EL_STR("model")); - el_val_t model = ({ el_val_t _if_result_263 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_263 = (chat_default_model()); } else { _if_result_263 = (req_model); } _if_result_263; }); + el_val_t model = ({ el_val_t _if_result_265 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_265 = (chat_default_model()); } else { _if_result_265 = (req_model); } _if_result_265; }); 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"))); @@ -1500,7 +1505,7 @@ int main(int _argc, char** _argv) { 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_264 = 0; if ((json_array_len(updated_hist2) > 20)) { _if_result_264 = (hist_trim_with_bell_guard(updated_hist2)); } else { _if_result_264 = (updated_hist2); } _if_result_264; }); + el_val_t final_hist = ({ el_val_t _if_result_266 = 0; if ((json_array_len(updated_hist2) > 20)) { _if_result_266 = (hist_trim_with_bell_guard(updated_hist2)); } else { _if_result_266 = (updated_hist2); } _if_result_266; }); state_set(EL_STR("conv_history"), final_hist); conv_history_persist(final_hist); el_val_t final_hist_len = json_array_len(final_hist); @@ -1508,7 +1513,7 @@ int main(int _argc, char** _argv) { 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_265 = 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_265 = (new_id); } else { _if_result_265 = (boot_id); } _if_result_265; }); + boot_id = ({ el_val_t _if_result_267 = 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_267 = (new_id); } else { _if_result_267 = (boot_id); } _if_result_267; }); 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(""))) { @@ -1519,9 +1524,9 @@ int main(int _argc, char** _argv) { } 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_266 = 0; if (act_ok) { _if_result_266 = (activation_nodes); } else { _if_result_266 = (EL_STR("[]")); } _if_result_266; }); + el_val_t act_out = ({ el_val_t _if_result_268 = 0; if (act_ok) { _if_result_268 = (activation_nodes); } else { _if_result_268 = (EL_STR("[]")); } _if_result_268; }); strengthen_chat_nodes(act_out); - el_val_t hist_warning = ({ el_val_t _if_result_267 = 0; if (hist_load_failed) { _if_result_267 = (EL_STR(",\"history_load_failed\":true")); } else { _if_result_267 = (EL_STR("")); } _if_result_267; }); + el_val_t hist_warning = ({ el_val_t _if_result_269 = 0; if (hist_load_failed) { _if_result_269 = (EL_STR(",\"history_load_failed\":true")); } else { _if_result_269 = (EL_STR("")); } _if_result_269; }); 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("}")); EL_NULL; return 0; diff --git a/dist/routes.c b/dist/routes.c index 13eb5ec..8506bc4 100644 --- a/dist/routes.c +++ b/dist/routes.c @@ -163,11 +163,6 @@ 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); @@ -405,6 +400,9 @@ el_val_t handle_connectors(el_val_t method, el_val_t clean, el_val_t body) { if (str_eq(clean, EL_STR("/api/connectors/oauth/start"))) { return connectd_post(EL_STR("/mcp/oauth/start"), body); } + if (str_eq(clean, EL_STR("/api/connectors/call"))) { + return connectd_post(EL_STR("/mcp/call"), body); + } return EL_STR("{\"ok\":false,\"error\":\"unknown connectors route\"}"); return 0; } @@ -513,7 +511,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { 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)); + el_val_t node_type = str_slice(clean, 17, str_len(clean)); return handle_api_list_typed(node_type, path, body); } if (str_starts_with(clean, EL_STR("/api/neuron/recall"))) { diff --git a/dist/routes.elh b/dist/routes.elh index 761ab6f..6ddf7d7 100644 --- a/dist/routes.elh +++ b/dist/routes.elh @@ -1,4 +1,5 @@ // auto-generated by elc --emit-header — do not edit +extern fn rate_limit_check(ip: String, path: String) -> String extern fn strip_query(path: String) -> String extern fn err_404(path: String) -> String extern fn err_405(method: String, path: String) -> String @@ -8,7 +9,7 @@ extern fn route_imprint_contextual(body: String) -> String extern fn route_imprint_user(body: String) -> String extern fn route_synthesize(body: String) -> String extern fn handle_dharma_recv(body: String) -> String -extern fn route_sessions() -> String -extern fn parse_session_id_from_path(path: String) -> String -extern fn parse_session_subpath(path: String) -> String +extern fn connectd_get(suffix: String) -> String +extern fn connectd_post(suffix: String, body: String) -> String +extern fn handle_connectors(method: String, clean: String, body: String) -> String extern fn handle_request(method: String, path: String, body: String) -> String diff --git a/dist/soul.c b/dist/soul.c index 09611dc..c97dca4 100644 --- a/dist/soul.c +++ b/dist/soul.c @@ -1164,6 +1164,9 @@ el_val_t handle_dharma_recv(el_val_t body); el_val_t route_sessions(void); el_val_t parse_session_id_from_path(el_val_t path); el_val_t parse_session_subpath(el_val_t path); +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 init_soul_edges(void); el_val_t load_identity_context(void); @@ -27287,7 +27290,12 @@ el_val_t handle_chat_agentic(el_val_t body) { el_val_t tools_json = agentic_tools_all(); el_val_t safe_msg = json_safe(message); el_val_t safe_sys = json_safe(system); - el_val_t prior_messages = ({ el_val_t _if_result_50 = 0; if ((agentic_hist_len > 0)) { el_val_t inner = str_slice(agentic_hist, 1, (str_len(agentic_hist) - 1)); _if_result_50 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",{\"role\":\"user\",\"content\":\"")), safe_msg), EL_STR("\"}]"))); } else { _if_result_50 = (el_str_concat(el_str_concat(EL_STR("[{\"role\":\"user\",\"content\":\""), safe_msg), EL_STR("\"}]"))); } _if_result_50; }); + /* PR#56: vision support in agentic chat — send image content block when present */ + 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_v1 = 0; if (str_eq(img_mt_raw, EL_STR(""))) { _if_result_v1 = (EL_STR("image/png")); } else { _if_result_v1 = (img_mt_raw); } _if_result_v1; }); + el_val_t cur_user_content = ({ el_val_t _if_result_v2 = 0; if (str_eq(img_b64, EL_STR(""))) { _if_result_v2 = (el_str_concat(el_str_concat(EL_STR("\""), safe_msg), EL_STR("\""))); } else { _if_result_v2 = (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_v2; }); + el_val_t prior_messages = ({ el_val_t _if_result_50 = 0; if ((agentic_hist_len > 0)) { el_val_t inner = str_slice(agentic_hist, 1, (str_len(agentic_hist) - 1)); _if_result_50 = (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_50 = (el_str_concat(el_str_concat(EL_STR("[{\"role\":\"user\",\"content\":"), cur_user_content), EL_STR("}]"))); } _if_result_50; }); 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); @@ -28890,6 +28898,57 @@ el_val_t parse_session_subpath(el_val_t path) { return 0; } +/* PR#57: connectors subsystem — neuron-connectd bridge on :7771 */ +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_cd1 = 0; if (str_eq(body, EL_STR(""))) { _if_result_cd1 = (EL_STR("{}")); } else { _if_result_cd1 = (body); } _if_result_cd1; }); + 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); + } + if (str_eq(clean, EL_STR("/api/connectors/call"))) { + return connectd_post(EL_STR("/mcp/call"), 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); if (str_eq(method, EL_STR("POST")) && str_eq(clean, EL_STR("/dharma/recv"))) { @@ -28989,12 +29048,15 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { 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)); + el_val_t node_type = str_slice(clean, 17, str_len(clean)); /* PR#58: was 16, left leading "/" on node_type */ 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); + } return err_404(clean); } if (str_eq(method, EL_STR("POST"))) { @@ -29128,6 +29190,9 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t 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"))) { diff --git a/routes.elh b/routes.elh index f1beb43..6ddf7d7 100644 --- a/routes.elh +++ b/routes.elh @@ -1,6 +1,6 @@ -// auto-generated by elc --emit-header - do not edit +// auto-generated by elc --emit-header — do not edit +extern fn rate_limit_check(ip: String, path: String) -> String extern fn strip_query(path: String) -> String -extern fn flag_true(body: String, key: String) -> Bool extern fn err_404(path: String) -> String extern fn err_405(method: String, path: String) -> String extern fn route_health() -> String @@ -9,7 +9,7 @@ extern fn route_imprint_contextual(body: String) -> String extern fn route_imprint_user(body: String) -> String extern fn route_synthesize(body: String) -> String extern fn handle_dharma_recv(body: String) -> String -extern fn route_sessions() -> String -extern fn parse_session_id_from_path(path: String) -> String -extern fn parse_session_subpath(path: String) -> String +extern fn connectd_get(suffix: String) -> String +extern fn connectd_post(suffix: String, body: String) -> String +extern fn handle_connectors(method: String, clean: String, body: String) -> String extern fn handle_request(method: String, path: String, body: String) -> String