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