The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { |
| 2 | type ModelCapabilities, |
| 3 | type ModelSelection, |
| 4 | type ProviderDriverKind, |
| 5 | type ProviderInstanceId, |
| 6 | type ProviderOptionSelection, |
| 7 | type ScopedThreadRef, |
| 8 | type ServerProviderModel, |
| 9 | } from "@t3tools/contracts"; |
| 10 | import { |
| 11 | buildExplicitProviderOptionSelectionsFromDescriptors, |
| 12 | getProviderOptionCurrentValue, |
| 13 | getProviderOptionDescriptors, |
| 14 | isClaudeUltrathinkPrompt, |
| 15 | normalizeModelSlug, |
| 16 | withImplicitFastModeDefault, |
| 17 | } from "@t3tools/shared/model"; |
| 18 | |
| 19 | import type { DraftId } from "../../composerDraftStore"; |
| 20 | import { getProviderModelCapabilities, keepsUnavailableModelOptions } from "../../providerModels"; |
| 21 | import { |
| 22 | getReasoningTraitStep, |
| 23 | type PromptUltrathink, |
| 24 | type TraitsSelectionInput, |
| 25 | } from "./TraitsPicker"; |
| 26 | |
| 27 | type ComposerProviderStateInput = { |
| 28 | provider: ProviderDriverKind; |
| 29 | model: string; |
| 30 | models: ReadonlyArray<ServerProviderModel>; |
| 31 | promptInjectionState?: ComposerPromptInjectionState; |
| 32 | modelOptions: ReadonlyArray<ProviderOptionSelection> | null | undefined; |
| 33 | includePlanAgent?: boolean | undefined; |
| 34 | }; |
| 35 | |
| 36 | type ComposerPromptInjectionState = "none" | "ultrathink"; |
| 37 | |
| 38 | type ComposerProviderState = { |
| 39 | provider: ProviderDriverKind; |
| 40 | promptEffort: string | null; |
| 41 | modelOptionsForDispatch: ReadonlyArray<ProviderOptionSelection> | undefined; |
| 42 | composerFrameClassName?: string; |
| 43 | composerSurfaceClassName?: string; |
| 44 | modelPickerIconClassName?: string; |
| 45 | }; |
| 46 | |
| 47 | type ComposerTraitsTarget = { |
| 48 | provider: ProviderDriverKind; |
| 49 | instanceId: ProviderInstanceId; |
| 50 | threadRef?: ScopedThreadRef | undefined; |
| 51 | draftId?: DraftId | undefined; |
| 52 | onDraftModelOptionsChange?: () => void; |
| 53 | model: string; |
| 54 | models: ReadonlyArray<ServerProviderModel>; |
| 55 | modelOptions: ReadonlyArray<ProviderOptionSelection> | undefined; |
| 56 | reportedModelSelection?: ModelSelection | null | undefined; |
| 57 | promptUltrathink: PromptUltrathink; |
| 58 | onPromptEdit: (edit: (prompt: string) => string) => void; |
| 59 | includePlanAgent?: boolean | undefined; |
| 60 | }; |
| 61 | |
| 62 | export function getComposerPromptInjectionState(prompt: string): ComposerPromptInjectionState { |
| 63 | return isClaudeUltrathinkPrompt(prompt) ? "ultrathink" : "none"; |
| 64 | } |
| 65 | |
| 66 | function resolveComposerOptionSelections( |
| 67 | models: ReadonlyArray<ServerProviderModel>, |
| 68 | model: string, |
| 69 | provider: ProviderDriverKind, |
| 70 | modelOptions: ReadonlyArray<ProviderOptionSelection> | null | undefined, |
| 71 | includePlanAgent: boolean | undefined, |
| 72 | ): { |
| 73 | caps: ModelCapabilities; |
| 74 | selections: ReadonlyArray<ProviderOptionSelection> | undefined; |
| 75 | } { |
| 76 | const caps = getProviderModelCapabilities(models, model, provider, includePlanAgent); |
| 77 | return { caps, selections: withImplicitFastModeDefault(caps, modelOptions) }; |
| 78 | } |
| 79 | |
| 80 | export function getComposerProviderState(input: ComposerProviderStateInput): ComposerProviderState { |
| 81 | const { |
| 82 | provider, |
| 83 | model, |
| 84 | models, |
| 85 | modelOptions, |
| 86 | promptInjectionState = "none", |
| 87 | includePlanAgent, |
| 88 | } = input; |
| 89 | if (keepsUnavailableModelOptions(provider, model)) { |
| 90 | const normalizedModel = normalizeModelSlug(model, provider); |
| 91 | const modelIsInCatalog = models.some((candidate) => candidate.slug === normalizedModel); |
| 92 | if (!modelIsInCatalog) { |
| 93 | const preservedOptions = modelOptions?.filter( |
| 94 | (option) => includePlanAgent || option.id !== "agent" || option.value !== "plan", |
| 95 | ); |
| 96 | return { |
| 97 | provider, |
| 98 | promptEffort: null, |
| 99 | modelOptionsForDispatch: |
| 100 | preservedOptions && preservedOptions.length > 0 ? preservedOptions : undefined, |
| 101 | }; |
| 102 | } |
| 103 | } |
| 104 | const { caps, selections } = resolveComposerOptionSelections( |
| 105 | models, |
| 106 | model, |
| 107 | provider, |
| 108 | modelOptions, |
| 109 | includePlanAgent, |
| 110 | ); |
| 111 | const descriptors = getProviderOptionDescriptors({ caps, selections }); |
| 112 | const primarySelectDescriptor = descriptors.find( |
| 113 | (descriptor): descriptor is Extract<(typeof descriptors)[number], { type: "select" }> => |
| 114 | descriptor.type === "select", |
| 115 | ); |
| 116 | const primaryValue = getProviderOptionCurrentValue(primarySelectDescriptor ?? null); |
| 117 | const promptEffort = typeof primaryValue === "string" ? primaryValue : null; |
| 118 | const ultrathinkActive = |
| 119 | (primarySelectDescriptor?.promptInjectedValues?.length ?? 0) > 0 && |
| 120 | promptInjectionState === "ultrathink"; |
| 121 | |
| 122 | return { |
| 123 | provider, |
| 124 | promptEffort, |
| 125 | modelOptionsForDispatch: buildExplicitProviderOptionSelectionsFromDescriptors( |
| 126 | descriptors, |
| 127 | selections, |
| 128 | ), |
| 129 | ...(ultrathinkActive |
| 130 | ? { |
| 131 | composerFrameClassName: "ultrathink-frame", |
| 132 | composerSurfaceClassName: "shadow-[0_0_0_1px_rgba(255,255,255,0.07)_inset]", |
| 133 | modelPickerIconClassName: "ultrathink-chroma", |
| 134 | } |
| 135 | : {}), |
| 136 | }; |
| 137 | } |
| 138 | |
| 139 | /** The caret stays on the same character when stepping strips a typed Ultrathink prefix. */ |
| 140 | export function getComposerReasoningStep(input: { |
| 141 | provider: ProviderDriverKind; |
| 142 | instanceId: ProviderInstanceId; |
| 143 | model: string; |
| 144 | models: ReadonlyArray<ServerProviderModel>; |
| 145 | modelOptions: ReadonlyArray<ProviderOptionSelection> | null | undefined; |
| 146 | reportedModelSelection?: ModelSelection | null | undefined; |
| 147 | prompt: string; |
| 148 | cursor: number; |
| 149 | includePlanAgent?: boolean | undefined; |
| 150 | direction: 1 | -1; |
| 151 | }): { |
| 152 | prompt: string; |
| 153 | cursor: number; |
| 154 | modelOptions: ReadonlyArray<ProviderOptionSelection>; |
| 155 | } | null { |
| 156 | const { prompt, cursor } = input; |
| 157 | const { selections } = resolveComposerOptionSelections( |
| 158 | input.models, |
| 159 | input.model, |
| 160 | input.provider, |
| 161 | input.modelOptions, |
| 162 | input.includePlanAgent, |
| 163 | ); |
| 164 | const step = getReasoningTraitStep({ |
| 165 | provider: input.provider, |
| 166 | instanceId: input.instanceId, |
| 167 | model: input.model, |
| 168 | models: input.models, |
| 169 | modelOptions: selections, |
| 170 | reportedModelSelection: input.reportedModelSelection, |
| 171 | prompt, |
| 172 | includePlanAgent: input.includePlanAgent, |
| 173 | direction: input.direction, |
| 174 | }); |
| 175 | if (!step) return null; |
| 176 | return { |
| 177 | prompt: step.prompt, |
| 178 | cursor: Math.max(0, cursor + step.prompt.length - prompt.length), |
| 179 | modelOptions: step.modelOptions, |
| 180 | }; |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | * The composer's traits input with the model's implicit defaults applied, so a pick writes what |
| 185 | * the composer would send. Null without a draft or thread to persist to. |
| 186 | */ |
| 187 | export function resolveComposerTraitsInput( |
| 188 | input: ComposerTraitsTarget, |
| 189 | ): TraitsSelectionInput | null { |
| 190 | const { threadRef, draftId, ...rest } = input; |
| 191 | const { selections } = resolveComposerOptionSelections( |
| 192 | input.models, |
| 193 | input.model, |
| 194 | input.provider, |
| 195 | input.modelOptions, |
| 196 | input.includePlanAgent, |
| 197 | ); |
| 198 | const persistence = threadRef ? { threadRef } : draftId ? { draftId } : null; |
| 199 | if (!persistence) return null; |
| 200 | return { ...rest, ...persistence, modelOptions: selections }; |
| 201 | } |