@morlay/dsh-conversation 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.
- package/LICENSE +21 -0
- package/README.md +49 -2
- package/dist/contract-DgH7vjO7.mjs +20 -0
- package/dist/contract.d.mts +3 -0
- package/dist/contract.mjs +3 -0
- package/dist/decorate-g56z_1VW.mjs +8 -0
- package/dist/endpoint.d.mts +42 -0
- package/dist/endpoint.mjs +50 -0
- package/dist/first-prompt-llm.service-BYX1KK4Z.mjs +100 -0
- package/dist/first-prompt-llm.service-DVcR15W4.d.mts +55 -0
- package/dist/index-BOtIdC0T.d.mts +45 -0
- package/dist/index.d.mts +6 -0
- package/dist/index.mjs +6 -0
- package/dist/server.d.mts +211 -0
- package/dist/server.mjs +372 -0
- package/dist/session-title.contract-1UPlQFwN.mjs +17 -0
- package/dist/session-title.contract-C3tGUiK2.d.mts +132 -0
- package/dist/testing.d.mts +14 -0
- package/dist/testing.mjs +24 -0
- package/dist/tool-results.group-BEGx5hXv.mjs +17 -0
- package/dist/tool-results.group-BUjXDYDr.d.mts +16 -0
- package/dist/ui.d.mts +531 -0
- package/dist/ui.mjs +6678 -0
- package/package.json +120 -3
- package/src/contract/api-session.contract.ts +182 -0
- package/src/contract/conversation-client.contract.ts +13 -0
- package/src/contract/index.ts +26 -0
- package/src/contract/input-triggers.contract.ts +15 -0
- package/src/contract/session-controller.contract.ts +11 -0
- package/src/contract/session-title.contract.ts +30 -0
- package/src/contract/ui-conversation.contract.ts +15 -0
- package/src/contract/ui-session-status.contract.ts +15 -0
- package/src/contract/ui-session.contract.ts +16 -0
- package/src/contract/ui-workspace.contract.ts +83 -0
- package/src/deliverable.service.ts +74 -0
- package/src/endpoint/file-references.group.ts +43 -0
- package/src/endpoint/index.ts +8 -0
- package/src/endpoint/session-reference.group.ts +43 -0
- package/src/endpoint/tool-results.group.ts +35 -0
- package/src/first-prompt-llm.service.ts +38 -0
- package/src/index.ts +16 -0
- package/src/server/api-session.provide.ts +232 -0
- package/src/server/client-settings-defaults.service.ts +39 -0
- package/src/server/conversation-server.provide.ts +33 -0
- package/src/server/deliverable.provide.ts +5 -0
- package/src/server/first-prompt-llm.provide.ts +5 -0
- package/src/server/index.ts +24 -0
- package/src/server/open-in-app.service.ts +52 -0
- package/src/server/session-controller.remote.ts +11 -0
- package/src/server/session-reference.provide.ts +15 -0
- package/src/server/session-reference.service.ts +58 -0
- package/src/server/session-remote.provide.ts +31 -0
- package/src/server/session-status.service.ts +36 -0
- package/src/server/session-title.provide.ts +5 -0
- package/src/server/tool-results-endpoint.service.ts +56 -0
- package/src/server/tool-results.service.ts +59 -0
- package/src/session-title.service.ts +38 -0
- package/src/testing/index.ts +35 -0
- package/src/ui/agent-scopes.service.ts +20 -0
- package/src/ui/approval-slot.ts +33 -0
- package/src/ui/approval.provide.ts +5 -0
- package/src/ui/approval.service.ts +12 -0
- package/src/ui/conversation/attachment-slots.ts +23 -0
- package/src/ui/conversation/chat-slots.ts +67 -0
- package/src/ui/conversation/conversation-slots.ts +90 -0
- package/src/ui/conversation/conversation.token.ts +6 -0
- package/src/ui/conversation/file-actions-slots.ts +40 -0
- package/src/ui/conversation/index.ts +13 -0
- package/src/ui/conversation/message-quotes-slot.ts +34 -0
- package/src/ui/conversation/open-in-app-slots.ts +64 -0
- package/src/ui/conversation/secret-mask-slot.ts +23 -0
- package/src/ui/conversation/subagent-slots.ts +31 -0
- package/src/ui/conversation/tool-slots.ts +27 -0
- package/src/ui/conversation/trajectory-slot.ts +49 -0
- package/src/ui/conversation/workflow-run-slot.ts +31 -0
- package/src/ui/conversation-ui.provide.ts +32 -0
- package/src/ui/deliverable.provide.ts +14 -0
- package/src/ui/deliverable.slot.ts +46 -0
- package/src/ui/index.ts +64 -0
- package/src/ui/input/command-ui-slot.ts +45 -0
- package/src/ui/input/index.ts +5 -0
- package/src/ui/input/input-trigger-slot.ts +27 -0
- package/src/ui/input/input.token.ts +2 -0
- package/src/ui/input/reference-slot.ts +42 -0
- package/src/ui/model-selection-slots.ts +50 -0
- package/src/ui/permission-slots.provide.ts +10 -0
- package/src/ui/permission-ui.provide.ts +5 -0
- package/src/ui/ports.ts +3 -0
- package/src/ui/projections/projections.service.ts +220 -0
- package/src/ui/projections/provide.ts +11 -0
- package/src/ui/remote-namespace.ts +24 -0
- package/src/ui/remote-session.ts +220 -0
- package/src/ui/session-controller.service.ts +335 -0
- package/src/ui/session-layout.ts +44 -0
- package/src/ui/session-reference-endpoint.client.ts +37 -0
- package/src/ui/session-scope.contribution.ts +81 -0
- package/src/ui/session-status-slots.ts +45 -0
- package/src/ui/session-submission.ts +108 -0
- package/src/ui/session-ui.provide.ts +51 -0
- package/src/ui/sessions.provide.ts +48 -0
- package/src/ui/setting-ui.provide.ts +35 -0
- package/src/ui/settings-general-slots.ts +66 -0
- package/src/ui/settings-host.ts +28 -0
- package/src/ui/settings-models-slots.ts +43 -0
- package/src/ui/shell/directory-picker-native-slots.ts +83 -0
- package/src/ui/shell/index.ts +74 -0
- package/src/ui/shell/rightbar-seats.ts +321 -0
- package/src/ui/shell/session-panel-slots.ts +33 -0
- package/src/ui/shell/sidebar-document-preview-slots.ts +62 -0
- package/src/ui/shell/sidebar-slots.ts +33 -0
- package/src/ui/shell/workspace-slots.ts +75 -0
- package/src/ui/ui-permission.service.ts +78 -0
- package/src/ui/ui-session.token.ts +5 -0
- package/src/ui/ui-settings.token.ts +9 -0
- package/src/ui/upstream-context.ts +27 -0
- package/src/ui/user-questions-slots.ts +39 -0
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// 会话域的**投影中继**:把 `session/control` 这条宿主流接到每个会话的 `ProjectionValueStore` 上。
|
|
2
|
+
//
|
|
3
|
+
// 为什么在会话域而不是框架:`session` 这个命名空间与 `SessionControlFrame` 的形状都是**本域的 wire 事实**
|
|
4
|
+
// (宿主 `SessionControlController` 算投影、推 `baseline` + `projection` 帧)。框架只留通用的
|
|
5
|
+
// `ProjectionValueStore`(键 → 值 + seq 语义),不解释任何一个命名空间。
|
|
6
|
+
//
|
|
7
|
+
// 分工:
|
|
8
|
+
// - 宿主:算投影、推帧;
|
|
9
|
+
// - 本域(这里):维护"会话 → 投影 store",消费那条流,按 `seq` 语义写入;
|
|
10
|
+
// - 消费方:只读 `faceOf(sessionId, key)`,不碰流、不碰 seq。
|
|
11
|
+
//
|
|
12
|
+
// 少了这一层,每个消费方都要自己 for-await 一条流、自己写 seq 比较——就会各自写出"偶尔回退"的时序 bug。
|
|
13
|
+
import { inject, Injectable, type OnDispose, type OnInit } from "@morlay/zephyr";
|
|
14
|
+
import { ClientConnection, Remote } from "@morlay/zephyr-app/contract";
|
|
15
|
+
import { ProjectionValueStore, type ProjectionFace } from "@morlay/zephyr-app/client";
|
|
16
|
+
import { remoteNamespaceOf } from "@morlay/zephyr-app/client/remote";
|
|
17
|
+
|
|
18
|
+
/** 本层读的那一组 namespace(`session/control` 那条流):等挂载与读方法都按它来,不各写一次字面量。 */
|
|
19
|
+
const SESSION_NAMESPACE = "session";
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* 一帧控制流(上游 `SessionControlFrame`):`baseline`(每条流一代一条)+ `projection`(每个提交事件一条)。
|
|
23
|
+
*
|
|
24
|
+
* `projection` 帧带的是 `changes: [{ key, val }]`——**没有** `key` / `value` 两个顶层字段;
|
|
25
|
+
* 那种旧形状(`{ type: 'projection', key, value }`)宿主早就不发了。
|
|
26
|
+
*/
|
|
27
|
+
interface ControlFrame {
|
|
28
|
+
readonly type?: string;
|
|
29
|
+
readonly value?: {
|
|
30
|
+
readonly projections?: Record<
|
|
31
|
+
string,
|
|
32
|
+
{ readonly asOfSeq?: number; readonly values?: Record<string, unknown> }
|
|
33
|
+
>;
|
|
34
|
+
};
|
|
35
|
+
readonly sessionId?: string;
|
|
36
|
+
readonly seq?: number;
|
|
37
|
+
readonly changes?: readonly { readonly key: string; readonly val?: unknown }[];
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
/** 框架对外给出的投影读面。 */
|
|
41
|
+
@Injectable()
|
|
42
|
+
export abstract class Projections {
|
|
43
|
+
/**
|
|
44
|
+
* 某个会话的一个投影键的读面(identity-stable,可先订阅后才有值)。
|
|
45
|
+
* @param sessionId - 会话身份。
|
|
46
|
+
* @param key - 投影键(`permissions` / `modelSelection` / `title`…)。
|
|
47
|
+
* @returns 读面。
|
|
48
|
+
*/
|
|
49
|
+
abstract faceOf(sessionId: string, key: string): ProjectionFace;
|
|
50
|
+
|
|
51
|
+
/**
|
|
52
|
+
* 某个会话的整份投影 store(要 `values()` / `subscribeAny()` 的消费方用)。
|
|
53
|
+
* @param sessionId - 会话身份。
|
|
54
|
+
* @returns store(不存在就建一个空的)。
|
|
55
|
+
*/
|
|
56
|
+
abstract storeOf(sessionId: string): ProjectionValueStore;
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* 用一帧基线校准某个会话(`session/follow` 首帧的 `projections` 走这里)。
|
|
60
|
+
* @param sessionId - 会话身份。
|
|
61
|
+
* @param baseline - 基线块。
|
|
62
|
+
*/
|
|
63
|
+
abstract seed(
|
|
64
|
+
sessionId: string,
|
|
65
|
+
baseline: { readonly asOfSeq: number; readonly values: Readonly<Record<string, unknown>> },
|
|
66
|
+
): void;
|
|
67
|
+
|
|
68
|
+
/**
|
|
69
|
+
* 用列表行上的 cached 值填空(只在没有 sequenced 行时生效)。
|
|
70
|
+
* @param sessionId - 会话身份。
|
|
71
|
+
* @param values - 键 → 值。
|
|
72
|
+
*/
|
|
73
|
+
abstract applyCached(sessionId: string, values: Readonly<Record<string, unknown>>): void;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
/** 默认实现:订阅 `remote.session.control`,把帧分发给对应会话的 store。 */
|
|
77
|
+
@Injectable()
|
|
78
|
+
export class ProjectionsDefault extends Projections implements OnInit, OnDispose {
|
|
79
|
+
private readonly remote = inject(Remote);
|
|
80
|
+
|
|
81
|
+
/**
|
|
82
|
+
* 连接面:**可选**。有它才跟连接代换代(换连接 = 旧值作废);
|
|
83
|
+
* 只装数据面的场景(会话目录的单元测试、纯 node)没有它,按单代跑。
|
|
84
|
+
*/
|
|
85
|
+
private readonly connection = inject.optional(ClientConnection);
|
|
86
|
+
|
|
87
|
+
private readonly stores = new Map<string, ProjectionValueStore>();
|
|
88
|
+
|
|
89
|
+
/** 当前连接代:换代时清空所有值(旧代的帧不能留到新一代)。 */
|
|
90
|
+
private generation: number | undefined;
|
|
91
|
+
|
|
92
|
+
private stopStream: (() => void) | undefined;
|
|
93
|
+
|
|
94
|
+
onInit(): void {
|
|
95
|
+
const connection = this.connection;
|
|
96
|
+
if (connection !== undefined) {
|
|
97
|
+
this.generation = connection.generation.getSnapshot()?.id;
|
|
98
|
+
this.stopGeneration = connection.generation.subscribe(() => {
|
|
99
|
+
const next = connection.generation.getSnapshot()?.id;
|
|
100
|
+
if (next === this.generation) return;
|
|
101
|
+
this.generation = next;
|
|
102
|
+
// 换代:值全部作废(面保留),再重开流拿新基线。
|
|
103
|
+
for (const store of this.stores.values()) store.clear();
|
|
104
|
+
this.open();
|
|
105
|
+
});
|
|
106
|
+
}
|
|
107
|
+
// 等**这一组**挂上再开流:`whenMounted()` 只等第一个 contribution,先到的是别的组时
|
|
108
|
+
// `remote.session` 还不存在,`open()` 会打一条告警,而这条流之后没有别的时机再被重开
|
|
109
|
+
// (症状:界面上所有"宿主算出来的值"停在旧值)。
|
|
110
|
+
void this.remote.whenNamespace(SESSION_NAMESPACE).then(() => {
|
|
111
|
+
if (this.disposed) return;
|
|
112
|
+
this.open();
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
faceOf(sessionId: string, key: string): ProjectionFace {
|
|
117
|
+
return this.storeOf(sessionId).faceOf(key);
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* 卸载:先中止流再退出。
|
|
122
|
+
*
|
|
123
|
+
* 中止放在前面,`catch` 里才能用 `signal.aborted` 把"正常收尾"与"真断连"分开——
|
|
124
|
+
* 否则每次关页面都会打一条 `session/control 结束于错误`。
|
|
125
|
+
*/
|
|
126
|
+
onDispose(): void {
|
|
127
|
+
this.disposed = true;
|
|
128
|
+
this.stopStream?.();
|
|
129
|
+
this.stopStream = undefined;
|
|
130
|
+
this.stopGeneration?.();
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/** 卸载标记:`whenMounted()` 迟到的兑现不能再开流。 */
|
|
134
|
+
private disposed = false;
|
|
135
|
+
|
|
136
|
+
/** 连接代的订阅退订函数。 */
|
|
137
|
+
private stopGeneration: (() => void) | undefined;
|
|
138
|
+
|
|
139
|
+
storeOf(sessionId: string): ProjectionValueStore {
|
|
140
|
+
const existing = this.stores.get(sessionId);
|
|
141
|
+
if (existing !== undefined) return existing;
|
|
142
|
+
const store = new ProjectionValueStore();
|
|
143
|
+
this.stores.set(sessionId, store);
|
|
144
|
+
return store;
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
seed(
|
|
148
|
+
sessionId: string,
|
|
149
|
+
baseline: { readonly asOfSeq: number; readonly values: Readonly<Record<string, unknown>> },
|
|
150
|
+
): void {
|
|
151
|
+
this.storeOf(sessionId).seed(baseline);
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
applyCached(sessionId: string, values: Readonly<Record<string, unknown>>): void {
|
|
155
|
+
this.storeOf(sessionId).applyCached(values);
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/** 开一条 `session/control` 流(幂等:先停旧的)。 */
|
|
159
|
+
private open(): void {
|
|
160
|
+
this.stopStream?.();
|
|
161
|
+
const namespace = remoteNamespaceOf(this.remote, SESSION_NAMESPACE);
|
|
162
|
+
const control = namespace?.control;
|
|
163
|
+
if (typeof control !== "function") {
|
|
164
|
+
// 走到这里说明**组挂上了、端点却不在**(服务端版本不配对),是真装配问题:
|
|
165
|
+
// 没有这条流,界面上所有"宿主算出来的值"都不会更新,不静默。
|
|
166
|
+
// 一行摘要,不带栈——这不是可恢复的异常,栈只会在每次装配时重复一遍。
|
|
167
|
+
console.warn(
|
|
168
|
+
`[projections] remote.${SESSION_NAMESPACE}.control 不存在:投影不会随宿主更新(界面会停在旧值)`,
|
|
169
|
+
);
|
|
170
|
+
return;
|
|
171
|
+
}
|
|
172
|
+
const controller = new AbortController();
|
|
173
|
+
this.stopStream = () => {
|
|
174
|
+
controller.abort();
|
|
175
|
+
};
|
|
176
|
+
const stream = (control as (signal: AbortSignal) => AsyncIterable<ControlFrame>).call(
|
|
177
|
+
namespace,
|
|
178
|
+
controller.signal,
|
|
179
|
+
);
|
|
180
|
+
void (async (): Promise<void> => {
|
|
181
|
+
try {
|
|
182
|
+
for await (const frame of stream) {
|
|
183
|
+
this.accept(frame);
|
|
184
|
+
}
|
|
185
|
+
} catch (error) {
|
|
186
|
+
if (!controller.signal.aborted)
|
|
187
|
+
console.error("[projections] session/control 结束于错误:", error);
|
|
188
|
+
}
|
|
189
|
+
})();
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* 一帧控制流 → store(按**字段**分流:有 `changes` 是增量,有 `value` 是基线)。
|
|
194
|
+
* @param frame - 上游 `SessionControlFrame`。
|
|
195
|
+
*/
|
|
196
|
+
private accept(frame: ControlFrame): void {
|
|
197
|
+
// **按字段判,不按 `type` 判**:宽容度必须与上游一致。
|
|
198
|
+
//
|
|
199
|
+
// 上游 `SessionManager.handleControlFrame` 写的是「基线条件 → 换基线,否则读 `sessionId`/`seq`/`changes`」,
|
|
200
|
+
// 它不看 `type`。我们这里按 `type` 判过一次,两边宽容度就不同了:上游换个标签、加个别名、或发个同形状的
|
|
201
|
+
// 新帧,我们就**静默丢帧**——而宽松那边(座位)照常更新,于是「一个界面动、一个界面不动」,只在真机暴露。
|
|
202
|
+
if (frame.changes !== undefined) {
|
|
203
|
+
if (frame.sessionId === undefined) {
|
|
204
|
+
console.warn("[projections] 带 `changes` 却没有 `sessionId` 的 control 帧:%o", frame);
|
|
205
|
+
return;
|
|
206
|
+
}
|
|
207
|
+
const store = this.storeOf(frame.sessionId);
|
|
208
|
+
for (const change of frame.changes) store.apply(change.key, change.val, frame.seq ?? 0);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
if (frame.value !== undefined) {
|
|
212
|
+
for (const [sessionId, block] of Object.entries(frame.value.projections ?? {})) {
|
|
213
|
+
this.storeOf(sessionId).seed({ asOfSeq: block.asOfSeq ?? 0, values: block.values ?? {} });
|
|
214
|
+
}
|
|
215
|
+
return;
|
|
216
|
+
}
|
|
217
|
+
// 既不是增量也不是基线:**不认识的帧只报警、不丢**(上游加新形状时这里先说话,别静默停在旧值)。
|
|
218
|
+
console.warn("[projections] 不认识的 control 帧:%o", frame);
|
|
219
|
+
}
|
|
220
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
// 投影读面的装配:会话域自己装(`ProjectionsDefault` 读的是本域的 `session/control` 流)。
|
|
2
|
+
import { provide } from "@morlay/zephyr";
|
|
3
|
+
import { ProjectionsDefault } from "./projections.service.ts";
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* 投影读面的装配:装一次,之后本域所有消费方都从 `Projections` 读宿主算出来的值。
|
|
7
|
+
*
|
|
8
|
+
* 排在 `provideClientRemote()` 之后(它要 `remote`)与 `provideClientConnection()` 之后(它要 `connection`)。
|
|
9
|
+
* @returns provider 清单。
|
|
10
|
+
*/
|
|
11
|
+
export const provideProjections = provide([ProjectionsDefault]);
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
// 上游 `Remote` 的 namespace 面:全包的「按名字取一条 face」只走这里。
|
|
2
|
+
//
|
|
3
|
+
// 为什么需要它:`Remote` 契约(`@morlay/zephyr-app` 的 `remote.contract.ts`)**故意不列 namespace**,
|
|
4
|
+
// 也不给按名字查的方法(那会退化成 service locator):每个 namespace 由一条 `@RemoteContract` 各自成
|
|
5
|
+
// 契约,运行期才由 `RemoteDefault` 的 Proxy 按 `$mount` 登记的 descriptor 回落出来。静态面上没有这些
|
|
6
|
+
// 成员可写,读它只能经 `Reflect.get`。
|
|
7
|
+
//
|
|
8
|
+
// 断言只到**松散形状**(`Record<string, unknown>`),不冒充具体 face:调用方按自己真正要的那几个成员
|
|
9
|
+
// 收窄一次(`face as { control?(signal): AsyncIterable<unknown> }`)。`@morlay/zephyr-app` 内部有同一份
|
|
10
|
+
// 读法(`src/client/remote/dynamic-face.ts` 的 `remoteNamespaceOf`),但它没从任何出口导出来——出口面
|
|
11
|
+
// 归 zephyr-app 那张票;它一旦导出,本文件删掉、调用点换个名字即可。
|
|
12
|
+
import type { Remote } from "@morlay/zephyr-app/client";
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* 上游 `Remote` 的一个 namespace 面(`remote.<name>`)。
|
|
16
|
+
* @param remote - `Remote` 契约实例。
|
|
17
|
+
* @param name - 上游 namespace 名(`session` / `commands` / `subagents` / `permissionPresets`…)。
|
|
18
|
+
* @returns 成员名 → 成员;这一组还没 `$mount` 时 `undefined`。
|
|
19
|
+
*/
|
|
20
|
+
export function namespaceOf(remote: Remote, name: string): Record<string, unknown> | undefined {
|
|
21
|
+
// namespace 是实例上的动态属性(Proxy 回落出来的),`Reflect.get` 是读它的正常入口。
|
|
22
|
+
const face: unknown = Reflect.get(remote, name);
|
|
23
|
+
return typeof face === "object" && face !== null ? (face as Record<string, unknown>) : undefined;
|
|
24
|
+
}
|
|
@@ -0,0 +1,220 @@
|
|
|
1
|
+
// 上游 `SessionManager` 要的三组面:`session` 以**会话域的 `SessionEndpointClient`** 为主,
|
|
2
|
+
// `commands` / `subagents` 接 `Remote` 的 namespace。
|
|
3
|
+
//
|
|
4
|
+
// 为什么一元方法不走 namespace:`Remote.session` 要 `$mount` 之后才存在,而 `SessionRemote`
|
|
5
|
+
// 是装配时就有的契约服务(所有 spec 的替身都实现它)。接 namespace 会让测试替身失效
|
|
6
|
+
// (「会话组还没挂上时 `list` 不是函数」),也会把"装配期就能用"变成"必须等 $mount"。
|
|
7
|
+
//
|
|
8
|
+
// 为什么流式方法反过来走 namespace:`follow` / `control` 这类长流只在真机上有生成面,
|
|
9
|
+
// 契约服务不声明它们——声明了,每个只关心目录的替身都得再实现一遍空流。
|
|
10
|
+
import { RemoteStream, type RemoteStreamOptions } from "@deepseek-ai/dsh-api-gateway/client";
|
|
11
|
+
import type { SessionRemotes } from "@deepseek-ai/dsh-api-session-controller/src/client/sessions/remotes.ts";
|
|
12
|
+
import type { CommandRunner } from "@morlay/dsh-command/contract";
|
|
13
|
+
import { splitCommandLine } from "@morlay/dsh-command/endpoint";
|
|
14
|
+
import type { ClientConnection, Remote } from "@morlay/zephyr-app/client";
|
|
15
|
+
import type { SessionEndpointClient } from "@morlay/dsh-session/ui";
|
|
16
|
+
import { namespaceOf } from "./remote-namespace.ts";
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* 没有连接面时的单代 generation:纯数据面测试按一代跑,重连等待永不触发。
|
|
20
|
+
* @returns 恒定的一代。
|
|
21
|
+
*/
|
|
22
|
+
function singleGeneration(): Pick<ClientConnection["generation"], "getSnapshot" | "subscribe"> {
|
|
23
|
+
return {
|
|
24
|
+
getSnapshot: () => ({ id: 0, host: { home: "" } }),
|
|
25
|
+
subscribe: () => () => {},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/** 一次命令调用的结算面:成功给上游要的 `{ matched }`,失败给 `code` / `message`。 */
|
|
30
|
+
type CommandsWireResult =
|
|
31
|
+
| { readonly ok: true; readonly value: unknown }
|
|
32
|
+
| { readonly ok: false; readonly error: { readonly code: string; readonly message: string } };
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 把一次调用结果转发出去:逐帧面(异步迭代器)原样给,一元结果包成上游要的 `RemoteResult`。
|
|
36
|
+
* @param outcome - 成员调用返回的 Promise 或值。
|
|
37
|
+
* @returns 上游要的形状。
|
|
38
|
+
*/
|
|
39
|
+
function forwarded(outcome: unknown): unknown {
|
|
40
|
+
if (outcome !== null && typeof outcome === "object" && Symbol.asyncIterator in outcome)
|
|
41
|
+
return outcome;
|
|
42
|
+
return asRemoteResult(outcome);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* 逐帧句柄(`follow` / `control`):这类返回值是 async iterable,不能包成 `RemoteResult`。
|
|
47
|
+
* @param value - 服务方法或 namespace 方法的返回值。
|
|
48
|
+
* @returns 是不是逐帧面。
|
|
49
|
+
*/
|
|
50
|
+
function isAsyncIterable(value: unknown): value is AsyncIterable<unknown> {
|
|
51
|
+
return (
|
|
52
|
+
typeof value === "object" &&
|
|
53
|
+
value !== null &&
|
|
54
|
+
typeof (value as { [Symbol.asyncIterator]?: unknown })[Symbol.asyncIterator] === "function"
|
|
55
|
+
);
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/** 上游 `SessionRemotes` 的活实现。 */
|
|
59
|
+
export class LiveSessionRemotes implements SessionRemotes {
|
|
60
|
+
/**
|
|
61
|
+
* @param sessionRemoteOf - 取 `session` 组的客户端形态(装配期就有)。
|
|
62
|
+
* @param commandRunnerOf - 取我们的 `CommandRunner` 契约服务(上游 `commands.execute` 映射到它)。
|
|
63
|
+
* @param remoteOf - 取 `Remote` 面(`subagents` 这个 namespace 从这里来)。
|
|
64
|
+
* @param connectionOf - 取连接面:上游 `RemoteStream` 用它跟连接代换代(没装就给单代)。
|
|
65
|
+
*/
|
|
66
|
+
constructor(
|
|
67
|
+
private readonly sessionRemoteOf: () => SessionEndpointClient,
|
|
68
|
+
private readonly commandRunnerOf: () => CommandRunner,
|
|
69
|
+
private readonly remoteOf: () => Remote,
|
|
70
|
+
private readonly connectionOf: () => ClientConnection | undefined,
|
|
71
|
+
) {}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* 上游 `ClientRemote` 的流面:**按上游 `RemoteStream` 的生命周期实现**。
|
|
75
|
+
*
|
|
76
|
+
* 上游的流消费方(`SessionEventStream`、`session/control`)传的是
|
|
77
|
+
* `{ name, open, ended }` 并要求逐帧带 `generation`(换代时据它重发基线);
|
|
78
|
+
* 我们 `Remote.$stream` 的签名是另一个形状(`args` + 裸帧),转发过去只会拿到空流,
|
|
79
|
+
* 于是 `session/follow` 永远停在 `loading`——事件窗口与投影都收不到帧。
|
|
80
|
+
*/
|
|
81
|
+
get $stream(): SessionRemotes["$stream"] {
|
|
82
|
+
return (<Item>(options: RemoteStreamOptions<Item>) =>
|
|
83
|
+
new RemoteStream<Item>(
|
|
84
|
+
{ generation: this.connectionOf()?.generation ?? singleGeneration() },
|
|
85
|
+
options,
|
|
86
|
+
)) as SessionRemotes["$stream"];
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
/**
|
|
90
|
+
* `session` 面:契约服务有的方法走契约服务(一元结果包成上游要的 `RemoteResult`),
|
|
91
|
+
* 没有的回落到 `Remote.session` namespace。
|
|
92
|
+
*
|
|
93
|
+
* **流式方法原样转发**:契约的客户端形态也有 `follow` / `control`(逐帧句柄),包成 `RemoteResult`
|
|
94
|
+
* 会把流拍平成 `{ ok, value }`——消费方随后拿到 `stream is not async iterable`。
|
|
95
|
+
*/
|
|
96
|
+
get session(): SessionRemotes["session"] {
|
|
97
|
+
const service = this.sessionRemoteOf();
|
|
98
|
+
// target 是空对象:`get` / `has` 全被下面两个陷阱接住,不会被读。
|
|
99
|
+
return new Proxy({} as SessionRemotes["session"], {
|
|
100
|
+
get: (_target, property) => {
|
|
101
|
+
const value: unknown = Reflect.get(service, property);
|
|
102
|
+
if (typeof value === "function") {
|
|
103
|
+
return (...args: never[]): unknown => {
|
|
104
|
+
const result: unknown = value.apply(service, args);
|
|
105
|
+
return isAsyncIterable(result) ? result : forwarded(result);
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
const face = this.rawOf("session") ?? {};
|
|
109
|
+
const streamed: unknown = Reflect.get(face, property);
|
|
110
|
+
// 流式方法原样转发:帧(`snapshot` / `event` / `projection`)不能包 `RemoteResult`。
|
|
111
|
+
if (typeof streamed !== "function") return streamed;
|
|
112
|
+
return (...args: never[]): unknown => streamed.apply(face, args);
|
|
113
|
+
},
|
|
114
|
+
has: (_target, property) =>
|
|
115
|
+
Reflect.has(service, property) || Reflect.has(this.rawOf("session") ?? {}, property),
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* `commands` 面:上游传**整行** `/permission danger-full-access`,我们的 `CommandRunner`
|
|
121
|
+
* 要 `(sessionId, name, input)` 三段——在这一层用 `splitCommandLine` 拆开(规则与 `commandLineOf` 成对)。
|
|
122
|
+
*
|
|
123
|
+
* 结算值必须往下传:上游 `ui-permission-presets` 的 `submit` 只按 `ok` / `matched` 判断,
|
|
124
|
+
* 把处理器的 `{ kind: 'error' }` 吞掉就等于"点了切换、什么都没变"。
|
|
125
|
+
*/
|
|
126
|
+
get commands(): SessionRemotes["commands"] {
|
|
127
|
+
const execute = async (
|
|
128
|
+
agentId: Parameters<SessionRemotes["commands"]["execute"]>[0],
|
|
129
|
+
line: string,
|
|
130
|
+
): Promise<CommandsWireResult> => {
|
|
131
|
+
// 拆名与拼回同一份规则(`splitCommandLine` / `commandLineOf`):上游传整行,`CommandRunner`
|
|
132
|
+
// 要「名字 + 输入」两段;wire 上又回到整行。规则漂了就是「名字里带斜杠」这类静默错。
|
|
133
|
+
const { name, input } = splitCommandLine(line);
|
|
134
|
+
try {
|
|
135
|
+
const executed = await this.commandRunnerOf().run(agentId, name, input);
|
|
136
|
+
// 命令被路由到了(注册表认得这个名字)就是 `matched`——处理器自己的失败走 `ok: false`。
|
|
137
|
+
if (executed.result.kind === "error") {
|
|
138
|
+
return {
|
|
139
|
+
ok: false,
|
|
140
|
+
error: { code: "gateway/invoke-failed", message: executed.result.text },
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
return { ok: true, value: { matched: true } };
|
|
144
|
+
} catch (error) {
|
|
145
|
+
const shaped = error as { code?: unknown; message?: unknown };
|
|
146
|
+
return {
|
|
147
|
+
ok: false,
|
|
148
|
+
error: {
|
|
149
|
+
code: typeof shaped.code === "string" ? shaped.code : "gateway/invoke-failed",
|
|
150
|
+
message: typeof shaped.message === "string" ? shaped.message : String(error),
|
|
151
|
+
},
|
|
152
|
+
};
|
|
153
|
+
}
|
|
154
|
+
};
|
|
155
|
+
// 单断言:上游 `RemoteResult` 的失败面是 `RemoteError` 实例(`code` / `message` / `details`),
|
|
156
|
+
// 我们这边是从抛出值折出来的**同形**对象(zephyr-app 的 `failureOf` 也是这么折的)——
|
|
157
|
+
// 上游只读 `ok` / `error.code`,两边的差别只在类型名。
|
|
158
|
+
return { execute } as SessionRemotes["commands"];
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
/** `subagents` namespace(走 `Remote`):每次读成员才解析(`$mount` 之前不读)。 */
|
|
162
|
+
get subagents(): SessionRemotes["subagents"] {
|
|
163
|
+
return new Proxy({} as SessionRemotes["subagents"], {
|
|
164
|
+
get: (_target, property) => {
|
|
165
|
+
const face = this.rawOf("subagents");
|
|
166
|
+
if (face === undefined) {
|
|
167
|
+
throw new Error(
|
|
168
|
+
'remote.subagents is not mounted yet; await whenNamespace("subagents") first',
|
|
169
|
+
);
|
|
170
|
+
}
|
|
171
|
+
const value: unknown = Reflect.get(face, property);
|
|
172
|
+
if (typeof value !== "function") return value;
|
|
173
|
+
return (...args: never[]): unknown => forwarded(value.apply(face, args));
|
|
174
|
+
},
|
|
175
|
+
has: (_target, property) => Reflect.has(this.rawOf("subagents") ?? {}, property),
|
|
176
|
+
});
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* `Remote` 上的一个 namespace 原始面:`$mount` 之前读不到,给 `undefined`。
|
|
181
|
+
* @param key - `session` / `subagents`。
|
|
182
|
+
* @returns 当前面,或 `undefined`。
|
|
183
|
+
*/
|
|
184
|
+
private rawOf(key: "session" | "subagents"): Record<string, unknown> | undefined {
|
|
185
|
+
return namespaceOf(this.remoteOf(), key);
|
|
186
|
+
}
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/**
|
|
190
|
+
* 把一次一元调用包成上游的 `RemoteResult`。
|
|
191
|
+
*
|
|
192
|
+
* 成功给 `{ ok: true, value }`;失败给 `{ ok: false, error }`,`error` 取抛出值的 `code`/`message`
|
|
193
|
+
* (上游读 `result.error.code` 分流,例如 `workspaceAttachSessionId`)。
|
|
194
|
+
* @param outcome - 调用返回的 Promise 或值。
|
|
195
|
+
* @returns 上游形状的结果。
|
|
196
|
+
*/
|
|
197
|
+
async function asRemoteResult(
|
|
198
|
+
outcome: unknown,
|
|
199
|
+
): Promise<{ ok: true; value: unknown } | { ok: false; error: { code: string; message: string } }> {
|
|
200
|
+
try {
|
|
201
|
+
const settled = await outcome;
|
|
202
|
+
// 已经是 `RemoteResult` 就原样返回:测试替身与真机面在这里可能不一致
|
|
203
|
+
// (真机给裸值、部分替身给 `{ok, value}`),包装器对两种都成立。
|
|
204
|
+
if (settled !== null && typeof settled === "object" && "ok" in settled) {
|
|
205
|
+
return settled as
|
|
206
|
+
| { ok: true; value: unknown }
|
|
207
|
+
| { ok: false; error: { code: string; message: string } };
|
|
208
|
+
}
|
|
209
|
+
return { ok: true, value: settled };
|
|
210
|
+
} catch (error) {
|
|
211
|
+
const shaped = error as { code?: unknown; message?: unknown };
|
|
212
|
+
return {
|
|
213
|
+
ok: false,
|
|
214
|
+
error: {
|
|
215
|
+
code: typeof shaped.code === "string" ? shaped.code : "gateway/invoke-failed",
|
|
216
|
+
message: typeof shaped.message === "string" ? shaped.message : String(error),
|
|
217
|
+
},
|
|
218
|
+
};
|
|
219
|
+
}
|
|
220
|
+
}
|