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.
import { getSystemHeaders } from "@/lib/impersonation";
import { getPostHogSessionHeader } from "@/services/analytics/posthog-request-context";
import { getWebSocketToken } from "@/lib/auth/actions";
import type { ChatStatus, UIMessage } from "ai";
import { deleteV2DisconnectSessionStream } from "@/app/api/__generated__/endpoints/chat/chat";
import { TOOL_PART_PREFIX } from "./components/JobStatsBar/constants";
import { parseSpecialMarkers } from "./helpers/messageMarkers";
import { WORKSPACE_FOLDER_PART_TYPE } from "./helpers/workspaceAttachments";
import { TURN_PAUSED_PART_TYPE } from "./turnPause";
/**
* Title/body/icon for the OS-level notification fired when a copilot session
* completes. Kept in sync with the same copy hardcoded in `public/push-sw.js`
* (NOTIFICATION_MAP.copilot_completion.session_completed) — the SW file is
* plain JS served from /public and can't import from this module, so the two
* sources are matched by test rather than by reference.
*/
export const COPILOT_COMPLETION_NOTIFICATION = {
title: "AutoGPT",
body: "Task completed",
icon: "/notification-icon-192.png",
} as const;
/**
* Returns HTTP headers required for direct backend requests from copilot:
* - Authorization Bearer token (JWT)
* - X-Act-As-User-Id impersonation header (if an admin is impersonating a user)
* - X-PostHog-Session-Id (with analytics consent), so the chat's backend
* events join the browser's PostHog session
*
* Use this for all direct-to-backend fetch/SSE calls so that admin user
* impersonation works consistently across the entire copilot feature.
*/
export async function getCopilotAuthHeaders(): Promise<Record<string, string>> {
const { token, error } = await getWebSocketToken();
if (error || !token) {
console.warn("[Copilot] Failed to get auth token:", error);
throw new CopilotAuthError();
}
return {
Authorization: `Bearer ${token}`,
...getSystemHeaders(),
...getPostHogSessionHeader(),
};
}
/** The server answered and has no session token: signed out, not offline. */
export class CopilotAuthError extends Error {
constructor() {
super("Authentication failed — please sign in again.");
this.name = "CopilotAuthError";
}
}
/**
* Safely parse a JSON string (from localStorage) into a `Set<string>` of
* session IDs. Returns an empty set for `null`, malformed, or non-array values.
*/
export function parseSessionIDs(raw: string | null | undefined): Set<string> {
if (!raw) return new Set();
try {
const parsed: unknown = JSON.parse(raw);
return Array.isArray(parsed)
? new Set<string>(parsed.filter((v) => typeof v === "string"))
: new Set();
} catch {
return new Set();
}
}
/**
* Resolve the actual dry_run value for a session from the raw API response.
* Returns true only when the session response is a 200 with metadata.dry_run === true.
* Returns false for missing/non-200 responses so callers never show a stale
* preference value when the real session state is unknown.
*/
export function resolveSessionDryRun(queryData: unknown): boolean {
if (
queryData == null ||
typeof queryData !== "object" ||
!("status" in queryData) ||
(queryData as { status: unknown }).status !== 200
)
return false;
const d = queryData as { data?: { metadata?: { dry_run?: unknown } } };
return d.data?.metadata?.dry_run === true;
}
/**
* Check whether a refetchSession result indicates the backend still has an
* active SSE stream for this session.
*/
export function hasActiveBackendStream(result: { data?: unknown }): boolean {
const d = result.data;
return (
d != null &&
typeof d === "object" &&
"status" in d &&
d.status === 200 &&
"data" in d &&
d.data != null &&
typeof d.data === "object" &&
"active_stream" in d.data &&
!!d.data.active_stream
);
}
/**
* Whether the trailing assistant message has at least one part the UI
* would visibly render: text with non-empty content, reasoning with
* non-empty content, any tool part (tool cards render regardless of
* state), or a Paused card — a turn refused before it ran streams nothing
* else. Used to gate the resume-snapshot discard — the replay may stream
* empty reasoning-start / step-start chunks for minutes before any
* rendered content (e.g. Perplexity deep research), and we do not want to
* drop the pre-replay snapshot until the user actually sees something.
*/
export function hasVisibleAssistantContent(messages: UIMessage[]): boolean {
const last = messages[messages.length - 1];
if (last?.role !== "assistant") return false;
return last.parts.some((part) => {
if (part.type === "text" && part.text.trim().length > 0) return true;
if (part.type === "reasoning" && part.text.trim().length > 0) return true;
if (part.type.startsWith(TOOL_PART_PREFIX)) return true;
if (part.type === TURN_PAUSED_PART_TYPE) return true;
return false;
});
}
/** Mark any in-progress tool parts as completed/errored so spinners stop. */
export function resolveInProgressTools(
messages: UIMessage[],
outcome: "completed" | "cancelled",
): UIMessage[] {
return messages.map((msg) => ({
...msg,
parts: msg.parts.map((part) =>
"state" in part &&
(part.state === "input-streaming" || part.state === "input-available")
? outcome === "cancelled"
? { ...part, state: "output-error" as const, errorText: "Cancelled" }
: { ...part, state: "output-available" as const, output: "" }
: part,
),
}));
}
const IN_PROGRESS_PART_STATES = new Set([
"streaming",
"input-streaming",
"input-available",
]);
/**
* True if the message is an assistant message with at least one part that
* the stream never finalised — i.e. text / reasoning in ``streaming`` or
* tool parts in ``input-streaming`` / ``input-available``. Used both for
* the partial-snapshot discard during resume and for zombie-part recovery
* on session re-entry.
*/
export function hasInProgressAssistantParts(
message: UIMessage | undefined,
): boolean {
if (message?.role !== "assistant") return false;
return message.parts.some((part) => {
if (!("state" in part) || typeof part.state !== "string") return false;
return IN_PROGRESS_PART_STATES.has(part.state);
});
}
const COPILOT_INTERRUPTED_MARKER =
"[__COPILOT_RETRYABLE_ERROR_a9c2__] Response was interrupted. Resend to try again.";
/**
* Close the last assistant message when the stream ended without
* finalising it (backend crash mid-write, the user switched away and the
* DB snapshot rehydrated with orphaned in-progress parts, etc.). Tool
* parts in ``input-streaming`` / ``input-available`` flip to
* ``output-error`` "Interrupted" so their spinners stop; text / reasoning
* parts in ``streaming`` flip to ``done`` so their typing animation ends
* but the partial content is preserved. A retryable-error marker is
* appended so the UI renders a "resend to try again" affordance.
*
* Only the last message is touched — earlier messages can't have unclosed
* parts in a healthy session. Returns the original array when no repair
* is needed, so callers can cheaply compare references.
*/
export function resolveInterruptedMessage(messages: UIMessage[]): UIMessage[] {
if (messages.length === 0) return messages;
const lastIdx = messages.length - 1;
const last = messages[lastIdx];
if (!hasInProgressAssistantParts(last)) return messages;
const resolvedParts = last.parts.map((part) => {
if (!("state" in part) || typeof part.state !== "string") return part;
if (part.state === "input-streaming" || part.state === "input-available") {
return {
...part,
state: "output-error" as const,
errorText: "Interrupted",
};
}
if (part.state === "streaming") {
return { ...part, state: "done" as const };
}
return part;
});
return [
...messages.slice(0, lastIdx),
{
...last,
parts: [
...resolvedParts,
{ type: "text" as const, text: COPILOT_INTERRUPTED_MARKER },
],
},
];
}
/**
* Extract the user-visible text from the arguments passed to `sendMessage`.
* Handles both `sendMessage("hello")` and `sendMessage({ text: "hello" })`.
*/
export function extractSendMessageText(firstArg: unknown): string {
if (firstArg && typeof firstArg === "object" && "text" in firstArg)
return (firstArg as { text: string }).text;
return String(firstArg ?? "");
}
/** What the files and folders passed to `sendMessage` point at, sorted, so
* two sends can be told apart when their text is the same. */
export function extractSendMessageAttachments(firstArg: unknown): string[] {
if (!firstArg || typeof firstArg !== "object") return [];
if ("files" in firstArg && Array.isArray(firstArg.files))
return attachmentKeys(firstArg.files);
if ("parts" in firstArg && Array.isArray(firstArg.parts))
return attachmentKeys(firstArg.parts);
return [];
}
function attachmentKeys(parts: readonly unknown[]): string[] {
return parts
.flatMap((part) => {
if (!part || typeof part !== "object" || !("type" in part)) return [];
if (part.type === "file" && "url" in part)
return typeof part.url === "string" ? [part.url] : [];
if (part.type === WORKSPACE_FOLDER_PART_TYPE && "data" in part) {
const data = part.data as { id?: unknown } | null;
return typeof data?.id === "string" ? [`folder:${data.id}`] : [];
}
return [];
})
.sort();
}
interface SuppressDuplicateArgs {
text: string;
/** The outgoing message's attachments, from `extractSendMessageAttachments`. */
attachments?: string[];
isReconnectScheduled: boolean;
lastSubmittedText: string | null;
messages: UIMessage[];
status?: ChatStatus;
}
/**
* Reason a sendMessage was suppressed, or ``null`` to pass through.
*
* - ``"reconnecting"``: the stream is reconnecting; the caller should
* notify the user (the UI may not yet reflect the disabled state).
* - ``"duplicate"``: the same text, with the same attachments, was just
* submitted, echoed back by the session and not answered yet — safe to
* silently drop (user double-clicked), because the message is already
* on screen.
*/
export type SuppressReason = "reconnecting" | "duplicate" | null;
/**
* Determine whether a sendMessage call should be suppressed to prevent
* duplicate POSTs during reconnect cycles or re-submits of the same text.
*
* Returns the reason so callers can surface user-visible feedback when
* the suppression isn't just a silent duplicate.
*/
export function getSendSuppressionReason({
text,
attachments = [],
isReconnectScheduled,
lastSubmittedText,
messages,
status,
}: SuppressDuplicateArgs): SuppressReason {
if (isReconnectScheduled) return "reconnecting";
const lastMessage = messages.at(-1);
const hasRetryableError =
status === "ready" &&
lastMessage?.role === "assistant" &&
lastMessage.parts.some(
(part) =>
part.type === "text" &&
parseSpecialMarkers(part.text).markerType === "retryable_error",
);
if (status === "error" || hasRetryableError) return null;
if (text && lastSubmittedText === text) {
const lastUserMsg = messages.filter((m) => m.role === "user").pop();
const lastUserText = lastUserMsg?.parts
?.map((p) => ("text" in p ? p.text : ""))
.join("")
.trim();
// The same caption on other files is a new message, not a resend.
const lastUserAttachments = attachmentKeys(lastUserMsg?.parts ?? []);
const isResend =
lastUserText === text &&
lastUserAttachments.join("\n") === attachments.join("\n");
// A resend is a duplicate only while the first send is still being
// answered: the turn is in flight, or its echo is the newest message.
// Once a reply (or a Stop marker) followed it, "yes" after "yes" is a
// new message, and dropping it would lose text the composer already cleared.
const stillBeingAnswered =
status === "streaming" ||
status === "submitted" ||
lastMessage?.role === "user";
if (isResend && stillBeingAnswered) return "duplicate";
}
return null;
}
/**
* Backwards-compatible boolean wrapper for ``getSendSuppressionReason``.
*
* @deprecated Call ``getSendSuppressionReason`` directly so callers can
* distinguish between reconnect and duplicate suppression.
*/
export function shouldSuppressDuplicateSend(
args: SuppressDuplicateArgs,
): boolean {
return getSendSuppressionReason(args) !== null;
}
/**
* Fire-and-forget: tell the backend to release XREAD listeners for a session.
*
* Called on session switch so the backend doesn't wait for its 5-10 s timeout
* before cleaning up. Failures are silently ignored — the backend will
* eventually clean up on its own.
*/
export function disconnectSessionStream(sessionId: string): void {
deleteV2DisconnectSessionStream(sessionId).catch(() => {});
}
/**
* Decide whether a reconnect request must be coalesced onto the debounce
* window boundary, rather than firing immediately.
*
* Returns the remaining milliseconds until the window closes (so the caller
* can schedule a `setTimeout` for that delay) when the previous resume
* happened inside the window, or `null` to let the reconnect proceed now.
*
* `lastResumeAt === 0` signals "no reconnect has fired yet in this session"
* — the first reconnect always passes through regardless of `now`.
*/
export function shouldDebounceReconnect(
lastResumeAt: number,
now: number,
windowMs: number,
): number | null {
if (lastResumeAt <= 0) return null;
const sinceLastResume = now - lastResumeAt;
if (sinceLastResume >= windowMs) return null;
return windowMs - sinceLastResume;
}
/**
* Deduplicate messages by ID and by consecutive content fingerprint.
*
* ID dedup catches exact duplicates within the same source.
* Content dedup uses a composite key of `role + preceding-user-message-id +
* content-fingerprint` to detect replayed messages that arrive with new
* IDs after an SSE reconnection replays from the beginning of the Redis
* stream. Scoping by user message ID (not text) preserves the second
* assistant reply when the user asks the same question twice and gets the
* same answer — two different user messages produce two different IDs even
* when their text is identical.
*/
export function deduplicateMessages(messages: UIMessage[]): UIMessage[] {
const seenIds = new Set<string>();
// Fingerprints seen per user turn. Nested rather than one composite
// `${userID}:${fingerprint}` string, which would be rebuilt and re-hashed in
// full for every assistant message on every call; the cached fingerprint
// string keeps its hash.
const seenFingerprintsByTurn = new Map<string, Set<string>>();
let lastUserMsgID = "";
const newestIndex = messages.length - 1;
const kept = messages.filter((msg, index) => {
if (seenIds.has(msg.id)) return false;
seenIds.add(msg.id);
if (msg.role === "user") {
// Track the ID (not text) of the latest user message so we can scope
// assistant fingerprints to their conversational turn. Using the ID
// means two user messages with identical text are still treated as
// distinct turns, preventing false-positive deduplication.
lastUserMsgID = msg.id;
}
if (msg.role === "assistant") {
const contentFingerprint = getContentFingerprint(
msg,
index === newestIndex,
);
if (contentFingerprint !== "[]") {
// Scope to the preceding user message turn so that identical assistant
// replies to *different* user prompts are preserved.
// NOTE: A streaming (in-progress) assistant message has a partial
// fingerprint that differs from its final form, so it would not be
// caught by this dedup. This is safe because every caller that invokes
// resumeStream() first strips the in-progress assistant message —
// handleReconnect, the wake-resync path, and the hydration-effect path
// all do this. See useCopilotStream.ts.
let seenInTurn = seenFingerprintsByTurn.get(lastUserMsgID);
if (!seenInTurn) {
seenInTurn = new Set<string>();
seenFingerprintsByTurn.set(lastUserMsgID, seenInTurn);
}
if (seenInTurn.has(contentFingerprint)) return false;
seenInTurn.add(contentFingerprint);
}
}
return true;
});
// Same array back when nothing was dropped, so memos keyed on it hold.
return kept.length === messages.length ? messages : kept;
}
// Keyed by the parts array, which every streamed update replaces, so a
// message that has not changed is never serialised twice. The length check
// catches parts pushed in place. The newest message is never cached: the SDK
// can still be appending to it in place until its next flush.
const contentFingerprints = new WeakMap<
UIMessage["parts"],
{ partCount: number; fingerprint: string }
>();
function getContentFingerprint(msg: UIMessage, isNewest: boolean): string {
if (isNewest) return computeContentFingerprint(msg.parts);
const cached = contentFingerprints.get(msg.parts);
if (cached && cached.partCount === msg.parts.length) {
return cached.fingerprint;
}
const fingerprint = computeContentFingerprint(msg.parts);
contentFingerprints.set(msg.parts, {
partCount: msg.parts.length,
fingerprint,
});
return fingerprint;
}
function computeContentFingerprint(parts: UIMessage["parts"]): string {
// JSON.stringify the parts array to avoid separator-collision false
// positives: a plain join("|") on ["a|b", "c"] and ["a", "b|c"]
// produces the same string. JSON encoding each element is unambiguous.
// Fall back to JSON.stringify(p) for parts that carry neither a text nor
// a toolCallId (e.g. step-start) so structurally different parts never
// collapse to the same empty-string fingerprint element.
return JSON.stringify(
parts.map(
(p) =>
("text" in p && p.text) ||
("toolCallId" in p && p.toolCallId) ||
JSON.stringify(p),
),
);
}
/**
* True when the server reports it moved a turn to a different execution
* engine. Nothing displays the engine and nothing can request one — the only
* consumer widens its post-finish refetch window, because a switch takes
* longer to settle. The named engine is deliberately not returned.
*/
export function isEngineSwitchPart(dataPart: {
type: string;
data?: unknown;
}): boolean {
if (dataPart.type !== "data-mode-changed") return false;
const mode = (dataPart.data as { mode?: string } | undefined)?.mode;
return mode === "extended_thinking" || mode === "fast";
}
export const CREATE_SESSION_RETRY_DELAY_MS = 500;
// A network error on create ("Load failed" on Safari, "Failed to fetch" on
// Chrome) can mean the server committed and only the response was lost. The
// request names its session id, so one retry adopts that session instead of
// bouncing the message back to the composer.
export function retryLostCreateResponse(failureCount: number, error: unknown) {
return failureCount < 1 && error instanceof TypeError;
}