halo-agent 0.1.1

Halo Agent and Workflow execution kernel
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
546
547
548
549
550
551
552
553
use std::collections::HashSet;

use pulldown_cmark::{Event, Options as MarkdownOptions, Tag};
use serde_json::{Map, Value, json};

use super::check_cancelled;
use crate::{
    Cancellation,
    document::{self, Document, Error},
};

mod liteparse;

#[derive(Clone, Copy, Debug)]
enum Method {
    General,
    One,
    Qa,
    Table,
}
#[derive(Clone, Copy, Debug, PartialEq)]
enum Format {
    Text,
    Markdown,
    Csv,
    Json,
    Binary(&'static str),
}

/// A concrete Parser. Configuration is kept private, not a second public Config.
#[derive(Clone, Debug)]
pub struct Parser {
    method: Method,
    format: Format,
    options: Map<String, Value>,
}

pub fn new(method: &str, config: Value) -> Result<Parser, Error> {
    let config = config
        .as_object()
        .ok_or_else(|| invalid("config", "parser config must be an object"))?;
    allowed_keys(config, &["format", "engine", "options"])?;
    let method = match method {
        "general" | "naive" => Method::General,
        "one" => Method::One,
        "qa" => Method::Qa,
        "table" => Method::Table,
        _ => {
            return Err(Error::new(
                "UNSUPPORTED",
                "parser method is not implemented",
            ));
        }
    };
    let format = match field(config, "format")? {
        "text" | "txt" => Format::Text,
        "markdown" | "md" => Format::Markdown,
        "csv" => Format::Csv,
        "json" => Format::Json,
        "pdf" => Format::Binary("pdf"),
        "doc" => Format::Binary("doc"),
        "docx" => Format::Binary("docx"),
        "docm" => Format::Binary("docm"),
        "dot" => Format::Binary("dot"),
        "dotm" => Format::Binary("dotm"),
        "dotx" => Format::Binary("dotx"),
        "xlsx" => Format::Binary("xlsx"),
        "xls" => Format::Binary("xls"),
        "xlsm" => Format::Binary("xlsm"),
        "xlsb" => Format::Binary("xlsb"),
        "pptx" => Format::Binary("pptx"),
        "ppt" => Format::Binary("ppt"),
        "pptm" => Format::Binary("pptm"),
        "pot" => Format::Binary("pot"),
        "potm" => Format::Binary("potm"),
        "potx" => Format::Binary("potx"),
        "odt" => Format::Binary("odt"),
        "ott" => Format::Binary("ott"),
        "ods" => Format::Binary("ods"),
        "ots" => Format::Binary("ots"),
        "odp" => Format::Binary("odp"),
        "otp" => Format::Binary("otp"),
        "rtf" => Format::Binary("rtf"),
        "pages" => Format::Binary("pages"),
        "key" => Format::Binary("key"),
        "numbers" => Format::Binary("numbers"),
        "tsv" => Format::Binary("tsv"),
        "png" => Format::Binary("png"),
        "jpg" | "jpeg" => Format::Binary("jpg"),
        "tif" | "tiff" => Format::Binary("tiff"),
        "gif" => Format::Binary("gif"),
        "bmp" => Format::Binary("bmp"),
        "webp" => Format::Binary("webp"),
        "svg" => Format::Binary("svg"),
        _ => {
            return Err(Error::new(
                "UNSUPPORTED",
                "parser format is not implemented",
            ));
        }
    };
    let engine = if matches!(format, Format::Binary(_)) && !config.contains_key("engine") {
        "liteparse"
    } else {
        field(config, "engine")?
    };
    let compatible = matches!(
        (format, engine),
        (Format::Text, "text")
            | (Format::Markdown, "markdown")
            | (Format::Csv, "csv")
            | (Format::Json, "json")
            | (Format::Binary(_), "liteparse")
    );
    if !compatible {
        return Err(Error::new(
            "UNSUPPORTED",
            "parser format and engine are incompatible",
        ));
    }
    if matches!(method, Method::Qa | Method::Table) && !matches!(format, Format::Csv | Format::Json)
    {
        return Err(Error::new(
            "UNSUPPORTED",
            "this domain parser requires CSV or JSON",
        ));
    }
    let options = match config.get("options") {
        None => Map::new(),
        Some(Value::Object(options)) => options.clone(),
        _ => return Err(invalid("options", "parser options must be an object")),
    };
    let mut allowed = vec!["trim", "normalize_newlines", "remove_empty"];
    if matches!(format, Format::Binary(_)) {
        allowed.extend([
            "ocr",
            "ocr_language",
            "ocr_server_url",
            "max_pages",
            "target_pages",
            "output",
        ]);
    }
    if format == Format::Csv {
        allowed.push("delimiter");
    }
    if matches!(method, Method::Qa) {
        allowed.extend(["question", "answer"]);
    }
    if format == Format::Json && matches!(method, Method::General) {
        allowed.push("content");
    }
    allowed_keys(&options, &allowed)?;
    for key in ["trim", "normalize_newlines", "remove_empty"] {
        if options.get(key).is_some_and(|value| !value.is_boolean()) {
            return Err(invalid(key, "option must be boolean"));
        }
    }
    if matches!(format, Format::Binary(_)) {
        liteparse::validate(&options)?;
    }
    for key in ["question", "answer", "content"] {
        if options
            .get(key)
            .is_some_and(|value| value.as_str().is_none_or(|value| value.trim().is_empty()))
        {
            return Err(invalid(key, "field name must be nonempty text"));
        }
    }
    if let Some(delimiter) = options.get("delimiter") {
        let delimiter = delimiter
            .as_str()
            .ok_or_else(|| invalid("delimiter", "delimiter must be text"))?;
        if delimiter.len() != 1 || matches!(delimiter.as_bytes()[0], b'"' | b'\r' | b'\n' | 0) {
            return Err(invalid(
                "delimiter",
                "CSV delimiter must be one non-quote, non-newline ASCII byte",
            ));
        }
    }
    if matches!(method, Method::Qa)
        && string_option(&options, "question", "question")
            == string_option(&options, "answer", "answer")
    {
        return Err(invalid(
            "question",
            "question and answer fields must differ",
        ));
    }
    Ok(Parser {
        method,
        format,
        options,
    })
}

fn invalid(field: &str, message: &str) -> Error {
    Error::new("INVALID_ARGUMENTS", message).with_details(field)
}
fn parse_error(field: &str, message: &str) -> Error {
    Error::new("PARSE_ERROR", message).with_details(field)
}
fn field<'a>(object: &'a Map<String, Value>, name: &str) -> Result<&'a str, Error> {
    object
        .get(name)
        .and_then(Value::as_str)
        .filter(|value| !value.trim().is_empty())
        .ok_or_else(|| invalid(name, "parser field must be nonempty text"))
}
fn string_option<'a>(options: &'a Map<String, Value>, key: &str, default: &'a str) -> &'a str {
    options.get(key).and_then(Value::as_str).unwrap_or(default)
}
fn allowed_keys(options: &Map<String, Value>, allowed: &[&str]) -> Result<(), Error> {
    if let Some(key) = options.keys().find(|key| !allowed.contains(&key.as_str())) {
        return Err(invalid(key, "unknown parser configuration field"));
    }
    Ok(())
}

