File size: 10,699 Bytes
261c237
e99df76
2415446
 
f7872b8
2415446
 
f7872b8
abe1c43
 
 
 
 
 
 
 
f7872b8
8e438ab
 
 
2415446
abe1c43
2415446
f7872b8
 
 
 
 
 
691dc7c
8e438ab
abe1c43
2415446
abe1c43
2415446
8e438ab
 
 
 
 
2415446
 
 
 
 
f7872b8
2415446
 
 
 
f7872b8
8e438ab
691dc7c
f7872b8
 
2415446
 
 
abe1c43
2415446
abe1c43
2415446
 
 
f7872b8
 
 
abe1c43
 
 
 
2415446
 
abe1c43
2415446
 
 
 
 
 
691dc7c
2415446
 
 
abe1c43
2415446
 
 
 
 
 
 
 
abe1c43
 
8e438ab
abe1c43
 
2415446
8e438ab
 
abe1c43
 
 
 
 
 
2415446
abe1c43
2415446
abe1c43
 
 
 
2415446
f7872b8
abe1c43
 
 
 
2415446
abe1c43
8e438ab
 
 
 
 
 
 
 
 
f7872b8
 
 
 
 
 
 
 
 
 
 
8e438ab
f7872b8
 
 
 
 
2415446
 
abe1c43
 
8e438ab
 
691dc7c
f7872b8
 
abe1c43
2415446
 
f7872b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
691dc7c
 
 
 
 
 
f7872b8
 
8e438ab
 
 
 
 
 
2415446
8e438ab
abe1c43
2415446
 
8e438ab
 
 
f7872b8
8e438ab
 
f7872b8
8e438ab
 
 
 
 
2415446
 
 
abe1c43
2415446
8e438ab
f7872b8
8e438ab
691dc7c
f7872b8
 
2415446
 
 
abe1c43
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8e438ab
 
 
 
 
 
abe1c43
 
 
 
 
 
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
from __future__ import annotations
"""Sparse managed-config validation, preview, and atomic persistence."""

from collections.abc import Mapping
from dataclasses import dataclass, field
from pathlib import Path

from free_claude_code.config.api_keys import ApiKeyList, api_key_values, first_api_key
from free_claude_code.config.env_files import (
    FCC_CONFIG_SCHEMA_ENV,
)
from free_claude_code.config.env_migrations import (
    CONFIG_SCHEMA_VERSION,
    recognized_env_keys,
    render_managed_config,
)
from free_claude_code.config.errors import ConfigConflictError
from free_claude_code.config.loader import ManagedConfigSnapshot
from free_claude_code.config.model_refs import normalize_retired_model_settings
from free_claude_code.config.provider_proxies import invalid_provider_proxy_keys
from free_claude_code.config.settings import Settings
from free_claude_code.core.json_types import JsonObject

from .credentials import (
    CredentialEditError,
    CredentialRowError,
    CredentialSubmission,
    resolve_credential_edit,
)
from .custom_providers import CustomProviderMutation, merge_custom_provider
from .manifest import FIELD_BY_KEY, FIELDS
from .state import ConfigInputValue
from .validation import settings_from_values
from .values import MASKED_SECRET, is_locked_source, load_value_state, normalize_for_env

_PROVIDER_PROXY_ERROR = (
    "must be a proxy URL with a supported scheme and host "
    "(for example http://127.0.0.1:8080 or socks5://127.0.0.1:1080)"
)


