mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-07-17 14:25:21 +00:00
Sets the default AI chat model to anthropic/claude-sonnet-4.6 and the default text-generation (WYSIWYG writing) model to anthropic/claude- haiku-4.5 across all three layers where defaults live: the backend hydrateDefaults() fallback in AiConfigurationService, the frontend createDefaults() in AiConfigurationForm, and the docblock example in AiTextGenerationService. Refreshes the DriverRegistryProvider suggested-model list to only include recent models from Anthropic (Sonnet 4.6, Haiku 4.5, Opus 4.6), OpenAI (GPT-5.4, GPT-5.4 mini), Google (Gemini 3.1 Pro preview, Gemini 3.1 Flash Lite preview) and Z.AI (GLM 5.1, GLM 4.7 Flash). Drops GPT-4o, Claude 3.5, Gemini 1.5 and Llama 3.3. The underlying config still accepts any OpenRouter model ID, so the suggested list is purely a UX surface — existing companies with a custom ai_chat_model retain their value untouched.
89 lines
3.6 KiB
PHP
89 lines
3.6 KiB
PHP
<?php
|
|
|
|
namespace App\Services\Ai;
|
|
|
|
use App\Services\AiConfigurationService;
|
|
use App\Support\Ai\AiException;
|
|
|
|
/**
|
|
* Stateless one-shot text generation for the WYSIWYG popup.
|
|
*
|
|
* Much simpler than the chat assistant — no conversation state, no tool calls,
|
|
* no history. Takes a user-authored instruction plus optional surrounding
|
|
* context (e.g. "here's the current editor content") and returns a single
|
|
* generated text blob the frontend can insert into the editor.
|
|
*
|
|
* The text-generation role is distinct from chat in two places:
|
|
* - AiConfigurationService.text_generation_enabled gates availability
|
|
* - ai_text_generation_model picks which model to use
|
|
*
|
|
* That means an instance can use a cheap fast model for one-shot writing
|
|
* (anthropic/claude-haiku-4.5) while pointing chat at a smarter model
|
|
* (anthropic/claude-sonnet-4.6) without config gymnastics.
|
|
*/
|
|
class AiTextGenerationService
|
|
{
|
|
public function __construct(
|
|
private readonly AiConfigurationService $aiConfiguration,
|
|
) {}
|
|
|
|
/**
|
|
* Generate text from a user instruction, optionally grounded in a context blob.
|
|
*
|
|
* @param int $companyId Current company — resolves config and model selection
|
|
* @param string $prompt User's instruction (e.g. "write a polite late-payment reminder")
|
|
* @param string|null $context Optional surrounding content — usually the editor's
|
|
* current HTML/text, passed when the user wants the AI
|
|
* to work from existing copy
|
|
*
|
|
* @throws AiException When AI is disabled, text generation is off, or the driver call fails
|
|
*/
|
|
public function generate(int $companyId, string $prompt, ?string $context = null): string
|
|
{
|
|
$driver = $this->aiConfiguration->makeDriver($companyId);
|
|
|
|
if ($driver === null) {
|
|
throw new AiException('AI is not enabled for this company', 'ai_disabled');
|
|
}
|
|
|
|
$resolved = $this->aiConfiguration->resolveForCompany($companyId);
|
|
if (empty($resolved['text_generation_enabled'])) {
|
|
throw new AiException('Text generation is not enabled for this company', 'text_generation_disabled');
|
|
}
|
|
|
|
$model = (string) ($resolved['ai_text_generation_model'] ?? '');
|
|
if ($model === '') {
|
|
throw new AiException('No text generation model configured', 'missing_model');
|
|
}
|
|
|
|
$fullPrompt = $this->buildPrompt($prompt, $context);
|
|
|
|
return trim($driver->textCompletion($fullPrompt, $model));
|
|
}
|
|
|
|
/**
|
|
* Compose the final prompt sent to the model.
|
|
*
|
|
* Keep the framing terse — text-generation output should not be padded
|
|
* with "Here is the text you requested:" preambles. The instruction is
|
|
* always placed last so the model gives it the most weight.
|
|
*/
|
|
protected function buildPrompt(string $prompt, ?string $context): string
|
|
{
|
|
$system = "You are a helpful writing assistant embedded in an invoicing application. Generate text based on the user's instruction. Rules:\n"
|
|
.'- Return only the requested text. No preamble, no explanation, no markdown code fences.'."\n"
|
|
.'- Match the tone and language implied by the instruction.'."\n"
|
|
.'- Be concise unless explicitly asked for length.';
|
|
|
|
if ($context !== null && trim($context) !== '') {
|
|
return $system."\n\n"
|
|
."Context (current content the user is working with):\n"
|
|
.trim($context)
|
|
."\n\n"
|
|
."Instruction: {$prompt}";
|
|
}
|
|
|
|
return $system."\n\nInstruction: {$prompt}";
|
|
}
|
|
}
|