@morlay/dsh-command 0.0.0-stage → 0.2.0-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.
Files changed (46) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +11 -2
  3. package/dist/command-endpoint-D8Ze5gCZ.d.mts +72 -0
  4. package/dist/command-registry.service-CFFfpdoB.mjs +13 -0
  5. package/dist/command-registry.service-CVViz-lS.d.mts +18 -0
  6. package/dist/contract.d.mts +73 -0
  7. package/dist/contract.mjs +55 -0
  8. package/dist/decorate-g56z_1VW.mjs +8 -0
  9. package/dist/endpoint.d.mts +18 -0
  10. package/dist/endpoint.mjs +77 -0
  11. package/dist/index.d.mts +3 -0
  12. package/dist/index.mjs +3 -0
  13. package/dist/server.d.mts +30 -0
  14. package/dist/server.mjs +99 -0
  15. package/dist/ui.d.mts +105 -0
  16. package/dist/ui.mjs +196 -0
  17. package/package.json +54 -3
  18. package/src/command-registry.service.ts +21 -0
  19. package/src/contract/command-catalog.contract.ts +13 -0
  20. package/src/contract/command-contribution.ts +17 -0
  21. package/src/contract/command-entry.ts +5 -0
  22. package/src/contract/command-registry.contract.ts +22 -0
  23. package/src/contract/command-runner.contract.ts +15 -0
  24. package/src/contract/command-ui.contract.ts +36 -0
  25. package/src/contract/command.decorator.ts +78 -0
  26. package/src/contract/index.ts +12 -0
  27. package/src/endpoint/command-endpoint.ts +113 -0
  28. package/src/endpoint/command-line.ts +32 -0
  29. package/src/endpoint/index.ts +11 -0
  30. package/src/index.ts +5 -0
  31. package/src/server/command-endpoint.service.ts +64 -0
  32. package/src/server/command-entry-of.ts +13 -0
  33. package/src/server/command-execution.ts +45 -0
  34. package/src/server/command.provide.ts +10 -0
  35. package/src/server/index.ts +16 -0
  36. package/src/server/registry-catalog.ts +30 -0
  37. package/src/server/registry-runner.ts +31 -0
  38. package/src/ui/command-endpoint.client.ts +84 -0
  39. package/src/ui/command-palette.store.ts +92 -0
  40. package/src/ui/command-palette.view.tsx +41 -0
  41. package/src/ui/command-ui.service.ts +13 -0
  42. package/src/ui/command.provide.ts +19 -0
  43. package/src/ui/conversation-input-overlay.slot.ts +8 -0
  44. package/src/ui/index.ts +11 -0
  45. package/src/ui/remote-command-catalog.ts +17 -0
  46. package/src/ui/remote-command-runner.ts +18 -0
