-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsession_logger.py
More file actions
149 lines (129 loc) · 5.27 KB
/
session_logger.py
File metadata and controls
149 lines (129 loc) · 5.27 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
import json
from pathlib import Path
from datetime import datetime
import json
import re
def slugify(text: str) -> str:
"""Convert text to a simple slug usable in file names."""
text = text.lower()
text = re.sub(r"[^a-z0-9]+", "_", text)
return text.strip("_") or "query"
def get_timestamp() -> str:
"""Return a UTC timestamp suitable for file names."""
return datetime.utcnow().strftime("%Y-%m-%dT%H%MZ")
def format_function_entry(node: dict, relevance: dict, graph: dict) -> dict:
"""Return structured JSON for a single function node."""
callers = []
for cid in node.get("called_by", []):
edge = next((e for e in graph.get("edges", []) if e["from"] == cid and e["to"] == node["id"]), None)
count = edge.get("weight", 1) if edge else 1
caller_node = next((n for n in graph.get("nodes", []) if n["id"] == cid), {})
callers.append({
"function": caller_node.get("name", cid.split("::")[-1]),
"file": caller_node.get("file_path"),
"count": count,
})
callees = []
for cid in node.get("calls", []):
edge = next((e for e in graph.get("edges", []) if e["from"] == node["id"] and e["to"] == cid), None)
count = edge.get("weight", 1) if edge else 1
callee_node = next((n for n in graph.get("nodes", []) if n["id"] == cid), {})
callees.append({
"function": callee_node.get("name", cid.split("::")[-1]),
"file": callee_node.get("file_path"),
"count": count,
})
name = node.get("name") or node.get("type")
return {
"function_name": name,
"file": node.get("file_path"),
"class": node.get("class"),
"relevance_scores": relevance,
"call_relations": {
"callers": callers,
"callees": callees,
},
"call_graph_role": node.get("call_graph_role"),
"parameters": node.get("parameters", {}),
"comment": (node.get("docstring") or " ").strip(),
"code": node.get("code", ""),
}
def log_session_to_json(data: dict, path: str | Path) -> str:
"""Write session data to ``path`` in JSON format and return file path."""
dest = Path(path)
if dest.suffix:
dest.parent.mkdir(parents=True, exist_ok=True)
full_path = dest
else:
dest.mkdir(parents=True, exist_ok=True)
slug = slugify(data.get("query", data.get("original_query", "query")))
fname = f"{get_timestamp()}_{slug}.json"
full_path = dest / fname
with open(full_path, "w", encoding="utf-8") as f:
json.dump(data, f, indent=2, ensure_ascii=False)
f.write("\n")
return str(full_path)
def log_summary_to_markdown(data: dict, path: str | Path) -> str:
"""Write a human-readable summary of ``data`` to ``path`` and return file path."""
dest = Path(path)
if dest.suffix:
dest.parent.mkdir(parents=True, exist_ok=True)
full_path = dest
else:
dest.mkdir(parents=True, exist_ok=True)
slug = slugify(data.get("original_query", "query"))
fname = f"query_{slug}_{get_timestamp()}.md"
full_path = dest / fname
subqueries = data.get("subqueries", [])
functions = data.get("functions", {})
total_sub = len(subqueries)
unique_funcs = len(functions)
core_hits = sum(1 for f in functions.values() if len(f.get("subqueries", [])) == total_sub)
unique_hits = sum(1 for f in functions.values() if len(f.get("subqueries", [])) == 1)
dup_funcs = sum(1 for f in functions.values() if f.get("duplicate_count", 0) > 1)
lines = [
f"# Query Summary",
"",
f"Original Query: {data.get('original_query','')}",
"",
f"Total subqueries: {total_sub}",
f"Total unique functions: {unique_funcs}",
f"Number of core hits: {core_hits}",
f"Number of unique hits: {unique_hits}",
f"Functions appearing multiple times: {dup_funcs}",
"",
"## Subquery Results",
]
for i, sq in enumerate(subqueries, start=1):
lines.append("")
lines.append(f"### {i}. {sq.get('text','')}")
for fn in sq.get("functions", []):
meta = functions.get(fn["name"], {})
tags = []
if len(meta.get("subqueries", [])) == total_sub:
tags.append("CORE")
if meta.get("duplicate_count", 0) > 1:
tags.append("DUP")
tag_text = f" [{' ,'.join(tags)}]" if tags else ""
lines.append(
f"- {fn['name']} ({fn['file']}, score {fn['score']:.3f}, rank {fn['rank']}){tag_text}"
)
conversation = data.get("conversation", [])
if conversation:
lines.append("")
lines.append("## Conversation History")
for i, round in enumerate(conversation, start=1):
lines.append("")
lines.append(f"### Round {i}")
lines.append("Prompt:")
lines.append(round.get("prompt", ""))
lines.append("Response:")
lines.append(round.get("response", ""))
final = data.get("llm_response")
if final:
lines.append("")
lines.append("## Final Output")
lines.append(final)
with open(full_path, "w", encoding="utf-8") as f:
f.write("\n".join(lines) + "\n")
return str(full_path)