Skip to content

Commit ff2c270

Browse files
Lightning00BladeDevtools-frontend LUCI CQ
authored andcommitted
[AI Assistance] Refactor Agent swaps
Bug: none Change-Id: I3474f221ac51e126d546a6f3a65a59ede4986602 Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/6032237 Reviewed-by: Alex Rudenko <[email protected]> Commit-Queue: Nikolay Vitkov <[email protected]>
1 parent 449c662 commit ff2c270

File tree

1 file changed

+26
-44
lines changed

1 file changed

+26
-44
lines changed

front_end/panels/freestyler/FreestylerPanel.ts

Lines changed: 26 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -386,8 +386,20 @@ export class FreestylerPanel extends UI.Panel.Panel {
386386
targetAgentType = AgentType.DRJONES_PERFORMANCE;
387387
}
388388

389-
this.#currentAgent = targetAgentType ? this.#createAgent(targetAgentType) : undefined;
390-
this.#viewProps.agentType = targetAgentType;
389+
const agent = targetAgentType ? this.#createAgent(targetAgentType) : undefined;
390+
this.#updateAgentState(agent);
391+
}
392+
393+
#updateAgentState(agent?: AiAgent<unknown>): void {
394+
if (this.#currentAgent !== agent) {
395+
this.#cancel();
396+
this.#currentAgent = agent;
397+
this.#viewProps.agentType = this.#currentAgent?.type;
398+
this.#viewProps.messages = [];
399+
this.#viewProps.isLoading = false;
400+
this.#viewProps.isReadOnly = this.#currentAgent?.isHistoryEntry ?? false;
401+
}
402+
391403
this.#onContextSelectionChanged();
392404
void this.doUpdate();
393405
}
@@ -491,7 +503,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
491503
}
492504

493505
this.#selectedElement = createNodeContext(selectedElementFilter(ev.data));
494-
this.#onContextSelectionChanged();
506+
this.#updateAgentState(this.#currentAgent);
495507
};
496508

497509
#handleNetworkRequestFlavorChange =
@@ -501,7 +513,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
501513
}
502514

503515
this.#selectedRequest = Boolean(ev.data) ? new RequestContext(ev.data) : null;
504-
this.#onContextSelectionChanged();
516+
this.#updateAgentState(this.#currentAgent);
505517
};
506518

507519
#handleTraceEntryNodeFlavorChange =
@@ -511,7 +523,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
511523
}
512524

513525
this.#selectedCallTree = Boolean(ev.data) ? new CallTreeContext(ev.data) : null;
514-
this.#onContextSelectionChanged();
526+
this.#updateAgentState(this.#currentAgent);
515527
};
516528

517529
#handleUISourceCodeFlavorChange =
@@ -524,7 +536,7 @@ export class FreestylerPanel extends UI.Panel.Panel {
524536
return;
525537
}
526538
this.#selectedFile = new FileContext(ev.data);
527-
this.#onContextSelectionChanged();
539+
this.#updateAgentState(this.#currentAgent);
528540
};
529541

530542
#handleFreestylerEnabledSettingChanged = (): void => {
@@ -629,21 +641,13 @@ export class FreestylerPanel extends UI.Panel.Panel {
629641
return;
630642
}
631643

632-
let newAgent = false;
644+
let agent = this.#currentAgent;
633645
if (!this.#currentAgent || this.#currentAgent.type !== targetAgentType || this.#currentAgent.isHistoryEntry ||
634646
targetAgentType === AgentType.DRJONES_PERFORMANCE) {
635-
this.#currentAgent = this.#createAgent(targetAgentType);
636-
newAgent = true;
647+
agent = this.#createAgent(targetAgentType);
637648
}
638-
this.#viewProps.agentType = this.#currentAgent.type;
649+
this.#updateAgentState(agent);
639650
this.#viewOutput.freestylerChatUi?.focusTextInput();
640-
this.#onContextSelectionChanged();
641-
void this.doUpdate();
642-
if (newAgent) {
643-
this.#viewProps.messages = [];
644-
this.#viewProps.isReadOnly = false;
645-
void this.#doConversation(this.#currentAgent.runFromHistory());
646-
}
647651
}
648652

