-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #305 from codefori/fix/continue-context
- Loading branch information
Showing
6 changed files
with
201 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
import { | ||
ContextItem, | ||
ContextProviderDescription, | ||
ContextProviderExtras, | ||
ContextSubmenuItem, | ||
IContextProvider, | ||
LoadSubmenuItemsArgs, | ||
} from "@continuedev/core"; | ||
import * as fs from "fs"; | ||
import * as os from "os"; | ||
import * as path from "path"; | ||
import * as vscode from "vscode"; | ||
import Schemas from "../../database/schemas"; | ||
import Table from "../../database/table"; | ||
import { | ||
createContinueContextItems, | ||
findPossibleTables, | ||
refsToMarkdown, | ||
} from "../context"; | ||
|
||
const listDb2Table: ContextProviderDescription = { | ||
title: "list Db2i Tables", | ||
displayTitle: "Db2i-tables", | ||
description: "Add Db2i Table info to Context", | ||
type: "submenu", | ||
}; | ||
|
||
export let provider: ListDb2iTables = undefined; | ||
|
||
class ListDb2iTables implements IContextProvider { | ||
constructor(private schema: string) { | ||
this.schema = schema; | ||
} | ||
|
||
get description(): ContextProviderDescription { | ||
return listDb2Table; | ||
} | ||
|
||
setCurrentSchema(schema: string) { | ||
this.schema = schema; | ||
} | ||
|
||
getCurrentSchema() { | ||
return this.schema; | ||
} | ||
|
||
async getColumnInfoForAllTables(schema: string) { | ||
const items: TableColumn[] = await Table.getItems(schema); | ||
|
||
return items.map((column) => ({ | ||
table_name: column.TABLE_NAME, | ||
schema: column.TABLE_SCHEMA, | ||
column_name: column.COLUMN_NAME, | ||
column_data_type: column.DATA_TYPE, | ||
})); | ||
} | ||
|
||
async getContextItems( | ||
query: string, | ||
extras: ContextProviderExtras | ||
): Promise<ContextItem[]> { | ||
let contextItems: ContextItem[] = []; | ||
if (query.toUpperCase() === this.schema.toUpperCase()) { | ||
const tableInfo = await this.getColumnInfoForAllTables(this.schema); | ||
contextItems.push({ | ||
name: `Info for all tables in ${this.schema}`, | ||
content: `Db2 for i table Assistant: The following table and column information is from the ${query} schema. Utilize the provided schema and table metadata to assist the user:\n${JSON.stringify( | ||
tableInfo | ||
)}`, | ||
description: "table metadata", | ||
}); | ||
} else { | ||
const tableInfo = await findPossibleTables( | ||
null, | ||
this.schema, | ||
query.split(` `) | ||
); | ||
const markdownRefs = refsToMarkdown(tableInfo); | ||
|
||
// add additional context for working with Db2 for i tables | ||
contextItems.push({ | ||
name: `Instructions`, | ||
content: `Db2 for i table Assistant: The following information is based on the ${query} table within the ${this.schema} schema. Utilize the provided schema and table metadata to assist the user. Only use valid Db2 for i SQL syntax and conventions. If input is unclear ask user to clarify`, | ||
description: "instructions for working with Db2 for i tables", | ||
}); | ||
|
||
contextItems.push(...createContinueContextItems(markdownRefs)); | ||
} | ||
return contextItems; | ||
} | ||
|
||
async loadSubmenuItems( | ||
args: LoadSubmenuItemsArgs | ||
): Promise<ContextSubmenuItem[]> { | ||
const tables: BasicSQLObject[] = await Schemas.getObjects(this.schema, [ | ||
`tables`, | ||
]); | ||
|
||
const schemaSubmenuItem: ContextSubmenuItem = { | ||
id: this.schema, | ||
title: this.schema, | ||
description: `All table info in schema: ${this.schema}`, | ||
}; | ||
|
||
const tableSubmenuItems: ContextSubmenuItem[] = tables.map((table) => ({ | ||
id: table.name, | ||
title: table.name, | ||
description: `${table.schema}-${table.name}`, | ||
})); | ||
|
||
return [schemaSubmenuItem, ...tableSubmenuItems]; | ||
} | ||
} | ||
|
||
export async function registerDb2iTablesProvider(schema?: string) { | ||
if (!schema) { | ||
return; | ||
} | ||
const continueID = `Continue.continue`; | ||
const continueEx = vscode.extensions.getExtension(continueID); | ||
if (continueEx) { | ||
if (!continueEx.isActive) { | ||
await continueEx.activate(); | ||
} | ||
|
||
if (provider) { | ||
provider.setCurrentSchema(schema); | ||
// save continue config file to trigger a config reload to update list tables provider | ||
const configFile = path.join(os.homedir(), `.continue`, `config.json`); | ||
const now = new Date(); | ||
fs.utimes(configFile, now, now, (err) => { | ||
if (err) { | ||
console.error("Error saving Continue config file:", err); | ||
return; | ||
} | ||
}); | ||
} else { | ||
const continueAPI = continueEx?.exports; | ||
provider = new ListDb2iTables(schema); | ||
continueAPI?.registerCustomContextProvider(provider); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters