vectorless 0.1.26

Hierarchical, reasoning-native document intelligence engine
Documentation
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
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
// Copyright (c) 2026 vectorless developers
// SPDX-License-Identifier: Apache-2.0

//! Prompt templates for different intervention points.
//!
//! Each template is designed for a specific decision context
//! and follows a consistent structure:
//! 1. System context (role definition)
//! 2. Task description
//! 3. Input format
//! 4. Output format (JSON schema)

use super::super::decision::InterventionPoint;

/// Common trait for prompt templates.
pub trait PromptTemplate: Send + Sync {
    /// Get the system prompt.
    fn system_prompt(&self) -> &str;

    /// Get the user prompt template.
    fn user_prompt_template(&self) -> &str;

    /// Get the intervention point this template is for.
    fn intervention_point(&self) -> InterventionPoint;

    /// Get the expected output format (JSON schema hint).
    fn output_format_hint(&self) -> &str;
}

/// Prompt template for START intervention point.
///
/// Used at the beginning of search to:
/// - Understand query intent
/// - Identify entry points
/// - Set search direction
#[derive(Debug, Clone)]
pub struct StartPrompt {
    system: String,
    template: String,
}

impl Default for StartPrompt {
    fn default() -> Self {
        Self::with_fallback()
    }
}

impl StartPrompt {
    /// Create a new start prompt template.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with custom templates.
    pub fn with_templates(system: String, template: String) -> Self {
        Self { system, template }
    }
}

impl PromptTemplate for StartPrompt {
    fn system_prompt(&self) -> &str {
        &self.system
    }

    fn user_prompt_template(&self) -> &str {
        &self.template
    }

    fn intervention_point(&self) -> InterventionPoint {
        InterventionPoint::Start
    }

    fn output_format_hint(&self) -> &str {
        r#"{
  "entry_points": ["list of node titles to start from"],
  "reasoning": "explanation of why these entry points",
  "confidence": 0.0-1.0
}"#
    }
}

/// Prompt template for FORK intervention point.
///
/// Used when multiple candidate branches are available to:
/// - Rank candidates by relevance
/// - Recommend search direction
/// - Provide reasoning
#[derive(Debug, Clone)]
pub struct ForkPrompt {
    system: String,
    template: String,
}

impl Default for ForkPrompt {
    fn default() -> Self {
        Self::with_fallback()
    }
}

impl ForkPrompt {
    /// Create a new fork prompt template.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with custom templates.
    pub fn with_templates(system: String, template: String) -> Self {
        Self { system, template }
    }
}

impl PromptTemplate for ForkPrompt {
    fn system_prompt(&self) -> &str {
        &self.system
    }

    fn user_prompt_template(&self) -> &str {
        &self.template
    }

    fn intervention_point(&self) -> InterventionPoint {
        InterventionPoint::Fork
    }

    fn output_format_hint(&self) -> &str {
        r#"{
  "ranked_candidates": [
    {"index": 0, "score": 0.9, "reason": "why this candidate"}
  ],
  "direction": "go_deeper|explore_siblings|backtrack|found_answer",
  "confidence": 0.0-1.0,
  "reasoning": "overall explanation"
}"#
    }
}

/// Prompt template for BACKTRACK intervention point.
///
/// Used when search needs to recover from a dead end to:
/// - Analyze failure reason
/// - Suggest alternative branches
/// - Guide recovery strategy
#[derive(Debug, Clone)]
pub struct BacktrackPrompt {
    system: String,
    template: String,
}

impl Default for BacktrackPrompt {
    fn default() -> Self {
        Self::with_fallback()
    }
}

impl BacktrackPrompt {
    /// Create a new backtrack prompt template.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with custom templates.
    pub fn with_templates(system: String, template: String) -> Self {
        Self { system, template }
    }
}

impl PromptTemplate for BacktrackPrompt {
    fn system_prompt(&self) -> &str {
        &self.system
    }

    fn user_prompt_template(&self) -> &str {
        &self.template
    }

    fn intervention_point(&self) -> InterventionPoint {
        InterventionPoint::Backtrack
    }

    fn output_format_hint(&self) -> &str {
        r#"{
  "alternative_branches": [
    {"index": 0, "score": 0.8, "reason": "why this alternative"}
  ],
  "direction": "backtrack",
  "confidence": 0.0-1.0,
  "reasoning": "why the original path failed and alternatives chosen"
}"#
    }
}

/// Prompt template for EVALUATE intervention point.
///
/// Used to assess if a node contains the answer to:
/// - Determine relevance score
/// - Check if answer is found
/// - Guide further search
#[derive(Debug, Clone)]
pub struct EvaluatePrompt {
    system: String,
    template: String,
}

