1
1
import express from "express" ;
2
2
import bodyParser from "body-parser" ;
3
3
import cors from "cors" ;
4
+ import path from "path" ;
5
+ import fs from "fs" ;
4
6
5
7
import {
6
8
AgentRuntime ,
@@ -79,6 +81,16 @@ export function createApiRouter(
79
81
res . json ( { agents : agentsList } ) ;
80
82
} ) ;
81
83
84
+ router . get ( '/storage' , async ( req , res ) => {
85
+ try {
86
+ const uploadDir = path . join ( process . cwd ( ) , "data" , "characters" ) ;
87
+ const files = await fs . promises . readdir ( uploadDir ) ;
88
+ res . json ( { files } ) ;
89
+ } catch ( error ) {
90
+ res . status ( 500 ) . json ( { error : error . message } ) ;
91
+ }
92
+ } ) ;
93
+
82
94
router . get ( "/agents/:agentId" , ( req , res ) => {
83
95
const { agentId } = validateUUIDParams ( req . params , res ) ?? {
84
96
agentId : null ,
@@ -127,7 +139,7 @@ export function createApiRouter(
127
139
} ;
128
140
if ( ! agentId ) return ;
129
141
130
- const agent : AgentRuntime = agents . get ( agentId ) ;
142
+ let agent : AgentRuntime = agents . get ( agentId ) ;
131
143
132
144
// update character
133
145
if ( agent ) {
@@ -137,6 +149,9 @@ export function createApiRouter(
137
149
// if it has a different name, the agentId will change
138
150
}
139
151
152
+ // stores the json data before it is modified with added data
153
+ const characterJson = { ...req . body } ;
154
+
140
155
// load character from body
141
156
const character = req . body ;
142
157
try {
@@ -152,7 +167,7 @@ export function createApiRouter(
152
167
153
168
// start it up (and register it)
154
169
try {
155
- await directClient . startAgent ( character ) ;
170
+ agent = await directClient . startAgent ( character ) ;
156
171
elizaLogger . log ( `${ character . name } started` ) ;
157
172
} catch ( e ) {
158
173
elizaLogger . error ( `Error starting agent: ${ e } ` ) ;
@@ -162,6 +177,19 @@ export function createApiRouter(
162
177
} ) ;
163
178
return ;
164
179
}
180
+
181
+ if ( process . env . USE_CHARACTER_STORAGE === 'true' ) {
182
+ let filename = '' ;
183
+ try {
184
+ filename = `${ agent . agentId } .json` ;
185
+ const uploadDir = path . join ( process . cwd ( ) , "data" , "characters" ) ;
186
+ const filepath = path . join ( uploadDir , filename ) ;
187
+ await fs . promises . writeFile ( filepath , JSON . stringify ( { ...characterJson , id : agent . agentId } , null , 2 ) ) ;
188
+ } catch ( error ) {
189
+ console . log ( "error:" , error . message ) ;
190
+ }
191
+ }
192
+
165
193
res . json ( {
166
194
id : character . id ,
167
195
character : character ,
0 commit comments