@contractspec/lib.knowledge 1.56.1 → 1.58.0
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/access/guard.d.ts +13 -17
- package/dist/access/guard.d.ts.map +1 -1
- package/dist/access/guard.js +60 -49
- package/dist/access/index.d.ts +2 -2
- package/dist/access/index.d.ts.map +1 -0
- package/dist/access/index.js +60 -2
- package/dist/index.d.ts +6 -12
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +455 -12
- package/dist/ingestion/document-processor.d.ts +18 -20
- package/dist/ingestion/document-processor.d.ts.map +1 -1
- package/dist/ingestion/document-processor.js +63 -53
- package/dist/ingestion/embedding-service.d.ts +7 -11
- package/dist/ingestion/embedding-service.d.ts.map +1 -1
- package/dist/ingestion/embedding-service.js +26 -25
- package/dist/ingestion/gmail-adapter.d.ts +13 -17
- package/dist/ingestion/gmail-adapter.d.ts.map +1 -1
- package/dist/ingestion/gmail-adapter.js +67 -46
- package/dist/ingestion/index.d.ts +6 -6
- package/dist/ingestion/index.d.ts.map +1 -0
- package/dist/ingestion/index.js +221 -6
- package/dist/ingestion/storage-adapter.d.ts +10 -14
- package/dist/ingestion/storage-adapter.d.ts.map +1 -1
- package/dist/ingestion/storage-adapter.js +31 -26
- package/dist/ingestion/vector-indexer.d.ts +11 -15
- package/dist/ingestion/vector-indexer.d.ts.map +1 -1
- package/dist/ingestion/vector-indexer.js +32 -32
- package/dist/node/access/guard.js +60 -0
- package/dist/node/access/index.js +60 -0
- package/dist/node/index.js +454 -0
- package/dist/node/ingestion/document-processor.js +64 -0
- package/dist/node/ingestion/embedding-service.js +26 -0
- package/dist/node/ingestion/gmail-adapter.js +72 -0
- package/dist/node/ingestion/index.js +221 -0
- package/dist/node/ingestion/storage-adapter.js +31 -0
- package/dist/node/ingestion/vector-indexer.js +32 -0
- package/dist/node/query/index.js +79 -0
- package/dist/node/query/service.js +79 -0
- package/dist/node/retriever/index.js +100 -0
- package/dist/node/retriever/interface.js +0 -0
- package/dist/node/retriever/static-retriever.js +43 -0
- package/dist/node/retriever/vector-retriever.js +58 -0
- package/dist/node/types.js +0 -0
- package/dist/query/index.d.ts +2 -2
- package/dist/query/index.d.ts.map +1 -0
- package/dist/query/index.js +79 -2
- package/dist/query/service.d.ts +20 -24
- package/dist/query/service.d.ts.map +1 -1
- package/dist/query/service.js +76 -62
- package/dist/retriever/index.d.ts +4 -4
- package/dist/retriever/index.d.ts.map +1 -0
- package/dist/retriever/index.js +100 -3
- package/dist/retriever/interface.d.ts +38 -43
- package/dist/retriever/interface.d.ts.map +1 -1
- package/dist/retriever/interface.js +1 -0
- package/dist/retriever/static-retriever.d.ts +13 -18
- package/dist/retriever/static-retriever.d.ts.map +1 -1
- package/dist/retriever/static-retriever.js +42 -46
- package/dist/retriever/vector-retriever.d.ts +23 -28
- package/dist/retriever/vector-retriever.d.ts.map +1 -1
- package/dist/retriever/vector-retriever.js +57 -59
- package/dist/types.d.ts +34 -39
- package/dist/types.d.ts.map +1 -1
- package/dist/types.js +1 -0
- package/package.json +152 -45
- package/dist/access/guard.js.map +0 -1
- package/dist/ingestion/document-processor.js.map +0 -1
- package/dist/ingestion/embedding-service.js.map +0 -1
- package/dist/ingestion/gmail-adapter.js.map +0 -1
- package/dist/ingestion/storage-adapter.js.map +0 -1
- package/dist/ingestion/vector-indexer.js.map +0 -1
- package/dist/query/service.js.map +0 -1
- package/dist/retriever/static-retriever.js.map +0 -1
- package/dist/retriever/vector-retriever.js.map +0 -1
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
// src/retriever/vector-retriever.ts
|
|
2
|
+
class VectorRetriever {
|
|
3
|
+
config;
|
|
4
|
+
spaceCollections;
|
|
5
|
+
staticContent;
|
|
6
|
+
constructor(config) {
|
|
7
|
+
this.config = config;
|
|
8
|
+
this.spaceCollections = config.spaceCollections instanceof Map ? config.spaceCollections : new Map(Object.entries(config.spaceCollections));
|
|
9
|
+
this.staticContent = config.staticContent ? config.staticContent instanceof Map ? config.staticContent : new Map(Object.entries(config.staticContent)) : new Map;
|
|
10
|
+
}
|
|
11
|
+
async retrieve(query, options) {
|
|
12
|
+
const collection = this.spaceCollections.get(options.spaceKey);
|
|
13
|
+
if (!collection) {
|
|
14
|
+
return [];
|
|
15
|
+
}
|
|
16
|
+
const embedding = await this.config.embeddings.embedQuery(query);
|
|
17
|
+
const results = await this.config.vectorStore.search({
|
|
18
|
+
collection,
|
|
19
|
+
vector: embedding.vector,
|
|
20
|
+
topK: options.topK ?? this.config.defaultTopK ?? 5,
|
|
21
|
+
namespace: options.tenantId,
|
|
22
|
+
filter: options.filter
|
|
23
|
+
});
|
|
24
|
+
const minScore = options.minScore ?? this.config.defaultMinScore ?? 0;
|
|
25
|
+
const filtered = results.filter((r) => r.score >= minScore);
|
|
26
|
+
return filtered.map((result) => ({
|
|
27
|
+
content: this.extractContent(result.payload),
|
|
28
|
+
source: result.id,
|
|
29
|
+
score: result.score,
|
|
30
|
+
metadata: result.payload
|
|
31
|
+
}));
|
|
32
|
+
}
|
|
33
|
+
async getStatic(spaceKey) {
|
|
34
|
+
return this.staticContent.get(spaceKey) ?? null;
|
|
35
|
+
}
|
|
36
|
+
supportsSpace(spaceKey) {
|
|
37
|
+
return this.spaceCollections.has(spaceKey);
|
|
38
|
+
}
|
|
39
|
+
listSpaces() {
|
|
40
|
+
return [...this.spaceCollections.keys()];
|
|
41
|
+
}
|
|
42
|
+
extractContent(payload) {
|
|
43
|
+
if (!payload)
|
|
44
|
+
return "";
|
|
45
|
+
if (typeof payload.text === "string")
|
|
46
|
+
return payload.text;
|
|
47
|
+
if (typeof payload.content === "string")
|
|
48
|
+
return payload.content;
|
|
49
|
+
return JSON.stringify(payload);
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
function createVectorRetriever(config) {
|
|
53
|
+
return new VectorRetriever(config);
|
|
54
|
+
}
|
|
55
|
+
export {
|
|
56
|
+
createVectorRetriever,
|
|
57
|
+
VectorRetriever
|
|
58
|
+
};
|
|
File without changes
|
package/dist/query/index.d.ts
CHANGED
|
@@ -1,2 +1,2 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
export * from './service';
|
|
2
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/query/index.ts"],"names":[],"mappings":"AAAA,cAAc,WAAW,CAAC"}
|
package/dist/query/index.js
CHANGED
|
@@ -1,3 +1,80 @@
|
|
|
1
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// src/query/service.ts
|
|
3
|
+
class KnowledgeQueryService {
|
|
4
|
+
embeddings;
|
|
5
|
+
vectorStore;
|
|
6
|
+
llm;
|
|
7
|
+
config;
|
|
8
|
+
constructor(embeddings, vectorStore, llm, config) {
|
|
9
|
+
this.embeddings = embeddings;
|
|
10
|
+
this.vectorStore = vectorStore;
|
|
11
|
+
this.llm = llm;
|
|
12
|
+
this.config = config;
|
|
13
|
+
}
|
|
14
|
+
async query(question) {
|
|
15
|
+
const embedding = await this.embeddings.embedQuery(question);
|
|
16
|
+
const results = await this.vectorStore.search({
|
|
17
|
+
collection: this.config.collection,
|
|
18
|
+
vector: embedding.vector,
|
|
19
|
+
topK: this.config.topK ?? 5,
|
|
20
|
+
namespace: this.config.namespace,
|
|
21
|
+
filter: undefined
|
|
22
|
+
});
|
|
23
|
+
const context = buildContext(results);
|
|
24
|
+
const messages = this.buildMessages(question, context);
|
|
25
|
+
const response = await this.llm.chat(messages);
|
|
26
|
+
return {
|
|
27
|
+
answer: response.message.content.map((part) => ("text" in part) ? part.text : "").join(""),
|
|
28
|
+
references: results.map((result) => ({
|
|
29
|
+
...result,
|
|
30
|
+
text: extractText(result)
|
|
31
|
+
})),
|
|
32
|
+
usage: response.usage
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
buildMessages(question, context) {
|
|
36
|
+
const systemPrompt = this.config.systemPrompt ?? "You are a knowledge assistant that answers questions using the provided context. Cite relevant sources if possible.";
|
|
37
|
+
return [
|
|
38
|
+
{
|
|
39
|
+
role: "system",
|
|
40
|
+
content: [{ type: "text", text: systemPrompt }]
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
role: "user",
|
|
44
|
+
content: [
|
|
45
|
+
{
|
|
46
|
+
type: "text",
|
|
47
|
+
text: `Question:
|
|
48
|
+
${question}
|
|
2
49
|
|
|
3
|
-
|
|
50
|
+
Context:
|
|
51
|
+
${context}`
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
function buildContext(results) {
|
|
59
|
+
if (results.length === 0) {
|
|
60
|
+
return "No relevant documents found.";
|
|
61
|
+
}
|
|
62
|
+
return results.map((result, index) => {
|
|
63
|
+
const text = extractText(result);
|
|
64
|
+
return `Source ${index + 1} (score: ${result.score.toFixed(3)}):
|
|
65
|
+
${text}`;
|
|
66
|
+
}).join(`
|
|
67
|
+
|
|
68
|
+
`);
|
|
69
|
+
}
|
|
70
|
+
function extractText(result) {
|
|
71
|
+
const payload = result.payload ?? {};
|
|
72
|
+
if (typeof payload.text === "string")
|
|
73
|
+
return payload.text;
|
|
74
|
+
if (typeof payload.content === "string")
|
|
75
|
+
return payload.content;
|
|
76
|
+
return JSON.stringify(payload);
|
|
77
|
+
}
|
|
78
|
+
export {
|
|
79
|
+
KnowledgeQueryService
|
|
80
|
+
};
|
package/dist/query/service.d.ts
CHANGED
|
@@ -1,28 +1,24 @@
|
|
|
1
|
-
import { EmbeddingProvider,
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
topK?: number;
|
|
8
|
-
systemPrompt?: string;
|
|
1
|
+
import type { EmbeddingProvider, VectorStoreProvider, VectorSearchResult, LLMProvider, LLMResponse } from '@contractspec/lib.contracts';
|
|
2
|
+
export interface KnowledgeQueryConfig {
|
|
3
|
+
collection: string;
|
|
4
|
+
namespace?: string;
|
|
5
|
+
topK?: number;
|
|
6
|
+
systemPrompt?: string;
|
|
9
7
|
}
|
|
10
|
-
interface KnowledgeAnswer {
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
8
|
+
export interface KnowledgeAnswer {
|
|
9
|
+
answer: string;
|
|
10
|
+
references: (VectorSearchResult & {
|
|
11
|
+
text?: string;
|
|
12
|
+
})[];
|
|
13
|
+
usage?: LLMResponse['usage'];
|
|
16
14
|
}
|
|
17
|
-
declare class KnowledgeQueryService {
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
15
|
+
export declare class KnowledgeQueryService {
|
|
16
|
+
private readonly embeddings;
|
|
17
|
+
private readonly vectorStore;
|
|
18
|
+
private readonly llm;
|
|
19
|
+
private readonly config;
|
|
20
|
+
constructor(embeddings: EmbeddingProvider, vectorStore: VectorStoreProvider, llm: LLMProvider, config: KnowledgeQueryConfig);
|
|
21
|
+
query(question: string): Promise<KnowledgeAnswer>;
|
|
22
|
+
private buildMessages;
|
|
25
23
|
}
|
|
26
|
-
//#endregion
|
|
27
|
-
export { KnowledgeAnswer, KnowledgeQueryConfig, KnowledgeQueryService };
|
|
28
24
|
//# sourceMappingURL=service.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"service.d.ts","
|
|
1
|
+
{"version":3,"file":"service.d.ts","sourceRoot":"","sources":["../../src/query/service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EACV,iBAAiB,EACjB,mBAAmB,EACnB,kBAAkB,EAClB,WAAW,EAEX,WAAW,EACZ,MAAM,6BAA6B,CAAC;AAErC,MAAM,WAAW,oBAAoB;IACnC,UAAU,EAAE,MAAM,CAAC;IACnB,SAAS,CAAC,EAAE,MAAM,CAAC;IACnB,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,YAAY,CAAC,EAAE,MAAM,CAAC;CACvB;AAED,MAAM,WAAW,eAAe;IAC9B,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,CAAC,kBAAkB,GAAG;QAChC,IAAI,CAAC,EAAE,MAAM,CAAC;KACf,CAAC,EAAE,CAAC;IACL,KAAK,CAAC,EAAE,WAAW,CAAC,OAAO,CAAC,CAAC;CAC9B;AAED,qBAAa,qBAAqB;IAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAoB;IAC/C,OAAO,CAAC,QAAQ,CAAC,WAAW,CAAsB;IAClD,OAAO,CAAC,QAAQ,CAAC,GAAG,CAAc;IAClC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAuB;gBAG5C,UAAU,EAAE,iBAAiB,EAC7B,WAAW,EAAE,mBAAmB,EAChC,GAAG,EAAE,WAAW,EAChB,MAAM,EAAE,oBAAoB;IAQxB,KAAK,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,eAAe,CAAC;IAwBvD,OAAO,CAAC,aAAa;CAoBtB"}
|
package/dist/query/service.js
CHANGED
|
@@ -1,66 +1,80 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// src/query/service.ts
|
|
3
|
+
class KnowledgeQueryService {
|
|
4
|
+
embeddings;
|
|
5
|
+
vectorStore;
|
|
6
|
+
llm;
|
|
7
|
+
config;
|
|
8
|
+
constructor(embeddings, vectorStore, llm, config) {
|
|
9
|
+
this.embeddings = embeddings;
|
|
10
|
+
this.vectorStore = vectorStore;
|
|
11
|
+
this.llm = llm;
|
|
12
|
+
this.config = config;
|
|
13
|
+
}
|
|
14
|
+
async query(question) {
|
|
15
|
+
const embedding = await this.embeddings.embedQuery(question);
|
|
16
|
+
const results = await this.vectorStore.search({
|
|
17
|
+
collection: this.config.collection,
|
|
18
|
+
vector: embedding.vector,
|
|
19
|
+
topK: this.config.topK ?? 5,
|
|
20
|
+
namespace: this.config.namespace,
|
|
21
|
+
filter: undefined
|
|
22
|
+
});
|
|
23
|
+
const context = buildContext(results);
|
|
24
|
+
const messages = this.buildMessages(question, context);
|
|
25
|
+
const response = await this.llm.chat(messages);
|
|
26
|
+
return {
|
|
27
|
+
answer: response.message.content.map((part) => ("text" in part) ? part.text : "").join(""),
|
|
28
|
+
references: results.map((result) => ({
|
|
29
|
+
...result,
|
|
30
|
+
text: extractText(result)
|
|
31
|
+
})),
|
|
32
|
+
usage: response.usage
|
|
33
|
+
};
|
|
34
|
+
}
|
|
35
|
+
buildMessages(question, context) {
|
|
36
|
+
const systemPrompt = this.config.systemPrompt ?? "You are a knowledge assistant that answers questions using the provided context. Cite relevant sources if possible.";
|
|
37
|
+
return [
|
|
38
|
+
{
|
|
39
|
+
role: "system",
|
|
40
|
+
content: [{ type: "text", text: systemPrompt }]
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
role: "user",
|
|
44
|
+
content: [
|
|
45
|
+
{
|
|
46
|
+
type: "text",
|
|
47
|
+
text: `Question:
|
|
48
|
+
${question}
|
|
49
|
+
|
|
50
|
+
Context:
|
|
51
|
+
${context}`
|
|
52
|
+
}
|
|
53
|
+
]
|
|
54
|
+
}
|
|
55
|
+
];
|
|
56
|
+
}
|
|
57
|
+
}
|
|
50
58
|
function buildContext(results) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
59
|
+
if (results.length === 0) {
|
|
60
|
+
return "No relevant documents found.";
|
|
61
|
+
}
|
|
62
|
+
return results.map((result, index) => {
|
|
63
|
+
const text = extractText(result);
|
|
64
|
+
return `Source ${index + 1} (score: ${result.score.toFixed(3)}):
|
|
65
|
+
${text}`;
|
|
66
|
+
}).join(`
|
|
67
|
+
|
|
68
|
+
`);
|
|
56
69
|
}
|
|
57
70
|
function extractText(result) {
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
71
|
+
const payload = result.payload ?? {};
|
|
72
|
+
if (typeof payload.text === "string")
|
|
73
|
+
return payload.text;
|
|
74
|
+
if (typeof payload.content === "string")
|
|
75
|
+
return payload.content;
|
|
76
|
+
return JSON.stringify(payload);
|
|
62
77
|
}
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
//# sourceMappingURL=service.js.map
|
|
78
|
+
export {
|
|
79
|
+
KnowledgeQueryService
|
|
80
|
+
};
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
1
|
+
export * from './interface';
|
|
2
|
+
export * from './static-retriever';
|
|
3
|
+
export * from './vector-retriever';
|
|
4
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/retriever/index.ts"],"names":[],"mappings":"AAAA,cAAc,aAAa,CAAC;AAC5B,cAAc,oBAAoB,CAAC;AACnC,cAAc,oBAAoB,CAAC"}
|
package/dist/retriever/index.js
CHANGED
|
@@ -1,4 +1,101 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
1
|
+
// @bun
|
|
2
|
+
// src/retriever/static-retriever.ts
|
|
3
|
+
class StaticRetriever {
|
|
4
|
+
content;
|
|
5
|
+
constructor(config) {
|
|
6
|
+
this.content = config.content instanceof Map ? config.content : new Map(Object.entries(config.content));
|
|
7
|
+
}
|
|
8
|
+
async retrieve(query, options) {
|
|
9
|
+
const content = this.content.get(options.spaceKey);
|
|
10
|
+
if (!content)
|
|
11
|
+
return [];
|
|
12
|
+
const queryLower = query.toLowerCase();
|
|
13
|
+
const lines = content.split(`
|
|
14
|
+
`).filter((line) => line.trim());
|
|
15
|
+
const results = [];
|
|
16
|
+
for (const line of lines) {
|
|
17
|
+
if (line.toLowerCase().includes(queryLower)) {
|
|
18
|
+
results.push({
|
|
19
|
+
content: line,
|
|
20
|
+
source: options.spaceKey,
|
|
21
|
+
score: 1,
|
|
22
|
+
metadata: { type: "static" }
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
return results.slice(0, options.topK ?? 5);
|
|
27
|
+
}
|
|
28
|
+
async getStatic(spaceKey) {
|
|
29
|
+
return this.content.get(spaceKey) ?? null;
|
|
30
|
+
}
|
|
31
|
+
supportsSpace(spaceKey) {
|
|
32
|
+
return this.content.has(spaceKey);
|
|
33
|
+
}
|
|
34
|
+
listSpaces() {
|
|
35
|
+
return [...this.content.keys()];
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
function createStaticRetriever(content) {
|
|
39
|
+
return new StaticRetriever({ content });
|
|
40
|
+
}
|
|
3
41
|
|
|
4
|
-
|
|
42
|
+
// src/retriever/vector-retriever.ts
|
|
43
|
+
class VectorRetriever {
|
|
44
|
+
config;
|
|
45
|
+
spaceCollections;
|
|
46
|
+
staticContent;
|
|
47
|
+
constructor(config) {
|
|
48
|
+
this.config = config;
|
|
49
|
+
this.spaceCollections = config.spaceCollections instanceof Map ? config.spaceCollections : new Map(Object.entries(config.spaceCollections));
|
|
50
|
+
this.staticContent = config.staticContent ? config.staticContent instanceof Map ? config.staticContent : new Map(Object.entries(config.staticContent)) : new Map;
|
|
51
|
+
}
|
|
52
|
+
async retrieve(query, options) {
|
|
53
|
+
const collection = this.spaceCollections.get(options.spaceKey);
|
|
54
|
+
if (!collection) {
|
|
55
|
+
return [];
|
|
56
|
+
}
|
|
57
|
+
const embedding = await this.config.embeddings.embedQuery(query);
|
|
58
|
+
const results = await this.config.vectorStore.search({
|
|
59
|
+
collection,
|
|
60
|
+
vector: embedding.vector,
|
|
61
|
+
topK: options.topK ?? this.config.defaultTopK ?? 5,
|
|
62
|
+
namespace: options.tenantId,
|
|
63
|
+
filter: options.filter
|
|
64
|
+
});
|
|
65
|
+
const minScore = options.minScore ?? this.config.defaultMinScore ?? 0;
|
|
66
|
+
const filtered = results.filter((r) => r.score >= minScore);
|
|
67
|
+
return filtered.map((result) => ({
|
|
68
|
+
content: this.extractContent(result.payload),
|
|
69
|
+
source: result.id,
|
|
70
|
+
score: result.score,
|
|
71
|
+
metadata: result.payload
|
|
72
|
+
}));
|
|
73
|
+
}
|
|
74
|
+
async getStatic(spaceKey) {
|
|
75
|
+
return this.staticContent.get(spaceKey) ?? null;
|
|
76
|
+
}
|
|
77
|
+
supportsSpace(spaceKey) {
|
|
78
|
+
return this.spaceCollections.has(spaceKey);
|
|
79
|
+
}
|
|
80
|
+
listSpaces() {
|
|
81
|
+
return [...this.spaceCollections.keys()];
|
|
82
|
+
}
|
|
83
|
+
extractContent(payload) {
|
|
84
|
+
if (!payload)
|
|
85
|
+
return "";
|
|
86
|
+
if (typeof payload.text === "string")
|
|
87
|
+
return payload.text;
|
|
88
|
+
if (typeof payload.content === "string")
|
|
89
|
+
return payload.content;
|
|
90
|
+
return JSON.stringify(payload);
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
function createVectorRetriever(config) {
|
|
94
|
+
return new VectorRetriever(config);
|
|
95
|
+
}
|
|
96
|
+
export {
|
|
97
|
+
createVectorRetriever,
|
|
98
|
+
createStaticRetriever,
|
|
99
|
+
VectorRetriever,
|
|
100
|
+
StaticRetriever
|
|
101
|
+
};
|
|
@@ -1,7 +1,4 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
3
|
-
//#region src/retriever/interface.d.ts
|
|
4
|
-
|
|
1
|
+
import type { RetrievalResult, RetrievalOptions } from '../types';
|
|
5
2
|
/**
|
|
6
3
|
* Unified interface for knowledge retrieval.
|
|
7
4
|
*
|
|
@@ -9,48 +6,46 @@ import { RetrievalOptions, RetrievalResult } from "../types.js";
|
|
|
9
6
|
* This interface is consumed by @contractspec/lib.ai-agent for both static injection
|
|
10
7
|
* and dynamic RAG tool queries.
|
|
11
8
|
*/
|
|
12
|
-
interface KnowledgeRetriever {
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
9
|
+
export interface KnowledgeRetriever {
|
|
10
|
+
/**
|
|
11
|
+
* Retrieve relevant content for a query using semantic search.
|
|
12
|
+
*
|
|
13
|
+
* @param query - The search query or question
|
|
14
|
+
* @param options - Retrieval options including space key and filters
|
|
15
|
+
* @returns Array of retrieval results sorted by relevance
|
|
16
|
+
*/
|
|
17
|
+
retrieve(query: string, options: RetrievalOptions): Promise<RetrievalResult[]>;
|
|
18
|
+
/**
|
|
19
|
+
* Get static content by space key (for required knowledge injection).
|
|
20
|
+
*
|
|
21
|
+
* Used for injecting required knowledge into agent system prompts
|
|
22
|
+
* without performing semantic search.
|
|
23
|
+
*
|
|
24
|
+
* @param spaceKey - The knowledge space key
|
|
25
|
+
* @returns The static content or null if not available
|
|
26
|
+
*/
|
|
27
|
+
getStatic(spaceKey: string): Promise<string | null>;
|
|
28
|
+
/**
|
|
29
|
+
* Check if this retriever supports a given knowledge space.
|
|
30
|
+
*
|
|
31
|
+
* @param spaceKey - The knowledge space key to check
|
|
32
|
+
* @returns True if the space is supported
|
|
33
|
+
*/
|
|
34
|
+
supportsSpace(spaceKey: string): boolean;
|
|
35
|
+
/**
|
|
36
|
+
* List all supported knowledge space keys.
|
|
37
|
+
*
|
|
38
|
+
* @returns Array of supported space keys
|
|
39
|
+
*/
|
|
40
|
+
listSpaces(): string[];
|
|
44
41
|
}
|
|
45
42
|
/**
|
|
46
43
|
* Configuration for creating a retriever.
|
|
47
44
|
*/
|
|
48
|
-
interface RetrieverConfig {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
45
|
+
export interface RetrieverConfig {
|
|
46
|
+
/** Default number of results to return */
|
|
47
|
+
defaultTopK?: number;
|
|
48
|
+
/** Default minimum score threshold */
|
|
49
|
+
defaultMinScore?: number;
|
|
53
50
|
}
|
|
54
|
-
//#endregion
|
|
55
|
-
export { KnowledgeRetriever, RetrieverConfig };
|
|
56
51
|
//# sourceMappingURL=interface.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"interface.d.ts","
|
|
1
|
+
{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["../../src/retriever/interface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAElE;;;;;;GAMG;AACH,MAAM,WAAW,kBAAkB;IACjC;;;;;;OAMG;IACH,QAAQ,CACN,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,eAAe,EAAE,CAAC,CAAC;IAE9B;;;;;;;;OAQG;IACH,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC,CAAC;IAEpD;;;;;OAKG;IACH,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC;IAEzC;;;;OAIG;IACH,UAAU,IAAI,MAAM,EAAE,CAAC;CACxB;AAED;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,0CAA0C;IAC1C,WAAW,CAAC,EAAE,MAAM,CAAC;IACrB,sCAAsC;IACtC,eAAe,CAAC,EAAE,MAAM,CAAC;CAC1B"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
// @bun
|
|
@@ -1,14 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import { KnowledgeRetriever, RetrieverConfig } from
|
|
3
|
-
|
|
4
|
-
//#region src/retriever/static-retriever.d.ts
|
|
5
|
-
|
|
1
|
+
import type { RetrievalResult, RetrievalOptions } from '../types';
|
|
2
|
+
import type { KnowledgeRetriever, RetrieverConfig } from './interface';
|
|
6
3
|
/**
|
|
7
4
|
* Configuration for the static retriever.
|
|
8
5
|
*/
|
|
9
|
-
interface StaticRetrieverConfig extends RetrieverConfig {
|
|
10
|
-
|
|
11
|
-
|
|
6
|
+
export interface StaticRetrieverConfig extends RetrieverConfig {
|
|
7
|
+
/** Map of space key to static content */
|
|
8
|
+
content: Map<string, string> | Record<string, string>;
|
|
12
9
|
}
|
|
13
10
|
/**
|
|
14
11
|
* A simple in-memory retriever for static knowledge content.
|
|
@@ -18,18 +15,16 @@ interface StaticRetrieverConfig extends RetrieverConfig {
|
|
|
18
15
|
* - Testing and development
|
|
19
16
|
* - Small knowledge bases that fit in memory
|
|
20
17
|
*/
|
|
21
|
-
declare class StaticRetriever implements KnowledgeRetriever {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
18
|
+
export declare class StaticRetriever implements KnowledgeRetriever {
|
|
19
|
+
private readonly content;
|
|
20
|
+
constructor(config: StaticRetrieverConfig);
|
|
21
|
+
retrieve(query: string, options: RetrievalOptions): Promise<RetrievalResult[]>;
|
|
22
|
+
getStatic(spaceKey: string): Promise<string | null>;
|
|
23
|
+
supportsSpace(spaceKey: string): boolean;
|
|
24
|
+
listSpaces(): string[];
|
|
28
25
|
}
|
|
29
26
|
/**
|
|
30
27
|
* Create a static retriever from a content map.
|
|
31
28
|
*/
|
|
32
|
-
declare function createStaticRetriever(content: Record<string, string> | Map<string, string>): StaticRetriever;
|
|
33
|
-
//#endregion
|
|
34
|
-
export { StaticRetriever, StaticRetrieverConfig, createStaticRetriever };
|
|
29
|
+
export declare function createStaticRetriever(content: Record<string, string> | Map<string, string>): StaticRetriever;
|
|
35
30
|
//# sourceMappingURL=static-retriever.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"static-retriever.d.ts","
|
|
1
|
+
{"version":3,"file":"static-retriever.d.ts","sourceRoot":"","sources":["../../src/retriever/static-retriever.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,eAAe,EAAE,gBAAgB,EAAE,MAAM,UAAU,CAAC;AAClE,OAAO,KAAK,EAAE,kBAAkB,EAAE,eAAe,EAAE,MAAM,aAAa,CAAC;AAEvE;;GAEG;AACH,MAAM,WAAW,qBAAsB,SAAQ,eAAe;IAC5D,yCAAyC;IACzC,OAAO,EAAE,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;CACvD;AAED;;;;;;;GAOG;AACH,qBAAa,eAAgB,YAAW,kBAAkB;IACxD,OAAO,CAAC,QAAQ,CAAC,OAAO,CAAsB;gBAElC,MAAM,EAAE,qBAAqB;IAOnC,QAAQ,CACZ,KAAK,EAAE,MAAM,EACb,OAAO,EAAE,gBAAgB,GACxB,OAAO,CAAC,eAAe,EAAE,CAAC;IAuBvB,SAAS,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,GAAG,IAAI,CAAC;IAIzD,aAAa,CAAC,QAAQ,EAAE,MAAM,GAAG,OAAO;IAIxC,UAAU,IAAI,MAAM,EAAE;CAGvB;AAED;;GAEG;AACH,wBAAgB,qBAAqB,CACnC,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,MAAM,CAAC,GACpD,eAAe,CAEjB"}
|