Metadata-Version: 2.5
Name: corfedo
Version: 0.0.2
Summary: Python client and AI tools for Corfedo code search.
Project-URL: Homepage, https://corfedo.ai
Project-URL: Documentation, https://docs.corfedo.ai/connect/sdks/python/
Project-URL: Repository, https://github.com/corfedo/corfedo-python-sdk
Project-URL: Changelog, https://github.com/corfedo/corfedo-python-sdk/blob/main/CHANGELOG.md
Author: Wojciech Wentland
License-Expression: Apache-2.0
License-File: LICENSE
License-File: NOTICE
License-File: contracts/LICENSE
License-File: contracts/NOTICE
Classifier: Development Status :: 3 - Alpha
Classifier: Framework :: AsyncIO
Classifier: Intended Audience :: Developers
Classifier: Programming Language :: Python :: 3
Classifier: Typing :: Typed
Requires-Python: >=3.11
Requires-Dist: authlib<1.10,>=1.9.1
Requires-Dist: cryptography>=44
Requires-Dist: httpx2<3,>=2.13
Requires-Dist: pydantic<3,>=2.11
Provides-Extra: langchain
Requires-Dist: langchain-core<2,>=1.6; extra == 'langchain'
Provides-Extra: pydantic-ai
Requires-Dist: pydantic-ai-slim<3,>=2.57; extra == 'pydantic-ai'
Description-Content-Type: text/markdown

<p align="center">
  <picture>
    <source media="(prefers-color-scheme: dark)" srcset="https://raw.githubusercontent.com/corfedo/corfedo-python-sdk/main/docs/images/corfedo-logo-dark.svg">
    <img src="https://raw.githubusercontent.com/corfedo/corfedo-python-sdk/main/docs/images/corfedo-logo-light.svg" alt="Corfedo" width="280">
  </picture>
</p>

<h1 align="center">Corfedo Python SDK</h1>

<p align="center">
  <a href="https://github.com/corfedo/corfedo-python-sdk/actions/workflows/ci.yml"><img src="https://github.com/corfedo/corfedo-python-sdk/actions/workflows/ci.yml/badge.svg" alt="CI"></a>
  <a href="https://pypi.org/project/corfedo/"><img src="https://img.shields.io/pypi/v/corfedo.svg" alt="PyPI"></a>
  <a href="https://pypi.org/project/corfedo/"><img src="https://img.shields.io/badge/python-3.11%2B-3776AB.svg" alt="Python 3.11+"></a>
  <a href="https://github.com/corfedo/corfedo-python-sdk/blob/main/LICENSE"><img src="https://img.shields.io/badge/license-Apache--2.0-blue.svg" alt="License: Apache-2.0"></a>
  <a href="https://docs.corfedo.ai/connect/sdks/python/"><img src="https://img.shields.io/badge/docs-docs.corfedo.ai-783B56.svg" alt="Documentation"></a>
</p>

<p align="center"><strong>Search and read code across every repository your Corfedo workspace indexes, and ask Corfedo AI, from Python.</strong></p>

