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

modelTraits.ts

180 lines6.3 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​
14export const REASONING_SELECT_DESCRIPTOR_IDS: ReadonlySet<string> = new Set([
15 ...REASONING_EFFORT_OPTION_IDS,
16 "thinking",
17]);
18​
19export function replaceDescriptorCurrentValue(
20 descriptors: ReadonlyArray<ProviderOptionDescriptor>,
21 descriptorId: string,
22 currentValue: string | boolean | undefined,
23): ReadonlyArray<ProviderOptionDescriptor> {
24 return descriptors.map((descriptor) =>
25 descriptor.id !== descriptorId
26 ? descriptor
27 : descriptor.type === "boolean"
28 ? {
29 ...descriptor,
30 ...(typeof currentValue === "boolean" ? { currentValue } : {}),
31 }
32 : {
33 ...descriptor,
34 ...(typeof currentValue === "string" ? { currentValue } : {}),
35 },
36 );
38​
39export function getDescriptorStringValue(
40 descriptor: Extract<ProviderOptionDescriptor, { type: "select" }> | null,
41 selection?: ModelSelection | null,
42 reportedSelection?: ModelSelection | null,
43): string | null {
44 if (!descriptor) {
45 return null;
46 }
47 const value = getProviderOptionCurrentValue(descriptor, selection, reportedSelection);
48 return typeof value === "string" ? value : null;
50​
51// Prompt-injected levels such as Ultrathink still work when typed, but pickers do not offer them.
52export function selectableProviderOptionChoices(
53 descriptor: Extract<ProviderOptionDescriptor, { type: "select" }>,
54) {
55 return descriptor.options.filter(
56 (option) => !descriptor.promptInjectedValues?.includes(option.id),
57 );
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 };
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 || descriptor.promptInjectedValues?.includes(value)) return null;
85 const isPrimary = descriptor.id === selected.primarySelectDescriptor?.id;
86 if (isPrimary && selected.ultrathinkInBodyText) return null;
87 return {
88 prompt:
89 isPrimary && selected.ultrathinkPromptControlled
90 ? prompt.replace(/^Ultrathink:\s*/i, "")
91 : prompt,
92 modelOptions: buildProviderOptionSelectionsFromDescriptors(
93 replaceDescriptorCurrentValue(selected.descriptors, descriptor.id, value),
94 ),
95 };
97​
98export function buildTraitsTriggerDisplay(input: {
99 provider: ProviderDriverKind;
100 descriptors: ReadonlyArray<ProviderOptionDescriptor>;
101 primarySelectDescriptorId: string | null;
102 ultrathinkPromptControlled: boolean;
103 modelSelection?: ModelSelection | null;
104 reportedModelSelection?: ModelSelection | null | undefined;
105}): {
106 label: string;
107 /** "Fast" or "Ultrafast" when a speed option is on. */
108 speedLabel: string | null;
109 /** Labels other than reasoning and speed, such as context window or thinking. */
110 otherLabels: ReadonlyArray<string>;
111} {
112 let fastModeFallbackLabel: string | null = null;
113 let speedLabel: string | null = null;
114 let reasoningLabelIndex = -1;
115 const labels: Array<string> = [];
116 for (const descriptor of input.descriptors) {
117 if (descriptor.id === "fastMode" && descriptor.type === "boolean") {
118 speedLabel = descriptor.currentValue === true ? "Fast" : null;
119 fastModeFallbackLabel = speedLabel ?? "Normal";
120 continue;
121 }
122 if (
123 input.provider === "codex" &&
124 descriptor.id === "serviceTier" &&
125 descriptor.type === "select"
126 ) {
127 const currentValue = getProviderOptionCurrentValue(descriptor);
128 const fastTier = descriptor.options.find(({ label }) => label === "Fast");
129 const ultrafastTier = descriptor.options.find(({ label }) => label === "Ultrafast");
130 if (
131 ((fastTier || ultrafastTier) && currentValue === "default") ||
132 (fastTier && currentValue === fastTier.id) ||
133 (ultrafastTier && currentValue === ultrafastTier.id)
134 ) {
135 speedLabel =
136 ultrafastTier && currentValue === ultrafastTier.id
137 ? "Ultrafast"
138 : fastTier && currentValue === fastTier.id
139 ? "Fast"
140 : null;
141 fastModeFallbackLabel =
142 descriptor.options.find(({ id }) => id === currentValue)?.label ?? "Normal";
143 continue;
144 }
145 }
146 const label =
147 input.ultrathinkPromptControlled && descriptor.id === input.primarySelectDescriptorId
148 ? "Ultrathink"
149 : descriptor.type === "boolean"
150 ? `${descriptor.label} ${descriptor.currentValue === true ? "On" : "Off"}`
151 : getProviderOptionCurrentLabel(
152 descriptor,
153 input.modelSelection,
154 input.reportedModelSelection,
155 );
156 if (typeof label === "string" && label.length > 0) {
157 if (
158 reasoningLabelIndex === -1 &&
159 descriptor.type === "select" &&
160 REASONING_SELECT_DESCRIPTOR_IDS.has(descriptor.id)
161 ) {
162 reasoningLabelIndex = labels.length;
163 }
164 labels.push(label);
165 }
166 }
167​
168 const otherLabels = labels.filter((_, index) => index !== reasoningLabelIndex);
169 if (labels.length === 0 && fastModeFallbackLabel !== null) {
170 return { label: fastModeFallbackLabel, speedLabel, otherLabels };
171 }
172 if (speedLabel) {
173 if (reasoningLabelIndex >= 0) {
174 labels[reasoningLabelIndex] = `${labels[reasoningLabelIndex]} ${speedLabel}`;
175 } else {
176 labels.push(speedLabel);
177 }
178 }
179 return { label: labels.join(" · "), speedLabel, otherLabels };