flowstatemcp 2.5.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.
- flowstate/__init__.py +34 -0
- flowstate/__main__.py +7 -0
- flowstate/activity.py +224 -0
- flowstate/agent_guide.py +251 -0
- flowstate/app_keys.py +141 -0
- flowstate/approvals.py +974 -0
- flowstate/approve_page.py +565 -0
- flowstate/ask.py +107 -0
- flowstate/autosession.py +480 -0
- flowstate/backup.py +201 -0
- flowstate/brief.py +420 -0
- flowstate/card_offer.py +208 -0
- flowstate/card_taps.py +165 -0
- flowstate/card_trust.py +127 -0
- flowstate/claims.py +461 -0
- flowstate/cleanup.py +619 -0
- flowstate/cli.py +728 -0
- flowstate/cli_data.py +434 -0
- flowstate/cli_more.py +302 -0
- flowstate/cli_review.py +459 -0
- flowstate/cloudflared_check.py +49 -0
- flowstate/conflict_card_ui.py +286 -0
- flowstate/conflict_cards.py +1193 -0
- flowstate/conflict_reopen.py +320 -0
- flowstate/conflicts.py +420 -0
- flowstate/connections.py +33 -0
- flowstate/database.py +2191 -0
- flowstate/dbconn.py +308 -0
- flowstate/deletes.py +860 -0
- flowstate/embeddings.py +354 -0
- flowstate/erase.py +1047 -0
- flowstate/extras.py +71 -0
- flowstate/git_capture.py +157 -0
- flowstate/guard.py +224 -0
- flowstate/handoffs.py +168 -0
- flowstate/history.py +755 -0
- flowstate/history_page.py +276 -0
- flowstate/http_app.py +204 -0
- flowstate/identity.py +387 -0
- flowstate/install_name.py +140 -0
- flowstate/ledger.py +272 -0
- flowstate/legacy_args.py +65 -0
- flowstate/links.py +609 -0
- flowstate/markdown_io.py +242 -0
- flowstate/meaning.py +398 -0
- flowstate/memory_import.py +92 -0
- flowstate/migrations/001_auto_sessions.sql +16 -0
- flowstate/migrations/002_review_queue.sql +33 -0
- flowstate/migrations/003_links.sql +47 -0
- flowstate/migrations/004_review_mode.sql +24 -0
- flowstate/migrations/005_history.sql +21 -0
- flowstate/migrations/006_activity.sql +26 -0
- flowstate/migrations/007_meaning_index.sql +16 -0
- flowstate/migrations/008_git_capture.sql +12 -0
- flowstate/migrations/009_hard_rules.sql +13 -0
- flowstate/migrations/010_handoffs.sql +14 -0
- flowstate/migrations/011_record_meta.sql +13 -0
- flowstate/migrations/012_accountability.sql +71 -0
- flowstate/migrations/013_todo_claims.sql +36 -0
- flowstate/migrations/014_decision_conflicts.sql +41 -0
- flowstate/migrations/015_session_runs.sql +17 -0
- flowstate/migrations/016_write_receipts.sql +35 -0
- flowstate/migrations/017_run_views.sql +26 -0
- flowstate/migrations/018_run_views_upto.sql +4 -0
- flowstate/migrations/019_approvals.sql +47 -0
- flowstate/migrations/020_source_stamps.sql +10 -0
- flowstate/migrations/021_review_links.sql +11 -0
- flowstate/migrations/022_review_start_tickets.sql +10 -0
- flowstate/migrations/023_conflict_cards.sql +68 -0
- flowstate/migrations/024_rule_confirmations.sql +7 -0
- flowstate/migrations/025_confirmed_hash.sql +7 -0
- flowstate/migrations/__init__.py +149 -0
- flowstate/models.py +307 -0
- flowstate/names.py +157 -0
- flowstate/offsite.py +386 -0
- flowstate/offsite_crypto.py +272 -0
- flowstate/paths.py +265 -0
- flowstate/popup.py +238 -0
- flowstate/priority.py +66 -0
- flowstate/privatefs.py +476 -0
- flowstate/project_history.py +710 -0
- flowstate/provenance.py +696 -0
- flowstate/receipts.py +377 -0
- flowstate/relay/__init__.py +72 -0
- flowstate/relay/acme.py +271 -0
- flowstate/relay/api.py +164 -0
- flowstate/relay/cli.py +344 -0
- flowstate/relay/crypto.py +137 -0
- flowstate/relay/ct.py +118 -0
- flowstate/relay/runner.py +177 -0
- flowstate/relay/store.py +191 -0
- flowstate/relay/tunnel.py +413 -0
- flowstate/release_signers +14 -0
- flowstate/remote_auth.py +1219 -0
- flowstate/review.py +320 -0
- flowstate/review_apply.py +140 -0
- flowstate/review_card_ui.py +241 -0
- flowstate/review_cards.py +564 -0
- flowstate/review_mode.py +274 -0
- flowstate/review_page.py +1361 -0
- flowstate/review_phone.py +465 -0
- flowstate/review_tunnel.py +338 -0
- flowstate/routing.py +392 -0
- flowstate/rules.py +418 -0
- flowstate/safety.py +98 -0
- flowstate/saves.py +83 -0
- flowstate/scheduler.py +160 -0
- flowstate/schema.sql +713 -0
- flowstate/search.py +355 -0
- flowstate/secret_store.py +271 -0
- flowstate/secrets_scan.py +135 -0
- flowstate/selfupdate.py +1020 -0
- flowstate/server.py +1299 -0
- flowstate/session_contents.py +382 -0
- flowstate/session_records.py +629 -0
- flowstate/site_style.py +46 -0
- flowstate/stored.py +158 -0
- flowstate/story.py +442 -0
- flowstate/surface.py +173 -0
- flowstate/tasks.py +231 -0
- flowstate/term.py +40 -0
- flowstate/tool_labels.py +104 -0
- flowstate/tooldefs.py +383 -0
- flowstate/tools/__init__.py +118 -0
- flowstate/tools/core.py +545 -0
- flowstate/tools/files.py +416 -0
- flowstate/tools/git_ops.py +1081 -0
- flowstate/tools/intelligence.py +376 -0
- flowstate/tools/learning.py +282 -0
- flowstate/tools/problems.py +159 -0
- flowstate/tools/sessions.py +266 -0
- flowstate/tools/story.py +48 -0
- flowstate/tools/todos.py +79 -0
- flowstate/tools/utils.py +71 -0
- flowstate/tools/variables.py +345 -0
- flowstate/updates.py +482 -0
- flowstate/urls.py +7 -0
- flowstate/waiting.py +828 -0
- flowstate/who.py +302 -0
- flowstatemcp-2.5.0.dist-info/METADATA +88 -0
- flowstatemcp-2.5.0.dist-info/RECORD +143 -0
- flowstatemcp-2.5.0.dist-info/WHEEL +4 -0
- flowstatemcp-2.5.0.dist-info/entry_points.txt +3 -0
flowstate/tools/files.py
ADDED
|
@@ -0,0 +1,416 @@
|
|
|
1
|
+
"""File attachment tools."""
|
|
2
|
+
|
|
3
|
+
import os
|
|
4
|
+
import shutil
|
|
5
|
+
import hashlib
|
|
6
|
+
from pathlib import Path
|
|
7
|
+
from typing import Optional
|
|
8
|
+
from .utils import get_db, data_dir
|
|
9
|
+
|
|
10
|
+
def _inside(base: Path, path: Path) -> Path:
|
|
11
|
+
"""`path` when it stays inside `base` (links resolved), else ValueError (audit N-F25: a
|
|
12
|
+
project named `../../x`, or a link under projects/, must not lead outside the data folder)."""
|
|
13
|
+
base_r, path_r = base.resolve(), path.resolve()
|
|
14
|
+
if path_r != base_r and base_r not in path_r.parents:
|
|
15
|
+
raise ValueError("That path would lead outside FlowState's projects folder; refused.")
|
|
16
|
+
return path
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def _copy_new(src: Path, dest: Path) -> None:
|
|
20
|
+
"""Copy into a file that doesn't exist yet, never through a link (task 56, N6: a link
|
|
21
|
+
pointing nowhere made shutil.copy2 write wherever it pointed). Owner-only, like FlowState's
|
|
22
|
+
other files."""
|
|
23
|
+
flags = os.O_WRONLY | os.O_CREAT | os.O_EXCL | getattr(os, "O_NOFOLLOW", 0) \
|
|
24
|
+
| getattr(os, "O_BINARY", 0)
|
|
25
|
+
fd = os.open(str(dest), flags, 0o600)
|
|
26
|
+
try:
|
|
27
|
+
with open(src, "rb") as fin, os.fdopen(fd, "wb") as fout:
|
|
28
|
+
fd = None
|
|
29
|
+
shutil.copyfileobj(fin, fout)
|
|
30
|
+
except BaseException:
|
|
31
|
+
if fd is not None:
|
|
32
|
+
os.close(fd)
|
|
33
|
+
try:
|
|
34
|
+
dest.unlink()
|
|
35
|
+
except OSError:
|
|
36
|
+
pass
|
|
37
|
+
raise
|
|
38
|
+
try:
|
|
39
|
+
st = os.stat(src)
|
|
40
|
+
os.utime(dest, ns=(st.st_atime_ns, st.st_mtime_ns))
|
|
41
|
+
except OSError:
|
|
42
|
+
pass
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def _get_project_bundle_path(project_name: str) -> Path:
|
|
46
|
+
"""Get the path to a project's bundle directory (always inside data_dir()/projects)."""
|
|
47
|
+
base = data_dir() / "projects"
|
|
48
|
+
slug = project_name.lower().replace(' ', '-').replace("\0", "")
|
|
49
|
+
return _inside(base, base / f"{slug}.flowstate")
|
|
50
|
+
|
|
51
|
+
|
|
52
|
+
def _compute_file_hash(file_path: Path) -> str:
|
|
53
|
+
"""Compute SHA256 hash of a file."""
|
|
54
|
+
sha256 = hashlib.sha256()
|
|
55
|
+
with open(file_path, "rb") as f:
|
|
56
|
+
for chunk in iter(lambda: f.read(8192), b""):
|
|
57
|
+
sha256.update(chunk)
|
|
58
|
+
return sha256.hexdigest()
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def _get_file_type(file_path: Path) -> str:
|
|
62
|
+
"""Determine file type category from extension."""
|
|
63
|
+
ext = file_path.suffix.lower()
|
|
64
|
+
if ext in ['.pdf', '.doc', '.docx', '.txt', '.md', '.rtf']:
|
|
65
|
+
return 'document'
|
|
66
|
+
elif ext in ['.png', '.jpg', '.jpeg', '.gif', '.svg', '.webp']:
|
|
67
|
+
return 'image'
|
|
68
|
+
elif ext in ['.py', '.js', '.ts', '.swift', '.rs', '.go', '.java', '.c', '.cpp', '.h', '.jsx', '.tsx', '.css', '.html']:
|
|
69
|
+
return 'code'
|
|
70
|
+
else:
|
|
71
|
+
return 'other'
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def attach_file(
|
|
75
|
+
project_id: int,
|
|
76
|
+
file_path: str,
|
|
77
|
+
component_id: Optional[int] = None,
|
|
78
|
+
problem_id: Optional[int] = None,
|
|
79
|
+
user_description: Optional[str] = None,
|
|
80
|
+
tags: Optional[list[str]] = None,
|
|
81
|
+
copy_to_bundle: bool = True
|
|
82
|
+
) -> dict:
|
|
83
|
+
"""
|
|
84
|
+
Attach a file to a project, component, or problem.
|
|
85
|
+
|
|
86
|
+
Args:
|
|
87
|
+
project_id: Which project to attach to
|
|
88
|
+
file_path: Path to the file to attach
|
|
89
|
+
component_id: Optional component to attach to
|
|
90
|
+
problem_id: Optional problem to attach to
|
|
91
|
+
user_description: Optional description from user
|
|
92
|
+
tags: Optional list of tags
|
|
93
|
+
copy_to_bundle: If True, copy file to project bundle; else reference external path
|
|
94
|
+
|
|
95
|
+
Returns:
|
|
96
|
+
The created attachment record
|
|
97
|
+
"""
|
|
98
|
+
db = get_db()
|
|
99
|
+
source_path = Path(file_path)
|
|
100
|
+
|
|
101
|
+
if not source_path.exists():
|
|
102
|
+
raise FileNotFoundError(f"File not found: {file_path}")
|
|
103
|
+
|
|
104
|
+
project = db.get_project(project_id)
|
|
105
|
+
if not project:
|
|
106
|
+
raise ValueError(f"Project not found: {project_id}")
|
|
107
|
+
|
|
108
|
+
# Compute file metadata
|
|
109
|
+
file_name = source_path.name
|
|
110
|
+
file_type = source_path.suffix.lstrip('.').lower()
|
|
111
|
+
file_size = source_path.stat().st_size
|
|
112
|
+
file_hash = _compute_file_hash(source_path)
|
|
113
|
+
|
|
114
|
+
# Determine storage path
|
|
115
|
+
if copy_to_bundle:
|
|
116
|
+
bundle_path = _get_project_bundle_path(project.name)
|
|
117
|
+
attachments_dir = _inside(bundle_path.parent, bundle_path / "attachments")
|
|
118
|
+
attachments_dir.mkdir(parents=True, exist_ok=True)
|
|
119
|
+
_inside(data_dir() / "projects", attachments_dir) # (a link made meanwhile)
|
|
120
|
+
|
|
121
|
+
# Copy file to bundle
|
|
122
|
+
dest_path = attachments_dir / file_name
|
|
123
|
+
# Handle name collision (a link counts as taken, even one pointing nowhere)
|
|
124
|
+
counter = 1
|
|
125
|
+
while os.path.lexists(dest_path):
|
|
126
|
+
stem = source_path.stem
|
|
127
|
+
dest_path = attachments_dir / f"{stem}_{counter}{source_path.suffix}"
|
|
128
|
+
counter += 1
|
|
129
|
+
|
|
130
|
+
_copy_new(source_path, dest_path)
|
|
131
|
+
stored_path = str(dest_path.relative_to(bundle_path))
|
|
132
|
+
is_external = False
|
|
133
|
+
else:
|
|
134
|
+
stored_path = str(source_path.absolute())
|
|
135
|
+
is_external = True
|
|
136
|
+
|
|
137
|
+
# Insert into database
|
|
138
|
+
conn = db._conn()
|
|
139
|
+
try:
|
|
140
|
+
cursor = conn.execute(
|
|
141
|
+
"""INSERT INTO attachments
|
|
142
|
+
(project_id, component_id, problem_id, file_name, file_path,
|
|
143
|
+
file_type, file_size, file_hash, is_external, user_description, tags)
|
|
144
|
+
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)""",
|
|
145
|
+
(project_id, component_id, problem_id, file_name, stored_path,
|
|
146
|
+
file_type, file_size, file_hash, is_external, user_description,
|
|
147
|
+
','.join(tags) if tags else None)
|
|
148
|
+
)
|
|
149
|
+
attachment_id = cursor.lastrowid
|
|
150
|
+
# Index its text for manage_files search (same transaction: attached = searchable)
|
|
151
|
+
row = conn.execute("SELECT * FROM attachments WHERE id = ?", (attachment_id,)).fetchone()
|
|
152
|
+
_index(conn, row, project.name)
|
|
153
|
+
conn.commit()
|
|
154
|
+
|
|
155
|
+
# Fetch and return the created attachment
|
|
156
|
+
row = conn.execute("SELECT * FROM attachments WHERE id = ?", (attachment_id,)).fetchone()
|
|
157
|
+
return dict(row) if row else {"id": attachment_id}
|
|
158
|
+
finally:
|
|
159
|
+
conn.close()
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def get_attachments(
|
|
163
|
+
project_id: Optional[int] = None,
|
|
164
|
+
component_id: Optional[int] = None,
|
|
165
|
+
problem_id: Optional[int] = None
|
|
166
|
+
) -> list[dict]:
|
|
167
|
+
"""
|
|
168
|
+
Get file attachments with optional filters.
|
|
169
|
+
|
|
170
|
+
Args:
|
|
171
|
+
project_id: Filter by project
|
|
172
|
+
component_id: Filter by component
|
|
173
|
+
problem_id: Filter by problem
|
|
174
|
+
|
|
175
|
+
Returns:
|
|
176
|
+
List of attachment records
|
|
177
|
+
"""
|
|
178
|
+
db = get_db()
|
|
179
|
+
conn = db._conn()
|
|
180
|
+
try:
|
|
181
|
+
conditions = []
|
|
182
|
+
params = []
|
|
183
|
+
|
|
184
|
+
if project_id is not None:
|
|
185
|
+
conditions.append("project_id = ?")
|
|
186
|
+
params.append(project_id)
|
|
187
|
+
if component_id is not None:
|
|
188
|
+
conditions.append("component_id = ?")
|
|
189
|
+
params.append(component_id)
|
|
190
|
+
if problem_id is not None:
|
|
191
|
+
conditions.append("problem_id = ?")
|
|
192
|
+
params.append(problem_id)
|
|
193
|
+
|
|
194
|
+
where_clause = " AND ".join(conditions) if conditions else "1=1"
|
|
195
|
+
rows = conn.execute(
|
|
196
|
+
f"SELECT * FROM attachments WHERE {where_clause} ORDER BY created_at DESC",
|
|
197
|
+
params
|
|
198
|
+
).fetchall()
|
|
199
|
+
return [dict(row) for row in rows]
|
|
200
|
+
finally:
|
|
201
|
+
conn.close()
|
|
202
|
+
|
|
203
|
+
|
|
204
|
+
def remove_attachment(
|
|
205
|
+
attachment_id: int,
|
|
206
|
+
delete_file: bool = False
|
|
207
|
+
) -> dict:
|
|
208
|
+
"""
|
|
209
|
+
Remove an attachment record, optionally deleting the file.
|
|
210
|
+
|
|
211
|
+
Args:
|
|
212
|
+
attachment_id: The attachment to remove
|
|
213
|
+
delete_file: If True and file is in bundle, delete the actual file
|
|
214
|
+
|
|
215
|
+
Returns:
|
|
216
|
+
Success status and info
|
|
217
|
+
"""
|
|
218
|
+
db = get_db()
|
|
219
|
+
conn = db._conn()
|
|
220
|
+
try:
|
|
221
|
+
# Get attachment first
|
|
222
|
+
row = conn.execute("SELECT * FROM attachments WHERE id = ?", (attachment_id,)).fetchone()
|
|
223
|
+
if not row:
|
|
224
|
+
return {"success": False, "error": "Attachment not found"}
|
|
225
|
+
|
|
226
|
+
attachment = dict(row)
|
|
227
|
+
|
|
228
|
+
# Delete file if requested and not external
|
|
229
|
+
if delete_file and not attachment['is_external']:
|
|
230
|
+
project = db.get_project(attachment['project_id'])
|
|
231
|
+
if project:
|
|
232
|
+
bundle_path = _get_project_bundle_path(project.name)
|
|
233
|
+
file_path = _inside(bundle_path, bundle_path / attachment['file_path'])
|
|
234
|
+
if file_path.exists():
|
|
235
|
+
os.remove(file_path)
|
|
236
|
+
|
|
237
|
+
# Delete from database
|
|
238
|
+
conn.execute("DELETE FROM attachments WHERE id = ?", (attachment_id,))
|
|
239
|
+
conn.commit()
|
|
240
|
+
|
|
241
|
+
return {"success": True, "deleted_attachment": attachment}
|
|
242
|
+
finally:
|
|
243
|
+
conn.close()
|
|
244
|
+
|
|
245
|
+
|
|
246
|
+
# ---------------------------------------------------------------- text index (2.4.0)
|
|
247
|
+
# Attached text files are split into sections of lines and stored in content_locations
|
|
248
|
+
# (snippet = the section's text), so manage_files search finds words in them. A file is
|
|
249
|
+
# indexed when attached; files attached before 2.4 are indexed on the first search that
|
|
250
|
+
# could find them. attachments.indexed_at marks a file as done (also binary or too big ones,
|
|
251
|
+
# which get no rows). Standard library only.
|
|
252
|
+
INDEX_MAX_BYTES = 2_000_000
|
|
253
|
+
SECTION_LINES = 40
|
|
254
|
+
SNIPPET_MAX = 4000
|
|
255
|
+
_TEXT_TYPES = {"txt", "md", "markdown", "rst", "csv", "tsv", "json", "jsonl", "yaml", "yml", "toml",
|
|
256
|
+
"ini", "cfg", "conf", "xml", "html", "htm", "css", "log", "sql", "sh", "bash", "zsh",
|
|
257
|
+
"ps1", "bat", "py", "js", "jsx", "ts", "tsx", "mjs", "swift", "rs", "go", "java", "kt",
|
|
258
|
+
"c", "h", "cpp", "hpp", "cs", "rb", "php", "pl", "lua", "r", "m", "scala", "env.example",
|
|
259
|
+
"tex", "org", "adoc", "properties", "gradle", "vue", "svelte", "dart", "ex", "exs"}
|
|
260
|
+
_CODE_TYPES = _TEXT_TYPES - {"txt", "md", "markdown", "rst", "csv", "tsv", "log", "tex", "org", "adoc"}
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def _file_of(row, project_name: str) -> Path:
|
|
264
|
+
if row["is_external"]:
|
|
265
|
+
return Path(row["file_path"])
|
|
266
|
+
bundle = _get_project_bundle_path(project_name)
|
|
267
|
+
return _inside(bundle, bundle / row["file_path"])
|
|
268
|
+
|
|
269
|
+
|
|
270
|
+
def _read_text(path: Path, file_type: str) -> Optional[str]:
|
|
271
|
+
"""The file's text, or None for a missing, binary or too-big file."""
|
|
272
|
+
try:
|
|
273
|
+
if not path.is_file() or path.stat().st_size > INDEX_MAX_BYTES:
|
|
274
|
+
return None
|
|
275
|
+
raw = path.read_bytes()
|
|
276
|
+
except OSError:
|
|
277
|
+
return None
|
|
278
|
+
if b"\x00" in raw[:8192] and (file_type or "").lower() not in _TEXT_TYPES:
|
|
279
|
+
return None
|
|
280
|
+
for enc in ("utf-8-sig", "utf-16") if raw[:2] in (b"\xff\xfe", b"\xfe\xff") else ("utf-8-sig",):
|
|
281
|
+
try:
|
|
282
|
+
return raw.decode(enc)
|
|
283
|
+
except UnicodeDecodeError:
|
|
284
|
+
continue
|
|
285
|
+
if (file_type or "").lower() in _TEXT_TYPES:
|
|
286
|
+
return raw.decode("utf-8", errors="replace")
|
|
287
|
+
return None
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def _sections(text: str, headings: bool = False):
|
|
291
|
+
"""(first line, last line, text) of each section: every SECTION_LINES lines, and in
|
|
292
|
+
markdown a heading starts one."""
|
|
293
|
+
lines = text.splitlines()
|
|
294
|
+
start, buf = 1, []
|
|
295
|
+
for n, line in enumerate(lines, 1):
|
|
296
|
+
heading = headings and line.startswith("#") and any(b.strip() for b in buf)
|
|
297
|
+
if buf and (len(buf) >= SECTION_LINES or heading):
|
|
298
|
+
yield start, n - 1, "\n".join(buf)
|
|
299
|
+
start, buf = n, []
|
|
300
|
+
buf.append(line)
|
|
301
|
+
if buf and any(b.strip() for b in buf):
|
|
302
|
+
yield start, len(lines), "\n".join(buf)
|
|
303
|
+
|
|
304
|
+
|
|
305
|
+
def _index(conn, row, project_name: str) -> int:
|
|
306
|
+
"""Index one attachment's text (inside the caller's transaction). Returns sections."""
|
|
307
|
+
try:
|
|
308
|
+
text = _read_text(_file_of(row, project_name), row["file_type"])
|
|
309
|
+
except ValueError: # a stored path leading outside the projects folder: never read
|
|
310
|
+
text = None
|
|
311
|
+
conn.execute("DELETE FROM content_locations WHERE attachment_id = ? AND location_type = 'line' "
|
|
312
|
+
"AND related_problem_id IS NULL AND related_learning_id IS NULL", (row["id"],))
|
|
313
|
+
n = 0
|
|
314
|
+
if text:
|
|
315
|
+
category = "code" if (row["file_type"] or "").lower() in _CODE_TYPES else "reference"
|
|
316
|
+
md = (row["file_type"] or "").lower() in ("md", "markdown")
|
|
317
|
+
for a, b, chunk in _sections(text, md):
|
|
318
|
+
if not chunk.strip():
|
|
319
|
+
continue
|
|
320
|
+
head = next((ln.strip().lstrip("#").strip() for ln in chunk.splitlines() if ln.strip()), "")
|
|
321
|
+
conn.execute(
|
|
322
|
+
"""INSERT INTO content_locations (attachment_id, description, category, location_type,
|
|
323
|
+
start_location, end_location, snippet) VALUES (?, ?, ?, 'line', ?, ?, ?)""",
|
|
324
|
+
(row["id"], f"{row['file_name']} lines {a}-{b}: {head[:80]}", category, str(a), str(b),
|
|
325
|
+
chunk[:SNIPPET_MAX]))
|
|
326
|
+
n += 1
|
|
327
|
+
conn.execute("UPDATE attachments SET indexed_at = CURRENT_TIMESTAMP, content_extracted = ? "
|
|
328
|
+
"WHERE id = ?", (bool(n), row["id"]))
|
|
329
|
+
return n
|
|
330
|
+
|
|
331
|
+
|
|
332
|
+
def index_pending(conn, project_id: Optional[int] = None, limit: int = 50) -> int:
|
|
333
|
+
"""Index attachments not indexed yet (attached before 2.4). Returns how many."""
|
|
334
|
+
sql = ("SELECT a.*, p.name AS project_name FROM attachments a JOIN projects p ON p.id = a.project_id "
|
|
335
|
+
"WHERE a.indexed_at IS NULL")
|
|
336
|
+
params: list = []
|
|
337
|
+
if project_id is not None:
|
|
338
|
+
sql += " AND a.project_id = ?"
|
|
339
|
+
params.append(project_id)
|
|
340
|
+
rows = conn.execute(sql + " ORDER BY a.id LIMIT ?", (*params, limit)).fetchall()
|
|
341
|
+
for r in rows:
|
|
342
|
+
_index(conn, r, r["project_name"])
|
|
343
|
+
if rows:
|
|
344
|
+
conn.commit()
|
|
345
|
+
return len(rows)
|
|
346
|
+
|
|
347
|
+
|
|
348
|
+
def _excerpt(snippet: str, words: list, width: int = 240) -> str:
|
|
349
|
+
"""The part of a section around the first word found, on one line."""
|
|
350
|
+
flat = " ".join((snippet or "").split())
|
|
351
|
+
low = flat.lower()
|
|
352
|
+
at = min((low.find(w) for w in words if low.find(w) >= 0), default=0)
|
|
353
|
+
start = max(0, at - width // 3)
|
|
354
|
+
out = flat[start:start + width]
|
|
355
|
+
return ("..." if start else "") + out + ("..." if start + width < len(flat) else "")
|
|
356
|
+
|
|
357
|
+
|
|
358
|
+
def search_file_content(
|
|
359
|
+
query: str,
|
|
360
|
+
project_id: Optional[int] = None,
|
|
361
|
+
file_types: Optional[list[str]] = None,
|
|
362
|
+
limit: int = 10
|
|
363
|
+
) -> list[dict]:
|
|
364
|
+
"""
|
|
365
|
+
Search the text of attached files (content_locations). Every word of the query must
|
|
366
|
+
appear in a section (case-insensitive for ASCII); each hit names the file and lines.
|
|
367
|
+
|
|
368
|
+
Args:
|
|
369
|
+
query: Search query
|
|
370
|
+
project_id: Optional project filter
|
|
371
|
+
file_types: Optional list of file types to search
|
|
372
|
+
limit: Max results
|
|
373
|
+
|
|
374
|
+
Returns:
|
|
375
|
+
List of hits: attachment id, file name and type, lines, a short excerpt
|
|
376
|
+
"""
|
|
377
|
+
db = get_db()
|
|
378
|
+
conn = db._conn()
|
|
379
|
+
try:
|
|
380
|
+
index_pending(conn, project_id)
|
|
381
|
+
words = [w.lower() for w in (query or "").split() if w.strip()][:8]
|
|
382
|
+
if not words:
|
|
383
|
+
return []
|
|
384
|
+
sql = """
|
|
385
|
+
SELECT cl.id, cl.attachment_id, cl.description, cl.category, cl.location_type,
|
|
386
|
+
cl.start_location, cl.end_location, cl.snippet,
|
|
387
|
+
a.file_name, a.file_path, a.file_type, a.project_id
|
|
388
|
+
FROM content_locations cl
|
|
389
|
+
JOIN attachments a ON cl.attachment_id = a.id
|
|
390
|
+
WHERE 1 = 1
|
|
391
|
+
"""
|
|
392
|
+
params: list = []
|
|
393
|
+
for w in words:
|
|
394
|
+
sql += " AND (cl.snippet LIKE ? ESCAPE '\\' OR cl.description LIKE ? ESCAPE '\\')"
|
|
395
|
+
pat = "%" + w.replace("\\", "\\\\").replace("%", "\\%").replace("_", "\\_") + "%"
|
|
396
|
+
params += [pat, pat]
|
|
397
|
+
if project_id is not None:
|
|
398
|
+
sql += " AND a.project_id = ?"
|
|
399
|
+
params.append(project_id)
|
|
400
|
+
|
|
401
|
+
if file_types:
|
|
402
|
+
placeholders = ",".join(["?"] * len(file_types))
|
|
403
|
+
sql += f" AND a.file_type IN ({placeholders})"
|
|
404
|
+
params.extend(file_types)
|
|
405
|
+
|
|
406
|
+
sql += " ORDER BY a.id DESC, cl.id LIMIT ?"
|
|
407
|
+
params.append(max(1, min(int(limit or 10), 50)))
|
|
408
|
+
|
|
409
|
+
out = []
|
|
410
|
+
for row in conn.execute(sql, params).fetchall():
|
|
411
|
+
hit = dict(row)
|
|
412
|
+
hit["excerpt"] = _excerpt(hit.pop("snippet"), words)
|
|
413
|
+
out.append(hit)
|
|
414
|
+
return out
|
|
415
|
+
finally:
|
|
416
|
+
conn.close()
|