mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-07-16 22:05:20 +00:00
Second phase of the AI feature. Users can now open a slide-in chat drawer from the SiteHeader and ask natural-language questions about their company's invoices, customers, payments, and expenses. The LLM invokes pre-defined read-only tool functions (scoped to the current company at execute time) to fetch data and synthesize answers.
**Database** — new ai_conversations and ai_messages tables. Messages are stored in OpenAI's chat format so AiAssistantService serializes a conversation into an API request with zero translation. Columns: role, content, tool_call_id, tool_calls JSON, model, tokens_in, tokens_out. Conversations are scoped (company_id, user_id) — one user's chats are invisible to everyone else, even inside the same company. Foreign-key cascade deletes.
**Tool infrastructure** — AiTool abstract base + AiToolRegistry singleton (registered in a new AiServiceProvider). The base class enforces the security rule: every tool's execute() receives companyId and userId as injected parameters; tools' JSON schemas NEVER include a company_id field. An LLM physically cannot pass a company_id and escape tenancy. Modules can register their own tools by resolving the registry from their own ServiceProvider::boot().
**Nine built-in tools**: search_invoices, get_invoice, search_customers, get_customer, list_recent_payments, list_overdue_invoices, get_company_stats (aggregates for named periods), search_items, list_expense_categories. All read-only; no mutations. Each returns JSON-encodable data the LLM can parse.
**AiAssistantService orchestration loop** — the heart of Phase 2. Flow: persist user message → build payload from system prompt + recent history (40-message window) + new user message → call driver.chatCompletion with tools → if tool_calls, execute each one via the registry (with injected scope), persist tool result, loop → if plain text, persist and return. Hard cap at 5 iterations to prevent runaway LLMs. System prompt pins the assistant to this company's data and forbids mutation.
**Controllers + policy + rate limit** — POST /api/v1/ai/chat runs the orchestration loop. GET/PATCH/DELETE /api/v1/ai/conversations for CRUD. AiConversationPolicy enforces user_id+company_id match on every action. A new 'ai' RateLimiter in RouteServiceProvider throttles to 30 req/min per (user, company). New 'use ai' Gate defined in AppServiceProvider returns true for any authenticated user — the per-company kill-switch still goes through AiConfigurationService::resolveForCompany.
**Frontend** — new features/company/ai/ folder with a Pinia store (ai-chat.store.ts) holding drawer state, current conversation, messages, and loading flags. AiChatDrawer.vue is a slide-in panel teleported to <body>, mounted globally in CompanyLayout.vue when bootstrap reports ai.enabled && ai.chat_enabled. Sub-components: AiChatMessage (user bubbles vs assistant bubbles), AiChatMessageInput (Enter submits, Shift+Enter newline), AiChatConversationList (sidebar with 'new chat' button, rename, delete). A SparklesIcon button in SiteHeader toggles the drawer.
**Driver test double** — tests use a ScriptedAiDriver registered via AiDriverFactory::register('scripted', ...) that returns pre-queued AiChatResponse objects. Feature tests cover: happy path (new conversation + message persistence), tool-call loop (multi-round-trip with search_invoices), runaway-loop cap, driver-throws path, ai_enabled=NO rejection, chat role disabled rejection, per-user conversation visibility, cross-user policy enforcement, cascade delete.
388 tests pass (was 372, +16 new). Pint clean. npm run build clean. Phase 3 (WYSIWYG text generation popup) is the remaining follow-up.
191 lines
5.8 KiB
TypeScript
191 lines
5.8 KiB
TypeScript
export const API = {
|
|
// Authentication & Password Reset
|
|
LOGIN: '/login',
|
|
LOGOUT: '/auth/logout',
|
|
FORGOT_PASSWORD: '/api/v1/auth/password/email',
|
|
RESET_PASSWORD: '/api/v1/auth/reset/password',
|
|
AUTH_CHECK: '/api/v1/auth/check',
|
|
CSRF_COOKIE: '/sanctum/csrf-cookie',
|
|
REGISTER_WITH_INVITATION: '/api/v1/auth/register-with-invitation',
|
|
INSTALLATION_LOGIN: '/api/v1/installation/login',
|
|
INSTALLATION_SET_DOMAIN: '/api/v1/installation/set-domain',
|
|
INSTALLATION_WIZARD_STEP: '/api/v1/installation/wizard-step',
|
|
INSTALLATION_SESSION_LOGIN: '/installation/session-login',
|
|
|
|
// Invitation Registration (public)
|
|
INVITATION_DETAILS: '/api/v1/invitations', // append /{token}/details
|
|
|
|
// Invitations (user-scoped)
|
|
INVITATIONS_PENDING: '/api/v1/invitations/pending',
|
|
INVITATIONS: '/api/v1/invitations', // append /{token}/accept or /{token}/decline
|
|
|
|
// Bootstrap & General
|
|
BOOTSTRAP: '/api/v1/bootstrap',
|
|
CONFIG: '/api/v1/config',
|
|
CURRENT_COMPANY: '/api/v1/current-company',
|
|
SEARCH: '/api/v1/search',
|
|
SEARCH_USERS: '/api/v1/search/user',
|
|
APP_VERSION: '/api/v1/app/version',
|
|
COUNTRIES: '/api/v1/countries',
|
|
|
|
// Dashboard
|
|
DASHBOARD: '/api/v1/dashboard',
|
|
|
|
// Customers
|
|
CUSTOMERS: '/api/v1/customers',
|
|
CUSTOMERS_DELETE: '/api/v1/customers/delete',
|
|
CUSTOMER_STATS: '/api/v1/customers', // append /{id}/stats
|
|
|
|
// Items & Units
|
|
ITEMS: '/api/v1/items',
|
|
ITEMS_DELETE: '/api/v1/items/delete',
|
|
UNITS: '/api/v1/units',
|
|
|
|
// Invoices
|
|
INVOICES: '/api/v1/invoices',
|
|
INVOICES_DELETE: '/api/v1/invoices/delete',
|
|
INVOICE_TEMPLATES: '/api/v1/invoices/templates',
|
|
|
|
// Recurring Invoices
|
|
RECURRING_INVOICES: '/api/v1/recurring-invoices',
|
|
RECURRING_INVOICES_DELETE: '/api/v1/recurring-invoices/delete',
|
|
RECURRING_INVOICE_FREQUENCY: '/api/v1/recurring-invoice-frequency',
|
|
|
|
// Estimates
|
|
ESTIMATES: '/api/v1/estimates',
|
|
ESTIMATES_DELETE: '/api/v1/estimates/delete',
|
|
ESTIMATE_TEMPLATES: '/api/v1/estimates/templates',
|
|
|
|
// Expenses
|
|
EXPENSES: '/api/v1/expenses',
|
|
EXPENSES_DELETE: '/api/v1/expenses/delete',
|
|
|
|
// Expense Categories
|
|
CATEGORIES: '/api/v1/categories',
|
|
|
|
// Payments
|
|
PAYMENTS: '/api/v1/payments',
|
|
PAYMENTS_DELETE: '/api/v1/payments/delete',
|
|
PAYMENT_METHODS: '/api/v1/payment-methods',
|
|
|
|
// Custom Fields
|
|
CUSTOM_FIELDS: '/api/v1/custom-fields',
|
|
|
|
// Notes
|
|
NOTES: '/api/v1/notes',
|
|
|
|
// Tax Types
|
|
TAX_TYPES: '/api/v1/tax-types',
|
|
|
|
// Roles & Abilities
|
|
ROLES: '/api/v1/roles',
|
|
ABILITIES: '/api/v1/abilities',
|
|
|
|
// Company
|
|
COMPANY: '/api/v1/company',
|
|
COMPANY_UPLOAD_LOGO: '/api/v1/company/upload-logo',
|
|
COMPANY_SETTINGS: '/api/v1/company/settings',
|
|
COMPANY_HAS_TRANSACTIONS: '/api/v1/company/has-transactions',
|
|
COMPANIES: '/api/v1/companies',
|
|
COMPANIES_DELETE: '/api/v1/companies/delete',
|
|
TRANSFER_OWNERSHIP: '/api/v1/transfer/ownership', // append /{userId}
|
|
|
|
// Company Invitations (company-scoped)
|
|
COMPANY_INVITATIONS: '/api/v1/company-invitations',
|
|
|
|
// Members
|
|
MEMBERS: '/api/v1/members',
|
|
MEMBERS_DELETE: '/api/v1/members/delete',
|
|
|
|
// User Profile & Settings
|
|
ME: '/api/v1/me',
|
|
ME_SETTINGS: '/api/v1/me/settings',
|
|
ME_UPLOAD_AVATAR: '/api/v1/me/upload-avatar',
|
|
|
|
// Global Settings (admin)
|
|
SETTINGS: '/api/v1/settings',
|
|
|
|
// Mail Configuration (global)
|
|
MAIL_DRIVERS: '/api/v1/mail/drivers',
|
|
MAIL_CONFIG: '/api/v1/mail/config',
|
|
MAIL_TEST: '/api/v1/mail/test',
|
|
|
|
// Company Mail Configuration
|
|
COMPANY_MAIL_DEFAULT_CONFIG: '/api/v1/company/mail/config',
|
|
COMPANY_MAIL_CONFIG: '/api/v1/company/mail/company-config',
|
|
COMPANY_MAIL_TEST: '/api/v1/company/mail/company-test',
|
|
|
|
// AI Configuration (global)
|
|
AI_DRIVERS: '/api/v1/ai/drivers',
|
|
AI_CONFIG: '/api/v1/ai/config',
|
|
AI_TEST: '/api/v1/ai/test',
|
|
|
|
// Company AI Configuration
|
|
COMPANY_AI_CONFIG: '/api/v1/company/ai/config',
|
|
COMPANY_AI_TEST: '/api/v1/company/ai/test',
|
|
|
|
// Installer AI Configuration
|
|
INSTALLATION_AI_CONFIG: '/api/v1/installation/ai/config',
|
|
|
|
// AI Chat (Phase 2)
|
|
AI_CHAT: '/api/v1/ai/chat',
|
|
AI_CONVERSATIONS: '/api/v1/ai/conversations',
|
|
|
|
// PDF Configuration
|
|
PDF_DRIVERS: '/api/v1/pdf/drivers',
|
|
PDF_CONFIG: '/api/v1/pdf/config',
|
|
|
|
// Disks & Backups
|
|
DISKS: '/api/v1/disks',
|
|
DISK_DRIVERS: '/api/v1/disk/drivers',
|
|
DISK_PURPOSES: '/api/v1/disk/purposes',
|
|
BACKUPS: '/api/v1/backups',
|
|
DOWNLOAD_BACKUP: '/api/v1/download-backup',
|
|
|
|
// Fonts
|
|
FONTS_STATUS: '/api/v1/fonts/status',
|
|
FONTS_INSTALL: '/api/v1/fonts',
|
|
|
|
// Exchange Rates & Currencies
|
|
CURRENCIES: '/api/v1/currencies',
|
|
CURRENCIES_USED: '/api/v1/currencies/used',
|
|
CURRENCIES_BULK_UPDATE: '/api/v1/currencies/bulk-update-exchange-rate',
|
|
EXCHANGE_RATE_PROVIDERS: '/api/v1/exchange-rate-providers',
|
|
USED_CURRENCIES: '/api/v1/used-currencies',
|
|
SUPPORTED_CURRENCIES: '/api/v1/supported-currencies',
|
|
|
|
// Serial Numbers
|
|
NEXT_NUMBER: '/api/v1/next-number',
|
|
NUMBER_PLACEHOLDERS: '/api/v1/number-placeholders',
|
|
|
|
// Formats
|
|
TIMEZONES: '/api/v1/timezones',
|
|
DATE_FORMATS: '/api/v1/date/formats',
|
|
TIME_FORMATS: '/api/v1/time/formats',
|
|
|
|
// Modules
|
|
MODULES: '/api/v1/modules',
|
|
MODULES_CHECK: '/api/v1/modules/check',
|
|
MODULES_DOWNLOAD: '/api/v1/modules/download',
|
|
MODULES_UPLOAD: '/api/v1/modules/upload',
|
|
MODULES_UNZIP: '/api/v1/modules/unzip',
|
|
MODULES_COPY: '/api/v1/modules/copy',
|
|
MODULES_COMPLETE: '/api/v1/modules/complete',
|
|
|
|
// Self Update
|
|
CHECK_UPDATE: '/api/v1/check/update',
|
|
UPDATE_DOWNLOAD: '/api/v1/update/download',
|
|
UPDATE_UNZIP: '/api/v1/update/unzip',
|
|
UPDATE_COPY: '/api/v1/update/copy',
|
|
UPDATE_DELETE: '/api/v1/update/delete',
|
|
UPDATE_CLEAN: '/api/v1/update/clean',
|
|
UPDATE_MIGRATE: '/api/v1/update/migrate',
|
|
UPDATE_FINISH: '/api/v1/update/finish',
|
|
|
|
// Super Admin
|
|
SUPER_ADMIN_DASHBOARD: '/api/v1/super-admin/dashboard',
|
|
SUPER_ADMIN_COMPANIES: '/api/v1/super-admin/companies',
|
|
SUPER_ADMIN_USERS: '/api/v1/super-admin/users',
|
|
SUPER_ADMIN_STOP_IMPERSONATING: '/api/v1/super-admin/stop-impersonating',
|
|
} as const
|