@kweaver-ai/kweaver-sdk 0.4.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.
Files changed (64) hide show
  1. package/bin/kweaver.js +9 -0
  2. package/dist/api/agent-chat.d.ts +69 -0
  3. package/dist/api/agent-chat.js +379 -0
  4. package/dist/api/agent-list.d.ts +12 -0
  5. package/dist/api/agent-list.js +33 -0
  6. package/dist/api/context-loader.d.ts +115 -0
  7. package/dist/api/context-loader.js +259 -0
  8. package/dist/api/conversations.d.ts +24 -0
  9. package/dist/api/conversations.js +64 -0
  10. package/dist/api/knowledge-networks.d.ts +57 -0
  11. package/dist/api/knowledge-networks.js +158 -0
  12. package/dist/api/ontology-query.d.ts +75 -0
  13. package/dist/api/ontology-query.js +238 -0
  14. package/dist/api/semantic-search.d.ts +12 -0
  15. package/dist/api/semantic-search.js +34 -0
  16. package/dist/auth/oauth.d.ts +75 -0
  17. package/dist/auth/oauth.js +417 -0
  18. package/dist/cli.d.ts +1 -0
  19. package/dist/cli.js +79 -0
  20. package/dist/client.d.ts +95 -0
  21. package/dist/client.js +104 -0
  22. package/dist/commands/agent-chat.d.ts +12 -0
  23. package/dist/commands/agent-chat.js +193 -0
  24. package/dist/commands/agent.d.ts +28 -0
  25. package/dist/commands/agent.js +431 -0
  26. package/dist/commands/auth.d.ts +9 -0
  27. package/dist/commands/auth.js +201 -0
  28. package/dist/commands/bkn.d.ts +70 -0
  29. package/dist/commands/bkn.js +1371 -0
  30. package/dist/commands/call.d.ts +14 -0
  31. package/dist/commands/call.js +151 -0
  32. package/dist/commands/context-loader.d.ts +1 -0
  33. package/dist/commands/context-loader.js +383 -0
  34. package/dist/commands/token.d.ts +2 -0
  35. package/dist/commands/token.js +24 -0
  36. package/dist/config/store.d.ts +77 -0
  37. package/dist/config/store.js +380 -0
  38. package/dist/index.d.ts +53 -0
  39. package/dist/index.js +44 -0
  40. package/dist/kweaver.d.ts +146 -0
  41. package/dist/kweaver.js +184 -0
  42. package/dist/resources/agents.d.ts +37 -0
  43. package/dist/resources/agents.js +60 -0
  44. package/dist/resources/bkn.d.ts +45 -0
  45. package/dist/resources/bkn.js +86 -0
  46. package/dist/resources/context-loader.d.ts +15 -0
  47. package/dist/resources/context-loader.js +32 -0
  48. package/dist/resources/conversations.d.ts +11 -0
  49. package/dist/resources/conversations.js +17 -0
  50. package/dist/resources/knowledge-networks.d.ts +65 -0
  51. package/dist/resources/knowledge-networks.js +167 -0
  52. package/dist/ui/ChatApp.d.ts +16 -0
  53. package/dist/ui/ChatApp.js +248 -0
  54. package/dist/ui/MarkdownBlock.d.ts +5 -0
  55. package/dist/ui/MarkdownBlock.js +137 -0
  56. package/dist/ui/display-text.d.ts +1 -0
  57. package/dist/ui/display-text.js +1 -0
  58. package/dist/utils/browser.d.ts +1 -0
  59. package/dist/utils/browser.js +20 -0
  60. package/dist/utils/display-text.d.ts +3 -0
  61. package/dist/utils/display-text.js +46 -0
  62. package/dist/utils/http.d.ts +17 -0
  63. package/dist/utils/http.js +72 -0
  64. package/package.json +62 -0
