Files
BookStack/app/Search/Queries/QueryController.php
Dan Brown 8eef5a1ee7 Vectors: Updated query response to use server-side-events
Allowing the vector query results and the LLM response to each come back
over the same HTTP request at two different times via a somewhat
standard.

Uses a package for JS SSE client, since native browser client does not
support over POST, which is probably important for this endpoint as we
don't want crawlers or other bots abusing this via accidentally.
2025-08-21 16:03:55 +01:00

54 lines
1.5 KiB
PHP

<?php
namespace BookStack\Search\Queries;
use BookStack\Http\Controller;
use BookStack\Search\SearchOptions;
use BookStack\Search\SearchRunner;
use Illuminate\Http\Request;
class QueryController extends Controller
{
public function __construct(
protected SearchRunner $searchRunner,
) {
}
/**
* Show the view to start a vector/LLM-based query search.
*/
public function show(Request $request)
{
// TODO - Validate if query system is active
$query = $request->get('ask', '');
// TODO - Placeholder
$entities = $this->searchRunner->searchEntities(SearchOptions::fromString("cat"), 'all', 1, 20)['results'];
// TODO - Set page title
return view('search.query', [
'query' => $query,
'entities' => $entities,
]);
}
/**
* Perform a vector/LLM-based query search.
*/
public function run(Request $request, VectorSearchRunner $searchRunner, LlmQueryRunner $llmRunner)
{
// TODO - Validate if query system is active
$query = $request->get('query', '');
return response()->eventStream(function () use ($query, $searchRunner, $llmRunner) {
$results = $query ? $searchRunner->run($query) : [];
$count = count($results);
yield "Found {$count} results for query: {$query}!";
$llmResult = $llmRunner->run($query, $results);
yield "LLM result: {$llmResult}";
});
}
}