1
1
using Microsoft . AutoGen . Agents . Abstractions ;
2
+ using Microsoft . Extensions . DependencyInjection ;
2
3
using Microsoft . Extensions . Logging ;
3
4
4
5
namespace Microsoft . AutoGen . Agents . Client ;
5
6
6
7
[ TopicSubscription ( "FileIO" ) ]
7
- public class FileAgent : IOAgent < AgentState > ,
8
+ public abstract class FileAgent (
9
+ IAgentContext context ,
10
+ [ FromKeyedServices ( "EventTypes" ) ] EventTypes typeRegistry ,
11
+ string inputPath = "input.txt" ,
12
+ string outputPath = "output.txt"
13
+ ) : IOAgent < AgentState > ( context , typeRegistry ) ,
8
14
IUseFiles ,
9
15
IHandle < Input > ,
10
16
IHandle < Output >
11
17
{
12
- public FileAgent ( IAgentContext context , EventTypes typeRegistry , string filePath ) : base ( context , typeRegistry )
13
- {
14
- _filePath = filePath ;
15
- }
16
- private readonly string _filePath ;
17
-
18
18
public override async Task Handle ( Input item )
19
19
{
20
-
21
20
// validate that the file exists
22
- if ( ! File . Exists ( _filePath ) )
21
+ if ( ! File . Exists ( inputPath ) )
23
22
{
24
- string errorMessage = $ "File not found: { _filePath } ";
23
+ var errorMessage = $ "File not found: { inputPath } ";
25
24
Logger . LogError ( errorMessage ) ;
26
25
//publish IOError event
27
26
var err = new IOError
@@ -31,36 +30,30 @@ public override async Task Handle(Input item)
31
30
await PublishEvent ( err ) ;
32
31
return ;
33
32
}
34
-
35
33
string content ;
36
34
using ( var reader = new StreamReader ( item . Message ) )
37
35
{
38
36
content = await reader . ReadToEndAsync ( ) ;
39
37
}
40
-
41
38
await ProcessInput ( content ) ;
42
-
43
39
var evt = new InputProcessed
44
40
{
45
41
Route = _route
46
42
} . ToCloudEvent ( this . AgentId . Key ) ;
47
43
await PublishEvent ( evt ) ;
48
44
}
49
-
50
45
public override async Task Handle ( Output item )
51
46
{
52
- using ( var writer = new StreamWriter ( _filePath , append : true ) )
47
+ using ( var writer = new StreamWriter ( outputPath , append : true ) )
53
48
{
54
49
await writer . WriteLineAsync ( item . Message ) ;
55
50
}
56
-
57
51
var evt = new OutputWritten
58
52
{
59
53
Route = _route
60
54
} . ToCloudEvent ( this . AgentId . Key ) ;
61
55
await PublishEvent ( evt ) ;
62
56
}
63
-
64
57
public override async Task < string > ProcessInput ( string message )
65
58
{
66
59
var evt = new InputProcessed
@@ -70,14 +63,12 @@ public override async Task<string> ProcessInput(string message)
70
63
await PublishEvent ( evt ) ;
71
64
return message ;
72
65
}
73
-
74
66
public override Task ProcessOutput ( string message )
75
67
{
76
68
// Implement your output processing logic here
77
69
return Task . CompletedTask ;
78
70
}
79
71
}
80
-
81
72
public interface IUseFiles
82
73
{
83
74
}
0 commit comments