Skip to content

Commit

Permalink
EmailReplyOuput working
Browse files Browse the repository at this point in the history
  • Loading branch information
alnutile committed Jun 18, 2024
1 parent ceaddaa commit be3159d
Show file tree
Hide file tree
Showing 38 changed files with 587 additions and 66 deletions.
3 changes: 0 additions & 3 deletions Modules/LlmDriver/app/Functions/SearchAndSummarize.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,6 @@ public function handle(
*/
foreach ($documentChunkResults as $result) {
$contentString = remove_ascii($result->content);
if (Feature::active('reduce_text')) {
$result = reduce_text_size($contentString);
}
$content[] = $contentString; //reduce_text_size seem to mess up Claude?
}

Expand Down
1 change: 0 additions & 1 deletion Modules/LlmDriver/app/GroqClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,6 @@ public function functionPromptChat(array $messages, array $only = []): array
foreach ($functionArray as $possibleFunction) {
$functions[] = $possibleFunction;
}

}

/**
Expand Down
4 changes: 2 additions & 2 deletions app/Domains/EmailParser/CredentialsDto.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ public function __construct(
public string $password,
public string $host,
public string $port,
public string $protocol,
public string $encryption,
public string $encryption = 'ssl',
public string $protocol = 'imap',
public string $email_box = 'Inbox'
) {
}
Expand Down
48 changes: 32 additions & 16 deletions app/Domains/EmailParser/EmailClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public function handle(CredentialsDto $credentials, bool $delete = true): array
$foldersToCheck = explode(',', trim($credentials->email_box));

$foldersToCheck = collect($foldersToCheck)->map(function ($folder) {
return str($folder)->lower()->toString();
return str($folder)->toString();
})->toArray();

$config = [
Expand Down Expand Up @@ -80,30 +80,46 @@ public function handle(CredentialsDto $credentials, bool $delete = true): array
'folders_to_check' => $foldersToCheck,
]);

$full_name = str($full_name)->lower()->toString();
$full_name = str($full_name)->toString();

if (in_array($full_name, $foldersToCheck)) {
Log::info('Found Folder', [
'full_name' => $full_name,
'folders_to_check' => $foldersToCheck,
]);

$messages = $folder->messages()->all()->get();
logger('[LaraChain] - Email Box Count', [

Log::info('[LaraChain] - Email Box Count', [
'count' => $messages->count(),
'folder' => $full_name,
]);

/** @var Message $message */
foreach ($messages as $message) {
$messageDto = MailDto::from([
'to' => $message->getTo()->toString(),
'from' => $message->getFrom()->toString(),
'body' => $message->getTextBody(),
'subject' => $message->getSubject(),
'date' => $message->getDate()->toString(),
'header' => $message->getHeader()->raw,
]);

$mail[] = $messageDto;

if ($delete) {
$message->delete(expunge: true);
$flags = $message->getFlags();

if (! $flags->contains('Seen')) {
$messageDto = MailDto::from([
'to' => $message->getTo()->toString(),
'from' => $message->getFrom()->toString(),
'body' => $message->getTextBody(),
'subject' => $message->getSubject(),
'date' => $message->getDate()->toString(),
'header' => $message->getHeader()->raw,
]);

$mail[] = $messageDto;

if ($delete) {
$message->delete(expunge: true);
} else {
$message->addFlag('Seen');
}
} else {
Log::info('[LaraChain] - Flag Seen', [
'flags' => $flags->toArray(),
]);
}
}

Expand Down
13 changes: 12 additions & 1 deletion app/Domains/Outputs/EmailReplyOutput.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Facades\App\Domains\EmailParser\EmailClient;
use Illuminate\Bus\Batch;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Log;
use LlmLaraHub\LlmDriver\LlmDriverFacade;

class EmailReplyOutput extends BaseOutput
Expand All @@ -22,10 +23,15 @@ public function handle(Output $output): void
* Webhooks
*/
$credentials = CredentialsDto::from($output->secrets);
$mails = EmailClient::handle($credentials);
$mails = EmailClient::handle($credentials, false);

$replies = [];

Log::info('[LaraChain] - EmailReplyOutput', [
'output' => $output->id,
'emails_found' => count($mails),
]);

foreach ($mails as $mailDto) {
$replies[] = new EmailReplyOutputJob($output, $mailDto);
}
Expand All @@ -37,7 +43,12 @@ public function handle(Output $output): void
})
->then(function (Batch $batch) {
})->catch(function (Batch $batch, \Throwable $e) {
Log::error('[LaraChain] - Error running Email Reply Output', [
'error' => $e->getMessage(),
'batch' => $batch->toArray(),
]);
})->finally(function (Batch $batch) {
//more here
})
->onQueue(
LlmDriverFacade::driver($output->collection->getDriver())->onQueue()
Expand Down
2 changes: 1 addition & 1 deletion app/Domains/Prompts/EmailReplyPrompt.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static function prompt(string $context, string $emailBeingRepliedTo): str
**Task**
Using the given context and the email that was sent to you, answer the question.
**Format**
Text output that will later be used in an email.
Text output that will later be used in an email. Just the body no salutation
**Email being replied to**
$emailBeingRepliedTo
Expand Down
5 changes: 5 additions & 0 deletions app/Http/Controllers/OutputController.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
use App\Http\Resources\CollectionResource;
use App\Http\Resources\DocumentResource;
use App\Http\Resources\OutputResource;
use App\Http\Resources\PersonaResource;
use App\Http\Resources\PublicOutputResource;
use App\Models\Collection;
use App\Models\Document;
use App\Models\Output;
use App\Models\Persona;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Gate;
use LlmLaraHub\LlmDriver\Requests\MessageInDto;
Expand Down Expand Up @@ -64,6 +66,7 @@ public function edit(Collection $collection, Output $output)
{
return inertia($this->edit_path, [
'output' => $output,
'personas' => PersonaResource::collection(Persona::orderBy('name')->get()),
'recurring' => RecurringTypeEnum::selectOptions(),
'collection' => new CollectionResource($collection),
'prompts' => $this->getPrompts(),
Expand Down Expand Up @@ -109,6 +112,7 @@ public function create(Collection $collection)
return inertia($this->create_path, [
'info' => $this->info,
'type' => $this->type,
'personas' => PersonaResource::collection(Persona::all()),
'recurring' => RecurringTypeEnum::selectOptions(),
'collection' => new CollectionResource($collection),
'prompts' => $this->getPrompts(),
Expand Down Expand Up @@ -168,6 +172,7 @@ protected function getValidationRules(): array
{
return [
'title' => 'required|string',
'persona_id' => ['nullable', 'integer'],
'summary' => 'required|string',
'active' => 'boolean|nullable',
'public' => 'boolean|nullable',
Expand Down
70 changes: 32 additions & 38 deletions app/Http/Controllers/Outputs/EmailReplyOutputController.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
use App\Http\Controllers\OutputController;
use App\Models\Collection;
use App\Models\Output;
use Facades\App\Domains\Outputs\EmailReplyOutput;

class EmailReplyOutputController extends OutputController
{
Expand All @@ -24,15 +25,10 @@ class EmailReplyOutputController extends OutputController

protected function getValidationRules(): array
{
return [
'title' => 'required|string',
'summary' => 'required|string',
'active' => 'boolean|nullable',
'public' => 'boolean|nullable',
'recurring' => 'string|nullable',

return array_merge(parent::getValidationRules(), [
'meta_data.signature' => ['required', 'string'],
'secrets' => ['required', 'array'],
];
]);
}

public function getPrompts(): array
Expand All @@ -52,43 +48,41 @@ public function updateOutput(Output $output, array $validated): void
'protocol' => data_get($validated, 'secrets.protocol', 'imap'),
'encryption' => data_get($validated, 'secrets.encryption', 'ssl'),
'delete' => data_get($validated, 'secrets.delete', false),
'email_box' => data_get($validated, 'secrets.email_box', 'Inbox'),
];

$validated['secrets'] = $secrets;
$output->update($validated);
}

protected function makeOutput(array $validated, Collection $collection): void
{

$secrets = [
'username' => data_get($validated, 'secrets.username', null),
'password' => data_get($validated, 'secrets.password', null),
'host' => data_get($validated, 'secrets.host', null),
'port' => data_get($validated, 'secrets.port', '993'),
'protocol' => data_get($validated, 'secrets.protocol', 'imap'),
'encryption' => data_get($validated, 'secrets.encryption', 'ssl'),
'delete' => data_get($validated, 'secrets.delete', false),
'email_box' => data_get($validated, 'secrets.email_box', null),
];

$output->meta_data = $validated['meta_data'];
$output->secrets = $secrets;
$validated['secrets'] = $secrets;

$output->updateQuietly();
$validated['collection_id'] = $collection->id;
$validated['type'] = $this->outputTypeEnum;

$output->update([
'title' => $validated['title'],
'summary' => $validated['summary'],
'recurring' => $validated['recurring'],
'active' => $validated['active'],
]);
Output::create($validated);
}

protected function makeOutput(array $validated, Collection $collection): void
public function check(Output $output)
{
Output::create([
'title' => $validated['title'],
'summary' => $validated['summary'],
'collection_id' => $collection->id,
'recurring' => data_get($validated, 'recurring', null),
'active' => data_get($validated, 'active', false),
'public' => data_get($validated, 'public', false),
'type' => $this->outputTypeEnum,
'meta_data' => data_get($validated, 'meta_data', []),
'secrets' => [
'username' => data_get($validated, 'secrets.username', null),
'password' => data_get($validated, 'secrets.password', null),
'host' => data_get($validated, 'secrets.host', null),
'port' => data_get($validated, 'secrets.port', '993'),
'protocol' => data_get($validated, 'secrets.protocol', 'imap'),
'encryption' => data_get($validated, 'secrets.encryption', 'ssl'),
'delete' => data_get($validated, 'secrets.delete', false),
'email_box' => data_get($validated, 'secrets.email_box', null),
],
]);
EmailReplyOutput::handle($output);

request()->session()->flash('flash.banner', 'Checking box sending replies');

return back();
}
}
1 change: 1 addition & 0 deletions app/Http/Resources/OutputResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ public function toArray(Request $request): array
'active' => $this->active,
'public' => $this->public,
'recurring' => $recurring,
'persona' => $this->persona,
'meta_data' => $this->meta_data,
'last_run' => $lastRun,
'slug' => $this->slug,
Expand Down
Loading

0 comments on commit be3159d

Please sign in to comment.