Open-source Grok Bot alternative for named AI teammates. Local-first desktop, web, and mobile clients with per-bot tools, instructions, and memory. Independent fork of T3 Code.
import { Predicate } from "effect";
import { createTranslator } from "@akeru/client-runtime/i18n";
import {
type ProviderDriverKind,
type ProviderOptionDescriptor,
type ProviderOptionSelection,
type ScopedThreadRef,
type ServerProviderModel,
} from "@akeru/contracts";
import {
getProviderOptionCurrentLabel,
getProviderOptionCurrentValue,
getProviderOptionDescriptors,
isClaudeUltrathinkPrompt,
} from "@akeru/shared/model";
import { useI18n } from "~/i18n";
import { DraftId } from "../../composerDraftStore";
import { getProviderModelCapabilities } from "../../providerModels";
export type ProviderOptions = ReadonlyArray<ProviderOptionSelection>;
export type TraitsPersistence =
| {
threadRef?: ScopedThreadRef;
draftId?: DraftId;
onModelOptionsChange?: never;
}
| {
threadRef?: undefined;
onModelOptionsChange: (nextOptions: ProviderOptions | undefined) => void;
};
export const ULTRATHINK_PROMPT_PREFIX = "Ultrathink:\n";
type Translate = ReturnType<typeof useI18n>["t"];
const translateEnglish: Translate = createTranslator("en").translate;
export function replaceDescriptorCurrentValue(
descriptors: ReadonlyArray<ProviderOptionDescriptor>,
descriptorId: string,
currentValue: string | boolean | undefined,
): ReadonlyArray<ProviderOptionDescriptor> {
return descriptors.map((descriptor) =>
descriptor.id !== descriptorId
? descriptor
: descriptor.type === "boolean"
? {
...descriptor,
...(Predicate.isBoolean(currentValue) ? { currentValue } : {}),
}
: {
...descriptor,
...(Predicate.isString(currentValue) ? { currentValue } : {}),
},
);
}
export function getDescriptorStringValue(
descriptor: Extract<ProviderOptionDescriptor, { type: "select" }> | null,
): string | null {
if (!descriptor) {
return null;
}
const value = getProviderOptionCurrentValue(descriptor);
return Predicate.isString(value) ? value : null;
}
function getSelectedTraits(
provider: ProviderDriverKind,
models: ReadonlyArray<ServerProviderModel>,
model: string | null | undefined,
prompt: string,
modelOptions: ProviderOptions | null | undefined,
allowPromptInjectedEffort: boolean,
) {
const caps = getProviderModelCapabilities(models, model, provider);
const descriptors = getProviderOptionDescriptors({
caps,
selections: modelOptions,
});
const selectDescriptors = descriptors.filter(
(descriptor): descriptor is Extract<ProviderOptionDescriptor, { type: "select" }> =>
descriptor.type === "select",
);
const booleanDescriptors = descriptors.filter(
(descriptor): descriptor is Extract<ProviderOptionDescriptor, { type: "boolean" }> =>
descriptor.type === "boolean",
);
const primarySelectDescriptor = selectDescriptors[0] ?? null;
const contextWindowDescriptor =
selectDescriptors.find((descriptor) => descriptor.id === "contextWindow") ?? null;
const agentDescriptor = selectDescriptors.find((descriptor) => descriptor.id === "agent") ?? null;
const fastModeDescriptor =
booleanDescriptors.find((descriptor) => descriptor.id === "fastMode") ?? null;
const thinkingDescriptor =
booleanDescriptors.find((descriptor) => descriptor.id === "thinking") ?? null;
// Prompt-controlled effort (e.g. ultrathink in prompt text)
const ultrathinkPromptControlled =
allowPromptInjectedEffort &&
(primarySelectDescriptor?.promptInjectedValues?.length ?? 0) > 0 &&
isClaudeUltrathinkPrompt(prompt);
// Check if "ultrathink" appears in the body text (not just our prefix)
const ultrathinkInBodyText =
ultrathinkPromptControlled && isClaudeUltrathinkPrompt(prompt.replace(/^Ultrathink:\s*/i, ""));
const effort =
(ultrathinkPromptControlled
? "ultrathink"
: getDescriptorStringValue(primarySelectDescriptor)) ?? null;
const thinkingEnabled = Predicate.isBoolean(thinkingDescriptor?.currentValue)
? thinkingDescriptor.currentValue
: null;
const contextWindow = getDescriptorStringValue(contextWindowDescriptor);
const selectedAgent = getDescriptorStringValue(agentDescriptor);
const selectedAgentLabel = agentDescriptor
? getProviderOptionCurrentLabel(agentDescriptor)
: null;
return {
caps,
descriptors,
selectDescriptors,
booleanDescriptors,
primarySelectDescriptor,
contextWindowDescriptor,
agentDescriptor,
fastModeDescriptor,
thinkingDescriptor,
effort,
thinkingEnabled,
contextWindow,
ultrathinkPromptControlled,
ultrathinkInBodyText,
selectedAgent,
selectedAgentLabel,
};
}
export function getTraitsSectionVisibility(input: {
provider: ProviderDriverKind;
models: ReadonlyArray<ServerProviderModel>;
model: string | null | undefined;
prompt: string;
modelOptions: ProviderOptions | null | undefined;
allowPromptInjectedEffort?: boolean;
}) {
const selected = getSelectedTraits(
input.provider,
input.models,
input.model,
input.prompt,
input.modelOptions,
input.allowPromptInjectedEffort ?? true,
);
const showEffort = selected.primarySelectDescriptor !== null;
const showThinking = selected.thinkingDescriptor !== null;
const showFastMode = selected.fastModeDescriptor !== null;
const showContextWindow = selected.contextWindowDescriptor !== null;
const showAgent = selected.agentDescriptor !== null;
return {
...selected,
showEffort,
showThinking,
showFastMode,
showContextWindow,
showAgent,
hasAnyControls: showEffort || showThinking || showFastMode || showContextWindow || showAgent,
};
}
export function shouldRenderTraitsControls(input: {
provider: ProviderDriverKind;
models: ReadonlyArray<ServerProviderModel>;
model: string | null | undefined;
prompt: string;
modelOptions: ProviderOptions | null | undefined;
allowPromptInjectedEffort?: boolean;
}): boolean {
return getTraitsSectionVisibility(input).hasAnyControls;
}
/**
* Build the traits trigger's text label plus whether the fast-mode bolt should
* render. Claude and Cursor expose fast mode as a boolean, while Codex exposes
* it through the Standard/Fast service tiers. In either form, fast mode is a
* lightning bolt when on and nothing at all when off. The one exception is when
* fast mode is the only trait, where a bare bolt (or bare chevron) would leave
* the trigger unreadable.
*/
export function buildTraitsTriggerDisplay(input: {
provider: ProviderDriverKind;
descriptors: ReadonlyArray<ProviderOptionDescriptor>;
primarySelectDescriptorId: string | null;
ultrathinkPromptControlled: boolean;
t?: Translate;
}) {
const t = input.t ?? translateEnglish;
let fastModeFallbackLabel: string | null = null;
let fastModeEnabled = false;
const labels: Array<string> = [];
for (const descriptor of input.descriptors) {
if (descriptor.id === "fastMode" && descriptor.type === "boolean") {
fastModeEnabled = descriptor.currentValue === true;
fastModeFallbackLabel = fastModeEnabled ? t("Fast") : t("Normal");
continue;
}
if (
input.provider === "codex" &&
descriptor.id === "serviceTier" &&
descriptor.type === "select"
) {
const currentValue = getProviderOptionCurrentValue(descriptor);
const fastTier = descriptor.options.find(({ label }) => label === "Fast");
if (fastTier && (currentValue === "default" || currentValue === fastTier.id)) {
fastModeEnabled = currentValue === fastTier.id;
fastModeFallbackLabel =
descriptor.options.find(({ id }) => id === currentValue)?.label ??
(fastModeEnabled ? t("Fast") : t("Normal"));
continue;
}
}
const label =
input.ultrathinkPromptControlled && descriptor.id === input.primarySelectDescriptorId
? "Ultrathink"
: descriptor.type === "boolean"
? descriptor.currentValue === true
? t("{label} On", { label: descriptor.label })
: t("{label} Off", { label: descriptor.label })
: getProviderOptionCurrentLabel(descriptor);
if (Predicate.isString(label) && label.length > 0) {
labels.push(label);
}
}
// Only fall back to text when fast mode is genuinely the sole trait. Keying
// off an empty label list alone would also catch descriptors that resolved to
// no label at all, printing a bogus "Normal" for a model without fast mode.
if (labels.length === 0 && fastModeFallbackLabel !== null) {
return { label: fastModeFallbackLabel, showFastModeIcon: false };
}
return { label: labels.join(" · "), showFastModeIcon: fastModeEnabled };
}