Open History to see the last commit
1import type { ModelSelection, ProviderOptionDescriptor } from "@t3tools/contracts";
2import {
3 buildProviderOptionSelectionsFromDescriptors,
4 getProviderOptionCurrentLabel,
5 getProviderOptionCurrentValue,
6 isClaudeUltrathinkPrompt,
7} from "@t3tools/shared/model";
8import { REASONING_EFFORT_OPTION_IDS } from "./state/threadExecution.ts";
9import { mFallback, msg } from "gt-i18n";
10import type { M } from "gt-i18n";
11
12const REASONING_DESCRIPTOR_IDS: ReadonlySet<string> = new Set([
13 ...REASONING_EFFORT_OPTION_IDS,
14 "thinking",
15]);
16
17/** The model's reasoning control: a known effort id, or a select its agent marked as reasoning. */
18export function isReasoningControl(descriptor: ProviderOptionDescriptor): boolean {
19 return (
20 (descriptor.type === "select" && descriptor.role === "reasoning") ||
21 REASONING_DESCRIPTOR_IDS.has(descriptor.id)
22 );
23}
24
25export function isReasoningSelect(
26 descriptor: ProviderOptionDescriptor,
27): descriptor is Extract<ProviderOptionDescriptor, { type: "select" }> {
28 return descriptor.type === "select" && isReasoningControl(descriptor);
29}
30
31export function replaceDescriptorCurrentValue(
32 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
33 descriptorId: string,
34 currentValue: string | boolean | undefined,
35): ReadonlyArray<ProviderOptionDescriptor> {
36 return descriptors.map((descriptor) =>
37 descriptor.id !== descriptorId
38 ? descriptor
39 : descriptor.type === "boolean"
40 ? {
41 ...descriptor,
42 ...(typeof currentValue === "boolean" ? { currentValue } : {}),
43 }
44 : {
45 ...descriptor,
46 ...(typeof currentValue === "string" ? { currentValue } : {}),
47 },
48 );
49}
50
51export function getDescriptorStringValue(
52 descriptor: Extract<ProviderOptionDescriptor, { type: "select" }> | null,
53 selection?: ModelSelection | null,
54 reportedSelection?: ModelSelection | null,
55): string | null {
56 if (!descriptor) {
57 return null;
58 }
59 const value = getProviderOptionCurrentValue(descriptor, selection, reportedSelection);
60 return typeof value === "string" ? value : null;
61}
62
63// Prompt-injected levels such as Ultrathink still work when typed, but pickers do not offer them.
64export function selectableProviderOptionChoices(
65 descriptor: Extract<ProviderOptionDescriptor, { type: "select" }>,
66) {
67 return descriptor.options.filter(
68 (option) => !descriptor.promptInjectedValues?.includes(option.id),
69 );
70}
71
72export function getPromptControlledTraits(
73 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
74 prompt: string,
75 allowPromptInjectedEffort = true,
76) {
77 const primarySelectDescriptor =
78 descriptors.find((descriptor) => descriptor.type === "select") ?? null;
79 const ultrathinkPromptControlled =
80 allowPromptInjectedEffort &&
81 (primarySelectDescriptor?.promptInjectedValues?.length ?? 0) > 0 &&
82 isClaudeUltrathinkPrompt(prompt);
83 const ultrathinkInBodyText =
84 ultrathinkPromptControlled && isClaudeUltrathinkPrompt(prompt.replace(/^Ultrathink:\s*/i, ""));
85 return { primarySelectDescriptor, ultrathinkPromptControlled, ultrathinkInBodyText };
86}
87
88export function getTraitSelectChange(
89 selected: ReturnType<typeof getPromptControlledTraits> & {
90 descriptors: ReadonlyArray<ProviderOptionDescriptor>;
91 },
92 descriptor: Extract<ProviderOptionDescriptor, { type: "select" }>,
93 prompt: string,
94 value: string,
95) {
96 if (!value || descriptor.promptInjectedValues?.includes(value)) return null;
97 const isPrimary = descriptor.id === selected.primarySelectDescriptor?.id;
98 if (isPrimary && selected.ultrathinkInBodyText) return null;
99 return {
100 prompt:
101 isPrimary && selected.ultrathinkPromptControlled
102 ? prompt.replace(/^Ultrathink:\s*/i, "")
103 : prompt,
104 modelOptions: buildProviderOptionSelectionsFromDescriptors(
105 replaceDescriptorCurrentValue(selected.descriptors, descriptor.id, value),
106 ),
107 };
108}
109
110export function buildTraitsTriggerDisplay(
111 input: {
112 descriptors: ReadonlyArray<ProviderOptionDescriptor>;
113 primarySelectDescriptorId: string | null;
114 ultrathinkPromptControlled: boolean;
115 modelSelection?: ModelSelection | null;
116 reportedModelSelection?: ModelSelection | null | undefined;
117 },
118 resolve: M = mFallback,
119): {
120 label: string;
121 /** "Fast" or "Ultrafast" when a speed option is on. */
122 speedLabel: string | null;
123 ultracode: boolean;
124} {
125 let fastModeFallbackLabel: string | null = null;
126 let speedLabel: string | null = null;
127 let reasoningLabelIndex = -1;
128 let ultracodeLabelIndex = -1;
129 const labels: Array<string> = [];
130 for (const descriptor of input.descriptors) {
131 if (descriptor.id === "fastMode" && descriptor.type === "boolean") {
132 speedLabel = descriptor.currentValue === true ? "Fast" : null;
133 fastModeFallbackLabel = speedLabel ?? resolve(msg({ context: "model speed tier" })`Normal`);
134 continue;
135 }
136 if (descriptor.id === "ultracode" && descriptor.type === "boolean") {
137 if (descriptor.currentValue === true) {
138 ultracodeLabelIndex = labels.length;
139 labels.push(descriptor.label);
140 }
141 continue;
142 }
143 if (descriptor.id === "serviceTier" && descriptor.type === "select") {
144 const currentValue = getProviderOptionCurrentValue(descriptor);
145 const fastTier = descriptor.options.find(({ label }) => label === "Fast");
146 const ultrafastTier = descriptor.options.find(({ label }) => label === "Ultrafast");
147 if (
148 ((fastTier || ultrafastTier) && currentValue === "default") ||
149 (fastTier && currentValue === fastTier.id) ||
150 (ultrafastTier && currentValue === ultrafastTier.id)
151 ) {
152 speedLabel =
153 ultrafastTier && currentValue === ultrafastTier.id
154 ? "Ultrafast"
155 : fastTier && currentValue === fastTier.id
156 ? "Fast"
157 : null;
158 fastModeFallbackLabel =
159 descriptor.options.find(({ id }) => id === currentValue)?.label ??
160 resolve(msg({ context: "model speed tier" })`Normal`);
161 continue;
162 }
163 }
164 const label =
165 input.ultrathinkPromptControlled && descriptor.id === input.primarySelectDescriptorId
166 ? "Ultrathink"
167 : descriptor.type === "boolean"
168 ? descriptor.currentValue === true
169 ? resolve(msg`${{ label: descriptor.label }} On`)
170 : resolve(msg`${{ label: descriptor.label }} Off`)
171 : (getProviderOptionCurrentLabel(
172 descriptor,
173 input.modelSelection,
174 input.reportedModelSelection,
175 ) ?? (descriptor.id === "thinking" ? descriptor.label : null));
176 if (typeof label === "string" && label.length > 0) {
177 if (reasoningLabelIndex === -1 && isReasoningSelect(descriptor)) {
178 reasoningLabelIndex = labels.length;
179 }
180 labels.push(label);
181 }
182 }
183
184 const ultracode = ultracodeLabelIndex >= 0;
185 if (labels.length === 0 && fastModeFallbackLabel !== null) {
186 return { label: fastModeFallbackLabel, speedLabel, ultracode };
187 }
188 if (speedLabel) {
189 if (reasoningLabelIndex >= 0) {
190 labels[reasoningLabelIndex] = `${labels[reasoningLabelIndex]} ${speedLabel}`;
191 } else {
192 labels.push(speedLabel);
193 }
194 }
195 return { label: labels.join(" · "), speedLabel, ultracode };
196}