🪨 feat: AWS Bedrock support (#3935)

* feat: Add BedrockIcon component to SVG library

* feat: EModelEndpoint.bedrock

* feat: first pass, bedrock chat. note: AgentClient is returning `agents` as conversation.endpoint

* fix: declare endpoint in initialization step

* chore: Update @librechat/agents dependency to version 1.4.5

* feat: backend content aggregation for agents/bedrock

* feat: abort agent requests

* feat: AWS Bedrock icons

* WIP: agent provider schema parsing

* chore: Update EditIcon props type

* refactor(useGenerationsByLatest): make agents and bedrock editable

* refactor: non-assistant message content, parts

* fix: Bedrock response `sender`

* fix: use endpointOption.model_parameters not endpointOption.modelOptions

* fix: types for step handler

* refactor: Update Agents.ToolCallDelta type

* refactor: Remove unnecessary assignment of parentMessageId in AskController

* refactor: remove unnecessary assignment of parentMessageId (agent request handler)

* fix(bedrock/agents): message regeneration

* refactor: dynamic form elements using react-hook-form Controllers

* fix: agent icons/labels for messages

* fix: agent actions

* fix: use of new dynamic tags causing application crash

* refactor: dynamic settings touch-ups

* refactor: update Slider component to allow custom track class name

* refactor: update DynamicSlider component styles

* refactor: use Constants value for GLOBAL_PROJECT_NAME (enum)

* feat: agent share global methods/controllers

* fix: agents query

* fix: `getResponseModel`

* fix: share prompt a11y issue

* refactor: update SharePrompt dialog theme styles

* refactor: explicit typing for SharePrompt

* feat: add agent roles/permissions

* chore: update @librechat/agents dependency to version 1.4.7 for tool_call_ids edge case

* fix(Anthropic): messages.X.content.Y.tool_use.input: Input should be a valid dictionary

* fix: handle text parts with tool_call_ids and empty text

* fix: role initialization

* refactor: don't make instructions required

* refactor: improve typing of Text part

* fix: setShowStopButton for agents route

* chore: remove params for now

* fix: add streamBuffer and streamRate to help prevent 'Overloaded' errors from Anthropic API

* refactor: remove console.log statement in ContentRender component

* chore: typing, rename Context to Delete Button

* chore(DeleteButton): logging

* refactor(Action): make accessible

* style(Action): improve a11y again

* refactor: remove use/mention of mongoose sessions

* feat: first pass, sharing agents

* feat: visual indicator for global agent, remove author when serving to non-author

* wip: params

* chore: fix typing issues

* fix(schemas): typing

* refactor: improve accessibility of ListCard component and fix console React warning

* wip: reset templates for non-legacy new convos

* Revert "wip: params"

This reverts commit f8067e91d4.

* Revert "refactor: dynamic form elements using react-hook-form Controllers"

This reverts commit 2150c4815d.

* fix(Parameters): types and parameter effect update to only update local state to parameters

* refactor: optimize useDebouncedInput hook for better performance

* feat: first pass, anthropic bedrock params

* chore: paramEndpoints check for endpointType too

* fix: maxTokens to use coerceNumber.optional(),

* feat: extra chat model params

* chore: reduce code repetition

* refactor: improve preset title handling in SaveAsPresetDialog component

* refactor: improve preset handling in HeaderOptions component

* chore: improve typing, replace legacy dialog for SaveAsPresetDialog

* feat: save as preset from parameters panel

* fix: multi-search in select dropdown when using Option type

* refactor: update default showDefault value to false in Dynamic components

* feat: Bedrock presets settings

* chore: config, fix agents schema, update config version

* refactor: update AWS region variable name in bedrock options endpoint to BEDROCK_AWS_DEFAULT_REGION

* refactor: update baseEndpointSchema in config.ts to include baseURL property

* refactor: update createRun function to include req parameter and set streamRate based on provider

* feat: availableRegions via config

* refactor: remove unused demo agent controller file

* WIP: title

* Update @librechat/agents to version 1.5.0

* chore: addTitle.js to handle empty responseText

* feat: support images and titles

* feat: context token updates

* Refactor BaseClient test to use expect.objectContaining

* refactor: add model select, remove header options params, move side panel params below prompts

* chore: update models list, catch title error

* feat: model service for bedrock models (env)

* chore: Remove verbose debug log in AgentClient class following stream

* feat(bedrock): track token spend; fix: token rates, value key mapping for AWS models

* refactor: handle streamRate in `handleLLMNewToken` callback

* chore: AWS Bedrock example config in `.env.example`

* refactor: Rename bedrockMeta to bedrockGeneral in settings.ts and use for AI21 and Amazon Bedrock providers

* refactor: Update `.env.example` with AWS Bedrock model IDs URL and additional notes

* feat: titleModel support for bedrock

* refactor: Update `.env.example` with additional notes for AWS Bedrock model IDs
This commit is contained in:
Danny Avila 2024-09-09 12:06:59 -04:00 committed by GitHub
parent 8c14360263
commit d59b62174f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
134 changed files with 3684 additions and 1213 deletions

View file

@ -241,6 +241,7 @@ export default function useChatFunctions({
},
},
];
setShowStopButton(true);
} else {
setShowStopButton(true);
}

