Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

🪄 feat: Agent Artifacts #5804

Merged
merged 9 commits into from
Feb 11, 2025
36 changes: 26 additions & 10 deletions api/app/clients/OpenAIClient.js
Original file line number Diff line number Diff line change
Expand Up @@ -1066,9 +1066,14 @@ ${convo}
});
}

getStreamText() {
/**
*
* @param {string[]} [intermediateReply]
* @returns {string}
*/
getStreamText(intermediateReply) {
if (!this.streamHandler) {
return '';
return intermediateReply?.join('') ?? '';
}

let thinkMatch;
Expand All @@ -1088,7 +1093,10 @@ ${convo}
}
}

const reasoningTokens = reasoningText.length > 0 ? `:::thinking\n${reasoningText}\n:::\n` : '';
const reasoningTokens =
reasoningText.length > 0
? `:::thinking\n${reasoningText.replace('<think>', '').replace('</think>', '').trim()}\n:::\n`
: '';

return `${reasoningTokens}${this.streamHandler.tokens.join('')}`;
}
Expand Down Expand Up @@ -1327,11 +1335,19 @@ ${convo}
streamPromise = new Promise((resolve) => {
streamResolve = resolve;
});
/** @type {OpenAI.OpenAI.CompletionCreateParamsStreaming} */
const params = {
...modelOptions,
stream: true,
};
if (
this.options.endpoint === EModelEndpoint.openAI ||
this.options.endpoint === EModelEndpoint.azureOpenAI
) {
params.stream_options = { include_usage: true };
}
const stream = await openai.beta.chat.completions
.stream({
...modelOptions,
stream: true,
})
.stream(params)
.on('abort', () => {
/* Do nothing here */
})
Expand Down Expand Up @@ -1471,7 +1487,7 @@ ${convo}
err?.message?.includes('abort') ||
(err instanceof OpenAI.APIError && err?.message?.includes('abort'))
) {
return intermediateReply.join('');
return this.getStreamText(intermediateReply);
}
if (
err?.message?.includes(
Expand All @@ -1489,15 +1505,15 @@ ${convo}
if (this.streamHandler && this.streamHandler.reasoningTokens.length) {
return this.getStreamText();
} else if (intermediateReply.length > 0) {
return intermediateReply.join('');
return this.getStreamText(intermediateReply);
} else {
throw err;
}
} else if (err instanceof OpenAI.APIError) {
if (this.streamHandler && this.streamHandler.reasoningTokens.length) {
return this.getStreamText();
} else if (intermediateReply.length > 0) {
return intermediateReply.join('');
return this.getStreamText(intermediateReply);
} else {
throw err;
}
Expand Down
3 changes: 3 additions & 0 deletions api/models/schema/agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ const agentSchema = mongoose.Schema(
model_parameters: {
type: Object,
},
artifacts: {
type: String,
},
access_level: {
type: Number,
},
Expand Down
18 changes: 18 additions & 0 deletions api/server/services/Endpoints/agents/initialize.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const getBedrockOptions = require('~/server/services/Endpoints/bedrock/options')
const initOpenAI = require('~/server/services/Endpoints/openAI/initialize');
const initCustom = require('~/server/services/Endpoints/custom/initialize');
const initGoogle = require('~/server/services/Endpoints/google/initialize');
const generateArtifactsPrompt = require('~/app/clients/prompts/artifacts');
const { getCustomEndpointConfig } = require('~/server/services/Config');
const { loadAgentTools } = require('~/server/services/ToolService');
const AgentClient = require('~/server/controllers/agents/client');
Expand Down Expand Up @@ -72,6 +73,16 @@ const primeResources = async (_attachments, _tool_resources) => {
}
};

/**
* @param {object} params
* @param {ServerRequest} params.req
* @param {ServerResponse} params.res
* @param {Agent} params.agent
* @param {object} [params.endpointOption]
* @param {AgentToolResources} [params.tool_resources]
* @param {boolean} [params.isInitialAgent]
* @returns {Promise<Agent>}
*/
const initializeAgentOptions = async ({
req,
res,
Expand Down Expand Up @@ -132,6 +143,13 @@ const initializeAgentOptions = async ({
agent.model_parameters.model = agent.model;
}

if (typeof agent.artifacts === 'string' && agent.artifacts !== '') {
agent.additional_instructions = generateArtifactsPrompt({
endpoint: agent.provider,
artifacts: agent.artifacts,
});
}

const tokensModel =
agent.provider === EModelEndpoint.azureOpenAI ? agent.model : agent.model_parameters.model;

Expand Down
1 change: 1 addition & 0 deletions api/server/utils/handleText.js
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,7 @@ function generateConfig(key, baseURL, endpoint) {
config.capabilities = [
AgentCapabilities.execute_code,
AgentCapabilities.file_search,
AgentCapabilities.artifacts,
AgentCapabilities.actions,
AgentCapabilities.tools,
];
Expand Down
5 changes: 3 additions & 2 deletions client/src/common/agents-types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AgentCapabilities } from 'librechat-data-provider';
import { AgentCapabilities, ArtifactModes } from 'librechat-data-provider';
import type { Agent, AgentProvider, AgentModelParameters } from 'librechat-data-provider';
import type { OptionWithIcon, ExtendedFile } from './types';

Expand All @@ -9,8 +9,8 @@ export type TAgentOption = OptionWithIcon &
};

export type TAgentCapabilities = {
[AgentCapabilities.execute_code]: boolean;
[AgentCapabilities.file_search]: boolean;
[AgentCapabilities.execute_code]: boolean;
[AgentCapabilities.end_after_tools]?: boolean;
[AgentCapabilities.hide_sequential_outputs]?: boolean;
};
Expand All @@ -26,4 +26,5 @@ export type AgentForm = {
tools?: string[];
provider?: AgentProvider | OptionWithIcon;
agent_ids?: string[];
[AgentCapabilities.artifacts]?: ArtifactModes | string;
} & TAgentCapabilities;
5 changes: 3 additions & 2 deletions client/src/common/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,8 @@ export type GenericSetter<T> = (value: T | ((currentValue: T) => T)) => void;
export type LastSelectedModels = Record<t.EModelEndpoint, string>;

export type LocalizeFunction = (
phraseKey: TranslationKeys,
options?: Record<string, string | number>
phraseKey: TranslationKeys,
options?: Record<string, string | number>,
) => string;

export type ChatFormValues = { text: string };
Expand All @@ -89,6 +89,7 @@ export type IconMapProps = {
iconURL?: string;
context?: 'landing' | 'menu-item' | 'nav' | 'message';
endpoint?: string | null;
endpointType?: string;
assistantName?: string;
agentName?: string;
avatar?: string;
Expand Down
4 changes: 2 additions & 2 deletions client/src/components/Chat/ChatView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function ChatView({ index = 0 }: { index?: number }) {
select: useCallback(
(data: TMessage[]) => {
const dataTree = buildTree({ messages: data, fileMap });
return dataTree?.length === 0 ? null : dataTree ?? null;
return dataTree?.length === 0 ? null : (dataTree ?? null);
},
[fileMap],
),
Expand Down Expand Up @@ -62,7 +62,7 @@ function ChatView({ index = 0 }: { index?: number }) {
<ChatFormProvider {...methods}>
<ChatContext.Provider value={chatHelpers}>
<AddedChatContext.Provider value={addedChatHelpers}>
<Presentation useSidePanel={true}>
<Presentation>
{content}
<div className="w-full border-t-0 pl-0 pt-2 dark:border-white/20 md:w-[calc(100%-.5rem)] md:border-t-0 md:border-transparent md:pl-0 md:pt-0 md:dark:border-transparent">
<ChatForm index={index} />
Expand Down
11 changes: 6 additions & 5 deletions client/src/components/Chat/Messages/Content/CodeAnalyze.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ import FinishedIcon from './FinishedIcon';
import MarkdownLite from './MarkdownLite';
import store from '~/store';

const radius = 56.08695652173913;
const circumference = 2 * Math.PI * radius;

export default function CodeAnalyze({
initialProgress = 0.1,
code,
Expand All @@ -22,9 +25,6 @@ export default function CodeAnalyze({
const progress = useProgress(initialProgress);
const showAnalysisCode = useRecoilValue(store.showCode);
const [showCode, setShowCode] = useState(showAnalysisCode);

const radius = 56.08695652173913;
const circumference = 2 * Math.PI * radius;
const offset = circumference - progress * circumference;

const logs = outputs.reduce((acc, output) => {
Expand Down Expand Up @@ -53,9 +53,10 @@ export default function CodeAnalyze({
<ProgressText
progress={progress}
onClick={() => setShowCode((prev) => !prev)}
inProgressText="Analyzing"
finishedText="Finished analyzing"
inProgressText={localize('com_ui_analyzing')}
finishedText={localize('com_ui_analyzing_finished')}
hasInput={!!code.length}
isExpanded={showCode}
/>
</div>
{showCode && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ import type { TAttachment } from 'librechat-data-provider';
import ProgressText from '~/components/Chat/Messages/Content/ProgressText';
import FinishedIcon from '~/components/Chat/Messages/Content/FinishedIcon';
import MarkdownLite from '~/components/Chat/Messages/Content/MarkdownLite';
import { useProgress, useLocalize } from '~/hooks';
import { CodeInProgress } from './CodeProgress';
import Attachment from './Attachment';
import LogContent from './LogContent';
import { useProgress } from '~/hooks';
import store from '~/store';

interface ParsedArgs {
Expand Down Expand Up @@ -36,6 +36,9 @@ export function useParseArgs(args: string): ParsedArgs {
}, [args]);
}

const radius = 56.08695652173913;
const circumference = 2 * Math.PI * radius;

export default function ExecuteCode({
initialProgress = 0.1,
args,
Expand All @@ -49,14 +52,12 @@ export default function ExecuteCode({
isSubmitting: boolean;
attachments?: TAttachment[];
}) {
const localize = useLocalize();
const showAnalysisCode = useRecoilValue(store.showCode);
const [showCode, setShowCode] = useState(showAnalysisCode);

const { lang, code } = useParseArgs(args);
const progress = useProgress(initialProgress);

const radius = 56.08695652173913;
const circumference = 2 * Math.PI * radius;
const offset = circumference - progress * circumference;

return (
Expand All @@ -78,9 +79,10 @@ export default function ExecuteCode({
<ProgressText
progress={progress}
onClick={() => setShowCode((prev) => !prev)}
inProgressText="Analyzing"
finishedText="Finished analyzing"
inProgressText={localize('com_ui_analyzing')}
finishedText={localize('com_ui_analyzing_finished')}
hasInput={!!code.length}
isExpanded={showCode}
/>
</div>
{showCode && (
Expand All @@ -105,9 +107,7 @@ export default function ExecuteCode({
)}
</div>
)}
{attachments?.map((attachment, index) => (
<Attachment attachment={attachment} key={index} />
))}
{attachments?.map((attachment, index) => <Attachment attachment={attachment} key={index} />)}
</>
);
}
12 changes: 10 additions & 2 deletions client/src/components/Chat/Messages/Content/ProgressText.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ export default function ProgressText({
authText,
hasInput = true,
popover = false,
isExpanded = false,
}: {
progress: number;
onClick?: () => void;
Expand All @@ -50,8 +51,9 @@ export default function ProgressText({
authText?: string;
hasInput?: boolean;
popover?: boolean;
isExpanded?: boolean;
}) {
const text = progress < 1 ? authText ?? inProgressText : finishedText;
const text = progress < 1 ? (authText ?? inProgressText) : finishedText;
return (
<Wrapper popover={popover}>
<button
Expand All @@ -61,7 +63,13 @@ export default function ProgressText({
onClick={onClick}
>
{text}
<svg width="16" height="17" viewBox="0 0 16 17" fill="none">
<svg
width="16"
height="17"
viewBox="0 0 16 17"
fill="none"
className={isExpanded ? 'rotate-180' : 'rotate-0'}
>
<path
className={hasInput ? '' : 'stroke-transparent'}
d="M11.3346 7.83203L8.00131 11.1654L4.66797 7.83203"
Expand Down
Loading
Loading