-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Expand file tree
/
Copy pathlistDirTool.tsx
More file actions
103 lines (85 loc) · 4.73 KB
/
listDirTool.tsx
File metadata and controls
103 lines (85 loc) · 4.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
/*---------------------------------------------------------------------------------------------
* Copyright (c) Microsoft Corporation. All rights reserved.
* Licensed under the MIT License. See License.txt in the project root for license information.
*--------------------------------------------------------------------------------------------*/
import * as l10n from '@vscode/l10n';
import { BasePromptElementProps, PromptElement, PromptPiece, PromptSizing, TextChunk } from '@vscode/prompt-tsx';
import type * as vscode from 'vscode';
import { IFileSystemService } from '../../../platform/filesystem/common/fileSystemService';
import { FileType } from '../../../platform/filesystem/common/fileTypes';
import { IPromptPathRepresentationService } from '../../../platform/prompts/common/promptPathRepresentationService';
import { IWorkspaceService } from '../../../platform/workspace/common/workspaceService';
import { CancellationToken } from '../../../util/vs/base/common/cancellation';
import { IInstantiationService, ServicesAccessor } from '../../../util/vs/platform/instantiation/common/instantiation';
import { LanguageModelPromptTsxPart, LanguageModelToolResult, MarkdownString } from '../../../vscodeTypes';
import { renderPromptElementJSON } from '../../prompts/node/base/promptRenderer';
import { ToolName } from '../common/toolNames';
import { ToolRegistry } from '../common/toolsRegistry';
import { formatUriForFileWidget } from '../common/toolUtils';
import { checkCancellation, isDirExternalAndNeedsConfirmation, resolveToolInputPath } from './toolUtils';
import { IBuildPromptContext } from '../../prompt/common/intents';
interface IListDirParams {
path: string;
}
class ListDirTool implements vscode.LanguageModelTool<IListDirParams> {
public static readonly toolName = ToolName.ListDirectory;
public static readonly nonDeferred = true;
private _promptContext: IBuildPromptContext | undefined;
constructor(
@IFileSystemService private readonly fsService: IFileSystemService,
@IInstantiationService private readonly instantiationService: IInstantiationService,
@IWorkspaceService private readonly workspaceService: IWorkspaceService,
@IPromptPathRepresentationService private readonly promptPathRepresentationService: IPromptPathRepresentationService,
) { }
async invoke(options: vscode.LanguageModelToolInvocationOptions<IListDirParams>, token: CancellationToken) {
const uri = resolveToolInputPath(options.input.path, this.promptPathRepresentationService);
checkCancellation(token);
const contents = await this.fsService.readDirectory(uri);
checkCancellation(token);
return new LanguageModelToolResult([
new LanguageModelPromptTsxPart(
await renderPromptElementJSON(this.instantiationService, ListDirResult, { results: contents }, options.tokenizationOptions, token))]);
}
async prepareInvocation(options: vscode.LanguageModelToolInvocationPrepareOptions<IListDirParams>, token: vscode.CancellationToken): Promise<vscode.PreparedToolInvocation | undefined> {
const uri = resolveToolInputPath(options.input.path, this.promptPathRepresentationService);
// Check if directory is external (outside workspace)
const isExternal = this.instantiationService.invokeFunction(
(accessor: ServicesAccessor) => isDirExternalAndNeedsConfirmation(accessor, uri, this._promptContext, { readOnly: true })
);
if (isExternal) {
const message = this.workspaceService.getWorkspaceFolders().length === 1
? new MarkdownString(l10n.t`${formatUriForFileWidget(uri)} is outside of the current folder.`)
: new MarkdownString(l10n.t`${formatUriForFileWidget(uri)} is outside of the current workspace.`);
return {
invocationMessage: new MarkdownString(l10n.t`Reading ${formatUriForFileWidget(uri)}`),
pastTenseMessage: new MarkdownString(l10n.t`Read ${formatUriForFileWidget(uri)}`),
confirmationMessages: {
title: l10n.t`Allow reading external directory?`,
message,
}
};
}
return {
invocationMessage: new MarkdownString(l10n.t`Reading ${formatUriForFileWidget(uri)}`),
pastTenseMessage: new MarkdownString(l10n.t`Read ${formatUriForFileWidget(uri)}`),
};
}
async resolveInput(input: IListDirParams, promptContext: IBuildPromptContext): Promise<IListDirParams> {
this._promptContext = promptContext;
return input;
}
}
ToolRegistry.registerTool(ListDirTool);
interface ListDirResultProps extends BasePromptElementProps {
results: [string, FileType][];
}
class ListDirResult extends PromptElement<ListDirResultProps> {
override render(state: void, sizing: PromptSizing): PromptPiece<any, any> | undefined {
if (this.props.results.length === 0) {
return <>Folder is empty</>;
}
return <>
{this.props.results.map(([name, type]) => <TextChunk>{name}{type === FileType.Directory ? '/' : ''}</TextChunk>)}
</>;
}
}