harborrag-adapters 2.0.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- harborrag_adapters/__init__.py +3 -0
- harborrag_adapters/chunking/__init__.py +42 -0
- harborrag_adapters/chunking/_base.py +574 -0
- harborrag_adapters/chunking/_spans.py +67 -0
- harborrag_adapters/chunking/code_structure.py +271 -0
- harborrag_adapters/chunking/document_structure.py +182 -0
- harborrag_adapters/chunking/factory.py +103 -0
- harborrag_adapters/chunking/languages.py +128 -0
- harborrag_adapters/chunking/markdown_sections.py +602 -0
- harborrag_adapters/chunking/recursive.py +161 -0
- harborrag_adapters/chunking/router.py +77 -0
- harborrag_adapters/chunking/separators.py +38 -0
- harborrag_adapters/chunking/text_structure.py +131 -0
- harborrag_adapters/chunking/tokenizers.py +48 -0
- harborrag_adapters/connectors/README.md +280 -0
- harborrag_adapters/connectors/__init__.py +100 -0
- harborrag_adapters/connectors/atlassian/__init__.py +4 -0
- harborrag_adapters/connectors/atlassian/client.py +349 -0
- harborrag_adapters/connectors/atlassian/query.py +79 -0
- harborrag_adapters/connectors/attachments/__init__.py +39 -0
- harborrag_adapters/connectors/attachments/loading.py +56 -0
- harborrag_adapters/connectors/attachments/processing.py +423 -0
- harborrag_adapters/connectors/attachments/records.py +75 -0
- harborrag_adapters/connectors/attachments/selection.py +37 -0
- harborrag_adapters/connectors/attachments/sources.py +298 -0
- harborrag_adapters/connectors/base.py +158 -0
- harborrag_adapters/connectors/confluence/__init__.py +20 -0
- harborrag_adapters/connectors/confluence/client.py +102 -0
- harborrag_adapters/connectors/confluence/config.py +117 -0
- harborrag_adapters/connectors/confluence/connector.py +326 -0
- harborrag_adapters/connectors/confluence/content.py +344 -0
- harborrag_adapters/connectors/confluence/discovery.py +274 -0
- harborrag_adapters/connectors/confluence/document_transform.py +207 -0
- harborrag_adapters/connectors/confluence/mappers.py +334 -0
- harborrag_adapters/connectors/confluence/normalization/README.md +55 -0
- harborrag_adapters/connectors/confluence/normalization/__init__.py +33 -0
- harborrag_adapters/connectors/confluence/normalization/adf.py +155 -0
- harborrag_adapters/connectors/confluence/normalization/block_handlers.py +330 -0
- harborrag_adapters/connectors/confluence/normalization/block_models.py +93 -0
- harborrag_adapters/connectors/confluence/normalization/errors.py +10 -0
- harborrag_adapters/connectors/confluence/normalization/hierarchy.py +310 -0
- harborrag_adapters/connectors/confluence/normalization/include_handlers.py +162 -0
- harborrag_adapters/connectors/confluence/normalization/macros.py +188 -0
- harborrag_adapters/connectors/confluence/normalization/markup.py +181 -0
- harborrag_adapters/connectors/confluence/normalization/nodes.py +64 -0
- harborrag_adapters/connectors/confluence/normalization/page.py +134 -0
- harborrag_adapters/connectors/confluence/normalization/schemas.py +176 -0
- harborrag_adapters/connectors/confluence/normalization/table_analysis.py +152 -0
- harborrag_adapters/connectors/confluence/normalization/table_evidence.py +84 -0
- harborrag_adapters/connectors/confluence/normalization/tables.py +318 -0
- harborrag_adapters/connectors/confluence/normalization/values.py +18 -0
- harborrag_adapters/connectors/confluence/policy.py +117 -0
- harborrag_adapters/connectors/confluence/query.py +193 -0
- harborrag_adapters/connectors/confluence/relations.py +138 -0
- harborrag_adapters/connectors/confluence/schemas.py +65 -0
- harborrag_adapters/connectors/confluence/title_resolution.py +123 -0
- harborrag_adapters/connectors/descriptors.py +67 -0
- harborrag_adapters/connectors/document_transform.py +26 -0
- harborrag_adapters/connectors/exceptions.py +127 -0
- harborrag_adapters/connectors/file_filters.py +76 -0
- harborrag_adapters/connectors/github/__init__.py +5 -0
- harborrag_adapters/connectors/github/client.py +238 -0
- harborrag_adapters/connectors/github/config.py +167 -0
- harborrag_adapters/connectors/github/connector.py +325 -0
- harborrag_adapters/connectors/github/filters.py +219 -0
- harborrag_adapters/connectors/github/mappers.py +142 -0
- harborrag_adapters/connectors/github/repository.py +451 -0
- harborrag_adapters/connectors/github/repository_paths.py +312 -0
- harborrag_adapters/connectors/github/schemas.py +41 -0
- harborrag_adapters/connectors/harbor_connector.py +100 -0
- harborrag_adapters/connectors/jira/__init__.py +17 -0
- harborrag_adapters/connectors/jira/client.py +138 -0
- harborrag_adapters/connectors/jira/config.py +152 -0
- harborrag_adapters/connectors/jira/connector.py +330 -0
- harborrag_adapters/connectors/jira/content.py +432 -0
- harborrag_adapters/connectors/jira/discovery.py +270 -0
- harborrag_adapters/connectors/jira/discovery_policy.py +260 -0
- harborrag_adapters/connectors/jira/document_transform.py +260 -0
- harborrag_adapters/connectors/jira/issues.py +411 -0
- harborrag_adapters/connectors/jira/mappers.py +238 -0
- harborrag_adapters/connectors/jira/query.py +299 -0
- harborrag_adapters/connectors/jira/schemas.py +137 -0
- harborrag_adapters/connectors/limits.py +22 -0
- harborrag_adapters/connectors/local/__init__.py +6 -0
- harborrag_adapters/connectors/local/config.py +63 -0
- harborrag_adapters/connectors/local/connector.py +126 -0
- harborrag_adapters/connectors/local/document_transform.py +25 -0
- harborrag_adapters/connectors/local/filesystem.py +337 -0
- harborrag_adapters/connectors/local/filesystem_paths.py +106 -0
- harborrag_adapters/connectors/local/filters.py +76 -0
- harborrag_adapters/connectors/local/mappers.py +109 -0
- harborrag_adapters/connectors/local/relations.py +116 -0
- harborrag_adapters/connectors/local/schemas.py +24 -0
- harborrag_adapters/connectors/local/secure_read.py +196 -0
- harborrag_adapters/connectors/local/selection.py +161 -0
- harborrag_adapters/connectors/local/skips.py +138 -0
- harborrag_adapters/connectors/policies/__init__.py +43 -0
- harborrag_adapters/connectors/policies/http.py +409 -0
- harborrag_adapters/connectors/policies/network.py +308 -0
- harborrag_adapters/connectors/policies/validation.py +116 -0
- harborrag_adapters/connectors/query_values.py +19 -0
- harborrag_adapters/connectors/rate_limiting.py +297 -0
- harborrag_adapters/connectors/registry.py +147 -0
- harborrag_adapters/connectors/sharepoint/__init__.py +5 -0
- harborrag_adapters/connectors/sharepoint/allowlist.py +128 -0
- harborrag_adapters/connectors/sharepoint/client.py +413 -0
- harborrag_adapters/connectors/sharepoint/config.py +249 -0
- harborrag_adapters/connectors/sharepoint/config_validation.py +271 -0
- harborrag_adapters/connectors/sharepoint/connector.py +530 -0
- harborrag_adapters/connectors/sharepoint/delegated.py +343 -0
- harborrag_adapters/connectors/sharepoint/drive.py +706 -0
- harborrag_adapters/connectors/sharepoint/drive_paths.py +281 -0
- harborrag_adapters/connectors/sharepoint/login.py +130 -0
- harborrag_adapters/connectors/sharepoint/mappers.py +280 -0
- harborrag_adapters/connectors/sharepoint/pages.py +189 -0
- harborrag_adapters/connectors/sharepoint/permissions.py +80 -0
- harborrag_adapters/connectors/sharepoint/schemas.py +62 -0
- harborrag_adapters/connectors/sharepoint/skips.py +295 -0
- harborrag_adapters/models/README.md +332 -0
- harborrag_adapters/models/__init__.py +0 -0
- harborrag_adapters/models/__main__.py +3 -0
- harborrag_adapters/models/chat/README.md +124 -0
- harborrag_adapters/models/chat/__init__.py +25 -0
- harborrag_adapters/models/chat/async_client.py +106 -0
- harborrag_adapters/models/chat/backend.py +55 -0
- harborrag_adapters/models/chat/backend_config.py +154 -0
- harborrag_adapters/models/chat/backends/__init__.py +9 -0
- harborrag_adapters/models/chat/backends/base.py +119 -0
- harborrag_adapters/models/chat/backends/direct.py +42 -0
- harborrag_adapters/models/chat/backends/factory.py +58 -0
- harborrag_adapters/models/chat/backends/proxy.py +155 -0
- harborrag_adapters/models/chat/backends/router.py +65 -0
- harborrag_adapters/models/chat/batch.py +141 -0
- harborrag_adapters/models/chat/batch_client.py +57 -0
- harborrag_adapters/models/chat/client.py +84 -0
- harborrag_adapters/models/chat/client_resources.py +92 -0
- harborrag_adapters/models/chat/client_runtime.py +178 -0
- harborrag_adapters/models/chat/configs.py +156 -0
- harborrag_adapters/models/chat/cost.py +61 -0
- harborrag_adapters/models/chat/errors.py +43 -0
- harborrag_adapters/models/chat/execution.py +304 -0
- harborrag_adapters/models/chat/factory.py +24 -0
- harborrag_adapters/models/chat/normalization.py +324 -0
- harborrag_adapters/models/chat/parameters.py +282 -0
- harborrag_adapters/models/chat/reasoning.py +109 -0
- harborrag_adapters/models/chat/registry.py +100 -0
- harborrag_adapters/models/chat/schemas.py +83 -0
- harborrag_adapters/models/chat/stream_execution.py +348 -0
- harborrag_adapters/models/chat/streaming.py +238 -0
- harborrag_adapters/models/chat/structured.py +303 -0
- harborrag_adapters/models/chat/structured_deadline.py +53 -0
- harborrag_adapters/models/chat/structured_policy.py +116 -0
- harborrag_adapters/models/chat/structured_strategy.py +10 -0
- harborrag_adapters/models/chat/structured_validation.py +85 -0
- harborrag_adapters/models/chat/tool_assembly.py +75 -0
- harborrag_adapters/models/chat/validation.py +315 -0
- harborrag_adapters/models/cli.py +129 -0
- harborrag_adapters/models/embed/__init__.py +23 -0
- harborrag_adapters/models/embed/batching.py +84 -0
- harborrag_adapters/models/embed/client.py +314 -0
- harborrag_adapters/models/embed/configs.py +171 -0
- harborrag_adapters/models/embed/errors.py +43 -0
- harborrag_adapters/models/embed/execution.py +348 -0
- harborrag_adapters/models/embed/execution_context.py +45 -0
- harborrag_adapters/models/embed/invocation.py +95 -0
- harborrag_adapters/models/embed/normalization.py +196 -0
- harborrag_adapters/models/embed/parameters.py +298 -0
- harborrag_adapters/models/embed/registry.py +124 -0
- harborrag_adapters/models/embed/validation.py +182 -0
- harborrag_adapters/models/normalization.py +23 -0
- harborrag_adapters/models/rerank/__init__.py +23 -0
- harborrag_adapters/models/rerank/client.py +299 -0
- harborrag_adapters/models/rerank/configs.py +117 -0
- harborrag_adapters/models/rerank/errors.py +43 -0
- harborrag_adapters/models/rerank/execution.py +309 -0
- harborrag_adapters/models/rerank/invocation.py +69 -0
- harborrag_adapters/models/rerank/normalization.py +145 -0
- harborrag_adapters/models/rerank/parameters.py +231 -0
- harborrag_adapters/models/rerank/registry.py +130 -0
- harborrag_adapters/models/rerank/validation.py +195 -0
- harborrag_adapters/models/runtime/__init__.py +155 -0
- harborrag_adapters/models/runtime/budget.py +273 -0
- harborrag_adapters/models/runtime/cache.py +308 -0
- harborrag_adapters/models/runtime/cache_redis.py +114 -0
- harborrag_adapters/models/runtime/client_lifecycle.py +64 -0
- harborrag_adapters/models/runtime/client_runtime.py +65 -0
- harborrag_adapters/models/runtime/config.py +269 -0
- harborrag_adapters/models/runtime/connections.py +113 -0
- harborrag_adapters/models/runtime/context.py +19 -0
- harborrag_adapters/models/runtime/distributed_config.py +66 -0
- harborrag_adapters/models/runtime/environment.py +131 -0
- harborrag_adapters/models/runtime/errors.py +162 -0
- harborrag_adapters/models/runtime/execution.py +235 -0
- harborrag_adapters/models/runtime/health.py +234 -0
- harborrag_adapters/models/runtime/introspection.py +206 -0
- harborrag_adapters/models/runtime/lifecycle.py +78 -0
- harborrag_adapters/models/runtime/litellm_backend.py +111 -0
- harborrag_adapters/models/runtime/litellm_router.py +76 -0
- harborrag_adapters/models/runtime/loading.py +125 -0
- harborrag_adapters/models/runtime/middleware.py +178 -0
- harborrag_adapters/models/runtime/model_config.py +237 -0
- harborrag_adapters/models/runtime/operation_deadline.py +61 -0
- harborrag_adapters/models/runtime/provider.py +284 -0
- harborrag_adapters/models/runtime/provider_validation.py +176 -0
- harborrag_adapters/models/runtime/redis_client.py +198 -0
- harborrag_adapters/models/runtime/redis_config.py +57 -0
- harborrag_adapters/models/runtime/responses.py +28 -0
- harborrag_adapters/models/runtime/retry.py +40 -0
- harborrag_adapters/models/runtime/routing.py +305 -0
- harborrag_adapters/models/runtime/routing_cursor.py +194 -0
- harborrag_adapters/models/runtime/routing_runtime.py +55 -0
- harborrag_adapters/models/runtime/routing_state.py +156 -0
- harborrag_adapters/models/runtime/routing_state_memory.py +210 -0
- harborrag_adapters/models/runtime/routing_state_redis.py +300 -0
- harborrag_adapters/models/runtime/routing_types.py +50 -0
- harborrag_adapters/models/runtime/runtime_services.py +125 -0
- harborrag_adapters/models/runtime/secrets/__init__.py +15 -0
- harborrag_adapters/models/runtime/secrets/aws.py +52 -0
- harborrag_adapters/models/runtime/secrets/azure.py +46 -0
- harborrag_adapters/models/runtime/secrets/base.py +29 -0
- harborrag_adapters/models/runtime/secrets/composite.py +25 -0
- harborrag_adapters/models/runtime/secrets/environment.py +28 -0
- harborrag_adapters/models/runtime/secrets/gcp.py +38 -0
- harborrag_adapters/models/runtime/secrets/vault.py +41 -0
- harborrag_adapters/models/runtime/security.py +238 -0
- harborrag_adapters/models/runtime/singleflight.py +285 -0
- harborrag_adapters/models/runtime/sync.py +111 -0
- harborrag_adapters/models/runtime/telemetry.py +309 -0
- harborrag_adapters/models/runtime/telemetry_adapters.py +283 -0
- harborrag_adapters/models/runtime/telemetry_metrics.py +103 -0
- harborrag_adapters/models/runtime/telemetry_operation.py +339 -0
- harborrag_adapters/models/runtime/transport.py +63 -0
- harborrag_adapters/parsers/README.md +154 -0
- harborrag_adapters/parsers/__init__.py +12 -0
- harborrag_adapters/parsers/common/__init__.py +21 -0
- harborrag_adapters/parsers/common/base.py +139 -0
- harborrag_adapters/parsers/common/config.py +28 -0
- harborrag_adapters/parsers/common/family.py +168 -0
- harborrag_adapters/parsers/common/html_markdown.py +311 -0
- harborrag_adapters/parsers/common/legacy_office.py +128 -0
- harborrag_adapters/parsers/common/limits.py +8 -0
- harborrag_adapters/parsers/common/markdown_tables.py +130 -0
- harborrag_adapters/parsers/common/metadata.py +17 -0
- harborrag_adapters/parsers/common/mime.py +34 -0
- harborrag_adapters/parsers/common/models.py +143 -0
- harborrag_adapters/parsers/common/normalization.py +119 -0
- harborrag_adapters/parsers/common/option_rules.py +110 -0
- harborrag_adapters/parsers/common/quality.py +14 -0
- harborrag_adapters/parsers/common/resources.py +346 -0
- harborrag_adapters/parsers/common/utils.py +90 -0
- harborrag_adapters/parsers/common/validation.py +258 -0
- harborrag_adapters/parsers/compat.py +75 -0
- harborrag_adapters/parsers/document/__init__.py +6 -0
- harborrag_adapters/parsers/document/base.py +15 -0
- harborrag_adapters/parsers/document/config.py +12 -0
- harborrag_adapters/parsers/document/engines/__init__.py +1 -0
- harborrag_adapters/parsers/document/engines/docx/__init__.py +5 -0
- harborrag_adapters/parsers/document/engines/docx/config.py +10 -0
- harborrag_adapters/parsers/document/engines/docx/engine.py +408 -0
- harborrag_adapters/parsers/document/engines/epub/__init__.py +5 -0
- harborrag_adapters/parsers/document/engines/epub/engine.py +373 -0
- harborrag_adapters/parsers/document/engines/msword/__init__.py +5 -0
- harborrag_adapters/parsers/document/engines/msword/config.py +10 -0
- harborrag_adapters/parsers/document/engines/msword/engine.py +163 -0
- harborrag_adapters/parsers/document/engines/msword/text.py +171 -0
- harborrag_adapters/parsers/document/engines/msword/word97.py +213 -0
- harborrag_adapters/parsers/document/engines/odt/__init__.py +5 -0
- harborrag_adapters/parsers/document/engines/odt/config.py +10 -0
- harborrag_adapters/parsers/document/engines/odt/engine.py +130 -0
- harborrag_adapters/parsers/document/normalization.py +12 -0
- harborrag_adapters/parsers/document/parser.py +20 -0
- harborrag_adapters/parsers/document/router.py +13 -0
- harborrag_adapters/parsers/errors.py +224 -0
- harborrag_adapters/parsers/factory.py +199 -0
- harborrag_adapters/parsers/image/__init__.py +6 -0
- harborrag_adapters/parsers/image/base.py +15 -0
- harborrag_adapters/parsers/image/config.py +16 -0
- harborrag_adapters/parsers/image/engines/__init__.py +1 -0
- harborrag_adapters/parsers/image/engines/ocr/__init__.py +5 -0
- harborrag_adapters/parsers/image/engines/ocr/config.py +17 -0
- harborrag_adapters/parsers/image/engines/ocr/engine.py +398 -0
- harborrag_adapters/parsers/image/parser.py +40 -0
- harborrag_adapters/parsers/image/router.py +13 -0
- harborrag_adapters/parsers/markup/__init__.py +6 -0
- harborrag_adapters/parsers/markup/base.py +12 -0
- harborrag_adapters/parsers/markup/config.py +12 -0
- harborrag_adapters/parsers/markup/engines/__init__.py +1 -0
- harborrag_adapters/parsers/markup/engines/html/__init__.py +5 -0
- harborrag_adapters/parsers/markup/engines/html/config.py +12 -0
- harborrag_adapters/parsers/markup/engines/html/engine.py +191 -0
- harborrag_adapters/parsers/markup/engines/markdown/__init__.py +5 -0
- harborrag_adapters/parsers/markup/engines/markdown/config.py +12 -0
- harborrag_adapters/parsers/markup/engines/markdown/engine.py +278 -0
- harborrag_adapters/parsers/markup/normalization.py +19 -0
- harborrag_adapters/parsers/markup/parser.py +22 -0
- harborrag_adapters/parsers/pdf/README.md +143 -0
- harborrag_adapters/parsers/pdf/__init__.py +26 -0
- harborrag_adapters/parsers/pdf/base.py +44 -0
- harborrag_adapters/parsers/pdf/config.py +184 -0
- harborrag_adapters/parsers/pdf/engines/__init__.py +1 -0
- harborrag_adapters/parsers/pdf/engines/docling/__init__.py +6 -0
- harborrag_adapters/parsers/pdf/engines/docling/config.py +39 -0
- harborrag_adapters/parsers/pdf/engines/docling/engine.py +272 -0
- harborrag_adapters/parsers/pdf/engines/docling/utils.py +216 -0
- harborrag_adapters/parsers/pdf/engines/liteparse/__init__.py +8 -0
- harborrag_adapters/parsers/pdf/engines/liteparse/config.py +162 -0
- harborrag_adapters/parsers/pdf/engines/liteparse/engine.py +252 -0
- harborrag_adapters/parsers/pdf/engines/mineru/__init__.py +6 -0
- harborrag_adapters/parsers/pdf/engines/mineru/config.py +81 -0
- harborrag_adapters/parsers/pdf/engines/mineru/engine.py +319 -0
- harborrag_adapters/parsers/pdf/engines/paddleocr/__init__.py +8 -0
- harborrag_adapters/parsers/pdf/engines/paddleocr/config.py +37 -0
- harborrag_adapters/parsers/pdf/engines/paddleocr/engine.py +232 -0
- harborrag_adapters/parsers/pdf/engines/pymupdf/__init__.py +6 -0
- harborrag_adapters/parsers/pdf/engines/pymupdf/config.py +23 -0
- harborrag_adapters/parsers/pdf/engines/pymupdf/engine.py +154 -0
- harborrag_adapters/parsers/pdf/models.py +31 -0
- harborrag_adapters/parsers/pdf/normalization.py +220 -0
- harborrag_adapters/parsers/pdf/page_limit.py +88 -0
- harborrag_adapters/parsers/pdf/parser.py +344 -0
- harborrag_adapters/parsers/pdf/parser_failures.py +40 -0
- harborrag_adapters/parsers/pdf/parser_support.py +122 -0
- harborrag_adapters/parsers/pdf/quality.py +36 -0
- harborrag_adapters/parsers/pdf/resources.py +39 -0
- harborrag_adapters/parsers/pdf/router.py +67 -0
- harborrag_adapters/parsers/pdf/utils.py +24 -0
- harborrag_adapters/parsers/presentation/__init__.py +6 -0
- harborrag_adapters/parsers/presentation/base.py +18 -0
- harborrag_adapters/parsers/presentation/config.py +12 -0
- harborrag_adapters/parsers/presentation/engines/__init__.py +1 -0
- harborrag_adapters/parsers/presentation/engines/msppt/__init__.py +7 -0
- harborrag_adapters/parsers/presentation/engines/msppt/config.py +10 -0
- harborrag_adapters/parsers/presentation/engines/msppt/engine.py +286 -0
- harborrag_adapters/parsers/presentation/engines/msppt/records.py +235 -0
- harborrag_adapters/parsers/presentation/engines/python_pptx/__init__.py +7 -0
- harborrag_adapters/parsers/presentation/engines/python_pptx/config.py +12 -0
- harborrag_adapters/parsers/presentation/engines/python_pptx/engine.py +215 -0
- harborrag_adapters/parsers/presentation/parser.py +25 -0
- harborrag_adapters/parsers/registry.py +303 -0
- harborrag_adapters/parsers/spreadsheet/__init__.py +6 -0
- harborrag_adapters/parsers/spreadsheet/base.py +19 -0
- harborrag_adapters/parsers/spreadsheet/config.py +12 -0
- harborrag_adapters/parsers/spreadsheet/engines/__init__.py +1 -0
- harborrag_adapters/parsers/spreadsheet/engines/csv/__init__.py +5 -0
- harborrag_adapters/parsers/spreadsheet/engines/csv/config.py +12 -0
- harborrag_adapters/parsers/spreadsheet/engines/csv/engine.py +231 -0
- harborrag_adapters/parsers/spreadsheet/engines/openpyxl/__init__.py +7 -0
- harborrag_adapters/parsers/spreadsheet/engines/openpyxl/config.py +12 -0
- harborrag_adapters/parsers/spreadsheet/engines/openpyxl/engine.py +347 -0
- harborrag_adapters/parsers/spreadsheet/engines/openpyxl/rendering.py +111 -0
- harborrag_adapters/parsers/spreadsheet/normalization.py +10 -0
- harborrag_adapters/parsers/spreadsheet/parser.py +25 -0
- harborrag_adapters/parsers/structured/__init__.py +6 -0
- harborrag_adapters/parsers/structured/base.py +12 -0
- harborrag_adapters/parsers/structured/config.py +12 -0
- harborrag_adapters/parsers/structured/engines/__init__.py +1 -0
- harborrag_adapters/parsers/structured/engines/json/__init__.py +5 -0
- harborrag_adapters/parsers/structured/engines/json/config.py +12 -0
- harborrag_adapters/parsers/structured/engines/json/engine.py +167 -0
- harborrag_adapters/parsers/structured/parser.py +25 -0
- harborrag_adapters/parsers/text/__init__.py +6 -0
- harborrag_adapters/parsers/text/base.py +12 -0
- harborrag_adapters/parsers/text/config.py +12 -0
- harborrag_adapters/parsers/text/engines/__init__.py +1 -0
- harborrag_adapters/parsers/text/engines/plain_text/__init__.py +5 -0
- harborrag_adapters/parsers/text/engines/plain_text/engine.py +156 -0
- harborrag_adapters/parsers/text/parser.py +22 -0
- harborrag_adapters/repositories/__init__.py +13 -0
- harborrag_adapters/repositories/backends/__init__.py +3 -0
- harborrag_adapters/repositories/backends/redis.py +87 -0
- harborrag_adapters/repositories/backends/sqlalchemy.py +142 -0
- harborrag_adapters/repositories/backends/sqlite.py +59 -0
- harborrag_adapters/repositories/database/__init__.py +19 -0
- harborrag_adapters/repositories/database/control_plane/__init__.py +1 -0
- harborrag_adapters/repositories/database/control_plane/alembic/baseline/__init__.py +1 -0
- harborrag_adapters/repositories/database/control_plane/alembic/baseline/control_plane.py +176 -0
- harborrag_adapters/repositories/database/control_plane/alembic/baseline/ingestion.py +321 -0
- harborrag_adapters/repositories/database/control_plane/alembic/env.py +52 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0001_initial.py +39 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0002_ingestion_stage.py +28 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0003_removal_reconciliation.py +24 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0004_overlapping_source_scopes.py +24 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0005_reindex_jobs.py +24 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0006_partial_ingestion_tasks.py +24 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0007_ingestion_api.py +24 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0008_authoritative_tenancy.py +202 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0009_conversation_memory.py +64 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0010_agent_runs.py +227 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0011_memories.py +66 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0012_atomic_job_event_sequence.py +42 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0013_control_plane_tenant_identity.py +56 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0014_secret_ciphertext.py +38 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0015_ingestion_task_events.py +56 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0016_pending_control_plane_effects.py +54 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0017_singleton_leases.py +63 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0018_effect_recovery_lease.py +60 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0019_ingestion_task_listing_index.py +44 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0020_graph_conflicts.py +57 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0021_topology_authority.py +155 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0022_topology_permissions_budget.py +192 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0023_summary_projection.py +76 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0024_agent_run_lease.py +48 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0025_conversation_session_kind.py +42 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0026_conversation_messages.py +201 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0027_memories_provenance.py +65 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0028_conversation_user_ownership.py +114 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0029_model_usage.py +67 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0030_conversation_message_partial.py +45 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0031_agent_run_user_ownership.py +70 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0032_tenant_scoped_secrets_and_provider_timestamps.py +123 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0033_conversation_turn_coordination.py +95 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0034_completion_request_claims.py +48 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0035_paused_ingestion_tasks.py +42 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0036_mcp_api_keys.py +51 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0037_task_document_results_status_index.py +40 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0038_purged_document_versions.py +50 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0039_unresolved_source_relations.py +59 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0040_mcp_api_keys_constraints.py +75 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0041_editable_connections.py +292 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0042_connection_revisions.py +56 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0043_project_versions.py +34 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0044_drop_agent_runs.py +67 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0045_drop_agent_traces.py +82 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0046_runtime_settings.py +56 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0047_chat_history_contract.py +48 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0048_drop_tenant_id_server_default.py +66 -0
- harborrag_adapters/repositories/database/control_plane/alembic/versions/0049_clear_raw_connection_secrets.py +252 -0
- harborrag_adapters/repositories/database/control_plane/completion_requests.py +268 -0
- harborrag_adapters/repositories/database/control_plane/config_store.py +205 -0
- harborrag_adapters/repositories/database/control_plane/config_store_feed.py +103 -0
- harborrag_adapters/repositories/database/control_plane/connections.py +316 -0
- harborrag_adapters/repositories/database/control_plane/conversation.py +462 -0
- harborrag_adapters/repositories/database/control_plane/conversation_directory.py +229 -0
- harborrag_adapters/repositories/database/control_plane/conversation_leases.py +144 -0
- harborrag_adapters/repositories/database/control_plane/engine.py +72 -0
- harborrag_adapters/repositories/database/control_plane/graph_conflicts.py +182 -0
- harborrag_adapters/repositories/database/control_plane/jobs.py +185 -0
- harborrag_adapters/repositories/database/control_plane/leases.py +91 -0
- harborrag_adapters/repositories/database/control_plane/mapping.py +86 -0
- harborrag_adapters/repositories/database/control_plane/mcp_api_keys.py +201 -0
- harborrag_adapters/repositories/database/control_plane/memory.py +217 -0
- harborrag_adapters/repositories/database/control_plane/migrations.py +100 -0
- harborrag_adapters/repositories/database/control_plane/model_catalog.py +221 -0
- harborrag_adapters/repositories/database/control_plane/model_catalog_rows.py +222 -0
- harborrag_adapters/repositories/database/control_plane/pending_effects.py +61 -0
- harborrag_adapters/repositories/database/control_plane/projects.py +98 -0
- harborrag_adapters/repositories/database/control_plane/schemas.py +392 -0
- harborrag_adapters/repositories/database/control_plane/schemas_completion_requests.py +31 -0
- harborrag_adapters/repositories/database/control_plane/schemas_conversation_memory.py +191 -0
- harborrag_adapters/repositories/database/control_plane/schemas_settings.py +49 -0
- harborrag_adapters/repositories/database/control_plane/schemas_usage.py +43 -0
- harborrag_adapters/repositories/database/control_plane/secrets.py +103 -0
- harborrag_adapters/repositories/database/control_plane/session.py +3 -0
- harborrag_adapters/repositories/database/control_plane/usage.py +97 -0
- harborrag_adapters/repositories/database/control_plane/workspace.py +176 -0
- harborrag_adapters/repositories/database/ingestion_control/__init__.py +28 -0
- harborrag_adapters/repositories/database/ingestion_control/connection_records.py +193 -0
- harborrag_adapters/repositories/database/ingestion_control/database.py +91 -0
- harborrag_adapters/repositories/database/ingestion_control/document_version_access.py +345 -0
- harborrag_adapters/repositories/database/ingestion_control/document_version_mapping.py +118 -0
- harborrag_adapters/repositories/database/ingestion_control/document_versions.py +397 -0
- harborrag_adapters/repositories/database/ingestion_control/failures.py +312 -0
- harborrag_adapters/repositories/database/ingestion_control/publication.py +266 -0
- harborrag_adapters/repositories/database/ingestion_control/reindex.py +208 -0
- harborrag_adapters/repositories/database/ingestion_control/reliability.py +350 -0
- harborrag_adapters/repositories/database/ingestion_control/removal_reconciliation.py +157 -0
- harborrag_adapters/repositories/database/ingestion_control/retention.py +348 -0
- harborrag_adapters/repositories/database/ingestion_control/row_mapping.py +26 -0
- harborrag_adapters/repositories/database/ingestion_control/row_values.py +72 -0
- harborrag_adapters/repositories/database/ingestion_control/schema.py +352 -0
- harborrag_adapters/repositories/database/ingestion_control/source_catalog.py +273 -0
- harborrag_adapters/repositories/database/ingestion_control/source_item_mapping.py +87 -0
- harborrag_adapters/repositories/database/ingestion_control/source_scans.py +445 -0
- harborrag_adapters/repositories/database/ingestion_control/summary_authority.py +326 -0
- harborrag_adapters/repositories/database/ingestion_control/summary_cache.py +55 -0
- harborrag_adapters/repositories/database/ingestion_control/summary_intent.py +108 -0
- harborrag_adapters/repositories/database/ingestion_control/summary_jobs.py +343 -0
- harborrag_adapters/repositories/database/ingestion_control/summary_maintenance.py +177 -0
- harborrag_adapters/repositories/database/ingestion_control/summary_products.py +138 -0
- harborrag_adapters/repositories/database/ingestion_control/summary_publication.py +193 -0
- harborrag_adapters/repositories/database/ingestion_control/summary_reads.py +286 -0
- harborrag_adapters/repositories/database/ingestion_control/summary_repository.py +19 -0
- harborrag_adapters/repositories/database/ingestion_control/summary_schema.py +53 -0
- harborrag_adapters/repositories/database/ingestion_control/task_events.py +123 -0
- harborrag_adapters/repositories/database/ingestion_control/task_lifecycle.py +377 -0
- harborrag_adapters/repositories/database/ingestion_control/task_results.py +246 -0
- harborrag_adapters/repositories/database/ingestion_control/tasks.py +343 -0
- harborrag_adapters/repositories/database/ingestion_control/tenant_directory.py +64 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/__init__.py +3 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/audit.py +116 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/authorization.py +133 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/budget.py +258 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/builds.py +281 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/configuration.py +188 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/derivations.py +283 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/guards.py +99 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/identity_proofs.py +103 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/intent.py +96 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/jobs.py +273 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/permissions.py +460 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/policy_schema.py +123 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/readiness.py +63 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/reads.py +281 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/reconciliation.py +80 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/repository.py +116 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/resolution.py +205 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/resolution_state.py +84 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/schema.py +127 -0
- harborrag_adapters/repositories/database/ingestion_control/topology/transactions.py +92 -0
- harborrag_adapters/repositories/database/ingestion_control/unresolved_relations.py +209 -0
- harborrag_adapters/repositories/errors.py +207 -0
- harborrag_adapters/repositories/graph/__init__.py +4 -0
- harborrag_adapters/repositories/graph/base.py +98 -0
- harborrag_adapters/repositories/graph/client.py +70 -0
- harborrag_adapters/repositories/graph/falkordb/__init__.py +17 -0
- harborrag_adapters/repositories/graph/falkordb/client.py +202 -0
- harborrag_adapters/repositories/graph/falkordb/config.py +52 -0
- harborrag_adapters/repositories/graph/falkordb/knowledge.py +315 -0
- harborrag_adapters/repositories/graph/falkordb/knowledge_admin.py +397 -0
- harborrag_adapters/repositories/graph/falkordb/knowledge_mapping.py +180 -0
- harborrag_adapters/repositories/graph/falkordb/knowledge_node_resolution.py +158 -0
- harborrag_adapters/repositories/graph/falkordb/knowledge_paths.py +118 -0
- harborrag_adapters/repositories/graph/falkordb/knowledge_provisioning.py +131 -0
- harborrag_adapters/repositories/graph/falkordb/knowledge_queries.py +346 -0
- harborrag_adapters/repositories/graph/falkordb/knowledge_repair.py +115 -0
- harborrag_adapters/repositories/graph/falkordb/knowledge_support.py +148 -0
- harborrag_adapters/repositories/graph/falkordb/knowledge_writes.py +363 -0
- harborrag_adapters/repositories/graph/falkordb/mapping.py +113 -0
- harborrag_adapters/repositories/graph/falkordb/parent_views.py +173 -0
- harborrag_adapters/repositories/graph/falkordb/plugin.py +26 -0
- harborrag_adapters/repositories/graph/falkordb/reader.py +117 -0
- harborrag_adapters/repositories/graph/falkordb/repository.py +312 -0
- harborrag_adapters/repositories/graph/falkordb/tenant_pool.py +183 -0
- harborrag_adapters/repositories/graph/falkordb/topology.py +217 -0
- harborrag_adapters/repositories/graph/falkordb/topology_mapping.py +100 -0
- harborrag_adapters/repositories/graph/falkordb/typed_topology.py +232 -0
- harborrag_adapters/repositories/graph/falkordb/typed_topology_validation.py +43 -0
- harborrag_adapters/repositories/graph/falkordb/unified_topology.py +219 -0
- harborrag_adapters/repositories/graph/falkordb/unified_topology_builder.py +282 -0
- harborrag_adapters/repositories/graph/traversal.py +21 -0
- harborrag_adapters/repositories/lifecycle.py +55 -0
- harborrag_adapters/repositories/object_store/__init__.py +57 -0
- harborrag_adapters/repositories/object_store/base.py +115 -0
- harborrag_adapters/repositories/object_store/body.py +38 -0
- harborrag_adapters/repositories/object_store/canonical_artifacts.py +333 -0
- harborrag_adapters/repositories/object_store/chunk_artifacts.py +214 -0
- harborrag_adapters/repositories/object_store/client.py +81 -0
- harborrag_adapters/repositories/object_store/comment_artifacts.py +193 -0
- harborrag_adapters/repositories/object_store/filesystem/__init__.py +15 -0
- harborrag_adapters/repositories/object_store/filesystem/access.py +276 -0
- harborrag_adapters/repositories/object_store/filesystem/config.py +11 -0
- harborrag_adapters/repositories/object_store/filesystem/plugin.py +29 -0
- harborrag_adapters/repositories/object_store/filesystem/repository.py +373 -0
- harborrag_adapters/repositories/object_store/ingestion_artifacts.py +257 -0
- harborrag_adapters/repositories/object_store/keys.py +35 -0
- harborrag_adapters/repositories/object_store/memory/__init__.py +11 -0
- harborrag_adapters/repositories/object_store/memory/config.py +9 -0
- harborrag_adapters/repositories/object_store/memory/plugin.py +30 -0
- harborrag_adapters/repositories/object_store/memory/repository.py +265 -0
- harborrag_adapters/repositories/object_store/raw_artifacts.py +124 -0
- harborrag_adapters/repositories/object_store/s3/__init__.py +6 -0
- harborrag_adapters/repositories/object_store/s3/body.py +59 -0
- harborrag_adapters/repositories/object_store/s3/client.py +95 -0
- harborrag_adapters/repositories/object_store/s3/config.py +42 -0
- harborrag_adapters/repositories/object_store/s3/object_metadata.py +28 -0
- harborrag_adapters/repositories/object_store/s3/operations.py +452 -0
- harborrag_adapters/repositories/object_store/s3/plugin.py +24 -0
- harborrag_adapters/repositories/object_store/s3/repository.py +191 -0
- harborrag_adapters/repositories/object_store/table_artifacts.py +152 -0
- harborrag_adapters/repositories/plugin.py +49 -0
- harborrag_adapters/repositories/policies/__init__.py +3 -0
- harborrag_adapters/repositories/policies/tenancy.py +21 -0
- harborrag_adapters/repositories/private_directories.py +38 -0
- harborrag_adapters/repositories/registry.py +112 -0
- harborrag_adapters/repositories/telemetry.py +147 -0
- harborrag_adapters/repositories/tool_audit.py +64 -0
- harborrag_adapters/repositories/vector/__init__.py +13 -0
- harborrag_adapters/repositories/vector/base.py +139 -0
- harborrag_adapters/repositories/vector/client.py +70 -0
- harborrag_adapters/repositories/vector/memory_index.py +215 -0
- harborrag_adapters/repositories/vector/memory_index_mapping.py +164 -0
- harborrag_adapters/repositories/vector/qdrant/__init__.py +17 -0
- harborrag_adapters/repositories/vector/qdrant/client.py +102 -0
- harborrag_adapters/repositories/vector/qdrant/collections.py +214 -0
- harborrag_adapters/repositories/vector/qdrant/config.py +72 -0
- harborrag_adapters/repositories/vector/qdrant/errors.py +116 -0
- harborrag_adapters/repositories/vector/qdrant/health.py +48 -0
- harborrag_adapters/repositories/vector/qdrant/mapping.py +124 -0
- harborrag_adapters/repositories/vector/qdrant/plugin.py +26 -0
- harborrag_adapters/repositories/vector/qdrant/query.py +439 -0
- harborrag_adapters/repositories/vector/qdrant/query_mapping.py +210 -0
- harborrag_adapters/repositories/vector/qdrant/repository.py +300 -0
- harborrag_adapters/repositories/vector/qdrant/schema_mapping.py +44 -0
- harborrag_adapters/topology/__init__.py +5 -0
- harborrag_adapters/topology/artifacts.py +111 -0
- harborrag_adapters/topology/descriptions.py +355 -0
- harborrag_adapters/topology/extraction_budget.py +82 -0
- harborrag_adapters/topology/extraction_schema.py +121 -0
- harborrag_adapters/topology/extractor.py +335 -0
- harborrag_adapters-2.0.0.dist-info/METADATA +352 -0
- harborrag_adapters-2.0.0.dist-info/RECORD +603 -0
- harborrag_adapters-2.0.0.dist-info/WHEEL +4 -0
- harborrag_adapters-2.0.0.dist-info/licenses/LICENSE +201 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Structure-aware text refiners implementing core ``TextRefiner``.
|
|
2
|
+
|
|
3
|
+
Engine segments canonical documents and calls a refiner only for a unit that
|
|
4
|
+
is still above its hard token limit. ``build_text_refiner`` returns the
|
|
5
|
+
routing refiner that picks a code, document, text or separator strategy for
|
|
6
|
+
that unit; each strategy only proposes cut positions and the shared base
|
|
7
|
+
enforces limits, coverage, and source spans. ``MarkdownChunker`` chunks a
|
|
8
|
+
whole Markdown file: it splits at sections first and refines each one.
|
|
9
|
+
|
|
10
|
+
Importing this package needs no optional dependency: tree-sitter grammars
|
|
11
|
+
(``chunking-code``), tiktoken (``chunking-tokens``) and LangChain
|
|
12
|
+
(``chunking``) are loaded only when used.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
from .code_structure import CodeStructureRefiner
|
|
16
|
+
from .document_structure import DocumentStructureRefiner
|
|
17
|
+
from .factory import GrammarSetMismatch, build_text_refiner, text_refiner_fingerprint
|
|
18
|
+
from .languages import guess_language, normalize_language
|
|
19
|
+
from .markdown_sections import ChunkRole, MarkdownChunk, MarkdownChunker
|
|
20
|
+
from .recursive import RecursiveTextRefiner
|
|
21
|
+
from .router import RoutingTextRefiner
|
|
22
|
+
from .separators import SeparatorRefiner
|
|
23
|
+
from .text_structure import TextStructureRefiner
|
|
24
|
+
from .tokenizers import TiktokenTokenCounter
|
|
25
|
+
|
|
26
|
+
__all__ = [
|
|
27
|
+
"ChunkRole",
|
|
28
|
+
"CodeStructureRefiner",
|
|
29
|
+
"DocumentStructureRefiner",
|
|
30
|
+
"GrammarSetMismatch",
|
|
31
|
+
"MarkdownChunk",
|
|
32
|
+
"MarkdownChunker",
|
|
33
|
+
"RecursiveTextRefiner",
|
|
34
|
+
"RoutingTextRefiner",
|
|
35
|
+
"SeparatorRefiner",
|
|
36
|
+
"TextStructureRefiner",
|
|
37
|
+
"TiktokenTokenCounter",
|
|
38
|
+
"build_text_refiner",
|
|
39
|
+
"guess_language",
|
|
40
|
+
"normalize_language",
|
|
41
|
+
"text_refiner_fingerprint",
|
|
42
|
+
]
|
|
@@ -0,0 +1,574 @@
|
|
|
1
|
+
"""Template for structure-aware text refiners.
|
|
2
|
+
|
|
3
|
+
A strategy only answers one question -- where may this text be cut, and how
|
|
4
|
+
strong is each cut -- by returning :class:`Cut` values from ``_propose``. The
|
|
5
|
+
base turns those into splits the same way for every strategy:
|
|
6
|
+
|
|
7
|
+
1. split the text at its strongest cuts, recursing into any piece that is still
|
|
8
|
+
over the limit with the next-strongest cuts (structure first);
|
|
9
|
+
2. merge neighbouring pieces that came from the same parent back together while
|
|
10
|
+
they fit (so a split never leaves needlessly small fragments); a piece that
|
|
11
|
+
only introduces what follows (a heading, a comment or decorator) joins the
|
|
12
|
+
piece after it, never the one before;
|
|
13
|
+
3. force-split any piece that has no cuts left with a token-bounded window;
|
|
14
|
+
4. settle pieces still below 1/8 of the limit (a tail such as ``}`` left by a
|
|
15
|
+
split definition, a lone statement, whitespace): join a neighbour when the
|
|
16
|
+
pair fits, otherwise move the boundary to a cut inside the neighbour that is
|
|
17
|
+
at least as strong as the one it replaces, so both sides are big enough;
|
|
18
|
+
5. recount every split with the injected counter, apply overlap, check that the
|
|
19
|
+
returned splits tile the source, and map them to source spans.
|
|
20
|
+
|
|
21
|
+
Only pieces cut by the token window are marked ``forced_split`` (boundary kind
|
|
22
|
+
``FORCED``); structural splits carry the refiner's ``split_kind``.
|
|
23
|
+
|
|
24
|
+
Library token counts are never trusted: engine rejects any split above the
|
|
25
|
+
limit under its own counter, so the counter injected here is the only judge.
|
|
26
|
+
"""
|
|
27
|
+
|
|
28
|
+
from __future__ import annotations
|
|
29
|
+
|
|
30
|
+
from abc import ABC, abstractmethod
|
|
31
|
+
from bisect import bisect_left, bisect_right
|
|
32
|
+
from collections.abc import Sequence
|
|
33
|
+
from dataclasses import dataclass, field, replace
|
|
34
|
+
from typing import ClassVar
|
|
35
|
+
|
|
36
|
+
from harborrag_core.contracts.chunking import (
|
|
37
|
+
SplitBoundaryKind,
|
|
38
|
+
TextRefinementRequest,
|
|
39
|
+
TextSplit,
|
|
40
|
+
TokenCounter,
|
|
41
|
+
)
|
|
42
|
+
from harborrag_core.ingestion import ChunkValidationError
|
|
43
|
+
|
|
44
|
+
from ._spans import source_span, validate_coverage
|
|
45
|
+
|
|
46
|
+
# A piece below maximum_tokens // SMALL_PIECE_DIVISOR is settled with a neighbour.
|
|
47
|
+
SMALL_PIECE_DIVISOR = 8
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
@dataclass(frozen=True, slots=True)
|
|
51
|
+
class Cut:
|
|
52
|
+
"""A position where ``request.content`` may be split.
|
|
53
|
+
|
|
54
|
+
``offset`` is the index of the first character of the following piece.
|
|
55
|
+
Lower ``priority`` is a stronger boundary. ``scope`` describes the piece that
|
|
56
|
+
starts at this offset (code refiners record enclosing definitions here).
|
|
57
|
+
``leads`` marks that piece as one that only introduces what follows it (a
|
|
58
|
+
comment or decorator), so it joins the next piece rather than the previous;
|
|
59
|
+
``after_lead`` says the same of the piece that *ends* here (needed when that
|
|
60
|
+
piece is the first of its run and so has no cut of its own before it).
|
|
61
|
+
"""
|
|
62
|
+
|
|
63
|
+
offset: int
|
|
64
|
+
priority: int
|
|
65
|
+
scope: tuple[str, ...] = ()
|
|
66
|
+
leads: bool = False
|
|
67
|
+
after_lead: bool = False
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
@dataclass(frozen=True, slots=True)
|
|
71
|
+
class _CutIndex:
|
|
72
|
+
"""Cuts sorted by offset, with their offsets for range lookups."""
|
|
73
|
+
|
|
74
|
+
cuts: list[Cut]
|
|
75
|
+
offsets: list[int] = field(init=False)
|
|
76
|
+
|
|
77
|
+
def __post_init__(self) -> None:
|
|
78
|
+
object.__setattr__(self, "offsets", [cut.offset for cut in self.cuts])
|
|
79
|
+
|
|
80
|
+
def inside(self, start: int, end: int) -> list[Cut]:
|
|
81
|
+
return self.cuts[bisect_right(self.offsets, start) : bisect_left(self.offsets, end)]
|
|
82
|
+
|
|
83
|
+
def at(self, offset: int) -> Cut | None:
|
|
84
|
+
position = bisect_left(self.offsets, offset)
|
|
85
|
+
if position < len(self.offsets) and self.offsets[position] == offset:
|
|
86
|
+
return self.cuts[position]
|
|
87
|
+
return None
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
@dataclass(frozen=True, slots=True)
|
|
91
|
+
class _Piece:
|
|
92
|
+
start: int
|
|
93
|
+
end: int
|
|
94
|
+
scope: tuple[str, ...]
|
|
95
|
+
tokens: int
|
|
96
|
+
forced: bool = False # cut by the token window, not at a structural boundary
|
|
97
|
+
leads: bool = False # only introduces what follows (see Cut.leads)
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class BaseTextRefiner(ABC):
|
|
101
|
+
"""Implement core ``TextRefiner`` for strategies that propose cut positions."""
|
|
102
|
+
|
|
103
|
+
name: ClassVar[str]
|
|
104
|
+
version: ClassVar[str]
|
|
105
|
+
# Boundary kind of a structural (not forced) split; None keeps the request's kind.
|
|
106
|
+
split_kind: ClassVar[SplitBoundaryKind | None] = None
|
|
107
|
+
|
|
108
|
+
def __init__(self, token_counter: TokenCounter) -> None:
|
|
109
|
+
self._token_counter = token_counter
|
|
110
|
+
|
|
111
|
+
def fingerprint(self) -> str:
|
|
112
|
+
"""Identify the boundary behaviour for processing-identity hashes."""
|
|
113
|
+
|
|
114
|
+
return f"{self.name}:{self.version}"
|
|
115
|
+
|
|
116
|
+
def split(self, request: TextRefinementRequest) -> tuple[TextSplit, ...]:
|
|
117
|
+
"""Return ordered splits that each fit ``request.maximum_tokens``."""
|
|
118
|
+
|
|
119
|
+
content = request.content
|
|
120
|
+
if not content.strip():
|
|
121
|
+
return ()
|
|
122
|
+
total = self._count(content)
|
|
123
|
+
if total < 1:
|
|
124
|
+
return ()
|
|
125
|
+
if total <= request.maximum_tokens:
|
|
126
|
+
return (
|
|
127
|
+
TextSplit(
|
|
128
|
+
content=content,
|
|
129
|
+
token_count=total,
|
|
130
|
+
source_span=request.source_span,
|
|
131
|
+
boundary_kind=request.boundary_kind,
|
|
132
|
+
structural_path=request.structural_path,
|
|
133
|
+
),
|
|
134
|
+
)
|
|
135
|
+
|
|
136
|
+
index = _CutIndex(self._normalized_cuts(self._propose(request), len(content)))
|
|
137
|
+
# Pack below the limit by the overlap so each split has room to repeat
|
|
138
|
+
# the end of the previous one without breaking the limit.
|
|
139
|
+
packing = _Packing(
|
|
140
|
+
content,
|
|
141
|
+
max(request.maximum_tokens - request.overlap_tokens, 1),
|
|
142
|
+
index,
|
|
143
|
+
self,
|
|
144
|
+
)
|
|
145
|
+
pieces = packing.settle_small(packing.descend(_Piece(0, len(content), (), total)))
|
|
146
|
+
ranges = self._fold_invisible(content, self._with_overlap(request, pieces, index.offsets))
|
|
147
|
+
|
|
148
|
+
results: list[TextSplit] = []
|
|
149
|
+
for start, end, piece in ranges:
|
|
150
|
+
text = content[start:end]
|
|
151
|
+
count = self._count(text)
|
|
152
|
+
if count > request.maximum_tokens:
|
|
153
|
+
raise ChunkValidationError(
|
|
154
|
+
f"{self.name} produced a split above maximum_tokens ({count})"
|
|
155
|
+
)
|
|
156
|
+
results.append(
|
|
157
|
+
TextSplit(
|
|
158
|
+
content=text,
|
|
159
|
+
token_count=count,
|
|
160
|
+
source_span=source_span(request, start, end),
|
|
161
|
+
boundary_kind=SplitBoundaryKind.FORCED
|
|
162
|
+
if piece.forced
|
|
163
|
+
else self.split_kind or request.boundary_kind,
|
|
164
|
+
structural_path=request.structural_path,
|
|
165
|
+
prefix="\n".join(piece.scope) if piece.scope else None,
|
|
166
|
+
forced_split=piece.forced,
|
|
167
|
+
)
|
|
168
|
+
)
|
|
169
|
+
if not results:
|
|
170
|
+
raise ChunkValidationError(f"{self.name} returned no content")
|
|
171
|
+
# Checked on what is returned, so no piece can be dropped unnoticed.
|
|
172
|
+
validate_coverage([(start, end) for start, end, _ in ranges], len(content))
|
|
173
|
+
return tuple(results)
|
|
174
|
+
|
|
175
|
+
def _fold_invisible(
|
|
176
|
+
self,
|
|
177
|
+
content: str,
|
|
178
|
+
ranges: list[tuple[int, int, _Piece]],
|
|
179
|
+
) -> list[tuple[int, int, _Piece]]:
|
|
180
|
+
"""Join ranges the counter sees as zero tokens (e.g. whitespace) to a neighbour.
|
|
181
|
+
|
|
182
|
+
A split must count at least one token, so such a range cannot stand
|
|
183
|
+
alone; dropping it would leave a gap in the source.
|
|
184
|
+
"""
|
|
185
|
+
|
|
186
|
+
folded: list[tuple[int, int, _Piece]] = []
|
|
187
|
+
pending: int | None = None
|
|
188
|
+
for start, end, piece in ranges:
|
|
189
|
+
if self._count(content[start:end]) < 1:
|
|
190
|
+
if folded:
|
|
191
|
+
folded[-1] = (folded[-1][0], end, folded[-1][2])
|
|
192
|
+
elif pending is None:
|
|
193
|
+
pending = start
|
|
194
|
+
continue
|
|
195
|
+
folded.append((pending if pending is not None else start, end, piece))
|
|
196
|
+
pending = None
|
|
197
|
+
return folded
|
|
198
|
+
|
|
199
|
+
@abstractmethod
|
|
200
|
+
def _propose(self, request: TextRefinementRequest) -> Sequence[Cut]:
|
|
201
|
+
"""Return every position where the request's content may be cut."""
|
|
202
|
+
|
|
203
|
+
def _count(self, text: str) -> int:
|
|
204
|
+
return self._token_counter.count(text)
|
|
205
|
+
|
|
206
|
+
def _leads(self, text: str) -> bool:
|
|
207
|
+
"""Report whether ``text`` only introduces what follows it (a bare heading).
|
|
208
|
+
|
|
209
|
+
Such a piece is joined to the next piece instead of trailing the
|
|
210
|
+
previous one.
|
|
211
|
+
"""
|
|
212
|
+
|
|
213
|
+
return False
|
|
214
|
+
|
|
215
|
+
@staticmethod
|
|
216
|
+
def _normalized_cuts(cuts: Sequence[Cut], size: int) -> list[Cut]:
|
|
217
|
+
"""Keep one cut per offset (the strongest), strictly inside the text."""
|
|
218
|
+
|
|
219
|
+
best: dict[int, Cut] = {}
|
|
220
|
+
for cut in cuts:
|
|
221
|
+
if not 0 < cut.offset < size:
|
|
222
|
+
continue
|
|
223
|
+
current = best.get(cut.offset)
|
|
224
|
+
if current is None or cut.priority < current.priority:
|
|
225
|
+
best[cut.offset] = cut
|
|
226
|
+
return [best[offset] for offset in sorted(best)]
|
|
227
|
+
|
|
228
|
+
def _with_overlap(
|
|
229
|
+
self,
|
|
230
|
+
request: TextRefinementRequest,
|
|
231
|
+
pieces: list[_Piece],
|
|
232
|
+
offsets: list[int],
|
|
233
|
+
) -> list[tuple[int, int, _Piece]]:
|
|
234
|
+
"""Extend each split backwards over at most ``overlap_tokens`` of context.
|
|
235
|
+
|
|
236
|
+
Overlap starts only at a proposed cut so it never begins mid-word, and is
|
|
237
|
+
skipped whenever it would break the limit. Callers that must not repeat
|
|
238
|
+
content (line-preserving roles) ask for zero overlap.
|
|
239
|
+
"""
|
|
240
|
+
|
|
241
|
+
overlap = request.overlap_tokens
|
|
242
|
+
if overlap < 1:
|
|
243
|
+
return [(piece.start, piece.end, piece) for piece in pieces]
|
|
244
|
+
content = request.content
|
|
245
|
+
ranges: list[tuple[int, int, _Piece]] = []
|
|
246
|
+
previous_start = 0
|
|
247
|
+
for position, piece in enumerate(pieces):
|
|
248
|
+
candidates: list[int] = []
|
|
249
|
+
if position:
|
|
250
|
+
lower = bisect_right(offsets, previous_start)
|
|
251
|
+
upper = bisect_left(offsets, piece.start)
|
|
252
|
+
for candidate in reversed(offsets[lower:upper]):
|
|
253
|
+
if self._count(content[candidate : piece.start]) > overlap:
|
|
254
|
+
break
|
|
255
|
+
candidates.append(candidate)
|
|
256
|
+
start = piece.start
|
|
257
|
+
# Furthest-back first; step forward only if the limit is broken.
|
|
258
|
+
for candidate in reversed(candidates):
|
|
259
|
+
if self._count(content[candidate : piece.end]) <= request.maximum_tokens:
|
|
260
|
+
start = candidate
|
|
261
|
+
break
|
|
262
|
+
ranges.append((start, piece.end, piece))
|
|
263
|
+
previous_start = piece.start
|
|
264
|
+
return ranges
|
|
265
|
+
|
|
266
|
+
|
|
267
|
+
class _Packing:
|
|
268
|
+
"""Structure-first descent, sibling merging and windowing for one request."""
|
|
269
|
+
|
|
270
|
+
def __init__(
|
|
271
|
+
self,
|
|
272
|
+
content: str,
|
|
273
|
+
limit: int,
|
|
274
|
+
index: _CutIndex,
|
|
275
|
+
refiner: BaseTextRefiner,
|
|
276
|
+
) -> None:
|
|
277
|
+
self._content = content
|
|
278
|
+
self._limit = limit
|
|
279
|
+
self._index = index
|
|
280
|
+
self._refiner = refiner
|
|
281
|
+
self._hidden: set[int] = set() # cut offsets ignored while descending a joined range
|
|
282
|
+
|
|
283
|
+
def _count(self, start: int, end: int) -> int:
|
|
284
|
+
return self._refiner._count(self._content[start:end])
|
|
285
|
+
|
|
286
|
+
def descend(self, span: _Piece) -> list[_Piece]:
|
|
287
|
+
if span.tokens <= self._limit:
|
|
288
|
+
return [span]
|
|
289
|
+
|
|
290
|
+
inside = [
|
|
291
|
+
cut
|
|
292
|
+
for cut in self._index.inside(span.start, span.end)
|
|
293
|
+
if cut.offset not in self._hidden
|
|
294
|
+
]
|
|
295
|
+
if not inside:
|
|
296
|
+
return self._window(span)
|
|
297
|
+
|
|
298
|
+
strongest = min(cut.priority for cut in inside)
|
|
299
|
+
level = [cut for cut in inside if cut.priority == strongest]
|
|
300
|
+
bounds = [(span.start, span.scope, span.leads)] + [
|
|
301
|
+
(cut.offset, cut.scope, cut.leads) for cut in level
|
|
302
|
+
]
|
|
303
|
+
children: list[list[_Piece]] = []
|
|
304
|
+
for position, (child_start, child_scope, leads) in enumerate(bounds):
|
|
305
|
+
child_end = bounds[position + 1][0] if position + 1 < len(bounds) else span.end
|
|
306
|
+
leads = leads or (position < len(level) and level[position].after_lead)
|
|
307
|
+
tokens = self._count(child_start, child_end)
|
|
308
|
+
child = _Piece(child_start, child_end, child_scope, tokens, leads=leads)
|
|
309
|
+
children.append(self.descend(child))
|
|
310
|
+
return self._merge_siblings(children)
|
|
311
|
+
|
|
312
|
+
def _merge_siblings(self, children: list[list[_Piece]]) -> list[_Piece]:
|
|
313
|
+
"""Greedily rejoin whole siblings while they fit.
|
|
314
|
+
|
|
315
|
+
A child that itself had to be split stays as its own run of pieces; the
|
|
316
|
+
only merge into it is a small preceding sibling joining its first piece,
|
|
317
|
+
which removes a cut without creating a new one. Its tail is never
|
|
318
|
+
extended into the next sibling, so the end of one definition is not
|
|
319
|
+
glued onto an unrelated one (a tail that ends up tiny is settled later,
|
|
320
|
+
see ``settle_small``). A sibling that leads (a bare heading, a comment
|
|
321
|
+
or decorator: ``Cut.leads`` or the refiner's ``_leads``) never joins the
|
|
322
|
+
piece before it, only the one after.
|
|
323
|
+
|
|
324
|
+
Merged sizes are estimated by adding piece counts, and every merged
|
|
325
|
+
piece is then counted once for real. A counter that is not additive
|
|
326
|
+
(a BPE merge across the join) falls back to exact counting for that run.
|
|
327
|
+
"""
|
|
328
|
+
|
|
329
|
+
merged = self._merge(children, exact=False)
|
|
330
|
+
if all(piece.tokens <= self._limit for piece in merged):
|
|
331
|
+
return merged
|
|
332
|
+
return self._merge(children, exact=True)
|
|
333
|
+
|
|
334
|
+
def _merge(self, children: list[list[_Piece]], *, exact: bool) -> list[_Piece]:
|
|
335
|
+
merged: list[_Piece] = []
|
|
336
|
+
open_piece: _Piece | None = None
|
|
337
|
+
pending: _Piece | None = None # a run of leading pieces waiting for what they introduce
|
|
338
|
+
|
|
339
|
+
def append(unit: _Piece) -> None:
|
|
340
|
+
nonlocal open_piece
|
|
341
|
+
joined = self._join(open_piece, unit, exact=exact) if open_piece is not None else None
|
|
342
|
+
if joined is None:
|
|
343
|
+
if open_piece is not None:
|
|
344
|
+
merged.append(self._settle(open_piece))
|
|
345
|
+
open_piece = unit
|
|
346
|
+
else:
|
|
347
|
+
open_piece = joined
|
|
348
|
+
|
|
349
|
+
def introduce(unit: _Piece) -> _Piece:
|
|
350
|
+
"""Prefix the pending leading run to ``unit`` when both fit together."""
|
|
351
|
+
nonlocal pending
|
|
352
|
+
if pending is None:
|
|
353
|
+
return unit
|
|
354
|
+
joined = self._join(pending, unit, exact=exact)
|
|
355
|
+
if joined is None: # they cannot share a piece: the run stands on its own
|
|
356
|
+
append(pending)
|
|
357
|
+
pending = None
|
|
358
|
+
return unit
|
|
359
|
+
pending = None
|
|
360
|
+
return joined
|
|
361
|
+
|
|
362
|
+
for child in children:
|
|
363
|
+
if len(child) == 1:
|
|
364
|
+
piece = child[0]
|
|
365
|
+
if piece.leads or self._refiner._leads(self._content[piece.start : piece.end]):
|
|
366
|
+
piece = piece if piece.leads else replace(piece, leads=True)
|
|
367
|
+
if pending is None:
|
|
368
|
+
pending = piece
|
|
369
|
+
continue
|
|
370
|
+
run = self._join(pending, piece, exact=exact)
|
|
371
|
+
if run is None:
|
|
372
|
+
append(pending)
|
|
373
|
+
run = piece
|
|
374
|
+
pending = run
|
|
375
|
+
continue
|
|
376
|
+
append(introduce(piece))
|
|
377
|
+
continue
|
|
378
|
+
first, *rest = child
|
|
379
|
+
if pending is not None and self._join(pending, first, exact=exact) is None:
|
|
380
|
+
# Split the leading run and the child as one unit, so the comment or
|
|
381
|
+
# decorator opens the child's first piece instead of being cut off.
|
|
382
|
+
first, *rest = self._descend_together(pending, child)
|
|
383
|
+
pending = None
|
|
384
|
+
first = introduce(first)
|
|
385
|
+
if open_piece is not None:
|
|
386
|
+
joined = self._join(open_piece, first, exact=exact)
|
|
387
|
+
if joined is None:
|
|
388
|
+
merged.append(self._settle(open_piece))
|
|
389
|
+
else:
|
|
390
|
+
first = joined
|
|
391
|
+
open_piece = None
|
|
392
|
+
merged.extend((self._settle(first), *rest))
|
|
393
|
+
if pending is not None: # nothing follows the leading run: let it trail
|
|
394
|
+
append(pending)
|
|
395
|
+
if open_piece is not None:
|
|
396
|
+
merged.append(self._settle(open_piece))
|
|
397
|
+
return merged
|
|
398
|
+
|
|
399
|
+
def _descend_together(self, run: _Piece, child: list[_Piece]) -> list[_Piece]:
|
|
400
|
+
"""Descend ``run`` + ``child`` as one range, ignoring the cuts that separate them."""
|
|
401
|
+
|
|
402
|
+
start, end = run.start, child[-1].end
|
|
403
|
+
hidden = {cut.offset for cut in self._index.inside(start, child[0].start + 1)}
|
|
404
|
+
saved, self._hidden = self._hidden, self._hidden | hidden
|
|
405
|
+
try:
|
|
406
|
+
return self.descend(_Piece(start, end, run.scope, self._count(start, end)))
|
|
407
|
+
finally:
|
|
408
|
+
self._hidden = saved
|
|
409
|
+
|
|
410
|
+
def _join(self, left: _Piece, right: _Piece, *, exact: bool) -> _Piece | None:
|
|
411
|
+
tokens = self._count(left.start, right.end) if exact else left.tokens + right.tokens
|
|
412
|
+
if tokens > self._limit:
|
|
413
|
+
return None
|
|
414
|
+
return _Piece(
|
|
415
|
+
left.start,
|
|
416
|
+
right.end,
|
|
417
|
+
_common_prefix(left.scope, right.scope),
|
|
418
|
+
tokens,
|
|
419
|
+
forced=left.forced or right.forced,
|
|
420
|
+
leads=left.leads and right.leads,
|
|
421
|
+
)
|
|
422
|
+
|
|
423
|
+
def _settle(self, piece: _Piece) -> _Piece:
|
|
424
|
+
"""Replace an estimated count with the real one."""
|
|
425
|
+
|
|
426
|
+
return replace(piece, tokens=self._count(piece.start, piece.end))
|
|
427
|
+
|
|
428
|
+
def _window(self, span: _Piece) -> list[_Piece]:
|
|
429
|
+
"""Force-split a range with no structural cuts at the token limit.
|
|
430
|
+
|
|
431
|
+
The bound gallops out from the limit (a token is at least one
|
|
432
|
+
character for practical counters), then a binary search finds the
|
|
433
|
+
longest prefix that fits, so each window costs about its own length.
|
|
434
|
+
"""
|
|
435
|
+
|
|
436
|
+
content = self._content
|
|
437
|
+
pieces: list[_Piece] = []
|
|
438
|
+
position = span.start
|
|
439
|
+
end = span.end
|
|
440
|
+
while position < end:
|
|
441
|
+
remaining = self._count(position, end) if end - position <= self._limit * 8 else None
|
|
442
|
+
if remaining is not None and remaining <= self._limit:
|
|
443
|
+
pieces.append(_Piece(position, end, span.scope, remaining, forced=bool(pieces)))
|
|
444
|
+
break
|
|
445
|
+
low = position
|
|
446
|
+
step = max(self._limit, 1)
|
|
447
|
+
high = min(position + step, end)
|
|
448
|
+
while high < end and self._count(position, high) <= self._limit:
|
|
449
|
+
low = high
|
|
450
|
+
step *= 2
|
|
451
|
+
high = min(position + step, end)
|
|
452
|
+
low = max(low, position + 1)
|
|
453
|
+
while low < high:
|
|
454
|
+
middle = (low + high + 1) // 2
|
|
455
|
+
if self._count(position, middle) <= self._limit:
|
|
456
|
+
low = middle
|
|
457
|
+
else:
|
|
458
|
+
high = middle - 1
|
|
459
|
+
stop = low
|
|
460
|
+
tokens = self._count(position, stop)
|
|
461
|
+
if tokens > self._limit:
|
|
462
|
+
raise ChunkValidationError(
|
|
463
|
+
f"{self._refiner.name} cannot fit a single character within "
|
|
464
|
+
f"maximum_tokens={self._limit}"
|
|
465
|
+
)
|
|
466
|
+
if (
|
|
467
|
+
stop - 1 > position
|
|
468
|
+
and content[stop - 1] == "\r"
|
|
469
|
+
and content[stop : stop + 1] == "\n"
|
|
470
|
+
):
|
|
471
|
+
stop -= 1
|
|
472
|
+
tokens = self._count(position, stop)
|
|
473
|
+
pieces.append(_Piece(position, stop, span.scope, tokens, forced=True))
|
|
474
|
+
position = stop
|
|
475
|
+
return pieces
|
|
476
|
+
|
|
477
|
+
def settle_small(self, pieces: list[_Piece]) -> list[_Piece]:
|
|
478
|
+
"""Join or rebalance pieces below ``limit // SMALL_PIECE_DIVISOR`` with a neighbour.
|
|
479
|
+
|
|
480
|
+
For each small piece, in order: join the previous piece if the pair
|
|
481
|
+
fits; else move the boundary back to a cut inside the previous piece;
|
|
482
|
+
else join the next piece; else move the boundary forward into it. A
|
|
483
|
+
moved boundary only lands on a cut at least as strong as the one it
|
|
484
|
+
replaces, so a definition that fits is never cut to make room. A small
|
|
485
|
+
piece that none of these help is kept.
|
|
486
|
+
"""
|
|
487
|
+
|
|
488
|
+
minimum = max(1, self._limit // SMALL_PIECE_DIVISOR)
|
|
489
|
+
settled = list(pieces)
|
|
490
|
+
kept: set[int] = set()
|
|
491
|
+
while len(settled) > 1:
|
|
492
|
+
small = next(
|
|
493
|
+
(
|
|
494
|
+
position
|
|
495
|
+
for position, piece in enumerate(settled)
|
|
496
|
+
if piece.tokens < minimum and piece.start not in kept
|
|
497
|
+
),
|
|
498
|
+
None,
|
|
499
|
+
)
|
|
500
|
+
if small is None:
|
|
501
|
+
break
|
|
502
|
+
if not self._absorb(settled, small, minimum):
|
|
503
|
+
kept.add(settled[small].start)
|
|
504
|
+
return settled
|
|
505
|
+
|
|
506
|
+
def _absorb(self, pieces: list[_Piece], small: int, minimum: int) -> bool:
|
|
507
|
+
for low in (small - 1, small): # the pair (previous, small), then (small, next)
|
|
508
|
+
high = low + 1
|
|
509
|
+
if low < 0 or high >= len(pieces):
|
|
510
|
+
continue
|
|
511
|
+
left, right = pieces[low], pieces[high]
|
|
512
|
+
tokens = self._count(left.start, right.end)
|
|
513
|
+
if tokens <= self._limit:
|
|
514
|
+
# The scope names what encloses the piece's start, where a reader lands.
|
|
515
|
+
pieces[low : high + 1] = [
|
|
516
|
+
_Piece(
|
|
517
|
+
left.start,
|
|
518
|
+
right.end,
|
|
519
|
+
left.scope,
|
|
520
|
+
tokens,
|
|
521
|
+
forced=left.forced or right.forced,
|
|
522
|
+
)
|
|
523
|
+
]
|
|
524
|
+
return True
|
|
525
|
+
pair = self._rebalance(
|
|
526
|
+
left, right, inside=left if low < small else right, minimum=minimum
|
|
527
|
+
)
|
|
528
|
+
if pair is not None:
|
|
529
|
+
pieces[low : high + 1] = pair
|
|
530
|
+
return True
|
|
531
|
+
return False
|
|
532
|
+
|
|
533
|
+
def _rebalance(
|
|
534
|
+
self, left: _Piece, right: _Piece, *, inside: _Piece, minimum: int
|
|
535
|
+
) -> list[_Piece] | None:
|
|
536
|
+
"""Re-cut ``left + right`` at a cut inside ``inside`` so both sides fit and are not small.
|
|
537
|
+
|
|
538
|
+
The replaced boundary is the cut at ``right.start``; only cuts at least
|
|
539
|
+
as strong qualify. The strongest wins, then the most even pair.
|
|
540
|
+
"""
|
|
541
|
+
|
|
542
|
+
boundary = self._index.at(right.start)
|
|
543
|
+
ceiling = boundary.priority if boundary is not None else None
|
|
544
|
+
by_priority: dict[int, list[Cut]] = {}
|
|
545
|
+
for cut in self._index.inside(inside.start, inside.end):
|
|
546
|
+
if ceiling is None or cut.priority <= ceiling:
|
|
547
|
+
by_priority.setdefault(cut.priority, []).append(cut)
|
|
548
|
+
best: tuple[int, Cut, int, int] | None = None
|
|
549
|
+
for priority in sorted(by_priority): # strongest level with a valid cut wins
|
|
550
|
+
for cut in by_priority[priority]:
|
|
551
|
+
first = self._count(left.start, cut.offset)
|
|
552
|
+
second = self._count(cut.offset, right.end)
|
|
553
|
+
if first > self._limit or second > self._limit or min(first, second) < minimum:
|
|
554
|
+
continue
|
|
555
|
+
if best is None or abs(first - second) < best[0]:
|
|
556
|
+
best = (abs(first - second), cut, first, second)
|
|
557
|
+
if best is not None:
|
|
558
|
+
break
|
|
559
|
+
if best is None:
|
|
560
|
+
return None
|
|
561
|
+
_, cut, first, second = best
|
|
562
|
+
return [
|
|
563
|
+
_Piece(left.start, cut.offset, left.scope, first, forced=left.forced),
|
|
564
|
+
_Piece(cut.offset, right.end, cut.scope, second, forced=right.forced),
|
|
565
|
+
]
|
|
566
|
+
|
|
567
|
+
|
|
568
|
+
def _common_prefix(left: tuple[str, ...], right: tuple[str, ...]) -> tuple[str, ...]:
|
|
569
|
+
shared: list[str] = []
|
|
570
|
+
for a, b in zip(left, right, strict=False):
|
|
571
|
+
if a != b:
|
|
572
|
+
break
|
|
573
|
+
shared.append(a)
|
|
574
|
+
return tuple(shared)
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
"""Source-span mapping and coverage checks shared by every text refiner."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from collections.abc import Sequence
|
|
6
|
+
|
|
7
|
+
from harborrag_core.contracts.chunking import SourceSpan, TextRefinementRequest
|
|
8
|
+
from harborrag_core.ingestion import ChunkValidationError
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def source_span(request: TextRefinementRequest, start: int, end: int) -> SourceSpan:
|
|
12
|
+
"""Map a half-open ``[start, end)`` range of ``request.content`` to the source.
|
|
13
|
+
|
|
14
|
+
Offsets are rebased on the request's own span; lines are inclusive, so the
|
|
15
|
+
end line is the line of the last character in the range.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
base = request.source_span
|
|
19
|
+
base_offset = base.start_offset if base and base.start_offset is not None else 0
|
|
20
|
+
|
|
21
|
+
start_line: int | None = None
|
|
22
|
+
end_line: int | None = None
|
|
23
|
+
if base and base.start_line is not None:
|
|
24
|
+
start_line = base.start_line + request.content.count("\n", 0, start)
|
|
25
|
+
end_line = base.start_line + request.content.count("\n", 0, max(end - 1, start))
|
|
26
|
+
|
|
27
|
+
return SourceSpan(
|
|
28
|
+
start_offset=base_offset + start,
|
|
29
|
+
end_offset=base_offset + end,
|
|
30
|
+
start_line=start_line,
|
|
31
|
+
end_line=end_line,
|
|
32
|
+
page_start=base.page_start if base else None,
|
|
33
|
+
page_end=base.page_end if base else None,
|
|
34
|
+
element_ids=base.element_ids if base else (),
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
def validate_coverage(ranges: Sequence[tuple[int, int]], source_size: int) -> None:
|
|
39
|
+
"""Require ordered ranges that leave no part of the source uncovered.
|
|
40
|
+
|
|
41
|
+
Ranges may overlap (refinement overlap), but never go backwards, skip
|
|
42
|
+
content, or run past the source.
|
|
43
|
+
"""
|
|
44
|
+
|
|
45
|
+
coverage_end = 0
|
|
46
|
+
last_start = -1
|
|
47
|
+
for start, end in ranges:
|
|
48
|
+
if start < 0 or end <= start or end > source_size:
|
|
49
|
+
raise ChunkValidationError(
|
|
50
|
+
"text split contains invalid source offsets: "
|
|
51
|
+
f"start={start}, end={end}, source_size={source_size}"
|
|
52
|
+
)
|
|
53
|
+
if start < last_start:
|
|
54
|
+
raise ChunkValidationError(
|
|
55
|
+
"text split source offsets are not ordered: "
|
|
56
|
+
f"start={start} precedes previous start={last_start}"
|
|
57
|
+
)
|
|
58
|
+
if start > coverage_end:
|
|
59
|
+
raise ChunkValidationError(
|
|
60
|
+
f"text split lost source content between offset {coverage_end} and {start}"
|
|
61
|
+
)
|
|
62
|
+
last_start = start
|
|
63
|
+
coverage_end = max(coverage_end, end)
|
|
64
|
+
if coverage_end != source_size:
|
|
65
|
+
raise ChunkValidationError(
|
|
66
|
+
f"text split lost trailing source content: covered {coverage_end} of {source_size}"
|
|
67
|
+
)
|