diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..cf78a69 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2026 Saucebase + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md index e6997d8..833e7f6 100644 --- a/README.md +++ b/README.md @@ -5,10 +5,11 @@ [![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) +[![License](https://img.shields.io/github/license/saucebase-dev/ai-chat)](LICENSE) -A streaming AI chat for Saucebase apps, at `/chat`, built on [laravel/ai](https://github.com/laravel/ai). +Laravel AI chat for Saucebase: streaming replies, conversation history, web search, image generation and plan limits, built on [laravel/ai](https://github.com/laravel/ai), on Vue or React. It opens at `/chat`. 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. @@ -28,9 +29,7 @@ A module for [Saucebase](https://github.com/saucebase-dev/saucebase), the AI-rea ## Install -AI Chat is a private module: your app needs access to the Saucebase package registry. - -**New to Saucebase?** Start a new app first: +**New to Saucebase?** Start a new app and pick **ai-chat** when it asks for modules: ```bash curl -fsSL https://install.saucebase.dev | bash @@ -96,3 +95,7 @@ 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*" ``` + +## License + +MIT. See [LICENSE](LICENSE). diff --git a/composer.json b/composer.json index 3e70cfb..1a0e0bd 100644 --- a/composer.json +++ b/composer.json @@ -1,8 +1,31 @@ { "name": "saucebase/ai-chat", - "description": "AI chat with streaming replies, web search, image generation, account-aware tools, plan usage limits and WebMCP.", + "description": "Laravel AI chat for your SaaS: streaming replies, conversation history, web search, image generation, tools that read the user's account, monthly limits per plan and a model picker for OpenAI, Anthropic, Gemini and more. Built on laravel/ai, on Vue and React (Inertia).", + "keywords": [ + "saucebase", + "saucebase-module", + "laravel", + "laravel-package", + "filament", + "inertiajs", + "vuejs", + "reactjs", + "laravel-ai", + "ai-chat", + "chatbot", + "ai-assistant", + "llm", + "openai", + "anthropic", + "gemini", + "ollama", + "image-generation", + "web-search", + "webmcp" + ], + "homepage": "https://saucebase-dev.github.io/docs/", "type": "saucebase-module", - "license": "proprietary", + "license": "MIT", "version": "v0.2.0", "authors": [ { diff --git a/resources/boost/skills/saucebase-ai-chat-development/SKILL.md b/resources/boost/skills/saucebase-ai-chat-development/SKILL.md index 586b26d..3dde8c5 100644 --- a/resources/boost/skills/saucebase-ai-chat-development/SKILL.md +++ b/resources/boost/skills/saucebase-ai-chat-development/SKILL.md @@ -101,3 +101,5 @@ npx playwright test modules/ai-chat/tests/e2e --project="@ai-chat*" # E2E ``` E2E specs turn test mode on per test (`laravel.config('ai-chat.test_mode', true)` in `beforeEach`, as themes does), so no key is needed in CI or locally. + +They also run inside the chat template, which puts the list in the app sidebar (`sessions=sidebar`) and seeds a Free plan of 50 messages a month. A spec about the panel sets `laravel.config('ai-chat.sessions', 'page')` too; one that sends many messages makes room with `limitMessages()`, and the limits specs count from the limit it returns, not a fixed number. diff --git a/tests/Support/AiChatTestHelper.php b/tests/Support/AiChatTestHelper.php index c65de3b..48dc7a5 100644 --- a/tests/Support/AiChatTestHelper.php +++ b/tests/Support/AiChatTestHelper.php @@ -81,12 +81,14 @@ public static function conversations(string $email, int $count): array /** * Subscribes the user to a plan with a monthly message limit: theirs alone, so specs * running beside it keep no limit (a key none of a user's plans declares is unlimited). - * False without billing. + * Returns the limit the user ends up with: an app's free plan can allow more (the chat + * template's allows 50), and billing gives the user the more generous of the two. Null + * without billing. */ - public static function limitMessages(string $email, int $limit): bool + public static function limitMessages(string $email, int $limit): ?int { if (! class_exists(Product::class)) { - return false; + return null; } $user = User::where('email', $email)->firstOrFail(); @@ -106,7 +108,7 @@ public static function limitMessages(string $email, int $limit): bool 'price_id' => $price->getKey(), ]); - return true; + return Usage::limit($user->fresh(), Usage::MESSAGES); } /** Turns WebMCP (beta) on or off for the whole site. Returns what it was. */ diff --git a/tests/e2e/tests/history.spec.ts b/tests/e2e/tests/history.spec.ts index 6805c98..6faa83b 100644 --- a/tests/e2e/tests/history.spec.ts +++ b/tests/e2e/tests/history.spec.ts @@ -15,11 +15,19 @@ const topOf = (page: Page, text: string) => test.describe('Chat history', () => { test.beforeEach(async ({ laravel, credentials, loginAs, page }) => { await laravel.config('ai-chat.test_mode', true); + // The chat's own panel, also in an app that puts the list in its sidebar (the chat template). + await laravel.config('ai-chat.sessions', 'page'); await loginAs(credentials.user); const id = await laravel.callFunction( `${helper}::conversation`, [credentials.user.email, 'Long chat', 120], ); + // Room for another question: these 60 are over a free plan's monthly limit (the chat + // template's allows 50). Without billing there is no limit. + await laravel.callFunction(`${helper}::limitMessages`, [ + credentials.user.email, + 1000, + ]); await page.goto(`/chat/${id}`); }); diff --git a/tests/e2e/tests/layout.spec.ts b/tests/e2e/tests/layout.spec.ts index d48cfdc..640f646 100644 --- a/tests/e2e/tests/layout.spec.ts +++ b/tests/e2e/tests/layout.spec.ts @@ -6,6 +6,8 @@ import { helper, panel } from '../helpers'; test.describe('Chat layout', () => { test.beforeEach(async ({ laravel, credentials, loginAs }) => { await laravel.config('ai-chat.test_mode', true); + // The chat's own panel, also in an app that puts the list in its sidebar (the chat template). + await laravel.config('ai-chat.sessions', 'page'); await loginAs(credentials.user); }); @@ -131,6 +133,8 @@ test.describe('Chat WebMCP', () => { test.beforeEach(async ({ laravel, credentials, loginAs }) => { await laravel.config('ai-chat.test_mode', true); + // The chat's own panel, also in an app that puts the list in its sidebar (the chat template). + await laravel.config('ai-chat.sessions', 'page'); await loginAs(credentials.user); }); diff --git a/tests/e2e/tests/limits.spec.ts b/tests/e2e/tests/limits.spec.ts index 7b76f11..b7f8a91 100644 --- a/tests/e2e/tests/limits.spec.ts +++ b/tests/e2e/tests/limits.spec.ts @@ -4,14 +4,22 @@ import { ask, helper } from '../helpers'; // The month's messages against the plan's limit, with billing: a bar above the box from 80%, // an orange one at the limit, which stays, and Send turned off. test.describe('Chat limits', () => { + // The user's monthly limit: 10 from their own plan, or more when the app's free plan + // allows more (the chat template's allows 50). + let limit = 0; + // Questions already asked: n questions are 2n - 1 stored messages, the last a question. + const asked = (questions: number) => 2 * questions - 1; + test.beforeEach(async ({ laravel, credentials, loginAs }) => { - // A plan of the user's own with 10 messages a month. - const limited = await laravel.callFunction( + const limited = await laravel.callFunction( `${helper}::limitMessages`, [credentials.user.email, 10], ); - test.skip(!limited, 'Billing is not installed.'); + test.skip(limited === null, 'Billing is not installed.'); + limit = limited ?? 0; await laravel.config('ai-chat.test_mode', true); + // The chat's own panel, also in an app that puts the list in its sidebar (the chat template). + await laravel.config('ai-chat.sessions', 'page'); await loginAs(credentials.user); }); @@ -20,20 +28,21 @@ test.describe('Chat limits', () => { laravel, credentials, }) => { - // 7 questions this month. + // One question short of 80%, which the bar shows from. + const shown = Math.ceil(limit * 0.8); await laravel.callFunction(`${helper}::conversation`, [ credentials.user.email, 'Earlier', - 13, + asked(shown - 1), ]); await page.goto('/chat'); await expect(page.getByTestId('chat-input')).toBeVisible(); await expect(page.getByTestId('chat-usage')).toHaveCount(0); - await ask(page, 'The eighth'); + await ask(page, 'One more'); const bar = page.getByTestId('chat-usage'); - await expect(bar).toContainText('8 of 10'); + await expect(bar).toContainText(`${shown} of ${limit}`); await expect(bar.getByTestId('chat-upgrade')).toBeVisible(); await bar.getByRole('button', { name: 'Dismiss' }).click(); @@ -49,16 +58,16 @@ test.describe('Chat limits', () => { laravel, credentials, }) => { - // 10 questions this month. + // Every question of the month. await laravel.callFunction(`${helper}::conversation`, [ credentials.user.email, 'Earlier', - 19, + asked(limit), ]); await page.goto('/chat'); const bar = page.getByTestId('chat-usage'); - await expect(bar).toContainText('all 10'); + await expect(bar).toContainText(`all ${limit}`); await expect(bar.getByRole('button', { name: 'Dismiss' })).toHaveCount( 0, ); diff --git a/tests/e2e/tests/sessions.spec.ts b/tests/e2e/tests/sessions.spec.ts index 52817f8..65e2ffb 100644 --- a/tests/e2e/tests/sessions.spec.ts +++ b/tests/e2e/tests/sessions.spec.ts @@ -5,6 +5,8 @@ import { ask, chatMenu, helper, panel } from '../helpers'; test.describe('Chat list', () => { test.beforeEach(async ({ laravel, credentials, loginAs }) => { await laravel.config('ai-chat.test_mode', true); + // The chat's own panel, also in an app that puts the list in its sidebar (the chat template). + await laravel.config('ai-chat.sessions', 'page'); await loginAs(credentials.user); });