[Corfedo](https://corfedo.ai) is a self-hosted code search and AI service. It indexes the repositories you choose
across your code hosts and answers searches and questions across them, for people and coding agents.
This repository holds the async Python client for its public HTTP API, with optional LangChain and Pydantic AI
integrations. Python 3.11 or later.

## Install

```sh
pip install corfedo                    # client and framework-neutral tools
pip install 'corfedo[pydantic-ai]'     # Pydantic AI toolset and model (corfedo.pydantic_ai)
pip install 'corfedo[langchain]'       # LangChain tools and chat model (corfedo.langchain)
```

## Quick start

```python
import os
from corfedo import CorfedoClient
from corfedo.models import SearchCode

async with CorfedoClient.from_api_key_file(os.environ["CORFEDO_API_KEY_FILE"]) as client:
    page = await client.search.search(SearchCode(query="where are search requests parsed"))
    for result in page.results:
        if result.kind == "file":
            print(f"{result.repository} {result.path} @{result.revision}")
```

Bare words return the files that contain them, the most relevant first. Text in double quotes
matches exactly and a regular expression between slashes matches a pattern; uppercase `AND`, `OR`
and `NOT` combine them, and filters such as `repo:`, `file:` and `lang:` narrow the search:

```python
SearchCode(query=r'"PlatformComposition" OR /Require\w+/ lang:csharp')
```

A person can sign in through the browser instead, for notebooks and local tools:

```python
from corfedo import CorfedoAuthentication, CorfedoClient, HumanAuthenticationOptions

credentials = await CorfedoAuthentication.sign_in(
    HumanAuthenticationOptions(api_base_uri="https://corfedo.example.com/")
)
async with CorfedoClient(credentials) as client:
    ...
```


With an AI framework, the application owns the model and the loop; Corfedo supplies the tools:

```python
from pydantic_ai import Agent
from corfedo.pydantic_ai import CorfedoToolset

agent = Agent(model, toolsets=[CorfedoToolset(client)])
```

`corfedo.langchain.get_corfedo_tools(client)` returns LangChain structured tools for
`create_agent`, and `corfedo.tools.CorfedoTools` is the framework-neutral layer both adapters use.
`corfedo.tools.INSTRUCTIONS` is optional text for the agent's system prompt: it explains that a run
of `*` in a credential is masked by the index.
Tools return exact JSON text; tool failures are `CorfedoToolError`s whose messages are safe to
show to the model. Other frameworks and provider SDKs use `CorfedoTools` directly: pass its
`definitions` to the model and its tool calls to `execute`. 

## Ask Corfedo AI

With an API key or sign-in that has the AI capability (`corfedo:ai:invoke`), `client.ai` sends
messages to Corfedo AI and streams its runs:

```python
from corfedo.models import SendAiMessage

async with client.ai.stream(SendAiMessage(text="How are shards selected?")) as run:
    async for event in run:
        if event.type == "text.delta":
            print(event.text, end="")
print(run.answer)
```

Streamed text is provisional; the answer comes from the run's completion. A dropped connection is
recovered inside the call. Corfedo AI is also a chat model for LangChain and Pydantic AI. The
conversation lives in Corfedo, so each call sends only the new message or the results of client
tools:

```python
from corfedo.langchain import ChatCorfedo
from corfedo.pydantic_ai import CorfedoModel, CorfedoModelSettings

answer = await ChatCorfedo(client=client).ainvoke("How are shards selected?")
agent = Agent(CorfedoModel(client), model_settings=CorfedoModelSettings(corfedo_tools=["search_code"]))
```

Sampling settings, system prompts and content other than text are rejected with
`CorfedoUnsupportedInputError`. The profile owns them.

More examples, including OpenAI Agents and Anthropic tool use, are in [`examples/`](https://github.com/corfedo/corfedo-python-sdk/tree/main/examples).

## Documentation

- [Python SDK guide](https://docs.corfedo.ai/connect/sdks/python/), [LangChain](https://docs.corfedo.ai/connect/frameworks/langchain/) and [Pydantic AI](https://docs.corfedo.ai/connect/frameworks/pydantic-ai/) on docs.corfedo.ai
- [API keys](https://github.com/corfedo/corfedo-python-sdk/blob/main/docs/api-keys.md) and [authentication](https://github.com/corfedo/corfedo-python-sdk/blob/main/docs/authentication.md)
- [Changelog](https://github.com/corfedo/corfedo-python-sdk/blob/main/CHANGELOG.md)

## Related repositories

- [corfedo-dotnet-sdk](https://github.com/corfedo/corfedo-dotnet-sdk): .NET client with Microsoft.Extensions.AI integration
- [corfedo-typescript-sdk](https://github.com/corfedo/corfedo-typescript-sdk): TypeScript client with Vercel AI SDK and LangChain.js integrations
- [corfedo-install](https://github.com/corfedo/corfedo-install): installer for self-hosted Corfedo on Docker or Kubernetes

## Development

```sh
uv sync --locked --all-extras
uv run python scripts/check_contract.py
uv run python scripts/check_version.py
uv run ruff check && uv run ruff format --check
uv run pyright
uv run pytest
uv build
uv run python tests/package/check_wheel.py
```

`python3 scripts/prepare_package.py` builds a preview package for testing before a release. Its version comes
from the sources, such as `0.0.1.dev0+s14275fd1c9c8e82a`, so the same sources always give the same version. The wheel,
the sdist and `manifest.json` go to `artifacts/packages/<version>/`. See [CONTRIBUTING.md](https://github.com/corfedo/corfedo-python-sdk/blob/main/CONTRIBUTING.md).

## License

Apache-2.0, see [LICENSE](https://github.com/corfedo/corfedo-python-sdk/blob/main/LICENSE) and [NOTICE](https://github.com/corfedo/corfedo-python-sdk/blob/main/NOTICE). The bundled API contract has its own notice in
[contracts/NOTICE](https://github.com/corfedo/corfedo-python-sdk/blob/main/contracts/NOTICE). Corfedo names, logos and brand assets are reserved and not covered by these
licenses.
