diff --git a/src/github/copilotRemoteAgent.ts b/src/github/copilotRemoteAgent.ts index 17968c9191..b5c109525c 100644 --- a/src/github/copilotRemoteAgent.ts +++ b/src/github/copilotRemoteAgent.ts @@ -908,7 +908,7 @@ export class CopilotRemoteAgentManager extends Disposable { if (sessionIndex === 0) { sessionPrompt = await this.getInitialSessionPrompt(session, pullRequest, capi, sessionPrompt); } else { - sessionPrompt = await this.getFollowUpSessionPrompt(sessionIndex, timelineEvents, sessionPrompt); + sessionPrompt = await this.getFollowUpSessionPrompt(session, sessionIndex, pullRequest, timelineEvents, capi, sessionPrompt); } // TODO: @rebornix, remove @copilot prefix from session prompt for now @@ -944,10 +944,34 @@ export class CopilotRemoteAgentManager extends Disposable { } private async getFollowUpSessionPrompt( + session: SessionInfo, sessionIndex: number, + pullRequest: PullRequestModel, timelineEvents: readonly TimelineEvent[], + capi: CopilotApi, defaultPrompt: string ): Promise { + // First, try to get problem statement from Jobs API (similar to initial session) + try { + const jobInfo = await capi.getJobBySessionId( + pullRequest.base.repositoryCloneUrl.owner, + pullRequest.base.repositoryCloneUrl.repositoryName, + session.id + ); + if (jobInfo && jobInfo.problem_statement) { + let prompt = jobInfo.problem_statement; + const titleMatch = jobInfo.problem_statement.match(/TITLE: \s*(.*)/i); + if (titleMatch && titleMatch[1]) { + prompt = titleMatch[1].trim(); + } + Logger.appendLine(`Session ${sessionIndex}: Found problem_statement from Jobs API: ${prompt}`, CopilotRemoteAgentManager.ID); + return prompt; + } + } catch (error) { + Logger.warn(`Failed to get job info for session ${session.id}: ${error}`, CopilotRemoteAgentManager.ID); + } + + // If no problem statement found, fall back to timeline event-based logic const copilotStartedEvents = timelineEvents .filter((event): event is CopilotStartedEvent => event.event === EventType.CopilotStarted) .sort((a, b) => new Date(a.createdAt).getTime() - new Date(b.createdAt).getTime()); diff --git a/src/test/github/copilotRemoteAgent.test.ts b/src/test/github/copilotRemoteAgent.test.ts new file mode 100644 index 0000000000..322feee056 --- /dev/null +++ b/src/test/github/copilotRemoteAgent.test.ts @@ -0,0 +1,25 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +import { default as assert } from 'assert'; + +describe('CopilotRemoteAgentManager - Secondary Session Problem Statement Retrieval', function () { + it('should extract title from problem statement correctly', function () { + // Test the TITLE extraction logic that was added to secondary sessions + const problemStatement1 = 'TITLE: Fix secondary session issue\nThis is a test problem statement for secondary sessions.'; + const titleMatch1 = problemStatement1.match(/TITLE: \s*(.*)/i); + assert.strictEqual(titleMatch1?.[1]?.trim(), 'Fix secondary session issue'); + + // Test with different formatting + const problemStatement2 = 'TITLE: Handle chat sessions properly \nDetailed description here.'; + const titleMatch2 = problemStatement2.match(/TITLE: \s*(.*)/i); + assert.strictEqual(titleMatch2?.[1]?.trim(), 'Handle chat sessions properly'); + + // Test without TITLE prefix + const problemStatement3 = 'Just a regular problem statement without title'; + const titleMatch3 = problemStatement3.match(/TITLE: \s*(.*)/i); + assert.strictEqual(titleMatch3, null); + }); +}); \ No newline at end of file