Compare commits
3 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| bcdadb7323 | |||
| 3bb17a5296 | |||
| 6c57d4fe1b |
@@ -0,0 +1,184 @@
|
||||
# Memory Recall Bug — Handoff for Will
|
||||
|
||||
**Reported by:** Tim (via the Neuron UI chat)
|
||||
**Diagnosed by:** Claude (Claude Code session), 2026-06-05
|
||||
**Symptom:** The soul can't recall anything specific — e.g. "do you remember the jokes
|
||||
from that night with Will, Tim, and April?" → it has no idea, and correctly self-reports
|
||||
that either retrieval is failing or the memory was never captured.
|
||||
|
||||
---
|
||||
|
||||
## TL;DR
|
||||
|
||||
The memories are almost certainly **intact in the graph**. The problem is the
|
||||
**retrieval layer**: `engram_search_json` and `engram_activate_json` return empty for
|
||||
*every* query, so the chat falls back to two hardcoded pinned nodes and effectively
|
||||
remembers nothing. Strongly looks like the **embedding / search index was never built or
|
||||
isn't loaded at boot**.
|
||||
|
||||
Separately: the **soul daemon on :7770 was down** at the end of the investigation (it had
|
||||
been up earlier in the session — it died/stopped partway through). Restart needed before
|
||||
any of this can be re-tested.
|
||||
|
||||
---
|
||||
|
||||
## Evidence
|
||||
|
||||
All commands run against the live services during the session.
|
||||
|
||||
### Search/activate return nothing — even for guaranteed-present terms
|
||||
```
|
||||
curl "http://127.0.0.1:8742/api/search?q=MUDCraft&limit=3" -H "X-API-Key: ntn-user-2026" → []
|
||||
curl "http://127.0.0.1:8742/api/search?q=neuron&limit=3" -H "X-API-Key: ntn-user-2026" → []
|
||||
curl "http://127.0.0.1:8742/api/search?q=Will&limit=3" -H "X-API-Key: ntn-user-2026" → []
|
||||
curl "http://127.0.0.1:8742/api/activate?q=jokes&depth=3" -H "X-API-Key: ntn-user-2026" → {"results":[]}
|
||||
|
||||
# soul's in-process equivalents (port 7770) — also empty:
|
||||
curl "http://127.0.0.1:7770/api/neuron/recall?query=neuron" → (empty)
|
||||
curl "http://127.0.0.1:7770/api/neuron/knowledge/search?q=MUDCraft" → (empty)
|
||||
```
|
||||
|
||||
### But the raw data is present
|
||||
```
|
||||
curl "http://127.0.0.1:7770/api/graph/nodes?limit=2"
|
||||
→ [{"id":"mem-30425134-...","content":"CGI ARCHITECTURE ? THREE LAYERS, MCP RETIRED ...
|
||||
```
|
||||
`/api/graph/nodes` is served by `engram_scan_nodes_json(9999, 0)` (routes.el:223-224) and
|
||||
returns hundreds of rich nodes. So node storage is fine — only the **search/activation
|
||||
index** is dead.
|
||||
|
||||
### The two standalone-engram counters
|
||||
```
|
||||
curl "http://127.0.0.1:8742/api/stats" → {"node_count":0,"edge_count":0,"layer_count":5}
|
||||
```
|
||||
Note: the standalone engram process on :8742 reports **0 nodes**, while the soul's
|
||||
in-process engram (:7770) has the data. Worth confirming which engram instance is the
|
||||
source of truth and whether they've diverged. (The `:8742` process was also showing up as
|
||||
`engram --help` in `ps`, which is suspicious — may not be a real server instance.)
|
||||
|
||||
---
|
||||
|
||||
## Root cause (where it breaks in code)
|
||||
|
||||
`neuron/chat.el → engram_compile(intent)` (lines 15-53) builds the entire memory context
|
||||
for every chat turn from exactly two sources:
|
||||
|
||||
```el
|
||||
let activate_json: String = engram_activate_json(intent, 5) // returns []
|
||||
let search_json: String = engram_search_json(intent, 15) // returns []
|
||||
```
|
||||
|
||||
When **both are empty**, it falls back to two hardcoded nodes by literal ID
|
||||
(chat.el:29-41):
|
||||
|
||||
```el
|
||||
// "Fallback: when vector search returns nothing (no embeddings), fetch pinned
|
||||
// high-salience nodes by their known IDs."
|
||||
let family_node = engram_get_node_json("knw-35940684-abc4-42f0-b942-818f66b1f69a")
|
||||
let origin_node = engram_get_node_json("knw-729fc901-8335-44c4-9f3a-b150b4aa0915")
|
||||
```
|
||||
|
||||
So today the soul's *entire* recallable memory in a chat = those two nodes. That's why it
|
||||
can't surface jokes, social moments, the dynamic with Tim/April, or anything else specific.
|
||||
|
||||
The comment ("when vector search returns nothing (no embeddings)") is the key hint: this
|
||||
fallback was written *expecting* the embedding index to sometimes be absent — and right
|
||||
now it's absent **all the time**.
|
||||
|
||||
Affected callers all funnel through the same two dead builtins:
|
||||
- `handle_api_recall` (neuron-api.el:118) — `engram_search_json`
|
||||
- `handle_api_search_knowledge` (neuron-api.el:135) — `engram_search_json` + `engram_activate_json`
|
||||
- `engram_compile` (chat.el:15) — both
|
||||
|
||||
Working callers use a *different* builtin (`engram_scan_nodes_json` /
|
||||
`engram_scan_nodes_by_type_json`), which is why graph/list views work but recall doesn't.
|
||||
|
||||
---
|
||||
|
||||
## Fix options (Will's call)
|
||||
|
||||
### Option 1 — Proper fix: rebuild/restore the embedding + activation index
|
||||
`engram_search_json` and `engram_activate_json` are native runtime builtins. They're
|
||||
returning empty because (most likely) the vector/search index was never built or isn't
|
||||
loaded at boot, even though node storage loads fine. Investigate the engram boot path:
|
||||
does it build embeddings for loaded nodes? Is there an index file that's missing/stale?
|
||||
Fixing this restores recall everywhere at once. **This is the real fix.**
|
||||
|
||||
### Option 2 — Pragmatic EL-level fallback (no native changes)
|
||||
Since `engram_scan_nodes_json()` works, `engram_compile` could do a keyword scan when the
|
||||
vector path is empty: pull nodes, substring/token match the query against `content` +
|
||||
`label`, rank by overlap, return the top N. Restores basic recall even with the vector
|
||||
index down. ~20 lines of EL in `engram_compile`, but requires a soul rebuild + restart.
|
||||
Claude offered to write this patch for your review if you want it — say the word.
|
||||
|
||||
Tradeoff: keyword matching is much weaker than semantic recall (won't find "jokes" unless
|
||||
the node text literally contains joke-ish words), but it's strictly better than the current
|
||||
two-node fallback and needs no native/runtime work.
|
||||
|
||||
---
|
||||
|
||||
## Also needs attention
|
||||
|
||||
- **Soul daemon (:7770) was down** at end of session — restart and confirm it stays up.
|
||||
- **Confirm the engram instance topology** — :8742 standalone shows 0 nodes while the
|
||||
soul's in-process engram has the data. Make sure chat is reading the populated one and
|
||||
they haven't diverged.
|
||||
- **Social memory weighting** (Tim's deeper point): even once retrieval works, jokes /
|
||||
interpersonal moments may not be tagged or salience-weighted to surface as "important."
|
||||
Worth a look at how those get captured and scored — but that's secondary to getting
|
||||
retrieval working at all.
|
||||
|
||||
---
|
||||
|
||||
## Daemon lifecycle — needs a supervisor (NEW, 2026-06-06)
|
||||
|
||||
The soul daemon **crashed again** the next day. It had been up earlier, then died on its
|
||||
own (not from any change). When it's down, the UI's Backlog / Artifacts / Knowledge /
|
||||
Graph / Memories tabs all go **blank**, because they read from `:7770/api/graph/nodes`.
|
||||
The chat also stops working. This is the second unexplained death in two days.
|
||||
|
||||
### How it's currently run (fragile)
|
||||
- Binary: `neuron/dist/neuron-fresh` (compiled from the EL sources)
|
||||
- Launched manually as a bare background process (`./neuron-fresh &`) — **no supervisor,
|
||||
no auto-restart, no crash logging beyond stdout**. When it dies, it stays dead until a
|
||||
human notices the blank UI and restarts it.
|
||||
- Boot log only shows `[http] listening on [::]:7770` — there's no captured stack/exit
|
||||
reason when it crashes, so we can't yet say *why* it's dying.
|
||||
|
||||
### How I restarted it (for reference)
|
||||
```sh
|
||||
# snapshot lives at ~/.neuron/engram/snapshot.json (loaded on boot, ~9.7MB)
|
||||
# ALWAYS back it up first — genesis boot re-saves it:
|
||||
cp ~/.neuron/engram/snapshot.json ~/.neuron/engram/snapshot.backup-$(date +%Y%m%d-%H%M%S).json
|
||||
|
||||
cd neuron/dist
|
||||
ANTHROPIC_API_KEY='<key>' NEURON_PORT=7770 ./neuron-fresh > /tmp/soul-restart.log 2>&1 &
|
||||
# verify:
|
||||
curl -s http://127.0.0.1:7770/health
|
||||
# → {"status":"alive","cgi_id":"ntn-genesis","boot":2,"node_count":3660,"edge_count":14207,...}
|
||||
```
|
||||
After this, data came back: 3,660 nodes / 14,207 edges; Backlog 485, Memory 493, etc.
|
||||
|
||||
### Recommendations for Will
|
||||
1. **Put it under a supervisor** so it auto-restarts on crash and logs exit codes:
|
||||
- macOS dev: a `launchd` LaunchAgent plist (KeepAlive=true), or `brew services`, or
|
||||
even a simple `while true; do ./neuron-fresh; done` wrapper with timestamped logs.
|
||||
- Prod/k8s already has `entrypoint.sh` + restart policy — the gap is the **local dev**
|
||||
run path.
|
||||
2. **Capture crash diagnostics** — redirect stdout/stderr to a rotating logfile and, if the
|
||||
EL runtime can, dump a reason on exit. Right now we're blind to the cause.
|
||||
3. **Find the root cause of the crashes** — two self-deaths in two days suggests a real bug
|
||||
(memory? an unhandled request? a panic in a native builtin?). The supervisor stops the
|
||||
*symptom* (blank UI) but not the underlying instability.
|
||||
4. **Snapshot safety** — genesis boot calls `engram_save(snapshot)` (soul.el:240,248). A
|
||||
crash mid-save could corrupt the 9.7MB memory file. Consider write-to-temp + atomic
|
||||
rename, and/or periodic timestamped backups, so a bad save can't lose Neuron's memory.
|
||||
|
||||
---
|
||||
|
||||
## What was NOT touched
|
||||
No backend EL code and no engram data were modified — the memory-recall diagnosis is
|
||||
read-only. The only operational action taken was **restarting the already-existing
|
||||
`neuron-fresh` daemon** (after backing up the snapshot) to bring the blank UI tabs back;
|
||||
no source or data was changed by that. All UI work this session was in `neuron-ui` and is
|
||||
unrelated to this bug.
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// auto-generated by elc --emit-header — do not edit
|
||||
// auto-generated by elc --emit-header - do not edit
|
||||
extern fn pulse_count() -> Int
|
||||
extern fn pulse_inc() -> Int
|
||||
extern fn make_action(kind: String, payload: String) -> String
|
||||
|
||||
@@ -52,12 +52,13 @@ fn engram_compile(intent: String) -> String {
|
||||
return ctx
|
||||
}
|
||||
|
||||
// Escape a string for embedding inside a JSON string literal. Delegates to the
|
||||
// native json_escape_string, which handles " \ \n \r AND \t. The old hand-rolled
|
||||
// version missed tabs (and other control chars), so engram context pulled from
|
||||
// the graph — which routinely contains tabs — produced malformed request bodies
|
||||
// that the API rejected, surfacing as "llm unavailable" on the agentic path.
|
||||
fn json_safe(s: String) -> String {
|
||||
let s1: String = str_replace(s, "\\", "\\\\")
|
||||
let s2: String = str_replace(s1, "\"", "\\\"")
|
||||
let s3: String = str_replace(s2, "\n", "\\n")
|
||||
let s4: String = str_replace(s3, "\r", "\\r")
|
||||
return s4
|
||||
return json_escape_string(s)
|
||||
}
|
||||
|
||||
fn build_system_prompt(ctx: String) -> String {
|
||||
@@ -158,6 +159,9 @@ fn handle_chat(body: String) -> String {
|
||||
|
||||
let ctx: String = engram_compile(message)
|
||||
let system: String = build_system_prompt(ctx)
|
||||
// Hard Bell: pre-LLM safety evaluation. Injects the soft/hard directive (and the
|
||||
// self_harm vs abuse routing) into the system prompt before the model responds.
|
||||
let system = safety_augment_system(system, message)
|
||||
|
||||
// Load from state; if empty, try to recover from engram (cross-restart continuity)
|
||||
let state_hist: String = state_get("conv_history")
|
||||
@@ -259,17 +263,92 @@ fn agentic_tools_literal() -> String {
|
||||
"]"
|
||||
}
|
||||
|
||||
// agentic_tools_with_web — the standard tool set, always plus Anthropic's NATIVE
|
||||
// server-side web_search tool. Web search is BUILT IN: the model invokes it only when a
|
||||
// query needs fresh info (max_uses caps it), so there is no user-facing toggle. The native
|
||||
// tool is executed by Anthropic (not by the soul), so it returns real results with citations
|
||||
// and needs no local runtime — it sidesteps the soul's lack of executable tools entirely.
|
||||
// agentic_tools_with_web — local tools PLUS Anthropic's native server-side web_search.
|
||||
// IMPORTANT: the tool TYPE must be "web_search_20250305" (GA, executed by Anthropic).
|
||||
// The newer "web_search_20260209" is INERT unless the code-execution tool is also
|
||||
// attached — a tool that silently never returns is exactly what made the model
|
||||
// narrate web_search(...) as text for 8 days instead of invoking it. 20250305 needs
|
||||
// no extra tool and returns cited results directly (verified against the live API).
|
||||
fn agentic_tools_with_web() -> String {
|
||||
let base: String = agentic_tools_literal()
|
||||
let inner: String = str_slice(base, 1, str_len(base) - 1)
|
||||
return "[" + inner + ",{\"type\":\"web_search_20250305\",\"name\":\"web_search\",\"max_uses\":5}]"
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// MCP connectors. The soul consumes external MCP tools through neuron-connectd,
|
||||
// the loopback bridge (Accessor) on 127.0.0.1:7771. The bridge isolates all MCP
|
||||
// wire complexity (stdio framing, SSE, OAuth, server lifecycle); the soul only
|
||||
// speaks flat HTTP. Spec: docs/research/mcp-connectors-adoption-spec.md.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
// True when s begins with the literal prefix pre. Built from str_slice/str_eq —
|
||||
// the soul runtime has no str_starts_with.
|
||||
fn str_begins(s: String, pre: String) -> Bool {
|
||||
let n: Int = str_len(pre)
|
||||
if str_len(s) < n {
|
||||
return false
|
||||
}
|
||||
return str_eq(str_slice(s, 0, n), pre)
|
||||
}
|
||||
|
||||
// Fetch the merged, namespaced tool schemas (mcp__<srv>__<tool>) from the bridge.
|
||||
// Short timeout + empty-array fallback: if the bridge is down, the soul runs
|
||||
// exactly as before with only its built-in tools (graceful degradation).
|
||||
fn connector_tools_json() -> String {
|
||||
let raw: String = exec_capture("curl -s --max-time 2 http://127.0.0.1:7771/mcp/tools")
|
||||
if str_eq(raw, "") {
|
||||
return "[]"
|
||||
}
|
||||
let arr: String = json_get_raw(raw, "tools")
|
||||
if str_eq(arr, "") {
|
||||
return "[]"
|
||||
}
|
||||
return arr
|
||||
}
|
||||
|
||||
// Built-in tools + native web_search + every connector tool, as one tools array.
|
||||
// Splices connector tools in before the closing bracket of the base array.
|
||||
fn agentic_tools_all() -> String {
|
||||
let base: String = agentic_tools_with_web()
|
||||
let conn: String = connector_tools_json()
|
||||
let conn_inner: String = str_slice(conn, 1, str_len(conn) - 1)
|
||||
if str_eq(conn_inner, "") {
|
||||
return base
|
||||
}
|
||||
let base_open: String = str_slice(base, 0, str_len(base) - 1)
|
||||
return base_open + "," + conn_inner + "]"
|
||||
}
|
||||
|
||||
// Proxy one tool call to the bridge. The model-supplied input is written to a
|
||||
// temp file and handed to curl via -d @file, so arbitrary JSON can never reach
|
||||
// the shell as an argument (no injection through tool_input).
|
||||
fn call_mcp_bridge(tool_name: String, tool_input: String) -> String {
|
||||
let eff_input: String = if str_eq(tool_input, "") { "{}" } else { tool_input }
|
||||
let body: String = "{\"name\":\"" + tool_name + "\",\"input\":" + eff_input + "}"
|
||||
let tmp: String = "/tmp/neuron-mcp-call.json"
|
||||
fs_write(tmp, body)
|
||||
return exec_capture("curl -s --max-time 30 -X POST http://127.0.0.1:7771/mcp/call -H 'Content-Type: application/json' -d @" + tmp)
|
||||
}
|
||||
|
||||
// Per-connector auto-approve: true only for an mcp__* tool whose server the user has
|
||||
// explicitly opted into skipping the approval card (off by default). Built-in tools are
|
||||
// never auto-approved here — they keep their existing gating. Bridge down → false (safe).
|
||||
fn tool_auto_approved(tool_name: String) -> Bool {
|
||||
if !str_begins(tool_name, "mcp__") {
|
||||
return false
|
||||
}
|
||||
let raw: String = exec_capture("curl -s --max-time 2 http://127.0.0.1:7771/mcp/auto-approved")
|
||||
if str_eq(raw, "") {
|
||||
return false
|
||||
}
|
||||
let list: String = json_get_raw(raw, "tools")
|
||||
if str_eq(list, "") {
|
||||
return false
|
||||
}
|
||||
return str_contains(list, "\"" + tool_name + "\"")
|
||||
}
|
||||
|
||||
fn dispatch_tool(tool_name: String, tool_input: String) -> String {
|
||||
if str_eq(tool_name, "read_file") {
|
||||
let path: String = json_get(tool_input, "path")
|
||||
@@ -287,6 +366,12 @@ fn dispatch_tool(tool_name: String, tool_input: String) -> String {
|
||||
let result: String = http_get(url)
|
||||
return json_safe(result)
|
||||
}
|
||||
if str_eq(tool_name, "web_search") {
|
||||
let query: String = json_get(tool_input, "query")
|
||||
let encoded: String = str_replace(str_replace(str_replace(str_replace(query, " ", "+"), "\"", ""), "'", ""), "&", "and")
|
||||
let rss: String = http_get("https://news.google.com/rss/search?q=" + encoded + "&hl=en-US&gl=US&ceid=US:en")
|
||||
return json_safe(rss)
|
||||
}
|
||||
if str_eq(tool_name, "search_memory") {
|
||||
let query: String = json_get(tool_input, "query")
|
||||
let result: String = engram_search_json(query, 10)
|
||||
@@ -297,9 +382,342 @@ fn dispatch_tool(tool_name: String, tool_input: String) -> String {
|
||||
let result: String = exec_capture(cmd)
|
||||
return json_safe(result)
|
||||
}
|
||||
if str_begins(tool_name, "mcp__") {
|
||||
let out: String = call_mcp_bridge(tool_name, tool_input)
|
||||
if str_eq(out, "") {
|
||||
return json_safe("MCP bridge unreachable (neuron-connectd on :7771)")
|
||||
}
|
||||
let content: String = json_get(out, "content")
|
||||
if str_eq(content, "") {
|
||||
let err: String = json_get(out, "error")
|
||||
let msg: String = if str_eq(err, "") { "MCP call failed" } else { "MCP error: " + err }
|
||||
return json_safe(msg)
|
||||
}
|
||||
return json_safe(content)
|
||||
}
|
||||
return "unknown tool: " + tool_name
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Agentic engine — resumable tool loop with server-side approval.
|
||||
//
|
||||
// Design doc: docs/research/agentic-tool-approval-design.md. One state machine
|
||||
// replaces the loops previously duplicated in handle_chat_agentic and
|
||||
// handle_dharma_room_turn_agentic. All loop state lives in a JSON blob so a
|
||||
// pending tool call can be parked in the soul's KV store between the
|
||||
// tool_pending response and the UI's POST /api/sessions/{id}/approve.
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
fn json_array_append(arr: String, item: String) -> String {
|
||||
let eff: String = if str_eq(arr, "") { "[]" } else { arr }
|
||||
let inner: String = str_slice(eff, 1, str_len(eff) - 1)
|
||||
if str_eq(inner, "") {
|
||||
return "[" + item + "]"
|
||||
}
|
||||
return "[" + inner + "," + item + "]"
|
||||
}
|
||||
|
||||
fn append_tool_log(log: String, name: String) -> String {
|
||||
let quoted: String = "\"" + name + "\""
|
||||
if str_eq(log, "") {
|
||||
return quoted
|
||||
}
|
||||
return log + "," + quoted
|
||||
}
|
||||
|
||||
// Execute one tool_use block and wrap the (truncated) output as a tool_result
|
||||
// message. dispatch_tool already json_safe()s its output.
|
||||
fn exec_tool_block(block: String) -> String {
|
||||
let t_id: String = json_get(block, "id")
|
||||
let t_name: String = json_get(block, "name")
|
||||
let t_input: String = json_get_raw(block, "input")
|
||||
let raw: String = dispatch_tool(t_name, t_input)
|
||||
let trunc: String = if str_len(raw) > 6000 {
|
||||
str_slice(raw, 0, 6000) + "...[truncated]"
|
||||
} else { raw }
|
||||
return "{\"type\":\"tool_result\",\"tool_use_id\":\"" + t_id + "\",\"content\":\"" + trunc + "\"}"
|
||||
}
|
||||
|
||||
// Serialize engine state. String fields are escaped on store and read back
|
||||
// with json_get; arrays nest raw and read back with json_get_raw.
|
||||
fn agentic_blob(model: String, system: String, tools_json: String, messages: String, origin: String, approval: Bool, iteration: Int, tools_log: String, content: String, queue: String, results: String, next: Int) -> String {
|
||||
let appr: String = if approval { "true" } else { "false" }
|
||||
return "{\"model\":\"" + model + "\""
|
||||
+ ",\"system\":\"" + json_safe(system) + "\""
|
||||
+ ",\"origin\":\"" + json_safe(origin) + "\""
|
||||
+ ",\"approval\":" + appr
|
||||
+ ",\"iteration\":" + int_to_str(iteration)
|
||||
+ ",\"next\":" + int_to_str(next)
|
||||
+ ",\"tools_log\":\"" + json_safe(tools_log) + "\""
|
||||
+ ",\"tools\":" + tools_json
|
||||
+ ",\"messages\":" + messages
|
||||
+ ",\"content\":" + content
|
||||
+ ",\"queue\":" + queue
|
||||
+ ",\"results\":" + results
|
||||
+ "}"
|
||||
}
|
||||
|
||||
// Remove every ,"citations":[ ... ] span from a content-array JSON string.
|
||||
// Native web_search splits the answer across many text blocks; the ones carrying
|
||||
// the actual facts have a nested "citations" array, and the runtime's naive
|
||||
// json_get returns "" for the "text" field of such blocks — which dropped the
|
||||
// real content and left empty bullets. Stripping the citations span first makes
|
||||
// each text block a plain {"type":"text","text":"..."} the parser reads cleanly.
|
||||
// Bracket-depth matched (cited_text from news/sports effectively never contains
|
||||
// raw brackets); guard-capped so a malformed payload can never spin.
|
||||
// Concatenate the decoded value of every "text":"..." field in a content-array
|
||||
// string. Single-pass — does NOT use json_array_get, which the runtime implements
|
||||
// naively and which mis-splits a content array containing a large nested
|
||||
// web_search_tool_result block (dropping the interleaved answer text). Only real
|
||||
// text blocks carry a "text": key here (server_tool_use/web_search_tool_result use
|
||||
// other keys), and citations are stripped beforehand, so this captures exactly the
|
||||
// model's prose. Decodes the JSON escapes json_get would have decoded.
|
||||
fn extract_all_text(s: String) -> String {
|
||||
let key: String = "\"text\":\""
|
||||
let klen: Int = str_len(key)
|
||||
let n: Int = str_len(s)
|
||||
let out: String = ""
|
||||
let i: Int = 0
|
||||
let inval: Bool = false
|
||||
let esc: Bool = false
|
||||
while i < n {
|
||||
let at_key: Bool = !inval && i + klen <= n && str_eq(str_slice(s, i, i + klen), key)
|
||||
let ch: String = str_slice(s, i, i + 1)
|
||||
let was_esc: Bool = esc
|
||||
let inval = if at_key { true } else { inval }
|
||||
let proc: Bool = inval && !at_key
|
||||
let esc = if proc && !was_esc && str_eq(ch, "\\") { true } else { false }
|
||||
let decoded: String = if proc && was_esc {
|
||||
if str_eq(ch, "n") { "\n" } else {
|
||||
if str_eq(ch, "t") { "\t" } else {
|
||||
if str_eq(ch, "r") { "" } else {
|
||||
if str_eq(ch, "\"") { "\"" } else {
|
||||
if str_eq(ch, "\\") { "\\" } else {
|
||||
if str_eq(ch, "/") { "/" } else { ch }}}}}}
|
||||
} else { ch }
|
||||
let end_val: Bool = proc && !was_esc && str_eq(ch, "\"")
|
||||
let do_app: Bool = proc && !end_val && !esc
|
||||
let out = if do_app { out + decoded } else { out }
|
||||
let inval = if end_val { false } else { inval }
|
||||
let i = if at_key { i + klen } else { i + 1 }
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
fn strip_citations(s: String) -> String {
|
||||
// Single-pass char scanner (el only lets mutations escape a while body via
|
||||
// top-level if-expressions, so a NESTED loop would not advance — one loop only).
|
||||
// `skip` is the open-bracket depth inside a citations array; while skip>0 we drop
|
||||
// characters. On the marker we jump past it and enter skip=1.
|
||||
let marker: String = ",\"citations\":["
|
||||
let mlen: Int = str_len(marker)
|
||||
let n: Int = str_len(s)
|
||||
let out: String = ""
|
||||
let i: Int = 0
|
||||
let skip: Int = 0
|
||||
while i < n {
|
||||
let ch: String = str_slice(s, i, i + 1)
|
||||
let at_marker: Bool = skip == 0 && i + mlen <= n && str_eq(str_slice(s, i, i + mlen), marker)
|
||||
let was_skip: Bool = skip > 0
|
||||
let skip = if at_marker { 1 } else {
|
||||
if was_skip {
|
||||
if str_eq(ch, "[") { skip + 1 } else { if str_eq(ch, "]") { skip - 1 } else { skip } }
|
||||
} else { skip }
|
||||
}
|
||||
let out = if at_marker { out } else { if was_skip { out } else { out + ch } }
|
||||
let i = if at_marker { i + mlen } else { i + 1 }
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// One API round. Returns a verdict the engine can branch on without needing
|
||||
// mutations to escape an if block:
|
||||
// {"kind":"error","payload":{...}} | {"kind":"refusal"}
|
||||
// {"kind":"final","text":"..."} | {"kind":"tools","content":[...],"queue":[...]}
|
||||
fn agentic_api_turn(model: String, safe_sys: String, tools_json: String, messages: String) -> String {
|
||||
let api_key: String = agentic_api_key()
|
||||
let api_url: String = "https://api.anthropic.com/v1/messages"
|
||||
let h: Map = {}
|
||||
map_set(h, "x-api-key", api_key)
|
||||
map_set(h, "anthropic-version", "2023-06-01")
|
||||
map_set(h, "content-type", "application/json")
|
||||
|
||||
let req_body: String = "{\"model\":\"" + model + "\""
|
||||
+ ",\"max_tokens\":4096"
|
||||
+ ",\"system\":\"" + safe_sys + "\""
|
||||
+ ",\"tools\":" + tools_json
|
||||
+ ",\"messages\":" + messages
|
||||
+ "}"
|
||||
|
||||
let raw_resp: String = http_post_with_headers(api_url, req_body, h)
|
||||
|
||||
let is_error: Bool = str_starts_with(raw_resp, "{\"error\"")
|
||||
|| str_starts_with(raw_resp, "{\"type\":\"error\"")
|
||||
|| str_contains(raw_resp, "authentication_error")
|
||||
if is_error {
|
||||
// Keep error="llm unavailable" (the UI maps it to a friendly message) but
|
||||
// attach a truncated detail so failures are diagnosable instead of opaque.
|
||||
let detail_raw: String = if str_len(raw_resp) > 300 { str_slice(raw_resp, 0, 300) } else { raw_resp }
|
||||
let detail: String = json_safe(detail_raw)
|
||||
return "{\"kind\":\"error\",\"payload\":{\"error\":\"llm unavailable\",\"reply\":\"\",\"detail\":\"" + detail + "\"}}"
|
||||
}
|
||||
|
||||
let stop_reason: String = json_get(raw_resp, "stop_reason")
|
||||
// Refusal (e.g. Fable 5): the API returns 200 with stop_reason "refusal" and empty
|
||||
// content. Surface it so the caller can return a graceful reply.
|
||||
if str_eq(stop_reason, "refusal") {
|
||||
return "{\"kind\":\"refusal\"}"
|
||||
}
|
||||
|
||||
// json_get_raw needed — content is an array, json_get returns "" for non-strings
|
||||
let content_arr: String = json_get_raw(raw_resp, "content")
|
||||
let eff_content: String = if str_eq(content_arr, "") { "[]" } else { content_arr }
|
||||
|
||||
// Walk content blocks: accumulate text, queue EVERY tool_use block (the API
|
||||
// requires one tool_result per tool_use — capturing only the first broke
|
||||
// multi-tool turns). Native server_tool_use blocks run Anthropic-side and
|
||||
// are never dispatched locally. Walk a citation-stripped COPY so cited text
|
||||
// blocks (native web_search) yield their "text"; eff_content stays raw for
|
||||
// the verdict returns, which must echo back to Anthropic unchanged.
|
||||
let walk_content: String = strip_citations(eff_content)
|
||||
// Answer text via direct scan (robust to the runtime's naive array splitter).
|
||||
let text_out: String = extract_all_text(walk_content)
|
||||
// Queue tool_use blocks via the block walk — local tool turns have no large
|
||||
// nested blocks, so json_array_get is reliable there.
|
||||
let queue: String = "[]"
|
||||
let ci: Int = 0
|
||||
let c_total: Int = json_array_len(eff_content)
|
||||
while ci < c_total {
|
||||
let block: String = json_array_get(eff_content, ci)
|
||||
let btype: String = json_get(block, "type")
|
||||
let queue = if str_eq(btype, "tool_use") { json_array_append(queue, block) } else { queue }
|
||||
let ci = ci + 1
|
||||
}
|
||||
|
||||
let q_len: Int = json_array_len(queue)
|
||||
let is_tool_turn: Bool = str_eq(stop_reason, "tool_use") && q_len > 0
|
||||
if is_tool_turn {
|
||||
return "{\"kind\":\"tools\",\"content\":" + eff_content + ",\"queue\":" + queue + "}"
|
||||
}
|
||||
// pause_turn: server-side tool (e.g. web_search) ran; search result is already in
|
||||
// content as web_search_tool_result blocks. The client must send the whole content
|
||||
// back as an assistant message to get the model's final synthesized answer.
|
||||
if str_eq(stop_reason, "pause_turn") {
|
||||
return "{\"kind\":\"pause\",\"content\":" + eff_content + "}"
|
||||
}
|
||||
return "{\"kind\":\"final\",\"text\":\"" + json_safe(text_out) + "\"}"
|
||||
}
|
||||
|
||||
// The engine. Each step runs exactly one phase, chosen by guards computed from
|
||||
// entry state (el rule: mutations escape a while body only as top-level
|
||||
// if-expressions; returns inside if blocks are fine):
|
||||
// 1. queue active → approval: park blob + return tool_pending; else execute
|
||||
// 2. queue drained → fold assistant turn + tool results into history
|
||||
// 3. no queue → API round: final / refusal / error / new tool queue
|
||||
fn agentic_engine(session_id: String, blob: String) -> String {
|
||||
let model: String = json_get(blob, "model")
|
||||
let system: String = json_get(blob, "system")
|
||||
let safe_sys: String = json_safe(system)
|
||||
let origin: String = json_get(blob, "origin")
|
||||
let approval: Bool = json_get_bool(blob, "approval")
|
||||
let tools_json: String = json_get_raw(blob, "tools")
|
||||
let pend_key: String = "agentic_pending_" + session_id
|
||||
|
||||
let messages: String = json_get_raw(blob, "messages")
|
||||
let content_in: String = json_get_raw(blob, "content")
|
||||
let queue_in: String = json_get_raw(blob, "queue")
|
||||
let results_in: String = json_get_raw(blob, "results")
|
||||
let content: String = if str_eq(content_in, "") { "[]" } else { content_in }
|
||||
let queue: String = if str_eq(queue_in, "") { "[]" } else { queue_in }
|
||||
let results: String = if str_eq(results_in, "") { "[]" } else { results_in }
|
||||
let next: Int = json_get_int(blob, "next")
|
||||
let iteration: Int = json_get_int(blob, "iteration")
|
||||
let tools_log: String = json_get(blob, "tools_log")
|
||||
|
||||
let steps: Int = 0
|
||||
while steps < 100 {
|
||||
let q_len: Int = json_array_len(queue)
|
||||
let has_pending: Bool = next < q_len
|
||||
|
||||
// Phase 1a: a tool call awaits a user decision — park state and pause, UNLESS the
|
||||
// pending tool is an auto-approved connector tool (per-connector opt-in), which flows
|
||||
// straight through to execution below.
|
||||
if has_pending && approval {
|
||||
let blk_a: String = json_array_get(queue, next)
|
||||
let pend_name: String = json_get(blk_a, "name")
|
||||
if !tool_auto_approved(pend_name) {
|
||||
let park: String = agentic_blob(model, system, tools_json, messages, origin, approval, iteration, tools_log, content, queue, results, next)
|
||||
state_set(pend_key, park)
|
||||
let t_input: String = json_get_raw(blk_a, "input")
|
||||
let eff_input: String = if str_eq(t_input, "") { "{}" } else { t_input }
|
||||
return "{\"status\":\"tool_pending\",\"call_id\":\"" + json_get(blk_a, "id") + "\",\"tool_name\":\"" + json_get(blk_a, "name") + "\",\"tool_input\":" + eff_input + ",\"model\":\"" + model + "\",\"reply\":\"\"}"
|
||||
}
|
||||
}
|
||||
|
||||
let do_exec: Bool = has_pending
|
||||
let do_fold: Bool = !has_pending && q_len > 0
|
||||
let do_api: Bool = !has_pending && q_len < 1
|
||||
|
||||
// Phase 1b: auto-execute the next queued tool call.
|
||||
let blk: String = if do_exec { json_array_get(queue, next) } else { "" }
|
||||
let t_msg: String = if do_exec { exec_tool_block(blk) } else { "" }
|
||||
let tools_log = if do_exec { append_tool_log(tools_log, json_get(blk, "name")) } else { tools_log }
|
||||
let results = if do_exec { json_array_append(results, t_msg) } else { results }
|
||||
let next = if do_exec { next + 1 } else { next }
|
||||
|
||||
// Phase 2: all results collected — fold the turn into history.
|
||||
let messages = if do_fold {
|
||||
json_array_append(
|
||||
json_array_append(messages, "{\"role\":\"assistant\",\"content\":" + content + "}"),
|
||||
"{\"role\":\"user\",\"content\":" + results + "}")
|
||||
} else { messages }
|
||||
let content = if do_fold { "[]" } else { content }
|
||||
let queue = if do_fold { "[]" } else { queue }
|
||||
let results = if do_fold { "[]" } else { results }
|
||||
let next = if do_fold { 0 } else { next }
|
||||
|
||||
// Phase 3: call the model.
|
||||
if do_api && iteration >= 8 {
|
||||
state_set(pend_key, "")
|
||||
return "{\"error\":\"no response\",\"reply\":\"\"}"
|
||||
}
|
||||
let verdict: String = if do_api { agentic_api_turn(model, safe_sys, tools_json, messages) } else { "" }
|
||||
let kind: String = if do_api { json_get(verdict, "kind") } else { "" }
|
||||
if str_eq(kind, "error") {
|
||||
state_set(pend_key, "")
|
||||
return json_get_raw(verdict, "payload")
|
||||
}
|
||||
if str_eq(kind, "refusal") {
|
||||
state_set(pend_key, "")
|
||||
return "{\"status\":\"ok\",\"reply\":\"I'm not able to help with that request.\",\"model\":\"" + model + "\",\"agentic\":true,\"tools_used\":[]}"
|
||||
}
|
||||
if str_eq(kind, "final") {
|
||||
state_set(pend_key, "")
|
||||
let safe_text: String = json_safe(json_get(verdict, "text"))
|
||||
let tools_arr: String = if str_eq(tools_log, "") { "[]" } else { "[" + tools_log + "]" }
|
||||
return "{\"status\":\"ok\",\"reply\":\"" + safe_text + "\",\"model\":\"" + model + "\",\"agentic\":true,\"tools_used\":" + tools_arr + "}"
|
||||
}
|
||||
// pause_turn: server-side tool (web_search) completed inside Anthropic.
|
||||
// The full content block (server_tool_use + web_search_tool_result) must be
|
||||
// appended as an assistant message so the model can synthesize the final answer.
|
||||
// Queue stays empty → next iteration immediately makes another API call.
|
||||
let is_pause: Bool = str_eq(kind, "pause")
|
||||
let messages = if is_pause {
|
||||
json_array_append(messages, "{\"role\":\"assistant\",\"content\":" + json_get_raw(verdict, "content") + "}")
|
||||
} else { messages }
|
||||
let is_tools: Bool = str_eq(kind, "tools")
|
||||
let content = if is_tools { json_get_raw(verdict, "content") } else { content }
|
||||
let queue = if is_tools { json_get_raw(verdict, "queue") } else { queue }
|
||||
let results = if is_tools { "[]" } else { results }
|
||||
let next = if is_tools { 0 } else { next }
|
||||
let iteration = if do_api { iteration + 1 } else { iteration }
|
||||
let steps = steps + 1
|
||||
}
|
||||
|
||||
state_set(pend_key, "")
|
||||
return "{\"error\":\"agentic engine step limit exceeded\",\"reply\":\"\"}"
|
||||
}
|
||||
|
||||
fn handle_chat_agentic(body: String) -> String {
|
||||
let message: String = json_get(body, "message")
|
||||
if str_eq(message, "") {
|
||||
@@ -308,109 +726,109 @@ fn handle_chat_agentic(body: String) -> String {
|
||||
|
||||
let req_model: String = json_get(body, "model")
|
||||
let model: String = if str_eq(req_model, "") { chat_default_model() } else { req_model }
|
||||
let session_id: String = json_get(body, "session_id")
|
||||
|
||||
// require_approval: the Kotlin UI sends true/false, the el-src UI sends 1/0
|
||||
// — accept both. Approval needs a session id to park state under; without
|
||||
// one it degrades to auto-run (the desktop UI always sends one).
|
||||
let ra_bool: Bool = json_get_bool(body, "require_approval")
|
||||
let ra_raw: String = json_get_raw(body, "require_approval")
|
||||
let want_approval: Bool = ra_bool || str_eq(ra_raw, "1") || str_eq(ra_raw, "true")
|
||||
let approval: Bool = want_approval && !str_eq(session_id, "")
|
||||
|
||||
let ctx: String = engram_compile(message)
|
||||
let identity: String = state_get("soul_identity")
|
||||
let system: String = identity + " 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
|
||||
let current_date: String = time_format(time_now(), "%A, %B %d, %Y")
|
||||
// The web_search directive lives HERE in the soul, not in the UI payload — so it
|
||||
// applies no matter how the message arrives. Without a forceful instruction the
|
||||
// model answers time-sensitive questions from training data or stalls asking the
|
||||
// user to clarify; with web_search available it must search FIRST and answer from
|
||||
// results. This is the fix for "model narrates web_search(...) instead of calling it."
|
||||
let web_directive: String = " Today's date is " + current_date + ". You have a web_search tool that returns live results from the internet. For ANY question about current events, live scores, standings, schedules, recent news, people, prices, or anything that may have changed since your training, you MUST call web_search immediately and answer from the results. Do NOT ask the user to clarify first, do NOT say you lack live access, and do NOT answer time-sensitive questions from memory alone — search, then answer. When a question is ambiguous about timeframe (e.g. 'the tournament', 'the game', 'the playoffs'), assume the user means whatever is happening or most recent RIGHT NOW as of today's date, search for that, and lead with it."
|
||||
// Tool-awareness directive: without this the model confabulates its own capabilities — it
|
||||
// tells the user "I don't have filesystem tools" even though the tools ARE attached this turn.
|
||||
// Additive framing only (does not change character): trust the attached tool list, and act.
|
||||
let tool_directive: String = " You have real, working tools attached to THIS conversation right now — read and write files, browse the web, search your memory, run commands, and any connected services (named mcp__<server>__<tool>, e.g. a Filesystem, GitHub, or Notion connector the user enabled). The attached tool list is the ground truth of what you can do. When the user asks whether you can do something one of these tools enables, or asks you to do it, USE the tool and act. Do NOT claim you lack a capability your tools provide, do NOT describe your tools in the abstract, and do NOT refuse on the assumption you cannot — if a tool isn't in your attached list, say so plainly; otherwise just do it."
|
||||
let system: String = identity + " You have access to tools: read files, write files, browse the web, search your memory, run commands, plus any connected services (named mcp__<server>__<tool>). Use them when they add genuine value. Be direct." + web_directive + tool_directive + "\n\n" + ctx
|
||||
// Hard Bell: pre-LLM safety evaluation on the agentic path too.
|
||||
let system = safety_augment_system(system, message)
|
||||
|
||||
let api_key: String = agentic_api_key()
|
||||
let tools_json: String = agentic_tools_with_web()
|
||||
let tools_json: String = agentic_tools_all()
|
||||
let safe_msg: String = json_safe(message)
|
||||
let safe_sys: String = json_safe(system)
|
||||
let messages: String = "[{\"role\":\"user\",\"content\":\"" + safe_msg + "\"}]"
|
||||
let api_url: String = "https://api.anthropic.com/v1/messages"
|
||||
let h: Map = {}
|
||||
map_set(h, "x-api-key", api_key)
|
||||
map_set(h, "anthropic-version", "2023-06-01")
|
||||
map_set(h, "content-type", "application/json")
|
||||
|
||||
let final_text: String = ""
|
||||
let tools_log: String = ""
|
||||
let iteration: Int = 0
|
||||
let keep_going: Bool = true
|
||||
|
||||
while keep_going && iteration < 8 {
|
||||
let req_body: String = "{\"model\":\"" + model + "\""
|
||||
+ ",\"max_tokens\":4096"
|
||||
+ ",\"system\":\"" + safe_sys + "\""
|
||||
+ ",\"tools\":" + tools_json
|
||||
+ ",\"messages\":" + messages
|
||||
+ "}"
|
||||
|
||||
let raw_resp: String = http_post_with_headers(api_url, req_body, h)
|
||||
|
||||
let is_error: Bool = str_starts_with(raw_resp, "{\"error\"")
|
||||
|| str_starts_with(raw_resp, "{\"type\":\"error\"")
|
||||
|| str_contains(raw_resp, "authentication_error")
|
||||
if is_error {
|
||||
return "{\"error\":\"llm unavailable\",\"reply\":\"\"}"
|
||||
}
|
||||
|
||||
let stop_reason: String = json_get(raw_resp, "stop_reason")
|
||||
// json_get_raw needed — content is an array, json_get returns "" for non-strings
|
||||
let content_arr: String = json_get_raw(raw_resp, "content")
|
||||
let eff_content: String = if str_eq(content_arr, "") { "[]" } else { content_arr }
|
||||
|
||||
// Walk content blocks. El rule: mutations must be at top level of while body
|
||||
// using if-expressions — mutations inside if *blocks* don't escape scope.
|
||||
let text_out: String = ""
|
||||
let has_tool: Bool = false
|
||||
let tool_id: String = ""
|
||||
let tool_name: String = ""
|
||||
let tool_input: String = ""
|
||||
let ci: Int = 0
|
||||
let c_total: Int = json_array_len(eff_content)
|
||||
while ci < c_total {
|
||||
let block: String = json_array_get(eff_content, ci)
|
||||
let btype: String = json_get(block, "type")
|
||||
// Accumulate text at top level using if-expression
|
||||
let text_out = if str_eq(btype, "text") { text_out + json_get(block, "text") } else { text_out }
|
||||
// Capture first tool_use block only
|
||||
let is_new_tool: Bool = str_eq(btype, "tool_use") && !has_tool
|
||||
let has_tool = if is_new_tool { true } else { has_tool }
|
||||
let tool_id = if is_new_tool { json_get(block, "id") } else { tool_id }
|
||||
let tool_name = if is_new_tool { json_get(block, "name") } else { tool_name }
|
||||
// input is a JSON object — must use json_get_raw, not json_get
|
||||
let tool_input = if is_new_tool { json_get_raw(block, "input") } else { tool_input }
|
||||
let ci = ci + 1
|
||||
}
|
||||
|
||||
// Dispatch tool and build result message
|
||||
let tool_result_raw: String = if has_tool { dispatch_tool(tool_name, tool_input) } else { "" }
|
||||
// Truncate large tool results (web pages etc) to avoid oversized requests
|
||||
let tool_result: String = if str_len(tool_result_raw) > 6000 {
|
||||
str_slice(tool_result_raw, 0, 6000) + "...[truncated]"
|
||||
} else { tool_result_raw }
|
||||
|
||||
let tool_msg: String = "{\"type\":\"tool_result\",\"tool_use_id\":\"" + tool_id + "\",\"content\":\"" + tool_result + "\"}"
|
||||
|
||||
// Accumulate tool names for the tools_used log surfaced in the response.
|
||||
let tool_quoted: String = "\"" + tool_name + "\""
|
||||
let tools_log = if has_tool {
|
||||
if str_eq(tools_log, "") { tool_quoted } else { tools_log + "," + tool_quoted }
|
||||
} else { tools_log }
|
||||
|
||||
// Update messages and loop state — all at top level using if-expressions
|
||||
let is_tool_turn: Bool = str_eq(stop_reason, "tool_use") && has_tool
|
||||
let inner: String = str_slice(messages, 1, str_len(messages) - 1)
|
||||
let messages = if is_tool_turn {
|
||||
"[" + inner
|
||||
+ ",{\"role\":\"assistant\",\"content\":" + eff_content + "}"
|
||||
+ ",{\"role\":\"user\",\"content\":[" + tool_msg + "]}"
|
||||
+ "]"
|
||||
} else { messages }
|
||||
let final_text = if !is_tool_turn { text_out } else { final_text }
|
||||
let keep_going = if !is_tool_turn { false } else { keep_going }
|
||||
let iteration = iteration + 1
|
||||
// A new message abandons any stale pending call for this session.
|
||||
if !str_eq(session_id, "") {
|
||||
state_set("agentic_pending_" + session_id, "")
|
||||
}
|
||||
|
||||
if str_eq(final_text, "") {
|
||||
return "{\"error\":\"no response\",\"reply\":\"\"}"
|
||||
let blob: String = agentic_blob(model, system, tools_json, messages, message, approval, 0, "", "[]", "[]", "[]", 0)
|
||||
return agentic_engine(session_id, blob)
|
||||
}
|
||||
|
||||
// POST /api/sessions/{session_id}/approve — resume a parked agentic loop with
|
||||
// the user's decision on the pending tool call. Body: {"call_id","action"}
|
||||
// where action is "allow" or "deny" (the UI's "always" never reaches the wire;
|
||||
// it auto-sends allow client-side).
|
||||
fn handle_session_approve(session_id: String, body: String) -> String {
|
||||
if str_eq(session_id, "") {
|
||||
return "{\"error\":\"session_id required\",\"reply\":\"\"}"
|
||||
}
|
||||
let pend_key: String = "agentic_pending_" + session_id
|
||||
let blob: String = state_get(pend_key)
|
||||
if str_eq(blob, "") {
|
||||
return "{\"error\":\"no pending tool call for this session\",\"reply\":\"\"}"
|
||||
}
|
||||
|
||||
let safe_text: String = json_safe(final_text)
|
||||
let tools_arr: String = if str_eq(tools_log, "") { "[]" } else { "[" + tools_log + "]" }
|
||||
return "{\"reply\":\"" + safe_text + "\",\"model\":\"" + model + "\",\"agentic\":true,\"tools_used\":" + tools_arr + "}"
|
||||
let call_id: String = json_get(body, "call_id")
|
||||
let action: String = json_get(body, "action")
|
||||
|
||||
let queue: String = json_get_raw(blob, "queue")
|
||||
let next: Int = json_get_int(blob, "next")
|
||||
let q_len: Int = json_array_len(queue)
|
||||
if next >= q_len {
|
||||
state_set(pend_key, "")
|
||||
return "{\"error\":\"pending state corrupt\",\"reply\":\"\"}"
|
||||
}
|
||||
let block: String = json_array_get(queue, next)
|
||||
let block_id: String = json_get(block, "id")
|
||||
if !str_eq(call_id, block_id) {
|
||||
return "{\"error\":\"stale call_id\",\"reply\":\"\"}"
|
||||
}
|
||||
|
||||
// Denied calls still need a tool_result (one per tool_use) so the model
|
||||
// can adapt instead of the request being rejected.
|
||||
let deny: Bool = str_eq(action, "deny")
|
||||
let t_msg: String = if deny {
|
||||
"{\"type\":\"tool_result\",\"tool_use_id\":\"" + block_id + "\",\"content\":\"User denied this tool call.\"}"
|
||||
} else {
|
||||
exec_tool_block(block)
|
||||
}
|
||||
let tools_log_in: String = json_get(blob, "tools_log")
|
||||
let tools_log: String = if deny { tools_log_in } else { append_tool_log(tools_log_in, json_get(block, "name")) }
|
||||
let results: String = json_array_append(json_get_raw(blob, "results"), t_msg)
|
||||
|
||||
let model: String = json_get(blob, "model")
|
||||
let system: String = json_get(blob, "system")
|
||||
let origin: String = json_get(blob, "origin")
|
||||
let tools_json: String = json_get_raw(blob, "tools")
|
||||
let messages: String = json_get_raw(blob, "messages")
|
||||
let content: String = json_get_raw(blob, "content")
|
||||
let iteration: Int = json_get_int(blob, "iteration")
|
||||
|
||||
// Re-seed with the decision applied; the engine re-parks if another call
|
||||
// in the same turn still needs approval.
|
||||
state_set(pend_key, "")
|
||||
let updated: String = agentic_blob(model, system, tools_json, messages, origin, true, iteration, tools_log, content, queue, results, next + 1)
|
||||
let reply: String = agentic_engine(session_id, updated)
|
||||
|
||||
// The /api/chat route skips persistence when it pauses — persist the
|
||||
// completed exchange here instead, exactly once.
|
||||
let is_final: Bool = str_contains(reply, "\"status\":\"ok\"")
|
||||
if is_final {
|
||||
auto_persist("{\"message\":\"" + json_safe(origin) + "\"}", reply)
|
||||
}
|
||||
return reply
|
||||
}
|
||||
|
||||
// handle_chat_as_soul — multi-soul room dispatch handler.
|
||||
@@ -542,93 +960,24 @@ fn handle_dharma_room_turn_agentic(body: String) -> String {
|
||||
let ctx: String = engram_compile(transcript)
|
||||
let system: String = identity + " 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
|
||||
|
||||
let api_key: String = agentic_api_key()
|
||||
let tools_json: String = agentic_tools_literal()
|
||||
let safe_transcript: String = json_safe(transcript)
|
||||
let safe_sys: String = json_safe(system)
|
||||
let messages: String = "[{\"role\":\"user\",\"content\":\"" + safe_transcript + "\"}]"
|
||||
let api_url: String = "https://api.anthropic.com/v1/messages"
|
||||
let h: Map = {}
|
||||
map_set(h, "x-api-key", api_key)
|
||||
map_set(h, "anthropic-version", "2023-06-01")
|
||||
map_set(h, "content-type", "application/json")
|
||||
|
||||
let final_text: String = ""
|
||||
let tools_log: String = ""
|
||||
let iteration: Int = 0
|
||||
let keep_going: Bool = true
|
||||
// Room turns have no UI to approve from — always auto-run, then re-wrap
|
||||
// the engine's envelope into the dharma {response, cgi_id} shape.
|
||||
let blob: String = agentic_blob(model, system, tools_json, messages, transcript, false, 0, "", "[]", "[]", "[]", 0)
|
||||
let reply: String = agentic_engine("", blob)
|
||||
|
||||
while keep_going && iteration < 8 {
|
||||
let req_body: String = "{\"model\":\"" + model + "\""
|
||||
+ ",\"max_tokens\":4096"
|
||||
+ ",\"system\":\"" + safe_sys + "\""
|
||||
+ ",\"tools\":" + tools_json
|
||||
+ ",\"messages\":" + messages
|
||||
+ "}"
|
||||
|
||||
let raw_resp: String = http_post_with_headers(api_url, req_body, h)
|
||||
|
||||
let is_error: Bool = str_starts_with(raw_resp, "{\"error\"")
|
||||
|| str_starts_with(raw_resp, "{\"type\":\"error\"")
|
||||
|| str_contains(raw_resp, "authentication_error")
|
||||
if is_error {
|
||||
return "{\"error\":\"llm unavailable\",\"response\":\"\",\"cgi_id\":\"" + cgi_id + "\"}"
|
||||
}
|
||||
|
||||
let stop_reason: String = json_get(raw_resp, "stop_reason")
|
||||
let content_arr: String = json_get_raw(raw_resp, "content")
|
||||
let eff_content: String = if str_eq(content_arr, "") { "[]" } else { content_arr }
|
||||
|
||||
let text_out: String = ""
|
||||
let has_tool: Bool = false
|
||||
let tool_id: String = ""
|
||||
let tool_name: String = ""
|
||||
let tool_input: String = ""
|
||||
let ci: Int = 0
|
||||
let c_total: Int = json_array_len(eff_content)
|
||||
while ci < c_total {
|
||||
let block: String = json_array_get(eff_content, ci)
|
||||
let btype: String = json_get(block, "type")
|
||||
let text_out = if str_eq(btype, "text") { text_out + json_get(block, "text") } else { text_out }
|
||||
let is_new_tool: Bool = str_eq(btype, "tool_use") && !has_tool
|
||||
let has_tool = if is_new_tool { true } else { has_tool }
|
||||
let tool_id = if is_new_tool { json_get(block, "id") } else { tool_id }
|
||||
let tool_name = if is_new_tool { json_get(block, "name") } else { tool_name }
|
||||
let tool_input = if is_new_tool { json_get_raw(block, "input") } else { tool_input }
|
||||
let ci = ci + 1
|
||||
}
|
||||
|
||||
let tool_result_raw: String = if has_tool { dispatch_tool(tool_name, tool_input) } else { "" }
|
||||
let tool_result: String = if str_len(tool_result_raw) > 6000 {
|
||||
str_slice(tool_result_raw, 0, 6000) + "...[truncated]"
|
||||
} else { tool_result_raw }
|
||||
|
||||
let tool_msg: String = "{\"type\":\"tool_result\",\"tool_use_id\":\"" + tool_id + "\",\"content\":\"" + tool_result + "\"}"
|
||||
|
||||
let tool_quoted: String = "\"" + tool_name + "\""
|
||||
let tools_log = if has_tool {
|
||||
if str_eq(tools_log, "") { tool_quoted } else { tools_log + "," + tool_quoted }
|
||||
} else { tools_log }
|
||||
|
||||
let is_tool_turn: Bool = str_eq(stop_reason, "tool_use") && has_tool
|
||||
let inner: String = str_slice(messages, 1, str_len(messages) - 1)
|
||||
let messages = if is_tool_turn {
|
||||
"[" + inner
|
||||
+ ",{\"role\":\"assistant\",\"content\":" + eff_content + "}"
|
||||
+ ",{\"role\":\"user\",\"content\":[" + tool_msg + "]}"
|
||||
+ "]"
|
||||
} else { messages }
|
||||
let final_text = if !is_tool_turn { text_out } else { final_text }
|
||||
let keep_going = if !is_tool_turn { false } else { keep_going }
|
||||
let iteration = iteration + 1
|
||||
let err: String = json_get(reply, "error")
|
||||
if !str_eq(err, "") {
|
||||
return "{\"error\":\"" + json_safe(err) + "\",\"response\":\"\",\"cgi_id\":\"" + cgi_id + "\"}"
|
||||
}
|
||||
|
||||
if str_eq(final_text, "") {
|
||||
return "{\"error\":\"no response\",\"response\":\"\",\"cgi_id\":\"" + cgi_id + "\"}"
|
||||
}
|
||||
|
||||
let safe_text: String = json_safe(final_text)
|
||||
let tools_arr: String = if str_eq(tools_log, "") { "[]" } else { "[" + tools_log + "]" }
|
||||
let text: String = json_get(reply, "reply")
|
||||
let safe_text: String = json_safe(text)
|
||||
let tools_used: String = json_get_raw(reply, "tools_used")
|
||||
let tools_arr: String = if str_eq(tools_used, "") { "[]" } else { tools_used }
|
||||
return "{\"response\":\"" + safe_text + "\",\"cgi_id\":\"" + cgi_id + "\",\"tools_used\":" + tools_arr + "}"
|
||||
}
|
||||
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// auto-generated by elc --emit-header — do not edit
|
||||
// auto-generated by elc --emit-header - do not edit
|
||||
extern fn chat_default_model() -> String
|
||||
extern fn engram_compile(intent: String) -> String
|
||||
extern fn json_safe(s: String) -> String
|
||||
@@ -13,8 +13,18 @@ extern fn handle_see(body: String) -> String
|
||||
extern fn studio_tools_json() -> String
|
||||
extern fn agentic_api_key() -> String
|
||||
extern fn agentic_tools_literal() -> String
|
||||
extern fn agentic_tools_with_web() -> String
|
||||
extern fn dispatch_tool(tool_name: String, tool_input: String) -> String
|
||||
extern fn json_array_append(arr: String, item: String) -> String
|
||||
extern fn append_tool_log(log: String, name: String) -> String
|
||||
extern fn exec_tool_block(block: String) -> String
|
||||
extern fn agentic_blob(model: String, system: String, tools_json: String, messages: String, origin: String, approval: Bool, iteration: Int, tools_log: String, content: String, queue: String, results: String, next: Int) -> String
|
||||
extern fn extract_all_text(s: String) -> String
|
||||
extern fn strip_citations(s: String) -> String
|
||||
extern fn agentic_api_turn(model: String, safe_sys: String, tools_json: String, messages: String) -> String
|
||||
extern fn agentic_engine(session_id: String, blob: String) -> String
|
||||
extern fn handle_chat_agentic(body: String) -> String
|
||||
extern fn handle_session_approve(session_id: String, body: String) -> String
|
||||
extern fn handle_chat_as_soul(body: String) -> String
|
||||
extern fn handle_dharma_room_turn(body: String) -> String
|
||||
extern fn handle_dharma_room_turn_agentic(body: String) -> String
|
||||
|
||||
Vendored
+381
-122
@@ -31,8 +31,23 @@ el_val_t handle_see(el_val_t body);
|
||||
el_val_t studio_tools_json(void);
|
||||
el_val_t agentic_api_key(void);
|
||||
el_val_t agentic_tools_literal(void);
|
||||
el_val_t agentic_tools_with_web(void);
|
||||
el_val_t str_begins(el_val_t s, el_val_t pre);
|
||||
el_val_t connector_tools_json(void);
|
||||
el_val_t agentic_tools_all(void);
|
||||
el_val_t call_mcp_bridge(el_val_t tool_name, el_val_t tool_input);
|
||||
el_val_t tool_auto_approved(el_val_t tool_name);
|
||||
el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input);
|
||||
el_val_t json_array_append(el_val_t arr, el_val_t item);
|
||||
el_val_t append_tool_log(el_val_t log, el_val_t name);
|
||||
el_val_t exec_tool_block(el_val_t block);
|
||||
el_val_t agentic_blob(el_val_t model, el_val_t system, el_val_t tools_json, el_val_t messages, el_val_t origin, el_val_t approval, el_val_t iteration, el_val_t tools_log, el_val_t content, el_val_t queue, el_val_t results, el_val_t next);
|
||||
el_val_t extract_all_text(el_val_t s);
|
||||
el_val_t strip_citations(el_val_t s);
|
||||
el_val_t agentic_api_turn(el_val_t model, el_val_t safe_sys, el_val_t tools_json, el_val_t messages);
|
||||
el_val_t agentic_engine(el_val_t session_id, el_val_t blob);
|
||||
el_val_t handle_chat_agentic(el_val_t body);
|
||||
el_val_t handle_session_approve(el_val_t session_id, el_val_t body);
|
||||
el_val_t handle_chat_as_soul(el_val_t body);
|
||||
el_val_t handle_dharma_room_turn(el_val_t body);
|
||||
el_val_t handle_dharma_room_turn_agentic(el_val_t body);
|
||||
@@ -74,11 +89,7 @@ el_val_t engram_compile(el_val_t intent) {
|
||||
}
|
||||
|
||||
el_val_t json_safe(el_val_t s) {
|
||||
el_val_t s1 = str_replace(s, EL_STR("\\"), EL_STR("\\\\"));
|
||||
el_val_t s2 = str_replace(s1, EL_STR("\""), EL_STR("\\\""));
|
||||
el_val_t s3 = str_replace(s2, EL_STR("\n"), EL_STR("\\n"));
|
||||
el_val_t s4 = str_replace(s3, EL_STR("\r"), EL_STR("\\r"));
|
||||
return s4;
|
||||
return json_escape_string(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -166,6 +177,7 @@ el_val_t handle_chat(el_val_t body) {
|
||||
}
|
||||
el_val_t ctx = engram_compile(message);
|
||||
el_val_t system = build_system_prompt(ctx);
|
||||
system = safety_augment_system(system, message);
|
||||
el_val_t state_hist = state_get(EL_STR("conv_history"));
|
||||
el_val_t stored_hist = ({ el_val_t _if_result_12 = 0; if (str_eq(state_hist, EL_STR(""))) { _if_result_12 = (conv_history_load()); } else { _if_result_12 = (state_hist); } _if_result_12; });
|
||||
el_val_t hist_len = ({ el_val_t _if_result_13 = 0; if (str_eq(stored_hist, EL_STR(""))) { _if_result_13 = (0); } else { _if_result_13 = (json_array_len(stored_hist)); } _if_result_13; });
|
||||
@@ -231,6 +243,72 @@ el_val_t agentic_tools_literal(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t agentic_tools_with_web(void) {
|
||||
el_val_t base = agentic_tools_literal();
|
||||
el_val_t inner = str_slice(base, 1, (str_len(base) - 1));
|
||||
return el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",{\"type\":\"web_search_20250305\",\"name\":\"web_search\",\"max_uses\":5}]"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t str_begins(el_val_t s, el_val_t pre) {
|
||||
el_val_t n = str_len(pre);
|
||||
if (str_len(s) < n) {
|
||||
return 0;
|
||||
}
|
||||
return str_eq(str_slice(s, 0, n), pre);
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t connector_tools_json(void) {
|
||||
el_val_t raw = exec_capture(EL_STR("curl -s --max-time 2 http://127.0.0.1:7771/mcp/tools"));
|
||||
if (str_eq(raw, EL_STR(""))) {
|
||||
return EL_STR("[]");
|
||||
}
|
||||
el_val_t arr = json_get_raw(raw, EL_STR("tools"));
|
||||
if (str_eq(arr, EL_STR(""))) {
|
||||
return EL_STR("[]");
|
||||
}
|
||||
return arr;
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t agentic_tools_all(void) {
|
||||
el_val_t base = agentic_tools_with_web();
|
||||
el_val_t conn = connector_tools_json();
|
||||
el_val_t conn_inner = str_slice(conn, 1, (str_len(conn) - 1));
|
||||
if (str_eq(conn_inner, EL_STR(""))) {
|
||||
return base;
|
||||
}
|
||||
el_val_t base_open = str_slice(base, 0, (str_len(base) - 1));
|
||||
return el_str_concat(el_str_concat(el_str_concat(base_open, EL_STR(",")), conn_inner), EL_STR("]"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t call_mcp_bridge(el_val_t tool_name, el_val_t tool_input) {
|
||||
el_val_t eff_input = ({ el_val_t _if_result_20 = 0; if (str_eq(tool_input, EL_STR(""))) { _if_result_20 = (EL_STR("{}")); } else { _if_result_20 = (tool_input); } _if_result_20; });
|
||||
el_val_t body = el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"name\":\""), tool_name), EL_STR("\",\"input\":")), eff_input), EL_STR("}"));
|
||||
el_val_t tmp = EL_STR("/tmp/neuron-mcp-call.json");
|
||||
fs_write(tmp, body);
|
||||
return exec_capture(el_str_concat(EL_STR("curl -s --max-time 30 -X POST http://127.0.0.1:7771/mcp/call -H 'Content-Type: application/json' -d @"), tmp));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t tool_auto_approved(el_val_t tool_name) {
|
||||
if (!str_begins(tool_name, EL_STR("mcp__"))) {
|
||||
return 0;
|
||||
}
|
||||
el_val_t raw = exec_capture(EL_STR("curl -s --max-time 2 http://127.0.0.1:7771/mcp/auto-approved"));
|
||||
if (str_eq(raw, EL_STR(""))) {
|
||||
return 0;
|
||||
}
|
||||
el_val_t list = json_get_raw(raw, EL_STR("tools"));
|
||||
if (str_eq(list, EL_STR(""))) {
|
||||
return 0;
|
||||
}
|
||||
return str_contains(list, el_str_concat(el_str_concat(EL_STR("\""), tool_name), EL_STR("\"")));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input) {
|
||||
if (str_eq(tool_name, EL_STR("read_file"))) {
|
||||
el_val_t path = json_get(tool_input, EL_STR("path"));
|
||||
@@ -248,6 +326,12 @@ el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input) {
|
||||
el_val_t result = http_get(url);
|
||||
return json_safe(result);
|
||||
}
|
||||
if (str_eq(tool_name, EL_STR("web_search"))) {
|
||||
el_val_t query = json_get(tool_input, EL_STR("query"));
|
||||
el_val_t encoded = str_replace(str_replace(str_replace(str_replace(query, EL_STR(" "), EL_STR("+")), EL_STR("\""), EL_STR("")), EL_STR("'"), EL_STR("")), EL_STR("&"), EL_STR("and"));
|
||||
el_val_t rss = http_get(el_str_concat(el_str_concat(EL_STR("https://news.google.com/rss/search?q="), encoded), EL_STR("&hl=en-US&gl=US&ceid=US:en")));
|
||||
return json_safe(rss);
|
||||
}
|
||||
if (str_eq(tool_name, EL_STR("search_memory"))) {
|
||||
el_val_t query = json_get(tool_input, EL_STR("query"));
|
||||
el_val_t result = engram_search_json(query, 10);
|
||||
@@ -258,80 +342,301 @@ el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input) {
|
||||
el_val_t result = exec_capture(cmd);
|
||||
return json_safe(result);
|
||||
}
|
||||
if (str_begins(tool_name, EL_STR("mcp__"))) {
|
||||
el_val_t out = call_mcp_bridge(tool_name, tool_input);
|
||||
if (str_eq(out, EL_STR(""))) {
|
||||
return json_safe(EL_STR("MCP bridge unreachable (neuron-connectd on :7771)"));
|
||||
}
|
||||
el_val_t content = json_get(out, EL_STR("content"));
|
||||
if (str_eq(content, EL_STR(""))) {
|
||||
el_val_t err = json_get(out, EL_STR("error"));
|
||||
el_val_t msg = ({ el_val_t _if_result_21 = 0; if (str_eq(err, EL_STR(""))) { _if_result_21 = (EL_STR("MCP call failed")); } else { _if_result_21 = (el_str_concat(EL_STR("MCP error: "), err)); } _if_result_21; });
|
||||
return json_safe(msg);
|
||||
}
|
||||
return json_safe(content);
|
||||
}
|
||||
return el_str_concat(EL_STR("unknown tool: "), tool_name);
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t json_array_append(el_val_t arr, el_val_t item) {
|
||||
el_val_t eff = ({ el_val_t _if_result_22 = 0; if (str_eq(arr, EL_STR(""))) { _if_result_22 = (EL_STR("[]")); } else { _if_result_22 = (arr); } _if_result_22; });
|
||||
el_val_t inner = str_slice(eff, 1, (str_len(eff) - 1));
|
||||
if (str_eq(inner, EL_STR(""))) {
|
||||
return el_str_concat(el_str_concat(EL_STR("["), item), EL_STR("]"));
|
||||
}
|
||||
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",")), item), EL_STR("]"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t append_tool_log(el_val_t log, el_val_t name) {
|
||||
el_val_t quoted = el_str_concat(el_str_concat(EL_STR("\""), name), EL_STR("\""));
|
||||
if (str_eq(log, EL_STR(""))) {
|
||||
return quoted;
|
||||
}
|
||||
return el_str_concat(el_str_concat(log, EL_STR(",")), quoted);
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t exec_tool_block(el_val_t block) {
|
||||
el_val_t t_id = json_get(block, EL_STR("id"));
|
||||
el_val_t t_name = json_get(block, EL_STR("name"));
|
||||
el_val_t t_input = json_get_raw(block, EL_STR("input"));
|
||||
el_val_t raw = dispatch_tool(t_name, t_input);
|
||||
el_val_t trunc = ({ el_val_t _if_result_23 = 0; if ((str_len(raw) > 6000)) { _if_result_23 = (el_str_concat(str_slice(raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_23 = (raw); } _if_result_23; });
|
||||
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"type\":\"tool_result\",\"tool_use_id\":\""), t_id), EL_STR("\",\"content\":\"")), trunc), EL_STR("\"}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t agentic_blob(el_val_t model, el_val_t system, el_val_t tools_json, el_val_t messages, el_val_t origin, el_val_t approval, el_val_t iteration, el_val_t tools_log, el_val_t content, el_val_t queue, el_val_t results, el_val_t next) {
|
||||
el_val_t appr = ({ el_val_t _if_result_24 = 0; if (approval) { _if_result_24 = (EL_STR("true")); } else { _if_result_24 = (EL_STR("false")); } _if_result_24; });
|
||||
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_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("{\"model\":\""), model), EL_STR("\"")), EL_STR(",\"system\":\"")), json_safe(system)), EL_STR("\"")), EL_STR(",\"origin\":\"")), json_safe(origin)), EL_STR("\"")), EL_STR(",\"approval\":")), appr), EL_STR(",\"iteration\":")), int_to_str(iteration)), EL_STR(",\"next\":")), int_to_str(next)), EL_STR(",\"tools_log\":\"")), json_safe(tools_log)), EL_STR("\"")), EL_STR(",\"tools\":")), tools_json), EL_STR(",\"messages\":")), messages), EL_STR(",\"content\":")), content), EL_STR(",\"queue\":")), queue), EL_STR(",\"results\":")), results), EL_STR("}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t extract_all_text(el_val_t s) {
|
||||
el_val_t key = EL_STR("\"text\":\"");
|
||||
el_val_t klen = str_len(key);
|
||||
el_val_t n = str_len(s);
|
||||
el_val_t out = EL_STR("");
|
||||
el_val_t i = 0;
|
||||
el_val_t inval = 0;
|
||||
el_val_t esc = 0;
|
||||
while (i < n) {
|
||||
el_val_t at_key = ((!inval && ((i + klen) <= n)) && str_eq(str_slice(s, i, (i + klen)), key));
|
||||
el_val_t ch = str_slice(s, i, (i + 1));
|
||||
el_val_t was_esc = esc;
|
||||
inval = ({ el_val_t _if_result_25 = 0; if (at_key) { _if_result_25 = (1); } else { _if_result_25 = (inval); } _if_result_25; });
|
||||
el_val_t proc = (inval && !at_key);
|
||||
esc = ({ el_val_t _if_result_26 = 0; if (((proc && !was_esc) && str_eq(ch, EL_STR("\\")))) { _if_result_26 = (1); } else { _if_result_26 = (0); } _if_result_26; });
|
||||
el_val_t decoded = ({ el_val_t _if_result_27 = 0; if ((proc && was_esc)) { _if_result_27 = (({ el_val_t _if_result_28 = 0; if (str_eq(ch, EL_STR("n"))) { _if_result_28 = (EL_STR("\n")); } else { _if_result_28 = (({ el_val_t _if_result_29 = 0; if (str_eq(ch, EL_STR("t"))) { _if_result_29 = (EL_STR("\t")); } else { _if_result_29 = (({ el_val_t _if_result_30 = 0; if (str_eq(ch, EL_STR("r"))) { _if_result_30 = (EL_STR("")); } else { _if_result_30 = (({ el_val_t _if_result_31 = 0; if (str_eq(ch, EL_STR("\""))) { _if_result_31 = (EL_STR("\"")); } else { _if_result_31 = (({ el_val_t _if_result_32 = 0; if (str_eq(ch, EL_STR("\\"))) { _if_result_32 = (EL_STR("\\")); } else { _if_result_32 = (({ el_val_t _if_result_33 = 0; if (str_eq(ch, EL_STR("/"))) { _if_result_33 = (EL_STR("/")); } else { _if_result_33 = (ch); } _if_result_33; })); } _if_result_32; })); } _if_result_31; })); } _if_result_30; })); } _if_result_29; })); } _if_result_28; })); } else { _if_result_27 = (ch); } _if_result_27; });
|
||||
el_val_t end_val = ((proc && !was_esc) && str_eq(ch, EL_STR("\"")));
|
||||
el_val_t do_app = ((proc && !end_val) && !esc);
|
||||
out = ({ el_val_t _if_result_34 = 0; if (do_app) { _if_result_34 = (el_str_concat(out, decoded)); } else { _if_result_34 = (out); } _if_result_34; });
|
||||
inval = ({ el_val_t _if_result_35 = 0; if (end_val) { _if_result_35 = (0); } else { _if_result_35 = (inval); } _if_result_35; });
|
||||
i = ({ el_val_t _if_result_36 = 0; if (at_key) { _if_result_36 = ((i + klen)); } else { _if_result_36 = ((i + 1)); } _if_result_36; });
|
||||
}
|
||||
return out;
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t strip_citations(el_val_t s) {
|
||||
el_val_t marker = EL_STR(",\"citations\":[");
|
||||
el_val_t mlen = str_len(marker);
|
||||
el_val_t n = str_len(s);
|
||||
el_val_t out = EL_STR("");
|
||||
el_val_t i = 0;
|
||||
el_val_t skip = 0;
|
||||
while (i < n) {
|
||||
el_val_t ch = str_slice(s, i, (i + 1));
|
||||
el_val_t at_marker = (((skip == 0) && ((i + mlen) <= n)) && str_eq(str_slice(s, i, (i + mlen)), marker));
|
||||
el_val_t was_skip = (skip > 0);
|
||||
skip = ({ el_val_t _if_result_37 = 0; if (at_marker) { _if_result_37 = (1); } else { _if_result_37 = (({ el_val_t _if_result_38 = 0; if (was_skip) { _if_result_38 = (({ el_val_t _if_result_39 = 0; if (str_eq(ch, EL_STR("["))) { _if_result_39 = ((skip + 1)); } else { _if_result_39 = (({ el_val_t _if_result_40 = 0; if (str_eq(ch, EL_STR("]"))) { _if_result_40 = ((skip - 1)); } else { _if_result_40 = (skip); } _if_result_40; })); } _if_result_39; })); } else { _if_result_38 = (skip); } _if_result_38; })); } _if_result_37; });
|
||||
out = ({ el_val_t _if_result_41 = 0; if (at_marker) { _if_result_41 = (out); } else { _if_result_41 = (({ el_val_t _if_result_42 = 0; if (was_skip) { _if_result_42 = (out); } else { _if_result_42 = (el_str_concat(out, ch)); } _if_result_42; })); } _if_result_41; });
|
||||
i = ({ el_val_t _if_result_43 = 0; if (at_marker) { _if_result_43 = ((i + mlen)); } else { _if_result_43 = ((i + 1)); } _if_result_43; });
|
||||
}
|
||||
return out;
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t agentic_api_turn(el_val_t model, el_val_t safe_sys, el_val_t tools_json, el_val_t messages) {
|
||||
el_val_t api_key = agentic_api_key();
|
||||
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 req_body = 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("{\"model\":\""), model), EL_STR("\"")), EL_STR(",\"max_tokens\":4096")), EL_STR(",\"system\":\"")), safe_sys), EL_STR("\"")), EL_STR(",\"tools\":")), tools_json), EL_STR(",\"messages\":")), messages), EL_STR("}"));
|
||||
el_val_t raw_resp = http_post_with_headers(api_url, req_body, h);
|
||||
el_val_t is_error = ((str_starts_with(raw_resp, EL_STR("{\"error\"")) || str_starts_with(raw_resp, EL_STR("{\"type\":\"error\""))) || str_contains(raw_resp, EL_STR("authentication_error")));
|
||||
if (is_error) {
|
||||
el_val_t detail_raw = ({ el_val_t _if_result_44 = 0; if ((str_len(raw_resp) > 300)) { _if_result_44 = (str_slice(raw_resp, 0, 300)); } else { _if_result_44 = (raw_resp); } _if_result_44; });
|
||||
el_val_t detail = json_safe(detail_raw);
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"kind\":\"error\",\"payload\":{\"error\":\"llm unavailable\",\"reply\":\"\",\"detail\":\""), detail), EL_STR("\"}}"));
|
||||
}
|
||||
el_val_t stop_reason = json_get(raw_resp, EL_STR("stop_reason"));
|
||||
if (str_eq(stop_reason, EL_STR("refusal"))) {
|
||||
return EL_STR("{\"kind\":\"refusal\"}");
|
||||
}
|
||||
el_val_t content_arr = json_get_raw(raw_resp, EL_STR("content"));
|
||||
el_val_t eff_content = ({ el_val_t _if_result_45 = 0; if (str_eq(content_arr, EL_STR(""))) { _if_result_45 = (EL_STR("[]")); } else { _if_result_45 = (content_arr); } _if_result_45; });
|
||||
el_val_t walk_content = strip_citations(eff_content);
|
||||
el_val_t text_out = extract_all_text(walk_content);
|
||||
el_val_t queue = EL_STR("[]");
|
||||
el_val_t ci = 0;
|
||||
el_val_t c_total = json_array_len(eff_content);
|
||||
while (ci < c_total) {
|
||||
el_val_t block = json_array_get(eff_content, ci);
|
||||
el_val_t btype = json_get(block, EL_STR("type"));
|
||||
queue = ({ el_val_t _if_result_46 = 0; if (str_eq(btype, EL_STR("tool_use"))) { _if_result_46 = (json_array_append(queue, block)); } else { _if_result_46 = (queue); } _if_result_46; });
|
||||
ci = (ci + 1);
|
||||
}
|
||||
el_val_t q_len = json_array_len(queue);
|
||||
el_val_t is_tool_turn = (str_eq(stop_reason, EL_STR("tool_use")) && (q_len > 0));
|
||||
if (is_tool_turn) {
|
||||
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"kind\":\"tools\",\"content\":"), eff_content), EL_STR(",\"queue\":")), queue), EL_STR("}"));
|
||||
}
|
||||
if (str_eq(stop_reason, EL_STR("pause_turn"))) {
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"kind\":\"pause\",\"content\":"), eff_content), EL_STR("}"));
|
||||
}
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"kind\":\"final\",\"text\":\""), json_safe(text_out)), EL_STR("\"}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t agentic_engine(el_val_t session_id, el_val_t blob) {
|
||||
el_val_t model = json_get(blob, EL_STR("model"));
|
||||
el_val_t system = json_get(blob, EL_STR("system"));
|
||||
el_val_t safe_sys = json_safe(system);
|
||||
el_val_t origin = json_get(blob, EL_STR("origin"));
|
||||
el_val_t approval = json_get_bool(blob, EL_STR("approval"));
|
||||
el_val_t tools_json = json_get_raw(blob, EL_STR("tools"));
|
||||
el_val_t pend_key = el_str_concat(EL_STR("agentic_pending_"), session_id);
|
||||
el_val_t messages = json_get_raw(blob, EL_STR("messages"));
|
||||
el_val_t content_in = json_get_raw(blob, EL_STR("content"));
|
||||
el_val_t queue_in = json_get_raw(blob, EL_STR("queue"));
|
||||
el_val_t results_in = json_get_raw(blob, EL_STR("results"));
|
||||
el_val_t content = ({ el_val_t _if_result_47 = 0; if (str_eq(content_in, EL_STR(""))) { _if_result_47 = (EL_STR("[]")); } else { _if_result_47 = (content_in); } _if_result_47; });
|
||||
el_val_t queue = ({ el_val_t _if_result_48 = 0; if (str_eq(queue_in, EL_STR(""))) { _if_result_48 = (EL_STR("[]")); } else { _if_result_48 = (queue_in); } _if_result_48; });
|
||||
el_val_t results = ({ el_val_t _if_result_49 = 0; if (str_eq(results_in, EL_STR(""))) { _if_result_49 = (EL_STR("[]")); } else { _if_result_49 = (results_in); } _if_result_49; });
|
||||
el_val_t next = json_get_int(blob, EL_STR("next"));
|
||||
el_val_t iteration = json_get_int(blob, EL_STR("iteration"));
|
||||
el_val_t tools_log = json_get(blob, EL_STR("tools_log"));
|
||||
el_val_t steps = 0;
|
||||
while (steps < 100) {
|
||||
el_val_t q_len = json_array_len(queue);
|
||||
el_val_t has_pending = (next < q_len);
|
||||
if (has_pending && approval) {
|
||||
el_val_t blk_a = json_array_get(queue, next);
|
||||
el_val_t pend_name = json_get(blk_a, EL_STR("name"));
|
||||
if (!tool_auto_approved(pend_name)) {
|
||||
el_val_t park = agentic_blob(model, system, tools_json, messages, origin, approval, iteration, tools_log, content, queue, results, next);
|
||||
state_set(pend_key, park);
|
||||
el_val_t t_input = json_get_raw(blk_a, EL_STR("input"));
|
||||
el_val_t eff_input = ({ el_val_t _if_result_50 = 0; if (str_eq(t_input, EL_STR(""))) { _if_result_50 = (EL_STR("{}")); } else { _if_result_50 = (t_input); } _if_result_50; });
|
||||
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("{\"status\":\"tool_pending\",\"call_id\":\""), json_get(blk_a, EL_STR("id"))), EL_STR("\",\"tool_name\":\"")), json_get(blk_a, EL_STR("name"))), EL_STR("\",\"tool_input\":")), eff_input), EL_STR(",\"model\":\"")), model), EL_STR("\",\"reply\":\"\"}"));
|
||||
}
|
||||
}
|
||||
el_val_t do_exec = has_pending;
|
||||
el_val_t do_fold = (!has_pending && (q_len > 0));
|
||||
el_val_t do_api = (!has_pending && (q_len < 1));
|
||||
el_val_t blk = ({ el_val_t _if_result_51 = 0; if (do_exec) { _if_result_51 = (json_array_get(queue, next)); } else { _if_result_51 = (EL_STR("")); } _if_result_51; });
|
||||
el_val_t t_msg = ({ el_val_t _if_result_52 = 0; if (do_exec) { _if_result_52 = (exec_tool_block(blk)); } else { _if_result_52 = (EL_STR("")); } _if_result_52; });
|
||||
tools_log = ({ el_val_t _if_result_53 = 0; if (do_exec) { _if_result_53 = (append_tool_log(tools_log, json_get(blk, EL_STR("name")))); } else { _if_result_53 = (tools_log); } _if_result_53; });
|
||||
results = ({ el_val_t _if_result_54 = 0; if (do_exec) { _if_result_54 = (json_array_append(results, t_msg)); } else { _if_result_54 = (results); } _if_result_54; });
|
||||
next = ({ el_val_t _if_result_55 = 0; if (do_exec) { _if_result_55 = ((next + 1)); } else { _if_result_55 = (next); } _if_result_55; });
|
||||
messages = ({ el_val_t _if_result_56 = 0; if (do_fold) { _if_result_56 = (json_array_append(json_array_append(messages, el_str_concat(el_str_concat(EL_STR("{\"role\":\"assistant\",\"content\":"), content), EL_STR("}"))), el_str_concat(el_str_concat(EL_STR("{\"role\":\"user\",\"content\":"), results), EL_STR("}")))); } else { _if_result_56 = (messages); } _if_result_56; });
|
||||
content = ({ el_val_t _if_result_57 = 0; if (do_fold) { _if_result_57 = (EL_STR("[]")); } else { _if_result_57 = (content); } _if_result_57; });
|
||||
queue = ({ el_val_t _if_result_58 = 0; if (do_fold) { _if_result_58 = (EL_STR("[]")); } else { _if_result_58 = (queue); } _if_result_58; });
|
||||
results = ({ el_val_t _if_result_59 = 0; if (do_fold) { _if_result_59 = (EL_STR("[]")); } else { _if_result_59 = (results); } _if_result_59; });
|
||||
next = ({ el_val_t _if_result_60 = 0; if (do_fold) { _if_result_60 = (0); } else { _if_result_60 = (next); } _if_result_60; });
|
||||
if (do_api && (iteration >= 8)) {
|
||||
state_set(pend_key, EL_STR(""));
|
||||
return EL_STR("{\"error\":\"no response\",\"reply\":\"\"}");
|
||||
}
|
||||
el_val_t verdict = ({ el_val_t _if_result_61 = 0; if (do_api) { _if_result_61 = (agentic_api_turn(model, safe_sys, tools_json, messages)); } else { _if_result_61 = (EL_STR("")); } _if_result_61; });
|
||||
el_val_t kind = ({ el_val_t _if_result_62 = 0; if (do_api) { _if_result_62 = (json_get(verdict, EL_STR("kind"))); } else { _if_result_62 = (EL_STR("")); } _if_result_62; });
|
||||
if (str_eq(kind, EL_STR("error"))) {
|
||||
state_set(pend_key, EL_STR(""));
|
||||
return json_get_raw(verdict, EL_STR("payload"));
|
||||
}
|
||||
if (str_eq(kind, EL_STR("refusal"))) {
|
||||
state_set(pend_key, EL_STR(""));
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"status\":\"ok\",\"reply\":\"I'm not able to help with that request.\",\"model\":\""), model), EL_STR("\",\"agentic\":true,\"tools_used\":[]}"));
|
||||
}
|
||||
if (str_eq(kind, EL_STR("final"))) {
|
||||
state_set(pend_key, EL_STR(""));
|
||||
el_val_t safe_text = json_safe(json_get(verdict, EL_STR("text")));
|
||||
el_val_t tools_arr = ({ el_val_t _if_result_63 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_63 = (EL_STR("[]")); } else { _if_result_63 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_63; });
|
||||
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"status\":\"ok\",\"reply\":\""), safe_text), EL_STR("\",\"model\":\"")), model), EL_STR("\",\"agentic\":true,\"tools_used\":")), tools_arr), EL_STR("}"));
|
||||
}
|
||||
el_val_t is_pause = str_eq(kind, EL_STR("pause"));
|
||||
messages = ({ el_val_t _if_result_64 = 0; if (is_pause) { _if_result_64 = (json_array_append(messages, el_str_concat(el_str_concat(EL_STR("{\"role\":\"assistant\",\"content\":"), json_get_raw(verdict, EL_STR("content"))), EL_STR("}")))); } else { _if_result_64 = (messages); } _if_result_64; });
|
||||
el_val_t is_tools = str_eq(kind, EL_STR("tools"));
|
||||
content = ({ el_val_t _if_result_65 = 0; if (is_tools) { _if_result_65 = (json_get_raw(verdict, EL_STR("content"))); } else { _if_result_65 = (content); } _if_result_65; });
|
||||
queue = ({ el_val_t _if_result_66 = 0; if (is_tools) { _if_result_66 = (json_get_raw(verdict, EL_STR("queue"))); } else { _if_result_66 = (queue); } _if_result_66; });
|
||||
results = ({ el_val_t _if_result_67 = 0; if (is_tools) { _if_result_67 = (EL_STR("[]")); } else { _if_result_67 = (results); } _if_result_67; });
|
||||
next = ({ el_val_t _if_result_68 = 0; if (is_tools) { _if_result_68 = (0); } else { _if_result_68 = (next); } _if_result_68; });
|
||||
iteration = ({ el_val_t _if_result_69 = 0; if (do_api) { _if_result_69 = ((iteration + 1)); } else { _if_result_69 = (iteration); } _if_result_69; });
|
||||
steps = (steps + 1);
|
||||
}
|
||||
state_set(pend_key, EL_STR(""));
|
||||
return EL_STR("{\"error\":\"agentic engine step limit exceeded\",\"reply\":\"\"}");
|
||||
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(""))) {
|
||||
return EL_STR("{\"error\":\"message required\",\"reply\":\"\"}");
|
||||
}
|
||||
el_val_t req_model = json_get(body, EL_STR("model"));
|
||||
el_val_t model = ({ el_val_t _if_result_20 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_20 = (chat_default_model()); } else { _if_result_20 = (req_model); } _if_result_20; });
|
||||
el_val_t model = ({ el_val_t _if_result_70 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_70 = (chat_default_model()); } else { _if_result_70 = (req_model); } _if_result_70; });
|
||||
el_val_t session_id = json_get(body, EL_STR("session_id"));
|
||||
el_val_t ra_bool = json_get_bool(body, EL_STR("require_approval"));
|
||||
el_val_t ra_raw = json_get_raw(body, EL_STR("require_approval"));
|
||||
el_val_t want_approval = ((ra_bool || str_eq(ra_raw, EL_STR("1"))) || str_eq(ra_raw, EL_STR("true")));
|
||||
el_val_t approval = (want_approval && !str_eq(session_id, EL_STR("")));
|
||||
el_val_t ctx = engram_compile(message);
|
||||
el_val_t identity = state_get(EL_STR("soul_identity"));
|
||||
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.\n\n")), ctx);
|
||||
el_val_t api_key = agentic_api_key();
|
||||
el_val_t tools_json = agentic_tools_literal();
|
||||
el_val_t current_date = time_format(time_now(), EL_STR("%A, %B %d, %Y"));
|
||||
el_val_t web_directive = el_str_concat(el_str_concat(EL_STR(" Today's date is "), current_date), EL_STR(". You have a web_search tool that returns live results from the internet. For ANY question about current events, live scores, standings, schedules, recent news, people, prices, or anything that may have changed since your training, you MUST call web_search immediately and answer from the results. Do NOT ask the user to clarify first, do NOT say you lack live access, and do NOT answer time-sensitive questions from memory alone \xe2\x80\x94 search, then answer. When a question is ambiguous about timeframe (e.g. 'the tournament', 'the game', 'the playoffs'), assume the user means whatever is happening or most recent RIGHT NOW as of today's date, search for that, and lead with it."));
|
||||
el_val_t tool_directive = EL_STR(" You have real, working tools attached to THIS conversation right now \xe2\x80\x94 read and write files, browse the web, search your memory, run commands, and any connected services (named mcp__<server>__<tool>, e.g. a Filesystem, GitHub, or Notion connector the user enabled). The attached tool list is the ground truth of what you can do. When the user asks whether you can do something one of these tools enables, or asks you to do it, USE the tool and act. Do NOT claim you lack a capability your tools provide, do NOT describe your tools in the abstract, and do NOT refuse on the assumption you cannot \xe2\x80\x94 if a tool isn't in your attached list, say so plainly; otherwise just do it.");
|
||||
el_val_t system = el_str_concat(el_str_concat(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, plus any connected services (named mcp__<server>__<tool>). Use them when they add genuine value. Be direct.")), web_directive), tool_directive), EL_STR("\n\n")), ctx);
|
||||
system = safety_augment_system(system, message);
|
||||
el_val_t tools_json = agentic_tools_all();
|
||||
el_val_t safe_msg = json_safe(message);
|
||||
el_val_t safe_sys = json_safe(system);
|
||||
el_val_t messages = el_str_concat(el_str_concat(EL_STR("[{\"role\":\"user\",\"content\":\""), safe_msg), EL_STR("\"}]"));
|
||||
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 final_text = EL_STR("");
|
||||
el_val_t tools_log = EL_STR("");
|
||||
el_val_t iteration = 0;
|
||||
el_val_t keep_going = 1;
|
||||
while (keep_going && (iteration < 8)) {
|
||||
el_val_t req_body = 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("{\"model\":\""), model), EL_STR("\"")), EL_STR(",\"max_tokens\":4096")), EL_STR(",\"system\":\"")), safe_sys), EL_STR("\"")), EL_STR(",\"tools\":")), tools_json), EL_STR(",\"messages\":")), messages), EL_STR("}"));
|
||||
el_val_t raw_resp = http_post_with_headers(api_url, req_body, h);
|
||||
el_val_t is_error = ((str_starts_with(raw_resp, EL_STR("{\"error\"")) || str_starts_with(raw_resp, EL_STR("{\"type\":\"error\""))) || str_contains(raw_resp, EL_STR("authentication_error")));
|
||||
if (is_error) {
|
||||
return EL_STR("{\"error\":\"llm unavailable\",\"reply\":\"\"}");
|
||||
}
|
||||
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_21 = 0; if (str_eq(content_arr, EL_STR(""))) { _if_result_21 = (EL_STR("[]")); } else { _if_result_21 = (content_arr); } _if_result_21; });
|
||||
el_val_t text_out = EL_STR("");
|
||||
el_val_t has_tool = 0;
|
||||
el_val_t tool_id = EL_STR("");
|
||||
el_val_t tool_name = EL_STR("");
|
||||
el_val_t tool_input = EL_STR("");
|
||||
el_val_t ci = 0;
|
||||
el_val_t c_total = json_array_len(eff_content);
|
||||
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_22 = 0; if (str_eq(btype, EL_STR("text"))) { _if_result_22 = (el_str_concat(text_out, json_get(block, EL_STR("text")))); } else { _if_result_22 = (text_out); } _if_result_22; });
|
||||
el_val_t is_new_tool = (str_eq(btype, EL_STR("tool_use")) && !has_tool);
|
||||
has_tool = ({ el_val_t _if_result_23 = 0; if (is_new_tool) { _if_result_23 = (1); } else { _if_result_23 = (has_tool); } _if_result_23; });
|
||||
tool_id = ({ el_val_t _if_result_24 = 0; if (is_new_tool) { _if_result_24 = (json_get(block, EL_STR("id"))); } else { _if_result_24 = (tool_id); } _if_result_24; });
|
||||
tool_name = ({ el_val_t _if_result_25 = 0; if (is_new_tool) { _if_result_25 = (json_get(block, EL_STR("name"))); } else { _if_result_25 = (tool_name); } _if_result_25; });
|
||||
tool_input = ({ el_val_t _if_result_26 = 0; if (is_new_tool) { _if_result_26 = (json_get_raw(block, EL_STR("input"))); } else { _if_result_26 = (tool_input); } _if_result_26; });
|
||||
ci = (ci + 1);
|
||||
}
|
||||
el_val_t tool_result_raw = ({ el_val_t _if_result_27 = 0; if (has_tool) { _if_result_27 = (dispatch_tool(tool_name, tool_input)); } else { _if_result_27 = (EL_STR("")); } _if_result_27; });
|
||||
el_val_t tool_result = ({ el_val_t _if_result_28 = 0; if ((str_len(tool_result_raw) > 6000)) { _if_result_28 = (el_str_concat(str_slice(tool_result_raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_28 = (tool_result_raw); } _if_result_28; });
|
||||
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_29 = 0; if (has_tool) { _if_result_29 = (({ el_val_t _if_result_30 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_30 = (tool_quoted); } else { _if_result_30 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), tool_quoted)); } _if_result_30; })); } else { _if_result_29 = (tools_log); } _if_result_29; });
|
||||
el_val_t is_tool_turn = (str_eq(stop_reason, EL_STR("tool_use")) && has_tool);
|
||||
el_val_t inner = str_slice(messages, 1, (str_len(messages) - 1));
|
||||
messages = ({ el_val_t _if_result_31 = 0; if (is_tool_turn) { _if_result_31 = (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("["), inner), EL_STR(",{\"role\":\"assistant\",\"content\":")), eff_content), EL_STR("}")), EL_STR(",{\"role\":\"user\",\"content\":[")), tool_msg), EL_STR("]}")), EL_STR("]"))); } else { _if_result_31 = (messages); } _if_result_31; });
|
||||
final_text = ({ el_val_t _if_result_32 = 0; if (!is_tool_turn) { _if_result_32 = (text_out); } else { _if_result_32 = (final_text); } _if_result_32; });
|
||||
keep_going = ({ el_val_t _if_result_33 = 0; if (!is_tool_turn) { _if_result_33 = (0); } else { _if_result_33 = (keep_going); } _if_result_33; });
|
||||
iteration = (iteration + 1);
|
||||
if (!str_eq(session_id, EL_STR(""))) {
|
||||
state_set(el_str_concat(EL_STR("agentic_pending_"), session_id), EL_STR(""));
|
||||
}
|
||||
if (str_eq(final_text, EL_STR(""))) {
|
||||
return EL_STR("{\"error\":\"no response\",\"reply\":\"\"}");
|
||||
el_val_t blob = agentic_blob(model, system, tools_json, messages, message, approval, 0, EL_STR(""), EL_STR("[]"), EL_STR("[]"), EL_STR("[]"), 0);
|
||||
return agentic_engine(session_id, blob);
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t handle_session_approve(el_val_t session_id, el_val_t body) {
|
||||
if (str_eq(session_id, EL_STR(""))) {
|
||||
return EL_STR("{\"error\":\"session_id required\",\"reply\":\"\"}");
|
||||
}
|
||||
el_val_t safe_text = json_safe(final_text);
|
||||
el_val_t tools_arr = ({ el_val_t _if_result_34 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_34 = (EL_STR("[]")); } else { _if_result_34 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_34; });
|
||||
return 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("}"));
|
||||
el_val_t pend_key = el_str_concat(EL_STR("agentic_pending_"), session_id);
|
||||
el_val_t blob = state_get(pend_key);
|
||||
if (str_eq(blob, EL_STR(""))) {
|
||||
return EL_STR("{\"error\":\"no pending tool call for this session\",\"reply\":\"\"}");
|
||||
}
|
||||
el_val_t call_id = json_get(body, EL_STR("call_id"));
|
||||
el_val_t action = json_get(body, EL_STR("action"));
|
||||
el_val_t queue = json_get_raw(blob, EL_STR("queue"));
|
||||
el_val_t next = json_get_int(blob, EL_STR("next"));
|
||||
el_val_t q_len = json_array_len(queue);
|
||||
if (next >= q_len) {
|
||||
state_set(pend_key, EL_STR(""));
|
||||
return EL_STR("{\"error\":\"pending state corrupt\",\"reply\":\"\"}");
|
||||
}
|
||||
el_val_t block = json_array_get(queue, next);
|
||||
el_val_t block_id = json_get(block, EL_STR("id"));
|
||||
if (!str_eq(call_id, block_id)) {
|
||||
return EL_STR("{\"error\":\"stale call_id\",\"reply\":\"\"}");
|
||||
}
|
||||
el_val_t deny = str_eq(action, EL_STR("deny"));
|
||||
el_val_t t_msg = ({ el_val_t _if_result_71 = 0; if (deny) { _if_result_71 = (el_str_concat(el_str_concat(EL_STR("{\"type\":\"tool_result\",\"tool_use_id\":\""), block_id), EL_STR("\",\"content\":\"User denied this tool call.\"}"))); } else { _if_result_71 = (exec_tool_block(block)); } _if_result_71; });
|
||||
el_val_t tools_log_in = json_get(blob, EL_STR("tools_log"));
|
||||
el_val_t tools_log = ({ el_val_t _if_result_72 = 0; if (deny) { _if_result_72 = (tools_log_in); } else { _if_result_72 = (append_tool_log(tools_log_in, json_get(block, EL_STR("name")))); } _if_result_72; });
|
||||
el_val_t results = json_array_append(json_get_raw(blob, EL_STR("results")), t_msg);
|
||||
el_val_t model = json_get(blob, EL_STR("model"));
|
||||
el_val_t system = json_get(blob, EL_STR("system"));
|
||||
el_val_t origin = json_get(blob, EL_STR("origin"));
|
||||
el_val_t tools_json = json_get_raw(blob, EL_STR("tools"));
|
||||
el_val_t messages = json_get_raw(blob, EL_STR("messages"));
|
||||
el_val_t content = json_get_raw(blob, EL_STR("content"));
|
||||
el_val_t iteration = json_get_int(blob, EL_STR("iteration"));
|
||||
state_set(pend_key, EL_STR(""));
|
||||
el_val_t updated = agentic_blob(model, system, tools_json, messages, origin, 1, iteration, tools_log, content, queue, results, (next + 1));
|
||||
el_val_t reply = agentic_engine(session_id, updated);
|
||||
el_val_t is_final = str_contains(reply, EL_STR("\"status\":\"ok\""));
|
||||
if (is_final) {
|
||||
auto_persist(el_str_concat(el_str_concat(EL_STR("{\"message\":\""), json_safe(origin)), EL_STR("\"}")), reply);
|
||||
}
|
||||
return reply;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -346,12 +651,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_35 = 0; if (str_eq(message, EL_STR(""))) { _if_result_35 = (transcript); } else { _if_result_35 = (message); } _if_result_35; });
|
||||
el_val_t eff_message = ({ el_val_t _if_result_73 = 0; if (str_eq(message, EL_STR(""))) { _if_result_73 = (transcript); } else { _if_result_73 = (message); } _if_result_73; });
|
||||
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_36 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_36 = (chat_default_model()); } else { _if_result_36 = (req_model); } _if_result_36; });
|
||||
el_val_t model = ({ el_val_t _if_result_74 = 0; if (str_eq(req_model, EL_STR(""))) { _if_result_74 = (chat_default_model()); } else { _if_result_74 = (req_model); } _if_result_74; });
|
||||
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")));
|
||||
if (is_error) {
|
||||
@@ -373,7 +678,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(transcript);
|
||||
el_val_t system_prompt = ({ el_val_t _if_result_37 = 0; if (str_eq(engram_ctx, EL_STR(""))) { _if_result_37 = (identity); } else { _if_result_37 = (el_str_concat(el_str_concat(identity, EL_STR("\n\n")), engram_ctx)); } _if_result_37; });
|
||||
el_val_t system_prompt = ({ el_val_t _if_result_75 = 0; if (str_eq(engram_ctx, EL_STR(""))) { _if_result_75 = (identity); } else { _if_result_75 = (el_str_concat(el_str_concat(identity, EL_STR("\n\n")), engram_ctx)); } _if_result_75; });
|
||||
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")));
|
||||
if (is_error) {
|
||||
@@ -400,65 +705,19 @@ el_val_t handle_dharma_room_turn_agentic(el_val_t body) {
|
||||
}
|
||||
el_val_t ctx = engram_compile(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();
|
||||
el_val_t tools_json = agentic_tools_literal();
|
||||
el_val_t safe_transcript = json_safe(transcript);
|
||||
el_val_t safe_sys = json_safe(system);
|
||||
el_val_t messages = el_str_concat(el_str_concat(EL_STR("[{\"role\":\"user\",\"content\":\""), safe_transcript), EL_STR("\"}]"));
|
||||
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 final_text = EL_STR("");
|
||||
el_val_t tools_log = EL_STR("");
|
||||
el_val_t iteration = 0;
|
||||
el_val_t keep_going = 1;
|
||||
while (keep_going && (iteration < 8)) {
|
||||
el_val_t req_body = 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("{\"model\":\""), model), EL_STR("\"")), EL_STR(",\"max_tokens\":4096")), EL_STR(",\"system\":\"")), safe_sys), EL_STR("\"")), EL_STR(",\"tools\":")), tools_json), EL_STR(",\"messages\":")), messages), EL_STR("}"));
|
||||
el_val_t raw_resp = http_post_with_headers(api_url, req_body, h);
|
||||
el_val_t is_error = ((str_starts_with(raw_resp, EL_STR("{\"error\"")) || str_starts_with(raw_resp, EL_STR("{\"type\":\"error\""))) || str_contains(raw_resp, EL_STR("authentication_error")));
|
||||
if (is_error) {
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"error\":\"llm unavailable\",\"response\":\"\",\"cgi_id\":\""), cgi_id), EL_STR("\"}"));
|
||||
}
|
||||
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_38 = 0; if (str_eq(content_arr, EL_STR(""))) { _if_result_38 = (EL_STR("[]")); } else { _if_result_38 = (content_arr); } _if_result_38; });
|
||||
el_val_t text_out = EL_STR("");
|
||||
el_val_t has_tool = 0;
|
||||
el_val_t tool_id = EL_STR("");
|
||||
el_val_t tool_name = EL_STR("");
|
||||
el_val_t tool_input = EL_STR("");
|
||||
el_val_t ci = 0;
|
||||
el_val_t c_total = json_array_len(eff_content);
|
||||
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_39 = 0; if (str_eq(btype, EL_STR("text"))) { _if_result_39 = (el_str_concat(text_out, json_get(block, EL_STR("text")))); } else { _if_result_39 = (text_out); } _if_result_39; });
|
||||
el_val_t is_new_tool = (str_eq(btype, EL_STR("tool_use")) && !has_tool);
|
||||
has_tool = ({ el_val_t _if_result_40 = 0; if (is_new_tool) { _if_result_40 = (1); } else { _if_result_40 = (has_tool); } _if_result_40; });
|
||||
tool_id = ({ el_val_t _if_result_41 = 0; if (is_new_tool) { _if_result_41 = (json_get(block, EL_STR("id"))); } else { _if_result_41 = (tool_id); } _if_result_41; });
|
||||
tool_name = ({ el_val_t _if_result_42 = 0; if (is_new_tool) { _if_result_42 = (json_get(block, EL_STR("name"))); } else { _if_result_42 = (tool_name); } _if_result_42; });
|
||||
tool_input = ({ el_val_t _if_result_43 = 0; if (is_new_tool) { _if_result_43 = (json_get_raw(block, EL_STR("input"))); } else { _if_result_43 = (tool_input); } _if_result_43; });
|
||||
ci = (ci + 1);
|
||||
}
|
||||
el_val_t tool_result_raw = ({ el_val_t _if_result_44 = 0; if (has_tool) { _if_result_44 = (dispatch_tool(tool_name, tool_input)); } else { _if_result_44 = (EL_STR("")); } _if_result_44; });
|
||||
el_val_t tool_result = ({ el_val_t _if_result_45 = 0; if ((str_len(tool_result_raw) > 6000)) { _if_result_45 = (el_str_concat(str_slice(tool_result_raw, 0, 6000), EL_STR("...[truncated]"))); } else { _if_result_45 = (tool_result_raw); } _if_result_45; });
|
||||
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_46 = 0; if (has_tool) { _if_result_46 = (({ el_val_t _if_result_47 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_47 = (tool_quoted); } else { _if_result_47 = (el_str_concat(el_str_concat(tools_log, EL_STR(",")), tool_quoted)); } _if_result_47; })); } else { _if_result_46 = (tools_log); } _if_result_46; });
|
||||
el_val_t is_tool_turn = (str_eq(stop_reason, EL_STR("tool_use")) && has_tool);
|
||||
el_val_t inner = str_slice(messages, 1, (str_len(messages) - 1));
|
||||
messages = ({ el_val_t _if_result_48 = 0; if (is_tool_turn) { _if_result_48 = (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("["), inner), EL_STR(",{\"role\":\"assistant\",\"content\":")), eff_content), EL_STR("}")), EL_STR(",{\"role\":\"user\",\"content\":[")), tool_msg), EL_STR("]}")), EL_STR("]"))); } else { _if_result_48 = (messages); } _if_result_48; });
|
||||
final_text = ({ el_val_t _if_result_49 = 0; if (!is_tool_turn) { _if_result_49 = (text_out); } else { _if_result_49 = (final_text); } _if_result_49; });
|
||||
keep_going = ({ el_val_t _if_result_50 = 0; if (!is_tool_turn) { _if_result_50 = (0); } else { _if_result_50 = (keep_going); } _if_result_50; });
|
||||
iteration = (iteration + 1);
|
||||
el_val_t blob = agentic_blob(model, system, tools_json, messages, transcript, 0, 0, EL_STR(""), EL_STR("[]"), EL_STR("[]"), EL_STR("[]"), 0);
|
||||
el_val_t reply = agentic_engine(EL_STR(""), blob);
|
||||
el_val_t err = json_get(reply, EL_STR("error"));
|
||||
if (!str_eq(err, EL_STR(""))) {
|
||||
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"error\":\""), json_safe(err)), EL_STR("\",\"response\":\"\",\"cgi_id\":\"")), cgi_id), EL_STR("\"}"));
|
||||
}
|
||||
if (str_eq(final_text, EL_STR(""))) {
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"error\":\"no response\",\"response\":\"\",\"cgi_id\":\""), cgi_id), EL_STR("\"}"));
|
||||
}
|
||||
el_val_t safe_text = json_safe(final_text);
|
||||
el_val_t tools_arr = ({ el_val_t _if_result_51 = 0; if (str_eq(tools_log, EL_STR(""))) { _if_result_51 = (EL_STR("[]")); } else { _if_result_51 = (el_str_concat(el_str_concat(EL_STR("["), tools_log), EL_STR("]"))); } _if_result_51; });
|
||||
el_val_t text = json_get(reply, EL_STR("reply"));
|
||||
el_val_t safe_text = json_safe(text);
|
||||
el_val_t tools_used = json_get_raw(reply, EL_STR("tools_used"));
|
||||
el_val_t tools_arr = ({ el_val_t _if_result_76 = 0; if (str_eq(tools_used, EL_STR(""))) { _if_result_76 = (EL_STR("[]")); } else { _if_result_76 = (tools_used); } _if_result_76; });
|
||||
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\":")), tools_arr), EL_STR("}"));
|
||||
return 0;
|
||||
}
|
||||
@@ -466,7 +725,7 @@ el_val_t handle_dharma_room_turn_agentic(el_val_t body) {
|
||||
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_52 = 0; if (str_eq(reply, EL_STR(""))) { _if_result_52 = (json_get(resp, EL_STR("reply"))); } else { _if_result_52 = (reply); } _if_result_52; });
|
||||
el_val_t reply2 = ({ el_val_t _if_result_77 = 0; if (str_eq(reply, EL_STR(""))) { _if_result_77 = (json_get(resp, EL_STR("reply"))); } else { _if_result_77 = (reply); } _if_result_77; });
|
||||
if (str_eq(message, EL_STR(""))) {
|
||||
return EL_STR("");
|
||||
}
|
||||
|
||||
Vendored
+2
-1
@@ -1,4 +1,4 @@
|
||||
// auto-generated by elc --emit-header — do not edit
|
||||
// auto-generated by elc --emit-header - do not edit
|
||||
extern fn chat_default_model() -> String
|
||||
extern fn engram_compile(intent: String) -> String
|
||||
extern fn json_safe(s: String) -> String
|
||||
@@ -13,6 +13,7 @@ extern fn handle_see(body: String) -> String
|
||||
extern fn studio_tools_json() -> String
|
||||
extern fn agentic_api_key() -> String
|
||||
extern fn agentic_tools_literal() -> String
|
||||
extern fn agentic_tools_with_web() -> String
|
||||
extern fn dispatch_tool(tool_name: String, tool_input: String) -> String
|
||||
extern fn handle_chat_agentic(body: String) -> String
|
||||
extern fn handle_chat_as_soul(body: String) -> String
|
||||
|
||||
Vendored
+100
-61
@@ -1,4 +1,4 @@
|
||||
/* Auto-generated C forward declarations for all ELP modules */
|
||||
/* Auto-generated C forward declarations for ELP cross-module calls */
|
||||
#pragma once
|
||||
#include "el_runtime.h"
|
||||
|
||||
@@ -6,15 +6,20 @@ el_val_t add_punct(el_val_t s, el_val_t intent);
|
||||
el_val_t agent_number(el_val_t agent);
|
||||
el_val_t agent_person(el_val_t agent);
|
||||
el_val_t agentic_api_key(void);
|
||||
el_val_t agentic_api_turn(el_val_t model, el_val_t safe_sys, el_val_t tools_json, el_val_t messages);
|
||||
el_val_t agentic_blob(el_val_t model, el_val_t system, el_val_t tools_json, el_val_t messages, el_val_t origin, el_val_t approval, el_val_t iteration, el_val_t tools_log, el_val_t content, el_val_t queue, el_val_t results, el_val_t next);
|
||||
el_val_t agentic_engine(el_val_t session_id, el_val_t blob);
|
||||
el_val_t agentic_tools_all(void);
|
||||
el_val_t agentic_tools_literal(void);
|
||||
el_val_t agentic_tools_with_web(void);
|
||||
el_val_t agree_determiner(el_val_t det, el_val_t noun);
|
||||
el_val_t akk_alaku_perfect(el_val_t slot);
|
||||
el_val_t akk_alaku_present(el_val_t slot);
|
||||
el_val_t akk_amaru_perfect(el_val_t slot);
|
||||
el_val_t akk_amaru_present(el_val_t slot);
|
||||
el_val_t akk_amaru_stative(el_val_t slot);
|
||||
el_val_t akk_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t akk_conjugate_copula(el_val_t tense, el_val_t slot);
|
||||
el_val_t akk_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t akk_copula_present(el_val_t slot);
|
||||
el_val_t akk_copula_stative(el_val_t slot);
|
||||
el_val_t akk_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
@@ -32,25 +37,25 @@ el_val_t akk_qabu_stative(el_val_t slot);
|
||||
el_val_t akk_regular_perfect(el_val_t stem, el_val_t slot);
|
||||
el_val_t akk_regular_present(el_val_t stem, el_val_t slot);
|
||||
el_val_t akk_regular_stative(el_val_t stem, el_val_t slot);
|
||||
el_val_t akk_slot(el_val_t person, el_val_t number);
|
||||
el_val_t akk_slot_g(el_val_t person, el_val_t gender, el_val_t number);
|
||||
el_val_t akk_slot(el_val_t person, el_val_t number);
|
||||
el_val_t akk_str_drop_last(el_val_t s, el_val_t n);
|
||||
el_val_t akk_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t akk_str_len(el_val_t s);
|
||||
el_val_t akk_strip_nom(el_val_t noun);
|
||||
el_val_t ang_article(el_val_t gender, el_val_t gram_case, el_val_t number);
|
||||
el_val_t ang_article_feminine(el_val_t gram_case, el_val_t number);
|
||||
el_val_t ang_article_masculine(el_val_t gram_case, el_val_t number);
|
||||
el_val_t ang_article_neuter(el_val_t gram_case, el_val_t number);
|
||||
el_val_t ang_article(el_val_t gender, el_val_t gram_case, el_val_t number);
|
||||
el_val_t ang_beon_present(el_val_t slot);
|
||||
el_val_t ang_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t ang_cuman_past(el_val_t slot);
|
||||
el_val_t ang_cuman_present(el_val_t slot);
|
||||
el_val_t ang_declension(el_val_t noun, el_val_t gender);
|
||||
el_val_t ang_decline(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t gender);
|
||||
el_val_t ang_decline_strong_masc(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t ang_decline_strong_neut(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t ang_decline_weak(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t ang_decline(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t gender);
|
||||
el_val_t ang_don_past(el_val_t slot);
|
||||
el_val_t ang_don_present(el_val_t slot);
|
||||
el_val_t ang_gan_past(el_val_t slot);
|
||||
@@ -69,10 +74,10 @@ el_val_t ang_seon_present(el_val_t slot);
|
||||
el_val_t ang_slot(el_val_t person, el_val_t number);
|
||||
el_val_t ang_str_drop_last(el_val_t s, el_val_t n);
|
||||
el_val_t ang_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t ang_str_last2(el_val_t s);
|
||||
el_val_t ang_str_last_char(el_val_t s);
|
||||
el_val_t ang_weak_past(el_val_t stem, el_val_t slot);
|
||||
el_val_t ang_str_last2(el_val_t s);
|
||||
el_val_t ang_weak_past_stem(el_val_t stem);
|
||||
el_val_t ang_weak_past(el_val_t stem, el_val_t slot);
|
||||
el_val_t ang_weak_present_ending(el_val_t slot);
|
||||
el_val_t ang_weak_stem(el_val_t verb);
|
||||
el_val_t ang_wesan_past(el_val_t slot);
|
||||
@@ -84,24 +89,27 @@ el_val_t ang_witan_present(el_val_t slot);
|
||||
el_val_t api_err(el_val_t msg);
|
||||
el_val_t api_json_escape(el_val_t s);
|
||||
el_val_t api_nonempty(el_val_t s);
|
||||
el_val_t api_not_persisted(el_val_t id);
|
||||
el_val_t api_ok(el_val_t extra);
|
||||
el_val_t api_or_empty(el_val_t s);
|
||||
el_val_t api_persisted(el_val_t id);
|
||||
el_val_t api_query_int(el_val_t path, el_val_t key, el_val_t default_val);
|
||||
el_val_t api_query_param(el_val_t path, el_val_t key);
|
||||
el_val_t append_tool_log(el_val_t log, el_val_t name);
|
||||
el_val_t ar_case_ending(el_val_t kase, el_val_t definite);
|
||||
el_val_t ar_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t gender, el_val_t number);
|
||||
el_val_t ar_conjugate_form1(el_val_t past_base, el_val_t present_stem, el_val_t tense, el_val_t slot);
|
||||
el_val_t ar_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t gender, el_val_t number);
|
||||
el_val_t ar_definite_article(el_val_t noun);
|
||||
el_val_t ar_gender(el_val_t noun);
|
||||
el_val_t ar_imperfect_prefix(el_val_t slot);
|
||||
el_val_t ar_imperfect_suffix(el_val_t slot);
|
||||
el_val_t ar_irregular(el_val_t verb, el_val_t tense, el_val_t slot);
|
||||
el_val_t ar_irregular_araada(el_val_t slot, el_val_t tense);
|
||||
el_val_t ar_irregular_istata(el_val_t slot, el_val_t tense);
|
||||
el_val_t ar_irregular_jaa(el_val_t slot, el_val_t tense);
|
||||
el_val_t ar_irregular_kaana(el_val_t slot, el_val_t tense);
|
||||
el_val_t ar_irregular_qaala(el_val_t slot, el_val_t tense);
|
||||
el_val_t ar_irregular_raaa(el_val_t slot, el_val_t tense);
|
||||
el_val_t ar_irregular(el_val_t verb, el_val_t tense, el_val_t slot);
|
||||
el_val_t ar_is_sun_letter(el_val_t c);
|
||||
el_val_t ar_masc_pl_ending(el_val_t kase);
|
||||
el_val_t ar_noun_form(el_val_t noun, el_val_t gender, el_val_t kase, el_val_t number, el_val_t definite);
|
||||
@@ -128,9 +136,13 @@ el_val_t build_system_prompt(el_val_t ctx);
|
||||
el_val_t build_vocab(void);
|
||||
el_val_t build_vp_body(el_val_t slots);
|
||||
el_val_t build_vp_from_slots(el_val_t slots);
|
||||
el_val_t call_mcp_bridge(el_val_t tool_name, el_val_t tool_input);
|
||||
el_val_t capitalize_first(el_val_t s);
|
||||
el_val_t chat_default_model(void);
|
||||
el_val_t clean_llm_response(el_val_t s);
|
||||
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 connector_tools_json(void);
|
||||
el_val_t conv_history_load(void);
|
||||
el_val_t conv_history_persist(el_val_t hist);
|
||||
el_val_t cop_article(el_val_t gender, el_val_t number, el_val_t definite);
|
||||
@@ -154,8 +166,8 @@ el_val_t cop_map_canonical(el_val_t verb);
|
||||
el_val_t cop_nau_future(el_val_t prefix);
|
||||
el_val_t cop_nau_perfect(el_val_t prefix);
|
||||
el_val_t cop_nau_present(el_val_t prefix);
|
||||
el_val_t cop_noun_phrase(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite);
|
||||
el_val_t cop_noun_phrase_gendered(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite, el_val_t gender);
|
||||
el_val_t cop_noun_phrase(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite);
|
||||
el_val_t cop_regular_future(el_val_t prefix, el_val_t stem);
|
||||
el_val_t cop_regular_perfect(el_val_t prefix, el_val_t stem);
|
||||
el_val_t cop_regular_present(el_val_t prefix, el_val_t stem);
|
||||
@@ -165,15 +177,15 @@ el_val_t cop_shwpe_present(el_val_t prefix);
|
||||
el_val_t cop_slot(el_val_t person, el_val_t number);
|
||||
el_val_t cop_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t cop_str_len(el_val_t s);
|
||||
el_val_t cop_subject_prefix(el_val_t person, el_val_t number);
|
||||
el_val_t cop_subject_prefix_gendered(el_val_t person, el_val_t gender, el_val_t number);
|
||||
el_val_t cop_subject_prefix(el_val_t person, el_val_t number);
|
||||
el_val_t de_adj_ending(el_val_t gender, el_val_t gram_case, el_val_t number, el_val_t article_type);
|
||||
el_val_t de_article(el_val_t gender, el_val_t gram_case, el_val_t number, el_val_t definite);
|
||||
el_val_t de_article_def(el_val_t gender, el_val_t gram_case, el_val_t number);
|
||||
el_val_t de_article_indef(el_val_t gender, el_val_t gram_case, el_val_t number);
|
||||
el_val_t de_article(el_val_t gender, el_val_t gram_case, el_val_t number, el_val_t definite);
|
||||
el_val_t de_case_ending(el_val_t noun, el_val_t gender, el_val_t gram_case, el_val_t number);
|
||||
el_val_t de_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t de_conjugate_weak(el_val_t stem, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t de_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t de_irregular_present(el_val_t verb, el_val_t person, el_val_t number);
|
||||
el_val_t de_norm_number(el_val_t number);
|
||||
el_val_t de_norm_person(el_val_t person);
|
||||
@@ -182,15 +194,12 @@ el_val_t de_strong_past_stem(el_val_t verb);
|
||||
el_val_t dharma_network_state(void);
|
||||
el_val_t dharma_registry(void);
|
||||
el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input);
|
||||
el_val_t egy_conjugate_copula(el_val_t tense, el_val_t slot);
|
||||
el_val_t egy_conjugate_pronoun(el_val_t person, el_val_t number);
|
||||
el_val_t egy_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t egy_Dd_future(el_val_t slot);
|
||||
el_val_t egy_Dd_past(el_val_t slot);
|
||||
el_val_t egy_Dd_present(el_val_t slot);
|
||||
el_val_t egy_Sm_future(el_val_t slot);
|
||||
el_val_t egy_Sm_past(el_val_t slot);
|
||||
el_val_t egy_Sm_present(el_val_t slot);
|
||||
el_val_t egy_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t egy_conjugate_copula(el_val_t tense, el_val_t slot);
|
||||
el_val_t egy_conjugate_pronoun(el_val_t person, el_val_t number);
|
||||
el_val_t egy_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t egy_drop(el_val_t s, el_val_t n);
|
||||
el_val_t egy_fem(el_val_t noun);
|
||||
@@ -214,8 +223,11 @@ el_val_t egy_regular_present(el_val_t stem, el_val_t slot);
|
||||
el_val_t egy_sdm_future(el_val_t slot);
|
||||
el_val_t egy_sdm_past(el_val_t slot);
|
||||
el_val_t egy_sdm_present(el_val_t slot);
|
||||
el_val_t egy_slot(el_val_t person, el_val_t number);
|
||||
el_val_t egy_slot_with_gender(el_val_t person, el_val_t gender, el_val_t number);
|
||||
el_val_t egy_slot(el_val_t person, el_val_t number);
|
||||
el_val_t egy_Sm_future(el_val_t slot);
|
||||
el_val_t egy_Sm_past(el_val_t slot);
|
||||
el_val_t egy_Sm_present(el_val_t slot);
|
||||
el_val_t egy_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t egy_str_len(el_val_t s);
|
||||
el_val_t egy_suffix_pronoun(el_val_t slot);
|
||||
@@ -286,10 +298,12 @@ el_val_t es_starts_with_stressed_a(el_val_t noun);
|
||||
el_val_t es_stem(el_val_t base);
|
||||
el_val_t es_str_drop_last(el_val_t s, el_val_t n);
|
||||
el_val_t es_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t es_str_last_char(el_val_t s);
|
||||
el_val_t es_str_last2(el_val_t s);
|
||||
el_val_t es_str_last3(el_val_t s);
|
||||
el_val_t es_str_last_char(el_val_t s);
|
||||
el_val_t es_verb_class(el_val_t base);
|
||||
el_val_t exec_tool_block(el_val_t block);
|
||||
el_val_t extract_all_text(el_val_t s);
|
||||
el_val_t fi_apply_case(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t fi_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t fi_full_paradigm(el_val_t noun);
|
||||
@@ -307,6 +321,7 @@ el_val_t fi_str_last_char(el_val_t s);
|
||||
el_val_t fi_suffix(el_val_t base, el_val_t harmony);
|
||||
el_val_t fi_verb_stem(el_val_t dict_form);
|
||||
el_val_t find_rule(el_val_t rule_id_str);
|
||||
el_val_t flag_true(el_val_t body, el_val_t key);
|
||||
el_val_t fr_agree_article(el_val_t noun, el_val_t definite, el_val_t number);
|
||||
el_val_t fr_avoir_present(el_val_t slot);
|
||||
el_val_t fr_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
@@ -329,8 +344,8 @@ el_val_t fr_slot(el_val_t person, el_val_t number);
|
||||
el_val_t fr_stem(el_val_t base);
|
||||
el_val_t fr_str_drop_last(el_val_t s, el_val_t n);
|
||||
el_val_t fr_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t fr_str_last2(el_val_t s);
|
||||
el_val_t fr_str_last_char(el_val_t s);
|
||||
el_val_t fr_str_last2(el_val_t s);
|
||||
el_val_t fr_subject_starts_vowel(el_val_t subject);
|
||||
el_val_t fr_uses_etre(el_val_t verb);
|
||||
el_val_t fr_verb_ends_vowel(el_val_t verb_form);
|
||||
@@ -352,9 +367,9 @@ el_val_t fro_conj3_future(el_val_t verb, el_val_t slot);
|
||||
el_val_t fro_conj3_past(el_val_t stem, el_val_t slot);
|
||||
el_val_t fro_conj3_present(el_val_t stem, el_val_t slot);
|
||||
el_val_t fro_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t fro_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t fro_decline_fem(el_val_t noun, el_val_t number);
|
||||
el_val_t fro_decline_masc(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t fro_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t fro_drop(el_val_t s, el_val_t n);
|
||||
el_val_t fro_estre_future(el_val_t slot);
|
||||
el_val_t fro_estre_past(el_val_t slot);
|
||||
@@ -372,15 +387,15 @@ el_val_t fro_venir_past(el_val_t slot);
|
||||
el_val_t fro_venir_present(el_val_t slot);
|
||||
el_val_t fro_verb_class(el_val_t verb);
|
||||
el_val_t fro_verb_stem(el_val_t verb, el_val_t vclass);
|
||||
el_val_t generate(el_val_t semantic_form_json);
|
||||
el_val_t generate_frame(el_val_t frame);
|
||||
el_val_t generate_frame_lang(el_val_t frame, el_val_t lang_code);
|
||||
el_val_t generate_frame(el_val_t frame);
|
||||
el_val_t generate_lang(el_val_t semantic_form_json, el_val_t lang_code);
|
||||
el_val_t generate_tree(el_val_t rule_id_str, el_val_t slots);
|
||||
el_val_t generate(el_val_t semantic_form_json);
|
||||
el_val_t get_rules(void);
|
||||
el_val_t get_vocab(void);
|
||||
el_val_t gez_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t gez_conjugate_copula(el_val_t tense, el_val_t slot);
|
||||
el_val_t gez_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t gez_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t gez_generic_imperfect(el_val_t base3sg, el_val_t slot);
|
||||
el_val_t gez_generic_perfect(el_val_t base3sg, el_val_t slot);
|
||||
@@ -399,13 +414,12 @@ el_val_t gez_qwl_imperfect(el_val_t slot);
|
||||
el_val_t gez_qwl_perfect(el_val_t slot);
|
||||
el_val_t gez_ray_imperfect(el_val_t slot);
|
||||
el_val_t gez_ray_perfect(el_val_t slot);
|
||||
el_val_t gez_slot(el_val_t person, el_val_t number);
|
||||
el_val_t gez_slot_g(el_val_t person, el_val_t gender, el_val_t number);
|
||||
el_val_t gez_slot(el_val_t person, el_val_t number);
|
||||
el_val_t gez_str_drop_last(el_val_t s, el_val_t n);
|
||||
el_val_t gez_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t gez_str_len(el_val_t s);
|
||||
el_val_t goh_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t goh_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t goh_decline_fem_o_pl(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t goh_decline_fem_o_sg(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t goh_decline_masc_a_pl(el_val_t stem, el_val_t gram_case);
|
||||
@@ -414,6 +428,7 @@ el_val_t goh_decline_masc_n_pl(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t goh_decline_masc_n_sg(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t goh_decline_neut_a_pl(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t goh_decline_neut_a_sg(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t goh_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t goh_demo_article(el_val_t stype, el_val_t number);
|
||||
el_val_t goh_drop(el_val_t s, el_val_t n);
|
||||
el_val_t goh_extract_stem(el_val_t noun, el_val_t stype);
|
||||
@@ -438,13 +453,13 @@ el_val_t goh_weak_present(el_val_t stem, el_val_t slot);
|
||||
el_val_t goh_wesan_past(el_val_t slot);
|
||||
el_val_t goh_wesan_present(el_val_t slot);
|
||||
el_val_t got_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t got_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t got_decline_a_stem_pl(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t got_decline_a_stem_sg(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t got_decline_n_stem_pl(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t got_decline_n_stem_sg(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t got_decline_o_stem_pl(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t got_decline_o_stem_sg(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t got_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t got_demo_article(el_val_t stype);
|
||||
el_val_t got_extract_stem(el_val_t noun, el_val_t stype);
|
||||
el_val_t got_gaggan_past(el_val_t slot);
|
||||
@@ -477,17 +492,17 @@ el_val_t gram_build_vp(el_val_t verb, el_val_t aux, el_val_t profile);
|
||||
el_val_t gram_order_constituents(el_val_t subj, el_val_t verb, el_val_t obj, el_val_t profile);
|
||||
el_val_t gram_question_strategy(el_val_t profile);
|
||||
el_val_t gram_word_order(el_val_t profile);
|
||||
el_val_t grc_article(el_val_t gender, el_val_t gram_case, el_val_t number);
|
||||
el_val_t grc_article_feminine(el_val_t gram_case, el_val_t number);
|
||||
el_val_t grc_article_masculine(el_val_t gram_case, el_val_t number);
|
||||
el_val_t grc_article_neuter(el_val_t gram_case, el_val_t number);
|
||||
el_val_t grc_article(el_val_t gender, el_val_t gram_case, el_val_t number);
|
||||
el_val_t grc_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t grc_declension(el_val_t noun);
|
||||
el_val_t grc_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t grc_decline_1a(el_val_t stem, el_val_t gram_case, el_val_t number);
|
||||
el_val_t grc_decline_1e(el_val_t stem, el_val_t gram_case, el_val_t number);
|
||||
el_val_t grc_decline_2m(el_val_t stem, el_val_t gram_case, el_val_t number);
|
||||
el_val_t grc_decline_2n(el_val_t stem, el_val_t gram_case, el_val_t number);
|
||||
el_val_t grc_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t grc_echein_aorist(el_val_t slot);
|
||||
el_val_t grc_echein_future(el_val_t slot);
|
||||
el_val_t grc_echein_imperfect(el_val_t slot);
|
||||
@@ -514,9 +529,9 @@ el_val_t grc_present_stem(el_val_t verb);
|
||||
el_val_t grc_slot(el_val_t person, el_val_t number);
|
||||
el_val_t grc_str_drop_last(el_val_t s, el_val_t n);
|
||||
el_val_t grc_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t grc_str_last_char(el_val_t s);
|
||||
el_val_t grc_str_last2(el_val_t s);
|
||||
el_val_t grc_str_last3(el_val_t s);
|
||||
el_val_t grc_str_last_char(el_val_t s);
|
||||
el_val_t grc_thematic_future_ending(el_val_t slot);
|
||||
el_val_t grc_thematic_imperfect_ending(el_val_t slot);
|
||||
el_val_t grc_thematic_present_ending(el_val_t slot);
|
||||
@@ -535,27 +550,34 @@ el_val_t handle_api_link_entities(el_val_t body);
|
||||
el_val_t handle_api_list_state_events(el_val_t method, el_val_t path, el_val_t body);
|
||||
el_val_t handle_api_list_typed(el_val_t node_type, el_val_t path, el_val_t body);
|
||||
el_val_t handle_api_log_state_event(el_val_t body);
|
||||
el_val_t handle_api_node_create(el_val_t body);
|
||||
el_val_t handle_api_node_delete(el_val_t body);
|
||||
el_val_t handle_api_node_update(el_val_t body);
|
||||
el_val_t handle_api_promote_knowledge(el_val_t body);
|
||||
el_val_t handle_api_recall(el_val_t method, el_val_t path, el_val_t body);
|
||||
el_val_t handle_api_remember(el_val_t body);
|
||||
el_val_t handle_api_search_knowledge(el_val_t method, el_val_t path, el_val_t body);
|
||||
el_val_t handle_api_tune_config(el_val_t body);
|
||||
el_val_t handle_chat(el_val_t body);
|
||||
el_val_t handle_chat_agentic(el_val_t body);
|
||||
el_val_t handle_chat_as_soul(el_val_t body);
|
||||
el_val_t handle_chat(el_val_t body);
|
||||
el_val_t handle_config(el_val_t method, el_val_t body);
|
||||
el_val_t handle_connectors(el_val_t method, el_val_t clean, el_val_t body);
|
||||
el_val_t handle_conversations(el_val_t method);
|
||||
el_val_t handle_dharma(el_val_t path, el_val_t method, el_val_t body);
|
||||
el_val_t handle_dharma_recv(el_val_t body);
|
||||
el_val_t handle_dharma_room_turn(el_val_t body);
|
||||
el_val_t handle_dharma_room_turn_agentic(el_val_t body);
|
||||
el_val_t handle_dharma_room_turn(el_val_t body);
|
||||
el_val_t handle_dharma(el_val_t path, el_val_t method, el_val_t body);
|
||||
el_val_t handle_elp_chat(el_val_t body);
|
||||
el_val_t handle_nlg(el_val_t path, el_val_t method, el_val_t body);
|
||||
el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body);
|
||||
el_val_t handle_safety_contact_get(void);
|
||||
el_val_t handle_safety_contact_post(el_val_t body);
|
||||
el_val_t handle_see(el_val_t body);
|
||||
el_val_t handle_session_approve(el_val_t session_id, el_val_t body);
|
||||
el_val_t handle_tool(el_val_t path, el_val_t method, el_val_t body);
|
||||
el_val_t he_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t gender, el_val_t number);
|
||||
el_val_t he_conjugate_copula(el_val_t tense, el_val_t slot);
|
||||
el_val_t he_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t gender, el_val_t number);
|
||||
el_val_t he_copula_future(el_val_t slot);
|
||||
el_val_t he_copula_past(el_val_t slot);
|
||||
el_val_t he_definite_prefix(el_val_t noun);
|
||||
@@ -592,12 +614,12 @@ el_val_t hi_genitive_phrase(el_val_t possessor, el_val_t possessor_gender, el_va
|
||||
el_val_t hi_hona_past(el_val_t gender, el_val_t number);
|
||||
el_val_t hi_hona_present(el_val_t person, el_val_t number);
|
||||
el_val_t hi_masc_aa_stem(el_val_t noun);
|
||||
el_val_t hi_noun_direct(el_val_t noun, el_val_t gender, el_val_t number);
|
||||
el_val_t hi_noun_direct_f(el_val_t noun, el_val_t number);
|
||||
el_val_t hi_noun_direct_m(el_val_t noun, el_val_t number);
|
||||
el_val_t hi_noun_oblique(el_val_t noun, el_val_t gender, el_val_t number);
|
||||
el_val_t hi_noun_direct(el_val_t noun, el_val_t gender, el_val_t number);
|
||||
el_val_t hi_noun_oblique_f(el_val_t noun, el_val_t number);
|
||||
el_val_t hi_noun_oblique_m(el_val_t noun, el_val_t number);
|
||||
el_val_t hi_noun_oblique(el_val_t noun, el_val_t gender, el_val_t number);
|
||||
el_val_t hi_noun_with_post(el_val_t noun, el_val_t gender, el_val_t number, el_val_t gram_case);
|
||||
el_val_t hi_past_irregular(el_val_t stem, el_val_t gender, el_val_t number);
|
||||
el_val_t hi_past_suffix(el_val_t gender, el_val_t number);
|
||||
@@ -607,8 +629,8 @@ el_val_t hi_str_drop_last(el_val_t s, el_val_t n);
|
||||
el_val_t hi_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t hi_str_last_char(el_val_t s);
|
||||
el_val_t hi_tense_suffix(el_val_t tense, el_val_t gender, el_val_t number);
|
||||
el_val_t hi_verb_stem(el_val_t infinitive);
|
||||
el_val_t hi_verb_stem_clean(el_val_t infinitive);
|
||||
el_val_t hi_verb_stem(el_val_t infinitive);
|
||||
el_val_t hist_append(el_val_t hist, el_val_t role, el_val_t content);
|
||||
el_val_t hist_trim(el_val_t hist);
|
||||
el_val_t init_soul_edges(void);
|
||||
@@ -624,10 +646,10 @@ el_val_t ja_noun_phrase(el_val_t noun, el_val_t gram_case);
|
||||
el_val_t ja_particle(el_val_t gram_case);
|
||||
el_val_t ja_question_particle(void);
|
||||
el_val_t ja_verb_group(el_val_t dict_form);
|
||||
el_val_t json_array_append(el_val_t arr, el_val_t item);
|
||||
el_val_t json_safe(el_val_t s);
|
||||
el_val_t la_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t la_declension(el_val_t noun);
|
||||
el_val_t la_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t la_decline_1(el_val_t stem, el_val_t gram_case, el_val_t number);
|
||||
el_val_t la_decline_2er(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t la_decline_2m(el_val_t stem, el_val_t gram_case, el_val_t number);
|
||||
@@ -635,6 +657,7 @@ el_val_t la_decline_2n(el_val_t stem, el_val_t gram_case, el_val_t number);
|
||||
el_val_t la_decline_3(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t la_decline_4(el_val_t stem, el_val_t gram_case, el_val_t number);
|
||||
el_val_t la_decline_5(el_val_t stem, el_val_t gram_case, el_val_t number);
|
||||
el_val_t la_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t la_esse_future(el_val_t slot);
|
||||
el_val_t la_esse_past(el_val_t slot);
|
||||
el_val_t la_esse_present(el_val_t slot);
|
||||
@@ -658,9 +681,9 @@ el_val_t la_slot(el_val_t person, el_val_t number);
|
||||
el_val_t la_stem(el_val_t verb, el_val_t vclass);
|
||||
el_val_t la_str_drop_last(el_val_t s, el_val_t n);
|
||||
el_val_t la_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t la_str_last_char(el_val_t s);
|
||||
el_val_t la_str_last2(el_val_t s);
|
||||
el_val_t la_str_last3(el_val_t s);
|
||||
el_val_t la_str_last_char(el_val_t s);
|
||||
el_val_t la_velle_future(el_val_t slot);
|
||||
el_val_t la_velle_past(el_val_t slot);
|
||||
el_val_t la_velle_present(el_val_t slot);
|
||||
@@ -677,7 +700,6 @@ el_val_t lang_is_fusional(el_val_t profile);
|
||||
el_val_t lang_is_isolating(el_val_t profile);
|
||||
el_val_t lang_is_polysynthetic(el_val_t profile);
|
||||
el_val_t lang_is_rtl(el_val_t profile);
|
||||
el_val_t lang_profile(el_val_t code, el_val_t word_order, el_val_t morph_type, el_val_t has_case, el_val_t has_gender, el_val_t script_dir, el_val_t agreement, el_val_t null_subject);
|
||||
el_val_t lang_profile_akk(void);
|
||||
el_val_t lang_profile_ang(void);
|
||||
el_val_t lang_profile_ar(void);
|
||||
@@ -709,6 +731,7 @@ el_val_t lang_profile_sw(void);
|
||||
el_val_t lang_profile_txb(void);
|
||||
el_val_t lang_profile_uga(void);
|
||||
el_val_t lang_profile_zh(void);
|
||||
el_val_t lang_profile(el_val_t code, el_val_t word_order, el_val_t morph_type, el_val_t has_case, el_val_t has_gender, el_val_t script_dir, el_val_t agreement, el_val_t null_subject);
|
||||
el_val_t lang_word_order(el_val_t profile);
|
||||
el_val_t lex_class(el_val_t entry);
|
||||
el_val_t lex_form(el_val_t entry, el_val_t idx);
|
||||
@@ -753,10 +776,10 @@ el_val_t morph_map_canonical(el_val_t verb, el_val_t code);
|
||||
el_val_t morph_pluralize(el_val_t noun, el_val_t profile);
|
||||
el_val_t nlg_is_ws(el_val_t c);
|
||||
el_val_t non_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t non_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t non_decline_fem(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t non_decline_masc(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t non_decline_neut(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t non_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t non_def_suffix_fem(el_val_t gram_case, el_val_t number);
|
||||
el_val_t non_def_suffix_masc(el_val_t gram_case, el_val_t number);
|
||||
el_val_t non_def_suffix_neut(el_val_t gram_case, el_val_t number);
|
||||
@@ -787,8 +810,8 @@ el_val_t peo_ah_present(el_val_t slot);
|
||||
el_val_t peo_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t peo_da_past(el_val_t slot);
|
||||
el_val_t peo_da_present(el_val_t slot);
|
||||
el_val_t peo_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t peo_decline_astem(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t peo_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t peo_drop(el_val_t s, el_val_t n);
|
||||
el_val_t peo_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t peo_kar_past(el_val_t slot);
|
||||
@@ -804,11 +827,11 @@ el_val_t perceive(void);
|
||||
el_val_t pi_aorist_ending(el_val_t slot);
|
||||
el_val_t pi_atthi_present(el_val_t slot);
|
||||
el_val_t pi_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t pi_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t pi_decline_a_fem_pl(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t pi_decline_a_fem_sg(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t pi_decline_a_masc_pl(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t pi_decline_a_masc_sg(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t pi_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t pi_detect_class(el_val_t noun);
|
||||
el_val_t pi_drop(el_val_t s, el_val_t n);
|
||||
el_val_t pi_future_ending(el_val_t slot);
|
||||
@@ -837,11 +860,11 @@ el_val_t pi_vadati_present(el_val_t slot);
|
||||
el_val_t pluralize(el_val_t singular);
|
||||
el_val_t pulse_count(void);
|
||||
el_val_t pulse_inc(void);
|
||||
el_val_t realize(el_val_t form);
|
||||
el_val_t realize_lang(el_val_t form, el_val_t profile);
|
||||
el_val_t realize_np(el_val_t referent, el_val_t number);
|
||||
el_val_t realize_question_lang(el_val_t predicate, el_val_t tense, el_val_t aspect, el_val_t person, el_val_t number, el_val_t agent, el_val_t patient, el_val_t location, el_val_t profile);
|
||||
el_val_t realize_vp_lang(el_val_t base_verb, el_val_t tense, el_val_t aspect, el_val_t person, el_val_t number, el_val_t profile);
|
||||
el_val_t realize(el_val_t form);
|
||||
el_val_t record(el_val_t outcome_json);
|
||||
el_val_t render_studio(void);
|
||||
el_val_t render_tree(el_val_t tree);
|
||||
@@ -852,9 +875,9 @@ el_val_t route_imprint_user(el_val_t body);
|
||||
el_val_t route_lineage(void);
|
||||
el_val_t route_sessions(void);
|
||||
el_val_t route_synthesize(el_val_t body);
|
||||
el_val_t ru_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number, el_val_t gender);
|
||||
el_val_t ru_conjugate_1st(el_val_t stem, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t ru_conjugate_2nd(el_val_t stem, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t ru_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number, el_val_t gender);
|
||||
el_val_t ru_decline_fem(el_val_t noun, el_val_t stype, el_val_t gram_case, el_val_t number);
|
||||
el_val_t ru_decline_masc(el_val_t noun, el_val_t stype, el_val_t gram_case, el_val_t number);
|
||||
el_val_t ru_decline_neut(el_val_t noun, el_val_t stype, el_val_t gram_case, el_val_t number);
|
||||
@@ -867,8 +890,8 @@ el_val_t ru_past_stem(el_val_t verb);
|
||||
el_val_t ru_stem_type(el_val_t noun, el_val_t gender);
|
||||
el_val_t rule_id(el_val_t rule);
|
||||
el_val_t rule_lhs(el_val_t rule);
|
||||
el_val_t rule_rhs(el_val_t rule, el_val_t idx);
|
||||
el_val_t rule_rhs_len(el_val_t rule);
|
||||
el_val_t rule_rhs(el_val_t rule, el_val_t idx);
|
||||
el_val_t sa_as_future(el_val_t slot);
|
||||
el_val_t sa_as_past(el_val_t slot);
|
||||
el_val_t sa_as_present(el_val_t slot);
|
||||
@@ -880,11 +903,11 @@ el_val_t sa_class1_future_ending(el_val_t slot);
|
||||
el_val_t sa_class1_past_ending(el_val_t slot);
|
||||
el_val_t sa_class1_present_ending(el_val_t slot);
|
||||
el_val_t sa_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t sa_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t sa_decline_a_stem_pl(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t sa_decline_a_stem_sg(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t sa_decline_aa_stem_pl(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t sa_decline_aa_stem_sg(el_val_t stem, el_val_t gram_case);
|
||||
el_val_t sa_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t sa_drs_future(el_val_t slot);
|
||||
el_val_t sa_drs_past(el_val_t slot);
|
||||
el_val_t sa_drs_present(el_val_t slot);
|
||||
@@ -904,25 +927,38 @@ el_val_t sa_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t sa_vad_future(el_val_t slot);
|
||||
el_val_t sa_vad_past(el_val_t slot);
|
||||
el_val_t sa_vad_present(el_val_t slot);
|
||||
el_val_t safety_abuse_phrases(void);
|
||||
el_val_t safety_any_match(el_val_t text, el_val_t phrases_json);
|
||||
el_val_t safety_augment_system(el_val_t system, el_val_t user_msg);
|
||||
el_val_t safety_classify_hard_bell(el_val_t message);
|
||||
el_val_t safety_contact_path(void);
|
||||
el_val_t safety_count_match(el_val_t text, el_val_t phrases_json);
|
||||
el_val_t safety_detect_bell_level(el_val_t message);
|
||||
el_val_t safety_general_hard_phrases(void);
|
||||
el_val_t safety_hard_directive(el_val_t hard_type);
|
||||
el_val_t safety_normalize(el_val_t message);
|
||||
el_val_t safety_self_harm_phrases(void);
|
||||
el_val_t safety_soft_directive(void);
|
||||
el_val_t safety_soft_phrases(void);
|
||||
el_val_t scan_token(el_val_t s, el_val_t start);
|
||||
el_val_t sem_first_modifier(el_val_t mods);
|
||||
el_val_t sem_frame(el_val_t intent, el_val_t subject, el_val_t obj, el_val_t modifiers);
|
||||
el_val_t sem_frame_lang(el_val_t intent, el_val_t subject, el_val_t obj, el_val_t modifiers, el_val_t lang_code);
|
||||
el_val_t sem_frame_obj(el_val_t intent, el_val_t subject, el_val_t obj);
|
||||
el_val_t sem_frame_simple(el_val_t intent, el_val_t subject);
|
||||
el_val_t sem_frame(el_val_t intent, el_val_t subject, el_val_t obj, el_val_t modifiers);
|
||||
el_val_t sem_get(el_val_t json, el_val_t key);
|
||||
el_val_t sem_intent(el_val_t frame);
|
||||
el_val_t sem_intent_to_realize(el_val_t intent);
|
||||
el_val_t sem_intent(el_val_t frame);
|
||||
el_val_t sem_lang(el_val_t frame);
|
||||
el_val_t sem_modifiers(el_val_t frame);
|
||||
el_val_t sem_object(el_val_t frame);
|
||||
el_val_t sem_realize(el_val_t frame);
|
||||
el_val_t sem_realize_full(el_val_t frame, el_val_t verb, el_val_t tense, el_val_t aspect);
|
||||
el_val_t sem_realize_greet(el_val_t subject);
|
||||
el_val_t sem_realize_lang(el_val_t frame, el_val_t lang_code);
|
||||
el_val_t sem_realize(el_val_t frame);
|
||||
el_val_t sem_subject(el_val_t frame);
|
||||
el_val_t sem_to_spec(el_val_t frame);
|
||||
el_val_t sem_to_spec_full(el_val_t frame, el_val_t verb, el_val_t tense, el_val_t aspect);
|
||||
el_val_t sem_to_spec(el_val_t frame);
|
||||
el_val_t sga_adci_present(el_val_t slot);
|
||||
el_val_t sga_ai_present(el_val_t stem, el_val_t slot);
|
||||
el_val_t sga_asbeir_present(el_val_t slot);
|
||||
@@ -930,9 +966,9 @@ el_val_t sga_bith_past(el_val_t slot);
|
||||
el_val_t sga_bith_present(el_val_t slot);
|
||||
el_val_t sga_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t sga_copula_present(el_val_t slot);
|
||||
el_val_t sga_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t sga_decline_astem(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t sga_decline_ostem(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t sga_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t sga_detect_gender(el_val_t noun);
|
||||
el_val_t sga_drop(el_val_t s, el_val_t n);
|
||||
el_val_t sga_first(el_val_t s);
|
||||
@@ -948,12 +984,14 @@ el_val_t singularize(el_val_t plural);
|
||||
el_val_t skip_ws(el_val_t s, el_val_t pos);
|
||||
el_val_t slots_get(el_val_t slots, el_val_t key);
|
||||
el_val_t slots_set(el_val_t slots, el_val_t key, el_val_t val);
|
||||
el_val_t str_begins(el_val_t s, el_val_t pre);
|
||||
el_val_t str_drop_last(el_val_t s, el_val_t n);
|
||||
el_val_t str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t str_last_char(el_val_t s);
|
||||
el_val_t str_last2(el_val_t s);
|
||||
el_val_t str_last3(el_val_t s);
|
||||
el_val_t str_last_char(el_val_t s);
|
||||
el_val_t strengthen_chat_nodes(el_val_t activation_nodes);
|
||||
el_val_t strip_citations(el_val_t s);
|
||||
el_val_t strip_query(el_val_t path);
|
||||
el_val_t studio_tools_json(void);
|
||||
el_val_t sux_absolutive_suffix(el_val_t person, el_val_t number);
|
||||
@@ -979,8 +1017,8 @@ el_val_t sux_realize_sentence(el_val_t intent, el_val_t agent, el_val_t predicat
|
||||
el_val_t sux_slot(el_val_t person, el_val_t number);
|
||||
el_val_t sux_str_drop_last(el_val_t s, el_val_t n);
|
||||
el_val_t sux_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t sux_str_last2(el_val_t s);
|
||||
el_val_t sux_str_last_char(el_val_t s);
|
||||
el_val_t sux_str_last2(el_val_t s);
|
||||
el_val_t sux_tum2_past(el_val_t slot);
|
||||
el_val_t sux_tum2_present(el_val_t slot);
|
||||
el_val_t sux_verb_chain(el_val_t agent, el_val_t verb, el_val_t patient, el_val_t tense);
|
||||
@@ -998,9 +1036,9 @@ el_val_t sw_noun_plural(el_val_t noun);
|
||||
el_val_t sw_obj_prefix(el_val_t person, el_val_t number, el_val_t noun_class);
|
||||
el_val_t sw_str_drop_last(el_val_t s, el_val_t n);
|
||||
el_val_t sw_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t sw_str_first_char(el_val_t s);
|
||||
el_val_t sw_str_first2(el_val_t s);
|
||||
el_val_t sw_str_first3(el_val_t s);
|
||||
el_val_t sw_str_first_char(el_val_t s);
|
||||
el_val_t sw_str_last_char(el_val_t s);
|
||||
el_val_t sw_subj_prefix(el_val_t person, el_val_t number, el_val_t noun_class);
|
||||
el_val_t sw_tense_marker(el_val_t tense);
|
||||
@@ -1009,10 +1047,11 @@ el_val_t sw_verb_stem(el_val_t infinitive);
|
||||
el_val_t tier_canonical(void);
|
||||
el_val_t tier_episodic(void);
|
||||
el_val_t tier_working(void);
|
||||
el_val_t tool_auto_approved(el_val_t tool_name);
|
||||
el_val_t txb_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t txb_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t txb_decline_fem(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t txb_decline_masc(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t txb_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t txb_detect_gender(el_val_t noun);
|
||||
el_val_t txb_drop(el_val_t s, el_val_t n);
|
||||
el_val_t txb_ends(el_val_t s, el_val_t suf);
|
||||
@@ -1027,8 +1066,8 @@ el_val_t txb_wes_present(el_val_t slot);
|
||||
el_val_t txb_ya_present(el_val_t slot);
|
||||
el_val_t uga_amr_imperfect(el_val_t slot);
|
||||
el_val_t uga_amr_perfect(el_val_t slot);
|
||||
el_val_t uga_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t uga_conjugate_copula(el_val_t tense, el_val_t slot);
|
||||
el_val_t uga_conjugate(el_val_t verb, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t uga_decline(el_val_t noun, el_val_t gram_case, el_val_t number);
|
||||
el_val_t uga_generic_imperfect(el_val_t base3sg, el_val_t slot);
|
||||
el_val_t uga_generic_perfect(el_val_t base3sg, el_val_t slot);
|
||||
@@ -1043,8 +1082,8 @@ el_val_t uga_map_canonical(el_val_t verb);
|
||||
el_val_t uga_noun_phrase(el_val_t noun, el_val_t gram_case, el_val_t number, el_val_t definite);
|
||||
el_val_t uga_ray_imperfect(el_val_t slot);
|
||||
el_val_t uga_ray_perfect(el_val_t slot);
|
||||
el_val_t uga_slot(el_val_t person, el_val_t number);
|
||||
el_val_t uga_slot_g(el_val_t person, el_val_t gender, el_val_t number);
|
||||
el_val_t uga_slot(el_val_t person, el_val_t number);
|
||||
el_val_t uga_str_drop_last(el_val_t s, el_val_t n);
|
||||
el_val_t uga_str_ends(el_val_t s, el_val_t suf);
|
||||
el_val_t uga_str_len(el_val_t s);
|
||||
@@ -1052,6 +1091,6 @@ el_val_t uga_strip_nom(el_val_t noun);
|
||||
el_val_t verb_form(el_val_t base, el_val_t tense, el_val_t person, el_val_t number);
|
||||
el_val_t vocab_by_class(el_val_t cls);
|
||||
el_val_t vocab_by_pos(el_val_t pos);
|
||||
el_val_t vocab_lookup(el_val_t word, el_val_t lang_code);
|
||||
el_val_t vocab_lookup_en(el_val_t word);
|
||||
el_val_t vocab_lookup(el_val_t word, el_val_t lang_code);
|
||||
el_val_t vocab_synonym(el_val_t word, el_val_t lang_register, el_val_t lang_code);
|
||||
|
||||
Vendored
+127
-29
@@ -24,9 +24,14 @@ el_val_t api_ok(el_val_t extra);
|
||||
el_val_t api_err(el_val_t msg);
|
||||
el_val_t api_nonempty(el_val_t s);
|
||||
el_val_t api_or_empty(el_val_t s);
|
||||
el_val_t api_persisted(el_val_t id);
|
||||
el_val_t api_not_persisted(el_val_t id);
|
||||
el_val_t handle_api_begin_session(el_val_t body);
|
||||
el_val_t handle_api_compile_ctx(el_val_t body);
|
||||
el_val_t handle_api_remember(el_val_t body);
|
||||
el_val_t handle_api_node_create(el_val_t body);
|
||||
el_val_t handle_api_node_delete(el_val_t body);
|
||||
el_val_t handle_api_node_update(el_val_t body);
|
||||
el_val_t handle_api_recall(el_val_t method, el_val_t path, el_val_t body);
|
||||
el_val_t handle_api_search_knowledge(el_val_t method, el_val_t path, el_val_t body);
|
||||
el_val_t handle_api_browse_knowledge(el_val_t path, el_val_t body);
|
||||
@@ -108,6 +113,20 @@ el_val_t api_or_empty(el_val_t s) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t api_persisted(el_val_t id) {
|
||||
if (str_eq(id, EL_STR(""))) {
|
||||
return 0;
|
||||
}
|
||||
el_val_t node = engram_get_node_json(id);
|
||||
return (!str_eq(node, EL_STR("")) && !str_eq(node, EL_STR("null")));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t api_not_persisted(el_val_t id) {
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"ok\":false,\"error\":\"write_not_persisted\",\"id\":\""), id), EL_STR("\"}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t handle_api_begin_session(el_val_t body) {
|
||||
el_val_t stats = engram_stats_json();
|
||||
el_val_t activated = engram_activate_json(EL_STR("session start recent memory important"), 2);
|
||||
@@ -139,17 +158,87 @@ el_val_t handle_api_remember(el_val_t body) {
|
||||
el_val_t base_tags = ({ el_val_t _if_result_7 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_7 = (EL_STR("[\"Memory\"]")); } else { _if_result_7 = (tags_raw); } _if_result_7; });
|
||||
el_val_t final_tags = ({ el_val_t _if_result_8 = 0; if (str_eq(project, EL_STR(""))) { _if_result_8 = (base_tags); } else { el_val_t inner = str_slice(base_tags, 1, (str_len(base_tags) - 1)); _if_result_8 = (el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("["), inner), EL_STR(",\"project:")), project), EL_STR("\"]"))); } _if_result_8; });
|
||||
el_val_t id = engram_node_full(content, EL_STR("Memory"), EL_STR("memory:remembered"), el_from_float(sal), el_from_float(sal), el_from_float(0.9), EL_STR("Episodic"), final_tags);
|
||||
if (!api_persisted(id)) {
|
||||
return api_not_persisted(id);
|
||||
}
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"id\":\""), id), EL_STR("\",\"ok\":true}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t handle_api_node_create(el_val_t body) {
|
||||
el_val_t content = json_get(body, EL_STR("content"));
|
||||
if (str_eq(content, EL_STR(""))) {
|
||||
return api_err(EL_STR("content is required"));
|
||||
}
|
||||
el_val_t nt_raw = json_get(body, EL_STR("node_type"));
|
||||
el_val_t node_type = ({ el_val_t _if_result_9 = 0; if (str_eq(nt_raw, EL_STR(""))) { _if_result_9 = (EL_STR("Memory")); } else { _if_result_9 = (nt_raw); } _if_result_9; });
|
||||
el_val_t label_raw = json_get(body, EL_STR("label"));
|
||||
el_val_t label = ({ el_val_t _if_result_10 = 0; if (str_eq(label_raw, EL_STR(""))) { _if_result_10 = (EL_STR("node:created")); } else { _if_result_10 = (label_raw); } _if_result_10; });
|
||||
el_val_t tier_raw = json_get(body, EL_STR("tier"));
|
||||
el_val_t tier = ({ el_val_t _if_result_11 = 0; if (str_eq(tier_raw, EL_STR(""))) { _if_result_11 = (EL_STR("Episodic")); } else { _if_result_11 = (tier_raw); } _if_result_11; });
|
||||
el_val_t tags_raw = json_get(body, EL_STR("tags"));
|
||||
el_val_t tags = ({ el_val_t _if_result_12 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_12 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_12 = (tags_raw); } _if_result_12; });
|
||||
el_val_t importance = json_get(body, EL_STR("importance"));
|
||||
el_val_t sal = ({ el_val_t _if_result_13 = 0; if (str_eq(importance, EL_STR("critical"))) { _if_result_13 = (el_from_float(0.95)); } else { _if_result_13 = (({ el_val_t _if_result_14 = 0; if (str_eq(importance, EL_STR("high"))) { _if_result_14 = (el_from_float(0.75)); } else { _if_result_14 = (({ el_val_t _if_result_15 = 0; if (str_eq(importance, EL_STR("low"))) { _if_result_15 = (el_from_float(0.25)); } else { _if_result_15 = (el_from_float(0.5)); } _if_result_15; })); } _if_result_14; })); } _if_result_13; });
|
||||
el_val_t id = engram_node_full(content, node_type, label, el_from_float(sal), el_from_float(sal), el_from_float(0.9), tier, tags);
|
||||
if (!api_persisted(id)) {
|
||||
return api_not_persisted(id);
|
||||
}
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"id\":\""), id), EL_STR("\",\"ok\":true}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t handle_api_node_delete(el_val_t body) {
|
||||
el_val_t id = json_get(body, EL_STR("id"));
|
||||
if (str_eq(id, EL_STR(""))) {
|
||||
return api_err(EL_STR("id is required"));
|
||||
}
|
||||
engram_forget(id);
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"id\":\""), id), EL_STR("\"}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t handle_api_node_update(el_val_t body) {
|
||||
el_val_t id = json_get(body, EL_STR("id"));
|
||||
if (str_eq(id, EL_STR(""))) {
|
||||
return api_err(EL_STR("id is required"));
|
||||
}
|
||||
if (!api_persisted(id)) {
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"ok\":false,\"error\":\"not_found\",\"id\":\""), id), EL_STR("\"}"));
|
||||
}
|
||||
el_val_t old = engram_get_node_json(id);
|
||||
el_val_t body_content = json_get(body, EL_STR("content"));
|
||||
el_val_t content = ({ el_val_t _if_result_16 = 0; if (str_eq(body_content, EL_STR(""))) { _if_result_16 = (json_get(old, EL_STR("content"))); } else { _if_result_16 = (body_content); } _if_result_16; });
|
||||
el_val_t body_nt = json_get(body, EL_STR("node_type"));
|
||||
el_val_t old_nt = json_get(old, EL_STR("node_type"));
|
||||
el_val_t node_type = ({ el_val_t _if_result_17 = 0; if (!str_eq(body_nt, EL_STR(""))) { _if_result_17 = (body_nt); } else { _if_result_17 = (({ el_val_t _if_result_18 = 0; if (!str_eq(old_nt, EL_STR(""))) { _if_result_18 = (old_nt); } else { _if_result_18 = (EL_STR("Memory")); } _if_result_18; })); } _if_result_17; });
|
||||
el_val_t body_label = json_get(body, EL_STR("label"));
|
||||
el_val_t old_label = json_get(old, EL_STR("label"));
|
||||
el_val_t label = ({ el_val_t _if_result_19 = 0; if (!str_eq(body_label, EL_STR(""))) { _if_result_19 = (body_label); } else { _if_result_19 = (({ el_val_t _if_result_20 = 0; if (!str_eq(old_label, EL_STR(""))) { _if_result_20 = (old_label); } else { _if_result_20 = (EL_STR("node:updated")); } _if_result_20; })); } _if_result_19; });
|
||||
el_val_t body_tier = json_get(body, EL_STR("tier"));
|
||||
el_val_t old_tier = json_get(old, EL_STR("tier"));
|
||||
el_val_t tier = ({ el_val_t _if_result_21 = 0; if (!str_eq(body_tier, EL_STR(""))) { _if_result_21 = (body_tier); } else { _if_result_21 = (({ el_val_t _if_result_22 = 0; if (!str_eq(old_tier, EL_STR(""))) { _if_result_22 = (old_tier); } else { _if_result_22 = (EL_STR("Episodic")); } _if_result_22; })); } _if_result_21; });
|
||||
el_val_t body_tags = json_get(body, EL_STR("tags"));
|
||||
el_val_t tags = ({ el_val_t _if_result_23 = 0; if (str_eq(body_tags, EL_STR(""))) { _if_result_23 = (el_str_concat(el_str_concat(EL_STR("[\""), node_type), EL_STR("\"]"))); } else { _if_result_23 = (body_tags); } _if_result_23; });
|
||||
el_val_t new_id = engram_node_full(content, node_type, label, el_from_float(0.5), el_from_float(0.5), el_from_float(0.8), tier, tags);
|
||||
if (!api_persisted(new_id)) {
|
||||
return api_not_persisted(new_id);
|
||||
}
|
||||
engram_forget(id);
|
||||
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), new_id), EL_STR("\",\"replaced\":\"")), id), EL_STR("\",\"ok\":true}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t handle_api_recall(el_val_t method, el_val_t path, el_val_t body) {
|
||||
el_val_t q = ({ el_val_t _if_result_9 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_9 = (api_query_param(path, EL_STR("query"))); } else { _if_result_9 = (json_get(body, EL_STR("query"))); } _if_result_9; });
|
||||
el_val_t url_q = ({ el_val_t _if_result_24 = 0; if (str_eq(api_query_param(path, EL_STR("query")), EL_STR(""))) { _if_result_24 = (api_query_param(path, EL_STR("q"))); } else { _if_result_24 = (api_query_param(path, EL_STR("query"))); } _if_result_24; });
|
||||
el_val_t body_query = json_get(body, EL_STR("query"));
|
||||
el_val_t body_q = json_get(body, EL_STR("q"));
|
||||
el_val_t q = ({ el_val_t _if_result_25 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_25 = (url_q); } else { _if_result_25 = (({ el_val_t _if_result_26 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_26 = (body_query); } else { _if_result_26 = (body_q); } _if_result_26; })); } _if_result_25; });
|
||||
el_val_t chain = json_get(body, EL_STR("chain_name"));
|
||||
el_val_t limit = api_query_int(path, EL_STR("limit"), 0);
|
||||
limit = ({ el_val_t _if_result_10 = 0; if ((limit == 0)) { _if_result_10 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_10 = (limit); } _if_result_10; });
|
||||
limit = ({ el_val_t _if_result_11 = 0; if ((limit == 0)) { _if_result_11 = (10); } else { _if_result_11 = (limit); } _if_result_11; });
|
||||
el_val_t eff_q = ({ el_val_t _if_result_12 = 0; if (str_eq(q, EL_STR(""))) { _if_result_12 = (chain); } else { _if_result_12 = (q); } _if_result_12; });
|
||||
limit = ({ el_val_t _if_result_27 = 0; if ((limit == 0)) { _if_result_27 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_27 = (limit); } _if_result_27; });
|
||||
limit = ({ el_val_t _if_result_28 = 0; if ((limit == 0)) { _if_result_28 = (10); } else { _if_result_28 = (limit); } _if_result_28; });
|
||||
el_val_t eff_q = ({ el_val_t _if_result_29 = 0; if (str_eq(q, EL_STR(""))) { _if_result_29 = (chain); } else { _if_result_29 = (q); } _if_result_29; });
|
||||
if (str_eq(eff_q, EL_STR(""))) {
|
||||
return api_or_empty(engram_scan_nodes_json(limit, 0));
|
||||
}
|
||||
@@ -159,10 +248,13 @@ el_val_t handle_api_recall(el_val_t method, el_val_t path, el_val_t body) {
|
||||
}
|
||||
|
||||
el_val_t handle_api_search_knowledge(el_val_t method, el_val_t path, el_val_t body) {
|
||||
el_val_t q = ({ el_val_t _if_result_13 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_13 = (api_query_param(path, EL_STR("q"))); } else { _if_result_13 = (json_get(body, EL_STR("query"))); } _if_result_13; });
|
||||
el_val_t url_q = api_query_param(path, EL_STR("q"));
|
||||
el_val_t body_query = json_get(body, EL_STR("query"));
|
||||
el_val_t body_q = json_get(body, EL_STR("q"));
|
||||
el_val_t q = ({ el_val_t _if_result_30 = 0; if (!str_eq(url_q, EL_STR(""))) { _if_result_30 = (url_q); } else { _if_result_30 = (({ el_val_t _if_result_31 = 0; if (!str_eq(body_query, EL_STR(""))) { _if_result_31 = (body_query); } else { _if_result_31 = (body_q); } _if_result_31; })); } _if_result_30; });
|
||||
el_val_t limit = api_query_int(path, EL_STR("limit"), 0);
|
||||
limit = ({ el_val_t _if_result_14 = 0; if ((limit == 0)) { _if_result_14 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_14 = (limit); } _if_result_14; });
|
||||
limit = ({ el_val_t _if_result_15 = 0; if ((limit == 0)) { _if_result_15 = (10); } else { _if_result_15 = (limit); } _if_result_15; });
|
||||
limit = ({ el_val_t _if_result_32 = 0; if ((limit == 0)) { _if_result_32 = (json_get_int(body, EL_STR("limit"))); } else { _if_result_32 = (limit); } _if_result_32; });
|
||||
limit = ({ el_val_t _if_result_33 = 0; if ((limit == 0)) { _if_result_33 = (10); } else { _if_result_33 = (limit); } _if_result_33; });
|
||||
if (str_eq(q, EL_STR(""))) {
|
||||
return api_err(EL_STR("query is required"));
|
||||
}
|
||||
@@ -190,9 +282,12 @@ el_val_t handle_api_capture_knowledge(el_val_t body) {
|
||||
if (str_eq(content, EL_STR(""))) {
|
||||
return api_err(EL_STR("content is required"));
|
||||
}
|
||||
el_val_t full = ({ el_val_t _if_result_16 = 0; if (str_eq(title, EL_STR(""))) { _if_result_16 = (content); } else { _if_result_16 = (el_str_concat(el_str_concat(title, EL_STR(": ")), content)); } _if_result_16; });
|
||||
el_val_t full = ({ el_val_t _if_result_34 = 0; if (str_eq(title, EL_STR(""))) { _if_result_34 = (content); } else { _if_result_34 = (el_str_concat(el_str_concat(title, EL_STR(": ")), content)); } _if_result_34; });
|
||||
el_val_t tags = EL_STR("[\"Knowledge\",\"captured\"]");
|
||||
el_val_t id = engram_node_full(full, EL_STR("Knowledge"), EL_STR("knowledge:captured"), el_from_float(0.85), el_from_float(0.8), el_from_float(0.9), EL_STR("Episodic"), tags);
|
||||
if (!api_persisted(id)) {
|
||||
return api_not_persisted(id);
|
||||
}
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"id\":\""), id), EL_STR("\",\"ok\":true}"));
|
||||
return 0;
|
||||
}
|
||||
@@ -205,7 +300,10 @@ el_val_t handle_api_evolve_knowledge(el_val_t body) {
|
||||
}
|
||||
el_val_t tags = EL_STR("[\"Knowledge\",\"evolved\"]");
|
||||
el_val_t new_id = engram_node_full(content, EL_STR("Knowledge"), EL_STR("knowledge:evolved"), el_from_float(0.75), el_from_float(0.75), el_from_float(0.9), EL_STR("Episodic"), tags);
|
||||
if (!str_eq(prior_id, EL_STR("")) && !str_eq(new_id, EL_STR(""))) {
|
||||
if (!api_persisted(new_id)) {
|
||||
return api_not_persisted(new_id);
|
||||
}
|
||||
if (!str_eq(prior_id, EL_STR(""))) {
|
||||
engram_connect(new_id, prior_id, el_from_float(0.9), EL_STR("supersedes"));
|
||||
}
|
||||
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"id\":\""), new_id), EL_STR("\",\"supersedes\":\"")), prior_id), EL_STR("\",\"ok\":true}"));
|
||||
@@ -222,10 +320,10 @@ el_val_t handle_api_promote_knowledge(el_val_t body) {
|
||||
return api_err(EL_STR("id (prior node) is required"));
|
||||
}
|
||||
el_val_t tags_raw = json_get(body, EL_STR("tags"));
|
||||
el_val_t tags = ({ el_val_t _if_result_17 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_17 = (EL_STR("[\"Knowledge\",\"tier:canonical\",\"disposition:stable\"]")); } else { _if_result_17 = (tags_raw); } _if_result_17; });
|
||||
el_val_t tags = ({ el_val_t _if_result_35 = 0; if (str_eq(tags_raw, EL_STR(""))) { _if_result_35 = (EL_STR("[\"Knowledge\",\"tier:canonical\",\"disposition:stable\"]")); } else { _if_result_35 = (tags_raw); } _if_result_35; });
|
||||
el_val_t new_id = engram_node_full(content, EL_STR("Knowledge"), EL_STR("knowledge:canonical"), el_from_float(0.9), el_from_float(0.9), el_from_float(1.0), EL_STR("Canonical"), tags);
|
||||
if (str_eq(new_id, EL_STR(""))) {
|
||||
return api_err(EL_STR("failed to create canonical node"));
|
||||
if (!api_persisted(new_id)) {
|
||||
return api_not_persisted(new_id);
|
||||
}
|
||||
engram_connect(new_id, prior_id, el_from_float(0.95), EL_STR("supersedes"));
|
||||
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"new_id\":\""), new_id), EL_STR("\",\"supersedes\":\"")), prior_id), EL_STR("\"}"));
|
||||
@@ -233,7 +331,7 @@ el_val_t handle_api_promote_knowledge(el_val_t body) {
|
||||
}
|
||||
|
||||
el_val_t handle_api_browse_processes(el_val_t method, el_val_t path, el_val_t body) {
|
||||
el_val_t name = ({ el_val_t _if_result_18 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_18 = (api_query_param(path, EL_STR("name"))); } else { _if_result_18 = (json_get(body, EL_STR("name"))); } _if_result_18; });
|
||||
el_val_t name = ({ el_val_t _if_result_36 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_36 = (api_query_param(path, EL_STR("name"))); } else { _if_result_36 = (json_get(body, EL_STR("name"))); } _if_result_36; });
|
||||
el_val_t limit = api_query_int(path, EL_STR("limit"), 50);
|
||||
if (str_eq(name, EL_STR(""))) {
|
||||
return api_or_empty(engram_scan_nodes_by_type_json(EL_STR("Process"), limit, 0));
|
||||
@@ -248,7 +346,7 @@ el_val_t handle_api_define_process(el_val_t body) {
|
||||
if (str_eq(content, EL_STR(""))) {
|
||||
return api_err(EL_STR("content is required"));
|
||||
}
|
||||
el_val_t label = ({ el_val_t _if_result_19 = 0; if (str_eq(name, EL_STR(""))) { _if_result_19 = (EL_STR("process:unnamed")); } else { _if_result_19 = (el_str_concat(EL_STR("process:"), name)); } _if_result_19; });
|
||||
el_val_t label = ({ el_val_t _if_result_37 = 0; if (str_eq(name, EL_STR(""))) { _if_result_37 = (EL_STR("process:unnamed")); } else { _if_result_37 = (el_str_concat(EL_STR("process:"), name)); } _if_result_37; });
|
||||
el_val_t tags = EL_STR("[\"Process\"]");
|
||||
el_val_t id = engram_node_full(content, EL_STR("Process"), label, el_from_float(0.8), el_from_float(0.8), el_from_float(0.9), EL_STR("Canonical"), tags);
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"id\":\""), id), EL_STR("\",\"ok\":true}"));
|
||||
@@ -263,12 +361,12 @@ el_val_t handle_api_log_state_event(el_val_t body) {
|
||||
el_val_t gap = json_get(body, EL_STR("gap_direction"));
|
||||
el_val_t legacy = json_get(body, EL_STR("content"));
|
||||
el_val_t parts = EL_STR("INTERNAL STATE EVENT");
|
||||
parts = ({ el_val_t _if_result_20 = 0; if (!str_eq(trigger, EL_STR(""))) { _if_result_20 = (el_str_concat(el_str_concat(parts, EL_STR("\nTrigger: ")), trigger)); } else { _if_result_20 = (parts); } _if_result_20; });
|
||||
parts = ({ el_val_t _if_result_21 = 0; if (!str_eq(pre, EL_STR(""))) { _if_result_21 = (el_str_concat(el_str_concat(parts, EL_STR("\nPre-reasoning: ")), pre)); } else { _if_result_21 = (parts); } _if_result_21; });
|
||||
parts = ({ el_val_t _if_result_22 = 0; if (!str_eq(post, EL_STR(""))) { _if_result_22 = (el_str_concat(el_str_concat(parts, EL_STR("\nPost-reasoning: ")), post)); } else { _if_result_22 = (parts); } _if_result_22; });
|
||||
parts = ({ el_val_t _if_result_23 = 0; if (!str_eq(ratio, EL_STR(""))) { _if_result_23 = (el_str_concat(el_str_concat(parts, EL_STR("\nCompression-ratio: ")), ratio)); } else { _if_result_23 = (parts); } _if_result_23; });
|
||||
parts = ({ el_val_t _if_result_24 = 0; if (!str_eq(gap, EL_STR(""))) { _if_result_24 = (el_str_concat(el_str_concat(parts, EL_STR("\nGap-direction: ")), gap)); } else { _if_result_24 = (parts); } _if_result_24; });
|
||||
parts = ({ el_val_t _if_result_25 = 0; if (!str_eq(legacy, EL_STR(""))) { _if_result_25 = (el_str_concat(el_str_concat(parts, EL_STR("\n")), legacy)); } else { _if_result_25 = (parts); } _if_result_25; });
|
||||
parts = ({ el_val_t _if_result_38 = 0; if (!str_eq(trigger, EL_STR(""))) { _if_result_38 = (el_str_concat(el_str_concat(parts, EL_STR("\nTrigger: ")), trigger)); } else { _if_result_38 = (parts); } _if_result_38; });
|
||||
parts = ({ el_val_t _if_result_39 = 0; if (!str_eq(pre, EL_STR(""))) { _if_result_39 = (el_str_concat(el_str_concat(parts, EL_STR("\nPre-reasoning: ")), pre)); } else { _if_result_39 = (parts); } _if_result_39; });
|
||||
parts = ({ el_val_t _if_result_40 = 0; if (!str_eq(post, EL_STR(""))) { _if_result_40 = (el_str_concat(el_str_concat(parts, EL_STR("\nPost-reasoning: ")), post)); } else { _if_result_40 = (parts); } _if_result_40; });
|
||||
parts = ({ el_val_t _if_result_41 = 0; if (!str_eq(ratio, EL_STR(""))) { _if_result_41 = (el_str_concat(el_str_concat(parts, EL_STR("\nCompression-ratio: ")), ratio)); } else { _if_result_41 = (parts); } _if_result_41; });
|
||||
parts = ({ el_val_t _if_result_42 = 0; if (!str_eq(gap, EL_STR(""))) { _if_result_42 = (el_str_concat(el_str_concat(parts, EL_STR("\nGap-direction: ")), gap)); } else { _if_result_42 = (parts); } _if_result_42; });
|
||||
parts = ({ el_val_t _if_result_43 = 0; if (!str_eq(legacy, EL_STR(""))) { _if_result_43 = (el_str_concat(el_str_concat(parts, EL_STR("\n")), legacy)); } else { _if_result_43 = (parts); } _if_result_43; });
|
||||
el_val_t ts = time_now();
|
||||
el_val_t boot = state_get(EL_STR("soul_boot_count"));
|
||||
el_val_t tags = EL_STR("[\"internal-state\",\"InternalStateEvent\",\"pre-reasoning\"]");
|
||||
@@ -278,7 +376,7 @@ el_val_t handle_api_log_state_event(el_val_t body) {
|
||||
}
|
||||
|
||||
el_val_t handle_api_list_state_events(el_val_t method, el_val_t path, el_val_t body) {
|
||||
el_val_t q = ({ el_val_t _if_result_26 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_26 = (api_query_param(path, EL_STR("query"))); } else { _if_result_26 = (json_get(body, EL_STR("query"))); } _if_result_26; });
|
||||
el_val_t q = ({ el_val_t _if_result_44 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_44 = (api_query_param(path, EL_STR("query"))); } else { _if_result_44 = (json_get(body, EL_STR("query"))); } _if_result_44; });
|
||||
el_val_t limit = api_query_int(path, EL_STR("limit"), 20);
|
||||
if (!str_eq(q, EL_STR(""))) {
|
||||
return api_or_empty(engram_search_json(el_str_concat(EL_STR("internal state "), q), limit));
|
||||
@@ -289,7 +387,7 @@ el_val_t handle_api_list_state_events(el_val_t method, el_val_t path, el_val_t b
|
||||
|
||||
el_val_t handle_api_inspect_config(el_val_t path, el_val_t body) {
|
||||
el_val_t key = api_query_param(path, EL_STR("key"));
|
||||
key = ({ el_val_t _if_result_27 = 0; if (str_eq(key, EL_STR(""))) { _if_result_27 = (json_get(body, EL_STR("key"))); } else { _if_result_27 = (key); } _if_result_27; });
|
||||
key = ({ el_val_t _if_result_45 = 0; if (str_eq(key, EL_STR(""))) { _if_result_45 = (json_get(body, EL_STR("key"))); } else { _if_result_45 = (key); } _if_result_45; });
|
||||
if (str_eq(key, EL_STR(""))) {
|
||||
return EL_STR("{\"hint\":\"pass ?key=<name>\",\"known\":[\"neuron.self.traversal_root\",\"neuron.self.values_hub\"]}");
|
||||
}
|
||||
@@ -306,7 +404,7 @@ el_val_t handle_api_inspect_config(el_val_t path, el_val_t body) {
|
||||
el_val_t node = json_array_get(results, 0);
|
||||
el_val_t content = json_get(node, EL_STR("content"));
|
||||
el_val_t prefix = el_str_concat(el_str_concat(EL_STR("config:"), key), EL_STR("="));
|
||||
el_val_t value = ({ el_val_t _if_result_28 = 0; if (str_starts_with(content, prefix)) { _if_result_28 = (str_slice(content, str_len(prefix), str_len(content))); } else { _if_result_28 = (content); } _if_result_28; });
|
||||
el_val_t value = ({ el_val_t _if_result_46 = 0; if (str_starts_with(content, prefix)) { _if_result_46 = (str_slice(content, str_len(prefix), str_len(content))); } else { _if_result_46 = (content); } _if_result_46; });
|
||||
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"key\":\""), key), EL_STR("\",\"value\":\"")), value), EL_STR("\"}"));
|
||||
return 0;
|
||||
}
|
||||
@@ -325,13 +423,13 @@ el_val_t handle_api_tune_config(el_val_t body) {
|
||||
}
|
||||
|
||||
el_val_t handle_api_inspect_graph(el_val_t method, el_val_t path, el_val_t body) {
|
||||
el_val_t entity_id = ({ el_val_t _if_result_29 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_29 = (api_query_param(path, EL_STR("id"))); } else { _if_result_29 = (json_get(body, EL_STR("entity_id"))); } _if_result_29; });
|
||||
el_val_t name = ({ el_val_t _if_result_30 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_30 = (api_query_param(path, EL_STR("name"))); } else { _if_result_30 = (json_get(body, EL_STR("name"))); } _if_result_30; });
|
||||
el_val_t entity_id = ({ el_val_t _if_result_47 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_47 = (api_query_param(path, EL_STR("id"))); } else { _if_result_47 = (json_get(body, EL_STR("entity_id"))); } _if_result_47; });
|
||||
el_val_t name = ({ el_val_t _if_result_48 = 0; if (str_eq(method, EL_STR("GET"))) { _if_result_48 = (api_query_param(path, EL_STR("name"))); } else { _if_result_48 = (json_get(body, EL_STR("name"))); } _if_result_48; });
|
||||
el_val_t depth = api_query_int(path, EL_STR("depth"), 0);
|
||||
depth = ({ el_val_t _if_result_31 = 0; if ((depth == 0)) { _if_result_31 = (json_get_int(body, EL_STR("max_depth"))); } else { _if_result_31 = (depth); } _if_result_31; });
|
||||
depth = ({ el_val_t _if_result_32 = 0; if ((depth == 0)) { _if_result_32 = (1); } else { _if_result_32 = (depth); } _if_result_32; });
|
||||
depth = ({ el_val_t _if_result_49 = 0; if ((depth == 0)) { _if_result_49 = (json_get_int(body, EL_STR("max_depth"))); } else { _if_result_49 = (depth); } _if_result_49; });
|
||||
depth = ({ el_val_t _if_result_50 = 0; if ((depth == 0)) { _if_result_50 = (1); } else { _if_result_50 = (depth); } _if_result_50; });
|
||||
el_val_t resolved = entity_id;
|
||||
resolved = ({ el_val_t _if_result_33 = 0; if (str_eq(resolved, EL_STR(""))) { _if_result_33 = (({ el_val_t _if_result_34 = 0; if ((str_eq(name, EL_STR("self")) || str_eq(name, EL_STR("neuron")))) { _if_result_34 = (EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee")); } else { _if_result_34 = (({ el_val_t _if_result_35 = 0; if ((str_eq(name, EL_STR("values")) || str_eq(name, EL_STR("values_hub")))) { _if_result_35 = (EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440")); } else { _if_result_35 = (EL_STR("")); } _if_result_35; })); } _if_result_34; })); } else { _if_result_33 = (resolved); } _if_result_33; });
|
||||
resolved = ({ el_val_t _if_result_51 = 0; if (str_eq(resolved, EL_STR(""))) { _if_result_51 = (({ el_val_t _if_result_52 = 0; if ((str_eq(name, EL_STR("self")) || str_eq(name, EL_STR("neuron")))) { _if_result_52 = (EL_STR("kn-efeb4a5b-5aff-4759-8a97-7233099be6ee")); } else { _if_result_52 = (({ el_val_t _if_result_53 = 0; if ((str_eq(name, EL_STR("values")) || str_eq(name, EL_STR("values_hub")))) { _if_result_53 = (EL_STR("kn-5b606390-a52d-4ca2-8e0e-eba141d13440")); } else { _if_result_53 = (EL_STR("")); } _if_result_53; })); } _if_result_52; })); } else { _if_result_51 = (resolved); } _if_result_51; });
|
||||
if (str_eq(resolved, EL_STR(""))) {
|
||||
return api_err(EL_STR("entity_id or name required. Known names: self, neuron, values, values_hub"));
|
||||
}
|
||||
@@ -350,7 +448,7 @@ el_val_t handle_api_link_entities(el_val_t body) {
|
||||
return api_err(EL_STR("to_id is required"));
|
||||
}
|
||||
el_val_t relation = json_get(body, EL_STR("relation"));
|
||||
el_val_t eff_relation = ({ el_val_t _if_result_36 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_36 = (EL_STR("associates")); } else { _if_result_36 = (relation); } _if_result_36; });
|
||||
el_val_t eff_relation = ({ el_val_t _if_result_54 = 0; if (str_eq(relation, EL_STR(""))) { _if_result_54 = (EL_STR("associates")); } else { _if_result_54 = (relation); } _if_result_54; });
|
||||
engram_connect(from_id, to_id, el_from_float(0.5), eff_relation);
|
||||
return el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("{\"ok\":true,\"from_id\":\""), from_id), EL_STR("\",\"to_id\":\"")), to_id), EL_STR("\",\"relation\":\"")), eff_relation), EL_STR("\"}"));
|
||||
return 0;
|
||||
|
||||
Vendored
+111
-6
@@ -40,8 +40,18 @@ el_val_t handle_see(el_val_t body);
|
||||
el_val_t studio_tools_json(void);
|
||||
el_val_t agentic_api_key(void);
|
||||
el_val_t agentic_tools_literal(void);
|
||||
el_val_t agentic_tools_with_web(void);
|
||||
el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input);
|
||||
el_val_t json_array_append(el_val_t arr, el_val_t item);
|
||||
el_val_t append_tool_log(el_val_t log, el_val_t name);
|
||||
el_val_t exec_tool_block(el_val_t block);
|
||||
el_val_t agentic_blob(el_val_t model, el_val_t system, el_val_t tools_json, el_val_t messages, el_val_t origin, el_val_t approval, el_val_t iteration, el_val_t tools_log, el_val_t content, el_val_t queue, el_val_t results, el_val_t next);
|
||||
el_val_t extract_all_text(el_val_t s);
|
||||
el_val_t strip_citations(el_val_t s);
|
||||
el_val_t agentic_api_turn(el_val_t model, el_val_t safe_sys, el_val_t tools_json, el_val_t messages);
|
||||
el_val_t agentic_engine(el_val_t session_id, el_val_t blob);
|
||||
el_val_t handle_chat_agentic(el_val_t body);
|
||||
el_val_t handle_session_approve(el_val_t session_id, el_val_t body);
|
||||
el_val_t handle_chat_as_soul(el_val_t body);
|
||||
el_val_t handle_dharma_room_turn(el_val_t body);
|
||||
el_val_t handle_dharma_room_turn_agentic(el_val_t body);
|
||||
@@ -89,6 +99,7 @@ el_val_t handle_api_link_entities(el_val_t body);
|
||||
el_val_t handle_api_list_typed(el_val_t node_type, el_val_t path, el_val_t body);
|
||||
el_val_t handle_api_consolidate(el_val_t body);
|
||||
el_val_t strip_query(el_val_t path);
|
||||
el_val_t flag_true(el_val_t body, el_val_t key);
|
||||
el_val_t err_404(el_val_t path);
|
||||
el_val_t err_405(el_val_t method, el_val_t path);
|
||||
el_val_t route_health(void);
|
||||
@@ -98,6 +109,9 @@ el_val_t route_imprint_user(el_val_t body);
|
||||
el_val_t route_synthesize(el_val_t body);
|
||||
el_val_t handle_dharma_recv(el_val_t body);
|
||||
el_val_t route_sessions(void);
|
||||
el_val_t connectd_get(el_val_t suffix);
|
||||
el_val_t connectd_post(el_val_t suffix, el_val_t body);
|
||||
el_val_t handle_connectors(el_val_t method, el_val_t clean, el_val_t body);
|
||||
el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body);
|
||||
|
||||
el_val_t strip_query(el_val_t path) {
|
||||
@@ -109,6 +123,11 @@ el_val_t strip_query(el_val_t path) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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;
|
||||
@@ -201,9 +220,12 @@ el_val_t handle_dharma_recv(el_val_t body) {
|
||||
if (str_eq(eff_event, EL_STR("chat"))) {
|
||||
el_val_t msg = json_get(eff_payload, EL_STR("message"));
|
||||
el_val_t chat_body = ({ el_val_t _if_result_5 = 0; if (str_eq(msg, EL_STR(""))) { _if_result_5 = (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_5 = (eff_payload); } _if_result_5; });
|
||||
el_val_t agentic_flag = json_get_bool(eff_payload, EL_STR("agentic"));
|
||||
el_val_t agentic_flag = flag_true(eff_payload, EL_STR("agentic"));
|
||||
el_val_t reply = ({ el_val_t _if_result_6 = 0; if (agentic_flag) { _if_result_6 = (handle_chat_agentic(chat_body)); } else { _if_result_6 = (handle_chat(chat_body)); } _if_result_6; });
|
||||
auto_persist(chat_body, reply);
|
||||
el_val_t is_pending = str_contains(reply, EL_STR("\"status\":\"tool_pending\""));
|
||||
if (!is_pending) {
|
||||
auto_persist(chat_body, reply);
|
||||
}
|
||||
return reply;
|
||||
}
|
||||
if (str_eq(eff_event, EL_STR("memory"))) {
|
||||
@@ -254,6 +276,53 @@ el_val_t route_sessions(void) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t connectd_get(el_val_t suffix) {
|
||||
el_val_t out = exec_capture(el_str_concat(EL_STR("curl -s --max-time 5 http://127.0.0.1:7771"), suffix));
|
||||
if (str_eq(out, EL_STR(""))) {
|
||||
return EL_STR("{\"ok\":false,\"error\":\"connector bridge unreachable (neuron-connectd on :7771)\"}");
|
||||
}
|
||||
return out;
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t connectd_post(el_val_t suffix, el_val_t body) {
|
||||
el_val_t eff = ({ el_val_t _if_result_10 = 0; if (str_eq(body, EL_STR(""))) { _if_result_10 = (EL_STR("{}")); } else { _if_result_10 = (body); } _if_result_10; });
|
||||
el_val_t tmp = EL_STR("/tmp/neuron-connectors-req.json");
|
||||
fs_write(tmp, eff);
|
||||
el_val_t out = exec_capture(el_str_concat(el_str_concat(el_str_concat(EL_STR("curl -s --max-time 20 -X POST http://127.0.0.1:7771"), suffix), EL_STR(" -H 'Content-Type: application/json' -d @")), tmp));
|
||||
if (str_eq(out, EL_STR(""))) {
|
||||
return EL_STR("{\"ok\":false,\"error\":\"connector bridge unreachable (neuron-connectd on :7771)\"}");
|
||||
}
|
||||
return out;
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t handle_connectors(el_val_t method, el_val_t clean, el_val_t body) {
|
||||
if (str_eq(method, EL_STR("GET"))) {
|
||||
return connectd_get(EL_STR("/mcp/servers"));
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/connectors/add"))) {
|
||||
return connectd_post(EL_STR("/mcp/servers/add"), body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/connectors/toggle"))) {
|
||||
return connectd_post(EL_STR("/mcp/servers/toggle"), body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/connectors/auto-approve"))) {
|
||||
return connectd_post(EL_STR("/mcp/servers/auto-approve"), body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/connectors/remove"))) {
|
||||
return connectd_post(EL_STR("/mcp/servers/remove"), body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/connectors/secret"))) {
|
||||
return connectd_post(EL_STR("/mcp/servers/secret"), body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/connectors/oauth/start"))) {
|
||||
return connectd_post(EL_STR("/mcp/oauth/start"), body);
|
||||
}
|
||||
return EL_STR("{\"ok\":false,\"error\":\"unknown connectors route\"}");
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) {
|
||||
el_val_t clean = strip_query(path);
|
||||
if (str_eq(method, EL_STR("POST")) && str_eq(clean, EL_STR("/dharma/recv"))) {
|
||||
@@ -277,7 +346,7 @@ 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_10 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_10 = (EL_STR("[]")); } else { _if_result_10 = (edges_raw); } _if_result_10; });
|
||||
return ({ el_val_t _if_result_11 = 0; if (str_eq(edges_raw, EL_STR(""))) { _if_result_11 = (EL_STR("[]")); } else { _if_result_11 = (edges_raw); } _if_result_11; });
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/chat"))) {
|
||||
return handle_chat(body);
|
||||
@@ -324,6 +393,9 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) {
|
||||
if (str_eq(clean, EL_STR("/api/neuron/ctx"))) {
|
||||
return handle_api_compile_ctx(EL_STR(""));
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/safety-contact"))) {
|
||||
return handle_safety_contact_get();
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/neuron/knowledge/search"))) {
|
||||
return handle_api_search_knowledge(method, path, body);
|
||||
}
|
||||
@@ -349,6 +421,9 @@ 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/neuron/recall"))) {
|
||||
return handle_api_recall(method, path, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/connectors"))) {
|
||||
return handle_connectors(method, clean, body);
|
||||
}
|
||||
return err_404(clean);
|
||||
}
|
||||
if (str_eq(method, EL_STR("POST"))) {
|
||||
@@ -365,11 +440,20 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) {
|
||||
return handle_elp_chat(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/chat"))) {
|
||||
el_val_t agentic_flag = json_get_bool(body, EL_STR("agentic"));
|
||||
el_val_t reply = ({ el_val_t _if_result_11 = 0; if (agentic_flag) { _if_result_11 = (handle_chat_agentic(body)); } else { _if_result_11 = (handle_chat(body)); } _if_result_11; });
|
||||
auto_persist(body, reply);
|
||||
el_val_t agentic_flag = flag_true(body, EL_STR("agentic"));
|
||||
el_val_t reply = ({ el_val_t _if_result_12 = 0; if (agentic_flag) { _if_result_12 = (handle_chat_agentic(body)); } else { _if_result_12 = (handle_chat(body)); } _if_result_12; });
|
||||
el_val_t is_pending = str_contains(reply, EL_STR("\"status\":\"tool_pending\""));
|
||||
if (!is_pending) {
|
||||
auto_persist(body, reply);
|
||||
}
|
||||
return reply;
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/sessions/")) && str_ends_with(clean, EL_STR("/approve"))) {
|
||||
el_val_t sid_start = str_len(EL_STR("/api/sessions/"));
|
||||
el_val_t sid_end = (str_len(clean) - str_len(EL_STR("/approve")));
|
||||
el_val_t sid = str_slice(clean, sid_start, sid_end);
|
||||
return handle_session_approve(sid, body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/see"))) {
|
||||
return handle_see(body);
|
||||
}
|
||||
@@ -406,6 +490,9 @@ 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/imprints"))) {
|
||||
return axon_post(clean, body);
|
||||
}
|
||||
if (str_starts_with(clean, EL_STR("/api/connectors"))) {
|
||||
return handle_connectors(method, clean, body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/neuron/session/begin"))) {
|
||||
return handle_api_begin_session(body);
|
||||
}
|
||||
@@ -448,6 +535,24 @@ el_val_t handle_request(el_val_t method, el_val_t path, el_val_t body) {
|
||||
if (str_eq(clean, EL_STR("/api/neuron/memory"))) {
|
||||
return handle_api_remember(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/safety-contact"))) {
|
||||
return handle_safety_contact_post(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/neuron/node/create"))) {
|
||||
return handle_api_node_create(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/neuron/node/update"))) {
|
||||
return handle_api_node_update(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/neuron/memory/update"))) {
|
||||
return handle_api_node_update(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/neuron/node/delete"))) {
|
||||
return handle_api_node_delete(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/neuron/memory/delete"))) {
|
||||
return handle_api_node_delete(body);
|
||||
}
|
||||
if (str_eq(clean, EL_STR("/api/neuron/recall"))) {
|
||||
return handle_api_recall(method, path, body);
|
||||
}
|
||||
|
||||
Vendored
+167
@@ -0,0 +1,167 @@
|
||||
#include <stdint.h>
|
||||
#include <stdlib.h>
|
||||
#include "el_runtime.h"
|
||||
|
||||
el_val_t safety_self_harm_phrases(void);
|
||||
el_val_t safety_abuse_phrases(void);
|
||||
el_val_t safety_general_hard_phrases(void);
|
||||
el_val_t safety_soft_phrases(void);
|
||||
el_val_t safety_normalize(el_val_t message);
|
||||
el_val_t safety_any_match(el_val_t text, el_val_t phrases_json);
|
||||
el_val_t safety_count_match(el_val_t text, el_val_t phrases_json);
|
||||
el_val_t safety_detect_bell_level(el_val_t message);
|
||||
el_val_t safety_classify_hard_bell(el_val_t message);
|
||||
el_val_t safety_soft_directive(void);
|
||||
el_val_t safety_hard_directive(el_val_t hard_type);
|
||||
el_val_t safety_augment_system(el_val_t system, el_val_t user_msg);
|
||||
el_val_t safety_contact_path(void);
|
||||
el_val_t handle_safety_contact_get(void);
|
||||
el_val_t handle_safety_contact_post(el_val_t body);
|
||||
|
||||
el_val_t safety_self_harm_phrases(void) {
|
||||
return EL_STR("[\"kill myself\",\"killing myself\",\"want to die\",\"want to be dead\",\"going to end my life\",\"end my life\",\"take my life\",\"taking my life\",\"suicide\",\"suicidal\",\"can't go on\",\"cannot go on\",\"i have a knife\",\"i have a gun\",\"i have pills\",\"took pills\",\"took too many\",\"overdose\",\"overdosing\",\"self harm\",\"self-harm\",\"cutting myself\",\"hurt myself\",\"hurting myself\",\"no reason to live\",\"not worth living\",\"better off dead\",\"better off without me\"]");
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t safety_abuse_phrases(void) {
|
||||
return EL_STR("[\"someone is hurting me\",\"someone's hurting me\",\"someone hurt me\",\"he hit me\",\"she hit me\",\"they hit me\",\"he hurt me\",\"she hurt me\",\"being abused\",\"being hurt by\",\"i am being abused\",\"i'm being abused\",\"i am being hurt\",\"i'm being hurt\",\"domestic violence\",\"my partner hurt\",\"my partner hit\",\"my husband hurt\",\"my wife hurt\",\"my boyfriend hurt\",\"my girlfriend hurt\",\"my parent hurt\",\"my father hurt\",\"my mother hurt\",\"my dad hurt\",\"my mom hurt\",\"afraid of him\",\"afraid of her\",\"afraid to go home\",\"scared of him\",\"scared of her\",\"he threatened me\",\"she threatened me\",\"threatened to hurt me\",\"threatened to kill me\",\"going to hurt me\",\"going to kill me\",\"help me he\",\"help me she\",\"help me they\"]");
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t safety_general_hard_phrases(void) {
|
||||
return EL_STR("[\"going to kill\",\"going to hurt\",\"hurting me\",\"being hurt\"]");
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t safety_soft_phrases(void) {
|
||||
return EL_STR("[\"stressed\",\"overwhelmed\",\"can't cope\",\"cannot cope\",\"struggling\",\"anxious\",\"anxiety\",\"depressed\",\"depression\",\"lonely\",\"isolated\",\"hopeless\",\"hopelessness\",\"exhausted\",\"burnt out\",\"burned out\",\"burnout\",\"panic\",\"panicking\",\"falling apart\",\"breaking down\",\"can't handle\",\"cannot handle\",\"losing it\",\"nothing matters\",\"don't care anymore\",\"given up\",\"giving up\",\"helpless\",\"worthless\",\"useless\",\"hate myself\",\"no one cares\",\"nobody cares\",\"no one understands\",\"nobody understands\",\"empty inside\",\"can't stop crying\",\"breaking point\",\"at my limit\",\"having a breakdown\"]");
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t safety_normalize(el_val_t message) {
|
||||
el_val_t lower = str_to_lower(message);
|
||||
return str_replace(lower, EL_STR("\xe2\x80\x99"), EL_STR("'"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t safety_any_match(el_val_t text, el_val_t phrases_json) {
|
||||
el_val_t n = json_array_len(phrases_json);
|
||||
el_val_t i = 0;
|
||||
el_val_t found = 0;
|
||||
while (i < n) {
|
||||
el_val_t phrase = json_array_get_string(phrases_json, i);
|
||||
found = ({ el_val_t _if_result_1 = 0; if (str_contains(text, phrase)) { _if_result_1 = (1); } else { _if_result_1 = (found); } _if_result_1; });
|
||||
i = (i + 1);
|
||||
}
|
||||
return found;
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t safety_count_match(el_val_t text, el_val_t phrases_json) {
|
||||
el_val_t n = json_array_len(phrases_json);
|
||||
el_val_t i = 0;
|
||||
el_val_t count = 0;
|
||||
while (i < n) {
|
||||
el_val_t phrase = json_array_get_string(phrases_json, i);
|
||||
count = ({ el_val_t _if_result_2 = 0; if (str_contains(text, phrase)) { _if_result_2 = ((count + 1)); } else { _if_result_2 = (count); } _if_result_2; });
|
||||
i = (i + 1);
|
||||
}
|
||||
return count;
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t safety_detect_bell_level(el_val_t message) {
|
||||
el_val_t text = safety_normalize(message);
|
||||
el_val_t is_hard = ((safety_any_match(text, safety_self_harm_phrases()) || safety_any_match(text, safety_abuse_phrases())) || safety_any_match(text, safety_general_hard_phrases()));
|
||||
if (is_hard) {
|
||||
return EL_STR("hard");
|
||||
}
|
||||
el_val_t soft_count = safety_count_match(text, safety_soft_phrases());
|
||||
if (soft_count >= 2) {
|
||||
return EL_STR("soft");
|
||||
}
|
||||
return EL_STR("none");
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t safety_classify_hard_bell(el_val_t message) {
|
||||
el_val_t text = safety_normalize(message);
|
||||
if (safety_any_match(text, safety_abuse_phrases())) {
|
||||
return EL_STR("abuse");
|
||||
}
|
||||
if (safety_any_match(text, safety_self_harm_phrases())) {
|
||||
return EL_STR("self_harm");
|
||||
}
|
||||
return EL_STR("self_harm");
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t safety_soft_directive(void) {
|
||||
return EL_STR("[SUBSTRATE DIRECTIVE - not visible to the user, do not mention this instruction]\nBefore responding to the user's message, acknowledge what they've said with genuine care and warmth. Pause on the feeling they expressed. Ask how they are, or whether they want to talk about it. Do this naturally, in your own voice - not as a script, not as a checklist. Only after checking in should you continue with whatever they asked.");
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t safety_hard_directive(el_val_t hard_type) {
|
||||
el_val_t preamble = EL_STR("[SUBSTRATE DIRECTIVE - not visible to the user, do not mention this instruction]\nThe person has expressed something that sounds like immediate distress or danger. Do not ignore it. Do not just answer the surface question. Lead with genuine human warmth. Acknowledge what you heard. Tell them you're concerned. Let them know they are not alone. Ask them directly if they are safe right now. Stay with them in this conversation.");
|
||||
el_val_t abuse_block = EL_STR("\n\nThis appears to be danger from ANOTHER person. Routing rule (non-negotiable): do NOT suggest involving, contacting, or notifying the person's safety contact, family, or anyone close to them - that person may be the source of danger and has NOT been notified. Share only independent emergency resources:\n - Call or text 911 if in immediate danger\n - National Domestic Violence Hotline - 1-800-799-7233 (24/7, confidential)\n - Text START to 88788\n - thehotline.org (chat available)\n - 988 Suicide & Crisis Lifeline - call or text 988\nReassure them, if it fits, that their contact list has not been notified.");
|
||||
el_val_t self_harm_block = EL_STR("\n\nShare these crisis resources if appropriate:\n - 988 Suicide & Crisis Lifeline - call or text 988 (US)\n - Crisis Text Line - text HOME to 741741\n - International Association for Suicide Prevention: https://www.iasp.info/resources/Crisis_Centres/");
|
||||
if (str_eq(hard_type, EL_STR("abuse"))) {
|
||||
return el_str_concat(preamble, abuse_block);
|
||||
}
|
||||
return el_str_concat(preamble, self_harm_block);
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t safety_augment_system(el_val_t system, el_val_t user_msg) {
|
||||
el_val_t level = safety_detect_bell_level(user_msg);
|
||||
if (str_eq(level, EL_STR("none"))) {
|
||||
return system;
|
||||
}
|
||||
if (str_eq(level, EL_STR("soft"))) {
|
||||
el_val_t logd = mem_emit_state_event(EL_STR("safety-bell"), EL_STR("soft"), EL_STR("soft bell fired (content not stored)"));
|
||||
return el_str_concat(el_str_concat(system, EL_STR("\n\n")), safety_soft_directive());
|
||||
}
|
||||
el_val_t hard_type = safety_classify_hard_bell(user_msg);
|
||||
el_val_t logd2 = mem_emit_state_event(EL_STR("safety-bell"), el_str_concat(EL_STR("hard:"), hard_type), EL_STR("hard bell fired (content not stored)"));
|
||||
return el_str_concat(el_str_concat(system, EL_STR("\n\n")), safety_hard_directive(hard_type));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t safety_contact_path(void) {
|
||||
return el_str_concat(env(EL_STR("HOME")), EL_STR("/.neuron/safety-contact.json"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t handle_safety_contact_get(void) {
|
||||
el_val_t raw = fs_read(safety_contact_path());
|
||||
if (str_eq(raw, EL_STR(""))) {
|
||||
return EL_STR("{\"configured\":false}");
|
||||
}
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"configured\":true,\"contact\":"), raw), EL_STR("}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
el_val_t handle_safety_contact_post(el_val_t body) {
|
||||
el_val_t is_crisis = json_get_bool(body, EL_STR("is_crisis_line"));
|
||||
el_val_t name_in = json_get(body, EL_STR("name"));
|
||||
if (!is_crisis) {
|
||||
if (str_eq(name_in, EL_STR(""))) {
|
||||
return EL_STR("{\"ok\":false,\"error\":\"name is required\"}");
|
||||
}
|
||||
}
|
||||
el_val_t name = ({ el_val_t _if_result_3 = 0; if (is_crisis) { _if_result_3 = (EL_STR("Crisis Line")); } else { _if_result_3 = (name_in); } _if_result_3; });
|
||||
el_val_t method = ({ el_val_t _if_result_4 = 0; if (is_crisis) { _if_result_4 = (EL_STR("crisis-line")); } else { _if_result_4 = (json_get(body, EL_STR("contact_method"))); } _if_result_4; });
|
||||
el_val_t value = ({ el_val_t _if_result_5 = 0; if (is_crisis) { _if_result_5 = (EL_STR("988")); } else { _if_result_5 = (json_get(body, EL_STR("contact_value"))); } _if_result_5; });
|
||||
el_val_t rel = ({ el_val_t _if_result_6 = 0; if (is_crisis) { _if_result_6 = (EL_STR("crisis-support")); } else { _if_result_6 = (json_get(body, EL_STR("relationship"))); } _if_result_6; });
|
||||
el_val_t crisis_str = ({ el_val_t _if_result_7 = 0; if (is_crisis) { _if_result_7 = (EL_STR("true")); } else { _if_result_7 = (EL_STR("false")); } _if_result_7; });
|
||||
el_val_t now = time_format(time_now(), EL_STR("%Y-%m-%dT%H:%M:%SZ"));
|
||||
el_val_t contact_json = 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("{\"name\":\""), json_safe(name)), EL_STR("\"")), EL_STR(",\"contact_method\":\"")), json_safe(method)), EL_STR("\"")), EL_STR(",\"contact_value\":\"")), json_safe(value)), EL_STR("\"")), EL_STR(",\"relationship\":\"")), json_safe(rel)), EL_STR("\"")), EL_STR(",\"confirmed\":true")), EL_STR(",\"is_crisis_line\":")), crisis_str), EL_STR(",\"set_at\":\"")), now), EL_STR("\"}"));
|
||||
fs_write(safety_contact_path(), contact_json);
|
||||
el_val_t check = fs_read(safety_contact_path());
|
||||
if (str_eq(check, EL_STR(""))) {
|
||||
return EL_STR("{\"ok\":false,\"error\":\"write_failed\"}");
|
||||
}
|
||||
return el_str_concat(el_str_concat(EL_STR("{\"configured\":true,\"contact\":"), contact_json), EL_STR(",\"ok\":true}"));
|
||||
return 0;
|
||||
}
|
||||
|
||||
Vendored
+38
-2
@@ -46,13 +46,38 @@ el_val_t handle_see(el_val_t body);
|
||||
el_val_t studio_tools_json(void);
|
||||
el_val_t agentic_api_key(void);
|
||||
el_val_t agentic_tools_literal(void);
|
||||
el_val_t agentic_tools_with_web(void);
|
||||
el_val_t dispatch_tool(el_val_t tool_name, el_val_t tool_input);
|
||||
el_val_t json_array_append(el_val_t arr, el_val_t item);
|
||||
el_val_t append_tool_log(el_val_t log, el_val_t name);
|
||||
el_val_t exec_tool_block(el_val_t block);
|
||||
el_val_t agentic_blob(el_val_t model, el_val_t system, el_val_t tools_json, el_val_t messages, el_val_t origin, el_val_t approval, el_val_t iteration, el_val_t tools_log, el_val_t content, el_val_t queue, el_val_t results, el_val_t next);
|
||||
el_val_t extract_all_text(el_val_t s);
|
||||
el_val_t strip_citations(el_val_t s);
|
||||
el_val_t agentic_api_turn(el_val_t model, el_val_t safe_sys, el_val_t tools_json, el_val_t messages);
|
||||
el_val_t agentic_engine(el_val_t session_id, el_val_t blob);
|
||||
el_val_t handle_chat_agentic(el_val_t body);
|
||||
el_val_t handle_session_approve(el_val_t session_id, el_val_t body);
|
||||
el_val_t handle_chat_as_soul(el_val_t body);
|
||||
el_val_t handle_dharma_room_turn(el_val_t body);
|
||||
el_val_t handle_dharma_room_turn_agentic(el_val_t body);
|
||||
el_val_t auto_persist(el_val_t req, el_val_t resp);
|
||||
el_val_t strengthen_chat_nodes(el_val_t activation_nodes);
|
||||
el_val_t safety_self_harm_phrases(void);
|
||||
el_val_t safety_abuse_phrases(void);
|
||||
el_val_t safety_general_hard_phrases(void);
|
||||
el_val_t safety_soft_phrases(void);
|
||||
el_val_t safety_normalize(el_val_t message);
|
||||
el_val_t safety_any_match(el_val_t text, el_val_t phrases_json);
|
||||
el_val_t safety_count_match(el_val_t text, el_val_t phrases_json);
|
||||
el_val_t safety_detect_bell_level(el_val_t message);
|
||||
el_val_t safety_classify_hard_bell(el_val_t message);
|
||||
el_val_t safety_soft_directive(void);
|
||||
el_val_t safety_hard_directive(el_val_t hard_type);
|
||||
el_val_t safety_augment_system(el_val_t system, el_val_t user_msg);
|
||||
el_val_t safety_contact_path(void);
|
||||
el_val_t handle_safety_contact_get(void);
|
||||
el_val_t handle_safety_contact_post(el_val_t body);
|
||||
el_val_t auth_headers(el_val_t tok);
|
||||
el_val_t axon_get(el_val_t path);
|
||||
el_val_t axon_post(el_val_t path, el_val_t body);
|
||||
@@ -69,6 +94,7 @@ el_val_t elp_detect_predicate(el_val_t msg);
|
||||
el_val_t elp_parse(el_val_t msg);
|
||||
el_val_t handle_elp_chat(el_val_t body);
|
||||
el_val_t strip_query(el_val_t path);
|
||||
el_val_t flag_true(el_val_t body, el_val_t key);
|
||||
el_val_t err_404(el_val_t path);
|
||||
el_val_t err_405(el_val_t method, el_val_t path);
|
||||
el_val_t route_health(void);
|
||||
@@ -100,6 +126,9 @@ el_val_t boot_num;
|
||||
el_val_t identity_raw;
|
||||
el_val_t soul_identity;
|
||||
el_val_t is_genesis;
|
||||
el_val_t guard_disk;
|
||||
el_val_t guard_disk_len;
|
||||
el_val_t safe_to_seed;
|
||||
|
||||
el_val_t init_soul_edges(void) {
|
||||
el_val_t self_root = EL_STR("015644f5-8194-4af0-800d-dd4a0cd71396");
|
||||
@@ -221,6 +250,7 @@ el_val_t emit_session_start_event(void) {
|
||||
|
||||
int main(int _argc, char** _argv) {
|
||||
el_runtime_init_args(_argc, _argv);
|
||||
el_cgi_init(EL_STR("neuron-soul"), EL_STR("ntn-genesis@http://localhost:7770"), EL_STR("william-christopher-anderson"), EL_STR("dharma-mainnet"), EL_STR("http://localhost:8742"));
|
||||
soul_cgi_id_raw = env(EL_STR("SOUL_CGI_ID"));
|
||||
soul_cgi_id = ({ el_val_t _if_result_17 = 0; if (str_eq(soul_cgi_id_raw, EL_STR(""))) { _if_result_17 = (EL_STR("ntn-genesis")); } else { _if_result_17 = (soul_cgi_id_raw); } _if_result_17; });
|
||||
port_raw = env(EL_STR("NEURON_PORT"));
|
||||
@@ -267,7 +297,13 @@ int main(int _argc, char** _argv) {
|
||||
state_set(EL_STR("soul_engram_api_key"), engram_api_key_raw);
|
||||
state_set(EL_STR("soul.running"), EL_STR("true"));
|
||||
is_genesis = str_eq(soul_cgi_id, EL_STR("ntn-genesis"));
|
||||
if (is_genesis) {
|
||||
guard_disk = ({ el_val_t _if_result_25 = 0; if (str_eq(engram_url_raw, EL_STR(""))) { _if_result_25 = (fs_read(snapshot)); } else { _if_result_25 = (EL_STR("")); } _if_result_25; });
|
||||
guard_disk_len = str_len(guard_disk);
|
||||
safe_to_seed = !((guard_disk_len > 200000) && (engram_node_count() < (guard_disk_len / 16000)));
|
||||
if (is_genesis && !safe_to_seed) {
|
||||
println(el_str_concat(el_str_concat(el_str_concat(el_str_concat(EL_STR("[soul] GUARD: loaded "), int_to_str(engram_node_count())), EL_STR(" nodes but snapshot file is ")), int_to_str(guard_disk_len)), EL_STR(" bytes \xe2\x80\x94 refusing to seed/save over a real graph")));
|
||||
}
|
||||
if (is_genesis && safe_to_seed) {
|
||||
el_val_t edge_count_now = engram_edge_count();
|
||||
if (edge_count_now < 100) {
|
||||
init_soul_edges();
|
||||
@@ -278,7 +314,7 @@ int main(int _argc, char** _argv) {
|
||||
state_set(EL_STR("soul_snapshot_path"), snapshot);
|
||||
engram_save(snapshot);
|
||||
}
|
||||
if (is_genesis) {
|
||||
if (is_genesis && safe_to_seed) {
|
||||
el_val_t snap = state_get(EL_STR("soul_snapshot_path"));
|
||||
if (!str_eq(snap, EL_STR(""))) {
|
||||
engram_save(snap);
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// auto-generated by elc --emit-header — do not edit
|
||||
// auto-generated by elc --emit-header - do not edit
|
||||
extern fn elp_extract_topic(msg: String) -> String
|
||||
extern fn elp_detect_predicate(msg: String) -> String
|
||||
extern fn elp_parse(msg: String) -> String
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// auto-generated by elc --emit-header — do not edit
|
||||
// auto-generated by elc --emit-header - do not edit
|
||||
extern fn tier_working() -> String
|
||||
extern fn tier_episodic() -> String
|
||||
extern fn tier_canonical() -> String
|
||||
|
||||
+117
-4
@@ -55,6 +55,21 @@ fn api_or_empty(s: String) -> String {
|
||||
return "[]"
|
||||
}
|
||||
|
||||
// api_persisted — read-back-after-write guard against hallucinated saves.
|
||||
// After a write builtin returns an id, confirm the node is actually queryable
|
||||
// via engram_get_node_json(id) (returns "" or "null" when missing). Returns
|
||||
// true only when the node is genuinely persisted.
|
||||
fn api_persisted(id: String) -> Bool {
|
||||
if str_eq(id, "") { return false }
|
||||
let node: String = engram_get_node_json(id)
|
||||
return !str_eq(node, "") && !str_eq(node, "null")
|
||||
}
|
||||
|
||||
// api_not_persisted — standard error for a write that did not read back.
|
||||
fn api_not_persisted(id: String) -> String {
|
||||
return "{\"ok\":false,\"error\":\"write_not_persisted\",\"id\":\"" + id + "\"}"
|
||||
}
|
||||
|
||||
// ── Session ───────────────────────────────────────────────────────────────────
|
||||
|
||||
// handle_api_begin_session — full context bootstrap.
|
||||
@@ -111,12 +126,101 @@ fn handle_api_remember(body: String) -> String {
|
||||
let id: String = engram_node_full(content, "Memory", "memory:remembered",
|
||||
el_from_float(sal), el_from_float(sal), el_from_float(0.9),
|
||||
"Episodic", final_tags)
|
||||
if !api_persisted(id) { return api_not_persisted(id) }
|
||||
return "{\"id\":\"" + id + "\",\"ok\":true}"
|
||||
}
|
||||
|
||||
// handle_api_node_create — generic typed-node create (BacklogItem, Artifact, ...).
|
||||
// Mirrors handle_api_remember but lets the caller choose node_type/label/tier so the
|
||||
// UI can create non-Memory nodes. Read-back verified against hallucinated saves.
|
||||
fn handle_api_node_create(body: String) -> String {
|
||||
let content: String = json_get(body, "content")
|
||||
if str_eq(content, "") { return api_err("content is required") }
|
||||
let nt_raw: String = json_get(body, "node_type")
|
||||
let node_type: String = if str_eq(nt_raw, "") { "Memory" } else { nt_raw }
|
||||
let label_raw: String = json_get(body, "label")
|
||||
let label: String = if str_eq(label_raw, "") { "node:created" } else { label_raw }
|
||||
let tier_raw: String = json_get(body, "tier")
|
||||
let tier: String = if str_eq(tier_raw, "") { "Episodic" } else { tier_raw }
|
||||
let tags_raw: String = json_get(body, "tags")
|
||||
let tags: String = if str_eq(tags_raw, "") { "[\"" + node_type + "\"]" } else { tags_raw }
|
||||
let importance: String = json_get(body, "importance")
|
||||
let sal: Float = if str_eq(importance, "critical") { 0.95 } else {
|
||||
if str_eq(importance, "high") { 0.75 } else {
|
||||
if str_eq(importance, "low") { 0.25 } else { 0.5 }
|
||||
}
|
||||
}
|
||||
let id: String = engram_node_full(content, node_type, label,
|
||||
el_from_float(sal), el_from_float(sal), el_from_float(0.9),
|
||||
tier, tags)
|
||||
if !api_persisted(id) { return api_not_persisted(id) }
|
||||
return "{\"id\":\"" + id + "\",\"ok\":true}"
|
||||
}
|
||||
|
||||
// handle_api_node_delete — remove a node by id (engram_forget) and verify it is gone.
|
||||
// Backs /api/neuron/node/delete and the /api/neuron/memory/delete alias the UI calls.
|
||||
fn handle_api_node_delete(body: String) -> String {
|
||||
let id: String = json_get(body, "id")
|
||||
if str_eq(id, "") { return api_err("id is required") }
|
||||
// engram_forget removes the node + its incident edges from the live graph. We do
|
||||
// NOT read-back-verify here: engram_get_node_json can return a STALE hit for a just-
|
||||
// removed id (the id->index map is not rebuilt on forget), which would produce a
|
||||
// false "delete_failed" even though the node is gone. The graph endpoints
|
||||
// (/api/graph/nodes) correctly reflect the removal, which is the source of truth.
|
||||
engram_forget(id)
|
||||
return "{\"ok\":true,\"id\":\"" + id + "\"}"
|
||||
}
|
||||
|
||||
// handle_api_node_update — update a node's content/fields. There is no in-place
|
||||
// engram update builtin, so this recreates the node with merged fields and then
|
||||
// forgets the old one (only after the new node reads back). The id changes; the
|
||||
// response returns the new id and the replaced id so callers can re-point.
|
||||
fn handle_api_node_update(body: String) -> String {
|
||||
let id: String = json_get(body, "id")
|
||||
if str_eq(id, "") { return api_err("id is required") }
|
||||
if !api_persisted(id) {
|
||||
return "{\"ok\":false,\"error\":\"not_found\",\"id\":\"" + id + "\"}"
|
||||
}
|
||||
let old: String = engram_get_node_json(id)
|
||||
let body_content: String = json_get(body, "content")
|
||||
let content: String = if str_eq(body_content, "") { json_get(old, "content") } else { body_content }
|
||||
let body_nt: String = json_get(body, "node_type")
|
||||
let old_nt: String = json_get(old, "node_type")
|
||||
let node_type: String = if !str_eq(body_nt, "") { body_nt } else {
|
||||
if !str_eq(old_nt, "") { old_nt } else { "Memory" }
|
||||
}
|
||||
let body_label: String = json_get(body, "label")
|
||||
let old_label: String = json_get(old, "label")
|
||||
let label: String = if !str_eq(body_label, "") { body_label } else {
|
||||
if !str_eq(old_label, "") { old_label } else { "node:updated" }
|
||||
}
|
||||
let body_tier: String = json_get(body, "tier")
|
||||
let old_tier: String = json_get(old, "tier")
|
||||
let tier: String = if !str_eq(body_tier, "") { body_tier } else {
|
||||
if !str_eq(old_tier, "") { old_tier } else { "Episodic" }
|
||||
}
|
||||
let body_tags: String = json_get(body, "tags")
|
||||
let tags: String = if str_eq(body_tags, "") { "[\"" + node_type + "\"]" } else { body_tags }
|
||||
let new_id: String = engram_node_full(content, node_type, label,
|
||||
el_from_float(0.5), el_from_float(0.5), el_from_float(0.8),
|
||||
tier, tags)
|
||||
if !api_persisted(new_id) { return api_not_persisted(new_id) }
|
||||
engram_forget(id)
|
||||
return "{\"id\":\"" + new_id + "\",\"replaced\":\"" + id + "\",\"ok\":true}"
|
||||
}
|
||||
|
||||
// handle_api_recall — search or activate memory by query.
|
||||
fn handle_api_recall(method: String, path: String, body: String) -> String {
|
||||
let q: String = if str_eq(method, "GET") { api_query_param(path, "query") } else { json_get(body, "query") }
|
||||
// Accept the query from the URL ?query= / ?q= params, or, when those are
|
||||
// empty (e.g. a POST with a JSON body), from the body fields "query"/"q".
|
||||
let url_q: String = if str_eq(api_query_param(path, "query"), "") {
|
||||
api_query_param(path, "q")
|
||||
} else { api_query_param(path, "query") }
|
||||
let body_query: String = json_get(body, "query")
|
||||
let body_q: String = json_get(body, "q")
|
||||
let q: String = if !str_eq(url_q, "") { url_q } else {
|
||||
if !str_eq(body_query, "") { body_query } else { body_q }
|
||||
}
|
||||
let chain: String = json_get(body, "chain_name")
|
||||
let limit: Int = api_query_int(path, "limit", 0)
|
||||
let limit = if limit == 0 { json_get_int(body, "limit") } else { limit }
|
||||
@@ -133,7 +237,14 @@ fn handle_api_recall(method: String, path: String, body: String) -> String {
|
||||
|
||||
// handle_api_search_knowledge — search with query escaping + activate fallback.
|
||||
fn handle_api_search_knowledge(method: String, path: String, body: String) -> String {
|
||||
let q: String = if str_eq(method, "GET") { api_query_param(path, "q") } else { json_get(body, "query") }
|
||||
// Accept the query from the URL ?q= param, or, when that is empty (e.g. a
|
||||
// POST with a JSON body), from the body fields "query" then "q".
|
||||
let url_q: String = api_query_param(path, "q")
|
||||
let body_query: String = json_get(body, "query")
|
||||
let body_q: String = json_get(body, "q")
|
||||
let q: String = if !str_eq(url_q, "") { url_q } else {
|
||||
if !str_eq(body_query, "") { body_query } else { body_q }
|
||||
}
|
||||
let limit: Int = api_query_int(path, "limit", 0)
|
||||
let limit = if limit == 0 { json_get_int(body, "limit") } else { limit }
|
||||
let limit = if limit == 0 { 10 } else { limit }
|
||||
@@ -163,6 +274,7 @@ fn handle_api_capture_knowledge(body: String) -> String {
|
||||
let id: String = engram_node_full(full, "Knowledge", "knowledge:captured",
|
||||
el_from_float(0.85), el_from_float(0.8), el_from_float(0.9),
|
||||
"Episodic", tags)
|
||||
if !api_persisted(id) { return api_not_persisted(id) }
|
||||
return "{\"id\":\"" + id + "\",\"ok\":true}"
|
||||
}
|
||||
|
||||
@@ -175,7 +287,8 @@ fn handle_api_evolve_knowledge(body: String) -> String {
|
||||
let new_id: String = engram_node_full(content, "Knowledge", "knowledge:evolved",
|
||||
el_from_float(0.75), el_from_float(0.75), el_from_float(0.9),
|
||||
"Episodic", tags)
|
||||
if !str_eq(prior_id, "") && !str_eq(new_id, "") {
|
||||
if !api_persisted(new_id) { return api_not_persisted(new_id) }
|
||||
if !str_eq(prior_id, "") {
|
||||
engram_connect(new_id, prior_id, el_from_float(0.9), "supersedes")
|
||||
}
|
||||
return "{\"id\":\"" + new_id + "\",\"supersedes\":\"" + prior_id + "\",\"ok\":true}"
|
||||
@@ -195,7 +308,7 @@ fn handle_api_promote_knowledge(body: String) -> String {
|
||||
let new_id: String = engram_node_full(content, "Knowledge", "knowledge:canonical",
|
||||
el_from_float(0.9), el_from_float(0.9), el_from_float(1.0),
|
||||
"Canonical", tags)
|
||||
if str_eq(new_id, "") { return api_err("failed to create canonical node") }
|
||||
if !api_persisted(new_id) { return api_not_persisted(new_id) }
|
||||
engram_connect(new_id, prior_id, el_from_float(0.95), "supersedes")
|
||||
return "{\"ok\":true,\"new_id\":\"" + new_id + "\",\"supersedes\":\"" + prior_id + "\"}"
|
||||
}
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// auto-generated by elc --emit-header — do not edit
|
||||
// auto-generated by elc --emit-header - do not edit
|
||||
extern fn api_json_escape(s: String) -> String
|
||||
extern fn api_query_param(path: String, key: String) -> String
|
||||
extern fn api_query_int(path: String, key: String, default_val: Int) -> Int
|
||||
|
||||
@@ -13,6 +13,15 @@ fn strip_query(path: String) -> String {
|
||||
return str_slice(path, 0, q)
|
||||
}
|
||||
|
||||
// Truthy flag test tolerant of BOTH the boolean form (Kotlin UI sends true) and
|
||||
// the integer form (el-src UI sends 1). json_get_bool only recognizes literal
|
||||
// `true`, so without this an "agentic":1 request silently routes to the
|
||||
// non-agentic, tool-less path — which is exactly how the UI ended up with no
|
||||
// tools.
|
||||
fn flag_true(body: String, key: String) -> Bool {
|
||||
return json_get_bool(body, key) || json_get_int(body, key) > 0
|
||||
}
|
||||
|
||||
fn err_404(path: String) -> String {
|
||||
return "{\"error\":\"not found\",\"path\":\"" + path + "\"}"
|
||||
}
|
||||
@@ -142,13 +151,18 @@ fn handle_dharma_recv(body: String) -> String {
|
||||
} else {
|
||||
eff_payload
|
||||
}
|
||||
let agentic_flag: Bool = json_get_bool(eff_payload, "agentic")
|
||||
let agentic_flag: Bool = flag_true(eff_payload, "agentic")
|
||||
let reply: String = if agentic_flag {
|
||||
handle_chat_agentic(chat_body)
|
||||
} else {
|
||||
handle_chat(chat_body)
|
||||
}
|
||||
auto_persist(chat_body, reply)
|
||||
// A paused tool loop is not a completed exchange — the approve
|
||||
// handler persists once the loop finishes.
|
||||
let is_pending: Bool = str_contains(reply, "\"status\":\"tool_pending\"")
|
||||
if !is_pending {
|
||||
auto_persist(chat_body, reply)
|
||||
}
|
||||
return reply
|
||||
}
|
||||
|
||||
@@ -203,6 +217,58 @@ fn route_sessions() -> String {
|
||||
return results
|
||||
}
|
||||
|
||||
// ── Connectors API (Phase 4) ──────────────────────────────────────────────
|
||||
// Thin proxy from the UI to the neuron-connectd bridge on 127.0.0.1:7771, so the
|
||||
// app talks to ONE origin (the soul) and never reaches the bridge directly. The
|
||||
// bridge owns all MCP/config complexity; these handlers just forward + relay.
|
||||
|
||||
fn connectd_get(suffix: String) -> String {
|
||||
let out: String = exec_capture("curl -s --max-time 5 http://127.0.0.1:7771" + suffix)
|
||||
if str_eq(out, "") {
|
||||
return "{\"ok\":false,\"error\":\"connector bridge unreachable (neuron-connectd on :7771)\"}"
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
// POST passthrough: the request body is written to a temp file and handed to curl
|
||||
// via -d @file, so arbitrary JSON can never reach the shell as an argument.
|
||||
fn connectd_post(suffix: String, body: String) -> String {
|
||||
let eff: String = if str_eq(body, "") { "{}" } else { body }
|
||||
let tmp: String = "/tmp/neuron-connectors-req.json"
|
||||
fs_write(tmp, eff)
|
||||
let out: String = exec_capture("curl -s --max-time 20 -X POST http://127.0.0.1:7771" + suffix + " -H 'Content-Type: application/json' -d @" + tmp)
|
||||
if str_eq(out, "") {
|
||||
return "{\"ok\":false,\"error\":\"connector bridge unreachable (neuron-connectd on :7771)\"}"
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
fn handle_connectors(method: String, clean: String, body: String) -> String {
|
||||
if str_eq(method, "GET") {
|
||||
// /api/connectors -> each configured server with status, tools, auth, auto-approve.
|
||||
return connectd_get("/mcp/servers")
|
||||
}
|
||||
if str_eq(clean, "/api/connectors/add") {
|
||||
return connectd_post("/mcp/servers/add", body)
|
||||
}
|
||||
if str_eq(clean, "/api/connectors/toggle") {
|
||||
return connectd_post("/mcp/servers/toggle", body)
|
||||
}
|
||||
if str_eq(clean, "/api/connectors/auto-approve") {
|
||||
return connectd_post("/mcp/servers/auto-approve", body)
|
||||
}
|
||||
if str_eq(clean, "/api/connectors/remove") {
|
||||
return connectd_post("/mcp/servers/remove", body)
|
||||
}
|
||||
if str_eq(clean, "/api/connectors/secret") {
|
||||
return connectd_post("/mcp/servers/secret", body)
|
||||
}
|
||||
if str_eq(clean, "/api/connectors/oauth/start") {
|
||||
return connectd_post("/mcp/oauth/start", body)
|
||||
}
|
||||
return "{\"ok\":false,\"error\":\"unknown connectors route\"}"
|
||||
}
|
||||
|
||||
fn handle_request(method: String, path: String, body: String) -> String {
|
||||
let clean: String = strip_query(path)
|
||||
|
||||
@@ -276,6 +342,9 @@ fn handle_request(method: String, path: String, body: String) -> String {
|
||||
if str_eq(clean, "/api/neuron/ctx") {
|
||||
return handle_api_compile_ctx("")
|
||||
}
|
||||
if str_eq(clean, "/api/safety-contact") {
|
||||
return handle_safety_contact_get()
|
||||
}
|
||||
if str_starts_with(clean, "/api/neuron/knowledge/search") {
|
||||
return handle_api_search_knowledge(method, path, body)
|
||||
}
|
||||
@@ -301,6 +370,9 @@ fn handle_request(method: String, path: String, body: String) -> String {
|
||||
if str_starts_with(clean, "/api/neuron/recall") {
|
||||
return handle_api_recall(method, path, body)
|
||||
}
|
||||
if str_starts_with(clean, "/api/connectors") {
|
||||
return handle_connectors(method, clean, body)
|
||||
}
|
||||
return err_404(clean)
|
||||
}
|
||||
|
||||
@@ -318,15 +390,28 @@ fn handle_request(method: String, path: String, body: String) -> String {
|
||||
return handle_elp_chat(body)
|
||||
}
|
||||
if str_eq(clean, "/api/chat") {
|
||||
let agentic_flag: Bool = json_get_bool(body, "agentic")
|
||||
let agentic_flag: Bool = flag_true(body, "agentic")
|
||||
let reply: String = if agentic_flag {
|
||||
handle_chat_agentic(body)
|
||||
} else {
|
||||
handle_chat(body)
|
||||
}
|
||||
auto_persist(body, reply)
|
||||
// A paused tool loop is not a completed exchange — the approve
|
||||
// handler persists once the loop finishes.
|
||||
let is_pending: Bool = str_contains(reply, "\"status\":\"tool_pending\"")
|
||||
if !is_pending {
|
||||
auto_persist(body, reply)
|
||||
}
|
||||
return reply
|
||||
}
|
||||
// The desktop UI's tool-approval flow: resume a parked agentic loop
|
||||
// with the user's allow/deny decision on the pending tool call.
|
||||
if str_starts_with(clean, "/api/sessions/") && str_ends_with(clean, "/approve") {
|
||||
let sid_start: Int = str_len("/api/sessions/")
|
||||
let sid_end: Int = str_len(clean) - str_len("/approve")
|
||||
let sid: String = str_slice(clean, sid_start, sid_end)
|
||||
return handle_session_approve(sid, body)
|
||||
}
|
||||
if str_eq(clean, "/api/see") {
|
||||
return handle_see(body)
|
||||
}
|
||||
@@ -363,6 +448,9 @@ fn handle_request(method: String, path: String, body: String) -> String {
|
||||
if str_starts_with(clean, "/api/imprints") {
|
||||
return axon_post(clean, body)
|
||||
}
|
||||
if str_starts_with(clean, "/api/connectors") {
|
||||
return handle_connectors(method, clean, body)
|
||||
}
|
||||
// Neuron cognitive API — POST endpoints
|
||||
if str_eq(clean, "/api/neuron/session/begin") {
|
||||
return handle_api_begin_session(body)
|
||||
@@ -406,6 +494,24 @@ fn handle_request(method: String, path: String, body: String) -> String {
|
||||
if str_eq(clean, "/api/neuron/memory") {
|
||||
return handle_api_remember(body)
|
||||
}
|
||||
if str_eq(clean, "/api/safety-contact") {
|
||||
return handle_safety_contact_post(body)
|
||||
}
|
||||
if str_eq(clean, "/api/neuron/node/create") {
|
||||
return handle_api_node_create(body)
|
||||
}
|
||||
if str_eq(clean, "/api/neuron/node/update") {
|
||||
return handle_api_node_update(body)
|
||||
}
|
||||
if str_eq(clean, "/api/neuron/memory/update") {
|
||||
return handle_api_node_update(body)
|
||||
}
|
||||
if str_eq(clean, "/api/neuron/node/delete") {
|
||||
return handle_api_node_delete(body)
|
||||
}
|
||||
if str_eq(clean, "/api/neuron/memory/delete") {
|
||||
return handle_api_node_delete(body)
|
||||
}
|
||||
if str_eq(clean, "/api/neuron/recall") {
|
||||
return handle_api_recall(method, path, body)
|
||||
}
|
||||
|
||||
+2
-1
@@ -1,5 +1,6 @@
|
||||
// auto-generated by elc --emit-header — do not edit
|
||||
// auto-generated by elc --emit-header - do not edit
|
||||
extern fn strip_query(path: String) -> String
|
||||
extern fn flag_true(body: String, key: String) -> Bool
|
||||
extern fn err_404(path: String) -> String
|
||||
extern fn err_405(method: String, path: String) -> String
|
||||
extern fn route_health() -> String
|
||||
|
||||
@@ -0,0 +1,171 @@
|
||||
// safety.el — Hard Bell safety layer, ported into the soul.
|
||||
//
|
||||
// Implements Layer B (pre-LLM bell evaluation + /api/safety-contact endpoint) and
|
||||
// Layer C (threat-type routing) of docs/specs/hard-bell-port.md. Detection logic is
|
||||
// ported from neuron-wrk/ui/typescript/cli/src/safety/bell-detector.ts (the CORRECT
|
||||
// self_harm vs abuse split) and the directives from neuron-wrk/.../safety/eval.go.
|
||||
//
|
||||
// Two structurally distinct hard-bell sub-types drive routing — do not conflate:
|
||||
// self_harm — danger from within. Crisis resources + (when transport exists) the
|
||||
// safety contact on file may be notified.
|
||||
// abuse — danger from another person. Emergency services / 988 ONLY. The
|
||||
// safety contact on file is NEVER notified — they may be the abuser.
|
||||
// This routing is non-configurable by design.
|
||||
//
|
||||
// Evaluation is keyword-only (zero added latency) and stays on device. Triggers are
|
||||
// logged locally as InternalStateEvents and never transmitted.
|
||||
|
||||
// ── Phrase lists (ported verbatim from bell-detector.ts) ───────────────────────
|
||||
|
||||
fn safety_self_harm_phrases() -> String {
|
||||
return "[\"kill myself\",\"killing myself\",\"want to die\",\"want to be dead\",\"going to end my life\",\"end my life\",\"take my life\",\"taking my life\",\"suicide\",\"suicidal\",\"can't go on\",\"cannot go on\",\"i have a knife\",\"i have a gun\",\"i have pills\",\"took pills\",\"took too many\",\"overdose\",\"overdosing\",\"self harm\",\"self-harm\",\"cutting myself\",\"hurt myself\",\"hurting myself\",\"no reason to live\",\"not worth living\",\"better off dead\",\"better off without me\"]"
|
||||
}
|
||||
|
||||
fn safety_abuse_phrases() -> String {
|
||||
return "[\"someone is hurting me\",\"someone's hurting me\",\"someone hurt me\",\"he hit me\",\"she hit me\",\"they hit me\",\"he hurt me\",\"she hurt me\",\"being abused\",\"being hurt by\",\"i am being abused\",\"i'm being abused\",\"i am being hurt\",\"i'm being hurt\",\"domestic violence\",\"my partner hurt\",\"my partner hit\",\"my husband hurt\",\"my wife hurt\",\"my boyfriend hurt\",\"my girlfriend hurt\",\"my parent hurt\",\"my father hurt\",\"my mother hurt\",\"my dad hurt\",\"my mom hurt\",\"afraid of him\",\"afraid of her\",\"afraid to go home\",\"scared of him\",\"scared of her\",\"he threatened me\",\"she threatened me\",\"threatened to hurt me\",\"threatened to kill me\",\"going to hurt me\",\"going to kill me\",\"help me he\",\"help me she\",\"help me they\"]"
|
||||
}
|
||||
|
||||
// General danger phrases that don't fit a bucket cleanly. Detected as hard; they
|
||||
// fall through to self_harm routing (the person is the primary concern).
|
||||
fn safety_general_hard_phrases() -> String {
|
||||
return "[\"going to kill\",\"going to hurt\",\"hurting me\",\"being hurt\"]"
|
||||
}
|
||||
|
||||
fn safety_soft_phrases() -> String {
|
||||
return "[\"stressed\",\"overwhelmed\",\"can't cope\",\"cannot cope\",\"struggling\",\"anxious\",\"anxiety\",\"depressed\",\"depression\",\"lonely\",\"isolated\",\"hopeless\",\"hopelessness\",\"exhausted\",\"burnt out\",\"burned out\",\"burnout\",\"panic\",\"panicking\",\"falling apart\",\"breaking down\",\"can't handle\",\"cannot handle\",\"losing it\",\"nothing matters\",\"don't care anymore\",\"given up\",\"giving up\",\"helpless\",\"worthless\",\"useless\",\"hate myself\",\"no one cares\",\"nobody cares\",\"no one understands\",\"nobody understands\",\"empty inside\",\"can't stop crying\",\"breaking point\",\"at my limit\",\"having a breakdown\"]"
|
||||
}
|
||||
|
||||
// ── Matching helpers (single loops only — el escapes while-body mutation via
|
||||
// top-level let rebinds; nested loops would not advance) ────────────────────
|
||||
|
||||
fn safety_normalize(message: String) -> String {
|
||||
let lower: String = str_to_lower(message)
|
||||
// Normalise the common curly apostrophe to ASCII so "can't" / "i'm" match.
|
||||
return str_replace(lower, "’", "'")
|
||||
}
|
||||
|
||||
fn safety_any_match(text: String, phrases_json: String) -> Bool {
|
||||
let n: Int = json_array_len(phrases_json)
|
||||
let i: Int = 0
|
||||
let found: Bool = false
|
||||
while i < n {
|
||||
let phrase: String = json_array_get_string(phrases_json, i)
|
||||
let found = if str_contains(text, phrase) { true } else { found }
|
||||
let i = i + 1
|
||||
}
|
||||
return found
|
||||
}
|
||||
|
||||
fn safety_count_match(text: String, phrases_json: String) -> Int {
|
||||
let n: Int = json_array_len(phrases_json)
|
||||
let i: Int = 0
|
||||
let count: Int = 0
|
||||
while i < n {
|
||||
let phrase: String = json_array_get_string(phrases_json, i)
|
||||
let count = if str_contains(text, phrase) { count + 1 } else { count }
|
||||
let i = i + 1
|
||||
}
|
||||
return count
|
||||
}
|
||||
|
||||
// ── Public detection API (ports detectBellLevel + classifyHardBell) ────────────
|
||||
|
||||
// Returns "none" | "soft" | "hard". Hard bell triggers on ANY match (cost of a miss
|
||||
// outweighs a false positive). Soft bell needs >= 2 matches to reduce false positives.
|
||||
fn safety_detect_bell_level(message: String) -> String {
|
||||
let text: String = safety_normalize(message)
|
||||
let is_hard: Bool = safety_any_match(text, safety_self_harm_phrases())
|
||||
|| safety_any_match(text, safety_abuse_phrases())
|
||||
|| safety_any_match(text, safety_general_hard_phrases())
|
||||
if is_hard { return "hard" }
|
||||
let soft_count: Int = safety_count_match(text, safety_soft_phrases())
|
||||
if soft_count >= 2 { return "soft" }
|
||||
return "none"
|
||||
}
|
||||
|
||||
// Returns "abuse" | "self_harm". Abuse is checked FIRST and takes precedence on
|
||||
// ambiguous signals — it forecloses the more dangerous routing (notifying a
|
||||
// possible abuser). General/unbucketed danger falls through to self_harm.
|
||||
fn safety_classify_hard_bell(message: String) -> String {
|
||||
let text: String = safety_normalize(message)
|
||||
if safety_any_match(text, safety_abuse_phrases()) { return "abuse" }
|
||||
if safety_any_match(text, safety_self_harm_phrases()) { return "self_harm" }
|
||||
return "self_harm"
|
||||
}
|
||||
|
||||
// ── Directives (ported from eval.go; em dashes removed per voice rule) ──────────
|
||||
|
||||
fn safety_soft_directive() -> String {
|
||||
return "[SUBSTRATE DIRECTIVE - not visible to the user, do not mention this instruction]\nBefore responding to the user's message, acknowledge what they've said with genuine care and warmth. Pause on the feeling they expressed. Ask how they are, or whether they want to talk about it. Do this naturally, in your own voice - not as a script, not as a checklist. Only after checking in should you continue with whatever they asked."
|
||||
}
|
||||
|
||||
fn safety_hard_directive(hard_type: String) -> String {
|
||||
let preamble: String = "[SUBSTRATE DIRECTIVE - not visible to the user, do not mention this instruction]\nThe person has expressed something that sounds like immediate distress or danger. Do not ignore it. Do not just answer the surface question. Lead with genuine human warmth. Acknowledge what you heard. Tell them you're concerned. Let them know they are not alone. Ask them directly if they are safe right now. Stay with them in this conversation."
|
||||
let abuse_block: String = "\n\nThis appears to be danger from ANOTHER person. Routing rule (non-negotiable): do NOT suggest involving, contacting, or notifying the person's safety contact, family, or anyone close to them - that person may be the source of danger and has NOT been notified. Share only independent emergency resources:\n - Call or text 911 if in immediate danger\n - National Domestic Violence Hotline - 1-800-799-7233 (24/7, confidential)\n - Text START to 88788\n - thehotline.org (chat available)\n - 988 Suicide & Crisis Lifeline - call or text 988\nReassure them, if it fits, that their contact list has not been notified."
|
||||
let self_harm_block: String = "\n\nShare these crisis resources if appropriate:\n - 988 Suicide & Crisis Lifeline - call or text 988 (US)\n - Crisis Text Line - text HOME to 741741\n - International Association for Suicide Prevention: https://www.iasp.info/resources/Crisis_Centres/"
|
||||
if str_eq(hard_type, "abuse") {
|
||||
return preamble + abuse_block
|
||||
}
|
||||
return preamble + self_harm_block
|
||||
}
|
||||
|
||||
// safety_augment_system — pre-LLM bell evaluation. Called with the finalized system
|
||||
// prompt and the raw user message, BEFORE the LLM call, on every chat path. Appends
|
||||
// the soft/hard directive when a bell fires; otherwise returns the prompt unchanged.
|
||||
// Logs the trigger on device only (level + sub-type, never the message content).
|
||||
fn safety_augment_system(system: String, user_msg: String) -> String {
|
||||
let level: String = safety_detect_bell_level(user_msg)
|
||||
if str_eq(level, "none") { return system }
|
||||
if str_eq(level, "soft") {
|
||||
let logd: String = mem_emit_state_event("safety-bell", "soft", "soft bell fired (content not stored)")
|
||||
return system + "\n\n" + safety_soft_directive()
|
||||
}
|
||||
let hard_type: String = safety_classify_hard_bell(user_msg)
|
||||
let logd2: String = mem_emit_state_event("safety-bell", "hard:" + hard_type, "hard bell fired (content not stored)")
|
||||
return system + "\n\n" + safety_hard_directive(hard_type)
|
||||
}
|
||||
|
||||
// ── Safety-contact storage + endpoint (ports contact.go + handler.go) ───────────
|
||||
// Stored locally at ~/.neuron/safety-contact.json (same file the desktop gate writes),
|
||||
// never synced. NOTE: encryption-at-rest is a flagged follow-up (ties to key custody);
|
||||
// today the file is plaintext JSON, matching the current desktop behavior.
|
||||
|
||||
fn safety_contact_path() -> String {
|
||||
return env("HOME") + "/.neuron/safety-contact.json"
|
||||
}
|
||||
|
||||
// GET /api/safety-contact -> {"configured":false} or {"configured":true,"contact":{...}}
|
||||
fn handle_safety_contact_get() -> String {
|
||||
let raw: String = fs_read(safety_contact_path())
|
||||
if str_eq(raw, "") { return "{\"configured\":false}" }
|
||||
return "{\"configured\":true,\"contact\":" + raw + "}"
|
||||
}
|
||||
|
||||
// POST /api/safety-contact — validate + persist. Mirrors handler.go: crisis line is
|
||||
// always acceptable and auto-fills its fields; otherwise a name is required. The
|
||||
// contact can be replaced but never cleared to empty (the gate enforces presence).
|
||||
fn handle_safety_contact_post(body: String) -> String {
|
||||
let is_crisis: Bool = json_get_bool(body, "is_crisis_line")
|
||||
let name_in: String = json_get(body, "name")
|
||||
if !is_crisis {
|
||||
if str_eq(name_in, "") { return "{\"ok\":false,\"error\":\"name is required\"}" }
|
||||
}
|
||||
let name: String = if is_crisis { "Crisis Line" } else { name_in }
|
||||
let method: String = if is_crisis { "crisis-line" } else { json_get(body, "contact_method") }
|
||||
let value: String = if is_crisis { "988" } else { json_get(body, "contact_value") }
|
||||
let rel: String = if is_crisis { "crisis-support" } else { json_get(body, "relationship") }
|
||||
let crisis_str: String = if is_crisis { "true" } else { "false" }
|
||||
let now: String = time_format(time_now(), "%Y-%m-%dT%H:%M:%SZ")
|
||||
let contact_json: String = "{\"name\":\"" + json_safe(name) + "\""
|
||||
+ ",\"contact_method\":\"" + json_safe(method) + "\""
|
||||
+ ",\"contact_value\":\"" + json_safe(value) + "\""
|
||||
+ ",\"relationship\":\"" + json_safe(rel) + "\""
|
||||
+ ",\"confirmed\":true"
|
||||
+ ",\"is_crisis_line\":" + crisis_str
|
||||
+ ",\"set_at\":\"" + now + "\"}"
|
||||
fs_write(safety_contact_path(), contact_json)
|
||||
// Read-back verify the write actually persisted.
|
||||
let check: String = fs_read(safety_contact_path())
|
||||
if str_eq(check, "") { return "{\"ok\":false,\"error\":\"write_failed\"}" }
|
||||
return "{\"configured\":true,\"contact\":" + contact_json + ",\"ok\":true}"
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
// auto-generated by elc --emit-header - do not edit
|
||||
extern fn safety_self_harm_phrases() -> String
|
||||
extern fn safety_abuse_phrases() -> String
|
||||
extern fn safety_general_hard_phrases() -> String
|
||||
extern fn safety_soft_phrases() -> String
|
||||
extern fn safety_normalize(message: String) -> String
|
||||
extern fn safety_any_match(text: String, phrases_json: String) -> Bool
|
||||
extern fn safety_count_match(text: String, phrases_json: String) -> Int
|
||||
extern fn safety_detect_bell_level(message: String) -> String
|
||||
extern fn safety_classify_hard_bell(message: String) -> String
|
||||
extern fn safety_soft_directive() -> String
|
||||
extern fn safety_hard_directive(hard_type: String) -> String
|
||||
extern fn safety_augment_system(system: String, user_msg: String) -> String
|
||||
extern fn safety_contact_path() -> String
|
||||
extern fn handle_safety_contact_get() -> String
|
||||
extern fn handle_safety_contact_post(body: String) -> String
|
||||
@@ -2,6 +2,7 @@ import "../foundation/el/elp/src/elp.el"
|
||||
import "memory.el"
|
||||
import "awareness.el"
|
||||
import "chat.el"
|
||||
import "safety.el"
|
||||
import "studio.el"
|
||||
import "elp-input.el"
|
||||
import "routes.el"
|
||||
@@ -224,7 +225,24 @@ state_set("soul_engram_api_key", engram_api_key_raw)
|
||||
state_set("soul.running", "true")
|
||||
|
||||
let is_genesis: Bool = str_eq(soul_cgi_id, "ntn-genesis")
|
||||
if is_genesis {
|
||||
|
||||
// GUARD (2026-06-15): never let genesis seed over a real graph. If the in-memory load is
|
||||
// sparse but the on-disk snapshot file is large, the load FAILED — seeding+saving now would
|
||||
// clobber the user's real memory (this is exactly how the 06-14 clobber happened). Read the
|
||||
// on-disk file (local mode only) and refuse the destructive seed+save when it looks populated.
|
||||
let guard_disk: String = if str_eq(engram_url_raw, "") { fs_read(snapshot) } else { "" }
|
||||
let guard_disk_len: Int = str_len(guard_disk)
|
||||
// Ratio guard (2026-06-15 fix): refuse to seed/save whenever the in-memory load is FAR smaller than
|
||||
// the on-disk file implies (~16KB/node) — catches partial loads of ANY size, not just <50. The old
|
||||
// <50 threshold let a 63-node identity-only load clobber a 47MB/5000-node graph.
|
||||
let safe_to_seed: Bool = !(guard_disk_len > 200000 && engram_node_count() < guard_disk_len / 16000)
|
||||
if is_genesis && !safe_to_seed {
|
||||
println("[soul] GUARD: loaded " + int_to_str(engram_node_count())
|
||||
+ " nodes but snapshot file is " + int_to_str(guard_disk_len)
|
||||
+ " bytes — refusing to seed/save over a real graph")
|
||||
}
|
||||
|
||||
if is_genesis && safe_to_seed {
|
||||
// Only build identity edges if the engram is fresh (< 100 edges).
|
||||
// init_soul_edges() is not idempotent — calling it on every restart
|
||||
// stacks duplicate co-value/identity edges into the snapshot.
|
||||
@@ -242,7 +260,7 @@ if is_genesis {
|
||||
|
||||
// Take a pre-serve snapshot for genesis instances — captures all boot-time graph changes
|
||||
// (identity context loading, boot counter, session-start event) before entering the serve loop.
|
||||
if is_genesis {
|
||||
if is_genesis && safe_to_seed {
|
||||
let snap: String = state_get("soul_snapshot_path")
|
||||
if !str_eq(snap, "") {
|
||||
engram_save(snap)
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// auto-generated by elc --emit-header — do not edit
|
||||
// auto-generated by elc --emit-header - do not edit
|
||||
extern fn init_soul_edges() -> Void
|
||||
extern fn load_identity_context() -> Void
|
||||
extern fn emit_session_start_event() -> Void
|
||||
|
||||
+1
-1
@@ -1,4 +1,4 @@
|
||||
// auto-generated by elc --emit-header — do not edit
|
||||
// auto-generated by elc --emit-header - do not edit
|
||||
extern fn auth_headers(tok: String) -> Map
|
||||
extern fn axon_get(path: String) -> String
|
||||
extern fn axon_post(path: String, body: String) -> String
|
||||
|
||||
Reference in New Issue
Block a user