Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 88 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,98 @@
# AI Chat
# Saucebase AI Chat

A streaming AI chat for Saucebase apps, at `/chat`.
<div align="center">

- Conversations per user, titled automatically, with a session list.
- Tools: web search (the provider's own), image generation, and `about_my_account`, which reads the signed-in user's profile, plan and workspace from the installed modules. It is the example to copy for your own tools.
- Monthly limits per plan (`ai_chat_messages`, `ai_chat_images`) with the Billing module.
- Admin: extra instructions, the models users can pick, and a read-only conversation viewer.
- WebMCP: the visitor's own browser agent can ask the assistant and read or switch chats.
[![Tests](https://github.com/saucebase-dev/ai-chat/actions/workflows/test.yml/badge.svg)](https://github.com/saucebase-dev/ai-chat/actions/workflows/test.yml)
[![Release](https://img.shields.io/github/v/release/saucebase-dev/ai-chat)](https://github.com/saucebase-dev/ai-chat/releases)
[![Saucebase Core](https://img.shields.io/packagist/dependency-v/saucebase/ai-chat/saucebase%2Fcore?label=Saucebase%20Core&color=FF6B35)](https://github.com/saucebase-dev/core)

</div>

A streaming AI chat for Saucebase apps, at `/chat`, built on [laravel/ai](https://github.com/laravel/ai).

A module for [Saucebase](https://github.com/saucebase-dev/saucebase), the AI-ready Laravel SaaS kit. Modules are Composer packages you add to a Saucebase app; once installed, the code is yours to change.

## Features

- **Streaming replies** in markdown, with the steps that led to them: thinking, tool calls and sources
- **Conversations** per user: titled automatically, renamed, pinned, deleted and searched, grouped by date; long chats load older messages as you scroll up
- **Tools**: web search (the provider's own), image generation, and `about_my_account`, which reads the signed-in user's profile, plan and workspace from the installed modules
- **Model picker** with the models you offer
- **Monthly limits per plan** for messages and images, with the [Billing](https://github.com/saucebase-dev/billing) module: a usage bar near the limit and an upgrade link
- **Dictation** in the message box
- **Admin panel**: provider API keys, the models users can pick, extra instructions, image settings, and a read-only conversation viewer
- **Test mode**: a demo provider answers, so you can build and try the chat without a key or tokens
- **WebMCP**: the visitor's own browser agent can ask the assistant and read or switch chats
- **Two layouts**: a "Chat" item in the app sidebar and the chats in a panel of their own, or the chats in the app sidebar for a chat-first app
- **Vue** (React to come)

## Install

AI Chat is a private module: your app needs access to the Saucebase package registry. <!-- TODO: how to get access, as for tenancy -->

**New to Saucebase?** Start a new app first:

```bash
curl -fsSL https://install.saucebase.dev | bash
```

It installs PHP and Composer if they're missing. With the CLI already installed, run `saucebase new my-app` instead.

**With your Saucebase app ready**, add AI Chat:

```bash
php artisan saucebase:module-install ai-chat
npm install && npm run build
```

Set a provider key in `.env` (see `config/ai.php` from laravel/ai). Without one, `AI_CHAT_TEST_MODE=true` answers through a demo provider (the replies in `resources/demos/`, offered as models in the picker) so the chat can be built and tried for free.
It needs [Auth](https://github.com/saucebase-dev/auth) for sign-in. Conversation titles are written by a queued job, so run a queue worker.

## Configuration

In the admin panel, under **Settings → AI chat** (people with the `manage ai chat` permission, and admins):

- **API keys** for each provider, stored encrypted. A key set in `.env` (laravel/ai's `config/ai.php`) works too, and the admin shows which keys come from there.
- **Models**: the ones users can pick, and the default. Only providers with a key are offered.
- **Instructions** added to the assistant's own.
- **Images**: the provider, model and quality `generate_image` uses. `.env` can set them too (`AI_CHAT_IMAGE_PROVIDER`, `AI_CHAT_IMAGE_MODEL`, `AI_CHAT_IMAGE_QUALITY`); without either, laravel/ai's default for images.

The conversation viewer is under **Chats**.

Where the chat list lives is `sessions` in `config/ai-chat.php` (publish the config, or set it from your own service provider):

| Value | Layout |
|---|---|
| `page` (default) | A "Chat" item in the app sidebar. On the chat, the app sidebar starts collapsed to its icons, and the chats, search and WebMCP sit in a resizable panel of their own (a sheet on phones). |
| `sidebar` | The chats and WebMCP in the app sidebar on every page, with no item and no panel. For an app where the chat is the product. |

## Limits

With Billing installed, a plan can set two limits per month, as capabilities on its product:

- `ai_chat_messages`: messages a user can send
- `ai_chat_images`: images the assistant can generate for them

A limit a user's plans don't set is unlimited. At the limit, sending is turned off until the next month, with a link to upgrade.

## Test mode

```dotenv
AI_CHAT_TEST_MODE=true
```

Every provider call goes to a demo provider: the picker offers the demos (`Demo: Steps`, `Demo: Image`…), and titles and images use it too. The real agent, tools, storage and streaming all run. Each demo is a JSON file in `resources/demos/`; adding one is adding a file. Test mode is refused in production.

## Add a tool

1. A class in `src/Ai/Tools/` implementing `Laravel\Ai\Contracts\Tool`, with a `NAME` constant. Take the user in the constructor and return a string (JSON for structured results). `AboutMyAccount` is the example to copy.
2. Return it from `ChatAgent::tools()`.
3. Give it labels in `TOOL_LABELS` (`resources/js/lib/tools.ts`), or the steps show its name.
4. Optionally, a demo in `resources/demos/` so test mode can show it.

## Tests

```bash
php artisan test --compact modules/ai-chat/tests
npx vitest run modules/ai-chat
npx playwright test modules/ai-chat/tests/e2e --project="@ai-chat*"
```