turnframe-understand 0.1.0

Turn understanding for Turnframe: small verified model tasks that split, route, locate, extract and check, assembled by code into one plan
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
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
//! `extract`: each argument's value as the user stated it, or `not_given`.
//!
//! A value is given in its argument's shape and always points at the words that state
//! it. Code turns it into the operation's value: words become text, a date expression a
//! date, an amount money, a handle a record ([`crate::values`]).

use std::collections::BTreeMap;
use std::fmt::Write as _;

use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use turnframe_core::ids::OperationKey;
use turnframe_core::operation::{ArgumentSpec, DateExpr, OperationSpec, ValueShape};
use turnframe_core::understanding::RecordValue;
use turnframe_provider::request::Message;
use turnframe_tasks::{ModelTask, StructuralError, TaskKind};

use crate::input::{RecordBrief, UnderstandingInput, WorkflowBrief};
use crate::render;
use crate::schema::{any_of, date_expression, index, object, one_of, variant};
use crate::values;
use crate::words::Span;

/// The message name of the user's current message.
pub const CURRENT: &str = "current";

/// The record handle for a record the user names that is not listed.
pub const BY_NAME: &str = "by_name";

const BUILT_IN: &str = include_str!("../../prompts/understand/extract.md");

/// The extraction task, over one turn.
#[derive(Debug, Clone, Copy)]
pub struct Extract<'a> {
    turn: &'a UnderstandingInput,
}

impl<'a> Extract<'a> {
    /// The task for `turn`.
    #[must_use]
    pub const fn new(turn: &'a UnderstandingInput) -> Self {
        Self { turn }
    }

    /// The turn it runs over.
    #[must_use]
    pub const fn turn(&self) -> &'a UnderstandingInput {
        self.turn
    }
}

/// The record an act applies to, as extraction shows it.
#[derive(Debug, Clone, Copy)]
pub enum RecordContext<'a> {
    /// A record in view.
    Existing(&'a RecordBrief),
    /// A record the act creates.
    New,
    /// A record an earlier act of this message creates.
    SameTurn,
    /// No record.
    Nothing,
}

/// A record an argument may name, with the handle it is listed under.
#[derive(Debug, Clone)]
pub struct RecordChoice {
    /// The handle.
    pub handle: String,
    /// What it resolves to.
    pub value: RecordValue,
    /// How it is shown.
    pub label: String,
}

/// One act's arguments to extract, and their context.
#[derive(Debug, Clone)]
pub struct ExtractInput<'a> {
    /// How the unit is shown.
    pub label: &'static str,
    /// Its words.
    pub words: Span,
    /// The operation.
    pub spec: &'a OperationSpec,
    /// Its workflow.
    pub workflow: &'a WorkflowBrief,
    /// The record it applies to.
    pub record: RecordContext<'a>,
    /// The arguments asked for, in declaration order.
    pub arguments: Vec<&'a ArgumentSpec>,
    /// The records each record-valued argument may name.
    pub record_choices: BTreeMap<String, Vec<RecordChoice>>,
    /// Words of this message the unit continues, such as the request a correction changes.
    pub continues: Option<Span>,
    /// Words of this message the other parts hold.
    pub others: Vec<Span>,
    /// Words of the other parts asking for this same operation: a value the segmentation
    /// cut in two runs on into a neighbouring one.
    pub kin: Vec<Span>,
    /// The other operations the same request asks for.
    pub also: Vec<OperationKey>,
    /// How many earlier messages are shown.
    pub transcript: usize,
    /// What the whole-turn check found, when this call reads the act again.
    pub note: Option<String>,
    /// Which of the unit's acts of this operation to read, and how many it asks for.
    pub occurrence: Option<(usize, usize)>,
}

impl ExtractInput<'_> {
    /// The names a value's `message` may take.
    #[must_use]
    pub fn messages(&self, turn: &UnderstandingInput) -> Vec<String> {
        let mut names = vec![CURRENT.to_owned()];
        names.extend(render::shown_messages(turn, self.transcript));
        names
    }
}

/// Every argument's value, by name.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Extraction {
    /// The values.
    pub arguments: BTreeMap<String, Given>,
}

