Skip to content

Commit 1bb2f71

Browse files
committed
fix webhook
1 parent a1b032e commit 1bb2f71

File tree

2 files changed

+37
-109
lines changed

2 files changed

+37
-109
lines changed

app/Domains/Sources/WebhookSource.php

+13-65
Original file line numberDiff line numberDiff line change
@@ -4,21 +4,15 @@
44

55
use App\Domains\Documents\StatusEnum;
66
use App\Domains\Documents\TypesEnum;
7-
use App\Helpers\TextChunker;
8-
use App\Jobs\DocumentProcessingCompleteJob;
9-
use App\Jobs\SummarizeDocumentJob;
10-
use App\Jobs\VectorlizeDataJob;
7+
use App\Jobs\ChunkDocumentJob;
118
use App\Models\Document;
12-
use App\Models\DocumentChunk;
9+
use App\Models\Message;
1310
use App\Models\Source;
11+
use Facades\App\Domains\Orchestration\OrchestrateVersionTwo;
1412
use Facades\App\Domains\Tokenizer\Templatizer;
15-
use Illuminate\Bus\Batch;
1613
use Illuminate\Support\Arr;
1714
use Illuminate\Support\Facades\Bus;
1815
use Illuminate\Support\Facades\Log;
19-
use LlmLaraHub\LlmDriver\Functions\ToolTypes;
20-
use LlmLaraHub\LlmDriver\LlmDriverFacade;
21-
use LlmLaraHub\TagFunction\Jobs\TagDocumentJob;
2216

2317
class WebhookSource extends BaseSource
2418
{
@@ -63,21 +57,22 @@ public function handle(Source $source): void
6357
$prompt = Templatizer::appendContext(true)
6458
->handle($this->source->getPrompt(), $encoded);
6559

66-
$results = LlmDriverFacade::driver(
67-
$source->getDriver()
68-
)->setToolType(ToolTypes::Source)
69-
->completion($prompt);
60+
/** @var Message $assistantMessage */
61+
$assistantMessage = OrchestrateVersionTwo::sourceOrchestrate(
62+
$source->refresh()->chat,
63+
$prompt
64+
);
7065

71-
if ($this->ifNotActionRequired($results->content)) {
66+
if ($this->ifNotActionRequired($assistantMessage->getContent())) {
7267
Log::info('[LaraChain] - Webhook Skipping', [
7368
'prompt' => $prompt,
7469
]);
7570
} else {
7671
Log::info('[LaraChain] - WebhookSource Transformation Results', [
77-
'results' => $results,
72+
'assistant_message' => $assistantMessage->id,
7873
]);
7974

80-
$promptResultsOriginal = $results->content;
75+
$promptResultsOriginal = $assistantMessage->getContent();
8176

8277
$this->addUserMessage($source, $promptResultsOriginal);
8378

@@ -86,10 +81,6 @@ public function handle(Source $source): void
8681
foreach ($promptResults as $promptResultIndex => $promptResult) {
8782
$promptResult = json_encode($promptResult);
8883

89-
/**
90-
* Could even do ONE more look at the data
91-
* with the Source Prompt and LLM
92-
*/
9384
$title = sprintf('WebhookSource - item #%d source: %s',
9485
$promptResultIndex + 1, md5($promptResult));
9586

@@ -106,52 +97,9 @@ public function handle(Source $source): void
10697
'original_content' => $promptResult,
10798
]);
10899

109-
$page_number = 1;
110-
111-
$chunked_chunks = TextChunker::handle($promptResult);
112-
113-
$chunks = [];
114-
115-
foreach ($chunked_chunks as $chunkSection => $chunkContent) {
116-
$guid = md5($chunkContent);
117-
118-
$DocumentChunk = DocumentChunk::updateOrCreate(
119-
[
120-
'document_id' => $document->id,
121-
'guid' => $guid,
122-
],
123-
[
124-
'sort_order' => $page_number,
125-
'section_number' => $chunkSection,
126-
'content' => to_utf8($chunkContent),
127-
'original_content' => to_utf8($chunkContent),
128-
]
129-
);
130-
131-
Log::info('[LaraLlama] WebhookSource adding to new batch');
132-
133-
$chunks[] = new VectorlizeDataJob($DocumentChunk);
134-
135-
$page_number++;
136-
}
137-
138-
Bus::batch($chunks)
139-
->name("Chunking Document from WebhookSource - {$this->source->id}")
100+
Bus::batch([new ChunkDocumentJob($document)])
101+
->name('Processing '.$title)
140102
->allowFailures()
141-
->finally(function (Batch $batch) use ($document) {
142-
Bus::batch([
143-
[
144-
new SummarizeDocumentJob($document),
145-
new TagDocumentJob($document),
146-
new DocumentProcessingCompleteJob($document),
147-
],
148-
])
149-
->name(sprintf('Final Document Steps Document %s id %d', $document->type->name, $document->id))
150-
->allowFailures()
151-
->onQueue(LlmDriverFacade::driver($document->getDriver())->onQueue())
152-
->dispatch();
153-
})
154-
->onQueue(LlmDriverFacade::driver($this->source->getDriver())->onQueue())
155103
->dispatch();
156104
}
157105

tests/Feature/WebhookSourceTest.php

+24-44
Original file line numberDiff line numberDiff line change
@@ -16,26 +16,21 @@ public function test_handle()
1616

1717
$payload = get_fixture('example_github.json');
1818

19-
LlmDriverFacade::shouldReceive('driver->onQueue')
20-
->twice()->andReturn('default');
21-
22-
LlmDriverFacade::shouldReceive('driver->completion')
23-
->once()->andReturn(
24-
CompletionResponse::from([
25-
'content' => get_fixture('github_transformed.json', false),
26-
])
27-
);
19+
LlmDriverFacade::shouldReceive('driver->setToolType->chat')->once()->andReturn(
20+
CompletionResponse::from([
21+
'content' => 'foo bar',
22+
])
23+
);
2824

2925
$source = Source::factory()->create();
3026

3127
(new \App\Domains\Sources\WebhookSource())
3228
->payload($payload)
3329
->handle($source);
3430

35-
$this->assertDatabaseCount('documents', 2);
36-
$this->assertDatabaseCount('document_chunks', 2);
31+
$this->assertDatabaseCount('documents', 1);
3732

38-
Bus::assertBatchCount(2);
33+
Bus::assertBatchCount(1);
3934

4035
}
4136

