Skip to content

Commit

Permalink
need to fix the response types before I can finish this test
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed May 10, 2023
1 parent e2dc902 commit facdbff
Show file tree
Hide file tree
Showing 14 changed files with 164 additions and 92 deletions.
24 changes: 12 additions & 12 deletions app/Http/Controllers/OutboundController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@

use App\Models\Outbound;
use App\Models\Project;
use Illuminate\Http\Request;
use App\ResponseType\ResponseDto;

class OutboundController extends Controller
{
public function __invoke(Project $project, Outbound $outbound)
{
/** @TODO find route binding for this */
if($outbound->project_id !== $project->id) {
if ($outbound->project_id !== $project->id) {
abort(404);
}
$validated = request()->validate([
'question' => ['required', 'max:5000'],
]);

/**
* @TODO they all have to be the same response type
* either a DTO or string
* maybe I need the status too?
* some might put a job on a queue
* some might need to be waited for
* some might use pusher so a queue etc
*/
//$response = outbound run
return response("", 200);
$user = auth()->user();
$request = $validated['question'];

/** @var ResponseDto $response */
$response = $outbound->run($user, $request);

return response($response->response, $response->status);
}
}
46 changes: 35 additions & 11 deletions app/Models/Outbound.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,64 @@
use App\Outbound\OutboundEnum;
use App\ResponseType\BaseResponseType;
use App\ResponseType\ResponseDto;
use App\ResponseType\ResponseTypeEnum;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
* @property ResponseTypeEnum $type
* @property int $project_id
* @property Project $project;
* @property array $prompt_token;
* @property Collection $response_types;
*
* @method Project project()
*/
class Outbound extends Model
{
use HasFactory;

protected ResponseDto $currentResponseDto;

protected $guarded = [];

protected $casts = [
'type' => OutboundEnum::class,
'active' => "bool"
'active' => 'bool',
];

public function project() {
public function project()
{
return $this->belongsTo(Project::class);
}

public function response_types()
{
return $this->hasMany(ResponseType::class)->orderBy('order');
}

/**
* @throws ResponseTypeMissingException
*/
public function run(User $user, string $request): ResponseDto
{
try {
$message = new Message([
'role' => 'user',
'content' => $request,
'user_id' => $user->id,
'project_id' => $this->project_id,
]);

$message = new Message([
'role' => 'user',
'content' => $request,
'user_id' => $user->id,
'project_id' => $this->project_id,
]);
try {

$dto = ResponseDto::from([
'message' => $message,
]);

$this->currentResponseDto = $dto;

foreach ($this->project->response_types as $response_type_model) {
foreach ($this->response_types as $response_type_model) {
$responseType = $response_type_model->type->label();
$responseTypeClass = app("App\ResponseType\Types\\".$responseType, [
'project' => $this->project,
Expand All @@ -57,7 +76,12 @@ public function run(User $user, string $request): ResponseDto
return $this->currentResponseDto;
} catch (\Exception $e) {
logger($e);
throw new ResponseTypeMissingException();

return ResponseDto::from([
'status' => 500,
'message' => $message,
'response' => $e->getMessage(),
]);
}
}
}
5 changes: 0 additions & 5 deletions app/Models/Project.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,6 @@ public function sources()
return $this->hasMany(Source::class)->orderBy('order');
}

public function response_types()
{
return $this->hasMany(ResponseType::class)->orderBy('order');
}

public function outbounds()
{
return $this->hasMany(Outbound::class);
Expand Down
14 changes: 5 additions & 9 deletions app/Models/ResponseType.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,18 @@

namespace App\Models;

use App\Exceptions\ResponseTypeMissingException;
use App\ResponseType\BaseResponseType;
use App\ResponseType\ResponseDto;
use App\ResponseType\ResponseTypeEnum;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

/**
* @property ResponseTypeEnum $type
* @property int $project_id
* @property Project $project;
* @property int $outbound_id
* @property Outbound $outbound;
* @property array $prompt_token;
*
* @method Project project()
* @method Outbound outbound()
*/
class ResponseType extends Model
{
Expand All @@ -30,10 +28,8 @@ class ResponseType extends Model
'type' => ResponseTypeEnum::class,
];

public function project()
public function outbound()
{
return $this->belongsTo(Project::class);
return $this->belongsTo(Outbound::class);
}


}
3 changes: 2 additions & 1 deletion app/ResponseType/ResponseDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ class ResponseDto extends Data
{
public function __construct(
public Message $message,
public mixed $response
public mixed $response,
public int $status = 200
) {
}
}
4 changes: 2 additions & 2 deletions database/factories/ResponseTypeFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Database\Factories;

use App\Models\Project;
use App\Models\Outbound;
use App\ResponseType\ResponseTypeEnum;
use Illuminate\Database\Eloquent\Factories\Factory;

Expand All @@ -22,7 +22,7 @@ public function definition(): array
'order' => fake()->randomDigitNotZero(),
'prompt_token' => [],
'type' => ResponseTypeEnum::ChatUi,
'project_id' => Project::factory(),
'outbound_id' => Outbound::factory(),
];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function up(): void
{
Schema::create('outbounds', function (Blueprint $table) {
$table->id();
$table->string("type")->default(OutboundEnum::ChatUi->value);
$table->boolean("active")->default(1);
$table->string('type')->default(OutboundEnum::ChatUi->value);
$table->boolean('active')->default(1);
$table->foreignIdFor(Project::class);
$table->timestamps();
});
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

use App\Models\Outbound;
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::table('response_types', function (Blueprint $table) {
$table->dropColumn('project_id');
$table->foreignIdFor(Outbound::class);
});
}

/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::table('response_types', function (Blueprint $table) {
//
});
}
};
57 changes: 27 additions & 30 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -1,32 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_DATABASE" value="testing"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
<env name="OPENAI_API_KEY" value="foobar"/>
<env name="OPENAI_MOCK" value="true"/>
</php>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage/>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_DATABASE" value="testing"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
<env name="OPENAI_API_KEY" value="foobar"/>
<env name="OPENAI_MOCK" value="true"/>
</php>
<source>
<include>
<directory suffix=".php">./app</directory>
</include>
</source>
</phpunit>
32 changes: 32 additions & 0 deletions phpunit.xml.bak
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="./vendor/phpunit/phpunit/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./app</directory>
</include>
</coverage>
<php>
<env name="APP_ENV" value="testing"/>
<env name="BCRYPT_ROUNDS" value="4"/>
<env name="CACHE_DRIVER" value="array"/>
<env name="DB_DATABASE" value="testing"/>
<env name="MAIL_MAILER" value="array"/>
<env name="QUEUE_CONNECTION" value="sync"/>
<env name="SESSION_DRIVER" value="array"/>
<env name="TELESCOPE_ENABLED" value="false"/>
<env name="OPENAI_API_KEY" value="foobar"/>
<env name="OPENAI_MOCK" value="true"/>
</php>
</phpunit>
8 changes: 6 additions & 2 deletions tests/Feature/ChatUiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Tests\Feature;

use App\Models\Message;
use App\Models\Outbound;
use App\Models\Project;
use App\Models\ResponseType;
use App\ResponseType\ResponseDto;
Expand Down Expand Up @@ -44,7 +45,7 @@ public function test_can_make_messages()
]);

$this->assertDatabaseCount('messages', 1);
$chatUi = new ChatUi($rt->project, $responseDto);
$chatUi = new ChatUi($rt->outbound->project, $responseDto);
$chatUi->handle($rt);
$this->assertDatabaseCount('messages', 3);
$this->assertNotNull(Message::whereRole('system')->first());
Expand All @@ -59,6 +60,9 @@ public function test_makes_assistant_message()
->andReturn('Foo bar');

$project = Project::factory()->create();
$outbound = Outbound::factory()->create([
'project_id' => $project->id,
]);
$message = Message::factory()->create([
'project_id' => $project->id,
]);
Expand All @@ -76,7 +80,7 @@ public function test_makes_assistant_message()
$rt = ResponseType::factory()
->chatUi()
->create([
'project_id' => $project->id,
'outbound_id' => $outbound->id,
]);

$this->assertDatabaseCount('messages', 2);
Expand Down
4 changes: 2 additions & 2 deletions tests/Feature/Http/Controllers/OutboundControllerTest.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php

it("should take request and kick off things", function () {
it('should take request and kick off things', function () {

});
})->todo();

0 comments on commit facdbff

Please sign in to comment.