@addozhang/dsh-discord 0.4.0 → 0.5.0-alpha.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -14
- package/README.zh.md +12 -13
- package/cordis.patch.yml +1 -1
- package/lib/client/card-controller.d.ts +1 -1
- package/lib/client/card-form.d.ts +2 -1
- package/lib/client/index.d.ts +2 -2
- package/lib/client/slot-contract.d.ts +7 -0
- package/lib/client/snapshot-store.d.ts +1 -1
- package/lib/compose.d.ts +2 -2
- package/lib/compose.js +1 -1
- package/lib/dsh/host-asks.d.ts +96 -0
- package/lib/dsh/host-asks.js +151 -0
- package/lib/dsh/host-events.d.ts +42 -0
- package/lib/dsh/host-events.js +279 -0
- package/lib/dsh/host-face.d.ts +418 -0
- package/lib/dsh/host-face.js +598 -0
- package/lib/features/adapter-status.d.ts +15 -2
- package/lib/features/adapter-status.js +100 -4
- package/lib/features/approval-store.d.ts +1 -1
- package/lib/features/interaction-router.d.ts +2 -2
- package/lib/features/model-control.d.ts +1 -1
- package/lib/features/project-list.d.ts +1 -1
- package/lib/features/project-list.js +1 -1
- package/lib/features/session-resume.d.ts +1 -1
- package/lib/i18n.d.ts +5 -0
- package/lib/i18n.js +14 -0
- package/lib/index.js +121 -67
- package/lib/settings.js +5 -2
- package/lib/startup.d.ts +1 -1
- package/lib/startup.js +14 -7
- package/lib/stream/live.d.ts +13 -36
- package/lib/stream/live.js +157 -86
- package/lib/stream/tool-view.d.ts +12 -2
- package/lib/stream/tool-view.js +32 -2
- package/package.json +10 -12
- package/lib/dsh/api-proxy-face.d.ts +0 -400
- package/lib/dsh/api-proxy-face.js +0 -515
|
@@ -1,515 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* The typed face over the Host's in-process `ctx.apiProxy` service
|
|
3
|
-
* (ApiProxyService, the transport-agnostic gateway's direct implementation).
|
|
4
|
-
* Its domain methods speak the narrow RPC signature: `RpcRequest<P>` in,
|
|
5
|
-
* `RpcResponse<T>` out, business errors on `result.ok === false` — they never
|
|
6
|
-
* throw for business outcomes. Two guarantees are added here:
|
|
7
|
-
*
|
|
8
|
-
* 1. Boundedness — a Host that never answers must not wedge an interaction
|
|
9
|
-
* handler (or a Discord ephemeral) forever; every call races a timeout and
|
|
10
|
-
* resolves to an unobservable outcome instead.
|
|
11
|
-
* 2. Observability — every terminal outcome is reported through the injected
|
|
12
|
-
* log sink, so a silent-void call can never again be misread as a hang.
|
|
13
|
-
*/
|
|
14
|
-
import { parseWorkspaceReference } from '../policy/disclosure.js';
|
|
15
|
-
/** Raised when the Host did not answer within the bounded window. */
|
|
16
|
-
export class RpcTimeoutError extends Error {
|
|
17
|
-
constructor(timeoutMs) {
|
|
18
|
-
super(`dsh apiProxy call did not answer within ${String(timeoutMs)}ms`);
|
|
19
|
-
this.name = 'RpcTimeoutError';
|
|
20
|
-
}
|
|
21
|
-
}
|
|
22
|
-
/** Race one apiProxy promise against a bounded window. */
|
|
23
|
-
export function withRpcTimeout(promise, timeoutMs) {
|
|
24
|
-
let timer;
|
|
25
|
-
const timeout = new Promise((_, reject) => {
|
|
26
|
-
timer = setTimeout(() => { reject(new RpcTimeoutError(timeoutMs)); }, timeoutMs);
|
|
27
|
-
});
|
|
28
|
-
return Promise.race([promise, timeout]).finally(() => {
|
|
29
|
-
if (timer !== undefined)
|
|
30
|
-
clearTimeout(timer);
|
|
31
|
-
});
|
|
32
|
-
}
|
|
33
|
-
/** Default bounded window for unary catalog reads (local, no model work). */
|
|
34
|
-
const CATALOG_TIMEOUT_MS = 5_000;
|
|
35
|
-
/** Default bounded window for prompt admission (the Host may enqueue). */
|
|
36
|
-
const PROMPT_TIMEOUT_MS = 30_000;
|
|
37
|
-
/** Mint a request envelope the way every client shape does. */
|
|
38
|
-
function mintRequest(payload, rpcId) {
|
|
39
|
-
return { rpcId: rpcId ?? crypto.randomUUID(), payload };
|
|
40
|
-
}
|
|
41
|
-
/**
|
|
42
|
-
* The bind flow's catalog verifier: resolves an opaque `ws:` reference
|
|
43
|
-
* against the live workspace list. A well-formed reference the catalog no
|
|
44
|
-
* longer knows — and any malformed one — resolve `stale` (fail-closed, no
|
|
45
|
-
* write can follow); a Host error is `failed`; a timeout is `unknown`.
|
|
46
|
-
*/
|
|
47
|
-
export function createWorkspaceResolver(dsh, options = {}) {
|
|
48
|
-
const port = createWorkspaceCatalogPort(dsh, options);
|
|
49
|
-
return {
|
|
50
|
-
async resolve(reference) {
|
|
51
|
-
const catalog = await port.listWorkspaces();
|
|
52
|
-
if (catalog.outcome !== 'completed') {
|
|
53
|
-
return catalog.outcome === 'unknown' ? { outcome: 'unknown' } : { outcome: 'failed' };
|
|
54
|
-
}
|
|
55
|
-
const id = parseWorkspaceReference(reference);
|
|
56
|
-
const found = id === undefined
|
|
57
|
-
? undefined
|
|
58
|
-
: catalog.workspaces.find(workspace => workspace.id === id);
|
|
59
|
-
return found === undefined
|
|
60
|
-
? { outcome: 'stale' }
|
|
61
|
-
: { outcome: 'found', workspace: { id: found.id, title: found.title } };
|
|
62
|
-
},
|
|
63
|
-
};
|
|
64
|
-
}
|
|
65
|
-
/**
|
|
66
|
-
* The `/project list` catalog port satisfied by the in-process apiProxy.
|
|
67
|
-
* Outcomes follow the port contract: a definitive Host error is `failed`
|
|
68
|
-
* (sanitized before Discord), while a timeout or unreadable body is
|
|
69
|
-
* `unknown` — delivery was not observed, so no retry is implied.
|
|
70
|
-
*/
|
|
71
|
-
export function createWorkspaceCatalogPort(dsh, options = {}) {
|
|
72
|
-
const timeoutMs = options.timeoutMs ?? CATALOG_TIMEOUT_MS;
|
|
73
|
-
const log = options.log;
|
|
74
|
-
return {
|
|
75
|
-
async listWorkspaces() {
|
|
76
|
-
let response;
|
|
77
|
-
try {
|
|
78
|
-
response = await withRpcTimeout(dsh.workspace.list(mintRequest({})), timeoutMs);
|
|
79
|
-
}
|
|
80
|
-
catch (cause) {
|
|
81
|
-
if (cause instanceof RpcTimeoutError) {
|
|
82
|
-
log?.('discord_workspace_list_timeout', { timeoutMs });
|
|
83
|
-
return { outcome: 'unknown' };
|
|
84
|
-
}
|
|
85
|
-
log?.('discord_workspace_list_threw', { cause: String(cause) });
|
|
86
|
-
return { outcome: 'failed' };
|
|
87
|
-
}
|
|
88
|
-
// A malformed envelope is a definitive, sanitized failure: the Host
|
|
89
|
-
// answered, so a retry is safe and semantics stay observable-free.
|
|
90
|
-
const result = response?.result;
|
|
91
|
-
if (result === undefined) {
|
|
92
|
-
log?.('discord_workspace_list_malformed');
|
|
93
|
-
return { outcome: 'failed' };
|
|
94
|
-
}
|
|
95
|
-
if (result.ok) {
|
|
96
|
-
const items = Array.isArray(result.value.items)
|
|
97
|
-
? result.value.items
|
|
98
|
-
: [];
|
|
99
|
-
return {
|
|
100
|
-
outcome: 'completed',
|
|
101
|
-
workspaces: items.map(workspace => ({
|
|
102
|
-
id: workspace.workspaceId,
|
|
103
|
-
title: workspace.title,
|
|
104
|
-
// The registered path rides every workspace.* row (Host
|
|
105
|
-
// WorkspaceView); /session resume scopes candidates by it and
|
|
106
|
-
// /project autocomplete abbreviates it — dropping it here once
|
|
107
|
-
// silently emptied the resume list everywhere (16.46).
|
|
108
|
-
...(typeof workspace.path === 'string' ? { path: workspace.path } : {}),
|
|
109
|
-
})),
|
|
110
|
-
// The registry's archived set rides the workspace.list value
|
|
111
|
-
// (sessions.list rows carry NO archived marker): /session resume
|
|
112
|
-
// subtracts it — resuming an archived session dead-ends in a
|
|
113
|
-
// thread whose turns never run (16.49).
|
|
114
|
-
archivedSessionIds: Array.isArray(result.value.archivedSessionIds)
|
|
115
|
-
? result.value.archivedSessionIds.filter((id) => typeof id === 'string')
|
|
116
|
-
: [],
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
log?.('discord_workspace_list_rejected', {
|
|
120
|
-
code: result.error.code,
|
|
121
|
-
message: result.error.message,
|
|
122
|
-
});
|
|
123
|
-
return { outcome: 'failed' };
|
|
124
|
-
},
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
/**
|
|
128
|
-
* Read one Workspace's full view (title plus canonical path). The path is
|
|
129
|
-
* for the administrator-only ephemeral info response — the disclosure
|
|
130
|
-
* policy owns whether it ever renders; this face only carries it in memory.
|
|
131
|
-
*/
|
|
132
|
-
export async function readWorkspaceDetail(dsh, reference, options = {}) {
|
|
133
|
-
const timeoutMs = options.timeoutMs ?? CATALOG_TIMEOUT_MS;
|
|
134
|
-
const log = options.log;
|
|
135
|
-
let response;
|
|
136
|
-
try {
|
|
137
|
-
response = await withRpcTimeout(dsh.workspace.list(mintRequest({})), timeoutMs);
|
|
138
|
-
}
|
|
139
|
-
catch (cause) {
|
|
140
|
-
if (cause instanceof RpcTimeoutError) {
|
|
141
|
-
log?.('discord_workspace_detail_timeout', { timeoutMs });
|
|
142
|
-
return { outcome: 'unknown' };
|
|
143
|
-
}
|
|
144
|
-
log?.('discord_workspace_detail_threw', { cause: String(cause) });
|
|
145
|
-
return { outcome: 'failed' };
|
|
146
|
-
}
|
|
147
|
-
const result = response?.result;
|
|
148
|
-
if (result === undefined) {
|
|
149
|
-
log?.('discord_workspace_detail_malformed');
|
|
150
|
-
return { outcome: 'failed' };
|
|
151
|
-
}
|
|
152
|
-
if (!result.ok) {
|
|
153
|
-
log?.('discord_workspace_detail_rejected', { code: result.error.code });
|
|
154
|
-
return { outcome: 'failed' };
|
|
155
|
-
}
|
|
156
|
-
const id = parseWorkspaceReference(reference) ?? (reference === '' ? undefined : reference);
|
|
157
|
-
const found = Array.isArray(result.value.items)
|
|
158
|
-
? result.value.items.find(workspace => workspace.workspaceId === id)
|
|
159
|
-
: undefined;
|
|
160
|
-
if (found === undefined)
|
|
161
|
-
return { outcome: 'stale' };
|
|
162
|
-
return {
|
|
163
|
-
outcome: 'found',
|
|
164
|
-
workspace: {
|
|
165
|
-
id: found.workspaceId,
|
|
166
|
-
title: found.title,
|
|
167
|
-
path: typeof found.path === 'string' ? found.path : undefined,
|
|
168
|
-
},
|
|
169
|
-
};
|
|
170
|
-
}
|
|
171
|
-
/**
|
|
172
|
-
* Submit one prompt turn through the in-process apiProxy. A definitive Host
|
|
173
|
-
* error is a rejection carrying the sanitized code; a timeout is `unknown` —
|
|
174
|
-
* the turn may or may not have been admitted, so callers must not resubmit.
|
|
175
|
-
* `options.rpcId` pins the adapter-owned stable request id, which the Host
|
|
176
|
-
* records on the durable `user/message` (`source.rpcId`) for reconciliation.
|
|
177
|
-
* Images (16.50) encode as ordered `image` parts after the text part — the
|
|
178
|
-
* rc.2 `session.prompt` content is parts-shaped at this seam.
|
|
179
|
-
*/
|
|
180
|
-
export async function promptSession(dsh, request, options = {}) {
|
|
181
|
-
return submitPromptTurn(dsh, { ...request, mode: 'queue' }, options);
|
|
182
|
-
}
|
|
183
|
-
/**
|
|
184
|
-
* Steer the session's active turn: `session.prompt` with `mode: 'steer'`,
|
|
185
|
-
* carrying the same stable request-id discipline as the queue path.
|
|
186
|
-
*/
|
|
187
|
-
export async function steerSession(dsh, request, options = {}) {
|
|
188
|
-
return submitPromptTurn(dsh, { ...request, mode: 'steer' }, options);
|
|
189
|
-
}
|
|
190
|
-
async function submitPromptTurn(dsh, request, options) {
|
|
191
|
-
const timeoutMs = options.timeoutMs ?? PROMPT_TIMEOUT_MS;
|
|
192
|
-
const log = options.log;
|
|
193
|
-
let response;
|
|
194
|
-
try {
|
|
195
|
-
response = await withRpcTimeout(dsh.sessions.prompt(mintRequest({
|
|
196
|
-
sessionId: request.sessionId,
|
|
197
|
-
mode: request.mode,
|
|
198
|
-
content: [
|
|
199
|
-
{ type: 'text', text: request.prompt },
|
|
200
|
-
...(request.images ?? []).map(image => ({ type: 'image', mediaType: image.mediaType, data: image.base64 })),
|
|
201
|
-
],
|
|
202
|
-
}, options.rpcId)), timeoutMs);
|
|
203
|
-
}
|
|
204
|
-
catch (cause) {
|
|
205
|
-
if (cause instanceof RpcTimeoutError) {
|
|
206
|
-
log?.('discord_prompt_submit_timeout', { timeoutMs, sessionId: request.sessionId });
|
|
207
|
-
return { outcome: 'unknown' };
|
|
208
|
-
}
|
|
209
|
-
log?.('discord_prompt_submit_threw', { cause: String(cause), sessionId: request.sessionId });
|
|
210
|
-
return { outcome: 'unknown' };
|
|
211
|
-
}
|
|
212
|
-
const result = response?.result;
|
|
213
|
-
if (result === undefined) {
|
|
214
|
-
// An unreadable body leaves admission unobservable: the turn MAY have
|
|
215
|
-
// been admitted, so the at-most-once discipline forbids resubmission.
|
|
216
|
-
log?.('discord_prompt_submit_malformed', { sessionId: request.sessionId });
|
|
217
|
-
return { outcome: 'unknown' };
|
|
218
|
-
}
|
|
219
|
-
if (result.ok) {
|
|
220
|
-
// The wire schema pins accepted to literal true; the Host's own zod
|
|
221
|
-
// parse enforces it before this seam ever sees the body.
|
|
222
|
-
return { outcome: 'accepted' };
|
|
223
|
-
}
|
|
224
|
-
log?.('discord_prompt_submit_rejected', {
|
|
225
|
-
code: result.error.code,
|
|
226
|
-
sessionId: request.sessionId,
|
|
227
|
-
});
|
|
228
|
-
return { outcome: 'rejected', reason: result.error.code };
|
|
229
|
-
}
|
|
230
|
-
/**
|
|
231
|
-
* Create one DSH Session against a preallocated id (design.md §10): DSH
|
|
232
|
-
* adopts the same session id idempotently, so an uncertain response never
|
|
233
|
-
* forks a second Session. Same outcome discipline as the prompt path.
|
|
234
|
-
*/
|
|
235
|
-
export async function createSessionViaProxy(dsh, request, options = {}) {
|
|
236
|
-
const timeoutMs = options.timeoutMs ?? PROMPT_TIMEOUT_MS;
|
|
237
|
-
const log = options.log;
|
|
238
|
-
let response;
|
|
239
|
-
try {
|
|
240
|
-
response = await withRpcTimeout(dsh.sessions.create(mintRequest({
|
|
241
|
-
workspaceId: request.workspaceId,
|
|
242
|
-
sessionId: request.sessionId,
|
|
243
|
-
})), timeoutMs);
|
|
244
|
-
}
|
|
245
|
-
catch (cause) {
|
|
246
|
-
if (cause instanceof RpcTimeoutError) {
|
|
247
|
-
log?.('discord_session_create_timeout', { timeoutMs, sessionId: request.sessionId });
|
|
248
|
-
return { outcome: 'unknown' };
|
|
249
|
-
}
|
|
250
|
-
log?.('discord_session_create_threw', { cause: String(cause), sessionId: request.sessionId });
|
|
251
|
-
return { outcome: 'unknown' };
|
|
252
|
-
}
|
|
253
|
-
const result = response?.result;
|
|
254
|
-
if (result === undefined) {
|
|
255
|
-
log?.('discord_session_create_malformed', { sessionId: request.sessionId });
|
|
256
|
-
return { outcome: 'unknown' };
|
|
257
|
-
}
|
|
258
|
-
if (result.ok) {
|
|
259
|
-
return { outcome: 'completed', sessionId: result.value.sessionId };
|
|
260
|
-
}
|
|
261
|
-
log?.('discord_session_create_rejected', { code: result.error.code, sessionId: request.sessionId });
|
|
262
|
-
return { outcome: 'rejected', reason: result.error.code };
|
|
263
|
-
}
|
|
264
|
-
/** List durable Session ids (`session.list`, v1 returns everything). */
|
|
265
|
-
export async function listSessionIds(dsh, options = {}) {
|
|
266
|
-
const timeoutMs = options.timeoutMs ?? CATALOG_TIMEOUT_MS;
|
|
267
|
-
const log = options.log;
|
|
268
|
-
let response;
|
|
269
|
-
try {
|
|
270
|
-
response = await withRpcTimeout(dsh.sessions.list(mintRequest({})), timeoutMs);
|
|
271
|
-
}
|
|
272
|
-
catch (cause) {
|
|
273
|
-
if (cause instanceof RpcTimeoutError) {
|
|
274
|
-
log?.('discord_session_list_timeout', { timeoutMs });
|
|
275
|
-
return { outcome: 'unknown' };
|
|
276
|
-
}
|
|
277
|
-
log?.('discord_session_list_threw', { cause: String(cause) });
|
|
278
|
-
return { outcome: 'unknown' };
|
|
279
|
-
}
|
|
280
|
-
const result = response?.result;
|
|
281
|
-
if (result === undefined) {
|
|
282
|
-
log?.('discord_session_list_malformed');
|
|
283
|
-
return { outcome: 'failed' };
|
|
284
|
-
}
|
|
285
|
-
if (result.ok) {
|
|
286
|
-
return { outcome: 'completed', ids: Array.isArray(result.value.items) ? result.value.items.map(item => item.sessionId) : [] };
|
|
287
|
-
}
|
|
288
|
-
log?.('discord_session_list_rejected', { code: result.error.code });
|
|
289
|
-
return { outcome: 'failed' };
|
|
290
|
-
}
|
|
291
|
-
/**
|
|
292
|
-
* The rich `sessions.list` for the /session resume surface: titles ride each
|
|
293
|
-
* row's projection values (absence = the session has no title yet), blank
|
|
294
|
-
* sessions are flagged, and rows arrive updatedAt-descending. Defensive
|
|
295
|
-
* narrowing: the wire is untrusted, extra/missing fields never throw.
|
|
296
|
-
*/
|
|
297
|
-
export async function listSessionSummaries(dsh, options = {}) {
|
|
298
|
-
const timeoutMs = options.timeoutMs ?? CATALOG_TIMEOUT_MS;
|
|
299
|
-
const log = options.log;
|
|
300
|
-
let response;
|
|
301
|
-
try {
|
|
302
|
-
response = await withRpcTimeout(dsh.sessions.list(mintRequest({})), timeoutMs);
|
|
303
|
-
}
|
|
304
|
-
catch (cause) {
|
|
305
|
-
if (cause instanceof RpcTimeoutError) {
|
|
306
|
-
log?.('discord_session_summaries_timeout', { timeoutMs });
|
|
307
|
-
return { outcome: 'unknown' };
|
|
308
|
-
}
|
|
309
|
-
log?.('discord_session_summaries_threw', { cause: String(cause) });
|
|
310
|
-
return { outcome: 'unknown' };
|
|
311
|
-
}
|
|
312
|
-
const result = response?.result;
|
|
313
|
-
if (result === undefined || !result.ok || !Array.isArray(result.value.items)) {
|
|
314
|
-
log?.('discord_session_summaries_malformed');
|
|
315
|
-
return { outcome: 'failed' };
|
|
316
|
-
}
|
|
317
|
-
const sessions = [];
|
|
318
|
-
// The wire is untrusted: narrow every row defensively before use.
|
|
319
|
-
const items = Array.isArray(result.value.items) ? result.value.items : [];
|
|
320
|
-
for (const item of items) {
|
|
321
|
-
if (typeof item !== 'object' || item === null)
|
|
322
|
-
continue;
|
|
323
|
-
const row = item;
|
|
324
|
-
if (typeof row.sessionId !== 'string' || row.sessionId === '')
|
|
325
|
-
continue;
|
|
326
|
-
const values = row.projections?.values;
|
|
327
|
-
const title = typeof values?.title === 'string' && values.title !== '' ? values.title : undefined;
|
|
328
|
-
sessions.push({
|
|
329
|
-
sessionId: row.sessionId,
|
|
330
|
-
title,
|
|
331
|
-
updatedAt: typeof row.updatedAt === 'number' ? row.updatedAt : 0,
|
|
332
|
-
running: row.running ?? false,
|
|
333
|
-
blank: row.blank === true,
|
|
334
|
-
cwd: typeof row.cwd === 'string' ? row.cwd : undefined,
|
|
335
|
-
origin: row.origin === 'subagent' ? 'subagent' : undefined,
|
|
336
|
-
});
|
|
337
|
-
}
|
|
338
|
-
return { outcome: 'completed', sessions };
|
|
339
|
-
}
|
|
340
|
-
/** Cancel the session's active turn (`session.cancel`); DSH preserves the pending inbox. */
|
|
341
|
-
export async function cancelSessionViaProxy(dsh, request, options = {}) {
|
|
342
|
-
const timeoutMs = options.timeoutMs ?? CATALOG_TIMEOUT_MS;
|
|
343
|
-
const log = options.log;
|
|
344
|
-
let response;
|
|
345
|
-
try {
|
|
346
|
-
response = await withRpcTimeout(dsh.sessions.cancel(mintRequest({ sessionId: request.sessionId })), timeoutMs);
|
|
347
|
-
}
|
|
348
|
-
catch (cause) {
|
|
349
|
-
if (cause instanceof RpcTimeoutError) {
|
|
350
|
-
log?.('discord_session_cancel_timeout', { timeoutMs, sessionId: request.sessionId });
|
|
351
|
-
return { outcome: 'unknown' };
|
|
352
|
-
}
|
|
353
|
-
log?.('discord_session_cancel_threw', { cause: String(cause), sessionId: request.sessionId });
|
|
354
|
-
return { outcome: 'unknown' };
|
|
355
|
-
}
|
|
356
|
-
const result = response?.result;
|
|
357
|
-
if (result === undefined) {
|
|
358
|
-
log?.('discord_session_cancel_malformed', { sessionId: request.sessionId });
|
|
359
|
-
return { outcome: 'unknown' };
|
|
360
|
-
}
|
|
361
|
-
if (result.ok)
|
|
362
|
-
return { outcome: 'accepted' };
|
|
363
|
-
log?.('discord_session_cancel_rejected', { code: result.error.code, sessionId: request.sessionId });
|
|
364
|
-
return { outcome: 'rejected', reason: result.error.code };
|
|
365
|
-
}
|
|
366
|
-
/** Remove one pending inbox item (`session.updateQueue`, action remove). */
|
|
367
|
-
export async function removeQueueItemViaProxy(dsh, request, options = {}) {
|
|
368
|
-
const timeoutMs = options.timeoutMs ?? CATALOG_TIMEOUT_MS;
|
|
369
|
-
const log = options.log;
|
|
370
|
-
let response;
|
|
371
|
-
try {
|
|
372
|
-
response = await withRpcTimeout(dsh.sessions.updateQueue(mintRequest({
|
|
373
|
-
sessionId: request.sessionId,
|
|
374
|
-
itemId: request.itemId,
|
|
375
|
-
action: { kind: 'remove' },
|
|
376
|
-
})), timeoutMs);
|
|
377
|
-
}
|
|
378
|
-
catch (cause) {
|
|
379
|
-
if (cause instanceof RpcTimeoutError) {
|
|
380
|
-
log?.('discord_queue_remove_timeout', { timeoutMs, sessionId: request.sessionId });
|
|
381
|
-
return { outcome: 'unknown' };
|
|
382
|
-
}
|
|
383
|
-
log?.('discord_queue_remove_threw', { cause: String(cause), sessionId: request.sessionId });
|
|
384
|
-
return { outcome: 'unknown' };
|
|
385
|
-
}
|
|
386
|
-
const result = response?.result;
|
|
387
|
-
if (result === undefined) {
|
|
388
|
-
log?.('discord_queue_remove_malformed', { sessionId: request.sessionId });
|
|
389
|
-
return { outcome: 'unknown' };
|
|
390
|
-
}
|
|
391
|
-
if (result.ok)
|
|
392
|
-
return { outcome: 'accepted' };
|
|
393
|
-
log?.('discord_queue_remove_rejected', { code: result.error.code, sessionId: request.sessionId });
|
|
394
|
-
return { outcome: 'rejected', reason: result.error.code };
|
|
395
|
-
}
|
|
396
|
-
/**
|
|
397
|
-
* The respond face for answerable server-requests (approvals, questions).
|
|
398
|
-
* Builds the full ClientResponse envelope — {type: 'client-response',
|
|
399
|
-
* rpcId, result: {ok: true, value}} is the wire contract; posting the bare
|
|
400
|
-
* payload is silently ignored by the Host (rpcId never resolves) — and
|
|
401
|
-
* maps the RpcReceipt onto the port outcome. Like every call in this module
|
|
402
|
-
* it is bounded (a Host that never resolves the receipt must not wedge an
|
|
403
|
-
* interaction handler or an expiry sweep forever) and never throws: a Host
|
|
404
|
-
* rejection or timeout resolves to `unknown` — the response may or may not
|
|
405
|
-
* have landed, and the callers park the ask `unresolved` on exactly that.
|
|
406
|
-
*/
|
|
407
|
-
export function createClientRespondPort(dsh, options = {}) {
|
|
408
|
-
const log = options.log;
|
|
409
|
-
const timeoutMs = options.timeoutMs ?? PROMPT_TIMEOUT_MS;
|
|
410
|
-
return {
|
|
411
|
-
async respond(rpcId, value) {
|
|
412
|
-
let receipt;
|
|
413
|
-
try {
|
|
414
|
-
const response = await withRpcTimeout(dsh.respond({
|
|
415
|
-
type: 'client-response',
|
|
416
|
-
rpcId,
|
|
417
|
-
result: { ok: true, value },
|
|
418
|
-
}), timeoutMs);
|
|
419
|
-
receipt = response;
|
|
420
|
-
}
|
|
421
|
-
catch (cause) {
|
|
422
|
-
const reason = cause instanceof RpcTimeoutError ? 'discord_client_respond_timeout' : 'discord_client_respond_threw';
|
|
423
|
-
log?.(reason, { rpcId, ...(cause instanceof RpcTimeoutError ? { timeoutMs } : { cause: String(cause) }) });
|
|
424
|
-
return { outcome: 'unknown' };
|
|
425
|
-
}
|
|
426
|
-
const table = receipt;
|
|
427
|
-
log?.('discord_client_respond_receipt', { rpcId, receipt: table ?? null });
|
|
428
|
-
if (table?.accepted === true)
|
|
429
|
-
return { outcome: 'confirmed' };
|
|
430
|
-
if (table?.accepted === false) {
|
|
431
|
-
return { outcome: 'rejected', reason: typeof table.reason === 'string' ? table.reason : 'respond-refused' };
|
|
432
|
-
}
|
|
433
|
-
return { outcome: 'unknown' };
|
|
434
|
-
},
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
/** Bounded window for the per-session model directory read. */
|
|
438
|
-
const MODELS_TIMEOUT_MS = 10_000;
|
|
439
|
-
/**
|
|
440
|
-
* The session's detached model directory: the live selection, whether the
|
|
441
|
-
* current route still serves, and the per-provider catalog groups the
|
|
442
|
-
* /model cascade browses.
|
|
443
|
-
*/
|
|
444
|
-
export async function sessionModels(dsh, request, options = {}) {
|
|
445
|
-
const timeoutMs = options.timeoutMs ?? MODELS_TIMEOUT_MS;
|
|
446
|
-
let response;
|
|
447
|
-
try {
|
|
448
|
-
response = await withRpcTimeout(dsh.sessions.models(mintRequest({ sessionId: request.sessionId })), timeoutMs);
|
|
449
|
-
}
|
|
450
|
-
catch (cause) {
|
|
451
|
-
if (cause instanceof RpcTimeoutError) {
|
|
452
|
-
options.log?.('discord_models_timeout', { sessionId: request.sessionId });
|
|
453
|
-
return { outcome: 'unknown' };
|
|
454
|
-
}
|
|
455
|
-
options.log?.('discord_models_threw', { cause: String(cause), sessionId: request.sessionId });
|
|
456
|
-
return { outcome: 'unknown' };
|
|
457
|
-
}
|
|
458
|
-
const result = response?.result;
|
|
459
|
-
if (result === undefined || !result.ok) {
|
|
460
|
-
options.log?.('discord_models_malformed', {
|
|
461
|
-
sessionId: request.sessionId,
|
|
462
|
-
code: result?.ok === false ? result.error.code : 'malformed',
|
|
463
|
-
});
|
|
464
|
-
return { outcome: 'failed' };
|
|
465
|
-
}
|
|
466
|
-
return { outcome: 'completed', models: result.value };
|
|
467
|
-
}
|
|
468
|
-
/**
|
|
469
|
-
* Select the complete model selection for one session (session.selectModel):
|
|
470
|
-
* the session switches immediately and the Host records the choice as the
|
|
471
|
-
* default for sessions that have not logged their own — the response only
|
|
472
|
-
* proves the session switch, so callers must not claim the persistence
|
|
473
|
-
* outcome (design.md §7).
|
|
474
|
-
*/
|
|
475
|
-
export async function selectSessionModel(dsh, request, options = {}) {
|
|
476
|
-
const timeoutMs = options.timeoutMs ?? PROMPT_TIMEOUT_MS;
|
|
477
|
-
let response;
|
|
478
|
-
try {
|
|
479
|
-
response = await withRpcTimeout(dsh.sessions.selectModel(mintRequest({
|
|
480
|
-
sessionId: request.sessionId,
|
|
481
|
-
provider: request.provider,
|
|
482
|
-
model: request.model,
|
|
483
|
-
...(request.reasoningEffort === undefined ? {} : { reasoningEffort: request.reasoningEffort }),
|
|
484
|
-
})), timeoutMs);
|
|
485
|
-
}
|
|
486
|
-
catch (cause) {
|
|
487
|
-
if (cause instanceof RpcTimeoutError) {
|
|
488
|
-
options.log?.('discord_model_select_timeout', { sessionId: request.sessionId });
|
|
489
|
-
return { outcome: 'unknown' };
|
|
490
|
-
}
|
|
491
|
-
options.log?.('discord_model_select_threw', { cause: String(cause), sessionId: request.sessionId });
|
|
492
|
-
return { outcome: 'unknown' };
|
|
493
|
-
}
|
|
494
|
-
const result = response?.result;
|
|
495
|
-
if (result === undefined) {
|
|
496
|
-
options.log?.('discord_model_select_malformed', { sessionId: request.sessionId });
|
|
497
|
-
return { outcome: 'unknown' };
|
|
498
|
-
}
|
|
499
|
-
if (result.ok) {
|
|
500
|
-
return { outcome: 'completed', selected: result.value.selected };
|
|
501
|
-
}
|
|
502
|
-
options.log?.('discord_model_select_rejected', { code: result.error.code, sessionId: request.sessionId });
|
|
503
|
-
return { outcome: 'rejected', reason: result.error.code };
|
|
504
|
-
}
|
|
505
|
-
/**
|
|
506
|
-
* The /model surface over the real session RPCs: the per-session live
|
|
507
|
-
* directory (`session.models`) and the guarded selection mutation
|
|
508
|
-
* (`session.selectModel`) — the shapes model-control reasons about.
|
|
509
|
-
*/
|
|
510
|
-
export function createModelPort(dsh, options = {}) {
|
|
511
|
-
return {
|
|
512
|
-
models: sessionId => sessionModels(dsh, { sessionId }, options),
|
|
513
|
-
selectModel: request => selectSessionModel(dsh, request, options),
|
|
514
|
-
};
|
|
515
|
-
}
|