From 6f493acba6faa63c6b660bf11f21aba23c64e62b Mon Sep 17 00:00:00 2001 From: QQ Date: Sat, 14 Nov 2026 14:56:36 +0800 Subject: [PATCH] Import packages/plugin/src/owner-peer.ts --- packages/plugin/src/owner-peer.ts | 73 +++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 packages/plugin/src/owner-peer.ts diff --git a/packages/plugin/src/owner-peer.ts b/packages/plugin/src/owner-peer.ts new file mode 100644 index 0000000..09f979e --- /dev/null +++ b/packages/plugin/src/owner-peer.ts @@ -0,0 +1,73 @@ +/** + * Owner Peer resolution for the Remote Host carrier. + * + * Harness 0.2.2 dereferences the calling Peer for every local Gateway stream + * (`peer.signal`, `peer.principal`, `peer.interactions`) and rejects forwarded + * event opens without a live Peer. The plugin therefore always attaches one to + * its local carrier, preferring the official `Peer` export and falling back to + * the Connection Host service when a build's module shape no longer exposes it. + */ + +/** Structural owner Peer used by the switch and per-connection bridges. */ +export interface OwnerPeerLike { + dispose?(): unknown +} + +/** Official Connection Host face that can mint in-process owner Peers. */ +export interface OwnerPeerServiceLike { + createPeer?(options: { principal: { kind: 'owner' }; interactions?: 'answer' | 'none' }): OwnerPeerLike +} + +/** One official `Peer` constructor, or a compatible predecessor export. */ +export type OwnerPeerConstructor = new (ctx: unknown, options: { + principal: { kind: 'owner' } + interactions?: 'answer' | 'none' +}) => OwnerPeerLike + +/** + * Select the official `Peer` export of the installed Connection package. + * @param connectionExports - module namespace of `@deepseek-ai/dsh-client-connection`. + * @returns the constructor, or undefined when this build exposes neither name. + */ +export function ownerPeerConstructor( + connectionExports: Record, +): OwnerPeerConstructor | undefined { + const candidate = typeof connectionExports.Peer === 'function' + ? connectionExports.Peer + : connectionExports.OperatorPeer + return typeof candidate === 'function' ? candidate as OwnerPeerConstructor : undefined +} + +/** + * Create a factory that mints owner Peers for the calling plugin. + * + * A factory is returned even when neither mechanism is available; it then + * resolves `undefined` and the local carrier reports an actionable peerless + * failure instead of a `TypeError` from the official Gateway. + * @param ctx - plugin Context that owns Peer-backed effects. + * @param constructor - official Peer constructor when exported by this build. + * @param connection - official Connection Host service, when composed. + * @returns the owner Peer factory. + */ +export function createOwnerPeerFactory( + ctx: unknown, + constructor: OwnerPeerConstructor | undefined, + connection: OwnerPeerServiceLike | undefined, +): () => OwnerPeerLike | undefined { + return () => { + if (constructor !== undefined) { + try { + return new constructor(ctx, { principal: { kind: 'owner' }, interactions: 'answer' }) + } catch { + // Fall through to the Connection Host service below. + } + } + const createPeer = connection?.createPeer + if (typeof createPeer !== 'function') return undefined + try { + return createPeer.call(connection, { principal: { kind: 'owner' }, interactions: 'answer' }) + } catch { + return undefined + } + } +}