---
title: Drive the panel from your page (host API and events)
description: Open, scope and select in the devtools panel from your own page with the <weft-devtools> element's methods, window.weft.devtools or the @weftgo/devtools exports, and follow its weft:run, weft:parked and weft:error events.
url: https://weftgo.dev/docs/observe/devtools/host-api/
type: how-to
sources:
  - 'W11-factcheck4, v0.20.2: weft/studio/web/src/panel/element.ts:659 (scopeNames: a run alone names nothing), 666-687 (scopeValue: the object form reads the same fields as the string), 1328-1339 (the refusal line); weft/studio/web/src/lib/scope.ts:6 (the string form)'
  - 'weft/studio/README.md:1456-1470 (the host API: the element''s methods, one implementation; the method table)'
  - weft/studio/README.md:1472-1480 (a session-only scope resolved through GET /api/sessions/{id}/public_id; the lines it says)
  - 'weft/studio/README.md:1482-1494 (the events: CustomEvents, bubbles, composed, a microtask; history; the detail table)'
  - 'weft/studio/README.md:1496-1507 (the global: window.weft created when absent, the footer lines, removed on disconnect, data-global=off, studio-local''s page)'
  - 'weft/AGENTS.md:355-356 (block 7: open/close/toggle/isOpen/scope/select/on/studioLink, window.weft.devtools unless data-global=off, events weft:run|parked|error)'
  - weft/CHANGELOG.md:2166 (the host API, plan C4.2)
  - 'weft/studio/web/src/panel/element.ts:532-569 (DevtoolsEvents), 571-576 (DEVTOOLS_EVENTS), 578-604 (DevtoolsAPI), 609-622 (API_OBJECTS), 709-729 (scopeValue: sessionId/runId accepted), 739-753 (WeftDevtools, observedAttributes), 1077-1087 (data-open read on first connect), 866-872 (autoMounted), 872-874 (options), 1252-1296 (isOpen getter, open, close), 1309-1357 (scope), 1363-1409 (select and its lines), 2283-2292 (on), 2292-2293 (studioLink), 1413-1432 (api: one frozen object), 1505-1548 (syncGlobal and its footer lines), 1472-1498 (dropGlobal), 791-791 (watchRuns: the baseline rule, run and error), 791-798 (reportParked), 748-761 (fire: queueMicrotask, bubbles, composed), 2115-2116 (rescan), 2497-2504 (toggle)'
  - 'weft/studio/web/src/npm/index.ts:93-122 (WeftDevtoolsElement), 129-152 (DevtoolsEvents), 261 (mount), 351-377 (scope), 386-391 (select), 395-397 (isOpen: a function), 403-407 (studioLink), 409-439 (open, close: a later panel starts that way; toggle), 447-461 (on: on document, no-op on a server)'
  - weft/studio/web/npm/README.md:99 (tokens), 103-126 (API table; no global; the scope subpath; calls while the page parses)
  - weft/studio/web/src/panel/api.test.ts (the host API), panel.test.ts
  - weft/studio/web/src/panel/state.ts:2055-2068 (a fetchMeta failure sets gone; 401/403 walled, never removed — v0.20.2); weft/studio/web/src/panel/element.ts:2276-2290 (the self-mounted dock removed, a mount the host made shows the not-reachable line)
  - 'observed: Chromium (Playwright 1.62.0) against a scratch app embedding weft studio v0.17.0 (WEFT_DISCOVERY=off), runtime.Sessions on: Object.keys(window.weft.devtools) is open, close, toggle, isOpen, scope, select, on, studioLink and isOpen is a boolean; a page listener on document heard nothing for the history read, then run {status: parked, step: 0} and parked {callId, ackId, name: refund} for a new turn, run succeeded for its resume, run running then succeeded for a tool turn; studioLink(run, 1) returned <endpoint>runs/<id>?step=1&view=story; the complete example below, with /chat replaced by /ask, selected the turn and opened the panel, counted one parked call in the output, and its report link opened the run''s Studio page'
---

# Drive the panel from your page (host API and events)

The `<weft-devtools>` element has methods your page can call: `open()`, `close()`, `toggle()`, `isOpen`, `scope(s)`, `select(runId, step?)`, `on(event, cb)` and `studioLink(runId, step?)`. Under the script tag the same methods are published as `window.weft.devtools`; from npm, the package's exports call them on every panel on the page. The panel also dispatches `weft:run`, `weft:parked` and `weft:error` events from the element, so a listener on `document` hears them.