@@ -0,0 +1,1371 @@
1
+ import { createInterface } from "node:readline";
2
+ import { readFileSync } from "node:fs";
3
+ import { ensureValidToken, formatHttpError } from "../auth/oauth.js";
4
+ import { listKnowledgeNetworks, getKnowledgeNetwork, createKnowledgeNetwork, updateKnowledgeNetwork, deleteKnowledgeNetwork, listObjectTypes, listRelationTypes, listActionTypes, } from "../api/knowledge-networks.js";
5
+ import { objectTypeQuery, objectTypeProperties, subgraph, actionTypeQuery, actionTypeExecute, actionExecutionGet, actionLogsList, actionLogGet, actionLogCancel, } from "../api/ontology-query.js";
6
+ import { semanticSearch } from "../api/semantic-search.js";
7
+ import { formatCallOutput } from "./call.js";
8
+ export function formatSimpleKnList(text, pretty, includeDetail = false) {
9
+ const parsed = JSON.parse(text);
10
+ const entries = Array.isArray(parsed.entries) ? parsed.entries : [];
11
+ const simplified = entries.map((entry) => ({
12
+ name: typeof entry.name === "string" ? entry.name : "",
13
+ id: typeof entry.id === "string" ? entry.id : "",
14
+ description: typeof entry.comment === "string" ? entry.comment : "",
15
+ ...(includeDetail && { detail: typeof entry.detail === "string" ? entry.detail : "" }),
16
+ }));
17
+ return JSON.stringify(simplified, null, pretty ? 2 : 0);
18
+ }
19
+ export function parseKnListArgs(args) {
20
+ let offset = 0;
21
+ let limit = 50;
22
+ let sort = "update_time";
23
+ let direction = "desc";
24
+ let businessDomain = "bd_public";
25
+ let detail = false;
26
+ let pretty = true;
27
+ let verbose = false;
28
+ let name_pattern;
29
+ let tag;
30
+ for (let i = 0; i < args.length; i += 1) {
31
+ const arg = args[i];
32
+ if (arg === "--help" || arg === "-h") {
33
+ throw new Error("help");
34
+ }
35
+ if (arg === "--offset") {
36
+ offset = parseInt(args[i + 1] ?? "0", 10);
37
+ if (Number.isNaN(offset) || offset < 0)
38
+ offset = 0;
39
+ i += 1;
40
+ continue;
41
+ }
42
+ if (arg === "--limit") {
43
+ limit = parseInt(args[i + 1] ?? "50", 10);
44
+ if (Number.isNaN(limit) || limit < 1)
45
+ limit = 50;
46
+ i += 1;
47
+ continue;
48
+ }
49
+ if (arg === "--sort") {
50
+ sort = args[i + 1] ?? "update_time";
51
+ i += 1;
52
+ continue;
53
+ }
54
+ if (arg === "--direction") {
55
+ const d = (args[i + 1] ?? "desc").toLowerCase();
56
+ direction = d === "asc" ? "asc" : "desc";
57
+ i += 1;
58
+ continue;
59
+ }
60
+ if (arg === "--name-pattern") {
61
+ name_pattern = args[i + 1] ?? "";
62
+ i += 1;
63
+ continue;
64
+ }
65
+ if (arg === "--tag") {
66
+ tag = args[i + 1] ?? "";
67
+ i += 1;
68
+ continue;
69
+ }
70
+ if (arg === "-bd" || arg === "--biz-domain") {
71
+ businessDomain = args[i + 1] ?? "bd_public";
72
+ if (!businessDomain || businessDomain.startsWith("-")) {
73
+ throw new Error("Missing value for biz-domain flag");
74
+ }
75
+ i += 1;
76
+ continue;
77
+ }
78
+ if (arg === "--pretty") {
79
+ pretty = true;
80
+ continue;
81
+ }
82
+ if (arg === "--detail") {
83
+ detail = true;
84
+ continue;
85
+ }
86
+ if (arg === "--verbose" || arg === "-v") {
87
+ verbose = true;
88
+ continue;
89
+ }
90
+ if (arg === "--simple") {
91
+ continue;
92
+ }
93
+ throw new Error(`Unsupported kn list argument: ${arg}`);
94
+ }
95
+ return { offset, limit, sort, direction, businessDomain, detail, pretty, verbose, name_pattern, tag };
96
+ }
97
+ export function parseKnGetArgs(args) {
98
+ let knId = "";
99
+ let stats = false;
100
+ let exportMode = false;
101
+ let businessDomain = "bd_public";
102
+ let pretty = true;
103
+ for (let i = 0; i < args.length; i += 1) {
104
+ const arg = args[i];
105
+ if (arg === "--help" || arg === "-h") {
106
+ throw new Error("help");
107
+ }
108
+ if (arg === "--stats") {
109
+ stats = true;
110
+ continue;
111
+ }
112
+ if (arg === "--export") {
113
+ exportMode = true;
114
+ continue;
115
+ }
116
+ if (arg === "-bd" || arg === "--biz-domain") {
117
+ businessDomain = args[i + 1] ?? "bd_public";
118
+ if (!businessDomain || businessDomain.startsWith("-")) {
119
+ throw new Error("Missing value for biz-domain flag");
120
+ }
121
+ i += 1;
122
+ continue;
123
+ }
124
+ if (arg === "--pretty") {
125
+ pretty = true;
126
+ continue;
127
+ }
128
+ if (!arg.startsWith("-") && !knId) {
129
+ knId = arg;
130
+ continue;
131
+ }
132
+ throw new Error(`Unsupported kn get argument: ${arg}`);
133
+ }
134
+ if (!knId) {
135
+ throw new Error("Missing kn-id. Usage: kweaver bkn get <kn-id> [options]");
136
+ }
137
+ return { knId, stats, export: exportMode, businessDomain, pretty };
138
+ }
139
+ const BODY_FILE_FLAGS = [
140
+ "--name",
141
+ "--comment",
142
+ "--tags",
143
+ "--icon",
144
+ "--color",
145
+ "--branch",
146
+ "--base-branch",
147
+ ];
148
+ export function parseKnCreateArgs(args) {
149
+ let bodyFile;
150
+ let import_mode;
151
+ let validate_dependency;
152
+ let businessDomain = "bd_public";
153
+ let pretty = true;
154
+ const flags = {};
155
+ for (let i = 0; i < args.length; i += 1) {
156
+ const arg = args[i];
157
+ if (arg === "--help" || arg === "-h") {
158
+ throw new Error("help");
159
+ }
160
+ if (arg === "--body-file") {
161
+ bodyFile = args[i + 1];
162
+ if (!bodyFile || bodyFile.startsWith("-")) {
163
+ throw new Error("Missing value for --body-file");
164
+ }
165
+ i += 1;
166
+ continue;
167
+ }
168
+ if (arg === "--import-mode") {
169
+ const m = (args[i + 1] ?? "normal").toLowerCase();
170
+ if (m !== "normal" && m !== "ignore" && m !== "overwrite") {
171
+ throw new Error("--import-mode must be normal, ignore, or overwrite");
172
+ }
173
+ import_mode = m;
174
+ i += 1;
175
+ continue;
176
+ }
177
+ if (arg === "--validate-dependency") {
178
+ const v = (args[i + 1] ?? "true").toLowerCase();
179
+ validate_dependency = v === "true" || v === "1";
180
+ i += 1;
181
+ continue;
182
+ }
183
+ if (arg === "-bd" || arg === "--biz-domain") {
184
+ businessDomain = args[i + 1] ?? "bd_public";
185
+ if (!businessDomain || businessDomain.startsWith("-")) {
186
+ throw new Error("Missing value for biz-domain flag");
187
+ }
188
+ i += 1;
189
+ continue;
190
+ }
191
+ if (arg === "--pretty") {
192
+ pretty = true;
193
+ continue;
194
+ }
195
+ if (BODY_FILE_FLAGS.includes(arg)) {
196
+ const key = arg.replace(/^--/, "").replace(/-/g, "_");
197
+ flags[key] = args[i + 1] ?? "";
198
+ i += 1;
199
+ continue;
200
+ }
201
+ throw new Error(`Unsupported kn create argument: ${arg}`);
202
+ }
203
+ let body;
204
+ if (bodyFile) {
205
+ if (Object.keys(flags).length > 0) {
206
+ throw new Error("Cannot use --body-file together with --name, --comment, --tags, etc.");
207
+ }
208
+ body = readFileSync(bodyFile, "utf8");
209
+ }
210
+ else {
211
+ const name = flags.name;
212
+ if (!name) {
213
+ throw new Error("--name is required when not using --body-file");
214
+ }
215
+ const payload = {
216
+ name: flags.name,
217
+ branch: flags.branch || "main",
218
+ base_branch: flags.base_branch ?? "",
219
+ };
220
+ if (flags.comment)
221
+ payload.comment = flags.comment;
222
+ if (flags.tags)
223
+ payload.tags = flags.tags.split(",").map((s) => s.trim()).filter(Boolean);
224
+ if (flags.icon)
225
+ payload.icon = flags.icon;
226
+ if (flags.color)
227
+ payload.color = flags.color;
228
+ body = JSON.stringify(payload);
229
+ }
230
+ return { body, import_mode, validate_dependency, businessDomain, pretty };
231
+ }
232
+ export function parseKnUpdateArgs(args) {
233
+ let knId = "";
234
+ let bodyFile;
235
+ let businessDomain = "bd_public";
236
+ let pretty = true;
237
+ const flags = {};
238
+ for (let i = 0; i < args.length; i += 1) {
239
+ const arg = args[i];
240
+ if (arg === "--help" || arg === "-h") {
241
+ throw new Error("help");
242
+ }
243
+ if (arg === "--body-file") {
244
+ bodyFile = args[i + 1];
245
+ if (!bodyFile || bodyFile.startsWith("-")) {
246
+ throw new Error("Missing value for --body-file");
247
+ }
248
+ i += 1;
249
+ continue;
250
+ }
251
+ if (arg === "-bd" || arg === "--biz-domain") {
252
+ businessDomain = args[i + 1] ?? "bd_public";
253
+ if (!businessDomain || businessDomain.startsWith("-")) {
254
+ throw new Error("Missing value for biz-domain flag");
255
+ }
256
+ i += 1;
257
+ continue;
258
+ }
259
+ if (arg === "--pretty") {
260
+ pretty = true;
261
+ continue;
262
+ }
263
+ if (BODY_FILE_FLAGS.includes(arg)) {
264
+ const key = arg.replace(/^--/, "").replace(/-/g, "_");
265
+ flags[key] = args[i + 1] ?? "";
266
+ i += 1;
267
+ continue;
268
+ }
269
+ if (!arg.startsWith("-") && !knId) {
270
+ knId = arg;
271
+ continue;
272
+ }
273
+ throw new Error(`Unsupported kn update argument: ${arg}`);
274
+ }
275
+ if (!knId) {
276
+ throw new Error("Missing kn-id. Usage: kweaver bkn update <kn-id> [options]");
277
+ }
278
+ let body;
279
+ if (bodyFile) {
280
+ if (Object.keys(flags).length > 0) {
281
+ throw new Error("Cannot use --body-file together with --name, --comment, --tags, etc.");
282
+ }
283
+ body = readFileSync(bodyFile, "utf8");
284
+ }
285
+ else {
286
+ const name = flags.name;
287
+ if (!name) {
288
+ throw new Error("--name is required when not using --body-file");
289
+ }
290
+ const payload = {
291
+ name: flags.name,
292
+ branch: flags.branch || "main",
293
+ base_branch: flags.base_branch ?? "",
294
+ };
295
+ if (flags.comment)
296
+ payload.comment = flags.comment;
297
+ if (flags.tags)
298
+ payload.tags = flags.tags.split(",").map((s) => s.trim()).filter(Boolean);
299
+ if (flags.icon)
300
+ payload.icon = flags.icon;
301
+ if (flags.color)
302
+ payload.color = flags.color;
303
+ body = JSON.stringify(payload);
304
+ }
305
+ return { knId, body, businessDomain, pretty };
306
+ }
307
+ export function parseKnDeleteArgs(args) {
308
+ let knId = "";
309
+ let businessDomain = "bd_public";
310
+ let yes = false;
311
+ for (let i = 0; i < args.length; i += 1) {
312
+ const arg = args[i];
313
+ if (arg === "--help" || arg === "-h") {
314
+ throw new Error("help");
315
+ }
316
+ if (arg === "--yes" || arg === "-y") {
317
+ yes = true;
318
+ continue;
319
+ }
320
+ if (arg === "-bd" || arg === "--biz-domain") {
321
+ businessDomain = args[i + 1] ?? "bd_public";
322
+ if (!businessDomain || businessDomain.startsWith("-")) {
323
+ throw new Error("Missing value for biz-domain flag");
324
+ }
325
+ i += 1;
326
+ continue;
327
+ }
328
+ if (!arg.startsWith("-") && !knId) {
329
+ knId = arg;
330
+ continue;
331
+ }
332
+ throw new Error(`Unsupported kn delete argument: ${arg}`);
333
+ }
334
+ if (!knId) {
335
+ throw new Error("Missing kn-id. Usage: kweaver bkn delete <kn-id>");
336
+ }
337
+ return { knId, businessDomain, yes };
338
+ }
339
+ function parseJsonObject(text, errorMessage) {
340
+ let parsed;
341
+ try {
342
+ parsed = JSON.parse(text);
343
+ }
344
+ catch {
345
+ throw new Error(errorMessage);
346
+ }
347
+ if (!parsed || Array.isArray(parsed) || typeof parsed !== "object") {
348
+ throw new Error(errorMessage);
349
+ }
350
+ return parsed;
351
+ }
352
+ function parseSearchAfterArray(text) {
353
+ let parsed;
354
+ try {
355
+ parsed = JSON.parse(text);
356
+ }
357
+ catch {
358
+ throw new Error("Invalid value for --search-after. Expected a JSON array string.");
359
+ }
360
+ if (!Array.isArray(parsed)) {
361
+ throw new Error("Invalid value for --search-after. Expected a JSON array string.");
362
+ }
363
+ return parsed;
364
+ }
365
+ export function parseKnObjectTypeQueryArgs(args) {
366
+ let pretty = true;
367
+ let businessDomain = "bd_public";
368
+ let limit;
369
+ let searchAfter;
370
+ const positionalArgs = [];
371
+ for (let i = 0; i < args.length; i += 1) {
372
+ const arg = args[i];
373
+ if (arg === "--help" || arg === "-h") {
374
+ throw new Error("help");
375
+ }
376
+ if (arg === "--pretty") {
377
+ pretty = true;
378
+ continue;
379
+ }
380
+ if (arg === "-bd" || arg === "--biz-domain") {
381
+ businessDomain = args[i + 1] ?? "bd_public";
382
+ if (!businessDomain || businessDomain.startsWith("-")) {
383
+ throw new Error("Missing value for biz-domain flag");
384
+ }
385
+ i += 1;
386
+ continue;
387
+ }
388
+ if (arg === "--limit") {
389
+ const rawLimit = args[i + 1];
390
+ const parsedLimit = parseInt(rawLimit ?? "", 10);
391
+ if (!rawLimit || rawLimit.startsWith("-") || Number.isNaN(parsedLimit) || parsedLimit < 1) {
392
+ throw new Error("Invalid value for --limit. Expected a positive integer.");
393
+ }
394
+ limit = parsedLimit;
395
+ i += 1;
396
+ continue;
397
+ }
398
+ if (arg === "--search-after") {
399
+ const rawSearchAfter = args[i + 1];
400
+ if (!rawSearchAfter) {
401
+ throw new Error("Missing value for --search-after. Expected a JSON array string.");
402
+ }
403
+ searchAfter = parseSearchAfterArray(rawSearchAfter);
404
+ i += 1;
405
+ continue;
406
+ }
407
+ positionalArgs.push(arg);
408
+ }
409
+ const [knId, otId, bodyText = "{}"] = positionalArgs;
410
+ if (!knId || !otId) {
411
+ throw new Error("Usage: kweaver bkn object-type query <kn-id> <ot-id> ['<json>'] [--limit <n>] [--search-after '<json-array>'] [--pretty] [-bd value]");
412
+ }
413
+ if (positionalArgs.length > 3) {
414
+ throw new Error("Usage: kweaver bkn object-type query <kn-id> <ot-id> ['<json>'] [--limit <n>] [--search-after '<json-array>'] [--pretty] [-bd value]");
415
+ }
416
+ const body = parseJsonObject(bodyText, "object-type query body must be a JSON object.");
417
+ if (limit !== undefined) {
418
+ body.limit = limit;
419
+ }
420
+ if (searchAfter !== undefined) {
421
+ body.search_after = searchAfter;
422
+ }
423
+ if (typeof body.limit !== "number" || !Number.isFinite(body.limit) || body.limit < 1) {
424
+ throw new Error("Missing limit. Provide it in body JSON or via --limit <n>.");
425
+ }
426
+ return {
427
+ knId,
428
+ otId,
429
+ body: JSON.stringify(body),
430
+ pretty,
431
+ businessDomain,
432
+ };
433
+ }
434
+ const KN_HELP = `kweaver bkn
435
+
436
+ Subcommands:
437
+ list [options] List business knowledge networks
438
+ get <kn-id> [options] Get knowledge network detail (use --stats or --export)
439
+ create [options] Create a knowledge network
440
+ update <kn-id> [options] Update a knowledge network
441
+ delete <kn-id> Delete a knowledge network
442
+ export <kn-id> Export knowledge network (alias for get --export)
443
+ stats <kn-id> Get statistics (alias for get --stats)
444
+ search <kn-id> <query> [options] Semantic search within a knowledge network
445
+ object-type list <kn-id> List object types (schema)
446
+ object-type query <kn-id> <ot-id> ['<json>'] Query object instances (ontology-query; supports --limit/--search-after)
447
+ object-type properties <kn-id> <ot-id> '<json>' Query object properties
448
+ relation-type list <kn-id> List relation types (schema)
449
+ subgraph <kn-id> '<json>' Query subgraph
450
+ action-type list <kn-id> List action types (schema)
451
+ action-type query <kn-id> <at-id> '<json>' Query action info
452
+ action-type execute <kn-id> <at-id> '<json>' Execute action (has side effects)
453
+ action-execution get <kn-id> <execution-id> Get execution status
454
+ action-log list <kn-id> [options] List action execution logs
455
+ action-log get <kn-id> <log-id> Get single execution log
456
+ action-log cancel <kn-id> <log-id> Cancel running execution (has side effects)
457
+
458
+ Use 'kweaver bkn <subcommand> --help' for subcommand options.`;
459
+ export async function runKnCommand(args) {
460
+ const [subcommand, ...rest] = args;
461
+ if (!subcommand || subcommand === "--help" || subcommand === "-h") {
462
+ console.log(KN_HELP);
463
+ return 0;
464
+ }
465
+ if (subcommand === "list") {
466
+ return runKnListCommand(rest);
467
+ }
468
+ if (subcommand === "get") {
469
+ return runKnGetCommand(rest);
470
+ }
471
+ if (subcommand === "create") {
472
+ return runKnCreateCommand(rest);
473
+ }
474
+ if (subcommand === "update") {
475
+ return runKnUpdateCommand(rest);
476
+ }
477
+ if (subcommand === "delete") {
478
+ return runKnDeleteCommand(rest);
479
+ }
480
+ if (subcommand === "export") {
481
+ return runKnGetCommand([...(rest[0] ? [rest[0]] : []), "--export", ...rest.slice(1)]);
482
+ }
483
+ if (subcommand === "stats") {
484
+ return runKnGetCommand([...(rest[0] ? [rest[0]] : []), "--stats", ...rest.slice(1)]);
485
+ }
486
+ if (subcommand === "search") {
487
+ return runKnSearchCommand(rest);
488
+ }
489
+ if (subcommand === "object-type") {
490
+ return runKnObjectTypeCommand(rest);
491
+ }
492
+ if (subcommand === "relation-type") {
493
+ return runKnRelationTypeCommand(rest);
494
+ }
495
+ if (subcommand === "subgraph") {
496
+ return runKnSubgraphCommand(rest);
497
+ }
498
+ if (subcommand === "action-type") {
499
+ return runKnActionTypeCommand(rest);
500
+ }
501
+ if (subcommand === "action-execution") {
502
+ return runKnActionExecutionCommand(rest);
503
+ }
504
+ if (subcommand === "action-log") {
505
+ return runKnActionLogCommand(rest);
506
+ }
507
+ console.error(`Unknown bkn subcommand: ${subcommand}`);
508
+ return 1;
509
+ }
510
+ /** Parse common flags for ontology-query subcommands; returns { filteredArgs, pretty, businessDomain } */
511
+ function parseOntologyQueryFlags(args) {
512
+ let pretty = true;
513
+ let businessDomain = "bd_public";
514
+ const filteredArgs = [];
515
+ for (let i = 0; i < args.length; i += 1) {
516
+ const arg = args[i];
517
+ if (arg === "--help" || arg === "-h") {
518
+ throw new Error("help");
519
+ }
520
+ if (arg === "--pretty") {
521
+ pretty = true;
522
+ continue;
523
+ }
524
+ if ((arg === "-bd" || arg === "--biz-domain") && args[i + 1]) {
525
+ businessDomain = args[i + 1];
526
+ i += 1;
527
+ continue;
528
+ }
529
+ filteredArgs.push(arg);
530
+ }
531
+ return { filteredArgs, pretty, businessDomain };
532
+ }
533
+ export function parseKnActionTypeExecuteArgs(args) {
534
+ let pretty = true;
535
+ let businessDomain = "bd_public";
536
+ let wait = true;
537
+ let timeout = 300;
538
+ const positional = [];
539
+ for (let i = 0; i < args.length; i += 1) {
540
+ const arg = args[i];
541
+ if (arg === "--help" || arg === "-h") {
542
+ throw new Error("help");
543
+ }
544
+ if (arg === "--pretty") {
545
+ pretty = true;
546
+ continue;
547
+ }
548
+ if ((arg === "-bd" || arg === "--biz-domain") && args[i + 1]) {
549
+ businessDomain = args[i + 1];
550
+ i += 1;
551
+ continue;
552
+ }
553
+ if (arg === "--wait") {
554
+ wait = true;
555
+ continue;
556
+ }
557
+ if (arg === "--no-wait") {
558
+ wait = false;
559
+ continue;
560
+ }
561
+ if (arg === "--timeout" && args[i + 1]) {
562
+ timeout = parseInt(args[i + 1], 10);
563
+ if (Number.isNaN(timeout) || timeout < 1)
564
+ timeout = 300;
565
+ i += 1;
566
+ continue;
567
+ }
568
+ positional.push(arg);
569
+ }
570
+ const [knId, atId, body] = positional;
571
+ if (!knId || !atId || !body) {
572
+ throw new Error("Missing kn-id, at-id, or body. Usage: kweaver bkn action-type execute <kn-id> <at-id> '<json>' [options]");
573
+ }
574
+ return {
575
+ knId,
576
+ atId,
577
+ body,
578
+ pretty,
579
+ businessDomain,
580
+ wait,
581
+ timeout,
582
+ };
583
+ }
584
+ async function runKnObjectTypeCommand(args) {
585
+ const [action, ...rest] = args;
586
+ if (!action || action === "--help" || action === "-h") {
587
+ console.log(`kweaver bkn object-type list <kn-id> [--pretty] [-bd value]
588
+ kweaver bkn object-type query <kn-id> <ot-id> ['<json>'] [--limit <n>] [--search-after '<json-array>'] [--pretty] [-bd value]
589
+ kweaver bkn object-type properties <kn-id> <ot-id> '<json>' [--pretty] [-bd value]
590
+
591
+ list: List object types (schema) from ontology-manager.
592
+ query/properties: Query via ontology-query API. For query, --limit and --search-after are merged into the JSON body.`);
593
+ return 0;
594
+ }
595
+ try {
596
+ if (action === "list") {
597
+ const parsed = parseOntologyQueryFlags(rest);
598
+ const [knId] = parsed.filteredArgs;
599
+ if (!knId) {
600
+ console.error("Usage: kweaver bkn object-type list <kn-id> [options]");
601
+ return 1;
602
+ }
603
+ const token = await ensureValidToken();
604
+ const body = await listObjectTypes({
605
+ baseUrl: token.baseUrl,
606
+ accessToken: token.accessToken,
607
+ knId,
608
+ businessDomain: parsed.businessDomain,
609
+ });
610
+ console.log(formatCallOutput(body, parsed.pretty));
611
+ return 0;
612
+ }
613
+ if (action === "query") {
614
+ const options = parseKnObjectTypeQueryArgs(rest);
615
+ const token = await ensureValidToken();
616
+ const result = await objectTypeQuery({
617
+ baseUrl: token.baseUrl,
618
+ accessToken: token.accessToken,
619
+ knId: options.knId,
620
+ otId: options.otId,
621
+ body: options.body,
622
+ businessDomain: options.businessDomain,
623
+ });
624
+ console.log(formatCallOutput(result, options.pretty));
625
+ return 0;
626
+ }
627
+ if (action === "properties") {
628
+ const parsed = parseOntologyQueryFlags(rest);
629
+ const [knId, otId, body] = parsed.filteredArgs;
630
+ if (!knId || !otId || !body) {
631
+ console.error("Usage: kweaver bkn object-type properties <kn-id> <ot-id> '<json>' [options]");
632
+ return 1;
633
+ }
634
+ const token = await ensureValidToken();
635
+ const result = await objectTypeProperties({
636
+ baseUrl: token.baseUrl,
637
+ accessToken: token.accessToken,
638
+ knId,
639
+ otId,
640
+ body,
641
+ businessDomain: parsed.businessDomain,
642
+ });
643
+ console.log(formatCallOutput(result, parsed.pretty));
644
+ return 0;
645
+ }
646
+ console.error(`Unknown object-type action: ${action}. Use list, query, or properties.`);
647
+ return 1;
648
+ }
649
+ catch (error) {
650
+ console.error(formatHttpError(error));
651
+ return 1;
652
+ }
653
+ }
654
+ async function runKnRelationTypeCommand(args) {
655
+ const [action, ...rest] = args;
656
+ if (!action || action === "--help" || action === "-h") {
657
+ console.log(`kweaver bkn relation-type list <kn-id> [--pretty] [-bd value]
658
+
659
+ List relation types (schema) from ontology-manager.`);
660
+ return 0;
661
+ }
662
+ if (action !== "list") {
663
+ console.error(`Unknown relation-type action: ${action}. Use list.`);
664
+ return 1;
665
+ }
666
+ try {
667
+ const parsed = parseOntologyQueryFlags(rest);
668
+ const [knId] = parsed.filteredArgs;
669
+ if (!knId) {
670
+ console.error("Usage: kweaver bkn relation-type list <kn-id> [options]");
671
+ return 1;
672
+ }
673
+ const token = await ensureValidToken();
674
+ const body = await listRelationTypes({
675
+ baseUrl: token.baseUrl,
676
+ accessToken: token.accessToken,
677
+ knId,
678
+ businessDomain: parsed.businessDomain,
679
+ });
680
+ console.log(formatCallOutput(body, parsed.pretty));
681
+ return 0;
682
+ }
683
+ catch (error) {
684
+ console.error(formatHttpError(error));
685
+ return 1;
686
+ }
687
+ }
688
+ async function runKnSubgraphCommand(args) {
689
+ let filteredArgs;
690
+ let pretty;
691
+ let businessDomain;
692
+ try {
693
+ const parsed = parseOntologyQueryFlags(args);
694
+ filteredArgs = parsed.filteredArgs;
695
+ pretty = parsed.pretty;
696
+ businessDomain = parsed.businessDomain;
697
+ }
698
+ catch (error) {
699
+ if (error instanceof Error && error.message === "help") {
700
+ console.log(`kweaver bkn subgraph <kn-id> '<json>' [--pretty] [-bd value]
701
+
702
+ Query subgraph via ontology-query API. JSON body format see ref/ontology/ontology-query.yaml.`);
703
+ return 0;
704
+ }
705
+ throw error;
706
+ }
707
+ const [knId, body] = filteredArgs;
708
+ if (!knId || !body) {
709
+ console.error("Usage: kweaver bkn subgraph <kn-id> '<json>' [options]");
710
+ return 1;
711
+ }
712
+ try {
713
+ const token = await ensureValidToken();
714
+ const result = await subgraph({
715
+ baseUrl: token.baseUrl,
716
+ accessToken: token.accessToken,
717
+ knId,
718
+ body,
719
+ businessDomain,
720
+ });
721
+ console.log(formatCallOutput(result, pretty));
722
+ return 0;
723
+ }
724
+ catch (error) {
725
+ console.error(formatHttpError(error));
726
+ return 1;
727
+ }
728
+ }
729
+ const TERMINAL_STATUSES = ["SUCCESS", "FAILED", "CANCELLED"];
730
+ function extractExecutionId(body) {
731
+ try {
732
+ const data = JSON.parse(body);
733
+ const id = data.execution_id ?? data.id;
734
+ return typeof id === "string" ? id : null;
735
+ }
736
+ catch {
737
+ return null;
738
+ }
739
+ }
740
+ function extractStatus(body) {
741
+ try {
742
+ const data = JSON.parse(body);
743
+ const status = data.status;
744
+ return typeof status === "string" ? status : "";
745
+ }
746
+ catch {
747
+ return "";
748
+ }
749
+ }
750
+ async function runKnActionTypeCommand(args) {
751
+ const [action, ...rest] = args;
752
+ if (!action || action === "--help" || action === "-h") {
753
+ console.log(`kweaver bkn action-type list <kn-id> [--pretty] [-bd value]
754
+ kweaver bkn action-type query <kn-id> <at-id> '<json>' [--pretty] [-bd value]
755
+ kweaver bkn action-type execute <kn-id> <at-id> '<json>' [--pretty] [-bd value] [--wait|--no-wait] [--timeout n]
756
+
757
+ list: List action types (schema) from ontology-manager.
758
+ query/execute: Query or execute actions. execute has side effects - only use when explicitly requested.
759
+ --wait (default) Poll until execution completes
760
+ --no-wait Return immediately after starting execution
761
+ --timeout <seconds> Max wait time when --wait (default: 300)`);
762
+ return 0;
763
+ }
764
+ if (action === "list") {
765
+ try {
766
+ const parsed = parseOntologyQueryFlags(rest);
767
+ const [knId] = parsed.filteredArgs;
768
+ if (!knId) {
769
+ console.error("Usage: kweaver bkn action-type list <kn-id> [options]");
770
+ return 1;
771
+ }
772
+ const token = await ensureValidToken();
773
+ const body = await listActionTypes({
774
+ baseUrl: token.baseUrl,
775
+ accessToken: token.accessToken,
776
+ knId,
777
+ businessDomain: parsed.businessDomain,
778
+ });
779
+ console.log(formatCallOutput(body, parsed.pretty));
780
+ return 0;
781
+ }
782
+ catch (error) {
783
+ console.error(formatHttpError(error));
784
+ return 1;
785
+ }
786
+ }
787
+ if (action === "query") {
788
+ let filteredArgs;
789
+ let pretty;
790
+ let businessDomain;
791
+ try {
792
+ const parsed = parseOntologyQueryFlags(rest);
793
+ filteredArgs = parsed.filteredArgs;
794
+ pretty = parsed.pretty;
795
+ businessDomain = parsed.businessDomain;
796
+ }
797
+ catch (error) {
798
+ if (error instanceof Error && error.message === "help")
799
+ return 0;
800
+ throw error;
801
+ }
802
+ const [knId, atId, body] = filteredArgs;
803
+ if (!knId || !atId || !body) {
804
+ console.error("Usage: kweaver bkn action-type query <kn-id> <at-id> '<json>' [options]");
805
+ return 1;
806
+ }
807
+ try {
808
+ const token = await ensureValidToken();
809
+ const result = await actionTypeQuery({
810
+ baseUrl: token.baseUrl,
811
+ accessToken: token.accessToken,
812
+ knId,
813
+ atId,
814
+ body,
815
+ businessDomain,
816
+ });
817
+ console.log(formatCallOutput(result, pretty));
818
+ return 0;
819
+ }
820
+ catch (error) {
821
+ console.error(formatHttpError(error));
822
+ return 1;
823
+ }
824
+ }
825
+ if (action === "execute") {
826
+ let options;
827
+ try {
828
+ options = parseKnActionTypeExecuteArgs(rest);
829
+ }
830
+ catch (error) {
831
+ if (error instanceof Error && error.message === "help")
832
+ return 0;
833
+ console.error(formatHttpError(error));
834
+ return 1;
835
+ }
836
+ try {
837
+ const token = await ensureValidToken();
838
+ const base = {
839
+ baseUrl: token.baseUrl,
840
+ accessToken: token.accessToken,
841
+ knId: options.knId,
842
+ atId: options.atId,
843
+ body: options.body,
844
+ businessDomain: options.businessDomain,
845
+ };
846
+ const result = await actionTypeExecute(base);
847
+ if (!options.wait) {
848
+ console.log(formatCallOutput(result, options.pretty));
849
+ return 0;
850
+ }
851
+ const executionId = extractExecutionId(result);
852
+ if (!executionId) {
853
+ console.log(formatCallOutput(result, options.pretty));
854
+ return 0;
855
+ }
856
+ const deadline = Date.now() + options.timeout * 1000;
857
+ let lastBody = result;
858
+ while (Date.now() < deadline) {
859
+ const status = extractStatus(lastBody);
860
+ if (TERMINAL_STATUSES.includes(status.toUpperCase())) {
861
+ console.log(formatCallOutput(lastBody, options.pretty));
862
+ return status.toUpperCase() === "SUCCESS" ? 0 : 1;
863
+ }
864
+ await new Promise((r) => setTimeout(r, 2000));
865
+ lastBody = await actionExecutionGet({
866
+ baseUrl: token.baseUrl,
867
+ accessToken: token.accessToken,
868
+ knId: options.knId,
869
+ executionId,
870
+ businessDomain: options.businessDomain,
871
+ });
872
+ }
873
+ console.error(`Action execution did not complete within ${options.timeout}s`);
874
+ console.log(formatCallOutput(lastBody, options.pretty));
875
+ return 1;
876
+ }
877
+ catch (error) {
878
+ console.error(formatHttpError(error));
879
+ return 1;
880
+ }
881
+ }
882
+ console.error(`Unknown action-type action: ${action}. Use list, query, or execute.`);
883
+ return 1;
884
+ }
885
+ async function runKnActionExecutionCommand(args) {
886
+ let filteredArgs;
887
+ let pretty;
888
+ let businessDomain;
889
+ try {
890
+ const parsed = parseOntologyQueryFlags(args);
891
+ filteredArgs = parsed.filteredArgs;
892
+ pretty = parsed.pretty;
893
+ businessDomain = parsed.businessDomain;
894
+ }
895
+ catch (error) {
896
+ if (error instanceof Error && error.message === "help") {
897
+ console.log(`kweaver bkn action-execution get <kn-id> <execution-id> [--pretty] [-bd value]
898
+
899
+ Get action execution status.`);
900
+ return 0;
901
+ }
902
+ throw error;
903
+ }
904
+ const [subAction, knId, executionId] = filteredArgs;
905
+ if (subAction !== "get" || !knId || !executionId) {
906
+ console.error("Usage: kweaver bkn action-execution get <kn-id> <execution-id> [options]");
907
+ return 1;
908
+ }
909
+ try {
910
+ const token = await ensureValidToken();
911
+ const result = await actionExecutionGet({
912
+ baseUrl: token.baseUrl,
913
+ accessToken: token.accessToken,
914
+ knId,
915
+ executionId,
916
+ businessDomain,
917
+ });
918
+ console.log(formatCallOutput(result, pretty));
919
+ return 0;
920
+ }
921
+ catch (error) {
922
+ console.error(formatHttpError(error));
923
+ return 1;
924
+ }
925
+ }
926
+ async function runKnActionLogCommand(args) {
927
+ const [action, ...rest] = args;
928
+ if (!action || action === "--help" || action === "-h") {
929
+ console.log(`kweaver bkn action-log list <kn-id> [options]
930
+ kweaver bkn action-log get <kn-id> <log-id> [options]
931
+ kweaver bkn action-log cancel <kn-id> <log-id> [options]
932
+
933
+ List/get execution logs. cancel has side effects - only use when explicitly requested.
934
+ Options for list: --limit, --need-total, --action-type-id, --status, --trigger-type, --search-after`);
935
+ return 0;
936
+ }
937
+ let pretty = true;
938
+ let businessDomain = "bd_public";
939
+ let limit;
940
+ let needTotal;
941
+ let actionTypeId;
942
+ let status;
943
+ let triggerType;
944
+ let searchAfter;
945
+ const filteredArgs = [];
946
+ for (let i = 0; i < rest.length; i += 1) {
947
+ const arg = rest[i];
948
+ if (arg === "--help" || arg === "-h") {
949
+ throw new Error("help");
950
+ }
951
+ if (arg === "--pretty") {
952
+ pretty = true;
953
+ continue;
954
+ }
955
+ if ((arg === "-bd" || arg === "--biz-domain") && rest[i + 1]) {
956
+ businessDomain = rest[i + 1];
957
+ i += 1;
958
+ continue;
959
+ }
960
+ if (arg === "--limit" && rest[i + 1]) {
961
+ limit = parseInt(rest[i + 1], 10);
962
+ i += 1;
963
+ continue;
964
+ }
965
+ if (arg === "--need-total" && rest[i + 1]) {
966
+ needTotal = rest[i + 1].toLowerCase() === "true";
967
+ i += 1;
968
+ continue;
969
+ }
970
+ if (arg === "--action-type-id" && rest[i + 1]) {
971
+ actionTypeId = rest[i + 1];
972
+ i += 1;
973
+ continue;
974
+ }
975
+ if (arg === "--status" && rest[i + 1]) {
976
+ status = rest[i + 1];
977
+ i += 1;
978
+ continue;
979
+ }
980
+ if (arg === "--trigger-type" && rest[i + 1]) {
981
+ triggerType = rest[i + 1];
982
+ i += 1;
983
+ continue;
984
+ }
985
+ if (arg === "--search-after" && rest[i + 1]) {
986
+ searchAfter = rest[i + 1];
987
+ i += 1;
988
+ continue;
989
+ }
990
+ filteredArgs.push(arg);
991
+ }
992
+ try {
993
+ const token = await ensureValidToken();
994
+ const base = {
995
+ baseUrl: token.baseUrl,
996
+ accessToken: token.accessToken,
997
+ businessDomain,
998
+ };
999
+ if (action === "list") {
1000
+ const [knId] = filteredArgs;
1001
+ if (!knId) {
1002
+ console.error("Usage: kweaver bkn action-log list <kn-id> [options]");
1003
+ return 1;
1004
+ }
1005
+ const result = await actionLogsList({
1006
+ ...base,
1007
+ knId,
1008
+ limit,
1009
+ needTotal,
1010
+ actionTypeId,
1011
+ status,
1012
+ triggerType,
1013
+ searchAfter,
1014
+ });
1015
+ console.log(formatCallOutput(result, pretty));
1016
+ return 0;
1017
+ }
1018
+ if (action === "get") {
1019
+ const [knId, logId] = filteredArgs;
1020
+ if (!knId || !logId) {
1021
+ console.error("Usage: kweaver bkn action-log get <kn-id> <log-id> [options]");
1022
+ return 1;
1023
+ }
1024
+ const result = await actionLogGet({ ...base, knId, logId });
1025
+ console.log(formatCallOutput(result, pretty));
1026
+ return 0;
1027
+ }
1028
+ if (action === "cancel") {
1029
+ const [knId, logId] = filteredArgs;
1030
+ if (!knId || !logId) {
1031
+ console.error("Usage: kweaver bkn action-log cancel <kn-id> <log-id> [options]");
1032
+ return 1;
1033
+ }
1034
+ const result = await actionLogCancel({ ...base, knId, logId });
1035
+ console.log(formatCallOutput(result, pretty));
1036
+ return 0;
1037
+ }
1038
+ console.error(`Unknown action-log action: ${action}. Use list, get, or cancel.`);
1039
+ return 1;
1040
+ }
1041
+ catch (error) {
1042
+ console.error(formatHttpError(error));
1043
+ return 1;
1044
+ }
1045
+ }
1046
+ async function runKnListCommand(args) {
1047
+ let options;
1048
+ try {
1049
+ options = parseKnListArgs(args);
1050
+ }
1051
+ catch (error) {
1052
+ if (error instanceof Error && error.message === "help") {
1053
+ console.log(KN_LIST_HELP);
1054
+ return 0;
1055
+ }
1056
+ console.error(formatHttpError(error));
1057
+ return 1;
1058
+ }
1059
+ try {
1060
+ const token = await ensureValidToken();
1061
+ const body = await listKnowledgeNetworks({
1062
+ baseUrl: token.baseUrl,
1063
+ accessToken: token.accessToken,
1064
+ businessDomain: options.businessDomain,
1065
+ offset: options.offset,
1066
+ limit: options.limit,
1067
+ sort: options.sort,
1068
+ direction: options.direction,
1069
+ name_pattern: options.name_pattern,
1070
+ tag: options.tag,
1071
+ });
1072
+ if (body) {
1073
+ console.log(options.verbose
1074
+ ? formatCallOutput(body, options.pretty)
1075
+ : formatSimpleKnList(body, options.pretty, options.detail));
1076
+ }
1077
+ return 0;
1078
+ }
1079
+ catch (error) {
1080
+ console.error(formatHttpError(error));
1081
+ return 1;
1082
+ }
1083
+ }
1084
+ const KN_LIST_HELP = `kweaver bkn list [options]
1085
+
1086
+ List business knowledge networks from the ontology-manager API.
1087
+
1088
+ Options:
1089
+ --offset <n> Offset (default: 0)
1090
+ --limit <n> Limit (default: 50)
1091
+ --sort <key> Sort field (default: update_time)
1092
+ --direction <asc|desc> Sort direction (default: desc)
1093
+ --name-pattern <s> Filter by name pattern
1094
+ --tag <s> Filter by tag
1095
+ --detail Include the detail field in simplified output
1096
+ --verbose, -v Show full JSON response
1097
+ -bd, --biz-domain <value> Business domain (default: bd_public)
1098
+ --pretty Pretty-print JSON output (applies to both modes)`;
1099
+ const KN_GET_HELP = `kweaver bkn get <kn-id> [options]
1100
+
1101
+ Get knowledge network detail.
1102
+
1103
+ Options:
1104
+ --stats Include statistics
1105
+ --export Export mode (include sub-types)
1106
+ -bd, --biz-domain <value> Business domain (default: bd_public)
1107
+ --pretty Pretty-print JSON output`;
1108
+ const KN_CREATE_HELP = `kweaver bkn create [options]
1109
+
1110
+ Create a knowledge network.
1111
+
1112
+ Options:
1113
+ --name <s> Name (required unless --body-file)
1114
+ --comment <s> Comment
1115
+ --tags <t1,t2> Comma-separated tags
1116
+ --icon <s> Icon
1117
+ --color <s> Color
1118
+ --branch <s> Branch (default: main)
1119
+ --base-branch <s> Base branch (default: empty for main)
1120
+ --body-file <path> Read full JSON body from file (cannot combine with flags above)
1121
+ --import-mode <normal|ignore|overwrite> Import mode (default: normal)
1122
+ --validate-dependency <true|false> Validate dependency (default: true)
1123
+ -bd, --biz-domain <value> Business domain (default: bd_public)
1124
+ --pretty Pretty-print JSON output`;
1125
+ const KN_UPDATE_HELP = `kweaver bkn update <kn-id> [options]
1126
+
1127
+ Update a knowledge network.
1128
+
1129
+ Options:
1130
+ --name <s> Name (required unless --body-file)
1131
+ --comment <s> Comment
1132
+ --tags <t1,t2> Comma-separated tags
1133
+ --icon <s> Icon
1134
+ --color <s> Color
1135
+ --branch <s> Branch (default: main)
1136
+ --base-branch <s> Base branch (default: empty for main)
1137
+ --body-file <path> Read full JSON body from file (cannot combine with flags above)
1138
+ -bd, --biz-domain <value> Business domain (default: bd_public)
1139
+ --pretty Pretty-print JSON output`;
1140
+ const KN_DELETE_HELP = `kweaver bkn delete <kn-id>
1141
+
1142
+ Delete a knowledge network and its object types, relation types, action types, and concept groups.
1143
+
1144
+ Options:
1145
+ --yes, -y Skip confirmation prompt
1146
+ -bd, --biz-domain <value> Business domain (default: bd_public)`;
1147
+ async function runKnGetCommand(args) {
1148
+ let options;
1149
+ try {
1150
+ options = parseKnGetArgs(args);
1151
+ }
1152
+ catch (error) {
1153
+ if (error instanceof Error && error.message === "help") {
1154
+ console.log(KN_GET_HELP);
1155
+ return 0;
1156
+ }
1157
+ console.error(formatHttpError(error));
1158
+ return 1;
1159
+ }
1160
+ try {
1161
+ const token = await ensureValidToken();
1162
+ const body = await getKnowledgeNetwork({
1163
+ baseUrl: token.baseUrl,
1164
+ accessToken: token.accessToken,
1165
+ knId: options.knId,
1166
+ businessDomain: options.businessDomain,
1167
+ mode: options.export ? "export" : undefined,
1168
+ include_statistics: options.stats ? true : undefined,
1169
+ });
1170
+ if (body) {
1171
+ console.log(formatCallOutput(body, options.pretty));
1172
+ }
1173
+ return 0;
1174
+ }
1175
+ catch (error) {
1176
+ console.error(formatHttpError(error));
1177
+ return 1;
1178
+ }
1179
+ }
1180
+ async function runKnCreateCommand(args) {
1181
+ let options;
1182
+ try {
1183
+ options = parseKnCreateArgs(args);
1184
+ }
1185
+ catch (error) {
1186
+ if (error instanceof Error && error.message === "help") {
1187
+ console.log(KN_CREATE_HELP);
1188
+ return 0;
1189
+ }
1190
+ console.error(formatHttpError(error));
1191
+ return 1;
1192
+ }
1193
+ try {
1194
+ const token = await ensureValidToken();
1195
+ const body = await createKnowledgeNetwork({
1196
+ baseUrl: token.baseUrl,
1197
+ accessToken: token.accessToken,
1198
+ body: options.body,
1199
+ businessDomain: options.businessDomain,
1200
+ import_mode: options.import_mode,
1201
+ validate_dependency: options.validate_dependency,
1202
+ });
1203
+ if (body) {
1204
+ console.log(formatCallOutput(body, options.pretty));
1205
+ }
1206
+ return 0;
1207
+ }
1208
+ catch (error) {
1209
+ console.error(formatHttpError(error));
1210
+ return 1;
1211
+ }
1212
+ }
1213
+ async function runKnUpdateCommand(args) {
1214
+ let options;
1215
+ try {
1216
+ options = parseKnUpdateArgs(args);
1217
+ }
1218
+ catch (error) {
1219
+ if (error instanceof Error && error.message === "help") {
1220
+ console.log(KN_UPDATE_HELP);
1221
+ return 0;
1222
+ }
1223
+ console.error(formatHttpError(error));
1224
+ return 1;
1225
+ }
1226
+ try {
1227
+ const token = await ensureValidToken();
1228
+ const body = await updateKnowledgeNetwork({
1229
+ baseUrl: token.baseUrl,
1230
+ accessToken: token.accessToken,
1231
+ knId: options.knId,
1232
+ body: options.body,
1233
+ businessDomain: options.businessDomain,
1234
+ });
1235
+ if (body) {
1236
+ console.log(formatCallOutput(body, options.pretty));
1237
+ }
1238
+ return 0;
1239
+ }
1240
+ catch (error) {
1241
+ console.error(formatHttpError(error));
1242
+ return 1;
1243
+ }
1244
+ }
1245
+ function confirmDelete(knId) {
1246
+ return new Promise((resolve) => {
1247
+ const rl = createInterface({ input: process.stdin, output: process.stdout });
1248
+ rl.question(`Delete knowledge network ${knId}? [y/N] `, (answer) => {
1249
+ rl.close();
1250
+ const trimmed = answer.trim().toLowerCase();
1251
+ resolve(trimmed === "y" || trimmed === "yes");
1252
+ });
1253
+ });
1254
+ }
1255
+ async function runKnDeleteCommand(args) {
1256
+ let options;
1257
+ try {
1258
+ options = parseKnDeleteArgs(args);
1259
+ }
1260
+ catch (error) {
1261
+ if (error instanceof Error && error.message === "help") {
1262
+ console.log(KN_DELETE_HELP);
1263
+ return 0;
1264
+ }
1265
+ console.error(formatHttpError(error));
1266
+ return 1;
1267
+ }
1268
+ if (!options.yes) {
1269
+ const confirmed = await confirmDelete(options.knId);
1270
+ if (!confirmed) {
1271
+ console.error("Aborted.");
1272
+ return 1;
1273
+ }
1274
+ }
1275
+ try {
1276
+ const token = await ensureValidToken();
1277
+ await deleteKnowledgeNetwork({
1278
+ baseUrl: token.baseUrl,
1279
+ accessToken: token.accessToken,
1280
+ knId: options.knId,
1281
+ businessDomain: options.businessDomain,
1282
+ });
1283
+ return 0;
1284
+ }
1285
+ catch (error) {
1286
+ console.error(formatHttpError(error));
1287
+ return 1;
1288
+ }
1289
+ }
1290
+ // ── search ──────────────────────────────────────────────────────────────────
1291
+ const KN_SEARCH_HELP = `kweaver bkn search <kn-id> <query> [--max-concepts <n>] [--mode <mode>] [--pretty] [-bd value]
1292
+
1293
+ Semantic search within a knowledge network via agent-retrieval API.
1294
+ Returns matched concepts (object types, relation types, action types).
1295
+
1296
+ Options:
1297
+ --max-concepts <n> Max concepts to return (default: 10)
1298
+ --mode <mode> Search mode (default: keyword_vector_retrieval)
1299
+ --pretty Pretty-print JSON output
1300
+ -bd, --biz-domain Override x-business-domain`;
1301
+ export function parseKnSearchArgs(args) {
1302
+ let knId = "";
1303
+ let query = "";
1304
+ let maxConcepts = 10;
1305
+ let mode = "keyword_vector_retrieval";
1306
+ let pretty = false;
1307
+ let businessDomain = process.env.KWEAVER_BUSINESS_DOMAIN ?? "bd_public";
1308
+ const positional = [];
1309
+ for (let i = 0; i < args.length; i++) {
1310
+ const arg = args[i];
1311
+ if (arg === "--max-concepts") {
1312
+ maxConcepts = Number(args[++i]);
1313
+ }
1314
+ else if (arg === "--mode") {
1315
+ mode = args[++i];
1316
+ }
1317
+ else if (arg === "--pretty") {
1318
+ pretty = true;
1319
+ }
1320
+ else if (arg === "-bd" || arg === "--biz-domain") {
1321
+ businessDomain = args[++i];
1322
+ }
1323
+ else if (arg === "--help" || arg === "-h") {
1324
+ throw Object.assign(new Error("help"), { isHelp: true });
1325
+ }
1326
+ else if (arg.startsWith("-")) {
1327
+ throw new Error(`Unknown flag: ${arg}`);
1328
+ }
1329
+ else {
1330
+ positional.push(arg);
1331
+ }
1332
+ }
1333
+ knId = positional[0] ?? "";
1334
+ query = positional.slice(1).join(" ");
1335
+ if (!knId || !query) {
1336
+ throw new Error("Usage: kweaver bkn search <kn-id> <query> [options]");
1337
+ }
1338
+ return { knId, query, maxConcepts, mode, pretty, businessDomain };
1339
+ }
1340
+ async function runKnSearchCommand(args) {
1341
+ let options;
1342
+ try {
1343
+ options = parseKnSearchArgs(args);
1344
+ }
1345
+ catch (error) {
1346
+ if (error instanceof Error && error.isHelp) {
1347
+ console.log(KN_SEARCH_HELP);
1348
+ return 0;
1349
+ }
1350
+ console.error(error instanceof Error ? error.message : String(error));
1351
+ return 1;
1352
+ }
1353
+ try {
1354
+ const token = await ensureValidToken();
1355
+ const result = await semanticSearch({
1356
+ baseUrl: token.baseUrl,
1357
+ accessToken: token.accessToken,
1358
+ knId: options.knId,
1359
+ query: options.query,
1360
+ businessDomain: options.businessDomain,
1361
+ maxConcepts: options.maxConcepts,
1362
+ mode: options.mode,
1363
+ });
1364
+ console.log(formatCallOutput(result, options.pretty));
1365
+ return 0;
1366
+ }
1367
+ catch (error) {
1368
+ console.error(formatHttpError(error));
1369
+ return 1;
1370
+ }
1371
+ }