82 lines
2.9 KiB
PHP
82 lines
2.9 KiB
PHP
<?php
|
|
require __DIR__ . '/../auth.php';
|
|
mvlog_require_login();
|
|
|
|
$MVLOG_ROOT = dirname(__DIR__);
|
|
header('Content-Type: application/json; charset=utf-8');
|
|
|
|
$id = strtolower(trim((string)($_POST['id'] ?? $_GET['id'] ?? '')));
|
|
$job = trim((string)($_POST['job'] ?? $_GET['job'] ?? ''));
|
|
|
|
function mvlog_article_id_is_valid(string $id): bool {
|
|
return (bool)preg_match('/^[0-9]{14}[a-f0-9]{16}$/', $id);
|
|
}
|
|
|
|
function mvlog_legacy_job_is_valid(string $job): bool {
|
|
return (bool)preg_match('/^[0-9]{8}[-_][A-Za-z0-9._-]+$/', $job);
|
|
}
|
|
|
|
function mvlog_read_article_id(string $jobdir): string {
|
|
$path = $jobdir . '/.mvlog-id';
|
|
if (!is_file($path)) return '';
|
|
$value = strtolower(trim((string)@file_get_contents($path)));
|
|
return mvlog_article_id_is_valid($value) ? $value : '';
|
|
}
|
|
|
|
function mvlog_find_jobdir_by_id(string $mvlogRoot, string $articleId): ?string {
|
|
if (!mvlog_article_id_is_valid($articleId)) return null;
|
|
|
|
$cacheFile = $mvlogRoot . '/cache/articles-index.json';
|
|
if (is_file($cacheFile)) {
|
|
$cache = json_decode((string)@file_get_contents($cacheFile), true);
|
|
if (is_array($cache) && is_array($cache['by_id'] ?? null)) {
|
|
$name = (string)($cache['by_id'][$articleId] ?? '');
|
|
if ($name !== '') {
|
|
$candidate = $mvlogRoot . '/in-dir/' . basename($name);
|
|
if (is_dir($candidate)) return $candidate;
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (glob($mvlogRoot . '/in-dir/*', GLOB_ONLYDIR) ?: [] as $dir) {
|
|
if (mvlog_read_article_id($dir) === $articleId) return $dir;
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
$jobdir = null;
|
|
if ($id !== '' && mvlog_article_id_is_valid($id)) {
|
|
$jobdir = mvlog_find_jobdir_by_id($MVLOG_ROOT, $id);
|
|
}
|
|
if ($jobdir === null && $job !== '' && mvlog_legacy_job_is_valid($job)) {
|
|
$candidate = $MVLOG_ROOT . '/in-dir/' . basename($job);
|
|
if (is_dir($candidate)) $jobdir = $candidate;
|
|
}
|
|
|
|
if ($jobdir === null || !is_dir($jobdir)) {
|
|
http_response_code(404);
|
|
echo json_encode(['status' => 'error', 'message' => 'job not found']);
|
|
exit;
|
|
}
|
|
|
|
function mvlog_read_personality_keys(string $script): array {
|
|
if (!is_file($script)) return [];
|
|
$src = (string)@file_get_contents($script);
|
|
if ($src === '') return [];
|
|
if (!preg_match('/PERSONALITY_KEYS=\((.*?)\)/s', $src, $m)) return [];
|
|
if (!preg_match_all('/"((?:\\\\.|[^"\\\\])*)"/', $m[1], $mm)) return [];
|
|
$out = [];
|
|
foreach ($mm[1] as $raw) {
|
|
$key = stripcslashes($raw);
|
|
if ($key === '') continue;
|
|
$out[] = ['key' => $key, 'label' => ucfirst(str_replace(['_', '-'], ' ', $key))];
|
|
}
|
|
return $out;
|
|
}
|
|
|
|
$path = $jobdir . '/.mvlog-describe-prompt.txt';
|
|
$prompt = is_file($path) ? trim((string)@file_get_contents($path)) : '';
|
|
$personalities = mvlog_read_personality_keys($MVLOG_ROOT . '/bin/generate_data.sh');
|
|
|
|
echo json_encode(['status' => 'ok', 'additional_prompt' => $prompt, 'personalities' => $personalities], JSON_UNESCAPED_UNICODE);
|