Skip to content

Commit

Permalink
add document references
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Apr 28, 2024
1 parent f29aea7 commit cb96e31
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 30 deletions.
19 changes: 19 additions & 0 deletions app/Http/Controllers/DownloadController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace App\Http\Controllers;

use App\Models\Collection;
use Illuminate\Support\Facades\Storage;

class DownloadController extends Controller
{
public function download(Collection $collection)
{
$validate = request()->validate([
'document_name' => 'required|string',
]);

return Storage::disk('collections')
->download($collection->id.'/'.$validate['document_name']);
}
}
1 change: 1 addition & 0 deletions app/Http/Resources/MessageResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function toArray(Request $request): array
'initials' => ($this->from_ai) ? 'Ai' : 'You',
'type' => 'text', //@TODO
'body' => $this->body,
'collection_id' => $this->chat->chatable_id,
'body_markdown' => str($this->body)->markdown(),
'diff_for_humans' => $this->created_at->diffForHumans(),
'message_document_references' => MessageDocumentReferenceResource::collection(
Expand Down
34 changes: 4 additions & 30 deletions resources/js/Pages/Chat/ChatBaloon.vue
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<script setup>
import { TabGroup, TabList, Tab, TabPanels, TabPanel, TransitionRoot } from '@headlessui/vue'
import ReferenceTable from './Components/ReferenceTable.vue'
const props = defineProps({
message: Object
Expand Down Expand Up @@ -40,10 +40,10 @@ const props = defineProps({
<TabGroup >
<TabList class="flex justify-start gap-4 items-center">
<Tab as="template" v-slot="{ selected }">
<div :class="{ 'underline text-gray-800': selected }" class="m4-2 text-gray-500">Message</div>
<div :class="{ 'underline text-gray-800': selected }" class="hover:cursor-pointer m4-2 text-gray-500">Message</div>
</Tab>
<Tab as="template" v-slot="{ selected }">
<div :class="{ 'underline text-gray-800': selected }" class="m4-2 text-gray-500">Sources</div>
<div :class="{ 'underline text-gray-800': selected }" class="hover:cursor-pointer m4-2 text-gray-500">Sources</div>
</Tab>
</TabList>
<TabPanels v-auto-animate>
Expand All @@ -65,35 +65,9 @@ const props = defineProps({
<div class="min-w-full">
<div>
<div class="overflow-x-auto">
<table class="table table-zebra">
<!-- head -->
<thead>
<tr>
<th></th>
<th>Document Name</th>
<th>Page</th>
<th>Distance</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<!-- row 1 -->
<tr v-for="reference in message.message_document_references" :key="reference.id">
<th>{{ reference.id }}</th>
<td>{{ reference.document_name }}</td>
<td>{{ reference.page }}</td>
<td>{{ reference.distance }}</td>
<td>
<span v-html="reference.summary"></span>
</td>

</tr>

</tbody>
</table>
<ReferenceTable :message="message" />
</div>
</div>

</div>
</TabPanel>
</TabPanels>
Expand Down
36 changes: 36 additions & 0 deletions resources/js/Pages/Chat/Components/ReferenceTable.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<template>
<table class="table table-zebra">
<!-- head -->
<thead>
<tr>
<th></th>
<th>Document Name</th>
<th>Page</th>
<th>Distance</th>
<th>Summary</th>
</tr>
</thead>
<tbody>
<!-- row 1 -->
<tr v-for="reference in message.message_document_references" :key="reference.id">
<th>{{ reference.id }}</th>
<td>
<a class="underline" :href="route('download.document', {
collection: message.collection_id,
document_name: reference.document_name
})">{{ reference.document_name }}</a>
</td>
<td>{{ reference.page }}</td>
<td>{{ reference.distance }}</td>
<td>
<span v-html="reference.summary"></span>
</td>
</tr>
</tbody>
</table>
</template>
<script setup>
const props = defineProps({
message: Object
})
</script>
8 changes: 8 additions & 0 deletions routes/web.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

use App\Http\Controllers\ChatController;
use App\Http\Controllers\CollectionController;
use App\Http\Controllers\DownloadController;
use App\Http\Controllers\ExampleChatBotController;
use App\Http\Controllers\ExampleController;
use App\Http\Controllers\ReindexCollectionController;
Expand All @@ -25,6 +26,13 @@
'verified',
])->group(function () {

Route::controller(DownloadController::class)->group(
function () {
Route::get('/collections/{collection}/download', 'download')
->name('download.document');
}
);

Route::get('/dashboard', function () {
return to_route('collections.index');
//return Inertia::render('Dashboard');
Expand Down
30 changes: 30 additions & 0 deletions tests/Feature/Http/Controllers/DownloadControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace Tests\Feature\Http\Controllers;

use App\Models\Collection;
use App\Models\Document;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;

class DownloadControllerTest extends TestCase
{
/**
* A basic feature test example.
*/
public function test_download(): void
{
$this->markTestSkipped('@TODO need to mock better');
Storage::fake('collections');
$user = $this->createUserWithCurrentTeam();
$collection = Collection::factory()->create();
$document = Document::factory()->create([
'collection_id' => $collection->id,
'file_path' => 'test.pdf',
]);
$this->actingAs($user)->get(
route('download.document', ['collection' => $collection,
'document_name' => $document->file_path])
)->assertStatus(200);
}
}

0 comments on commit cb96e31

Please sign in to comment.