refactor: massive codebase restructuring — naming, splitting, DRY
Crate renames: - zesdex-entities::seaorm → domain (misleading name, no SeaORM used) - zesdex-dto → merged into zesdex-entities (100% re-exports) - zesdex-libs → zesdex-infra (vague name) Module renames: - app/harness → guard (misleading: safety gatekeeper, not test harness) - runtime/commands → action_dispatch (name clashed with controller/command) - resources → prompts (embedded prompt text, not general resources) - tool/seqthink → sequential_think (unreadable abbreviation) - msglog/query → insert (module only inserts, never queries) Dead code removal: - app/mode/help.rs (orphaned — not declared in mod.rs) - app/mode/loading.rs (orphaned — not declared in mod.rs) File splitting (71 new files, avg ~115 lines/file): - app/runtime/actions/: 1→8 files (was 2030 lines) - view/overlays/: 1→16 files (was 1167 lines) - tool/lsp/: 1→8 per-tool files (was 909 lines) - main.rs: 1→5 files (session, daemon, attach, event_loop) - workflow/engine + hive_mind: 2→10 files - subagent/engine + auto: 2→9 files - lsp/provisioner: 1→5 files - review/: 1→6 files - guard/: 1→2 files (extracted patterns) - state/misc: 1→3 files (input, scroll) - mcp/: 1→3 files (transport, adapter) - stream/json_repair extracted from turn.rs DRY: - Pattern constants (STUB_PATTERNS etc) in guard/patterns shared with subagent - 3 near-identical background spawners → 1 generic + thin wrappers - Shared spawn_subagent_with_drain() extracted - Shared create_session() in main - write_osc52 deduplicated Bug fixes: - archive_message(): sess.db → db (wrong variable name) - execute_one_tool(): wrong parameter name - check_credential_read() function was missing (restored from test expectations)
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
//! Utility to repair truncated JSON by closing open strings, braces, and
|
||||
//! brackets using a LIFO stack.
|
||||
//!
|
||||
//! LLM responses can be cut off (`max_tokens`, network) mid‑JSON string, but
|
||||
//! we want tools to receive whatever arguments were already emitted so the
|
||||
//! partial work can proceed.
|
||||
//!
|
||||
//! Why LIFO vs. depth counters: `{` inside `[` must be closed with `}`
|
||||
//! *before* the `]`, not after it. Simple depth counters get the order
|
||||
//! wrong for nested heterogenous structures.
|
||||
|
||||
/// Try to repair truncated JSON by closing open strings, braces, and brackets.
|
||||
///
|
||||
/// Flow: scan character-by-character tracking string/escape state. For
|
||||
/// every `{` or `[` seen outside a string, push onto a LIFO stack; on
|
||||
/// `}`/`]` pop the matching opener (tracking remaining depth only).
|
||||
/// At the end, if the last char was a backslash (start of an escape
|
||||
/// sequence), remove it; if inside a string, append `"`; then close
|
||||
/// every unclosed opener in reverse (LIFO) order.
|
||||
pub fn repair_incomplete_json(s: &str) -> String {
|
||||
let mut stack: Vec<char> = Vec::new();
|
||||
let mut in_string = false;
|
||||
let mut prev_was_backslash = false;
|
||||
// `true` only when the very last character consumed was a bare `\`
|
||||
// inside a string (i.e. the start of an escape that was never completed).
|
||||
let mut ends_with_unclosed_escape = false;
|
||||
|
||||
for c in s.chars() {
|
||||
if prev_was_backslash {
|
||||
// Consume the character that was being escaped — the escape is
|
||||
// complete, so clear the unclosed-escape flag.
|
||||
prev_was_backslash = false;
|
||||
ends_with_unclosed_escape = false;
|
||||
continue;
|
||||
}
|
||||
if c == '\\' && in_string {
|
||||
prev_was_backslash = true;
|
||||
ends_with_unclosed_escape = true;
|
||||
continue;
|
||||
}
|
||||
ends_with_unclosed_escape = false;
|
||||
if c == '"' {
|
||||
in_string = !in_string;
|
||||
continue;
|
||||
}
|
||||
if in_string {
|
||||
continue;
|
||||
}
|
||||
match c {
|
||||
'{' | '[' => stack.push(c),
|
||||
'}' | ']' => {
|
||||
stack.pop();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let mut result = s.to_string();
|
||||
if ends_with_unclosed_escape {
|
||||
// The last character is a dangling backslash that started an escape
|
||||
// but got cut off before the escaped char — remove it.
|
||||
result.pop();
|
||||
}
|
||||
if in_string {
|
||||
result.push('"');
|
||||
}
|
||||
for &opener in stack.iter().rev() {
|
||||
match opener {
|
||||
'{' => result.push('}'),
|
||||
'[' => result.push(']'),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn repair_closes_unclosed_string() {
|
||||
let result = repair_incomplete_json("{\"key\": \"value");
|
||||
assert_eq!(result, "{\"key\": \"value\"}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repair_closes_unclosed_object() {
|
||||
let result = repair_incomplete_json("{\"key\": \"value\"");
|
||||
assert_eq!(result, "{\"key\": \"value\"}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repair_closes_nested_structures() {
|
||||
let result = repair_incomplete_json("{\"a\": [1, 2, {\"b\": 3");
|
||||
assert_eq!(result, "{\"a\": [1, 2, {\"b\": 3}]}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repair_leaves_complete_json_unchanged() {
|
||||
let s = "{\"a\": 1, \"b\": \"hello\"}";
|
||||
assert_eq!(repair_incomplete_json(s), s);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repair_handles_trailing_backslash_before_cut() {
|
||||
// Truncated inside an escape sequence like "hello\"
|
||||
let result = repair_incomplete_json("{\"text\": \"hello\\");
|
||||
assert_eq!(result, "{\"text\": \"hello\"}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repair_handles_escaped_quotes_inside_string() {
|
||||
// Input ends with `\"` where the `"` is the escaped character
|
||||
// (consumed by the backslash handler), so the string is still
|
||||
// unterminated. Repair adds `"` to close the string and `}` to
|
||||
// close the object.
|
||||
let result = repair_incomplete_json("{\"msg\": \"he said \\\"hello\\\"");
|
||||
assert_eq!(result, "{\"msg\": \"he said \\\"hello\\\"\"}");
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
//! SSE stream parser: converts SSE- or JSON-chunked LLM responses into
|
||||
//! typed `StreamEvent` variants (tokens, reasoning, tool calls, usage, done).
|
||||
pub mod json_repair;
|
||||
pub mod turn;
|
||||
|
||||
pub use zesdex_entities::{SseParser, StreamEvent};
|
||||
|
||||
@@ -1,85 +1,13 @@
|
||||
//! Accumulates streaming LLM responses into complete message/tool-call
|
||||
//! representation via `StreamedTurn`, and provides a standalone tool-call
|
||||
//! accumulator in `tools::ToolCallAccumulator`.
|
||||
use super::json_repair::repair_incomplete_json;
|
||||
use super::StreamEvent;
|
||||
use crate::dto::chat::message::ChatMessage;
|
||||
use crate::dto::chat::tool::{ToolCall, ToolFunction};
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json::Value;
|
||||
|
||||
/// Try to repair truncated JSON by closing open strings, braces, and brackets.
|
||||
///
|
||||
/// Flow: scan character-by-character tracking string/escape state. For
|
||||
/// every `{` or `[` seen outside a string, push onto a LIFO stack; on
|
||||
/// `}`/`]` pop the matching opener (tracking remaining depth only).
|
||||
/// At the end, if the last char was a backslash (start of an escape
|
||||
/// sequence), remove it; if inside a string, append `"`; then close
|
||||
/// every unclosed opener in reverse (LIFO) order.
|
||||
///
|
||||
/// Why: LLM responses can be cut off (`max_tokens`, network) mid‑JSON
|
||||
/// string, but we want tools to receive whatever arguments were already
|
||||
/// emitted so the partial work can proceed.
|
||||
///
|
||||
/// Why LIFO vs. depth counters: `{` inside `[` must be closed with `}`
|
||||
/// *before* the `]`, not after it. Simple depth counters get the order
|
||||
/// wrong for nested heterogenous structures.
|
||||
fn repair_incomplete_json(s: &str) -> String {
|
||||
let mut stack: Vec<char> = Vec::new();
|
||||
let mut in_string = false;
|
||||
let mut prev_was_backslash = false;
|
||||
// `true` only when the very last character consumed was a bare `\`
|
||||
// inside a string (i.e. the start of an escape that was never completed).
|
||||
let mut ends_with_unclosed_escape = false;
|
||||
|
||||
for c in s.chars() {
|
||||
if prev_was_backslash {
|
||||
// Consume the character that was being escaped — the escape is
|
||||
// complete, so clear the unclosed-escape flag.
|
||||
prev_was_backslash = false;
|
||||
ends_with_unclosed_escape = false;
|
||||
continue;
|
||||
}
|
||||
if c == '\\' && in_string {
|
||||
prev_was_backslash = true;
|
||||
ends_with_unclosed_escape = true;
|
||||
continue;
|
||||
}
|
||||
ends_with_unclosed_escape = false;
|
||||
if c == '"' {
|
||||
in_string = !in_string;
|
||||
continue;
|
||||
}
|
||||
if in_string {
|
||||
continue;
|
||||
}
|
||||
match c {
|
||||
'{' | '[' => stack.push(c),
|
||||
'}' | ']' => {
|
||||
stack.pop();
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
let mut result = s.to_string();
|
||||
if ends_with_unclosed_escape {
|
||||
// The last character is a dangling backslash that started an escape
|
||||
// but got cut off before the escaped char — remove it.
|
||||
result.pop();
|
||||
}
|
||||
if in_string {
|
||||
result.push('"');
|
||||
}
|
||||
for &opener in stack.iter().rev() {
|
||||
match opener {
|
||||
'{' => result.push('}'),
|
||||
'[' => result.push(']'),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
/// Accumulates a single streaming assistant turn into its final
|
||||
/// `ChatMessage` form, including tool-call deltas and content/reasoning.
|
||||
#[derive(Debug, Clone, Serialize, Deserialize)]
|
||||
@@ -285,47 +213,6 @@ mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repair_closes_unclosed_string() {
|
||||
let result = repair_incomplete_json("{\"key\": \"value");
|
||||
assert_eq!(result, "{\"key\": \"value\"}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repair_closes_unclosed_object() {
|
||||
let result = repair_incomplete_json("{\"key\": \"value\"");
|
||||
assert_eq!(result, "{\"key\": \"value\"}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repair_closes_nested_structures() {
|
||||
let result = repair_incomplete_json("{\"a\": [1, 2, {\"b\": 3");
|
||||
assert_eq!(result, "{\"a\": [1, 2, {\"b\": 3}]}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repair_leaves_complete_json_unchanged() {
|
||||
let s = "{\"a\": 1, \"b\": \"hello\"}";
|
||||
assert_eq!(repair_incomplete_json(s), s);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repair_handles_trailing_backslash_before_cut() {
|
||||
// Truncated inside an escape sequence like "hello\"
|
||||
let result = repair_incomplete_json("{\"text\": \"hello\\");
|
||||
assert_eq!(result, "{\"text\": \"hello\"}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn repair_handles_escaped_quotes_inside_string() {
|
||||
// Input ends with `\"` where the `"` is the escaped character
|
||||
// (consumed by the backslash handler), so the string is still
|
||||
// unterminated. Repair adds `"` to close the string and `}` to
|
||||
// close the object.
|
||||
let result = repair_incomplete_json("{\"msg\": \"he said \\\"hello\\\"");
|
||||
assert_eq!(result, "{\"msg\": \"he said \\\"hello\\\"\"}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn build_assistant_message_repairs_truncated_tool_call() {
|
||||
let mut turn = StreamedTurn::new();
|
||||
|
||||
Reference in New Issue
Block a user