Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions dimos/agents2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@

from dimos.agents2.agent import Agent
from dimos.agents2.spec import AgentSpec
from dimos.protocol.skill.skill import skill
from dimos.protocol.skill.type import Output, Reducer, Stream
41 changes: 31 additions & 10 deletions dimos/agents2/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@

def toolmsg_from_state(state: SkillState) -> ToolMessage:
if state.skill_config.output != Output.standard:
content = "Special output, see separate message"
content = "output attached in separate messages"
else:
content = state.content()

Expand Down Expand Up @@ -99,6 +99,10 @@ def snapshot_to_messages(
# (images for example, requires to be a HumanMessage)
special_msgs: List[HumanMessage] = []

# for special skills that want to return a separate message that should
# stay in history, like actual human messages, critical events
history_msgs: List[HumanMessage] = []

# Initialize state_msg
state_msg = None

Expand All @@ -109,12 +113,19 @@ def snapshot_to_messages(
if skill_state.call_id in tool_call_ids:
tool_msgs.append(toolmsg_from_state(skill_state))

special_data = skill_state.skill_config.output != Output.standard
if skill_state.skill_config.output == Output.human:
content = skill_state.content()
if not content:
continue
history_msgs.append(HumanMessage(content=content))
continue

special_data = skill_state.skill_config.output == Output.image
if special_data:
content = skill_state.content()
if not content:
continue
special_msgs.append(HumanMessage(content=[content]))
special_msgs.append(HumanMessage(content=content))

if skill_state.call_id in tool_call_ids:
continue
Expand All @@ -127,7 +138,8 @@ def snapshot_to_messages(
)

return {
"tool_msgs": tool_msgs if tool_msgs else [],
"tool_msgs": tool_msgs,
"history_msgs": history_msgs,
"state_msgs": ([state_msg] if state_msg else []) + special_msgs,
}

Expand Down Expand Up @@ -200,9 +212,10 @@ def execute_tool_calls(self, tool_calls: List[ToolCall]) -> None:
def run_implicit_skill(self, skill_name: str, *args, **kwargs) -> None:
self.coordinator.call_skill(False, skill_name, {"args": args, "kwargs": kwargs})

async def agent_loop(self, seed_query: str = ""):
async def agent_loop(self, first_query: str = ""):
self.state_messages = []
self.append_history(HumanMessage(seed_query))
if first_query:
self.append_history(HumanMessage(first_query))

try:
while True:
Expand Down Expand Up @@ -246,19 +259,27 @@ async def agent_loop(self, seed_query: str = ""):
snapshot_msgs = snapshot_to_messages(update, msg.tool_calls)

self.state_messages = snapshot_msgs.get("state_msgs", [])
self.append_history(*snapshot_msgs.get("tool_msgs", []))
self.append_history(
*snapshot_msgs.get("tool_msgs", []), *snapshot_msgs.get("history_msgs", [])
)

except Exception as e:
logger.error(f"Error in agent loop: {e}")
import traceback

traceback.print_exc()

@rpc
def loop_thread(self):
asyncio.run_coroutine_threadsafe(self.agent_loop(), self._loop)
return True

@rpc
def query(self, query: str):
return asyncio.ensure_future(self.agent_loop(query), loop=self._loop)
return asyncio.run_coroutine_threadsafe(self.agent_loop(query), self._loop).result()

def query_async(self, query: str):
return self.agent_loop(query)
async def query_async(self, query: str):
return await self.agent_loop(query)

def register_skills(self, container):
return self.coordinator.register_skills(container)
Expand Down
35 changes: 35 additions & 0 deletions dimos/agents2/cli/human.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
# Copyright 2025 Dimensional Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import queue

from dimos.agents2 import Output, Reducer, Stream, skill
from dimos.core import Module, pLCMTransport


class HumanInput(Module):
running: bool = False

@skill(stream=Stream.call_agent, reducer=Reducer.string, output=Output.human)
def human(self):
"""receives human input, no need to run this, it's running implicitly"""
if self.running:
return "already running"
self.running = True
transport = pLCMTransport("/human_input")

msg_queue = queue.Queue()
transport.subscribe(msg_queue.put)
for message in iter(msg_queue.get, None):
yield message
Loading