konen 0.1.0__py3-none-any.whl
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/__init__.py +47 -0
- konen/backend/__init__.py +43 -0
- konen/backend/ai_agent.py +11 -0
- konen/backend/ai_team.py +70 -0
- konen/backend/claude_engine.py +200 -0
- konen/backend/engine_factory.py +87 -0
- konen/backend/engine_router.py +43 -0
- konen/backend/gemini_engine.py +138 -0
- konen/backend/gpt_engine.py +119 -0
- konen/backend/interaction_round.py +213 -0
- konen/backend/interaction_state.py +87 -0
- konen/backend/kimi_engine.py +159 -0
- konen/backend/llm_engine.py +57 -0
- konen/backend/llm_pool.py +65 -0
- konen/backend/llm_response.py +16 -0
- konen/backend/orchestrator.py +233 -0
- konen/backend/provider_history.py +94 -0
- konen/backend/provider_round.py +31 -0
- konen/backend/transcript_history.py +177 -0
- konen/backend/transcript_round.py +87 -0
- konen/backend/workspace.py +624 -0
- konen/cli/__init__.py +1 -0
- konen/cli/app.py +900 -0
- konen/cli/commands.py +314 -0
- konen/cli/completer.py +248 -0
- konen/cli/main.py +24 -0
- konen/cli/renderer.py +459 -0
- konen/cli/session.py +180 -0
- konen-0.1.0.dist-info/METADATA +60 -0
- konen-0.1.0.dist-info/RECORD +33 -0
- konen-0.1.0.dist-info/WHEEL +4 -0
- konen-0.1.0.dist-info/entry_points.txt +3 -0
- konen-0.1.0.dist-info/licenses/LICENSE +21 -0
konen/__init__.py
ADDED
|
@@ -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()))
|
konen/backend/ai_team.py
ADDED
|
@@ -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)
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
|
|
3
|
+
from .workspace import Workspace
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(slots=True)
|
|
7
|
+
class EngineRouter:
|
|
8
|
+
"""Mapping from agent identifiers to engine identifiers."""
|
|
9
|
+
|
|
10
|
+
table: dict[str, str] = field(default_factory=dict)
|
|
11
|
+
|
|
12
|
+
def __post_init__(self) -> None:
|
|
13
|
+
self.table = dict(self.table)
|
|
14
|
+
|
|
15
|
+
@classmethod
|
|
16
|
+
def from_workspace(cls, workspace: Workspace) -> "EngineRouter":
|
|
17
|
+
"""Build a router from workspace configuration."""
|
|
18
|
+
mapping_data = workspace.load_agent_engine_map_config()
|
|
19
|
+
return cls(table=mapping_data["agent_engine_map"])
|
|
20
|
+
|
|
21
|
+
def assign_engine(self, agent_uid: str, engine_uid: str) -> None:
|
|
22
|
+
"""Assign an engine to an agent."""
|
|
23
|
+
self.table[agent_uid] = engine_uid
|
|
24
|
+
|
|
25
|
+
def unassign_engine(self, agent_uid: str) -> None:
|
|
26
|
+
"""Remove an agent's engine assignment."""
|
|
27
|
+
if agent_uid not in self.table:
|
|
28
|
+
raise ValueError(f"No engine mapping exists for agent '{agent_uid}'.")
|
|
29
|
+
del self.table[agent_uid]
|
|
30
|
+
|
|
31
|
+
def get_engine_uid(self, agent_uid: str) -> str:
|
|
32
|
+
"""Return the engine identifier assigned to an agent."""
|
|
33
|
+
try:
|
|
34
|
+
return self.table[agent_uid]
|
|
35
|
+
except KeyError as exc:
|
|
36
|
+
raise ValueError(
|
|
37
|
+
f"No engine mapping exists for agent '{agent_uid}'."
|
|
38
|
+
) from exc
|
|
39
|
+
|
|
40
|
+
def reload_from_workspace(self, workspace: Workspace) -> None:
|
|
41
|
+
"""Reload the router from workspace configuration."""
|
|
42
|
+
router = self.from_workspace(workspace)
|
|
43
|
+
self.table = dict(router.table)
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import Any
|
|
3
|
+
|
|
4
|
+
from google import genai
|
|
5
|
+
|
|
6
|
+
from .llm_engine import LLMEngine
|
|
7
|
+
from .llm_response import LLMResponse
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
@dataclass(slots=True)
|
|
11
|
+
class GeminiEngine(LLMEngine):
|
|
12
|
+
"""Google Gemini execution engine."""
|
|
13
|
+
|
|
14
|
+
def __post_init__(self) -> None:
|
|
15
|
+
if self.model_family != "gemini":
|
|
16
|
+
raise ValueError("GeminiEngine requires model_family='gemini'.")
|
|
17
|
+
super().__post_init__()
|
|
18
|
+
|
|
19
|
+
def _initialize_client(self) -> genai.Client:
|
|
20
|
+
api_key = self.config.get("api_key")
|
|
21
|
+
if api_key is None:
|
|
22
|
+
return genai.Client()
|
|
23
|
+
return genai.Client(api_key=api_key)
|
|
24
|
+
|
|
25
|
+
def generate_response(
|
|
26
|
+
self,
|
|
27
|
+
prompt: str,
|
|
28
|
+
system_instruction: str | None = None,
|
|
29
|
+
provider_state: dict[str, Any] | None = None,
|
|
30
|
+
) -> LLMResponse:
|
|
31
|
+
"""Generate a response with the Gemini Interactions API."""
|
|
32
|
+
generation_kwargs: dict[str, Any] = {
|
|
33
|
+
"model": self.model_name,
|
|
34
|
+
"input": prompt,
|
|
35
|
+
"store": True,
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
if system_instruction is not None:
|
|
39
|
+
generation_kwargs["system_instruction"] = system_instruction
|
|
40
|
+
|
|
41
|
+
previous_interaction_id = self._previous_interaction_id(provider_state)
|
|
42
|
+
if previous_interaction_id is not None:
|
|
43
|
+
generation_kwargs["previous_interaction_id"] = previous_interaction_id
|
|
44
|
+
|
|
45
|
+
generation_config: dict[str, Any] = {}
|
|
46
|
+
|
|
47
|
+
thinking_level = self.config.get("thinking_level")
|
|
48
|
+
if thinking_level is not None:
|
|
49
|
+
generation_config["thinking_level"] = thinking_level
|
|
50
|
+
|
|
51
|
+
if generation_config:
|
|
52
|
+
generation_kwargs["generation_config"] = generation_config
|
|
53
|
+
|
|
54
|
+
interaction = self.client.interactions.create(**generation_kwargs)
|
|
55
|
+
|
|
56
|
+
status = self._status_value(interaction.status)
|
|
57
|
+
if status is None:
|
|
58
|
+
raise RuntimeError("Gemini interaction did not return a status.")
|
|
59
|
+
|
|
60
|
+
metadata = self._build_response_metadata()
|
|
61
|
+
metadata["interaction_status"] = status
|
|
62
|
+
|
|
63
|
+
interaction_id = interaction.id
|
|
64
|
+
if interaction_id is not None:
|
|
65
|
+
metadata["provider_response_id"] = interaction_id
|
|
66
|
+
|
|
67
|
+
usage = interaction.usage
|
|
68
|
+
if usage is not None:
|
|
69
|
+
token_usage: dict[str, int] = {}
|
|
70
|
+
|
|
71
|
+
usage_fields = {
|
|
72
|
+
"input_token_count": usage.total_input_tokens,
|
|
73
|
+
"output_token_count": usage.total_output_tokens,
|
|
74
|
+
"total_token_count": usage.total_tokens,
|
|
75
|
+
"cached_token_count": usage.total_cached_tokens,
|
|
76
|
+
"thought_token_count": usage.total_thought_tokens,
|
|
77
|
+
"tool_use_token_count": usage.total_tool_use_tokens,
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
for name, value in usage_fields.items():
|
|
81
|
+
if value is not None:
|
|
82
|
+
token_usage[name] = value
|
|
83
|
+
|
|
84
|
+
if token_usage:
|
|
85
|
+
metadata["token_usage"] = token_usage
|
|
86
|
+
|
|
87
|
+
if status in {"failed", "cancelled", "budget_exceeded"}:
|
|
88
|
+
raise RuntimeError(f"Gemini interaction ended with status '{status}'.")
|
|
89
|
+
|
|
90
|
+
if status in {"queued", "in_progress", "requires_action"}:
|
|
91
|
+
raise RuntimeError(
|
|
92
|
+
"Gemini interaction did not complete synchronously; "
|
|
93
|
+
f"status is '{status}'."
|
|
94
|
+
)
|
|
95
|
+
|
|
96
|
+
if status not in {"completed", "incomplete"}:
|
|
97
|
+
raise RuntimeError(f"Unexpected Gemini interaction status '{status}'.")
|
|
98
|
+
|
|
99
|
+
try:
|
|
100
|
+
text = interaction.output_text
|
|
101
|
+
except (ValueError, AttributeError):
|
|
102
|
+
text = None
|
|
103
|
+
|
|
104
|
+
next_provider_state: dict[str, Any] = {}
|
|
105
|
+
|
|
106
|
+
if status == "completed":
|
|
107
|
+
if not isinstance(interaction_id, str) or not interaction_id:
|
|
108
|
+
raise RuntimeError("Completed Gemini interaction did not return an ID.")
|
|
109
|
+
|
|
110
|
+
next_provider_state["interaction_id"] = interaction_id
|
|
111
|
+
|
|
112
|
+
return LLMResponse(
|
|
113
|
+
text=text, metadata=metadata, provider_state=next_provider_state
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
@staticmethod
|
|
117
|
+
def _previous_interaction_id(provider_state: dict[str, Any] | None) -> str | None:
|
|
118
|
+
"""Validate and return the previous Gemini interaction identifier."""
|
|
119
|
+
if provider_state is None:
|
|
120
|
+
return None
|
|
121
|
+
|
|
122
|
+
interaction_id = provider_state.get("interaction_id")
|
|
123
|
+
|
|
124
|
+
if not isinstance(interaction_id, str) or not interaction_id:
|
|
125
|
+
raise ValueError(
|
|
126
|
+
"Gemini provider state must contain a non-empty "
|
|
127
|
+
"'interaction_id' string."
|
|
128
|
+
)
|
|
129
|
+
|
|
130
|
+
return interaction_id
|
|
131
|
+
|
|
132
|
+
@staticmethod
|
|
133
|
+
def _status_value(status: Any) -> str | None:
|
|
134
|
+
"""Return the Gemini interaction status value."""
|
|
135
|
+
if status is None:
|
|
136
|
+
return None
|
|
137
|
+
|
|
138
|
+
return getattr(status, "value", str(status))
|