@@ -8,47 +8,16 @@ import {
8
8
type AgentRuntime ,
9
9
elizaLogger ,
10
10
getEnvVariable ,
11
- type UUID ,
12
- validateCharacterConfig ,
13
11
ServiceType ,
14
12
type Character ,
13
+ settings ,
15
14
} from "@elizaos/core" ;
16
15
17
16
import type { TeeLogQuery , TeeLogService } from "@elizaos/plugin-tee-log" ;
18
17
import { REST , Routes } from "discord.js" ;
19
18
import type { DirectClient } from "." ;
20
- import { validateUuid } from "@elizaos/core" ;
21
-
22
- interface UUIDParams {
23
- agentId : UUID ;
24
- roomId ?: UUID ;
25
- }
26
-
27
- function validateUUIDParams (
28
- params : { agentId : string ; roomId ?: string } ,
29
- res : express . Response
30
- ) : UUIDParams | null {
31
- const agentId = validateUuid ( params . agentId ) ;
32
- if ( ! agentId ) {
33
- res . status ( 400 ) . json ( {
34
- error : "Invalid AgentId format. Expected to be a UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ,
35
- } ) ;
36
- return null ;
37
- }
38
-
39
- if ( params . roomId ) {
40
- const roomId = validateUuid ( params . roomId ) ;
41
- if ( ! roomId ) {
42
- res . status ( 400 ) . json ( {
43
- error : "Invalid RoomId format. Expected to be a UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ,
44
- } ) ;
45
- return null ;
46
- }
47
- return { agentId, roomId } ;
48
- }
49
-
50
- return { agentId } ;
51
- }
19
+ import { validateUUIDParams } from "." ;
20
+ import { md5 , signToken } from "./auth" ;
52
21
53
22
export function createApiRouter (
54
23
agents : Map < string , AgentRuntime > ,
@@ -65,14 +34,6 @@ export function createApiRouter(
65
34
} )
66
35
) ;
67
36
68
- router . get ( "/" , ( req , res ) => {
69
- res . send ( "Welcome, this is the REST API!" ) ;
70
- } ) ;
71
-
72
- router . get ( "/hello" , ( req , res ) => {
73
- res . json ( { message : "Hello World!" } ) ;
74
- } ) ;
75
-
76
37
router . get ( "/agents" , ( req , res ) => {
77
38
const agentsList = Array . from ( agents . values ( ) ) . map ( ( agent ) => ( {
78
39
id : agent . agentId ,
@@ -116,102 +77,6 @@ export function createApiRouter(
116
77
} ) ;
117
78
} ) ;
118
79
119
- router . delete ( "/agents/:agentId" , async ( req , res ) => {
120
- const { agentId } = validateUUIDParams ( req . params , res ) ?? {
121
- agentId : null ,
122
- } ;
123
- if ( ! agentId ) return ;
124
-
125
- const agent : AgentRuntime = agents . get ( agentId ) ;
126
-
127
- if ( agent ) {
128
- agent . stop ( ) ;
129
- directClient . unregisterAgent ( agent ) ;
130
- res . status ( 204 ) . json ( { success : true } ) ;
131
- } else {
132
- res . status ( 404 ) . json ( { error : "Agent not found" } ) ;
133
- }
134
- } ) ;
135
-
136
- router . post ( "/agents/:agentId/set" , async ( req , res ) => {
137
- const { agentId } = validateUUIDParams ( req . params , res ) ?? {
138
- agentId : null ,
139
- } ;
140
- if ( ! agentId ) return ;
141
-
142
- let agent : AgentRuntime = agents . get ( agentId ) ;
143
-
144
- // update character
145
- if ( agent ) {
146
- // stop agent
147
- agent . stop ( ) ;
148
- directClient . unregisterAgent ( agent ) ;
149
- // if it has a different name, the agentId will change
150
- }
151
-
152
- // stores the json data before it is modified with added data
153
- const characterJson = { ...req . body } ;
154
-
155
- // load character from body
156
- const character = req . body ;
157
- try {
158
- validateCharacterConfig ( character ) ;
159
- } catch ( e ) {
160
- elizaLogger . error ( `Error parsing character: ${ e } ` ) ;
161
- res . status ( 400 ) . json ( {
162
- success : false ,
163
- message : e . message ,
164
- } ) ;
165
- return ;
166
- }
167
-
168
- // start it up (and register it)
169
- try {
170
- agent = await directClient . startAgent ( character ) ;
171
- elizaLogger . log ( `${ character . name } started` ) ;
172
- } catch ( e ) {
173
- elizaLogger . error ( `Error starting agent: ${ e } ` ) ;
174
- res . status ( 500 ) . json ( {
175
- success : false ,
176
- message : e . message ,
177
- } ) ;
178
- return ;
179
- }
180
-
181
- if ( process . env . USE_CHARACTER_STORAGE === "true" ) {
182
- try {
183
- const filename = `${ agent . agentId } .json` ;
184
- const uploadDir = path . join (
185
- process . cwd ( ) ,
186
- "data" ,
187
- "characters"
188
- ) ;
189
- const filepath = path . join ( uploadDir , filename ) ;
190
- await fs . promises . mkdir ( uploadDir , { recursive : true } ) ;
191
- await fs . promises . writeFile (
192
- filepath ,
193
- JSON . stringify (
194
- { ...characterJson , id : agent . agentId } ,
195
- null ,
196
- 2
197
- )
198
- ) ;
199
- elizaLogger . info (
200
- `Character stored successfully at ${ filepath } `
201
- ) ;
202
- } catch ( error ) {
203
- elizaLogger . error (
204
- `Failed to store character: ${ error . message } `
205
- ) ;
206
- }
207
- }
208
-
209
- res . json ( {
210
- id : character . id ,
211
- character : character ,
212
- } ) ;
213
- } ) ;
214
-
215
80
router . get ( "/agents/:agentId/channels" , async ( req , res ) => {
216
81
const { agentId } = validateUUIDParams ( req . params , res ) ?? {
217
82
agentId : null ,
@@ -404,53 +269,14 @@ export function createApiRouter(
404
269
}
405
270
) ;
406
271
407
- router . post ( "/agent/start" , async ( req , res ) => {
408
- const { characterPath, characterJson } = req . body ;
409
- console . log ( "characterPath:" , characterPath ) ;
410
- console . log ( "characterJson:" , characterJson ) ;
411
- try {
412
- let character : Character ;
413
- if ( characterJson ) {
414
- character = await directClient . jsonToCharacter (
415
- characterPath ,
416
- characterJson
417
- ) ;
418
- } else if ( characterPath ) {
419
- character =
420
- await directClient . loadCharacterTryPath ( characterPath ) ;
421
- } else {
422
- throw new Error ( "No character path or JSON provided" ) ;
423
- }
424
- await directClient . startAgent ( character ) ;
425
- elizaLogger . log ( `${ character . name } started` ) ;
426
-
427
- res . json ( {
428
- id : character . id ,
429
- character : character ,
430
- } ) ;
431
- } catch ( e ) {
432
- elizaLogger . error ( `Error parsing character: ${ e } ` ) ;
433
- res . status ( 400 ) . json ( {
434
- error : e . message ,
435
- } ) ;
436
- return ;
437
- }
438
- } ) ;
439
-
440
- router . post ( "/agents/:agentId/stop" , async ( req , res ) => {
441
- const agentId = req . params . agentId ;
442
- console . log ( "agentId" , agentId ) ;
443
- const agent : AgentRuntime = agents . get ( agentId ) ;
444
-
445
- // update character
446
- if ( agent ) {
447
- // stop agent
448
- agent . stop ( ) ;
449
- directClient . unregisterAgent ( agent ) ;
450
- // if it has a different name, the agentId will change
451
- res . json ( { success : true } ) ;
272
+ router . post ( "/auth/login" , async ( req , res ) => {
273
+ const { username, password } = req . body ;
274
+ const valid = username === settings . JWT_USERNAME && password === md5 ( settings . JWT_PASSWORD ) ;
275
+ if ( valid ) {
276
+ const token = signToken ( { username } ) ;
277
+ res . json ( { success : true , token : token } ) ;
452
278
} else {
453
- res . status ( 404 ) . json ( { error : "Agent not found " } ) ;
279
+ res . status ( 401 ) . json ( { error : "Invalid username or password " } ) ;
454
280
}
455
281
} ) ;
456
282
0 commit comments