Skip to content

Commit f1f7265

Browse files
committed
Add configuration setting for schema definition context in Continue and Copilot
1 parent f0b13ec commit f1f7265

File tree

6 files changed

+57
-20
lines changed

6 files changed

+57
-20
lines changed

package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@
101101
}
102102
}
103103
},
104+
{
105+
"id": "vscode-db2i.ai",
106+
"title": "AI integrations",
107+
"properties": {
108+
"vscode-db2i.ai.useSchemaDefinition": {
109+
"type": "boolean",
110+
"description": "Provide Schema definition as additional context in Continue and Copilot",
111+
"default": true
112+
}
113+
}
114+
},
104115
{
105116
"id": "vscode-db2i.resultsets",
106117
"title": "Viewing Data",

src/aiProviders/context.ts

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { JobManager } from "../config";
44
import Schemas, { AllSQLTypes, SQLType } from "../database/schemas";
55
import Statement from "../database/statement";
66
import { DB2_SYSTEM_PROMPT } from "./continue/prompts";
7+
import Configuration from "../configuration";
78

89
export function canTalkToDb() {
910
return JobManager.getSelection() !== undefined;
@@ -36,7 +37,7 @@ interface ContextDefinition {
3637
* @param schema - The name of the database schema to process.
3738
* @returns A promise that resolves to an array of filtered objects representing the schema.
3839
*/
39-
export async function buildSchemaDefinition(schema: string) {
40+
export async function buildSchemaDefinition(schema: string): Promise<Partial<BasicSQLObject>[] | undefined> {
4041
/**
4142
* Cleans an object by removing properties with undefined, null, or empty string values.
4243
* If the value is an object (but not an array), it cleans that object as well.
@@ -108,8 +109,14 @@ export async function buildSchemaDefinition(schema: string) {
108109
return data.map(filterBasicSQLObject);
109110
}
110111

111-
const compressedData = filterBasicSQLObjects(allInfo);
112-
return compressedData;
112+
const useSchemaDef: boolean = Configuration.get<boolean>(`ai.useSchemaDefinition`);
113+
if (useSchemaDef) {
114+
const compressedData = filterBasicSQLObjects(allInfo);
115+
return compressedData;
116+
}
117+
118+
return undefined;
119+
113120
}
114121

115122
/**
@@ -150,8 +157,10 @@ export async function generateTableDefinition(schema: string, input: string[]) {
150157
`tables`,
151158
]);
152159

153-
const filteredTables = validWords.filter((word) =>
154-
allTables.some((table) => table.name == word)
160+
const filteredTables = Array.from(
161+
new Set(
162+
validWords.filter((word) => allTables.some((table) => table.name == word))
163+
)
155164
);
156165

157166
await Promise.all(

src/aiProviders/continue/continueContextProvider.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,13 @@ export class db2ContextProvider implements IContextProvider {
143143

144144
// 1. SCHEMA Definiton (semantic)
145145
const schemaSemantic = await buildSchemaDefinition(schema);
146-
contextItems.push({
147-
name: `SCHEMA Definition`,
148-
description: `${schema} definition`,
149-
content: JSON.stringify(schemaSemantic),
150-
});
146+
if (schemaSemantic) {
147+
contextItems.push({
148+
name: `SCHEMA Definition`,
149+
description: `${schema} definition`,
150+
content: JSON.stringify(schemaSemantic),
151+
});
152+
}
151153
try {
152154
switch (true) {
153155
case fullInput.includes(`*SELF`) || query?.includes(`*SELF`):

src/aiProviders/continue/listTablesContextProvider.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,13 @@ class ListDb2iTables implements IContextProvider {
7070
let contextItems: ContextItem[] = [];
7171
if (query.toUpperCase() === this.schema.toUpperCase()) {
7272
const schemaSemantic = await buildSchemaDefinition(this.schema);
73-
contextItems.push({
74-
name: `SCHEMA Definition`,
75-
description: `${this.schema} definition`,
76-
content: JSON.stringify(schemaSemantic),
77-
});
73+
if (schemaSemantic) {
74+
contextItems.push({
75+
name: `SCHEMA Definition`,
76+
description: `${this.schema} definition`,
77+
content: JSON.stringify(schemaSemantic),
78+
});
79+
}
7880
} else {
7981
const tablesRefs = await generateTableDefinition(
8082
this.schema,

src/aiProviders/copilot/index.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,13 @@ export function activateChat(context: vscode.ExtensionContext) {
6363
// 1. SCHEMA Definiton (semantic)
6464
let usingSchema = getCurrentSchema();
6565
const schemaSemantic = await buildSchemaDefinition(usingSchema);
66-
messages.push(
67-
vscode.LanguageModelChatMessage.Assistant(
68-
JSON.stringify(schemaSemantic)
69-
)
70-
);
66+
if (schemaSemantic) {
67+
messages.push(
68+
vscode.LanguageModelChatMessage.Assistant(
69+
JSON.stringify(schemaSemantic)
70+
)
71+
);
72+
}
7173

7274
switch (request.command) {
7375
case `activity`:

src/contributes.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,17 @@
5757
]
5858
}
5959
}
60+
},
61+
{
62+
"id": "vscode-db2i.ai",
63+
"title": "AI integrations",
64+
"properties": {
65+
"vscode-db2i.ai.useSchemaDefinition": {
66+
"type": "boolean",
67+
"description": "Provide Schema definition as additional context in Continue and Copilot",
68+
"default": true
69+
}
70+
}
6071
}
6172
],
6273
"viewsContainers": {

0 commit comments

Comments
 (0)