@@ -45,15 +40,11 @@ public function test_non_json()
4540

4641
$payload = get_fixture('example_github.json');
4742

48-
LlmDriverFacade::shouldReceive('driver->onQueue')
49-
->once()->andReturn('default');
50-
51-
LlmDriverFacade::shouldReceive('driver->completion')
52-
->once()->andReturn(
53-
CompletionResponse::from([
54-
'content' => 'Foo Bar',
55-
])
56-
);
43+
LlmDriverFacade::shouldReceive('driver->setToolType->chat')->once()->andReturn(
44+
CompletionResponse::from([
45+
'content' => 'foo bar',
46+
])
47+
);
5748

5849
$source = Source::factory()->create();
5950

@@ -62,7 +53,6 @@ public function test_non_json()
6253
->handle($source);
6354

6455
$this->assertDatabaseCount('documents', 1);
65-
$this->assertDatabaseCount('document_chunks', 1);
6656

6757
Bus::assertBatchCount(1);
6858

@@ -74,15 +64,11 @@ public function test_prevent_duplicates_github()
7464

7565
$payload = get_fixture('example_github.json');
7666

77-
LlmDriverFacade::shouldReceive('driver->onQueue')
78-
->times(2)->andReturn('default');
79-
80-
LlmDriverFacade::shouldReceive('driver->completion')
81-
->once()->andReturn(
82-
CompletionResponse::from([
83-
'content' => get_fixture('github_transformed.json', false),
84-
])
85-
);
67+
LlmDriverFacade::shouldReceive('driver->setToolType->chat')->once()->andReturn(
68+
CompletionResponse::from([
69+
'content' => 'foo bar',
70+
])
71+
);
8672

8773
$source = Source::factory()->create();
8874

@@ -94,10 +80,9 @@ public function test_prevent_duplicates_github()
9480
->payload($payload)
9581
->handle($source);
9682

97-
$this->assertDatabaseCount('documents', 2);
98-
$this->assertDatabaseCount('document_chunks', 2);
83+
$this->assertDatabaseCount('documents', 1);
9984

100-
Bus::assertBatchCount(2);
85+
Bus::assertBatchCount(1);
10186

10287
}
10388

@@ -110,15 +95,11 @@ public function test_prevent_duplicates_statamic()
11095
$payload['id'] = 'fake_id';
11196
$payload['content'] = $payload;
11297

113-
LlmDriverFacade::shouldReceive('driver->onQueue')
114-
->once()->andReturn('default');
115-
116-
LlmDriverFacade::shouldReceive('driver->completion')
117-
->times(1)->andReturn(
118-
CompletionResponse::from([
119-
'content' => 'Foo Bar',
120-
])
121-
);
98+
LlmDriverFacade::shouldReceive('driver->setToolType->chat')->once()->andReturn(
99+
CompletionResponse::from([
100+
'content' => 'foo bar',
101+
])
102+
);
122103

123104
$source = Source::factory()->create();
124105

@@ -131,7 +112,6 @@ public function test_prevent_duplicates_statamic()
131112
->handle($source);
132113

133114
$this->assertDatabaseCount('documents', 1);
134-
$this->assertDatabaseCount('document_chunks', 1);
135115

136116
Bus::assertBatchCount(1);
137117

0 commit comments

Comments
 (0)