@dotcms/react 1.2.0 → 1.2.1
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/README.md +136 -0
- package/index.esm.js +2195 -856
- package/package.json +1 -1
- package/src/index.d.ts +2 -0
- package/src/lib/next/hooks/useAISearch.d.ts +30 -0
- package/src/lib/next/shared/types.d.ts +24 -0
package/README.md
CHANGED
|
@@ -20,6 +20,7 @@ The `@dotcms/react` SDK is the DotCMS official React library. It empowers React
|
|
|
20
20
|
- [DotCMSEditableText](#dotcmseditabletext)
|
|
21
21
|
- [useEditableDotCMSPage](#useeditabledotcmspage)
|
|
22
22
|
- [useDotCMSShowWhen](#usedotcmsshowwhen)
|
|
23
|
+
- [useAISearch](#useaisearch)
|
|
23
24
|
- [Troubleshooting](#troubleshooting)
|
|
24
25
|
- [Common Issues & Solutions](#common-issues--solutions)
|
|
25
26
|
- [Debugging Tips](#debugging-tips)
|
|
@@ -443,6 +444,141 @@ const MyEditButton = () => {
|
|
|
443
444
|
};
|
|
444
445
|
```
|
|
445
446
|
|
|
447
|
+
### useAISearch
|
|
448
|
+
|
|
449
|
+
`useAISearch` is a hook that enables AI-powered semantic search capabilities for your dotCMS content. It manages search state, handles API calls, and provides real-time status updates.
|
|
450
|
+
|
|
451
|
+
| Param | Type | Required | Description |
|
|
452
|
+
| ----------- | ----------------------- | -------- | ------------------------------------------------------------------------------ |
|
|
453
|
+
| `client` | `DotCMSClient` | ✅ | The dotCMS client instance created with `createDotCMSClient()` |
|
|
454
|
+
| `indexName` | `string` | ✅ | Name of the AI search index to query |
|
|
455
|
+
| `params` | `DotCMSAISearchParams` | ✅ | Search configuration including query params (limit, offset) and AI config |
|
|
456
|
+
|
|
457
|
+
#### Return Value
|
|
458
|
+
|
|
459
|
+
The hook returns an object with the following properties:
|
|
460
|
+
|
|
461
|
+
| Property | Type | Description |
|
|
462
|
+
| ---------- | ---------------------------------------- | -------------------------------------------------------------- |
|
|
463
|
+
| `response` | `DotCMSAISearchResponse<T> \| null` | Full search response including metadata and results |
|
|
464
|
+
| `results` | `DotCMSAISearchContentletData<T>[] \| undefined` | Array of search results with match scores |
|
|
465
|
+
| `status` | `DotCMSEntityStatus` | Current state: `IDLE`, `LOADING`, `SUCCESS`, or `ERROR` |
|
|
466
|
+
| `search` | `(prompt: string) => Promise<void>` | Function to execute a search with a text prompt |
|
|
467
|
+
| `reset` | `() => void` | Function to reset search state to idle |
|
|
468
|
+
|
|
469
|
+
#### Usage
|
|
470
|
+
|
|
471
|
+
```tsx
|
|
472
|
+
'use client';
|
|
473
|
+
|
|
474
|
+
import { useState } from 'react';
|
|
475
|
+
import { useAISearch } from '@dotcms/react';
|
|
476
|
+
import type { DotCMSBasicContentlet } from '@dotcms/types';
|
|
477
|
+
import { dotCMSClient } from '@/lib/dotCMSClient';
|
|
478
|
+
|
|
479
|
+
interface BlogPost extends DotCMSBasicContentlet {
|
|
480
|
+
body: string;
|
|
481
|
+
author: string;
|
|
482
|
+
}
|
|
483
|
+
|
|
484
|
+
const AISearchComponent = () => {
|
|
485
|
+
const [searchQuery, setSearchQuery] = useState('');
|
|
486
|
+
|
|
487
|
+
const { results, status, search, reset } = useAISearch<BlogPost>({
|
|
488
|
+
client: dotCMSClient,
|
|
489
|
+
indexName: 'blog-search-index',
|
|
490
|
+
params: {
|
|
491
|
+
query: {
|
|
492
|
+
limit: 10,
|
|
493
|
+
offset: 0,
|
|
494
|
+
contentType: 'Blog'
|
|
495
|
+
},
|
|
496
|
+
config: {
|
|
497
|
+
threshold: 0.5,
|
|
498
|
+
responseLength: 1024
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
});
|
|
502
|
+
|
|
503
|
+
const handleSearch = async () => {
|
|
504
|
+
if (searchQuery.trim()) {
|
|
505
|
+
await search(searchQuery);
|
|
506
|
+
}
|
|
507
|
+
};
|
|
508
|
+
|
|
509
|
+
const handleReset = () => {
|
|
510
|
+
setSearchQuery('');
|
|
511
|
+
reset();
|
|
512
|
+
};
|
|
513
|
+
|
|
514
|
+
return (
|
|
515
|
+
<div className="search-container">
|
|
516
|
+
<div className="search-bar">
|
|
517
|
+
<input
|
|
518
|
+
type="text"
|
|
519
|
+
value={searchQuery}
|
|
520
|
+
onChange={(e) => setSearchQuery(e.target.value)}
|
|
521
|
+
placeholder="Search with AI..."
|
|
522
|
+
/>
|
|
523
|
+
<button onClick={handleSearch} disabled={status.state === 'LOADING'}>
|
|
524
|
+
{status.state === 'LOADING' ? 'Searching...' : 'Search'}
|
|
525
|
+
</button>
|
|
526
|
+
<button onClick={handleReset}>Reset</button>
|
|
527
|
+
</div>
|
|
528
|
+
|
|
529
|
+
{status.state === 'ERROR' && (
|
|
530
|
+
<div className="error">
|
|
531
|
+
Error: {status.error.message}
|
|
532
|
+
</div>
|
|
533
|
+
)}
|
|
534
|
+
|
|
535
|
+
{status.state === 'SUCCESS' && results && (
|
|
536
|
+
<div className="results">
|
|
537
|
+
<h2>Found {results.length} results</h2>
|
|
538
|
+
{results.map((result) => (
|
|
539
|
+
<article key={result.identifier}>
|
|
540
|
+
<h3>{result.title}</h3>
|
|
541
|
+
<p>Author: {result.author}</p>
|
|
542
|
+
{result.matches?.map((match, idx) => (
|
|
543
|
+
<div key={idx} className="match">
|
|
544
|
+
<span>Relevance: {match.distance.toFixed(2)}</span>
|
|
545
|
+
<p>{match.extractedText}</p>
|
|
546
|
+
</div>
|
|
547
|
+
))}
|
|
548
|
+
</article>
|
|
549
|
+
))}
|
|
550
|
+
</div>
|
|
551
|
+
)}
|
|
552
|
+
</div>
|
|
553
|
+
);
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
export default AISearchComponent;
|
|
557
|
+
```
|
|
558
|
+
|
|
559
|
+
#### Features
|
|
560
|
+
|
|
561
|
+
- **Automatic State Management**: Handles loading, success, error, and idle states
|
|
562
|
+
- **Type-Safe Results**: Generic typing ensures type safety for your content types
|
|
563
|
+
- **Search Validation**: Automatically validates and trims search prompts
|
|
564
|
+
- **Configurable Parameters**: Support for pagination, thresholds, and AI configuration
|
|
565
|
+
- **Error Handling**: Provides detailed error information through status state
|
|
566
|
+
- **Reset Capability**: Easy state reset for clearing search results
|
|
567
|
+
|
|
568
|
+
#### Configuration Options
|
|
569
|
+
|
|
570
|
+
**Query Parameters (`params.query`):**
|
|
571
|
+
- `limit`: Maximum number of results (default: 1000)
|
|
572
|
+
- `offset`: Number of results to skip (default: 0)
|
|
573
|
+
- `siteId`: Filter by specific site
|
|
574
|
+
- `contentType`: Filter by content type
|
|
575
|
+
- `languageId`: Filter by language
|
|
576
|
+
|
|
577
|
+
**AI Configuration (`params.config`):**
|
|
578
|
+
- `threshold`: Minimum similarity score (default: 0.5)
|
|
579
|
+
- `distanceFunction`: Vector distance algorithm (default: cosine)
|
|
580
|
+
- `responseLength`: Maximum response text length (default: 1024)
|
|
581
|
+
|
|
446
582
|
## Troubleshooting
|
|
447
583
|
|
|
448
584
|
### Common Issues & Solutions
|