interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / crates / allen-reflex / src / segment.rs

135 lines · 4778 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16//! Ordered request handling. A message is cut into requests at explicit sequence markers
17//! ("and then", ", then", "after that", ";", "and approve ..."). The exam contract says
18//! "if the message asks for several steps, give only the first step", so the core decides on
19//! the first request that is not a pure acknowledgement, and copies its argument only from it.
20//! Sentence breaks alone do not cut ("I do not know what to ask. Show me the options." is
21//! one request), except that a leading sentence of fillers ("Ok.") is dropped.
22
23use crate::lexicon::{is_in, AND_VERBS, FILLER, FILLER_COMMA};
24use crate::text::{gap, Token};
25use alloc::vec::Vec;
26
27/// A request: token range `[start, end)`.
28#[derive(Clone, Copy, Debug, PartialEq, Eq)]
29pub struct Clause {
30 /// first token
31 pub start: usize,
32 /// one past the last token
33 pub end: usize,
34}
35
36const TRAILING_MARKERS: &[&str] = &["afterwards", "afterward", "later", "next"];
37
38/// Cut `toks` into requests in order.
39pub fn clauses(toks: &[Token]) -> Vec<Clause> {
40 let mut out = Vec::new();
41 let n = toks.len();
42 let mut s = 0;
43 let mut i = 0;
44 while i < n {
45 let w = toks[i].word.as_str();
46 let g = toks[i].gap;
47 // ";" always separates
48 if i > s && g & gap::SEMI != 0 {
49 out.push(Clause { start: s, end: i });
50 s = i;
51 i += 1;
52 continue;
53 }
54 // "and then", ", then", ". Then"
55 if w == "then" && i > s {
56 let prev_and = toks[i - 1].word == "and" && i - 1 > s;
57 if prev_and || g & (gap::COMMA | gap::STOP) != 0 {
58 let e = if prev_and { i - 1 } else { i };
59 out.push(Clause { start: s, end: e });
60 s = i + 1;
61 i += 1;
62 continue;
63 }
64 }
65 // "after that" (", after that", "and after that")
66 if w == "after" && i + 1 < n && toks[i + 1].word == "that" && i > s {
67 let prev_and = toks[i - 1].word == "and" && i - 1 > s;
68 if prev_and || g & (gap::COMMA | gap::STOP) != 0 {
69 let e = if prev_and { i - 1 } else { i };
70 out.push(Clause { start: s, end: e });
71 s = i + 2;
72 i += 2;
73 continue;
74 }
75 }
76 // "X and approve it": a second agent verb starts a new request
77 if w == "and" && i > s && i + 1 < n && is_in(AND_VERBS, toks[i + 1].word.as_str()) {
78 out.push(Clause { start: s, end: i });
79 s = i + 1;
80 i += 1;
81 continue;
82 }
83 i += 1;
84 }
85 if s < n {
86 out.push(Clause { start: s, end: n });
87 }
88 // trailing sequence markers ("... and approve it afterwards") are not content
89 for c in out.iter_mut() {
90 while c.end > c.start && is_in(TRAILING_MARKERS, toks[c.end - 1].word.as_str()) {
91 c.end -= 1;
92 }
93 }
94 out.retain(|c| c.end > c.start);
95 out
96}
97
98fn all_filler(toks: &[Token], s: usize, e: usize) -> bool {
99 e > s
100 && toks[s..e]
101 .iter()
102 .all(|t| !t.alnum || is_in(FILLER, &t.word))
103}
104
105/// The request the decision is about: the first clause that is not only fillers, with any
106/// leading filler sentence or filler-comma word removed. Falls back to the first clause.
107pub fn primary(toks: &[Token], cl: &[Clause]) -> Option<Clause> {
108 let first = *cl.first()?;
109 let pick = cl
110 .iter()
111 .copied()
112 .find(|c| !all_filler(toks, c.start, c.end))
113 .unwrap_or(first);
114 let mut c = pick;
115 // drop leading filler sentences ("Ok. Remember ...")
116 loop {
117 let mut j = c.start + 1;
118 while j < c.end && toks[j].gap & gap::STOP == 0 {
119 j += 1;
120 }
121 if j < c.end && all_filler(toks, c.start, j) {
122 c.start = j;
123 } else {
124 break;
125 }
126 }
127 // drop "Ok," / "Well," before more words
128 while c.end - c.start > 1
129 && is_in(FILLER_COMMA, &toks[c.start].word)
130 && toks[c.start + 1].gap & gap::COMMA != 0
131 {
132 c.start += 1;
133 }
134 Some(c)
135}