Fix agentic tool loop: El scoping rules, json_get_raw for array/object fields, result truncation

This commit is contained in:
Will Anderson
2026-05-03 12:36:42 -05:00
parent af2ce3ddf3
commit d500415316
9 changed files with 179 additions and 48 deletions
+18 -4
View File
@@ -87,14 +87,28 @@ el_val_t handle_elp_chat(el_val_t body) {
fi = (fi + 1);
}
el_val_t frame_nodes = ({ el_val_t _if_result_13 = 0; if (str_eq(kept_json, EL_STR(""))) { _if_result_13 = (EL_STR("[]")); } else { _if_result_13 = (el_str_concat(el_str_concat(EL_STR("["), kept_json), EL_STR("]"))); } _if_result_13; });
el_val_t top_node = json_array_get(frame_nodes, 0);
el_val_t fn_total = json_array_len(frame_nodes);
el_val_t fn_i = 0;
el_val_t topic_lower = str_to_lower(topic);
el_val_t found_node = EL_STR("");
while (fn_i < fn_total) {
el_val_t candidate = json_array_get(frame_nodes, fn_i);
el_val_t cand_content = json_get(candidate, EL_STR("content"));
el_val_t cand_lower = str_to_lower(cand_content);
el_val_t matches = str_contains(cand_lower, topic_lower);
if (matches && str_eq(found_node, EL_STR(""))) {
found_node = candidate;
}
fn_i = (fn_i + 1);
}
el_val_t top_node = ({ el_val_t _if_result_14 = 0; if (str_eq(found_node, EL_STR(""))) { _if_result_14 = (json_array_get(frame_nodes, 0)); } else { _if_result_14 = (found_node); } _if_result_14; });
el_val_t top_raw = json_get(top_node, EL_STR("content"));
el_val_t patient_raw = ({ el_val_t _if_result_14 = 0; if (str_eq(top_raw, EL_STR(""))) { _if_result_14 = (topic); } else { _if_result_14 = (({ el_val_t _if_result_15 = 0; if ((str_len(top_raw) > 200)) { _if_result_15 = (str_slice(top_raw, 0, 200)); } else { _if_result_15 = (top_raw); } _if_result_15; })); } _if_result_14; });
el_val_t patient_raw = ({ el_val_t _if_result_15 = 0; if (str_eq(top_raw, EL_STR(""))) { _if_result_15 = (topic); } else { _if_result_15 = (({ el_val_t _if_result_16 = 0; if ((str_len(top_raw) > 200)) { _if_result_16 = (str_slice(top_raw, 0, 200)); } else { _if_result_16 = (top_raw); } _if_result_16; })); } _if_result_15; });
el_val_t patient_safe = str_replace(str_replace(patient_raw, EL_STR("\""), EL_STR("'")), EL_STR("\n"), EL_STR(" "));
el_val_t intent_val = ({ el_val_t _if_result_16 = 0; if (str_eq(predicate, EL_STR("store"))) { _if_result_16 = (EL_STR("command")); } else { _if_result_16 = (EL_STR("assert")); } _if_result_16; });
el_val_t intent_val = ({ el_val_t _if_result_17 = 0; if (str_eq(predicate, EL_STR("store"))) { _if_result_17 = (EL_STR("command")); } else { _if_result_17 = (EL_STR("assert")); } _if_result_17; });
el_val_t gen_form = el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"intent\":\""), intent_val), EL_STR("\"")), EL_STR(",\"agent\":\"I\"")), EL_STR(",\"predicate\":\"")), predicate), EL_STR("\"")), EL_STR(",\"patient\":\"")), patient_safe), EL_STR("\"")), EL_STR(",\"tense\":\"present\",\"aspect\":\"simple\",\"lang\":\"en\"}"));
el_val_t realized = generate(gen_form);
el_val_t response = ({ el_val_t _if_result_17 = 0; if (str_eq(realized, EL_STR(""))) { _if_result_17 = (({ el_val_t _if_result_18 = 0; if (str_eq(patient_safe, EL_STR(""))) { _if_result_18 = (EL_STR("Nothing in the engram matched that query.")); } else { _if_result_18 = (patient_safe); } _if_result_18; })); } else { _if_result_17 = (realized); } _if_result_17; });
el_val_t response = ({ el_val_t _if_result_18 = 0; if (str_eq(realized, EL_STR(""))) { _if_result_18 = (({ el_val_t _if_result_19 = 0; if (str_eq(patient_safe, EL_STR(""))) { _if_result_19 = (EL_STR("Nothing in the engram matched that query.")); } else { _if_result_19 = (patient_safe); } _if_result_19; })); } else { _if_result_18 = (realized); } _if_result_18; });
el_val_t safe_resp = str_replace(str_replace(response, EL_STR("\""), EL_STR("'")), EL_STR("\r"), EL_STR(""));
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"response\":\""), safe_resp), EL_STR("\",\"model\":\"elp-native\",\"frame\":")), frame), EL_STR(",\"nodes\":")), frame_nodes), EL_STR("}"));
return 0;