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 / dialog.rs

100 lines · 3156 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//! Dialog state with explicit roles and turn order. Earlier assistant turns are decision
17//! JSON; walking them in order gives the pending-task state the lifecycle uses
18//! (`lifecycle_fsm.c`: task and prompt create a pending item; approve, deny and cancel
19//! resolve it; remember and prefer never change it).
20
21use crate::action::Action;
22
23/// Who wrote a turn.
24#[derive(Clone, Copy, PartialEq, Eq, Debug)]
25pub enum Role {
26 /// the operator
27 User,
28 /// the agent (its earlier decision JSON)
29 Assistant,
30}
31
32/// One turn of the conversation.
33#[derive(Clone, Copy, Debug)]
34pub struct Turn<'a> {
35 /// author
36 pub role: Role,
37 /// raw text
38 pub text: &'a str,
39}
40
41/// Is there a pending task or reply waiting for the operator?
42#[derive(Clone, Copy, PartialEq, Eq, Debug)]
43pub enum Pending {
44 /// no earlier turn tells us
45 Unknown,
46 /// a task or prompt is waiting
47 Yes,
48 /// the last one was approved, denied or cancelled
49 No,
50}
51
52/// What the earlier turns say.
53#[derive(Clone, Copy, Debug)]
54pub struct DialogState {
55 /// pending-task state before the scored turn
56 pub pending: Pending,
57 /// the agent's previous decision, if it was readable
58 pub prev: Option<Action>,
59 /// index of the scored (last) user turn, if any
60 pub last_user: Option<usize>,
61}
62
63/// Read the `"action"` value out of an assistant decision line. Tolerant: returns None
64/// for anything that is not a recognisable decision.
65pub fn parse_action(s: &str) -> Option<Action> {
66 let k = s.find("\"action\"")?;
67 let rest = &s[k + 8..];
68 let c = rest.find(':')?;
69 let rest = rest[c + 1..].trim_start();
70 let rest = rest.strip_prefix('"')?;
71 let e = rest.find('"')?;
72 Action::from_name(&rest[..e])
73}
74
75/// Walk the turns in order. The scored turn is the last user turn; turns after it are ignored.
76pub fn state(turns: &[Turn]) -> DialogState {
77 let last_user = turns.iter().rposition(|t| t.role == Role::User);
78 let upto = last_user.unwrap_or(0);
79 let mut pending = Pending::Unknown;
80 let mut prev = None;
81 for t in &turns[..upto] {
82 if t.role != Role::Assistant {
83 continue;
84 }
85 let a = parse_action(t.text);
86 prev = a;
87 if let Some(a) = a {
88 if a.creates_pending() {
89 pending = Pending::Yes;
90 } else if a.needs_pending() {
91 pending = Pending::No;
92 }
93 }
94 }
95 DialogState {
96 pending,
97 prev,
98 last_user,
99 }
100}