Metadata-Version: 2.4
Name: codex-python-kagent
Version: 0.1.8
Summary: Async Codex-backed coding agent SDK.
Author: Federico Goiriz
License: MIT License
        
        Copyright (c) 2026 Federico Goiriz
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: agent,codex,coding-agent,sdk
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.12
Classifier: Programming Language :: Python :: 3.13
Classifier: Typing :: Typed
Requires-Python: >=3.12
Description-Content-Type: text/markdown

# kagent

Async Python SDK for running Codex-backed coding-agent tasks with a small, memorable thread API.

The PyPI distribution is `codex-python-kagent`; the import package is `kagent`.

```python
import asyncio

from kagent import kagent


async def main() -> None:
    agent = kagent()
    run = await agent.thread("main").run("Create hello.txt with exactly: hello")
    print(run.text)


asyncio.run(main())
```

## Status

`kagent` is experimental. It wraps the local Codex CLI/app-server flow and currently depends on
the experimental OpenAI Codex Python SDK for `runner="sdk"`.

The package itself has no required runtime dependencies so it can be built and published cleanly.
The default runner uses the Codex CLI. The experimental Codex Python SDK is kept in a local uv
dependency group instead of PyPI metadata.

## Install

In a uv project:

```bash
uv add codex-python-kagent
kagent login
```

For API-key auth instead of ChatGPT subscription auth:

```bash
export OPENAI_API_KEY="sk-..."
kagent login --api-key-env OPENAI_API_KEY
```

For local development in this repo:

```bash
uv sync --group dev --group codex --group examples
```

The local machine must also have the Codex CLI installed and authenticated:

```bash
codex login status
```

## API

Use `kagent(...)` when you already authenticated with `kagent login` or `codex login`.

```python
from kagent import kagent

agent = kagent(workspace=".", model="gpt-5.5")
thread = agent.thread("refactor-auth")
await thread.run("Refactor auth.py without changing behavior.")
await thread.run("Now run the tests and fix any failures.")
```

Create a fresh Codex-named thread by omitting the id:

```python
thread = agent.thread()
await thread.run("Create hello.txt with exactly: hello")
print(thread.id)
```

Stream events with `async for` while still getting the final result:

```python
stream = agent.thread("refactor-auth").stream("Run the tests and fix failures.")

async for event in stream:
    print(event.get("type") or event.get("method"))

print(stream.result.text)
print(stream.events)
```

Or trigger Codex auth from Python after configuring the agent:

```python
import os

from kagent import kagent

agent = kagent(model="gpt-5.5").login()
api_agent = kagent(model="gpt-5.5").login(api_key=os.environ["OPENAI_API_KEY"])
```

Options:

```python
kagent(
    workspace=".",
    model="gpt-5.5",
    runner="cli",
    ask_for_approval="never",
    sandbox="danger-full-access",
    skip_git_repo_check=True,
    yolo=True,
    codex_bin=None,
    store_path=None,
)
```

`agent.thread(id, ...)` accepts a human-readable id. `kagent` maps that id globally to Codex's
generated session id using a local `shelve` store. If you omit `id`, kagent starts a new Codex
thread and adopts Codex's generated thread id as `thread.id` after the first run starts. By
default, the store lives at `~/.kagent/threads`, not inside the workspace. Set `KAGENT_HOME` to
move the whole kagent state directory or `KAGENT_STORE_PATH` to point directly at a custom thread
store.

Thread creation and loading:

```python
agent.thread("billing-main") # create only; raise if saved already
agent.load("billing-main")   # resume only; never create
```

`runner="cli"` uses `codex exec` through an async subprocess. It stores Codex's generated
session id in that user-level store, then uses `codex exec resume` on later runs with the same
human-readable `Thread` id. If you need separate threads for separate projects, choose separate
ids such as `billing-main` and `website-main`.

`runner="sdk"` uses the experimental Codex app-server SDK and persistent threads. It requires the
local development `codex` dependency group.

```python
agent = kagent(model="gpt-5.5", runner="cli", yolo=True)

await agent.thread("goal-test").run(
    "Create hello.txt with exactly the text hello.",
    goal=True,
)
```

There is no native `codex exec --goal` flag. `goal=True` translates to a prompt beginning with
`/goal ...`, which is the behavior verified locally. Goal runs use the CLI runner, not app-server
SDK thread persistence.

`yolo=True` maps to Codex's `--dangerously-bypass-approvals-and-sandbox` flag.

## Examples

Already logged in with `kagent login` or `codex login`:

```bash
uv run python examples/01_already_logged_in.py
```

Trigger ChatGPT/Codex subscription login from Python:

```bash
uv run python examples/02_subscription_login.py
```

Log in with an OpenAI API key from Python:

```bash
export OPENAI_API_KEY="sk-..."
uv run python examples/03_api_key_login.py
```

Stream events in real time:

```bash
uv run python examples/04_stream_events.py
```

Run the OpenHands comparison example:

```bash
uv run --group examples python examples/openhands_thread.py
```

OpenHands may require interactive ChatGPT subscription login on first run.

## Development

```bash
uv sync --group dev --group codex --group examples
uv run pytest
uv run ruff check .
uv run mypy
uv build
```

Publish:

```bash
export UV_PUBLISH_TOKEN="pypi-..."
uv build
uv publish
```

The package uses:

- `src/` layout
- `hatchling` build backend
- MIT license
- `py.typed` for typed package consumers
- `uv.lock` committed for reproducible local development
- pytest, ruff, and mypy for validation

## License

MIT
