Who answers
See which AIs this visitor has, route a request, and handle the case where none can answer.
import { NoProviderError } from "@leuria/client"
const state = ai.getState()
for (const provider of state.providers) print(`${provider.label}: ${provider.status}`)
print(`A plain request would go to: ${state.active?.label ?? "nobody yet"}`)
try {
// Only the browser's built-in model, whatever else is ready.
const text = await ai.chat({ prompt: "Say hi in three words.", provider: ["browser"], signal }).text()
print(text)
} catch (error) {
if (!(error instanceof NoProviderError)) throw error
for (const reason of error.reasons) print(`${reason.label}: ${reason.reason}`)
if (error.actionable) print(`A click would fix it: ai.connect("${error.actionable}")`)
}
How it works
ai.getState()lists every provider with its status (ready,needs-action,downloading,unavailable…), plusactive(what answers now) andpending(what a click would turn on).ai.subscribe()tells you when it changes.providerpicks providers by id, in order.localOnly: trueskips the ones that run on your side, such asserver().NoProviderErrorsays, per provider, why it couldn't answer.actionablenames one thatai.connect()would fix: call it from a click, since it may open a window. See Providers and the cascade.