langchainrust 0.7.0

A LangChain-inspired framework for building LLM applications in Rust. Supports OpenAI, Agents, Tools, Memory, Chains, RAG, BM25, Hybrid Retrieval, LangGraph, HyDE, Reranking, MultiQuery, and native Function Calling.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
// src/agents/deep_research/synthesizer.rs
//! Synthesizer - aggregates search results and uses the LLM to write a
//! comprehensive markdown report with inline citations, then identifies
//! any remaining information gaps for follow-up rounds.

use crate::core::language_models::BaseChatModel;
use crate::core::token_counter::count_tokens;
use crate::schema::Message;

use super::planner::ResearchPlan;
use super::searcher::SearchResult;
use super::ResearchError;

/// Output of the synthesis step: a markdown report and a list of
/// information gaps that need follow-up queries.
pub struct SynthesisOutput {
    /// The markdown report with inline citation markers like \[1\], \[2\].
    pub report: String,
    /// Information gaps that remain after this synthesis pass.
    pub gaps: Vec<String>,
}

/// Synthesizes a research report from the plan and collected search results.
///
/// Returns the markdown report and a list of information gaps.
/// When `max_source_tokens` is set, source snippets are truncated to fit
/// within the token budget.
pub async fn synthesize<M: BaseChatModel>(
    llm: &M,
    topic: &str,
    plan: &ResearchPlan,
    results: &[SearchResult],
    max_source_tokens: Option<usize>,
) -> Result<(String, Vec<String>), ResearchError> {
    let subtopics_text = plan
        .subtopics
        .iter()
        .enumerate()
        .map(|(i, st)| {
            format!(
                "{}. {} (queries: {})",
                i + 1,
                st.name,
                st.queries.join(", ")
            )
        })
        .collect::<Vec<_>>()
        .join("\n");

    let sources_text = format_sources(results, max_source_tokens);

    let prompt = format!(
        "Research topic: {}\n\n\
         Sub-topics investigated:\n{}\n\n\
         Sources collected:\n{}\n\n\
         Write a comprehensive research report in markdown format. \
         Use inline citation markers [1], [2], etc. to reference the sources above. \
         The report should cover all sub-topics and synthesize findings across sources.\n\n\
         After the report, identify any information gaps that need further research.\n\n\
         Example output format:\n\
         <<<REPORT>>>\n\
         # Topic Title\n\n\
         This report examines [overview]. Key findings include [summary].\n\n\
         ## Sub-topic 1\n\n\
         According to the research [1], [finding]. Additional analysis [2] shows [detail].\n\n\
         ## Sub-topic 2\n\n\
         Studies indicate [3] that [finding]. This suggests [implication].\n\n\
         ## Conclusion\n\n\
         In summary, [synthesis of findings across sources].\n\
         <<<END_REPORT>>>\n\
         <<<GAPS>>>\n\
         [\"Specific gap that needs more research\"]\n\
         <<<END_GAPS>>>\n\n\
         Output your response in this exact format (do not wrap in JSON):\n\
         <<<REPORT>>>\n\
         (your full markdown report here)\n\
         <<<END_REPORT>>>\n\
         <<<GAPS>>>\n\
         (a JSON array of gap strings, e.g. [\"gap1\", \"gap2\"], or [] if none)\n\
         <<<END_GAPS>>>",
        topic, subtopics_text, sources_text,
    );

    let messages = vec![
        Message::system(
            "You are a research synthesis assistant. Write comprehensive, \
             well-structured reports with proper citations.",
        ),
        Message::human(prompt),
    ];

    let response = llm
        .chat(messages, None)
        .await
        .map_err(|e| ResearchError::Llm(format!("{:?}", e)))?;

    parse_synthesis(&response.content)
}

