-
Notifications
You must be signed in to change notification settings - Fork 9
Add get package copilot tool #135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
05c2a10
add pkg copilot tool
eleanorjboyd 2ede747
redesign of how run works
eleanorjboyd ce019ae
add mocked copilot tools types
eleanorjboyd fe141ec
fix tests to be passing
eleanorjboyd cb8f358
rename call for tool
eleanorjboyd 243bfe1
enable vscode-editing
eleanorjboyd 1805b25
allow tool to be callable by vscode tools
eleanorjboyd d15354b
remove duplicate register
eleanorjboyd 5b21677
Merge branch 'main' into add-package-tool
eleanorjboyd ea9c649
add support for pkg versions
eleanorjboyd ee16cd7
update model description
eleanorjboyd ace5146
add description
eleanorjboyd File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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,4 @@ | ||
import * as vscode from 'vscode'; | ||
export function registerTools<T>(name: string, tool: vscode.LanguageModelTool<T>): vscode.Disposable { | ||
return vscode.lm.registerTool(name, tool); | ||
} |
This file contains hidden or 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,92 @@ | ||
import { | ||
CancellationToken, | ||
LanguageModelTextPart, | ||
LanguageModelTool, | ||
LanguageModelToolInvocationOptions, | ||
LanguageModelToolInvocationPrepareOptions, | ||
LanguageModelToolResult, | ||
PreparedToolInvocation, | ||
Uri, | ||
} from 'vscode'; | ||
import { PythonPackageGetterApi, PythonProjectEnvironmentApi } from './api'; | ||
import { createDeferred } from './common/utils/deferred'; | ||
|
||
export interface IGetActiveFile { | ||
filePath?: string; | ||
} | ||
|
||
/** | ||
* A tool to get the list of installed Python packages in the active environment. | ||
*/ | ||
export class GetPackagesTool implements LanguageModelTool<IGetActiveFile> { | ||
constructor(private readonly api: PythonProjectEnvironmentApi & PythonPackageGetterApi) {} | ||
/** | ||
* Invokes the tool to get the list of installed packages. | ||
* @param options - The invocation options containing the file path. | ||
* @param token - The cancellation token. | ||
* @returns The result containing the list of installed packages or an error message. | ||
*/ | ||
async invoke( | ||
options: LanguageModelToolInvocationOptions<IGetActiveFile>, | ||
token: CancellationToken, | ||
): Promise<LanguageModelToolResult> { | ||
const deferredReturn = createDeferred<LanguageModelToolResult>(); | ||
token.onCancellationRequested(() => { | ||
const errorMessage: string = `Operation cancelled by the user.`; | ||
deferredReturn.resolve({ content: [new LanguageModelTextPart(errorMessage)] } as LanguageModelToolResult); | ||
}); | ||
|
||
const parameters: IGetActiveFile = options.input; | ||
|
||
if (parameters.filePath === undefined || parameters.filePath === '') { | ||
throw new Error('Invalid input: filePath is required'); | ||
} | ||
const fileUri = Uri.file(parameters.filePath); | ||
|
||
try { | ||
const environment = await this.api.getEnvironment(fileUri); | ||
if (!environment) { | ||
// Check if the file is a notebook or a notebook cell to throw specific error messages. | ||
if (fileUri.fsPath.endsWith('.ipynb') || fileUri.fsPath.includes('.ipynb#')) { | ||
throw new Error('Unable to access Jupyter kernels for notebook cells'); | ||
} | ||
throw new Error('No environment found'); | ||
} | ||
await this.api.refreshPackages(environment); | ||
const installedPackages = await this.api.getPackages(environment); | ||
|
||
let resultMessage: string; | ||
if (!installedPackages || installedPackages.length === 0) { | ||
resultMessage = 'No packages are installed in the current environment.'; | ||
} else { | ||
const packageNames = installedPackages | ||
.map((pkg) => pkg.version ? `${pkg.name} (${pkg.version})` : pkg.name) | ||
.join(', '); | ||
resultMessage = 'The packages installed in the current environment are as follows:\n' + packageNames; | ||
} | ||
|
||
const textPart = new LanguageModelTextPart(resultMessage || ''); | ||
deferredReturn.resolve({ content: [textPart] }); | ||
} catch (error) { | ||
const errorMessage: string = `An error occurred while fetching packages: ${error}`; | ||
deferredReturn.resolve({ content: [new LanguageModelTextPart(errorMessage)] } as LanguageModelToolResult); | ||
} | ||
return deferredReturn.promise; | ||
} | ||
|
||
/** | ||
* Prepares the invocation of the tool. | ||
* @param _options - The preparation options. | ||
* @param _token - The cancellation token. | ||
* @returns The prepared tool invocation. | ||
*/ | ||
async prepareInvocation?( | ||
_options: LanguageModelToolInvocationPrepareOptions<IGetActiveFile>, | ||
_token: CancellationToken, | ||
): Promise<PreparedToolInvocation> { | ||
const message = 'Preparing to fetch the list of installed Python packages...'; | ||
return { | ||
invocationMessage: message, | ||
}; | ||
} | ||
} |
This file contains hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.