Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 22 additions & 16 deletions packages/core/src/runtime.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2255,27 +2255,33 @@ export class AgentRuntime implements IAgentRuntime {
throw new Error('Agent name is required');
}

const agents = await this.adapter.getAgents();
const existingAgentId = agents.find((a) => a.name === agent.name)?.id;

if (existingAgentId) {
// Update the agent on restart with the latest character configuration
const updatedAgent = {
...agent,
id: existingAgentId,
updatedAt: Date.now(),
};
try {
const agents = await this.adapter.getAgents();
const existingAgentId = agents.find((a) => a.name === agent.name)?.id;

if (existingAgentId) {
// Update the agent on restart with the latest character configuration
const updatedAgent = {
...agent,
id: existingAgentId,
updatedAt: Date.now(),
};

await this.adapter.updateAgent(existingAgentId, updatedAgent);
const existingAgent = await this.adapter.getAgent(existingAgentId);
await this.adapter.updateAgent(existingAgentId, updatedAgent);
const existingAgent = await this.adapter.getAgent(existingAgentId);

if (!existingAgent) {
throw new Error(`Failed to retrieve agent after update: ${existingAgentId}`);
if (!existingAgent) {
throw new Error(`Failed to retrieve agent after update: ${existingAgentId}`);
}

this.logger.debug(`Updated existing agent ${agent.name} on restart`);
return existingAgent;
}

this.logger.debug(`Updated existing agent ${agent.name} on restart`);
return existingAgent;
} catch (error: any) {
this.logger.error('Failed to ensure agent exists:', error);
}


// Create new agent if it doesn't exist
const newAgent: Agent = {
Expand Down