Additional inputs for Describe functionality
This commit is contained in:
parent
efaa5c4306
commit
ad184d778d
5 changed files with 239 additions and 38 deletions
66
lib/describe_prompt.php
Normal file
66
lib/describe_prompt.php
Normal file
|
|
@ -0,0 +1,66 @@
|
|||
<?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;
|
||||
}
|
||||
|
||||
$path = $jobdir . '/.mvlog-describe-prompt.txt';
|
||||
$prompt = is_file($path) ? trim((string)@file_get_contents($path)) : '';
|
||||
|
||||
echo json_encode(['status' => 'ok', 'additional_prompt' => $prompt], JSON_UNESCAPED_UNICODE);
|
||||
Loading…
Add table
Add a link
Reference in a new issue