Roman Rizzi 30242a27e6
REFACTOR: Move personas into its own module. (#1233)
This change moves all the personas code into its own module. We want to treat them as a building block features can built on top of, same as `Completions::Llm`.

The code to title a message was moved from `Bot` to `Playground`.
2025-03-31 14:42:33 -03:00

63 lines
1.5 KiB
Ruby

# frozen_string_literal: true
module DiscourseAi
module Personas
module Tools
class DbSchema < Tool
def self.signature
{
name: name,
description: "Will load schema information for specific tables in the database",
parameters: [
{
name: "tables",
description:
"list of tables to load schema information for, comma separated list eg: (users,posts))",
type: "string",
required: true,
},
],
}
end
def self.name
"schema"
end
def tables
parameters[:tables]
end
def invoke
tables_arr = tables.split(",").map(&:strip)
table_info = {}
DB
.query(<<~SQL, tables_arr)
select table_name, column_name, data_type from information_schema.columns
where table_schema = 'public'
and table_name in (?)
order by table_name
SQL
.each do |row|
(table_info[row.table_name] ||= []) << "#{row.column_name} #{row.data_type}"
end
schema_info =
table_info
.map { |table_name, columns| "#{table_name}(#{columns.join(",")})" }
.join("\n")
{ schema_info: schema_info, tables: tables }
end
protected
def description_args
{ tables: tables }
end
end
end
end
end