@deepseek-ai/dsh-api-remotes 0.2.2-rc.1 → 0.2.2-rc.3

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/src/index.ts ADDED
@@ -0,0 +1,171 @@
1
+ /** Host BFF entry and Loader shell for the Remote contribution assembly. */
2
+
3
+ import { homedir } from 'node:os'
4
+ import type { Context } from '@deepseek-ai/cordis'
5
+ import type { Agent } from '@deepseek-ai/dsh-agent'
6
+ import type {
7
+ TypertRemoteEventDispatch,
8
+ TypertRemoteEventInvocation,
9
+ TypertRemoteEventOutcome,
10
+ TypertRemoteEventSource,
11
+ } from '@deepseek-ai/dsh-api-gateway'
12
+ import { Deque } from '@deepseek-ai/dsh-deque'
13
+ import { carrierKeyOf } from '@deepseek-ai/dsh-scope'
14
+ import { isJsonValue, type JsonValue } from '@deepseek-ai/dsh-util-values'
15
+ import { API_REMOTE_FORWARDED_EVENTS } from './remote-events.ts'
16
+
17
+ // The owner packages' client-safe `./types` exports carry the cordis `Events`
18
+ // declarations for every allowlisted event. Pulling them into this face is what
19
+ // makes the shape assertion below judge real signatures rather than an empty
20
+ // event vocabulary.
21
+ import type {} from '@deepseek-ai/dsh-commands/types'
22
+ import type {} from '@deepseek-ai/dsh-cordis-host-runner/types'
23
+ import type {} from '@deepseek-ai/dsh-credentials/types'
24
+ import type {} from '@deepseek-ai/dsh-goal/types'
25
+ import type {} from '@deepseek-ai/dsh-llm/types'
26
+ import type {} from '@deepseek-ai/dsh-agent-preset-registry/types'
27
+ import type {} from '@deepseek-ai/dsh-permission-presets/types'
28
+ import type {} from '@deepseek-ai/dsh-settings/types'
29
+ import type {} from '@deepseek-ai/dsh-user-approval'
30
+ import type {} from '@deepseek-ai/dsh-user-questions'
31
+ import type {} from '@deepseek-ai/dsh-web/types'
32
+ export type {} from '@deepseek-ai/dsh-api-session-controller/types'
33
+
34
+ export { API_REMOTE_FORWARDED_EVENTS } from './remote-events.ts'
35
+ export type { ApiRemoteForwardedEvent } from './types.ts'
36
+
37
+ /** Required Host service: the Gateway owns the physical Remote stream mux. */
38
+ export const inject = ['typertGateway']
39
+
40
+ /** Host plugin body registering this application's selected Cordis event source. */
41
+ export function apply(ctx: Context): void {
42
+ ctx.effect(
43
+ () => ctx.typertGateway.registerRemoteEvents(remoteEventSource(ctx), { home: homedir() }),
44
+ 'api-remotes: forwarded Cordis event source',
45
+ )
46
+ }
47
+
48
+ /** Create the sole queue and listener set consumed by the registered Gateway. */
49
+ function remoteEventSource(ctx: Context): TypertRemoteEventSource {
50
+ return (signal) => {
51
+ const queue = new RemoteEventQueue()
52
+ const disposers = API_REMOTE_FORWARDED_EVENTS.map((entry) => {
53
+ const { event, mode } = entry
54
+ if (mode === 'emit') {
55
+ const resources = 'resources' in entry ? entry.resources : undefined
56
+ return ctx.on(event as never, ((...args: unknown[]) => {
57
+ queue.push({ event, args: assertJsonArgs(event, args), ...resources === undefined ? {} : { resources } })
58
+ }) as never)
59
+ }
60
+ return ctx.on(event as never, (function (
61
+ this: unknown,
62
+ request: object,
63
+ next: () => unknown,
64
+ ) {
65
+ const carrierAgent = carrierKeyOf(this)
66
+ if (carrierAgent === undefined) return next()
67
+ const agent = (request as { readonly agent?: Agent }).agent
68
+ if (agent === undefined || agent !== carrierAgent) {
69
+ throw new TypeError(`forwarded scoped event ${JSON.stringify(event)} must carry its Agent directly`)
70
+ }
71
+ return forwardWaterfall(
72
+ queue,
73
+ event,
74
+ request,
75
+ { value: agent.ctx, subject: agent, agentId: agent.id },
76
+ next,
77
+ )
78
+ }) as never)
79
+ })
80
+ return queue.iterate(signal, () => {
81
+ for (const dispose of disposers) dispose()
82
+ })
83
+ }
84
+ }
85
+
86
+ /** One pull-driven queue bridging synchronous Cordis listeners to an AsyncIterable. */
87
+ class RemoteEventQueue {
88
+ private readonly buffer = new Deque<TypertRemoteEventDispatch>()
89
+ private waiter: (() => void) | undefined
90
+ private done = false
91
+
92
+ push(frame: TypertRemoteEventDispatch): boolean {
93
+ if (this.done) return false
94
+ this.buffer.pushBack(frame)
95
+ this.waiter?.()
96
+ return true
97
+ }
98
+
99
+ private end(reason: unknown): void {
100
+ if (this.done) return
101
+ this.done = true
102
+ while (this.buffer.size > 0) {
103
+ const dispatch = this.buffer.popFront() as TypertRemoteEventDispatch
104
+ if ('context' in dispatch) dispatch.reject(reason)
105
+ }
106
+ this.waiter?.()
107
+ }
108
+
109
+ async *iterate(signal: AbortSignal, cleanup: () => void): AsyncGenerator<TypertRemoteEventDispatch> {
110
+ const abort = (): void => { this.end(remoteEventSourceEndReason(signal)) }
111
+ signal.addEventListener('abort', abort, { once: true })
112
+ try {
113
+ while (true) {
114
+ if (this.done || signal.aborted) return
115
+ while (this.buffer.size > 0) yield this.buffer.popFront() as TypertRemoteEventDispatch
116
+ await new Promise<void>((resolve) => { this.waiter = resolve })
117
+ this.waiter = undefined
118
+ }
119
+ } finally {
120
+ signal.removeEventListener('abort', abort)
121
+ this.end(remoteEventSourceEndReason(signal))
122
+ cleanup()
123
+ }
124
+ }
125
+ }
126
+
127
+ /**
128
+ * Normalize an event-source shutdown for pending Host waterfalls.
129
+ * @param signal - source lifetime whose reason wins after cancellation.
130
+ * @returns the cancellation reason or an unexpected-end failure.
131
+ */
132
+ function remoteEventSourceEndReason(signal: AbortSignal): unknown {
133
+ if (signal.aborted) return signal.reason
134
+ return new Error('api-remotes: forwarded Remote event source ended')
135
+ }
136
+
137
+ /** Bridge one Cordis waterfall listener through the Gateway-owned pending event. */
138
+ function forwardWaterfall(
139
+ queue: RemoteEventQueue,
140
+ event: string,
141
+ request: object,
142
+ context: TypertRemoteEventInvocation['context'],
143
+ next: () => unknown,
144
+ ): Promise<unknown> {
145
+ const settled = Promise.withResolvers<unknown>()
146
+ const dispatch: TypertRemoteEventInvocation = {
147
+ event,
148
+ request,
149
+ context,
150
+ resolve: (outcome: TypertRemoteEventOutcome) => {
151
+ if (outcome.kind === 'result') {
152
+ settled.resolve(outcome.value)
153
+ return
154
+ }
155
+ void Promise.resolve().then(next).then(settled.resolve, settled.reject)
156
+ },
157
+ reject: settled.reject,
158
+ }
159
+ if (!queue.push(dispatch)) void Promise.resolve().then(next).then(settled.resolve, settled.reject)
160
+ return settled.promise
161
+ }
162
+
163
+ /** Reject an allowlisted event whose runtime arguments are not lossless JSON data. */
164
+ function assertJsonArgs(event: string, args: readonly unknown[]): JsonValue[] {
165
+ for (const [index, arg] of args.entries()) {
166
+ if (!isJsonValue(arg)) {
167
+ throw new Error(`forwarded host event "${event}" argument ${String(index)} is not lossless JSON data`)
168
+ }
169
+ }
170
+ return args as JsonValue[]
171
+ }
@@ -0,0 +1,59 @@
1
+ /**
2
+ * The one home of this application's forwarded-Host-event allowlist. Both
3
+ * compiler faces list this file, so the Host forwarding loop and the consumer
4
+ * `ctx.remote.$on` key face read one declaration instead of two copies that
5
+ * could drift; `./types.ts` derives the type projection from it and stays
6
+ * type-only.
7
+ */
8
+
9
+ import type {} from '@deepseek-ai/dsh-api-session-controller/remote-events'
10
+ import type { SessionId } from '@deepseek-ai/dsh-session/types'
11
+ import type {} from '@deepseek-ai/dsh-deepseek-account/types'
12
+ import type {} from '@deepseek-ai/dsh-permission-presets/types'
13
+ import type {} from '@deepseek-ai/dsh-plugin-manager/types'
14
+ import type {} from '@deepseek-ai/dsh-plugin-market/types'
15
+ import type {} from '@deepseek-ai/dsh-schedule/client'
16
+ import type { TypertForwardableEventEntry } from '@deepseek-ai/dsh-typert-protocol'
17
+ import type {} from '@deepseek-ai/dsh-web/types'
18
+
19
+ /**
20
+ * Host events this application forwards without renaming. The explicit mode is
21
+ * both the Host dispatch strategy and the legal key set of `ctx.remote.$on`.
22
+ * An `emit` entry declares readers of the resources its payload carries, or
23
+ * none to touch the built-in `global` resource; it reaches a non-owner Peer
24
+ * only when access control allows it. Each notification names one resource
25
+ * identity or none, and collection changes travel on follow streams instead.
26
+ */
27
+ export const API_REMOTE_FORWARDED_EVENTS = [
28
+ { event: 'agent-preset/selected', mode: 'emit' },
29
+ { event: 'approval/request', mode: 'waterfall' },
30
+ { event: 'api-session/activity', mode: 'emit', resources: { session: (sessionId): SessionId => sessionId } },
31
+ { event: 'api-session/added', mode: 'emit', resources: { session: (summary): SessionId => summary.sessionId } },
32
+ { event: 'api-session/error', mode: 'emit', resources: { session: (sessionId): SessionId => sessionId } },
33
+ { event: 'api-session/removed', mode: 'emit', resources: { session: (sessionId): SessionId => sessionId } },
34
+ { event: 'api-session/status', mode: 'emit', resources: { session: (sessionId): SessionId => sessionId } },
35
+ { event: 'commands/change', mode: 'emit' },
36
+ { event: 'deepseek-account/session-expired', mode: 'emit' },
37
+ { event: 'deepseek-account/model-sign-in-required', mode: 'emit' },
38
+ { event: 'credentials/record-updated', mode: 'emit' },
39
+ { event: 'credentials/reference-updated', mode: 'emit' },
40
+ { event: 'goal/activation-changed', mode: 'emit' },
41
+ { event: 'cordis/request-run', mode: 'emit' },
42
+ { event: 'cordis/request-run-resolved', mode: 'emit' },
43
+ { event: 'cordis/dynamic-package', mode: 'emit' },
44
+ { event: 'cordis/dynamic-retract', mode: 'emit' },
45
+ { event: 'cordis/inspect-query', mode: 'emit' },
46
+ { event: 'cordis/inspect-query-resolved', mode: 'emit' },
47
+ { event: 'llm/adapters-updated', mode: 'emit' },
48
+ { event: 'llm/models-updated', mode: 'emit' },
49
+ { event: 'permission-presets/catalog-changed', mode: 'emit' },
50
+ { event: 'plugin-manager/changed', mode: 'emit' },
51
+ { event: 'plugin-manager/install-log', mode: 'emit' },
52
+ { event: 'plugin-manager/install-state', mode: 'emit' },
53
+ { event: 'plugin-manager/update-state', mode: 'emit' },
54
+ { event: 'plugin-market/changed', mode: 'emit' },
55
+ { event: 'settings/document-updated', mode: 'emit' },
56
+ { event: 'schedule/changed', mode: 'emit' },
57
+ { event: 'user-questions/request', mode: 'waterfall' },
58
+ { event: 'web/providers-changed', mode: 'emit' },
59
+ ] as const satisfies readonly TypertForwardableEventEntry[]
package/src/types.ts ADDED
@@ -0,0 +1,19 @@
1
+ /**
2
+ * Type face of the forwarded-Host-event allowlist: the consumer key projection
3
+ * and the selection seat it fills. The allowlist VALUE lives in
4
+ * `./remote-events.ts`, keeping this module type-only per the package
5
+ * convention; both compiler faces list both files, so the Host forwarding loop
6
+ * and the consumer `ctx.remote.$on` key face read one declaration instead of
7
+ * two copies that could drift.
8
+ *
9
+ * @module @deepseek-ai/dsh-api-remotes/types
10
+ */
11
+
12
+ import type { API_REMOTE_FORWARDED_EVENTS } from './remote-events.ts'
13
+
14
+ /** Type projection of the allowlist; the consumer and the Host read this one. */
15
+ export type ApiRemoteForwardedEvent = typeof API_REMOTE_FORWARDED_EVENTS[number]['event']
16
+
17
+ declare module '@deepseek-ai/dsh-typert-protocol' {
18
+ interface TypertRemoteEventSelection extends Record<ApiRemoteForwardedEvent, true> {}
19
+ }