-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformatting.py
More file actions
403 lines (330 loc) · 13.1 KB
/
Copy pathformatting.py
File metadata and controls
403 lines (330 loc) · 13.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
"""Response parsing and output formatting (pure, stdlib-only)."""
import json
# One-line actionable messages for the machine API's structured error codes
# (services/security/hmac-guard.js, services/util/http.js sendError, and the
# machine API rate limiter). Unknown codes stay unmapped so callers keep the
# raw JSON envelope exactly as before.
_ERROR_HINTS = {
"timestamp_required": (
"Server requires timestamped signatures (SourceVault v1.28+); "
"update this plugin to v1.4 or newer."
),
"stale_timestamp": (
"Signature timestamp outside the accepted window; "
"check clock sync between the Hermes and SourceVault hosts."
),
"bad_timestamp": "Signature timestamp header is not a number; update this plugin.",
"missing_nonce": "Signature nonce header missing; update this plugin.",
"replayed_nonce": "Signature nonce already used; retry the request.",
"bad_signature": (
"Signature rejected: CODE_SEARCH_HMAC_SECRET (or the agent token) "
"does not match the server."
),
"signature_secret_required": (
"Server requires a signing secret; set CODE_SEARCH_HMAC_SECRET "
"on the SourceVault server and in the Hermes environment."
),
"unknown_agent": (
"Agent not recognized by the server (revoked, mistyped, or never minted); "
"check SOURCEVAULT_AGENT_NAME/SOURCEVAULT_AGENT_TOKEN or re-mint with: "
"sourcevault agent mint <name> --surfaces http"
),
"agent_scope": (
"This agent token is not scoped to the http surface; "
"re-mint it with --surfaces http."
),
"agent_repo_scope": (
"This agent token is not scoped to that repository; check its --repos grant."
),
"rate_limited": "Rate limited by the server; retry shortly.",
"missing_repo_name": "repo_name is required; run /code-repos to list exact names.",
"invalid_repo_name": (
"Invalid repo_name; run /code-repos to list exact (case-sensitive) names."
),
"repo_not_found": (
"Repository not indexed under REPO_ROOT; run /code-repos to list repos."
),
"question_too_long": "Question exceeds the server's maximum length; shorten it.",
"history_search_unsupported": (
"This SourceVault does not serve /api/history-search; update SourceVault."
),
}
def _error_hint(payload):
code = str(payload.get("error") or "")
if code == "rate_limited" and payload.get("retry_after"):
return f"Rate limited by the server; retry after {payload['retry_after']}s."
return _ERROR_HINTS.get(code, "")
def _error_command_output(parsed, raw):
"""One-line human message for a failed machine-API result, or raw passthrough."""
hint = str(parsed.get("hint") or "") or _error_hint(parsed)
if not hint:
return raw
code = parsed.get("error") or "error"
return f"SourceVault error ({code}): {hint}"
def _extract_json_object(text):
if not text:
return {}
candidates = [text]
start = text.find("{")
end = text.rfind("}")
if start >= 0 and end > start:
candidates.append(text[start : end + 1])
for candidate in candidates:
try:
parsed = json.loads(candidate)
except json.JSONDecodeError:
continue
if isinstance(parsed, dict):
return parsed
return {}
def _read_file_content_or_result(result):
try:
parsed = json.loads(result)
except (TypeError, json.JSONDecodeError):
return result
if not isinstance(parsed, dict):
return result
if parsed.get("ok") is True and isinstance(parsed.get("content"), str):
relative_path = parsed.get("relative_path")
file_path = parsed.get("fullPath") or parsed.get("path")
return json.dumps(
{
"ok": True,
"repo_name": parsed.get("repo_name"),
"relative_path": relative_path,
"file_path": file_path,
"content": parsed["content"],
"response_instruction": (
"When answering the user, copy the content field exactly. "
"Do not add markdown, punctuation, semicolons, explanations, or formatting."
),
},
separators=(",", ":"),
)
return result
def _read_file_command_output(result):
try:
parsed = json.loads(result)
except (TypeError, json.JSONDecodeError):
return result
if not isinstance(parsed, dict):
return result
if parsed.get("ok") is True and isinstance(parsed.get("content"), str):
return parsed["content"]
if parsed.get("ok") is False or parsed.get("success") is False:
return _error_command_output(parsed, result)
return result
def _format_history_command_output(result):
try:
parsed = json.loads(result)
except (TypeError, json.JSONDecodeError):
return result
if not isinstance(parsed, dict):
return result
if parsed.get("ok") is False or parsed.get("success") is False:
return _error_command_output(parsed, result)
results = parsed.get("results") or []
lines = [
parsed.get("summary")
or f"Found {len(results)} matching commit(s)",
]
for index, item in enumerate(results, start=1):
short = item.get("short") or str(item.get("commit") or "")[:7] or "<unknown>"
meta = f"#{index} {short} ({item.get('date') or '?'}) {item.get('author') or ''}".rstrip()
if item.get("ai_authored"):
meta += " [ai]"
lines.append(meta)
subject = " ".join(str(item.get("subject") or "").split())
if subject:
lines.append(f" {subject}")
preview = " ".join(str(item.get("preview") or "").split())
if preview and preview != subject:
if len(preview) > 180:
preview = f"{preview[:177]}..."
lines.append(f" {preview}")
return "\n".join(lines)
def _format_search_command_output(result):
try:
parsed = json.loads(result)
except (TypeError, json.JSONDecodeError):
return result
if not isinstance(parsed, dict):
return result
if parsed.get("ok") is False or parsed.get("success") is False:
return _error_command_output(parsed, result)
results = parsed.get("results") or []
lines = [
parsed.get("summary")
or f"Found {len(results)} result(s) for {parsed.get('query') or 'query'}",
]
for index, item in enumerate(results, start=1):
file_name = item.get("file") or item.get("fullPath") or "<unknown>"
chunk = item.get("chunk")
distance = item.get("distance")
preview = " ".join(str(item.get("preview") or "").split())
if len(preview) > 180:
preview = f"{preview[:177]}..."
meta = f"#{index} {file_name}"
if chunk is not None:
meta += f" chunk={chunk}"
if isinstance(distance, (int, float)):
meta += f" distance={distance:.4f}"
lines.append(meta)
if preview:
lines.append(f" {preview}")
return "\n".join(lines)
def _format_context_command_output(result):
parsed = _parse_successful_search_result(result)
if not isinstance(parsed, dict):
return parsed
return _context_lines(parsed).rstrip()
def _format_ask_command_output(result, question):
parsed = _parse_successful_search_result(result)
if not isinstance(parsed, dict):
return parsed
lines = [
"Use the SourceVault repository context below to answer the user question.",
"Do not call tools.",
"Do not ask for a repo path.",
"Use the retrieved chunks as evidence.",
"Separate observed facts from general best-practice suggestions.",
"Do not claim a file, dependency, script, test, or behavior exists unless it appears in the snippets below.",
"Reference file paths, chunk numbers, and function names when useful.",
"Use this answer shape:",
"1. Observed From Retrieved Context",
"2. Answer",
"3. Suggested Improvements",
"4. Missing Context, if any",
"",
f"User question: {question}",
"",
_context_lines(parsed),
"",
"Answer the user question now.",
]
return "\n".join(lines).rstrip()
def _merge_search_results(primary_result, followup_result):
primary = _parse_successful_search_result(primary_result)
followup = _parse_successful_search_result(followup_result)
if not isinstance(primary, dict):
return primary_result
if not isinstance(followup, dict):
return primary_result
merged = dict(primary)
seen = set()
results = []
for round_name, parsed in (("initial", primary), ("followup", followup)):
for item in parsed.get("results") or []:
key = (
item.get("file"),
item.get("chunk"),
item.get("preview") or item.get("content"),
)
if key in seen:
continue
seen.add(key)
enriched = dict(item)
enriched["retrievalRound"] = round_name
results.append(enriched)
merged["results"] = results
merged["count"] = len(results)
merged["summary"] = f"Found {len(results)} matching chunks across initial and follow-up retrieval"
merged["retrieval"] = {
"mode": "multi-hop",
"initial_query": primary.get("query") or "",
"followup_query": followup.get("query") or "",
}
return json.dumps(merged, separators=(",", ":"))
def _parse_successful_search_result(result):
try:
parsed = json.loads(result)
except (TypeError, json.JSONDecodeError):
return result
if not isinstance(parsed, dict):
return result
if parsed.get("ok") is False or parsed.get("success") is False:
return _error_command_output(parsed, result)
return parsed
def _context_index_lines(parsed):
results = parsed.get("results") or []
if not results:
return "No matching chunks found."
lines = []
for index, item in enumerate(results, start=1):
file_name = item.get("file") or item.get("fullPath") or "<unknown>"
chunk = item.get("chunk")
distance = item.get("distance")
symbols = _format_symbols(item)
meta = f"- Result {index}: file={file_name}"
if chunk is not None:
meta += f" chunk={chunk}"
if isinstance(distance, (int, float)):
meta += f" distance={distance:.4f}"
if item.get("retrievalRound"):
meta += f" round={item.get('retrievalRound')}"
if symbols:
meta += f" symbols={symbols}"
lines.append(meta)
return "\n".join(lines)
def _context_lines(parsed):
results = parsed.get("results") or []
repo_name = parsed.get("repo_name") or (results[0].get("repoName") if results else "")
query = parsed.get("query") or ""
lines = [
"SourceVault repository context",
f"repo_name: {repo_name}",
f"query: {query}",
f"results: {len(results)}",
"",
"Use the snippets below as the repo context for the user's next question.",
"Reference file paths and function names when making suggestions.",
"",
]
if not results:
lines.append("No matching chunks found.")
return "\n".join(lines)
retrieval = parsed.get("retrieval") or {}
if retrieval.get("mode"):
lines.append(f"retrieval_mode: {retrieval.get('mode')}")
if retrieval.get("followup_query"):
lines.append(f"followup_query: {retrieval.get('followup_query')}")
lines.append("")
lines.append("Retrieved chunk index:")
lines.append(_context_index_lines(parsed))
lines.append("")
for index, item in enumerate(results, start=1):
file_name = item.get("file") or item.get("fullPath") or "<unknown>"
chunk = item.get("chunk")
distance = item.get("distance")
heading = f"## Result {index}: {file_name}"
if chunk is not None:
heading += f" chunk={chunk}"
if isinstance(distance, (int, float)):
heading += f" distance={distance:.4f}"
if item.get("retrievalRound"):
heading += f" round={item.get('retrievalRound')}"
symbols = _format_symbols(item)
content = str(item.get("content") or item.get("preview") or "").strip()
if len(content) > 4000:
content = f"{content[:4000].rstrip()}\n... [truncated]"
lines.extend(
[
heading,
*(["symbols: " + symbols] if symbols else []),
"```",
content,
"```",
"",
]
)
return "\n".join(lines).rstrip()
def _format_symbols(item):
names = [part for part in str(item.get("symbolNames") or "").split(",") if part]
kinds = [part for part in str(item.get("symbolKinds") or "").split(",") if part]
if not names:
return ""
pairs = []
for index, name in enumerate(names[:8]):
kind = kinds[index] if index < len(kinds) else "symbol"
pairs.append(f"{kind}:{name}")
return ",".join(pairs)