@@ -9,80 +9,98 @@ import { PrismaClient } from '@magickml/server-db'
99import { v4 as uuidv4 } from 'uuid'
1010import fs from 'fs'
1111
12- const prisma = new PrismaClient ( )
12+ // Create a single PrismaClient instance to be reused
13+ const prisma = new PrismaClient ( {
14+ log : [ 'error' ] ,
15+ datasources : {
16+ db : {
17+ url : process . env . DATABASE_URL ,
18+ } ,
19+ } ,
20+ } )
1321
1422type Config = NitroRuntimeConfig & AgentInterface
1523
1624export default defineNitroPlugin ( async nitroApp => {
17- const app = ( await initApp ( ) ) as any
18-
19- const runtimeConfig = useRuntimeConfig < Config > ( )
20- const config = { ...runtimeConfig }
21-
22- nitroApp . agentServer = app
25+ try {
26+ const app = ( await initApp ( ) ) as any
27+ const runtimeConfig = useRuntimeConfig < Config > ( )
28+ const config = { ...runtimeConfig }
2329
24- let agentId : string | undefined
30+ nitroApp . agentServer = app
2531
26- try {
27- const configFile = fs . readFileSync ( 'agent-config.json' , 'utf8' )
28- const configData = JSON . parse ( configFile )
29- agentId = configData . AGENT_ID
30- } catch ( error ) {
31- console . error ( 'Error reading agent-config.json:' , error )
32- }
32+ let agentId : string | undefined
3333
34- agentId = agentId || runtimeConfig . agentId || uuidv4 ( )
34+ try {
35+ const configFile = fs . readFileSync ( 'agent-config.json' , 'utf8' )
36+ const configData = JSON . parse ( configFile )
37+ agentId = configData . AGENT_ID
38+ } catch ( error ) {
39+ console . error ( 'Error reading agent-config.json:' , error )
40+ }
3541
36- const existingAgent = await prisma . agents . findUnique ( {
37- where : {
38- id : agentId ,
39- } ,
40- } )
41-
42- if ( ! existingAgent ) {
43- const agent = await prisma . agents . create ( {
44- data : {
45- id : agentId as string ,
46- name : agentId as string ,
47- enabled : true ,
48- version : '2.0' ,
49- updatedAt : new Date ( ) . toISOString ( ) ,
50- createdAt : new Date ( ) . toISOString ( ) ,
51- isDraft : false ,
52- projectId : runtimeConfig . projectId || 'default' ,
53- worldId : runtimeConfig . worldId || 'default' ,
54- } ,
42+ agentId = agentId || runtimeConfig . agentId || uuidv4 ( )
43+
44+ // Use a single transaction for database operations
45+ const agent = await prisma . $transaction ( async tx => {
46+ const existingAgent = await tx . agents . findUnique ( {
47+ where : { id : agentId } ,
48+ } )
49+
50+ if ( ! existingAgent ) {
51+ const newAgent = await tx . agents . create ( {
52+ data : {
53+ id : agentId as string ,
54+ name : agentId as string ,
55+ enabled : true ,
56+ version : '2.0' ,
57+ updatedAt : new Date ( ) . toISOString ( ) ,
58+ createdAt : new Date ( ) . toISOString ( ) ,
59+ isDraft : false ,
60+ projectId : runtimeConfig . projectId || 'default' ,
61+ worldId : runtimeConfig . worldId || 'default' ,
62+ } ,
63+ } )
64+
65+ const configData = { AGENT_ID : newAgent . id }
66+ fs . writeFileSync (
67+ 'agent-config.json' ,
68+ JSON . stringify ( configData , null , 2 )
69+ )
70+
71+ config . agentId = newAgent . id
72+ config . id = newAgent . id
73+
74+ return newAgent
75+ }
76+
77+ config . agentId = existingAgent . id
78+ config . projectId = existingAgent . projectId || 'default'
79+ config . id = existingAgent . id
80+
81+ return existingAgent
5582 } )
5683
57- console . log ( 'Agent created:' , agent . id )
58- console . log ( 'AGHHHH' )
84+ console . log ( 'Agent configured:' , agent . id )
5985
60- // Double-check that the agent was created
61- const verifyAgent = await prisma . agents . findUnique ( {
62- where : { id : agent . id } ,
63- } )
86+ // Create agent instance
87+ const agentInstance = new Agent ( config , app . get ( 'pubsub' ) , app )
88+ await agentInstance . waitForInitialization ( )
89+ await agentInstance . spellbook . loadSpells ( magickSpells )
6490
65- if ( ! verifyAgent ) {
66- console . error ( 'Agent creation failed or not immediately visible' )
67- } else {
68- console . log ( 'Agent verified in database' )
69- }
91+ nitroApp . agent = agentInstance
7092
71- const configData = { AGENT_ID : agent . id }
72- fs . writeFileSync ( 'agent-config.json' , JSON . stringify ( configData , null , 2 ) )
93+ // Add cleanup on process termination
94+ const cleanup = async ( ) => {
95+ await prisma . $disconnect ( )
96+ // Add any other cleanup needed
97+ }
7398
74- config . agentId = agent . id
75- config . id = agent . id
76- } else {
77- console . log ( 'Existing agent found:' , existingAgent . id )
78- config . agentId = existingAgent . id
79- config . projectId = existingAgent . projectId || 'default'
80- config . id = existingAgent . id
99+ process . on ( 'SIGINT' , cleanup )
100+ process . on ( 'SIGTERM' , cleanup )
101+ } catch ( error ) {
102+ console . error ( 'Error initializing agent:' , error )
103+ await prisma . $disconnect ( )
104+ throw error
81105 }
82- // // use data and app to create agent
83- const agent = new Agent ( config , app . get ( 'pubsub' ) , app )
84- await agent . waitForInitialization ( )
85- await agent . spellbook . loadSpells ( magickSpells )
86-
87- nitroApp . agent = agent
88106} )
0 commit comments