feat: API for content
This commit is contained in:
@@ -3,9 +3,11 @@
|
||||
namespace App\Http\Controllers\Api;
|
||||
|
||||
use App\Http\Controllers\Controller;
|
||||
use App\Models\Document;
|
||||
use App\Models\Guide;
|
||||
use App\Models\GuidePage;
|
||||
use App\Models\GuidePageEdge;
|
||||
use App\Models\KnowledgeBase;
|
||||
use App\Services\KnowledgeContextService;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
@@ -168,6 +170,66 @@ class TerminalApiController extends Controller
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* GET /api/terminal/documents/{document}/content
|
||||
* 读取文档全文或指定行号区间
|
||||
*/
|
||||
public function documentContent(Request $request, int $documentId): JsonResponse
|
||||
{
|
||||
$request->validate([
|
||||
'start_line' => 'sometimes|integer|min:1',
|
||||
'end_line' => 'sometimes|integer|min:1',
|
||||
]);
|
||||
|
||||
$terminal = $request->attributes->get('terminal');
|
||||
|
||||
// Find document and verify access through station → knowledge_base
|
||||
$accessibleKbIds = KnowledgeBase::where(function ($q) use ($terminal) {
|
||||
$q->whereDoesntHave('stations'); // global knowledge bases
|
||||
if ($terminal->station_id) {
|
||||
$q->orWhereHas('stations', fn ($sq) => $sq->where('stations.id', $terminal->station_id));
|
||||
}
|
||||
})->where('status', 'active')->pluck('id');
|
||||
|
||||
$document = Document::where('id', $documentId)
|
||||
->whereIn('knowledge_base_id', $accessibleKbIds)
|
||||
->where('conversion_status', 'completed')
|
||||
->first();
|
||||
|
||||
if (!$document) {
|
||||
return response()->json(['error' => 'Document not found'], 404);
|
||||
}
|
||||
|
||||
$content = $document->getMarkdownContent();
|
||||
if ($content === null) {
|
||||
return response()->json(['error' => 'Document content unavailable'], 404);
|
||||
}
|
||||
|
||||
$lines = explode("\n", $content);
|
||||
$totalLines = count($lines);
|
||||
|
||||
$startLine = $request->integer('start_line', 1);
|
||||
$endLine = $request->integer('end_line', min($startLine + 49, $totalLines));
|
||||
$endLine = min($endLine, $totalLines);
|
||||
|
||||
if ($startLine > $totalLines) {
|
||||
return response()->json([
|
||||
'error' => "start_line ({$startLine}) exceeds total lines ({$totalLines})",
|
||||
], 422);
|
||||
}
|
||||
|
||||
$slice = array_slice($lines, $startLine - 1, $endLine - $startLine + 1);
|
||||
|
||||
return response()->json([
|
||||
'id' => $document->id,
|
||||
'title' => $document->title,
|
||||
'total_lines' => $totalLines,
|
||||
'start_line' => $startLine,
|
||||
'end_line' => $endLine,
|
||||
'content' => implode("\n", $slice),
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* POST /api/terminal/heartbeat
|
||||
* 终端心跳上报
|
||||
|
||||
Reference in New Issue
Block a user