Skip to content

Commit 0a566bd

Browse files
authored
Merge pull request elizaOS#4028 from elizaOS/tcm/fix-cli-agent
fix: cli agent command
2 parents e45c275 + d4f31c1 commit 0a566bd

File tree

2 files changed

+82
-15
lines changed

2 files changed

+82
-15
lines changed

packages/cli/src/commands/agent.ts

+5-14
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import type { Agent } from '@elizaos/core';
77
import { Command } from 'commander';
88

99
const AGENT_RUNTIME_URL =
10-
process.env.AGENT_RUNTIME_URL?.replace(/\/$/, '') || 'http://localhost:3000';
11-
const AGENTS_BASE_URL = `${AGENT_RUNTIME_URL}/agents`;
10+
process.env.AGENT_RUNTIME_URL?.replace(/\/$/, '') ||
11+
`http://localhost:${process.env.SERVER_PORT}`;
12+
const AGENTS_BASE_URL = `${AGENT_RUNTIME_URL}/api/agents`;
1213

1314
// Define basic agent interface for type safety
1415
/**
@@ -109,16 +110,6 @@ interface ApiResponse<T> {
109110
};
110111
}
111112

112-
/**
113-
* Interface representing the response from starting an agent.
114-
* @property {string} id - The unique identifier for the response.
115-
* @property {Partial<Agent>} character - The partial information of the Agent object associated with the response.
116-
*/
117-
interface AgentStartResponse {
118-
id: string;
119-
character: Partial<Agent>;
120-
}
121-
122113
agent
123114
.command('list')
124115
.alias('ls')
@@ -283,14 +274,14 @@ agent
283274
);
284275
}
285276

286-
const data = (await response.json()) as ApiResponse<AgentStartResponse>;
277+
const data = (await response.json()) as ApiResponse<Agent>;
287278
const result = data.data;
288279

289280
if (!result) {
290281
throw new Error('Failed to start agent: No data returned from server');
291282
}
292283

293-
logger.debug(`Successfully started agent ${result.character.name} (${result.id})`);
284+
logger.debug(`Successfully started agent ${result.name} (${result.id})`);
294285
} catch (error) {
295286
handleError(error);
296287
}

packages/plugin-sql/src/base.ts

+77-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import {
2626
lte,
2727
or,
2828
sql,
29+
not,
2930
} from 'drizzle-orm';
3031
import { v4 } from 'uuid';
3132
import { DIMENSION_MAP, type EmbeddingDimensionColumn } from './schema/embedding';
@@ -291,11 +292,86 @@ export abstract class BaseDrizzleAdapter<
291292
* @returns {Promise<boolean>} - A boolean indicating if the deletion was successful.
292293
*/
293294
async deleteAgent(agentId: UUID): Promise<boolean> {
294-
// casacade delete all related for the agent
295295
return this.withDatabase(async () => {
296296
await this.db.transaction(async (tx) => {
297+
const entities = await this.db
298+
.select({ entityId: entityTable.id })
299+
.from(entityTable)
300+
.where(eq(entityTable.agentId, agentId));
301+
302+
const entityIds = entities.map((e) => e.entityId);
303+
304+
let memoryIds: UUID[] = [];
305+
306+
if (entityIds.length > 0) {
307+
const entityMemories = await this.db
308+
.select({ memoryId: memoryTable.id })
309+
.from(memoryTable)
310+
.where(inArray(memoryTable.entityId, entityIds));
311+
312+
memoryIds = entityMemories.map((m) => m.memoryId);
313+
}
314+
315+
const agentMemories = await this.db
316+
.select({ memoryId: memoryTable.id })
317+
.from(memoryTable)
318+
.where(eq(memoryTable.agentId, agentId));
319+
320+
memoryIds.push(...agentMemories.map((m) => m.memoryId));
321+
322+
if (memoryIds.length > 0) {
323+
await tx.delete(embeddingTable).where(inArray(embeddingTable.memoryId, memoryIds));
324+
325+
await tx.delete(memoryTable).where(inArray(memoryTable.id, memoryIds));
326+
}
327+
328+
const rooms = await this.db
329+
.select({ roomId: roomTable.id })
330+
.from(roomTable)
331+
.where(eq(roomTable.agentId, agentId));
332+
333+
const roomIds = rooms.map((r) => r.roomId);
334+
335+
if (entityIds.length > 0) {
336+
await tx.delete(logTable).where(inArray(logTable.entityId, entityIds));
337+
await tx.delete(participantTable).where(inArray(participantTable.entityId, entityIds));
338+
}
339+
340+
if (roomIds.length > 0) {
341+
await tx.delete(logTable).where(inArray(logTable.roomId, roomIds));
342+
await tx.delete(participantTable).where(inArray(participantTable.roomId, roomIds));
343+
}
344+
345+
await tx.delete(participantTable).where(eq(participantTable.agentId, agentId));
346+
347+
if (roomIds.length > 0) {
348+
await tx.delete(roomTable).where(inArray(roomTable.id, roomIds));
349+
}
350+
351+
await tx.delete(cacheTable).where(eq(cacheTable.agentId, agentId));
352+
353+
await tx.delete(relationshipTable).where(eq(relationshipTable.agentId, agentId));
354+
355+
await tx.delete(entityTable).where(eq(entityTable.agentId, agentId));
356+
357+
const newAgent = await this.db
358+
.select({ newAgentId: agentTable.id })
359+
.from(agentTable)
360+
.where(not(eq(agentTable.id, agentId)))
361+
.limit(1);
362+
363+
if (newAgent.length > 0) {
364+
await tx
365+
.update(worldTable)
366+
.set({ agentId: newAgent[0].newAgentId })
367+
.where(eq(worldTable.agentId, agentId));
368+
} else {
369+
await tx.delete(worldTable).where(eq(worldTable.agentId, agentId));
370+
}
371+
297372
await tx.delete(agentTable).where(eq(agentTable.id, agentId));
298373
});
374+
299375
return true;
300376
});
301377
}

0 commit comments

Comments
 (0)