Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

feat(frontend/copilot): add per-turn work-done summary stats (#12257)

Ubbe6abe39b33a1b
diff --git a/autogpt_platform/backend/backend/copilot/stream_registry.py b/autogpt_platform/backend/backend/copilot/stream_registry.py
index 80ddaa8321..4af11dcb5b 100644
--- a/autogpt_platform/backend/backend/copilot/stream_registry.py
+++ b/autogpt_platform/backend/backend/copilot/stream_registry.py
@@ -733,7 +733,10 @@ async def mark_session_completed(
# This is the SINGLE place that publishes StreamFinish — services and
# the processor must NOT publish it themselves.
try:
- await publish_chunk(turn_id, StreamFinish())
+ await publish_chunk(
+ turn_id,
+ StreamFinish(),
+ )
except Exception as e:
logger.error(
f"Failed to publish StreamFinish for session {session_id}: {e}. "
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx
index 2e0d8a0bb9..ffe55659db 100644
--- a/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/ChatMessagesContainer/ChatMessagesContainer.tsx
@@ -6,6 +6,8 @@ import {
import { Message, MessageContent } from "@/components/ai-elements/message";
import { LoadingSpinner } from "@/components/atoms/LoadingSpinner/LoadingSpinner";
import { FileUIPart, UIDataTypes, UIMessage, UITools } from "ai";
+import { TOOL_PART_PREFIX } from "../JobStatsBar/constants";
+import { TurnStatsBar } from "../JobStatsBar/TurnStatsBar";
import { parseSpecialMarkers } from "./helpers";
import { AssistantMessageActions } from "./components/AssistantMessageActions";
import { MessageAttachments } from "./components/MessageAttachments";
@@ -21,6 +23,23 @@ interface Props {
sessionID?: string | null;
}
+/** Collect all messages belonging to a turn: the user message + every
+ * assistant message up to (but not including) the next user message. */
+function getTurnMessages(
+ messages: UIMessage<unknown, UIDataTypes, UITools>[],
+ lastAssistantIndex: number,
+): UIMessage<unknown, UIDataTypes, UITools>[] {
+ const userIndex = messages.findLastIndex(
+ (m, i) => i < lastAssistantIndex && m.role === "user",
+ );
+ const nextUserIndex = messages.findIndex(
+ (m, i) => i > lastAssistantIndex && m.role === "user",
+ );
+ const start = userIndex >= 0 ? userIndex : lastAssistantIndex;
+ const end = nextUserIndex >= 0 ? nextUserIndex : messages.length;
+ return messages.slice(start, end);
+}
+
export function ChatMessagesContainer({
messages,
status,
@@ -31,9 +50,6 @@ export function ChatMessagesContainer({
}: Props) {
const lastMessage = messages[messages.length - 1];
- // Determine if something is visibly "in-flight" in the last assistant message:
- // - Text is actively streaming (last part is non-empty text)
- // - A tool call is pending (state is input-streaming or input-available)
const hasInflight = (() => {
if (lastMessage?.role !== "assistant") return false;
const parts = lastMessage.parts;
@@ -41,13 +57,11 @@ export function ChatMessagesContainer({
const lastPart = parts[parts.length - 1];
- // Text is actively being written
if (lastPart.type === "text" && lastPart.text.trim().length > 0)
return true;
- // A tool call is still pending (no output yet)
if (
- lastPart.type.startsWith("tool-") &&
+ lastPart.type.startsWith(TOOL_PART_PREFIX) &&
"state" in lastPart &&
(lastPart.state === "input-streaming" ||
lastPart.state === "input-available")
@@ -80,9 +94,13 @@ export function ChatMessagesContainer({
const isCurrentlyStreaming =
isLastAssistant &&
(status === "streaming" || status === "submitted");
+
+ const isAssistant = message.role === "assistant";
+
const nextMessage = messages[messageIndex + 1];
const isLastInTurn =
- message.role === "assistant" &&
+ isAssistant &&
+ messageIndex <= messages.length - 1 &&
(!nextMessage || nextMessage.role === "user");
const textParts = message.parts.filter(
(p): p is Extract<typeof p, { type: "text" }> => p.type === "text",
@@ -118,6 +136,11 @@ export function ChatMessagesContainer({
partIndex={i}
/>
))}
+ {isLastInTurn && !isCurrentlyStreaming && (
+ <TurnStatsBar
+ turnMessages={getTurnMessages(messages, messageIndex)}
+ />
+ )}
{isLastAssistant && showThinking && (
<ThinkingIndicator active={showThinking} />
)}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/JobStatsBar/TurnStatsBar.tsx b/autogpt_platform/frontend/src/app/(platform)/copilot/components/JobStatsBar/TurnStatsBar.tsx
new file mode 100644
index 0000000000..9735addea2
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/JobStatsBar/TurnStatsBar.tsx
@@ -0,0 +1,29 @@
+import type { UIDataTypes, UIMessage, UITools } from "ai";
+import { getWorkDoneCounters } from "./useWorkDoneCounters";
+
+interface Props {
+ turnMessages: UIMessage<unknown, UIDataTypes, UITools>[];
+}
+
+export function TurnStatsBar({ turnMessages }: Props) {
+ const { counters } = getWorkDoneCounters(turnMessages);
+
+ if (counters.length === 0) return null;
+
+ return (
+ <div className="mt-2 flex items-center gap-1.5">
+ {counters.map(function renderCounter(counter, index) {
+ return (
+ <span key={counter.category} className="flex items-center gap-1">
+ {index > 0 && (
+ <span className="text-xs text-neutral-300">&middot;</span>
+ )}
+ <span className="text-[11px] tabular-nums text-neutral-500">
+ {counter.count} {counter.label}
+ </span>
+ </span>
+ );
+ })}
+ </div>
+ );
+}
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/JobStatsBar/constants.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/JobStatsBar/constants.ts
new file mode 100644
index 0000000000..7b373db6a4
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/JobStatsBar/constants.ts
@@ -0,0 +1 @@
+export const TOOL_PART_PREFIX = "tool-";
diff --git a/autogpt_platform/frontend/src/app/(platform)/copilot/components/JobStatsBar/useWorkDoneCounters.ts b/autogpt_platform/frontend/src/app/(platform)/copilot/components/JobStatsBar/useWorkDoneCounters.ts
new file mode 100644
index 0000000000..69b65feac7
--- /dev/null
+++ b/autogpt_platform/frontend/src/app/(platform)/copilot/components/JobStatsBar/useWorkDoneCounters.ts
@@ -0,0 +1,75 @@
+import type { UIDataTypes, UIMessage, UITools } from "ai";
+import { TOOL_PART_PREFIX } from "./constants";
+
+const TOOL_TO_CATEGORY: Record<string, string> = {
+ find_agent: "search",
+ find_library_agent: "search",
+ run_agent: "agent run",
+ run_block: "block run",
+ create_agent: "agent created",
+ edit_agent: "agent edited",
+ schedule_agent: "agent scheduled",
+};
+
+const MAX_COUNTERS = 3;
+
+function pluralize(label: string, count: number): string {
+ if (count === 1) return label;
+
+ // "agent created" -> "agents created", "agent edited" -> "agents edited"
+ const nounVerbMatch = label.match(
+ /^(\w+)\s+(created|edited|scheduled|run)$/i,
+ );
+ if (nounVerbMatch) {
+ return pluralizeWord(nounVerbMatch[1]) + " " + nounVerbMatch[2];
+ }
+
+ return pluralizeWord(label);
+}
+
+function pluralizeWord(word: string): string {
+ if (word.endsWith("ch") || word.endsWith("sh") || word.endsWith("x"))
+ return word + "es";
+ return word + "s";
+}
+
+export interface WorkDoneCounter {
+ label: string;
+ count: number;
+ category: string;
+}
+
+export function getWorkDoneCounters(
+ messages: UIMessage<unknown, UIDataTypes, UITools>[],
+) {
+ const categoryCounts = new Map<string, number>();
+
+ for (const message of messages) {
+ if (message.role !== "assistant") continue;
+
+ for (const part of message.parts) {
+ if (!part.type.startsWith(TOOL_PART_PREFIX)) continue;
+
+ const toolName = part.type.replace(TOOL_PART_PREFIX, "");
+ const category = TOOL_TO_CATEGORY[toolName];
+ if (!category) continue;
+
+ categoryCounts.set(category, (categoryCounts.get(category) ?? 0) + 1);
+ }
+ }
+
+ const counters: WorkDoneCounter[] = Array.from(categoryCounts.entries())
+ .map(function toCounter([category, count]) {
+ return {
+ label: pluralize(category, count),
+ count,
+ category,
+ };
+ })
+ .sort(function byCountDesc(a, b) {
+ return b.count - a.count;
+ })
+ .slice(0, MAX_COUNTERS);
+
+ return { counters };
+}