agentdatabase 0.2.0__tar.gz → 0.3.0__tar.gz
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.
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/PKG-INFO +1 -1
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/pyproject.toml +1 -1
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdatabase.egg-info/PKG-INFO +1 -1
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdatabase.egg-info/SOURCES.txt +3 -0
- agentdatabase-0.3.0/src/agentdb/adapters/__init__.py +10 -0
- agentdatabase-0.3.0/src/agentdb/adapters/capabilities.py +52 -0
- agentdatabase-0.3.0/src/agentdb/adapters/claude_agent_sdk.py +234 -0
- agentdatabase-0.2.0/src/agentdb/adapters/claude_agent_sdk.py → agentdatabase-0.3.0/src/agentdb/adapters/middleware.py +304 -207
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/LICENSE +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/README.md +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/setup.cfg +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdatabase.egg-info/dependency_links.txt +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdatabase.egg-info/entry_points.txt +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdatabase.egg-info/requires.txt +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdatabase.egg-info/top_level.txt +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/__init__.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/adapters/hermes.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/backend.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/core/__init__.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/core/directory_tracking.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/core/file_integrity.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/core/models.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/core/profiles.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/core/store.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/core/system.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/embeddings/__init__.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/embeddings/provider.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/embeddings/store.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/embeddings/text.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/gateway/__init__.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/gateway/adapter.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/governance/__init__.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/governance/conflict_detector.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/governance/lifecycle_manager.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/governance/permission_router.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/interface/__init__.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/interface/client.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/interface/mcp_server.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/notifications/__init__.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/notifications/channels.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/notifications/dispatcher.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/providers/__init__.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/providers/anthropic.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/retrieval/__init__.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/retrieval/algorithm.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/skills/__init__.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/skills/evolution/__init__.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/skills/evolution/manager.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/skills/evolution/skillopt_adapter.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/skills/evolution/wikiskill_adapter.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/skills/skill_store.py +0 -0
- {agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/testing.py +0 -0
|
@@ -10,8 +10,11 @@ src/agentdatabase.egg-info/top_level.txt
|
|
|
10
10
|
src/agentdb/__init__.py
|
|
11
11
|
src/agentdb/backend.py
|
|
12
12
|
src/agentdb/testing.py
|
|
13
|
+
src/agentdb/adapters/__init__.py
|
|
14
|
+
src/agentdb/adapters/capabilities.py
|
|
13
15
|
src/agentdb/adapters/claude_agent_sdk.py
|
|
14
16
|
src/agentdb/adapters/hermes.py
|
|
17
|
+
src/agentdb/adapters/middleware.py
|
|
15
18
|
src/agentdb/core/__init__.py
|
|
16
19
|
src/agentdb/core/directory_tracking.py
|
|
17
20
|
src/agentdb/core/file_integrity.py
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
from .middleware import AgentDBMiddleware
|
|
2
|
+
from .capabilities import RunnerCapabilities, CLAUDE_CAPABILITIES, FIREWORKS_CAPABILITIES, LOCAL_CAPABILITIES
|
|
3
|
+
|
|
4
|
+
__all__ = [
|
|
5
|
+
"AgentDBMiddleware",
|
|
6
|
+
"RunnerCapabilities",
|
|
7
|
+
"CLAUDE_CAPABILITIES",
|
|
8
|
+
"FIREWORKS_CAPABILITIES",
|
|
9
|
+
"LOCAL_CAPABILITIES",
|
|
10
|
+
]
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
@dataclass(frozen=True)
|
|
5
|
+
class RunnerCapabilities:
|
|
6
|
+
"""Frozen dataclass representing the capabilities of a runner tier."""
|
|
7
|
+
|
|
8
|
+
can_call_tools_mid_turn: bool = False
|
|
9
|
+
can_write_mid_turn: bool = False
|
|
10
|
+
has_native_memory: bool = False
|
|
11
|
+
inject_skills: bool = True
|
|
12
|
+
inject_workspace: bool = True
|
|
13
|
+
inject_work_context: bool = True
|
|
14
|
+
max_injection_tokens: int = 4096
|
|
15
|
+
auto_elevate_threshold: int = 3
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
# Claude capabilities: no injection (Claude has native context management)
|
|
19
|
+
CLAUDE_CAPABILITIES = RunnerCapabilities(
|
|
20
|
+
can_call_tools_mid_turn=True,
|
|
21
|
+
can_write_mid_turn=True,
|
|
22
|
+
has_native_memory=False,
|
|
23
|
+
inject_skills=False,
|
|
24
|
+
inject_workspace=False,
|
|
25
|
+
inject_work_context=False,
|
|
26
|
+
max_injection_tokens=0,
|
|
27
|
+
auto_elevate_threshold=3,
|
|
28
|
+
)
|
|
29
|
+
|
|
30
|
+
# Fireworks capabilities: partial injection
|
|
31
|
+
FIREWORKS_CAPABILITIES = RunnerCapabilities(
|
|
32
|
+
can_call_tools_mid_turn=False,
|
|
33
|
+
can_write_mid_turn=False,
|
|
34
|
+
has_native_memory=True,
|
|
35
|
+
inject_skills=True,
|
|
36
|
+
inject_workspace=True,
|
|
37
|
+
inject_work_context=False,
|
|
38
|
+
max_injection_tokens=2048,
|
|
39
|
+
auto_elevate_threshold=3,
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# Local capabilities: full injection
|
|
43
|
+
LOCAL_CAPABILITIES = RunnerCapabilities(
|
|
44
|
+
can_call_tools_mid_turn=False,
|
|
45
|
+
can_write_mid_turn=False,
|
|
46
|
+
has_native_memory=False,
|
|
47
|
+
inject_skills=True,
|
|
48
|
+
inject_workspace=True,
|
|
49
|
+
inject_work_context=True,
|
|
50
|
+
max_injection_tokens=4096,
|
|
51
|
+
auto_elevate_threshold=3,
|
|
52
|
+
)
|
|
@@ -0,0 +1,234 @@
|
|
|
1
|
+
"""
|
|
2
|
+
Claude Agent SDK runtime adapter (backward-compat shim).
|
|
3
|
+
|
|
4
|
+
AgentDBClaudeMiddleware has been renamed to AgentDBMiddleware and moved to
|
|
5
|
+
`agentdb.adapters.middleware`. This module re-exports it under the old name
|
|
6
|
+
for backward compatibility and retains AgentDBClaudeRuntime.
|
|
7
|
+
"""
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
from typing import TYPE_CHECKING, Any, Callable, Optional
|
|
11
|
+
|
|
12
|
+
from .middleware import AgentDBMiddleware as AgentDBClaudeMiddleware # noqa: F401
|
|
13
|
+
from .middleware import AgentDBMiddleware # noqa: F401
|
|
14
|
+
from .middleware import _extract_text_from_model_response
|
|
15
|
+
from .capabilities import CLAUDE_CAPABILITIES
|
|
16
|
+
|
|
17
|
+
if TYPE_CHECKING:
|
|
18
|
+
from ..interface.client import AgentDB, RetrievalResult
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class AgentDBClaudeRuntime:
|
|
22
|
+
"""
|
|
23
|
+
Higher-level runtime wrapper for easy integration.
|
|
24
|
+
|
|
25
|
+
Developers can instantiate once and call `handle_turn(...)` from their
|
|
26
|
+
Slack/event loop without manually orchestrating middleware hooks.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
*,
|
|
32
|
+
middleware: AgentDBMiddleware,
|
|
33
|
+
invoke_model: Callable[[list[dict[str, str]]], Any],
|
|
34
|
+
default_system_prompt: Optional[str] = None,
|
|
35
|
+
response_to_text: Callable[[Any], str] = _extract_text_from_model_response,
|
|
36
|
+
):
|
|
37
|
+
self._middleware = middleware
|
|
38
|
+
self._invoke_model = invoke_model
|
|
39
|
+
self._default_system_prompt = default_system_prompt
|
|
40
|
+
self._response_to_text = response_to_text
|
|
41
|
+
self._sdk_client_factory = None
|
|
42
|
+
|
|
43
|
+
@classmethod
|
|
44
|
+
def from_claude_client(
|
|
45
|
+
cls,
|
|
46
|
+
*,
|
|
47
|
+
db: "AgentDB",
|
|
48
|
+
agent_id: str,
|
|
49
|
+
client: Any,
|
|
50
|
+
model: str,
|
|
51
|
+
system_prompt: Optional[str] = None,
|
|
52
|
+
max_tokens: int = 1024,
|
|
53
|
+
) -> "AgentDBClaudeRuntime":
|
|
54
|
+
middleware = AgentDBMiddleware(db=db, agent_id=agent_id, capabilities=CLAUDE_CAPABILITIES)
|
|
55
|
+
|
|
56
|
+
def _invoke(messages: list[dict[str, str]]) -> Any:
|
|
57
|
+
system_blocks = [m["content"] for m in messages if m.get("role") == "system"]
|
|
58
|
+
chat_messages = [m for m in messages if m.get("role") != "system"]
|
|
59
|
+
if hasattr(client, "messages") and hasattr(client.messages, "create"):
|
|
60
|
+
payload: dict[str, Any] = {
|
|
61
|
+
"model": model,
|
|
62
|
+
"max_tokens": max_tokens,
|
|
63
|
+
"messages": chat_messages,
|
|
64
|
+
}
|
|
65
|
+
if system_blocks:
|
|
66
|
+
payload["system"] = "\n\n".join(system_blocks)
|
|
67
|
+
return client.messages.create(**payload)
|
|
68
|
+
if hasattr(client, "run"):
|
|
69
|
+
return client.run(model=model, messages=messages, max_tokens=max_tokens)
|
|
70
|
+
raise ValueError(
|
|
71
|
+
"Unsupported client: expected `messages.create(...)` or `run(...)` API."
|
|
72
|
+
)
|
|
73
|
+
|
|
74
|
+
return cls(
|
|
75
|
+
middleware=middleware,
|
|
76
|
+
invoke_model=_invoke,
|
|
77
|
+
default_system_prompt=system_prompt,
|
|
78
|
+
response_to_text=_extract_text_from_model_response,
|
|
79
|
+
)
|
|
80
|
+
|
|
81
|
+
@classmethod
|
|
82
|
+
def from_sdk_client(
|
|
83
|
+
cls,
|
|
84
|
+
*,
|
|
85
|
+
db: "AgentDB",
|
|
86
|
+
agent_id: str,
|
|
87
|
+
sdk_client_factory: Callable[[str], Any],
|
|
88
|
+
system_prompt: Optional[str] = None,
|
|
89
|
+
) -> "AgentDBClaudeRuntime":
|
|
90
|
+
"""
|
|
91
|
+
Build a runtime that wraps a ClaudeSDKClient (agentic subprocess).
|
|
92
|
+
|
|
93
|
+
sdk_client_factory: callable(thread_id: str) -> client
|
|
94
|
+
The client must support: await client.query(prompt) and
|
|
95
|
+
async-for client.receive_response() yielding messages with
|
|
96
|
+
.content (list of blocks with .text) or .is_error/.result.
|
|
97
|
+
"""
|
|
98
|
+
middleware = AgentDBMiddleware(db=db, agent_id=agent_id, capabilities=CLAUDE_CAPABILITIES)
|
|
99
|
+
instance = cls(
|
|
100
|
+
middleware=middleware,
|
|
101
|
+
invoke_model=lambda messages: "",
|
|
102
|
+
default_system_prompt=system_prompt,
|
|
103
|
+
)
|
|
104
|
+
instance._sdk_client_factory = sdk_client_factory
|
|
105
|
+
return instance
|
|
106
|
+
|
|
107
|
+
async def handle_turn_async(
|
|
108
|
+
self,
|
|
109
|
+
*,
|
|
110
|
+
user_query: str,
|
|
111
|
+
session_id: str,
|
|
112
|
+
message_id: Optional[str] = None,
|
|
113
|
+
system_prompt: Optional[str] = None,
|
|
114
|
+
external_context: Optional[list[dict[str, Any]]] = None,
|
|
115
|
+
base_messages: Optional[list[dict[str, str]]] = None,
|
|
116
|
+
memory_writes: Optional[list[dict[str, Any]]] = None,
|
|
117
|
+
tracked_directories: Optional[list[str]] = None,
|
|
118
|
+
directory_snapshots: Optional[list[dict[str, Any]]] = None,
|
|
119
|
+
tracking_base_dir: Optional[str] = None,
|
|
120
|
+
max_files_per_directory: Optional[int] = None,
|
|
121
|
+
outcome_type: Optional[str] = None,
|
|
122
|
+
outcome_value: Optional[float] = None,
|
|
123
|
+
) -> dict[str, Any]:
|
|
124
|
+
sdk_factory = getattr(self, "_sdk_client_factory", None)
|
|
125
|
+
if sdk_factory is None:
|
|
126
|
+
raise RuntimeError(
|
|
127
|
+
"handle_turn_async requires a runtime built with from_sdk_client(). "
|
|
128
|
+
"Use handle_turn() for the messages.create() path."
|
|
129
|
+
)
|
|
130
|
+
|
|
131
|
+
messages, retrieval = await self._middleware.prepare_turn_async(
|
|
132
|
+
user_query=user_query,
|
|
133
|
+
session_id=session_id,
|
|
134
|
+
system_prompt=system_prompt if system_prompt is not None else self._default_system_prompt,
|
|
135
|
+
external_context=external_context,
|
|
136
|
+
base_messages=base_messages,
|
|
137
|
+
)
|
|
138
|
+
|
|
139
|
+
system_blocks = [m["content"] for m in messages if m.get("role") == "system"]
|
|
140
|
+
user_messages = [m for m in messages if m.get("role") == "user"]
|
|
141
|
+
prompt_parts = system_blocks + [m["content"] for m in user_messages]
|
|
142
|
+
prompt = "\n\n".join(prompt_parts)
|
|
143
|
+
|
|
144
|
+
client = sdk_factory(session_id)
|
|
145
|
+
await client.query(prompt)
|
|
146
|
+
|
|
147
|
+
text_parts: list[str] = []
|
|
148
|
+
async for message in client.receive_response():
|
|
149
|
+
is_error = getattr(message, "is_error", False)
|
|
150
|
+
if is_error:
|
|
151
|
+
error_msg = getattr(message, "result", "Claude SDK error")
|
|
152
|
+
raise RuntimeError(error_msg or "Claude SDK returned an error")
|
|
153
|
+
content = getattr(message, "content", None)
|
|
154
|
+
if content is not None:
|
|
155
|
+
for block in content:
|
|
156
|
+
text = getattr(block, "text", None)
|
|
157
|
+
if text is not None:
|
|
158
|
+
text_parts.append(text)
|
|
159
|
+
|
|
160
|
+
response_text = "\n".join(text_parts).strip()
|
|
161
|
+
|
|
162
|
+
await self._middleware.finalize_turn_async(
|
|
163
|
+
session_id=session_id,
|
|
164
|
+
model_response=response_text,
|
|
165
|
+
memory_writes=memory_writes,
|
|
166
|
+
message_id=message_id,
|
|
167
|
+
tracked_directories=tracked_directories,
|
|
168
|
+
directory_snapshots=directory_snapshots,
|
|
169
|
+
tracking_base_dir=tracking_base_dir,
|
|
170
|
+
max_files_per_directory=max_files_per_directory,
|
|
171
|
+
outcome_type=outcome_type,
|
|
172
|
+
outcome_value=outcome_value,
|
|
173
|
+
)
|
|
174
|
+
|
|
175
|
+
return {
|
|
176
|
+
"response_text": response_text,
|
|
177
|
+
"raw_response": None,
|
|
178
|
+
"retrieval": retrieval,
|
|
179
|
+
"messages": messages,
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
def handle_turn(
|
|
183
|
+
self,
|
|
184
|
+
*,
|
|
185
|
+
user_query: str,
|
|
186
|
+
session_id: str,
|
|
187
|
+
message_id: Optional[str] = None,
|
|
188
|
+
system_prompt: Optional[str] = None,
|
|
189
|
+
external_context: Optional[list[dict[str, Any]]] = None,
|
|
190
|
+
base_messages: Optional[list[dict[str, str]]] = None,
|
|
191
|
+
memory_writes: Optional[list[dict[str, Any]]] = None,
|
|
192
|
+
tracked_directories: Optional[list[str]] = None,
|
|
193
|
+
directory_snapshots: Optional[list[dict[str, Any]]] = None,
|
|
194
|
+
tracking_base_dir: Optional[str] = None,
|
|
195
|
+
max_files_per_directory: Optional[int] = None,
|
|
196
|
+
outcome_type: Optional[str] = None,
|
|
197
|
+
outcome_value: Optional[float] = None,
|
|
198
|
+
) -> dict[str, Any]:
|
|
199
|
+
messages, retrieval = self._middleware.prepare_turn(
|
|
200
|
+
user_query=user_query,
|
|
201
|
+
session_id=session_id,
|
|
202
|
+
system_prompt=system_prompt if system_prompt is not None else self._default_system_prompt,
|
|
203
|
+
external_context=external_context,
|
|
204
|
+
base_messages=base_messages,
|
|
205
|
+
)
|
|
206
|
+
raw_response = self._invoke_model(messages)
|
|
207
|
+
response_text = self._response_to_text(raw_response)
|
|
208
|
+
self._middleware.finalize_turn(
|
|
209
|
+
session_id=session_id,
|
|
210
|
+
model_response=response_text,
|
|
211
|
+
memory_writes=memory_writes,
|
|
212
|
+
message_id=message_id,
|
|
213
|
+
tracked_directories=tracked_directories,
|
|
214
|
+
directory_snapshots=directory_snapshots,
|
|
215
|
+
tracking_base_dir=tracking_base_dir,
|
|
216
|
+
max_files_per_directory=max_files_per_directory,
|
|
217
|
+
outcome_type=outcome_type,
|
|
218
|
+
outcome_value=outcome_value,
|
|
219
|
+
)
|
|
220
|
+
return {
|
|
221
|
+
"response_text": response_text,
|
|
222
|
+
"raw_response": raw_response,
|
|
223
|
+
"retrieval": retrieval,
|
|
224
|
+
"messages": messages,
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
def get_sdk_hooks(self) -> dict[str, Any]:
|
|
228
|
+
"""
|
|
229
|
+
Return hook handlers from the middleware for Claude SDK integration.
|
|
230
|
+
|
|
231
|
+
Returns:
|
|
232
|
+
{"PreToolUse": callable}
|
|
233
|
+
"""
|
|
234
|
+
return self._middleware.build_hook_handlers()
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
"""
|
|
2
|
-
|
|
2
|
+
AgentDBMiddleware — model-agnostic middleware for AgentDB integrations.
|
|
3
3
|
|
|
4
|
-
This adapter does not import
|
|
5
|
-
runtime contract wrapper around AgentDB so
|
|
4
|
+
This adapter does not import any specific model SDK. Instead, it provides a
|
|
5
|
+
runtime contract wrapper around AgentDB so agents can enforce:
|
|
6
6
|
|
|
7
7
|
1) pre-response retrieval (always call AgentDB.retrieve)
|
|
8
8
|
2) post-response persistence policy
|
|
@@ -19,9 +19,11 @@ from datetime import datetime, timezone
|
|
|
19
19
|
from typing import TYPE_CHECKING, Any, Awaitable, Callable, Optional
|
|
20
20
|
import json
|
|
21
21
|
import os
|
|
22
|
+
import subprocess
|
|
22
23
|
|
|
23
24
|
from ..core.directory_tracking import collect_directory_snapshots
|
|
24
25
|
from ..gateway.adapter import GatewayAdapter
|
|
26
|
+
from .capabilities import RunnerCapabilities, LOCAL_CAPABILITIES
|
|
25
27
|
|
|
26
28
|
if TYPE_CHECKING:
|
|
27
29
|
from ..interface.client import AgentDB, RetrievalResult
|
|
@@ -70,6 +72,16 @@ def _extract_text_from_model_response(raw_response: Any) -> str:
|
|
|
70
72
|
return str(raw_response)
|
|
71
73
|
|
|
72
74
|
|
|
75
|
+
@dataclass
|
|
76
|
+
class DirectoryRecord:
|
|
77
|
+
"""Tracks file system directory access within a session."""
|
|
78
|
+
path: str
|
|
79
|
+
first_seen_turn: int
|
|
80
|
+
last_seen_turn: int
|
|
81
|
+
access_count: int
|
|
82
|
+
elevated: bool = False
|
|
83
|
+
|
|
84
|
+
|
|
73
85
|
@dataclass(frozen=True)
|
|
74
86
|
class ClaudeRuntimeConfig:
|
|
75
87
|
model: str
|
|
@@ -77,9 +89,9 @@ class ClaudeRuntimeConfig:
|
|
|
77
89
|
max_tokens: int = 1024
|
|
78
90
|
|
|
79
91
|
|
|
80
|
-
class
|
|
92
|
+
class AgentDBMiddleware(GatewayAdapter):
|
|
81
93
|
"""
|
|
82
|
-
Middleware-style helper for
|
|
94
|
+
Middleware-style helper for model-agnostic AgentDB integrations.
|
|
83
95
|
|
|
84
96
|
Model calls are supplied as callbacks so this module stays runtime-agnostic.
|
|
85
97
|
Inherits GatewayAdapter for access to db.gateway (reconciliation, proposals).
|
|
@@ -90,12 +102,16 @@ class AgentDBClaudeMiddleware(GatewayAdapter):
|
|
|
90
102
|
db: "AgentDB",
|
|
91
103
|
agent_id: str,
|
|
92
104
|
conversation_started_callback: Optional[Callable[..., Any]] = None,
|
|
105
|
+
capabilities: RunnerCapabilities = LOCAL_CAPABILITIES,
|
|
93
106
|
):
|
|
94
107
|
resolved = db.config_for(agent_id)
|
|
95
108
|
super().__init__(db, agent_id, profile=resolved["name"])
|
|
96
109
|
self._conversation_started_callback = conversation_started_callback
|
|
97
110
|
self._resolved = resolved
|
|
98
111
|
self._session_working_dirs: dict[str, list[str]] = {}
|
|
112
|
+
self._session_dir_state: dict[str, list[DirectoryRecord]] = {}
|
|
113
|
+
self._session_work_log: dict[str, list[dict]] = {}
|
|
114
|
+
self._capabilities = capabilities
|
|
99
115
|
self.gateway.subscribe("conflict_detected", self._on_conflict)
|
|
100
116
|
|
|
101
117
|
def prepare_turn(
|
|
@@ -321,6 +337,126 @@ class AgentDBClaudeMiddleware(GatewayAdapter):
|
|
|
321
337
|
)
|
|
322
338
|
return response_text
|
|
323
339
|
|
|
340
|
+
def _build_work_context_block(self, session_id: str) -> str:
|
|
341
|
+
"""Return a formatted markdown block of the last 5 turns from the work log, or '' if empty."""
|
|
342
|
+
log = self._session_work_log.get(session_id, [])
|
|
343
|
+
if not log:
|
|
344
|
+
return ""
|
|
345
|
+
lines: list[str] = []
|
|
346
|
+
for entry in log[-5:]:
|
|
347
|
+
parts: list[str] = [f"Turn {entry['turn_num']}:"]
|
|
348
|
+
if entry.get("files_read"):
|
|
349
|
+
parts.append(f"read {', '.join(entry['files_read'])}.")
|
|
350
|
+
if entry.get("files_written"):
|
|
351
|
+
parts.append(f"wrote {', '.join(entry['files_written'])}.")
|
|
352
|
+
if entry.get("key_fact"):
|
|
353
|
+
parts.append(entry["key_fact"])
|
|
354
|
+
lines.append(" ".join(parts))
|
|
355
|
+
return "\n".join(lines)
|
|
356
|
+
|
|
357
|
+
def _trim_to_budget(self, content: str, token_budget: int) -> str:
|
|
358
|
+
"""Truncate content to token_budget tokens (approximated as len // 4)."""
|
|
359
|
+
if len(content) // 4 <= token_budget:
|
|
360
|
+
return content
|
|
361
|
+
return content[: token_budget * 4] + "…"
|
|
362
|
+
|
|
363
|
+
def _build_skills_block(self, session_id: str, user_query: str) -> str:
|
|
364
|
+
"""Return a formatted markdown block of relevant skills, or '' if none qualify."""
|
|
365
|
+
try:
|
|
366
|
+
if user_query.strip():
|
|
367
|
+
skills = self._db.skills.search(query=user_query, agent_id=self._agent_id)
|
|
368
|
+
else:
|
|
369
|
+
skills = self._db.skills.get_for_agent(self._agent_id)
|
|
370
|
+
|
|
371
|
+
skills = sorted(
|
|
372
|
+
(s for s in skills if s.health_score >= 0.5 and not s.archived),
|
|
373
|
+
key=lambda s: s.health_score,
|
|
374
|
+
reverse=True,
|
|
375
|
+
)[:5]
|
|
376
|
+
|
|
377
|
+
if not skills:
|
|
378
|
+
return ""
|
|
379
|
+
|
|
380
|
+
return "\n\n".join(f"### {s.name}\n{s.content}" for s in skills)
|
|
381
|
+
except Exception:
|
|
382
|
+
return ""
|
|
383
|
+
|
|
384
|
+
_WORKSPACE_EXCLUDED_DIRS = {"__pycache__", "node_modules", "dist", ".venv", "venv", "env"}
|
|
385
|
+
|
|
386
|
+
def _build_workspace_block(self, session_id: str, working_dirs: list[str]) -> str:
|
|
387
|
+
"""
|
|
388
|
+
Build a workspace snapshot block for injection. Best-effort: swallows all exceptions.
|
|
389
|
+
Lists up to 2 levels deep, excludes hidden dirs and common noise dirs.
|
|
390
|
+
Appends git branch/status when available. Includes session temp dirs.
|
|
391
|
+
Returns '' if nothing collected.
|
|
392
|
+
"""
|
|
393
|
+
sections: list[str] = []
|
|
394
|
+
|
|
395
|
+
try:
|
|
396
|
+
for raw_dir in working_dirs:
|
|
397
|
+
try:
|
|
398
|
+
if not os.path.isdir(raw_dir):
|
|
399
|
+
continue
|
|
400
|
+
|
|
401
|
+
lines: list[str] = [raw_dir]
|
|
402
|
+
|
|
403
|
+
# List up to 2 levels deep
|
|
404
|
+
try:
|
|
405
|
+
for entry in sorted(os.scandir(raw_dir), key=lambda e: e.name):
|
|
406
|
+
if entry.name.startswith(".") or entry.name in self._WORKSPACE_EXCLUDED_DIRS:
|
|
407
|
+
continue
|
|
408
|
+
lines.append(f" {entry.name}{'/' if entry.is_dir() else ''}")
|
|
409
|
+
if entry.is_dir():
|
|
410
|
+
try:
|
|
411
|
+
for sub in sorted(os.scandir(entry.path), key=lambda e: e.name):
|
|
412
|
+
if sub.name.startswith(".") or sub.name in self._WORKSPACE_EXCLUDED_DIRS:
|
|
413
|
+
continue
|
|
414
|
+
lines.append(f" {sub.name}{'/' if sub.is_dir() else ''}")
|
|
415
|
+
except OSError:
|
|
416
|
+
pass
|
|
417
|
+
except OSError:
|
|
418
|
+
pass
|
|
419
|
+
|
|
420
|
+
# Git info
|
|
421
|
+
try:
|
|
422
|
+
branch_result = subprocess.run(
|
|
423
|
+
["git", "-C", raw_dir, "rev-parse", "--abbrev-ref", "HEAD"],
|
|
424
|
+
capture_output=True, text=True, timeout=5,
|
|
425
|
+
)
|
|
426
|
+
if branch_result.returncode == 0:
|
|
427
|
+
branch = branch_result.stdout.strip()
|
|
428
|
+
lines.append(f" [git branch: {branch}]")
|
|
429
|
+
status_result = subprocess.run(
|
|
430
|
+
["git", "-C", raw_dir, "status", "--short"],
|
|
431
|
+
capture_output=True, text=True, timeout=5,
|
|
432
|
+
)
|
|
433
|
+
if status_result.returncode == 0 and status_result.stdout.strip():
|
|
434
|
+
for status_line in status_result.stdout.strip().splitlines():
|
|
435
|
+
lines.append(f" {status_line}")
|
|
436
|
+
except Exception:
|
|
437
|
+
pass
|
|
438
|
+
|
|
439
|
+
sections.append("\n".join(lines))
|
|
440
|
+
except Exception:
|
|
441
|
+
pass
|
|
442
|
+
|
|
443
|
+
# Temp dirs from session state (non-elevated only)
|
|
444
|
+
try:
|
|
445
|
+
records = self._session_dir_state.get(session_id, [])
|
|
446
|
+
temp_lines: list[str] = []
|
|
447
|
+
for record in records:
|
|
448
|
+
if not record.elevated:
|
|
449
|
+
temp_lines.append(f" {record.path} (this session)")
|
|
450
|
+
if temp_lines:
|
|
451
|
+
sections.append("Session directories:\n" + "\n".join(temp_lines))
|
|
452
|
+
except Exception:
|
|
453
|
+
pass
|
|
454
|
+
|
|
455
|
+
except Exception:
|
|
456
|
+
pass
|
|
457
|
+
|
|
458
|
+
return "\n\n".join(sections)
|
|
459
|
+
|
|
324
460
|
async def prepare_turn_async(
|
|
325
461
|
self,
|
|
326
462
|
*,
|
|
@@ -331,7 +467,7 @@ class AgentDBClaudeMiddleware(GatewayAdapter):
|
|
|
331
467
|
base_messages: Optional[list[dict[str, str]]] = None,
|
|
332
468
|
working_dirs: Optional[list[str]] = None,
|
|
333
469
|
) -> tuple[list[dict[str, str]], "RetrievalResult"]:
|
|
334
|
-
|
|
470
|
+
messages, retrieval = self.prepare_turn(
|
|
335
471
|
user_query=user_query,
|
|
336
472
|
session_id=session_id,
|
|
337
473
|
system_prompt=system_prompt,
|
|
@@ -340,6 +476,41 @@ class AgentDBClaudeMiddleware(GatewayAdapter):
|
|
|
340
476
|
working_dirs=working_dirs,
|
|
341
477
|
)
|
|
342
478
|
|
|
479
|
+
new_blocks: list[dict[str, str]] = []
|
|
480
|
+
|
|
481
|
+
skills_budget = int(self._capabilities.max_injection_tokens * 0.4)
|
|
482
|
+
workspace_budget = int(self._capabilities.max_injection_tokens * 0.3)
|
|
483
|
+
work_context_budget = int(self._capabilities.max_injection_tokens * 0.3)
|
|
484
|
+
|
|
485
|
+
if self._capabilities.inject_skills:
|
|
486
|
+
block = self._build_skills_block(session_id, user_query)
|
|
487
|
+
if block:
|
|
488
|
+
block = self._trim_to_budget(block, skills_budget)
|
|
489
|
+
if block:
|
|
490
|
+
new_blocks.append({"role": "system", "content": f"## Skills\n{block}"})
|
|
491
|
+
|
|
492
|
+
if self._capabilities.inject_workspace:
|
|
493
|
+
ws = self._build_workspace_block(session_id, working_dirs or [])
|
|
494
|
+
if ws:
|
|
495
|
+
ws = self._trim_to_budget(ws, workspace_budget)
|
|
496
|
+
if ws:
|
|
497
|
+
new_blocks.append({"role": "system", "content": f"## Workspace\n{ws}"})
|
|
498
|
+
|
|
499
|
+
if self._capabilities.inject_work_context:
|
|
500
|
+
wc = self._build_work_context_block(session_id)
|
|
501
|
+
if wc:
|
|
502
|
+
wc = self._trim_to_budget(wc, work_context_budget)
|
|
503
|
+
if wc:
|
|
504
|
+
new_blocks.append({"role": "system", "content": f"## Current work context\n{wc}"})
|
|
505
|
+
|
|
506
|
+
if new_blocks:
|
|
507
|
+
first_user = next(
|
|
508
|
+
(i for i, m in enumerate(messages) if m["role"] == "user"), len(messages)
|
|
509
|
+
)
|
|
510
|
+
messages[first_user:first_user] = new_blocks
|
|
511
|
+
|
|
512
|
+
return messages, retrieval
|
|
513
|
+
|
|
343
514
|
async def finalize_turn_async(
|
|
344
515
|
self,
|
|
345
516
|
*,
|
|
@@ -411,6 +582,73 @@ class AgentDBClaudeMiddleware(GatewayAdapter):
|
|
|
411
582
|
)
|
|
412
583
|
return response_text
|
|
413
584
|
|
|
585
|
+
def record_turn_context(
|
|
586
|
+
self,
|
|
587
|
+
session_id: str,
|
|
588
|
+
turn_num: int,
|
|
589
|
+
files_read: list[str],
|
|
590
|
+
files_written: list[str],
|
|
591
|
+
key_fact: str,
|
|
592
|
+
) -> None:
|
|
593
|
+
"""
|
|
594
|
+
Record turn context to the session work log.
|
|
595
|
+
Caps at 20 turns; drops oldest when exceeding limit.
|
|
596
|
+
"""
|
|
597
|
+
if session_id not in self._session_work_log:
|
|
598
|
+
self._session_work_log[session_id] = []
|
|
599
|
+
|
|
600
|
+
entry = {
|
|
601
|
+
"turn_num": turn_num,
|
|
602
|
+
"files_read": files_read,
|
|
603
|
+
"files_written": files_written,
|
|
604
|
+
"key_fact": key_fact,
|
|
605
|
+
}
|
|
606
|
+
self._session_work_log[session_id].append(entry)
|
|
607
|
+
|
|
608
|
+
# Cap at 20 turns: drop index 0 if list exceeds 20
|
|
609
|
+
if len(self._session_work_log[session_id]) > 20:
|
|
610
|
+
self._session_work_log[session_id].pop(0)
|
|
611
|
+
|
|
612
|
+
def report_directories(
|
|
613
|
+
self,
|
|
614
|
+
session_id: str,
|
|
615
|
+
dirs: list[str],
|
|
616
|
+
) -> None:
|
|
617
|
+
"""
|
|
618
|
+
Report accessed directories for a session.
|
|
619
|
+
Upserts DirectoryRecord entries; caps total list at 200 per session.
|
|
620
|
+
"""
|
|
621
|
+
if session_id not in self._session_dir_state:
|
|
622
|
+
self._session_dir_state[session_id] = []
|
|
623
|
+
|
|
624
|
+
# Build a dict for fast lookup
|
|
625
|
+
records_by_path = {r.path: r for r in self._session_dir_state[session_id]}
|
|
626
|
+
|
|
627
|
+
for path in dirs:
|
|
628
|
+
if path in records_by_path:
|
|
629
|
+
# Upsert: increment access_count and update last_seen_turn
|
|
630
|
+
records_by_path[path].access_count += 1
|
|
631
|
+
records_by_path[path].last_seen_turn = len(self._session_dir_state[session_id])
|
|
632
|
+
else:
|
|
633
|
+
# New entry: check cap before inserting
|
|
634
|
+
if len(self._session_dir_state[session_id]) >= 200:
|
|
635
|
+
# Drop oldest (index 0)
|
|
636
|
+
self._session_dir_state[session_id].pop(0)
|
|
637
|
+
# Rebuild dict after dropping
|
|
638
|
+
records_by_path = {r.path: r for r in self._session_dir_state[session_id]}
|
|
639
|
+
|
|
640
|
+
# Create new record
|
|
641
|
+
turn_proxy = len(self._session_dir_state[session_id])
|
|
642
|
+
new_record = DirectoryRecord(
|
|
643
|
+
path=path,
|
|
644
|
+
first_seen_turn=turn_proxy,
|
|
645
|
+
last_seen_turn=turn_proxy,
|
|
646
|
+
access_count=1,
|
|
647
|
+
elevated=False,
|
|
648
|
+
)
|
|
649
|
+
self._session_dir_state[session_id].append(new_record)
|
|
650
|
+
records_by_path[path] = new_record
|
|
651
|
+
|
|
414
652
|
def _on_conflict(self, **payload) -> None:
|
|
415
653
|
"""
|
|
416
654
|
Platform-agnostic conflict handler.
|
|
@@ -614,218 +852,77 @@ class AgentDBClaudeMiddleware(GatewayAdapter):
|
|
|
614
852
|
)
|
|
615
853
|
}
|
|
616
854
|
|
|
617
|
-
|
|
618
|
-
class AgentDBClaudeRuntime:
|
|
619
|
-
"""
|
|
620
|
-
Higher-level runtime wrapper for easy integration.
|
|
621
|
-
|
|
622
|
-
Developers can instantiate once and call `handle_turn(...)` from their
|
|
623
|
-
Slack/event loop without manually orchestrating middleware hooks.
|
|
624
|
-
"""
|
|
625
|
-
|
|
626
|
-
def __init__(
|
|
855
|
+
def elevate_directory(
|
|
627
856
|
self,
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
|
|
631
|
-
|
|
632
|
-
|
|
633
|
-
):
|
|
634
|
-
self._middleware = middleware
|
|
635
|
-
self._invoke_model = invoke_model
|
|
636
|
-
self._default_system_prompt = default_system_prompt
|
|
637
|
-
self._response_to_text = response_to_text
|
|
638
|
-
self._sdk_client_factory = None
|
|
639
|
-
|
|
640
|
-
@classmethod
|
|
641
|
-
def from_claude_client(
|
|
642
|
-
cls,
|
|
643
|
-
*,
|
|
644
|
-
db: "AgentDB",
|
|
645
|
-
agent_id: str,
|
|
646
|
-
client: Any,
|
|
647
|
-
model: str,
|
|
648
|
-
system_prompt: Optional[str] = None,
|
|
649
|
-
max_tokens: int = 1024,
|
|
650
|
-
) -> "AgentDBClaudeRuntime":
|
|
651
|
-
middleware = AgentDBClaudeMiddleware(db=db, agent_id=agent_id)
|
|
652
|
-
|
|
653
|
-
def _invoke(messages: list[dict[str, str]]) -> Any:
|
|
654
|
-
system_blocks = [m["content"] for m in messages if m.get("role") == "system"]
|
|
655
|
-
chat_messages = [m for m in messages if m.get("role") != "system"]
|
|
656
|
-
if hasattr(client, "messages") and hasattr(client.messages, "create"):
|
|
657
|
-
payload: dict[str, Any] = {
|
|
658
|
-
"model": model,
|
|
659
|
-
"max_tokens": max_tokens,
|
|
660
|
-
"messages": chat_messages,
|
|
661
|
-
}
|
|
662
|
-
if system_blocks:
|
|
663
|
-
payload["system"] = "\n\n".join(system_blocks)
|
|
664
|
-
return client.messages.create(**payload)
|
|
665
|
-
if hasattr(client, "run"):
|
|
666
|
-
return client.run(model=model, messages=messages, max_tokens=max_tokens)
|
|
667
|
-
raise ValueError(
|
|
668
|
-
"Unsupported client: expected `messages.create(...)` or `run(...)` API."
|
|
669
|
-
)
|
|
670
|
-
|
|
671
|
-
return cls(
|
|
672
|
-
middleware=middleware,
|
|
673
|
-
invoke_model=_invoke,
|
|
674
|
-
default_system_prompt=system_prompt,
|
|
675
|
-
response_to_text=_extract_text_from_model_response,
|
|
676
|
-
)
|
|
677
|
-
|
|
678
|
-
@classmethod
|
|
679
|
-
def from_sdk_client(
|
|
680
|
-
cls,
|
|
681
|
-
*,
|
|
682
|
-
db: "AgentDB",
|
|
683
|
-
agent_id: str,
|
|
684
|
-
sdk_client_factory: Callable[[str], Any],
|
|
685
|
-
system_prompt: Optional[str] = None,
|
|
686
|
-
) -> "AgentDBClaudeRuntime":
|
|
857
|
+
session_id: str,
|
|
858
|
+
path: str,
|
|
859
|
+
origin: str = "human_approved",
|
|
860
|
+
confidence: float = 0.8,
|
|
861
|
+
) -> "MemoryRecord":
|
|
687
862
|
"""
|
|
688
|
-
|
|
689
|
-
|
|
690
|
-
sdk_client_factory: callable(thread_id: str) -> client
|
|
691
|
-
The client must support: await client.query(prompt) and
|
|
692
|
-
async-for client.receive_response() yielding messages with
|
|
693
|
-
.content (list of blocks with .text) or .is_error/.result.
|
|
863
|
+
Persist a directory path to AgentDB and mark it elevated in session state.
|
|
864
|
+
Returns the written MemoryRecord.
|
|
694
865
|
"""
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
)
|
|
701
|
-
instance._sdk_client_factory = sdk_client_factory
|
|
702
|
-
return instance
|
|
703
|
-
|
|
704
|
-
async def handle_turn_async(
|
|
705
|
-
self,
|
|
706
|
-
*,
|
|
707
|
-
user_query: str,
|
|
708
|
-
session_id: str,
|
|
709
|
-
message_id: Optional[str] = None,
|
|
710
|
-
system_prompt: Optional[str] = None,
|
|
711
|
-
external_context: Optional[list[dict[str, Any]]] = None,
|
|
712
|
-
base_messages: Optional[list[dict[str, str]]] = None,
|
|
713
|
-
memory_writes: Optional[list[dict[str, Any]]] = None,
|
|
714
|
-
tracked_directories: Optional[list[str]] = None,
|
|
715
|
-
directory_snapshots: Optional[list[dict[str, Any]]] = None,
|
|
716
|
-
tracking_base_dir: Optional[str] = None,
|
|
717
|
-
max_files_per_directory: Optional[int] = None,
|
|
718
|
-
outcome_type: Optional[str] = None,
|
|
719
|
-
outcome_value: Optional[float] = None,
|
|
720
|
-
) -> dict[str, Any]:
|
|
721
|
-
sdk_factory = getattr(self, "_sdk_client_factory", None)
|
|
722
|
-
if sdk_factory is None:
|
|
723
|
-
raise RuntimeError(
|
|
724
|
-
"handle_turn_async requires a runtime built with from_sdk_client(). "
|
|
725
|
-
"Use handle_turn() for the messages.create() path."
|
|
726
|
-
)
|
|
727
|
-
|
|
728
|
-
messages, retrieval = await self._middleware.prepare_turn_async(
|
|
729
|
-
user_query=user_query,
|
|
866
|
+
record = self._db.write(
|
|
867
|
+
key=f"workspace.dir.{path}",
|
|
868
|
+
value=path,
|
|
869
|
+
origin=origin,
|
|
870
|
+
agent_id=self._agent_id,
|
|
730
871
|
session_id=session_id,
|
|
731
|
-
|
|
732
|
-
external_context=external_context,
|
|
733
|
-
base_messages=base_messages,
|
|
872
|
+
confidence=confidence,
|
|
734
873
|
)
|
|
735
874
|
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
744
|
-
|
|
745
|
-
|
|
746
|
-
|
|
747
|
-
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
if content is not None:
|
|
752
|
-
for block in content:
|
|
753
|
-
text = getattr(block, "text", None)
|
|
754
|
-
if text is not None:
|
|
755
|
-
text_parts.append(text)
|
|
756
|
-
|
|
757
|
-
response_text = "\n".join(text_parts).strip()
|
|
875
|
+
if session_id not in self._session_dir_state:
|
|
876
|
+
self._session_dir_state[session_id] = []
|
|
877
|
+
|
|
878
|
+
records_by_path = {r.path: r for r in self._session_dir_state[session_id]}
|
|
879
|
+
if path in records_by_path:
|
|
880
|
+
records_by_path[path].elevated = True
|
|
881
|
+
else:
|
|
882
|
+
new_record = DirectoryRecord(
|
|
883
|
+
path=path,
|
|
884
|
+
first_seen_turn=0,
|
|
885
|
+
last_seen_turn=0,
|
|
886
|
+
access_count=1,
|
|
887
|
+
elevated=True,
|
|
888
|
+
)
|
|
889
|
+
self._session_dir_state[session_id].append(new_record)
|
|
758
890
|
|
|
759
|
-
|
|
760
|
-
session_id=session_id,
|
|
761
|
-
model_response=response_text,
|
|
762
|
-
memory_writes=memory_writes,
|
|
763
|
-
message_id=message_id,
|
|
764
|
-
tracked_directories=tracked_directories,
|
|
765
|
-
directory_snapshots=directory_snapshots,
|
|
766
|
-
tracking_base_dir=tracking_base_dir,
|
|
767
|
-
max_files_per_directory=max_files_per_directory,
|
|
768
|
-
outcome_type=outcome_type,
|
|
769
|
-
outcome_value=outcome_value,
|
|
770
|
-
)
|
|
891
|
+
return record
|
|
771
892
|
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
893
|
+
def write_session_summary(self, session_id: str, jsonl_path: str) -> None:
|
|
894
|
+
"""
|
|
895
|
+
Append a session summary JSON line to jsonl_path (creates file if missing).
|
|
896
|
+
"""
|
|
897
|
+
summary = {
|
|
898
|
+
"type": "session_summary",
|
|
899
|
+
"session_id": session_id,
|
|
900
|
+
"timestamp": datetime.now(timezone.utc).isoformat(),
|
|
901
|
+
"agent_id": self._agent_id,
|
|
902
|
+
"agentdb_checkpoint_key": f"memory/compaction/checkpoint/{session_id}",
|
|
903
|
+
"working_dirs": self._session_working_dirs.get(session_id, []),
|
|
904
|
+
"temp_dirs": [r.path for r in self._session_dir_state.get(session_id, []) if not r.elevated],
|
|
905
|
+
"elevated_dirs": [r.path for r in self._session_dir_state.get(session_id, []) if r.elevated],
|
|
906
|
+
"turn_count": len(self._session_work_log.get(session_id, [])),
|
|
907
|
+
"last_work_context": self._build_work_context_block(session_id),
|
|
777
908
|
}
|
|
909
|
+
with open(jsonl_path, "a", encoding="utf-8") as f:
|
|
910
|
+
f.write(json.dumps(summary) + "\n")
|
|
778
911
|
|
|
779
|
-
def
|
|
912
|
+
async def end_session(
|
|
780
913
|
self,
|
|
781
|
-
*,
|
|
782
|
-
user_query: str,
|
|
783
914
|
session_id: str,
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
|
|
788
|
-
|
|
789
|
-
tracked_directories: Optional[list[str]] = None,
|
|
790
|
-
directory_snapshots: Optional[list[dict[str, Any]]] = None,
|
|
791
|
-
tracking_base_dir: Optional[str] = None,
|
|
792
|
-
max_files_per_directory: Optional[int] = None,
|
|
793
|
-
outcome_type: Optional[str] = None,
|
|
794
|
-
outcome_value: Optional[float] = None,
|
|
795
|
-
) -> dict[str, Any]:
|
|
796
|
-
messages, retrieval = self._middleware.prepare_turn(
|
|
797
|
-
user_query=user_query,
|
|
798
|
-
session_id=session_id,
|
|
799
|
-
system_prompt=system_prompt if system_prompt is not None else self._default_system_prompt,
|
|
800
|
-
external_context=external_context,
|
|
801
|
-
base_messages=base_messages,
|
|
802
|
-
)
|
|
803
|
-
raw_response = self._invoke_model(messages)
|
|
804
|
-
response_text = self._response_to_text(raw_response)
|
|
805
|
-
self._middleware.finalize_turn(
|
|
806
|
-
session_id=session_id,
|
|
807
|
-
model_response=response_text,
|
|
808
|
-
memory_writes=memory_writes,
|
|
809
|
-
message_id=message_id,
|
|
810
|
-
tracked_directories=tracked_directories,
|
|
811
|
-
directory_snapshots=directory_snapshots,
|
|
812
|
-
tracking_base_dir=tracking_base_dir,
|
|
813
|
-
max_files_per_directory=max_files_per_directory,
|
|
814
|
-
outcome_type=outcome_type,
|
|
815
|
-
outcome_value=outcome_value,
|
|
816
|
-
)
|
|
817
|
-
return {
|
|
818
|
-
"response_text": response_text,
|
|
819
|
-
"raw_response": raw_response,
|
|
820
|
-
"retrieval": retrieval,
|
|
821
|
-
"messages": messages,
|
|
822
|
-
}
|
|
823
|
-
|
|
824
|
-
def get_sdk_hooks(self) -> dict[str, Any]:
|
|
915
|
+
jsonl_path: str,
|
|
916
|
+
summary: Optional[dict] = None,
|
|
917
|
+
run_consolidation: bool = False,
|
|
918
|
+
llm_fn: Optional[Callable] = None,
|
|
919
|
+
) -> None:
|
|
825
920
|
"""
|
|
826
|
-
|
|
827
|
-
|
|
828
|
-
Returns:
|
|
829
|
-
{"PreToolUse": callable}
|
|
921
|
+
Finalize a session: checkpoint, optionally consolidate, write summary, clear state.
|
|
830
922
|
"""
|
|
831
|
-
|
|
923
|
+
self._db.checkpoint(agent_id=self._agent_id, session_id=session_id)
|
|
924
|
+
if run_consolidation:
|
|
925
|
+
self._db.consolidate(agent_id=self._agent_id, llm_fn=llm_fn)
|
|
926
|
+
self.write_session_summary(session_id, jsonl_path)
|
|
927
|
+
self._session_dir_state.pop(session_id, None)
|
|
928
|
+
self._session_work_log.pop(session_id, None)
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
{agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/skills/evolution/skillopt_adapter.py
RENAMED
|
File without changes
|
{agentdatabase-0.2.0 → agentdatabase-0.3.0}/src/agentdb/skills/evolution/wikiskill_adapter.py
RENAMED
|
File without changes
|
|
File without changes
|
|
File without changes
|