Skip to content

Commit ac6728c

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored andcommitted
[Freestyler] Switch between Agents
This changes the behavior when switching between Agents, now every Agent has its own chat that you can continue from. Clearing only clears the current agent info. Bug: 369303513 Change-Id: I875008bc7cd0361f523c420fcc10f33933a62f75 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/5965796 Commit-Queue: Alex Rudenko <[email protected]> Commit-Queue: Nikolay Vitkov <[email protected]> Auto-Submit: Nikolay Vitkov <[email protected]> Reviewed-by: Alex Rudenko <[email protected]>
1 parent ef7e70f commit ac6728c

File tree

7 files changed

+175
-31
lines changed

7 files changed

+175
-31
lines changed

front_end/panels/freestyler/AiAgent.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,76 @@ describeWithEnvironment('AiAgent', () => {
152152
]);
153153
});
154154
});
155+
156+
describe('runFromHistory', () => {
157+
it('should run', async () => {
158+
const agent = new AiAgentMock({
159+
aidaClient: {} as Host.AidaClient.AidaClient,
160+
});
161+
agent.chatNewHistoryForTesting = new Map([
162+
[
163+
0,
164+
[
165+
{
166+
type: ResponseType.USER_QUERY,
167+
query: 'first question',
168+
},
169+
{
170+
type: ResponseType.QUERYING,
171+
query: 'first enhancements',
172+
},
173+
{
174+
type: ResponseType.ANSWER,
175+
text: 'first answer',
176+
},
177+
],
178+
],
179+
[
180+
1,
181+
[
182+
{
183+
type: ResponseType.USER_QUERY,
184+
query: 'second question',
185+
},
186+
{
187+
type: ResponseType.QUERYING,
188+
query: 'second enhancements',
189+
},
190+
{
191+
type: ResponseType.ANSWER,
192+
text: 'second answer',
193+
},
194+
],
195+
],
196+
]);
197+
198+
const responses = await Array.fromAsync(agent.runFromHistory());
199+
assert.deepStrictEqual(responses, [
200+
{
201+
type: ResponseType.USER_QUERY,
202+
query: 'first question',
203+
},
204+
{
205+
type: ResponseType.QUERYING,
206+
query: 'first enhancements',
207+
},
208+
{
209+
type: ResponseType.ANSWER,
210+
text: 'first answer',
211+
},
212+
{
213+
type: ResponseType.USER_QUERY,
214+
query: 'second question',
215+
},
216+
{
217+
type: ResponseType.QUERYING,
218+
query: 'second enhancements',
219+
},
220+
{
221+
type: ResponseType.ANSWER,
222+
text: 'second answer',
223+
},
224+
]);
225+
});
226+
});
155227
});

front_end/panels/freestyler/AiAgent.ts

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,7 @@ export interface UserQuery {
8484
}
8585

8686
export type ResponseData = AnswerResponse|ErrorResponse|ActionResponse|SideEffectResponse|ThoughtResponse|TitleResponse|
87-
QueryResponse|ContextResponse;
88-
export type ResponseDataWithHistory = ResponseData|UserQuery;
87+
QueryResponse|ContextResponse|UserQuery;
8988

