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.

4.1 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, path, props, reducers, selectors } from 'kea'

import { llmPlaygroundModelLogic } from './llmPlaygroundModelLogic'
import { llmPlaygroundPromptsLogic, type PromptConfig } from './llmPlaygroundPromptsLogic'
import { extractVariablesFromTexts, getVariableValue, promptTemplateTexts } from './playgroundTemplating'

export type LLMPlaygroundVariablesLogicProps = Record<string, never>

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface llmPlaygroundVariablesLogicValues {
    systemOnePromptIds: string[] // llmPlaygroundModelLogic
    promptConfigs: PromptConfig[] // llmPlaygroundPromptsLogic
    detectedVariables: string[]
    unfilledVariables: string[]
    variableValues: Record<string, string>
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface llmPlaygroundVariablesLogicActions {
    resetPlayground: () => {
        value: true
    } // llmPlaygroundPromptsLogic
    setupPlaygroundFromEvent: (payload: import('./llmPlaygroundPromptsLogic').PlaygroundSetupPayload) => {
        payload: import('./llmPlaygroundPromptsLogic').PlaygroundSetupPayload
    } // llmPlaygroundPromptsLogic
    setVariableValue: (
        name: string,
        value: string
    ) => {
        name: string
        value: string
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface llmPlaygroundVariablesLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        detectedVariables: (promptConfigs: PromptConfig[], systemOnePromptIds: string[]) => string[]
        unfilledVariables: (detectedVariables: string[], variableValues: Record<string, string>) => string[]
    }
}

export type llmPlaygroundVariablesLogicType = MakeLogicType<
    llmPlaygroundVariablesLogicValues,
    llmPlaygroundVariablesLogicActions,
    LLMPlaygroundVariablesLogicProps,
    llmPlaygroundVariablesLogicMeta
>

export const llmPlaygroundVariablesLogic = kea<llmPlaygroundVariablesLogicType>([
    path(['products', 'ai_observability', 'frontend', 'playground', 'llmPlaygroundVariablesLogic']),
    props({} as LLMPlaygroundVariablesLogicProps),

    connect(() => ({
        values: [llmPlaygroundPromptsLogic, ['promptConfigs'], llmPlaygroundModelLogic, ['systemOnePromptIds']],
        actions: [llmPlaygroundPromptsLogic, ['resetPlayground', 'setupPlaygroundFromEvent']],
    })),

    actions({
        setVariableValue: (name: string, value: string) => ({ name, value }),
    }),

    reducers({
        // Values are keyed by name and shared across comparison panels. Entries for
        // variables no longer present in any prompt are kept, so deleting and retyping
        // a placeholder does not lose its value. A future dataset row can hydrate this
        // record wholesale.
        variableValues: [
            {} as Record<string, string>,
            {
                resetPlayground: () => ({}),
                // A new source (saved prompt, evaluation, trace) starts a fresh test
                // context; values from the previous prompt must not silently fill
                // same-named variables in the new one.
                setupPlaygroundFromEvent: () => ({}),
                setVariableValue: (
                    state: Record<string, string>,
                    { name, value }: { name: string; value: string }
                ) => ({ ...state, [name]: value }),
            },
        ],
    }),

    selectors({
        detectedVariables: [
            (s) => [s.promptConfigs, s.systemOnePromptIds],
            (promptConfigs: PromptConfig[], systemOnePromptIds: string[]): string[] =>
                extractVariablesFromTexts(
                    promptConfigs.flatMap((prompt) =>
                        promptTemplateTexts(prompt, systemOnePromptIds.includes(prompt.id))
                    )
                ),
        ],
        unfilledVariables: [
            (s) => [s.detectedVariables, s.variableValues],
            (detectedVariables: string[], variableValues: Record<string, string>): string[] =>
                detectedVariables.filter((name) => !getVariableValue(variableValues, name)),
        ],
    }),
])