Class: ObsidianFetch::Vault
- Inherits:
-
Object
- Object
- ObsidianFetch::Vault
- Defined in:
- lib/obsidian_fetch.rb
Constant Summary collapse
- MAX_LIST_SIZE =
20
Instance Attribute Summary collapse
-
#links_by_file_name ⇒ Object
readonly
Returns the value of attribute links_by_file_name.
-
#links_by_file_path ⇒ Object
readonly
Returns the value of attribute links_by_file_path.
-
#notes ⇒ Object
readonly
Returns the value of attribute notes.
Class Method Summary collapse
-
.normalize_note_name(note_name) ⇒ Object
ノート名を正規化する ファイル名に使えない文字については除去しない。aliasで設定されている場合もあるため。.
Instance Method Summary collapse
-
#initialize(vault_pathes) ⇒ Vault
constructor
A new instance of Vault.
- #tool_list(name) ⇒ Object
- #tool_read(name) ⇒ Object
Constructor Details
#initialize(vault_pathes) ⇒ Vault
Returns a new instance of Vault.
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/obsidian_fetch.rb', line 14 def initialize vault_pathes @vault_pathes = vault_pathes # key: note_name, value: [file_pathes] @notes = {} # key: file_path, value: [file_pathes] @links_by_file_path = {} # ノートが見つからなかった場合に被リンクを表示するためのハッシュ # key: note_name, value: [file_pathes] @links_by_file_name = {} collect_pathes() collect_notes() collect_links() end |
Instance Attribute Details
#links_by_file_name ⇒ Object (readonly)
Returns the value of attribute links_by_file_name.
12 13 14 |
# File 'lib/obsidian_fetch.rb', line 12 def links_by_file_name @links_by_file_name end |
#links_by_file_path ⇒ Object (readonly)
Returns the value of attribute links_by_file_path.
12 13 14 |
# File 'lib/obsidian_fetch.rb', line 12 def links_by_file_path @links_by_file_path end |
#notes ⇒ Object (readonly)
Returns the value of attribute notes.
12 13 14 |
# File 'lib/obsidian_fetch.rb', line 12 def notes @notes end |
Class Method Details
.normalize_note_name(note_name) ⇒ Object
ノート名を正規化するファイル名に使えない文字については除去しない。aliasで設定されている場合もあるため。
42 43 44 45 |
# File 'lib/obsidian_fetch.rb', line 42 def self.normalize_note_name(note_name) # ノート名を小文字に変換し、リンクに使えない文字を除去する note_name.gsub(/[\[\]#\^\|]/, '') end |
Instance Method Details
#tool_list(name) ⇒ Object
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 |
# File 'lib/obsidian_fetch.rb', line 210 def tool_list name name = Vault.normalize_note_name(name) split_name = name.split(/[\s ]+/) # 空白文字で検索された場合は失敗した旨を返す # 仮に全ノートからランダムなMAX_LIST_SIZEを返してしまうと、LLMが誤って呼び出した場合に偽の関連性を持ってしまうため if split_name.empty? return <<~EOS It cannot be listed in a blank string. EOS end matched_notes = @notes.select do |note_name, _file_pathes| split_name.map {|name_part| note_name.include?(name_part) }.all? end # 名前で検索したが見つからない場合 if matched_notes.empty? has_found_link = @links_by_file_name[name] && !@links_by_file_name[name].empty? return <<~EOS unless has_found_link Note not found: #{name} Search again with a substring or a string with a different notation. EOS return <<~EOS Note not found: #{name} However, I found other notes linked to this note. #{@links_by_file_name[name].shuffle.map { |file_path| "- #{File.basename(file_path, '.md')}" }.join("\n")} EOS end # マッチした名前の数が多すぎる場合は、ランダムにMAX_LIST_SIZE個選ぶ preface = "Notes matching '#{name}' are as follows.\n" if matched_notes.size > MAX_LIST_SIZE matched_notes = matched_notes.to_a.sample(MAX_LIST_SIZE).to_h preface = "Too many notes matched. I will show you only #{MAX_LIST_SIZE} of them.\n" + preface end # マッチした名前のリストで返す list = matched_notes.keys.shuffle.map do |note_name| "- #{note_name}" end.join("\n") preface + list end |
#tool_read(name) ⇒ Object
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 |
# File 'lib/obsidian_fetch.rb', line 133 def tool_read name name = Vault.normalize_note_name(name) file_pathes = @notes[name] # 名前のノートが見つからないが、nameがパスっぽい場合は、パスを修正したうえでもう一度試す preface = "" if file_pathes.nil? && name.include?('/') fixed_name = File.basename(name, '.md') file_pathes = @notes[fixed_name] if file_pathes.nil? # もしも名前で見つからなければ、リンクにも存在しないか確認する link_pathes = @links_by_file_name[fixed_name] if link_pathes.nil? return note_not_found(name) else # リンク先のノートが見つかった場合は、prefaceを追加する preface = <<~EOS Presumably a path was specified. The process was automatically renamed and processed. EOS return list_links(fixed_name, preface) end else # ノート名が見つかった場合は、prefaceを追加する preface = <<~EOS Presumably a path was specified. The process was automatically renamed and processed. EOS return open_file(fixed_name, file_pathes, preface) end end # 名前のノートが存在しない場合 if file_pathes.nil? return note_not_found(name) if @links_by_file_name[name].nil? return list_links(name, preface) end open_file(name, file_pathes, preface) end |