impl Parser {
    fn enabled(&self, key: &str, default: bool) -> bool {
        self.options
            .get(key)
            .and_then(Value::as_bool)
            .unwrap_or(default)
    }
    fn clean(&self, text: &str) -> String {
        let text = if self.enabled("trim", false) {
            text.trim()
        } else {
            text
        };
        if self.enabled("normalize_newlines", false) {
            text.replace("\r\n", "\n").replace('\r', "\n")
        } else {
            text.to_owned()
        }
    }
    fn document(&self, content: &str, role: Option<&str>) -> Document {
        let mut metadata = Map::from_iter([("method".into(), json!("text"))]);
        if let Some(role) = role {
            metadata.insert("layout".into(), json!({"role":role}));
        }
        Document {
            content: self.clean(content),
            metadata,
            ..Document::default()
        }
    }
    fn qa(&self, question: &str, answer: &str) -> Document {
        let question = self.clean(question);
        let answer = self.clean(answer);
        let mut document = self.document(&format!("{question}\n{answer}"), None);
        document
            .metadata
            .insert("qa".into(), json!({"question":question,"answer":answer}));
        document
    }

    fn text(&self, text: &str, cancellation: &Cancellation) -> Result<Vec<Document>, Error> {
        // Paragraph recognition is parsing, not length/token chunking. Original
        // separators inside a paragraph are retained unless explicitly normalized.
        let mut result = Vec::new();
        let mut start = None;
        let mut offset = 0;
        let mut paragraph = 0u64;
        for line in text.split_inclusive('\n') {
            check_cancelled(cancellation)?;
            if line.trim().is_empty() {
                if let Some(start) = start.take() {
                    paragraph += 1;
                    let mut document = self.document(&text[start..offset], Some("paragraph"));
                    document.metadata.insert(
                        "location".into(),
                        json!({"paragraph":{"from":paragraph,"to":paragraph}}),
                    );
                    result.push(document);
                }
            } else {
                start.get_or_insert(offset);
            }
            offset += line.len();
        }
        if let Some(start) = start {
            paragraph += 1;
            let mut document = self.document(&text[start..], Some("paragraph"));
            document.metadata.insert(
                "location".into(),
                json!({"paragraph":{"from":paragraph,"to":paragraph}}),
            );
            result.push(document);
        }
        Ok(result)
    }

