@@ -41,52 +41,64 @@ export async function getContextItems(input: string, options: PromptOptions = {}
41
41
// TODO: self?
42
42
43
43
progress ( `Finding objects to work with...` ) ;
44
- const context = await getSqlContextItems ( input ) ;
45
44
46
- contextItems . push ( ...context . items ) ;
45
+ // First, let's take the user input and see if contains any references to SQL objects.
46
+ // This returns a list of references to SQL objects, such as tables, views, schemas, etc,
47
+ // and the context items that are related to those references.
48
+ const userInput = await getSqlContextItems ( input ) ;
47
49
48
- if ( context . refs . filter ( r => r . sqlType === `TABLE` ) . length >= 2 ) {
49
- const randomIndexA = Math . floor ( Math . random ( ) * context . refs . length ) ;
50
- const randomIndexB = Math . floor ( Math . random ( ) * context . refs . length ) ;
51
- const tableA = context . refs [ randomIndexA ] . name ;
52
- const tableB = context . refs [ randomIndexB ] . name ;
50
+ contextItems . push ( ...userInput . items ) ;
51
+
52
+ // If the user referenced 2 or more tables, let's add a follow up
53
+ if ( userInput . refs . filter ( r => r . sqlType === `TABLE` ) . length >= 2 ) {
54
+ const randomIndexA = Math . floor ( Math . random ( ) * userInput . refs . length ) ;
55
+ const randomIndexB = Math . floor ( Math . random ( ) * userInput . refs . length ) ;
56
+ const tableA = userInput . refs [ randomIndexA ] . name ;
57
+ const tableB = userInput . refs [ randomIndexB ] . name ;
53
58
54
59
if ( tableA !== tableB ) {
55
60
followUps . push ( `How can I join ${ tableA } and ${ tableB } ?` ) ;
56
61
}
57
62
}
58
63
59
- // If the user only requests one reference, then let's find related objects
60
- if ( context . refs . length === 1 ) {
61
- const ref = context . refs [ 0 ] ;
64
+ // If the user only requests one reference, then let's do something
65
+ if ( userInput . refs . length === 1 ) {
66
+ const ref = userInput . refs [ 0 ] ;
62
67
const prettyNameRef = Statement . prettyName ( ref . name ) ;
63
68
64
69
if ( ref . sqlType === `SCHEMA` ) {
70
+ // If the only reference is a schema, let's just add follow ups
65
71
followUps . push (
66
72
`What are some objects in that schema?` ,
67
73
`What is the difference between a schema and a library?` ,
68
74
) ;
75
+
69
76
} else {
77
+ // If the user referenced a table, view, or other object, let's fetch related objects
70
78
progress ( `Finding objects related to ${ prettyNameRef } ...` ) ;
71
79
72
80
const relatedObjects = await Schemas . getRelatedObjects ( ref ) ;
73
81
const contentItems = await getContentItemsForRefs ( relatedObjects ) ;
74
82
75
83
contextItems . push ( ...contentItems ) ;
76
84
85
+ // Then also add some follow ups
77
86
if ( relatedObjects . length === 1 ) {
78
87
followUps . push ( `How is ${ prettyNameRef } related to ${ Statement . prettyName ( relatedObjects [ 0 ] . name ) } ?` ) ;
79
88
} else if ( ref . sqlType === `TABLE` ) {
80
89
followUps . push ( `What are some objects related to that table?` ) ;
81
90
}
82
91
}
83
92
84
- } else if ( context . refs . length > 1 ) {
85
- const randomRef = context . refs [ Math . floor ( Math . random ( ) * context . refs . length ) ] ;
93
+ } else if ( userInput . refs . length > 1 ) {
94
+ // If there are multiple references, let's just add a follow up
95
+ const randomRef = userInput . refs [ Math . floor ( Math . random ( ) * userInput . refs . length ) ] ;
86
96
const prettyNameRef = Statement . prettyName ( randomRef . name ) ;
87
97
88
98
followUps . push ( `What are some objects related to ${ prettyNameRef } ?` ) ;
99
+
89
100
} else if ( useSchemaDef ) {
101
+ // If the user didn't reference any objects, but we are using schema definitions, let's just add the schema definition
90
102
progress ( `Getting info for schema ${ currentSchema } ...` ) ;
91
103
const schemaSemantic = await buildSchemaDefinition ( currentSchema ) ;
92
104
if ( schemaSemantic ) {
0 commit comments