Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions studio/shell/inference.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ export const prismFrame = (globalThis.__prismFrame ??= () => (globalThis.__prism
// case once, as the workspace opens. Left plain: what the service worker reads (the grants and published
// .holo bytes, public and addressed by their content) and the audit it writes.
const SEALED = new Set(["settings", "apps", "chats", "files", "objects", "refs", "versions"]);
const plainPlace = (store, key) => !SEALED.has(store) || (store === "refs" && /^(capabilities|holo):/.test(key));
// A gallery card is made to be published, and its picture is a Blob, which cannot be read inside a transaction.
const plainPlace = (store, key) => !SEALED.has(store) || (store === "refs" && /^(capabilities|holo|gallery):/.test(key));
const isSealed = (value) => value?.sealedWork instanceof Uint8Array;
const word = (n) => [0, 8, 16, 24].map((shift) => ((n >>> shift) & 255).toString(16).padStart(2, "0")).join("");
export const workspaceRequest = (...words) => "workspace " + [1, ...words].map(word).join("");
Expand All @@ -98,9 +99,20 @@ const refusal = (code) => Object.assign(new Error(code), { code });
// including what was told before it mounted.
export const tell = (answer) => { (globalThis.__workspaceTold ??= []).push(answer); window.dispatchEvent(new CustomEvent("foundry:workspace", { detail: answer })); };
const base64 = (bytes) => { let text = ""; for (let i = 0; i < bytes.length; i += 32768) text += String.fromCharCode(...bytes.subarray(i, i + 32768)); return btoa(text); };
const encode = (value) => enc.encode(JSON.stringify(value, function (name, v) { const raw = this[name]; return raw instanceof Uint8Array ? { $bytes: base64(raw) } : raw instanceof Date ? { $date: v } : v; }));
// Values as JSON, as IndexedDB would keep them: bytes, dates and a key whose value is undefined are tagged, then
// restored by assignment, so the key stays (a reviver returning undefined would drop it).
const encode = (value) => enc.encode(JSON.stringify(value, function (name, v) { const raw = this[name]; return raw instanceof Uint8Array ? { $bytes: base64(raw) } : raw instanceof Date ? { $date: v } : raw === undefined ? { $undefined: 1 } : v; }));
const unbase64 = (text) => Uint8Array.from(atob(text), (c) => c.charCodeAt(0));
const decode = (bytes) => JSON.parse(new TextDecoder().decode(bytes), (name, v) => (v?.$bytes !== undefined ? unbase64(v.$bytes) : v?.$date !== undefined ? new Date(v.$date) : v));
const tagged = (v, tag) => Object.keys(v).length === 1 && tag in v;
const restore = (v) => {
if (!v || typeof v !== "object") return v;
if (tagged(v, "$bytes")) return unbase64(v.$bytes);
if (tagged(v, "$date")) return new Date(v.$date);
if (tagged(v, "$undefined")) return undefined;
for (const k of Object.keys(v)) v[k] = restore(v[k]);
return v;
};
const decode = (bytes) => restore(JSON.parse(new TextDecoder().decode(bytes)));
const wrap = (target, own) => new Proxy(target, { get: (t, p) => (p in own ? own[p] : typeof t[p] === "function" ? t[p].bind(t) : t[p]), set: (t, p, v) => ((t[p] = v), true) });
let workspaceOpen = null;
export const workspace = () => (workspaceOpen ??= openWorkspace());
Expand Down
2 changes: 1 addition & 1 deletion studio/tools/atomic_browser_settings_browser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ try {
const before=await page.evaluate(()=>window.__host.get('settings','user'));
const late=await page.evaluate(async()=>{
const put=IDBObjectStore.prototype.put;let injected=false;
IDBObjectStore.prototype.put=function(value,key){const q=put.call(this,value,key);if(this.name==='settings'&&key==='user'&&value.enableAppBlueprint===true){injected=true;q.addEventListener('success',()=>this.transaction.abort(),{once:true});}return q;};
IDBObjectStore.prototype.put=function(value,key){const q=put.call(this,value,key);if(this.name==='settings'&&key==='user'&&!injected){injected=true;q.addEventListener('success',()=>this.transaction.abort(),{once:true});}return q;};
try{await window.electron.ipcRenderer.invoke('set-user-settings',{enableAppBlueprint:true});return {injected,code:null};}catch(e){return {injected,code:e.code};}finally{IDBObjectStore.prototype.put=put;}
});assert.deepEqual(late,{injected:true,code:'settingsStorageFailed'});assert.deepEqual(await page.evaluate(()=>window.__host.get('settings','user')),before);
await peer.reload();await peer.locator('[role=textbox]').waitFor();assert.equal((await peer.evaluate(()=>window.electron.ipcRenderer.invoke('get-user-settings'))).enableAppBlueprint,false);
Expand Down
2 changes: 1 addition & 1 deletion studio/tools/source_bound_gallery_browser.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ try {
const context=await browser.newContext({serviceWorkers:'block'}),page=await context.newPage(),peer=await context.newPage(),errors=[];
for(const p of [page,peer])p.on('pageerror',e=>errors.push(e.message));
await context.route('**/*',r=>new URL(r.request().url()).origin===origin?r.continue():r.abort());
const initialize=async p=>{await p.goto(base);await p.waitForFunction(()=>!!window.__host&&!!window.electron);await p.evaluate(async base=>{window.gallery=await import(base+'gallery-store.mjs');window.sourceModule=await import(base+'project-edit.mjs');window.database=await (await import(base+'inference.js')).db();},base);};
const initialize=async p=>{await p.goto(base);await p.waitForFunction(()=>!!window.__host&&!!window.electron);await p.evaluate(async base=>{window.gallery=await import(base+'gallery-store.mjs');window.sourceModule=await import(base+'project-edit.mjs');window.database=await (await import(base+'inference.js')).workspace();},base);};
await initialize(page);await initialize(peer);
const call=(channel,input)=>page.evaluate(({channel,input})=>window.electron.ipcRenderer.invoke(channel,input),{channel,input});
const make=async name=>(await call('create-app',{name,initialChatMode:'build'})).app.id;
Expand Down
Loading