@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.
- package/bin/kweaver.js +9 -0
- package/dist/api/agent-chat.d.ts +69 -0
- package/dist/api/agent-chat.js +379 -0
- package/dist/api/agent-list.d.ts +12 -0
- package/dist/api/agent-list.js +33 -0
- package/dist/api/context-loader.d.ts +115 -0
- package/dist/api/context-loader.js +259 -0
- package/dist/api/conversations.d.ts +24 -0
- package/dist/api/conversations.js +64 -0
- package/dist/api/knowledge-networks.d.ts +57 -0
- package/dist/api/knowledge-networks.js +158 -0
- package/dist/api/ontology-query.d.ts +75 -0
- package/dist/api/ontology-query.js +238 -0
- package/dist/api/semantic-search.d.ts +12 -0
- package/dist/api/semantic-search.js +34 -0
- package/dist/auth/oauth.d.ts +75 -0
- package/dist/auth/oauth.js +417 -0
- package/dist/cli.d.ts +1 -0
- package/dist/cli.js +79 -0
- package/dist/client.d.ts +95 -0
- package/dist/client.js +104 -0
- package/dist/commands/agent-chat.d.ts +12 -0
- package/dist/commands/agent-chat.js +193 -0
- package/dist/commands/agent.d.ts +28 -0
- package/dist/commands/agent.js +431 -0
- package/dist/commands/auth.d.ts +9 -0
- package/dist/commands/auth.js +201 -0
- package/dist/commands/bkn.d.ts +70 -0
- package/dist/commands/bkn.js +1371 -0
- package/dist/commands/call.d.ts +14 -0
- package/dist/commands/call.js +151 -0
- package/dist/commands/context-loader.d.ts +1 -0
- package/dist/commands/context-loader.js +383 -0
- package/dist/commands/token.d.ts +2 -0
- package/dist/commands/token.js +24 -0
- package/dist/config/store.d.ts +77 -0
- package/dist/config/store.js +380 -0
- package/dist/index.d.ts +53 -0
- package/dist/index.js +44 -0
- package/dist/kweaver.d.ts +146 -0
- package/dist/kweaver.js +184 -0
- package/dist/resources/agents.d.ts +37 -0
- package/dist/resources/agents.js +60 -0
- package/dist/resources/bkn.d.ts +45 -0
- package/dist/resources/bkn.js +86 -0
- package/dist/resources/context-loader.d.ts +15 -0
- package/dist/resources/context-loader.js +32 -0
- package/dist/resources/conversations.d.ts +11 -0
- package/dist/resources/conversations.js +17 -0
- package/dist/resources/knowledge-networks.d.ts +65 -0
- package/dist/resources/knowledge-networks.js +167 -0
- package/dist/ui/ChatApp.d.ts +16 -0
- package/dist/ui/ChatApp.js +248 -0
- package/dist/ui/MarkdownBlock.d.ts +5 -0
- package/dist/ui/MarkdownBlock.js +137 -0
- package/dist/ui/display-text.d.ts +1 -0
- package/dist/ui/display-text.js +1 -0
- package/dist/utils/browser.d.ts +1 -0
- package/dist/utils/browser.js +20 -0
- package/dist/utils/display-text.d.ts +3 -0
- package/dist/utils/display-text.js +46 -0
- package/dist/utils/http.d.ts +17 -0
- package/dist/utils/http.js +72 -0
- package/package.json +62 -0
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface CallInvocation {
|
|
2
|
+
url: string;
|
|
3
|
+
method: string;
|
|
4
|
+
headers: Headers;
|
|
5
|
+
body?: string;
|
|
6
|
+
pretty: boolean;
|
|
7
|
+
verbose: boolean;
|
|
8
|
+
businessDomain: string;
|
|
9
|
+
}
|
|
10
|
+
export declare function parseCallArgs(args: string[]): CallInvocation;
|
|
11
|
+
export declare function formatCallOutput(text: string, pretty: boolean): string;
|
|
12
|
+
export declare function stripSseDoneMarker(text: string, contentType?: string | null): string;
|
|
13
|
+
export declare function formatVerboseRequest(invocation: CallInvocation): string[];
|
|
14
|
+
export declare function runCallCommand(args: string[]): Promise<number>;
|
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
import { ensureValidToken, formatHttpError } from "../auth/oauth.js";
|
|
2
|
+
import { HttpError } from "../utils/http.js";
|
|
3
|
+
export function parseCallArgs(args) {
|
|
4
|
+
const headers = new Headers();
|
|
5
|
+
let method = "GET";
|
|
6
|
+
let body;
|
|
7
|
+
let url;
|
|
8
|
+
let pretty = true;
|
|
9
|
+
let verbose = false;
|
|
10
|
+
let businessDomain = "bd_public";
|
|
11
|
+
for (let index = 0; index < args.length; index += 1) {
|
|
12
|
+
const arg = args[index];
|
|
13
|
+
if (arg === "-X" || arg === "--request") {
|
|
14
|
+
method = (args[index + 1] ?? "").toUpperCase();
|
|
15
|
+
index += 1;
|
|
16
|
+
continue;
|
|
17
|
+
}
|
|
18
|
+
if (arg === "-H" || arg === "--header") {
|
|
19
|
+
const header = args[index + 1];
|
|
20
|
+
if (!header) {
|
|
21
|
+
throw new Error("Missing value for header flag");
|
|
22
|
+
}
|
|
23
|
+
const separatorIndex = header.indexOf(":");
|
|
24
|
+
if (separatorIndex === -1) {
|
|
25
|
+
throw new Error(`Invalid header format: ${header}`);
|
|
26
|
+
}
|
|
27
|
+
const name = header.slice(0, separatorIndex).trim();
|
|
28
|
+
const value = header.slice(separatorIndex + 1).trim();
|
|
29
|
+
headers.set(name, value);
|
|
30
|
+
index += 1;
|
|
31
|
+
continue;
|
|
32
|
+
}
|
|
33
|
+
if (arg === "-d" || arg === "--data" || arg === "--data-raw") {
|
|
34
|
+
body = args[index + 1] ?? "";
|
|
35
|
+
if (method === "GET") {
|
|
36
|
+
method = "POST";
|
|
37
|
+
}
|
|
38
|
+
index += 1;
|
|
39
|
+
continue;
|
|
40
|
+
}
|
|
41
|
+
if (arg === "--pretty") {
|
|
42
|
+
pretty = true;
|
|
43
|
+
continue;
|
|
44
|
+
}
|
|
45
|
+
if (arg === "--verbose") {
|
|
46
|
+
verbose = true;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (arg === "-bd" || arg === "--biz-domain") {
|
|
50
|
+
businessDomain = args[index + 1] ?? "";
|
|
51
|
+
if (!businessDomain || businessDomain.startsWith("-")) {
|
|
52
|
+
throw new Error("Missing value for biz-domain flag");
|
|
53
|
+
}
|
|
54
|
+
index += 1;
|
|
55
|
+
continue;
|
|
56
|
+
}
|
|
57
|
+
if (arg === "--url") {
|
|
58
|
+
url = args[index + 1];
|
|
59
|
+
index += 1;
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
if (!arg.startsWith("-") && !url) {
|
|
63
|
+
url = arg;
|
|
64
|
+
continue;
|
|
65
|
+
}
|
|
66
|
+
throw new Error(`Unsupported call argument: ${arg}`);
|
|
67
|
+
}
|
|
68
|
+
if (!url) {
|
|
69
|
+
throw new Error("Missing request URL");
|
|
70
|
+
}
|
|
71
|
+
return { url, method, headers, body, pretty, verbose, businessDomain };
|
|
72
|
+
}
|
|
73
|
+
function injectAuthHeaders(headers, accessToken, businessDomain) {
|
|
74
|
+
if (!headers.has("authorization")) {
|
|
75
|
+
headers.set("authorization", `Bearer ${accessToken}`);
|
|
76
|
+
}
|
|
77
|
+
if (!headers.has("token")) {
|
|
78
|
+
headers.set("token", accessToken);
|
|
79
|
+
}
|
|
80
|
+
if (!headers.has("x-business-domain")) {
|
|
81
|
+
headers.set("x-business-domain", businessDomain);
|
|
82
|
+
}
|
|
83
|
+
}
|
|
84
|
+
export function formatCallOutput(text, pretty) {
|
|
85
|
+
if (!pretty || !text) {
|
|
86
|
+
return text;
|
|
87
|
+
}
|
|
88
|
+
try {
|
|
89
|
+
return JSON.stringify(JSON.parse(text), null, 2);
|
|
90
|
+
}
|
|
91
|
+
catch {
|
|
92
|
+
return text;
|
|
93
|
+
}
|
|
94
|
+
}
|
|
95
|
+
export function stripSseDoneMarker(text, contentType) {
|
|
96
|
+
if (!text || !contentType?.includes("text/event-stream")) {
|
|
97
|
+
return text;
|
|
98
|
+
}
|
|
99
|
+
const lines = text.split(/\r?\n/).filter((line) => line.trim() !== "data: [DONE]");
|
|
100
|
+
return lines.join("\n").trimEnd();
|
|
101
|
+
}
|
|
102
|
+
export function formatVerboseRequest(invocation) {
|
|
103
|
+
const lines = [
|
|
104
|
+
`Method: ${invocation.method}`,
|
|
105
|
+
`URL: ${invocation.url}`,
|
|
106
|
+
"Headers:",
|
|
107
|
+
];
|
|
108
|
+
const entries = Array.from(invocation.headers.entries()).sort(([a], [b]) => a.localeCompare(b));
|
|
109
|
+
for (const [name, value] of entries) {
|
|
110
|
+
lines.push(` ${name}: ${value}`);
|
|
111
|
+
}
|
|
112
|
+
lines.push(`Body: ${invocation.body ? "present" : "empty"}`);
|
|
113
|
+
return lines;
|
|
114
|
+
}
|
|
115
|
+
export async function runCallCommand(args) {
|
|
116
|
+
let invocation;
|
|
117
|
+
try {
|
|
118
|
+
invocation = parseCallArgs(args);
|
|
119
|
+
}
|
|
120
|
+
catch (error) {
|
|
121
|
+
console.error(formatHttpError(error));
|
|
122
|
+
return 1;
|
|
123
|
+
}
|
|
124
|
+
try {
|
|
125
|
+
const token = await ensureValidToken();
|
|
126
|
+
injectAuthHeaders(invocation.headers, token.accessToken, invocation.businessDomain);
|
|
127
|
+
if (invocation.verbose) {
|
|
128
|
+
for (const line of formatVerboseRequest(invocation)) {
|
|
129
|
+
console.error(line);
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
const response = await fetch(invocation.url, {
|
|
133
|
+
method: invocation.method,
|
|
134
|
+
headers: invocation.headers,
|
|
135
|
+
body: invocation.body,
|
|
136
|
+
});
|
|
137
|
+
const rawText = await response.text();
|
|
138
|
+
const text = stripSseDoneMarker(rawText, response.headers.get("content-type"));
|
|
139
|
+
if (!response.ok) {
|
|
140
|
+
throw new HttpError(response.status, response.statusText, text);
|
|
141
|
+
}
|
|
142
|
+
if (text) {
|
|
143
|
+
console.log(formatCallOutput(text, invocation.pretty));
|
|
144
|
+
}
|
|
145
|
+
return 0;
|
|
146
|
+
}
|
|
147
|
+
catch (error) {
|
|
148
|
+
console.error(formatHttpError(error));
|
|
149
|
+
return 1;
|
|
150
|
+
}
|
|
151
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare function runContextLoaderCommand(args: string[]): Promise<number>;
|
|
@@ -0,0 +1,383 @@
|
|
|
1
|
+
import { ensureValidToken, formatHttpError } from "../auth/oauth.js";
|
|
2
|
+
import { knSearch, knSchemaSearch, queryObjectInstance, queryInstanceSubgraph, getLogicPropertiesValues, getActionInfo, listTools, listResources, readResource, listResourceTemplates, listPrompts, getPrompt, } from "../api/context-loader.js";
|
|
3
|
+
import { addContextLoaderEntry, getCurrentContextLoaderKn, getCurrentPlatform, loadContextLoaderConfig, removeContextLoaderEntry, setCurrentContextLoader, } from "../config/store.js";
|
|
4
|
+
const MCP_NOT_CONFIGURED = "Context-loader MCP is not configured. Run: kweaver context-loader config set --kn-id <kn-id>";
|
|
5
|
+
function ensureContextLoaderConfig() {
|
|
6
|
+
const platform = getCurrentPlatform();
|
|
7
|
+
if (!platform) {
|
|
8
|
+
throw new Error("No platform selected. Run: kweaver auth <platform-url>");
|
|
9
|
+
}
|
|
10
|
+
const kn = getCurrentContextLoaderKn();
|
|
11
|
+
if (!kn) {
|
|
12
|
+
throw new Error(MCP_NOT_CONFIGURED);
|
|
13
|
+
}
|
|
14
|
+
return {
|
|
15
|
+
mcpUrl: kn.mcpUrl,
|
|
16
|
+
knId: kn.knId,
|
|
17
|
+
accessToken: "", // filled by caller after ensureValidToken
|
|
18
|
+
};
|
|
19
|
+
}
|
|
20
|
+
function formatOutput(value, pretty) {
|
|
21
|
+
const json = JSON.stringify(value, null, pretty ? 2 : 0);
|
|
22
|
+
return json;
|
|
23
|
+
}
|
|
24
|
+
export async function runContextLoaderCommand(args) {
|
|
25
|
+
const [subcommand, ...rest] = args;
|
|
26
|
+
if (!subcommand || subcommand === "--help" || subcommand === "-h") {
|
|
27
|
+
console.log(`kweaver context-loader
|
|
28
|
+
|
|
29
|
+
Subcommands:
|
|
30
|
+
config set --kn-id <id> [--name n] Add or update kn config (MCP URL derived from platform)
|
|
31
|
+
config use <name> Switch current config
|
|
32
|
+
config list List all configs and current
|
|
33
|
+
config remove <name> Remove a config
|
|
34
|
+
config show Show current config (knId + mcpUrl)
|
|
35
|
+
tools tools/list - list available tools
|
|
36
|
+
resources resources/list - list resources
|
|
37
|
+
resource <uri> resources/read - read resource by URI
|
|
38
|
+
templates resources/templates/list - list resource templates
|
|
39
|
+
prompts prompts/list - list prompts
|
|
40
|
+
prompt <name> [--args json] prompts/get - get prompt by name
|
|
41
|
+
kn-search <query> [--only-schema] Layer 1: Search schema (object_types, relation_types, action_types)
|
|
42
|
+
kn-schema-search <query> [--max N] Layer 1: Discover candidate concepts
|
|
43
|
+
query-object-instance <json> Layer 2: Query instances (args as JSON)
|
|
44
|
+
query-instance-subgraph <json> Layer 2: Query subgraph (args as JSON)
|
|
45
|
+
get-logic-properties <json> Layer 3: Get logic property values (args as JSON)
|
|
46
|
+
get-action-info <json> Layer 3: Get action info (args as JSON)
|
|
47
|
+
|
|
48
|
+
Examples:
|
|
49
|
+
kweaver context-loader config set --kn-id d5iv6c9818p72mpje8pg
|
|
50
|
+
kweaver context-loader config set --kn-id xyz123 --name project-a
|
|
51
|
+
kweaver context-loader kn-search "高血压 治疗 药品" --only-schema --pretty`);
|
|
52
|
+
return 0;
|
|
53
|
+
}
|
|
54
|
+
if (subcommand === "config") {
|
|
55
|
+
return runConfigCommand(rest);
|
|
56
|
+
}
|
|
57
|
+
const token = await ensureValidToken();
|
|
58
|
+
const base = ensureContextLoaderConfig();
|
|
59
|
+
const options = { ...base, accessToken: token.accessToken };
|
|
60
|
+
let pretty = true;
|
|
61
|
+
const prettyIdx = rest.indexOf("--pretty");
|
|
62
|
+
if (prettyIdx !== -1) {
|
|
63
|
+
pretty = true;
|
|
64
|
+
rest.splice(prettyIdx, 1);
|
|
65
|
+
}
|
|
66
|
+
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);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
catch (error) {
|
|
105
|
+
console.error(formatHttpError(error));
|
|
106
|
+
return 1;
|
|
107
|
+
}
|
|
108
|
+
console.error(`Unknown context-loader subcommand: ${subcommand}`);
|
|
109
|
+
return 1;
|
|
110
|
+
}
|
|
111
|
+
async function runConfigCommand(args) {
|
|
112
|
+
const [action, ...rest] = args;
|
|
113
|
+
if (!action || action === "--help" || action === "-h") {
|
|
114
|
+
console.log(`kweaver context-loader config
|
|
115
|
+
|
|
116
|
+
Subcommands:
|
|
117
|
+
set --kn-id <id> [--name <name>] Add or update kn config (default name: default)
|
|
118
|
+
use <name> Switch current config
|
|
119
|
+
list List all configs and current
|
|
120
|
+
remove <name> Remove a config
|
|
121
|
+
show Show current config (knId + mcpUrl)`);
|
|
122
|
+
return 0;
|
|
123
|
+
}
|
|
124
|
+
const platform = getCurrentPlatform();
|
|
125
|
+
if (!platform) {
|
|
126
|
+
console.error("No platform selected. Run: kweaver auth <platform-url>");
|
|
127
|
+
return 1;
|
|
128
|
+
}
|
|
129
|
+
if (action === "show") {
|
|
130
|
+
const kn = getCurrentContextLoaderKn();
|
|
131
|
+
if (!kn) {
|
|
132
|
+
console.log("Context-loader MCP is not configured.");
|
|
133
|
+
console.log(MCP_NOT_CONFIGURED);
|
|
134
|
+
return 0;
|
|
135
|
+
}
|
|
136
|
+
console.log(JSON.stringify({ mcpUrl: kn.mcpUrl, knId: kn.knId }, null, 2));
|
|
137
|
+
return 0;
|
|
138
|
+
}
|
|
139
|
+
if (action === "list") {
|
|
140
|
+
const config = loadContextLoaderConfig();
|
|
141
|
+
if (!config || config.configs.length === 0) {
|
|
142
|
+
console.log("Context-loader MCP is not configured.");
|
|
143
|
+
console.log(MCP_NOT_CONFIGURED);
|
|
144
|
+
return 0;
|
|
145
|
+
}
|
|
146
|
+
for (const entry of config.configs) {
|
|
147
|
+
const mark = entry.name === config.current ? " (current)" : "";
|
|
148
|
+
console.log(` ${entry.name}: ${entry.knId}${mark}`);
|
|
149
|
+
}
|
|
150
|
+
return 0;
|
|
151
|
+
}
|
|
152
|
+
if (action === "set") {
|
|
153
|
+
let knId;
|
|
154
|
+
let name = "default";
|
|
155
|
+
for (let i = 0; i < rest.length; i += 1) {
|
|
156
|
+
const arg = rest[i];
|
|
157
|
+
if ((arg === "--kn-id" || arg === "-k") && rest[i + 1]) {
|
|
158
|
+
knId = rest[i + 1];
|
|
159
|
+
i += 1;
|
|
160
|
+
}
|
|
161
|
+
else if ((arg === "--name" || arg === "-n") && rest[i + 1]) {
|
|
162
|
+
name = rest[i + 1];
|
|
163
|
+
i += 1;
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
if (!knId) {
|
|
167
|
+
console.error("Usage: kweaver context-loader config set --kn-id <id> [--name <name>]");
|
|
168
|
+
return 1;
|
|
169
|
+
}
|
|
170
|
+
addContextLoaderEntry(platform, name, knId);
|
|
171
|
+
console.log(`Context-loader config '${name}' saved.`);
|
|
172
|
+
return 0;
|
|
173
|
+
}
|
|
174
|
+
if (action === "use") {
|
|
175
|
+
const name = rest[0];
|
|
176
|
+
if (!name) {
|
|
177
|
+
console.error("Usage: kweaver context-loader config use <name>");
|
|
178
|
+
return 1;
|
|
179
|
+
}
|
|
180
|
+
try {
|
|
181
|
+
setCurrentContextLoader(platform, name);
|
|
182
|
+
console.log(`Switched to context-loader config '${name}'.`);
|
|
183
|
+
return 0;
|
|
184
|
+
}
|
|
185
|
+
catch (error) {
|
|
186
|
+
console.error(error instanceof Error ? error.message : String(error));
|
|
187
|
+
return 1;
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
if (action === "remove") {
|
|
191
|
+
const name = rest[0];
|
|
192
|
+
if (!name) {
|
|
193
|
+
console.error("Usage: kweaver context-loader config remove <name>");
|
|
194
|
+
return 1;
|
|
195
|
+
}
|
|
196
|
+
removeContextLoaderEntry(platform, name);
|
|
197
|
+
console.log(`Removed context-loader config '${name}'.`);
|
|
198
|
+
return 0;
|
|
199
|
+
}
|
|
200
|
+
console.error(`Unknown config subcommand: ${action}`);
|
|
201
|
+
return 1;
|
|
202
|
+
}
|
|
203
|
+
async function runListTools(options, args, pretty) {
|
|
204
|
+
let cursor;
|
|
205
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
206
|
+
if ((args[i] === "--cursor" || args[i] === "-c") && args[i + 1]) {
|
|
207
|
+
cursor = args[i + 1];
|
|
208
|
+
i += 1;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const result = await listTools(options, cursor ? { cursor } : undefined);
|
|
212
|
+
console.log(formatOutput(result, pretty));
|
|
213
|
+
return 0;
|
|
214
|
+
}
|
|
215
|
+
async function runListResources(options, args, pretty) {
|
|
216
|
+
let cursor;
|
|
217
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
218
|
+
if ((args[i] === "--cursor" || args[i] === "-c") && args[i + 1]) {
|
|
219
|
+
cursor = args[i + 1];
|
|
220
|
+
i += 1;
|
|
221
|
+
}
|
|
222
|
+
}
|
|
223
|
+
const result = await listResources(options, cursor ? { cursor } : undefined);
|
|
224
|
+
console.log(formatOutput(result, pretty));
|
|
225
|
+
return 0;
|
|
226
|
+
}
|
|
227
|
+
async function runReadResource(options, args, pretty) {
|
|
228
|
+
const uri = args.find((a) => !a.startsWith("-"));
|
|
229
|
+
if (!uri) {
|
|
230
|
+
console.error("Usage: kweaver context-loader resource <uri>");
|
|
231
|
+
return 1;
|
|
232
|
+
}
|
|
233
|
+
const result = await readResource(options, uri);
|
|
234
|
+
console.log(formatOutput(result, pretty));
|
|
235
|
+
return 0;
|
|
236
|
+
}
|
|
237
|
+
async function runListTemplates(options, args, pretty) {
|
|
238
|
+
let cursor;
|
|
239
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
240
|
+
if ((args[i] === "--cursor" || args[i] === "-c") && args[i + 1]) {
|
|
241
|
+
cursor = args[i + 1];
|
|
242
|
+
i += 1;
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const result = await listResourceTemplates(options, cursor ? { cursor } : undefined);
|
|
246
|
+
console.log(formatOutput(result, pretty));
|
|
247
|
+
return 0;
|
|
248
|
+
}
|
|
249
|
+
async function runListPrompts(options, args, pretty) {
|
|
250
|
+
let cursor;
|
|
251
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
252
|
+
if ((args[i] === "--cursor" || args[i] === "-c") && args[i + 1]) {
|
|
253
|
+
cursor = args[i + 1];
|
|
254
|
+
i += 1;
|
|
255
|
+
}
|
|
256
|
+
}
|
|
257
|
+
const result = await listPrompts(options, cursor ? { cursor } : undefined);
|
|
258
|
+
console.log(formatOutput(result, pretty));
|
|
259
|
+
return 0;
|
|
260
|
+
}
|
|
261
|
+
async function runGetPrompt(options, args, pretty) {
|
|
262
|
+
const name = args.find((a) => !a.startsWith("-"));
|
|
263
|
+
if (!name) {
|
|
264
|
+
console.error("Usage: kweaver context-loader prompt <name> [--args json]");
|
|
265
|
+
return 1;
|
|
266
|
+
}
|
|
267
|
+
let promptArgs;
|
|
268
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
269
|
+
if ((args[i] === "--args" || args[i] === "-a") && args[i + 1]) {
|
|
270
|
+
try {
|
|
271
|
+
promptArgs = JSON.parse(args[i + 1]);
|
|
272
|
+
}
|
|
273
|
+
catch {
|
|
274
|
+
console.error("Invalid --args JSON");
|
|
275
|
+
return 1;
|
|
276
|
+
}
|
|
277
|
+
i += 1;
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
const result = await getPrompt(options, name, promptArgs);
|
|
281
|
+
console.log(formatOutput(result, pretty));
|
|
282
|
+
return 0;
|
|
283
|
+
}
|
|
284
|
+
async function runKnSearch(options, args, pretty) {
|
|
285
|
+
let query;
|
|
286
|
+
let onlySchema = false;
|
|
287
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
288
|
+
const arg = args[i];
|
|
289
|
+
if (arg === "--only-schema") {
|
|
290
|
+
onlySchema = true;
|
|
291
|
+
}
|
|
292
|
+
else if (!arg.startsWith("-") && !query) {
|
|
293
|
+
query = arg;
|
|
294
|
+
}
|
|
295
|
+
}
|
|
296
|
+
if (!query) {
|
|
297
|
+
console.error("Usage: kweaver context-loader kn-search <query> [--only-schema]");
|
|
298
|
+
return 1;
|
|
299
|
+
}
|
|
300
|
+
const result = await knSearch(options, { query, only_schema: onlySchema });
|
|
301
|
+
console.log(formatOutput(result, pretty));
|
|
302
|
+
return 0;
|
|
303
|
+
}
|
|
304
|
+
async function runKnSchemaSearch(options, args, pretty) {
|
|
305
|
+
let query;
|
|
306
|
+
let maxConcepts;
|
|
307
|
+
for (let i = 0; i < args.length; i += 1) {
|
|
308
|
+
const arg = args[i];
|
|
309
|
+
if ((arg === "--max" || arg === "-n") && args[i + 1]) {
|
|
310
|
+
maxConcepts = parseInt(args[i + 1], 10);
|
|
311
|
+
i += 1;
|
|
312
|
+
}
|
|
313
|
+
else if (!arg.startsWith("-") && !query) {
|
|
314
|
+
query = arg;
|
|
315
|
+
}
|
|
316
|
+
}
|
|
317
|
+
if (!query) {
|
|
318
|
+
console.error("Usage: kweaver context-loader kn-schema-search <query> [--max N]");
|
|
319
|
+
return 1;
|
|
320
|
+
}
|
|
321
|
+
const result = await knSchemaSearch(options, {
|
|
322
|
+
query,
|
|
323
|
+
max_concepts: maxConcepts,
|
|
324
|
+
});
|
|
325
|
+
console.log(formatOutput(result, pretty));
|
|
326
|
+
return 0;
|
|
327
|
+
}
|
|
328
|
+
function parseJsonArg(args) {
|
|
329
|
+
const raw = args.join(" ").trim();
|
|
330
|
+
if (!raw) {
|
|
331
|
+
throw new Error("Missing JSON argument");
|
|
332
|
+
}
|
|
333
|
+
try {
|
|
334
|
+
return JSON.parse(raw);
|
|
335
|
+
}
|
|
336
|
+
catch {
|
|
337
|
+
throw new Error("Invalid JSON argument");
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
async function runQueryObjectInstance(options, args, pretty) {
|
|
341
|
+
const body = parseJsonArg(args);
|
|
342
|
+
if (!body.ot_id || !body.condition) {
|
|
343
|
+
console.error("JSON must include ot_id and condition. See ref/contextloader/examples.md");
|
|
344
|
+
return 1;
|
|
345
|
+
}
|
|
346
|
+
const result = await queryObjectInstance(options, {
|
|
347
|
+
ot_id: body.ot_id,
|
|
348
|
+
limit: body.limit,
|
|
349
|
+
condition: body.condition,
|
|
350
|
+
});
|
|
351
|
+
console.log(formatOutput(result, pretty));
|
|
352
|
+
return 0;
|
|
353
|
+
}
|
|
354
|
+
async function runQueryInstanceSubgraph(options, args, pretty) {
|
|
355
|
+
const body = parseJsonArg(args);
|
|
356
|
+
if (!Array.isArray(body.relation_type_paths)) {
|
|
357
|
+
console.error("JSON must include relation_type_paths array. See ref/contextloader/examples.md");
|
|
358
|
+
return 1;
|
|
359
|
+
}
|
|
360
|
+
const result = await queryInstanceSubgraph(options, body);
|
|
361
|
+
console.log(formatOutput(result, pretty));
|
|
362
|
+
return 0;
|
|
363
|
+
}
|
|
364
|
+
async function runGetLogicProperties(options, args, pretty) {
|
|
365
|
+
const body = parseJsonArg(args);
|
|
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 ref/contextloader/examples.md");
|
|
368
|
+
return 1;
|
|
369
|
+
}
|
|
370
|
+
const result = await getLogicPropertiesValues(options, body);
|
|
371
|
+
console.log(formatOutput(result, pretty));
|
|
372
|
+
return 0;
|
|
373
|
+
}
|
|
374
|
+
async function runGetActionInfo(options, args, pretty) {
|
|
375
|
+
const body = parseJsonArg(args);
|
|
376
|
+
if (!body.at_id || !body._instance_identity) {
|
|
377
|
+
console.error("JSON must include at_id and _instance_identity. See ref/contextloader/examples.md");
|
|
378
|
+
return 1;
|
|
379
|
+
}
|
|
380
|
+
const result = await getActionInfo(options, body);
|
|
381
|
+
console.log(formatOutput(result, pretty));
|
|
382
|
+
return 0;
|
|
383
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { ensureValidToken, formatHttpError } from "../auth/oauth.js";
|
|
2
|
+
export function parseTokenArgs(args) {
|
|
3
|
+
if (args.length > 0) {
|
|
4
|
+
throw new Error("Usage: kweaver token");
|
|
5
|
+
}
|
|
6
|
+
}
|
|
7
|
+
export async function runTokenCommand(args) {
|
|
8
|
+
try {
|
|
9
|
+
parseTokenArgs(args);
|
|
10
|
+
}
|
|
11
|
+
catch (error) {
|
|
12
|
+
console.error(formatHttpError(error));
|
|
13
|
+
return 1;
|
|
14
|
+
}
|
|
15
|
+
try {
|
|
16
|
+
const token = await ensureValidToken();
|
|
17
|
+
console.log(token.accessToken);
|
|
18
|
+
return 0;
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
console.error(formatHttpError(error));
|
|
22
|
+
return 1;
|
|
23
|
+
}
|
|
24
|
+
}
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
export interface ClientConfig {
|
|
2
|
+
baseUrl: string;
|
|
3
|
+
clientId: string;
|
|
4
|
+
clientSecret: string;
|
|
5
|
+
redirectUri: string;
|
|
6
|
+
logoutRedirectUri: string;
|
|
7
|
+
scope: string;
|
|
8
|
+
lang?: string;
|
|
9
|
+
product?: string;
|
|
10
|
+
xForwardedPrefix?: string;
|
|
11
|
+
}
|
|
12
|
+
export interface TokenConfig {
|
|
13
|
+
baseUrl: string;
|
|
14
|
+
accessToken: string;
|
|
15
|
+
tokenType: string;
|
|
16
|
+
scope: string;
|
|
17
|
+
expiresIn?: number;
|
|
18
|
+
expiresAt?: string;
|
|
19
|
+
refreshToken?: string;
|
|
20
|
+
idToken?: string;
|
|
21
|
+
obtainedAt: string;
|
|
22
|
+
}
|
|
23
|
+
export interface CallbackSession {
|
|
24
|
+
baseUrl: string;
|
|
25
|
+
redirectUri: string;
|
|
26
|
+
code: string;
|
|
27
|
+
state: string;
|
|
28
|
+
scope?: string;
|
|
29
|
+
receivedAt: string;
|
|
30
|
+
}
|
|
31
|
+
/** Single context-loader entry (named kn_id). */
|
|
32
|
+
export interface ContextLoaderEntry {
|
|
33
|
+
name: string;
|
|
34
|
+
knId: string;
|
|
35
|
+
}
|
|
36
|
+
/** Per-platform context-loader config: multiple kn entries, one current. */
|
|
37
|
+
export interface ContextLoaderConfig {
|
|
38
|
+
configs: ContextLoaderEntry[];
|
|
39
|
+
current: string;
|
|
40
|
+
}
|
|
41
|
+
export interface PlatformSummary {
|
|
42
|
+
baseUrl: string;
|
|
43
|
+
hasToken: boolean;
|
|
44
|
+
isCurrent: boolean;
|
|
45
|
+
alias?: string;
|
|
46
|
+
}
|
|
47
|
+
export declare function getConfigDir(): string;
|
|
48
|
+
export declare function getCurrentPlatform(): string | null;
|
|
49
|
+
export declare function setCurrentPlatform(baseUrl: string): void;
|
|
50
|
+
export declare function setPlatformAlias(baseUrl: string, alias: string): void;
|
|
51
|
+
export declare function deletePlatformAlias(baseUrl: string): void;
|
|
52
|
+
export declare function getPlatformAlias(baseUrl: string): string | null;
|
|
53
|
+
export declare function resolvePlatformIdentifier(value: string): string | null;
|
|
54
|
+
export declare function loadClientConfig(baseUrl?: string): ClientConfig | null;
|
|
55
|
+
export declare function saveClientConfig(config: ClientConfig): void;
|
|
56
|
+
export declare function loadTokenConfig(baseUrl?: string): TokenConfig | null;
|
|
57
|
+
export declare function saveTokenConfig(config: TokenConfig): void;
|
|
58
|
+
export declare function loadCallbackSession(baseUrl?: string): CallbackSession | null;
|
|
59
|
+
export declare function saveCallbackSession(session: CallbackSession): void;
|
|
60
|
+
export declare function loadContextLoaderConfig(baseUrl?: string): ContextLoaderConfig | null;
|
|
61
|
+
export declare function saveContextLoaderConfig(baseUrl: string, config: ContextLoaderConfig): void;
|
|
62
|
+
export interface CurrentContextLoaderKn {
|
|
63
|
+
mcpUrl: string;
|
|
64
|
+
knId: string;
|
|
65
|
+
}
|
|
66
|
+
export declare function getCurrentContextLoaderKn(baseUrl?: string): CurrentContextLoaderKn | null;
|
|
67
|
+
export declare function addContextLoaderEntry(baseUrl: string, name: string, knId: string): void;
|
|
68
|
+
export declare function setCurrentContextLoader(baseUrl: string, name: string): void;
|
|
69
|
+
export declare function removeContextLoaderEntry(baseUrl: string, name: string): void;
|
|
70
|
+
export declare function hasPlatform(baseUrl: string): boolean;
|
|
71
|
+
/**
|
|
72
|
+
* Remove token and callback for a platform so the next auth will do a full login.
|
|
73
|
+
* Keeps client config so the same app registration can be reused.
|
|
74
|
+
*/
|
|
75
|
+
export declare function clearPlatformSession(baseUrl: string): void;
|
|
76
|
+
export declare function deletePlatform(baseUrl: string): void;
|
|
77
|
+
export declare function listPlatforms(): PlatformSummary[];
|