1
+ import { elizaLogger } from "@elizaos/core" ;
2
+ import {
3
+ ActionExample ,
4
+ Content ,
5
+ HandlerCallback ,
6
+ IAgentRuntime ,
7
+ Memory ,
8
+ ModelClass ,
9
+ State ,
10
+ type Action ,
11
+ } from "@elizaos/core" ;
12
+ import { composeContext } from "@elizaos/core" ;
13
+ import { generateObjectDeprecated } from "@elizaos/core" ;
14
+ import { ACTIONS } from "solana-agent-kit" ;
15
+ import { getSAK } from "../utils" ;
16
+
17
+ const GIBWORK_ACTION = ACTIONS . CREATE_GIBWORK_TASK_ACTION ;
18
+
19
+ export interface GibWorkContent extends Content {
20
+ title : string ;
21
+ content : string ;
22
+ requirements : string ;
23
+ tags : string [ ] ;
24
+ tokenMintAddress : string ;
25
+ tokenAmount : number ;
26
+ }
27
+
28
+ function isGibWorkContent (
29
+ runtime : IAgentRuntime ,
30
+ content : any
31
+ ) : content is GibWorkContent {
32
+ elizaLogger . log ( "Content for gibwork" , content ) ;
33
+ return (
34
+ typeof content . title === "string" &&
35
+ typeof content . content === "string" &&
36
+ typeof content . requirements === "string" &&
37
+ Array . isArray ( content . tags ) &&
38
+ typeof content . tokenMintAddress === "string" &&
39
+ typeof content . tokenAmount === "number"
40
+ ) ;
41
+ }
42
+
43
+ const gibworkTemplate = `Respond with a JSON markdown block containing only the extracted values. Use null for any values that cannot be determined.
44
+
45
+ Example response:
46
+ \`\`\`json
47
+ {
48
+ "title": "Build a Solana dApp",
49
+ "content": "Create a simple Solana dApp with React frontend",
50
+ "requirements": "Experience with Rust and React",
51
+ "tags": ["solana", "rust", "react"],
52
+ "tokenMintAddress": "EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v",
53
+ "tokenAmount": 100
54
+ }
55
+ \`\`\`
56
+
57
+ {{recentMessages}}
58
+
59
+ Given the recent messages, extract the following information about the GibWork task:
60
+ - Title of the task
61
+ - Content/description of the task
62
+ - Requirements for the task
63
+ - Tags related to the task
64
+ - Token mint address for payment
65
+ - Token amount for payment
66
+
67
+ Respond with a JSON markdown block containing only the extracted values.` ;
68
+
69
+ export default {
70
+ name : GIBWORK_ACTION . name ,
71
+ similes : GIBWORK_ACTION . similes ,
72
+ validate : async ( runtime : IAgentRuntime , message : Memory ) => {
73
+ elizaLogger . log ( "Validating gibwork task from user:" , message . userId ) ;
74
+ return false ;
75
+ } ,
76
+ description : GIBWORK_ACTION . description ,
77
+ handler : async (
78
+ runtime : IAgentRuntime ,
79
+ message : Memory ,
80
+ state : State ,
81
+ _options : { [ key : string ] : unknown } ,
82
+ callback ?: HandlerCallback
83
+ ) : Promise < boolean > => {
84
+ elizaLogger . log ( "Starting CREATE_GIBWORK_TASK handler..." ) ;
85
+ const sak = await getSAK ( runtime ) ;
86
+
87
+ // Initialize or update state
88
+ if ( ! state ) {
89
+ state = ( await runtime . composeState ( message ) ) as State ;
90
+ } else {
91
+ state = await runtime . updateRecentMessageState ( state ) ;
92
+ }
93
+
94
+ // Compose gibwork context
95
+ const gibworkContext = composeContext ( {
96
+ state,
97
+ template : gibworkTemplate ,
98
+ } ) ;
99
+
100
+ // Generate gibwork content
101
+ const content = await generateObjectDeprecated ( {
102
+ runtime,
103
+ context : gibworkContext ,
104
+ modelClass : ModelClass . LARGE ,
105
+ } ) ;
106
+
107
+ // Validate gibwork content
108
+ if ( ! isGibWorkContent ( runtime , content ) ) {
109
+ elizaLogger . error ( "Invalid content for CREATE_GIBWORK_TASK action." ) ;
110
+ if ( callback ) {
111
+ callback ( {
112
+ text : "Unable to process GibWork task creation. Invalid content provided." ,
113
+ content : { error : "Invalid gibwork content" } ,
114
+ } ) ;
115
+ }
116
+ return false ;
117
+ }
118
+
119
+ try {
120
+ const gibworkResult = await sak . createGibworkTask (
121
+ content . title ,
122
+ content . content ,
123
+ content . requirements ,
124
+ content . tags ,
125
+ content . tokenMintAddress ,
126
+ content . tokenAmount
127
+ ) ;
128
+
129
+ console . log ( "GibWork task creation result:" , gibworkResult ) ;
130
+
131
+ if ( callback ) {
132
+ callback ( {
133
+ text : `Successfully created GibWork task: ${ content . title } ` ,
134
+ content : {
135
+ success : true ,
136
+ gibworkResult : gibworkResult ,
137
+ } ,
138
+ } ) ;
139
+ }
140
+
141
+ return true ;
142
+ } catch ( error ) {
143
+ elizaLogger . error ( "Error during GibWork task creation:" , error ) ;
144
+ if ( callback ) {
145
+ callback ( {
146
+ text : `Error creating GibWork task: ${ error . message } ` ,
147
+ content : { error : error . message } ,
148
+ } ) ;
149
+ }
150
+ return false ;
151
+ }
152
+ } ,
153
+
154
+ examples : [
155
+ [
156
+ {
157
+ user : "{{user1}}" ,
158
+ content : {
159
+ text : "Create a GibWork task for building a Solana dApp, offering 100 USDC" ,
160
+ } ,
161
+ } ,
162
+ {
163
+ user : "{{user2}}" ,
164
+ content : {
165
+ text : "Creating GibWork task" ,
166
+ action : "CREATE_GIBWORK_TASK" ,
167
+ } ,
168
+ } ,
169
+ {
170
+ user : "{{user2}}" ,
171
+ content : {
172
+ text : "Successfully created GibWork task: Build a Solana dApp" ,
173
+ } ,
174
+ } ,
175
+ ] ,
176
+ ] as ActionExample [ ] [ ] ,
177
+ } as Action ;
0 commit comments