Skip to content

Commit 765e927

Browse files
committed
Fetch schema info when referenced
Signed-off-by: worksofliam <[email protected]>
1 parent 8392a2e commit 765e927

File tree

3 files changed

+38
-25
lines changed

3 files changed

+38
-25
lines changed

src/aiProviders/context.ts

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,15 @@ export async function getContentItemsForRefs(allObjects: ResolvedSqlObject[]): P
209209
allObjects.map(async (o) => {
210210
try {
211211
if (o.sqlType === `SCHEMA`) {
212-
// TODO: maybe we want to include info about a schema here?
212+
const schemaSemantic = await buildSchemaDefinition(o.name);
213+
if (schemaSemantic) {
214+
return {
215+
name: `SCHEMA Definition`,
216+
description: `${o.name} definition`,
217+
content: JSON.stringify(schemaSemantic)
218+
};
219+
}
220+
213221
return undefined;
214222

215223
} else {

src/aiProviders/prompt.ts

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,21 +36,8 @@ export async function getContextItems(input: string, options: PromptOptions = {}
3636

3737
if (currentJob) {
3838
const currentSchema = currentJob?.job.options.libraries[0] || "QGPL";
39-
4039
const useSchemaDef: boolean = Configuration.get<boolean>(`ai.useSchemaDefinition`);
4140

42-
if (useSchemaDef) {
43-
progress(`Building schema definition for ${currentSchema}...`);
44-
const schemaSemantic = await buildSchemaDefinition(currentSchema);
45-
if (schemaSemantic) {
46-
contextItems.push({
47-
name: `SCHEMA Definition`,
48-
description: `${currentSchema} definition`,
49-
content: JSON.stringify(schemaSemantic)
50-
});
51-
}
52-
}
53-
5441
// TODO: self?
5542

5643
progress(`Finding objects to work with...`);
@@ -73,24 +60,42 @@ export async function getContextItems(input: string, options: PromptOptions = {}
7360
if (context.refs.length === 1) {
7461
const ref = context.refs[0];
7562
const prettyNameRef = Statement.prettyName(ref.name);
76-
progress(`Finding objects related to ${prettyNameRef}...`);
7763

78-
const relatedObjects = await Schemas.getRelatedObjects(ref);
79-
const contentItems = await getContentItemsForRefs(relatedObjects);
64+
if (ref.sqlType === `SCHEMA`) {
65+
followUps.push(
66+
`What are some objects in that schema?`,
67+
`What is the difference between a schema and a library?`,
68+
);
69+
} else {
70+
progress(`Finding objects related to ${prettyNameRef}...`);
71+
72+
const relatedObjects = await Schemas.getRelatedObjects(ref);
73+
const contentItems = await getContentItemsForRefs(relatedObjects);
8074

81-
contextItems.push(...contentItems);
75+
contextItems.push(...contentItems);
8276

83-
if (relatedObjects.length === 1) {
84-
followUps.push(`How is ${prettyNameRef} related to ${Statement.prettyName(relatedObjects[0].name)}?`);
85-
} else if (ref.sqlType === `TABLE`) {
86-
followUps.push(`What are some objects related to that table?`);
77+
if (relatedObjects.length === 1) {
78+
followUps.push(`How is ${prettyNameRef} related to ${Statement.prettyName(relatedObjects[0].name)}?`);
79+
} else if (ref.sqlType === `TABLE`) {
80+
followUps.push(`What are some objects related to that table?`);
81+
}
8782
}
8883

8984
} else if (context.refs.length > 1) {
9085
const randomRef = context.refs[Math.floor(Math.random() * context.refs.length)];
9186
const prettyNameRef = Statement.prettyName(randomRef.name);
9287

9388
followUps.push(`What are some objects related to ${prettyNameRef}?`);
89+
} else if (useSchemaDef) {
90+
progress(`Getting info for schema ${currentSchema}...`);
91+
const schemaSemantic = await buildSchemaDefinition(currentSchema);
92+
if (schemaSemantic) {
93+
contextItems.push({
94+
name: `SCHEMA Definition`,
95+
description: `${currentSchema} definition`,
96+
content: JSON.stringify(schemaSemantic)
97+
});
98+
}
9499
}
95100

96101
if (options.withDb2Prompt) {

src/database/schemas.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,12 @@ export default class Schemas {
7171
for (const obj of sqlObjects) {
7272
if (obj.schema) {
7373
statements.push(
74-
`${BASE_RESOLVE_SELECT} from table(qsys2.object_statistics(?, '*ALL', object_name => ?)) where SQL_OBJECT_TYPE IS NOT NULL`
74+
`${BASE_RESOLVE_SELECT} from table(qsys2.object_statistics(?, '*ALL', object_name => ?))`
7575
);
7676
parameters.push(obj.schema, obj.name);
7777
} else {
7878
statements.push(
79-
`${BASE_RESOLVE_SELECT} from table(qsys2.object_statistics('*LIBL', '*ALL', object_name => ?)) where SQL_OBJECT_TYPE IS NOT NULL`
79+
`${BASE_RESOLVE_SELECT} from table(qsys2.object_statistics('*LIBL', '*ALL', object_name => ?))`
8080
);
8181
parameters.push(obj.name);
8282
}
@@ -117,7 +117,7 @@ export default class Schemas {
117117
name: object.NAME,
118118
schema: object.SCHEMA,
119119
sqlType: object.SQLTYPE
120-
}));
120+
})).filter(o => o.sqlType);
121121

122122
return resolvedObjects;
123123
}

0 commit comments

Comments
 (0)