A "debug this" button beside a reply, which opens the panel on that reply's turn:

```js
function debugThis(runId) {
  const devtools = window.weft?.devtools
  devtools?.select(runId)
  devtools?.open()
}
```

The run id is the one your chat handler names in its `Weft-Scope` response header ([Scope the panel](https://weftgo.dev/docs/observe/devtools/scope/#the-scope-header)). The complete example below reads it from there.

## Methods

The methods are the element's own, one implementation for both installs. None of them throws.

| Method | What it does |
|---|---|
| `open()`, `close()`, `toggle()` | Expand, collapse or flip the dock. `open()` also brings back a hidden panel |
| `isOpen` | Whether the dock is expanded. A property on the element and on `window.weft.devtools`; from npm, a function, `isOpen()` |
| `scope(s)` | Sets the explicit scope, [rung 1](https://weftgo.dev/docs/observe/devtools/scope/#rung-1-explicit): it wins over the URL, markers and headers, and its run pins. `s` is a `Scope` or its string form, `"pub_…;session=…;flow=…;run=…;agent=…;tag.<k>=<v>"`; both forms take the same scopes: a public id, a session, an agent, a flow or a tag. `scope(null)` clears what `scope()` set and puts back what `mount()` named |
| `select(runId, step?)` | Selects that turn, and the step by its ordinal; `⤢` then carries the step |
| `on(event, cb)` | Calls `cb(detail)` for each `weft:<event>` this panel dispatches, and returns the unsubscribe. A `cb` that throws is swallowed |
| `studioLink(runId, step?)` | The Studio page of that run (and step), the link `⤢` carries, never with a token. `""` before the panel knows its endpoint |

`scope()` reads `sessionId` and `runId` as `session` and `run`, so a `run` event's `detail` can be fed straight back. A scope that names none of a public id, a session, an agent, a flow or a tag (a `run` alone included) reads `scope: no public id, session, agent, flow or tag`; an empty string, or `{publicId: ""}`, is the newest-runs list. A scope with a session and no public id (`";session=s_…"`) is resolved through `GET /api/sessions/{id}/public_id`, which only setup A and the dev token may ask. Under a panel token the panel does not ask and says `session s_…: the session lookup needs the dev token`. A session created without `thread.PublicID` reads `session s_… has no public id · not recorded …`; an unknown one, `… · unknown session`. Either way the panel keeps its scope.

`select()` waits for a `scope()` called just before it to settle. A run the turn list does not show is read by id (`GET /api/runs/{id}`, scoped by the token) and joins the list when it belongs to the conversation. Otherwise the panel says one line instead of throwing:

| Line | When |
|---|---|
| `run r_… not in this conversation` | the run belongs to another conversation |
| `run r_…: Studio did not answer` | the read failed |
| `step n not in run r_… · showing step m` | the run has no step `n`; the last step is carried |
| `select r_…: the panel is not connected` | the panel's start hung for 10 s |

`studioLink()` builds the same URL as `⤢`, for a "report this run" link in your own UI. For example, `studioLink(id, 1)` returns `http://127.0.0.1:8080/studio/runs/<id>?step=1&view=story`.

## The global

While a `<weft-devtools>` loaded by the script tag is connected, `window.weft.devtools` is its API: one frozen object with the eight members above. It is the only global the panel adds.

- `window.weft` is created only when it is absent. An existing plain object gets the one `devtools` property.
- Anything else is left alone, and the panel's footer says so: `window.weft is the page's: no window.weft.devtools`, or `window.weft.devtools is the page's: not replaced` when your page already has a `devtools`.
- With two panels on the page, the first to connect keeps the name; the other takes over when it disconnects.
- On disconnect the property is deleted, and so is `window.weft` when the panel created it and nothing else was put in it.

Keep it off with `data-global="off"` (or `"false"`) on the tag or element, or with a meta tag:

```html
<meta name="weft:global" content="off">
```

The element's own methods still work: `document.querySelector("weft-devtools").open()`. The npm package never adds the global; its exports are the API.

## Events

The panel dispatches three `CustomEvent`s from the element, with `bubbles` and `composed` set, after its own work (a microtask). A listener on the element or on `document` hears them:

```js
document.addEventListener("weft:parked", (e) => {
  const { runId, callId, name } = e.detail
  console.log(`${name} (${callId}) is waiting for a decision on ${runId}`)
})
```

| Event | `detail` | When |
|---|---|---|
| `weft:run` | `{runId, status, publicId?, sessionId?, step?}` | A run starts (`status` `"running"`), and on each status change after it, once per transition. `status` is `running`, `succeeded`, `failed`, `parked` (succeeded with calls awaiting a decision) or `interrupted`. `step` is the run's last step ordinal the panel knows |
| `weft:parked` | `{runId, callId, ackId, name}` | Once per call a run parked on, read from the run's pending list. `ackId` is the id the approval names, so it equals `callId` |
| `weft:error` | `{message, runId}` | A run of the conversation failed: its error, once per run. Run errors only; the panel's own trouble (Studio not answering) is a line in the panel, not an event |

Only runs of the conversation the panel follows are reported; in the fallback, with no scope, there are none. Only transitions the panel sees are reported: the first read of a conversation's turn list is history and fires nothing, except a run that reads running there and the run the scope pins.

`ackId` is what a decision takes. Your app decides its own turns through its `Session`, `s.Decide(ctx, thread.Approve(ackId))`, or lets the panel decide them through `runtime.Sessions` ([Approve and steer your app's own turns](https://weftgo.dev/docs/observe/devtools/app-turns/)).

## The element

`<weft-devtools>` is the panel. The script tag defines it and mounts one by itself, unless your markup has one; you can also write it into your page:

```html
<weft-devtools data-position="right-dock" data-scope="pub_demo"></weft-devtools>
```

It reads its configuration from its own `data-*` attributes and applies changes to them live, except `data-mode` and `data-open`, which are read once ([Install](https://weftgo.dev/docs/observe/devtools/install/#where-configuration-comes-from)). Its public members, as the npm package types them (`WeftDevtoolsElement`):

| Member | What it is |
|---|---|
| `open`, `close`, `toggle`, `isOpen`, `scope`, `select`, `on`, `studioLink` | The methods above. `isOpen` is a read-only property |
| `options` | Configuration rung 1: the options `mount()` passed, or `null` |
| `autoMounted` | `true` only on the dock the bundle mounted by itself, the one node the panel may remove: when no Studio answers `/api/meta`, unless the endpoint was named and is on loopback |
| `rescan()` | Re-reads the configuration ladder and applies it, as an attribute change does. The script tag calls it when `window.__WEFT__` changes |
| `syncGlobal()` | Publishes or removes `window.weft.devtools` as the configuration says |

A panel you wrote yourself stays when no Studio answers and shows `Studio not reachable at <endpoint> · retry`; only an `autoMounted` dock removes itself. A 401 or 403 removes no dock: the line reads `Studio needs a token at <endpoint> — …`.

## npm exports

`@weftgo/devtools` exports the same methods as functions that act on every `<weft-devtools>` on the page. The package is built from weft's repository, not installed from the registry ([Install › npm](https://weftgo.dev/docs/observe/devtools/install/#npm)).

```ts

mount({ endpoint: "/studio/" })
const off = on("parked", ({ runId, name }) => console.log(name, "parked on", runId))
```

| Export | Differs from the element's method in that |
|---|---|
| `mount(opts?)` | Creates the panel and returns it; no element method matches it ([Install › npm](https://weftgo.dev/docs/observe/devtools/install/#import-it)) |
| `open()`, `close()` | Act on every panel; a panel mounted later starts open or closed accordingly |
| `toggle()` | Flips every panel there now |
| `isOpen()` | A function: whether any panel on the page is expanded (`false` with none) |
| `scope(s)` | Points every panel at `s` and writes it into each element's `data-weft-scope`; a panel mounted later starts in it. `scope(null)` clears it on every panel |
| `select(runId, step?)` | Selects in every panel |
| `studioLink(runId, step?)` | Asks the first panel on the page; `""` with none |
| `on(event, cb)` | Listens on `document`, so it hears every panel, one mounted later included. `event` is `"run"`, `"parked"` or `"error"`, without the `weft:` prefix |
| `serializeScope(s)`, `parseScope(str)` | The scope's string form; also alone in `@weftgo/devtools/scope` |
| types `Scope`, `MountOptions`, `PanelOptions`, `Position`, `Mode`, `WeftDevtoolsElement`, `DevtoolsEvents` | `DevtoolsEvents` types each event's `detail` |

Calls made while the page is still parsing take effect once the dock is mounted. On a server every export is a no-op: `mount()` returns `null`, `on()` returns a no-op unsubscribe, `isOpen()` is `false` and `studioLink()` is `""`. The package adds no global.

## Complete example

A chat page for the [devtools example program](https://weftgo.dev/docs/observe/devtools/install/#complete-example): each reply gets "debug this" and "report this run" links built from the `Weft-Scope` header of its `/chat` response, and parked calls are counted in an `<output>`:

```html
<!doctype html>
<html lang="en">
<head><meta charset="utf-8"><title>Order help</title></head>
<body>
<h1>Order help</h1>
<p>parked calls: <output id="parked">0</output></p>
<form id="ask"><input name="text" value="Where is order 1234?" size="40"> <button>Ask</button></form>
<ol id="answers"></ol>
<script>
// The run id the chat handler named in its Weft-Scope header.
function runOf(res) {
  const field = (res.headers.get("Weft-Scope") || "").split(";").find((f) => f.startsWith("run="))
  return field ? decodeURIComponent(field.slice(4)) : ""
}

document.getElementById("ask").addEventListener("submit", async (e) => {
  e.preventDefault()
  const res = await fetch("/chat?c=pub_demo", { method: "POST", body: new FormData(e.target).get("text") })
  const runId = runOf(res)
  const li = document.createElement("li")
  li.append(await res.text(), " ")
  const debug = document.createElement("button")
  debug.textContent = "debug this"
  debug.onclick = () => { window.weft?.devtools?.select(runId); window.weft?.devtools?.open() }
  const report = document.createElement("a")
  report.textContent = "report this run"
  report.target = "_blank"
  report.href = window.weft?.devtools?.studioLink(runId) || "#"
  li.append(debug, " ", report)
  document.getElementById("answers").append(li)
})

document.addEventListener("weft:parked", () => {
  const out = document.getElementById("parked")
  out.textContent = String(Number(out.textContent) + 1)
})
</script>
<script type="module" src="/studio/panel.js" data-weft data-scope="pub_demo"></script>
</body>
</html>
```

The listener on `document` needs no global: the events reach it whether or not `window.weft.devtools` exists. weft's own `examples/studio-local` page does the same with "debug this", "report this run" and `on("parked")`.

## Best practices & gotchas

- **Call through `window.weft?.devtools?.`.** The global exists only while a script-tag panel is connected, and never under `data-global="off"` or npm; optional chaining keeps your page working without the panel. (Source: `studio/README.md:1496-1503`)
- **Read `isOpen` as a property on the element, `isOpen()` as a function from npm.** Calling the property throws a `TypeError` in your code, not the panel's. (Source: `panel/element.ts:1202`; `npm/index.ts:395-397`)
- **Expect no events for history.** The first read of a conversation is a baseline; a parked turn already in the list fires `weft:parked` only if it is the run the scope pins. (Source: `studio/README.md:1484-1488`; `panel/element.ts:1450-1509`)
- **Listen on `document` for one handler across panels.** The events bubble and are composed, so they leave the shadow root. (Source: `panel/element.ts:1779-1793`)
- **`studioLink` never carries a token.** Under a panel token the Studio page it opens asks for one; the panel's own `⤢` uses the hand-off ([Secure the devtools panel](https://weftgo.dev/docs/observe/devtools/security/#handoff)). (Source: `panel/element.ts:1385-1393`)

## See also

- [Scope the panel to a conversation](https://weftgo.dev/docs/observe/devtools/scope/)
- [Approve and steer your app's own turns](https://weftgo.dev/docs/observe/devtools/app-turns/)
- [Lay out and theme the panel](https://weftgo.dev/docs/observe/devtools/layout-and-theme/)
- [Install the devtools panel](https://weftgo.dev/docs/observe/devtools/install/)
- [Devtools panel reference](https://weftgo.dev/docs/reference/panel/)

Symbols used: [`thread.PublicID`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#PublicID) · [`thread.Approve`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#Approve) · [`Session.Decide`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/thread#Session.Decide) · [`runtime.Sessions`](https://pkg.go.dev/github.com/weftgo/weft@v0.20.2/runtime#Sessions)