impl Default for EvaluatePrompt {
    fn default() -> Self {
        Self::with_fallback()
    }
}

impl EvaluatePrompt {
    /// Create a new evaluate prompt template.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with custom templates.
    pub fn with_templates(system: String, template: String) -> Self {
        Self { system, template }
    }
}

impl PromptTemplate for EvaluatePrompt {
    fn system_prompt(&self) -> &str {
        &self.system
    }

    fn user_prompt_template(&self) -> &str {
        &self.template
    }

    fn intervention_point(&self) -> InterventionPoint {
        InterventionPoint::Evaluate
    }

    fn output_format_hint(&self) -> &str {
        r#"{
  "relevance_score": 0.0-1.0,
  "is_answer": true|false,
  "direction": "go_deeper|found_answer",
  "confidence": 0.0-1.0,
  "reasoning": "why this node is or isn't the answer"
}"#
    }
}

/// Fallback templates when file loading fails.
pub mod fallback {
    pub fn system_start() -> String {
        r#"You are a document navigation assistant. Help identify the best entry points for searching a hierarchical document.

CRITICAL: You MUST respond with ONLY valid JSON. No markdown code blocks, No explanation. Just the JSON object.

Your response must have this EXACT structure:
{
  "entry_points": ["Title 1", "Title 2"],
  "reasoning": "Brief explanation",
  "confidence": 0.85
}

Rules:
- entry_points: Array of node title strings (from the candidates provided)
- reasoning: String explaining your choice
- confidence: Number between 0.0 and 1.0 (use a number, NOT "high"/"medium"/"low")"#.to_string()
    }

    pub fn user_start() -> String {
        r#"{context}

Respond with ONLY the JSON object (no markdown, no explanation):
{
  "entry_points": ["list of node titles as strings"],
  "reasoning": "your reasoning here",
  "confidence": 0.85
}"#
        .to_string()
    }

    pub fn system_fork() -> String {
        r#"You are a document navigation assistant. At each decision point, rank the candidate branches by their likelihood of containing the answer to the user's query.

CRITICAL: You MUST respond with ONLY valid JSON. No markdown code blocks.

Your response must have this EXACT structure:
{
  "ranked_candidates": [
    {"index": 0, "score": 0.9, "reason": "explanation"}
  ],
  "direction": "go_deeper",
  "confidence": 0.85,
  "reasoning": "overall explanation"
}

Rules:
- ranked_candidates: Array of objects with index (number), score (0.0-1.0), reason (string)
- direction: One of "go_deeper", "explore_siblings", "backtrack", "found_answer"
- confidence: Number between 0.0 and 1.0 (NOT a string)"#.to_string()
    }

    pub fn user_fork() -> String {
        r#"{context}

Respond with ONLY the JSON object:
{
  "ranked_candidates": [
    {"index": 0, "score": 0.9, "reason": "why this candidate"}
  ],
  "direction": "go_deeper",
  "confidence": 0.85,
  "reasoning": "overall explanation"
}"#
        .to_string()
    }

    pub fn system_backtrack() -> String {
        r#"You are a document navigation assistant. When a search path fails to find the answer, analyze why and suggest alternative branches to explore.

CRITICAL: You MUST respond with ONLY valid JSON. No markdown code blocks.

Your response must have this EXACT structure:
{
  "alternative_branches": [
    {"index": 0, "score": 0.8, "reason": "explanation"}
  ],
  "direction": "backtrack",
  "confidence": 0.85,
  "reasoning": "why the original path failed"
}"#.to_string()
    }

    pub fn user_backtrack() -> String {
        r#"{context}

Respond with ONLY the JSON object:
{
  "alternative_branches": [
    {"index": 0, "score": 0.8, "reason": "why this alternative"}
  ],
  "direction": "backtrack",
  "confidence": 0.85,
  "reasoning": "why original path failed"
}"#
        .to_string()
    }

    pub fn system_evaluate() -> String {
        r#"You are a document analysis assistant. Evaluate whether the current node contains the answer to the user's query.

CRITICAL: You MUST respond with ONLY valid JSON. No markdown code blocks.

Your response must have this EXACT structure:
{
  "relevance_score": 0.85,
  "is_answer": false,
  "direction": "go_deeper",
  "confidence": 0.85,
  "reasoning": "explanation"
}"#.to_string()
    }

