-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Expand file tree
/
Copy pathcrud.ts
More file actions
183 lines (159 loc) · 5.82 KB
/
Copy pathcrud.ts
File metadata and controls
183 lines (159 loc) · 5.82 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import { checkServer, displayAgent, handleError } from '@/src/utils';
import { AgentsService, MemoryService } from '@elizaos/api-client';
import { asUUID, UUID, type Agent } from '@elizaos/core';
import type { OptionValues } from 'commander';
import { readFileSync, writeFileSync } from 'node:fs';
import path from 'node:path';
import { createApiClientConfig } from '../../shared';
import { resolveAgentId } from '../utils';
/**
* Parse error response and throw appropriate error
* @param response - The fetch Response object
* @param defaultMessage - Default error message if JSON parsing fails
*/
/**
* Get command implementation - retrieves and displays agent details
*/
export async function getAgent(opts: OptionValues): Promise<void> {
try {
const resolvedAgentId = await resolveAgentId(opts.name, opts);
const config = createApiClientConfig(opts);
const agentsService = new AgentsService(config);
console.info(`Getting agent ${resolvedAgentId}`);
// API Endpoint: GET /agents/:agentId
let agentId: UUID;
try {
agentId = asUUID(resolvedAgentId);
} catch (error) {
throw new Error(
`Invalid agent ID format: ${resolvedAgentId}. Please provide a valid UUID, agent name, or index.`
);
}
const agent = await agentsService.getAgent(agentId);
if (!agent) {
throw new Error('No agent data received from server');
}
// Save to file if output option is specified - exit early
if (opts.output !== undefined) {
// Extract config without metadata fields
const { id, createdAt, updatedAt, enabled, ...agentConfig } = agent;
// Create filename with appropriate .json extension
const filename =
opts.output === true
? `${agent.name || 'agent'}.json`
: `${String(opts.output)}${String(opts.output).endsWith('.json') ? '' : '.json'}`;
// Save file and exit
const jsonPath = path.resolve(process.cwd(), filename);
writeFileSync(jsonPath, JSON.stringify(agentConfig, null, 2));
console.log(`Saved agent configuration to ${jsonPath}`);
return;
}
// Display agent details if not using output option
displayAgent(agent as Partial<Agent>, 'Agent Details');
// Display JSON if requested
if (opts.json) {
const { id, createdAt, updatedAt, enabled, ...agentConfig } = agent;
console.log(JSON.stringify(agentConfig, null, 2));
}
} catch (error) {
await checkServer(opts);
handleError(error);
}
}
/**
* Remove command implementation - deletes an agent
*/
export async function removeAgent(opts: OptionValues): Promise<void> {
try {
const resolvedAgentId = await resolveAgentId(opts.name, opts);
const config = createApiClientConfig(opts);
const agentsService = new AgentsService(config);
console.info(`Removing agent ${resolvedAgentId}`);
// API Endpoint: DELETE /agents/:agentId
let agentId: UUID;
try {
agentId = asUUID(resolvedAgentId);
} catch (error) {
throw new Error(
`Invalid agent ID format: ${resolvedAgentId}. Please provide a valid UUID, agent name, or index.`
);
}
await agentsService.deleteAgent(agentId);
console.log(`Successfully removed agent ${opts.name}`);
} catch (error) {
await checkServer(opts);
handleError(error);
}
}
/**
* Clear memories command implementation - clears all memories for an agent
*/
export async function clearAgentMemories(opts: OptionValues): Promise<void> {
try {
const resolvedAgentId = await resolveAgentId(opts.name, opts);
const config = createApiClientConfig(opts);
const memoryService = new MemoryService(config);
console.info(`Clearing all memories for agent ${resolvedAgentId}`);
// API Endpoint: DELETE /api/memory/:agentId/memories
let agentId: UUID;
try {
agentId = asUUID(resolvedAgentId);
} catch (error) {
throw new Error(
`Invalid agent ID format: ${resolvedAgentId}. Please provide a valid UUID, agent name, or index.`
);
}
const result = await memoryService.clearAgentMemories(agentId);
console.log(`Successfully cleared ${result?.deleted || 0} memories for agent ${opts.name}`);
} catch (error) {
await checkServer(opts);
handleError(error);
}
}
/**
* Set command implementation - updates agent configuration
*/
export async function setAgentConfig(opts: OptionValues): Promise<void> {
try {
const resolvedAgentId = await resolveAgentId(opts.name, opts);
console.info(`Updating configuration for agent ${resolvedAgentId}`);
let config: Record<string, unknown>;
if (opts.config) {
try {
config = JSON.parse(opts.config);
} catch (error) {
throw new Error(
`Failed to parse config JSON string: ${error instanceof Error ? error.message : String(error)}`
);
}
} else if (opts.file) {
try {
config = JSON.parse(readFileSync(opts.file, 'utf8'));
} catch (error) {
throw new Error(
`Failed to read or parse config file: ${error instanceof Error ? error.message : String(error)}`
);
}
} else {
throw new Error('Please provide either a config JSON string (-c) or a config file path (-f)');
}
// API Endpoint: PATCH /agents/:agentId
const clientConfig = createApiClientConfig(opts);
const agentsService = new AgentsService(clientConfig);
let agentId: UUID;
try {
agentId = asUUID(resolvedAgentId);
} catch (error) {
throw new Error(
`Invalid agent ID format: ${resolvedAgentId}. Please provide a valid UUID, agent name, or index.`
);
}
const updatedAgent = await agentsService.updateAgent(agentId, config);
console.log(
`Successfully updated configuration for agent ${updatedAgent?.id || resolvedAgentId}`
);
} catch (error) {
await checkServer(opts);
handleError(error);
}
}