@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.
- package/lib/client.js +2411 -7959
- package/lib/client.js.map +1 -0
- package/lib/types/client/api-catalog.d.ts.map +1 -0
- package/lib/types/client/api-catalog.js.map +1 -0
- package/lib/types/client/evaluator.d.ts.map +1 -0
- package/lib/types/client/evaluator.js.map +1 -0
- package/lib/types/client/guard.d.ts.map +1 -0
- package/lib/types/client/guard.js.map +1 -0
- package/lib/types/client/index.d.ts.map +1 -0
- package/lib/types/client/index.js.map +1 -0
- package/lib/types/client/inspect-registry.d.ts.map +1 -0
- package/lib/types/client/inspect-registry.js.map +1 -0
- package/lib/types/client/orchestrator.d.ts.map +1 -0
- package/lib/types/client/orchestrator.js.map +1 -0
- package/lib/types/client/providers.d.ts.map +1 -0
- package/lib/types/client/providers.js.map +1 -0
- package/lib/types/client/runtime.d.ts.map +1 -0
- package/lib/types/client/runtime.js.map +1 -0
- package/lib/types/client/slot-catalog.d.ts +6 -2
- package/lib/types/client/slot-catalog.d.ts.map +1 -0
- package/lib/types/client/slot-catalog.js.map +1 -0
- package/lib/types/index.d.ts.map +1 -0
- package/lib/types/index.js.map +1 -0
- package/package.json +14 -11
- package/src/client/api-catalog.ts +1208 -0
- package/src/client/evaluator.ts +222 -0
- package/src/client/guard.ts +245 -0
- package/src/client/index.ts +308 -0
- package/src/client/inspect-registry.ts +150 -0
- package/src/client/orchestrator.ts +458 -0
- package/src/client/providers.ts +263 -0
- package/src/client/runtime.ts +512 -0
- package/src/client/slot-catalog.ts +5782 -0
- package/src/index.ts +9 -0
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dynamic-package runner, browser half: the load engine that turns one browser
|
|
3
|
+
* half's source into a live cordis plugin (closure → guard → module table →
|
|
4
|
+
* loader entry, ./runtime.ts), plus the retract announcement that unloads it.
|
|
5
|
+
*
|
|
6
|
+
* Nothing loads on activation: this page holds no dynamic package until a
|
|
7
|
+
* dispatch arrives, and a dispatch only follows a model `cordis_run` or a user
|
|
8
|
+
* pressing a card's start control. A refresh therefore starts clean by design —
|
|
9
|
+
* host process memory still holds the definition, the page simply does not run
|
|
10
|
+
* it until asked again.
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
14
|
+
import { TimerService } from '@deepseek-ai/cordis-plugin-timer'
|
|
15
|
+
import type {
|
|
16
|
+
ApprovalRequestId, CordisDynamicPluginId, DynamicCordisInvokeResult,
|
|
17
|
+
DynamicCordisInventoryRow,
|
|
18
|
+
} from '@deepseek-ai/dsh-api-remotes/client'
|
|
19
|
+
import type { ClientModuleSystem } from '@deepseek-ai/dsh-client-modules/client'
|
|
20
|
+
import type { SlotRegistry } from '@deepseek-ai/dsh-client-ui-renderer/client'
|
|
21
|
+
import type { JsonValue } from '@deepseek-ai/dsh-util-values'
|
|
22
|
+
// The Client Remote assembly is the one place the two planes meet: it mounts the
|
|
23
|
+
// `dynamicCordisRunner` namespace and re-exports its payload vocabulary, so this
|
|
24
|
+
// package names what it sends without importing a Host package.
|
|
25
|
+
import type { DynamicCordisLivePackage } from './runtime.ts'
|
|
26
|
+
import { DynamicCordisPackageRunner } from './runtime.ts'
|
|
27
|
+
import { CordisRunOrchestrator } from './orchestrator.ts'
|
|
28
|
+
import { ClientCordisInspectRegistry, provideClientCordisInspect } from './inspect-registry.ts'
|
|
29
|
+
import { clientInspectProviders } from './providers.ts'
|
|
30
|
+
import type { CordisRunActivity, CordisRunFailure, CordisUserRunRequest } from './orchestrator.ts'
|
|
31
|
+
import type { CordisObservable, DynamicCordisRenderFailure } from './runtime.ts'
|
|
32
|
+
|
|
33
|
+
export { CordisRunOrchestrator } from './orchestrator.ts'
|
|
34
|
+
export { ClientCordisInspectRegistry } from './inspect-registry.ts'
|
|
35
|
+
export type {
|
|
36
|
+
ClientCordisInspectHost, ClientCordisInspectProviderRegistration, ClientCordisInspectQueryContext,
|
|
37
|
+
} from './inspect-registry.ts'
|
|
38
|
+
export type {
|
|
39
|
+
CordisRunActivity, CordisRunFailure, CordisRunHostSeam,
|
|
40
|
+
CordisRunOrchestratorEnv, CordisRunRequest, CordisUserRunRequest,
|
|
41
|
+
} from './orchestrator.ts'
|
|
42
|
+
export { DynamicCordisPackageRunner } from './runtime.ts'
|
|
43
|
+
export type {
|
|
44
|
+
CordisObservable, DynamicCordisClientHalf, DynamicCordisLivePackage, DynamicCordisLoadErrorCause,
|
|
45
|
+
DynamicCordisLoadResult, DynamicCordisRenderFailure, DynamicCordisRunnerEnv,
|
|
46
|
+
} from './runtime.ts'
|
|
47
|
+
|
|
48
|
+
export { DynamicCordisStyles, evaluateClientHalf, isDynamicCordisPlugin } from './evaluator.ts'
|
|
49
|
+
export type { DynamicCordisClosureEnv, DynamicCordisEvaluatedPlugin } from './evaluator.ts'
|
|
50
|
+
export { dynamicCordisContext } from './guard.ts'
|
|
51
|
+
export type { DynamicCordisGuardEnv, DynamicCordisSlotLedgerRow } from './guard.ts'
|
|
52
|
+
// Re-exported so consumers of the service face and the two events can name
|
|
53
|
+
// their subjects without reaching into the wire contract themselves.
|
|
54
|
+
export type {
|
|
55
|
+
ApprovalRequestId, CordisDynamicPackageId, CordisDynamicPluginId, CordisDynamicPluginRunId,
|
|
56
|
+
DynamicCordisPackage,
|
|
57
|
+
} from '@deepseek-ai/dsh-api-remotes/client'
|
|
58
|
+
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* What a run surface reads and calls. The activity map is the single home of
|
|
62
|
+
* "a run is in flight", so an affordance never keeps its own copy — that is what
|
|
63
|
+
* makes it survive a remount.
|
|
64
|
+
*/
|
|
65
|
+
export interface CordisRunnerFace {
|
|
66
|
+
/** Each definition's in-flight run activity. */
|
|
67
|
+
readonly activeRuns: CordisObservable<ReadonlyMap<CordisDynamicPluginId, CordisRunActivity>>
|
|
68
|
+
/** The last failure of this page's own run attempt, per definition. */
|
|
69
|
+
readonly lastRunError: CordisObservable<ReadonlyMap<CordisDynamicPluginId, CordisRunFailure>>
|
|
70
|
+
/**
|
|
71
|
+
* This page's last render crash per definition: a browser half that loaded
|
|
72
|
+
* cleanly and then broke while React rendered it. Page-local and current by
|
|
73
|
+
* construction — cleared when the package stops, is retracted, or loads again —
|
|
74
|
+
* which is what makes it safe for a row to render directly. The host keeps its
|
|
75
|
+
* own last-across-pages copy for the model; the two have different owners and
|
|
76
|
+
* lifetimes and neither is derived from the other.
|
|
77
|
+
*/
|
|
78
|
+
readonly renderFailures: CordisObservable<ReadonlyMap<CordisDynamicPluginId, DynamicCordisRenderFailure>>
|
|
79
|
+
/**
|
|
80
|
+
* Restore pending approvals after a page reconnect or missed event.
|
|
81
|
+
* @param rows - current dynamic Plugin inventory.
|
|
82
|
+
*/
|
|
83
|
+
reconcileApprovals(rows: readonly DynamicCordisInventoryRow[]): void
|
|
84
|
+
/**
|
|
85
|
+
* Answer one run request with "run it" and drive both halves.
|
|
86
|
+
* @param requestId - the request being answered; unknown or settled ids are a no-op.
|
|
87
|
+
* @param approveFutureVersions - whether this decision covers later Packages of the same Plugin.
|
|
88
|
+
* @returns after the orchestration settled.
|
|
89
|
+
*/
|
|
90
|
+
approve(requestId: ApprovalRequestId, approveFutureVersions: boolean): Promise<void>
|
|
91
|
+
/**
|
|
92
|
+
* Answer one run request with "do not run it".
|
|
93
|
+
* @param requestId - the request being answered; unknown or settled ids are a no-op.
|
|
94
|
+
* @returns after the refusal reached the host.
|
|
95
|
+
*/
|
|
96
|
+
decline(requestId: ApprovalRequestId): Promise<void>
|
|
97
|
+
/**
|
|
98
|
+
* Run a definition here at the user's own request (the gesture authorizes it).
|
|
99
|
+
* A definition with a browser half also loads onto this page; a host-only one
|
|
100
|
+
* only comes up in the host process.
|
|
101
|
+
* @param request - the definition to run, its session, and whether it has a browser half.
|
|
102
|
+
* @returns after the orchestration settled.
|
|
103
|
+
*/
|
|
104
|
+
startUserRun(request: CordisUserRunRequest): Promise<void>
|
|
105
|
+
/**
|
|
106
|
+
* Observe what this page has loaded.
|
|
107
|
+
* @param fn - notified after every converged load or unload.
|
|
108
|
+
* @returns unsubscribe.
|
|
109
|
+
*/
|
|
110
|
+
subscribe(fn: () => void): () => void
|
|
111
|
+
/**
|
|
112
|
+
* Read what this page currently has loaded.
|
|
113
|
+
* @returns immutable rows for live Client halves.
|
|
114
|
+
*/
|
|
115
|
+
getSnapshot(): readonly DynamicCordisLivePackage[]
|
|
116
|
+
/**
|
|
117
|
+
* Whether this page loaded a definition's browser half — page-local truth,
|
|
118
|
+
* never the host's "it is running".
|
|
119
|
+
* @param pluginId - stable Plugin identity.
|
|
120
|
+
* @returns true while a load is live here.
|
|
121
|
+
*/
|
|
122
|
+
isLoaded(pluginId: CordisDynamicPluginId): boolean
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
declare module '@deepseek-ai/cordis' {
|
|
126
|
+
interface Context {
|
|
127
|
+
/** Run orchestration and page-local load state: what run surfaces read and call. */
|
|
128
|
+
dynamicCordisRunner: CordisRunnerFace
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/** Teaching text for a routing failure the infrastructure itself reports. */
|
|
133
|
+
function invokeFailure(pluginId: CordisDynamicPluginId, method: string, result: Extract<DynamicCordisInvokeResult, { ok: false }>): string {
|
|
134
|
+
const where = `host.call("${method}") on ${pluginId}`
|
|
135
|
+
if (result.code === 'plugin-not-running') {
|
|
136
|
+
return `${where} found no active Host half — the Plugin is stopped or was removed.`
|
|
137
|
+
}
|
|
138
|
+
if (result.code === 'stale-run') {
|
|
139
|
+
return `${where} belongs to an activation that has already been replaced.`
|
|
140
|
+
}
|
|
141
|
+
if (result.code === 'method-not-found') {
|
|
142
|
+
return `${where} is not registered: the host half must declare it with harness.handle("${method}", fn).`
|
|
143
|
+
}
|
|
144
|
+
return `${where} failed inside the host handler: ${result.message}`
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/** Preserve a Host handler's stack while adding the Client call site diagnosis. */
|
|
148
|
+
function invokeError(
|
|
149
|
+
pluginId: CordisDynamicPluginId,
|
|
150
|
+
method: string,
|
|
151
|
+
result: Extract<DynamicCordisInvokeResult, { ok: false }>,
|
|
152
|
+
): Error {
|
|
153
|
+
const error = new Error(invokeFailure(pluginId, method, result))
|
|
154
|
+
if (result.stack !== undefined) error.stack = `${error.stack ?? error.message}\nHost stack:\n${result.stack}`
|
|
155
|
+
return error
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
/**
|
|
159
|
+
* Teaching text for a `host.call` the wire itself refused: the generated codec
|
|
160
|
+
* rejected the argument before sending, or the result on the way back, or the
|
|
161
|
+
* transport broke. The infrastructure's message names the field it refused but
|
|
162
|
+
* not the call it belonged to, and the model authored both halves — so this adds
|
|
163
|
+
* the call and the contract it has to satisfy.
|
|
164
|
+
*/
|
|
165
|
+
function wireFailure(id: CordisDynamicPluginId, method: string, error: unknown): string {
|
|
166
|
+
const message = error instanceof Error ? error.message : String(error)
|
|
167
|
+
return `host.call("${method}") on ${id} did not complete: ${message}\n`
|
|
168
|
+
+ 'Both directions carry JSON only: pass plain JSON data as the argument — or omit it, and the handler receives '
|
|
169
|
+
+ `null — and answer from harness.handle("${method}", fn) with JSON (\`return null\` when there is nothing to report).`
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
/** Stable Cordis plugin name. */
|
|
173
|
+
export const name = 'cordis-client-runner'
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Required services: the loader/module chain for entries, the slot registry for
|
|
177
|
+
* contributions, and the `dynamicCordisRunner` Remote namespace. Declaring the
|
|
178
|
+
* namespace parks this plugin until the host side exists, so a page never loads
|
|
179
|
+
* a browser half whose host half it could not reach.
|
|
180
|
+
*/
|
|
181
|
+
export const inject = ['loader', 'modules', 'slots', 'remote', 'remote.dynamicCordisRunner']
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* Client plugin body: build the runner and subscribe the dispatch family.
|
|
185
|
+
* @param ctx - client root context.
|
|
186
|
+
*/
|
|
187
|
+
export function apply(ctx: Context): void {
|
|
188
|
+
new TimerService(ctx)
|
|
189
|
+
const inspect = new ClientCordisInspectRegistry({
|
|
190
|
+
sync: async (providers) => {
|
|
191
|
+
const answered = await ctx.remote.dynamicCordisRunner.syncInspectManifest(providers)
|
|
192
|
+
if (!answered.ok) throw new Error(`${answered.error.code}: ${answered.error.message}`)
|
|
193
|
+
},
|
|
194
|
+
resolve: async (agentId, requestId, resolution) => {
|
|
195
|
+
const answered = await ctx.remote.dynamicCordisRunner.resolveInspectQuery(agentId, requestId, resolution)
|
|
196
|
+
if (!answered.ok) throw new Error(`${answered.error.code}: ${answered.error.message}`)
|
|
197
|
+
},
|
|
198
|
+
})
|
|
199
|
+
provideClientCordisInspect(ctx, inspect)
|
|
200
|
+
for (const provider of clientInspectProviders(ctx)) {
|
|
201
|
+
ctx.effect(() => inspect.register(provider), `cordis-client-runner: inspect ${provider.manifest.id}`)
|
|
202
|
+
}
|
|
203
|
+
ctx.on('connection/reset', () => { inspect.publish() })
|
|
204
|
+
|
|
205
|
+
const runner = new DynamicCordisPackageRunner({
|
|
206
|
+
ctx,
|
|
207
|
+
loader: ctx.loader,
|
|
208
|
+
modules: ctx.get('modules') as ClientModuleSystem,
|
|
209
|
+
slots: ctx.get('slots') as SlotRegistry,
|
|
210
|
+
invoke: async (pluginId, pluginRunId, method, args) => {
|
|
211
|
+
// Model-authored arguments reach this boundary untyped; the namespace's
|
|
212
|
+
// generated codec is what validates them as JSON, and its rejection is a
|
|
213
|
+
// bare field name — this is the only place that still knows which call it
|
|
214
|
+
// belonged to, so the teaching has to be added here.
|
|
215
|
+
const answered = await ctx.remote.dynamicCordisRunner.invoke(pluginId, pluginRunId, method, args as JsonValue)
|
|
216
|
+
.catch((error: unknown) => { throw new Error(wireFailure(pluginId, method, error)) })
|
|
217
|
+
// Two failure layers, and they teach different things: the carrier's error
|
|
218
|
+
// branch means the call never reached the host half, while the namespace's
|
|
219
|
+
// own `ok: false` is that half answering with a refusal.
|
|
220
|
+
if (!answered.ok) throw new Error(wireFailure(pluginId, method, `${answered.error.code}: ${answered.error.message}`))
|
|
221
|
+
const result = answered.value
|
|
222
|
+
if (result.ok) return result.value
|
|
223
|
+
throw invokeError(pluginId, method, result)
|
|
224
|
+
},
|
|
225
|
+
// Post-settle diagnosis, deliberately fire-and-forget: the run this package
|
|
226
|
+
// belongs to was answered before it ever rendered, so nothing waits on this
|
|
227
|
+
// and a failed report must not turn one crash into two.
|
|
228
|
+
reportRenderFailure: (agentId, pluginId, pluginRunId, failure) => {
|
|
229
|
+
void ctx.remote.dynamicCordisRunner.reportRenderFailure(agentId, pluginId, pluginRunId, failure).then((result) => {
|
|
230
|
+
if (!result.ok) {
|
|
231
|
+
console.error(`[cordis-client-runner] reporting a render failure of ${pluginId} failed:`, result.error)
|
|
232
|
+
}
|
|
233
|
+
}, (error: unknown) => {
|
|
234
|
+
console.error(`[cordis-client-runner] reporting a render failure of ${pluginId} failed:`, error)
|
|
235
|
+
})
|
|
236
|
+
},
|
|
237
|
+
reportGuardFailure: (agentId, pluginId, pluginRunId, failure) => {
|
|
238
|
+
void ctx.remote.dynamicCordisRunner.reportClientGuardFailure(agentId, pluginId, pluginRunId, failure).then((result) => {
|
|
239
|
+
if (!result.ok) {
|
|
240
|
+
console.error(`[cordis-client-runner] reporting a guard failure of ${pluginId} failed:`, result.error)
|
|
241
|
+
}
|
|
242
|
+
}, (error: unknown) => {
|
|
243
|
+
console.error(`[cordis-client-runner] reporting a guard failure of ${pluginId} failed:`, error)
|
|
244
|
+
})
|
|
245
|
+
},
|
|
246
|
+
})
|
|
247
|
+
const orchestrator = new CordisRunOrchestrator({
|
|
248
|
+
runner,
|
|
249
|
+
host: {
|
|
250
|
+
// The seam names business payloads only, so a carrier failure is folded
|
|
251
|
+
// here into whatever each verb already does with one: the short-circuit
|
|
252
|
+
// message for a start, a throw where the caller has a catch of its own.
|
|
253
|
+
runHostHalf: async (agentId, pluginId, packageId, mode, requestId, approveFutureVersions) => {
|
|
254
|
+
const answered = await ctx.remote.dynamicCordisRunner.runHostHalf(
|
|
255
|
+
agentId, pluginId, packageId, mode, requestId, approveFutureVersions,
|
|
256
|
+
)
|
|
257
|
+
return answered.ok ? answered.value : { ok: false, message: `${answered.error.code}: ${answered.error.message}` }
|
|
258
|
+
},
|
|
259
|
+
getClientCode: async (agentId, pluginId, pluginRunId) => {
|
|
260
|
+
const answered = await ctx.remote.dynamicCordisRunner.getClientCode(agentId, pluginId, pluginRunId)
|
|
261
|
+
if (!answered.ok) throw new Error(`${answered.error.code}: ${answered.error.message}`)
|
|
262
|
+
return answered.value
|
|
263
|
+
},
|
|
264
|
+
resolveRequestRun: async (requestId, resolution) => {
|
|
265
|
+
const answered = await ctx.remote.dynamicCordisRunner.resolveRequestRun(requestId, resolution)
|
|
266
|
+
// Thrown rather than returned: `answer` logs and drops a failed answer,
|
|
267
|
+
// and the host settles the request on its own either way.
|
|
268
|
+
if (!answered.ok) throw new Error(`${answered.error.code}: ${answered.error.message}`)
|
|
269
|
+
return answered.value
|
|
270
|
+
},
|
|
271
|
+
settleUserRun: async (agentId, pluginId, resolution) => {
|
|
272
|
+
const answered = await ctx.remote.dynamicCordisRunner.settleUserRun(agentId, pluginId, resolution)
|
|
273
|
+
if (!answered.ok) throw new Error(`${answered.error.code}: ${answered.error.message}`)
|
|
274
|
+
return answered.value
|
|
275
|
+
},
|
|
276
|
+
},
|
|
277
|
+
})
|
|
278
|
+
const face: CordisRunnerFace = {
|
|
279
|
+
activeRuns: orchestrator.activeRuns,
|
|
280
|
+
lastRunError: orchestrator.lastRunError,
|
|
281
|
+
renderFailures: runner.renderFailures,
|
|
282
|
+
reconcileApprovals: (rows) => { orchestrator.reconcileApprovals(rows) },
|
|
283
|
+
approve: (requestId, approveFutureVersions) => orchestrator.approve(requestId, approveFutureVersions),
|
|
284
|
+
decline: requestId => orchestrator.decline(requestId),
|
|
285
|
+
startUserRun: request => orchestrator.startUserRun(request),
|
|
286
|
+
subscribe: fn => runner.subscribe(fn),
|
|
287
|
+
getSnapshot: () => runner.getSnapshot(),
|
|
288
|
+
isLoaded: id => runner.isLoaded(id),
|
|
289
|
+
}
|
|
290
|
+
ctx.provide('dynamicCordisRunner', face)
|
|
291
|
+
ctx.effect(() => () => { void runner.dispose() }, 'cordis-client-runner: dynamic package runner')
|
|
292
|
+
|
|
293
|
+
// Forwarded Host events: `$on` hands the listener the Host's own argument list,
|
|
294
|
+
// so these read the request itself rather than a transport envelope.
|
|
295
|
+
ctx.remote.$on('cordis/request-run', (request) => {
|
|
296
|
+
orchestrator.open(request)
|
|
297
|
+
})
|
|
298
|
+
ctx.remote.$on('cordis/request-run-resolved', (resolved) => { orchestrator.close(resolved.requestId) })
|
|
299
|
+
ctx.remote.$on('cordis/dynamic-retract', (retracted) => {
|
|
300
|
+
runner.retract(retracted.pluginId, retracted.pluginRunId)
|
|
301
|
+
})
|
|
302
|
+
ctx.remote.$on('cordis/inspect-query', (request) => {
|
|
303
|
+
void inspect.query(request).catch((error: unknown) => {
|
|
304
|
+
console.error(`[cordis-client-runner] inspect query ${request.provider}.${request.method} failed:`, error)
|
|
305
|
+
})
|
|
306
|
+
})
|
|
307
|
+
ctx.remote.$on('cordis/inspect-query-resolved', (resolved) => { inspect.close(resolved.requestId) })
|
|
308
|
+
}
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
/** Browser registry for read-only Cordis capability providers. */
|
|
2
|
+
|
|
3
|
+
import type { Context } from '@deepseek-ai/cordis'
|
|
4
|
+
import type {
|
|
5
|
+
CordisInspectProviderManifest, CordisInspectQueryRequest, CordisInspectQueryResolution,
|
|
6
|
+
CordisInspectRequestId, SessionId,
|
|
7
|
+
} from '@deepseek-ai/dsh-api-remotes/client'
|
|
8
|
+
import type { JsonValue } from '@deepseek-ai/dsh-util-values'
|
|
9
|
+
|
|
10
|
+
/** Context supplied to a Client inspect provider query. */
|
|
11
|
+
export interface ClientCordisInspectQueryContext {
|
|
12
|
+
/** Cancellation broadcast by the Host. */
|
|
13
|
+
signal: AbortSignal
|
|
14
|
+
/** Session whose model requested the query. */
|
|
15
|
+
sessionId: SessionId
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
/** Client provider registration retained beside its serializable manifest. */
|
|
19
|
+
export interface ClientCordisInspectProviderRegistration {
|
|
20
|
+
/** Provider and explicit query directory. */
|
|
21
|
+
manifest: CordisInspectProviderManifest
|
|
22
|
+
/** Execute one declared read-only method. */
|
|
23
|
+
query(method: string, input: JsonValue | undefined, context: ClientCordisInspectQueryContext): Promise<JsonValue>
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
/** Remote operations needed by the Client registry. */
|
|
27
|
+
export interface ClientCordisInspectHost {
|
|
28
|
+
/** Replace the Host's mirrored Client manifest. */
|
|
29
|
+
sync(providers: readonly CordisInspectProviderManifest[]): Promise<void>
|
|
30
|
+
/** Submit one query result; the first accepted page wins. */
|
|
31
|
+
resolve(
|
|
32
|
+
sessionId: SessionId,
|
|
33
|
+
requestId: CordisInspectRequestId,
|
|
34
|
+
resolution: CordisInspectQueryResolution,
|
|
35
|
+
): Promise<void>
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/** Client provider registry, manifest publisher, and live query dispatcher. */
|
|
39
|
+
export class ClientCordisInspectRegistry {
|
|
40
|
+
private readonly providers = new Map<string, ClientCordisInspectProviderRegistration>()
|
|
41
|
+
private readonly active = new Map<CordisInspectRequestId, AbortController>()
|
|
42
|
+
private publishQueued = false
|
|
43
|
+
private syncChain = Promise.resolve()
|
|
44
|
+
|
|
45
|
+
/** @param host - folded manifest and query result transport. */
|
|
46
|
+
constructor(private readonly host: ClientCordisInspectHost) {}
|
|
47
|
+
|
|
48
|
+
/**
|
|
49
|
+
* Register one Client provider and publish a new complete manifest.
|
|
50
|
+
* @param registration - provider manifest and local handler.
|
|
51
|
+
* @returns idempotent disposer.
|
|
52
|
+
*/
|
|
53
|
+
register(registration: ClientCordisInspectProviderRegistration): () => void {
|
|
54
|
+
const { manifest } = registration
|
|
55
|
+
if (manifest.id.trim() === '') throw new Error('Client Cordis inspect provider id must not be empty')
|
|
56
|
+
if (this.providers.has(manifest.id)) throw new Error(`Client Cordis inspect provider "${manifest.id}" is already registered`)
|
|
57
|
+
const names = new Set<string>()
|
|
58
|
+
for (const method of manifest.methods) {
|
|
59
|
+
if (names.has(method.name)) throw new Error(`Client Cordis inspect provider "${manifest.id}" repeats method "${method.name}"`)
|
|
60
|
+
names.add(method.name)
|
|
61
|
+
}
|
|
62
|
+
this.providers.set(manifest.id, registration)
|
|
63
|
+
this.publish()
|
|
64
|
+
let disposed = false
|
|
65
|
+
return () => {
|
|
66
|
+
if (disposed) return
|
|
67
|
+
disposed = true
|
|
68
|
+
if (this.providers.get(manifest.id) === registration) {
|
|
69
|
+
this.providers.delete(manifest.id)
|
|
70
|
+
this.publish()
|
|
71
|
+
}
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** Publish the current complete manifest, including after reconnect. */
|
|
76
|
+
publish(): void {
|
|
77
|
+
if (this.publishQueued) return
|
|
78
|
+
this.publishQueued = true
|
|
79
|
+
queueMicrotask(() => {
|
|
80
|
+
this.publishQueued = false
|
|
81
|
+
const manifests = [...this.providers.values()].map(provider => provider.manifest)
|
|
82
|
+
this.syncChain = this.syncChain.then(async () => {
|
|
83
|
+
await this.host.sync(manifests)
|
|
84
|
+
}).catch((error: unknown) => {
|
|
85
|
+
console.error('[cordis-client-runner] syncing inspect providers failed:', error)
|
|
86
|
+
})
|
|
87
|
+
})
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Execute and answer one Host-broadcast query.
|
|
92
|
+
* @param request - exact provider query and Session correlation received from Host.
|
|
93
|
+
* @returns after the first local result has been sent back to Host.
|
|
94
|
+
*/
|
|
95
|
+
async query(request: CordisInspectQueryRequest): Promise<void> {
|
|
96
|
+
if (this.active.has(request.requestId)) return
|
|
97
|
+
const controller = new AbortController()
|
|
98
|
+
this.active.set(request.requestId, controller)
|
|
99
|
+
let resolution: CordisInspectQueryResolution
|
|
100
|
+
try {
|
|
101
|
+
const provider = this.providers.get(request.provider)
|
|
102
|
+
if (provider === undefined) {
|
|
103
|
+
resolution = { ok: false, reason: 'provider-missing', message: `Client inspect provider "${request.provider}" is unavailable` }
|
|
104
|
+
} else if (!provider.manifest.methods.some(method => method.name === request.method)) {
|
|
105
|
+
resolution = { ok: false, reason: 'method-missing', message: `Client inspect provider "${request.provider}" has no method "${request.method}"` }
|
|
106
|
+
} else {
|
|
107
|
+
const data = await provider.query(request.method, request.input, {
|
|
108
|
+
signal: controller.signal,
|
|
109
|
+
sessionId: request.agentId,
|
|
110
|
+
})
|
|
111
|
+
resolution = controller.signal.aborted
|
|
112
|
+
? { ok: false, reason: 'cancelled', message: 'Client inspect query was cancelled' }
|
|
113
|
+
: { ok: true, data }
|
|
114
|
+
}
|
|
115
|
+
} catch (error) {
|
|
116
|
+
resolution = controller.signal.aborted
|
|
117
|
+
? { ok: false, reason: 'cancelled', message: 'Client inspect query was cancelled' }
|
|
118
|
+
: { ok: false, reason: 'provider-error', message: error instanceof Error ? error.message : String(error) }
|
|
119
|
+
} finally {
|
|
120
|
+
this.active.delete(request.requestId)
|
|
121
|
+
}
|
|
122
|
+
if (controller.signal.aborted) return
|
|
123
|
+
await this.host.resolve(request.agentId, request.requestId, resolution)
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
/**
|
|
127
|
+
* Cancel local work after another page answered or the Tool call ended.
|
|
128
|
+
* @param requestId - query correlation that is no longer answerable.
|
|
129
|
+
*/
|
|
130
|
+
close(requestId: CordisInspectRequestId): void {
|
|
131
|
+
this.active.get(requestId)?.abort()
|
|
132
|
+
this.active.delete(requestId)
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
declare module '@deepseek-ai/cordis' {
|
|
137
|
+
interface Context {
|
|
138
|
+
/** Browser registry for pre-definition Cordis capability discovery. */
|
|
139
|
+
cordisInspect: ClientCordisInspectRegistry
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Provide the registry as a normal Client service.
|
|
145
|
+
* @param ctx - Client Cordis context receiving the service.
|
|
146
|
+
* @param registry - page-local inspect registry to publish.
|
|
147
|
+
*/
|
|
148
|
+
export function provideClientCordisInspect(ctx: Context, registry: ClientCordisInspectRegistry): void {
|
|
149
|
+
ctx.provide('cordisInspect', registry)
|
|
150
|
+
}
|