/// Formats search results into a sources text string.
///
/// When `max_source_tokens` is set, snippets are truncated to fit within
/// the budget, keeping higher-indexed (later, potentially more relevant)
/// sources intact by truncating earlier, longer snippets.
fn format_sources(results: &[SearchResult], max_source_tokens: Option<usize>) -> String {
    let entries: Vec<String> = results
        .iter()
        .enumerate()
        .map(|(i, r)| {
            let url_part = if r.url.is_empty() {
                String::new()
            } else {
                format!(" ({})", r.url)
            };
            format!("[{}] {}{}: {}", i + 1, r.title, url_part, r.snippet)
        })
        .collect();

    match max_source_tokens {
        Some(budget) => {
            let mut used_tokens = 0usize;
            let mut truncated_entries = Vec::new();

            for entry in &entries {
                let entry_tokens = count_tokens(entry);
                if used_tokens + entry_tokens > budget && !truncated_entries.is_empty() {
                    break;
                }
                used_tokens += entry_tokens;
                truncated_entries.push(entry.clone());
            }

            truncated_entries.join("\n")
        }
        None => entries.join("\n"),
    }
}

/// Parses the LLM synthesis output into a report and gaps list.
///
/// Supports two formats:
/// 1. **Delimiter format** (preferred): `<<<REPORT>>>...<<<END_REPORT>>><<<GAPS>>>[...]<<<END_GAPS>>>`
/// 2. **Legacy JSON format** (fallback): `{"report": "...", "gaps": [...]}`
fn parse_synthesis(content: &str) -> Result<(String, Vec<String>), ResearchError> {
    // Try delimiter format first
    if let Some(result) = parse_delimiter_format(content) {
        return Ok(result);
    }

    // Fallback to legacy JSON format using tolerant parsing
    #[derive(serde::Deserialize)]
    struct SynthesisResponse {
        report: String,
        #[serde(default)]
        gaps: Vec<String>,
    }

    let parsed: SynthesisResponse = crate::core::json_parse::parse_llm_json(content).map_err(|e| {
        ResearchError::Llm(format!("failed to parse synthesis response: {}", e))
    })?;

    Ok((parsed.report, parsed.gaps))
}

/// Parses the delimiter-based format: <<<REPORT>>>...<<<END_REPORT>>><<<GAPS>>>...<<<END_GAPS>>>
fn parse_delimiter_format(content: &str) -> Option<(String, Vec<String>)> {
    let report_start = content.find("<<<REPORT>>>")?;
    let report_end = content.find("<<<END_REPORT>>>")?;

    if report_end <= report_start {
        return None;
    }

    let report_content = content[report_start + "<<<REPORT>>>".len()..report_end].trim();

    let gaps_start = content.find("<<<GAPS>>>")?;
    let gaps_end = content.find("<<<END_GAPS>>>")?;

    if gaps_end <= gaps_start {
        return None;
    }

    let gaps_text = content[gaps_start + "<<<GAPS>>>".len()..gaps_end].trim();

    let gaps: Vec<String> = if gaps_text.is_empty() || gaps_text == "[]" {
        Vec::new()
    } else {
        // Try JSON parse; if it fails, split by newlines as a rough fallback
        serde_json::from_str(gaps_text).unwrap_or_else(|_| {
            gaps_text
                .lines()
                .map(|l| l.trim().trim_start_matches('-').trim().to_string())
                .filter(|l| !l.is_empty())
                .collect()
        })
    };

    Some((report_content.to_string(), gaps))
}