/// One argument's value as the model gave it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
#[allow(missing_docs)]
pub enum Given {
    /// The user did not state it.
    NotGiven,
    /// The user's words are the value. `text` copies them and narrows the pointer to
    /// them; left empty, the pointer stands.
    Words {
        text: String,
        message: String,
        from: usize,
        to: usize,
    },
    /// Text the model wrote from the words pointed at.
    Written {
        message: String,
        from: usize,
        to: usize,
        text: String,
    },
    /// A value of a closed set, a number or a flag.
    Value {
        message: String,
        from: usize,
        to: usize,
        value: Value,
    },
    /// A date as the user said it.
    Date {
        message: String,
        from: usize,
        to: usize,
        date: DateExpr,
    },
    /// An amount of money.
    Money {
        message: String,
        from: usize,
        to: usize,
        amount: String,
        currency: String,
    },
    /// A record, by its handle; one not listed, by its name as written.
    Record {
        message: String,
        from: usize,
        to: usize,
        record: String,
        #[serde(default)]
        name: String,
    },
}

impl Given {
    /// The message and words a given value points at.
    #[must_use]
    pub fn pointer(&self) -> Option<(&str, Span)> {
        match self {
            Self::NotGiven => None,
            Self::Words {
                message, from, to, ..
            }
            | Self::Written {
                message, from, to, ..
            }
            | Self::Value {
                message, from, to, ..
            }
            | Self::Date {
                message, from, to, ..
            }
            | Self::Money {
                message, from, to, ..
            }
            | Self::Record {
                message, from, to, ..
            } => Some((message, Span::from_shown(*from, *to))),
        }
    }
}

/// The kind of value an argument of this shape is given as.
fn given_kind(shape: &ValueShape) -> &'static str {
    match shape {
        ValueShape::Text { written: false } => "words",
        ValueShape::Text { written: true } => "written",
        ValueShape::Date { .. } => "date",
        ValueShape::Money => "money",
        ValueShape::Record { .. } => "record",
        _ => "value",
    }
}

fn argument_schema(
    argument: &ArgumentSpec,
    spec: &OperationSpec,
    messages: &[String],
    records: Option<&Vec<RecordChoice>>,
) -> Value {
    let pointer = || {
        vec![
            ("message", one_of(messages.iter().cloned())),
            ("from", index()),
            ("to", index()),
        ]
    };
    let mut fields = pointer();
    let kind = given_kind(&argument.shape);
    match &argument.shape {
        ValueShape::Text { written: false } => fields.insert(
            0,
            (
                "text",
                json!({
                    "type": "string",
                    "description": "The value's own words, copied exactly as the user wrote them, without the punctuation that ends their sentence."
                }),
            ),
        ),
        ValueShape::Text { written: true } => fields.push(("text", json!({ "type": "string" }))),
        ValueShape::Enum { values } => fields.push(("value", one_of(values.iter().cloned()))),
        ValueShape::Integer => fields.push(("value", json!({ "type": "integer" }))),
        ValueShape::Number => fields.push(("value", json!({ "type": "number" }))),
        ValueShape::Bool => fields.push(("value", json!({ "type": "boolean" }))),
        ValueShape::Date { .. } => fields.push(("date", date_expression())),
        ValueShape::Money => {
            fields.push(("amount", json!({ "type": "string" })));
            fields.push(("currency", json!({ "type": "string" })));
        }
        ValueShape::Record { .. } => {
            let mut handles: Vec<String> = records
                .map(|choices| choices.iter().map(|c| c.handle.clone()).collect())
                .unwrap_or_default();
            handles.push(BY_NAME.to_owned());
            // The name comes first, as a text value's words do: copying it anchors the
            // choice, where a bare handle list reads as «nothing here» when it is short.
            fields.insert(
                0,
                (
                    "name",
                    json!({
                        "type": "string",
                        "description": "The words naming the record, copied exactly as the user wrote them, without the punctuation that ends their sentence."
                    }),
                ),
            );
            fields.push(("record", one_of(handles)));
        }
        _ => fields.push(("value", structured(spec, &argument.name))),
    }
    any_of(vec![variant("not_given", vec![]), variant(kind, fields)])
}

/// The argument's own schema from the arguments type, with no references left.
fn structured(spec: &OperationSpec, name: &str) -> Value {
    let root = spec.arguments_schema.as_value();
    let property = root
        .pointer(&format!("/properties/{name}"))
        .cloned()
        .unwrap_or_else(|| json!({}));
    let defs = root.get("$defs").cloned();
    inline(property, defs.as_ref(), 0)
}

