forked from Rockachopa/Timmy-time-dashboard
feat: add delegate_to_kimi() tool for coding delegation (#67)
Timmy can now delegate coding tasks to Kimi CLI (262K context). Includes timeout handling, workdir validation, output truncation. Sovereign division of labor — Timmy plans, Kimi codes.
This commit is contained in:
@@ -590,9 +590,10 @@ def create_full_toolkit(base_dir: str | Path | None = None):
|
||||
|
||||
# Inter-agent delegation - dispatch tasks to swarm agents
|
||||
try:
|
||||
from timmy.tools_delegation import delegate_task, list_swarm_agents
|
||||
from timmy.tools_delegation import delegate_task, delegate_to_kimi, list_swarm_agents
|
||||
|
||||
toolkit.register(delegate_task, name="delegate_task")
|
||||
toolkit.register(delegate_to_kimi, name="delegate_to_kimi")
|
||||
toolkit.register(list_swarm_agents, name="list_swarm_agents")
|
||||
except Exception as exc:
|
||||
logger.warning("Tool execution failed (Delegation tools registration): %s", exc)
|
||||
|
||||
@@ -87,3 +87,73 @@ def list_swarm_agents() -> dict[str, Any]:
|
||||
"error": str(e),
|
||||
"agents": [],
|
||||
}
|
||||
|
||||
|
||||
def delegate_to_kimi(task: str, working_directory: str = "") -> dict[str, Any]:
|
||||
"""Delegate a coding task to Kimi, the external coding agent.
|
||||
|
||||
Kimi has 262K context and is optimized for code tasks: writing,
|
||||
debugging, refactoring, test writing. Timmy thinks and plans,
|
||||
Kimi executes bulk code changes.
|
||||
|
||||
Args:
|
||||
task: Clear, specific coding task description. Include file paths
|
||||
and expected behavior. Good: "Fix the bug in src/timmy/session.py
|
||||
where sessions don't persist." Bad: "Fix all bugs."
|
||||
working_directory: Directory for Kimi to work in. Defaults to repo root.
|
||||
|
||||
Returns:
|
||||
Dict with success status and Kimi's output or error.
|
||||
"""
|
||||
import shutil
|
||||
import subprocess
|
||||
from pathlib import Path
|
||||
|
||||
from config import settings
|
||||
|
||||
kimi_path = shutil.which("kimi")
|
||||
if not kimi_path:
|
||||
return {
|
||||
"success": False,
|
||||
"error": "kimi CLI not found on PATH. Install with: pip install kimi-cli",
|
||||
}
|
||||
|
||||
workdir = working_directory or settings.repo_root
|
||||
if not Path(workdir).is_dir():
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Working directory does not exist: {workdir}",
|
||||
}
|
||||
|
||||
cmd = [kimi_path, "--print", "-p", task]
|
||||
|
||||
logger.info("Delegating to Kimi: %s (cwd=%s)", task[:80], workdir)
|
||||
|
||||
try:
|
||||
result = subprocess.run(
|
||||
cmd,
|
||||
capture_output=True,
|
||||
text=True,
|
||||
timeout=300, # 5 minute timeout for coding tasks
|
||||
cwd=workdir,
|
||||
)
|
||||
|
||||
output = result.stdout.strip()
|
||||
if result.returncode != 0 and result.stderr:
|
||||
output += "\n\nSTDERR:\n" + result.stderr.strip()
|
||||
|
||||
return {
|
||||
"success": result.returncode == 0,
|
||||
"output": output[-4000:] if len(output) > 4000 else output,
|
||||
"return_code": result.returncode,
|
||||
}
|
||||
except subprocess.TimeoutExpired:
|
||||
return {
|
||||
"success": False,
|
||||
"error": "Kimi timed out after 300s. Task may be too broad — try breaking it into smaller pieces.",
|
||||
}
|
||||
except Exception as exc:
|
||||
return {
|
||||
"success": False,
|
||||
"error": f"Failed to run Kimi: {exc}",
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user