Skip to content

Commit 0df87bf

Browse files
authored
[Feat]: Tell the agent the current date (#7509)
1 parent 8e9eb7d commit 0df87bf

File tree

7 files changed

+21
-2
lines changed

7 files changed

+21
-2
lines changed

openhands/agenthub/codeact_agent/prompts/additional_info.j2

+3
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,8 @@ For example, if you are using vite.config.js, you should set server.host and ser
2424
{% if runtime_info.additional_agent_instructions %}
2525
{{ runtime_info.additional_agent_instructions }}
2626
{% endif %}
27+
{% if runtime_info.date %}
28+
Today's date is {{ runtime_info.date }} (UTC).
29+
{% endif %}
2730
</RUNTIME_INFORMATION>
2831
{% endif %}

openhands/events/observation/agent.py

+2
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ class RecallObservation(Observation):
7272
repo_instructions: str = ''
7373
runtime_hosts: dict[str, int] = field(default_factory=dict)
7474
additional_agent_instructions: str = ''
75+
date: str = ''
7576

7677
# knowledge
7778
microagent_knowledge: list[MicroagentKnowledge] = field(default_factory=list)
@@ -112,6 +113,7 @@ def __str__(self) -> str:
112113
f'repo_instructions={self.repo_instructions[:20]}...',
113114
f'runtime_hosts={self.runtime_hosts}',
114115
f'additional_agent_instructions={self.additional_agent_instructions[:20]}...',
116+
f'date={self.date}'
115117
]
116118
)
117119
else:

openhands/memory/conversation_memory.py

+4-1
Original file line numberDiff line numberDiff line change
@@ -399,13 +399,16 @@ def _process_observation(
399399
else:
400400
repo_info = None
401401

402+
date = obs.date
403+
402404
if obs.runtime_hosts or obs.additional_agent_instructions:
403405
runtime_info = RuntimeInfo(
404406
available_hosts=obs.runtime_hosts,
405407
additional_agent_instructions=obs.additional_agent_instructions,
408+
date=date,
406409
)
407410
else:
408-
runtime_info = None
411+
runtime_info = RuntimeInfo(date=date)
409412

410413
repo_instructions = (
411414
obs.repo_instructions if obs.repo_instructions else ''

openhands/memory/memory.py

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import asyncio
22
import os
33
import uuid
4+
from datetime import datetime, timezone
45
from typing import Callable
56

67
import openhands
@@ -170,6 +171,7 @@ def _on_workspace_context_recall(
170171
else '',
171172
microagent_knowledge=microagent_knowledge,
172173
content='Added workspace context',
174+
date=self.runtime_info.date if self.runtime_info is not None else '',
173175
)
174176
return obs
175177
return None
@@ -263,13 +265,17 @@ def set_repository_info(self, repo_name: str, repo_directory: str) -> None:
263265
def set_runtime_info(self, runtime: Runtime) -> None:
264266
"""Store runtime info (web hosts, ports, etc.)."""
265267
# e.g. { '127.0.0.1': 8080 }
268+
utc_now = datetime.now(timezone.utc)
269+
date = str(utc_now.date())
270+
266271
if runtime.web_hosts or runtime.additional_agent_instructions:
267272
self.runtime_info = RuntimeInfo(
268273
available_hosts=runtime.web_hosts,
269274
additional_agent_instructions=runtime.additional_agent_instructions,
275+
date=date,
270276
)
271277
else:
272-
self.runtime_info = None
278+
self.runtime_info = RuntimeInfo(date=date)
273279

274280
def send_error_message(self, message_id: str, message: str):
275281
"""Sends an error message if the callback function was provided."""

openhands/utils/prompt.py

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
@dataclass
1313
class RuntimeInfo:
14+
date: str
1415
available_hosts: dict[str, int] = field(default_factory=dict)
1516
additional_agent_instructions: str = ''
1617

tests/unit/test_observation_serialization.py

+2
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ def test_microagent_observation_serialization():
245245
'runtime_hosts': {'host1': 8080, 'host2': 8081},
246246
'repo_instructions': 'complex_repo_instructions',
247247
'additional_agent_instructions': 'You know it all about this runtime',
248+
'date': '04/12/1023',
248249
'microagent_knowledge': [],
249250
},
250251
}
@@ -263,6 +264,7 @@ def test_microagent_observation_microagent_knowledge_serialization():
263264
'repo_instructions': '',
264265
'runtime_hosts': {},
265266
'additional_agent_instructions': '',
267+
'date': '',
266268
'microagent_knowledge': [
267269
{
268270
'name': 'microagent1',

tests/unit/test_prompt_manager.py

+2
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,7 @@ def test_build_workspace_context_with_repo_and_runtime(prompt_dir):
239239
# Create repository and runtime information
240240
repo_info = RepositoryInfo(repo_name='owner/repo', repo_directory='/workspace/repo')
241241
runtime_info = RuntimeInfo(
242+
date='02/12/1232',
242243
available_hosts={'example.com': 8080},
243244
additional_agent_instructions='You know everything about this runtime.',
244245
)
@@ -260,6 +261,7 @@ def test_build_workspace_context_with_repo_and_runtime(prompt_dir):
260261
assert '<RUNTIME_INFORMATION>' in result
261262
assert 'example.com (port 8080)' in result
262263
assert 'You know everything about this runtime.' in result
264+
assert "Today's date is 02/12/1232 (UTC)."
263265

264266
# Clean up
265267
os.remove(os.path.join(prompt_dir, 'additional_info.j2'))

0 commit comments

Comments
 (0)