@kweaver-ai/kweaver-sdk 0.4.1 → 0.4.4

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.
@@ -1,5 +1,6 @@
1
1
  import { ensureValidToken, formatHttpError } from "../auth/oauth.js";
2
2
  import { HttpError } from "../utils/http.js";
3
+ import { resolveBusinessDomain } from "../config/store.js";
3
4
  export function parseCallArgs(args) {
4
5
  const headers = new Headers();
5
6
  let method = "GET";
@@ -7,7 +8,7 @@ export function parseCallArgs(args) {
7
8
  let url;
8
9
  let pretty = true;
9
10
  let verbose = false;
10
- let businessDomain = "bd_public";
11
+ let businessDomain = "";
11
12
  for (let index = 0; index < args.length; index += 1) {
12
13
  const arg = args[index];
13
14
  if (arg === "-X" || arg === "--request") {
@@ -42,7 +43,7 @@ export function parseCallArgs(args) {
42
43
  pretty = true;
43
44
  continue;
44
45
  }
45
- if (arg === "--verbose") {
46
+ if (arg === "-v" || arg === "--verbose") {
46
47
  verbose = true;
47
48
  continue;
48
49
  }
@@ -68,6 +69,8 @@ export function parseCallArgs(args) {
68
69
  if (!url) {
69
70
  throw new Error("Missing request URL");
70
71
  }
72
+ if (!businessDomain)
73
+ businessDomain = resolveBusinessDomain();
71
74
  return { url, method, headers, body, pretty, verbose, businessDomain };
72
75
  }
73
76
  function injectAuthHeaders(headers, accessToken, businessDomain) {
@@ -113,6 +116,21 @@ export function formatVerboseRequest(invocation) {
113
116
  return lines;
114
117
  }
115
118
  export async function runCallCommand(args) {
119
+ if (args.length === 0 || args[0] === "--help" || args[0] === "-h") {
120
+ console.log(`kweaver call <url> [-X METHOD] [-H "Name: value"] [-d BODY] [--pretty] [--verbose] [-bd value]
121
+
122
+ Call an API with curl-style flags and auto-injected token headers.
123
+
124
+ Options:
125
+ <url> API path (e.g. /api/ontology-manager/v1/knowledge-networks)
126
+ -X, --request HTTP method (default: GET)
127
+ -H, --header Extra header (repeatable)
128
+ -d, --data JSON request body
129
+ -bd, --biz-domain Override x-business-domain (default: bd_public)
130
+ -v, --verbose Print request info to stderr
131
+ --pretty Pretty-print JSON output (default)`);
132
+ return 0;
133
+ }
116
134
  let invocation;
117
135
  try {
118
136
  invocation = parseCallArgs(args);
@@ -121,17 +139,22 @@ export async function runCallCommand(args) {
121
139
  console.error(formatHttpError(error));
122
140
  return 1;
123
141
  }
124
- try {
142
+ const execute = async () => {
125
143
  const token = await ensureValidToken();
126
- injectAuthHeaders(invocation.headers, token.accessToken, invocation.businessDomain);
144
+ // Prepend baseUrl when the URL is a relative path (no scheme)
145
+ const url = invocation.url.startsWith("/")
146
+ ? token.baseUrl.replace(/\/+$/, "") + invocation.url
147
+ : invocation.url;
148
+ const headers = new Headers(invocation.headers);
149
+ injectAuthHeaders(headers, token.accessToken, invocation.businessDomain);
127
150
  if (invocation.verbose) {
128
- for (const line of formatVerboseRequest(invocation)) {
151
+ for (const line of formatVerboseRequest({ ...invocation, url, headers })) {
129
152
  console.error(line);
130
153
  }
131
154
  }
132
- const response = await fetch(invocation.url, {
155
+ const response = await fetch(url, {
133
156
  method: invocation.method,
134
- headers: invocation.headers,
157
+ headers,
135
158
  body: invocation.body,
136
159
  });
137
160
  const rawText = await response.text();
@@ -143,8 +166,21 @@ export async function runCallCommand(args) {
143
166
  console.log(formatCallOutput(text, invocation.pretty));
144
167
  }
145
168
  return 0;
169
+ };
170
+ try {
171
+ return await execute();
146
172
  }
147
173
  catch (error) {
174
+ if (error instanceof HttpError && error.status === 401) {
175
+ try {
176
+ await ensureValidToken({ forceRefresh: true });
177
+ return await execute();
178
+ }
179
+ catch (retryError) {
180
+ console.error(formatHttpError(retryError));
181
+ return 1;
182
+ }
183
+ }
148
184
  console.error(formatHttpError(error));
149
185
  return 1;
150
186
  }
@@ -0,0 +1 @@
1
+ export declare function runConfigCommand(args: string[]): Promise<number>;
@@ -0,0 +1,52 @@
1
+ import { getCurrentPlatform, resolveBusinessDomain, savePlatformBusinessDomain, loadPlatformBusinessDomain, } from "../config/store.js";
2
+ const HELP = `kweaver config
3
+
4
+ Subcommands:
5
+ set-bd <value> Set the default business domain for the current platform
6
+ show Show current config (platform, business domain)
7
+ --help Show this message
8
+
9
+ Examples:
10
+ kweaver config set-bd 54308785-4438-43df-9490-a7fd11df5765
11
+ kweaver config show`;
12
+ export async function runConfigCommand(args) {
13
+ const [sub, ...rest] = args;
14
+ if (!sub || sub === "--help" || sub === "-h" || sub === "help") {
15
+ console.log(HELP);
16
+ return 0;
17
+ }
18
+ if (sub === "show") {
19
+ const platform = getCurrentPlatform();
20
+ if (!platform) {
21
+ console.error("No active platform. Run `kweaver auth login <url>` first.");
22
+ return 1;
23
+ }
24
+ const bd = resolveBusinessDomain(platform);
25
+ const source = process.env.KWEAVER_BUSINESS_DOMAIN
26
+ ? "env"
27
+ : loadPlatformBusinessDomain(platform)
28
+ ? "config"
29
+ : "default";
30
+ console.log(`Platform: ${platform}`);
31
+ console.log(`Business Domain: ${bd} (${source})`);
32
+ return 0;
33
+ }
34
+ if (sub === "set-bd") {
35
+ const value = rest[0];
36
+ if (!value || value.startsWith("-")) {
37
+ console.error("Usage: kweaver config set-bd <value>");
38
+ return 1;
39
+ }
40
+ const platform = getCurrentPlatform();
41
+ if (!platform) {
42
+ console.error("No active platform. Run `kweaver auth login <url>` first.");
43
+ return 1;
44
+ }
45
+ savePlatformBusinessDomain(platform, value);
46
+ console.log(`Business domain set to: ${value}`);
47
+ return 0;
48
+ }
49
+ console.error(`Unknown config subcommand: ${sub}`);
50
+ console.log(HELP);
51
+ return 1;
52
+ }
@@ -1,4 +1,5 @@
1
1
  import { ensureValidToken, formatHttpError } from "../auth/oauth.js";
2
+ import { HttpError } from "../utils/http.js";
2
3
  import { knSearch, knSchemaSearch, queryObjectInstance, queryInstanceSubgraph, getLogicPropertiesValues, getActionInfo, listTools, listResources, readResource, listResourceTemplates, listPrompts, getPrompt, } from "../api/context-loader.js";
3
4
  import { addContextLoaderEntry, getCurrentContextLoaderKn, getCurrentPlatform, loadContextLoaderConfig, removeContextLoaderEntry, setCurrentContextLoader, } from "../config/store.js";
4
5
  const MCP_NOT_CONFIGURED = "Context-loader MCP is not configured. Run: kweaver context-loader config set --kn-id <kn-id>";
@@ -54,59 +55,64 @@ Examples:
54
55
  if (subcommand === "config") {
55
56
  return runConfigCommand(rest);
56
57
  }
57
- const token = await ensureValidToken();
58
- const base = ensureContextLoaderConfig();
59
- const options = { ...base, accessToken: token.accessToken };
60
58
  let pretty = true;
61
59
  const prettyIdx = rest.indexOf("--pretty");
62
60
  if (prettyIdx !== -1) {
63
61
  pretty = true;
64
62
  rest.splice(prettyIdx, 1);
65
63
  }
64
+ const dispatch = async () => {
65
+ const token = await ensureValidToken();
66
+ const base = ensureContextLoaderConfig();
67
+ const options = { ...base, accessToken: token.accessToken };
68
+ if (subcommand === "tools")
69
+ return runListTools(options, rest, pretty);
70
+ if (subcommand === "resources")
71
+ return runListResources(options, rest, pretty);
72
+ if (subcommand === "resource")
73
+ return runReadResource(options, rest, pretty);
74
+ if (subcommand === "templates")
75
+ return runListTemplates(options, rest, pretty);
76
+ if (subcommand === "prompts")
77
+ return runListPrompts(options, rest, pretty);
78
+ if (subcommand === "prompt")
79
+ return runGetPrompt(options, rest, pretty);
80
+ if (subcommand === "kn-search")
81
+ return runKnSearch(options, rest, pretty);
82
+ if (subcommand === "kn-schema-search")
83
+ return runKnSchemaSearch(options, rest, pretty);
84
+ if (subcommand === "query-object-instance")
85
+ return runQueryObjectInstance(options, rest, pretty);
86
+ if (subcommand === "query-instance-subgraph")
87
+ return runQueryInstanceSubgraph(options, rest, pretty);
88
+ if (subcommand === "get-logic-properties")
89
+ return runGetLogicProperties(options, rest, pretty);
90
+ if (subcommand === "get-action-info")
91
+ return runGetActionInfo(options, rest, pretty);
92
+ return -1;
93
+ };
66
94
  try {
67
- if (subcommand === "tools") {
68
- return await runListTools(options, rest, pretty);
69
- }
70
- if (subcommand === "resources") {
71
- return await runListResources(options, rest, pretty);
72
- }
73
- if (subcommand === "resource") {
74
- return await runReadResource(options, rest, pretty);
75
- }
76
- if (subcommand === "templates") {
77
- return await runListTemplates(options, rest, pretty);
78
- }
79
- if (subcommand === "prompts") {
80
- return await runListPrompts(options, rest, pretty);
81
- }
82
- if (subcommand === "prompt") {
83
- return await runGetPrompt(options, rest, pretty);
84
- }
85
- if (subcommand === "kn-search") {
86
- return await runKnSearch(options, rest, pretty);
87
- }
88
- if (subcommand === "kn-schema-search") {
89
- return await runKnSchemaSearch(options, rest, pretty);
90
- }
91
- if (subcommand === "query-object-instance") {
92
- return await runQueryObjectInstance(options, rest, pretty);
93
- }
94
- if (subcommand === "query-instance-subgraph") {
95
- return await runQueryInstanceSubgraph(options, rest, pretty);
96
- }
97
- if (subcommand === "get-logic-properties") {
98
- return await runGetLogicProperties(options, rest, pretty);
99
- }
100
- if (subcommand === "get-action-info") {
101
- return await runGetActionInfo(options, rest, pretty);
95
+ const code = await dispatch();
96
+ if (code === -1) {
97
+ console.error(`Unknown context-loader subcommand: ${subcommand}`);
98
+ return 1;
102
99
  }
100
+ return code;
103
101
  }
104
102
  catch (error) {
103
+ if (error instanceof HttpError && error.status === 401) {
104
+ try {
105
+ await ensureValidToken({ forceRefresh: true });
106
+ return await dispatch();
107
+ }
108
+ catch (retryError) {
109
+ console.error(formatHttpError(retryError));
110
+ return 1;
111
+ }
112
+ }
105
113
  console.error(formatHttpError(error));
106
114
  return 1;
107
115
  }
108
- console.error(`Unknown context-loader subcommand: ${subcommand}`);
109
- return 1;
110
116
  }
111
117
  async function runConfigCommand(args) {
112
118
  const [action, ...rest] = args;
@@ -1,7 +1,9 @@
1
1
  import { createInterface } from "node:readline";
2
2
  import { ensureValidToken, formatHttpError } from "../auth/oauth.js";
3
+ import { HttpError } from "../utils/http.js";
3
4
  import { testDatasource, createDatasource, listDatasources, getDatasource, deleteDatasource, listTablesWithColumns, } from "../api/datasources.js";
4
5
  import { formatCallOutput } from "./call.js";
6
+ import { resolveBusinessDomain } from "../config/store.js";
5
7
  function confirmYes(prompt) {
6
8
  return new Promise((resolve) => {
7
9
  const rl = createInterface({ input: process.stdin, output: process.stdout });
@@ -34,26 +36,38 @@ Subcommands:
34
36
  Test connectivity, register datasource, and discover tables.`);
35
37
  return 0;
36
38
  }
37
- try {
38
- if (subcommand === "list") {
39
+ const dispatch = () => {
40
+ if (subcommand === "list")
39
41
  return runDsListCommand(rest);
40
- }
41
- if (subcommand === "get") {
42
+ if (subcommand === "get")
42
43
  return runDsGetCommand(rest);
43
- }
44
- if (subcommand === "delete") {
44
+ if (subcommand === "delete")
45
45
  return runDsDeleteCommand(rest);
46
- }
47
- if (subcommand === "tables") {
46
+ if (subcommand === "tables")
48
47
  return runDsTablesCommand(rest);
49
- }
50
- if (subcommand === "connect") {
48
+ if (subcommand === "connect")
51
49
  return runDsConnectCommand(rest);
50
+ return Promise.resolve(-1);
51
+ };
52
+ try {
53
+ const code = await dispatch();
54
+ if (code === -1) {
55
+ console.error(`Unknown ds subcommand: ${subcommand}`);
56
+ return 1;
52
57
  }
53
- console.error(`Unknown ds subcommand: ${subcommand}`);
54
- return 1;
58
+ return code;
55
59
  }
56
60
  catch (error) {
61
+ if (error instanceof HttpError && error.status === 401) {
62
+ try {
63
+ await ensureValidToken({ forceRefresh: true });
64
+ return await dispatch();
65
+ }
66
+ catch (retryError) {
67
+ console.error(formatHttpError(retryError));
68
+ return 1;
69
+ }
70
+ }
57
71
  console.error(formatHttpError(error));
58
72
  return 1;
59
73
  }
@@ -61,7 +75,7 @@ Subcommands:
61
75
  export function parseDsListArgs(args) {
62
76
  let keyword;
63
77
  let type;
64
- let businessDomain = "bd_public";
78
+ let businessDomain = "";
65
79
  let pretty = true;
66
80
  for (let i = 0; i < args.length; i += 1) {
67
81
  const arg = args[i];
@@ -84,6 +98,8 @@ export function parseDsListArgs(args) {
84
98
  continue;
85
99
  }
86
100
  }
101
+ if (!businessDomain)
102
+ businessDomain = resolveBusinessDomain();
87
103
  return { keyword, type, businessDomain, pretty };
88
104
  }
89
105
  async function runDsListCommand(args) {
@@ -0,0 +1 @@
1
+ export declare function runVegaCommand(args: string[]): Promise<number>;