: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, connect, kea, listeners, path, reducers, selectors } from 'kea'
import { loaders } from 'kea-loaders'
import posthog from 'posthog-js'
import { lemonToast } from 'lib/lemon-ui/LemonToast/LemonToast'
import { projectLogic } from 'scenes/projectLogic'
import {
tasksConfigAgentInstructionsCreate,
tasksConfigList,
tasksMeConfigAgentInstructionsCreate,
tasksMeConfigList,
} from 'products/tasks/frontend/generated/api'
export type AgentInstructionsScope = 'project' | 'personal'
function trackSaved(scope: AgentInstructionsScope, instructions: string): void {
// pinned: analytics event name and properties, renaming breaks dashboards
posthog.capture('agent instructions saved', {
scope,
length: instructions.length,
cleared: instructions.trim().length === 0,
})
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface agentInstructionsSettingsLogicValues {
currentProjectId: number | null // projectLogic
personalDirty: boolean
personalDraft: string | null
personalInstructions: string | null
personalInstructionsLoading: boolean
personalValue: string
projectDirty: boolean
projectDraft: string | null
projectInstructions: string | null
projectInstructionsLoading: boolean
projectValue: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface agentInstructionsSettingsLogicActions {
loadPersonalInstructions: () => any
loadPersonalInstructionsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadPersonalInstructionsSuccess: (
personalInstructions: string | null,
payload?: any
) => {
personalInstructions: string | null
payload?: any
}
loadProjectInstructions: () => any
loadProjectInstructionsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
loadProjectInstructionsSuccess: (
projectInstructions: string | null,
payload?: any
) => {
projectInstructions: string | null
payload?: any
}
savePersonalInstructions: (instructions: string) => string
savePersonalInstructionsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
savePersonalInstructionsSuccess: (
personalInstructions: string | null,
payload?: string
) => {
personalInstructions: string | null
payload?: string
}
saveProjectInstructions: (instructions: string) => string
saveProjectInstructionsFailure: (
error: string,
errorObject?: any
) => {
error: string
errorObject?: any
}
saveProjectInstructionsSuccess: (
projectInstructions: string | null,
payload?: string
) => {
projectInstructions: string | null
payload?: string
}
setPersonalDraft: (draft: string) => {
draft: string
}
setProjectDraft: (draft: string) => {
draft: string
}
submitPersonalDraft: () => {
value: true
}
submitProjectDraft: () => {
value: true
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface agentInstructionsSettingsLogicMeta {
__keaTypeGenInternalSelectorTypes: {
projectValue: (projectDraft: string | null, projectInstructions: string | null) => string
personalValue: (personalDraft: string | null, personalInstructions: string | null) => string
projectDirty: (projectDraft: string | null, projectInstructions: string | null) => boolean
personalDirty: (personalDraft: string | null, personalInstructions: string | null) => boolean
}
}
export type agentInstructionsSettingsLogicType = MakeLogicType<
agentInstructionsSettingsLogicValues,
agentInstructionsSettingsLogicActions,
Record<string, any>,
agentInstructionsSettingsLogicMeta
>
export const agentInstructionsSettingsLogic = kea<agentInstructionsSettingsLogicType>([
path(['scenes', 'settings', 'environment', 'agentInstructionsSettingsLogic']),
connect(() => ({
values: [projectLogic, ['currentProjectId']],
})),
actions({
setProjectDraft: (draft: string) => ({ draft }),
setPersonalDraft: (draft: string) => ({ draft }),
submitProjectDraft: true,
submitPersonalDraft: true,
}),
loaders(({ values }) => ({
projectInstructions: {
__default: null as string | null,
loadProjectInstructions: async (): Promise<string | null> => {
if (values.currentProjectId == null) {
return null
}
return (await tasksConfigList(String(values.currentProjectId))).agent_instructions
},
saveProjectInstructions: async (instructions: string): Promise<string | null> => {
const response = await tasksConfigAgentInstructionsCreate(String(values.currentProjectId), {
agent_instructions: instructions,
})
lemonToast.success(instructions.trim() ? 'Project instructions saved' : 'Project instructions cleared')
return response.agent_instructions
},
},
personalInstructions: {
__default: null as string | null,
loadPersonalInstructions: async (): Promise<string | null> => {
if (values.currentProjectId == null) {
return null
}
return (await tasksMeConfigList(String(values.currentProjectId))).agent_instructions
},
savePersonalInstructions: async (instructions: string): Promise<string | null> => {
const response = await tasksMeConfigAgentInstructionsCreate(String(values.currentProjectId), {
agent_instructions: instructions,
})
lemonToast.success(instructions.trim() ? 'Your instructions saved' : 'Your instructions cleared')
return response.agent_instructions
},
},
})),
// A draft is null while untouched, so a load landing mid-edit never replaces what someone is typing.
reducers({
projectDraft: [
null as string | null,
{
setProjectDraft: (_, { draft }) => draft,
saveProjectInstructionsSuccess: () => null,
},
],
personalDraft: [
null as string | null,
{
setPersonalDraft: (_, { draft }) => draft,
savePersonalInstructionsSuccess: () => null,
},
],
}),
selectors({
projectValue: [
(s) => [s.projectDraft, s.projectInstructions],
(draft: string | null, stored: string | null): string => draft ?? stored ?? '',
],
personalValue: [
(s) => [s.personalDraft, s.personalInstructions],
(draft: string | null, stored: string | null): string => draft ?? stored ?? '',
],
projectDirty: [
(s) => [s.projectDraft, s.projectInstructions],
(draft: string | null, stored: string | null): boolean => draft !== null && draft !== (stored ?? ''),
],
personalDirty: [
(s) => [s.personalDraft, s.personalInstructions],
(draft: string | null, stored: string | null): boolean => draft !== null && draft !== (stored ?? ''),
],
}),
listeners(({ actions, values }) => ({
submitProjectDraft: () => {
actions.saveProjectInstructions(values.projectValue)
},
submitPersonalDraft: () => {
actions.savePersonalInstructions(values.personalValue)
},
saveProjectInstructionsSuccess: ({ projectInstructions }) => {
trackSaved('project', projectInstructions ?? '')
},
savePersonalInstructionsSuccess: ({ personalInstructions }) => {
trackSaved('personal', personalInstructions ?? '')
},
})),
afterMount(({ actions }) => {
actions.loadProjectInstructions()
actions.loadPersonalInstructions()
}),
])