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
//! `segment`: which units the message holds, what kind each is, and its words.

use serde::{Deserialize, Serialize};
use serde_json::{Value, json};
use turnframe_core::plan::AnswerBasis;
use turnframe_core::understanding::{ConstraintKind, UnitKind};
use turnframe_provider::request::Message;
use turnframe_tasks::{ModelTask, StructuralError, TaskKind};

use crate::input::UnderstandingInput;
use crate::render;
use crate::schema::{any_of, array, nullable, object, one_of, span, text, variant};
use crate::tasks::{check_one_of, check_span};
use crate::words::Span;

/// Names a unit's workflow when the message does not say which.
pub const UNKNOWN: &str = "unknown";

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

const BASES: [&str; 4] = [
    "current_committed_state",
    "proposed_state",
    "committed_state_after_turn",
    "general_domain_knowledge",
];

const CONSTRAINTS: [&str; 7] = [
    "do_not_submit",
    "do_not_delete",
    "draft_only",
    "ask_before_applying",
    "apply_only_if",
    "no_external_effects",
    "keep_unchanged",
];

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

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

    fn workflows(&self) -> Vec<String> {
        let mut names: Vec<String> = self
            .turn
            .workflows
            .iter()
            .map(|workflow| workflow.key.to_string())
            .collect();
        names.push(UNKNOWN.to_owned());
        names
    }

    /// Whether the message can answer the assistant: it asked for a value, or it said
    /// something last that a value can answer.
    fn answers_the_assistant(&self) -> bool {
        self.turn.expectation.is_some()
            || self
                .turn
                .transcript
                .iter()
                .any(|message| message.speaker == crate::input::Speaker::Assistant)
    }

    fn options(&self) -> Vec<String> {
        self.turn
            .card
            .as_ref()
            .filter(|card| card.accepts_typed_answer)
            .map(|card| card.options.iter().map(|o| o.id.to_string()).collect())
            .unwrap_or_default()
    }

    fn receipts(&self) -> Vec<String> {
        let mut keys: Vec<String> = self.turn.receipts.iter().map(|r| r.key.clone()).collect();
        keys.push(UNKNOWN.to_owned());
        keys
    }
}

/// The units of a message, as the model found them.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct Segmentation {
    /// What the message asks for, in order, in a sentence or two.
    pub analysis: String,
    /// The units, in the order listed.
    pub units: Vec<SegmentedUnit>,
}

/// One unit as the model found it.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "snake_case")]
#[allow(missing_docs)]
pub enum SegmentedUnit {
    Request {
        words: Span,
        workflow: String,
    },
    Question {
        words: Span,
        workflow: String,
        basis: AnswerBasis,
        continues_previous: bool,
    },
    Constraint {
        words: Span,
        constraint: ConstraintKind,
    },
    Correction {
        words: Span,
        workflow: String,
        /// The unit number it changes, from 1; `None` for an earlier turn.
        corrects: Option<usize>,
    },
    Cancel {
        words: Span,
        workflow: String,
        /// The unit number it withdraws, from 1; `None` for an earlier turn.
        cancels: Option<usize>,
    },
    CardAnswer {
        words: Span,
        option: String,
    },
    Dispute {
        words: Span,
        receipt: String,
    },
    ProvidesValue {
        words: Span,
    },
    Chitchat {
        words: Span,
    },
}

impl SegmentedUnit {
    /// Its words.
    #[must_use]
    pub const fn words(&self) -> Span {
        match self {
            Self::Request { words, .. }
            | Self::Question { words, .. }
            | Self::Constraint { words, .. }
            | Self::Correction { words, .. }
            | Self::Cancel { words, .. }
            | Self::CardAnswer { words, .. }
            | Self::Dispute { words, .. }
            | Self::ProvidesValue { words }
            | Self::Chitchat { words } => *words,
        }
    }

