Elffuss BraidHub

kikocis/continue public

main · 3.7 KiBRawPermalink
1import { getDatabaseAdapter } from "dbinfoz";
2
3import {
4 ContextItem,
5 ContextProviderDescription,
6 ContextProviderExtras,
7 ContextSubmenuItem,
8 LoadSubmenuItemsArgs,
9} from "../../index.js";
10import { BaseContextProvider } from "../index.js";
11
12class DatabaseContextProvider extends BaseContextProvider {
13 static description: ContextProviderDescription = {
14 title: "database",
15 displayTitle: "Database",
16 description: "Table schemas",
17 type: "submenu",
18 renderInlineAs: "",
19 };
20
21 get deprecationMessage() {
22 return "The database context provider is deprecated and may be removed in a later version. Please consider using a database MCP server like postgres-mcp (https://hub.continue.dev/anthropic/postgres-mcp) instead.";
23 }
24
25 async getContextItems(
26 query: string,
27 extras: ContextProviderExtras,
28 ): Promise<ContextItem[]> {
29 const contextItems: ContextItem[] = [];
30
31 const connections = this.options?.connections;
32
33 if (connections === null) {
34 return contextItems;
35 }
36
37 const [connectionName, table] = query.split(".");
38
39 for (const connection of connections) {
40 if (connection.name === connectionName) {
41 // @ts-ignore (incorrect typings on module's declaration file)
42 const adapter = getDatabaseAdapter(
43 connection.connection_type,
44 connection.connection,
45 );
46 const tablesAndSchemas = await adapter.getAllTablesAndSchemas(
47 connection.connection.database,
48 );
49
50 if (table === "all") {
51 let prompt = `Schema for all tables on ${connection.connection_type} is `;
52 prompt += JSON.stringify(tablesAndSchemas);
53
54 const contextItem = {
55 name: `${connectionName}-all-tables-schemas`,
56 description: "Schema for all tables.",
57 content: prompt,
58 };
59
60 contextItems.push(contextItem);
61 } else {
62 const tables = Object.keys(tablesAndSchemas);
63
64 tables.forEach((tableName) => {
65 if (table === tableName) {
66 let prompt = `Schema for ${tableName} on ${connection.connection_type} is `;
67 prompt += JSON.stringify(tablesAndSchemas[tableName]);
68
69 const contextItem = {
70 name: `${connectionName}-${tableName}-schema`,
71 description: `${tableName} Schema`,
72 content: prompt,
73 };
74
75 contextItems.push(contextItem);
76 }
77 });
78 }
79 }
80 }
81
82 return contextItems;
83 }
84
85 async loadSubmenuItems(
86 args: LoadSubmenuItemsArgs,
87 ): Promise<ContextSubmenuItem[]> {
88 const contextItems: ContextSubmenuItem[] = [];
89 const connections = this.options?.connections;
90
91 if (connections === null) {
92 return contextItems;
93 }
94
95 for (const connection of connections) {
96 // @ts-ignore (incorrect typings on module's declaration file)
97 const adapter = getDatabaseAdapter(
98 connection.connection_type,
99 connection.connection,
100 );
101 const tablesAndSchemas = await adapter.getAllTablesAndSchemas(
102 connection.connection.database,
103 );
104 const tables = Object.keys(tablesAndSchemas);
105
106 const contextItem = {
107 id: `${connection.name}.all`,
108 title: `${connection.name} all table schemas`,
109 description: "",
110 };
111
112 contextItems.push(contextItem);
113
114 tables.forEach((tableName) => {
115 const contextItem = {
116 id: `${connection.name}.${tableName}`,
117 title: `${connection.name}.${tableName} schema`,
118 description: "",
119 };
120
121 contextItems.push(contextItem);
122 });
123 }
124
125 return contextItems;
126 }
127}
128
129export default DatabaseContextProvider;