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.7 KB
Raw Blame
import { MakeLogicType, actions, kea, key, path, props, reducers } from 'kea'

import { uuid } from 'lib/utils/dom'

import type { CommentContext } from '../utils/commentContext'

export interface TaskCommentContextsLogicProps {
    taskId: string
}

export interface PendingCommentContext extends CommentContext {
    id: string
    screenshotFile?: File
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface taskCommentContextsLogicValues {
    commentContexts: PendingCommentContext[]
    pendingCommentText: string[]
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface taskCommentContextsLogicActions {
    addCommentToChat: (
        context: CommentContext | null,
        comment: string,
        screenshotFile?: File
    ) => {
        comment: string
        context: {
            body: string
            id: string
            label: string
            screenshot?: string | undefined
            screenshotFile?: File | undefined
        } | null
    }
    clearCommentContexts: () => {
        value: true
    }
    consumePendingCommentText: () => {
        value: true
    }
    removeCommentContext: (id: string) => {
        id: string
    }
    restoreCommentContexts: (contexts: CommentContext[]) => {
        contexts: {
            body: string
            id: string
            label: string
            screenshot?: string | undefined
        }[]
    }
}

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

export type taskCommentContextsLogicType = MakeLogicType<
    taskCommentContextsLogicValues,
    taskCommentContextsLogicActions,
    TaskCommentContextsLogicProps,
    taskCommentContextsLogicMeta
>

export const taskCommentContextsLogic = kea<taskCommentContextsLogicType>([
    props({} as TaskCommentContextsLogicProps),
    key((props) => props.taskId),
    path((key) => ['products', 'posthog_ai', 'frontend', 'logics', 'taskCommentContextsLogic', key]),
    actions({
        addCommentToChat: (context: CommentContext | null, comment: string, screenshotFile?: File) => ({
            context: context
                ? {
                      ...context,
                      id: uuid(),
                      ...(screenshotFile ? { screenshot: screenshotFile.name, screenshotFile } : {}),
                  }
                : null,
            comment,
        }),
        removeCommentContext: (id: string) => ({ id }),
        restoreCommentContexts: (contexts: CommentContext[]) => ({
            contexts: contexts.map((context) => ({ ...context, id: uuid() })),
        }),
        clearCommentContexts: true,
        consumePendingCommentText: true,
    }),
    reducers({
        commentContexts: [
            [] as PendingCommentContext[],
            {
                addCommentToChat: (state, { context }) => (context ? [...state, context] : state),
                restoreCommentContexts: (state, { contexts }) => [
                    ...contexts.filter(
                        (restored) =>
                            !state.some((held) => held.label === restored.label && held.body === restored.body)
                    ),
                    ...state,
                ],
                removeCommentContext: (state, { id }) => state.filter((context) => context.id !== id),
                clearCommentContexts: () => [],
            },
        ],
        pendingCommentText: [
            [] as string[],
            {
                addCommentToChat: (state, { comment }) => [...state, comment],
                consumePendingCommentText: () => [],
            },
        ],
    }),
])