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