add system prompt and chunks index views and edit
This commit is contained in:
@@ -5,14 +5,14 @@ declare(strict_types=1);
|
||||
namespace App\Agent;
|
||||
|
||||
use App\Context\ContextService;
|
||||
use App\Context\UrlAnalyzer;
|
||||
use App\Repository\SystemPromptRepository;
|
||||
use DateTimeImmutable;
|
||||
|
||||
final class PromptBuilder
|
||||
final readonly class PromptBuilder
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ContextService $contextService,
|
||||
private readonly UrlAnalyzer $urlAnalyzer,
|
||||
private ContextService $contextService,
|
||||
private SystemPromptRepository $systemPromptRepository,
|
||||
)
|
||||
{
|
||||
}
|
||||
@@ -25,6 +25,7 @@ final class PromptBuilder
|
||||
* @param string $urlContent
|
||||
* @param string[] $knowledgeChunks
|
||||
* @param bool $fullContext
|
||||
* @return string
|
||||
*/
|
||||
public function build(
|
||||
string $prompt,
|
||||
@@ -39,7 +40,8 @@ final class PromptBuilder
|
||||
// ------------------------------------------------------------
|
||||
// 1) SYSTEM INSTRUCTIONS
|
||||
// ------------------------------------------------------------
|
||||
$systemLines = [
|
||||
//ToDO: remove old systemLines
|
||||
/*$systemLines = [
|
||||
'You are a conversational AI assistant.',
|
||||
'Respond clearly, precisely, and in context of the ongoing conversation.',
|
||||
'The conversation context is authoritative and must be respected.',
|
||||
@@ -66,7 +68,15 @@ final class PromptBuilder
|
||||
'- Answer directly and confidently using always correct canonical terminology.'
|
||||
];
|
||||
|
||||
$systemBlock = "SYSTEM:\n" . implode("\n", $systemLines);
|
||||
$systemBlock = "SYSTEM:\n" . implode("\n", $systemLines);*/
|
||||
|
||||
$activePrompt = $this->systemPromptRepository->findActive();
|
||||
|
||||
if (!$activePrompt) {
|
||||
throw new \RuntimeException('No active system prompt configured.');
|
||||
}
|
||||
|
||||
$systemBlock = "SYSTEM:\n" . $activePrompt->getContent();
|
||||
|
||||
// ------------------------------------------------------------
|
||||
// 2) CONVERSATION CONTEXT (AUTHORITATIVE)
|
||||
|
||||
35
src/Controller/Admin/SystemAgentController.php
Normal file
35
src/Controller/Admin/SystemAgentController.php
Normal file
@@ -0,0 +1,35 @@
|
||||
<?php
|
||||
// src/Controller/Admin/SystemAgentController.php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use App\Service\Admin\IndexNdjsonInspector;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
#[IsGranted('ROLE_SUPER_ADMIN')]
|
||||
final class SystemAgentController extends AbstractController
|
||||
{
|
||||
#[Route('/admin/system/agent', name: 'admin_system_agent', methods: ['GET'])]
|
||||
public function index(Request $request, IndexNdjsonInspector $inspector): Response
|
||||
{
|
||||
$page = max(1, (int) $request->query->get('page', 1));
|
||||
$limit = max(1, min(200, (int) $request->query->get('limit', 50)));
|
||||
|
||||
$paths = $inspector->getPaths();
|
||||
$meta = $inspector->readMeta();
|
||||
$ndjsonPage = $inspector->readNdjsonPage($page, $limit);
|
||||
|
||||
return $this->render('admin/system/agent_overview.html.twig', [
|
||||
'paths' => $paths,
|
||||
'meta' => $meta,
|
||||
'ndjson' => $ndjsonPage,
|
||||
'debugCount'=>count($ndjsonPage['items'])
|
||||
]);
|
||||
}
|
||||
}
|
||||
94
src/Controller/Admin/SystemPromptController.php
Normal file
94
src/Controller/Admin/SystemPromptController.php
Normal file
@@ -0,0 +1,94 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Controller\Admin;
|
||||
|
||||
use App\Entity\SystemPrompt;
|
||||
use App\Repository\SystemPromptRepository;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
use Symfony\Component\Security\Http\Attribute\IsGranted;
|
||||
|
||||
#[IsGranted('ROLE_SUPER_ADMIN')]
|
||||
final class SystemPromptController extends AbstractController
|
||||
{
|
||||
#[Route('/admin/system/prompt', name: 'admin_system_prompt')]
|
||||
public function index(
|
||||
Request $request,
|
||||
SystemPromptRepository $repo,
|
||||
EntityManagerInterface $em
|
||||
): Response {
|
||||
|
||||
if ($request->isMethod('POST')) {
|
||||
$content = trim($request->request->get('content', ''));
|
||||
$comment = trim($request->request->get('comment', ''));
|
||||
|
||||
if ($content !== '') {
|
||||
|
||||
$active = $repo->findActive();
|
||||
if ($active) {
|
||||
$active->deactivate();
|
||||
}
|
||||
|
||||
$new = new SystemPrompt(
|
||||
version: $repo->getNextVersion(),
|
||||
content: $content,
|
||||
comment: $comment ?: null,
|
||||
active: true
|
||||
);
|
||||
|
||||
$em->persist($new);
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', 'Neue Version gespeichert.');
|
||||
return $this->redirectToRoute('admin_system_prompt');
|
||||
}
|
||||
}
|
||||
|
||||
return $this->render('admin/system/prompt.html.twig', [
|
||||
'active' => $repo->findActive(),
|
||||
'all' => $repo->findBy([], ['version' => 'DESC']),
|
||||
]);
|
||||
}
|
||||
|
||||
#[Route('/admin/system/prompt/{id}/activate', name: 'admin_system_prompt_activate', methods: ['POST'])]
|
||||
public function activate(
|
||||
SystemPrompt $prompt,
|
||||
SystemPromptRepository $repo,
|
||||
EntityManagerInterface $em
|
||||
): Response {
|
||||
|
||||
foreach ($repo->findBy(['active' => true]) as $p) {
|
||||
$p->deactivate();
|
||||
}
|
||||
|
||||
$prompt->activate();
|
||||
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', 'Version aktiviert.');
|
||||
return $this->redirectToRoute('admin_system_prompt');
|
||||
}
|
||||
|
||||
#[Route('/admin/system/prompt/{id}/delete', name: 'admin_system_prompt_delete', methods: ['POST'])]
|
||||
public function delete(
|
||||
SystemPrompt $prompt,
|
||||
EntityManagerInterface $em
|
||||
): Response {
|
||||
|
||||
if ($prompt->isActive()) {
|
||||
$this->addFlash('danger', 'Aktive Version kann nicht gelöscht werden.');
|
||||
return $this->redirectToRoute('admin_system_prompt');
|
||||
}
|
||||
|
||||
$em->remove($prompt);
|
||||
$em->flush();
|
||||
|
||||
$this->addFlash('success', 'Version gelöscht.');
|
||||
return $this->redirectToRoute('admin_system_prompt');
|
||||
}
|
||||
}
|
||||
111
src/Entity/SystemPrompt.php
Normal file
111
src/Entity/SystemPrompt.php
Normal file
@@ -0,0 +1,111 @@
|
||||
<?php
|
||||
// src/Entity/SystemPrompt.php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Entity;
|
||||
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
|
||||
#[ORM\Entity]
|
||||
#[ORM\Table(name: 'system_prompt')]
|
||||
class SystemPrompt
|
||||
{
|
||||
#[ORM\Id]
|
||||
#[ORM\Column(type: 'uuid', unique: true)]
|
||||
private Uuid $id;
|
||||
|
||||
#[ORM\Column(type: 'integer')]
|
||||
private int $version;
|
||||
|
||||
#[ORM\Column(type: 'text')]
|
||||
private string $content;
|
||||
|
||||
// 📝 Neuer Kommentar
|
||||
#[ORM\Column(type: 'string', length: 255, nullable: true)]
|
||||
private ?string $comment = null;
|
||||
|
||||
#[ORM\Column(type: 'boolean')]
|
||||
private bool $active = false;
|
||||
|
||||
#[ORM\Column(type: 'datetime_immutable')]
|
||||
private \DateTimeImmutable $createdAt;
|
||||
|
||||
// Optional für Governance / spätere Audit-Erweiterung
|
||||
#[ORM\Column(type: 'datetime_immutable', nullable: true)]
|
||||
private ?\DateTimeImmutable $updatedAt = null;
|
||||
|
||||
public function __construct(
|
||||
int $version,
|
||||
string $content,
|
||||
?string $comment = null,
|
||||
bool $active = false
|
||||
) {
|
||||
$this->id = Uuid::v4();
|
||||
$this->version = $version;
|
||||
$this->content = $content;
|
||||
$this->comment = $comment;
|
||||
$this->active = $active;
|
||||
$this->createdAt = new \DateTimeImmutable();
|
||||
}
|
||||
|
||||
public function getId(): Uuid
|
||||
{
|
||||
return $this->id;
|
||||
}
|
||||
|
||||
public function getVersion(): int
|
||||
{
|
||||
return $this->version;
|
||||
}
|
||||
|
||||
public function getContent(): string
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function getComment(): ?string
|
||||
{
|
||||
return $this->comment;
|
||||
}
|
||||
|
||||
public function isActive(): bool
|
||||
{
|
||||
return $this->active;
|
||||
}
|
||||
|
||||
public function getCreatedAt(): \DateTimeImmutable
|
||||
{
|
||||
return $this->createdAt;
|
||||
}
|
||||
|
||||
public function getUpdatedAt(): ?\DateTimeImmutable
|
||||
{
|
||||
return $this->updatedAt;
|
||||
}
|
||||
|
||||
public function activate(): void
|
||||
{
|
||||
$this->active = true;
|
||||
$this->touch();
|
||||
}
|
||||
|
||||
public function deactivate(): void
|
||||
{
|
||||
$this->active = false;
|
||||
$this->touch();
|
||||
}
|
||||
|
||||
public function updateContent(string $content, ?string $comment = null): void
|
||||
{
|
||||
$this->content = $content;
|
||||
$this->comment = $comment;
|
||||
$this->touch();
|
||||
}
|
||||
|
||||
private function touch(): void
|
||||
{
|
||||
$this->updatedAt = new \DateTimeImmutable();
|
||||
}
|
||||
}
|
||||
38
src/Repository/SystemPromptRepository.php
Normal file
38
src/Repository/SystemPromptRepository.php
Normal file
@@ -0,0 +1,38 @@
|
||||
<?php
|
||||
// src/Repository/SystemPromptRepository.php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Repository;
|
||||
|
||||
use App\Entity\SystemPrompt;
|
||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||
use Doctrine\Persistence\ManagerRegistry;
|
||||
|
||||
class SystemPromptRepository extends ServiceEntityRepository
|
||||
{
|
||||
public function __construct(ManagerRegistry $registry)
|
||||
{
|
||||
parent::__construct($registry, SystemPrompt::class);
|
||||
}
|
||||
|
||||
public function findActive(): ?SystemPrompt
|
||||
{
|
||||
return $this->createQueryBuilder('p')
|
||||
->andWhere('p.active = true')
|
||||
->orderBy('p.version', 'DESC')
|
||||
->setMaxResults(1)
|
||||
->getQuery()
|
||||
->getOneOrNullResult();
|
||||
}
|
||||
|
||||
public function getNextVersion(): int
|
||||
{
|
||||
$max = $this->createQueryBuilder('p')
|
||||
->select('MAX(p.version)')
|
||||
->getQuery()
|
||||
->getSingleScalarResult();
|
||||
|
||||
return ((int)$max) + 1;
|
||||
}
|
||||
}
|
||||
132
src/Service/Admin/IndexNdjsonInspector.php
Normal file
132
src/Service/Admin/IndexNdjsonInspector.php
Normal file
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
// src/Service/Admin/IndexNdjsonInspector.php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Service\Admin;
|
||||
|
||||
final class IndexNdjsonInspector
|
||||
{
|
||||
private string $ndjsonPath;
|
||||
private string $metaPath;
|
||||
|
||||
public function __construct(string $ndJsonPath, string $indexMetaPath)
|
||||
{
|
||||
// Passe diese Pfade an deine echten Ablageorte an, falls abweichend:
|
||||
// z.B. var/rag/index.ndjson oder storage/rag/index.ndjson etc.
|
||||
$this->ndjsonPath = $ndJsonPath;
|
||||
$this->metaPath = $indexMetaPath;
|
||||
}
|
||||
|
||||
public function getPaths(): array
|
||||
{
|
||||
return [
|
||||
'ndjson' => $this->ndjsonPath,
|
||||
'meta' => $this->metaPath,
|
||||
];
|
||||
}
|
||||
|
||||
public function readMeta(): array
|
||||
{
|
||||
if (!is_file($this->metaPath)) {
|
||||
return [
|
||||
'error' => 'index_meta.json nicht gefunden',
|
||||
'path' => $this->metaPath,
|
||||
];
|
||||
}
|
||||
|
||||
$raw = @file_get_contents($this->metaPath);
|
||||
if ($raw === false) {
|
||||
return [
|
||||
'error' => 'index_meta.json konnte nicht gelesen werden',
|
||||
'path' => $this->metaPath,
|
||||
];
|
||||
}
|
||||
|
||||
$data = json_decode($raw, true);
|
||||
if (!is_array($data)) {
|
||||
return [
|
||||
'error' => 'index_meta.json ist kein valides JSON-Objekt',
|
||||
'path' => $this->metaPath,
|
||||
];
|
||||
}
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Liest NDJSON "streaming" und gibt nur einen Ausschnitt zurück.
|
||||
* - Keine Voll-Loads
|
||||
* - Maximal limit Zeilen
|
||||
* - Offset = (page-1)*limit
|
||||
*/
|
||||
public function readNdjsonPage(int $page, int $limit): array
|
||||
{
|
||||
$page = max(1, $page);
|
||||
$limit = max(1, min(200, $limit)); // hard cap für Admin-UI
|
||||
|
||||
if (!is_file($this->ndjsonPath)) {
|
||||
return [
|
||||
'error' => 'index.ndjson nicht gefunden',
|
||||
'path' => $this->ndjsonPath,
|
||||
'items' => [],
|
||||
];
|
||||
}
|
||||
|
||||
$handle = @fopen($this->ndjsonPath, 'rb');
|
||||
if ($handle === false) {
|
||||
return [
|
||||
'error' => 'index.ndjson konnte nicht geöffnet werden',
|
||||
'path' => $this->ndjsonPath,
|
||||
'items' => [],
|
||||
];
|
||||
}
|
||||
|
||||
$offsetLines = ($page - 1) * $limit;
|
||||
$items = [];
|
||||
$lineNo = 0;
|
||||
|
||||
// Sicherheitslimit: wir lesen max. X Bytes pro Line (um Monster-Zeilen abzufangen)
|
||||
$maxLineBytes = 1024 * 1024; // 1MB pro Zeile
|
||||
|
||||
while (!feof($handle)) {
|
||||
$line = fgets($handle, $maxLineBytes);
|
||||
if ($line === false) {
|
||||
break;
|
||||
}
|
||||
|
||||
$lineNo++;
|
||||
|
||||
if ($lineNo <= $offsetLines) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$line = trim($line);
|
||||
if ($line === '') {
|
||||
continue;
|
||||
}
|
||||
|
||||
$decoded = json_decode($line, true);
|
||||
if (!is_array($decoded)) {
|
||||
// Ungültige Zeile ignorieren, aber sichtbar machen wäre auch ok.
|
||||
continue;
|
||||
}
|
||||
|
||||
$items[] = $decoded;
|
||||
|
||||
if (count($items) >= $limit) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
fclose($handle);
|
||||
|
||||
return [
|
||||
'error' => null,
|
||||
'path' => $this->ndjsonPath,
|
||||
'items' => $items,
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
];
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user