Files
hapi/ios/Packages/HapiKit/Sources/HapiProtocol/Catalog/ModelCatalog.swift
T
3f8686e623 feat(claude): offer the four current Claude models, drop the 1M presets (#2153)
* fix(claude): drop the obsolete 1M model presets

The opus, sonnet and fable aliases now resolve to models with a native 1M
window (Opus 5.5, Sonnet 5.5, Fable 5.1), so Claude Code documents the
"[1m]" suffix as having no effect on them and its own /model picker no
longer lists 1M rows. Remove Sonnet 1M / Opus 1M / Fable 1M from the
shared, Android and iOS catalogs.

Sessions stored with a "[1m]" value keep working: the composer keeps
the current model selectable, and the context-budget fallback treats any
"[1m]" model and the remaining aliases as 1M, so local-mode sessions on
plain opus/sonnet no longer show a 200k budget.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(cli): seed a 1M turn-1 context window for bare Claude aliases

With the 1M presets gone, a session on plain fable/opus/sonnet must not
seed 200k before its first result: those aliases now resolve to models
with a native 1M window. Seed 1M when the selected model is a preset
alias; an explicit full id still seeds conservatively until its result
arrives.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(claude): offer the four current Claude models as presets

The Claude presets now mirror the aliases Claude Code's /model lists, in
its order: Opus, Fable, Sonnet, Haiku. Each alias resolves to the newest
model of its family, and all four have a native 1M window, so neither
1M variants nor pinned older versions are offered. Haiku was missing.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* feat(claude): read the effective context window from get_context_usage

The 1M seed for the aliases is wrong for users who disable 1M
(CLAUDE_CODE_DISABLE_1M_CONTEXT) or cap it with autoCompactWindow. In
remote mode, ask Claude via the get_context_usage control request after
each system/init and use its maxTokens, which is what /context shows.
A result's raw contextWindow no longer overwrites that value. Older CLIs
that reject the request keep the existing seed.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
Co-authored-by: weishu <twsxtd@gmail.com>
2026-10-29 22:28:59 +08:00

134 lines
4.8 KiB
Swift

import Foundation
// Static model / effort catalogs — data port of `shared/src/models.ts`
// (`CLAUDE_MODEL_LABELS`) and `shared/src/effort.ts` (`CLAUDE_EFFORT_LABELS`),
// plus the composer option-list builders from
// `web/src/components/AssistantChat/claudeModelOptions.ts` /
// `claudeEffortOptions.ts` (mirroring the Android port's
// `core/protocol/.../catalog/Models.kt`). Codex-family model lists are NOT
// static — they come from `GET /api/sessions/:id/codex-models` per session.
/// `CLAUDE_MODEL_LABELS` / `CLAUDE_MODEL_PRESETS`.
public enum ClaudeModels {
/// Preset ids in declaration (picker) order.
public static let presets: [String] = [
"opus",
"fable",
"sonnet",
"haiku",
]
private static let labels: [String: String] = [
"opus": "Opus",
"fable": "Fable",
"sonnet": "Sonnet",
"haiku": "Haiku",
]
/// `getClaudeModelLabel`: trimmed lookup; unknown/blank → nil.
public static func label(for model: String?) -> String? {
let trimmed = model?.trimmingCharacters(in: .whitespaces) ?? ""
guard !trimmed.isEmpty else { return nil }
return labels[trimmed]
}
}
/// `CLAUDE_EFFORT_LABELS` / `CLAUDE_EFFORT_LEVELS`.
public enum ClaudeEfforts {
/// Levels in ascending order.
public static let levels: [String] = ["low", "medium", "high", "xhigh", "max"]
private static let labels: [String: String] = [
"low": "Low",
"medium": "Medium",
"high": "High",
"xhigh": "XHigh",
"max": "Max",
]
public static func label(for level: String) -> String? {
labels[level]
}
}
/// One picker row: `value == nil` means "clear back to the agent default"
/// (wire `model: null` / `effort: null`).
public struct CatalogOption: Equatable, Hashable, Sendable {
public let value: String?
public let label: String
public init(value: String?, label: String) {
self.value = value
self.label = label
}
}
/// Option-list builders for the session config sheet.
public enum ModelCatalog {
/// `normalizeClaudeComposerModel`: `auto`/`default`/blank collapse to nil
/// (the Default row).
public static func normalizeClaudeModel(_ model: String?) -> String? {
let trimmed = model?.trimmingCharacters(in: .whitespaces) ?? ""
if trimmed.isEmpty || trimmed == "auto" || trimmed == "default" {
return nil
}
return trimmed
}
/// `getClaudeComposerModelOptions`: Default first, then a synthetic row
/// for a current model outside the preset list, then the presets.
public static func claudeModelOptions(currentModel: String?) -> [CatalogOption] {
let normalized = normalizeClaudeModel(currentModel)
var options = [CatalogOption(value: nil, label: "Default")]
if let normalized, !ClaudeModels.presets.contains(normalized) {
options.append(CatalogOption(
value: normalized,
label: ClaudeModels.label(for: normalized) ?? normalized
))
}
for preset in ClaudeModels.presets {
options.append(CatalogOption(
value: preset,
label: ClaudeModels.label(for: preset) ?? preset
))
}
return options
}
/// `normalizeClaudeComposerEffort`: lowercased; `auto`/`default`/blank → nil.
public static func normalizeClaudeEffort(_ effort: String?) -> String? {
let trimmed = effort?.trimmingCharacters(in: .whitespaces).lowercased() ?? ""
if trimmed.isEmpty || trimmed == "auto" || trimmed == "default" {
return nil
}
return trimmed
}
/// `getClaudeComposerEffortOptions`: Auto first, synthetic current, then
/// the levels.
public static func claudeEffortOptions(currentEffort: String?) -> [CatalogOption] {
let normalized = normalizeClaudeEffort(currentEffort)
var options = [CatalogOption(value: nil, label: "Auto")]
if let normalized, !ClaudeEfforts.levels.contains(normalized) {
options.append(CatalogOption(
value: normalized,
label: ClaudeEfforts.label(for: normalized) ?? capitalizedFirst(normalized)
))
}
for level in ClaudeEfforts.levels {
options.append(CatalogOption(
value: level,
label: ClaudeEfforts.label(for: level) ?? capitalizedFirst(level)
))
}
return options
}
/// Kotlin `replaceFirstChar { uppercase }` twin, shared with the codex
/// effort rows built from wire level ids.
public static func capitalizedFirst(_ value: String) -> String {
guard let first = value.first else { return value }
return first.uppercased() + value.dropFirst()
}
}