@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,259 @@
|
|
|
1
|
+
import { fetchTextOrThrow } from "../utils/http.js";
|
|
2
|
+
const MCP_PROTOCOL_VERSION = "2024-11-05";
|
|
3
|
+
const sessionCache = new Map();
|
|
4
|
+
function sessionKey(options) {
|
|
5
|
+
return `${options.mcpUrl}:${options.knId}`;
|
|
6
|
+
}
|
|
7
|
+
function buildHeaders(options, sessionId) {
|
|
8
|
+
const headers = {
|
|
9
|
+
"Content-Type": "application/json",
|
|
10
|
+
Accept: "application/json, text/event-stream",
|
|
11
|
+
Authorization: `Bearer ${options.accessToken}`,
|
|
12
|
+
"X-Kn-ID": options.knId,
|
|
13
|
+
"MCP-Protocol-Version": MCP_PROTOCOL_VERSION,
|
|
14
|
+
};
|
|
15
|
+
if (sessionId) {
|
|
16
|
+
headers["MCP-Session-Id"] = sessionId;
|
|
17
|
+
}
|
|
18
|
+
return headers;
|
|
19
|
+
}
|
|
20
|
+
async function ensureSession(options) {
|
|
21
|
+
const key = sessionKey(options);
|
|
22
|
+
const cached = sessionCache.get(key);
|
|
23
|
+
if (cached)
|
|
24
|
+
return cached;
|
|
25
|
+
const initBody = JSON.stringify({
|
|
26
|
+
jsonrpc: "2.0",
|
|
27
|
+
id: 1,
|
|
28
|
+
method: "initialize",
|
|
29
|
+
params: {
|
|
30
|
+
protocolVersion: MCP_PROTOCOL_VERSION,
|
|
31
|
+
capabilities: {},
|
|
32
|
+
clientInfo: { name: "kweaver-caller", version: "0.1.0" },
|
|
33
|
+
},
|
|
34
|
+
});
|
|
35
|
+
const { response, body } = await fetchTextOrThrow(options.mcpUrl, {
|
|
36
|
+
method: "POST",
|
|
37
|
+
headers: buildHeaders(options),
|
|
38
|
+
body: initBody,
|
|
39
|
+
});
|
|
40
|
+
const sessionId = response.headers.get("MCP-Session-Id") ?? response.headers.get("mcp-session-id");
|
|
41
|
+
if (!sessionId) {
|
|
42
|
+
throw new Error("MCP server did not return MCP-Session-Id. The server may require session initialization.");
|
|
43
|
+
}
|
|
44
|
+
const initNotifBody = JSON.stringify({
|
|
45
|
+
jsonrpc: "2.0",
|
|
46
|
+
method: "notifications/initialized",
|
|
47
|
+
});
|
|
48
|
+
await fetchTextOrThrow(options.mcpUrl, {
|
|
49
|
+
method: "POST",
|
|
50
|
+
headers: buildHeaders(options, sessionId),
|
|
51
|
+
body: initNotifBody,
|
|
52
|
+
});
|
|
53
|
+
sessionCache.set(key, sessionId);
|
|
54
|
+
return sessionId;
|
|
55
|
+
}
|
|
56
|
+
function isMissingInputParams(obj) {
|
|
57
|
+
return (typeof obj === "object" &&
|
|
58
|
+
obj !== null &&
|
|
59
|
+
obj.error_code === "MISSING_INPUT_PARAMS");
|
|
60
|
+
}
|
|
61
|
+
/** Build retry hint from MISSING_INPUT_PARAMS response. */
|
|
62
|
+
export function formatMissingInputParamsHint(err) {
|
|
63
|
+
const lines = [
|
|
64
|
+
"MISSING_INPUT_PARAMS: " + err.message,
|
|
65
|
+
"Add the following to additional_context and retry:",
|
|
66
|
+
];
|
|
67
|
+
for (const m of err.missing ?? []) {
|
|
68
|
+
for (const p of m.params ?? []) {
|
|
69
|
+
if (p.hint)
|
|
70
|
+
lines.push(` - ${p.name}: ${p.hint}`);
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
return lines.join("\n");
|
|
74
|
+
}
|
|
75
|
+
/** Guardrail: value_from only supports "const"; must appear with value. */
|
|
76
|
+
export function validateCondition(condition) {
|
|
77
|
+
if (!condition || typeof condition !== "object")
|
|
78
|
+
return;
|
|
79
|
+
const c = condition;
|
|
80
|
+
const sub = c.sub_conditions;
|
|
81
|
+
if (Array.isArray(sub)) {
|
|
82
|
+
for (const s of sub) {
|
|
83
|
+
if (s && typeof s === "object") {
|
|
84
|
+
const sc = s;
|
|
85
|
+
if (sc.value_from !== undefined && sc.value_from !== "const") {
|
|
86
|
+
throw new Error('Condition value_from only supports "const". Must use value_from: "const" with value.');
|
|
87
|
+
}
|
|
88
|
+
if (sc.value_from === "const" && sc.value === undefined) {
|
|
89
|
+
throw new Error('When value_from is "const", value must be provided.');
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (c.value_from !== undefined && c.value_from !== "const") {
|
|
95
|
+
throw new Error('Condition value_from only supports "const".');
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Guardrail: _instance_identity must be a plain object (from Layer 2 output, not fabricated). */
|
|
99
|
+
export function validateInstanceIdentity(v, label) {
|
|
100
|
+
if (v === null || typeof v !== "object" || Array.isArray(v)) {
|
|
101
|
+
throw new Error(`${label} must be a plain object from Layer 2 query result (_instance_identity). Do not fabricate.`);
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
/** Guardrail: _instance_identities must be array of plain objects from Layer 2. */
|
|
105
|
+
export function validateInstanceIdentities(v) {
|
|
106
|
+
if (!Array.isArray(v)) {
|
|
107
|
+
throw new Error("_instance_identities must be an array from Layer 2 query result.");
|
|
108
|
+
}
|
|
109
|
+
for (let i = 0; i < v.length; i += 1) {
|
|
110
|
+
validateInstanceIdentity(v[i], `_instance_identities[${i}]`);
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
let requestId = 0;
|
|
114
|
+
/** Call a generic MCP JSON-RPC method (e.g. tools/list, resources/list). Returns result as-is. */
|
|
115
|
+
async function callMcpMethod(options, method, params = {}) {
|
|
116
|
+
const sessionId = await ensureSession(options);
|
|
117
|
+
const id = (requestId += 1);
|
|
118
|
+
const body = JSON.stringify({
|
|
119
|
+
jsonrpc: "2.0",
|
|
120
|
+
method,
|
|
121
|
+
params: Object.keys(params).length > 0 ? params : undefined,
|
|
122
|
+
id,
|
|
123
|
+
});
|
|
124
|
+
const { body: responseBody } = await fetchTextOrThrow(options.mcpUrl, {
|
|
125
|
+
method: "POST",
|
|
126
|
+
headers: buildHeaders(options, sessionId),
|
|
127
|
+
body,
|
|
128
|
+
});
|
|
129
|
+
let parsed;
|
|
130
|
+
try {
|
|
131
|
+
parsed = JSON.parse(responseBody);
|
|
132
|
+
}
|
|
133
|
+
catch {
|
|
134
|
+
throw new Error(`Context-loader returned invalid JSON: ${responseBody.slice(0, 200)}`);
|
|
135
|
+
}
|
|
136
|
+
const rpc = parsed;
|
|
137
|
+
if (rpc.error) {
|
|
138
|
+
throw new Error(`Context-loader error: ${rpc.error.message}`);
|
|
139
|
+
}
|
|
140
|
+
if (rpc.result !== undefined) {
|
|
141
|
+
return rpc.result;
|
|
142
|
+
}
|
|
143
|
+
throw new Error("Context-loader returned no result");
|
|
144
|
+
}
|
|
145
|
+
async function callTool(options, toolName, args) {
|
|
146
|
+
const sessionId = await ensureSession(options);
|
|
147
|
+
const id = (requestId += 1);
|
|
148
|
+
const body = JSON.stringify({
|
|
149
|
+
jsonrpc: "2.0",
|
|
150
|
+
method: "tools/call",
|
|
151
|
+
params: { name: toolName, arguments: args },
|
|
152
|
+
id,
|
|
153
|
+
});
|
|
154
|
+
const { body: responseBody } = await fetchTextOrThrow(options.mcpUrl, {
|
|
155
|
+
method: "POST",
|
|
156
|
+
headers: buildHeaders(options, sessionId),
|
|
157
|
+
body,
|
|
158
|
+
});
|
|
159
|
+
let parsed;
|
|
160
|
+
try {
|
|
161
|
+
parsed = JSON.parse(responseBody);
|
|
162
|
+
}
|
|
163
|
+
catch {
|
|
164
|
+
throw new Error(`Context-loader returned invalid JSON: ${responseBody.slice(0, 200)}`);
|
|
165
|
+
}
|
|
166
|
+
if (isMissingInputParams(parsed)) {
|
|
167
|
+
throw new Error(formatMissingInputParamsHint(parsed));
|
|
168
|
+
}
|
|
169
|
+
const rpc = parsed;
|
|
170
|
+
if (rpc.error) {
|
|
171
|
+
const data = rpc.error.data;
|
|
172
|
+
if (isMissingInputParams(data)) {
|
|
173
|
+
throw new Error(formatMissingInputParamsHint(data));
|
|
174
|
+
}
|
|
175
|
+
throw new Error(`Context-loader error: ${rpc.error.message}`);
|
|
176
|
+
}
|
|
177
|
+
const result = rpc.result;
|
|
178
|
+
if (result !== undefined) {
|
|
179
|
+
const content = result.content;
|
|
180
|
+
if (Array.isArray(content) && content.length > 0) {
|
|
181
|
+
const first = content[0];
|
|
182
|
+
const text = first?.text;
|
|
183
|
+
if (typeof text === "string") {
|
|
184
|
+
try {
|
|
185
|
+
return JSON.parse(text);
|
|
186
|
+
}
|
|
187
|
+
catch {
|
|
188
|
+
return { raw: text };
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
return result;
|
|
193
|
+
}
|
|
194
|
+
const direct = parsed;
|
|
195
|
+
if ("object_types" in direct ||
|
|
196
|
+
"concepts" in direct ||
|
|
197
|
+
"datas" in direct ||
|
|
198
|
+
"entries" in direct ||
|
|
199
|
+
"_dynamic_tools" in direct) {
|
|
200
|
+
return parsed;
|
|
201
|
+
}
|
|
202
|
+
throw new Error("Context-loader returned no result");
|
|
203
|
+
}
|
|
204
|
+
/** Layer 1: kn_search. Returns object_types, relation_types, action_types. */
|
|
205
|
+
export async function knSearch(options, args) {
|
|
206
|
+
return callTool(options, "kn_search", { ...args });
|
|
207
|
+
}
|
|
208
|
+
/** Layer 1: kn_schema_search. Returns concepts (candidate discovery only). */
|
|
209
|
+
export async function knSchemaSearch(options, args) {
|
|
210
|
+
return callTool(options, "kn_schema_search", { ...args });
|
|
211
|
+
}
|
|
212
|
+
/** Layer 2: query_object_instance. Returns datas with _instance_identity. */
|
|
213
|
+
export async function queryObjectInstance(options, args) {
|
|
214
|
+
validateCondition(args.condition);
|
|
215
|
+
return callTool(options, "query_object_instance", { ...args });
|
|
216
|
+
}
|
|
217
|
+
/** Layer 2: query_instance_subgraph. Returns entries with nested _instance_identity. */
|
|
218
|
+
export async function queryInstanceSubgraph(options, args) {
|
|
219
|
+
for (const path of args.relation_type_paths) {
|
|
220
|
+
for (const ot of path.object_types) {
|
|
221
|
+
validateCondition(ot.condition);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
return callTool(options, "query_instance_subgraph", { ...args });
|
|
225
|
+
}
|
|
226
|
+
/** Layer 3: get_logic_properties_values. Throws with retry hint on MISSING_INPUT_PARAMS. */
|
|
227
|
+
export async function getLogicPropertiesValues(options, args) {
|
|
228
|
+
validateInstanceIdentities(args._instance_identities);
|
|
229
|
+
return callTool(options, "get_logic_properties_values", { ...args });
|
|
230
|
+
}
|
|
231
|
+
/** Layer 3: get_action_info. Returns _dynamic_tools. */
|
|
232
|
+
export async function getActionInfo(options, args) {
|
|
233
|
+
validateInstanceIdentity(args._instance_identity, "_instance_identity");
|
|
234
|
+
return callTool(options, "get_action_info", { ...args });
|
|
235
|
+
}
|
|
236
|
+
/** MCP tools/list. Returns list of available tools. */
|
|
237
|
+
export async function listTools(options, params) {
|
|
238
|
+
return callMcpMethod(options, "tools/list", params ? { cursor: params.cursor } : {});
|
|
239
|
+
}
|
|
240
|
+
/** MCP resources/list. Returns list of available resources. */
|
|
241
|
+
export async function listResources(options, params) {
|
|
242
|
+
return callMcpMethod(options, "resources/list", params ? { cursor: params.cursor } : {});
|
|
243
|
+
}
|
|
244
|
+
/** MCP resources/read. Returns resource content by URI. */
|
|
245
|
+
export async function readResource(options, uri) {
|
|
246
|
+
return callMcpMethod(options, "resources/read", { uri });
|
|
247
|
+
}
|
|
248
|
+
/** MCP resources/templates/list. Returns list of resource templates. */
|
|
249
|
+
export async function listResourceTemplates(options, params) {
|
|
250
|
+
return callMcpMethod(options, "resources/templates/list", params ? { cursor: params.cursor } : {});
|
|
251
|
+
}
|
|
252
|
+
/** MCP prompts/list. Returns list of available prompts. */
|
|
253
|
+
export async function listPrompts(options, params) {
|
|
254
|
+
return callMcpMethod(options, "prompts/list", params ? { cursor: params.cursor } : {});
|
|
255
|
+
}
|
|
256
|
+
/** MCP prompts/get. Returns prompt by name with optional arguments. */
|
|
257
|
+
export async function getPrompt(options, name, args) {
|
|
258
|
+
return callMcpMethod(options, "prompts/get", args ? { name, arguments: args } : { name });
|
|
259
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
export interface ListConversationsOptions {
|
|
2
|
+
baseUrl: string;
|
|
3
|
+
accessToken: string;
|
|
4
|
+
agentId: string;
|
|
5
|
+
businessDomain?: string;
|
|
6
|
+
limit?: number;
|
|
7
|
+
}
|
|
8
|
+
export interface ListMessagesOptions {
|
|
9
|
+
baseUrl: string;
|
|
10
|
+
accessToken: string;
|
|
11
|
+
conversationId: string;
|
|
12
|
+
businessDomain?: string;
|
|
13
|
+
limit?: number;
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* List conversations for an agent.
|
|
17
|
+
* Returns empty array on 404 (endpoint may not be available in all deployments).
|
|
18
|
+
*/
|
|
19
|
+
export declare function listConversations(opts: ListConversationsOptions): Promise<string>;
|
|
20
|
+
/**
|
|
21
|
+
* List messages for a conversation.
|
|
22
|
+
* Returns empty array on 404 (endpoint may not be available in all deployments).
|
|
23
|
+
*/
|
|
24
|
+
export declare function listMessages(opts: ListMessagesOptions): Promise<string>;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
function buildConversationsUrl(baseUrl, agentId) {
|
|
2
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
3
|
+
return `${base}/api/agent-app/v1/app/${agentId}/conversations`;
|
|
4
|
+
}
|
|
5
|
+
function buildMessagesUrl(baseUrl, conversationId) {
|
|
6
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
7
|
+
return `${base}/api/agent-app/v1/conversations/${conversationId}/messages`;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* List conversations for an agent.
|
|
11
|
+
* Returns empty array on 404 (endpoint may not be available in all deployments).
|
|
12
|
+
*/
|
|
13
|
+
export async function listConversations(opts) {
|
|
14
|
+
const { baseUrl, accessToken, agentId, businessDomain = "bd_public", limit } = opts;
|
|
15
|
+
const url = new URL(buildConversationsUrl(baseUrl, agentId));
|
|
16
|
+
if (limit !== undefined) {
|
|
17
|
+
url.searchParams.set("limit", String(limit));
|
|
18
|
+
}
|
|
19
|
+
const response = await fetch(url.toString(), {
|
|
20
|
+
method: "GET",
|
|
21
|
+
headers: {
|
|
22
|
+
accept: "application/json",
|
|
23
|
+
authorization: `Bearer ${accessToken}`,
|
|
24
|
+
token: accessToken,
|
|
25
|
+
"x-business-domain": businessDomain,
|
|
26
|
+
},
|
|
27
|
+
});
|
|
28
|
+
if (response.status === 404) {
|
|
29
|
+
return "[]";
|
|
30
|
+
}
|
|
31
|
+
const body = await response.text();
|
|
32
|
+
if (!response.ok) {
|
|
33
|
+
return "[]";
|
|
34
|
+
}
|
|
35
|
+
return body || "[]";
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* List messages for a conversation.
|
|
39
|
+
* Returns empty array on 404 (endpoint may not be available in all deployments).
|
|
40
|
+
*/
|
|
41
|
+
export async function listMessages(opts) {
|
|
42
|
+
const { baseUrl, accessToken, conversationId, businessDomain = "bd_public", limit } = opts;
|
|
43
|
+
const url = new URL(buildMessagesUrl(baseUrl, conversationId));
|
|
44
|
+
if (limit !== undefined) {
|
|
45
|
+
url.searchParams.set("limit", String(limit));
|
|
46
|
+
}
|
|
47
|
+
const response = await fetch(url.toString(), {
|
|
48
|
+
method: "GET",
|
|
49
|
+
headers: {
|
|
50
|
+
accept: "application/json",
|
|
51
|
+
authorization: `Bearer ${accessToken}`,
|
|
52
|
+
token: accessToken,
|
|
53
|
+
"x-business-domain": businessDomain,
|
|
54
|
+
},
|
|
55
|
+
});
|
|
56
|
+
if (response.status === 404) {
|
|
57
|
+
return "[]";
|
|
58
|
+
}
|
|
59
|
+
const body = await response.text();
|
|
60
|
+
if (!response.ok) {
|
|
61
|
+
return "[]";
|
|
62
|
+
}
|
|
63
|
+
return body || "[]";
|
|
64
|
+
}
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
export interface ListKnowledgeNetworksOptions {
|
|
2
|
+
baseUrl: string;
|
|
3
|
+
accessToken: string;
|
|
4
|
+
businessDomain?: string;
|
|
5
|
+
offset?: number;
|
|
6
|
+
limit?: number;
|
|
7
|
+
sort?: string;
|
|
8
|
+
direction?: "asc" | "desc";
|
|
9
|
+
name_pattern?: string;
|
|
10
|
+
tag?: string;
|
|
11
|
+
}
|
|
12
|
+
export declare function listKnowledgeNetworks(options: ListKnowledgeNetworksOptions): Promise<string>;
|
|
13
|
+
export interface GetKnowledgeNetworkOptions {
|
|
14
|
+
baseUrl: string;
|
|
15
|
+
accessToken: string;
|
|
16
|
+
knId: string;
|
|
17
|
+
businessDomain?: string;
|
|
18
|
+
mode?: "export" | "";
|
|
19
|
+
include_statistics?: boolean;
|
|
20
|
+
}
|
|
21
|
+
export declare function getKnowledgeNetwork(options: GetKnowledgeNetworkOptions): Promise<string>;
|
|
22
|
+
export interface CreateKnowledgeNetworkOptions {
|
|
23
|
+
baseUrl: string;
|
|
24
|
+
accessToken: string;
|
|
25
|
+
body: string;
|
|
26
|
+
businessDomain?: string;
|
|
27
|
+
import_mode?: "normal" | "ignore" | "overwrite";
|
|
28
|
+
validate_dependency?: boolean;
|
|
29
|
+
}
|
|
30
|
+
export declare function createKnowledgeNetwork(options: CreateKnowledgeNetworkOptions): Promise<string>;
|
|
31
|
+
export interface UpdateKnowledgeNetworkOptions {
|
|
32
|
+
baseUrl: string;
|
|
33
|
+
accessToken: string;
|
|
34
|
+
knId: string;
|
|
35
|
+
body: string;
|
|
36
|
+
businessDomain?: string;
|
|
37
|
+
}
|
|
38
|
+
export declare function updateKnowledgeNetwork(options: UpdateKnowledgeNetworkOptions): Promise<string>;
|
|
39
|
+
export interface DeleteKnowledgeNetworkOptions {
|
|
40
|
+
baseUrl: string;
|
|
41
|
+
accessToken: string;
|
|
42
|
+
knId: string;
|
|
43
|
+
businessDomain?: string;
|
|
44
|
+
}
|
|
45
|
+
export declare function deleteKnowledgeNetwork(options: DeleteKnowledgeNetworkOptions): Promise<void>;
|
|
46
|
+
/** List object types, relation types, or action types (ontology-manager). */
|
|
47
|
+
export interface ListSchemaTypesOptions {
|
|
48
|
+
baseUrl: string;
|
|
49
|
+
accessToken: string;
|
|
50
|
+
knId: string;
|
|
51
|
+
businessDomain?: string;
|
|
52
|
+
branch?: string;
|
|
53
|
+
limit?: number;
|
|
54
|
+
}
|
|
55
|
+
export declare function listObjectTypes(options: ListSchemaTypesOptions): Promise<string>;
|
|
56
|
+
export declare function listRelationTypes(options: ListSchemaTypesOptions): Promise<string>;
|
|
57
|
+
export declare function listActionTypes(options: ListSchemaTypesOptions): Promise<string>;
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import { HttpError } from "../utils/http.js";
|
|
2
|
+
function buildHeaders(accessToken, businessDomain) {
|
|
3
|
+
return {
|
|
4
|
+
accept: "application/json, text/plain, */*",
|
|
5
|
+
"accept-language": "zh-cn",
|
|
6
|
+
authorization: `Bearer ${accessToken}`,
|
|
7
|
+
token: accessToken,
|
|
8
|
+
"x-business-domain": businessDomain,
|
|
9
|
+
"x-language": "zh-cn",
|
|
10
|
+
};
|
|
11
|
+
}
|
|
12
|
+
export async function listKnowledgeNetworks(options) {
|
|
13
|
+
const { baseUrl, accessToken, businessDomain = "bd_public", offset = 0, limit = 50, sort = "update_time", direction = "desc", name_pattern, tag, } = options;
|
|
14
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
15
|
+
const url = new URL(`${base}/api/ontology-manager/v1/knowledge-networks`);
|
|
16
|
+
url.searchParams.set("offset", String(offset));
|
|
17
|
+
url.searchParams.set("limit", String(limit));
|
|
18
|
+
url.searchParams.set("direction", direction);
|
|
19
|
+
url.searchParams.set("sort", sort);
|
|
20
|
+
if (name_pattern !== undefined && name_pattern !== "") {
|
|
21
|
+
url.searchParams.set("name_pattern", name_pattern);
|
|
22
|
+
}
|
|
23
|
+
if (tag !== undefined && tag !== "") {
|
|
24
|
+
url.searchParams.set("tag", tag);
|
|
25
|
+
}
|
|
26
|
+
const response = await fetch(url.toString(), {
|
|
27
|
+
method: "GET",
|
|
28
|
+
headers: buildHeaders(accessToken, businessDomain),
|
|
29
|
+
});
|
|
30
|
+
const body = await response.text();
|
|
31
|
+
if (!response.ok) {
|
|
32
|
+
throw new HttpError(response.status, response.statusText, body);
|
|
33
|
+
}
|
|
34
|
+
return body;
|
|
35
|
+
}
|
|
36
|
+
export async function getKnowledgeNetwork(options) {
|
|
37
|
+
const { baseUrl, accessToken, knId, businessDomain = "bd_public", mode, include_statistics, } = options;
|
|
38
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
39
|
+
const url = new URL(`${base}/api/ontology-manager/v1/knowledge-networks/${encodeURIComponent(knId)}`);
|
|
40
|
+
if (mode === "export") {
|
|
41
|
+
url.searchParams.set("mode", "export");
|
|
42
|
+
}
|
|
43
|
+
if (include_statistics === true) {
|
|
44
|
+
url.searchParams.set("include_statistics", "true");
|
|
45
|
+
}
|
|
46
|
+
const response = await fetch(url.toString(), {
|
|
47
|
+
method: "GET",
|
|
48
|
+
headers: buildHeaders(accessToken, businessDomain),
|
|
49
|
+
});
|
|
50
|
+
const body = await response.text();
|
|
51
|
+
if (!response.ok) {
|
|
52
|
+
throw new HttpError(response.status, response.statusText, body);
|
|
53
|
+
}
|
|
54
|
+
return body;
|
|
55
|
+
}
|
|
56
|
+
export async function createKnowledgeNetwork(options) {
|
|
57
|
+
const { baseUrl, accessToken, body, businessDomain = "bd_public", import_mode, validate_dependency, } = options;
|
|
58
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
59
|
+
const url = new URL(`${base}/api/ontology-manager/v1/knowledge-networks`);
|
|
60
|
+
if (import_mode) {
|
|
61
|
+
url.searchParams.set("import_mode", import_mode);
|
|
62
|
+
}
|
|
63
|
+
if (validate_dependency !== undefined) {
|
|
64
|
+
url.searchParams.set("validate_dependency", String(validate_dependency));
|
|
65
|
+
}
|
|
66
|
+
const response = await fetch(url.toString(), {
|
|
67
|
+
method: "POST",
|
|
68
|
+
headers: {
|
|
69
|
+
...buildHeaders(accessToken, businessDomain),
|
|
70
|
+
"content-type": "application/json",
|
|
71
|
+
},
|
|
72
|
+
body,
|
|
73
|
+
});
|
|
74
|
+
const responseBody = await response.text();
|
|
75
|
+
if (!response.ok) {
|
|
76
|
+
throw new HttpError(response.status, response.statusText, responseBody);
|
|
77
|
+
}
|
|
78
|
+
return responseBody;
|
|
79
|
+
}
|
|
80
|
+
export async function updateKnowledgeNetwork(options) {
|
|
81
|
+
const { baseUrl, accessToken, knId, body, businessDomain = "bd_public", } = options;
|
|
82
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
83
|
+
const url = `${base}/api/ontology-manager/v1/knowledge-networks/${encodeURIComponent(knId)}`;
|
|
84
|
+
const response = await fetch(url, {
|
|
85
|
+
method: "PUT",
|
|
86
|
+
headers: {
|
|
87
|
+
...buildHeaders(accessToken, businessDomain),
|
|
88
|
+
"content-type": "application/json",
|
|
89
|
+
},
|
|
90
|
+
body,
|
|
91
|
+
});
|
|
92
|
+
const responseBody = await response.text();
|
|
93
|
+
if (!response.ok) {
|
|
94
|
+
throw new HttpError(response.status, response.statusText, responseBody);
|
|
95
|
+
}
|
|
96
|
+
return responseBody;
|
|
97
|
+
}
|
|
98
|
+
export async function deleteKnowledgeNetwork(options) {
|
|
99
|
+
const { baseUrl, accessToken, knId, businessDomain = "bd_public", } = options;
|
|
100
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
101
|
+
const url = `${base}/api/ontology-manager/v1/knowledge-networks/${encodeURIComponent(knId)}`;
|
|
102
|
+
const response = await fetch(url, {
|
|
103
|
+
method: "DELETE",
|
|
104
|
+
headers: buildHeaders(accessToken, businessDomain),
|
|
105
|
+
});
|
|
106
|
+
if (!response.ok) {
|
|
107
|
+
const body = await response.text();
|
|
108
|
+
throw new HttpError(response.status, response.statusText, body);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
export async function listObjectTypes(options) {
|
|
112
|
+
const { baseUrl, accessToken, knId, businessDomain = "bd_public", branch = "main", limit = -1, } = options;
|
|
113
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
114
|
+
const url = new URL(`${base}/api/ontology-manager/v1/knowledge-networks/${encodeURIComponent(knId)}/object-types`);
|
|
115
|
+
url.searchParams.set("branch", branch);
|
|
116
|
+
url.searchParams.set("limit", String(limit));
|
|
117
|
+
const response = await fetch(url.toString(), {
|
|
118
|
+
method: "GET",
|
|
119
|
+
headers: buildHeaders(accessToken, businessDomain),
|
|
120
|
+
});
|
|
121
|
+
const body = await response.text();
|
|
122
|
+
if (!response.ok) {
|
|
123
|
+
throw new HttpError(response.status, response.statusText, body);
|
|
124
|
+
}
|
|
125
|
+
return body;
|
|
126
|
+
}
|
|
127
|
+
export async function listRelationTypes(options) {
|
|
128
|
+
const { baseUrl, accessToken, knId, businessDomain = "bd_public", branch = "main", limit = -1, } = options;
|
|
129
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
130
|
+
const url = new URL(`${base}/api/ontology-manager/v1/knowledge-networks/${encodeURIComponent(knId)}/relation-types`);
|
|
131
|
+
url.searchParams.set("branch", branch);
|
|
132
|
+
url.searchParams.set("limit", String(limit));
|
|
133
|
+
const response = await fetch(url.toString(), {
|
|
134
|
+
method: "GET",
|
|
135
|
+
headers: buildHeaders(accessToken, businessDomain),
|
|
136
|
+
});
|
|
137
|
+
const body = await response.text();
|
|
138
|
+
if (!response.ok) {
|
|
139
|
+
throw new HttpError(response.status, response.statusText, body);
|
|
140
|
+
}
|
|
141
|
+
return body;
|
|
142
|
+
}
|
|
143
|
+
export async function listActionTypes(options) {
|
|
144
|
+
const { baseUrl, accessToken, knId, businessDomain = "bd_public", branch = "main", limit = -1, } = options;
|
|
145
|
+
const base = baseUrl.replace(/\/+$/, "");
|
|
146
|
+
const url = new URL(`${base}/api/ontology-manager/v1/knowledge-networks/${encodeURIComponent(knId)}/action-types`);
|
|
147
|
+
url.searchParams.set("branch", branch);
|
|
148
|
+
url.searchParams.set("limit", String(limit));
|
|
149
|
+
const response = await fetch(url.toString(), {
|
|
150
|
+
method: "GET",
|
|
151
|
+
headers: buildHeaders(accessToken, businessDomain),
|
|
152
|
+
});
|
|
153
|
+
const body = await response.text();
|
|
154
|
+
if (!response.ok) {
|
|
155
|
+
throw new HttpError(response.status, response.statusText, body);
|
|
156
|
+
}
|
|
157
|
+
return body;
|
|
158
|
+
}
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
export interface OntologyQueryBaseOptions {
|
|
2
|
+
baseUrl: string;
|
|
3
|
+
accessToken: string;
|
|
4
|
+
knId: string;
|
|
5
|
+
businessDomain?: string;
|
|
6
|
+
}
|
|
7
|
+
/** Object-type query: POST with X-HTTP-Method-Override: GET */
|
|
8
|
+
export interface ObjectTypeQueryOptions extends OntologyQueryBaseOptions {
|
|
9
|
+
otId: string;
|
|
10
|
+
body: string;
|
|
11
|
+
includeTypeInfo?: boolean;
|
|
12
|
+
includeLogicParams?: boolean;
|
|
13
|
+
excludeSystemProperties?: string[];
|
|
14
|
+
}
|
|
15
|
+
export declare function objectTypeQuery(options: ObjectTypeQueryOptions): Promise<string>;
|
|
16
|
+
/** Object-type properties: POST with X-HTTP-Method-Override: GET */
|
|
17
|
+
export interface ObjectTypePropertiesOptions extends OntologyQueryBaseOptions {
|
|
18
|
+
otId: string;
|
|
19
|
+
body: string;
|
|
20
|
+
excludeSystemProperties?: string[];
|
|
21
|
+
}
|
|
22
|
+
export declare function objectTypeProperties(options: ObjectTypePropertiesOptions): Promise<string>;
|
|
23
|
+
/** Subgraph: POST */
|
|
24
|
+
export interface SubgraphOptions extends OntologyQueryBaseOptions {
|
|
25
|
+
body: string;
|
|
26
|
+
includeLogicParams?: boolean;
|
|
27
|
+
excludeSystemProperties?: string[];
|
|
28
|
+
queryType?: "" | "relation_path";
|
|
29
|
+
}
|
|
30
|
+
export declare function subgraph(options: SubgraphOptions): Promise<string>;
|
|
31
|
+
/** Action-type query: POST with X-HTTP-Method-Override: GET */
|
|
32
|
+
export interface ActionTypeQueryOptions extends OntologyQueryBaseOptions {
|
|
33
|
+
atId: string;
|
|
34
|
+
body: string;
|
|
35
|
+
includeTypeInfo?: boolean;
|
|
36
|
+
excludeSystemProperties?: string[];
|
|
37
|
+
}
|
|
38
|
+
export declare function actionTypeQuery(options: ActionTypeQueryOptions): Promise<string>;
|
|
39
|
+
/** Action-type execute: POST (has side effects) */
|
|
40
|
+
export interface ActionTypeExecuteOptions extends OntologyQueryBaseOptions {
|
|
41
|
+
atId: string;
|
|
42
|
+
body: string;
|
|
43
|
+
}
|
|
44
|
+
export declare function actionTypeExecute(options: ActionTypeExecuteOptions): Promise<string>;
|
|
45
|
+
/** Action-execution get: GET */
|
|
46
|
+
export interface ActionExecutionGetOptions extends OntologyQueryBaseOptions {
|
|
47
|
+
executionId: string;
|
|
48
|
+
}
|
|
49
|
+
export declare function actionExecutionGet(options: ActionExecutionGetOptions): Promise<string>;
|
|
50
|
+
/** Action-logs list: GET */
|
|
51
|
+
export interface ActionLogsListOptions extends OntologyQueryBaseOptions {
|
|
52
|
+
actionTypeId?: string;
|
|
53
|
+
status?: string;
|
|
54
|
+
triggerType?: string;
|
|
55
|
+
startTimeFrom?: number;
|
|
56
|
+
startTimeTo?: number;
|
|
57
|
+
limit?: number;
|
|
58
|
+
needTotal?: boolean;
|
|
59
|
+
searchAfter?: string;
|
|
60
|
+
}
|
|
61
|
+
export declare function actionLogsList(options: ActionLogsListOptions): Promise<string>;
|
|
62
|
+
/** Action-log get: GET */
|
|
63
|
+
export interface ActionLogGetOptions extends OntologyQueryBaseOptions {
|
|
64
|
+
logId: string;
|
|
65
|
+
resultsLimit?: number;
|
|
66
|
+
resultsOffset?: number;
|
|
67
|
+
resultsStatus?: "success" | "failed";
|
|
68
|
+
}
|
|
69
|
+
export declare function actionLogGet(options: ActionLogGetOptions): Promise<string>;
|
|
70
|
+
/** Action-log cancel: POST (has side effects) */
|
|
71
|
+
export interface ActionLogCancelOptions extends OntologyQueryBaseOptions {
|
|
72
|
+
logId: string;
|
|
73
|
+
body?: string;
|
|
74
|
+
}
|
|
75
|
+
export declare function actionLogCancel(options: ActionLogCancelOptions): Promise<string>;
|