649653
#onHistoryClicked(event: Event): void {
@@ -690,57 +694,39 @@ export class FreestylerPanel extends UI.Panel.Panel {
690694

691695
#clearHistory(): void {
692696
this.#agents = new Set();
693-
this.#currentAgent = undefined;
694-
this.#viewProps.messages = [];
695-
this.#viewProps.agentType = undefined;
696-
this.#runAbortController.abort();
697697
void AiHistoryStorage.instance().deleteAll();
698-
void this.doUpdate();
698+
this.#updateAgentState();
699699
}
700700

701701
#onDeleteClicked(): void {
702702
if (this.#currentAgent) {
703703
this.#agents.delete(this.#currentAgent);
704704
void AiHistoryStorage.instance().deleteHistoryEntry(this.#currentAgent.id);
705-
this.#currentAgent = undefined;
706-
this.#cancel();
707705
}
708-
709-
this.#viewProps.messages = [];
710-
706+
this.#updateAgentState();
711707
this.#selectDefaultAgentIfNeeded();
712-
this.#onContextSelectionChanged();
713-
void this.doUpdate();
714708
UI.ARIAUtils.alert(i18nString(UIStrings.chatDeleted));
715709
}
716710

717711
async #switchAgent(agent: AiAgent<unknown>): Promise<void> {
718712
if (this.#currentAgent === agent) {
719713
return;
720714
}
721-
722-
this.#currentAgent = agent;
723-
this.#viewProps.messages = [];
724-
this.#viewProps.agentType = agent.type;
725-
this.#onContextSelectionChanged();
715+
this.#updateAgentState(agent);
726716
this.#viewProps.isReadOnly = true;
727717
await this.#doConversation(agent.runFromHistory());
728718
}
729719

730720
#handleNewChatRequest(): void {
731-
this.#viewProps.messages = [];
732-
this.#viewProps.isLoading = false;
733-
this.#currentAgent = undefined;
734-
this.#cancel();
735-
721+
this.#updateAgentState();
736722
this.#selectDefaultAgentIfNeeded();
737-
void this.doUpdate();
738723
UI.ARIAUtils.alert(i18nString(UIStrings.newChatCreated));
739724
}
740725

741726
#handleCrossOriginChatCancellation(): void {
742727
if (this.#previousSameOriginContext) {
743728
this.#onContextSelectionChanged(this.#previousSameOriginContext);
729+
void this.doUpdate();
744730
}
745731
}
746732

@@ -754,15 +740,13 @@ export class FreestylerPanel extends UI.Panel.Panel {
754740
#onContextSelectionChanged(contextToRestore?: ConversationContext<unknown>): void {
755741
if (!this.#currentAgent) {
756742
this.#viewProps.blockedByCrossOrigin = false;
757-
void this.doUpdate();
758743
return;
759744
}
760745
const currentContext = contextToRestore ?? this.#getConversationContext();
761746
this.#viewProps.selectedContext = currentContext;
762747
if (!currentContext) {
763748
this.#viewProps.blockedByCrossOrigin = false;
764749
this.#viewProps.requiresNewConversation = false;
765-
void this.doUpdate();
766750
return;
767751
}
768752
this.#viewProps.blockedByCrossOrigin = !currentContext.isOriginAllowed(this.#currentAgent.origin);
@@ -772,11 +756,9 @@ export class FreestylerPanel extends UI.Panel.Panel {
772756
if (this.#viewProps.blockedByCrossOrigin && this.#previousSameOriginContext) {
773757
this.#viewProps.onCancelCrossOriginChat = this.#handleCrossOriginChatCancellation.bind(this);
774758
}
775-
this.#viewProps.isReadOnly = this.#currentAgent.isHistoryEntry;
776759
this.#viewProps.requiresNewConversation = this.#currentAgent.type === AgentType.DRJONES_PERFORMANCE &&
777760
Boolean(this.#currentAgent.context) && this.#currentAgent.context !== currentContext;
778761
this.#viewProps.stripLinks = this.#viewProps.agentType === AgentType.DRJONES_PERFORMANCE;
779-
void this.doUpdate();
780762
}
781763

782764
#getConversationContext(): ConversationContext<unknown>|null {

0 commit comments

Comments
 (0)