Skip to content

Commit dcd6d0f

Browse files
author
InAnYan
committed
Start working on ScenarioProcessed
1 parent d7b784c commit dcd6d0f

File tree

7 files changed

+381
-14
lines changed

7 files changed

+381
-14
lines changed

module/move/assistant/src/agents.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,6 @@ crate::mod_interface!
1111
layer context;
1212
layer scenario_raw;
1313
layer scenario_raw_processors;
14+
layer scenario_processed;
1415

1516
}

module/move/assistant/src/agents/path.rs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ mod private
1212
sync::LazyLock,
1313
};
1414

15+
use serde::
16+
{
17+
Serialize,
18+
Deserialize,
19+
};
20+
1521
use regex::Regex;
1622

1723
/// Path separator string.
@@ -41,7 +47,7 @@ mod private
4147
///
4248
/// Paths resemble filesystem path, path separator is `::`.
4349
/// Absolute path starts with `::`.
44-
#[ derive( Debug, Clone, Eq, PartialEq, Hash ) ]
50+
#[ derive( Debug, Clone, Eq, PartialEq, Hash, Serialize, Deserialize ) ]
4551
pub struct Path( String );
4652

4753
impl Path
Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
//!
2+
//! Scenario representation. Stores parsed representation of templates and paths.
3+
//! This is the type used for running scenarios.
4+
//!
5+
//! For a more simplistic representation use `ScenarioRaw`.
6+
//!
7+
8+
mod private
9+
{
10+
use serde::
11+
{
12+
Serialize,
13+
Deserialize,
14+
};
15+
16+
use crate::*;
17+
use agents::
18+
{
19+
path::Path,
20+
scenario_raw::ScenarioRaw,
21+
};
22+
23+
/// New type for templates in scenarios.
24+
#[ derive( Debug, Serialize, Deserialize, PartialEq ) ]
25+
pub struct TemplateBody( pub String );
26+
27+
/// Struct that represents user written scenarios.
28+
///
29+
/// This is a processed form of a scenario, templates and paths are distinguished here with types.
30+
/// For more simplistic representation of scenarios, use `ScenarioRaw` type.
31+
#[ derive( Debug, Serialize, Deserialize, PartialEq ) ]
32+
pub struct ScenarioProcessed
33+
{
34+
/// Nodes in the scenario.
35+
pub nodes: Vec< Node >,
36+
}
37+
38+
impl TryFrom< ScenarioRaw > for ScenarioProcessed
39+
{
40+
type Error = std::io::Error;
41+
42+
fn try_from( scenario_raw : ScenarioRaw ) -> Result< Self, Self::Error >
43+
{
44+
let nodes : Result< Vec< Node >, Self::Error > = scenario_raw.nodes.into_iter().map( | n | n.try_from() ).collect();
45+
46+
Ok( Self { nodes } )
47+
}
48+
}
49+
50+
/// Node representation in a scenario file.
51+
///
52+
/// This is a processed form of a node, templates and paths are distinguished here with types.
53+
/// For more simplistic representation of scenarios, use `NodeRaw` type.
54+
#[ derive( Debug, Serialize, Deserialize, PartialEq ) ]
55+
pub struct Node
56+
{
57+
/// ID of the node. Must be unique, will also identify node output.
58+
pub id : String,
59+
60+
/// Type of the node.
61+
pub r#type : Path,
62+
63+
/// Specific type of `Node`.
64+
pub kind : NodeKind,
65+
66+
/// ID of the next node to execute.
67+
pub next : Path,
68+
}
69+
70+
impl TryFrom< NodeRaw > for Node
71+
{
72+
type Error = std::io::Error;
73+
74+
fn try_form( node_raw : NodeRaw ) -> Result< Self, Self::Error >
75+
{
76+
Ok
77+
(
78+
Self
79+
{
80+
id : node_raw.id,
81+
r#type : Path::try_from( node_raw.r#type )?,
82+
kind : NodeKind::try_from( node_raw.params )?,
83+
next : Path::try_from( node_raw.path )?,
84+
}
85+
)
86+
}
87+
}
88+
89+
/// Representation of different types of nodes. Contains parameters unique to types.
90+
/// Used in `Node` struct, where you can find common parameters like `id`, `type`, and others.
91+
#[ derive( Debug, Serialize, Deserialize, PartialEq ) ]
92+
pub enum NodeKind
93+
{
94+
/// Read input from `stdin`.
95+
TriggerStdin
96+
{
97+
/// Prompt to display to `stdout` before reading the `stdin`.
98+
prompt : TemplateBody,
99+
},
100+
101+
/// Get output from LLM.
102+
AgentsCompletion
103+
{
104+
/// Agent's system message template.
105+
system_message : TemplateBody,
106+
/// Agent's user message template.
107+
user_message : TemplateBody,
108+
/// Reuse chat history of other agent.
109+
agent_reuse : Path,
110+
},
111+
112+
/// Print output to `stdout`.
113+
EventStdout
114+
{
115+
/// Prompt to display to `stdout`.
116+
output : TemplateBody,
117+
},
118+
}
119+
120+
impl NodeKind
121+
{
122+
/// Convert `NodeRaw` into `NodeKind`.
123+
pub fn try_from_params
124+
(
125+
params : HashMap< String, String >,
126+
r#type : &Path,
127+
) -> Result< Self, std::io::Error >
128+
{
129+
match path.inner().as_str()
130+
{
131+
"::trigger::stdin" =>
132+
{
133+
let Some()
134+
}
135+
}
136+
}
137+
}
138+
139+
impl TryFrom< HashMap< String, String > > for NodeKind
140+
{
141+
type Error = std::io::Error;
142+
143+
fn try_from( map : HashMap< String, String> ) -> Result< Self, Self::Error >
144+
{
145+
146+
}
147+
}
148+
}
149+
150+
crate::mod_interface!
151+
{
152+
own use
153+
{
154+
TemplateBody,
155+
ScenarioProcessed,
156+
};
157+
}