fn inline(value: Value, defs: Option<&Value>, depth: usize) -> Value {
    match value {
        Value::Object(map) if depth < 16 => {
            if let Some(Value::String(reference)) = map.get("$ref") {
                let name = reference.rsplit('/').next().unwrap_or_default();
                let target = defs.and_then(|d| d.get(name)).cloned().unwrap_or(json!({}));
                return inline(target, defs, depth + 1);
            }
            Value::Object(
                map.into_iter()
                    .map(|(key, child)| (key, inline(child, defs, depth + 1)))
                    .collect(),
            )
        }
        Value::Array(items) => Value::Array(
            items
                .into_iter()
                .map(|item| inline(item, defs, depth + 1))
                .collect(),
        ),
        other => other,
    }
}

impl<'a> ModelTask for Extract<'a> {
    type Input = ExtractInput<'a>;
    type Output = Extraction;

    fn kind(&self) -> TaskKind {
        TaskKind::Extract
    }

    fn prompt_name(&self) -> &str {
        "understand.extract"
    }

    fn instructions(&self) -> &str {
        BUILT_IN
    }

    fn schema(&self, input: &ExtractInput<'a>) -> Value {
        let messages = input.messages(self.turn);
        let properties = input
            .arguments
            .iter()
            .map(|argument| {
                (
                    argument.name.as_str(),
                    argument_schema(
                        argument,
                        input.spec,
                        &messages,
                        input.record_choices.get(&argument.name),
                    ),
                )
            })
            .collect();
        object(vec![("arguments", object(properties))])
    }

    fn render(&self, input: &ExtractInput<'a>) -> Vec<Message> {
        let turn = self.turn;
        let words = &turn.message;
        let record = match input.record {
            RecordContext::Existing(record) => {
                Some(format!("Record: {}", render::record_line(record, false)))
            }
            RecordContext::New => Some(format!("Record: a new {} record", input.workflow.key)),
            RecordContext::SameTurn => Some(format!(
                "Record: the {} record this message creates",
                input.workflow.key
            )),
            RecordContext::Nothing => None,
        };
        let mut choices = String::new();
        for (name, listed) in &input.record_choices {
            if !choices.is_empty() {
                choices.push_str("\n\n");
            }
            if listed.is_empty() {
                let _ = write!(
                    choices,
                    "No record is listed for {name}: one the user names is {BY_NAME}, with its name."
                );
                continue;
            }
            let _ = write!(choices, "Records {name} may name:");
            for choice in listed {
                let _ = write!(choices, "\n- {}: {}", choice.handle, choice.label);
            }
            let _ = write!(
                choices,
                "\n- {BY_NAME}: a record the user names that is not listed"
            );
        }
        let asked: Vec<&str> = input.arguments.iter().map(|a| a.name.as_str()).collect();
        let offered = input
            .spec
            .arguments
            .iter()
            .filter(|argument| render::model_given(&argument.source))
            .count();
        let only = (asked.len() < offered).then(|| format!("Give only: {}.", asked.join(", ")));
        vec![Message::user(render::sections([
            Some(format!(
                "Operation: {}",
                render::operation_line(input.spec, self.turn)
            )),
            input
                .spec
                .guidance
                .as_ref()
                .map(|g| format!("Guidance: {g}")),
            render::arguments(input.spec, turn),
            only,
            render::examples(input.spec),
            render::glossary(input.workflow),
            record,
            (!choices.is_empty()).then_some(choices),
            Some(format!("Today: {}", turn.today.format("%A %-d %B %Y"))),
            render::transcript(turn, input.transcript),
            Some(render::titled_message(
                &format!("Message ({CURRENT})"),
                words,
            )),
            Some(render::unit(input.label, words, input.words)),
            input
                .continues
                .map(|span| render::unit("It continues", words, span)),
            (!input.also.is_empty()).then(|| {
                let keys: Vec<&str> = input.also.iter().map(OperationKey::as_str).collect();
                format!(
                    "This request also asks for {}: the words of its values are that \
                     operation's.",
                    keys.join(", ")
                )
            }),
            (!input.others.is_empty()).then(|| {
                let spans: Vec<String> = input
                    .others
                    .iter()
                    .map(|span| {
                        let (from, to) = span.shown();
                        format!("words {from} to {to}")
                    })
                    .collect();
                format!(
                    "Other parts of the message, each read on its own, give none of this \
                     part's values: {}.",
                    spans.join(", ")
                )
            }),
            input.occurrence.map(|(number, of)| {
                format!(
                    "This request asks for {} {of} times: give only the values of occurrence \
                     {number} of {of}, counting in the order the message says them.",
                    input.spec.key
                )
            }),
            input
                .note
                .as_ref()
                .map(|note| format!("A check of the whole message found: {note}")),
        ]))]
    }

