dao-ai 0.0.35__py3-none-any.whl → 0.1.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.
Files changed (58) hide show
  1. dao_ai/__init__.py +29 -0
  2. dao_ai/cli.py +195 -30
  3. dao_ai/config.py +797 -242
  4. dao_ai/genie/__init__.py +38 -0
  5. dao_ai/genie/cache/__init__.py +43 -0
  6. dao_ai/genie/cache/base.py +72 -0
  7. dao_ai/genie/cache/core.py +75 -0
  8. dao_ai/genie/cache/lru.py +329 -0
  9. dao_ai/genie/cache/semantic.py +919 -0
  10. dao_ai/genie/core.py +35 -0
  11. dao_ai/graph.py +27 -253
  12. dao_ai/hooks/__init__.py +9 -6
  13. dao_ai/hooks/core.py +22 -190
  14. dao_ai/memory/__init__.py +10 -0
  15. dao_ai/memory/core.py +23 -5
  16. dao_ai/memory/databricks.py +389 -0
  17. dao_ai/memory/postgres.py +2 -2
  18. dao_ai/messages.py +6 -4
  19. dao_ai/middleware/__init__.py +125 -0
  20. dao_ai/middleware/assertions.py +778 -0
  21. dao_ai/middleware/base.py +50 -0
  22. dao_ai/middleware/core.py +61 -0
  23. dao_ai/middleware/guardrails.py +415 -0
  24. dao_ai/middleware/human_in_the_loop.py +228 -0
  25. dao_ai/middleware/message_validation.py +554 -0
  26. dao_ai/middleware/summarization.py +192 -0
  27. dao_ai/models.py +1177 -108
  28. dao_ai/nodes.py +118 -161
  29. dao_ai/optimization.py +664 -0
  30. dao_ai/orchestration/__init__.py +52 -0
  31. dao_ai/orchestration/core.py +287 -0
  32. dao_ai/orchestration/supervisor.py +264 -0
  33. dao_ai/orchestration/swarm.py +226 -0
  34. dao_ai/prompts.py +126 -29
  35. dao_ai/providers/databricks.py +126 -381
  36. dao_ai/state.py +139 -21
  37. dao_ai/tools/__init__.py +11 -5
  38. dao_ai/tools/core.py +57 -4
  39. dao_ai/tools/email.py +280 -0
  40. dao_ai/tools/genie.py +108 -35
  41. dao_ai/tools/mcp.py +4 -3
  42. dao_ai/tools/memory.py +50 -0
  43. dao_ai/tools/python.py +4 -12
  44. dao_ai/tools/search.py +14 -0
  45. dao_ai/tools/slack.py +1 -1
  46. dao_ai/tools/unity_catalog.py +8 -6
  47. dao_ai/tools/vector_search.py +16 -9
  48. dao_ai/utils.py +72 -8
  49. dao_ai-0.1.0.dist-info/METADATA +1878 -0
  50. dao_ai-0.1.0.dist-info/RECORD +62 -0
  51. dao_ai/chat_models.py +0 -204
  52. dao_ai/guardrails.py +0 -112
  53. dao_ai/tools/human_in_the_loop.py +0 -100
  54. dao_ai-0.0.35.dist-info/METADATA +0 -1169
  55. dao_ai-0.0.35.dist-info/RECORD +0 -41
  56. {dao_ai-0.0.35.dist-info → dao_ai-0.1.0.dist-info}/WHEEL +0 -0
  57. {dao_ai-0.0.35.dist-info → dao_ai-0.1.0.dist-info}/entry_points.txt +0 -0
  58. {dao_ai-0.0.35.dist-info → dao_ai-0.1.0.dist-info}/licenses/LICENSE +0 -0
