Skip to content

codenib.web.local

Prepare a local repository for the CodeNib Wiki runtime.

Functions:

Name Description
prepare_local_wiki

Write the registry and config consumed by the existing Wiki service.

prepare_local_wiki

prepare_local_wiki(
    repo_path: Path,
    manifest_path: Path,
    *,
    frontend_port: int,
    agent_wiki: bool = False,
    model: str | None = None,
    api_base: str | None = None,
    api_key_env: str | None = None,
    model_options: Mapping[str, Any] | None = None
) -> LocalWiki

Write the registry and config consumed by the existing Wiki service.

Source code in codenib/web/local.py
def prepare_local_wiki(
    repo_path: Path,
    manifest_path: Path,
    *,
    frontend_port: int,
    agent_wiki: bool = False,
    model: str | None = None,
    api_base: str | None = None,
    api_key_env: str | None = None,
    model_options: Mapping[str, Any] | None = None,
) -> LocalWiki:
    """Write the registry and config consumed by the existing Wiki service."""
    repo_path = repo_path.expanduser().resolve()
    manifest_path = manifest_path.expanduser().resolve()
    manifest = RepoManifest.load(str(manifest_path))
    _validate_checkout_identity(
        repo_path,
        manifest,
        artifact_root=manifest_path.parent,
    )

    data_dir = manifest_path.parent / "wiki"
    data_dir.mkdir(parents=True, exist_ok=True)
    repo_name = _repository_slug(repo_path)
    repo_id = _repo_id(repo_name.rsplit("/", 1)[-1])
    language = manifest.languages[0] if manifest.languages else "unknown"
    save_registry(
        str(data_dir / "qa_registry.json"),
        [
            RepoEntry(
                instance_id=repo_id,
                repo=repo_name,
                base_commit=manifest.commit,
                language=language,
                repo_dir=str(repo_path),
                manifest_path=str(manifest_path),
            )
        ],
    )

    mode = "hybrid" if manifest.index_is_current("vector") else "sparse"
    config_path = data_dir / "config.yaml"
    config = {
        "data_dir": str(data_dir),
        "mode": mode,
        "wiki_agent": agent_wiki,
        "cors_origins": [
            f"http://localhost:{frontend_port}",
            f"http://127.0.0.1:{frontend_port}",
        ],
    }
    if model:
        config["model"] = model
    if api_base:
        config["model_api_base"] = api_base
    options = validate_model_options(model_options)
    if options:
        config["model_options"] = options
    with config_path.open("w", encoding="utf-8") as handle:
        yaml.safe_dump(config, handle, sort_keys=True)

    runtime_env: dict[str, str] = {}
    if model:
        runtime_env["CODENIB_DEMO_MODEL"] = model
    if api_base:
        runtime_env["CODENIB_DEMO_API_BASE"] = api_base
    if options:
        runtime_env["CODENIB_DEMO_MODEL_OPTIONS"] = json.dumps(
            options,
            sort_keys=True,
            separators=(",", ":"),
        )
    if api_key_env:
        api_key = os.environ.get(api_key_env)
        if not api_key:
            raise ValueError(
                f"API key environment variable is unset or empty: {api_key_env}"
            )
        runtime_env["CODENIB_DEMO_API_KEY"] = api_key

    return LocalWiki(
        repo_path=repo_path,
        manifest_path=manifest_path,
        data_dir=data_dir,
        config_path=config_path,
        repo_id=repo_id,
        runtime_env=runtime_env,
    )