View file

@ -41,7 +41,7 @@ function useDebouncedInput<T = unknown>({
const newValue: T =
typeof e !== 'object'
? e
: ((e as React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>)?.target
: ((e as React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>).target
.value as unknown as T);
setValue(newValue);
setDebouncedOption(newValue);

View file

@ -38,29 +38,31 @@ function useParameterEffects<T = unknown>({
/** Resets the local state if conversationId changed */
useEffect(() => {
if (!conversation?.conversationId) {
const conversationId = conversation?.conversationId ?? '';
if (!conversationId) {
return;
}
if (idRef.current === conversation?.conversationId) {
if (idRef.current === conversationId) {
return;
}
idRef.current = conversation?.conversationId;
idRef.current = conversationId;
setInputValue(defaultValue as T);
}, [setInputValue, conversation?.conversationId, defaultValue]);
/** Resets the local state if presetId changed */
useEffect(() => {
if (!preset?.presetId) {
const presetId = preset?.presetId ?? '';
if (!presetId) {
return;
}
if (presetIdRef.current === preset?.presetId) {
if (presetIdRef.current === presetId) {
return;
}
presetIdRef.current = preset?.presetId;
presetIdRef.current = presetId;
setInputValue(defaultValue as T);
}, [setInputValue, preset?.presetId, defaultValue]);
}

View file

@ -1,8 +1,13 @@
import { useRecoilValue } from 'recoil';
import { useCallback, useMemo } from 'react';
import { isAssistantsEndpoint } from 'librechat-data-provider';
import { isAssistantsEndpoint, isAgentsEndpoint } from 'librechat-data-provider';
import type { TMessageProps } from '~/common';
import { useChatContext, useAddedChatContext, useAssistantsMapContext } from '~/Providers';
import {
useChatContext,
useAddedChatContext,
useAssistantsMapContext,
useAgentsMapContext,
} from '~/Providers';
import useCopyToClipboard from './useCopyToClipboard';
import { useAuthContext } from '~/hooks/AuthContext';
import useLocalize from '~/hooks/useLocalize';
@ -35,6 +40,8 @@ export default function useMessageActions(props: TMessageActions) {
() => (isMultiMessage === true ? addedConvo : rootConvo),
[isMultiMessage, addedConvo, rootConvo],
);
const agentMap = useAgentsMapContext();
const assistantMap = useAssistantsMapContext();
const { text, content, messageId = null, isCreatedByUser } = message ?? {};
@ -56,6 +63,26 @@ export default function useMessageActions(props: TMessageActions) {
return assistantMap?.[endpointKey] ? assistantMap[endpointKey][modelKey] : undefined;
}, [conversation?.endpoint, message?.model, assistantMap]);
const agent = useMemo(() => {
if (!isAgentsEndpoint(conversation?.endpoint)) {
return undefined;
}
if (!agentMap) {
return undefined;
}
const modelKey = message?.model ?? '';
if (modelKey) {
return agentMap[modelKey];
}
const agentId = conversation?.agent_id ?? '';
if (agentId) {
return agentMap[agentId];
}
}, [agentMap, conversation?.agent_id, conversation?.endpoint, message?.model]);
const isSubmitting = useMemo(
() => (isMultiMessage === true ? isSubmittingAdditional : isSubmittingRoot),
[isMultiMessage, isSubmittingAdditional, isSubmittingRoot],
@ -74,17 +101,20 @@ export default function useMessageActions(props: TMessageActions) {
const messageLabel = useMemo(() => {
if (message?.isCreatedByUser === true) {
return UsernameDisplay ? (user?.name ?? '') || user?.username : localize('com_user_message');
} else if (agent) {
return agent.name ?? 'Assistant';
} else if (assistant) {
return assistant.name ?? 'Assistant';
} else {
return message?.sender;
}
}, [message, assistant, UsernameDisplay, user, localize]);
}, [message, agent, assistant, UsernameDisplay, user, localize]);
return {
ask,
edit,
index,
agent,
assistant,
enterEdit,
conversation,

View file

@ -103,7 +103,7 @@ export default function useMessageHelpers(props: TMessageProps) {
const modelKey = message?.model ?? '';
return agentMap ? agentMap[modelKey] : undefined;
}, [agentMap, conversation?.endpoint]);
}, [agentMap, conversation?.endpoint, message?.model]);
const regenerateMessage = () => {
if ((isSubmitting && isCreatedByUser === true) || !message) {

View file

@ -1,15 +1,11 @@
import { useMemo } from 'react';
import { MessageSquareQuote, ArrowRightToLine, Settings2, Bookmark } from 'lucide-react';
import {
ArrowRightToLine,
MessageSquareQuote,
Bookmark,
// Settings2,
} from 'lucide-react';
import {
EModelEndpoint,
isAssistantsEndpoint,
isAgentsEndpoint,
PermissionTypes,
paramEndpoints,
EModelEndpoint,
Permissions,
} from 'librechat-data-provider';
import type { TConfig, TInterfaceConfig } from 'librechat-data-provider';
@ -18,7 +14,7 @@ import BookmarkPanel from '~/components/SidePanel/Bookmarks/BookmarkPanel';
import PanelSwitch from '~/components/SidePanel/Builder/PanelSwitch';
import AgentPanelSwitch from '~/components/SidePanel/Agents/AgentPanelSwitch';
import PromptsAccordion from '~/components/Prompts/PromptsAccordion';
// import Parameters from '~/components/SidePanel/Parameters/Panel';
import Parameters from '~/components/SidePanel/Parameters/Panel';
import FilesPanel from '~/components/SidePanel/Files/Panel';
import { Blocks, AttachmentIcon } from '~/components/svg';
import { useHasAccess } from '~/hooks';
@ -54,7 +50,7 @@ export default function useSideNavLinks({
assistants &&
assistants.disableBuilder !== true &&
keyProvided &&
interfaceConfig.parameters
interfaceConfig.parameters === true
) {
links.push({
title: 'com_sidepanel_assistant_builder',
@ -70,7 +66,7 @@ export default function useSideNavLinks({
agents &&
// agents.disableBuilder !== true &&
keyProvided &&
interfaceConfig.parameters
interfaceConfig.parameters === true
) {
links.push({
title: 'com_sidepanel_agent_builder',
@ -91,6 +87,16 @@ export default function useSideNavLinks({
});
}
if (interfaceConfig.parameters === true && paramEndpoints.has(endpoint ?? '') && keyProvided) {
links.push({
title: 'com_sidepanel_parameters',
label: '',
icon: Settings2,
id: 'parameters',
Component: Parameters,
});
}
links.push({
title: 'com_sidepanel_attach_files',
label: '',
@ -119,13 +125,14 @@ export default function useSideNavLinks({
return links;
}, [
assistants,
agents,
keyProvided,
hidePanel,
endpoint,
interfaceConfig.parameters,
keyProvided,
assistants,
endpoint,
agents,
hasAccessToPrompts,
hasAccessToBookmarks,
hidePanel,
]);
return Links;

View file

@ -117,8 +117,8 @@ export default function useSSE(
};
createdHandler(data, { ...submission, userMessage } as EventSubmission);
} else if (data.event) {
stepHandler(data);
} else if (data.event != null) {
stepHandler(data, { ...submission, userMessage } as EventSubmission);
} else if (data.sync != null) {
const runId = v4();
setActiveRunId(runId);

View file

@ -1,6 +1,12 @@
import { useCallback, useRef } from 'react';
import { StepTypes, ContentTypes, ToolCallTypes } from 'librechat-data-provider';
import type { Agents, PartMetadata, TMessage } from 'librechat-data-provider';
import type {
Agents,
PartMetadata,
TMessage,
TMessageContentParts,
EventSubmission,
} from 'librechat-data-provider';
import { getNonEmptyValue } from 'librechat-data-provider';
type TUseStepHandler = {
@ -13,8 +19,17 @@ type TStepEvent = {
data: Agents.MessageDeltaEvent | Agents.RunStep | Agents.ToolEndEvent;
};
type MessageDeltaUpdate = { type: ContentTypes.TEXT; text: string; tool_call_ids?: string[] };
type AllContentTypes =
| ContentTypes.TEXT
| ContentTypes.TOOL_CALL
| ContentTypes.IMAGE_FILE
| ContentTypes.IMAGE_URL
| ContentTypes.ERROR;
export default function useStepHandler({ setMessages, getMessages }: TUseStepHandler) {
const toolCallIdMap = useRef(new Map<string, string>());
const toolCallIdMap = useRef(new Map<string, string | undefined>());
const messageMap = useRef(new Map<string, TMessage>());
const stepMap = useRef(new Map<string, Agents.RunStep>());
@ -24,41 +39,53 @@ export default function useStepHandler({ setMessages, getMessages }: TUseStepHan
contentPart: Agents.MessageContentComplex,
finalUpdate = false,
) => {
if (!contentPart.type) {
const contentType = contentPart.type ?? '';
if (!contentType) {
console.warn('No content type found in content part');
return message;
}
const updatedContent = [...(message.content || [])];
const updatedContent = [...(message.content || [])] as Array<
Partial<TMessageContentParts> | undefined
>;
if (!updatedContent[index]) {
updatedContent[index] = { type: contentPart.type };
updatedContent[index] = { type: contentPart.type as AllContentTypes };
}
if (
contentPart.type.startsWith(ContentTypes.TEXT) &&
contentType.startsWith(ContentTypes.TEXT) &&
ContentTypes.TEXT in contentPart &&
typeof contentPart.text === 'string'
) {
const currentContent = updatedContent[index] as { type: ContentTypes.TEXT; text: string };
updatedContent[index] = {
const currentContent = updatedContent[index] as MessageDeltaUpdate;
const update: MessageDeltaUpdate = {
type: ContentTypes.TEXT,
text: (currentContent.text || '') + contentPart.text,
};
} else if (contentPart.type === 'image_url' && 'image_url' in contentPart) {
const currentContent = updatedContent[index] as { type: 'image_url'; image_url: string };
if (contentPart.tool_call_ids != null) {
update.tool_call_ids = contentPart.tool_call_ids;
}
updatedContent[index] = update;
} else if (contentType === ContentTypes.IMAGE_URL && 'image_url' in contentPart) {
const currentContent = updatedContent[index] as {
type: ContentTypes.IMAGE_URL;
image_url: string;
};
updatedContent[index] = {
...currentContent,
};
} else if (contentPart.type === ContentTypes.TOOL_CALL && 'tool_call' in contentPart) {
const existingContent = updatedContent[index] as Agents.ToolCallContent;
} else if (contentType === ContentTypes.TOOL_CALL && 'tool_call' in contentPart) {
const existingContent = updatedContent[index] as Agents.ToolCallContent | undefined;
const existingToolCall = existingContent?.tool_call;
const toolCallArgs = (contentPart.tool_call.args as unknown as string | undefined) ?? '';
const args = finalUpdate
? contentPart.tool_call.args
: (existingContent?.tool_call?.args || '') + (contentPart.tool_call.args || '');
: (existingToolCall?.args ?? '') + toolCallArgs;
const id = getNonEmptyValue([contentPart.tool_call.id, existingContent?.tool_call?.id]) ?? '';
const name =
getNonEmptyValue([contentPart.tool_call.name, existingContent?.tool_call?.name]) ?? '';
const id = getNonEmptyValue([contentPart.tool_call.id, existingToolCall?.id]) ?? '';
const name = getNonEmptyValue([contentPart.tool_call.name, existingToolCall?.name]) ?? '';
const newToolCall: Agents.ToolCall & PartMetadata = {
id,
@ -78,16 +105,17 @@ export default function useStepHandler({ setMessages, getMessages }: TUseStepHan
};
}
return { ...message, content: updatedContent };
return { ...message, content: updatedContent as TMessageContentParts[] };
};
return useCallback(
({ event, data }: TStepEvent) => {
({ event, data }: TStepEvent, submission: EventSubmission) => {
const messages = getMessages() || [];
const { userMessage } = submission;
if (event === 'on_run_step') {
const runStep = data as Agents.RunStep;
const responseMessageId = runStep.runId;
const responseMessageId = runStep.runId ?? '';
if (!responseMessageId) {
console.warn('No message id found in run step event');
return;
@ -98,12 +126,11 @@ export default function useStepHandler({ setMessages, getMessages }: TUseStepHan
if (!response) {
const responseMessage = messages[messages.length - 1] as TMessage;
const userMessage = messages[messages.length - 2];
response = {
...responseMessage,
parentMessageId: userMessage?.messageId,
conversationId: userMessage?.conversationId,
parentMessageId: userMessage.messageId,
conversationId: userMessage.conversationId,
messageId: responseMessageId,
content: [],
};
@ -115,20 +142,23 @@ export default function useStepHandler({ setMessages, getMessages }: TUseStepHan
// Store tool call IDs if present
if (runStep.stepDetails.type === StepTypes.TOOL_CALLS) {
runStep.stepDetails.tool_calls.forEach((toolCall) => {
if ('id' in toolCall && toolCall.id) {
toolCallIdMap.current.set(runStep.id, toolCall.id);
const toolCallId = toolCall.id ?? '';
if ('id' in toolCall && toolCallId) {
toolCallIdMap.current.set(runStep.id, toolCallId);
}
});
}
} else if (event === 'on_message_delta') {
const messageDelta = data as Agents.MessageDeltaEvent;
const runStep = stepMap.current.get(messageDelta.id);
if (!runStep || !runStep.runId) {
const responseMessageId = runStep?.runId ?? '';
if (!runStep || !responseMessageId) {
console.warn('No run step or runId found for message delta event');
return;
}
const response = messageMap.current.get(runStep.runId);
const response = messageMap.current.get(responseMessageId);
if (response && messageDelta.delta.content) {
const contentPart = Array.isArray(messageDelta.delta.content)
? messageDelta.delta.content[0]
@ -136,19 +166,21 @@ export default function useStepHandler({ setMessages, getMessages }: TUseStepHan
const updatedResponse = updateContent(response, runStep.index, contentPart);
messageMap.current.set(runStep.runId, updatedResponse);
messageMap.current.set(responseMessageId, updatedResponse);
const currentMessages = getMessages() || [];
setMessages([...currentMessages.slice(0, -1), updatedResponse]);
}
} else if (event === 'on_run_step_delta') {
const runStepDelta = data as Agents.RunStepDeltaEvent;
const runStep = stepMap.current.get(runStepDelta.id);
if (!runStep || !runStep.runId) {
const responseMessageId = runStep?.runId ?? '';
if (!runStep || !responseMessageId) {
console.warn('No run step or runId found for run step delta event');
return;
}
const response = messageMap.current.get(runStep.runId);
const response = messageMap.current.get(responseMessageId);
if (
response &&
runStepDelta.delta.type === StepTypes.TOOL_CALLS &&
@ -157,13 +189,13 @@ export default function useStepHandler({ setMessages, getMessages }: TUseStepHan
let updatedResponse = { ...response };
runStepDelta.delta.tool_calls.forEach((toolCallDelta) => {
const toolCallId = toolCallIdMap.current.get(runStepDelta.id) || '';
const toolCallId = toolCallIdMap.current.get(runStepDelta.id) ?? '';
const contentPart: Agents.MessageContentComplex = {
type: ContentTypes.TOOL_CALL,
tool_call: {
name: toolCallDelta.name ?? '',
args: toolCallDelta.args || '',
args: toolCallDelta.args ?? '',
id: toolCallId,
},
};
@ -171,7 +203,7 @@ export default function useStepHandler({ setMessages, getMessages }: TUseStepHan
updatedResponse = updateContent(updatedResponse, runStep.index, contentPart);
});
messageMap.current.set(runStep.runId, updatedResponse);
messageMap.current.set(responseMessageId, updatedResponse);
const updatedMessages = messages.map((msg) =>
msg.messageId === runStep.runId ? updatedResponse : msg,
);
@ -184,12 +216,14 @@ export default function useStepHandler({ setMessages, getMessages }: TUseStepHan
const { id: stepId } = result;
const runStep = stepMap.current.get(stepId);
if (!runStep || !runStep.runId) {
const responseMessageId = runStep?.runId ?? '';
if (!runStep || !responseMessageId) {
console.warn('No run step or runId found for completed tool call event');
return;
}
const response = messageMap.current.get(runStep.runId);
const response = messageMap.current.get(responseMessageId);
if (response) {
let updatedResponse = { ...response };
@ -200,7 +234,7 @@ export default function useStepHandler({ setMessages, getMessages }: TUseStepHan
updatedResponse = updateContent(updatedResponse, runStep.index, contentPart, true);
messageMap.current.set(runStep.runId, updatedResponse);
messageMap.current.set(responseMessageId, updatedResponse);
const updatedMessages = messages.map((msg) =>
msg.messageId === runStep.runId ? updatedResponse : msg,
);

View file

@ -3,7 +3,7 @@ import { EModelEndpoint, isAssistantsEndpoint } from 'librechat-data-provider';
type TUseGenerations = {
endpoint?: string;
message: TMessage;
message?: TMessage;
isSubmitting: boolean;
isEditing?: boolean;
latestMessage: TMessage | null;
@ -16,15 +16,25 @@ export default function useGenerationsByLatest({
isEditing = false,
latestMessage,
}: TUseGenerations) {
const { error, messageId, searchResult, finish_reason, isCreatedByUser } = message ?? {};
const isEditableEndpoint = !![
EModelEndpoint.openAI,
EModelEndpoint.custom,
EModelEndpoint.google,
EModelEndpoint.anthropic,
EModelEndpoint.gptPlugins,
EModelEndpoint.azureOpenAI,
].find((e) => e === endpoint);
const {
messageId,
searchResult = false,
error = false,
finish_reason = '',
isCreatedByUser = false,
} = message ?? {};
const isEditableEndpoint = Boolean(
[
EModelEndpoint.openAI,
EModelEndpoint.custom,
EModelEndpoint.google,
EModelEndpoint.agents,
EModelEndpoint.bedrock,
EModelEndpoint.anthropic,
EModelEndpoint.gptPlugins,
EModelEndpoint.azureOpenAI,
].find((e) => e === endpoint),
);
const continueSupported =
latestMessage?.messageId === messageId &&
@ -34,18 +44,20 @@ export default function useGenerationsByLatest({
!searchResult &&
isEditableEndpoint;
const branchingSupported =
// 5/21/23: Bing is allowing editing and Message regenerating
!![
const branchingSupported = Boolean(
[
EModelEndpoint.azureOpenAI,
EModelEndpoint.openAI,
EModelEndpoint.custom,
EModelEndpoint.agents,
EModelEndpoint.bedrock,
EModelEndpoint.chatGPTBrowser,
EModelEndpoint.google,
EModelEndpoint.bingAI,
EModelEndpoint.gptPlugins,
EModelEndpoint.anthropic,
].find((e) => e === endpoint);
].find((e) => e === endpoint),
);
const regenerateEnabled =
!isCreatedByUser && !searchResult && !isEditing && !isSubmitting && branchingSupported;

View file

@ -5,7 +5,12 @@ import {
useGetEndpointsQuery,
} from 'librechat-data-provider/react-query';
import { useNavigate } from 'react-router-dom';
import { FileSources, LocalStorageKeys, isAssistantsEndpoint } from 'librechat-data-provider';
import {
FileSources,
LocalStorageKeys,
isAssistantsEndpoint,
paramEndpoints,
} from 'librechat-data-provider';
import { useRecoilState, useRecoilValue, useSetRecoilState, useRecoilCallback } from 'recoil';
import type {
TPreset,
@ -67,7 +72,7 @@ const useNewConvo = (index = 0) => {
) => {
const modelsConfig = modelsData ?? modelsQuery.data;
const { endpoint = null } = conversation;
const buildDefaultConversation = endpoint === null || buildDefault;
const buildDefaultConversation = (endpoint === null || buildDefault) ?? false;
const activePreset =
// use default preset only when it's defined,
// preset is not provided,
@ -95,27 +100,24 @@ const useNewConvo = (index = 0) => {
const isAssistantEndpoint = isAssistantsEndpoint(defaultEndpoint);
const assistants: AssistantListItem[] = assistantsListMap[defaultEndpoint] ?? [];
const currentAssistantId = conversation.assistant_id ?? '';
const currentAssistant = assistantsListMap[defaultEndpoint]?.[currentAssistantId] as
| AssistantListItem
| undefined;
if (
conversation.assistant_id &&
!assistantsListMap[defaultEndpoint]?.[conversation.assistant_id]
) {
if (currentAssistantId && !currentAssistant) {
conversation.assistant_id = undefined;
}
if (!conversation.assistant_id && isAssistantEndpoint) {
if (!currentAssistantId && isAssistantEndpoint) {
conversation.assistant_id =
localStorage.getItem(
`${LocalStorageKeys.ASST_ID_PREFIX}${index}${defaultEndpoint}`,
) ?? assistants[0]?.id;
}
if (
conversation.assistant_id &&
isAssistantEndpoint &&
conversation.conversationId === 'new'
) {
const assistant = assistants.find((asst) => asst.id === conversation.assistant_id);
if (currentAssistantId && isAssistantEndpoint && conversation.conversationId === 'new') {
const assistant = assistants.find((asst) => asst.id === currentAssistantId);
conversation.model = assistant?.model;
updateLastSelectedModel({
endpoint: defaultEndpoint,
@ -123,7 +125,7 @@ const useNewConvo = (index = 0) => {
});
}
if (conversation.assistant_id && !isAssistantEndpoint) {
if (currentAssistantId && !isAssistantEndpoint) {
conversation.assistant_id = undefined;
}
@ -136,17 +138,17 @@ const useNewConvo = (index = 0) => {
});
}
if (!keepAddedConvos) {
if (!(keepAddedConvos ?? false)) {
clearAllConversations(true);
}
setConversation(conversation);
setSubmission({} as TSubmission);
if (!keepLatestMessage) {
if (!(keepLatestMessage ?? false)) {
clearAllLatestMessages();
}
if (conversation.conversationId === 'new' && !modelsData) {
const appTitle = localStorage.getItem(LocalStorageKeys.APP_TITLE);
const appTitle = localStorage.getItem(LocalStorageKeys.APP_TITLE) ?? '';
if (appTitle) {
document.title = appTitle;
}
@ -166,7 +168,7 @@ const useNewConvo = (index = 0) => {
const newConversation = useCallback(
({
template = {},
template: _template = {},
preset: _preset,
modelsData,
buildDefault = true,
@ -182,6 +184,16 @@ const useNewConvo = (index = 0) => {
} = {}) => {
pauseGlobalAudio();
const templateConvoId = _template.conversationId ?? '';
const isParamEndpoint =
paramEndpoints.has(_template.endpoint ?? '') ||
paramEndpoints.has(_preset?.endpoint ?? '') ||
paramEndpoints.has(_template.endpointType ?? '');
const template =
isParamEndpoint && templateConvoId && templateConvoId === 'new'
? { endpoint: _template.endpoint }
: _template;
const conversation = {
conversationId: 'new',
title: 'New Chat',
@ -193,7 +205,12 @@ const useNewConvo = (index = 0) => {
let preset = _preset;
const defaultModelSpec = getDefaultModelSpec(startupConfig?.modelSpecs?.list);
if (!preset && startupConfig && startupConfig.modelSpecs?.prioritize && defaultModelSpec) {
if (
!preset &&
startupConfig &&
startupConfig.modelSpecs?.prioritize === true &&
defaultModelSpec
) {
preset = {
...defaultModelSpec.preset,
iconURL: getModelSpecIconURL(defaultModelSpec),
@ -203,10 +220,17 @@ const useNewConvo = (index = 0) => {
if (conversation.conversationId === 'new' && !modelsData) {
const filesToDelete = Array.from(files.values())
.filter((file) => file.filepath && file.source && !file.embedded && file.temp_file_id)
.filter(
(file) =>
file.filepath != null &&
file.filepath !== '' &&
file.source &&
!(file.embedded ?? false) &&
file.temp_file_id,
)
.map((file) => ({
file_id: file.file_id,
embedded: !!file.embedded,
embedded: !!(file.embedded ?? false),
filepath: file.filepath as string,
source: file.source as FileSources, // Ensure that the source is of type FileSources
}));