Skip to content

Commit 4fa956f

Browse files
committed
use same context backend as chatai
1 parent 38d26c2 commit 4fa956f

File tree

5 files changed

+32
-153
lines changed

5 files changed

+32
-153
lines changed

package-lock.json

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1090,6 +1090,11 @@
10901090
"command": "vscode-db2i.self.displayDetails",
10911091
"when": "view == vscode-db2i.self.nodes && viewItem == selfCodeNode",
10921092
"group": "navigation"
1093+
},
1094+
{
1095+
"command": "vscode-db2i.self.explainSelf",
1096+
"when": "view == vscode-db2i.self.nodes && viewItem == selfCodeNode",
1097+
"group": "navigation"
10931098
}
10941099
],
10951100
"editor/title": [
Lines changed: 10 additions & 150 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import * as vscode from "vscode";
22
import { JobManager } from "../../config";
3-
import { JobInfo, SQLJobManager } from "../../connection/manager";
3+
import { JobInfo } from "../../connection/manager";
44
import Statement from "../../database/statement";
5-
import { table } from "console";
6-
import { selfCodesResultsView } from "../../views/jobManager/selfCodes/selfCodesResultsView";
75
import { SelfCodeNode } from "../../views/jobManager/selfCodes/nodes";
8-
import { SQLJob } from "../../connection/sqlJob";
6+
import { findPossibleTables } from "../../chat/context";
7+
import { ContextItem, ContextProviderDescription, ContextProviderExtras, ContextSubmenuItem, IContextProvider, LoadSubmenuItemsArgs } from "../../..";
98

109
const db2ContextProviderDesc: ContextProviderDescription = {
1110
title: "db2i",
@@ -14,8 +13,6 @@ const db2ContextProviderDesc: ContextProviderDescription = {
1413
type: "normal",
1514
};
1615

17-
type TableRefs = { [key: string]: TableColumn[]}
18-
1916
/**
2017
* - Get Existing Db2 connection from vscode
2118
* - Use connection to build query for database to add additional context
@@ -36,114 +33,6 @@ export class db2ContextProvider implements IContextProvider {
3633
return currentJob?.job.options.libraries[0] || "QGPL";
3734
};
3835

39-
async findPossibleTables(schema: string, words: string[]) {
40-
// let refSchema: string = "";
41-
// let table: string = "";
42-
// words.forEach(item => {
43-
// if (item.includes(`.`)) {
44-
// refSchema = item.split(`.`)[0];
45-
// table = item.split(`.`)[1];
46-
// }
47-
// })
48-
49-
words = words.map((word) =>
50-
word.replace(/[.,\/#!?$%\^&\*;:{}=\-_`~()]/g, "")
51-
);
52-
53-
// Add extra words for words with S at the end, to ignore possible plurals
54-
words.forEach((item) => {
55-
if (item.endsWith(`s`)) {
56-
words.push(item.slice(0, -1));
57-
}
58-
});
59-
60-
const validWords = words
61-
.filter((item) => item.length > 2 && !item.includes(`'`))
62-
.map((item) => `'${Statement.delimName(item, true)}'`);
63-
64-
const objectFindStatement = [
65-
`SELECT `,
66-
` column.TABLE_NAME,`,
67-
` column.COLUMN_NAME,`,
68-
` key.CONSTRAINT_NAME,`,
69-
` column.DATA_TYPE, `,
70-
` column.CHARACTER_MAXIMUM_LENGTH,`,
71-
` column.NUMERIC_SCALE, `,
72-
` column.NUMERIC_PRECISION,`,
73-
` column.IS_NULLABLE, `,
74-
// ` column.HAS_DEFAULT, `,
75-
// ` column.COLUMN_DEFAULT, `,
76-
` column.COLUMN_TEXT, `,
77-
` column.IS_IDENTITY`,
78-
`FROM QSYS2.SYSCOLUMNS2 as column`,
79-
`LEFT JOIN QSYS2.syskeycst as key`,
80-
` on `,
81-
` column.table_schema = key.table_schema and`,
82-
` column.table_name = key.table_name and`,
83-
` column.column_name = key.column_name`,
84-
`WHERE column.TABLE_SCHEMA = '${schema}'`,
85-
...[
86-
words.length > 0
87-
? `AND column.TABLE_NAME in (${validWords.join(`, `)})`
88-
: ``,
89-
],
90-
`ORDER BY column.ORDINAL_POSITION`,
91-
].join(` `);
92-
93-
// TODO
94-
const result: TableColumn[] = await JobManager.runSQL(objectFindStatement);
95-
96-
const tables: TableRefs = {};
97-
98-
for (const row of result) {
99-
if (!tables[row.TABLE_NAME]) {
100-
tables[row.TABLE_NAME] = [];
101-
}
102-
103-
tables[row.TABLE_NAME].push(row);
104-
}
105-
106-
return tables;
107-
}
108-
109-
async findAllTables(schema: string) {
110-
const objectFindStatement = [
111-
`SELECT `,
112-
` column.TABLE_NAME,`,
113-
` column.COLUMN_NAME,`,
114-
` key.CONSTRAINT_NAME,`,
115-
` column.DATA_TYPE, `,
116-
` column.CHARACTER_MAXIMUM_LENGTH,`,
117-
` column.NUMERIC_SCALE, `,
118-
` column.NUMERIC_PRECISION,`,
119-
` column.IS_NULLABLE, `,
120-
// ` column.HAS_DEFAULT, `,
121-
// ` column.COLUMN_DEFAULT, `,
122-
` column.COLUMN_TEXT, `,
123-
` column.IS_IDENTITY`,
124-
`FROM QSYS2.SYSCOLUMNS2 as column`,
125-
`LEFT JOIN QSYS2.syskeycst as key`,
126-
` on `,
127-
` column.table_schema = key.table_schema and`,
128-
` column.table_name = key.table_name and`,
129-
` column.column_name = key.column_name`,
130-
`WHERE column.TABLE_SCHEMA = '${schema}'`,
131-
].join(` `);
132-
133-
const result: TableColumn[] = await JobManager.runSQL(objectFindStatement);
134-
135-
const tables: TableRefs = {};
136-
137-
for (const row of result) {
138-
if (!tables[row.TABLE_NAME]) {
139-
tables[row.TABLE_NAME] = [];
140-
}
141-
142-
tables[row.TABLE_NAME].push(row);
143-
}
144-
145-
return tables;
146-
}
14736
async getSelfCodes(selected: JobInfo): Promise<SelfCodeNode[]|undefined> {
14837
const current_job = selected.job.id;
14938
const content = `SELECT
@@ -154,7 +43,7 @@ export class db2ContextProvider implements IContextProvider {
15443
order by logged_time desc`;
15544

15645
try {
157-
const result = await selected.job.query<SelfCodeNode>(content).run(10000);
46+
const result = await selected.job.query<SelfCodeNode>(content).execute(10000);
15847
if (result.success) {
15948
const data: SelfCodeNode[] = result.data.map((row) => ({
16049
...row,
@@ -202,19 +91,21 @@ export class db2ContextProvider implements IContextProvider {
20291
return contextItems;
20392
default:
20493
// const contextItems: ContextItem[] = [];
205-
const tableRefs = await this.findPossibleTables(
94+
const tableRefs = await findPossibleTables(
95+
null,
20696
schema,
20797
fullInput.split(` `)
20898
);
20999
for (const table of Object.keys(tableRefs)) {
210100
const columnData: TableColumn[] = tableRefs[table];
101+
const tableSchema = columnData.length > 0 ? columnData[0].TABLE_SCHEMA : null;
211102

212103
// create context item
213-
let prompt = `Db2 for i schema ${schema} table ${table}\n`;
104+
let prompt = `Db2 for i schema ${tableSchema} table ${table}\n`;
214105
prompt += `Column Info: ${JSON.stringify(columnData)}\n\n`;
215106

216107
contextItems.push({
217-
name: `${job.name}-${schema}-${table}`,
108+
name: `${job.name}-${tableSchema}-${table}`,
218109
description: `Schema and table information or ${table}`,
219110
content: prompt,
220111
});
@@ -255,41 +146,10 @@ export class db2ContextProvider implements IContextProvider {
255146
}
256147
}
257148

258-
class MyCustomProvider implements IContextProvider {
259-
260-
get description(): ContextProviderDescription {
261-
return {
262-
title: "custom",
263-
displayTitle: "Custom",
264-
description: "Custom description",
265-
type: "normal",
266-
};
267-
}
268-
269-
async getContextItems(
270-
query: string,
271-
extras: ContextProviderExtras
272-
): Promise<ContextItem[]> {
273-
return [
274-
{
275-
name: "Custom",
276-
description: "Custom description",
277-
content: "Custom content",
278-
},
279-
];
280-
}
281-
282-
async loadSubmenuItems(
283-
args: LoadSubmenuItemsArgs
284-
): Promise<ContextSubmenuItem[]> {
285-
return [];
286-
}
287-
}
288-
289149
export async function registerContinueProvider() {
290150
const provider = new db2ContextProvider();
291151
const continueID = `Continue.continue`;
292152
const continueEx = vscode.extensions.getExtension(continueID);
293153
const continueAPI = continueEx?.exports;
294154
continueAPI?.registerCustomContextProvider(provider);
295-
}
155+
}

src/chat/context.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export type TableRefs = { [key: string]: TableColumn[] };
1818
export async function getTableMetaData(schema: string, tableName: string): Promise<TableColumn[]> {
1919
const objectFindStatement = [
2020
`SELECT `,
21+
` column.TABLE_SCHEMA,`,
2122
` column.TABLE_NAME,`,
2223
` column.COLUMN_NAME,`,
2324
` key.CONSTRAINT_NAME,`,
@@ -50,7 +51,9 @@ export async function parsePromptForRefs(stream: vscode.ChatResponseStream, prom
5051
const [schema, table] = word.split(`.`);
5152
const cleanedTable = table.replace(/[,\/#!?$%\^&\*;:{}=\-_`~()]/g, "");
5253
if (schema && cleanedTable) {
53-
stream.progress(`looking up information for ${schema}.${cleanedTable}`)
54+
if (stream !== null) {
55+
stream.progress(`looking up information for ${schema}.${cleanedTable}`)
56+
}
5457
const data = await getTableMetaData(schema, cleanedTable);
5558
tables[cleanedTable] = tables[cleanedTable] || [];
5659
tables[cleanedTable].push(...data);

src/views/jobManager/selfCodes/contributes.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,12 @@
9292
"category": "Db2 for i",
9393
"icon": "$(info)"
9494
},
95+
{
96+
"command": "vscode-db2i.self.explainSelf",
97+
"title": "Explain SELF Code with continue",
98+
"category": "Db2 for i",
99+
"icon": "$(debug-alt)"
100+
},
95101
{
96102
"command": "vscode-db2i.self.help",
97103
"title": "Open SELF Documentation",
@@ -147,6 +153,11 @@
147153
"command": "vscode-db2i.self.displayDetails",
148154
"when": "view == vscode-db2i.self.nodes && viewItem == selfCodeNode",
149155
"group": "navigation"
156+
},
157+
{
158+
"command": "vscode-db2i.self.explainSelf",
159+
"when": "view == vscode-db2i.self.nodes && viewItem == selfCodeNode",
160+
"group": "navigation"
150161
}
151162
]
152163
}

0 commit comments

Comments
 (0)