flowly-code 1.0.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.
Files changed (86) hide show
  1. flowly_code/__init__.py +30 -0
  2. flowly_code/__main__.py +8 -0
  3. flowly_code/activity/__init__.py +1 -0
  4. flowly_code/activity/bus.py +91 -0
  5. flowly_code/activity/events.py +40 -0
  6. flowly_code/agent/__init__.py +8 -0
  7. flowly_code/agent/context.py +485 -0
  8. flowly_code/agent/loop.py +1349 -0
  9. flowly_code/agent/memory.py +109 -0
  10. flowly_code/agent/skills.py +259 -0
  11. flowly_code/agent/subagent.py +249 -0
  12. flowly_code/agent/tools/__init__.py +6 -0
  13. flowly_code/agent/tools/base.py +55 -0
  14. flowly_code/agent/tools/delegate.py +194 -0
  15. flowly_code/agent/tools/dispatch.py +840 -0
  16. flowly_code/agent/tools/docker.py +609 -0
  17. flowly_code/agent/tools/filesystem.py +280 -0
  18. flowly_code/agent/tools/mcp.py +85 -0
  19. flowly_code/agent/tools/message.py +235 -0
  20. flowly_code/agent/tools/registry.py +257 -0
  21. flowly_code/agent/tools/screenshot.py +444 -0
  22. flowly_code/agent/tools/shell.py +166 -0
  23. flowly_code/agent/tools/spawn.py +65 -0
  24. flowly_code/agent/tools/system.py +917 -0
  25. flowly_code/agent/tools/trello.py +420 -0
  26. flowly_code/agent/tools/web.py +139 -0
  27. flowly_code/agent/tools/x.py +399 -0
  28. flowly_code/bus/__init__.py +6 -0
  29. flowly_code/bus/events.py +37 -0
  30. flowly_code/bus/queue.py +81 -0
  31. flowly_code/channels/__init__.py +6 -0
  32. flowly_code/channels/base.py +121 -0
  33. flowly_code/channels/manager.py +135 -0
  34. flowly_code/channels/telegram.py +1132 -0
  35. flowly_code/cli/__init__.py +1 -0
  36. flowly_code/cli/commands.py +1831 -0
  37. flowly_code/cli/setup.py +1356 -0
  38. flowly_code/compaction/__init__.py +39 -0
  39. flowly_code/compaction/estimator.py +88 -0
  40. flowly_code/compaction/pruning.py +223 -0
  41. flowly_code/compaction/service.py +297 -0
  42. flowly_code/compaction/summarizer.py +384 -0
  43. flowly_code/compaction/types.py +71 -0
  44. flowly_code/config/__init__.py +6 -0
  45. flowly_code/config/loader.py +102 -0
  46. flowly_code/config/schema.py +324 -0
  47. flowly_code/exec/__init__.py +39 -0
  48. flowly_code/exec/approvals.py +288 -0
  49. flowly_code/exec/executor.py +184 -0
  50. flowly_code/exec/safety.py +247 -0
  51. flowly_code/exec/types.py +88 -0
  52. flowly_code/gateway/__init__.py +5 -0
  53. flowly_code/gateway/server.py +103 -0
  54. flowly_code/heartbeat/__init__.py +5 -0
  55. flowly_code/heartbeat/service.py +130 -0
  56. flowly_code/multiagent/README.md +248 -0
  57. flowly_code/multiagent/__init__.py +1 -0
  58. flowly_code/multiagent/invoke.py +210 -0
  59. flowly_code/multiagent/orchestrator.py +156 -0
  60. flowly_code/multiagent/router.py +156 -0
  61. flowly_code/multiagent/setup.py +171 -0
  62. flowly_code/pairing/__init__.py +21 -0
  63. flowly_code/pairing/store.py +343 -0
  64. flowly_code/providers/__init__.py +6 -0
  65. flowly_code/providers/base.py +69 -0
  66. flowly_code/providers/litellm_provider.py +178 -0
  67. flowly_code/providers/transcription.py +64 -0
  68. flowly_code/session/__init__.py +5 -0
  69. flowly_code/session/manager.py +249 -0
  70. flowly_code/skills/README.md +24 -0
  71. flowly_code/skills/compact/SKILL.md +27 -0
  72. flowly_code/skills/github/SKILL.md +48 -0
  73. flowly_code/skills/skill-creator/SKILL.md +371 -0
  74. flowly_code/skills/summarize/SKILL.md +67 -0
  75. flowly_code/skills/tmux/SKILL.md +121 -0
  76. flowly_code/skills/tmux/scripts/find-sessions.sh +112 -0
  77. flowly_code/skills/tmux/scripts/wait-for-text.sh +83 -0
  78. flowly_code/skills/weather/SKILL.md +49 -0
  79. flowly_code/utils/__init__.py +5 -0
  80. flowly_code/utils/helpers.py +91 -0
  81. flowly_code-1.0.0.dist-info/METADATA +724 -0
  82. flowly_code-1.0.0.dist-info/RECORD +86 -0
  83. flowly_code-1.0.0.dist-info/WHEEL +4 -0
  84. flowly_code-1.0.0.dist-info/entry_points.txt +2 -0
  85. flowly_code-1.0.0.dist-info/licenses/LICENSE +191 -0
  86. flowly_code-1.0.0.dist-info/licenses/NOTICE +74 -0
