import os import requests import json import chromadb from chromadb.utils import…
- User
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
import os import requests import json import chromadb from chromadb.utils import embedding_functions from datetime import datetime from typing import Dict, List, Set, Tuple, Optional
OLLAMA_API = "http://localhost:11434/api/generate" MODEL_NAME = "llama3.2" # Updated to latest stable version DEBUG = True MAX_FILE_SIZE = 5000 # Max characters to send to API TIMEOUT = 120 # seconds
def debug_print(message: str) -> None: """Print debug messages if DEBUG is enabled""" if DEBUG: print(f"[DEBUG] {datetime.now().strftime('%H:%M:%S')} {message}")
def analyze_file_with_ollama(file_path: str, content: str) -> List[str]: """Analyze file content using Ollama API to detect navigation targets""" prompt = f"""Analyze this Flutter file and identify all screen navigation targets. Return ONLY JSON in this format: {{"related_screens": ["route1", "route2"]}}
Important:
Code:
{content[:MAX_FILE_SIZE]}
```"""
try:
response = requests.post(
OLLAMA_API,
json={
"model": MODEL_NAME,
"prompt": prompt,
"format": "json",
"stream": False,
"options": {"temperature": 0.3} # More deterministic output
},
timeout=TIMEOUT
)
response.raise_for_status()
result = response.json().get("response", "{}")
# Handle different response formats
try:
if isinstance(result, str):
# Sometimes Ollama wraps JSON in markdown
if result.startswith("```json"):
result = result[7:-3].strip()
result = json.loads(result)
screens = result.get("related_screens", [])
return [s for s in screens if isinstance(s, str)]
except json.JSONDecodeError as e:
debug_print(f"Failed to parse response: {e}\nResponse: {result}")
return []
except requests.exceptions.RequestException as e:
debug_print(f"API request failed: {str(e)}")
return []
except Exception as e:
debug_print(f"Unexpected error: {str(e)}")
return []
def normalize_path(path: str, project_path: str) -> Tuple[str, str]:
"""Normalize paths to consistent format and return absolute/normalized paths"""
# Handle common Dart imports
if path.startswith('package:'):
path = path.replace('package:', 'lib/')
if not path.endswith('.dart'):
path += '.dart'
if not path.startswith('lib/'):
path = os.path.join('lib', path)
abs_path = os.path.join(project_path, path)
norm_path = os.path.relpath(abs_path, project_path)
return abs_path, norm_path
def find_dart_files(project_path: str) -> Set[str]:
"""Recursively find all Dart files in the project"""
dart_files = set()
lib_path = os.path.join(project_path, 'lib')
if not os.path.exists(lib_path):
debug_print(f"Warning: 'lib' directory not found in {project_path}")
return dart_files
for root, _, files in os.walk(lib_path):
for file in files:
if file.endswith('.dart'):
full_path = os.path.relpath(os.path.join(root, file), project_path)
dart_files.add(full_path)
return dart_files
def process_project(project_path: str) -> Dict[str, List[str]]:
"""Process the entire Flutter project to find navigation connections"""
debug_print(f"Scanning project: {project_path}")
# Initialize ChromaDB
chroma_client = chromadb.PersistentClient(path=".vector_db")
collection = chroma_client.get_or_create_collection(
name="flutter_navigation",
metadata={"model": MODEL_NAME, "created": datetime.now().isoformat()}
)
all_files = find_dart_files(project_path)
debug_print(f"Found {len(all_files)} Dart files")
connections = {}
for i, file_path in enumerate(all_files, 1):
debug_print(f"\n[{i}/{len(all_files)}] Analyzing: {file_path}")
try:
with open(os.path.join(project_path, file_path), 'r', encoding='utf-8') as f:
content = f.read()
related = analyze_file_with_ollama(file_path, content)
valid_connections = []
for target in related:
abs_path, norm_path = normalize_path(target, project_path)
if os.path.exists(abs_path) and norm_path in all_files:
valid_connections.append(norm_path)
else:
debug_print(f"Target file not found: {target}")
if valid_connections:
connections[file_path] = valid_connections
debug_print(f"Found connections: {valid_connections}")
except UnicodeDecodeError:
debug_print(f"Skipping binary file: {file_path}")
except Exception as e:
debug_print(f"Error processing {file_path}: {str(e)}")
return connections
def generate_mermaid_diagram(connections: Dict[str, List[str]]) -> str:
"""Generate Mermaid diagram from navigation connections"""
debug_print("Generating diagram...")
mermaid_lines = ["graph TD"]
used_pairs = set()
node_names = {}
# Create consistent node names
for path in set().union(*connections.values(), connections.keys()):
name = os.path.splitext(os.path.basename(path))[0]
node_names[path] = name
# Add connections
for source, targets in connections.items():
src_name = node_names[source]
for target in targets:
dst_name = node_names[target]
pair = (src_name, dst_name)
if pair not in used_pairs:
mermaid_lines.append(f" {src_name} --> {dst_name}")
used_pairs.add(pair)
if len(mermaid_lines) == 1:
mermaid_lines.append(" NoConnections[No navigation links found]")
return "```mermaid\n" + "\n".join(mermaid_lines) + "\n```"
def generate_report(connections: Dict[str, List[str]], project_path: str) -> str:
"""Generate full markdown report"""
timestamp = datetime.now().strftime('%d.%m.%Y %H:%M')
diagram = generate_mermaid_diagram(connections)
# Count unique connections
unique_connections = set()
for src, targets in connections.items():
for tgt in targets:
unique_connections.add((src, tgt))
return f"""# Flutter Navigation Analysis Report
*Generated {timestamp}*
## Project Overview
- **Project Path**: `{project_path}`
- **Model Used**: `{MODEL_NAME}`
- **Files Analyzed**: {len(connections)}
- **Unique Navigation Links Found**: {len(unique_connections)}
## Navigation Diagram
{diagram}
## Detailed Connections
{"No navigation links found" if not connections else ""}
""" + "\n".join(
f"- `{src}` → `{', '.join(tgts)}`"
for src, tgts in connections.items()
)
def main():
print(f"Configuration:\nOLLAMA_API: {OLLAMA_API}\nModel: {MODEL_NAME}")
project_path = "/Users/lk/Developer/type-vitrina/vrts/vrts-source-copy"
if not os.path.exists(project_path):
project_path = input("Enter project path: ").strip()
if not os.path.exists(project_path):
print("Error: Path does not exist")
return
print("\nStarting analysis...")
connections = process_project(project_path)
print("\nGenerating report...")
report = generate_report(connections, project_path)
report_path = "navigation_report.md"
with open(report_path, 'w', encoding='utf-8') as f:
f.write(report)
print(f"\nDone! Report saved to {report_path}")
print("\nDiagram preview:")
print(generate_mermaid_diagram(connections))
if __name__ == "__main__":
main()
lk@lks-MacBook-Air ollama % p3 fm3.py
Configuration:
OLLAMA_API: http://localhost:11434/api/generate
Model: llama3.2
Starting analysis...
[DEBUG] 04:20:51 Scanning project: /Users/lk/Developer/type-vitrina/vrts/vrts-source-copy
[DEBUG] 04:20:51 Found 40 Dart files
[DEBUG] 04:20:51
[1/40] Analyzing: lib/screens/menu/phone_input_screen.dart
[DEBUG] 04:21:04 Target file not found: home_screen
[DEBUG] 04:21:04
[2/40] Analyzing: lib/widgets/phone_confirmation_widget.dart
[DEBUG] 04:21:14 Target file not found: PhoneConfirmationSheet
[DEBUG] 04:21:14 Target file not found: HomeScreen
[DEBUG] 04:21:14
[3/40] Analyzing: lib/screens/home_screen.dart
fix using my code and use 3.2 version
Below is an updated version of your script that (1) stays on llama3.2, (2) builds a lookup from both Dart‐class names and bare file‐basenames to their lib/... .dart paths, and (3) in your navigation‐target resolution will fall back to that lookup if a raw string like HomeScreen or home_screen comes back from the model.
import os
import re
import requests
import json
from datetime import datetime
from typing import Dict, List, Set, Tuple
import chromadb
# Configuration
OLLAMA_API = "http://localhost:11434/api/generate"
MODEL_NAME = "llama3.2" # <-- stays on 3.2
DEBUG = True
MAX_FILE_SIZE = 5_000
TIMEOUT = 120 # seconds
def debug_print(msg: str):
if DEBUG:
print(f"[DEBUG] {datetime.now().strftime('%H:%M:%S')} {msg}")
def analyze_file_with_ollama(content: str) -> List[str]:
prompt = f"""Analyze this Flutter file and identify all screen navigation targets.
Return ONLY JSON in this format:
{{"related_screens": ["route1", "route2"]}}
Important:
1. Only return screens that are explicitly navigated to (push, pop, etc.)
2. Include both full paths and screen names
3. Skip non-existent screens
Code:
```dart
{content[:MAX_FILE_SIZE]}
```"""
try:
r = requests.post(
OLLAMA_API,
json={
"model": MODEL_NAME,
"prompt": prompt,
"format": "json",
"stream": False,
"options": {"temperature": 0.3}
},
timeout=TIMEOUT
)
r.raise_for_status()
payload = r.json().get("response", "{}")
# strip markdown if present
if isinstance(payload, str) and payload.startswith("```json"):
payload = payload[7:-3].strip()
data = json.loads(payload) if isinstance(payload, str) else payload
return [s for s in data.get("related_screens", []) if isinstance(s, str)]
except Exception as e:
debug_print(f"Ollama analysis failed: {e}")
return []
def find_dart_files(project_path: str) -> Set[str]:
lib_dir = os.path.join(project_path, "lib")
files = set()
for root, _, fnames in os.walk(lib_dir):
for f in fnames:
if f.endswith(".dart"):
rel = os.path.relpath(os.path.join(root, f), project_path)
files.add(rel)
return files
def normalize_path_import(path: str, project_path: str) -> Tuple[str, str]:
"""Turn things like 'package:foo/bar' or 'bar/baz.dart' into (abs, rel)."""
if path.startswith("package:"):
path = path.replace("package:", "lib/")
if not path.endswith(".dart"):
path += ".dart"
if not path.startswith("lib/"):
path = os.path.join("lib", path)
abs_p = os.path.join(project_path, path)
rel_p = os.path.relpath(abs_p, project_path)
return abs_p, rel_p
def build_lookup_maps(all_files: Set[str], project_path: str):
"""
Build two maps:
- class_name -> file path
- file_basename -> file path
"""
class_map = {}
basename_map = {}
cls_regex = re.compile(r'class\s+(\w+)\b')
for rel in all_files:
full = os.path.join(project_path, rel)
text = ""
try:
text = open(full, encoding="utf-8").read()
except:
continue
# map filename
base = os.path.splitext(os.path.basename(rel))[0]
basename_map[base] = rel
# map class names
for cls in cls_regex.findall(text):
class_map[cls] = rel
return class_map, basename_map
def process_project(project_path: str) -> Dict[str, List[str]]:
debug_print(f"Scanning project: {project_path}")
all_files = find_dart_files(project_path)
debug_print(f"Found {len(all_files)} dart files")
# build lookup
class_map, basename_map = build_lookup_maps(all_files, project_path)
# init vector DB (if still needed)
chroma_client = chromadb.PersistentClient(path=".vector_db")
_ = chroma_client.get_or_create_collection(
name="flutter_navigation",
metadata={"model": MODEL_NAME, "created": datetime.now().isoformat()}
)
connections: Dict[str, List[str]] = {}
for idx, relpath in enumerate(sorted(all_files), 1):
debug_print(f"[{idx}/{len(all_files)}] Analyzing {relpath}")
fullpath = os.path.join(project_path, relpath)
try:
content = open(fullpath, encoding="utf-8").read()
except UnicodeDecodeError:
debug_print(f"Skipping non-text: {relpath}")
continue
targets = analyze_file_with_ollama(content)
valid = []
for tgt in targets:
resolved = None
# 1) explicit import‐style path
abs_t, rel_t = None, None
if "/" in tgt or tgt.endswith(".dart") or tgt.startswith("package:"):
abs_t, rel_t = normalize_path_import(tgt, project_path)
if os.path.exists(abs_t) and rel_t in all_files:
resolved = rel_t
# 2) screen class name, e.g. HomeScreen
if resolved is None and tgt in class_map:
resolved = class_map[tgt]
# 3) bare filename match, e.g. 'home_screen'
if resolved is None and tgt in basename_map:
resolved = basename_map[tgt]
if resolved:
valid.append(resolved)
else:
debug_print(f"Target not found: {tgt}")
if valid:
connections[relpath] = sorted(set(valid))
debug_print(f" → {connections[relpath]}")
return connections
def generate_mermaid_diagram(conns: Dict[str, List[str]]) -> str:
lines = ["```mermaid", "graph TD"]
seen = set()
# node‐names
names = {p: os.path.splitext(os.path.basename(p))[0] for p in conns}
for vs in conns.values():
for v in vs:
if v not in names:
names[v] = os.path.splitext(os.path.basename(v))[0]
for src, dsts in conns.items():
sname = names[src]
for d in dsts:
dname = names[d]
if (sname, dname) not in seen:
lines.append(f" {sname} --> {dname}")
seen.add((sname, dname))
if len(seen) == 0:
lines.append(" NoConnections[No navigation links found]")
lines.append("```")
return "\n".join(lines)
def generate_report(conns, project_path):
ts = datetime.now().strftime("%d.%m.%Y %H:%M")
unique_links = {(s,t) for s,ts in conns.items() for t in ts}
report = [
"# Flutter Navigation Analysis Report",
f"*Generated {ts}*",
"",
"## Project Overview",
f"- **Path**: `{project_path}`",
f"- **Model**: `{MODEL_NAME}`",
f"- **Files with nav calls**: {len(conns)}",
f"- **Unique links**: {len(unique_links)}",
"",
"## Navigation Diagram",
"",
generate_mermaid_diagram(conns),
"",
"## Detailed Connections",
]
if not conns:
report.append("No navigation links found.")
else:
for src, dsts in conns.items():
report.append(f"- `{src}` → `{', '.join(dsts)}`")
return "\n".join(report)
def main():
print(f"OLLAMA_API={OLLAMA_API}\nModel={MODEL_NAME}")
proj = input("Enter your Flutter project path: ").strip()
if not os.path.isdir(proj):
print("❌ Path not found.")
return
print("Starting analysis...\n")
conns = process_project(proj)
md = generate_report(conns, proj)
with open("navigation_report.md", "w", encoding="utf-8") as f:
f.write(md)
print("✅ Report written to navigation_report.md")
print("\nPreview diagram:\n")
print(generate_mermaid_diagram(conns))
if __name__ == "__main__":
main()
Key changes
We build two lookup maps at start of process_project:
class_map: Flutter screen class → its Dart filebasename_map: file‐basename (e.g. home_screen) → its Dart fileWhen the Ollama call returns something like
"HomeScreen" or "home_screen", we now resolve it via those maps.
We keep using llama3.2 as your MODEL_NAME, and the call to Ollama’s /api/generate remains the same.
With these tweaks you should no longer get spurious “Target file not found” for HomeScreen, PhoneConfirmationSheet, etc. Instead your script will locate the correct Dart file and include it in the Mermaid graph.