Skip to content

: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.

feat(tasks): render task runs with PostHog AI sandbox renderer (#65442)

Georgiy Tarasov188be0cc28ba
diff --git a/products/tasks/frontend/components/TaskDetailPage.tsx b/products/tasks/frontend/components/TaskDetailPage.tsx
index 3ba13264d7ee..8b8e6e17a358 100644
--- a/products/tasks/frontend/components/TaskDetailPage.tsx
+++ b/products/tasks/frontend/components/TaskDetailPage.tsx
@@ -31,8 +31,8 @@ import {
import { taskDetailSceneLogic } from '../logics/taskDetailSceneLogic'
import { CollapsibleContent } from './CollapsibleContent'
+import { TaskRunChat } from './TaskRunChat'
import { TaskRunItem } from './TaskRunItem'
-import { TaskSessionView } from './TaskSessionView'
export interface TaskDetailPageProps {
taskId: string
@@ -40,19 +40,7 @@ export interface TaskDetailPageProps {
export function TaskDetailPage({ taskId }: TaskDetailPageProps): JSX.Element {
const sceneLogic = taskDetailSceneLogic({ taskId })
- const {
- task,
- taskLoading,
- runs,
- selectedRunId,
- selectedRun,
- runsLoading,
- logs,
- logsLoading,
- shouldPoll,
- streamEntries,
- isStreaming,
- } = useValues(sceneLogic)
+ const { task, taskLoading, runs, selectedRunId, selectedRun, runsLoading } = useValues(sceneLogic)
const { setSelectedRunId, runTask, deleteTask } = useActions(sceneLogic)
const { featureFlags } = useValues(featureFlagLogic)
const sceneMenuBarEnabled = !!featureFlags[FEATURE_FLAGS.SCENE_MENU_BAR]
@@ -225,15 +213,7 @@ export function TaskDetailPage({ taskId }: TaskDetailPageProps): JSX.Element {
</div>
) : selectedRun ? (
<div className="flex-1 overflow-hidden">
- <TaskSessionView
- logs={logs}
- logsLoading={logsLoading}
- streamEntries={streamEntries}
- isPolling={shouldPoll}
- isStreaming={isStreaming}
- initialPrompt={task.description}
- run={selectedRun}
- />
+ <TaskRunChat taskId={task.id} runId={selectedRun.id} />
</div>
) : null}
</SceneContent>
diff --git a/products/tasks/frontend/components/TaskRunChat.tsx b/products/tasks/frontend/components/TaskRunChat.tsx
new file mode 100644
index 000000000000..eb60f5233a17
--- /dev/null
+++ b/products/tasks/frontend/components/TaskRunChat.tsx
@@ -0,0 +1,99 @@
+import { BindLogic, useActions, useValues } from 'kea'
+import { useState } from 'react'
+
+import { LemonButton, LemonTextArea } from '@posthog/lemon-ui'
+
+import {
+ isTerminalRunStatus,
+ SandboxContextUsage,
+ SandboxPermissionInput,
+ SandboxQuestionInput,
+ SandboxResourcesBar,
+ SandboxThreadView,
+ sandboxStreamLogic,
+} from 'products/posthog_ai/frontend/sandbox'
+
+import { taskRunChatLogic } from '../logics/taskRunChatLogic'
+
+export interface TaskRunChatProps {
+ taskId: string
+ runId: string
+}
+
+export function TaskRunChat({ taskId, runId }: TaskRunChatProps): JSX.Element {
+ // The outer BindLogic binds the shared sandboxStreamLogic instance (keyed by runId) consumed by
+ // both the renderer (thread/permission/question views) and taskRunChatLogic. We bind in live mode;
+ // bootstrapRun (called from taskRunChatLogic.afterMount) reads the run status from the tasks API and
+ // never opens SSE for an already-terminal run — same pattern SandboxRunViewer uses.
+ return (
+ <BindLogic logic={sandboxStreamLogic} props={{ streamKey: runId }}>
+ <BindLogic logic={taskRunChatLogic} props={{ taskId, runId }}>
+ <TaskRunChatContent taskId={taskId} runId={runId} />
+ </BindLogic>
+ </BindLogic>
+ )
+}
+
+function TaskRunChatContent({ taskId, runId }: TaskRunChatProps): JSX.Element {
+ const { pendingPermissionRequest, currentRunStatus } = useValues(sandboxStreamLogic({ streamKey: runId }))
+ const { sendMessage } = useActions(taskRunChatLogic({ taskId, runId }))
+ const { sendingMessage } = useValues(taskRunChatLogic({ taskId, runId }))
+
+ const isTerminal = isTerminalRunStatus(currentRunStatus)
+ const [composerText, setComposerText] = useState('')
+
+ const isQuestion = !!pendingPermissionRequest?.questions && pendingPermissionRequest.questions.length > 0
+
+ const handleSend = (): void => {
+ const trimmed = composerText.trim()
+ if (!trimmed || sendingMessage || isTerminal) {
+ return
+ }
+ sendMessage(trimmed)
+ setComposerText('')
+ }
+
+ return (
+ <div className="flex flex-col h-full overflow-hidden">
+ <div className="flex-1 overflow-y-auto px-4 py-2">
+ <SandboxThreadView />
+ </div>
+
+ <SandboxResourcesBar />
+
+ {pendingPermissionRequest && !isTerminal && (
+ <div className="border-t px-4 py-3">
+ {isQuestion ? (
+ <SandboxQuestionInput streamKey={runId} request={pendingPermissionRequest} />
+ ) : (
+ <SandboxPermissionInput streamKey={runId} request={pendingPermissionRequest} />
+ )}
+ </div>
+ )}
+
+ {!isTerminal && !pendingPermissionRequest && (
+ <div className="border-t px-4 py-3 flex gap-2 items-end">
+ <LemonTextArea
+ className="flex-1"
+ value={composerText}
+ onChange={setComposerText}
+ placeholder="Send a follow-up message…"
+ minRows={1}
+ maxRows={8}
+ onPressCmdEnter={handleSend}
+ />
+ <LemonButton
+ type="primary"
+ onClick={handleSend}
+ loading={sendingMessage}
+ disabledReason={!composerText.trim() ? 'Type a message first' : undefined}
+ >
+ Send
+ </LemonButton>
+ </div>
+ )}
+
+ <SandboxContextUsage />
+ </div>
+ )
+}
diff --git a/products/tasks/frontend/logics/taskRunChatLogic.ts b/products/tasks/frontend/logics/taskRunChatLogic.ts
new file mode 100644
index 000000000000..67df4f6c44d8
--- /dev/null
+++ b/products/tasks/frontend/logics/taskRunChatLogic.ts
@@ -0,0 +1,80 @@
+import { actions, afterMount, connect, kea, key, listeners, path, props, reducers, selectors } from 'kea'
+
+import { lemonToast } from 'lib/lemon-ui/LemonToast'
+import { projectLogic } from 'scenes/projectLogic'
+
+import { isTerminalRunStatus, sandboxStreamLogic } from 'products/posthog_ai/frontend/sandbox'
+import { tasksRunsCommandCreate } from 'products/tasks/frontend/generated/api'
+
+import type { taskRunChatLogicType } from './taskRunChatLogicType'
+
+export interface TaskRunChatLogicProps {
+ taskId: string
+ runId: string
+}
+
+export const taskRunChatLogic = kea<taskRunChatLogicType>([
+ path(['products', 'tasks', 'logics', 'taskRunChatLogic']),
+ props({} as TaskRunChatLogicProps),
+ key((props) => props.runId),
+
+ connect((props: TaskRunChatLogicProps) => ({
+ values: [
+ projectLogic,
+ ['currentProjectId'],
+ sandboxStreamLogic({ streamKey: props.runId }),
+ ['currentRunStatus'],
+ ],
+ actions: [sandboxStreamLogic({ streamKey: props.runId }), ['bootstrapRun', 'pushHumanMessage', 'reset']],
+ })),
+
+ actions({
+ sendMessage: (content: string) => ({ content }),
+ setSendingMessage: (sending: boolean) => ({ sending }),
+ }),
+
+ reducers({
+ sendingMessage: [
+ false,
+ {
+ setSendingMessage: (_, { sending }) => sending,
+ },
+ ],
+ }),
+
+ selectors({
+ isTerminal: [(s) => [s.currentRunStatus], (status): boolean => isTerminalRunStatus(status)],
+ }),
+
+ listeners(({ actions, values, props }) => ({
+ sendMessage: async ({ content }) => {
+ if (values.sendingMessage || !content.trim() || values.isTerminal) {
+ return
+ }
+ if (values.currentProjectId == null) {
+ return
+ }
+ actions.setSendingMessage(true)
+ try {
+ await tasksRunsCommandCreate(String(values.currentProjectId), props.taskId, props.runId, {
+ jsonrpc: '2.0',
+ method: 'user_message',
+ params: { content },
+ })
+ actions.pushHumanMessage(content)
+ } catch {
+ lemonToast.error('Failed to send message. Please try again.')
+ } finally {
+ actions.setSendingMessage(false)
+ }
+ },
+ })),
+
+ afterMount(({ props, actions }) => {
+ // sandboxStreamLogic is already bound (the BindLogic in TaskRunChat mounts it first), so a
+ // reset + bootstrapRun here re-bootstraps cleanly when a reused logic instance is remounted.
+ // Live vs. replay mode is resolved inside bootstrapRun from the run status the tasks API returns.
+ actions.reset()
+ actions.bootstrapRun({ taskId: props.taskId, runId: props.runId })
+ }),
+])