Skip to content

Commit ccd083f

Browse files
authored
Fix remote issues with task tools (microsoft#259033)
1 parent b0271fa commit ccd083f

File tree

2 files changed

+31
-7
lines changed

2 files changed

+31
-7
lines changed

src/vs/workbench/contrib/terminalContrib/chatAgentTools/browser/task/createAndRunTaskTool.ts

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -84,18 +84,25 @@ export class CreateAndRunTaskTool implements IToolImpl {
8484
if (!exists) {
8585
await this._fileService.createFile(tasksJsonUri, VSBuffer.fromString(tasksJsonContent), { overwrite: true });
8686
_progress.report({ message: 'Created tasks.json file' });
87-
await timeout(200);
8887
} else {
8988
// add to the existing tasks.json file
9089
const content = await this._fileService.readFile(tasksJsonUri);
9190
const tasksJson = JSON.parse(content.value.toString());
9291
tasksJson.tasks.push(newTask);
9392
await this._fileService.writeFile(tasksJsonUri, VSBuffer.fromString(JSON.stringify(tasksJson, null, '\t')));
9493
_progress.report({ message: 'Updated tasks.json file' });
95-
await timeout(200);
9694
}
9795
_progress.report({ message: new MarkdownString(localize('copilotChat.fetchingTask', 'Resolving the task')) });
98-
const task = (await this._tasksService.tasks())?.find(t => t._label === args.task.label);
96+
97+
let task: Task | undefined;
98+
const start = Date.now();
99+
while (Date.now() - start < 5000 && !token.isCancellationRequested) {
100+
task = (await this._tasksService.tasks())?.find(t => t._label === args.task.label);
101+
if (task) {
102+
break;
103+
}
104+
await timeout(100);
105+
}
99106
if (!task) {
100107
return { content: [{ kind: 'text', value: `Task not found: ${args.task.label}` }], toolResultMessage: new MarkdownString(localize('copilotChat.taskNotFound', 'Task not found: `{0}`', args.task.label)) };
101108
}

src/vs/workbench/contrib/terminalContrib/chatAgentTools/browser/task/taskHelpers.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,14 @@ export function getTaskRepresentation(task: IConfiguredTask | Task): string {
3636
export async function getTaskForTool(id: string | undefined, taskDefinition: { taskLabel?: string; taskType?: string }, workspaceFolder: string, configurationService: IConfigurationService, taskService: ITaskService): Promise<Task | undefined> {
3737
let index = 0;
3838
let task: IConfiguredTask | undefined;
39-
const configTasks: IConfiguredTask[] = (configurationService.getValue('tasks') as { tasks: IConfiguredTask[] }).tasks ?? [];
39+
const workspaceFolderToTaskMap = await taskService.getWorkspaceTasks();
40+
let configTasks: IConfiguredTask[] = [];
41+
for (const folder of workspaceFolderToTaskMap.keys()) {
42+
const tasksConfig = configurationService.getValue('tasks', { resource: URI.parse(folder) }) as { tasks: IConfiguredTask[] } | undefined;
43+
if (tasksConfig?.tasks) {
44+
configTasks = configTasks.concat(tasksConfig.tasks);
45+
}
46+
}
4047
for (const configTask of configTasks) {
4148
if (!configTask.type || 'hide' in configTask && configTask.hide) {
4249
// Skip these as they are not included in the agent prompt and we need to align with
@@ -56,7 +63,18 @@ export async function getTaskForTool(id: string | undefined, taskDefinition: { t
5663
if (!task) {
5764
return;
5865
}
59-
const configuringTasks: IStringDictionary<ConfiguringTask> | undefined = (await taskService.getWorkspaceTasks())?.get(URI.file(workspaceFolder).toString())?.configurations?.byIdentifier;
66+
67+
let tasksForWorkspace;
68+
for (const [folder, tasks] of workspaceFolderToTaskMap) {
69+
if (URI.parse(folder).path === workspaceFolder) {
70+
tasksForWorkspace = tasks;
71+
break;
72+
}
73+
}
74+
if (!tasksForWorkspace) {
75+
return;
76+
}
77+
const configuringTasks: IStringDictionary<ConfiguringTask> | undefined = tasksForWorkspace.configurations?.byIdentifier;
6078
const configuredTask: ConfiguringTask | undefined = Object.values(configuringTasks ?? {}).find(t => {
6179
return t.type === task.type && (t._label === task.label || t._label === `${task.type}: ${getTaskRepresentation(task)}` || t._label === getTaskRepresentation(task));
6280
});
@@ -65,9 +83,8 @@ export async function getTaskForTool(id: string | undefined, taskDefinition: { t
6583
resolvedTask = await taskService.tryResolveTask(configuredTask);
6684
}
6785
if (!resolvedTask) {
68-
const customTasks: Task[] | undefined = (await taskService.getWorkspaceTasks())?.get(URI.file(workspaceFolder).toString())?.set?.tasks;
86+
const customTasks: Task[] | undefined = tasksForWorkspace.set?.tasks;
6987
resolvedTask = customTasks?.find(t => task.label === t._label || task.label === t._label);
70-
7188
}
7289
return resolvedTask;
7390
}

0 commit comments

Comments
 (0)