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.

1.8 KB
Raw Blame
import { FileSystemImport } from '~/queries/schema/schema-general'

import { DecideRequestApiQuestions, DecideResponseApi } from 'products/ml_inference/frontend/generated/api.schemas'

import { sidebarProductMeta } from '../../sidebarProductMeta'
import { productsItemName } from './productsCatalog'

export const APP_MATCH_THRESHOLD = 0.5

export interface AppMatchGroups {
    matching: FileSystemImport[]
    other: FileSystemImport[]
}

export function buildAppRankingQuestions(items: FileSystemImport[]): DecideRequestApiQuestions[] {
    const batches: DecideRequestApiQuestions[] = []
    for (let offset = 0; offset < items.length; offset += 32) {
        batches.push(
            Object.fromEntries(
                items.slice(offset, offset + 32).map((item, index) => [
                    `app_${offset + index}`,
                    {
                        type: 'noul',
                        instructions: `Would this PostHog app help with the user's goal? Interpret partial words and unfinished descriptions as intent. App: ${productsItemName(item)}. Category: ${item.category ?? ''}. ${sidebarProductMeta(item).description ?? ''} Example: ${sidebarProductMeta(item).example ?? ''}`,
                    },
                ])
            )
        )
    }
    return batches
}

export function readAppRankings(result: DecideResponseApi, items: FileSystemImport[]): Record<string, number> {
    const scores: Record<string, number> = {}
    for (const [index, item] of items.entries()) {
        const answer = result.answers[`app_${index}`]
        if (answer?.type === 'noul' && typeof answer.probability === 'number' && Number.isFinite(answer.probability)) {
            scores[item.path] = Math.max(0, Math.min(1, answer.probability))
        }
    }
    return scores
}