Skip to content

Commit 209dab4

Browse files
author
InAnYan
committed
Implement ScenarioProcessed
1 parent dcd6d0f commit 209dab4

File tree

2 files changed

+32
-244
lines changed

2 files changed

+32
-244
lines changed

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

Lines changed: 20 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,28 @@
77
88
mod private
99
{
10-
use serde::
11-
{
12-
Serialize,
13-
Deserialize,
14-
};
10+
use std::collections::HashMap;
1511

1612
use crate::*;
1713
use agents::
1814
{
1915
path::Path,
20-
scenario_raw::ScenarioRaw,
16+
scenario_raw::
17+
{
18+
ScenarioRaw,
19+
NodeRaw,
20+
},
2121
};
2222

2323
/// New type for templates in scenarios.
24-
#[ derive( Debug, Serialize, Deserialize, PartialEq ) ]
24+
#[ derive( Debug, PartialEq ) ]
2525
pub struct TemplateBody( pub String );
2626

2727
/// Struct that represents user written scenarios.
2828
///
29-
/// This is a processed form of a scenario, templates and paths are distinguished here with types.
29+
/// This is a processed form of a scenario, paths are distinguished here with types.
3030
/// For more simplistic representation of scenarios, use `ScenarioRaw` type.
31-
#[ derive( Debug, Serialize, Deserialize, PartialEq ) ]
31+
#[ derive( Debug, PartialEq ) ]
3232
pub struct ScenarioProcessed
3333
{
3434
/// Nodes in the scenario.
@@ -41,17 +41,18 @@ mod private
4141

4242
fn try_from( scenario_raw : ScenarioRaw ) -> Result< Self, Self::Error >
4343
{
44-
let nodes : Result< Vec< Node >, Self::Error > = scenario_raw.nodes.into_iter().map( | n | n.try_from() ).collect();
44+
let nodes : Result< Vec< Node >, Self::Error > =
45+
scenario_raw.nodes.into_iter().map( | rn | Node::try_from( rn ) ).collect();
4546

46-
Ok( Self { nodes } )
47+
Ok( Self { nodes : nodes? } )
4748
}
4849
}
4950

5051
/// Node representation in a scenario file.
5152
///
52-
/// This is a processed form of a node, templates and paths are distinguished here with types.
53+
/// This is a processed form of a node, paths are distinguished here with types.
5354
/// For more simplistic representation of scenarios, use `NodeRaw` type.
54-
#[ derive( Debug, Serialize, Deserialize, PartialEq ) ]
55+
#[ derive( Debug, PartialEq ) ]
5556
pub struct Node
5657
{
5758
/// ID of the node. Must be unique, will also identify node output.
@@ -60,8 +61,8 @@ mod private
6061
/// Type of the node.
6162
pub r#type : Path,
6263

63-
/// Specific type of `Node`.
64-
pub kind : NodeKind,
64+
/// Parameters of the node.
65+
pub params : HashMap< String, String >,
6566

6667
/// ID of the next node to execute.
6768
pub next : Path,
@@ -71,80 +72,20 @@ mod private
7172
{
7273
type Error = std::io::Error;
7374

74-
fn try_form( node_raw : NodeRaw ) -> Result< Self, Self::Error >
75+
fn try_from( node_raw : NodeRaw ) -> Result< Self, Self::Error >
7576
{
7677
Ok
7778
(
7879
Self
7980
{
8081
id : node_raw.id,
8182
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 )?,
83+
params : node_raw.params,
84+
next : Path::try_from( node_raw.next )?,
8485
}
8586
)
8687
}
8788
}
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-
}
14889
}
14990

15091
crate::mod_interface!
@@ -153,5 +94,6 @@ crate::mod_interface!
15394
{
15495
TemplateBody,
15596
ScenarioProcessed,
97+
Node,
15698
};
15799
}

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

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

1115
use former::Former;
1216
use serde::
1317
{
14-
serialize,
15-
deserialize,
18+
Serialize,
19+
Deserialize,
1620
};
1721

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

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

51-
/// Specific type of `NodeRaw`.
52-
pub kind : NodeRawKind,
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 >,
5358

54-
/// ID of the next node to execute.
59+
/// ID of the next node to execute. Represented as a path.
5560
pub next : String,
5661
}
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-
21562
}
21663

21764
crate::mod_interface!
@@ -220,6 +67,5 @@ crate::mod_interface!
22067
{
22168
ScenarioRaw,
22269
NodeRaw,
223-
NodeRawKind,
22470
};
22571
}

0 commit comments

Comments
 (0)