AI models
Connect the agent chat and AI features to a real model provider, such as Anthropic or OpenAI, through a server route so your API key never reaches the browser.
Where AI is scripted today
| Feature | Where the answer comes from |
|---|---|
| Agent chat (Rune and your agents) | createAssistantReply() in src/lib/chat.ts. Contract summaries, payment reminders and receivables answers are built from your records; other prompts cycle through scripted answers. |
| Task requests in the chat | Handled before the agent: they create a real task in the app |
| Pipeline questions and adding a lead | src/lib/lead-ai.ts |
| People, relationships, overdue and upcoming invoices | src/lib/network-ai.ts |
| Payment reminders and receivables | src/lib/finance-answers.ts |
| Finding, summarising and drafting email | src/lib/email-ai.ts |
| Making a whiteboard from a template | src/lib/board-ai.ts |
| Write with AI in notes | src/lib/note-ai-draft.ts |
| New workflow from a description | src/lib/workflow-draft.ts |
createAssistantReply() checks these in order (boards, email, leads, people and invoices, contracts, reminders, receivables) and falls back to the scripted answers. The chat already shows a thinking stage and types each reply out word by word, so it looks the same once real replies arrive.
Add a server route
Create a Route Handler that holds your API key, calls the model and streams the text back. Install the provider's SDK, put the key in .env.local and choose a model from the provider's docs. Two examples:
Check the session at the top of the route. Without it, anyone who finds the URL can use your API key.
Connect the chat
- In
chat-provider.tsx, the reply is created right away and added after a short pause. Instead, add an empty reply, then call/api/chatwith the conversation so far. - Read the response as it streams and append each piece of text to that reply, so it fills in live.
- Turn off the chat's simulated typing for live replies, since the text now arrives piece by piece.
- Keep the task requests and record-based answers if you like them: only send other prompts to the model. To let the model answer from records itself, pass the relevant ones into the conversation.
- Show a short error in the chat if the request fails, so people can try again.
Write with AI and workflow drafts follow the same pattern: replace the draft function with a call to a server route.
Give the model tools
Each answer file above shows which facts Rune needs for a kind of question. With a real model, turn each one into a tool: the model decides which to call, your route runs it against your database, and the model writes the reply from what comes back.
| Tool | Replaces | Returns or does |
|---|---|---|
find_leads | lead-ai.ts | Open leads filtered by stage, owner, close month or gone quiet |
create_lead | lead-ai.ts | Adds a lead and returns its id |
find_people | network-ai.ts | Decision makers, who knows someone best, relationships going cold |
list_invoices | network-ai.ts, finance-answers.ts | Overdue or upcoming invoices, with the billing contact |
search_emails | email-ai.ts | Threads by sender, company, date or topic |
draft_email | email-ai.ts, finance-answers.ts | A draft that opens in the composer; never sent by the model |
create_board | board-ai.ts | A board from a template, filled in for a customer |
Run the tools on the server with the signed-in person's access, so the model only sees what they could see. Send the ids it used back with the reply in the fields the chat already understands (leads, people, invoices, emails, boards and emailDraft on ChatMessage), and the chat shows the same cards as today.
Keep the demo's rules: when a tool changes something (adding a lead, making a board), the reply shows a card for what it made so the person can check it, and email is only ever drafted. Let the person press Send.
Cost and limits
- Cap reply length with
max_tokens(Anthropic) ormax_output_tokens(OpenAI), and send only as much history as you need. - Limit use per person, for example a number of messages per minute and per day, checked in the route.
- Set a spending limit or alert in your provider's dashboard.
- Handle rate limits. Providers reject requests when you send too many; show a friendly “try again in a moment” message instead of a broken reply.