Skip to content

Commit 812136c

Browse files
author
phernandez
committed
fix: add logfire spans to cli
1 parent 3e8e3e8 commit 812136c

File tree

16 files changed

+272
-237
lines changed

16 files changed

+272
-237
lines changed
Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
"""Database management commands."""
22

33
import asyncio
4+
5+
import logfire
46
import typer
57
from loguru import logger
68

@@ -13,13 +15,14 @@ def reset(
1315
reindex: bool = typer.Option(False, "--reindex", help="Rebuild indices from filesystem"),
1416
): # pragma: no cover
1517
"""Reset database (drop all tables and recreate)."""
16-
if typer.confirm("This will delete all data. Are you sure?"):
17-
logger.info("Resetting database...")
18-
asyncio.run(migrations.reset_database())
18+
with logfire.span("reset"): # pyright: ignore [reportGeneralTypeIssues]
19+
if typer.confirm("This will delete all data in your db. Are you sure?"):
20+
logger.info("Resetting database...")
21+
asyncio.run(migrations.reset_database())
1922

20-
if reindex:
21-
# Import and run sync
22-
from basic_memory.cli.commands.sync import sync
23+
if reindex:
24+
# Import and run sync
25+
from basic_memory.cli.commands.sync import sync
2326

24-
logger.info("Rebuilding search index from filesystem...")
25-
sync(watch=False) # pyright: ignore
27+
logger.info("Rebuilding search index from filesystem...")
28+
sync(watch=False) # pyright: ignore

src/basic_memory/cli/commands/import_chatgpt.py

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77
from typing import Dict, Any, List, Annotated, Set, Optional
88

