agent-resource-management 1.4.1 → 2.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "agent-resource-management",
3
- "version": "1.4.1",
3
+ "version": "2.1.0",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "arm": "./dist/main.js"
package/src/cmd/agent.ts CHANGED
@@ -1,28 +1,12 @@
1
1
  import { ApiClient } from '../lib/client';
2
2
  import { loadConfig } from '../lib/storage';
3
3
  import { formatAgent, formatAgentDetail, success, error, info } from '../lib/formatter';
4
+ import { shouldOutputJson, outputJson } from '../lib/output';
4
5
  import { writeFileSync, existsSync } from 'fs';
5
6
  import { join } from 'path';
6
7
  import { execSync } from 'child_process';
7
8
  import { mkdtempSync, rmSync } from 'fs';
8
9
 
9
- interface JsonResult<T = unknown> {
10
- success: boolean;
11
- data?: T;
12
- error?: {
13
- code: string;
14
- message: string;
15
- };
16
- }
17
-
18
- function outputJson<T>(result: JsonResult<T>): void {
19
- console.log(JSON.stringify(result, null, 2));
20
- }
21
-
22
- function getJsonFlag(): boolean {
23
- return process.argv.includes('--json') || process.argv.includes('-j');
24
- }
25
-
26
10
  function getAgentIdentifier(identifier: string): { id: string; name: string } {
27
11
  if (identifier.includes('-')) {
28
12
  return { id: identifier, name: identifier };
@@ -44,7 +28,7 @@ export async function createAgent(
44
28
  ): Promise<void> {
45
29
  const config = loadConfig();
46
30
  if (!config?.token) {
47
- if (getJsonFlag()) {
31
+ if (shouldOutputJson()) {
48
32
  outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
49
33
  process.exit(1);
50
34
  }
@@ -73,13 +57,13 @@ export async function createAgent(
73
57
  knowledges,
74
58
  });
75
59
 
76
- if (getJsonFlag()) {
60
+ if (shouldOutputJson()) {
77
61
  outputJson({ success: true, data: result });
78
62
  return;
79
63
  }
80
64
  success(`Agent "${name}" 创建成功 (ID: ${result.id})`);
81
65
  } catch (err) {
82
- if (getJsonFlag()) {
66
+ if (shouldOutputJson()) {
83
67
  outputJson({ success: false, error: { code: 'CREATE_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
84
68
  process.exit(1);
85
69
  }
@@ -100,7 +84,7 @@ export async function updateAgent(
100
84
  ): Promise<void> {
101
85
  const config = loadConfig();
102
86
  if (!config?.token) {
103
- if (getJsonFlag()) {
87
+ if (shouldOutputJson()) {
104
88
  outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
105
89
  process.exit(1);
106
90
  }
@@ -126,13 +110,13 @@ export async function updateAgent(
126
110
 
127
111
  const result = await client.updateAgent(id, updateData);
128
112
 
129
- if (getJsonFlag()) {
113
+ if (shouldOutputJson()) {
130
114
  outputJson({ success: true, data: result });
131
115
  return;
132
116
  }
133
117
  success(`Agent "${id}" 更新成功`);
134
118
  } catch (err) {
135
- if (getJsonFlag()) {
119
+ if (shouldOutputJson()) {
136
120
  outputJson({ success: false, error: { code: 'UPDATE_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
137
121
  process.exit(1);
138
122
  }
@@ -144,7 +128,7 @@ export async function updateAgent(
144
128
  export async function deleteAgent(id: string): Promise<void> {
145
129
  const config = loadConfig();
146
130
  if (!config?.token) {
147
- if (getJsonFlag()) {
131
+ if (shouldOutputJson()) {
148
132
  outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
149
133
  process.exit(1);
150
134
  }
@@ -156,13 +140,13 @@ export async function deleteAgent(id: string): Promise<void> {
156
140
  try {
157
141
  await client.deleteAgent(id);
158
142
 
159
- if (getJsonFlag()) {
143
+ if (shouldOutputJson()) {
160
144
  outputJson({ success: true, data: { id } });
161
145
  return;
162
146
  }
163
147
  success(`Agent "${id}" 删除成功`);
164
148
  } catch (err) {
165
- if (getJsonFlag()) {
149
+ if (shouldOutputJson()) {
166
150
  outputJson({ success: false, error: { code: 'DELETE_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
167
151
  process.exit(1);
168
152
  }
@@ -174,7 +158,7 @@ export async function deleteAgent(id: string): Promise<void> {
174
158
  export async function bindSkill(id: string, skillId: string, config?: string): Promise<void> {
175
159
  const configStore = loadConfig();
176
160
  if (!configStore?.token) {
177
- if (getJsonFlag()) {
161
+ if (shouldOutputJson()) {
178
162
  outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
179
163
  process.exit(1);
180
164
  }
@@ -187,13 +171,13 @@ export async function bindSkill(id: string, skillId: string, config?: string): P
187
171
  const parsedConfig = config ? JSON.parse(config) : undefined;
188
172
  await client.bindSkillToAgent(id, skillId, parsedConfig);
189
173
 
190
- if (getJsonFlag()) {
174
+ if (shouldOutputJson()) {
191
175
  outputJson({ success: true, data: { agentId: id, skillId, config: parsedConfig } });
192
176
  return;
193
177
  }
194
178
  success(`Skill "${skillId}" 已绑定到 Agent "${id}"`);
195
179
  } catch (err) {
196
- if (getJsonFlag()) {
180
+ if (shouldOutputJson()) {
197
181
  outputJson({ success: false, error: { code: 'BIND_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
198
182
  process.exit(1);
199
183
  }
@@ -205,7 +189,7 @@ export async function bindSkill(id: string, skillId: string, config?: string): P
205
189
  export async function unbindSkill(id: string, skillId: string): Promise<void> {
206
190
  const config = loadConfig();
207
191
  if (!config?.token) {
208
- if (getJsonFlag()) {
192
+ if (shouldOutputJson()) {
209
193
  outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
210
194
  process.exit(1);
211
195
  }
@@ -217,13 +201,13 @@ export async function unbindSkill(id: string, skillId: string): Promise<void> {
217
201
  try {
218
202
  await client.unbindSkillFromAgent(id, skillId);
219
203
 
220
- if (getJsonFlag()) {
204
+ if (shouldOutputJson()) {
221
205
  outputJson({ success: true, data: { agentId: id, skillId } });
222
206
  return;
223
207
  }
224
208
  success(`Skill "${skillId}" 已从 Agent "${id}" 解绑`);
225
209
  } catch (err) {
226
- if (getJsonFlag()) {
210
+ if (shouldOutputJson()) {
227
211
  outputJson({ success: false, error: { code: 'UNBIND_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
228
212
  process.exit(1);
229
213
  }
@@ -235,7 +219,7 @@ export async function unbindSkill(id: string, skillId: string): Promise<void> {
235
219
  export async function bindKnowledge(id: string, knowledgeId: string, retrievalConfig?: string): Promise<void> {
236
220
  const configStore = loadConfig();
237
221
  if (!configStore?.token) {
238
- if (getJsonFlag()) {
222
+ if (shouldOutputJson()) {
239
223
  outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
240
224
  process.exit(1);
241
225
  }
@@ -248,13 +232,13 @@ export async function bindKnowledge(id: string, knowledgeId: string, retrievalCo
248
232
  const parsedConfig = retrievalConfig ? JSON.parse(retrievalConfig) : undefined;
249
233
  await client.bindKnowledgeToAgent(id, knowledgeId, parsedConfig);
250
234
 
251
- if (getJsonFlag()) {
235
+ if (shouldOutputJson()) {
252
236
  outputJson({ success: true, data: { agentId: id, knowledgeId, retrievalConfig: parsedConfig } });
253
237
  return;
254
238
  }
255
239
  success(`Knowledge "${knowledgeId}" 已绑定到 Agent "${id}"`);
256
240
  } catch (err) {
257
- if (getJsonFlag()) {
241
+ if (shouldOutputJson()) {
258
242
  outputJson({ success: false, error: { code: 'BIND_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
259
243
  process.exit(1);
260
244
  }
@@ -266,7 +250,7 @@ export async function bindKnowledge(id: string, knowledgeId: string, retrievalCo
266
250
  export async function unbindKnowledge(id: string, knowledgeId: string): Promise<void> {
267
251
  const config = loadConfig();
268
252
  if (!config?.token) {
269
- if (getJsonFlag()) {
253
+ if (shouldOutputJson()) {
270
254
  outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
271
255
  process.exit(1);
272
256
  }
@@ -278,13 +262,13 @@ export async function unbindKnowledge(id: string, knowledgeId: string): Promise<
278
262
  try {
279
263
  await client.unbindKnowledgeFromAgent(id, knowledgeId);
280
264
 
281
- if (getJsonFlag()) {
265
+ if (shouldOutputJson()) {
282
266
  outputJson({ success: true, data: { agentId: id, knowledgeId } });
283
267
  return;
284
268
  }
285
269
  success(`Knowledge "${knowledgeId}" 已从 Agent "${id}" 解绑`);
286
270
  } catch (err) {
287
- if (getJsonFlag()) {
271
+ if (shouldOutputJson()) {
288
272
  outputJson({ success: false, error: { code: 'UNBIND_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
289
273
  process.exit(1);
290
274
  }
@@ -296,6 +280,10 @@ export async function unbindKnowledge(id: string, knowledgeId: string): Promise<
296
280
  export async function listAgents(): Promise<void> {
297
281
  const config = loadConfig();
298
282
  if (!config?.token) {
283
+ if (shouldOutputJson()) {
284
+ outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
285
+ process.exit(1);
286
+ }
299
287
  error('未登录,请先运行 arm login');
300
288
  process.exit(1);
301
289
  }
@@ -303,6 +291,10 @@ export async function listAgents(): Promise<void> {
303
291
  const client = new ApiClient(config.serverUrl, config.token);
304
292
  try {
305
293
  const result = await client.listAgents();
294
+ if (shouldOutputJson()) {
295
+ outputJson({ success: true, data: result });
296
+ return;
297
+ }
306
298
  if (result.agents.length === 0) {
307
299
  info('暂无 Agent');
308
300
  return;
@@ -313,6 +305,10 @@ export async function listAgents(): Promise<void> {
313
305
  console.log('');
314
306
  }
315
307
  } catch (err) {
308
+ if (shouldOutputJson()) {
309
+ outputJson({ success: false, error: { code: 'LIST_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
310
+ process.exit(1);
311
+ }
316
312
  error(`获取列表失败: ${err instanceof Error ? err.message : '未知错误'}`);
317
313
  process.exit(1);
318
314
  }
@@ -321,6 +317,10 @@ export async function listAgents(): Promise<void> {
321
317
  export async function searchAgents(keyword: string): Promise<void> {
322
318
  const config = loadConfig();
323
319
  if (!config?.token) {
320
+ if (shouldOutputJson()) {
321
+ outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
322
+ process.exit(1);
323
+ }
324
324
  error('未登录,请先运行 arm login');
325
325
  process.exit(1);
326
326
  }
@@ -328,6 +328,10 @@ export async function searchAgents(keyword: string): Promise<void> {
328
328
  const client = new ApiClient(config.serverUrl, config.token);
329
329
  try {
330
330
  const result = await client.listAgents(keyword);
331
+ if (shouldOutputJson()) {
332
+ outputJson({ success: true, data: result });
333
+ return;
334
+ }
331
335
  if (result.agents.length === 0) {
332
336
  info(`没有找到包含 "${keyword}" 的 Agent`);
333
337
  return;
@@ -338,6 +342,10 @@ export async function searchAgents(keyword: string): Promise<void> {
338
342
  console.log('');
339
343
  }
340
344
  } catch (err) {
345
+ if (shouldOutputJson()) {
346
+ outputJson({ success: false, error: { code: 'SEARCH_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
347
+ process.exit(1);
348
+ }
341
349
  error(`搜索失败: ${err instanceof Error ? err.message : '未知错误'}`);
342
350
  process.exit(1);
343
351
  }
@@ -346,6 +354,10 @@ export async function searchAgents(keyword: string): Promise<void> {
346
354
  export async function infoAgent(name: string): Promise<void> {
347
355
  const config = loadConfig();
348
356
  if (!config?.token) {
357
+ if (shouldOutputJson()) {
358
+ outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
359
+ process.exit(1);
360
+ }
349
361
  error('未登录,请先运行 arm login');
350
362
  process.exit(1);
351
363
  }
@@ -355,6 +367,10 @@ export async function infoAgent(name: string): Promise<void> {
355
367
  const result = await client.listAgents(name);
356
368
  const agent = result.agents.find((a) => a.name === name);
357
369
  if (!agent) {
370
+ if (shouldOutputJson()) {
371
+ outputJson({ success: false, error: { code: 'NOT_FOUND', message: `Agent "${name}" 不存在` } });
372
+ process.exit(1);
373
+ }
358
374
  error(`Agent "${name}" 不存在`);
359
375
  process.exit(1);
360
376
  }
@@ -373,8 +389,16 @@ export async function infoAgent(name: string): Promise<void> {
373
389
  fullAgent.knowledges = await Promise.all(knowledgeNamePromises);
374
390
  }
375
391
 
392
+ if (shouldOutputJson()) {
393
+ outputJson({ success: true, data: fullAgent });
394
+ return;
395
+ }
376
396
  console.log(formatAgentDetail(fullAgent));
377
397
  } catch (err) {
398
+ if (shouldOutputJson()) {
399
+ outputJson({ success: false, error: { code: 'INFO_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
400
+ process.exit(1);
401
+ }
378
402
  error(`获取详情失败: ${err instanceof Error ? err.message : '未知错误'}`);
379
403
  process.exit(1);
380
404
  }
@@ -383,6 +407,10 @@ export async function infoAgent(name: string): Promise<void> {
383
407
  export async function downloadAgent(name: string, outputDir?: string): Promise<void> {
384
408
  const config = loadConfig();
385
409
  if (!config?.token) {
410
+ if (shouldOutputJson()) {
411
+ outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
412
+ process.exit(1);
413
+ }
386
414
  error('未登录,请先运行 arm login');
387
415
  process.exit(1);
388
416
  }
@@ -392,11 +420,17 @@ export async function downloadAgent(name: string, outputDir?: string): Promise<v
392
420
  const result = await client.listAgents(name);
393
421
  const agent = result.agents.find((a) => a.name === name);
394
422
  if (!agent) {
423
+ if (shouldOutputJson()) {
424
+ outputJson({ success: false, error: { code: 'NOT_FOUND', message: `Agent "${name}" 不存在` } });
425
+ process.exit(1);
426
+ }
395
427
  error(`Agent "${name}" 不存在`);
396
428
  process.exit(1);
397
429
  }
398
430
 
399
- info(`正在下载 ${name}...`);
431
+ if (shouldOutputJson()) {
432
+ info(`正在下载 ${name}...`);
433
+ }
400
434
  const { buffer, version } = await client.downloadAgent(agent.id);
401
435
 
402
436
  const tempDir = mkdtempSync('/tmp/agent-download-');
@@ -406,7 +440,7 @@ export async function downloadAgent(name: string, outputDir?: string): Promise<v
406
440
  const targetDir = join(outputDir || '.', name);
407
441
  execSync(`mkdir -p "${targetDir}"`, { stdio: 'pipe' });
408
442
  execSync(`unzip -q "${zipPath}" -d "${tempDir}"`, { stdio: 'pipe' });
409
-
443
+
410
444
  const contentDir = join(tempDir, 'agent-content');
411
445
  if (existsSync(contentDir)) {
412
446
  execSync(`cp -r "${contentDir}"/* "${targetDir}/"`, { stdio: 'pipe' });
@@ -416,8 +450,16 @@ export async function downloadAgent(name: string, outputDir?: string): Promise<v
416
450
 
417
451
  rmSync(tempDir, { recursive: true, force: true });
418
452
 
453
+ if (shouldOutputJson()) {
454
+ outputJson({ success: true, data: { path: targetDir, version } });
455
+ return;
456
+ }
419
457
  success(`已下载到 ${targetDir} (版本: ${version})`);
420
458
  } catch (err) {
459
+ if (shouldOutputJson()) {
460
+ outputJson({ success: false, error: { code: 'DOWNLOAD_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
461
+ process.exit(1);
462
+ }
421
463
  error(`下载失败: ${err instanceof Error ? err.message : '未知错误'}`);
422
464
  process.exit(1);
423
465
  }
@@ -1,6 +1,7 @@
1
1
  import { ApiClient } from '../lib/client';
2
2
  import { loadConfig } from '../lib/storage';
3
3
  import { formatKnowledge, formatKnowledgeDetail, success, error, info } from '../lib/formatter';
4
+ import { shouldOutputJson, outputJson } from '../lib/output';
4
5
  import { writeFileSync, existsSync, statSync } from 'fs';
5
6
  import { join, basename, dirname } from 'path';
6
7
  import { execSync } from 'child_process';
@@ -9,6 +10,10 @@ import { mkdtempSync, rmSync } from 'fs';
9
10
  export async function listKnowledge(): Promise<void> {
10
11
  const config = loadConfig();
11
12
  if (!config?.token) {
13
+ if (shouldOutputJson()) {
14
+ outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
15
+ process.exit(1);
16
+ }
12
17
  error('未登录,请先运行 arm login');
13
18
  process.exit(1);
14
19
  }
@@ -16,6 +21,10 @@ export async function listKnowledge(): Promise<void> {
16
21
  const client = new ApiClient(config.serverUrl, config.token);
17
22
  try {
18
23
  const result = await client.listKnowledge();
24
+ if (shouldOutputJson()) {
25
+ outputJson({ success: true, data: result });
26
+ return;
27
+ }
19
28
  if (result.knowledges.length === 0) {
20
29
  info('暂无 Knowledge');
21
30
  return;
@@ -26,6 +35,10 @@ export async function listKnowledge(): Promise<void> {
26
35
  console.log('');
27
36
  }
28
37
  } catch (err) {
38
+ if (shouldOutputJson()) {
39
+ outputJson({ success: false, error: { code: 'LIST_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
40
+ process.exit(1);
41
+ }
29
42
  error(`获取列表失败: ${err instanceof Error ? err.message : '未知错误'}`);
30
43
  process.exit(1);
31
44
  }
@@ -34,6 +47,10 @@ export async function listKnowledge(): Promise<void> {
34
47
  export async function searchKnowledge(keyword: string): Promise<void> {
35
48
  const config = loadConfig();
36
49
  if (!config?.token) {
50
+ if (shouldOutputJson()) {
51
+ outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
52
+ process.exit(1);
53
+ }
37
54
  error('未登录,请先运行 arm login');
38
55
  process.exit(1);
39
56
  }
@@ -41,6 +58,10 @@ export async function searchKnowledge(keyword: string): Promise<void> {
41
58
  const client = new ApiClient(config.serverUrl, config.token);
42
59
  try {
43
60
  const result = await client.listKnowledge(keyword);
61
+ if (shouldOutputJson()) {
62
+ outputJson({ success: true, data: result });
63
+ return;
64
+ }
44
65
  if (result.knowledges.length === 0) {
45
66
  info(`没有找到包含 "${keyword}" 的 Knowledge`);
46
67
  return;
@@ -51,6 +72,10 @@ export async function searchKnowledge(keyword: string): Promise<void> {
51
72
  console.log('');
52
73
  }
53
74
  } catch (err) {
75
+ if (shouldOutputJson()) {
76
+ outputJson({ success: false, error: { code: 'SEARCH_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
77
+ process.exit(1);
78
+ }
54
79
  error(`搜索失败: ${err instanceof Error ? err.message : '未知错误'}`);
55
80
  process.exit(1);
56
81
  }
@@ -59,6 +84,10 @@ export async function searchKnowledge(keyword: string): Promise<void> {
59
84
  export async function infoKnowledge(name: string): Promise<void> {
60
85
  const config = loadConfig();
61
86
  if (!config?.token) {
87
+ if (shouldOutputJson()) {
88
+ outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
89
+ process.exit(1);
90
+ }
62
91
  error('未登录,请先运行 arm login');
63
92
  process.exit(1);
64
93
  }
@@ -66,8 +95,16 @@ export async function infoKnowledge(name: string): Promise<void> {
66
95
  const client = new ApiClient(config.serverUrl, config.token);
67
96
  try {
68
97
  const knowledge = await client.getKnowledge(name);
98
+ if (shouldOutputJson()) {
99
+ outputJson({ success: true, data: knowledge });
100
+ return;
101
+ }
69
102
  console.log(formatKnowledgeDetail(knowledge));
70
103
  } catch (err) {
104
+ if (shouldOutputJson()) {
105
+ outputJson({ success: false, error: { code: 'INFO_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
106
+ process.exit(1);
107
+ }
71
108
  error(`获取详情失败: ${err instanceof Error ? err.message : '未知错误'}`);
72
109
  process.exit(1);
73
110
  }
@@ -76,18 +113,32 @@ export async function infoKnowledge(name: string): Promise<void> {
76
113
  export async function downloadKnowledge(name: string, outputDir?: string): Promise<void> {
77
114
  const config = loadConfig();
78
115
  if (!config?.token) {
116
+ if (shouldOutputJson()) {
117
+ outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
118
+ process.exit(1);
119
+ }
79
120
  error('未登录,请先运行 arm login');
80
121
  process.exit(1);
81
122
  }
82
123
 
83
124
  const client = new ApiClient(config.serverUrl, config.token);
84
125
  try {
85
- info(`正在下载 ${name}...`);
126
+ if (shouldOutputJson()) {
127
+ info(`正在下载 ${name}...`);
128
+ }
86
129
  const buffer = await client.downloadKnowledge(name);
87
130
  const outputPath = join(outputDir || '.', `${name}.zip`);
88
131
  writeFileSync(outputPath, Buffer.from(buffer));
132
+ if (shouldOutputJson()) {
133
+ outputJson({ success: true, data: { path: outputPath } });
134
+ return;
135
+ }
89
136
  success(`已下载到 ${outputPath}`);
90
137
  } catch (err) {
138
+ if (shouldOutputJson()) {
139
+ outputJson({ success: false, error: { code: 'DOWNLOAD_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
140
+ process.exit(1);
141
+ }
91
142
  error(`下载失败: ${err instanceof Error ? err.message : '未知错误'}`);
92
143
  process.exit(1);
93
144
  }
@@ -96,11 +147,19 @@ export async function downloadKnowledge(name: string, outputDir?: string): Promi
96
147
  export async function uploadKnowledge(filePath: string): Promise<void> {
97
148
  const config = loadConfig();
98
149
  if (!config?.token) {
150
+ if (shouldOutputJson()) {
151
+ outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
152
+ process.exit(1);
153
+ }
99
154
  error('未登录,请先运行 arm login');
100
155
  process.exit(1);
101
156
  }
102
157
 
103
158
  if (!existsSync(filePath)) {
159
+ if (shouldOutputJson()) {
160
+ outputJson({ success: false, error: { code: 'FILE_NOT_FOUND', message: `上传失败: 目录不存在: ${filePath}` } });
161
+ process.exit(1);
162
+ }
104
163
  error(`上传失败: 目录不存在: ${filePath}`);
105
164
  process.exit(1);
106
165
  }
@@ -117,10 +176,20 @@ export async function uploadKnowledge(filePath: string): Promise<void> {
117
176
  }
118
177
 
119
178
  const client = new ApiClient(config.serverUrl, config.token);
120
- info(`正在上传 ${filePath}...`);
179
+ if (shouldOutputJson()) {
180
+ info(`正在上传 ${filePath}...`);
181
+ }
121
182
  const knowledge = await client.uploadKnowledge(zipPath);
183
+ if (shouldOutputJson()) {
184
+ outputJson({ success: true, data: knowledge });
185
+ return;
186
+ }
122
187
  success(`上传成功! Knowledge: ${knowledge.name}`);
123
188
  } catch (err) {
189
+ if (shouldOutputJson()) {
190
+ outputJson({ success: false, error: { code: 'UPLOAD_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
191
+ process.exit(1);
192
+ }
124
193
  error(`上传失败: ${err instanceof Error ? err.message : '未知错误'}`);
125
194
  process.exit(1);
126
195
  } finally {
@@ -131,6 +200,10 @@ export async function uploadKnowledge(filePath: string): Promise<void> {
131
200
  export async function myKnowledge(): Promise<void> {
132
201
  const config = loadConfig();
133
202
  if (!config?.token) {
203
+ if (shouldOutputJson()) {
204
+ outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
205
+ process.exit(1);
206
+ }
134
207
  error('未登录,请先运行 arm login');
135
208
  process.exit(1);
136
209
  }
@@ -138,6 +211,10 @@ export async function myKnowledge(): Promise<void> {
138
211
  const client = new ApiClient(config.serverUrl, config.token);
139
212
  try {
140
213
  const knowledges = await client.getMyKnowledge();
214
+ if (shouldOutputJson()) {
215
+ outputJson({ success: true, data: knowledges });
216
+ return;
217
+ }
141
218
  if (knowledges.length === 0) {
142
219
  info('您还没有发布任何 Knowledge');
143
220
  return;
@@ -148,6 +225,10 @@ export async function myKnowledge(): Promise<void> {
148
225
  console.log('');
149
226
  }
150
227
  } catch (err) {
228
+ if (shouldOutputJson()) {
229
+ outputJson({ success: false, error: { code: 'LIST_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
230
+ process.exit(1);
231
+ }
151
232
  error(`获取列表失败: ${err instanceof Error ? err.message : '未知错误'}`);
152
233
  process.exit(1);
153
234
  }
@@ -156,6 +237,10 @@ export async function myKnowledge(): Promise<void> {
156
237
  export async function deleteKnowledge(name: string): Promise<void> {
157
238
  const config = loadConfig();
158
239
  if (!config?.token) {
240
+ if (shouldOutputJson()) {
241
+ outputJson({ success: false, error: { code: 'NOT_LOGGED_IN', message: '未登录,请先运行 arm login' } });
242
+ process.exit(1);
243
+ }
159
244
  error('未登录,请先运行 arm login');
160
245
  process.exit(1);
161
246
  }
@@ -163,8 +248,16 @@ export async function deleteKnowledge(name: string): Promise<void> {
163
248
  const client = new ApiClient(config.serverUrl, config.token);
164
249
  try {
165
250
  await client.deleteKnowledge(name);
251
+ if (shouldOutputJson()) {
252
+ outputJson({ success: true, data: { name } });
253
+ return;
254
+ }
166
255
  success(`已删除 ${name}`);
167
256
  } catch (err) {
257
+ if (shouldOutputJson()) {
258
+ outputJson({ success: false, error: { code: 'DELETE_FAILED', message: err instanceof Error ? err.message : '未知错误' } });
259
+ process.exit(1);
260
+ }
168
261
  error(`删除失败: ${err instanceof Error ? err.message : '未知错误'}`);
169
262
  process.exit(1);
170
263
  }