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.

34.5 KB
Raw Blame
import { MakeLogicType, actions, beforeUnmount, connect, kea, listeners, path, props, reducers, selectors } from 'kea'
import posthog from 'posthog-js'

import { lemonToast } from '@posthog/lemon-ui'

import api, { ApiError, RateLimitError } from 'lib/api'
import { SetupTaskId, globalSetupLogic } from 'lib/components/ProductSetup'
import { getAccessControlDisabledReason } from 'lib/utils/accessControlUtils'
import { uuid } from 'lib/utils/dom'

import { AccessControlLevel, AccessControlResourceType } from '~/types'

import type { ModelOption } from '../modelPickerLogic'
import { llmProviderKeysLogic, normalizeLLMProvider } from '../settings/llmProviderKeysLogic'
import type { LLMProviderKey } from '../settings/llmProviderKeysLogic'
import { llmPlaygroundModelLogic } from './llmPlaygroundModelLogic'
import { llmPlaygroundPromptsLogic, type Message, type PromptConfig } from './llmPlaygroundPromptsLogic'
import { llmPlaygroundVariablesLogic } from './llmPlaygroundVariablesLogic'
import {
    type PlaygroundDecisionResult,
    type PlaygroundQuestion,
    decisionQuestionsDisabledReason,
    toDecisionQuestionsPayload,
} from './playgroundDecisions'
import { isMessageSendable, toProviderMessages } from './playgroundMessageMapping'
import { resolveProviderKeyForPrompt } from './playgroundModelMatching'
import {
    extractVariablesFromTexts,
    getVariableValue,
    promptTemplateTexts,
    substituteVariables,
} from './playgroundTemplating'

interface ToolCallChunk {
    id?: string
    function: {
        name?: string
        arguments?: string
    }
}

export interface AggregatedToolCall {
    id: string
    name: string
    arguments: string
}

export interface UsageSummary {
    prompt_tokens?: number | null
    completion_tokens?: number | null
    total_tokens?: number | null
    cache_read_tokens?: number | null
    cache_write_tokens?: number | null
}

export function appendToolCallChunk(state: AggregatedToolCall[], toolCall: ToolCallChunk): AggregatedToolCall[] {
    if (toolCall.id && toolCall.id !== 'null') {
        const existingIndex = state.findIndex((tc) => tc.id === toolCall.id)
        if (existingIndex >= 0) {
            const updated = [...state]
            updated[existingIndex] = {
                ...updated[existingIndex],
                name: toolCall.function?.name || updated[existingIndex].name,
                arguments: updated[existingIndex].arguments + (toolCall.function?.arguments || ''),
            }
            return updated
        }
        return [
            ...state,
            {
                id: toolCall.id,
                name: toolCall.function?.name || '',
                arguments: toolCall.function?.arguments || '',
            },
        ]
    }

    if (state.length === 0) {
        return state
    }

    const updated = [...state]
    const lastIndex = updated.length - 1
    updated[lastIndex] = {
        ...updated[lastIndex],
        arguments: updated[lastIndex].arguments + (toolCall.function?.arguments || ''),
    }
    return updated
}

/** Neutralize markdown syntax in a value we did not author.
 *
 * A result card renders its text with `LemonMarkdown`, images included, so a model id taken from
 * an ingested `$ai_model` property would otherwise turn `![x](https://…)` into a live request off
 * an analyst's browser. CommonMark treats a backslash before any ASCII punctuation as a literal,
 * so escaping the whole punctuation range covers link, image, and emphasis syntax at once.
 */
