@kweaver-ai/kweaver-sdk 0.5.0 → 0.5.2

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 (66) hide show
  1. package/README.md +6 -1
  2. package/README.zh.md +5 -0
  3. package/dist/api/agent-chat.d.ts +1 -1
  4. package/dist/api/agent-chat.js +4 -4
  5. package/dist/api/agent-list.d.ts +35 -0
  6. package/dist/api/agent-list.js +86 -12
  7. package/dist/api/bkn-backend.d.ts +60 -0
  8. package/dist/api/bkn-backend.js +103 -10
  9. package/dist/api/conversations.d.ts +6 -3
  10. package/dist/api/conversations.js +26 -27
  11. package/dist/api/dataflow.js +1 -10
  12. package/dist/api/datasources.js +1 -10
  13. package/dist/api/dataviews.js +1 -10
  14. package/dist/api/headers.d.ts +9 -0
  15. package/dist/api/headers.js +25 -0
  16. package/dist/api/knowledge-networks.d.ts +41 -0
  17. package/dist/api/knowledge-networks.js +69 -22
  18. package/dist/api/ontology-query.d.ts +14 -1
  19. package/dist/api/ontology-query.js +63 -49
  20. package/dist/api/semantic-search.js +2 -12
  21. package/dist/api/skills.d.ts +141 -0
  22. package/dist/api/skills.js +216 -0
  23. package/dist/api/vega.d.ts +63 -0
  24. package/dist/api/vega.js +131 -10
  25. package/dist/auth/oauth.d.ts +5 -1
  26. package/dist/auth/oauth.js +293 -94
  27. package/dist/cli.js +29 -4
  28. package/dist/client.d.ts +3 -0
  29. package/dist/client.js +4 -0
  30. package/dist/commands/agent.d.ts +33 -1
  31. package/dist/commands/agent.js +721 -49
  32. package/dist/commands/auth.js +211 -21
  33. package/dist/commands/bkn-ops.d.ts +77 -0
  34. package/dist/commands/bkn-ops.js +1056 -0
  35. package/dist/commands/bkn-query.d.ts +14 -0
  36. package/dist/commands/bkn-query.js +370 -0
  37. package/dist/commands/bkn-schema.d.ts +135 -0
  38. package/dist/commands/bkn-schema.js +1461 -0
  39. package/dist/commands/bkn-utils.d.ts +36 -0
  40. package/dist/commands/bkn-utils.js +102 -0
  41. package/dist/commands/bkn.d.ts +7 -113
  42. package/dist/commands/bkn.js +175 -2429
  43. package/dist/commands/dataview.d.ts +7 -0
  44. package/dist/commands/dataview.js +38 -2
  45. package/dist/commands/ds.d.ts +1 -0
  46. package/dist/commands/ds.js +8 -1
  47. package/dist/commands/import-csv.d.ts +2 -0
  48. package/dist/commands/import-csv.js +3 -2
  49. package/dist/commands/skill.d.ts +26 -0
  50. package/dist/commands/skill.js +524 -0
  51. package/dist/commands/vega.js +371 -14
  52. package/dist/config/jwt.d.ts +6 -0
  53. package/dist/config/jwt.js +21 -0
  54. package/dist/config/store.d.ts +37 -5
  55. package/dist/config/store.js +363 -30
  56. package/dist/index.d.ts +6 -1
  57. package/dist/index.js +5 -1
  58. package/dist/resources/bkn.d.ts +4 -0
  59. package/dist/resources/bkn.js +4 -0
  60. package/dist/resources/conversations.d.ts +5 -2
  61. package/dist/resources/conversations.js +17 -3
  62. package/dist/resources/skills.d.ts +47 -0
  63. package/dist/resources/skills.js +47 -0
  64. package/dist/resources/vega.d.ts +11 -0
  65. package/dist/resources/vega.js +37 -1
  66. package/package.json +1 -1
