bailian-kb-dsh 0.1.18
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 +169 -0
- package/cordis.patch.yml +13 -0
- package/dist/api-types.d.ts +89 -0
- package/dist/api-types.js +2 -0
- package/dist/bl-cli.d.ts +28 -0
- package/dist/bl-cli.js +46 -0
- package/dist/chat.d.ts +11 -0
- package/dist/chat.js +44 -0
- package/dist/client.d.ts +40 -0
- package/dist/client.js +76 -0
- package/dist/console-login.d.ts +83 -0
- package/dist/console-login.js +263 -0
- package/dist/endpoints.d.ts +20 -0
- package/dist/endpoints.js +22 -0
- package/dist/index.d.ts +65 -0
- package/dist/index.js +557 -0
- package/dist/service-cache.d.ts +151 -0
- package/dist/service-cache.js +234 -0
- package/dist/service-catalog.d.ts +34 -0
- package/dist/service-catalog.js +115 -0
- package/dist/service-context.d.ts +40 -0
- package/dist/service-context.js +125 -0
- package/dist/services.d.ts +41 -0
- package/dist/services.js +78 -0
- package/dist/skill.d.ts +39 -0
- package/dist/skill.js +119 -0
- package/dist/sse.d.ts +11 -0
- package/dist/sse.js +48 -0
- package/dist/tools.d.ts +38 -0
- package/dist/tools.js +244 -0
- package/dist/web/client.js +1052 -0
- package/dist/web/client.js.map +1 -0
- package/package.json +118 -0
- package/skills/bailian-kb/SKILL.md +96 -0
package/dist/tools.js
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The two model-facing knowledge tools (kb_search, kb_chat). agent_id is REQUIRED in the schema:
|
|
3
|
+
* a model cannot know from the tool spec whether this deployment configures a default service, and a
|
|
4
|
+
* missing default previously only surfaced at call time, forcing a wasted round-trip. The per-call
|
|
5
|
+
* fallback to a configured default (settings/config or credential) is retained as defense-in-depth,
|
|
6
|
+
* but note defineTool validates args against the schema before execute, so through that entry point
|
|
7
|
+
* the fallback is inert; the model-facing contract is explicit.
|
|
8
|
+
*
|
|
9
|
+
* These descriptions are deliberately STATIC. The available service ids are
|
|
10
|
+
* deployment state that changes while the process runs, and re-registering a tool
|
|
11
|
+
* to refresh its description invalidates the prompt prefix cache from the first
|
|
12
|
+
* changed schema token. The live catalog therefore rides an `agent/pre-step`
|
|
13
|
+
* context message instead (see `service-context.ts`), leaving these schemas
|
|
14
|
+
* byte-stable for the life of the process.
|
|
15
|
+
*/
|
|
16
|
+
import { defineTool } from "@deepseek-ai/dsh-tools";
|
|
17
|
+
import { KbApiError } from "./client.js";
|
|
18
|
+
import { consumeChatStream } from "./chat.js";
|
|
19
|
+
import { KB_PATHS } from "./endpoints.js";
|
|
20
|
+
/** Client-side chunk cap applied when the model omits top_k. */
|
|
21
|
+
const DEFAULT_TOP_K = 5;
|
|
22
|
+
/**
|
|
23
|
+
* Rethrow an API failure, appending a freshly refreshed service list when the
|
|
24
|
+
* server rejected the request.
|
|
25
|
+
*
|
|
26
|
+
* On these two endpoints `agent_id` is the only caller-supplied identifier, so a
|
|
27
|
+
* 4xx is most often a service id that no longer exists — the recovery the model
|
|
28
|
+
* needs is the current list, delivered in the error message. The message is
|
|
29
|
+
* ordinary conversation text appended at the tail, so unlike a re-registered
|
|
30
|
+
* description it does not disturb the request prefix.
|
|
31
|
+
*/
|
|
32
|
+
async function withServiceHint(err, scene, describe) {
|
|
33
|
+
if (describe === undefined || !(err instanceof KbApiError))
|
|
34
|
+
throw err;
|
|
35
|
+
const status = err.status;
|
|
36
|
+
if (status === undefined || status < 400 || status >= 500)
|
|
37
|
+
throw err;
|
|
38
|
+
// Best-effort enrichment: a failing refresh must not replace the real error.
|
|
39
|
+
const summary = await describe(scene).catch(() => undefined);
|
|
40
|
+
if (summary === undefined)
|
|
41
|
+
throw err;
|
|
42
|
+
throw new KbApiError(`${err.message}
|
|
43
|
+
|
|
44
|
+
${summary}`, status);
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Build the two tool definitions over one shared client.
|
|
48
|
+
* @param deps - client plus the deployment's explicit pinning and timeout choices.
|
|
49
|
+
* @returns definitions ready for `ctx.tools.register()`.
|
|
50
|
+
*/
|
|
51
|
+
export function createKbTools(deps) {
|
|
52
|
+
// chatTimeoutMs is deliberately NOT destructured: reading it off deps at
|
|
53
|
+
// execute time keeps a live-settings getter live.
|
|
54
|
+
const { client, resolveDefaultRetrieveAgentId, resolveDefaultChatAgentId } = deps;
|
|
55
|
+
const agentIdParam = {
|
|
56
|
+
type: "string",
|
|
57
|
+
required: true,
|
|
58
|
+
description: "Retrieval/Q&A service id. REQUIRED: the schema cannot know whether this deployment " +
|
|
59
|
+
"configures a default service, so always pass one. The deployed services of this workspace, " +
|
|
60
|
+
"with their ids, are listed in a context message in this conversation; take the id from the " +
|
|
61
|
+
"section matching the tool you are calling. If that list is absent or none of its services " +
|
|
62
|
+
"covers the question, run `bl knowledge service list --scene search --name <keyword>` to look " +
|
|
63
|
+
"(workspaceId resolves automatically from DSH settings: bailian-kb.workspaceId in " +
|
|
64
|
+
"~/.dsh/settings.yaml).",
|
|
65
|
+
};
|
|
66
|
+
const resolveRetrieveAgentId = async (supplied) => {
|
|
67
|
+
if (supplied !== undefined)
|
|
68
|
+
return supplied;
|
|
69
|
+
const defaultId = resolveDefaultRetrieveAgentId === undefined
|
|
70
|
+
? undefined
|
|
71
|
+
: await resolveDefaultRetrieveAgentId();
|
|
72
|
+
if (defaultId === undefined) {
|
|
73
|
+
throw new Error("agent_id is required: no default retrieval service is configured. Pass agent_id explicitly " +
|
|
74
|
+
"(find ids: `bl knowledge service list --scene search --workspace-id <workspaceId>`), or configure a " +
|
|
75
|
+
"default: bailian-kb.defaultRetrieveAgentId in ~/.dsh/settings.yaml or " +
|
|
76
|
+
"BAILIAN_DEFAULT_RETRIEVE_AGENT_ID in ~/.dsh/.credentials.yaml.");
|
|
77
|
+
}
|
|
78
|
+
return defaultId;
|
|
79
|
+
};
|
|
80
|
+
const resolveChatAgentId = async (supplied) => {
|
|
81
|
+
if (supplied !== undefined)
|
|
82
|
+
return supplied;
|
|
83
|
+
const defaultId = resolveDefaultChatAgentId === undefined ? undefined : await resolveDefaultChatAgentId();
|
|
84
|
+
if (defaultId === undefined) {
|
|
85
|
+
throw new Error("agent_id is required: no default chat service is configured. Pass agent_id explicitly " +
|
|
86
|
+
"(find ids: `bl knowledge service list --scene chat --workspace-id <workspaceId>`), or configure a " +
|
|
87
|
+
"default: bailian-kb.defaultChatAgentId in ~/.dsh/settings.yaml or " +
|
|
88
|
+
"BAILIAN_DEFAULT_CHAT_AGENT_ID in ~/.dsh/.credentials.yaml.");
|
|
89
|
+
}
|
|
90
|
+
return defaultId;
|
|
91
|
+
};
|
|
92
|
+
const search = defineTool({
|
|
93
|
+
name: "kb_search",
|
|
94
|
+
description: "Semantic search over a Bailian knowledge base. Returns raw knowledge chunks with scores and source " +
|
|
95
|
+
"references for you to verify, cite, or combine with other context. Retrieval scope and strategy " +
|
|
96
|
+
"(multi-KB weighting, routing, reranking) come from the service configuration. " +
|
|
97
|
+
"top_k caps how many chunks return (client-side cut of the score-ranked results). " +
|
|
98
|
+
"Use kb_chat instead when the user question can be answered by the knowledge base alone. " +
|
|
99
|
+
"Credentials and workspace resolve automatically from DSH config " +
|
|
100
|
+
"(bailian-kb in ~/.dsh/settings.yaml, DASHSCOPE_API_KEY in ~/.dsh/.credentials.yaml) — " +
|
|
101
|
+
"never read or pass them yourself. agent_id is REQUIRED (see its parameter description). " +
|
|
102
|
+
"If no listed service covers what the user is asking about, say so plainly rather than trying " +
|
|
103
|
+
"the closest-looking id: unrelated evidence is worse for the user than none.",
|
|
104
|
+
parameters: {
|
|
105
|
+
query: { type: "string", required: true, description: "Search query text." },
|
|
106
|
+
agent_id: agentIdParam,
|
|
107
|
+
top_k: {
|
|
108
|
+
type: "integer",
|
|
109
|
+
description: `Maximum chunks to return; defaults to ${DEFAULT_TOP_K}.`,
|
|
110
|
+
},
|
|
111
|
+
images: {
|
|
112
|
+
type: "array",
|
|
113
|
+
items: { type: "string" },
|
|
114
|
+
description: "Image URLs for multimodal retrieval.",
|
|
115
|
+
},
|
|
116
|
+
},
|
|
117
|
+
output: {
|
|
118
|
+
schema: {
|
|
119
|
+
type: "object",
|
|
120
|
+
additionalProperties: false,
|
|
121
|
+
properties: {
|
|
122
|
+
chunks: {
|
|
123
|
+
type: "array",
|
|
124
|
+
required: true,
|
|
125
|
+
items: {
|
|
126
|
+
type: "object",
|
|
127
|
+
additionalProperties: false,
|
|
128
|
+
properties: {
|
|
129
|
+
text: { type: "string", required: true },
|
|
130
|
+
score: { type: "number", required: true },
|
|
131
|
+
doc_name: { type: "string" },
|
|
132
|
+
doc_id: { type: "string" },
|
|
133
|
+
title: { type: "string" },
|
|
134
|
+
},
|
|
135
|
+
},
|
|
136
|
+
},
|
|
137
|
+
total: { type: "integer", required: true },
|
|
138
|
+
},
|
|
139
|
+
},
|
|
140
|
+
render: (_args, value) => [
|
|
141
|
+
{
|
|
142
|
+
type: "text",
|
|
143
|
+
text: value.chunks.length === 0
|
|
144
|
+
? "No matching knowledge chunks."
|
|
145
|
+
: value.chunks
|
|
146
|
+
.map((c, i) => `[${i + 1}] (score ${c.score.toFixed(2)}${c.doc_name ? `, ${c.doc_name}` : ""}) ${c.text}`)
|
|
147
|
+
.join("\n"),
|
|
148
|
+
},
|
|
149
|
+
],
|
|
150
|
+
},
|
|
151
|
+
async execute(args) {
|
|
152
|
+
const topK = args.top_k ?? DEFAULT_TOP_K;
|
|
153
|
+
const body = {
|
|
154
|
+
query: args.query,
|
|
155
|
+
agent_id: await resolveRetrieveAgentId(args.agent_id),
|
|
156
|
+
...(client.agentVersion ? { agent_version: client.agentVersion } : {}),
|
|
157
|
+
...(args.images && args.images.length > 0 ? { images: args.images } : {}),
|
|
158
|
+
};
|
|
159
|
+
const res = await client
|
|
160
|
+
.postJson(KB_PATHS.search, body)
|
|
161
|
+
.catch(async (err) => await withServiceHint(err, "search", deps.describeServicesAfterRefresh));
|
|
162
|
+
const nodes = (res.data?.nodes ?? []).slice(0, topK);
|
|
163
|
+
return {
|
|
164
|
+
chunks: nodes.map((n) => ({
|
|
165
|
+
text: n.text,
|
|
166
|
+
score: n.score,
|
|
167
|
+
...(typeof n.metadata?.doc_name === "string" ? { doc_name: n.metadata.doc_name } : {}),
|
|
168
|
+
...(typeof n.metadata?.doc_id === "string" ? { doc_id: n.metadata.doc_id } : {}),
|
|
169
|
+
...(typeof n.metadata?.title === "string" ? { title: n.metadata.title } : {}),
|
|
170
|
+
})),
|
|
171
|
+
total: res.data?.total ?? nodes.length,
|
|
172
|
+
};
|
|
173
|
+
},
|
|
174
|
+
presentCall: (args) => ({
|
|
175
|
+
card: "generic",
|
|
176
|
+
title: "Search knowledge base",
|
|
177
|
+
kind: "search",
|
|
178
|
+
rawInput: args,
|
|
179
|
+
}),
|
|
180
|
+
});
|
|
181
|
+
const chat = defineTool({
|
|
182
|
+
name: "kb_chat",
|
|
183
|
+
description: "Ask the knowledge base directly and get a complete, domain-tuned answer from a specialized RAG pipeline " +
|
|
184
|
+
"(multi-round retrieval + reranking + grounded generation). For knowledge Q&A this typically outperforms " +
|
|
185
|
+
"searching and synthesizing yourself when the question can be answered by the knowledge base alone; " +
|
|
186
|
+
"use kb_search instead when you need raw chunks to verify, cite, or combine with other work. " +
|
|
187
|
+
"The pipeline runs an internal analysis/retrieval loop and may take a few minutes. " +
|
|
188
|
+
"Credentials and workspace resolve automatically from DSH config " +
|
|
189
|
+
"(bailian-kb in ~/.dsh/settings.yaml, DASHSCOPE_API_KEY in ~/.dsh/.credentials.yaml) — " +
|
|
190
|
+
"never read or pass them yourself. agent_id is REQUIRED (see its parameter description). " +
|
|
191
|
+
"If no listed service covers what the user is asking about, say so plainly rather than trying " +
|
|
192
|
+
"the closest-looking id.",
|
|
193
|
+
parameters: {
|
|
194
|
+
message: { type: "string", required: true, description: "The question to ask." },
|
|
195
|
+
agent_id: agentIdParam,
|
|
196
|
+
},
|
|
197
|
+
output: {
|
|
198
|
+
schema: {
|
|
199
|
+
type: "object",
|
|
200
|
+
additionalProperties: false,
|
|
201
|
+
properties: {
|
|
202
|
+
answer: { type: "string", required: true },
|
|
203
|
+
request_id: { type: "string" },
|
|
204
|
+
},
|
|
205
|
+
},
|
|
206
|
+
render: (_args, value) => [
|
|
207
|
+
{ type: "text", text: value.answer.length === 0 ? "(empty answer)" : value.answer },
|
|
208
|
+
],
|
|
209
|
+
},
|
|
210
|
+
async execute(args) {
|
|
211
|
+
const chatTimeoutMs = deps.chatTimeoutMs;
|
|
212
|
+
const body = {
|
|
213
|
+
input: { messages: [{ role: "user", content: args.message }] },
|
|
214
|
+
parameters: {
|
|
215
|
+
agent_options: {
|
|
216
|
+
agent_id: await resolveChatAgentId(args.agent_id),
|
|
217
|
+
...(client.agentVersion ? { agent_version: client.agentVersion } : {}),
|
|
218
|
+
},
|
|
219
|
+
},
|
|
220
|
+
stream: true,
|
|
221
|
+
};
|
|
222
|
+
let res;
|
|
223
|
+
try {
|
|
224
|
+
res = await client.postSse(KB_PATHS.chat, body, AbortSignal.timeout(chatTimeoutMs));
|
|
225
|
+
}
|
|
226
|
+
catch (err) {
|
|
227
|
+
if (err instanceof Error && err.name === "TimeoutError") {
|
|
228
|
+
throw new Error(`knowledge chat timed out after ${chatTimeoutMs}ms; the pipeline runs a multi-round retrieval loop ` +
|
|
229
|
+
"and long questions can exceed the deployment timeout. Retry, or use kb_search for raw chunks instead.");
|
|
230
|
+
}
|
|
231
|
+
return await withServiceHint(err, "chat", deps.describeServicesAfterRefresh);
|
|
232
|
+
}
|
|
233
|
+
const { answer, requestId } = await consumeChatStream(res);
|
|
234
|
+
return { answer, ...(requestId ? { request_id: requestId } : {}) };
|
|
235
|
+
},
|
|
236
|
+
presentCall: (args) => ({
|
|
237
|
+
card: "generic",
|
|
238
|
+
title: "Ask knowledge base (may take a few minutes)",
|
|
239
|
+
kind: "fetch",
|
|
240
|
+
rawInput: args,
|
|
241
|
+
}),
|
|
242
|
+
});
|
|
243
|
+
return [search, chat];
|
|
244
|
+
}
|