lha 1.0.2

Long-Horizon Agent command-line package that installs the lha binary.
Documentation
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
use crate::product::protocol::items::AgentMessageContent;
use crate::product::protocol::items::AgentMessageItem;
use crate::product::protocol::items::ReasoningItem;
use crate::product::protocol::items::TurnItem;
use crate::product::protocol::items::UserMessageItem;
use crate::product::protocol::items::WebSearchItem;
use crate::product::protocol::models::ContentItem;
use crate::product::protocol::models::ReasoningItemContent;
use crate::product::protocol::models::ReasoningItemReasoningSummary;
use crate::product::protocol::models::TranscriptItem;
use crate::product::protocol::models::WebSearchAction;
use crate::product::protocol::models::is_image_close_tag_text;
use crate::product::protocol::models::is_image_open_tag_text;
use crate::product::protocol::models::is_local_image_close_tag_text;
use crate::product::protocol::models::is_local_image_open_tag_text;
use crate::product::protocol::user_input::UserInput;
use tracing::warn;
use uuid::Uuid;

use crate::product::agent::instructions::SkillInstructions;
use crate::product::agent::instructions::UserInstructions;
use crate::product::agent::session_prefix::is_session_prefix;
use crate::product::agent::user_shell_command::is_user_shell_command_text;
use crate::product::agent::web_search::web_search_action_detail;

fn parse_user_message(message: &[ContentItem]) -> Option<UserMessageItem> {
    if UserInstructions::is_user_instructions(message)
        || SkillInstructions::is_skill_instructions(message)
    {
        return None;
    }

    let mut content: Vec<UserInput> = Vec::new();

    for (idx, content_item) in message.iter().enumerate() {
        match content_item {
            ContentItem::InputText { text } => {
                if (is_local_image_open_tag_text(text) || is_image_open_tag_text(text))
                    && (matches!(message.get(idx + 1), Some(ContentItem::InputImage { .. })))
                    || (idx > 0
                        && (is_local_image_close_tag_text(text) || is_image_close_tag_text(text))
                        && matches!(message.get(idx - 1), Some(ContentItem::InputImage { .. })))
                {
                    continue;
                }
                if is_session_prefix(text) || is_user_shell_command_text(text) {
                    return None;
                }
                content.push(UserInput::Text {
                    text: text.clone(),
                    // Model input content does not carry UI element ranges.
                    text_elements: Vec::new(),
                });
            }
            ContentItem::InputImage { image_url } => {
                content.push(UserInput::Image {
                    image_url: image_url.clone(),
                });
            }
            ContentItem::OutputText { text } => {
                if is_session_prefix(text) {
                    return None;
                }
                warn!("Output text in user message: {}", text);
            }
        }
    }

    Some(UserMessageItem::new(&content))
}

fn parse_agent_message(id: Option<&String>, message: &[ContentItem]) -> AgentMessageItem {
    let mut content: Vec<AgentMessageContent> = Vec::new();
    for content_item in message.iter() {
        match content_item {
            ContentItem::OutputText { text } => {
                content.push(AgentMessageContent::Text { text: text.clone() });
            }
            _ => {
                warn!(
                    "Unexpected content item in agent message: {:?}",
                    content_item
                );
            }
        }
    }
    let id = id.cloned().unwrap_or_else(|| Uuid::new_v4().to_string());
    AgentMessageItem {
        id,
        content,
        memory_citation: None,
    }
}

