OpenRAG is a comprehensive, single package Retrieval-Augmented Generation platform built on Langflow, Docling, and Opensearch.
Python
feat: Collapse function calls (#1669)
Mike Fortman7b7184fe15e0
diff --git a/frontend/app/chat/_components/assistant-message.tsx b/frontend/app/chat/_components/assistant-message.tsxindex 02ba2e061..d32188b25 100644--- a/frontend/app/chat/_components/assistant-message.tsx+++ b/frontend/app/chat/_components/assistant-message.tsx@@ -17,7 +17,7 @@ import type {FunctionCall,TokenUsage as TokenUsageType,} from "../_types/types";-import { FunctionCalls } from "./function-calls";+import { FunctionCallsContainer } from "./function-calls";import { Message } from "./message";import MessageActions from "./message-actions";import { TokenUsage } from "./token-usage";@@ -124,11 +124,12 @@ export function AssistantMessage({) : undefined}>- <FunctionCalls+ <FunctionCallsContainerfunctionCalls={functionCalls}messageIndex={messageIndex}expandedFunctionCalls={expandedFunctionCalls}onToggle={onToggle}+ isStreaming={isStreaming}/><div className="relative">{/* Slide animation for initial greeting */}diff --git a/frontend/app/chat/_components/function-calls.tsx b/frontend/app/chat/_components/function-calls.tsxdeleted file mode 100644index 35ef83e39..000000000--- a/frontend/app/chat/_components/function-calls.tsx+++ /dev/null@@ -1,239 +0,0 @@-import { ChevronDown, ChevronRight, Settings } from "lucide-react";-import { Badge } from "@/components/ui/badge";-import type { FunctionCall } from "../_types/types";--interface FunctionCallsProps {- functionCalls: FunctionCall[];- messageIndex?: number;- expandedFunctionCalls: Set<string>;- onToggle: (functionCallId: string) => void;-}--export function FunctionCalls({- functionCalls,- messageIndex,- expandedFunctionCalls,- onToggle,-}: FunctionCallsProps) {- if (!functionCalls || functionCalls.length === 0) return null;-- return (- <div className="mb-3 space-y-2">- {functionCalls.map((fc, index) => {- const functionCallId = `${messageIndex || "streaming"}-${index}`;- const isExpanded = expandedFunctionCalls.has(functionCallId);-- // Determine display name - show both name and type if available- const displayName =- fc.type && fc.type !== fc.name ? `${fc.name} (${fc.type})` : fc.name;-- return (- <div- key={index}- className="fc-card rounded-lg bg-blue-500/10 border border-blue-500/20 p-3"- >- <div- className="flex items-center gap-2 cursor-pointer hover:bg-blue-500/5 -m-3 p-3 rounded-lg transition-colors"- onClick={() => onToggle(functionCallId)}- >- <Settings className="h-4 w-4 text-blue-400" />- <span className="text-sm font-medium text-blue-400 flex-1">- Function Call: {displayName}- </span>- {fc.id && (- <span className="text-xs text-blue-400 font-mono">- {fc.id.substring(0, 8)}...- </span>- )}- <Badge- variant="outline"- className={- fc.status === "completed"- ? "fc-status-completed border-green-500/40 bg-green-500/20 text-green-400"- : fc.status === "error"- ? "border-red-500/40 bg-red-500/20 text-red-400"- : "border-yellow-500/40 bg-yellow-500/20 text-yellow-400"- }- >- {fc.status}- </Badge>- {isExpanded ? (- <ChevronDown className="h-4 w-4 text-blue-400" />- ) : (- <ChevronRight className="h-4 w-4 text-blue-400" />- )}- </div>-- {isExpanded && (- <div className="mt-3 pt-3 border-t border-blue-500/20">- {/* Show type information if available */}- {fc.type && (- <div className="text-xs text-muted-foreground mb-3">- <span className="font-medium">Type:</span>- <span className="fc-value ml-2 px-2 py-1 bg-muted/30 rounded font-mono">- {fc.type}- </span>- </div>- )}-- {/* Show ID if available */}- {fc.id && (- <div className="text-xs text-muted-foreground mb-3">- <span className="font-medium">ID:</span>- <span className="fc-value ml-2 px-2 py-1 bg-muted/30 rounded font-mono">- {fc.id}- </span>- </div>- )}-- {/* Show arguments - either completed or streaming */}- {(fc.arguments || fc.argumentsString) && (- <div className="text-xs text-muted-foreground mb-3">- <span className="font-medium">Arguments:</span>- <pre className="mt-1 p-2 bg-muted/30 rounded text-xs overflow-x-auto">- {fc.arguments- ? JSON.stringify(fc.arguments, null, 2)- : fc.argumentsString || "..."}- </pre>- </div>- )}-- {fc.result && (- <div className="text-xs text-muted-foreground">- <span className="font-medium">Result:</span>- {Array.isArray(fc.result) ? (- <div className="mt-1 space-y-2">- {(() => {- // Handle different result formats- let resultsToRender = fc.result;-- // Check if this is function_call format with nested results- // Function call format: results = [{ results: [...] }]- // Tool call format: results = [{ text_key: ..., data: {...} }]- if (- fc.result.length > 0 &&- fc.result[0]?.results &&- Array.isArray(fc.result[0].results) &&- !fc.result[0].text_key- ) {- resultsToRender = fc.result[0].results;- }-- type ToolResultItem = {- text_key?: string;- data?: { file_path?: string; text?: string };- filename?: string;- page?: number;- score?: number;- source_url?: string | null;- text?: string;- };- const items =- resultsToRender as unknown as ToolResultItem[];- return items.map((result, idx: number) => (- <div- key={idx}- className="fc-result p-2 bg-muted/30 rounded"- >- {/* Handle tool_call format (file_path in data) */}- {result.data?.file_path && (- <div className="font-medium text-blue-400 mb-1 text-xs">- π {result.data.file_path || "Unknown file"}- </div>- )}-- {/* Handle function_call format (filename directly) */}- {result.filename && !result.data?.file_path && (- <div className="font-medium text-blue-400 mb-1 text-xs">- π {result.filename}- {result.page && ` (page ${result.page})`}- {result.score && (- <span className="ml-2 text-xs text-muted-foreground">- Score: {result.score.toFixed(3)}- </span>- )}- </div>- )}-- {/* Handle tool_call text format */}- {result.data?.text && (- <div className="text-xs text-foreground whitespace-pre-wrap max-h-32 overflow-y-auto">- {result.data.text.length > 300- ? result.data.text.substring(0, 300) + "..."- : result.data.text}- </div>- )}-- {/* Handle function_call text format */}- {result.text && !result.data?.text && (- <div className="text-xs text-foreground whitespace-pre-wrap max-h-32 overflow-y-auto">- {result.text.length > 300- ? result.text.substring(0, 300) + "..."- : result.text}- </div>- )}-- {/* Show additional metadata for function_call format */}- {result.source_url && (- <div className="text-xs text-muted-foreground mt-1">- <a- href={result.source_url}- target="_blank"- rel="noopener noreferrer"- className="text-blue-400 hover:underline"- >- Source URL- </a>- </div>- )}-- {result.text_key && (- <div className="text-xs text-muted-foreground mt-1">- Key: {result.text_key}- </div>- )}- </div>- ));- })()}- <div className="text-xs text-muted-foreground">- Found {(() => {- let resultsToCount = fc.result;- if (- fc.result.length > 0 &&- fc.result[0]?.results &&- Array.isArray(fc.result[0].results) &&- !fc.result[0].text_key- ) {- resultsToCount = fc.result[0].results;- }- return resultsToCount.length;- })()} result- {(() => {- let resultsToCount = fc.result;- if (- fc.result.length > 0 &&- fc.result[0]?.results &&- Array.isArray(fc.result[0].results) &&- !fc.result[0].text_key- ) {- resultsToCount = fc.result[0].results;- }- return resultsToCount.length !== 1 ? "s" : "";- })()}- </div>- </div>- ) : (- <pre className="mt-1 p-2 bg-muted/30 rounded text-xs overflow-x-auto">- {JSON.stringify(fc.result, null, 2)}- </pre>- )}- </div>- )}- </div>- )}- </div>- );- })}- </div>- );-}diff --git a/frontend/app/chat/_components/function-calls/call.tsx b/frontend/app/chat/_components/function-calls/call.tsxnew file mode 100644index 000000000..d851d7340--- /dev/null+++ b/frontend/app/chat/_components/function-calls/call.tsx@@ -0,0 +1,94 @@+import { ChevronDown, ChevronRight, Settings } from "lucide-react";+import { Badge } from "@/components/ui/badge";+import type { FunctionCall as FunctionCallType } from "../../_types/types";+import { FunctionCallResult } from "./result";++interface FunctionCallProps {+ fc: FunctionCallType;+ isExpanded: boolean;+ onToggle: () => void;+ className?: string;+}++export function FunctionCall({+ fc,+ isExpanded,+ onToggle,+ className = "",+}: FunctionCallProps) {+ const displayName =+ fc.type && fc.type !== fc.name ? `${fc.name} (${fc.type})` : fc.name;++ return (+ <div+ className={`fc-card bg-blue-500/10 border-blue-500/20 p-3 ${className}`}+ >+ <div+ className="flex items-center gap-2 cursor-pointer hover:bg-blue-500/5 -m-3 p-3 rounded-lg transition-colors"+ onClick={onToggle}+ >+ <Settings className="h-4 w-4 text-blue-400" />+ <span className="text-sm font-medium text-blue-400 flex-1">+ Function Call: {displayName}+ </span>+ {fc.id && (+ <span className="text-xs text-blue-400 font-mono">+ {fc.id.substring(0, 8)}...+ </span>+ )}+ <Badge+ variant="outline"+ className={+ fc.status === "completed"+ ? "fc-status-completed border-green-500/40 bg-green-500/20 text-green-400"+ : fc.status === "error"+ ? "border-red-500/40 bg-red-500/20 text-red-400"+ : "border-yellow-500/40 bg-yellow-500/20 text-yellow-400"+ }+ >+ {fc.status}+ </Badge>+ {isExpanded ? (+ <ChevronDown className="h-4 w-4 text-blue-400" />+ ) : (+ <ChevronRight className="h-4 w-4 text-blue-400" />+ )}+ </div>++ {isExpanded && (+ <div className="mt-3 pt-3 border-t border-blue-500/20">+ {fc.type && (+ <div className="text-xs text-muted-foreground mb-3">+ <span className="font-medium">Type:</span>+ <span className="fc-value ml-2 px-2 py-1 bg-muted/30 rounded font-mono">+ {fc.type}+ </span>+ </div>+ )}++ {fc.id && (+ <div className="text-xs text-muted-foreground mb-3">+ <span className="font-medium">ID:</span>+ <span className="fc-value ml-2 px-2 py-1 bg-muted/30 rounded font-mono">+ {fc.id}+ </span>+ </div>+ )}++ {(fc.arguments || fc.argumentsString) && (+ <div className="text-xs text-muted-foreground mb-3">+ <span className="font-medium">Arguments:</span>+ <pre className="mt-1 p-2 bg-muted/30 rounded text-xs overflow-x-auto">+ {fc.arguments+ ? JSON.stringify(fc.arguments, null, 2)+ : fc.argumentsString || "..."}+ </pre>+ </div>+ )}++ {fc.result && <FunctionCallResult result={fc.result} />}+ </div>+ )}+ </div>+ );+}diff --git a/frontend/app/chat/_components/function-calls/container.tsx b/frontend/app/chat/_components/function-calls/container.tsxnew file mode 100644index 000000000..43a623609--- /dev/null+++ b/frontend/app/chat/_components/function-calls/container.tsx@@ -0,0 +1,65 @@+import { useState } from "react";+import type { FunctionCall as FunctionCallType } from "../../_types/types";+import { FunctionCall } from "./call";+import { FunctionCallsHeader } from "./header";++interface FunctionCallsContainerProps {+ functionCalls: FunctionCallType[];+ messageIndex?: number;+ expandedFunctionCalls: Set<string>;+ onToggle: (functionCallId: string) => void;+ isStreaming?: boolean;+}++export function FunctionCallsContainer({+ functionCalls,+ messageIndex,+ expandedFunctionCalls,+ onToggle,+ isStreaming = false,+}: FunctionCallsContainerProps) {+ const [isGroupExpanded, setIsGroupExpanded] = useState(isStreaming);++ if (!functionCalls || functionCalls.length === 0) return null;++ const showGroupHeader = functionCalls.length > 1;+ const uniqueToolCount = new Set(functionCalls.map((fc) => fc.name)).size;+ const showCards = !showGroupHeader || isGroupExpanded;++ return (+ <div className="mb-3">+ {showGroupHeader && (+ <FunctionCallsHeader+ uniqueToolCount={uniqueToolCount}+ totalCalls={functionCalls.length}+ isExpanded={isGroupExpanded}+ onToggle={() => setIsGroupExpanded((v) => !v)}+ />+ )}+ {showCards &&+ functionCalls.map((fc, index) => {+ const functionCallId = `${messageIndex || "streaming"}-${index}`;+ const isExpanded = expandedFunctionCalls.has(functionCallId);+ const isFirst = index === 0;+ const isLast = index === functionCalls.length - 1;+ const topRounded = isFirst && !showGroupHeader;+ const bottomRounded = isLast;+ const radius = `${topRounded ? "rounded-t-lg" : ""} ${+ bottomRounded ? "rounded-b-lg" : ""+ }`.trim();+ const border =+ isFirst && !showGroupHeader ? "border" : "border border-t-0";++ return (+ <FunctionCall+ key={index}+ fc={fc}+ isExpanded={isExpanded}+ onToggle={() => onToggle(functionCallId)}+ className={`${border} ${radius}`}+ />+ );+ })}+ </div>+ );+}diff --git a/frontend/app/chat/_components/function-calls/header.tsx b/frontend/app/chat/_components/function-calls/header.tsxnew file mode 100644index 000000000..1b8784a77--- /dev/null+++ b/frontend/app/chat/_components/function-calls/header.tsx@@ -0,0 +1,35 @@+import { ChevronDown, ChevronRight, Settings } from "lucide-react";++interface FunctionCallsHeaderProps {+ uniqueToolCount: number;+ totalCalls: number;+ isExpanded: boolean;+ onToggle: () => void;+}++export function FunctionCallsHeader({+ uniqueToolCount,+ totalCalls,+ isExpanded,+ onToggle,+}: FunctionCallsHeaderProps) {+ return (+ <div+ className={`fc-card bg-function-call-header hover:bg-function-call-header-hover border border-blue-500/20 p-3 flex items-center gap-2 cursor-pointer transition-colors ${+ isExpanded ? "rounded-t-lg" : "rounded-lg"+ }`}+ onClick={onToggle}+ >+ <Settings className="h-4 w-4 text-blue-400" />+ <span className="text-sm font-medium text-blue-400 flex-1">+ Used {uniqueToolCount} {uniqueToolCount === 1 ? "tool" : "tools"} (+ {totalCalls} calls)+ </span>+ {isExpanded ? (+ <ChevronDown className="h-4 w-4 text-blue-400" />+ ) : (+ <ChevronRight className="h-4 w-4 text-blue-400" />+ )}+ </div>+ );+}diff --git a/frontend/app/chat/_components/function-calls/index.ts b/frontend/app/chat/_components/function-calls/index.tsnew file mode 100644index 000000000..7e8d9b531--- /dev/null+++ b/frontend/app/chat/_components/function-calls/index.ts@@ -0,0 +1 @@+export { FunctionCallsContainer } from "./container";diff --git a/frontend/app/chat/_components/function-calls/result.tsx b/frontend/app/chat/_components/function-calls/result.tsxnew file mode 100644index 000000000..8d41c6bf6--- /dev/null+++ b/frontend/app/chat/_components/function-calls/result.tsx@@ -0,0 +1,104 @@+import type { FunctionCall as FunctionCallType } from "../../_types/types";++type ToolResultItem = {+ text_key?: string;+ data?: { file_path?: string; text?: string };+ filename?: string;+ page?: number;+ score?: number;+ source_url?: string | null;+ text?: string;+};++interface FunctionCallResultProps {+ result: FunctionCallType["result"];+}++export function FunctionCallResult({ result }: FunctionCallResultProps) {+ if (!Array.isArray(result)) {+ return (+ <div className="text-xs text-muted-foreground">+ <span className="font-medium">Result:</span>+ <pre className="mt-1 p-2 bg-muted/30 rounded text-xs overflow-x-auto">+ {JSON.stringify(result, null, 2)}+ </pre>+ </div>+ );+ }++ const isNestedFormat =+ result.length > 0 &&+ result[0]?.results &&+ Array.isArray(result[0].results) &&+ !result[0].text_key;+ const items = (+ isNestedFormat ? result[0].results : result+ ) as ToolResultItem[];++ return (+ <div className="text-xs text-muted-foreground">+ <span className="font-medium">Result:</span>+ <div className="mt-1 space-y-2">+ {items.map((item, idx) => (+ <div key={idx} className="fc-result p-2 bg-muted/30 rounded">+ {item.data?.file_path && (+ <div className="font-medium text-blue-400 mb-1 text-xs">+ π {item.data.file_path || "Unknown file"}+ </div>+ )}++ {item.filename && !item.data?.file_path && (+ <div className="font-medium text-blue-400 mb-1 text-xs">+ π {item.filename}+ {item.page && ` (page ${item.page})`}+ {item.score && (+ <span className="ml-2 text-xs text-muted-foreground">+ Score: {item.score.toFixed(3)}+ </span>+ )}+ </div>+ )}++ {item.data?.text && (+ <div className="text-xs text-foreground whitespace-pre-wrap max-h-32 overflow-y-auto">+ {item.data.text.length > 300+ ? item.data.text.substring(0, 300) + "..."+ : item.data.text}+ </div>+ )}++ {item.text && !item.data?.text && (+ <div className="text-xs text-foreground whitespace-pre-wrap max-h-32 overflow-y-auto">+ {item.text.length > 300+ ? item.text.substring(0, 300) + "..."+ : item.text}+ </div>+ )}++ {item.source_url && (+ <div className="text-xs text-muted-foreground mt-1">+ <a+ href={item.source_url}+ target="_blank"+ rel="noopener noreferrer"+ className="text-blue-400 hover:underline"+ >+ Source URL+ </a>+ </div>+ )}++ {item.text_key && (+ <div className="text-xs text-muted-foreground mt-1">+ Key: {item.text_key}+ </div>+ )}+ </div>+ ))}+ <div className="text-xs text-muted-foreground">+ Found {items.length} result{items.length !== 1 ? "s" : ""}+ </div>+ </div>+ </div>+ );+}diff --git a/frontend/app/globals.css b/frontend/app/globals.cssindex fad853f2f..770721c4a 100644--- a/frontend/app/globals.css+++ b/frontend/app/globals.css@@ -89,6 +89,9 @@--failure-message: hsl(var(--foreground));--failure-scroll: #ff6464;--failure-muted: #a66262;++ --function-call-header: rgba(59, 130, 246, 0.18);+ --function-call-header-hover: rgba(59, 130, 246, 0.24);--failure-component-cause: 0 0% 32%; /* #525252 */}@@ -158,6 +161,9 @@--failure-message: #ffc9c9cc;--failure-scroll: #ff6464;--failure-muted: #a66262;++ --function-call-header: hsla(219, 30%, 9%, 1);+ --function-call-header-hover: hsla(219, 30%, 12%, 1);--failure-component-cause: 0 0% 32%; /* #525252 */}diff --git a/frontend/tailwind.config.ts b/frontend/tailwind.config.tsindex cbed098bc..b50d0e638 100644--- a/frontend/tailwind.config.ts+++ b/frontend/tailwind.config.ts@@ -166,6 +166,10 @@ const config = {"component-icon": "var(--component-icon)","flow-icon": "var(--flow-icon)","placeholder-foreground": "hsl(var(--placeholder-foreground))",+ "function-call-header": {+ DEFAULT: "var(--function-call-header)",+ hover: "var(--function-call-header-hover)",+ },badge: {DEFAULT: "hsl(var(--badge))",foreground: "hsl(var(--badge-foreground))",