@@ -0,0 +1,14 @@
1
+ export declare function runKnSubgraphCommand(args: string[]): Promise<number>;
2
+ export declare function runKnActionExecutionCommand(args: string[]): Promise<number>;
3
+ export declare function runKnActionLogCommand(args: string[]): Promise<number>;
4
+ export declare function parseKnSearchArgs(args: string[]): {
5
+ knId: string;
6
+ query: string;
7
+ maxConcepts: number;
8
+ mode: string;
9
+ pretty: boolean;
10
+ businessDomain: string;
11
+ };
12
+ export declare function runKnSearchCommand(args: string[]): Promise<number>;
13
+ export declare function runKnRelationTypePathsCommand(args: string[]): Promise<number>;
14
+ export declare function runKnResourcesCommand(args: string[]): Promise<number>;
@@ -0,0 +1,370 @@
1
+ import { ensureValidToken, formatHttpError } from "../auth/oauth.js";
2
+ import { subgraph, actionExecutionGet, actionLogsList, actionLogGet, actionLogCancel, } from "../api/ontology-query.js";
3
+ import { queryRelationTypePaths, listBknResources } from "../api/bkn-backend.js";
4
+ import { semanticSearch } from "../api/semantic-search.js";
5
+ import { formatCallOutput } from "./call.js";
6
+ import { resolveBusinessDomain } from "../config/store.js";
7
+ import { parseOntologyQueryFlags } from "./bkn-utils.js";
8
+ // ── subgraph ─────────────────────────────────────────────────────────────────
9
+ export async function runKnSubgraphCommand(args) {
10
+ let filteredArgs;
11
+ let pretty;
12
+ let businessDomain;
13
+ try {
14
+ const parsed = parseOntologyQueryFlags(args);
15
+ filteredArgs = parsed.filteredArgs;
16
+ pretty = parsed.pretty;
17
+ businessDomain = parsed.businessDomain;
18
+ }
19
+ catch (error) {
20
+ if (error instanceof Error && error.message === "help") {
21
+ console.log(`kweaver bkn subgraph <kn-id> '<json>' [--pretty] [-bd value]
22
+
23
+ Query subgraph via ontology-query API. JSON body format see references/json-formats.md#subgraph.`);
24
+ return 0;
25
+ }
26
+ throw error;
27
+ }
28
+ const [knId, body] = filteredArgs;
29
+ if (!knId || !body) {
30
+ console.error("Usage: kweaver bkn subgraph <kn-id> '<json>' [options]");
31
+ return 1;
32
+ }
33
+ try {
34
+ // Auto-detect query_type=relation_path when body contains source_object_type_id
35
+ let queryType;
36
+ try {
37
+ const parsedBody = JSON.parse(body);
38
+ if (parsedBody.source_object_type_id) {
39
+ queryType = "relation_path";
40
+ }
41
+ }
42
+ catch {
43
+ // Not valid JSON — let the API return the error
44
+ }
45
+ const token = await ensureValidToken();
46
+ const result = await subgraph({
47
+ baseUrl: token.baseUrl,
48
+ accessToken: token.accessToken,
49
+ knId,
50
+ body,
51
+ businessDomain,
52
+ queryType,
53
+ });
54
+ if (result.length > 100_000) {
55
+ console.error(`[warn] Response is ${(result.length / 1024).toFixed(0)}KB. Consider narrowing the subgraph query.`);
56
+ }
57
+ console.log(formatCallOutput(result, pretty));
58
+ return 0;
59
+ }
60
+ catch (error) {
61
+ console.error(formatHttpError(error));
62
+ return 1;
63
+ }
64
+ }
65
+ // ── action-execution ─────────────────────────────────────────────────────────
66
+ export async function runKnActionExecutionCommand(args) {
67
+ let filteredArgs;
68
+ let pretty;
69
+ let businessDomain;
70
+ try {
71
+ const parsed = parseOntologyQueryFlags(args);
72
+ filteredArgs = parsed.filteredArgs;
73
+ pretty = parsed.pretty;
74
+ businessDomain = parsed.businessDomain;
75
+ }
76
+ catch (error) {
77
+ if (error instanceof Error && error.message === "help") {
78
+ console.log(`kweaver bkn action-execution get <kn-id> <execution-id> [--pretty] [-bd value]
79
+
80
+ Get action execution status.`);
81
+ return 0;
82
+ }
83
+ throw error;
84
+ }
85
+ const [subAction, knId, executionId] = filteredArgs;
86
+ if (subAction !== "get" || !knId || !executionId) {
87
+ console.error("Usage: kweaver bkn action-execution get <kn-id> <execution-id> [options]");
88
+ return 1;
89
+ }
90
+ try {
91
+ const token = await ensureValidToken();
92
+ const result = await actionExecutionGet({
93
+ baseUrl: token.baseUrl,
94
+ accessToken: token.accessToken,
95
+ knId,
96
+ executionId,
97
+ businessDomain,
98
+ });
99
+ console.log(formatCallOutput(result, pretty));
100
+ return 0;
101
+ }
102
+ catch (error) {
103
+ console.error(formatHttpError(error));
104
+ return 1;
105
+ }
106
+ }
107
+ // ── action-log ───────────────────────────────────────────────────────────────
108
+ export async function runKnActionLogCommand(args) {
109
+ const [action, ...rest] = args;
110
+ if (!action || action === "--help" || action === "-h") {
111
+ console.log(`kweaver bkn action-log list <kn-id> [options]
112
+ kweaver bkn action-log get <kn-id> <log-id> [options]
113
+ kweaver bkn action-log cancel <kn-id> <log-id> [options]
114
+
115
+ List/get execution logs. cancel has side effects - only use when explicitly requested.
116
+ Options for list: --limit, --need-total, --action-type-id, --status, --trigger-type, --search-after`);
117
+ return 0;
118
+ }
119
+ let pretty = true;
120
+ let businessDomain = "";
121
+ let limit = 30;
122
+ let needTotal;
123
+ let actionTypeId;
124
+ let status;
125
+ let triggerType;
126
+ let searchAfter;
127
+ const filteredArgs = [];
128
+ for (let i = 0; i < rest.length; i += 1) {
129
+ const arg = rest[i];
130
+ if (arg === "--help" || arg === "-h") {
131
+ throw new Error("help");
132
+ }
133
+ if (arg === "--pretty") {
134
+ pretty = true;
135
+ continue;
136
+ }
137
+ if ((arg === "-bd" || arg === "--biz-domain") && rest[i + 1]) {
138
+ businessDomain = rest[i + 1];
139
+ i += 1;
140
+ continue;
141
+ }
142
+ if (arg === "--limit" && rest[i + 1]) {
143
+ limit = parseInt(rest[i + 1], 10);
144
+ i += 1;
145
+ continue;
146
+ }
147
+ if (arg === "--need-total" && rest[i + 1]) {
148
+ needTotal = rest[i + 1].toLowerCase() === "true";
149
+ i += 1;
150
+ continue;
151
+ }
152
+ if (arg === "--action-type-id" && rest[i + 1]) {
153
+ actionTypeId = rest[i + 1];
154
+ i += 1;
155
+ continue;
156
+ }
157
+ if (arg === "--status" && rest[i + 1]) {
158
+ status = rest[i + 1];
159
+ i += 1;
160
+ continue;
161
+ }
162
+ if (arg === "--trigger-type" && rest[i + 1]) {
163
+ triggerType = rest[i + 1];
164
+ i += 1;
165
+ continue;
166
+ }
167
+ if (arg === "--search-after" && rest[i + 1]) {
168
+ searchAfter = rest[i + 1];
169
+ i += 1;
170
+ continue;
171
+ }
172
+ filteredArgs.push(arg);
173
+ }
174
+ if (!businessDomain)
175
+ businessDomain = resolveBusinessDomain();
176
+ try {
177
+ const token = await ensureValidToken();
178
+ const base = {
179
+ baseUrl: token.baseUrl,
180
+ accessToken: token.accessToken,
181
+ businessDomain,
182
+ };
183
+ if (action === "list") {
184
+ const [knId] = filteredArgs;
185
+ if (!knId) {
186
+ console.error("Usage: kweaver bkn action-log list <kn-id> [options]");
187
+ return 1;
188
+ }
189
+ const result = await actionLogsList({
190
+ ...base,
191
+ knId,
192
+ limit,
193
+ needTotal,
194
+ actionTypeId,
195
+ status,
196
+ triggerType,
197
+ searchAfter,
198
+ });
199
+ console.log(formatCallOutput(result, pretty));
200
+ return 0;
201
+ }
202
+ if (action === "get") {
203
+ const [knId, logId] = filteredArgs;
204
+ if (!knId || !logId) {
205
+ console.error("Usage: kweaver bkn action-log get <kn-id> <log-id> [options]");
206
+ return 1;
207
+ }
208
+ const result = await actionLogGet({ ...base, knId, logId });
209
+ console.log(formatCallOutput(result, pretty));
210
+ return 0;
211
+ }
212
+ if (action === "cancel") {
213
+ const [knId, logId] = filteredArgs;
214
+ if (!knId || !logId) {
215
+ console.error("Usage: kweaver bkn action-log cancel <kn-id> <log-id> [options]");
216
+ return 1;
217
+ }
218
+ const result = await actionLogCancel({ ...base, knId, logId });
219
+ console.log(formatCallOutput(result, pretty));
220
+ return 0;
221
+ }
222
+ console.error(`Unknown action-log action: ${action}. Use list, get, or cancel.`);
223
+ return 1;
224
+ }
225
+ catch (error) {
226
+ console.error(formatHttpError(error));
227
+ return 1;
228
+ }
229
+ }
230
+ // ── search ───────────────────────────────────────────────────────────────────
231
+ const KN_SEARCH_HELP = `kweaver bkn search <kn-id> <query> [--max-concepts <n>] [--mode <mode>] [--pretty] [-bd value]
232
+
233
+ Semantic search within a knowledge network via agent-retrieval API.
234
+ Returns matched concepts (object types, relation types, action types).
235
+
236
+ Options:
237
+ --max-concepts <n> Max concepts to return (default: 10)
238
+ --mode <mode> Search mode (default: keyword_vector_retrieval)
239
+ --pretty Pretty-print JSON output
240
+ -bd, --biz-domain Override x-business-domain`;
241
+ export function parseKnSearchArgs(args) {
242
+ let knId = "";
243
+ let query = "";
244
+ let maxConcepts = 10;
245
+ let mode = "keyword_vector_retrieval";
246
+ let pretty = false;
247
+ let businessDomain = process.env.KWEAVER_BUSINESS_DOMAIN ?? "";
248
+ const positional = [];
249
+ for (let i = 0; i < args.length; i++) {
250
+ const arg = args[i];
251
+ if (arg === "--max-concepts") {
252
+ maxConcepts = Number(args[++i]);
253
+ }
254
+ else if (arg === "--mode") {
255
+ mode = args[++i];
256
+ }
257
+ else if (arg === "--pretty") {
258
+ pretty = true;
259
+ }
260
+ else if (arg === "-bd" || arg === "--biz-domain") {
261
+ businessDomain = args[++i];
262
+ }
263
+ else if (arg === "--help" || arg === "-h") {
264
+ throw Object.assign(new Error("help"), { isHelp: true });
265
+ }
266
+ else if (arg.startsWith("-")) {
267
+ throw new Error(`Unknown flag: ${arg}`);
268
+ }
269
+ else {
270
+ positional.push(arg);
271
+ }
272
+ }
273
+ knId = positional[0] ?? "";
274
+ query = positional.slice(1).join(" ");
275
+ if (!knId || !query) {
276
+ throw new Error("Usage: kweaver bkn search <kn-id> <query> [options]");
277
+ }
278
+ if (!businessDomain)
279
+ businessDomain = resolveBusinessDomain();
280
+ return { knId, query, maxConcepts, mode, pretty, businessDomain };
281
+ }
282
+ export async function runKnSearchCommand(args) {
283
+ let options;
284
+ try {
285
+ options = parseKnSearchArgs(args);
286
+ }
287
+ catch (error) {
288
+ if (error instanceof Error && error.isHelp) {
289
+ console.log(KN_SEARCH_HELP);
290
+ return 0;
291
+ }
292
+ console.error(error instanceof Error ? error.message : String(error));
293
+ return 1;
294
+ }
295
+ try {
296
+ const token = await ensureValidToken();
297
+ const result = await semanticSearch({
298
+ baseUrl: token.baseUrl,
299
+ accessToken: token.accessToken,
300
+ knId: options.knId,
301
+ query: options.query,
302
+ businessDomain: options.businessDomain,
303
+ maxConcepts: options.maxConcepts,
304
+ mode: options.mode,
305
+ });
306
+ console.log(formatCallOutput(result, options.pretty));
307
+ return 0;
308
+ }
309
+ catch (error) {
310
+ console.error(formatHttpError(error));
311
+ return 1;
312
+ }
313
+ }
314
+ // ── relation-type-paths ───────────────────────────────────────────────────────
315
+ export async function runKnRelationTypePathsCommand(args) {
316
+ let parsed;
317
+ try {
318
+ parsed = parseOntologyQueryFlags(args);
319
+ }
320
+ catch (error) {
321
+ if (error instanceof Error && error.message === "help") {
322
+ console.log(`kweaver bkn relation-type-paths <kn-id> '<json>' [--pretty] [-bd value]
323
+
324
+ Query relation type paths between object types.`);
325
+ return 0;
326
+ }
327
+ throw error;
328
+ }
329
+ const [knId, body] = parsed.filteredArgs;
330
+ if (!knId || !body) {
331
+ console.log(`kweaver bkn relation-type-paths <kn-id> '<json>' [--pretty] [-bd value]
332
+
333
+ Query relation type paths between object types.`);
334
+ return knId && !body ? 1 : 0;
335
+ }
336
+ const token = await ensureValidToken();
337
+ const result = await queryRelationTypePaths({
338
+ baseUrl: token.baseUrl,
339
+ accessToken: token.accessToken,
340
+ knId,
341
+ body,
342
+ businessDomain: parsed.businessDomain,
343
+ });
344
+ console.log(formatCallOutput(result, parsed.pretty));
345
+ return 0;
346
+ }
347
+ // ── resources ─────────────────────────────────────────────────────────────────
348
+ export async function runKnResourcesCommand(args) {
349
+ let parsed;
350
+ try {
351
+ parsed = parseOntologyQueryFlags(args);
352
+ }
353
+ catch (error) {
354
+ if (error instanceof Error && error.message === "help") {
355
+ console.log(`kweaver bkn resources [--pretty] [-bd value]
356
+
357
+ List available resources.`);
358
+ return 0;
359
+ }
360
+ throw error;
361
+ }
362
+ const token = await ensureValidToken();
363
+ const result = await listBknResources({
364
+ baseUrl: token.baseUrl,
365
+ accessToken: token.accessToken,
366
+ businessDomain: parsed.businessDomain,
367
+ });
368
+ console.log(formatCallOutput(result, parsed.pretty));
369
+ return 0;
370
+ }
@@ -0,0 +1,135 @@
1
+ export interface KnObjectTypeQueryOptions {
2
+ knId: string;
3
+ otId: string;
4
+ body: string;
5
+ pretty: boolean;
6
+ businessDomain: string;
7
+ }
8
+ export declare function parseKnObjectTypeQueryArgs(args: string[]): KnObjectTypeQueryOptions;
9
+ export declare function normalizeAdpFieldType(raw: string | undefined): string;
10
+ /**
11
+ * Ensure each data_property has mapped_field (same-name mapping) for build engine compatibility.
12
+ */
13
+ export declare function ensureMappedFieldOnDataProperty(prop: Record<string, unknown>): Record<string, unknown>;
14
+ /** Result of parsing `object-type create`; may require async dataview GET to fill properties. */
15
+ export type ObjectTypeCreateParsed = {
16
+ mode: "complete";
17
+ knId: string;
18
+ body: string;
19
+ businessDomain: string;
20
+ branch: string;
21
+ pretty: boolean;
22
+ } | {
23
+ mode: "needsDataview";
24
+ knId: string;
25
+ dataviewId: string;
26
+ entry: Record<string, unknown>;
27
+ businessDomain: string;
28
+ branch: string;
29
+ pretty: boolean;
30
+ };
31
+ /** Parse object-type create args: --name --dataview-id --primary-key --display-key [--property '<json>' ...] */
32
+ export declare function parseObjectTypeCreateArgs(args: string[]): ObjectTypeCreateParsed;
33
+ /**
34
+ * Load dataview fields and build data_properties (with mapped_field). Used when no --property flags.
35
+ */
36
+ export declare function finalizeObjectTypeCreateFromDataview(options: {
37
+ baseUrl: string;
38
+ accessToken: string;
39
+ dataviewId: string;
40
+ entry: Record<string, unknown>;
41
+ businessDomain: string;
42
+ }): Promise<string>;
43
+ /** Fields merged via GET → modify → PUT (not raw body mode). */
44
+ export interface ObjectTypeMergeFields {
45
+ name?: string;
46
+ displayKey?: string;
47
+ addProperties: Record<string, unknown>[];
48
+ removeProperties: string[];
49
+ tags?: string[];
50
+ comment?: string;
51
+ icon?: string;
52
+ color?: string;
53
+ }
54
+ export type ObjectTypeUpdateParsed = {
55
+ mode: "body";
56
+ knId: string;
57
+ otId: string;
58
+ body: string;
59
+ businessDomain: string;
60
+ pretty: boolean;
61
+ } | {
62
+ mode: "merge";
63
+ knId: string;
64
+ otId: string;
65
+ merge: ObjectTypeMergeFields;
66
+ businessDomain: string;
67
+ pretty: boolean;
68
+ branch: string;
69
+ };
70
+ /** Prepare a GET response entry for PUT (drop read-only fields). */
71
+ export declare function stripObjectTypeForPut(entry: Record<string, unknown>): Record<string, unknown>;
72
+ /**
73
+ * Apply merge flags onto a stripped object-type object (mutates copy).
74
+ * - Add: property `name` not in list → append.
75
+ * - Update: property `name` exists → replace entry (same as add; CLI also accepts `--update-property`).
76
+ * - Delete: `--remove-property` removes by `name` before adds are applied.
77
+ */
78
+ export declare function applyObjectTypeMerge(target: Record<string, unknown>, merge: ObjectTypeMergeFields): Record<string, unknown>;
79
+ /** Parse object-type update: raw JSON body OR merge flags (GET-merge-PUT). */
80
+ export declare function parseObjectTypeUpdateArgs(args: string[]): ObjectTypeUpdateParsed;
81
+ /** Parse object-type delete args: <kn-id> <ot-ids> [-y] */
82
+ export declare function parseObjectTypeDeleteArgs(args: string[]): {
83
+ knId: string;
84
+ otIds: string;
85
+ businessDomain: string;
86
+ yes: boolean;
87
+ };
88
+ export interface KnActionTypeExecuteOptions {
89
+ knId: string;
90
+ atId: string;
91
+ body: string;
92
+ pretty: boolean;
93
+ businessDomain: string;
94
+ wait: boolean;
95
+ timeout: number;
96
+ }
97
+ export declare function parseKnActionTypeExecuteArgs(args: string[]): KnActionTypeExecuteOptions;
98
+ /** Parse relation-type create args: --name --source --target [--mapping src:tgt ...] */
99
+ export declare function parseRelationTypeCreateArgs(args: string[]): {
100
+ knId: string;
101
+ body: string;
102
+ businessDomain: string;
103
+ branch: string;
104
+ pretty: boolean;
105
+ };
106
+ /** Parse relation-type update args: --source and --target are required by the API */
107
+ export declare function parseRelationTypeUpdateArgs(args: string[]): {
108
+ knId: string;
109
+ rtId: string;
110
+ body: string;
111
+ businessDomain: string;
112
+ pretty: boolean;
113
+ };
114
+ /** Parse relation-type delete args: <kn-id> <rt-ids> [-y] */
115
+ export declare function parseRelationTypeDeleteArgs(args: string[]): {
116
+ knId: string;
117
+ rtIds: string;
118
+ businessDomain: string;
119
+ yes: boolean;
120
+ };
121
+ export declare function runKnObjectTypeCommand(args: string[]): Promise<number>;
122
+ export declare function runKnRelationTypeCommand(args: string[]): Promise<number>;
123
+ export declare function runKnActionTypeCommand(args: string[]): Promise<number>;
124
+ export interface ConceptGroupParsed {
125
+ action: string;
126
+ knId: string;
127
+ itemId: string;
128
+ body: string;
129
+ extra: string;
130
+ yes: boolean;
131
+ pretty: boolean;
132
+ businessDomain: string;
133
+ }
134
+ export declare function parseConceptGroupArgs(args: string[]): ConceptGroupParsed;
135
+ export declare function runKnConceptGroupCommand(args: string[]): Promise<number>;