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
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
from openhands.agent_server.mcp_oauth_store import (
create_settings_backed_mcp_tool_provider,
)
from openhands.agent_server.message_sender import sender_message_header
from openhands.agent_server.message_sender import sender_message_line
from openhands.agent_server.models import (
ConversationInfo,
ConversationPage,
Expand All @@ -71,7 +71,7 @@
run_store,
)
from openhands.agent_server.project_service import ProjectService
from openhands.agent_server.project_wake import project_sender_header
from openhands.agent_server.project_wake import project_sender_line
from openhands.agent_server.pub_sub import Subscriber
from openhands.agent_server.server_details_router import update_last_execution_time
from openhands.agent_server.telemetry import (
Expand Down Expand Up @@ -1678,11 +1678,14 @@ async def _create_conversation(
content = list(initial_message.content)
sender_id = initial_message.sender_conversation_id
if sender_id is not None:
header = await sender_message_header(self, sender_id, conversation_id)
header = (
await project_sender_header(self, sender_id, request.tags) or header
sender_line = await sender_message_line(
self, sender_id, conversation_id
)
content.insert(0, TextContent(text=header))
sender_line = (
await project_sender_line(self, sender_id, request.tags)
or sender_line
)
content.insert(0, TextContent(text=sender_line))
initial_sender = conversation_sender(sender_id)
first_message = Message(role=initial_message.role, content=content)

Expand Down
14 changes: 7 additions & 7 deletions openhands-agent-server/openhands/agent_server/event_router.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
host_experimental_features,
is_enabled,
)
from openhands.agent_server.message_sender import SenderError, sender_message_header
from openhands.agent_server.message_sender import SenderError, sender_message_line
from openhands.agent_server.models import (
AskUserAnswerRequest,
ConfirmationResponseRequest,
Expand All @@ -46,7 +46,7 @@
SettingsStore,
run_store,
)
from openhands.agent_server.project_wake import project_sender_header
from openhands.agent_server.project_wake import project_sender_line
from openhands.agent_server.server_context import ServerContext, get_server_context
from openhands.sdk import Message, TextContent
from openhands.sdk.conversation.state import ConversationExecutionStatus
Expand Down Expand Up @@ -297,22 +297,22 @@ async def send_message(
if context.conversation_service is None:
raise Unavailable("Conversation service is not available")
try:
header = await sender_message_header(
sender_line = await sender_message_line(
context.conversation_service,
request.sender_conversation_id,
conversation_id,
)
header = (
await project_sender_header(
sender_line = (
await project_sender_line(
context.conversation_service,
request.sender_conversation_id,
event_service.stored.tags,
)
or header
or sender_line
)
except SenderError as e:
raise Unprocessable(str(e))
content.insert(0, TextContent(text=header))
content.insert(0, TextContent(text=sender_line))
sender = conversation_sender(request.sender_conversation_id)
message = Message(role=request.role, content=content)
# The inner gateway route holds the lock for its whole turn; refuse before
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class SenderError(ValueError):
"""The ``sender_conversation_id`` of a message cannot send it."""


def sender_header(title: str | None, conversation_id: UUID) -> str:
def sender_line(title: str | None, conversation_id: UUID) -> str:
"""First line of a message one conversation sends another.

The sender is a conversation mention, so the receiving agent can copy it
Expand All @@ -28,7 +28,7 @@ def sender_header(title: str | None, conversation_id: UUID) -> str:
)


async def sender_message_header(
async def sender_message_line(
service: "ConversationService", sender_id: UUID, target_id: UUID
) -> str:
"""The line a message from ``sender_id`` to ``target_id`` opens with.
Expand All @@ -41,4 +41,4 @@ async def sender_message_header(
sender = await service.get_conversation(sender_id)
if sender is None:
raise SenderError("Sender conversation not found")
return sender_header(sender.title, sender_id)
return sender_line(sender.title, sender_id)
22 changes: 11 additions & 11 deletions openhands-agent-server/openhands/agent_server/project_prompts.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

- :func:`project_context` is the ``<PROJECT>`` block of a coordinator or
worker: the project's name and goal, its coordinator, and its memory index.
- :func:`sender_header` opens every message one conversation sends another,
- :func:`sender_line` opens every message one conversation sends another,
including a worker's brief, because a summary of earlier events can
otherwise lose who wrote it.
- :func:`update_header` opens the update a coordinator receives when one of
- :func:`update_line` opens the update a coordinator receives when one of
its project's conversations finishes a run.
"""

Expand All @@ -28,14 +28,14 @@ def _name(title: str | None, conversation_id: UUID) -> str:
# A conversation mention, ``[Title](conversation:<hex>)``: the agent can
# copy it into its own messages, and UIs render it as a link. The title is
# sanitized there (one line, no brackets, parentheses or quotes), so it
# cannot end the header or the guidance early.
# cannot end the line or the guidance early.
return conversation_mention(title, conversation_id)


SenderRole = Literal["coordinator", "worker"]
"""How a sender in the receiver's project relates to it: the project's
coordinator, or another conversation of the project. A sender outside any
project gets :func:`openhands.agent_server.message_sender.sender_header`."""
project gets :func:`openhands.agent_server.message_sender.sender_line`."""

_SENDER_ROLES: dict[SenderRole, str] = {
"coordinator": "the coordinator of your project",
Expand All @@ -51,7 +51,7 @@ def _name(title: str | None, conversation_id: UUID) -> str:
}


def sender_header(title: str | None, conversation_id: UUID, *, role: SenderRole) -> str:
def sender_line(title: str | None, conversation_id: UUID, *, role: SenderRole) -> str:
"""First line of a message a conversation sends another of its project."""
return (
f"[Sent by {_name(title, conversation_id)}, {_SENDER_ROLES[role]}, not "
Expand All @@ -64,7 +64,7 @@ def sender_header(title: str | None, conversation_id: UUID, *, role: SenderRole)
"""Longest final message passed on in an update; the rest is cut."""


def update_header(title: str | None, conversation_id: UUID, *, asked: bool) -> str:
def update_line(title: str | None, conversation_id: UUID, *, asked: bool) -> str:
"""First line of the update a coordinator gets when a member finishes."""
if asked:
return (
Expand All @@ -87,7 +87,7 @@ def update_header(title: str | None, conversation_id: UUID, *, asked: bool) -> s
)


def card_header(title: str | None, conversation_id: UUID) -> str:
def card_line(title: str | None, conversation_id: UUID) -> str:
"""First line of the update a coordinator gets when a member it started
ends its turn on an ``ask_user`` card: the user answers it through the
coordinator."""
Expand Down Expand Up @@ -117,7 +117,7 @@ def card_header(title: str | None, conversation_id: UUID) -> str:
}


def stop_header(title: str | None, conversation_id: UUID, *, status: str) -> str:
def stop_line(title: str | None, conversation_id: UUID, *, status: str) -> str:
"""First line of the update a coordinator gets when a member it started
stops on the user (confirmation, error or stuck)."""
outcome = _STOPS[status]
Expand All @@ -136,7 +136,7 @@ def stop_header(title: str | None, conversation_id: UUID, *, status: str) -> str
without a run and do not start a turn: the board ignores them."""


def user_message_header(title: str | None, conversation_id: UUID) -> str:
def user_message_line(title: str | None, conversation_id: UUID) -> str:
"""First line of the copy a coordinator gets of the user's message to a member."""
return (
f"{USER_MESSAGE_PREFIX}{_name(title, conversation_id)}, a "
Expand All @@ -146,7 +146,7 @@ def user_message_header(title: str | None, conversation_id: UUID) -> str:
)


def user_turn_header(title: str | None, conversation_id: UUID, *, asked: bool) -> str:
def user_turn_line(title: str | None, conversation_id: UUID, *, asked: bool) -> str:
"""First line of a member's final message on a turn the user started."""
outcome = "with a question for the user" if asked else "of a turn"
return (
Expand All @@ -157,7 +157,7 @@ def user_turn_header(title: str | None, conversation_id: UUID, *, asked: bool) -
)


def paused_header(title: str | None, conversation_id: UUID) -> str:
def paused_line(title: str | None, conversation_id: UUID) -> str:
"""First line of the note a coordinator gets when the user pauses a
member during a turn the coordinator started."""
return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ async def _project_prompt(
worker, or ``None`` for the coordinator, whose id is ``coordinator_id``.
Returns the block's body (``None``
when the project cannot be read) and how the starter relates to the
project, for the header of the worker's brief.
project, for the sender line of the worker's brief.
"""
try:
project = (
Expand Down Expand Up @@ -720,7 +720,7 @@ async def _start_worker_from_profile(
It keeps what makes it part of the project: the ``project`` tag, the
coordinator's workspace (in a worktree when asked), the parent link to
the coordinator, the user's confirmation policy, plugins, hooks and
secrets, the coordinator as sender of the task (with a header saying
secrets, the coordinator as sender of the task (with a sender line saying
so), the worker's prompt and project context, and the project tools a
worker uses. It does not take the coordinator's agent, launch
additions or client tools.
Expand All @@ -744,7 +744,7 @@ async def _start_worker_from_profile(
secrets_encrypted=False,
client_tools=[],
agent_launch_additions=None,
# The coordinator sends the task, which opens with its header.
# The coordinator sends the task, which opens with its sender line.
initial_message=SendMessageRequest(
role="user",
content=[TextContent(text=request.task)],
Expand Down
46 changes: 23 additions & 23 deletions openhands-agent-server/openhands/agent_server/project_wake.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Messages one conversation sends another, and the coordinator's wake-up.

:func:`project_sender_header` holds a project member's
:func:`project_sender_line` holds a project member's
``sender_conversation_id`` to its project and writes the line its message
opens with. :func:`notify_coordinator` wakes a project's coordinator when one of its
conversations finishes a run the coordinator asked for.

The coordinator gets the member's final message, not its events: one user
message that records the member as ``sender`` and opens with
:func:`~openhands.agent_server.project_prompts.update_header`. A member the
:func:`~openhands.agent_server.project_prompts.update_line`. A member the
coordinator started that stops on the user instead (confirmation, error,
stuck) sends :func:`~openhands.agent_server.project_prompts.stop_header`, and a
stuck) sends :func:`~openhands.agent_server.project_prompts.stop_line`, and a
pause is saved for awareness (:func:`notify_coordinator_of_stop`). An update
to a coordinator that waits for confirmation is saved without a run, since a
run would approve its pending actions. The coordinator reads more with
Expand Down Expand Up @@ -40,14 +40,14 @@
from openhands.agent_server.persistence import PersistedProjects
from openhands.agent_server.project_prompts import (
SenderRole,
card_header,
paused_header,
sender_header,
stop_header,
update_header,
card_line,
paused_line,
sender_line,
stop_line,
update_line,
update_text,
user_message_header,
user_turn_header,
user_message_line,
user_turn_line,
)
from openhands.agent_server.project_turns import (
TurnOutcome,
Expand Down Expand Up @@ -107,13 +107,13 @@ async def _coordinator_id(
return project.coordinator_conversation_id if project else None


async def project_sender_header(
async def project_sender_line(
service: "ConversationService", sender_id: UUID, target_tags: dict[str, str]
) -> str | None:
"""The line a message from a project member opens with, or ``None`` when
the sender is in no project (the generic header applies).
the sender is in no project (the generic sender line applies).

Called beside :func:`~openhands.agent_server.message_sender.sender_message_header`,
Called beside :func:`~openhands.agent_server.message_sender.sender_message_line`,
which checks the sender exists and is not the target.

Raises :class:`SenderError` when the target is not in the sender's
Expand All @@ -132,7 +132,7 @@ async def project_sender_header(
raise SenderError("Sender is not a conversation of this project")
coordinator = project.coordinator_conversation_id == sender_id
role: SenderRole = "coordinator" if coordinator else "worker"
return sender_header(sender.title, sender_id, role=role)
return sender_line(sender.title, sender_id, role=role)


async def _coordinator(
Expand Down Expand Up @@ -276,16 +276,16 @@ async def notify_coordinator(
if member_events is not None:
card = await asyncio.to_thread(member_events.pending_ask)
if by_user:
header = user_turn_header(title, member_id, asked=asked)
line = user_turn_line(title, member_id, asked=asked)
elif card is not None:
header = card_header(title, member_id)
line = card_line(title, member_id)
else:
header = update_header(title, member_id, asked=asked)
line = update_line(title, member_id, asked=asked)
await _deliver(
coordinator_id,
coordinator,
member_id,
[TextContent(text=header), TextContent(text=update_text(text))],
[TextContent(text=line), TextContent(text=update_text(text))],
run=not by_user,
)
return True
Expand Down Expand Up @@ -324,16 +324,16 @@ async def notify_coordinator_of_stop(
# delivers this, with the status the member comes back in.
return False
paused = status == "paused"
header = (
paused_header(title, member_id)
line = (
paused_line(title, member_id)
if paused
else stop_header(title, member_id, status=status)
else stop_line(title, member_id, status=status)
)
await _deliver(
coordinator_id,
coordinator,
member_id,
[TextContent(text=header), TextContent(text=update_text(text))],
[TextContent(text=line), TextContent(text=update_text(text))],
run=not paused,
)
return True
Expand Down Expand Up @@ -365,7 +365,7 @@ async def relay_user_message(
member_id,
[
TextContent(
text=user_message_header(member.title if member else None, member_id)
text=user_message_line(member.title if member else None, member_id)
),
TextContent(text=update_text(text)),
],
Expand Down
Loading
Loading