You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
3.0 KiB

11 months ago
  1. module.exports.SqlAgentListTables = {
  2. name: "sql-list-tables",
  3. plugin: function () {
  4. const {
  5. listSQLConnections,
  6. getDBClient,
  7. } = require("./SQLConnectors/index.js");
  8. return {
  9. name: "sql-list-tables",
  10. setup(aibitat) {
  11. aibitat.function({
  12. super: aibitat,
  13. name: this.name,
  14. description:
  15. "List all available tables in a database via its `database_id`.",
  16. examples: [
  17. {
  18. prompt: "What tables are there in the `access-logs` database?",
  19. call: JSON.stringify({ database_id: "access-logs" }),
  20. },
  21. {
  22. prompt:
  23. "What information can you access in the customer_accts postgres db?",
  24. call: JSON.stringify({ database_id: "customer_accts" }),
  25. },
  26. {
  27. prompt: "Can you tell me what is in the primary-logs db?",
  28. call: JSON.stringify({ database_id: "primary-logs" }),
  29. },
  30. ],
  31. parameters: {
  32. $schema: "http://json-schema.org/draft-07/schema#",
  33. type: "object",
  34. properties: {
  35. database_id: {
  36. type: "string",
  37. description:
  38. "The database identifier for which we will list all tables for. This is a required parameter",
  39. },
  40. },
  41. additionalProperties: false,
  42. },
  43. required: ["database_id"],
  44. handler: async function ({ database_id = "" }) {
  45. try {
  46. this.super.handlerProps.log(`Using the sql-list-tables tool.`);
  47. const databaseConfig = (await listSQLConnections()).find(
  48. (db) => db.database_id === database_id
  49. );
  50. if (!databaseConfig) {
  51. this.super.handlerProps.log(
  52. `sql-list-tables failed to find config!`,
  53. database_id
  54. );
  55. return `No database connection for ${database_id} was found!`;
  56. }
  57. const db = getDBClient(databaseConfig.engine, databaseConfig);
  58. this.super.introspect(
  59. `${this.caller}: Checking what are the available tables in the ${databaseConfig.database_id} database.`
  60. );
  61. this.super.introspect(`Running SQL: ${db.getTablesSql()}`);
  62. const result = await db.runQuery(db.getTablesSql(database_id));
  63. if (result.error) {
  64. this.super.handlerProps.log(
  65. `sql-list-tables tool reported error`,
  66. result.error
  67. );
  68. this.super.introspect(`Error: ${result.error}`);
  69. return `There was an error running the query: ${result.error}`;
  70. }
  71. return JSON.stringify(result);
  72. } catch (e) {
  73. console.error(e);
  74. return e.message;
  75. }
  76. },
  77. });
  78. },
  79. };
  80. },
  81. };