Skip to content

Commit be3159d

Browse files
committed
EmailReplyOuput working
1 parent ceaddaa commit be3159d

38 files changed

+587
-66
lines changed

Modules/LlmDriver/app/Functions/SearchAndSummarize.php

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,6 @@ public function handle(
7777
*/
7878
foreach ($documentChunkResults as $result) {
7979
$contentString = remove_ascii($result->content);
80-
if (Feature::active('reduce_text')) {
81-
$result = reduce_text_size($contentString);
82-
}
8380
$content[] = $contentString; //reduce_text_size seem to mess up Claude?
8481
}
8582

Modules/LlmDriver/app/GroqClient.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -214,7 +214,6 @@ public function functionPromptChat(array $messages, array $only = []): array
214214
foreach ($functionArray as $possibleFunction) {
215215
$functions[] = $possibleFunction;
216216
}
217-
218217
}
219218

220219
/**

app/Domains/EmailParser/CredentialsDto.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@ public function __construct(
1111
public string $password,
1212
public string $host,
1313
public string $port,
14-
public string $protocol,
15-
public string $encryption,
14+
public string $encryption = 'ssl',
15+
public string $protocol = 'imap',
1616
public string $email_box = 'Inbox'
1717
) {
1818
}

app/Domains/EmailParser/EmailClient.php

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public function handle(CredentialsDto $credentials, bool $delete = true): array
4242
$foldersToCheck = explode(',', trim($credentials->email_box));
4343

4444
$foldersToCheck = collect($foldersToCheck)->map(function ($folder) {
45-
return str($folder)->lower()->toString();
45+
return str($folder)->toString();
4646
})->toArray();
4747

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

83-
$full_name = str($full_name)->lower()->toString();
83+
$full_name = str($full_name)->toString();
8484

8585
if (in_array($full_name, $foldersToCheck)) {
86+
Log::info('Found Folder', [
87+
'full_name' => $full_name,
88+
'folders_to_check' => $foldersToCheck,
89+
]);
90+
8691
$messages = $folder->messages()->all()->get();
87-
logger('[LaraChain] - Email Box Count', [
92+
93+
Log::info('[LaraChain] - Email Box Count', [
8894
'count' => $messages->count(),
8995
'folder' => $full_name,
9096
]);
9197

9298
/** @var Message $message */
9399
foreach ($messages as $message) {
94-
$messageDto = MailDto::from([
95-
'to' => $message->getTo()->toString(),
96-
'from' => $message->getFrom()->toString(),
97-
'body' => $message->getTextBody(),
98-
'subject' => $message->getSubject(),
99-
'date' => $message->getDate()->toString(),
100-
'header' => $message->getHeader()->raw,
101-
]);
102-
103-
$mail[] = $messageDto;
104-
105-
if ($delete) {
106-
$message->delete(expunge: true);
100+
$flags = $message->getFlags();
101+
102+
if (! $flags->contains('Seen')) {
103+
$messageDto = MailDto::from([
104+
'to' => $message->getTo()->toString(),
105+
'from' => $message->getFrom()->toString(),
106+
'body' => $message->getTextBody(),
107+
'subject' => $message->getSubject(),
108+
'date' => $message->getDate()->toString(),
109+
'header' => $message->getHeader()->raw,
110+
]);
111+
112+
$mail[] = $messageDto;
113+
114+
if ($delete) {
115+
$message->delete(expunge: true);
116+
} else {
117+
$message->addFlag('Seen');
118+
}
119+
} else {
120+
Log::info('[LaraChain] - Flag Seen', [
121+
'flags' => $flags->toArray(),
122+
]);
107123
}
108124
}
109125

app/Domains/Outputs/EmailReplyOutput.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use Facades\App\Domains\EmailParser\EmailClient;
99
use Illuminate\Bus\Batch;
1010
use Illuminate\Support\Facades\Bus;
11+
use Illuminate\Support\Facades\Log;
1112
use LlmLaraHub\LlmDriver\LlmDriverFacade;
1213

1314
class EmailReplyOutput extends BaseOutput
@@ -22,10 +23,15 @@ public function handle(Output $output): void
2223
* Webhooks
2324
*/
2425
$credentials = CredentialsDto::from($output->secrets);
25-
$mails = EmailClient::handle($credentials);
26+
$mails = EmailClient::handle($credentials, false);
2627

2728
$replies = [];
2829

30+
Log::info('[LaraChain] - EmailReplyOutput', [
31+
'output' => $output->id,
32+
'emails_found' => count($mails),
33+
]);
34+
2935
foreach ($mails as $mailDto) {
3036
$replies[] = new EmailReplyOutputJob($output, $mailDto);
3137
}
@@ -37,7 +43,12 @@ public function handle(Output $output): void
3743
})
3844
->then(function (Batch $batch) {
3945
})->catch(function (Batch $batch, \Throwable $e) {
46+
Log::error('[LaraChain] - Error running Email Reply Output', [
47+
'error' => $e->getMessage(),
48+
'batch' => $batch->toArray(),
49+
]);
4050
})->finally(function (Batch $batch) {
51+
//more here
4152
})
4253
->onQueue(
4354
LlmDriverFacade::driver($output->collection->getDriver())->onQueue()

app/Domains/Prompts/EmailReplyPrompt.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public static function prompt(string $context, string $emailBeingRepliedTo): str
1616
**Task**
1717
Using the given context and the email that was sent to you, answer the question.
1818
**Format**
19-
Text output that will later be used in an email.
19+
Text output that will later be used in an email. Just the body no salutation
2020
2121
**Email being replied to**
2222
$emailBeingRepliedTo

app/Http/Controllers/OutputController.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99
use App\Http\Resources\CollectionResource;
1010
use App\Http\Resources\DocumentResource;
1111
use App\Http\Resources\OutputResource;
12+
use App\Http\Resources\PersonaResource;
1213
use App\Http\Resources\PublicOutputResource;
1314
use App\Models\Collection;
1415
use App\Models\Document;
1516
use App\Models\Output;
17+
use App\Models\Persona;
1618
use Illuminate\Support\Arr;
1719
use Illuminate\Support\Facades\Gate;
1820
use LlmLaraHub\LlmDriver\Requests\MessageInDto;
@@ -64,6 +66,7 @@ public function edit(Collection $collection, Output $output)
6466
{
6567
return inertia($this->edit_path, [
6668
'output' => $output,
69+
'personas' => PersonaResource::collection(Persona::orderBy('name')->get()),
6770
'recurring' => RecurringTypeEnum::selectOptions(),
6871
'collection' => new CollectionResource($collection),
6972
'prompts' => $this->getPrompts(),
@@ -109,6 +112,7 @@ public function create(Collection $collection)
109112
return inertia($this->create_path, [
110113
'info' => $this->info,
111114
'type' => $this->type,
115+
'personas' => PersonaResource::collection(Persona::all()),
112116
'recurring' => RecurringTypeEnum::selectOptions(),
113117
'collection' => new CollectionResource($collection),
114118
'prompts' => $this->getPrompts(),
@@ -168,6 +172,7 @@ protected function getValidationRules(): array
168172
{
169173
return [
170174
'title' => 'required|string',
175+
'persona_id' => ['nullable', 'integer'],
171176
'summary' => 'required|string',
172177
'active' => 'boolean|nullable',
173178
'public' => 'boolean|nullable',

app/Http/Controllers/Outputs/EmailReplyOutputController.php

Lines changed: 32 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use App\Http\Controllers\OutputController;
88
use App\Models\Collection;
99
use App\Models\Output;
10+
use Facades\App\Domains\Outputs\EmailReplyOutput;
1011

1112
class EmailReplyOutputController extends OutputController
1213
{
@@ -24,15 +25,10 @@ class EmailReplyOutputController extends OutputController
2425

2526
protected function getValidationRules(): array
2627
{
27-
return [
28-
'title' => 'required|string',
29-
'summary' => 'required|string',
30-
'active' => 'boolean|nullable',
31-
'public' => 'boolean|nullable',
32-
'recurring' => 'string|nullable',
28+
29+
return array_merge(parent::getValidationRules(), [
3330
'meta_data.signature' => ['required', 'string'],
34-
'secrets' => ['required', 'array'],
35-
];
31+
]);
3632
}
3733

3834
public function getPrompts(): array
@@ -52,43 +48,41 @@ public function updateOutput(Output $output, array $validated): void
5248
'protocol' => data_get($validated, 'secrets.protocol', 'imap'),
5349
'encryption' => data_get($validated, 'secrets.encryption', 'ssl'),
5450
'delete' => data_get($validated, 'secrets.delete', false),
51+
'email_box' => data_get($validated, 'secrets.email_box', 'Inbox'),
52+
];
53+
54+
$validated['secrets'] = $secrets;
55+
$output->update($validated);
56+
}
57+
58+
protected function makeOutput(array $validated, Collection $collection): void
59+
{
60+
61+
$secrets = [
62+
'username' => data_get($validated, 'secrets.username', null),
63+
'password' => data_get($validated, 'secrets.password', null),
64+
'host' => data_get($validated, 'secrets.host', null),
65+
'port' => data_get($validated, 'secrets.port', '993'),
66+
'protocol' => data_get($validated, 'secrets.protocol', 'imap'),
67+
'encryption' => data_get($validated, 'secrets.encryption', 'ssl'),
68+
'delete' => data_get($validated, 'secrets.delete', false),
5569
'email_box' => data_get($validated, 'secrets.email_box', null),
5670
];
5771

58-
$output->meta_data = $validated['meta_data'];
59-
$output->secrets = $secrets;
72+
$validated['secrets'] = $secrets;
6073

61-
$output->updateQuietly();
74+
$validated['collection_id'] = $collection->id;
75+
$validated['type'] = $this->outputTypeEnum;
6276

63-
$output->update([
64-
'title' => $validated['title'],
65-
'summary' => $validated['summary'],
66-
'recurring' => $validated['recurring'],
67-
'active' => $validated['active'],
68-
]);
77+
Output::create($validated);
6978
}
7079

71-
protected function makeOutput(array $validated, Collection $collection): void
80+
public function check(Output $output)
7281
{
73-
Output::create([
74-
'title' => $validated['title'],
75-
'summary' => $validated['summary'],
76-
'collection_id' => $collection->id,
77-
'recurring' => data_get($validated, 'recurring', null),
78-
'active' => data_get($validated, 'active', false),
79-
'public' => data_get($validated, 'public', false),
80-
'type' => $this->outputTypeEnum,
81-
'meta_data' => data_get($validated, 'meta_data', []),
82-
'secrets' => [
83-
'username' => data_get($validated, 'secrets.username', null),
84-
'password' => data_get($validated, 'secrets.password', null),
85-
'host' => data_get($validated, 'secrets.host', null),
86-
'port' => data_get($validated, 'secrets.port', '993'),
87-
'protocol' => data_get($validated, 'secrets.protocol', 'imap'),
88-
'encryption' => data_get($validated, 'secrets.encryption', 'ssl'),
89-
'delete' => data_get($validated, 'secrets.delete', false),
90-
'email_box' => data_get($validated, 'secrets.email_box', null),
91-
],
92-
]);
82+
EmailReplyOutput::handle($output);
83+
84+
request()->session()->flash('flash.banner', 'Checking box sending replies');
85+
86+
return back();
9387
}
9488
}

app/Http/Resources/OutputResource.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public function toArray(Request $request): array
4242
'active' => $this->active,
4343
'public' => $this->public,
4444
'recurring' => $recurring,
45+
'persona' => $this->persona,
4546
'meta_data' => $this->meta_data,
4647
'last_run' => $lastRun,
4748
'slug' => $this->slug,

0 commit comments

Comments
 (0)