9089
export interface AidaBuildRequestOptions {
9190
input: string;
@@ -139,7 +138,7 @@ export abstract class AiAgent<T> {
139138
* Mapping between the unique request id and
140139
* the history chuck it created
141140
*/
142-
#history = new Map<number, ResponseDataWithHistory[]>();
141+
#history = new Map<number, ResponseData[]>();
143142

144143
constructor(opts: AgentOptions) {
145144
this.#aidaClient = opts.aidaClient;
@@ -150,7 +149,7 @@ export abstract class AiAgent<T> {
150149
return this.#historyEntry;
151150
}
152151

153-
set chatNewHistoryForTesting(history: Map<number, ResponseDataWithHistory[]>) {
152+
set chatNewHistoryForTesting(history: Map<number, ResponseData[]>) {
154153
this.#history = history;
155154
}
156155

@@ -327,7 +326,7 @@ STOP`;
327326
return [...historyAll.values()].flat();
328327
}
329328

330-
#addHistory(id: number, data: ResponseDataWithHistory): void {
329+
#addHistory(id: number, data: ResponseData): void {
331330
const currentRunEntries = this.#history.get(id);
332331
if (currentRunEntries) {
333332
currentRunEntries.push(data);
@@ -342,16 +341,18 @@ STOP`;
342341
}): AsyncGenerator<ResponseData, void, void> {
343342
const id = this.#runId++;
344343

344+
const response = {
345+
type: ResponseType.USER_QUERY,
346+
query,
347+
} as const;
348+
this.#addHistory(id, response);
349+
yield response;
350+
345351
for await (const response of this.handleContextDetails(options.selected)) {
346352
this.#addHistory(id, response);
347353
yield response;
348354
}
349355

350-
this.#addHistory(id, {
351-
type: ResponseType.USER_QUERY,
352-
query,
353-
});
354-
355356
query = await this.enhanceQuery(query, options.selected);
356357

357358
for (let i = 0; i < MAX_STEP; i++) {
@@ -471,6 +472,14 @@ STOP`;
471472
window.dispatchEvent(new CustomEvent('freestylerdone'));
472473
}
473474
}
475+
476+
async * runFromHistory(): AsyncGenerator<ResponseData, void, void> {
477+
for (const historyChunk of this.#history.values()) {
478+
for (const entry of historyChunk) {
479+
yield entry;
480+
}
481+
}
482+
}
474483
}
475484

476485
export function isDebugMode(): boolean {

front_end/panels/freestyler/DrJonesFileAgent.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,10 @@ describeWithMockConnection('DrJonesFileAgent', () => {
165165
const responses = await Array.fromAsync(agent.run('test', {selected: uiSourceCode}));
166166

167167
assert.deepStrictEqual(responses, [
168+
{
169+
type: ResponseType.USER_QUERY,
170+
query: 'test',
171+
},
168172
{
169173
type: ResponseType.CONTEXT,
170174
title: 'Analyzing file',

front_end/panels/freestyler/DrJonesNetworkAgent.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ describeWithMockConnection('DrJonesNetworkAgent', () => {
217217

218218
const responses = await Array.fromAsync(agent.run('test', {selected: selectedNetworkRequest}));
219219
assert.deepStrictEqual(responses, [
220+
{
221+
type: ResponseType.USER_QUERY,
222+
query: 'test',
223+
},
220224
{
221225
type: ResponseType.CONTEXT,
222226
title: 'Analyzing network data',

front_end/panels/freestyler/DrJonesPerformanceAgent.test.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,6 +153,10 @@ describeWithEnvironment('DrJonesPerformanceAgent', () => {
153153
const responses = await Array.fromAsync(agent.run('test', {selected: aiNodeTree}));
154154

155155
assert.deepStrictEqual(responses, [
156+
{
157+
type: ResponseType.USER_QUERY,
158+
query: 'test',
159+
},
156160
{
157161
type: ResponseType.CONTEXT,
158162
title: 'Analyzing stack',

front_end/panels/freestyler/FreestylerAgent.test.ts

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,10 @@ c`;
760760

761761
const responses = await Array.fromAsync(agent.run('test', {selected: element}));
762762
assert.deepStrictEqual(responses, [
763+
{
764+
type: Freestyler.ResponseType.USER_QUERY,
765+
query: 'test',
766+
},
763767
{
764768
type: Freestyler.ResponseType.CONTEXT,
765769
title: 'Analyzing the prompt',
@@ -813,6 +817,10 @@ c`;
813817

814818
const responses = await Array.fromAsync(agent.run('test', {selected: element}));
815819
assert.deepStrictEqual(responses, [
820+
{
821+
type: Freestyler.ResponseType.USER_QUERY,
822+
query: 'test',
823+
},
816824
{
817825
type: Freestyler.ResponseType.CONTEXT,
818826
title: 'Analyzing the prompt',
@@ -859,6 +867,10 @@ c`;
859867

860868
const responses = await Array.fromAsync(agent.run('test', {selected: element}));
861869
assert.deepStrictEqual(responses, [
870+
{
871+
type: Freestyler.ResponseType.USER_QUERY,
872+
query: 'test',
873+
},
862874
{
863875
type: Freestyler.ResponseType.CONTEXT,
864876
title: 'Analyzing the prompt',
@@ -904,6 +916,10 @@ c`;
904916

905917
const responses = await Array.fromAsync(agent.run('test', {selected: element}));
906918
assert.deepStrictEqual(responses, [
919+
{
920+
type: Freestyler.ResponseType.USER_QUERY,
921+
query: 'test',
922+
},
907923
{
908924
type: Freestyler.ResponseType.CONTEXT,
909925
title: 'Analyzing the prompt',
@@ -943,6 +959,10 @@ c`;
943959
});
944960
const responses = await Array.fromAsync(agent.run('test', {selected: element}));
945961
assert.deepStrictEqual(responses, [
962+
{
963+
type: Freestyler.ResponseType.USER_QUERY,
964+
query: 'test',
965+
},
946966
{
947967
type: Freestyler.ResponseType.CONTEXT,
948968
title: 'Analyzing the prompt',
@@ -1002,6 +1022,10 @@ ANSWER: this is the answer`,
10021022
});
10031023
const responses = await Array.fromAsync(agent.run('test', {selected: element}));
10041024
assert.deepStrictEqual(responses, [
1025+
{
1026+
type: Freestyler.ResponseType.USER_QUERY,
1027+
query: 'test',
1028+
},
10051029
{
10061030
type: Freestyler.ResponseType.CONTEXT,
10071031
title: 'Analyzing the prompt',

0 commit comments

Comments
 (0)