module/move/assistant/src/agents/scenario_raw.rs

Lines changed: 166 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,17 +6,13 @@
66
77
mod private
88
{
9-
use std::
10-
{
11-
io,
12-
collections::HashMap,
13-
};
9+
use std::io;
1410

1511
use former::Former;
1612
use serde::
1713
{
18-
Serialize,
19-
Deserialize,
14+
serialize,
15+
deserialize,
2016
};
2117

2218
/// Struct that represents user written scenarios.
@@ -49,16 +45,173 @@ mod private
4945
/// ID of the node. Must be unique, will also identify node output.
5046
pub id : String,
5147

52-
/// Type of the node. Represented as a path.
48+
/// Type of the node.
5349
pub r#type : String,
5450

55-
/// Rest of the key-value pairs in the node that are specific to node types.
56-
#[ serde( flatten ) ]
57-
pub params : HashMap< String, String >,
51+
/// Specific type of `NodeRaw`.
52+
pub kind : NodeRawKind,
5853

59-
/// ID of the next node to execute. Represented as a path.
54+
/// ID of the next node to execute.
6055
pub next : String,
6156
}
57+
58+
impl format_tools::Fields< &'_ str, &'_ str > for NodeRaw
59+
{
60+
type Key< 'k > = &'k str;
61+
type Val< 'v > = &'v str;
62+
63+
fn fields( &self ) -> impl format_tools::IteratorTrait< Item = ( &'_ str, &'_ str ) >
64+
{
65+
let mut dst = Vec::new();
66+
67+
dst.push( ( "id", self.id.as_str() ) );
68+
dst.push( ( "type", self.r#type.as_str() ) );
69+
70+
dst.extend( self.kind.fields() );
71+
72+
dst.push( ( "next", self.next.as_str() ) );
73+
74+
dst.into_iter()
75+
}
76+
}
77+
78+
/// Representation of different types of nodes. Contains parameters unique to types.
79+
/// Used in `Node` struct, where you can find common parameters like `id`, `type`, and others.
80+
#[ derive( Debug, Serialize, Deserialize, PartialEq ) ]
81+
pub enum NodeRawKind
82+
{
83+
/// Read input from `stdin`.
84+
TriggerStdin( TriggerStdinRaw ),
85+
86+
/// Get output from LLM.
87+
AgentsCompletion( AgentsCompletionRaw ),
88+
89+
/// Print output to `stdout`.
90+
EventStdout( EventStdoutRaw ),
91+
}
92+
93+
impl format_tools::Fields< &'_ str, &'_ str > for NodeRawKind
94+
{
95+
type Key< 'k > = &'k str;
96+
type Val< 'v > = &'v str;
97+
98+
fn fields( &self ) -> impl format_tools::IteratorTrait< Item = ( &'_ str, &'_ str ) >
99+
{
100+
match self
101+
{
102+
Self::TriggerStdin( v ) => v.fields(),
103+
Self::AgentsCompletion( v ) => v.fields(),
104+
Self::EventStdout( v ) => v.fields(),
105+
}
106+
}
107+
}
108+
109+
/// Read input from `stdin`.
110+
#[ derive( Debug, Serialize, Deserialize, Former, PartialEq ) ]
111+
pub struct TriggerStdinRaw
112+
{
113+
pub prompt : String;
114+
}
115+
116+
impl From< TriggerStdinRaw > for NodeRawKind
117+
{
118+
fn from( value : TriggerStdinRaw ) -> Self
119+
{
120+
Self::TriggerStdin( value )
121+
}
122+
}
123+
124+
impl format_tools::Fields< &'_ str, &'_ str > for TriggerStdinRaw
125+
{
126+
type Key< 'k > = &'k str;
127+
type Val< 'v > = &'v str;
128+
129+
fn fields( &self ) -> impl format_tools::IteratorTrait< Item = ( &'_ str, &'_ str ) >
130+
{
131+
std::iter::once( ( "prompt", prompt.as_str() ) )
132+
}
133+
}
134+
135+
/// Get output from LLM.
136+
#[ derive( Debug, Serialize, Deserialize, Former, PartialEq ) ]
137+
pub struct AgentsCompletionRaw
138+
{
139+
/// Agent's system message template.
140+
pub system_message : String,
141+
142+
/// Agent's user message template.
143+
pub user_message : String,
144+
145+
/// Reuse chat history of other agent.
146+
pub agent_reuse : Option< String >,
147+
}
148+
149+
impl From< AgentsCompletionRaw > for NodeRawKind
150+
{
151+
fn from( value : AgentsCompletionRaw ) -> Self
152+
{
153+
Self::AgentsCompletion( value )
154+
}
155+
}
156+
157+
impl format_tools::Fields< &'_ str, &'_ str > for AgentsCompletionRaw
158+
{
159+
type Key< 'k > = &'k str;
160+
type Val< 'v > = &'v str;
161+
162+
fn fields( &self ) -> impl format_tools::IteratorTrait< Item = ( &'_ str, &'_ str ) >
163+
{
164+
let mut dst = Vec::new();
165+
166+
dst.push( ( "system_message", system_message.as_str() ) );
167+
dst.push( ( "user_message", user_message.as_str() ) );
168+
169+
if let Some( agent_reuse ) = agent_reuse
170+
{
171+
dst.push( ( "agent_reuse", agent_reuse.as_str() ) );
172+
}
173+
174+
dst.into_iter()
175+
}
176+
}
177+
178+
/// Print output to `stdout`.
179+
#[ derive( Debug, Serialize, Deserialize, Former, PartialEq ) ]
180+
pub struct EventStdoutRaw
181+
{
182+
/// Prompt to display to `stdout`.
183+
pub output : String,
184+
}
185+
186+
impl From< EventsStdoutRaw > for NodeRawKind
187+
{
188+
fn from( value : EventsStdoutRaw ) -> Self
189+
{
190+
Self::EventsStdout( value )
191+
}
192+
}
193+
194+
impl format_tools::Fields< &'_ str, &'_ str > for EventStdoutRaw
195+
{
196+
type Key< 'k > = &'k str;
197+
type Val< 'v > = &'v str;
198+
199+
fn fields( &self ) -> impl format_tools::IteratorTrait< Item = ( &'_ str, &'_ str ) >
200+
{
201+
let mut dst = Vec::new();
202+
203+
dst.push( ( "system_message", system_message.as_str() ) );
204+
dst.push( ( "user_message", user_message.as_str() ) );
205+
206+
if let Some( agent_reuse ) = agent_reuse
207+
{
208+
dst.push( ( "agent_reuse", agent_reuse.as_str() ) );
209+
}
210+
211+
dst.into_iter()
212+
}
213+
}
214+
62215
}
63216

64217
crate::mod_interface!
@@ -67,5 +220,6 @@ crate::mod_interface!
67220
{
68221
ScenarioRaw,
69222
NodeRaw,
223+
NodeRawKind,
70224
};
71225
}

0 commit comments

Comments
 (0)