discourse/plugins/discourse-ai/lib/agents/tool_runner/index.rb
Jarek Radosz 8baf4d5d4c
DEV: Enable Style/RedundantSelf rubocop rule (#40098)
(to be enabled in the shared config)
2026-05-19 19:27:45 +02:00

51 lines
1.3 KiB
Ruby
Vendored

# frozen_string_literal: true
module DiscourseAi
module Agents
class ToolRunner
module Index
def attach_index(mini_racer_context)
mini_racer_context.attach(
"_index_search",
->(*params) do
in_attached_function do
query, options = params
self.running_attached_function = true
options ||= {}
options = options.symbolize_keys
rag_search(query, **options)
end
end,
)
mini_racer_context.attach(
"_index_get_file",
->(filename) { in_attached_function { rag_get_file(filename) } },
)
end
private
def rag_search(query, filenames: nil, limit: 10)
RagDocumentFragment
.search(
target_id: tool.id,
target_type: "AiTool",
query: query,
filenames: filenames,
limit: limit,
)
.map { |fragment| { fragment: fragment[:fragment], metadata: fragment[:metadata] } }
end
def rag_get_file(filename)
RagDocumentFragment.read_file(
target_id: tool.id,
target_type: "AiTool",
filename: filename,
)
end
end
end
end
end