use crate::partitioner::ChatMessage;
use crate::pruners::purge_reasoning_artifacts;
use serde_json::Value;
pub fn count_line_braces(line: &str) -> (usize, usize, isize) {
let mut open = 0usize;
let mut close = 0usize;
let mut in_single = false;
let mut in_double = false;
let mut in_template = false;
let bytes = line.as_bytes();
let mut i = 0usize;
while i < bytes.len() {
let ch = bytes[i];
let prev = if i > 0 { bytes[i - 1] } else { 0 };
if prev == b'\\' {
i += 1;
continue;
}
if !in_double && !in_template && ch == b'\'' {
in_single = !in_single;
} else if !in_single && !in_double && ch == b'`' {
in_template = !in_template;
} else if !in_single && !in_template && ch == b'"' {
in_double = !in_double;
} else if !in_single && !in_double && !in_template {
if ch == b'/' && bytes.get(i + 1) == Some(&b'/') {
break;
}
if ch == b'{' {
open += 1;
} else if ch == b'}' {
close += 1;
}
}
i += 1;
}
(open, close, (open as isize) - (close as isize))
}
pub fn compact_code_skeleton(raw_code: &str, max_lines: usize) -> String {
let trimmed = raw_code.trim();
if trimmed.is_empty() || trimmed.starts_with("// [AST CODE SKELETON") {
return trimmed.to_string();
}
#[derive(PartialEq, Eq)]
enum State {
Top,
Interface,
Class,
SkipClassMethod,
SkipTopBody,
}
let mut state = State::Top;
let mut in_multi_comment = false;
let mut brace_depth: isize = 0;
let mut import_count = 0usize;
let mut out_lines = Vec::new();
out_lines.push("// [AST CODE SKELETON: Signatures & Contracts Preserved]".to_string());
for raw_line in trimmed.lines() {
let line = raw_line.trim();
if line.starts_with("/*") {
in_multi_comment = true;
}
if in_multi_comment {
if line.contains("*/") {
in_multi_comment = false;
}
continue;
}
if line.is_empty() || line.starts_with("//") || line.starts_with('#') {
continue;
}
let (open, close, net) = count_line_braces(line);
match state {
State::SkipTopBody => {
brace_depth = (brace_depth + net).max(0);
if brace_depth == 0 {
state = State::Top;
}
continue;
}
State::SkipClassMethod => {
brace_depth = (brace_depth + net).max(0);
if brace_depth <= 1 {
if brace_depth == 0 {
out_lines.push("}".to_string());
state = State::Top;
} else {
state = State::Class;
}
}
continue;
}
State::Interface => {
out_lines.push(raw_line.to_string());
brace_depth = (brace_depth + net).max(0);
if brace_depth == 0 {
state = State::Top;
}
}
State::Class => {
let next_depth = (brace_depth + net).max(0);
if next_depth == 0 && close > 0 && open == 0 {
out_lines.push("}".to_string());
brace_depth = 0;
state = State::Top;
continue;
}
let is_control = line.starts_with("if ")
|| line.starts_with("if(")
|| line.starts_with("for ")
|| line.starts_with("while ")
|| line.starts_with("switch ")
|| line.starts_with("return ");
if !is_control && line.contains('(') {
let sig = raw_line.trim_end().trim_end_matches('{').trim_end();
out_lines.push(format!("{};", sig));
if net > 0 {
brace_depth = next_depth;
state = State::SkipClassMethod;
}
} else if net > 0 {
brace_depth = next_depth;
state = State::SkipClassMethod;
} else {
brace_depth = next_depth;
}
}
State::Top => {
if line.starts_with("import ") || line.starts_with("from ") || line.starts_with("use ") {
if import_count < 8 {
out_lines.push(raw_line.to_string());
import_count += 1;
}
continue;
}
let is_type_decl = line.starts_with("export interface ")
|| line.starts_with("interface ")
|| line.starts_with("export type ")
|| line.starts_with("type ")
|| line.starts_with("export enum ")
|| line.starts_with("enum ")
|| line.starts_with("pub struct ")
|| line.starts_with("struct ");
if is_type_decl {
out_lines.push(raw_line.to_string());
if net > 0 {
brace_depth += net;
state = State::Interface;
}
continue;
}
let is_class_decl = line.starts_with("export class ")
|| line.starts_with("class ")
|| line.starts_with("impl ");
if is_class_decl {
out_lines.push(raw_line.to_string());
if net > 0 {
brace_depth += net;
state = State::Class;
}
continue;
}
let is_fn_decl = line.starts_with("export function ")
|| line.starts_with("export async function ")
|| line.starts_with("function ")
|| line.starts_with("async function ")
|| line.starts_with("pub fn ")
|| line.starts_with("fn ")
|| line.starts_with("def ")
|| line.starts_with("async def ")
|| ((line.starts_with("export const ") || line.starts_with("const "))
&& line.contains("=>"));
if is_fn_decl {
let sig = raw_line.trim_end().trim_end_matches('{').trim_end();
out_lines.push(format!("{};", sig));
if net > 0 {
brace_depth += net;
state = State::SkipTopBody;
}
} else if net > 0 {
brace_depth += net;
state = State::SkipTopBody;
}
}
}
if out_lines.len() >= max_lines {
out_lines.push("// ... [remaining implementation bodies elided]".to_string());
break;
}
}
out_lines.join("\n")
}
fn extract_tag_inner<'a>(html: &'a str, tag: &str) -> Option<&'a str> {
let open_pat = format!("<{}", tag);
let close_pat = format!("</{}>", tag);
let lower = html.to_ascii_lowercase();
let start = lower.find(&open_pat)?;
let gt = lower[start..].find('>')? + start + 1;
let end = lower[gt..].find(&close_pat)? + gt;
Some(html[gt..end].trim())
}
fn push_unique(vec: &mut Vec<String>, item: String, cap: usize) {
if !item.is_empty() && !vec.contains(&item) && vec.len() < cap {
vec.push(item);
}
}
pub fn distill_app_graph_blueprint(raw_html: &str, fallback_title: &str) -> String {
let text = raw_html.trim();
if text.len() <= 360 || text.contains("[RELATIONAL ARTIFACT BLUEPRINT:") {
return text.to_string();
}
let title = extract_tag_inner(text, "title")
.or_else(|| extract_tag_inner(text, "h1"))
.or_else(|| extract_tag_inner(text, "h2"))
.unwrap_or(fallback_title)
.replace(['\n', '\r', '"'], "");
let mut stack = Vec::new();
if text.contains("tailwindcss") {
stack.push("Tailwind CSS");
}
if text.contains("THREE.") || text.contains("three.js") {
stack.push("Three.js 3D WebGL");
} else if text.contains("<canvas") || text.contains("getContext(") {
stack.push("HTML5 Canvas 2D");
}
if text.contains("<svg") {
stack.push("Inline SVG");
}
if text.contains("localStorage") {
stack.push("LocalStorage State");
}
let stack_str = if stack.is_empty() {
"Vanilla JavaScript & CSS".to_string()
} else {
stack.join(" + ")
};
let mut dom_ids = Vec::new();
for pat in ["id=\"", "id='", "getElementById(\"", "getElementById('", "querySelector(\"#", "querySelector('#"] {
let mut cur = 0usize;
while let Some(pos) = text[cur..].find(pat) {
let start = cur + pos + pat.len();
let bytes = text.as_bytes();
let mut end = start;
while end < bytes.len() {
let b = bytes[end];
if b.is_ascii_alphanumeric() || b == b'_' || b == b'-' {
end += 1;
} else {
break;
}
}
if end > start {
push_unique(&mut dom_ids, format!("#{}", &text[start..end]), 12);
}
cur = end;
}
}
let dom_str = if dom_ids.is_empty() {
"#app (root container)".to_string()
} else {
dom_ids.join(" | ")
};
let mut fn_sigs = Vec::new();
let mut fn_names = Vec::new();
let mut state_vars = Vec::new();
for stmt in text.split(['\n', ';', '>', '<', '}']) {
let t = stmt.trim();
for kw in ["let ", "var ", "const "] {
if let Some(rest) = t.strip_prefix(kw) {
if let Some(eq_idx) = rest.find('=') {
let var_name = rest[..eq_idx]
.split(':')
.next()
.unwrap_or("")
.trim();
let rhs = rest[eq_idx + 1..].trim();
let valid_ident = !var_name.is_empty()
&& var_name
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'$')
&& !matches!(var_name, "i" | "j" | "k" | "e" | "evt" | "el" | "ctx" | "canvas");
if valid_ident {
if rhs.starts_with('(') || rhs.starts_with("async") {
let params = rhs
.find(')')
.and_then(|close| rhs.find('(').map(|open| &rhs[open..=close]))
.unwrap_or("()");
push_unique(&mut fn_names, var_name.to_string(), 10);
push_unique(&mut fn_sigs, format!("{}{}", var_name, params), 10);
} else if kw != "const " || rhs.starts_with('{') || rhs.starts_with('[') || rhs.starts_with("new ") {
push_unique(&mut state_vars, var_name.to_string(), 10);
}
}
}
}
}
for fkw in ["function ", "async function "] {
if let Some(rest) = t.strip_prefix(fkw) {
if let Some(open_p) = rest.find('(') {
let name = rest[..open_p].trim();
if !name.is_empty()
&& name
.bytes()
.all(|b| b.is_ascii_alphanumeric() || b == b'_' || b == b'$')
{
let close_p = rest[open_p..].find(')').map(|c| open_p + c).unwrap_or(open_p);
let params = if close_p > open_p {
&rest[open_p..=close_p]
} else {
"()"
};
push_unique(&mut fn_names, name.to_string(), 10);
push_unique(&mut fn_sigs, format!("{}{}", name, params), 10);
}
}
}
}
}
let state_str = if state_vars.is_empty() {
"Internal application state".to_string()
} else {
state_vars.join(" | ")
};
let methods_str = if fn_sigs.is_empty() {
"Event dispatchers & render cycle".to_string()
} else {
fn_sigs.join(" | ")
};
let mut edges = Vec::new();
if text.contains("addEventListener(\"click\"")
|| text.contains("addEventListener('click'")
|| text.contains("onclick=")
{
let btn = dom_ids
.iter()
.find(|id| id.contains("btn") || id.contains("start") || id.contains("toggle") || id.contains("reset"))
.cloned()
.or_else(|| dom_ids.first().cloned())
.unwrap_or_else(|| "#control".to_string());
let handler = fn_names
.iter()
.find(|f| f.contains("click") || f.contains("handle") || f.contains("toggle") || f.contains("start"))
.cloned()
.or_else(|| fn_names.first().cloned())
.unwrap_or_else(|| "handleClick".to_string());
edges.push(format!("({}) --[ON_CLICK]--> ({}()) --[MUTATES]--> (State)", btn, handler));
}
if text.contains("requestAnimationFrame") || text.contains("setInterval") {
let loop_fn = fn_names
.iter()
.find(|f| f.contains("render") || f.contains("animate") || f.contains("loop") || f.contains("tick") || f.contains("draw"))
.cloned()
.unwrap_or_else(|| "renderLoop".to_string());
let target = dom_ids
.iter()
.find(|id| id.contains("canvas") || id.contains("stage") || id.contains("viewport"))
.cloned()
.unwrap_or_else(|| "#canvas".to_string());
edges.push(format!("(requestAnimationFrame) --[TICK_CADENCE]--> ({}()) --[DRAWS_FRAME]--> ({})", loop_fn, target));
}
if edges.is_empty() {
let first_fn = fn_names.first().cloned().unwrap_or_else(|| "handler".to_string());
edges.push(format!("(UserInteraction) --[EVENT_DISPATCH]--> ({}()) --[RE_RENDERS]--> (DOM)", first_fn));
}
format!(
"<!-- [RELATIONAL ARTIFACT BLUEPRINT: \"{}\"]\nArchitecture & Stack: {}\nNodes:\n * DOM Elements: {}\n * State Stores: {}\n * Methods: {}\nRelational Edges:\n * {}\nGlobal Invariants:\n * Preserve existing DOM element IDs ({}), state variables ({}), and method signatures ({}) while implementing requested updates.\n-->",
title,
stack_str,
dom_str,
state_str,
methods_str,
edges.join("\n * "),
dom_str,
state_str,
methods_str
)
}
pub fn distill_diagram_blueprint(raw_mermaid: &str) -> String {
let text = raw_mermaid.trim();
if text.len() <= 250 || text.contains("[RELATIONAL DIAGRAM BLUEPRINT:") {
return text.to_string();
}
let mut lines = text.lines().map(str::trim).filter(|l| !l.is_empty());
let first_line = lines.next().unwrap_or("flowchart TD");
let mut subgraphs = Vec::new();
let mut nodes = Vec::new();
let mut edges = Vec::new();
let mut total_transitions = 0usize;
for line in text.lines() {
let t = line.trim();
if let Some(rest) = t.strip_prefix("subgraph ") {
let sg_id = rest.split(['[', ' ']).next().unwrap_or("").trim();
push_unique(&mut subgraphs, sg_id.to_string(), 8);
continue;
}
for arrow in ["-->", "==>", "-.->", "->>", "-->>"] {
if let Some(pos) = t.find(arrow) {
total_transitions += 1;
let left = t[..pos].trim();
let right = t[pos + arrow.len()..].trim();
let from_id = left
.split(['[', '(', '{', ' '])
.next()
.unwrap_or("")
.trim();
let after_label = if right.starts_with('|') {
right[1..]
.find('|')
.map(|end_bar| right[end_bar + 2..].trim())
.unwrap_or(right)
} else {
right
};
let to_id = after_label
.split(['[', '(', '{', ':', ' '])
.next()
.unwrap_or("")
.trim();
push_unique(&mut nodes, from_id.to_string(), 14);
push_unique(&mut nodes, to_id.to_string(), 14);
if !from_id.is_empty() && !to_id.is_empty() {
push_unique(&mut edges, format!("{} {} {}", from_id, arrow, to_id), 14);
}
break;
}
}
}
format!(
"%% [RELATIONAL DIAGRAM BLUEPRINT: {}]\nSubgraphs: {}\nEntities: {}\nEdges: {}\nTopology: ~{} connected transitions.\n[REFINEMENT DIRECTIVE]: Preserve existing node identifiers and relationship topology while applying updates. %%",
first_line,
if subgraphs.is_empty() { "None".to_string() } else { subgraphs.join(", ") },
nodes.join(", "),
edges.join(", "),
total_transitions
)
}
pub fn distill_voxel_blueprint(raw_json: &str) -> String {
let trimmed = raw_json.trim();
if trimmed.contains("[RELATIONAL VOXEL BLUEPRINT:") {
return trimmed.to_string();
}
let Ok(parsed) = serde_json::from_str::<Value>(trimmed) else {
return trimmed.to_string();
};
let Some(obj) = parsed.as_object() else {
return trimmed.to_string();
};
let voxel_count = obj
.get("voxels")
.and_then(|v| v.as_array())
.map(|a| a.len())
.unwrap_or(0);
if voxel_count <= 5 {
return trimmed.to_string();
}
let title = obj
.get("title")
.and_then(|v| v.as_str())
.unwrap_or("3D Voxel Model");
let dims = obj
.get("dimensions")
.map(|d| d.to_string())
.unwrap_or_else(|| "[16,16,16]".to_string());
let palette_keys: Vec<String> = obj
.get("palette")
.and_then(|p| p.as_object())
.map(|m| m.keys().cloned().collect())
.unwrap_or_default();
format!(
"<!-- [RELATIONAL VOXEL BLUEPRINT: {}]\nDimensions: {}\nPalette Keys: {}\nVoxel Count: {}\n[REFINEMENT DIRECTIVE]: Preserve existing palette keys and dimensions while applying requested modifications. -->",
title,
dims,
palette_keys.join(", "),
voxel_count
)
}
pub fn distill_document_graph_blueprint(raw_doc: &str) -> String {
let text = raw_doc.trim();
if text.len() <= 800 || text.contains("[RELATIONAL ARTIFACT BLUEPRINT:") {
return text.to_string();
}
let mut title = "Structured Document".to_string();
let mut sections = Vec::new();
for line in text.lines() {
let t = line.trim();
if let Some(h1) = t.strip_prefix("# ") {
if title == "Structured Document" {
title = h1.trim().replace('"', "");
}
} else if let Some(h2) = t.strip_prefix("## ") {
push_unique(&mut sections, h2.trim().to_string(), 12);
} else if let Some(h3) = t.strip_prefix("### ") {
push_unique(&mut sections, h3.trim().to_string(), 12);
}
}
let lower = text.to_ascii_lowercase();
let is_contract = lower.contains("agreement")
|| lower.contains("indemn")
|| lower.contains("liability")
|| lower.contains("confidentiality")
|| lower.contains("statement of work");
let is_resume = lower.contains("curriculum vitae")
|| lower.contains("resume")
|| (lower.contains("experience") && lower.contains("skills") && lower.contains("education"));
let mut nodes = Vec::new();
let mut edges = Vec::new();
let invariant: &str;
if is_contract {
nodes.push(format!("Agreement(\"{}\")", title));
for s in §ions {
nodes.push(format!("Clause(\"{}\")", s));
}
if sections.iter().any(|s| s.to_ascii_lowercase().contains("indemn"))
&& sections.iter().any(|s| s.to_ascii_lowercase().contains("liabil"))
{
edges.push("(Clause:Indemnity) --[BOUND_BY_CAP]--> (Clause:Liability)".to_string());
}
if sections.iter().any(|s| s.to_ascii_lowercase().contains("terminat")) {
edges.push("(Clause:Breach) --[TRIGGERS_NOTICE]--> (Clause:Termination)".to_string());
}
if edges.is_empty() {
edges.push(format!(
"(Parties) --[EXECUTE_TERMS]--> ({})",
sections.join(", ")
));
}
invariant = "Cross-clause legal coherence, party definitions, and liability boundaries must remain synchronized.";
} else if is_resume {
nodes.push(format!("Candidate(\"{}\")", title));
for s in §ions {
nodes.push(format!("Section(\"{}\")", s));
}
edges.push("(Candidate) --[PROVEN_EXPERIENCE]--> (Experience) --[DEMONSTRATES_STACK]--> (Skills)".to_string());
invariant = "Chronological accuracy, role titles, and technical skill alignment must be preserved across all sections.";
} else {
nodes.push(format!("Document(\"{}\")", title));
for s in §ions {
nodes.push(format!("Section(\"{}\")", s));
}
for pair in sections.windows(2).take(4) {
edges.push(format!(
"(Section:{}) --[FLOWS_INTO]--> (Section:{})",
pair[0], pair[1]
));
}
if edges.is_empty() {
edges.push("(Document) --[DECOMPOSES_INTO]--> (Core Sections)".to_string());
}
invariant = "Narrative flow, entity definitions, and section structure must be maintained without disjointed transitions.";
}
format!(
"<!-- [RELATIONAL ARTIFACT BLUEPRINT: \"{}\"]\nNodes:\n * {}\nRelationships:\n * {}\nGlobal Invariants:\n * {}\n-->",
title,
nodes.join(" | "),
edges.join("\n * "),
invariant
)
}
pub fn distill_turn_for_commit(content: &str) -> String {
let cleaned = purge_reasoning_artifacts(content);
if cleaned.len() <= 320 || cleaned.contains("[RELATIONAL ARTIFACT BLUEPRINT:") {
return cleaned;
}
if !cleaned.contains("```") {
if cleaned.contains("<!DOCTYPE") || cleaned.contains("<html") {
return distill_app_graph_blueprint(&cleaned, "Interactive Application");
}
let heading_count = cleaned
.lines()
.filter(|l| {
let t = l.trim();
t.starts_with("## ") || t.starts_with("### ")
})
.count();
if cleaned.len() > 800 && heading_count >= 3 {
return distill_document_graph_blueprint(&cleaned);
}
return cleaned;
}
let mut out = String::with_capacity(cleaned.len() / 2);
let mut lines = cleaned.lines().peekable();
let mut in_fence = false;
let mut fence_marker = String::new();
let mut fence_lang = String::new();
let mut fence_body = Vec::new();
while let Some(line) = lines.next() {
let trimmed = line.trim();
if !in_fence && trimmed.starts_with("```") {
let ticks: String = trimmed.chars().take_while(|&c| c == '`').collect();
let lang = trimmed[ticks.len()..].trim().to_ascii_lowercase();
in_fence = true;
fence_marker = ticks;
fence_lang = lang;
fence_body.clear();
continue;
}
if in_fence && trimmed.starts_with(&fence_marker) && trimmed.chars().all(|c| c == '`') {
let body_str = fence_body.join("\n");
let compacted_body = distill_fenced_block(&fence_lang, &body_str);
out.push_str(&format!("```{}\n{}\n```\n", fence_lang, compacted_body));
in_fence = false;
fence_body.clear();
continue;
}
if in_fence {
fence_body.push(line);
} else {
out.push_str(line);
out.push('\n');
}
}
if in_fence && !fence_body.is_empty() {
let body_str = fence_body.join("\n");
let compacted_body = distill_fenced_block(&fence_lang, &body_str);
out.push_str(&format!("```{}\n{}\n```\n", fence_lang, compacted_body));
}
out.trim_end().to_string()
}
fn distill_fenced_block(lang: &str, body: &str) -> String {
let trimmed = body.trim();
if trimmed.len() <= 250 {
return trimmed.to_string();
}
if lang == "mermaid"
|| lang == "diagram"
|| lang == "flowchart"
|| trimmed.starts_with("flowchart ")
|| trimmed.starts_with("sequenceDiagram")
{
return distill_diagram_blueprint(trimmed);
}
if lang == "voxel"
|| lang == "voxel_world"
|| (trimmed.starts_with('{') && trimmed.contains("\"voxels\""))
{
return distill_voxel_blueprint(trimmed);
}
if lang == "html"
|| lang == "visual"
|| lang == "interactive"
|| lang == "sandbox"
|| lang.starts_with("canvas")
|| trimmed.contains("<!DOCTYPE")
|| trimmed.contains("<html")
|| trimmed.contains("<script")
{
return distill_app_graph_blueprint(trimmed, "Interactive Application");
}
if lang == "markdown" || lang == "md" || lang == "document" {
return distill_document_graph_blueprint(trimmed);
}
compact_code_skeleton(trimmed, 30)
}
pub fn compact_cold_start_session(messages: &[ChatMessage]) -> Vec<ChatMessage> {
if messages.is_empty() {
return Vec::new();
}
let last_idx = messages.len() - 1;
let mut compacted: Vec<ChatMessage> = messages
.iter()
.enumerate()
.map(|(idx, m)| {
if m.role == "system" || (idx == last_idx && m.role == "user") {
m.clone()
} else if m.role == "user" && m.content.contains("[CURRENT ARTIFACT CODE]") {
let mut collapsed = m.content.clone();
if let Some(pos) = collapsed.find("[CURRENT ARTIFACT CODE]") {
let prefix = &collapsed[..pos];
collapsed = format!(
"{}[PREVIOUS ARTIFACT CODE: Compacted in conversation history]",
prefix
);
}
ChatMessage {
role: m.role.clone(),
content: collapsed,
cache_control: m.cache_control.clone(),
}
} else {
ChatMessage {
role: m.role.clone(),
content: distill_turn_for_commit(&m.content),
cache_control: m.cache_control.clone(),
}
}
})
.collect();
let blueprint_indices: Vec<usize> = compacted
.iter()
.enumerate()
.filter_map(|(idx, m)| {
if m.role == "assistant"
&& (m.content.contains("[RELATIONAL ARTIFACT BLUEPRINT:")
|| m.content.contains("[RELATIONAL DIAGRAM BLUEPRINT:")
|| m.content.contains("[RELATIONAL VOXEL BLUEPRINT:"))
{
Some(idx)
} else {
None
}
})
.collect();
if blueprint_indices.len() > 1 {
for &old_idx in &blueprint_indices[..blueprint_indices.len() - 1] {
compacted[old_idx].content =
"<!-- [HISTORICAL ARTIFACT VERSION: Superseded by newer blueprint in active working memory.] -->"
.to_string();
}
}
compacted
}