@@ -0,0 +1,109 @@
1
+ """Memory system for persistent agent memory."""
2
+
3
+ from pathlib import Path
4
+ from datetime import datetime
5
+
6
+ from flowly_code.utils.helpers import ensure_dir, today_date
7
+
8
+
9
+ class MemoryStore:
10
+ """
11
+ Memory system for the agent.
12
+
13
+ Supports daily notes (memory/YYYY-MM-DD.md) and long-term memory (MEMORY.md).
14
+ """
15
+
16
+ def __init__(self, workspace: Path):
17
+ self.workspace = workspace
18
+ self.memory_dir = ensure_dir(workspace / "memory")
19
+ self.memory_file = self.memory_dir / "MEMORY.md"
20
+
21
+ def get_today_file(self) -> Path:
22
+ """Get path to today's memory file."""
23
+ return self.memory_dir / f"{today_date()}.md"
24
+
25
+ def read_today(self) -> str:
26
+ """Read today's memory notes."""
27
+ today_file = self.get_today_file()
28
+ if today_file.exists():
29
+ return today_file.read_text(encoding="utf-8")
30
+ return ""
31
+
32
+ def append_today(self, content: str) -> None:
33
+ """Append content to today's memory notes."""
34
+ today_file = self.get_today_file()
35
+
36
+ if today_file.exists():
37
+ existing = today_file.read_text(encoding="utf-8")
38
+ content = existing + "\n" + content
39
+ else:
40
+ # Add header for new day
41
+ header = f"# {today_date()}\n\n"
42
+ content = header + content
43
+
44
+ today_file.write_text(content, encoding="utf-8")
45
+
46
+ def read_long_term(self) -> str:
47
+ """Read long-term memory (MEMORY.md)."""
48
+ if self.memory_file.exists():
49
+ return self.memory_file.read_text(encoding="utf-8")
50
+ return ""
51
+
52
+ def write_long_term(self, content: str) -> None:
53
+ """Write to long-term memory (MEMORY.md)."""
54
+ self.memory_file.write_text(content, encoding="utf-8")
55
+
56
+ def get_recent_memories(self, days: int = 7) -> str:
57
+ """
58
+ Get memories from the last N days.
59
+
60
+ Args:
61
+ days: Number of days to look back.
62
+
63
+ Returns:
64
+ Combined memory content.
65
+ """
66
+ from datetime import timedelta
67
+
68
+ memories = []
69
+ today = datetime.now().date()
70
+
71
+ for i in range(days):
72
+ date = today - timedelta(days=i)
73
+ date_str = date.strftime("%Y-%m-%d")
74
+ file_path = self.memory_dir / f"{date_str}.md"
75
+
76
+ if file_path.exists():
77
+ content = file_path.read_text(encoding="utf-8")
78
+ memories.append(content)
79
+
80
+ return "\n\n---\n\n".join(memories)
81
+
82
+ def list_memory_files(self) -> list[Path]:
83
+ """List all memory files sorted by date (newest first)."""
84
+ if not self.memory_dir.exists():
85
+ return []
86
+
87
+ files = list(self.memory_dir.glob("????-??-??.md"))
88
+ return sorted(files, reverse=True)
89
+
90
+ def get_memory_context(self) -> str:
91
+ """
92
+ Get memory context for the agent.
93
+
94
+ Returns:
95
+ Formatted memory context including long-term and recent memories.
96
+ """
97
+ parts = []
98
+
99
+ # Long-term memory
100
+ long_term = self.read_long_term()
101
+ if long_term:
102
+ parts.append("## Long-term Memory\n" + long_term)
103
+
104
+ # Today's notes
105
+ today = self.read_today()
106
+ if today:
107
+ parts.append("## Today's Notes\n" + today)
108
+
109
+ return "\n\n".join(parts) if parts else ""
@@ -0,0 +1,259 @@
1
+ """Skills loader for agent capabilities."""
2
+
3
+ import json
4
+ import os
5
+ import platform
6
+ import re
7
+ import shutil
8
+ from pathlib import Path
9
+
10
+ # Default builtin skills directory (relative to this file)
11
+ BUILTIN_SKILLS_DIR = Path(__file__).parent.parent / "skills"
12
+
13
+ # Managed skills directory (installed via flowly-hub)
14
+ MANAGED_SKILLS_DIR = Path.home() / ".flowly" / "skills"
15
+
16
+
17
+ class SkillsLoader:
18
+ """
19
+ Loader for agent skills.
20
+
21
+ Skills are markdown files (SKILL.md) that teach the agent how to use
22
+ specific tools or perform certain tasks.
23
+
24
+ Loading priority (highest to lowest):
25
+ 1. Workspace skills (<workspace>/skills/)
26
+ 2. Managed skills (~/.flowly/skills/) - installed via flowly-hub
27
+ 3. Builtin skills (bundled with package)
28
+ """
29
+
30
+ def __init__(self, workspace: Path, builtin_skills_dir: Path | None = None):
31
+ self.workspace = workspace
32
+ self.workspace_skills = workspace / "skills"
33
+ self.managed_skills = MANAGED_SKILLS_DIR
34
+ self.builtin_skills = builtin_skills_dir or BUILTIN_SKILLS_DIR
35
+
36
+ def list_skills(self, filter_unavailable: bool = True) -> list[dict[str, str]]:
37
+ """
38
+ List all available skills.
39
+
40
+ Args:
41
+ filter_unavailable: If True, filter out skills with unmet requirements.
42
+
43
+ Returns:
44
+ List of skill info dicts with 'name', 'path', 'source'.
45
+ """
46
+ skills = []
47
+
48
+ # Workspace skills (highest priority)
49
+ if self.workspace_skills.exists():
50
+ for skill_dir in self.workspace_skills.iterdir():
51
+ if skill_dir.is_dir():
52
+ skill_file = skill_dir / "SKILL.md"
53
+ if skill_file.exists():
54
+ skills.append({"name": skill_dir.name, "path": str(skill_file), "source": "workspace"})
55
+
56
+ # Managed skills (installed via flowly-hub)
57
+ if self.managed_skills.exists():
58
+ for skill_dir in self.managed_skills.iterdir():
59
+ if skill_dir.is_dir():
60
+ skill_file = skill_dir / "SKILL.md"
61
+ if skill_file.exists() and not any(s["name"] == skill_dir.name for s in skills):
62
+ skills.append({"name": skill_dir.name, "path": str(skill_file), "source": "managed"})
63
+
64
+ # Built-in skills
65
+ if self.builtin_skills and self.builtin_skills.exists():
66
+ for skill_dir in self.builtin_skills.iterdir():
67
+ if skill_dir.is_dir():
68
+ skill_file = skill_dir / "SKILL.md"
69
+ if skill_file.exists() and not any(s["name"] == skill_dir.name for s in skills):
70
+ skills.append({"name": skill_dir.name, "path": str(skill_file), "source": "builtin"})
71
+
72
+ # Filter by requirements
73
+ if filter_unavailable:
74
+ return [s for s in skills if self._check_requirements(self._get_skill_meta(s["name"]))]
75
+ return skills
76
+
77
+ def load_skill(self, name: str) -> str | None:
78
+ """
79
+ Load a skill by name.
80
+
81
+ Args:
82
+ name: Skill name (directory name).
83
+
84
+ Returns:
85
+ Skill content or None if not found.
86
+ """
87
+ # Check workspace first (highest priority)
88
+ workspace_skill = self.workspace_skills / name / "SKILL.md"
89
+ if workspace_skill.exists():
90
+ return workspace_skill.read_text(encoding="utf-8")
91
+
92
+ # Check managed skills (installed via flowly-hub)
93
+ managed_skill = self.managed_skills / name / "SKILL.md"
94
+ if managed_skill.exists():
95
+ return managed_skill.read_text(encoding="utf-8")
96
+
97
+ # Check built-in (lowest priority)
98
+ if self.builtin_skills:
99
+ builtin_skill = self.builtin_skills / name / "SKILL.md"
100
+ if builtin_skill.exists():
101
+ return builtin_skill.read_text(encoding="utf-8")
102
+
103
+ return None
104
+
105
+ def load_skills_for_context(self, skill_names: list[str]) -> str:
106
+ """
107
+ Load specific skills for inclusion in agent context.
108
+
109
+ Args:
110
+ skill_names: List of skill names to load.
111
+
112
+ Returns:
113
+ Formatted skills content.
114
+ """
115
+ parts = []
116
+ for name in skill_names:
117
+ content = self.load_skill(name)
118
+ if content:
119
+ content = self._strip_frontmatter(content)
120
+ parts.append(f"### Skill: {name}\n\n{content}")
121
+
122
+ return "\n\n---\n\n".join(parts) if parts else ""
123
+
124
+ def build_skills_summary(self) -> str:
125
+ """
126
+ Build a summary of all skills (name, description, path, availability).
127
+
128
+ This is used for progressive loading - the agent can read the full
129
+ skill content using read_file when needed.
130
+
131
+ Returns:
132
+ XML-formatted skills summary.
133
+ """
134
+ all_skills = self.list_skills(filter_unavailable=False)
135
+ if not all_skills:
136
+ return ""
137
+
138
+ def escape_xml(s: str) -> str:
139
+ return s.replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;")
140
+
141
+ lines = ["<skills>"]
142
+ for s in all_skills:
143
+ name = escape_xml(s["name"])
144
+ path = s["path"]
145
+ desc = escape_xml(self._get_skill_description(s["name"]))
146
+ skill_meta = self._get_skill_meta(s["name"])
147
+ available = self._check_requirements(skill_meta)
148
+
149
+ lines.append(f" <skill available=\"{str(available).lower()}\">")
150
+ lines.append(f" <name>{name}</name>")
151
+ lines.append(f" <description>{desc}</description>")
152
+ lines.append(f" <location>{path}</location>")
153
+
154
+ # Show missing requirements for unavailable skills
155
+ if not available:
156
+ missing = self._get_missing_requirements(skill_meta)
157
+ if missing:
158
+ lines.append(f" <requires>{escape_xml(missing)}</requires>")
159
+
160
+ lines.append(f" </skill>")
161
+ lines.append("</skills>")
162
+
163
+ return "\n".join(lines)
164
+
165
+ def _get_missing_requirements(self, skill_meta: dict) -> str:
166
+ """Get a description of missing requirements."""
167
+ missing = []
168
+ requires = skill_meta.get("requires", {})
169
+ for b in requires.get("bins", []):
170
+ if not shutil.which(b):
171
+ missing.append(f"CLI: {b}")
172
+ for env in requires.get("env", []):
173
+ if not os.environ.get(env):
174
+ missing.append(f"ENV: {env}")
175
+ return ", ".join(missing)
176
+
177
+ def _get_skill_description(self, name: str) -> str:
178
+ """Get the description of a skill from its frontmatter."""
179
+ meta = self.get_skill_metadata(name)
180
+ if meta and meta.get("description"):
181
+ return meta["description"]
182
+ return name # Fallback to skill name
183
+
184
+ def _strip_frontmatter(self, content: str) -> str:
185
+ """Remove YAML frontmatter from markdown content."""
186
+ if content.startswith("---"):
187
+ match = re.match(r"^---\n.*?\n---\n", content, re.DOTALL)
188
+ if match:
189
+ return content[match.end():].strip()
190
+ return content
191
+
192
+ def _parse_flowly_metadata(self, raw: str) -> dict:
193
+ """Parse flowly metadata JSON from frontmatter."""
194
+ try:
195
+ data = json.loads(raw)
196
+ return data.get("flowly", {}) if isinstance(data, dict) else {}
197
+ except (json.JSONDecodeError, TypeError):
198
+ return {}
199
+
200
+ def _check_requirements(self, skill_meta: dict) -> bool:
201
+ """Check if skill requirements are met (bins, env vars, OS)."""
202
+ # Check OS compatibility
203
+ supported_os = skill_meta.get("os", [])
204
+ if supported_os:
205
+ current_os = platform.system().lower()
206
+ os_map = {"windows": "windows", "darwin": "darwin", "linux": "linux"}
207
+ if os_map.get(current_os, current_os) not in supported_os:
208
+ return False
209
+
210
+ requires = skill_meta.get("requires", {})
211
+ for b in requires.get("bins", []):
212
+ if not shutil.which(b):
213
+ return False
214
+ for env in requires.get("env", []):
215
+ if not os.environ.get(env):
216
+ return False
217
+ return True
218
+
219
+ def _get_skill_meta(self, name: str) -> dict:
220
+ """Get flowly metadata for a skill (cached in frontmatter)."""
221
+ meta = self.get_skill_metadata(name) or {}
222
+ return self._parse_flowly_metadata(meta.get("metadata", ""))
223
+
224
+ def get_always_skills(self) -> list[str]:
225
+ """Get skills marked as always=true that meet requirements."""
226
+ result = []
227
+ for s in self.list_skills(filter_unavailable=True):
228
+ meta = self.get_skill_metadata(s["name"]) or {}
229
+ skill_meta = self._parse_flowly_metadata(meta.get("metadata", ""))
230
+ if skill_meta.get("always") or meta.get("always"):
231
+ result.append(s["name"])
232
+ return result
233
+
234
+ def get_skill_metadata(self, name: str) -> dict | None:
235
+ """
236
+ Get metadata from a skill's frontmatter.
237
+
238
+ Args:
239
+ name: Skill name.
240
+
241
+ Returns:
242
+ Metadata dict or None.
243
+ """
244
+ content = self.load_skill(name)
245
+ if not content:
246
+ return None
247
+
248
+ if content.startswith("---"):
249
+ match = re.match(r"^---\n(.*?)\n---", content, re.DOTALL)
250
+ if match:
251
+ # Simple YAML parsing
252
+ metadata = {}
253
+ for line in match.group(1).split("\n"):
254
+ if ":" in line:
255
+ key, value = line.split(":", 1)
256
+ metadata[key.strip()] = value.strip().strip('"\'')
257
+ return metadata
258
+
259
+ return None
@@ -0,0 +1,249 @@
1
+ """Subagent manager for background task execution."""
2
+
3
+ import asyncio
4
+ import json
5
+ import uuid
6
+ from pathlib import Path
7
+ from typing import Any
8
+
9
+ from loguru import logger
10
+
11
+ from flowly_code.bus.events import InboundMessage
12
+ from flowly_code.bus.queue import MessageBus
13
+ from flowly_code.providers.base import LLMProvider
14
+ from flowly_code.agent.tools.registry import ToolRegistry
15
+ from flowly_code.agent.tools.filesystem import ReadFileTool, WriteFileTool, ListDirTool
16
+ from flowly_code.agent.tools.shell import ExecTool
17
+ from flowly_code.agent.tools.web import WebSearchTool, WebFetchTool
18
+
19
+
20
+ class SubagentManager:
21
+ """
22
+ Manages background subagent execution.
23
+
24
+ Subagents are lightweight agent instances that run in the background
25
+ to handle specific tasks. They share the same LLM provider but have
26
+ isolated context and a focused system prompt.
27
+ """
28
+
29
+ def __init__(
30
+ self,
31
+ provider: LLMProvider,
32
+ workspace: Path,
33
+ bus: MessageBus,
34
+ model: str | None = None,
35
+ brave_api_key: str | None = None,
36
+ activity_bus: "ActivityBus | None" = None,
37
+ ):
38
+ self.provider = provider
39
+ self.workspace = workspace
40
+ self.bus = bus
41
+ self.model = model or provider.get_default_model()
42
+ self.brave_api_key = brave_api_key
43
+ self.activity_bus = activity_bus
44
+ self._running_tasks: dict[str, asyncio.Task[None]] = {}
45
+
46
+ def _emit_activity(self, event_type: str, **kwargs: Any) -> None:
47
+ """Emit an activity event if bus is available and has subscribers."""
48
+ if not self.activity_bus or not self.activity_bus.has_subscribers:
49
+ return
50
+ try:
51
+ from flowly_code.activity.events import ActivityEvent
52
+ kwargs.setdefault("agent_name", kwargs.get("subagent_label", "subagent"))
53
+ self.activity_bus.emit(ActivityEvent(type=event_type, **kwargs))
54
+ except Exception:
55
+ pass
56
+
57
+ async def spawn(
58
+ self,
59
+ task: str,
60
+ label: str | None = None,
61
+ origin_channel: str = "cli",
62
+ origin_chat_id: str = "direct",
63
+ ) -> str:
64
+ """
65
+ Spawn a subagent to execute a task in the background.
66
+
67
+ Args:
68
+ task: The task description for the subagent.
69
+ label: Optional human-readable label for the task.
70
+ origin_channel: The channel to announce results to.
71
+ origin_chat_id: The chat ID to announce results to.
72
+
73
+ Returns:
74
+ Status message indicating the subagent was started.
75
+ """
76
+ task_id = str(uuid.uuid4())[:8]
77
+ display_label = label or task[:30] + ("..." if len(task) > 30 else "")
78
+
79
+ origin = {
80
+ "channel": origin_channel,
81
+ "chat_id": origin_chat_id,
82
+ }
83
+
84
+ # Create background task
85
+ bg_task = asyncio.create_task(
86
+ self._run_subagent(task_id, task, display_label, origin)
87
+ )
88
+ self._running_tasks[task_id] = bg_task
89
+
90
+ # Cleanup when done
91
+ bg_task.add_done_callback(lambda _: self._running_tasks.pop(task_id, None))
92
+
93
+ logger.info(f"Spawned subagent [{task_id}]: {display_label}")
94
+ self._emit_activity("subagent_spawn", subagent_id=task_id, subagent_label=display_label)
95
+ return f"Subagent [{display_label}] started (id: {task_id}). I'll notify you when it completes."
96
+
97
+ async def _run_subagent(
98
+ self,
99
+ task_id: str,
100
+ task: str,
101
+ label: str,
102
+ origin: dict[str, str],
103
+ ) -> None:
104
+ """Execute the subagent task and announce the result."""
105
+ logger.info(f"Subagent [{task_id}] starting task: {label}")
106
+
107
+ try:
108
+ # Build subagent tools (no message tool, no spawn tool)
109
+ tools = ToolRegistry()
110
+ tools.register(ReadFileTool())
111
+ tools.register(WriteFileTool())
112
+ tools.register(ListDirTool())
113
+ tools.register(ExecTool(working_dir=str(self.workspace)))
114
+ tools.register(WebSearchTool(api_key=self.brave_api_key))
115
+ tools.register(WebFetchTool())
116
+
117
+ # Build messages with subagent-specific prompt
118
+ system_prompt = self._build_subagent_prompt(task)
119
+ messages: list[dict[str, Any]] = [
120
+ {"role": "system", "content": system_prompt},
121
+ {"role": "user", "content": task},
122
+ ]
123
+
124
+ # Run agent loop (limited iterations)
125
+ max_iterations = 15
126
+ iteration = 0
127
+ final_result: str | None = None
128
+
129
+ while iteration < max_iterations:
130
+ iteration += 1
131
+
132
+ response = await self.provider.chat(
133
+ messages=messages,
134
+ tools=tools.get_definitions(),
135
+ model=self.model,
136
+ )
137
+
138
+ if response.has_tool_calls:
139
+ # Add assistant message with tool calls
140
+ tool_call_dicts = [
141
+ {
142
+ "id": tc.id,
143
+ "type": "function",
144
+ "function": {
145
+ "name": tc.name,
146
+ "arguments": json.dumps(tc.arguments),
147
+ },
148
+ }
149
+ for tc in response.tool_calls
150
+ ]
151
+ messages.append({
152
+ "role": "assistant",
153
+ "content": response.content or "",
154
+ "tool_calls": tool_call_dicts,
155
+ })
156
+
157
+ # Execute tools
158
+ for tool_call in response.tool_calls:
159
+ logger.debug(f"Subagent [{task_id}] executing: {tool_call.name}")
160
+ result = await tools.execute(tool_call.name, tool_call.arguments)
161
+ messages.append({
162
+ "role": "tool",
163
+ "tool_call_id": tool_call.id,
164
+ "name": tool_call.name,
165
+ "content": result,
166
+ })
167
+ else:
168
+ final_result = response.content
169
+ break
170
+
171
+ if final_result is None:
172
+ final_result = "Task completed but no final response was generated."
173
+
174
+ logger.info(f"Subagent [{task_id}] completed successfully")
175
+ self._emit_activity("subagent_end", subagent_id=task_id, subagent_label=label, success=True)
176
+ await self._announce_result(task_id, label, task, final_result, origin, "ok")
177
+
178
+ except Exception as e:
179
+ error_msg = f"Error: {str(e)}"
180
+ logger.error(f"Subagent [{task_id}] failed: {e}")
181
+ self._emit_activity("subagent_end", subagent_id=task_id, subagent_label=label, success=False, error_message=str(e))
182
+ await self._announce_result(task_id, label, task, error_msg, origin, "error")
183
+
184
+ async def _announce_result(
185
+ self,
186
+ task_id: str,
187
+ label: str,
188
+ task: str,
189
+ result: str,
190
+ origin: dict[str, str],
191
+ status: str,
192
+ ) -> None:
193
+ """Announce the subagent result to the main agent via the message bus."""
194
+ status_text = "completed successfully" if status == "ok" else "failed"
195
+
196
+ announce_content = f"""[Subagent '{label}' {status_text}]
197
+
198
+ Task: {task}
199
+
200
+ Result:
201
+ {result}
202
+
203
+ Summarize this naturally for the user. Keep it brief (1-2 sentences). Do not mention technical details like "subagent" or task IDs."""
204
+
205
+ # Inject as system message to trigger main agent
206
+ msg = InboundMessage(
207
+ channel="system",
208
+ sender_id="subagent",
209
+ chat_id=f"{origin['channel']}:{origin['chat_id']}",
210
+ content=announce_content,
211
+ )
212
+
213
+ await self.bus.publish_inbound(msg)
214
+ logger.debug(f"Subagent [{task_id}] announced result to {origin['channel']}:{origin['chat_id']}")
215
+
216
+ def _build_subagent_prompt(self, task: str) -> str:
217
+ """Build a focused system prompt for the subagent."""
218
+ return f"""# Subagent
219
+
220
+ You are a subagent spawned by the main agent to complete a specific task.
221
+
222
+ ## Your Task
223
+ {task}
224
+
225
+ ## Rules
226
+ 1. Stay focused - complete only the assigned task, nothing else
227
+ 2. Your final response will be reported back to the main agent
228
+ 3. Do not initiate conversations or take on side tasks
229
+ 4. Be concise but informative in your findings
230
+
231
+ ## What You Can Do
232
+ - Read and write files in the workspace
233
+ - Execute shell commands
234
+ - Search the web and fetch web pages
235
+ - Complete the task thoroughly
236
+
237
+ ## What You Cannot Do
238
+ - Send messages directly to users (no message tool available)
239
+ - Spawn other subagents
240
+ - Access the main agent's conversation history
241
+
242
+ ## Workspace
243
+ Your workspace is at: {self.workspace}
244
+
245
+ When you have completed the task, provide a clear summary of your findings or actions."""
246
+
247
+ def get_running_count(self) -> int:
248
+ """Return the number of currently running subagents."""
249
+ return len(self._running_tasks)
@@ -0,0 +1,6 @@
1
+ """Agent tools module."""
2
+
3
+ from flowly_code.agent.tools.base import Tool
4
+ from flowly_code.agent.tools.registry import ToolRegistry
5
+
6
+ __all__ = ["Tool", "ToolRegistry"]
@@ -0,0 +1,55 @@
1
+ """Base class for agent tools."""
2
+
3
+ from abc import ABC, abstractmethod
4
+ from typing import Any
5
+
6
+
7
+ class Tool(ABC):
8
+ """
9
+ Abstract base class for agent tools.
10
+
11
+ Tools are capabilities that the agent can use to interact with
12
+ the environment, such as reading files, executing commands, etc.
13
+ """
14
+
15
+ @property
16
+ @abstractmethod
17
+ def name(self) -> str:
18
+ """Tool name used in function calls."""
19
+ pass
20
+
21
+ @property
22
+ @abstractmethod
23
+ def description(self) -> str:
24
+ """Description of what the tool does."""
25
+ pass
26
+
27
+ @property
28
+ @abstractmethod
29
+ def parameters(self) -> dict[str, Any]:
30
+ """JSON Schema for tool parameters."""
31
+ pass
32
+
33
+ @abstractmethod
34
+ async def execute(self, **kwargs: Any) -> str:
35
+ """
36
+ Execute the tool with given parameters.
37
+
38
+ Args:
39
+ **kwargs: Tool-specific parameters.
40
+
41
+ Returns:
42
+ String result of the tool execution.
43
+ """
44
+ pass
45
+
46
+ def to_schema(self) -> dict[str, Any]:
47
+ """Convert tool to OpenAI function schema format."""
48
+ return {
49
+ "type": "function",
50
+ "function": {
51
+ "name": self.name,
52
+ "description": self.description,
53
+ "parameters": self.parameters,
54
+ }
55
+ }