pub fn parse_turn_item<T>(item: &T) -> Option<TurnItem>
where
    T: Clone + Into<TranscriptItem>,
{
    let item: TranscriptItem = item.clone().into();
    match item {
        TranscriptItem::Message {
            role, content, id, ..
        } => match role.as_str() {
            "user" => parse_user_message(&content).map(TurnItem::UserMessage),
            "assistant" => Some(TurnItem::AgentMessage(parse_agent_message(
                id.as_ref(),
                &content,
            ))),
            "system" => None,
            _ => None,
        },
        TranscriptItem::Reasoning {
            id,
            summary,
            content,
            ..
        } => {
            let summary_text = summary
                .iter()
                .map(|entry| match entry {
                    ReasoningItemReasoningSummary::SummaryText { text } => text.clone(),
                })
                .collect();
            let raw_content = content
                .unwrap_or_default()
                .into_iter()
                .map(|entry| match entry {
                    ReasoningItemContent::ReasoningText { text }
                    | ReasoningItemContent::Text { text } => text,
                })
                .collect();
            Some(TurnItem::Reasoning(ReasoningItem {
                id,
                summary_text,
                raw_content,
            }))
        }
        TranscriptItem::HostedActivity {
            id,
            activity_type,
            payload,
            ..
        } if activity_type == "web_search" => {
            let action = serde_json::from_value(payload).unwrap_or(WebSearchAction::Other);
            let query = web_search_action_detail(&action);
            Some(TurnItem::WebSearch(WebSearchItem {
                id: id.unwrap_or_default(),
                query,
                action,
            }))
        }
        _ => None,
    }
}

#[cfg(test)]
mod tests {
    use super::parse_turn_item;
    use crate::product::protocol::items::AgentMessageContent;
    use crate::product::protocol::items::TurnItem;
    use crate::product::protocol::items::WebSearchItem;
    use crate::product::protocol::models::ContentItem;
    use crate::product::protocol::models::ReasoningItemContent;
    use crate::product::protocol::models::ReasoningItemReasoningSummary;
    use crate::product::protocol::models::TranscriptItem;
    use crate::product::protocol::models::WebSearchAction;
    use crate::product::protocol::user_input::UserInput;
    use pretty_assertions::assert_eq;

    #[test]
    fn parses_user_message_with_text_and_two_images() {
        let img1 = "https://example.com/one.png".to_string();
        let img2 = "https://example.com/two.jpg".to_string();

        let item = TranscriptItem::Message {
            id: None,
            role: "user".to_string(),
            content: vec![
                ContentItem::InputText {
                    text: "Hello world".to_string(),
                },
                ContentItem::InputImage {
                    image_url: img1.clone(),
                },
                ContentItem::InputImage {
                    image_url: img2.clone(),
                },
            ],
            end_turn: None,
        };

        let turn_item = parse_turn_item(&item).expect("expected user message turn item");

        match turn_item {
            TurnItem::UserMessage(user) => {
                let expected_content = vec![
                    UserInput::Text {
                        text: "Hello world".to_string(),
                        text_elements: Vec::new(),
                    },
                    UserInput::Image { image_url: img1 },
                    UserInput::Image { image_url: img2 },
                ];
                assert_eq!(user.content, expected_content);
            }
            other => panic!("expected TurnItem::UserMessage, got {other:?}"),
        }
    }

    #[test]
    fn skips_local_image_label_text() {
        let image_url = "data:image/png;base64,abc".to_string();
        let label = crate::product::protocol::models::local_image_open_tag_text(1);
        let user_text = "Please review this image.".to_string();

        let item = TranscriptItem::Message {
            id: None,
            role: "user".to_string(),
            content: vec![
                ContentItem::InputText { text: label },
                ContentItem::InputImage {
                    image_url: image_url.clone(),
                },
                ContentItem::InputText {
                    text: "</image>".to_string(),
                },
                ContentItem::InputText {
                    text: user_text.clone(),
                },
            ],
            end_turn: None,
        };

        let turn_item = parse_turn_item(&item).expect("expected user message turn item");

        match turn_item {
            TurnItem::UserMessage(user) => {
                let expected_content = vec![
                    UserInput::Image { image_url },
                    UserInput::Text {
                        text: user_text,
                        text_elements: Vec::new(),
                    },
                ];
                assert_eq!(user.content, expected_content);
            }
            other => panic!("expected TurnItem::UserMessage, got {other:?}"),
        }
    }

