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.

5.7 KB
Raw Blame
import { MakeLogicType, afterMount, connect, kea, path, selectors } from 'kea'
import { loaders } from 'kea-loaders'

import { projectLogic } from 'scenes/projectLogic'

import { tasksMeConfigList } from 'products/tasks/frontend/generated/api'
import { TaskRuntimeEnumApi } from 'products/tasks/frontend/generated/api.schemas'
import type {
    TasksResolvedAIRunDefaultsApi,
    TasksUserConfigResponseApi,
} from 'products/tasks/frontend/generated/api.schemas'

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface taskRunDefaultsLogicValues {
    currentProjectId: number | null // projectLogic
    acpDefaults: TasksResolvedAIRunDefaultsApi | null
    defaultEffort: string | null
    defaultModel: string | null
    defaultRuntimeAdapter: string | null
    defaultsResolved: boolean
    myConfig: TasksUserConfigResponseApi | null
    myConfigLoading: boolean
    resolvedDefaults: TasksResolvedAIRunDefaultsApi | null
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface taskRunDefaultsLogicActions {
    loadMyConfig: () => any
    loadMyConfigFailure: (
        error: string,
        errorObject?: any
    ) => {
        error: string
        errorObject?: any
    }
    loadMyConfigSuccess: (
        myConfig: TasksUserConfigResponseApi | null,
        payload?: any
    ) => {
        myConfig: TasksUserConfigResponseApi | null
        payload?: any
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface taskRunDefaultsLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        resolvedDefaults: (myConfig: TasksUserConfigResponseApi | null) => TasksResolvedAIRunDefaultsApi | null
        acpDefaults: (resolvedDefaults: TasksResolvedAIRunDefaultsApi | null) => TasksResolvedAIRunDefaultsApi | null
        defaultModel: (acpDefaults: TasksResolvedAIRunDefaultsApi | null) => string | null
        defaultEffort: (acpDefaults: TasksResolvedAIRunDefaultsApi | null) => string | null
        defaultRuntimeAdapter: (acpDefaults: TasksResolvedAIRunDefaultsApi | null) => string | null
        defaultsResolved: (myConfig: TasksUserConfigResponseApi | null) => boolean
    }
}

export type taskRunDefaultsLogicType = MakeLogicType<
    taskRunDefaultsLogicValues,
    taskRunDefaultsLogicActions,
    Record<string, any>,
    taskRunDefaultsLogicMeta
>

/**
 * The server-side default AI run configuration for the current user in the current project —
 * the user's per-project preference over the project default, as resolved by the tasks backend.
 * Composers read it to show what a run launches with when nothing is picked; a run created
 * without an explicit selection gets the same defaults applied server-side.
 */
export const taskRunDefaultsLogic = kea<taskRunDefaultsLogicType>([
    path(['products', 'posthog_ai', 'frontend', 'logics', 'taskRunDefaultsLogic']),

    connect(() => ({
        values: [projectLogic, ['currentProjectId']],
    })),

    loaders(({ values }) => ({
        myConfig: {
            __default: null as TasksUserConfigResponseApi | null,
            loadMyConfig: async (): Promise<TasksUserConfigResponseApi | null> => {
                if (values.currentProjectId == null) {
                    return null
                }
                try {
                    return await tasksMeConfigList(String(values.currentProjectId))
                } catch {
                    // Defaults are a nicety — a failed fetch (missing scope, transient error) must never
                    // break the composer, which falls back to its built-in defaults.
                    return null
                }
            },
        },
    })),

    selectors({
        resolvedDefaults: [
            (s) => [s.myConfig],
            (myConfig: TasksUserConfigResponseApi | null): TasksResolvedAIRunDefaultsApi | null =>
                myConfig?.resolved_ai_run_defaults ?? null,
        ],
        acpDefaults: [
            (s) => [s.resolvedDefaults],
            (defaults: TasksResolvedAIRunDefaultsApi | null): TasksResolvedAIRunDefaultsApi | null =>
                defaults?.runtime === TaskRuntimeEnumApi.Pi ? null : (defaults ?? null),
        ],
        // Whatever runtime the default names: the composer's pickers are runtime-aware (they scope the
        // permission modes to the selected model's harness), so a Codex default renders as readily as a
        // Claude one. Filtering by adapter here would drop the preference to the built-in model rather
        // than to the next level that configured one, which reads as the setting being ignored.
        defaultModel: [
            (s) => [s.acpDefaults],
            (defaults: TasksResolvedAIRunDefaultsApi | null): string | null => defaults?.model ?? null,
        ],
        defaultEffort: [
            (s) => [s.acpDefaults],
            (defaults: TasksResolvedAIRunDefaultsApi | null): string | null => defaults?.reasoning_effort ?? null,
        ],
        // The server already resolved which runtime the default belongs to; carrying it beats
        // re-deriving from the client catalogue, which can be stale and mislabel the harness.
        defaultRuntimeAdapter: [
            (s) => [s.acpDefaults],
            (defaults: TasksResolvedAIRunDefaultsApi | null): string | null => defaults?.runtime_adapter ?? null,
        ],
        // The defaults are null both before the config lands and when nothing is stored. A caller that
        // would override a stored default by acting on its absence has to tell the two apart.
        defaultsResolved: [
            (s) => [s.myConfig],
            (myConfig: TasksUserConfigResponseApi | null): boolean => myConfig !== null,
        ],
    }),

    afterMount(({ actions }) => {
        actions.loadMyConfig()
    }),
])