    fn markdown(&self, text: &str, cancellation: &Cancellation) -> Result<Vec<Document>, Error> {
        let mut result = Vec::new();
        let mut depth = 0usize;
        let mut start = 0;
        let mut role = "paragraph";
        let mut options = MarkdownOptions::empty();
        options.insert(
            MarkdownOptions::ENABLE_TABLES
                | MarkdownOptions::ENABLE_STRIKETHROUGH
                | MarkdownOptions::ENABLE_TASKLISTS,
        );
        for (event, range) in pulldown_cmark::Parser::new_ext(text, options).into_offset_iter() {
            check_cancelled(cancellation)?;
            match event {
                Event::Start(tag) => {
                    if depth == 0 {
                        start = range.start;
                        role = match tag {
                            Tag::Heading { .. } => "heading",
                            Tag::CodeBlock(_) => "code",
                            Tag::List(_) => "list",
                            Tag::Table(_) => "table",
                            _ => "paragraph",
                        };
                    }
                    depth += 1;
                }
                Event::End(_) => {
                    depth -= 1;
                    if depth == 0 {
                        result.push(self.document(&text[start..range.end], Some(role)));
                    }
                }
                Event::Rule if depth == 0 => result.push(self.document(&text[range], None)),
                _ => {}
            }
        }
        // Content retains source markup. No rendered offsets are mislabeled as
        // original paragraph/character locations and no raw HTML is executed.
        Ok(result)
    }

    fn csv(&self, text: &str, cancellation: &Cancellation) -> Result<Vec<Document>, Error> {
        let delimiter = string_option(&self.options, "delimiter", ",").as_bytes()[0];
        validate_csv_quotes(text.as_bytes(), delimiter, cancellation)?;
        let mut reader = csv::ReaderBuilder::new()
            .delimiter(delimiter)
            .has_headers(true)
            .flexible(false)
            .from_reader(text.as_bytes());
        let headers = reader
            .headers()
            .map_err(|_| parse_error("header", "CSV header cannot be parsed"))?
            .clone();
        let mut names = HashSet::new();
        if headers
            .iter()
            .any(|name| name.trim().is_empty() || !names.insert(name))
        {
            return Err(parse_error(
                "header",
                "CSV columns must have unique nonempty names",
            ));
        }
        let question = string_option(&self.options, "question", "question");
        let answer = string_option(&self.options, "answer", "answer");
        let qa_columns = if matches!(self.method, Method::Qa) {
            Some((
                headers
                    .iter()
                    .position(|name| name == question)
                    .ok_or_else(|| parse_error("question", "CSV question column is missing"))?,
                headers
                    .iter()
                    .position(|name| name == answer)
                    .ok_or_else(|| parse_error("answer", "CSV answer column is missing"))?,
            ))
        } else {
            None
        };
        let mut result = Vec::new();
        for (index, record) in reader.records().enumerate() {
            check_cancelled(cancellation)?;
            let record = record.map_err(|_| {
                parse_error(&format!("row={}", index + 1), "CSV row cannot be parsed")
            })?;
            if let Some((question, answer)) = qa_columns {
                result.push(self.qa(&record[question], &record[answer]));
            } else {
                let fields: Map<String, Value> = headers
                    .iter()
                    .zip(record.iter())
                    .map(|(key, value)| (key.into(), json!(value)))
                    .collect();
                let content = headers
                    .iter()
                    .zip(record.iter())
                    .map(|(key, value)| format!("{key}: {value}"))
                    .collect::<Vec<_>>()
                    .join("\n");
                let mut document = self.document(&content, Some("row"));
                document
                    .metadata
                    .insert("table".into(), json!({"fields":fields}));
                result.push(document);
            }
        }
        Ok(result)
    }

