Skip to content

Commit

Permalink
chore: further improvements
Browse files Browse the repository at this point in the history
- streamline parameters for change set deletion
- remove the ref for latestRequest again
  • Loading branch information
sdirix committed Jan 24, 2025
1 parent fd53967 commit ec65d29
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 13 deletions.
23 changes: 14 additions & 9 deletions packages/ai-chat-ui/src/browser/chat-input-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,8 @@ export class AIChatInputWidget extends ReactWidget {
interface ChatInputProperties {
onCancel: (requestModel: ChatRequestModel) => void;
onQuery: (query: string) => void;
onDeleteChangeSet: (requestModel: ChatRequestModel) => void;
onDeleteChangeSetElement: (requestModel: ChatRequestModel, index: number) => void;
onDeleteChangeSet: (sessionId: string) => void;
onDeleteChangeSetElement: (sessionId: string, index: number) => void;
isEnabled?: boolean;
chatModel: ChatModel;
editorProvider: MonacoEditorProvider;
Expand All @@ -150,10 +150,14 @@ interface ChatInputProperties {
}

const ChatInput: React.FunctionComponent<ChatInputProperties> = (props: ChatInputProperties) => {
const onDeleteChangeSet = () => props.onDeleteChangeSet(props.chatModel.id);
const onDeleteChangeSetElement = (index: number) => props.onDeleteChangeSetElement(props.chatModel.id, index);

const [inProgress, setInProgress] = React.useState(false);
const [changeSetUI, setChangeSetUI] = React.useState(
() => props.chatModel.changeSet ? buildChangeSetUI(props.chatModel.changeSet, props.labelProvider, onDeleteChangeSet, onDeleteChangeSetElement) : undefined
);

// eslint-disable-next-line no-null/no-null
const editorContainerRef = React.useRef<HTMLDivElement | null>(null);
// eslint-disable-next-line no-null/no-null
Expand Down Expand Up @@ -235,15 +239,13 @@ const ChatInput: React.FunctionComponent<ChatInputProperties> = (props: ChatInpu
};
}, []);

const latestRequest = React.useRef<ChatRequestModel>();
const responseListenerRef = React.useRef<Disposable>();
// track chat model updates to keep our UI in sync
// - keep "inProgress" in sync with the request state
// - keep "changeSetUI" in sync with the change set
React.useEffect(() => {
const listener = props.chatModel.onDidChange(event => {
if (event.kind === 'addRequest') {
latestRequest.current = event.request;
if (event.request) {
setInProgress(ChatRequestModel.isInProgress(event.request));
}
Expand All @@ -259,6 +261,7 @@ const ChatInput: React.FunctionComponent<ChatInputProperties> = (props: ChatInpu
}
}
});
setChangeSetUI(props.chatModel.changeSet ? buildChangeSetUI(props.chatModel.changeSet, props.labelProvider, onDeleteChangeSet, onDeleteChangeSetElement) : undefined);
return () => {
listener?.dispose();
responseListenerRef.current?.dispose();
Expand Down Expand Up @@ -316,15 +319,13 @@ const ChatInput: React.FunctionComponent<ChatInputProperties> = (props: ChatInpu
className: 'codicon-add'
}] : [];

const onDeleteChangeSet = () => latestRequest.current ? props.onDeleteChangeSet(latestRequest.current) : undefined;
const onDeleteChangeSetElement = (index: number) => latestRequest.current ? props.onDeleteChangeSetElement(latestRequest.current, index) : undefined;

const rightOptions = inProgress
? [{
title: 'Cancel (Esc)',
handler: () => {
if (latestRequest.current) {
props.onCancel(latestRequest.current);
const latestRequest = getLatestRequest(props.chatModel);
if (latestRequest) {
props.onCancel(latestRequest);
}
setInProgress(false);
},
Expand Down Expand Up @@ -484,3 +485,7 @@ function acceptablePendingElements(changeSet: ChangeSet): ChangeSetElement[] {
return changeSet.getElements().filter(e => e.accept && (e.state === undefined || e.state === 'pending'));
}

function getLatestRequest(chatModel: ChatModel): ChatRequestModel | undefined {
const requests = chatModel.getRequests();
return requests.length > 0 ? requests[requests.length - 1] : undefined;
}
8 changes: 4 additions & 4 deletions packages/ai-chat-ui/src/browser/chat-view-widget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ export class ChatViewWidget extends BaseWidget implements ExtractableWidget, Sta
this.chatService.cancelRequest(requestModel.session.id, requestModel.id);
}

protected onDeleteChangeSet(requestModel: ChatRequestModel): void {
this.chatService.deleteChangeSet(requestModel.session.id);
protected onDeleteChangeSet(sessionId: string): void {
this.chatService.deleteChangeSet(sessionId);
}

protected onDeleteChangeSetElement(requestModel: ChatRequestModel, index: number): void {
this.chatService.deleteChangeSetElement(requestModel.session.id, index);
protected onDeleteChangeSetElement(sessionId: string, index: number): void {
this.chatService.deleteChangeSetElement(sessionId, index);
}

lock(): void {
Expand Down

0 comments on commit ec65d29

Please sign in to comment.