File size: 7,062 Bytes
c453128
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
"""Unit tests for agent_gui.activity_parser β€” pure parsing logic, no I/O."""
import json
import pytest
from agent_gui.db import Message
from agent_gui.activity_parser import parse_activity, ActivityEvent


def _msg(
    role: str,
    content: str | None = None,
    tool_calls: list[dict] | None = None,
    tool_name: str | None = None,
    tool_call_id: str | None = None,
    ts: str = "2024-01-01T00:00:00",
    mid: int = 1,
) -> Message:
    return Message(
        id=mid,
        session_id="s1",
        role=role,
        content=content,
        timestamp=ts,
        tool_calls=tool_calls or [],
        tool_name=tool_name,
        tool_call_id=tool_call_id,
    )


# ── parse_activity basics ──────────────────────────────────────────────────────


def test_empty_messages_returns_empty():
    assert parse_activity([]) == []


def test_user_message_produces_event():
    events = parse_activity([_msg("user", content="hello world")])
    assert any(e.event_type == "user_message" for e in events)


def test_assistant_text_message_produces_event():
    events = parse_activity([_msg("assistant", content="I will help you.")])
    types = {e.event_type for e in events}
    assert types & {"message", "user_message"}  # non-empty response produces at least one


def _tc(name: str, args: dict, tc_id: str = "tc1") -> dict:
    """Build an OpenAI-format tool call dict (the schema activity_parser expects)."""
    import json
    return {
        "type": "function",
        "id": tc_id,
        "function": {"name": name, "arguments": json.dumps(args)},
    }


def test_tool_call_produces_tool_event():
    events = parse_activity([_msg("assistant", tool_calls=[_tc("bash", {"command": "ls"})])])
    tool_events = [e for e in events if e.event_type == "tool_call"]
    assert len(tool_events) >= 1
    assert tool_events[0].tool_name == "bash"


def test_tool_result_event():
    # tool role = result from a previous call
    events = parse_activity([
        _msg("tool", content="file.txt\ndir/", tool_name="bash", tool_call_id="tc1"),
    ])
    result_events = [e for e in events if e.event_type == "tool_result"]
    assert len(result_events) >= 1


def test_icons_assigned_for_known_tools():
    events = parse_activity([_msg("assistant", tool_calls=[
        _tc("write_file", {"path": "/workspace/x.py"})
    ])])
    assert any(e.icon == "πŸ“" for e in events)


def test_unknown_tool_gets_default_icon():
    events = parse_activity([_msg("assistant", tool_calls=[
        _tc("some_unknown_tool_xyz", {})
    ])])
    tool_events = [e for e in events if e.event_type == "tool_call"]
    assert any(e.icon == "πŸ”§" for e in tool_events)


def test_workspace_prefix_stripped_from_user_message():
    """The [Your workspace is ...] prefix injected by the server should be stripped."""
    content = "[Your workspace is /workspace/foo β€” task instructions are in TASK.md there.]\n\nActual task text"
    events = parse_activity([_msg("user", content=content)])
    user_events = [e for e in events if e.event_type == "user_message"]
    assert len(user_events) >= 1
    assert "workspace" not in user_events[0].title.lower() or "Actual task" in user_events[0].detail


def test_current_workspace_paths_prefix_stripped():
    """The current "[Workspace paths: … ]" wrapper must be stripped from the feed."""
    content = (
        "[Workspace paths:\n"
        "  - Terminal/bash (inside Docker): /workspace/foo\n"
        "  - vision_analyze and host file tools: /Users/x/foo\n"
        "Use the Docker path for terminal commands; use the host path for vision_analyze.\n"
        "Your task is provided below β€” start working on it immediately without reading any files first.]\n\n"
        "Write a joke about houses"
    )
    events = parse_activity([_msg("user", content=content)])
    user_events = [e for e in events if e.event_type == "user_message"]
    assert len(user_events) == 1
    assert user_events[0].detail == "Write a joke about houses"


def test_compact_workspace_prefix_stripped():
    """The current "[Workspace: …]" wrapper must be stripped from the feed."""
    content = (
        "[Workspace: all tools run inside Docker β€” use /workspace/ paths.\n"
        "  - Workspace root: /workspace/\n"
        "Your task is provided below β€” start working on it immediately "
        "without reading any files first.]\n\n"
        "Build a todo app"
    )
    events = parse_activity([_msg("user", content=content)])
    user_events = [e for e in events if e.event_type == "user_message"]
    assert len(user_events) == 1
    assert user_events[0].detail == "Build a todo app"


def test_attached_image_marker_stripped():
    """The frontend's "[Attached image: …]" marker must not show in the feed."""
    content = "[Attached image: foo.png]\nWhat is in this image"
    events = parse_activity([_msg("user", content=content)])
    user_events = [e for e in events if e.event_type == "user_message"]
    assert len(user_events) == 1
    assert user_events[0].detail == "What is in this image"


def test_workspace_and_attachment_both_stripped():
    content = (
        "[Workspace paths:\n  - x: /workspace/foo\nYour task is provided below.]\n\n"
        "[Attached image: a.png]\nDescribe it"
    )
    events = parse_activity([_msg("user", content=content)])
    user_events = [e for e in events if e.event_type == "user_message"]
    assert user_events[0].detail == "Describe it"


def test_tool_detail_bash_shows_command():
    events = parse_activity([_msg("assistant", tool_calls=[
        _tc("bash", {"command": "echo hello"})
    ])])
    tool_events = [e for e in events if e.event_type == "tool_call"]
    assert any("echo hello" in e.detail for e in tool_events)


def test_tool_detail_write_shows_path():
    events = parse_activity([_msg("assistant", tool_calls=[
        _tc("write_file", {"path": "/workspace/out.py"})
    ])])
    tool_events = [e for e in events if e.event_type == "tool_call"]
    assert any("/workspace/out.py" in e.detail for e in tool_events)


def test_timestamps_preserved():
    msg = _msg("user", content="hi", ts="2024-06-15T12:34:56")
    events = parse_activity([msg])
    assert any("2024-06-15" in e.timestamp for e in events)


def test_multiple_tool_calls_in_one_message():
    tcs = [
        _tc("bash",      {"command": "ls"},             "tc1"),
        _tc("read_file", {"path": "/workspace/x.py"},   "tc2"),
    ]
    events = parse_activity([_msg("assistant", tool_calls=tcs)])
    tool_events = [e for e in events if e.event_type == "tool_call"]
    assert len(tool_events) == 2


def test_json_encoded_content_blocks_parsed():
    """Hermes sometimes encodes assistant content as a JSON list of blocks."""
    blocks = json.dumps([{"type": "text", "text": "I'm working on it."}])
    events = parse_activity([_msg("assistant", content=blocks)])
    # Should surface the text somehow, not crash
    assert isinstance(events, list)