    fn json(&self, text: &str, cancellation: &Cancellation) -> Result<Vec<Document>, Error> {
        let value: Value =
            serde_json::from_str(text).map_err(|_| parse_error("data", "invalid JSON input"))?;
        let rows = match value {
            Value::Array(rows) => rows,
            value => vec![value],
        };
        let mut result = Vec::new();
        for (index, row) in rows.into_iter().enumerate() {
            check_cancelled(cancellation)?;
            let context = format!("row={}", index + 1);
            let document = match self.method {
                Method::Qa => {
                    let object = row
                        .as_object()
                        .ok_or_else(|| parse_error(&context, "QA row must be an object"))?;
                    let question =
                        scalar(object.get(string_option(&self.options, "question", "question")))
                            .ok_or_else(|| {
                                parse_error(&context, "QA question must be scalar content")
                            })?;
                    let answer =
                        scalar(object.get(string_option(&self.options, "answer", "answer")))
                            .ok_or_else(|| {
                                parse_error(&context, "QA answer must be scalar content")
                            })?;
                    self.qa(&question, &answer)
                }
                Method::Table => {
                    let object = row
                        .as_object()
                        .ok_or_else(|| parse_error(&context, "table row must be an object"))?;
                    let mut document = self.document(&row.to_string(), Some("row"));
                    document
                        .metadata
                        .insert("table".into(), json!({"fields":object}));
                    document
                }
                _ => {
                    let content = match &row {
                        Value::String(content) => Some(content.as_str()),
                        Value::Object(object) => object
                            .get(string_option(&self.options, "content", "content"))
                            .and_then(Value::as_str),
                        _ => None,
                    }
                    .ok_or_else(|| parse_error(&context, "JSON row needs text content"))?;
                    self.document(content, None)
                }
            };
            result.push(document);
        }
        Ok(result)
    }
}

fn scalar(value: Option<&Value>) -> Option<String> {
    match value? {
        Value::String(value) => Some(value.clone()),
        value @ (Value::Number(_) | Value::Bool(_)) => Some(value.to_string()),
        _ => None,
    }
}

// The csv reader deliberately accepts some malformed quotes. Reject truncated
// or inconsistent quoted fields before treating its permissive output as facts.
fn validate_csv_quotes(
    data: &[u8],
    delimiter: u8,
    cancellation: &Cancellation,
) -> Result<(), Error> {
    #[derive(Clone, Copy)]
    enum State {
        Start,
        Plain,
        Quoted,
        Closed,
    }
    let mut state = State::Start;
    for (index, byte) in data.iter().copied().enumerate() {
        if index % 4096 == 0 {
            check_cancelled(cancellation)?;
        }
        state = match (state, byte) {
            (State::Quoted, b'"') => State::Closed,
            (State::Quoted, _) => State::Quoted,
            (State::Closed, b'"') => State::Quoted,
            (_, byte) if byte == delimiter || matches!(byte, b'\r' | b'\n') => State::Start,
            (State::Closed, _) | (State::Plain, b'"') => {
                return Err(parse_error("data", "malformed CSV quoting"));
            }
            (State::Start, b'"') => State::Quoted,
            _ => State::Plain,
        };
    }
    if matches!(state, State::Quoted) {
        return Err(parse_error("data", "unterminated CSV quoted field"));
    }
    Ok(())
}

impl document::Parser for Parser {
    const FORMATS: &'static [&'static str] = &[
        "text", "txt", "markdown", "md", "csv", "json", "pdf", "doc", "docx", "docm", "dot",
        "dotm", "dotx", "xlsx", "xls", "xlsm", "xlsb", "pptx", "ppt", "pptm", "pot", "potm",
        "potx", "odt", "ott", "ods", "ots", "odp", "otp", "rtf", "pages", "key", "numbers", "tsv",
        "png", "jpg", "jpeg", "tif", "tiff", "gif", "bmp", "webp", "svg",
    ];

    async fn parse(
        &self,
        data: &[u8],
        cancellation: &Cancellation,
    ) -> Result<Vec<Document>, Error> {
        check_cancelled(cancellation)?;
        if let Format::Binary(extension) = self.format {
            return liteparse::parse(data, extension, self.method, &self.options, cancellation)
                .await;
        }
        let data = data.strip_prefix(&[0xef, 0xbb, 0xbf]).unwrap_or(data);
        let text = std::str::from_utf8(data)
            .map_err(|_| parse_error("data", "input is not UTF-8 text"))?;
        let mut result = if matches!(self.method, Method::One) {
            // `one` preserves the complete source, but still validates its format.
            if self.format == Format::Json {
                let _: Value = serde_json::from_str(text)
                    .map_err(|_| parse_error("data", "invalid JSON input"))?;
            }
            if self.format == Format::Csv {
                self.csv(text, cancellation)?;
            }
            vec![self.document(text, None)]
        } else {
            match self.format {
                Format::Text => self.text(text, cancellation)?,
                Format::Markdown => self.markdown(text, cancellation)?,
                Format::Csv => self.csv(text, cancellation)?,
                Format::Json => self.json(text, cancellation)?,
                Format::Binary(_) => unreachable!("binary formats return before UTF-8 decoding"),
            }
        };
        if self.enabled("remove_empty", true) {
            result.retain(|document| !document.content.trim().is_empty());
        }
        check_cancelled(cancellation)?;
        Ok(result)
    }
}