2024-06-25 03:02:38 -04:00
|
|
|
import { v4 } from 'uuid';
|
2024-08-30 00:14:37 -04:00
|
|
|
import { useCallback, useRef } from 'react';
|
2024-08-18 19:02:46 -04:00
|
|
|
import { useSetRecoilState } from 'recoil';
|
2024-06-25 03:02:38 -04:00
|
|
|
import { useParams } from 'react-router-dom';
|
|
|
|
|
import { useQueryClient } from '@tanstack/react-query';
|
|
|
|
|
import {
|
|
|
|
|
QueryKeys,
|
|
|
|
|
Constants,
|
|
|
|
|
EndpointURLs,
|
|
|
|
|
tPresetSchema,
|
|
|
|
|
tMessageSchema,
|
|
|
|
|
tConvoUpdateSchema,
|
|
|
|
|
} from 'librechat-data-provider';
|
|
|
|
|
import type {
|
|
|
|
|
TMessage,
|
|
|
|
|
TConversation,
|
2024-08-21 18:18:45 -04:00
|
|
|
EventSubmission,
|
2024-06-25 03:02:38 -04:00
|
|
|
ConversationData,
|
|
|
|
|
} from 'librechat-data-provider';
|
|
|
|
|
import type { SetterOrUpdater, Resetter } from 'recoil';
|
2024-08-19 03:51:17 -04:00
|
|
|
import type { TResData, TFinalResData, ConvoGenerator } from '~/common';
|
🚧 chore: merge latest dev build to main repo (#3844)
* agents - phase 1 (#30)
* chore: copy assistant files
* feat: frontend and data-provider
* feat: backend get endpoint test
* fix(MessageEndpointIcon): switched to AgentName and AgentAvatar
* fix: small fixes
* fix: agent endpoint config
* fix: show Agent Builder
* chore: install agentus
* chore: initial scaffolding for agents
* fix: updated Assistant logic to Agent Logic for some Agent components
* WIP first pass, demo of agent package
* WIP: initial backend infra for agents
* fix: agent list error
* wip: agents routing
* chore: Refactor useSSE hook to handle different data events
* wip: correctly emit events
* chore: Update @librechat/agentus npm dependency to version 1.0.9
* remove comment
* first pass: streaming agent text
* chore: Remove @librechat/agentus root-level workspace npm dependency
* feat: Agent Schema and Model
* fix: content handling fixes
* fix: content message save
* WIP: new content data
* fix: run step issue with tool calls
* chore: Update @librechat/agentus npm dependency to version 1.1.5
* feat: update controller and agent routes
* wip: initial backend tool and tool error handling support
* wip: tool chunks
* chore: Update @librechat/agentus npm dependency to version 1.1.7
* chore: update tool_call typing, add test conditions and logs
* fix: create agent
* fix: create agent
* first pass: render completed content parts
* fix: remove logging, fix step handler typing
* chore: Update @librechat/agentus npm dependency to version 1.1.9
* refactor: cleanup maps on unmount
* chore: Update BaseClient.js to safely count tokens for string, number, and boolean values
* fix: support subsequent messages with tool_calls
* chore: export order
* fix: select agent
* fix: tool call types and handling
* chore: switch to anthropic for testing
* fix: AgentSelect
* refactor: experimental: OpenAIClient to use array for intermediateReply
* fix(useSSE): revert old condition for streaming legacy client tokens
* fix: lint
* revert `agent_id` to `id`
* chore: update localization keys for agent-related components
* feat: zod schema handling for actions
* refactor(actions): if no params, no zodSchema
* chore: Update @librechat/agentus npm dependency to version 1.2.1
* feat: first pass, actions
* refactor: empty schema for actions without params
* feat: Update createRun function to accept additional options
* fix: message payload formatting; feat: add more client options
* fix: ToolCall component rendering when action has no args but has output
* refactor(ToolCall): allow non-stringy args
* WIP: first pass, correctly formatted tool_calls between providers
* refactor: Remove duplicate import of 'roles' module
* refactor: Exclude 'vite.config.ts' from TypeScript compilation
* refactor: fix agent related types
> - no need to use endpoint/model fields for identifying agent metadata
> - add `provider` distinction for agent-configured 'endpoint'
- no need for agent-endpoint map
- reduce complexity of tools as functions into tools as string[]
- fix types related to above changes
- reduce unnecessary variables for queries/mutations and corresponding react-query keys
* refactor: Add tools and tool_kwargs fields to agent schema
* refactor: Remove unused code and update dependencies
* refactor: Update updateAgentHandler to use req.body directly
* refactor: Update AgentSelect component to use localized hooks
* refactor: Update agent schema to include tools and provider fields
* refactor(AgentPanel): add scrollbar gutter, add provider field to form, fix agent schema required values
* refactor: Update AgentSwitcher component to use selectedAgentId instead of selectedAgent
* refactor: Update AgentPanel component to include alternateName import and defaultAgentFormValues
* refactor(SelectDropDown): allow setting value as option while still supporting legacy usage (string values only)
* refactor: SelectDropdown changes - Only necessary when the available values are objects with label/value fields and the selected value is expected to be a string.
* refactor: TypeError issues and handle provider as option
* feat: Add placeholder for provider selection in AgentPanel component
* refactor: Update agent schema to include author and provider fields
* fix: show expected 'create agent' placeholder when creating agent
* chore: fix localization strings, hide capabilities form for now
* chore: typing
* refactor: import order and use compact agents schema for now
* chore: typing
* refactor: Update AgentForm type to use AgentCapabilities
* fix agent form agent selection issues
* feat: responsive agent selection
* fix: Handle cancelled fetch in useSelectAgent hook
* fix: reset agent form on accordion close/open
* feat: Add agent_id to default conversation for agents endpoint
* feat: agents endpoint request handling
* refactor: reset conversation model on agent select
* refactor: add `additional_instructions` to conversation schema, organize other fields
* chore: casing
* chore: types
* refactor(loadAgentTools): explicitly pass agent_id, do not pass `model` to loadAgentTools for now, load action sets by agent_id
* WIP: initial draft of real agent client initialization
* WIP: first pass, anthropic agent requests
* feat: remember last selected agent
* feat: openai and azure connected
* fix: prioritize agent model for runs unless an explicit override model is passed from client
* feat: Agent Actions
* fix: save agent id to convo
* feat: model panel (#29)
* feat: model panel
* bring back comments
* fix: method still null
* fix: AgentPanel FormContext
* feat: add more parameters
* fix: style issues; refactor: Agent Controller
* fix: cherry-pick
* fix: Update AgentAvatar component to use AssistantIcon instead of BrainCircuit
* feat: OGDialog for delete agent; feat(assistant): update Agent types, introduced `model_parameters`
* feat: icon and general `model_parameters` update
* feat: use react-hook-form better
* fix: agent builder form reset issue when switching panels
* refactor: modularize agent builder form
---------
Co-authored-by: Danny Avila <danny@librechat.ai>
* fix: AgentPanel and ModelPanel type issues and use `useFormContext` and `watch` instead of `methods` directly and `useWatch`.
* fix: tool call issues due to invalid input (anthropic) of empty string
* fix: handle empty text in Part component
---------
Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>
* refactor: remove form ModelPanel and fixed nested ternary expressions in AgentConfig
* fix: Model Parameters not saved correctly
* refactor: remove console log
* feat: avatar upload and get for Agents (#36)
Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>
* chore: update to public package
* fix: typing, optional chaining
* fix: cursor not showing for content parts
* chore: conditionally enable agents
* ci: fix azure test
* ci: fix frontend tests, fix eslint api
* refactor: Remove unused errorContentPart variable
* continue of the agent message PR (#40)
* last fixes
* fix: agentMap
* pr merge test (#41)
* fix: model icon not fetching correctly
* remove console logs
* feat: agent name
* refactor: pass documentsMap as a prop to allow re-render of assistant form
* refactor: pass documentsMap as a prop to allow re-render of assistant form
* chore: Bump version to 0.7.419
* fix: TypeError: Cannot read properties of undefined (reading 'id')
* refactor: update AgentSwitcher component to use ControlCombobox instead of Combobox
---------
Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>
2024-08-31 16:33:51 -04:00
|
|
|
import type { TGenTitleMutation } from '~/data-provider';
|
2024-06-25 03:02:38 -04:00
|
|
|
import {
|
2024-08-09 02:11:56 -04:00
|
|
|
scrollToEnd,
|
2024-06-25 03:02:38 -04:00
|
|
|
addConversation,
|
2024-09-10 19:00:27 -04:00
|
|
|
getAllContentText,
|
2024-06-25 03:02:38 -04:00
|
|
|
deleteConversation,
|
|
|
|
|
updateConversation,
|
|
|
|
|
getConversationById,
|
|
|
|
|
} from '~/utils';
|
2024-09-30 17:17:57 -04:00
|
|
|
import useAttachmentHandler from '~/hooks/SSE/useAttachmentHandler';
|
2024-06-25 03:02:38 -04:00
|
|
|
import useContentHandler from '~/hooks/SSE/useContentHandler';
|
🚧 chore: merge latest dev build to main repo (#3844)
* agents - phase 1 (#30)
* chore: copy assistant files
* feat: frontend and data-provider
* feat: backend get endpoint test
* fix(MessageEndpointIcon): switched to AgentName and AgentAvatar
* fix: small fixes
* fix: agent endpoint config
* fix: show Agent Builder
* chore: install agentus
* chore: initial scaffolding for agents
* fix: updated Assistant logic to Agent Logic for some Agent components
* WIP first pass, demo of agent package
* WIP: initial backend infra for agents
* fix: agent list error
* wip: agents routing
* chore: Refactor useSSE hook to handle different data events
* wip: correctly emit events
* chore: Update @librechat/agentus npm dependency to version 1.0.9
* remove comment
* first pass: streaming agent text
* chore: Remove @librechat/agentus root-level workspace npm dependency
* feat: Agent Schema and Model
* fix: content handling fixes
* fix: content message save
* WIP: new content data
* fix: run step issue with tool calls
* chore: Update @librechat/agentus npm dependency to version 1.1.5
* feat: update controller and agent routes
* wip: initial backend tool and tool error handling support
* wip: tool chunks
* chore: Update @librechat/agentus npm dependency to version 1.1.7
* chore: update tool_call typing, add test conditions and logs
* fix: create agent
* fix: create agent
* first pass: render completed content parts
* fix: remove logging, fix step handler typing
* chore: Update @librechat/agentus npm dependency to version 1.1.9
* refactor: cleanup maps on unmount
* chore: Update BaseClient.js to safely count tokens for string, number, and boolean values
* fix: support subsequent messages with tool_calls
* chore: export order
* fix: select agent
* fix: tool call types and handling
* chore: switch to anthropic for testing
* fix: AgentSelect
* refactor: experimental: OpenAIClient to use array for intermediateReply
* fix(useSSE): revert old condition for streaming legacy client tokens
* fix: lint
* revert `agent_id` to `id`
* chore: update localization keys for agent-related components
* feat: zod schema handling for actions
* refactor(actions): if no params, no zodSchema
* chore: Update @librechat/agentus npm dependency to version 1.2.1
* feat: first pass, actions
* refactor: empty schema for actions without params
* feat: Update createRun function to accept additional options
* fix: message payload formatting; feat: add more client options
* fix: ToolCall component rendering when action has no args but has output
* refactor(ToolCall): allow non-stringy args
* WIP: first pass, correctly formatted tool_calls between providers
* refactor: Remove duplicate import of 'roles' module
* refactor: Exclude 'vite.config.ts' from TypeScript compilation
* refactor: fix agent related types
> - no need to use endpoint/model fields for identifying agent metadata
> - add `provider` distinction for agent-configured 'endpoint'
- no need for agent-endpoint map
- reduce complexity of tools as functions into tools as string[]
- fix types related to above changes
- reduce unnecessary variables for queries/mutations and corresponding react-query keys
* refactor: Add tools and tool_kwargs fields to agent schema
* refactor: Remove unused code and update dependencies
* refactor: Update updateAgentHandler to use req.body directly
* refactor: Update AgentSelect component to use localized hooks
* refactor: Update agent schema to include tools and provider fields
* refactor(AgentPanel): add scrollbar gutter, add provider field to form, fix agent schema required values
* refactor: Update AgentSwitcher component to use selectedAgentId instead of selectedAgent
* refactor: Update AgentPanel component to include alternateName import and defaultAgentFormValues
* refactor(SelectDropDown): allow setting value as option while still supporting legacy usage (string values only)
* refactor: SelectDropdown changes - Only necessary when the available values are objects with label/value fields and the selected value is expected to be a string.
* refactor: TypeError issues and handle provider as option
* feat: Add placeholder for provider selection in AgentPanel component
* refactor: Update agent schema to include author and provider fields
* fix: show expected 'create agent' placeholder when creating agent
* chore: fix localization strings, hide capabilities form for now
* chore: typing
* refactor: import order and use compact agents schema for now
* chore: typing
* refactor: Update AgentForm type to use AgentCapabilities
* fix agent form agent selection issues
* feat: responsive agent selection
* fix: Handle cancelled fetch in useSelectAgent hook
* fix: reset agent form on accordion close/open
* feat: Add agent_id to default conversation for agents endpoint
* feat: agents endpoint request handling
* refactor: reset conversation model on agent select
* refactor: add `additional_instructions` to conversation schema, organize other fields
* chore: casing
* chore: types
* refactor(loadAgentTools): explicitly pass agent_id, do not pass `model` to loadAgentTools for now, load action sets by agent_id
* WIP: initial draft of real agent client initialization
* WIP: first pass, anthropic agent requests
* feat: remember last selected agent
* feat: openai and azure connected
* fix: prioritize agent model for runs unless an explicit override model is passed from client
* feat: Agent Actions
* fix: save agent id to convo
* feat: model panel (#29)
* feat: model panel
* bring back comments
* fix: method still null
* fix: AgentPanel FormContext
* feat: add more parameters
* fix: style issues; refactor: Agent Controller
* fix: cherry-pick
* fix: Update AgentAvatar component to use AssistantIcon instead of BrainCircuit
* feat: OGDialog for delete agent; feat(assistant): update Agent types, introduced `model_parameters`
* feat: icon and general `model_parameters` update
* feat: use react-hook-form better
* fix: agent builder form reset issue when switching panels
* refactor: modularize agent builder form
---------
Co-authored-by: Danny Avila <danny@librechat.ai>
* fix: AgentPanel and ModelPanel type issues and use `useFormContext` and `watch` instead of `methods` directly and `useWatch`.
* fix: tool call issues due to invalid input (anthropic) of empty string
* fix: handle empty text in Part component
---------
Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>
* refactor: remove form ModelPanel and fixed nested ternary expressions in AgentConfig
* fix: Model Parameters not saved correctly
* refactor: remove console log
* feat: avatar upload and get for Agents (#36)
Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>
* chore: update to public package
* fix: typing, optional chaining
* fix: cursor not showing for content parts
* chore: conditionally enable agents
* ci: fix azure test
* ci: fix frontend tests, fix eslint api
* refactor: Remove unused errorContentPart variable
* continue of the agent message PR (#40)
* last fixes
* fix: agentMap
* pr merge test (#41)
* fix: model icon not fetching correctly
* remove console logs
* feat: agent name
* refactor: pass documentsMap as a prop to allow re-render of assistant form
* refactor: pass documentsMap as a prop to allow re-render of assistant form
* chore: Bump version to 0.7.419
* fix: TypeError: Cannot read properties of undefined (reading 'id')
* refactor: update AgentSwitcher component to use ControlCombobox instead of Combobox
---------
Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>
2024-08-31 16:33:51 -04:00
|
|
|
import useStepHandler from '~/hooks/SSE/useStepHandler';
|
2024-06-25 03:02:38 -04:00
|
|
|
import { useAuthContext } from '~/hooks/AuthContext';
|
2024-09-10 19:00:27 -04:00
|
|
|
import { MESSAGE_UPDATE_INTERVAL } from '~/common';
|
2024-08-13 03:04:27 -04:00
|
|
|
import { useLiveAnnouncer } from '~/Providers';
|
2024-08-18 19:02:46 -04:00
|
|
|
import store from '~/store';
|
2024-06-25 03:02:38 -04:00
|
|
|
|
|
|
|
|
type TSyncData = {
|
|
|
|
|
sync: boolean;
|
|
|
|
|
thread_id: string;
|
|
|
|
|
messages?: TMessage[];
|
|
|
|
|
requestMessage: TMessage;
|
|
|
|
|
responseMessage: TMessage;
|
|
|
|
|
conversationId: string;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export type EventHandlerParams = {
|
|
|
|
|
isAddedRequest?: boolean;
|
|
|
|
|
genTitle?: TGenTitleMutation;
|
|
|
|
|
setCompleted: React.Dispatch<React.SetStateAction<Set<unknown>>>;
|
|
|
|
|
setMessages: (messages: TMessage[]) => void;
|
|
|
|
|
getMessages: () => TMessage[] | undefined;
|
|
|
|
|
setIsSubmitting: SetterOrUpdater<boolean>;
|
|
|
|
|
setConversation?: SetterOrUpdater<TConversation | null>;
|
|
|
|
|
newConversation?: ConvoGenerator;
|
|
|
|
|
setShowStopButton: SetterOrUpdater<boolean>;
|
|
|
|
|
resetLatestMessage?: Resetter;
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
export default function useEventHandlers({
|
|
|
|
|
genTitle,
|
|
|
|
|
setMessages,
|
|
|
|
|
getMessages,
|
|
|
|
|
setCompleted,
|
2024-09-30 17:17:57 -04:00
|
|
|
isAddedRequest = false,
|
2024-06-25 03:02:38 -04:00
|
|
|
setConversation,
|
|
|
|
|
setIsSubmitting,
|
|
|
|
|
newConversation,
|
|
|
|
|
setShowStopButton,
|
|
|
|
|
resetLatestMessage,
|
|
|
|
|
}: EventHandlerParams) {
|
|
|
|
|
const queryClient = useQueryClient();
|
2024-08-18 19:02:46 -04:00
|
|
|
const setAbortScroll = useSetRecoilState(store.abortScroll);
|
2024-08-30 00:14:37 -04:00
|
|
|
const { announcePolite } = useLiveAnnouncer();
|
2024-06-25 03:02:38 -04:00
|
|
|
|
2024-08-30 00:14:37 -04:00
|
|
|
const lastAnnouncementTimeRef = useRef(Date.now());
|
2024-06-25 03:02:38 -04:00
|
|
|
const { conversationId: paramId } = useParams();
|
|
|
|
|
const { token } = useAuthContext();
|
|
|
|
|
|
|
|
|
|
const contentHandler = useContentHandler({ setMessages, getMessages });
|
2024-09-10 19:00:27 -04:00
|
|
|
const stepHandler = useStepHandler({
|
|
|
|
|
setMessages,
|
|
|
|
|
getMessages,
|
|
|
|
|
announcePolite,
|
|
|
|
|
setIsSubmitting,
|
|
|
|
|
lastAnnouncementTimeRef,
|
|
|
|
|
});
|
2024-09-30 17:17:57 -04:00
|
|
|
const attachmentHandler = useAttachmentHandler();
|
2024-06-25 03:02:38 -04:00
|
|
|
|
|
|
|
|
const messageHandler = useCallback(
|
2024-08-21 18:18:45 -04:00
|
|
|
(data: string | undefined, submission: EventSubmission) => {
|
2024-06-25 03:02:38 -04:00
|
|
|
const {
|
|
|
|
|
messages,
|
|
|
|
|
userMessage,
|
|
|
|
|
plugin,
|
|
|
|
|
plugins,
|
|
|
|
|
initialResponse,
|
|
|
|
|
isRegenerate = false,
|
|
|
|
|
} = submission;
|
2024-08-13 03:04:27 -04:00
|
|
|
const text = data ?? '';
|
2024-08-26 15:34:46 -04:00
|
|
|
setIsSubmitting(true);
|
2024-08-30 00:14:37 -04:00
|
|
|
|
|
|
|
|
const currentTime = Date.now();
|
|
|
|
|
if (currentTime - lastAnnouncementTimeRef.current > MESSAGE_UPDATE_INTERVAL) {
|
|
|
|
|
announcePolite({ message: 'composing', isStatus: true });
|
|
|
|
|
lastAnnouncementTimeRef.current = currentTime;
|
2024-08-13 03:04:27 -04:00
|
|
|
}
|
2024-06-25 03:02:38 -04:00
|
|
|
|
|
|
|
|
if (isRegenerate) {
|
|
|
|
|
setMessages([
|
|
|
|
|
...messages,
|
|
|
|
|
{
|
|
|
|
|
...initialResponse,
|
2024-08-13 03:04:27 -04:00
|
|
|
text,
|
2024-06-25 03:02:38 -04:00
|
|
|
plugin: plugin ?? null,
|
|
|
|
|
plugins: plugins ?? [],
|
|
|
|
|
// unfinished: true
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
} else {
|
|
|
|
|
setMessages([
|
|
|
|
|
...messages,
|
|
|
|
|
userMessage,
|
|
|
|
|
{
|
|
|
|
|
...initialResponse,
|
2024-08-13 03:04:27 -04:00
|
|
|
text,
|
2024-06-25 03:02:38 -04:00
|
|
|
plugin: plugin ?? null,
|
|
|
|
|
plugins: plugins ?? [],
|
|
|
|
|
// unfinished: true
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
}
|
|
|
|
|
},
|
2024-08-26 15:34:46 -04:00
|
|
|
[setMessages, announcePolite, setIsSubmitting],
|
2024-06-25 03:02:38 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const cancelHandler = useCallback(
|
2024-08-21 18:18:45 -04:00
|
|
|
(data: TResData, submission: EventSubmission) => {
|
2024-06-25 03:02:38 -04:00
|
|
|
const { requestMessage, responseMessage, conversation } = data;
|
|
|
|
|
const { messages, isRegenerate = false } = submission;
|
|
|
|
|
|
2024-09-30 17:17:57 -04:00
|
|
|
const convoUpdate =
|
|
|
|
|
(conversation as TConversation | null) ?? (submission.conversation as TConversation);
|
2024-06-25 03:02:38 -04:00
|
|
|
|
|
|
|
|
// update the messages
|
|
|
|
|
if (isRegenerate) {
|
2024-09-30 17:17:57 -04:00
|
|
|
const messagesUpdate = (
|
|
|
|
|
[...messages, responseMessage] as Array<TMessage | undefined>
|
|
|
|
|
).filter((msg) => msg);
|
|
|
|
|
setMessages(messagesUpdate as TMessage[]);
|
2024-06-25 03:02:38 -04:00
|
|
|
} else {
|
2024-09-30 17:17:57 -04:00
|
|
|
const messagesUpdate = (
|
|
|
|
|
[...messages, requestMessage, responseMessage] as Array<TMessage | undefined>
|
|
|
|
|
).filter((msg) => msg);
|
|
|
|
|
setMessages(messagesUpdate as TMessage[]);
|
2024-06-25 03:02:38 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isNewConvo = conversation.conversationId !== submission.conversation.conversationId;
|
|
|
|
|
if (isNewConvo) {
|
|
|
|
|
queryClient.setQueryData<ConversationData>([QueryKeys.allConversations], (convoData) => {
|
|
|
|
|
if (!convoData) {
|
|
|
|
|
return convoData;
|
|
|
|
|
}
|
|
|
|
|
return deleteConversation(convoData, submission.conversation.conversationId as string);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// refresh title
|
2024-08-13 03:04:27 -04:00
|
|
|
if (genTitle && isNewConvo && requestMessage.parentMessageId === Constants.NO_PARENT) {
|
2024-06-25 03:02:38 -04:00
|
|
|
setTimeout(() => {
|
|
|
|
|
genTitle.mutate({ conversationId: convoUpdate.conversationId as string });
|
|
|
|
|
}, 2500);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (setConversation && !isAddedRequest) {
|
|
|
|
|
setConversation((prevState) => {
|
|
|
|
|
const update = {
|
|
|
|
|
...prevState,
|
|
|
|
|
...convoUpdate,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return update;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
},
|
|
|
|
|
[setMessages, setConversation, genTitle, isAddedRequest, queryClient, setIsSubmitting],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const syncHandler = useCallback(
|
2024-08-21 18:18:45 -04:00
|
|
|
(data: TSyncData, submission: EventSubmission) => {
|
2024-06-25 03:02:38 -04:00
|
|
|
const { conversationId, thread_id, responseMessage, requestMessage } = data;
|
|
|
|
|
const { initialResponse, messages: _messages, userMessage } = submission;
|
|
|
|
|
|
|
|
|
|
const messages = _messages.filter((msg) => msg.messageId !== userMessage.messageId);
|
|
|
|
|
|
|
|
|
|
setMessages([
|
|
|
|
|
...messages,
|
|
|
|
|
requestMessage,
|
|
|
|
|
{
|
|
|
|
|
...initialResponse,
|
|
|
|
|
...responseMessage,
|
|
|
|
|
},
|
|
|
|
|
]);
|
|
|
|
|
|
2024-08-30 00:14:37 -04:00
|
|
|
announcePolite({
|
2024-08-19 03:51:17 -04:00
|
|
|
message: 'start',
|
2024-08-30 00:14:37 -04:00
|
|
|
isStatus: true,
|
2024-08-19 03:51:17 -04:00
|
|
|
});
|
|
|
|
|
|
2024-06-25 03:02:38 -04:00
|
|
|
let update = {} as TConversation;
|
|
|
|
|
if (setConversation && !isAddedRequest) {
|
|
|
|
|
setConversation((prevState) => {
|
|
|
|
|
let title = prevState?.title;
|
|
|
|
|
const parentId = requestMessage.parentMessageId;
|
2024-09-30 17:17:57 -04:00
|
|
|
if (
|
|
|
|
|
parentId !== Constants.NO_PARENT &&
|
2025-02-09 18:05:31 +01:00
|
|
|
(title?.toLowerCase().includes('new chat') ?? false)
|
2024-09-30 17:17:57 -04:00
|
|
|
) {
|
2024-06-25 03:02:38 -04:00
|
|
|
const convos = queryClient.getQueryData<ConversationData>([QueryKeys.allConversations]);
|
|
|
|
|
const cachedConvo = getConversationById(convos, conversationId);
|
|
|
|
|
title = cachedConvo?.title;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update = tConvoUpdateSchema.parse({
|
|
|
|
|
...prevState,
|
|
|
|
|
conversationId,
|
|
|
|
|
thread_id,
|
|
|
|
|
title,
|
|
|
|
|
messages: [requestMessage.messageId, responseMessage.messageId],
|
|
|
|
|
}) as TConversation;
|
|
|
|
|
|
|
|
|
|
return update;
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
queryClient.setQueryData<ConversationData>([QueryKeys.allConversations], (convoData) => {
|
|
|
|
|
if (!convoData) {
|
|
|
|
|
return convoData;
|
|
|
|
|
}
|
|
|
|
|
if (requestMessage.parentMessageId === Constants.NO_PARENT) {
|
|
|
|
|
return addConversation(convoData, update);
|
|
|
|
|
} else {
|
|
|
|
|
return updateConversation(convoData, update);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else if (setConversation) {
|
|
|
|
|
setConversation((prevState) => {
|
|
|
|
|
update = tConvoUpdateSchema.parse({
|
|
|
|
|
...prevState,
|
|
|
|
|
conversationId,
|
|
|
|
|
thread_id,
|
|
|
|
|
messages: [requestMessage.messageId, responseMessage.messageId],
|
|
|
|
|
}) as TConversation;
|
|
|
|
|
return update;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setShowStopButton(true);
|
|
|
|
|
if (resetLatestMessage) {
|
|
|
|
|
resetLatestMessage();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[
|
|
|
|
|
queryClient,
|
2024-08-19 03:51:17 -04:00
|
|
|
setMessages,
|
2024-06-25 03:02:38 -04:00
|
|
|
isAddedRequest,
|
2024-08-30 00:14:37 -04:00
|
|
|
announcePolite,
|
2024-08-19 03:51:17 -04:00
|
|
|
setConversation,
|
2024-06-25 03:02:38 -04:00
|
|
|
setShowStopButton,
|
|
|
|
|
resetLatestMessage,
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const createdHandler = useCallback(
|
2024-08-21 18:18:45 -04:00
|
|
|
(data: TResData, submission: EventSubmission) => {
|
2025-02-06 08:11:47 -08:00
|
|
|
const { messages, userMessage, isRegenerate = false, isTemporary = false } = submission;
|
2024-06-25 03:02:38 -04:00
|
|
|
const initialResponse = {
|
|
|
|
|
...submission.initialResponse,
|
2024-08-13 03:04:27 -04:00
|
|
|
parentMessageId: userMessage.messageId,
|
|
|
|
|
messageId: userMessage.messageId + '_',
|
2024-06-25 03:02:38 -04:00
|
|
|
};
|
|
|
|
|
if (isRegenerate) {
|
|
|
|
|
setMessages([...messages, initialResponse]);
|
|
|
|
|
} else {
|
|
|
|
|
setMessages([...messages, userMessage, initialResponse]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const { conversationId, parentMessageId } = userMessage;
|
2024-08-30 00:14:37 -04:00
|
|
|
lastAnnouncementTimeRef.current = Date.now();
|
|
|
|
|
announcePolite({
|
2024-08-19 03:51:17 -04:00
|
|
|
message: 'start',
|
2024-08-30 00:14:37 -04:00
|
|
|
isStatus: true,
|
2024-08-13 03:04:27 -04:00
|
|
|
});
|
2024-06-25 03:02:38 -04:00
|
|
|
|
|
|
|
|
let update = {} as TConversation;
|
|
|
|
|
if (setConversation && !isAddedRequest) {
|
|
|
|
|
setConversation((prevState) => {
|
|
|
|
|
let title = prevState?.title;
|
2024-08-13 03:04:27 -04:00
|
|
|
const parentId = isRegenerate ? userMessage.overrideParentMessageId : parentMessageId;
|
2024-09-30 17:17:57 -04:00
|
|
|
if (
|
|
|
|
|
parentId !== Constants.NO_PARENT &&
|
2025-02-09 18:05:31 +01:00
|
|
|
(title?.toLowerCase().includes('new chat') ?? false)
|
2024-09-30 17:17:57 -04:00
|
|
|
) {
|
2024-06-25 03:02:38 -04:00
|
|
|
const convos = queryClient.getQueryData<ConversationData>([QueryKeys.allConversations]);
|
|
|
|
|
const cachedConvo = getConversationById(convos, conversationId);
|
|
|
|
|
title = cachedConvo?.title;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
update = tConvoUpdateSchema.parse({
|
|
|
|
|
...prevState,
|
|
|
|
|
conversationId,
|
|
|
|
|
title,
|
|
|
|
|
}) as TConversation;
|
|
|
|
|
|
|
|
|
|
return update;
|
|
|
|
|
});
|
|
|
|
|
|
2025-02-06 08:11:47 -08:00
|
|
|
if (isTemporary) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-06-25 03:02:38 -04:00
|
|
|
queryClient.setQueryData<ConversationData>([QueryKeys.allConversations], (convoData) => {
|
|
|
|
|
if (!convoData) {
|
|
|
|
|
return convoData;
|
|
|
|
|
}
|
|
|
|
|
if (parentMessageId === Constants.NO_PARENT) {
|
|
|
|
|
return addConversation(convoData, update);
|
|
|
|
|
} else {
|
|
|
|
|
return updateConversation(convoData, update);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
} else if (setConversation) {
|
|
|
|
|
setConversation((prevState) => {
|
|
|
|
|
update = tConvoUpdateSchema.parse({
|
|
|
|
|
...prevState,
|
|
|
|
|
conversationId,
|
|
|
|
|
}) as TConversation;
|
|
|
|
|
return update;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (resetLatestMessage) {
|
|
|
|
|
resetLatestMessage();
|
|
|
|
|
}
|
2024-08-09 02:11:56 -04:00
|
|
|
|
2024-08-18 19:02:46 -04:00
|
|
|
scrollToEnd(() => setAbortScroll(false));
|
2024-06-25 03:02:38 -04:00
|
|
|
},
|
2024-08-13 03:04:27 -04:00
|
|
|
[
|
|
|
|
|
setMessages,
|
|
|
|
|
queryClient,
|
2024-08-18 19:02:46 -04:00
|
|
|
setAbortScroll,
|
2024-08-13 03:04:27 -04:00
|
|
|
isAddedRequest,
|
2024-08-30 00:14:37 -04:00
|
|
|
announcePolite,
|
2024-08-18 19:02:46 -04:00
|
|
|
setConversation,
|
|
|
|
|
resetLatestMessage,
|
2024-08-13 03:04:27 -04:00
|
|
|
],
|
2024-06-25 03:02:38 -04:00
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const finalHandler = useCallback(
|
2024-08-21 18:18:45 -04:00
|
|
|
(data: TFinalResData, submission: EventSubmission) => {
|
2024-06-25 03:02:38 -04:00
|
|
|
const { requestMessage, responseMessage, conversation, runMessages } = data;
|
2025-02-06 08:11:47 -08:00
|
|
|
const {
|
|
|
|
|
messages,
|
|
|
|
|
conversation: submissionConvo,
|
|
|
|
|
isRegenerate = false,
|
|
|
|
|
isTemporary = false,
|
|
|
|
|
} = submission;
|
2024-06-25 03:02:38 -04:00
|
|
|
|
|
|
|
|
setShowStopButton(false);
|
2024-08-13 03:04:27 -04:00
|
|
|
setCompleted((prev) => new Set(prev.add(submission.initialResponse.messageId)));
|
2024-06-25 03:02:38 -04:00
|
|
|
|
|
|
|
|
const currentMessages = getMessages();
|
2024-08-19 03:51:17 -04:00
|
|
|
/* Early return if messages are empty; i.e., the user navigated away */
|
|
|
|
|
if (!currentMessages || currentMessages.length === 0) {
|
2024-06-25 03:02:38 -04:00
|
|
|
return setIsSubmitting(false);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-13 03:04:27 -04:00
|
|
|
/* a11y announcements */
|
|
|
|
|
announcePolite({
|
2024-08-30 00:14:37 -04:00
|
|
|
message: 'end',
|
|
|
|
|
isStatus: true,
|
2024-08-13 03:04:27 -04:00
|
|
|
});
|
|
|
|
|
|
2024-08-30 00:14:37 -04:00
|
|
|
announcePolite({
|
2024-09-10 19:00:27 -04:00
|
|
|
message: getAllContentText(responseMessage),
|
2024-08-30 00:14:37 -04:00
|
|
|
});
|
2024-08-13 03:04:27 -04:00
|
|
|
|
2024-08-19 03:51:17 -04:00
|
|
|
/* Update messages; if assistants endpoint, client doesn't receive responseMessage */
|
2024-06-25 03:02:38 -04:00
|
|
|
if (runMessages) {
|
|
|
|
|
setMessages([...runMessages]);
|
|
|
|
|
} else if (isRegenerate && responseMessage) {
|
|
|
|
|
setMessages([...messages, responseMessage]);
|
2024-08-19 03:51:17 -04:00
|
|
|
} else if (requestMessage != null && responseMessage != null) {
|
2024-06-25 03:02:38 -04:00
|
|
|
setMessages([...messages, requestMessage, responseMessage]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const isNewConvo = conversation.conversationId !== submissionConvo.conversationId;
|
|
|
|
|
if (isNewConvo) {
|
|
|
|
|
queryClient.setQueryData<ConversationData>([QueryKeys.allConversations], (convoData) => {
|
|
|
|
|
if (!convoData) {
|
|
|
|
|
return convoData;
|
|
|
|
|
}
|
|
|
|
|
return deleteConversation(convoData, submissionConvo.conversationId as string);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 03:51:17 -04:00
|
|
|
/* Refresh title */
|
2024-06-25 03:02:38 -04:00
|
|
|
if (
|
|
|
|
|
genTitle &&
|
|
|
|
|
isNewConvo &&
|
2025-02-06 08:11:47 -08:00
|
|
|
!isTemporary &&
|
2024-06-25 03:02:38 -04:00
|
|
|
requestMessage &&
|
|
|
|
|
requestMessage.parentMessageId === Constants.NO_PARENT
|
|
|
|
|
) {
|
|
|
|
|
setTimeout(() => {
|
|
|
|
|
genTitle.mutate({ conversationId: conversation.conversationId as string });
|
|
|
|
|
}, 2500);
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-19 03:51:17 -04:00
|
|
|
if (setConversation && isAddedRequest !== true) {
|
2024-08-26 15:34:46 -04:00
|
|
|
if (window.location.pathname === '/c/new') {
|
|
|
|
|
window.history.pushState({}, '', '/c/' + conversation.conversationId);
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-25 03:02:38 -04:00
|
|
|
setConversation((prevState) => {
|
|
|
|
|
const update = {
|
|
|
|
|
...prevState,
|
|
|
|
|
...conversation,
|
|
|
|
|
};
|
|
|
|
|
|
2024-08-19 03:51:17 -04:00
|
|
|
if (prevState?.model != null && prevState.model !== submissionConvo.model) {
|
2024-06-25 03:02:38 -04:00
|
|
|
update.model = prevState.model;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return update;
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
},
|
|
|
|
|
[
|
|
|
|
|
genTitle,
|
|
|
|
|
queryClient,
|
|
|
|
|
getMessages,
|
|
|
|
|
setMessages,
|
|
|
|
|
setCompleted,
|
|
|
|
|
isAddedRequest,
|
2024-08-13 03:04:27 -04:00
|
|
|
announcePolite,
|
2024-06-25 03:02:38 -04:00
|
|
|
setConversation,
|
|
|
|
|
setIsSubmitting,
|
|
|
|
|
setShowStopButton,
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const errorHandler = useCallback(
|
2024-08-21 18:18:45 -04:00
|
|
|
({ data, submission }: { data?: TResData; submission: EventSubmission }) => {
|
2024-06-25 03:02:38 -04:00
|
|
|
const { messages, userMessage, initialResponse } = submission;
|
|
|
|
|
|
|
|
|
|
setCompleted((prev) => new Set(prev.add(initialResponse.messageId)));
|
|
|
|
|
|
2024-09-30 17:17:57 -04:00
|
|
|
const conversationId = userMessage.conversationId ?? submission.conversationId ?? '';
|
2024-06-25 03:02:38 -04:00
|
|
|
|
|
|
|
|
const parseErrorResponse = (data: TResData | Partial<TMessage>) => {
|
|
|
|
|
const metadata = data['responseMessage'] ?? data;
|
2024-08-19 03:51:17 -04:00
|
|
|
const errorMessage: Partial<TMessage> = {
|
2024-06-25 03:02:38 -04:00
|
|
|
...initialResponse,
|
|
|
|
|
...metadata,
|
|
|
|
|
error: true,
|
2024-08-13 03:04:27 -04:00
|
|
|
parentMessageId: userMessage.messageId,
|
2024-06-25 03:02:38 -04:00
|
|
|
};
|
|
|
|
|
|
2024-08-19 03:51:17 -04:00
|
|
|
if (errorMessage.messageId === undefined || errorMessage.messageId === '') {
|
2024-06-25 03:02:38 -04:00
|
|
|
errorMessage.messageId = v4();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return tMessageSchema.parse(errorMessage);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if (!data) {
|
2024-09-30 17:17:57 -04:00
|
|
|
const convoId = conversationId || v4();
|
2024-06-25 03:02:38 -04:00
|
|
|
const errorResponse = parseErrorResponse({
|
|
|
|
|
text: 'Error connecting to server, try refreshing the page.',
|
|
|
|
|
...submission,
|
|
|
|
|
conversationId: convoId,
|
|
|
|
|
});
|
|
|
|
|
setMessages([...messages, userMessage, errorResponse]);
|
|
|
|
|
if (newConversation) {
|
|
|
|
|
newConversation({
|
|
|
|
|
template: { conversationId: convoId },
|
2024-08-13 03:04:27 -04:00
|
|
|
preset: tPresetSchema.parse(submission.conversation),
|
2024-06-25 03:02:38 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-30 17:17:57 -04:00
|
|
|
const receivedConvoId = data.conversationId ?? '';
|
|
|
|
|
if (!conversationId && !receivedConvoId) {
|
2024-06-25 03:02:38 -04:00
|
|
|
const convoId = v4();
|
|
|
|
|
const errorResponse = parseErrorResponse(data);
|
|
|
|
|
setMessages([...messages, userMessage, errorResponse]);
|
|
|
|
|
if (newConversation) {
|
|
|
|
|
newConversation({
|
|
|
|
|
template: { conversationId: convoId },
|
2024-08-13 03:04:27 -04:00
|
|
|
preset: tPresetSchema.parse(submission.conversation),
|
2024-06-25 03:02:38 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
return;
|
2024-09-30 17:17:57 -04:00
|
|
|
} else if (!receivedConvoId) {
|
2024-06-25 03:02:38 -04:00
|
|
|
const errorResponse = parseErrorResponse(data);
|
|
|
|
|
setMessages([...messages, userMessage, errorResponse]);
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
console.log('Error:', data);
|
|
|
|
|
const errorResponse = tMessageSchema.parse({
|
|
|
|
|
...data,
|
|
|
|
|
error: true,
|
2024-08-13 03:04:27 -04:00
|
|
|
parentMessageId: userMessage.messageId,
|
2024-06-25 03:02:38 -04:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
setMessages([...messages, userMessage, errorResponse]);
|
2024-09-30 17:17:57 -04:00
|
|
|
if (receivedConvoId && paramId === 'new' && newConversation) {
|
2024-06-25 03:02:38 -04:00
|
|
|
newConversation({
|
2024-09-30 17:17:57 -04:00
|
|
|
template: { conversationId: receivedConvoId },
|
2024-08-13 03:04:27 -04:00
|
|
|
preset: tPresetSchema.parse(submission.conversation),
|
2024-06-25 03:02:38 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
return;
|
|
|
|
|
},
|
|
|
|
|
[setMessages, paramId, setIsSubmitting, setCompleted, newConversation],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
const abortConversation = useCallback(
|
2024-08-21 18:18:45 -04:00
|
|
|
async (conversationId = '', submission: EventSubmission, messages?: TMessage[]) => {
|
2024-06-25 03:02:38 -04:00
|
|
|
const runAbortKey = `${conversationId}:${messages?.[messages.length - 1]?.messageId ?? ''}`;
|
|
|
|
|
console.log({ conversationId, submission, messages, runAbortKey });
|
2024-09-30 17:17:57 -04:00
|
|
|
const { endpoint: _endpoint, endpointType } =
|
|
|
|
|
(submission.conversation as TConversation | null) ?? {};
|
2024-06-25 03:02:38 -04:00
|
|
|
const endpoint = endpointType ?? _endpoint;
|
|
|
|
|
try {
|
|
|
|
|
const response = await fetch(`${EndpointURLs[endpoint ?? '']}/abort`, {
|
|
|
|
|
method: 'POST',
|
|
|
|
|
headers: {
|
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
|
Authorization: `Bearer ${token}`,
|
|
|
|
|
},
|
|
|
|
|
body: JSON.stringify({
|
|
|
|
|
abortKey: runAbortKey,
|
|
|
|
|
endpoint,
|
|
|
|
|
}),
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Check if the response is JSON
|
|
|
|
|
const contentType = response.headers.get('content-type');
|
2024-08-19 03:51:17 -04:00
|
|
|
if (contentType != null && contentType.includes('application/json')) {
|
2024-06-25 03:02:38 -04:00
|
|
|
const data = await response.json();
|
|
|
|
|
console.log(`[aborted] RESPONSE STATUS: ${response.status}`, data);
|
|
|
|
|
if (response.status === 404) {
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
return;
|
|
|
|
|
}
|
2024-09-30 17:17:57 -04:00
|
|
|
if (data.final === true) {
|
2024-06-25 03:02:38 -04:00
|
|
|
finalHandler(data, submission);
|
|
|
|
|
} else {
|
|
|
|
|
cancelHandler(data, submission);
|
|
|
|
|
}
|
|
|
|
|
} else if (response.status === 204) {
|
|
|
|
|
const responseMessage = {
|
|
|
|
|
...submission.initialResponse,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const data = {
|
|
|
|
|
requestMessage: submission.userMessage,
|
|
|
|
|
responseMessage: responseMessage,
|
|
|
|
|
conversation: submission.conversation,
|
|
|
|
|
};
|
|
|
|
|
console.log(`[aborted] RESPONSE STATUS: ${response.status}`, data);
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
} else {
|
|
|
|
|
throw new Error(
|
|
|
|
|
'Unexpected response from server; Status: ' +
|
|
|
|
|
response.status +
|
|
|
|
|
' ' +
|
|
|
|
|
response.statusText,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Error cancelling request');
|
|
|
|
|
console.error(error);
|
2024-09-30 17:17:57 -04:00
|
|
|
const convoId = conversationId || v4();
|
2024-06-25 03:02:38 -04:00
|
|
|
const text =
|
2024-08-13 03:04:27 -04:00
|
|
|
submission.initialResponse.text.length > 45 ? submission.initialResponse.text : '';
|
2024-06-25 03:02:38 -04:00
|
|
|
const errorMessage = {
|
|
|
|
|
...submission,
|
|
|
|
|
...submission.initialResponse,
|
2024-09-30 17:17:57 -04:00
|
|
|
text: (text || (error as Error | undefined)?.message) ?? 'Error cancelling request',
|
2024-06-25 03:02:38 -04:00
|
|
|
unfinished: !!text.length,
|
|
|
|
|
error: true,
|
|
|
|
|
};
|
|
|
|
|
const errorResponse = tMessageSchema.parse(errorMessage);
|
|
|
|
|
setMessages([...submission.messages, submission.userMessage, errorResponse]);
|
|
|
|
|
if (newConversation) {
|
|
|
|
|
newConversation({
|
|
|
|
|
template: { conversationId: convoId },
|
2024-08-13 03:04:27 -04:00
|
|
|
preset: tPresetSchema.parse(submission.conversation),
|
2024-06-25 03:02:38 -04:00
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
setIsSubmitting(false);
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
[token, setIsSubmitting, finalHandler, cancelHandler, setMessages, newConversation],
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return {
|
🚧 chore: merge latest dev build to main repo (#3844)
* agents - phase 1 (#30)
* chore: copy assistant files
* feat: frontend and data-provider
* feat: backend get endpoint test
* fix(MessageEndpointIcon): switched to AgentName and AgentAvatar
* fix: small fixes
* fix: agent endpoint config
* fix: show Agent Builder
* chore: install agentus
* chore: initial scaffolding for agents
* fix: updated Assistant logic to Agent Logic for some Agent components
* WIP first pass, demo of agent package
* WIP: initial backend infra for agents
* fix: agent list error
* wip: agents routing
* chore: Refactor useSSE hook to handle different data events
* wip: correctly emit events
* chore: Update @librechat/agentus npm dependency to version 1.0.9
* remove comment
* first pass: streaming agent text
* chore: Remove @librechat/agentus root-level workspace npm dependency
* feat: Agent Schema and Model
* fix: content handling fixes
* fix: content message save
* WIP: new content data
* fix: run step issue with tool calls
* chore: Update @librechat/agentus npm dependency to version 1.1.5
* feat: update controller and agent routes
* wip: initial backend tool and tool error handling support
* wip: tool chunks
* chore: Update @librechat/agentus npm dependency to version 1.1.7
* chore: update tool_call typing, add test conditions and logs
* fix: create agent
* fix: create agent
* first pass: render completed content parts
* fix: remove logging, fix step handler typing
* chore: Update @librechat/agentus npm dependency to version 1.1.9
* refactor: cleanup maps on unmount
* chore: Update BaseClient.js to safely count tokens for string, number, and boolean values
* fix: support subsequent messages with tool_calls
* chore: export order
* fix: select agent
* fix: tool call types and handling
* chore: switch to anthropic for testing
* fix: AgentSelect
* refactor: experimental: OpenAIClient to use array for intermediateReply
* fix(useSSE): revert old condition for streaming legacy client tokens
* fix: lint
* revert `agent_id` to `id`
* chore: update localization keys for agent-related components
* feat: zod schema handling for actions
* refactor(actions): if no params, no zodSchema
* chore: Update @librechat/agentus npm dependency to version 1.2.1
* feat: first pass, actions
* refactor: empty schema for actions without params
* feat: Update createRun function to accept additional options
* fix: message payload formatting; feat: add more client options
* fix: ToolCall component rendering when action has no args but has output
* refactor(ToolCall): allow non-stringy args
* WIP: first pass, correctly formatted tool_calls between providers
* refactor: Remove duplicate import of 'roles' module
* refactor: Exclude 'vite.config.ts' from TypeScript compilation
* refactor: fix agent related types
> - no need to use endpoint/model fields for identifying agent metadata
> - add `provider` distinction for agent-configured 'endpoint'
- no need for agent-endpoint map
- reduce complexity of tools as functions into tools as string[]
- fix types related to above changes
- reduce unnecessary variables for queries/mutations and corresponding react-query keys
* refactor: Add tools and tool_kwargs fields to agent schema
* refactor: Remove unused code and update dependencies
* refactor: Update updateAgentHandler to use req.body directly
* refactor: Update AgentSelect component to use localized hooks
* refactor: Update agent schema to include tools and provider fields
* refactor(AgentPanel): add scrollbar gutter, add provider field to form, fix agent schema required values
* refactor: Update AgentSwitcher component to use selectedAgentId instead of selectedAgent
* refactor: Update AgentPanel component to include alternateName import and defaultAgentFormValues
* refactor(SelectDropDown): allow setting value as option while still supporting legacy usage (string values only)
* refactor: SelectDropdown changes - Only necessary when the available values are objects with label/value fields and the selected value is expected to be a string.
* refactor: TypeError issues and handle provider as option
* feat: Add placeholder for provider selection in AgentPanel component
* refactor: Update agent schema to include author and provider fields
* fix: show expected 'create agent' placeholder when creating agent
* chore: fix localization strings, hide capabilities form for now
* chore: typing
* refactor: import order and use compact agents schema for now
* chore: typing
* refactor: Update AgentForm type to use AgentCapabilities
* fix agent form agent selection issues
* feat: responsive agent selection
* fix: Handle cancelled fetch in useSelectAgent hook
* fix: reset agent form on accordion close/open
* feat: Add agent_id to default conversation for agents endpoint
* feat: agents endpoint request handling
* refactor: reset conversation model on agent select
* refactor: add `additional_instructions` to conversation schema, organize other fields
* chore: casing
* chore: types
* refactor(loadAgentTools): explicitly pass agent_id, do not pass `model` to loadAgentTools for now, load action sets by agent_id
* WIP: initial draft of real agent client initialization
* WIP: first pass, anthropic agent requests
* feat: remember last selected agent
* feat: openai and azure connected
* fix: prioritize agent model for runs unless an explicit override model is passed from client
* feat: Agent Actions
* fix: save agent id to convo
* feat: model panel (#29)
* feat: model panel
* bring back comments
* fix: method still null
* fix: AgentPanel FormContext
* feat: add more parameters
* fix: style issues; refactor: Agent Controller
* fix: cherry-pick
* fix: Update AgentAvatar component to use AssistantIcon instead of BrainCircuit
* feat: OGDialog for delete agent; feat(assistant): update Agent types, introduced `model_parameters`
* feat: icon and general `model_parameters` update
* feat: use react-hook-form better
* fix: agent builder form reset issue when switching panels
* refactor: modularize agent builder form
---------
Co-authored-by: Danny Avila <danny@librechat.ai>
* fix: AgentPanel and ModelPanel type issues and use `useFormContext` and `watch` instead of `methods` directly and `useWatch`.
* fix: tool call issues due to invalid input (anthropic) of empty string
* fix: handle empty text in Part component
---------
Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>
* refactor: remove form ModelPanel and fixed nested ternary expressions in AgentConfig
* fix: Model Parameters not saved correctly
* refactor: remove console log
* feat: avatar upload and get for Agents (#36)
Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>
* chore: update to public package
* fix: typing, optional chaining
* fix: cursor not showing for content parts
* chore: conditionally enable agents
* ci: fix azure test
* ci: fix frontend tests, fix eslint api
* refactor: Remove unused errorContentPart variable
* continue of the agent message PR (#40)
* last fixes
* fix: agentMap
* pr merge test (#41)
* fix: model icon not fetching correctly
* remove console logs
* feat: agent name
* refactor: pass documentsMap as a prop to allow re-render of assistant form
* refactor: pass documentsMap as a prop to allow re-render of assistant form
* chore: Bump version to 0.7.419
* fix: TypeError: Cannot read properties of undefined (reading 'id')
* refactor: update AgentSwitcher component to use ControlCombobox instead of Combobox
---------
Co-authored-by: Marco Beretta <81851188+berry-13@users.noreply.github.com>
2024-08-31 16:33:51 -04:00
|
|
|
stepHandler,
|
2024-06-25 03:02:38 -04:00
|
|
|
syncHandler,
|
|
|
|
|
finalHandler,
|
|
|
|
|
errorHandler,
|
|
|
|
|
messageHandler,
|
|
|
|
|
contentHandler,
|
|
|
|
|
createdHandler,
|
2024-09-30 17:17:57 -04:00
|
|
|
attachmentHandler,
|
2024-06-25 03:02:38 -04:00
|
|
|
abortConversation,
|
|
|
|
|
};
|
|
|
|
|
}
|