export function escapeMarkdownInline(value: string): string {
    return value.replace(/[!-/:-@[-`{-~]/g, '\\$&')
}

export function describeError(err: unknown, fallbackMessage: string): { message: string; status?: number } {
    if (err instanceof ApiError) {
        const dataError = typeof err.data?.error === 'string' ? err.data.error : null
        return {
            message: dataError || err.detail || err.message || fallbackMessage,
            status: err.status,
        }
    }
    return {
        message: (err instanceof Error && err.message) || fallbackMessage,
    }
}

function normalizeUsageFromStreamChunk(data: Record<string, unknown>): UsageSummary {
    const promptTokens = (data.input_tokens as number | undefined) ?? (data.prompt_tokens as number | undefined) ?? null
    const completionTokens =
        (data.output_tokens as number | undefined) ?? (data.completion_tokens as number | undefined) ?? null
    const totalTokens = (data.total_tokens as number | undefined) ?? null
    const cacheReadTokens = (data.cache_read_tokens as number | undefined) ?? null
    const cacheWriteTokens = (data.cache_write_tokens as number | undefined) ?? null

    return {
        prompt_tokens: typeof promptTokens === 'number' ? promptTokens : null,
        completion_tokens: typeof completionTokens === 'number' ? completionTokens : null,
        total_tokens: typeof totalTokens === 'number' ? totalTokens : null,
        cache_read_tokens: typeof cacheReadTokens === 'number' ? cacheReadTokens : null,
        cache_write_tokens: typeof cacheWriteTokens === 'number' ? cacheWriteTokens : null,
    }
}

/** Merge a new usage chunk into existing usage, keeping non-zero values from prior chunks.
 * Anthropic sends input_tokens in message_start and output_tokens in message_delta,
 * so a simple replace would zero out the input_tokens. */
export function mergeUsage(prev: UsageSummary, next: UsageSummary): UsageSummary {
    const pick = (a: number | null | undefined, b: number | null | undefined): number | null => {
        if (typeof b === 'number' && b > 0) {
            return b
        }
        return typeof a === 'number' ? a : null
    }
    return {
        prompt_tokens: pick(prev.prompt_tokens, next.prompt_tokens),
        completion_tokens: pick(prev.completion_tokens, next.completion_tokens),
        total_tokens: pick(prev.total_tokens, next.total_tokens),
        cache_read_tokens: pick(prev.cache_read_tokens, next.cache_read_tokens),
        cache_write_tokens: pick(prev.cache_write_tokens, next.cache_write_tokens),
    }
}

export interface ComparisonItem {
    id: string
    promptId: string
    promptLabel?: string
    model: string
    systemPrompt: string
    requestMessages: Message[]
    response: string
    reasoning?: string
    toolCalls?: AggregatedToolCall[]
    expectsJson?: boolean
    provider?: string
    providerKeyId?: string | null
    error?: boolean
    usage?: UsageSummary
    ttftMs?: number | null
    latencyMs?: number | null
    /** Set for a System One run, with the questions as asked so the answers keep their labels after edits. */
    decision?: { questions: PlaygroundQuestion[]; result: PlaygroundDecisionResult } | null
}

function buildCompletionRequest(
    prompt: PromptConfig,
    model: string,
    provider: string,
    providerKeyId: string | null,
    system: string,
    messages: Message[]
): Record<string, unknown> {
    return {
        system,
        messages: toProviderMessages(messages),
        model,
        provider,
        thinking: prompt.thinking,
        ...(providerKeyId ? { provider_key_id: providerKeyId } : {}),
        ...(prompt.tools ? { tools: prompt.tools } : {}),
        ...(prompt.responseFormat ? { response_format: prompt.responseFormat } : {}),
        ...(prompt.maxTokens !== null && prompt.maxTokens > 0 ? { max_tokens: prompt.maxTokens } : {}),
        ...(typeof prompt.temperature === 'number' ? { temperature: prompt.temperature } : {}),
        ...(typeof prompt.topP === 'number' ? { top_p: prompt.topP } : {}),
        ...(prompt.reasoningLevel ? { reasoning_level: prompt.reasoningLevel } : {}),
    }
}

/** Throws RateLimitError for a throttled request, so it reaches the same banner as a throttled stream. */
async function requestDecision(
    prompt: PromptConfig,
    providerKeyId: string | null,
    variableValues: Record<string, string>,
    signal: AbortSignal
): Promise<PlaygroundDecisionResult> {
    const resolve = (text: string): string => substituteVariables(text, variableValues)
    try {
        // nosemgrep: prefer-codegen-api -- the llm_proxy routes sit on the root router, which the generated client does not cover.
        return await api.create<PlaygroundDecisionResult>(
            '/api/llm_proxy/decision/',
            {
                provider_key_id: providerKeyId,
                state: resolve(prompt.decisionInput),
                questions: toDecisionQuestionsPayload(prompt.decisionQuestions, resolve),
            },
            { signal }
        )
    } catch (err) {
        const retryAfter =
            err instanceof ApiError && err.status === 429 ? parseInt(err.headers?.get('Retry-After') ?? '', 10) : NaN
        throw Number.isFinite(retryAfter) ? new RateLimitError(retryAfter) : err
    }
}

/** Two connections can serve the same model id, so the selected key decides which entry the prompt means. */
export function findPromptModel(prompt: PromptConfig, options: ModelOption[]): ModelOption | undefined {
    return (
        options.find((option) => option.id === prompt.model && option.providerKeyId === prompt.selectedProviderKeyId) ??
        options.find((option) => option.id === prompt.model)
    )
}

export function isPromptRunnable(prompt: PromptConfig, isSystemOne: boolean): boolean {
    return isSystemOne ? !!prompt.decisionInput.trim() : prompt.messages.some(isMessageSendable)
}

// Module-level abort controller so an in-flight run can be cancelled. Using a ref instead of
// Kea state avoids storing a non-serializable object in reducers.
let currentAbortController: AbortController | null = null

// An abandoned run stops streaming and reports nothing, because nobody is waiting for its result.
function abandonCurrentRun(): void {
    currentAbortController?.abort()
    currentAbortController = null
}

export type LLMPlaygroundRunLogicProps = Record<string, never>

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface llmPlaygroundRunLogicValues {
    activeProviderKeyId: string | null // llmPlaygroundModelLogic
    runnableModelOptions: ModelOption[] // llmPlaygroundModelLogic
    systemOnePromptIds: string[] // llmPlaygroundModelLogic
    promptConfigs: PromptConfig[] // llmPlaygroundPromptsLogic
    variableValues: Record<string, string> // llmPlaygroundVariablesLogic
    providerKeys: LLMProviderKey[] // llmProviderKeysLogic
    comparisonItems: ComparisonItem[]
    hasRunnablePrompts: boolean
    rateLimitedUntil: number | null
    runAccessDeniedReason: string | null
    submitting: boolean
    subscriptionRequired: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface llmPlaygroundRunLogicActions {
    resetPlayground: () => {
        value: true
    } // llmPlaygroundPromptsLogic
    abortRun: () => {
        value: true
    }
    addToComparison: (item: ComparisonItem) => {
        item: ComparisonItem
    }
    finishSubmitPrompt: () => {
        value: true
    }
    setRateLimited: (retryAfterSeconds: number) => {
        retryAfterSeconds: number
    }
    setSubscriptionRequired: (required: boolean) => {
        required: boolean
    }
    submitPrompt: () => {
        value: true
    }
    updateComparisonItem: (
        id: string,
        payload: Partial<ComparisonItem>
    ) => {
        id: string
        payload: Partial<ComparisonItem>
    }
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface llmPlaygroundRunLogicMeta {
    __keaTypeGenInternalSelectorTypes: {
        hasRunnablePrompts: (promptConfigs: PromptConfig[], systemOnePromptIds: string[]) => boolean
    }
}

export type llmPlaygroundRunLogicType = MakeLogicType<
    llmPlaygroundRunLogicValues,
    llmPlaygroundRunLogicActions,
    LLMPlaygroundRunLogicProps,
    llmPlaygroundRunLogicMeta
>

export const llmPlaygroundRunLogic = kea<llmPlaygroundRunLogicType>([
    path(['products', 'ai_observability', 'frontend', 'playground', 'llmPlaygroundRunLogic']),
    props({} as LLMPlaygroundRunLogicProps),

    connect(() => ({
        values: [
            llmPlaygroundPromptsLogic,
            ['promptConfigs'],
            llmPlaygroundModelLogic,
            ['runnableModelOptions', 'activeProviderKeyId', 'systemOnePromptIds'],
            llmProviderKeysLogic,
            ['providerKeys'],
            llmPlaygroundVariablesLogic,
            ['variableValues'],
        ],
        actions: [llmPlaygroundPromptsLogic, ['resetPlayground']],
    })),

    actions({
        submitPrompt: true,
        finishSubmitPrompt: true,
        abortRun: true,
        addToComparison: (item: ComparisonItem) => ({ item }),
        updateComparisonItem: (id: string, payload: Partial<ComparisonItem>) => ({ id, payload }),
        setRateLimited: (retryAfterSeconds: number) => ({ retryAfterSeconds }),
        setSubscriptionRequired: (required: boolean) => ({ required }),
    }),

    reducers({
        submitting: [
            false as boolean,
            {
                submitPrompt: () => true,
                finishSubmitPrompt: () => false,
            },
        ],
        comparisonItems: [
            [] as ComparisonItem[],
            {
                resetPlayground: () => [],
                submitPrompt: () => [],
                addToComparison: (state: ComparisonItem[], { item }: { item: ComparisonItem }) => [...state, item],
                updateComparisonItem: (
                    state: ComparisonItem[],
                    { id, payload }: { id: string; payload: Partial<ComparisonItem> }
                ) => state.map((item) => (item.id === id ? { ...item, ...payload } : item)),
            },
        ],
        rateLimitedUntil: [
            null as number | null,
            {
                setRateLimited: (_: number | null, { retryAfterSeconds }: { retryAfterSeconds: number }) =>
                    Date.now() + retryAfterSeconds * 1000,
            },
        ],
        subscriptionRequired: [
            false as boolean,
            {
                setSubscriptionRequired: (_: boolean, { required }: { required: boolean }) => required,
            },
        ],
    }),

    selectors({
        hasRunnablePrompts: [
            (s) => [s.promptConfigs, s.systemOnePromptIds],
            (promptConfigs: PromptConfig[], systemOnePromptIds: string[]): boolean =>
                promptConfigs.some((prompt) => isPromptRunnable(prompt, systemOnePromptIds.includes(prompt.id))),
        ],
        // Resource-level only, so this comes from the server-rendered app context rather than the API.
        // A selector keeps it readable from the scene and overridable in tests.
        runAccessDeniedReason: [
            () => [],
            (): string | null =>
                getAccessControlDisabledReason(AccessControlResourceType.LlmPlayground, AccessControlLevel.Editor),
        ],
    }),

    listeners(({ actions, values }) => ({
        abortRun: () => {
            posthog.capture('llma playground prompt aborted')
            currentAbortController?.abort()
        },
        setRateLimited: ({ retryAfterSeconds }) => {
            posthog.capture('llma playground rate limited', { retry_after_seconds: retryAfterSeconds })
        },
        setSubscriptionRequired: ({ required }) => {
            if (required) {
                posthog.capture('llma playground subscription required')
            }
        },

        submitPrompt: async () => {
            abandonCurrentRun()

            // Guarding here rather than only on the Run button, because both message textareas
            // submit on Cmd+Enter and bypass the button entirely.
            if (values.runAccessDeniedReason) {
                lemonToast.error(values.runAccessDeniedReason)
                actions.finishSubmitPrompt()
                return
            }

            const runnablePrompts = values.promptConfigs
                .map((prompt: PromptConfig, index: number) => ({
                    prompt,
                    index,
                    messagesToSend: prompt.messages.filter(isMessageSendable),
                }))
                .filter(({ prompt }: { prompt: PromptConfig }) =>
                    isPromptRunnable(prompt, values.systemOnePromptIds.includes(prompt.id))
                )

            if (runnablePrompts.length === 0) {
                lemonToast.error('Add some messages, or the input for a System One model, before running prompts')
                actions.finishSubmitPrompt()
                return
            }

            // Count only the prompts actually submitted; a skipped panel's variables are not sent,
            // so they belong in neither the warning nor the event.
            const runVariables = extractVariablesFromTexts(
                runnablePrompts.flatMap(({ prompt }) =>
                    promptTemplateTexts(prompt, values.systemOnePromptIds.includes(prompt.id))
                )
            )
            const runUnfilledVariables = runVariables.filter((name) => !getVariableValue(values.variableValues, name))

            posthog.capture('llma playground prompt submitted', {
                prompt_count: runnablePrompts.length,
                models: runnablePrompts.map(({ prompt }) => prompt.model),
                has_tools: runnablePrompts.some(({ prompt }) => !!prompt.tools?.length),
                has_response_format: runnablePrompts.some(({ prompt }) => !!prompt.responseFormat),
                total_message_count: runnablePrompts.reduce(
                    (sum, { messagesToSend }) => sum + messagesToSend.length,
                    0
                ),
                variable_count: runVariables.length,
                unfilled_variable_count: runUnfilledVariables.length,
            })

            if (runUnfilledVariables.length > 0) {
                const names = runUnfilledVariables.map((name) => `{{${name}}}`).join(', ')
                const suffix =
                    runUnfilledVariables.length === 1
                        ? 'The placeholder is sent as written.'
                        : 'The placeholders are sent as written.'
                lemonToast.warning(`No value for ${names}. ${suffix}`)
            }

            const abortController = new AbortController()
            currentAbortController = abortController
            const isAbandoned = (): boolean => currentAbortController !== abortController
            try {
                const runs = runnablePrompts.map(async ({ prompt, index, messagesToSend }) => {
                    // Resolve {{variables}} only in what is sent; the editors keep the raw template.
                    const resolvedSystemPrompt = substituteVariables(prompt.systemPrompt, values.variableValues)
                    const resolvedMessages = messagesToSend.map(
                        (m: Message): Message => ({
                            ...m,
                            content: substituteVariables(m.content, values.variableValues),
                            ...(m.toolCalls?.length
                                ? {
                                      toolCalls: m.toolCalls.map((toolCall) => ({
                                          ...toolCall,
                                          arguments: substituteVariables(toolCall.arguments, values.variableValues),
                                      })),
                                  }
                                : {}),
                        })
                    )
                    const liveItemId = uuid()
                    let responseUsage: UsageSummary = {}
                    let ttftMs: number | null = null
                    let latencyMs: number | null = null
                    let firstTokenTime: number | null = null
                    let startTime: number | null = null
                    let responseText = ''
                    let responseReasoning = ''
                    let responseHasError = false
                    let toolCalls: AggregatedToolCall[] = []
                    let providerKeyId: string | null = null
                    let selectedModelProvider = ''
                    let decision: ComparisonItem['decision'] = null
                    let itemAdded = false
                    let reportedStreamError: unknown = undefined

                    const upsertLiveItem = (): void => {
                        const payload: ComparisonItem = {
                            id: liveItemId,
                            promptId: prompt.id,
                            promptLabel: `Prompt ${index + 1}`,
                            model: prompt.model,
                            systemPrompt: resolvedSystemPrompt,
                            requestMessages: resolvedMessages,
                            response: responseText,
                            reasoning: responseReasoning,
                            toolCalls,
                            expectsJson: !!prompt.responseFormat,
                            provider: selectedModelProvider,
                            providerKeyId,
                            error: responseHasError,
                            usage: responseUsage,
                            ttftMs,
                            latencyMs,
                            decision,
                        }
                        if (!itemAdded) {
                            actions.addToComparison(payload)
                            itemAdded = true
                        } else {
                            actions.updateComparisonItem(liveItemId, payload)
                        }
                    }

                    const reportStreamError = (err: unknown): void => {
                        if (err instanceof RateLimitError) {
                            actions.setRateLimited(err.retryAfterSeconds)
                            responseHasError = true
                            upsertLiveItem()
                            return
                        }
                        if (err instanceof ApiError && err.status === 402) {
                            actions.setSubscriptionRequired(true)
                            responseHasError = true
                            upsertLiveItem()
                            return
                        }
                        const { message, status } = describeError(err, 'Unknown error')
                        const errorLabel = err instanceof ApiError ? 'Error' : 'Stream Connection Error'
                        responseText += `\n\n**${errorLabel}:** ${message}`
                        responseHasError = true
                        upsertLiveItem()
                        posthog.captureException(err, {
                            tag: 'llma-playground-prompt-run',
                            model: prompt.model,
                            provider: selectedModelProvider,
                            status,
                        })
                    }

                    upsertLiveItem()

                    try {
                        startTime = performance.now()

                        const selectedModel = findPromptModel(prompt, values.runnableModelOptions)
                        if (!selectedModel?.provider) {
                            // Reachable without the model ever being picked here: opening a trace in
                            // the playground, or loading a saved prompt, can carry a model the current
                            // key set no longer offers. Name it so the user knows what to change.
                            const describeMissingModel = (model: string): string =>
                                `Model '${model}' is not one of your available models. Pick a different model and try again.`
                            // The toast renders as text, the result card renders as markdown.
                            lemonToast.error(describeMissingModel(prompt.model))
                            responseText = `**Error:** ${describeMissingModel(escapeMarkdownInline(prompt.model))}`
                            responseHasError = true
                            upsertLiveItem()
                            return
                        }

                        // Model options carry the provider's display name ("Azure OpenAI"), but the
                        // completion endpoint validates against enum values ("azure_openai").
                        const normalizedProvider = normalizeLLMProvider(selectedModel.provider)
                        if (!normalizedProvider) {
                            const describeUnknownProvider = (model: string): string =>
                                `Model '${model}' has a provider PostHog does not recognize. Pick a different model and try again.`
                            lemonToast.error(describeUnknownProvider(prompt.model))
                            responseText = `**Error:** ${describeUnknownProvider(escapeMarkdownInline(prompt.model))}`
                            responseHasError = true
                            upsertLiveItem()
                            return
                        }

                        providerKeyId =
                            resolveProviderKeyForPrompt(prompt, values.runnableModelOptions, values.providerKeys)?.id ??
                            values.activeProviderKeyId
                        selectedModelProvider = normalizedProvider

                        if (normalizedProvider === 'system_one') {
                            const questionsError = decisionQuestionsDisabledReason(prompt.decisionQuestions)
                            if (questionsError) {
                                lemonToast.error(questionsError)
                                responseText = `**Error:** ${questionsError}`
                                responseHasError = true
                                upsertLiveItem()
                                return
                            }
                            const askedQuestions = prompt.decisionQuestions
                            try {
                                const result = await requestDecision(
                                    prompt,
                                    providerKeyId,
                                    values.variableValues,
                                    abortController.signal
                                )
                                decision = { questions: askedQuestions, result }
                                responseUsage = {
                                    prompt_tokens: result.input_tokens,
                                    completion_tokens: result.output_tokens,
                                }
                                upsertLiveItem()
                            } catch (err) {
                                if (!isAbandoned() && !abortController.signal.aborted) {
                                    reportedStreamError = err
                                    reportStreamError(err)
                                }
                            }
                        } else {
                            // nosemgrep: prefer-codegen-api -- Legacy raw API call with a hand-written URL and an unchecked response type. No generated function covers this endpoint yet. Find out why the generated client skips it (no schema, no product tag, or excluded from the spec) and fix that first.
                            await api.stream('/api/llm_proxy/completion', {
                                method: 'POST',
                                data: buildCompletionRequest(
                                    prompt,
                                    selectedModel.id,
                                    selectedModelProvider,
                                    providerKeyId,
                                    resolvedSystemPrompt,
                                    resolvedMessages
                                ),
                                headers: { 'Content-Type': 'application/json' },
                                signal: abortController.signal,
                                onMessage: (event) => {
                                    if (abortController.signal.aborted || !event.data) {
                                        return
                                    }

                                    try {
                                        const data = JSON.parse(event.data)
                                        if (data.type === 'text') {
                                            if (firstTokenTime === null && startTime !== null) {
                                                firstTokenTime = performance.now()
                                                ttftMs = firstTokenTime - startTime
                                            }
                                            responseText += data.text
                                            upsertLiveItem()
                                        } else if (data.type === 'tool_call') {
                                            if (firstTokenTime === null && startTime !== null) {
                                                firstTokenTime = performance.now()
                                                ttftMs = firstTokenTime - startTime
                                            }
                                            toolCalls = appendToolCallChunk(toolCalls, data)
                                            actions.updateComparisonItem(liveItemId, {
                                                toolCalls,
                                                ttftMs,
                                            })
                                        } else if (data.type === 'reasoning') {
                                            const reasoningChunk =
                                                typeof data.reasoning === 'string'
                                                    ? data.reasoning
                                                    : typeof data.text === 'string'
                                                      ? data.text
                                                      : ''
                                            responseReasoning += reasoningChunk
                                            actions.updateComparisonItem(liveItemId, {
                                                reasoning: responseReasoning,
                                                ttftMs,
                                            })
                                        } else if (data.type === 'usage') {
                                            responseUsage = mergeUsage(
                                                responseUsage,
                                                normalizeUsageFromStreamChunk(data)
                                            )
                                            actions.updateComparisonItem(liveItemId, { usage: responseUsage })
                                        } else if (data.error) {
                                            responseText += `\n\n**LLM Error:** ${data.error}`
                                            responseHasError = true
                                            upsertLiveItem()
                                        }
                                    } catch (e) {
                                        console.error('Error parsing stream message:', e, 'Data:', event.data)
                                        responseText += `\n\n**Stream Error:** Could not parse response chunk.`
                                        responseHasError = true
                                        upsertLiveItem()
                                    }
                                },
                                onError: (err) => {
                                    // api.stream can pass the same error to onError twice: once from its onopen
                                    // handler, and again when fetch-event-source catches the rethrow below.
                                    if (!abortController.signal.aborted && err !== reportedStreamError) {
                                        reportedStreamError = err
                                        reportStreamError(err)
                                    }
                                    // fetch-event-source sends the request again whenever onerror returns, which
                                    // would charge the user for a duplicate completion. Throwing ends the stream.
                                    throw err
                                },
                            })
                        }

                        globalSetupLogic.findMounted()?.actions.markTaskAsCompleted(SetupTaskId.RunAiPlayground)
                    } catch (error) {
                        if (!isAbandoned() && error !== reportedStreamError) {
                            const { message, status } = describeError(error, 'Failed to initiate prompt submission.')
                            responseText += `\n\n**Error:** ${message}`
                            responseHasError = true
                            lemonToast.error(message)
                            posthog.captureException(error, {
                                tag: 'llma-playground-prompt-submit',
                                model: prompt.model,
                                provider: selectedModelProvider,
                                status,
                            })
                        }
                    } finally {
                        if (startTime !== null) {
                            latencyMs = performance.now() - startTime
                        }
                    }

                    if (isAbandoned()) {
                        return
                    }
                    // fetch-event-source resolves rather than rejects on abort, so a stop by the user ends up here.
                    if (abortController.signal.aborted) {
                        responseText += `${responseText ? '\n\n' : ''}*Generation stopped.*`
                    }

                    upsertLiveItem()

                    posthog.capture('llma playground prompt completed', {
                        model: prompt.model,
                        provider: selectedModelProvider,
                        latency_ms: latencyMs,
                        ttft_ms: ttftMs,
                        prompt_tokens: responseUsage.prompt_tokens,
                        completion_tokens: responseUsage.completion_tokens,
                        success: !responseHasError,
                        has_tools: !!prompt.tools?.length,
                        has_response_format: !!prompt.responseFormat,
                        aborted: abortController.signal.aborted,
                    })
                })

                await Promise.allSettled(runs)
            } finally {
                abortController.abort()
                if (!isAbandoned()) {
                    currentAbortController = null
                    actions.finishSubmitPrompt()
                }
            }
        },
    })),
    beforeUnmount(() => {
        abandonCurrentRun()
    }),
])