Class: ObsidianFetch::Vault

Inherits:
Object
  • Object
show all
Defined in:
lib/obsidian_fetch.rb

Constant Summary collapse

MAX_LIST_SIZE =
20

Instance Attribute Summary collapse

Instance Method Summary collapse

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

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

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

#notesObject (readonly)

Returns the value of attribute notes.



12
13
14
# File 'lib/obsidian_fetch.rb', line 12

def notes
  @notes
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 "      It cannot be listed in a blank string.\n    EOS\n  end\n  matched_notes = @notes.select do |note_name, _file_pathes|\n    split_name.map {|name_part| note_name.include?(name_part) }.all?\n  end\n  # \u540D\u524D\u3067\u691C\u7D22\u3057\u305F\u304C\u898B\u3064\u304B\u3089\u306A\u3044\u5834\u5408\n  if matched_notes.empty?\n    has_found_link = @links_by_file_name[name] && !@links_by_file_name[name].empty?\n    return <<~EOS unless has_found_link\n      Note not found: \#{name}\n      Search again with a substring or a string with a different notation.\n    EOS\n    return <<~EOS\n      Note not found: \#{name}\n      However, I found other notes linked to this note.\n      \#{@links_by_file_name[name].shuffle.map { |file_path| \"- \#{File.basename(file_path, '.md')}\" }.join(\"\\n\")}\n    EOS\n  end\n  # \u30DE\u30C3\u30C1\u3057\u305F\u540D\u524D\u306E\u6570\u304C\u591A\u3059\u304E\u308B\u5834\u5408\u306F\u3001\u30E9\u30F3\u30C0\u30E0\u306BMAX_LIST_SIZE\u500B\u9078\u3076\n  preface = \"Notes matching '\#{name}' are as follows.\\n\"\n  if matched_notes.size > MAX_LIST_SIZE\n    matched_notes = matched_notes.to_a.sample(MAX_LIST_SIZE).to_h\n    preface = \"Too many notes matched. I will show you only \#{MAX_LIST_SIZE} of them.\\n\" + preface\n  end\n  # \u30DE\u30C3\u30C1\u3057\u305F\u540D\u524D\u306E\u30EA\u30B9\u30C8\u3067\u8FD4\u3059\n  list = matched_notes.keys.shuffle.map do |note_name|\n    \"- \#{note_name}\"\n  end.join(\"\\n\")\n  preface + list\nend\n"

#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 = "          Presumably a path was specified. The process was automatically renamed and processed.\n        EOS\n        return list_links(fixed_name, preface)\n      end\n    else\n      # \u30CE\u30FC\u30C8\u540D\u304C\u898B\u3064\u304B\u3063\u305F\u5834\u5408\u306F\u3001preface\u3092\u8FFD\u52A0\u3059\u308B\n      preface = <<~EOS\n        Presumably a path was specified. The process was automatically renamed and processed.\n      EOS\n      return open_file(fixed_name, file_pathes, preface)\n    end\n  end\n\n  # \u540D\u524D\u306E\u30CE\u30FC\u30C8\u304C\u5B58\u5728\u3057\u306A\u3044\u5834\u5408\n  if file_pathes.nil?\n    return note_not_found(name) if @links_by_file_name[name].nil?\n    return list_links(name, preface)\n  end\n\n  open_file(name, file_pathes, preface)\nend\n"