Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

modelTraits.ts

187 lines6.5 KB

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