package/dist/ui.mjs ADDED
@@ -0,0 +1,196 @@
1
+ import { CommandUi } from "./contract.mjs";
2
+ import * as UiCommandsPlugin from "@deepseek-ai/dsh-client-ui-commands/client";
3
+ import { fromCordis } from "@morlay/zephyr/compat";
4
+ import { Injectable, inject, provide } from "@morlay/zephyr";
5
+ import { SessionId } from "@morlay/dsh-session/endpoint";
6
+ import { CommandCatalog, CommandRunner } from "@morlay/dsh-command/contract";
7
+ import { t as __decorate } from "./decorate-g56z_1VW.mjs";
8
+ import { Remote, RemoteTransport, SlotProvider, SlotView, SlotViewBase } from "@morlay/zephyr-app/client";
9
+ import { useSyncExternalStore } from "react";
10
+ import { jsx, jsxs } from "react/jsx-runtime";
11
+ import { CommandEndpoint, commandLineOf } from "@morlay/dsh-command/endpoint";
12
+ import { RemoteClient, groupNameOf, remoteDescriptorsOf } from "@morlay/zephyr-app/remote";
13
+ // src/ui/command-ui.service.ts
14
+ /** 命令 UI 行的基类:mount 由基类给出,子类只补注入与 onInit。 */
15
+ var CommandUiRow = class extends fromCordis(CommandUi, UiCommandsPlugin) {};
16
+ // src/ui/command-palette.store.ts
17
+ const CLOSED = {
18
+ open: false,
19
+ query: "",
20
+ selected: 0,
21
+ entries: []
22
+ };
23
+ let CommandPaletteStore = class CommandPaletteStore {
24
+ catalog = inject(CommandCatalog);
25
+ runner = inject(CommandRunner);
26
+ listeners = /* @__PURE__ */ new Set();
27
+ states = /* @__PURE__ */ new Map();
28
+ /** 一个会话的当前状态(没打开过就是关闭态)。 */
29
+ stateOf(sessionId) {
30
+ return this.states.get(sessionId) ?? CLOSED;
31
+ }
32
+ /** 订阅状态变化。 */
33
+ subscribe(listener) {
34
+ this.listeners.add(listener);
35
+ return () => {
36
+ this.listeners.delete(listener);
37
+ };
38
+ }
39
+ /** 打开弹层(拉一次目录)。 */
40
+ async open(sessionId) {
41
+ const entries = await this.catalog.list(SessionId(sessionId));
42
+ this.patch(sessionId, {
43
+ open: true,
44
+ query: "",
45
+ selected: 0,
46
+ entries
47
+ });
48
+ }
49
+ /** 关闭弹层。 */
50
+ close(sessionId) {
51
+ this.patch(sessionId, CLOSED);
52
+ }
53
+ /** 改过滤词(选中项回到第一个)。 */
54
+ setQuery(sessionId, query) {
55
+ this.patch(sessionId, {
56
+ query,
57
+ selected: 0
58
+ });
59
+ }
60
+ /** 在过滤后的列表里移动选中项(循环)。 */
61
+ move(sessionId, delta) {
62
+ const state = this.stateOf(sessionId);
63
+ const visible = this.visibleOf(state);
64
+ if (visible.length === 0) return;
65
+ this.patch(sessionId, { selected: (state.selected + delta + visible.length) % visible.length });
66
+ }
67
+ /** 过滤后的可见项:空查询给全部,否则按名字与说明做包含匹配。 */
68
+ visibleOf(state) {
69
+ const query = state.query.trim().toLowerCase();
70
+ if (query === "") return state.entries;
71
+ return state.entries.filter((entry) => entry.name.toLowerCase().includes(query) || entry.description.toLowerCase().includes(query));
72
+ }
73
+ /** 执行选中项(执行完关闭;没有选中项时只关闭)。 */
74
+ async run(sessionId) {
75
+ const state = this.stateOf(sessionId);
76
+ const target = this.visibleOf(state)[state.selected];
77
+ this.close(sessionId);
78
+ if (target === void 0) return;
79
+ await this.runner.run(SessionId(sessionId), target.name);
80
+ }
81
+ patch(sessionId, next) {
82
+ this.states.set(sessionId, {
83
+ ...this.stateOf(sessionId),
84
+ ...next
85
+ });
86
+ for (const listener of Array.from(this.listeners)) listener();
87
+ }
88
+ };
89
+ CommandPaletteStore = __decorate([Injectable()], CommandPaletteStore);
90
+ // src/ui/conversation-input-overlay.slot.ts
91
+ let ConversationInputOverlaySlot = class ConversationInputOverlaySlot extends SlotViewBase {};
92
+ ConversationInputOverlaySlot = __decorate([SlotProvider({
93
+ name: "conversation.input.overlay",
94
+ multi: true,
95
+ key: "command-popup"
96
+ })], ConversationInputOverlaySlot);
97
+ // src/ui/command-palette.view.tsx
98
+ let CommandPaletteView = class CommandPaletteView extends ConversationInputOverlaySlot {
99
+ palette = inject(CommandPaletteStore);
100
+ render() {
101
+ const sessionId = this.viewProps?.sessionId;
102
+ if (sessionId === void 0) return null;
103
+ const state = useSyncExternalStore((listener) => this.palette.subscribe(listener), () => this.palette.stateOf(sessionId));
104
+ if (!state.open) return null;
105
+ const visible = this.palette.visibleOf(state);
106
+ return /* @__PURE__ */ jsx("div", {
107
+ "data-command-palette": "",
108
+ children: /* @__PURE__ */ jsx("ul", { children: visible.map((entry, index) => /* @__PURE__ */ jsxs("li", {
109
+ "data-selected": index === state.selected ? "" : void 0,
110
+ children: [`/${entry.name}`, entry.description === "" ? null : /* @__PURE__ */ jsx("span", { children: entry.description })]
111
+ }, entry.name)) })
112
+ });
113
+ }
114
+ };
115
+ CommandPaletteView = __decorate([SlotView()], CommandPaletteView);
116
+ // src/ui/command-endpoint.client.ts
117
+ let CommandEndpointClient = class CommandEndpointClient extends CommandEndpoint {
118
+ /** 转发目标:`remote.invoke`(wire 的命名参数由契约的 `parameters` 声明决定)。 */
119
+ remote = inject(Remote);
120
+ /** 传输:转发走它(我们的组不注册进上游 `remote`)。 */
121
+ transport = inject(RemoteTransport);
122
+ /**
123
+ * 挂上这一组的 descriptor:客户端形态**自己挂自己那一组**,不另开 binding 行。
124
+ *
125
+ * 面本身是给**上游消费方**用的(`ctx.remote.commands.execute(sessionId, line, attachments)`);
126
+ * 本仓自己的调用走下面的显式转发。
127
+ * @param ctx - 本行 ctx:disposer 挂在它上面,卸载随行 fiber。
128
+ */
129
+ async onInit(ctx) {
130
+ const dispose = await this.remote.$mount(remoteDescriptorsOf(CommandEndpoint));
131
+ ctx.effect(() => dispose);
132
+ }
133
+ /**
134
+ * 列目录:wire 是 `{ sessionId }`(一个位置参数)。
135
+ * @param sessionId - 目标会话。
136
+ * @returns 目录(未知会话给空数组)。
137
+ */
138
+ async list(sessionId) {
139
+ return await this.invoke("list", { sessionId });
140
+ }
141
+ /**
142
+ * 执行一条命令:wire 是 `{ sessionId, line, submittedAttachments }`(三个扁平字段)。
143
+ * @param sessionId - 目标会话。
144
+ * @param line - 整行(`/plan off`)。
145
+ * @param submittedAttachments - 随命令提交的附件(本装配不消费,仍按协议传)。
146
+ * @returns 注册表给出的执行结果。
147
+ */
148
+ async execute(sessionId, line, submittedAttachments) {
149
+ return await this.invoke("execute", {
150
+ sessionId,
151
+ line,
152
+ submittedAttachments
153
+ });
154
+ }
155
+ /**
156
+ * 一次 unary 调用:端点名由组名(契约上的 `@RemoteGroup`)与方法名拼出,不另写字符串表。
157
+ * @param method - 方法名。
158
+ * @param args - wire 的命名参数。
159
+ * @returns 宿主给的原样值。
160
+ */
161
+ async invoke(method, args) {
162
+ return await this.transport.invoke(`${groupNameOf(this.constructor)}/${method}`, { args });
163
+ }
164
+ };
165
+ CommandEndpointClient = __decorate([RemoteClient()], CommandEndpointClient);
166
+ // src/ui/remote-command-catalog.ts
167
+ let RemoteCommandCatalog = class RemoteCommandCatalog extends CommandCatalog {
168
+ endpoint = inject(CommandEndpointClient);
169
+ async list(sessionId) {
170
+ return this.endpoint.list(sessionId);
171
+ }
172
+ };
173
+ RemoteCommandCatalog = __decorate([Injectable()], RemoteCommandCatalog);
174
+ // src/ui/remote-command-runner.ts
175
+ let RemoteCommandRunner = class RemoteCommandRunner extends CommandRunner {
176
+ endpoint = inject(CommandEndpointClient);
177
+ async run(sessionId, name, input) {
178
+ return this.endpoint.execute(sessionId, commandLineOf(name, input), []);
179
+ }
180
+ };
181
+ RemoteCommandRunner = __decorate([Injectable()], RemoteCommandRunner);
182
+ // src/ui/command.provide.ts
183
+ /**
184
+ * command 的浏览器半:RPC 客户端 + 目录/执行器的远端实现 + 命令 UI 行 + 弹层的 store 与视图。
185
+ *
186
+ * 顺序 = 依赖:端点客户端先(其余都经它走 wire);`commands` 的 namespace 面由它的 `onInit` 挂上
187
+ * (没有单独的绑定行),因此它要排在消费方之前——上游 `ui-commands` 的 `static inject` 里有
188
+ * `remote.commands`(那一行在会话域的输入区组里装,因为它的头一项依赖是 `inputTriggers`)。
189
+ */
190
+ const provideCommandUi = provide([
191
+ CommandEndpointClient,
192
+ RemoteCommandCatalog,
193
+ RemoteCommandRunner,
194
+ CommandPaletteStore
195
+ ]);
196
+ export { CommandEndpointClient, CommandPaletteStore, CommandPaletteView, CommandUi, CommandUiRow, ConversationInputOverlaySlot, RemoteCommandCatalog, RemoteCommandRunner, provideCommandUi };
package/package.json CHANGED
@@ -1,6 +1,57 @@
1
1
  {
2
2
  "name": "@morlay/dsh-command",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.2.0-alpha.0",
4
+ "private": false,
5
+ "description": "命令注册表与斜杠命令的两半:谁能登记一条命令、模型与用户怎么触发它,各有各的面。",
6
+ "files": [
7
+ "dist",
8
+ "src",
9
+ "!**/__tests__"
10
+ ],
11
+ "type": "module",
12
+ "exports": {
13
+ ".": {
14
+ "types": "./dist/index.d.mts",
15
+ "default": "./dist/index.mjs"
16
+ },
17
+ "./contract": {
18
+ "types": "./dist/contract.d.mts",
19
+ "default": "./dist/contract.mjs"
20
+ },
21
+ "./endpoint": {
22
+ "types": "./dist/endpoint.d.mts",
23
+ "default": "./dist/endpoint.mjs"
24
+ },
25
+ "./server": {
26
+ "types": "./dist/server.d.mts",
27
+ "default": "./dist/server.mjs"
28
+ },
29
+ "./ui": {
30
+ "types": "./dist/ui.d.mts",
31
+ "default": "./dist/ui.mjs"
32
+ },
33
+ "./package.json": "./package.json"
34
+ },
35
+ "dependencies": {
36
+ "@morlay/dsh-agent": "^0.2.0-alpha.0",
37
+ "@morlay/dsh-session": "^0.2.0-alpha.0",
38
+ "@morlay/zephyr": "^0.2.0-alpha.0",
39
+ "@morlay/zephyr-app": "^0.2.0-alpha.0"
40
+ },
41
+ "devDependencies": {
42
+ "@morlay/dsh-agent-loop": "^0.2.0-alpha.0"
43
+ },
44
+ "peerDependencies": {
45
+ "@deepseek-ai/dsh-client-ui-commands": "^0.2.3-alpha.3",
46
+ "@deepseek-ai/dsh-commands": "^0.2.3-alpha.3"
47
+ },
48
+ "engines": {
49
+ "dsh": ">= 0.2.3-alpha.3 < 0.2.4"
50
+ },
51
+ "repository": {
52
+ "type": "git",
53
+ "url": "https://github.com/morlay/zephyr.git",
54
+ "directory": "packages/agent/dsh-command"
55
+ },
56
+ "scripts": {}
6
57
  }
