Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor find last methods #114

Merged
merged 3 commits into from
Jan 29, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion public/hooks/use_chat_actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

import { TAB_ID } from '../utils/constants';
import { ASSISTANT_API } from '../../common/constants/llm';
import { findLastIndex } from '../utils';
import {
IMessage,
ISuggestedAction,
Expand Down Expand Up @@ -187,7 +188,8 @@ export const useChatActions = (): AssistantActions => {
* In implementation of Agent framework, it will generate a new interactionId
* so need to remove the staled interaction in Frontend manually.
*/
const findRegeratedMessageIndex = chatState.messages.findLastIndex(
const findRegeratedMessageIndex = findLastIndex(
chatState.messages,
(message) => message.type === 'input'
);
if (findRegeratedMessageIndex > -1) {
Expand Down
3 changes: 2 additions & 1 deletion public/hooks/use_chat_state.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import { produce } from 'immer';
import React, { useContext, useMemo, useReducer } from 'react';
import { IMessage, Interaction } from '../../common/types/chat_saved_object_attributes';
import { findLastIndex } from '../utils';

export interface ChatState {
messages: IMessage[];
Expand Down Expand Up @@ -104,7 +105,7 @@ const chatStateReducer: React.Reducer<ChatState, ChatStateAction> = (state, acti
draft.llmResponding = false;
break;
case 'regenerate':
const lastInputIndex = draft.messages.findLastIndex((msg) => msg.type === 'input');
const lastInputIndex = findLastIndex(draft.messages, (msg) => msg.type === 'input');
// Exclude the last outputs
draft.messages = draft.messages.slice(0, lastInputIndex + 1);
draft.llmResponding = true;
Expand Down
3 changes: 2 additions & 1 deletion public/hooks/use_feed_back.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ export const useFeedback = (interaction?: Interaction | null) => {
});
const inputMessage = chatState.messages
.slice(0, outputMessageIndex)
.findLast((item) => item.type === 'input');
.reverse()
.find((item) => item.type === 'input');
if (!inputMessage) {
return;
}
Expand Down
3 changes: 2 additions & 1 deletion public/tabs/chat/chat_page_content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import {
import { WelcomeMessage } from '../../components/chat_welcome_message';
import { useChatContext } from '../../contexts';
import { useChatState, useChatActions } from '../../hooks';
import { findLastIndex } from '../../utils';
import { MessageBubble } from './messages/message_bubble';
import { MessageContent } from './messages/message_content';
import { SuggestionBubble } from './suggestions/suggestion_bubble';
Expand Down Expand Up @@ -81,7 +82,7 @@ export const ChatPageContent: React.FC<ChatPageContentProps> = React.memo((props
}

const firstInputIndex = chatState.messages.findIndex((msg) => msg.type === 'input');
const lastInputIndex = chatState.messages.findLastIndex((msg) => msg.type === 'input');
const lastInputIndex = findLastIndex(chatState.messages, (msg) => msg.type === 'input');

return (
<>
Expand Down
18 changes: 18 additions & 0 deletions public/utils/find_last_index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

export const findLastIndex = <T>(
array: T[],
predicate: (value: T, index: number, array: T[]) => unknown
) => {
if (array.length === 0) {
return -1;
}
const index = [...array].reverse().findIndex(predicate);
if (index === -1) {
return -1;
}
Copy link
Collaborator

@Hailong-am Hailong-am Jan 26, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why don't just search from last element to first?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that means write a for loop to search from end to start?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that sounds like more straightforward and no additional array copy and reverse

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense, will update later.

return array.length - index - 1;
};
1 change: 1 addition & 0 deletions public/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@
*/

export * from './notebook';
export * from './find_last_index';
19 changes: 19 additions & 0 deletions public/utils/tests/find_last_index.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
/*
* Copyright OpenSearch Contributors
* SPDX-License-Identifier: Apache-2.0
*/

import { findLastIndex } from '../find_last_index';

describe('findLastIndex should return consistent value', () => {
it('should return -1 if element not found', () => {
expect(findLastIndex([1, 2, 3, 3, 4], (item) => item === 5)).toBe(-1);
expect(findLastIndex([], () => true)).toBe(-1);
});
it('should return consistent index if element found', () => {
expect(findLastIndex([1, 2, 3, 4], (item) => item === 3)).toBe(2);
expect(findLastIndex([1, 2, 3, 3, 4], (item) => item === 3)).toBe(3);
expect(findLastIndex([1, 2, 3, 4, 4], (item) => item === 4)).toBe(4);
expect(findLastIndex([1, 1, 2, 3, 4], (item) => item === 1)).toBe(1);
});
});
Loading