: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.
Python ab-testingai-analyticsanalyticscdpdata-warehouseexperimentsfeature-flagsjavascriptproduct-analyticspythonreactsession-replaysurveystypescriptweb-analytics
import { MakeLogicType, actions, afterMount, kea, listeners, path, reducers, selectors } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'
import api from 'lib/api'
import { ApiError, isAccessDeniedError } from 'lib/api-error'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { CoreMemory } from '~/types'
export const CORE_MEMORY_MAX_CHARACTERS = 10000
export type CoreMemoryForm = {
text: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface maxSettingsLogicValues {
coreMemory: CoreMemory | null
coreMemoryForm: CoreMemoryForm
coreMemoryFormAllErrors: Record<string, any>
coreMemoryFormChanged: boolean
coreMemoryFormErrors: DeepPartialMap<CoreMemoryForm, ValidationErrorType>
coreMemoryFormHasErrors: boolean
coreMemoryFormManualErrors: Record<string, any>
coreMemoryFormTouched: boolean
coreMemoryFormTouches: Record<string, boolean>
coreMemoryFormValidationErrors: DeepPartialMap<CoreMemoryForm, ValidationErrorType>
coreMemoryLoadError: string | null
coreMemoryLoading: boolean
coreMemoryOverLimit: boolean
isCoreMemoryFormSubmitting: boolean
isCoreMemoryFormValid: boolean
isLoading: boolean
isUpdating: boolean
showCoreMemoryFormErrors: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface maxSettingsLogicActions {
loadCoreMemory: () => any
loadCoreMemoryFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadCoreMemorySuccess: (
coreMemory: CoreMemory | null,
payload?: any
) => {
coreMemory: CoreMemory | null
payload?: any
}
resetCoreMemoryForm: (values?: CoreMemoryForm) => {
values?: CoreMemoryForm
}
setCoreMemoryFormManualErrors: (errors: Record<string, any>) => {
errors: Record<string, any>
}
setCoreMemoryFormValue: (
key: FieldName,
value: any
) => {
name: FieldName
value: any
}
setCoreMemoryFormValues: (values: DeepPartial<CoreMemoryForm>) => {
values: DeepPartial<CoreMemoryForm>
}
setIsLoading: (isLoading: boolean) => {
isLoading: boolean
}
submitCoreMemoryForm: () => {
value: boolean
}
submitCoreMemoryFormFailure: (
error: Error,
errors: Record<string, any>
) => {
error: Error
errors: Record<string, any>
}
submitCoreMemoryFormRequest: (coreMemoryForm: CoreMemoryForm) => {
coreMemoryForm: CoreMemoryForm
}
submitCoreMemoryFormSuccess: (coreMemoryForm: CoreMemoryForm) => {
coreMemoryForm: CoreMemoryForm
}
touchCoreMemoryFormField: (key: string) => {
key: string
}
trimCoreMemoryToFit: () => {
value: true
}
updateCoreMemory: (data: CoreMemoryForm) => CoreMemoryForm
updateCoreMemoryFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
updateCoreMemorySuccess: (
coreMemory: CoreMemory,
payload?: CoreMemoryForm
) => {
coreMemory: CoreMemory
payload?: CoreMemoryForm
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface maxSettingsLogicMeta {
__keaTypeGenInternalSelectorTypes: {
coreMemoryOverLimit: (coreMemoryForm: CoreMemoryForm) => boolean
}
}
export type maxSettingsLogicType = MakeLogicType<
maxSettingsLogicValues,
maxSettingsLogicActions,
Record<string, any>,
maxSettingsLogicMeta
>
export const maxSettingsLogic = kea<maxSettingsLogicType>([
path(['scenes', 'project', 'Settings', 'maxSettingsLogic']),
actions({
setIsLoading: (isLoading: boolean) => ({ isLoading }),
trimCoreMemoryToFit: true,
}),
reducers({
isLoading: [
false,
{
loadCoreMemory: () => true,
loadCoreMemorySuccess: () => false,
loadCoreMemoryFailure: () => false,
},
],
coreMemoryLoadError: [
null as string | null,
{
loadCoreMemory: () => null,
loadCoreMemorySuccess: () => null,
loadCoreMemoryFailure: (_, { error }) => error || 'Could not load memory.',
},
],
isUpdating: [
false,
{
createCoreMemory: () => true,
createCoreMemorySuccess: () => false,
createCoreMemoryFailure: () => false,
updateCoreMemory: () => true,
updateCoreMemorySuccess: () => false,
updateCoreMemoryFailure: () => false,
},
],
}),
loaders(({ values }) => ({
coreMemory: {
__default: null as CoreMemory | null,
loadCoreMemory: async (): Promise<CoreMemory | null> => {
// Let HTTP errors (403, timeout) reach loadCoreMemoryFailure so the UI can show a real
// error with a retry, rather than an empty textarea that reads as "my memory is gone".
// An empty result list is a genuine "no memory yet" state and still resolves to null.
const response = await api.coreMemory.list()
return response.results[0] || null
},
updateCoreMemory: async (data: CoreMemoryForm) => {
if (!values.coreMemory) {
const response = await api.coreMemory.create(data)
lemonToast.success('PostHog AI memory has been created.')
posthog.capture('core memory saved', { action: 'create', length: data.text.length })
return response
}
const response = await api.coreMemory.update(values.coreMemory.id, data)
lemonToast.success('PostHog AI memory has been updated.')
posthog.capture('core memory saved', { action: 'update', length: data.text.length })
return response
},
},
})),
forms(({ actions }) => ({
coreMemoryForm: {
defaults: { text: '' } as CoreMemoryForm,
submit: ({ text }) => {
actions.updateCoreMemory({ text })
},
},
})),
selectors({
coreMemoryOverLimit: [
(s) => [s.coreMemoryForm],
(coreMemoryForm: CoreMemoryForm) => (coreMemoryForm?.text?.length ?? 0) > CORE_MEMORY_MAX_CHARACTERS,
],
}),
listeners(({ actions, values }) => ({
loadCoreMemorySuccess: ({ coreMemory }) => {
if (coreMemory) {
actions.setCoreMemoryFormValue('text', coreMemory.text)
}
},
loadCoreMemoryFailure: ({ errorObject }) => {
posthog.capture('core memory load failed', {
status: errorObject instanceof ApiError ? errorObject.status : undefined,
access_denied: errorObject instanceof ApiError ? isAccessDeniedError(errorObject) : undefined,
})
},
trimCoreMemoryToFit: () => {
const trimmed = (values.coreMemoryForm?.text ?? '').slice(0, CORE_MEMORY_MAX_CHARACTERS)
actions.setCoreMemoryFormValue('text', trimmed)
posthog.capture('core memory trimmed to fit', { length: trimmed.length })
},
updateCoreMemoryFailure: ({ error, errorObject }) => {
const detail = errorObject instanceof ApiError ? errorObject.detail : null
lemonToast.error(detail || error || 'Could not save memory.')
posthog.capture('core memory save failed', {
status: errorObject instanceof ApiError ? errorObject.status : undefined,
over_limit: values.coreMemoryOverLimit,
})
},
})),
afterMount(({ actions }) => {
actions.loadCoreMemory()
}),
])