From 9aa8db136209f586b11e7ba9bd6ec95c8674d43c Mon Sep 17 00:00:00 2001 From: Will Anderson Date: Sun, 28 Jun 2026 14:23:32 -0500 Subject: [PATCH] Add plan-mode endpoint to agentic path MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit POST /api/chat {mode:'plan'} returns a structured step list from the LLM without executing any tools. The client can show and edit this plan before sending the real agentic request. Implements issue #27 item 2 (Agent panel soul contract — plan mode). --- chat.el | 49 ++++++++++++++ chat.elh | 1 + dist/chat.c | 176 +++++++++++++++++++++++++++++--------------------- dist/chat.elh | 1 + dist/routes.c | 34 +++++----- routes.el | 15 ++++- 6 files changed, 186 insertions(+), 90 deletions(-) diff --git a/chat.el b/chat.el index e3094b4..c0b7a46 100644 --- a/chat.el +++ b/chat.el @@ -1573,6 +1573,55 @@ fn next_bridge_id() -> String { return "br-" + uid } +fn handle_chat_plan(body: String) -> String { + let message: String = json_get(body, "message") + if str_eq(message, "") { + return "{\"error\":\"message required\",\"plan\":null}" + } + + let req_model: String = json_get(body, "model") + let model: String = if str_eq(req_model, "") { chat_default_model() } else { req_model } + + let op_home: String = env("HOME") + let op_user: String = env("USER") + let op_display: String = if str_eq(op_user, "") { "the current user" } else { op_user } + + // Compile context — same intent-seeding as agentic path so the plan is grounded. + let ctx: String = engram_compile(message) + let ctx_block: String = if str_eq(ctx, "") { "" } else { "\n\n[CONTEXT]\n" + ctx } + + let plan_system: String = "You are in PLAN MODE. Your job is to produce a concise step-by-step plan for the request below — WITHOUT executing it.\n\nReturn ONLY a JSON object. No markdown. No preamble. No explanation. Just the JSON:\n{\"steps\":[{\"id\":\"s1\",\"title\":\"<2-6 word title>\",\"detail\":\"\"},{\"id\":\"s2\",...}]}\n\nPlan rules:\n- 3-7 steps (more only when genuinely needed for a complex multi-file task)\n- Each step is one atomic, independently verifiable action\n- title: 2-6 words, imperative (e.g. \"Read config file\", \"Write updated handler\")\n- detail: exactly one sentence describing what happens\n- No tool calls. No execution. No side effects. The user approves before anything runs.\n\nOperator: " + op_display + " at " + op_home + ctx_block + + let raw: String = llm_call_system(model, plan_system, message) + + let is_error: Bool = str_starts_with(raw, "{\"error\"") + if is_error { + return "{\"error\":\"plan generation failed\",\"plan\":null,\"detail\":" + raw + "}" + } + + // Extract the JSON object from the response (LLM sometimes wraps in markdown). + let brace_start: Int = str_index_of(raw, "{") + // Scan backwards to find the last closing brace (str_last_index_of not available). + let brace_end: Int = -1 + let scan_i: Int = str_len(raw) - 1 + while scan_i >= 0 { + let ch: String = str_slice(raw, scan_i, scan_i + 1) + let brace_end = if str_eq(ch, "}") && brace_end < 0 { scan_i } else { brace_end } + let scan_i = if brace_end >= 0 { -1 } else { scan_i - 1 } + } + let plan_json: String = if brace_start >= 0 { + if brace_end > brace_start { + str_slice(raw, brace_start, brace_end + 1) + } else { + raw + } + } else { + raw + } + + return "{\"plan\":" + plan_json + ",\"model\":\"" + json_safe(model) + "\"}" +} + fn handle_chat_agentic(body: String) -> String { let message: String = json_get(body, "message") if str_eq(message, "") { diff --git a/chat.elh b/chat.elh index 7613588..083a968 100644 --- a/chat.elh +++ b/chat.elh @@ -43,6 +43,7 @@ extern fn resolve_in_root(path: String, root: String) -> String extern fn dispatch_tool(tool_name: String, tool_input: String) -> String extern fn is_builtin_tool(tool_name: String) -> Bool extern fn next_bridge_id() -> String +extern fn handle_chat_plan(body: String) -> String extern fn handle_chat_agentic(body: String) -> String extern fn agentic_loop(session_id: String, model: String, safe_sys: String, tools_json: String, messages_in: String, h: Map, tools_log_in: String) -> String extern fn bridge_save(session_id: String, model: String, safe_sys: String, tools_json: String, messages: String, tools_log: String, tool_use_id: String) -> Bool diff --git a/dist/chat.c b/dist/chat.c index 51592b1..926cb64 100644 --- a/dist/chat.c +++ b/dist/chat.c @@ -61,6 +61,7 @@ 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_plan(el_val_t body); 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); @@ -1029,6 +1030,37 @@ el_val_t next_bridge_id(void) { return 0; } +el_val_t handle_chat_plan(el_val_t body) { + el_val_t message = json_get(body, EL_STR("message")); + if (str_eq(message, EL_STR(""))) { + return EL_STR("{\"error\":\"message required\",\"plan\":null}"); + } + el_val_t req_model = json_get(body, EL_STR("model")); + el_val_t model = ({ el_val_t _if_result_147 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_147 = (chat_default_model()); } else { _if_result_147 = (req_model); } _if_result_147; }); + el_val_t op_home = env(EL_STR("HOME")); + el_val_t op_user = env(EL_STR("USER")); + el_val_t op_display = ({ el_val_t _if_result_148 = 0; if (str_eq(op_user, EL_STR(""))) { _if_result_148 = (EL_STR("the current user")); } else { _if_result_148 = (op_user); } _if_result_148; }); + el_val_t ctx = engram_compile(message); + el_val_t ctx_block = ({ el_val_t _if_result_149 = 0; if (str_eq(ctx, EL_STR(""))) { _if_result_149 = (EL_STR("")); } else { _if_result_149 = (el_str_concat(EL_STR("\n\n[CONTEXT]\n"), ctx)); } _if_result_149; }); + el_val_t plan_system = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("You are in PLAN MODE. Your job is to produce a concise step-by-step plan for the request below \xe2\x80\x94 WITHOUT executing it.\n\nReturn ONLY a JSON object. No markdown. No preamble. No explanation. Just the JSON:\n{\"steps\":[{\"id\":\"s1\",\"title\":\"<2-6 word title>\",\"detail\":\"\"},{\"id\":\"s2\",...}]}\n\nPlan rules:\n- 3-7 steps (more only when genuinely needed for a complex multi-file task)\n- Each step is one atomic, independently verifiable action\n- title: 2-6 words, imperative (e.g. \"Read config file\", \"Write updated handler\")\n- detail: exactly one sentence describing what happens\n- No tool calls. No execution. No side effects. The user approves before anything runs.\n\nOperator: "), op_display), EL_STR(" at ")), op_home), ctx_block); + el_val_t raw = llm_call_system(model, plan_system, message); + el_val_t is_error = str_starts_with(raw, EL_STR("{\"error\"")); + if (is_error) { + return el_str_concat(el_str_concat(EL_STR("{\"error\":\"plan generation failed\",\"plan\":null,\"detail\":"), raw), EL_STR("}")); + } + el_val_t brace_start = str_index_of(raw, EL_STR("{")); + el_val_t brace_end = (-1); + el_val_t scan_i = (str_len(raw) - 1); + while (scan_i >= 0) { + el_val_t ch = str_slice(raw, scan_i, (scan_i + 1)); + brace_end = ({ el_val_t _if_result_150 = 0; if ((str_eq(ch, EL_STR("}")) && (brace_end < 0))) { _if_result_150 = (scan_i); } else { _if_result_150 = (brace_end); } _if_result_150; }); + scan_i = ({ el_val_t _if_result_151 = 0; if ((brace_end >= 0)) { _if_result_151 = ((-1)); } else { _if_result_151 = ((scan_i - 1)); } _if_result_151; }); + } + el_val_t plan_json = ({ el_val_t _if_result_152 = 0; if ((brace_start >= 0)) { _if_result_152 = (({ el_val_t _if_result_153 = 0; if ((brace_end > brace_start)) { _if_result_153 = (str_slice(raw, brace_start, (brace_end + 1))); } else { _if_result_153 = (raw); } _if_result_153; })); } else { _if_result_152 = (raw); } _if_result_152; }); + return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"plan\":"), plan_json), EL_STR(",\"model\":\"")), json_safe(model)), EL_STR("\"}")); + return 0; +} + el_val_t handle_chat_agentic(el_val_t body) { el_val_t message = json_get(body, EL_STR("message")); if (str_eq(message, EL_STR(""))) { @@ -1046,23 +1078,23 @@ el_val_t handle_chat_agentic(el_val_t body) { return el_str_concat(el_str_concat(EL_STR("{\"reply\":\""), json_safe(safety_validate(EL_STR(""), EL_STR("hard_bell")))), EL_STR("\",\"model\":\"\",\"agentic\":true,\"tools_used\":[]}")); } el_val_t req_model = json_get(body, EL_STR("model")); - el_val_t model = ({ el_val_t _if_result_147 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_147 = (chat_default_model()); } else { _if_result_147 = (req_model); } _if_result_147; }); + el_val_t model = ({ el_val_t _if_result_154 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_154 = (chat_default_model()); } else { _if_result_154 = (req_model); } _if_result_154; }); el_val_t req_session = json_get(body, EL_STR("session_id")); - el_val_t session_valid = ({ el_val_t _if_result_148 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_148 = (1); } else { _if_result_148 = (session_exists(req_session)); } _if_result_148; }); + el_val_t session_valid = ({ el_val_t _if_result_155 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_155 = (1); } else { _if_result_155 = (session_exists(req_session)); } _if_result_155; }); if (!session_valid) { return el_str_concat(el_str_concat(EL_STR("{\"error\":\"session not found\",\"session_id\":\""), req_session), EL_STR("\",\"reply\":\"\"}")); } - el_val_t hist_key = ({ el_val_t _if_result_149 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_149 = (EL_STR("conv_history")); } else { _if_result_149 = (el_str_concat(EL_STR("session_hist_"), req_session)); } _if_result_149; }); + el_val_t hist_key = ({ el_val_t _if_result_156 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_156 = (EL_STR("conv_history")); } else { _if_result_156 = (el_str_concat(EL_STR("session_hist_"), req_session)); } _if_result_156; }); el_val_t agentic_hist = state_get(hist_key); - el_val_t agentic_hist_len = ({ el_val_t _if_result_150 = 0; if (str_eq(agentic_hist, EL_STR(""))) { _if_result_150 = (0); } else { _if_result_150 = (json_array_len(agentic_hist)); } _if_result_150; }); + el_val_t agentic_hist_len = ({ el_val_t _if_result_157 = 0; if (str_eq(agentic_hist, EL_STR(""))) { _if_result_157 = (0); } else { _if_result_157 = (json_array_len(agentic_hist)); } _if_result_157; }); el_val_t ag_is_cont = engram_is_continuation(message, agentic_hist_len); - el_val_t ag_last_entry = ({ el_val_t _if_result_151 = 0; if (ag_is_cont) { _if_result_151 = (json_array_get(agentic_hist, (agentic_hist_len - 1))); } else { _if_result_151 = (EL_STR("")); } _if_result_151; }); - el_val_t ag_last_content = ({ el_val_t _if_result_152 = 0; if (!str_eq(ag_last_entry, EL_STR(""))) { _if_result_152 = (json_get(ag_last_entry, EL_STR("content"))); } else { _if_result_152 = (EL_STR("")); } _if_result_152; }); - el_val_t ag_thread_snip = ({ el_val_t _if_result_153 = 0; if ((str_len(ag_last_content) > 150)) { _if_result_153 = (str_slice(ag_last_content, 0, 150)); } else { _if_result_153 = (ag_last_content); } _if_result_153; }); - el_val_t ag_seed = ({ el_val_t _if_result_154 = 0; if (!str_eq(ag_thread_snip, EL_STR(""))) { _if_result_154 = (el_str_concat(el_str_concat(ag_thread_snip, EL_STR(" ")), message)); } else { _if_result_154 = (message); } _if_result_154; }); + el_val_t ag_last_entry = ({ el_val_t _if_result_158 = 0; if (ag_is_cont) { _if_result_158 = (json_array_get(agentic_hist, (agentic_hist_len - 1))); } else { _if_result_158 = (EL_STR("")); } _if_result_158; }); + el_val_t ag_last_content = ({ el_val_t _if_result_159 = 0; if (!str_eq(ag_last_entry, EL_STR(""))) { _if_result_159 = (json_get(ag_last_entry, EL_STR("content"))); } else { _if_result_159 = (EL_STR("")); } _if_result_159; }); + el_val_t ag_thread_snip = ({ el_val_t _if_result_160 = 0; if ((str_len(ag_last_content) > 150)) { _if_result_160 = (str_slice(ag_last_content, 0, 150)); } else { _if_result_160 = (ag_last_content); } _if_result_160; }); + el_val_t ag_seed = ({ el_val_t _if_result_161 = 0; if (!str_eq(ag_thread_snip, EL_STR(""))) { _if_result_161 = (el_str_concat(el_str_concat(ag_thread_snip, EL_STR(" ")), message)); } else { _if_result_161 = (message); } _if_result_161; }); el_val_t ctx = engram_compile(ag_seed); el_val_t identity = state_get(EL_STR("soul_identity")); - el_val_t ag_session_preload = ({ el_val_t _if_result_155 = 0; if ((agentic_hist_len == 0)) { el_val_t ag_profile_nodes = engram_search_json(EL_STR("Persona soul:persona identity principal"), 8); el_val_t ag_profile_ok = (!str_eq(ag_profile_nodes, EL_STR("")) && !str_eq(ag_profile_nodes, EL_STR("[]"))); el_val_t ag_profile_nodes2 = ({ el_val_t _if_result_156 = 0; if (ag_profile_ok) { _if_result_156 = (ag_profile_nodes); } else { _if_result_156 = (engram_search_json(EL_STR("user profile preferences name"), 8)); } _if_result_156; }); el_val_t ag_work_nodes = engram_search_json(EL_STR("WorkItem status:in_progress active work"), 6); el_val_t ag_work_ok = (!str_eq(ag_work_nodes, EL_STR("")) && !str_eq(ag_work_nodes, EL_STR("[]"))); el_val_t ag_work_nodes2 = ({ el_val_t _if_result_157 = 0; if (ag_work_ok) { _if_result_157 = (ag_work_nodes); } else { _if_result_157 = (engram_search_json(EL_STR("active project task current in_progress"), 6)); } _if_result_157; }); el_val_t ag_continuity_nodes = engram_search_json(EL_STR("last-session-topic session:emotional-summary conv:history last session"), 3); el_val_t ag_continuity_ok = (!str_eq(ag_continuity_nodes, EL_STR("")) && !str_eq(ag_continuity_nodes, EL_STR("[]"))); el_val_t ag_continuity_snip = ({ el_val_t _if_result_158 = 0; if (ag_continuity_ok) { el_val_t acn0 = json_array_get(ag_continuity_nodes, 0); el_val_t acc = json_get(acn0, EL_STR("content")); _if_result_158 = (({ el_val_t _if_result_159 = 0; if ((str_len(acc) > 350)) { _if_result_159 = (str_slice(acc, 0, 350)); } else { _if_result_159 = (acc); } _if_result_159; })); } else { _if_result_158 = (EL_STR("")); } _if_result_158; }); el_val_t ag_profile_bullets = session_preload_bullets(ag_profile_nodes2, 8, 350); el_val_t ag_work_bullets = session_preload_bullets(ag_work_nodes2, 6, 350); el_val_t ag_has_profile = !str_eq(ag_profile_bullets, EL_STR("")); el_val_t ag_has_work = !str_eq(ag_work_bullets, EL_STR("")); el_val_t ag_has_cont = !str_eq(ag_continuity_snip, EL_STR("")); _if_result_155 = (({ el_val_t _if_result_160 = 0; if (((ag_has_profile || ag_has_work) || ag_has_cont)) { el_val_t p = ({ el_val_t _if_result_161 = 0; if (ag_has_profile) { _if_result_161 = (el_str_concat(el_str_concat(EL_STR("[USER CONTEXT \xe2\x80\x94 from memory]\n"), ag_profile_bullets), EL_STR("\n\n"))); } else { _if_result_161 = (EL_STR("")); } _if_result_161; }); el_val_t w = ({ el_val_t _if_result_162 = 0; if (ag_has_work) { _if_result_162 = (el_str_concat(el_str_concat(EL_STR("[ACTIVE WORK \xe2\x80\x94 from memory]\n"), ag_work_bullets), EL_STR("\n\n"))); } else { _if_result_162 = (EL_STR("")); } _if_result_162; }); el_val_t c = ({ el_val_t _if_result_163 = 0; if (ag_has_cont) { _if_result_163 = (el_str_concat(el_str_concat(EL_STR("[CONTINUING FROM LAST SESSION]\n"), ag_continuity_snip), EL_STR("\n\n"))); } else { _if_result_163 = (EL_STR("")); } _if_result_163; }); _if_result_160 = (el_str_concat(el_str_concat(el_str_concat(EL_STR("\n\n"), p), w), c)); } else { _if_result_160 = (EL_STR("")); } _if_result_160; })); } else { _if_result_155 = (EL_STR("")); } _if_result_155; }); + el_val_t ag_session_preload = ({ el_val_t _if_result_162 = 0; if ((agentic_hist_len == 0)) { el_val_t ag_profile_nodes = engram_search_json(EL_STR("Persona soul:persona identity principal"), 8); el_val_t ag_profile_ok = (!str_eq(ag_profile_nodes, EL_STR("")) && !str_eq(ag_profile_nodes, EL_STR("[]"))); el_val_t ag_profile_nodes2 = ({ el_val_t _if_result_163 = 0; if (ag_profile_ok) { _if_result_163 = (ag_profile_nodes); } else { _if_result_163 = (engram_search_json(EL_STR("user profile preferences name"), 8)); } _if_result_163; }); el_val_t ag_work_nodes = engram_search_json(EL_STR("WorkItem status:in_progress active work"), 6); el_val_t ag_work_ok = (!str_eq(ag_work_nodes, EL_STR("")) && !str_eq(ag_work_nodes, EL_STR("[]"))); el_val_t ag_work_nodes2 = ({ el_val_t _if_result_164 = 0; if (ag_work_ok) { _if_result_164 = (ag_work_nodes); } else { _if_result_164 = (engram_search_json(EL_STR("active project task current in_progress"), 6)); } _if_result_164; }); el_val_t ag_continuity_nodes = engram_search_json(EL_STR("last-session-topic session:emotional-summary conv:history last session"), 3); el_val_t ag_continuity_ok = (!str_eq(ag_continuity_nodes, EL_STR("")) && !str_eq(ag_continuity_nodes, EL_STR("[]"))); el_val_t ag_continuity_snip = ({ el_val_t _if_result_165 = 0; if (ag_continuity_ok) { el_val_t acn0 = json_array_get(ag_continuity_nodes, 0); el_val_t acc = json_get(acn0, EL_STR("content")); _if_result_165 = (({ el_val_t _if_result_166 = 0; if ((str_len(acc) > 350)) { _if_result_166 = (str_slice(acc, 0, 350)); } else { _if_result_166 = (acc); } _if_result_166; })); } else { _if_result_165 = (EL_STR("")); } _if_result_165; }); el_val_t ag_profile_bullets = session_preload_bullets(ag_profile_nodes2, 8, 350); el_val_t ag_work_bullets = session_preload_bullets(ag_work_nodes2, 6, 350); el_val_t ag_has_profile = !str_eq(ag_profile_bullets, EL_STR("")); el_val_t ag_has_work = !str_eq(ag_work_bullets, EL_STR("")); el_val_t ag_has_cont = !str_eq(ag_continuity_snip, EL_STR("")); _if_result_162 = (({ el_val_t _if_result_167 = 0; if (((ag_has_profile || ag_has_work) || ag_has_cont)) { el_val_t p = ({ el_val_t _if_result_168 = 0; if (ag_has_profile) { _if_result_168 = (el_str_concat(el_str_concat(EL_STR("[USER CONTEXT \xe2\x80\x94 from memory]\n"), ag_profile_bullets), EL_STR("\n\n"))); } else { _if_result_168 = (EL_STR("")); } _if_result_168; }); el_val_t w = ({ el_val_t _if_result_169 = 0; if (ag_has_work) { _if_result_169 = (el_str_concat(el_str_concat(EL_STR("[ACTIVE WORK \xe2\x80\x94 from memory]\n"), ag_work_bullets), EL_STR("\n\n"))); } else { _if_result_169 = (EL_STR("")); } _if_result_169; }); el_val_t c = ({ el_val_t _if_result_170 = 0; if (ag_has_cont) { _if_result_170 = (el_str_concat(el_str_concat(EL_STR("[CONTINUING FROM LAST SESSION]\n"), ag_continuity_snip), EL_STR("\n\n"))); } else { _if_result_170 = (EL_STR("")); } _if_result_170; }); _if_result_167 = (el_str_concat(el_str_concat(el_str_concat(EL_STR("\n\n"), p), w), c)); } else { _if_result_167 = (EL_STR("")); } _if_result_167; })); } else { _if_result_162 = (EL_STR("")); } _if_result_162; }); el_val_t system = el_str_concat(el_str_concat(el_str_concat(identity, EL_STR(" You have access to tools: read files, write files, browse the web, search your memory, run commands. Use them when they add genuine value. Be direct.\n\n")), ctx), ag_session_preload); el_val_t api_key = agentic_api_key(); el_val_t tools_json = agentic_tools_all(); @@ -1070,19 +1102,19 @@ el_val_t handle_chat_agentic(el_val_t body) { el_val_t safe_sys = json_safe(system); el_val_t img_b64 = json_get(body, EL_STR("image")); el_val_t img_mt_raw = json_get(body, EL_STR("image_media_type")); - el_val_t img_mt = ({ el_val_t _if_result_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 img_mt = ({ el_val_t _if_result_171 = 0; if (str_eq(img_mt_raw, EL_STR(""))) { _if_result_171 = (EL_STR("image/png")); } else { _if_result_171 = (img_mt_raw); } _if_result_171; }); + el_val_t cur_user_content = ({ el_val_t _if_result_172 = 0; if (str_eq(img_b64, EL_STR(""))) { _if_result_172 = (el_str_concat(el_str_concat(EL_STR("\""), safe_msg), EL_STR("\""))); } else { _if_result_172 = (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_172; }); + el_val_t prior_messages = ({ el_val_t _if_result_173 = 0; if ((agentic_hist_len > 0)) { el_val_t inner = str_slice(agentic_hist, 1, (str_len(agentic_hist) - 1)); _if_result_173 = (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_173 = (el_str_concat(el_str_concat(EL_STR("[{\"role\":\"user\",\"content\":"), cur_user_content), EL_STR("}]"))); } _if_result_173; }); 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_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 session_id = ({ el_val_t _if_result_174 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_174 = (next_bridge_id()); } else { _if_result_174 = (req_session); } _if_result_174; }); 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_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; }); + el_val_t discard_hist = ({ el_val_t _if_result_175 = 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_176 = 0; if ((json_array_len(updated2) > 40)) { _if_result_176 = (hist_trim(updated2)); } else { _if_result_176 = (updated2); } _if_result_176; }); (void)(state_set(hist_key, trimmed)); (void)(({ el_val_t _if_result_177 = 0; if (str_eq(hist_key, EL_STR("conv_history"))) { _if_result_177 = (conv_history_persist(trimmed)); } else { _if_result_177 = (({ el_val_t _if_result_178 = 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_178 = (({ el_val_t _if_result_179 = 0; if (str_eq(sess_hist_id, EL_STR(""))) { _if_result_179 = (println(el_str_concat(EL_STR("[chat] agentic: named session history persist failed for session="), req_session))); } else { } _if_result_179; })); } else { } _if_result_178; })); } _if_result_177; })); _if_result_175 = (1); } else { _if_result_175 = (0); } _if_result_175; }); return result; return 0; } @@ -1107,7 +1139,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_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 eff_content = ({ el_val_t _if_result_180 = 0; if (str_eq(content_arr, EL_STR(""))) { _if_result_180 = (EL_STR("[]")); } else { _if_result_180 = (content_arr); } _if_result_180; }); el_val_t text_out = EL_STR(""); el_val_t has_tool = 0; el_val_t tool_id = EL_STR(""); @@ -1118,51 +1150,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_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; }); + text_out = ({ el_val_t _if_result_181 = 0; if (str_eq(btype, EL_STR("text"))) { _if_result_181 = (el_str_concat(text_out, json_get(block, EL_STR("text")))); } else { _if_result_181 = (text_out); } _if_result_181; }); el_val_t is_new_tool = (str_eq(btype, EL_STR("tool_use")) && !has_tool); - 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; }); + has_tool = ({ el_val_t _if_result_182 = 0; if (is_new_tool) { _if_result_182 = (1); } else { _if_result_182 = (has_tool); } _if_result_182; }); + tool_id = ({ el_val_t _if_result_183 = 0; if (is_new_tool) { _if_result_183 = (json_get(block, EL_STR("id"))); } else { _if_result_183 = (tool_id); } _if_result_183; }); + tool_name = ({ el_val_t _if_result_184 = 0; if (is_new_tool) { _if_result_184 = (json_get(block, EL_STR("name"))); } else { _if_result_184 = (tool_name); } _if_result_184; }); + tool_input = ({ el_val_t _if_result_185 = 0; if (is_new_tool) { _if_result_185 = (json_get_raw(block, EL_STR("input"))); } else { _if_result_185 = (tool_input); } _if_result_185; }); 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_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 always_list = ({ el_val_t _if_result_186 = 0; if (!str_eq(session_id, EL_STR(""))) { _if_result_186 = (state_get(always_key)); } else { _if_result_186 = (EL_STR("")); } _if_result_186; }); 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_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_result_raw = ({ el_val_t _if_result_187 = 0; if ((is_tool_turn && !needs_bridge)) { _if_result_187 = (dispatch_tool(tool_name, tool_input)); } else { _if_result_187 = (EL_STR("")); } _if_result_187; }); + el_val_t tool_result = ({ el_val_t _if_result_188 = 0; if ((str_len(tool_result_raw) > 6000)) { _if_result_188 = (el_str_concat(str_slice(tool_result_raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_188 = (tool_result_raw); } _if_result_188; }); 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_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; }); + tools_log = ({ el_val_t _if_result_189 = 0; if (has_tool) { _if_result_189 = (({ el_val_t _if_result_190 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_190 = (tool_quoted); } else { _if_result_190 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), tool_quoted)); } _if_result_190; })); } else { _if_result_189 = (tools_log); } _if_result_189; }); 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_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; }); + messages = ({ el_val_t _if_result_191 = 0; if (local_continue) { el_val_t inner2 = str_slice(messages_with_assistant, 1, (str_len(messages_with_assistant) - 1)); _if_result_191 = (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_191 = (messages); } _if_result_191; }); + pending = ({ el_val_t _if_result_192 = 0; if (needs_bridge) { _if_result_192 = (1); } else { _if_result_192 = (pending); } _if_result_192; }); + pend_tool_id = ({ el_val_t _if_result_193 = 0; if (needs_bridge) { _if_result_193 = (tool_id); } else { _if_result_193 = (pend_tool_id); } _if_result_193; }); + pend_tool_name = ({ el_val_t _if_result_194 = 0; if (needs_bridge) { _if_result_194 = (tool_name); } else { _if_result_194 = (pend_tool_name); } _if_result_194; }); + pend_tool_input = ({ el_val_t _if_result_195 = 0; if (needs_bridge) { _if_result_195 = (tool_input); } else { _if_result_195 = (pend_tool_input); } _if_result_195; }); 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_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; }); + final_text = ({ el_val_t _if_result_196 = 0; if (!is_tool_turn) { _if_result_196 = (text_out); } else { _if_result_196 = (final_text); } _if_result_196; }); + keep_going = ({ el_val_t _if_result_197 = 0; if (local_continue) { _if_result_197 = (keep_going); } else { _if_result_197 = (0); } _if_result_197; }); iteration = (iteration + 1); } if (pending) { - 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; }); + el_val_t safe_in = ({ el_val_t _if_result_198 = 0; if (str_eq(pend_tool_input, EL_STR(""))) { _if_result_198 = (EL_STR("{}")); } else { _if_result_198 = (pend_tool_input); } _if_result_198; }); + el_val_t tools_arr = ({ el_val_t _if_result_199 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_199 = (EL_STR("[]")); } else { _if_result_199 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_199; }); 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_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; }); + el_val_t err_msg = ({ el_val_t _if_result_200 = 0; if (hit_cap) { _if_result_200 = (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_200 = (EL_STR("no response")); } _if_result_200; }); 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_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; }); + el_val_t tools_arr = ({ el_val_t _if_result_201 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_201 = (EL_STR("[]")); } else { _if_result_201 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_201; }); 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; } @@ -1185,17 +1217,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_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; }); + messages = ({ el_val_t _if_result_202 = 0; if (str_eq(messages, EL_STR(""))) { _if_result_202 = (json_get(blob, EL_STR("messages"))); } else { _if_result_202 = (messages); } _if_result_202; }); el_val_t tools_json = json_get_raw(blob, EL_STR("tools_raw")); - 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; }); + tools_json = ({ el_val_t _if_result_203 = 0; if (str_eq(tools_json, EL_STR(""))) { _if_result_203 = (json_get(blob, EL_STR("tools_json"))); } else { _if_result_203 = (tools_json); } _if_result_203; }); 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_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 use_id = ({ el_val_t _if_result_204 = 0; if (str_eq(tool_use_id, EL_STR(""))) { _if_result_204 = (saved_use_id); } else { _if_result_204 = (tool_use_id); } _if_result_204; }); + el_val_t eff_use_id = ({ el_val_t _if_result_205 = 0; if (str_eq(use_id, saved_use_id)) { _if_result_205 = (use_id); } else { _if_result_205 = (saved_use_id); } _if_result_205; }); + el_val_t trimmed = ({ el_val_t _if_result_206 = 0; if ((str_len(content) > 6000)) { _if_result_206 = (el_str_concat(str_slice(content, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_206 = (content); } _if_result_206; }); 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)); @@ -1231,12 +1263,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_200 = 0; if (str_eq(message, EL_STR(""))) { _if_result_200 = (transcript); } else { _if_result_200 = (message); } _if_result_200; }); + el_val_t eff_message = ({ el_val_t _if_result_207 = 0; if (str_eq(message, EL_STR(""))) { _if_result_207 = (transcript); } else { _if_result_207 = (message); } _if_result_207; }); 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_201 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_201 = (chat_default_model()); } else { _if_result_201 = (req_model); } _if_result_201; }); + el_val_t model = ({ el_val_t _if_result_208 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_208 = (chat_default_model()); } else { _if_result_208 = (req_model); } _if_result_208; }); 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"))); @@ -1259,7 +1291,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_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; }); + el_val_t system_prompt = ({ el_val_t _if_result_209 = 0; if (str_eq(engram_ctx, EL_STR(""))) { _if_result_209 = (identity); } else { _if_result_209 = (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_209; }); 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"))); @@ -1299,7 +1331,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_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 session_id = ({ el_val_t _if_result_210 = 0; if (str_eq(room_id, EL_STR(""))) { _if_result_210 = (el_str_concat(EL_STR("dharma:"), next_bridge_id())); } else { _if_result_210 = (el_str_concat(EL_STR("dharma:"), room_id)); } _if_result_210; }); 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(""))) { @@ -1314,7 +1346,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_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 eff_tools = ({ el_val_t _if_result_211 = 0; if (str_eq(tools_arr, EL_STR(""))) { _if_result_211 = (EL_STR("[]")); } else { _if_result_211 = (tools_arr); } _if_result_211; }); 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; @@ -1325,7 +1357,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_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 trimmed = ({ el_val_t _if_result_212 = 0; if ((str_len(safe_text) > 800)) { _if_result_212 = (str_slice(safe_text, 0, 800)); } else { _if_result_212 = (safe_text); } _if_result_212; }); 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); @@ -1356,7 +1388,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_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 trimmed = ({ el_val_t _if_result_213 = 0; if ((str_len(safe_text) > 800)) { _if_result_213 = (str_slice(safe_text, 0, 800)); } else { _if_result_213 = (safe_text); } _if_result_213; }); 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); @@ -1390,8 +1422,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_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; }); + el_val_t snip = ({ el_val_t _if_result_214 = 0; if ((str_len(msg) > 80)) { _if_result_214 = (str_slice(msg, 0, 80)); } else { _if_result_214 = (msg); } _if_result_214; }); + snippets = ({ el_val_t _if_result_215 = 0; if (str_eq(snippets, EL_STR(""))) { _if_result_215 = (snip); } else { _if_result_215 = (el_str_concat(el_str_concat(snippets, EL_STR("; ")), snip)); } _if_result_215; }); count = (count + 1); } i = (i + 1); @@ -1406,7 +1438,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_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; }); + el_val_t reply2 = ({ el_val_t _if_result_216 = 0; if (str_eq(reply, EL_STR(""))) { _if_result_216 = (json_get(resp, EL_STR("reply"))); } else { _if_result_216 = (reply); } _if_result_216; }); if (str_eq(message, EL_STR(""))) { return EL_STR(""); } @@ -1418,42 +1450,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_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 tags = ({ el_val_t _if_result_217 = 0; if (is_bell) { _if_result_217 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"bell:"), bell_level), EL_STR("\",\"affective\"]"))); } else { _if_result_217 = (({ el_val_t _if_result_218 = 0; if (is_positive) { _if_result_218 = (el_str_concat(el_str_concat(EL_STR("[\"Conversation\",\"chat\",\"timestamped\",\"joy:"), positive_level), EL_STR("\",\"affective\"]"))); } else { _if_result_218 = (EL_STR("[\"Conversation\",\"chat\",\"timestamped\"]")); } _if_result_218; })); } _if_result_217; }); 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_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 summary = ({ el_val_t _if_result_219 = 0; if ((str_len(message) > 120)) { _if_result_219 = (str_slice(message, 0, 120)); } else { _if_result_219 = (message); } _if_result_219; }); 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_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 sal_a = ({ el_val_t _if_result_220 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_220 = (el_from_float(0.98)); } else { _if_result_220 = (el_from_float(0.88)); } _if_result_220; }); + el_val_t sal_b = ({ el_val_t _if_result_221 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_221 = (el_from_float(0.98)); } else { _if_result_221 = (el_from_float(0.88)); } _if_result_221; }); + el_val_t sal_c = ({ el_val_t _if_result_222 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_222 = (el_from_float(1.0)); } else { _if_result_222 = (el_from_float(0.95)); } _if_result_222; }); 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_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 bell_key = ({ el_val_t _if_result_223 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_223 = (EL_STR("session_bell_count")); } else { _if_result_223 = (el_str_concat(EL_STR("session_bell_count:"), sess_id)); } _if_result_223; }); el_val_t prior_count = state_get(bell_key); - 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; }); + el_val_t prior_n = ({ el_val_t _if_result_224 = 0; if (str_eq(prior_count, EL_STR(""))) { _if_result_224 = (0); } else { _if_result_224 = (str_to_int(prior_count)); } _if_result_224; }); state_set(bell_key, int_to_str((prior_n + 1))); - 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 level_key = ({ el_val_t _if_result_225 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_225 = (EL_STR("session_bell_level")); } else { _if_result_225 = (el_str_concat(EL_STR("session_bell_level:"), sess_id)); } _if_result_225; }); el_val_t prior_level = state_get(level_key); - 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; }); + el_val_t new_level = ({ el_val_t _if_result_226 = 0; if (str_eq(bell_level, EL_STR("hard"))) { _if_result_226 = (EL_STR("hard")); } else { _if_result_226 = (({ el_val_t _if_result_227 = 0; if (str_eq(prior_level, EL_STR("hard"))) { _if_result_227 = (EL_STR("hard")); } else { _if_result_227 = (EL_STR("soft")); } _if_result_227; })); } _if_result_226; }); state_set(level_key, new_level); - 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; }); + el_val_t signal_key = ({ el_val_t _if_result_228 = 0; if (str_eq(sess_id, EL_STR(""))) { _if_result_228 = (EL_STR("session_bell_signal")); } else { _if_result_228 = (el_str_concat(EL_STR("session_bell_signal:"), sess_id)); } _if_result_228; }); state_set(signal_key, safe_summary); } if (is_positive) { - 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 pos_summary = ({ el_val_t _if_result_229 = 0; if ((str_len(message) > 120)) { _if_result_229 = (str_slice(message, 0, 120)); } else { _if_result_229 = (message); } _if_result_229; }); 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_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_sal_a = ({ el_val_t _if_result_230 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_230 = (el_from_float(0.88)); } else { _if_result_230 = (el_from_float(0.75)); } _if_result_230; }); + el_val_t pos_sal_b = ({ el_val_t _if_result_231 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_231 = (el_from_float(0.88)); } else { _if_result_231 = (el_from_float(0.75)); } _if_result_231; }); + el_val_t pos_sal_c = ({ el_val_t _if_result_232 = 0; if (str_eq(positive_level, EL_STR("high"))) { _if_result_232 = (el_from_float(0.95)); } else { _if_result_232 = (el_from_float(0.85)); } _if_result_232; }); 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); @@ -1490,11 +1522,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_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 session_preload = ({ el_val_t _if_result_233 = 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_234 = 0; if (work_ok_typed) { _if_result_234 = (work_nodes_typed); } else { _if_result_234 = (engram_search_json(EL_STR("active project task current in_progress"), 6)); } _if_result_234; }); 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_235 = 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_236 = 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_237 = 0; if ((str_len(c0) > 120)) { _if_result_237 = (str_slice(c0, 0, 120)); } else { _if_result_237 = (c0); } _if_result_237; }); _if_result_236 = (({ el_val_t _if_result_238 = 0; if ((id_in_seen(id0, seen_ids) || str_eq(s0, EL_STR("")))) { _if_result_238 = (bullets); } else { _if_result_238 = (el_str_concat(EL_STR("- "), s0)); } _if_result_238; })); } else { _if_result_236 = (bullets); } _if_result_236; }); el_val_t bullets = ({ el_val_t _if_result_239 = 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_240 = 0; if ((str_len(c1) > 120)) { _if_result_240 = (str_slice(c1, 0, 120)); } else { _if_result_240 = (c1); } _if_result_240; }); _if_result_239 = (({ el_val_t _if_result_241 = 0; if ((id_in_seen(id1, seen_ids) || str_eq(s1, EL_STR("")))) { _if_result_241 = (bullets); } else { _if_result_241 = (el_str_concat(el_str_concat(bullets, EL_STR("\n- ")), s1)); } _if_result_241; })); } else { _if_result_239 = (bullets); } _if_result_239; }); el_val_t bullets = ({ el_val_t _if_result_242 = 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_243 = 0; if ((str_len(c2) > 120)) { _if_result_243 = (str_slice(c2, 0, 120)); } else { _if_result_243 = (c2); } _if_result_243; }); _if_result_242 = (({ el_val_t _if_result_244 = 0; if ((id_in_seen(id2, seen_ids) || str_eq(s2, EL_STR("")))) { _if_result_244 = (bullets); } else { _if_result_244 = (el_str_concat(el_str_concat(bullets, EL_STR("\n- ")), s2)); } _if_result_244; })); } else { _if_result_242 = (bullets); } _if_result_242; }); _if_result_235 = (bullets); } else { _if_result_235 = (EL_STR("")); } _if_result_235; }); el_val_t work_bullets = ({ el_val_t _if_result_245 = 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_246 = 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_247 = 0; if ((str_len(wc0) > 120)) { _if_result_247 = (str_slice(wc0, 0, 120)); } else { _if_result_247 = (wc0); } _if_result_247; }); _if_result_246 = (({ el_val_t _if_result_248 = 0; if ((id_in_seen(wid0, seen_ids) || str_eq(ws0, EL_STR("")))) { _if_result_248 = (wb); } else { _if_result_248 = (el_str_concat(EL_STR("- "), ws0)); } _if_result_248; })); } else { _if_result_246 = (wb); } _if_result_246; }); el_val_t wb = ({ el_val_t _if_result_249 = 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_250 = 0; if ((str_len(wc1) > 120)) { _if_result_250 = (str_slice(wc1, 0, 120)); } else { _if_result_250 = (wc1); } _if_result_250; }); _if_result_249 = (({ el_val_t _if_result_251 = 0; if ((id_in_seen(wid1, seen_ids) || str_eq(ws1, EL_STR("")))) { _if_result_251 = (wb); } else { _if_result_251 = (el_str_concat(el_str_concat(wb, EL_STR("\n- ")), ws1)); } _if_result_251; })); } else { _if_result_249 = (wb); } _if_result_249; }); _if_result_245 = (wb); } else { _if_result_245 = (EL_STR("")); } _if_result_245; }); el_val_t project_bullets = ({ el_val_t _if_result_252 = 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_253 = 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_254 = 0; if ((str_len(prc0) > 120)) { _if_result_254 = (str_slice(prc0, 0, 120)); } else { _if_result_254 = (prc0); } _if_result_254; }); _if_result_253 = (({ el_val_t _if_result_255 = 0; if ((id_in_seen(prid0, seen_ids) || str_eq(ps0, EL_STR("")))) { _if_result_255 = (pb); } else { _if_result_255 = (el_str_concat(EL_STR("- "), ps0)); } _if_result_255; })); } else { _if_result_253 = (pb); } _if_result_253; }); el_val_t pb = ({ el_val_t _if_result_256 = 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_257 = 0; if ((str_len(prc1) > 120)) { _if_result_257 = (str_slice(prc1, 0, 120)); } else { _if_result_257 = (prc1); } _if_result_257; }); _if_result_256 = (({ el_val_t _if_result_258 = 0; if ((id_in_seen(prid1, seen_ids) || str_eq(ps1, EL_STR("")))) { _if_result_258 = (pb); } else { _if_result_258 = (el_str_concat(el_str_concat(pb, EL_STR("\n- ")), ps1)); } _if_result_258; })); } else { _if_result_256 = (pb); } _if_result_256; }); _if_result_252 = (pb); } else { _if_result_252 = (EL_STR("")); } _if_result_252; }); el_val_t summary_bullet = ({ el_val_t _if_result_259 = 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_260 = 0; if ((str_len(sc0) > 200)) { _if_result_260 = (str_slice(sc0, 0, 200)); } else { _if_result_260 = (sc0); } _if_result_260; }); _if_result_259 = (({ el_val_t _if_result_261 = 0; if ((id_in_seen(snid0, seen_ids) || str_eq(ss0, EL_STR("")))) { _if_result_261 = (EL_STR("")); } else { _if_result_261 = (el_str_concat(EL_STR("- "), ss0)); } _if_result_261; })); } else { _if_result_259 = (EL_STR("")); } _if_result_259; }); 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_262 = 0; if ((((hp || hw) || hpr) || hs)) { el_val_t sec_p = ({ el_val_t _if_result_263 = 0; if (hp) { _if_result_263 = (el_str_concat(EL_STR("[USER CONTEXT \xe2\x80\x94 from memory]\n"), profile_bullets)); } else { _if_result_263 = (EL_STR("")); } _if_result_263; }); el_val_t sec_w = ({ el_val_t _if_result_264 = 0; if (hw) { _if_result_264 = (el_str_concat(EL_STR("[ACTIVE WORK \xe2\x80\x94 from memory]\n"), work_bullets)); } else { _if_result_264 = (EL_STR("")); } _if_result_264; }); el_val_t sec_pr = ({ el_val_t _if_result_265 = 0; if (hpr) { _if_result_265 = (el_str_concat(EL_STR("[PROJECTS \xe2\x80\x94 from memory]\n"), project_bullets)); } else { _if_result_265 = (EL_STR("")); } _if_result_265; }); el_val_t sec_s = ({ el_val_t _if_result_266 = 0; if (hs) { _if_result_266 = (el_str_concat(EL_STR("[PREVIOUS SESSION \xe2\x80\x94 from memory]\n"), summary_bullet)); } else { _if_result_266 = (EL_STR("")); } _if_result_266; }); el_val_t sep1 = ({ el_val_t _if_result_267 = 0; if ((hp && ((hw || hpr) || hs))) { _if_result_267 = (EL_STR("\n\n")); } else { _if_result_267 = (EL_STR("")); } _if_result_267; }); el_val_t sep2 = ({ el_val_t _if_result_268 = 0; if ((hw && (hpr || hs))) { _if_result_268 = (EL_STR("\n\n")); } else { _if_result_268 = (EL_STR("")); } _if_result_268; }); el_val_t sep3 = ({ el_val_t _if_result_269 = 0; if ((hpr && hs)) { _if_result_269 = (EL_STR("\n\n")); } else { _if_result_269 = (EL_STR("")); } _if_result_269; }); _if_result_262 = (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_262 = (EL_STR("")); } _if_result_262; }); _if_result_233 = (preload); } else { _if_result_233 = (EL_STR("")); } _if_result_233; }); + el_val_t rendered_hist = ({ el_val_t _if_result_270 = 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_270 = (rh_out); } else { _if_result_270 = (EL_STR("")); } _if_result_270; }); + el_val_t full_system = ({ el_val_t _if_result_271 = 0; if ((hist_len > 0)) { _if_result_271 = (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_271 = (el_str_concat(system, session_preload)); } _if_result_271; }); el_val_t req_model = json_get(body, EL_STR("model")); - 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; }); + el_val_t model = ({ el_val_t _if_result_272 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_272 = (chat_default_model()); } else { _if_result_272 = (req_model); } _if_result_272; }); 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"))); @@ -1505,7 +1537,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_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; }); + el_val_t final_hist = ({ el_val_t _if_result_273 = 0; if ((json_array_len(updated_hist2) > 20)) { _if_result_273 = (hist_trim_with_bell_guard(updated_hist2)); } else { _if_result_273 = (updated_hist2); } _if_result_273; }); state_set(EL_STR("conv_history"), final_hist); conv_history_persist(final_hist); el_val_t final_hist_len = json_array_len(final_hist); @@ -1513,7 +1545,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_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; }); + boot_id = ({ el_val_t _if_result_274 = 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_274 = (new_id); } else { _if_result_274 = (boot_id); } _if_result_274; }); 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(""))) { @@ -1524,9 +1556,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_268 = 0; if (act_ok) { _if_result_268 = (activation_nodes); } else { _if_result_268 = (EL_STR("[]")); } _if_result_268; }); + el_val_t act_out = ({ el_val_t _if_result_275 = 0; if (act_ok) { _if_result_275 = (activation_nodes); } else { _if_result_275 = (EL_STR("[]")); } _if_result_275; }); strengthen_chat_nodes(act_out); - 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; }); + el_val_t hist_warning = ({ el_val_t _if_result_276 = 0; if (hist_load_failed) { _if_result_276 = (EL_STR(",\"history_load_failed\":true")); } else { _if_result_276 = (EL_STR("")); } _if_result_276; }); 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/chat.elh b/dist/chat.elh index 7613588..083a968 100644 --- a/dist/chat.elh +++ b/dist/chat.elh @@ -43,6 +43,7 @@ extern fn resolve_in_root(path: String, root: String) -> String extern fn dispatch_tool(tool_name: String, tool_input: String) -> String extern fn is_builtin_tool(tool_name: String) -> Bool extern fn next_bridge_id() -> String +extern fn handle_chat_plan(body: String) -> String extern fn handle_chat_agentic(body: String) -> String extern fn agentic_loop(session_id: String, model: String, safe_sys: String, tools_json: String, messages_in: String, h: Map, tools_log_in: String) -> String extern fn bridge_save(session_id: String, model: String, safe_sys: String, tools_json: String, messages: String, tools_log: String, tool_use_id: String) -> Bool diff --git a/dist/routes.c b/dist/routes.c index 8506bc4..202eff0 100644 --- a/dist/routes.c +++ b/dist/routes.c @@ -85,6 +85,7 @@ 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_plan(el_val_t body); 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); @@ -317,22 +318,23 @@ el_val_t handle_dharma_recv(el_val_t body) { 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; }); + el_val_t req_mode = json_get(chat_body, EL_STR("mode")); + el_val_t reply = ({ el_val_t _if_result_15 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_15 = (handle_chat_plan(chat_body)); } else { _if_result_15 = (({ el_val_t _if_result_16 = 0; if (agentic_flag) { _if_result_16 = (handle_chat_agentic(chat_body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_16 = (screened_reply); } _if_result_16; })); } _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; }); + el_val_t limit = ({ el_val_t _if_result_17 = 0; if (str_eq(limit_str, EL_STR(""))) { _if_result_17 = (20); } else { _if_result_17 = (str_to_int(limit_str)); } _if_result_17; }); + el_val_t q = ({ el_val_t _if_result_18 = 0; if (str_eq(query, EL_STR(""))) { _if_result_18 = (eff_payload); } else { _if_result_18 = (query); } _if_result_18; }); 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; }); + el_val_t eff_method = ({ el_val_t _if_result_19 = 0; if (str_eq(method_field, EL_STR(""))) { _if_result_19 = (EL_STR("POST")); } else { _if_result_19 = (method_field); } _if_result_19; }); return handle_tool(path_field, eff_method, tool_body); } if (str_eq(eff_event, EL_STR("see"))) { @@ -367,7 +369,7 @@ el_val_t connectd_get(el_val_t suffix) { } el_val_t connectd_post(el_val_t suffix, el_val_t body) { - el_val_t eff = ({ el_val_t _if_result_19 = 0; if (str_eq(body, EL_STR(""))) { _if_result_19 = (EL_STR("{}")); } else { _if_result_19 = (body); } _if_result_19; }); + el_val_t eff = ({ el_val_t _if_result_20 = 0; if (str_eq(body, EL_STR(""))) { _if_result_20 = (EL_STR("{}")); } else { _if_result_20 = (body); } _if_result_20; }); 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)); @@ -434,16 +436,17 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { engram_save(snap_path); el_val_t snap = fs_read(snap_path); el_val_t edges_raw = json_get_raw(snap, EL_STR("edges")); - return ({ el_val_t _if_result_20 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_20 = (EL_STR("[]")); } else { _if_result_20 = (edges_raw); } _if_result_20; }); + return ({ el_val_t _if_result_21 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_21 = (EL_STR("[]")); } else { _if_result_21 = (edges_raw); } _if_result_21; }); } 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; }); + el_val_t eff_msg = ({ el_val_t _if_result_22 = 0; if (str_eq(raw_msg, EL_STR(""))) { _if_result_22 = (body); } else { _if_result_22 = (raw_msg); } _if_result_22; }); 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; }); + el_val_t req_mode = json_get(body, EL_STR("mode")); + el_val_t reply = ({ el_val_t _if_result_23 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_23 = (handle_chat_plan(body)); } else { _if_result_23 = (({ el_val_t _if_result_24 = 0; if (agentic_flag) { _if_result_24 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(eff_msg); _if_result_24 = (screened_reply); } _if_result_24; })); } _if_result_23; }); auto_persist(body, reply); return reply; } @@ -526,7 +529,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { if (str_starts_with(clean, EL_STR("/api/sessions/"))) { el_val_t gs_after = str_slice(clean, 14, str_len(clean)); el_val_t gs_slash = str_index_of(gs_after, EL_STR("/")); - el_val_t gs_id = ({ el_val_t _if_result_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; }); + el_val_t gs_id = ({ el_val_t _if_result_25 = 0; if ((gs_slash < 0)) { _if_result_25 = (gs_after); } else { _if_result_25 = (str_slice(gs_after, 0, gs_slash)); } _if_result_25; }); if (!str_eq(gs_id, EL_STR(""))) { return session_get(gs_id); } @@ -540,14 +543,14 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { if (str_starts_with(clean, EL_STR("/api/sessions/")) && str_ends_with(clean, EL_STR("/tool_result"))) { el_val_t after = str_slice(clean, 14, str_len(clean)); el_val_t slash = str_index_of(after, EL_STR("/")); - el_val_t session_id = ({ el_val_t _if_result_24 = 0; if ((slash < 0)) { _if_result_24 = (after); } else { _if_result_24 = (str_slice(after, 0, slash)); } _if_result_24; }); + el_val_t session_id = ({ el_val_t _if_result_26 = 0; if ((slash < 0)) { _if_result_26 = (after); } else { _if_result_26 = (str_slice(after, 0, slash)); } _if_result_26; }); 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; }); + el_val_t sess_id = ({ el_val_t _if_result_27 = 0; if ((sess_slash < 0)) { _if_result_27 = (sess_after); } else { _if_result_27 = (str_slice(sess_after, 0, sess_slash)); } _if_result_27; }); + el_val_t sess_sub = ({ el_val_t _if_result_28 = 0; if ((sess_slash < 0)) { _if_result_28 = (EL_STR("")); } else { _if_result_28 = (str_slice(sess_after, (sess_slash + 1), str_len(sess_after))); } _if_result_28; }); if (!str_eq(sess_id, EL_STR("")) && str_eq(sess_sub, EL_STR("approve"))) { return handle_session_approve(sess_id, body); } @@ -570,7 +573,8 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { 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; }); + el_val_t req_mode = json_get(body, EL_STR("mode")); + el_val_t reply = ({ el_val_t _if_result_29 = 0; if (str_eq(req_mode, EL_STR("plan"))) { _if_result_29 = (handle_chat_plan(body)); } else { _if_result_29 = (({ el_val_t _if_result_30 = 0; if (agentic_flag) { _if_result_30 = (handle_chat_agentic(body)); } else { el_val_t screened_reply = layered_cycle(raw_msg); _if_result_30 = (screened_reply); } _if_result_30; })); } _if_result_29; }); auto_persist(body, reply); return reply; } @@ -694,7 +698,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { if (str_starts_with(clean, EL_STR("/api/sessions/"))) { el_val_t del_after = str_slice(clean, 14, str_len(clean)); el_val_t del_slash = str_index_of(del_after, EL_STR("/")); - el_val_t del_id = ({ el_val_t _if_result_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; }); + el_val_t del_id = ({ el_val_t _if_result_31 = 0; if ((del_slash < 0)) { _if_result_31 = (del_after); } else { _if_result_31 = (str_slice(del_after, 0, del_slash)); } _if_result_31; }); if (!str_eq(del_id, EL_STR(""))) { return session_delete(del_id); } @@ -705,7 +709,7 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) { if (str_starts_with(clean, EL_STR("/api/sessions/"))) { el_val_t patch_after = str_slice(clean, 14, str_len(clean)); el_val_t patch_slash = str_index_of(patch_after, EL_STR("/")); - el_val_t patch_id = ({ el_val_t _if_result_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; }); + el_val_t patch_id = ({ el_val_t _if_result_32 = 0; if ((patch_slash < 0)) { _if_result_32 = (patch_after); } else { _if_result_32 = (str_slice(patch_after, 0, patch_slash)); } _if_result_32; }); if (!str_eq(patch_id, EL_STR(""))) { return session_update_patch(patch_id, body); } diff --git a/routes.el b/routes.el index 8326ccb..09444ad 100644 --- a/routes.el +++ b/routes.el @@ -229,7 +229,10 @@ fn handle_dharma_recv(body: String) -> String { } let agentic_flag: Bool = json_get_bool(eff_payload, "agentic") let raw_msg: String = json_get(chat_body, "message") - let reply: String = if agentic_flag { + let req_mode: String = json_get(chat_body, "mode") + let reply: String = if str_eq(req_mode, "plan") { + handle_chat_plan(chat_body) + } else if agentic_flag { handle_chat_agentic(chat_body) } else { let screened_reply: String = layered_cycle(raw_msg) @@ -391,7 +394,10 @@ fn handle_request(method: String, path: String, body: String) -> String { return "{\"error\":\"message is required\",\"code\":\"missing_param\"}" } let agentic_flag: Bool = json_get_bool(body, "agentic") - let reply: String = if agentic_flag { + let req_mode: String = json_get(body, "mode") + let reply: String = if str_eq(req_mode, "plan") { + handle_chat_plan(body) + } else if agentic_flag { handle_chat_agentic(body) } else { let screened_reply: String = layered_cycle(eff_msg) @@ -540,7 +546,10 @@ fn handle_request(method: String, path: String, body: String) -> String { return "{\"error\":\"message is required\",\"code\":\"missing_param\"}" } let agentic_flag: Bool = json_get_bool(body, "agentic") - let reply: String = if agentic_flag { + let req_mode: String = json_get(body, "mode") + let reply: String = if str_eq(req_mode, "plan") { + handle_chat_plan(body) + } else if agentic_flag { handle_chat_agentic(body) } else { let screened_reply: String = layered_cycle(raw_msg)