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.

3.4 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, key, listeners, path, props } from 'kea'
import { loaders } from 'kea-loaders'

import { projectLogic } from 'scenes/projectLogic'

import { taskLogic } from 'products/posthog_ai/frontend/api/logics'
import { tasksRunsRetrieve } from 'products/tasks/frontend/generated/api'
import type { TaskRunDetailDTOApi } from 'products/tasks/frontend/generated/api.schemas'

import type { Task } from '../../../../products/posthog_ai/frontend/types/taskTypes'
import { todayAiChatLogic } from './todayAiChatLogic'

export interface TodayPanelTaskLogicProps {
    taskId: string
    /** The run the panel shows, which can be older than the task's latest. */
    runId: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayPanelTaskLogicValues {
    currentProjectId: number | null // projectLogic
    task: Task | null // taskLogic
    taskLoading: boolean // taskLogic
    run: TaskRunDetailDTOApi | null
    runLoading: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayPanelTaskLogicActions {
    loadTask: () => any // taskLogic
    startNewChat: () => {
        value: true
    } // todayAiChatLogic
    archive: () => {
        value: true
    }
    loadRun: () => any
    loadRunFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadRunSuccess: (
        run: TaskRunDetailDTOApi,
        payload?: any
    ) => {
        run: TaskRunDetailDTOApi
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface todayPanelTaskLogicMeta {
    key: string
}

export type todayPanelTaskLogicType = MakeLogicType<
    todayPanelTaskLogicValues,
    todayPanelTaskLogicActions,
    TodayPanelTaskLogicProps,
    todayPanelTaskLogicMeta
>

export const todayPanelTaskLogic = kea<todayPanelTaskLogicType>([
    props({} as TodayPanelTaskLogicProps),
    key((props) => `${props.taskId}:${props.runId}`),
    path((key) => ['layout', 'today', 'todayPanelTaskLogic', key]),
    connect((props: TodayPanelTaskLogicProps) => ({
        values: [taskLogic({ taskId: props.taskId }), ['task', 'taskLoading'], projectLogic, ['currentProjectId']],
        actions: [taskLogic({ taskId: props.taskId }), ['loadTask'], todayAiChatLogic, ['startNewChat']],
    })),
    actions({
        archive: true,
    }),
    loaders(({ props, values }) => ({
        run: [
            null as TaskRunDetailDTOApi | null,
            {
                loadRun: async () =>
                    await tasksRunsRetrieve(String(values.currentProjectId), props.taskId, props.runId),
            },
        ],
    })),
    listeners(({ actions, props }) => ({
        archive: async () => {
            try {
                await taskLogic({ taskId: props.taskId }).asyncActions.deleteTask({ leavePage: false })
            } catch {
                // The loader reports the failure. The panel stays on the task, which is still there.
                return
            }
            // An archived task has nothing left to show, so the panel moves on to a new chat.
            actions.startNewChat()
        },
    })),
    afterMount(({ actions, values }) => {
        if (!values.task && !values.taskLoading) {
            actions.loadTask()
        }
        actions.loadRun()
    }),
])