@xenosystem/agent-interface-electron-host 0.1.32 → 0.1.33

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.
@@ -0,0 +1,30 @@
1
+ import type { AgentHostClientIdentity, AgentHostEvent, AgentHostTransport } from '@xenosystem/agent-interface-contract';
2
+ export interface DesktopAttachedSession {
3
+ role: string;
4
+ descriptor: {
5
+ instanceId: string;
6
+ endpoint: string;
7
+ };
8
+ transport: AgentHostTransport;
9
+ close(): Promise<void>;
10
+ }
11
+ /** Each document owns a client socket, never the shared host authority. Socket
12
+ * loss then reaches the existing canonical server releaseClient path exactly. */
13
+ export declare class DesktopDocumentConnections {
14
+ private readonly options;
15
+ private readonly entries;
16
+ private closing;
17
+ constructor(options: {
18
+ attach: () => Promise<DesktopAttachedSession>;
19
+ expectedAuthority: () => {
20
+ instanceId: string;
21
+ endpoint: string;
22
+ };
23
+ onEvent: (identity: AgentHostClientIdentity, event: AgentHostEvent) => void;
24
+ });
25
+ request(identity: AgentHostClientIdentity, dispatch: (transport: AgentHostTransport) => Promise<unknown>): Promise<unknown>;
26
+ retire(identity: Pick<AgentHostClientIdentity, 'instanceId'>): Promise<void>;
27
+ close(): Promise<void>;
28
+ private create;
29
+ }
30
+ //# sourceMappingURL=desktopDocumentConnections.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"desktopDocumentConnections.d.ts","sourceRoot":"","sources":["../src/desktopDocumentConnections.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,uBAAuB,EAAE,cAAc,EAAE,kBAAkB,EAAE,MAAM,sCAAsC,CAAA;AAEvH,MAAM,WAAW,sBAAsB;IACrC,IAAI,EAAE,MAAM,CAAA;IACZ,UAAU,EAAE;QAAE,UAAU,EAAE,MAAM,CAAC;QAAC,QAAQ,EAAE,MAAM,CAAA;KAAE,CAAA;IACpD,SAAS,EAAE,kBAAkB,CAAA;IAC7B,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAA;CACvB;AAGD;iFACiF;AACjF,qBAAa,0BAA0B;IAGzB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAFpC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA2B;IACnD,OAAO,CAAC,OAAO,CAAQ;gBACM,OAAO,EAAE;QACpC,MAAM,EAAE,MAAM,OAAO,CAAC,sBAAsB,CAAC,CAAA;QAC7C,iBAAiB,EAAE,MAAM;YAAE,UAAU,EAAE,MAAM,CAAC;YAAC,QAAQ,EAAE,MAAM,CAAA;SAAE,CAAA;QACjE,OAAO,EAAE,CAAC,QAAQ,EAAE,uBAAuB,EAAE,KAAK,EAAE,cAAc,KAAK,IAAI,CAAA;KAC5E;IAEK,OAAO,CAAC,QAAQ,EAAE,uBAAuB,EAAE,QAAQ,EAAE,CAAC,SAAS,EAAE,kBAAkB,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;IAYjI,MAAM,CAAC,QAAQ,EAAE,IAAI,CAAC,uBAAuB,EAAE,YAAY,CAAC,GAAG,OAAO,CAAC,IAAI,CAAC;IAgBtE,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAK5B,OAAO,CAAC,MAAM;CAyBf"}
@@ -0,0 +1,81 @@
1
+ /** Each document owns a client socket, never the shared host authority. Socket
2
+ * loss then reaches the existing canonical server releaseClient path exactly. */
3
+ export class DesktopDocumentConnections {
4
+ options;
5
+ entries = new Map();
6
+ closing = false;
7
+ constructor(options) {
8
+ this.options = options;
9
+ }
10
+ async request(identity, dispatch) {
11
+ if (this.closing)
12
+ throw new Error('Desktop document connections are closing.');
13
+ const entry = this.entries.get(identity.instanceId) || this.create(identity);
14
+ if (entry.retired)
15
+ throw new Error('Desktop document connection was retired.');
16
+ const session = await entry.ready;
17
+ if (entry.retired || this.closing) {
18
+ await this.retire(identity);
19
+ throw new Error('Desktop document connection was retired while attaching.');
20
+ }
21
+ return dispatch(session.transport);
22
+ }
23
+ retire(identity) {
24
+ const entry = this.entries.get(identity.instanceId);
25
+ if (!entry)
26
+ return Promise.resolve();
27
+ entry.retired = true;
28
+ entry.unsubscribe?.();
29
+ delete entry.unsubscribe;
30
+ if (entry.retirement)
31
+ return entry.retirement;
32
+ const retirement = (async () => {
33
+ try {
34
+ await entry.ready;
35
+ }
36
+ catch { /* An attached session, if any, is retained below. */ }
37
+ if (entry.session)
38
+ await entry.session.close();
39
+ if (this.entries.get(identity.instanceId) === entry)
40
+ this.entries.delete(identity.instanceId);
41
+ })();
42
+ entry.retirement = retirement;
43
+ void retirement.finally(() => { if (entry.retirement === retirement)
44
+ delete entry.retirement; }).catch(() => { });
45
+ return retirement;
46
+ }
47
+ async close() {
48
+ this.closing = true;
49
+ await Promise.all([...this.entries.keys()].map(instanceId => this.retire({ instanceId })));
50
+ }
51
+ create(identity) {
52
+ const expected = this.options.expectedAuthority();
53
+ const entry = { retired: false, ready: Promise.resolve(undefined) };
54
+ this.entries.set(identity.instanceId, entry);
55
+ entry.ready = Promise.resolve().then(async () => {
56
+ if (entry.retired)
57
+ throw new Error('Desktop document retired before attachment.');
58
+ const session = await this.options.attach();
59
+ // Never close an owner session from this client-only pool, even if an
60
+ // erroneous injected factory hands one back.
61
+ if (session.role !== 'attached-client')
62
+ throw new Error('Desktop documents may only attach to an existing authority.');
63
+ entry.session = session;
64
+ if (session.descriptor.instanceId !== expected.instanceId || session.descriptor.endpoint !== expected.endpoint) {
65
+ entry.retired = true;
66
+ await session.close();
67
+ delete entry.session;
68
+ throw new Error('Desktop shared authority changed during document attachment.');
69
+ }
70
+ if (!entry.retired)
71
+ entry.unsubscribe = session.transport.subscribe(event => {
72
+ if (!entry.retired && this.entries.get(identity.instanceId) === entry)
73
+ this.options.onEvent(identity, event);
74
+ });
75
+ return session;
76
+ });
77
+ void entry.ready.catch(() => { });
78
+ return entry;
79
+ }
80
+ }
81
+ //# sourceMappingURL=desktopDocumentConnections.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"desktopDocumentCon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A;QACF,KAAK,KAAK,CAAC,KAAK,CAAC,KAAK,CAAC,GAAG,EAAE,GAAE,CAAC,CAAC,CAAA;QAChC,OAAO,KAAK,CAAA;IACd,CAAC;CACF"}
@@ -0,0 +1,45 @@
1
+ import { type AgentHostClientIdentity } from '@xenosystem/agent-interface-contract';
2
+ export declare const DESKTOP_BOOTSTRAP_CHANNEL = "xeno-agent-host:bootstrap";
3
+ /** The two other channel names a product must agree on with its preload. */
4
+ export declare const AGENT_HOST_REQUEST_CHANNEL = "xeno-agent-host:request";
5
+ export declare const AGENT_HOST_EVENT_CHANNEL = "xeno-agent-host:event";
6
+ export interface DesktopDocumentReceipt {
7
+ identity: AgentHostClientIdentity;
8
+ token: string;
9
+ }
10
+ export declare function sameDesktopDocumentUrl(actual: string, expected: string): boolean;
11
+ /**
12
+ * Which product this authority mints identities for.
13
+ *
14
+ * Parameters rather than constants because this class is the ONLY safe way for an Electron
15
+ * product to mint a client identity, and every product needs its own. Hard-coding
16
+ * `xeno-agent-desktop`/`standalone-desktop` here is what kept it app-private, and the
17
+ * alternative — each product writing its own copy — is a second implementation of the check
18
+ * that decides whether a renderer may speak to the agent host at all.
19
+ */
20
+ export interface DocumentAuthorityProduct {
21
+ /** Stable product id, e.g. `xeno-agent-desktop`, `xeno-canvas`. */
22
+ clientId: string;
23
+ /** The surface the host will see. The embed layer refuses a client whose surface disagrees
24
+ * with its mount, so this is not cosmetic. */
25
+ surface: AgentHostClientIdentity['surface'];
26
+ }
27
+ /** Main-process authority. Renderer claims never create a client identity. */
28
+ export declare class DesktopSenderAuthority {
29
+ private readonly version;
30
+ private readonly uuid;
31
+ private readonly onRetire;
32
+ private readonly senders;
33
+ private readonly product;
34
+ constructor(version: string, uuid?: () => string, onRetire?: (identity: AgentHostClientIdentity) => void, product?: DocumentAuthorityProduct);
35
+ register(sender: object, allowedUrl: string): void;
36
+ remove(sender: object): void;
37
+ invalidate(sender: object): void;
38
+ committed(sender: object, url: string, sameDocument?: boolean): void;
39
+ bootstrap(sender: object, isMainFrame: boolean, url: string): DesktopDocumentReceipt;
40
+ assert(sender: object, isMainFrame: boolean, url: string, token: unknown, handshake?: boolean): AgentHostClientIdentity;
41
+ canReceive(sender: object, url: string): boolean;
42
+ request(sender: object, isMainFrame: boolean, url: string, token: unknown, method: string, input: unknown, dispatch: (input: unknown) => Promise<unknown>): Promise<unknown>;
43
+ private document;
44
+ }
45
+ //# sourceMappingURL=desktopSenderAuthority.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"desktopSenderAuthority.d.ts","sourceRoot":"","sources":["../src/desktopSenderAuthority.ts"],"names":[],"mappings":"AACA,OAAO,EAAmD,KAAK,uBAAuB,EAAE,MAAM,sCAAsC,CAAA;AAEpI,eAAO,MAAM,yBAAyB,8BAA8B,CAAA;AACpE,4EAA4E;AAC5E,eAAO,MAAM,0BAA0B,4BAA4B,CAAA;AACnE,eAAO,MAAM,wBAAwB,0BAA0B,CAAA;AAC/D,MAAM,WAAW,sBAAsB;IAAG,QAAQ,EAAE,uBAAuB,CAAC;IAAC,KAAK,EAAE,MAAM,CAAA;CAAE;AAK5F,wBAAgB,sBAAsB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,OAAO,CAMhF;AAED;;;;;;;;GAQG;AACH,MAAM,WAAW,wBAAwB;IACvC,mEAAmE;IACnE,QAAQ,EAAE,MAAM,CAAA;IAChB;mDAC+C;IAC/C,OAAO,EAAE,uBAAuB,CAAC,SAAS,CAAC,CAAA;CAC5C;AAED,8EAA8E;AAC9E,qBAAa,sBAAsB;IAGrB,OAAO,CAAC,QAAQ,CAAC,OAAO;IAAU,OAAO,CAAC,QAAQ,CAAC,IAAI;IACjE,OAAO,CAAC,QAAQ,CAAC,QAAQ;IAH3B,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAkC;IAC1D,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA0B;gBACrB,OAAO,EAAE,MAAM,EAAmB,IAAI,GAAE,MAAM,MAAmB,EAC3E,QAAQ,GAAE,CAAC,QAAQ,EAAE,uBAAuB,KAAK,IAAe,EACjF,OAAO,GAAE,wBAA4F;IAIvG,QAAQ,CAAC,MAAM,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,GAAG,IAAI;IAIlD,MAAM,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAC5B,UAAU,CAAC,MAAM,EAAE,MAAM,GAAG,IAAI;IAOhC,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,EAAE,YAAY,UAAQ,GAAG,IAAI;IAgBlE,SAAS,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,sBAAsB;IAKpF,MAAM,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,SAAS,UAAQ,GAAG,uBAAuB;IAMrH,UAAU,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO;IAI1C,OAAO,CAAC,MAAM,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,MAAM,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,QAAQ,EAAE,CAAC,KAAK,EAAE,OAAO,KAAK,OAAO,CAAC,OAAO,CAAC,GAAG,OAAO,CAAC,OAAO,CAAC;IAgClL,OAAO,CAAC,QAAQ;CAKjB"}
@@ -0,0 +1,133 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import { isAgentHostClientIdentity, isAgentHostHandshake } from '@xenosystem/agent-interface-contract';
3
+ export const DESKTOP_BOOTSTRAP_CHANNEL = 'xeno-agent-host:bootstrap';
4
+ /** The two other channel names a product must agree on with its preload. */
5
+ export const AGENT_HOST_REQUEST_CHANNEL = 'xeno-agent-host:request';
6
+ export const AGENT_HOST_EVENT_CHANNEL = 'xeno-agent-host:event';
7
+ const record = (value) => Boolean(value) && typeof value === 'object' && !Array.isArray(value);
8
+ export function sameDesktopDocumentUrl(actual, expected) {
9
+ try {
10
+ const left = new URL(actual), right = new URL(expected);
11
+ left.hash = '';
12
+ right.hash = '';
13
+ return !left.username && !left.password && left.href === right.href;
14
+ }
15
+ catch {
16
+ return false;
17
+ }
18
+ }
19
+ /** Main-process authority. Renderer claims never create a client identity. */
20
+ export class DesktopSenderAuthority {
21
+ version;
22
+ uuid;
23
+ onRetire;
24
+ senders = new Map();
25
+ product;
26
+ constructor(version, uuid = randomUUID, onRetire = () => { }, product = { clientId: 'xeno-agent-desktop', surface: 'standalone-desktop' }) {
27
+ this.version = version;
28
+ this.uuid = uuid;
29
+ this.onRetire = onRetire;
30
+ this.product = product;
31
+ }
32
+ register(sender, allowedUrl) {
33
+ if (this.senders.has(sender))
34
+ throw new Error('Desktop sender is already registered.');
35
+ this.senders.set(sender, { allowedUrl });
36
+ }
37
+ remove(sender) { this.invalidate(sender); this.senders.delete(sender); }
38
+ invalidate(sender) {
39
+ const registration = this.senders.get(sender);
40
+ const prior = registration?.document;
41
+ if (registration)
42
+ delete registration.document;
43
+ if (prior)
44
+ this.onRetire({ ...prior.identity });
45
+ }
46
+ committed(sender, url, sameDocument = false) {
47
+ const registration = this.senders.get(sender);
48
+ if (!registration)
49
+ return;
50
+ if (sameDocument)
51
+ return;
52
+ const prior = registration.document;
53
+ // A foreign committed document must not retain the old page's authority.
54
+ this.invalidate(sender);
55
+ if (!sameDesktopDocumentUrl(url, registration.allowedUrl))
56
+ return;
57
+ const instanceId = this.uuid(), token = this.uuid();
58
+ if (!instanceId || !token || instanceId === token || instanceId === prior?.identity.instanceId || token === prior?.token
59
+ || [...this.senders.values()].some(item => item.document?.identity.instanceId === instanceId || item.document?.token === token)) {
60
+ throw new Error('Duplicate desktop document identity refused.');
61
+ }
62
+ registration.document = { identity: { clientId: this.product.clientId, clientVersion: this.version, instanceId, surface: this.product.surface }, token, acknowledged: false };
63
+ }
64
+ bootstrap(sender, isMainFrame, url) {
65
+ const document = this.document(sender, isMainFrame, url);
66
+ return { identity: { ...document.identity }, token: document.token };
67
+ }
68
+ assert(sender, isMainFrame, url, token, handshake = false) {
69
+ const document = this.document(sender, isMainFrame, url);
70
+ if (token !== document.token || (handshake && !document.acknowledged))
71
+ throw new Error('Desktop document authority refused.');
72
+ return { ...document.identity };
73
+ }
74
+ canReceive(sender, url) {
75
+ try {
76
+ return this.document(sender, true, url).acknowledged;
77
+ }
78
+ catch {
79
+ return false;
80
+ }
81
+ }
82
+ async request(sender, isMainFrame, url, token, method, input, dispatch) {
83
+ const identity = this.assert(sender, isMainFrame, url, token, method !== 'host.handshake');
84
+ const document = this.senders.get(sender).document;
85
+ if (!record(input))
86
+ throw new Error('Desktop request input is invalid.');
87
+ const matches = (value) => isAgentHostClientIdentity(value)
88
+ && value.clientId === identity.clientId && value.clientVersion === identity.clientVersion
89
+ && value.surface === identity.surface && value.instanceId === identity.instanceId
90
+ && Object.keys(value).every(key => ['clientId', 'clientVersion', 'surface', 'instanceId'].includes(key));
91
+ if ((method === 'host.handshake' && !matches(input.identity))
92
+ || ('identity' in input && !matches(input.identity))
93
+ || ('client' in input && !matches(input.client))
94
+ || (record(input.command) && !matches(input.command.client)))
95
+ throw new Error('Desktop request client identity refused.');
96
+ const current = () => this.senders.get(sender)?.document === document;
97
+ if (method === 'host.handshake') {
98
+ const fingerprint = JSON.stringify(input);
99
+ if (document.handshake && document.handshake !== fingerprint)
100
+ throw new Error('Desktop handshake changed within the document.');
101
+ if (document.pending)
102
+ return document.pending;
103
+ document.handshake = fingerprint;
104
+ const pending = dispatch(structuredClone(input)).then(result => {
105
+ if (!current())
106
+ throw new Error('Desktop document changed during handshake.');
107
+ if (!isAgentHostHandshake(result) || !matches(result.surface))
108
+ throw new Error('Desktop host handshake was not acknowledged.');
109
+ document.acknowledged = true;
110
+ return result;
111
+ });
112
+ document.pending = pending;
113
+ try {
114
+ return await pending;
115
+ }
116
+ finally {
117
+ if (current())
118
+ delete document.pending;
119
+ }
120
+ }
121
+ const result = await dispatch(structuredClone(input));
122
+ if (!current())
123
+ throw new Error('Desktop document changed during request.');
124
+ return result;
125
+ }
126
+ document(sender, isMainFrame, url) {
127
+ const registration = this.senders.get(sender);
128
+ if (!isMainFrame || !registration?.document || !sameDesktopDocumentUrl(url, registration.allowedUrl))
129
+ throw new Error('Desktop sender is not an authorized main document.');
130
+ return registration.document;
131
+ }
132
+ }
133
+ //# sourceMappingURL=desktopSenderAuthority.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"desktopSenderAuthority.js","sourceRoot":"","sources":["../src/desktopSenderAuthority.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAA;AACxC,OAAO,EAAE,yBAAyB,EAAE,oBAAoB,EAAgC,MAAM,sCAAsC,CAAA;AAEpI,MAAM,CAAC,MAAM,yBAAyB,GAAG,2BAA2B,CAAA;AACpE,4EAA4E;AAC5E,MAAM,CAAC,MAAM,0BAA0B,GAAG,yBAAyB,CAAA;AACnE,MAAM,CAAC,MAAM,wBAAwB,GAAG,uBAAuB,CAAA;AAI/D,MAAM,MAAM,GAAG,CAAC,KAAc,EAAoC,EAAE,CAAC,OAAO,CAAC,KAAK,CAAC,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,CAAA;AAEzI,MAAM,UAAU,sBAAsB,CAAC,MAAc,EAAE,QAAgB;IACrE,IAAI,CAAC;QACH,MAAM,IAAI,GAAG,IAAI,GAAG,CAAC,MAAM,CAAC,EAAE,KAAK,GAAG,IAAI,GAAG,CAAC,QAAQ,CAAC,CAAA;QACvD,IAAI,CAAC,IAAI,GAAG,EAAE,CAAC;QAAC,KAAK,CAAC,IAAI,GAAG,EAAE,CAAA;QAC/B,OAAO,CAAC,IAAI,CAAC,QAAQ,IAAI,CAAC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W,IAAI,CAAC,YAAY,EAAE,QAAQ,IAAI,CAAC,sBAAsB,CAAC,GAAG,EAAE,YAAY,CAAC,UAAU,CAAC;YAAE,MAAM,IAAI,KAAK,CAAC,oDAAoD,CAAC,CAAA;QAC3K,OAAO,YAAY,CAAC,QAAQ,CAAA;IAC9B,CAAC;CACF"}
package/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ export * from './desktopSenderAuthority.js';
2
+ export * from './desktopDocumentConnections.js';
1
3
  export * from './sdkAgent.js';
