@kweaver-ai/kweaver-sdk 0.4.0 → 0.4.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.
- package/README.md +139 -0
- package/README.zh.md +139 -0
- package/dist/api/agent-list.d.ts +51 -0
- package/dist/api/agent-list.js +116 -7
- package/dist/api/bkn-backend.d.ts +16 -0
- package/dist/api/bkn-backend.js +46 -0
- package/dist/api/datasources.d.ts +73 -0
- package/dist/api/datasources.js +218 -0
- package/dist/api/dataviews.d.ts +20 -0
- package/dist/api/dataviews.js +72 -0
- package/dist/api/knowledge-networks.d.ts +84 -0
- package/dist/api/knowledge-networks.js +167 -0
- package/dist/api/ontology-query.d.ts +1 -1
- package/dist/api/ontology-query.js +1 -0
- package/dist/api/vega.d.ts +110 -0
- package/dist/api/vega.js +251 -0
- package/dist/auth/oauth.d.ts +3 -1
- package/dist/auth/oauth.js +12 -9
- package/dist/cli.js +15 -0
- package/dist/client.d.ts +19 -0
- package/dist/client.js +76 -13
- package/dist/commands/agent.d.ts +7 -0
- package/dist/commands/agent.js +392 -13
- package/dist/commands/bkn.d.ts +22 -0
- package/dist/commands/bkn.js +1057 -41
- package/dist/commands/call.js +20 -1
- package/dist/commands/context-loader.js +4 -4
- package/dist/commands/ds.d.ts +7 -0
- package/dist/commands/ds.js +283 -0
- package/dist/commands/vega.d.ts +1 -0
- package/dist/commands/vega.js +663 -0
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1 -1
- package/dist/resources/agents.d.ts +83 -9
- package/dist/resources/agents.js +46 -10
- package/dist/resources/bkn.d.ts +12 -0
- package/dist/resources/bkn.js +12 -0
- package/dist/resources/knowledge-networks.js +19 -58
- package/dist/utils/crypto.d.ts +10 -0
- package/dist/utils/crypto.js +31 -0
- package/package.json +4 -2
package/dist/commands/call.js
CHANGED
|
@@ -42,7 +42,7 @@ export function parseCallArgs(args) {
|
|
|
42
42
|
pretty = true;
|
|
43
43
|
continue;
|
|
44
44
|
}
|
|
45
|
-
if (arg === "--verbose") {
|
|
45
|
+
if (arg === "-v" || arg === "--verbose") {
|
|
46
46
|
verbose = true;
|
|
47
47
|
continue;
|
|
48
48
|
}
|
|
@@ -113,6 +113,21 @@ export function formatVerboseRequest(invocation) {
|
|
|
113
113
|
return lines;
|
|
114
114
|
}
|
|
115
115
|
export async function runCallCommand(args) {
|
|
116
|
+
if (args.length === 0 || args[0] === "--help" || args[0] === "-h") {
|
|
117
|
+
console.log(`kweaver call <url> [-X METHOD] [-H "Name: value"] [-d BODY] [--pretty] [--verbose] [-bd value]
|
|
118
|
+
|
|
119
|
+
Call an API with curl-style flags and auto-injected token headers.
|
|
120
|
+
|
|
121
|
+
Options:
|
|
122
|
+
<url> API path (e.g. /api/ontology-manager/v1/knowledge-networks)
|
|
123
|
+
-X, --request HTTP method (default: GET)
|
|
124
|
+
-H, --header Extra header (repeatable)
|
|
125
|
+
-d, --data JSON request body
|
|
126
|
+
-bd, --biz-domain Override x-business-domain (default: bd_public)
|
|
127
|
+
-v, --verbose Print request info to stderr
|
|
128
|
+
--pretty Pretty-print JSON output (default)`);
|
|
129
|
+
return 0;
|
|
130
|
+
}
|
|
116
131
|
let invocation;
|
|
117
132
|
try {
|
|
118
133
|
invocation = parseCallArgs(args);
|
|
@@ -123,6 +138,10 @@ export async function runCallCommand(args) {
|
|
|
123
138
|
}
|
|
124
139
|
try {
|
|
125
140
|
const token = await ensureValidToken();
|
|
141
|
+
// Prepend baseUrl when the URL is a relative path (no scheme)
|
|
142
|
+
if (invocation.url.startsWith("/")) {
|
|
143
|
+
invocation.url = token.baseUrl.replace(/\/+$/, "") + invocation.url;
|
|
144
|
+
}
|
|
126
145
|
injectAuthHeaders(invocation.headers, token.accessToken, invocation.businessDomain);
|
|
127
146
|
if (invocation.verbose) {
|
|
128
147
|
for (const line of formatVerboseRequest(invocation)) {
|
|
@@ -340,7 +340,7 @@ function parseJsonArg(args) {
|
|
|
340
340
|
async function runQueryObjectInstance(options, args, pretty) {
|
|
341
341
|
const body = parseJsonArg(args);
|
|
342
342
|
if (!body.ot_id || !body.condition) {
|
|
343
|
-
console.error("JSON must include ot_id and condition. See
|
|
343
|
+
console.error("JSON must include ot_id and condition. See references/json-formats.md#context-loader");
|
|
344
344
|
return 1;
|
|
345
345
|
}
|
|
346
346
|
const result = await queryObjectInstance(options, {
|
|
@@ -354,7 +354,7 @@ async function runQueryObjectInstance(options, args, pretty) {
|
|
|
354
354
|
async function runQueryInstanceSubgraph(options, args, pretty) {
|
|
355
355
|
const body = parseJsonArg(args);
|
|
356
356
|
if (!Array.isArray(body.relation_type_paths)) {
|
|
357
|
-
console.error("JSON must include relation_type_paths array. See
|
|
357
|
+
console.error("JSON must include relation_type_paths array. See references/json-formats.md#context-loader");
|
|
358
358
|
return 1;
|
|
359
359
|
}
|
|
360
360
|
const result = await queryInstanceSubgraph(options, body);
|
|
@@ -364,7 +364,7 @@ async function runQueryInstanceSubgraph(options, args, pretty) {
|
|
|
364
364
|
async function runGetLogicProperties(options, args, pretty) {
|
|
365
365
|
const body = parseJsonArg(args);
|
|
366
366
|
if (!body.ot_id || !body.query || !body._instance_identities || !body.properties) {
|
|
367
|
-
console.error("JSON must include ot_id, query, _instance_identities, properties. See
|
|
367
|
+
console.error("JSON must include ot_id, query, _instance_identities, properties. See references/json-formats.md#context-loader");
|
|
368
368
|
return 1;
|
|
369
369
|
}
|
|
370
370
|
const result = await getLogicPropertiesValues(options, body);
|
|
@@ -374,7 +374,7 @@ async function runGetLogicProperties(options, args, pretty) {
|
|
|
374
374
|
async function runGetActionInfo(options, args, pretty) {
|
|
375
375
|
const body = parseJsonArg(args);
|
|
376
376
|
if (!body.at_id || !body._instance_identity) {
|
|
377
|
-
console.error("JSON must include at_id and _instance_identity. See
|
|
377
|
+
console.error("JSON must include at_id and _instance_identity. See references/json-formats.md#context-loader");
|
|
378
378
|
return 1;
|
|
379
379
|
}
|
|
380
380
|
const result = await getActionInfo(options, body);
|
|
@@ -0,0 +1,283 @@
|
|
|
1
|
+
import { createInterface } from "node:readline";
|
|
2
|
+
import { ensureValidToken, formatHttpError } from "../auth/oauth.js";
|
|
3
|
+
import { testDatasource, createDatasource, listDatasources, getDatasource, deleteDatasource, listTablesWithColumns, } from "../api/datasources.js";
|
|
4
|
+
import { formatCallOutput } from "./call.js";
|
|
5
|
+
function confirmYes(prompt) {
|
|
6
|
+
return new Promise((resolve) => {
|
|
7
|
+
const rl = createInterface({ input: process.stdin, output: process.stdout });
|
|
8
|
+
rl.question(`${prompt} [y/N] `, (answer) => {
|
|
9
|
+
rl.close();
|
|
10
|
+
const trimmed = answer.trim().toLowerCase();
|
|
11
|
+
resolve(trimmed === "y" || trimmed === "yes");
|
|
12
|
+
});
|
|
13
|
+
});
|
|
14
|
+
}
|
|
15
|
+
function extractDatasourceId(body) {
|
|
16
|
+
const parsed = JSON.parse(body);
|
|
17
|
+
const item = Array.isArray(parsed) ? parsed[0] : parsed;
|
|
18
|
+
if (!item || typeof item !== "object")
|
|
19
|
+
return "";
|
|
20
|
+
const id = item.id ?? item.ds_id;
|
|
21
|
+
return id != null ? String(id) : "";
|
|
22
|
+
}
|
|
23
|
+
export async function runDsCommand(args) {
|
|
24
|
+
const [subcommand, ...rest] = args;
|
|
25
|
+
if (!subcommand || subcommand === "--help" || subcommand === "-h") {
|
|
26
|
+
console.log(`kweaver ds
|
|
27
|
+
|
|
28
|
+
Subcommands:
|
|
29
|
+
list [--keyword X] [--type Y] List datasources
|
|
30
|
+
get <id> Get datasource details
|
|
31
|
+
delete <id> [-y] Delete a datasource
|
|
32
|
+
tables <id> [--keyword X] List tables with columns
|
|
33
|
+
connect <db_type> <host> <port> <database> --account X --password Y [--schema Z] [--name N]
|
|
34
|
+
Test connectivity, register datasource, and discover tables.`);
|
|
35
|
+
return 0;
|
|
36
|
+
}
|
|
37
|
+
try {
|
|
38
|
+
if (subcommand === "list") {
|
|
39
|
+
return runDsListCommand(rest);
|
|
40
|
+
}
|
|
41
|
+
if (subcommand === "get") {
|
|
42
|
+
return runDsGetCommand(rest);
|
|
43
|
+
}
|
|
44
|
+
if (subcommand === "delete") {
|
|
45
|
+
return runDsDeleteCommand(rest);
|
|
46
|
+
}
|
|
47
|
+
if (subcommand === "tables") {
|
|
48
|
+
return runDsTablesCommand(rest);
|
|
49
|
+
}
|
|
50
|
+
if (subcommand === "connect") {
|
|
51
|
+
return runDsConnectCommand(rest);
|
|
52
|
+
}
|
|
53
|
+
console.error(`Unknown ds subcommand: ${subcommand}`);
|
|
54
|
+
return 1;
|
|
55
|
+
}
|
|
56
|
+
catch (error) {
|
|
57
|
+
console.error(formatHttpError(error));
|
|
58
|
+
return 1;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
export function parseDsListArgs(args) {
|
|
62
|
+
let keyword;
|
|
63
|
+
let type;
|
|
64
|
+
let businessDomain = "bd_public";
|
|
65
|
+
let pretty = true;
|
|
66
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
67
|
+
const arg = args[i];
|
|
68
|
+
if (arg === "--help" || arg === "-h")
|
|
69
|
+
throw new Error("help");
|
|
70
|
+
if (arg === "--keyword" && args[i + 1]) {
|
|
71
|
+
keyword = args[++i];
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
if (arg === "--type" && args[i + 1]) {
|
|
75
|
+
type = args[++i];
|
|
76
|
+
continue;
|
|
77
|
+
}
|
|
78
|
+
if ((arg === "-bd" || arg === "--biz-domain") && args[i + 1]) {
|
|
79
|
+
businessDomain = args[++i];
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
if (arg === "--pretty") {
|
|
83
|
+
pretty = true;
|
|
84
|
+
continue;
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
return { keyword, type, businessDomain, pretty };
|
|
88
|
+
}
|
|
89
|
+
async function runDsListCommand(args) {
|
|
90
|
+
try {
|
|
91
|
+
const opts = parseDsListArgs(args);
|
|
92
|
+
const token = await ensureValidToken();
|
|
93
|
+
const body = await listDatasources({
|
|
94
|
+
baseUrl: token.baseUrl,
|
|
95
|
+
accessToken: token.accessToken,
|
|
96
|
+
keyword: opts.keyword,
|
|
97
|
+
type: opts.type,
|
|
98
|
+
businessDomain: opts.businessDomain,
|
|
99
|
+
});
|
|
100
|
+
console.log(formatCallOutput(body, opts.pretty));
|
|
101
|
+
return 0;
|
|
102
|
+
}
|
|
103
|
+
catch (error) {
|
|
104
|
+
if (error instanceof Error && error.message === "help") {
|
|
105
|
+
console.log(`kweaver ds list [options]
|
|
106
|
+
|
|
107
|
+
Options:
|
|
108
|
+
--keyword <s> Filter by keyword
|
|
109
|
+
--type <s> Filter by database type
|
|
110
|
+
-bd, --biz-domain Business domain (default: bd_public)
|
|
111
|
+
--pretty Pretty-print JSON (default)`);
|
|
112
|
+
return 0;
|
|
113
|
+
}
|
|
114
|
+
throw error;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
async function runDsGetCommand(args) {
|
|
118
|
+
const id = args.find((a) => !a.startsWith("-"));
|
|
119
|
+
if (!id) {
|
|
120
|
+
console.error("Usage: kweaver ds get <id>");
|
|
121
|
+
return 1;
|
|
122
|
+
}
|
|
123
|
+
const token = await ensureValidToken();
|
|
124
|
+
const body = await getDatasource({
|
|
125
|
+
baseUrl: token.baseUrl,
|
|
126
|
+
accessToken: token.accessToken,
|
|
127
|
+
id,
|
|
128
|
+
});
|
|
129
|
+
console.log(formatCallOutput(body, true));
|
|
130
|
+
return 0;
|
|
131
|
+
}
|
|
132
|
+
async function runDsDeleteCommand(args) {
|
|
133
|
+
let id = "";
|
|
134
|
+
let yes = false;
|
|
135
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
136
|
+
const arg = args[i];
|
|
137
|
+
if (arg === "--yes" || arg === "-y")
|
|
138
|
+
yes = true;
|
|
139
|
+
else if (!arg.startsWith("-"))
|
|
140
|
+
id = arg;
|
|
141
|
+
}
|
|
142
|
+
if (!id) {
|
|
143
|
+
console.error("Usage: kweaver ds delete <id> [-y]");
|
|
144
|
+
return 1;
|
|
145
|
+
}
|
|
146
|
+
if (!yes) {
|
|
147
|
+
const confirmed = await confirmYes("Are you sure you want to delete this datasource?");
|
|
148
|
+
if (!confirmed) {
|
|
149
|
+
console.error("Aborted.");
|
|
150
|
+
return 1;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
const token = await ensureValidToken();
|
|
154
|
+
await deleteDatasource({
|
|
155
|
+
baseUrl: token.baseUrl,
|
|
156
|
+
accessToken: token.accessToken,
|
|
157
|
+
id,
|
|
158
|
+
});
|
|
159
|
+
console.error(`Deleted ${id}`);
|
|
160
|
+
return 0;
|
|
161
|
+
}
|
|
162
|
+
async function runDsTablesCommand(args) {
|
|
163
|
+
let id = "";
|
|
164
|
+
let keyword;
|
|
165
|
+
let pretty = true;
|
|
166
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
167
|
+
const arg = args[i];
|
|
168
|
+
if (arg === "--keyword" && args[i + 1]) {
|
|
169
|
+
keyword = args[++i];
|
|
170
|
+
continue;
|
|
171
|
+
}
|
|
172
|
+
if (arg === "--pretty") {
|
|
173
|
+
pretty = true;
|
|
174
|
+
continue;
|
|
175
|
+
}
|
|
176
|
+
if (!arg.startsWith("-"))
|
|
177
|
+
id = arg;
|
|
178
|
+
}
|
|
179
|
+
if (!id) {
|
|
180
|
+
console.error("Usage: kweaver ds tables <id> [--keyword X]");
|
|
181
|
+
return 1;
|
|
182
|
+
}
|
|
183
|
+
const token = await ensureValidToken();
|
|
184
|
+
const body = await listTablesWithColumns({
|
|
185
|
+
baseUrl: token.baseUrl,
|
|
186
|
+
accessToken: token.accessToken,
|
|
187
|
+
id,
|
|
188
|
+
keyword,
|
|
189
|
+
});
|
|
190
|
+
console.log(formatCallOutput(body, pretty));
|
|
191
|
+
return 0;
|
|
192
|
+
}
|
|
193
|
+
async function runDsConnectCommand(args) {
|
|
194
|
+
let dbType = "";
|
|
195
|
+
let host = "";
|
|
196
|
+
let port = 0;
|
|
197
|
+
let database = "";
|
|
198
|
+
let account = "";
|
|
199
|
+
let password = "";
|
|
200
|
+
let schema;
|
|
201
|
+
let name;
|
|
202
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
203
|
+
const arg = args[i];
|
|
204
|
+
if (arg === "--account" && args[i + 1]) {
|
|
205
|
+
account = args[++i];
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (arg === "--password" && args[i + 1]) {
|
|
209
|
+
password = args[++i];
|
|
210
|
+
continue;
|
|
211
|
+
}
|
|
212
|
+
if (arg === "--schema" && args[i + 1]) {
|
|
213
|
+
schema = args[++i];
|
|
214
|
+
continue;
|
|
215
|
+
}
|
|
216
|
+
if (arg === "--name" && args[i + 1]) {
|
|
217
|
+
name = args[++i];
|
|
218
|
+
continue;
|
|
219
|
+
}
|
|
220
|
+
if (!arg.startsWith("-")) {
|
|
221
|
+
if (!dbType)
|
|
222
|
+
dbType = arg;
|
|
223
|
+
else if (!host)
|
|
224
|
+
host = arg;
|
|
225
|
+
else if (port === 0)
|
|
226
|
+
port = parseInt(arg, 10);
|
|
227
|
+
else if (!database)
|
|
228
|
+
database = arg;
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
if (!dbType || !host || !database || !account || !password) {
|
|
232
|
+
console.error("Usage: kweaver ds connect <db_type> <host> <port> <database> --account X --password Y [--schema Z] [--name N]");
|
|
233
|
+
return 1;
|
|
234
|
+
}
|
|
235
|
+
if (Number.isNaN(port) || port < 1) {
|
|
236
|
+
console.error("Invalid port");
|
|
237
|
+
return 1;
|
|
238
|
+
}
|
|
239
|
+
const token = await ensureValidToken();
|
|
240
|
+
const base = { baseUrl: token.baseUrl, accessToken: token.accessToken };
|
|
241
|
+
console.error("Testing connectivity ...");
|
|
242
|
+
await testDatasource({
|
|
243
|
+
...base,
|
|
244
|
+
type: dbType,
|
|
245
|
+
host,
|
|
246
|
+
port,
|
|
247
|
+
database,
|
|
248
|
+
account,
|
|
249
|
+
password,
|
|
250
|
+
schema,
|
|
251
|
+
});
|
|
252
|
+
const dsName = name ?? database;
|
|
253
|
+
const createBody = await createDatasource({
|
|
254
|
+
...base,
|
|
255
|
+
name: dsName,
|
|
256
|
+
type: dbType,
|
|
257
|
+
host,
|
|
258
|
+
port,
|
|
259
|
+
database,
|
|
260
|
+
account,
|
|
261
|
+
password,
|
|
262
|
+
schema,
|
|
263
|
+
});
|
|
264
|
+
const dsId = extractDatasourceId(createBody);
|
|
265
|
+
if (!dsId) {
|
|
266
|
+
console.error("Failed to get datasource ID from create response");
|
|
267
|
+
return 1;
|
|
268
|
+
}
|
|
269
|
+
const tablesBody = await listTablesWithColumns({
|
|
270
|
+
...base,
|
|
271
|
+
id: dsId,
|
|
272
|
+
});
|
|
273
|
+
const tables = JSON.parse(tablesBody);
|
|
274
|
+
const output = {
|
|
275
|
+
datasource_id: dsId,
|
|
276
|
+
tables: tables.map((t) => ({
|
|
277
|
+
name: t.name,
|
|
278
|
+
columns: t.columns.map((c) => ({ name: c.name, type: c.type, comment: c.comment })),
|
|
279
|
+
})),
|
|
280
|
+
};
|
|
281
|
+
console.log(JSON.stringify(output, null, 2));
|
|
282
|
+
return 0;
|
|
283
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runVegaCommand(args: string[]): Promise<number>;
|