forked from Gitlawb/openclaude
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodel.tsx
More file actions
332 lines (328 loc) · 11.9 KB
/
model.tsx
File metadata and controls
332 lines (328 loc) · 11.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import { c as _c } from "react-compiler-runtime";
import chalk from 'chalk';
import * as React from 'react';
import type { CommandResultDisplay } from '../../commands.js';
import { ModelPicker } from '../../components/ModelPicker.js';
import { COMMON_HELP_ARGS, COMMON_INFO_ARGS } from '../../constants/xml.js';
import { fetchBootstrapData } from '../../services/api/bootstrap.js';
import { type AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS, logEvent } from '../../services/analytics/index.js';
import { useAppState, useSetAppState } from '../../state/AppState.js';
import type { LocalJSXCommandCall } from '../../types/command.js';
import type { EffortLevel } from '../../utils/effort.js';
import { isBilledAsExtraUsage } from '../../utils/extraUsage.js';
import { clearFastModeCooldown, isFastModeAvailable, isFastModeEnabled, isFastModeSupportedByModel } from '../../utils/fastMode.js';
import { MODEL_ALIASES } from '../../utils/model/aliases.js';
import { checkOpus1mAccess, checkSonnet1mAccess } from '../../utils/model/check1mAccess.js';
import type { ModelOption } from '../../utils/model/modelOptions.js';
import { discoverOpenAICompatibleModelOptions } from '../../utils/model/openaiModelDiscovery.js';
import { getAPIProvider } from '../../utils/model/providers.js';
import { getActiveOpenAIModelOptionsCache, setActiveOpenAIModelOptionsCache } from '../../utils/providerProfiles.js';
import { getDefaultMainLoopModelSetting, isOpus1mMergeEnabled, renderDefaultModelSetting } from '../../utils/model/model.js';
import { isModelAllowed } from '../../utils/model/modelAllowlist.js';
import { validateModel } from '../../utils/model/validateModel.js';
import { getAdditionalModelOptionsCacheScope } from '../../services/api/providerConfig.js';
function ModelPickerWrapper(t0) {
const $ = _c(17);
const {
onDone
} = t0;
const mainLoopModel = useAppState(_temp);
const mainLoopModelForSession = useAppState(_temp2);
const isFastMode = useAppState(_temp3);
const setAppState = useSetAppState();
let t1;
if ($[0] !== mainLoopModel || $[1] !== onDone) {
t1 = function handleCancel() {
logEvent("tengu_model_command_menu", {
action: "cancel" as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS
});
const displayModel = renderModelLabel(mainLoopModel);
onDone(`Kept model as ${chalk.bold(displayModel)}`, {
display: "system"
});
};
$[0] = mainLoopModel;
$[1] = onDone;
$[2] = t1;
} else {
t1 = $[2];
}
const handleCancel = t1;
let t2;
if ($[3] !== isFastMode || $[4] !== mainLoopModel || $[5] !== onDone || $[6] !== setAppState) {
t2 = function handleSelect(model, effort) {
logEvent("tengu_model_command_menu", {
action: model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
from_model: mainLoopModel as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS,
to_model: model as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS
});
setAppState(prev => ({
...prev,
mainLoopModel: model,
mainLoopModelForSession: null
}));
let message = `Set model to ${chalk.bold(renderModelLabel(model))}`;
if (effort !== undefined) {
message = message + ` with ${chalk.bold(effort)} effort`;
}
let wasFastModeToggledOn = undefined;
if (isFastModeEnabled()) {
clearFastModeCooldown();
if (!isFastModeSupportedByModel(model) && isFastMode) {
setAppState(_temp4);
wasFastModeToggledOn = false;
} else {
if (isFastModeSupportedByModel(model) && isFastModeAvailable() && isFastMode) {
message = message + " \xB7 Fast mode ON";
wasFastModeToggledOn = true;
}
}
}
if (isBilledAsExtraUsage(model, wasFastModeToggledOn === true, isOpus1mMergeEnabled())) {
message = message + " \xB7 Billed as extra usage";
}
if (wasFastModeToggledOn === false) {
message = message + " \xB7 Fast mode OFF";
}
onDone(message);
};
$[3] = isFastMode;
$[4] = mainLoopModel;
$[5] = onDone;
$[6] = setAppState;
$[7] = t2;
} else {
t2 = $[7];
}
const handleSelect = t2;
let t3;
if ($[8] !== isFastMode || $[9] !== mainLoopModel) {
t3 = isFastModeEnabled() && isFastMode && isFastModeSupportedByModel(mainLoopModel) && isFastModeAvailable();
$[8] = isFastMode;
$[9] = mainLoopModel;
$[10] = t3;
} else {
t3 = $[10];
}
let t4;
if ($[11] !== handleCancel || $[12] !== handleSelect || $[13] !== mainLoopModel || $[14] !== mainLoopModelForSession || $[15] !== t3) {
t4 = <ModelPicker initial={mainLoopModel} sessionModel={mainLoopModelForSession} onSelect={handleSelect} onCancel={handleCancel} isStandaloneCommand={true} showFastModeNotice={t3} />;
$[11] = handleCancel;
$[12] = handleSelect;
$[13] = mainLoopModel;
$[14] = mainLoopModelForSession;
$[15] = t3;
$[16] = t4;
} else {
t4 = $[16];
}
return t4;
}
function _temp4(prev_0) {
return {
...prev_0,
fastMode: false
};
}
function _temp3(s_1) {
return s_1.fastMode;
}
function _temp2(s_0) {
return s_0.mainLoopModelForSession;
}
function _temp(s) {
return s.mainLoopModel;
}
function SetModelAndClose({
args,
onDone
}: {
args: string;
onDone: (result?: string, options?: {
display?: CommandResultDisplay;
}) => void;
}): React.ReactNode {
const isFastMode = useAppState(s => s.fastMode);
const setAppState = useSetAppState();
const model = args === 'default' ? null : args;
React.useEffect(() => {
async function handleModelChange(): Promise<void> {
if (model && !isModelAllowed(model)) {
onDone(`Model '${model}' is not available. Your organization restricts model selection.`, {
display: 'system'
});
return;
}
// @[MODEL LAUNCH]: Update check for 1M access.
if (model && isOpus1mUnavailable(model)) {
onDone(`Opus 4.6 with 1M context is not available for your account. Learn more: https://code.claude.com/docs/en/model-config#extended-context-with-1m`, {
display: 'system'
});
return;
}
if (model && isSonnet1mUnavailable(model)) {
onDone(`Sonnet 4.6 with 1M context is not available for your account. Learn more: https://code.claude.com/docs/en/model-config#extended-context-with-1m`, {
display: 'system'
});
return;
}
// Skip validation for default model
if (!model) {
setModel(null);
return;
}
// Skip validation for known aliases - they're predefined and should work
if (isKnownAlias(model)) {
setModel(model);
return;
}
// Validate and set custom model
try {
// Don't use parseUserSpecifiedModel for non-aliases since it lowercases the input
// and model names are case-sensitive
const {
valid,
error: error_0
} = await validateModel(model);
if (valid) {
setModel(model);
} else {
onDone(error_0 || `Model '${model}' not found`, {
display: 'system'
});
}
} catch (error) {
onDone(`Failed to validate model: ${(error as Error).message}`, {
display: 'system'
});
}
}
function setModel(modelValue: string | null): void {
setAppState(prev => ({
...prev,
mainLoopModel: modelValue,
mainLoopModelForSession: null
}));
let message = `Set model to ${chalk.bold(renderModelLabel(modelValue))}`;
let wasFastModeToggledOn = undefined;
if (isFastModeEnabled()) {
clearFastModeCooldown();
if (!isFastModeSupportedByModel(modelValue) && isFastMode) {
setAppState(prev_0 => ({
...prev_0,
fastMode: false
}));
wasFastModeToggledOn = false;
// Do not update fast mode in settings since this is an automatic downgrade
} else if (isFastModeSupportedByModel(modelValue) && isFastMode) {
message += ` · Fast mode ON`;
wasFastModeToggledOn = true;
}
}
if (isBilledAsExtraUsage(modelValue, wasFastModeToggledOn === true, isOpus1mMergeEnabled())) {
message += ` · Billed as extra usage`;
}
if (wasFastModeToggledOn === false) {
// Fast mode was toggled off, show suffix after extra usage billing
message += ` · Fast mode OFF`;
}
onDone(message);
}
void handleModelChange();
}, [model, onDone, setAppState]);
return null;
}
function isKnownAlias(model: string): boolean {
return (MODEL_ALIASES as readonly string[]).includes(model.toLowerCase().trim());
}
function isOpus1mUnavailable(model: string): boolean {
const m = model.toLowerCase();
return !checkOpus1mAccess() && !isOpus1mMergeEnabled() && m.includes('opus') && m.includes('[1m]');
}
function isSonnet1mUnavailable(model: string): boolean {
const m = model.toLowerCase();
// Warn about Sonnet and Sonnet 4.6, but not Sonnet 4.5 since that had
// a different access criteria.
return !checkSonnet1mAccess() && (m.includes('sonnet[1m]') || m.includes('sonnet-4-6[1m]'));
}
function ShowModelAndClose(t0) {
const {
onDone
} = t0;
const mainLoopModel = useAppState(_temp7);
const mainLoopModelForSession = useAppState(_temp8);
const effortValue = useAppState(_temp9);
const displayModel = renderModelLabel(mainLoopModel);
const effortInfo = effortValue !== undefined ? ` (effort: ${effortValue})` : "";
if (mainLoopModelForSession) {
onDone(`Current model: ${chalk.bold(renderModelLabel(mainLoopModelForSession))} (session override from plan mode)\nBase model: ${displayModel}${effortInfo}`);
} else {
onDone(`Current model: ${displayModel}${effortInfo}`);
}
return null;
}
function _temp9(s_1) {
return s_1.effortValue;
}
function _temp8(s_0) {
return s_0.mainLoopModelForSession;
}
function _temp7(s) {
return s.mainLoopModel;
}
function haveSameModelOptions(left: ModelOption[], right: ModelOption[]): boolean {
if (left.length !== right.length) {
return false;
}
return left.every((option, index) => {
const other = right[index];
return other !== undefined && option.value === other.value && option.label === other.label && option.description === other.description && option.descriptionForModel === other.descriptionForModel;
});
}
async function refreshOpenAIModelOptionsCache(): Promise<void> {
if (getAPIProvider() !== 'openai') {
return;
}
try {
const discoveredOptions = await discoverOpenAICompatibleModelOptions();
if (discoveredOptions.length === 0) {
return;
}
const currentOptions = getActiveOpenAIModelOptionsCache();
if (haveSameModelOptions(currentOptions, discoveredOptions)) {
return;
}
setActiveOpenAIModelOptionsCache(discoveredOptions);
} catch {
// Keep /model usable even if endpoint discovery fails.
}
}
export const call: LocalJSXCommandCall = async (onDone, _context, args) => {
args = args?.trim() || '';
if (COMMON_INFO_ARGS.includes(args)) {
logEvent('tengu_model_command_inline_help', {
args: args as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS
});
return <ShowModelAndClose onDone={onDone} />;
}
if (COMMON_HELP_ARGS.includes(args)) {
onDone('Run /model to open the model selection menu, or /model [modelName] to set the model.', {
display: 'system'
});
return;
}
if (args) {
logEvent('tengu_model_command_inline', {
args: args as AnalyticsMetadata_I_VERIFIED_THIS_IS_NOT_CODE_OR_FILEPATHS
});
return <SetModelAndClose args={args} onDone={onDone} />;
}
if (getAdditionalModelOptionsCacheScope()?.startsWith('openai:')) {
void refreshOpenAIModelOptionsCache();
}
return <ModelPickerWrapper onDone={onDone} />;
};
function renderModelLabel(model: string | null): string {
const rendered = renderDefaultModelSetting(model ?? getDefaultMainLoopModelSetting());
return model === null ? `${rendered} (default)` : rendered;
}