Skip to content

Native AI Support

AI features bring more than a request to a model: conversation state, streamed replies, cancellation, retries, tools, and the UI around them. Sygnal provides those pieces through the same drivers, behaviors, actions, and state you use for the rest of an app.

Take the counter from the homepage and add a conversation. The green lines are additions; the counter’s existing intent and reducers keep working. The chat behavior owns message history, prompt input, streaming, Stop, errors, and Retry. You write the view.

This example runs here with a scripted reply, so you can try it without a model or a key. The opening question is sent by the line marked “Demo only.” Send another message, press Stop during a reply, or edit the component.

The demo model: a small streaming server route

This route plays the model for this page. It always sends the same reply; it does not reason about your question or the count. The docs runtime registers the real chat driver against it, using the Open Responses transport shown below.

Demo server
import { encodeOpenResponses } from 'sygnal/ai'
export default {
'POST /v1/responses': () => ({
sse: encodeOpenResponses([
'Try the counter buttons. ',
'The conversation has its own state, ',
'and the counter keeps working. ',
'This reply is scripted; connect a model to answer your questions.',
]),
delayMs: 200,
chunkMs: 250,
}),
}
Counter.jsx
import { chat, messageText } from 'sygnal/ai'
function Counter({ state }) {
const c = state.chat
const busy = c.status === 'submitted' || c.status === 'streaming'
return (
<div>
<p>Count: {state.count}</p>
<button className="increment">+1</button>
<button className="reset">Reset</button>
<ol aria-live="polite">
{c.messages.map((m) => <li key={m.id}>{m.role}: {messageText(m)}</li>)}
{c.draft && <li>assistant: {c.draft}</li>}
</ol>
<form className="ask">
<label>Message <input className="prompt" value={c.prompt} /></label>
<button type="submit" disabled={busy}>Send</button>
<button type="button" className="stop" disabled={!busy}>Stop</button>
</form>
{c.error && <p role="alert">{c.error} <button className="retry">Retry</button></p>}
</div>
)
}
Counter.initialState = { count: 0 }
Counter.intent = ({ DOM }) => ({
INCREMENT: DOM.click('.increment'),
RESET: DOM.click('.reset'),
})
Counter.model = {
INCREMENT: (state) => ({ ...state, count: state.count + 1 }),
RESET: (state) => ({ ...state, count: 0 }),
// Demo only: send an opening question when this example mounts.
BOOTSTRAP: { EFFECT: (state, data, next) => next('chat.SEND', 'What can I try here?') },
}
Counter.uses = {
chat: chat({ agent: false, form: '.ask', prompt: '.prompt', stop: '.stop', regenerate: '.retry' }),
}
export default Counter

The counter still owns state.count. The behavior adds state.chat and namespaced actions such as chat.SEND, without new chat reducers in your component. The selectors connect the form and buttons to that behavior. With agent: false, this is plain chat: the model cannot read or operate the counter.

On the root component, add the driver and choose a transport. Here it connects to a local Ollama model; the highlighted lines are the new driver setup. Pull the model with ollama pull llama3.2 first (local setup).

Counter.jsx
import { makeChatDriver, openResponses } from 'sygnal/ai'
Counter.drivers = {
LLM: makeChatDriver({
transport: openResponses({
baseURL: 'http://localhost:11434/v1', model: 'llama3.2',
}),
}),
}

For a hosted provider, use a server route that keeps the API key off the page. Authentication, rate limits, conversation storage, and your product’s UI are still application work. The small addition here covers the client conversation lifecycle, rather than an entire production chat service.

To let a model use the app, declare the state it can read and the actions it may call. The agent example builds on this counter; the same declaration can serve an in-app assistant, WebMCP, and tests. You choose where to stop: a plain chat needs none of those tools.

Some ideas run through every part of sygnal/ai:

  • Pay per use. An app that never imports sygnal/ai carries none of it, and each transport and behavior is left out unless you import it.
  • The model chooses; the app does. A model picks an action, a part, an answer. It never writes your markup, your state paths or your action names, so every label and button stays the app’s own.
  • Nothing is privileged. A tool call runs the same reducer as a click, with the same validation. Actions you don’t declare simply don’t exist as far as a model is concerned.
  • Keys stay on the server. Production requests go through your own server route; in development, a local model needs no key at all.
  • Testable like everything else. Fake replies, scripted streams and tool calls run in renderComponent() with no network.

If a coding agent is writing your app, start with Why Sygnal if AI already knows React? for the tradeoffs, then Built for Coding Agents for the tooling. The features on this page are for the app you ship; you can use that tooling without adding AI to your product.

If you want to see a model stream into a component in five minutes, start with AI Chat: it runs against a local model with no account and no key. If you’re more curious about the agent side, the Agents guide shows how one small declaration makes an app operable by a model. And if you just want something to copy, the AI recipes build complete features: a triaged support inbox, a form filled from pasted text, on-device summaries.

The API is collected in the sygnal/ai reference.