feat: document & audio corpus picker for all tools

- Add "Select from My Docs" button to all text tool forms; free-tier
  users see an upgrade modal, paid (CaveauAI) users get a searchable
  multi-select modal backed by /api/dashboard/documents.php
- Add "Select from My Audio" picker on Transcribe with single-select
  and a "Save to My Audio" button for persisting uploaded clips
- New PHP helpers in bootstrap.php: dbnToolsFetchDocChunks,
  dbnToolsClientIdFromSession, dbnToolsInjectDocContent
- timeline, ask, redact APIs prepend selected document content
  (fetched from client_chunks SQL) before the textarea text
- api/dashboard/audio-upload.php stores audio files on server and
  creates a client_documents row with source_type='audio'
- api/transcribe.php falls back to stored audio via audio_doc_id POST
  field when no file is uploaded
- api/dashboard/documents.php supports ?source_type= filter
- tools.js: doc_ids added to JSON payload; stored-audio transcribe path
- New assets/css/doc-picker.css, assets/js/doc-picker.js
- SQL migration: scripts/sql/audio_docs_column.sql

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-23 21:38:04 +02:00
parent 58e1d1dae1
commit f383ad5b74
14 changed files with 857 additions and 15 deletions
+85
View File
@@ -0,0 +1,85 @@
<?php
/**
* POST /api/dashboard/audio-upload.php
*
* Stores an uploaded audio file on the server and creates a client_documents
* row with source_type='audio' so it appears in the Audio picker on Transcribe.
*
* Request: multipart/form-data, field name "audio"
* Response: { ok: true, document: { id, title, file_size_bytes, audio_storage_path } }
*/
declare(strict_types=1);
require_once dirname(__DIR__, 2) . '/includes/bootstrap.php';
dbnToolsRequireAuth();
dbnToolsRequireMethod('POST');
try {
$tenant = dbnToolsEnsureDashboardTenant();
} catch (DbnToolsHttpException $e) {
dbnToolsError($e->getMessage(), $e->status, $e->errorCode);
}
$clientId = (int)$tenant['client_id'];
if (empty($_FILES['audio']) || $_FILES['audio']['error'] !== UPLOAD_ERR_OK) {
$code = $_FILES['audio']['error'] ?? -1;
$msgs = [
UPLOAD_ERR_INI_SIZE => 'File exceeds server upload limit.',
UPLOAD_ERR_FORM_SIZE => 'File exceeds form size limit.',
UPLOAD_ERR_PARTIAL => 'File was only partially uploaded.',
UPLOAD_ERR_NO_FILE => 'No audio file received.',
];
dbnToolsError($msgs[$code] ?? "Upload error (code {$code}).", 400, 'upload_error');
}
$file = $_FILES['audio'];
$maxBytes = 200 * 1024 * 1024;
if ($file['size'] > $maxBytes) {
dbnToolsError('File too large. Maximum 200 MB.', 413, 'file_too_large');
}
$allowedExts = ['mp3', 'wav', 'ogg', 'oga', 'm4a', 'mp4', 'flac', 'webm', 'aac'];
$ext = strtolower(pathinfo($file['name'], PATHINFO_EXTENSION));
if (!in_array($ext, $allowedExts, true)) {
dbnToolsError("Unsupported format: .{$ext}. Use MP3, WAV, OGG, M4A, FLAC, or WebM.", 415, 'unsupported_format');
}
// Resolve storage directory
$storageRoot = dbnToolsEnv('AUDIO_STORAGE_ROOT', '/home/dobetternorge/audio-uploads');
$clientDir = rtrim($storageRoot, '/') . '/' . $clientId;
if (!is_dir($clientDir) && !mkdir($clientDir, 0750, true)) {
dbnToolsError('Could not create storage directory.', 500, 'storage_error');
}
$uniqueId = bin2hex(random_bytes(12));
$storagePath = $clientDir . '/' . $uniqueId . '.' . $ext;
if (!move_uploaded_file($file['tmp_name'], $storagePath)) {
dbnToolsError('Failed to store uploaded file.', 500, 'move_error');
}
$title = pathinfo($file['name'], PATHINFO_FILENAME);
$title = preg_replace('/[_\-]+/', ' ', $title);
$title = mb_substr(trim($title), 0, 200) ?: 'Audio ' . date('Y-m-d');
$db = dbnToolsDb();
$db->prepare(
'INSERT INTO client_documents
(client_id, title, source_type, audio_storage_path, status, file_size_bytes, created_at, updated_at)
VALUES (?, ?, ?, ?, ?, ?, NOW(), NOW())'
)->execute([$clientId, $title, 'audio', $storagePath, 'ready', (int)$file['size']]);
$docId = (int)$db->lastInsertId();
dbnToolsRespond([
'ok' => true,
'document' => [
'id' => $docId,
'title' => $title,
'file_size_bytes' => (int)$file['size'],
'audio_storage_path' => $storagePath,
'source_type' => 'audio',
],
]);