@dataclass(frozen=True, slots=True)
class PreparedAdminUpdate:
    """Validated Admin update ready for an atomic managed-file commit."""

    target_values: dict[str, str] = field(repr=False)
    settings: Settings | None
    errors: tuple[str, ...]
    pending_fields: tuple[str, ...]
    path: Path
    expected: ManagedConfigSnapshot = field(repr=False)
    changed_keys: tuple[str, ...] = ()
    custom_provider_id: str | None = None
    credential_submissions: tuple[CredentialSubmission, ...] = ()
    credential_row_errors: tuple[CredentialRowError, ...] = ()

    @property
    def valid(self) -> bool:
        return self.settings is not None and not self.errors

    def validation_response(self) -> JsonObject:
        return {
            "valid": self.valid,
            "errors": list(self.errors),
            "credential_row_errors": [
                error.public() for error in self.credential_row_errors
            ],
            "env_preview": render_managed_config(
                self.target_values,
                mask_secrets=True,
            ),
        }

    def applied_response(self) -> JsonObject:
        if not self.valid:
            return self.validation_response() | {
                "applied": False,
                "pending_fields": [],
            }
        return {
            "custom_provider_id": self.custom_provider_id,
            "applied": True,
            "valid": True,
            "errors": [],
            "env_preview": render_managed_config(
                self.target_values,
                mask_secrets=True,
            ),
            "path": str(self.path),
            "pending_fields": list(self.pending_fields),
        }


def target_values_with_updates(
    updates: Mapping[str, ConfigInputValue],
    snapshot: ManagedConfigSnapshot,
) -> dict[str, str]:
    """Return sparse managed state after applying valid partial-update semantics."""

    state = load_value_state(snapshot)
    values = dict(snapshot.managed)
    known = recognized_env_keys()
    for key in tuple(values):
        if key in known and key != FCC_CONFIG_SCHEMA_ENV and not values[key].strip():
            values.pop(key)

    for key, submitted in updates.items():
        field = FIELD_BY_KEY.get(key)
        if field is None or is_locked_source(state[key].source):
            continue
        if field.secret and (
            submitted == MASKED_SECRET
            or (isinstance(submitted, str) and not submitted.strip())
        ):
            continue
        value = normalize_for_env(submitted, key=key)
        if value is None or (field.nullable and not value):
            values.pop(key, None)
        else:
            values[key] = value

    values[FCC_CONFIG_SCHEMA_ENV] = CONFIG_SCHEMA_VERSION
    return normalize_retired_model_settings(values, preserve_empty_overrides=False)


def pending_restart_fields(active: Settings, prospective: Settings) -> tuple[str, ...]:
    """Compare saved intent with the settings actually used by running resources."""
    voice_credentials = {
        _active_voice_credential(active),
        _active_voice_credential(prospective),
    }
    pending = []
    for spec in FIELDS:
        if spec.settings_attr is None or not (
            spec.restart_required
            or spec.session_sensitive
            or spec.key in voice_credentials
        ):
            continue
        before, after = (
            getattr(active, spec.settings_attr),
            getattr(prospective, spec.settings_attr),
        )
        if spec.field_type == "api_key_list":
            before, after = first_api_key(before), first_api_key(after)
        if before != after:
            pending.append(spec.key)
    return tuple(pending)