9+
import logfire
910
import typer
1011
from loguru import logger
1112
from rich.console import Console
@@ -225,35 +226,38 @@ def import_chatgpt(
225226
After importing, run 'basic-memory sync' to index the new files.
226227
"""
227228

228-
try:
229-
if conversations_json:
230-
if not conversations_json.exists():
231-
typer.echo(f"Error: File not found: {conversations_json}", err=True)
232-
raise typer.Exit(1)
229+
with logfire.span("import chatgpt"): # pyright: ignore [reportGeneralTypeIssues]
230+
try:
231+
if conversations_json:
232+
if not conversations_json.exists():
233+
typer.echo(f"Error: File not found: {conversations_json}", err=True)
234+
raise typer.Exit(1)
233235

234-
# Get markdown processor
235-
markdown_processor = asyncio.run(get_markdown_processor())
236+
# Get markdown processor
237+
markdown_processor = asyncio.run(get_markdown_processor())
236238

237-
# Process the file
238-
base_path = config.home / folder
239-
console.print(f"\nImporting chats from {conversations_json}...writing to {base_path}")
240-
results = asyncio.run(
241-
process_chatgpt_json(conversations_json, folder, markdown_processor)
242-
)
239+
# Process the file
240+
base_path = config.home / folder
241+
console.print(
242+
f"\nImporting chats from {conversations_json}...writing to {base_path}"
243+
)
244+
results = asyncio.run(
245+
process_chatgpt_json(conversations_json, folder, markdown_processor)
246+
)
243247

244-
# Show results
245-
console.print(
246-
Panel(
247-
f"[green]Import complete![/green]\n\n"
248-
f"Imported {results['conversations']} conversations\n"
249-
f"Containing {results['messages']} messages",
250-
expand=False,
248+
# Show results
249+
console.print(
250+
Panel(
251+
f"[green]Import complete![/green]\n\n"
252+
f"Imported {results['conversations']} conversations\n"
253+
f"Containing {results['messages']} messages",
254+
expand=False,
255+
)
251256
)
252-
)
253257

254-
console.print("\nRun 'basic-memory sync' to index the new files.")
258+
console.print("\nRun 'basic-memory sync' to index the new files.")
255259

256-
except Exception as e:
257-
logger.error("Import failed")
258-
typer.echo(f"Error during import: {e}", err=True)
259-
raise typer.Exit(1)
260+
except Exception as e:
261+
logger.error("Import failed")
262+
typer.echo(f"Error during import: {e}", err=True)
263+
raise typer.Exit(1)

src/basic_memory/cli/commands/import_claude_conversations.py

Lines changed: 29 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from pathlib import Path
77
from typing import Dict, Any, List, Annotated
88

9+
import logfire
910
import typer
1011
from loguru import logger
1112
from rich.console import Console
@@ -178,34 +179,35 @@ def import_claude(
178179
After importing, run 'basic-memory sync' to index the new files.
179180
"""
180181

181-
try:
182-
if not conversations_json.exists():
183-
typer.echo(f"Error: File not found: {conversations_json}", err=True)
184-
raise typer.Exit(1)
182+
with logfire.span("import claude conversations"): # pyright: ignore [reportGeneralTypeIssues]
183+
try:
184+
if not conversations_json.exists():
185+
typer.echo(f"Error: File not found: {conversations_json}", err=True)
186+
raise typer.Exit(1)
187+
188+
# Get markdown processor
189+
markdown_processor = asyncio.run(get_markdown_processor())
185190

186-
# Get markdown processor
187-
markdown_processor = asyncio.run(get_markdown_processor())
188-
189-
# Process the file
190-
base_path = config.home / folder
191-
console.print(f"\nImporting chats from {conversations_json}...writing to {base_path}")
192-
results = asyncio.run(
193-
process_conversations_json(conversations_json, base_path, markdown_processor)
194-
)
195-
196-
# Show results
197-
console.print(
198-
Panel(
199-
f"[green]Import complete![/green]\n\n"
200-
f"Imported {results['conversations']} conversations\n"
201-
f"Containing {results['messages']} messages",
202-
expand=False,
191+
# Process the file
192+
base_path = config.home / folder
193+
console.print(f"\nImporting chats from {conversations_json}...writing to {base_path}")
194+
results = asyncio.run(
195+
process_conversations_json(conversations_json, base_path, markdown_processor)
203196
)
204-
)
205197

206-
console.print("\nRun 'basic-memory sync' to index the new files.")
198+
# Show results
199+
console.print(
200+
Panel(
201+
f"[green]Import complete![/green]\n\n"
202+
f"Imported {results['conversations']} conversations\n"
203+
f"Containing {results['messages']} messages",
204+
expand=False,
205+
)
206+
)
207207

208-
except Exception as e:
209-
logger.error("Import failed")
210-
typer.echo(f"Error during import: {e}", err=True)
211-
raise typer.Exit(1)
208+
console.print("\nRun 'basic-memory sync' to index the new files.")
209+
210+
except Exception as e:
211+
logger.error("Import failed")
212+
typer.echo(f"Error during import: {e}", err=True)
213+
raise typer.Exit(1)

src/basic_memory/cli/commands/import_claude_projects.py

Lines changed: 30 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pathlib import Path
66
from typing import Dict, Any, Annotated, Optional
77

8+
import logfire
89
import typer
910
from loguru import logger
1011
from rich.console import Console
@@ -160,36 +161,36 @@ def import_projects(
160161
161162
After importing, run 'basic-memory sync' to index the new files.
162163
"""
164+
with logfire.span("import claude projects"): # pyright: ignore [reportGeneralTypeIssues]
165+
try:
166+
if projects_json:
167+
if not projects_json.exists():
168+
typer.echo(f"Error: File not found: {projects_json}", err=True)
169+
raise typer.Exit(1)
170+
171+
# Get markdown processor
172+
markdown_processor = asyncio.run(get_markdown_processor())
173+
174+
# Process the file
175+
base_path = config.home / base_folder if base_folder else config.home
176+
console.print(f"\nImporting projects from {projects_json}...writing to {base_path}")
177+
results = asyncio.run(
178+
process_projects_json(projects_json, base_path, markdown_processor)
179+
)
163180

164-
try:
165-
if projects_json:
166-
if not projects_json.exists():
167-
typer.echo(f"Error: File not found: {projects_json}", err=True)
168-
raise typer.Exit(1)
169-
170-
# Get markdown processor
171-
markdown_processor = asyncio.run(get_markdown_processor())
172-
173-
# Process the file
174-
base_path = config.home / base_folder if base_folder else config.home
175-
console.print(f"\nImporting projects from {projects_json}...writing to {base_path}")
176-
results = asyncio.run(
177-
process_projects_json(projects_json, base_path, markdown_processor)
178-
)
179-
180-
# Show results
181-
console.print(
182-
Panel(
183-
f"[green]Import complete![/green]\n\n"
184-
f"Imported {results['documents']} project documents\n"
185-
f"Imported {results['prompts']} prompt templates",
186-
expand=False,
181+
# Show results
182+
console.print(
183+
Panel(
184+
f"[green]Import complete![/green]\n\n"
185+
f"Imported {results['documents']} project documents\n"
186+
f"Imported {results['prompts']} prompt templates",
187+
expand=False,
188+
)
187189
)
188-
)
189190

190-
console.print("\nRun 'basic-memory sync' to index the new files.")
191+
console.print("\nRun 'basic-memory sync' to index the new files.")
191192

192-
except Exception as e:
193-
logger.error("Import failed")
194-
typer.echo(f"Error during import: {e}", err=True)
195-
raise typer.Exit(1)
193+
except Exception as e:
194+
logger.error("Import failed")
195+
typer.echo(f"Error during import: {e}", err=True)
196+
raise typer.Exit(1)

src/basic_memory/cli/commands/import_memory_json.py

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from pathlib import Path
66
from typing import Dict, Any, List, Annotated
77

8+
import logfire
89
import typer
910
from loguru import logger
1011
from rich.console import Console
@@ -113,32 +114,33 @@ def memory_json(
113114
After importing, run 'basic-memory sync' to index the new files.
114115
"""
115116

116-
if not json_path.exists():
117-
typer.echo(f"Error: File not found: {json_path}", err=True)
118-
raise typer.Exit(1)
119-
120-
try:
121-
# Get markdown processor
122-
markdown_processor = asyncio.run(get_markdown_processor())
123-
124-
# Process the file
125-
base_path = config.home
126-
console.print(f"\nImporting from {json_path}...writing to {base_path}")
127-
results = asyncio.run(process_memory_json(json_path, base_path, markdown_processor))
128-
129-
# Show results
130-
console.print(
131-
Panel(
132-
f"[green]Import complete![/green]\n\n"
133-
f"Created {results['entities']} entities\n"
134-
f"Added {results['relations']} relations",
135-
expand=False,
117+
with logfire.span("import memory_json"): # pyright: ignore [reportGeneralTypeIssues]
118+
if not json_path.exists():
119+
typer.echo(f"Error: File not found: {json_path}", err=True)
120+
raise typer.Exit(1)
121+
122+
try:
123+
# Get markdown processor
124+
markdown_processor = asyncio.run(get_markdown_processor())
125+
126+
# Process the file
127+
base_path = config.home
128+
console.print(f"\nImporting from {json_path}...writing to {base_path}")
129+
results = asyncio.run(process_memory_json(json_path, base_path, markdown_processor))
130+
131+
# Show results
132+
console.print(
133+
Panel(
134+
f"[green]Import complete![/green]\n\n"
135+
f"Created {results['entities']} entities\n"
136+
f"Added {results['relations']} relations",
137+
expand=False,
138+
)
136139
)
137-
)
138140

139-
console.print("\nRun 'basic-memory sync' to index the new files.")
141+
console.print("\nRun 'basic-memory sync' to index the new files.")
140142

141-
except Exception as e:
142-
logger.error("Import failed")
143-
typer.echo(f"Error during import: {e}", err=True)
144-
raise typer.Exit(1)
143+
except Exception as e:
144+
logger.error("Import failed")
145+
typer.echo(f"Error during import: {e}", err=True)
146+
raise typer.Exit(1)

src/basic_memory/cli/commands/mcp.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@ def mcp(): # pragma: no cover
1717
home_dir = config.home
1818
logger.info(f"Starting Basic Memory MCP server {basic_memory.__version__}")
1919
logger.info(f"Home directory: {home_dir}")
20-
mcp_server.run()
20+
mcp_server.run()

src/basic_memory/cli/commands/status.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import asyncio
44
from typing import Set, Dict
55

6+
import logfire
67
import typer
78
from loguru import logger
89
from rich.console import Console
@@ -146,9 +147,10 @@ def status(
146147
verbose: bool = typer.Option(False, "--verbose", "-v", help="Show detailed file information"),
147148
):
148149
"""Show sync status between files and database."""
149-
try:
150-
sync_service = asyncio.run(get_file_change_scanner())
151-
asyncio.run(run_status(sync_service, verbose)) # pragma: no cover
152-
except Exception as e:
153-
logger.exception(f"Error checking status: {e}")
154-
raise typer.Exit(code=1) # pragma: no cover
150+
with logfire.span("status"): # pyright: ignore [reportGeneralTypeIssues]
151+
try:
152+
sync_service = asyncio.run(get_file_change_scanner())
153+
asyncio.run(run_status(sync_service, verbose)) # pragma: no cover
154+
except Exception as e:
155+
logger.exception(f"Error checking status: {e}")
156+
raise typer.Exit(code=1) # pragma: no cover

src/basic_memory/db.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
from pathlib import Path
55
from typing import AsyncGenerator, Optional
66

7-
import logfire
87

98
from basic_memory.config import ProjectConfig
109
from alembic import command

0 commit comments

Comments
 (0)