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.

7.3 KB
Raw Blame
import { MakeLogicType, actions, afterMount, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'

import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { projectLogic } from 'scenes/projectLogic'

import { signalsConfigCreate, signalsConfigList } from '../../generated/api'
import type { SignalTeamConfigApi } from '../../generated/api.schemas'

export const STEERING_FIELDS = {
    scout: 'scout_instructions',
    research: 'research_instructions',
    implementation: 'implementation_instructions',
} as const

export type SteeringRole = keyof typeof STEERING_FIELDS

export interface SaveSteeringInstructions {
    role: SteeringRole
    instructions: string
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface agentSteeringLogicValues {
    currentProjectId: number | null // projectLogic
    dirtyRoles: SteeringRole[]
    drafts: Partial<Record<SteeringRole, string>>
    instructions: string
    instructionsDirty: boolean
    selectedRole: SteeringRole
    steeringConfig: SignalTeamConfigApi | null
    steeringConfigLoading: boolean
    storedInstructions: string | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface agentSteeringLogicActions {
    discardDraft: (role: SteeringRole) => {
        role: 'implementation' | 'research' | 'scout'
    }
    loadSteeringConfig: () => any
    loadSteeringConfigFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadSteeringConfigSuccess: (
        steeringConfig: SignalTeamConfigApi | null,
        payload?: any
    ) => {
        steeringConfig: SignalTeamConfigApi | null
        payload?: any
    }
    saveSteeringInstructions: ({ role, instructions }: SaveSteeringInstructions) => SaveSteeringInstructions
    saveSteeringInstructionsFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    saveSteeringInstructionsSuccess: (
        steeringConfig: SignalTeamConfigApi,
        payload?: SaveSteeringInstructions
    ) => {
        steeringConfig: SignalTeamConfigApi
        payload?: SaveSteeringInstructions
    }
    selectRole: (role: SteeringRole) => {
        role: 'implementation' | 'research' | 'scout'
    }
    setDraft: (
        role: SteeringRole,
        instructions: string
    ) => {
        instructions: string
        role: 'implementation' | 'research' | 'scout'
    }
    submitInstructions: () => {
        value: true
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface agentSteeringLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        dirtyRoles: (
            drafts: Partial<Record<'implementation' | 'research' | 'scout', string>>,
            steeringConfig: SignalTeamConfigApi | null
        ) => SteeringRole[]
        storedInstructions: (
            steeringConfig: SignalTeamConfigApi | null,
            selectedRole: 'implementation' | 'research' | 'scout'
        ) => string | null
        instructions: (
            drafts: Partial<Record<'implementation' | 'research' | 'scout', string>>,
            selectedRole: 'implementation' | 'research' | 'scout',
            storedInstructions: string | null
        ) => string
        instructionsDirty: (instructions: string, storedInstructions: string | null) => boolean
    }
}

export type agentSteeringLogicType = MakeLogicType<
    agentSteeringLogicValues,
    agentSteeringLogicActions,
    Record<string, any>,
    agentSteeringLogicMeta
>

export const agentSteeringLogic = kea<agentSteeringLogicType>([
    path(['products', 'signals', 'inbox', 'logics', 'agentSteeringLogic']),
    connect(() => ({ values: [projectLogic, ['currentProjectId']] })),
    actions({
        selectRole: (role: SteeringRole) => ({ role }),
        setDraft: (role: SteeringRole, instructions: string) => ({ role, instructions }),
        discardDraft: (role: SteeringRole) => ({ role }),
        submitInstructions: true,
    }),
    loaders(({ values }) => ({
        steeringConfig: {
            __default: null as SignalTeamConfigApi | null,
            loadSteeringConfig: async (): Promise<SignalTeamConfigApi | null> =>
                values.currentProjectId == null ? null : signalsConfigList(String(values.currentProjectId)),
            saveSteeringInstructions: async ({
                role,
                instructions,
            }: SaveSteeringInstructions): Promise<SignalTeamConfigApi> =>
                signalsConfigCreate(String(values.currentProjectId), { [STEERING_FIELDS[role]]: instructions }),
        },
    })),
    reducers({
        selectedRole: ['research' as SteeringRole, { selectRole: (_, { role }) => role }],
        drafts: [
            {} as Partial<Record<SteeringRole, string>>,
            {
                setDraft: (state, { role, instructions }) => ({ ...state, [role]: instructions }),
                discardDraft: (state, { role }) => {
                    const next = { ...state }
                    delete next[role]
                    return next
                },
                saveSteeringInstructionsSuccess: (state, { payload }) => {
                    if (!payload || state[payload.role] !== payload.instructions) {
                        return state
                    }
                    const next = { ...state }
                    delete next[payload.role]
                    return next
                },
            },
        ],
    }),
    selectors({
        dirtyRoles: [
            (s) => [s.drafts, s.steeringConfig],
            (drafts: Partial<Record<SteeringRole, string>>, config: SignalTeamConfigApi | null): SteeringRole[] =>
                config === null
                    ? []
                    : (Object.keys(STEERING_FIELDS) as SteeringRole[]).filter(
                          (role) => drafts[role] !== undefined && drafts[role] !== (config[STEERING_FIELDS[role]] ?? '')
                      ),
        ],
        storedInstructions: [
            (s) => [s.steeringConfig, s.selectedRole],
            (config: SignalTeamConfigApi | null, role: SteeringRole): string | null =>
                config === null ? null : (config[STEERING_FIELDS[role]] ?? ''),
        ],
        instructions: [
            (s) => [s.drafts, s.selectedRole, s.storedInstructions],
            (drafts: Partial<Record<SteeringRole, string>>, role: SteeringRole, stored: string | null): string =>
                drafts[role] ?? stored ?? '',
        ],
        instructionsDirty: [
            (s) => [s.instructions, s.storedInstructions],
            (instructions: string, stored: string | null): boolean => stored !== null && instructions !== stored,
        ],
    }),
    listeners(({ actions, values }) => ({
        submitInstructions: () => {
            if (values.steeringConfigLoading || !values.instructionsDirty || values.currentProjectId == null) {
                return
            }
            actions.saveSteeringInstructions({ role: values.selectedRole, instructions: values.instructions })
        },
        saveSteeringInstructionsSuccess: () => {
            lemonToast.success('Steering instructions saved')
        },
    })),
    afterMount(({ actions }) => actions.loadSteeringConfig()),
])