@nac3/runtime 2.8.0 → 2.9.0
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/CHANGELOG.md +111 -0
- package/README.md +1 -0
- package/SPEC.md +45 -4
- package/contract-head.json +4 -4
- package/dist/cli-levels.json +2 -2
- package/dist/cli-lint.mjs +255 -6
- package/dist/cli.js +3 -3
- package/dist/nac-mcp-interop.browser.js +556 -0
- package/dist/nac-mcp-interop.cjs +561 -0
- package/dist/nac-mcp-interop.d.ts +57 -0
- package/dist/nac-mcp-interop.mjs +561 -0
- package/dist/nac-v2-extensions.d.ts +1 -1
- package/dist/nac.browser.js +33 -5
- package/dist/nac.cjs +33 -5
- package/dist/nac.d.ts +1 -1
- package/dist/nac.mjs +33 -5
- package/package.json +28 -10
|
@@ -0,0 +1,556 @@
|
|
|
1
|
+
// Generated by build.mjs -- do not edit. Source: yujin.app/nac-spec/js/nac-mcp-interop.js
|
|
2
|
+
/**
|
|
3
|
+
* NAC MCP Interop -- host-side runtime for cross-app navigation.
|
|
4
|
+
*
|
|
5
|
+
* Adds FOUR public functions to window.NAC:
|
|
6
|
+
*
|
|
7
|
+
* NAC.export_tree(opts?) -> NACExportV1
|
|
8
|
+
* NAC.import_remote_tree(payload, conn) -> RemoteHandle
|
|
9
|
+
* NAC.list_remote_apps() -> RemoteHandle[]
|
|
10
|
+
* NAC.disconnect_remote(namespace) -> void
|
|
11
|
+
*
|
|
12
|
+
* And patches NAC.click / fill / tab / tab_by_label / etc to
|
|
13
|
+
* detect 'remote:<namespace>:<peer_id>' prefixes and proxy the
|
|
14
|
+
* call via the registered transport (http or stdio).
|
|
15
|
+
*
|
|
16
|
+
* Spec: docs/NAC_INTEROP_MCP.md.
|
|
17
|
+
*
|
|
18
|
+
* STATUS: published since 2.9.0 as the fourth entry of the package
|
|
19
|
+
* (owner decision D-31 of 2026-11-27, closing PND-137). Until that day
|
|
20
|
+
* this header said "v2.3 PREVIEW, lives in branch feat/nac-interop-mcp"
|
|
21
|
+
* while the file sat in main and reached no adopter: the build emitted
|
|
22
|
+
* three bundles and npm install brought none of this.
|
|
23
|
+
*
|
|
24
|
+
* import '@nac3/runtime'; // first: the runtime
|
|
25
|
+
* import '@nac3/runtime/mcp-interop'; // then: this
|
|
26
|
+
*
|
|
27
|
+
* Load order is the same contract js/nac-v2-extensions.js has: with no
|
|
28
|
+
* window.NAC this file warns and installs nothing.
|
|
29
|
+
*
|
|
30
|
+
* WHAT IT IS NOT. Cross-app navigation is OPTIONAL and no conformance
|
|
31
|
+
* level mentions it. An agent reaches a NAC3 application by calling
|
|
32
|
+
* NAC.click / NAC.call on the document, or through the intermediary of
|
|
33
|
+
* SPEC 11.1; what this file adds is a different capability -- driving a
|
|
34
|
+
* SECOND application by name from inside the first.
|
|
35
|
+
*
|
|
36
|
+
* IT NEEDS A DOCUMENT. The proxy reports through DOM events, so three
|
|
37
|
+
* of the four functions refuse by name under the headless profile
|
|
38
|
+
* (SPEC 19) instead of throwing ReferenceError at a property of
|
|
39
|
+
* undefined -- the same rule D-26 wrote for the primitives of the core
|
|
40
|
+
* runtime. list_remote_apps() reads state only and works anywhere.
|
|
41
|
+
*/
|
|
42
|
+
|
|
43
|
+
(function (global) {
|
|
44
|
+
'use strict';
|
|
45
|
+
|
|
46
|
+
if (!global.NAC) {
|
|
47
|
+
console.warn('[nac-interop] NAC runtime not loaded yet; skipping interop install');
|
|
48
|
+
return;
|
|
49
|
+
}
|
|
50
|
+
if (global.NAC.__interop_installed) return;
|
|
51
|
+
global.NAC.__interop_installed = true;
|
|
52
|
+
|
|
53
|
+
var REMOTE_PREFIX = 'remote:';
|
|
54
|
+
var _remotes = Object.create(null); /* namespace -> RemoteHandle */
|
|
55
|
+
var _origClick, _origFill, _origSelect, _origTab, _origTabByLabel,
|
|
56
|
+
_origGoToSection, _origDragDrop, _origEditField,
|
|
57
|
+
_origDtAddRow, _origDtRemoveRow, _origDtEditCell, _origDtCommit,
|
|
58
|
+
_origDtDiscard, _origDtReadAggregate;
|
|
59
|
+
|
|
60
|
+
/* ---------- Helpers ---------- */
|
|
61
|
+
|
|
62
|
+
function _now() { return new Date().toISOString(); }
|
|
63
|
+
|
|
64
|
+
function _err(code, message) {
|
|
65
|
+
var e = new Error(message);
|
|
66
|
+
e.code = code;
|
|
67
|
+
return e;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
/* D-26 made this the rule for the core runtime: a primitive that
|
|
71
|
+
needs a document NAMES that it has none instead of dying on a
|
|
72
|
+
property of undefined. This module became publishable the day it
|
|
73
|
+
entered the package (D-31), and a published entry that throws
|
|
74
|
+
ReferenceError on a server is the same defect wearing a new file
|
|
75
|
+
name. Three of the four public functions report through DOM
|
|
76
|
+
events; list_remote_apps does not, and is not guarded. */
|
|
77
|
+
function _requireDocument(fnName) {
|
|
78
|
+
if (typeof document === 'undefined') {
|
|
79
|
+
throw _err('no_document',
|
|
80
|
+
fnName + ': cross-app navigation reports through DOM events and there is no '
|
|
81
|
+
+ 'document in this process (SPEC 19 headless profile). list_remote_apps() is '
|
|
82
|
+
+ 'the only function here that works without one.');
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
function _parseRemote(nac_id) {
|
|
87
|
+
if (typeof nac_id !== 'string') return null;
|
|
88
|
+
if (nac_id.indexOf(REMOTE_PREFIX) !== 0) return null;
|
|
89
|
+
var rest = nac_id.slice(REMOTE_PREFIX.length);
|
|
90
|
+
var colon = rest.indexOf(':');
|
|
91
|
+
if (colon < 0) return null;
|
|
92
|
+
return {
|
|
93
|
+
namespace: rest.slice(0, colon),
|
|
94
|
+
peer_id: rest.slice(colon + 1)
|
|
95
|
+
};
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
function _emitLocalAck(plugin, action_id, ok, detail) {
|
|
99
|
+
var name = ok ? 'nac:action:succeeded' : 'nac:action:failed';
|
|
100
|
+
document.dispatchEvent(new CustomEvent(name, {
|
|
101
|
+
detail: Object.assign({
|
|
102
|
+
plugin: plugin,
|
|
103
|
+
action_id: action_id,
|
|
104
|
+
is_trusted: false, /* remote actions are NEVER user-trusted */
|
|
105
|
+
via_interop: true
|
|
106
|
+
}, detail || {})
|
|
107
|
+
}));
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/* ---------- export_tree ---------- */
|
|
111
|
+
|
|
112
|
+
function export_tree(opts) {
|
|
113
|
+
_requireDocument('export_tree');
|
|
114
|
+
opts = opts || {};
|
|
115
|
+
var scope = opts.scope || 'full';
|
|
116
|
+
var includeDom = !!opts.include_dom_state;
|
|
117
|
+
var includeLocales = opts.include_locales || null;
|
|
118
|
+
|
|
119
|
+
/* Collect manifests. We prefer NAC.list_registered_plugins
|
|
120
|
+
(v2.3 introspection helper) because it returns EVERY
|
|
121
|
+
registered manifest regardless of DOM mount state -- which
|
|
122
|
+
is the correct behavior for export (a peer can want to
|
|
123
|
+
export a manifest even if the plugin's DOM is currently
|
|
124
|
+
hidden). Fall back to NAC.describe()'s plugin list if the
|
|
125
|
+
helper isn't there (older runtimes). */
|
|
126
|
+
var manifests = {};
|
|
127
|
+
var snap = (typeof global.NAC.describe === 'function') ? global.NAC.describe() : { plugins: [], active: null };
|
|
128
|
+
var pluginSlugs;
|
|
129
|
+
if (typeof global.NAC.list_registered_plugins === 'function') {
|
|
130
|
+
pluginSlugs = global.NAC.list_registered_plugins();
|
|
131
|
+
} else {
|
|
132
|
+
pluginSlugs = (snap.plugins || []).map(function (p) { return p && p.plugin; }).filter(Boolean);
|
|
133
|
+
}
|
|
134
|
+
pluginSlugs.forEach(function (slug) {
|
|
135
|
+
var m = (typeof global.NAC.manifest === 'function') ? global.NAC.manifest(slug) : null;
|
|
136
|
+
if (!m) return;
|
|
137
|
+
if (includeLocales && Array.isArray(includeLocales)) {
|
|
138
|
+
m = _filterLocales(m, includeLocales);
|
|
139
|
+
}
|
|
140
|
+
if (scope === 'active_plugin' && snap.active !== slug) return;
|
|
141
|
+
if (scope.indexOf('plugin_slug:') === 0 && scope.slice(12) !== slug) return;
|
|
142
|
+
manifests[slug] = m;
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
/* Collect v2.0 scope tree if available. */
|
|
146
|
+
var scopeTree = [];
|
|
147
|
+
var dataTables = [];
|
|
148
|
+
if (typeof global.NAC.describe_v2 === 'function') {
|
|
149
|
+
try {
|
|
150
|
+
var v2 = global.NAC.describe_v2();
|
|
151
|
+
scopeTree = v2.v2_scope_entries || [];
|
|
152
|
+
dataTables = v2.data_tables || [];
|
|
153
|
+
} catch (e) { /* tolerated */ }
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/* State block. */
|
|
157
|
+
var state = {
|
|
158
|
+
user_lang: (global.NacChat && global.NacChat._lang) || document.documentElement.lang || 'en'
|
|
159
|
+
};
|
|
160
|
+
if (includeDom) {
|
|
161
|
+
state.url = location.href;
|
|
162
|
+
state.title = document.title;
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
return {
|
|
166
|
+
app_id: global.__YUJIN_APP_ID__ || 'nac-app-' + Math.random().toString(36).slice(2, 10),
|
|
167
|
+
app_version: global.__YUJIN_APP_VERSION__ || '1.0.0',
|
|
168
|
+
nac_version: '2.3',
|
|
169
|
+
exported_at: _now(),
|
|
170
|
+
active_plugin: snap.active || null,
|
|
171
|
+
manifests: manifests,
|
|
172
|
+
scope_tree: scopeTree,
|
|
173
|
+
data_tables: dataTables,
|
|
174
|
+
state: state,
|
|
175
|
+
ack_endpoint: global.__YUJIN_ACK_ENDPOINT__ || null /* host-configurable */
|
|
176
|
+
};
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
function _filterLocales(manifest, locales) {
|
|
180
|
+
/* Deep-clone the manifest with label_i18n restricted to the
|
|
181
|
+
requested locales. Idempotent + side-effect-free. */
|
|
182
|
+
function reduce(obj) {
|
|
183
|
+
if (obj == null || typeof obj !== 'object') return obj;
|
|
184
|
+
if (Array.isArray(obj)) return obj.map(reduce);
|
|
185
|
+
var out = {};
|
|
186
|
+
for (var k in obj) {
|
|
187
|
+
if (!Object.prototype.hasOwnProperty.call(obj, k)) continue;
|
|
188
|
+
if (k === 'label_i18n' && obj[k] && typeof obj[k] === 'object') {
|
|
189
|
+
var filtered = {};
|
|
190
|
+
locales.forEach(function (loc) {
|
|
191
|
+
if (obj[k][loc]) filtered[loc] = obj[k][loc];
|
|
192
|
+
});
|
|
193
|
+
out[k] = filtered;
|
|
194
|
+
} else {
|
|
195
|
+
out[k] = reduce(obj[k]);
|
|
196
|
+
}
|
|
197
|
+
}
|
|
198
|
+
return out;
|
|
199
|
+
}
|
|
200
|
+
return reduce(manifest);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
/* ---------- import_remote_tree ---------- */
|
|
204
|
+
|
|
205
|
+
function import_remote_tree(payload, conn) {
|
|
206
|
+
_requireDocument('import_remote_tree');
|
|
207
|
+
if (!payload || typeof payload !== 'object') {
|
|
208
|
+
throw _err('invalid', 'import_remote_tree: payload required');
|
|
209
|
+
}
|
|
210
|
+
if (!conn || !conn.endpoint || !conn.bearer) {
|
|
211
|
+
throw _err('invalid', 'import_remote_tree: conn.endpoint + conn.bearer required');
|
|
212
|
+
}
|
|
213
|
+
var transport = conn.transport || 'http';
|
|
214
|
+
var namespace = conn.namespace || payload.app_id;
|
|
215
|
+
if (!namespace) throw _err('invalid', 'import_remote_tree: namespace required (no app_id in payload)');
|
|
216
|
+
if (_remotes[namespace]) {
|
|
217
|
+
throw _err('conflict', 'import_remote_tree: namespace "' + namespace + '" already imported; disconnect first');
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/* Register each remote plugin under namespaced slug. */
|
|
221
|
+
var importedPlugins = [];
|
|
222
|
+
for (var slug in payload.manifests) {
|
|
223
|
+
if (!Object.prototype.hasOwnProperty.call(payload.manifests, slug)) continue;
|
|
224
|
+
var m = payload.manifests[slug];
|
|
225
|
+
var nsSlug = REMOTE_PREFIX + namespace + ':' + slug;
|
|
226
|
+
/* Build a namespaced copy of the manifest. */
|
|
227
|
+
var nsManifest = JSON.parse(JSON.stringify(m));
|
|
228
|
+
nsManifest.plugin_slug = nsSlug;
|
|
229
|
+
nsManifest.__interop_origin = { namespace: namespace, peer_plugin: slug };
|
|
230
|
+
/* Prefix every id in elements / tabs / fields / actions. */
|
|
231
|
+
['elements', 'tabs', 'fields', 'actions'].forEach(function (key) {
|
|
232
|
+
if (Array.isArray(nsManifest[key])) {
|
|
233
|
+
nsManifest[key].forEach(function (el) {
|
|
234
|
+
if (el && el.id) el.__peer_id = el.id;
|
|
235
|
+
if (el && el.id) el.id = REMOTE_PREFIX + namespace + ':' + el.id;
|
|
236
|
+
if (el && el.nac_id) el.__peer_nac_id = el.nac_id;
|
|
237
|
+
if (el && el.nac_id) el.nac_id = REMOTE_PREFIX + namespace + ':' + el.nac_id;
|
|
238
|
+
});
|
|
239
|
+
}
|
|
240
|
+
});
|
|
241
|
+
try {
|
|
242
|
+
global.NAC.register(nsManifest);
|
|
243
|
+
importedPlugins.push(nsSlug);
|
|
244
|
+
} catch (e) {
|
|
245
|
+
console.error('[nac-interop] failed to register namespaced plugin', nsSlug, e);
|
|
246
|
+
}
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
/* Build the handle. */
|
|
250
|
+
var handle = {
|
|
251
|
+
namespace: namespace,
|
|
252
|
+
app_id: payload.app_id,
|
|
253
|
+
app_version: payload.app_version,
|
|
254
|
+
nac_version: payload.nac_version,
|
|
255
|
+
imported_at: _now(),
|
|
256
|
+
transport: transport,
|
|
257
|
+
endpoint: conn.endpoint,
|
|
258
|
+
bearer: conn.bearer,
|
|
259
|
+
hmac_secret: conn.hmac_secret || null,
|
|
260
|
+
imported_plugins: importedPlugins,
|
|
261
|
+
sse: null,
|
|
262
|
+
_stats: { invocations: 0, successes: 0, failures: 0 }
|
|
263
|
+
};
|
|
264
|
+
_remotes[namespace] = handle;
|
|
265
|
+
|
|
266
|
+
/* Auto-subscribe to ack events. */
|
|
267
|
+
if (conn.auto_subscribe !== false && payload.ack_endpoint && transport === 'http') {
|
|
268
|
+
_subscribeRemote(handle, payload.ack_endpoint);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
document.dispatchEvent(new CustomEvent('nac:interop:imported', {
|
|
272
|
+
detail: { namespace: namespace, app_id: payload.app_id, plugins: importedPlugins }
|
|
273
|
+
}));
|
|
274
|
+
|
|
275
|
+
return handle;
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
function _subscribeRemote(handle, ackEndpoint) {
|
|
279
|
+
if (typeof EventSource === 'undefined') {
|
|
280
|
+
console.warn('[nac-interop] EventSource not available; ack subscription skipped');
|
|
281
|
+
return;
|
|
282
|
+
}
|
|
283
|
+
/* Bearer can't go in headers for EventSource; use ?bearer query
|
|
284
|
+
string per the spec. */
|
|
285
|
+
var url = ackEndpoint
|
|
286
|
+
+ (ackEndpoint.indexOf('?') < 0 ? '?' : '&')
|
|
287
|
+
+ 'bearer=' + encodeURIComponent(handle.bearer);
|
|
288
|
+
var es = new EventSource(url);
|
|
289
|
+
es.addEventListener('nac:action:succeeded', function (ev) {
|
|
290
|
+
_relayPeerEvent(handle, 'nac:action:succeeded', ev.data);
|
|
291
|
+
});
|
|
292
|
+
es.addEventListener('nac:action:failed', function (ev) {
|
|
293
|
+
_relayPeerEvent(handle, 'nac:action:failed', ev.data);
|
|
294
|
+
});
|
|
295
|
+
es.addEventListener('nac:tab:activated', function (ev) {
|
|
296
|
+
_relayPeerEvent(handle, 'nac:tab:activated', ev.data);
|
|
297
|
+
});
|
|
298
|
+
es.addEventListener('nac:field:changed', function (ev) {
|
|
299
|
+
_relayPeerEvent(handle, 'nac:field:changed', ev.data);
|
|
300
|
+
});
|
|
301
|
+
es.onerror = function (e) {
|
|
302
|
+
console.warn('[nac-interop] SSE error on', handle.namespace, e);
|
|
303
|
+
};
|
|
304
|
+
handle.sse = es;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
function _relayPeerEvent(handle, eventName, dataStr) {
|
|
308
|
+
var detail;
|
|
309
|
+
try { detail = JSON.parse(dataStr); } catch (_) { detail = { raw: dataStr }; }
|
|
310
|
+
/* Namespace the action_id / field_id / tab_id so local
|
|
311
|
+
listeners see the remote: prefix. */
|
|
312
|
+
['action_id', 'field_id', 'tab_id', 'option_id', 'plugin'].forEach(function (k) {
|
|
313
|
+
if (detail[k] && typeof detail[k] === 'string'
|
|
314
|
+
&& detail[k].indexOf(REMOTE_PREFIX) !== 0) {
|
|
315
|
+
detail[k] = REMOTE_PREFIX + handle.namespace + ':' + detail[k];
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
detail.via_interop = true;
|
|
319
|
+
detail.is_trusted = false;
|
|
320
|
+
document.dispatchEvent(new CustomEvent(eventName, { detail: detail }));
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
function list_remote_apps() {
|
|
324
|
+
return Object.keys(_remotes).map(function (ns) {
|
|
325
|
+
var h = _remotes[ns];
|
|
326
|
+
return {
|
|
327
|
+
namespace: h.namespace,
|
|
328
|
+
app_id: h.app_id,
|
|
329
|
+
nac_version: h.nac_version,
|
|
330
|
+
imported_at: h.imported_at,
|
|
331
|
+
imported_plugins: h.imported_plugins.slice(),
|
|
332
|
+
stats: Object.assign({}, h._stats)
|
|
333
|
+
};
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
function disconnect_remote(namespace) {
|
|
338
|
+
_requireDocument('disconnect_remote');
|
|
339
|
+
var handle = _remotes[namespace];
|
|
340
|
+
if (!handle) return;
|
|
341
|
+
/* Unregister each namespaced plugin. */
|
|
342
|
+
handle.imported_plugins.forEach(function (slug) {
|
|
343
|
+
if (typeof global.NAC.unregister === 'function') {
|
|
344
|
+
try { global.NAC.unregister(slug); } catch (_) {}
|
|
345
|
+
}
|
|
346
|
+
});
|
|
347
|
+
/* Close SSE. */
|
|
348
|
+
if (handle.sse) { try { handle.sse.close(); } catch (_) {} }
|
|
349
|
+
delete _remotes[namespace];
|
|
350
|
+
document.dispatchEvent(new CustomEvent('nac:interop:disconnected', {
|
|
351
|
+
detail: { namespace: namespace, app_id: handle.app_id }
|
|
352
|
+
}));
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/* ---------- HMAC helpers (SubtleCrypto-based) ---------- */
|
|
356
|
+
|
|
357
|
+
async function _hmacSign(secret, body) {
|
|
358
|
+
if (!secret || typeof secret !== 'string') return null;
|
|
359
|
+
if (!global.crypto || !global.crypto.subtle) return null;
|
|
360
|
+
var enc = new TextEncoder();
|
|
361
|
+
var key = await global.crypto.subtle.importKey(
|
|
362
|
+
'raw',
|
|
363
|
+
enc.encode(secret),
|
|
364
|
+
{ name: 'HMAC', hash: 'SHA-256' },
|
|
365
|
+
false,
|
|
366
|
+
['sign']
|
|
367
|
+
);
|
|
368
|
+
var sig = await global.crypto.subtle.sign('HMAC', key, enc.encode(body));
|
|
369
|
+
var arr = new Uint8Array(sig);
|
|
370
|
+
var hex = '';
|
|
371
|
+
for (var i = 0; i < arr.length; i++) {
|
|
372
|
+
hex += arr[i].toString(16).padStart(2, '0');
|
|
373
|
+
}
|
|
374
|
+
return hex;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/* ---------- Proxy invoke ---------- */
|
|
378
|
+
|
|
379
|
+
async function _proxyInvoke(handle, peer_id, action_kind, args) {
|
|
380
|
+
handle._stats.invocations++;
|
|
381
|
+
var body = {
|
|
382
|
+
bearer: handle.bearer,
|
|
383
|
+
nac_id: peer_id,
|
|
384
|
+
action: { kind: action_kind, args: args || {} }
|
|
385
|
+
};
|
|
386
|
+
/* Optional HMAC on sensitive actions; the host can mark verbs
|
|
387
|
+
sensitive via window.__NAC_INTEROP_SENSITIVE_VERBS__. */
|
|
388
|
+
if (handle.hmac_secret) {
|
|
389
|
+
var canonical = JSON.stringify({
|
|
390
|
+
nac_id: body.nac_id, kind: body.action.kind, args: body.action.args
|
|
391
|
+
});
|
|
392
|
+
try { body.hmac = await _hmacSign(handle.hmac_secret, canonical); } catch (_) {}
|
|
393
|
+
}
|
|
394
|
+
var resp;
|
|
395
|
+
try {
|
|
396
|
+
resp = await fetch(handle.endpoint + '/nac.invoke', {
|
|
397
|
+
method: 'POST',
|
|
398
|
+
headers: { 'Content-Type': 'application/json' },
|
|
399
|
+
body: JSON.stringify(body)
|
|
400
|
+
});
|
|
401
|
+
} catch (e) {
|
|
402
|
+
handle._stats.failures++;
|
|
403
|
+
throw _err('network', 'nac.invoke network error: ' + e.message);
|
|
404
|
+
}
|
|
405
|
+
if (!resp.ok) {
|
|
406
|
+
handle._stats.failures++;
|
|
407
|
+
throw _err('http_' + resp.status, 'nac.invoke HTTP ' + resp.status);
|
|
408
|
+
}
|
|
409
|
+
var json;
|
|
410
|
+
try { json = await resp.json(); }
|
|
411
|
+
catch (e) {
|
|
412
|
+
handle._stats.failures++;
|
|
413
|
+
throw _err('parse', 'nac.invoke returned non-JSON');
|
|
414
|
+
}
|
|
415
|
+
if (json.ok === false) {
|
|
416
|
+
handle._stats.failures++;
|
|
417
|
+
throw _err((json.error && json.error.code) || 'peer_error',
|
|
418
|
+
(json.error && json.error.message) || 'peer rejected the invocation');
|
|
419
|
+
}
|
|
420
|
+
handle._stats.successes++;
|
|
421
|
+
return json.result || { ok: true };
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
/* ---------- Patch NAC public API to route remote: prefix ---------- */
|
|
425
|
+
|
|
426
|
+
function _wrap(fnName, kindForProxy, argMapper) {
|
|
427
|
+
var orig = global.NAC[fnName];
|
|
428
|
+
if (typeof orig !== 'function') return;
|
|
429
|
+
global.NAC[fnName] = async function () {
|
|
430
|
+
var args = Array.prototype.slice.call(arguments);
|
|
431
|
+
var firstArg = args[0];
|
|
432
|
+
var parsed = _parseRemote(firstArg);
|
|
433
|
+
if (!parsed) return orig.apply(global.NAC, args);
|
|
434
|
+
var handle = _remotes[parsed.namespace];
|
|
435
|
+
if (!handle) {
|
|
436
|
+
throw _err('not_found',
|
|
437
|
+
fnName + ': namespace "' + parsed.namespace + '" not imported (call NAC.import_remote_tree first)');
|
|
438
|
+
}
|
|
439
|
+
var peerArgs = (typeof argMapper === 'function') ? argMapper(args, parsed) : {};
|
|
440
|
+
try {
|
|
441
|
+
var result = await _proxyInvoke(handle, parsed.peer_id, kindForProxy, peerArgs);
|
|
442
|
+
_emitLocalAck(REMOTE_PREFIX + parsed.namespace + ':' + (peerArgs.plugin || parsed.peer_id.split('.')[0]),
|
|
443
|
+
firstArg, true, { result: result });
|
|
444
|
+
return result;
|
|
445
|
+
} catch (e) {
|
|
446
|
+
_emitLocalAck(REMOTE_PREFIX + parsed.namespace,
|
|
447
|
+
firstArg, false, { error: { code: e.code, message: e.message } });
|
|
448
|
+
throw e;
|
|
449
|
+
}
|
|
450
|
+
};
|
|
451
|
+
return orig;
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
_origClick = _wrap('click', 'click', function (args) { return { opts: args[1] || {} }; });
|
|
455
|
+
_origFill = _wrap('fill', 'fill', function (args) { return { value: args[1] }; });
|
|
456
|
+
_origSelect = _wrap('select', 'select', function (args) { return { value: args[1] }; });
|
|
457
|
+
_origGoToSection = _wrap('go_to_section', 'go_to_section', function () { return {}; });
|
|
458
|
+
_origEditField = _wrap('edit_field', 'edit_field', function () { return {}; });
|
|
459
|
+
|
|
460
|
+
/* tab(plugin, tab_key): if plugin starts with remote: prefix, parse and proxy. */
|
|
461
|
+
_origTab = global.NAC.tab;
|
|
462
|
+
if (typeof _origTab === 'function') {
|
|
463
|
+
global.NAC.tab = async function (plugin, tab_key) {
|
|
464
|
+
var parsedPlugin = _parseRemote(plugin);
|
|
465
|
+
if (!parsedPlugin) return _origTab.call(global.NAC, plugin, tab_key);
|
|
466
|
+
var handle = _remotes[parsedPlugin.namespace];
|
|
467
|
+
if (!handle) throw _err('not_found', 'tab: namespace "' + parsedPlugin.namespace + '" not imported');
|
|
468
|
+
/* tab_key may also have the prefix; strip it if so. */
|
|
469
|
+
var peerTabKey = tab_key;
|
|
470
|
+
var parsedTab = _parseRemote(tab_key);
|
|
471
|
+
if (parsedTab && parsedTab.namespace === parsedPlugin.namespace) {
|
|
472
|
+
peerTabKey = parsedTab.peer_id;
|
|
473
|
+
}
|
|
474
|
+
var result = await _proxyInvoke(handle, parsedPlugin.peer_id,
|
|
475
|
+
'tab', { tab_key: peerTabKey });
|
|
476
|
+
_emitLocalAck(plugin, REMOTE_PREFIX + parsedPlugin.namespace + ':' + peerTabKey,
|
|
477
|
+
true, { result: result });
|
|
478
|
+
return result;
|
|
479
|
+
};
|
|
480
|
+
}
|
|
481
|
+
|
|
482
|
+
/* tab_by_label and click_by_verb also accept a plugin arg. */
|
|
483
|
+
_origTabByLabel = global.NAC.tab_by_label;
|
|
484
|
+
if (typeof _origTabByLabel === 'function') {
|
|
485
|
+
global.NAC.tab_by_label = async function (plugin, label, opts) {
|
|
486
|
+
var parsedPlugin = _parseRemote(plugin);
|
|
487
|
+
if (!parsedPlugin) return _origTabByLabel.call(global.NAC, plugin, label, opts);
|
|
488
|
+
var handle = _remotes[parsedPlugin.namespace];
|
|
489
|
+
if (!handle) throw _err('not_found', 'tab_by_label: namespace not imported');
|
|
490
|
+
return _proxyInvoke(handle, parsedPlugin.peer_id, 'tab_by_label', { label: label });
|
|
491
|
+
};
|
|
492
|
+
}
|
|
493
|
+
|
|
494
|
+
var _origClickByVerb = global.NAC.click_by_verb;
|
|
495
|
+
if (typeof _origClickByVerb === 'function') {
|
|
496
|
+
global.NAC.click_by_verb = async function (plugin, verb, opts) {
|
|
497
|
+
var parsedPlugin = _parseRemote(plugin);
|
|
498
|
+
if (!parsedPlugin) return _origClickByVerb.call(global.NAC, plugin, verb, opts);
|
|
499
|
+
var handle = _remotes[parsedPlugin.namespace];
|
|
500
|
+
if (!handle) throw _err('not_found', 'click_by_verb: namespace not imported');
|
|
501
|
+
return _proxyInvoke(handle, parsedPlugin.peer_id, 'click_by_verb', { verb: verb });
|
|
502
|
+
};
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
/* dt_* family: each takes table_id as first arg. */
|
|
506
|
+
['dt_add_row', 'dt_remove_row', 'dt_edit_cell',
|
|
507
|
+
'dt_commit', 'dt_discard', 'dt_read_aggregate'].forEach(function (fnName) {
|
|
508
|
+
var orig = global.NAC[fnName];
|
|
509
|
+
if (typeof orig !== 'function') return;
|
|
510
|
+
global.NAC[fnName] = function () {
|
|
511
|
+
var args = Array.prototype.slice.call(arguments);
|
|
512
|
+
var tableId = args[0];
|
|
513
|
+
var parsed = _parseRemote(tableId);
|
|
514
|
+
if (!parsed) return orig.apply(global.NAC, args);
|
|
515
|
+
var handle = _remotes[parsed.namespace];
|
|
516
|
+
if (!handle) throw _err('not_found', fnName + ': namespace not imported');
|
|
517
|
+
var argsPayload = {};
|
|
518
|
+
switch (fnName) {
|
|
519
|
+
case 'dt_add_row': argsPayload = { values: args[1] || {} }; break;
|
|
520
|
+
case 'dt_remove_row': argsPayload = { row_id: args[1] }; break;
|
|
521
|
+
case 'dt_edit_cell': argsPayload = { row_id: args[1], column: args[2], value: args[3] }; break;
|
|
522
|
+
case 'dt_commit': argsPayload = {}; break;
|
|
523
|
+
case 'dt_discard': argsPayload = {}; break;
|
|
524
|
+
case 'dt_read_aggregate':argsPayload = { agg_key: args[1], column: args[2] }; break;
|
|
525
|
+
}
|
|
526
|
+
return _proxyInvoke(handle, parsed.peer_id, fnName, argsPayload);
|
|
527
|
+
};
|
|
528
|
+
});
|
|
529
|
+
|
|
530
|
+
/* ---------- Expose on NAC ---------- */
|
|
531
|
+
|
|
532
|
+
global.NAC.export_tree = export_tree;
|
|
533
|
+
global.NAC.import_remote_tree = import_remote_tree;
|
|
534
|
+
global.NAC.list_remote_apps = list_remote_apps;
|
|
535
|
+
global.NAC.disconnect_remote = disconnect_remote;
|
|
536
|
+
|
|
537
|
+
/* Behind a switch since 2026-11-24. This was the only
|
|
538
|
+
unconditional console.log left in published code: an install
|
|
539
|
+
banner every page that loads this file printed, whether or not
|
|
540
|
+
anyone was debugging. It prints no user content -- unlike the
|
|
541
|
+
three in nac-chat-client.js -- so the cost was noise rather than
|
|
542
|
+
disclosure, but a library that writes to the host console
|
|
543
|
+
uninvited is still the host's decision to make, not ours.
|
|
544
|
+
window.NAC_INTEROP_DEBUG = true brings it back. The warn and
|
|
545
|
+
error calls in this file are untouched. */
|
|
546
|
+
if (global.NAC_INTEROP_DEBUG === true && global.console
|
|
547
|
+
&& typeof global.console.log === 'function') {
|
|
548
|
+
global.console.log('[nac-interop] installed (v2.3 preview) on top of NAC ' + global.NAC.version);
|
|
549
|
+
}
|
|
550
|
+
|
|
551
|
+
/* The same guard its three siblings carry. Until 2.9.0 this file ended
|
|
552
|
+
with (window), so the ESM and CJS bundles of the package could not
|
|
553
|
+
even be LOADED outside a browser -- which is one reason it was never
|
|
554
|
+
published. */
|
|
555
|
+
})(typeof window !== 'undefined' ? window : globalThis);
|
|
556
|
+
|