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.

98 lines
3.4 KiB

11 months ago
  1. module.exports.SqlAgentGetTableSchema = {
  2. name: "sql-get-table-schema",
  3. plugin: function () {
  4. const {
  5. listSQLConnections,
  6. getDBClient,
  7. } = require("./SQLConnectors/index.js");
  8. return {
  9. name: "sql-get-table-schema",
  10. setup(aibitat) {
  11. aibitat.function({
  12. super: aibitat,
  13. name: this.name,
  14. description:
  15. "Gets the table schema in SQL for a given `table` and `database_id`",
  16. examples: [
  17. {
  18. prompt: "What does the customers table in access-logs look like?",
  19. call: JSON.stringify({
  20. database_id: "access-logs",
  21. table_name: "customers",
  22. }),
  23. },
  24. {
  25. prompt:
  26. "Get me the full name of a company in records-main, the table should be call comps",
  27. call: JSON.stringify({
  28. database_id: "records-main",
  29. table_name: "comps",
  30. }),
  31. },
  32. ],
  33. parameters: {
  34. $schema: "http://json-schema.org/draft-07/schema#",
  35. type: "object",
  36. properties: {
  37. database_id: {
  38. type: "string",
  39. description:
  40. "The database identifier for which we will connect to to query the table schema. This is a required field.",
  41. },
  42. table_name: {
  43. type: "string",
  44. description:
  45. "The database identifier for the table name we want the schema for. This is a required field.",
  46. },
  47. },
  48. additionalProperties: false,
  49. },
  50. required: ["database_id", "table_name"],
  51. handler: async function ({ database_id = "", table_name = "" }) {
  52. this.super.handlerProps.log(`Using the sql-get-table-schema tool.`);
  53. try {
  54. const databaseConfig = (await listSQLConnections()).find(
  55. (db) => db.database_id === database_id
  56. );
  57. if (!databaseConfig) {
  58. this.super.handlerProps.log(
  59. `sql-get-table-schema to find config!`,
  60. database_id
  61. );
  62. return `No database connection for ${database_id} was found!`;
  63. }
  64. const db = getDBClient(databaseConfig.engine, databaseConfig);
  65. this.super.introspect(
  66. `${this.caller}: Querying the table schema for ${table_name} in the ${databaseConfig.database_id} database.`
  67. );
  68. this.super.introspect(
  69. `Running SQL: ${db.getTableSchemaSql(table_name)}`
  70. );
  71. const result = await db.runQuery(
  72. db.getTableSchemaSql(table_name)
  73. );
  74. if (result.error) {
  75. this.super.handlerProps.log(
  76. `sql-get-table-schema tool reported error`,
  77. result.error
  78. );
  79. this.super.introspect(`Error: ${result.error}`);
  80. return `There was an error running the query: ${result.error}`;
  81. }
  82. return JSON.stringify(result);
  83. } catch (e) {
  84. this.super.handlerProps.log(
  85. `sql-get-table-schema raised an error. ${e.message}`
  86. );
  87. return e.message;
  88. }
  89. },
  90. });
  91. },
  92. };
  93. },
  94. };