From 758a81c7373f398294ccb80d00a5c7d181f151d7 Mon Sep 17 00:00:00 2001 From: Nicholas Wengel Date: Wed, 14 Feb 2024 22:13:08 -0700 Subject: [PATCH] feat: postgres context provider first slice --- .../providers/PostgresContextProvider.ts | 143 ++++++++++++++++++ core/context/providers/index.ts | 2 + core/index.d.ts | 3 +- 3 files changed, 147 insertions(+), 1 deletion(-) create mode 100644 core/context/providers/PostgresContextProvider.ts diff --git a/core/context/providers/PostgresContextProvider.ts b/core/context/providers/PostgresContextProvider.ts new file mode 100644 index 000000000..b76748a64 --- /dev/null +++ b/core/context/providers/PostgresContextProvider.ts @@ -0,0 +1,143 @@ +import { BaseContextProvider } from ".."; +import { + ContextItem, + ContextProviderDescription, + ContextProviderExtras, + LoadSubmenuItemsArgs, + ContextSubmenuItem, +} from "../.."; + +const ALL_TABLES = "__all_tables"; +const DEFAULT_SAMPLE_ROWS = 3; + +class PostgresContextProvider extends BaseContextProvider { + static description: ContextProviderDescription = { + title: "postgres", + displayTitle: "PostgreSQL", + description: "Retrieve PostgreSQL table schema and sample rows", + type: "submenu", + }; + + constructor(options: { + host: string; + port: number; + user: string; + password: string; + database: string; + schema?: string; + sampleRows?: number; + }) { + super(options); + } + + private async getPool() { + const pg = await require("pg"); + return new pg.Pool({ + host: this.options.host, + port: this.options.port, + user: this.options.user, + password: this.options.password, + database: this.options.database, + }); + } + + async getContextItems( + query: string = "", + _: ContextProviderExtras = {} as ContextProviderExtras + ): Promise { + const pool = await this.getPool(); + + try { + const contextItems: ContextItem[] = []; + + var schemaQuery = ` +SELECT column_name, data_type, character_maximum_length +FROM INFORMATION_SCHEMA.COLUMNS`; + if (this.options.query != ALL_TABLES) { + // Get the table schema + const tableName = query; + if (!tableName.includes(".")) { + throw new Error( + `Table name must be in format schema.table_name, got ${tableName}` + ); + } + schemaQuery += `WHERE table_name = '${tableName.split(".")[1]}'`; + } + + const { rows: tableSchemas } = await pool.query(schemaQuery); + + var prompt = `Postgres schema for database ${this.options.database}`; + for (const tableSchema of tableSchemas) { + const tableName = query; + + // Get the sample rows + const sampleRows = this.options.sampleRows ?? DEFAULT_SAMPLE_ROWS; + const { rows: sampleRowResults } = await pool.query(` +SELECT * +FROM ${tableName} +LIMIT ${sampleRows}`); + + // Create prompt from the table schema and sample rows + var prompt = `Table schema for ${tableName}:\n${JSON.stringify( + tableSchema, + null, + 2 + )}\n\n`; + prompt += `Sample rows: ${JSON.stringify(sampleRowResults, null, 2)}`; + } + + contextItems.push({ + name: `${this.options.database}-${query}-schema-and-sample-rows`, + description: `Schema and sample rows for ${ + query == ALL_TABLES ? "all table" : query + } in ${this.options.database} database`, + content: prompt, + }); + + return contextItems; + } catch (error) { + throw new Error(`Failed to query PostgreSQL database: ${error}`); + } finally { + } + } + + async loadSubmenuItems( + _: LoadSubmenuItemsArgs + ): Promise { + const pool = await this.getPool(); + + const schema = this.options.schema ?? "public"; + var tablesInfoQuery = ` +SELECT table_schema, table_name +FROM information_schema.tables`; + if (schema != null) { + tablesInfoQuery += ` WHERE table_schema = '${schema}'`; + } + const tablesInfo = await pool.query(tablesInfoQuery).rows; + + try { + const contextItems: ContextSubmenuItem[] = []; + + for (const tableInfo of tablesInfo) { + const tableName = `${tableInfo.table_schema}.${tableInfo.table_name}`; + + contextItems.push({ + id: tableName, + title: tableName, + description: `Schema from ${tableName} and ${this.options.sampleRows} sample rows.`, + }); + contextItems.push({ + id: ALL_TABLES, + title: "All tables", + description: `Schema from all tables and ${this.options.sampleRows} sample rows each.`, + }); + } + + return contextItems; + } catch (error) { + throw new Error(`Failed to query PostgreSQL database: ${error}`); + } + } +} + +export default PostgresContextProvider; diff --git a/core/context/providers/index.ts b/core/context/providers/index.ts index e335a8363..eff3c3b62 100644 --- a/core/context/providers/index.ts +++ b/core/context/providers/index.ts @@ -15,6 +15,7 @@ import ProblemsContextProvider from "./ProblemsContextProvider"; import SearchContextProvider from "./SearchContextProvider"; import TerminalContextProvider from "./TerminalContextProvider"; import URLContextProvider from "./URLContextProvider"; +import PostgresContextProvider from "./PostgresContextProvider"; const Providers: (typeof BaseContextProvider)[] = [ DiffContextProvider, @@ -32,6 +33,7 @@ const Providers: (typeof BaseContextProvider)[] = [ DocsContextProvider, CodeHighlightsContextProvider, CodeOutlineContextProvider, + PostgresContextProvider, ]; export function contextProviderClassFromName( diff --git a/core/index.d.ts b/core/index.d.ts index 1a1407d52..bb6f6be94 100644 --- a/core/index.d.ts +++ b/core/index.d.ts @@ -414,7 +414,8 @@ type ContextProviderName = | "http" | "codebase" | "problems" - | "folder"; + | "folder" + | "postgres"; type TemplateType = | "llama2"