@deepseek-ai/dsh-cordis-client-runner 0.2.2-alpha.3 → 0.2.2-rc.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.
Files changed (34) hide show
  1. package/lib/client.js +2411 -7959
  2. package/lib/client.js.map +1 -0
  3. package/lib/types/client/api-catalog.d.ts.map +1 -0
  4. package/lib/types/client/api-catalog.js.map +1 -0
  5. package/lib/types/client/evaluator.d.ts.map +1 -0
  6. package/lib/types/client/evaluator.js.map +1 -0
  7. package/lib/types/client/guard.d.ts.map +1 -0
  8. package/lib/types/client/guard.js.map +1 -0
  9. package/lib/types/client/index.d.ts.map +1 -0
  10. package/lib/types/client/index.js.map +1 -0
  11. package/lib/types/client/inspect-registry.d.ts.map +1 -0
  12. package/lib/types/client/inspect-registry.js.map +1 -0
  13. package/lib/types/client/orchestrator.d.ts.map +1 -0
  14. package/lib/types/client/orchestrator.js.map +1 -0
  15. package/lib/types/client/providers.d.ts.map +1 -0
  16. package/lib/types/client/providers.js.map +1 -0
  17. package/lib/types/client/runtime.d.ts.map +1 -0
  18. package/lib/types/client/runtime.js.map +1 -0
  19. package/lib/types/client/slot-catalog.d.ts +6 -2
  20. package/lib/types/client/slot-catalog.d.ts.map +1 -0
  21. package/lib/types/client/slot-catalog.js.map +1 -0
  22. package/lib/types/index.d.ts.map +1 -0
  23. package/lib/types/index.js.map +1 -0
  24. package/package.json +14 -11
  25. package/src/client/api-catalog.ts +1208 -0
  26. package/src/client/evaluator.ts +222 -0
  27. package/src/client/guard.ts +245 -0
  28. package/src/client/index.ts +308 -0
  29. package/src/client/inspect-registry.ts +150 -0
  30. package/src/client/orchestrator.ts +458 -0
  31. package/src/client/providers.ts +263 -0
  32. package/src/client/runtime.ts +512 -0
  33. package/src/client/slot-catalog.ts +5782 -0
  34. package/src/index.ts +9 -0
