1
+ import { Action , IAgentRuntime , Memory , State } from "@ai16z/eliza" ;
2
+ import { dadJokeActionContent , dadJokeData } from "./types.ts" ;
3
+ import { dadJokeProvider } from "./provider.ts" ;
4
+
5
+ export const getDadJokeAction : Action = {
6
+ name : "GET_DAD_JOKE" ,
7
+ description : "Retrieves a random dad joke" ,
8
+ similes : [
9
+ "DAD_JOKE" ,
10
+ "JOKE" ,
11
+ ] ,
12
+ examples : [
13
+ [
14
+ {
15
+ user : "{{user1}}" ,
16
+ content : {
17
+ text : "Tell me a dad joke" ,
18
+ } as dadJokeActionContent ,
19
+ } ,
20
+ {
21
+ user : "{{agentName}}" ,
22
+ content : {
23
+ text : "Why couldn't the bicycle stand up by itself? It was two tired." ,
24
+ action : "GET_DAD_JOKE" ,
25
+ } ,
26
+ } ,
27
+
28
+ ] ,
29
+ [
30
+ {
31
+ user : "{{user1}}" ,
32
+ content : {
33
+ text : "You got any dad jokes?" ,
34
+ } as dadJokeActionContent ,
35
+ } ,
36
+ {
37
+ user : "{{agentName}}" ,
38
+ content : {
39
+ text : "What do you call a fake noodle? An impasta." ,
40
+ action : "GET_DAD_JOKE" ,
41
+ } ,
42
+ } ,
43
+ ] ,
44
+ ] ,
45
+
46
+ validate : async (
47
+ runtime : IAgentRuntime ,
48
+ message : Memory ,
49
+ state ?: State ,
50
+ ) : Promise < boolean > => {
51
+ try {
52
+ const content = message . content as dadJokeActionContent ;
53
+ return (
54
+ typeof content . text === "string" &&
55
+ content . text . toLowerCase ( ) . includes ( "joke" )
56
+ ) ;
57
+ } catch {
58
+ return false ;
59
+ }
60
+ } ,
61
+
62
+ handler : async (
63
+ runtime : IAgentRuntime ,
64
+ message : Memory ,
65
+ state ?: State ,
66
+ ) : Promise < dadJokeData > => {
67
+ try {
68
+ const response = await dadJokeProvider . get ( runtime , message , state ) ;
69
+ if ( ! response . success ) {
70
+ throw new Error ( response . error ) ;
71
+ }
72
+
73
+ return response . data ;
74
+ } catch ( error ) {
75
+ throw new Error ( error instanceof Error ? error . message : "Failed to retrieve dad joke" ) ;
76
+ }
77
+ }
78
+ }
0 commit comments