: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 {
ClaudeRuntimeAdapterEnumApi,
CodexRuntimeAdapterEnumApi,
CodexTaskRunCreateSchemaInitialPermissionModeEnumApi,
InitialPermissionModeEnumApi,
ModelChoiceApi,
ReasoningEffortEnumApi,
RuntimeAdapterEnumApi,
TaskRunCreateRequestSchemaApi,
TaskRuntimeEnumApi,
} from 'products/tasks/frontend/generated/api.schemas'
import { isOfferedModel, normalizeModelId } from 'products/tasks/frontend/modelCatalog'
import {
CAPABILITY_LADDER_BY_RUNTIME_ADAPTER,
DEFAULT_MODEL_BY_RUNTIME_ADAPTER,
MODELS,
REASONING_EFFORT_LABELS,
RUNTIME_OPTIONS,
} from 'products/tasks/frontend/modelCatalog.generated'
import { type PermissionMode, resolveModeForRuntimeAdapter } from './composerModes'
export interface ComposerEffortOption {
value: ReasoningEffortEnumApi
label: string
}
// What a thinking model supports at minimum. Only reached for a model the catalogue does not describe, which now means
// a run started on a model since retired from the catalog.
const FALLBACK_EFFORTS: ReasoningEffortEnumApi[] = [
ReasoningEffortEnumApi.Low,
ReasoningEffortEnumApi.Medium,
ReasoningEffortEnumApi.High,
]
export const DEFAULT_COMPOSER_MODEL = DEFAULT_MODEL_BY_RUNTIME_ADAPTER.claude
export const PI_DEFAULT_MODEL = 'gpt-5.6-terra'
/** The model a task runs on when neither the run nor the session names one. */
export function getDefaultModelForTaskRuntime(taskRuntime: TaskRuntimeEnumApi = TaskRuntimeEnumApi.Acp): string {
return taskRuntime === TaskRuntimeEnumApi.Pi ? PI_DEFAULT_MODEL : DEFAULT_COMPOSER_MODEL
}
export const DEFAULT_COMPOSER_EFFORT: ReasoningEffortEnumApi = ReasoningEffortEnumApi.High
const EFFORT_LABELS: Record<string, string> = REASONING_EFFORT_LABELS
// The catalogue is keyed by bare catalog ids, so a provider-qualified id is folded onto the model it names before
// any lookup. A run stored as `anthropic/claude-opus-5` otherwise reads as an unknown model on this surface alone.
function catalogueEntry(catalogue: ModelChoiceApi[], model: string | null | undefined): ModelChoiceApi | undefined {
if (!model) {
return undefined
}
const normalized = normalizeModelId(model)
return catalogue.find((option) => option.model === normalized)
}
export function getEffortsForModel(
catalogue: ModelChoiceApi[],
model: string | null | undefined
): ComposerEffortOption[] {
const efforts = catalogueEntry(catalogue, model)?.supported_efforts ?? FALLBACK_EFFORTS
return efforts.map((value) => ({ value, label: EFFORT_LABELS[value] ?? value }))
}
// Which runtime drives this model. The adapter is a property of the model, not an independent choice, so
// deriving it is what keeps a `(runtime_adapter, model)` pair from ever disagreeing. Defaults to `claude` for a
// model the catalogue hasn't described — the fallback lineup is Claude-only.
export function getRuntimeAdapterForModel(
catalogue: ModelChoiceApi[],
model: string | null | undefined
): RuntimeAdapterEnumApi {
return catalogueEntry(catalogue, model)?.runtime_adapter ?? RuntimeAdapterEnumApi.Claude
}
// The harnesses the catalogue actually offers, in the order the models arrive. Derived rather than enumerated, so a
// runtime the gateway stops serving disappears from the picker on its own.
export function listRuntimeAdapters(
catalogue: ModelChoiceApi[],
taskRuntime: TaskRuntimeEnumApi = TaskRuntimeEnumApi.Acp
): RuntimeAdapterEnumApi[] {
if (taskRuntime === TaskRuntimeEnumApi.Pi) {
return []
}
return [...new Set(catalogue.map((option) => option.runtime_adapter))]
}
export function modelsForRuntimeAdapter(
catalogue: ModelChoiceApi[],
runtimeAdapter: RuntimeAdapterEnumApi,
taskRuntime: TaskRuntimeEnumApi = TaskRuntimeEnumApi.Acp
): ModelChoiceApi[] {
if (taskRuntime === TaskRuntimeEnumApi.Pi) {
return catalogue
}
return catalogue.filter((option) => option.runtime_adapter === runtimeAdapter)
}
// Everything still offered, plus the model this run is already on when the catalogue has since retired it — without
// that entry the picker names the run's own model by its raw id. Call it through `useMemo`: composers re-render on
// every keystroke.
export function pickerModels(catalogue: ModelChoiceApi[], selectedModel: string | null | undefined): ModelChoiceApi[] {
const selected = catalogueEntry(catalogue, selectedModel)
return catalogue.filter((option) => isOfferedModel(option.model) || option.model === selected?.model)
}
// The model the ladder runs at the default effort. Landing there puts a fresh selection on a slider notch,
// so the picker opens on Faster/Smarter; a default that sits off the ladder sends it straight to Advanced.
function ladderDefaultModel(runtimeAdapter: RuntimeAdapterEnumApi): string | undefined {
return CAPABILITY_LADDER_BY_RUNTIME_ADAPTER[runtimeAdapter].find(
(notch) => notch.effort === DEFAULT_COMPOSER_EFFORT
)?.model
}
export function getDefaultModelForRuntimeAdapter(
catalogue: ModelChoiceApi[],
runtimeAdapter: RuntimeAdapterEnumApi,
configuredModel?: string | null
): string | null {
const models = modelsForRuntimeAdapter(catalogue, runtimeAdapter)
const preferredModel = configuredModel ? normalizeModelId(configuredModel) : null
const defaultModel = DEFAULT_MODEL_BY_RUNTIME_ADAPTER[runtimeAdapter]
const ladderModel = ladderDefaultModel(runtimeAdapter)
return (
models.find((option) => option.model === preferredModel)?.model ??
models.find((option) => option.model === defaultModel)?.model ??
models.find((option) => option.model === ladderModel)?.model ??
models[0]?.model ??
null
)
}
/** One stop on the Faster/Smarter slider: a model paired with the effort it runs at. */
export interface CapabilityNotch {
model: string
effort: ReasoningEffortEnumApi
}
/**
* The ladder rungs the live catalogue actually serves. A notch naming a model the gateway no longer offers — or an
* effort that model no longer accepts — drops out rather than becoming a stop that fails on send. The picker falls
* back to its plain effort list when fewer than two rungs survive.
*/
export function getCapabilityLadder(
catalogue: ModelChoiceApi[],
runtimeAdapter: RuntimeAdapterEnumApi
): CapabilityNotch[] {
return CAPABILITY_LADDER_BY_RUNTIME_ADAPTER[runtimeAdapter].filter((notch) =>
catalogue.some((option) => option.model === notch.model && option.supported_efforts.includes(notch.effort))
)
}
export function getHarnessLabel(harness: string): string {
return RUNTIME_OPTIONS.find((option) => (option.runtimeAdapter ?? option.runtime) === harness)?.label ?? harness
}
export function getModelLabel(catalogue: ModelChoiceApi[], model: string | null | undefined): string {
return catalogueEntry(catalogue, model)?.display_name ?? model ?? 'Model'
}
export function getEffortLabel(effort: string | null | undefined): string {
return effort ? (EFFORT_LABELS[effort] ?? effort) : 'Effort'
}
export interface ModelCostDisplay {
/** Per-token cost against the catalog baseline, ready to render: `2.5×`, `≈0.55×`. */
multiplier: string
/** The rates behind it: `Input $2 · Output $10 per 1M tokens`. */
summary: string
}
/** What a model costs, or `null` where the catalog quotes no rate and a picker shows none. */
export function getModelCost(model: string | null | undefined): ModelCostDisplay | null {
if (!model) {
return null
}
const entry = MODELS.find((candidate) => candidate.id === normalizeModelId(model))
if (!entry?.costMultiplier || !entry.costSummary) {
return null
}
return { multiplier: entry.costMultiplier, summary: entry.costSummary }
}
// Keep an effort only if the model supports it, else nothing. The stored-preference counterpart of
// `resolveEffortForModel`: a settings row wants "no pick" when the effort no longer applies, where a
// composer wants a concrete value to send. Mirrors the backend's `filter_unsupported_effort`.
export function filterEffortForModel(
catalogue: ModelChoiceApi[],
effort: string | null | undefined,
model: string | null | undefined
): ReasoningEffortEnumApi | null {
const allowed = getEffortsForModel(catalogue, model).map((option) => option.value)
return effort && allowed.includes(effort as ReasoningEffortEnumApi) ? (effort as ReasoningEffortEnumApi) : null
}
// Clamp an effort to one the selected model actually supports — the new-run path can inherit an effort from a
// previous run on a different model (e.g. `max` carried over to a model that only offers low/medium/high), and the
// backend rejects an out-of-range effort. Falls back to the default when valid, else the highest available.
export function resolveEffortForModel(
catalogue: ModelChoiceApi[],
effort: string | null | undefined,
model: string | null | undefined
): ReasoningEffortEnumApi {
const allowed = getEffortsForModel(catalogue, model).map((option) => option.value)
// A model with no effort control reports none; the picker hides the row, and `buildRunCreateRequest`
// omits the field entirely. This value is only ever a display fallback.
if (!allowed.length) {
return DEFAULT_COMPOSER_EFFORT
}
if (effort && allowed.includes(effort as ReasoningEffortEnumApi)) {
return effort as ReasoningEffortEnumApi
}
return allowed.includes(DEFAULT_COMPOSER_EFFORT) ? DEFAULT_COMPOSER_EFFORT : allowed[allowed.length - 1]
}
/**
* A run-create request that pins no runtime selection at all: the backend resolves the model
* triple from the stored team/user default (or its own fallback), and the permission mode
* rides along, clamped server-side to whichever runtime the default names. Used whenever the
* composer's selection is untouched — pinning the displayed fallback instead would freeze a
* value the server could have resolved better (and would go stale the moment the default
* changes or a fetch fails). The generated request types are discriminated on
* `runtime_adapter`, so this shape deliberately steps outside them.
*/
export function buildServerResolvedRunCreateRequest(
permissionMode: PermissionMode,
rest: Partial<TaskRunCreateRequestSchemaApi>
): TaskRunCreateRequestSchemaApi {
return { ...rest, initial_permission_mode: permissionMode } as unknown as TaskRunCreateRequestSchemaApi
}
/**
* Build a run-create request for the picked model.
*
* The request schema is discriminated on `runtime_adapter`, and each runtime names its permission modes
* differently — so which runtime a model belongs to has to be decided in one place rather than assumed at each
* call site. Everything else the caller needs (branch, resume id, message) rides along in `rest`.
*/
export function buildRunCreateRequest(
catalogue: ModelChoiceApi[],
model: string,
reasoningEffort: ReasoningEffortEnumApi,
permissionMode: PermissionMode,
rest: Partial<TaskRunCreateRequestSchemaApi>
): TaskRunCreateRequestSchemaApi {
// A model with no effort control rejects any effort at all — `get_reasoning_effort_error` answers
// "Supported values: none" — so the field is left off rather than sent with a display fallback.
const effort = getEffortsForModel(catalogue, model).length ? reasoningEffort : undefined
if (getRuntimeAdapterForModel(catalogue, model) === CodexRuntimeAdapterEnumApi.Codex) {
return {
...rest,
runtime_adapter: CodexRuntimeAdapterEnumApi.Codex,
model,
reasoning_effort: effort,
initial_permission_mode: resolveModeForRuntimeAdapter(
CodexRuntimeAdapterEnumApi.Codex,
permissionMode
) as CodexTaskRunCreateSchemaInitialPermissionModeEnumApi,
}
}
return {
...rest,
runtime_adapter: ClaudeRuntimeAdapterEnumApi.Claude,
model,
reasoning_effort: effort,
initial_permission_mode: resolveModeForRuntimeAdapter(
ClaudeRuntimeAdapterEnumApi.Claude,
permissionMode
) as InitialPermissionModeEnumApi,
}
}