Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

💬 feat: Read a Model Call as a Conversation in the Trace Viewer (#16103)

Danny Avilaa35745bf5f65
diff --git a/client/src/components/Chat/Trace/Conversation.tsx b/client/src/components/Chat/Trace/Conversation.tsx
new file mode 100644
index 0000000000..d9ef6860a8
--- /dev/null
+++ b/client/src/components/Chat/Trace/Conversation.tsx
@@ -0,0 +1,164 @@
+import { memo } from 'react';
+import { User, Sparkles, ScrollText } from 'lucide-react';
+import type { TTracePrompt, TTraceMessage, TTraceContent } from 'librechat-data-provider';
+import type { RecordPresentation } from './present';
+import type { TranslationKeys } from '~/hooks';
+import StackedToolIcons from '~/components/Chat/Messages/Content/ToolOutput/StackedToolIcons';
+import { formatJSON } from '~/utils/json';
+import { useLocalize } from '~/hooks';
+
+type ToolTitle = (name: string) => Pick<RecordPresentation, 'title' | 'caption'>;
+
+const ROLE_LABELS: Record<Exclude<TTraceMessage['role'], 'tool'>, TranslationKeys> = {
+ system: 'com_ui_trace_message_system',
+ user: 'com_ui_trace_message_user',
+ assistant: 'com_ui_model',
+};
+
+function Text({ content }: { content: TTraceContent }) {
+ const localize = useLocalize();
+ return (
+ <>
+ <p className="max-h-48 overflow-auto whitespace-pre-wrap break-words text-sm text-text-primary">
+ {content.value}
+ </p>
+ {content.truncated && (
+ <p className="text-xs text-text-secondary">{localize('com_ui_trace_truncated')}</p>
+ )}
+ </>
+ );
+}
+
+function Message({
+ message,
+ toolTitleFor,
+ mcpIconMap,
+}: {
+ message: TTraceMessage;
+ toolTitleFor: ToolTitle;
+ mcpIconMap: Map<string, string>;
+}) {
+ const localize = useLocalize();
+ const tool = message.role === 'tool' && message.toolName != null ? message.toolName : undefined;
+ const toolTitle = tool != null ? toolTitleFor(tool) : undefined;
+ return (
+ <li className="flex flex-col gap-1 rounded-lg border border-border-light bg-surface-primary-alt p-2">
+ <span className="flex items-center gap-1.5 text-xs font-medium text-text-secondary">
+ {message.role === 'user' && <User className="size-3.5" aria-hidden="true" />}
+ {message.role === 'assistant' && <Sparkles className="size-3.5" aria-hidden="true" />}
+ {message.role === 'tool' && (
+ <StackedToolIcons toolNames={[tool ?? '']} mcpIconMap={mcpIconMap} />
+ )}
+ {message.role === 'tool'
+ ? localize('com_ui_trace_message_tool', {
+ 0: toolTitle?.title ?? localize('com_ui_trace_kind_tool'),
+ })
+ : localize(ROLE_LABELS[message.role])}
+ {toolTitle?.caption != null && <span className="font-normal">{toolTitle.caption}</span>}
+ </span>
+ {message.text != null && <Text content={message.text} />}
+ {message.attachments != null && (
+ <p className="text-xs text-text-secondary">
+ {localize('com_ui_trace_message_attachments', { 0: message.attachments.join(', ') })}
+ </p>
+ )}
+ {message.toolCalls?.map((call, index) => {
+ const { title, caption } = toolTitleFor(call.name);
+ return (
+ <details key={`${call.name}-${index}`} className="group text-xs">
+ <summary className="flex cursor-pointer items-center gap-1.5 rounded-md py-0.5 text-text-primary marker:content-none hover:bg-surface-hover">
+ <StackedToolIcons toolNames={[call.name]} mcpIconMap={mcpIconMap} />
+ <span className="min-w-0 truncate">
+ {localize('com_ui_trace_message_asked_tool', { 0: title })}
+ </span>
+ {caption != null && <span className="truncate text-text-secondary">{caption}</span>}
+ </summary>
+ {call.args != null && (
+ <pre className="mt-1 max-h-40 overflow-auto whitespace-pre-wrap break-words font-mono text-xs text-text-secondary">
+ {call.args.truncated ? call.args.value : formatJSON(call.args.value)}
+ </pre>
+ )}
+ </details>
+ );
+ })}
+ </li>
+ );
+}
+
+/**
+ * A model call as a conversation: what it wrote, then what it was given. The
+ * instructions stay folded, since they are long and the same on every call, and
+ * the newest messages, which the call answered, are what opens.
+ */
+function Conversation({
+ prompt,
+ reply,
+ toolTitleFor,
+ mcpIconMap,
+}: {
+ prompt?: TTracePrompt;
+ reply?: TTraceMessage;
+ toolTitleFor: ToolTitle;
+ mcpIconMap: Map<string, string>;
+}) {
+ const localize = useLocalize();
+ const heading = 'text-xs font-semibold uppercase tracking-wide text-text-secondary';
+ const [first, ...rest] = prompt?.messages ?? [];
+ const system = first?.role === 'system' ? first : undefined;
+ const messages = system != null ? rest : (prompt?.messages ?? []);
+ return (
+ <div className="flex flex-col gap-4">
+ {reply != null && (
+ <section className="flex flex-col gap-1.5">
+ <h4 className={heading}>{localize('com_ui_trace_reply')}</h4>
+ <ul className="flex flex-col gap-1.5">
+ <Message message={reply} toolTitleFor={toolTitleFor} mcpIconMap={mcpIconMap} />
+ </ul>
+ </section>
+ )}
+ {prompt != null && (
+ <section className="flex flex-col gap-1.5">
+ <h4 className={heading}>{localize('com_ui_trace_prompt')}</h4>
+ {system?.text != null && (
+ <details className="rounded-lg border border-border-light bg-surface-primary-alt p-2 text-sm">
+ <summary className="flex cursor-pointer items-center gap-1.5 text-xs font-medium text-text-secondary marker:content-none">
+ <ScrollText className="size-3.5" aria-hidden="true" />
+ {localize('com_ui_trace_message_system')}
+ </summary>
+ <div className="mt-1.5 flex flex-col gap-1">
+ <Text content={system.text} />
+ </div>
+ </details>
+ )}
+ {prompt.omitted > 0 && (
+ <p className="px-2 text-xs text-text-secondary">
+ {localize('com_ui_trace_prompt_omitted', { 0: String(prompt.omitted) })}
+ </p>
+ )}
+ <ul className="flex flex-col gap-1.5">
+ {messages.map((message, index) => (
+ <Message
+ key={index}
+ message={message}
+ toolTitleFor={toolTitleFor}
+ mcpIconMap={mcpIconMap}
+ />
+ ))}
+ </ul>
+ {prompt.tools != null && (
+ <details className="px-2 text-xs text-text-secondary">
+ <summary className="cursor-pointer">
+ {localize('com_ui_trace_prompt_tools', { 0: String(prompt.tools.length) })}
+ </summary>
+ <p className="mt-1 break-words">
+ {prompt.tools.map((name) => toolTitleFor(name).title).join(', ')}
+ </p>
+ </details>
+ )}
+ </section>
+ )}
+ </div>
+ );
+}
+
+export default memo(Conversation);
diff --git a/client/src/components/Chat/Trace/Inspector.tsx b/client/src/components/Chat/Trace/Inspector.tsx
index a4b17d8d3d..2b9e310f90 100644
--- a/client/src/components/Chat/Trace/Inspector.tsx
+++ b/client/src/components/Chat/Trace/Inspector.tsx
@@ -3,7 +3,7 @@ import copy from 'copy-to-clipboard';
import { Link } from 'react-router-dom';
import { X, Copy, Check, MessageSquare } from 'lucide-react';
import { Button, Spinner, buttonVariants } from '@librechat/client';
-import type { TTraceContent } from 'librechat-data-provider';
+import type { TTraceContent, TTraceRecordDetail } from 'librechat-data-provider';
import type { ReactNode } from 'react';
import type { RecordPresentation, ToolCallView } from './present';
import type { TranslationKeys } from '~/hooks';
@@ -14,10 +14,12 @@ import { useTraceFormat, recordDurationText } from './format';
import { formatCost, formatTokens } from '~/utils/tokens';
import { appearanceOf, STATUS_LABEL } from './kinds';
import { cn, renderAgentAvatar } from '~/utils';
+import Conversation from './Conversation';
import { formatJSON } from '~/utils/json';
import { useLocalize } from '~/hooks';
type Field = { label: TranslationKeys; value: ReactNode };
+type ToolFor = (name: string) => Pick<RecordPresentation, 'title' | 'caption'>;
function Section({ title, fields }: { title: TranslationKeys; fields: Field[] }) {
const localize = useLocalize();
@@ -69,13 +71,21 @@ function RecordContent({
recordId,
messageId,
sourceId,
+ toolFor,
+ mcpIconMap,
+ placeholder,
}: {
conversationId: string;
recordId: string;
messageId: string;
sourceId?: string;
+ toolFor: ToolFor;
+ mcpIconMap: Map<string, string>;
+ /** What the chat already knows the record wrote: shown at once, until the record's own reply loads. */
+ placeholder?: ReactNode;
}) {
const localize = useLocalize();
+ const [raw, setRaw] = useState(false);
const { data, isLoading, isError, refetch } = useConversationTraceRecordQuery(
{ conversationId, recordId, messageId, sourceId },
true,
@@ -83,10 +93,13 @@ function RecordContent({
if (isLoading) {
return (
- <div className="flex justify-center py-4" role="status">
- <Spinner className="size-4 text-text-secondary" />
- <span className="sr-only">{localize('com_ui_trace_loading')}</span>
- </div>
+ <>
+ {placeholder}
+ <div className="flex justify-center py-4" role="status">
+ <Spinner className="size-4 text-text-secondary" />
+ <span className="sr-only">{localize('com_ui_trace_loading')}</span>
+ </div>
+ </>
);
}
if (isError || !data) {
@@ -105,11 +118,46 @@ function RecordContent({
if (!data.input && !data.output && !data.metadata) {
return <p className="text-sm text-text-secondary">{localize('com_ui_trace_content_empty')}</p>;
}
+ const readable = data.prompt != null || data.reply != null;
return (
<div className="flex flex-col gap-3">
- <ContentBlock label="com_ui_trace_input" content={data.input} />
- <ContentBlock label="com_ui_trace_output" content={data.output} />
- <ContentBlock label="com_ui_trace_metadata" content={data.metadata} />
+ {data.reply == null && placeholder}
+ {readable && (
+ <Button
+ size="sm"
+ variant="ghost"
+ aria-pressed={raw}
+ className="self-end"
+ onClick={() => setRaw((current) => !current)}
+ >
+ {localize(raw ? 'com_ui_trace_view_conversation' : 'com_ui_trace_view_raw')}
+ </Button>
+ )}
+ {readable && !raw && (
+ <Conversation
+ prompt={data.prompt}
+ reply={data.reply}
+ toolTitleFor={toolFor}
+ mcpIconMap={mcpIconMap}
+ />
+ )}
+ {(!readable || raw) && (
+ <RawContent input={data.input} output={data.output} metadata={data.metadata} />
+ )}
+ </div>
+ );
+}
+
+function RawContent({
+ input,
+ output,
+ metadata,
+}: Pick<TTraceRecordDetail, 'input' | 'output' | 'metadata'>) {
+ return (
+ <div className="flex flex-col gap-3">
+ <ContentBlock label="com_ui_trace_input" content={input} />
+ <ContentBlock label="com_ui_trace_output" content={output} />
+ <ContentBlock label="com_ui_trace_metadata" content={metadata} />
</div>
);
}
@@ -208,6 +256,7 @@ function Inspector({
node,
presentation,
mcpIconMap,
+ toolFor,
turnStart,
sourceId,
conversationId,
@@ -219,6 +268,7 @@ function Inspector({
node: TraceNode;
presentation: RecordPresentation;
mcpIconMap: Map<string, string>;
+ toolFor: ToolFor;
turnStart: number;
/** The page source that listed this record. */
sourceId?: string;
@@ -276,6 +326,25 @@ function Inspector({
});
}
+ const preview = presentation.calls == null && presentation.preview != null && (
+ <p className="whitespace-pre-wrap break-words rounded-lg border border-border-light bg-surface-primary-alt p-2 text-sm">
+ {presentation.preview}
+ </p>
+ );
+ /** A model call is read as a conversation, which is what the panel is opened for, so it leads. */
+ const leadsWithContent = showContent && record.kind === 'generation';
+ const content = (
+ <RecordContent
+ conversationId={conversationId}
+ recordId={record.id}
+ messageId={record.messageId}
+ sourceId={sourceId}
+ toolFor={toolFor}
+ mcpIconMap={mcpIconMap}
+ placeholder={leadsWithContent ? preview : undefined}
+ />
+ );
+
return (
<aside
aria-labelledby={headingId}
@@ -342,11 +411,8 @@ function Inspector({
{record.agentId != null && presentation.agent !== undefined && (
<AgentCard agent={presentation.agent} agentId={record.agentId} />
)}
- {presentation.calls == null && presentation.preview != null && (
- <p className="whitespace-pre-wrap break-words rounded-lg border border-border-light bg-surface-primary-alt p-2 text-sm">
- {presentation.preview}
- </p>
- )}
+ {!leadsWithContent && preview}
+ {leadsWithContent && content}
{presentation.calls != null && (
<ToolCalls calls={presentation.calls} mcpIconMap={mcpIconMap} />
)}
@@ -379,17 +445,12 @@ function Inspector({
},
]}
/>
- {showContent && (
+ {showContent && !leadsWithContent && (
<section className="flex flex-col gap-2">
<h4 className="text-xs font-semibold uppercase tracking-wide text-text-secondary">
{localize('com_ui_trace_content')}
</h4>
- <RecordContent
- conversationId={conversationId}
- recordId={record.id}
- messageId={record.messageId}
- sourceId={sourceId}
- />
+ {content}
</section>
)}
</div>
diff --git a/client/src/components/Chat/Trace/Viewer.tsx b/client/src/components/Chat/Trace/Viewer.tsx
index 483f891f6e..6530514f18 100644
--- a/client/src/components/Chat/Trace/Viewer.tsx
+++ b/client/src/components/Chat/Trace/Viewer.tsx
@@ -196,10 +196,11 @@ export default function Viewer({
return presentation;
};
}, [localize, activity, previewOf, mcpServerNames, agentOf]);
- const toolTitleFor = useCallback(
- (name: string) => presentTool(name, { localize, mcpServerNames }).title,
+ const toolFor = useCallback(
+ (name: string) => presentTool(name, { localize, mcpServerNames }),
[localize, mcpServerNames],
);
+ const toolTitleFor = useCallback((name: string) => toolFor(name).title, [toolFor]);
/** The user message each response answered: what tells one response from another in the ledger. */
const askedByResponse = useMemo(() => {
const textById = new Map<string, string>();
@@ -576,6 +577,7 @@ export default function Viewer({
node={selectedNode}
presentation={presentFor(selectedNode)}
mcpIconMap={mcpIconMap}
+ toolFor={toolFor}
turnStart={selectedTurnStart}
sourceId={recordSources.get(selectedNode.record.id)}
conversationId={conversationId}
diff --git a/client/src/components/Chat/Trace/__tests__/Viewer.test.tsx b/client/src/components/Chat/Trace/__tests__/Viewer.test.tsx
index e780f47ab5..f02436fb71 100644
--- a/client/src/components/Chat/Trace/__tests__/Viewer.test.tsx
+++ b/client/src/components/Chat/Trace/__tests__/Viewer.test.tsx
@@ -992,6 +992,58 @@ describe('Trace Viewer', () => {
expect(dataService.getConversationTraceRecord).not.toHaveBeenCalled();
});
+ it('shows a model call as a conversation, newest messages open, with the raw form one click away', async () => {
+ mockStartupConfig = {
+ interface: { traceViewer: { enabled: true, showInputOutput: true } },
+ };
+ const content = (value: string) => ({ value, truncated: false });
+ jest.spyOn(dataService, 'getConversationTraceRecord').mockResolvedValue({
+ record: sdkRecords[2],
+ contentAvailable: true,
+ input: { value: '{"messages":[{"role":"sys', truncated: true },
+ prompt: {
+ total: 40,
+ omitted: 37,
+ tools: ['web_search', 'read_file'],
+ messages: [
+ { role: 'system', text: content('You are Codebase Scout.') },
+ { role: 'user', text: content('Where does it read from?') },
+ { role: 'tool', toolName: 'web_search', text: content('ten results') },
+ ],
+ },
+ reply: {
+ role: 'assistant',
+ text: content('Let me look that up.'),
+ toolCalls: [{ name: 'web_search', args: content('{"query":"weather"}') }],
+ },
+ });
+ renderSdkRun();
+
+ await userEvent.click(await recordRow('com_ui_model: Let me look that up\\.'));
+
+ const inspector = screen.getByTestId('trace-inspector');
+ expect(await within(inspector).findByText('com_ui_trace_reply')).toBeInTheDocument();
+ expect(within(inspector).getByText('Where does it read from?')).toBeVisible();
+ expect(within(inspector).getByText('com_ui_trace_prompt_omitted 37')).toBeInTheDocument();
+ expect(
+ within(inspector).getByText('com_ui_trace_message_tool com_ui_tool_name_web_search'),
+ ).toBeInTheDocument();
+ expect(
+ within(inspector).getByText('com_ui_trace_message_asked_tool com_ui_tool_name_web_search'),
+ ).toBeInTheDocument();
+ expect(
+ within(inspector).getByText('You are Codebase Scout.').closest('details'),
+ ).not.toHaveAttribute('open');
+ expect(within(inspector).queryByText(/"messages":\[/)).not.toBeInTheDocument();
+
+ await userEvent.click(
+ within(inspector).getByRole('button', { name: 'com_ui_trace_view_raw' }),
+ );
+
+ expect(within(inspector).getByText(/"messages":\[/)).toBeInTheDocument();
+ expect(within(inspector).queryByText('com_ui_trace_reply')).not.toBeInTheDocument();
+ });
+
it('shows the agent by name with its id to copy and a chat one click away', async () => {
renderSdkRun();
await recordRow('com_ui_model: Let me look that up\\.');
diff --git a/client/src/locales/en/translation.json b/client/src/locales/en/translation.json
index 037faac25d..ae67d26eeb 100644
--- a/client/src/locales/en/translation.json
+++ b/client/src/locales/en/translation.json
@@ -2708,6 +2708,11 @@
"com_ui_trace_kind_tool": "Tool",
"com_ui_trace_load_older": "Load older records",
"com_ui_trace_loading": "Loading trace",
+ "com_ui_trace_message_asked_tool": "Asked for {{0}}",
+ "com_ui_trace_message_attachments": "Attached: {{0}}",
+ "com_ui_trace_message_system": "Instructions",
+ "com_ui_trace_message_tool": "Tool result · {{0}}",
+ "com_ui_trace_message_user": "User",
"com_ui_trace_metadata": "Metadata",
"com_ui_trace_model": "Model",
"com_ui_trace_model_calls_count": "{{count}} model calls",
@@ -2719,10 +2724,14 @@
"com_ui_trace_overview": "Timeline overview",
"com_ui_trace_overview_hint": "Drag to focus an interval. Scroll or press plus and minus to zoom, use the arrow keys to pan, and press Escape to reset.",
"com_ui_trace_partial": "Showing the newest {{0}} records",
+ "com_ui_trace_prompt": "What the model saw",
+ "com_ui_trace_prompt_omitted": "{{0}} earlier messages not shown",
+ "com_ui_trace_prompt_tools": "{{0}} tools available",
"com_ui_trace_record_id": "Record ID",
"com_ui_trace_recorded_as": "Recorded as",
"com_ui_trace_records": "Trace records",
"com_ui_trace_refresh": "Refresh trace",
+ "com_ui_trace_reply": "What it wrote",
"com_ui_trace_role_phase_label": "Phase label",
"com_ui_trace_role_reasoning_label": "Reasoning label",
"com_ui_trace_role_run": "Agent run",
@@ -2772,6 +2781,8 @@
"com_ui_trace_usage_reasoning": "Reasoning",
"com_ui_trace_usage_total": "Total",
"com_ui_trace_view": "View trace",
+ "com_ui_trace_view_conversation": "View as conversation",
+ "com_ui_trace_view_raw": "View raw",
"com_ui_transferred_to": "Transferred to",
"com_ui_travel": "Travel",
"com_ui_trust_app": "I trust this application",
diff --git a/packages/api/src/langfuse/prompt.spec.ts b/packages/api/src/langfuse/prompt.spec.ts
new file mode 100644
index 0000000000..28169d9a5f
--- /dev/null
+++ b/packages/api/src/langfuse/prompt.spec.ts
@@ -0,0 +1,132 @@
+import { toTracePrompt, toTraceReply } from './prompt';
+
+const system = { role: 'system', content: 'S'.repeat(400) };
+const turn = (index: number) => [
+ { role: 'user', content: `question ${index}` },
+ {
+ role: 'assistant',
+ content: '',
+ tool_calls: [{ id: `call-${index}`, name: 'web_search', args: { query: `q${index}` } }],
+ },
+ { role: 'web_search', content: `result ${index}`, additional_kwargs: {} },
+];
+const conversation = (turns: number) => ({
+ messages: [system, ...Array.from({ length: turns }, (_, index) => turn(index)).flat()],
+ tools: [{ type: 'function', name: 'web_search' }, { function: { name: 'read_file' } }, {}],
+});
+
+describe('toTracePrompt', () => {
+ it('reads the conversation Langfuse stores as JSON text, in the application roles', () => {
+ const prompt = toTracePrompt(JSON.stringify(conversation(1)), 10_000);
+
+ expect(prompt).toEqual({
+ total: 4,
+ omitted: 0,
+ tools: ['web_search', 'read_file'],
+ messages: [
+ { role: 'system', text: { value: 'S'.repeat(400), truncated: false } },
+ { role: 'user', text: { value: 'question 0', truncated: false } },
+ {
+ role: 'assistant',
+ toolCalls: [{ name: 'web_search', args: { value: '{"query":"q0"}', truncated: false } }],
+ },
+ { role: 'tool', toolName: 'web_search', text: { value: 'result 0', truncated: false } },
+ ],
+ });
+ });
+
+ it('keeps the system message and the newest messages when the conversation outgrows the budget', () => {
+ const prompt = toTracePrompt(conversation(40), 200);
+
+ expect(prompt?.total).toBe(121);
+ expect(prompt?.messages[0]).toEqual({
+ role: 'system',
+ text: { value: 'S'.repeat(50), truncated: true },
+ });
+ expect(prompt?.messages[(prompt?.messages.length ?? 0) - 1]).toEqual({
+ role: 'tool',
+ toolName: 'web_search',
+ text: { value: 'result 39', truncated: false },
+ });
+ expect(prompt?.omitted).toBe(121 - (prompt?.messages.length ?? 0));
+ expect(prompt?.omitted).toBeGreaterThan(100);
+ const spent = (prompt?.messages ?? []).reduce(
+ (total, message) => total + (message.text?.value.length ?? 0),
+ 0,
+ );
+ expect(spent).toBeLessThanOrEqual(200);
+ });
+
+ it('bounds one long message alone so it cannot hide the messages before it', () => {
+ const prompt = toTracePrompt(
+ [
+ { role: 'user', content: 'first' },
+ { role: 'assistant', content: 'A'.repeat(1000) },
+ ],
+ 400,
+ );
+
+ expect(prompt?.messages).toEqual([
+ { role: 'user', text: { value: 'first', truncated: false } },
+ { role: 'assistant', text: { value: 'A'.repeat(100), truncated: true } },
+ ]);
+ });
+
+ it('joins text parts and names the parts that are not text', () => {
+ const prompt = toTracePrompt(
+ [
+ {
+ type: 'human',
+ content: [
+ { type: 'text', text: 'Look at this' },
+ { type: 'image_url', image_url: { url: 'data:image/png;base64,AAAA' } },
+ { type: 'text', text: 'and this.' },
+ ],
+ },
+ ],
+ 1000,
+ );
+
+ expect(prompt?.messages).toEqual([
+ {
+ role: 'user',
+ text: { value: 'Look at this\nand this.', truncated: false },
+ attachments: ['image_url'],
+ },
+ ]);
+ });
+
+ it('returns nothing for input that is not a conversation', () => {
+ expect(toTracePrompt('plain text prompt', 100)).toBeUndefined();
+ expect(toTracePrompt('{"messages":', 100)).toBeUndefined();
+ expect(toTracePrompt({ question: 'hi' }, 100)).toBeUndefined();
+ expect(toTracePrompt([], 100)).toBeUndefined();
+ expect(toTracePrompt([1, 'two', null], 100)).toBeUndefined();
+ expect(toTracePrompt(undefined, 100)).toBeUndefined();
+ });
+});
+
+describe('toTraceReply', () => {
+ it('reads the message a model call wrote, with the tools it asked for', () => {
+ expect(
+ toTraceReply(
+ JSON.stringify({
+ role: 'assistant',
+ content: [{ type: 'text', text: 'Let me check.', index: 0 }],
+ tool_calls: [{ type: 'function', function: { name: 'read_file', arguments: '{"p":1}' } }],
+ }),
+ 1000,
+ ),
+ ).toEqual({
+ role: 'assistant',
+ text: { value: 'Let me check.', truncated: false },
+ toolCalls: [{ name: 'read_file', args: { value: '{"p":1}', truncated: false } }],
+ });
+ });
+
+ it('returns nothing for output that is not an assistant message', () => {
+ expect(toTraceReply('short', 100)).toBeUndefined();
+ expect(toTraceReply({ role: 'user', content: 'hi' }, 100)).toBeUndefined();
+ expect(toTraceReply({ role: 'assistant', content: '' }, 100)).toBeUndefined();
+ });
+});
diff --git a/packages/api/src/langfuse/prompt.ts b/packages/api/src/langfuse/prompt.ts
new file mode 100644
index 0000000000..0b8ee1675c
--- /dev/null
+++ b/packages/api/src/langfuse/prompt.ts
@@ -0,0 +1,224 @@
+import type {
+ TTracePrompt,
+ TTraceMessage,
+ TTraceContent,
+ TTraceToolCall,
+ TTraceMessageRole,
+} from 'librechat-data-provider';
+
+/** Shares of the content budget: the system message may not crowd out the turns the call answered. */
+const SYSTEM_SHARE = 0.25;
+const MESSAGE_SHARE = 0.25;
+const MAX_TOOL_CALLS = 64;
+const MAX_TOOL_NAMES = 128;
+const MAX_ATTACHMENTS = 16;
+const NAME_MAX_LENGTH = 256;
+
+const ROLES: Record<string, TTraceMessageRole> = {
+ system: 'system',
+ developer: 'system',
+ user: 'user',
+ human: 'user',
+ assistant: 'assistant',
+ ai: 'assistant',
+ tool: 'tool',
+ function: 'tool',
+};
+
+type Json = string | number | boolean | null | Json[] | { [key: string]: Json };
+type JsonObject = { [key: string]: Json };
+
+function isObject(value: Json | undefined): value is JsonObject {
+ return value != null && typeof value === 'object' && !Array.isArray(value);
+}
+
+/** Langfuse returns a generation's input and output as JSON text; an SDK may also hand back the value. */
+function parse(value: unknown): Json | undefined {
+ if (typeof value !== 'string') {
+ return value as Json | undefined;
+ }
+ const trimmed = value.trimStart();
+ if (!trimmed.startsWith('{') && !trimmed.startsWith('[')) {
+ return undefined;
+ }
+ try {
+ return JSON.parse(value) as Json;
+ } catch {
+ return undefined;
+ }
+}
+
+function bounded(text: string, maxLength: number): TTraceContent | undefined {
+ if (text === '') {
+ return undefined;
+ }
+ return text.length > maxLength
+ ? { value: text.slice(0, maxLength), truncated: true }
+ : { value: text, truncated: false };
+}
+
+function name(value: Json | undefined): string | undefined {
+ return typeof value === 'string' && value !== '' ? value.slice(0, NAME_MAX_LENGTH) : undefined;
+}
+
+/** A message's text and the types of its parts that are not text. */
+function contentOf(content: Json | undefined): { text: string; attachments: string[] } {
+ if (typeof content === 'string') {
+ return { text: content, attachments: [] };
+ }
+ if (!Array.isArray(content)) {
+ return { text: '', attachments: [] };
+ }
+ const texts: string[] = [];
+ const attachments: string[] = [];
+ for (const part of content) {
+ if (typeof part === 'string') {
+ texts.push(part);
+ continue;
+ }
+ if (!isObject(part)) {
+ continue;
+ }
+ if (typeof part.text === 'string') {
+ texts.push(part.text);
+ continue;
+ }
+ const type = name(part.type);
+ if (type != null && attachments.length < MAX_ATTACHMENTS) {
+ attachments.push(type);
+ }
+ }
+ return { text: texts.join('\n'), attachments };
+}
+
+function toolCallsOf(calls: Json | undefined, maxLength: number): TTraceToolCall[] {
+ if (!Array.isArray(calls)) {
+ return [];
+ }
+ const result: TTraceToolCall[] = [];
+ for (const call of calls) {
+ if (result.length === MAX_TOOL_CALLS || !isObject(call)) {
+ continue;
+ }
+ const fn = isObject(call.function) ? call.function : undefined;
+ const callName = name(call.name) ?? name(fn?.name);
+ if (callName == null) {
+ continue;
+ }
+ const rawArgs = call.args ?? fn?.arguments;
+ const args =
+ rawArgs == null
+ ? undefined
+ : bounded(typeof rawArgs === 'string' ? rawArgs : JSON.stringify(rawArgs), maxLength);
+ result.push({ name: callName, ...(args ? { args } : {}) });
+ }
+ return result;
+}
+
+/**
+ * One message in the application's terms. A tool's answer is recorded under the
+ * tool's own name as its role, so a role that is none of the known ones is a
+ * tool message from that tool.
+ */
+function toMessage(value: Json, maxLength: number): TTraceMessage | undefined {
+ if (!isObject(value)) {
+ return undefined;
+ }
+ const rawRole = name(value.role) ?? name(value.type);
+ if (rawRole == null) {
+ return undefined;
+ }
+ const known = ROLES[rawRole.toLowerCase()];
+ const role = known ?? 'tool';
+ const { text, attachments } = contentOf(value.content);
+ const content = bounded(text, maxLength);
+ const toolCalls = role === 'assistant' ? toolCallsOf(value.tool_calls, maxLength) : [];
+ const toolName =
+ role === 'tool' ? (name(value.name) ?? (known == null ? rawRole : undefined)) : undefined;
+ return {
+ role,
+ ...(content ? { text: content } : {}),
+ ...(toolName != null ? { toolName } : {}),
+ ...(toolCalls.length > 0 ? { toolCalls } : {}),
+ ...(attachments.length > 0 ? { attachments } : {}),
+ };
+}
+
+function costOf(message: TTraceMessage): number {
+ let cost = message.text?.value.length ?? 0;
+ for (const call of message.toolCalls ?? []) {
+ cost += call.name.length + (call.args?.value.length ?? 0);
+ }
+ return cost;
+}
+
+function messagesOf(parsed: Json | undefined): Json | undefined {
+ return isObject(parsed) ? parsed.messages : undefined;
+}
+
+/**
+ * Reads a model call's input as a conversation within `maxLength` characters.
+ * Cutting the raw input at its front keeps only the start of the system message
+ * of a long conversation, so the budget goes to the system message first (a
+ * share of it) and then to the newest messages backwards, each bounded alone.
+ */
+export function toTracePrompt(input: unknown, maxLength: number): TTracePrompt | undefined {
+ const parsed = parse(input);
+ const list = Array.isArray(parsed) ? parsed : messagesOf(parsed);
+ if (!Array.isArray(list) || list.length === 0) {
+ return undefined;
+ }
+ const perMessage = Math.max(1, Math.floor(maxLength * MESSAGE_SHARE));
+ const first = toMessage(list[0], Math.max(1, Math.floor(maxLength * SYSTEM_SHARE)));
+ const system = first?.role === 'system' ? first : undefined;
+ const oldest = system ? 1 : 0;
+ let remaining = maxLength - (system ? costOf(system) : 0);
+ const newest: TTraceMessage[] = [];
+ let index = list.length - 1;
+ for (; index >= oldest && remaining > 0; index--) {
+ const message = toMessage(list[index], Math.min(perMessage, remaining));
+ if (message == null) {
+ continue;
+ }
+ remaining -= costOf(message);
+ newest.push(message);
+ }
+ const messages = [...(system ? [system] : []), ...newest.reverse()];
+ if (messages.length === 0) {
+ return undefined;
+ }
+ const tools = isObject(parsed) && Array.isArray(parsed.tools) ? toolNamesOf(parsed.tools) : [];
+ return {
+ messages,
+ total: list.length,
+ omitted: index - oldest + 1,
+ ...(tools.length > 0 ? { tools } : {}),
+ };
+}
+
+function toolNamesOf(tools: Json[]): string[] {
+ const names: string[] = [];
+ for (const tool of tools) {
+ if (names.length === MAX_TOOL_NAMES || !isObject(tool)) {
+ continue;
+ }
+ const fn = isObject(tool.function) ? tool.function : undefined;
+ const toolName = name(tool.name) ?? name(fn?.name);
+ if (toolName != null) {
+ names.push(toolName);
+ }
+ }
+ return names;
+}
+
+/** Reads a model call's output as the message it wrote, when it is one. */
+export function toTraceReply(output: unknown, maxLength: number): TTraceMessage | undefined {
+ const parsed = parse(output);
+ if (!isObject(parsed)) {
+ return undefined;
+ }
+ const message = toMessage(parsed, maxLength);
+ return message?.role === 'assistant' && (message.text != null || message.toolCalls != null)
+ ? message
+ : undefined;
+}
diff --git a/packages/api/src/langfuse/reader.spec.ts b/packages/api/src/langfuse/reader.spec.ts
index 073dba7cfa..1236aa7eca 100644
--- a/packages/api/src/langfuse/reader.spec.ts
+++ b/packages/api/src/langfuse/reader.spec.ts
@@ -1376,6 +1376,48 @@ describe('createLangfuseTraceReader', () => {
});
});
+ it('reads a model call as a conversation, and only a model call', async () => {
+ const io = {
+ input:
+ '{"messages":[{"role":"system","content":"Be brief."},{"role":"user","content":"hello"}]}',
+ output: '{"role":"assistant","content":[{"type":"text","text":"Hi."}]}',
+ };
+ const settings = resolveTraceViewerConfig({ enabled: true, showInputOutput: true });
+ const { reader } = setup({
+ responses: [
+ jsonResponse({ data: [observation({ id: 'obs-llm', type: 'GENERATION', ...io })] }),
+ jsonResponse({ data: [observation(io)] }),
+ ],
+ });
+
+ const modelCall = await reader.getRecord({
+ ...createQuery({ settings }),
+ recordId: 'obs-llm',
+ messageId: 'response-1',
+ });
+ const wrapper = await reader.getRecord({
+ ...createQuery({ settings }),
+ recordId: 'obs-root',
+ messageId: 'response-1',
+ });
+
+ expect(modelCall?.prompt).toEqual({
+ total: 2,
+ omitted: 0,
+ messages: [
+ { role: 'system', text: { value: 'Be brief.', truncated: false } },
+ { role: 'user', text: { value: 'hello', truncated: false } },
+ ],
+ });
+ expect(modelCall?.reply).toEqual({
+ role: 'assistant',
+ text: { value: 'Hi.', truncated: false },
+ });
+ expect(modelCall?.input?.value).toBe(io.input);
+ expect(wrapper?.prompt).toBeUndefined();
+ expect(wrapper?.reply).toBeUndefined();
+ });
+
it('keeps literal strings that spell an empty object or null', async () => {
const { reader } = setup({
responses: [
diff --git a/packages/api/src/langfuse/reader.ts b/packages/api/src/langfuse/reader.ts
index ae8fb058f4..fac7bd4796 100644
--- a/packages/api/src/langfuse/reader.ts
+++ b/packages/api/src/langfuse/reader.ts
@@ -16,6 +16,7 @@ import type {
import type { LangfuseScoreDestination } from './destinations';
import type { TraceQuery, TraceReader } from '~/traces/types';
import { exportsInternalTraceUserId } from './identity';
+import { toTracePrompt, toTraceReply } from './prompt';
import { getScoreDestinations } from './destinations';
import { TraceReadError } from '~/traces/types';
import { mergeHeaders } from '~/utils/headers';
@@ -1091,9 +1092,14 @@ export function createLangfuseTraceReader({
const input = toContent(observation.input, maxLength);
const output = toContent(observation.output, maxLength);
const metadata = toContent(observation.metadata, maxLength);
+ const isModelCall = record.kind === 'generation';
+ const prompt = isModelCall ? toTracePrompt(observation.input, maxLength) : undefined;
+ const reply = isModelCall ? toTraceReply(observation.output, maxLength) : undefined;
return {
record,
contentAvailable: true,
+ ...(prompt ? { prompt } : {}),
+ ...(reply ? { reply } : {}),
...(input ? { input } : {}),
...(output ? { output } : {}),
...(metadata ? { metadata } : {}),
diff --git a/packages/data-provider/src/types/traces.ts b/packages/data-provider/src/types/traces.ts
index 1dec9cce57..b70bb2d49d 100644
--- a/packages/data-provider/src/types/traces.ts
+++ b/packages/data-provider/src/types/traces.ts
@@ -115,10 +115,47 @@ export type TTraceContent = {
truncated: boolean;
};
+export type TTraceMessageRole = 'system' | 'user' | 'assistant' | 'tool';
+
+export type TTraceToolCall = {
+ name: string;
+ args?: TTraceContent;
+};
+
+/** One message of a model call's conversation, each bounded on its own so a long one hides no other. */
+export type TTraceMessage = {
+ role: TTraceMessageRole;
+ text?: TTraceContent;
+ /** The tool a `tool` message answers for. */
+ toolName?: string;
+ /** The tools an `assistant` message asked for. */
+ toolCalls?: TTraceToolCall[];
+ /** Parts that are not text (an image, a file), by their type. */
+ attachments?: string[];
+};
+
+/**
+ * What a model call was given, as a conversation. A long one keeps its system
+ * message and its newest messages, which are what the call answered, and
+ * `omitted` counts the older ones left out between them.
+ */
+export type TTracePrompt = {
+ messages: TTraceMessage[];
+ /** Every message the call was given, listed or not. */
+ total: number;
+ omitted: number;
+ /** Names of the tools the model could call. */
+ tools?: string[];
+};
+
export type TTraceRecordDetail = {
record: TTraceRecord;
/** False when the deployment withholds input, output and metadata. */
contentAvailable: boolean;
+ /** Set when the input is a conversation the backend could read as one; `input` stays the raw form. */
+ prompt?: TTracePrompt;
+ /** Set when the output is a message the backend could read as one; `output` stays the raw form. */
+ reply?: TTraceMessage;
input?: TTraceContent;
output?: TTraceContent;
metadata?: TTraceContent;