claude-code-rust 0.12.1 → 0.12.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,228 @@
1
+ const OPUS_MODEL_ALIAS = "opus";
2
+ const MAX_MODEL_VERSION_PARTS = 2;
3
+ const RELEASE_BUILD_TOKEN = /^20\d{6}$/;
4
+ function isUnavailableModelId(id) {
5
+ const normalized = id.trim().toLowerCase();
6
+ // TODO: Revisit only after a product decision if Anthropic restores Fable 5 access.
7
+ return normalized === "fable" || normalized.startsWith("claude-fable-5");
8
+ }
9
+ function isEffortLevel(value) {
10
+ return (value === "low" ||
11
+ value === "medium" ||
12
+ value === "high" ||
13
+ value === "xhigh" ||
14
+ value === "max");
15
+ }
16
+ function normalizeModelKey(id) {
17
+ const original = id.trim();
18
+ if (!original) {
19
+ return { original, family: "unknown", versionParts: [], variantParts: [], buildParts: [] };
20
+ }
21
+ const lower = original.toLowerCase();
22
+ const contextMatch = lower.match(/\[([^\]]+)\]$/);
23
+ const contextSuffix = contextMatch?.[1];
24
+ const withoutContext = contextMatch ? lower.slice(0, contextMatch.index) : lower;
25
+ const withoutPrefix = withoutContext.startsWith("claude-")
26
+ ? withoutContext.slice("claude-".length)
27
+ : withoutContext;
28
+ const parts = withoutPrefix.split("-").filter((part) => part.length > 0);
29
+ const familyPart = parts[0] ?? "";
30
+ const family = familyPart === "opus" || familyPart === "sonnet" || familyPart === "haiku"
31
+ ? familyPart
32
+ : "unknown";
33
+ const versionParts = [];
34
+ const variantParts = [];
35
+ const buildParts = [];
36
+ if (family !== "unknown") {
37
+ for (const part of parts.slice(1)) {
38
+ if (/^\d+$/.test(part)) {
39
+ if (versionParts.length < MAX_MODEL_VERSION_PARTS) {
40
+ const parsed = Number.parseInt(part, 10);
41
+ if (Number.isFinite(parsed)) {
42
+ versionParts.push(parsed);
43
+ }
44
+ continue;
45
+ }
46
+ if (RELEASE_BUILD_TOKEN.test(part)) {
47
+ buildParts.push(part);
48
+ continue;
49
+ }
50
+ }
51
+ variantParts.push(part);
52
+ }
53
+ }
54
+ return {
55
+ original,
56
+ family,
57
+ versionParts,
58
+ variantParts,
59
+ buildParts,
60
+ ...(contextSuffix ? { contextSuffix } : {}),
61
+ };
62
+ }
63
+ function modelKeysAreCompatible(leftId, rightId) {
64
+ const left = normalizeModelKey(leftId);
65
+ const right = normalizeModelKey(rightId);
66
+ if (left.family === "unknown" || right.family === "unknown") {
67
+ return left.original.toLowerCase() === right.original.toLowerCase();
68
+ }
69
+ if (left.family !== right.family) {
70
+ return false;
71
+ }
72
+ if (left.variantParts.join(".") !== right.variantParts.join(".")) {
73
+ return false;
74
+ }
75
+ if (left.versionParts.length === 0 || right.versionParts.length === 0) {
76
+ return true;
77
+ }
78
+ return left.versionParts.join(".") === right.versionParts.join(".");
79
+ }
80
+ function sameContextSuffix(leftId, rightId) {
81
+ const left = normalizeModelKey(leftId);
82
+ const right = normalizeModelKey(rightId);
83
+ return (left.contextSuffix?.toLowerCase() ?? "") === (right.contextSuffix?.toLowerCase() ?? "");
84
+ }
85
+ function sameFamilyAndVersion(leftId, rightId) {
86
+ const left = normalizeModelKey(leftId);
87
+ const right = normalizeModelKey(rightId);
88
+ if (left.family === "unknown" || right.family === "unknown") {
89
+ return left.original.toLowerCase() === right.original.toLowerCase();
90
+ }
91
+ if (left.family !== right.family) {
92
+ return false;
93
+ }
94
+ if (left.versionParts.length === 0 || right.versionParts.length === 0) {
95
+ return left.versionParts.length === right.versionParts.length;
96
+ }
97
+ return left.versionParts.join(".") === right.versionParts.join(".");
98
+ }
99
+ function hasVariantSiblingConflict(availableModels, candidateId, resolvedId) {
100
+ if (sameContextSuffix(candidateId, resolvedId)) {
101
+ return false;
102
+ }
103
+ const resolvedContext = normalizeModelKey(resolvedId).contextSuffix?.toLowerCase() ?? "";
104
+ if (!resolvedContext) {
105
+ return false;
106
+ }
107
+ return availableModels.some((entry) => {
108
+ if (entry.id === candidateId) {
109
+ return false;
110
+ }
111
+ if (!sameFamilyAndVersion(entry.id, resolvedId)) {
112
+ return false;
113
+ }
114
+ const entryContext = normalizeModelKey(entry.id).contextSuffix?.toLowerCase() ?? "";
115
+ return entryContext === resolvedContext;
116
+ });
117
+ }
118
+ function humanizeModelId(id) {
119
+ const normalized = normalizeModelKey(id);
120
+ if (normalized.family === "unknown") {
121
+ return id;
122
+ }
123
+ const familyLabel = normalized.family === "opus"
124
+ ? "Opus"
125
+ : normalized.family === "sonnet"
126
+ ? "Sonnet"
127
+ : "Haiku";
128
+ const versionLabel = normalized.versionParts.length > 0 ? ` ${normalized.versionParts.join(".")}` : "";
129
+ const contextLabel = normalized.contextSuffix?.toLowerCase() === "1m"
130
+ ? " [1M]"
131
+ : normalized.contextSuffix
132
+ ? ` [${normalized.contextSuffix}]`
133
+ : "";
134
+ return `${familyLabel}${versionLabel}${contextLabel}`;
135
+ }
136
+ function shortDisplayNameForModelId(id) {
137
+ return humanizeModelId(id);
138
+ }
139
+ function currentModelIsAuthoritative(resolvedId, requestedId) {
140
+ const resolved = resolvedId.trim();
141
+ if (!resolved || resolved === "Connecting...") {
142
+ return Boolean(requestedId?.trim());
143
+ }
144
+ return true;
145
+ }
146
+ function resolveCatalogModel(availableModels, resolvedId, requestedId) {
147
+ const exactResolved = availableModels.find((entry) => entry.id === resolvedId);
148
+ if (exactResolved) {
149
+ return exactResolved;
150
+ }
151
+ if (requestedId) {
152
+ const exactRequested = availableModels.find((entry) => entry.id === requestedId);
153
+ if (exactRequested &&
154
+ modelKeysAreCompatible(exactRequested.id, resolvedId) &&
155
+ !hasVariantSiblingConflict(availableModels, exactRequested.id, resolvedId)) {
156
+ return exactRequested;
157
+ }
158
+ }
159
+ const compatible = availableModels.filter((entry) => modelKeysAreCompatible(entry.id, resolvedId) &&
160
+ !hasVariantSiblingConflict(availableModels, entry.id, resolvedId));
161
+ return compatible.length === 1 ? compatible[0] : undefined;
162
+ }
163
+ export function mapAvailableModels(models) {
164
+ if (!Array.isArray(models)) {
165
+ return [];
166
+ }
167
+ return models
168
+ .filter((entry) => {
169
+ return (typeof entry?.value === "string" &&
170
+ entry.value.trim().length > 0 &&
171
+ !isUnavailableModelId(entry.value) &&
172
+ typeof entry.displayName === "string" &&
173
+ entry.displayName.trim().length > 0);
174
+ })
175
+ .map((entry) => ({
176
+ id: entry.value,
177
+ display_name: entry.displayName,
178
+ supports_effort: entry.supportsEffort === true,
179
+ supported_effort_levels: Array.isArray(entry.supportedEffortLevels)
180
+ ? entry.supportedEffortLevels.filter(isEffortLevel)
181
+ : [],
182
+ ...(typeof entry.supportsAdaptiveThinking === "boolean"
183
+ ? { supports_adaptive_thinking: entry.supportsAdaptiveThinking }
184
+ : {}),
185
+ ...(typeof entry.supportsFastMode === "boolean"
186
+ ? { supports_fast_mode: entry.supportsFastMode }
187
+ : {}),
188
+ ...(typeof entry.supportsAutoMode === "boolean"
189
+ ? { supports_auto_mode: entry.supportsAutoMode }
190
+ : {}),
191
+ ...(typeof entry.description === "string" && entry.description.trim().length > 0
192
+ ? { description: entry.description }
193
+ : {}),
194
+ }));
195
+ }
196
+ export function resolveCurrentModel(session) {
197
+ const requestedId = session.requestedModelId?.trim() || undefined;
198
+ const resolvedId = session.resolvedRuntimeModelId?.trim() ||
199
+ session.model.trim() ||
200
+ requestedId ||
201
+ OPUS_MODEL_ALIAS;
202
+ const catalogModel = resolveCatalogModel(session.availableModels, resolvedId, requestedId);
203
+ const runtimeDisplayId = resolvedId || requestedId || OPUS_MODEL_ALIAS;
204
+ const displayNameShort = shortDisplayNameForModelId(runtimeDisplayId);
205
+ const displayNameLong = catalogModel?.display_name ?? humanizeModelId(runtimeDisplayId);
206
+ return {
207
+ resolved_id: resolvedId,
208
+ display_name_short: displayNameShort,
209
+ display_name_long: displayNameLong,
210
+ supports_effort: catalogModel?.supports_effort === true,
211
+ supported_effort_levels: catalogModel?.supported_effort_levels ?? [],
212
+ is_authoritative: currentModelIsAuthoritative(resolvedId, requestedId),
213
+ ...(requestedId ? { requested_id: requestedId } : {}),
214
+ ...(catalogModel ? { catalog_id: catalogModel.id } : {}),
215
+ ...(catalogModel?.supports_fast_mode !== undefined
216
+ ? { supports_fast_mode: catalogModel.supports_fast_mode }
217
+ : {}),
218
+ ...(catalogModel?.supports_auto_mode !== undefined
219
+ ? { supports_auto_mode: catalogModel.supports_auto_mode }
220
+ : {}),
221
+ ...(catalogModel?.supports_adaptive_thinking !== undefined
222
+ ? { supports_adaptive_thinking: catalogModel.supports_adaptive_thinking }
223
+ : {}),
224
+ };
225
+ }
226
+ export function currentModelsEqual(left, right) {
227
+ return JSON.stringify(left) === JSON.stringify(right);
228
+ }