: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 {
LogicWrapper,
MakeLogicType,
actions,
afterMount,
beforeUnmount,
connect,
kea,
key,
listeners,
path,
props,
reducers,
selectors,
} from 'kea'
import { projectLogic } from 'scenes/projectLogic'
import type { TurnSuggestion } from '../types/streamTypes'
import { recordTurnSuggestionResolution } from '../utils/recordTurnSuggestionResolution'
import { captureTurnSuggestionEvent } from '../utils/turnSuggestionEvents'
import { runStreamLogic } from './runStreamLogic'
/** Time between the offer arriving and the card appearing, so the reader finishes the answer first. */
const REVEAL_DELAY_MS = 2500
export interface TurnSuggestionLogicProps {
streamKey: string
turnIndex: number
sessionId: string
revealDelayMs?: number
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface turnSuggestionLogicValues {
currentProjectId: number | null // projectLogic
suggestion: TurnSuggestion | null // runStreamLogic
turnSuggestionAcceptedHere: number | null // runStreamLogic
turnSuggestionExpandedHere: number | null // runStreamLogic
completed: boolean
dismissed: boolean
expanded: boolean
revealReady: boolean
shownSuggestion: TurnSuggestion | null
visible: boolean
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface turnSuggestionLogicActions {
markTurnSuggestionExpanded: (turnIndex: number) => {
turnIndex: number
} // runStreamLogic
recordTurnSuggestionOutcome: (
turnIndex: number,
outcome: string
) => {
outcome: string
turnIndex: number
} // runStreamLogic
dismiss: () => {
value: true
}
expand: () => {
value: true
}
reportShown: (suggestion: TurnSuggestion) => {
suggestion: TurnSuggestion
}
reveal: () => {
value: true
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface turnSuggestionLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
completed: (turnSuggestionAcceptedHere: number | null, shownSuggestion: TurnSuggestion | null) => boolean
expanded: (turnSuggestionExpandedHere: number | null, suggestion: TurnSuggestion | null) => boolean
visible: (suggestion: TurnSuggestion | null, revealReady: boolean, dismissed: boolean) => boolean
}
}
export type turnSuggestionLogicType = MakeLogicType<
turnSuggestionLogicValues,
turnSuggestionLogicActions,
TurnSuggestionLogicProps,
turnSuggestionLogicMeta
>
export const turnSuggestionLogic: LogicWrapper<turnSuggestionLogicType> = kea<turnSuggestionLogicType>([
props({} as TurnSuggestionLogicProps),
key((props) => `${props.streamKey}:${props.turnIndex}`),
path((key) => ['products', 'posthog_ai', 'frontend', 'logics', 'turnSuggestionLogic', key]),
connect((props: TurnSuggestionLogicProps) => ({
values: [
runStreamLogic({ streamKey: props.streamKey }),
['turnSuggestion as suggestion', 'turnSuggestionAcceptedHere', 'turnSuggestionExpandedHere'],
projectLogic,
['currentProjectId'],
],
actions: [
runStreamLogic({ streamKey: props.streamKey }),
['recordTurnSuggestionOutcome', 'markTurnSuggestionExpanded'],
],
})),
actions({
dismiss: true,
expand: true,
reveal: true,
reportShown: (suggestion: TurnSuggestion) => ({ suggestion }),
}),
reducers({
dismissed: [false, { dismiss: () => true }],
revealReady: [false, { reveal: () => true }],
shownSuggestion: [
null as TurnSuggestion | null,
{
reportShown: (_, { suggestion }) => suggestion,
},
],
}),
selectors({
completed: [
(s) => [s.turnSuggestionAcceptedHere, s.shownSuggestion],
(turnSuggestionAcceptedHere: number | null, shownSuggestion: TurnSuggestion | null): boolean =>
shownSuggestion !== null && turnSuggestionAcceptedHere === shownSuggestion.turnIndex,
],
expanded: [
(s) => [s.turnSuggestionExpandedHere, s.suggestion],
(turnSuggestionExpandedHere: number | null, suggestion: TurnSuggestion | null): boolean =>
suggestion !== null && turnSuggestionExpandedHere === suggestion.turnIndex,
],
visible: [
(s) => [s.suggestion, s.revealReady, s.dismissed],
(suggestion: TurnSuggestion | null, revealReady: boolean, dismissed: boolean): boolean =>
!!suggestion && revealReady && !dismissed,
],
}),
listeners(({ actions, values, props }) => ({
reportShown: ({ suggestion }) => {
captureTurnSuggestionEvent('shown', props, suggestion)
},
expand: () => {
const { suggestion } = values
if (suggestion && !values.expanded) {
actions.markTurnSuggestionExpanded(suggestion.turnIndex)
captureTurnSuggestionEvent('expanded', props, suggestion)
}
},
dismiss: () => {
const { suggestion } = values
if (suggestion) {
actions.recordTurnSuggestionOutcome(suggestion.turnIndex, 'dismissed')
captureTurnSuggestionEvent('dismissed', props, suggestion)
recordTurnSuggestionResolution(values.currentProjectId, props.sessionId, suggestion, 'dismissed')
}
},
})),
afterMount(({ actions, props, cache }) => {
const delay = props.revealDelayMs ?? REVEAL_DELAY_MS
if (delay === 0) {
actions.reveal()
return
}
cache.disposables.add(() => {
const timer = window.setTimeout(() => actions.reveal(), delay)
return () => window.clearTimeout(timer)
}, 'reveal')
}),
beforeUnmount(({ values, props }) => {
// The card unmounts with its suggestion gone when a new message closed it.
if (values.shownSuggestion && !values.suggestion && !values.dismissed && !values.completed) {
captureTurnSuggestionEvent('superseded', props, values.shownSuggestion)
}
}),
])