Local AI
Local AI is in early access. Request access.
Puck’s Local AI Toolkit gives AI agents the tools they need to read and edit Puck pages directly in the browser or on the server.
Use your own AI model & infrastructure
The AI Toolkit is model, infrastructure, and framework-agnostic.
Local AI vs Puck AI
Puck AI is provided as part of Puck Cloud. Local AI is designed to work entirely in your environment, and supports most Puck AI features.
Install
Install the toolkit and the AI SDK:
npm i @puckeditor/ai-toolkit @puckeditor/ai-toolkit-ai-sdk ai @ai-sdk/react @ai-sdk/openai --saveCreate the agent
Pass your config to toolDefinitions() to give the agent tools for your components, then stream its responses from an API route.
import { openai } from "@ai-sdk/openai";
import type { Config } from "@puckeditor/core";
import { toolDefinitions } from "@puckeditor/ai-toolkit-ai-sdk";
import { createAgentUIStreamResponse, ToolLoopAgent, type UIMessage } from "ai";
export async function POST(req: Request) {
const { messages, config }: { messages: UIMessage[]; config: Config } =
await req.json();
const agent = new ToolLoopAgent({
model: openai("gpt-5.6-luna"), // Any AI SDK model
instructions: "You are an assistant that builds pages for Paws & Co.",
// Tools to read and change the page
tools: toolDefinitions({ config }),
});
return createAgentUIStreamResponse({ agent, uiMessages: messages });
}Run tools in the editor
Chat with the agent using the AI SDK’s useChat(). When the agent calls a tool, run it against the editor with getAiToolkit() and return the output.
import { useState } from "react";
import { useChat } from "@ai-sdk/react";
import {
DefaultChatTransport,
lastAssistantMessageIsCompleteWithToolCalls,
} from "ai";
import { useGetPuck } from "@puckeditor/core";
import { getAiToolkit } from "@puckeditor/ai-toolkit";
import { config } from "./puck.config";
export function ChatPlugin() {
const getPuck = useGetPuck();
const [prompt, setPrompt] = useState("");
const { messages, sendMessage, addToolOutput } = useChat({
transport: new DefaultChatTransport({ api: "/api/chat", body: { config } }),
// Send tool outputs back to the agent
sendAutomaticallyWhen: lastAssistantMessageIsCompleteWithToolCalls,
async onToolCall({ toolCall }) {
const { toolName, input, toolCallId } = toolCall;
// Apply the tool call to the page
const result = await getAiToolkit(getPuck).streamTool({
toolCallId,
toolName,
input,
hasFinished: true,
});
addToolOutput({ tool: toolName, toolCallId, output: result.output });
},
});
return (
<div>
{messages.map((message) =>
message.parts.map((part, i) =>
part.type === "text" ? <p key={i}>{part.text}</p> : null
)
)}
<form
onSubmit={(e) => {
e.preventDefault();
sendMessage({ text: prompt });
setPrompt("");
}}
>
<input value={prompt} onChange={(e) => setPrompt(e.target.value)} />
<button>Send</button>
</form>
</div>
);
}Add the chat to Puck
Render the chat as a plugin, so useGetPuck() can reach the editor.
import { Puck } from "@puckeditor/core";
import { ChatPlugin } from "./ChatPlugin";
export function Editor() {
return (
<Puck
config={config}
data={data}
plugins={[{ name: "chat", label: "Chat", render: ChatPlugin }]}
/>
);
}