@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 197 lines 7.16 KB
Raw
1 import type { ModelSelection, ProviderOptionDescriptor } from "@t3tools/contracts";
2 import {
3 buildProviderOptionSelectionsFromDescriptors,
4 getProviderOptionCurrentLabel,
5 getProviderOptionCurrentValue,
6 isClaudeUltrathinkPrompt,
7 } from "@t3tools/shared/model";
8 import { REASONING_EFFORT_OPTION_IDS } from "./state/threadExecution.ts";
9 import { mFallback, msg } from "gt-i18n";
10 import type { MFunctionType } from "gt-i18n/types";
11
12 const 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. */
18 export function isReasoningControl(descriptor: ProviderOptionDescriptor): boolean {
19 return (
20 (descriptor.type === "select" && descriptor.role === "reasoning") ||
21 REASONING_DESCRIPTOR_IDS.has(descriptor.id)
22 );
23 }
24
25 export function isReasoningSelect(
26 descriptor: ProviderOptionDescriptor,
27 ): descriptor is Extract<ProviderOptionDescriptor, { type: "select" }> {
28 return descriptor.type === "select" && isReasoningControl(descriptor);
29 }
30
31 export 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
51 export 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.
64 export function selectableProviderOptionChoices(
65 descriptor: Extract<ProviderOptionDescriptor, { type: "select" }>,
66 ) {
67 return descriptor.options.filter(
68 (option) => !descriptor.promptInjectedValues?.includes(option.id),
69 );
70 }
71
72 export 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
88 export 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
110 export 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: MFunctionType = 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 =
134 speedLabel ?? resolve(msg("Normal", { $context: "model speed tier" }));
135 continue;
136 }
137 if (descriptor.id === "ultracode" && descriptor.type === "boolean") {
138 if (descriptor.currentValue === true) {
139 ultracodeLabelIndex = labels.length;
140 labels.push(descriptor.label);
141 }
142 continue;
143 }
144 if (descriptor.id === "serviceTier" && descriptor.type === "select") {
145 const currentValue = getProviderOptionCurrentValue(descriptor);
146 const fastTier = descriptor.options.find(({ label }) => label === "Fast");
147 const ultrafastTier = descriptor.options.find(({ label }) => label === "Ultrafast");
148 if (
149 ((fastTier || ultrafastTier) && currentValue === "default") ||
150 (fastTier && currentValue === fastTier.id) ||
151 (ultrafastTier && currentValue === ultrafastTier.id)
152 ) {
153 speedLabel =
154 ultrafastTier && currentValue === ultrafastTier.id
155 ? "Ultrafast"
156 : fastTier && currentValue === fastTier.id
157 ? "Fast"
158 : null;
159 fastModeFallbackLabel =
160 descriptor.options.find(({ id }) => id === currentValue)?.label ??
161 resolve(msg("Normal", { $context: "model speed tier" }));
162 continue;
163 }
164 }
165 const label =
166 input.ultrathinkPromptControlled && descriptor.id === input.primarySelectDescriptorId
167 ? "Ultrathink"
168 : descriptor.type === "boolean"
169 ? descriptor.currentValue === true
170 ? resolve(msg("{label} On", { label: descriptor.label }))
171 : resolve(msg("{label} Off", { label: descriptor.label }))
172 : (getProviderOptionCurrentLabel(
173 descriptor,
174 input.modelSelection,
175 input.reportedModelSelection,
176 ) ?? (descriptor.id === "thinking" ? descriptor.label : null));
177 if (typeof label === "string" && label.length > 0) {
178 if (reasoningLabelIndex === -1 && isReasoningSelect(descriptor)) {
179 reasoningLabelIndex = labels.length;
180 }
181 labels.push(label);
182 }
183 }
184
185 const ultracode = ultracodeLabelIndex >= 0;
186 if (labels.length === 0 && fastModeFallbackLabel !== null) {
187 return { label: fastModeFallbackLabel, speedLabel, ultracode };
188 }
189 if (speedLabel) {
190 if (reasoningLabelIndex >= 0) {
191 labels[reasoningLabelIndex] = `${labels[reasoningLabelIndex]} ${speedLabel}`;
192 } else {
193 labels.push(speedLabel);
194 }
195 }
196 return { label: labels.join(" · "), speedLabel, ultracode };
197 }