Spaces:
Running
Running
File size: 3,834 Bytes
3934d27 e99df76 3934d27 691dc7c 3934d27 8e438ab 3934d27 691dc7c 3934d27 691dc7c 3934d27 8e438ab 3934d27 691dc7c 3934d27 2e7b8ca 3934d27 691dc7c 3934d27 691dc7c 3934d27 691dc7c 3934d27 691dc7c 3934d27 8e438ab 691dc7c 8e438ab 691dc7c 8e438ab 691dc7c 8e438ab 691dc7c 8e438ab | 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 | from __future__ import annotations
"""Process-local OpenCode v2 configuration for FCC model routing."""
from dataclasses import dataclass
from free_claude_code.application.model_catalog import (
CatalogModel,
context_window_for_client,
)
from free_claude_code.config.server_urls import proxy_v1_url
from free_claude_code.core.json_types import JsonObject
from free_claude_code.core.model_capabilities import ModelInputModality
OPENCODE_API_KEY_ENV = "FCC_OPENCODE_API_KEY"
OPENCODE_PROVIDER_ID = "free-claude-code"
@dataclass(frozen=True, slots=True)
class OpenCodeConfig:
"""Secret-free file and overlay configuration for one OpenCode process."""
file: JsonObject
overlay: JsonObject
def build_opencode_config(
models: tuple[CatalogModel, ...], *, default_model_id: str, proxy_root_url: str
) -> OpenCodeConfig:
"""Translate a non-empty FCC model snapshot into OpenCode v2 config."""
if not models:
raise ValueError("OpenCode requires at least one routable FCC model")
model_config: JsonObject = {
model.wire_slug: _model_config(model) for model in models
}
provider_config: JsonObject = {
"name": "Free Claude Code",
"package": "@opencode/ai/providers/openai/responses",
"settings": {
"baseURL": proxy_v1_url(proxy_root_url),
"apiKey": f"{{env:{OPENCODE_API_KEY_ENV}}}",
"headerTimeout": False,
"chunkTimeout": False,
"timeout": False,
},
}
default_model = f"{OPENCODE_PROVIDER_ID}/{default_model_id}"
# OpenCode's buffer is global, so it must also fit the smallest selectable
# model. Leave its native 20K default alone when every known window fits it.
buffer = min(
(
model.context_window_tokens // 4
for model in models
if model.context_window_tokens is not None
),
default=20000,
)
compaction: JsonObject = (
{"compaction": {"buffer": buffer}} if buffer < 20000 else {}
)
return OpenCodeConfig(
file={
"providers": {
OPENCODE_PROVIDER_ID: {
**provider_config,
"models": model_config,
}
}
},
overlay={
**compaction,
"providers": {OPENCODE_PROVIDER_ID: provider_config},
"experimental": {
"policies": [
{"action": "provider.use", "resource": "*", "effect": "deny"},
{
"action": "provider.use",
"resource": OPENCODE_PROVIDER_ID,
"effect": "allow",
},
]
},
"model": default_model,
"agents": {"title": {"model": default_model}},
},
)
def _model_config(model: CatalogModel) -> JsonObject:
config: JsonObject = {"name": model.display_name}
if model.input_modalities is not None:
config["capabilities"] = {
"tools": True,
"input": [
modality.value
for modality in ModelInputModality
if modality in model.input_modalities
],
"output": ["text"],
}
limits: JsonObject = {}
limits["context"] = context_window_for_client(model)
if model.max_output_tokens is not None:
limits["output"] = model.max_output_tokens
elif model.context_window_tokens is not None:
# An omitted output limit inherits 32K in OpenCode, which can consume
# the entire known window. This fallback is an OpenCode runtime budget.
limits["output"] = max(1, min(4096, model.context_window_tokens // 4))
if limits:
config["limit"] = limits
return config
|