@@ -0,0 +1,228 @@
1
+ """
2
+ Human-in-the-loop middleware for DAO AI agents.
3
+
4
+ This module provides utilities for creating HITL middleware from DAO AI configuration.
5
+ It re-exports LangChain's built-in HumanInTheLoopMiddleware.
6
+
7
+ LangChain's HumanInTheLoopMiddleware automatically:
8
+ - Pauses agent execution for human approval of tool calls
9
+ - Allows humans to approve, edit, or reject tool calls
10
+ - Uses LangGraph's interrupt mechanism for persistence across pauses
11
+
12
+ Example:
13
+ from dao_ai.middleware import create_human_in_the_loop_middleware
14
+
15
+ middleware = create_human_in_the_loop_middleware(
16
+ interrupt_on={"send_email": True, "delete_record": True},
17
+ )
18
+ """
19
+
20
+ from typing import Any, Sequence
21
+
22
+ from langchain.agents.middleware import HumanInTheLoopMiddleware
23
+ from langchain.agents.middleware.human_in_the_loop import (
24
+ Action,
25
+ ActionRequest,
26
+ ApproveDecision,
27
+ Decision,
28
+ DecisionType,
29
+ EditDecision,
30
+ HITLRequest,
31
+ HITLResponse,
32
+ InterruptOnConfig,
33
+ RejectDecision,
34
+ ReviewConfig,
35
+ )
36
+ from loguru import logger
37
+
38
+ from dao_ai.config import HumanInTheLoopModel, ToolModel
39
+
40
+ __all__ = [
41
+ # LangChain middleware
42
+ "HumanInTheLoopMiddleware",
43
+ # LangChain HITL types
44
+ "Action",
45
+ "ActionRequest",
46
+ "ApproveDecision",
47
+ "Decision",
48
+ "DecisionType",
49
+ "EditDecision",
50
+ "HITLRequest",
51
+ "HITLResponse",
52
+ "InterruptOnConfig",
53
+ "RejectDecision",
54
+ "ReviewConfig",
55
+ # DAO AI helper functions and models
56
+ "create_human_in_the_loop_middleware",
57
+ "create_hitl_middleware_from_tool_models",
58
+ ]
59
+
60
+
61
+ def _hitl_config_to_allowed_decisions(
62
+ hitl_config: HumanInTheLoopModel,
63
+ ) -> list[DecisionType]:
64
+ """
65
+ Extract allowed decisions from HumanInTheLoopModel.
66
+
67
+ LangChain's HumanInTheLoopMiddleware supports 3 decision types:
68
+ - "approve": Execute tool with original arguments
69
+ - "edit": Modify arguments before execution
70
+ - "reject": Skip execution with optional feedback message
71
+
72
+ Args:
73
+ hitl_config: HumanInTheLoopModel with allowed_decisions
74
+
75
+ Returns:
76
+ List of allowed decision types (e.g., ["approve", "edit", "reject"])
77
+ """
78
+ return hitl_config.allowed_decisions # type: ignore
79
+
80
+
81
+ def _config_to_interrupt_on_entry(
82
+ config: HumanInTheLoopModel | bool,
83
+ ) -> dict[str, Any] | bool:
84
+ """
85
+ Convert a HITL config value to interrupt_on entry format.
86
+
87
+ Args:
88
+ config: HumanInTheLoopModel, True, or False
89
+
90
+ Returns:
91
+ dict with allowed_decisions and optional description, True, or False
92
+ """
93
+ if config is False:
94
+ return False
95
+ if config is True:
96
+ return {"allowed_decisions": ["approve", "edit", "reject"]}
97
+ if isinstance(config, HumanInTheLoopModel):
98
+ interrupt_entry: dict[str, Any] = {
99
+ "allowed_decisions": _hitl_config_to_allowed_decisions(config)
100
+ }
101
+ # If review_prompt is provided, use it as the description
102
+ if config.review_prompt is not None:
103
+ interrupt_entry["description"] = config.review_prompt
104
+ return interrupt_entry
105
+
106
+ logger.warning(f"Unknown HITL config type: {type(config)}, defaulting to True")
107
+ return True
108
+
109
+
110
+ def create_human_in_the_loop_middleware(
111
+ interrupt_on: dict[str, HumanInTheLoopModel | bool | dict[str, Any]],
112
+ description_prefix: str = "Tool execution pending approval",
113
+ ) -> HumanInTheLoopMiddleware:
114
+ """
115
+ Create a HumanInTheLoopMiddleware instance.
116
+
117
+ Factory function for creating LangChain's built-in HumanInTheLoopMiddleware.
118
+ Accepts HumanInTheLoopModel, bool, or raw dict configurations per tool.
119
+
120
+ Note: This middleware requires a checkpointer to be configured on the agent.
121
+
122
+ Args:
123
+ interrupt_on: Dictionary mapping tool names to HITL configuration.
124
+ Each tool can be configured with:
125
+ - HumanInTheLoopModel: Full configuration with custom settings
126
+ - True: Enable HITL with default settings (approve, edit, reject)
127
+ - False: Disable HITL for this tool
128
+ - dict: Raw interrupt_on config (e.g., {"allowed_decisions": [...]})
129
+ description_prefix: Message prefix shown when pausing for review
130
+
131
+ Returns:
132
+ HumanInTheLoopMiddleware configured with the specified parameters
133
+
134
+ Example:
135
+ from dao_ai.config import HumanInTheLoopModel
136
+
137
+ middleware = create_human_in_the_loop_middleware(
138
+ interrupt_on={
139
+ "send_email": HumanInTheLoopModel(review_prompt="Review email"),
140
+ "delete_record": True,
141
+ "search": False,
142
+ },
143
+ )
144
+ """
145
+ # Convert HumanInTheLoopModel entries to dict format
146
+ normalized_interrupt_on: dict[str, Any] = {}
147
+ for tool_name, config in interrupt_on.items():
148
+ if isinstance(config, (HumanInTheLoopModel, bool)):
149
+ normalized_interrupt_on[tool_name] = _config_to_interrupt_on_entry(config)
150
+ else:
151
+ # Already in dict format
152
+ normalized_interrupt_on[tool_name] = config
153
+
154
+ logger.debug(
155
+ f"Creating HITL middleware for {len(normalized_interrupt_on)} tools: "
156
+ f"{list(normalized_interrupt_on.keys())}"
157
+ )
158
+
159
+ return HumanInTheLoopMiddleware(
160
+ interrupt_on=normalized_interrupt_on,
161
+ description_prefix=description_prefix,
162
+ )
163
+
164
+
165
+ def create_hitl_middleware_from_tool_models(
166
+ tool_models: Sequence[ToolModel],
167
+ description_prefix: str = "Tool execution pending approval",
168
+ ) -> HumanInTheLoopMiddleware | None:
169
+ """
170
+ Create HumanInTheLoopMiddleware from ToolModel configurations.
171
+
172
+ Scans tool_models for those with human_in_the_loop configured and
173
+ creates the appropriate middleware. This is the primary entry point
174
+ used by the agent node creation.
175
+
176
+ Args:
177
+ tool_models: List of ToolModel configurations from agent config
178
+ description_prefix: Message prefix shown when pausing for review
179
+
180
+ Returns:
181
+ HumanInTheLoopMiddleware if any tools require approval, None otherwise
182
+
183
+ Example:
184
+ from dao_ai.config import ToolModel, PythonFunctionModel, HumanInTheLoopModel
185
+
186
+ tool_models = [
187
+ ToolModel(
188
+ name="email_tool",
189
+ function=PythonFunctionModel(
190
+ name="send_email",
191
+ human_in_the_loop=HumanInTheLoopModel(
192
+ review_prompt="Review this email",
193
+ ),
194
+ ),
195
+ ),
196
+ ]
197
+
198
+ middleware = create_hitl_middleware_from_tool_models(tool_models)
199
+ """
200
+ from dao_ai.config import BaseFunctionModel
201
+
202
+ interrupt_on: dict[str, HumanInTheLoopModel] = {}
203
+
204
+ for tool_model in tool_models:
205
+ function = tool_model.function
206
+
207
+ if not isinstance(function, BaseFunctionModel):
208
+ continue
209
+
210
+ hitl_config: HumanInTheLoopModel | None = function.human_in_the_loop
211
+ if not hitl_config:
212
+ continue
213
+
214
+ # Get tool names created by this function
215
+ for func_tool in function.as_tools():
216
+ tool_name: str | None = getattr(func_tool, "name", None)
217
+ if tool_name:
218
+ interrupt_on[tool_name] = hitl_config
219
+ logger.debug(f"Tool '{tool_name}' configured for HITL")
220
+
221
+ if not interrupt_on:
222
+ logger.debug("No tools require HITL - returning None")
223
+ return None
224
+
225
+ return create_human_in_the_loop_middleware(
226
+ interrupt_on=interrupt_on,
227
+ description_prefix=description_prefix,
228
+ )