    /// The same unit over `words`.
    fn at(&self, words: Span) -> Self {
        let mut unit = self.clone();
        match &mut unit {
            Self::Request { words: own, .. }
            | Self::Question { words: own, .. }
            | Self::Constraint { words: own, .. }
            | Self::Correction { words: own, .. }
            | Self::Cancel { words: own, .. }
            | Self::CardAnswer { words: own, .. }
            | Self::Dispute { words: own, .. }
            | Self::ProvidesValue { words: own }
            | Self::Chitchat { words: own } => *own = words,
        }
        unit
    }

    /// Its kind.
    #[must_use]
    pub const fn kind(&self) -> UnitKind {
        match self {
            Self::Request { .. } => UnitKind::Request,
            Self::Question { .. } => UnitKind::Question,
            Self::Constraint { .. } => UnitKind::Constraint,
            Self::Correction { .. } => UnitKind::Correction,
            Self::Cancel { .. } => UnitKind::Cancel,
            Self::CardAnswer { .. } => UnitKind::CardAnswer,
            Self::Dispute { .. } => UnitKind::Dispute,
            Self::ProvidesValue { .. } => UnitKind::ProvidesValue,
            Self::Chitchat { .. } => UnitKind::Chitchat,
        }
    }

    /// The workflow it names, `unknown` included.
    #[must_use]
    pub fn workflow(&self) -> Option<&str> {
        match self {
            Self::Request { workflow, .. }
            | Self::Question { workflow, .. }
            | Self::Correction { workflow, .. }
            | Self::Cancel { workflow, .. } => Some(workflow),
            _ => None,
        }
    }

    /// The unit number it corrects or cancels, when it is about this message.
    #[must_use]
    pub const fn refers_to(&self) -> Option<usize> {
        match self {
            Self::Correction { corrects, .. } => *corrects,
            Self::Cancel { cancels, .. } => *cancels,
            _ => None,
        }
    }
}

impl ModelTask for Segment<'_> {
    type Input = ();
    type Output = Segmentation;

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

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

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

    fn schema(&self, _input: &()) -> Value {
        let workflow = one_of(self.workflows());
        let words = || ("words", span());
        let mut variants = vec![
            variant("request", vec![words(), ("workflow", workflow.clone())]),
            variant(
                "question",
                vec![
                    words(),
                    ("workflow", workflow.clone()),
                    ("basis", one_of(BASES)),
                    ("continues_previous", json!({ "type": "boolean" })),
                ],
            ),
            variant(
                "constraint",
                vec![words(), ("constraint", one_of(CONSTRAINTS))],
            ),
            variant(
                "correction",
                vec![
                    words(),
                    ("workflow", workflow.clone()),
                    ("corrects", nullable(unit_number())),
                ],
            ),
            variant(
                "cancel",
                vec![
                    words(),
                    ("workflow", workflow),
                    ("cancels", nullable(unit_number())),
                ],
            ),
        ];
        let options = self.options();
        if !options.is_empty() {
            variants.push(variant(
                "card_answer",
                vec![words(), ("option", one_of(options))],
            ));
        }
        // Both answer something the assistant said: with nothing said, there is nothing
        // to contest or to give a value for.
        if self.answers_the_assistant() {
            variants.push(variant(
                "dispute",
                vec![words(), ("receipt", one_of(self.receipts()))],
            ));
            variants.push(variant("provides_value", vec![words()]));
        }
        variants.push(variant("chitchat", vec![words()]));
        object(vec![
            (
                "analysis",
                text("What the message asks for, in order, in a sentence or two."),
            ),
            ("units", array(any_of(variants))),
        ])
    }

    fn render(&self, _input: &()) -> Vec<Message> {
        let turn = self.turn;
        vec![Message::user(render::sections([
            Some(render::workflows(turn)),
            render::last_assistant(turn),
            render::card(turn),
            render::expectation(turn),
            render::receipts(turn),
            Some(render::message(&turn.message)),
        ]))]
    }

    fn check(&self, _input: &(), output: &Segmentation) -> Result<(), StructuralError> {
        if output.units.is_empty() {
            return Err(StructuralError::new(
                "no_units",
                "`units` is empty; every message has at least one unit, chitchat included",
            ));
        }
        let (workflows, options, receipts) = (self.workflows(), self.options(), self.receipts());
        let mut card_answers = 0;
        for (position, unit) in output.units.iter().enumerate() {
            let number = position + 1;
            check_span(&format!("unit {number}"), unit.words(), &self.turn.message)?;
            if let Some(workflow) = unit.workflow() {
                check_one_of("workflow", workflow, &workflows)?;
            }
            match unit {
                SegmentedUnit::CardAnswer { option, .. } => {
                    card_answers += 1;
                    check_one_of("option", option, &options)?;
                }
                SegmentedUnit::Dispute { .. } if !self.answers_the_assistant() => {
                    return Err(StructuralError::new(
                        "nothing_said",
                        "a dispute contests what the assistant did or said, and it has said nothing",
                    ));
                }
                SegmentedUnit::Dispute { receipt, .. } => {
                    check_one_of("receipt", receipt, &receipts)?;
                }
                SegmentedUnit::ProvidesValue { .. } if !self.answers_the_assistant() => {
                    return Err(StructuralError::new(
                        "no_expectation",
                        "provides_value needs the assistant to have asked for a value",
                    ));
                }
                _ => {}
            }
        }
        if card_answers > 1 {
            return Err(StructuralError::new(
                "several_card_answers",
                "the card on screen takes one answer; list one card_answer unit at most",
            ));
        }
        check_disjoint(&output.units)
    }

    /// Readings that differ only by a single word at a unit's edge agree: one keeps a word
    /// joining two parts that the other leaves out, and both say the same of the message.
    fn agree(&self, left: &Segmentation, right: &Segmentation) -> bool {
        left.units.len() == right.units.len()
            && left.units.iter().zip(&right.units).all(|(a, b)| {
                let (mine, theirs) = (a.words(), b.words());
                a.at(theirs) == *b
                    && mine.from.abs_diff(theirs.from) <= 1
                    && mine.to.abs_diff(theirs.to) <= 1
            })
    }
}

