Skip to content

Open-source platform for 3D AI agents. Turn text or a photo into a rigged, animated GLB avatar, give it an LLM brain, memory and a wallet, and embed it anywhere with one web component. Remote MCP server, x402 payments, Three.js, browser-native.

feat: implement Brain Studio mount logic and resize handling

nirholasa02f3238697f
diff --git a/public/studio.css b/public/studio.css
deleted file mode 100644
index 4f9301805..000000000
--- a/public/studio.css
+++ /dev/null
@@ -1,303 +0,0 @@
-/* Agent Studio — shell + Brain Studio (P0 shell · P1 brain)
- * Uses the platform design tokens (public/tokens.css). Dark-first; light theme
- * inherits via the token overrides. */
-
-.studio {
- --studio-stage-w: clamp(320px, 34vw, 560px);
- --node-persona: #8b8bff;
- --node-model: #4fd1c5;
- --node-memory: #f6ad55;
- --node-skill: #b794f4;
- --node-market: #68d391;
- --node-output: #fc8181;
- position: relative;
- display: grid;
- grid-template-columns: var(--studio-stage-w) 1fr;
- gap: var(--space-sm);
- height: calc(100vh - var(--nav-h, 56px));
- padding: var(--space-sm);
- box-sizing: border-box;
- background: var(--bg-0);
- color: var(--ink);
-}
-.studio--stage-collapsed { grid-template-columns: 64px 1fr; }
-
-/* ── Stage ─────────────────────────────────────────────────────────────── */
-.studio__stage {
- position: relative;
- border-radius: var(--radius-lg);
- background:
- radial-gradient(120% 80% at 50% 0%, rgba(139, 139, 255, 0.10), transparent 60%),
- var(--surface-glass);
- border: 1px solid var(--surface-2);
- overflow: hidden;
- min-height: 0;
-}
-.studio__stage-frame { position: absolute; inset: 0; }
-.studio__stage-frame agent-presence { display: block; width: 100%; height: 100%; }
-.studio__stage-meta {
- position: absolute; left: var(--space-md); bottom: var(--space-md);
- pointer-events: none; text-shadow: 0 1px 8px rgba(0, 0, 0, 0.6);
-}
-.studio__stage-name { font-size: var(--text-lg); font-weight: var(--weight-semibold); }
-.studio__stage-sub { font-size: var(--text-sm); color: var(--ink-dim); }
-.studio__stage-collapse {
- position: absolute; top: var(--space-sm); right: var(--space-sm);
- width: 28px; height: 28px; border-radius: var(--radius-pill);
- border: 1px solid var(--surface-3); background: var(--surface-1); color: var(--ink);
- cursor: pointer; display: grid; place-items: center; transition: background .15s, transform .15s;
-}
-.studio__stage-collapse:hover { background: var(--surface-3); }
-.studio--stage-collapsed .studio__stage-meta { display: none; }
-.studio--stage-collapsed .studio__stage-collapse { transform: rotate(180deg); }
-
-/* ── Panel + tabs ──────────────────────────────────────────────────────── */
-.studio__panel { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
-.studio__tabs {
- display: flex; align-items: center; gap: var(--space-3xs);
- padding: var(--space-3xs); margin-bottom: var(--space-sm);
- background: var(--surface-1); border: 1px solid var(--surface-2); border-radius: var(--radius-pill);
-}
-.studio__tab {
- display: inline-flex; align-items: center; gap: var(--space-2xs);
- padding: var(--space-2xs) var(--space-md); border: 0; border-radius: var(--radius-pill);
- background: transparent; color: var(--ink-dim); font-size: var(--text-ui); font-weight: var(--weight-medium);
- cursor: pointer; transition: background .15s, color .15s; white-space: nowrap;
-}
-.studio__tab:hover { color: var(--ink); }
-.studio__tab.is-active { background: var(--surface-3); color: var(--ink-bright); }
-.studio__tab:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
-.studio__tab-key {
- display: inline-grid; place-items: center; width: 16px; height: 16px;
- font-size: var(--text-2xs); color: var(--ink-faint); border: 1px solid var(--surface-3); border-radius: 4px;
-}
-.studio__tab.is-active .studio__tab-key { color: var(--ink); }
-.studio__save { margin-left: auto; padding-right: var(--space-sm); font-size: var(--text-sm); color: var(--ink-dim); min-height: 1em; }
-.studio__save.is-saving { color: var(--node-memory); }
-.studio__save.is-saved { color: var(--node-market); }
-.studio__save.is-error { color: var(--node-output); }
-
-.studio__tabpanels { flex: 1; min-height: 0; position: relative; }
-.studio__tabpanel { position: absolute; inset: 0; overflow: auto; }
-.studio__tabpanel:not(.is-active) { display: none; }
-.studio__tabpanel:focus-visible { outline: 2px solid var(--accent); outline-offset: -2px; }
-
-/* ── Placeholder tabs (P2–P5 land here) ────────────────────────────────── */
-.studio__placeholder {
- height: 100%; display: grid; place-content: center; justify-items: center; text-align: center;
- gap: var(--space-sm); padding: var(--space-xl); color: var(--ink-dim);
-}
-.studio__placeholder-glyph {
- width: 56px; height: 56px; border-radius: var(--radius-lg);
- background: var(--surface-2); border: 1px dashed var(--surface-3);
-}
-.studio__placeholder h2 { margin: 0; color: var(--ink); font-size: var(--text-xl); }
-.studio__placeholder p { margin: 0; max-width: 42ch; }
-
-/* ── Buttons ───────────────────────────────────────────────────────────── */
-.studio__btn {
- display: inline-flex; align-items: center; gap: var(--space-2xs);
- padding: var(--space-2xs) var(--space-md); border-radius: var(--radius-md);
- border: 1px solid var(--surface-3); background: var(--surface-1); color: var(--ink);
- font-size: var(--text-sm); font-weight: var(--weight-medium); cursor: pointer; text-decoration: none;
- transition: background .15s, border-color .15s, transform .1s;
-}
-.studio__btn:hover { background: var(--surface-3); }
-.studio__btn:active { transform: translateY(1px); }
-.studio__btn:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
-.studio__btn-primary { background: var(--accent); color: var(--bg-0); border-color: var(--accent); }
-.studio__btn-primary:hover { filter: brightness(1.08); background: var(--accent); }
-.studio__btn:disabled { opacity: .5; cursor: not-allowed; }
-
-/* ── Overlays (loading / onboard / error) ──────────────────────────────── */
-.studio__overlay { position: absolute; inset: 0; z-index: 20; display: grid; place-items: center; background: var(--bg-0); }
-.studio[data-state="ready"] .studio__overlay { display: none; }
-.studio__cta { text-align: center; max-width: 44ch; padding: var(--space-xl); }
-.studio__cta h1 { font-size: var(--text-xl); margin: 0 0 var(--space-sm); }
-.studio__cta p { color: var(--ink-dim); margin: 0 0 var(--space-lg); }
-.studio__skeleton { display: grid; grid-template-columns: var(--studio-stage-w) 1fr; gap: var(--space-sm); width: 100%; height: 100%; padding: var(--space-sm); box-sizing: border-box; }
-.studio__skeleton-stage, .sk-bar, .sk-block { background: linear-gradient(90deg, var(--surface-1), var(--surface-3), var(--surface-1)); background-size: 200% 100%; animation: sk 1.4s linear infinite; border-radius: var(--radius-lg); }
-.studio__skeleton-panel { display: flex; flex-direction: column; gap: var(--space-sm); }
-.sk-bar { height: 44px; border-radius: var(--radius-pill); }
-.sk-block { flex: 1; }
-@keyframes sk { from { background-position: 200% 0; } to { background-position: -200% 0; } }
-
-/* ════════════════════════ Brain Studio ════════════════════════ */
-.brainstudio { height: 100%; display: flex; flex-direction: column; min-height: 0; }
-.brainstudio__editor { display: flex; flex-direction: column; height: 100%; min-height: 0; }
-
-/* Onboarding */
-.brainstudio__onboard { height: 100%; display: grid; place-items: center; padding: var(--space-lg); }
-.brainstudio__onboard-inner { max-width: 720px; text-align: center; }
-.brainstudio__onboard h1 { font-size: var(--text-2xl); margin: 0 0 var(--space-xs); }
-.brainstudio__onboard p { color: var(--ink-dim); margin: 0 0 var(--space-lg); }
-.brainstudio__templates { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: var(--space-sm); margin-bottom: var(--space-lg); }
-.brainstudio__tpl {
- text-align: left; padding: var(--space-md); border-radius: var(--radius-lg); cursor: pointer;
- border: 1px solid var(--surface-3); background: var(--surface-1); color: var(--ink);
- display: flex; flex-direction: column; gap: var(--space-2xs); transition: transform .12s, border-color .15s, background .15s;
-}
-.brainstudio__tpl:hover { transform: translateY(-2px); background: var(--surface-2); }
-.brainstudio__tpl::before { content: ''; width: 26px; height: 4px; border-radius: 2px; background: var(--accent); }
-.brainstudio__tpl--persona::before { background: var(--node-persona); }
-.brainstudio__tpl--model::before { background: var(--node-model); }
-.brainstudio__tpl--memory::before { background: var(--node-memory); }
-.brainstudio__tpl--market::before { background: var(--node-market); }
-.brainstudio__tpl-name { font-weight: var(--weight-semibold); }
-.brainstudio__tpl-tag { font-size: var(--text-sm); color: var(--ink-dim); }
-
-/* Toolbar */
-.brainstudio__toolbar { display: flex; align-items: center; gap: var(--space-sm); margin-bottom: var(--space-sm); flex-wrap: wrap; }
-.brainstudio__palette { display: flex; gap: var(--space-2xs); flex-wrap: wrap; }
-.brainstudio__tools { margin-left: auto; display: flex; gap: var(--space-2xs); }
-.brainstudio__chip {
- padding: var(--space-3xs) var(--space-sm); border-radius: var(--radius-pill); cursor: pointer;
- border: 1px solid var(--surface-3); background: var(--surface-1); color: var(--ink-dim);
- font-size: var(--text-sm); font-weight: var(--weight-medium); transition: color .15s, border-color .15s, background .15s;
-}
-.brainstudio__chip:hover { color: var(--ink); background: var(--surface-2); }
-.brainstudio__chip--persona:hover { border-color: var(--node-persona); }
-.brainstudio__chip--model:hover { border-color: var(--node-model); }
-.brainstudio__chip--memory:hover { border-color: var(--node-memory); }
-.brainstudio__chip--skill:hover { border-color: var(--node-skill); }
-.brainstudio__chip--market:hover { border-color: var(--node-market); }
-.brainstudio__chip--output:hover { border-color: var(--node-output); }
-
-/* Main split: canvas + side */
-.brainstudio__main { flex: 1; display: grid; grid-template-columns: 1fr minmax(280px, 360px); gap: var(--space-sm); min-height: 0; }
-.brainstudio__canvas {
- position: relative; overflow: hidden; border-radius: var(--radius-lg);
- border: 1px solid var(--surface-2); background-color: var(--bg-1);
- background-image: radial-gradient(var(--surface-3) 1px, transparent 1px);
- background-size: 22px 22px; touch-action: none; cursor: grab;
-}
-.brainstudio__canvas.is-panning { cursor: grabbing; }
-.brainstudio__side { display: flex; flex-direction: column; gap: var(--space-sm); min-height: 0; }
-
-/* ── Graph (BrainGraphView) ─────────────────────────────────────────────── */
-.bgraph { position: absolute; inset: 0; overflow: hidden; }
-.bgraph__world { position: absolute; inset: 0; transform-origin: 0 0; }
-.bgraph__edges { position: absolute; left: 0; top: 0; width: 8000px; height: 8000px; overflow: visible; pointer-events: none; }
-.bgraph__nodes { position: absolute; inset: 0; }
-.bgraph__hint { position: absolute; left: 50%; bottom: 10px; transform: translateX(-50%); font-size: var(--text-sm); color: var(--ink-dim); background: var(--surface-1); padding: 2px 10px; border-radius: var(--radius-pill); pointer-events: none; }
-
-.bedge { fill: none; stroke: var(--surface-3); stroke-width: 2; transition: stroke .2s; }
-.bedge-hit { fill: none; stroke: transparent; stroke-width: 14; pointer-events: stroke; cursor: pointer; }
-.bedge.is-active { stroke: var(--accent); stroke-width: 2.5; filter: drop-shadow(0 0 4px var(--accent)); }
-.bedge--ghost { stroke: var(--accent); stroke-dasharray: 5 4; opacity: .7; }
-.bedge.is-flow { stroke-dasharray: 7 6; animation: bflow .6s linear infinite; }
-@keyframes bflow { to { stroke-dashoffset: -26; } }
-
-.bnode {
- position: absolute; left: 0; top: 0; box-sizing: border-box;
- border-radius: var(--radius-md); border: 1px solid var(--surface-3);
- background: var(--surface-glass); backdrop-filter: blur(8px);
- box-shadow: 0 6px 24px rgba(0, 0, 0, 0.35); user-select: none;
- transition: box-shadow .15s, border-color .15s;
-}
-.bnode.is-selected { border-color: var(--accent); box-shadow: 0 0 0 1px var(--accent), 0 8px 28px rgba(0, 0, 0, 0.45); }
-.bnode.is-active { border-color: var(--accent); }
-.bnode.is-pulse { animation: bpulse .7s ease-out; }
-@keyframes bpulse { 0% { box-shadow: 0 0 0 0 var(--accent); } 100% { box-shadow: 0 0 0 14px transparent; } }
-.bnode.is-busy .bnode__head::after { content: ''; width: 8px; height: 8px; border-radius: 50%; background: var(--node-memory); margin-left: auto; animation: bbusy .8s ease-in-out infinite; }
-@keyframes bbusy { 0%, 100% { opacity: .3; } 50% { opacity: 1; } }
-
-.bnode__head { display: flex; align-items: center; gap: var(--space-2xs); height: 34px; padding: 0 var(--space-sm); border-bottom: 1px solid var(--surface-2); cursor: grab; border-radius: var(--radius-md) var(--radius-md) 0 0; }
-.bnode__head::before { content: ''; width: 8px; height: 8px; border-radius: 2px; background: var(--accent); }
-.bnode--persona .bnode__head::before { background: var(--node-persona); }
-.bnode--model .bnode__head::before { background: var(--node-model); }
-.bnode--memory .bnode__head::before { background: var(--node-memory); }
-.bnode--skill .bnode__head::before { background: var(--node-skill); }
-.bnode--market .bnode__head::before { background: var(--node-market); }
-.bnode--output .bnode__head::before { background: var(--node-output); }
-.bnode__title { font-size: var(--text-sm); font-weight: var(--weight-semibold); }
-.bnode__del { margin-left: auto; border: 0; background: transparent; color: var(--ink-faint); cursor: pointer; font-size: var(--text-base); line-height: 1; padding: 2px 4px; border-radius: 4px; }
-.bnode__del:hover { color: var(--node-output); background: var(--surface-2); }
-.bnode__summary { padding: var(--space-2xs) var(--space-sm); font-size: var(--text-sm); color: var(--ink-dim); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
-.bnode__ports { display: flex; justify-content: space-between; padding-bottom: var(--space-2xs); }
-.bnode__col { display: flex; flex-direction: column; gap: 6px; }
-.bport { display: flex; align-items: center; gap: 5px; height: 20px; font-size: var(--text-2xs); color: var(--ink-faint); cursor: crosshair; padding: 0 var(--space-2xs); }
-.bport--out { flex-direction: row-reverse; }
-.bport__dot { width: 11px; height: 11px; border-radius: 50%; background: var(--bg-1); border: 2px solid var(--ink-dim); transition: transform .1s, border-color .15s; }
-.bport:hover .bport__dot { transform: scale(1.25); }
-.bport--signal .bport__dot { border-color: var(--node-persona); }
-.bport--context .bport__dot { border-color: var(--node-memory); }
-.bport--tool .bport__dot { border-color: var(--node-skill); }
-.bnode__stat { padding: 2px var(--space-sm) var(--space-2xs); font-size: var(--text-2xs); color: var(--node-model); }
-
-/* ── Inspector ──────────────────────────────────────────────────────────── */
-.brainstudio__inspector { background: var(--surface-1); border: 1px solid var(--surface-2); border-radius: var(--radius-lg); padding: var(--space-md); overflow: auto; max-height: 48%; }
-.brainstudio__inspector-empty h3, .brainstudio__insp-head h3 { margin: 0 0 var(--space-2xs); font-size: var(--text-md); }
-.brainstudio__inspector-empty p { margin: 0; color: var(--ink-dim); font-size: var(--text-sm); }
-.brainstudio__insp-head { display: flex; align-items: center; gap: var(--space-2xs); margin-bottom: var(--space-sm); }
-.brainstudio__insp-dot { width: 10px; height: 10px; border-radius: 3px; background: var(--accent); }
-.brainstudio__insp-dot--persona { background: var(--node-persona); }
-.brainstudio__insp-dot--model { background: var(--node-model); }
-.brainstudio__insp-dot--memory { background: var(--node-memory); }
-.brainstudio__insp-dot--skill { background: var(--node-skill); }
-.brainstudio__insp-dot--market { background: var(--node-market); }
-.brainstudio__insp-dot--output { background: var(--node-output); }
-.brainstudio__fields { display: flex; flex-direction: column; gap: var(--space-sm); }
-.brainstudio__field { display: flex; flex-direction: column; gap: var(--space-3xs); }
-.brainstudio__field-label { font-size: var(--text-sm); color: var(--ink-dim); }
-.brainstudio__input { width: 100%; box-sizing: border-box; padding: var(--space-2xs) var(--space-sm); background: var(--bg-1); color: var(--ink); border: 1px solid var(--surface-3); border-radius: var(--radius-md); font-size: var(--text-sm); font-family: inherit; }
-.brainstudio__input:focus { outline: none; border-color: var(--accent); }
-textarea.brainstudio__input { resize: vertical; }
-.brainstudio__toggle { position: relative; display: inline-block; width: 38px; height: 22px; }
-.brainstudio__toggle input { opacity: 0; width: 0; height: 0; }
-.brainstudio__toggle span { position: absolute; inset: 0; background: var(--surface-3); border-radius: var(--radius-pill); transition: background .15s; cursor: pointer; }
-.brainstudio__toggle span::before { content: ''; position: absolute; width: 16px; height: 16px; left: 3px; top: 3px; background: var(--ink); border-radius: 50%; transition: transform .15s; }
-.brainstudio__toggle input:checked + span { background: var(--accent); }
-.brainstudio__toggle input:checked + span::before { transform: translateX(16px); background: var(--bg-0); }
-.brainstudio__toggle input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }
-
-/* ── Test chat ──────────────────────────────────────────────────────────── */
-.brainstudio__test { flex: 1; display: flex; flex-direction: column; min-height: 0; background: var(--surface-1); border: 1px solid var(--surface-2); border-radius: var(--radius-lg); overflow: hidden; }
-.brainstudio__test-head { padding: var(--space-2xs) var(--space-md); border-bottom: 1px solid var(--surface-2); font-size: var(--text-sm); font-weight: var(--weight-semibold); }
-.brainstudio__test-model { color: var(--ink-faint); font-weight: var(--weight-regular); }
-.brainstudio__transcript { flex: 1; overflow: auto; padding: var(--space-sm); display: flex; flex-direction: column; gap: var(--space-sm); }
-.brainstudio__msg { max-width: 90%; padding: var(--space-2xs) var(--space-sm); border-radius: var(--radius-md); font-size: var(--text-sm); white-space: pre-wrap; word-break: break-word; }
-.brainstudio__msg--user { align-self: flex-end; background: var(--accent); color: var(--bg-0); }
-.brainstudio__msg--agent { align-self: flex-start; background: var(--surface-3); color: var(--ink); }
-.brainstudio__msg.is-error { background: rgba(252, 129, 129, 0.15); color: var(--node-output); }
-.brainstudio__msg-stat { margin-top: 4px; font-size: var(--text-2xs); color: var(--ink-faint); }
-.brainstudio__msg--user .brainstudio__msg-stat { display: none; }
-.brainstudio__compose { display: flex; gap: var(--space-2xs); padding: var(--space-2xs); border-top: 1px solid var(--surface-2); }
-.brainstudio__compose textarea { flex: 1; resize: none; padding: var(--space-2xs) var(--space-sm); background: var(--bg-1); color: var(--ink); border: 1px solid var(--surface-3); border-radius: var(--radius-md); font-family: inherit; font-size: var(--text-sm); }
-.brainstudio__compose textarea:focus { outline: none; border-color: var(--accent); }
-
-/* ── Modal (templates + behavior diff) ──────────────────────────────────── */
-.brainstudio__modal { position: absolute; inset: 0; z-index: 30; display: grid; place-items: center; background: rgba(0, 0, 0, 0.55); padding: var(--space-md); }
-.brainstudio__modal-card { width: min(720px, 100%); max-height: 86%; overflow: auto; background: var(--bg-1); border: 1px solid var(--surface-3); border-radius: var(--radius-lg); padding: var(--space-lg); }
-.brainstudio__modal-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: var(--space-2xs); }
-.brainstudio__modal-head h2 { margin: 0; font-size: var(--text-lg); }
-.brainstudio__modal-x { border: 0; background: transparent; color: var(--ink-dim); font-size: var(--text-xl); cursor: pointer; line-height: 1; }
-.brainstudio__modal-sub { margin: 0 0 var(--space-md); color: var(--ink-dim); font-size: var(--text-sm); }
-.brainstudio__behavior-meta { display: flex; flex-wrap: wrap; gap: var(--space-sm); margin-bottom: var(--space-md); font-size: var(--text-sm); color: var(--ink-dim); }
-.brainstudio__behavior-meta b { color: var(--ink); font-weight: var(--weight-medium); }
-.brainstudio__diff { white-space: pre-wrap; word-break: break-word; background: var(--bg-0); border: 1px solid var(--surface-2); border-radius: var(--radius-md); padding: var(--space-md); font-size: var(--text-sm); line-height: var(--leading-normal); max-height: 50vh; overflow: auto; }
-.diff-add { color: var(--node-market); }
-.diff-del { color: var(--node-output); text-decoration: line-through; opacity: .8; }
-.diff-muted { color: var(--ink-faint); }
-
-/* ── Responsive ─────────────────────────────────────────────────────────── */
-@media (max-width: 1024px) {
- .brainstudio__main { grid-template-columns: 1fr; grid-template-rows: minmax(260px, 1fr) auto; }
- .brainstudio__side { flex-direction: row; }
- .brainstudio__inspector { max-height: none; flex: 1; }
- .brainstudio__test { flex: 1; }
-}
-@media (max-width: 900px) {
- .studio { grid-template-columns: 1fr; grid-template-rows: minmax(160px, 26vh) 1fr; height: auto; min-height: calc(100vh - var(--nav-h, 56px)); }
- .studio__skeleton { grid-template-columns: 1fr; grid-template-rows: 26vh 1fr; }
- .brainstudio__side { flex-direction: column; }
-}
-@media (max-width: 560px) {
- .studio__tab { padding: var(--space-2xs) var(--space-sm); }
- .studio__tab-key { display: none; }
-}
-@media (prefers-reduced-motion: reduce) {
- .bedge.is-flow { animation: none; }
- .bnode.is-pulse { animation: none; }
- .studio__skeleton-stage, .sk-bar, .sk-block { animation: none; }
-}
diff --git a/src/studio/brain/brain-mount.js b/src/studio/brain/brain-mount.js
new file mode 100644
index 000000000..3eef26c88
--- /dev/null
+++ b/src/studio/brain/brain-mount.js
@@ -0,0 +1,39 @@
+/**
+ * Brain Studio — mount bootstrap (P1)
+ * ===================================
+ * The Agent Studio shell (P0) dispatches `studio-shell:ready` once it has loaded
+ * the caller's agent through the shared store and rendered the tab panels. Each
+ * sub-studio binds itself to its panel then. This module is P1's binding: it
+ * mounts the Brain Studio into the shell's `[data-studio-mount="brain"]` panel.
+ *
+ * The agent-studio page loads this module alongside the shell:
+ * <script type="module" src="/src/studio/brain/brain-mount.js"></script>
+ *
+ * It is idempotent and order-independent — it handles both "event fires after we
+ * subscribe" and "shell was already ready before this module evaluated".
+ */
+
+import { studio } from '../agent-studio-store.js';
+import { mountBrainStudio } from './brain-studio.js';
+
+const SELECTOR = '[data-studio-mount="brain"]';
+
+function mount() {
+ const panel = document.querySelector(SELECTOR);
+ if (!panel || panel.dataset.brainMounted) return false;
+ mountBrainStudio(panel, { studio });
+ return true;
+}
+
+// Primary path: the shell tells us it's ready (store loaded, panels rendered).
+document.addEventListener('studio-shell:ready', mount, { once: false });
+
+// Fallback: the panel may already exist (event missed due to script ordering).
+// Poll briefly for the panel; stop as soon as we mount or the shell renders a
+// non-shell state (gate/error) where the panel never appears.
+if (!mount()) {
+ let tries = 0;
+ const t = setInterval(() => {
+ if (mount() || ++tries > 60) clearInterval(t); // ~6s ceiling
+ }, 100);
+}
diff --git a/src/studio/brain/brain-studio.js b/src/studio/brain/brain-studio.js
index 35f8138a2..674d44eb4 100644
--- a/src/studio/brain/brain-studio.js
+++ b/src/studio/brain/brain-studio.js
@@ -150,10 +150,20 @@ class BrainStudio {
this.el.querySelector('#bsOnboard').hidden = true;
this.el.querySelector('#bsEditor').hidden = false;
if (!this.graph) {
- this.graph = new BrainGraphView(this.el.querySelector('#bsCanvas'), {
+ const canvas = this.el.querySelector('#bsCanvas');
+ this.graph = new BrainGraphView(canvas, {
onChange: (g) => this._onGraphChange(g),
onSelect: (n) => this._renderInspector(n),
});
+ // The Brain tab may be mounted while hidden (zero size), so the initial
+ // fit() no-ops. Re-frame once the panel first gains a real size.
+ if (typeof ResizeObserver !== 'undefined') {
+ let framed = false;
+ const ro = new ResizeObserver(() => {
+ if (!framed && canvas.clientWidth > 0) { framed = true; this.graph.fit(); ro.disconnect(); }
+ });
+ ro.observe(canvas);
+ }
this.runtime = new BrainRuntime({
graphView: this.graph,
studio: this.studio,