@dxos/plugin-search 0.8.1-staging.5be625a → 0.8.1-staging.97aedb1
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/dist/lib/browser/app-graph-builder-RHZORTY2.mjs +76 -0
- package/dist/lib/browser/app-graph-builder-RHZORTY2.mjs.map +7 -0
- package/dist/lib/browser/{chunk-2QDXE2SP.mjs → chunk-J6YWSQTW.mjs} +2 -2
- package/dist/lib/browser/{chunk-RJRPR6BG.mjs → chunk-LXHJVTYG.mjs} +3 -3
- package/dist/lib/browser/{chunk-RJRPR6BG.mjs.map → chunk-LXHJVTYG.mjs.map} +2 -2
- package/dist/lib/browser/{chunk-2G5TBIJT.mjs → chunk-NFZSXZMP.mjs} +55 -4
- package/dist/lib/browser/chunk-NFZSXZMP.mjs.map +7 -0
- package/dist/lib/browser/index.mjs +29 -18
- package/dist/lib/browser/index.mjs.map +3 -3
- package/dist/lib/browser/{intent-resolver-M3EJMBGZ.mjs → intent-resolver-G76A35DX.mjs} +4 -4
- package/dist/lib/browser/meta.json +1 -1
- package/dist/lib/browser/react-surface-QBBLEI2Q.mjs +609 -0
- package/dist/lib/browser/react-surface-QBBLEI2Q.mjs.map +7 -0
- package/dist/types/src/capabilities/app-graph-builder.d.ts.map +1 -1
- package/dist/types/src/capabilities/react-surface.d.ts.map +1 -1
- package/dist/types/src/components/SearchMain.d.ts +3 -2
- package/dist/types/src/components/SearchMain.d.ts.map +1 -1
- package/dist/types/src/components/SearchResults/SearchResults.d.ts +1 -1
- package/dist/types/src/components/SearchResults/SearchResults.d.ts.map +1 -1
- package/dist/types/src/components/Searchbar/Searchbar.d.ts +2 -1
- package/dist/types/src/components/Searchbar/Searchbar.d.ts.map +1 -1
- package/dist/types/src/components/Searchbar/Searchbar.stories.d.ts +1 -1
- package/dist/types/src/components/Searchbar/Searchbar.stories.d.ts.map +1 -1
- package/dist/types/src/hooks/index.d.ts +4 -0
- package/dist/types/src/hooks/index.d.ts.map +1 -0
- package/dist/types/src/{search-sync.d.ts → hooks/sync.d.ts} +3 -9
- package/dist/types/src/hooks/sync.d.ts.map +1 -0
- package/dist/types/src/hooks/sync.test.d.ts +2 -0
- package/dist/types/src/hooks/sync.test.d.ts.map +1 -0
- package/dist/types/src/{context.d.ts → hooks/useGlobalSearch.d.ts} +2 -2
- package/dist/types/src/hooks/useGlobalSearch.d.ts.map +1 -0
- package/dist/types/src/{search.d.ts → hooks/useSearchResults.d.ts} +1 -1
- package/dist/types/src/hooks/useSearchResults.d.ts.map +1 -0
- package/dist/types/src/hooks/useWebSearch.d.ts +10 -0
- package/dist/types/src/hooks/useWebSearch.d.ts.map +1 -0
- package/dist/types/src/index.d.ts +2 -2
- package/dist/types/src/index.d.ts.map +1 -1
- package/dist/types/src/search/exa.d.ts +19 -0
- package/dist/types/src/search/exa.d.ts.map +1 -0
- package/dist/types/src/search/exa.test.d.ts +2 -0
- package/dist/types/src/search/exa.test.d.ts.map +1 -0
- package/dist/types/src/search/index.d.ts +2 -0
- package/dist/types/src/search/index.d.ts.map +1 -0
- package/dist/types/src/translations.d.ts +1 -0
- package/dist/types/src/translations.d.ts.map +1 -1
- package/dist/types/src/{types.d.ts → types/action.d.ts} +1 -1
- package/dist/types/src/types/action.d.ts.map +1 -0
- package/dist/types/src/types/index.d.ts +3 -0
- package/dist/types/src/types/index.d.ts.map +1 -0
- package/dist/types/src/types/search.d.ts +21 -0
- package/dist/types/src/types/search.d.ts.map +1 -0
- package/package.json +29 -18
- package/src/SearchPlugin.tsx +7 -7
- package/src/capabilities/app-graph-builder.ts +28 -3
- package/src/capabilities/react-surface.tsx +26 -3
- package/src/components/Search.stories.tsx +1 -1
- package/src/components/SearchDialog.tsx +1 -1
- package/src/components/SearchMain.tsx +30 -11
- package/src/components/SearchResults/SearchResults.stories.tsx +1 -1
- package/src/components/SearchResults/SearchResults.tsx +16 -30
- package/src/components/Searchbar/Searchbar.stories.tsx +5 -2
- package/src/components/Searchbar/Searchbar.tsx +15 -3
- package/src/hooks/index.ts +7 -0
- package/src/{search.test.ts → hooks/sync.test.ts} +1 -1
- package/src/{search-sync.ts → hooks/sync.ts} +3 -10
- package/src/{context.tsx → hooks/useGlobalSearch.tsx} +2 -1
- package/src/{search.ts → hooks/useSearchResults.ts} +1 -1
- package/src/hooks/useWebSearch.ts +60 -0
- package/src/index.ts +2 -2
- package/src/search/exa.test.ts +237 -0
- package/src/search/exa.ts +250 -0
- package/src/search/index.ts +5 -0
- package/src/translations.ts +1 -0
- package/src/{types.tsx → types/action.tsx} +1 -1
- package/src/types/index.ts +6 -0
- package/src/types/search.ts +29 -0
- package/dist/lib/browser/app-graph-builder-SKWILENV.mjs +0 -41
- package/dist/lib/browser/app-graph-builder-SKWILENV.mjs.map +0 -7
- package/dist/lib/browser/chunk-2G5TBIJT.mjs.map +0 -7
- package/dist/lib/browser/react-surface-NAYLAW2V.mjs +0 -343
- package/dist/lib/browser/react-surface-NAYLAW2V.mjs.map +0 -7
- package/dist/types/src/context.d.ts.map +0 -1
- package/dist/types/src/search-sync.d.ts.map +0 -1
- package/dist/types/src/search.d.ts.map +0 -1
- package/dist/types/src/search.test.d.ts +0 -2
- package/dist/types/src/search.test.d.ts.map +0 -1
- package/dist/types/src/types.d.ts.map +0 -1
- /package/dist/lib/browser/{chunk-2QDXE2SP.mjs.map → chunk-J6YWSQTW.mjs.map} +0 -0
- /package/dist/lib/browser/{intent-resolver-M3EJMBGZ.mjs.map → intent-resolver-G76A35DX.mjs.map} +0 -0
|
@@ -6,9 +6,10 @@ import React from 'react';
|
|
|
6
6
|
|
|
7
7
|
import { Capabilities, contributes, createSurface, useLayout, useAppGraph } from '@dxos/app-framework';
|
|
8
8
|
import { getActiveSpace } from '@dxos/plugin-space';
|
|
9
|
+
import { isSpace, type Space } from '@dxos/react-client/echo';
|
|
9
10
|
|
|
10
11
|
import { SEARCH_DIALOG, SearchDialog, type SearchDialogProps, SearchMain } from '../components';
|
|
11
|
-
import { SearchContextProvider } from '../
|
|
12
|
+
import { SearchContextProvider } from '../hooks';
|
|
12
13
|
|
|
13
14
|
export default () =>
|
|
14
15
|
contributes(Capabilities.ReactSurface, [
|
|
@@ -23,17 +24,39 @@ export default () =>
|
|
|
23
24
|
),
|
|
24
25
|
}),
|
|
25
26
|
createSurface({
|
|
27
|
+
// id: `${SEARCH_DIALOG}/search-input`,
|
|
26
28
|
id: 'search-input',
|
|
27
29
|
role: 'search-input',
|
|
28
30
|
component: () => {
|
|
29
31
|
const layout = useLayout();
|
|
30
32
|
const { graph } = useAppGraph();
|
|
31
33
|
const space = graph ? getActiveSpace(graph, layout.active[0]) : undefined;
|
|
32
|
-
|
|
34
|
+
if (!space) {
|
|
35
|
+
return null;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return (
|
|
39
|
+
<SearchContextProvider>
|
|
40
|
+
<SearchMain space={space} />
|
|
41
|
+
</SearchContextProvider>
|
|
42
|
+
);
|
|
43
|
+
},
|
|
44
|
+
}),
|
|
45
|
+
createSurface({
|
|
46
|
+
id: `${SEARCH_DIALOG}/search`,
|
|
47
|
+
role: 'deck-companion--search',
|
|
48
|
+
filter: (data): data is { subject: Space } => isSpace(data.subject),
|
|
49
|
+
component: ({ data }) => {
|
|
50
|
+
const space = data.subject;
|
|
51
|
+
if (!space) {
|
|
52
|
+
return null;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
return (
|
|
33
56
|
<SearchContextProvider>
|
|
34
57
|
<SearchMain space={space} />
|
|
35
58
|
</SearchContextProvider>
|
|
36
|
-
)
|
|
59
|
+
);
|
|
37
60
|
},
|
|
38
61
|
}),
|
|
39
62
|
]);
|
|
@@ -12,7 +12,7 @@ import { withLayout, withTheme } from '@dxos/storybook-utils';
|
|
|
12
12
|
|
|
13
13
|
import { SearchResults } from './SearchResults';
|
|
14
14
|
import { Searchbar, type SearchbarProps } from './Searchbar';
|
|
15
|
-
import { SearchContextProvider, useGlobalSearch, useGlobalSearchResults } from '../
|
|
15
|
+
import { SearchContextProvider, useGlobalSearch, useGlobalSearchResults } from '../hooks';
|
|
16
16
|
|
|
17
17
|
faker.seed(1);
|
|
18
18
|
|
|
@@ -12,8 +12,8 @@ import { Button, Dialog, Icon, toLocalizedString, useTranslation } from '@dxos/r
|
|
|
12
12
|
import { SearchList, type SearchListItemProps } from '@dxos/react-ui-searchlist';
|
|
13
13
|
import { descriptionText, mx } from '@dxos/react-ui-theme';
|
|
14
14
|
|
|
15
|
+
import { useSearchResults } from '../hooks';
|
|
15
16
|
import { SEARCH_PLUGIN } from '../meta';
|
|
16
|
-
import { useSearchResults } from '../search';
|
|
17
17
|
|
|
18
18
|
export const SEARCH_DIALOG = `${SEARCH_PLUGIN}/SearchDialog`;
|
|
19
19
|
|
|
@@ -4,20 +4,23 @@
|
|
|
4
4
|
|
|
5
5
|
import React, { type FC, useState } from 'react';
|
|
6
6
|
|
|
7
|
+
import { log } from '@dxos/log';
|
|
7
8
|
import { useClient } from '@dxos/react-client';
|
|
8
9
|
import { Filter, useQuery, type Space } from '@dxos/react-client/echo';
|
|
9
|
-
import { useTranslation } from '@dxos/react-ui';
|
|
10
|
+
import { type ThemedClassName, useTranslation } from '@dxos/react-ui';
|
|
10
11
|
import { groupSurface, mx } from '@dxos/react-ui-theme';
|
|
11
12
|
|
|
12
13
|
import { SearchResults } from './SearchResults';
|
|
13
14
|
import { Searchbar } from './Searchbar';
|
|
14
|
-
import { useGlobalSearch, useGlobalSearchResults } from '../
|
|
15
|
+
import { useGlobalSearch, useGlobalSearchResults } from '../hooks';
|
|
16
|
+
import { useWebSearch } from '../hooks/useWebSearch';
|
|
15
17
|
import { SEARCH_PLUGIN } from '../meta';
|
|
16
18
|
|
|
17
|
-
export const SearchMain: FC<{ space: Space }
|
|
19
|
+
export const SearchMain: FC<ThemedClassName<{ space: Space }>> = ({ classNames, space }) => {
|
|
18
20
|
const { t } = useTranslation(SEARCH_PLUGIN);
|
|
19
21
|
const client = useClient();
|
|
20
22
|
const { setMatch } = useGlobalSearch();
|
|
23
|
+
const [query, setQuery] = useState<string>();
|
|
21
24
|
// TODO(burdon): UX to select all spaces.
|
|
22
25
|
const allSpaces = false;
|
|
23
26
|
|
|
@@ -25,6 +28,14 @@ export const SearchMain: FC<{ space: Space }> = ({ space }) => {
|
|
|
25
28
|
const objects = useQuery(allSpaces ? client.spaces : space, Filter.all());
|
|
26
29
|
const results = useGlobalSearchResults(objects);
|
|
27
30
|
|
|
31
|
+
const {
|
|
32
|
+
runSearch,
|
|
33
|
+
results: webResults,
|
|
34
|
+
isLoading,
|
|
35
|
+
} = useWebSearch({
|
|
36
|
+
query,
|
|
37
|
+
});
|
|
38
|
+
|
|
28
39
|
const [selected, setSelected] = useState<string>();
|
|
29
40
|
|
|
30
41
|
// TODO(burdon): Activate and/or filter current main (set context).
|
|
@@ -32,15 +43,23 @@ export const SearchMain: FC<{ space: Space }> = ({ space }) => {
|
|
|
32
43
|
setSelected((selected) => (selected === id ? undefined : id));
|
|
33
44
|
};
|
|
34
45
|
|
|
46
|
+
const allResults = [...results, ...webResults];
|
|
47
|
+
log.info('results', { results, webResults });
|
|
48
|
+
|
|
35
49
|
return (
|
|
36
|
-
<div className='flex flex-col grow h-full'>
|
|
37
|
-
<Searchbar
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
50
|
+
<div className={mx('flex flex-col grow h-full overflow-hidden', classNames)}>
|
|
51
|
+
<Searchbar
|
|
52
|
+
placeholder={t('search placeholder')}
|
|
53
|
+
onChange={(text) => {
|
|
54
|
+
setQuery(text);
|
|
55
|
+
setMatch?.(text);
|
|
56
|
+
}}
|
|
57
|
+
onSubmit={runSearch}
|
|
58
|
+
/>
|
|
59
|
+
{isLoading && <div className={mx('flex flex-col grow overflow-hidden', groupSurface)}>Loading...</div>}
|
|
60
|
+
{allResults.length > 0 && (
|
|
61
|
+
<div className={mx('flex flex-col grow overflow-hidden', groupSurface)}>
|
|
62
|
+
<SearchResults items={allResults} selected={selected} onSelect={handleSelect} />
|
|
44
63
|
</div>
|
|
45
64
|
)}
|
|
46
65
|
</div>
|
|
@@ -11,7 +11,7 @@ import { faker } from '@dxos/random';
|
|
|
11
11
|
import { withLayout, withTheme } from '@dxos/storybook-utils';
|
|
12
12
|
|
|
13
13
|
import { SearchResults, type SearchResultsProps } from './SearchResults';
|
|
14
|
-
import { filterObjectsSync } from '../../
|
|
14
|
+
import { filterObjectsSync } from '../../hooks';
|
|
15
15
|
|
|
16
16
|
faker.seed(1);
|
|
17
17
|
|
|
@@ -2,31 +2,13 @@
|
|
|
2
2
|
// Copyright 2023 DXOS.org
|
|
3
3
|
//
|
|
4
4
|
|
|
5
|
-
import { type Icon, Buildings, Folders, User } from '@phosphor-icons/react';
|
|
6
5
|
import React, { type FC, forwardRef } from 'react';
|
|
7
6
|
|
|
8
|
-
import { ScrollArea } from '@dxos/react-ui';
|
|
9
7
|
import { Card } from '@dxos/react-ui-card';
|
|
10
8
|
import { ghostHover, mx } from '@dxos/react-ui-theme';
|
|
11
9
|
|
|
12
10
|
import { SEARCH_RESULT } from '../../meta';
|
|
13
|
-
import type
|
|
14
|
-
|
|
15
|
-
// TODO(burdon): Factor out.
|
|
16
|
-
const styles = {
|
|
17
|
-
selected: '!bg-primary-100 !dark:bg-primary-900',
|
|
18
|
-
};
|
|
19
|
-
|
|
20
|
-
// TODO(burdon): Registry defined by plugins?
|
|
21
|
-
const getIcon = (type: string): Icon | undefined => {
|
|
22
|
-
const iconMap: Record<string, Icon> = {
|
|
23
|
-
user: User,
|
|
24
|
-
organization: Buildings,
|
|
25
|
-
project: Folders,
|
|
26
|
-
};
|
|
27
|
-
|
|
28
|
-
return iconMap[type];
|
|
29
|
-
};
|
|
11
|
+
import { type SearchResult } from '../../types';
|
|
30
12
|
|
|
31
13
|
export const Snippet: FC<{ text: string; match?: RegExp }> = ({ text, match }) => {
|
|
32
14
|
let content = <>{text}</>;
|
|
@@ -58,15 +40,23 @@ export const Snippet: FC<{ text: string; match?: RegExp }> = ({ text, match }) =
|
|
|
58
40
|
export type SearchItemProps = SearchResult & { selected: boolean } & Pick<SearchResultsProps, 'onSelect'>;
|
|
59
41
|
|
|
60
42
|
export const SearchItem = forwardRef<HTMLDivElement, SearchItemProps>((item, forwardRef) => {
|
|
61
|
-
const { id,
|
|
62
|
-
|
|
43
|
+
const { id, objectType, label, snippet, match, selected, onSelect } = item;
|
|
44
|
+
|
|
45
|
+
// TODO(burdon): Convert react-ui-card to use react-ui-icon.
|
|
46
|
+
// TODO(burdon): Get icon from plugin registry based on object type.
|
|
47
|
+
const Icon = undefined;
|
|
63
48
|
|
|
64
49
|
return (
|
|
65
50
|
<Card.Root
|
|
66
51
|
ref={forwardRef}
|
|
67
|
-
classNames={mx('mx-2 mt-2 cursor-pointer',
|
|
52
|
+
classNames={mx('mx-2 mt-2 cursor-pointer', selected && '!bg-groupSurface', ghostHover)}
|
|
68
53
|
onClick={() => onSelect?.(id)}
|
|
69
54
|
>
|
|
55
|
+
{objectType && (
|
|
56
|
+
<>
|
|
57
|
+
<div className='text-xs text-neutral-400 ml-4'>{objectType}</div>
|
|
58
|
+
</>
|
|
59
|
+
)}
|
|
70
60
|
<Card.Header>
|
|
71
61
|
<Card.Title title={label ?? 'Untitled'} />
|
|
72
62
|
{Icon && <Card.Endcap Icon={Icon} />}
|
|
@@ -90,8 +80,8 @@ export type SearchResultsProps = {
|
|
|
90
80
|
// TODO(burdon): Key cursor up/down.
|
|
91
81
|
export const SearchResults = ({ items, selected, onSelect }: SearchResultsProps) => {
|
|
92
82
|
return (
|
|
93
|
-
<
|
|
94
|
-
<
|
|
83
|
+
<div className='flex flex-col grow overflow-y-auto'>
|
|
84
|
+
<div className='flex flex-col'>
|
|
95
85
|
{items.map((item) => (
|
|
96
86
|
<SearchItem
|
|
97
87
|
key={item.id}
|
|
@@ -101,11 +91,7 @@ export const SearchResults = ({ items, selected, onSelect }: SearchResultsProps)
|
|
|
101
91
|
selected={selected === item.id}
|
|
102
92
|
/>
|
|
103
93
|
))}
|
|
104
|
-
</
|
|
105
|
-
|
|
106
|
-
<ScrollArea.Thumb />
|
|
107
|
-
</ScrollArea.Scrollbar>
|
|
108
|
-
<ScrollArea.Corner />
|
|
109
|
-
</ScrollArea.Root>
|
|
94
|
+
</div>
|
|
95
|
+
</div>
|
|
110
96
|
);
|
|
111
97
|
};
|
|
@@ -6,20 +6,23 @@ import '@dxos-theme';
|
|
|
6
6
|
|
|
7
7
|
import { type Meta } from '@storybook/react';
|
|
8
8
|
|
|
9
|
+
import { withTheme } from '@dxos/storybook-utils';
|
|
10
|
+
|
|
9
11
|
import { Searchbar, type SearchbarProps } from './Searchbar';
|
|
10
12
|
|
|
11
13
|
const meta: Meta<SearchbarProps> = {
|
|
12
14
|
title: 'plugins/plugin-search/Searchbar',
|
|
13
15
|
component: Searchbar,
|
|
16
|
+
decorators: [withTheme],
|
|
14
17
|
parameters: {
|
|
15
18
|
layout: 'centered',
|
|
16
19
|
},
|
|
17
20
|
};
|
|
18
21
|
|
|
22
|
+
export default meta;
|
|
23
|
+
|
|
19
24
|
export const Default = {
|
|
20
25
|
args: {
|
|
21
26
|
placeholder: 'Search...',
|
|
22
27
|
},
|
|
23
28
|
};
|
|
24
|
-
|
|
25
|
-
export default meta;
|
|
@@ -15,10 +15,11 @@ export type SearchbarProps = Pick<TextInputProps, 'variant' | 'placeholder'> & {
|
|
|
15
15
|
};
|
|
16
16
|
value?: string;
|
|
17
17
|
onChange?: (text?: string) => void;
|
|
18
|
+
onSubmit?: (text?: string) => void;
|
|
18
19
|
delay?: number;
|
|
19
20
|
};
|
|
20
21
|
|
|
21
|
-
export const Searchbar = ({ classes, variant, placeholder, value, onChange }: SearchbarProps) => {
|
|
22
|
+
export const Searchbar = ({ classes, variant, placeholder, value, onChange, onSubmit }: SearchbarProps) => {
|
|
22
23
|
const inputRef = useRef<HTMLInputElement>(null);
|
|
23
24
|
const [text, setText] = useState(value);
|
|
24
25
|
useEffect(() => {
|
|
@@ -34,8 +35,19 @@ export const Searchbar = ({ classes, variant, placeholder, value, onChange }: Se
|
|
|
34
35
|
inputRef.current?.focus();
|
|
35
36
|
};
|
|
36
37
|
|
|
38
|
+
const handleKeyDown = (event: React.KeyboardEvent<HTMLInputElement>) => {
|
|
39
|
+
switch (event.key) {
|
|
40
|
+
case 'Enter':
|
|
41
|
+
onSubmit?.(text);
|
|
42
|
+
break;
|
|
43
|
+
case 'Escape':
|
|
44
|
+
handleReset();
|
|
45
|
+
break;
|
|
46
|
+
}
|
|
47
|
+
};
|
|
48
|
+
|
|
37
49
|
return (
|
|
38
|
-
<div className='flex
|
|
50
|
+
<div className='flex shrink-0w-full items-center pli-1 pbs-1'>
|
|
39
51
|
<Input.Root>
|
|
40
52
|
<Input.TextInput
|
|
41
53
|
ref={inputRef}
|
|
@@ -44,7 +56,7 @@ export const Searchbar = ({ classes, variant, placeholder, value, onChange }: Se
|
|
|
44
56
|
value={text ?? ''}
|
|
45
57
|
classNames={mx('pl-3 pr-10', classes?.input)}
|
|
46
58
|
onChange={({ target }) => handleChange(target.value)}
|
|
47
|
-
onKeyDown={
|
|
59
|
+
onKeyDown={handleKeyDown}
|
|
48
60
|
/>
|
|
49
61
|
|
|
50
62
|
{/* TODO(burdon): Margin should be density specific. */}
|
|
@@ -6,6 +6,8 @@ import { AST, type S } from '@dxos/echo-schema';
|
|
|
6
6
|
import { getSchema } from '@dxos/live-object';
|
|
7
7
|
import { TextType } from '@dxos/schema';
|
|
8
8
|
|
|
9
|
+
import { type SearchResult } from '../types';
|
|
10
|
+
|
|
9
11
|
export const queryStringToMatch = (queryString?: string): RegExp | undefined => {
|
|
10
12
|
const trimmed = queryString?.trim();
|
|
11
13
|
return trimmed ? new RegExp(trimmed, 'i') : undefined;
|
|
@@ -34,15 +36,6 @@ const getIcon = (schema: S.Schema.AnyNoContext | undefined): string | undefined
|
|
|
34
36
|
// TODO(burdon): Reconcile with agent index.
|
|
35
37
|
export type TextFields = Record<string, string>;
|
|
36
38
|
|
|
37
|
-
export type SearchResult = {
|
|
38
|
-
id: string;
|
|
39
|
-
type?: string;
|
|
40
|
-
label?: string;
|
|
41
|
-
match?: RegExp;
|
|
42
|
-
snippet?: string;
|
|
43
|
-
object?: any;
|
|
44
|
-
};
|
|
45
|
-
|
|
46
39
|
// TODO(thure): This returns `SearchResult[]` which is not just a narrower set of objects as the name implies.
|
|
47
40
|
export const filterObjectsSync = <T extends Record<string, any>>(objects: T[], match?: RegExp): SearchResult[] => {
|
|
48
41
|
if (!match) {
|
|
@@ -84,7 +77,7 @@ export const filterObjectsSync = <T extends Record<string, any>>(objects: T[], m
|
|
|
84
77
|
};
|
|
85
78
|
|
|
86
79
|
// TODO(burdon): Use schema?
|
|
87
|
-
const getStringProperty = (object: Record<string, unknown>, keys: string[]): string | undefined => {
|
|
80
|
+
export const getStringProperty = (object: Record<string, unknown>, keys: string[]): string | undefined => {
|
|
88
81
|
let label;
|
|
89
82
|
keys.some((key) => {
|
|
90
83
|
const value = object[key];
|
|
@@ -6,7 +6,8 @@ import React, { createContext, type PropsWithChildren, useContext, useMemo, useS
|
|
|
6
6
|
|
|
7
7
|
import { raise } from '@dxos/debug';
|
|
8
8
|
|
|
9
|
-
import { filterObjectsSync, queryStringToMatch
|
|
9
|
+
import { filterObjectsSync, queryStringToMatch } from './sync';
|
|
10
|
+
import { type SearchResult } from '../types';
|
|
10
11
|
|
|
11
12
|
type SearchContextType = {
|
|
12
13
|
match?: RegExp;
|
|
@@ -8,7 +8,7 @@ import { useEffect, useState } from 'react';
|
|
|
8
8
|
import { TextType } from '@dxos/schema';
|
|
9
9
|
|
|
10
10
|
// TODO(thure): Deprecate search-sync, move still-relevant utilities elsewhere (here, probably).
|
|
11
|
-
import { mapObjectToTextFields, queryStringToMatch } from './
|
|
11
|
+
import { mapObjectToTextFields, queryStringToMatch } from './sync';
|
|
12
12
|
|
|
13
13
|
export const filterObjects = async <T extends Record<string, any>>(
|
|
14
14
|
objects: T[],
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
//
|
|
2
|
+
// Copyright 2025 DXOS.org
|
|
3
|
+
//
|
|
4
|
+
|
|
5
|
+
import { useState } from 'react';
|
|
6
|
+
|
|
7
|
+
import { AIServiceEdgeClient } from '@dxos/assistant';
|
|
8
|
+
import { AI_SERVICE_ENDPOINT } from '@dxos/assistant/testing';
|
|
9
|
+
import { getSchema, getTypename } from '@dxos/echo-schema';
|
|
10
|
+
import type { DXN } from '@dxos/keys';
|
|
11
|
+
import { log } from '@dxos/log';
|
|
12
|
+
import { getIconAnnotation } from '@dxos/schema';
|
|
13
|
+
import { Testing } from '@dxos/schema/testing';
|
|
14
|
+
|
|
15
|
+
import { getStringProperty } from './sync';
|
|
16
|
+
import { search } from '../search';
|
|
17
|
+
import type { SearchResult } from '../types';
|
|
18
|
+
|
|
19
|
+
// TODO(dmaretskyi): Get from config/credentials
|
|
20
|
+
const EXA_API_KEY = '9c7e17ff-0c85-4cd5-827a-8b489f139e03';
|
|
21
|
+
|
|
22
|
+
export const useWebSearch = ({ query, context }: { query?: string; context?: string }) => {
|
|
23
|
+
const aiService = new AIServiceEdgeClient({
|
|
24
|
+
endpoint: AI_SERVICE_ENDPOINT.REMOTE,
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const [results, setResults] = useState<SearchResult[]>([]);
|
|
28
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
29
|
+
|
|
30
|
+
const runSearch = async () => {
|
|
31
|
+
try {
|
|
32
|
+
setIsLoading(true);
|
|
33
|
+
log.info('Running web search', { query, context });
|
|
34
|
+
const results = await search({
|
|
35
|
+
query,
|
|
36
|
+
context,
|
|
37
|
+
schema: [Testing.Project, Testing.Org, Testing.Contact],
|
|
38
|
+
aiService,
|
|
39
|
+
exaApiKey: EXA_API_KEY,
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
const mappedResults = results.data.map((result): SearchResult => {
|
|
43
|
+
const schema = getSchema(result);
|
|
44
|
+
return {
|
|
45
|
+
id: result.id,
|
|
46
|
+
objectType: getTypename(result) as DXN.String | undefined,
|
|
47
|
+
label: getStringProperty(result, ['name', 'title', 'label']),
|
|
48
|
+
snippet: getStringProperty(result, ['description', 'content', 'website', 'email']),
|
|
49
|
+
object: result,
|
|
50
|
+
icon: schema?.pipe(getIconAnnotation),
|
|
51
|
+
};
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
setResults(mappedResults);
|
|
55
|
+
} finally {
|
|
56
|
+
setIsLoading(false);
|
|
57
|
+
}
|
|
58
|
+
};
|
|
59
|
+
return { runSearch, results, isLoading };
|
|
60
|
+
};
|