    fn check(&self, input: &ExtractInput<'a>, output: &Extraction) -> Result<(), StructuralError> {
        values::convert(self.turn, input, output).map(|_| ())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn a_note_from_the_whole_turn_check_is_shown() {
        let turn = UnderstandingInput::new("name Lisbon", "en-GB", chrono::NaiveDate::MIN);
        let spec = OperationSpec::new("trip.set_name").summary("Name the trip.");
        let workflow = WorkflowBrief::new("trip");
        let input = ExtractInput {
            label: "Request",
            words: Span::new(0, 1),
            spec: &spec,
            workflow: &workflow,
            record: RecordContext::Nothing,
            arguments: Vec::new(),
            record_choices: BTreeMap::new(),
            continues: None,
            others: Vec::new(),
            kin: Vec::new(),
            also: Vec::new(),
            transcript: 0,
            note: Some("the value of value is in «rent»".to_owned()),
            occurrence: None,
        };
        let rendered = format!("{:?}", Extract::new(&turn).render(&input));
        assert!(
            rendered
                .contains("A check of the whole message found: the value of value is in «rent»"),
            "{rendered}"
        );
    }

    #[test]
    fn the_words_other_parts_hold_are_named() {
        let turn =
            UnderstandingInput::new("name Lisbon, fly tomorrow", "en-GB", chrono::NaiveDate::MIN);
        let spec = OperationSpec::new("trip.set_name").summary("Name the trip.");
        let workflow = WorkflowBrief::new("trip");
        let input = ExtractInput {
            label: "Request",
            words: Span::new(0, 1),
            spec: &spec,
            workflow: &workflow,
            record: RecordContext::Nothing,
            arguments: Vec::new(),
            record_choices: BTreeMap::new(),
            continues: None,
            others: vec![Span::new(2, 3)],
            kin: Vec::new(),
            also: Vec::new(),
            transcript: 0,
            note: None,
            occurrence: None,
        };
        let rendered = format!("{:?}", Extract::new(&turn).render(&input));
        assert!(
            rendered.contains(
                "Other parts of the message, each read on its own, give none of this part's \
                 values: words 3 to 4."
            ),
            "{rendered}"
        );
    }

    #[test]
    fn the_other_operations_of_the_request_are_named() {
        let turn = UnderstandingInput::new(
            "name Lisbon and fly tomorrow",
            "en-GB",
            chrono::NaiveDate::MIN,
        );
        let spec = OperationSpec::new("trip.set_name").summary("Name the trip.");
        let workflow = WorkflowBrief::new("trip");
        let input = ExtractInput {
            label: "Request",
            words: Span::new(0, 4),
            spec: &spec,
            workflow: &workflow,
            record: RecordContext::Nothing,
            arguments: Vec::new(),
            record_choices: BTreeMap::new(),
            continues: None,
            others: Vec::new(),
            kin: Vec::new(),
            also: vec!["trip.set_travel_date".into()],
            transcript: 0,
            note: None,
            occurrence: None,
        };
        let rendered = format!("{:?}", Extract::new(&turn).render(&input));
        assert!(
            rendered.contains(
                "This request also asks for trip.set_travel_date: the words of its values are \
                 that operation's."
            ),
            "{rendered}"
        );
    }

    #[test]
    fn a_given_value_deserializes_by_its_kind() {
        let given: Given = serde_json::from_value(json!({
            "kind": "date", "message": "current", "from": 4, "to": 4,
            "date": {"kind": "relative", "unit": "day", "amount": 1}
        }))
        .unwrap();
        assert_eq!(
            given.pointer(),
            Some(("current", Span::new(3, 3))),
            "a model counts words from 1"
        );
        let absent: Given = serde_json::from_value(json!({"kind": "not_given"})).unwrap();
        assert_eq!(absent, Given::NotGiven);
    }
}