@intentic/sandbox-contract 1.412.1 → 1.414.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/dist/contracts/ci.contract.d.ts +5 -0
- package/dist/contracts/ci.contract.d.ts.map +1 -1
- package/dist/contracts/pages.contract.d.ts +13 -0
- package/dist/contracts/pages.contract.d.ts.map +1 -1
- package/dist/contracts/pages.contract.js +19 -0
- package/dist/contracts/pages.contract.js.map +1 -1
- package/dist/contracts/system.contract.d.ts +3 -0
- package/dist/contracts/system.contract.d.ts.map +1 -1
- package/dist/events/transcript.d.ts.map +1 -1
- package/dist/events/transcript.js.map +1 -1
- package/dist/ids/conversation-ids.d.ts +4 -1
- package/dist/ids/conversation-ids.d.ts.map +1 -1
- package/dist/ids/conversation-ids.js +24 -8
- package/dist/ids/conversation-ids.js.map +1 -1
- package/dist/protocol/doors/webext-links.d.ts +7 -0
- package/dist/protocol/doors/webext-links.d.ts.map +1 -1
- package/dist/protocol/doors/webext-links.js +7 -0
- package/dist/protocol/doors/webext-links.js.map +1 -1
- package/dist/protocol/raw/raw-routes.d.ts +15 -0
- package/dist/protocol/raw/raw-routes.d.ts.map +1 -1
- package/dist/protocol/raw/raw-routes.js +3 -0
- package/dist/protocol/raw/raw-routes.js.map +1 -1
- package/dist/schemas/ci.d.ts +12 -0
- package/dist/schemas/ci.d.ts.map +1 -1
- package/dist/schemas/ci.js +10 -0
- package/dist/schemas/ci.js.map +1 -1
- package/dist/schemas/metrics.d.ts +8 -0
- package/dist/schemas/metrics.d.ts.map +1 -1
- package/dist/schemas/metrics.js +1 -0
- package/dist/schemas/metrics.js.map +1 -1
- package/dist/schemas/webext.d.ts +19 -0
- package/dist/schemas/webext.d.ts.map +1 -1
- package/dist/schemas/webext.js +6 -0
- package/dist/schemas/webext.js.map +1 -1
- package/dist/state/fix-stance.d.ts.map +1 -1
- package/dist/state/fix-stance.js +1 -1
- package/dist/state/fix-stance.js.map +1 -1
- package/dist/text/land-rows.d.ts.map +1 -1
- package/dist/text/land-rows.js +5 -2
- package/dist/text/land-rows.js.map +1 -1
- package/dist/text/pages.d.ts +21 -0
- package/dist/text/pages.d.ts.map +1 -1
- package/dist/text/pages.js +34 -3
- package/dist/text/pages.js.map +1 -1
- package/package.json +5 -5
- package/src/contracts/pages.contract.ts +27 -2
- package/src/events/transcript.ts +2 -1
- package/src/ids/conversation-ids.test.ts +50 -17
- package/src/ids/conversation-ids.ts +42 -15
- package/src/protocol/doors/webext-links.ts +25 -0
- package/src/protocol/ingress-contract.fixture.json +3 -0
- package/src/protocol/raw/raw-routes.ts +7 -0
- package/src/schemas/ci.ts +16 -0
- package/src/schemas/metrics.ts +3 -0
- package/src/schemas/webext.ts +16 -0
- package/src/state/fix-stance.ts +3 -2
- package/src/text/land-rows.ts +6 -3
- package/src/text/pages.test.ts +58 -0
- package/src/text/pages.ts +96 -21
- package/src/text/transcript-fold.test.ts +0 -2
package/src/text/pages.ts
CHANGED
|
@@ -3,28 +3,37 @@
|
|
|
3
3
|
// own frame (the editor's chat/transcript/pages). A page runs its scripts in a frame with an opaque origin and no
|
|
4
4
|
// network: the policy below is the whole of what it may load, and the bootstrap is the whole of what it may say.
|
|
5
5
|
import { parseJsonOr } from "@intentic/base/json";
|
|
6
|
+
import { z } from "zod";
|
|
6
7
|
|
|
7
8
|
// Everything a sealed page may load is already inside it: its own inline scripts and styles, and pictures, fonts and
|
|
8
|
-
// media carried in as data. Nothing reaches the network, another frame, or a form target.
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
9
|
+
// media carried in as data. Nothing reaches the network, another frame, or a form target. The one exception is the
|
|
10
|
+
// video and audio files the page named on disk when it was shown (PAGE_MEDIA below): too large to carry, they stream
|
|
11
|
+
// from the sandbox, and the chat opens `media-src` to exactly those addresses, one by one, for that page alone.
|
|
12
|
+
export const sealedPagePolicy = (media: readonly string[] = []): string =>
|
|
13
|
+
[
|
|
14
|
+
`default-src 'none'`,
|
|
15
|
+
// Inline only, which is every script a sealed document can hold. Eval gives nothing more to a frame that can
|
|
16
|
+
// reach nothing.
|
|
17
|
+
`script-src 'unsafe-inline' 'unsafe-eval'`,
|
|
18
|
+
`style-src 'unsafe-inline'`,
|
|
19
|
+
`img-src data: blob:`,
|
|
20
|
+
`font-src data:`,
|
|
21
|
+
// An address that could break out of its source expression (a space, `;` or `,`) is left out rather than let
|
|
22
|
+
// it widen the policy.
|
|
23
|
+
[`media-src data: blob:`, ...media.filter((address) => /^https?:\/\/[^\s;,']+$/i.test(address))].join(` `),
|
|
24
|
+
// Reading back what the page already holds: `fetch()` on a data: or blob: URL (audio to decode, a file it
|
|
25
|
+
// built), which reaches no network. Every other address stays refused, the page's own media included.
|
|
26
|
+
`connect-src data: blob:`,
|
|
27
|
+
`frame-src 'none'`,
|
|
28
|
+
`worker-src 'none'`,
|
|
29
|
+
`object-src 'none'`,
|
|
30
|
+
`form-action 'none'`,
|
|
31
|
+
`base-uri 'none'`,
|
|
32
|
+
`manifest-src 'none'`,
|
|
33
|
+
].join(`; `);
|
|
34
|
+
|
|
35
|
+
// The seal of a page that names no media: every page drawn anywhere but the chat's own frame, and the headless check.
|
|
36
|
+
export const SEALED_PAGE_POLICY = sealedPagePolicy();
|
|
28
37
|
|
|
29
38
|
// The bridge between a page and the chat around it speaks the MCP Apps extension's own JSON-RPC over postMessage
|
|
30
39
|
// (io.modelcontextprotocol/ui), so one host serves both the agent's pages and an MCP server's app. `submit` is this
|
|
@@ -45,6 +54,9 @@ export const PAGE_BRIDGE = {
|
|
|
45
54
|
// An uncaught error in the page's own scripts, told as it happens: the chat hands it to the agent that showed the
|
|
46
55
|
// page while its turn still runs, and offers the reader the ask afterwards.
|
|
47
56
|
error: `intentic/page-error`,
|
|
57
|
+
// A video or audio element of the page's own failed to load: the chat checks whether the page's media ticket is
|
|
58
|
+
// still good, since the sealed frame cannot see why a load failed and the chat can.
|
|
59
|
+
mediaError: `intentic/media-error`,
|
|
48
60
|
} as const;
|
|
49
61
|
|
|
50
62
|
// How many distinct errors a page tells, and how long each may be: enough to fix it by, never a flood.
|
|
@@ -210,6 +222,7 @@ function fault(t){var at=Date.now();setTimeout(function(){var seen=[];for(var i=
|
|
|
210
222
|
function said(x,fallback){return x&&typeof x.stack==="string"?x.stack.split("\\n").slice(0,2).map(function(l){return l.trim();}).join(" "):fallback;}
|
|
211
223
|
window.addEventListener("error",function(e){if(e instanceof ErrorEvent)fault(said(e.error,(e.message||"Script error")+(e.lineno?" (line "+e.lineno+")":"")));});
|
|
212
224
|
window.addEventListener("unhandledrejection",function(e){fault("Unhandled rejection: "+said(e.reason,String(e.reason)));});
|
|
225
|
+
var mediaTold=false;window.addEventListener("error",function(e){var t=e.target;if(mediaTold||!t||!t.tagName||!/^(VIDEO|AUDIO|SOURCE)$/.test(t.tagName))return;var s=String(t.currentSrc||t.src||"");if(/^(data|blob):/i.test(s)||s==="")return;mediaTold=true;post({jsonrpc:"2.0",method:${JSON.stringify(PAGE_BRIDGE.mediaError)},params:{}});},true);
|
|
213
226
|
function request(method,params){return new Promise(function(resolve,reject){if(!framed){reject(new Error("This page is not inside a chat."));return;}var id="intentic-"+(++n);pending[id]={resolve:resolve,reject:reject};post({jsonrpc:"2.0",id:id,method:method,params:params});});}
|
|
214
227
|
window.addEventListener("message",function(e){if(e.source!==window.parent)return;var d=e.data;if(!d||d.jsonrpc!=="2.0")return;if(d.method===${JSON.stringify(PAGE_BRIDGE.hostContextChanged)}&&d.params){apply({appearance:d.params.theme,variables:d.params.styles&&d.params.styles.variables});return;}if(d.id!==undefined&&pending[d.id]){var p=pending[d.id];delete pending[d.id];if(d.error)p.reject(new Error(String(d.error.message||"Refused")));else p.resolve(d.result);}});
|
|
215
228
|
if(framed){var h=-1,z=function(){var r=document.documentElement,b=document.body,v=Math.ceil(Math.max(r.scrollHeight>r.clientHeight?r.scrollHeight:r.getBoundingClientRect().height,b?b.getBoundingClientRect().bottom:0));if(v===h||v<=0)return;h=v;post({jsonrpc:"2.0",method:${JSON.stringify(PAGE_BRIDGE.sizeChanged)},params:{height:v}});};if(window.ResizeObserver){var o=new ResizeObserver(z);o.observe(document.documentElement);document.addEventListener("DOMContentLoaded",function(){if(document.body)o.observe(document.body);z();});}window.addEventListener("load",z);setTimeout(z,50);}
|
|
@@ -320,7 +333,7 @@ const SIZE_SCRIPT =
|
|
|
320
333
|
`window.parent.postMessage({jsonrpc:"2.0",method:${JSON.stringify(PAGE_BRIDGE.sizeChanged)},params:{height:v}},"*");}` +
|
|
321
334
|
`if(window.ResizeObserver)new ResizeObserver(z).observe(document.documentElement);window.addEventListener("load",z);setTimeout(z,50);})();`;
|
|
322
335
|
export const publishedPage = (stored: string): string =>
|
|
323
|
-
intoHead(stored, `<meta http-equiv="Content-Security-Policy" content="${SEALED_PAGE_POLICY}"><meta name="referrer" content="no-referrer"><script>${SIZE_SCRIPT}</script>`);
|
|
336
|
+
intoHead(withoutPageMedia(stored), `<meta http-equiv="Content-Security-Policy" content="${SEALED_PAGE_POLICY}"><meta name="referrer" content="no-referrer"><script>${SIZE_SCRIPT}</script>`);
|
|
324
337
|
|
|
325
338
|
// What an MCP server's app is shown with (agent/pages/mcp-apps.ts): the call's arguments and its result, kept inside the
|
|
326
339
|
// stored page as a data block the chat hands the app over the bridge (`tool-input`, `tool-result`) once it is ready.
|
|
@@ -346,5 +359,67 @@ export const readMcpAppData = (html: string): McpAppData | undefined => {
|
|
|
346
359
|
return typeof data?.server === `string` && typeof data.tool === `string` ? { server: data.server, tool: data.tool, input: data.input, result: data.result } : undefined;
|
|
347
360
|
};
|
|
348
361
|
|
|
362
|
+
// The video and audio files a page named on disk (agent/pages/page-assets.ts), far too large to carry inside it as
|
|
363
|
+
// pictures are: when the page is shown, each file's reference becomes `intentic-media:<n>` and the page carries a
|
|
364
|
+
// manifest of what <n> was at that moment (its real path, type, size and modification time), signed under a key only the
|
|
365
|
+
// daemon holds, over the page's own stored path and those files, so a manifest written into the records by anyone else,
|
|
366
|
+
// or moved to another page, is never served (agent/pages/page-media.ts). The chat swaps each
|
|
367
|
+
// reference for an address under a ticket it mints for that one page (the daemon's GET /pages/media/{ticket}/{n}),
|
|
368
|
+
// which streams the file in byte ranges only while it is still exactly what was shown. Anywhere else (the headless
|
|
369
|
+
// check, a download, a published share) the reference loads nothing and the player stays empty.
|
|
370
|
+
export const PAGE_MEDIA_REF = `intentic-media:`;
|
|
371
|
+
export const PAGE_MEDIA_ID = `intentic-page-media`;
|
|
372
|
+
// How many media files one page may name: a comparison of takes, not a library.
|
|
373
|
+
export const PAGE_MEDIA_MAX = 32;
|
|
374
|
+
export interface PageMediaFile {
|
|
375
|
+
// The file's real path in the sandbox, links resolved.
|
|
376
|
+
readonly path: string;
|
|
377
|
+
// The type it is served as, from its name, its bytes having said the same.
|
|
378
|
+
readonly type: string;
|
|
379
|
+
readonly size: number;
|
|
380
|
+
readonly mtimeMs: number;
|
|
381
|
+
}
|
|
382
|
+
// Every reference in a page's text, with the manifest index it names.
|
|
383
|
+
export const PAGE_MEDIA_REFS = /intentic-media:(\d{1,3})/g;
|
|
384
|
+
const PAGE_MEDIA_OPEN = `<script type="application/json" id="${PAGE_MEDIA_ID}">`;
|
|
385
|
+
const PAGE_MEDIA_BLOCK = new RegExp(`${PAGE_MEDIA_OPEN.replace(/[.*+?^${}()|[\]\\]/g, `\\$&`)}([\\s\\S]*?)</script>`);
|
|
386
|
+
export interface PageMediaManifest {
|
|
387
|
+
readonly files: readonly PageMediaFile[];
|
|
388
|
+
// The daemon's HMAC over `pageMediaSigned(page, files)`, base64url.
|
|
389
|
+
readonly signature: string;
|
|
390
|
+
}
|
|
391
|
+
// What a manifest's signature covers: the page it belongs to, by its stored path, and each file's every field, in a
|
|
392
|
+
// fixed order, so no spelling of the same JSON can sign differently and a manifest copied into another page fails.
|
|
393
|
+
export const pageMediaSigned = (page: string, files: readonly PageMediaFile[]): string =>
|
|
394
|
+
JSON.stringify([page, files.map((file) => [file.path, file.type, file.size, file.mtimeMs])]);
|
|
395
|
+
export const pageMediaScript = (manifest: PageMediaManifest): string =>
|
|
396
|
+
`${PAGE_MEDIA_OPEN}${JSON.stringify({ files: manifest.files, signature: manifest.signature }).replace(/</g, `\\u003c`)}</script>`;
|
|
397
|
+
// A page's text with no manifest: what the agent wrote can never pose as the one the daemon writes, and a page leaving
|
|
398
|
+
// the sandbox (a published share) does not carry the sandbox's paths with it. Each whole block goes, then any opening
|
|
399
|
+
// left over, repeated until none is left, so removing one cannot join the text around it into another.
|
|
400
|
+
export const withoutPageMedia = (html: string): string => {
|
|
401
|
+
let out = html.replace(new RegExp(PAGE_MEDIA_BLOCK.source, `g`), ``);
|
|
402
|
+
while (out.includes(PAGE_MEDIA_OPEN)) {
|
|
403
|
+
out = out.split(PAGE_MEDIA_OPEN).join(``);
|
|
404
|
+
}
|
|
405
|
+
return out;
|
|
406
|
+
};
|
|
407
|
+
const PageMediaManifestSchema = z.object({
|
|
408
|
+
files: z
|
|
409
|
+
.array(z.object({ path: z.string().min(1), type: z.string().min(1), size: z.number().nonnegative(), mtimeMs: z.number() }))
|
|
410
|
+
.min(1)
|
|
411
|
+
.max(PAGE_MEDIA_MAX),
|
|
412
|
+
signature: z.string().min(1),
|
|
413
|
+
});
|
|
414
|
+
// A stored page's manifest as written, or undefined for a page that names no media (or a block that does not parse). Not
|
|
415
|
+
// yet trusted: only the daemon can say whether its signature holds.
|
|
416
|
+
export const readPageMedia = (html: string): PageMediaManifest | undefined => {
|
|
417
|
+
const match = PAGE_MEDIA_BLOCK.exec(html);
|
|
418
|
+
return match === null ? undefined : PageMediaManifestSchema.safeParse(parseJsonOr(match[1] ?? ``, undefined)).data;
|
|
419
|
+
};
|
|
420
|
+
// A page's media references swapped for what `address` answers for each index; one past the manifest is left as it is.
|
|
421
|
+
export const placePageMedia = (html: string, count: number, address: (index: number) => string): string =>
|
|
422
|
+
html.replace(PAGE_MEDIA_REFS, (whole, index: string) => (Number(index) < count ? address(Number(index)) : whole));
|
|
423
|
+
|
|
349
424
|
// A page's answer as the agent is handed it: JSON text, whatever the page sent, refused past the cap rather than cut.
|
|
350
425
|
export const pageValueText = (value: unknown): string => JSON.stringify(value === undefined ? null : value) ?? `null`;
|
|
@@ -503,7 +503,6 @@ describe("foldTurn", () => {
|
|
|
503
503
|
expect(foldOf("go", [{ kind: "landed", landed: true, deps: { missing: 3, started: [], deferred: true } }]).at(-1)).toEqual({
|
|
504
504
|
role: "notice",
|
|
505
505
|
text: "Changes landed in your workspace: review them in the Changes panel. 3 dependencies it added or changed are being installed in your tree: the install waits for any other install to finish, appears in Work terminals, and its outcome lands in Activity.",
|
|
506
|
-
noticeAction: "landHold",
|
|
507
506
|
noticeCode: { code: "landed", params: { deps: 3, queued: true } },
|
|
508
507
|
});
|
|
509
508
|
});
|
|
@@ -1200,7 +1199,6 @@ describe(`the code beside a notice's words`, () => {
|
|
|
1200
1199
|
expect(lastOf([{ kind: `landed`, landed: true }])).toEqual({
|
|
1201
1200
|
role: `notice`,
|
|
1202
1201
|
text: `Changes landed in your workspace: review them in the Changes panel.`,
|
|
1203
|
-
noticeAction: `landHold`,
|
|
1204
1202
|
noticeCode: { code: `landed` },
|
|
1205
1203
|
});
|
|
1206
1204
|
expect(lastOf([{ kind: `landed`, landed: true, deps: { missing: 2, started: [], deferred: true } }])?.noticeCode).toEqual({
|