@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,335 @@
|
|
|
1
|
+
// sessions 行(浏览器侧):**组合上游 `ClientSessions`**,与 `workspaces` 行组合 `ClientWorkspaceModel` 同构。
|
|
2
|
+
//
|
|
3
|
+
// 为什么不自己实现:上游 `ClientSessions` 已经在构造里接好了整套语义——
|
|
4
|
+
// - `create` 成功后写**占位行**(`recordMutation({ kind: 'placeholder', … })`);
|
|
5
|
+
// - `retain` 按 source 计数,`retainInfo` 暴露 `retainedBy`;
|
|
6
|
+
// - `manager.subscribe` → `projectList()` 把目录投影成 `list` 快照;
|
|
7
|
+
// - dispose 时收敛 scope 与 manager 的静默期。
|
|
8
|
+
// 手写这套(本文件的历史)漏一条就是一类 bug,且只能靠"点某个按钮没反应"发现。
|
|
9
|
+
import { ClientSessions } from "@deepseek-ai/dsh-api-session-controller/src/client/sessions/service.ts";
|
|
10
|
+
import {
|
|
11
|
+
inject,
|
|
12
|
+
Injectable,
|
|
13
|
+
kAdapted,
|
|
14
|
+
kFace,
|
|
15
|
+
kMounted,
|
|
16
|
+
type Context,
|
|
17
|
+
type OnDispose,
|
|
18
|
+
type OnInit,
|
|
19
|
+
} from "@morlay/zephyr";
|
|
20
|
+
import { CommandRunner } from "@morlay/dsh-command/contract";
|
|
21
|
+
import { ClientConnection, Remote } from "@morlay/zephyr-app/client";
|
|
22
|
+
import {
|
|
23
|
+
SessionController,
|
|
24
|
+
type SessionCatalogState,
|
|
25
|
+
type SessionRetainHandle,
|
|
26
|
+
} from "@morlay/dsh-conversation/contract";
|
|
27
|
+
import { sessionGroupName } from "@morlay/dsh-session/endpoint";
|
|
28
|
+
import { SessionEndpointClient } from "@morlay/dsh-session/ui";
|
|
29
|
+
import { LiveSessionRemotes } from "./remote-session.ts";
|
|
30
|
+
import { asUpstreamContext } from "./upstream-context.ts";
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* `onInit` 之前的目录快照:空且 `pending`("还没拉"与"真没有会话"要分得开)。
|
|
34
|
+
*
|
|
35
|
+
* 相位取**上游的** `SessionListPhase`:这里曾经映射成自造的 `'loading'`,那是契约手抄上游形状留下的
|
|
36
|
+
* 第二种取值——同一个快照两种语义,读的人得先知道是"哪个契约的 phase"。
|
|
37
|
+
*/
|
|
38
|
+
const EMPTY_CATALOG_STATE: SessionCatalogState = {
|
|
39
|
+
ids: [],
|
|
40
|
+
byId: {},
|
|
41
|
+
phase: "pending",
|
|
42
|
+
projectionsBySession: {},
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* `onInit` 之前的目录读面:空且 `pending`。
|
|
47
|
+
*
|
|
48
|
+
* 身份**模块级稳定**(`useSyncExternalStore` 的依赖要比得下去重),并且不缓存到实例上。
|
|
49
|
+
*/
|
|
50
|
+
const EMPTY_CATALOG: SessionController["list"] = {
|
|
51
|
+
getSnapshot: () => EMPTY_CATALOG_STATE,
|
|
52
|
+
subscribe: () => () => {},
|
|
53
|
+
};
|
|
54
|
+
|
|
55
|
+
/** 上游 `session/control` 的帧形状(本行只把它转交给 `ClientSessions.handleControlFrame`)。 */
|
|
56
|
+
type ControlFrame = Parameters<ClientSessions["handleControlFrame"]>[0];
|
|
57
|
+
|
|
58
|
+
@Injectable()
|
|
59
|
+
export class SessionControllerDefault extends SessionController implements OnInit, OnDispose {
|
|
60
|
+
// 契约面由上游 `ClientSessions` 发布(服务名 `sessions`,与令牌名同一字符串):本行**不再自己注册**,
|
|
61
|
+
// 否则 `store.provide("sessions")` 与上游构造里那句 `reflect.provide("sessions", this)` 撞车
|
|
62
|
+
// (`service "sessions" has been registered at <root>`)。装配器按 `kAdapted`/`kFace` 在 `onInit` 之后
|
|
63
|
+
// 把上游实例采纳成这个令牌的值(`bootstrap.ts` 的 `bindFace`)。
|
|
64
|
+
readonly [kAdapted] = true;
|
|
65
|
+
|
|
66
|
+
/** 上游实例是否已经挂上(`bindFace` 用它区分「忘了 mount」与「上游本来就不发布」)。 */
|
|
67
|
+
[kMounted] = false;
|
|
68
|
+
|
|
69
|
+
/** 采纳进令牌表的契约面:`onInit` 里拿到上游 `ClientSessions` 之后填。 */
|
|
70
|
+
[kFace]: unknown;
|
|
71
|
+
|
|
72
|
+
private readonly remote = inject(Remote);
|
|
73
|
+
|
|
74
|
+
/** `session` 组的客户端形态:一元方法与 `control` 流都走它(组名与帧形状的 home 在契约类上)。 */
|
|
75
|
+
private readonly sessionEndpoint = inject(SessionEndpointClient);
|
|
76
|
+
|
|
77
|
+
/** `commands.execute` 的真身:我们的命令执行器。 */
|
|
78
|
+
/**
|
|
79
|
+
* 命令执行器:**可选**——只测 `session` 面的装配(如 `sessions-create.spec`)可以不装它。
|
|
80
|
+
* 上游 `commands.execute` 落到这里;没装时那一路报明确的"未挂载"而不是让整个行失败。
|
|
81
|
+
*/
|
|
82
|
+
private readonly commandRunner = inject.optional(CommandRunner);
|
|
83
|
+
|
|
84
|
+
/** 连接面:**可选**——只测数据面的装配没有它,按单代跑(上游流生命周期的重连因此不触发)。 */
|
|
85
|
+
private readonly connection = inject.optional(ClientConnection);
|
|
86
|
+
|
|
87
|
+
private inner: ClientSessions | undefined;
|
|
88
|
+
|
|
89
|
+
onInit(ctx: Context): void {
|
|
90
|
+
// 上游 `ClientSessions` 要一个真 cordis ctx 来挂 scope fiber(`createScope`)与收尾 effect;
|
|
91
|
+
// 行 ctx 就是它。它自己那句 `reflect.provide("sessions", this)` 是这个名字的**唯一**所有者,
|
|
92
|
+
// 本行只把接出来的那一份登记成契约面(见类头 `kAdapted`)。
|
|
93
|
+
this.inner = new ClientSessions(
|
|
94
|
+
asUpstreamContext(ctx),
|
|
95
|
+
new LiveSessionRemotes(
|
|
96
|
+
() => this.sessionEndpoint,
|
|
97
|
+
() => {
|
|
98
|
+
const runner = this.commandRunner;
|
|
99
|
+
if (runner === undefined)
|
|
100
|
+
throw new Error("[sessions] command-runner 未装配:commands.execute 不可用");
|
|
101
|
+
return runner;
|
|
102
|
+
},
|
|
103
|
+
() => this.remote,
|
|
104
|
+
() => this.connection,
|
|
105
|
+
),
|
|
106
|
+
);
|
|
107
|
+
// 契约面 = 上游那一份(装配器 `bindFace` 在 `onInit` 之后采进令牌表)。
|
|
108
|
+
this[kMounted] = true;
|
|
109
|
+
this[kFace] = this.inner;
|
|
110
|
+
// **必须拉一次目录**:上游 `ClientSessions` 的构造只装 `list` 快照(`phase: 'pending'`),
|
|
111
|
+
// 目录要 `refreshList()` 才有行;而 `retain(id)` 的 `resolveTarget` 会校验
|
|
112
|
+
// `summaries` 里有没有这个 id(`sessions.retain: unknown session …`)。
|
|
113
|
+
// 不拉的话:侧栏空、`retain` 全失败、主区永远停在 hero。
|
|
114
|
+
//
|
|
115
|
+
// **等 `session` 组挂上再拉**:`SessionEndpointClient.onInit` 是 async(`await this.remote.$mount(...)`),
|
|
116
|
+
// 不等的话首次 `session/list` 打在「组还没挂载」上——请求根本发不出去,症状是「首次打开会话列表是空的」,
|
|
117
|
+
// 而它一直被当成「还没加载」。`openControl()` 用的就是同一条等待。
|
|
118
|
+
const first = this.inner;
|
|
119
|
+
void (async (): Promise<void> => {
|
|
120
|
+
await this.remote.whenNamespace(sessionGroupName());
|
|
121
|
+
// 等挂载期间可能已经卸载(`dispose()` 把 `inner` 清了):那一份不必再拉。
|
|
122
|
+
if (this.inner !== first) return;
|
|
123
|
+
await first.refresh();
|
|
124
|
+
})().catch((error: unknown) => {
|
|
125
|
+
// 宿主没挂 「会话组」 时目录就是空的,保持空态即可,不炸装配。
|
|
126
|
+
console.warn("[sessions] 首次拉取目录失败:%s", String(error));
|
|
127
|
+
});
|
|
128
|
+
// 宿主推的变化要喂给 `ClientSessions` 的 handle*:它自己只在构造里订阅了 manager 的投影,
|
|
129
|
+
// 不接这条线的话侧栏不会跟着 added/removed/status 更新(列表停在首帧)。
|
|
130
|
+
const sessions = this.inner;
|
|
131
|
+
const offs = [
|
|
132
|
+
this.remote.$on(
|
|
133
|
+
"api-session/added",
|
|
134
|
+
(summary: Parameters<ClientSessions["handleSessionAdded"]>[0]) => {
|
|
135
|
+
sessions.handleSessionAdded(summary);
|
|
136
|
+
},
|
|
137
|
+
),
|
|
138
|
+
this.remote.$on(
|
|
139
|
+
"api-session/removed",
|
|
140
|
+
(id: Parameters<ClientSessions["handleSessionRemoved"]>[0]) => {
|
|
141
|
+
sessions.handleSessionRemoved(id);
|
|
142
|
+
},
|
|
143
|
+
),
|
|
144
|
+
this.remote.$on(
|
|
145
|
+
"api-session/status",
|
|
146
|
+
(
|
|
147
|
+
id: Parameters<ClientSessions["handleSessionStatus"]>[0],
|
|
148
|
+
running: Parameters<ClientSessions["handleSessionStatus"]>[1],
|
|
149
|
+
) => {
|
|
150
|
+
sessions.handleSessionStatus(id, running);
|
|
151
|
+
},
|
|
152
|
+
),
|
|
153
|
+
];
|
|
154
|
+
// 会话内的事件帧交给 `ClientSessions.handleControlFrame`(它管会话的生命周期面)。
|
|
155
|
+
// **与同域的 `ProjectionsDefault` 那条流并存**:它的 `open()` 只把帧折进**它自己**的投影 store,
|
|
156
|
+
// 而 `binding().session.projections`(模型选择、权限 chip 读的都是它)只吃这一条线。
|
|
157
|
+
//
|
|
158
|
+
// 换代必须**重开**:连接一换,旧 socket 上的流就断了,而这条流之后没有别的时机再开
|
|
159
|
+
// (`Projections` 那边也是这么做的)。不重开的症状:界面上所有"宿主算出来的值"冻在旧值,
|
|
160
|
+
// 刷新(= 新装配)才好——模型选择切了没反应就是这个形态。
|
|
161
|
+
this.stopGeneration = this.connection?.generation.subscribe(() => {
|
|
162
|
+
// **先**作废旧代,**再**重开流——顺序照上游 `apply` 的 `connected()`
|
|
163
|
+
// (`vendor/.../api/session-controller/src/client/index.ts`:第一句就是 `sessions.handleConnected()`,
|
|
164
|
+
// 然后才 `control.restart()`)。反过来的话,新代的基线与旧代的值会先在同一个 store 里相遇:
|
|
165
|
+
// 上游 `ProjectionValueStore` 是**高 seq 胜**,旧代留下的更高水位会把新代基线挡掉(`seed` 不覆盖)。
|
|
166
|
+
//
|
|
167
|
+
// 这一句我们以前整条漏了:`handleConnected()` 做三件事(清投影 store / 重拉目录 / 重拉寻址父会话的投影),
|
|
168
|
+
// 一件都不做时症状是「重连之后目录项与投影都冻在旧代,刷新才好」。
|
|
169
|
+
sessions.handleConnected();
|
|
170
|
+
this.openControl();
|
|
171
|
+
});
|
|
172
|
+
this.openControl();
|
|
173
|
+
ctx.effect(() => () => {
|
|
174
|
+
this.stopControl();
|
|
175
|
+
this.stopGeneration?.();
|
|
176
|
+
this.stopGeneration = undefined;
|
|
177
|
+
for (const off of offs) off();
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
onDispose(): void {
|
|
182
|
+
// `ClientSessions` 自己在 `rootCtx.effect` 里做了 dispose(含 manager 的静默期),这里只清引用。
|
|
183
|
+
this.stopControl();
|
|
184
|
+
this.stopGeneration?.();
|
|
185
|
+
this.stopGeneration = undefined;
|
|
186
|
+
this.inner = undefined;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** 停掉当前这条 `session/control` 流(换代重开与卸载共用)。 */
|
|
190
|
+
private stopControl(): void {
|
|
191
|
+
this.stopStream?.();
|
|
192
|
+
this.stopStream = undefined;
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/** 连接代订阅的退订函数。 */
|
|
196
|
+
private stopGeneration: (() => void) | undefined;
|
|
197
|
+
|
|
198
|
+
/** 当前这条 `session/control` 流的停止函数。 */
|
|
199
|
+
private stopStream: (() => void) | undefined;
|
|
200
|
+
|
|
201
|
+
/**
|
|
202
|
+
* 开一条 `session/control` 流(幂等:先停旧的)。
|
|
203
|
+
*
|
|
204
|
+
* 等的是**这个 namespace**(不是"第一次 `$mount`"):组是各行分别挂的,
|
|
205
|
+
* 先挂上的那个与 `session` 组无关;按名字等才不受装配顺序影响。
|
|
206
|
+
*/
|
|
207
|
+
private openControl(): void {
|
|
208
|
+
this.stopControl();
|
|
209
|
+
const sessions = this.inner;
|
|
210
|
+
if (sessions === undefined) return;
|
|
211
|
+
const controller = new AbortController();
|
|
212
|
+
this.stopStream = () => {
|
|
213
|
+
controller.abort();
|
|
214
|
+
};
|
|
215
|
+
void (async (): Promise<void> => {
|
|
216
|
+
try {
|
|
217
|
+
await this.remote.whenNamespace(sessionGroupName());
|
|
218
|
+
if (controller.signal.aborted) return;
|
|
219
|
+
// 契约面把帧声明成裸 `sessionId` + 不透明值(跨端值不引宿主包);上游 `handleControlFrame`
|
|
220
|
+
// 要的是 branded 的同一份帧——同一条 wire 的两种声明,这里按上游那一份转一次。
|
|
221
|
+
const frames = this.sessionEndpoint.control(
|
|
222
|
+
controller.signal,
|
|
223
|
+
) as AsyncIterable<ControlFrame>;
|
|
224
|
+
for await (const frame of frames) {
|
|
225
|
+
sessions.handleControlFrame(frame);
|
|
226
|
+
}
|
|
227
|
+
} catch (error) {
|
|
228
|
+
if (!controller.signal.aborted)
|
|
229
|
+
console.warn("[sessions] session/control 流结束:%s", String(error));
|
|
230
|
+
}
|
|
231
|
+
})();
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** 上游实例;`onInit` 之前访问是装配错误,直接说清楚。 */
|
|
235
|
+
private get sessions(): ClientSessions {
|
|
236
|
+
if (this.inner === undefined) throw new Error("[sessions] 行还没 onInit:装配顺序错了");
|
|
237
|
+
return this.inner;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
/**
|
|
241
|
+
* 目录读面:**上游那份** `ObservableSnapshot<SessionListState>`,原样交出。
|
|
242
|
+
*
|
|
243
|
+
* 不重映射相位、不重打字行:那是契约手抄上游形状的年代留下的第二层(`pending` → `loading`、
|
|
244
|
+
* 行 `unknown`),而快照本来就是上游 `ClientSessions.list` 造的——转一层只会多出一份要同步的形状。
|
|
245
|
+
* 上游 store 的身份自身稳定(`useSyncExternalStore` 的依赖比得下去重),所以这里也不需要缓存包装。
|
|
246
|
+
*/
|
|
247
|
+
get list(): SessionController["list"] {
|
|
248
|
+
// 装配期就可能被别的行的订阅读到(上游 `ui-session-status` 在 status 快照变化时读 `sessions.list`):
|
|
249
|
+
// 那时本行还没 `onInit`,上游 `ClientSessions` 不存在。给「空且 pending」的读面而不是抛——
|
|
250
|
+
// 抛出去只会被第三方 store 的订阅器吞成一条 `subscriber failed`,读的人还是什么都没拿到。
|
|
251
|
+
return this.inner === undefined ? EMPTY_CATALOG : this.inner.list;
|
|
252
|
+
}
|
|
253
|
+
|
|
254
|
+
get searchResultLimit(): SessionController["searchResultLimit"] {
|
|
255
|
+
return this.sessions.searchResultLimit;
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
create(
|
|
259
|
+
opts?: Parameters<SessionController["create"]>[0],
|
|
260
|
+
): ReturnType<SessionController["create"]> {
|
|
261
|
+
return this.sessions.create(opts);
|
|
262
|
+
}
|
|
263
|
+
|
|
264
|
+
fork(opts: Parameters<SessionController["fork"]>[0]): ReturnType<SessionController["fork"]> {
|
|
265
|
+
return this.sessions.fork(opts);
|
|
266
|
+
}
|
|
267
|
+
|
|
268
|
+
search(
|
|
269
|
+
...args: Parameters<SessionController["search"]>
|
|
270
|
+
): ReturnType<SessionController["search"]> {
|
|
271
|
+
return this.sessions.search(...args);
|
|
272
|
+
}
|
|
273
|
+
|
|
274
|
+
refresh(): ReturnType<SessionController["refresh"]> {
|
|
275
|
+
return this.sessions.refresh();
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
refreshProjections(
|
|
279
|
+
sessionId: Parameters<SessionController["refreshProjections"]>[0],
|
|
280
|
+
): ReturnType<SessionController["refreshProjections"]> {
|
|
281
|
+
return this.sessions.refreshProjections(sessionId);
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
subagentAddress(
|
|
285
|
+
sessionId: Parameters<SessionController["subagentAddress"]>[0],
|
|
286
|
+
): ReturnType<SessionController["subagentAddress"]> {
|
|
287
|
+
return this.sessions.subagentAddress(sessionId);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
scopeOf(
|
|
291
|
+
ctx: Parameters<SessionController["scopeOf"]>[0],
|
|
292
|
+
): ReturnType<SessionController["scopeOf"]> {
|
|
293
|
+
// 入参已经是上游要的 cordis `Context`:契约照上游声明,边界转换只剩 `onInit` 那一处(行 ctx 是本仓的)。
|
|
294
|
+
return this.sessions.scopeOf(ctx);
|
|
295
|
+
}
|
|
296
|
+
|
|
297
|
+
scope(id: Parameters<SessionController["scope"]>[0]): ReturnType<SessionController["scope"]> {
|
|
298
|
+
// **不换对象**:`sessionOf` 靠 ctx 上的标签反查会话,包一层就断链;类型也已经是上游 `AgentContext`。
|
|
299
|
+
return this.sessions.scope(id);
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
sessionOf(
|
|
303
|
+
ctx: Parameters<SessionController["sessionOf"]>[0],
|
|
304
|
+
): ReturnType<SessionController["sessionOf"]> {
|
|
305
|
+
return this.sessions.sessionOf(ctx);
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
get(id: string): ReturnType<SessionController["get"]> {
|
|
309
|
+
return this.sessions.binding(id as Parameters<ClientSessions["binding"]>[0]);
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
binding(id: string): ReturnType<SessionController["binding"]> {
|
|
313
|
+
return this.sessions.binding(id as Parameters<ClientSessions["binding"]>[0]);
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
retainInfo(id: string): ReturnType<SessionController["retainInfo"]> {
|
|
317
|
+
return this.sessions.retainInfo(id as Parameters<ClientSessions["retainInfo"]>[0]);
|
|
318
|
+
}
|
|
319
|
+
|
|
320
|
+
retain(
|
|
321
|
+
target: Parameters<SessionController["retain"]>[0],
|
|
322
|
+
options: Parameters<SessionController["retain"]>[1],
|
|
323
|
+
): ReturnType<SessionController["retain"]> {
|
|
324
|
+
// **原样交上游那份引用**:包一层会让上游的 `using` / `[Symbol.dispose]` 语义落空(包装对象不是 Disposable)。
|
|
325
|
+
return this.sessions.retain(target, options);
|
|
326
|
+
}
|
|
327
|
+
|
|
328
|
+
using<T>(
|
|
329
|
+
target: Parameters<SessionController["using"]>[0],
|
|
330
|
+
options: Parameters<SessionController["using"]>[1],
|
|
331
|
+
operation: (reference: SessionRetainHandle) => T | Promise<T>,
|
|
332
|
+
): Promise<T> {
|
|
333
|
+
return this.sessions.using(target, options, operation);
|
|
334
|
+
}
|
|
335
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
// 会话域的行:`ui-session`(会话选中/状态/绑定的对外面 `ctx.uiSession`)。
|
|
2
|
+
import * as UiSessionPlugin from "@deepseek-ai/dsh-client-ui-session/client";
|
|
3
|
+
import { inject, Injectable, type Context } from "@morlay/zephyr";
|
|
4
|
+
import { fromCordis } from "@morlay/zephyr/compat";
|
|
5
|
+
import { SessionController } from "@morlay/dsh-conversation/contract";
|
|
6
|
+
import { Remote, RemoteAccess, Slots } from "@morlay/zephyr-app/client";
|
|
7
|
+
import { UiSession } from "./ui-session.token.ts";
|
|
8
|
+
|
|
9
|
+
@Injectable()
|
|
10
|
+
export class SessionLayout extends fromCordis(UiSession, UiSessionPlugin) {
|
|
11
|
+
private readonly sessions = inject(SessionController);
|
|
12
|
+
|
|
13
|
+
private readonly remoteAccess = inject(RemoteAccess);
|
|
14
|
+
|
|
15
|
+
private readonly slots = inject(Slots);
|
|
16
|
+
|
|
17
|
+
private readonly remote = inject(Remote);
|
|
18
|
+
|
|
19
|
+
override async onInit(ctx: Context): Promise<void> {
|
|
20
|
+
// 上游 `apply` 读 `ctx.sessions` / `ctx.remoteAccess` / `ctx.slots`(+ `inject` 声明的 `remote`):
|
|
21
|
+
// 全部按 deps 交进去(`fromCordis` 的读面替换)。
|
|
22
|
+
try {
|
|
23
|
+
// **显式全给**:上游 `inject = ['sessions', 'slots', 'remoteAccess']`(`ui-session/src/client/index.ts:620`),
|
|
24
|
+
// 它的 `apply` 第一行就是 `new UiSession(ctx, ctx.sessions)`,随后 `ctx.slots.installScope('session', …)`。
|
|
25
|
+
//
|
|
26
|
+
// 以前这里只交 `remoteAccess`,其余「靠 store 名字满足」——但我们的令牌名是 `session-controller`,
|
|
27
|
+
// 上游要的是 `sessions`:**名字对不上 → `ctx.sessions` 是 undefined → `apply` 第一行就抛 →**
|
|
28
|
+
// **整段不执行(含 `installScope`),且静默**。症状是 `scope('session')` 空、右栏永远没有 panel。
|
|
29
|
+
//
|
|
30
|
+
// 「不交 deps 是为了让子服务拿到真 ctx」这条旧理由不成立:`mount` 内部本来就用 `withDeps` 包了
|
|
31
|
+
// `upstreamCtx`(真 ctx),交 deps 只替换**按名字读**的那一层。同目录的 `SessionStatusSlots`
|
|
32
|
+
// 早就把 `remote` / `sessions` / `remoteAccess` 显式交了,工作正常。
|
|
33
|
+
await this.mount(ctx, undefined, {
|
|
34
|
+
remoteAccess: this.remoteAccess,
|
|
35
|
+
sessions: this.sessions,
|
|
36
|
+
slots: this.slots,
|
|
37
|
+
remote: this.remote,
|
|
38
|
+
});
|
|
39
|
+
} catch (error) {
|
|
40
|
+
// 带阶段的错误:不然上游 `apply` 里的失败会被错误边界吞成空白页。
|
|
41
|
+
throw new Error(`SessionLayout.mount 失败:${(error as Error).message}`, { cause: error });
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
// `SessionReferenceEndpoint` 的**客户端形态**:只消费、不实现。
|
|
2
|
+
//
|
|
3
|
+
// 挂载也归它自己(`onInit` 里 `$mount(remoteDescriptorsOf(SessionReferenceEndpoint))`):
|
|
4
|
+
// `remote.sessionReferenceResolver` 这个名字要发布出去——**上游 `ui-reference` 按名字读它**
|
|
5
|
+
// (`ctx.remote.sessionReferenceResolver.candidates(...)`,缺了就是那个 TypeError 的来源)。
|
|
6
|
+
//
|
|
7
|
+
// 转发走 `transport`(我们的组不经过上游 `remote` 的注册表)。
|
|
8
|
+
import { inject, type Context, type OnInit } from "@morlay/zephyr";
|
|
9
|
+
import { Remote, RemoteTransport } from "@morlay/zephyr-app/client";
|
|
10
|
+
import { RemoteClient, groupNameOf, remoteDescriptorsOf } from "@morlay/zephyr-app/remote";
|
|
11
|
+
import { SessionReferenceEndpoint } from "@morlay/dsh-conversation/endpoint";
|
|
12
|
+
|
|
13
|
+
/** `sessionReferenceResolver` 组的客户端契约实例。 */
|
|
14
|
+
@RemoteClient()
|
|
15
|
+
export class SessionReferenceEndpointClient extends SessionReferenceEndpoint implements OnInit {
|
|
16
|
+
/** `remote.sessionReferenceResolver` 这一面(发布给按名字读的上游消费方)。 */
|
|
17
|
+
protected readonly remote = inject(Remote);
|
|
18
|
+
|
|
19
|
+
/** 传输:转发走它。 */
|
|
20
|
+
protected readonly transport = inject(RemoteTransport);
|
|
21
|
+
|
|
22
|
+
/**
|
|
23
|
+
* 挂上这一组的 descriptor:`remote.sessionReferenceResolver` 由它发布。
|
|
24
|
+
* @param ctx - 本行 ctx:disposer 挂在它上面。
|
|
25
|
+
*/
|
|
26
|
+
async onInit(ctx: Context): Promise<void> {
|
|
27
|
+
try {
|
|
28
|
+
const dispose = await this.remote.$mount(remoteDescriptorsOf(SessionReferenceEndpoint));
|
|
29
|
+
ctx.effect(() => dispose);
|
|
30
|
+
} catch (error) {
|
|
31
|
+
throw new Error(
|
|
32
|
+
`挂载 remote.${groupNameOf(SessionReferenceEndpoint)} 失败:${(error as Error).message}`,
|
|
33
|
+
{ cause: error },
|
|
34
|
+
);
|
|
35
|
+
}
|
|
36
|
+
}
|
|
37
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// `session` / `session-maybe` 的 scope adapter:layout 的 main/sidebar 子树会渲染这个 scope,
|
|
2
|
+
// 缺了它渲染直接抛 `SlotAssemblyError`。
|
|
3
|
+
//
|
|
4
|
+
// 真 adapter 由**上游 `ui-session` 行**装(`client/index.ts:633` 的 `ctx.slots.installScope("session", …)`),
|
|
5
|
+
// 它在本仓的那一行是 `session-layout.ts`(`provideSessionShell()`)。本文件是**兜底**:
|
|
6
|
+
// 该行不在的装配里给一份空投影——`session-maybe` 的语义就是「可能没有会话」,空投影正是它的合法形态。
|
|
7
|
+
//
|
|
8
|
+
// 判据(`__tests__/session-scope-fallback.spec.ts` 四条):真 adapter 在就不装;不在才装;
|
|
9
|
+
// **行序两种都成立**(不靠清单顺序碰巧对);台账漏记时 fail loud(不静默占掉这个 scope)。
|
|
10
|
+
import type {
|
|
11
|
+
RootStandardSourceContribution,
|
|
12
|
+
SlotScopeAdapter,
|
|
13
|
+
StandardSourceBinding,
|
|
14
|
+
} from "@deepseek-ai/dsh-client-ui-slots/client";
|
|
15
|
+
import { inject, type Context } from "@morlay/zephyr";
|
|
16
|
+
import { Slot, SlotContribution, SlotScopes } from "@morlay/zephyr-app/client";
|
|
17
|
+
import { UiSession } from "../contract/index.ts";
|
|
18
|
+
|
|
19
|
+
/** 无会话:标准源全空,绑定不带 scope key。 */
|
|
20
|
+
export function absentSessionAdapter(): SlotScopeAdapter {
|
|
21
|
+
const binding: StandardSourceBinding = { key: undefined, hooks: {}, keyedHooks: {}, props: {} };
|
|
22
|
+
const source = { getSnapshot: () => binding, subscribe: () => () => {} };
|
|
23
|
+
return { current: source, bindingSource: () => source };
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** layout / sidebar 的组件读这两个 root 标准 seat(`useSessions` / `useSessionStatus`)。 */
|
|
27
|
+
export const absentSessions: RootStandardSourceContribution = {
|
|
28
|
+
hooks: {
|
|
29
|
+
sessions: { getSnapshot: () => ({ byId: {}, allIds: [] }), subscribe: () => () => {} },
|
|
30
|
+
sessionStatus: { getSnapshot: () => new Map(), subscribe: () => () => {} },
|
|
31
|
+
},
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* 兜底:没有 `ui-session` 行时提供 `session` scope 与 root 标准源,让渲染前提成立。
|
|
36
|
+
*
|
|
37
|
+
* **正确性不依赖清单行序**:下面那个 `uiSession` 字段就是排序边——装配器按注入占位做依赖序
|
|
38
|
+
* (`bootstrap.ts` 的 `requiredTokensOf`),所以本行总在 `ui-session` 行**之后**激活,读台账时
|
|
39
|
+
* 真 adapter 已经装好了。装的是空投影还是让位,由台账那一刻的事实决定,不由谁的清单在前。
|
|
40
|
+
*/
|
|
41
|
+
@Slot({
|
|
42
|
+
scope: { session: absentSessionAdapter() },
|
|
43
|
+
root: absentSessions,
|
|
44
|
+
})
|
|
45
|
+
export class SessionScopeContribution extends SlotContribution {
|
|
46
|
+
/**
|
|
47
|
+
* 已装过的 scope adapter(`SlotScopes` 台账)。**读台账,不读 `Slots.spec`**:
|
|
48
|
+
* `spec` 查的是**位置声明**(slot 的 kind/scope),没有叫 `session` 的位置,那里永远 `undefined`。
|
|
49
|
+
*
|
|
50
|
+
* 自己 inject 而不是用基类的字段:那一个在 `super.onInit(ctx)` 里才就位,而这里要判断的正是
|
|
51
|
+
* 「要不要调 `super.onInit`」。
|
|
52
|
+
*/
|
|
53
|
+
private readonly scopes = inject.optional(SlotScopes);
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* `ui-session` 行的令牌:**只当排序边用**(值在 `onInit` 的第三条判据里读)。
|
|
57
|
+
*
|
|
58
|
+
* 为什么不能省:`ui-session` 与兜底装的是**同一个 scope**(上游 `installScope` 撞车直接抛
|
|
59
|
+
* `slot scope 'session' already has an adapter`)。兜底先跑就会占掉它,真 adapter 随后装不上——
|
|
60
|
+
* 而那正是「右栏空白八轮」的那条链。靠清单顺序躲是脆的(`rightbar-seats.spec.dom.tsx` 就是
|
|
61
|
+
* 用两种行序钉这件事的),所以把「我在你之后」写成注入边,由装配器排序。
|
|
62
|
+
*/
|
|
63
|
+
private readonly uiSession = inject.optional(UiSession);
|
|
64
|
+
|
|
65
|
+
override onInit(ctx: Context): void {
|
|
66
|
+
// 台账不在 = 装的是上游 `SlotRegistry`(dsh 形态),或这一半压根没装渲染面。两种情况下
|
|
67
|
+
// `session` scope 由上游行自己管,本行不介入:硬装会撞它的 adapter。
|
|
68
|
+
if (this.scopes === undefined) return;
|
|
69
|
+
// 真 adapter 已装(排序边保证真行先跑):让位,不重复装。
|
|
70
|
+
if (this.scopes.scopeOf("session") !== undefined) return;
|
|
71
|
+
// 真行在、台账却没有它——那是台账漏记(95dc967f 修的就是这笔丢失)。此时装空投影会把真 adapter
|
|
72
|
+
// 顶掉,症状是「有会话、右栏却恒空」。停在这里,把排查点指到台账那一笔。
|
|
73
|
+
if (this.uiSession !== undefined)
|
|
74
|
+
throw new Error(
|
|
75
|
+
"session scope 兜底:ui-session 行在装配里,但它的 adapter 没进 SlotScopes 台账;" +
|
|
76
|
+
"此时装空投影会占掉这个 scope(上游 installScope 随后撞 already has an adapter)。" +
|
|
77
|
+
"查 SlotsRegistry.installScope 里记台账的那一笔。",
|
|
78
|
+
);
|
|
79
|
+
super.onInit(ctx);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
// 会话界面里「未读标记」那一行:上游 `ui-session-status` 的 **client 行**。
|
|
2
|
+
//
|
|
3
|
+
// 它把子服务发布成 `uiSession.sessionStatus`(`super(ctx, 'uiSession.sessionStatus')`),下游
|
|
4
|
+
// `ui-workspace`(未读标记)与 `ui-sidebar` 都按 `ctx.uiSession.sessionStatus` 读。
|
|
5
|
+
// 装配顺序:`provideSessionShell()`(出 `uiSession`)之后、`provideWorkspaceUi()` 之前。
|
|
6
|
+
import * as UiSessionStatusPlugin from "@deepseek-ai/dsh-client-ui-session-status/client";
|
|
7
|
+
import SessionStatusRemote from "@deepseek-ai/dsh-client-ui-session-status/remote";
|
|
8
|
+
import { inject, Injectable, type Context } from "@morlay/zephyr";
|
|
9
|
+
import { fromCordis } from "@morlay/zephyr/compat";
|
|
10
|
+
import { remoteNamespaceKey } from "@morlay/zephyr-app/client/remote";
|
|
11
|
+
import { Remote, RemoteAccess, Slots } from "@morlay/zephyr-app/client";
|
|
12
|
+
import { UiSession, UiSessionStatus } from "@morlay/dsh-conversation/contract";
|
|
13
|
+
import { SessionController } from "@morlay/dsh-conversation/contract";
|
|
14
|
+
import { namespaceOf } from "./remote-namespace.ts";
|
|
15
|
+
|
|
16
|
+
@Injectable()
|
|
17
|
+
export class SessionStatusSlots extends fromCordis(UiSessionStatus, UiSessionStatusPlugin) {
|
|
18
|
+
private readonly remote = inject(Remote);
|
|
19
|
+
|
|
20
|
+
private readonly remoteAccess = inject(RemoteAccess);
|
|
21
|
+
|
|
22
|
+
private readonly sessions = inject(SessionController);
|
|
23
|
+
|
|
24
|
+
private readonly slots = inject(Slots);
|
|
25
|
+
|
|
26
|
+
private readonly uiSession = inject(UiSession);
|
|
27
|
+
|
|
28
|
+
override async onInit(ctx: Context): Promise<void> {
|
|
29
|
+
// 上游 apply 里 `new SessionReadModel(ctx.remote.sessionStatus)`:这份 descriptor 要**先挂上**,
|
|
30
|
+
// 否则那一面是 `undefined`,读它的 `.follow` 就抛
|
|
31
|
+
// `Cannot read properties of undefined (reading 'follow')`(前端表现是"提醒同步不了")。
|
|
32
|
+
const dispose = await this.remote.$mount(SessionStatusRemote);
|
|
33
|
+
ctx.effect(() => dispose);
|
|
34
|
+
const remote = this.remote;
|
|
35
|
+
await this.mount(ctx, undefined, {
|
|
36
|
+
remote,
|
|
37
|
+
// 带点的名字按**字面键**给出(`fromCordis` 的 deps 校验逐字匹配)。
|
|
38
|
+
[remoteNamespaceKey("sessionStatus")]: namespaceOf(remote, "sessionStatus"),
|
|
39
|
+
remoteAccess: this.remoteAccess,
|
|
40
|
+
sessions: this.sessions,
|
|
41
|
+
slots: this.slots,
|
|
42
|
+
uiSession: this.uiSession,
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
}
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
// 提交路径上的两个**形状转换**:上游组件给的是它自己的 `PromptContentPart[]` / `QueueAction`,
|
|
2
|
+
// 我们的端点契约是自声明的同形类型。这里逐字段校验并复制,不用断言。
|
|
3
|
+
import type { PromptContentPart, QueueAction } from "@morlay/dsh-session/endpoint";
|
|
4
|
+
|
|
5
|
+
/** 上游 `PromptContentPart` 里 `file` 那一支的 receipt id(品牌类型)。 */
|
|
6
|
+
type FileReceiptId = Extract<PromptContentPart, { readonly type: "file" }>["receiptId"];
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* 裸字符串 → 上游 `FileUploadReceiptId`。
|
|
10
|
+
*
|
|
11
|
+
* 上游没有给这个品牌导出工厂(`client/file-upload/src/types.ts:39` 只有类型),品牌又只在编译期
|
|
12
|
+
* (与上游 `brandString` 同一步:`value as T`)——所以在这一处边界铸造一次,值原样不动。
|
|
13
|
+
* @param raw - 上游组件传来的裸字符串。
|
|
14
|
+
* @returns 品牌化的 receipt id。
|
|
15
|
+
*/
|
|
16
|
+
const receiptIdOf = (raw: string): FileReceiptId => raw as FileReceiptId;
|
|
17
|
+
|
|
18
|
+
/** 一段文本(`QueueAction` 的 `edit` 内容)。 */
|
|
19
|
+
interface TextBlockLike {
|
|
20
|
+
readonly type: "text";
|
|
21
|
+
readonly text: string;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* 把一个未知值收窄成文本块。
|
|
26
|
+
* @param value - 上游给的内容段。
|
|
27
|
+
* @returns 文本块或 `undefined`。
|
|
28
|
+
*/
|
|
29
|
+
function textBlockOf(value: unknown): TextBlockLike | undefined {
|
|
30
|
+
if (typeof value !== "object" || value === null) return undefined;
|
|
31
|
+
if (!("type" in value) || value.type !== "text") return undefined;
|
|
32
|
+
if (!("text" in value) || typeof value.text !== "string") return undefined;
|
|
33
|
+
return { type: "text", text: value.text };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* 把上游的内容段收窄成契约里的 `PromptContentPart`(上游同名类型)。
|
|
38
|
+
* @param content - 上游组件传入的内容段。
|
|
39
|
+
* @returns 契约形状的内容段。
|
|
40
|
+
* @throws 遇到无法识别的段时(不静默丢弃内容)。
|
|
41
|
+
*/
|
|
42
|
+
export function promptContentOf(content: readonly unknown[]): readonly PromptContentPart[] {
|
|
43
|
+
return content.map((part) => {
|
|
44
|
+
const text = textBlockOf(part);
|
|
45
|
+
if (text !== undefined) return text;
|
|
46
|
+
if (typeof part !== "object" || part === null || !("type" in part)) {
|
|
47
|
+
throw new Error("[sessions] prompt 内容段缺少 type");
|
|
48
|
+
}
|
|
49
|
+
if (
|
|
50
|
+
part.type === "image" &&
|
|
51
|
+
"mediaType" in part &&
|
|
52
|
+
"data" in part &&
|
|
53
|
+
typeof part.mediaType === "string" &&
|
|
54
|
+
typeof part.data === "string"
|
|
55
|
+
) {
|
|
56
|
+
const mediaType = part.mediaType;
|
|
57
|
+
if (
|
|
58
|
+
mediaType !== "image/png" &&
|
|
59
|
+
mediaType !== "image/jpeg" &&
|
|
60
|
+
mediaType !== "image/webp" &&
|
|
61
|
+
mediaType !== "image/gif"
|
|
62
|
+
) {
|
|
63
|
+
throw new Error(`[sessions] 不支持的图片类型 "${mediaType}"`);
|
|
64
|
+
}
|
|
65
|
+
return {
|
|
66
|
+
type: "image",
|
|
67
|
+
mediaType,
|
|
68
|
+
data: part.data,
|
|
69
|
+
...("name" in part && typeof part.name === "string" ? { name: part.name } : {}),
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
if (part.type === "file" && "receiptId" in part && typeof part.receiptId === "string") {
|
|
73
|
+
return { type: "file", receiptId: receiptIdOf(part.receiptId) };
|
|
74
|
+
}
|
|
75
|
+
throw new Error("[sessions] 无法识别的 prompt 内容段");
|
|
76
|
+
});
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
/**
|
|
80
|
+
* 把上游的队列动作收窄成契约里的 `QueueAction`(上游同名类型)。
|
|
81
|
+
* @param action - 上游组件传入的动作。
|
|
82
|
+
* @returns 契约形状的动作。
|
|
83
|
+
* @throws 遇到无法识别的动作时。
|
|
84
|
+
*/
|
|
85
|
+
export function queueActionOf(action: unknown): QueueAction {
|
|
86
|
+
if (typeof action !== "object" || action === null || !("kind" in action)) {
|
|
87
|
+
throw new Error("[sessions] 队列动作缺少 kind");
|
|
88
|
+
}
|
|
89
|
+
switch (action.kind) {
|
|
90
|
+
case "remove":
|
|
91
|
+
return { kind: "remove" };
|
|
92
|
+
case "steer":
|
|
93
|
+
return { kind: "steer" };
|
|
94
|
+
case "edit": {
|
|
95
|
+
if (!("content" in action) || !Array.isArray(action.content)) {
|
|
96
|
+
throw new Error("[sessions] edit 动作缺少 content");
|
|
97
|
+
}
|
|
98
|
+
const blocks = action.content.map((block) => {
|
|
99
|
+
const text = textBlockOf(block);
|
|
100
|
+
if (text === undefined) throw new Error("[sessions] edit 内容只支持文本块");
|
|
101
|
+
return text;
|
|
102
|
+
});
|
|
103
|
+
return { kind: "edit", content: blocks };
|
|
104
|
+
}
|
|
105
|
+
default:
|
|
106
|
+
throw new Error("[sessions] 无法识别的队列动作");
|
|
107
|
+
}
|
|
108
|
+
}
|