-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagent_tools.py
More file actions
1437 lines (1139 loc) · 58.8 KB
/
Copy pathagent_tools.py
File metadata and controls
1437 lines (1139 loc) · 58.8 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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
"""
Agent Tools Module for Glyph.
Handles structured tool calls for Obsidian vault operations.
"""
import json
import re
from pathlib import Path
from typing import Dict, Any, List, Optional, Union
from dataclasses import dataclass
from datetime import datetime
from rich.panel import Panel
from rich.table import Table
from rich.text import Text
from rich import box
from agent_config import get_agent_config
from backup_manager import get_backup_manager
from ui_helpers import (
GLYPH_PRIMARY, GLYPH_SUCCESS, GLYPH_ERROR, GLYPH_WARNING,
GLYPH_HIGHLIGHT, GLYPH_MUTED, show_success_message, show_error_message, console
)
@dataclass
class ToolCallResult:
"""Result of a tool call execution."""
success: bool
message: str
data: Optional[Dict[str, Any]] = None
backup_created: Optional[str] = None
class AgentToolError(Exception):
"""Exception raised by agent tool operations."""
pass
class AgentTools:
"""Handles all agent tool calls for Obsidian vault operations."""
def __init__(self, session_id: Optional[str] = None):
self.config = get_agent_config()
self.backup_manager = get_backup_manager()
self.session_id = session_id or datetime.now().strftime('%Y%m%d_%H%M%S')
self.tool_call_count = 0
# Import memory and context after initialization to avoid circular imports
from agent_memory import get_agent_memory
from agent_context import get_conversation_context
self.memory = get_agent_memory()
self.context = get_conversation_context()
# Validate vault configuration
if not self.config.is_vault_configured():
raise AgentToolError("Agent vault not configured. Run 'glyph --setup-agent' first.")
self.vault_path = Path(self.config.get_vault_path())
def _validate_vault_access(self):
"""Validate vault access and permissions."""
if not self.vault_path.exists():
raise AgentToolError(f"Vault path does not exist: {self.vault_path}")
if not self.vault_path.is_dir():
raise AgentToolError(f"Vault path is not a directory: {self.vault_path}")
def _validate_note_name(self, name: str) -> str:
"""Validate and normalize note name."""
if not name:
raise AgentToolError("Note name cannot be empty")
# Remove or replace invalid characters
safe_name = re.sub(r'[<>:"/\\|?*]', '_', name)
# Add .md extension if not present
if not safe_name.endswith('.md'):
safe_name += '.md'
return safe_name
def _get_note_path(self, name: str, folder: Optional[str] = None) -> Path:
"""Get full path to a note within the vault."""
safe_name = self._validate_note_name(name)
if folder:
# Validate folder path
folder_path = self.vault_path / folder
if not folder_path.exists():
folder_path.mkdir(parents=True, exist_ok=True)
return folder_path / safe_name
else:
return self.vault_path / safe_name
def _resolve_note_with_fallback(self, name: str) -> Optional[Path]:
"""Resolve note name to path with smart fallback for similar notes."""
try:
# 1. Check memory first - highest priority
memory_result = self.memory.resolve_note_reference(name)
if memory_result:
note_path = self.vault_path / memory_result
if note_path.exists():
console.print(f"[dim]🧠 Memory resolved: '{name}' → '{memory_result}'[/dim]")
return note_path
else:
console.print(f"[warning]Memory reference invalid: {memory_result}[/warning]")
# 2. Check conversation context
context_result = self.context.resolve_reference(name)
if context_result:
note_path = self.vault_path / context_result
if note_path.exists():
console.print(f"[dim]🔄 Context resolved: '{name}' → '{context_result}'[/dim]")
# Remember this resolution for future use
self.memory.register_note_reference(name, context_result, "context resolution")
return note_path
# 3. Traditional contextual reference resolution
resolved_name = self._resolve_contextual_reference(name)
if resolved_name and resolved_name != name:
console.print(f"[dim]→ Resolving '{name}' to '{resolved_name}' from context[/dim]")
name = resolved_name
# Handle relative paths
if '/' in name:
note_path = self.vault_path / name
if not name.endswith('.md'):
note_path = self.vault_path / (name + '.md')
else:
note_path = self._get_note_path(name)
# First try exact match
if note_path.exists():
return note_path
# If exact match fails, try smart note finding
console.print(f"[warning]note '{name}' not found - searching for similar notes...[/warning]")
suggestions = self._find_similar_notes(name, max_suggestions=5)
if not suggestions:
console.print(f"[error]no similar notes found for '{name}'[/error]")
return None
# Ask user to confirm which note they meant
selected_note = self._ask_user_for_note_confirmation(name, suggestions)
if not selected_note:
console.print(f"[warning]note selection cancelled for '{name}'[/warning]")
return None
# Return the selected note path
return self.vault_path / selected_note
except Exception as e:
console.print(f"[error]❌ Error resolving note '{name}': {e}[/error]")
return None
def _resolve_contextual_reference(self, name: str) -> Optional[str]:
"""Resolve contextual references like 'current', 'last', 'recent' to actual note names."""
# This method should access working context from the agent LLM
# Since we don't have direct access, we'll implement a simple version here
# For references that can be resolved from recent operations
name_lower = name.lower().strip()
# These are handled by the LLM prompt context, but we can add some basic resolution here
if name_lower in ['current', 'focus', 'current note', 'focused note']:
# Could be resolved if we had access to working context
# For now, return None to let normal resolution handle it
return None
elif name_lower in ['last', 'last note', 'recent', 'recent note']:
# Could be resolved if we had access to working context
# For now, return None to let normal resolution handle it
return None
return None
def _backup_before_edit(self, file_path: Path) -> Optional[str]:
"""Create backup before editing if enabled."""
if not self.config.get_backup_before_edits():
return None
if file_path.exists():
backup_path = self.backup_manager.create_backup(
str(file_path),
backup_type="agent_edit"
)
return backup_path
return None
def _increment_tool_calls(self):
"""Increment and validate tool call count."""
self.tool_call_count += 1
max_calls = self.config.get_max_tool_calls()
if self.tool_call_count > max_calls:
raise AgentToolError(f"Maximum tool calls per session exceeded ({max_calls})")
def _open_in_obsidian_after_edit(self, note_path: Path) -> bool:
"""Open a note in Obsidian after editing it."""
try:
from utils import open_obsidian_with_file
return open_obsidian_with_file(str(note_path))
except Exception as e:
# Don't fail the operation if Obsidian opening fails
return False
def execute_tool_call(self, tool_call: Dict[str, Any]) -> ToolCallResult:
"""Execute a tool call with validation and error handling."""
try:
self._validate_vault_access()
self._increment_tool_calls()
tool_name = tool_call.get("tool_call")
arguments = tool_call.get("arguments", {})
if not tool_name:
return ToolCallResult(False, "No tool_call specified")
# Route to appropriate tool method
method_name = f"_tool_{tool_name}"
if not hasattr(self, method_name):
return ToolCallResult(False, f"Unknown tool call: {tool_name}")
method = getattr(self, method_name)
return method(**arguments)
except AgentToolError as e:
return ToolCallResult(False, str(e))
except Exception as e:
return ToolCallResult(False, f"Unexpected error: {str(e)}")
def learn_from_interaction(self, user_input: str, tool_call: Dict[str, Any], result: ToolCallResult):
"""Learn from user interactions for future reference resolution."""
try:
tool_name = tool_call.get("tool_call", "")
arguments = tool_call.get("arguments", {})
# Learn note references
if result.success and "note" in str(result.data):
resolved_note = None
# Extract resolved note name from result
if result.data and isinstance(result.data, dict):
resolved_note = (result.data.get("note_name") or
result.data.get("note_path") or
result.data.get("note"))
if resolved_note:
# Extract how user referred to the note
note_references = self._extract_user_note_references(user_input)
for ref in note_references:
self.memory.register_note_reference(ref, resolved_note, user_input)
# Extract entities from the content if applicable
if "content" in str(result.data):
content = result.data.get("content", "")
if content:
self.memory.extract_and_register_entities(content, resolved_note)
# Update conversation context
self.context.update_focus(resolved_note, tool_name)
# Learn user patterns
self.memory.learn_user_pattern(user_input, resolved_note)
except Exception as e:
console.print(f"[dim]Warning: Learning failed: {e}[/dim]")
def _extract_user_note_references(self, user_input: str) -> List[str]:
"""Extract how user refers to notes in their input."""
import re
references = []
# Common patterns for note references
patterns = [
r'(?:my|the)\s+([a-zA-Z][\w\s]{2,30})(?:\s+(?:note|file|document))?',
r'([a-zA-Z][\w\s]{2,30})\s+(?:note|document|file|sop)',
r'"([^"]+)"', # Quoted references
r'([A-Z][a-z]+(?:\s+[A-Z][a-z]+)*(?:\s+[A-Z][a-z]+)*)' # Title case phrases
]
for pattern in patterns:
matches = re.findall(pattern, user_input, re.IGNORECASE)
for match in matches:
if isinstance(match, tuple):
match = match[0]
match = match.strip()
if len(match) > 2 and match.lower() not in ['the', 'my', 'note', 'file', 'document']:
references.append(match)
return list(set(references)) # Remove duplicates
# File & Note Management Tools
def _tool_create_note(self, name: str, folder: Optional[str] = None, content: str = "") -> ToolCallResult:
"""Create a new note in the vault."""
try:
note_path = self._get_note_path(name, folder)
if note_path.exists():
return ToolCallResult(False, f"Note already exists: {note_path.name}")
# Create the note
note_path.write_text(content, encoding='utf-8')
# Open the note in Obsidian
obsidian_opened = self._open_in_obsidian_after_edit(note_path)
message = f"Created note: {note_path.relative_to(self.vault_path)}"
if obsidian_opened:
message += " (opened in Obsidian)"
return ToolCallResult(
True,
message,
{"note_name": name, "note_path": str(note_path.relative_to(self.vault_path)), "opened_in_obsidian": obsidian_opened}
)
except Exception as e:
return ToolCallResult(False, f"Failed to create note: {e}")
def _tool_delete_note(self, name: str) -> ToolCallResult:
"""Delete a note from the vault."""
try:
note_path = self._get_note_path(name)
if not note_path.exists():
return ToolCallResult(False, f"Note does not exist: {name}")
# Create backup before deletion
backup_path = self._backup_before_edit(note_path)
# Delete the note
note_path.unlink()
return ToolCallResult(
True,
f"Deleted note: {name}",
{"note_name": name, "note_path": str(note_path.relative_to(self.vault_path))},
backup_path
)
except Exception as e:
return ToolCallResult(False, f"Failed to delete note: {e}")
def _tool_rename_note(self, old_name: str, new_name: str) -> ToolCallResult:
"""Rename a note in the vault."""
try:
old_path = self._get_note_path(old_name)
new_path = self._get_note_path(new_name)
if not old_path.exists():
return ToolCallResult(False, f"Note does not exist: {old_name}")
if new_path.exists():
return ToolCallResult(False, f"Target name already exists: {new_name}")
# Create backup before rename
backup_path = self._backup_before_edit(old_path)
# Rename the note
old_path.rename(new_path)
# Open the renamed note in Obsidian
obsidian_opened = self._open_in_obsidian_after_edit(new_path)
message = f"Renamed note: {old_name} → {new_name}"
if obsidian_opened:
message += " (opened in Obsidian)"
return ToolCallResult(
True,
message,
{
"note_name": new_name,
"old_path": str(old_path.relative_to(self.vault_path)),
"new_path": str(new_path.relative_to(self.vault_path)),
"opened_in_obsidian": obsidian_opened
},
backup_path
)
except Exception as e:
return ToolCallResult(False, f"Failed to rename note: {e}")
def _tool_move_note(self, name: str, target_folder: str) -> ToolCallResult:
"""Move a note to a different folder."""
try:
current_path = self._get_note_path(name)
target_path = self._get_note_path(name, target_folder)
if not current_path.exists():
return ToolCallResult(False, f"Note does not exist: {name}")
if target_path.exists():
return ToolCallResult(False, f"Note already exists in target folder: {target_folder}/{name}")
# Create backup before move
backup_path = self._backup_before_edit(current_path)
# Ensure target folder exists
target_path.parent.mkdir(parents=True, exist_ok=True)
# Move the note
current_path.rename(target_path)
# Open the moved note in Obsidian
obsidian_opened = self._open_in_obsidian_after_edit(target_path)
message = f"Moved note: {name} → {target_folder}/"
if obsidian_opened:
message += " (opened in Obsidian)"
return ToolCallResult(
True,
message,
{
"old_path": str(current_path.relative_to(self.vault_path)),
"new_path": str(target_path.relative_to(self.vault_path)),
"opened_in_obsidian": obsidian_opened
},
backup_path
)
except Exception as e:
return ToolCallResult(False, f"Failed to move note: {e}")
def _tool_list_notes(self, query: Optional[str] = None, folder: Optional[str] = None) -> ToolCallResult:
"""List notes in the vault with optional filtering and semantic search."""
try:
search_path = self.vault_path
if folder:
search_path = self.vault_path / folder
if not search_path.exists():
return ToolCallResult(False, f"Folder does not exist: {folder}")
# Find all markdown files
md_files = list(search_path.rglob("*.md"))
# Filter by query if provided
if query:
query_lower = query.lower()
filtered_files = []
# Multi-word semantic search
query_words = query_lower.split()
for f in md_files:
file_path_lower = str(f.relative_to(self.vault_path)).lower()
# Method 1: Exact phrase match (highest priority)
if query_lower in file_path_lower:
filtered_files.append((f, 100)) # Priority score
# Method 2: All words present (high priority)
elif all(word in file_path_lower for word in query_words):
filtered_files.append((f, 90))
# Method 3: Most words present (medium priority)
elif len(query_words) > 1:
word_matches = sum(1 for word in query_words if word in file_path_lower)
if word_matches >= len(query_words) * 0.5: # At least half the words
priority = 50 + (word_matches / len(query_words)) * 30
filtered_files.append((f, priority))
# Method 4: Single word match (lower priority)
else:
# Check both filename and full path (including folder names)
filename_match = query_lower in f.stem.lower()
path_match = query_lower in file_path_lower
if filename_match or path_match:
filtered_files.append((f, 30))
# Sort by priority (highest first) and extract files
filtered_files.sort(key=lambda x: x[1], reverse=True)
md_files = [f[0] for f in filtered_files]
# Convert to relative paths
note_list = [str(f.relative_to(self.vault_path)) for f in md_files]
return ToolCallResult(
True,
f"Found {len(note_list)} notes" + (f" matching '{query}'" if query else ""),
{"notes": note_list, "count": len(note_list), "query": query}
)
except Exception as e:
return ToolCallResult(False, f"Failed to list notes: {e}")
def _find_similar_notes(self, target_name: str, max_suggestions: int = 5) -> List[Dict[str, Any]]:
"""Find notes similar to the target name using improved matching algorithms."""
try:
# Get all notes in the vault
all_notes_result = self._tool_list_notes()
if not all_notes_result.success:
return []
all_notes = all_notes_result.data.get('notes', [])
if not all_notes:
return []
# Use our improved matching algorithm directly
return self._fallback_simple_matching(target_name, all_notes, max_suggestions)
except Exception as e:
# Return empty list on any error
console.print(f"[error]Error finding similar notes: {e}[/error]")
return []
def _fallback_simple_matching(self, target_name: str, all_notes: List[str], max_suggestions: int) -> List[Dict[str, Any]]:
"""Advanced fallback matching using multiple algorithms."""
import difflib
import re
# Clean target name for matching
target_clean = target_name.lower().replace('.md', '').strip()
target_words = re.findall(r'\w+', target_clean)
suggestions = []
for note in all_notes:
note_clean = note.lower().replace('.md', '').strip()
note_words = re.findall(r'\w+', note_clean)
# 1. Exact substring match (highest priority)
if target_clean in note_clean:
suggestions.append({
"note": note,
"reason": f"Contains '{target_clean}'",
"confidence": 0.95
})
continue
# 2. Reverse substring match
if note_clean in target_clean:
suggestions.append({
"note": note,
"reason": f"Note name contained in search",
"confidence": 0.90
})
continue
# 3. Word-by-word matching
word_matches = 0
partial_matches = 0
for target_word in target_words:
for note_word in note_words:
if target_word == note_word:
word_matches += 1
elif target_word in note_word or note_word in target_word:
partial_matches += 0.5
# 4. Fuzzy string matching
similarity = difflib.SequenceMatcher(None, target_clean, note_clean).ratio()
# 5. Check for common patterns
pattern_bonus = 0
# Check if it's a folder path search
if '/' in target_name:
folder_part = target_name.split('/')[-1].lower()
if folder_part in note_clean:
pattern_bonus = 0.3
# Calculate final score
word_score = (word_matches + partial_matches) / len(target_words) if target_words else 0
final_score = max(similarity, word_score) + pattern_bonus
# Only include notes with decent relevance
if final_score > 0.25 or word_matches > 0:
reason_parts = []
if word_matches > 0:
reason_parts.append(f"{word_matches} exact word matches")
if partial_matches > 0:
reason_parts.append(f"{partial_matches:.1f} partial matches")
if similarity > 0.3:
reason_parts.append(f"{similarity:.1%} similarity")
reason = ", ".join(reason_parts) if reason_parts else f"Low similarity ({similarity:.1%})"
suggestions.append({
"note": note,
"reason": reason,
"confidence": final_score
})
# Sort by confidence and return top suggestions
suggestions.sort(key=lambda x: x['confidence'], reverse=True)
return suggestions[:max_suggestions]
def _ask_user_for_note_confirmation(self, target_name: str, suggestions: List[Dict[str, Any]]) -> Optional[str]:
"""Clean note selection interface like modern CLI tools."""
console.print(f"[muted]note '{target_name}' not found. similar notes:[/muted]")
console.print()
# Show options cleanly
for i, suggestion in enumerate(suggestions, 1):
note_name = suggestion['note']
confidence = suggestion.get('confidence', 0)
reason = suggestion.get('reason', '')
# Clean confidence indicator
if confidence >= 0.8:
conf_icon = "●●●"
elif confidence >= 0.6:
conf_icon = "●●○"
else:
conf_icon = "●○○"
console.print(f" [muted]{i}.[/muted] {note_name}")
console.print(f" [muted]{conf_icon} {reason}[/muted]")
console.print()
# Add manual entry option
console.print(f" [muted]{len(suggestions) + 1}.[/muted] [italic]type exact note name manually[/italic]")
console.print()
# Simple prompt
console.print(f"[muted]select note (1-{len(suggestions) + 1}) or 'n' to cancel:[/muted] ", end="")
choice = input().strip()
if choice.lower() in ['n', 'no', 'cancel']:
return None
try:
index = int(choice) - 1
if 0 <= index < len(suggestions):
selected_note = suggestions[index]['note']
console.print(f"[success]selected:[/success] {selected_note}")
return selected_note
elif index == len(suggestions):
# Manual entry
console.print("[muted]enter exact note name (with or without .md):[/muted] ", end="")
manual_name = input().strip()
if manual_name:
if not manual_name.endswith('.md'):
manual_name += '.md'
console.print(f"[success]using:[/success] {manual_name}")
return manual_name
else:
console.print("[error]no name entered[/error]")
return None
else:
console.print("[error]invalid selection[/error]")
return None
except ValueError:
console.print("[error]invalid input[/error]")
return None
# Section Editing Tools
def _tool_insert_section(self, note: str, heading: str, content: str, position: str = "end") -> ToolCallResult:
"""Insert a new section in a note with smart note finding."""
try:
note_path = self._resolve_note_with_fallback(note)
if not note_path:
return ToolCallResult(False, f"Could not find or resolve note: {note}")
# Create backup before edit
backup_path = self._backup_before_edit(note_path)
# Read current content
current_content = note_path.read_text(encoding='utf-8')
# Format section
section_content = f"\n## {heading}\n\n{content}\n"
if position == "end":
new_content = current_content + section_content
elif position == "start":
new_content = section_content + current_content
else:
# Insert after specific heading (future enhancement)
new_content = current_content + section_content
# Write updated content
note_path.write_text(new_content, encoding='utf-8')
# Open the note in Obsidian
obsidian_opened = self._open_in_obsidian_after_edit(note_path)
message = f"Inserted section '{heading}' in {note}"
if obsidian_opened:
message += " (opened in Obsidian)"
return ToolCallResult(
True,
message,
{"note": note, "heading": heading, "position": position, "opened_in_obsidian": obsidian_opened},
backup_path
)
except Exception as e:
return ToolCallResult(False, f"Failed to insert section: {e}")
def _tool_append_section(self, note: str, heading: str, content: str) -> ToolCallResult:
"""Append content to an existing section with smart note finding."""
try:
note_path = self._resolve_note_with_fallback(note)
if not note_path:
return ToolCallResult(False, f"Could not find or resolve note: {note}")
# Create backup before edit
backup_path = self._backup_before_edit(note_path)
# Read current content
current_content = note_path.read_text(encoding='utf-8')
# Find the section and append content
lines = current_content.split('\n')
new_lines = []
found_section = False
in_section = False
for i, line in enumerate(lines):
new_lines.append(line)
# Check if this is the target heading
if line.strip().startswith('#') and heading.lower() in line.lower():
found_section = True
in_section = True
continue
# Check if we've reached the next section
if in_section and line.strip().startswith('#'):
# Insert content before this new section
new_lines.insert(-1, content)
in_section = False
# If we found the section but never hit another heading, append at the end
if found_section and in_section:
new_lines.append(content)
if not found_section:
return ToolCallResult(False, f"Section '{heading}' not found in {note}")
# Write updated content
note_path.write_text('\n'.join(new_lines), encoding='utf-8')
# Open the note in Obsidian
obsidian_opened = self._open_in_obsidian_after_edit(note_path)
message = f"Appended content to section '{heading}' in {note}"
if obsidian_opened:
message += " (opened in Obsidian)"
return ToolCallResult(
True,
message,
{"note": note, "heading": heading, "opened_in_obsidian": obsidian_opened},
backup_path
)
except Exception as e:
return ToolCallResult(False, f"Failed to append to section: {e}")
def _tool_replace_section(self, note: str, heading: str, content: str) -> ToolCallResult:
"""
Replace the ENTIRE content of an existing section.
WARNING: This completely replaces all content in the section.
For minor edits, use edit_section_content or fix_spelling_in_section instead.
"""
try:
note_path = self._resolve_note_with_fallback(note)
if not note_path:
return ToolCallResult(False, f"Could not find or resolve note: {note}")
# Create backup before edit
backup_path = self._backup_before_edit(note_path)
# Read current content
current_content = note_path.read_text(encoding='utf-8')
# Find and replace the section content
lines = current_content.split('\n')
new_lines = []
found_section = False
in_section = False
for line in lines:
# Check if this is the target heading
if line.strip().startswith('#') and heading.lower() in line.lower():
new_lines.append(line)
new_lines.append('') # Empty line after heading
new_lines.append(content)
found_section = True
in_section = True
continue
# Skip content in target section
if in_section:
if line.strip().startswith('#'):
# Reached next section
new_lines.append('') # Empty line before next section
new_lines.append(line)
in_section = False
# Skip lines within the target section
continue
new_lines.append(line)
if not found_section:
return ToolCallResult(False, f"Section '{heading}' not found in {note}")
# Write updated content
note_path.write_text('\n'.join(new_lines), encoding='utf-8')
# Open the note in Obsidian
obsidian_opened = self._open_in_obsidian_after_edit(note_path)
message = f"Replaced section '{heading}' in {note}"
if obsidian_opened:
message += " (opened in Obsidian)"
return ToolCallResult(
True,
message,
{"note": note, "heading": heading, "opened_in_obsidian": obsidian_opened},
backup_path
)
except Exception as e:
return ToolCallResult(False, f"Failed to replace section: {e}")
def _tool_edit_section_content(self, note: str, heading: str, old_text: str, new_text: str) -> ToolCallResult:
"""Edit specific content within a section without replacing the entire section."""
try:
note_path = self._resolve_note_with_fallback(note)
if not note_path:
return ToolCallResult(False, f"Could not find or resolve note: {note}")
# Create backup before edit
backup_path = self._backup_before_edit(note_path)
# Read current content
current_content = note_path.read_text(encoding='utf-8')
# Find the section and make targeted edit
lines = current_content.split('\n')
new_lines = []
found_section = False
in_section = False
section_content_lines = []
for line in lines:
# Check if this is the target heading
if line.strip().startswith('#') and heading.lower() in line.lower():
new_lines.append(line)
found_section = True
in_section = True
continue
# Check if we've reached the next section
if in_section and line.strip().startswith('#'):
# Process the collected section content
section_content = '\n'.join(section_content_lines)
if old_text in section_content:
edited_content = section_content.replace(old_text, new_text)
new_lines.extend(edited_content.split('\n'))
else:
new_lines.extend(section_content_lines)
# Add the new section header
new_lines.append(line)
in_section = False
section_content_lines = []
continue
# Collect section content or add to output
if in_section:
section_content_lines.append(line)
else:
new_lines.append(line)
# Handle case where target section is at the end of file
if found_section and in_section and section_content_lines:
section_content = '\n'.join(section_content_lines)
if old_text in section_content:
edited_content = section_content.replace(old_text, new_text)
new_lines.extend(edited_content.split('\n'))
else:
new_lines.extend(section_content_lines)
if not found_section:
return ToolCallResult(False, f"Section '{heading}' not found in {note}")
# Check if any changes were made
final_content = '\n'.join(new_lines)
if final_content == current_content:
return ToolCallResult(False, f"Text '{old_text}' not found in section '{heading}'")
# Write updated content
note_path.write_text(final_content, encoding='utf-8')
# Open the note in Obsidian
obsidian_opened = self._open_in_obsidian_after_edit(note_path)
message = f"Edited content in section '{heading}' of {note}"
if obsidian_opened:
message += " (opened in Obsidian)"
return ToolCallResult(
True,
message,
{"note": note, "heading": heading, "old_text": old_text, "new_text": new_text, "opened_in_obsidian": obsidian_opened},
backup_path
)
except Exception as e:
return ToolCallResult(False, f"Failed to edit section content: {e}")
def _tool_fix_spelling_in_section(self, note: str, heading: str, corrections: dict) -> ToolCallResult:
"""Fix multiple spelling errors in a section using a dictionary of corrections."""
try:
note_path = self._resolve_note_with_fallback(note)
if not note_path:
return ToolCallResult(False, f"Could not find or resolve note: {note}")
# Create backup before edit
backup_path = self._backup_before_edit(note_path)
# Read current content
current_content = note_path.read_text(encoding='utf-8')
# Find the section and make spelling corrections
lines = current_content.split('\n')
new_lines = []
found_section = False
in_section = False
section_content_lines = []
corrections_made = 0
for line in lines:
# Check if this is the target heading
if line.strip().startswith('#') and heading.lower() in line.lower():
new_lines.append(line)
found_section = True
in_section = True
continue
# Check if we've reached the next section
if in_section and line.strip().startswith('#'):
# Process the collected section content with corrections
section_content = '\n'.join(section_content_lines)
corrected_content = section_content
for wrong_word, correct_word in corrections.items():
if wrong_word in corrected_content:
corrected_content = corrected_content.replace(wrong_word, correct_word)
corrections_made += 1
new_lines.extend(corrected_content.split('\n'))
# Add the new section header
new_lines.append(line)
in_section = False
section_content_lines = []
continue
# Collect section content or add to output
if in_section:
section_content_lines.append(line)
else:
new_lines.append(line)
# Handle case where target section is at the end of file
if found_section and in_section and section_content_lines:
section_content = '\n'.join(section_content_lines)
corrected_content = section_content
for wrong_word, correct_word in corrections.items():
if wrong_word in corrected_content:
corrected_content = corrected_content.replace(wrong_word, correct_word)
corrections_made += 1
new_lines.extend(corrected_content.split('\n'))
if not found_section:
return ToolCallResult(False, f"Section '{heading}' not found in {note}")
if corrections_made == 0:
return ToolCallResult(False, f"No spelling errors found to correct in section '{heading}'")
# Write updated content
note_path.write_text('\n'.join(new_lines), encoding='utf-8')
# Open the note in Obsidian
obsidian_opened = self._open_in_obsidian_after_edit(note_path)
message = f"Fixed {corrections_made} spelling error(s) in section '{heading}' of {note}"
if obsidian_opened:
message += " (opened in Obsidian)"
return ToolCallResult(