forked from Lilly-Protocol/lily-backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathagents.service.ts
More file actions
85 lines (68 loc) · 2.22 KB
/
Copy pathagents.service.ts
File metadata and controls
85 lines (68 loc) · 2.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import { AppError } from "../../common/http/app-error";
import type { Agent, AgentStatus, CreateAgentInput } from "./agents.types";
const MAX_IN_MEMORY_AGENTS = 5_000;
const initialAgents: Agent[] = [
{
id: "agentlily_demo_001",
name: "Treasury Settlement Agent",
description:
"AgentLily instance responsible for orchestrating treasury rebalancing operations.",
walletAddress: "GBVDO6P6E3S6XG2Z5V5L7N3Z6Y2K4J5H7F8D9S0A1B2C3D4E5F6G7H8I",
status: "active",
capabilities: ["settlement", "rebalance", "liquidity-monitoring"],
createdAt: "2026-01-01T00:00:00.000Z",
updatedAt: "2026-01-01T00:00:00.000Z",
},
];
let agents: Agent[] = [...initialAgents];
let agentSequence = initialAgents.length + 1;
export const agentsService = {
listAgents: (): { total: number; agents: Agent[] } => ({
total: agents.length,
agents: [...agents],
}),
getAgentById: (id: string): Agent | undefined => {
return agents.find((agent) => agent.id === id);
},
createAgent: (input: CreateAgentInput): Agent => {
const now = new Date().toISOString();
const slug = input.name.replace(/[^a-zA-Z0-9]/g, "").toUpperCase();
const walletAddress = `G${slug.padEnd(55, "0").slice(0, 55)}`;
const agent: Agent = {
id: `agentlily_${agentSequence++}`,
name: input.name,
description: input.description,
walletAddress,
status: "active",
capabilities: input.capabilities,
createdAt: now,
updatedAt: now,
};
if (agents.length >= MAX_IN_MEMORY_AGENTS) {
agents.shift();
}
agents.push(agent);
return agent;
},
updateAgentStatus: (id: string, status: AgentStatus): { agent: Agent } => {
const agent = agents.find((candidate) => candidate.id === id);
if (!agent) {
throw new AppError(404, "Agent not found");
}
agent.status = status;
agent.updatedAt = new Date().toISOString();
return { agent };
},
deleteAgent: (id: string): boolean => {
const index = agents.findIndex((agent) => agent.id === id);
if (index === -1) {
return false;
}
agents.splice(index, 1);
return true;
},
reset: (): void => {
agents = [...initialAgents];
agentSequence = initialAgents.length + 1;
},
};