-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathstrategy.rs
More file actions
314 lines (269 loc) · 9.44 KB
/
Copy pathstrategy.rs
File metadata and controls
314 lines (269 loc) · 9.44 KB
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
// Copyright (c) 2026 vectorless developers
// SPDX-License-Identifier: Apache-2.0
//! Summary generation strategies.
use async_trait::async_trait;
use crate::document::{DocumentTree, NodeId};
use crate::llm::memo::{MemoKey, MemoStore, MemoValue};
use crate::llm::{LlmClient, LlmResult};
use crate::utils::fingerprint::Fingerprint;
/// Configuration for summary strategies.
#[derive(Debug, Clone)]
pub struct SummaryStrategyConfig {
/// Maximum tokens for a summary.
pub max_tokens: usize,
/// Minimum content tokens to generate summary.
pub min_content_tokens: usize,
/// Whether to persist lazy-generated summaries.
pub persist_lazy: bool,
/// Token threshold below which the original content is used as summary
/// instead of calling LLM. Saves API cost for short, self-contained nodes.
/// Set to 0 to always call LLM.
pub shortcut_threshold: usize,
}
impl Default for SummaryStrategyConfig {
fn default() -> Self {
Self {
max_tokens: 200,
min_content_tokens: 50,
persist_lazy: false,
shortcut_threshold: 50,
}
}
}
/// Strategy for generating summaries.
#[derive(Debug, Clone)]
pub enum SummaryStrategy {
/// No summary generation.
None,
/// Generate for all nodes.
Full {
/// Strategy configuration.
config: SummaryStrategyConfig,
},
/// Generate selectively.
Selective {
/// Minimum tokens threshold.
min_tokens: usize,
/// Only generate for branch nodes (non-leaves).
branch_only: bool,
/// Strategy configuration.
config: SummaryStrategyConfig,
},
/// Generate on-demand at query time.
Lazy {
/// Whether to persist generated summaries.
persist: bool,
/// Strategy configuration.
config: SummaryStrategyConfig,
},
}
impl Default for SummaryStrategy {
fn default() -> Self {
Self::Full {
config: SummaryStrategyConfig::default(),
}
}
}
impl SummaryStrategy {
/// Create a "none" strategy.
pub fn none() -> Self {
Self::None
}
/// Create a "full" strategy.
pub fn full() -> Self {
Self::Full {
config: SummaryStrategyConfig::default(),
}
}
/// Create a "selective" strategy.
pub fn selective(min_tokens: usize, branch_only: bool) -> Self {
Self::Selective {
min_tokens,
branch_only,
config: SummaryStrategyConfig::default(),
}
}
/// Create a "lazy" strategy.
pub fn lazy(persist: bool) -> Self {
Self::Lazy {
persist,
config: SummaryStrategyConfig::default(),
}
}
/// Check if we should generate a summary for a node.
pub fn should_generate(
&self,
tree: &DocumentTree,
node_id: NodeId,
token_count: usize,
) -> bool {
match self {
Self::None => false,
Self::Full { .. } => token_count > 0,
Self::Selective {
min_tokens,
branch_only,
..
} => {
let is_branch = !tree.is_leaf(node_id);
let enough_tokens = token_count >= *min_tokens;
if *branch_only {
is_branch && enough_tokens
} else {
enough_tokens
}
}
Self::Lazy { .. } => false, // Generated on-demand
}
}
/// Check if lazy strategy is enabled.
pub fn is_lazy(&self) -> bool {
matches!(self, Self::Lazy { .. })
}
/// Get the config.
pub fn config(&self) -> SummaryStrategyConfig {
match self {
Self::None => SummaryStrategyConfig::default(),
Self::Full { config } => config.clone(),
Self::Selective { config, .. } => config.clone(),
Self::Lazy { config, .. } => config.clone(),
}
}
/// Get the shortcut threshold (tokens below which content is used as-is).
pub fn shortcut_threshold(&self) -> usize {
self.config().shortcut_threshold
}
}
/// Summary generator trait.
#[async_trait]
pub trait SummaryGenerator: Send + Sync {
/// Generate a summary for the given content.
async fn generate(&self, title: &str, content: &str) -> LlmResult<String>;
/// Generate a summary with leaf/non-leaf context.
/// Non-leaf nodes get a navigation-oriented prompt ("what does this section cover"),
/// leaf nodes get a content-oriented prompt ("what does this section say").
async fn generate_for_node(
&self,
title: &str,
content: &str,
is_leaf: bool,
) -> LlmResult<String> {
let _ = is_leaf;
self.generate(title, content).await
}
}
/// LLM-based summary generator.
pub struct LlmSummaryGenerator {
client: LlmClient,
max_tokens: usize,
/// Optional memo store for caching results.
memo_store: Option<MemoStore>,
}
impl LlmSummaryGenerator {
/// Create a new summary generator.
pub fn new(client: LlmClient) -> Self {
Self {
client,
max_tokens: 200,
memo_store: None,
}
}
/// Set max tokens.
pub fn with_max_tokens(mut self, max_tokens: usize) -> Self {
self.max_tokens = max_tokens;
self
}
/// Set memo store for caching.
pub fn with_memo_store(mut self, store: MemoStore) -> Self {
self.memo_store = Some(store);
self
}
}
#[async_trait]
impl SummaryGenerator for LlmSummaryGenerator {
async fn generate(&self, title: &str, content: &str) -> LlmResult<String> {
// Compute content fingerprint for cache key
let content_fp = Fingerprint::from_str(&format!("{}|{}", title, content));
let memo_key = MemoKey::summary(&content_fp);
// Check memo store first
if let Some(ref store) = self.memo_store {
if let Some(cached) = store.get(&memo_key) {
if let Some(summary) = cached.as_summary() {
tracing::debug!("Memo cache hit for summary: {}", title);
return Ok(summary.to_string());
}
}
}
// Generate with LLM
let system_prompt = "You are a document summarization assistant. \
Generate a concise summary (2-3 sentences) of the given section. \
Focus on the main topics and key information. \
Respond with only the summary, no additional text.";
let user_prompt = format!("Title: {}\n\nContent:\n{}", title, content);
let summary = self
.client
.complete_with_max_tokens(&system_prompt, &user_prompt, self.max_tokens as u16)
.await?;
// Cache the result
if let Some(ref store) = self.memo_store {
// Estimate tokens saved (roughly: input + output tokens)
let tokens_saved = (title.len() + content.len() + summary.len()) / 4;
store.put_with_tokens(
memo_key,
MemoValue::Summary(summary.clone()),
tokens_saved as u64,
);
tracing::debug!("Memo cache stored for summary: {}", title);
}
Ok(summary)
}
async fn generate_for_node(
&self,
title: &str,
content: &str,
is_leaf: bool,
) -> LlmResult<String> {
// Compute content fingerprint for cache key (include leaf flag)
let content_fp = Fingerprint::from_str(&format!("{}|{}|leaf={}", title, content, is_leaf));
let memo_key = MemoKey::summary(&content_fp);
// Check memo store first
if let Some(ref store) = self.memo_store {
if let Some(cached) = store.get(&memo_key) {
if let Some(summary) = cached.as_summary() {
tracing::debug!("Memo cache hit for summary: {}", title);
return Ok(summary.to_string());
}
}
}
// Choose prompt based on node type
let system_prompt = if is_leaf {
// Leaf nodes: content-oriented — "what does this section say"
"You are a document summarization assistant. \
Generate a concise summary (2-3 sentences) of the given section's content. \
Focus on the key information and facts presented. \
Respond with only the summary, no additional text."
} else {
// Non-leaf (branch) nodes: navigation-oriented — "what does this section cover"
"You are a document summarization assistant. \
Generate a concise overview (2-3 sentences) describing what topics and subtopics \
this section covers. This summary will be used as a navigation guide. \
Respond with only the summary, no additional text."
};
let user_prompt = format!("Title: {}\n\nContent:\n{}", title, content);
let summary = self
.client
.complete_with_max_tokens(&system_prompt, &user_prompt, self.max_tokens as u16)
.await?;
// Cache the result
if let Some(ref store) = self.memo_store {
let tokens_saved = (title.len() + content.len() + summary.len()) / 4;
store.put_with_tokens(
memo_key,
MemoValue::Summary(summary.clone()),
tokens_saved as u64,
);
tracing::debug!("Memo cache stored for summary: {}", title);
}
Ok(summary)
}
}