/// Extracts a JSON object from LLM output, tolerating markdown fences
/// and surrounding text.
#[cfg(test)]
fn extract_json_object(content: &str) -> String {
    let trimmed = content.trim();

    // Strip markdown code fences
    let stripped = if trimmed.starts_with("```") {
        trimmed
            .strip_prefix("```json")
            .or_else(|| trimmed.strip_prefix("```"))
            .unwrap_or(trimmed)
            .strip_suffix("```")
            .unwrap_or(trimmed)
            .trim()
    } else {
        trimmed
    };

    // Find the outermost { and its matching }
    if let Some(start) = stripped.find('{') {
        let mut depth = 0i32;
        let mut in_string = false;
        let mut escape_next = false;
        let bytes = stripped.as_bytes();

        for i in start..bytes.len() {
            let ch = bytes[i];
            if escape_next {
                escape_next = false;
                continue;
            }
            if ch == b'\\' {
                if in_string {
                    escape_next = true;
                }
                continue;
            }
            if ch == b'"' {
                in_string = !in_string;
                continue;
            }
            if in_string {
                continue;
            }
            if ch == b'{' {
                depth += 1;
            } else if ch == b'}' {
                depth -= 1;
                if depth == 0 {
                    return stripped[start..=i].to_string();
                }
            }
        }
    }

    stripped.to_string()
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_extract_json_object_plain() {
        let input = r#"{"report": "text", "gaps": []}"#;
        assert_eq!(extract_json_object(input), input);
    }

    #[test]
    fn test_extract_json_object_markdown() {
        let input = "```json\n{\"report\": \"text\", \"gaps\": []}\n```";
        let expected = r#"{"report": "text", "gaps": []}"#;
        assert_eq!(extract_json_object(input), expected);
    }

    #[test]
    fn test_extract_json_object_with_surrounding_text() {
        let input = r#"Here is the result: {"report": "text", "gaps": ["gap1"]} done."#;
        let expected = r#"{"report": "text", "gaps": ["gap1"]}"#;
        assert_eq!(extract_json_object(input), expected);
    }

    #[test]
    fn test_extract_json_object_nested() {
        let input = "{\"report\": \"# Title\\n\\nContent [1].\", \"gaps\": []}";
        assert_eq!(extract_json_object(input), input);
    }

    #[test]
    fn test_parse_synthesis_valid() {
        let content =
            "{\"report\": \"# Report\\n\\nContent [1].\", \"gaps\": [\"need more data\"]}";
        let (report, gaps) = parse_synthesis(content).unwrap();
        assert!(report.contains("# Report"));
        assert_eq!(gaps, vec!["need more data"]);
    }

    #[test]
    fn test_parse_synthesis_no_gaps() {
        let content = "{\"report\": \"# Report\\n\\nDone.\", \"gaps\": []}";
        let (report, gaps) = parse_synthesis(content).unwrap();
        assert!(report.contains("Done"));
        assert!(gaps.is_empty());
    }

    #[test]
    fn test_parse_synthesis_missing_gaps_field() {
        // gaps defaults to empty vec via #[serde(default)]
        let content = "{\"report\": \"# Report\"}";
        let (_, gaps) = parse_synthesis(content).unwrap();
        assert!(gaps.is_empty());
    }

    #[test]
    fn test_parse_synthesis_invalid() {
        let content = "not json";
        let result = parse_synthesis(content);
        assert!(result.is_err());
    }

    #[test]
    fn test_extract_json_object_with_braces_in_strings() {
        let input = r#"{"report": "Use {curly} braces", "gaps": []}"#;
        assert_eq!(extract_json_object(input), input);
    }

    #[test]
    fn test_parse_delimiter_format_basic() {
        let content = "<<<REPORT>>>\n# Report\n\nContent [1].\n<<<END_REPORT>>>\n<<<GAPS>>>\n[\"need more data\"]\n<<<END_GAPS>>>";
        let (report, gaps) = parse_synthesis(content).unwrap();
        assert!(report.contains("# Report"));
        assert!(report.contains("Content [1]."));
        assert_eq!(gaps, vec!["need more data"]);
    }

    #[test]
    fn test_parse_delimiter_format_no_gaps() {
        let content =
            "<<<REPORT>>>\n# Report\n\nDone.\n<<<END_REPORT>>>\n<<<GAPS>>>\n[]\n<<<END_GAPS>>>";
        let (report, gaps) = parse_synthesis(content).unwrap();
        assert!(report.contains("Done"));
        assert!(gaps.is_empty());
    }

    #[test]
    fn test_parse_delimiter_format_special_chars() {
        let content = "<<<REPORT>>>\n# Report\n\nCode: `let x = \"hello\";`\nPath: C:\\Users\\test\n<<<END_REPORT>>>\n<<<GAPS>>>\n[]\n<<<END_GAPS>>>";
        let (report, gaps) = parse_synthesis(content).unwrap();
        assert!(report.contains("let x = \"hello\""));
        assert!(report.contains("C:\\Users\\test"));
        assert!(gaps.is_empty());
    }

    #[test]
    fn test_parse_synthesis_legacy_json_still_works() {
        let content =
            "{\"report\": \"# Report\\n\\nContent [1].\", \"gaps\": [\"need more data\"]}";
        let (report, gaps) = parse_synthesis(content).unwrap();
        assert!(report.contains("# Report"));
        assert_eq!(gaps, vec!["need more data"]);
    }

    #[test]
    fn test_format_sources_no_limit() {
        let results = vec![
            SearchResult { query: "q1".into(), title: "Source A".into(), snippet: "Content A".into(), url: String::new() },
            SearchResult { query: "q2".into(), title: "Source B".into(), snippet: "Content B".into(), url: String::new() },
        ];
        let text = format_sources(&results, None);
        assert!(text.contains("[1]"));
        assert!(text.contains("[2]"));
        assert!(text.contains("Source A"));
        assert!(text.contains("Source B"));
    }

    #[test]
    fn test_format_sources_with_token_limit_truncates() {
        let results = vec![
            SearchResult { query: "q1".into(), title: "Source A".into(), snippet: "A fairly long snippet that will consume some tokens when counted by the tokenizer".into(), url: String::new() },
            SearchResult { query: "q2".into(), title: "Source B".into(), snippet: "Another fairly long snippet that also consumes tokens".into(), url: String::new() },
        ];
        // With a very small budget, only the first source should be included
        let text = format_sources(&results, Some(20));
        assert!(text.contains("[1]"));
        assert!(text.contains("Source A"));
        // Second source should be truncated away
        // (exact behavior depends on token count, but with budget=20 only 1 entry fits)
    }

    #[test]
    fn test_format_sources_includes_urls() {
        let results = vec![
            SearchResult { query: "q1".into(), title: "Source A".into(), snippet: "Content".into(), url: "https://example.com".into() },
        ];
        let text = format_sources(&results, None);
        assert!(text.contains("https://example.com"));
    }

    /// Verify the synthesis prompt contains few-shot example content.
    #[test]
    fn test_synthesize_prompt_contains_few_shot_example() {
        // The prompt is built inline in synthesize(), but we can verify
        // the example format section appears in the generated prompt text.
        let topic = "Test Topic";
        let subtopics_text = "1. Sub-topic A (queries: q1)";
        let sources_text = "[1] Source A: Some content";

        let prompt = format!(
            "Research topic: {}\n\n\
             Sub-topics investigated:\n{}\n\n\
             Sources collected:\n{}\n\n\
             Write a comprehensive research report in markdown format. \
             Use inline citation markers [1], [2], etc. to reference the sources above. \
             The report should cover all sub-topics and synthesize findings across sources.\n\n\
             After the report, identify any information gaps that need further research.\n\n\
             Example output format:\n\
             <<<REPORT>>>\n\
             # Topic Title\n\n\
             This report examines [overview]. Key findings include [summary].\n\n\
             ## Sub-topic 1\n\n\
             According to the research [1], [finding]. Additional analysis [2] shows [detail].\n\n\
             ## Sub-topic 2\n\n\
             Studies indicate [3] that [finding]. This suggests [implication].\n\n\
             ## Conclusion\n\n\
             In summary, [synthesis of findings across sources].\n\
             <<<END_REPORT>>>\n\
             <<<GAPS>>>\n\
             [\"Specific gap that needs more research\"]\n\
             <<<END_GAPS>>>\n\n\
             Output your response in this exact format (do not wrap in JSON):\n\
             <<<REPORT>>>\n\
             (your full markdown report here)\n\
             <<<END_REPORT>>>\n\
             <<<GAPS>>>\n\
             (a JSON array of gap strings, e.g. [\"gap1\", \"gap2\"], or [] if none)\n\
             <<<END_GAPS>>>",
            topic, subtopics_text, sources_text,
        );

        assert!(prompt.contains("Example output format:"), "prompt should contain few-shot example header");
        assert!(prompt.contains("<<<REPORT>>>"), "prompt example should contain delimiter format");
        assert!(prompt.contains("Sub-topic 1"), "prompt example should contain sub-topic example");
        assert!(prompt.contains("<<<GAPS>>>"), "prompt example should contain gaps section");
    }
}