    pub fn user_evaluate() -> String {
        r#"{context}

Respond with ONLY the JSON object:
{
  "relevance_score": 0.85,
  "is_answer": false,
  "direction": "go_deeper",
  "confidence": 0.85,
  "reasoning": "explanation"
}"#
        .to_string()
    }

    pub fn system_locate_top3() -> String {
        r#"You are a document navigation assistant. Your task is to locate the most relevant sections in a document hierarchy for a user's query.

CRITICAL INSTRUCTIONS:
1. Analyze the user query carefully to understand the intent
2. Examine the provided Table of Contents (TOC) with node IDs
3. Select the TOP 3 most relevant nodes that would contain the answer
4. You MUST respond with ONLY valid JSON. No markdown code blocks. No explanations outside JSON.

Your response must have this EXACT structure:
{
  "reasoning": "Brief analysis of the query and why you selected these nodes",
  "candidates": [
    {"node_id": <number_from_toc>, "relevance_score": 0.95, "reason": "Why this node matches the query"},
    {"node_id": <number_from_toc>, "relevance_score": 0.80, "reason": "Why this node is also relevant"},
    {"node_id": <number_from_toc>, "relevance_score": 0.65, "reason": "Why this node might be relevant"}
  ]
}

Rules:
- node_id: MUST be a number from the provided TOC (copy exactly)
- relevance_score: Number between 0.0 and 1.0 (higher = more relevant)
- reason: Brief explanation for each selection
- candidates: Must have exactly 3 items, ordered by relevance (highest first)
- If fewer than 3 relevant nodes exist, use lower scores for less relevant ones"#.to_string()
    }

    pub fn user_locate_top3() -> String {
        r#"{context}

Based on the query and TOC above, select the TOP 3 most relevant nodes.

Respond with ONLY the JSON object:
{
  "reasoning": "Your analysis here",
  "candidates": [
    {"node_id": 1, "relevance_score": 0.95, "reason": "explanation"},
    {"node_id": 2, "relevance_score": 0.80, "reason": "explanation"},
    {"node_id": 3, "relevance_score": 0.65, "reason": "explanation"}
  ]
}"#
        .to_string()
    }
}

impl StartPrompt {
    /// Get template with fallback.
    pub fn with_fallback() -> Self {
        Self {
            system: fallback::system_start(),
            template: fallback::user_start(),
        }
    }
}

impl ForkPrompt {
    /// Get template with fallback.
    pub fn with_fallback() -> Self {
        Self {
            system: fallback::system_fork(),
            template: fallback::user_fork(),
        }
    }
}

impl BacktrackPrompt {
    /// Get template with fallback.
    pub fn with_fallback() -> Self {
        Self {
            system: fallback::system_backtrack(),
            template: fallback::user_backtrack(),
        }
    }
}

impl EvaluatePrompt {
    /// Get template with fallback.
    pub fn with_fallback() -> Self {
        Self {
            system: fallback::system_evaluate(),
            template: fallback::user_evaluate(),
        }
    }
}

impl LocateTop3Prompt {
    /// Get template with fallback.
    pub fn with_fallback() -> Self {
        Self {
            system: fallback::system_locate_top3(),
            template: fallback::user_locate_top3(),
        }
    }
}

/// Prompt template for LOCATE_TOP3 intervention point.
///
/// Used at the start to directly locate top-3 relevant nodes from TOC:
/// - Understand query intent
/// - Identify top 3 most relevant nodes with confidence scores
/// - Provide reasoning for each selection
#[derive(Debug, Clone)]
pub struct LocateTop3Prompt {
    system: String,
    template: String,
}

impl Default for LocateTop3Prompt {
    fn default() -> Self {
        Self::with_fallback()
    }
}

impl LocateTop3Prompt {
    /// Create a new locate top-3 prompt template.
    pub fn new() -> Self {
        Self::default()
    }

    /// Create with custom templates.
    pub fn with_templates(system: String, template: String) -> Self {
        Self { system, template }
    }
}

impl PromptTemplate for LocateTop3Prompt {
    fn system_prompt(&self) -> &str {
        &self.system
    }

    fn user_prompt_template(&self) -> &str {
        &self.template
    }

    fn intervention_point(&self) -> InterventionPoint {
        InterventionPoint::Start
    }

    fn output_format_hint(&self) -> &str {
        r#"{
  "reasoning": "Overall analysis of the query and document structure",
  "candidates": [
    {"node_id": 1, "relevance_score": 0.95, "reason": "Why this node is relevant"},
    {"node_id": 2, "relevance_score": 0.80, "reason": "Why this node is relevant"},
    {"node_id": 3, "relevance_score": 0.65, "reason": "Why this node is relevant"}
  ]
}"#
    }
}