@@ -6,12 +6,44 @@ import {
6
6
AgentRuntime ,
7
7
elizaLogger ,
8
8
getEnvVariable ,
9
+ UUID ,
9
10
validateCharacterConfig ,
10
11
} from "@elizaos/core" ;
11
12
12
13
import { REST , Routes } from "discord.js" ;
13
14
import { DirectClient } from "." ;
14
- import { stringToUuid } from "@elizaos/core" ;
15
+ import { validateUuid } from "@elizaos/core" ;
16
+
17
+ interface UUIDParams {
18
+ agentId : UUID ;
19
+ roomId ?: UUID ;
20
+ }
21
+
22
+ function validateUUIDParams (
23
+ params : { agentId : string ; roomId ?: string } ,
24
+ res : express . Response
25
+ ) : UUIDParams | null {
26
+ const agentId = validateUuid ( params . agentId ) ;
27
+ if ( ! agentId ) {
28
+ res . status ( 400 ) . json ( {
29
+ error : "Invalid AgentId format. Expected to be a UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ,
30
+ } ) ;
31
+ return null ;
32
+ }
33
+
34
+ if ( params . roomId ) {
35
+ const roomId = validateUuid ( params . roomId ) ;
36
+ if ( ! roomId ) {
37
+ res . status ( 400 ) . json ( {
38
+ error : "Invalid RoomId format. Expected to be a UUID: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" ,
39
+ } ) ;
40
+ return null ;
41
+ }
42
+ return { agentId, roomId } ;
43
+ }
44
+
45
+ return { agentId } ;
46
+ }
15
47
16
48
export function createApiRouter (
17
49
agents : Map < string , AgentRuntime > ,
@@ -46,7 +78,11 @@ export function createApiRouter(
46
78
} ) ;
47
79
48
80
router . get ( "/agents/:agentId" , ( req , res ) => {
49
- const agentId = req . params . agentId ;
81
+ const { agentId } = validateUUIDParams ( req . params , res ) ?? {
82
+ agentId : null ,
83
+ } ;
84
+ if ( ! agentId ) return ;
85
+
50
86
const agent = agents . get ( agentId ) ;
51
87
52
88
if ( ! agent ) {
@@ -66,8 +102,11 @@ export function createApiRouter(
66
102
} ) ;
67
103
68
104
router . post ( "/agents/:agentId/set" , async ( req , res ) => {
69
- const agentId = req . params . agentId ;
70
- console . log ( "agentId" , agentId ) ;
105
+ const { agentId } = validateUUIDParams ( req . params , res ) ?? {
106
+ agentId : null ,
107
+ } ;
108
+ if ( ! agentId ) return ;
109
+
71
110
let agent : AgentRuntime = agents . get ( agentId ) ;
72
111
73
112
// update character
@@ -102,7 +141,11 @@ export function createApiRouter(
102
141
} ) ;
103
142
104
143
router . get ( "/agents/:agentId/channels" , async ( req , res ) => {
105
- const agentId = req . params . agentId ;
144
+ const { agentId } = validateUUIDParams ( req . params , res ) ?? {
145
+ agentId : null ,
146
+ } ;
147
+ if ( ! agentId ) return ;
148
+
106
149
const runtime = agents . get ( agentId ) ;
107
150
108
151
if ( ! runtime ) {
@@ -128,8 +171,12 @@ export function createApiRouter(
128
171
} ) ;
129
172
130
173
router . get ( "/agents/:agentId/:roomId/memories" , async ( req , res ) => {
131
- const agentId = req . params . agentId ;
132
- const roomId = stringToUuid ( req . params . roomId ) ;
174
+ const { agentId, roomId } = validateUUIDParams ( req . params , res ) ?? {
175
+ agentId : null ,
176
+ roomId : null ,
177
+ } ;
178
+ if ( ! agentId || ! roomId ) return ;
179
+
133
180
let runtime = agents . get ( agentId ) ;
134
181
135
182
// if runtime is null, look for runtime with the same name
@@ -146,7 +193,7 @@ export function createApiRouter(
146
193
147
194
try {
148
195
const memories = await runtime . messageManager . getMemories ( {
149
- roomId,
196
+ roomId : roomId ,
150
197
} ) ;
151
198
const response = {
152
199
agentId,
0 commit comments