1
+ import {
2
+ ActionExample ,
3
+ Content ,
4
+ generateText ,
5
+ HandlerCallback ,
6
+ IAgentRuntime ,
7
+ Memory ,
8
+ ModelClass ,
9
+ State ,
10
+ type Action ,
11
+ } from "@ai16z/eliza" ;
12
+
13
+
14
+ export const currentNewsAction : Action = {
15
+ name : "CURRENT_NEWS" ,
16
+ similes : [ "NEWS" , "GET_NEWS" , "GET_CURRENT_NEWS" ] ,
17
+ validate : async ( _runtime : IAgentRuntime , _message : Memory ) => {
18
+ return true ;
19
+ } ,
20
+ description :
21
+ "Get the latest news about a specific topic if asked by the user." ,
22
+ handler : async (
23
+ _runtime : IAgentRuntime ,
24
+ _message : Memory ,
25
+ _state : State ,
26
+ _options : { [ key : string ] : unknown ; } ,
27
+ _callback : HandlerCallback ,
28
+ ) : Promise < boolean > => {
29
+ async function getCurrentNews ( searchTerm : string ) {
30
+ try {
31
+ const apiKey = process . env . NEWS_API_KEY ;
32
+ if ( ! apiKey ) {
33
+ throw new Error ( 'NEWS_API_KEY environment variable is not set' ) ;
34
+ }
35
+
36
+ const response = await fetch ( `https://newsapi.org/v2/everything?q=${ searchTerm } &sortBy=publishedAt&apiKey=${ apiKey } ` ) ;
37
+ const data = await response . json ( ) ;
38
+
39
+ if ( ! data . articles || ! Array . isArray ( data . articles ) ) {
40
+ return "No news articles found." ;
41
+ }
42
+
43
+ return data . articles . slice ( 0 , 5 ) . map ( article => {
44
+ const content = article . content || article . description || "No content available" ;
45
+ return `${ article . title || "No title" } \n${ article . description || "No description" } \n${ article . url || "" } \n${ content . slice ( 0 , 1000 ) } ` ;
46
+ } ) . join ( "\n\n" ) ;
47
+ } catch ( error ) {
48
+ console . error ( "Error fetching news:" , error ) ;
49
+ return "Sorry, there was an error fetching the news." ;
50
+ }
51
+ }
52
+
53
+ const context = `Extract the search term from the {{userName}} message. The message is: ${ _message . content . text } . Only return the search term, no other text.`
54
+
55
+ const searchTerm = await generateText ( {
56
+ runtime : _runtime ,
57
+ context,
58
+ modelClass : ModelClass . SMALL ,
59
+ stop : [ "\n" ] ,
60
+ } ) ;
61
+
62
+ const currentNews = await getCurrentNews ( searchTerm ) ;
63
+ const responseText = ` *protocol droid noises*\n\n${ currentNews } ` ;
64
+
65
+
66
+ const newMemory : Memory = {
67
+ userId : _message . agentId ,
68
+ agentId : _message . agentId ,
69
+ roomId : _message . roomId ,
70
+ content : {
71
+ text : responseText ,
72
+ action : "CURRENT_NEWS_RESPONSE" ,
73
+ source : _message . content ?. source ,
74
+ } as Content ,
75
+ } ;
76
+
77
+ await _runtime . messageManager . createMemory ( newMemory ) ;
78
+
79
+ _callback ( newMemory . content ) ;
80
+ return true ;
81
+
82
+ } ,
83
+ examples : [
84
+ [
85
+ {
86
+ user : "{{user1}}" ,
87
+ content : { text : "what's the latest news about <searchTerm>?" } ,
88
+ } ,
89
+ {
90
+ user : "{{user2}}" ,
91
+ content : { text : "" , action : "CURRENT NEWS" } ,
92
+ } ,
93
+ ] ,
94
+
95
+ [
96
+ {
97
+ user : "{{user1}}" ,
98
+ content : { text : "can you show me the latest news about <searchTerm>?" } ,
99
+ } ,
100
+ {
101
+ user : "{{user2}}" ,
102
+ content : { text : "" , action : "CURRENT NEWS" } ,
103
+ } ,
104
+ ] ,
105
+
106
+ [
107
+ {
108
+ user : "{{user1}}" ,
109
+ content : { text : "what's in the <searchTerm> news today?" } ,
110
+ } ,
111
+ {
112
+ user : "{{user2}}" ,
113
+ content : { text : "" , action : "CURRENT NEWS" } ,
114
+ } ,
115
+ ] ,
116
+
117
+ [
118
+ {
119
+ user : "{{user1}}" ,
120
+ content : { text : "show me current events about <searchTerm>?" } ,
121
+ } ,
122
+ {
123
+ user : "{{user2}}" ,
124
+ content : { text : "" , action : "CURRENT NEWS" } ,
125
+ } ,
126
+ ] ,
127
+
128
+ [
129
+ {
130
+ user : "{{user1}}" ,
131
+ content : { text : "what's going on in the world of <searchTerm>?" } ,
132
+ } ,
133
+ {
134
+ user : "{{user2}}" ,
135
+ content : { text : "" , action : "CURRENT NEWS" } ,
136
+ } ,
137
+ ] ,
138
+
139
+ [
140
+ {
141
+ user : "{{user1}}" ,
142
+ content : { text : "give me the latest headlines about <searchTerm>?" } ,
143
+ } ,
144
+ {
145
+ user : "{{user2}}" ,
146
+ content : { text : "" , action : "CURRENT NEWS" } ,
147
+ } ,
148
+ ] ,
149
+
150
+ [
151
+ {
152
+ user : "{{user1}}" ,
153
+ content : { text : "show me news updates about <searchTerm>?" } ,
154
+ } ,
155
+ {
156
+ user : "{{user2}}" ,
157
+ content : { text : "" , action : "CURRENT NEWS" } ,
158
+ } ,
159
+ ] ,
160
+
161
+ [
162
+ {
163
+ user : "{{user1}}" ,
164
+ content : { text : "what are today's top stories about <searchTerm>?" } ,
165
+ } ,
166
+ {
167
+ user : "{{user2}}" ,
168
+ content : { text : "" , action : "CURRENT NEWS" } ,
169
+ } ,
170
+ ] ,
171
+ ] as ActionExample [ ] [ ] ,
172
+ } as Action ;
0 commit comments