    #[test]
    fn skips_unnamed_image_label_text() {
        let image_url = "data:image/png;base64,abc".to_string();
        let label = crate::product::protocol::models::image_open_tag_text();
        let user_text = "Please review this image.".to_string();

        let item = TranscriptItem::Message {
            id: None,
            role: "user".to_string(),
            content: vec![
                ContentItem::InputText { text: label },
                ContentItem::InputImage {
                    image_url: image_url.clone(),
                },
                ContentItem::InputText {
                    text: crate::product::protocol::models::image_close_tag_text(),
                },
                ContentItem::InputText {
                    text: user_text.clone(),
                },
            ],
            end_turn: None,
        };

        let turn_item = parse_turn_item(&item).expect("expected user message turn item");

        match turn_item {
            TurnItem::UserMessage(user) => {
                let expected_content = vec![
                    UserInput::Image { image_url },
                    UserInput::Text {
                        text: user_text,
                        text_elements: Vec::new(),
                    },
                ];
                assert_eq!(user.content, expected_content);
            }
            other => panic!("expected TurnItem::UserMessage, got {other:?}"),
        }
    }

    #[test]
    fn skips_user_instructions_and_env() {
        let items = vec![
            TranscriptItem::Message {
                id: None,
                role: "user".to_string(),
                content: vec![ContentItem::InputText {
                    text: "<user_instructions>test_text</user_instructions>".to_string(),
                }],
                end_turn: None,
            },
            TranscriptItem::Message {
                id: None,
                role: "user".to_string(),
                content: vec![ContentItem::InputText {
                    text: "<environment_context>test_text</environment_context>".to_string(),
                }],
                end_turn: None,
            },
            TranscriptItem::Message {
                id: None,
                role: "user".to_string(),
                content: vec![ContentItem::InputText {
                    text: "# AGENTS.md instructions for test_directory\n\n<INSTRUCTIONS>\ntest_text\n</INSTRUCTIONS>".to_string(),
                }],
                end_turn: None,
            },
            TranscriptItem::Message {
                id: None,
                role: "user".to_string(),
                content: vec![ContentItem::InputText {
                    text: "<skill>\n<name>demo</name>\n<path>skills/demo/SKILL.md</path>\nbody\n</skill>"
                        .to_string(),
                }],
                end_turn: None,
            },
            TranscriptItem::Message {
                id: None,
                role: "user".to_string(),
                content: vec![ContentItem::InputText {
                    text: "<user_shell_command>echo 42</user_shell_command>".to_string(),
                }],
                end_turn: None,
            },
        ];

        for item in items {
            let turn_item = parse_turn_item(&item);
            assert!(turn_item.is_none(), "expected none, got {turn_item:?}");
        }
    }

    #[test]
    fn parses_agent_message() {
        let item = TranscriptItem::Message {
            id: Some("msg-1".to_string()),
            role: "assistant".to_string(),
            content: vec![ContentItem::OutputText {
                text: "Hello from LHA".to_string(),
            }],
            end_turn: None,
        };

        let turn_item = parse_turn_item(&item).expect("expected agent message turn item");

        match turn_item {
            TurnItem::AgentMessage(message) => {
                let Some(AgentMessageContent::Text { text }) = message.content.first() else {
                    panic!("expected agent message text content");
                };
                assert_eq!(text, "Hello from LHA");
            }
            other => panic!("expected TurnItem::AgentMessage, got {other:?}"),
        }
    }

    #[test]
    fn parses_reasoning_summary_and_raw_content() {
        let item = TranscriptItem::Reasoning {
            id: "reasoning_1".to_string(),
            summary: vec![
                ReasoningItemReasoningSummary::SummaryText {
                    text: "Step 1".to_string(),
                },
                ReasoningItemReasoningSummary::SummaryText {
                    text: "Step 2".to_string(),
                },
            ],
            content: Some(vec![ReasoningItemContent::ReasoningText {
                text: "raw details".to_string(),
            }]),
            encrypted_content: None,
        };

        let turn_item = parse_turn_item(&item).expect("expected reasoning turn item");

        match turn_item {
            TurnItem::Reasoning(reasoning) => {
                assert_eq!(
                    reasoning.summary_text,
                    vec!["Step 1".to_string(), "Step 2".to_string()]
                );
                assert_eq!(reasoning.raw_content, vec!["raw details".to_string()]);
            }
            other => panic!("expected TurnItem::Reasoning, got {other:?}"),
        }
    }