@@ -0,0 +1,458 @@
1
+ /**
2
+ * Page-side run orchestration for model approvals and direct panel gestures.
3
+ * Host activation always precedes Client loading. The same Plugin-keyed state
4
+ * drives every surface, so remounting a panel never loses an open approval or
5
+ * an in-flight transition.
6
+ */
7
+
8
+ import type {
9
+ ApprovalRequestId,
10
+ CordisDynamicPackageId,
11
+ CordisDynamicPluginId,
12
+ CordisDynamicPluginRunId,
13
+ CordisDynamicRunMode,
14
+ DynamicCordisClientSource,
15
+ DynamicCordisHostHalfResult,
16
+ DynamicCordisInventoryRow,
17
+ DynamicCordisResolveAck,
18
+ DynamicCordisRunResolution,
19
+ DynamicCordisRunResponse,
20
+ SessionId,
21
+ } from '@deepseek-ai/dsh-api-remotes/client'
22
+ import { errorDetails } from './runtime.ts'
23
+ import type { CordisErrorDetails, CordisObservable, DynamicCordisPackageRunner } from './runtime.ts'
24
+
25
+ /** One Plugin's in-flight approval or activation. */
26
+ export type CordisRunActivity =
27
+ | {
28
+ phase: 'awaiting-approval'
29
+ requestId: ApprovalRequestId
30
+ agentId: SessionId
31
+ packageId: CordisDynamicPackageId
32
+ mode: CordisDynamicRunMode
33
+ name: string
34
+ purpose: string
35
+ }
36
+ | {
37
+ phase: 'orchestrating'
38
+ agentId: SessionId
39
+ packageId: CordisDynamicPackageId
40
+ mode: CordisDynamicRunMode
41
+ }
42
+
43
+ /** Why this page's latest activation attempt failed. */
44
+ export interface CordisRunFailure {
45
+ /** Package the attempt targeted. */
46
+ packageId: CordisDynamicPackageId
47
+ /** Which half or settlement stage failed. */
48
+ reason: 'host-half-failed' | 'client-half-failed'
49
+ /** Actionable failure text. */
50
+ message: string
51
+ /** Original failure stack when available. */
52
+ stack?: string
53
+ }
54
+
55
+ /** Host operations consumed by the orchestrator after transport folding. */
56
+ export interface CordisRunHostSeam {
57
+ /** Start a new Host activation or attach this page to an existing one. */
58
+ runHostHalf(
59
+ agentId: SessionId,
60
+ pluginId: CordisDynamicPluginId,
61
+ packageId: CordisDynamicPackageId,
62
+ mode: CordisDynamicRunMode,
63
+ requestId: ApprovalRequestId | null,
64
+ approveFutureVersions: boolean,
65
+ ): Promise<DynamicCordisHostHalfResult>
66
+ /** Fetch Client source for one exact active run. */
67
+ getClientCode(
68
+ agentId: SessionId,
69
+ pluginId: CordisDynamicPluginId,
70
+ pluginRunId: CordisDynamicPluginRunId,
71
+ ): Promise<DynamicCordisClientSource>
72
+ /** Settle a model-driven approval. */
73
+ resolveRequestRun(
74
+ requestId: ApprovalRequestId,
75
+ resolution: DynamicCordisRunResolution,
76
+ ): Promise<DynamicCordisResolveAck>
77
+ /** Settle a direct panel activation after this page handles its Client half. */
78
+ settleUserRun(
79
+ agentId: SessionId,
80
+ pluginId: CordisDynamicPluginId,
81
+ resolution: DynamicCordisRunResolution,
82
+ ): Promise<DynamicCordisRunResponse>
83
+ }
84
+
85
+ /** Dependencies of one page's orchestrator. */
86
+ export interface CordisRunOrchestratorEnv {
87
+ /** Page-local Client loader. */
88
+ runner: DynamicCordisPackageRunner
89
+ /** Folded Host RPC operations. */
90
+ host: CordisRunHostSeam
91
+ }
92
+
93
+ /** Forwarded approval request fields used by this page. */
94
+ export interface CordisRunRequest {
95
+ requestId: ApprovalRequestId
96
+ agentId: SessionId
97
+ pluginId: CordisDynamicPluginId
98
+ packageId: CordisDynamicPackageId
99
+ mode: CordisDynamicRunMode
100
+ name: string
101
+ purpose: string
102
+ requiresApproval: boolean
103
+ }
104
+
105
+ /** Direct panel activation request. */
106
+ export interface CordisUserRunRequest {
107
+ agentId: SessionId
108
+ pluginId: CordisDynamicPluginId
109
+ packageId: CordisDynamicPackageId
110
+ mode: CordisDynamicRunMode
111
+ /** Host-only Packages finish without a Client load or settlement call. */
112
+ hasClientHalf: boolean
113
+ }
114
+
115
+ interface RunPlan extends CordisUserRunRequest {
116
+ requestId?: ApprovalRequestId
117
+ approveFutureVersions?: boolean
118
+ }
119
+
120
+ /** Drives Host → Client activation and publishes Plugin-keyed activity. */
121
+ export class CordisRunOrchestrator {
122
+ private readonly requests = new Map<ApprovalRequestId, CordisRunRequest>()
123
+ private readonly activity = new Map<CordisDynamicPluginId, CordisRunActivity>()
124
+ private readonly failures = new Map<CordisDynamicPluginId, CordisRunFailure>()
125
+ private readonly inFlight = new Map<CordisDynamicPluginId, Promise<void>>()
126
+ private readonly listeners = new Set<() => void>()
127
+ private activityCache: ReadonlyMap<CordisDynamicPluginId, CordisRunActivity> | undefined
128
+ private failureCache: ReadonlyMap<CordisDynamicPluginId, CordisRunFailure> | undefined
129
+
130
+ /** @param env - Client loader and folded Host operations. */
131
+ constructor(private readonly env: CordisRunOrchestratorEnv) {}
132
+
133
+ /** Open approvals and current activation attempts, keyed by stable Plugin ID. */
134
+ readonly activeRuns: CordisObservable<ReadonlyMap<CordisDynamicPluginId, CordisRunActivity>> = {
135
+ getSnapshot: () => this.activityCache ??= new Map(this.activity),
136
+ subscribe: fn => this.observe(fn),
137
+ }
138
+
139
+ /** Latest page-side activation failure for each Plugin. */
140
+ readonly lastRunError: CordisObservable<ReadonlyMap<CordisDynamicPluginId, CordisRunFailure>> = {
141
+ getSnapshot: () => this.failureCache ??= new Map(this.failures),
142
+ subscribe: fn => this.observe(fn),
143
+ }
144
+
145
+ /**
146
+ * Register a Client activation request, starting it immediately when the Plugin is already authorized.
147
+ * @param request - forwarded approval and activation metadata.
148
+ */
149
+ open(request: CordisRunRequest): void {
150
+ this.requests.set(request.requestId, request)
151
+ if (!request.requiresApproval) {
152
+ void this.orchestrate({
153
+ agentId: request.agentId,
154
+ pluginId: request.pluginId,
155
+ packageId: request.packageId,
156
+ mode: request.mode,
157
+ requestId: request.requestId,
158
+ hasClientHalf: true,
159
+ }).catch((error: unknown) => {
160
+ console.error(`[cordis-client-runner] automatic activation ${request.requestId} failed:`, error)
161
+ })
162
+ return
163
+ }
164
+ if (this.activity.get(request.pluginId)?.phase !== 'orchestrating') {
165
+ this.activity.set(request.pluginId, {
166
+ phase: 'awaiting-approval',
167
+ requestId: request.requestId,
168
+ agentId: request.agentId,
169
+ packageId: request.packageId,
170
+ mode: request.mode,
171
+ name: request.name,
172
+ purpose: request.purpose,
173
+ })
174
+ }
175
+ this.commit()
176
+ }
177
+
178
+ /**
179
+ * Rebuild pending approvals and automatic Client activations from an authoritative Host inventory read.
180
+ * @param rows - complete process-wide Plugin inventory.
181
+ */
182
+ reconcileApprovals(rows: readonly DynamicCordisInventoryRow[]): void {
183
+ const expected = new Map<ApprovalRequestId, CordisRunRequest>()
184
+ for (const row of rows) {
185
+ const attempt = row.latestRun
186
+ if (attempt?.approvalRequestId === undefined
187
+ || (attempt.status !== 'awaiting-approval'
188
+ && attempt.status !== 'starting-host'
189
+ && attempt.status !== 'client-pending')) continue
190
+ const pkg = row.packages.find(candidate => candidate.packageId === attempt.packageId)
191
+ if (pkg === undefined) continue
192
+ expected.set(attempt.approvalRequestId, {
193
+ requestId: attempt.approvalRequestId,
194
+ agentId: row.agentId,
195
+ pluginId: row.pluginId,
196
+ packageId: attempt.packageId,
197
+ mode: attempt.mode,
198
+ name: pkg.name,
199
+ purpose: pkg.purpose,
200
+ requiresApproval: attempt.requiresApproval ?? attempt.status === 'awaiting-approval',
201
+ })
202
+ }
203
+
204
+ let changed = false
205
+ for (const [requestId, request] of [...this.requests]) {
206
+ if (expected.has(requestId)) continue
207
+ this.requests.delete(requestId)
208
+ const current = this.activity.get(request.pluginId)
209
+ if (current?.phase === 'awaiting-approval' && current.requestId === requestId) {
210
+ this.activity.delete(request.pluginId)
211
+ }
212
+ changed = true
213
+ }
214
+ for (const [requestId, request] of expected) {
215
+ const previous = this.requests.get(requestId)
216
+ const current = this.activity.get(request.pluginId)
217
+ if (!request.requiresApproval && current?.phase === 'orchestrating') continue
218
+ if (request.requiresApproval
219
+ && sameRequest(previous, request)
220
+ && current?.phase === 'awaiting-approval'
221
+ && current.requestId === requestId) continue
222
+ if (!request.requiresApproval) {
223
+ this.open(request)
224
+ changed = true
225
+ continue
226
+ }
227
+ this.requests.set(requestId, request)
228
+ if (current?.phase !== 'orchestrating') {
229
+ this.activity.set(request.pluginId, {
230
+ phase: 'awaiting-approval',
231
+ requestId,
232
+ agentId: request.agentId,
233
+ packageId: request.packageId,
234
+ mode: request.mode,
235
+ name: request.name,
236
+ purpose: request.purpose,
237
+ })
238
+ }
239
+ changed = true
240
+ }
241
+ if (changed) this.commit()
242
+ }
243
+
244
+ /**
245
+ * Close an approval settled by another page or by cancellation.
246
+ * @param requestId - approval request that can no longer be answered here.
247
+ */
248
+ close(requestId: ApprovalRequestId): void {
249
+ const request = this.requests.get(requestId)
250
+ if (request === undefined) return
251
+ this.requests.delete(requestId)
252
+ const current = this.activity.get(request.pluginId)
253
+ if (current?.phase === 'awaiting-approval' && current.requestId === requestId) {
254
+ this.activity.delete(request.pluginId)
255
+ }
256
+ this.commit()
257
+ }
258
+
259
+ /**
260
+ * Approve and execute one still-open model request.
261
+ * @param requestId - approval request to execute.
262
+ * @param approveFutureVersions - whether this approval covers later Packages for the same Plugin.
263
+ */
264
+ approve(requestId: ApprovalRequestId, approveFutureVersions: boolean): Promise<void> {
265
+ const request = this.requests.get(requestId)
266
+ if (request === undefined || !request.requiresApproval) return Promise.resolve()
267
+ return this.orchestrate({
268
+ agentId: request.agentId,
269
+ pluginId: request.pluginId,
270
+ packageId: request.packageId,
271
+ mode: request.mode,
272
+ requestId,
273
+ approveFutureVersions,
274
+ hasClientHalf: true,
275
+ })
276
+ }
277
+
278
+ /**
279
+ * Reject one still-open model request without executing either half.
280
+ * @param requestId - approval request to reject.
281
+ */
282
+ async decline(requestId: ApprovalRequestId): Promise<void> {
283
+ const request = this.requests.get(requestId)
284
+ if (request === undefined || !request.requiresApproval) return
285
+ const current = this.activity.get(request.pluginId)
286
+ if (current?.phase !== 'awaiting-approval' || current.requestId !== requestId) return
287
+ this.requests.delete(requestId)
288
+ this.activity.delete(request.pluginId)
289
+ this.commit()
290
+ await this.answer(requestId, { ok: false, reason: 'rejected' })
291
+ }
292
+
293
+ /**
294
+ * Execute a direct panel run; the user gesture itself authorizes it.
295
+ * @param request - exact Package activation selected by the user.
296
+ */
297
+ startUserRun(request: CordisUserRunRequest): Promise<void> {
298
+ return this.orchestrate(request)
299
+ }
300
+
301
+ private observe(fn: () => void): () => void {
302
+ this.listeners.add(fn)
303
+ return () => { this.listeners.delete(fn) }
304
+ }
305
+
306
+ private commit(): void {
307
+ this.activityCache = undefined
308
+ this.failureCache = undefined
309
+ for (const fn of [...this.listeners]) fn()
310
+ }
311
+
312
+ private orchestrate(plan: RunPlan): Promise<void> {
313
+ const running = this.inFlight.get(plan.pluginId)
314
+ if (running !== undefined) return running
315
+ this.activity.set(plan.pluginId, {
316
+ phase: 'orchestrating',
317
+ agentId: plan.agentId,
318
+ packageId: plan.packageId,
319
+ mode: plan.mode,
320
+ })
321
+ this.failures.delete(plan.pluginId)
322
+ if (plan.requestId !== undefined) this.requests.delete(plan.requestId)
323
+ this.commit()
324
+ const attempt = this.drive(plan).finally(() => {
325
+ this.inFlight.delete(plan.pluginId)
326
+ this.activity.delete(plan.pluginId)
327
+ this.commit()
328
+ })
329
+ this.inFlight.set(plan.pluginId, attempt)
330
+ return attempt
331
+ }
332
+
333
+ private async drive(plan: RunPlan): Promise<void> {
334
+ const started = await this.startHost(plan)
335
+ if (!started.ok) {
336
+ this.fail(plan, 'host-half-failed', started)
337
+ if (plan.requestId !== undefined) {
338
+ await this.answer(plan.requestId, { ...started, reason: 'host-half-failed' })
339
+ }
340
+ return
341
+ }
342
+ if (!plan.hasClientHalf) return
343
+
344
+ let source: DynamicCordisClientSource
345
+ try {
346
+ source = await this.env.host.getClientCode(plan.agentId, plan.pluginId, started.pluginRunId)
347
+ } catch (error) {
348
+ await this.finishClientFailure(plan, started.pluginRunId, started.startedHere, errorDetails(error), error)
349
+ return
350
+ }
351
+ const loaded = await this.env.runner.load({
352
+ pluginId: source.pluginId,
353
+ packageId: source.packageId,
354
+ pluginRunId: source.pluginRunId,
355
+ agentId: plan.agentId,
356
+ name: source.name,
357
+ code: source.code,
358
+ }).catch((error: unknown) => ({ ok: false, cause: 'evaluate', ...errorDetails(error), error }) as const)
359
+ if (!loaded.ok) {
360
+ await this.finishClientFailure(
361
+ plan,
362
+ started.pluginRunId,
363
+ started.startedHere,
364
+ {
365
+ message: `${loaded.cause}: ${loaded.message}`,
366
+ ...loaded.stack === undefined ? {} : { stack: loaded.stack },
367
+ },
368
+ loaded.error,
369
+ )
370
+ return
371
+ }
372
+ const resolution: DynamicCordisRunResolution = {
373
+ ok: true,
374
+ pluginRunId: loaded.pluginRunId,
375
+ ...loaded.waitingFor === undefined ? {} : { waitingFor: loaded.waitingFor },
376
+ }
377
+ if (plan.requestId !== undefined) {
378
+ await this.answer(plan.requestId, resolution)
379
+ return
380
+ }
381
+ await this.settleDirect(plan, resolution)
382
+ }
383
+
384
+ private async startHost(plan: RunPlan): Promise<DynamicCordisHostHalfResult> {
385
+ try {
386
+ return await this.env.host.runHostHalf(
387
+ plan.agentId,
388
+ plan.pluginId,
389
+ plan.packageId,
390
+ plan.mode,
391
+ plan.requestId ?? null,
392
+ plan.approveFutureVersions ?? false,
393
+ )
394
+ } catch (error) {
395
+ return { ok: false, ...errorDetails(error) }
396
+ }
397
+ }
398
+
399
+ private async finishClientFailure(
400
+ plan: RunPlan,
401
+ pluginRunId: CordisDynamicPluginRunId,
402
+ startedHere: boolean,
403
+ failure: CordisErrorDetails,
404
+ originalError?: unknown,
405
+ ): Promise<void> {
406
+ console.error(
407
+ `[cordis-client-runner] Client activation ${plan.pluginId}/${plan.packageId} (${pluginRunId}) failed:`,
408
+ originalError ?? failure,
409
+ )
410
+ this.fail(plan, 'client-half-failed', failure)
411
+ const resolution: DynamicCordisRunResolution = {
412
+ ok: false,
413
+ reason: 'client-half-failed',
414
+ pluginRunId,
415
+ startedHere,
416
+ ...failure,
417
+ }
418
+ if (plan.requestId !== undefined) await this.answer(plan.requestId, resolution)
419
+ else await this.settleDirect(plan, resolution)
420
+ }
421
+
422
+ private async settleDirect(plan: RunPlan, resolution: DynamicCordisRunResolution): Promise<void> {
423
+ try {
424
+ const response = await this.env.host.settleUserRun(plan.agentId, plan.pluginId, resolution)
425
+ if (!response.ok) this.fail(plan, 'client-half-failed', response)
426
+ } catch (error) {
427
+ this.fail(plan, 'client-half-failed', errorDetails(error))
428
+ }
429
+ }
430
+
431
+ private async answer(requestId: ApprovalRequestId, resolution: DynamicCordisRunResolution): Promise<void> {
432
+ try {
433
+ await this.env.host.resolveRequestRun(requestId, resolution)
434
+ } catch (error) {
435
+ console.error(`[cordis-client-runner] answering run request ${requestId} failed:`, error)
436
+ }
437
+ }
438
+
439
+ private fail(
440
+ plan: Pick<RunPlan, 'pluginId' | 'packageId'>,
441
+ reason: CordisRunFailure['reason'],
442
+ failure: CordisErrorDetails,
443
+ ): void {
444
+ this.failures.set(plan.pluginId, { packageId: plan.packageId, reason, ...failure })
445
+ this.commit()
446
+ }
447
+ }
448
+
449
+ function sameRequest(left: CordisRunRequest | undefined, right: CordisRunRequest): boolean {
450
+ return left?.requestId === right.requestId
451
+ && left.agentId === right.agentId
452
+ && left.pluginId === right.pluginId
453
+ && left.packageId === right.packageId
454
+ && left.mode === right.mode
455
+ && left.name === right.name
456
+ && left.purpose === right.purpose
457
+ && left.requiresApproval === right.requiresApproval
458
+ }