Compare commits

...

1 Commits

Author SHA1 Message Date
will.anderson 9aa8db1362 Add plan-mode endpoint to agentic path
Neuron Soul CI / build (pull_request) Has been cancelled
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).
2026-06-28 14:23:32 -05:00
6 changed files with 186 additions and 90 deletions
+49
View File
@@ -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\":\"<one concrete sentence>\"},{\"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, "") {
+1
View File
@@ -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
Generated Vendored
+104 -72
View File
File diff suppressed because one or more lines are too long
Generated Vendored
+1
View File
@@ -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
Generated Vendored
+19 -15
View File
@@ -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);
}
+12 -3
View File
@@ -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)