:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
feat(tasks): add pretty-print and hide-debug toggles to task log view (#62364)
Andrew Maguire31dfa6c303ed
diff --git a/products/tasks/frontend/components/TaskSessionView.tsx b/products/tasks/frontend/components/TaskSessionView.tsxindex 5959a51ceda2..da3c28196aad 100644--- a/products/tasks/frontend/components/TaskSessionView.tsx+++ b/products/tasks/frontend/components/TaskSessionView.tsx@@ -2,7 +2,7 @@ import { useEffect, useMemo, useState } from 'react'import { TextMorph } from 'torph/react'import { IconCopy } from '@posthog/icons'-import { LemonButton, LemonTag, Spinner } from '@posthog/lemon-ui'+import { LemonButton, LemonSwitch, LemonTag, Spinner } from '@posthog/lemon-ui'import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'@@ -208,6 +208,8 @@ export function TaskSessionView({initialPrompt,run,}: TaskSessionViewProps): JSX.Element {+ // Debug lines are noise by default; opt in to show them.+ const [showDebug, setShowDebug] = useState(false)const parsedLogs = useMemo(() => parseLogs(logs), [logs])// Use stream entries when available (real-time), otherwise fall back to parsed S3 logsconst entries = useMemo(() => {@@ -215,6 +217,15 @@ export function TaskSessionView({return filterDuplicateInitialPromptEntry(mergeDuplicateUserPromptEntries(sourceEntries), initialPrompt)}, [initialPrompt, parsedLogs, streamEntries])+ const debugCount = useMemo(+ () => entries.filter((entry) => entry.type === 'console' && entry.level === 'debug').length,+ [entries]+ )+ const visibleEntries = useMemo(+ () => (showDebug ? entries : entries.filter((entry) => !(entry.type === 'console' && entry.level === 'debug'))),+ [entries, showDebug]+ )+const handleCopyLogs = (): void => {navigator.clipboard.writeText(logs).then(() => lemonToast.success('Logs copied to clipboard'),@@ -242,14 +253,25 @@ export function TaskSessionView({<div className="flex justify-between items-center px-4 py-2 border-b"><div className="flex items-center gap-2">{run && <TaskRunStatusBadge run={run} />}- <span className="text-sm font-semibold">Logs ({entries.length})</span>+ <span className="text-sm font-semibold">Logs ({visibleEntries.length})</span>+ </div>+ <div className="flex items-center gap-2">+ {debugCount > 0 && (+ <LemonSwitch+ label={`Show debug (${debugCount})`}+ checked={showDebug}+ onChange={setShowDebug}+ size="xsmall"+ bordered+ />+ )}+ <LemonButton size="xsmall" icon={<IconCopy />} onClick={handleCopyLogs}>+ Copy+ </LemonButton></div>- <LemonButton size="xsmall" icon={<IconCopy />} onClick={handleCopyLogs}>- Copy- </LemonButton></div><div className="flex-1 overflow-auto p-4 font-mono text-sm bg-bg-3000">- {entries.map((entry) => (+ {visibleEntries.map((entry) => (<LogEntryRenderer key={entry.id} entry={entry} />))}{(isPolling || isStreaming) && <HedgehogStatus />}diff --git a/products/tasks/frontend/components/session/ToolCallEntry.tsx b/products/tasks/frontend/components/session/ToolCallEntry.tsxindex 879196f774fe..0948d04e6ed1 100644--- a/products/tasks/frontend/components/session/ToolCallEntry.tsx+++ b/products/tasks/frontend/components/session/ToolCallEntry.tsx@@ -1,7 +1,7 @@import { useState } from 'react'import { IconChevronRight, IconTerminal } from '@posthog/icons'-import { LemonTag, Spinner } from '@posthog/lemon-ui'+import { LemonSwitch, LemonTag, Spinner } from '@posthog/lemon-ui'import { ToolStatus } from '../../lib/parse-logs'@@ -20,8 +20,75 @@ const STATUS_CONFIG: Record<ToolStatus, { label: string; type: 'default' | 'primerror: { label: 'Failed', type: 'danger' },}+function isTextPart(item: unknown): item is { type: 'text'; text: string } {+ return (+ typeof item === 'object' &&+ item !== null &&+ (item as Record<string, unknown>).type === 'text' &&+ typeof (item as Record<string, unknown>).text === 'string'+ )+}++// Returns the parsed value only when the string is a JSON object/array, otherwise undefined.+function tryParseJson(text: string): unknown {+ const trimmed = text.trim()+ const first = trimmed[0]+ if (first !== '{' && first !== '[') {+ return undefined+ }+ try {+ return JSON.parse(trimmed)+ } catch {+ return undefined+ }+}++// Recursively expand any string that is itself JSON, so double-encoded payloads render as real structure.+function expandEmbeddedJson(value: unknown): unknown {+ if (typeof value === 'string') {+ const parsed = tryParseJson(value)+ return parsed !== undefined ? expandEmbeddedJson(parsed) : value+ }+ if (Array.isArray(value)) {+ return value.map(expandEmbeddedJson)+ }+ if (value && typeof value === 'object') {+ return Object.fromEntries(+ Object.entries(value as Record<string, unknown>).map(([key, val]) => [key, expandEmbeddedJson(val)])+ )+ }+ return value+}++// MCP tool results usually arrive as [{ type: 'text', text: '...' }]; pull the text out so newlines render.+function extractTextParts(value: unknown): string | null {+ const items = Array.isArray(value) ? value : [value]+ const texts: string[] = []+ for (const item of items) {+ if (!isTextPart(item)) {+ return null+ }+ texts.push(item.text)+ }+ return texts.length ? texts.join('\n\n') : null+}++function formatValue(value: unknown, pretty: boolean): string {+ if (!pretty) {+ return typeof value === 'string' ? value : JSON.stringify(value, null, 2)+ }+ const text = extractTextParts(value)+ const candidate = text ?? (typeof value === 'string' ? value : null)+ if (candidate !== null) {+ const parsed = tryParseJson(candidate)+ return parsed !== undefined ? JSON.stringify(expandEmbeddedJson(parsed), null, 2) : candidate+ }+ return JSON.stringify(expandEmbeddedJson(value), null, 2)+}+export function ToolCallEntry({ toolName, status, args, result, timestamp }: ToolCallEntryProps): JSX.Element {const [isOpen, setIsOpen] = useState(false)+ const [pretty, setPretty] = useState(true)const config = STATUS_CONFIG[status]const hasContent = args || result !== undefinedconst isLoading = status === 'pending' || status === 'running'@@ -59,11 +126,20 @@ export function ToolCallEntry({ toolName, status, args, result, timestamp }: Too{isOpen && hasContent && (<div className="ml-5 mt-1 rounded bg-bg-light p-2 overflow-hidden">+ <div className="flex justify-end mb-1">+ <LemonSwitch+ label="Pretty print"+ checked={pretty}+ onChange={setPretty}+ size="xsmall"+ bordered+ />+ </div>{args && (<div className="mb-2"><div className="text-xs font-medium text-muted mb-1">Arguments</div><pre className="text-xs overflow-x-auto whitespace-pre-wrap break-words">- {JSON.stringify(args, null, 2)}+ {formatValue(args, pretty)}</pre></div>)}@@ -71,7 +147,7 @@ export function ToolCallEntry({ toolName, status, args, result, timestamp }: Too<div><div className="text-xs font-medium text-muted mb-1">Result</div><pre className="text-xs overflow-x-auto whitespace-pre-wrap break-words">- {typeof result === 'string' ? result : JSON.stringify(result, null, 2)}+ {formatValue(result, pretty)}</pre></div>)}