@@ -0,0 +1,21 @@
1
+ import CommandRuntime from "@deepseek-ai/dsh-commands";
2
+ import { Injectable, type Context } from "@morlay/zephyr";
3
+ import { fromCordis } from "@morlay/zephyr/compat";
4
+ import { CommandRegistry } from "./contract/index.ts";
5
+
6
+ /**
7
+ * 命令注册表的实现:直接复用上游 `CommandRuntime`(它就是这个注册表)。
8
+ *
9
+ * 两个名字各有出处,**互不干扰**:
10
+ * - 上游按 `ctx.inject(['commands'])` 找它(`permission-presets` 就这样注册 `/permission`)——
11
+ * 这条由 `mount` 的**双注册**满足:上游自己的 `provide`/`Service` 名字同时落到本行的 cordis ctx;
12
+ * - 我们自己的代码按令牌 `CommandRegistry` 注入它——这条由 `fromCordis(契约, 实现)` 绑定。
13
+ *
14
+ * 所以不需要"别名契约":一个实现兑现一个契约,另一个名字走上游自己的注册通道。
15
+ */
16
+ @Injectable()
17
+ export class CommandRegistryDefault extends fromCordis(CommandRegistry, CommandRuntime) {
18
+ override async onInit(ctx: Context): Promise<void> {
19
+ await this.mount(ctx);
20
+ }
21
+ }
@@ -0,0 +1,13 @@
1
+ // `commands/list` 的契约:某个会话当前可见的命令(只读)。
2
+ //
3
+ // 与 host 侧的 `CommandRegistry` 不同:那是可写注册表(谁能注册命令),这里是「某会话能看见什么」。
4
+ import type { SessionId } from "@morlay/dsh-session/endpoint";
5
+ import { Injectable } from "@morlay/zephyr";
6
+ import type { CommandEntry } from "./command-entry.ts";
7
+
8
+ /** 命令目录:某个会话当前可见的命令。实现由组合层提供(host 直连、走 remote、或测试里的假实现)。 */
9
+ @Injectable()
10
+ export abstract class CommandCatalog {
11
+ /** 拉一个会话可见的命令(实现方可缓存)。 */
12
+ abstract list(sessionId: SessionId): Promise<readonly CommandEntry[]>;
13
+ }
@@ -0,0 +1,17 @@
1
+ // 一条**命令贡献**的形状:注册表要的那三样,与上游 `CommandDefinition` 的必填字段对齐。
2
+ //
3
+ // 它是接口不是令牌:贡献方 `implements CommandContribution` 并用 `@Command()` 声明装配期登记,
4
+ // 不自己写 `registry.register(...)`。
5
+ import type { CommandInvocation, CommandResult } from "@morlay/dsh-command/server";
6
+
7
+ /** 一条命令:名字、一行说明、执行入口。 */
8
+ export interface CommandContribution {
9
+ /** `/name` 里的名字(不含斜杠;上游要求小写、字母开头)。 */
10
+ readonly name: string;
11
+
12
+ /** 一行说明(发现面展示用,不能为空)。 */
13
+ readonly description: string;
14
+
15
+ /** 执行;返回的结果被注册表包成 `command/run` / `command/done` 事件。 */
16
+ readonly handler: (invocation: CommandInvocation) => CommandResult | Promise<CommandResult>;
17
+ }
@@ -0,0 +1,5 @@
1
+ // 一条命令的形状:`CommandCatalog` 的返回项、RPC 的 `commands/list` 响应也用它。
2
+ //
3
+ // **就是上游那份**(`CommandDescriptor`):手抄过一次(`definitionId: string` / `input: unknown`),
4
+ // 结果是 wire 类型比协议更松——按 `Typerted` 的投影开守卫时,红的就是这两个字段。
5
+ export type { CommandDescriptor as CommandEntry } from "@deepseek-ai/dsh-commands/types";
@@ -0,0 +1,22 @@
1
+ // 命令**注册表**的契约:只回答「谁能登记一条命令」。
2
+ //
3
+ // 查询与执行不在它上面——只读面是 `CommandCatalog`(某会话能看见什么),执行面是 `CommandRunner`。
4
+ // 宿主侧要按 agent 读/执行(上游 `list(agent)` / `find(agent, name)` / `execute(agent, …)`)时用
5
+ // 实现类 `CommandRegistryDefault`(本包 `server/` 内部),不从契约面把它们放出来。
6
+ import type { CommandFileReceiptResolver } from "@deepseek-ai/dsh-commands";
7
+ import { Injectable } from "@morlay/zephyr";
8
+ import type { CommandContribution } from "./command-contribution.ts";
9
+
10
+ @Injectable({ name: "commands" })
11
+ export abstract class CommandRegistry {
12
+ /** 登记一条命令;返回**这一条**的撤销函数(`@Command()` 生成的登记在行卸载时调它)。 */
13
+ abstract register(contribution: CommandContribution): () => void;
14
+
15
+ /**
16
+ * 命令提交里「暂存文件回执」的唯一解析方(会话上传方装它,提交时按 id 换回附件引用)。
17
+ *
18
+ * 它和 `register` 是同一族:都是往注册表里**装一个面**。漏了它,上传行拿到的是空壳,
19
+ * 报错落在 `ctx.commands.registerFileReceiptResolver is not a function`(离这里很远)。
20
+ */
21
+ abstract registerFileReceiptResolver(resolver: CommandFileReceiptResolver): () => void;
22
+ }
@@ -0,0 +1,15 @@
1
+ // `commands/execute` 的契约:执行一条命令。
2
+ //
3
+ // 返回**注册表的结算值**(`commandId` + `result`),不是 `void`:处理器把失败写在 `result.kind` 里
4
+ // (例如档位名不在目录中),调用方(上游权限 chip)正是按它判断成败。丢掉返回值就等于把失败
5
+ // 伪装成成功——真机上表现为"点了切换、什么都不变"。
6
+ import type { SessionId } from "@morlay/dsh-session/endpoint";
7
+ import { Injectable } from "@morlay/zephyr";
8
+ import type { CommandExecuteValue } from "../endpoint/command-endpoint.ts";
9
+
10
+ /** 执行一条命令(宿主侧 `commands/execute` 的端口)。 */
11
+ @Injectable()
12
+ export abstract class CommandRunner {
13
+ /** 执行;找不到命令或执行失败以异常抛出,处理器自己结算的失败在 `result.kind` 上。 */
14
+ abstract run(sessionId: SessionId, name: string, input?: string): Promise<CommandExecuteValue>;
15
+ }
@@ -0,0 +1,36 @@
1
+ // 命令 UI 的契约(浏览器半):`/` 命令源、客户端贡献注册表、每会话的弹层控制器。
2
+ //
3
+ // 实现是上游 `ui-commands` 的 `CommandUiRuntime`(它自己注册成 cordis 服务 `commandUi`)。
4
+ // 这里只声明**我们要用的面**——各域的行(权限、模型选择)通过它把 `/permission`、`/model`
5
+ // 挂成输入区的弹层贡献。
6
+ //
7
+ // 没有这个契约时用的是空面(`{ register: () => () => {} }`):贡献被吞掉,`/model`、`/compress`
8
+ // 因此不出现在 `/` 面板里,而调用方也不会报错——静默失效比报错更难查。
9
+ import type {
10
+ CommandContribution,
11
+ CommandDecoration,
12
+ } from "@deepseek-ai/dsh-client-ui-commands/client";
13
+ import { Injectable } from "@morlay/zephyr";
14
+
15
+ @Injectable({ name: "commandUi" })
16
+ export abstract class CommandUi {
17
+ /**
18
+ * 登记一条客户端命令贡献(`/model` 的弹层就是这个)。
19
+ * @param contribution - 名字、可用性判断、弹层规格。
20
+ * @returns 撤销函数。
21
+ */
22
+ abstract register(contribution: CommandContribution): () => void;
23
+
24
+ /**
25
+ * 给宿主已有的一条命令挂「裸调用时的弹层」(`/permission` 用这个)。
26
+ * @param decoration - 宿主命令名 + 可用性 + 弹层规格。
27
+ * @returns 撤销函数。
28
+ */
29
+ abstract decorate(decoration: CommandDecoration): () => void;
30
+
31
+ /**
32
+ * 关掉某条命令的弹层(目录失效时用)。
33
+ * @param name - 命令名(不含斜杠)。
34
+ */
35
+ abstract dismiss(name: string): void;
36
+ }
@@ -0,0 +1,78 @@
1
+ // `@Command()`:命令贡献的登记写法(`@Command()` + `implements CommandContribution`,不手写 `register`)。
2
+ //
3
+ // **每类一个具体装饰器**:这里只做「命令」这一类的登记,不参数化注册表、不留通用入口——
4
+ // `@Tool` / `@Skill` / `@Policy` 三个装饰器**各自一份**:形态真的完全一致时才谈提取(提取前先记 ADR)。
5
+ //
6
+ // 手法与 `configure` 相同:装饰器返回**子类**。`inject()` 是构造期的字段占位,zephyr 只解析
7
+ // 实例**自有**字段,所以要拿到注册表就得自己构造实例;登记发生在装配期(provider 的 `onInit`),
8
+ // 撤销随行卸载(`onDispose`)。
9
+ import { inject, Injectable, type Awaitable, type Context } from "@morlay/zephyr";
10
+ import { CommandRegistry } from "./command-registry.contract.ts";
11
+ import type { CommandContribution } from "./command-contribution.ts";
12
+
13
+ /**
14
+ * 贡献类的构造面。
15
+ *
16
+ * 用 `any[]` 而不是 zephyr 的 `Newable`(`never[]`):TS 的混入规则(TS2545)要求被 `extends` 的
17
+ * 类型变量的构造是「单个 `any[]` rest 参数」,写成 `never[]` 或 `ConstructorParameters<T>` 都不认。
18
+ */
19
+ // oxlint-disable-next-line typescript/no-explicit-any -- mixin 的构造签名必须是 `any[]`(TS2545),`never[]` 不被 `extends` 接受
20
+ type CommandClass<T> = new (...args: any[]) => T;
21
+
22
+ /**
23
+ * 把类声明成一条命令贡献:装配期登记进 `CommandRegistry`,行卸载时撤销。
24
+ *
25
+ * ```ts
26
+ * @Command()
27
+ * export class GoalCommand implements CommandContribution { … } // 包仍有自己的 module.ts
28
+ * ```
29
+ * @returns 类装饰器(返回绑好装配期登记的 Provider 子类)。
30
+ */
31
+ export function Command(): <T extends CommandClass<CommandContribution>>(target: T) => T {
32
+ return <T extends CommandClass<CommandContribution>>(target: T): T => {
33
+ // 贡献方自己写的生命周期:装饰器只**包一层**,不能把它吞掉。
34
+ const hooks = target.prototype as {
35
+ onInit?: (ctx: Context) => Awaitable<void>;
36
+ onDispose?: () => Awaitable<void>;
37
+ };
38
+
39
+ class CommandProvider extends target {
40
+ // 混入类的构造要显式转发:TS 只认「单个 rest 参数的构造」,否则 `extends` 类型变量报 TS2545。
41
+ // oxlint-disable-next-line typescript/no-explicit-any -- 混入类构造的显式转发,同 TS2545 的 `any[]` 要求
42
+ constructor(...args: any[]) {
43
+ super(...args);
44
+ }
45
+
46
+ /** 注册表:构造期占位,装配期解析(`inject` 只在 Provider 自有的字段生效)。 */
47
+ private readonly registry = inject(CommandRegistry);
48
+
49
+ /** 这一条的撤销函数:`onDispose` 里调一次,行卸载即从注册表里摘掉。 */
50
+ private release: (() => void) | undefined;
51
+
52
+ async onInit(ctx: Context): Promise<void> {
53
+ await hooks.onInit?.call(this, ctx);
54
+ this.release = this.registry.register({
55
+ name: this.name,
56
+ description: this.description,
57
+ // 上游把贡献冻结成副本再调 `handler`:包一层把 `this` 留在实例上(否则 handler 的
58
+ // `this` 是那份副本,注入的依赖全都读不到)。
59
+ handler: (invocation) => this.handler(invocation),
60
+ });
61
+ }
62
+
63
+ async onDispose(): Promise<void> {
64
+ this.release?.();
65
+ this.release = undefined;
66
+ await hooks.onDispose?.call(this);
67
+ }
68
+ }
69
+
70
+ // 令牌名沿用贡献类自己:`Injectable` 按类名取服务名,叫 `CommandProvider` 的话每条命令
71
+ // 都会挤到同一个名字上。
72
+ Object.defineProperty(CommandProvider, "name", { value: target.name });
73
+ Injectable()(CommandProvider);
74
+ // 子类实例就是 `T` 的实例(它 `extends target`);TS 不认「类型变量的 `extends` 结果仍是 `T`」,
75
+ // 这里是唯一的断言,且是就地收窄——不是先抹成 `unknown` 再转。
76
+ return CommandProvider as T;
77
+ };
78
+ }
@@ -0,0 +1,12 @@
1
+ // command 的**契约面**(`@morlay/dsh-command/contract`):进程内的端口。
2
+ //
3
+ // 注册表 / 目录 / 执行器 / 按会话取 agent —— 全是进程内的服务面,**不涉及 remote**;
4
+ // 远程面在 `./endpoint`。
5
+ export * from "./command-entry.ts";
6
+ export * from "./command-contribution.ts";
7
+ export * from "./command-registry.contract.ts";
8
+ export * from "./command-catalog.contract.ts";
9
+ export * from "./command-runner.contract.ts";
10
+ export * from "./command-ui.contract.ts";
11
+ // `@Command()`:命令贡献的登记写法,属契约面。
12
+ export * from "./command.decorator.ts";
@@ -0,0 +1,113 @@
1
+ // `commands` 这一组的**跨端契约**:组名、组内每个方法的名字与请求/响应形状。
2
+ //
3
+ // 两端共用同一份声明:宿主侧实现它(`server/`),浏览器侧按它消费(`ui/` 的 remote 实现)。
4
+ // 契约是普通类:客户端形态(`@RemoteClient()`)与宿主形态各自继承它。
5
+ // **它不继承 `RemoteClientBase`**:那个基类带一个 `inject(Remote)` 字段,而 `Remote` 只有浏览器侧有;
6
+ // 契约被两端共用,继承它会让宿主装配去解析一个不存在的服务。
7
+ //
8
+ // **这一组是自研 wire**(我们挂自己的 descriptor,不挂上游 `@deepseek-ai/dsh-commands/remote`):
9
+ // 上游那条 remote 的两个端点都带 `scope: { context: 'agent' }` + `lookup`(wire 传 id、网关解析成
10
+ // `Agent`),本装配的 mux 载具不做 scope/lookup 解析。所以我们只沿用上游的**调用形状**(位置参数、
11
+ // 整行、附件),名字用本仓词汇(`sessionId`)。**形状有上游类型可守**:文件末尾的
12
+ // `CommandEndpointCovered` 对上游 `commands` 那条 wire 面的裸值视图;descriptor 的参数名与宿主入参映射
13
+ // 另由 `__tests__/command-endpoint-wire.spec.ts` 钉住。
14
+ import { RemoteGroup, RemoteMethod } from "@morlay/zephyr-app/remote";
15
+ import type { AssertTrue, AssertWireFace } from "@morlay/zephyr-app/client";
16
+ import type { AsyncReturn, DropParam, MapParam } from "@morlay/zephyr-app/typert";
17
+ import type { Agent } from "@morlay/dsh-agent";
18
+ import type { SessionId } from "@morlay/dsh-session/endpoint";
19
+ import type CommandRuntime from "@deepseek-ai/dsh-commands";
20
+ import type { CommandExecution, CommandSubmitAttachment } from "@deepseek-ai/dsh-commands/types";
21
+ import type { CommandEntry } from "@morlay/dsh-command/contract";
22
+
23
+ /** `commands/list` 的请求。 */
24
+ export interface CommandListRequest {
25
+ readonly sessionId: string;
26
+ }
27
+
28
+ /**
29
+ * `commands/list` 的响应:**裸数组**。
30
+ *
31
+ * 消费方是上游 `ui-commands` 的目录(`const list = await ctx.remote.commands.list(id)` 之后直接迭代),
32
+ * 包一层 `{ commands }` 会让它报 `list is not iterable`。
33
+ */
34
+ export type CommandListValue = readonly CommandEntry[];
35
+
36
+ /**
37
+ * `commands/execute` 的响应:上游 `CommandExecution`(`commandId` 生命周期配对 id + 归一化结果)。
38
+ *
39
+ * **直接取上游那份**:手抄过一次(`commandId: string` 对 branded、`result` 手写联合),形状漂了只能靠
40
+ * 运行时发现。`result` 是判别式联合(`success` / `error`),消费方按 `kind` 分派。
41
+ */
42
+ export type CommandExecuteValue = CommandExecution;
43
+
44
+ /**
45
+ * 执行配对 id 的**铸点**(上游 `CommandId` 品牌工厂,纯函数)。
46
+ *
47
+ * 两个用处:宿主在「注册表里没有这条命令」时回一条结构化失败(需要一个配对 id),以及测试替身构造
48
+ * `CommandExecuteValue`。契约面允许「品牌工厂」这类纯值,所以从 `@deepseek-ai/dsh-commands/src/brand.ts`
49
+ * 转出,消费方不必各自去 import 上游深路径。
50
+ */
51
+ export { CommandId } from "@deepseek-ai/dsh-commands/src/brand.ts";
52
+
53
+ /** `commands` 组:列目录、执行命令。 */
54
+ @RemoteGroup({ name: "commands" })
55
+ export class CommandEndpoint {
56
+ /**
57
+ * 列出某个会话可见的命令。
58
+ *
59
+ * wire 是**一个位置参数** `{ sessionId }`:上游消费方(`ui-commands` 的目录)按
60
+ * `remote.commands.list(sessionId)` 传位置标量;声明了 `parameters` 之后宿主也按位置取。
61
+ * @param _sessionId - 会话 id(上游称 agent id)。
62
+ * @returns 目录(未知会话给空数组)。
63
+ */
64
+ @RemoteMethod({ parameters: ["sessionId"] })
65
+ async list(_sessionId: SessionId): Promise<CommandListValue> {
66
+ throw new Error("commands/list:契约类未实现(宿主侧要在子类里实现它)");
67
+ }
68
+
69
+ /**
70
+ * 执行一条命令。
71
+ *
72
+ * **wire 是三个位置参数**(`{ sessionId, line, submittedAttachments }`),不是"一个请求对象":
73
+ * 上游的消费方全都按位置调(`ui-commands` 的 `ctx.remote.commands.execute(sessionId, line, attachments)`、
74
+ * `ui-plan` 的 `execute(sessionId, '/plan off', [])`、会话面的 `execute(this.sessionId, line, [])`)。
75
+ * 之前这里没声明 `parameters`,descriptor 把第一个实参编成 `request`,于是整行与附件都丢了、
76
+ * 宿主只拿到一个字符串——真机上的症状是 `commands/execute: 缺少会话或命令名`。
77
+ * @param _sessionId - 目标会话(上游称 agent id)。
78
+ * @param _line - **整行**(`/plan off`):上游协议就是传整行,拆分在宿主侧做(`splitCommandLine`)。
79
+ * @param _submittedAttachments - 随命令提交的附件;本装配的命令都不消费它们(见债:命令附件未接线)。
80
+ * @returns 注册表给出的执行结果。
81
+ */
82
+ @RemoteMethod({ parameters: ["sessionId", "line", "submittedAttachments"] })
83
+ async execute(
84
+ _sessionId: SessionId,
85
+ _line: string,
86
+ _submittedAttachments: readonly CommandSubmitAttachment[],
87
+ ): Promise<CommandExecuteValue | undefined> {
88
+ throw new Error("commands/execute:契约类未实现(宿主侧要在子类里实现它)");
89
+ }
90
+ }
91
+
92
+ /**
93
+ * **上游 `commands` 的 wire 面**:从宿主类 `CommandRuntime` 投影出来,三步(规则与出处见
94
+ * `@morlay/zephyr-app/typert` 的 `params.ts`):`DropParam`(末尾 `signal` 走 cancellation)→
95
+ * `MapParam`(lookup:`Agent` → `SessionId`)→ `AsyncReturn`(wire 上每次调用都是异步的)。
96
+ *
97
+ * 业务类型只出现在这一行组合里——框架那三个工具不认识 `Agent` / `SessionId` / `AbortSignal`。
98
+ */
99
+ type CommandsWire = AsyncReturn<MapParam<DropParam<CommandRuntime, AbortSignal>, Agent, SessionId>>;
100
+
101
+ /**
102
+ * 覆盖守卫:契约对上**宿主类**的 wire 面(见 {@link CommandsWire})。
103
+ *
104
+ * 这一组是自研 wire(不挂上游 descriptor:它的端点带 `scope`/`lookup`,本装配的载具不解析),但
105
+ * **调用形状**是上游的协议——签名漂了(像 `execute` 曾经漏掉 `parameters`、只收一个 `request`)就红。
106
+ *
107
+ * 部分覆盖:宿主类还有 `register` / `find` / `registerFileReceiptResolver` 这些**不进 wire** 的方法,所以
108
+ * 覆盖范围**显式列在下面的 `Pick` 里**(按 `keyof CommandEndpoint` 取会放过「契约掉了一个端点」这种退化)。
109
+ * `Pick` 还守住另一半:契约声明了宿主类没有的端点,它自己会报。
110
+ */
111
+ export type CommandEndpointCovered = AssertTrue<
112
+ AssertWireFace<Pick<CommandsWire, "list" | "execute">, CommandEndpoint>
113
+ >;
@@ -0,0 +1,32 @@
1
+ // 命令行的**拼装与拆分**:`/name input` 的规则只在这里写一份。
2
+ //
3
+ // 为什么需要两向:wire 上的 `commands/execute` 传的是**整行**(上游客户端就是这么调的:
4
+ // `remote.commands.execute(sessionId, "/plan off", [])`),而注册表的执行入口、我们的
5
+ // `CommandRunner.run(sessionId, name, input)`、以及同进程端口都是「名字 + 输入」两段。
6
+ // 两段 ↔ 整行的转换只此一处:客户端拼、宿主拆,规则漂了就是「命令名里多了个斜杠」这类静默错。
7
+
8
+ /**
9
+ * 由名字与输入拼出一行。
10
+ * @param name - 命令名(带不带前导 `/` 都行)。
11
+ * @param input - 名字后面的输入;`undefined` 与空串都表示没有。
12
+ * @returns 以 `/` 开头的命令行。
13
+ */
14
+ export function commandLineOf(name: string, input?: string): string {
15
+ const bare = name.startsWith("/") ? name.slice(1) : name;
16
+ return input === undefined || input === "" ? `/${bare}` : `/${bare} ${input}`;
17
+ }
18
+
19
+ /**
20
+ * 拆出一行的名字与输入(首个空格为界;名字不带前导 `/`)。
21
+ * @param line - 以 `/` 开头的命令行(没有前导 `/` 也按同一规则拆)。
22
+ * @returns 命令名(可能是空串:只有空白输入)与它后面的输入。
23
+ */
24
+ export function splitCommandLine(line: string): {
25
+ readonly name: string;
26
+ readonly input: string | undefined;
27
+ } {
28
+ const text = line.startsWith("/") ? line.slice(1) : line;
29
+ const separator = text.indexOf(" ");
30
+ if (separator === -1) return { name: text, input: undefined };
31
+ return { name: text.slice(0, separator), input: text.slice(separator + 1) };
32
+ }
@@ -0,0 +1,11 @@
1
+ // command 的 **RPC 面**(`@morlay/dsh-command/endpoint`):组契约 + 请求/响应形状。
2
+ //
3
+ // 与 `contract/` 的分界:那里是**进程内的端口**(`@morlay/dsh-command/contract`);这里是**两端共用面**
4
+ // (`@RemoteGroup` 的组契约与 wire 形状)。
5
+ //
6
+ // 浏览器半要消费的那几个只读契约(`CommandCatalog` / `CommandEntry` / `CommandRunner`)**不在这里转出**:
7
+ // 它们本来就是契约面的东西,走 `@morlay/dsh-command/contract`——在这里 re-export 等于给同一批符号造
8
+ // 第二个 home,也让「跨面 `export * from "../contract/*"`」这条判据失效。
9
+ export * from "./command-endpoint.ts";
10
+ // 命令行的拼装 / 拆分:客户端与宿主共用同一份规则(`/name input`)。
11
+ export * from "./command-line.ts";
package/src/index.ts ADDED
@@ -0,0 +1,5 @@
1
+ // command 的包根:契约 + 默认实现 + 装配模块 + 贡献的声明式登记(聚合 `./contract`)。
2
+ //
3
+ // 两半各有出口:宿主半 `./server`(RPC 契约与实现、同进程端口实现),浏览器半 `./ui`(store / 位置 / 视图)。
4
+ export * from "./contract/index.ts";
5
+ export * from "./command-registry.service.ts";