seed-kernel 1.0.11__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.
- seed/__init__.py +125 -0
- seed/cli.py +102 -0
- seed/core/__init__.py +8 -0
- seed/core/_session_cache.py +41 -0
- seed/core/agent_context.py +163 -0
- seed/core/agent_registry.py +180 -0
- seed/core/agent_runtime.py +2384 -0
- seed/core/chat_events.py +70 -0
- seed/core/commands.py +149 -0
- seed/core/config_plane.py +537 -0
- seed/core/engine.py +150 -0
- seed/core/env_access.py +168 -0
- seed/core/execution.py +148 -0
- seed/core/llm_exec.py +1652 -0
- seed/core/llm_presets.py +305 -0
- seed/core/llm_sess.py +1094 -0
- seed/core/media_store.py +84 -0
- seed/core/mem_bridge.py +294 -0
- seed/core/mem_sys.py +409 -0
- seed/core/model_providers.py +88 -0
- seed/core/models.py +211 -0
- seed/core/paths.py +109 -0
- seed/core/persistence.py +133 -0
- seed/core/proj_reg.py +609 -0
- seed/core/proj_todos.py +141 -0
- seed/core/projection_audit.py +317 -0
- seed/core/routing.py +192 -0
- seed/core/sess_store.py +44 -0
- seed/core/tool_output_cap.py +122 -0
- seed/core/tool_runtime.py +330 -0
- seed/core/trace_audit.py +123 -0
- seed/core/turn_loop.py +750 -0
- seed/core/usage_accumulator.py +72 -0
- seed/integrations/__init__.py +33 -0
- seed/integrations/agent_tools.py +160 -0
- seed/integrations/cron_sched.py +773 -0
- seed/integrations/env_config.py +77 -0
- seed/integrations/exec_backend.py +432 -0
- seed/integrations/hooks.py +61 -0
- seed/integrations/hooks_config.py +109 -0
- seed/integrations/instruction_release.py +256 -0
- seed/integrations/lsp_client.py +219 -0
- seed/integrations/lsp_config.py +99 -0
- seed/integrations/mcp_client.py +1283 -0
- seed/integrations/mcp_config.py +208 -0
- seed/integrations/mcp_registry.py +154 -0
- seed/integrations/message_api.py +16 -0
- seed/integrations/prompt_task.py +39 -0
- seed/integrations/safety.py +331 -0
- seed/integrations/session_title.py +211 -0
- seed/integrations/symbol_index.py +311 -0
- seed/integrations/task_runner.py +275 -0
- seed/integrations/webhook_auth.py +33 -0
- seed/integrations/webhook_dedup.py +132 -0
- seed/models.py +31 -0
- seed_kernel-1.0.11.dist-info/METADATA +17 -0
- seed_kernel-1.0.11.dist-info/RECORD +60 -0
- seed_kernel-1.0.11.dist-info/WHEEL +5 -0
- seed_kernel-1.0.11.dist-info/entry_points.txt +2 -0
- seed_kernel-1.0.11.dist-info/top_level.txt +1 -0
seed/core/media_store.py
ADDED
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
"""Session-scoped media file storage (attachments for vision tools)."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
import os
|
|
6
|
+
import re
|
|
7
|
+
import uuid
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
from typing import Optional
|
|
10
|
+
|
|
11
|
+
_SAFE = re.compile(r"[^a-zA-Z0-9._-]+")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
def _safe_segment(name: str, *, max_len: int = 64) -> str:
|
|
15
|
+
s = _SAFE.sub("_", (name or "").strip()).strip("._-") or "file"
|
|
16
|
+
return s[:max_len]
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def attachments_root(agent_id: str) -> Path:
|
|
20
|
+
from seed.core.llm_sess import agent_sessions_dir
|
|
21
|
+
|
|
22
|
+
root = agent_sessions_dir(agent_id) / "attachments"
|
|
23
|
+
root.mkdir(parents=True, exist_ok=True)
|
|
24
|
+
return root
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def session_attachments_dir(agent_id: str, session_id: str) -> Path:
|
|
28
|
+
d = attachments_root(agent_id) / _safe_segment(session_id, max_len=128)
|
|
29
|
+
d.mkdir(parents=True, exist_ok=True)
|
|
30
|
+
return d
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def save_session_media(
|
|
34
|
+
*,
|
|
35
|
+
agent_id: str,
|
|
36
|
+
session_id: str,
|
|
37
|
+
raw_bytes: bytes,
|
|
38
|
+
filename: str,
|
|
39
|
+
mime: str = "",
|
|
40
|
+
attachment_id: Optional[str] = None,
|
|
41
|
+
) -> tuple[str, Path]:
|
|
42
|
+
"""Persist bytes under session attachments; return (attachment_id, absolute path)."""
|
|
43
|
+
aid = (attachment_id or "").strip() or uuid.uuid4().hex[:12]
|
|
44
|
+
ext = Path(filename or "blob").suffix.lower()
|
|
45
|
+
if not ext or len(ext) > 10:
|
|
46
|
+
ext = _guess_ext(mime) or ".bin"
|
|
47
|
+
safe_name = _safe_segment(Path(filename or "file").stem, max_len=48)
|
|
48
|
+
out = session_attachments_dir(agent_id, session_id) / f"{aid}_{safe_name}{ext}"
|
|
49
|
+
out.write_bytes(raw_bytes)
|
|
50
|
+
return aid, out.resolve()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def resolve_session_media_path(
|
|
54
|
+
agent_id: str,
|
|
55
|
+
session_id: str,
|
|
56
|
+
attachment_id: str,
|
|
57
|
+
) -> Optional[Path]:
|
|
58
|
+
"""Find attachment file by id prefix in session directory."""
|
|
59
|
+
aid = (attachment_id or "").strip()
|
|
60
|
+
if not aid:
|
|
61
|
+
return None
|
|
62
|
+
base = session_attachments_dir(agent_id, session_id)
|
|
63
|
+
if not base.is_dir():
|
|
64
|
+
return None
|
|
65
|
+
for p in base.iterdir():
|
|
66
|
+
if not p.is_file():
|
|
67
|
+
continue
|
|
68
|
+
if p.name == aid or p.name.startswith(f"{aid}_"):
|
|
69
|
+
return p.resolve()
|
|
70
|
+
return None
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def _guess_ext(mime: str) -> str:
|
|
74
|
+
m = (mime or "").lower().split(";")[0].strip()
|
|
75
|
+
return {
|
|
76
|
+
"image/png": ".png",
|
|
77
|
+
"image/jpeg": ".jpg",
|
|
78
|
+
"image/jpg": ".jpg",
|
|
79
|
+
"image/webp": ".webp",
|
|
80
|
+
"image/gif": ".gif",
|
|
81
|
+
"application/pdf": ".pdf",
|
|
82
|
+
"text/plain": ".txt",
|
|
83
|
+
"text/markdown": ".md",
|
|
84
|
+
}.get(m, "")
|
seed/core/mem_bridge.py
ADDED
|
@@ -0,0 +1,294 @@
|
|
|
1
|
+
"""Episodic memory: project-scoped experience snippets injected at compact boundaries."""
|
|
2
|
+
from __future__ import annotations
|
|
3
|
+
|
|
4
|
+
import re
|
|
5
|
+
from datetime import datetime, timedelta, timezone
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Any, Dict, List, Optional
|
|
8
|
+
|
|
9
|
+
from seed.core import env_access as _ea
|
|
10
|
+
from seed.core.mem_sys import MemorySystem
|
|
11
|
+
|
|
12
|
+
_METADATA_EPISODIC_BLOCK = "episodic_block"
|
|
13
|
+
_METADATA_EPISODIC_PROJECT_ID = "episodic_project_id"
|
|
14
|
+
_METADATA_EPISODIC_REFRESHED_AT = "episodic_refreshed_at"
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
_EPISODIC_START = "\n## Seed episodic memory (recent)\n"
|
|
18
|
+
_EPISODIC_END = "\n## End Seed episodic memory\n"
|
|
19
|
+
|
|
20
|
+
_EPISODIC_BLOCK = re.compile(
|
|
21
|
+
r"(?:\n## CodeAgent episodic memory \(recent\)\n.*?\n## End CodeAgent episodic memory\n"
|
|
22
|
+
r"|\n## Seed episodic memory \(recent\)\n.*?\n## End Seed episodic memory\n)",
|
|
23
|
+
re.DOTALL,
|
|
24
|
+
)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
# Match a ``## TTL`` section that gives a lifetime in seconds.
|
|
28
|
+
# ## TTL
|
|
29
|
+
# 600
|
|
30
|
+
# Or on one line:
|
|
31
|
+
# ## TTL: 600
|
|
32
|
+
_TTL_SECTION_RE = re.compile(
|
|
33
|
+
r"^\s*##\s*TTL\s*[::]?\s*(?P<inline>\d+)?\s*$(?:\r?\n\s*(?P<below>\d+)\s*$)?",
|
|
34
|
+
re.IGNORECASE | re.MULTILINE,
|
|
35
|
+
)
|
|
36
|
+
|
|
37
|
+
# Match a ``## Expires`` section giving an absolute ISO-8601 timestamp.
|
|
38
|
+
# ## Expires
|
|
39
|
+
# 2026-04-20T15:00:00Z
|
|
40
|
+
_EXPIRES_SECTION_RE = re.compile(
|
|
41
|
+
r"^\s*##\s*Expires\s*[::]?\s*(?P<inline>[0-9T:\-+Z \.]+)?\s*$"
|
|
42
|
+
r"(?:\r?\n\s*(?P<below>[0-9T:\-+Z \.]+)\s*$)?",
|
|
43
|
+
re.IGNORECASE | re.MULTILINE,
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def strip_episodic_block(system_text: str) -> str:
|
|
48
|
+
return _EPISODIC_BLOCK.sub("\n", system_text or "").strip()
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
def _parse_ttl_seconds(text: str) -> Optional[int]:
|
|
52
|
+
m = _TTL_SECTION_RE.search(text or "")
|
|
53
|
+
if not m:
|
|
54
|
+
return None
|
|
55
|
+
raw = (m.group("inline") or m.group("below") or "").strip()
|
|
56
|
+
if not raw:
|
|
57
|
+
return None
|
|
58
|
+
try:
|
|
59
|
+
v = int(raw)
|
|
60
|
+
except ValueError:
|
|
61
|
+
return None
|
|
62
|
+
return v if v > 0 else None
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def _parse_expires_dt(text: str) -> Optional[datetime]:
|
|
66
|
+
m = _EXPIRES_SECTION_RE.search(text or "")
|
|
67
|
+
if not m:
|
|
68
|
+
return None
|
|
69
|
+
raw = (m.group("inline") or m.group("below") or "").strip()
|
|
70
|
+
if not raw:
|
|
71
|
+
return None
|
|
72
|
+
# Accept trailing Z (Zulu) as UTC.
|
|
73
|
+
iso = raw.replace("Z", "+00:00")
|
|
74
|
+
try:
|
|
75
|
+
dt = datetime.fromisoformat(iso)
|
|
76
|
+
except ValueError:
|
|
77
|
+
return None
|
|
78
|
+
if dt.tzinfo is None:
|
|
79
|
+
dt = dt.replace(tzinfo=timezone.utc)
|
|
80
|
+
return dt
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _is_expired(path: Path, text: str, *, now: Optional[datetime] = None) -> bool:
|
|
84
|
+
"""True if TTL / Expires markers say this experience file is stale."""
|
|
85
|
+
cur = now or datetime.now(timezone.utc)
|
|
86
|
+
exp = _parse_expires_dt(text)
|
|
87
|
+
if exp is not None:
|
|
88
|
+
return cur >= exp
|
|
89
|
+
ttl = _parse_ttl_seconds(text)
|
|
90
|
+
if ttl is not None:
|
|
91
|
+
try:
|
|
92
|
+
mtime = datetime.fromtimestamp(path.stat().st_mtime, tz=timezone.utc)
|
|
93
|
+
except OSError:
|
|
94
|
+
return False
|
|
95
|
+
return cur >= mtime + timedelta(seconds=ttl)
|
|
96
|
+
return False
|
|
97
|
+
|
|
98
|
+
|
|
99
|
+
def experience_file_expired(path: Path, text: str, *, now: Optional[datetime] = None) -> bool:
|
|
100
|
+
"""True if ``## TTL`` / ``## Expires`` in this experience markdown is past (same rules as episodic inject)."""
|
|
101
|
+
return _is_expired(path, text, now=now)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def _td_seconds(seconds: int):
|
|
105
|
+
# Imported lazily to avoid top-level import noise.
|
|
106
|
+
from datetime import timedelta
|
|
107
|
+
return timedelta(seconds=seconds)
|
|
108
|
+
|
|
109
|
+
|
|
110
|
+
def parsed_experience_project_id(text: str) -> Optional[str]:
|
|
111
|
+
"""Return canonical project id from ``## Project`` section, or None if absent."""
|
|
112
|
+
m = re.search(r"(?im)^##\s*Project\s*\n\s*(\S+)", text or "")
|
|
113
|
+
if not m:
|
|
114
|
+
return None
|
|
115
|
+
return (m.group(1) or "").strip() or None
|
|
116
|
+
|
|
117
|
+
|
|
118
|
+
def build_episodic_snippets(
|
|
119
|
+
project_root: Path,
|
|
120
|
+
*,
|
|
121
|
+
session_id: Optional[str],
|
|
122
|
+
max_chars: int,
|
|
123
|
+
session_only: bool,
|
|
124
|
+
project_id: Optional[str] = None,
|
|
125
|
+
project_scope: bool = False,
|
|
126
|
+
) -> str:
|
|
127
|
+
"""
|
|
128
|
+
Recent experience files (newest first), optional filter by logged ## Session field.
|
|
129
|
+
|
|
130
|
+
Files carrying an explicit ``## TTL`` (seconds from mtime) or ``## Expires``
|
|
131
|
+
(absolute ISO-8601 timestamp) are **skipped once expired** so stale
|
|
132
|
+
runtime facts (PID/port/"running" snapshots) stop being re-injected.
|
|
133
|
+
"""
|
|
134
|
+
try:
|
|
135
|
+
# If caller passes agents/<id>/memory, MemorySystem will use it directly.
|
|
136
|
+
ms = MemorySystem(base_path=project_root.resolve())
|
|
137
|
+
except Exception:
|
|
138
|
+
return ""
|
|
139
|
+
exp_dir = ms.memory_path / "experiences"
|
|
140
|
+
if not exp_dir.is_dir():
|
|
141
|
+
return ""
|
|
142
|
+
|
|
143
|
+
files = sorted(exp_dir.glob("*.md"), key=lambda p: p.stat().st_mtime, reverse=True)[:30]
|
|
144
|
+
parts: List[str] = []
|
|
145
|
+
total = 0
|
|
146
|
+
now = datetime.now(timezone.utc)
|
|
147
|
+
for f in files:
|
|
148
|
+
try:
|
|
149
|
+
text = f.read_text(encoding="utf-8", errors="replace")
|
|
150
|
+
except OSError:
|
|
151
|
+
continue
|
|
152
|
+
if _is_expired(f, text, now=now):
|
|
153
|
+
continue
|
|
154
|
+
if session_only and session_id:
|
|
155
|
+
if "## session" in text.lower():
|
|
156
|
+
if session_id not in text:
|
|
157
|
+
continue
|
|
158
|
+
if project_scope:
|
|
159
|
+
exp_proj = parsed_experience_project_id(text)
|
|
160
|
+
want = (project_id or "").strip()
|
|
161
|
+
if want:
|
|
162
|
+
if exp_proj != want:
|
|
163
|
+
continue
|
|
164
|
+
else:
|
|
165
|
+
if exp_proj is not None:
|
|
166
|
+
continue
|
|
167
|
+
snippet = text.strip()
|
|
168
|
+
if len(snippet) > 900:
|
|
169
|
+
snippet = snippet[:450] + "\n…\n" + snippet[-400:]
|
|
170
|
+
chunk = f"### {f.name}\n{snippet}\n"
|
|
171
|
+
if total + len(chunk) > max_chars:
|
|
172
|
+
break
|
|
173
|
+
parts.append(chunk)
|
|
174
|
+
total += len(chunk)
|
|
175
|
+
return "\n".join(parts).strip()
|
|
176
|
+
|
|
177
|
+
|
|
178
|
+
def episodic_memory_base(agent_id: str, project_id: Optional[str] = None) -> Path:
|
|
179
|
+
"""Directory root passed to ``MemorySystem`` (``.../memory`` under agent or project)."""
|
|
180
|
+
from seed.core.paths import agent_id_default, agent_memory_dir, agent_project_data_subdir
|
|
181
|
+
|
|
182
|
+
aid = (agent_id or "").strip() or agent_id_default()
|
|
183
|
+
pid = (project_id or "").strip()
|
|
184
|
+
if pid:
|
|
185
|
+
return agent_project_data_subdir(aid, pid, "memory")
|
|
186
|
+
return agent_memory_dir(aid)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def _utc_iso() -> str:
|
|
190
|
+
return datetime.now(timezone.utc).isoformat()
|
|
191
|
+
|
|
192
|
+
|
|
193
|
+
def episodic_project_changed(metadata: Optional[Dict[str, Any]], project_id: Optional[str]) -> bool:
|
|
194
|
+
"""True when a prior episodic snapshot exists and the bound project id changed."""
|
|
195
|
+
if not isinstance(metadata, dict):
|
|
196
|
+
return False
|
|
197
|
+
if _METADATA_EPISODIC_PROJECT_ID not in metadata:
|
|
198
|
+
return False
|
|
199
|
+
want = (project_id or "").strip()
|
|
200
|
+
got = str(metadata.get(_METADATA_EPISODIC_PROJECT_ID) or "").strip()
|
|
201
|
+
return got != want
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def episodic_needs_bootstrap(metadata: Optional[Dict[str, Any]]) -> bool:
|
|
205
|
+
"""True when this session has never scanned experiences (first LLM turn)."""
|
|
206
|
+
if _ea.pick_default("1", *_ea.MEMORY_INJECT).lower() in ("0", "false", "no"):
|
|
207
|
+
return False
|
|
208
|
+
if not isinstance(metadata, dict):
|
|
209
|
+
return True
|
|
210
|
+
return _METADATA_EPISODIC_BLOCK not in metadata
|
|
211
|
+
|
|
212
|
+
|
|
213
|
+
def refresh_episodic_snapshot(
|
|
214
|
+
metadata: Dict[str, Any],
|
|
215
|
+
agent_id: str,
|
|
216
|
+
session_id: Optional[str],
|
|
217
|
+
project_id: Optional[str] = None,
|
|
218
|
+
) -> None:
|
|
219
|
+
"""
|
|
220
|
+
Scan ``memory/experiences`` and store a frozen block on session metadata.
|
|
221
|
+
|
|
222
|
+
Called on first LLM turn (bootstrap), after context compact, or when project binding changes.
|
|
223
|
+
Not on every LLM turn thereafter.
|
|
224
|
+
"""
|
|
225
|
+
pid = (project_id or "").strip()
|
|
226
|
+
if _ea.pick_default("1", *_ea.MEMORY_INJECT).lower() in ("0", "false", "no"):
|
|
227
|
+
metadata.pop(_METADATA_EPISODIC_BLOCK, None)
|
|
228
|
+
metadata.pop(_METADATA_EPISODIC_PROJECT_ID, None)
|
|
229
|
+
metadata.pop(_METADATA_EPISODIC_REFRESHED_AT, None)
|
|
230
|
+
return
|
|
231
|
+
|
|
232
|
+
max_c = int(_ea.pick_default("5000", *_ea.MEMORY_INJECT_MAX_CHARS))
|
|
233
|
+
session_only = _ea.pick_default("", *_ea.MEMORY_INJECT_SESSION_ONLY).lower() in (
|
|
234
|
+
"1",
|
|
235
|
+
"true",
|
|
236
|
+
"yes",
|
|
237
|
+
)
|
|
238
|
+
snippets = build_episodic_snippets(
|
|
239
|
+
episodic_memory_base(agent_id, pid or None),
|
|
240
|
+
session_id=session_id,
|
|
241
|
+
max_chars=max_c,
|
|
242
|
+
session_only=session_only,
|
|
243
|
+
project_id=pid or None,
|
|
244
|
+
project_scope=bool(pid),
|
|
245
|
+
)
|
|
246
|
+
if snippets:
|
|
247
|
+
metadata[_METADATA_EPISODIC_BLOCK] = _EPISODIC_START + snippets + _EPISODIC_END
|
|
248
|
+
else:
|
|
249
|
+
metadata[_METADATA_EPISODIC_BLOCK] = ""
|
|
250
|
+
metadata[_METADATA_EPISODIC_PROJECT_ID] = pid
|
|
251
|
+
metadata[_METADATA_EPISODIC_REFRESHED_AT] = _utc_iso()
|
|
252
|
+
|
|
253
|
+
|
|
254
|
+
def apply_persisted_episodic_to_messages(
|
|
255
|
+
messages: List[Dict[str, Any]],
|
|
256
|
+
metadata: Optional[Dict[str, Any]],
|
|
257
|
+
) -> None:
|
|
258
|
+
"""Append metadata episodic block to ``messages[0]`` (after compact/skills content)."""
|
|
259
|
+
if not messages or messages[0].get("role") != "system":
|
|
260
|
+
return
|
|
261
|
+
base = strip_episodic_block(str(messages[0].get("content") or ""))
|
|
262
|
+
if _ea.pick_default("1", *_ea.MEMORY_INJECT).lower() in ("0", "false", "no"):
|
|
263
|
+
messages[0]["content"] = base
|
|
264
|
+
return
|
|
265
|
+
block = ""
|
|
266
|
+
if isinstance(metadata, dict):
|
|
267
|
+
raw = metadata.get(_METADATA_EPISODIC_BLOCK)
|
|
268
|
+
if isinstance(raw, str):
|
|
269
|
+
block = raw.strip()
|
|
270
|
+
messages[0]["content"] = (base + block) if block else base
|
|
271
|
+
|
|
272
|
+
|
|
273
|
+
def finalize_episodic_for_llm(
|
|
274
|
+
messages: List[Dict[str, Any]],
|
|
275
|
+
metadata: Optional[Dict[str, Any]],
|
|
276
|
+
*,
|
|
277
|
+
agent_id: str,
|
|
278
|
+
session_id: Optional[str],
|
|
279
|
+
project_id: Optional[str] = None,
|
|
280
|
+
compact_happened: bool = False,
|
|
281
|
+
) -> None:
|
|
282
|
+
"""
|
|
283
|
+
Refresh episodic snapshot on session bootstrap, compact, or project change; apply to api messages.
|
|
284
|
+
"""
|
|
285
|
+
md = metadata if isinstance(metadata, dict) else {}
|
|
286
|
+
if (
|
|
287
|
+
episodic_needs_bootstrap(md)
|
|
288
|
+
or compact_happened
|
|
289
|
+
or episodic_project_changed(md, project_id)
|
|
290
|
+
):
|
|
291
|
+
refresh_episodic_snapshot(md, agent_id, session_id, project_id)
|
|
292
|
+
apply_persisted_episodic_to_messages(messages, md)
|
|
293
|
+
|
|
294
|
+
|