fn unit_number() -> Value {
    json!({ "type": "integer", "minimum": 1 })
}

fn check_disjoint(units: &[SegmentedUnit]) -> Result<(), StructuralError> {
    let mut spans: Vec<(Span, usize)> = units
        .iter()
        .enumerate()
        .map(|(index, unit)| (unit.words(), index + 1))
        .collect();
    spans.sort();
    for pair in spans.windows(2) {
        let ((first, a), (second, b)) = (pair[0], pair[1]);
        if second.from <= first.to {
            return Err(StructuralError::new(
                "units_overlap",
                format!("units {a} and {b} share words; each word belongs to one unit at most"),
            ));
        }
    }
    Ok(())
}

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

    #[test]
    fn readings_that_differ_by_one_word_at_a_units_edge_agree() {
        let turn = UnderstandingInput::new("x", "en-GB", chrono::NaiveDate::MIN);
        let task = Segment::new(&turn);
        let reading = |units: Vec<SegmentedUnit>| Segmentation {
            analysis: String::new(),
            units,
        };
        let request = |from, to| SegmentedUnit::Request {
            words: Span::new(from, to),
            workflow: "trip".to_owned(),
        };
        let kept = reading(vec![request(12, 24), request(26, 32)]);
        assert!(task.agree(&kept, &reading(vec![request(12, 24), request(25, 32)])));
        assert!(!task.agree(&kept, &reading(vec![request(12, 24), request(24, 32)])));
        assert!(!task.agree(
            &kept,
            &reading(vec![
                request(12, 24),
                SegmentedUnit::Chitchat {
                    words: Span::new(25, 32)
                }
            ])
        ));
    }

    #[test]
    fn the_listed_bases_and_constraints_are_the_ones_that_deserialize() {
        for basis in BASES {
            serde_json::from_value::<AnswerBasis>(json!(basis)).unwrap();
        }
        for constraint in CONSTRAINTS {
            serde_json::from_value::<ConstraintKind>(json!(constraint)).unwrap();
        }
    }
}