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
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Publishes the cached service catalog into each request as a sourced context
|
|
3
|
+
* message, on the `agent/pre-step` waterfall.
|
|
4
|
+
*
|
|
5
|
+
* Why a context message rather than the tool descriptions: a tool description is
|
|
6
|
+
* fixed when the plugin loads, and a plugin loads once per PROCESS, not once per
|
|
7
|
+
* session. In a long-running host the TTL would be evaluated exactly once at
|
|
8
|
+
* `apply()` and a service created elsewhere would never be noticed until a
|
|
9
|
+
* restart. Re-registering tools to refresh a description instead invalidates the
|
|
10
|
+
* prompt prefix cache from the first changed schema token. Injecting context
|
|
11
|
+
* keeps the tool schemas byte-stable forever and still refreshes per step.
|
|
12
|
+
*
|
|
13
|
+
* Two hard constraints follow from `agent/pre-step` semantics:
|
|
14
|
+
*
|
|
15
|
+
* 1. `pre-step` fires once per STEP, and a step is one model request — a turn with
|
|
16
|
+
* five tool calls fires it six times. Re-injecting each time would insert six
|
|
17
|
+
* copies into one turn and void the KV cache from the first insertion onward,
|
|
18
|
+
* so change suppression is a correctness requirement, not an optimization.
|
|
19
|
+
* 2. A throwing listener fails the proposed step, i.e. the user's turn stalls.
|
|
20
|
+
* Everything here is therefore wrapped: any failure degrades to "inject
|
|
21
|
+
* nothing this step".
|
|
22
|
+
*/
|
|
23
|
+
import { createUserMessage } from "@deepseek-ai/dsh-llm";
|
|
24
|
+
import { buildServiceCatalog } from "./service-catalog.js";
|
|
25
|
+
/** Marks this plugin's own injections in the durable log. */
|
|
26
|
+
const SOURCE_PLUGIN = "tool-bailian-kb/services";
|
|
27
|
+
/** Whether one durable message came from this module. */
|
|
28
|
+
function isOwnInjection(source) {
|
|
29
|
+
return source.kind === "plugin" && source.plugin === SOURCE_PLUGIN;
|
|
30
|
+
}
|
|
31
|
+
/** Concatenate a message's text parts, which is what the model actually reads. */
|
|
32
|
+
function messageText(message) {
|
|
33
|
+
return message.content.flatMap((part) => (part.type === "text" ? [part.text] : [])).join("\n");
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* The catalog text this session last injected AND still shows the model.
|
|
37
|
+
*
|
|
38
|
+
* The visibility test is the subtle half. Scanning only for "did we ever publish
|
|
39
|
+
* this" would make compaction permanent data loss: once the catalog message is
|
|
40
|
+
* dropped from the surface, an identical digest would suppress every future
|
|
41
|
+
* injection and the model would silently spend the rest of the session without a
|
|
42
|
+
* service list.
|
|
43
|
+
* @param agent - the subject agent.
|
|
44
|
+
* @returns the visible catalog text, or undefined when none is currently visible.
|
|
45
|
+
*/
|
|
46
|
+
function visibleCatalogText(agent) {
|
|
47
|
+
const visible = new Set(agent.session.surface.nodes);
|
|
48
|
+
const events = agent.session.events;
|
|
49
|
+
for (let index = events.length - 1; index >= 0; index -= 1) {
|
|
50
|
+
const event = events[index];
|
|
51
|
+
if (event === undefined)
|
|
52
|
+
continue;
|
|
53
|
+
if (event.type !== "user/message" || !isOwnInjection(event.data.source))
|
|
54
|
+
continue;
|
|
55
|
+
return visible.has(event.seq) ? messageText(event.data) : undefined;
|
|
56
|
+
}
|
|
57
|
+
return undefined;
|
|
58
|
+
}
|
|
59
|
+
/** This module's proposed-but-not-yet-entered message, if the batch already carries one. */
|
|
60
|
+
function pendingCatalog(messages) {
|
|
61
|
+
return messages.find((message) => isOwnInjection(message.source));
|
|
62
|
+
}
|
|
63
|
+
/**
|
|
64
|
+
* Install the pre-step listener that keeps the catalog present and current.
|
|
65
|
+
* @param ctx - a context with `agents` available.
|
|
66
|
+
* @param opts - the cache plus the deployment's resolved workspace and defaults.
|
|
67
|
+
*/
|
|
68
|
+
export function installServiceContext(ctx, opts) {
|
|
69
|
+
ctx.on("agent/pre-step", async ({ agent, signal }, next) => {
|
|
70
|
+
const decision = await next();
|
|
71
|
+
if (decision.kind === "reject" || signal.aborted)
|
|
72
|
+
return decision;
|
|
73
|
+
try {
|
|
74
|
+
const workspaceId = await opts.resolveWorkspaceId();
|
|
75
|
+
// Nothing is configured yet: the tools themselves will explain that.
|
|
76
|
+
if (workspaceId === undefined || workspaceId === "")
|
|
77
|
+
return decision;
|
|
78
|
+
// Refresh scheduling lives here, not at plugin load, so a long-running
|
|
79
|
+
// process still notices services created elsewhere. Never awaited: a slow
|
|
80
|
+
// list request must not delay the user's request.
|
|
81
|
+
if (opts.cache.isStale(workspaceId))
|
|
82
|
+
void opts.cache.refresh();
|
|
83
|
+
const document = opts.cache.peek(workspaceId);
|
|
84
|
+
if (document === undefined)
|
|
85
|
+
return decision;
|
|
86
|
+
const [defaultRetrieveAgentId, defaultChatAgentId] = await Promise.all([
|
|
87
|
+
opts.resolveDefaultRetrieveAgentId(),
|
|
88
|
+
opts.resolveDefaultChatAgentId(),
|
|
89
|
+
]);
|
|
90
|
+
if (signal.aborted)
|
|
91
|
+
return decision;
|
|
92
|
+
const text = buildServiceCatalog({
|
|
93
|
+
entries: document.entries,
|
|
94
|
+
total: document.total,
|
|
95
|
+
truncated: document.truncated,
|
|
96
|
+
...(defaultRetrieveAgentId !== undefined ? { defaultRetrieveAgentId } : {}),
|
|
97
|
+
...(defaultChatAgentId !== undefined ? { defaultChatAgentId } : {}),
|
|
98
|
+
});
|
|
99
|
+
if (text === undefined)
|
|
100
|
+
return decision;
|
|
101
|
+
// Identical to what the model already sees: stay out of the way. This is
|
|
102
|
+
// the branch that runs on nearly every step.
|
|
103
|
+
if (visibleCatalogText(agent) === text)
|
|
104
|
+
return decision;
|
|
105
|
+
const pending = pendingCatalog(decision.messages);
|
|
106
|
+
if (pending !== undefined && messageText(pending) === text)
|
|
107
|
+
return decision;
|
|
108
|
+
const catalog = createUserMessage({
|
|
109
|
+
content: [{ type: "text", text }],
|
|
110
|
+
source: { kind: "plugin", plugin: SOURCE_PLUGIN, form: "catalog" },
|
|
111
|
+
});
|
|
112
|
+
return {
|
|
113
|
+
kind: "enter",
|
|
114
|
+
messages: pending === undefined
|
|
115
|
+
? [...decision.messages, catalog]
|
|
116
|
+
: decision.messages.map((message) => (message.id === pending.id ? catalog : message)),
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
catch (failed) {
|
|
120
|
+
// A throw here would fail the user's step; a missing catalog is far cheaper.
|
|
121
|
+
opts.warn(`bailian-kb service catalog not injected: ${String(failed)}`);
|
|
122
|
+
return decision;
|
|
123
|
+
}
|
|
124
|
+
}, { prepend: true });
|
|
125
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retrieval-service discovery for the plugin's internal cache. Not a model tool:
|
|
3
|
+
* see `KB_PATHS.serviceList`.
|
|
4
|
+
*
|
|
5
|
+
* Listing is paged, and a workspace can hold far more services than are useful
|
|
6
|
+
* for routing, so this module stops after {@link MAX_PAGES} and reports the
|
|
7
|
+
* shortfall rather than paging through everything. Only deployed services are
|
|
8
|
+
* requested: anything else is not callable by the default service version, so it
|
|
9
|
+
* would be noise in the catalog the model reads.
|
|
10
|
+
*/
|
|
11
|
+
import type { ServiceScene } from "./api-types.js";
|
|
12
|
+
import type { KbClient } from "./client.js";
|
|
13
|
+
/** One deployed retrieval or Q&A service, reduced to the fields that inform routing. */
|
|
14
|
+
export interface ServiceEntry {
|
|
15
|
+
agent_id: string;
|
|
16
|
+
agent_name: string;
|
|
17
|
+
scene: ServiceScene;
|
|
18
|
+
/** Deployment status as reported for the service; only callable states are listed. */
|
|
19
|
+
status: string;
|
|
20
|
+
/** Last modification timestamp; the only signal for "which of these is in use". */
|
|
21
|
+
modify_time?: string;
|
|
22
|
+
/** Service description, when the listing provides one. */
|
|
23
|
+
description?: string;
|
|
24
|
+
}
|
|
25
|
+
export interface ServiceList {
|
|
26
|
+
entries: ServiceEntry[];
|
|
27
|
+
/** Server-reported total across the queried scenes, including rows never fetched. */
|
|
28
|
+
total: number;
|
|
29
|
+
/** True when a scene reported more rows than {@link MAX_PAGES} pages returned. */
|
|
30
|
+
truncated: boolean;
|
|
31
|
+
/** Scenes whose query failed; a partial list stays usable. */
|
|
32
|
+
failedScenes: ServiceScene[];
|
|
33
|
+
}
|
|
34
|
+
/**
|
|
35
|
+
* List the deployed services of both scenes.
|
|
36
|
+
* A scene that fails is recorded and skipped rather than failing the whole
|
|
37
|
+
* refresh: half a list still routes better than none.
|
|
38
|
+
* @param client - the shared knowledge API client.
|
|
39
|
+
* @returns merged entries plus totals, truncation, and per-scene failures.
|
|
40
|
+
*/
|
|
41
|
+
export declare function listServices(client: KbClient): Promise<ServiceList>;
|
package/dist/services.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Retrieval-service discovery for the plugin's internal cache. Not a model tool:
|
|
3
|
+
* see `KB_PATHS.serviceList`.
|
|
4
|
+
*
|
|
5
|
+
* Listing is paged, and a workspace can hold far more services than are useful
|
|
6
|
+
* for routing, so this module stops after {@link MAX_PAGES} and reports the
|
|
7
|
+
* shortfall rather than paging through everything. Only deployed services are
|
|
8
|
+
* requested: anything else is not callable by the default service version, so it
|
|
9
|
+
* would be noise in the catalog the model reads.
|
|
10
|
+
*/
|
|
11
|
+
import { KB_PATHS } from "./endpoints.js";
|
|
12
|
+
/** Page size requested per listing call. */
|
|
13
|
+
const PAGE_SIZE = 100;
|
|
14
|
+
/** Pages fetched per scene before reporting truncation; well past the useful routing range. */
|
|
15
|
+
const MAX_PAGES = 2;
|
|
16
|
+
const SCENES = ["search", "chat"];
|
|
17
|
+
/**
|
|
18
|
+
* Fetch the deployed services of one scene, stopping at the page cap.
|
|
19
|
+
* @param client - the shared knowledge API client.
|
|
20
|
+
* @param scene - `search` or `chat`.
|
|
21
|
+
* @returns the scene's entries, its server-reported total, and whether rows were left unfetched.
|
|
22
|
+
*/
|
|
23
|
+
async function listScene(client, scene) {
|
|
24
|
+
const entries = [];
|
|
25
|
+
let total = 0;
|
|
26
|
+
for (let page = 1; page <= MAX_PAGES; page += 1) {
|
|
27
|
+
const res = await client.postJson(KB_PATHS.serviceList, {
|
|
28
|
+
agent_scene: scene,
|
|
29
|
+
agent_status: "deployed",
|
|
30
|
+
page_number: page,
|
|
31
|
+
page_size: PAGE_SIZE,
|
|
32
|
+
});
|
|
33
|
+
total = res.data?.total_count ?? total;
|
|
34
|
+
const rows = res.data?.rows ?? [];
|
|
35
|
+
for (const row of rows) {
|
|
36
|
+
const agentId = row.agent_id ?? "";
|
|
37
|
+
// A row without an id cannot be called, so it has no reason to exist here.
|
|
38
|
+
if (agentId === "")
|
|
39
|
+
continue;
|
|
40
|
+
entries.push({
|
|
41
|
+
agent_id: agentId,
|
|
42
|
+
agent_name: row.agent_name ?? "",
|
|
43
|
+
scene,
|
|
44
|
+
status: row.agent_status ?? "",
|
|
45
|
+
...(typeof row.modify_time === "string" ? { modify_time: row.modify_time } : {}),
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
// A short page is the last page; the server has nothing further to give.
|
|
49
|
+
if (rows.length < PAGE_SIZE)
|
|
50
|
+
return { entries, total, truncated: false };
|
|
51
|
+
}
|
|
52
|
+
return { entries, total, truncated: total > entries.length };
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* List the deployed services of both scenes.
|
|
56
|
+
* A scene that fails is recorded and skipped rather than failing the whole
|
|
57
|
+
* refresh: half a list still routes better than none.
|
|
58
|
+
* @param client - the shared knowledge API client.
|
|
59
|
+
* @returns merged entries plus totals, truncation, and per-scene failures.
|
|
60
|
+
*/
|
|
61
|
+
export async function listServices(client) {
|
|
62
|
+
const entries = [];
|
|
63
|
+
const failedScenes = [];
|
|
64
|
+
let total = 0;
|
|
65
|
+
let truncated = false;
|
|
66
|
+
for (const scene of SCENES) {
|
|
67
|
+
try {
|
|
68
|
+
const result = await listScene(client, scene);
|
|
69
|
+
entries.push(...result.entries);
|
|
70
|
+
total += result.total;
|
|
71
|
+
truncated = truncated || result.truncated;
|
|
72
|
+
}
|
|
73
|
+
catch (_sceneFailed) {
|
|
74
|
+
failedScenes.push(scene);
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
return { entries, total, truncated, failedScenes };
|
|
78
|
+
}
|
package/dist/skill.d.ts
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime skill registration: the packaged bailian-kb SKILL.md joins the catalog
|
|
3
|
+
* when a skills registry is composed.
|
|
4
|
+
*
|
|
5
|
+
* The file's YAML frontmatter is the single source of truth for the routing name
|
|
6
|
+
* and description — duplicating them here drifts, and the copy that loses is the
|
|
7
|
+
* one nobody reads. The frontmatter must also be STRIPPED from the registered
|
|
8
|
+
* body: `SkillDefinition.content` is contractually the body a provider has
|
|
9
|
+
* already cleaned of its own metadata, and `ctx.skills.register()` parses
|
|
10
|
+
* nothing, so handing over the raw file ships the YAML block into the model's
|
|
11
|
+
* context.
|
|
12
|
+
*/
|
|
13
|
+
import type { Context } from "@deepseek-ai/cordis";
|
|
14
|
+
/** The registrable fields carried by one skill file. */
|
|
15
|
+
export interface ParsedSkillFile {
|
|
16
|
+
/** Kebab-case skill name from frontmatter. */
|
|
17
|
+
name: string;
|
|
18
|
+
/** Routing description from frontmatter (the catalog truncates at 500 chars). */
|
|
19
|
+
description: string;
|
|
20
|
+
/** Optional extra routing guidance. */
|
|
21
|
+
whenToUse?: string;
|
|
22
|
+
/** Optional frontmatter `metadata` object. */
|
|
23
|
+
metadata?: Record<string, unknown>;
|
|
24
|
+
/** Markdown body with the frontmatter block removed. */
|
|
25
|
+
content: string;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Parse one skill file into its registrable fields.
|
|
29
|
+
* @param raw - the file's full text.
|
|
30
|
+
* @returns the parsed fields, or undefined when the frontmatter is absent, unparsable, or missing name/description.
|
|
31
|
+
*/
|
|
32
|
+
export declare function parseSkillFile(raw: string): ParsedSkillFile | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Register the management skill when the skills registry is composed; headless
|
|
35
|
+
* assemblies without the seam stay unaffected. An unreadable or malformed file
|
|
36
|
+
* degrades to a warning — a broken bundled asset must not fail plugin load.
|
|
37
|
+
* @param ctx - the plugin context.
|
|
38
|
+
*/
|
|
39
|
+
export declare function registerSkill(ctx: Context): void;
|
package/dist/skill.js
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Runtime skill registration: the packaged bailian-kb SKILL.md joins the catalog
|
|
3
|
+
* when a skills registry is composed.
|
|
4
|
+
*
|
|
5
|
+
* The file's YAML frontmatter is the single source of truth for the routing name
|
|
6
|
+
* and description — duplicating them here drifts, and the copy that loses is the
|
|
7
|
+
* one nobody reads. The frontmatter must also be STRIPPED from the registered
|
|
8
|
+
* body: `SkillDefinition.content` is contractually the body a provider has
|
|
9
|
+
* already cleaned of its own metadata, and `ctx.skills.register()` parses
|
|
10
|
+
* nothing, so handing over the raw file ships the YAML block into the model's
|
|
11
|
+
* context.
|
|
12
|
+
*/
|
|
13
|
+
import { readFileSync } from "node:fs";
|
|
14
|
+
import { join } from "node:path";
|
|
15
|
+
import { fileURLToPath } from "node:url";
|
|
16
|
+
import { parse as parseYaml } from "yaml";
|
|
17
|
+
const SKILL_DIR = fileURLToPath(new URL("../skills/bailian-kb/", import.meta.url));
|
|
18
|
+
/**
|
|
19
|
+
* Locate the frontmatter block, mirroring the filesystem provider's delimiters
|
|
20
|
+
* so a file that loads from disk behaves identically when bundled.
|
|
21
|
+
* @param raw - the file's full text.
|
|
22
|
+
* @returns the frontmatter YAML and the body after it, or undefined when unfenced.
|
|
23
|
+
*/
|
|
24
|
+
function splitFrontmatter(raw) {
|
|
25
|
+
const firstLineEnd = raw.indexOf("\n");
|
|
26
|
+
if (firstLineEnd < 0)
|
|
27
|
+
return undefined;
|
|
28
|
+
if (raw.slice(0, firstLineEnd).replace(/\r$/, "") !== "---")
|
|
29
|
+
return undefined;
|
|
30
|
+
const start = firstLineEnd + 1;
|
|
31
|
+
let lineStart = start;
|
|
32
|
+
while (lineStart <= raw.length) {
|
|
33
|
+
const nextNewline = raw.indexOf("\n", lineStart);
|
|
34
|
+
const lineEnd = nextNewline < 0 ? raw.length : nextNewline;
|
|
35
|
+
if (raw.slice(lineStart, lineEnd).replace(/\r$/, "") === "---") {
|
|
36
|
+
return {
|
|
37
|
+
yaml: raw.slice(start, lineStart),
|
|
38
|
+
body: raw.slice(nextNewline < 0 ? raw.length : nextNewline + 1),
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
if (nextNewline < 0)
|
|
42
|
+
return undefined;
|
|
43
|
+
lineStart = nextNewline + 1;
|
|
44
|
+
}
|
|
45
|
+
return undefined;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Parse one skill file into its registrable fields.
|
|
49
|
+
* @param raw - the file's full text.
|
|
50
|
+
* @returns the parsed fields, or undefined when the frontmatter is absent, unparsable, or missing name/description.
|
|
51
|
+
*/
|
|
52
|
+
export function parseSkillFile(raw) {
|
|
53
|
+
const split = splitFrontmatter(raw);
|
|
54
|
+
if (split === undefined)
|
|
55
|
+
return undefined;
|
|
56
|
+
let data;
|
|
57
|
+
try {
|
|
58
|
+
data = parseYaml(split.yaml);
|
|
59
|
+
}
|
|
60
|
+
catch (_invalidYaml) {
|
|
61
|
+
return undefined;
|
|
62
|
+
}
|
|
63
|
+
if (typeof data !== "object" || data === null || Array.isArray(data))
|
|
64
|
+
return undefined;
|
|
65
|
+
const record = data;
|
|
66
|
+
const name = typeof record.name === "string" ? record.name.trim() : "";
|
|
67
|
+
const description = typeof record.description === "string" ? record.description.trim() : "";
|
|
68
|
+
// The registry rejects a blank description outright; failing here keeps the
|
|
69
|
+
// diagnostic on the file instead of on the registration call.
|
|
70
|
+
if (name === "" || description === "")
|
|
71
|
+
return undefined;
|
|
72
|
+
const whenToUse = typeof record.whenToUse === "string" ? record.whenToUse.trim() : "";
|
|
73
|
+
const metadata = typeof record.metadata === "object" &&
|
|
74
|
+
record.metadata !== null &&
|
|
75
|
+
!Array.isArray(record.metadata)
|
|
76
|
+
? record.metadata
|
|
77
|
+
: undefined;
|
|
78
|
+
return {
|
|
79
|
+
name,
|
|
80
|
+
description,
|
|
81
|
+
...(whenToUse !== "" ? { whenToUse } : {}),
|
|
82
|
+
...(metadata !== undefined ? { metadata } : {}),
|
|
83
|
+
content: split.body,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Register the management skill when the skills registry is composed; headless
|
|
88
|
+
* assemblies without the seam stay unaffected. An unreadable or malformed file
|
|
89
|
+
* degrades to a warning — a broken bundled asset must not fail plugin load.
|
|
90
|
+
* @param ctx - the plugin context.
|
|
91
|
+
*/
|
|
92
|
+
export function registerSkill(ctx) {
|
|
93
|
+
ctx.inject(["skills"], (skillCtx) => {
|
|
94
|
+
const path = join(SKILL_DIR, "SKILL.md");
|
|
95
|
+
let raw;
|
|
96
|
+
try {
|
|
97
|
+
raw = readFileSync(path, "utf8");
|
|
98
|
+
}
|
|
99
|
+
catch (unreadable) {
|
|
100
|
+
skillCtx.logger.warn(`bailian-kb skill not registered: cannot read ${path}: ${String(unreadable)}`);
|
|
101
|
+
return;
|
|
102
|
+
}
|
|
103
|
+
const parsed = parseSkillFile(raw);
|
|
104
|
+
if (parsed === undefined) {
|
|
105
|
+
skillCtx.logger.warn(`bailian-kb skill not registered: ${path} needs YAML frontmatter carrying name and description`);
|
|
106
|
+
return;
|
|
107
|
+
}
|
|
108
|
+
skillCtx.skills.register({
|
|
109
|
+
name: parsed.name,
|
|
110
|
+
description: parsed.description,
|
|
111
|
+
...(parsed.whenToUse !== undefined ? { whenToUse: parsed.whenToUse } : {}),
|
|
112
|
+
...(parsed.metadata !== undefined ? { metadata: parsed.metadata } : {}),
|
|
113
|
+
content: parsed.content,
|
|
114
|
+
source: "bundled",
|
|
115
|
+
path,
|
|
116
|
+
resourceBase: { kind: "directory", path: SKILL_DIR },
|
|
117
|
+
});
|
|
118
|
+
});
|
|
119
|
+
}
|
package/dist/sse.d.ts
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
/** Minimal SSE parser for the knowledge chat stream: `event:`/`data:` lines, events split on blank lines. */
|
|
2
|
+
export interface SseEvent {
|
|
3
|
+
event?: string;
|
|
4
|
+
data: string;
|
|
5
|
+
}
|
|
6
|
+
/**
|
|
7
|
+
* Parse one SSE byte stream into events.
|
|
8
|
+
* @param body - the response body stream.
|
|
9
|
+
* @returns events in stream order; multi-`data:` events join with newlines per the SSE spec.
|
|
10
|
+
*/
|
|
11
|
+
export declare function parseSseStream(body: ReadableStream<Uint8Array>): AsyncGenerator<SseEvent>;
|
package/dist/sse.js
ADDED
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
/** Minimal SSE parser for the knowledge chat stream: `event:`/`data:` lines, events split on blank lines. */
|
|
2
|
+
/**
|
|
3
|
+
* Parse one SSE byte stream into events.
|
|
4
|
+
* @param body - the response body stream.
|
|
5
|
+
* @returns events in stream order; multi-`data:` events join with newlines per the SSE spec.
|
|
6
|
+
*/
|
|
7
|
+
export async function* parseSseStream(body) {
|
|
8
|
+
const decoder = new TextDecoder();
|
|
9
|
+
let buffer = "";
|
|
10
|
+
let event;
|
|
11
|
+
let data = [];
|
|
12
|
+
const flush = () => {
|
|
13
|
+
if (data.length === 0)
|
|
14
|
+
return undefined;
|
|
15
|
+
const out = { event, data: data.join("\n") };
|
|
16
|
+
event = undefined;
|
|
17
|
+
data = [];
|
|
18
|
+
return out;
|
|
19
|
+
};
|
|
20
|
+
const reader = body.getReader();
|
|
21
|
+
while (true) {
|
|
22
|
+
const { done, value } = await reader.read();
|
|
23
|
+
buffer += done ? "" : decoder.decode(value, { stream: true });
|
|
24
|
+
let newline;
|
|
25
|
+
while ((newline = buffer.indexOf("\n")) !== -1) {
|
|
26
|
+
const line = buffer.slice(0, newline).replace(/\r$/, "");
|
|
27
|
+
buffer = buffer.slice(newline + 1);
|
|
28
|
+
if (line === "") {
|
|
29
|
+
const out = flush();
|
|
30
|
+
if (out)
|
|
31
|
+
yield out;
|
|
32
|
+
}
|
|
33
|
+
else if (line.startsWith("event:")) {
|
|
34
|
+
event = line.slice(6).trim();
|
|
35
|
+
}
|
|
36
|
+
else if (line.startsWith("data:")) {
|
|
37
|
+
data.push(line.slice(5).trimStart());
|
|
38
|
+
}
|
|
39
|
+
// comment/id/retry lines are irrelevant to this API and are skipped
|
|
40
|
+
}
|
|
41
|
+
if (done) {
|
|
42
|
+
const out = flush();
|
|
43
|
+
if (out)
|
|
44
|
+
yield out;
|
|
45
|
+
return;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
}
|
package/dist/tools.d.ts
ADDED
|
@@ -0,0 +1,38 @@
|
|
|
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 { type KbClient } from "./client.js";
|
|
17
|
+
export interface KbToolDeps {
|
|
18
|
+
client: KbClient;
|
|
19
|
+
/** Resolves the default retrieval agent id per call (settings/patch config or credential); omitted means no default for kb_search. */
|
|
20
|
+
resolveDefaultRetrieveAgentId?: () => Promise<string | undefined>;
|
|
21
|
+
/** Resolves the default chat agent id per call (settings/patch config or credential); omitted means no default for kb_chat. */
|
|
22
|
+
resolveDefaultChatAgentId?: () => Promise<string | undefined>;
|
|
23
|
+
/**
|
|
24
|
+
* Refreshes the service cache and summarizes what the workspace currently
|
|
25
|
+
* deploys for one scene. Called only after a client-side API failure, so a
|
|
26
|
+
* stale cached id self-corrects within the same step instead of waiting for the
|
|
27
|
+
* next scheduled refresh.
|
|
28
|
+
*/
|
|
29
|
+
describeServicesAfterRefresh?: (scene: "search" | "chat") => Promise<string | undefined>;
|
|
30
|
+
/** Read per call (a live-settings deployment supplies a getter). */
|
|
31
|
+
chatTimeoutMs: number;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Build the two tool definitions over one shared client.
|
|
35
|
+
* @param deps - client plus the deployment's explicit pinning and timeout choices.
|
|
36
|
+
* @returns definitions ready for `ctx.tools.register()`.
|
|
37
|
+
*/
|
|
38
|
+
export declare function createKbTools(deps: KbToolDeps): import("@deepseek-ai/dsh-tools").ToolDefinition[];
|