@xopcai/extension-ui-sdk 0.0.524 → 0.0.526
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/package.json +1 -1
- package/src/transport.test.ts +100 -0
- package/src/transport.ts +27 -3
package/package.json
CHANGED
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
// @vitest-environment jsdom
|
|
2
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
|
3
|
+
|
|
4
|
+
import { Transport } from './transport.js';
|
|
5
|
+
|
|
6
|
+
describe('Transport timeouts and native bridge events', () => {
|
|
7
|
+
const posted: string[] = [];
|
|
8
|
+
|
|
9
|
+
beforeEach(() => {
|
|
10
|
+
vi.useFakeTimers();
|
|
11
|
+
posted.length = 0;
|
|
12
|
+
(window as unknown as { __XOPC_NATIVE_BRIDGE__?: { postMessage: (p: string) => void } }).__XOPC_NATIVE_BRIDGE__ = {
|
|
13
|
+
postMessage(payload: string) {
|
|
14
|
+
posted.push(payload);
|
|
15
|
+
},
|
|
16
|
+
};
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
afterEach(() => {
|
|
20
|
+
delete (window as unknown as { __XOPC_NATIVE_BRIDGE__?: unknown }).__XOPC_NATIVE_BRIDGE__;
|
|
21
|
+
vi.useRealTimers();
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
function dispatchInit(extensionId = 'demo-space') {
|
|
25
|
+
window.dispatchEvent(new CustomEvent('xopc-native-host-message', {
|
|
26
|
+
detail: {
|
|
27
|
+
source: 'xopc-host',
|
|
28
|
+
type: 'init',
|
|
29
|
+
extensionId,
|
|
30
|
+
permissions: ['theme', 'storage'],
|
|
31
|
+
locale: 'zh',
|
|
32
|
+
theme: { mode: 'light', tokens: {} },
|
|
33
|
+
},
|
|
34
|
+
}));
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
it('uses interactionTimeout for interactive picker, approval, and share methods', async () => {
|
|
38
|
+
const transport = new Transport();
|
|
39
|
+
dispatchInit();
|
|
40
|
+
await transport.ready;
|
|
41
|
+
|
|
42
|
+
const pickPromise = transport.request('media.pickImage', { source: 'library' });
|
|
43
|
+
const approvalPromise = transport.request('automation.reviewProposal', { proposalId: 'p1' });
|
|
44
|
+
const permPromise = transport.request('ui.reviewPermissions');
|
|
45
|
+
const sharePromise = transport.request('ui.share', { text: 'hello' });
|
|
46
|
+
const normalPromise = transport.request('records.list', { collection: 'logs' });
|
|
47
|
+
|
|
48
|
+
// Attach catch handlers before advancing timers so Vitest never sees an unhandled rejection.
|
|
49
|
+
const normalResult = normalPromise.catch((err: Error) => err.message);
|
|
50
|
+
|
|
51
|
+
// At 10s, normal request times out, while interactive requests remain pending.
|
|
52
|
+
await vi.advanceTimersByTimeAsync(10_000);
|
|
53
|
+
expect(await normalResult).toContain("Request 'records.list' timed out after 10000ms");
|
|
54
|
+
|
|
55
|
+
// Resolve interactive requests well after the 10s default timeout (e.g. 60s).
|
|
56
|
+
await vi.advanceTimersByTimeAsync(50_000);
|
|
57
|
+
const reqs = posted
|
|
58
|
+
.map((raw) => JSON.parse(raw) as { type: string; requestId?: string; method?: string })
|
|
59
|
+
.filter((msg) => msg.type === 'request');
|
|
60
|
+
for (const req of reqs) {
|
|
61
|
+
if (req.method === 'records.list' || !req.requestId) continue;
|
|
62
|
+
window.dispatchEvent(new CustomEvent('xopc-native-host-message', {
|
|
63
|
+
detail: {
|
|
64
|
+
source: 'xopc-host',
|
|
65
|
+
type: 'response',
|
|
66
|
+
requestId: req.requestId,
|
|
67
|
+
result: { ok: true, method: req.method },
|
|
68
|
+
},
|
|
69
|
+
}));
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
await expect(pickPromise).resolves.toEqual({ ok: true, method: 'media.pickImage' });
|
|
73
|
+
await expect(approvalPromise).resolves.toEqual({ ok: true, method: 'automation.reviewProposal' });
|
|
74
|
+
await expect(permPromise).resolves.toEqual({ ok: true, method: 'ui.reviewPermissions' });
|
|
75
|
+
await expect(sharePromise).resolves.toEqual({ ok: true, method: 'ui.share' });
|
|
76
|
+
|
|
77
|
+
transport.dispose();
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it('forwards permissions.changed host events to subscribers', async () => {
|
|
81
|
+
const transport = new Transport();
|
|
82
|
+
dispatchInit();
|
|
83
|
+
await transport.ready;
|
|
84
|
+
|
|
85
|
+
const handler = vi.fn();
|
|
86
|
+
transport.on('permissions.changed', handler);
|
|
87
|
+
|
|
88
|
+
window.dispatchEvent(new CustomEvent('xopc-native-host-message', {
|
|
89
|
+
detail: {
|
|
90
|
+
source: 'xopc-host',
|
|
91
|
+
type: 'event',
|
|
92
|
+
event: 'permissions.changed',
|
|
93
|
+
data: { permissions: ['theme', 'storage', 'records.read'] },
|
|
94
|
+
},
|
|
95
|
+
}));
|
|
96
|
+
|
|
97
|
+
expect(handler).toHaveBeenCalledWith({ permissions: ['theme', 'storage', 'records.read'] });
|
|
98
|
+
transport.dispose();
|
|
99
|
+
});
|
|
100
|
+
});
|
package/src/transport.ts
CHANGED
|
@@ -35,6 +35,15 @@ type PendingRequest = {
|
|
|
35
35
|
timer: ReturnType<typeof setTimeout>;
|
|
36
36
|
};
|
|
37
37
|
|
|
38
|
+
const INTERACTION_METHODS = new Set([
|
|
39
|
+
'files.pickAttachment',
|
|
40
|
+
'media.pickImage',
|
|
41
|
+
'automation.reviewProposal',
|
|
42
|
+
'apps.reviewRequest',
|
|
43
|
+
'ui.reviewPermissions',
|
|
44
|
+
'ui.share',
|
|
45
|
+
]);
|
|
46
|
+
|
|
38
47
|
export type TransportOptions = {
|
|
39
48
|
timeout?: number;
|
|
40
49
|
};
|
|
@@ -100,9 +109,7 @@ export class Transport {
|
|
|
100
109
|
throw new Error('Transport disposed');
|
|
101
110
|
}
|
|
102
111
|
const requestId = this.nextRequestId();
|
|
103
|
-
const timeout = method
|
|
104
|
-
: method === 'capability.call' && params && typeof params === 'object' && 'id' in params
|
|
105
|
-
&& typeof params.id === 'string' && (params.id === 'xopc.agent.delegate' || params.id.startsWith('xopc.connectors.')) ? this.delegationTimeout : this.timeout;
|
|
112
|
+
const timeout = this.resolveRequestTimeout(method, params);
|
|
106
113
|
const message: ExtensionRequest = {
|
|
107
114
|
source: 'xopc-extension',
|
|
108
115
|
extensionId: this.extensionId,
|
|
@@ -152,6 +159,23 @@ export class Transport {
|
|
|
152
159
|
};
|
|
153
160
|
}
|
|
154
161
|
|
|
162
|
+
private resolveRequestTimeout(method: string, params?: unknown): number {
|
|
163
|
+
if (INTERACTION_METHODS.has(method)) {
|
|
164
|
+
return this.interactionTimeout;
|
|
165
|
+
}
|
|
166
|
+
if (
|
|
167
|
+
method === 'capability.call'
|
|
168
|
+
&& params
|
|
169
|
+
&& typeof params === 'object'
|
|
170
|
+
&& 'id' in params
|
|
171
|
+
&& typeof params.id === 'string'
|
|
172
|
+
&& (params.id === 'xopc.agent.delegate' || params.id.startsWith('xopc.connectors.'))
|
|
173
|
+
) {
|
|
174
|
+
return this.delegationTimeout;
|
|
175
|
+
}
|
|
176
|
+
return this.timeout;
|
|
177
|
+
}
|
|
178
|
+
|
|
155
179
|
private nextRequestId(): string {
|
|
156
180
|
this.requestCounter += 1;
|
|
157
181
|
return `req_${Date.now()}_${this.requestCounter}`;
|