konen 0.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- konen-0.1.0/LICENSE +21 -0
- konen-0.1.0/PKG-INFO +60 -0
- konen-0.1.0/README.md +41 -0
- konen-0.1.0/pyproject.toml +54 -0
- konen-0.1.0/pyproject.toml.orig +49 -0
- konen-0.1.0/src/konen/__init__.py +47 -0
- konen-0.1.0/src/konen/backend/__init__.py +43 -0
- konen-0.1.0/src/konen/backend/ai_agent.py +11 -0
- konen-0.1.0/src/konen/backend/ai_team.py +70 -0
- konen-0.1.0/src/konen/backend/claude_engine.py +200 -0
- konen-0.1.0/src/konen/backend/engine_factory.py +87 -0
- konen-0.1.0/src/konen/backend/engine_router.py +43 -0
- konen-0.1.0/src/konen/backend/gemini_engine.py +138 -0
- konen-0.1.0/src/konen/backend/gpt_engine.py +119 -0
- konen-0.1.0/src/konen/backend/interaction_round.py +213 -0
- konen-0.1.0/src/konen/backend/interaction_state.py +87 -0
- konen-0.1.0/src/konen/backend/kimi_engine.py +159 -0
- konen-0.1.0/src/konen/backend/llm_engine.py +57 -0
- konen-0.1.0/src/konen/backend/llm_pool.py +65 -0
- konen-0.1.0/src/konen/backend/llm_response.py +16 -0
- konen-0.1.0/src/konen/backend/orchestrator.py +233 -0
- konen-0.1.0/src/konen/backend/provider_history.py +94 -0
- konen-0.1.0/src/konen/backend/provider_round.py +31 -0
- konen-0.1.0/src/konen/backend/transcript_history.py +177 -0
- konen-0.1.0/src/konen/backend/transcript_round.py +87 -0
- konen-0.1.0/src/konen/backend/workspace.py +624 -0
- konen-0.1.0/src/konen/cli/__init__.py +1 -0
- konen-0.1.0/src/konen/cli/app.py +900 -0
- konen-0.1.0/src/konen/cli/commands.py +314 -0
- konen-0.1.0/src/konen/cli/completer.py +248 -0
- konen-0.1.0/src/konen/cli/main.py +24 -0
- konen-0.1.0/src/konen/cli/renderer.py +459 -0
- konen-0.1.0/src/konen/cli/session.py +180 -0
konen-0.1.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 PolyShape P.C.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
konen-0.1.0/PKG-INFO
ADDED
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: konen
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Human-centered star-graph orchestration of AI agents
|
|
5
|
+
Author: Theodore Papamarkou
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
License-File: LICENSE
|
|
8
|
+
Requires-Dist: anthropic
|
|
9
|
+
Requires-Dist: google-genai>=2.18.1,<3.0.0
|
|
10
|
+
Requires-Dist: jsonschema
|
|
11
|
+
Requires-Dist: kimi-sdk
|
|
12
|
+
Requires-Dist: openai
|
|
13
|
+
Requires-Dist: prompt-toolkit
|
|
14
|
+
Requires-Dist: rich
|
|
15
|
+
Requires-Python: >=3.13, <3.16
|
|
16
|
+
Project-URL: Source, https://github.com/polyshape/konen
|
|
17
|
+
Project-URL: Documentation, https://polyshape.github.io/konen/
|
|
18
|
+
Description-Content-Type: text/markdown
|
|
19
|
+
|
|
20
|
+
<p align="center">
|
|
21
|
+
<img src="https://raw.githubusercontent.com/polyshape/konen/main/logo/konen_logo_transparent_bg.svg" alt="Konen logo" width="120">
|
|
22
|
+
</p>
|
|
23
|
+
|
|
24
|
+
# Konen: human-centered orchestration of AI agents
|
|
25
|
+
|
|
26
|
+
*The Swiss army knife for agentic IREs*
|
|
27
|
+
|
|
28
|
+
Konen is pronounced /ˈkoʊnɛn/. Its name comes from the star graph $K_{1,n}$, reflecting Konen's human-centered approach to agentic interaction. The human occupies the central node and orchestrates a collection of AI agents represented by the leaf nodes.
|
|
29
|
+
|
|
30
|
+
Konen emphasizes human-directed deliberation. A user decides which agents participate and how they engage with each other's contributions, while maintaining a shared conversation history. Although similar capabilities exist in other multi-model tools, Konen offers a simple, focused approach suited to iterative exploration of ideas, critical analysis, and research. Thus, Konen is suited to use as an integrated research environment (IRE) for coordinating interactions across AI agents and model providers.
|
|
31
|
+
|
|
32
|
+
## Installation
|
|
33
|
+
|
|
34
|
+
Konen requires Python 3.13, 3.14, or 3.15.
|
|
35
|
+
|
|
36
|
+
### pip
|
|
37
|
+
|
|
38
|
+
Install Konen from PyPI:
|
|
39
|
+
|
|
40
|
+
```bash
|
|
41
|
+
pip install konen
|
|
42
|
+
```
|
|
43
|
+
|
|
44
|
+
### uv
|
|
45
|
+
|
|
46
|
+
To install the CLI in an isolated environment:
|
|
47
|
+
|
|
48
|
+
```bash
|
|
49
|
+
uv tool install konen
|
|
50
|
+
```
|
|
51
|
+
|
|
52
|
+
Run `konen` after installing it. For a source checkout and development setup, see [DEVELOPMENT.md](https://github.com/polyshape/konen/blob/main/DEVELOPMENT.md).
|
|
53
|
+
|
|
54
|
+
## Documentation
|
|
55
|
+
|
|
56
|
+
Read the [documentation](https://polyshape.github.io/konen/), starting with the [first walkthrough example](https://polyshape.github.io/konen/examples/ai-consciousness-evidence.html).
|
|
57
|
+
|
|
58
|
+
## License
|
|
59
|
+
|
|
60
|
+
Konen is distributed under the MIT License. See [LICENSE](https://github.com/polyshape/konen/blob/main/LICENSE).
|
konen-0.1.0/README.md
ADDED
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
<p align="center">
|
|
2
|
+
<img src="https://raw.githubusercontent.com/polyshape/konen/main/logo/konen_logo_transparent_bg.svg" alt="Konen logo" width="120">
|
|
3
|
+
</p>
|
|
4
|
+
|
|
5
|
+
# Konen: human-centered orchestration of AI agents
|
|
6
|
+
|
|
7
|
+
*The Swiss army knife for agentic IREs*
|
|
8
|
+
|
|
9
|
+
Konen is pronounced /ˈkoʊnɛn/. Its name comes from the star graph $K_{1,n}$, reflecting Konen's human-centered approach to agentic interaction. The human occupies the central node and orchestrates a collection of AI agents represented by the leaf nodes.
|
|
10
|
+
|
|
11
|
+
Konen emphasizes human-directed deliberation. A user decides which agents participate and how they engage with each other's contributions, while maintaining a shared conversation history. Although similar capabilities exist in other multi-model tools, Konen offers a simple, focused approach suited to iterative exploration of ideas, critical analysis, and research. Thus, Konen is suited to use as an integrated research environment (IRE) for coordinating interactions across AI agents and model providers.
|
|
12
|
+
|
|
13
|
+
## Installation
|
|
14
|
+
|
|
15
|
+
Konen requires Python 3.13, 3.14, or 3.15.
|
|
16
|
+
|
|
17
|
+
### pip
|
|
18
|
+
|
|
19
|
+
Install Konen from PyPI:
|
|
20
|
+
|
|
21
|
+
```bash
|
|
22
|
+
pip install konen
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
### uv
|
|
26
|
+
|
|
27
|
+
To install the CLI in an isolated environment:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
uv tool install konen
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Run `konen` after installing it. For a source checkout and development setup, see [DEVELOPMENT.md](https://github.com/polyshape/konen/blob/main/DEVELOPMENT.md).
|
|
34
|
+
|
|
35
|
+
## Documentation
|
|
36
|
+
|
|
37
|
+
Read the [documentation](https://polyshape.github.io/konen/), starting with the [first walkthrough example](https://polyshape.github.io/konen/examples/ai-consciousness-evidence.html).
|
|
38
|
+
|
|
39
|
+
## License
|
|
40
|
+
|
|
41
|
+
Konen is distributed under the MIT License. See [LICENSE](https://github.com/polyshape/konen/blob/main/LICENSE).
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "konen"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Human-centered star-graph orchestration of AI agents"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
requires-python = ">=3.13,<3.16"
|
|
7
|
+
license = "MIT"
|
|
8
|
+
license-files = ["LICENSE"]
|
|
9
|
+
dependencies = [
|
|
10
|
+
"anthropic",
|
|
11
|
+
"google-genai>=2.18.1,<3.0.0",
|
|
12
|
+
"jsonschema",
|
|
13
|
+
"kimi-sdk",
|
|
14
|
+
"openai",
|
|
15
|
+
"prompt-toolkit",
|
|
16
|
+
"rich",
|
|
17
|
+
]
|
|
18
|
+
|
|
19
|
+
[[project.authors]]
|
|
20
|
+
name = "Theodore Papamarkou"
|
|
21
|
+
|
|
22
|
+
[project.scripts]
|
|
23
|
+
konen = "konen.cli.main:main"
|
|
24
|
+
|
|
25
|
+
[project.urls]
|
|
26
|
+
Source = "https://github.com/polyshape/konen"
|
|
27
|
+
Documentation = "https://polyshape.github.io/konen/"
|
|
28
|
+
|
|
29
|
+
[dependency-groups]
|
|
30
|
+
dev = [
|
|
31
|
+
"ipykernel",
|
|
32
|
+
"pytest",
|
|
33
|
+
"ruff",
|
|
34
|
+
]
|
|
35
|
+
docs = [
|
|
36
|
+
"myst-parser",
|
|
37
|
+
"sphinx",
|
|
38
|
+
]
|
|
39
|
+
|
|
40
|
+
[tool.ruff]
|
|
41
|
+
line-length = 88
|
|
42
|
+
|
|
43
|
+
[tool.ruff.lint]
|
|
44
|
+
select = [
|
|
45
|
+
"E4",
|
|
46
|
+
"E7",
|
|
47
|
+
"E9",
|
|
48
|
+
"F",
|
|
49
|
+
"I",
|
|
50
|
+
]
|
|
51
|
+
|
|
52
|
+
[build-system]
|
|
53
|
+
requires = ["uv_build>=0.11.32,<0.12"]
|
|
54
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
[project]
|
|
2
|
+
name = "konen"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "Human-centered star-graph orchestration of AI agents"
|
|
5
|
+
readme = "README.md"
|
|
6
|
+
authors = [
|
|
7
|
+
{ name = "Theodore Papamarkou" }
|
|
8
|
+
]
|
|
9
|
+
requires-python = ">=3.13,<3.16"
|
|
10
|
+
license = "MIT"
|
|
11
|
+
license-files = ["LICENSE"]
|
|
12
|
+
|
|
13
|
+
dependencies = [
|
|
14
|
+
"anthropic",
|
|
15
|
+
"google-genai>=2.18.1,<3.0.0",
|
|
16
|
+
"jsonschema",
|
|
17
|
+
"kimi-sdk",
|
|
18
|
+
"openai",
|
|
19
|
+
"prompt-toolkit",
|
|
20
|
+
"rich"
|
|
21
|
+
]
|
|
22
|
+
|
|
23
|
+
[project.scripts]
|
|
24
|
+
konen = "konen.cli.main:main"
|
|
25
|
+
|
|
26
|
+
[project.urls]
|
|
27
|
+
Source = "https://github.com/polyshape/konen"
|
|
28
|
+
Documentation = "https://polyshape.github.io/konen/"
|
|
29
|
+
|
|
30
|
+
[dependency-groups]
|
|
31
|
+
dev = [
|
|
32
|
+
"ipykernel",
|
|
33
|
+
"pytest",
|
|
34
|
+
"ruff"
|
|
35
|
+
]
|
|
36
|
+
docs = [
|
|
37
|
+
"myst-parser",
|
|
38
|
+
"sphinx"
|
|
39
|
+
]
|
|
40
|
+
|
|
41
|
+
[tool.ruff]
|
|
42
|
+
line-length = 88
|
|
43
|
+
|
|
44
|
+
[tool.ruff.lint]
|
|
45
|
+
select = ["E4", "E7", "E9", "F", "I"]
|
|
46
|
+
|
|
47
|
+
[build-system]
|
|
48
|
+
requires = ["uv_build>=0.11.32,<0.12"]
|
|
49
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
"""Konen public API."""
|
|
2
|
+
|
|
3
|
+
from .backend import (
|
|
4
|
+
AIAgent,
|
|
5
|
+
AITeam,
|
|
6
|
+
ClaudeEngine,
|
|
7
|
+
EngineFactory,
|
|
8
|
+
EngineRouter,
|
|
9
|
+
GeminiEngine,
|
|
10
|
+
GPTEngine,
|
|
11
|
+
InteractionRound,
|
|
12
|
+
InteractionState,
|
|
13
|
+
KimiEngine,
|
|
14
|
+
LLMEngine,
|
|
15
|
+
LLMPool,
|
|
16
|
+
LLMResponse,
|
|
17
|
+
Orchestrator,
|
|
18
|
+
ProviderHistory,
|
|
19
|
+
ProviderRound,
|
|
20
|
+
TranscriptHistory,
|
|
21
|
+
TranscriptRound,
|
|
22
|
+
Workspace,
|
|
23
|
+
)
|
|
24
|
+
|
|
25
|
+
__version__ = "0.1.0"
|
|
26
|
+
|
|
27
|
+
__all__ = [
|
|
28
|
+
"AIAgent",
|
|
29
|
+
"AITeam",
|
|
30
|
+
"ClaudeEngine",
|
|
31
|
+
"EngineFactory",
|
|
32
|
+
"EngineRouter",
|
|
33
|
+
"GeminiEngine",
|
|
34
|
+
"GPTEngine",
|
|
35
|
+
"InteractionRound",
|
|
36
|
+
"InteractionState",
|
|
37
|
+
"KimiEngine",
|
|
38
|
+
"LLMEngine",
|
|
39
|
+
"LLMPool",
|
|
40
|
+
"LLMResponse",
|
|
41
|
+
"Orchestrator",
|
|
42
|
+
"ProviderHistory",
|
|
43
|
+
"ProviderRound",
|
|
44
|
+
"TranscriptHistory",
|
|
45
|
+
"TranscriptRound",
|
|
46
|
+
"Workspace",
|
|
47
|
+
]
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
"""Konen backend API."""
|
|
2
|
+
|
|
3
|
+
from .ai_agent import AIAgent
|
|
4
|
+
from .ai_team import AITeam
|
|
5
|
+
from .claude_engine import ClaudeEngine
|
|
6
|
+
from .engine_factory import EngineFactory
|
|
7
|
+
from .engine_router import EngineRouter
|
|
8
|
+
from .gemini_engine import GeminiEngine
|
|
9
|
+
from .gpt_engine import GPTEngine
|
|
10
|
+
from .interaction_round import InteractionRound
|
|
11
|
+
from .interaction_state import InteractionState
|
|
12
|
+
from .kimi_engine import KimiEngine
|
|
13
|
+
from .llm_engine import LLMEngine
|
|
14
|
+
from .llm_pool import LLMPool
|
|
15
|
+
from .llm_response import LLMResponse
|
|
16
|
+
from .orchestrator import Orchestrator
|
|
17
|
+
from .provider_history import ProviderHistory
|
|
18
|
+
from .provider_round import ProviderRound
|
|
19
|
+
from .transcript_history import TranscriptHistory
|
|
20
|
+
from .transcript_round import TranscriptRound
|
|
21
|
+
from .workspace import Workspace
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"AIAgent",
|
|
25
|
+
"AITeam",
|
|
26
|
+
"ClaudeEngine",
|
|
27
|
+
"EngineFactory",
|
|
28
|
+
"EngineRouter",
|
|
29
|
+
"GeminiEngine",
|
|
30
|
+
"GPTEngine",
|
|
31
|
+
"InteractionRound",
|
|
32
|
+
"InteractionState",
|
|
33
|
+
"KimiEngine",
|
|
34
|
+
"LLMEngine",
|
|
35
|
+
"LLMPool",
|
|
36
|
+
"LLMResponse",
|
|
37
|
+
"Orchestrator",
|
|
38
|
+
"ProviderHistory",
|
|
39
|
+
"ProviderRound",
|
|
40
|
+
"TranscriptHistory",
|
|
41
|
+
"TranscriptRound",
|
|
42
|
+
"Workspace",
|
|
43
|
+
]
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
from uuid import uuid4
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
@dataclass(frozen=True, slots=True)
|
|
6
|
+
class AIAgent:
|
|
7
|
+
"""AI agent with a name, description, and identifier."""
|
|
8
|
+
|
|
9
|
+
name: str
|
|
10
|
+
description: str
|
|
11
|
+
uid: str = field(default_factory=lambda: str(uuid4()))
|
|
@@ -0,0 +1,70 @@
|
|
|
1
|
+
from collections.abc import Iterable
|
|
2
|
+
|
|
3
|
+
from .ai_agent import AIAgent
|
|
4
|
+
from .workspace import Workspace
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class AITeam:
|
|
8
|
+
"""Collection of AI agents indexed by identifier."""
|
|
9
|
+
|
|
10
|
+
def __init__(self, agents: Iterable[AIAgent] = ()) -> None:
|
|
11
|
+
self._agents: dict[str, AIAgent] = {}
|
|
12
|
+
for agent in agents:
|
|
13
|
+
self._require_agent_absent(agent.uid)
|
|
14
|
+
self._agents[agent.uid] = agent
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def from_workspace(cls, workspace: Workspace) -> "AITeam":
|
|
18
|
+
"""Build a team from workspace configuration."""
|
|
19
|
+
agents_data = workspace.load_agents_config()
|
|
20
|
+
agents_config = agents_data["agents"]
|
|
21
|
+
|
|
22
|
+
agents: list[AIAgent] = []
|
|
23
|
+
for agent_uid, config in agents_config.items():
|
|
24
|
+
agents.append(
|
|
25
|
+
AIAgent(
|
|
26
|
+
name=config["name"],
|
|
27
|
+
description=config["description"],
|
|
28
|
+
uid=agent_uid,
|
|
29
|
+
)
|
|
30
|
+
)
|
|
31
|
+
return cls(agents)
|
|
32
|
+
|
|
33
|
+
def get_agent(self, agent_uid: str) -> AIAgent:
|
|
34
|
+
"""Return the agent with the given identifier."""
|
|
35
|
+
self._require_agent_present(agent_uid)
|
|
36
|
+
return self._agents[agent_uid]
|
|
37
|
+
|
|
38
|
+
def list_agents(self) -> list[str]:
|
|
39
|
+
"""Return sorted agent identifiers."""
|
|
40
|
+
return sorted(self._agents.keys())
|
|
41
|
+
|
|
42
|
+
def to_context(self) -> dict[str, dict[str, str]]:
|
|
43
|
+
"""Return agent data as team context."""
|
|
44
|
+
return {
|
|
45
|
+
agent_uid: {"name": agent.name, "description": agent.description}
|
|
46
|
+
for agent_uid, agent in sorted(self._agents.items())
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
def add_agent(self, agent: AIAgent) -> None:
|
|
50
|
+
"""Add an agent."""
|
|
51
|
+
self._require_agent_absent(agent.uid)
|
|
52
|
+
self._agents[agent.uid] = agent
|
|
53
|
+
|
|
54
|
+
def remove_agent(self, agent_uid: str) -> None:
|
|
55
|
+
"""Remove the agent with the given identifier."""
|
|
56
|
+
self._require_agent_present(agent_uid)
|
|
57
|
+
del self._agents[agent_uid]
|
|
58
|
+
|
|
59
|
+
def reload_from_workspace(self, workspace: Workspace) -> None:
|
|
60
|
+
"""Reload the team from workspace configuration."""
|
|
61
|
+
team = self.from_workspace(workspace)
|
|
62
|
+
self._agents = dict(team._agents)
|
|
63
|
+
|
|
64
|
+
def _require_agent_absent(self, agent_uid: str) -> None:
|
|
65
|
+
if agent_uid in self._agents:
|
|
66
|
+
raise ValueError(f"Agent '{agent_uid}' already exists in AI team.")
|
|
67
|
+
|
|
68
|
+
def _require_agent_present(self, agent_uid: str) -> None:
|
|
69
|
+
if agent_uid not in self._agents:
|
|
70
|
+
raise ValueError(f"Agent '{agent_uid}' does not exist in AI team.")
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
import copy
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from typing import Any
|
|
4
|
+
|
|
5
|
+
from anthropic import Anthropic
|
|
6
|
+
|
|
7
|
+
from .llm_engine import LLMEngine
|
|
8
|
+
from .llm_response import LLMResponse
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
@dataclass(slots=True)
|
|
12
|
+
class ClaudeEngine(LLMEngine):
|
|
13
|
+
"""Anthropic Claude execution engine."""
|
|
14
|
+
|
|
15
|
+
def __post_init__(self) -> None:
|
|
16
|
+
if self.model_family != "claude":
|
|
17
|
+
raise ValueError("ClaudeEngine requires model_family='claude'.")
|
|
18
|
+
super().__post_init__()
|
|
19
|
+
|
|
20
|
+
def _initialize_client(self) -> Anthropic:
|
|
21
|
+
api_key = self.config.get("api_key")
|
|
22
|
+
if api_key is None:
|
|
23
|
+
return Anthropic()
|
|
24
|
+
return Anthropic(api_key=api_key)
|
|
25
|
+
|
|
26
|
+
def prepare_provider_state(
|
|
27
|
+
self, states: list[dict[str, Any]]
|
|
28
|
+
) -> dict[str, Any] | None:
|
|
29
|
+
"""Prepare Anthropic message history from prior provider states."""
|
|
30
|
+
if not states:
|
|
31
|
+
return None
|
|
32
|
+
|
|
33
|
+
messages: list[dict[str, Any]] = []
|
|
34
|
+
for state in states:
|
|
35
|
+
user_message = state.get("user_message")
|
|
36
|
+
assistant_message = state.get("assistant_message")
|
|
37
|
+
|
|
38
|
+
if not isinstance(user_message, dict):
|
|
39
|
+
raise ValueError(
|
|
40
|
+
"Claude provider state must contain a 'user_message' dictionary."
|
|
41
|
+
)
|
|
42
|
+
if not isinstance(assistant_message, dict):
|
|
43
|
+
raise ValueError(
|
|
44
|
+
"Claude provider state must contain an "
|
|
45
|
+
"'assistant_message' dictionary."
|
|
46
|
+
)
|
|
47
|
+
|
|
48
|
+
messages.append(copy.deepcopy(user_message))
|
|
49
|
+
messages.append(copy.deepcopy(assistant_message))
|
|
50
|
+
|
|
51
|
+
return {"messages": messages}
|
|
52
|
+
|
|
53
|
+
def generate_response(
|
|
54
|
+
self,
|
|
55
|
+
prompt: str,
|
|
56
|
+
system_instruction: str | None = None,
|
|
57
|
+
provider_state: dict[str, Any] | None = None,
|
|
58
|
+
) -> LLMResponse:
|
|
59
|
+
"""Generate a response with the Anthropic Messages API."""
|
|
60
|
+
messages = self._previous_messages(provider_state)
|
|
61
|
+
user_message = {"role": "user", "content": prompt}
|
|
62
|
+
messages.append(copy.deepcopy(user_message))
|
|
63
|
+
|
|
64
|
+
message_kwargs: dict[str, Any] = {
|
|
65
|
+
"model": self.model_name,
|
|
66
|
+
"max_tokens": self._max_tokens(),
|
|
67
|
+
"messages": messages,
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
if system_instruction is not None:
|
|
71
|
+
message_kwargs["system"] = system_instruction
|
|
72
|
+
|
|
73
|
+
thinking = self.config.get("thinking")
|
|
74
|
+
if thinking is not None:
|
|
75
|
+
if not isinstance(thinking, dict):
|
|
76
|
+
raise ValueError(
|
|
77
|
+
"ClaudeEngine.config['thinking'] must be a dictionary."
|
|
78
|
+
)
|
|
79
|
+
message_kwargs["thinking"] = copy.deepcopy(thinking)
|
|
80
|
+
|
|
81
|
+
message = self.client.messages.create(**message_kwargs)
|
|
82
|
+
|
|
83
|
+
stop_reason = message.stop_reason
|
|
84
|
+
if stop_reason is None:
|
|
85
|
+
raise RuntimeError("Claude message did not return a stop reason.")
|
|
86
|
+
|
|
87
|
+
metadata = self._build_response_metadata()
|
|
88
|
+
metadata["provider_response_id"] = message.id
|
|
89
|
+
metadata["stop_reason"] = stop_reason
|
|
90
|
+
|
|
91
|
+
if message.stop_sequence is not None:
|
|
92
|
+
metadata["stop_sequence"] = message.stop_sequence
|
|
93
|
+
|
|
94
|
+
usage = message.usage
|
|
95
|
+
cache_creation_tokens = usage.cache_creation_input_tokens or 0
|
|
96
|
+
cached_tokens = usage.cache_read_input_tokens or 0
|
|
97
|
+
input_tokens = usage.input_tokens + cache_creation_tokens + cached_tokens
|
|
98
|
+
|
|
99
|
+
token_usage: dict[str, int] = {
|
|
100
|
+
"input_token_count": input_tokens,
|
|
101
|
+
"output_token_count": usage.output_tokens,
|
|
102
|
+
"total_token_count": input_tokens + usage.output_tokens,
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
if usage.cache_creation_input_tokens is not None:
|
|
106
|
+
token_usage["cache_creation_token_count"] = (
|
|
107
|
+
usage.cache_creation_input_tokens
|
|
108
|
+
)
|
|
109
|
+
|
|
110
|
+
if usage.cache_read_input_tokens is not None:
|
|
111
|
+
token_usage["cached_token_count"] = usage.cache_read_input_tokens
|
|
112
|
+
|
|
113
|
+
output_details = getattr(usage, "output_tokens_details", None)
|
|
114
|
+
if output_details is not None:
|
|
115
|
+
thinking_tokens = getattr(output_details, "thinking_tokens", None)
|
|
116
|
+
if thinking_tokens is not None:
|
|
117
|
+
token_usage["thinking_token_count"] = thinking_tokens
|
|
118
|
+
|
|
119
|
+
metadata["token_usage"] = token_usage
|
|
120
|
+
|
|
121
|
+
if stop_reason in {"tool_use", "pause_turn"}:
|
|
122
|
+
raise RuntimeError(
|
|
123
|
+
"Claude message requires continuation handling that Konen "
|
|
124
|
+
f"does not yet implement; stop_reason is '{stop_reason}'."
|
|
125
|
+
)
|
|
126
|
+
|
|
127
|
+
supported_stop_reasons = {
|
|
128
|
+
"end_turn",
|
|
129
|
+
"max_tokens",
|
|
130
|
+
"stop_sequence",
|
|
131
|
+
"refusal",
|
|
132
|
+
"model_context_window_exceeded",
|
|
133
|
+
}
|
|
134
|
+
if stop_reason not in supported_stop_reasons:
|
|
135
|
+
raise RuntimeError(f"Unexpected Claude stop reason '{stop_reason}'.")
|
|
136
|
+
|
|
137
|
+
text_parts = [block.text for block in message.content if block.type == "text"]
|
|
138
|
+
text = "".join(text_parts) or None
|
|
139
|
+
|
|
140
|
+
assistant_content = [
|
|
141
|
+
block.model_dump(mode="json", exclude_none=True)
|
|
142
|
+
for block in message.content
|
|
143
|
+
]
|
|
144
|
+
assistant_message = {"role": "assistant", "content": assistant_content}
|
|
145
|
+
|
|
146
|
+
return LLMResponse(
|
|
147
|
+
text=text,
|
|
148
|
+
metadata=metadata,
|
|
149
|
+
provider_state={
|
|
150
|
+
"user_message": user_message,
|
|
151
|
+
"assistant_message": assistant_message,
|
|
152
|
+
},
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
def _max_tokens(self) -> int:
|
|
156
|
+
"""Return the validated maximum output token count."""
|
|
157
|
+
max_tokens = self.config.get("max_tokens")
|
|
158
|
+
|
|
159
|
+
if (
|
|
160
|
+
not isinstance(max_tokens, int)
|
|
161
|
+
or isinstance(max_tokens, bool)
|
|
162
|
+
or max_tokens <= 0
|
|
163
|
+
):
|
|
164
|
+
raise ValueError(
|
|
165
|
+
"ClaudeEngine.config['max_tokens'] must be a positive integer."
|
|
166
|
+
)
|
|
167
|
+
|
|
168
|
+
return max_tokens
|
|
169
|
+
|
|
170
|
+
@staticmethod
|
|
171
|
+
def _previous_messages(
|
|
172
|
+
provider_state: dict[str, Any] | None,
|
|
173
|
+
) -> list[dict[str, Any]]:
|
|
174
|
+
"""Validate and return prior Anthropic messages."""
|
|
175
|
+
if provider_state is None:
|
|
176
|
+
return []
|
|
177
|
+
|
|
178
|
+
messages = provider_state.get("messages")
|
|
179
|
+
if not isinstance(messages, list) or not messages:
|
|
180
|
+
raise ValueError(
|
|
181
|
+
"Claude prepared provider state must contain a non-empty "
|
|
182
|
+
"'messages' list."
|
|
183
|
+
)
|
|
184
|
+
|
|
185
|
+
for message in messages:
|
|
186
|
+
if not isinstance(message, dict):
|
|
187
|
+
raise ValueError("Claude provider state messages must be dictionaries.")
|
|
188
|
+
|
|
189
|
+
if message.get("role") not in {"user", "assistant"}:
|
|
190
|
+
raise ValueError(
|
|
191
|
+
"Claude provider state messages must have role 'user' "
|
|
192
|
+
"or 'assistant'."
|
|
193
|
+
)
|
|
194
|
+
|
|
195
|
+
if "content" not in message:
|
|
196
|
+
raise ValueError(
|
|
197
|
+
"Claude provider state messages must contain 'content'."
|
|
198
|
+
)
|
|
199
|
+
|
|
200
|
+
return copy.deepcopy(messages)
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
from collections.abc import Callable
|
|
2
|
+
from typing import Any, ClassVar
|
|
3
|
+
|
|
4
|
+
from .claude_engine import ClaudeEngine
|
|
5
|
+
from .gemini_engine import GeminiEngine
|
|
6
|
+
from .gpt_engine import GPTEngine
|
|
7
|
+
from .kimi_engine import KimiEngine
|
|
8
|
+
from .llm_engine import LLMEngine
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class EngineFactory:
|
|
12
|
+
"""Factory for provider-specific execution engines."""
|
|
13
|
+
|
|
14
|
+
_builders: ClassVar[dict[str, Callable[[dict[str, Any]], LLMEngine]]] = {}
|
|
15
|
+
|
|
16
|
+
@classmethod
|
|
17
|
+
def register(
|
|
18
|
+
cls, model_family: str, builder: Callable[[dict[str, Any]], LLMEngine]
|
|
19
|
+
) -> None:
|
|
20
|
+
"""Register a builder for a model family."""
|
|
21
|
+
cls._builders[model_family] = builder
|
|
22
|
+
|
|
23
|
+
@classmethod
|
|
24
|
+
def build(
|
|
25
|
+
cls,
|
|
26
|
+
model_family: str,
|
|
27
|
+
model_name: str,
|
|
28
|
+
config: dict[str, Any] | None = None,
|
|
29
|
+
uid: str | None = None,
|
|
30
|
+
) -> LLMEngine:
|
|
31
|
+
"""Build an engine for a model family."""
|
|
32
|
+
try:
|
|
33
|
+
builder = cls._builders[model_family]
|
|
34
|
+
except KeyError as exc:
|
|
35
|
+
raise ValueError(f"Unsupported model_family: '{model_family}'.") from exc
|
|
36
|
+
kwargs: dict[str, Any] = {
|
|
37
|
+
"model_family": model_family,
|
|
38
|
+
"model_name": model_name,
|
|
39
|
+
}
|
|
40
|
+
if config is not None:
|
|
41
|
+
kwargs["config"] = dict(config)
|
|
42
|
+
if uid is not None:
|
|
43
|
+
kwargs["uid"] = uid
|
|
44
|
+
return builder(kwargs)
|
|
45
|
+
|
|
46
|
+
@staticmethod
|
|
47
|
+
def _validate_builder_kwargs(kwargs: dict[str, Any]) -> None:
|
|
48
|
+
"""Validate keyword arguments for an engine builder."""
|
|
49
|
+
allowed_keys = {"model_family", "model_name", "config", "uid"}
|
|
50
|
+
unexpected_keys = sorted(set(kwargs) - allowed_keys)
|
|
51
|
+
if unexpected_keys:
|
|
52
|
+
unexpected_keys_str = ", ".join(unexpected_keys)
|
|
53
|
+
raise ValueError(f"Unexpected engine kwargs: {unexpected_keys_str}.")
|
|
54
|
+
|
|
55
|
+
@staticmethod
|
|
56
|
+
def _claude_engine_builder(kwargs: dict[str, Any]) -> LLMEngine:
|
|
57
|
+
kwargs = dict(kwargs)
|
|
58
|
+
kwargs["model_family"] = "claude"
|
|
59
|
+
EngineFactory._validate_builder_kwargs(kwargs)
|
|
60
|
+
return ClaudeEngine(**kwargs)
|
|
61
|
+
|
|
62
|
+
@staticmethod
|
|
63
|
+
def _gemini_engine_builder(kwargs: dict[str, Any]) -> LLMEngine:
|
|
64
|
+
kwargs = dict(kwargs)
|
|
65
|
+
kwargs["model_family"] = "gemini"
|
|
66
|
+
EngineFactory._validate_builder_kwargs(kwargs)
|
|
67
|
+
return GeminiEngine(**kwargs)
|
|
68
|
+
|
|
69
|
+
@staticmethod
|
|
70
|
+
def _gpt_engine_builder(kwargs: dict[str, Any]) -> LLMEngine:
|
|
71
|
+
kwargs = dict(kwargs)
|
|
72
|
+
kwargs["model_family"] = "gpt"
|
|
73
|
+
EngineFactory._validate_builder_kwargs(kwargs)
|
|
74
|
+
return GPTEngine(**kwargs)
|
|
75
|
+
|
|
76
|
+
@staticmethod
|
|
77
|
+
def _kimi_engine_builder(kwargs: dict[str, Any]) -> LLMEngine:
|
|
78
|
+
kwargs = dict(kwargs)
|
|
79
|
+
kwargs["model_family"] = "kimi"
|
|
80
|
+
EngineFactory._validate_builder_kwargs(kwargs)
|
|
81
|
+
return KimiEngine(**kwargs)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
EngineFactory.register("claude", EngineFactory._claude_engine_builder)
|
|
85
|
+
EngineFactory.register("gemini", EngineFactory._gemini_engine_builder)
|
|
86
|
+
EngineFactory.register("gpt", EngineFactory._gpt_engine_builder)
|
|
87
|
+
EngineFactory.register("kimi", EngineFactory._kimi_engine_builder)
|