@peam-ai/search 0.1.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/LICENSE +201 -0
- package/README.md +11 -0
- package/dist/index.d.mts +66 -0
- package/dist/index.d.ts +66 -0
- package/dist/index.js +291 -0
- package/dist/index.js.map +1 -0
- package/dist/index.mjs +263 -0
- package/dist/index.mjs.map +1 -0
- package/package.json +49 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
package/dist/index.d.mts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { StructuredPage } from '@peam-ai/parser';
|
|
2
|
+
|
|
3
|
+
interface PageToIndex {
|
|
4
|
+
path: string;
|
|
5
|
+
structuredPage: StructuredPage;
|
|
6
|
+
}
|
|
7
|
+
interface SearchIndexData {
|
|
8
|
+
keys: string[];
|
|
9
|
+
data: Record<string, string>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Build a search index from structured pages
|
|
13
|
+
*/
|
|
14
|
+
declare function buildSearchIndex(pages: PageToIndex[]): Promise<SearchIndexData>;
|
|
15
|
+
|
|
16
|
+
type StructuredPageDocumentData = {
|
|
17
|
+
id: string;
|
|
18
|
+
path: string;
|
|
19
|
+
content: StructuredPage;
|
|
20
|
+
[key: string]: any;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
interface TextSearchOptions {
|
|
24
|
+
limit?: number;
|
|
25
|
+
offset?: number;
|
|
26
|
+
suggest?: boolean;
|
|
27
|
+
}
|
|
28
|
+
declare class TextSearch {
|
|
29
|
+
private index;
|
|
30
|
+
private initialized;
|
|
31
|
+
private documentIds;
|
|
32
|
+
constructor();
|
|
33
|
+
private getIndex;
|
|
34
|
+
initialize(): Promise<void>;
|
|
35
|
+
addDocument(document: StructuredPageDocumentData): Promise<void>;
|
|
36
|
+
search(query: string, options?: TextSearchOptions): Promise<StructuredPageDocumentData[]>;
|
|
37
|
+
count(): number;
|
|
38
|
+
getDocument(path: string): StructuredPageDocumentData | null;
|
|
39
|
+
getAllDocuments(limit?: number): StructuredPageDocumentData[];
|
|
40
|
+
clear(): void;
|
|
41
|
+
export(handler: (key: string, data: string) => Promise<void>): Promise<{
|
|
42
|
+
keys: string[];
|
|
43
|
+
}>;
|
|
44
|
+
import(handler: (key: string) => Promise<string>, keys: string[]): Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface SearchEngineConfig {
|
|
48
|
+
}
|
|
49
|
+
declare class SearchEngine {
|
|
50
|
+
private textSearch;
|
|
51
|
+
private initialized;
|
|
52
|
+
constructor(_config?: SearchEngineConfig);
|
|
53
|
+
initialize(): Promise<void>;
|
|
54
|
+
addPage(path: string, content: StructuredPage): Promise<void>;
|
|
55
|
+
search(query: string, options?: TextSearchOptions): Promise<StructuredPageDocumentData[]>;
|
|
56
|
+
count(): number;
|
|
57
|
+
getDocument(path: string): StructuredPageDocumentData | null;
|
|
58
|
+
getAllDocuments(limit?: number): StructuredPageDocumentData[];
|
|
59
|
+
clear(): void;
|
|
60
|
+
export(handler: (key: string, data: string) => Promise<void>): Promise<{
|
|
61
|
+
keys: string[];
|
|
62
|
+
}>;
|
|
63
|
+
import(handler: (key: string) => Promise<string>, keys: string[]): Promise<void>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export { type PageToIndex, SearchEngine, type SearchEngineConfig, type SearchIndexData, type StructuredPageDocumentData, TextSearch, buildSearchIndex };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
import { StructuredPage } from '@peam-ai/parser';
|
|
2
|
+
|
|
3
|
+
interface PageToIndex {
|
|
4
|
+
path: string;
|
|
5
|
+
structuredPage: StructuredPage;
|
|
6
|
+
}
|
|
7
|
+
interface SearchIndexData {
|
|
8
|
+
keys: string[];
|
|
9
|
+
data: Record<string, string>;
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Build a search index from structured pages
|
|
13
|
+
*/
|
|
14
|
+
declare function buildSearchIndex(pages: PageToIndex[]): Promise<SearchIndexData>;
|
|
15
|
+
|
|
16
|
+
type StructuredPageDocumentData = {
|
|
17
|
+
id: string;
|
|
18
|
+
path: string;
|
|
19
|
+
content: StructuredPage;
|
|
20
|
+
[key: string]: any;
|
|
21
|
+
};
|
|
22
|
+
|
|
23
|
+
interface TextSearchOptions {
|
|
24
|
+
limit?: number;
|
|
25
|
+
offset?: number;
|
|
26
|
+
suggest?: boolean;
|
|
27
|
+
}
|
|
28
|
+
declare class TextSearch {
|
|
29
|
+
private index;
|
|
30
|
+
private initialized;
|
|
31
|
+
private documentIds;
|
|
32
|
+
constructor();
|
|
33
|
+
private getIndex;
|
|
34
|
+
initialize(): Promise<void>;
|
|
35
|
+
addDocument(document: StructuredPageDocumentData): Promise<void>;
|
|
36
|
+
search(query: string, options?: TextSearchOptions): Promise<StructuredPageDocumentData[]>;
|
|
37
|
+
count(): number;
|
|
38
|
+
getDocument(path: string): StructuredPageDocumentData | null;
|
|
39
|
+
getAllDocuments(limit?: number): StructuredPageDocumentData[];
|
|
40
|
+
clear(): void;
|
|
41
|
+
export(handler: (key: string, data: string) => Promise<void>): Promise<{
|
|
42
|
+
keys: string[];
|
|
43
|
+
}>;
|
|
44
|
+
import(handler: (key: string) => Promise<string>, keys: string[]): Promise<void>;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
interface SearchEngineConfig {
|
|
48
|
+
}
|
|
49
|
+
declare class SearchEngine {
|
|
50
|
+
private textSearch;
|
|
51
|
+
private initialized;
|
|
52
|
+
constructor(_config?: SearchEngineConfig);
|
|
53
|
+
initialize(): Promise<void>;
|
|
54
|
+
addPage(path: string, content: StructuredPage): Promise<void>;
|
|
55
|
+
search(query: string, options?: TextSearchOptions): Promise<StructuredPageDocumentData[]>;
|
|
56
|
+
count(): number;
|
|
57
|
+
getDocument(path: string): StructuredPageDocumentData | null;
|
|
58
|
+
getAllDocuments(limit?: number): StructuredPageDocumentData[];
|
|
59
|
+
clear(): void;
|
|
60
|
+
export(handler: (key: string, data: string) => Promise<void>): Promise<{
|
|
61
|
+
keys: string[];
|
|
62
|
+
}>;
|
|
63
|
+
import(handler: (key: string) => Promise<string>, keys: string[]): Promise<void>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export { type PageToIndex, SearchEngine, type SearchEngineConfig, type SearchIndexData, type StructuredPageDocumentData, TextSearch, buildSearchIndex };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,291 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __defProp = Object.defineProperty;
|
|
3
|
+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
4
|
+
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
|
+
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
6
|
+
var __export = (target, all) => {
|
|
7
|
+
for (var name in all)
|
|
8
|
+
__defProp(target, name, { get: all[name], enumerable: true });
|
|
9
|
+
};
|
|
10
|
+
var __copyProps = (to, from, except, desc) => {
|
|
11
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
12
|
+
for (let key of __getOwnPropNames(from))
|
|
13
|
+
if (!__hasOwnProp.call(to, key) && key !== except)
|
|
14
|
+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
|
|
15
|
+
}
|
|
16
|
+
return to;
|
|
17
|
+
};
|
|
18
|
+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
|
|
19
|
+
var __async = (__this, __arguments, generator) => {
|
|
20
|
+
return new Promise((resolve, reject) => {
|
|
21
|
+
var fulfilled = (value) => {
|
|
22
|
+
try {
|
|
23
|
+
step(generator.next(value));
|
|
24
|
+
} catch (e) {
|
|
25
|
+
reject(e);
|
|
26
|
+
}
|
|
27
|
+
};
|
|
28
|
+
var rejected = (value) => {
|
|
29
|
+
try {
|
|
30
|
+
step(generator.throw(value));
|
|
31
|
+
} catch (e) {
|
|
32
|
+
reject(e);
|
|
33
|
+
}
|
|
34
|
+
};
|
|
35
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
36
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
37
|
+
});
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
// src/index.ts
|
|
41
|
+
var index_exports = {};
|
|
42
|
+
__export(index_exports, {
|
|
43
|
+
SearchEngine: () => SearchEngine,
|
|
44
|
+
TextSearch: () => TextSearch,
|
|
45
|
+
buildSearchIndex: () => buildSearchIndex
|
|
46
|
+
});
|
|
47
|
+
module.exports = __toCommonJS(index_exports);
|
|
48
|
+
|
|
49
|
+
// src/searchEngine.ts
|
|
50
|
+
var import_logger2 = require("@peam-ai/logger");
|
|
51
|
+
|
|
52
|
+
// src/textSearch.ts
|
|
53
|
+
var import_logger = require("@peam-ai/logger");
|
|
54
|
+
var import_flexsearch = require("flexsearch");
|
|
55
|
+
var PEAM_DOCUMENT_IDS_KEY = "peam.documentIds";
|
|
56
|
+
var MAX_DOCUMENTS_RETRIEVE = 25;
|
|
57
|
+
var log = import_logger.loggers.search;
|
|
58
|
+
var TextSearch = class {
|
|
59
|
+
constructor() {
|
|
60
|
+
this.initialized = false;
|
|
61
|
+
this.index = this.getIndex();
|
|
62
|
+
this.documentIds = /* @__PURE__ */ new Set();
|
|
63
|
+
}
|
|
64
|
+
getIndex() {
|
|
65
|
+
return new import_flexsearch.Document({
|
|
66
|
+
worker: false,
|
|
67
|
+
document: {
|
|
68
|
+
id: "path",
|
|
69
|
+
index: ["content:title", "content:description", "content:textContent", "content:author", "content:keywords"],
|
|
70
|
+
store: true
|
|
71
|
+
},
|
|
72
|
+
tokenize: "forward",
|
|
73
|
+
resolution: 9,
|
|
74
|
+
context: {
|
|
75
|
+
resolution: 3,
|
|
76
|
+
depth: 2,
|
|
77
|
+
bidirectional: true
|
|
78
|
+
},
|
|
79
|
+
cache: 100,
|
|
80
|
+
encoder: import_flexsearch.Charset.LatinExtra
|
|
81
|
+
});
|
|
82
|
+
}
|
|
83
|
+
initialize() {
|
|
84
|
+
return __async(this, null, function* () {
|
|
85
|
+
if (this.initialized) {
|
|
86
|
+
log.debug("Text search already initialized");
|
|
87
|
+
return;
|
|
88
|
+
}
|
|
89
|
+
this.initialized = true;
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
addDocument(document) {
|
|
93
|
+
return __async(this, null, function* () {
|
|
94
|
+
if (!this.initialized) {
|
|
95
|
+
throw new Error("TextSearch not initialized. Call initialize() first.");
|
|
96
|
+
}
|
|
97
|
+
log.debug("Adding document to text search:", document.path);
|
|
98
|
+
this.index.add(document);
|
|
99
|
+
this.documentIds.add(document.path);
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
search(_0) {
|
|
103
|
+
return __async(this, arguments, function* (query, options = {}) {
|
|
104
|
+
if (!this.initialized) {
|
|
105
|
+
throw new Error("TextSearch not initialized. Call initialize() first.");
|
|
106
|
+
}
|
|
107
|
+
const limit = options.limit || MAX_DOCUMENTS_RETRIEVE;
|
|
108
|
+
const offset = options.offset || 0;
|
|
109
|
+
log.debug("Searching for:", query);
|
|
110
|
+
const results = yield this.index.search(query, {
|
|
111
|
+
limit: limit + offset,
|
|
112
|
+
suggest: options.suggest,
|
|
113
|
+
enrich: true
|
|
114
|
+
});
|
|
115
|
+
const pathSet = /* @__PURE__ */ new Set();
|
|
116
|
+
const documents = [];
|
|
117
|
+
for (const fieldResults of results) {
|
|
118
|
+
if (Array.isArray(fieldResults.result)) {
|
|
119
|
+
for (const result of fieldResults.result) {
|
|
120
|
+
const id = typeof result === "object" && "id" in result ? result.id : result;
|
|
121
|
+
const doc = typeof result === "object" && "doc" in result ? result.doc : null;
|
|
122
|
+
if (!pathSet.has(id) && doc) {
|
|
123
|
+
pathSet.add(id);
|
|
124
|
+
documents.push(doc);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
const pagedResults = documents.slice(offset, offset + limit);
|
|
130
|
+
return pagedResults;
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
count() {
|
|
134
|
+
return this.documentIds.size;
|
|
135
|
+
}
|
|
136
|
+
getDocument(path) {
|
|
137
|
+
return this.index.get(path);
|
|
138
|
+
}
|
|
139
|
+
getAllDocuments(limit) {
|
|
140
|
+
const documents = [];
|
|
141
|
+
let count = 0;
|
|
142
|
+
limit = limit || MAX_DOCUMENTS_RETRIEVE;
|
|
143
|
+
for (const id of this.documentIds) {
|
|
144
|
+
if (count >= limit) {
|
|
145
|
+
break;
|
|
146
|
+
}
|
|
147
|
+
const doc = this.index.get(id);
|
|
148
|
+
if (doc) {
|
|
149
|
+
documents.push(doc);
|
|
150
|
+
count++;
|
|
151
|
+
}
|
|
152
|
+
}
|
|
153
|
+
log.debug("Retrieved documents from store (limit):", documents.length, limit);
|
|
154
|
+
return documents;
|
|
155
|
+
}
|
|
156
|
+
clear() {
|
|
157
|
+
this.index.clear();
|
|
158
|
+
this.index = this.getIndex();
|
|
159
|
+
this.documentIds.clear();
|
|
160
|
+
}
|
|
161
|
+
export(handler) {
|
|
162
|
+
return __async(this, null, function* () {
|
|
163
|
+
const keys = [];
|
|
164
|
+
yield handler(PEAM_DOCUMENT_IDS_KEY, JSON.stringify(Array.from(this.documentIds)));
|
|
165
|
+
keys.push(PEAM_DOCUMENT_IDS_KEY);
|
|
166
|
+
yield this.index.export((key, data) => __async(this, null, function* () {
|
|
167
|
+
keys.push(key);
|
|
168
|
+
yield handler(key, data);
|
|
169
|
+
}));
|
|
170
|
+
log.debug("Exported keys:", keys.length);
|
|
171
|
+
return { keys };
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
import(handler, keys) {
|
|
175
|
+
return __async(this, null, function* () {
|
|
176
|
+
const documentIdsData = yield handler(PEAM_DOCUMENT_IDS_KEY);
|
|
177
|
+
if (documentIdsData) {
|
|
178
|
+
const parsed = typeof documentIdsData === "string" ? JSON.parse(documentIdsData) : documentIdsData;
|
|
179
|
+
this.documentIds = new Set(parsed);
|
|
180
|
+
}
|
|
181
|
+
for (const key of keys) {
|
|
182
|
+
if (key === PEAM_DOCUMENT_IDS_KEY) {
|
|
183
|
+
continue;
|
|
184
|
+
}
|
|
185
|
+
try {
|
|
186
|
+
const data = yield handler(key);
|
|
187
|
+
if (data) {
|
|
188
|
+
this.index.import(key, data);
|
|
189
|
+
}
|
|
190
|
+
} catch (error) {
|
|
191
|
+
log.error("Error importing key:", key, error);
|
|
192
|
+
}
|
|
193
|
+
}
|
|
194
|
+
this.initialized = true;
|
|
195
|
+
log.debug("Import completed with keys:", keys.length);
|
|
196
|
+
});
|
|
197
|
+
}
|
|
198
|
+
};
|
|
199
|
+
|
|
200
|
+
// src/searchEngine.ts
|
|
201
|
+
var log2 = import_logger2.loggers.search;
|
|
202
|
+
var SearchEngine = class {
|
|
203
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
204
|
+
constructor(_config) {
|
|
205
|
+
this.textSearch = new TextSearch();
|
|
206
|
+
this.initialized = false;
|
|
207
|
+
}
|
|
208
|
+
initialize() {
|
|
209
|
+
return __async(this, null, function* () {
|
|
210
|
+
if (this.initialized) {
|
|
211
|
+
return;
|
|
212
|
+
}
|
|
213
|
+
log2.debug("Initializing search engine");
|
|
214
|
+
yield this.textSearch.initialize();
|
|
215
|
+
this.initialized = true;
|
|
216
|
+
});
|
|
217
|
+
}
|
|
218
|
+
addPage(path, content) {
|
|
219
|
+
return __async(this, null, function* () {
|
|
220
|
+
if (!this.initialized) {
|
|
221
|
+
throw new Error("Search engine not initialized. Call initialize() first.");
|
|
222
|
+
}
|
|
223
|
+
const document = {
|
|
224
|
+
id: path,
|
|
225
|
+
path,
|
|
226
|
+
content
|
|
227
|
+
};
|
|
228
|
+
yield this.textSearch.addDocument(document);
|
|
229
|
+
log2.debug("Page added to search engine:", path);
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
search(_0) {
|
|
233
|
+
return __async(this, arguments, function* (query, options = {}) {
|
|
234
|
+
if (!this.initialized) {
|
|
235
|
+
throw new Error("Search engine not initialized. Call initialize() first.");
|
|
236
|
+
}
|
|
237
|
+
log2.debug("Performing text search:", query);
|
|
238
|
+
return this.textSearch.search(query, options);
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
count() {
|
|
242
|
+
return this.textSearch.count();
|
|
243
|
+
}
|
|
244
|
+
getDocument(path) {
|
|
245
|
+
return this.textSearch.getDocument(path);
|
|
246
|
+
}
|
|
247
|
+
getAllDocuments(limit) {
|
|
248
|
+
return this.textSearch.getAllDocuments(limit);
|
|
249
|
+
}
|
|
250
|
+
clear() {
|
|
251
|
+
this.textSearch.clear();
|
|
252
|
+
}
|
|
253
|
+
export(handler) {
|
|
254
|
+
return __async(this, null, function* () {
|
|
255
|
+
return this.textSearch.export(handler);
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
import(handler, keys) {
|
|
259
|
+
return __async(this, null, function* () {
|
|
260
|
+
yield this.textSearch.import(handler, keys);
|
|
261
|
+
this.initialized = true;
|
|
262
|
+
log2.debug("Search engine initialized from imported data");
|
|
263
|
+
});
|
|
264
|
+
}
|
|
265
|
+
};
|
|
266
|
+
|
|
267
|
+
// src/indexBuilder.ts
|
|
268
|
+
function buildSearchIndex(pages) {
|
|
269
|
+
return __async(this, null, function* () {
|
|
270
|
+
const searchEngine = new SearchEngine();
|
|
271
|
+
yield searchEngine.initialize();
|
|
272
|
+
for (const page of pages) {
|
|
273
|
+
yield searchEngine.addPage(page.path, page.structuredPage);
|
|
274
|
+
}
|
|
275
|
+
const exportedData = {};
|
|
276
|
+
const result = yield searchEngine.export((key, data) => __async(null, null, function* () {
|
|
277
|
+
exportedData[key] = data;
|
|
278
|
+
}));
|
|
279
|
+
return {
|
|
280
|
+
keys: result.keys,
|
|
281
|
+
data: exportedData
|
|
282
|
+
};
|
|
283
|
+
});
|
|
284
|
+
}
|
|
285
|
+
// Annotate the CommonJS export names for ESM import in node:
|
|
286
|
+
0 && (module.exports = {
|
|
287
|
+
SearchEngine,
|
|
288
|
+
TextSearch,
|
|
289
|
+
buildSearchIndex
|
|
290
|
+
});
|
|
291
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/index.ts","../src/searchEngine.ts","../src/textSearch.ts","../src/indexBuilder.ts"],"sourcesContent":["export { buildSearchIndex, type PageToIndex, type SearchIndexData } from './indexBuilder';\nexport { SearchEngine } from './searchEngine';\nexport { TextSearch } from './textSearch';\n\nexport type { StructuredPageDocumentData } from './types';\n\nexport type { SearchEngineConfig } from './searchEngine';\n","import { loggers } from '@peam-ai/logger';\nimport type { StructuredPage } from '@peam-ai/parser';\nimport { TextSearch, type TextSearchOptions } from './textSearch';\nimport type { StructuredPageDocumentData } from './types';\n\nconst log = loggers.search;\n\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type\nexport interface SearchEngineConfig {\n // Reserved for future configuration options\n}\n\nexport class SearchEngine {\n private textSearch: TextSearch;\n private initialized: boolean;\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n constructor(_config?: SearchEngineConfig) {\n this.textSearch = new TextSearch();\n this.initialized = false;\n }\n\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n log.debug('Initializing search engine');\n await this.textSearch.initialize();\n this.initialized = true;\n }\n\n async addPage(path: string, content: StructuredPage): Promise<void> {\n if (!this.initialized) {\n throw new Error('Search engine not initialized. Call initialize() first.');\n }\n\n const document: StructuredPageDocumentData = {\n id: path,\n path,\n content,\n };\n\n await this.textSearch.addDocument(document);\n log.debug('Page added to search engine:', path);\n }\n\n async search(query: string, options: TextSearchOptions = {}): Promise<StructuredPageDocumentData[]> {\n if (!this.initialized) {\n throw new Error('Search engine not initialized. Call initialize() first.');\n }\n\n log.debug('Performing text search:', query);\n return this.textSearch.search(query, options);\n }\n\n count(): number {\n return this.textSearch.count();\n }\n\n getDocument(path: string) {\n return this.textSearch.getDocument(path);\n }\n\n getAllDocuments(limit?: number): StructuredPageDocumentData[] {\n return this.textSearch.getAllDocuments(limit);\n }\n\n clear(): void {\n this.textSearch.clear();\n }\n\n async export(handler: (key: string, data: string) => Promise<void>): Promise<{ keys: string[] }> {\n return this.textSearch.export(handler);\n }\n\n async import(handler: (key: string) => Promise<string>, keys: string[]): Promise<void> {\n await this.textSearch.import(handler, keys);\n this.initialized = true;\n log.debug('Search engine initialized from imported data');\n }\n}\n","import { loggers } from '@peam-ai/logger';\nimport { Charset, Document } from 'flexsearch';\nimport type { StructuredPageDocumentData } from './types';\n\nexport interface TextSearchOptions {\n limit?: number;\n offset?: number;\n suggest?: boolean;\n}\n\nconst PEAM_DOCUMENT_IDS_KEY = 'peam.documentIds';\nconst MAX_DOCUMENTS_RETRIEVE = 25;\nconst log = loggers.search;\n\nexport class TextSearch {\n private index: Document<StructuredPageDocumentData>;\n private initialized: boolean;\n private documentIds: Set<string>;\n\n constructor() {\n this.initialized = false;\n this.index = this.getIndex();\n this.documentIds = new Set();\n }\n\n private getIndex() {\n return new Document<StructuredPageDocumentData>({\n worker: false,\n document: {\n id: 'path',\n index: ['content:title', 'content:description', 'content:textContent', 'content:author', 'content:keywords'],\n store: true,\n },\n tokenize: 'forward',\n resolution: 9,\n context: {\n resolution: 3,\n depth: 2,\n bidirectional: true,\n },\n cache: 100,\n encoder: Charset.LatinExtra,\n });\n }\n\n async initialize(): Promise<void> {\n if (this.initialized) {\n log.debug('Text search already initialized');\n return;\n }\n\n this.initialized = true;\n }\n\n async addDocument(document: StructuredPageDocumentData): Promise<void> {\n if (!this.initialized) {\n throw new Error('TextSearch not initialized. Call initialize() first.');\n }\n\n log.debug('Adding document to text search:', document.path);\n\n this.index.add(document);\n this.documentIds.add(document.path);\n }\n\n async search(query: string, options: TextSearchOptions = {}): Promise<StructuredPageDocumentData[]> {\n if (!this.initialized) {\n throw new Error('TextSearch not initialized. Call initialize() first.');\n }\n\n const limit = options.limit || MAX_DOCUMENTS_RETRIEVE;\n const offset = options.offset || 0;\n\n log.debug('Searching for:', query);\n\n const results = await this.index.search(query, {\n limit: limit + offset,\n suggest: options.suggest,\n enrich: true,\n });\n\n const pathSet = new Set<string>();\n const documents: StructuredPageDocumentData[] = [];\n\n for (const fieldResults of results) {\n if (Array.isArray(fieldResults.result)) {\n for (const result of fieldResults.result) {\n const id = typeof result === 'object' && 'id' in result ? result.id : result;\n const doc = typeof result === 'object' && 'doc' in result ? result.doc : null;\n\n if (!pathSet.has(id as string) && doc) {\n pathSet.add(id as string);\n documents.push(doc);\n }\n }\n }\n }\n\n const pagedResults = documents.slice(offset, offset + limit);\n\n return pagedResults;\n }\n\n count(): number {\n return this.documentIds.size;\n }\n\n getDocument(path: string) {\n return this.index.get(path);\n }\n\n getAllDocuments(limit?: number): StructuredPageDocumentData[] {\n const documents: StructuredPageDocumentData[] = [];\n let count = 0;\n limit = limit || MAX_DOCUMENTS_RETRIEVE;\n\n for (const id of this.documentIds) {\n if (count >= limit) {\n break;\n }\n\n const doc = this.index.get(id);\n if (doc) {\n documents.push(doc);\n count++;\n }\n }\n\n log.debug('Retrieved documents from store (limit):', documents.length, limit);\n return documents;\n }\n\n clear(): void {\n this.index.clear();\n this.index = this.getIndex();\n this.documentIds.clear();\n }\n\n async export(handler: (key: string, data: string) => Promise<void>): Promise<{ keys: string[] }> {\n const keys: string[] = [];\n\n await handler(PEAM_DOCUMENT_IDS_KEY, JSON.stringify(Array.from(this.documentIds)));\n keys.push(PEAM_DOCUMENT_IDS_KEY);\n\n await this.index.export(async (key: string, data: string) => {\n keys.push(key);\n await handler(key, data);\n });\n\n log.debug('Exported keys:', keys.length);\n\n return { keys };\n }\n\n async import(handler: (key: string) => Promise<string>, keys: string[]): Promise<void> {\n const documentIdsData = await handler(PEAM_DOCUMENT_IDS_KEY);\n if (documentIdsData) {\n const parsed = typeof documentIdsData === 'string' ? JSON.parse(documentIdsData) : documentIdsData;\n this.documentIds = new Set(parsed);\n }\n\n for (const key of keys) {\n if (key === PEAM_DOCUMENT_IDS_KEY) {\n continue;\n }\n\n try {\n const data = await handler(key);\n if (data) {\n this.index.import(key, data);\n }\n } catch (error) {\n log.error('Error importing key:', key, error);\n }\n }\n\n this.initialized = true;\n log.debug('Import completed with keys:', keys.length);\n }\n}\n","import type { StructuredPage } from '@peam-ai/parser';\nimport { SearchEngine } from './searchEngine';\n\nexport interface PageToIndex {\n path: string;\n structuredPage: StructuredPage;\n}\n\nexport interface SearchIndexData {\n keys: string[];\n data: Record<string, string>;\n}\n\n/**\n * Build a search index from structured pages\n */\nexport async function buildSearchIndex(pages: PageToIndex[]): Promise<SearchIndexData> {\n const searchEngine = new SearchEngine();\n await searchEngine.initialize();\n\n for (const page of pages) {\n await searchEngine.addPage(page.path, page.structuredPage);\n }\n\n const exportedData: Record<string, string> = {};\n const result = await searchEngine.export(async (key, data) => {\n exportedData[key] = data;\n });\n\n return {\n keys: result.keys,\n data: exportedData,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACAA,IAAAA,iBAAwB;;;ACAxB,oBAAwB;AACxB,wBAAkC;AASlC,IAAM,wBAAwB;AAC9B,IAAM,yBAAyB;AAC/B,IAAM,MAAM,sBAAQ;AAEb,IAAM,aAAN,MAAiB;AAAA,EAKtB,cAAc;AACZ,SAAK,cAAc;AACnB,SAAK,QAAQ,KAAK,SAAS;AAC3B,SAAK,cAA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]}
|
package/dist/index.mjs
ADDED
|
@@ -0,0 +1,263 @@
|
|
|
1
|
+
var __async = (__this, __arguments, generator) => {
|
|
2
|
+
return new Promise((resolve, reject) => {
|
|
3
|
+
var fulfilled = (value) => {
|
|
4
|
+
try {
|
|
5
|
+
step(generator.next(value));
|
|
6
|
+
} catch (e) {
|
|
7
|
+
reject(e);
|
|
8
|
+
}
|
|
9
|
+
};
|
|
10
|
+
var rejected = (value) => {
|
|
11
|
+
try {
|
|
12
|
+
step(generator.throw(value));
|
|
13
|
+
} catch (e) {
|
|
14
|
+
reject(e);
|
|
15
|
+
}
|
|
16
|
+
};
|
|
17
|
+
var step = (x) => x.done ? resolve(x.value) : Promise.resolve(x.value).then(fulfilled, rejected);
|
|
18
|
+
step((generator = generator.apply(__this, __arguments)).next());
|
|
19
|
+
});
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// src/searchEngine.ts
|
|
23
|
+
import { loggers as loggers2 } from "@peam-ai/logger";
|
|
24
|
+
|
|
25
|
+
// src/textSearch.ts
|
|
26
|
+
import { loggers } from "@peam-ai/logger";
|
|
27
|
+
import { Charset, Document } from "flexsearch";
|
|
28
|
+
var PEAM_DOCUMENT_IDS_KEY = "peam.documentIds";
|
|
29
|
+
var MAX_DOCUMENTS_RETRIEVE = 25;
|
|
30
|
+
var log = loggers.search;
|
|
31
|
+
var TextSearch = class {
|
|
32
|
+
constructor() {
|
|
33
|
+
this.initialized = false;
|
|
34
|
+
this.index = this.getIndex();
|
|
35
|
+
this.documentIds = /* @__PURE__ */ new Set();
|
|
36
|
+
}
|
|
37
|
+
getIndex() {
|
|
38
|
+
return new Document({
|
|
39
|
+
worker: false,
|
|
40
|
+
document: {
|
|
41
|
+
id: "path",
|
|
42
|
+
index: ["content:title", "content:description", "content:textContent", "content:author", "content:keywords"],
|
|
43
|
+
store: true
|
|
44
|
+
},
|
|
45
|
+
tokenize: "forward",
|
|
46
|
+
resolution: 9,
|
|
47
|
+
context: {
|
|
48
|
+
resolution: 3,
|
|
49
|
+
depth: 2,
|
|
50
|
+
bidirectional: true
|
|
51
|
+
},
|
|
52
|
+
cache: 100,
|
|
53
|
+
encoder: Charset.LatinExtra
|
|
54
|
+
});
|
|
55
|
+
}
|
|
56
|
+
initialize() {
|
|
57
|
+
return __async(this, null, function* () {
|
|
58
|
+
if (this.initialized) {
|
|
59
|
+
log.debug("Text search already initialized");
|
|
60
|
+
return;
|
|
61
|
+
}
|
|
62
|
+
this.initialized = true;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
addDocument(document) {
|
|
66
|
+
return __async(this, null, function* () {
|
|
67
|
+
if (!this.initialized) {
|
|
68
|
+
throw new Error("TextSearch not initialized. Call initialize() first.");
|
|
69
|
+
}
|
|
70
|
+
log.debug("Adding document to text search:", document.path);
|
|
71
|
+
this.index.add(document);
|
|
72
|
+
this.documentIds.add(document.path);
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
search(_0) {
|
|
76
|
+
return __async(this, arguments, function* (query, options = {}) {
|
|
77
|
+
if (!this.initialized) {
|
|
78
|
+
throw new Error("TextSearch not initialized. Call initialize() first.");
|
|
79
|
+
}
|
|
80
|
+
const limit = options.limit || MAX_DOCUMENTS_RETRIEVE;
|
|
81
|
+
const offset = options.offset || 0;
|
|
82
|
+
log.debug("Searching for:", query);
|
|
83
|
+
const results = yield this.index.search(query, {
|
|
84
|
+
limit: limit + offset,
|
|
85
|
+
suggest: options.suggest,
|
|
86
|
+
enrich: true
|
|
87
|
+
});
|
|
88
|
+
const pathSet = /* @__PURE__ */ new Set();
|
|
89
|
+
const documents = [];
|
|
90
|
+
for (const fieldResults of results) {
|
|
91
|
+
if (Array.isArray(fieldResults.result)) {
|
|
92
|
+
for (const result of fieldResults.result) {
|
|
93
|
+
const id = typeof result === "object" && "id" in result ? result.id : result;
|
|
94
|
+
const doc = typeof result === "object" && "doc" in result ? result.doc : null;
|
|
95
|
+
if (!pathSet.has(id) && doc) {
|
|
96
|
+
pathSet.add(id);
|
|
97
|
+
documents.push(doc);
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
const pagedResults = documents.slice(offset, offset + limit);
|
|
103
|
+
return pagedResults;
|
|
104
|
+
});
|
|
105
|
+
}
|
|
106
|
+
count() {
|
|
107
|
+
return this.documentIds.size;
|
|
108
|
+
}
|
|
109
|
+
getDocument(path) {
|
|
110
|
+
return this.index.get(path);
|
|
111
|
+
}
|
|
112
|
+
getAllDocuments(limit) {
|
|
113
|
+
const documents = [];
|
|
114
|
+
let count = 0;
|
|
115
|
+
limit = limit || MAX_DOCUMENTS_RETRIEVE;
|
|
116
|
+
for (const id of this.documentIds) {
|
|
117
|
+
if (count >= limit) {
|
|
118
|
+
break;
|
|
119
|
+
}
|
|
120
|
+
const doc = this.index.get(id);
|
|
121
|
+
if (doc) {
|
|
122
|
+
documents.push(doc);
|
|
123
|
+
count++;
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
log.debug("Retrieved documents from store (limit):", documents.length, limit);
|
|
127
|
+
return documents;
|
|
128
|
+
}
|
|
129
|
+
clear() {
|
|
130
|
+
this.index.clear();
|
|
131
|
+
this.index = this.getIndex();
|
|
132
|
+
this.documentIds.clear();
|
|
133
|
+
}
|
|
134
|
+
export(handler) {
|
|
135
|
+
return __async(this, null, function* () {
|
|
136
|
+
const keys = [];
|
|
137
|
+
yield handler(PEAM_DOCUMENT_IDS_KEY, JSON.stringify(Array.from(this.documentIds)));
|
|
138
|
+
keys.push(PEAM_DOCUMENT_IDS_KEY);
|
|
139
|
+
yield this.index.export((key, data) => __async(this, null, function* () {
|
|
140
|
+
keys.push(key);
|
|
141
|
+
yield handler(key, data);
|
|
142
|
+
}));
|
|
143
|
+
log.debug("Exported keys:", keys.length);
|
|
144
|
+
return { keys };
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
import(handler, keys) {
|
|
148
|
+
return __async(this, null, function* () {
|
|
149
|
+
const documentIdsData = yield handler(PEAM_DOCUMENT_IDS_KEY);
|
|
150
|
+
if (documentIdsData) {
|
|
151
|
+
const parsed = typeof documentIdsData === "string" ? JSON.parse(documentIdsData) : documentIdsData;
|
|
152
|
+
this.documentIds = new Set(parsed);
|
|
153
|
+
}
|
|
154
|
+
for (const key of keys) {
|
|
155
|
+
if (key === PEAM_DOCUMENT_IDS_KEY) {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
try {
|
|
159
|
+
const data = yield handler(key);
|
|
160
|
+
if (data) {
|
|
161
|
+
this.index.import(key, data);
|
|
162
|
+
}
|
|
163
|
+
} catch (error) {
|
|
164
|
+
log.error("Error importing key:", key, error);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
this.initialized = true;
|
|
168
|
+
log.debug("Import completed with keys:", keys.length);
|
|
169
|
+
});
|
|
170
|
+
}
|
|
171
|
+
};
|
|
172
|
+
|
|
173
|
+
// src/searchEngine.ts
|
|
174
|
+
var log2 = loggers2.search;
|
|
175
|
+
var SearchEngine = class {
|
|
176
|
+
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
177
|
+
constructor(_config) {
|
|
178
|
+
this.textSearch = new TextSearch();
|
|
179
|
+
this.initialized = false;
|
|
180
|
+
}
|
|
181
|
+
initialize() {
|
|
182
|
+
return __async(this, null, function* () {
|
|
183
|
+
if (this.initialized) {
|
|
184
|
+
return;
|
|
185
|
+
}
|
|
186
|
+
log2.debug("Initializing search engine");
|
|
187
|
+
yield this.textSearch.initialize();
|
|
188
|
+
this.initialized = true;
|
|
189
|
+
});
|
|
190
|
+
}
|
|
191
|
+
addPage(path, content) {
|
|
192
|
+
return __async(this, null, function* () {
|
|
193
|
+
if (!this.initialized) {
|
|
194
|
+
throw new Error("Search engine not initialized. Call initialize() first.");
|
|
195
|
+
}
|
|
196
|
+
const document = {
|
|
197
|
+
id: path,
|
|
198
|
+
path,
|
|
199
|
+
content
|
|
200
|
+
};
|
|
201
|
+
yield this.textSearch.addDocument(document);
|
|
202
|
+
log2.debug("Page added to search engine:", path);
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
search(_0) {
|
|
206
|
+
return __async(this, arguments, function* (query, options = {}) {
|
|
207
|
+
if (!this.initialized) {
|
|
208
|
+
throw new Error("Search engine not initialized. Call initialize() first.");
|
|
209
|
+
}
|
|
210
|
+
log2.debug("Performing text search:", query);
|
|
211
|
+
return this.textSearch.search(query, options);
|
|
212
|
+
});
|
|
213
|
+
}
|
|
214
|
+
count() {
|
|
215
|
+
return this.textSearch.count();
|
|
216
|
+
}
|
|
217
|
+
getDocument(path) {
|
|
218
|
+
return this.textSearch.getDocument(path);
|
|
219
|
+
}
|
|
220
|
+
getAllDocuments(limit) {
|
|
221
|
+
return this.textSearch.getAllDocuments(limit);
|
|
222
|
+
}
|
|
223
|
+
clear() {
|
|
224
|
+
this.textSearch.clear();
|
|
225
|
+
}
|
|
226
|
+
export(handler) {
|
|
227
|
+
return __async(this, null, function* () {
|
|
228
|
+
return this.textSearch.export(handler);
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
import(handler, keys) {
|
|
232
|
+
return __async(this, null, function* () {
|
|
233
|
+
yield this.textSearch.import(handler, keys);
|
|
234
|
+
this.initialized = true;
|
|
235
|
+
log2.debug("Search engine initialized from imported data");
|
|
236
|
+
});
|
|
237
|
+
}
|
|
238
|
+
};
|
|
239
|
+
|
|
240
|
+
// src/indexBuilder.ts
|
|
241
|
+
function buildSearchIndex(pages) {
|
|
242
|
+
return __async(this, null, function* () {
|
|
243
|
+
const searchEngine = new SearchEngine();
|
|
244
|
+
yield searchEngine.initialize();
|
|
245
|
+
for (const page of pages) {
|
|
246
|
+
yield searchEngine.addPage(page.path, page.structuredPage);
|
|
247
|
+
}
|
|
248
|
+
const exportedData = {};
|
|
249
|
+
const result = yield searchEngine.export((key, data) => __async(null, null, function* () {
|
|
250
|
+
exportedData[key] = data;
|
|
251
|
+
}));
|
|
252
|
+
return {
|
|
253
|
+
keys: result.keys,
|
|
254
|
+
data: exportedData
|
|
255
|
+
};
|
|
256
|
+
});
|
|
257
|
+
}
|
|
258
|
+
export {
|
|
259
|
+
SearchEngine,
|
|
260
|
+
TextSearch,
|
|
261
|
+
buildSearchIndex
|
|
262
|
+
};
|
|
263
|
+
//# sourceMappingURL=index.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/searchEngine.ts","../src/textSearch.ts","../src/indexBuilder.ts"],"sourcesContent":["import { loggers } from '@peam-ai/logger';\nimport type { StructuredPage } from '@peam-ai/parser';\nimport { TextSearch, type TextSearchOptions } from './textSearch';\nimport type { StructuredPageDocumentData } from './types';\n\nconst log = loggers.search;\n\n// eslint-disable-next-line @typescript-eslint/no-empty-object-type\nexport interface SearchEngineConfig {\n // Reserved for future configuration options\n}\n\nexport class SearchEngine {\n private textSearch: TextSearch;\n private initialized: boolean;\n\n // eslint-disable-next-line @typescript-eslint/no-unused-vars\n constructor(_config?: SearchEngineConfig) {\n this.textSearch = new TextSearch();\n this.initialized = false;\n }\n\n async initialize(): Promise<void> {\n if (this.initialized) {\n return;\n }\n\n log.debug('Initializing search engine');\n await this.textSearch.initialize();\n this.initialized = true;\n }\n\n async addPage(path: string, content: StructuredPage): Promise<void> {\n if (!this.initialized) {\n throw new Error('Search engine not initialized. Call initialize() first.');\n }\n\n const document: StructuredPageDocumentData = {\n id: path,\n path,\n content,\n };\n\n await this.textSearch.addDocument(document);\n log.debug('Page added to search engine:', path);\n }\n\n async search(query: string, options: TextSearchOptions = {}): Promise<StructuredPageDocumentData[]> {\n if (!this.initialized) {\n throw new Error('Search engine not initialized. Call initialize() first.');\n }\n\n log.debug('Performing text search:', query);\n return this.textSearch.search(query, options);\n }\n\n count(): number {\n return this.textSearch.count();\n }\n\n getDocument(path: string) {\n return this.textSearch.getDocument(path);\n }\n\n getAllDocuments(limit?: number): StructuredPageDocumentData[] {\n return this.textSearch.getAllDocuments(limit);\n }\n\n clear(): void {\n this.textSearch.clear();\n }\n\n async export(handler: (key: string, data: string) => Promise<void>): Promise<{ keys: string[] }> {\n return this.textSearch.export(handler);\n }\n\n async import(handler: (key: string) => Promise<string>, keys: string[]): Promise<void> {\n await this.textSearch.import(handler, keys);\n this.initialized = true;\n log.debug('Search engine initialized from imported data');\n }\n}\n","import { loggers } from '@peam-ai/logger';\nimport { Charset, Document } from 'flexsearch';\nimport type { StructuredPageDocumentData } from './types';\n\nexport interface TextSearchOptions {\n limit?: number;\n offset?: number;\n suggest?: boolean;\n}\n\nconst PEAM_DOCUMENT_IDS_KEY = 'peam.documentIds';\nconst MAX_DOCUMENTS_RETRIEVE = 25;\nconst log = loggers.search;\n\nexport class TextSearch {\n private index: Document<StructuredPageDocumentData>;\n private initialized: boolean;\n private documentIds: Set<string>;\n\n constructor() {\n this.initialized = false;\n this.index = this.getIndex();\n this.documentIds = new Set();\n }\n\n private getIndex() {\n return new Document<StructuredPageDocumentData>({\n worker: false,\n document: {\n id: 'path',\n index: ['content:title', 'content:description', 'content:textContent', 'content:author', 'content:keywords'],\n store: true,\n },\n tokenize: 'forward',\n resolution: 9,\n context: {\n resolution: 3,\n depth: 2,\n bidirectional: true,\n },\n cache: 100,\n encoder: Charset.LatinExtra,\n });\n }\n\n async initialize(): Promise<void> {\n if (this.initialized) {\n log.debug('Text search already initialized');\n return;\n }\n\n this.initialized = true;\n }\n\n async addDocument(document: StructuredPageDocumentData): Promise<void> {\n if (!this.initialized) {\n throw new Error('TextSearch not initialized. Call initialize() first.');\n }\n\n log.debug('Adding document to text search:', document.path);\n\n this.index.add(document);\n this.documentIds.add(document.path);\n }\n\n async search(query: string, options: TextSearchOptions = {}): Promise<StructuredPageDocumentData[]> {\n if (!this.initialized) {\n throw new Error('TextSearch not initialized. Call initialize() first.');\n }\n\n const limit = options.limit || MAX_DOCUMENTS_RETRIEVE;\n const offset = options.offset || 0;\n\n log.debug('Searching for:', query);\n\n const results = await this.index.search(query, {\n limit: limit + offset,\n suggest: options.suggest,\n enrich: true,\n });\n\n const pathSet = new Set<string>();\n const documents: StructuredPageDocumentData[] = [];\n\n for (const fieldResults of results) {\n if (Array.isArray(fieldResults.result)) {\n for (const result of fieldResults.result) {\n const id = typeof result === 'object' && 'id' in result ? result.id : result;\n const doc = typeof result === 'object' && 'doc' in result ? result.doc : null;\n\n if (!pathSet.has(id as string) && doc) {\n pathSet.add(id as string);\n documents.push(doc);\n }\n }\n }\n }\n\n const pagedResults = documents.slice(offset, offset + limit);\n\n return pagedResults;\n }\n\n count(): number {\n return this.documentIds.size;\n }\n\n getDocument(path: string) {\n return this.index.get(path);\n }\n\n getAllDocuments(limit?: number): StructuredPageDocumentData[] {\n const documents: StructuredPageDocumentData[] = [];\n let count = 0;\n limit = limit || MAX_DOCUMENTS_RETRIEVE;\n\n for (const id of this.documentIds) {\n if (count >= limit) {\n break;\n }\n\n const doc = this.index.get(id);\n if (doc) {\n documents.push(doc);\n count++;\n }\n }\n\n log.debug('Retrieved documents from store (limit):', documents.length, limit);\n return documents;\n }\n\n clear(): void {\n this.index.clear();\n this.index = this.getIndex();\n this.documentIds.clear();\n }\n\n async export(handler: (key: string, data: string) => Promise<void>): Promise<{ keys: string[] }> {\n const keys: string[] = [];\n\n await handler(PEAM_DOCUMENT_IDS_KEY, JSON.stringify(Array.from(this.documentIds)));\n keys.push(PEAM_DOCUMENT_IDS_KEY);\n\n await this.index.export(async (key: string, data: string) => {\n keys.push(key);\n await handler(key, data);\n });\n\n log.debug('Exported keys:', keys.length);\n\n return { keys };\n }\n\n async import(handler: (key: string) => Promise<string>, keys: string[]): Promise<void> {\n const documentIdsData = await handler(PEAM_DOCUMENT_IDS_KEY);\n if (documentIdsData) {\n const parsed = typeof documentIdsData === 'string' ? JSON.parse(documentIdsData) : documentIdsData;\n this.documentIds = new Set(parsed);\n }\n\n for (const key of keys) {\n if (key === PEAM_DOCUMENT_IDS_KEY) {\n continue;\n }\n\n try {\n const data = await handler(key);\n if (data) {\n this.index.import(key, data);\n }\n } catch (error) {\n log.error('Error importing key:', key, error);\n }\n }\n\n this.initialized = true;\n log.debug('Import completed with keys:', keys.length);\n }\n}\n","import type { StructuredPage } from '@peam-ai/parser';\nimport { SearchEngine } from './searchEngine';\n\nexport interface PageToIndex {\n path: string;\n structuredPage: StructuredPage;\n}\n\nexport interface SearchIndexData {\n keys: string[];\n data: Record<string, string>;\n}\n\n/**\n * Build a search index from structured pages\n */\nexport async function buildSearchIndex(pages: PageToIndex[]): Promise<SearchIndexData> {\n const searchEngine = new SearchEngine();\n await searchEngine.initialize();\n\n for (const page of pages) {\n await searchEngine.addPage(page.path, page.structuredPage);\n }\n\n const exportedData: Record<string, string> = {};\n const result = await searchEngine.export(async (key, data) => {\n exportedData[key] = data;\n });\n\n return {\n keys: result.keys,\n data: exportedData,\n };\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;;;AAAA,SAAS,WAAAA,gBAAe;;;ACAxB,SAAS,eAAe;AACxB,SAAS,SAAS,gBAAgB;AASlC,IAAM,wBAAwB;AAC9B,IAAM,yBAAyB;AAC/B,IAAM,MAAM,QAAQ;AAEb,IAAM,aAAN,MAAiB;AAAA,EAKtB,cAAc;AACZ,SAAK,cAAc;AACnB,SAAK,QAAQ,KAAK,SAAS;AAC3B,SAAK,cAAc,oBAAI,IAAI;AAAA,EAC7B;AAAA,EAEQ,WAAW;AACjB,WAAO,IAAI,SAAqC;AAAA,MAC9C,QAAQ;AAAA,MACR,UAAU;AAAA,QACR,IAAI;AAAA,QACJ,OAAO,CAAC,iBAAiB,uBAAuB,uBAAuB,kBAAkB,kBAAkB;AAAA,QAC3G,OAAO;AAAA,MACT;AAAA,MACA,UAAU;AAAA,MACV,YAAY;AAAA,MACZ,SAAS;AAAA,QACP,YAAY;AAAA,QACZ,OAAO;AAAA,QACP,eAAe;AAAA,MACjB;AAAA,MACA,OAAO;AAAA,MACP,SAAS,QAAQ;AAAA,IACnB,CAAC;AAAA,EACH;AAAA,EAEM,aAA4B;AAAA;AAChC,UAAI,KAAK,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AAAA,EAEA,gBAAgB,OAA8C;AAC5D,WAAO,KAAK,WAAW,gBAAgB,KAAK;AAAA,EAC9C;AAAA,EAEA,QAAc;AACZ,SAAK,WAAW,MAAM;AAAA,EACxB;AAAA,EAEM,OAAO,SAAoF;AAAA;AAC/F,aAAO,KAAK,WAAW,OAAO,OAAO;AAAA,IACvC;AAAA;AAAA,EAEM,OAAO,SAA2C,MAA+B;AAAA;AACrF,YAAM,KAAK,WAAW,OAAO,SAAS,IAAI;AAC1C,WAAK,cAAc;AACnB,MAAAA,KAAI,MAAM,8CAA8C;AAAA,IAC1D;AAAA;AACF;;;AEjEA,SAAsB,iBAAiB,OAAgD;AAAA;AACrF,UAAM,eAAe,IAAI,aAAa;AACtC,UAAM,aAAa,WAAW;AAE9B,eAAW,QAAQ,OAAO;AACxB,YAAM,aAAa,QAAQ,KAAK,MAAM,KAAK,cAAc;AAAA,IAC3D;AAEA,UAAM,eAAuC,CAAC;AAC9C,UAAM,SAAS,MAAM,aAAa,OAAO,CAAO,KAAK,SAAS;AAC5D,mBAAa,GAAG,IAAI;AAAA,IACtB,EAAC;AAED,WAAO;AAAA,MACL,MAAM,OAAO;AAAA,MACb,MAAM;AAAA,IACR;AAAA,EACF;AAAA;","names":["loggers","log","loggers"]}
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@peam-ai/search",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Vector database and search functionality for Peam",
|
|
5
|
+
"main": "./dist/index.js",
|
|
6
|
+
"module": "./dist/index.mjs",
|
|
7
|
+
"types": "./dist/index.d.ts",
|
|
8
|
+
"homepage": "https://peam.ai",
|
|
9
|
+
"author": "Afshin Mehrabani",
|
|
10
|
+
"license": "Apache-2.0",
|
|
11
|
+
"repository": {
|
|
12
|
+
"type": "git",
|
|
13
|
+
"url": "https://github.com/peam-ai/peam.git",
|
|
14
|
+
"directory": "packages/search"
|
|
15
|
+
},
|
|
16
|
+
"bugs": {
|
|
17
|
+
"url": "https://github.com/peam-ai/peam/issues"
|
|
18
|
+
},
|
|
19
|
+
"files": [
|
|
20
|
+
"dist/**/*"
|
|
21
|
+
],
|
|
22
|
+
"exports": {
|
|
23
|
+
"./package.json": "./package.json",
|
|
24
|
+
".": {
|
|
25
|
+
"types": "./dist/index.d.ts",
|
|
26
|
+
"import": "./dist/index.mjs",
|
|
27
|
+
"require": "./dist/index.js"
|
|
28
|
+
}
|
|
29
|
+
},
|
|
30
|
+
"dependencies": {
|
|
31
|
+
"flexsearch": "^0.8.212",
|
|
32
|
+
"@peam-ai/logger": "0.1.1",
|
|
33
|
+
"@peam-ai/parser": "0.1.1"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/node": "^22.10.2",
|
|
37
|
+
"tsup": "^8.2.4",
|
|
38
|
+
"typescript": "^5.5.4"
|
|
39
|
+
},
|
|
40
|
+
"scripts": {
|
|
41
|
+
"build": "tsup",
|
|
42
|
+
"dev": "tsup --watch",
|
|
43
|
+
"clean": "rm -rf dist",
|
|
44
|
+
"lint": "eslint .",
|
|
45
|
+
"test:eslint": "eslint \"src/**/*.ts*\"",
|
|
46
|
+
"test:prettier": "prettier --check \"src/**/*.ts*\"",
|
|
47
|
+
"test:jest": "jest"
|
|
48
|
+
}
|
|
49
|
+
}
|