@morlay/dsh-reference-injection 0.0.2-alpha.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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2025 morlay
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # @morlay/dsh-reference-injection
2
+
3
+ 把用户消息里的 skill 引用在 `agent/pre-step` 边界展开成 `<skill_content>` 注入消息:引用由 client 面同一份
4
+ 统一解析(`@morlay/dsh-client-ui-primitives` 的 `findReferences`)认领,消息文本本身不动。
5
+
6
+ ## 行为
7
+
8
+ - 只扫描本步 claimed 的 `source.kind === 'user'` 消息(外部文本伪造不了这个手势);
9
+ - 认领所有引用形态:`skill:name`、`@skill:name`、`[label](skill:name)`、`@[label](skill:name)`;
10
+ 手写的 inline code(`` `skill:name` ``)由 `parseReferenceToken` 再兜一次;
11
+ 代码块内的同形文本不会命中(解析器保证);
12
+ - 名字不在 skill 注册表里、或该 skill 不允许用户调用时保持普通文本;
13
+ - 去重按首次出现顺序;每个名字注入一条 instructions 形态的消息,追加在本步已有注入之后。
14
+
15
+ ## 装配
16
+
17
+ 部署侧(preset / profile bundle)插入一行即可;本部署的装配在 `@morlay/dsh-preset` 的 bundle patch 里
18
+ (`packages/preset/dsh-preset/cordis.patch.yml`)。
19
+
20
+ ## 范围
21
+
22
+ 只处理 `protocol === 'skill'` 的引用;文件引用的解析与渲染不在本插件,判定见
23
+ [ADR-引用的统一解析与渲染转换](../../session/ui-conversation-message-actions/.agents/adrs/20260917-引用的统一解析与渲染转换.md)。
@@ -0,0 +1,3 @@
1
+ # 本包是纯插件:装配点是部署侧(preset)的一行,不经 bundle patch;
2
+ # 空 patch 只为满足包 exports 约定。
3
+ []
@@ -0,0 +1,7 @@
1
+ import { Context } from "@deepseek-ai/cordis";
2
+ //#region src/index.d.ts
3
+ declare const name = "reference-injection";
4
+ declare const inject: string[];
5
+ declare function apply(ctx: Context): void;
6
+ //#endregion
7
+ export { apply, inject, name };
package/dist/index.mjs ADDED
@@ -0,0 +1,81 @@
1
+ import { createUserMessage } from "@deepseek-ai/dsh-llm";
2
+ import { isUserInvocable, renderSkillContent } from "@deepseek-ai/dsh-skill";
3
+ import { findReferences, parseReferenceToken } from "@morlay/dsh-client-ui-primitives";
4
+ import { fromMarkdown } from "mdast-util-from-markdown";
5
+ //#region src/links.ts
6
+ const SKILL_PROTOCOL = "skill";
7
+ function walk(node, visit) {
8
+ visit(node);
9
+ for (const child of node.children ?? []) walk(child, visit);
10
+ }
11
+ function inlineCodeSkillNames(text) {
12
+ const names = [];
13
+ walk(fromMarkdown(text), (node) => {
14
+ if (node.type !== "inlineCode" || node.value === void 0) return;
15
+ const reference = parseReferenceToken(node.value);
16
+ if (reference?.protocol === SKILL_PROTOCOL && reference.path !== void 0) names.push(reference.path);
17
+ });
18
+ return names;
19
+ }
20
+ function skillNamesIn(messages) {
21
+ const names = [];
22
+ const push = (name) => {
23
+ if (name === void 0 || name === "" || names.includes(name)) return;
24
+ names.push(name);
25
+ };
26
+ for (const message of messages) {
27
+ if (message.source.kind !== "user") continue;
28
+ for (const block of message.content) {
29
+ if (block.type !== "text") continue;
30
+ for (const span of findReferences(block.text)) {
31
+ if (span.reference.protocol !== SKILL_PROTOCOL) continue;
32
+ push(span.reference.path);
33
+ }
34
+ for (const name of inlineCodeSkillNames(block.text)) push(name);
35
+ }
36
+ }
37
+ return names;
38
+ }
39
+ //#endregion
40
+ //#region src/index.ts
41
+ const name = "reference-injection";
42
+ const inject = ["skills"];
43
+ function apply(ctx) {
44
+ ctx.on("agent/pre-step", async ({ agent, messages, signal }, next) => {
45
+ const decision = await next();
46
+ if (decision.kind === "reject") return decision;
47
+ const names = skillNamesIn(messages);
48
+ if (names.length === 0) return decision;
49
+ signal.throwIfAborted();
50
+ const lookup = {
51
+ cwd: agent.session.header.cwd,
52
+ signal,
53
+ scope: agent
54
+ };
55
+ const injections = [];
56
+ for (const name of names) {
57
+ const skill = await ctx.skills.get(name, lookup);
58
+ signal.throwIfAborted();
59
+ if (skill === void 0 || !isUserInvocable(skill)) continue;
60
+ const source = {
61
+ kind: "skill-invocation",
62
+ name,
63
+ form: "instructions"
64
+ };
65
+ injections.push(createUserMessage({
66
+ content: [{
67
+ type: "text",
68
+ text: renderSkillContent(skill)
69
+ }],
70
+ source
71
+ }));
72
+ }
73
+ if (injections.length === 0) return decision;
74
+ return {
75
+ ...decision,
76
+ messages: [...decision.messages, ...injections]
77
+ };
78
+ });
79
+ }
80
+ //#endregion
81
+ export { apply, inject, name };
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "@morlay/dsh-reference-injection",
3
+ "version": "0.0.2-alpha.0",
4
+ "description": "Expands a referenced skill (`skill:name`) into an injected <skill_content> instructions message at the pre-step boundary.",
5
+ "keywords": [
6
+ "context",
7
+ "dsh",
8
+ "dsh-plugin",
9
+ "reference",
10
+ "skill"
11
+ ],
12
+ "license": "MIT",
13
+ "repository": {
14
+ "type": "git",
15
+ "url": "https://github.com/morlay/dsh-plugin.git"
16
+ },
17
+ "files": [
18
+ "dist",
19
+ "src",
20
+ "cordis.patch.yml",
21
+ "!**/__tests__"
22
+ ],
23
+ "type": "module",
24
+ "exports": {
25
+ ".": "./dist/index.mjs",
26
+ "./package.json": "./package.json",
27
+ "./cordis.patch.yml": "./cordis.patch.yml"
28
+ },
29
+ "dependencies": {
30
+ "@morlay/dsh-client-ui-primitives": "^0.0.2-alpha.0",
31
+ "mdast-util-from-markdown": "^2.0.3"
32
+ },
33
+ "devDependencies": {
34
+ "@deepseek-ai/cordis": "^4.0.2",
35
+ "@deepseek-ai/dsh-agent": "^0.1.6-alpha.2",
36
+ "@deepseek-ai/dsh-llm": "^0.1.6-alpha.2",
37
+ "@deepseek-ai/dsh-session": "^0.1.6-alpha.2",
38
+ "@deepseek-ai/dsh-skill": "^0.1.6-alpha.2",
39
+ "@types/mdast": "^4.0.4"
40
+ },
41
+ "peerDependencies": {
42
+ "@deepseek-ai/cordis": "^4.0.2",
43
+ "@deepseek-ai/dsh-agent": "^0.1.6-alpha.2",
44
+ "@deepseek-ai/dsh-llm": "^0.1.6-alpha.2",
45
+ "@deepseek-ai/dsh-session": "^0.1.6-alpha.2",
46
+ "@deepseek-ai/dsh-skill": "^0.1.6-alpha.2"
47
+ },
48
+ "scripts": {
49
+ "build": "pnpm exec tsdown"
50
+ }
51
+ }
package/src/index.ts ADDED
@@ -0,0 +1,45 @@
1
+ import type { Context } from "@deepseek-ai/cordis";
2
+ import type { PreStepDecision } from "@deepseek-ai/dsh-agent";
3
+ import { createUserMessage } from "@deepseek-ai/dsh-llm";
4
+ import type { UserMessage } from "@deepseek-ai/dsh-session";
5
+ import {
6
+ isUserInvocable,
7
+ renderSkillContent,
8
+ type SkillInvocationSource,
9
+ } from "@deepseek-ai/dsh-skill";
10
+ import { skillNamesIn } from "./links.ts";
11
+
12
+ export const name = "reference-injection";
13
+
14
+ export const inject = ["skills"];
15
+
16
+ export function apply(ctx: Context): void {
17
+ ctx.on("agent/pre-step", async ({ agent, messages, signal }, next): Promise<PreStepDecision> => {
18
+ const decision = await next();
19
+ if (decision.kind === "reject") return decision;
20
+ const names = skillNamesIn(messages);
21
+ if (names.length === 0) return decision;
22
+ signal.throwIfAborted();
23
+ const lookup = { cwd: agent.session.header.cwd, signal, scope: agent };
24
+ const injections: UserMessage[] = [];
25
+ for (const name of names) {
26
+ const skill = await ctx.skills.get(name, lookup);
27
+ signal.throwIfAborted();
28
+
29
+ if (skill === undefined || !isUserInvocable(skill)) continue;
30
+ const source: SkillInvocationSource = {
31
+ kind: "skill-invocation",
32
+ name,
33
+ form: "instructions",
34
+ };
35
+ injections.push(
36
+ createUserMessage({
37
+ content: [{ type: "text", text: renderSkillContent(skill) }],
38
+ source,
39
+ }),
40
+ );
41
+ }
42
+ if (injections.length === 0) return decision;
43
+ return { ...decision, messages: [...decision.messages, ...injections] };
44
+ });
45
+ }
package/src/links.ts ADDED
@@ -0,0 +1,49 @@
1
+ import type { UserMessage } from "@deepseek-ai/dsh-session";
2
+ import { findReferences, parseReferenceToken } from "@morlay/dsh-client-ui-primitives";
3
+ import { fromMarkdown } from "mdast-util-from-markdown";
4
+
5
+ const SKILL_PROTOCOL = "skill";
6
+
7
+ interface MarkdownNode {
8
+ readonly type: string;
9
+ readonly value?: string;
10
+ readonly children?: readonly MarkdownNode[];
11
+ }
12
+
13
+ function walk(node: MarkdownNode, visit: (node: MarkdownNode) => void): void {
14
+ visit(node);
15
+ for (const child of node.children ?? []) walk(child, visit);
16
+ }
17
+
18
+ function inlineCodeSkillNames(text: string): string[] {
19
+ const names: string[] = [];
20
+ const root = fromMarkdown(text) as unknown as MarkdownNode;
21
+ walk(root, (node) => {
22
+ if (node.type !== "inlineCode" || node.value === undefined) return;
23
+ const reference = parseReferenceToken(node.value);
24
+ if (reference?.protocol === SKILL_PROTOCOL && reference.path !== undefined) {
25
+ names.push(reference.path);
26
+ }
27
+ });
28
+ return names;
29
+ }
30
+
31
+ export function skillNamesIn(messages: readonly UserMessage[]): string[] {
32
+ const names: string[] = [];
33
+ const push = (name: string | undefined): void => {
34
+ if (name === undefined || name === "" || names.includes(name)) return;
35
+ names.push(name);
36
+ };
37
+ for (const message of messages) {
38
+ if ((message.source as { kind?: unknown }).kind !== "user") continue;
39
+ for (const block of message.content) {
40
+ if (block.type !== "text") continue;
41
+ for (const span of findReferences(block.text)) {
42
+ if (span.reference.protocol !== SKILL_PROTOCOL) continue;
43
+ push(span.reference.path);
44
+ }
45
+ for (const name of inlineCodeSkillNames(block.text)) push(name);
46
+ }
47
+ }
48
+ return names;
49
+ }