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}): { label: string } {
114 let fastModeFallbackLabel: string | null = null;
115 let speedLabel: string | null = null;
116 let reasoningLabelIndex = -1;
117 const labels: Array<string> = [];
118 for (const descriptor of input.descriptors) {
119 if (descriptor.id === "fastMode" && descriptor.type === "boolean") {
120 speedLabel = descriptor.currentValue === true ? "Fast" : null;
121 fastModeFallbackLabel = speedLabel ?? "Normal";
122 continue;
123 }
124 if (
125 input.provider === "codex" &&
126 descriptor.id === "serviceTier" &&
127 descriptor.type === "select"
128 ) {
129 const currentValue = getProviderOptionCurrentValue(descriptor);
130 const fastTier = descriptor.options.find(({ label }) => label === "Fast");
131 const ultrafastTier = descriptor.options.find(({ label }) => label === "Ultrafast");
132 if (
133 ((fastTier || ultrafastTier) && currentValue === "default") ||
134 (fastTier && currentValue === fastTier.id) ||
135 (ultrafastTier && currentValue === ultrafastTier.id)
136 ) {
137 speedLabel =
138 ultrafastTier && currentValue === ultrafastTier.id
139 ? "Ultrafast"
140 : fastTier && currentValue === fastTier.id
141 ? "Fast"
142 : null;
143 fastModeFallbackLabel =
144 descriptor.options.find(({ id }) => id === currentValue)?.label ?? "Normal";
145 continue;
146 }
147 }
148 const label =
149 input.ultrathinkPromptControlled && descriptor.id === input.primarySelectDescriptorId
150 ? "Ultrathink"
151 : descriptor.type === "boolean"
152 ? `${descriptor.label} ${descriptor.currentValue === true ? "On" : "Off"}`
153 : getProviderOptionCurrentLabel(
154 descriptor,
155 input.modelSelection,
156 input.reportedModelSelection,
157 );
158 if (typeof label === "string" && label.length > 0) {
159 if (
160 reasoningLabelIndex === -1 &&
161 descriptor.type === "select" &&
162 REASONING_SELECT_DESCRIPTOR_IDS.has(descriptor.id)
163 ) {
164 reasoningLabelIndex = labels.length;
165 }
166 labels.push(label);
167 }
168 }
169
170 if (labels.length === 0 && fastModeFallbackLabel !== null) {
171 return { label: fastModeFallbackLabel };
172 }
173 if (speedLabel) {
174 if (reasoningLabelIndex >= 0) {
175 labels[reasoningLabelIndex] = `${labels[reasoningLabelIndex]} ${speedLabel}`;
176 } else {
177 labels.push(speedLabel);
178 }
179 }
180 return { label: labels.join(" · ") };
181}