Skip to content

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

View as Markdown

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:

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). The complete example below reads it from there.

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

MethodWhat it does
open(), close(), toggle()Expand, collapse or flip the dock. open() also brings back a hidden panel
isOpenWhether 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: 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:

LineWhen
run r_… not in this conversationthe run belongs to another conversation
run r_…: Studio did not answerthe read failed
step n not in run r_… · showing step mthe run has no step n; the last step is carried
select r_…: the panel is not connectedthe 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.

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:

<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.

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

document.addEventListener("weft:parked", (e) => {
const { runId, callId, name } = e.detail
console.log(`${name} (${callId}) is waiting for a decision on ${runId}`)
})
EventdetailWhen
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).

<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:

<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). Its public members, as the npm package types them (WeftDevtoolsElement):

MemberWhat it is
open, close, toggle, isOpen, scope, select, on, studioLinkThe methods above. isOpen is a read-only property
optionsConfiguration rung 1: the options mount() passed, or null
autoMountedtrue 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> — ….

@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).

import { mount, on, open, select, studioLink } from "@weftgo/devtools"
mount({ endpoint: "/studio/" })
const off = on("parked", ({ runId, name }) => console.log(name, "parked on", runId))
ExportDiffers from the element’s method in that
mount(opts?)Creates the panel and returns it; no element method matches it (Install › npm)
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, DevtoolsEventsDevtoolsEvents 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.

A chat page for the devtools example program: 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>:

<!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").

  • 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). (Source: panel/element.ts:1385-1393)

Symbols used: thread.PublicID · thread.Approve · Session.Decide · runtime.Sessions