    #[test]
    fn parses_reasoning_including_raw_content() {
        let item = TranscriptItem::Reasoning {
            id: "reasoning_2".to_string(),
            summary: vec![ReasoningItemReasoningSummary::SummaryText {
                text: "Summarized step".to_string(),
            }],
            content: Some(vec![
                ReasoningItemContent::ReasoningText {
                    text: "raw step".to_string(),
                },
                ReasoningItemContent::Text {
                    text: "final thought".to_string(),
                },
            ]),
            encrypted_content: None,
        };

        let turn_item = parse_turn_item(&item).expect("expected reasoning turn item");

        match turn_item {
            TurnItem::Reasoning(reasoning) => {
                assert_eq!(reasoning.summary_text, vec!["Summarized step".to_string()]);
                assert_eq!(
                    reasoning.raw_content,
                    vec!["raw step".to_string(), "final thought".to_string()]
                );
            }
            other => panic!("expected TurnItem::Reasoning, got {other:?}"),
        }
    }

    #[test]
    fn parses_web_search_call() {
        let item = TranscriptItem::HostedActivity {
            id: Some("ws_1".to_string()),
            activity_type: "web_search".to_string(),
            status: Some("completed".to_string()),
            payload: serde_json::to_value(WebSearchAction::Search {
                query: Some("weather".to_string()),
                queries: None,
            })
            .expect("serialize web search action"),
        };

        let turn_item = parse_turn_item(&item).expect("expected web search turn item");

        match turn_item {
            TurnItem::WebSearch(search) => assert_eq!(
                search,
                WebSearchItem {
                    id: "ws_1".to_string(),
                    query: "weather".to_string(),
                    action: WebSearchAction::Search {
                        query: Some("weather".to_string()),
                        queries: None,
                    },
                }
            ),
            other => panic!("expected TurnItem::WebSearch, got {other:?}"),
        }
    }

    #[test]
    fn parses_web_search_open_page_call() {
        let item = TranscriptItem::HostedActivity {
            id: Some("ws_open".to_string()),
            activity_type: "web_search".to_string(),
            status: Some("completed".to_string()),
            payload: serde_json::to_value(WebSearchAction::OpenPage {
                url: Some("https://example.com".to_string()),
            })
            .expect("serialize web search action"),
        };

        let turn_item = parse_turn_item(&item).expect("expected web search turn item");

        match turn_item {
            TurnItem::WebSearch(search) => assert_eq!(
                search,
                WebSearchItem {
                    id: "ws_open".to_string(),
                    query: "https://example.com".to_string(),
                    action: WebSearchAction::OpenPage {
                        url: Some("https://example.com".to_string()),
                    },
                }
            ),
            other => panic!("expected TurnItem::WebSearch, got {other:?}"),
        }
    }

    #[test]
    fn parses_web_search_find_in_page_call() {
        let item = TranscriptItem::HostedActivity {
            id: Some("ws_find".to_string()),
            activity_type: "web_search".to_string(),
            status: Some("completed".to_string()),
            payload: serde_json::to_value(WebSearchAction::FindInPage {
                url: Some("https://example.com".to_string()),
                pattern: Some("needle".to_string()),
            })
            .expect("serialize web search action"),
        };

        let turn_item = parse_turn_item(&item).expect("expected web search turn item");

        match turn_item {
            TurnItem::WebSearch(search) => assert_eq!(
                search,
                WebSearchItem {
                    id: "ws_find".to_string(),
                    query: "'needle' in https://example.com".to_string(),
                    action: WebSearchAction::FindInPage {
                        url: Some("https://example.com".to_string()),
                        pattern: Some("needle".to_string()),
                    },
                }
            ),
            other => panic!("expected TurnItem::WebSearch, got {other:?}"),
        }
    }

    #[test]
    fn parses_partial_web_search_call_without_action_as_other() {
        let item = TranscriptItem::HostedActivity {
            id: Some("ws_partial".to_string()),
            activity_type: "web_search".to_string(),
            status: Some("in_progress".to_string()),
            payload: serde_json::Value::Null,
        };

        let turn_item = parse_turn_item(&item).expect("expected web search turn item");
        match turn_item {
            TurnItem::WebSearch(search) => assert_eq!(
                search,
                WebSearchItem {
                    id: "ws_partial".to_string(),
                    query: String::new(),
                    action: WebSearchAction::Other,
                }
            ),
            other => panic!("expected TurnItem::WebSearch, got {other:?}"),
        }
    }
}