fix(chat): add distill_transcript (was called but never defined)

handle_dharma_room_turn and handle_dharma_chat both called
distill_transcript since June 30 but the function was never declared,
causing a build failure. Implements last-3-messages extraction for JSON
array transcripts and last-500-char truncation for plain text.
This commit is contained in:
2026-07-01 11:25:48 -05:00
parent 9d266aac4c
commit 31dd93d5f4
2 changed files with 159 additions and 121 deletions
+38
View File
@@ -594,6 +594,44 @@ fn engram_compile(intent: String) -> String {
if str_starts_with(ctx, "[") { return truncated + "]" }
return truncated
}
// distill_transcript extract the salient tail from a full conversation transcript.
//
// Purpose: before activating working memory on a transcript, reduce it to the
// last N turns. Activating on the ENTIRE transcript (which may contain hundreds
// of messages) would produce noisy, over-broad seed finding too many nodes match
// too many words, collapse the WM to breakthrough-floor nodes. Taking only the tail
// focuses activation on what's contextually live right now.
//
// Handles two transcript formats:
// JSON array: [{"role":"human","content":"..."},...] extract last 3 messages' content
// Plain text: raw string return last 500 chars
//
// Returns a string of at most 500 chars suitable for engram_compile/engram_activate.
// (Added 2026-07-01 self-review: was called in handle_dharma_room_turn and
// handle_dharma_chat but never defined caused build failure since June 30.)
fn distill_transcript(transcript: String) -> String {
if str_eq(transcript, "") { return "" }
// JSON array format: extract last 3 messages' content fields
if str_starts_with(transcript, "[") {
let n: Int = json_array_len(transcript)
if n == 0 { return "" }
let m0: String = json_array_get(transcript, n - 1)
let m1: String = if n > 1 { json_array_get(transcript, n - 2) } else { "" }
let m2: String = if n > 2 { json_array_get(transcript, n - 3) } else { "" }
let c0: String = json_get(m0, "content")
let c1: String = json_get(m1, "content")
let c2: String = json_get(m2, "content")
let combined: String = c2 + " " + c1 + " " + c0
let len: Int = str_len(combined)
if len > 500 { return str_slice(combined, len - 500, len) }
return combined
}
// Plain text: return last 500 chars
let len: Int = str_len(transcript)
if len > 500 { return str_slice(transcript, len - 500, len) }
return transcript
}
fn json_safe(s: String) -> String {
let s1: String = str_replace(s, "\\", "\\\\")
let s2: String = str_replace(s1, "\"", "\\\"")
Generated Vendored
+121 -121
View File
File diff suppressed because one or more lines are too long