: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, LogicWrapper, actions, connect, kea, key, listeners, path, props, selectors } from 'kea'
import { type AttachedContextItem, NEW_CHAT_KEY, attachedContextItemKey } from '~/layout/ai/attachedContext'
import { attachedContextLogic } from '~/layout/ai/attachedContextLogic'
import { type ChatChipsByTask, type ContextChip, contextChip } from '~/layout/ai/chatContextChips'
export interface ContextAttachment {
chip: ContextChip
previewHtml: string
}
function uniqueBy<T>(items: T[], keyOf: (item: T) => string): T[] {
const seen = new Set<string>()
return items.filter((item) => {
const itemKey = keyOf(item)
const isNew = !seen.has(itemKey)
seen.add(itemKey)
return isNew
})
}
/**
* What a send carries before dedupe. A surface that pins its context replaces the page's items, and still sends picks.
*/
export function chatSendItems(
pageItems: AttachedContextItem[],
picks: AttachedContextItem[],
pinnedItems?: AttachedContextItem[]
): AttachedContextItem[] {
return uniqueBy([...(pinnedItems ?? pageItems), ...picks], attachedContextItemKey)
}
export interface ChatContextLogicProps {
taskId?: string
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface chatContextLogicValues {
chatChipsByTask: ChatChipsByTask // attachedContextLogic
contextItems: AttachedContextItem[] // attachedContextLogic
pickedByChat: Record<string, AttachedContextItem[]> // attachedContextLogic
removedGroupsByTask: Record<string, string[]> // attachedContextLogic
attachments: ContextAttachment[]
chips: ContextChip[]
pageItems: AttachedContextItem[]
picks: AttachedContextItem[]
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface chatContextLogicActions {
dismissContext: (
key: string,
dismissGroup?: string | undefined
) => {
dismissGroup: string | undefined
key: string
} // attachedContextLogic
removeChatChip: (
taskId: string,
chipId: string,
dismissGroup?: string | undefined
) => {
chipId: string
dismissGroup: string | undefined
taskId: string
} // attachedContextLogic
removePickedItem: (
key: string,
taskId?: string | undefined
) => {
key: string
taskId: string
} // attachedContextLogic
removeChip: (chip: ContextChip) => {
chip: ContextChip
}
}
// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface chatContextLogicMeta {
key: string
__keaTypeGenInternalSelectorTypes: {
picks: (pickedByChat: Record<string, AttachedContextItem[]>, arg: string) => AttachedContextItem[]
pageItems: (
contextItems: AttachedContextItem[],
removedGroupsByTask: Record<string, string[]>,
arg: string | undefined
) => AttachedContextItem[]
chips: (
chatChipsByTask: ChatChipsByTask,
pageItems: AttachedContextItem[],
picks: AttachedContextItem[],
arg: string
) => ContextChip[]
attachments: (pageItems: AttachedContextItem[]) => ContextAttachment[]
}
}
export type chatContextLogicType = MakeLogicType<
chatContextLogicValues,
chatContextLogicActions,
ChatContextLogicProps,
chatContextLogicMeta
>
const chatKey = (_: unknown, props: ChatContextLogicProps): string => props.taskId || NEW_CHAT_KEY
/**
* The one definition of what is in a chat: its saved chips, the items it takes from the current page, and its picks.
* A new chat has no saved chips and takes everything on the page. A started chat keeps the chips it saved and takes
* only the hidden items, because a visible item reaches it only through a pick.
*/
export const chatContextLogic: LogicWrapper<chatContextLogicType> = kea<chatContextLogicType>([
props({} as ChatContextLogicProps),
key(({ taskId }) => taskId || NEW_CHAT_KEY),
path((logicKey) => ['products', 'posthog_ai', 'frontend', 'logics', 'chatContextLogic', logicKey]),
connect(() => ({
values: [attachedContextLogic, ['contextItems', 'chatChipsByTask', 'pickedByChat', 'removedGroupsByTask']],
actions: [attachedContextLogic, ['dismissContext', 'removeChatChip', 'removePickedItem']],
})),
actions({
removeChip: (chip: ContextChip) => ({ chip }),
}),
selectors({
picks: [
(s) => [s.pickedByChat, chatKey],
(pickedByChat: Record<string, AttachedContextItem[]>, chat: string): AttachedContextItem[] =>
pickedByChat[chat] ?? [],
],
pageItems: [
(s) => [s.contextItems, s.removedGroupsByTask, (_, p: ChatContextLogicProps) => p.taskId],
(
contextItems: AttachedContextItem[],
removedGroupsByTask: Record<string, string[]>,
taskId?: string
): AttachedContextItem[] => {
if (!taskId) {
return contextItems
}
const removedGroups = removedGroupsByTask[taskId] ?? []
return contextItems.filter(
(item) => item.hidden && !(item.dismissGroup && removedGroups.includes(item.dismissGroup))
)
},
],
chips: [
(s) => [s.chatChipsByTask, s.pageItems, s.picks, chatKey],
(
chatChipsByTask: ChatChipsByTask,
pageItems: AttachedContextItem[],
picks: AttachedContextItem[],
chat: string
): ContextChip[] =>
uniqueBy(
[
...(chatChipsByTask[chat] ?? []),
...pageItems.filter((item) => !item.hidden && !item.previewHtml).map(contextChip),
...picks.map(contextChip),
],
(chip) => chip.id
),
],
attachments: [
(s) => [s.pageItems],
(pageItems: AttachedContextItem[]): ContextAttachment[] =>
pageItems.flatMap((item) =>
!item.hidden && item.previewHtml ? [{ chip: contextChip(item), previewHtml: item.previewHtml }] : []
),
],
}),
listeners(({ actions, values, props }) => ({
removeChip: ({ chip }) => {
actions.removePickedItem(chip.id, props.taskId)
if (props.taskId) {
actions.removeChatChip(props.taskId, chip.id, chip.dismissGroup)
} else if (values.pageItems.some((item: AttachedContextItem) => attachedContextItemKey(item) === chip.id)) {
actions.dismissContext(chip.id, chip.dismissGroup)
}
},
})),
])