@xpert-ai/plugin-sdk 3.23.0 → 3.25.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/_object_without_properties_loose.cjs.js +16 -0
- package/_object_without_properties_loose.esm.js +14 -0
- package/analytics-result.cjs.js +212 -0
- package/analytics-result.esm.js +201 -0
- package/analytics.cjs.default.js +1 -0
- package/analytics.cjs.js +17 -0
- package/analytics.cjs.mjs +2 -0
- package/analytics.d.ts +1 -0
- package/analytics.esm.js +2 -0
- package/artifact-document.cjs.default.js +1 -0
- package/artifact-document.cjs.js +581 -0
- package/artifact-document.cjs.mjs +2 -0
- package/artifact-document.d.ts +1 -0
- package/artifact-document.esm.js +563 -0
- package/data-workbench.cjs.js +23 -6
- package/data-workbench.esm.js +23 -6
- package/docs/README.md +8 -0
- package/docs/analytics.md +108 -0
- package/docs/artifact-documents.md +74 -0
- package/docs/artifact-drafts.md +24 -0
- package/docs/artifact-presentations.md +43 -0
- package/docs/json-encoded-schema.md +19 -0
- package/docs/mcp-client.md +63 -0
- package/docs/provider-credential-validation.md +34 -0
- package/docs/rest-client.md +24 -0
- package/docs/workspace-dataset-pipelines.md +120 -0
- package/docs/workspace-file-tools.md +66 -0
- package/index.cjs.js +555 -108
- package/index.esm.js +573 -172
- package/model-provider-toolset.cjs.js +1 -1
- package/model-provider-toolset.esm.js +1 -1
- package/model-provider.cjs.js +47 -1
- package/model-provider.esm.js +41 -2
- package/package.json +14 -2
- package/src/analytics.d.ts +3 -0
- package/src/artifact-document.d.ts +5 -0
- package/src/index.d.ts +2 -0
- package/src/lib/agent/handoff/agent-runtime-message.contract.d.ts +80 -80
- package/src/lib/agent/middleware/capabilities/assistant-task.d.ts +36 -1
- package/src/lib/agent/runtime/dispatch.d.ts +284 -284
- package/src/lib/agent/runtime/index.d.ts +1 -0
- package/src/lib/agent/runtime/pipeline-scope.d.ts +110 -0
- package/src/lib/agent/runtime/types.d.ts +4 -2
- package/src/lib/ai-model/ai-model-provider.interface.d.ts +3 -1
- package/src/lib/ai-model/utils/limits.d.ts +4 -2
- package/src/lib/artifact-document/dependencies.d.ts +101 -0
- package/src/lib/artifact-document/host.d.ts +30 -0
- package/src/lib/artifact-document/protocol.d.ts +60 -0
- package/src/lib/artifact-document/runtime.d.ts +4 -0
- package/src/lib/artifact-document/sha256.d.ts +2 -0
- package/src/lib/artifact-presentation/index.d.ts +35 -0
- package/src/lib/core/index.d.ts +1 -0
- package/src/lib/core/json-encoded-schema.d.ts +15 -0
- package/src/lib/data/analytics/analytics-payload.d.ts +3 -0
- package/src/lib/data/analytics/analytics-provider.d.ts +74 -0
- package/src/lib/data/analytics/analytics-result.d.ts +4 -0
- package/src/lib/data/analytics/analytics.registry.d.ts +10 -0
- package/src/lib/data/analytics/analytics.schema.d.ts +206 -0
- package/src/lib/data/analytics/index.d.ts +5 -0
- package/src/lib/data/datasource/index.d.ts +1 -0
- package/src/lib/data/datasource/types.d.ts +1 -1
- package/src/lib/data/datasource/workbench-adapter.d.ts +11 -1
- package/src/lib/data/datasource/workbench-sql.d.ts +12 -0
- package/src/lib/project-pipeline/adapters.d.ts +87 -0
- package/src/lib/project-pipeline/capability.d.ts +100 -0
- package/src/lib/project-pipeline/index.d.ts +5 -0
- package/src/lib/project-pipeline/projection.d.ts +71 -0
- package/src/lib/project-pipeline/registry.d.ts +509 -0
- package/src/lib/project-pipeline/runtime-registry.d.ts +45 -0
- package/src/lib/project-pipeline/scoped-registry.d.ts +10 -0
- package/src/lib/project-task/index.d.ts +6 -0
- package/src/lib/project-type/index.d.ts +17 -1
- package/src/lib/rag/types.d.ts +1 -1
- package/src/lib/runtime/capabilities/artifact-drafts.d.ts +29 -0
- package/src/lib/runtime/capabilities/artifacts.d.ts +12 -0
- package/src/lib/runtime/capabilities/index.d.ts +3 -0
- package/src/lib/runtime/capabilities/mcp-client-runtime.d.ts +41 -0
- package/src/lib/runtime/capabilities/rest-client-runtime.d.ts +34 -0
- package/src/lib/runtime/capabilities/sandbox-jobs.d.ts +36 -2
- package/src/lib/runtime/index.d.ts +2 -0
- package/src/lib/runtime/mutation-evidence.d.ts +7 -0
- package/src/lib/runtime/workspace-file-tools.d.ts +11 -0
- package/src/lib/sandbox/file-edits.d.ts +4 -0
- package/src/lib/sandbox/protocol.d.ts +7 -1
- package/src/lib/sandbox/runtime-provider.d.ts +4 -0
- package/src/lib/sandbox/sandbox.d.ts +2 -8
- package/src/lib/tool-provider/adapters.d.ts +1 -1
- package/src/lib/tool-provider/types.d.ts +2 -0
|
@@ -0,0 +1,581 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var v3 = require('zod/v3');
|
|
4
|
+
var _object_without_properties_loose = require('./_object_without_properties_loose.cjs.js');
|
|
5
|
+
|
|
6
|
+
/** Wire namespace/version shared by the trusted host and its isolated document bootstrap. */ const ARTIFACT_DOCUMENT_CHANNEL = 'xpert.artifact-document';
|
|
7
|
+
const ARTIFACT_DOCUMENT_VERSION = 1;
|
|
8
|
+
/** Portable binding/control identifier; shape validation never grants resource access. */ const artifactDocumentId = v3.z.string().regex(/^[A-Za-z][A-Za-z0-9_.-]{0,79}$/);
|
|
9
|
+
const artifactDocumentValue = v3.z.union([
|
|
10
|
+
v3.z.string().max(4000),
|
|
11
|
+
v3.z.number().finite(),
|
|
12
|
+
v3.z.boolean(),
|
|
13
|
+
v3.z.null()
|
|
14
|
+
]);
|
|
15
|
+
const artifactDocumentControls = v3.z.record(artifactDocumentId, artifactDocumentValue).refine((value)=>Object.keys(value).length <= 24, 'Too many controls');
|
|
16
|
+
/** The entire untrusted query envelope. Provider definitions, SQL and identity fields are forbidden. */ const artifactDocumentQuery = v3.z.object({
|
|
17
|
+
type: v3.z.literal('query'),
|
|
18
|
+
requestId: v3.z.string().min(1).max(80),
|
|
19
|
+
bindingId: artifactDocumentId,
|
|
20
|
+
controls: artifactDocumentControls
|
|
21
|
+
}).strict();
|
|
22
|
+
/** Only public presentation context may cross into the untrusted frame. Never include credentials here. */ const artifactDocumentContext = v3.z.object({
|
|
23
|
+
bindingIds: v3.z.array(artifactDocumentId).max(64).refine((ids)=>new Set(ids).size === ids.length, 'Duplicate binding IDs'),
|
|
24
|
+
defaults: artifactDocumentControls,
|
|
25
|
+
theme: v3.z.enum([
|
|
26
|
+
'light',
|
|
27
|
+
'dark'
|
|
28
|
+
]),
|
|
29
|
+
locale: v3.z.string().min(1).max(80)
|
|
30
|
+
}).strict();
|
|
31
|
+
/** Format/UTF-8 size gate, not a sanitizer. Isolation and server authorization enforce security. */ const artifactDocumentHtml = v3.z.string().min(1).max(300000).superRefine((html, ctx)=>{
|
|
32
|
+
if (new TextEncoder().encode(html).byteLength > 300000 || !/<html[\s>]/i.test(html) || !/<body[\s>]/i.test(html)) ctx.addIssue({
|
|
33
|
+
code: 'custom',
|
|
34
|
+
message: 'Supply a complete UTF-8 HTML document, at most 300 KB'
|
|
35
|
+
});
|
|
36
|
+
});
|
|
37
|
+
|
|
38
|
+
// Why this exists: dependency verification is synchronous and also runs on HTTP
|
|
39
|
+
// intranets and in opaque-origin documents, where Web Crypto may be unavailable.
|
|
40
|
+
// SHA-256 per FIPS 180-4 sections 5/6.2; only for bounded public asset integrity.
|
|
41
|
+
// https://doi.org/10.6028/NIST.FIPS.180-4
|
|
42
|
+
const ROUND_CONSTANTS = new Uint32Array([
|
|
43
|
+
0x428a2f98,
|
|
44
|
+
0x71374491,
|
|
45
|
+
0xb5c0fbcf,
|
|
46
|
+
0xe9b5dba5,
|
|
47
|
+
0x3956c25b,
|
|
48
|
+
0x59f111f1,
|
|
49
|
+
0x923f82a4,
|
|
50
|
+
0xab1c5ed5,
|
|
51
|
+
0xd807aa98,
|
|
52
|
+
0x12835b01,
|
|
53
|
+
0x243185be,
|
|
54
|
+
0x550c7dc3,
|
|
55
|
+
0x72be5d74,
|
|
56
|
+
0x80deb1fe,
|
|
57
|
+
0x9bdc06a7,
|
|
58
|
+
0xc19bf174,
|
|
59
|
+
0xe49b69c1,
|
|
60
|
+
0xefbe4786,
|
|
61
|
+
0x0fc19dc6,
|
|
62
|
+
0x240ca1cc,
|
|
63
|
+
0x2de92c6f,
|
|
64
|
+
0x4a7484aa,
|
|
65
|
+
0x5cb0a9dc,
|
|
66
|
+
0x76f988da,
|
|
67
|
+
0x983e5152,
|
|
68
|
+
0xa831c66d,
|
|
69
|
+
0xb00327c8,
|
|
70
|
+
0xbf597fc7,
|
|
71
|
+
0xc6e00bf3,
|
|
72
|
+
0xd5a79147,
|
|
73
|
+
0x06ca6351,
|
|
74
|
+
0x14292967,
|
|
75
|
+
0x27b70a85,
|
|
76
|
+
0x2e1b2138,
|
|
77
|
+
0x4d2c6dfc,
|
|
78
|
+
0x53380d13,
|
|
79
|
+
0x650a7354,
|
|
80
|
+
0x766a0abb,
|
|
81
|
+
0x81c2c92e,
|
|
82
|
+
0x92722c85,
|
|
83
|
+
0xa2bfe8a1,
|
|
84
|
+
0xa81a664b,
|
|
85
|
+
0xc24b8b70,
|
|
86
|
+
0xc76c51a3,
|
|
87
|
+
0xd192e819,
|
|
88
|
+
0xd6990624,
|
|
89
|
+
0xf40e3585,
|
|
90
|
+
0x106aa070,
|
|
91
|
+
0x19a4c116,
|
|
92
|
+
0x1e376c08,
|
|
93
|
+
0x2748774c,
|
|
94
|
+
0x34b0bcb5,
|
|
95
|
+
0x391c0cb3,
|
|
96
|
+
0x4ed8aa4a,
|
|
97
|
+
0x5b9cca4f,
|
|
98
|
+
0x682e6ff3,
|
|
99
|
+
0x748f82ee,
|
|
100
|
+
0x78a5636f,
|
|
101
|
+
0x84c87814,
|
|
102
|
+
0x8cc70208,
|
|
103
|
+
0x90befffa,
|
|
104
|
+
0xa4506ceb,
|
|
105
|
+
0xbef9a3f7,
|
|
106
|
+
0xc67178f2
|
|
107
|
+
]);
|
|
108
|
+
const rotateRight = (value, bits)=>value >>> bits | value << 32 - bits;
|
|
109
|
+
/** Internal one-shot digest. Callers bound asset sizes before allocating hashing buffers. */ function sha256Hex(bytes) {
|
|
110
|
+
const padded = new Uint8Array(Math.ceil((bytes.length + 9) / 64) * 64);
|
|
111
|
+
padded.set(bytes);
|
|
112
|
+
padded[bytes.length] = 0x80;
|
|
113
|
+
const view = new DataView(padded.buffer);
|
|
114
|
+
// SHA-256 appends the original bit length as a big-endian 64-bit integer.
|
|
115
|
+
view.setUint32(padded.length - 8, Math.floor(bytes.length / 0x20000000));
|
|
116
|
+
view.setUint32(padded.length - 4, bytes.length * 8 >>> 0);
|
|
117
|
+
const state = new Uint32Array([
|
|
118
|
+
0x6a09e667,
|
|
119
|
+
0xbb67ae85,
|
|
120
|
+
0x3c6ef372,
|
|
121
|
+
0xa54ff53a,
|
|
122
|
+
0x510e527f,
|
|
123
|
+
0x9b05688c,
|
|
124
|
+
0x1f83d9ab,
|
|
125
|
+
0x5be0cd19
|
|
126
|
+
]);
|
|
127
|
+
const words = new Uint32Array(64);
|
|
128
|
+
for(let offset = 0; offset < padded.length; offset += 64){
|
|
129
|
+
for(let i = 0; i < 16; i++)words[i] = view.getUint32(offset + i * 4);
|
|
130
|
+
for(let i = 16; i < 64; i++){
|
|
131
|
+
const x = words[i - 15], y = words[i - 2];
|
|
132
|
+
const s0 = rotateRight(x, 7) ^ rotateRight(x, 18) ^ x >>> 3;
|
|
133
|
+
const s1 = rotateRight(y, 17) ^ rotateRight(y, 19) ^ y >>> 10;
|
|
134
|
+
words[i] = words[i - 16] + s0 + words[i - 7] + s1 >>> 0;
|
|
135
|
+
}
|
|
136
|
+
let [a, b, c, d, e, f, g, h] = state;
|
|
137
|
+
for(let i = 0; i < 64; i++){
|
|
138
|
+
const s1 = rotateRight(e, 6) ^ rotateRight(e, 11) ^ rotateRight(e, 25);
|
|
139
|
+
const choice = e & f ^ ~e & g;
|
|
140
|
+
const t1 = h + s1 + choice + ROUND_CONSTANTS[i] + words[i] >>> 0;
|
|
141
|
+
const s0 = rotateRight(a, 2) ^ rotateRight(a, 13) ^ rotateRight(a, 22);
|
|
142
|
+
const majority = a & b ^ a & c ^ b & c;
|
|
143
|
+
const t2 = s0 + majority >>> 0;
|
|
144
|
+
h = g;
|
|
145
|
+
g = f;
|
|
146
|
+
f = e;
|
|
147
|
+
e = d + t1 >>> 0;
|
|
148
|
+
d = c;
|
|
149
|
+
c = b;
|
|
150
|
+
b = a;
|
|
151
|
+
a = t1 + t2 >>> 0;
|
|
152
|
+
}
|
|
153
|
+
state[0] = state[0] + a >>> 0;
|
|
154
|
+
state[1] = state[1] + b >>> 0;
|
|
155
|
+
state[2] = state[2] + c >>> 0;
|
|
156
|
+
state[3] = state[3] + d >>> 0;
|
|
157
|
+
state[4] = state[4] + e >>> 0;
|
|
158
|
+
state[5] = state[5] + f >>> 0;
|
|
159
|
+
state[6] = state[6] + g >>> 0;
|
|
160
|
+
state[7] = state[7] + h >>> 0;
|
|
161
|
+
}
|
|
162
|
+
return Array.from(state, (word)=>word.toString(16).padStart(8, '0')).join('');
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
/** A lock is public metadata; only the trusted host resolves it to packaged, immutable bytes. */ const dependencyLockObject = v3.z.object({
|
|
166
|
+
id: artifactDocumentId,
|
|
167
|
+
version: v3.z.string().regex(/^\d+\.\d+\.\d+(?:-[A-Za-z0-9.-]+)?$/),
|
|
168
|
+
format: v3.z.literal('classic-script'),
|
|
169
|
+
global: v3.z.string().regex(/^[A-Za-z_$][A-Za-z0-9_$]{0,79}$/).refine((value)=>![
|
|
170
|
+
'window',
|
|
171
|
+
'self',
|
|
172
|
+
'globalThis',
|
|
173
|
+
'document',
|
|
174
|
+
'parent',
|
|
175
|
+
'top',
|
|
176
|
+
'location',
|
|
177
|
+
'xpertLiveArtifact',
|
|
178
|
+
'__proto__',
|
|
179
|
+
'constructor',
|
|
180
|
+
'prototype'
|
|
181
|
+
].includes(value)),
|
|
182
|
+
sha256: v3.z.string().regex(/^[a-f0-9]{64}$/)
|
|
183
|
+
}).strict();
|
|
184
|
+
// Explicit outputs keep required fields in declarations emitted by non-strict host tsconfigs.
|
|
185
|
+
const artifactDocumentDependencyLock = dependencyLockObject.transform((value)=>({
|
|
186
|
+
id: value.id,
|
|
187
|
+
version: value.version,
|
|
188
|
+
format: 'classic-script',
|
|
189
|
+
global: value.global,
|
|
190
|
+
sha256: value.sha256
|
|
191
|
+
}));
|
|
192
|
+
const artifactDocumentDependencyLocks = v3.z.array(artifactDocumentDependencyLock).max(8).superRefine((values, ctx)=>{
|
|
193
|
+
if (new Set(values.map((value)=>value.id)).size !== values.length || new Set(values.map((value)=>value.global)).size !== values.length) ctx.addIssue({
|
|
194
|
+
code: 'custom',
|
|
195
|
+
message: 'Duplicate dependency ID or global'
|
|
196
|
+
});
|
|
197
|
+
});
|
|
198
|
+
const artifactDocumentDependencyAssets = v3.z.array(dependencyLockObject.extend({
|
|
199
|
+
source: v3.z.string().min(1).max(4000000)
|
|
200
|
+
}).strict().transform((value)=>({
|
|
201
|
+
id: value.id,
|
|
202
|
+
version: value.version,
|
|
203
|
+
format: 'classic-script',
|
|
204
|
+
global: value.global,
|
|
205
|
+
sha256: value.sha256,
|
|
206
|
+
source: value.source
|
|
207
|
+
}))).max(8);
|
|
208
|
+
/** Browser/server identical verification. Integrity is not authorization: the caller owns library admission. */ function verifyArtifactDocumentDependencies(input, expected) {
|
|
209
|
+
const assets = artifactDocumentDependencyAssets.parse(input);
|
|
210
|
+
const locks = artifactDocumentDependencyLocks.parse(assets.map((_param)=>{
|
|
211
|
+
var { source } = _param, lock = _object_without_properties_loose._object_without_properties_loose(_param, [
|
|
212
|
+
"source"
|
|
213
|
+
]);
|
|
214
|
+
return lock;
|
|
215
|
+
}));
|
|
216
|
+
let total = 0;
|
|
217
|
+
for (const asset of assets){
|
|
218
|
+
const bytes = new TextEncoder().encode(asset.source);
|
|
219
|
+
total += bytes.length;
|
|
220
|
+
if (bytes.length > 4000000 || total > 8000000) throw new Error('DOCUMENT_DEPENDENCY_TOO_LARGE');
|
|
221
|
+
if (sha256Hex(bytes) !== asset.sha256) throw new Error('DOCUMENT_DEPENDENCY_INTEGRITY_FAILED');
|
|
222
|
+
}
|
|
223
|
+
if (expected && JSON.stringify(artifactDocumentDependencyLocks.parse(expected)) !== JSON.stringify(locks)) throw new Error('DOCUMENT_DEPENDENCY_LOCK_MISMATCH');
|
|
224
|
+
return assets.map((asset)=>({
|
|
225
|
+
id: asset.id,
|
|
226
|
+
version: asset.version,
|
|
227
|
+
global: asset.global,
|
|
228
|
+
sha256: asset.sha256,
|
|
229
|
+
source: asset.source,
|
|
230
|
+
format: 'classic-script'
|
|
231
|
+
}));
|
|
232
|
+
}
|
|
233
|
+
/** Assign script.textContent to preserve exact bytes without eval or HTML parser termination hazards. */ function renderArtifactDocumentDependencies(assets) {
|
|
234
|
+
const literal = (value)=>JSON.stringify(value).replace(/</g, '\\u003c').replace(/>/g, '\\u003e');
|
|
235
|
+
return verifyArtifactDocumentDependencies(assets).map((asset)=>`<script>(()=>{const s=document.createElement('script');s.textContent=${literal(asset.source)};document.head.appendChild(s);s.remove();if(typeof window[${literal(asset.global)}]==='undefined')throw new Error('DOCUMENT_DEPENDENCY_GLOBAL_MISSING');})();</script>`).join('');
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
const ARTIFACT_DOCUMENT_CSP = [
|
|
239
|
+
"default-src 'none'",
|
|
240
|
+
"script-src 'unsafe-inline'",
|
|
241
|
+
"style-src 'unsafe-inline'",
|
|
242
|
+
'img-src data: blob:',
|
|
243
|
+
'font-src data:',
|
|
244
|
+
"connect-src 'none'",
|
|
245
|
+
"frame-src 'none'",
|
|
246
|
+
"worker-src 'none'",
|
|
247
|
+
"object-src 'none'",
|
|
248
|
+
"base-uri 'none'",
|
|
249
|
+
"form-action 'none'"
|
|
250
|
+
].join('; ');
|
|
251
|
+
/** Host-owned prefix precedes all authored markup, including malformed/duplicate heads. */ function renderArtifactDocument(html, sessionId, dependencies = []) {
|
|
252
|
+
artifactDocumentHtml.parse(html);
|
|
253
|
+
if (!/^[a-f0-9]{32}$/.test(sessionId)) throw new Error('Invalid Artifact document session ID');
|
|
254
|
+
return `<!doctype html><meta charset="utf-8"><meta http-equiv="Content-Security-Policy" content="${ARTIFACT_DOCUMENT_CSP}"><script>${ARTIFACT_DOCUMENT_RUNTIME.replace('__XPERT_DOCUMENT_SESSION__', sessionId)}</script>${renderArtifactDocumentDependencies(dependencies)}${html}`;
|
|
255
|
+
}
|
|
256
|
+
const ARTIFACT_DOCUMENT_RUNTIME = String.raw`
|
|
257
|
+
;(() => {
|
|
258
|
+
'use strict'
|
|
259
|
+
const SESSION_ID = '__XPERT_DOCUMENT_SESSION__'
|
|
260
|
+
const CHANNEL = 'xpert.artifact-document',
|
|
261
|
+
VERSION = 1,
|
|
262
|
+
pending = new Map(),
|
|
263
|
+
listeners = new Map(),
|
|
264
|
+
lastControls = new Map(),
|
|
265
|
+
latest = new Map()
|
|
266
|
+
let port,
|
|
267
|
+
context,
|
|
268
|
+
sequence = 0,
|
|
269
|
+
resolveReady
|
|
270
|
+
const ready = new Promise((resolve) => (resolveReady = resolve))
|
|
271
|
+
const emit = (id, dataset) => {
|
|
272
|
+
for (const fn of listeners.get(id) || [])
|
|
273
|
+
try {
|
|
274
|
+
fn(dataset)
|
|
275
|
+
} catch (e) {
|
|
276
|
+
console.error(e)
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
const fail = (code) => Object.assign(new Error(code), { code })
|
|
280
|
+
const query = async (bindingId, controls = {}) => {
|
|
281
|
+
await ready
|
|
282
|
+
if (!context.bindingIds.includes(bindingId)) {
|
|
283
|
+
port.postMessage({ type: 'diagnostic', code: 'DOCUMENT_BINDING_DENIED' })
|
|
284
|
+
throw fail('DOCUMENT_BINDING_DENIED')
|
|
285
|
+
}
|
|
286
|
+
const requestId = 'q' + ++sequence,
|
|
287
|
+
values = { ...context.defaults, ...controls }
|
|
288
|
+
lastControls.set(bindingId, values)
|
|
289
|
+
latest.set(bindingId, requestId)
|
|
290
|
+
return new Promise((resolve, reject) => {
|
|
291
|
+
const timer = setTimeout(() => {
|
|
292
|
+
pending.delete(requestId)
|
|
293
|
+
reject(fail('DOCUMENT_QUERY_TIMEOUT'))
|
|
294
|
+
}, 45000)
|
|
295
|
+
pending.set(requestId, { resolve, reject, timer, bindingId })
|
|
296
|
+
port.postMessage({ type: 'query', requestId, bindingId, controls: values })
|
|
297
|
+
})
|
|
298
|
+
}
|
|
299
|
+
const subscribe = (id, fn) => {
|
|
300
|
+
if (typeof fn !== 'function') throw fail('DOCUMENT_INVALID_LISTENER')
|
|
301
|
+
const set = listeners.get(id) || new Set()
|
|
302
|
+
set.add(fn)
|
|
303
|
+
listeners.set(id, set)
|
|
304
|
+
return () => set.delete(fn)
|
|
305
|
+
}
|
|
306
|
+
const refresh = async (id) => query(id, lastControls.get(id) || {})
|
|
307
|
+
const bindSelect = (element, config) => {
|
|
308
|
+
if (
|
|
309
|
+
!element ||
|
|
310
|
+
element.tagName !== 'SELECT' ||
|
|
311
|
+
!config ||
|
|
312
|
+
typeof config.bindingId !== 'string' ||
|
|
313
|
+
typeof config.controlId !== 'string' ||
|
|
314
|
+
typeof config.fieldId !== 'string'
|
|
315
|
+
)
|
|
316
|
+
throw fail('DOCUMENT_INVALID_SELECT')
|
|
317
|
+
const allValue = config.allValue === undefined ? null : config.allValue,
|
|
318
|
+
known = new Set(),
|
|
319
|
+
append = (value, label) => {
|
|
320
|
+
const key = JSON.stringify(value)
|
|
321
|
+
if (known.has(key)) return
|
|
322
|
+
known.add(key)
|
|
323
|
+
const option = document.createElement('option')
|
|
324
|
+
option.value = key
|
|
325
|
+
option.textContent = label
|
|
326
|
+
element.append(option)
|
|
327
|
+
}
|
|
328
|
+
element.replaceChildren()
|
|
329
|
+
append(allValue, config.allLabel || 'All')
|
|
330
|
+
const update = (dataset) => {
|
|
331
|
+
const selected = (lastControls.get(config.bindingId) || context.defaults)[config.controlId]
|
|
332
|
+
for (const row of dataset.rows || []) {
|
|
333
|
+
const value = row[config.fieldId]
|
|
334
|
+
if (value !== null && ['string', 'number', 'boolean'].includes(typeof value)) append(value, String(value))
|
|
335
|
+
}
|
|
336
|
+
const key = JSON.stringify(selected === undefined ? allValue : selected)
|
|
337
|
+
if (known.has(key)) element.value = key
|
|
338
|
+
}
|
|
339
|
+
const unsubscribe = subscribe(config.bindingId, update),
|
|
340
|
+
change = () =>
|
|
341
|
+
ready
|
|
342
|
+
.then(() =>
|
|
343
|
+
query(config.bindingId, {
|
|
344
|
+
...(lastControls.get(config.bindingId) || context.defaults),
|
|
345
|
+
[config.controlId]: JSON.parse(element.value)
|
|
346
|
+
})
|
|
347
|
+
)
|
|
348
|
+
.catch((error) =>
|
|
349
|
+
element.dispatchEvent(new CustomEvent('xpert-query-error', { detail: { code: error.code }, bubbles: true }))
|
|
350
|
+
)
|
|
351
|
+
element.addEventListener('change', change)
|
|
352
|
+
return () => {
|
|
353
|
+
unsubscribe()
|
|
354
|
+
element.removeEventListener('change', change)
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
Object.defineProperty(window, 'xpertLiveArtifact', {
|
|
358
|
+
value: Object.freeze({ ready: () => ready, query, subscribe, refresh, bindSelect }),
|
|
359
|
+
writable: false,
|
|
360
|
+
configurable: false
|
|
361
|
+
})
|
|
362
|
+
window.addEventListener('message', (event) => {
|
|
363
|
+
const data = event.data
|
|
364
|
+
if (
|
|
365
|
+
port ||
|
|
366
|
+
event.source !== parent ||
|
|
367
|
+
!data ||
|
|
368
|
+
data.channel !== CHANNEL ||
|
|
369
|
+
data.version !== VERSION ||
|
|
370
|
+
data.type !== 'init' ||
|
|
371
|
+
data.sessionId !== SESSION_ID ||
|
|
372
|
+
!event.ports[0]
|
|
373
|
+
)
|
|
374
|
+
return
|
|
375
|
+
port = event.ports[0]
|
|
376
|
+
context = data.context
|
|
377
|
+
document.documentElement.dataset.theme = context.theme
|
|
378
|
+
document.documentElement.style.colorScheme = context.theme
|
|
379
|
+
port.onmessage = (event) => {
|
|
380
|
+
const data = event.data
|
|
381
|
+
if (data.type === 'refresh') {
|
|
382
|
+
for (const id of new Set([...listeners.keys(), ...lastControls.keys()])) refresh(id).catch(console.error)
|
|
383
|
+
return
|
|
384
|
+
}
|
|
385
|
+
if (data.type !== 'result') return
|
|
386
|
+
const entry = pending.get(data.requestId)
|
|
387
|
+
if (!entry) return
|
|
388
|
+
pending.delete(data.requestId)
|
|
389
|
+
clearTimeout(entry.timer)
|
|
390
|
+
if (latest.get(entry.bindingId) !== data.requestId) {
|
|
391
|
+
entry.reject(fail('DOCUMENT_QUERY_SUPERSEDED'))
|
|
392
|
+
return
|
|
393
|
+
}
|
|
394
|
+
if (data.ok) {
|
|
395
|
+
emit(entry.bindingId, data.dataset)
|
|
396
|
+
entry.resolve(data.dataset)
|
|
397
|
+
} else entry.reject(fail(data.code || 'DOCUMENT_QUERY_FAILED'))
|
|
398
|
+
}
|
|
399
|
+
port.start()
|
|
400
|
+
resolveReady(context)
|
|
401
|
+
})
|
|
402
|
+
parent.postMessage({ channel: CHANNEL, version: VERSION, type: 'ready', sessionId: SESSION_ID }, '*')
|
|
403
|
+
})()
|
|
404
|
+
`;
|
|
405
|
+
|
|
406
|
+
// Invariants: a frame WindowProxy survives navigation, so source checks alone
|
|
407
|
+
// cannot identify a document generation. Each mount owns one handshake nonce,
|
|
408
|
+
// one MessagePort and a snapshot of the declared binding IDs.
|
|
409
|
+
const readySchema = v3.z.object({
|
|
410
|
+
channel: v3.z.literal(ARTIFACT_DOCUMENT_CHANNEL),
|
|
411
|
+
version: v3.z.literal(ARTIFACT_DOCUMENT_VERSION),
|
|
412
|
+
type: v3.z.literal('ready'),
|
|
413
|
+
sessionId: v3.z.string().regex(/^[a-f0-9]{32}$/)
|
|
414
|
+
}).strict();
|
|
415
|
+
const diagnosticSchema = v3.z.object({
|
|
416
|
+
type: v3.z.literal('diagnostic'),
|
|
417
|
+
code: v3.z.literal('DOCUMENT_BINDING_DENIED')
|
|
418
|
+
}).strict();
|
|
419
|
+
/** Dedicated query bridge. Requires a dedicated host document with installArtifactDocumentFramePolicy installed. */ function mountArtifactDocument(frame, options) {
|
|
420
|
+
if (!frame.ownerDocument.querySelector('meta[data-xpert-document-policy]')) {
|
|
421
|
+
throw new Error('Install the Artifact document frame policy before mounting a document');
|
|
422
|
+
}
|
|
423
|
+
const context = artifactDocumentContext.parse(options.context);
|
|
424
|
+
const sessionId = Array.from(crypto.getRandomValues(new Uint8Array(16)), (byte)=>byte.toString(16).padStart(2, '0')).join('');
|
|
425
|
+
// Validate/render before allocating a channel or installing listeners.
|
|
426
|
+
const source = renderArtifactDocument(options.html, sessionId, options.dependencies);
|
|
427
|
+
const channel = new MessageChannel();
|
|
428
|
+
const bindingIds = new Set(context.bindingIds);
|
|
429
|
+
const requestIds = new Set();
|
|
430
|
+
const activeIds = new Set();
|
|
431
|
+
const latest = new Map();
|
|
432
|
+
const hostWindow = frame.ownerDocument.defaultView;
|
|
433
|
+
let connected = false, disposed = false, requests = 0, started = Date.now();
|
|
434
|
+
frame.setAttribute('sandbox', 'allow-scripts');
|
|
435
|
+
frame.referrerPolicy = 'no-referrer';
|
|
436
|
+
// Host UI observers cannot leave query accounting stuck or alter protocol completion.
|
|
437
|
+
const observe = (callback, value)=>{
|
|
438
|
+
try {
|
|
439
|
+
callback == null ? void 0 : callback(value);
|
|
440
|
+
} catch (e) {
|
|
441
|
+
/* The query channel must survive a failing UI observer. */ }
|
|
442
|
+
};
|
|
443
|
+
const post = (message)=>{
|
|
444
|
+
if (!disposed) channel.port1.postMessage(message);
|
|
445
|
+
};
|
|
446
|
+
const reject = (requestId, code)=>{
|
|
447
|
+
post({
|
|
448
|
+
type: 'result',
|
|
449
|
+
requestId,
|
|
450
|
+
ok: false,
|
|
451
|
+
code
|
|
452
|
+
});
|
|
453
|
+
observe(options.onError, code);
|
|
454
|
+
};
|
|
455
|
+
channel.port1.onmessage = async (event)=>{
|
|
456
|
+
if (disposed) return;
|
|
457
|
+
const diagnostic = diagnosticSchema.safeParse(event.data);
|
|
458
|
+
if (diagnostic.success) {
|
|
459
|
+
observe(options.onError, diagnostic.data.code);
|
|
460
|
+
return;
|
|
461
|
+
}
|
|
462
|
+
const parsed = artifactDocumentQuery.safeParse(event.data);
|
|
463
|
+
if (!parsed.success) {
|
|
464
|
+
observe(options.onError, 'DOCUMENT_INVALID_REQUEST');
|
|
465
|
+
return;
|
|
466
|
+
}
|
|
467
|
+
const input = {
|
|
468
|
+
type: parsed.data.type,
|
|
469
|
+
requestId: parsed.data.requestId,
|
|
470
|
+
bindingId: parsed.data.bindingId,
|
|
471
|
+
controls: parsed.data.controls
|
|
472
|
+
};
|
|
473
|
+
if (!bindingIds.has(input.bindingId)) {
|
|
474
|
+
reject(input.requestId, 'DOCUMENT_BINDING_DENIED');
|
|
475
|
+
return;
|
|
476
|
+
}
|
|
477
|
+
if (Date.now() - started >= 60000) {
|
|
478
|
+
requests = 0;
|
|
479
|
+
started = Date.now();
|
|
480
|
+
requestIds.clear();
|
|
481
|
+
}
|
|
482
|
+
// Keep in-flight IDs protected even after the one-minute replay/rate window rolls over.
|
|
483
|
+
if (activeIds.has(input.requestId) || requestIds.has(input.requestId)) {
|
|
484
|
+
reject(input.requestId, 'DOCUMENT_REQUEST_REPLAY');
|
|
485
|
+
return;
|
|
486
|
+
}
|
|
487
|
+
if (activeIds.size >= 8 || ++requests > 60) {
|
|
488
|
+
reject(input.requestId, 'DOCUMENT_QUERY_LIMIT');
|
|
489
|
+
return;
|
|
490
|
+
}
|
|
491
|
+
requestIds.add(input.requestId);
|
|
492
|
+
activeIds.add(input.requestId);
|
|
493
|
+
latest.set(input.bindingId, input.requestId);
|
|
494
|
+
observe(options.onBusy, true);
|
|
495
|
+
try {
|
|
496
|
+
const dataset = await options.query(input);
|
|
497
|
+
if (!disposed) {
|
|
498
|
+
post({
|
|
499
|
+
type: 'result',
|
|
500
|
+
requestId: input.requestId,
|
|
501
|
+
ok: true,
|
|
502
|
+
dataset
|
|
503
|
+
});
|
|
504
|
+
if (latest.get(input.bindingId) === input.requestId) observe(options.onResult, dataset);
|
|
505
|
+
}
|
|
506
|
+
} catch (e) {
|
|
507
|
+
if (!disposed) {
|
|
508
|
+
post({
|
|
509
|
+
type: 'result',
|
|
510
|
+
requestId: input.requestId,
|
|
511
|
+
ok: false,
|
|
512
|
+
code: 'DOCUMENT_QUERY_FAILED'
|
|
513
|
+
});
|
|
514
|
+
if (latest.get(input.bindingId) === input.requestId) observe(options.onError, 'DOCUMENT_QUERY_FAILED');
|
|
515
|
+
}
|
|
516
|
+
} finally{
|
|
517
|
+
activeIds.delete(input.requestId);
|
|
518
|
+
if (!disposed) observe(options.onBusy, activeIds.size > 0);
|
|
519
|
+
}
|
|
520
|
+
};
|
|
521
|
+
const ready = (event)=>{
|
|
522
|
+
var _frame_contentWindow;
|
|
523
|
+
if (disposed || connected || event.source !== frame.contentWindow) return;
|
|
524
|
+
const parsed = readySchema.safeParse(event.data);
|
|
525
|
+
if (!parsed.success || parsed.data.sessionId !== sessionId) return;
|
|
526
|
+
connected = true;
|
|
527
|
+
(_frame_contentWindow = frame.contentWindow) == null ? void 0 : _frame_contentWindow.postMessage({
|
|
528
|
+
channel: ARTIFACT_DOCUMENT_CHANNEL,
|
|
529
|
+
version: ARTIFACT_DOCUMENT_VERSION,
|
|
530
|
+
type: 'init',
|
|
531
|
+
sessionId,
|
|
532
|
+
context
|
|
533
|
+
}, '*', [
|
|
534
|
+
channel.port2
|
|
535
|
+
]);
|
|
536
|
+
};
|
|
537
|
+
hostWindow.addEventListener('message', ready);
|
|
538
|
+
channel.port1.start();
|
|
539
|
+
frame.srcdoc = source;
|
|
540
|
+
return {
|
|
541
|
+
refresh () {
|
|
542
|
+
post({
|
|
543
|
+
type: 'refresh'
|
|
544
|
+
});
|
|
545
|
+
},
|
|
546
|
+
dispose () {
|
|
547
|
+
if (disposed) return;
|
|
548
|
+
disposed = true;
|
|
549
|
+
hostWindow.removeEventListener('message', ready);
|
|
550
|
+
channel.port1.close();
|
|
551
|
+
channel.port2.close();
|
|
552
|
+
observe(options.onBusy, false);
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
}
|
|
556
|
+
/** Installs a document-wide policy once. Use a dedicated host: all URL-based child frames are denied; srcdoc works. */ function installArtifactDocumentFramePolicy(hostDocument) {
|
|
557
|
+
if (hostDocument.querySelector('meta[data-xpert-document-policy]')) return;
|
|
558
|
+
const meta = hostDocument.createElement('meta');
|
|
559
|
+
meta.httpEquiv = 'Content-Security-Policy';
|
|
560
|
+
meta.content = "frame-src 'none'";
|
|
561
|
+
meta.dataset['xpertDocumentPolicy'] = '1';
|
|
562
|
+
hostDocument.head.prepend(meta);
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
exports.ARTIFACT_DOCUMENT_CHANNEL = ARTIFACT_DOCUMENT_CHANNEL;
|
|
566
|
+
exports.ARTIFACT_DOCUMENT_CSP = ARTIFACT_DOCUMENT_CSP;
|
|
567
|
+
exports.ARTIFACT_DOCUMENT_VERSION = ARTIFACT_DOCUMENT_VERSION;
|
|
568
|
+
exports.artifactDocumentContext = artifactDocumentContext;
|
|
569
|
+
exports.artifactDocumentControls = artifactDocumentControls;
|
|
570
|
+
exports.artifactDocumentDependencyAssets = artifactDocumentDependencyAssets;
|
|
571
|
+
exports.artifactDocumentDependencyLock = artifactDocumentDependencyLock;
|
|
572
|
+
exports.artifactDocumentDependencyLocks = artifactDocumentDependencyLocks;
|
|
573
|
+
exports.artifactDocumentHtml = artifactDocumentHtml;
|
|
574
|
+
exports.artifactDocumentId = artifactDocumentId;
|
|
575
|
+
exports.artifactDocumentQuery = artifactDocumentQuery;
|
|
576
|
+
exports.artifactDocumentValue = artifactDocumentValue;
|
|
577
|
+
exports.installArtifactDocumentFramePolicy = installArtifactDocumentFramePolicy;
|
|
578
|
+
exports.mountArtifactDocument = mountArtifactDocument;
|
|
579
|
+
exports.renderArtifactDocument = renderArtifactDocument;
|
|
580
|
+
exports.renderArtifactDocumentDependencies = renderArtifactDocumentDependencies;
|
|
581
|
+
exports.verifyArtifactDocumentDependencies = verifyArtifactDocumentDependencies;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./src/artifact-document";
|