Skip to content

Commit 2101878

Browse files
authored
Merge branch 'codefori:main' into fix/token_tests
2 parents 1230643 + e3d4614 commit 2101878

File tree

4 files changed

+55
-107
lines changed

4 files changed

+55
-107
lines changed

src/aiProviders/context.ts

Lines changed: 29 additions & 82 deletions
Original file line numberDiff line numberDiff line change
@@ -14,73 +14,31 @@ export function getCurrentSchema(): string {
1414
};
1515

1616
export type TableRefs = { [key: string]: TableColumn[] };
17-
18-
export async function getTableMetaData(schema: string, tableName: string): Promise<TableColumn[]> {
19-
const objectFindStatement = [
20-
`SELECT `,
21-
` column.TABLE_SCHEMA,`,
22-
` column.TABLE_NAME,`,
23-
` column.COLUMN_NAME,`,
24-
` key.CONSTRAINT_NAME,`,
25-
` column.DATA_TYPE, `,
26-
` column.CHARACTER_MAXIMUM_LENGTH,`,
27-
` column.NUMERIC_SCALE, `,
28-
` column.NUMERIC_PRECISION,`,
29-
` column.IS_NULLABLE, `,
30-
// ` column.HAS_DEFAULT, `,
31-
// ` column.COLUMN_DEFAULT, `,
32-
` column.COLUMN_TEXT, `,
33-
` column.IS_IDENTITY`,
34-
`FROM QSYS2.SYSCOLUMNS2 as column`,
35-
`LEFT JOIN QSYS2.syskeycst as key`,
36-
` on `,
37-
` column.table_schema = key.table_schema and`,
38-
` column.table_name = key.table_name and`,
39-
` column.column_name = key.column_name`,
40-
`WHERE column.TABLE_SCHEMA = '${Statement.delimName(schema, true)}'`,
41-
`AND column.TABLE_NAME = '${Statement.delimName(tableName, true)}'`,
42-
`ORDER BY column.ORDINAL_POSITION`,
43-
].join(` `);
44-
45-
return await JobManager.runSQL(objectFindStatement);
46-
}
47-
48-
export async function parsePromptForRefs(stream: vscode.ChatResponseStream, prompt: string[]): Promise<TableRefs> {
49-
const tables: TableRefs = {};
50-
for (const word of prompt) {
51-
const [schema, table] = word.split(`.`);
52-
const cleanedTable = table.replace(/[,\/#!?$%\^&\*;:{}=\-_`~()]/g, "");
53-
if (schema && cleanedTable) {
54-
if (stream !== null) {
55-
stream.progress(`looking up information for ${schema}.${cleanedTable}`)
56-
}
57-
const data = await getTableMetaData(schema, cleanedTable);
58-
tables[cleanedTable] = tables[cleanedTable] || [];
59-
tables[cleanedTable].push(...data);
60-
}
61-
}
62-
return tables;
17+
interface MarkdownRef {
18+
TABLE_NAME: string,
19+
COLUMN_INFO?: string,
20+
SCHMEA?: string,
6321
}
6422

6523
export async function findPossibleTables(stream: vscode.ChatResponseStream, schema: string, words: string[]) {
6624

6725
let tables: TableRefs = {}
6826

69-
// parse all SCHEMA.TABLE references first
70-
tables = await parsePromptForRefs(stream, words.filter(word => word.includes('.')));
71-
27+
// Parse all SCHEMA.TABLE references first
28+
const schemaTableRefs = words.filter(word => word.includes('.'));
7229
const justWords = words.map(word => word.replace(/[,\/#!?$%\^&\*;:{}=\-_`~()]/g, ""));
7330

7431
// Remove plurals from words
7532
justWords.push(...justWords.filter(word => word.endsWith('s')).map(word => word.slice(0, -1)));
7633

77-
// filter prompt for possible refs to tables
34+
// Filter prompt for possible refs to tables
7835
const validWords = justWords
7936
.filter(word => word.length > 2 && !word.endsWith('s') && !word.includes(`'`))
8037
.map(word => `'${Statement.delimName(word, true)}'`);
8138

8239
const objectFindStatement = [
8340
`SELECT `,
41+
` column.TABLE_SCHEMA,`,
8442
` column.TABLE_NAME,`,
8543
` column.COLUMN_NAME,`,
8644
` key.CONSTRAINT_NAME,`,
@@ -99,15 +57,18 @@ export async function findPossibleTables(stream: vscode.ChatResponseStream, sche
9957
` column.table_schema = key.table_schema and`,
10058
` column.table_name = key.table_name and`,
10159
` column.column_name = key.column_name`,
102-
`WHERE column.TABLE_SCHEMA = '${schema}'`,
60+
`WHERE column.TABLE_SCHEMA = '${Statement.delimName(schema, true)}'`,
10361
...[
104-
words.length > 0
105-
? `AND column.TABLE_NAME in (${validWords.join(`, `)})`
106-
: ``,
62+
schemaTableRefs.length > 0
63+
? `AND (column.TABLE_NAME in (${validWords.join(`, `)}) OR (${schemaTableRefs.map(ref => {
64+
const [schema, table] = ref.split('.');
65+
const cleanedTable = table.replace(/[,\/#!?$%\^&\*;:{}=\-_`~()]/g, "");
66+
return `(column.TABLE_SCHEMA = '${Statement.delimName(schema, true)}' AND column.TABLE_NAME = '${Statement.delimName(cleanedTable, true)}')`;
67+
}).join(' OR ')}))`
68+
: `AND column.TABLE_NAME in (${validWords.join(`, `)})`,
10769
],
10870
`ORDER BY column.ORDINAL_POSITION`,
10971
].join(` `);
110-
11172
// TODO
11273
const result: TableColumn[] = await JobManager.runSQL(objectFindStatement);
11374

@@ -141,38 +102,24 @@ export async function findPossibleTables(stream: vscode.ChatResponseStream, sche
141102
export function refsToMarkdown(refs: TableRefs) {
142103
const condensedResult = Object.keys(refs).length > 5;
143104

144-
let markdown: string[] = [];
145-
105+
let markdownRefs: MarkdownRef[] = [];
146106
for (const tableName in refs) {
147107
if (tableName.startsWith(`SYS`)) continue;
148108

149-
markdown.push(`# ${tableName}`, ``);
150-
151-
if (condensedResult) {
152-
markdown.push(`| Column | Type | Text |`);
153-
markdown.push(`| - | - | - |`);
154-
} else {
155-
markdown.push(
156-
`| Column | Type | Nullable | Identity | Text | Constraint |`
157-
);
158-
markdown.push(`| - | - | - | - | - | - |`);
159-
}
160-
for (const column of refs[tableName]) {
161-
if (condensedResult) {
162-
markdown.push(
163-
`| ${column.COLUMN_NAME} | ${column.DATA_TYPE} | ${column.COLUMN_TEXT} |`
164-
);
165-
} else {
166-
markdown.push(
167-
`| ${column.COLUMN_NAME} | ${column.DATA_TYPE} | ${column.IS_NULLABLE} | ${column.IS_IDENTITY} | ${column.COLUMN_TEXT} | ${column.CONSTRAINT_NAME} |`
168-
);
169-
}
170-
}
171-
172-
markdown.push(``);
109+
const curRef: MarkdownRef = {
110+
TABLE_NAME: tableName,
111+
SCHMEA: refs[tableName][0].TABLE_SCHEMA,
112+
COLUMN_INFO: refs[tableName].map(column => {
113+
const lengthPrecision = column.CHARACTER_MAXIMUM_LENGTH
114+
? `(${column.CHARACTER_MAXIMUM_LENGTH}${column.NUMERIC_PRECISION ? `:${column.NUMERIC_PRECISION}` : ``})`
115+
: ``;
116+
return `${column.COLUMN_NAME}${column.COLUMN_TEXT ? ` - ${column.COLUMN_TEXT}` : ``} ${column.DATA_TYPE}${lengthPrecision} is_identity: ${column.IS_IDENTITY} is_nullable: ${column.IS_NULLABLE}`;
117+
}).join(`\n`)
118+
};
119+
markdownRefs.push(curRef);
173120
}
174121

175-
return markdown.join(`\n`);
122+
return markdownRefs;
176123
}
177124

178125
export async function getSystemStatus(): Promise<string> {

src/aiProviders/continue/continueContextProvider.ts

Lines changed: 13 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import * as vscode from "vscode";
22
import { JobManager } from "../../config";
33
import { JobInfo } from "../../connection/manager";
44
import { SelfCodeNode } from "../../views/jobManager/selfCodes/nodes";
5-
import { canTalkToDb, findPossibleTables } from "../context";
5+
import { canTalkToDb, findPossibleTables, refsToMarkdown } from "../context";
66
import {
77
ContextItem,
88
ContextProviderDescription,
@@ -135,7 +135,7 @@ export class db2ContextProvider implements IContextProvider {
135135
content: prompt,
136136
});
137137
}
138-
138+
139139
return contextItems;
140140
default:
141141
// const contextItems: ContextItem[] = [];
@@ -144,24 +144,19 @@ export class db2ContextProvider implements IContextProvider {
144144
schema,
145145
fullInput.split(` `)
146146
);
147-
for (const table of Object.keys(tableRefs)) {
148-
const columnData: TableColumn[] = tableRefs[table];
149-
if (columnData && columnData.length > 0) {
150-
const tableSchema =
151-
columnData.length > 0 ? columnData[0].TABLE_SCHEMA : null;
147+
const markdownRefs = refsToMarkdown(tableRefs);
152148

153-
// create context item
154-
let prompt = `Db2 for i Table meta data for schema ${tableSchema} table ${table}\n`;
155-
prompt += `Column Info: ${JSON.stringify(columnData)}\n\n`;
156-
157-
contextItems.push({
158-
name: `${job.name}-${tableSchema}-${table}`,
159-
description: `Schema and table information for ${table}`,
160-
content: prompt,
161-
});
162-
}
149+
for (const tableRef of markdownRefs) {
150+
let prompt = `Table: ${tableRef.TABLE_NAME} (Schema: ${tableRef.SCHMEA}) Column Information:\n`;
151+
prompt += `Format: column_name (column_text) type(length:precision) is_identity is_nullable\n`
152+
prompt += `${tableRef.COLUMN_INFO}`;
153+
contextItems.push({
154+
name: `${job.name}-${tableRef.SCHMEA}-${tableRef.TABLE_NAME}`,
155+
description: `Column information for ${tableRef.TABLE_NAME}`,
156+
content: prompt,
157+
});
163158
}
164-
159+
165160
return contextItems;
166161
}
167162
} catch (error) {

src/aiProviders/copilot/index.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import {
55
findPossibleTables,
66
getCurrentSchema,
77
getSystemStatus,
8+
refsToMarkdown,
89
} from "../context";
910
import { JobManager } from "../../config";
1011

@@ -89,6 +90,8 @@ export function activateChat(context: vscode.ExtensionContext) {
8990
request.prompt.split(` `)
9091
);
9192

93+
const markdownRefs = refsToMarkdown(refs);
94+
9295
messages = [
9396
vscode.LanguageModelChatMessage.Assistant(
9497
`You are a an IBM i savant speciallizing in database features in Db2 for i. Your job is to help developers write and debug their SQL along with offering SQL programming advice.`
@@ -118,11 +121,14 @@ export function activateChat(context: vscode.ExtensionContext) {
118121
}
119122

120123
if (Object.keys(refs).length > 0) {
121-
messages.push(
122-
vscode.LanguageModelChatMessage.Assistant(
123-
`Here are new table references ${JSON.stringify(refs)}`
124-
),
125-
);
124+
for (const tableRef of markdownRefs) {
125+
messages.push(
126+
vscode.LanguageModelChatMessage.Assistant(
127+
`Here are new table references for ${tableRef.TABLE_NAME} (Schema: ${tableRef.SCHMEA})\nFormat: column_name (column_text) type(length:precision) is_identity is_nullable\n${tableRef.COLUMN_INFO}`
128+
),
129+
);
130+
131+
}
126132
}
127133

128134
stream.progress(`Building response...`);

src/connection/manager.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ export class SQLJobManager {
3030
const config = instance.getConfig();
3131

3232
const newJob = predefinedJob || (new OldSQLJob({
33-
libraries: [config.currentLibrary, ...config.libraryList],
33+
libraries: [config.currentLibrary, ...config.libraryList.filter((item) => item != config.currentLibrary)],
3434
naming: `system`,
3535
"full open": false,
3636
"transaction isolation": "none",
@@ -161,4 +161,4 @@ export class SQLJobManager {
161161
static getSelfDefault(): SelfValue {
162162
return Configuration.get<SelfValue>(`jobSelfDefault`) || `*NONE`;
163163
}
164-
}
164+
}

0 commit comments

Comments
 (0)