hindsight-api 0.2.1__py3-none-any.whl → 0.4.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.
- hindsight_api/admin/__init__.py +1 -0
- hindsight_api/admin/cli.py +311 -0
- hindsight_api/alembic/versions/f1a2b3c4d5e6_add_memory_links_composite_index.py +44 -0
- hindsight_api/alembic/versions/g2a3b4c5d6e7_add_tags_column.py +48 -0
- hindsight_api/alembic/versions/h3c4d5e6f7g8_mental_models_v4.py +112 -0
- hindsight_api/alembic/versions/i4d5e6f7g8h9_delete_opinions.py +41 -0
- hindsight_api/alembic/versions/j5e6f7g8h9i0_mental_model_versions.py +95 -0
- hindsight_api/alembic/versions/k6f7g8h9i0j1_add_directive_subtype.py +58 -0
- hindsight_api/alembic/versions/l7g8h9i0j1k2_add_worker_columns.py +109 -0
- hindsight_api/alembic/versions/m8h9i0j1k2l3_mental_model_id_to_text.py +41 -0
- hindsight_api/alembic/versions/n9i0j1k2l3m4_learnings_and_pinned_reflections.py +134 -0
- hindsight_api/alembic/versions/o0j1k2l3m4n5_migrate_mental_models_data.py +113 -0
- hindsight_api/alembic/versions/p1k2l3m4n5o6_new_knowledge_architecture.py +194 -0
- hindsight_api/alembic/versions/q2l3m4n5o6p7_fix_mental_model_fact_type.py +50 -0
- hindsight_api/alembic/versions/r3m4n5o6p7q8_add_reflect_response_to_reflections.py +47 -0
- hindsight_api/alembic/versions/s4n5o6p7q8r9_add_consolidated_at_to_memory_units.py +53 -0
- hindsight_api/alembic/versions/t5o6p7q8r9s0_rename_mental_models_to_observations.py +134 -0
- hindsight_api/alembic/versions/u6p7q8r9s0t1_mental_models_text_id.py +41 -0
- hindsight_api/alembic/versions/v7q8r9s0t1u2_add_max_tokens_to_mental_models.py +50 -0
- hindsight_api/api/http.py +1406 -118
- hindsight_api/api/mcp.py +11 -196
- hindsight_api/config.py +359 -27
- hindsight_api/engine/consolidation/__init__.py +5 -0
- hindsight_api/engine/consolidation/consolidator.py +859 -0
- hindsight_api/engine/consolidation/prompts.py +69 -0
- hindsight_api/engine/cross_encoder.py +706 -88
- hindsight_api/engine/db_budget.py +284 -0
- hindsight_api/engine/db_utils.py +11 -0
- hindsight_api/engine/directives/__init__.py +5 -0
- hindsight_api/engine/directives/models.py +37 -0
- hindsight_api/engine/embeddings.py +553 -29
- hindsight_api/engine/entity_resolver.py +8 -5
- hindsight_api/engine/interface.py +40 -17
- hindsight_api/engine/llm_wrapper.py +744 -68
- hindsight_api/engine/memory_engine.py +2505 -1017
- hindsight_api/engine/mental_models/__init__.py +14 -0
- hindsight_api/engine/mental_models/models.py +53 -0
- hindsight_api/engine/query_analyzer.py +4 -3
- hindsight_api/engine/reflect/__init__.py +18 -0
- hindsight_api/engine/reflect/agent.py +933 -0
- hindsight_api/engine/reflect/models.py +109 -0
- hindsight_api/engine/reflect/observations.py +186 -0
- hindsight_api/engine/reflect/prompts.py +483 -0
- hindsight_api/engine/reflect/tools.py +437 -0
- hindsight_api/engine/reflect/tools_schema.py +250 -0
- hindsight_api/engine/response_models.py +168 -4
- hindsight_api/engine/retain/bank_utils.py +79 -201
- hindsight_api/engine/retain/fact_extraction.py +424 -195
- hindsight_api/engine/retain/fact_storage.py +35 -12
- hindsight_api/engine/retain/link_utils.py +29 -24
- hindsight_api/engine/retain/orchestrator.py +24 -43
- hindsight_api/engine/retain/types.py +11 -2
- hindsight_api/engine/search/graph_retrieval.py +43 -14
- hindsight_api/engine/search/link_expansion_retrieval.py +391 -0
- hindsight_api/engine/search/mpfp_retrieval.py +362 -117
- hindsight_api/engine/search/reranking.py +2 -2
- hindsight_api/engine/search/retrieval.py +848 -201
- hindsight_api/engine/search/tags.py +172 -0
- hindsight_api/engine/search/think_utils.py +42 -141
- hindsight_api/engine/search/trace.py +12 -1
- hindsight_api/engine/search/tracer.py +26 -6
- hindsight_api/engine/search/types.py +21 -3
- hindsight_api/engine/task_backend.py +113 -106
- hindsight_api/engine/utils.py +1 -152
- hindsight_api/extensions/__init__.py +10 -1
- hindsight_api/extensions/builtin/tenant.py +5 -1
- hindsight_api/extensions/context.py +10 -1
- hindsight_api/extensions/operation_validator.py +81 -4
- hindsight_api/extensions/tenant.py +26 -0
- hindsight_api/main.py +69 -6
- hindsight_api/mcp_local.py +12 -53
- hindsight_api/mcp_tools.py +494 -0
- hindsight_api/metrics.py +433 -48
- hindsight_api/migrations.py +141 -1
- hindsight_api/models.py +3 -3
- hindsight_api/pg0.py +53 -0
- hindsight_api/server.py +39 -2
- hindsight_api/worker/__init__.py +11 -0
- hindsight_api/worker/main.py +296 -0
- hindsight_api/worker/poller.py +486 -0
- {hindsight_api-0.2.1.dist-info → hindsight_api-0.4.0.dist-info}/METADATA +16 -6
- hindsight_api-0.4.0.dist-info/RECORD +112 -0
- {hindsight_api-0.2.1.dist-info → hindsight_api-0.4.0.dist-info}/entry_points.txt +2 -0
- hindsight_api/engine/retain/observation_regeneration.py +0 -254
- hindsight_api/engine/search/observation_utils.py +0 -125
- hindsight_api/engine/search/scoring.py +0 -159
- hindsight_api-0.2.1.dist-info/RECORD +0 -75
- {hindsight_api-0.2.1.dist-info → hindsight_api-0.4.0.dist-info}/WHEEL +0 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Mental models module for Hindsight.
|
|
3
|
+
|
|
4
|
+
Mental models contain directives - hard rules that are injected into reflect prompts.
|
|
5
|
+
Directives are user-defined and their observations are user-provided (not LLM-generated).
|
|
6
|
+
|
|
7
|
+
Other types of consolidated knowledge are handled by:
|
|
8
|
+
- Learnings: Automatic bottom-up consolidation from facts
|
|
9
|
+
- Pinned Reflections: User-curated living documents
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from .models import MentalModel, MentalModelSubtype
|
|
13
|
+
|
|
14
|
+
__all__ = ["MentalModel", "MentalModelSubtype"]
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Pydantic models for mental models.
|
|
3
|
+
"""
|
|
4
|
+
|
|
5
|
+
from datetime import datetime, timezone
|
|
6
|
+
from enum import Enum
|
|
7
|
+
|
|
8
|
+
from pydantic import BaseModel, Field
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class MentalModelSubtype(str, Enum):
|
|
12
|
+
"""Subtype of mental model.
|
|
13
|
+
|
|
14
|
+
Currently only DIRECTIVE is supported. Other types of consolidated knowledge
|
|
15
|
+
are handled by:
|
|
16
|
+
- Learnings: Automatic bottom-up consolidation from facts
|
|
17
|
+
- Pinned Reflections: User-curated living documents
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
DIRECTIVE = "directive" # User-defined hard rules, observations user-provided
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
class MentalModel(BaseModel):
|
|
24
|
+
"""
|
|
25
|
+
A mental model representing synthesized understanding.
|
|
26
|
+
|
|
27
|
+
Mental models are the agent's consolidated knowledge. Unlike raw facts,
|
|
28
|
+
mental models provide:
|
|
29
|
+
- A one-liner description for quick scanning/retrieval
|
|
30
|
+
- A full summary for deep understanding
|
|
31
|
+
- Links to related mental models
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
id: str = Field(description="Unique identifier within the bank")
|
|
35
|
+
bank_id: str = Field(description="Bank this mental model belongs to")
|
|
36
|
+
subtype: MentalModelSubtype = Field(description="How this model was created")
|
|
37
|
+
name: str = Field(description="Human-readable name")
|
|
38
|
+
description: str = Field(description="One-liner for quick scanning and retrieval matching")
|
|
39
|
+
summary: str | None = Field(default=None, description="Full synthesized understanding")
|
|
40
|
+
|
|
41
|
+
# References
|
|
42
|
+
entity_id: str | None = Field(default=None, description="Reference to entities table when type=entity")
|
|
43
|
+
source_facts: list[str] = Field(default_factory=list, description="Fact IDs used to generate summary")
|
|
44
|
+
links: list[str] = Field(default_factory=list, description="Related mental model IDs")
|
|
45
|
+
|
|
46
|
+
# Tags for scoped visibility (similar to document tags)
|
|
47
|
+
tags: list[str] = Field(default_factory=list, description="Tags for scoped visibility filtering")
|
|
48
|
+
|
|
49
|
+
# Timestamps
|
|
50
|
+
last_updated: datetime | None = Field(default=None, description="When summary was last regenerated")
|
|
51
|
+
created_at: datetime = Field(
|
|
52
|
+
default_factory=lambda: datetime.now(timezone.utc), description="When this model was created"
|
|
53
|
+
)
|
|
@@ -84,7 +84,7 @@ class DateparserQueryAnalyzer(QueryAnalyzer):
|
|
|
84
84
|
|
|
85
85
|
Performance:
|
|
86
86
|
- ~10-50ms per query
|
|
87
|
-
- No model loading required
|
|
87
|
+
- No model loading required (lazy import on first use)
|
|
88
88
|
"""
|
|
89
89
|
|
|
90
90
|
def __init__(self):
|
|
@@ -112,8 +112,6 @@ class DateparserQueryAnalyzer(QueryAnalyzer):
|
|
|
112
112
|
Returns:
|
|
113
113
|
QueryAnalysis with temporal_constraint if found
|
|
114
114
|
"""
|
|
115
|
-
self.load()
|
|
116
|
-
|
|
117
115
|
if reference_date is None:
|
|
118
116
|
reference_date = datetime.now()
|
|
119
117
|
|
|
@@ -123,6 +121,9 @@ class DateparserQueryAnalyzer(QueryAnalyzer):
|
|
|
123
121
|
if period_result is not None:
|
|
124
122
|
return QueryAnalysis(temporal_constraint=period_result)
|
|
125
123
|
|
|
124
|
+
# Lazy load dateparser (only imports on first call, then cached)
|
|
125
|
+
self.load()
|
|
126
|
+
|
|
126
127
|
# Use dateparser's search_dates to find temporal expressions
|
|
127
128
|
settings = {
|
|
128
129
|
"RELATIVE_BASE": reference_date,
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Reflect agent module for agentic reflection with tools.
|
|
3
|
+
|
|
4
|
+
The reflect agent uses an iterative loop with tools to:
|
|
5
|
+
1. Lookup mental models (existing knowledge)
|
|
6
|
+
2. Recall facts (semantic + temporal search)
|
|
7
|
+
3. Expand memories (get chunk/document context)
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from .agent import ReflectAgentResult, run_reflect_agent
|
|
11
|
+
from .models import ReflectAction, ReflectActionBatch
|
|
12
|
+
|
|
13
|
+
__all__ = [
|
|
14
|
+
"run_reflect_agent",
|
|
15
|
+
"ReflectAgentResult",
|
|
16
|
+
"ReflectAction",
|
|
17
|
+
"ReflectActionBatch",
|
|
18
|
+
]
|