Utilities and Support API¶
HoloDeck provides a rich set of utility modules for template rendering, file processing, search, chunking, context generation, tool initialization, logging, validation, and more.
Template Engine¶
Jinja2-based template rendering with restricted filters and YAML validation against the AgentConfig schema. Used by holodeck init to scaffold projects from built-in templates.
TemplateRenderer()
¶
Renders Jinja2 templates and validates output against schemas.
Provides safe template rendering with: - Restricted Jinja2 filters for security - YAML validation against AgentConfig schema - Clear error messages for debugging
Initialize the TemplateRenderer with a secure Jinja2 environment.
Source code in src/holodeck/lib/template_engine.py
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | |
get_available_templates()
staticmethod
¶
Get available templates with metadata.
Discovers templates from the templates/ directory and extracts metadata (name, display_name, description) from their manifest.yaml files.
Returns:
| Type | Description |
|---|---|
list[dict[str, str]]
|
List of dicts with 'value', 'display_name', 'description' keys. |
list[dict[str, str]]
|
Returns empty list if templates directory doesn't exist. |
Source code in src/holodeck/lib/template_engine.py
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 | |
list_available_templates()
staticmethod
¶
List all available built-in templates.
Discovers templates from the templates/ directory structure.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of template names (e.g., ['conversational', 'research', |
list[str]
|
'customer-support']) |
Source code in src/holodeck/lib/template_engine.py
225 226 227 228 229 230 231 232 233 234 235 | |
render_and_validate(template_path, variables)
¶
Render a Jinja2 template and validate output (for YAML files).
Combines rendering and validation in a safe way: only returns rendered content if both rendering and validation succeed. This is the recommended way to process agent.yaml templates.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template_path
|
str
|
Path to the Jinja2 template file |
required |
variables
|
dict[str, Any]
|
Dictionary of variables to pass to the template |
required |
Returns:
| Type | Description |
|---|---|
str
|
Rendered and validated template content as a string |
Raises:
| Type | Description |
|---|---|
InitError
|
If rendering fails |
ValidationError
|
If validation fails |
Source code in src/holodeck/lib/template_engine.py
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 | |
render_template(template_path, variables)
¶
Render a Jinja2 template with provided variables.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
template_path
|
str
|
Path to the Jinja2 template file |
required |
variables
|
dict[str, Any]
|
Dictionary of variables to pass to the template |
required |
Returns:
| Type | Description |
|---|---|
str
|
Rendered template content as a string |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If template file doesn't exist |
InitError
|
If rendering fails (syntax errors, undefined variables, etc.) |
Source code in src/holodeck/lib/template_engine.py
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 | |
validate_agent_config(yaml_content)
¶
Validate YAML content against Agent schema.
Parses YAML and validates it against the Agent Pydantic model. This is the critical validation gate for agent.yaml files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
yaml_content
|
str
|
YAML content as a string |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Agent |
Agent
|
Validated Agent configuration object |
Raises:
| Type | Description |
|---|---|
ValidationError
|
If YAML is invalid or doesn't match schema |
InitError
|
If parsing fails |
Source code in src/holodeck/lib/template_engine.py
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 | |
File Processing¶
Multimodal file processor that converts Office documents, PDFs, images (OCR), CSV, and JSON into markdown for LLM consumption. Supports local and remote files with caching, page/sheet/range extraction, and configurable timeouts.
SourceFile(path, content='', mtime=0.0, size_bytes=0, file_type='', chunks=list())
dataclass
¶
Source file to be ingested into vector store.
Represents a file during the ingestion process with metadata and content.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
Path
|
Absolute file path |
content |
str
|
File content converted to markdown (populated by FileProcessor) |
mtime |
float
|
File modification time (Unix timestamp) |
size_bytes |
int
|
File size in bytes |
file_type |
str
|
File extension (.txt, .md, .pdf, .csv, .json, etc.) |
chunks |
list[str]
|
Text chunks after splitting (populated by TextChunker) |
FileProcessor(cache_dir=None, download_timeout_ms=30000, max_retries=3, processing_timeout_ms=30000, read_only=False)
¶
Process files with markitdown for multimodal test inputs.
Initialize file processor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cache_dir
|
str | None
|
Directory for caching remote files. Defaults to .holodeck/cache/ |
None
|
download_timeout_ms
|
int
|
Timeout for file downloads in milliseconds |
30000
|
max_retries
|
int
|
Maximum number of retry attempts for downloads |
3
|
processing_timeout_ms
|
int
|
Timeout for file processing in milliseconds. Defaults to 30000ms. |
30000
|
read_only
|
bool
|
Convert local files only: create no cache directory,
write no cache file and download nothing. |
False
|
Source code in src/holodeck/lib/file_processor.py
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 | |
from_execution_config(config, cache_dir=None, max_retries=3, read_only=False)
classmethod
¶
Create FileProcessor from ExecutionConfig.
Factory method that handles conversion from ExecutionConfig's seconds-based timeouts to FileProcessor's milliseconds-based timeouts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ExecutionConfig
|
ExecutionConfig with timeout settings in seconds |
required |
cache_dir
|
str | None
|
Override cache directory (defaults to config.cache_dir) |
None
|
max_retries
|
int
|
Maximum retry attempts for downloads |
3
|
read_only
|
bool
|
See |
False
|
Returns:
| Type | Description |
|---|---|
FileProcessor
|
Configured FileProcessor instance |
Source code in src/holodeck/lib/file_processor.py
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 | |
process_file(file_input)
¶
Process a single file input to markdown.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_input
|
FileInput
|
File input configuration with path or URL |
required |
Returns:
| Type | Description |
|---|---|
ProcessedFileInput
|
ProcessedFileInput with markdown content and metadata |
Source code in src/holodeck/lib/file_processor.py
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 | |
Search¶
Hybrid search combining semantic (vector) similarity with keyword (full-text) matching via Reciprocal Rank Fusion (RRF). Includes tiered strategy selection based on vector store provider capabilities.
Hybrid Search¶
SearchResult(chunk_id, content, fused_score, source_path, parent_chain, section_id, subsection_ids=list(), semantic_score=None, keyword_score=None, exact_match=False, definitions_context=list())
dataclass
¶
A single result from hybrid search.
Represents a search result that may include scores from both semantic (vector) and keyword (full-text) search, along with document structure metadata and related definitions.
Attributes:
| Name | Type | Description |
|---|---|---|
chunk_id |
str
|
Unique identifier of the matched chunk |
content |
str
|
The text content of the matched chunk |
fused_score |
float
|
Combined score from semantic and keyword search (0.0-1.0) |
source_path |
str
|
Path to the source document file |
parent_chain |
list[str]
|
List of ancestor headings from root to immediate parent |
section_id |
str
|
Document section identifier (e.g., "1.2.3") |
subsection_ids |
list[str]
|
List of inline subsection IDs contained in this chunk |
semantic_score |
float | None
|
Score from semantic/vector similarity search (optional) |
keyword_score |
float | None
|
Score from keyword/full-text search (optional) |
exact_match |
bool
|
Whether this result contains an exact phrase match |
definitions_context |
list[DefinitionEntry]
|
Related definitions for terms found in the content |
Example
result = SearchResult( ... chunk_id="policy_md_chunk_5", ... content="Force Majeure means any event...", ... fused_score=0.92, ... source_path="/docs/policy.md", ... parent_chain=["Chapter 1", "Definitions"], ... section_id="1.2", ... subsection_ids=["subsec_a_findings", "para_1_access"], ... semantic_score=0.88, ... keyword_score=0.95, ... exact_match=True, ... ) print(result.format())
format()
¶
Format result for agent consumption.
Produces a human-readable representation of the search result suitable for inclusion in agent context or display to users.
Returns:
| Type | Description |
|---|---|
str
|
Formatted string with score, source, location, content, |
str
|
and any relevant definitions. |
Example
result = SearchResult( ... chunk_id="doc_0", ... content="Hello world", ... fused_score=0.85, ... source_path="/doc.md", ... parent_chain=["Chapter 1"], ... section_id="1.1", ... ) print(result.format()) Score: 0.850 | Source: /doc.md Location: Chapter 1 Section: 1.1
Hello world
Source code in src/holodeck/lib/hybrid_search.py
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 | |
reciprocal_rank_fusion(ranked_lists, k=60, weights=None)
¶
Merge multiple ranked lists using Reciprocal Rank Fusion (RRF).
RRF combines results from different retrieval systems by scoring each document based on its rank in each list: score(d) = Σ weight_i / (k + rank_i(d))
This approach is robust to different score distributions across retrieval systems and doesn't require score calibration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ranked_lists
|
list[list[tuple[str, float]]]
|
List of ranked result lists, each containing (doc_id, score) tuples sorted by relevance descending. |
required |
k
|
int
|
RRF constant (default 60). Higher values give more weight to lower-ranked results, reducing the impact of rank position. |
60
|
weights
|
list[float] | None
|
Optional weights for each list (default equal weights). Use to prioritize certain retrieval modalities. |
None
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, float]]
|
Merged list of (doc_id, score) tuples sorted by RRF score. |
list[tuple[str, float]]
|
Scores are normalized to 0-1 range based on maximum possible score. |
Example
semantic = [("a", 0.9), ("b", 0.8), ("c", 0.7)] keyword = [("b", 0.95), ("a", 0.85), ("d", 0.75)] fused = reciprocal_rank_fusion([semantic, keyword], k=60) print(fused[0]) # Most relevant document ('b', 0.032...)
Source code in src/holodeck/lib/hybrid_search.py
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 | |
Keyword Search¶
KeywordSearchStrategy
¶
Bases: str, Enum
Keyword search strategy based on provider capabilities.
Determines whether to use native hybrid search or BM25 fallback: - NATIVE_HYBRID: Provider supports hybrid_search() API directly - FALLBACK_BM25: Use rank_bm25 + app-level RRF fusion
The executor selects the strategy from adapter.capabilities.
KeywordDocument
¶
Bases: _KeywordDocumentRequired
Structured document for multi-field keyword indexing.
Contains the content and metadata fields extracted from DocumentChunk that are relevant for keyword-based retrieval. Fields are used for multi-field indexing with per-field boosting.
Required Attributes
id: Unique chunk identifier. content: Primary text content (contextualized_content or content fallback).
Optional Attributes
parent_chain: Ancestor heading chain joined with " > " (e.g., "Chapter 1 > Definitions"). section_id: Document section identifier (e.g., "1.2.3", "203(a)"). defined_term: The term being defined (if chunk_type is definition). chunk_type: Classification of content type (content, definition, requirement, etc.). source_file: Source filename extracted from source_path.
KeywordSearchProvider
¶
Bases: Protocol
Protocol for keyword search backends.
Defines the interface that all keyword search providers must implement. Uses structural subtyping (Protocol) so providers satisfy the interface via duck typing without explicit inheritance.
Methods:
| Name | Description |
|---|---|
build |
Index documents for keyword search. |
search |
Search indexed documents and return ranked results. |
build(documents)
¶
Build keyword index from documents.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
list[KeywordDocument]
|
List of KeywordDocument dicts with structured fields. |
required |
Source code in src/holodeck/lib/keyword_search.py
253 254 255 256 257 258 259 | |
search(query, top_k=10)
¶
Search indexed documents for matching results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Search query string. |
required |
top_k
|
int
|
Maximum number of results to return. |
10
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, float]]
|
List of (doc_id, score) tuples sorted by score descending. |
Source code in src/holodeck/lib/keyword_search.py
261 262 263 264 265 266 267 268 269 270 271 | |
InMemoryBM25KeywordProvider(k1=1.5, b=0.75)
¶
In-memory BM25 keyword search provider with multi-field indexing.
Provides keyword-based search using the BM25Okapi algorithm from the rank_bm25 library. Indexes multiple document fields (content, parent_chain, section_id, defined_term, source_file) with implicit boosting via field repetition.
Attributes:
| Name | Type | Description |
|---|---|---|
k1 |
BM25 term frequency saturation parameter (default 1.5) |
|
b |
BM25 length normalization parameter (default 0.75) |
Example
provider = InMemoryBM25KeywordProvider() provider.build([ ... KeywordDocument(id="doc1", content="The quick brown fox"), ... KeywordDocument(id="doc2", content="The lazy dog"), ... ]) results = provider.search("brown fox", top_k=2) print(results[0]) # (doc_id, score) tuple ('doc1', 1.234)
Initialize the BM25 provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
k1
|
float
|
BM25 k1 parameter for term frequency saturation. Higher values give more weight to term frequency. |
1.5
|
b
|
float
|
BM25 b parameter for document length normalization. Higher values penalize longer documents more. |
0.75
|
Source code in src/holodeck/lib/keyword_search.py
297 298 299 300 301 302 303 304 305 306 307 308 309 | |
build(documents)
¶
Build BM25 index from structured keyword documents.
Builds a composite text from each KeywordDocument's fields with implicit boosting (defined_term 3x, parent_chain 2x, section_id 2x, content 1x, source_file 1x), then indexes via BM25Okapi.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
list[KeywordDocument]
|
List of KeywordDocument dicts with structured fields. |
required |
Raises:
| Type | Description |
|---|---|
ImportError
|
If rank_bm25 is not installed. |
Example
provider.build([ ... KeywordDocument( ... id="chunk1", ... content="Force Majeure means any event...", ... parent_chain="Chapter 1 > Definitions", ... section_id="1.2", ... defined_term="Force Majeure", ... ), ... ])
Source code in src/holodeck/lib/keyword_search.py
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 | |
search(query, top_k=10)
¶
Search indexed documents for matching results.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Search query string |
required |
top_k
|
int
|
Maximum number of results to return |
10
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, float]]
|
List of (doc_id, score) tuples sorted by BM25 score descending. |
list[tuple[str, float]]
|
Returns empty list if index not built. |
Example
results = provider.search("brown fox", top_k=5) [('doc1', 1.234), ('doc3', 0.567)]
Source code in src/holodeck/lib/keyword_search.py
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 | |
OpenSearchKeywordProvider(endpoint, index_name, username=None, password=None, api_key=None, verify_certs=True, timeout_seconds=10)
¶
OpenSearch-backed keyword search provider with multi-field indexing.
Uses the opensearch-py low-level client to index and search documents via BM25 scoring on an external OpenSearch cluster. Implements the KeywordSearchProvider protocol.
Indexes multiple fields from KeywordDocument with per-field boosting: - content: Primary text (standard analyzer) - parent_chain: Heading hierarchy (standard analyzer, 2x boost) - section_id: Section identifiers (simple analyzer, 2x boost) - defined_term: Definition terms (standard analyzer, 3x boost) - chunk_type: Content classification (keyword type, filterable) - source_file: Source filename (simple analyzer)
Attributes:
| Name | Type | Description |
|---|---|---|
endpoint |
OpenSearch endpoint URL. |
|
index_name |
Name of the OpenSearch index. |
|
verify_certs |
Whether to verify TLS certificates. |
|
timeout_seconds |
Connection timeout in seconds. |
Example
provider = OpenSearchKeywordProvider( ... endpoint="https://search.example.com:9200", ... index_name="my-index", ... username="admin", ... password="secret", ... ) provider.build([KeywordDocument(id="doc1", content="The quick brown fox")]) results = provider.search("brown fox", top_k=5) print(results[0]) # (chunk_id, score) tuple ('doc1', 3.456)
Initialize the OpenSearch keyword provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
endpoint
|
str
|
OpenSearch endpoint URL (e.g. "https://host:9200"). |
required |
index_name
|
str
|
Name of the index to create/use. |
required |
username
|
str | None
|
Basic auth username (used with password). |
None
|
password
|
str | None
|
Basic auth password (used with username). |
None
|
api_key
|
str | None
|
API key for authentication (alternative to basic auth). |
None
|
verify_certs
|
bool
|
Whether to verify TLS certificates. |
True
|
timeout_seconds
|
int
|
Connection timeout in seconds. |
10
|
Raises:
| Type | Description |
|---|---|
MissingExtraError
|
opensearch-py (the |
Source code in src/holodeck/lib/keyword_search.py
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 523 524 525 526 | |
build(documents)
¶
Build OpenSearch index from structured keyword documents.
Creates the index if it does not exist. If the index already exists, clears all existing documents before re-indexing. Uses bulk indexing with refresh=True for immediate searchability.
Each KeywordDocument's fields are indexed into separate OpenSearch fields with per-field boosting applied at query time via multi_match.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
documents
|
list[KeywordDocument]
|
List of KeywordDocument dicts with structured fields. |
required |
Raises:
| Type | Description |
|---|---|
OpenSearchException
|
On connection or indexing errors. |
Source code in src/holodeck/lib/keyword_search.py
528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 | |
search(query, top_k=10)
¶
Search indexed documents using multi-field BM25 scoring.
Uses a multi_match query across all indexed fields with per-field boost factors (defined_term^3, parent_chain^2, section_id^2, content, source_file).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Search query string. |
required |
top_k
|
int
|
Maximum number of results to return. |
10
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, float]]
|
List of (chunk_id, score) tuples sorted by BM25 score descending. |
list[tuple[str, float]]
|
Returns empty list if the index does not exist. |
Raises:
| Type | Description |
|---|---|
OpenSearchException
|
On connection errors. |
Source code in src/holodeck/lib/keyword_search.py
594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 | |
HybridSearchExecutor(adapter, semantic_weight=0.5, keyword_weight=0.3, rrf_k=60, keyword_index_config=None, chunk_decoder=None)
¶
Executes hybrid search using appropriate strategy for provider.
Routes search requests to either native hybrid search or BM25 fallback based on the provider's capabilities.
Attributes:
| Name | Type | Description |
|---|---|---|
provider |
Vector store provider name |
|
adapter |
The vector store adapter the tool owns |
|
strategy |
Determined keyword search strategy |
Example
executor = HybridSearchExecutor(adapter) executor.build_keyword_index(documents) # Optional for fallback results = await executor.search(query, embedding, top_k=10)
Initialize the hybrid search executor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
adapter
|
VectorStoreAdapter
|
Vector store adapter. Its capabilities decide the
strategy: native hybrid when it supports
|
required |
semantic_weight
|
float
|
Weight for semantic results in RRF fusion (default 0.5) |
0.5
|
keyword_weight
|
float
|
Weight for keyword results in RRF fusion (default 0.3) |
0.3
|
rrf_k
|
int
|
RRF ranking constant (default 60) |
60
|
keyword_index_config
|
KeywordIndexConfig | None
|
Keyword index backend configuration. If None, defaults to in-memory BM25. |
None
|
chunk_decoder
|
Callable[[Record], DocumentChunk] | None
|
Optional callback to reconstruct a
|
None
|
Source code in src/holodeck/lib/keyword_search.py
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 | |
build_keyword_index(chunks)
async
¶
Build keyword index with graceful degradation.
Creates the keyword search index from the provided chunks and stores a chunk map for ID-based lookups. Routes to the appropriate backend based on keyword_index_config: - 'opensearch': OpenSearchKeywordProvider (I/O offloaded via asyncio.to_thread) - 'in-memory' or None: InMemoryBM25KeywordProvider (called directly)
Each chunk is converted to a KeywordDocument with multiple fields (content, parent_chain, section_id, defined_term, source_file) for multi-field indexing with per-field boosting.
If index build fails, logs a warning and continues with semantic-only search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunks
|
list[DocumentChunk]
|
List of DocumentChunk objects. All relevant fields (content, parent_chain, section_id, defined_term, etc.) are indexed for keyword search. |
required |
Example
await executor.build_keyword_index(chunks)
Source code in src/holodeck/lib/keyword_search.py
720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 | |
get_chunk(chunk_id)
¶
Look up a chunk by ID from the stored chunk map.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_id
|
str
|
The unique identifier of the chunk. |
required |
Returns:
| Type | Description |
|---|---|
DocumentChunk | None
|
The DocumentChunk if found, or None. |
Source code in src/holodeck/lib/keyword_search.py
821 822 823 824 825 826 827 828 829 830 | |
keyword_search(query, top_k=10)
async
¶
Perform keyword-only search.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Search query string. |
required |
top_k
|
int
|
Maximum number of results to return. |
10
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, float]]
|
List of (chunk_id, score) tuples sorted by score descending. |
list[tuple[str, float]]
|
Returns empty list if keyword index is not built or on search error. |
Source code in src/holodeck/lib/keyword_search.py
832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 | |
search(query, query_embedding, top_k=10)
async
¶
Execute hybrid search and return ranked results.
Routes to native hybrid search or BM25 fallback based on provider.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
query
|
str
|
Search query string |
required |
query_embedding
|
list[float]
|
Pre-computed query embedding vector |
required |
top_k
|
int
|
Maximum number of results to return |
10
|
Returns:
| Type | Description |
|---|---|
list[tuple[str, float]]
|
List of (chunk_id, score) tuples sorted by relevance. |
list[tuple[str, float]]
|
Scores are normalized to 0-1 range. |
Example
results = await executor.search( ... "reporting requirements", ... [0.1, 0.2, ...], ... top_k=10 ... ) [('chunk_report', 0.95), ('chunk_other', 0.72), ...]
Source code in src/holodeck/lib/keyword_search.py
856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 | |
Chunking¶
Text splitting and structure-aware chunking for preparing documents for embedding generation and hierarchical retrieval.
Text Chunker¶
A token-based paragraph splitter.
TextChunker(chunk_size=DEFAULT_CHUNK_SIZE, chunk_overlap=DEFAULT_CHUNK_OVERLAP, separator_list=None)
¶
Split plain text into token-bounded, optionally overlapping chunks.
Attributes:
| Name | Type | Description |
|---|---|---|
chunk_size |
Maximum number of tokens in a chunk (cl100k_base) |
|
chunk_overlap |
Tokens repeated from the end of the previous chunk |
|
separator_list |
Boundary order used (fixed: paragraph, line, word) |
Initialize text chunker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_size
|
int
|
Maximum tokens per chunk (default: 512) |
DEFAULT_CHUNK_SIZE
|
chunk_overlap
|
int
|
Tokens repeated at the start of the next chunk (default: 50) |
DEFAULT_CHUNK_OVERLAP
|
separator_list
|
list[str] | None
|
Must be None or the default list. The splitter uses a fixed boundary order, so other values are rejected. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If chunk_size <= 0, chunk_overlap < 0, chunk_overlap >= chunk_size, or separator_list is not the default. |
Source code in src/holodeck/lib/text_chunker.py
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 | |
split_text(text)
¶
Split text into chunks of at most chunk_size tokens.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to split into chunks |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
Non-empty chunks without leading or trailing whitespace. Each chunk |
list[str]
|
after the first starts with up to chunk_overlap tokens copied from |
list[str]
|
the end of the previous chunk. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If text is empty, or a single character needs more tokens than chunk_size (the message names the minimum size). |
Source code in src/holodeck/lib/text_chunker.py
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 | |
Structured Chunker¶
Structure-aware markdown parsing with heading hierarchy extraction, chunk type classification, and parent chain building.
SubsectionPattern(name, pattern, level, extract_title=True)
dataclass
¶
Configurable pattern for implicit subsection detection in documents.
Used to recognize legislative-style numbering schemes (e.g., (a), (1), (A)) as implicit headings that should create separate chunks with proper hierarchy.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Human-readable name for the pattern (e.g., "subsection", "paragraph") |
pattern |
Pattern[str]
|
Compiled regex to match the marker. Should have groups for: - Group 1: The marker itself (e.g., "a", "1", "A") - Group 2: Optional text after the marker (title/content) |
level |
int
|
Heading level to assign (3-6, since 1-2 are reserved for markdown) |
extract_title |
bool
|
Whether to include text after marker as the heading title |
Example
pattern = SubsectionPattern( ... name="subsection", ... pattern=re.compile(r"^(([a-z]))\s(.)$", re.MULTILINE), ... level=3, ... extract_title=True, ... )
ChunkType
¶
Bases: str, Enum
Classification of chunk content type.
Used to categorize document chunks for specialized handling during search and retrieval operations.
Attributes:
| Name | Type | Description |
|---|---|---|
CONTENT |
Regular document content (paragraphs, lists, etc.) |
|
DEFINITION |
Term definitions (e.g., glossary entries, defined terms) |
|
REQUIREMENT |
Requirements or obligations (e.g., "shall", "must") |
|
REFERENCE |
Cross-references or citations to other sections |
|
HEADER |
Section headers or titles |
DocumentChunk(id, source_path, chunk_index, content, parent_chain=list(), section_id='', chunk_type=ChunkType.CONTENT, cross_references=list(), heading_level=0, embedding=None, contextualized_content='', mtime=0.0, defined_term='', defined_term_normalized='', subsection_ids=list())
dataclass
¶
A parsed section of a document with structure metadata.
Represents a single chunk of a document that preserves its position in the document hierarchy, cross-references to other sections, and optional definition information.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique identifier for this chunk (typically source_path + chunk_index) |
source_path |
str
|
Path to the source document file |
chunk_index |
int
|
Zero-based index of this chunk within the document |
content |
str
|
The actual text content of the chunk |
parent_chain |
list[str]
|
List of ancestor heading texts from root to immediate parent |
section_id |
str
|
Document section identifier (e.g., "1.2.3", "A.1") |
chunk_type |
ChunkType
|
Classification of the content type |
cross_references |
list[str]
|
List of section IDs referenced by this chunk |
heading_level |
int
|
Heading level if this is a header chunk (1-6, 0 for non-headers) |
embedding |
list[float] | None
|
Optional vector embedding for semantic search |
contextualized_content |
str
|
Content with added context for better retrieval |
mtime |
float
|
File modification time (Unix timestamp) for change detection |
defined_term |
str
|
The term being defined (if chunk_type is DEFINITION) |
defined_term_normalized |
str
|
Lowercase normalized term for case-insensitive lookup |
Example
chunk = DocumentChunk( ... id="policy_md_chunk_5", ... source_path="/docs/policy.md", ... chunk_index=5, ... content="Force Majeure means any event beyond...", ... parent_chain=["Chapter 1", "Definitions"], ... section_id="1.2", ... chunk_type=ChunkType.DEFINITION, ... defined_term="Force Majeure", ... defined_term_normalized="force majeure", ... )
to_record_dict()
¶
Convert to dict for vector store record creation.
Serializes list fields as JSON strings for storage in vector databases that expect flat field structures.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with all fields serialized for vector store insertion. |
dict[str, Any]
|
List fields (parent_chain, cross_references) are JSON-encoded. |
Example
chunk = DocumentChunk( ... id="doc_0", ... source_path="/doc.md", ... chunk_index=0, ... content="Hello", ... parent_chain=["Ch1", "Sec1"], ... ) record = chunk.to_record_dict() record["parent_chain"] '["Ch1", "Sec1"]'
Source code in src/holodeck/lib/structured_chunker.py
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 | |
StructuredChunker(max_tokens=DEFAULT_MAX_TOKENS, subsection_patterns=None, max_subsection_depth=None, split_on_level=None)
¶
Structure-aware markdown chunker with hierarchy preservation.
Parses markdown documents into chunks while preserving: - Parent chain (heading hierarchy for navigation context) - Section IDs (normalized identifiers) - Chunk type classification (content, definition, requirement, etc.) - Token-bounded sections with sentence-aware splitting
The chunker follows Anthropic's contextual retrieval baseline with a default max_tokens of 800 per chunk.
Attributes:
| Name | Type | Description |
|---|---|---|
max_tokens |
int
|
Maximum tokens per chunk (default 800). |
Example
chunker = StructuredChunker(max_tokens=800) chunks = chunker.parse(markdown_content, "document.md") for chunk in chunks: ... print(f"{chunk.section_id}: {chunk.parent_chain}")
Initialize the structured chunker.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_tokens
|
int
|
Maximum tokens per chunk. Defaults to 800. |
DEFAULT_MAX_TOKENS
|
subsection_patterns
|
list[SubsectionPattern] | None
|
Optional list of SubsectionPattern for detecting implicit headings (e.g., legislative numbering like (a), (1)). When provided, enables enhanced hierarchy extraction. |
None
|
max_subsection_depth
|
int | None
|
Maximum number of subsection levels to recognize. If None (default), uses all patterns in subsection_patterns. Must be between 1 and len(subsection_patterns) if specified. |
None
|
split_on_level
|
int | None
|
Heading levels <= this value create new chunks. Levels > this value are accumulated into parent chunks with their markers tracked in subsection_ids. If None (default): - With subsection_patterns: defaults to len(patterns) // 2 - Without patterns: defaults to 6 (split on all markdown levels) |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If max_tokens is not positive. |
ValueError
|
If max_subsection_depth is invalid for the given patterns. |
ValueError
|
If split_on_level is invalid. |
Source code in src/holodeck/lib/structured_chunker.py
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 | |
max_tokens
property
¶
Get the maximum tokens per chunk.
is_header_only(chunk)
¶
Check if chunk contains only a heading with no body content.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk
|
DocumentChunk
|
DocumentChunk to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if chunk is header-only (no substantive content). |
Source code in src/holodeck/lib/structured_chunker.py
1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 | |
parse(markdown, source_path='', mtime=0.0)
¶
Parse markdown into structure-aware chunks.
Main entry point for document processing. Extracts headings, builds parent chains, and splits content into token-bounded chunks while preserving hierarchical context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
markdown
|
str
|
The markdown content to parse. |
required |
source_path
|
str
|
Optional path to source file (for metadata). |
''
|
mtime
|
float
|
Optional file modification time (Unix timestamp). |
0.0
|
Returns:
| Type | Description |
|---|---|
list[DocumentChunk]
|
List of DocumentChunk objects with hierarchy preserved. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If markdown is empty or whitespace-only. |
Example
chunker = StructuredChunker() chunks = chunker.parse("# Title\n\nContent", "doc.md") print(chunks[0].section_id) 'sec_title'
Source code in src/holodeck/lib/structured_chunker.py
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 | |
Structured Data Loader¶
Loads and iterates over structured data from CSV, JSON, and JSONL files with field mapping, schema validation, and batch processing for vector store ingestion.
StructuredDataLoader(source_path, id_field, vector_fields, metadata_fields=None, field_separator='\n', delimiter=None, batch_size=10000)
¶
Load and iterate over structured data from CSV, JSON, or JSONL files.
This class provides a unified interface for loading structured data from various file formats and iterating over records with field mapping.
Attributes:
| Name | Type | Description |
|---|---|---|
source_path |
Path to the source data file. |
|
id_field |
Field name to use as unique record identifier. |
|
vector_fields |
List of field names whose values will be embedded. |
|
metadata_fields |
List of field names to include as metadata. |
|
field_separator |
Separator for concatenating multiple vector fields. |
|
delimiter |
CSV delimiter (auto-detected if None). |
|
batch_size |
Number of records per batch. |
Example
loader = StructuredDataLoader( ... source_path="products.csv", ... id_field="id", ... vector_fields=["description"], ... metadata_fields=["title", "category"], ... ) for record in loader.iter_records(): ... print(record["id"], record["content"])
Initialize the StructuredDataLoader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
source_path
|
str
|
Path to the source data file. |
required |
id_field
|
str
|
Field name to use as unique record identifier. |
required |
vector_fields
|
list[str]
|
List of field names whose values will be embedded. |
required |
metadata_fields
|
list[str] | None
|
List of field names to include as metadata. If None, includes all fields except id_field and vector_fields. |
None
|
field_separator
|
str
|
Separator for concatenating multiple vector fields. |
'\n'
|
delimiter
|
str | None
|
CSV delimiter. If None, auto-detected from content. |
None
|
batch_size
|
int
|
Number of records per batch for iter_batches(). |
10000
|
Source code in src/holodeck/lib/structured_loader.py
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 | |
file_type
property
¶
Get the detected file type (cached).
iter_batches()
¶
Iterate over batches of records.
Groups records into batches of batch_size for efficient processing.
Yields:
| Type | Description |
|---|---|
list[dict[str, Any]]
|
List of record dictionaries (up to batch_size per batch). |
Example
for batch in loader.iter_batches(): ... print(f"Processing {len(batch)} records") ... for record in batch: ... process(record)
Source code in src/holodeck/lib/structured_loader.py
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 | |
iter_records()
¶
Iterate over records with field mapping applied.
Yields dictionaries with keys: - id: The record identifier (from id_field) - content: Concatenated vector field values - metadata: Dictionary of metadata field values
Records with empty content (all vector fields empty) are skipped.
Yields:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with id, content, and metadata keys. |
Example
for record in loader.iter_records(): ... print(record["id"]) ... print(record["content"]) ... print(record["metadata"])
Source code in src/holodeck/lib/structured_loader.py
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 | |
validate_schema()
¶
Validate that configured fields exist in the source data.
Checks that id_field, vector_fields, and metadata_fields all exist in the source data schema.
Returns:
| Type | Description |
|---|---|
list[str]
|
List of available field names in the source. |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If any configured field doesn't exist in the source. |
Example
loader = StructuredDataLoader(...) available_fields = loader.validate_schema() print(available_fields) ['id', 'title', 'description', 'category', 'price']
Source code in src/holodeck/lib/structured_loader.py
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 | |
Context Generation¶
Implements the Anthropic contextual retrieval approach -- generating short context snippets for document chunks to improve semantic search retrieval by 35-49%.
Claude SDK Context Generator¶
Uses the Claude Agent SDK query() function for contextual embeddings. Supports batched prompts with JSON array parsing and automatic single-chunk fallback.
ClaudeContextConfig(model='claude-haiku-4-5', batch_size=10, concurrency=5, max_retries=3, base_delay=1.0, max_document_tokens=8000)
dataclass
¶
Configuration for ClaudeSDKContextGenerator.
Attributes:
| Name | Type | Description |
|---|---|---|
model |
str
|
Claude model ID for context generation (default: Haiku for cost). |
batch_size |
int
|
Number of chunks per batch prompt. |
concurrency |
int
|
Maximum concurrent |
max_retries |
int
|
Retry attempts per query. |
base_delay |
float
|
Initial retry delay in seconds. |
max_document_tokens |
int
|
Document truncation limit in tokens. |
ClaudeSDKContextGenerator(config=None, max_context_tokens=100)
¶
Generate contextual embeddings using the Claude Agent SDK.
Conforms to the ContextGenerator protocol defined in
holodeck.lib.backends.base.
Uses the Claude Agent SDK query() function to call a cheap/fast model
(Haiku by default) for generating situating context for document chunks.
Supports batched prompts (multiple chunks per call) with JSON parsing and
automatic fallback to single-chunk prompts on failure.
Attributes:
| Name | Type | Description |
|---|---|---|
_config |
Configuration for the generator. |
|
_max_context_tokens |
Maximum tokens for generated context. |
|
_encoder |
Tiktoken encoder for token counting. |
Initialize the Claude SDK Context Generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
config
|
ClaudeContextConfig | None
|
Configuration for the generator. Uses defaults if not provided. |
None
|
max_context_tokens
|
int
|
Maximum tokens for generated context (default: 100). |
100
|
Source code in src/holodeck/lib/claude_context_generator.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
context_policy()
¶
Return the identity of the stored context text: recipe and model.
Part of the pipeline fingerprint of a hierarchical document tool.
Source code in src/holodeck/lib/claude_context_generator.py
110 111 112 113 114 115 116 117 118 119 120 121 | |
contextualize_batch(chunks, document_text, concurrency=None)
async
¶
Generate contextual descriptions for a batch of chunks.
Splits chunks into sub-batches of config.batch_size, processes them
concurrently (bounded by semaphore), and returns results in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunks
|
list[DocumentChunk]
|
Document chunks to contextualize. |
required |
document_text
|
str
|
Full text of the source document. |
required |
concurrency
|
int | None
|
Maximum concurrent LLM calls. Uses config default if None. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
A list of contextualized content strings, one per chunk. |
Source code in src/holodeck/lib/claude_context_generator.py
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 | |
LLM Context Generator¶
Uses the configured chat-completion provider for contextual embeddings. Supports adaptive concurrency on rate limiting and exponential backoff retry logic.
RetryConfig(max_retries=3, base_delay=1.0, exponential_base=2.0, max_delay=10.0)
dataclass
¶
Configuration for exponential backoff retry logic.
Attributes:
| Name | Type | Description |
|---|---|---|
max_retries |
int
|
Maximum number of retry attempts (default: 3). |
base_delay |
float
|
Initial delay in seconds before first retry (default: 1.0). |
exponential_base |
float
|
Multiplier for exponential backoff (default: 2.0). |
max_delay |
float
|
Maximum delay cap in seconds (default: 10.0). |
Example
With defaults, delays are: 1s, 2s, 4s (capped at max_delay if exceeded).
LLMContextGenerator(model_spec, max_context_tokens=DEFAULT_MAX_CONTEXT_TOKENS, max_document_tokens=DEFAULT_MAX_DOCUMENT_TOKENS, concurrency=None, retry_config=None)
¶
Generate contextual embeddings using LLM (Anthropic approach).
Conforms to the ContextGenerator protocol defined in
holodeck.lib.backends.base.
This class generates short context snippets for document chunks to improve semantic search retrieval. It follows Anthropic's contextual retrieval approach which prepends situational context to each chunk before embedding.
Attributes:
| Name | Type | Description |
|---|---|---|
_model_spec |
Resolved LiteLLM call arguments for the context model. |
|
_max_context_tokens |
Maximum tokens for generated context (default: 100). |
|
_max_document_tokens |
Maximum tokens for document in prompt (default: 8000). |
|
_concurrency |
Current concurrency limit for batch processing. |
|
_retry_config |
Configuration for exponential backoff retries. |
Example
from holodeck.lib.litellm_support import resolve_litellm_model spec = resolve_litellm_model(agent.model, kind="chat") generator = LLMContextGenerator(model_spec=spec) context = await generator.generate_context(chunk_text, document_text)
Initialize the LLM Context Generator.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model_spec
|
LiteLLMModelSpec
|
Resolved LiteLLM call arguments (chat kind). |
required |
max_context_tokens
|
int
|
Maximum tokens for generated context (default: 100). |
DEFAULT_MAX_CONTEXT_TOKENS
|
max_document_tokens
|
int
|
Maximum tokens for document truncation (default: 8000). |
DEFAULT_MAX_DOCUMENT_TOKENS
|
concurrency
|
int | None
|
Maximum concurrent LLM requests (default: 10). |
None
|
retry_config
|
RetryConfig | None
|
Configuration for retry logic. Uses defaults if not provided. |
None
|
Source code in src/holodeck/lib/llm_context_generator.py
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 | |
context_policy()
¶
Return the identity of the stored context text: recipe and model.
Part of the pipeline fingerprint of a hierarchical document tool.
Source code in src/holodeck/lib/llm_context_generator.py
129 130 131 132 133 134 135 136 137 138 | |
contextualize_batch(chunks, document_text, concurrency=None)
async
¶
Batch process multiple chunks with concurrency control.
Processes all chunks concurrently using a semaphore to limit the number of simultaneous LLM calls. Results maintain the same order as input chunks.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunks
|
list[DocumentChunk]
|
List of DocumentChunks to contextualize. |
required |
document_text
|
str
|
The full document text for context. |
required |
concurrency
|
int | None
|
Optional concurrency override. Uses instance default if None. |
None
|
Returns:
| Type | Description |
|---|---|
list[str]
|
List of contextualized content strings in same order as input chunks. |
Example
results = await generator.contextualize_batch(chunks, document) assert len(results) == len(chunks)
Source code in src/holodeck/lib/llm_context_generator.py
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 | |
contextualize_chunk(chunk, document_text)
async
¶
Contextualize a single document chunk.
Generates context for the chunk and prepends it to the chunk content in the format: "{context}\n\n{chunk.content}".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk
|
DocumentChunk
|
The DocumentChunk to contextualize. |
required |
document_text
|
str
|
The full document text for context. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Contextualized content string. On failure, returns original content. |
Example
result = await generator.contextualize_chunk(chunk, document) print(result) "This chunk defines force majeure terms.
Force Majeure means any event beyond reasonable control..."
Source code in src/holodeck/lib/llm_context_generator.py
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 | |
generate_context(chunk_text, document_text)
async
¶
Generate context for a single chunk.
Uses the LLM to generate a short (50-100 token) context snippet that situates the chunk within the broader document. Includes retry logic with exponential backoff for resilience.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_text
|
str
|
The chunk content to generate context for. |
required |
document_text
|
str
|
The full document text for context. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Generated context string, or empty string on failure. |
Example
context = await generator.generate_context( ... "Force Majeure means any event...", ... full_policy_document ... ) print(context) "This is the definition section of the insurance policy."
Source code in src/holodeck/lib/llm_context_generator.py
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 | |
Tool Initialization¶
Shared tool initialization for VectorStoreTool and HierarchicalDocumentTool. Provider-agnostic: works across every backend (OpenAI Agents and Claude).
resolve_embedding_model(agent)
¶
Resolve embedding model name from agent config.
Checks vectorstore and hierarchical-doc tool configs for explicit
embedding_model values first. If explicit values conflict across tools,
raises an error because embedding services are shared per agent. Falls back
to provider defaults when no explicit value is configured.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Agent
|
Agent configuration. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Embedding model name string. |
Raises:
| Type | Description |
|---|---|
ToolInitializerError
|
If explicit embedding_model values conflict. |
Source code in src/holodeck/lib/tool_initializer.py
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 | |
create_embedding_service(agent, *, dimensions=None)
¶
Create a LiteLLM-backed embedding service from agent config.
For Anthropic provider: uses agent.embedding_provider config.
For OpenAI/Azure/Ollama: uses agent.model config directly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Agent
|
Agent configuration. |
required |
dimensions
|
int | None
|
Explicit output dimensions forwarded to the provider on
every request. |
None
|
Returns:
| Type | Description |
|---|---|
LiteLLMEmbeddingService
|
An initialized LiteLLMEmbeddingService instance. |
Raises:
| Type | Description |
|---|---|
ToolInitializerError
|
If provider doesn't support embeddings. |
Source code in src/holodeck/lib/tool_initializer.py
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 | |
initialize_tools(agent, force_ingest=False, execution_config=None, base_dir=None, context_generator=None)
async
¶
Initialize all vectorstore and hierarchical-doc tools for an agent.
Creates embedding service, initializes each tool, returns dict keyed by tool config name. This is the main entry point for both backends.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Agent
|
Agent configuration. |
required |
force_ingest
|
bool
|
Force re-ingestion of vector store source files. |
False
|
execution_config
|
ExecutionConfig | None
|
Execution configuration for file processing. |
None
|
base_dir
|
str | None
|
Base directory for resolving relative source paths. If None, falls back to agent_base_dir context variable. |
None
|
context_generator
|
Any | None
|
Optional pre-built ContextGenerator instance. When provided, takes highest priority for contextual embeddings. |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict mapping tool name to initialized tool instance. |
Raises:
| Type | Description |
|---|---|
ToolInitializerError
|
On failure. |
Source code in src/holodeck/lib/tool_initializer.py
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 | |
initialize_hierarchical_doc_tools(agent, embedding_service, force_ingest, provider_type, base_dir=None, context_generator=None, execution_config=None)
async
¶
Initialize all hierarchical document tools from agent config.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent
|
Agent
|
Agent configuration. |
required |
embedding_service
|
Any
|
Embedding service (LiteLLMEmbeddingService). |
required |
force_ingest
|
bool
|
Force re-ingestion of source files. |
required |
provider_type
|
str
|
Provider type string for dimension resolution. |
required |
base_dir
|
str | None
|
Base directory for resolving relative source paths. |
None
|
context_generator
|
Any | None
|
Optional pre-built ContextGenerator instance. |
None
|
execution_config
|
ExecutionConfig | None
|
Execution configuration forwarded to each tool so
|
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dict mapping tool name to initialized HierarchicalDocumentTool instance. |
Raises:
| Type | Description |
|---|---|
ToolInitializerError
|
If any tool fails to initialize. |
Source code in src/holodeck/lib/tool_initializer.py
533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 | |
Instruction Resolution¶
Resolves agent instructions from Instructions config objects, supporting both inline text and file-based instructions with base directory resolution.
resolve_instructions(instructions, base_dir=None)
¶
Resolve agent instructions from an Instructions config object.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
instructions
|
Instructions
|
Instructions config with either |
required |
base_dir
|
Path | None
|
Explicit base directory for resolving relative file paths.
Falls back to the |
None
|
Returns:
| Type | Description |
|---|---|
str
|
The resolved instruction text. |
Raises:
| Type | Description |
|---|---|
ConfigError
|
If the instructions file is missing or cannot be read. |
Source code in src/holodeck/lib/instruction_resolver.py
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 | |
Vector Store¶
Tools reach every vector store through the VectorStoreAdapter contract in
holodeck.lib.retrieval. Each provider has a native adapter under
holodeck.lib.retrieval.adapters; create_adapter picks it from the tool's
database.provider.
create_adapter(provider, schema, connection=None)
¶
Build the adapter for provider.
Raises:
| Type | Description |
|---|---|
RetiredProviderError
|
The provider name was retired. |
UnknownProviderError
|
The provider name is not known. |
MissingClientError
|
The provider's client library is not installed. |
Source code in src/holodeck/lib/retrieval/registry.py
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 | |
VectorStoreAdapter
¶
Bases: Protocol
One collection in one vector store.
An adapter owns its client. close releases it. Every method that the
provider cannot do raises UnsupportedOperationError before it touches
the store. Reads never return vectors unless include_vectors is set.
capabilities
property
¶
Operations this adapter supports.
provider
property
¶
Provider name as written in agent YAML.
schema
property
¶
Layout of the collection.
close()
async
¶
Release the client. Safe to call more than once.
Source code in src/holodeck/lib/retrieval/adapter.py
118 119 120 | |
collection_exists()
async
¶
Return True when the collection exists in the store.
Source code in src/holodeck/lib/retrieval/adapter.py
51 52 53 | |
count()
async
¶
Return the number of records. Zero when the collection is absent.
Source code in src/holodeck/lib/retrieval/adapter.py
63 64 65 | |
delete(ids)
async
¶
Delete records by ID. Missing IDs are ignored.
Source code in src/holodeck/lib/retrieval/adapter.py
77 78 79 | |
delete_collection()
async
¶
Delete the collection. No error when it does not exist.
Source code in src/holodeck/lib/retrieval/adapter.py
59 60 61 | |
delete_where(record_filter)
async
¶
Delete every record that matches the filter. Return the count.
Source code in src/holodeck/lib/retrieval/adapter.py
93 94 95 | |
ensure_collection()
async
¶
Create the collection and its indexes when it does not exist.
Source code in src/holodeck/lib/retrieval/adapter.py
55 56 57 | |
find(record_filter, *, limit=None)
async
¶
Return every record that matches the filter, without vectors.
Source code in src/holodeck/lib/retrieval/adapter.py
81 82 83 84 85 | |
get(ids, *, include_vectors=False)
async
¶
Return the records that exist, in the order of ids.
Source code in src/holodeck/lib/retrieval/adapter.py
71 72 73 74 75 | |
hybrid_search(query)
async
¶
Return fused vector and lexical results, best first.
Source code in src/holodeck/lib/retrieval/adapter.py
114 115 116 | |
lexical_search(query)
async
¶
Return records that match the query tokens, best first.
Source code in src/holodeck/lib/retrieval/adapter.py
110 111 112 | |
list_page(*, cursor=None, limit=DEFAULT_PAGE_SIZE)
async
¶
Return one page of all records, without vectors.
Source code in src/holodeck/lib/retrieval/adapter.py
87 88 89 90 91 | |
replace_source(record_filter, records)
async
¶
Replace the records that match the filter with records.
Stale records are removed even when the new set is smaller.
Source code in src/holodeck/lib/retrieval/adapter.py
97 98 99 100 101 102 103 104 | |
upsert(records)
async
¶
Insert or replace records by ID. Every record carries a vector.
Source code in src/holodeck/lib/retrieval/adapter.py
67 68 69 | |
vector_search(query)
async
¶
Return the nearest records, best first, scored by similarity.
Source code in src/holodeck/lib/retrieval/adapter.py
106 107 108 | |
QueryResult
¶
Search result from vector store query.
Represents a single match returned from semantic search operations.
Attributes:
| Name | Type | Description |
|---|---|---|
content |
str
|
Matched document chunk content |
score |
float
|
Relevance/similarity score (0.0-1.0, higher is better) |
source_path |
str
|
Original source file path |
chunk_index |
int
|
Chunk index within source file |
metadata |
dict[str, Any]
|
Additional metadata (file_type, file_size, mtime, etc.) |
StructuredQueryResult
¶
Search result from structured data vector store query.
Represents a single match returned from semantic search over structured data (CSV, JSON, JSONL files). Unlike QueryResult which uses chunk_index, this uses the original record ID from the source data.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Original record identifier from the id_field in source data |
content |
str
|
Concatenated vector field content that was embedded |
score |
float
|
Relevance/similarity score (0.0-1.0, higher is better) |
source_file |
str
|
Original source file path (e.g., "products.csv") |
metadata |
dict[str, Any]
|
Dictionary of metadata field values from the source record |
Logging¶
Centralized logging configuration with support for console and file handlers, environment variable configuration, log rotation, and structured logging patterns.
Logging Configuration¶
setup_logging(level=None, log_file=None, log_format=None, verbose=False, quiet=False)
¶
Configure logging for HoloDeck application.
This function sets up the root logger with appropriate handlers and formatters. It respects environment variables and command-line flags for configuration. It also configures third-party library loggers to respect the quiet flag.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
Log level (DEBUG, INFO, WARNING, ERROR, CRITICAL). If not provided, uses HOLODECK_LOG_LEVEL env var or defaults to INFO. |
None
|
log_file
|
str
|
Path to log file. If not provided, uses HOLODECK_LOG_FILE env var. If neither is set, only console logging is used. |
None
|
log_format
|
str
|
Log format string. If not provided, uses HOLODECK_LOG_FORMAT env var or default format. |
None
|
verbose
|
bool
|
If True, sets log level to DEBUG. Overrides level parameter. |
False
|
quiet
|
bool
|
If True, sets log level to ERROR. Overrides verbose and level. |
False
|
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
setup_logging(verbose=True) # Enable DEBUG logging setup_logging(quiet=True) # Only show ERROR and above setup_logging(log_file="/var/log/holodeck.log") # Enable file logging
Source code in src/holodeck/lib/logging_config.py
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 | |
get_logger(name)
¶
Get a logger instance for the specified module.
This is a convenience wrapper around logging.getLogger() that ensures consistent logger naming across the application.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Name of the logger, typically name of the calling module. |
required |
Returns:
| Type | Description |
|---|---|
Logger
|
logging.Logger: Logger instance for the specified name. |
Example
logger = get_logger(name) logger.info("Processing started")
Source code in src/holodeck/lib/logging_config.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
set_log_level(level)
¶
Dynamically change the log level for all loggers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str
|
New log level (DEBUG, INFO, WARNING, ERROR, CRITICAL). |
required |
Returns:
| Type | Description |
|---|---|
None
|
None |
Example
set_log_level("DEBUG") # Enable debug logging
Source code in src/holodeck/lib/logging_config.py
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 | |
configure_third_party_loggers(log_level)
¶
Configure known third-party loggers to respect the specified log level.
This suppresses noisy INFO logs from libraries like httpx, chromadb, etc. Can be called from both traditional logging and OTel logging setup.
Setting level on parent logger (e.g., "chromadb") also affects child loggers (e.g., "chromadb.telemetry.product.posthog").
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
log_level
|
int
|
The logging level to apply (e.g., logging.WARNING) |
required |
Source code in src/holodeck/lib/logging_config.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | |
Logging Utilities¶
LogTimer(logger, level=logging.INFO)
¶
Timer utility for logging operation durations.
This class provides a simple way to measure and log operation durations.
Example
logger = logging.getLogger(name) timer = LogTimer(logger) timer.start("Processing batch")
Do work...¶
timer.stop() # Logs: "Processing batch completed in X.XXs"
Initialize the timer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Logger to use for timing messages. |
required |
level
|
int
|
Log level to use (default: INFO). |
INFO
|
Source code in src/holodeck/lib/logging_utils.py
162 163 164 165 166 167 168 169 170 171 172 173 | |
start(operation)
¶
Start timing an operation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
operation
|
str
|
Name/description of the operation. |
required |
Source code in src/holodeck/lib/logging_utils.py
175 176 177 178 179 180 181 182 183 184 | |
stop(context=None)
¶
Stop timing and log the elapsed time.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
context
|
dict
|
Additional context to include in log. |
None
|
Returns:
| Name | Type | Description |
|---|---|---|
float |
float
|
Elapsed time in seconds. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If start() was not called first. |
Source code in src/holodeck/lib/logging_utils.py
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 | |
log_operation(logger, operation, level=logging.INFO, context=None)
¶
Context manager for logging operation start, completion, and timing.
Logs the start of an operation, then logs its completion with elapsed time. If an exception occurs, logs the error with the operation context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Logger to use for logging. |
required |
operation
|
str
|
Name/description of the operation. |
required |
level
|
int
|
Log level to use (default: INFO). |
INFO
|
context
|
dict
|
Additional context to include in logs. |
None
|
Yields:
| Type | Description |
|---|---|
None
|
None |
Example
logger = logging.getLogger(name) with log_operation(logger, "Processing file", context={"file": "test.txt"}): ... # Do work here ... process_file("test.txt")
Source code in src/holodeck/lib/logging_utils.py
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 | |
log_context(logger, **kwargs)
¶
Context manager for adding structured context to log messages.
This is useful for adding contextual information that should be included in all log messages within a specific scope.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Logger to use. |
required |
**kwargs
|
Any
|
Key-value pairs to add to log context. |
{}
|
Yields:
| Type | Description |
|---|---|
None
|
None |
Example
logger = logging.getLogger(name) with log_context(logger, test_id="test-001", attempt=1): ... logger.info("Processing test") # Includes test_id and attempt
Source code in src/holodeck/lib/logging_utils.py
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 | |
log_with_context(logger, level, message, **context)
¶
Log a message with structured context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Logger to use. |
required |
level
|
int
|
Log level. |
required |
message
|
str
|
Log message. |
required |
**context
|
Any
|
Additional context key-value pairs. |
{}
|
Example
logger = logging.getLogger(name) log_with_context( ... logger, ... logging.INFO, ... "Test passed", ... test_id="test-001", ... duration=1.23 ... )
Source code in src/holodeck/lib/logging_utils.py
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 | |
log_exception(logger, message, exc, level=logging.ERROR, context=None)
¶
Log an exception with context and stack trace.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Logger to use. |
required |
message
|
str
|
Error message describing what failed. |
required |
exc
|
Exception
|
The exception that occurred. |
required |
level
|
int
|
Log level (default: ERROR). |
ERROR
|
context
|
dict
|
Additional context information. |
None
|
Example
logger = logging.getLogger(name) try: ... risky_operation() ... except Exception as e: ... log_exception(logger, "Operation failed", e, context={"id": "123"})
Source code in src/holodeck/lib/logging_utils.py
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 | |
log_retry(logger, operation, attempt, max_attempts, delay, error=None)
¶
Log a retry attempt with structured context.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
logger
|
Logger
|
Logger to use. |
required |
operation
|
str
|
Name of the operation being retried. |
required |
attempt
|
int
|
Current attempt number. |
required |
max_attempts
|
int
|
Maximum number of attempts. |
required |
delay
|
float
|
Delay before next retry in seconds. |
required |
error
|
Exception
|
The error that caused the retry. |
None
|
Example
logger = logging.getLogger(name) log_retry(logger, "API call", attempt=2, max_attempts=3, delay=5.0)
Source code in src/holodeck/lib/logging_utils.py
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 | |
Validation¶
Shared validation functions and constants for agent name validation, chat input validation, tool name sanitization, and tool output sanitization.
validate_agent_name(name)
¶
Validate agent name format.
Agent names must: - Not be empty - Be 64 characters or less - Start with a letter (a-z, A-Z) - Contain only alphanumeric characters, hyphens, and underscores
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
The agent name to validate |
required |
Returns:
| Type | Description |
|---|---|
str
|
The validated agent name (unchanged if valid) |
Raises:
| Type | Description |
|---|---|
ValueError
|
If agent name is invalid |
Source code in src/holodeck/lib/validation.py
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 | |
ValidationPipeline(max_length=10000)
¶
Extensible validation pipeline for user input.
Initialize the pipeline with a max length constraint.
Source code in src/holodeck/lib/validation.py
64 65 66 | |
validate(message)
¶
Validate a message and return (is_valid, error_message).
Source code in src/holodeck/lib/validation.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 | |
sanitize_tool_output(output, max_length=5000)
¶
Remove control/ANSI sequences and truncate long outputs.
Source code in src/holodeck/lib/validation.py
91 92 93 94 95 96 97 98 99 | |
sanitize_tool_name(name)
¶
Sanitize a string to be a valid tool name.
Tool names must match pattern ^[0-9A-Za-z_]+$ (alphanumeric and underscores). This function: 1. Replaces all invalid characters with underscores 2. Collapses multiple consecutive underscores into one 3. Strips leading/trailing underscores 4. Validates the result matches the required pattern
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
str
|
Raw name string to sanitize. |
required |
Returns:
| Type | Description |
|---|---|
str
|
Sanitized name containing only alphanumeric characters and underscores. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the input is empty or results in an empty/invalid name. |
Examples:
>>> sanitize_tool_name("server-filesystem")
'server_filesystem'
>>> sanitize_tool_name("foo--bar")
'foo_bar'
>>> sanitize_tool_name("my.tool.name")
'my_tool_name'
>>> sanitize_tool_name(" spaces ")
'spaces'
Source code in src/holodeck/lib/validation.py
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 | |
Chat History¶
Utilities for extracting tool information from agent execution results.
extract_tool_names(tool_calls)
¶
Extract tool names from tool calls list.
Tool calls are represented as list of dicts with 'name' and 'arguments' keys.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_calls
|
list[dict[str, Any]]
|
List of tool call dicts from agent. |
required |
Returns:
| Type | Description |
|---|---|
list[str]
|
List of tool names that were called. |
Source code in src/holodeck/lib/chat_history_utils.py
10 11 12 13 14 15 16 17 18 19 20 21 | |
Definition Extraction¶
Data structures for representing extracted definitions from documents. Definitions are key terms and their explanations used to enhance search results with contextual information.
DefinitionEntry(id, source_path, term, term_normalized, definition_text, source_section, exceptions=list())
dataclass
¶
An extracted definition from a document.
Represents a term definition extracted from a document, including the term itself, its definition text, and metadata about where it was found.
Attributes:
| Name | Type | Description |
|---|---|---|
id |
str
|
Unique identifier for this definition entry |
source_path |
str
|
Path to the source document containing the definition |
term |
str
|
The term being defined (original casing) |
term_normalized |
str
|
Lowercase normalized term for case-insensitive lookup |
definition_text |
str
|
The full definition text explaining the term |
source_section |
str
|
Section ID or heading where the definition was found |
exceptions |
list[str]
|
List of exceptions or exclusions to the definition |
Example
entry = DefinitionEntry( ... id="policy_md_def_force_majeure", ... source_path="/docs/policy.md", ... term="Force Majeure", ... term_normalized="force majeure", ... definition_text="Any event beyond the reasonable control...", ... source_section="1.2 Definitions", ... exceptions=["acts of negligence", "breach of contract"], ... )
UI Utilities¶
Terminal detection, color output, and spinner animation utilities for the CLI layer.
Colors¶
ANSIColors
¶
ANSI color escape codes for terminal output.
Provides standard ANSI color codes that can be used with the colorize() function or applied directly to strings.
Attributes:
| Name | Type | Description |
|---|---|---|
GREEN |
Bright green color (for success indicators). |
|
RED |
Bright red color (for failure indicators). |
|
YELLOW |
Bright yellow color (for warnings). |
|
RESET |
Reset code to restore default terminal color. |
colorize(text, color, force_tty=None)
¶
Apply ANSI color codes to text if in TTY mode.
Wraps text with the specified color code and reset sequence, but only if stdout is connected to a terminal. This ensures clean output in CI/CD logs and file redirects.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
text
|
str
|
Text to colorize. |
required |
color
|
str
|
ANSI color code to apply (e.g., ANSIColors.GREEN). |
required |
force_tty
|
bool | None
|
Override TTY detection (for testing). None uses auto-detection. |
None
|
Returns:
| Type | Description |
|---|---|
str
|
Colorized text if in TTY mode, plain text otherwise. |
Source code in src/holodeck/lib/ui/colors.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | |
Spinner¶
SpinnerMixin
¶
Mixin providing spinner animation functionality.
Provides braille spinner characters and rotation logic for progress indicators. Classes using this mixin should initialize _spinner_index = 0 in their init.
Class Attributes
SPINNER_CHARS: List of braille characters for spinner animation.
Instance Attributes
_spinner_index: Current position in spinner rotation (must be initialized).
get_spinner_char()
¶
Get current spinner character and advance rotation.
Returns:
| Type | Description |
|---|---|
str
|
Current spinner character from the braille sequence. |
Source code in src/holodeck/lib/ui/spinner.py
36 37 38 39 40 41 42 43 44 | |
Terminal Detection¶
is_tty()
¶
Check if stdout is connected to a terminal.
Used to determine whether to use rich formatting (colors, spinners) or plain text output suitable for CI/CD logs.
Returns:
| Type | Description |
|---|---|
bool
|
True if stdout is a TTY (interactive terminal), False otherwise. |
Source code in src/holodeck/lib/ui/terminal.py
9 10 11 12 13 14 15 16 17 18 | |
Error Hierarchy¶
Exception classes for HoloDeck library operations. All exceptions inherit from HoloDeckError, enabling generic error handling with except HoloDeckError.
HoloDeckError
¶
Bases: Exception
Base exception for all HoloDeck library errors.
This is the parent class for all exceptions raised by the holodeck library. Users can catch this to handle any library error generically.
ValidationError
¶
Bases: HoloDeckError
Raised when validation fails.
This exception is raised when: - Input validation fails - Schema validation fails - Configuration is invalid
Attributes:
| Name | Type | Description |
|---|---|---|
message |
Description of the validation failure |
InitError
¶
Bases: HoloDeckError
Raised when initialization fails.
This exception is raised when: - Project initialization fails - Directory creation fails - File writing fails - Template rendering fails
Attributes:
| Name | Type | Description |
|---|---|---|
message |
Description of the initialization failure |
TemplateError
¶
Bases: HoloDeckError
Raised when template processing fails.
This exception is raised when: - Template manifest is malformed or missing - Jinja2 rendering fails - Generated content doesn't validate
Attributes:
| Name | Type | Description |
|---|---|---|
message |
Description of the template failure |