mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-19 09:50:15 +01:00
🪨 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 commitf8067e91d4. * Revert "refactor: dynamic form elements using react-hook-form Controllers" This reverts commit2150c4815d. * 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:
parent
8c14360263
commit
d59b62174f
134 changed files with 3684 additions and 1213 deletions
|
|
@ -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,
|
||||
);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue