lwazi 1.8.2 → 1.8.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "lwazi",
3
- "version": "1.8.2",
3
+ "version": "1.8.4",
4
4
  "description": "Lwazi is an AI assistant for Laravel. Install with one command to add an AI assistant to your Laravel app.",
5
5
  "main": "bin/lwazi.js",
6
6
  "bin": {
@@ -0,0 +1,84 @@
1
+ <?php
2
+
3
+ namespace Lwazi\Core\Console;
4
+
5
+ use Illuminate\Console\Command;
6
+ use Illuminate\Support\Facades\File;
7
+ use Lwazi\Core\Services\ContentIndexer;
8
+
9
+ class BuildContentIndexCommand extends Command
10
+ {
11
+ protected $signature = 'lwazi:build-content-index';
12
+ protected $description = 'Build content index from blade files for website search';
13
+
14
+ public function handle(): int
15
+ {
16
+ $this->info('Building content index from blade files...');
17
+
18
+ $viewsPath = resource_path('views');
19
+ if (!is_dir($viewsPath)) {
20
+ $this->error('Views directory not found');
21
+ return Command::FAILURE;
22
+ }
23
+
24
+ $indexer = new ContentIndexer();
25
+ $this->indexBladeFiles($viewsPath, $indexer);
26
+
27
+ $docCount = $indexer->getDocumentCount();
28
+ $this->info('Indexed ' . $docCount . ' pages');
29
+
30
+ $data = $indexer->toArray();
31
+ $data['generated_at'] = now()->toIso8601String();
32
+
33
+ $path = storage_path('lwazi/content_index.json');
34
+ if (!is_dir(dirname($path))) {
35
+ mkdir(dirname($path), 0755, true);
36
+ }
37
+
38
+ file_put_contents($path, json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES));
39
+
40
+ $this->info("Content index saved to: {$path}");
41
+
42
+ return Command::SUCCESS;
43
+ }
44
+
45
+ protected function indexBladeFiles(string $path, ContentIndexer $indexer): void
46
+ {
47
+ $files = File::allFiles($path);
48
+
49
+ foreach ($files as $file) {
50
+ $ext = $file->getExtension();
51
+ if ($ext !== 'blade.php' && $ext !== 'php') {
52
+ continue;
53
+ }
54
+
55
+ $filePath = $file->getPathname();
56
+ $relativePath = str_replace($path . '/', '', $filePath);
57
+
58
+ if (str_contains($relativePath, 'admin/') || str_contains($relativePath, '/admin.')) {
59
+ continue;
60
+ }
61
+
62
+ $content = File::get($filePath);
63
+
64
+ $content = preg_replace('/@php.*?@endphp/s', '', $content);
65
+ $content = preg_replace('/@section.*?@endsection/s', '', $content);
66
+ $content = preg_replace('/@yield.*?(?=\s|$)/', '', $content);
67
+ $content = preg_replace('/\{\{.*?\}\}/', '', $content);
68
+ $content = preg_replace('/\{!!.*?!!\}/', '', $content);
69
+ $content = strip_tags($content);
70
+ $content = preg_replace('/\s+/', ' ', $content);
71
+ $content = trim($content);
72
+
73
+ if (strlen($content) < 20) {
74
+ continue;
75
+ }
76
+
77
+ $pathName = '/' . str_replace(['.blade.php', '.php', '/'], ['', '', '-'], $relativePath);
78
+ $pathName = preg_replace('/-+/', '-', $pathName);
79
+ $pathName = rtrim($pathName, '-') ?: '/';
80
+
81
+ $indexer->indexPage($pathName, '<html><body><p>' . $content . '</p></body></html>');
82
+ }
83
+ }
84
+ }
@@ -338,8 +338,22 @@ class ProjectAnalyzer
338
338
  $pages = $this->knowledge['pages'] ?? [];
339
339
  $routes = $this->knowledge['routes'] ?? [];
340
340
 
341
+ $publicRoutes = array_filter($routes, function($r) {
342
+ $uri = $r['uri'] ?? '';
343
+ $name = $r['name'] ?? '';
344
+
345
+ if (str_starts_with($uri, '/admin') || str_starts_with($uri, '/api') || str_starts_with($uri, '/_')) {
346
+ return false;
347
+ }
348
+ if ($name && (str_starts_with($name, 'admin.') || str_starts_with($name, 'api.'))) {
349
+ return false;
350
+ }
351
+
352
+ return true;
353
+ });
354
+
341
355
  $tree->buildFromPages($pages);
342
- $tree->buildFromRoutes($routes);
356
+ $tree->buildFromRoutes(array_values($publicRoutes));
343
357
 
344
358
  $this->knowledge['navigation_tree'] = $tree->toArray();
345
359
 
@@ -13,6 +13,7 @@ use Lwazi\Core\Services\LwaziAgent;
13
13
  use Lwazi\Core\Console\AnalyzeProjectCommand;
14
14
  use Lwazi\Core\Console\LwaziIngestCommand;
15
15
  use Lwazi\Core\Console\SetupCommand;
16
+ use Lwazi\Core\Console\BuildContentIndexCommand;
16
17
  use Lwazi\Core\Http\Middleware\InjectLwaziChat;
17
18
 
18
19
  class LwaziServiceProvider extends ServiceProvider
@@ -36,6 +37,7 @@ class LwaziServiceProvider extends ServiceProvider
36
37
  AnalyzeProjectCommand::class,
37
38
  SetupCommand::class,
38
39
  LwaziIngestCommand::class,
40
+ BuildContentIndexCommand::class,
39
41
  ]);
40
42
  }
41
43
  }
@@ -184,13 +184,31 @@ class ContentIndexer
184
184
 
185
185
  protected function generateSnippet(array $doc, array $terms): string
186
186
  {
187
- $text = implode(' ', $doc['paragraphs'] ?? []);
187
+ if (!is_array($doc)) {
188
+ return '';
189
+ }
190
+
191
+ $paragraphs = $doc['paragraphs'] ?? null;
192
+ $text = '';
188
193
 
189
- if (empty($text)) {
190
- $text = implode(' ', $doc['lists'] ?? []);
194
+ if (is_array($paragraphs) && count($paragraphs) > 0) {
195
+ $first = $paragraphs[0];
196
+ if (is_string($first)) {
197
+ $text = implode(' ', $paragraphs);
198
+ }
191
199
  }
192
200
 
193
- if (empty($text)) {
201
+ if ($text === '') {
202
+ $lists = $doc['lists'] ?? null;
203
+ if (is_array($lists) && count($lists) > 0) {
204
+ $first = $lists[0];
205
+ if (is_string($first)) {
206
+ $text = implode(' ', $lists);
207
+ }
208
+ }
209
+ }
210
+
211
+ if ($text === '' || !is_string($text)) {
194
212
  return '';
195
213
  }
196
214
 
@@ -199,6 +217,7 @@ class ContentIndexer
199
217
  $bestLen = PHP_INT_MAX;
200
218
 
201
219
  foreach ($terms as $term) {
220
+ if (!is_string($term)) continue;
202
221
  $term = strtolower($term);
203
222
  $pos = strpos($text, $term);
204
223
  if ($pos !== false && $pos < $bestLen) {
@@ -218,9 +237,9 @@ class ContentIndexer
218
237
  $snippet = '...' . $snippet;
219
238
  }
220
239
 
221
- $snippet = preg_replace('/\s+/', ' ', $snippet);
240
+ $snippet = preg_replace('/\s+/', ' ', $snippet ?? '');
222
241
 
223
- return trim($snippet) . '...';
242
+ return (is_string($snippet) ? trim($snippet) : '') . '...';
224
243
  }
225
244
 
226
245
  public function toArray(): array
@@ -233,6 +252,11 @@ class ContentIndexer
233
252
  ];
234
253
  }
235
254
 
255
+ public function getDocumentCount(): int
256
+ {
257
+ return $this->totalDocuments;
258
+ }
259
+
236
260
  public static function fromArray(array $data): self
237
261
  {
238
262
  $indexer = new self();
@@ -50,8 +50,23 @@ class KnowledgeBaseGenerator
50
50
  protected function buildNavigationTree(array $pages, array $routes): array
51
51
  {
52
52
  $tree = new NavigationTree();
53
+
54
+ $publicRoutes = array_filter($routes, function($r) {
55
+ $uri = $r['uri'] ?? '';
56
+ $name = $r['name'] ?? '';
57
+
58
+ if (str_starts_with($uri, '/admin') || str_starts_with($uri, '/api') || str_starts_with($uri, '/_')) {
59
+ return false;
60
+ }
61
+ if ($name && (str_starts_with($name, 'admin.') || str_starts_with($name, 'api.'))) {
62
+ return false;
63
+ }
64
+
65
+ return true;
66
+ });
67
+
53
68
  $tree->buildFromPages($pages);
54
- $tree->buildFromRoutes($routes);
69
+ $tree->buildFromRoutes(array_values($publicRoutes));
55
70
  return $tree->toArray();
56
71
  }
57
72
 
@@ -45,7 +45,7 @@ class LwaziService
45
45
 
46
46
  $this->conversationHistory[] = ['role' => 'user', 'content' => $message];
47
47
 
48
- if ($this->looksLikeNavigationQuery($message)) {
48
+ if ($this->looksLikeNavigationQuery($message) && !$this->looksLikeDataQuery($message)) {
49
49
  error_log('Lwazi: looks like navigation query');
50
50
  $nav = $this->findNavigationAnswer($message);
51
51
  if ($nav) {
@@ -53,11 +53,10 @@ class LwaziService
53
53
  $this->conversationHistory[] = ['role' => 'assistant', 'content' => $nav];
54
54
  return $nav;
55
55
  }
56
- $fallback = $this->navigationFallbackWithLLM($message);
57
- if ($fallback) {
58
- error_log('Lwazi: found fallback: ' . $fallback);
59
- $this->conversationHistory[] = ['role' => 'assistant', 'content' => $fallback];
60
- return $fallback;
56
+ $contentResponse = $this->searchContent($message);
57
+ if ($contentResponse) {
58
+ $this->conversationHistory[] = ['role' => 'assistant', 'content' => $contentResponse];
59
+ return $contentResponse;
61
60
  }
62
61
  }
63
62
 
@@ -70,33 +69,35 @@ class LwaziService
70
69
  $this->conversationHistory[] = ['role' => 'assistant', 'content' => $nav];
71
70
  return $nav;
72
71
  }
73
- $fallback = $this->navigationFallbackWithLLM($message);
74
- if ($fallback) {
75
- $this->conversationHistory[] = ['role' => 'assistant', 'content' => $fallback];
76
- return $fallback;
72
+ $contentResponse = $this->searchContent($message);
73
+ if ($contentResponse) {
74
+ $this->conversationHistory[] = ['role' => 'assistant', 'content' => $contentResponse];
75
+ return $contentResponse;
77
76
  }
77
+ return "I couldn't find a page matching that. Could you try a different question?";
78
78
  }
79
79
 
80
- if ($intent === 'content' || $intent === 'navigation') {
80
+ if ($intent === 'content') {
81
81
  $contentResponse = $this->searchContent($message);
82
82
  if ($contentResponse) {
83
83
  $this->conversationHistory[] = ['role' => 'assistant', 'content' => $contentResponse];
84
84
  return $contentResponse;
85
85
  }
86
+ return "I couldn't find information about that. Could you try a different question?";
86
87
  }
87
88
 
88
- if ($intent === 'navigation') {
89
- $nav = $this->navigationFallbackWithLLM($message);
90
- if ($nav) {
91
- $this->conversationHistory[] = ['role' => 'assistant', 'content' => $nav];
92
- return $nav;
89
+ if ($intent === 'data') {
90
+ $dataResponse = $this->fetchRelevantData($message);
91
+ if ($dataResponse) {
92
+ $this->conversationHistory[] = ['role' => 'assistant', 'content' => $dataResponse];
93
+ return $dataResponse;
93
94
  }
94
- }
95
-
96
- $dataResponse = $this->fetchRelevantData($message);
97
- if ($dataResponse) {
98
- $this->conversationHistory[] = ['role' => 'assistant', 'content' => $dataResponse];
99
- return $dataResponse;
95
+ $contentResponse = $this->searchContent($message);
96
+ if ($contentResponse) {
97
+ $this->conversationHistory[] = ['role' => 'assistant', 'content' => $contentResponse];
98
+ return $contentResponse;
99
+ }
100
+ return "I couldn't find information about that. Could you try a different question?";
100
101
  }
101
102
 
102
103
  $prompt = $this->buildPrompt();
@@ -113,16 +114,26 @@ class LwaziService
113
114
 
114
115
  protected function selectIntentWithLLM(string $message): ?string
115
116
  {
116
- if ($this->looksLikeNavigationQuery($message)) {
117
+ if ($this->looksLikeDataQuery($message)) {
118
+ return 'data';
119
+ }
120
+
121
+ if ($this->looksLikeNavigationQuery($message) && !$this->looksLikeContentQuery($message)) {
117
122
  return 'navigation';
118
123
  }
119
124
 
125
+ $msgLower = strtolower($message);
126
+
127
+ if (preg_match('/\b(is there|are there|do i have|can i find|show me|find|get|list|what.*contain|information about|info about)\b/i', $message)) {
128
+ return 'content';
129
+ }
130
+
120
131
  $prompt =
121
132
  "Classify the user's intent as one of: navigation, content, data, general. Return JSON: {\"intent\":\"navigation|content|data|general\"}.\n\n" .
122
- "- navigation: user wants to find a page or link\n" .
123
- "- content: user is asking for information that might be on a page (not data)\n" .
124
- "- data: user wants to query database records\n" .
125
- "- general: casual conversation\n\n" .
133
+ "- navigation: user wants to find a specific page or link (where, which page, how do i get to)\n" .
134
+ "- content: user is asking for information that might be on a webpage, NOT database data (is there, are there, show me, what does X contain)\n" .
135
+ "- data: user explicitly wants to query database records about their own data (my data, my records, my account)\n" .
136
+ "- general: casual conversation, greetings\n\n" .
126
137
  "QUESTION:\n" .
127
138
  $message;
128
139
 
@@ -145,6 +156,33 @@ class LwaziService
145
156
  return null;
146
157
  }
147
158
 
159
+ protected function looksLikeDataQuery(string $message): bool
160
+ {
161
+ $msgLower = strtolower($message);
162
+
163
+ $personalPatterns = [
164
+ '/\bmy\b/', '/\bme\b/', '/\bmine\b/', '/\bmy own\b/',
165
+ '/\bmy account\b/', '/\bmy profile\b/', '/\bmy data\b/',
166
+ '/\bmy records\b/', '/\bmy applications\b/', '/\bmy submissions\b/',
167
+ ];
168
+
169
+ foreach ($personalPatterns as $pattern) {
170
+ if (preg_match($pattern, $msgLower)) {
171
+ return true;
172
+ }
173
+ }
174
+
175
+ return false;
176
+ }
177
+
178
+ protected function looksLikeContentQuery(string $message): bool
179
+ {
180
+ return (bool) preg_match(
181
+ '/\b(is there|are there|can i find|show me|what.*contain|information about|passed|failed|grade|result|score|check if)\b/i',
182
+ $message
183
+ );
184
+ }
185
+
148
186
  public function looksLikeNavigationQuery(string $message): bool
149
187
  {
150
188
  return (bool) preg_match(
@@ -314,24 +352,50 @@ class LwaziService
314
352
 
315
353
  protected function findBestRouteWithTerms(array $routes, array $terms): ?string
316
354
  {
355
+ $filtered = array_filter($routes, function($r) {
356
+ $uri = $r['uri'] ?? '';
357
+ if (str_starts_with($uri, '/admin') || str_starts_with($uri, '/api') || str_starts_with($uri, '/_')) {
358
+ return false;
359
+ }
360
+ return true;
361
+ });
362
+
363
+ $routes = array_values($filtered);
364
+
365
+ if (empty($routes)) {
366
+ return null;
367
+ }
368
+
317
369
  $scored = [];
370
+ $termSet = array_filter(array_map('strtolower', array_map('trim', $terms)), fn($t) => strlen($t) >= 3);
371
+
372
+ if (empty($termSet)) {
373
+ return null;
374
+ }
375
+
318
376
  foreach ($routes as $r) {
319
377
  $uri = strtolower($r['uri'] ?? '');
320
- if ($uri === '') continue;
378
+ if ($uri === '' || $uri === '/') continue;
321
379
 
380
+ $uriClean = preg_replace('/[^a-z0-9]/', '', $uri);
381
+
382
+ $hasExactMatch = false;
322
383
  $score = 0;
323
- foreach ($terms as $term) {
324
- $term = strtolower(trim($term));
325
- if (strlen($term) < 2) continue;
326
- if (str_contains($uri, $term)) {
327
- $score += 10;
328
- if ($uri === $term || $uri === '/' . $term) {
329
- $score += 20;
330
- }
384
+
385
+ foreach ($termSet as $term) {
386
+ $termClean = preg_replace('/[^a-z0-9]/', '', $term);
387
+
388
+ if (strlen($termClean) < 3) continue;
389
+
390
+ if ($uri === '/' . $term || $uri === $term || str_contains($uri, '/' . $term . '/')) {
391
+ $hasExactMatch = true;
392
+ $score += 50;
393
+ } elseif (!empty($termClean) && str_contains($uriClean, $termClean)) {
394
+ $score += 5;
331
395
  }
332
396
  }
333
397
 
334
- if ($score > 0) {
398
+ if ($hasExactMatch && $score > 0) {
335
399
  $scored[] = ['uri' => $r['uri'], 'score' => $score];
336
400
  }
337
401
  }
@@ -571,6 +635,12 @@ class LwaziService
571
635
 
572
636
  protected function fetchRelevantData(string $message): ?string
573
637
  {
638
+ if ($this->isPersonalQuery($message) && empty($this->currentContextId)) {
639
+ $context = $this->ragService->getContextConfig();
640
+ $contextParam = $context['param'] ?? config('lwazi.context_param', 'context_id');
641
+ return "I can look that up for you, but I need your {$contextParam} to access your personal information.";
642
+ }
643
+
574
644
  $fieldInfo = $this->selectFieldWithLLM($message) ?: $this->ragService->findFieldForQuery($message);
575
645
 
576
646
  if ($fieldInfo) {
@@ -126,6 +126,10 @@ class NavigationTree
126
126
  $heading = $page['heading'] ?? '';
127
127
  $links = $page['links'] ?? [];
128
128
 
129
+ if (preg_match('#/admin/#', $file) || preg_match('#/admin\.blade\.php$#', $file)) {
130
+ return;
131
+ }
132
+
129
133
  $labels = array_filter([$title, $heading]);
130
134
  $primaryLabel = reset($labels) ?: basename($file, '.blade.php');
131
135
 
@@ -137,6 +141,10 @@ class NavigationTree
137
141
  continue;
138
142
  }
139
143
 
144
+ if (str_starts_with($href, '/admin') || str_starts_with($href, '/api') || str_starts_with($href, '/_')) {
145
+ continue;
146
+ }
147
+
140
148
  $this->addPath($href, $text, $primaryLabel);
141
149
  }
142
150
  }
@@ -221,6 +229,11 @@ class NavigationTree
221
229
  {
222
230
  $queryNormalized = $this->normalizeText($query);
223
231
  $queryTokens = $this->tokenize($query);
232
+
233
+ if (count($queryTokens) === 0) {
234
+ return null;
235
+ }
236
+
224
237
  $expandedTokens = $this->expandWithSynonyms($queryTokens);
225
238
 
226
239
  $candidates = [];
@@ -238,7 +251,11 @@ class NavigationTree
238
251
 
239
252
  usort($candidates, fn($a, $b) => $b['score'] <=> $a['score']);
240
253
 
241
- return $candidates[0] ?? null;
254
+ if (empty($candidates) || $candidates[0]['score'] < 15) {
255
+ return null;
256
+ }
257
+
258
+ return $candidates[0];
242
259
  }
243
260
 
244
261
  public function searchTree(string $query): ?array
@@ -326,16 +343,19 @@ class NavigationTree
326
343
  $labelNormalized = $entry['normalizedLabel'] ?? $this->normalizeText($entry['label'] ?? '');
327
344
 
328
345
  foreach ($queryTokens as $token) {
346
+ if (strlen($token) < 3) continue;
347
+
329
348
  if ($token === $labelNormalized) {
330
349
  $score += 20;
331
- } elseif (str_contains($labelNormalized, $token)) {
350
+ } elseif (strlen($token) >= 4 && str_contains($labelNormalized, $token)) {
332
351
  $score += 10;
333
- } elseif ($this->fuzzyMatch($labelNormalized, $token)) {
352
+ } elseif (strlen($token) >= 4 && $this->fuzzyMatch($labelNormalized, $token)) {
334
353
  $score += 15;
335
354
  }
336
355
  }
337
356
 
338
357
  foreach ($expandedTokens as $token) {
358
+ if (strlen($token) < 4) continue;
339
359
  if (str_contains($labelNormalized, $token)) {
340
360
  $score += 5;
341
361
  }
@@ -369,13 +389,27 @@ class NavigationTree
369
389
  return true;
370
390
  }
371
391
 
392
+ $commonPrefix = 0;
393
+ $minLen = min(strlen($a), strlen($b));
394
+ for ($i = 0; $i < $minLen; $i++) {
395
+ if ($a[$i] === $b[$i]) {
396
+ $commonPrefix++;
397
+ } else {
398
+ break;
399
+ }
400
+ }
401
+
402
+ if ($commonPrefix < 3) {
403
+ return false;
404
+ }
405
+
372
406
  $lev = levenshtein($a, $b);
373
407
  $maxLen = max(strlen($a), strlen($b));
374
408
  if ($maxLen === 0) {
375
409
  return false;
376
410
  }
377
411
 
378
- return $lev / $maxLen < 0.3;
412
+ return $lev / $maxLen < 0.2;
379
413
  }
380
414
 
381
415
  protected function normalizeText(string $text): string
@@ -506,6 +540,10 @@ class NavigationTree
506
540
  $queryStems = $tokenizer->tokenizeAndStem($query);
507
541
  $queryStems = $tokenizer->removeStopWords($queryStems);
508
542
 
543
+ if (count($queryStems) === 0) {
544
+ return null;
545
+ }
546
+
509
547
  $candidates = [];
510
548
 
511
549
  foreach ($this->stemmedIndex as $path => $stems) {
@@ -525,7 +563,7 @@ class NavigationTree
525
563
  }
526
564
  }
527
565
 
528
- if (empty($candidates)) {
566
+ if (empty($candidates) || $candidates[0]['score'] < 2) {
529
567
  return null;
530
568
  }
531
569