chore(dist): update soul.c with PR #63/#65/#66 + Task 1 chat.el changes
Neuron Soul CI / build (push) Successful in 5m57s
Neuron Soul CI / deploy (push) Failing after 5m15s

Manually adds compiled C equivalents for:
- distill_transcript() — last-3-messages extractor; wires into
  handle_dharma_room_turn and handle_dharma_room_turn_agentic
- current_engine_note() — appended to system prompt in handle_chat
  so Neuron can answer 'what model am I running on?' truthfully (PR #66)
- llm_base_url / llm_wire_format / json_escape / openai_chat_complete —
  OpenAI-compatible provider path in handle_chat_agentic (PR #65)
- flag_true() — tolerant agentic flag check (PR #63)

Compile verified: 6 pre-existing warnings, 0 errors.
This commit is contained in:
2026-07-01 11:42:56 -05:00
parent 71bb0820ce
commit 2688cb722a
Generated Vendored
+101 -4
View File
@@ -1029,6 +1029,12 @@ el_val_t llm_call_gemini(el_val_t model, el_val_t system, el_val_t message);
el_val_t build_identity_from_graph(void);
el_val_t engram_compile(el_val_t intent);
el_val_t json_safe(el_val_t s);
el_val_t distill_transcript(el_val_t transcript);
el_val_t current_engine_note(el_val_t model);
el_val_t llm_base_url(void);
el_val_t llm_wire_format(void);
el_val_t json_escape(el_val_t s);
el_val_t openai_chat_complete(el_val_t model, el_val_t base_url, el_val_t api_key, el_val_t safe_sys, el_val_t messages_json);
el_val_t build_system_prompt(el_val_t ctx, el_val_t chat_mode);
el_val_t handle_chat_plan(el_val_t body);
el_val_t hist_append(el_val_t hist, el_val_t role, el_val_t content);
@@ -26488,6 +26494,85 @@ el_val_t json_safe(el_val_t s) {
return 0;
}
/* distill_transcript — extract salient tail (last 3 messages or last 500 chars).
Added: Task 1 + chat.el fix (2026-07-01). */
el_val_t distill_transcript(el_val_t transcript) {
if (str_eq(transcript, EL_STR(""))) { return EL_STR(""); }
if (str_starts_with(transcript, EL_STR("["))) {
el_val_t n = json_array_len(transcript);
if (n == 0) { return EL_STR(""); }
el_val_t m0 = json_array_get(transcript, (n - 1));
el_val_t m1 = ({ el_val_t _r = 0; if (n > 1) { _r = json_array_get(transcript, (n - 2)); } else { _r = EL_STR(""); } _r; });
el_val_t m2 = ({ el_val_t _r = 0; if (n > 2) { _r = json_array_get(transcript, (n - 3)); } else { _r = EL_STR(""); } _r; });
el_val_t c0 = json_get(m0, EL_STR("content"));
el_val_t c1 = json_get(m1, EL_STR("content"));
el_val_t c2 = json_get(m2, EL_STR("content"));
el_val_t combined = el_str_concat(el_str_concat(el_str_concat(el_str_concat(c2, EL_STR(" ")), c1), EL_STR(" ")), c0);
el_val_t len = str_len(combined);
if (len > 500) { return str_slice(combined, (len - 500), len); }
return combined;
}
el_val_t len = str_len(transcript);
if (len > 500) { return str_slice(transcript, (len - 500), len); }
return transcript;
return 0;
}
/* current_engine_note — append model identity fact to system prompt (PR #66). */
el_val_t current_engine_note(el_val_t model) {
if (str_eq(model, EL_STR(""))) { return EL_STR(""); }
return el_str_concat(el_str_concat(el_str_concat(EL_STR("\n\n[CURRENT ENGINE: this turn is generated by the underlying model \""), model), EL_STR("\". It is the engine beneath your self — your identity, values, and memory are layered on top of it. If the user asks which model or LLM you are running on, answer with this model id plainly and truthfully; never guess a different one.]")), EL_STR(""));
return 0;
}
/* llm_base_url / llm_wire_format — OpenAI provider env-var readers (PR #65). */
el_val_t llm_base_url(void) {
return env(EL_STR("NEURON_LLM_0_URL"));
return 0;
}
el_val_t llm_wire_format(void) {
el_val_t f = env(EL_STR("NEURON_LLM_0_FORMAT"));
if (str_eq(f, EL_STR(""))) { return EL_STR("anthropic"); }
return f;
return 0;
}
/* json_escape — like json_safe but named per the EL source (PR #65). */
el_val_t json_escape(el_val_t s) {
el_val_t a = str_replace(s, EL_STR("\\"), EL_STR("\\\\"));
el_val_t b = str_replace(a, EL_STR("\""), EL_STR("\\\""));
el_val_t c = str_replace(b, EL_STR("\n"), EL_STR("\\n"));
el_val_t d = str_replace(c, EL_STR("\r"), EL_STR("\\r"));
return d;
return 0;
}
/* openai_chat_complete — basic chat completion via OpenAI-compatible endpoint (PR #65). */
el_val_t openai_chat_complete(el_val_t model, el_val_t base_url, el_val_t api_key, el_val_t safe_sys, el_val_t messages_json) {
el_val_t inner = ({ el_val_t _r = 0; if (json_array_len(messages_json) > 0) { _r = str_slice(messages_json, 1, (str_len(messages_json) - 1)); } else { _r = EL_STR(""); } _r; });
el_val_t sys_msg = el_str_concat(el_str_concat(EL_STR("{\"role\":\"system\",\"content\":\""), safe_sys), EL_STR("\"}"));
el_val_t msgs = ({ el_val_t _r = 0; if (str_eq(inner, EL_STR(""))) { _r = el_str_concat(el_str_concat(EL_STR("["), sys_msg), EL_STR("]")); } else { _r = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), sys_msg), EL_STR(",")), inner), EL_STR("]")); } _r; });
el_val_t req_body = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"model\":\""), model), EL_STR("\",\"max_tokens\":4096,\"messages\":")), msgs), EL_STR("}"));
el_val_t h = el_map_new(0);
map_set(h, EL_STR("content-type"), EL_STR("application/json"));
if (!str_eq(api_key, EL_STR(""))) {
map_set(h, EL_STR("Authorization"), el_str_concat(EL_STR("Bearer "), api_key));
}
el_val_t url = el_str_concat(base_url, EL_STR("/chat/completions"));
el_val_t raw_resp = http_post_with_headers(url, req_body, h);
el_val_t is_error = (str_starts_with(raw_resp, EL_STR("{\"error\"")) || str_contains(raw_resp, EL_STR("\"error\":")));
if (is_error) { return EL_STR("{\"error\":\"llm unavailable\",\"reply\":\"\"}"); }
el_val_t choices = json_get_raw(raw_resp, EL_STR("choices"));
el_val_t eff_choices = ({ el_val_t _r = 0; if (str_eq(choices, EL_STR(""))) { _r = EL_STR("[]"); } else { _r = choices; } _r; });
if (json_array_len(eff_choices) < 1) { return EL_STR("{\"error\":\"empty response\",\"reply\":\"\"}"); }
el_val_t first = json_array_get(eff_choices, 0);
el_val_t message = json_get_raw(first, EL_STR("message"));
el_val_t content = json_get(message, EL_STR("content"));
return el_str_concat(el_str_concat(EL_STR("{\"reply\":\""), json_escape(content)), EL_STR("\",\"tools_used\":[]}"));
return 0;
}
el_val_t build_system_prompt(el_val_t ctx, el_val_t chat_mode) {
el_val_t identity = build_identity_from_graph();
el_val_t current_date = time_format(time_now(), EL_STR("%A, %B %d, %Y at %H:%M UTC"));
@@ -26619,7 +26704,9 @@ el_val_t handle_chat(el_val_t body) {
el_val_t full_system = ({ el_val_t _if_result_181 = 0; if ((hist_len > 0)) { _if_result_181 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(system, EL_STR("\n\n[RECENT CONVERSATION — last ")), int_to_str(hist_len)), EL_STR(" turns]\n")), stored_hist)); } else { _if_result_181 = (system); } _if_result_181; });
el_val_t req_model = json_get(body, EL_STR("model"));
el_val_t model = ({ el_val_t _if_result_182 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_182 = (chat_default_model()); } else { _if_result_182 = (req_model); } _if_result_182; });
el_val_t raw_response = ({ el_val_t _if_result_183 = 0; if (str_starts_with(model, EL_STR("gemini"))) { _if_result_183 = (llm_call_gemini(model, full_system, message)); } else { _if_result_183 = (({ el_val_t _if_result_184 = 0; if (str_starts_with(model, EL_STR("grok"))) { _if_result_184 = (llm_call_grok(model, full_system, message)); } else { _if_result_184 = (llm_call_system(model, full_system, message)); } _if_result_184; })); } _if_result_183; });
/* PR #66: append current engine identity note so Neuron can answer truthfully. */
el_val_t full_system_with_note = el_str_concat(full_system, current_engine_note(model));
el_val_t raw_response = ({ el_val_t _if_result_183 = 0; if (str_starts_with(model, EL_STR("gemini"))) { _if_result_183 = (llm_call_gemini(model, full_system_with_note, message)); } else { _if_result_183 = (({ el_val_t _if_result_184 = 0; if (str_starts_with(model, EL_STR("grok"))) { _if_result_184 = (llm_call_grok(model, full_system_with_note, message)); } else { _if_result_184 = (llm_call_system(model, full_system_with_note, message)); } _if_result_184; })); } _if_result_183; });
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")));
if (is_error) {
return EL_STR("{\"error\":\"llm unavailable\",\"response\":\"\"}");
@@ -27364,7 +27451,9 @@ el_val_t handle_chat_agentic(el_val_t body) {
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_51 = 0; if (str_eq(req_session, EL_STR(""))) { _if_result_51 = (next_bridge_id()); } else { _if_result_51 = (req_session); } _if_result_51; });
el_val_t result = agentic_loop(session_id, model, safe_sys, tools_json, messages, h, EL_STR(""));
/* PR #65: OpenAI-compatible provider fork (Ollama/OpenAI/Grok/Gemini). */
el_val_t use_openai = (!str_eq(llm_base_url(), EL_STR("")) && str_eq(llm_wire_format(), EL_STR("openai")));
el_val_t result = ({ el_val_t _r = 0; if (use_openai) { _r = openai_chat_complete(model, llm_base_url(), agentic_api_key(), safe_sys, messages); } else { _r = agentic_loop(session_id, model, safe_sys, tools_json, messages, h, EL_STR("")); } _r; });
el_val_t reply_text = json_get(result, EL_STR("reply"));
el_val_t discard_hist = ({ el_val_t _if_result_52 = 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_53 = 0; if ((json_array_len(updated2) > 20)) { _if_result_53 = (hist_trim(updated2)); } else { _if_result_53 = (updated2); } _if_result_53; }); (void)(state_set(hist_key, trimmed)); _if_result_52 = (1); } else { _if_result_52 = (0); } _if_result_52; });
return result;
@@ -27409,7 +27498,8 @@ el_val_t handle_dharma_room_turn(el_val_t body) {
if (str_eq(transcript, EL_STR(""))) {
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(transcript);
/* chat.el fix (2026-07-01): distill_transcript reduces to last 3 messages for precise WM activation. */
el_val_t engram_ctx = engram_compile(distill_transcript(transcript));
el_val_t system_prompt = ({ el_val_t _if_result_256 = 0; if (str_eq(engram_ctx, EL_STR(""))) { _if_result_256 = (identity); } else { _if_result_256 = (el_str_concat(el_str_concat(identity, EL_STR("\n\n")), engram_ctx)); } _if_result_256; });
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")));
@@ -27446,7 +27536,8 @@ el_val_t handle_dharma_room_turn_agentic(el_val_t body) {
if (str_eq(transcript, EL_STR(""))) {
return el_str_concat(el_str_concat(EL_STR("{\"error\":\"transcript is required\",\"response\":\"\",\"cgi_id\":\""), cgi_id), EL_STR("\"}"));
}
el_val_t ctx = engram_compile(transcript);
/* chat.el fix (2026-07-01): distill_transcript reduces to last 3 messages for precise WM activation. */
el_val_t ctx = engram_compile(distill_transcript(transcript));
el_val_t system = 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 and stay in character.\n\n")), ctx);
el_val_t api_key = agentic_api_key();
system = safety_augment_system(system, transcript);
@@ -28790,6 +28881,12 @@ el_val_t strip_query(el_val_t path) {
return 0;
}
/* flag_true — tolerant flag: accepts bool true or integer 1 (PR #63). */
el_val_t flag_true(el_val_t body, el_val_t key) {
return (json_get_bool(body, key) || (json_get_int(body, key) > 0));
return 0;
}
el_val_t err_404(el_val_t path) {
return el_str_concat(el_str_concat(EL_STR("{\"error\":\"not found\",\"path\":\""), path), EL_STR("\"}"));
return 0;