def prepare_admin_update(
    updates: Mapping[str, ConfigInputValue],
    snapshot: ManagedConfigSnapshot,
    active_settings: Settings,
    custom_provider: CustomProviderMutation | None = None,
    *,
    revision_key: bytes,
) -> PreparedAdminUpdate:
    """Validate an update and construct its prospective Settings snapshot."""

    resolved = dict(updates)
    row_errors: list[CredentialRowError] = []
    credential_errors: list[str] = []
    submissions: list[CredentialSubmission] = []
    for key, submitted in updates.items():
        spec = FIELD_BY_KEY.get(key)
        if spec is None or spec.field_type != "api_key_list":
            continue
        assert spec.settings_attr is not None
        try:
            keys = resolve_credential_edit(
                key,
                getattr(snapshot.settings, spec.settings_attr),
                snapshot.sources[spec.settings_attr].value,
                submitted,
                revision_key,
                key=key,
            )
        except ConfigConflictError:
            raise
        except ValueError as exc:
            resolved.pop(key)
            credential_errors.append(str(exc))
            if isinstance(exc, CredentialEditError):
                row_errors.extend(exc.row_errors)
        else:
            resolved[key] = api_key_values(keys)
            submissions.append(CredentialSubmission(key, keys))

    custom_keys: ApiKeyList | None = None
    if (
        custom_provider is not None
        and custom_provider.action != "delete"
        and "api_keys" in custom_provider.values
    ):
        changes = dict(custom_provider.values)
        submitted = changes.pop("api_keys")
        definition = snapshot.settings.custom_provider(
            custom_provider.provider_id or ""
        )
        try:
            custom_keys = resolve_credential_edit(
                "custom:" + (custom_provider.provider_id or "new"),
                definition.api_keys if definition else (),
                snapshot.sources["custom_providers"].value,
                submitted,
                revision_key,
                key="api_keys",
                provider_id=custom_provider.provider_id,
                new_owner=custom_provider.action == "create",
            )
        except ConfigConflictError:
            raise
        except ValueError as exc:
            credential_errors.append(str(exc))
            if isinstance(exc, CredentialEditError):
                row_errors.extend(exc.row_errors)
        else:
            changes["api_keys"] = api_key_values(custom_keys)
        custom_provider = custom_provider.model_copy(update={"values": changes})

    update_errors = (*credential_errors, *_update_protocol_errors(resolved))
    target_values = target_values_with_updates(resolved, snapshot)
    custom_id = None
    if custom_provider is not None:
        try:
            custom_id = merge_custom_provider(target_values, snapshot, custom_provider)
        except ValueError as exc:
            update_errors = (*update_errors, str(exc))
        if custom_keys is not None:
            submissions.append(CredentialSubmission("api_keys", custom_keys, custom_id))
    settings, settings_errors = settings_from_values(target_values, snapshot.process)
    errors = (
        *update_errors,
        *settings_errors,
        *_provider_proxy_errors(target_values),
    )
    pending_fields = (
        pending_restart_fields(active_settings, settings)
        if settings is not None and not errors
        else ()
    )
    state = load_value_state(snapshot)
    changed_keys = tuple(
        key
        for key, submitted in resolved.items()
        if key in FIELD_BY_KEY
        and not is_locked_source(state[key].source)
        and (value := normalize_for_env(submitted, key=key))
        and value != MASKED_SECRET
        and value != state[key].value
        and key in target_values
        and target_values[key] == value
    )
    return PreparedAdminUpdate(
        target_values=target_values,
        settings=settings,
        errors=errors,
        pending_fields=pending_fields,
        path=snapshot.path,
        expected=snapshot,
        changed_keys=changed_keys,
        custom_provider_id=custom_id,
        credential_submissions=tuple(submissions),
        credential_row_errors=tuple(row_errors),
    )


def _update_protocol_errors(
    updates: Mapping[str, ConfigInputValue],
) -> tuple[str, ...]:
    errors: list[str] = []
    for key, submitted in updates.items():
        field = FIELD_BY_KEY.get(key)
        if field is None or (field.secret and submitted == MASKED_SECRET):
            continue
        if submitted is None and not field.nullable:
            errors.append(f"{key}: this setting cannot be removed")
            continue
        if (
            not field.secret
            and isinstance(submitted, str)
            and not submitted.strip()
            and not field.nullable
        ):
            errors.append(f"{key}: this setting cannot be blank")
    return tuple(errors)


def _provider_proxy_errors(values: Mapping[str, str]) -> tuple[str, ...]:
    return tuple(
        f"{key}: {_PROVIDER_PROXY_ERROR}" for key in invalid_provider_proxy_keys(values)
    )


def _active_voice_credential(settings: Settings) -> str | None:
    if not settings.voice_note_enabled:
        return None
    if settings.whisper_device == "nvidia_nim":
        return "NVIDIA_NIM_API_KEY"
    return "HUGGINGFACE_API_KEY"