2
4
  export * from './headlessAgentSurface.js';
3
5
  export * from './sdkNativeHeadlessRunner.js';
@@ -1 +1 @@
1
- {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,2BAA2B,CAAA;AACzC,cAAc,8BAA8B,CAAA;AAC5C;;;;GAIG;AACH,cAAc,oBAAoB,CAAA;AAClC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,2BAA2B,CAAA"}
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAaA,cAAc,6BAA6B,CAAA;AAC3C,cAAc,iCAAiC,CAAA;AAC/C,cAAc,eAAe,CAAA;AAC7B,cAAc,2BAA2B,CAAA;AACzC,cAAc,8BAA8B,CAAA;AAC5C;;;;GAIG;AACH,cAAc,oBAAoB,CAAA;AAClC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,2BAA2B,CAAA"}
package/dist/index.js CHANGED
@@ -1,3 +1,18 @@
1
+ /*
2
+ * The Electron main-process boundary a product needs to host the Agent panel.
3
+ *
4
+ * 🔴 These two modules lived in `apps/desktop/src/main/` and were therefore UNPUBLISHED, which
5
+ * meant the standalone app was the only product that could mount the interface at all: the
6
+ * panel is installable from npm, and the code that decides whether a renderer may speak to the
7
+ * agent host was not. Any other product had to reimplement per-document identity minting,
8
+ * sender validation and connection pooling — a second copy of a security boundary, which is
9
+ * exactly the failure `XENO AGENT PANEL - SPEC.md` D7d refuses.
10
+ *
11
+ * Moved rather than ported: `apps/desktop` consumes these, so there is one implementation and
12
+ * its existing tests still guard it.
13
+ */
14
+ export * from './desktopSenderAuthority.js';
15
+ export * from './desktopDocumentConnections.js';
1
16
  export * from './sdkAgent.js';
2
17
  export * from './headlessAgentSurface.js';
3
18
  export * from './sdkNativeHeadlessRunner.js';
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,cAAc,eAAe,CAAA;AAC7B,cAAc,2BAA2B,CAAA;AACzC,cAAc,8BAA8B,CAAA;AAC5C;;;;GAIG;AACH,cAAc,oBAAoB,CAAA;AAClC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,2BAA2B,CAAA"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AACH,cAAc,6BAA6B,CAAA;AAC3C,cAAc,iCAAiC,CAAA;AAC/C,cAAc,eAAe,CAAA;AAC7B,cAAc,2BAA2B,CAAA;AACzC,cAAc,8BAA8B,CAAA;AAC5C;;;;GAIG;AACH,cAAc,oBAAoB,CAAA;AAClC,cAAc,sBAAsB,CAAA;AACpC,cAAc,2BAA2B,CAAA;AACzC,cAAc,2BAA2B,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xenosystem/agent-interface-electron-host",
3
- "version": "0.1.32",
3
+ "version": "0.1.33",
4
4
  "license": "UNLICENSED",
5
5
  "type": "module",
6
6
  "main": "./dist/index.js",
@@ -29,7 +29,9 @@
29
29
  "dependencies": {
30
30
  "@xenosystem/agent-sdk": "0.9.27",
31
31
  "@xenosystem/web-context-client": "0.1.1",
32
- "@xenosystem/agent-interface-host": "0.1.32"
32
+ "@xenosystem/agent-interface-client": "0.1.33",
33
+ "@xenosystem/agent-interface-host": "0.1.33",
34
+ "@xenosystem/agent-interface-contract": "0.1.33"
33
35
  },
34
36
  "peerDependencies": {
35
37
  "electron": ">=44"